gup_benchmark.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. #define GUP_BENCHMARK _IOWR('g', 2, struct gup_benchmark)
  9. #define PIN_FAST_BENCHMARK _IOWR('g', 3, struct gup_benchmark)
  10. #define PIN_BENCHMARK _IOWR('g', 4, struct gup_benchmark)
  11. #define PIN_LONGTERM_BENCHMARK _IOWR('g', 5, struct gup_benchmark)
  12. struct gup_benchmark {
  13. __u64 get_delta_usec;
  14. __u64 put_delta_usec;
  15. __u64 addr;
  16. __u64 size;
  17. __u32 nr_pages_per_call;
  18. __u32 flags;
  19. __u64 expansion[10]; /* For future use */
  20. };
  21. static void put_back_pages(unsigned int cmd, struct page **pages,
  22. unsigned long nr_pages)
  23. {
  24. unsigned long i;
  25. switch (cmd) {
  26. case GUP_FAST_BENCHMARK:
  27. case GUP_BENCHMARK:
  28. for (i = 0; i < nr_pages; i++)
  29. put_page(pages[i]);
  30. break;
  31. case PIN_FAST_BENCHMARK:
  32. case PIN_BENCHMARK:
  33. case PIN_LONGTERM_BENCHMARK:
  34. unpin_user_pages(pages, nr_pages);
  35. break;
  36. }
  37. }
  38. static void verify_dma_pinned(unsigned int cmd, struct page **pages,
  39. unsigned long nr_pages)
  40. {
  41. unsigned long i;
  42. struct page *page;
  43. switch (cmd) {
  44. case PIN_FAST_BENCHMARK:
  45. case PIN_BENCHMARK:
  46. case PIN_LONGTERM_BENCHMARK:
  47. for (i = 0; i < nr_pages; i++) {
  48. page = pages[i];
  49. if (WARN(!page_maybe_dma_pinned(page),
  50. "pages[%lu] is NOT dma-pinned\n", i)) {
  51. dump_page(page, "gup_benchmark failure");
  52. break;
  53. }
  54. }
  55. break;
  56. }
  57. }
  58. static int __gup_benchmark_ioctl(unsigned int cmd,
  59. struct gup_benchmark *gup)
  60. {
  61. ktime_t start_time, end_time;
  62. unsigned long i, nr_pages, addr, next;
  63. int nr;
  64. struct page **pages;
  65. int ret = 0;
  66. bool needs_mmap_lock =
  67. cmd != GUP_FAST_BENCHMARK && cmd != PIN_FAST_BENCHMARK;
  68. if (gup->size > ULONG_MAX)
  69. return -EINVAL;
  70. nr_pages = gup->size / PAGE_SIZE;
  71. pages = kvcalloc(nr_pages, sizeof(void *), GFP_KERNEL);
  72. if (!pages)
  73. return -ENOMEM;
  74. if (needs_mmap_lock && mmap_read_lock_killable(current->mm)) {
  75. ret = -EINTR;
  76. goto free_pages;
  77. }
  78. i = 0;
  79. nr = gup->nr_pages_per_call;
  80. start_time = ktime_get();
  81. for (addr = gup->addr; addr < gup->addr + gup->size; addr = next) {
  82. if (nr != gup->nr_pages_per_call)
  83. break;
  84. next = addr + nr * PAGE_SIZE;
  85. if (next > gup->addr + gup->size) {
  86. next = gup->addr + gup->size;
  87. nr = (next - addr) / PAGE_SIZE;
  88. }
  89. /* Filter out most gup flags: only allow a tiny subset here: */
  90. gup->flags &= FOLL_WRITE;
  91. switch (cmd) {
  92. case GUP_FAST_BENCHMARK:
  93. nr = get_user_pages_fast(addr, nr, gup->flags,
  94. pages + i);
  95. break;
  96. case GUP_BENCHMARK:
  97. nr = get_user_pages(addr, nr, gup->flags, pages + i,
  98. NULL);
  99. break;
  100. case PIN_FAST_BENCHMARK:
  101. nr = pin_user_pages_fast(addr, nr, gup->flags,
  102. pages + i);
  103. break;
  104. case PIN_BENCHMARK:
  105. nr = pin_user_pages(addr, nr, gup->flags, pages + i,
  106. NULL);
  107. break;
  108. case PIN_LONGTERM_BENCHMARK:
  109. nr = pin_user_pages(addr, nr,
  110. gup->flags | FOLL_LONGTERM,
  111. pages + i, NULL);
  112. break;
  113. default:
  114. ret = -EINVAL;
  115. goto unlock;
  116. }
  117. if (nr <= 0)
  118. break;
  119. i += nr;
  120. }
  121. end_time = ktime_get();
  122. /* Shifting the meaning of nr_pages: now it is actual number pinned: */
  123. nr_pages = i;
  124. gup->get_delta_usec = ktime_us_delta(end_time, start_time);
  125. gup->size = addr - gup->addr;
  126. /*
  127. * Take an un-benchmark-timed moment to verify DMA pinned
  128. * state: print a warning if any non-dma-pinned pages are found:
  129. */
  130. verify_dma_pinned(cmd, pages, nr_pages);
  131. start_time = ktime_get();
  132. put_back_pages(cmd, pages, nr_pages);
  133. end_time = ktime_get();
  134. gup->put_delta_usec = ktime_us_delta(end_time, start_time);
  135. unlock:
  136. if (needs_mmap_lock)
  137. mmap_read_unlock(current->mm);
  138. free_pages:
  139. kvfree(pages);
  140. return ret;
  141. }
  142. static long gup_benchmark_ioctl(struct file *filep, unsigned int cmd,
  143. unsigned long arg)
  144. {
  145. struct gup_benchmark gup;
  146. int ret;
  147. switch (cmd) {
  148. case GUP_FAST_BENCHMARK:
  149. case GUP_BENCHMARK:
  150. case PIN_FAST_BENCHMARK:
  151. case PIN_BENCHMARK:
  152. case PIN_LONGTERM_BENCHMARK:
  153. break;
  154. default:
  155. return -EINVAL;
  156. }
  157. if (copy_from_user(&gup, (void __user *)arg, sizeof(gup)))
  158. return -EFAULT;
  159. ret = __gup_benchmark_ioctl(cmd, &gup);
  160. if (ret)
  161. return ret;
  162. if (copy_to_user((void __user *)arg, &gup, sizeof(gup)))
  163. return -EFAULT;
  164. return 0;
  165. }
  166. static const struct file_operations gup_benchmark_fops = {
  167. .open = nonseekable_open,
  168. .unlocked_ioctl = gup_benchmark_ioctl,
  169. };
  170. static int gup_benchmark_init(void)
  171. {
  172. debugfs_create_file_unsafe("gup_benchmark", 0600, NULL, NULL,
  173. &gup_benchmark_fops);
  174. return 0;
  175. }
  176. late_initcall(gup_benchmark_init);