map_benchmark.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2020 HiSilicon Limited.
  4. */
  5. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  6. #include <linux/debugfs.h>
  7. #include <linux/delay.h>
  8. #include <linux/device.h>
  9. #include <linux/dma-mapping.h>
  10. #include <linux/kernel.h>
  11. #include <linux/kthread.h>
  12. #include <linux/map_benchmark.h>
  13. #include <linux/math64.h>
  14. #include <linux/module.h>
  15. #include <linux/pci.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/slab.h>
  18. #include <linux/timekeeping.h>
  19. struct map_benchmark_data {
  20. struct map_benchmark bparam;
  21. struct device *dev;
  22. struct dentry *debugfs;
  23. enum dma_data_direction dir;
  24. atomic64_t sum_map_100ns;
  25. atomic64_t sum_unmap_100ns;
  26. atomic64_t sum_sq_map;
  27. atomic64_t sum_sq_unmap;
  28. atomic64_t loops;
  29. };
  30. static int map_benchmark_thread(void *data)
  31. {
  32. void *buf;
  33. dma_addr_t dma_addr;
  34. struct map_benchmark_data *map = data;
  35. int npages = map->bparam.granule;
  36. u64 size = npages * PAGE_SIZE;
  37. int ret = 0;
  38. buf = alloc_pages_exact(size, GFP_KERNEL);
  39. if (!buf)
  40. return -ENOMEM;
  41. while (!kthread_should_stop()) {
  42. u64 map_100ns, unmap_100ns, map_sq, unmap_sq;
  43. ktime_t map_stime, map_etime, unmap_stime, unmap_etime;
  44. ktime_t map_delta, unmap_delta;
  45. /*
  46. * for a non-coherent device, if we don't stain them in the
  47. * cache, this will give an underestimate of the real-world
  48. * overhead of BIDIRECTIONAL or TO_DEVICE mappings;
  49. * 66 means evertything goes well! 66 is lucky.
  50. */
  51. if (map->dir != DMA_FROM_DEVICE)
  52. memset(buf, 0x66, size);
  53. map_stime = ktime_get();
  54. dma_addr = dma_map_single(map->dev, buf, size, map->dir);
  55. if (unlikely(dma_mapping_error(map->dev, dma_addr))) {
  56. pr_err("dma_map_single failed on %s\n",
  57. dev_name(map->dev));
  58. ret = -ENOMEM;
  59. goto out;
  60. }
  61. map_etime = ktime_get();
  62. map_delta = ktime_sub(map_etime, map_stime);
  63. /* Pretend DMA is transmitting */
  64. ndelay(map->bparam.dma_trans_ns);
  65. unmap_stime = ktime_get();
  66. dma_unmap_single(map->dev, dma_addr, size, map->dir);
  67. unmap_etime = ktime_get();
  68. unmap_delta = ktime_sub(unmap_etime, unmap_stime);
  69. /* calculate sum and sum of squares */
  70. map_100ns = div64_ul(map_delta, 100);
  71. unmap_100ns = div64_ul(unmap_delta, 100);
  72. map_sq = map_100ns * map_100ns;
  73. unmap_sq = unmap_100ns * unmap_100ns;
  74. atomic64_add(map_100ns, &map->sum_map_100ns);
  75. atomic64_add(unmap_100ns, &map->sum_unmap_100ns);
  76. atomic64_add(map_sq, &map->sum_sq_map);
  77. atomic64_add(unmap_sq, &map->sum_sq_unmap);
  78. atomic64_inc(&map->loops);
  79. /*
  80. * We may test for a long time so periodically check whether
  81. * we need to schedule to avoid starving the others. Otherwise
  82. * we may hangup the kernel in a non-preemptible kernel when
  83. * the test kthreads number >= CPU number, the test kthreads
  84. * will run endless on every CPU since the thread resposible
  85. * for notifying the kthread stop (in do_map_benchmark())
  86. * could not be scheduled.
  87. *
  88. * Note this may degrade the test concurrency since the test
  89. * threads may need to share the CPU time with other load
  90. * in the system. So it's recommended to run this benchmark
  91. * on an idle system.
  92. */
  93. cond_resched();
  94. }
  95. out:
  96. free_pages_exact(buf, size);
  97. return ret;
  98. }
  99. static int do_map_benchmark(struct map_benchmark_data *map)
  100. {
  101. struct task_struct **tsk;
  102. int threads = map->bparam.threads;
  103. int node = map->bparam.node;
  104. u64 loops;
  105. int ret = 0;
  106. int i;
  107. tsk = kmalloc_array(threads, sizeof(*tsk), GFP_KERNEL);
  108. if (!tsk)
  109. return -ENOMEM;
  110. get_device(map->dev);
  111. for (i = 0; i < threads; i++) {
  112. tsk[i] = kthread_create_on_node(map_benchmark_thread, map,
  113. map->bparam.node, "dma-map-benchmark/%d", i);
  114. if (IS_ERR(tsk[i])) {
  115. pr_err("create dma_map thread failed\n");
  116. ret = PTR_ERR(tsk[i]);
  117. while (--i >= 0)
  118. kthread_stop(tsk[i]);
  119. goto out;
  120. }
  121. if (node != NUMA_NO_NODE)
  122. kthread_bind_mask(tsk[i], cpumask_of_node(node));
  123. }
  124. /* clear the old value in the previous benchmark */
  125. atomic64_set(&map->sum_map_100ns, 0);
  126. atomic64_set(&map->sum_unmap_100ns, 0);
  127. atomic64_set(&map->sum_sq_map, 0);
  128. atomic64_set(&map->sum_sq_unmap, 0);
  129. atomic64_set(&map->loops, 0);
  130. for (i = 0; i < threads; i++) {
  131. get_task_struct(tsk[i]);
  132. wake_up_process(tsk[i]);
  133. }
  134. msleep_interruptible(map->bparam.seconds * 1000);
  135. /* wait for the completion of all started benchmark threads */
  136. for (i = 0; i < threads; i++) {
  137. int kthread_ret = kthread_stop_put(tsk[i]);
  138. if (kthread_ret)
  139. ret = kthread_ret;
  140. }
  141. if (ret)
  142. goto out;
  143. loops = atomic64_read(&map->loops);
  144. if (likely(loops > 0)) {
  145. u64 map_variance, unmap_variance;
  146. u64 sum_map = atomic64_read(&map->sum_map_100ns);
  147. u64 sum_unmap = atomic64_read(&map->sum_unmap_100ns);
  148. u64 sum_sq_map = atomic64_read(&map->sum_sq_map);
  149. u64 sum_sq_unmap = atomic64_read(&map->sum_sq_unmap);
  150. /* average latency */
  151. map->bparam.avg_map_100ns = div64_u64(sum_map, loops);
  152. map->bparam.avg_unmap_100ns = div64_u64(sum_unmap, loops);
  153. /* standard deviation of latency */
  154. map_variance = div64_u64(sum_sq_map, loops) -
  155. map->bparam.avg_map_100ns *
  156. map->bparam.avg_map_100ns;
  157. unmap_variance = div64_u64(sum_sq_unmap, loops) -
  158. map->bparam.avg_unmap_100ns *
  159. map->bparam.avg_unmap_100ns;
  160. map->bparam.map_stddev = int_sqrt64(map_variance);
  161. map->bparam.unmap_stddev = int_sqrt64(unmap_variance);
  162. }
  163. out:
  164. put_device(map->dev);
  165. kfree(tsk);
  166. return ret;
  167. }
  168. static long map_benchmark_ioctl(struct file *file, unsigned int cmd,
  169. unsigned long arg)
  170. {
  171. struct map_benchmark_data *map = file->private_data;
  172. void __user *argp = (void __user *)arg;
  173. u64 old_dma_mask;
  174. int ret;
  175. if (copy_from_user(&map->bparam, argp, sizeof(map->bparam)))
  176. return -EFAULT;
  177. switch (cmd) {
  178. case DMA_MAP_BENCHMARK:
  179. if (map->bparam.threads == 0 ||
  180. map->bparam.threads > DMA_MAP_MAX_THREADS) {
  181. pr_err("invalid thread number\n");
  182. return -EINVAL;
  183. }
  184. if (map->bparam.seconds == 0 ||
  185. map->bparam.seconds > DMA_MAP_MAX_SECONDS) {
  186. pr_err("invalid duration seconds\n");
  187. return -EINVAL;
  188. }
  189. if (map->bparam.dma_trans_ns > DMA_MAP_MAX_TRANS_DELAY) {
  190. pr_err("invalid transmission delay\n");
  191. return -EINVAL;
  192. }
  193. if (map->bparam.node != NUMA_NO_NODE &&
  194. (map->bparam.node < 0 || map->bparam.node >= MAX_NUMNODES ||
  195. !node_possible(map->bparam.node))) {
  196. pr_err("invalid numa node\n");
  197. return -EINVAL;
  198. }
  199. if (map->bparam.granule < 1 || map->bparam.granule > 1024) {
  200. pr_err("invalid granule size\n");
  201. return -EINVAL;
  202. }
  203. switch (map->bparam.dma_dir) {
  204. case DMA_MAP_BIDIRECTIONAL:
  205. map->dir = DMA_BIDIRECTIONAL;
  206. break;
  207. case DMA_MAP_FROM_DEVICE:
  208. map->dir = DMA_FROM_DEVICE;
  209. break;
  210. case DMA_MAP_TO_DEVICE:
  211. map->dir = DMA_TO_DEVICE;
  212. break;
  213. default:
  214. pr_err("invalid DMA direction\n");
  215. return -EINVAL;
  216. }
  217. old_dma_mask = dma_get_mask(map->dev);
  218. ret = dma_set_mask(map->dev,
  219. DMA_BIT_MASK(map->bparam.dma_bits));
  220. if (ret) {
  221. pr_err("failed to set dma_mask on device %s\n",
  222. dev_name(map->dev));
  223. return -EINVAL;
  224. }
  225. ret = do_map_benchmark(map);
  226. /*
  227. * restore the original dma_mask as many devices' dma_mask are
  228. * set by architectures, acpi, busses. When we bind them back
  229. * to their original drivers, those drivers shouldn't see
  230. * dma_mask changed by benchmark
  231. */
  232. dma_set_mask(map->dev, old_dma_mask);
  233. if (ret)
  234. return ret;
  235. break;
  236. default:
  237. return -EINVAL;
  238. }
  239. if (copy_to_user(argp, &map->bparam, sizeof(map->bparam)))
  240. return -EFAULT;
  241. return ret;
  242. }
  243. static const struct file_operations map_benchmark_fops = {
  244. .open = simple_open,
  245. .unlocked_ioctl = map_benchmark_ioctl,
  246. };
  247. static void map_benchmark_remove_debugfs(void *data)
  248. {
  249. struct map_benchmark_data *map = (struct map_benchmark_data *)data;
  250. debugfs_remove(map->debugfs);
  251. }
  252. static int __map_benchmark_probe(struct device *dev)
  253. {
  254. struct dentry *entry;
  255. struct map_benchmark_data *map;
  256. int ret;
  257. map = devm_kzalloc(dev, sizeof(*map), GFP_KERNEL);
  258. if (!map)
  259. return -ENOMEM;
  260. map->dev = dev;
  261. ret = devm_add_action(dev, map_benchmark_remove_debugfs, map);
  262. if (ret) {
  263. pr_err("Can't add debugfs remove action\n");
  264. return ret;
  265. }
  266. /*
  267. * we only permit a device bound with this driver, 2nd probe
  268. * will fail
  269. */
  270. entry = debugfs_create_file("dma_map_benchmark", 0600, NULL, map,
  271. &map_benchmark_fops);
  272. if (IS_ERR(entry))
  273. return PTR_ERR(entry);
  274. map->debugfs = entry;
  275. return 0;
  276. }
  277. static int map_benchmark_platform_probe(struct platform_device *pdev)
  278. {
  279. return __map_benchmark_probe(&pdev->dev);
  280. }
  281. static struct platform_driver map_benchmark_platform_driver = {
  282. .driver = {
  283. .name = "dma_map_benchmark",
  284. },
  285. .probe = map_benchmark_platform_probe,
  286. };
  287. static int
  288. map_benchmark_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  289. {
  290. return __map_benchmark_probe(&pdev->dev);
  291. }
  292. static struct pci_driver map_benchmark_pci_driver = {
  293. .name = "dma_map_benchmark",
  294. .probe = map_benchmark_pci_probe,
  295. };
  296. static int __init map_benchmark_init(void)
  297. {
  298. int ret;
  299. ret = pci_register_driver(&map_benchmark_pci_driver);
  300. if (ret)
  301. return ret;
  302. ret = platform_driver_register(&map_benchmark_platform_driver);
  303. if (ret) {
  304. pci_unregister_driver(&map_benchmark_pci_driver);
  305. return ret;
  306. }
  307. return 0;
  308. }
  309. static void __exit map_benchmark_cleanup(void)
  310. {
  311. platform_driver_unregister(&map_benchmark_platform_driver);
  312. pci_unregister_driver(&map_benchmark_pci_driver);
  313. }
  314. module_init(map_benchmark_init);
  315. module_exit(map_benchmark_cleanup);
  316. MODULE_AUTHOR("Barry Song <song.bao.hua@hisilicon.com>");
  317. MODULE_DESCRIPTION("dma_map benchmark driver");