diff mbox series

[v2,10/11] selftests/mm: move uffd* routines from vm_util.c to uffd-common.c

Message ID 20230603021558.95299-11-jhubbard@nvidia.com (mailing list archive)
State New
Headers show
Series A minor flurry of selftest/mm fixes | expand

Commit Message

John Hubbard June 3, 2023, 2:15 a.m. UTC
Move the uffd*() routines to their natural home. Note that
ksm_functional_tests.c also depend, intentionally (due to a recent
commit [1]), upon uffd-common.[ch].

Cc: David Hildenbrand <david@redhat.com>
Cc: Peter Xu <peterx@redhat.com>
Signed-off-by: John Hubbard <jhubbard@nvidia.com>
---
 tools/testing/selftests/mm/Makefile           |   7 +-
 tools/testing/selftests/mm/hugepage-mremap.c  |   2 +-
 .../selftests/mm/ksm_functional_tests.c       |   2 +-
 tools/testing/selftests/mm/uffd-common.c      | 105 ++++++++++++++++++
 tools/testing/selftests/mm/uffd-common.h      |  12 +-
 tools/testing/selftests/mm/vm_util.c          | 104 -----------------
 tools/testing/selftests/mm/vm_util.h          |  10 --
 7 files changed, 122 insertions(+), 120 deletions(-)

Comments

David Hildenbrand June 5, 2023, 11:38 a.m. UTC | #1
On 03.06.23 04:15, John Hubbard wrote:
> Move the uffd*() routines to their natural home. Note that
> ksm_functional_tests.c also depend, intentionally (due to a recent
> commit [1]), upon uffd-common.[ch].
> 
> Cc: David Hildenbrand <david@redhat.com>
> Cc: Peter Xu <peterx@redhat.com>
> Signed-off-by: John Hubbard <jhubbard@nvidia.com>

Acked-by: David Hildenbrand <david@redhat.com>
Peter Xu June 5, 2023, 3:59 p.m. UTC | #2
On Fri, Jun 02, 2023 at 07:15:57PM -0700, John Hubbard wrote:
> Move the uffd*() routines to their natural home. Note that
> ksm_functional_tests.c also depend, intentionally (due to a recent
> commit [1]), upon uffd-common.[ch].
> 
> Cc: David Hildenbrand <david@redhat.com>
> Cc: Peter Xu <peterx@redhat.com>
> Signed-off-by: John Hubbard <jhubbard@nvidia.com>
> ---
>  tools/testing/selftests/mm/Makefile           |   7 +-
>  tools/testing/selftests/mm/hugepage-mremap.c  |   2 +-
>  .../selftests/mm/ksm_functional_tests.c       |   2 +-
>  tools/testing/selftests/mm/uffd-common.c      | 105 ++++++++++++++++++
>  tools/testing/selftests/mm/uffd-common.h      |  12 +-
>  tools/testing/selftests/mm/vm_util.c          | 104 -----------------
>  tools/testing/selftests/mm/vm_util.h          |  10 --
>  7 files changed, 122 insertions(+), 120 deletions(-)
> 
> diff --git a/tools/testing/selftests/mm/Makefile b/tools/testing/selftests/mm/Makefile
> index 23af4633f0f4..a15572758954 100644
> --- a/tools/testing/selftests/mm/Makefile
> +++ b/tools/testing/selftests/mm/Makefile
> @@ -109,8 +109,11 @@ include ../lib.mk
>  
>  $(TEST_GEN_PROGS): vm_util.c
>  
> -$(OUTPUT)/uffd-stress: uffd-common.c
> -$(OUTPUT)/uffd-unit-tests: uffd-common.c
> +$(OUTPUT)/uffd-stress:          uffd-common.c
> +$(OUTPUT)/uffd-unit-tests:      uffd-common.c
> +$(OUTPUT)/hugepage-mremap:      uffd-common.c
> +$(OUTPUT)/write_to_hugetlbfs:   uffd-common.c
> +$(OUTPUT)/ksm_functional_tests: uffd-common.c

Sorry, John, I still cannot follow..

As I said before uffd-common.[ch] was for uffd stress/unit tests.  I
confess my fault to not have named it uffd-test-common.[ch] already.

I think it's fine to keep uffd_*() helpers in vm_util.[ch] for now, until
it grows.  Just like if one day we'll have a pagemap.c test we don't
necessary need to move pagemap_*() helpers from vm_utils.[ch] into
pagemap.[ch].  It just keeps common test helpers.

Can we avoid linking those into other tests in whatever way?  Maybe
renaming it to uffd-test-common.[ch] may be cleaner?
John Hubbard June 5, 2023, 7:09 p.m. UTC | #3
On 6/5/23 08:59, Peter Xu wrote:
...
>> -$(OUTPUT)/uffd-stress: uffd-common.c
>> -$(OUTPUT)/uffd-unit-tests: uffd-common.c
>> +$(OUTPUT)/uffd-stress:          uffd-common.c
>> +$(OUTPUT)/uffd-unit-tests:      uffd-common.c
>> +$(OUTPUT)/hugepage-mremap:      uffd-common.c
>> +$(OUTPUT)/write_to_hugetlbfs:   uffd-common.c
>> +$(OUTPUT)/ksm_functional_tests: uffd-common.c
> 
> Sorry, John, I still cannot follow..
> 
> As I said before uffd-common.[ch] was for uffd stress/unit tests.  I
> confess my fault to not have named it uffd-test-common.[ch] already.

Actually, given that there is nothing *except* test code in this
directory, I think your original choice of file names is just right.

> 
> I think it's fine to keep uffd_*() helpers in vm_util.[ch] for now, until
> it grows.  Just like if one day we'll have a pagemap.c test we don't
> necessary need to move pagemap_*() helpers from vm_utils.[ch] into
> pagemap.[ch].  It just keeps common test helpers.
> 
> Can we avoid linking those into other tests in whatever way?  Maybe
> renaming it to uffd-test-common.[ch] may be cleaner?
> 

It sounds like you are suggesting this:

$(OUTPUT)/uffd-stress:          uffd-common.c uffd-test-common.c
$(OUTPUT)/uffd-unit-tests:      uffd-common.c uffd-test-common.c
$(OUTPUT)/hugepage-mremap:      uffd-test-common.c
$(OUTPUT)/write_to_hugetlbfs:   uffd-test-common.c
$(OUTPUT)/ksm_functional_tests: uffd-test-common.c

...approximately. Do I have that correct? I can arrange it that way
if you feel it's a better end result. (And it's better than leaving
uffd*() helpers in vm_utils, imho.)

thanks,
Peter Xu June 5, 2023, 7:24 p.m. UTC | #4
On Mon, Jun 05, 2023 at 12:09:56PM -0700, John Hubbard wrote:
> On 6/5/23 08:59, Peter Xu wrote:
> ...
> > > -$(OUTPUT)/uffd-stress: uffd-common.c
> > > -$(OUTPUT)/uffd-unit-tests: uffd-common.c
> > > +$(OUTPUT)/uffd-stress:          uffd-common.c
> > > +$(OUTPUT)/uffd-unit-tests:      uffd-common.c
> > > +$(OUTPUT)/hugepage-mremap:      uffd-common.c
> > > +$(OUTPUT)/write_to_hugetlbfs:   uffd-common.c
> > > +$(OUTPUT)/ksm_functional_tests: uffd-common.c
> > 
> > Sorry, John, I still cannot follow..
> > 
> > As I said before uffd-common.[ch] was for uffd stress/unit tests.  I
> > confess my fault to not have named it uffd-test-common.[ch] already.
> 
> Actually, given that there is nothing *except* test code in this
> directory, I think your original choice of file names is just right.
> 
> > 
> > I think it's fine to keep uffd_*() helpers in vm_util.[ch] for now, until
> > it grows.  Just like if one day we'll have a pagemap.c test we don't
> > necessary need to move pagemap_*() helpers from vm_utils.[ch] into
> > pagemap.[ch].  It just keeps common test helpers.
> > 
> > Can we avoid linking those into other tests in whatever way?  Maybe
> > renaming it to uffd-test-common.[ch] may be cleaner?
> > 
> 
> It sounds like you are suggesting this:
> 
> $(OUTPUT)/uffd-stress:          uffd-common.c uffd-test-common.c
> $(OUTPUT)/uffd-unit-tests:      uffd-common.c uffd-test-common.c
> $(OUTPUT)/hugepage-mremap:      uffd-test-common.c
> $(OUTPUT)/write_to_hugetlbfs:   uffd-test-common.c
> $(OUTPUT)/ksm_functional_tests: uffd-test-common.c
> 
> ...approximately. Do I have that correct? I can arrange it that way
> if you feel it's a better end result. (And it's better than leaving
> uffd*() helpers in vm_utils, imho.)

Yes, as long as we don't link (especially) the uffd test specific globals
into non-uffd test programs I'll have no issue.  Thanks.
John Hubbard June 5, 2023, 7:28 p.m. UTC | #5
On 6/5/23 12:24, Peter Xu wrote:
...
>> It sounds like you are suggesting this:
>>
>> $(OUTPUT)/uffd-stress:          uffd-common.c uffd-test-common.c
>> $(OUTPUT)/uffd-unit-tests:      uffd-common.c uffd-test-common.c
>> $(OUTPUT)/hugepage-mremap:      uffd-test-common.c
>> $(OUTPUT)/write_to_hugetlbfs:   uffd-test-common.c
>> $(OUTPUT)/ksm_functional_tests: uffd-test-common.c
>>
>> ...approximately. Do I have that correct? I can arrange it that way
>> if you feel it's a better end result. (And it's better than leaving
>> uffd*() helpers in vm_utils, imho.)
> 
> Yes, as long as we don't link (especially) the uffd test specific globals
> into non-uffd test programs I'll have no issue.  Thanks.
> 

OK very good. I'll put that together.


thanks,
diff mbox series

Patch

diff --git a/tools/testing/selftests/mm/Makefile b/tools/testing/selftests/mm/Makefile
index 23af4633f0f4..a15572758954 100644
--- a/tools/testing/selftests/mm/Makefile
+++ b/tools/testing/selftests/mm/Makefile
@@ -109,8 +109,11 @@  include ../lib.mk
 
 $(TEST_GEN_PROGS): vm_util.c
 
-$(OUTPUT)/uffd-stress: uffd-common.c
-$(OUTPUT)/uffd-unit-tests: uffd-common.c
+$(OUTPUT)/uffd-stress:          uffd-common.c
+$(OUTPUT)/uffd-unit-tests:      uffd-common.c
+$(OUTPUT)/hugepage-mremap:      uffd-common.c
+$(OUTPUT)/write_to_hugetlbfs:   uffd-common.c
+$(OUTPUT)/ksm_functional_tests: uffd-common.c
 
 ifeq ($(MACHINE),x86_64)
 BINARIES_32 := $(patsubst %,$(OUTPUT)/%,$(BINARIES_32))
diff --git a/tools/testing/selftests/mm/hugepage-mremap.c b/tools/testing/selftests/mm/hugepage-mremap.c
index cabd0084f57b..8158fe909f5e 100644
--- a/tools/testing/selftests/mm/hugepage-mremap.c
+++ b/tools/testing/selftests/mm/hugepage-mremap.c
@@ -24,7 +24,7 @@ 
 #include <sys/ioctl.h>
 #include <string.h>
 #include <stdbool.h>
-#include "vm_util.h"
+#include "uffd-common.h"
 
 #define DEFAULT_LENGTH_MB 10UL
 #define MB_TO_BYTES(x) (x * 1024 * 1024)
diff --git a/tools/testing/selftests/mm/ksm_functional_tests.c b/tools/testing/selftests/mm/ksm_functional_tests.c
index 26853badae70..648188ad73fa 100644
--- a/tools/testing/selftests/mm/ksm_functional_tests.c
+++ b/tools/testing/selftests/mm/ksm_functional_tests.c
@@ -22,7 +22,7 @@ 
 #include <linux/userfaultfd.h>
 
 #include "../kselftest.h"
-#include "vm_util.h"
+#include "uffd-common.h"
 
 #define KiB 1024u
 #define MiB (1024 * KiB)
diff --git a/tools/testing/selftests/mm/uffd-common.c b/tools/testing/selftests/mm/uffd-common.c
index 61c6250adf93..e1ad63668a05 100644
--- a/tools/testing/selftests/mm/uffd-common.c
+++ b/tools/testing/selftests/mm/uffd-common.c
@@ -6,6 +6,7 @@ 
  */
 
 #include "uffd-common.h"
+#include "vm_util.h"
 
 #define BASE_PMD_ADDR ((void *)(1UL << 30))
 
@@ -616,3 +617,107 @@  int copy_page(int ufd, unsigned long offset, bool wp)
 {
 	return __copy_page(ufd, offset, false, wp);
 }
+
+/* If `ioctls' non-NULL, the allowed ioctls will be returned into the var */
+int uffd_register_with_ioctls(int uffd, void *addr, uint64_t len,
+			      bool miss, bool wp, bool minor, uint64_t *ioctls)
+{
+	struct uffdio_register uffdio_register = { 0 };
+	uint64_t mode = 0;
+	int ret = 0;
+
+	if (miss)
+		mode |= UFFDIO_REGISTER_MODE_MISSING;
+	if (wp)
+		mode |= UFFDIO_REGISTER_MODE_WP;
+	if (minor)
+		mode |= UFFDIO_REGISTER_MODE_MINOR;
+
+	uffdio_register.range.start = (unsigned long)addr;
+	uffdio_register.range.len = len;
+	uffdio_register.mode = mode;
+
+	if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register) == -1)
+		ret = -errno;
+	else if (ioctls)
+		*ioctls = uffdio_register.ioctls;
+
+	return ret;
+}
+
+int uffd_register(int uffd, void *addr, uint64_t len,
+		  bool miss, bool wp, bool minor)
+{
+	return uffd_register_with_ioctls(uffd, addr, len,
+					 miss, wp, minor, NULL);
+}
+
+int uffd_unregister(int uffd, void *addr, uint64_t len)
+{
+	struct uffdio_range range = { .start = (uintptr_t)addr, .len = len };
+	int ret = 0;
+
+	if (ioctl(uffd, UFFDIO_UNREGISTER, &range) == -1)
+		ret = -errno;
+
+	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/uffd-common.h b/tools/testing/selftests/mm/uffd-common.h
index 6068f2346b86..a1cdb78c0762 100644
--- a/tools/testing/selftests/mm/uffd-common.h
+++ b/tools/testing/selftests/mm/uffd-common.h
@@ -19,8 +19,6 @@ 
 #include <signal.h>
 #include <poll.h>
 #include <string.h>
-#include <linux/mman.h>
-#include <sys/mman.h>
 #include <sys/syscall.h>
 #include <sys/ioctl.h>
 #include <sys/wait.h>
@@ -110,6 +108,16 @@  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_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);
+
 #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 01296c17df02..c64a0134f83c 100644
--- a/tools/testing/selftests/mm/vm_util.c
+++ b/tools/testing/selftests/mm/vm_util.c
@@ -198,110 +198,6 @@  unsigned long default_huge_page_size(void)
 	return hps;
 }
 
-/* If `ioctls' non-NULL, the allowed ioctls will be returned into the var */
-int uffd_register_with_ioctls(int uffd, void *addr, uint64_t len,
-			      bool miss, bool wp, bool minor, uint64_t *ioctls)
-{
-	struct uffdio_register uffdio_register = { 0 };
-	uint64_t mode = 0;
-	int ret = 0;
-
-	if (miss)
-		mode |= UFFDIO_REGISTER_MODE_MISSING;
-	if (wp)
-		mode |= UFFDIO_REGISTER_MODE_WP;
-	if (minor)
-		mode |= UFFDIO_REGISTER_MODE_MINOR;
-
-	uffdio_register.range.start = (unsigned long)addr;
-	uffdio_register.range.len = len;
-	uffdio_register.mode = mode;
-
-	if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register) == -1)
-		ret = -errno;
-	else if (ioctls)
-		*ioctls = uffdio_register.ioctls;
-
-	return ret;
-}
-
-int uffd_register(int uffd, void *addr, uint64_t len,
-		  bool miss, bool wp, bool minor)
-{
-	return uffd_register_with_ioctls(uffd, addr, len,
-					 miss, wp, minor, NULL);
-}
-
-int uffd_unregister(int uffd, void *addr, uint64_t len)
-{
-	struct uffdio_range range = { .start = (uintptr_t)addr, .len = len };
-	int ret = 0;
-
-	if (ioctl(uffd, UFFDIO_UNREGISTER, &range) == -1)
-		ret = -errno;
-
-	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;
-}
-
 unsigned int psize(void)
 {
 	if (!__page_size)
diff --git a/tools/testing/selftests/mm/vm_util.h b/tools/testing/selftests/mm/vm_util.h
index 8aa543a3678b..f04f82771cd0 100644
--- a/tools/testing/selftests/mm/vm_util.h
+++ b/tools/testing/selftests/mm/vm_util.h
@@ -33,16 +33,6 @@  bool check_huge_shmem(void *addr, int nr_hpages, uint64_t hpage_size);
 int64_t allocate_transhuge(void *ptr, int pagemap_fd);
 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);
-
 /*
  * On ppc64 this will only work with radix 2M hugepage size
  */