test_kmod.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239
  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_fs = 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_RW(config_test_driver);
  584. static ssize_t config_test_fs_store(struct device *dev,
  585. struct device_attribute *attr,
  586. const char *buf, size_t count)
  587. {
  588. struct kmod_test_device *test_dev = dev_to_test_dev(dev);
  589. struct test_config *config = &test_dev->config;
  590. int copied;
  591. mutex_lock(&test_dev->config_mutex);
  592. kfree_const(config->test_fs);
  593. config->test_fs = NULL;
  594. copied = config_copy_test_fs(config, buf, count);
  595. mutex_unlock(&test_dev->config_mutex);
  596. return copied;
  597. }
  598. static ssize_t config_test_fs_show(struct device *dev,
  599. struct device_attribute *attr,
  600. char *buf)
  601. {
  602. struct kmod_test_device *test_dev = dev_to_test_dev(dev);
  603. struct test_config *config = &test_dev->config;
  604. return config_test_show_str(&test_dev->config_mutex, buf,
  605. config->test_fs);
  606. }
  607. static DEVICE_ATTR_RW(config_test_fs);
  608. static int trigger_config_run_type(struct kmod_test_device *test_dev,
  609. enum kmod_test_case test_case,
  610. const char *test_str)
  611. {
  612. int copied = 0;
  613. struct test_config *config = &test_dev->config;
  614. mutex_lock(&test_dev->config_mutex);
  615. switch (test_case) {
  616. case TEST_KMOD_DRIVER:
  617. kfree_const(config->test_driver);
  618. config->test_driver = NULL;
  619. copied = config_copy_test_driver_name(config, test_str,
  620. strlen(test_str));
  621. break;
  622. case TEST_KMOD_FS_TYPE:
  623. kfree_const(config->test_fs);
  624. config->test_driver = NULL;
  625. copied = config_copy_test_fs(config, test_str,
  626. strlen(test_str));
  627. break;
  628. default:
  629. mutex_unlock(&test_dev->config_mutex);
  630. return -EINVAL;
  631. }
  632. config->test_case = test_case;
  633. mutex_unlock(&test_dev->config_mutex);
  634. if (copied <= 0 || copied != strlen(test_str)) {
  635. test_dev->test_is_oom = true;
  636. return -ENOMEM;
  637. }
  638. test_dev->test_is_oom = false;
  639. return trigger_config_run(test_dev);
  640. }
  641. static void free_test_dev_info(struct kmod_test_device *test_dev)
  642. {
  643. vfree(test_dev->info);
  644. test_dev->info = NULL;
  645. }
  646. static int kmod_config_sync_info(struct kmod_test_device *test_dev)
  647. {
  648. struct test_config *config = &test_dev->config;
  649. free_test_dev_info(test_dev);
  650. test_dev->info =
  651. vzalloc(array_size(sizeof(struct kmod_test_device_info),
  652. config->num_threads));
  653. if (!test_dev->info)
  654. return -ENOMEM;
  655. return 0;
  656. }
  657. /*
  658. * Old kernels may not have this, if you want to port this code to
  659. * test it on older kernels.
  660. */
  661. #ifdef get_kmod_umh_limit
  662. static unsigned int kmod_init_test_thread_limit(void)
  663. {
  664. return get_kmod_umh_limit();
  665. }
  666. #else
  667. static unsigned int kmod_init_test_thread_limit(void)
  668. {
  669. return TEST_START_NUM_THREADS;
  670. }
  671. #endif
  672. static int __kmod_config_init(struct kmod_test_device *test_dev)
  673. {
  674. struct test_config *config = &test_dev->config;
  675. int ret = -ENOMEM, copied;
  676. __kmod_config_free(config);
  677. copied = config_copy_test_driver_name(config, TEST_START_DRIVER,
  678. strlen(TEST_START_DRIVER));
  679. if (copied != strlen(TEST_START_DRIVER))
  680. goto err_out;
  681. copied = config_copy_test_fs(config, TEST_START_TEST_FS,
  682. strlen(TEST_START_TEST_FS));
  683. if (copied != strlen(TEST_START_TEST_FS))
  684. goto err_out;
  685. config->num_threads = kmod_init_test_thread_limit();
  686. config->test_result = 0;
  687. config->test_case = TEST_START_TEST_CASE;
  688. ret = kmod_config_sync_info(test_dev);
  689. if (ret)
  690. goto err_out;
  691. test_dev->test_is_oom = false;
  692. return 0;
  693. err_out:
  694. test_dev->test_is_oom = true;
  695. WARN_ON(test_dev->test_is_oom);
  696. __kmod_config_free(config);
  697. return ret;
  698. }
  699. static ssize_t reset_store(struct device *dev,
  700. struct device_attribute *attr,
  701. const char *buf, size_t count)
  702. {
  703. struct kmod_test_device *test_dev = dev_to_test_dev(dev);
  704. int ret;
  705. mutex_lock(&test_dev->trigger_mutex);
  706. mutex_lock(&test_dev->config_mutex);
  707. ret = __kmod_config_init(test_dev);
  708. if (ret < 0) {
  709. ret = -ENOMEM;
  710. dev_err(dev, "could not alloc settings for config trigger: %d\n",
  711. ret);
  712. goto out;
  713. }
  714. dev_info(dev, "reset\n");
  715. ret = count;
  716. out:
  717. mutex_unlock(&test_dev->config_mutex);
  718. mutex_unlock(&test_dev->trigger_mutex);
  719. return ret;
  720. }
  721. static DEVICE_ATTR_WO(reset);
  722. static int test_dev_config_update_uint_sync(struct kmod_test_device *test_dev,
  723. const char *buf, size_t size,
  724. unsigned int *config,
  725. int (*test_sync)(struct kmod_test_device *test_dev))
  726. {
  727. int ret;
  728. unsigned long new;
  729. unsigned int old_val;
  730. ret = kstrtoul(buf, 10, &new);
  731. if (ret)
  732. return ret;
  733. if (new > UINT_MAX)
  734. return -EINVAL;
  735. mutex_lock(&test_dev->config_mutex);
  736. old_val = *config;
  737. *(unsigned int *)config = new;
  738. ret = test_sync(test_dev);
  739. if (ret) {
  740. *(unsigned int *)config = old_val;
  741. ret = test_sync(test_dev);
  742. WARN_ON(ret);
  743. mutex_unlock(&test_dev->config_mutex);
  744. return -EINVAL;
  745. }
  746. mutex_unlock(&test_dev->config_mutex);
  747. /* Always return full write size even if we didn't consume all */
  748. return size;
  749. }
  750. static int test_dev_config_update_uint_range(struct kmod_test_device *test_dev,
  751. const char *buf, size_t size,
  752. unsigned int *config,
  753. unsigned int min,
  754. unsigned int max)
  755. {
  756. int ret;
  757. unsigned long new;
  758. ret = kstrtoul(buf, 10, &new);
  759. if (ret)
  760. return ret;
  761. if (new < min || new > max)
  762. return -EINVAL;
  763. mutex_lock(&test_dev->config_mutex);
  764. *config = new;
  765. mutex_unlock(&test_dev->config_mutex);
  766. /* Always return full write size even if we didn't consume all */
  767. return size;
  768. }
  769. static int test_dev_config_update_int(struct kmod_test_device *test_dev,
  770. const char *buf, size_t size,
  771. int *config)
  772. {
  773. int ret;
  774. long new;
  775. ret = kstrtol(buf, 10, &new);
  776. if (ret)
  777. return ret;
  778. if (new < INT_MIN || new > INT_MAX)
  779. return -EINVAL;
  780. mutex_lock(&test_dev->config_mutex);
  781. *config = new;
  782. mutex_unlock(&test_dev->config_mutex);
  783. /* Always return full write size even if we didn't consume all */
  784. return size;
  785. }
  786. static ssize_t test_dev_config_show_int(struct kmod_test_device *test_dev,
  787. char *buf,
  788. int config)
  789. {
  790. int val;
  791. mutex_lock(&test_dev->config_mutex);
  792. val = config;
  793. mutex_unlock(&test_dev->config_mutex);
  794. return snprintf(buf, PAGE_SIZE, "%d\n", val);
  795. }
  796. static ssize_t test_dev_config_show_uint(struct kmod_test_device *test_dev,
  797. char *buf,
  798. unsigned int config)
  799. {
  800. unsigned int val;
  801. mutex_lock(&test_dev->config_mutex);
  802. val = config;
  803. mutex_unlock(&test_dev->config_mutex);
  804. return snprintf(buf, PAGE_SIZE, "%u\n", val);
  805. }
  806. static ssize_t test_result_store(struct device *dev,
  807. struct device_attribute *attr,
  808. const char *buf, size_t count)
  809. {
  810. struct kmod_test_device *test_dev = dev_to_test_dev(dev);
  811. struct test_config *config = &test_dev->config;
  812. return test_dev_config_update_int(test_dev, buf, count,
  813. &config->test_result);
  814. }
  815. static ssize_t config_num_threads_store(struct device *dev,
  816. struct device_attribute *attr,
  817. const char *buf, size_t count)
  818. {
  819. struct kmod_test_device *test_dev = dev_to_test_dev(dev);
  820. struct test_config *config = &test_dev->config;
  821. return test_dev_config_update_uint_sync(test_dev, buf, count,
  822. &config->num_threads,
  823. kmod_config_sync_info);
  824. }
  825. static ssize_t config_num_threads_show(struct device *dev,
  826. struct device_attribute *attr,
  827. char *buf)
  828. {
  829. struct kmod_test_device *test_dev = dev_to_test_dev(dev);
  830. struct test_config *config = &test_dev->config;
  831. return test_dev_config_show_int(test_dev, buf, config->num_threads);
  832. }
  833. static DEVICE_ATTR_RW(config_num_threads);
  834. static ssize_t config_test_case_store(struct device *dev,
  835. struct device_attribute *attr,
  836. const char *buf, size_t count)
  837. {
  838. struct kmod_test_device *test_dev = dev_to_test_dev(dev);
  839. struct test_config *config = &test_dev->config;
  840. return test_dev_config_update_uint_range(test_dev, buf, count,
  841. &config->test_case,
  842. __TEST_KMOD_INVALID + 1,
  843. __TEST_KMOD_MAX - 1);
  844. }
  845. static ssize_t config_test_case_show(struct device *dev,
  846. struct device_attribute *attr,
  847. char *buf)
  848. {
  849. struct kmod_test_device *test_dev = dev_to_test_dev(dev);
  850. struct test_config *config = &test_dev->config;
  851. return test_dev_config_show_uint(test_dev, buf, config->test_case);
  852. }
  853. static DEVICE_ATTR_RW(config_test_case);
  854. static ssize_t test_result_show(struct device *dev,
  855. struct device_attribute *attr,
  856. char *buf)
  857. {
  858. struct kmod_test_device *test_dev = dev_to_test_dev(dev);
  859. struct test_config *config = &test_dev->config;
  860. return test_dev_config_show_int(test_dev, buf, config->test_result);
  861. }
  862. static DEVICE_ATTR_RW(test_result);
  863. #define TEST_KMOD_DEV_ATTR(name) &dev_attr_##name.attr
  864. static struct attribute *test_dev_attrs[] = {
  865. TEST_KMOD_DEV_ATTR(trigger_config),
  866. TEST_KMOD_DEV_ATTR(config),
  867. TEST_KMOD_DEV_ATTR(reset),
  868. TEST_KMOD_DEV_ATTR(config_test_driver),
  869. TEST_KMOD_DEV_ATTR(config_test_fs),
  870. TEST_KMOD_DEV_ATTR(config_num_threads),
  871. TEST_KMOD_DEV_ATTR(config_test_case),
  872. TEST_KMOD_DEV_ATTR(test_result),
  873. NULL,
  874. };
  875. ATTRIBUTE_GROUPS(test_dev);
  876. static int kmod_config_init(struct kmod_test_device *test_dev)
  877. {
  878. int ret;
  879. mutex_lock(&test_dev->config_mutex);
  880. ret = __kmod_config_init(test_dev);
  881. mutex_unlock(&test_dev->config_mutex);
  882. return ret;
  883. }
  884. static struct kmod_test_device *alloc_test_dev_kmod(int idx)
  885. {
  886. int ret;
  887. struct kmod_test_device *test_dev;
  888. struct miscdevice *misc_dev;
  889. test_dev = vzalloc(sizeof(struct kmod_test_device));
  890. if (!test_dev)
  891. goto err_out;
  892. mutex_init(&test_dev->config_mutex);
  893. mutex_init(&test_dev->trigger_mutex);
  894. mutex_init(&test_dev->thread_mutex);
  895. init_completion(&test_dev->kthreads_done);
  896. ret = kmod_config_init(test_dev);
  897. if (ret < 0) {
  898. pr_err("Cannot alloc kmod_config_init()\n");
  899. goto err_out_free;
  900. }
  901. test_dev->dev_idx = idx;
  902. misc_dev = &test_dev->misc_dev;
  903. misc_dev->minor = MISC_DYNAMIC_MINOR;
  904. misc_dev->name = kasprintf(GFP_KERNEL, "test_kmod%d", idx);
  905. if (!misc_dev->name) {
  906. pr_err("Cannot alloc misc_dev->name\n");
  907. goto err_out_free_config;
  908. }
  909. misc_dev->groups = test_dev_groups;
  910. return test_dev;
  911. err_out_free_config:
  912. free_test_dev_info(test_dev);
  913. kmod_config_free(test_dev);
  914. err_out_free:
  915. vfree(test_dev);
  916. test_dev = NULL;
  917. err_out:
  918. return NULL;
  919. }
  920. static void free_test_dev_kmod(struct kmod_test_device *test_dev)
  921. {
  922. if (test_dev) {
  923. kfree_const(test_dev->misc_dev.name);
  924. test_dev->misc_dev.name = NULL;
  925. free_test_dev_info(test_dev);
  926. kmod_config_free(test_dev);
  927. vfree(test_dev);
  928. test_dev = NULL;
  929. }
  930. }
  931. static struct kmod_test_device *register_test_dev_kmod(void)
  932. {
  933. struct kmod_test_device *test_dev = NULL;
  934. int ret;
  935. mutex_lock(&reg_dev_mutex);
  936. /* int should suffice for number of devices, test for wrap */
  937. if (num_test_devs + 1 == INT_MAX) {
  938. pr_err("reached limit of number of test devices\n");
  939. goto out;
  940. }
  941. test_dev = alloc_test_dev_kmod(num_test_devs);
  942. if (!test_dev)
  943. goto out;
  944. ret = misc_register(&test_dev->misc_dev);
  945. if (ret) {
  946. pr_err("could not register misc device: %d\n", ret);
  947. free_test_dev_kmod(test_dev);
  948. goto out;
  949. }
  950. test_dev->dev = test_dev->misc_dev.this_device;
  951. list_add_tail(&test_dev->list, &reg_test_devs);
  952. dev_info(test_dev->dev, "interface ready\n");
  953. num_test_devs++;
  954. out:
  955. mutex_unlock(&reg_dev_mutex);
  956. return test_dev;
  957. }
  958. static int __init test_kmod_init(void)
  959. {
  960. struct kmod_test_device *test_dev;
  961. int ret;
  962. test_dev = register_test_dev_kmod();
  963. if (!test_dev) {
  964. pr_err("Cannot add first test kmod device\n");
  965. return -ENODEV;
  966. }
  967. /*
  968. * With some work we might be able to gracefully enable
  969. * testing with this driver built-in, for now this seems
  970. * rather risky. For those willing to try have at it,
  971. * and enable the below. Good luck! If that works, try
  972. * lowering the init level for more fun.
  973. */
  974. if (force_init_test) {
  975. ret = trigger_config_run_type(test_dev,
  976. TEST_KMOD_DRIVER, "tun");
  977. if (WARN_ON(ret))
  978. return ret;
  979. ret = trigger_config_run_type(test_dev,
  980. TEST_KMOD_FS_TYPE, "btrfs");
  981. if (WARN_ON(ret))
  982. return ret;
  983. }
  984. return 0;
  985. }
  986. late_initcall(test_kmod_init);
  987. static
  988. void unregister_test_dev_kmod(struct kmod_test_device *test_dev)
  989. {
  990. mutex_lock(&test_dev->trigger_mutex);
  991. mutex_lock(&test_dev->config_mutex);
  992. test_dev_kmod_stop_tests(test_dev);
  993. dev_info(test_dev->dev, "removing interface\n");
  994. misc_deregister(&test_dev->misc_dev);
  995. mutex_unlock(&test_dev->config_mutex);
  996. mutex_unlock(&test_dev->trigger_mutex);
  997. free_test_dev_kmod(test_dev);
  998. }
  999. static void __exit test_kmod_exit(void)
  1000. {
  1001. struct kmod_test_device *test_dev, *tmp;
  1002. mutex_lock(&reg_dev_mutex);
  1003. list_for_each_entry_safe(test_dev, tmp, &reg_test_devs, list) {
  1004. list_del(&test_dev->list);
  1005. unregister_test_dev_kmod(test_dev);
  1006. }
  1007. mutex_unlock(&reg_dev_mutex);
  1008. }
  1009. module_exit(test_kmod_exit);
  1010. MODULE_AUTHOR("Luis R. Rodriguez <mcgrof@kernel.org>");
  1011. MODULE_LICENSE("GPL");