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