xref: /linux/mm/gup_test.c (revision 6aacab308a5dfd222b2d23662bbae60c11007cfb)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/kernel.h>
3 #include <linux/mm.h>
4 #include <linux/slab.h>
5 #include <linux/uaccess.h>
6 #include <linux/ktime.h>
7 #include <linux/debugfs.h>
8 #include <linux/highmem.h>
9 #include "gup_test.h"
10 
11 static void put_back_pages(unsigned int cmd, struct page **pages,
12 			   unsigned long nr_pages, unsigned int gup_test_flags)
13 {
14 	unsigned long i;
15 
16 	switch (cmd) {
17 	case GUP_FAST_BENCHMARK:
18 	case GUP_BASIC_TEST:
19 		for (i = 0; i < nr_pages; i++)
20 			put_page(pages[i]);
21 		break;
22 
23 	case PIN_FAST_BENCHMARK:
24 	case PIN_BASIC_TEST:
25 	case PIN_LONGTERM_BENCHMARK:
26 		unpin_user_pages(pages, nr_pages);
27 		break;
28 	case DUMP_USER_PAGES_TEST:
29 		if (gup_test_flags & GUP_TEST_FLAG_DUMP_PAGES_USE_PIN) {
30 			unpin_user_pages(pages, nr_pages);
31 		} else {
32 			for (i = 0; i < nr_pages; i++)
33 				put_page(pages[i]);
34 
35 		}
36 		break;
37 	}
38 }
39 
40 static void verify_dma_pinned(unsigned int cmd, struct page **pages,
41 			      unsigned long nr_pages)
42 {
43 	unsigned long i;
44 	struct folio *folio;
45 
46 	switch (cmd) {
47 	case PIN_FAST_BENCHMARK:
48 	case PIN_BASIC_TEST:
49 	case PIN_LONGTERM_BENCHMARK:
50 		for (i = 0; i < nr_pages; i++) {
51 			folio = page_folio(pages[i]);
52 
53 			if (WARN(!folio_maybe_dma_pinned(folio),
54 				 "pages[%lu] is NOT dma-pinned\n", i)) {
55 
56 				dump_page(&folio->page, "gup_test failure");
57 				break;
58 			} else if (cmd == PIN_LONGTERM_BENCHMARK &&
59 				WARN(!folio_is_longterm_pinnable(folio),
60 				     "pages[%lu] is NOT pinnable but pinned\n",
61 				     i)) {
62 				dump_page(&folio->page, "gup_test failure");
63 				break;
64 			}
65 		}
66 		break;
67 	}
68 }
69 
70 static void dump_pages_test(struct gup_test *gup, struct page **pages,
71 			    unsigned long nr_pages)
72 {
73 	unsigned int index_to_dump;
74 	unsigned int i;
75 
76 	/*
77 	 * Zero out any user-supplied page index that is out of range. Remember:
78 	 * .which_pages[] contains a 1-based set of page indices.
79 	 */
80 	for (i = 0; i < GUP_TEST_MAX_PAGES_TO_DUMP; i++) {
81 		if (gup->which_pages[i] > nr_pages) {
82 			pr_warn("ZEROING due to out of range: .which_pages[%u]: %u\n",
83 				i, gup->which_pages[i]);
84 			gup->which_pages[i] = 0;
85 		}
86 	}
87 
88 	for (i = 0; i < GUP_TEST_MAX_PAGES_TO_DUMP; i++) {
89 		index_to_dump = gup->which_pages[i];
90 
91 		if (index_to_dump) {
92 			index_to_dump--; // Decode from 1-based, to 0-based
93 			pr_info("---- page #%u, starting from user virt addr: 0x%llx\n",
94 				index_to_dump, gup->addr);
95 			dump_page(pages[index_to_dump],
96 				  "gup_test: dump_pages() test");
97 		}
98 	}
99 }
100 
101 static int __gup_test_ioctl(unsigned int cmd,
102 		struct gup_test *gup)
103 {
104 	ktime_t start_time, end_time;
105 	unsigned long i, nr_pages, addr, next;
106 	long nr;
107 	struct page **pages;
108 	int ret = 0;
109 	bool needs_mmap_lock =
110 		cmd != GUP_FAST_BENCHMARK && cmd != PIN_FAST_BENCHMARK;
111 
112 	if (gup->size > ULONG_MAX)
113 		return -EINVAL;
114 
115 	nr_pages = gup->size / PAGE_SIZE;
116 	pages = kvcalloc(nr_pages, sizeof(void *), GFP_KERNEL);
117 	if (!pages)
118 		return -ENOMEM;
119 
120 	if (needs_mmap_lock && mmap_read_lock_killable(current->mm)) {
121 		ret = -EINTR;
122 		goto free_pages;
123 	}
124 
125 	i = 0;
126 	nr = gup->nr_pages_per_call;
127 	start_time = ktime_get();
128 	for (addr = gup->addr; addr < gup->addr + gup->size; addr = next) {
129 		if (nr != gup->nr_pages_per_call)
130 			break;
131 
132 		next = addr + nr * PAGE_SIZE;
133 		if (next > gup->addr + gup->size) {
134 			next = gup->addr + gup->size;
135 			nr = (next - addr) / PAGE_SIZE;
136 		}
137 
138 		switch (cmd) {
139 		case GUP_FAST_BENCHMARK:
140 			nr = get_user_pages_fast(addr, nr, gup->gup_flags,
141 						 pages + i);
142 			break;
143 		case GUP_BASIC_TEST:
144 			nr = get_user_pages(addr, nr, gup->gup_flags, pages + i);
145 			break;
146 		case PIN_FAST_BENCHMARK:
147 			nr = pin_user_pages_fast(addr, nr, gup->gup_flags,
148 						 pages + i);
149 			break;
150 		case PIN_BASIC_TEST:
151 			nr = pin_user_pages(addr, nr, gup->gup_flags, pages + i);
152 			break;
153 		case PIN_LONGTERM_BENCHMARK:
154 			nr = pin_user_pages(addr, nr,
155 					    gup->gup_flags | FOLL_LONGTERM,
156 					    pages + i);
157 			break;
158 		case DUMP_USER_PAGES_TEST:
159 			if (gup->test_flags & GUP_TEST_FLAG_DUMP_PAGES_USE_PIN)
160 				nr = pin_user_pages(addr, nr, gup->gup_flags,
161 						    pages + i);
162 			else
163 				nr = get_user_pages(addr, nr, gup->gup_flags,
164 						    pages + i);
165 			break;
166 		default:
167 			ret = -EINVAL;
168 			goto unlock;
169 		}
170 
171 		if (nr <= 0)
172 			break;
173 		i += nr;
174 	}
175 	end_time = ktime_get();
176 
177 	/* Shifting the meaning of nr_pages: now it is actual number pinned: */
178 	nr_pages = i;
179 
180 	gup->get_delta_usec = ktime_us_delta(end_time, start_time);
181 	gup->size = addr - gup->addr;
182 
183 	/*
184 	 * Take an un-benchmark-timed moment to verify DMA pinned
185 	 * state: print a warning if any non-dma-pinned pages are found:
186 	 */
187 	verify_dma_pinned(cmd, pages, nr_pages);
188 
189 	if (cmd == DUMP_USER_PAGES_TEST)
190 		dump_pages_test(gup, pages, nr_pages);
191 
192 	start_time = ktime_get();
193 
194 	put_back_pages(cmd, pages, nr_pages, gup->test_flags);
195 
196 	end_time = ktime_get();
197 	gup->put_delta_usec = ktime_us_delta(end_time, start_time);
198 
199 unlock:
200 	if (needs_mmap_lock)
201 		mmap_read_unlock(current->mm);
202 free_pages:
203 	kvfree(pages);
204 	return ret;
205 }
206 
207 static DEFINE_MUTEX(pin_longterm_test_mutex);
208 static struct page **pin_longterm_test_pages;
209 static unsigned long pin_longterm_test_nr_pages;
210 
211 static inline void pin_longterm_test_stop(void)
212 {
213 	if (pin_longterm_test_pages) {
214 		if (pin_longterm_test_nr_pages)
215 			unpin_user_pages(pin_longterm_test_pages,
216 					 pin_longterm_test_nr_pages);
217 		kvfree(pin_longterm_test_pages);
218 		pin_longterm_test_pages = NULL;
219 		pin_longterm_test_nr_pages = 0;
220 	}
221 }
222 
223 static inline int pin_longterm_test_start(unsigned long arg)
224 {
225 	long nr_pages, cur_pages, addr, remaining_pages;
226 	int gup_flags = FOLL_LONGTERM;
227 	struct pin_longterm_test args;
228 	struct page **pages;
229 	int ret = 0;
230 	bool fast;
231 
232 	if (pin_longterm_test_pages)
233 		return -EINVAL;
234 
235 	if (copy_from_user(&args, (void __user *)arg, sizeof(args)))
236 		return -EFAULT;
237 
238 	if (args.flags &
239 	    ~(PIN_LONGTERM_TEST_FLAG_USE_WRITE|PIN_LONGTERM_TEST_FLAG_USE_FAST))
240 		return -EINVAL;
241 	if (!IS_ALIGNED(args.addr | args.size, PAGE_SIZE))
242 		return -EINVAL;
243 	if (args.size > LONG_MAX)
244 		return -EINVAL;
245 	nr_pages = args.size / PAGE_SIZE;
246 	if (!nr_pages)
247 		return -EINVAL;
248 
249 	pages = kvcalloc(nr_pages, sizeof(void *), GFP_KERNEL);
250 	if (!pages)
251 		return -ENOMEM;
252 
253 	if (args.flags & PIN_LONGTERM_TEST_FLAG_USE_WRITE)
254 		gup_flags |= FOLL_WRITE;
255 	fast = !!(args.flags & PIN_LONGTERM_TEST_FLAG_USE_FAST);
256 
257 	if (!fast && mmap_read_lock_killable(current->mm)) {
258 		kvfree(pages);
259 		return -EINTR;
260 	}
261 
262 	pin_longterm_test_pages = pages;
263 	pin_longterm_test_nr_pages = 0;
264 
265 	while (nr_pages - pin_longterm_test_nr_pages) {
266 		remaining_pages = nr_pages - pin_longterm_test_nr_pages;
267 		addr = args.addr + pin_longterm_test_nr_pages * PAGE_SIZE;
268 
269 		if (fast)
270 			cur_pages = pin_user_pages_fast(addr, remaining_pages,
271 							gup_flags, pages);
272 		else
273 			cur_pages = pin_user_pages(addr, remaining_pages,
274 						   gup_flags, pages);
275 		if (cur_pages < 0) {
276 			pin_longterm_test_stop();
277 			ret = cur_pages;
278 			break;
279 		}
280 		pin_longterm_test_nr_pages += cur_pages;
281 		pages += cur_pages;
282 	}
283 
284 	if (!fast)
285 		mmap_read_unlock(current->mm);
286 	return ret;
287 }
288 
289 static inline int pin_longterm_test_read(unsigned long arg)
290 {
291 	__u64 user_addr;
292 	unsigned long i;
293 
294 	if (!pin_longterm_test_pages)
295 		return -EINVAL;
296 
297 	if (copy_from_user(&user_addr, (void __user *)arg, sizeof(user_addr)))
298 		return -EFAULT;
299 
300 	for (i = 0; i < pin_longterm_test_nr_pages; i++) {
301 		void *addr = kmap_local_page(pin_longterm_test_pages[i]);
302 		unsigned long ret;
303 
304 		ret = copy_to_user((void __user *)(unsigned long)user_addr, addr,
305 				   PAGE_SIZE);
306 		kunmap_local(addr);
307 		if (ret)
308 			return -EFAULT;
309 		user_addr += PAGE_SIZE;
310 	}
311 	return 0;
312 }
313 
314 static long pin_longterm_test_ioctl(struct file *filep, unsigned int cmd,
315 				    unsigned long arg)
316 {
317 	int ret = -EINVAL;
318 
319 	if (mutex_lock_killable(&pin_longterm_test_mutex))
320 		return -EINTR;
321 
322 	switch (cmd) {
323 	case PIN_LONGTERM_TEST_START:
324 		ret = pin_longterm_test_start(arg);
325 		break;
326 	case PIN_LONGTERM_TEST_STOP:
327 		pin_longterm_test_stop();
328 		ret = 0;
329 		break;
330 	case PIN_LONGTERM_TEST_READ:
331 		ret = pin_longterm_test_read(arg);
332 		break;
333 	}
334 
335 	mutex_unlock(&pin_longterm_test_mutex);
336 	return ret;
337 }
338 
339 static long gup_test_ioctl(struct file *filep, unsigned int cmd,
340 		unsigned long arg)
341 {
342 	struct gup_test gup;
343 	int ret;
344 
345 	switch (cmd) {
346 	case GUP_FAST_BENCHMARK:
347 	case PIN_FAST_BENCHMARK:
348 	case PIN_LONGTERM_BENCHMARK:
349 	case GUP_BASIC_TEST:
350 	case PIN_BASIC_TEST:
351 	case DUMP_USER_PAGES_TEST:
352 		break;
353 	case PIN_LONGTERM_TEST_START:
354 	case PIN_LONGTERM_TEST_STOP:
355 	case PIN_LONGTERM_TEST_READ:
356 		return pin_longterm_test_ioctl(filep, cmd, arg);
357 	default:
358 		return -EINVAL;
359 	}
360 
361 	if (copy_from_user(&gup, (void __user *)arg, sizeof(gup)))
362 		return -EFAULT;
363 
364 	ret = __gup_test_ioctl(cmd, &gup);
365 	if (ret)
366 		return ret;
367 
368 	if (copy_to_user((void __user *)arg, &gup, sizeof(gup)))
369 		return -EFAULT;
370 
371 	return 0;
372 }
373 
374 static int gup_test_release(struct inode *inode, struct file *file)
375 {
376 	pin_longterm_test_stop();
377 
378 	return 0;
379 }
380 
381 static const struct file_operations gup_test_fops = {
382 	.open = nonseekable_open,
383 	.unlocked_ioctl = gup_test_ioctl,
384 	.compat_ioctl = compat_ptr_ioctl,
385 	.release = gup_test_release,
386 };
387 
388 static int __init gup_test_init(void)
389 {
390 	debugfs_create_file_unsafe("gup_test", 0600, NULL, NULL,
391 				   &gup_test_fops);
392 
393 	return 0;
394 }
395 
396 late_initcall(gup_test_init);
397