blob: 173bb38f368824e79273ccfa35f6ffe768f60ccf [file] [log] [blame]
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -08001#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>
John Hubbardb9dcfdf2020-12-14 19:05:08 -08007#include "gup_test.h"
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -08008
John Hubbard41c45d32020-04-01 21:05:41 -07009static void put_back_pages(unsigned int cmd, struct page **pages,
10 unsigned long nr_pages)
11{
12 unsigned long i;
13
14 switch (cmd) {
15 case GUP_FAST_BENCHMARK:
John Hubbarda9bed1e2020-12-14 19:05:17 -080016 case GUP_BASIC_TEST:
John Hubbard41c45d32020-04-01 21:05:41 -070017 for (i = 0; i < nr_pages; i++)
18 put_page(pages[i]);
19 break;
20
21 case PIN_FAST_BENCHMARK:
John Hubbarda9bed1e2020-12-14 19:05:17 -080022 case PIN_BASIC_TEST:
Barry Song657d4f72020-10-13 16:51:54 -070023 case PIN_LONGTERM_BENCHMARK:
John Hubbard41c45d32020-04-01 21:05:41 -070024 unpin_user_pages(pages, nr_pages);
25 break;
26 }
27}
28
29static void verify_dma_pinned(unsigned int cmd, struct page **pages,
30 unsigned long nr_pages)
31{
32 unsigned long i;
33 struct page *page;
34
35 switch (cmd) {
36 case PIN_FAST_BENCHMARK:
John Hubbarda9bed1e2020-12-14 19:05:17 -080037 case PIN_BASIC_TEST:
Barry Song657d4f72020-10-13 16:51:54 -070038 case PIN_LONGTERM_BENCHMARK:
John Hubbard41c45d32020-04-01 21:05:41 -070039 for (i = 0; i < nr_pages; i++) {
40 page = pages[i];
41 if (WARN(!page_maybe_dma_pinned(page),
42 "pages[%lu] is NOT dma-pinned\n", i)) {
43
John Hubbard9c84f222020-12-14 19:05:05 -080044 dump_page(page, "gup_test failure");
John Hubbard41c45d32020-04-01 21:05:41 -070045 break;
46 }
47 }
48 break;
49 }
50}
51
John Hubbard9c84f222020-12-14 19:05:05 -080052static int __gup_test_ioctl(unsigned int cmd,
53 struct gup_test *gup)
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -080054{
55 ktime_t start_time, end_time;
YueHaibing51896862018-10-05 15:51:44 -070056 unsigned long i, nr_pages, addr, next;
57 int nr;
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -080058 struct page **pages;
Navid Emamdoosta7c46c02020-01-04 13:00:12 -080059 int ret = 0;
Jann Hornf3964592020-10-17 16:14:12 -070060 bool needs_mmap_lock =
61 cmd != GUP_FAST_BENCHMARK && cmd != PIN_FAST_BENCHMARK;
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -080062
Dan Carpenter4b408c72018-10-30 15:04:32 -070063 if (gup->size > ULONG_MAX)
64 return -EINVAL;
65
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -080066 nr_pages = gup->size / PAGE_SIZE;
Kees Cook778e1cd2018-06-12 14:04:48 -070067 pages = kvcalloc(nr_pages, sizeof(void *), GFP_KERNEL);
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -080068 if (!pages)
69 return -ENOMEM;
70
Jann Hornf3964592020-10-17 16:14:12 -070071 if (needs_mmap_lock && mmap_read_lock_killable(current->mm)) {
72 ret = -EINTR;
73 goto free_pages;
74 }
75
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -080076 i = 0;
77 nr = gup->nr_pages_per_call;
78 start_time = ktime_get();
79 for (addr = gup->addr; addr < gup->addr + gup->size; addr = next) {
80 if (nr != gup->nr_pages_per_call)
81 break;
82
83 next = addr + nr * PAGE_SIZE;
84 if (next > gup->addr + gup->size) {
85 next = gup->addr + gup->size;
86 nr = (next - addr) / PAGE_SIZE;
87 }
88
John Hubbardbdffe232020-01-30 22:13:32 -080089 /* Filter out most gup flags: only allow a tiny subset here: */
90 gup->flags &= FOLL_WRITE;
91
Keith Busch714a3a12018-10-26 15:09:56 -070092 switch (cmd) {
93 case GUP_FAST_BENCHMARK:
John Hubbardbdffe232020-01-30 22:13:32 -080094 nr = get_user_pages_fast(addr, nr, gup->flags,
Keith Busch714a3a12018-10-26 15:09:56 -070095 pages + i);
96 break;
John Hubbarda9bed1e2020-12-14 19:05:17 -080097 case GUP_BASIC_TEST:
John Hubbardbdffe232020-01-30 22:13:32 -080098 nr = get_user_pages(addr, nr, gup->flags, pages + i,
Keith Busch714a3a12018-10-26 15:09:56 -070099 NULL);
100 break;
John Hubbard41c45d32020-04-01 21:05:41 -0700101 case PIN_FAST_BENCHMARK:
102 nr = pin_user_pages_fast(addr, nr, gup->flags,
103 pages + i);
104 break;
John Hubbarda9bed1e2020-12-14 19:05:17 -0800105 case PIN_BASIC_TEST:
John Hubbard41c45d32020-04-01 21:05:41 -0700106 nr = pin_user_pages(addr, nr, gup->flags, pages + i,
107 NULL);
108 break;
Barry Song657d4f72020-10-13 16:51:54 -0700109 case PIN_LONGTERM_BENCHMARK:
110 nr = pin_user_pages(addr, nr,
111 gup->flags | FOLL_LONGTERM,
112 pages + i, NULL);
113 break;
Keith Busch714a3a12018-10-26 15:09:56 -0700114 default:
Navid Emamdoosta7c46c02020-01-04 13:00:12 -0800115 ret = -EINVAL;
Jann Hornf3964592020-10-17 16:14:12 -0700116 goto unlock;
Keith Busch714a3a12018-10-26 15:09:56 -0700117 }
118
Michael S. Tsirkin09e35a42018-04-13 15:35:16 -0700119 if (nr <= 0)
120 break;
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -0800121 i += nr;
122 }
123 end_time = ktime_get();
124
John Hubbard41c45d32020-04-01 21:05:41 -0700125 /* Shifting the meaning of nr_pages: now it is actual number pinned: */
126 nr_pages = i;
127
Keith Busch26db3d02018-10-26 15:09:52 -0700128 gup->get_delta_usec = ktime_us_delta(end_time, start_time);
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -0800129 gup->size = addr - gup->addr;
130
John Hubbard41c45d32020-04-01 21:05:41 -0700131 /*
132 * Take an un-benchmark-timed moment to verify DMA pinned
133 * state: print a warning if any non-dma-pinned pages are found:
134 */
135 verify_dma_pinned(cmd, pages, nr_pages);
136
Keith Busch26db3d02018-10-26 15:09:52 -0700137 start_time = ktime_get();
John Hubbard41c45d32020-04-01 21:05:41 -0700138
139 put_back_pages(cmd, pages, nr_pages);
140
Keith Busch26db3d02018-10-26 15:09:52 -0700141 end_time = ktime_get();
142 gup->put_delta_usec = ktime_us_delta(end_time, start_time);
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -0800143
Jann Hornf3964592020-10-17 16:14:12 -0700144unlock:
145 if (needs_mmap_lock)
146 mmap_read_unlock(current->mm);
147free_pages:
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -0800148 kvfree(pages);
Navid Emamdoosta7c46c02020-01-04 13:00:12 -0800149 return ret;
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -0800150}
151
John Hubbard9c84f222020-12-14 19:05:05 -0800152static long gup_test_ioctl(struct file *filep, unsigned int cmd,
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -0800153 unsigned long arg)
154{
John Hubbard9c84f222020-12-14 19:05:05 -0800155 struct gup_test gup;
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -0800156 int ret;
157
Keith Busch714a3a12018-10-26 15:09:56 -0700158 switch (cmd) {
159 case GUP_FAST_BENCHMARK:
John Hubbard41c45d32020-04-01 21:05:41 -0700160 case PIN_FAST_BENCHMARK:
Barry Song657d4f72020-10-13 16:51:54 -0700161 case PIN_LONGTERM_BENCHMARK:
John Hubbarda9bed1e2020-12-14 19:05:17 -0800162 case GUP_BASIC_TEST:
163 case PIN_BASIC_TEST:
Keith Busch714a3a12018-10-26 15:09:56 -0700164 break;
165 default:
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -0800166 return -EINVAL;
Keith Busch714a3a12018-10-26 15:09:56 -0700167 }
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -0800168
169 if (copy_from_user(&gup, (void __user *)arg, sizeof(gup)))
170 return -EFAULT;
171
John Hubbard9c84f222020-12-14 19:05:05 -0800172 ret = __gup_test_ioctl(cmd, &gup);
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -0800173 if (ret)
174 return ret;
175
176 if (copy_to_user((void __user *)arg, &gup, sizeof(gup)))
177 return -EFAULT;
178
179 return 0;
180}
181
John Hubbard9c84f222020-12-14 19:05:05 -0800182static const struct file_operations gup_test_fops = {
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -0800183 .open = nonseekable_open,
John Hubbard9c84f222020-12-14 19:05:05 -0800184 .unlocked_ioctl = gup_test_ioctl,
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -0800185};
186
John Hubbard9c84f222020-12-14 19:05:05 -0800187static int gup_test_init(void)
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -0800188{
John Hubbard9c84f222020-12-14 19:05:05 -0800189 debugfs_create_file_unsafe("gup_test", 0600, NULL, NULL,
190 &gup_test_fops);
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -0800191
192 return 0;
193}
194
John Hubbard9c84f222020-12-14 19:05:05 -0800195late_initcall(gup_test_init);