blob: 7e6f2d2dafb55acf8760b289b334eab25ebb3345 [file] [log] [blame]
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -08001#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
8#define GUP_FAST_BENCHMARK _IOWR('g', 1, struct gup_benchmark)
9
10struct gup_benchmark {
11 __u64 delta_usec;
12 __u64 addr;
13 __u64 size;
14 __u32 nr_pages_per_call;
15 __u32 flags;
16};
17
18static int __gup_benchmark_ioctl(unsigned int cmd,
19 struct gup_benchmark *gup)
20{
21 ktime_t start_time, end_time;
YueHaibing51896862018-10-05 15:51:44 -070022 unsigned long i, nr_pages, addr, next;
23 int nr;
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -080024 struct page **pages;
25
Dan Carpenter30598422018-10-30 15:04:32 -070026 if (gup->size > ULONG_MAX)
27 return -EINVAL;
28
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -080029 nr_pages = gup->size / PAGE_SIZE;
Kees Cook778e1cd2018-06-12 14:04:48 -070030 pages = kvcalloc(nr_pages, sizeof(void *), GFP_KERNEL);
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -080031 if (!pages)
32 return -ENOMEM;
33
34 i = 0;
35 nr = gup->nr_pages_per_call;
36 start_time = ktime_get();
37 for (addr = gup->addr; addr < gup->addr + gup->size; addr = next) {
38 if (nr != gup->nr_pages_per_call)
39 break;
40
41 next = addr + nr * PAGE_SIZE;
42 if (next > gup->addr + gup->size) {
43 next = gup->addr + gup->size;
44 nr = (next - addr) / PAGE_SIZE;
45 }
46
47 nr = get_user_pages_fast(addr, nr, gup->flags & 1, pages + i);
Michael S. Tsirkin09e35a42018-04-13 15:35:16 -070048 if (nr <= 0)
49 break;
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -080050 i += nr;
51 }
52 end_time = ktime_get();
53
54 gup->delta_usec = ktime_us_delta(end_time, start_time);
55 gup->size = addr - gup->addr;
56
57 for (i = 0; i < nr_pages; i++) {
58 if (!pages[i])
59 break;
60 put_page(pages[i]);
61 }
62
63 kvfree(pages);
64 return 0;
65}
66
67static long gup_benchmark_ioctl(struct file *filep, unsigned int cmd,
68 unsigned long arg)
69{
70 struct gup_benchmark gup;
71 int ret;
72
73 if (cmd != GUP_FAST_BENCHMARK)
74 return -EINVAL;
75
76 if (copy_from_user(&gup, (void __user *)arg, sizeof(gup)))
77 return -EFAULT;
78
79 ret = __gup_benchmark_ioctl(cmd, &gup);
80 if (ret)
81 return ret;
82
83 if (copy_to_user((void __user *)arg, &gup, sizeof(gup)))
84 return -EFAULT;
85
86 return 0;
87}
88
89static const struct file_operations gup_benchmark_fops = {
90 .open = nonseekable_open,
91 .unlocked_ioctl = gup_benchmark_ioctl,
92};
93
94static int gup_benchmark_init(void)
95{
96 void *ret;
97
98 ret = debugfs_create_file_unsafe("gup_benchmark", 0600, NULL, NULL,
99 &gup_benchmark_fops);
100 if (!ret)
101 pr_warn("Failed to create gup_benchmark in debugfs");
102
103 return 0;
104}
105
106late_initcall(gup_benchmark_init);