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) |
| 9 | |
| 10 | struct gup_benchmark { |
| 11 | __u64 delta_usec; |
| 12 | __u64 addr; |
| 13 | __u64 size; |
| 14 | __u32 nr_pages_per_call; |
| 15 | __u32 flags; |
| 16 | }; |
| 17 | |
| 18 | static int __gup_benchmark_ioctl(unsigned int cmd, |
| 19 | struct gup_benchmark *gup) |
| 20 | { |
| 21 | ktime_t start_time, end_time; |
YueHaibing | 5189686 | 2018-10-05 15:51:44 -0700 | [diff] [blame] | 22 | unsigned long i, nr_pages, addr, next; |
| 23 | int nr; |
Kirill A. Shutemov | 64c349f | 2017-11-17 15:31:22 -0800 | [diff] [blame] | 24 | struct page **pages; |
| 25 | |
Dan Carpenter | 3059842 | 2018-10-30 15:04:32 -0700 | [diff] [blame] | 26 | if (gup->size > ULONG_MAX) |
| 27 | return -EINVAL; |
| 28 | |
Kirill A. Shutemov | 64c349f | 2017-11-17 15:31:22 -0800 | [diff] [blame] | 29 | nr_pages = gup->size / PAGE_SIZE; |
Kees Cook | 778e1cd | 2018-06-12 14:04:48 -0700 | [diff] [blame] | 30 | pages = kvcalloc(nr_pages, sizeof(void *), GFP_KERNEL); |
Kirill A. Shutemov | 64c349f | 2017-11-17 15:31:22 -0800 | [diff] [blame] | 31 | if (!pages) |
| 32 | return -ENOMEM; |
| 33 | |
| 34 | i = 0; |
| 35 | nr = gup->nr_pages_per_call; |
| 36 | start_time = ktime_get(); |
| 37 | for (addr = gup->addr; addr < gup->addr + gup->size; addr = next) { |
| 38 | if (nr != gup->nr_pages_per_call) |
| 39 | break; |
| 40 | |
| 41 | next = addr + nr * PAGE_SIZE; |
| 42 | if (next > gup->addr + gup->size) { |
| 43 | next = gup->addr + gup->size; |
| 44 | nr = (next - addr) / PAGE_SIZE; |
| 45 | } |
| 46 | |
| 47 | nr = get_user_pages_fast(addr, nr, gup->flags & 1, pages + i); |
Michael S. Tsirkin | 09e35a4 | 2018-04-13 15:35:16 -0700 | [diff] [blame] | 48 | if (nr <= 0) |
| 49 | break; |
Kirill A. Shutemov | 64c349f | 2017-11-17 15:31:22 -0800 | [diff] [blame] | 50 | i += nr; |
| 51 | } |
| 52 | end_time = ktime_get(); |
| 53 | |
| 54 | gup->delta_usec = ktime_us_delta(end_time, start_time); |
| 55 | gup->size = addr - gup->addr; |
| 56 | |
| 57 | for (i = 0; i < nr_pages; i++) { |
| 58 | if (!pages[i]) |
| 59 | break; |
| 60 | put_page(pages[i]); |
| 61 | } |
| 62 | |
| 63 | kvfree(pages); |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | static long gup_benchmark_ioctl(struct file *filep, unsigned int cmd, |
| 68 | unsigned long arg) |
| 69 | { |
| 70 | struct gup_benchmark gup; |
| 71 | int ret; |
| 72 | |
| 73 | if (cmd != GUP_FAST_BENCHMARK) |
| 74 | return -EINVAL; |
| 75 | |
| 76 | if (copy_from_user(&gup, (void __user *)arg, sizeof(gup))) |
| 77 | return -EFAULT; |
| 78 | |
| 79 | ret = __gup_benchmark_ioctl(cmd, &gup); |
| 80 | if (ret) |
| 81 | return ret; |
| 82 | |
| 83 | if (copy_to_user((void __user *)arg, &gup, sizeof(gup))) |
| 84 | return -EFAULT; |
| 85 | |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | static const struct file_operations gup_benchmark_fops = { |
| 90 | .open = nonseekable_open, |
| 91 | .unlocked_ioctl = gup_benchmark_ioctl, |
| 92 | }; |
| 93 | |
| 94 | static int gup_benchmark_init(void) |
| 95 | { |
| 96 | void *ret; |
| 97 | |
| 98 | ret = debugfs_create_file_unsafe("gup_benchmark", 0600, NULL, NULL, |
| 99 | &gup_benchmark_fops); |
| 100 | if (!ret) |
| 101 | pr_warn("Failed to create gup_benchmark in debugfs"); |
| 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | late_initcall(gup_benchmark_init); |