From: Juan Quintela <quintela@redhat.com>
To: qemu-devel@nongnu.org
Cc: "Richard Henderson" <richard.henderson@linaro.org>,
"Michael S. Tsirkin" <mst@redhat.com>,
"Laurent Vivier" <lvivier@redhat.com>,
"Ilya Leoshkevich" <iii@linux.ibm.com>,
"Halil Pasic" <pasic@linux.ibm.com>,
"Marc-André Lureau" <marcandre.lureau@redhat.com>,
"Coiby Xu" <Coiby.Xu@gmail.com>,
"Eric Farman" <farman@linux.ibm.com>,
"Alex Williamson" <alex.williamson@redhat.com>,
"Christian Borntraeger" <borntraeger@linux.ibm.com>,
"Stefan Hajnoczi" <stefanha@redhat.com>,
"Philippe Mathieu-Daudé" <philmd@linaro.org>,
"Stefan Berger" <stefanb@linux.vnet.ibm.com>,
"Eric Blake" <eblake@redhat.com>,
"Eduardo Habkost" <eduardo@habkost.net>,
"Dr. David Alan Gilbert" <dgilbert@redhat.com>,
"Thomas Huth" <thuth@redhat.com>,
"David Hildenbrand" <david@redhat.com>,
"Marcel Apfelbaum" <marcel.apfelbaum@gmail.com>,
"John Snow" <jsnow@redhat.com>,
"Yanan Wang" <wangyanan55@huawei.com>,
"Daniel P. Berrangé" <berrange@redhat.com>,
"Vladimir Sementsov-Ogievskiy" <vsementsov@yandex-team.ru>,
qemu-block@nongnu.org, "Paolo Bonzini" <pbonzini@redhat.com>,
"Juan Quintela" <quintela@redhat.com>,
"Fam Zheng" <fam@euphon.net>,
qemu-s390x@nongnu.org, "Peter Xu" <peterx@redhat.com>
Subject: [PULL 06/26] util/userfaultfd: Add uffd_open()
Date: Thu, 2 Feb 2023 17:06:20 +0100 [thread overview]
Message-ID: <20230202160640.2300-7-quintela@redhat.com> (raw)
In-Reply-To: <20230202160640.2300-1-quintela@redhat.com>
From: Peter Xu <peterx@redhat.com>
Add a helper to create the uffd handle.
Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Reviewed-by: Juan Quintela <quintela@redhat.com>
Signed-off-by: Peter Xu <peterx@redhat.com>
Signed-off-by: Juan Quintela <quintela@redhat.com>
---
include/qemu/userfaultfd.h | 8 ++++++++
migration/postcopy-ram.c | 11 +++++------
tests/qtest/migration-test.c | 3 ++-
util/userfaultfd.c | 13 +++++++++++--
4 files changed, 26 insertions(+), 9 deletions(-)
diff --git a/include/qemu/userfaultfd.h b/include/qemu/userfaultfd.h
index 6b74f92792..2101115f70 100644
--- a/include/qemu/userfaultfd.h
+++ b/include/qemu/userfaultfd.h
@@ -17,6 +17,14 @@
#include "exec/hwaddr.h"
#include <linux/userfaultfd.h>
+/**
+ * uffd_open(): Open an userfaultfd handle for current context.
+ *
+ * @flags: The flags we want to pass in when creating the handle.
+ *
+ * Returns: the uffd handle if >=0, or <0 if error happens.
+ */
+int uffd_open(int flags);
int uffd_query_features(uint64_t *features);
int uffd_create_fd(uint64_t features, bool non_blocking);
void uffd_close_fd(int uffd_fd);
diff --git a/migration/postcopy-ram.c b/migration/postcopy-ram.c
index b9a37ef255..0c55df0e52 100644
--- a/migration/postcopy-ram.c
+++ b/migration/postcopy-ram.c
@@ -37,6 +37,7 @@
#include "qemu-file.h"
#include "yank_functions.h"
#include "tls.h"
+#include "qemu/userfaultfd.h"
/* Arbitrary limit on size of each discard command,
* keeps them around ~200 bytes
@@ -226,11 +227,9 @@ static bool receive_ufd_features(uint64_t *features)
int ufd;
bool ret = true;
- /* if we are here __NR_userfaultfd should exists */
- ufd = syscall(__NR_userfaultfd, O_CLOEXEC);
+ ufd = uffd_open(O_CLOEXEC);
if (ufd == -1) {
- error_report("%s: syscall __NR_userfaultfd failed: %s", __func__,
- strerror(errno));
+ error_report("%s: uffd_open() failed: %s", __func__, strerror(errno));
return false;
}
@@ -375,7 +374,7 @@ bool postcopy_ram_supported_by_host(MigrationIncomingState *mis)
goto out;
}
- ufd = syscall(__NR_userfaultfd, O_CLOEXEC);
+ ufd = uffd_open(O_CLOEXEC);
if (ufd == -1) {
error_report("%s: userfaultfd not available: %s", __func__,
strerror(errno));
@@ -1160,7 +1159,7 @@ static int postcopy_temp_pages_setup(MigrationIncomingState *mis)
int postcopy_ram_incoming_setup(MigrationIncomingState *mis)
{
/* Open the fd for the kernel to give us userfaults */
- mis->userfault_fd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
+ mis->userfault_fd = uffd_open(O_CLOEXEC | O_NONBLOCK);
if (mis->userfault_fd == -1) {
error_report("%s: Failed to open userfault fd: %s", __func__,
strerror(errno));
diff --git a/tests/qtest/migration-test.c b/tests/qtest/migration-test.c
index 1dd32c9506..7a5d1922dd 100644
--- a/tests/qtest/migration-test.c
+++ b/tests/qtest/migration-test.c
@@ -62,13 +62,14 @@ static bool uffd_feature_thread_id;
#include <sys/eventfd.h>
#include <sys/ioctl.h>
#include <linux/userfaultfd.h>
+#include "qemu/userfaultfd.h"
static bool ufd_version_check(void)
{
struct uffdio_api api_struct;
uint64_t ioctl_mask;
- int ufd = syscall(__NR_userfaultfd, O_CLOEXEC);
+ int ufd = uffd_open(O_CLOEXEC);
if (ufd == -1) {
g_test_message("Skipping test: userfaultfd not available");
diff --git a/util/userfaultfd.c b/util/userfaultfd.c
index f1cd6af2b1..4953b3137d 100644
--- a/util/userfaultfd.c
+++ b/util/userfaultfd.c
@@ -19,6 +19,15 @@
#include <sys/syscall.h>
#include <sys/ioctl.h>
+int uffd_open(int flags)
+{
+#if defined(__NR_userfaultfd)
+ return syscall(__NR_userfaultfd, flags);
+#else
+ return -EINVAL;
+#endif
+}
+
/**
* uffd_query_features: query UFFD features
*
@@ -32,7 +41,7 @@ int uffd_query_features(uint64_t *features)
struct uffdio_api api_struct = { 0 };
int ret = -1;
- uffd_fd = syscall(__NR_userfaultfd, O_CLOEXEC);
+ uffd_fd = uffd_open(O_CLOEXEC);
if (uffd_fd < 0) {
trace_uffd_query_features_nosys(errno);
return -1;
@@ -69,7 +78,7 @@ int uffd_create_fd(uint64_t features, bool non_blocking)
uint64_t ioctl_mask = BIT(_UFFDIO_REGISTER) | BIT(_UFFDIO_UNREGISTER);
flags = O_CLOEXEC | (non_blocking ? O_NONBLOCK : 0);
- uffd_fd = syscall(__NR_userfaultfd, flags);
+ uffd_fd = uffd_open(flags);
if (uffd_fd < 0) {
trace_uffd_create_fd_nosys(errno);
return -1;
--
2.39.1
next prev parent reply other threads:[~2023-02-02 16:09 UTC|newest]
Thread overview: 31+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-02-02 16:06 [PULL 00/26] Next patches Juan Quintela
2023-02-02 16:06 ` [PULL 01/26] migration: Fix migration crash when target psize larger than host Juan Quintela
2023-02-02 16:06 ` [PULL 02/26] migration: No save_live_pending() method uses the QEMUFile parameter Juan Quintela
2023-02-02 16:06 ` [PULL 03/26] migration: Split save_live_pending() into state_pending_* Juan Quintela
2023-02-02 16:06 ` [PULL 04/26] migration: Remove unused threshold_size parameter Juan Quintela
2023-02-02 16:06 ` [PULL 05/26] migration: simplify migration_iteration_run() Juan Quintela
2023-02-02 16:06 ` Juan Quintela [this message]
2023-02-02 16:06 ` [PULL 07/26] migration/ram: Fix populate_read_range() Juan Quintela
2023-02-02 16:06 ` [PULL 08/26] migration/ram: Fix error handling in ram_write_tracking_start() Juan Quintela
2023-02-02 16:06 ` [PULL 09/26] migration/ram: Don't explicitly unprotect when unregistering uffd-wp Juan Quintela
2023-02-02 16:06 ` [PULL 10/26] migration/ram: Rely on used_length for uffd_change_protection() Juan Quintela
2023-02-02 16:06 ` [PULL 11/26] migration/ram: Optimize ram_write_tracking_start() for RamDiscardManager Juan Quintela
2023-02-02 16:06 ` [PULL 12/26] migration/savevm: Move more savevm handling into vmstate_save() Juan Quintela
2023-02-02 16:06 ` [PULL 13/26] migration/savevm: Prepare vmdesc json writer in qemu_savevm_state_setup() Juan Quintela
2023-02-02 16:06 ` [PULL 14/26] migration/savevm: Allow immutable device state to be migrated early (i.e., before RAM) Juan Quintela
2023-02-02 16:06 ` [PULL 15/26] migration/vmstate: Introduce VMSTATE_WITH_TMP_TEST() and VMSTATE_BITMAP_TEST() Juan Quintela
2023-02-02 16:06 ` [PULL 16/26] migration/ram: Factor out check for advised postcopy Juan Quintela
2023-02-02 16:06 ` [PULL 17/26] virtio-mem: Fail if a memory backend with "prealloc=on" is specified Juan Quintela
2023-02-02 16:06 ` [PULL 18/26] virtio-mem: Migrate immutable properties early Juan Quintela
2023-02-02 16:06 ` [PULL 19/26] virtio-mem: Proper support for preallocation with migration Juan Quintela
2023-02-02 16:06 ` [PULL 20/26] migration: Show downtime during postcopy phase Juan Quintela
2023-02-02 16:06 ` [PULL 21/26] migration/rdma: fix return value for qio_channel_rdma_{readv, writev} Juan Quintela
2023-02-02 16:06 ` [PULL 22/26] migration: Add canary to VMSTATE_END_OF_LIST Juan Quintela
2023-02-02 16:06 ` [PULL 23/26] migration: Perform vmsd structure check during tests Juan Quintela
2023-02-02 16:06 ` [PULL 24/26] migration/dirtyrate: Show sample pages only in page-sampling mode Juan Quintela
2023-02-02 16:06 ` [PULL 25/26] io: Add support for MSG_PEEK for socket channel Juan Quintela
2023-02-02 16:06 ` [PULL 26/26] migration: check magic value for deciding the mapping of channels Juan Quintela
2023-02-04 10:19 ` [PULL 00/26] Next patches Peter Maydell
2023-02-06 22:06 ` Peter Xu
2023-02-06 23:33 ` Juan Quintela
2023-02-07 0:49 ` Juan Quintela
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=20230202160640.2300-7-quintela@redhat.com \
--to=quintela@redhat.com \
--cc=Coiby.Xu@gmail.com \
--cc=alex.williamson@redhat.com \
--cc=berrange@redhat.com \
--cc=borntraeger@linux.ibm.com \
--cc=david@redhat.com \
--cc=dgilbert@redhat.com \
--cc=eblake@redhat.com \
--cc=eduardo@habkost.net \
--cc=fam@euphon.net \
--cc=farman@linux.ibm.com \
--cc=iii@linux.ibm.com \
--cc=jsnow@redhat.com \
--cc=lvivier@redhat.com \
--cc=marcandre.lureau@redhat.com \
--cc=marcel.apfelbaum@gmail.com \
--cc=mst@redhat.com \
--cc=pasic@linux.ibm.com \
--cc=pbonzini@redhat.com \
--cc=peterx@redhat.com \
--cc=philmd@linaro.org \
--cc=qemu-block@nongnu.org \
--cc=qemu-devel@nongnu.org \
--cc=qemu-s390x@nongnu.org \
--cc=richard.henderson@linaro.org \
--cc=stefanb@linux.vnet.ibm.com \
--cc=stefanha@redhat.com \
--cc=thuth@redhat.com \
--cc=vsementsov@yandex-team.ru \
--cc=wangyanan55@huawei.com \
/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).