Commit | Line | Data |
---|---|---|
64c349f4 | 1 | #include <fcntl.h> |
62e80f2b | 2 | #include <errno.h> |
64c349f4 KS |
3 | #include <stdio.h> |
4 | #include <stdlib.h> | |
5 | #include <unistd.h> | |
62e80f2b | 6 | #include <dirent.h> |
64c349f4 KS |
7 | #include <sys/ioctl.h> |
8 | #include <sys/mman.h> | |
64c349f4 KS |
9 | #include <sys/stat.h> |
10 | #include <sys/types.h> | |
f39bd853 PX |
11 | #include <pthread.h> |
12 | #include <assert.h> | |
0e29bc0e | 13 | #include <mm/gup_test.h> |
62e80f2b | 14 | #include "../kselftest.h" |
64c349f4 | 15 | |
6f6a841f MR |
16 | #include "util.h" |
17 | ||
64c349f4 | 18 | #define MB (1UL << 20) |
64c349f4 | 19 | |
bdffe23e JH |
20 | /* Just the flags we need, copied from mm.h: */ |
21 | #define FOLL_WRITE 0x01 /* check pte is writable */ | |
e44605a8 | 22 | #define FOLL_TOUCH 0x02 /* mark page accessed */ |
bdffe23e | 23 | |
17de1e55 JS |
24 | #define GUP_TEST_FILE "/sys/kernel/debug/gup_test" |
25 | ||
f39bd853 PX |
26 | static unsigned long cmd = GUP_FAST_BENCHMARK; |
27 | static int gup_fd, repeats = 1; | |
28 | static unsigned long size = 128 * MB; | |
29 | /* Serialize prints */ | |
30 | static pthread_mutex_t print_mutex = PTHREAD_MUTEX_INITIALIZER; | |
31 | ||
a9bed1e1 JH |
32 | static char *cmd_to_str(unsigned long cmd) |
33 | { | |
34 | switch (cmd) { | |
35 | case GUP_FAST_BENCHMARK: | |
36 | return "GUP_FAST_BENCHMARK"; | |
37 | case PIN_FAST_BENCHMARK: | |
38 | return "PIN_FAST_BENCHMARK"; | |
39 | case PIN_LONGTERM_BENCHMARK: | |
40 | return "PIN_LONGTERM_BENCHMARK"; | |
41 | case GUP_BASIC_TEST: | |
42 | return "GUP_BASIC_TEST"; | |
43 | case PIN_BASIC_TEST: | |
44 | return "PIN_BASIC_TEST"; | |
f4f9bda4 JH |
45 | case DUMP_USER_PAGES_TEST: |
46 | return "DUMP_USER_PAGES_TEST"; | |
a9bed1e1 JH |
47 | } |
48 | return "Unknown command"; | |
49 | } | |
50 | ||
f39bd853 PX |
51 | void *gup_thread(void *data) |
52 | { | |
53 | struct gup_test gup = *(struct gup_test *)data; | |
54 | int i; | |
55 | ||
56 | /* Only report timing information on the *_BENCHMARK commands: */ | |
57 | if ((cmd == PIN_FAST_BENCHMARK) || (cmd == GUP_FAST_BENCHMARK) || | |
58 | (cmd == PIN_LONGTERM_BENCHMARK)) { | |
59 | for (i = 0; i < repeats; i++) { | |
60 | gup.size = size; | |
61 | if (ioctl(gup_fd, cmd, &gup)) | |
62 | perror("ioctl"), exit(1); | |
63 | ||
64 | pthread_mutex_lock(&print_mutex); | |
65 | printf("%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 | printf(", truncated (size: %lld)", gup.size); | |
70 | printf("\n"); | |
71 | pthread_mutex_unlock(&print_mutex); | |
72 | } | |
73 | } else { | |
74 | gup.size = size; | |
75 | if (ioctl(gup_fd, cmd, &gup)) { | |
76 | perror("ioctl"); | |
77 | exit(1); | |
78 | } | |
79 | ||
80 | pthread_mutex_lock(&print_mutex); | |
81 | printf("%s: done\n", cmd_to_str(cmd)); | |
82 | if (gup.size != size) | |
83 | printf("Truncated (size: %lld)\n", gup.size); | |
84 | pthread_mutex_unlock(&print_mutex); | |
85 | } | |
86 | ||
87 | return NULL; | |
88 | } | |
89 | ||
64c349f4 KS |
90 | int main(int argc, char **argv) |
91 | { | |
f4f9bda4 | 92 | struct gup_test gup = { 0 }; |
f39bd853 | 93 | int filed, i, opt, nr_pages = 1, thp = -1, write = 1, nthreads = 1, ret; |
e44605a8 | 94 | int flags = MAP_PRIVATE, touch = 0; |
aeb85ed4 | 95 | char *file = "/dev/zero"; |
f39bd853 | 96 | pthread_t *tid; |
64c349f4 KS |
97 | char *p; |
98 | ||
f39bd853 | 99 | while ((opt = getopt(argc, argv, "m:r:n:F:f:abcj:tTLUuwWSHpz")) != -1) { |
64c349f4 | 100 | switch (opt) { |
41c45d37 JH |
101 | case 'a': |
102 | cmd = PIN_FAST_BENCHMARK; | |
103 | break; | |
104 | case 'b': | |
a9bed1e1 | 105 | cmd = PIN_BASIC_TEST; |
41c45d37 | 106 | break; |
657d4f79 BS |
107 | case 'L': |
108 | cmd = PIN_LONGTERM_BENCHMARK; | |
109 | break; | |
f4f9bda4 JH |
110 | case 'c': |
111 | cmd = DUMP_USER_PAGES_TEST; | |
112 | /* | |
113 | * Dump page 0 (index 1). May be overridden later, by | |
114 | * user's non-option arguments. | |
115 | * | |
116 | * .which_pages is zero-based, so that zero can mean "do | |
117 | * nothing". | |
118 | */ | |
119 | gup.which_pages[0] = 1; | |
120 | break; | |
79dbf135 PT |
121 | case 'p': |
122 | /* works only with DUMP_USER_PAGES_TEST */ | |
123 | gup.test_flags |= GUP_TEST_FLAG_DUMP_PAGES_USE_PIN; | |
124 | break; | |
f4f9bda4 JH |
125 | case 'F': |
126 | /* strtol, so you can pass flags in hex form */ | |
79dbf135 | 127 | gup.gup_flags = strtol(optarg, 0, 0); |
f4f9bda4 | 128 | break; |
f39bd853 PX |
129 | case 'j': |
130 | nthreads = atoi(optarg); | |
131 | break; | |
64c349f4 KS |
132 | case 'm': |
133 | size = atoi(optarg) * MB; | |
134 | break; | |
135 | case 'r': | |
136 | repeats = atoi(optarg); | |
137 | break; | |
138 | case 'n': | |
139 | nr_pages = atoi(optarg); | |
140 | break; | |
141 | case 't': | |
142 | thp = 1; | |
143 | break; | |
144 | case 'T': | |
145 | thp = 0; | |
146 | break; | |
714a3a1e | 147 | case 'U': |
a9bed1e1 | 148 | cmd = GUP_BASIC_TEST; |
714a3a1e | 149 | break; |
41c45d37 JH |
150 | case 'u': |
151 | cmd = GUP_FAST_BENCHMARK; | |
152 | break; | |
64c349f4 KS |
153 | case 'w': |
154 | write = 1; | |
319e0bec | 155 | break; |
79dbf135 PT |
156 | case 'W': |
157 | write = 0; | |
158 | break; | |
aeb85ed4 KB |
159 | case 'f': |
160 | file = optarg; | |
161 | break; | |
0dd8666a KB |
162 | case 'S': |
163 | flags &= ~MAP_PRIVATE; | |
164 | flags |= MAP_SHARED; | |
165 | break; | |
3821b76c | 166 | case 'H': |
64801d19 | 167 | flags |= (MAP_HUGETLB | MAP_ANONYMOUS); |
3821b76c | 168 | break; |
e44605a8 PT |
169 | case 'z': |
170 | /* fault pages in gup, do not fault in userland */ | |
171 | touch = 1; | |
172 | break; | |
64c349f4 KS |
173 | default: |
174 | return -1; | |
175 | } | |
176 | } | |
177 | ||
f4f9bda4 JH |
178 | if (optind < argc) { |
179 | int extra_arg_count = 0; | |
180 | /* | |
181 | * For example: | |
182 | * | |
183 | * ./gup_test -c 0 1 0x1001 | |
184 | * | |
185 | * ...to dump pages 0, 1, and 4097 | |
186 | */ | |
187 | ||
188 | while ((optind < argc) && | |
189 | (extra_arg_count < GUP_TEST_MAX_PAGES_TO_DUMP)) { | |
190 | /* | |
191 | * Do the 1-based indexing here, so that the user can | |
192 | * use normal 0-based indexing on the command line. | |
193 | */ | |
194 | long page_index = strtol(argv[optind], 0, 0) + 1; | |
195 | ||
196 | gup.which_pages[extra_arg_count] = page_index; | |
197 | extra_arg_count++; | |
198 | optind++; | |
199 | } | |
200 | } | |
201 | ||
aeb85ed4 KB |
202 | filed = open(file, O_RDWR|O_CREAT); |
203 | if (filed < 0) { | |
204 | perror("open"); | |
205 | exit(filed); | |
206 | } | |
207 | ||
64c349f4 | 208 | gup.nr_pages_per_call = nr_pages; |
bdffe23e | 209 | if (write) |
79dbf135 | 210 | gup.gup_flags |= FOLL_WRITE; |
64c349f4 | 211 | |
9b4d5c01 | 212 | gup_fd = open(GUP_TEST_FILE, O_RDWR); |
f39bd853 | 213 | if (gup_fd == -1) { |
62e80f2b SK |
214 | switch (errno) { |
215 | case EACCES: | |
216 | if (getuid()) | |
217 | printf("Please run this test as root\n"); | |
218 | break; | |
219 | case ENOENT: | |
220 | if (opendir("/sys/kernel/debug") == NULL) { | |
221 | printf("mount debugfs at /sys/kernel/debug\n"); | |
222 | break; | |
223 | } | |
224 | printf("check if CONFIG_GUP_TEST is enabled in kernel config\n"); | |
225 | break; | |
226 | default: | |
9b4d5c01 | 227 | perror("failed to open " GUP_TEST_FILE); |
62e80f2b SK |
228 | break; |
229 | } | |
230 | exit(KSFT_SKIP); | |
aa803771 | 231 | } |
64c349f4 | 232 | |
0dd8666a | 233 | p = mmap(NULL, size, PROT_READ | PROT_WRITE, flags, filed, 0); |
aa803771 JP |
234 | if (p == MAP_FAILED) { |
235 | perror("mmap"); | |
236 | exit(1); | |
237 | } | |
64c349f4 KS |
238 | gup.addr = (unsigned long)p; |
239 | ||
240 | if (thp == 1) | |
241 | madvise(p, size, MADV_HUGEPAGE); | |
242 | else if (thp == 0) | |
243 | madvise(p, size, MADV_NOHUGEPAGE); | |
244 | ||
e44605a8 PT |
245 | /* |
246 | * FOLL_TOUCH, in gup_test, is used as an either/or case: either | |
247 | * fault pages in from the kernel via FOLL_TOUCH, or fault them | |
248 | * in here, from user space. This allows comparison of performance | |
249 | * between those two cases. | |
250 | */ | |
251 | if (touch) { | |
252 | gup.gup_flags |= FOLL_TOUCH; | |
253 | } else { | |
254 | for (; (unsigned long)p < gup.addr + size; p += PAGE_SIZE) | |
255 | p[0] = 0; | |
256 | } | |
64c349f4 | 257 | |
f39bd853 PX |
258 | tid = malloc(sizeof(pthread_t) * nthreads); |
259 | assert(tid); | |
260 | for (i = 0; i < nthreads; i++) { | |
261 | ret = pthread_create(&tid[i], NULL, gup_thread, &gup); | |
262 | assert(ret == 0); | |
263 | } | |
264 | for (i = 0; i < nthreads; i++) { | |
265 | ret = pthread_join(tid[i], NULL); | |
266 | assert(ret == 0); | |
64c349f4 | 267 | } |
f39bd853 | 268 | free(tid); |
64c349f4 KS |
269 | |
270 | return 0; | |
271 | } |