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