blob: 5b42d3d4b60aa3a921002abf8f2872bc6e0764f8 [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)
Keith Busch714a3a12018-10-26 15:09:56 -07009#define GUP_LONGTERM_BENCHMARK _IOWR('g', 2, struct gup_benchmark)
10#define GUP_BENCHMARK _IOWR('g', 3, struct gup_benchmark)
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -080011
12struct gup_benchmark {
Keith Busch26db3d02018-10-26 15:09:52 -070013 __u64 get_delta_usec;
14 __u64 put_delta_usec;
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -080015 __u64 addr;
16 __u64 size;
17 __u32 nr_pages_per_call;
18 __u32 flags;
Keith Busch26db3d02018-10-26 15:09:52 -070019 __u64 expansion[10]; /* For future use */
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -080020};
21
22static int __gup_benchmark_ioctl(unsigned int cmd,
23 struct gup_benchmark *gup)
24{
25 ktime_t start_time, end_time;
YueHaibing51896862018-10-05 15:51:44 -070026 unsigned long i, nr_pages, addr, next;
27 int nr;
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -080028 struct page **pages;
29
Dan Carpenter4b408c72018-10-30 15:04:32 -070030 if (gup->size > ULONG_MAX)
31 return -EINVAL;
32
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -080033 nr_pages = gup->size / PAGE_SIZE;
Kees Cook778e1cd2018-06-12 14:04:48 -070034 pages = kvcalloc(nr_pages, sizeof(void *), GFP_KERNEL);
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -080035 if (!pages)
36 return -ENOMEM;
37
38 i = 0;
39 nr = gup->nr_pages_per_call;
40 start_time = ktime_get();
41 for (addr = gup->addr; addr < gup->addr + gup->size; addr = next) {
42 if (nr != gup->nr_pages_per_call)
43 break;
44
45 next = addr + nr * PAGE_SIZE;
46 if (next > gup->addr + gup->size) {
47 next = gup->addr + gup->size;
48 nr = (next - addr) / PAGE_SIZE;
49 }
50
Keith Busch714a3a12018-10-26 15:09:56 -070051 switch (cmd) {
52 case GUP_FAST_BENCHMARK:
53 nr = get_user_pages_fast(addr, nr, gup->flags & 1,
54 pages + i);
55 break;
56 case GUP_LONGTERM_BENCHMARK:
57 nr = get_user_pages_longterm(addr, nr, gup->flags & 1,
58 pages + i, NULL);
59 break;
60 case GUP_BENCHMARK:
61 nr = get_user_pages(addr, nr, gup->flags & 1, pages + i,
62 NULL);
63 break;
64 default:
65 return -1;
66 }
67
Michael S. Tsirkin09e35a42018-04-13 15:35:16 -070068 if (nr <= 0)
69 break;
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -080070 i += nr;
71 }
72 end_time = ktime_get();
73
Keith Busch26db3d02018-10-26 15:09:52 -070074 gup->get_delta_usec = ktime_us_delta(end_time, start_time);
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -080075 gup->size = addr - gup->addr;
76
Keith Busch26db3d02018-10-26 15:09:52 -070077 start_time = ktime_get();
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -080078 for (i = 0; i < nr_pages; i++) {
79 if (!pages[i])
80 break;
81 put_page(pages[i]);
82 }
Keith Busch26db3d02018-10-26 15:09:52 -070083 end_time = ktime_get();
84 gup->put_delta_usec = ktime_us_delta(end_time, start_time);
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -080085
86 kvfree(pages);
87 return 0;
88}
89
90static long gup_benchmark_ioctl(struct file *filep, unsigned int cmd,
91 unsigned long arg)
92{
93 struct gup_benchmark gup;
94 int ret;
95
Keith Busch714a3a12018-10-26 15:09:56 -070096 switch (cmd) {
97 case GUP_FAST_BENCHMARK:
98 case GUP_LONGTERM_BENCHMARK:
99 case GUP_BENCHMARK:
100 break;
101 default:
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -0800102 return -EINVAL;
Keith Busch714a3a12018-10-26 15:09:56 -0700103 }
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -0800104
105 if (copy_from_user(&gup, (void __user *)arg, sizeof(gup)))
106 return -EFAULT;
107
108 ret = __gup_benchmark_ioctl(cmd, &gup);
109 if (ret)
110 return ret;
111
112 if (copy_to_user((void __user *)arg, &gup, sizeof(gup)))
113 return -EFAULT;
114
115 return 0;
116}
117
118static const struct file_operations gup_benchmark_fops = {
119 .open = nonseekable_open,
120 .unlocked_ioctl = gup_benchmark_ioctl,
121};
122
123static int gup_benchmark_init(void)
124{
125 void *ret;
126
127 ret = debugfs_create_file_unsafe("gup_benchmark", 0600, NULL, NULL,
128 &gup_benchmark_fops);
129 if (!ret)
130 pr_warn("Failed to create gup_benchmark in debugfs");
131
132 return 0;
133}
134
135late_initcall(gup_benchmark_init);