Merge tag 'acpi-4.20-rc1-2' of git://git.kernel.org/pub/scm/linux/kernel/git/rafael...
[linux-2.6-block.git] / mm / gup_benchmark.c
CommitLineData
64c349f4
KS
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
8#define GUP_FAST_BENCHMARK _IOWR('g', 1, struct gup_benchmark)
714a3a1e
KB
9#define GUP_LONGTERM_BENCHMARK _IOWR('g', 2, struct gup_benchmark)
10#define GUP_BENCHMARK _IOWR('g', 3, struct gup_benchmark)
64c349f4
KS
11
12struct gup_benchmark {
26db3d09
KB
13 __u64 get_delta_usec;
14 __u64 put_delta_usec;
64c349f4
KS
15 __u64 addr;
16 __u64 size;
17 __u32 nr_pages_per_call;
18 __u32 flags;
26db3d09 19 __u64 expansion[10]; /* For future use */
64c349f4
KS
20};
21
22static int __gup_benchmark_ioctl(unsigned int cmd,
23 struct gup_benchmark *gup)
24{
25 ktime_t start_time, end_time;
51896864
Y
26 unsigned long i, nr_pages, addr, next;
27 int nr;
64c349f4
KS
28 struct page **pages;
29
30 nr_pages = gup->size / PAGE_SIZE;
778e1cdd 31 pages = kvcalloc(nr_pages, sizeof(void *), GFP_KERNEL);
64c349f4
KS
32 if (!pages)
33 return -ENOMEM;
34
35 i = 0;
36 nr = gup->nr_pages_per_call;
37 start_time = ktime_get();
38 for (addr = gup->addr; addr < gup->addr + gup->size; addr = next) {
39 if (nr != gup->nr_pages_per_call)
40 break;
41
42 next = addr + nr * PAGE_SIZE;
43 if (next > gup->addr + gup->size) {
44 next = gup->addr + gup->size;
45 nr = (next - addr) / PAGE_SIZE;
46 }
47
714a3a1e
KB
48 switch (cmd) {
49 case GUP_FAST_BENCHMARK:
50 nr = get_user_pages_fast(addr, nr, gup->flags & 1,
51 pages + i);
52 break;
53 case GUP_LONGTERM_BENCHMARK:
54 nr = get_user_pages_longterm(addr, nr, gup->flags & 1,
55 pages + i, NULL);
56 break;
57 case GUP_BENCHMARK:
58 nr = get_user_pages(addr, nr, gup->flags & 1, pages + i,
59 NULL);
60 break;
61 default:
62 return -1;
63 }
64
09e35a4a
MT
65 if (nr <= 0)
66 break;
64c349f4
KS
67 i += nr;
68 }
69 end_time = ktime_get();
70
26db3d09 71 gup->get_delta_usec = ktime_us_delta(end_time, start_time);
64c349f4
KS
72 gup->size = addr - gup->addr;
73
26db3d09 74 start_time = ktime_get();
64c349f4
KS
75 for (i = 0; i < nr_pages; i++) {
76 if (!pages[i])
77 break;
78 put_page(pages[i]);
79 }
26db3d09
KB
80 end_time = ktime_get();
81 gup->put_delta_usec = ktime_us_delta(end_time, start_time);
64c349f4
KS
82
83 kvfree(pages);
84 return 0;
85}
86
87static long gup_benchmark_ioctl(struct file *filep, unsigned int cmd,
88 unsigned long arg)
89{
90 struct gup_benchmark gup;
91 int ret;
92
714a3a1e
KB
93 switch (cmd) {
94 case GUP_FAST_BENCHMARK:
95 case GUP_LONGTERM_BENCHMARK:
96 case GUP_BENCHMARK:
97 break;
98 default:
64c349f4 99 return -EINVAL;
714a3a1e 100 }
64c349f4
KS
101
102 if (copy_from_user(&gup, (void __user *)arg, sizeof(gup)))
103 return -EFAULT;
104
105 ret = __gup_benchmark_ioctl(cmd, &gup);
106 if (ret)
107 return ret;
108
109 if (copy_to_user((void __user *)arg, &gup, sizeof(gup)))
110 return -EFAULT;
111
112 return 0;
113}
114
115static const struct file_operations gup_benchmark_fops = {
116 .open = nonseekable_open,
117 .unlocked_ioctl = gup_benchmark_ioctl,
118};
119
120static int gup_benchmark_init(void)
121{
122 void *ret;
123
124 ret = debugfs_create_file_unsafe("gup_benchmark", 0600, NULL, NULL,
125 &gup_benchmark_fops);
126 if (!ret)
127 pr_warn("Failed to create gup_benchmark in debugfs");
128
129 return 0;
130}
131
132late_initcall(gup_benchmark_init);