test_kmod.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231
  1. // SPDX-License-Identifier: GPL-2.0-or-later OR copyleft-next-0.3.1
  2. /*
  3. * kmod stress test driver
  4. *
  5. * Copyright (C) 2017 Luis R. Rodriguez <mcgrof@kernel.org>
  6. */
  7. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8. /*
  9. * This driver provides an interface to trigger and test the kernel's
  10. * module loader through a series of configurations and a few triggers.
  11. * To test this driver use the following script as root:
  12. *
  13. * tools/testing/selftests/kmod/kmod.sh --help
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/kmod.h>
  18. #include <linux/printk.h>
  19. #include <linux/kthread.h>
  20. #include <linux/sched.h>
  21. #include <linux/fs.h>
  22. #include <linux/miscdevice.h>
  23. #include <linux/vmalloc.h>
  24. #include <linux/slab.h>
  25. #include <linux/device.h>
  26. #define TEST_START_NUM_THREADS 50
  27. #define TEST_START_DRIVER "test_module"
  28. #define TEST_START_TEST_CASE TEST_KMOD_DRIVER
  29. static bool force_init_test = false;
  30. module_param(force_init_test, bool_enable_only, 0444);
  31. MODULE_PARM_DESC(force_init_test,
  32. "Force kicking a test immediately after driver loads");
  33. static char *start_driver;
  34. module_param(start_driver, charp, 0444);
  35. MODULE_PARM_DESC(start_driver,
  36. "Module/driver to use for the testing after driver loads");
  37. static char *start_test_fs;
  38. module_param(start_test_fs, charp, 0444);
  39. MODULE_PARM_DESC(start_test_fs,
  40. "File system to use for the testing after driver loads");
  41. /*
  42. * For device allocation / registration
  43. */
  44. static DEFINE_MUTEX(reg_dev_mutex);
  45. static LIST_HEAD(reg_test_devs);
  46. /*
  47. * num_test_devs actually represents the *next* ID of the next
  48. * device we will allow to create.
  49. */
  50. static int num_test_devs;
  51. /**
  52. * enum kmod_test_case - linker table test case
  53. * @TEST_KMOD_DRIVER: stress tests request_module()
  54. * @TEST_KMOD_FS_TYPE: stress tests get_fs_type()
  55. *
  56. * If you add a test case, please be sure to review if you need to set
  57. * @need_mod_put for your tests case.
  58. */
  59. enum kmod_test_case {
  60. /* private: */
  61. __TEST_KMOD_INVALID = 0,
  62. /* public: */
  63. TEST_KMOD_DRIVER,
  64. TEST_KMOD_FS_TYPE,
  65. /* private: */
  66. __TEST_KMOD_MAX,
  67. };
  68. struct test_config {
  69. char *test_driver;
  70. char *test_fs;
  71. unsigned int num_threads;
  72. enum kmod_test_case test_case;
  73. int test_result;
  74. };
  75. struct kmod_test_device;
  76. /**
  77. * struct kmod_test_device_info - thread info
  78. *
  79. * @ret_sync: return value if request_module() is used, sync request for
  80. * @TEST_KMOD_DRIVER
  81. * @fs_sync: return value of get_fs_type() for @TEST_KMOD_FS_TYPE
  82. * @task_sync: kthread's task_struct or %NULL if not running
  83. * @thread_idx: thread ID
  84. * @test_dev: test device test is being performed under
  85. * @need_mod_put: Some tests (get_fs_type() is one) requires putting the module
  86. * (module_put(fs_sync->owner)) when done, otherwise you will not be able
  87. * to unload the respective modules and re-test. We use this to keep
  88. * accounting of when we need this and to help out in case we need to
  89. * error out and deal with module_put() on error.
  90. */
  91. struct kmod_test_device_info {
  92. int ret_sync;
  93. struct file_system_type *fs_sync;
  94. struct task_struct *task_sync;
  95. unsigned int thread_idx;
  96. struct kmod_test_device *test_dev;
  97. bool need_mod_put;
  98. };
  99. /**
  100. * struct kmod_test_device - test device to help test kmod
  101. *
  102. * @dev_idx: unique ID for test device
  103. * @config: configuration for the test
  104. * @misc_dev: we use a misc device under the hood
  105. * @dev: pointer to misc_dev's own struct device
  106. * @config_mutex: protects configuration of test
  107. * @trigger_mutex: the test trigger can only be fired once at a time
  108. * @thread_mutex: protects @done count, and the @info per each thread
  109. * @done: number of threads which have completed or failed
  110. * @test_is_oom: when we run out of memory, use this to halt moving forward
  111. * @kthreads_done: completion used to signal when all work is done
  112. * @list: needed to be part of the reg_test_devs
  113. * @info: array of info for each thread
  114. */
  115. struct kmod_test_device {
  116. int dev_idx;
  117. struct test_config config;
  118. struct miscdevice misc_dev;
  119. struct device *dev;
  120. struct mutex config_mutex;
  121. struct mutex trigger_mutex;
  122. struct mutex thread_mutex;
  123. unsigned int done;
  124. bool test_is_oom;
  125. struct completion kthreads_done;
  126. struct list_head list;
  127. struct kmod_test_device_info *info;
  128. };
  129. static const char *test_case_str(enum kmod_test_case test_case)
  130. {
  131. switch (test_case) {
  132. case TEST_KMOD_DRIVER:
  133. return "TEST_KMOD_DRIVER";
  134. case TEST_KMOD_FS_TYPE:
  135. return "TEST_KMOD_FS_TYPE";
  136. default:
  137. return "invalid";
  138. }
  139. }
  140. static struct miscdevice *dev_to_misc_dev(struct device *dev)
  141. {
  142. return dev_get_drvdata(dev);
  143. }
  144. static struct kmod_test_device *misc_dev_to_test_dev(struct miscdevice *misc_dev)
  145. {
  146. return container_of(misc_dev, struct kmod_test_device, misc_dev);
  147. }
  148. static struct kmod_test_device *dev_to_test_dev(struct device *dev)
  149. {
  150. struct miscdevice *misc_dev;
  151. misc_dev = dev_to_misc_dev(dev);
  152. return misc_dev_to_test_dev(misc_dev);
  153. }
  154. /* Must run with thread_mutex held */
  155. static void kmod_test_done_check(struct kmod_test_device *test_dev,
  156. unsigned int idx)
  157. {
  158. struct test_config *config = &test_dev->config;
  159. test_dev->done++;
  160. dev_dbg(test_dev->dev, "Done thread count: %u\n", test_dev->done);
  161. if (test_dev->done == config->num_threads) {
  162. dev_info(test_dev->dev, "Done: %u threads have all run now\n",
  163. test_dev->done);
  164. dev_info(test_dev->dev, "Last thread to run: %u\n", idx);
  165. complete(&test_dev->kthreads_done);
  166. }
  167. }
  168. static void test_kmod_put_module(struct kmod_test_device_info *info)
  169. {
  170. struct kmod_test_device *test_dev = info->test_dev;
  171. struct test_config *config = &test_dev->config;
  172. if (!info->need_mod_put)
  173. return;
  174. switch (config->test_case) {
  175. case TEST_KMOD_DRIVER:
  176. break;
  177. case TEST_KMOD_FS_TYPE:
  178. if (info->fs_sync && info->fs_sync->owner)
  179. module_put(info->fs_sync->owner);
  180. break;
  181. default:
  182. BUG();
  183. }
  184. info->need_mod_put = true;
  185. }
  186. static int run_request(void *data)
  187. {
  188. struct kmod_test_device_info *info = data;
  189. struct kmod_test_device *test_dev = info->test_dev;
  190. struct test_config *config = &test_dev->config;
  191. switch (config->test_case) {
  192. case TEST_KMOD_DRIVER:
  193. info->ret_sync = request_module("%s", config->test_driver);
  194. break;
  195. case TEST_KMOD_FS_TYPE:
  196. info->fs_sync = get_fs_type(config->test_fs);
  197. info->need_mod_put = true;
  198. break;
  199. default:
  200. /* __trigger_config_run() already checked for test sanity */
  201. BUG();
  202. return -EINVAL;
  203. }
  204. dev_dbg(test_dev->dev, "Ran thread %u\n", info->thread_idx);
  205. test_kmod_put_module(info);
  206. mutex_lock(&test_dev->thread_mutex);
  207. info->task_sync = NULL;
  208. kmod_test_done_check(test_dev, info->thread_idx);
  209. mutex_unlock(&test_dev->thread_mutex);
  210. return 0;
  211. }
  212. static int tally_work_test(struct kmod_test_device_info *info)
  213. {
  214. struct kmod_test_device *test_dev = info->test_dev;
  215. struct test_config *config = &test_dev->config;
  216. int err_ret = 0;
  217. switch (config->test_case) {
  218. case TEST_KMOD_DRIVER:
  219. /*
  220. * Only capture errors, if one is found that's
  221. * enough, for now.
  222. */
  223. if (info->ret_sync != 0)
  224. err_ret = info->ret_sync;
  225. dev_info(test_dev->dev,
  226. "Sync thread %d return status: %d\n",
  227. info->thread_idx, info->ret_sync);
  228. break;
  229. case TEST_KMOD_FS_TYPE:
  230. /* For now we make this simple */
  231. if (!info->fs_sync)
  232. err_ret = -EINVAL;
  233. dev_info(test_dev->dev, "Sync thread %u fs: %s\n",
  234. info->thread_idx, info->fs_sync ? config->test_fs :
  235. "NULL");
  236. break;
  237. default:
  238. BUG();
  239. }
  240. return err_ret;
  241. }
  242. /*
  243. * XXX: add result option to display if all errors did not match.
  244. * For now we just keep any error code if one was found.
  245. *
  246. * If this ran it means *all* tasks were created fine and we
  247. * are now just collecting results.
  248. *
  249. * Only propagate errors, do not override with a subsequent success case.
  250. */
  251. static void tally_up_work(struct kmod_test_device *test_dev)
  252. {
  253. struct test_config *config = &test_dev->config;
  254. struct kmod_test_device_info *info;
  255. unsigned int idx;
  256. int err_ret = 0;
  257. int ret = 0;
  258. mutex_lock(&test_dev->thread_mutex);
  259. dev_info(test_dev->dev, "Results:\n");
  260. for (idx=0; idx < config->num_threads; idx++) {
  261. info = &test_dev->info[idx];
  262. ret = tally_work_test(info);
  263. if (ret)
  264. err_ret = ret;
  265. }
  266. /*
  267. * Note: request_module() returns 256 for a module not found even
  268. * though modprobe itself returns 1.
  269. */
  270. config->test_result = err_ret;
  271. mutex_unlock(&test_dev->thread_mutex);
  272. }
  273. static int try_one_request(struct kmod_test_device *test_dev, unsigned int idx)
  274. {
  275. struct kmod_test_device_info *info = &test_dev->info[idx];
  276. int fail_ret = -ENOMEM;
  277. mutex_lock(&test_dev->thread_mutex);
  278. info->thread_idx = idx;
  279. info->test_dev = test_dev;
  280. info->task_sync = kthread_run(run_request, info, "%s-%u",
  281. KBUILD_MODNAME, idx);
  282. if (!info->task_sync || IS_ERR(info->task_sync)) {
  283. test_dev->test_is_oom = true;
  284. dev_err(test_dev->dev, "Setting up thread %u failed\n", idx);
  285. info->task_sync = NULL;
  286. goto err_out;
  287. } else
  288. dev_dbg(test_dev->dev, "Kicked off thread %u\n", idx);
  289. mutex_unlock(&test_dev->thread_mutex);
  290. return 0;
  291. err_out:
  292. info->ret_sync = fail_ret;
  293. mutex_unlock(&test_dev->thread_mutex);
  294. return fail_ret;
  295. }
  296. static void test_dev_kmod_stop_tests(struct kmod_test_device *test_dev)
  297. {
  298. struct test_config *config = &test_dev->config;
  299. struct kmod_test_device_info *info;
  300. unsigned int i;
  301. dev_info(test_dev->dev, "Ending request_module() tests\n");
  302. mutex_lock(&test_dev->thread_mutex);
  303. for (i=0; i < config->num_threads; i++) {
  304. info = &test_dev->info[i];
  305. if (info->task_sync && !IS_ERR(info->task_sync)) {
  306. dev_info(test_dev->dev,
  307. "Stopping still-running thread %i\n", i);
  308. kthread_stop(info->task_sync);
  309. }
  310. /*
  311. * info->task_sync is well protected, it can only be
  312. * NULL or a pointer to a struct. If its NULL we either
  313. * never ran, or we did and we completed the work. Completed
  314. * tasks *always* put the module for us. This is a sanity
  315. * check -- just in case.
  316. */
  317. if (info->task_sync && info->need_mod_put)
  318. test_kmod_put_module(info);
  319. }
  320. mutex_unlock(&test_dev->thread_mutex);
  321. }
  322. /*
  323. * Only wait *iff* we did not run into any errors during all of our thread
  324. * set up. If run into any issues we stop threads and just bail out with
  325. * an error to the trigger. This also means we don't need any tally work
  326. * for any threads which fail.
  327. */
  328. static int try_requests(struct kmod_test_device *test_dev)
  329. {
  330. struct test_config *config = &test_dev->config;
  331. unsigned int idx;
  332. int ret;
  333. bool any_error = false;
  334. for (idx=0; idx < config->num_threads; idx++) {
  335. if (test_dev->test_is_oom) {
  336. any_error = true;
  337. break;
  338. }
  339. ret = try_one_request(test_dev, idx);
  340. if (ret) {
  341. any_error = true;
  342. break;
  343. }
  344. }
  345. if (!any_error) {
  346. test_dev->test_is_oom = false;
  347. dev_info(test_dev->dev,
  348. "No errors were found while initializing threads\n");
  349. wait_for_completion(&test_dev->kthreads_done);
  350. tally_up_work(test_dev);
  351. } else {
  352. test_dev->test_is_oom = true;
  353. dev_info(test_dev->dev,
  354. "At least one thread failed to start, stop all work\n");
  355. test_dev_kmod_stop_tests(test_dev);
  356. return -ENOMEM;
  357. }
  358. return 0;
  359. }
  360. static int run_test_driver(struct kmod_test_device *test_dev)
  361. {
  362. struct test_config *config = &test_dev->config;
  363. dev_info(test_dev->dev, "Test case: %s (%u)\n",
  364. test_case_str(config->test_case),
  365. config->test_case);
  366. dev_info(test_dev->dev, "Test driver to load: %s\n",
  367. config->test_driver);
  368. dev_info(test_dev->dev, "Number of threads to run: %u\n",
  369. config->num_threads);
  370. dev_info(test_dev->dev, "Thread IDs will range from 0 - %u\n",
  371. config->num_threads - 1);
  372. return try_requests(test_dev);
  373. }
  374. static int run_test_fs_type(struct kmod_test_device *test_dev)
  375. {
  376. struct test_config *config = &test_dev->config;
  377. dev_info(test_dev->dev, "Test case: %s (%u)\n",
  378. test_case_str(config->test_case),
  379. config->test_case);
  380. dev_info(test_dev->dev, "Test filesystem to load: %s\n",
  381. config->test_fs);
  382. dev_info(test_dev->dev, "Number of threads to run: %u\n",
  383. config->num_threads);
  384. dev_info(test_dev->dev, "Thread IDs will range from 0 - %u\n",
  385. config->num_threads - 1);
  386. return try_requests(test_dev);
  387. }
  388. static ssize_t config_show(struct device *dev,
  389. struct device_attribute *attr,
  390. char *buf)
  391. {
  392. struct kmod_test_device *test_dev = dev_to_test_dev(dev);
  393. struct test_config *config = &test_dev->config;
  394. int len = 0;
  395. mutex_lock(&test_dev->config_mutex);
  396. len += snprintf(buf, PAGE_SIZE,
  397. "Custom trigger configuration for: %s\n",
  398. dev_name(dev));
  399. len += snprintf(buf+len, PAGE_SIZE - len,
  400. "Number of threads:\t%u\n",
  401. config->num_threads);
  402. len += snprintf(buf+len, PAGE_SIZE - len,
  403. "Test_case:\t%s (%u)\n",
  404. test_case_str(config->test_case),
  405. config->test_case);
  406. if (config->test_driver)
  407. len += snprintf(buf+len, PAGE_SIZE - len,
  408. "driver:\t%s\n",
  409. config->test_driver);
  410. else
  411. len += snprintf(buf+len, PAGE_SIZE - len,
  412. "driver:\tEMPTY\n");
  413. if (config->test_fs)
  414. len += snprintf(buf+len, PAGE_SIZE - len,
  415. "fs:\t%s\n",
  416. config->test_fs);
  417. else
  418. len += snprintf(buf+len, PAGE_SIZE - len,
  419. "fs:\tEMPTY\n");
  420. mutex_unlock(&test_dev->config_mutex);
  421. return len;
  422. }
  423. static DEVICE_ATTR_RO(config);
  424. /*
  425. * This ensures we don't allow kicking threads through if our configuration
  426. * is faulty.
  427. */
  428. static int __trigger_config_run(struct kmod_test_device *test_dev)
  429. {
  430. struct test_config *config = &test_dev->config;
  431. test_dev->done = 0;
  432. switch (config->test_case) {
  433. case TEST_KMOD_DRIVER:
  434. return run_test_driver(test_dev);
  435. case TEST_KMOD_FS_TYPE:
  436. if (!config->test_fs) {
  437. dev_warn(test_dev->dev,
  438. "No fs type specified, can't run the test\n");
  439. return -EINVAL;
  440. }
  441. return run_test_fs_type(test_dev);
  442. default:
  443. dev_warn(test_dev->dev,
  444. "Invalid test case requested: %u\n",
  445. config->test_case);
  446. return -EINVAL;
  447. }
  448. }
  449. static int trigger_config_run(struct kmod_test_device *test_dev)
  450. {
  451. struct test_config *config = &test_dev->config;
  452. int ret;
  453. mutex_lock(&test_dev->trigger_mutex);
  454. mutex_lock(&test_dev->config_mutex);
  455. ret = __trigger_config_run(test_dev);
  456. if (ret < 0)
  457. goto out;
  458. dev_info(test_dev->dev, "General test result: %d\n",
  459. config->test_result);
  460. /*
  461. * We must return 0 after a trigger even unless something went
  462. * wrong with the setup of the test. If the test setup went fine
  463. * then userspace must just check the result of config->test_result.
  464. * One issue with relying on the return from a call in the kernel
  465. * is if the kernel returns a positive value using this trigger
  466. * will not return the value to userspace, it would be lost.
  467. *
  468. * By not relying on capturing the return value of tests we are using
  469. * through the trigger it also us to run tests with set -e and only
  470. * fail when something went wrong with the driver upon trigger
  471. * requests.
  472. */
  473. ret = 0;
  474. out:
  475. mutex_unlock(&test_dev->config_mutex);
  476. mutex_unlock(&test_dev->trigger_mutex);
  477. return ret;
  478. }
  479. static ssize_t
  480. trigger_config_store(struct device *dev,
  481. struct device_attribute *attr,
  482. const char *buf, size_t count)
  483. {
  484. struct kmod_test_device *test_dev = dev_to_test_dev(dev);
  485. int ret;
  486. if (test_dev->test_is_oom)
  487. return -ENOMEM;
  488. /* For all intents and purposes we don't care what userspace
  489. * sent this trigger, we care only that we were triggered.
  490. * We treat the return value only for caputuring issues with
  491. * the test setup. At this point all the test variables should
  492. * have been allocated so typically this should never fail.
  493. */
  494. ret = trigger_config_run(test_dev);
  495. if (unlikely(ret < 0))
  496. goto out;
  497. /*
  498. * Note: any return > 0 will be treated as success
  499. * and the error value will not be available to userspace.
  500. * Do not rely on trying to send to userspace a test value
  501. * return value as positive return errors will be lost.
  502. */
  503. if (WARN_ON(ret > 0))
  504. return -EINVAL;
  505. ret = count;
  506. out:
  507. return ret;
  508. }
  509. static DEVICE_ATTR_WO(trigger_config);
  510. /*
  511. * XXX: move to kstrncpy() once merged.
  512. *
  513. * Users should use kfree_const() when freeing these.
  514. */
  515. static int __kstrncpy(char **dst, const char *name, size_t count, gfp_t gfp)
  516. {
  517. *dst = kstrndup(name, count, gfp);
  518. if (!*dst)
  519. return -ENOSPC;
  520. return count;
  521. }
  522. static int config_copy_test_driver_name(struct test_config *config,
  523. const char *name,
  524. size_t count)
  525. {
  526. return __kstrncpy(&config->test_driver, name, count, GFP_KERNEL);
  527. }
  528. static int config_copy_test_fs(struct test_config *config, const char *name,
  529. size_t count)
  530. {
  531. return __kstrncpy(&config->test_fs, name, count, GFP_KERNEL);
  532. }
  533. static void __kmod_config_free(struct test_config *config)
  534. {
  535. if (!config)
  536. return;
  537. kfree_const(config->test_driver);
  538. config->test_driver = NULL;
  539. kfree_const(config->test_fs);
  540. config->test_fs = NULL;
  541. }
  542. static void kmod_config_free(struct kmod_test_device *test_dev)
  543. {
  544. struct test_config *config;
  545. if (!test_dev)
  546. return;
  547. config = &test_dev->config;
  548. mutex_lock(&test_dev->config_mutex);
  549. __kmod_config_free(config);
  550. mutex_unlock(&test_dev->config_mutex);
  551. }
  552. static ssize_t config_test_driver_store(struct device *dev,
  553. struct device_attribute *attr,
  554. const char *buf, size_t count)
  555. {
  556. struct kmod_test_device *test_dev = dev_to_test_dev(dev);
  557. struct test_config *config = &test_dev->config;
  558. int copied;
  559. mutex_lock(&test_dev->config_mutex);
  560. kfree_const(config->test_driver);
  561. config->test_driver = NULL;
  562. copied = config_copy_test_driver_name(config, buf, count);
  563. mutex_unlock(&test_dev->config_mutex);
  564. return copied;
  565. }
  566. /*
  567. * As per sysfs_kf_seq_show() the buf is max PAGE_SIZE.
  568. */
  569. static ssize_t config_test_show_str(struct mutex *config_mutex,
  570. char *dst,
  571. char *src)
  572. {
  573. int len;
  574. mutex_lock(config_mutex);
  575. len = snprintf(dst, PAGE_SIZE, "%s\n", src);
  576. mutex_unlock(config_mutex);
  577. return len;
  578. }
  579. static ssize_t config_test_driver_show(struct device *dev,
  580. struct device_attribute *attr,
  581. char *buf)
  582. {
  583. struct kmod_test_device *test_dev = dev_to_test_dev(dev);
  584. struct test_config *config = &test_dev->config;
  585. return config_test_show_str(&test_dev->config_mutex, buf,
  586. config->test_driver);
  587. }
  588. static DEVICE_ATTR_RW(config_test_driver);
  589. static ssize_t config_test_fs_store(struct device *dev,
  590. struct device_attribute *attr,
  591. const char *buf, size_t count)
  592. {
  593. struct kmod_test_device *test_dev = dev_to_test_dev(dev);
  594. struct test_config *config = &test_dev->config;
  595. int copied;
  596. mutex_lock(&test_dev->config_mutex);
  597. kfree_const(config->test_fs);
  598. config->test_fs = NULL;
  599. copied = config_copy_test_fs(config, buf, count);
  600. mutex_unlock(&test_dev->config_mutex);
  601. return copied;
  602. }
  603. static ssize_t config_test_fs_show(struct device *dev,
  604. struct device_attribute *attr,
  605. char *buf)
  606. {
  607. struct kmod_test_device *test_dev = dev_to_test_dev(dev);
  608. struct test_config *config = &test_dev->config;
  609. return config_test_show_str(&test_dev->config_mutex, buf,
  610. config->test_fs);
  611. }
  612. static DEVICE_ATTR_RW(config_test_fs);
  613. static int trigger_config_run_type(struct kmod_test_device *test_dev,
  614. enum kmod_test_case test_case)
  615. {
  616. struct test_config *config = &test_dev->config;
  617. mutex_lock(&test_dev->config_mutex);
  618. switch (test_case) {
  619. case TEST_KMOD_DRIVER:
  620. break;
  621. case TEST_KMOD_FS_TYPE:
  622. if (!config->test_fs) {
  623. mutex_unlock(&test_dev->config_mutex);
  624. return 0;
  625. }
  626. break;
  627. default:
  628. mutex_unlock(&test_dev->config_mutex);
  629. return -EINVAL;
  630. }
  631. config->test_case = test_case;
  632. mutex_unlock(&test_dev->config_mutex);
  633. test_dev->test_is_oom = false;
  634. return trigger_config_run(test_dev);
  635. }
  636. static void free_test_dev_info(struct kmod_test_device *test_dev)
  637. {
  638. vfree(test_dev->info);
  639. test_dev->info = NULL;
  640. }
  641. static int kmod_config_sync_info(struct kmod_test_device *test_dev)
  642. {
  643. struct test_config *config = &test_dev->config;
  644. free_test_dev_info(test_dev);
  645. test_dev->info =
  646. vzalloc(array_size(sizeof(struct kmod_test_device_info),
  647. config->num_threads));
  648. if (!test_dev->info)
  649. return -ENOMEM;
  650. return 0;
  651. }
  652. /*
  653. * Old kernels may not have this, if you want to port this code to
  654. * test it on older kernels.
  655. */
  656. #ifdef get_kmod_umh_limit
  657. static unsigned int kmod_init_test_thread_limit(void)
  658. {
  659. return get_kmod_umh_limit();
  660. }
  661. #else
  662. static unsigned int kmod_init_test_thread_limit(void)
  663. {
  664. return TEST_START_NUM_THREADS;
  665. }
  666. #endif
  667. static int __kmod_config_init(struct kmod_test_device *test_dev)
  668. {
  669. struct test_config *config = &test_dev->config;
  670. const char *test_start_driver = start_driver ? start_driver :
  671. TEST_START_DRIVER;
  672. int ret = -ENOMEM, copied;
  673. __kmod_config_free(config);
  674. copied = config_copy_test_driver_name(config, test_start_driver,
  675. strlen(test_start_driver));
  676. if (copied != strlen(test_start_driver))
  677. goto err_out;
  678. if (start_test_fs) {
  679. copied = config_copy_test_fs(config, start_test_fs,
  680. strlen(start_test_fs));
  681. if (copied != strlen(start_test_fs))
  682. goto err_out;
  683. }
  684. config->num_threads = kmod_init_test_thread_limit();
  685. config->test_result = 0;
  686. config->test_case = TEST_START_TEST_CASE;
  687. ret = kmod_config_sync_info(test_dev);
  688. if (ret)
  689. goto err_out;
  690. test_dev->test_is_oom = false;
  691. return 0;
  692. err_out:
  693. test_dev->test_is_oom = true;
  694. WARN_ON(test_dev->test_is_oom);
  695. __kmod_config_free(config);
  696. return ret;
  697. }
  698. static ssize_t reset_store(struct device *dev,
  699. struct device_attribute *attr,
  700. const char *buf, size_t count)
  701. {
  702. struct kmod_test_device *test_dev = dev_to_test_dev(dev);
  703. int ret;
  704. mutex_lock(&test_dev->trigger_mutex);
  705. mutex_lock(&test_dev->config_mutex);
  706. ret = __kmod_config_init(test_dev);
  707. if (ret < 0) {
  708. ret = -ENOMEM;
  709. dev_err(dev, "could not alloc settings for config trigger: %d\n",
  710. ret);
  711. goto out;
  712. }
  713. dev_info(dev, "reset\n");
  714. ret = count;
  715. out:
  716. mutex_unlock(&test_dev->config_mutex);
  717. mutex_unlock(&test_dev->trigger_mutex);
  718. return ret;
  719. }
  720. static DEVICE_ATTR_WO(reset);
  721. static int test_dev_config_update_uint_sync(struct kmod_test_device *test_dev,
  722. const char *buf, size_t size,
  723. unsigned int *config,
  724. int (*test_sync)(struct kmod_test_device *test_dev))
  725. {
  726. int ret;
  727. unsigned int val;
  728. unsigned int old_val;
  729. ret = kstrtouint(buf, 10, &val);
  730. if (ret)
  731. return ret;
  732. mutex_lock(&test_dev->config_mutex);
  733. old_val = *config;
  734. *(unsigned int *)config = val;
  735. ret = test_sync(test_dev);
  736. if (ret) {
  737. *(unsigned int *)config = old_val;
  738. ret = test_sync(test_dev);
  739. WARN_ON(ret);
  740. mutex_unlock(&test_dev->config_mutex);
  741. return -EINVAL;
  742. }
  743. mutex_unlock(&test_dev->config_mutex);
  744. /* Always return full write size even if we didn't consume all */
  745. return size;
  746. }
  747. static int test_dev_config_update_uint_range(struct kmod_test_device *test_dev,
  748. const char *buf, size_t size,
  749. unsigned int *config,
  750. unsigned int min,
  751. unsigned int max)
  752. {
  753. unsigned int val;
  754. int ret;
  755. ret = kstrtouint(buf, 10, &val);
  756. if (ret)
  757. return ret;
  758. if (val < min || val > max)
  759. return -EINVAL;
  760. mutex_lock(&test_dev->config_mutex);
  761. *config = val;
  762. mutex_unlock(&test_dev->config_mutex);
  763. /* Always return full write size even if we didn't consume all */
  764. return size;
  765. }
  766. static int test_dev_config_update_int(struct kmod_test_device *test_dev,
  767. const char *buf, size_t size,
  768. int *config)
  769. {
  770. int val;
  771. int ret;
  772. ret = kstrtoint(buf, 10, &val);
  773. if (ret)
  774. return ret;
  775. mutex_lock(&test_dev->config_mutex);
  776. *config = val;
  777. mutex_unlock(&test_dev->config_mutex);
  778. /* Always return full write size even if we didn't consume all */
  779. return size;
  780. }
  781. static ssize_t test_dev_config_show_int(struct kmod_test_device *test_dev,
  782. char *buf,
  783. int config)
  784. {
  785. int val;
  786. mutex_lock(&test_dev->config_mutex);
  787. val = config;
  788. mutex_unlock(&test_dev->config_mutex);
  789. return snprintf(buf, PAGE_SIZE, "%d\n", val);
  790. }
  791. static ssize_t test_dev_config_show_uint(struct kmod_test_device *test_dev,
  792. char *buf,
  793. unsigned int config)
  794. {
  795. unsigned int val;
  796. mutex_lock(&test_dev->config_mutex);
  797. val = config;
  798. mutex_unlock(&test_dev->config_mutex);
  799. return snprintf(buf, PAGE_SIZE, "%u\n", val);
  800. }
  801. static ssize_t test_result_store(struct device *dev,
  802. struct device_attribute *attr,
  803. const char *buf, size_t count)
  804. {
  805. struct kmod_test_device *test_dev = dev_to_test_dev(dev);
  806. struct test_config *config = &test_dev->config;
  807. return test_dev_config_update_int(test_dev, buf, count,
  808. &config->test_result);
  809. }
  810. static ssize_t config_num_threads_store(struct device *dev,
  811. struct device_attribute *attr,
  812. const char *buf, size_t count)
  813. {
  814. struct kmod_test_device *test_dev = dev_to_test_dev(dev);
  815. struct test_config *config = &test_dev->config;
  816. return test_dev_config_update_uint_sync(test_dev, buf, count,
  817. &config->num_threads,
  818. kmod_config_sync_info);
  819. }
  820. static ssize_t config_num_threads_show(struct device *dev,
  821. struct device_attribute *attr,
  822. char *buf)
  823. {
  824. struct kmod_test_device *test_dev = dev_to_test_dev(dev);
  825. struct test_config *config = &test_dev->config;
  826. return test_dev_config_show_int(test_dev, buf, config->num_threads);
  827. }
  828. static DEVICE_ATTR_RW(config_num_threads);
  829. static ssize_t config_test_case_store(struct device *dev,
  830. struct device_attribute *attr,
  831. const char *buf, size_t count)
  832. {
  833. struct kmod_test_device *test_dev = dev_to_test_dev(dev);
  834. struct test_config *config = &test_dev->config;
  835. return test_dev_config_update_uint_range(test_dev, buf, count,
  836. &config->test_case,
  837. __TEST_KMOD_INVALID + 1,
  838. __TEST_KMOD_MAX - 1);
  839. }
  840. static ssize_t config_test_case_show(struct device *dev,
  841. struct device_attribute *attr,
  842. char *buf)
  843. {
  844. struct kmod_test_device *test_dev = dev_to_test_dev(dev);
  845. struct test_config *config = &test_dev->config;
  846. return test_dev_config_show_uint(test_dev, buf, config->test_case);
  847. }
  848. static DEVICE_ATTR_RW(config_test_case);
  849. static ssize_t test_result_show(struct device *dev,
  850. struct device_attribute *attr,
  851. char *buf)
  852. {
  853. struct kmod_test_device *test_dev = dev_to_test_dev(dev);
  854. struct test_config *config = &test_dev->config;
  855. return test_dev_config_show_int(test_dev, buf, config->test_result);
  856. }
  857. static DEVICE_ATTR_RW(test_result);
  858. #define TEST_KMOD_DEV_ATTR(name) &dev_attr_##name.attr
  859. static struct attribute *test_dev_attrs[] = {
  860. TEST_KMOD_DEV_ATTR(trigger_config),
  861. TEST_KMOD_DEV_ATTR(config),
  862. TEST_KMOD_DEV_ATTR(reset),
  863. TEST_KMOD_DEV_ATTR(config_test_driver),
  864. TEST_KMOD_DEV_ATTR(config_test_fs),
  865. TEST_KMOD_DEV_ATTR(config_num_threads),
  866. TEST_KMOD_DEV_ATTR(config_test_case),
  867. TEST_KMOD_DEV_ATTR(test_result),
  868. NULL,
  869. };
  870. ATTRIBUTE_GROUPS(test_dev);
  871. static int kmod_config_init(struct kmod_test_device *test_dev)
  872. {
  873. int ret;
  874. mutex_lock(&test_dev->config_mutex);
  875. ret = __kmod_config_init(test_dev);
  876. mutex_unlock(&test_dev->config_mutex);
  877. return ret;
  878. }
  879. static struct kmod_test_device *alloc_test_dev_kmod(int idx)
  880. {
  881. int ret;
  882. struct kmod_test_device *test_dev;
  883. struct miscdevice *misc_dev;
  884. test_dev = vzalloc(sizeof(struct kmod_test_device));
  885. if (!test_dev)
  886. goto err_out;
  887. mutex_init(&test_dev->config_mutex);
  888. mutex_init(&test_dev->trigger_mutex);
  889. mutex_init(&test_dev->thread_mutex);
  890. init_completion(&test_dev->kthreads_done);
  891. ret = kmod_config_init(test_dev);
  892. if (ret < 0) {
  893. pr_err("Cannot alloc kmod_config_init()\n");
  894. goto err_out_free;
  895. }
  896. test_dev->dev_idx = idx;
  897. misc_dev = &test_dev->misc_dev;
  898. misc_dev->minor = MISC_DYNAMIC_MINOR;
  899. misc_dev->name = kasprintf(GFP_KERNEL, "test_kmod%d", idx);
  900. if (!misc_dev->name) {
  901. pr_err("Cannot alloc misc_dev->name\n");
  902. goto err_out_free_config;
  903. }
  904. misc_dev->groups = test_dev_groups;
  905. return test_dev;
  906. err_out_free_config:
  907. free_test_dev_info(test_dev);
  908. kmod_config_free(test_dev);
  909. err_out_free:
  910. vfree(test_dev);
  911. test_dev = NULL;
  912. err_out:
  913. return NULL;
  914. }
  915. static void free_test_dev_kmod(struct kmod_test_device *test_dev)
  916. {
  917. if (test_dev) {
  918. kfree_const(test_dev->misc_dev.name);
  919. test_dev->misc_dev.name = NULL;
  920. free_test_dev_info(test_dev);
  921. kmod_config_free(test_dev);
  922. vfree(test_dev);
  923. test_dev = NULL;
  924. }
  925. }
  926. static struct kmod_test_device *register_test_dev_kmod(void)
  927. {
  928. struct kmod_test_device *test_dev = NULL;
  929. int ret;
  930. mutex_lock(&reg_dev_mutex);
  931. /* int should suffice for number of devices, test for wrap */
  932. if (num_test_devs + 1 == INT_MAX) {
  933. pr_err("reached limit of number of test devices\n");
  934. goto out;
  935. }
  936. test_dev = alloc_test_dev_kmod(num_test_devs);
  937. if (!test_dev)
  938. goto out;
  939. ret = misc_register(&test_dev->misc_dev);
  940. if (ret) {
  941. pr_err("could not register misc device: %d\n", ret);
  942. free_test_dev_kmod(test_dev);
  943. test_dev = NULL;
  944. goto out;
  945. }
  946. test_dev->dev = test_dev->misc_dev.this_device;
  947. list_add_tail(&test_dev->list, &reg_test_devs);
  948. dev_info(test_dev->dev, "interface ready\n");
  949. num_test_devs++;
  950. out:
  951. mutex_unlock(&reg_dev_mutex);
  952. return test_dev;
  953. }
  954. static int __init test_kmod_init(void)
  955. {
  956. struct kmod_test_device *test_dev;
  957. int ret;
  958. test_dev = register_test_dev_kmod();
  959. if (!test_dev) {
  960. pr_err("Cannot add first test kmod device\n");
  961. return -ENODEV;
  962. }
  963. /*
  964. * With some work we might be able to gracefully enable
  965. * testing with this driver built-in, for now this seems
  966. * rather risky. For those willing to try have at it,
  967. * and enable the below. Good luck! If that works, try
  968. * lowering the init level for more fun.
  969. */
  970. if (force_init_test) {
  971. ret = trigger_config_run_type(test_dev, TEST_KMOD_DRIVER);
  972. if (WARN_ON(ret))
  973. return ret;
  974. ret = trigger_config_run_type(test_dev, TEST_KMOD_FS_TYPE);
  975. if (WARN_ON(ret))
  976. return ret;
  977. }
  978. return 0;
  979. }
  980. late_initcall(test_kmod_init);
  981. static
  982. void unregister_test_dev_kmod(struct kmod_test_device *test_dev)
  983. {
  984. mutex_lock(&test_dev->trigger_mutex);
  985. mutex_lock(&test_dev->config_mutex);
  986. test_dev_kmod_stop_tests(test_dev);
  987. dev_info(test_dev->dev, "removing interface\n");
  988. misc_deregister(&test_dev->misc_dev);
  989. mutex_unlock(&test_dev->config_mutex);
  990. mutex_unlock(&test_dev->trigger_mutex);
  991. free_test_dev_kmod(test_dev);
  992. }
  993. static void __exit test_kmod_exit(void)
  994. {
  995. struct kmod_test_device *test_dev, *tmp;
  996. mutex_lock(&reg_dev_mutex);
  997. list_for_each_entry_safe(test_dev, tmp, &reg_test_devs, list) {
  998. list_del(&test_dev->list);
  999. unregister_test_dev_kmod(test_dev);
  1000. }
  1001. mutex_unlock(&reg_dev_mutex);
  1002. }
  1003. module_exit(test_kmod_exit);
  1004. MODULE_AUTHOR("Luis R. Rodriguez <mcgrof@kernel.org>");
  1005. MODULE_DESCRIPTION("kmod stress test driver");
  1006. MODULE_LICENSE("GPL");