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