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> |
| 7 | |
| 8 | #define GUP_FAST_BENCHMARK _IOWR('g', 1, struct gup_benchmark) |
Keith Busch | 714a3a1 | 2018-10-26 15:09:56 -0700 | [diff] [blame] | 9 | #define GUP_LONGTERM_BENCHMARK _IOWR('g', 2, struct gup_benchmark) |
| 10 | #define GUP_BENCHMARK _IOWR('g', 3, struct gup_benchmark) |
Kirill A. Shutemov | 64c349f | 2017-11-17 15:31:22 -0800 | [diff] [blame] | 11 | |
| 12 | struct gup_benchmark { |
Keith Busch | 26db3d0 | 2018-10-26 15:09:52 -0700 | [diff] [blame] | 13 | __u64 get_delta_usec; |
| 14 | __u64 put_delta_usec; |
Kirill A. Shutemov | 64c349f | 2017-11-17 15:31:22 -0800 | [diff] [blame] | 15 | __u64 addr; |
| 16 | __u64 size; |
| 17 | __u32 nr_pages_per_call; |
| 18 | __u32 flags; |
Keith Busch | 26db3d0 | 2018-10-26 15:09:52 -0700 | [diff] [blame] | 19 | __u64 expansion[10]; /* For future use */ |
Kirill A. Shutemov | 64c349f | 2017-11-17 15:31:22 -0800 | [diff] [blame] | 20 | }; |
| 21 | |
| 22 | static int __gup_benchmark_ioctl(unsigned int cmd, |
| 23 | struct gup_benchmark *gup) |
| 24 | { |
| 25 | ktime_t start_time, end_time; |
YueHaibing | 5189686 | 2018-10-05 15:51:44 -0700 | [diff] [blame] | 26 | unsigned long i, nr_pages, addr, next; |
| 27 | int nr; |
Kirill A. Shutemov | 64c349f | 2017-11-17 15:31:22 -0800 | [diff] [blame] | 28 | struct page **pages; |
Navid Emamdoost | a7c46c0 | 2020-01-04 13:00:12 -0800 | [diff] [blame] | 29 | int ret = 0; |
Kirill A. Shutemov | 64c349f | 2017-11-17 15:31:22 -0800 | [diff] [blame] | 30 | |
Dan Carpenter | 4b408c7 | 2018-10-30 15:04:32 -0700 | [diff] [blame] | 31 | if (gup->size > ULONG_MAX) |
| 32 | return -EINVAL; |
| 33 | |
Kirill A. Shutemov | 64c349f | 2017-11-17 15:31:22 -0800 | [diff] [blame] | 34 | nr_pages = gup->size / PAGE_SIZE; |
Kees Cook | 778e1cd | 2018-06-12 14:04:48 -0700 | [diff] [blame] | 35 | pages = kvcalloc(nr_pages, sizeof(void *), GFP_KERNEL); |
Kirill A. Shutemov | 64c349f | 2017-11-17 15:31:22 -0800 | [diff] [blame] | 36 | if (!pages) |
| 37 | return -ENOMEM; |
| 38 | |
| 39 | i = 0; |
| 40 | nr = gup->nr_pages_per_call; |
| 41 | start_time = ktime_get(); |
| 42 | for (addr = gup->addr; addr < gup->addr + gup->size; addr = next) { |
| 43 | if (nr != gup->nr_pages_per_call) |
| 44 | break; |
| 45 | |
| 46 | next = addr + nr * PAGE_SIZE; |
| 47 | if (next > gup->addr + gup->size) { |
| 48 | next = gup->addr + gup->size; |
| 49 | nr = (next - addr) / PAGE_SIZE; |
| 50 | } |
| 51 | |
John Hubbard | bdffe23 | 2020-01-30 22:13:32 -0800 | [diff] [blame] | 52 | /* Filter out most gup flags: only allow a tiny subset here: */ |
| 53 | gup->flags &= FOLL_WRITE; |
| 54 | |
Keith Busch | 714a3a1 | 2018-10-26 15:09:56 -0700 | [diff] [blame] | 55 | switch (cmd) { |
| 56 | case GUP_FAST_BENCHMARK: |
John Hubbard | bdffe23 | 2020-01-30 22:13:32 -0800 | [diff] [blame] | 57 | nr = get_user_pages_fast(addr, nr, gup->flags, |
Keith Busch | 714a3a1 | 2018-10-26 15:09:56 -0700 | [diff] [blame] | 58 | pages + i); |
| 59 | break; |
| 60 | case GUP_LONGTERM_BENCHMARK: |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 61 | nr = get_user_pages(addr, nr, |
John Hubbard | bdffe23 | 2020-01-30 22:13:32 -0800 | [diff] [blame] | 62 | gup->flags | FOLL_LONGTERM, |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 63 | pages + i, NULL); |
Keith Busch | 714a3a1 | 2018-10-26 15:09:56 -0700 | [diff] [blame] | 64 | break; |
| 65 | case GUP_BENCHMARK: |
John Hubbard | bdffe23 | 2020-01-30 22:13:32 -0800 | [diff] [blame] | 66 | nr = get_user_pages(addr, nr, gup->flags, pages + i, |
Keith Busch | 714a3a1 | 2018-10-26 15:09:56 -0700 | [diff] [blame] | 67 | NULL); |
| 68 | break; |
| 69 | default: |
Navid Emamdoost | a7c46c0 | 2020-01-04 13:00:12 -0800 | [diff] [blame] | 70 | kvfree(pages); |
| 71 | ret = -EINVAL; |
| 72 | goto out; |
Keith Busch | 714a3a1 | 2018-10-26 15:09:56 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Michael S. Tsirkin | 09e35a4 | 2018-04-13 15:35:16 -0700 | [diff] [blame] | 75 | if (nr <= 0) |
| 76 | break; |
Kirill A. Shutemov | 64c349f | 2017-11-17 15:31:22 -0800 | [diff] [blame] | 77 | i += nr; |
| 78 | } |
| 79 | end_time = ktime_get(); |
| 80 | |
Keith Busch | 26db3d0 | 2018-10-26 15:09:52 -0700 | [diff] [blame] | 81 | gup->get_delta_usec = ktime_us_delta(end_time, start_time); |
Kirill A. Shutemov | 64c349f | 2017-11-17 15:31:22 -0800 | [diff] [blame] | 82 | gup->size = addr - gup->addr; |
| 83 | |
Keith Busch | 26db3d0 | 2018-10-26 15:09:52 -0700 | [diff] [blame] | 84 | start_time = ktime_get(); |
Kirill A. Shutemov | 64c349f | 2017-11-17 15:31:22 -0800 | [diff] [blame] | 85 | for (i = 0; i < nr_pages; i++) { |
| 86 | if (!pages[i]) |
| 87 | break; |
| 88 | put_page(pages[i]); |
| 89 | } |
Keith Busch | 26db3d0 | 2018-10-26 15:09:52 -0700 | [diff] [blame] | 90 | end_time = ktime_get(); |
| 91 | gup->put_delta_usec = ktime_us_delta(end_time, start_time); |
Kirill A. Shutemov | 64c349f | 2017-11-17 15:31:22 -0800 | [diff] [blame] | 92 | |
| 93 | kvfree(pages); |
Navid Emamdoost | a7c46c0 | 2020-01-04 13:00:12 -0800 | [diff] [blame] | 94 | out: |
| 95 | return ret; |
Kirill A. Shutemov | 64c349f | 2017-11-17 15:31:22 -0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | static long gup_benchmark_ioctl(struct file *filep, unsigned int cmd, |
| 99 | unsigned long arg) |
| 100 | { |
| 101 | struct gup_benchmark gup; |
| 102 | int ret; |
| 103 | |
Keith Busch | 714a3a1 | 2018-10-26 15:09:56 -0700 | [diff] [blame] | 104 | switch (cmd) { |
| 105 | case GUP_FAST_BENCHMARK: |
| 106 | case GUP_LONGTERM_BENCHMARK: |
| 107 | case GUP_BENCHMARK: |
| 108 | break; |
| 109 | default: |
Kirill A. Shutemov | 64c349f | 2017-11-17 15:31:22 -0800 | [diff] [blame] | 110 | return -EINVAL; |
Keith Busch | 714a3a1 | 2018-10-26 15:09:56 -0700 | [diff] [blame] | 111 | } |
Kirill A. Shutemov | 64c349f | 2017-11-17 15:31:22 -0800 | [diff] [blame] | 112 | |
| 113 | if (copy_from_user(&gup, (void __user *)arg, sizeof(gup))) |
| 114 | return -EFAULT; |
| 115 | |
| 116 | ret = __gup_benchmark_ioctl(cmd, &gup); |
| 117 | if (ret) |
| 118 | return ret; |
| 119 | |
| 120 | if (copy_to_user((void __user *)arg, &gup, sizeof(gup))) |
| 121 | return -EFAULT; |
| 122 | |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | static const struct file_operations gup_benchmark_fops = { |
| 127 | .open = nonseekable_open, |
| 128 | .unlocked_ioctl = gup_benchmark_ioctl, |
| 129 | }; |
| 130 | |
| 131 | static int gup_benchmark_init(void) |
| 132 | { |
Greg Kroah-Hartman | d9f7979 | 2019-03-05 15:46:09 -0800 | [diff] [blame] | 133 | debugfs_create_file_unsafe("gup_benchmark", 0600, NULL, NULL, |
| 134 | &gup_benchmark_fops); |
Kirill A. Shutemov | 64c349f | 2017-11-17 15:31:22 -0800 | [diff] [blame] | 135 | |
| 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | late_initcall(gup_benchmark_init); |