blob: ad9d5b1c44739755146c17f8e5a5f6ff4659bc50 [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;
Navid Emamdoosta7c46c02020-01-04 13:00:12 -080029 int ret = 0;
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -080030
Dan Carpenter4b408c72018-10-30 15:04:32 -070031 if (gup->size > ULONG_MAX)
32 return -EINVAL;
33
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -080034 nr_pages = gup->size / PAGE_SIZE;
Kees Cook778e1cd2018-06-12 14:04:48 -070035 pages = kvcalloc(nr_pages, sizeof(void *), GFP_KERNEL);
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -080036 if (!pages)
37 return -ENOMEM;
38
39 i = 0;
40 nr = gup->nr_pages_per_call;
41 start_time = ktime_get();
42 for (addr = gup->addr; addr < gup->addr + gup->size; addr = next) {
43 if (nr != gup->nr_pages_per_call)
44 break;
45
46 next = addr + nr * PAGE_SIZE;
47 if (next > gup->addr + gup->size) {
48 next = gup->addr + gup->size;
49 nr = (next - addr) / PAGE_SIZE;
50 }
51
Keith Busch714a3a12018-10-26 15:09:56 -070052 switch (cmd) {
53 case GUP_FAST_BENCHMARK:
54 nr = get_user_pages_fast(addr, nr, gup->flags & 1,
55 pages + i);
56 break;
57 case GUP_LONGTERM_BENCHMARK:
Ira Weiny932f4a62019-05-13 17:17:03 -070058 nr = get_user_pages(addr, nr,
59 (gup->flags & 1) | FOLL_LONGTERM,
60 pages + i, NULL);
Keith Busch714a3a12018-10-26 15:09:56 -070061 break;
62 case GUP_BENCHMARK:
63 nr = get_user_pages(addr, nr, gup->flags & 1, pages + i,
64 NULL);
65 break;
66 default:
Navid Emamdoosta7c46c02020-01-04 13:00:12 -080067 kvfree(pages);
68 ret = -EINVAL;
69 goto out;
Keith Busch714a3a12018-10-26 15:09:56 -070070 }
71
Michael S. Tsirkin09e35a42018-04-13 15:35:16 -070072 if (nr <= 0)
73 break;
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -080074 i += nr;
75 }
76 end_time = ktime_get();
77
Keith Busch26db3d02018-10-26 15:09:52 -070078 gup->get_delta_usec = ktime_us_delta(end_time, start_time);
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -080079 gup->size = addr - gup->addr;
80
Keith Busch26db3d02018-10-26 15:09:52 -070081 start_time = ktime_get();
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -080082 for (i = 0; i < nr_pages; i++) {
83 if (!pages[i])
84 break;
85 put_page(pages[i]);
86 }
Keith Busch26db3d02018-10-26 15:09:52 -070087 end_time = ktime_get();
88 gup->put_delta_usec = ktime_us_delta(end_time, start_time);
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -080089
90 kvfree(pages);
Navid Emamdoosta7c46c02020-01-04 13:00:12 -080091out:
92 return ret;
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -080093}
94
95static long gup_benchmark_ioctl(struct file *filep, unsigned int cmd,
96 unsigned long arg)
97{
98 struct gup_benchmark gup;
99 int ret;
100
Keith Busch714a3a12018-10-26 15:09:56 -0700101 switch (cmd) {
102 case GUP_FAST_BENCHMARK:
103 case GUP_LONGTERM_BENCHMARK:
104 case GUP_BENCHMARK:
105 break;
106 default:
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -0800107 return -EINVAL;
Keith Busch714a3a12018-10-26 15:09:56 -0700108 }
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -0800109
110 if (copy_from_user(&gup, (void __user *)arg, sizeof(gup)))
111 return -EFAULT;
112
113 ret = __gup_benchmark_ioctl(cmd, &gup);
114 if (ret)
115 return ret;
116
117 if (copy_to_user((void __user *)arg, &gup, sizeof(gup)))
118 return -EFAULT;
119
120 return 0;
121}
122
123static const struct file_operations gup_benchmark_fops = {
124 .open = nonseekable_open,
125 .unlocked_ioctl = gup_benchmark_ioctl,
126};
127
128static int gup_benchmark_init(void)
129{
Greg Kroah-Hartmand9f79792019-03-05 15:46:09 -0800130 debugfs_create_file_unsafe("gup_benchmark", 0600, NULL, NULL,
131 &gup_benchmark_fops);
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -0800132
133 return 0;
134}
135
136late_initcall(gup_benchmark_init);