Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

lib/test_kmod: do not hardcode/depend on any filesystem

Right now test_kmod has hardcoded dependencies on btrfs/xfs. That is not
optimal since you end up needing to select/build them, but it is not
really required since other fs could be selected for the testing. Also,
we can't change the default/driver module used for testing on
initialization.

Thus make it more generic: introduce two module parameters (start_driver
and start_test_fs), which allow to select which modules/fs to use for the
testing on test_kmod initialization. Then it's up to the user to select
which modules/fs to use for testing based on his config. However, keep
test_module as required default.

This way, config/modules becomes selectable as when the testing is done
from selftests (userspace).

While at it, also change trigger_config_run_type, since at module
initialization we already set the defaults at __kmod_config_init and
should not need to do it again in test_kmod_init(), thus we can avoid to
again set test_driver/test_fs.

Link: https://lkml.kernel.org/r/20250418165047.702487-1-herton@redhat.com
Signed-off-by: Herton R. Krzesinski <herton@redhat.com>
Reviewed-by: Luis Chambelrain <mcgrof@kernel.org>
Cc: Daniel Gomez <da.gomez@samsung.com>
Cc: Nathan Chancellor <nathan@kernel.org>
Cc: Petr Pavlu <petr.pavlu@suse.com>
Cc: Sami Tolvanen <samitolvanen@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Herton R. Krzesinski and committed by
Andrew Morton
92f3c5a0 2a1c6158

+34 -41
-6
lib/Kconfig.debug
··· 2983 2983 config TEST_KMOD 2984 2984 tristate "kmod stress tester" 2985 2985 depends on m 2986 - depends on NETDEVICES && NET_CORE && INET # for TUN 2987 - depends on BLOCK 2988 - depends on PAGE_SIZE_LESS_THAN_256KB # for BTRFS 2989 2986 select TEST_LKM 2990 - select XFS_FS 2991 - select TUN 2992 - select BTRFS_FS 2993 2987 help 2994 2988 Test the kernel's module loading mechanism: kmod. kmod implements 2995 2989 support to load modules using the Linux kernel's usermode helper.
+34 -30
lib/test_kmod.c
··· 28 28 29 29 #define TEST_START_NUM_THREADS 50 30 30 #define TEST_START_DRIVER "test_module" 31 - #define TEST_START_TEST_FS "xfs" 32 31 #define TEST_START_TEST_CASE TEST_KMOD_DRIVER 33 32 34 - 35 33 static bool force_init_test = false; 36 - module_param(force_init_test, bool_enable_only, 0644); 34 + module_param(force_init_test, bool_enable_only, 0444); 37 35 MODULE_PARM_DESC(force_init_test, 38 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"); 39 45 40 46 /* 41 47 * For device allocation / registration ··· 514 508 case TEST_KMOD_DRIVER: 515 509 return run_test_driver(test_dev); 516 510 case TEST_KMOD_FS_TYPE: 511 + if (!config->test_fs) { 512 + dev_warn(test_dev->dev, 513 + "No fs type specified, can't run the test\n"); 514 + return -EINVAL; 515 + } 517 516 return run_test_fs_type(test_dev); 518 517 default: 519 518 dev_warn(test_dev->dev, ··· 732 721 static DEVICE_ATTR_RW(config_test_fs); 733 722 734 723 static int trigger_config_run_type(struct kmod_test_device *test_dev, 735 - enum kmod_test_case test_case, 736 - const char *test_str) 724 + enum kmod_test_case test_case) 737 725 { 738 - int copied = 0; 739 726 struct test_config *config = &test_dev->config; 740 727 741 728 mutex_lock(&test_dev->config_mutex); 742 729 743 730 switch (test_case) { 744 731 case TEST_KMOD_DRIVER: 745 - kfree_const(config->test_driver); 746 - config->test_driver = NULL; 747 - copied = config_copy_test_driver_name(config, test_str, 748 - strlen(test_str)); 749 732 break; 750 733 case TEST_KMOD_FS_TYPE: 751 - kfree_const(config->test_fs); 752 - config->test_fs = NULL; 753 - copied = config_copy_test_fs(config, test_str, 754 - strlen(test_str)); 734 + if (!config->test_fs) { 735 + mutex_unlock(&test_dev->config_mutex); 736 + return 0; 737 + } 755 738 break; 756 739 default: 757 740 mutex_unlock(&test_dev->config_mutex); ··· 755 750 config->test_case = test_case; 756 751 757 752 mutex_unlock(&test_dev->config_mutex); 758 - 759 - if (copied <= 0 || copied != strlen(test_str)) { 760 - test_dev->test_is_oom = true; 761 - return -ENOMEM; 762 - } 763 753 764 754 test_dev->test_is_oom = false; 765 755 ··· 800 800 static int __kmod_config_init(struct kmod_test_device *test_dev) 801 801 { 802 802 struct test_config *config = &test_dev->config; 803 + const char *test_start_driver = start_driver ? start_driver : 804 + TEST_START_DRIVER; 803 805 int ret = -ENOMEM, copied; 804 806 805 807 __kmod_config_free(config); 806 808 807 - copied = config_copy_test_driver_name(config, TEST_START_DRIVER, 808 - strlen(TEST_START_DRIVER)); 809 - if (copied != strlen(TEST_START_DRIVER)) 809 + copied = config_copy_test_driver_name(config, test_start_driver, 810 + strlen(test_start_driver)); 811 + if (copied != strlen(test_start_driver)) 810 812 goto err_out; 811 813 812 - copied = config_copy_test_fs(config, TEST_START_TEST_FS, 813 - strlen(TEST_START_TEST_FS)); 814 - if (copied != strlen(TEST_START_TEST_FS)) 815 - goto err_out; 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 + } 816 821 817 822 config->num_threads = kmod_init_test_thread_limit(); 818 823 config->test_result = 0; ··· 1183 1178 * lowering the init level for more fun. 1184 1179 */ 1185 1180 if (force_init_test) { 1186 - ret = trigger_config_run_type(test_dev, 1187 - TEST_KMOD_DRIVER, "tun"); 1181 + ret = trigger_config_run_type(test_dev, TEST_KMOD_DRIVER); 1188 1182 if (WARN_ON(ret)) 1189 1183 return ret; 1190 - ret = trigger_config_run_type(test_dev, 1191 - TEST_KMOD_FS_TYPE, "btrfs"); 1184 + 1185 + ret = trigger_config_run_type(test_dev, TEST_KMOD_FS_TYPE); 1192 1186 if (WARN_ON(ret)) 1193 1187 return ret; 1194 1188 }
-5
tools/testing/selftests/kmod/config
··· 1 1 CONFIG_TEST_KMOD=m 2 2 CONFIG_TEST_LKM=m 3 - CONFIG_XFS_FS=m 4 - 5 - # For the module parameter force_init_test is used 6 - CONFIG_TUN=m 7 - CONFIG_BTRFS_FS=m