Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests/vm: gup_test: introduce the dump_pages() sub-test

For quite a while, I was doing a quick hack to gup_test.c (previously,
gup_benchmark.c) whenever I wanted to try out my changes to dump_page().
This makes that hack unnecessary, and instead allows anyone to easily get
the same coverage from a user space program. That saves a lot of time
because you don't have to change the kernel, in order to test different
pages and options.

The new sub-test takes advantage of the existing gup_test infrastructure,
which already provides a simple user space program, some allocated user
space pages, an ioctl call, pinning of those pages (via either
get_user_pages or pin_user_pages) and a corresponding kernel-side test
invocation. There's not much more required, mainly just a couple of
inputs from the user.

In fact, the new test re-uses the existing command line options in order
to get various helpful combinations (THP or normal, _fast or slow gup, gup
vs. pup, and more).

New command line options are: which pages to dump, and what type of
"get/pin" to use.

In order to figure out which pages to dump, the logic is:

* If the user doesn't specify anything, the page 0 (the first page in
the address range that the program sets up for testing) is dumped.

* Or, the user can type up to 8 page indices anywhere on the command
line. If you type more than 8, then it uses the first 8 and ignores the
remaining items.

For example:

./gup_test -ct -F 1 0 19 0x1000

Meaning:
-c: dump pages sub-test
-t: use THP pages
-F 1: use pin_user_pages() instead of get_user_pages()
0 19 0x1000: dump pages 0, 19, and 4096

Link: https://lkml.kernel.org/r/20201026064021.3545418-7-jhubbard@nvidia.com
Signed-off-by: John Hubbard <jhubbard@nvidia.com>
Cc: Jérôme Glisse <jglisse@redhat.com>
Cc: Jonathan Corbet <corbet@lwn.net>
Cc: Ralph Campbell <rcampbell@nvidia.com>
Cc: Shuah Khan <shuah@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

John Hubbard and committed by
Linus Torvalds
f4f9bda4 a9bed1e1

+113 -4
+6
mm/Kconfig
··· 832 832 get_user_pages*() and pin_user_pages*(), as well as smoke tests of 833 833 the non-_fast variants. 834 834 835 + There is also a sub-test that allows running dump_page() on any 836 + of up to eight pages (selected by command line args) within the 837 + range of user-space addresses. These pages are either pinned via 838 + pin_user_pages*(), or pinned via get_user_pages*(), as specified 839 + by other command line arguments. 840 + 835 841 See tools/testing/selftests/vm/gup_test.c 836 842 837 843 config GUP_GET_PTE_LOW_HIGH
+54 -2
mm/gup_test.c
··· 7 7 #include "gup_test.h" 8 8 9 9 static void put_back_pages(unsigned int cmd, struct page **pages, 10 - unsigned long nr_pages) 10 + unsigned long nr_pages, unsigned int gup_test_flags) 11 11 { 12 12 unsigned long i; 13 13 ··· 22 22 case PIN_BASIC_TEST: 23 23 case PIN_LONGTERM_BENCHMARK: 24 24 unpin_user_pages(pages, nr_pages); 25 + break; 26 + case DUMP_USER_PAGES_TEST: 27 + if (gup_test_flags & GUP_TEST_FLAG_DUMP_PAGES_USE_PIN) { 28 + unpin_user_pages(pages, nr_pages); 29 + } else { 30 + for (i = 0; i < nr_pages; i++) 31 + put_page(pages[i]); 32 + 33 + } 25 34 break; 26 35 } 27 36 } ··· 55 46 } 56 47 } 57 48 break; 49 + } 50 + } 51 + 52 + static void dump_pages_test(struct gup_test *gup, struct page **pages, 53 + unsigned long nr_pages) 54 + { 55 + unsigned int index_to_dump; 56 + unsigned int i; 57 + 58 + /* 59 + * Zero out any user-supplied page index that is out of range. Remember: 60 + * .which_pages[] contains a 1-based set of page indices. 61 + */ 62 + for (i = 0; i < GUP_TEST_MAX_PAGES_TO_DUMP; i++) { 63 + if (gup->which_pages[i] > nr_pages) { 64 + pr_warn("ZEROING due to out of range: .which_pages[%u]: %u\n", 65 + i, gup->which_pages[i]); 66 + gup->which_pages[i] = 0; 67 + } 68 + } 69 + 70 + for (i = 0; i < GUP_TEST_MAX_PAGES_TO_DUMP; i++) { 71 + index_to_dump = gup->which_pages[i]; 72 + 73 + if (index_to_dump) { 74 + index_to_dump--; // Decode from 1-based, to 0-based 75 + pr_info("---- page #%u, starting from user virt addr: 0x%llx\n", 76 + index_to_dump, gup->addr); 77 + dump_page(pages[index_to_dump], 78 + "gup_test: dump_pages() test"); 79 + } 58 80 } 59 81 } 60 82 ··· 151 111 gup->flags | FOLL_LONGTERM, 152 112 pages + i, NULL); 153 113 break; 114 + case DUMP_USER_PAGES_TEST: 115 + if (gup->flags & GUP_TEST_FLAG_DUMP_PAGES_USE_PIN) 116 + nr = pin_user_pages(addr, nr, gup->flags, 117 + pages + i, NULL); 118 + else 119 + nr = get_user_pages(addr, nr, gup->flags, 120 + pages + i, NULL); 121 + break; 154 122 default: 155 123 ret = -EINVAL; 156 124 goto unlock; ··· 182 134 */ 183 135 verify_dma_pinned(cmd, pages, nr_pages); 184 136 137 + if (cmd == DUMP_USER_PAGES_TEST) 138 + dump_pages_test(gup, pages, nr_pages); 139 + 185 140 start_time = ktime_get(); 186 141 187 - put_back_pages(cmd, pages, nr_pages); 142 + put_back_pages(cmd, pages, nr_pages, gup->flags); 188 143 189 144 end_time = ktime_get(); 190 145 gup->put_delta_usec = ktime_us_delta(end_time, start_time); ··· 212 161 case PIN_LONGTERM_BENCHMARK: 213 162 case GUP_BASIC_TEST: 214 163 case PIN_BASIC_TEST: 164 + case DUMP_USER_PAGES_TEST: 215 165 break; 216 166 default: 217 167 return -EINVAL;
+10
mm/gup_test.h
··· 9 9 #define PIN_LONGTERM_BENCHMARK _IOWR('g', 3, struct gup_test) 10 10 #define GUP_BASIC_TEST _IOWR('g', 4, struct gup_test) 11 11 #define PIN_BASIC_TEST _IOWR('g', 5, struct gup_test) 12 + #define DUMP_USER_PAGES_TEST _IOWR('g', 6, struct gup_test) 13 + 14 + #define GUP_TEST_MAX_PAGES_TO_DUMP 8 15 + 16 + #define GUP_TEST_FLAG_DUMP_PAGES_USE_PIN 0x1 12 17 13 18 struct gup_test { 14 19 __u64 get_delta_usec; ··· 22 17 __u64 size; 23 18 __u32 nr_pages_per_call; 24 19 __u32 flags; 20 + /* 21 + * Each non-zero entry is the number of the page (1-based: first page is 22 + * page 1, so that zero entries mean "do nothing") from the .addr base. 23 + */ 24 + __u32 which_pages[GUP_TEST_MAX_PAGES_TO_DUMP]; 25 25 }; 26 26 27 27 #endif /* __GUP_TEST_H */
+43 -2
tools/testing/selftests/vm/gup_test.c
··· 27 27 return "GUP_BASIC_TEST"; 28 28 case PIN_BASIC_TEST: 29 29 return "PIN_BASIC_TEST"; 30 + case DUMP_USER_PAGES_TEST: 31 + return "DUMP_USER_PAGES_TEST"; 30 32 } 31 33 return "Unknown command"; 32 34 } 33 35 34 36 int main(int argc, char **argv) 35 37 { 36 - struct gup_test gup; 38 + struct gup_test gup = { 0 }; 37 39 unsigned long size = 128 * MB; 38 40 int i, fd, filed, opt, nr_pages = 1, thp = -1, repeats = 1, write = 0; 39 41 unsigned long cmd = GUP_FAST_BENCHMARK; ··· 43 41 char *file = "/dev/zero"; 44 42 char *p; 45 43 46 - while ((opt = getopt(argc, argv, "m:r:n:f:abtTLUuwSH")) != -1) { 44 + while ((opt = getopt(argc, argv, "m:r:n:F:f:abctTLUuwSH")) != -1) { 47 45 switch (opt) { 48 46 case 'a': 49 47 cmd = PIN_FAST_BENCHMARK; ··· 53 51 break; 54 52 case 'L': 55 53 cmd = PIN_LONGTERM_BENCHMARK; 54 + break; 55 + case 'c': 56 + cmd = DUMP_USER_PAGES_TEST; 57 + /* 58 + * Dump page 0 (index 1). May be overridden later, by 59 + * user's non-option arguments. 60 + * 61 + * .which_pages is zero-based, so that zero can mean "do 62 + * nothing". 63 + */ 64 + gup.which_pages[0] = 1; 65 + break; 66 + case 'F': 67 + /* strtol, so you can pass flags in hex form */ 68 + gup.flags = strtol(optarg, 0, 0); 56 69 break; 57 70 case 'm': 58 71 size = atoi(optarg) * MB; ··· 105 88 break; 106 89 default: 107 90 return -1; 91 + } 92 + } 93 + 94 + if (optind < argc) { 95 + int extra_arg_count = 0; 96 + /* 97 + * For example: 98 + * 99 + * ./gup_test -c 0 1 0x1001 100 + * 101 + * ...to dump pages 0, 1, and 4097 102 + */ 103 + 104 + while ((optind < argc) && 105 + (extra_arg_count < GUP_TEST_MAX_PAGES_TO_DUMP)) { 106 + /* 107 + * Do the 1-based indexing here, so that the user can 108 + * use normal 0-based indexing on the command line. 109 + */ 110 + long page_index = strtol(argv[optind], 0, 0) + 1; 111 + 112 + gup.which_pages[extra_arg_count] = page_index; 113 + extra_arg_count++; 114 + optind++; 108 115 } 109 116 } 110 117