2 # SPDX-License-Identifier: GPL-2.0-or-later OR copyleft-next-0.3.1
3 # Copyright (C) 2017 Luis R. Rodriguez <mcgrof@kernel.org>
5 # This is a stress test script for kmod, the kernel module loader. It uses
6 # test_kmod which exposes a series of knobs for the API for us so we can
7 # tweak each test in userspace rather than in kernelspace.
9 # The way kmod works is it uses the kernel's usermode helper API to eventually
10 # call /sbin/modprobe. It has a limit of the number of concurrent calls
11 # possible. The kernel interface to load modules is request_module(), however
12 # mount uses get_fs_type(). Both behave slightly differently, but the
13 # differences are important enough to test each call separately. For this
14 # reason test_kmod starts by providing tests for both calls.
16 # The test driver test_kmod assumes a series of defaults which you can
17 # override by exporting to your environment prior running this script.
18 # For instance this script assumes you do not have xfs loaded upon boot.
19 # If this is false, export DEFAULT_KMOD_FS="ext4" prior to running this
20 # script if the filesystem module you don't have loaded upon bootup
21 # is ext4 instead. Refer to allow_user_defaults() for a list of user
22 # override variables possible.
24 # You'll want at least 4 GiB of RAM to expect to run these tests
25 # without running out of memory on them. For other requirements refer
31 TEST_DRIVER
="test_${TEST_NAME}"
32 TEST_DIR
=$
(dirname $0)
36 # TEST_ID:TEST_COUNT:ENABLED
38 # TEST_ID: is the test id number
39 # TEST_COUNT: number of times we should run the test
40 # ENABLED: 1 if enabled, 0 otherwise
42 # Once these are enabled please leave them as-is. Write your own test,
43 # we have tons of space.
45 ALL_TESTS
="$ALL_TESTS 0002:3:1"
46 ALL_TESTS
="$ALL_TESTS 0003:1:1"
47 ALL_TESTS
="$ALL_TESTS 0004:1:1"
48 ALL_TESTS
="$ALL_TESTS 0005:10:1"
49 ALL_TESTS
="$ALL_TESTS 0006:10:1"
50 ALL_TESTS
="$ALL_TESTS 0007:5:1"
51 ALL_TESTS
="$ALL_TESTS 0008:150:1"
52 ALL_TESTS
="$ALL_TESTS 0009:150:1"
53 ALL_TESTS
="$ALL_TESTS 0010:1:1"
54 ALL_TESTS
="$ALL_TESTS 0011:1:1"
55 ALL_TESTS
="$ALL_TESTS 0012:1:1"
56 ALL_TESTS
="$ALL_TESTS 0013:1:1"
58 # Kselftest framework requirement - SKIP code is 4.
63 if [ ! -d $DIR ]; then
64 echo "$0: $DIR not present" >&2
65 echo "You must have the following enabled in your kernel:" >&2
66 cat $TEST_DIR/config
>&2
71 function allow_user_defaults
()
73 if [ -z $DEFAULT_KMOD_DRIVER ]; then
74 DEFAULT_KMOD_DRIVER
="test_module"
77 if [ -z $DEFAULT_KMOD_FS ]; then
81 if [ -z $PROC_DIR ]; then
82 PROC_DIR
="/proc/sys/kernel/"
85 if [ -z $MODPROBE_LIMIT ]; then
90 DIR
="/sys/devices/virtual/misc/${TEST_DRIVER}0/"
93 if [ -z $DEFAULT_NUM_TESTS ]; then
97 MODPROBE_LIMIT_FILE
="${PROC_DIR}/kmod-limit"
102 if ! which modprobe
2> /dev
/null
> /dev
/null
; then
103 echo "$0: You need modprobe installed" >&2
107 if ! which kmod
2> /dev
/null
> /dev
/null
; then
108 echo "$0: You need kmod installed" >&2
112 # kmod 19 has a bad bug where it returns 0 when modprobe
113 # gets called *even* if the module was not loaded due to
114 # some bad heuristics. For details see:
116 # A work around is possible in-kernel but its rather
118 KMOD_VERSION
=$
(kmod
--version |
awk '{print $3}')
119 if [[ $KMOD_VERSION -le 19 ]]; then
120 echo "$0: You need at least kmod 20" >&2
121 echo "kmod <= 19 is buggy, for details see:" >&2
122 echo "https://git.kernel.org/cgit/utils/kernel/kmod/kmod.git/commit/libkmod/libkmod-module.c?id=fd44a98ae2eb5eb32161088954ab21e58e19dfc4" >&2
127 if [ $uid -ne 0 ]; then
128 echo $msg must be run as root
>&2
133 function load_req_mod
()
135 trap "test_modprobe" EXIT
137 if [ ! -d $DIR ]; then
138 # Alanis: "Oh isn't it ironic?"
139 modprobe
$TEST_DRIVER
145 echo "$MODPROBE" > /proc
/sys
/kernel
/modprobe
146 echo "Test completed"
152 # kmod calls modprobe and upon of a module not found
153 # modprobe returns just 1... However in the kernel we
154 # *sometimes* see 256...
175 echo MODULE_NOT_FOUND
;;
190 config_set_test_case_driver
()
192 if ! echo -n 1 >$DIR/config_test_case
; then
193 echo "$0: Unable to set to test case to driver" >&2
198 config_set_test_case_fs
()
200 if ! echo -n 2 >$DIR/config_test_case
; then
201 echo "$0: Unable to set to test case to fs" >&2
208 if ! echo -n $1 >$DIR/config_num_threads
; then
209 echo "$0: Unable to set to number of threads" >&2
214 config_get_modprobe_limit
()
216 if [[ -f ${MODPROBE_LIMIT_FILE} ]] ; then
217 MODPROBE_LIMIT
=$
(cat $MODPROBE_LIMIT_FILE)
222 config_num_thread_limit_extra
()
224 MODPROBE_LIMIT
=$
(config_get_modprobe_limit
)
225 let EXTRA_LIMIT
=$MODPROBE_LIMIT+$1
226 config_num_threads
$EXTRA_LIMIT
229 # For special characters use printf directly,
230 # refer to kmod_test_0001
233 if ! echo -n $1 >$DIR/config_test_driver
; then
234 echo "$0: Unable to set driver" >&2
241 if ! echo -n $1 >$DIR/config_test_fs
; then
242 echo "$0: Unable to set driver" >&2
249 cat $DIR/config_test_driver
252 config_get_test_result
()
259 if ! echo -n "1" >"$DIR"/reset; then
260 echo "$0: reset should have worked" >&2
267 echo "----------------------------------------------------"
269 echo "----------------------------------------------------"
274 if ! echo -n "1" >"$DIR"/trigger_config
2>/dev
/null
; then
275 echo "$1: FAIL - loading should have worked"
279 echo "$1: OK! - loading kmod test"
282 config_trigger_want_fail
()
284 if echo "1" > $DIR/trigger_config
2>/dev
/null
; then
285 echo "$1: FAIL - test case was expected to fail"
289 echo "$1: OK! - kmod test case failed as expected"
292 config_expect_result
()
294 RC
=$
(config_get_test_result
)
295 RC_NAME
=$
(errno_val_to_name
$RC)
298 ERRNO
=$
(errno_name_to_val
$ERRNO_NAME)
300 if [[ $ERRNO_NAME = "-ERR_ANY" ]]; then
301 if [[ $RC -ge 0 ]]; then
302 echo "$1: FAIL, test expects $ERRNO_NAME - got $RC_NAME ($RC)" >&2
306 elif [[ $RC != $ERRNO ]]; then
307 echo "$1: FAIL, test expects $ERRNO_NAME ($ERRNO) - got $RC_NAME ($RC)" >&2
311 echo "$1: OK! - Return value: $RC ($RC_NAME), expected $ERRNO_NAME"
314 kmod_defaults_driver
()
317 modprobe
-r $DEFAULT_KMOD_DRIVER
318 config_set_driver
$DEFAULT_KMOD_DRIVER
324 modprobe
-r $DEFAULT_KMOD_FS
325 config_set_fs
$DEFAULT_KMOD_FS
326 config_set_test_case_fs
329 kmod_test_0001_driver
()
335 printf $NAME >"$DIR"/config_test_driver
336 config_trigger
${FUNCNAME[0]}
337 config_expect_result
${FUNCNAME[0]} MODULE_NOT_FOUND
346 printf $NAME >"$DIR"/config_test_fs
347 config_trigger
${FUNCNAME[0]}
348 config_expect_result
${FUNCNAME[0]} -EINVAL
353 kmod_test_0001_driver
357 kmod_test_0002_driver
()
359 NAME
="nope-$DEFAULT_KMOD_DRIVER"
362 config_set_driver
$NAME
364 config_trigger
${FUNCNAME[0]}
365 config_expect_result
${FUNCNAME[0]} MODULE_NOT_FOUND
370 NAME
="nope-$DEFAULT_KMOD_FS"
374 config_trigger
${FUNCNAME[0]}
375 config_expect_result
${FUNCNAME[0]} -EINVAL
380 kmod_test_0002_driver
388 config_trigger
${FUNCNAME[0]}
389 config_expect_result
${FUNCNAME[0]} SUCCESS
396 config_trigger
${FUNCNAME[0]}
397 config_expect_result
${FUNCNAME[0]} SUCCESS
403 config_trigger
${FUNCNAME[0]}
404 config_expect_result
${FUNCNAME[0]} SUCCESS
410 config_trigger
${FUNCNAME[0]}
411 config_expect_result
${FUNCNAME[0]} SUCCESS
423 MODPROBE_LIMIT
=$
(config_get_modprobe_limit
)
424 let EXTRA
=$MODPROBE_LIMIT/6
425 config_num_thread_limit_extra
$EXTRA
426 config_trigger
${FUNCNAME[0]}
427 config_expect_result
${FUNCNAME[0]} SUCCESS
433 MODPROBE_LIMIT
=$
(config_get_modprobe_limit
)
434 let EXTRA
=$MODPROBE_LIMIT/4
435 config_num_thread_limit_extra
$EXTRA
436 config_trigger
${FUNCNAME[0]}
437 config_expect_result
${FUNCNAME[0]} SUCCESS
444 echo "/KMOD_TEST_NONEXISTENT" > /proc
/sys
/kernel
/modprobe
445 config_trigger
${FUNCNAME[0]}
446 config_expect_result
${FUNCNAME[0]} -ENOENT
447 echo "$MODPROBE" > /proc
/sys
/kernel
/modprobe
454 # This causes the kernel to not even try executing modprobe. The error
455 # code is still -ENOENT like when modprobe doesn't exist, so we can't
456 # easily test for the exact difference. But this still is a useful test
457 # since there was a bug where request_module() returned 0 in this case.
458 echo > /proc
/sys
/kernel
/modprobe
459 config_trigger
${FUNCNAME[0]}
460 config_expect_result
${FUNCNAME[0]} -ENOENT
461 echo "$MODPROBE" > /proc
/sys
/kernel
/modprobe
464 kmod_check_visibility
()
469 modprobe
$DEFAULT_KMOD_DRIVER
471 local priv
=$
(eval $cmd)
472 local unpriv
=$
(capsh
--drop=CAP_SYSLOG
-- -c "$cmd")
474 if [ "$priv" = "$unpriv" ] || \
475 [ "${priv:0:3}" = "0x0" ] || \
476 [ "${unpriv:0:3}" != "0x0" ] ; then
477 echo "${FUNCNAME[0]}: FAIL, $name visible to unpriv: '$priv' vs '$unpriv'" >&2
480 echo "${FUNCNAME[0]}: OK!"
486 kmod_check_visibility
/proc
/modules \
487 "grep '^${DEFAULT_KMOD_DRIVER}\b' /proc/modules | awk '{print \$NF}'"
492 kmod_check_visibility
'/sys/module/*/sections/*' \
493 "cat /sys/module/${DEFAULT_KMOD_DRIVER}/sections/.*text | head -n1"
500 echo "TEST_ID x NUM_TEST"
501 echo "TEST_ID: Test ID"
502 echo "NUM_TESTS: Number of recommended times to run the test"
504 echo "0001 x $(get_test_count 0001) - Simple test - 1 thread for empty string"
505 echo "0002 x $(get_test_count 0002) - Simple test - 1 thread for modules/filesystems that do not exist"
506 echo "0003 x $(get_test_count 0003) - Simple test - 1 thread for get_fs_type() only"
507 echo "0004 x $(get_test_count 0004) - Simple test - 2 threads for get_fs_type() only"
508 echo "0005 x $(get_test_count 0005) - multithreaded tests with default setup - request_module() only"
509 echo "0006 x $(get_test_count 0006) - multithreaded tests with default setup - get_fs_type() only"
510 echo "0007 x $(get_test_count 0007) - multithreaded tests with default setup test request_module() and get_fs_type()"
511 echo "0008 x $(get_test_count 0008) - multithreaded - push kmod_concurrent over max_modprobes for request_module()"
512 echo "0009 x $(get_test_count 0009) - multithreaded - push kmod_concurrent over max_modprobes for get_fs_type()"
513 echo "0010 x $(get_test_count 0010) - test nonexistent modprobe path"
514 echo "0011 x $(get_test_count 0011) - test completely disabling module autoloading"
515 echo "0012 x $(get_test_count 0012) - test /proc/modules address visibility under CAP_SYSLOG"
516 echo "0013 x $(get_test_count 0013) - test /sys/module/*/sections/* visibility under CAP_SYSLOG"
521 NUM_TESTS
=$
(grep -o ' ' <<<"$ALL_TESTS" |
grep -c .
)
522 let NUM_TESTS
=$NUM_TESTS+1
523 MAX_TEST
=$
(printf "%04d\n" $NUM_TESTS)
524 echo "Usage: $0 [ -t <4-number-digit> ] | [ -w <4-number-digit> ] |"
525 echo " [ -s <4-number-digit> ] | [ -c <4-number-digit> <test- count>"
526 echo " [ all ] [ -h | --help ] [ -l ]"
528 echo "Valid tests: 0001-$MAX_TEST"
530 echo " all Runs all tests (default)"
531 echo " -t Run test ID the number amount of times is recommended"
532 echo " -w Watch test ID run until it runs into an error"
533 echo " -s Run test ID once"
534 echo " -c Run test ID x test-count number of times"
535 echo " -l List all test ID list"
536 echo " -h|--help Help"
538 echo "If an error every occurs execution will immediately terminate."
539 echo "If you are adding a new test try using -w <test-ID> first to"
540 echo "make sure the test passes a series of tests."
544 echo "${TEST_NAME}.sh -- executes all tests"
545 echo "${TEST_NAME}.sh -t 0008 -- Executes test ID 0008 number of times is recommended"
546 echo "${TEST_NAME}.sh -w 0008 -- Watch test ID 0008 run until an error occurs"
547 echo "${TEST_NAME}.sh -s 0008 -- Run test ID 0008 once"
548 echo "${TEST_NAME}.sh -c 0008 3 -- Run test ID 0008 three times"
557 if ! [[ $1 =~
$re ]]; then
562 function get_test_data
()
565 local field_num
=$
(echo $1 |
sed 's/^0*//')
566 echo $ALL_TESTS |
awk '{print $'$field_num'}'
569 function get_test_count
()
571 TEST_DATA
=$
(get_test_data
$1)
572 LAST_TWO
=${TEST_DATA#*:*}
576 function get_test_enabled
()
578 TEST_DATA
=$
(get_test_data
$1)
579 echo ${TEST_DATA#*:*:}
582 function run_all_tests
()
584 for i
in $ALL_TESTS ; do
586 ENABLED
=$
(get_test_enabled
$TEST_ID)
587 TEST_COUNT
=$
(get_test_count
$TEST_ID)
588 if [[ $ENABLED -eq "1" ]]; then
589 test_case
$TEST_ID $TEST_COUNT
596 if [ $# -ne 3 ]; then
600 echo "Running test: $2 - run #$1"
603 function watch_case
()
608 if [ $# -eq 1 ]; then
610 watch_log
$i ${TEST_NAME}_test_
$1
622 NUM_TESTS
=$DEFAULT_NUM_TESTS
623 if [ $# -eq 2 ]; then
628 while [ $i -lt $NUM_TESTS ]; do
630 watch_log
$i ${TEST_NAME}_test_
$1 noclear
631 RUN_TEST
=${TEST_NAME}_test_
$1
637 function parse_args
()
639 if [ $# -eq 0 ]; then
642 if [[ "$1" = "all" ]]; then
644 elif [[ "$1" = "-w" ]]; then
647 elif [[ "$1" = "-t" ]]; then
650 test_case
$1 $
(get_test_count
$1)
651 elif [[ "$1" = "-c" ]]; then
656 elif [[ "$1" = "-s" ]]; then
659 elif [[ "$1" = "-l" ]]; then
661 elif [[ "$1" = "-h" ||
"$1" = "--help" ]]; then
673 MODPROBE
=$
(</proc
/sys
/kernel
/modprobe
)
674 trap "test_finish" EXIT