From: John Hubbard <jhubbard@nvidia.com>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: David Hildenbrand <david@redhat.com>,
Peter Xu <peterx@redhat.com>, "Shuah Khan" <shuah@kernel.org>,
Nathan Chancellor <nathan@kernel.org>, <linux-mm@kvack.org>,
<linux-kselftest@vger.kernel.org>,
LKML <linux-kernel@vger.kernel.org>,
John Hubbard <jhubbard@nvidia.com>
Subject: [PATCH v3 09/11] selftests/mm: move certain uffd*() routines from vm_util.c to uffd-common.c
Date: Tue, 6 Jun 2023 00:16:35 -0700 [thread overview]
Message-ID: <20230606071637.267103-10-jhubbard@nvidia.com> (raw)
In-Reply-To: <20230606071637.267103-1-jhubbard@nvidia.com>
There are only three uffd*() routines that are used outside of the uffd
selftests. Leave these in vm_util.c, where they are available to any mm
selftest program:
uffd_register()
uffd_unregister()
uffd_register_with_ioctls().
A few other uffd*() routines, however, are only used by the uffd-focused
tests found in uffd-stress.c and uffd-unit-tests.c. Move those routines
into uffd-common.c.
Cc: Peter Xu <peterx@redhat.com>
Acked-by: David Hildenbrand <david@redhat.com>
Signed-off-by: John Hubbard <jhubbard@nvidia.com>
---
tools/testing/selftests/mm/uffd-common.c | 59 ++++++++++++++++++++++++
tools/testing/selftests/mm/uffd-common.h | 5 ++
tools/testing/selftests/mm/vm_util.c | 59 ------------------------
tools/testing/selftests/mm/vm_util.h | 4 --
4 files changed, 64 insertions(+), 63 deletions(-)
diff --git a/tools/testing/selftests/mm/uffd-common.c b/tools/testing/selftests/mm/uffd-common.c
index 61c6250adf93..ba20d7504022 100644
--- a/tools/testing/selftests/mm/uffd-common.c
+++ b/tools/testing/selftests/mm/uffd-common.c
@@ -616,3 +616,62 @@ int copy_page(int ufd, unsigned long offset, bool wp)
{
return __copy_page(ufd, offset, false, wp);
}
+
+int uffd_open_dev(unsigned int flags)
+{
+ int fd, uffd;
+
+ fd = open("/dev/userfaultfd", O_RDWR | O_CLOEXEC);
+ if (fd < 0)
+ return fd;
+ uffd = ioctl(fd, USERFAULTFD_IOC_NEW, flags);
+ close(fd);
+
+ return uffd;
+}
+
+int uffd_open_sys(unsigned int flags)
+{
+#ifdef __NR_userfaultfd
+ return syscall(__NR_userfaultfd, flags);
+#else
+ return -1;
+#endif
+}
+
+int uffd_open(unsigned int flags)
+{
+ int uffd = uffd_open_sys(flags);
+
+ if (uffd < 0)
+ uffd = uffd_open_dev(flags);
+
+ return uffd;
+}
+
+int uffd_get_features(uint64_t *features)
+{
+ struct uffdio_api uffdio_api = { .api = UFFD_API, .features = 0 };
+ /*
+ * This should by default work in most kernels; the feature list
+ * will be the same no matter what we pass in here.
+ */
+ int fd = uffd_open(UFFD_USER_MODE_ONLY);
+
+ if (fd < 0)
+ /* Maybe the kernel is older than user-only mode? */
+ fd = uffd_open(0);
+
+ if (fd < 0)
+ return fd;
+
+ if (ioctl(fd, UFFDIO_API, &uffdio_api)) {
+ close(fd);
+ return -errno;
+ }
+
+ *features = uffdio_api.features;
+ close(fd);
+
+ return 0;
+}
diff --git a/tools/testing/selftests/mm/uffd-common.h b/tools/testing/selftests/mm/uffd-common.h
index 6068f2346b86..197f5262fe0d 100644
--- a/tools/testing/selftests/mm/uffd-common.h
+++ b/tools/testing/selftests/mm/uffd-common.h
@@ -110,6 +110,11 @@ int __copy_page(int ufd, unsigned long offset, bool retry, bool wp);
int copy_page(int ufd, unsigned long offset, bool wp);
void *uffd_poll_thread(void *arg);
+int uffd_open_dev(unsigned int flags);
+int uffd_open_sys(unsigned int flags);
+int uffd_open(unsigned int flags);
+int uffd_get_features(uint64_t *features);
+
#define TEST_ANON 1
#define TEST_HUGETLB 2
#define TEST_SHMEM 3
diff --git a/tools/testing/selftests/mm/vm_util.c b/tools/testing/selftests/mm/vm_util.c
index 9b06a5034808..681277615839 100644
--- a/tools/testing/selftests/mm/vm_util.c
+++ b/tools/testing/selftests/mm/vm_util.c
@@ -242,62 +242,3 @@ int uffd_unregister(int uffd, void *addr, uint64_t len)
return ret;
}
-
-int uffd_open_dev(unsigned int flags)
-{
- int fd, uffd;
-
- fd = open("/dev/userfaultfd", O_RDWR | O_CLOEXEC);
- if (fd < 0)
- return fd;
- uffd = ioctl(fd, USERFAULTFD_IOC_NEW, flags);
- close(fd);
-
- return uffd;
-}
-
-int uffd_open_sys(unsigned int flags)
-{
-#ifdef __NR_userfaultfd
- return syscall(__NR_userfaultfd, flags);
-#else
- return -1;
-#endif
-}
-
-int uffd_open(unsigned int flags)
-{
- int uffd = uffd_open_sys(flags);
-
- if (uffd < 0)
- uffd = uffd_open_dev(flags);
-
- return uffd;
-}
-
-int uffd_get_features(uint64_t *features)
-{
- struct uffdio_api uffdio_api = { .api = UFFD_API, .features = 0 };
- /*
- * This should by default work in most kernels; the feature list
- * will be the same no matter what we pass in here.
- */
- int fd = uffd_open(UFFD_USER_MODE_ONLY);
-
- if (fd < 0)
- /* Maybe the kernel is older than user-only mode? */
- fd = uffd_open(0);
-
- if (fd < 0)
- return fd;
-
- if (ioctl(fd, UFFDIO_API, &uffdio_api)) {
- close(fd);
- return -errno;
- }
-
- *features = uffdio_api.features;
- close(fd);
-
- return 0;
-}
diff --git a/tools/testing/selftests/mm/vm_util.h b/tools/testing/selftests/mm/vm_util.h
index 07f39ed2efba..c2d4ff798b91 100644
--- a/tools/testing/selftests/mm/vm_util.h
+++ b/tools/testing/selftests/mm/vm_util.h
@@ -48,10 +48,6 @@ unsigned long default_huge_page_size(void);
int uffd_register(int uffd, void *addr, uint64_t len,
bool miss, bool wp, bool minor);
int uffd_unregister(int uffd, void *addr, uint64_t len);
-int uffd_open_dev(unsigned int flags);
-int uffd_open_sys(unsigned int flags);
-int uffd_open(unsigned int flags);
-int uffd_get_features(uint64_t *features);
int uffd_register_with_ioctls(int uffd, void *addr, uint64_t len,
bool miss, bool wp, bool minor, uint64_t *ioctls);
--
2.40.1
next prev parent reply other threads:[~2023-06-06 7:18 UTC|newest]
Thread overview: 39+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-06-06 7:16 [PATCH v3 00/11] A minor flurry of selftest/mm fixes John Hubbard
2023-06-06 7:16 ` [PATCH v3 01/11] selftests/mm: fix uffd-stress unused function warning John Hubbard
2023-06-06 7:46 ` Muhammad Usama Anjum
2023-06-06 7:16 ` [PATCH v3 02/11] selftests/mm: fix unused variable warnings in hugetlb-madvise.c, migration.c John Hubbard
2023-06-06 7:48 ` Muhammad Usama Anjum
2023-06-06 7:16 ` [PATCH v3 03/11] selftests/mm: fix "warning: expression which evaluates to zero..." in mlock2-tests.c John Hubbard
2023-06-06 7:49 ` Muhammad Usama Anjum
2023-06-06 7:16 ` [PATCH v3 04/11] selftests/mm: fix invocation of tests that are run via shell scripts John Hubbard
2023-06-06 7:51 ` Muhammad Usama Anjum
2023-06-06 7:16 ` [PATCH v3 05/11] selftests/mm: .gitignore: add mkdirty, va_high_addr_switch John Hubbard
2023-06-06 7:52 ` Muhammad Usama Anjum
2023-06-06 7:16 ` [PATCH v3 06/11] selftests/mm: fix two -Wformat-security warnings in uffd builds John Hubbard
2023-06-06 7:54 ` Muhammad Usama Anjum
2023-06-06 7:16 ` [PATCH v3 07/11] selftests/mm: fix a "possibly uninitialized" warning in pkey-x86.h John Hubbard
2023-06-06 7:55 ` Muhammad Usama Anjum
2023-06-06 7:16 ` [PATCH v3 08/11] selftests/mm: fix build failures due to missing MADV_COLLAPSE John Hubbard
2023-06-06 7:55 ` Muhammad Usama Anjum
2023-06-06 7:16 ` John Hubbard [this message]
2023-06-06 7:56 ` [PATCH v3 09/11] selftests/mm: move certain uffd*() routines from vm_util.c to uffd-common.c Muhammad Usama Anjum
2023-06-06 7:16 ` [PATCH v3 10/11] Documentation: kselftest: "make headers" is a prerequisite John Hubbard
2023-06-06 7:57 ` Muhammad Usama Anjum
2023-07-10 14:20 ` Mark Brown
2023-06-06 7:16 ` [PATCH v3 11/11] selftests: error out if kernel header files are not yet built John Hubbard
2023-06-06 7:38 ` Muhammad Usama Anjum
2023-06-06 20:10 ` John Hubbard
2023-06-07 5:37 ` Muhammad Usama Anjum
2023-06-06 7:57 ` Muhammad Usama Anjum
2023-11-03 12:16 ` Peter Zijlstra
2023-11-03 12:22 ` David Hildenbrand
2023-11-03 12:46 ` Peter Zijlstra
2023-11-03 12:59 ` David Hildenbrand
2023-11-03 13:00 ` David Hildenbrand
2023-11-03 13:08 ` Peter Zijlstra
2023-12-08 15:14 ` Peter Zijlstra
2023-12-08 15:21 ` David Hildenbrand
2023-12-08 20:29 ` John Hubbard
2023-12-08 22:10 ` Peter Zijlstra
2023-12-09 1:39 ` John Hubbard
2023-12-08 12:44 ` Miroslav Benes
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=20230606071637.267103-10-jhubbard@nvidia.com \
--to=jhubbard@nvidia.com \
--cc=akpm@linux-foundation.org \
--cc=david@redhat.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=nathan@kernel.org \
--cc=peterx@redhat.com \
--cc=shuah@kernel.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