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)
11 #define PIN_FAST_BENCHMARK _IOWR('g', 4, struct gup_benchmark)
12 #define PIN_BENCHMARK _IOWR('g', 5, struct gup_benchmark)
14 struct gup_benchmark
{
19 __u32 nr_pages_per_call
;
21 __u64 expansion
[10]; /* For future use */
24 static void put_back_pages(unsigned int cmd
, struct page
**pages
,
25 unsigned long nr_pages
)
30 case GUP_FAST_BENCHMARK
:
31 case GUP_LONGTERM_BENCHMARK
:
33 for (i
= 0; i
< nr_pages
; i
++)
37 case PIN_FAST_BENCHMARK
:
39 unpin_user_pages(pages
, nr_pages
);
44 static void verify_dma_pinned(unsigned int cmd
, struct page
**pages
,
45 unsigned long nr_pages
)
51 case PIN_FAST_BENCHMARK
:
53 for (i
= 0; i
< nr_pages
; i
++) {
55 if (WARN(!page_maybe_dma_pinned(page
),
56 "pages[%lu] is NOT dma-pinned\n", i
)) {
58 dump_page(page
, "gup_benchmark failure");
66 static int __gup_benchmark_ioctl(unsigned int cmd
,
67 struct gup_benchmark
*gup
)
69 ktime_t start_time
, end_time
;
70 unsigned long i
, nr_pages
, addr
, next
;
75 if (gup
->size
> ULONG_MAX
)
78 nr_pages
= gup
->size
/ PAGE_SIZE
;
79 pages
= kvcalloc(nr_pages
, sizeof(void *), GFP_KERNEL
);
84 nr
= gup
->nr_pages_per_call
;
85 start_time
= ktime_get();
86 for (addr
= gup
->addr
; addr
< gup
->addr
+ gup
->size
; addr
= next
) {
87 if (nr
!= gup
->nr_pages_per_call
)
90 next
= addr
+ nr
* PAGE_SIZE
;
91 if (next
> gup
->addr
+ gup
->size
) {
92 next
= gup
->addr
+ gup
->size
;
93 nr
= (next
- addr
) / PAGE_SIZE
;
96 /* Filter out most gup flags: only allow a tiny subset here: */
97 gup
->flags
&= FOLL_WRITE
;
100 case GUP_FAST_BENCHMARK
:
101 nr
= get_user_pages_fast(addr
, nr
, gup
->flags
,
104 case GUP_LONGTERM_BENCHMARK
:
105 nr
= get_user_pages(addr
, nr
,
106 gup
->flags
| FOLL_LONGTERM
,
110 nr
= get_user_pages(addr
, nr
, gup
->flags
, pages
+ i
,
113 case PIN_FAST_BENCHMARK
:
114 nr
= pin_user_pages_fast(addr
, nr
, gup
->flags
,
118 nr
= pin_user_pages(addr
, nr
, gup
->flags
, pages
+ i
,
131 end_time
= ktime_get();
133 /* Shifting the meaning of nr_pages: now it is actual number pinned: */
136 gup
->get_delta_usec
= ktime_us_delta(end_time
, start_time
);
137 gup
->size
= addr
- gup
->addr
;
140 * Take an un-benchmark-timed moment to verify DMA pinned
141 * state: print a warning if any non-dma-pinned pages are found:
143 verify_dma_pinned(cmd
, pages
, nr_pages
);
145 start_time
= ktime_get();
147 put_back_pages(cmd
, pages
, nr_pages
);
149 end_time
= ktime_get();
150 gup
->put_delta_usec
= ktime_us_delta(end_time
, start_time
);
157 static long gup_benchmark_ioctl(struct file
*filep
, unsigned int cmd
,
160 struct gup_benchmark gup
;
164 case GUP_FAST_BENCHMARK
:
165 case GUP_LONGTERM_BENCHMARK
:
167 case PIN_FAST_BENCHMARK
:
174 if (copy_from_user(&gup
, (void __user
*)arg
, sizeof(gup
)))
177 ret
= __gup_benchmark_ioctl(cmd
, &gup
);
181 if (copy_to_user((void __user
*)arg
, &gup
, sizeof(gup
)))
187 static const struct file_operations gup_benchmark_fops
= {
188 .open
= nonseekable_open
,
189 .unlocked_ioctl
= gup_benchmark_ioctl
,
192 static int gup_benchmark_init(void)
194 debugfs_create_file_unsafe("gup_benchmark", 0600, NULL
, NULL
,
195 &gup_benchmark_fops
);
200 late_initcall(gup_benchmark_init
);