mm/gup_benchmark.c: time put_page()
[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)
9
10struct gup_benchmark {
26db3d09
KB
11 __u64 get_delta_usec;
12 __u64 put_delta_usec;
64c349f4
KS
13 __u64 addr;
14 __u64 size;
15 __u32 nr_pages_per_call;
16 __u32 flags;
26db3d09 17 __u64 expansion[10]; /* For future use */
64c349f4
KS
18};
19
20static int __gup_benchmark_ioctl(unsigned int cmd,
21 struct gup_benchmark *gup)
22{
23 ktime_t start_time, end_time;
51896864
Y
24 unsigned long i, nr_pages, addr, next;
25 int nr;
64c349f4
KS
26 struct page **pages;
27
28 nr_pages = gup->size / PAGE_SIZE;
778e1cdd 29 pages = kvcalloc(nr_pages, sizeof(void *), GFP_KERNEL);
64c349f4
KS
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);
09e35a4a
MT
47 if (nr <= 0)
48 break;
64c349f4
KS
49 i += nr;
50 }
51 end_time = ktime_get();
52
26db3d09 53 gup->get_delta_usec = ktime_us_delta(end_time, start_time);
64c349f4
KS
54 gup->size = addr - gup->addr;
55
26db3d09 56 start_time = ktime_get();
64c349f4
KS
57 for (i = 0; i < nr_pages; i++) {
58 if (!pages[i])
59 break;
60 put_page(pages[i]);
61 }
26db3d09
KB
62 end_time = ktime_get();
63 gup->put_delta_usec = ktime_us_delta(end_time, start_time);
64c349f4
KS
64
65 kvfree(pages);
66 return 0;
67}
68
69static 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
91static const struct file_operations gup_benchmark_fops = {
92 .open = nonseekable_open,
93 .unlocked_ioctl = gup_benchmark_ioctl,
94};
95
96static 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
108late_initcall(gup_benchmark_init);