10 #include <sys/types.h>
13 #include <mm/gup_test.h>
14 #include "../kselftest.h"
17 #define MB (1UL << 20)
19 /* Just the flags we need, copied from mm.h: */
20 #define FOLL_WRITE 0x01 /* check pte is writable */
21 #define FOLL_TOUCH 0x02 /* mark page accessed */
23 #define GUP_TEST_FILE "/sys/kernel/debug/gup_test"
25 static unsigned long cmd = GUP_FAST_BENCHMARK;
26 static int gup_fd, repeats = 1;
27 static unsigned long size = 128 * MB;
28 /* Serialize prints */
29 static pthread_mutex_t print_mutex = PTHREAD_MUTEX_INITIALIZER;
31 static char *cmd_to_str(unsigned long cmd)
34 case GUP_FAST_BENCHMARK:
35 return "GUP_FAST_BENCHMARK";
36 case PIN_FAST_BENCHMARK:
37 return "PIN_FAST_BENCHMARK";
38 case PIN_LONGTERM_BENCHMARK:
39 return "PIN_LONGTERM_BENCHMARK";
41 return "GUP_BASIC_TEST";
43 return "PIN_BASIC_TEST";
44 case DUMP_USER_PAGES_TEST:
45 return "DUMP_USER_PAGES_TEST";
47 return "Unknown command";
50 void *gup_thread(void *data)
52 struct gup_test gup = *(struct gup_test *)data;
55 /* Only report timing information on the *_BENCHMARK commands: */
56 if ((cmd == PIN_FAST_BENCHMARK) || (cmd == GUP_FAST_BENCHMARK) ||
57 (cmd == PIN_LONGTERM_BENCHMARK)) {
58 for (i = 0; i < repeats; i++) {
60 status = ioctl(gup_fd, cmd, &gup);
64 pthread_mutex_lock(&print_mutex);
65 ksft_print_msg("%s: Time: get:%lld put:%lld us",
66 cmd_to_str(cmd), gup.get_delta_usec,
69 ksft_print_msg(", truncated (size: %lld)", gup.size);
71 pthread_mutex_unlock(&print_mutex);
75 status = ioctl(gup_fd, cmd, &gup);
79 pthread_mutex_lock(&print_mutex);
80 ksft_print_msg("%s: done\n", cmd_to_str(cmd));
82 ksft_print_msg("Truncated (size: %lld)\n", gup.size);
83 pthread_mutex_unlock(&print_mutex);
87 ksft_test_result(!status, "ioctl status %d\n", status);
91 int main(int argc, char **argv)
93 struct gup_test gup = { 0 };
94 int filed, i, opt, nr_pages = 1, thp = -1, write = 1, nthreads = 1, ret;
95 int flags = MAP_PRIVATE, touch = 0;
96 char *file = "/dev/zero";
100 while ((opt = getopt(argc, argv, "m:r:n:F:f:abcj:tTLUuwWSHpz")) != -1) {
103 cmd = PIN_FAST_BENCHMARK;
106 cmd = PIN_BASIC_TEST;
109 cmd = PIN_LONGTERM_BENCHMARK;
112 cmd = DUMP_USER_PAGES_TEST;
114 * Dump page 0 (index 1). May be overridden later, by
115 * user's non-option arguments.
117 * .which_pages is zero-based, so that zero can mean "do
120 gup.which_pages[0] = 1;
123 /* works only with DUMP_USER_PAGES_TEST */
124 gup.test_flags |= GUP_TEST_FLAG_DUMP_PAGES_USE_PIN;
127 /* strtol, so you can pass flags in hex form */
128 gup.gup_flags = strtol(optarg, 0, 0);
131 nthreads = atoi(optarg);
134 size = atoi(optarg) * MB;
137 repeats = atoi(optarg);
140 nr_pages = atoi(optarg);
149 cmd = GUP_BASIC_TEST;
152 cmd = GUP_FAST_BENCHMARK;
164 flags &= ~MAP_PRIVATE;
168 flags |= (MAP_HUGETLB | MAP_ANONYMOUS);
171 /* fault pages in gup, do not fault in userland */
175 ksft_exit_fail_msg("Wrong argument\n");
180 int extra_arg_count = 0;
184 * ./gup_test -c 0 1 0x1001
186 * ...to dump pages 0, 1, and 4097
189 while ((optind < argc) &&
190 (extra_arg_count < GUP_TEST_MAX_PAGES_TO_DUMP)) {
192 * Do the 1-based indexing here, so that the user can
193 * use normal 0-based indexing on the command line.
195 long page_index = strtol(argv[optind], 0, 0) + 1;
197 gup.which_pages[extra_arg_count] = page_index;
204 ksft_set_plan(nthreads);
206 filed = open(file, O_RDWR|O_CREAT);
208 ksft_exit_fail_msg("Unable to open %s: %s\n", file, strerror(errno));
210 gup.nr_pages_per_call = nr_pages;
212 gup.gup_flags |= FOLL_WRITE;
214 gup_fd = open(GUP_TEST_FILE, O_RDWR);
219 ksft_print_msg("Please run this test as root\n");
222 if (opendir("/sys/kernel/debug") == NULL)
223 ksft_print_msg("mount debugfs at /sys/kernel/debug\n");
224 ksft_print_msg("check if CONFIG_GUP_TEST is enabled in kernel config\n");
227 ksft_print_msg("failed to open %s: %s\n", GUP_TEST_FILE, strerror(errno));
230 ksft_test_result_skip("Please run this test as root\n");
231 return ksft_exit_pass();
234 p = mmap(NULL, size, PROT_READ | PROT_WRITE, flags, filed, 0);
236 ksft_exit_fail_msg("mmap: %s\n", strerror(errno));
237 gup.addr = (unsigned long)p;
240 madvise(p, size, MADV_HUGEPAGE);
242 madvise(p, size, MADV_NOHUGEPAGE);
245 * FOLL_TOUCH, in gup_test, is used as an either/or case: either
246 * fault pages in from the kernel via FOLL_TOUCH, or fault them
247 * in here, from user space. This allows comparison of performance
248 * between those two cases.
251 gup.gup_flags |= FOLL_TOUCH;
253 for (; (unsigned long)p < gup.addr + size; p += psize())
257 tid = malloc(sizeof(pthread_t) * nthreads);
259 for (i = 0; i < nthreads; i++) {
260 ret = pthread_create(&tid[i], NULL, gup_thread, &gup);
263 for (i = 0; i < nthreads; i++) {
264 ret = pthread_join(tid[i], NULL);
270 return ksft_exit_pass();