Kirill A. Shutemov | 64c349f | 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> |
John Hubbard | b9dcfdf | 2020-12-14 19:05:08 -0800 | [diff] [blame] | 7 | #include "gup_test.h" |
Kirill A. Shutemov | 64c349f | 2017-11-17 15:31:22 -0800 | [diff] [blame] | 8 | |
John Hubbard | 41c45d3 | 2020-04-01 21:05:41 -0700 | [diff] [blame] | 9 | static 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 Hubbard | a9bed1e | 2020-12-14 19:05:17 -0800 | [diff] [blame^] | 16 | case GUP_BASIC_TEST: |
John Hubbard | 41c45d3 | 2020-04-01 21:05:41 -0700 | [diff] [blame] | 17 | for (i = 0; i < nr_pages; i++) |
| 18 | put_page(pages[i]); |
| 19 | break; |
| 20 | |
| 21 | case PIN_FAST_BENCHMARK: |
John Hubbard | a9bed1e | 2020-12-14 19:05:17 -0800 | [diff] [blame^] | 22 | case PIN_BASIC_TEST: |
Barry Song | 657d4f7 | 2020-10-13 16:51:54 -0700 | [diff] [blame] | 23 | case PIN_LONGTERM_BENCHMARK: |
John Hubbard | 41c45d3 | 2020-04-01 21:05:41 -0700 | [diff] [blame] | 24 | unpin_user_pages(pages, nr_pages); |
| 25 | break; |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | static 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 Hubbard | a9bed1e | 2020-12-14 19:05:17 -0800 | [diff] [blame^] | 37 | case PIN_BASIC_TEST: |
Barry Song | 657d4f7 | 2020-10-13 16:51:54 -0700 | [diff] [blame] | 38 | case PIN_LONGTERM_BENCHMARK: |
John Hubbard | 41c45d3 | 2020-04-01 21:05:41 -0700 | [diff] [blame] | 39 | 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 Hubbard | 9c84f22 | 2020-12-14 19:05:05 -0800 | [diff] [blame] | 44 | dump_page(page, "gup_test failure"); |
John Hubbard | 41c45d3 | 2020-04-01 21:05:41 -0700 | [diff] [blame] | 45 | break; |
| 46 | } |
| 47 | } |
| 48 | break; |
| 49 | } |
| 50 | } |
| 51 | |
John Hubbard | 9c84f22 | 2020-12-14 19:05:05 -0800 | [diff] [blame] | 52 | static int __gup_test_ioctl(unsigned int cmd, |
| 53 | struct gup_test *gup) |
Kirill A. Shutemov | 64c349f | 2017-11-17 15:31:22 -0800 | [diff] [blame] | 54 | { |
| 55 | ktime_t start_time, end_time; |
YueHaibing | 5189686 | 2018-10-05 15:51:44 -0700 | [diff] [blame] | 56 | unsigned long i, nr_pages, addr, next; |
| 57 | int nr; |
Kirill A. Shutemov | 64c349f | 2017-11-17 15:31:22 -0800 | [diff] [blame] | 58 | struct page **pages; |
Navid Emamdoost | a7c46c0 | 2020-01-04 13:00:12 -0800 | [diff] [blame] | 59 | int ret = 0; |
Jann Horn | f396459 | 2020-10-17 16:14:12 -0700 | [diff] [blame] | 60 | bool needs_mmap_lock = |
| 61 | cmd != GUP_FAST_BENCHMARK && cmd != PIN_FAST_BENCHMARK; |
Kirill A. Shutemov | 64c349f | 2017-11-17 15:31:22 -0800 | [diff] [blame] | 62 | |
Dan Carpenter | 4b408c7 | 2018-10-30 15:04:32 -0700 | [diff] [blame] | 63 | if (gup->size > ULONG_MAX) |
| 64 | return -EINVAL; |
| 65 | |
Kirill A. Shutemov | 64c349f | 2017-11-17 15:31:22 -0800 | [diff] [blame] | 66 | nr_pages = gup->size / PAGE_SIZE; |
Kees Cook | 778e1cd | 2018-06-12 14:04:48 -0700 | [diff] [blame] | 67 | pages = kvcalloc(nr_pages, sizeof(void *), GFP_KERNEL); |
Kirill A. Shutemov | 64c349f | 2017-11-17 15:31:22 -0800 | [diff] [blame] | 68 | if (!pages) |
| 69 | return -ENOMEM; |
| 70 | |
Jann Horn | f396459 | 2020-10-17 16:14:12 -0700 | [diff] [blame] | 71 | if (needs_mmap_lock && mmap_read_lock_killable(current->mm)) { |
| 72 | ret = -EINTR; |
| 73 | goto free_pages; |
| 74 | } |
| 75 | |
Kirill A. Shutemov | 64c349f | 2017-11-17 15:31:22 -0800 | [diff] [blame] | 76 | 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 Hubbard | bdffe23 | 2020-01-30 22:13:32 -0800 | [diff] [blame] | 89 | /* Filter out most gup flags: only allow a tiny subset here: */ |
| 90 | gup->flags &= FOLL_WRITE; |
| 91 | |
Keith Busch | 714a3a1 | 2018-10-26 15:09:56 -0700 | [diff] [blame] | 92 | switch (cmd) { |
| 93 | case GUP_FAST_BENCHMARK: |
John Hubbard | bdffe23 | 2020-01-30 22:13:32 -0800 | [diff] [blame] | 94 | nr = get_user_pages_fast(addr, nr, gup->flags, |
Keith Busch | 714a3a1 | 2018-10-26 15:09:56 -0700 | [diff] [blame] | 95 | pages + i); |
| 96 | break; |
John Hubbard | a9bed1e | 2020-12-14 19:05:17 -0800 | [diff] [blame^] | 97 | case GUP_BASIC_TEST: |
John Hubbard | bdffe23 | 2020-01-30 22:13:32 -0800 | [diff] [blame] | 98 | nr = get_user_pages(addr, nr, gup->flags, pages + i, |
Keith Busch | 714a3a1 | 2018-10-26 15:09:56 -0700 | [diff] [blame] | 99 | NULL); |
| 100 | break; |
John Hubbard | 41c45d3 | 2020-04-01 21:05:41 -0700 | [diff] [blame] | 101 | case PIN_FAST_BENCHMARK: |
| 102 | nr = pin_user_pages_fast(addr, nr, gup->flags, |
| 103 | pages + i); |
| 104 | break; |
John Hubbard | a9bed1e | 2020-12-14 19:05:17 -0800 | [diff] [blame^] | 105 | case PIN_BASIC_TEST: |
John Hubbard | 41c45d3 | 2020-04-01 21:05:41 -0700 | [diff] [blame] | 106 | nr = pin_user_pages(addr, nr, gup->flags, pages + i, |
| 107 | NULL); |
| 108 | break; |
Barry Song | 657d4f7 | 2020-10-13 16:51:54 -0700 | [diff] [blame] | 109 | case PIN_LONGTERM_BENCHMARK: |
| 110 | nr = pin_user_pages(addr, nr, |
| 111 | gup->flags | FOLL_LONGTERM, |
| 112 | pages + i, NULL); |
| 113 | break; |
Keith Busch | 714a3a1 | 2018-10-26 15:09:56 -0700 | [diff] [blame] | 114 | default: |
Navid Emamdoost | a7c46c0 | 2020-01-04 13:00:12 -0800 | [diff] [blame] | 115 | ret = -EINVAL; |
Jann Horn | f396459 | 2020-10-17 16:14:12 -0700 | [diff] [blame] | 116 | goto unlock; |
Keith Busch | 714a3a1 | 2018-10-26 15:09:56 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Michael S. Tsirkin | 09e35a4 | 2018-04-13 15:35:16 -0700 | [diff] [blame] | 119 | if (nr <= 0) |
| 120 | break; |
Kirill A. Shutemov | 64c349f | 2017-11-17 15:31:22 -0800 | [diff] [blame] | 121 | i += nr; |
| 122 | } |
| 123 | end_time = ktime_get(); |
| 124 | |
John Hubbard | 41c45d3 | 2020-04-01 21:05:41 -0700 | [diff] [blame] | 125 | /* Shifting the meaning of nr_pages: now it is actual number pinned: */ |
| 126 | nr_pages = i; |
| 127 | |
Keith Busch | 26db3d0 | 2018-10-26 15:09:52 -0700 | [diff] [blame] | 128 | gup->get_delta_usec = ktime_us_delta(end_time, start_time); |
Kirill A. Shutemov | 64c349f | 2017-11-17 15:31:22 -0800 | [diff] [blame] | 129 | gup->size = addr - gup->addr; |
| 130 | |
John Hubbard | 41c45d3 | 2020-04-01 21:05:41 -0700 | [diff] [blame] | 131 | /* |
| 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 Busch | 26db3d0 | 2018-10-26 15:09:52 -0700 | [diff] [blame] | 137 | start_time = ktime_get(); |
John Hubbard | 41c45d3 | 2020-04-01 21:05:41 -0700 | [diff] [blame] | 138 | |
| 139 | put_back_pages(cmd, pages, nr_pages); |
| 140 | |
Keith Busch | 26db3d0 | 2018-10-26 15:09:52 -0700 | [diff] [blame] | 141 | end_time = ktime_get(); |
| 142 | gup->put_delta_usec = ktime_us_delta(end_time, start_time); |
Kirill A. Shutemov | 64c349f | 2017-11-17 15:31:22 -0800 | [diff] [blame] | 143 | |
Jann Horn | f396459 | 2020-10-17 16:14:12 -0700 | [diff] [blame] | 144 | unlock: |
| 145 | if (needs_mmap_lock) |
| 146 | mmap_read_unlock(current->mm); |
| 147 | free_pages: |
Kirill A. Shutemov | 64c349f | 2017-11-17 15:31:22 -0800 | [diff] [blame] | 148 | kvfree(pages); |
Navid Emamdoost | a7c46c0 | 2020-01-04 13:00:12 -0800 | [diff] [blame] | 149 | return ret; |
Kirill A. Shutemov | 64c349f | 2017-11-17 15:31:22 -0800 | [diff] [blame] | 150 | } |
| 151 | |
John Hubbard | 9c84f22 | 2020-12-14 19:05:05 -0800 | [diff] [blame] | 152 | static long gup_test_ioctl(struct file *filep, unsigned int cmd, |
Kirill A. Shutemov | 64c349f | 2017-11-17 15:31:22 -0800 | [diff] [blame] | 153 | unsigned long arg) |
| 154 | { |
John Hubbard | 9c84f22 | 2020-12-14 19:05:05 -0800 | [diff] [blame] | 155 | struct gup_test gup; |
Kirill A. Shutemov | 64c349f | 2017-11-17 15:31:22 -0800 | [diff] [blame] | 156 | int ret; |
| 157 | |
Keith Busch | 714a3a1 | 2018-10-26 15:09:56 -0700 | [diff] [blame] | 158 | switch (cmd) { |
| 159 | case GUP_FAST_BENCHMARK: |
John Hubbard | 41c45d3 | 2020-04-01 21:05:41 -0700 | [diff] [blame] | 160 | case PIN_FAST_BENCHMARK: |
Barry Song | 657d4f7 | 2020-10-13 16:51:54 -0700 | [diff] [blame] | 161 | case PIN_LONGTERM_BENCHMARK: |
John Hubbard | a9bed1e | 2020-12-14 19:05:17 -0800 | [diff] [blame^] | 162 | case GUP_BASIC_TEST: |
| 163 | case PIN_BASIC_TEST: |
Keith Busch | 714a3a1 | 2018-10-26 15:09:56 -0700 | [diff] [blame] | 164 | break; |
| 165 | default: |
Kirill A. Shutemov | 64c349f | 2017-11-17 15:31:22 -0800 | [diff] [blame] | 166 | return -EINVAL; |
Keith Busch | 714a3a1 | 2018-10-26 15:09:56 -0700 | [diff] [blame] | 167 | } |
Kirill A. Shutemov | 64c349f | 2017-11-17 15:31:22 -0800 | [diff] [blame] | 168 | |
| 169 | if (copy_from_user(&gup, (void __user *)arg, sizeof(gup))) |
| 170 | return -EFAULT; |
| 171 | |
John Hubbard | 9c84f22 | 2020-12-14 19:05:05 -0800 | [diff] [blame] | 172 | ret = __gup_test_ioctl(cmd, &gup); |
Kirill A. Shutemov | 64c349f | 2017-11-17 15:31:22 -0800 | [diff] [blame] | 173 | 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 Hubbard | 9c84f22 | 2020-12-14 19:05:05 -0800 | [diff] [blame] | 182 | static const struct file_operations gup_test_fops = { |
Kirill A. Shutemov | 64c349f | 2017-11-17 15:31:22 -0800 | [diff] [blame] | 183 | .open = nonseekable_open, |
John Hubbard | 9c84f22 | 2020-12-14 19:05:05 -0800 | [diff] [blame] | 184 | .unlocked_ioctl = gup_test_ioctl, |
Kirill A. Shutemov | 64c349f | 2017-11-17 15:31:22 -0800 | [diff] [blame] | 185 | }; |
| 186 | |
John Hubbard | 9c84f22 | 2020-12-14 19:05:05 -0800 | [diff] [blame] | 187 | static int gup_test_init(void) |
Kirill A. Shutemov | 64c349f | 2017-11-17 15:31:22 -0800 | [diff] [blame] | 188 | { |
John Hubbard | 9c84f22 | 2020-12-14 19:05:05 -0800 | [diff] [blame] | 189 | debugfs_create_file_unsafe("gup_test", 0600, NULL, NULL, |
| 190 | &gup_test_fops); |
Kirill A. Shutemov | 64c349f | 2017-11-17 15:31:22 -0800 | [diff] [blame] | 191 | |
| 192 | return 0; |
| 193 | } |
| 194 | |
John Hubbard | 9c84f22 | 2020-12-14 19:05:05 -0800 | [diff] [blame] | 195 | late_initcall(gup_test_init); |