Linux Audio

Check our new training course

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