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
{
14 __u32 nr_pages_per_call
;
18 static int __gup_benchmark_ioctl(unsigned int cmd
,
19 struct gup_benchmark
*gup
)
21 ktime_t start_time
, end_time
;
22 unsigned long i
, nr_pages
, addr
, next
;
26 if (gup
->size
> ULONG_MAX
)
29 nr_pages
= gup
->size
/ PAGE_SIZE
;
30 pages
= kvcalloc(nr_pages
, sizeof(void *), GFP_KERNEL
);
35 nr
= gup
->nr_pages_per_call
;
36 start_time
= ktime_get();
37 for (addr
= gup
->addr
; addr
< gup
->addr
+ gup
->size
; addr
= next
) {
38 if (nr
!= gup
->nr_pages_per_call
)
41 next
= addr
+ nr
* PAGE_SIZE
;
42 if (next
> gup
->addr
+ gup
->size
) {
43 next
= gup
->addr
+ gup
->size
;
44 nr
= (next
- addr
) / PAGE_SIZE
;
47 nr
= get_user_pages_fast(addr
, nr
, gup
->flags
& 1, pages
+ i
);
52 end_time
= ktime_get();
54 gup
->delta_usec
= ktime_us_delta(end_time
, start_time
);
55 gup
->size
= addr
- gup
->addr
;
57 for (i
= 0; i
< nr_pages
; i
++) {
67 static long gup_benchmark_ioctl(struct file
*filep
, unsigned int cmd
,
70 struct gup_benchmark gup
;
73 if (cmd
!= GUP_FAST_BENCHMARK
)
76 if (copy_from_user(&gup
, (void __user
*)arg
, sizeof(gup
)))
79 ret
= __gup_benchmark_ioctl(cmd
, &gup
);
83 if (copy_to_user((void __user
*)arg
, &gup
, sizeof(gup
)))
89 static const struct file_operations gup_benchmark_fops
= {
90 .open
= nonseekable_open
,
91 .unlocked_ioctl
= gup_benchmark_ioctl
,
94 static int gup_benchmark_init(void)
98 ret
= debugfs_create_file_unsafe("gup_benchmark", 0600, NULL
, NULL
,
101 pr_warn("Failed to create gup_benchmark in debugfs");
106 late_initcall(gup_benchmark_init
);