// SPDX-License-Identifier: GPL-2.0-or-later OR copyleft-next-0.3.1 /* * kmod stress test driver * * Copyright (C) 2017 Luis R. Rodriguez <mcgrof@kernel.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
*/
staticbool force_init_test = false;
module_param(force_init_test, bool_enable_only, 0444);
MODULE_PARM_DESC(force_init_test, "Force kicking a test immediately after driver loads"); staticchar *start_driver;
module_param(start_driver, charp, 0444);
MODULE_PARM_DESC(start_driver, "Module/driver to use for the testing after driver loads"); staticchar *start_test_fs;
module_param(start_test_fs, charp, 0444);
MODULE_PARM_DESC(start_test_fs, "File system to use for the testing after driver loads");
/* * num_test_devs actually represents the *next* ID of the next * device we will allow to create.
*/ staticint num_test_devs;
/** * enum kmod_test_case - linker table test case * @TEST_KMOD_DRIVER: stress tests request_module() * @TEST_KMOD_FS_TYPE: stress tests get_fs_type() * * If you add a test case, please be sure to review if you need to set * @need_mod_put for your tests case.
*/ enum kmod_test_case { /* private: */
__TEST_KMOD_INVALID = 0, /* public: */
/** * struct 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 * @task_sync: kthread's task_struct or %NULL if not running * @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; unsignedint thread_idx; struct kmod_test_device *test_dev; bool need_mod_put;
};
/** * struct 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_mutex: 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;
/* Must run with thread_mutex held */ staticvoid kmod_test_done_check(struct kmod_test_device *test_dev, unsignedint idx)
{ struct test_config *config = &test_dev->config;
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);
}
}
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 success case.
*/ staticvoid tally_up_work(struct kmod_test_device *test_dev)
{ struct test_config *config = &test_dev->config; struct kmod_test_device_info *info; unsignedint 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;
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.
*/ staticint try_requests(struct kmod_test_device *test_dev)
{ struct test_config *config = &test_dev->config; unsignedint 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;
}
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;
} static DEVICE_ATTR_RO(config);
/* * This ensures we don't allow kicking threads through if our configuration * is faulty.
*/ staticint __trigger_config_run(struct kmod_test_device *test_dev)
{ struct test_config *config = &test_dev->config;
test_dev->done = 0;
switch (config->test_case) { case TEST_KMOD_DRIVER: return run_test_driver(test_dev); case TEST_KMOD_FS_TYPE: if (!config->test_fs) {
dev_warn(test_dev->dev, "No fs type specified, can't run the test\n"); return -EINVAL;
} return run_test_fs_type(test_dev); default:
dev_warn(test_dev->dev, "Invalid test case requested: %u\n",
config->test_case); return -EINVAL;
}
}
ret = __trigger_config_run(test_dev); if (ret < 0) goto out;
dev_info(test_dev->dev, "General test result: %d\n",
config->test_result);
/* * We must return 0 after a trigger even unless something went * wrong with the setup of the test. If the test setup went fine * then userspace must just check the result of config->test_result. * One issue with relying on the return from a call in the kernel * is if the kernel returns a positive value using this trigger * will not return the value to userspace, it would be lost. * * By not relying on capturing the return value of tests we are using * through the trigger it also us to run tests with set -e and only * fail when something went wrong with the driver upon trigger * requests.
*/
ret = 0;
/* For all intents and purposes we don't care what userspace * sent this trigger, we care only that we were triggered. * We treat the return value only for caputuring issues with * the test setup. At this point all the test variables should * have been allocated so typically this should never fail.
*/
ret = trigger_config_run(test_dev); if (unlikely(ret < 0)) goto out;
/* * Note: any return > 0 will be treated as success * and the error value will not be available to userspace. * Do not rely on trying to send to userspace a test value * return value as positive return errors will be lost.
*/ if (WARN_ON(ret > 0)) return -EINVAL;
ret = count;
out: return ret;
} static DEVICE_ATTR_WO(trigger_config);
/* * XXX: move to kstrncpy() once merged. * * Users should use kfree_const() when freeing these.
*/ staticint __kstrncpy(char **dst, constchar *name, size_t count, gfp_t gfp)
{
*dst = kstrndup(name, count, gfp); if (!*dst) return -ENOSPC; return count;
}
/* * As per sysfs_kf_seq_show() the buf is max PAGE_SIZE.
*/ static ssize_t config_test_show_str(struct mutex *config_mutex, char *dst, char *src)
{ int len;
mutex_lock(config_mutex);
len = snprintf(dst, PAGE_SIZE, "%s\n", src);
mutex_unlock(config_mutex);
free_test_dev_info(test_dev);
test_dev->info =
vzalloc(array_size(sizeof(struct kmod_test_device_info),
config->num_threads)); if (!test_dev->info) return -ENOMEM;
return 0;
}
/* * Old kernels may not have this, if you want to port this code to * test it on older kernels.
*/ #ifdef get_kmod_umh_limit staticunsignedint kmod_init_test_thread_limit(void)
{ return get_kmod_umh_limit();
} #else staticunsignedint kmod_init_test_thread_limit(void)
{ return TEST_START_NUM_THREADS;
} #endif
/* Always return full write size even if we didn't consume all */ return size;
}
staticint test_dev_config_update_int(struct kmod_test_device *test_dev, constchar *buf, size_t size, int *config)
{ int val; int ret;
ret = kstrtoint(buf, 10, &val); if (ret) return ret;
mutex_lock(&test_dev->config_mutex);
*config = val;
mutex_unlock(&test_dev->config_mutex); /* Always return full write size even if we didn't consume all */ return size;
}
static ssize_t test_dev_config_show_int(struct kmod_test_device *test_dev, char *buf, int config)
{ int val;
mutex_lock(&test_dev->config_mutex);
val = config;
mutex_unlock(&test_dev->config_mutex);
/* int should suffice for number of devices, test for wrap */ if (num_test_devs + 1 == INT_MAX) {
pr_err("reached limit of number of test devices\n"); goto out;
}
test_dev = alloc_test_dev_kmod(num_test_devs); if (!test_dev) goto out;
ret = misc_register(&test_dev->misc_dev); if (ret) {
pr_err("could not register misc device: %d\n", ret);
free_test_dev_kmod(test_dev);
test_dev = NULL; goto out;
}
staticint __init test_kmod_init(void)
{ struct kmod_test_device *test_dev; int ret;
test_dev = register_test_dev_kmod(); if (!test_dev) {
pr_err("Cannot add first test kmod device\n"); return -ENODEV;
}
/* * With some work we might be able to gracefully enable * testing with this driver built-in, for now this seems * rather risky. For those willing to try have at it, * and enable the below. Good luck! If that works, try * lowering the init level for more fun.
*/ if (force_init_test) {
ret = trigger_config_run_type(test_dev, TEST_KMOD_DRIVER); if (WARN_ON(ret)) return ret;
ret = trigger_config_run_type(test_dev, TEST_KMOD_FS_TYPE); if (WARN_ON(ret)) return ret;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.