1 #include <linux/kernel.h>
3 #include <linux/slab.h>
4 #include <linux/uaccess.h>
5 #include <linux/ktime.h>
6 #include <linux/debugfs.h>
8 #define GUP_FAST_BENCHMARK _IOWR('g', 1, struct gup_benchmark)
10 struct gup_benchmark {
15 __u32 nr_pages_per_call;
17 __u64 expansion[10]; /* For future use */
20 static int __gup_benchmark_ioctl(unsigned int cmd,
21 struct gup_benchmark *gup)
23 ktime_t start_time, end_time;
24 unsigned long i, nr_pages, addr, next;
28 nr_pages = gup->size / PAGE_SIZE;
29 pages = kvcalloc(nr_pages, sizeof(void *), GFP_KERNEL);
34 nr = gup->nr_pages_per_call;
35 start_time = ktime_get();
36 for (addr = gup->addr; addr < gup->addr + gup->size; addr = next) {
37 if (nr != gup->nr_pages_per_call)
40 next = addr + nr * PAGE_SIZE;
41 if (next > gup->addr + gup->size) {
42 next = gup->addr + gup->size;
43 nr = (next - addr) / PAGE_SIZE;
46 nr = get_user_pages_fast(addr, nr, gup->flags & 1, pages + i);
51 end_time = ktime_get();
53 gup->get_delta_usec = ktime_us_delta(end_time, start_time);
54 gup->size = addr - gup->addr;
56 start_time = ktime_get();
57 for (i = 0; i < nr_pages; i++) {
62 end_time = ktime_get();
63 gup->put_delta_usec = ktime_us_delta(end_time, start_time);
69 static long gup_benchmark_ioctl(struct file *filep, unsigned int cmd,
72 struct gup_benchmark gup;
75 if (cmd != GUP_FAST_BENCHMARK)
78 if (copy_from_user(&gup, (void __user *)arg, sizeof(gup)))
81 ret = __gup_benchmark_ioctl(cmd, &gup);
85 if (copy_to_user((void __user *)arg, &gup, sizeof(gup)))
91 static const struct file_operations gup_benchmark_fops = {
92 .open = nonseekable_open,
93 .unlocked_ioctl = gup_benchmark_ioctl,
96 static int gup_benchmark_init(void)
100 ret = debugfs_create_file_unsafe("gup_benchmark", 0600, NULL, NULL,
101 &gup_benchmark_fops);
103 pr_warn("Failed to create gup_benchmark in debugfs");
108 late_initcall(gup_benchmark_init);