]> git.ipfire.org Git - thirdparty/kernel/linux.git/blob - mm/gup_benchmark.c
mm/gup_benchmark.c: time put_page()
[thirdparty/kernel/linux.git] / mm / gup_benchmark.c
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)
9
10 struct gup_benchmark {
11 __u64 get_delta_usec;
12 __u64 put_delta_usec;
13 __u64 addr;
14 __u64 size;
15 __u32 nr_pages_per_call;
16 __u32 flags;
17 __u64 expansion[10]; /* For future use */
18 };
19
20 static int __gup_benchmark_ioctl(unsigned int cmd,
21 struct gup_benchmark *gup)
22 {
23 ktime_t start_time, end_time;
24 unsigned long i, nr_pages, addr, next;
25 int nr;
26 struct page **pages;
27
28 nr_pages = gup->size / PAGE_SIZE;
29 pages = kvcalloc(nr_pages, sizeof(void *), GFP_KERNEL);
30 if (!pages)
31 return -ENOMEM;
32
33 i = 0;
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)
38 break;
39
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;
44 }
45
46 nr = get_user_pages_fast(addr, nr, gup->flags & 1, pages + i);
47 if (nr <= 0)
48 break;
49 i += nr;
50 }
51 end_time = ktime_get();
52
53 gup->get_delta_usec = ktime_us_delta(end_time, start_time);
54 gup->size = addr - gup->addr;
55
56 start_time = ktime_get();
57 for (i = 0; i < nr_pages; i++) {
58 if (!pages[i])
59 break;
60 put_page(pages[i]);
61 }
62 end_time = ktime_get();
63 gup->put_delta_usec = ktime_us_delta(end_time, start_time);
64
65 kvfree(pages);
66 return 0;
67 }
68
69 static long gup_benchmark_ioctl(struct file *filep, unsigned int cmd,
70 unsigned long arg)
71 {
72 struct gup_benchmark gup;
73 int ret;
74
75 if (cmd != GUP_FAST_BENCHMARK)
76 return -EINVAL;
77
78 if (copy_from_user(&gup, (void __user *)arg, sizeof(gup)))
79 return -EFAULT;
80
81 ret = __gup_benchmark_ioctl(cmd, &gup);
82 if (ret)
83 return ret;
84
85 if (copy_to_user((void __user *)arg, &gup, sizeof(gup)))
86 return -EFAULT;
87
88 return 0;
89 }
90
91 static const struct file_operations gup_benchmark_fops = {
92 .open = nonseekable_open,
93 .unlocked_ioctl = gup_benchmark_ioctl,
94 };
95
96 static int gup_benchmark_init(void)
97 {
98 void *ret;
99
100 ret = debugfs_create_file_unsafe("gup_benchmark", 0600, NULL, NULL,
101 &gup_benchmark_fops);
102 if (!ret)
103 pr_warn("Failed to create gup_benchmark in debugfs");
104
105 return 0;
106 }
107
108 late_initcall(gup_benchmark_init);