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