Kirill A. Shutemov | 64c349f4 | 2017-11-17 15:31:22 -0800 | [diff] [blame] | 1 | #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> |
David Hildenbrand | a0ac9b35 | 2022-12-05 20:37:13 +0100 | [diff] [blame] | 7 | #include <linux/highmem.h> |
John Hubbard | b9dcfdf | 2020-12-14 19:05:08 -0800 | [diff] [blame] | 8 | #include "gup_test.h" |
Kirill A. Shutemov | 64c349f4 | 2017-11-17 15:31:22 -0800 | [diff] [blame] | 9 | |
John Hubbard | 41c45d3 | 2020-04-01 21:05:41 -0700 | [diff] [blame] | 10 | static void put_back_pages(unsigned int cmd, struct page **pages, |
John Hubbard | f4f9bda | 2020-12-14 19:05:21 -0800 | [diff] [blame] | 11 | unsigned long nr_pages, unsigned int gup_test_flags) |
John Hubbard | 41c45d3 | 2020-04-01 21:05:41 -0700 | [diff] [blame] | 12 | { |
| 13 | unsigned long i; |
| 14 | |
| 15 | switch (cmd) { |
| 16 | case GUP_FAST_BENCHMARK: |
John Hubbard | a9bed1e | 2020-12-14 19:05:17 -0800 | [diff] [blame] | 17 | case GUP_BASIC_TEST: |
John Hubbard | 41c45d3 | 2020-04-01 21:05:41 -0700 | [diff] [blame] | 18 | for (i = 0; i < nr_pages; i++) |
| 19 | put_page(pages[i]); |
| 20 | break; |
| 21 | |
| 22 | case PIN_FAST_BENCHMARK: |
John Hubbard | a9bed1e | 2020-12-14 19:05:17 -0800 | [diff] [blame] | 23 | case PIN_BASIC_TEST: |
Barry Song | 657d4f7 | 2020-10-13 16:51:54 -0700 | [diff] [blame] | 24 | case PIN_LONGTERM_BENCHMARK: |
John Hubbard | 41c45d3 | 2020-04-01 21:05:41 -0700 | [diff] [blame] | 25 | unpin_user_pages(pages, nr_pages); |
| 26 | break; |
John Hubbard | f4f9bda | 2020-12-14 19:05:21 -0800 | [diff] [blame] | 27 | case DUMP_USER_PAGES_TEST: |
| 28 | if (gup_test_flags & GUP_TEST_FLAG_DUMP_PAGES_USE_PIN) { |
| 29 | unpin_user_pages(pages, nr_pages); |
| 30 | } else { |
| 31 | for (i = 0; i < nr_pages; i++) |
| 32 | put_page(pages[i]); |
| 33 | |
| 34 | } |
| 35 | break; |
John Hubbard | 41c45d3 | 2020-04-01 21:05:41 -0700 | [diff] [blame] | 36 | } |
| 37 | } |
| 38 | |
| 39 | static void verify_dma_pinned(unsigned int cmd, struct page **pages, |
| 40 | unsigned long nr_pages) |
| 41 | { |
| 42 | unsigned long i; |
Vishal Moola (Oracle) | c9223a4 | 2023-06-13 19:13:10 -0700 | [diff] [blame] | 43 | struct folio *folio; |
John Hubbard | 41c45d3 | 2020-04-01 21:05:41 -0700 | [diff] [blame] | 44 | |
| 45 | switch (cmd) { |
| 46 | case PIN_FAST_BENCHMARK: |
John Hubbard | a9bed1e | 2020-12-14 19:05:17 -0800 | [diff] [blame] | 47 | case PIN_BASIC_TEST: |
Barry Song | 657d4f7 | 2020-10-13 16:51:54 -0700 | [diff] [blame] | 48 | case PIN_LONGTERM_BENCHMARK: |
John Hubbard | 41c45d3 | 2020-04-01 21:05:41 -0700 | [diff] [blame] | 49 | for (i = 0; i < nr_pages; i++) { |
Vishal Moola (Oracle) | c9223a4 | 2023-06-13 19:13:10 -0700 | [diff] [blame] | 50 | folio = page_folio(pages[i]); |
| 51 | |
| 52 | if (WARN(!folio_maybe_dma_pinned(folio), |
John Hubbard | 41c45d3 | 2020-04-01 21:05:41 -0700 | [diff] [blame] | 53 | "pages[%lu] is NOT dma-pinned\n", i)) { |
| 54 | |
Vishal Moola (Oracle) | c9223a4 | 2023-06-13 19:13:10 -0700 | [diff] [blame] | 55 | dump_page(&folio->page, "gup_test failure"); |
John Hubbard | 41c45d3 | 2020-04-01 21:05:41 -0700 | [diff] [blame] | 56 | break; |
Pavel Tatashin | e44605a | 2021-05-04 18:39:27 -0700 | [diff] [blame] | 57 | } else if (cmd == PIN_LONGTERM_BENCHMARK && |
Vishal Moola (Oracle) | c9223a4 | 2023-06-13 19:13:10 -0700 | [diff] [blame] | 58 | WARN(!folio_is_longterm_pinnable(folio), |
Pavel Tatashin | e44605a | 2021-05-04 18:39:27 -0700 | [diff] [blame] | 59 | "pages[%lu] is NOT pinnable but pinned\n", |
| 60 | i)) { |
Vishal Moola (Oracle) | c9223a4 | 2023-06-13 19:13:10 -0700 | [diff] [blame] | 61 | dump_page(&folio->page, "gup_test failure"); |
Pavel Tatashin | e44605a | 2021-05-04 18:39:27 -0700 | [diff] [blame] | 62 | break; |
John Hubbard | 41c45d3 | 2020-04-01 21:05:41 -0700 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | break; |
| 66 | } |
| 67 | } |
| 68 | |
John Hubbard | f4f9bda | 2020-12-14 19:05:21 -0800 | [diff] [blame] | 69 | static void dump_pages_test(struct gup_test *gup, struct page **pages, |
| 70 | unsigned long nr_pages) |
| 71 | { |
| 72 | unsigned int index_to_dump; |
| 73 | unsigned int i; |
| 74 | |
| 75 | /* |
| 76 | * Zero out any user-supplied page index that is out of range. Remember: |
| 77 | * .which_pages[] contains a 1-based set of page indices. |
| 78 | */ |
| 79 | for (i = 0; i < GUP_TEST_MAX_PAGES_TO_DUMP; i++) { |
| 80 | if (gup->which_pages[i] > nr_pages) { |
| 81 | pr_warn("ZEROING due to out of range: .which_pages[%u]: %u\n", |
| 82 | i, gup->which_pages[i]); |
| 83 | gup->which_pages[i] = 0; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | for (i = 0; i < GUP_TEST_MAX_PAGES_TO_DUMP; i++) { |
| 88 | index_to_dump = gup->which_pages[i]; |
| 89 | |
| 90 | if (index_to_dump) { |
| 91 | index_to_dump--; // Decode from 1-based, to 0-based |
| 92 | pr_info("---- page #%u, starting from user virt addr: 0x%llx\n", |
| 93 | index_to_dump, gup->addr); |
| 94 | dump_page(pages[index_to_dump], |
| 95 | "gup_test: dump_pages() test"); |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
John Hubbard | 9c84f22 | 2020-12-14 19:05:05 -0800 | [diff] [blame] | 100 | static int __gup_test_ioctl(unsigned int cmd, |
| 101 | struct gup_test *gup) |
Kirill A. Shutemov | 64c349f4 | 2017-11-17 15:31:22 -0800 | [diff] [blame] | 102 | { |
| 103 | ktime_t start_time, end_time; |
YueHaibing | 5189686 | 2018-10-05 15:51:44 -0700 | [diff] [blame] | 104 | unsigned long i, nr_pages, addr, next; |
Pavel Tatashin | 79dbf13 | 2021-05-04 18:39:23 -0700 | [diff] [blame] | 105 | long nr; |
Kirill A. Shutemov | 64c349f4 | 2017-11-17 15:31:22 -0800 | [diff] [blame] | 106 | struct page **pages; |
Navid Emamdoost | a7c46c0 | 2020-01-04 13:00:12 -0800 | [diff] [blame] | 107 | int ret = 0; |
Jann Horn | f396459 | 2020-10-17 16:14:12 -0700 | [diff] [blame] | 108 | bool needs_mmap_lock = |
| 109 | cmd != GUP_FAST_BENCHMARK && cmd != PIN_FAST_BENCHMARK; |
Kirill A. Shutemov | 64c349f4 | 2017-11-17 15:31:22 -0800 | [diff] [blame] | 110 | |
Dan Carpenter | 4b408c7 | 2018-10-30 15:04:32 -0700 | [diff] [blame] | 111 | if (gup->size > ULONG_MAX) |
| 112 | return -EINVAL; |
| 113 | |
Kirill A. Shutemov | 64c349f4 | 2017-11-17 15:31:22 -0800 | [diff] [blame] | 114 | nr_pages = gup->size / PAGE_SIZE; |
Kees Cook | 778e1cd | 2018-06-12 14:04:48 -0700 | [diff] [blame] | 115 | pages = kvcalloc(nr_pages, sizeof(void *), GFP_KERNEL); |
Kirill A. Shutemov | 64c349f4 | 2017-11-17 15:31:22 -0800 | [diff] [blame] | 116 | if (!pages) |
| 117 | return -ENOMEM; |
| 118 | |
Jann Horn | f396459 | 2020-10-17 16:14:12 -0700 | [diff] [blame] | 119 | if (needs_mmap_lock && mmap_read_lock_killable(current->mm)) { |
| 120 | ret = -EINTR; |
| 121 | goto free_pages; |
| 122 | } |
| 123 | |
Kirill A. Shutemov | 64c349f4 | 2017-11-17 15:31:22 -0800 | [diff] [blame] | 124 | i = 0; |
| 125 | nr = gup->nr_pages_per_call; |
| 126 | start_time = ktime_get(); |
| 127 | for (addr = gup->addr; addr < gup->addr + gup->size; addr = next) { |
| 128 | if (nr != gup->nr_pages_per_call) |
| 129 | break; |
| 130 | |
| 131 | next = addr + nr * PAGE_SIZE; |
| 132 | if (next > gup->addr + gup->size) { |
| 133 | next = gup->addr + gup->size; |
| 134 | nr = (next - addr) / PAGE_SIZE; |
| 135 | } |
| 136 | |
Keith Busch | 714a3a1 | 2018-10-26 15:09:56 -0700 | [diff] [blame] | 137 | switch (cmd) { |
| 138 | case GUP_FAST_BENCHMARK: |
Pavel Tatashin | 79dbf13 | 2021-05-04 18:39:23 -0700 | [diff] [blame] | 139 | nr = get_user_pages_fast(addr, nr, gup->gup_flags, |
Keith Busch | 714a3a1 | 2018-10-26 15:09:56 -0700 | [diff] [blame] | 140 | pages + i); |
| 141 | break; |
John Hubbard | a9bed1e | 2020-12-14 19:05:17 -0800 | [diff] [blame] | 142 | case GUP_BASIC_TEST: |
Lorenzo Stoakes | 54d0206 | 2023-05-17 20:25:33 +0100 | [diff] [blame] | 143 | nr = get_user_pages(addr, nr, gup->gup_flags, pages + i); |
Keith Busch | 714a3a1 | 2018-10-26 15:09:56 -0700 | [diff] [blame] | 144 | break; |
John Hubbard | 41c45d3 | 2020-04-01 21:05:41 -0700 | [diff] [blame] | 145 | case PIN_FAST_BENCHMARK: |
Pavel Tatashin | 79dbf13 | 2021-05-04 18:39:23 -0700 | [diff] [blame] | 146 | nr = pin_user_pages_fast(addr, nr, gup->gup_flags, |
John Hubbard | 41c45d3 | 2020-04-01 21:05:41 -0700 | [diff] [blame] | 147 | pages + i); |
| 148 | break; |
John Hubbard | a9bed1e | 2020-12-14 19:05:17 -0800 | [diff] [blame] | 149 | case PIN_BASIC_TEST: |
Lorenzo Stoakes | 4c630f3 | 2023-05-17 20:25:45 +0100 | [diff] [blame] | 150 | nr = pin_user_pages(addr, nr, gup->gup_flags, pages + i); |
John Hubbard | 41c45d3 | 2020-04-01 21:05:41 -0700 | [diff] [blame] | 151 | break; |
Barry Song | 657d4f7 | 2020-10-13 16:51:54 -0700 | [diff] [blame] | 152 | case PIN_LONGTERM_BENCHMARK: |
| 153 | nr = pin_user_pages(addr, nr, |
Pavel Tatashin | 79dbf13 | 2021-05-04 18:39:23 -0700 | [diff] [blame] | 154 | gup->gup_flags | FOLL_LONGTERM, |
Lorenzo Stoakes | 4c630f3 | 2023-05-17 20:25:45 +0100 | [diff] [blame] | 155 | pages + i); |
Barry Song | 657d4f7 | 2020-10-13 16:51:54 -0700 | [diff] [blame] | 156 | break; |
John Hubbard | f4f9bda | 2020-12-14 19:05:21 -0800 | [diff] [blame] | 157 | case DUMP_USER_PAGES_TEST: |
Pavel Tatashin | 79dbf13 | 2021-05-04 18:39:23 -0700 | [diff] [blame] | 158 | if (gup->test_flags & GUP_TEST_FLAG_DUMP_PAGES_USE_PIN) |
| 159 | nr = pin_user_pages(addr, nr, gup->gup_flags, |
Lorenzo Stoakes | 4c630f3 | 2023-05-17 20:25:45 +0100 | [diff] [blame] | 160 | pages + i); |
John Hubbard | f4f9bda | 2020-12-14 19:05:21 -0800 | [diff] [blame] | 161 | else |
Pavel Tatashin | 79dbf13 | 2021-05-04 18:39:23 -0700 | [diff] [blame] | 162 | nr = get_user_pages(addr, nr, gup->gup_flags, |
Lorenzo Stoakes | 54d0206 | 2023-05-17 20:25:33 +0100 | [diff] [blame] | 163 | pages + i); |
John Hubbard | f4f9bda | 2020-12-14 19:05:21 -0800 | [diff] [blame] | 164 | break; |
Keith Busch | 714a3a1 | 2018-10-26 15:09:56 -0700 | [diff] [blame] | 165 | default: |
Navid Emamdoost | a7c46c0 | 2020-01-04 13:00:12 -0800 | [diff] [blame] | 166 | ret = -EINVAL; |
Jann Horn | f396459 | 2020-10-17 16:14:12 -0700 | [diff] [blame] | 167 | goto unlock; |
Keith Busch | 714a3a1 | 2018-10-26 15:09:56 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Michael S. Tsirkin | 09e35a4 | 2018-04-13 15:35:16 -0700 | [diff] [blame] | 170 | if (nr <= 0) |
| 171 | break; |
Kirill A. Shutemov | 64c349f4 | 2017-11-17 15:31:22 -0800 | [diff] [blame] | 172 | i += nr; |
| 173 | } |
| 174 | end_time = ktime_get(); |
| 175 | |
John Hubbard | 41c45d3 | 2020-04-01 21:05:41 -0700 | [diff] [blame] | 176 | /* Shifting the meaning of nr_pages: now it is actual number pinned: */ |
| 177 | nr_pages = i; |
| 178 | |
Keith Busch | 26db3d0 | 2018-10-26 15:09:52 -0700 | [diff] [blame] | 179 | gup->get_delta_usec = ktime_us_delta(end_time, start_time); |
Kirill A. Shutemov | 64c349f4 | 2017-11-17 15:31:22 -0800 | [diff] [blame] | 180 | gup->size = addr - gup->addr; |
| 181 | |
John Hubbard | 41c45d3 | 2020-04-01 21:05:41 -0700 | [diff] [blame] | 182 | /* |
| 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 Hubbard | f4f9bda | 2020-12-14 19:05:21 -0800 | [diff] [blame] | 188 | if (cmd == DUMP_USER_PAGES_TEST) |
| 189 | dump_pages_test(gup, pages, nr_pages); |
| 190 | |
Keith Busch | 26db3d0 | 2018-10-26 15:09:52 -0700 | [diff] [blame] | 191 | start_time = ktime_get(); |
John Hubbard | 41c45d3 | 2020-04-01 21:05:41 -0700 | [diff] [blame] | 192 | |
Pavel Tatashin | 79dbf13 | 2021-05-04 18:39:23 -0700 | [diff] [blame] | 193 | put_back_pages(cmd, pages, nr_pages, gup->test_flags); |
John Hubbard | 41c45d3 | 2020-04-01 21:05:41 -0700 | [diff] [blame] | 194 | |
Keith Busch | 26db3d0 | 2018-10-26 15:09:52 -0700 | [diff] [blame] | 195 | end_time = ktime_get(); |
| 196 | gup->put_delta_usec = ktime_us_delta(end_time, start_time); |
Kirill A. Shutemov | 64c349f4 | 2017-11-17 15:31:22 -0800 | [diff] [blame] | 197 | |
Jann Horn | f396459 | 2020-10-17 16:14:12 -0700 | [diff] [blame] | 198 | unlock: |
| 199 | if (needs_mmap_lock) |
| 200 | mmap_read_unlock(current->mm); |
| 201 | free_pages: |
Kirill A. Shutemov | 64c349f4 | 2017-11-17 15:31:22 -0800 | [diff] [blame] | 202 | kvfree(pages); |
Navid Emamdoost | a7c46c0 | 2020-01-04 13:00:12 -0800 | [diff] [blame] | 203 | return ret; |
Kirill A. Shutemov | 64c349f4 | 2017-11-17 15:31:22 -0800 | [diff] [blame] | 204 | } |
| 205 | |
David Hildenbrand | c77369b | 2022-09-27 13:01:19 +0200 | [diff] [blame] | 206 | static DEFINE_MUTEX(pin_longterm_test_mutex); |
| 207 | static struct page **pin_longterm_test_pages; |
| 208 | static unsigned long pin_longterm_test_nr_pages; |
| 209 | |
| 210 | static inline void pin_longterm_test_stop(void) |
| 211 | { |
| 212 | if (pin_longterm_test_pages) { |
| 213 | if (pin_longterm_test_nr_pages) |
| 214 | unpin_user_pages(pin_longterm_test_pages, |
| 215 | pin_longterm_test_nr_pages); |
David Hildenbrand | 61b963b | 2022-12-12 19:20:18 +0100 | [diff] [blame] | 216 | kvfree(pin_longterm_test_pages); |
David Hildenbrand | c77369b | 2022-09-27 13:01:19 +0200 | [diff] [blame] | 217 | pin_longterm_test_pages = NULL; |
| 218 | pin_longterm_test_nr_pages = 0; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | static inline int pin_longterm_test_start(unsigned long arg) |
| 223 | { |
| 224 | long nr_pages, cur_pages, addr, remaining_pages; |
| 225 | int gup_flags = FOLL_LONGTERM; |
| 226 | struct pin_longterm_test args; |
| 227 | struct page **pages; |
| 228 | int ret = 0; |
| 229 | bool fast; |
| 230 | |
| 231 | if (pin_longterm_test_pages) |
| 232 | return -EINVAL; |
| 233 | |
| 234 | if (copy_from_user(&args, (void __user *)arg, sizeof(args))) |
| 235 | return -EFAULT; |
| 236 | |
| 237 | if (args.flags & |
| 238 | ~(PIN_LONGTERM_TEST_FLAG_USE_WRITE|PIN_LONGTERM_TEST_FLAG_USE_FAST)) |
| 239 | return -EINVAL; |
| 240 | if (!IS_ALIGNED(args.addr | args.size, PAGE_SIZE)) |
| 241 | return -EINVAL; |
| 242 | if (args.size > LONG_MAX) |
| 243 | return -EINVAL; |
| 244 | nr_pages = args.size / PAGE_SIZE; |
| 245 | if (!nr_pages) |
| 246 | return -EINVAL; |
| 247 | |
| 248 | pages = kvcalloc(nr_pages, sizeof(void *), GFP_KERNEL); |
| 249 | if (!pages) |
| 250 | return -ENOMEM; |
| 251 | |
| 252 | if (args.flags & PIN_LONGTERM_TEST_FLAG_USE_WRITE) |
| 253 | gup_flags |= FOLL_WRITE; |
| 254 | fast = !!(args.flags & PIN_LONGTERM_TEST_FLAG_USE_FAST); |
| 255 | |
| 256 | if (!fast && mmap_read_lock_killable(current->mm)) { |
David Hildenbrand | 61b963b | 2022-12-12 19:20:18 +0100 | [diff] [blame] | 257 | kvfree(pages); |
David Hildenbrand | c77369b | 2022-09-27 13:01:19 +0200 | [diff] [blame] | 258 | return -EINTR; |
| 259 | } |
| 260 | |
| 261 | pin_longterm_test_pages = pages; |
| 262 | pin_longterm_test_nr_pages = 0; |
| 263 | |
| 264 | while (nr_pages - pin_longterm_test_nr_pages) { |
| 265 | remaining_pages = nr_pages - pin_longterm_test_nr_pages; |
| 266 | addr = args.addr + pin_longterm_test_nr_pages * PAGE_SIZE; |
| 267 | |
| 268 | if (fast) |
| 269 | cur_pages = pin_user_pages_fast(addr, remaining_pages, |
| 270 | gup_flags, pages); |
| 271 | else |
| 272 | cur_pages = pin_user_pages(addr, remaining_pages, |
Lorenzo Stoakes | 4c630f3 | 2023-05-17 20:25:45 +0100 | [diff] [blame] | 273 | gup_flags, pages); |
David Hildenbrand | c77369b | 2022-09-27 13:01:19 +0200 | [diff] [blame] | 274 | if (cur_pages < 0) { |
| 275 | pin_longterm_test_stop(); |
| 276 | ret = cur_pages; |
| 277 | break; |
| 278 | } |
| 279 | pin_longterm_test_nr_pages += cur_pages; |
| 280 | pages += cur_pages; |
| 281 | } |
| 282 | |
| 283 | if (!fast) |
| 284 | mmap_read_unlock(current->mm); |
| 285 | return ret; |
| 286 | } |
| 287 | |
| 288 | static inline int pin_longterm_test_read(unsigned long arg) |
| 289 | { |
| 290 | __u64 user_addr; |
| 291 | unsigned long i; |
| 292 | |
| 293 | if (!pin_longterm_test_pages) |
| 294 | return -EINVAL; |
| 295 | |
| 296 | if (copy_from_user(&user_addr, (void __user *)arg, sizeof(user_addr))) |
| 297 | return -EFAULT; |
| 298 | |
| 299 | for (i = 0; i < pin_longterm_test_nr_pages; i++) { |
David Hildenbrand | a0ac9b35 | 2022-12-05 20:37:13 +0100 | [diff] [blame] | 300 | void *addr = kmap_local_page(pin_longterm_test_pages[i]); |
| 301 | unsigned long ret; |
David Hildenbrand | c77369b | 2022-09-27 13:01:19 +0200 | [diff] [blame] | 302 | |
David Hildenbrand | a0ac9b35 | 2022-12-05 20:37:13 +0100 | [diff] [blame] | 303 | ret = copy_to_user((void __user *)(unsigned long)user_addr, addr, |
| 304 | PAGE_SIZE); |
| 305 | kunmap_local(addr); |
| 306 | if (ret) |
David Hildenbrand | c77369b | 2022-09-27 13:01:19 +0200 | [diff] [blame] | 307 | return -EFAULT; |
| 308 | user_addr += PAGE_SIZE; |
| 309 | } |
| 310 | return 0; |
| 311 | } |
| 312 | |
| 313 | static long pin_longterm_test_ioctl(struct file *filep, unsigned int cmd, |
| 314 | unsigned long arg) |
| 315 | { |
| 316 | int ret = -EINVAL; |
| 317 | |
| 318 | if (mutex_lock_killable(&pin_longterm_test_mutex)) |
| 319 | return -EINTR; |
| 320 | |
| 321 | switch (cmd) { |
| 322 | case PIN_LONGTERM_TEST_START: |
| 323 | ret = pin_longterm_test_start(arg); |
| 324 | break; |
| 325 | case PIN_LONGTERM_TEST_STOP: |
| 326 | pin_longterm_test_stop(); |
| 327 | ret = 0; |
| 328 | break; |
| 329 | case PIN_LONGTERM_TEST_READ: |
| 330 | ret = pin_longterm_test_read(arg); |
| 331 | break; |
| 332 | } |
| 333 | |
| 334 | mutex_unlock(&pin_longterm_test_mutex); |
| 335 | return ret; |
| 336 | } |
| 337 | |
John Hubbard | 9c84f22 | 2020-12-14 19:05:05 -0800 | [diff] [blame] | 338 | static long gup_test_ioctl(struct file *filep, unsigned int cmd, |
Kirill A. Shutemov | 64c349f4 | 2017-11-17 15:31:22 -0800 | [diff] [blame] | 339 | unsigned long arg) |
| 340 | { |
John Hubbard | 9c84f22 | 2020-12-14 19:05:05 -0800 | [diff] [blame] | 341 | struct gup_test gup; |
Kirill A. Shutemov | 64c349f4 | 2017-11-17 15:31:22 -0800 | [diff] [blame] | 342 | int ret; |
| 343 | |
Keith Busch | 714a3a1 | 2018-10-26 15:09:56 -0700 | [diff] [blame] | 344 | switch (cmd) { |
| 345 | case GUP_FAST_BENCHMARK: |
John Hubbard | 41c45d3 | 2020-04-01 21:05:41 -0700 | [diff] [blame] | 346 | case PIN_FAST_BENCHMARK: |
Barry Song | 657d4f7 | 2020-10-13 16:51:54 -0700 | [diff] [blame] | 347 | case PIN_LONGTERM_BENCHMARK: |
John Hubbard | a9bed1e | 2020-12-14 19:05:17 -0800 | [diff] [blame] | 348 | case GUP_BASIC_TEST: |
| 349 | case PIN_BASIC_TEST: |
John Hubbard | f4f9bda | 2020-12-14 19:05:21 -0800 | [diff] [blame] | 350 | case DUMP_USER_PAGES_TEST: |
Keith Busch | 714a3a1 | 2018-10-26 15:09:56 -0700 | [diff] [blame] | 351 | break; |
David Hildenbrand | c77369b | 2022-09-27 13:01:19 +0200 | [diff] [blame] | 352 | case PIN_LONGTERM_TEST_START: |
| 353 | case PIN_LONGTERM_TEST_STOP: |
| 354 | case PIN_LONGTERM_TEST_READ: |
| 355 | return pin_longterm_test_ioctl(filep, cmd, arg); |
Keith Busch | 714a3a1 | 2018-10-26 15:09:56 -0700 | [diff] [blame] | 356 | default: |
Kirill A. Shutemov | 64c349f4 | 2017-11-17 15:31:22 -0800 | [diff] [blame] | 357 | return -EINVAL; |
Keith Busch | 714a3a1 | 2018-10-26 15:09:56 -0700 | [diff] [blame] | 358 | } |
Kirill A. Shutemov | 64c349f4 | 2017-11-17 15:31:22 -0800 | [diff] [blame] | 359 | |
| 360 | if (copy_from_user(&gup, (void __user *)arg, sizeof(gup))) |
| 361 | return -EFAULT; |
| 362 | |
John Hubbard | 9c84f22 | 2020-12-14 19:05:05 -0800 | [diff] [blame] | 363 | ret = __gup_test_ioctl(cmd, &gup); |
Kirill A. Shutemov | 64c349f4 | 2017-11-17 15:31:22 -0800 | [diff] [blame] | 364 | if (ret) |
| 365 | return ret; |
| 366 | |
| 367 | if (copy_to_user((void __user *)arg, &gup, sizeof(gup))) |
| 368 | return -EFAULT; |
| 369 | |
| 370 | return 0; |
| 371 | } |
| 372 | |
David Hildenbrand | c77369b | 2022-09-27 13:01:19 +0200 | [diff] [blame] | 373 | static int gup_test_release(struct inode *inode, struct file *file) |
| 374 | { |
| 375 | pin_longterm_test_stop(); |
| 376 | |
| 377 | return 0; |
| 378 | } |
| 379 | |
John Hubbard | 9c84f22 | 2020-12-14 19:05:05 -0800 | [diff] [blame] | 380 | static const struct file_operations gup_test_fops = { |
Kirill A. Shutemov | 64c349f4 | 2017-11-17 15:31:22 -0800 | [diff] [blame] | 381 | .open = nonseekable_open, |
John Hubbard | 9c84f22 | 2020-12-14 19:05:05 -0800 | [diff] [blame] | 382 | .unlocked_ioctl = gup_test_ioctl, |
Haibo Li | 4f572f0 | 2023-05-26 10:21:25 +0800 | [diff] [blame] | 383 | .compat_ioctl = compat_ptr_ioctl, |
David Hildenbrand | c77369b | 2022-09-27 13:01:19 +0200 | [diff] [blame] | 384 | .release = gup_test_release, |
Kirill A. Shutemov | 64c349f4 | 2017-11-17 15:31:22 -0800 | [diff] [blame] | 385 | }; |
| 386 | |
Barry Song | afaa788 | 2020-12-14 19:05:34 -0800 | [diff] [blame] | 387 | static int __init gup_test_init(void) |
Kirill A. Shutemov | 64c349f4 | 2017-11-17 15:31:22 -0800 | [diff] [blame] | 388 | { |
John Hubbard | 9c84f22 | 2020-12-14 19:05:05 -0800 | [diff] [blame] | 389 | debugfs_create_file_unsafe("gup_test", 0600, NULL, NULL, |
| 390 | &gup_test_fops); |
Kirill A. Shutemov | 64c349f4 | 2017-11-17 15:31:22 -0800 | [diff] [blame] | 391 | |
| 392 | return 0; |
| 393 | } |
| 394 | |
John Hubbard | 9c84f22 | 2020-12-14 19:05:05 -0800 | [diff] [blame] | 395 | late_initcall(gup_test_init); |