1246 lines · 30.4 KB · C
RawDownload
/* * kmod stress test driver * * Copyright (C) 2017 Luis R. Rodriguez <mcgrof@kernel.org> * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free * Software Foundation; either version 2 of the License, or at your option any * later version; or, when distributed separately from the Linux kernel or * when incorporated into other software packages, subject to the following * license: * * This program is free software; you can redistribute it and/or modify it * under the terms of copyleft-next (version 0.3.1 or later) as published * at http://copyleft-next.org/. */#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt /* * This driver provides an interface to trigger and test the kernel's * module loader through a series of configurations and a few triggers. * To test this driver use the following script as root: * * tools/testing/selftests/kmod/kmod.sh --help */ #include <linux/kernel.h>#include <linux/module.h>#include <linux/kmod.h>#include <linux/printk.h>#include <linux/kthread.h>#include <linux/sched.h>#include <linux/fs.h>#include <linux/miscdevice.h>#include <linux/vmalloc.h>#include <linux/slab.h>#include <linux/device.h> #define TEST_START_NUM_THREADS	50#define TEST_START_DRIVER	"test_module"#define TEST_START_TEST_FS	"xfs"#define TEST_START_TEST_CASE	TEST_KMOD_DRIVER  static bool force_init_test = false;module_param(force_init_test, bool_enable_only, 0644);MODULE_PARM_DESC(force_init_test,		 "Force kicking a test immediately after driver loads"); /* * For device allocation / registration */static DEFINE_MUTEX(reg_dev_mutex);static LIST_HEAD(reg_test_devs); /* * num_test_devs actually represents the *next* ID of the next * device we will allow to create. */static int num_test_devs; /** * enum kmod_test_case - linker table test case * * If you add a  test case, please be sure to review if you need to se * @need_mod_put for your tests case. * * @TEST_KMOD_DRIVER: stress tests request_module() * @TEST_KMOD_FS_TYPE: stress tests get_fs_type() */enum kmod_test_case {	__TEST_KMOD_INVALID = 0, 	TEST_KMOD_DRIVER,	TEST_KMOD_FS_TYPE, 	__TEST_KMOD_MAX,}; struct test_config {	char *test_driver;	char *test_fs;	unsigned int num_threads;	enum kmod_test_case test_case;	int test_result;}; struct kmod_test_device; /** * kmod_test_device_info - thread info * * @ret_sync: return value if request_module() is used, sync request for * 	@TEST_KMOD_DRIVER * @fs_sync: return value of get_fs_type() for @TEST_KMOD_FS_TYPE * @thread_idx: thread ID * @test_dev: test device test is being performed under * @need_mod_put: Some tests (get_fs_type() is one) requires putting the module *	(module_put(fs_sync->owner)) when done, otherwise you will not be able *	to unload the respective modules and re-test. We use this to keep *	accounting of when we need this and to help out in case we need to *	error out and deal with module_put() on error. */struct kmod_test_device_info {	int ret_sync;	struct file_system_type *fs_sync;	struct task_struct *task_sync;	unsigned int thread_idx;	struct kmod_test_device *test_dev;	bool need_mod_put;}; /** * kmod_test_device - test device to help test kmod * * @dev_idx: unique ID for test device * @config: configuration for the test * @misc_dev: we use a misc device under the hood * @dev: pointer to misc_dev's own struct device * @config_mutex: protects configuration of test * @trigger_mutex: the test trigger can only be fired once at a time * @thread_lock: protects @done count, and the @info per each thread * @done: number of threads which have completed or failed * @test_is_oom: when we run out of memory, use this to halt moving forward * @kthreads_done: completion used to signal when all work is done * @list: needed to be part of the reg_test_devs * @info: array of info for each thread */struct kmod_test_device {	int dev_idx;	struct test_config config;	struct miscdevice misc_dev;	struct device *dev;	struct mutex config_mutex;	struct mutex trigger_mutex;	struct mutex thread_mutex; 	unsigned int done; 	bool test_is_oom;	struct completion kthreads_done;	struct list_head list; 	struct kmod_test_device_info *info;}; static const char *test_case_str(enum kmod_test_case test_case){	switch (test_case) {	case TEST_KMOD_DRIVER:		return "TEST_KMOD_DRIVER";	case TEST_KMOD_FS_TYPE:		return "TEST_KMOD_FS_TYPE";	default:		return "invalid";	}} static struct miscdevice *dev_to_misc_dev(struct device *dev){	return dev_get_drvdata(dev);} static struct kmod_test_device *misc_dev_to_test_dev(struct miscdevice *misc_dev){	return container_of(misc_dev, struct kmod_test_device, misc_dev);} static struct kmod_test_device *dev_to_test_dev(struct device *dev){	struct miscdevice *misc_dev; 	misc_dev = dev_to_misc_dev(dev); 	return misc_dev_to_test_dev(misc_dev);} /* Must run with thread_mutex held */static void kmod_test_done_check(struct kmod_test_device *test_dev,				 unsigned int idx){	struct test_config *config = &test_dev->config; 	test_dev->done++;	dev_dbg(test_dev->dev, "Done thread count: %u\n", test_dev->done); 	if (test_dev->done == config->num_threads) {		dev_info(test_dev->dev, "Done: %u threads have all run now\n",			 test_dev->done);		dev_info(test_dev->dev, "Last thread to run: %u\n", idx);		complete(&test_dev->kthreads_done);	}} static void test_kmod_put_module(struct kmod_test_device_info *info){	struct kmod_test_device *test_dev = info->test_dev;	struct test_config *config = &test_dev->config; 	if (!info->need_mod_put)		return; 	switch (config->test_case) {	case TEST_KMOD_DRIVER:		break;	case TEST_KMOD_FS_TYPE:		if (info && info->fs_sync && info->fs_sync->owner)			module_put(info->fs_sync->owner);		break;	default:		BUG();	} 	info->need_mod_put = true;} static int run_request(void *data){	struct kmod_test_device_info *info = data;	struct kmod_test_device *test_dev = info->test_dev;	struct test_config *config = &test_dev->config; 	switch (config->test_case) {	case TEST_KMOD_DRIVER:		info->ret_sync = request_module("%s", config->test_driver);		break;	case TEST_KMOD_FS_TYPE:		info->fs_sync = get_fs_type(config->test_fs);		info->need_mod_put = true;		break;	default:		/* __trigger_config_run() already checked for test sanity */		BUG();		return -EINVAL;	} 	dev_dbg(test_dev->dev, "Ran thread %u\n", info->thread_idx); 	test_kmod_put_module(info); 	mutex_lock(&test_dev->thread_mutex);	info->task_sync = NULL;	kmod_test_done_check(test_dev, info->thread_idx);	mutex_unlock(&test_dev->thread_mutex); 	return 0;} static int tally_work_test(struct kmod_test_device_info *info){	struct kmod_test_device *test_dev = info->test_dev;	struct test_config *config = &test_dev->config;	int err_ret = 0; 	switch (config->test_case) {	case TEST_KMOD_DRIVER:		/*		 * Only capture errors, if one is found that's		 * enough, for now.		 */		if (info->ret_sync != 0)			err_ret = info->ret_sync;		dev_info(test_dev->dev,			 "Sync thread %d return status: %d\n",			 info->thread_idx, info->ret_sync);		break;	case TEST_KMOD_FS_TYPE:		/* For now we make this simple */		if (!info->fs_sync)			err_ret = -EINVAL;		dev_info(test_dev->dev, "Sync thread %u fs: %s\n",			 info->thread_idx, info->fs_sync ? config->test_fs :			 "NULL");		break;	default:		BUG();	} 	return err_ret;} /* * XXX: add result option to display if all errors did not match. * For now we just keep any error code if one was found. * * If this ran it means *all* tasks were created fine and we * are now just collecting results. * * Only propagate errors, do not override with a subsequent sucess case. */static void tally_up_work(struct kmod_test_device *test_dev){	struct test_config *config = &test_dev->config;	struct kmod_test_device_info *info;	unsigned int idx;	int err_ret = 0;	int ret = 0; 	mutex_lock(&test_dev->thread_mutex); 	dev_info(test_dev->dev, "Results:\n"); 	for (idx=0; idx < config->num_threads; idx++) {		info = &test_dev->info[idx];		ret = tally_work_test(info);		if (ret)			err_ret = ret;	} 	/*	 * Note: request_module() returns 256 for a module not found even	 * though modprobe itself returns 1.	 */	config->test_result = err_ret; 	mutex_unlock(&test_dev->thread_mutex);} static int try_one_request(struct kmod_test_device *test_dev, unsigned int idx){	struct kmod_test_device_info *info = &test_dev->info[idx];	int fail_ret = -ENOMEM; 	mutex_lock(&test_dev->thread_mutex); 	info->thread_idx = idx;	info->test_dev = test_dev;	info->task_sync = kthread_run(run_request, info, "%s-%u",				      KBUILD_MODNAME, idx); 	if (!info->task_sync || IS_ERR(info->task_sync)) {		test_dev->test_is_oom = true;		dev_err(test_dev->dev, "Setting up thread %u failed\n", idx);		info->task_sync = NULL;		goto err_out;	} else		dev_dbg(test_dev->dev, "Kicked off thread %u\n", idx); 	mutex_unlock(&test_dev->thread_mutex); 	return 0; err_out:	info->ret_sync = fail_ret;	mutex_unlock(&test_dev->thread_mutex); 	return fail_ret;} static void test_dev_kmod_stop_tests(struct kmod_test_device *test_dev){	struct test_config *config = &test_dev->config;	struct kmod_test_device_info *info;	unsigned int i; 	dev_info(test_dev->dev, "Ending request_module() tests\n"); 	mutex_lock(&test_dev->thread_mutex); 	for (i=0; i < config->num_threads; i++) {		info = &test_dev->info[i];		if (info->task_sync && !IS_ERR(info->task_sync)) {			dev_info(test_dev->dev,				 "Stopping still-running thread %i\n", i);			kthread_stop(info->task_sync);		} 		/*		 * info->task_sync is well protected, it can only be		 * NULL or a pointer to a struct. If its NULL we either		 * never ran, or we did and we completed the work. Completed		 * tasks *always* put the module for us. This is a sanity		 * check -- just in case.		 */		if (info->task_sync && info->need_mod_put)			test_kmod_put_module(info);	} 	mutex_unlock(&test_dev->thread_mutex);} /* * Only wait *iff* we did not run into any errors during all of our thread * set up. If run into any issues we stop threads and just bail out with * an error to the trigger. This also means we don't need any tally work * for any threads which fail. */static int try_requests(struct kmod_test_device *test_dev){	struct test_config *config = &test_dev->config;	unsigned int idx;	int ret;	bool any_error = false; 	for (idx=0; idx < config->num_threads; idx++) {		if (test_dev->test_is_oom) {			any_error = true;			break;		} 		ret = try_one_request(test_dev, idx);		if (ret) {			any_error = true;			break;		}	} 	if (!any_error) {		test_dev->test_is_oom = false;		dev_info(test_dev->dev,			 "No errors were found while initializing threads\n");		wait_for_completion(&test_dev->kthreads_done);		tally_up_work(test_dev);	} else {		test_dev->test_is_oom = true;		dev_info(test_dev->dev,			 "At least one thread failed to start, stop all work\n");		test_dev_kmod_stop_tests(test_dev);		return -ENOMEM;	} 	return 0;} static int run_test_driver(struct kmod_test_device *test_dev){	struct test_config *config = &test_dev->config; 	dev_info(test_dev->dev, "Test case: %s (%u)\n",		 test_case_str(config->test_case),		 config->test_case);	dev_info(test_dev->dev, "Test driver to load: %s\n",		 config->test_driver);	dev_info(test_dev->dev, "Number of threads to run: %u\n",		 config->num_threads);	dev_info(test_dev->dev, "Thread IDs will range from 0 - %u\n",		 config->num_threads - 1); 	return try_requests(test_dev);} static int run_test_fs_type(struct kmod_test_device *test_dev){	struct test_config *config = &test_dev->config; 	dev_info(test_dev->dev, "Test case: %s (%u)\n",		 test_case_str(config->test_case),		 config->test_case);	dev_info(test_dev->dev, "Test filesystem to load: %s\n",		 config->test_fs);	dev_info(test_dev->dev, "Number of threads to run: %u\n",		 config->num_threads);	dev_info(test_dev->dev, "Thread IDs will range from 0 - %u\n",		 config->num_threads - 1); 	return try_requests(test_dev);} static ssize_t config_show(struct device *dev,			   struct device_attribute *attr,			   char *buf){	struct kmod_test_device *test_dev = dev_to_test_dev(dev);	struct test_config *config = &test_dev->config;	int len = 0; 	mutex_lock(&test_dev->config_mutex); 	len += snprintf(buf, PAGE_SIZE,			"Custom trigger configuration for: %s\n",			dev_name(dev)); 	len += snprintf(buf+len, PAGE_SIZE - len,			"Number of threads:\t%u\n",			config->num_threads); 	len += snprintf(buf+len, PAGE_SIZE - len,			"Test_case:\t%s (%u)\n",			test_case_str(config->test_case),			config->test_case); 	if (config->test_driver)		len += snprintf(buf+len, PAGE_SIZE - len,				"driver:\t%s\n",				config->test_driver);	else		len += snprintf(buf+len, PAGE_SIZE - len,				"driver:\tEMPTY\n"); 	if (config->test_fs)		len += snprintf(buf+len, PAGE_SIZE - len,				"fs:\t%s\n",				config->test_fs);	else		len += snprintf(buf+len, PAGE_SIZE - len,				"fs:\tEMPTY\n"); 	mutex_unlock(&test_dev->config_mutex); 	return len;

Lines 1–500 of 1246.