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)
9 #define GUP_LONGTERM_BENCHMARK _IOWR('g', 2, struct gup_benchmark)
10 #define GUP_BENCHMARK _IOWR('g', 3, struct gup_benchmark)
12 struct gup_benchmark
{
17 __u32 nr_pages_per_call
;
19 __u64 expansion
[10]; /* For future use */
22 static int __gup_benchmark_ioctl(unsigned int cmd
,
23 struct gup_benchmark
*gup
)
25 ktime_t start_time
, end_time
;
26 unsigned long i
, nr_pages
, addr
, next
;
31 if (gup
->size
> ULONG_MAX
)
34 nr_pages
= gup
->size
/ PAGE_SIZE
;
35 pages
= kvcalloc(nr_pages
, sizeof(void *), GFP_KERNEL
);
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
)
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
;
52 /* Filter out most gup flags: only allow a tiny subset here: */
53 gup
->flags
&= FOLL_WRITE
;
56 case GUP_FAST_BENCHMARK
:
57 nr
= get_user_pages_fast(addr
, nr
, gup
->flags
,
60 case GUP_LONGTERM_BENCHMARK
:
61 nr
= get_user_pages(addr
, nr
,
62 gup
->flags
| FOLL_LONGTERM
,
66 nr
= get_user_pages(addr
, nr
, gup
->flags
, pages
+ i
,
79 end_time
= ktime_get();
81 gup
->get_delta_usec
= ktime_us_delta(end_time
, start_time
);
82 gup
->size
= addr
- gup
->addr
;
84 start_time
= ktime_get();
85 for (i
= 0; i
< nr_pages
; i
++) {
90 end_time
= ktime_get();
91 gup
->put_delta_usec
= ktime_us_delta(end_time
, start_time
);
98 static long gup_benchmark_ioctl(struct file
*filep
, unsigned int cmd
,
101 struct gup_benchmark gup
;
105 case GUP_FAST_BENCHMARK
:
106 case GUP_LONGTERM_BENCHMARK
:
113 if (copy_from_user(&gup
, (void __user
*)arg
, sizeof(gup
)))
116 ret
= __gup_benchmark_ioctl(cmd
, &gup
);
120 if (copy_to_user((void __user
*)arg
, &gup
, sizeof(gup
)))
126 static const struct file_operations gup_benchmark_fops
= {
127 .open
= nonseekable_open
,
128 .unlocked_ioctl
= gup_benchmark_ioctl
,
131 static int gup_benchmark_init(void)
133 debugfs_create_file_unsafe("gup_benchmark", 0600, NULL
, NULL
,
134 &gup_benchmark_fops
);
139 late_initcall(gup_benchmark_init
);