blob: d974dec19e1caba90ce5fd87678b3f73e8d0082d [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,
John Hubbardf4f9bda2020-12-14 19:05:21 -080010 unsigned long nr_pages, unsigned int gup_test_flags)
John Hubbard41c45d32020-04-01 21:05:41 -070011{
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;
John Hubbardf4f9bda2020-12-14 19:05:21 -080026 case DUMP_USER_PAGES_TEST:
27 if (gup_test_flags & GUP_TEST_FLAG_DUMP_PAGES_USE_PIN) {
28 unpin_user_pages(pages, nr_pages);
29 } else {
30 for (i = 0; i < nr_pages; i++)
31 put_page(pages[i]);
32
33 }
34 break;
John Hubbard41c45d32020-04-01 21:05:41 -070035 }
36}
37
38static void verify_dma_pinned(unsigned int cmd, struct page **pages,
39 unsigned long nr_pages)
40{
41 unsigned long i;
42 struct page *page;
43
44 switch (cmd) {
45 case PIN_FAST_BENCHMARK:
John Hubbarda9bed1e2020-12-14 19:05:17 -080046 case PIN_BASIC_TEST:
Barry Song657d4f72020-10-13 16:51:54 -070047 case PIN_LONGTERM_BENCHMARK:
John Hubbard41c45d32020-04-01 21:05:41 -070048 for (i = 0; i < nr_pages; i++) {
49 page = pages[i];
50 if (WARN(!page_maybe_dma_pinned(page),
51 "pages[%lu] is NOT dma-pinned\n", i)) {
52
John Hubbard9c84f222020-12-14 19:05:05 -080053 dump_page(page, "gup_test failure");
John Hubbard41c45d32020-04-01 21:05:41 -070054 break;
Pavel Tatashine44605a2021-05-04 18:39:27 -070055 } else if (cmd == PIN_LONGTERM_BENCHMARK &&
56 WARN(!is_pinnable_page(page),
57 "pages[%lu] is NOT pinnable but pinned\n",
58 i)) {
59 dump_page(page, "gup_test failure");
60 break;
John Hubbard41c45d32020-04-01 21:05:41 -070061 }
62 }
63 break;
64 }
65}
66
John Hubbardf4f9bda2020-12-14 19:05:21 -080067static void dump_pages_test(struct gup_test *gup, struct page **pages,
68 unsigned long nr_pages)
69{
70 unsigned int index_to_dump;
71 unsigned int i;
72
73 /*
74 * Zero out any user-supplied page index that is out of range. Remember:
75 * .which_pages[] contains a 1-based set of page indices.
76 */
77 for (i = 0; i < GUP_TEST_MAX_PAGES_TO_DUMP; i++) {
78 if (gup->which_pages[i] > nr_pages) {
79 pr_warn("ZEROING due to out of range: .which_pages[%u]: %u\n",
80 i, gup->which_pages[i]);
81 gup->which_pages[i] = 0;
82 }
83 }
84
85 for (i = 0; i < GUP_TEST_MAX_PAGES_TO_DUMP; i++) {
86 index_to_dump = gup->which_pages[i];
87
88 if (index_to_dump) {
89 index_to_dump--; // Decode from 1-based, to 0-based
90 pr_info("---- page #%u, starting from user virt addr: 0x%llx\n",
91 index_to_dump, gup->addr);
92 dump_page(pages[index_to_dump],
93 "gup_test: dump_pages() test");
94 }
95 }
96}
97
John Hubbard9c84f222020-12-14 19:05:05 -080098static int __gup_test_ioctl(unsigned int cmd,
99 struct gup_test *gup)
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -0800100{
101 ktime_t start_time, end_time;
YueHaibing51896862018-10-05 15:51:44 -0700102 unsigned long i, nr_pages, addr, next;
Pavel Tatashin79dbf132021-05-04 18:39:23 -0700103 long nr;
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -0800104 struct page **pages;
Navid Emamdoosta7c46c02020-01-04 13:00:12 -0800105 int ret = 0;
Jann Hornf3964592020-10-17 16:14:12 -0700106 bool needs_mmap_lock =
107 cmd != GUP_FAST_BENCHMARK && cmd != PIN_FAST_BENCHMARK;
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -0800108
Dan Carpenter4b408c72018-10-30 15:04:32 -0700109 if (gup->size > ULONG_MAX)
110 return -EINVAL;
111
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -0800112 nr_pages = gup->size / PAGE_SIZE;
Kees Cook778e1cd2018-06-12 14:04:48 -0700113 pages = kvcalloc(nr_pages, sizeof(void *), GFP_KERNEL);
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -0800114 if (!pages)
115 return -ENOMEM;
116
Jann Hornf3964592020-10-17 16:14:12 -0700117 if (needs_mmap_lock && mmap_read_lock_killable(current->mm)) {
118 ret = -EINTR;
119 goto free_pages;
120 }
121
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -0800122 i = 0;
123 nr = gup->nr_pages_per_call;
124 start_time = ktime_get();
125 for (addr = gup->addr; addr < gup->addr + gup->size; addr = next) {
126 if (nr != gup->nr_pages_per_call)
127 break;
128
129 next = addr + nr * PAGE_SIZE;
130 if (next > gup->addr + gup->size) {
131 next = gup->addr + gup->size;
132 nr = (next - addr) / PAGE_SIZE;
133 }
134
Keith Busch714a3a12018-10-26 15:09:56 -0700135 switch (cmd) {
136 case GUP_FAST_BENCHMARK:
Pavel Tatashin79dbf132021-05-04 18:39:23 -0700137 nr = get_user_pages_fast(addr, nr, gup->gup_flags,
Keith Busch714a3a12018-10-26 15:09:56 -0700138 pages + i);
139 break;
John Hubbarda9bed1e2020-12-14 19:05:17 -0800140 case GUP_BASIC_TEST:
Pavel Tatashin79dbf132021-05-04 18:39:23 -0700141 nr = get_user_pages(addr, nr, gup->gup_flags, pages + i,
Keith Busch714a3a12018-10-26 15:09:56 -0700142 NULL);
143 break;
John Hubbard41c45d32020-04-01 21:05:41 -0700144 case PIN_FAST_BENCHMARK:
Pavel Tatashin79dbf132021-05-04 18:39:23 -0700145 nr = pin_user_pages_fast(addr, nr, gup->gup_flags,
John Hubbard41c45d32020-04-01 21:05:41 -0700146 pages + i);
147 break;
John Hubbarda9bed1e2020-12-14 19:05:17 -0800148 case PIN_BASIC_TEST:
Pavel Tatashin79dbf132021-05-04 18:39:23 -0700149 nr = pin_user_pages(addr, nr, gup->gup_flags, pages + i,
John Hubbard41c45d32020-04-01 21:05:41 -0700150 NULL);
151 break;
Barry Song657d4f72020-10-13 16:51:54 -0700152 case PIN_LONGTERM_BENCHMARK:
153 nr = pin_user_pages(addr, nr,
Pavel Tatashin79dbf132021-05-04 18:39:23 -0700154 gup->gup_flags | FOLL_LONGTERM,
Barry Song657d4f72020-10-13 16:51:54 -0700155 pages + i, NULL);
156 break;
John Hubbardf4f9bda2020-12-14 19:05:21 -0800157 case DUMP_USER_PAGES_TEST:
Pavel Tatashin79dbf132021-05-04 18:39:23 -0700158 if (gup->test_flags & GUP_TEST_FLAG_DUMP_PAGES_USE_PIN)
159 nr = pin_user_pages(addr, nr, gup->gup_flags,
John Hubbardf4f9bda2020-12-14 19:05:21 -0800160 pages + i, NULL);
161 else
Pavel Tatashin79dbf132021-05-04 18:39:23 -0700162 nr = get_user_pages(addr, nr, gup->gup_flags,
John Hubbardf4f9bda2020-12-14 19:05:21 -0800163 pages + i, NULL);
164 break;
Keith Busch714a3a12018-10-26 15:09:56 -0700165 default:
Navid Emamdoosta7c46c02020-01-04 13:00:12 -0800166 ret = -EINVAL;
Jann Hornf3964592020-10-17 16:14:12 -0700167 goto unlock;
Keith Busch714a3a12018-10-26 15:09:56 -0700168 }
169
Michael S. Tsirkin09e35a42018-04-13 15:35:16 -0700170 if (nr <= 0)
171 break;
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -0800172 i += nr;
173 }
174 end_time = ktime_get();
175
John Hubbard41c45d32020-04-01 21:05:41 -0700176 /* Shifting the meaning of nr_pages: now it is actual number pinned: */
177 nr_pages = i;
178
Keith Busch26db3d02018-10-26 15:09:52 -0700179 gup->get_delta_usec = ktime_us_delta(end_time, start_time);
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -0800180 gup->size = addr - gup->addr;
181
John Hubbard41c45d32020-04-01 21:05:41 -0700182 /*
183 * Take an un-benchmark-timed moment to verify DMA pinned
184 * state: print a warning if any non-dma-pinned pages are found:
185 */
186 verify_dma_pinned(cmd, pages, nr_pages);
187
John Hubbardf4f9bda2020-12-14 19:05:21 -0800188 if (cmd == DUMP_USER_PAGES_TEST)
189 dump_pages_test(gup, pages, nr_pages);
190
Keith Busch26db3d02018-10-26 15:09:52 -0700191 start_time = ktime_get();
John Hubbard41c45d32020-04-01 21:05:41 -0700192
Pavel Tatashin79dbf132021-05-04 18:39:23 -0700193 put_back_pages(cmd, pages, nr_pages, gup->test_flags);
John Hubbard41c45d32020-04-01 21:05:41 -0700194
Keith Busch26db3d02018-10-26 15:09:52 -0700195 end_time = ktime_get();
196 gup->put_delta_usec = ktime_us_delta(end_time, start_time);
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -0800197
Jann Hornf3964592020-10-17 16:14:12 -0700198unlock:
199 if (needs_mmap_lock)
200 mmap_read_unlock(current->mm);
201free_pages:
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -0800202 kvfree(pages);
Navid Emamdoosta7c46c02020-01-04 13:00:12 -0800203 return ret;
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -0800204}
205
John Hubbard9c84f222020-12-14 19:05:05 -0800206static long gup_test_ioctl(struct file *filep, unsigned int cmd,
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -0800207 unsigned long arg)
208{
John Hubbard9c84f222020-12-14 19:05:05 -0800209 struct gup_test gup;
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -0800210 int ret;
211
Keith Busch714a3a12018-10-26 15:09:56 -0700212 switch (cmd) {
213 case GUP_FAST_BENCHMARK:
John Hubbard41c45d32020-04-01 21:05:41 -0700214 case PIN_FAST_BENCHMARK:
Barry Song657d4f72020-10-13 16:51:54 -0700215 case PIN_LONGTERM_BENCHMARK:
John Hubbarda9bed1e2020-12-14 19:05:17 -0800216 case GUP_BASIC_TEST:
217 case PIN_BASIC_TEST:
John Hubbardf4f9bda2020-12-14 19:05:21 -0800218 case DUMP_USER_PAGES_TEST:
Keith Busch714a3a12018-10-26 15:09:56 -0700219 break;
220 default:
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -0800221 return -EINVAL;
Keith Busch714a3a12018-10-26 15:09:56 -0700222 }
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -0800223
224 if (copy_from_user(&gup, (void __user *)arg, sizeof(gup)))
225 return -EFAULT;
226
John Hubbard9c84f222020-12-14 19:05:05 -0800227 ret = __gup_test_ioctl(cmd, &gup);
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -0800228 if (ret)
229 return ret;
230
231 if (copy_to_user((void __user *)arg, &gup, sizeof(gup)))
232 return -EFAULT;
233
234 return 0;
235}
236
John Hubbard9c84f222020-12-14 19:05:05 -0800237static const struct file_operations gup_test_fops = {
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -0800238 .open = nonseekable_open,
John Hubbard9c84f222020-12-14 19:05:05 -0800239 .unlocked_ioctl = gup_test_ioctl,
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -0800240};
241
Barry Songafaa7882020-12-14 19:05:34 -0800242static int __init gup_test_init(void)
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -0800243{
John Hubbard9c84f222020-12-14 19:05:05 -0800244 debugfs_create_file_unsafe("gup_test", 0600, NULL, NULL,
245 &gup_test_fops);
Kirill A. Shutemov64c349f2017-11-17 15:31:22 -0800246
247 return 0;
248}
249
John Hubbard9c84f222020-12-14 19:05:05 -0800250late_initcall(gup_test_init);