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
54 * @TEST_KMOD_DRIVER: stress tests request_module()
55 * @TEST_KMOD_FS_TYPE: stress tests get_fs_type()
57 * If you add a test case, please be sure to review if you need to set
58 * @need_mod_put for your tests case.
61 __TEST_KMOD_INVALID = 0,
72 unsigned int num_threads;
73 enum kmod_test_case test_case;
77 struct kmod_test_device;
80 * struct kmod_test_device_info - thread info
82 * @ret_sync: return value if request_module() is used, sync request for
84 * @fs_sync: return value of get_fs_type() for @TEST_KMOD_FS_TYPE
85 * @thread_idx: thread ID
86 * @test_dev: test device test is being performed under
87 * @need_mod_put: Some tests (get_fs_type() is one) requires putting the module
88 * (module_put(fs_sync->owner)) when done, otherwise you will not be able
89 * to unload the respective modules and re-test. We use this to keep
90 * accounting of when we need this and to help out in case we need to
91 * error out and deal with module_put() on error.
93 struct kmod_test_device_info {
95 struct file_system_type *fs_sync;
96 struct task_struct *task_sync;
97 unsigned int thread_idx;
98 struct kmod_test_device *test_dev;
103 * struct kmod_test_device - test device to help test kmod
105 * @dev_idx: unique ID for test device
106 * @config: configuration for the test
107 * @misc_dev: we use a misc device under the hood
108 * @dev: pointer to misc_dev's own struct device
109 * @config_mutex: protects configuration of test
110 * @trigger_mutex: the test trigger can only be fired once at a time
111 * @thread_lock: protects @done count, and the @info per each thread
112 * @done: number of threads which have completed or failed
113 * @test_is_oom: when we run out of memory, use this to halt moving forward
114 * @kthreads_done: completion used to signal when all work is done
115 * @list: needed to be part of the reg_test_devs
116 * @info: array of info for each thread
118 struct kmod_test_device {
120 struct test_config config;
121 struct miscdevice misc_dev;
123 struct mutex config_mutex;
124 struct mutex trigger_mutex;
125 struct mutex thread_mutex;
130 struct completion kthreads_done;
131 struct list_head list;
133 struct kmod_test_device_info *info;
136 static const char *test_case_str(enum kmod_test_case test_case)
139 case TEST_KMOD_DRIVER:
140 return "TEST_KMOD_DRIVER";
141 case TEST_KMOD_FS_TYPE:
142 return "TEST_KMOD_FS_TYPE";
148 static struct miscdevice *dev_to_misc_dev(struct device *dev)
150 return dev_get_drvdata(dev);
153 static struct kmod_test_device *misc_dev_to_test_dev(struct miscdevice *misc_dev)
155 return container_of(misc_dev, struct kmod_test_device, misc_dev);
158 static struct kmod_test_device *dev_to_test_dev(struct device *dev)
160 struct miscdevice *misc_dev;
162 misc_dev = dev_to_misc_dev(dev);
164 return misc_dev_to_test_dev(misc_dev);
167 /* Must run with thread_mutex held */
168 static void kmod_test_done_check(struct kmod_test_device *test_dev,
171 struct test_config *config = &test_dev->config;
174 dev_dbg(test_dev->dev, "Done thread count: %u\n", test_dev->done);
176 if (test_dev->done == config->num_threads) {
177 dev_info(test_dev->dev, "Done: %u threads have all run now\n",
179 dev_info(test_dev->dev, "Last thread to run: %u\n", idx);
180 complete(&test_dev->kthreads_done);
184 static void test_kmod_put_module(struct kmod_test_device_info *info)
186 struct kmod_test_device *test_dev = info->test_dev;
187 struct test_config *config = &test_dev->config;
189 if (!info->need_mod_put)
192 switch (config->test_case) {
193 case TEST_KMOD_DRIVER:
195 case TEST_KMOD_FS_TYPE:
196 if (info->fs_sync && info->fs_sync->owner)
197 module_put(info->fs_sync->owner);
203 info->need_mod_put = true;
206 static int run_request(void *data)
208 struct kmod_test_device_info *info = data;
209 struct kmod_test_device *test_dev = info->test_dev;
210 struct test_config *config = &test_dev->config;
212 switch (config->test_case) {
213 case TEST_KMOD_DRIVER:
214 info->ret_sync = request_module("%s", config->test_driver);
216 case TEST_KMOD_FS_TYPE:
217 info->fs_sync = get_fs_type(config->test_fs);
218 info->need_mod_put = true;
221 /* __trigger_config_run() already checked for test sanity */
226 dev_dbg(test_dev->dev, "Ran thread %u\n", info->thread_idx);
228 test_kmod_put_module(info);
230 mutex_lock(&test_dev->thread_mutex);
231 info->task_sync = NULL;
232 kmod_test_done_check(test_dev, info->thread_idx);
233 mutex_unlock(&test_dev->thread_mutex);
238 static int tally_work_test(struct kmod_test_device_info *info)
240 struct kmod_test_device *test_dev = info->test_dev;
241 struct test_config *config = &test_dev->config;
244 switch (config->test_case) {
245 case TEST_KMOD_DRIVER:
247 * Only capture errors, if one is found that's
250 if (info->ret_sync != 0)
251 err_ret = info->ret_sync;
252 dev_info(test_dev->dev,
253 "Sync thread %d return status: %d\n",
254 info->thread_idx, info->ret_sync);
256 case TEST_KMOD_FS_TYPE:
257 /* For now we make this simple */
260 dev_info(test_dev->dev, "Sync thread %u fs: %s\n",
261 info->thread_idx, info->fs_sync ? config->test_fs :
272 * XXX: add result option to display if all errors did not match.
273 * For now we just keep any error code if one was found.
275 * If this ran it means *all* tasks were created fine and we
276 * are now just collecting results.
278 * Only propagate errors, do not override with a subsequent success case.
280 static void tally_up_work(struct kmod_test_device *test_dev)
282 struct test_config *config = &test_dev->config;
283 struct kmod_test_device_info *info;
288 mutex_lock(&test_dev->thread_mutex);
290 dev_info(test_dev->dev, "Results:\n");
292 for (idx=0; idx < config->num_threads; idx++) {
293 info = &test_dev->info[idx];
294 ret = tally_work_test(info);
300 * Note: request_module() returns 256 for a module not found even
301 * though modprobe itself returns 1.
303 config->test_result = err_ret;
305 mutex_unlock(&test_dev->thread_mutex);
308 static int try_one_request(struct kmod_test_device *test_dev, unsigned int idx)
310 struct kmod_test_device_info *info = &test_dev->info[idx];
311 int fail_ret = -ENOMEM;
313 mutex_lock(&test_dev->thread_mutex);
315 info->thread_idx = idx;
316 info->test_dev = test_dev;
317 info->task_sync = kthread_run(run_request, info, "%s-%u",
318 KBUILD_MODNAME, idx);
320 if (!info->task_sync || IS_ERR(info->task_sync)) {
321 test_dev->test_is_oom = true;
322 dev_err(test_dev->dev, "Setting up thread %u failed\n", idx);
323 info->task_sync = NULL;
326 dev_dbg(test_dev->dev, "Kicked off thread %u\n", idx);
328 mutex_unlock(&test_dev->thread_mutex);
333 info->ret_sync = fail_ret;
334 mutex_unlock(&test_dev->thread_mutex);
339 static void test_dev_kmod_stop_tests(struct kmod_test_device *test_dev)
341 struct test_config *config = &test_dev->config;
342 struct kmod_test_device_info *info;
345 dev_info(test_dev->dev, "Ending request_module() tests\n");
347 mutex_lock(&test_dev->thread_mutex);
349 for (i=0; i < config->num_threads; i++) {
350 info = &test_dev->info[i];
351 if (info->task_sync && !IS_ERR(info->task_sync)) {
352 dev_info(test_dev->dev,
353 "Stopping still-running thread %i\n", i);
354 kthread_stop(info->task_sync);
358 * info->task_sync is well protected, it can only be
359 * NULL or a pointer to a struct. If its NULL we either
360 * never ran, or we did and we completed the work. Completed
361 * tasks *always* put the module for us. This is a sanity
362 * check -- just in case.
364 if (info->task_sync && info->need_mod_put)
365 test_kmod_put_module(info);
368 mutex_unlock(&test_dev->thread_mutex);
372 * Only wait *iff* we did not run into any errors during all of our thread
373 * set up. If run into any issues we stop threads and just bail out with
374 * an error to the trigger. This also means we don't need any tally work
375 * for any threads which fail.
377 static int try_requests(struct kmod_test_device *test_dev)
379 struct test_config *config = &test_dev->config;
382 bool any_error = false;
384 for (idx=0; idx < config->num_threads; idx++) {
385 if (test_dev->test_is_oom) {
390 ret = try_one_request(test_dev, idx);
398 test_dev->test_is_oom = false;
399 dev_info(test_dev->dev,
400 "No errors were found while initializing threads\n");
401 wait_for_completion(&test_dev->kthreads_done);
402 tally_up_work(test_dev);
404 test_dev->test_is_oom = true;
405 dev_info(test_dev->dev,
406 "At least one thread failed to start, stop all work\n");
407 test_dev_kmod_stop_tests(test_dev);
414 static int run_test_driver(struct kmod_test_device *test_dev)
416 struct test_config *config = &test_dev->config;
418 dev_info(test_dev->dev, "Test case: %s (%u)\n",
419 test_case_str(config->test_case),
421 dev_info(test_dev->dev, "Test driver to load: %s\n",
422 config->test_driver);
423 dev_info(test_dev->dev, "Number of threads to run: %u\n",
424 config->num_threads);
425 dev_info(test_dev->dev, "Thread IDs will range from 0 - %u\n",
426 config->num_threads - 1);
428 return try_requests(test_dev);
431 static int run_test_fs_type(struct kmod_test_device *test_dev)
433 struct test_config *config = &test_dev->config;
435 dev_info(test_dev->dev, "Test case: %s (%u)\n",
436 test_case_str(config->test_case),
438 dev_info(test_dev->dev, "Test filesystem to load: %s\n",
440 dev_info(test_dev->dev, "Number of threads to run: %u\n",
441 config->num_threads);
442 dev_info(test_dev->dev, "Thread IDs will range from 0 - %u\n",
443 config->num_threads - 1);
445 return try_requests(test_dev);
448 static ssize_t config_show(struct device *dev,
449 struct device_attribute *attr,
452 struct kmod_test_device *test_dev = dev_to_test_dev(dev);
453 struct test_config *config = &test_dev->config;
456 mutex_lock(&test_dev->config_mutex);
458 len += snprintf(buf, PAGE_SIZE,
459 "Custom trigger configuration for: %s\n",
462 len += snprintf(buf+len, PAGE_SIZE - len,
463 "Number of threads:\t%u\n",
464 config->num_threads);
466 len += snprintf(buf+len, PAGE_SIZE - len,
467 "Test_case:\t%s (%u)\n",
468 test_case_str(config->test_case),
471 if (config->test_driver)
472 len += snprintf(buf+len, PAGE_SIZE - len,
474 config->test_driver);
476 len += snprintf(buf+len, PAGE_SIZE - len,
480 len += snprintf(buf+len, PAGE_SIZE - len,
484 len += snprintf(buf+len, PAGE_SIZE - len,
487 mutex_unlock(&test_dev->config_mutex);
491 static DEVICE_ATTR_RO(config);
494 * This ensures we don't allow kicking threads through if our configuration
497 static int __trigger_config_run(struct kmod_test_device *test_dev)
499 struct test_config *config = &test_dev->config;
503 switch (config->test_case) {
504 case TEST_KMOD_DRIVER:
505 return run_test_driver(test_dev);
506 case TEST_KMOD_FS_TYPE:
507 return run_test_fs_type(test_dev);
509 dev_warn(test_dev->dev,
510 "Invalid test case requested: %u\n",
516 static int trigger_config_run(struct kmod_test_device *test_dev)
518 struct test_config *config = &test_dev->config;
521 mutex_lock(&test_dev->trigger_mutex);
522 mutex_lock(&test_dev->config_mutex);
524 ret = __trigger_config_run(test_dev);
527 dev_info(test_dev->dev, "General test result: %d\n",
528 config->test_result);
531 * We must return 0 after a trigger even unless something went
532 * wrong with the setup of the test. If the test setup went fine
533 * then userspace must just check the result of config->test_result.
534 * One issue with relying on the return from a call in the kernel
535 * is if the kernel returns a positive value using this trigger
536 * will not return the value to userspace, it would be lost.
538 * By not relying on capturing the return value of tests we are using
539 * through the trigger it also us to run tests with set -e and only
540 * fail when something went wrong with the driver upon trigger
546 mutex_unlock(&test_dev->config_mutex);
547 mutex_unlock(&test_dev->trigger_mutex);
553 trigger_config_store(struct device *dev,
554 struct device_attribute *attr,
555 const char *buf, size_t count)
557 struct kmod_test_device *test_dev = dev_to_test_dev(dev);
560 if (test_dev->test_is_oom)
563 /* For all intents and purposes we don't care what userspace
564 * sent this trigger, we care only that we were triggered.
565 * We treat the return value only for caputuring issues with
566 * the test setup. At this point all the test variables should
567 * have been allocated so typically this should never fail.
569 ret = trigger_config_run(test_dev);
570 if (unlikely(ret < 0))
574 * Note: any return > 0 will be treated as success
575 * and the error value will not be available to userspace.
576 * Do not rely on trying to send to userspace a test value
577 * return value as positive return errors will be lost.
579 if (WARN_ON(ret > 0))
586 static DEVICE_ATTR_WO(trigger_config);
589 * XXX: move to kstrncpy() once merged.
591 * Users should use kfree_const() when freeing these.
593 static int __kstrncpy(char **dst, const char *name, size_t count, gfp_t gfp)
595 *dst = kstrndup(name, count, gfp);
601 static int config_copy_test_driver_name(struct test_config *config,
605 return __kstrncpy(&config->test_driver, name, count, GFP_KERNEL);
609 static int config_copy_test_fs(struct test_config *config, const char *name,
612 return __kstrncpy(&config->test_fs, name, count, GFP_KERNEL);
615 static void __kmod_config_free(struct test_config *config)
620 kfree_const(config->test_driver);
621 config->test_driver = NULL;
623 kfree_const(config->test_fs);
624 config->test_fs = NULL;
627 static void kmod_config_free(struct kmod_test_device *test_dev)
629 struct test_config *config;
634 config = &test_dev->config;
636 mutex_lock(&test_dev->config_mutex);
637 __kmod_config_free(config);
638 mutex_unlock(&test_dev->config_mutex);
641 static ssize_t config_test_driver_store(struct device *dev,
642 struct device_attribute *attr,
643 const char *buf, size_t count)
645 struct kmod_test_device *test_dev = dev_to_test_dev(dev);
646 struct test_config *config = &test_dev->config;
649 mutex_lock(&test_dev->config_mutex);
651 kfree_const(config->test_driver);
652 config->test_driver = NULL;
654 copied = config_copy_test_driver_name(config, buf, count);
655 mutex_unlock(&test_dev->config_mutex);
661 * As per sysfs_kf_seq_show() the buf is max PAGE_SIZE.
663 static ssize_t config_test_show_str(struct mutex *config_mutex,
669 mutex_lock(config_mutex);
670 len = snprintf(dst, PAGE_SIZE, "%s\n", src);
671 mutex_unlock(config_mutex);
676 static ssize_t config_test_driver_show(struct device *dev,
677 struct device_attribute *attr,
680 struct kmod_test_device *test_dev = dev_to_test_dev(dev);
681 struct test_config *config = &test_dev->config;
683 return config_test_show_str(&test_dev->config_mutex, buf,
684 config->test_driver);
686 static DEVICE_ATTR_RW(config_test_driver);
688 static ssize_t config_test_fs_store(struct device *dev,
689 struct device_attribute *attr,
690 const char *buf, size_t count)
692 struct kmod_test_device *test_dev = dev_to_test_dev(dev);
693 struct test_config *config = &test_dev->config;
696 mutex_lock(&test_dev->config_mutex);
698 kfree_const(config->test_fs);
699 config->test_fs = NULL;
701 copied = config_copy_test_fs(config, buf, count);
702 mutex_unlock(&test_dev->config_mutex);
707 static ssize_t config_test_fs_show(struct device *dev,
708 struct device_attribute *attr,
711 struct kmod_test_device *test_dev = dev_to_test_dev(dev);
712 struct test_config *config = &test_dev->config;
714 return config_test_show_str(&test_dev->config_mutex, buf,
717 static DEVICE_ATTR_RW(config_test_fs);
719 static int trigger_config_run_type(struct kmod_test_device *test_dev,
720 enum kmod_test_case test_case,
721 const char *test_str)
724 struct test_config *config = &test_dev->config;
726 mutex_lock(&test_dev->config_mutex);
729 case TEST_KMOD_DRIVER:
730 kfree_const(config->test_driver);
731 config->test_driver = NULL;
732 copied = config_copy_test_driver_name(config, test_str,
735 case TEST_KMOD_FS_TYPE:
736 kfree_const(config->test_fs);
737 config->test_fs = NULL;
738 copied = config_copy_test_fs(config, test_str,
742 mutex_unlock(&test_dev->config_mutex);
746 config->test_case = test_case;
748 mutex_unlock(&test_dev->config_mutex);
750 if (copied <= 0 || copied != strlen(test_str)) {
751 test_dev->test_is_oom = true;
755 test_dev->test_is_oom = false;
757 return trigger_config_run(test_dev);
760 static void free_test_dev_info(struct kmod_test_device *test_dev)
762 vfree(test_dev->info);
763 test_dev->info = NULL;
766 static int kmod_config_sync_info(struct kmod_test_device *test_dev)
768 struct test_config *config = &test_dev->config;
770 free_test_dev_info(test_dev);
772 vzalloc(array_size(sizeof(struct kmod_test_device_info),
773 config->num_threads));
781 * Old kernels may not have this, if you want to port this code to
782 * test it on older kernels.
784 #ifdef get_kmod_umh_limit
785 static unsigned int kmod_init_test_thread_limit(void)
787 return get_kmod_umh_limit();
790 static unsigned int kmod_init_test_thread_limit(void)
792 return TEST_START_NUM_THREADS;
796 static int __kmod_config_init(struct kmod_test_device *test_dev)
798 struct test_config *config = &test_dev->config;
799 int ret = -ENOMEM, copied;
801 __kmod_config_free(config);
803 copied = config_copy_test_driver_name(config, TEST_START_DRIVER,
804 strlen(TEST_START_DRIVER));
805 if (copied != strlen(TEST_START_DRIVER))
808 copied = config_copy_test_fs(config, TEST_START_TEST_FS,
809 strlen(TEST_START_TEST_FS));
810 if (copied != strlen(TEST_START_TEST_FS))
813 config->num_threads = kmod_init_test_thread_limit();
814 config->test_result = 0;
815 config->test_case = TEST_START_TEST_CASE;
817 ret = kmod_config_sync_info(test_dev);
821 test_dev->test_is_oom = false;
826 test_dev->test_is_oom = true;
827 WARN_ON(test_dev->test_is_oom);
829 __kmod_config_free(config);
834 static ssize_t reset_store(struct device *dev,
835 struct device_attribute *attr,
836 const char *buf, size_t count)
838 struct kmod_test_device *test_dev = dev_to_test_dev(dev);
841 mutex_lock(&test_dev->trigger_mutex);
842 mutex_lock(&test_dev->config_mutex);
844 ret = __kmod_config_init(test_dev);
847 dev_err(dev, "could not alloc settings for config trigger: %d\n",
852 dev_info(dev, "reset\n");
856 mutex_unlock(&test_dev->config_mutex);
857 mutex_unlock(&test_dev->trigger_mutex);
861 static DEVICE_ATTR_WO(reset);
863 static int test_dev_config_update_uint_sync(struct kmod_test_device *test_dev,
864 const char *buf, size_t size,
865 unsigned int *config,
866 int (*test_sync)(struct kmod_test_device *test_dev))
870 unsigned int old_val;
872 ret = kstrtouint(buf, 10, &val);
876 mutex_lock(&test_dev->config_mutex);
879 *(unsigned int *)config = val;
881 ret = test_sync(test_dev);
883 *(unsigned int *)config = old_val;
885 ret = test_sync(test_dev);
888 mutex_unlock(&test_dev->config_mutex);
892 mutex_unlock(&test_dev->config_mutex);
893 /* Always return full write size even if we didn't consume all */
897 static int test_dev_config_update_uint_range(struct kmod_test_device *test_dev,
898 const char *buf, size_t size,
899 unsigned int *config,
906 ret = kstrtouint(buf, 10, &val);
910 if (val < min || val > max)
913 mutex_lock(&test_dev->config_mutex);
915 mutex_unlock(&test_dev->config_mutex);
917 /* Always return full write size even if we didn't consume all */
921 static int test_dev_config_update_int(struct kmod_test_device *test_dev,
922 const char *buf, size_t size,
928 ret = kstrtoint(buf, 10, &val);
932 mutex_lock(&test_dev->config_mutex);
934 mutex_unlock(&test_dev->config_mutex);
935 /* Always return full write size even if we didn't consume all */
939 static ssize_t test_dev_config_show_int(struct kmod_test_device *test_dev,
945 mutex_lock(&test_dev->config_mutex);
947 mutex_unlock(&test_dev->config_mutex);
949 return snprintf(buf, PAGE_SIZE, "%d\n", val);
952 static ssize_t test_dev_config_show_uint(struct kmod_test_device *test_dev,
958 mutex_lock(&test_dev->config_mutex);
960 mutex_unlock(&test_dev->config_mutex);
962 return snprintf(buf, PAGE_SIZE, "%u\n", val);
965 static ssize_t test_result_store(struct device *dev,
966 struct device_attribute *attr,
967 const char *buf, size_t count)
969 struct kmod_test_device *test_dev = dev_to_test_dev(dev);
970 struct test_config *config = &test_dev->config;
972 return test_dev_config_update_int(test_dev, buf, count,
973 &config->test_result);
976 static ssize_t config_num_threads_store(struct device *dev,
977 struct device_attribute *attr,
978 const char *buf, size_t count)
980 struct kmod_test_device *test_dev = dev_to_test_dev(dev);
981 struct test_config *config = &test_dev->config;
983 return test_dev_config_update_uint_sync(test_dev, buf, count,
984 &config->num_threads,
985 kmod_config_sync_info);
988 static ssize_t config_num_threads_show(struct device *dev,
989 struct device_attribute *attr,
992 struct kmod_test_device *test_dev = dev_to_test_dev(dev);
993 struct test_config *config = &test_dev->config;
995 return test_dev_config_show_int(test_dev, buf, config->num_threads);
997 static DEVICE_ATTR_RW(config_num_threads);
999 static ssize_t config_test_case_store(struct device *dev,
1000 struct device_attribute *attr,
1001 const char *buf, size_t count)
1003 struct kmod_test_device *test_dev = dev_to_test_dev(dev);
1004 struct test_config *config = &test_dev->config;
1006 return test_dev_config_update_uint_range(test_dev, buf, count,
1008 __TEST_KMOD_INVALID + 1,
1009 __TEST_KMOD_MAX - 1);
1012 static ssize_t config_test_case_show(struct device *dev,
1013 struct device_attribute *attr,
1016 struct kmod_test_device *test_dev = dev_to_test_dev(dev);
1017 struct test_config *config = &test_dev->config;
1019 return test_dev_config_show_uint(test_dev, buf, config->test_case);
1021 static DEVICE_ATTR_RW(config_test_case);
1023 static ssize_t test_result_show(struct device *dev,
1024 struct device_attribute *attr,
1027 struct kmod_test_device *test_dev = dev_to_test_dev(dev);
1028 struct test_config *config = &test_dev->config;
1030 return test_dev_config_show_int(test_dev, buf, config->test_result);
1032 static DEVICE_ATTR_RW(test_result);
1034 #define TEST_KMOD_DEV_ATTR(name) &dev_attr_##name.attr
1036 static struct attribute *test_dev_attrs[] = {
1037 TEST_KMOD_DEV_ATTR(trigger_config),
1038 TEST_KMOD_DEV_ATTR(config),
1039 TEST_KMOD_DEV_ATTR(reset),
1041 TEST_KMOD_DEV_ATTR(config_test_driver),
1042 TEST_KMOD_DEV_ATTR(config_test_fs),
1043 TEST_KMOD_DEV_ATTR(config_num_threads),
1044 TEST_KMOD_DEV_ATTR(config_test_case),
1045 TEST_KMOD_DEV_ATTR(test_result),
1050 ATTRIBUTE_GROUPS(test_dev);
1052 static int kmod_config_init(struct kmod_test_device *test_dev)
1056 mutex_lock(&test_dev->config_mutex);
1057 ret = __kmod_config_init(test_dev);
1058 mutex_unlock(&test_dev->config_mutex);
1063 static struct kmod_test_device *alloc_test_dev_kmod(int idx)
1066 struct kmod_test_device *test_dev;
1067 struct miscdevice *misc_dev;
1069 test_dev = vzalloc(sizeof(struct kmod_test_device));
1073 mutex_init(&test_dev->config_mutex);
1074 mutex_init(&test_dev->trigger_mutex);
1075 mutex_init(&test_dev->thread_mutex);
1077 init_completion(&test_dev->kthreads_done);
1079 ret = kmod_config_init(test_dev);
1081 pr_err("Cannot alloc kmod_config_init()\n");
1085 test_dev->dev_idx = idx;
1086 misc_dev = &test_dev->misc_dev;
1088 misc_dev->minor = MISC_DYNAMIC_MINOR;
1089 misc_dev->name = kasprintf(GFP_KERNEL, "test_kmod%d", idx);
1090 if (!misc_dev->name) {
1091 pr_err("Cannot alloc misc_dev->name\n");
1092 goto err_out_free_config;
1094 misc_dev->groups = test_dev_groups;
1098 err_out_free_config:
1099 free_test_dev_info(test_dev);
1100 kmod_config_free(test_dev);
1108 static void free_test_dev_kmod(struct kmod_test_device *test_dev)
1111 kfree_const(test_dev->misc_dev.name);
1112 test_dev->misc_dev.name = NULL;
1113 free_test_dev_info(test_dev);
1114 kmod_config_free(test_dev);
1120 static struct kmod_test_device *register_test_dev_kmod(void)
1122 struct kmod_test_device *test_dev = NULL;
1125 mutex_lock(®_dev_mutex);
1127 /* int should suffice for number of devices, test for wrap */
1128 if (num_test_devs + 1 == INT_MAX) {
1129 pr_err("reached limit of number of test devices\n");
1133 test_dev = alloc_test_dev_kmod(num_test_devs);
1137 ret = misc_register(&test_dev->misc_dev);
1139 pr_err("could not register misc device: %d\n", ret);
1140 free_test_dev_kmod(test_dev);
1145 test_dev->dev = test_dev->misc_dev.this_device;
1146 list_add_tail(&test_dev->list, ®_test_devs);
1147 dev_info(test_dev->dev, "interface ready\n");
1152 mutex_unlock(®_dev_mutex);
1158 static int __init test_kmod_init(void)
1160 struct kmod_test_device *test_dev;
1163 test_dev = register_test_dev_kmod();
1165 pr_err("Cannot add first test kmod device\n");
1170 * With some work we might be able to gracefully enable
1171 * testing with this driver built-in, for now this seems
1172 * rather risky. For those willing to try have at it,
1173 * and enable the below. Good luck! If that works, try
1174 * lowering the init level for more fun.
1176 if (force_init_test) {
1177 ret = trigger_config_run_type(test_dev,
1178 TEST_KMOD_DRIVER, "tun");
1181 ret = trigger_config_run_type(test_dev,
1182 TEST_KMOD_FS_TYPE, "btrfs");
1189 late_initcall(test_kmod_init);
1192 void unregister_test_dev_kmod(struct kmod_test_device *test_dev)
1194 mutex_lock(&test_dev->trigger_mutex);
1195 mutex_lock(&test_dev->config_mutex);
1197 test_dev_kmod_stop_tests(test_dev);
1199 dev_info(test_dev->dev, "removing interface\n");
1200 misc_deregister(&test_dev->misc_dev);
1202 mutex_unlock(&test_dev->config_mutex);
1203 mutex_unlock(&test_dev->trigger_mutex);
1205 free_test_dev_kmod(test_dev);
1208 static void __exit test_kmod_exit(void)
1210 struct kmod_test_device *test_dev, *tmp;
1212 mutex_lock(®_dev_mutex);
1213 list_for_each_entry_safe(test_dev, tmp, ®_test_devs, list) {
1214 list_del(&test_dev->list);
1215 unregister_test_dev_kmod(test_dev);
1217 mutex_unlock(®_dev_mutex);
1219 module_exit(test_kmod_exit);
1221 MODULE_AUTHOR("Luis R. Rodriguez <mcgrof@kernel.org>");
1222 MODULE_LICENSE("GPL");