1 #define __SANE_USERSPACE_TYPES__ // Use ll64
2 #include <fcntl.h>
3 #include <errno.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <unistd.h>
7 #include <dirent.h>
8 #include <sys/ioctl.h>
9 #include <sys/mman.h>
10 #include <sys/stat.h>
11 #include <sys/types.h>
12 #include <pthread.h>
13 #include <assert.h>
14 #include <mm/gup_test.h>
15 #include "kselftest.h"
16 #include "vm_util.h"
17
18 #define MB (1UL << 20)
19
20 /* Just the flags we need, copied from the kernel internals. */
21 #define FOLL_WRITE 0x01 /* check pte is writable */
22
23 #define GUP_TEST_FILE "/sys/kernel/debug/gup_test"
24
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;
30
cmd_to_str(unsigned long cmd)31 static char *cmd_to_str(unsigned long cmd)
32 {
33 switch (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";
40 case GUP_BASIC_TEST:
41 return "GUP_BASIC_TEST";
42 case PIN_BASIC_TEST:
43 return "PIN_BASIC_TEST";
44 case DUMP_USER_PAGES_TEST:
45 return "DUMP_USER_PAGES_TEST";
46 }
47 return "Unknown command";
48 }
49
gup_thread(void * data)50 void *gup_thread(void *data)
51 {
52 struct gup_test gup = *(struct gup_test *)data;
53 int i, status;
54
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++) {
59 gup.size = size;
60 status = ioctl(gup_fd, cmd, &gup);
61 if (status)
62 break;
63
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,
67 gup.put_delta_usec);
68 if (gup.size != size)
69 ksft_print_msg(", truncated (size: %lld)", gup.size);
70 ksft_print_msg("\n");
71 pthread_mutex_unlock(&print_mutex);
72 }
73 } else {
74 gup.size = size;
75 status = ioctl(gup_fd, cmd, &gup);
76 if (status)
77 goto return_;
78
79 pthread_mutex_lock(&print_mutex);
80 ksft_print_msg("%s: done\n", cmd_to_str(cmd));
81 if (gup.size != size)
82 ksft_print_msg("Truncated (size: %lld)\n", gup.size);
83 pthread_mutex_unlock(&print_mutex);
84 }
85
86 return_:
87 ksft_test_result(!status, "ioctl status %d\n", status);
88 return NULL;
89 }
90
main(int argc,char ** argv)91 int main(int argc, char **argv)
92 {
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;
96 char *file = "/dev/zero";
97 pthread_t *tid;
98 char *p;
99
100 while ((opt = getopt(argc, argv, "m:r:n:F:f:abcj:tTLUuwWSHpz")) != -1) {
101 switch (opt) {
102 case 'a':
103 cmd = PIN_FAST_BENCHMARK;
104 break;
105 case 'b':
106 cmd = PIN_BASIC_TEST;
107 break;
108 case 'L':
109 cmd = PIN_LONGTERM_BENCHMARK;
110 break;
111 case 'c':
112 cmd = DUMP_USER_PAGES_TEST;
113 /*
114 * Dump page 0 (index 1). May be overridden later, by
115 * user's non-option arguments.
116 *
117 * .which_pages is zero-based, so that zero can mean "do
118 * nothing".
119 */
120 gup.which_pages[0] = 1;
121 break;
122 case 'p':
123 /* works only with DUMP_USER_PAGES_TEST */
124 gup.test_flags |= GUP_TEST_FLAG_DUMP_PAGES_USE_PIN;
125 break;
126 case 'F':
127 /* strtol, so you can pass flags in hex form */
128 gup.gup_flags = strtol(optarg, 0, 0);
129 break;
130 case 'j':
131 nthreads = atoi(optarg);
132 break;
133 case 'm':
134 size = atoi(optarg) * MB;
135 break;
136 case 'r':
137 repeats = atoi(optarg);
138 break;
139 case 'n':
140 nr_pages = atoi(optarg);
141 if (nr_pages < 0)
142 nr_pages = size / psize();
143 break;
144 case 't':
145 thp = 1;
146 break;
147 case 'T':
148 thp = 0;
149 break;
150 case 'U':
151 cmd = GUP_BASIC_TEST;
152 break;
153 case 'u':
154 cmd = GUP_FAST_BENCHMARK;
155 break;
156 case 'w':
157 write = 1;
158 break;
159 case 'W':
160 write = 0;
161 break;
162 case 'f':
163 file = optarg;
164 break;
165 case 'S':
166 flags &= ~MAP_PRIVATE;
167 flags |= MAP_SHARED;
168 break;
169 case 'H':
170 flags |= (MAP_HUGETLB | MAP_ANONYMOUS);
171 break;
172 default:
173 ksft_exit_fail_msg("Wrong argument\n");
174 }
175 }
176
177 if (optind < argc) {
178 int extra_arg_count = 0;
179 /*
180 * For example:
181 *
182 * ./gup_test -c 0 1 0x1001
183 *
184 * ...to dump pages 0, 1, and 4097
185 */
186
187 while ((optind < argc) &&
188 (extra_arg_count < GUP_TEST_MAX_PAGES_TO_DUMP)) {
189 /*
190 * Do the 1-based indexing here, so that the user can
191 * use normal 0-based indexing on the command line.
192 */
193 long page_index = strtol(argv[optind], 0, 0) + 1;
194
195 gup.which_pages[extra_arg_count] = page_index;
196 extra_arg_count++;
197 optind++;
198 }
199 }
200
201 ksft_print_header();
202 ksft_set_plan(nthreads);
203
204 filed = open(file, O_RDWR|O_CREAT, 0664);
205 if (filed < 0)
206 ksft_exit_fail_msg("Unable to open %s: %s\n", file, strerror(errno));
207
208 gup.nr_pages_per_call = nr_pages;
209 if (write)
210 gup.gup_flags |= FOLL_WRITE;
211
212 gup_fd = open(GUP_TEST_FILE, O_RDWR);
213 if (gup_fd == -1) {
214 switch (errno) {
215 case EACCES:
216 if (getuid())
217 ksft_print_msg("Please run this test as root\n");
218 break;
219 case ENOENT:
220 if (opendir("/sys/kernel/debug") == NULL)
221 ksft_print_msg("mount debugfs at /sys/kernel/debug\n");
222 ksft_print_msg("check if CONFIG_GUP_TEST is enabled in kernel config\n");
223 break;
224 default:
225 ksft_print_msg("failed to open %s: %s\n", GUP_TEST_FILE, strerror(errno));
226 break;
227 }
228 ksft_test_result_skip("Please run this test as root\n");
229 ksft_exit_pass();
230 }
231
232 p = mmap(NULL, size, PROT_READ | PROT_WRITE, flags, filed, 0);
233 if (p == MAP_FAILED)
234 ksft_exit_fail_msg("mmap: %s\n", strerror(errno));
235 gup.addr = (unsigned long)p;
236
237 if (thp == 1)
238 madvise(p, size, MADV_HUGEPAGE);
239 else if (thp == 0)
240 madvise(p, size, MADV_NOHUGEPAGE);
241
242 /* Fault them in here, from user space. */
243 for (; (unsigned long)p < gup.addr + size; p += psize())
244 p[0] = 0;
245
246 tid = malloc(sizeof(pthread_t) * nthreads);
247 assert(tid);
248 for (i = 0; i < nthreads; i++) {
249 ret = pthread_create(&tid[i], NULL, gup_thread, &gup);
250 assert(ret == 0);
251 }
252 for (i = 0; i < nthreads; i++) {
253 ret = pthread_join(tid[i], NULL);
254 assert(ret == 0);
255 }
256
257 free(tid);
258
259 ksft_exit_pass();
260 }
261