qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Juan Quintela <quintela@redhat.com>
To: qemu-devel@nongnu.org
Cc: dgilbert@redhat.com, lvivier@redhat.com, peterx@redhat.com,
	Alexey Perevalov <a.perevalov@samsung.com>
Subject: [Qemu-devel] [PULL 15/16] migration: split ufd_version_check onto receive/request features part
Date: Wed, 20 Sep 2017 12:46:38 +0200	[thread overview]
Message-ID: <20170920104639.2335-16-quintela@redhat.com> (raw)
In-Reply-To: <20170920104639.2335-1-quintela@redhat.com>

From: Alexey Perevalov <a.perevalov@samsung.com>

This modification is necessary for userfault fd features which are
required to be requested from userspace.
UFFD_FEATURE_THREAD_ID is a one of such "on demand" feature, which will
be introduced in the next patch.

QEMU have to use separate userfault file descriptor, due to
userfault context has internal state, and after first call of
ioctl UFFD_API it changes its state to UFFD_STATE_RUNNING (in case of
success), but kernel while handling ioctl UFFD_API expects UFFD_STATE_WAIT_API.
So only one ioctl with UFFD_API is possible per ufd.

Reviewed-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
Signed-off-by: Alexey Perevalov <a.perevalov@samsung.com>
Signed-off-by: Juan Quintela <quintela@redhat.com>
---
 migration/postcopy-ram.c | 94 ++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 88 insertions(+), 6 deletions(-)

diff --git a/migration/postcopy-ram.c b/migration/postcopy-ram.c
index cf62fc756f..0de68e8b25 100644
--- a/migration/postcopy-ram.c
+++ b/migration/postcopy-ram.c
@@ -61,16 +61,67 @@ struct PostcopyDiscardState {
 #include <sys/eventfd.h>
 #include <linux/userfaultfd.h>
 
-static bool ufd_version_check(int ufd, MigrationIncomingState *mis)
+
+/**
+ * receive_ufd_features: check userfault fd features, to request only supported
+ * features in the future.
+ *
+ * Returns: true on success
+ *
+ * __NR_userfaultfd - should be checked before
+ *  @features: out parameter will contain uffdio_api.features provided by kernel
+ *              in case of success
+ */
+static bool receive_ufd_features(uint64_t *features)
 {
-    struct uffdio_api api_struct;
-    uint64_t ioctl_mask;
+    struct uffdio_api api_struct = {0};
+    int ufd;
+    bool ret = true;
 
+    /* if we are here __NR_userfaultfd should exists */
+    ufd = syscall(__NR_userfaultfd, O_CLOEXEC);
+    if (ufd == -1) {
+        error_report("%s: syscall __NR_userfaultfd failed: %s", __func__,
+                     strerror(errno));
+        return false;
+    }
+
+    /* ask features */
     api_struct.api = UFFD_API;
     api_struct.features = 0;
     if (ioctl(ufd, UFFDIO_API, &api_struct)) {
         error_report("%s: UFFDIO_API failed: %s", __func__,
                      strerror(errno));
+        ret = false;
+        goto release_ufd;
+    }
+
+    *features = api_struct.features;
+
+release_ufd:
+    close(ufd);
+    return ret;
+}
+
+/**
+ * request_ufd_features: this function should be called only once on a newly
+ * opened ufd, subsequent calls will lead to error.
+ *
+ * Returns: true on succes
+ *
+ * @ufd: fd obtained from userfaultfd syscall
+ * @features: bit mask see UFFD_API_FEATURES
+ */
+static bool request_ufd_features(int ufd, uint64_t features)
+{
+    struct uffdio_api api_struct = {0};
+    uint64_t ioctl_mask;
+
+    api_struct.api = UFFD_API;
+    api_struct.features = features;
+    if (ioctl(ufd, UFFDIO_API, &api_struct)) {
+        error_report("%s failed: UFFDIO_API failed: %s", __func__,
+                     strerror(errno));
         return false;
     }
 
@@ -82,11 +133,42 @@ static bool ufd_version_check(int ufd, MigrationIncomingState *mis)
         return false;
     }
 
+    return true;
+}
+
+static bool ufd_check_and_apply(int ufd, MigrationIncomingState *mis)
+{
+    uint64_t asked_features = 0;
+    static uint64_t supported_features;
+
+    /*
+     * it's not possible to
+     * request UFFD_API twice per one fd
+     * userfault fd features is persistent
+     */
+    if (!supported_features) {
+        if (!receive_ufd_features(&supported_features)) {
+            error_report("%s failed", __func__);
+            return false;
+        }
+    }
+
+    /*
+     * request features, even if asked_features is 0, due to
+     * kernel expects UFFD_API before UFFDIO_REGISTER, per
+     * userfault file descriptor
+     */
+    if (!request_ufd_features(ufd, asked_features)) {
+        error_report("%s failed: features %" PRIu64, __func__,
+                     asked_features);
+        return false;
+    }
+
     if (getpagesize() != ram_pagesize_summary()) {
         bool have_hp = false;
         /* We've got a huge page */
 #ifdef UFFD_FEATURE_MISSING_HUGETLBFS
-        have_hp = api_struct.features & UFFD_FEATURE_MISSING_HUGETLBFS;
+        have_hp = supported_features & UFFD_FEATURE_MISSING_HUGETLBFS;
 #endif
         if (!have_hp) {
             error_report("Userfault on this host does not support huge pages");
@@ -147,7 +229,7 @@ bool postcopy_ram_supported_by_host(MigrationIncomingState *mis)
     }
 
     /* Version and features check */
-    if (!ufd_version_check(ufd, mis)) {
+    if (!ufd_check_and_apply(ufd, mis)) {
         goto out;
     }
 
@@ -523,7 +605,7 @@ int postcopy_ram_enable_notify(MigrationIncomingState *mis)
      * Although the host check already tested the API, we need to
      * do the check again as an ABI handshake on the new fd.
      */
-    if (!ufd_version_check(mis->userfault_fd, mis)) {
+    if (!ufd_check_and_apply(mis->userfault_fd, mis)) {
         return -1;
     }
 
-- 
2.13.5

  parent reply	other threads:[~2017-09-20 13:17 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-09-20 10:46 [Qemu-devel] [PULL 00/16] Migration PULL request Juan Quintela
2017-09-20 10:46 ` [Qemu-devel] [PULL 01/16] migration: Create migration_ioc_process_incoming() Juan Quintela
2017-09-20 10:46 ` [Qemu-devel] [PULL 02/16] migration: Teach it about G_SOURCE_REMOVE Juan Quintela
2017-09-20 10:46 ` [Qemu-devel] [PULL 03/16] migration: Add comments to channel functions Juan Quintela
2017-09-20 10:46 ` [Qemu-devel] [PULL 04/16] migration: Create migration_has_all_channels Juan Quintela
2017-09-20 10:46 ` [Qemu-devel] [PULL 05/16] migration: Add multifd capability Juan Quintela
2017-09-20 10:46 ` [Qemu-devel] [PULL 06/16] migration: Create x-multifd-channels parameter Juan Quintela
2017-09-20 10:46 ` [Qemu-devel] [PULL 07/16] migration: Create x-multifd-page-count parameter Juan Quintela
2017-09-20 13:59   ` Eric Blake
2017-09-20 10:46 ` [Qemu-devel] [PULL 08/16] migration: Create multifd migration threads Juan Quintela
2017-09-20 10:46 ` [Qemu-devel] [PULL 09/16] migration: Split migration_fd_process_incoming Juan Quintela
2017-09-20 10:46 ` [Qemu-devel] [PULL 10/16] bitmap: remove BITOP_WORD() Juan Quintela
2017-09-20 10:46 ` [Qemu-devel] [PULL 11/16] bitmap: introduce bitmap_count_one() Juan Quintela
2017-09-20 10:46 ` [Qemu-devel] [PULL 12/16] bitmap: provide to_le/from_le helpers Juan Quintela
2017-09-20 10:46 ` [Qemu-devel] [PULL 13/16] migration: pass MigrationIncomingState* into migration check functions Juan Quintela
2017-09-20 10:46 ` [Qemu-devel] [PULL 14/16] migration: fix hardcoded function name in error report Juan Quintela
2017-09-20 10:46 ` Juan Quintela [this message]
2017-09-20 10:46 ` [Qemu-devel] [PULL 16/16] migration: introduce postcopy-blocktime capability Juan Quintela
2017-09-20 11:42 ` [Qemu-devel] [PULL 00/16] Migration PULL request Juan Quintela
2017-09-20 19:32   ` Peter Maydell
2017-09-21  4:10     ` Peter Xu
2017-09-21  4:21       ` Peter Xu
2017-09-20 14:55 ` no-reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170920104639.2335-16-quintela@redhat.com \
    --to=quintela@redhat.com \
    --cc=a.perevalov@samsung.com \
    --cc=dgilbert@redhat.com \
    --cc=lvivier@redhat.com \
    --cc=peterx@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).