gup_benchmark.c 2.1 KB

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