thebeanogamer / rpms / qemu-kvm

Forked from rpms/qemu-kvm 5 months ago
Clone
ed5979
From 80445fed73a7d1a87e8ce96f6cb7d505e437f845 Mon Sep 17 00:00:00 2001
ed5979
From: Peter Xu <peterx@redhat.com>
ed5979
Date: Wed, 1 Feb 2023 16:10:54 -0500
ed5979
Subject: [PATCH 4/8] util/userfaultfd: Add uffd_open()
ed5979
MIME-Version: 1.0
ed5979
Content-Type: text/plain; charset=UTF-8
ed5979
Content-Transfer-Encoding: 8bit
ed5979
ed5979
RH-Author: Peter Xu <peterx@redhat.com>
ed5979
RH-MergeRequest: 149: Support /dev/userfaultfd
ed5979
RH-Bugzilla: 2158704
ed5979
RH-Acked-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
ed5979
RH-Acked-by: quintela1 <quintela@redhat.com>
ed5979
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
ed5979
RH-Commit: [2/3] 4c81696314ab26db47c3415fa2c2501c6a572b5c (peterx/qemu-kvm)
ed5979
ed5979
Add a helper to create the uffd handle.
ed5979
ed5979
Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
ed5979
Reviewed-by: Juan Quintela <quintela@redhat.com>
ed5979
Signed-off-by: Peter Xu <peterx@redhat.com>
ed5979
Signed-off-by: Juan Quintela <quintela@redhat.com>
ed5979
(cherry picked from commit d5890ea0722831eea76a0efd23a496b3e8815fe8)
ed5979
Signed-off-by: Peter Xu <peterx@redhat.com>
ed5979
---
ed5979
 include/qemu/userfaultfd.h   | 12 ++++++++++++
ed5979
 migration/postcopy-ram.c     | 11 +++++------
ed5979
 tests/qtest/migration-test.c |  4 ++--
ed5979
 util/userfaultfd.c           | 13 +++++++++++--
ed5979
 4 files changed, 30 insertions(+), 10 deletions(-)
ed5979
ed5979
diff --git a/include/qemu/userfaultfd.h b/include/qemu/userfaultfd.h
ed5979
index 6b74f92792..d764496f0b 100644
ed5979
--- a/include/qemu/userfaultfd.h
ed5979
+++ b/include/qemu/userfaultfd.h
ed5979
@@ -13,10 +13,20 @@
ed5979
 #ifndef USERFAULTFD_H
ed5979
 #define USERFAULTFD_H
ed5979
 
ed5979
+#ifdef CONFIG_LINUX
ed5979
+
ed5979
 #include "qemu/osdep.h"
ed5979
 #include "exec/hwaddr.h"
ed5979
 #include <linux/userfaultfd.h>
ed5979
 
ed5979
+/**
ed5979
+ * uffd_open(): Open an userfaultfd handle for current context.
ed5979
+ *
ed5979
+ * @flags: The flags we want to pass in when creating the handle.
ed5979
+ *
ed5979
+ * Returns: the uffd handle if >=0, or <0 if error happens.
ed5979
+ */
ed5979
+int uffd_open(int flags);
ed5979
 int uffd_query_features(uint64_t *features);
ed5979
 int uffd_create_fd(uint64_t features, bool non_blocking);
ed5979
 void uffd_close_fd(int uffd_fd);
ed5979
@@ -32,4 +42,6 @@ int uffd_wakeup(int uffd_fd, void *addr, uint64_t length);
ed5979
 int uffd_read_events(int uffd_fd, struct uffd_msg *msgs, int count);
ed5979
 bool uffd_poll_events(int uffd_fd, int tmo);
ed5979
 
ed5979
+#endif /* CONFIG_LINUX */
ed5979
+
ed5979
 #endif /* USERFAULTFD_H */
ed5979
diff --git a/migration/postcopy-ram.c b/migration/postcopy-ram.c
ed5979
index b9a37ef255..0c55df0e52 100644
ed5979
--- a/migration/postcopy-ram.c
ed5979
+++ b/migration/postcopy-ram.c
ed5979
@@ -37,6 +37,7 @@
ed5979
 #include "qemu-file.h"
ed5979
 #include "yank_functions.h"
ed5979
 #include "tls.h"
ed5979
+#include "qemu/userfaultfd.h"
ed5979
 
ed5979
 /* Arbitrary limit on size of each discard command,
ed5979
  * keeps them around ~200 bytes
ed5979
@@ -226,11 +227,9 @@ static bool receive_ufd_features(uint64_t *features)
ed5979
     int ufd;
ed5979
     bool ret = true;
ed5979
 
ed5979
-    /* if we are here __NR_userfaultfd should exists */
ed5979
-    ufd = syscall(__NR_userfaultfd, O_CLOEXEC);
ed5979
+    ufd = uffd_open(O_CLOEXEC);
ed5979
     if (ufd == -1) {
ed5979
-        error_report("%s: syscall __NR_userfaultfd failed: %s", __func__,
ed5979
-                     strerror(errno));
ed5979
+        error_report("%s: uffd_open() failed: %s", __func__, strerror(errno));
ed5979
         return false;
ed5979
     }
ed5979
 
ed5979
@@ -375,7 +374,7 @@ bool postcopy_ram_supported_by_host(MigrationIncomingState *mis)
ed5979
         goto out;
ed5979
     }
ed5979
 
ed5979
-    ufd = syscall(__NR_userfaultfd, O_CLOEXEC);
ed5979
+    ufd = uffd_open(O_CLOEXEC);
ed5979
     if (ufd == -1) {
ed5979
         error_report("%s: userfaultfd not available: %s", __func__,
ed5979
                      strerror(errno));
ed5979
@@ -1160,7 +1159,7 @@ static int postcopy_temp_pages_setup(MigrationIncomingState *mis)
ed5979
 int postcopy_ram_incoming_setup(MigrationIncomingState *mis)
ed5979
 {
ed5979
     /* Open the fd for the kernel to give us userfaults */
ed5979
-    mis->userfault_fd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
ed5979
+    mis->userfault_fd = uffd_open(O_CLOEXEC | O_NONBLOCK);
ed5979
     if (mis->userfault_fd == -1) {
ed5979
         error_report("%s: Failed to open userfault fd: %s", __func__,
ed5979
                      strerror(errno));
ed5979
diff --git a/tests/qtest/migration-test.c b/tests/qtest/migration-test.c
ed5979
index dbde726adf..0100e1bdbc 100644
ed5979
--- a/tests/qtest/migration-test.c
ed5979
+++ b/tests/qtest/migration-test.c
ed5979
@@ -61,14 +61,14 @@ static bool uffd_feature_thread_id;
ed5979
 #if defined(__linux__) && defined(__NR_userfaultfd) && defined(CONFIG_EVENTFD)
ed5979
 #include <sys/eventfd.h>
ed5979
 #include <sys/ioctl.h>
ed5979
-#include <linux/userfaultfd.h>
ed5979
+#include "qemu/userfaultfd.h"
ed5979
 
ed5979
 static bool ufd_version_check(void)
ed5979
 {
ed5979
     struct uffdio_api api_struct;
ed5979
     uint64_t ioctl_mask;
ed5979
 
ed5979
-    int ufd = syscall(__NR_userfaultfd, O_CLOEXEC);
ed5979
+    int ufd = uffd_open(O_CLOEXEC);
ed5979
 
ed5979
     if (ufd == -1) {
ed5979
         g_test_message("Skipping test: userfaultfd not available");
ed5979
diff --git a/util/userfaultfd.c b/util/userfaultfd.c
ed5979
index f1cd6af2b1..4953b3137d 100644
ed5979
--- a/util/userfaultfd.c
ed5979
+++ b/util/userfaultfd.c
ed5979
@@ -19,6 +19,15 @@
ed5979
 #include <sys/syscall.h>
ed5979
 #include <sys/ioctl.h>
ed5979
 
ed5979
+int uffd_open(int flags)
ed5979
+{
ed5979
+#if defined(__NR_userfaultfd)
ed5979
+    return syscall(__NR_userfaultfd, flags);
ed5979
+#else
ed5979
+    return -EINVAL;
ed5979
+#endif
ed5979
+}
ed5979
+
ed5979
 /**
ed5979
  * uffd_query_features: query UFFD features
ed5979
  *
ed5979
@@ -32,7 +41,7 @@ int uffd_query_features(uint64_t *features)
ed5979
     struct uffdio_api api_struct = { 0 };
ed5979
     int ret = -1;
ed5979
 
ed5979
-    uffd_fd = syscall(__NR_userfaultfd, O_CLOEXEC);
ed5979
+    uffd_fd = uffd_open(O_CLOEXEC);
ed5979
     if (uffd_fd < 0) {
ed5979
         trace_uffd_query_features_nosys(errno);
ed5979
         return -1;
ed5979
@@ -69,7 +78,7 @@ int uffd_create_fd(uint64_t features, bool non_blocking)
ed5979
     uint64_t ioctl_mask = BIT(_UFFDIO_REGISTER) | BIT(_UFFDIO_UNREGISTER);
ed5979
 
ed5979
     flags = O_CLOEXEC | (non_blocking ? O_NONBLOCK : 0);
ed5979
-    uffd_fd = syscall(__NR_userfaultfd, flags);
ed5979
+    uffd_fd = uffd_open(flags);
ed5979
     if (uffd_fd < 0) {
ed5979
         trace_uffd_create_fd_nosys(errno);
ed5979
         return -1;
ed5979
-- 
ed5979
2.31.1
ed5979