Skip to content

Commit

Permalink
lib/test_sysctl: support testing of sysctl. boot parameter
Browse files Browse the repository at this point in the history
Testing is done by a new parameter debug.test_sysctl.boot_int which
defaults to 0 and it's expected that the tester passes a boot parameter
that sets it to 1.  The test checks if it's set to 1.

To distinguish true failure from parameter not being set, the test
checks /proc/cmdline for the expected parameter, and whether test_sysctl
is built-in and not a module.

[[email protected]: skip the new test if boot_int sysctl is not present]
  Link: http://lkml.kernel.org/r/[email protected]

Signed-off-by: Vlastimil Babka <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Cc: Alexey Dobriyan <[email protected]>
Cc: Christian Brauner <[email protected]>
Cc: David Rientjes <[email protected]>
Cc: "Eric W . Biederman" <[email protected]>
Cc: Greg Kroah-Hartman <[email protected]>
Cc: "Guilherme G . Piccoli" <[email protected]>
Cc: Iurii Zaikin <[email protected]>
Cc: Ivan Teterevkov <[email protected]>
Cc: Kees Cook <[email protected]>
Cc: Luis Chamberlain <[email protected]>
Cc: Masami Hiramatsu <[email protected]>
Cc: Matthew Wilcox <[email protected]>
Cc: Michal Hocko <[email protected]>
Cc: Michal Hocko <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Link: http://lkml.kernel.org/r/[email protected]
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
tehcaster authored and torvalds committed Jun 8, 2020
1 parent 4546cde commit 4f2f682
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/test_sysctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ struct test_sysctl_data {
int int_0002;
int int_0003[4];

int boot_int;

unsigned int uint_0001;

char string_0001[65];
Expand All @@ -61,6 +63,8 @@ static struct test_sysctl_data test_data = {
.int_0003[2] = 2,
.int_0003[3] = 3,

.boot_int = 0,

.uint_0001 = 314,

.string_0001 = "(none)",
Expand Down Expand Up @@ -91,6 +95,15 @@ static struct ctl_table test_table[] = {
.mode = 0644,
.proc_handler = proc_dointvec,
},
{
.procname = "boot_int",
.data = &test_data.boot_int,
.maxlen = sizeof(test_data.boot_int),
.mode = 0644,
.proc_handler = proc_dointvec,
.extra1 = SYSCTL_ZERO,
.extra2 = SYSCTL_ONE,
},
{
.procname = "uint_0001",
.data = &test_data.uint_0001,
Expand Down
42 changes: 42 additions & 0 deletions tools/testing/selftests/sysctl/sysctl.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ ALL_TESTS="$ALL_TESTS 0003:1:1:int_0002"
ALL_TESTS="$ALL_TESTS 0004:1:1:uint_0001"
ALL_TESTS="$ALL_TESTS 0005:3:1:int_0003"
ALL_TESTS="$ALL_TESTS 0006:50:1:bitmap_0001"
ALL_TESTS="$ALL_TESTS 0007:1:1:boot_int"

test_modprobe()
{
Expand Down Expand Up @@ -752,6 +753,46 @@ sysctl_test_0006()
run_bitmaptest
}

sysctl_test_0007()
{
TARGET="${SYSCTL}/boot_int"
if [ ! -f $TARGET ]; then
echo "Skipping test for $TARGET as it is not present ..."
return $ksft_skip
fi

if [ -d $DIR ]; then
echo "Boot param test only possible sysctl_test is built-in, not module:"
cat $TEST_DIR/config >&2
return $ksft_skip
fi

echo -n "Testing if $TARGET is set to 1 ..."
ORIG=$(cat "${TARGET}")

if [ x$ORIG = "x1" ]; then
echo "ok"
return 0
fi
echo "FAIL"
echo "Checking if /proc/cmdline contains setting of the expected parameter ..."
if [ ! -f /proc/cmdline ]; then
echo "/proc/cmdline does not exist, test inconclusive"
return 0
fi

FOUND=$(grep -c "sysctl[./]debug[./]test_sysctl[./]boot_int=1" /proc/cmdline)
if [ $FOUND = "1" ]; then
echo "Kernel param found but $TARGET is not 1, TEST FAILED"
rc=1
test_rc
fi

echo "Skipping test, expected kernel parameter missing."
echo "To perform this test, make sure kernel is booted with parameter: sysctl.debug.test_sysctl.boot_int=1"
return $ksft_skip
}

list_tests()
{
echo "Test ID list:"
Expand All @@ -766,6 +807,7 @@ list_tests()
echo "0004 x $(get_test_count 0004) - tests proc_douintvec()"
echo "0005 x $(get_test_count 0005) - tests proc_douintvec() array"
echo "0006 x $(get_test_count 0006) - tests proc_do_large_bitmap()"
echo "0007 x $(get_test_count 0007) - tests setting sysctl from kernel boot param"
}

usage()
Expand Down

0 comments on commit 4f2f682

Please sign in to comment.