test_async_driver_probe.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2014 Google, Inc.
  4. */
  5. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  6. #include <linux/delay.h>
  7. #include <linux/init.h>
  8. #include <linux/hrtimer.h>
  9. #include <linux/module.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/time.h>
  12. #include <linux/numa.h>
  13. #include <linux/nodemask.h>
  14. #include <linux/topology.h>
  15. #define TEST_PROBE_DELAY (5 * 1000) /* 5 sec */
  16. #define TEST_PROBE_THRESHOLD (TEST_PROBE_DELAY / 2)
  17. static atomic_t warnings, errors, timeout, async_completed;
  18. static int test_probe(struct platform_device *pdev)
  19. {
  20. struct device *dev = &pdev->dev;
  21. /*
  22. * Determine if we have hit the "timeout" limit for the test if we
  23. * have then report it as an error, otherwise we wil sleep for the
  24. * required amount of time and then report completion.
  25. */
  26. if (atomic_read(&timeout)) {
  27. dev_err(dev, "async probe took too long\n");
  28. atomic_inc(&errors);
  29. } else {
  30. dev_dbg(&pdev->dev, "sleeping for %d msecs in probe\n",
  31. TEST_PROBE_DELAY);
  32. msleep(TEST_PROBE_DELAY);
  33. dev_dbg(&pdev->dev, "done sleeping\n");
  34. }
  35. /*
  36. * Report NUMA mismatch if device node is set and we are not
  37. * performing an async init on that node.
  38. */
  39. if (dev->driver->probe_type == PROBE_PREFER_ASYNCHRONOUS) {
  40. if (IS_ENABLED(CONFIG_NUMA) &&
  41. dev_to_node(dev) != numa_node_id()) {
  42. dev_warn(dev, "NUMA node mismatch %d != %d\n",
  43. dev_to_node(dev), numa_node_id());
  44. atomic_inc(&warnings);
  45. }
  46. atomic_inc(&async_completed);
  47. }
  48. return 0;
  49. }
  50. static struct platform_driver async_driver = {
  51. .driver = {
  52. .name = "test_async_driver",
  53. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  54. },
  55. .probe = test_probe,
  56. };
  57. static struct platform_driver sync_driver = {
  58. .driver = {
  59. .name = "test_sync_driver",
  60. .probe_type = PROBE_FORCE_SYNCHRONOUS,
  61. },
  62. .probe = test_probe,
  63. };
  64. static struct platform_device *async_dev[NR_CPUS * 2];
  65. static struct platform_device *sync_dev[2];
  66. static struct platform_device *
  67. test_platform_device_register_node(char *name, int id, int nid)
  68. {
  69. struct platform_device *pdev;
  70. int ret;
  71. pdev = platform_device_alloc(name, id);
  72. if (!pdev)
  73. return ERR_PTR(-ENOMEM);
  74. if (nid != NUMA_NO_NODE)
  75. set_dev_node(&pdev->dev, nid);
  76. ret = platform_device_add(pdev);
  77. if (ret) {
  78. platform_device_put(pdev);
  79. return ERR_PTR(ret);
  80. }
  81. return pdev;
  82. }
  83. static int __init test_async_probe_init(void)
  84. {
  85. struct platform_device **pdev = NULL;
  86. int async_id = 0, sync_id = 0;
  87. unsigned long long duration;
  88. ktime_t calltime;
  89. int err, nid, cpu;
  90. pr_info("registering first set of asynchronous devices...\n");
  91. for_each_online_cpu(cpu) {
  92. nid = cpu_to_node(cpu);
  93. pdev = &async_dev[async_id];
  94. *pdev = test_platform_device_register_node("test_async_driver",
  95. async_id,
  96. nid);
  97. if (IS_ERR(*pdev)) {
  98. err = PTR_ERR(*pdev);
  99. *pdev = NULL;
  100. pr_err("failed to create async_dev: %d\n", err);
  101. goto err_unregister_async_devs;
  102. }
  103. async_id++;
  104. }
  105. pr_info("registering asynchronous driver...\n");
  106. calltime = ktime_get();
  107. err = platform_driver_register(&async_driver);
  108. if (err) {
  109. pr_err("Failed to register async_driver: %d\n", err);
  110. goto err_unregister_async_devs;
  111. }
  112. duration = (unsigned long long)ktime_ms_delta(ktime_get(), calltime);
  113. pr_info("registration took %lld msecs\n", duration);
  114. if (duration > TEST_PROBE_THRESHOLD) {
  115. pr_err("test failed: probe took too long\n");
  116. err = -ETIMEDOUT;
  117. goto err_unregister_async_driver;
  118. }
  119. pr_info("registering second set of asynchronous devices...\n");
  120. calltime = ktime_get();
  121. for_each_online_cpu(cpu) {
  122. nid = cpu_to_node(cpu);
  123. pdev = &async_dev[async_id];
  124. *pdev = test_platform_device_register_node("test_async_driver",
  125. async_id,
  126. nid);
  127. if (IS_ERR(*pdev)) {
  128. err = PTR_ERR(*pdev);
  129. *pdev = NULL;
  130. pr_err("failed to create async_dev: %d\n", err);
  131. goto err_unregister_async_driver;
  132. }
  133. async_id++;
  134. }
  135. duration = (unsigned long long)ktime_ms_delta(ktime_get(), calltime);
  136. dev_info(&(*pdev)->dev,
  137. "registration took %lld msecs\n", duration);
  138. if (duration > TEST_PROBE_THRESHOLD) {
  139. dev_err(&(*pdev)->dev,
  140. "test failed: probe took too long\n");
  141. err = -ETIMEDOUT;
  142. goto err_unregister_async_driver;
  143. }
  144. pr_info("registering first synchronous device...\n");
  145. nid = cpu_to_node(cpu);
  146. pdev = &sync_dev[sync_id];
  147. *pdev = test_platform_device_register_node("test_sync_driver",
  148. sync_id,
  149. NUMA_NO_NODE);
  150. if (IS_ERR(*pdev)) {
  151. err = PTR_ERR(*pdev);
  152. *pdev = NULL;
  153. pr_err("failed to create sync_dev: %d\n", err);
  154. goto err_unregister_async_driver;
  155. }
  156. sync_id++;
  157. pr_info("registering synchronous driver...\n");
  158. calltime = ktime_get();
  159. err = platform_driver_register(&sync_driver);
  160. if (err) {
  161. pr_err("Failed to register async_driver: %d\n", err);
  162. goto err_unregister_sync_devs;
  163. }
  164. duration = (unsigned long long)ktime_ms_delta(ktime_get(), calltime);
  165. pr_info("registration took %lld msecs\n", duration);
  166. if (duration < TEST_PROBE_THRESHOLD) {
  167. dev_err(&(*pdev)->dev,
  168. "test failed: probe was too quick\n");
  169. err = -ETIMEDOUT;
  170. goto err_unregister_sync_driver;
  171. }
  172. pr_info("registering second synchronous device...\n");
  173. pdev = &sync_dev[sync_id];
  174. calltime = ktime_get();
  175. *pdev = test_platform_device_register_node("test_sync_driver",
  176. sync_id,
  177. NUMA_NO_NODE);
  178. if (IS_ERR(*pdev)) {
  179. err = PTR_ERR(*pdev);
  180. *pdev = NULL;
  181. pr_err("failed to create sync_dev: %d\n", err);
  182. goto err_unregister_sync_driver;
  183. }
  184. sync_id++;
  185. duration = (unsigned long long)ktime_ms_delta(ktime_get(), calltime);
  186. dev_info(&(*pdev)->dev,
  187. "registration took %lld msecs\n", duration);
  188. if (duration < TEST_PROBE_THRESHOLD) {
  189. dev_err(&(*pdev)->dev,
  190. "test failed: probe was too quick\n");
  191. err = -ETIMEDOUT;
  192. goto err_unregister_sync_driver;
  193. }
  194. /*
  195. * The async events should have completed while we were taking care
  196. * of the synchronous events. We will now terminate any outstanding
  197. * asynchronous probe calls remaining by forcing timeout and remove
  198. * the driver before we return which should force the flush of the
  199. * pending asynchronous probe calls.
  200. *
  201. * Otherwise if they completed without errors or warnings then
  202. * report successful completion.
  203. */
  204. if (atomic_read(&async_completed) != async_id) {
  205. pr_err("async events still pending, forcing timeout\n");
  206. atomic_inc(&timeout);
  207. err = -ETIMEDOUT;
  208. } else if (!atomic_read(&errors) && !atomic_read(&warnings)) {
  209. pr_info("completed successfully\n");
  210. return 0;
  211. }
  212. err_unregister_sync_driver:
  213. platform_driver_unregister(&sync_driver);
  214. err_unregister_sync_devs:
  215. while (sync_id--)
  216. platform_device_unregister(sync_dev[sync_id]);
  217. err_unregister_async_driver:
  218. platform_driver_unregister(&async_driver);
  219. err_unregister_async_devs:
  220. while (async_id--)
  221. platform_device_unregister(async_dev[async_id]);
  222. /*
  223. * If err is already set then count that as an additional error for
  224. * the test. Otherwise we will report an invalid argument error and
  225. * not count that as we should have reached here as a result of
  226. * errors or warnings being reported by the probe routine.
  227. */
  228. if (err)
  229. atomic_inc(&errors);
  230. else
  231. err = -EINVAL;
  232. pr_err("Test failed with %d errors and %d warnings\n",
  233. atomic_read(&errors), atomic_read(&warnings));
  234. return err;
  235. }
  236. module_init(test_async_probe_init);
  237. static void __exit test_async_probe_exit(void)
  238. {
  239. int id = 2;
  240. platform_driver_unregister(&async_driver);
  241. platform_driver_unregister(&sync_driver);
  242. while (id--)
  243. platform_device_unregister(sync_dev[id]);
  244. id = NR_CPUS * 2;
  245. while (id--)
  246. platform_device_unregister(async_dev[id]);
  247. }
  248. module_exit(test_async_probe_exit);
  249. MODULE_DESCRIPTION("Test module for asynchronous driver probing");
  250. MODULE_AUTHOR("Dmitry Torokhov <dtor@chromium.org>");
  251. MODULE_LICENSE("GPL");