test_kmod.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246
  1. /*
  2. * kmod stress test driver
  3. *
  4. * Copyright (C) 2017 Luis R. Rodriguez <mcgrof@kernel.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 2 of the License, or at your option any
  9. * later version; or, when distributed separately from the Linux kernel or
  10. * when incorporated into other software packages, subject to the following
  11. * license:
  12. *
  13. * This program is free software; you can redistribute it and/or modify it
  14. * under the terms of copyleft-next (version 0.3.1 or later) as published
  15. * at http://copyleft-next.org/.
  16. */
  17. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  18. /*
  19. * This driver provides an interface to trigger and test the kernel's
  20. * module loader through a series of configurations and a few triggers.
  21. * To test this driver use the following script as root:
  22. *
  23. * tools/testing/selftests/kmod/kmod.sh --help
  24. */
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/kmod.h>
  28. #include <linux/printk.h>
  29. #include <linux/kthread.h>
  30. #include <linux/sched.h>
  31. #include <linux/fs.h>
  32. #include <linux/miscdevice.h>
  33. #include <linux/vmalloc.h>
  34. #include <linux/slab.h>
  35. #include <linux/device.h>
  36. #define TEST_START_NUM_THREADS 50
  37. #define TEST_START_DRIVER "test_module"
  38. #define TEST_START_TEST_FS "xfs"
  39. #define TEST_START_TEST_CASE TEST_KMOD_DRIVER
  40. static bool force_init_test = false;
  41. module_param(force_init_test, bool_enable_only, 0644);
  42. MODULE_PARM_DESC(force_init_test,
  43. "Force kicking a test immediately after driver loads");
  44. /*
  45. * For device allocation / registration
  46. */
  47. static DEFINE_MUTEX(reg_dev_mutex);
  48. static LIST_HEAD(reg_test_devs);
  49. /*
  50. * num_test_devs actually represents the *next* ID of the next
  51. * device we will allow to create.
  52. */
  53. static int num_test_devs;
  54. /**
  55. * enum kmod_test_case - linker table test case
  56. *
  57. * If you add a test case, please be sure to review if you need to se
  58. * @need_mod_put for your tests case.
  59. *
  60. * @TEST_KMOD_DRIVER: stress tests request_module()
  61. * @TEST_KMOD_FS_TYPE: stress tests get_fs_type()
  62. */
  63. enum kmod_test_case {
  64. __TEST_KMOD_INVALID = 0,
  65. TEST_KMOD_DRIVER,
  66. TEST_KMOD_FS_TYPE,
  67. __TEST_KMOD_MAX,
  68. };
  69. struct test_config {
  70. char *test_driver;
  71. char *test_fs;
  72. unsigned int num_threads;
  73. enum kmod_test_case test_case;
  74. int test_result;
  75. };
  76. struct kmod_test_device;
  77. /**
  78. * kmod_test_device_info - thread info
  79. *
  80. * @ret_sync: return value if request_module() is used, sync request for
  81. * @TEST_KMOD_DRIVER
  82. * @fs_sync: return value of get_fs_type() for @TEST_KMOD_FS_TYPE
  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. * 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_lock: 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 && 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 sucess 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. return run_test_fs_type(test_dev);
  437. default:
  438. dev_warn(test_dev->dev,
  439. "Invalid test case requested: %u\n",
  440. config->test_case);
  441. return -EINVAL;
  442. }
  443. }
  444. static int trigger_config_run(struct kmod_test_device *test_dev)
  445. {
  446. struct test_config *config = &test_dev->config;
  447. int ret;
  448. mutex_lock(&test_dev->trigger_mutex);
  449. mutex_lock(&test_dev->config_mutex);
  450. ret = __trigger_config_run(test_dev);
  451. if (ret < 0)
  452. goto out;
  453. dev_info(test_dev->dev, "General test result: %d\n",
  454. config->test_result);
  455. /*
  456. * We must return 0 after a trigger even unless something went
  457. * wrong with the setup of the test. If the test setup went fine
  458. * then userspace must just check the result of config->test_result.
  459. * One issue with relying on the return from a call in the kernel
  460. * is if the kernel returns a possitive value using this trigger
  461. * will not return the value to userspace, it would be lost.
  462. *
  463. * By not relying on capturing the return value of tests we are using
  464. * through the trigger it also us to run tests with set -e and only
  465. * fail when something went wrong with the driver upon trigger
  466. * requests.
  467. */
  468. ret = 0;
  469. out:
  470. mutex_unlock(&test_dev->config_mutex);
  471. mutex_unlock(&test_dev->trigger_mutex);
  472. return ret;
  473. }
  474. static ssize_t
  475. trigger_config_store(struct device *dev,
  476. struct device_attribute *attr,
  477. const char *buf, size_t count)
  478. {
  479. struct kmod_test_device *test_dev = dev_to_test_dev(dev);
  480. int ret;
  481. if (test_dev->test_is_oom)
  482. return -ENOMEM;
  483. /* For all intents and purposes we don't care what userspace
  484. * sent this trigger, we care only that we were triggered.
  485. * We treat the return value only for caputuring issues with
  486. * the test setup. At this point all the test variables should
  487. * have been allocated so typically this should never fail.
  488. */
  489. ret = trigger_config_run(test_dev);
  490. if (unlikely(ret < 0))
  491. goto out;
  492. /*
  493. * Note: any return > 0 will be treated as success
  494. * and the error value will not be available to userspace.
  495. * Do not rely on trying to send to userspace a test value
  496. * return value as possitive return errors will be lost.
  497. */
  498. if (WARN_ON(ret > 0))
  499. return -EINVAL;
  500. ret = count;
  501. out:
  502. return ret;
  503. }
  504. static DEVICE_ATTR_WO(trigger_config);
  505. /*
  506. * XXX: move to kstrncpy() once merged.
  507. *
  508. * Users should use kfree_const() when freeing these.
  509. */
  510. static int __kstrncpy(char **dst, const char *name, size_t count, gfp_t gfp)
  511. {
  512. *dst = kstrndup(name, count, gfp);
  513. if (!*dst)
  514. return -ENOSPC;
  515. return count;
  516. }
  517. static int config_copy_test_driver_name(struct test_config *config,
  518. const char *name,
  519. size_t count)
  520. {
  521. return __kstrncpy(&config->test_driver, name, count, GFP_KERNEL);
  522. }
  523. static int config_copy_test_fs(struct test_config *config, const char *name,
  524. size_t count)
  525. {
  526. return __kstrncpy(&config->test_fs, name, count, GFP_KERNEL);
  527. }
  528. static void __kmod_config_free(struct test_config *config)
  529. {
  530. if (!config)
  531. return;
  532. kfree_const(config->test_driver);
  533. config->test_driver = NULL;
  534. kfree_const(config->test_fs);
  535. config->test_driver = NULL;
  536. }
  537. static void kmod_config_free(struct kmod_test_device *test_dev)
  538. {
  539. struct test_config *config;
  540. if (!test_dev)
  541. return;
  542. config = &test_dev->config;
  543. mutex_lock(&test_dev->config_mutex);
  544. __kmod_config_free(config);
  545. mutex_unlock(&test_dev->config_mutex);
  546. }
  547. static ssize_t config_test_driver_store(struct device *dev,
  548. struct device_attribute *attr,
  549. const char *buf, size_t count)
  550. {
  551. struct kmod_test_device *test_dev = dev_to_test_dev(dev);
  552. struct test_config *config = &test_dev->config;
  553. int copied;
  554. mutex_lock(&test_dev->config_mutex);
  555. kfree_const(config->test_driver);
  556. config->test_driver = NULL;
  557. copied = config_copy_test_driver_name(config, buf, count);
  558. mutex_unlock(&test_dev->config_mutex);
  559. return copied;
  560. }
  561. /*
  562. * As per sysfs_kf_seq_show() the buf is max PAGE_SIZE.
  563. */
  564. static ssize_t config_test_show_str(struct mutex *config_mutex,
  565. char *dst,
  566. char *src)
  567. {
  568. int len;
  569. mutex_lock(config_mutex);
  570. len = snprintf(dst, PAGE_SIZE, "%s\n", src);
  571. mutex_unlock(config_mutex);
  572. return len;
  573. }
  574. static ssize_t config_test_driver_show(struct device *dev,
  575. struct device_attribute *attr,
  576. char *buf)
  577. {
  578. struct kmod_test_device *test_dev = dev_to_test_dev(dev);
  579. struct test_config *config = &test_dev->config;
  580. return config_test_show_str(&test_dev->config_mutex, buf,
  581. config->test_driver);
  582. }
  583. static DEVICE_ATTR(config_test_driver, 0644, config_test_driver_show,
  584. config_test_driver_store);
  585. static ssize_t config_test_fs_store(struct device *dev,
  586. struct device_attribute *attr,
  587. const char *buf, size_t count)
  588. {
  589. struct kmod_test_device *test_dev = dev_to_test_dev(dev);
  590. struct test_config *config = &test_dev->config;
  591. int copied;
  592. mutex_lock(&test_dev->config_mutex);
  593. kfree_const(config->test_fs);
  594. config->test_fs = NULL;
  595. copied = config_copy_test_fs(config, buf, count);
  596. mutex_unlock(&test_dev->config_mutex);
  597. return copied;
  598. }
  599. static ssize_t config_test_fs_show(struct device *dev,
  600. struct device_attribute *attr,
  601. char *buf)
  602. {
  603. struct kmod_test_device *test_dev = dev_to_test_dev(dev);
  604. struct test_config *config = &test_dev->config;
  605. return config_test_show_str(&test_dev->config_mutex, buf,
  606. config->test_fs);
  607. }
  608. static DEVICE_ATTR(config_test_fs, 0644, config_test_fs_show,
  609. config_test_fs_store);
  610. static int trigger_config_run_type(struct kmod_test_device *test_dev,
  611. enum kmod_test_case test_case,
  612. const char *test_str)
  613. {
  614. int copied = 0;
  615. struct test_config *config = &test_dev->config;
  616. mutex_lock(&test_dev->config_mutex);
  617. switch (test_case) {
  618. case TEST_KMOD_DRIVER:
  619. kfree_const(config->test_driver);
  620. config->test_driver = NULL;
  621. copied = config_copy_test_driver_name(config, test_str,
  622. strlen(test_str));
  623. break;
  624. case TEST_KMOD_FS_TYPE:
  625. kfree_const(config->test_fs);
  626. config->test_driver = NULL;
  627. copied = config_copy_test_fs(config, test_str,
  628. strlen(test_str));
  629. break;
  630. default:
  631. mutex_unlock(&test_dev->config_mutex);
  632. return -EINVAL;
  633. }
  634. config->test_case = test_case;
  635. mutex_unlock(&test_dev->config_mutex);
  636. if (copied <= 0 || copied != strlen(test_str)) {
  637. test_dev->test_is_oom = true;
  638. return -ENOMEM;
  639. }
  640. test_dev->test_is_oom = false;
  641. return trigger_config_run(test_dev);
  642. }
  643. static void free_test_dev_info(struct kmod_test_device *test_dev)
  644. {
  645. vfree(test_dev->info);
  646. test_dev->info = NULL;
  647. }
  648. static int kmod_config_sync_info(struct kmod_test_device *test_dev)
  649. {
  650. struct test_config *config = &test_dev->config;
  651. free_test_dev_info(test_dev);
  652. test_dev->info = vzalloc(config->num_threads *
  653. sizeof(struct kmod_test_device_info));
  654. if (!test_dev->info) {
  655. dev_err(test_dev->dev, "Cannot alloc test_dev info\n");
  656. return -ENOMEM;
  657. }
  658. return 0;
  659. }
  660. /*
  661. * Old kernels may not have this, if you want to port this code to
  662. * test it on older kernels.
  663. */
  664. #ifdef get_kmod_umh_limit
  665. static unsigned int kmod_init_test_thread_limit(void)
  666. {
  667. return get_kmod_umh_limit();
  668. }
  669. #else
  670. static unsigned int kmod_init_test_thread_limit(void)
  671. {
  672. return TEST_START_NUM_THREADS;
  673. }
  674. #endif
  675. static int __kmod_config_init(struct kmod_test_device *test_dev)
  676. {
  677. struct test_config *config = &test_dev->config;
  678. int ret = -ENOMEM, copied;
  679. __kmod_config_free(config);
  680. copied = config_copy_test_driver_name(config, TEST_START_DRIVER,
  681. strlen(TEST_START_DRIVER));
  682. if (copied != strlen(TEST_START_DRIVER))
  683. goto err_out;
  684. copied = config_copy_test_fs(config, TEST_START_TEST_FS,
  685. strlen(TEST_START_TEST_FS));
  686. if (copied != strlen(TEST_START_TEST_FS))
  687. goto err_out;
  688. config->num_threads = kmod_init_test_thread_limit();
  689. config->test_result = 0;
  690. config->test_case = TEST_START_TEST_CASE;
  691. ret = kmod_config_sync_info(test_dev);
  692. if (ret)
  693. goto err_out;
  694. test_dev->test_is_oom = false;
  695. return 0;
  696. err_out:
  697. test_dev->test_is_oom = true;
  698. WARN_ON(test_dev->test_is_oom);
  699. __kmod_config_free(config);
  700. return ret;
  701. }
  702. static ssize_t reset_store(struct device *dev,
  703. struct device_attribute *attr,
  704. const char *buf, size_t count)
  705. {
  706. struct kmod_test_device *test_dev = dev_to_test_dev(dev);
  707. int ret;
  708. mutex_lock(&test_dev->trigger_mutex);
  709. mutex_lock(&test_dev->config_mutex);
  710. ret = __kmod_config_init(test_dev);
  711. if (ret < 0) {
  712. ret = -ENOMEM;
  713. dev_err(dev, "could not alloc settings for config trigger: %d\n",
  714. ret);
  715. goto out;
  716. }
  717. dev_info(dev, "reset\n");
  718. ret = count;
  719. out:
  720. mutex_unlock(&test_dev->config_mutex);
  721. mutex_unlock(&test_dev->trigger_mutex);
  722. return ret;
  723. }
  724. static DEVICE_ATTR_WO(reset);
  725. static int test_dev_config_update_uint_sync(struct kmod_test_device *test_dev,
  726. const char *buf, size_t size,
  727. unsigned int *config,
  728. int (*test_sync)(struct kmod_test_device *test_dev))
  729. {
  730. int ret;
  731. unsigned long new;
  732. unsigned int old_val;
  733. ret = kstrtoul(buf, 10, &new);
  734. if (ret)
  735. return ret;
  736. if (new > UINT_MAX)
  737. return -EINVAL;
  738. mutex_lock(&test_dev->config_mutex);
  739. old_val = *config;
  740. *(unsigned int *)config = new;
  741. ret = test_sync(test_dev);
  742. if (ret) {
  743. *(unsigned int *)config = old_val;
  744. ret = test_sync(test_dev);
  745. WARN_ON(ret);
  746. mutex_unlock(&test_dev->config_mutex);
  747. return -EINVAL;
  748. }
  749. mutex_unlock(&test_dev->config_mutex);
  750. /* Always return full write size even if we didn't consume all */
  751. return size;
  752. }
  753. static int test_dev_config_update_uint_range(struct kmod_test_device *test_dev,
  754. const char *buf, size_t size,
  755. unsigned int *config,
  756. unsigned int min,
  757. unsigned int max)
  758. {
  759. int ret;
  760. unsigned long new;
  761. ret = kstrtoul(buf, 10, &new);
  762. if (ret)
  763. return ret;
  764. if (new < min || new > max)
  765. return -EINVAL;
  766. mutex_lock(&test_dev->config_mutex);
  767. *config = new;
  768. mutex_unlock(&test_dev->config_mutex);
  769. /* Always return full write size even if we didn't consume all */
  770. return size;
  771. }
  772. static int test_dev_config_update_int(struct kmod_test_device *test_dev,
  773. const char *buf, size_t size,
  774. int *config)
  775. {
  776. int ret;
  777. long new;
  778. ret = kstrtol(buf, 10, &new);
  779. if (ret)
  780. return ret;
  781. if (new > INT_MAX || new < INT_MIN)
  782. return -EINVAL;
  783. mutex_lock(&test_dev->config_mutex);
  784. *config = new;
  785. mutex_unlock(&test_dev->config_mutex);
  786. /* Always return full write size even if we didn't consume all */
  787. return size;
  788. }
  789. static ssize_t test_dev_config_show_int(struct kmod_test_device *test_dev,
  790. char *buf,
  791. int config)
  792. {
  793. int val;
  794. mutex_lock(&test_dev->config_mutex);
  795. val = config;
  796. mutex_unlock(&test_dev->config_mutex);
  797. return snprintf(buf, PAGE_SIZE, "%d\n", val);
  798. }
  799. static ssize_t test_dev_config_show_uint(struct kmod_test_device *test_dev,
  800. char *buf,
  801. unsigned int config)
  802. {
  803. unsigned int val;
  804. mutex_lock(&test_dev->config_mutex);
  805. val = config;
  806. mutex_unlock(&test_dev->config_mutex);
  807. return snprintf(buf, PAGE_SIZE, "%u\n", val);
  808. }
  809. static ssize_t test_result_store(struct device *dev,
  810. struct device_attribute *attr,
  811. const char *buf, size_t count)
  812. {
  813. struct kmod_test_device *test_dev = dev_to_test_dev(dev);
  814. struct test_config *config = &test_dev->config;
  815. return test_dev_config_update_int(test_dev, buf, count,
  816. &config->test_result);
  817. }
  818. static ssize_t config_num_threads_store(struct device *dev,
  819. struct device_attribute *attr,
  820. const char *buf, size_t count)
  821. {
  822. struct kmod_test_device *test_dev = dev_to_test_dev(dev);
  823. struct test_config *config = &test_dev->config;
  824. return test_dev_config_update_uint_sync(test_dev, buf, count,
  825. &config->num_threads,
  826. kmod_config_sync_info);
  827. }
  828. static ssize_t config_num_threads_show(struct device *dev,
  829. struct device_attribute *attr,
  830. char *buf)
  831. {
  832. struct kmod_test_device *test_dev = dev_to_test_dev(dev);
  833. struct test_config *config = &test_dev->config;
  834. return test_dev_config_show_int(test_dev, buf, config->num_threads);
  835. }
  836. static DEVICE_ATTR(config_num_threads, 0644, config_num_threads_show,
  837. config_num_threads_store);
  838. static ssize_t config_test_case_store(struct device *dev,
  839. struct device_attribute *attr,
  840. const char *buf, size_t count)
  841. {
  842. struct kmod_test_device *test_dev = dev_to_test_dev(dev);
  843. struct test_config *config = &test_dev->config;
  844. return test_dev_config_update_uint_range(test_dev, buf, count,
  845. &config->test_case,
  846. __TEST_KMOD_INVALID + 1,
  847. __TEST_KMOD_MAX - 1);
  848. }
  849. static ssize_t config_test_case_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_uint(test_dev, buf, config->test_case);
  856. }
  857. static DEVICE_ATTR(config_test_case, 0644, config_test_case_show,
  858. config_test_case_store);
  859. static ssize_t test_result_show(struct device *dev,
  860. struct device_attribute *attr,
  861. char *buf)
  862. {
  863. struct kmod_test_device *test_dev = dev_to_test_dev(dev);
  864. struct test_config *config = &test_dev->config;
  865. return test_dev_config_show_int(test_dev, buf, config->test_result);
  866. }
  867. static DEVICE_ATTR(test_result, 0644, test_result_show, test_result_store);
  868. #define TEST_KMOD_DEV_ATTR(name) &dev_attr_##name.attr
  869. static struct attribute *test_dev_attrs[] = {
  870. TEST_KMOD_DEV_ATTR(trigger_config),
  871. TEST_KMOD_DEV_ATTR(config),
  872. TEST_KMOD_DEV_ATTR(reset),
  873. TEST_KMOD_DEV_ATTR(config_test_driver),
  874. TEST_KMOD_DEV_ATTR(config_test_fs),
  875. TEST_KMOD_DEV_ATTR(config_num_threads),
  876. TEST_KMOD_DEV_ATTR(config_test_case),
  877. TEST_KMOD_DEV_ATTR(test_result),
  878. NULL,
  879. };
  880. ATTRIBUTE_GROUPS(test_dev);
  881. static int kmod_config_init(struct kmod_test_device *test_dev)
  882. {
  883. int ret;
  884. mutex_lock(&test_dev->config_mutex);
  885. ret = __kmod_config_init(test_dev);
  886. mutex_unlock(&test_dev->config_mutex);
  887. return ret;
  888. }
  889. static struct kmod_test_device *alloc_test_dev_kmod(int idx)
  890. {
  891. int ret;
  892. struct kmod_test_device *test_dev;
  893. struct miscdevice *misc_dev;
  894. test_dev = vzalloc(sizeof(struct kmod_test_device));
  895. if (!test_dev) {
  896. pr_err("Cannot alloc test_dev\n");
  897. goto err_out;
  898. }
  899. mutex_init(&test_dev->config_mutex);
  900. mutex_init(&test_dev->trigger_mutex);
  901. mutex_init(&test_dev->thread_mutex);
  902. init_completion(&test_dev->kthreads_done);
  903. ret = kmod_config_init(test_dev);
  904. if (ret < 0) {
  905. pr_err("Cannot alloc kmod_config_init()\n");
  906. goto err_out_free;
  907. }
  908. test_dev->dev_idx = idx;
  909. misc_dev = &test_dev->misc_dev;
  910. misc_dev->minor = MISC_DYNAMIC_MINOR;
  911. misc_dev->name = kasprintf(GFP_KERNEL, "test_kmod%d", idx);
  912. if (!misc_dev->name) {
  913. pr_err("Cannot alloc misc_dev->name\n");
  914. goto err_out_free_config;
  915. }
  916. misc_dev->groups = test_dev_groups;
  917. return test_dev;
  918. err_out_free_config:
  919. free_test_dev_info(test_dev);
  920. kmod_config_free(test_dev);
  921. err_out_free:
  922. vfree(test_dev);
  923. test_dev = NULL;
  924. err_out:
  925. return NULL;
  926. }
  927. static void free_test_dev_kmod(struct kmod_test_device *test_dev)
  928. {
  929. if (test_dev) {
  930. kfree_const(test_dev->misc_dev.name);
  931. test_dev->misc_dev.name = NULL;
  932. free_test_dev_info(test_dev);
  933. kmod_config_free(test_dev);
  934. vfree(test_dev);
  935. test_dev = NULL;
  936. }
  937. }
  938. static struct kmod_test_device *register_test_dev_kmod(void)
  939. {
  940. struct kmod_test_device *test_dev = NULL;
  941. int ret;
  942. mutex_lock(&reg_dev_mutex);
  943. /* int should suffice for number of devices, test for wrap */
  944. if (unlikely(num_test_devs + 1) < 0) {
  945. pr_err("reached limit of number of test devices\n");
  946. goto out;
  947. }
  948. test_dev = alloc_test_dev_kmod(num_test_devs);
  949. if (!test_dev)
  950. goto out;
  951. ret = misc_register(&test_dev->misc_dev);
  952. if (ret) {
  953. pr_err("could not register misc device: %d\n", ret);
  954. free_test_dev_kmod(test_dev);
  955. goto out;
  956. }
  957. test_dev->dev = test_dev->misc_dev.this_device;
  958. list_add_tail(&test_dev->list, &reg_test_devs);
  959. dev_info(test_dev->dev, "interface ready\n");
  960. num_test_devs++;
  961. out:
  962. mutex_unlock(&reg_dev_mutex);
  963. return test_dev;
  964. }
  965. static int __init test_kmod_init(void)
  966. {
  967. struct kmod_test_device *test_dev;
  968. int ret;
  969. test_dev = register_test_dev_kmod();
  970. if (!test_dev) {
  971. pr_err("Cannot add first test kmod device\n");
  972. return -ENODEV;
  973. }
  974. /*
  975. * With some work we might be able to gracefully enable
  976. * testing with this driver built-in, for now this seems
  977. * rather risky. For those willing to try have at it,
  978. * and enable the below. Good luck! If that works, try
  979. * lowering the init level for more fun.
  980. */
  981. if (force_init_test) {
  982. ret = trigger_config_run_type(test_dev,
  983. TEST_KMOD_DRIVER, "tun");
  984. if (WARN_ON(ret))
  985. return ret;
  986. ret = trigger_config_run_type(test_dev,
  987. TEST_KMOD_FS_TYPE, "btrfs");
  988. if (WARN_ON(ret))
  989. return ret;
  990. }
  991. return 0;
  992. }
  993. late_initcall(test_kmod_init);
  994. static
  995. void unregister_test_dev_kmod(struct kmod_test_device *test_dev)
  996. {
  997. mutex_lock(&test_dev->trigger_mutex);
  998. mutex_lock(&test_dev->config_mutex);
  999. test_dev_kmod_stop_tests(test_dev);
  1000. dev_info(test_dev->dev, "removing interface\n");
  1001. misc_deregister(&test_dev->misc_dev);
  1002. kfree(&test_dev->misc_dev.name);
  1003. mutex_unlock(&test_dev->config_mutex);
  1004. mutex_unlock(&test_dev->trigger_mutex);
  1005. free_test_dev_kmod(test_dev);
  1006. }
  1007. static void __exit test_kmod_exit(void)
  1008. {
  1009. struct kmod_test_device *test_dev, *tmp;
  1010. mutex_lock(&reg_dev_mutex);
  1011. list_for_each_entry_safe(test_dev, tmp, &reg_test_devs, list) {
  1012. list_del(&test_dev->list);
  1013. unregister_test_dev_kmod(test_dev);
  1014. }
  1015. mutex_unlock(&reg_dev_mutex);
  1016. }
  1017. module_exit(test_kmod_exit);
  1018. MODULE_AUTHOR("Luis R. Rodriguez <mcgrof@kernel.org>");
  1019. MODULE_LICENSE("GPL");