gup_benchmark.c 2.1 KB

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