diff mbox series

[RFC,5/5] mm/gup_test: Verify GUP grabs same pages twice

Message ID 20240618-exclusive-gup-v1-5-30472a19c5d1@quicinc.com (mailing list archive)
State New, archived
Headers show
Series mm/gup: Introduce exclusive GUP pinning | expand

Commit Message

Elliot Berman June 19, 2024, 12:05 a.m. UTC
GUP'ing pages should get the same pages, test it. In case of
FOLL_EXCLUSIVE, the second pin should fail to get any pages.

Note: this change ought to be refactored to pull out the GUP'ing bits
that's duplicated between the original and the second GUP.

Signed-off-by: Elliot Berman <quic_eberman@quicinc.com>
---
 mm/gup_test.c                         | 86 +++++++++++++++++++++++++++++++++++
 mm/gup_test.h                         |  1 +
 tools/testing/selftests/mm/gup_test.c |  5 +-
 3 files changed, 91 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/mm/gup_test.c b/mm/gup_test.c
index 9c6b8c93e44a7..28cc422b60b78 100644
--- a/mm/gup_test.c
+++ b/mm/gup_test.c
@@ -86,6 +86,89 @@  static void verify_exclusive_pinned(unsigned int gup_flags, struct page **pages,
 	}
 }
 
+static int verify_gup_twice(unsigned int cmd, struct gup_test *gup,
+			    struct page **expected_pages,
+			    unsigned long expected_nr_pages)
+{
+	unsigned long i, nr_pages, addr, next;
+	long nr;
+	struct page **pages __free(kfree) = NULL;
+	int ret = 0;
+
+	nr_pages = gup->size / PAGE_SIZE;
+	pages = kvcalloc(nr_pages, sizeof(void *), GFP_KERNEL);
+	if (!pages)
+		return -ENOMEM;
+
+	i = 0;
+	nr = gup->nr_pages_per_call;
+	for (addr = gup->addr; addr < gup->addr + gup->size; addr = next) {
+		if (nr != gup->nr_pages_per_call)
+			break;
+
+		next = addr + nr * PAGE_SIZE;
+		if (next > gup->addr + gup->size) {
+			next = gup->addr + gup->size;
+			nr = (next - addr) / PAGE_SIZE;
+		}
+
+		switch (cmd) {
+		case GUP_FAST_BENCHMARK:
+			nr = get_user_pages_fast(addr, nr, gup->gup_flags,
+						 pages + i);
+			break;
+		case GUP_BASIC_TEST:
+			nr = get_user_pages(addr, nr, gup->gup_flags, pages + i);
+			break;
+		case PIN_FAST_BENCHMARK:
+			nr = pin_user_pages_fast(addr, nr, gup->gup_flags,
+						 pages + i);
+			break;
+		case PIN_BASIC_TEST:
+			nr = pin_user_pages(addr, nr, gup->gup_flags, pages + i);
+			break;
+		case PIN_LONGTERM_BENCHMARK:
+			nr = pin_user_pages(addr, nr,
+					    gup->gup_flags | FOLL_LONGTERM,
+					    pages + i);
+			break;
+		default:
+			pr_err("cmd %d not supported for %s\n", cmd, __func__);
+			return -EINVAL;
+		}
+
+		if (nr <= 0)
+			break;
+		i += nr;
+	}
+
+	nr_pages = i;
+
+	if (gup->gup_flags & FOLL_EXCLUSIVE) {
+		if (WARN(nr_pages,
+			 "Able to acquire exclusive pin twice for %ld of %ld pages",
+			 nr_pages, expected_nr_pages)) {
+			dump_page(pages[0],
+				  "gup_test: verify_gup_twice() test");
+			ret = -EIO;
+		}
+	} else if (nr_pages != expected_nr_pages) {
+		pr_err("%s: Expected %ld pages, got %ld\n", __func__,
+		       expected_nr_pages, nr_pages);
+		ret = -EIO;
+	} else {
+		for (i = 0; i < nr_pages; i++) {
+			if (WARN(pages[i] != expected_pages[i],
+				 "pages[%lu] mismatch\n", i))
+				break;
+		}
+	}
+
+	put_back_pages(cmd, pages, nr_pages, gup->test_flags);
+
+	return ret;
+}
+
 static void dump_pages_test(struct gup_test *gup, struct page **pages,
 			    unsigned long nr_pages)
 {
@@ -210,6 +293,9 @@  static int __gup_test_ioctl(unsigned int cmd,
 	if (cmd == DUMP_USER_PAGES_TEST)
 		dump_pages_test(gup, pages, nr_pages);
 
+	if (gup->test_flags & GUP_TEST_FLAG_GUP_TWICE)
+		ret = verify_gup_twice(cmd, gup, pages, nr_pages);
+
 	start_time = ktime_get();
 
 	put_back_pages(cmd, pages, nr_pages, gup->test_flags);
diff --git a/mm/gup_test.h b/mm/gup_test.h
index 5b37b54e8bea6..fcd41919b0159 100644
--- a/mm/gup_test.h
+++ b/mm/gup_test.h
@@ -17,6 +17,7 @@ 
 #define GUP_TEST_MAX_PAGES_TO_DUMP		8
 
 #define GUP_TEST_FLAG_DUMP_PAGES_USE_PIN	0x1
+#define GUP_TEST_FLAG_GUP_TWICE			0x2
 
 struct gup_test {
 	__u64 get_delta_usec;
diff --git a/tools/testing/selftests/mm/gup_test.c b/tools/testing/selftests/mm/gup_test.c
index bdeaac67ff9aa..b4b10c8338f80 100644
--- a/tools/testing/selftests/mm/gup_test.c
+++ b/tools/testing/selftests/mm/gup_test.c
@@ -98,7 +98,7 @@  int main(int argc, char **argv)
 	pthread_t *tid;
 	char *p;
 
-	while ((opt = getopt(argc, argv, "m:r:n:F:f:abcj:tTLUuwWSHpz")) != -1) {
+	while ((opt = getopt(argc, argv, "m:r:n:F:f:abcj:dtTLUuwWSHpz")) != -1) {
 		switch (opt) {
 		case 'a':
 			cmd = PIN_FAST_BENCHMARK;
@@ -172,6 +172,9 @@  int main(int argc, char **argv)
 			/* fault pages in gup, do not fault in userland */
 			touch = 1;
 			break;
+		case 'd':
+			gup.test_flags |= GUP_TEST_FLAG_GUP_TWICE;
+			break;
 		default:
 			ksft_exit_fail_msg("Wrong argument\n");
 		}