1 // SPDX-License-Identifier: GPL-2.0-or-later OR copyleft-next-0.3.1
3 * kmod stress test driver
5 * Copyright (C) 2017 Luis R. Rodriguez <mcgrof@kernel.org>
7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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:
14 * tools/testing/selftests/kmod/kmod.sh --help
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>
24 #include <linux/miscdevice.h>
25 #include <linux/vmalloc.h>
26 #include <linux/slab.h>
27 #include <linux/device.h>
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
35 static bool force_init_test = false;
36 module_param(force_init_test, bool_enable_only, 0644);
37 MODULE_PARM_DESC(force_init_test,
38 "Force kicking a test immediately after driver loads");
41 * For device allocation / registration
43 static DEFINE_MUTEX(reg_dev_mutex);
44 static LIST_HEAD(reg_test_devs);
47 * num_test_devs actually represents the *next* ID of the next
48 * device we will allow to create.
50 static int num_test_devs;
53 * enum kmod_test_case - linker table test case
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.
58 * @TEST_KMOD_DRIVER: stress tests request_module()
59 * @TEST_KMOD_FS_TYPE: stress tests get_fs_type()
62 __TEST_KMOD_INVALID = 0,
73 unsigned int num_threads;
74 enum kmod_test_case test_case;
78 struct kmod_test_device;
81 * kmod_test_device_info - thread info
83 * @ret_sync: return value if request_module() is used, sync request for
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.
94 struct kmod_test_device_info {
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;
104 * kmod_test_device - test device to help test kmod
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
119 struct kmod_test_device {
121 struct test_config config;
122 struct miscdevice misc_dev;
124 struct mutex config_mutex;
125 struct mutex trigger_mutex;
126 struct mutex thread_mutex;
131 struct completion kthreads_done;
132 struct list_head list;
134 struct kmod_test_device_info *info;
137 static const char *test_case_str(enum kmod_test_case test_case)
140 case TEST_KMOD_DRIVER:
141 return "TEST_KMOD_DRIVER";
142 case TEST_KMOD_FS_TYPE:
143 return "TEST_KMOD_FS_TYPE";
149 static struct miscdevice *dev_to_misc_dev(struct device *dev)
151 return dev_get_drvdata(dev);
154 static struct kmod_test_device *misc_dev_to_test_dev(struct miscdevice *misc_dev)
156 return container_of(misc_dev, struct kmod_test_device, misc_dev);
159 static struct kmod_test_device *dev_to_test_dev(struct device *dev)
161 struct miscdevice *misc_dev;
163 misc_dev = dev_to_misc_dev(dev);
165 return misc_dev_to_test_dev(misc_dev);
168 /* Must run with thread_mutex held */
169 static void kmod_test_done_check(struct kmod_test_device *test_dev,
172 struct test_config *config = &test_dev->config;
175 dev_dbg(test_dev->dev, "Done thread count: %u\n", test_dev->done);
177 if (test_dev->done == config->num_threads) {
178 dev_info(test_dev->dev, "Done: %u threads have all run now\n",
180 dev_info(test_dev->dev, "Last thread to run: %u\n", idx);
181 complete(&test_dev->kthreads_done);
185 static void test_kmod_put_module(struct kmod_test_device_info *info)
187 struct kmod_test_device *test_dev = info->test_dev;
188 struct test_config *config = &test_dev->config;
190 if (!info->need_mod_put)
193 switch (config->test_case) {
194 case TEST_KMOD_DRIVER:
196 case TEST_KMOD_FS_TYPE:
197 if (info->fs_sync && info->fs_sync->owner)
198 module_put(info->fs_sync->owner);
204 info->need_mod_put = true;
207 static int run_request(void *data)
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;
213 switch (config->test_case) {
214 case TEST_KMOD_DRIVER:
215 info->ret_sync = request_module("%s", config->test_driver);
217 case TEST_KMOD_FS_TYPE:
218 info->fs_sync = get_fs_type(config->test_fs);
219 info->need_mod_put = true;
222 /* __trigger_config_run() already checked for test sanity */
227 dev_dbg(test_dev->dev, "Ran thread %u\n", info->thread_idx);
229 test_kmod_put_module(info);
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);
239 static int tally_work_test(struct kmod_test_device_info *info)
241 struct kmod_test_device *test_dev = info->test_dev;
242 struct test_config *config = &test_dev->config;
245 switch (config->test_case) {
246 case TEST_KMOD_DRIVER:
248 * Only capture errors, if one is found that's
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);
257 case TEST_KMOD_FS_TYPE:
258 /* For now we make this simple */
261 dev_info(test_dev->dev, "Sync thread %u fs: %s\n",
262 info->thread_idx, info->fs_sync ? config->test_fs :
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.
276 * If this ran it means *all* tasks were created fine and we
277 * are now just collecting results.
279 * Only propagate errors, do not override with a subsequent success case.
281 static void tally_up_work(struct kmod_test_device *test_dev)
283 struct test_config *config = &test_dev->config;
284 struct kmod_test_device_info *info;
289 mutex_lock(&test_dev->thread_mutex);
291 dev_info(test_dev->dev, "Results:\n");
293 for (idx=0; idx < config->num_threads; idx++) {
294 info = &test_dev->info[idx];
295 ret = tally_work_test(info);
301 * Note: request_module() returns 256 for a module not found even
302 * though modprobe itself returns 1.
304 config->test_result = err_ret;
306 mutex_unlock(&test_dev->thread_mutex);
309 static int try_one_request(struct kmod_test_device *test_dev, unsigned int idx)
311 struct kmod_test_device_info *info = &test_dev->info[idx];
312 int fail_ret = -ENOMEM;
314 mutex_lock(&test_dev->thread_mutex);
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);
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;
327 dev_dbg(test_dev->dev, "Kicked off thread %u\n", idx);
329 mutex_unlock(&test_dev->thread_mutex);
334 info->ret_sync = fail_ret;
335 mutex_unlock(&test_dev->thread_mutex);
340 static void test_dev_kmod_stop_tests(struct kmod_test_device *test_dev)
342 struct test_config *config = &test_dev->config;
343 struct kmod_test_device_info *info;
346 dev_info(test_dev->dev, "Ending request_module() tests\n");
348 mutex_lock(&test_dev->thread_mutex);
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);
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.
365 if (info->task_sync && info->need_mod_put)
366 test_kmod_put_module(info);
369 mutex_unlock(&test_dev->thread_mutex);
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.
378 static int try_requests(struct kmod_test_device *test_dev)
380 struct test_config *config = &test_dev->config;
383 bool any_error = false;
385 for (idx=0; idx < config->num_threads; idx++) {
386 if (test_dev->test_is_oom) {
391 ret = try_one_request(test_dev, idx);
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);
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);
415 static int run_test_driver(struct kmod_test_device *test_dev)
417 struct test_config *config = &test_dev->config;
419 dev_info(test_dev->dev, "Test case: %s (%u)\n",
420 test_case_str(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);
429 return try_requests(test_dev);
432 static int run_test_fs_type(struct kmod_test_device *test_dev)
434 struct test_config *config = &test_dev->config;
436 dev_info(test_dev->dev, "Test case: %s (%u)\n",
437 test_case_str(config->test_case),
439 dev_info(test_dev->dev, "Test filesystem to load: %s\n",
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);
446 return try_requests(test_dev);
449 static ssize_t config_show(struct device *dev,
450 struct device_attribute *attr,
453 struct kmod_test_device *test_dev = dev_to_test_dev(dev);
454 struct test_config *config = &test_dev->config;
457 mutex_lock(&test_dev->config_mutex);
459 len += snprintf(buf, PAGE_SIZE,
460 "Custom trigger configuration for: %s\n",
463 len += snprintf(buf+len, PAGE_SIZE - len,
464 "Number of threads:\t%u\n",
465 config->num_threads);
467 len += snprintf(buf+len, PAGE_SIZE - len,
468 "Test_case:\t%s (%u)\n",
469 test_case_str(config->test_case),
472 if (config->test_driver)
473 len += snprintf(buf+len, PAGE_SIZE - len,
475 config->test_driver);
477 len += snprintf(buf+len, PAGE_SIZE - len,
481 len += snprintf(buf+len, PAGE_SIZE - len,
485 len += snprintf(buf+len, PAGE_SIZE - len,
488 mutex_unlock(&test_dev->config_mutex);
492 static DEVICE_ATTR_RO(config);
495 * This ensures we don't allow kicking threads through if our configuration
498 static int __trigger_config_run(struct kmod_test_device *test_dev)
500 struct test_config *config = &test_dev->config;
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);
510 dev_warn(test_dev->dev,
511 "Invalid test case requested: %u\n",
517 static int trigger_config_run(struct kmod_test_device *test_dev)
519 struct test_config *config = &test_dev->config;
522 mutex_lock(&test_dev->trigger_mutex);
523 mutex_lock(&test_dev->config_mutex);
525 ret = __trigger_config_run(test_dev);
528 dev_info(test_dev->dev, "General test result: %d\n",
529 config->test_result);
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.
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
547 mutex_unlock(&test_dev->config_mutex);
548 mutex_unlock(&test_dev->trigger_mutex);
554 trigger_config_store(struct device *dev,
555 struct device_attribute *attr,
556 const char *buf, size_t count)
558 struct kmod_test_device *test_dev = dev_to_test_dev(dev);
561 if (test_dev->test_is_oom)
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.
570 ret = trigger_config_run(test_dev);
571 if (unlikely(ret < 0))
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.
580 if (WARN_ON(ret > 0))
587 static DEVICE_ATTR_WO(trigger_config);
590 * XXX: move to kstrncpy() once merged.
592 * Users should use kfree_const() when freeing these.
594 static int __kstrncpy(char **dst, const char *name, size_t count, gfp_t gfp)
596 *dst = kstrndup(name, count, gfp);
602 static int config_copy_test_driver_name(struct test_config *config,
606 return __kstrncpy(&config->test_driver, name, count, GFP_KERNEL);
610 static int config_copy_test_fs(struct test_config *config, const char *name,
613 return __kstrncpy(&config->test_fs, name, count, GFP_KERNEL);
616 static void __kmod_config_free(struct test_config *config)
621 kfree_const(config->test_driver);
622 config->test_driver = NULL;
624 kfree_const(config->test_fs);
625 config->test_fs = NULL;
628 static void kmod_config_free(struct kmod_test_device *test_dev)
630 struct test_config *config;
635 config = &test_dev->config;
637 mutex_lock(&test_dev->config_mutex);
638 __kmod_config_free(config);
639 mutex_unlock(&test_dev->config_mutex);
642 static ssize_t config_test_driver_store(struct device *dev,
643 struct device_attribute *attr,
644 const char *buf, size_t count)
646 struct kmod_test_device *test_dev = dev_to_test_dev(dev);
647 struct test_config *config = &test_dev->config;
650 mutex_lock(&test_dev->config_mutex);
652 kfree_const(config->test_driver);
653 config->test_driver = NULL;
655 copied = config_copy_test_driver_name(config, buf, count);
656 mutex_unlock(&test_dev->config_mutex);
662 * As per sysfs_kf_seq_show() the buf is max PAGE_SIZE.
664 static ssize_t config_test_show_str(struct mutex *config_mutex,
670 mutex_lock(config_mutex);
671 len = snprintf(dst, PAGE_SIZE, "%s\n", src);
672 mutex_unlock(config_mutex);
677 static ssize_t config_test_driver_show(struct device *dev,
678 struct device_attribute *attr,
681 struct kmod_test_device *test_dev = dev_to_test_dev(dev);
682 struct test_config *config = &test_dev->config;
684 return config_test_show_str(&test_dev->config_mutex, buf,
685 config->test_driver);
687 static DEVICE_ATTR_RW(config_test_driver);
689 static ssize_t config_test_fs_store(struct device *dev,
690 struct device_attribute *attr,
691 const char *buf, size_t count)
693 struct kmod_test_device *test_dev = dev_to_test_dev(dev);
694 struct test_config *config = &test_dev->config;
697 mutex_lock(&test_dev->config_mutex);
699 kfree_const(config->test_fs);
700 config->test_fs = NULL;
702 copied = config_copy_test_fs(config, buf, count);
703 mutex_unlock(&test_dev->config_mutex);
708 static ssize_t config_test_fs_show(struct device *dev,
709 struct device_attribute *attr,
712 struct kmod_test_device *test_dev = dev_to_test_dev(dev);
713 struct test_config *config = &test_dev->config;
715 return config_test_show_str(&test_dev->config_mutex, buf,
718 static DEVICE_ATTR_RW(config_test_fs);
720 static int trigger_config_run_type(struct kmod_test_device *test_dev,
721 enum kmod_test_case test_case,
722 const char *test_str)
725 struct test_config *config = &test_dev->config;
727 mutex_lock(&test_dev->config_mutex);
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,
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,
743 mutex_unlock(&test_dev->config_mutex);
747 config->test_case = test_case;
749 mutex_unlock(&test_dev->config_mutex);
751 if (copied <= 0 || copied != strlen(test_str)) {
752 test_dev->test_is_oom = true;
756 test_dev->test_is_oom = false;
758 return trigger_config_run(test_dev);
761 static void free_test_dev_info(struct kmod_test_device *test_dev)
763 vfree(test_dev->info);
764 test_dev->info = NULL;
767 static int kmod_config_sync_info(struct kmod_test_device *test_dev)
769 struct test_config *config = &test_dev->config;
771 free_test_dev_info(test_dev);
773 vzalloc(array_size(sizeof(struct kmod_test_device_info),
774 config->num_threads));
782 * Old kernels may not have this, if you want to port this code to
783 * test it on older kernels.
785 #ifdef get_kmod_umh_limit
786 static unsigned int kmod_init_test_thread_limit(void)
788 return get_kmod_umh_limit();
791 static unsigned int kmod_init_test_thread_limit(void)
793 return TEST_START_NUM_THREADS;
797 static int __kmod_config_init(struct kmod_test_device *test_dev)
799 struct test_config *config = &test_dev->config;
800 int ret = -ENOMEM, copied;
802 __kmod_config_free(config);
804 copied = config_copy_test_driver_name(config, TEST_START_DRIVER,
805 strlen(TEST_START_DRIVER));
806 if (copied != strlen(TEST_START_DRIVER))
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))
814 config->num_threads = kmod_init_test_thread_limit();
815 config->test_result = 0;
816 config->test_case = TEST_START_TEST_CASE;
818 ret = kmod_config_sync_info(test_dev);
822 test_dev->test_is_oom = false;
827 test_dev->test_is_oom = true;
828 WARN_ON(test_dev->test_is_oom);
830 __kmod_config_free(config);
835 static ssize_t reset_store(struct device *dev,
836 struct device_attribute *attr,
837 const char *buf, size_t count)
839 struct kmod_test_device *test_dev = dev_to_test_dev(dev);
842 mutex_lock(&test_dev->trigger_mutex);
843 mutex_lock(&test_dev->config_mutex);
845 ret = __kmod_config_init(test_dev);
848 dev_err(dev, "could not alloc settings for config trigger: %d\n",
853 dev_info(dev, "reset\n");
857 mutex_unlock(&test_dev->config_mutex);
858 mutex_unlock(&test_dev->trigger_mutex);
862 static DEVICE_ATTR_WO(reset);
864 static 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))
871 unsigned int old_val;
873 ret = kstrtouint(buf, 10, &val);
877 mutex_lock(&test_dev->config_mutex);
880 *(unsigned int *)config = val;
882 ret = test_sync(test_dev);
884 *(unsigned int *)config = old_val;
886 ret = test_sync(test_dev);
889 mutex_unlock(&test_dev->config_mutex);
893 mutex_unlock(&test_dev->config_mutex);
894 /* Always return full write size even if we didn't consume all */
898 static int test_dev_config_update_uint_range(struct kmod_test_device *test_dev,
899 const char *buf, size_t size,
900 unsigned int *config,
907 ret = kstrtouint(buf, 10, &val);
911 if (val < min || val > max)
914 mutex_lock(&test_dev->config_mutex);
916 mutex_unlock(&test_dev->config_mutex);
918 /* Always return full write size even if we didn't consume all */
922 static int test_dev_config_update_int(struct kmod_test_device *test_dev,
923 const char *buf, size_t size,
929 ret = kstrtoint(buf, 10, &val);
933 mutex_lock(&test_dev->config_mutex);
935 mutex_unlock(&test_dev->config_mutex);
936 /* Always return full write size even if we didn't consume all */
940 static ssize_t test_dev_config_show_int(struct kmod_test_device *test_dev,
946 mutex_lock(&test_dev->config_mutex);
948 mutex_unlock(&test_dev->config_mutex);
950 return snprintf(buf, PAGE_SIZE, "%d\n", val);
953 static ssize_t test_dev_config_show_uint(struct kmod_test_device *test_dev,
959 mutex_lock(&test_dev->config_mutex);
961 mutex_unlock(&test_dev->config_mutex);
963 return snprintf(buf, PAGE_SIZE, "%u\n", val);
966 static ssize_t test_result_store(struct device *dev,
967 struct device_attribute *attr,
968 const char *buf, size_t count)
970 struct kmod_test_device *test_dev = dev_to_test_dev(dev);
971 struct test_config *config = &test_dev->config;
973 return test_dev_config_update_int(test_dev, buf, count,
974 &config->test_result);
977 static ssize_t config_num_threads_store(struct device *dev,
978 struct device_attribute *attr,
979 const char *buf, size_t count)
981 struct kmod_test_device *test_dev = dev_to_test_dev(dev);
982 struct test_config *config = &test_dev->config;
984 return test_dev_config_update_uint_sync(test_dev, buf, count,
985 &config->num_threads,
986 kmod_config_sync_info);
989 static ssize_t config_num_threads_show(struct device *dev,
990 struct device_attribute *attr,
993 struct kmod_test_device *test_dev = dev_to_test_dev(dev);
994 struct test_config *config = &test_dev->config;
996 return test_dev_config_show_int(test_dev, buf, config->num_threads);
998 static DEVICE_ATTR_RW(config_num_threads);
1000 static ssize_t config_test_case_store(struct device *dev,
1001 struct device_attribute *attr,
1002 const char *buf, size_t count)
1004 struct kmod_test_device *test_dev = dev_to_test_dev(dev);
1005 struct test_config *config = &test_dev->config;
1007 return test_dev_config_update_uint_range(test_dev, buf, count,
1009 __TEST_KMOD_INVALID + 1,
1010 __TEST_KMOD_MAX - 1);
1013 static ssize_t config_test_case_show(struct device *dev,
1014 struct device_attribute *attr,
1017 struct kmod_test_device *test_dev = dev_to_test_dev(dev);
1018 struct test_config *config = &test_dev->config;
1020 return test_dev_config_show_uint(test_dev, buf, config->test_case);
1022 static DEVICE_ATTR_RW(config_test_case);
1024 static ssize_t test_result_show(struct device *dev,
1025 struct device_attribute *attr,
1028 struct kmod_test_device *test_dev = dev_to_test_dev(dev);
1029 struct test_config *config = &test_dev->config;
1031 return test_dev_config_show_int(test_dev, buf, config->test_result);
1033 static DEVICE_ATTR_RW(test_result);
1035 #define TEST_KMOD_DEV_ATTR(name) &dev_attr_##name.attr
1037 static struct attribute *test_dev_attrs[] = {
1038 TEST_KMOD_DEV_ATTR(trigger_config),
1039 TEST_KMOD_DEV_ATTR(config),
1040 TEST_KMOD_DEV_ATTR(reset),
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),
1051 ATTRIBUTE_GROUPS(test_dev);
1053 static int kmod_config_init(struct kmod_test_device *test_dev)
1057 mutex_lock(&test_dev->config_mutex);
1058 ret = __kmod_config_init(test_dev);
1059 mutex_unlock(&test_dev->config_mutex);
1064 static struct kmod_test_device *alloc_test_dev_kmod(int idx)
1067 struct kmod_test_device *test_dev;
1068 struct miscdevice *misc_dev;
1070 test_dev = vzalloc(sizeof(struct kmod_test_device));
1074 mutex_init(&test_dev->config_mutex);
1075 mutex_init(&test_dev->trigger_mutex);
1076 mutex_init(&test_dev->thread_mutex);
1078 init_completion(&test_dev->kthreads_done);
1080 ret = kmod_config_init(test_dev);
1082 pr_err("Cannot alloc kmod_config_init()\n");
1086 test_dev->dev_idx = idx;
1087 misc_dev = &test_dev->misc_dev;
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;
1095 misc_dev->groups = test_dev_groups;
1099 err_out_free_config:
1100 free_test_dev_info(test_dev);
1101 kmod_config_free(test_dev);
1109 static void free_test_dev_kmod(struct kmod_test_device *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);
1121 static struct kmod_test_device *register_test_dev_kmod(void)
1123 struct kmod_test_device *test_dev = NULL;
1126 mutex_lock(®_dev_mutex);
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");
1134 test_dev = alloc_test_dev_kmod(num_test_devs);
1138 ret = misc_register(&test_dev->misc_dev);
1140 pr_err("could not register misc device: %d\n", ret);
1141 free_test_dev_kmod(test_dev);
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");
1153 mutex_unlock(®_dev_mutex);
1159 static int __init test_kmod_init(void)
1161 struct kmod_test_device *test_dev;
1164 test_dev = register_test_dev_kmod();
1166 pr_err("Cannot add first test kmod device\n");
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.
1177 if (force_init_test) {
1178 ret = trigger_config_run_type(test_dev,
1179 TEST_KMOD_DRIVER, "tun");
1182 ret = trigger_config_run_type(test_dev,
1183 TEST_KMOD_FS_TYPE, "btrfs");
1190 late_initcall(test_kmod_init);
1193 void unregister_test_dev_kmod(struct kmod_test_device *test_dev)
1195 mutex_lock(&test_dev->trigger_mutex);
1196 mutex_lock(&test_dev->config_mutex);
1198 test_dev_kmod_stop_tests(test_dev);
1200 dev_info(test_dev->dev, "removing interface\n");
1201 misc_deregister(&test_dev->misc_dev);
1203 mutex_unlock(&test_dev->config_mutex);
1204 mutex_unlock(&test_dev->trigger_mutex);
1206 free_test_dev_kmod(test_dev);
1209 static void __exit test_kmod_exit(void)
1211 struct kmod_test_device *test_dev, *tmp;
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);
1218 mutex_unlock(®_dev_mutex);
1220 module_exit(test_kmod_exit);
1222 MODULE_AUTHOR("Luis R. Rodriguez <mcgrof@kernel.org>");
1223 MODULE_LICENSE("GPL");