Skip to contentSkip to conversation

support setting sysctl parameters from kernel command line

PATCH In review Vlastimil Babka 4 participants 17 messages Apr 27, 2020 – May 15, 2020

Subsystems (9)
lore
Share & tools
lore
#0 lore raw

v3 cover support setting sysctl parameters from kernel command line

This series adds support for something that seems like many people always wanted but nobody added it yet, so here's the ability to set sysctl parameters via kernel command line options in the form of sysctl.vm.something=1

The important part is Patch 1. The second, not so important part is an attempt to clean up legacy one-off parameters that do the same thing as a sysctl. I don't want to remove them completely for compatibility reasons, but with generic sysctl support the idea is to remove the one-off param handlers and treat the parameters as aliases for the sysctl variants.

I have identified several parameters that mention sysctl counterparts in Documentation/admin-guide/kernel-parameters.txt but there might be more. The conversion also has varying level of success:

- numa_zonelist_order is converted in Patch 2 together with adding the necessary infrastructure. It's easy as it doesn't really do anything but warn on deprecated value these days.
- hung_task_panic is converted in Patch 3, but there's a downside that now it only accepts 0 and 1, while previously it was any integer value
- nmi_watchdog maps to two sysctls nmi_watchdog and hardlockup_panic, so there's no straighforward conversion possible
- traceoff_on_warning is a flag without value and it would be required to handle that somehow in the conversion infractructure, which seems pointless for a single flag

Vlastimil Babka (3):
kernel/sysctl: support setting sysctl parameters from kernel command
line
kernel/sysctl: support handling command line aliases kernel/hung_task convert hung_task_panic boot parameter to sysctl

Changes since v2:
- rebase to 5.7-rc3
- add acks
- add a testcase to tools/testing/selftests/sysctl/sysctl.sh - this adds a new sysctl parameter ../boot_int in lib/test_sysctl and expects the tester to pass the respective boot parameter in any feasible way (including bootconfig) the testcase checks /proc/cmdline to distinguish real failure from the lack of parameter. That allows us to test this without introducing further new functionality (patches 4+5).

Vlastimil Babka (5):
kernel/sysctl: support setting sysctl parameters from kernel command
line
kernel/sysctl: support handling command line aliases kernel/hung_task convert hung_task_panic boot parameter to sysctl tools/testing/selftests/sysctl/sysctl.sh: support CONFIG_TEST_SYSCTL=y lib/test_sysctl: support testing of sysctl. boot parameter

 .../admin-guide/kernel-parameters.txt         |  11 +-
 fs/proc/proc_sysctl.c                         | 142 ++++++++++++++++++
 include/linux/sysctl.h                        |   4 +
 init/main.c                                   |   2 +
 kernel/hung_task.c                            |  10 --
 lib/test_sysctl.c                             |  13 ++
 mm/page_alloc.c                               |   9 --
 tools/testing/selftests/sysctl/sysctl.sh      |  42 +++++-
 8 files changed, 210 insertions(+), 23 deletions(-)
Signature · 1 line
2.26.0
#1 lore raw

PATCH v3 1/5 kernel/sysctl: support setting sysctl parameters from kernel command line

A recently proposed patch to add vm_swappiness command line parameter in addition to existing sysctl [1] made me wonder why we don't have a general support for passing sysctl parameters via command line. Googling found only somebody else wondering the same [2], but I haven't found any prior discussion with reasons why not to do this.

Settings the vm_swappiness issue aside (the underlying issue might be solved in a different way), quick search of kernel-parameters.txt shows there are already some that exist as both sysctl and kernel parameter - hung_task_panic, nmi_watchdog, numa_zonelist_order, traceoff_on_warning. A general mechanism would remove the need to add more of those one-offs and might be handy in situations where configuration by e.g. /etc/sysctl.d/ is impractical.

Hence, this patch adds a new parse_args() pass that looks for parameters prefixed by 'sysctl.' and tries to interpret them as writes to the corresponding sys/ files using an temporary in-kernel procfs mount. This mechanism was suggested by Eric W. Biederman [3], as it handles all dynamically registered sysctl tables, even though we don't handle modular sysctls. Errors due to e.g. invalid parameter name or value are reported in the kernel log.

The processing is hooked right before the init process is loaded, as some handlers might be more complicated than simple setters and might need some subsystems to be initialized. At the moment the init process can be started and eventually execute a process writing to /proc/sys/ then it should be also fine to do that from the kernel.

Sysctls registered later on module load time are not set by this mechanism - it's expected that in such scenarios, setting sysctl values from userspace is practical enough.

[1] https://lore.kernel.org/r/BL0PR02MB560167492CA4094C91589930E9FC0@BL0PR02MB5601.namprd02.prod.outlook.com/
[2] https://unix.stackexchange.com/questions/558802/how-to-set-sysctl-using-kernel-command-line-parameter
[3] https://lore.kernel.org/r/[email protected]/

---
 .../admin-guide/kernel-parameters.txt         |   9 ++
 fs/proc/proc_sysctl.c                         | 107 ++++++++++++++++++
 include/linux/sysctl.h                        |   4 +
 init/main.c                                   |   2 +
 4 files changed, 122 insertions(+)
Documentation/admin-guide/kernel-parameters.txt +9 -0
index 7bc83f3d9bdf..73dbbf942431 100644
@@ -4891,6 +4891,15 @@
 
 	switches=	[HW,M68k]
 
+	sysctl.*=	[KNL]
+			Set a sysctl parameter, right before loading the init
+			process, as if the value was written to the respective
+			/proc/sys/... file. Both '.' and '/' are recognized as
+			separators. Unrecognized parameters and invalid values
+			are reported in the kernel log. Sysctls registered
+			later by a loaded module cannot be set this way.
+			Example: sysctl.vm.swappiness=40
+
 	sysfs.deprecated=0|1 [KNL]
 			Enable/disable old style sysfs layout for old udev
 			on older distributions. When this option is enabled
fs/proc/proc_sysctl.c +107 -0

116 lines not on this page · raw message

include/linux/sysctl.h +4 -0
index 02fa84493f23..bee62d30ebdb 100644
@@ -206,6 +206,7 @@ struct ctl_table_header *register_sysctl_paths(const struct ctl_path *path,
 void unregister_sysctl_table(struct ctl_table_header * table);
 
 extern int sysctl_init(void);
+void do_sysctl_args(void);
 
 extern struct ctl_table sysctl_mount_point[];
 
@@ -236,6 +237,9 @@ static inline void setup_sysctl_set(struct ctl_table_set *p,
 {
 }
 
+static inline void do_sysctl_args(void)
+{
+}
 #endif /* CONFIG_SYSCTL */
 
 int sysctl_max_threads(struct ctl_table *table, int write,
init/main.c +2 -0
index a48617f2e5e5..7b43118215d6 100644
@@ -1372,6 +1372,8 @@ static int __ref kernel_init(void *unused)
 
 	rcu_end_inkernel_boot();
 
+	do_sysctl_args();
+
 	if (ramdisk_execute_command) {
 		ret = run_init_process(ramdisk_execute_command);
 		if (!ret)
Signature · 1 line
2.26.0
maintainer
#2 lore raw

Re: [PATCH v3 1/5] kernel/sysctl: support setting sysctl parameters from kernel command line

Quoted 32 lines · On Mon, 27 Apr 2020 20:04:29 +0200 Vlastimil Babka <[email protected]> wrote:

A recently proposed patch to add vm_swappiness command line parameter in addition to existing sysctl [1] made me wonder why we don't have a general support for passing sysctl parameters via command line. Googling found only somebody else wondering the same [2], but I haven't found any prior discussion with reasons why not to do this.

Settings the vm_swappiness issue aside (the underlying issue might be solved in a different way), quick search of kernel-parameters.txt shows there are already some that exist as both sysctl and kernel parameter - hung_task_panic, nmi_watchdog, numa_zonelist_order, traceoff_on_warning. A general mechanism would remove the need to add more of those one-offs and might be handy in situations where configuration by e.g. /etc/sysctl.d/ is impractical.

Hence, this patch adds a new parse_args() pass that looks for parameters prefixed by 'sysctl.' and tries to interpret them as writes to the corresponding sys/ files using an temporary in-kernel procfs mount. This mechanism was suggested by Eric W. Biederman [3], as it handles all dynamically registered sysctl tables, even though we don't handle modular sysctls. Errors due to e.g. invalid parameter name or value are reported in the kernel log.

The processing is hooked right before the init process is loaded, as some handlers might be more complicated than simple setters and might need some subsystems to be initialized. At the moment the init process can be started and eventually execute a process writing to /proc/sys/ then it should be also fine to do that from the kernel.

Sysctls registered later on module load time are not set by this mechanism - it's expected that in such scenarios, setting sysctl values from userspace is practical enough.

...

Documentation/admin-guide/kernel-parameters.txt line 4901

Quoted 8 lines
Documentation/admin-guide/kernel-parameters.txt +8 -0
+	sysctl.*=	[KNL]
+			Set a sysctl parameter, right before loading the init
+			process, as if the value was written to the respective
+			/proc/sys/... file. Both '.' and '/' are recognized as
+			separators. Unrecognized parameters and invalid values
+			are reported in the kernel log. Sysctls registered
+			later by a loaded module cannot be set this way.
+			Example: sysctl.vm.swappiness=40

Why support "."? I think only supporting "/" is perfectly adequate and simplifies documentation. It aligns the command-line syntax with the rest of the sysctl documentation. I'm not seeing the need to provide two ways of doing the same thing?

#3 lore raw

Re: [PATCH v3 1/5] kernel/sysctl: support setting sysctl parameters from kernel command line

Quoted 4 lines · Andrew Morton
Quoted 2 lines · On Mon, 27 Apr 2020 20:04:29 +0200 Vlastimil Babka <[email protected]> wrote:

...

Quoted 13 lines

Documentation/admin-guide/kernel-parameters.txt line 4901

Quoted 8 lines
Documentation/admin-guide/kernel-parameters.txt +8 -0
+	sysctl.*=	[KNL]
+			Set a sysctl parameter, right before loading the init
+			process, as if the value was written to the respective
+			/proc/sys/... file. Both '.' and '/' are recognized as
+			separators. Unrecognized parameters and invalid values
+			are reported in the kernel log. Sysctls registered
+			later by a loaded module cannot be set this way.
+			Example: sysctl.vm.swappiness=40

Why support "."? I think only supporting "/" is perfectly adequate and simplifies documentation. It aligns the command-line syntax with the rest of the sysctl documentation. I'm not seeing the need to provide two ways of doing the same thing?

AFAIK the "." is traditional, and "/" is a newer artefact of moving from the binary syscall form to procfs based form. So by "command-line syntax" you mean echo and cat, not sysctl tool? Because "man sysctl" says:

variable
The name of a key to read from. An example is kernel.ostype. The '/'
separator is also accepted in place of a '.'.

So I'm not strongly against supporting only / but I expect most people are used to the . and it will take them two attempts to pass the sysctl boot parameter correctly if they don't use it regularly - first trying . form, wonder why it doesn't work, then read the doc and realize it's not supported?

Vlastimil

#4 lore raw

Re: [PATCH v3 1/5] kernel/sysctl: support setting sysctl parameters from kernel command line

Quoted 26 lines · Vlastimil Babka
Quoted 16 lines · Andrew Morton

Documentation/admin-guide/kernel-parameters.txt line 4901

Quoted 9 lines · On Mon, 27 Apr 2020 20:04:29 +0200 Vlastimil Babka <[email protected]> wrote:

...

Documentation/admin-guide/kernel-parameters.txt +8 -0
+	sysctl.*=	[KNL]
+			Set a sysctl parameter, right before loading the init
+			process, as if the value was written to the respective
+			/proc/sys/... file. Both '.' and '/' are recognized as
+			separators. Unrecognized parameters and invalid values
+			are reported in the kernel log. Sysctls registered
+			later by a loaded module cannot be set this way.
+			Example: sysctl.vm.swappiness=40

Why support "."? I think only supporting "/" is perfectly adequate and simplifies documentation. It aligns the command-line syntax with the rest of the sysctl documentation. I'm not seeing the need to provide two ways of doing the same thing?

AFAIK the "." is traditional, and "/" is a newer artefact of moving from the binary syscall form to procfs based form. So by "command-line syntax" you mean echo and cat, not sysctl tool? Because "man sysctl" says:

variable
The name of a key to read from. An example is kernel.ostype. The '/'
separator is also accepted in place of a '.'.

Quoted 5 lines

So I'm not strongly against supporting only / but I expect most people are used to the . and it will take them two attempts to pass the sysctl boot parameter correctly if they don't use it regularly - first trying . form, wonder why it doesn't work, then read the doc and realize it's not supported?

Yes, I do agree. I have only recently learned that sysctl supports / as well. Most people are simply used to . notation. The copy of the arch and . -> / substitution is a trivial operation and I do not think it is a real reason to introduce unnecessarily harder to use interface.

Signature · 2 lines
Michal Hocko
SUSE Labs
#5 lore raw

PATCH v3 2/5 kernel/sysctl: support handling command line aliases

In reply to Vlastimil Babka

We can now handle sysctl parameters on kernel command line, but historically some parameters introduced their own command line equivalent, which we don't want to remove for compatibility reasons. We can however convert them to the generic infrastructure with a table translating the legacy command line parameters to their sysctl names, and removing the one-off param handlers.

This patch adds the support and makes the first conversion to demonstrate it, on the (deprecated) numa_zonelist_order parameter.

---
 fs/proc/proc_sysctl.c | 48 ++++++++++++++++++++++++++++++++++++-------
 mm/page_alloc.c       |  9 --------
 2 files changed, 41 insertions(+), 16 deletions(-)
fs/proc/proc_sysctl.c +41 -7
index 7804da5f5be0..209ad03e1b82 100644
@@ -1694,6 +1694,37 @@ int __init proc_sys_init(void)
 	return sysctl_init();
 }
 
+struct sysctl_alias {
+	const char *kernel_param;
+	const char *sysctl_param;
+};
+
+/*
+ * Historically some settings had both sysctl and a command line parameter.
+ * With the generic sysctl. parameter support, we can handle them at a single
+ * place and only keep the historical name for compatibility. This is not meant
+ * to add brand new aliases. When adding existing aliases, consider whether
+ * the possibly different moment of changing the value (e.g. from early_param
+ * to the moment do_sysctl_args() is called) is an issue for the specific
+ * parameter.
+ */
+static const struct sysctl_alias sysctl_aliases[] = {
+	{"numa_zonelist_order",		"vm.numa_zonelist_order" },
+	{ }
+};
+
+static const char *sysctl_find_alias(char *param)
+{
+	const struct sysctl_alias *alias;
+
+	for (alias = &sysctl_aliases[0]; alias->kernel_param != NULL; alias++) {
+		if (strcmp(alias->kernel_param, param) == 0)
+			return alias->sysctl_param;
+	}
+
+	return NULL;
+}
+
 /* Set sysctl value passed on kernel command line. */
 static int process_sysctl_arg(char *param, char *val,
 			       const char *unused, void *arg)
@@ -1707,15 +1738,18 @@ static int process_sysctl_arg(char *param, char *val,
 	loff_t pos = 0;
 	ssize_t wret;
 
-	if (strncmp(param, "sysctl", sizeof("sysctl") - 1))
-		return 0;
-
-	param += sizeof("sysctl") - 1;
+	if (strncmp(param, "sysctl", sizeof("sysctl") - 1) == 0) {
+		param += sizeof("sysctl") - 1;
 
-	if (param[0] != '/' && param[0] != '.')
-		return 0;
+		if (param[0] != '/' && param[0] != '.')
+			return 0;
 
-	param++;
+		param++;
+	} else {
+		param = (char *) sysctl_find_alias(param);
+		if (!param)
+			return 0;
+	}
 
 	/*
 	 * To set sysctl options, we use a temporary mount of proc, look up the
mm/page_alloc.c +0 -9
index 69827d4fa052..80b2788711c9 100644
@@ -5531,15 +5531,6 @@ static int __parse_numa_zonelist_order(char *s)
 	return 0;
 }
 
-static __init int setup_numa_zonelist_order(char *s)
-{
-	if (!s)
-		return 0;
-
-	return __parse_numa_zonelist_order(s);
-}
-early_param("numa_zonelist_order", setup_numa_zonelist_order);
-
 char numa_zonelist_order[] = "Node";
 
 /*
Signature · 1 line
2.26.0
#6 lore raw

PATCH v3 3/5 kernel/hung_task convert hung_task_panic boot parameter to sysctl

In reply to Vlastimil Babka

We can now handle sysctl parameters on kernel command line and have infrastructure to convert legacy command line options that duplicate sysctl to become a sysctl alias.

This patch converts the hung_task_panic parameter. Note that the sysctl handler is more strict and allows only 0 and 1, while the legacy parameter allowed any non-zero value. But there is little reason anyone would not be using 1.

---
 Documentation/admin-guide/kernel-parameters.txt |  2 +-
 fs/proc/proc_sysctl.c                           |  1 +
 kernel/hung_task.c                              | 10 ----------
 3 files changed, 2 insertions(+), 11 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +1 -1
index 73dbbf942431..1b6e710b7a8a 100644
@@ -1491,7 +1491,7 @@
 			[KNL] Should the hung task detector generate panics.
 			Format: <integer>
 
-			A nonzero value instructs the kernel to panic when a
+			A value of 1 instructs the kernel to panic when a
 			hung task is detected. The default value is controlled
 			by the CONFIG_BOOTPARAM_HUNG_TASK_PANIC build-time
 			option. The value selected by this boot parameter can
fs/proc/proc_sysctl.c +1 -0
index 209ad03e1b82..e8f97bd65909 100644
@@ -1710,6 +1710,7 @@ struct sysctl_alias {
  */
 static const struct sysctl_alias sysctl_aliases[] = {
 	{"numa_zonelist_order",		"vm.numa_zonelist_order" },
+	{"hung_task_panic",		"kernel.hung_task_panic" },
 	{ }
 };
 
kernel/hung_task.c +0 -10
index 14a625c16cb3..b22b5eeab3cb 100644
@@ -63,16 +63,6 @@ static struct task_struct *watchdog_task;
 unsigned int __read_mostly sysctl_hung_task_panic =
 				CONFIG_BOOTPARAM_HUNG_TASK_PANIC_VALUE;
 
-static int __init hung_task_panic_setup(char *str)
-{
-	int rc = kstrtouint(str, 0, &sysctl_hung_task_panic);
-
-	if (rc)
-		return rc;
-	return 1;
-}
-__setup("hung_task_panic=", hung_task_panic_setup);
-
 static int
 hung_task_panic(struct notifier_block *this, unsigned long event, void *ptr)
 {
Signature · 1 line
2.26.0
#7 lore raw

PATCH v3 4/5 tools/testing/selftests/sysctl/sysctl.sh: support CONFIG_TEST_SYSCTL=y

In reply to Vlastimil Babka

The testing script recommends CONFIG_TEST_SYSCTL=y, but actually only works with CONFIG_TEST_SYSCTL=m. Testing of sysctl setting via boot param however requires the test to be built-in, so make sure the test script supports it.

---
 tools/testing/selftests/sysctl/sysctl.sh | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
tools/testing/selftests/sysctl/sysctl.sh +3 -3
index 6a970b127c9b..ce1eeea6f769 100755
@@ -42,7 +42,7 @@ ALL_TESTS="$ALL_TESTS 0006:50:1:bitmap_0001"
 
 test_modprobe()
 {
-       if [ ! -d $DIR ]; then
+       if [ ! -d $SYSCTL ]; then
                echo "$0: $DIR not present" >&2
                echo "You must have the following enabled in your kernel:" >&2
                cat $TEST_DIR/config >&2
@@ -122,9 +122,9 @@ test_reqs()
 
 function load_req_mod()
 {
-	if [ ! -d $DIR ]; then
+	if [ ! -d $DIR -a ! -d $SYSCTL ]; then
 		if ! modprobe -q -n $TEST_DRIVER; then
-			echo "$0: module $TEST_DRIVER not found [SKIP]"
+			echo "$0: module $TEST_DRIVER not found and not built-in [SKIP]"
 			exit $ksft_skip
 		fi
 		modprobe $TEST_DRIVER
Signature · 1 line
2.26.0
Acked-by on v3 4/5
#8 lore raw

Re: [PATCH v3 4/5] tools/testing/selftests/sysctl/sysctl.sh: support CONFIG_TEST_SYSCTL=y

Quoted 5 lines · Vlastimil Babka

The testing script recommends CONFIG_TEST_SYSCTL=y, but actually only works with CONFIG_TEST_SYSCTL=m. Testing of sysctl setting via boot param however requires the test to be built-in, so make sure the test script supports it.

Luis

#9 lore raw

PATCH v3 5/5 lib/test_sysctl: support testing of sysctl. boot parameter

In reply to Vlastimil Babka

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.

---
 lib/test_sysctl.c                        | 13 +++++++++
 tools/testing/selftests/sysctl/sysctl.sh | 36 ++++++++++++++++++++++++
 2 files changed, 49 insertions(+)
lib/test_sysctl.c +13 -0
index 566dad3f4196..84eaae22d3a6 100644
@@ -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];
@@ -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)",
@@ -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,
tools/testing/selftests/sysctl/sysctl.sh +36 -0
index ce1eeea6f769..ef6417b8067b 100755
@@ -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()
 {
@@ -752,6 +753,40 @@ sysctl_test_0006()
 	run_bitmaptest
 }
 
+sysctl_test_0007()
+{
+	TARGET="${SYSCTL}/boot_int"
+	if [ -d $DIR ]; then
+		echo "Boot param test only possible sysctl_test is built-in, not module:"
+		cat $TEST_DIR/config >&2
+		return 0
+	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"
+}
+
 list_tests()
 {
 	echo "Test ID list:"
@@ -766,6 +801,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()
Signature · 1 line
2.26.0
#10 lore raw

Re: [PATCH v3 5/5] lib/test_sysctl: support testing of sysctl. boot parameter

Quoted 64 lines · Vlastimil Babka

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.

---
 lib/test_sysctl.c                        | 13 +++++++++
 tools/testing/selftests/sysctl/sysctl.sh | 36 ++++++++++++++++++++++++
 2 files changed, 49 insertions(+)
lib/test_sysctl.c +13 -0
index 566dad3f4196..84eaae22d3a6 100644
@@ -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];
@@ -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)",
@@ -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,
tools/testing/selftests/sysctl/sysctl.sh +1 -0
index ce1eeea6f769..ef6417b8067b 100755
@@ -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()
 {
@@ -752,6 +753,40 @@ sysctl_test_0006()
 	run_bitmaptest

tools/testing/selftests/sysctl/sysctl.sh sysctl_test_0006() · line 763

Quoted 10 lines
tools/testing/selftests/sysctl/sysctl.sh +8 -0
 }
 
+sysctl_test_0007()
+{
+	TARGET="${SYSCTL}/boot_int"
+	if [ -d $DIR ]; then
+		echo "Boot param test only possible sysctl_test is built-in, not module:"
+		cat $TEST_DIR/config >&2
+		return 0
+	fi

Nice, also we could just require

tools/testing/selftests/sysctl/config +2 -0
index 6ca14800d755..34461cc99a2b 100644
@@ -1 +1,3 @@
 CONFIG_TEST_SYSCTL=y
+CONFIG_IKCONFIG=y
+CONFIG_IKCONFIG_PROC=y

tools/testing/selftests/firmware/fw_lib.sh then has a kconfig_has() which can verify the exact config.

tools/testing/selftests/sysctl/sysctl.sh sysctl_test_0006() · line 766

Quoted 3 lines
tools/testing/selftests/sysctl/sysctl.sh +3 -0
+
+	echo -n "Testing if $TARGET is set to 1 ..."
+	ORIG=$(cat "${TARGET}")

This would fail if someone uses this script to test an older kernel, and the scripts in selftests are supposed to work with older kernels. One way to address this would be to just see if the file exists first and ignore the test if the $SYSCTL directory exists but the file $TARGET does not.

For now we can just do this:

if [ ! -d $TARGET ]; then
echo "Skipping test for $TARGET as it is not present ..." return 0
fi

Luis

#11 lore raw

Re: [PATCH v3 5/5] lib/test_sysctl: support testing of sysctl. boot parameter

Quoted 12 lines · Luis Chamberlain

On Mon, Apr 27, 2020 at 08:04:33PM +0200, Vlastimil Babka wrote:
Nice, also we could just require

tools/testing/selftests/sysctl/config +2 -0
index 6ca14800d755..34461cc99a2b 100644
@@ -1 +1,3 @@
 CONFIG_TEST_SYSCTL=y
+CONFIG_IKCONFIG=y
+CONFIG_IKCONFIG_PROC=y
Quoted 2 lines

tools/testing/selftests/firmware/fw_lib.sh then has a kconfig_has() which can verify the exact config.

Hmm but it also has a (firmware area specific) fallback for case where IKCONFIG_PROC doesn't exist. So it's simpler to just keep checking the module dir, IMHO, as that would be the fallback.

Quoted 6 lines

Review

Quoted 3 lines
(quoted diff) +3 -0
+
+	echo -n "Testing if $TARGET is set to 1 ..."
+	ORIG=$(cat "${TARGET}")

This would fail if someone uses this script to test an older kernel, and the scripts in selftests are supposed to work with older kernels.

Oh, I didn't know that it's supposed to.

Quoted 11 lines

One
way to address this would be to just see if the file exists first and ignore the test if the $SYSCTL directory exists but the file $TARGET does not.

For now we can just do this:

if [ ! -d $TARGET ]; then
echo "Skipping test for $TARGET as it is not present ..." return 0
fi

OK, just the -d test needs to be fixed :) Andrew can you please apply:

----8<----
From a999e993a89e521b152bbd4b1466f69e62879c30 Mon Sep 17 00:00:00 2001
From: Vlastimil Babka <[email protected]>
Date: Mon, 11 May 2020 12:59:49 +0200
Subject: [PATCH] lib/test_sysctl: support testing of sysctl. boot parameter -
fix

Skip the new test if boot_int sysctl is not present, otherwise, per Luis, "This would fail if someone uses this script to test an older kernel, and the scripts in selftests are supposed to work with older kernels."

---
 tools/testing/selftests/sysctl/sysctl.sh | 5 +++++
 1 file changed, 5 insertions(+)
tools/testing/selftests/sysctl/sysctl.sh +5 -0
index ef6417b8067b..148704f465b5 100755
@@ -756,6 +756,11 @@ sysctl_test_0006()
 sysctl_test_0007()
 {
 	TARGET="${SYSCTL}/boot_int"
+	if [ ! -f $TARGET ]; then
+		echo "Skipping test for $TARGET as it is not present ..."
+		return 0
+	fi
+
 	if [ -d $DIR ]; then
 		echo "Boot param test only possible sysctl_test is built-in, not module:"
 		cat $TEST_DIR/config >&2
Signature · 1 line
2.26.2
#12 lore raw

Re: [PATCH v3 5/5] lib/test_sysctl: support testing of sysctl. boot parameter

Quoted 14 lines · Vlastimil Babka
Quoted 12 lines · Luis Chamberlain

On Mon, Apr 27, 2020 at 08:04:33PM +0200, Vlastimil Babka wrote:
Nice, also we could just require

tools/testing/selftests/sysctl/config +2 -0
index 6ca14800d755..34461cc99a2b 100644
@@ -1 +1,3 @@
 CONFIG_TEST_SYSCTL=y
+CONFIG_IKCONFIG=y
+CONFIG_IKCONFIG_PROC=y
Quoted 6 lines
Quoted 2 lines

tools/testing/selftests/firmware/fw_lib.sh then has a kconfig_has() which can verify the exact config.

Hmm but it also has a (firmware area specific) fallback for case where IKCONFIG_PROC doesn't exist. So it's simpler to just keep checking the module dir, IMHO, as that would be the fallback.

As you wish.

Quoted 8 lines
Quoted 6 lines

Review

Quoted 3 lines
(quoted diff) +3 -0
+
+	echo -n "Testing if $TARGET is set to 1 ..."
+	ORIG=$(cat "${TARGET}")

This would fail if someone uses this script to test an older kernel, and the scripts in selftests are supposed to work with older kernels.

Oh, I didn't know that it's supposed to.

Yeap, that's how they are used.

tools/testing/selftests/sysctl/sysctl.sh sysctl_test_0006()

Quoted 36 lines
Quoted 11 lines

One
way to address this would be to just see if the file exists first and ignore the test if the $SYSCTL directory exists but the file $TARGET does not.

For now we can just do this:

if [ ! -d $TARGET ]; then
echo "Skipping test for $TARGET as it is not present ..." return 0
fi

OK, just the -d test needs to be fixed :) Andrew can you please apply:

----8<----
From a999e993a89e521b152bbd4b1466f69e62879c30 Mon Sep 17 00:00:00 2001
From: Vlastimil Babka <[email protected]>
Date: Mon, 11 May 2020 12:59:49 +0200
Subject: [PATCH] lib/test_sysctl: support testing of sysctl. boot parameter -
fix

Skip the new test if boot_int sysctl is not present, otherwise, per Luis, "This would fail if someone uses this script to test an older kernel, and the scripts in selftests are supposed to work with older kernels."

---
 tools/testing/selftests/sysctl/sysctl.sh | 5 +++++
 1 file changed, 5 insertions(+)
tools/testing/selftests/sysctl/sysctl.sh +0 -0
index ef6417b8067b..148704f465b5 100755
@@ -756,6 +756,11 @@ sysctl_test_0006()

You want to:

# Kselftest framework requirement - SKIP code is 4. ksft_skip=4

tools/testing/selftests/sysctl/sysctl.sh sysctl_test_0006() · line 762

Quoted 7 lines
tools/testing/selftests/sysctl/sysctl.sh +4 -0
 sysctl_test_0007()
 {
 	TARGET="${SYSCTL}/boot_int"
+	if [ ! -f $TARGET ]; then
+		echo "Skipping test for $TARGET as it is not present ..."
+		return 0
+	fi

And return 4 instead.

Luis

Quoted 7 lines
tools/testing/selftests/sysctl/sysctl.sh +1 -0
+
 	if [ -d $DIR ]; then
 		echo "Boot param test only possible sysctl_test is built-in, not module:"
 		cat $TEST_DIR/config >&2
Signature · 1 line
2.26.2
#13 lore raw

Re: [PATCH v3 5/5] lib/test_sysctl: support testing of sysctl. boot parameter

Quoted 30 lines · Luis Chamberlain

tools/testing/selftests/sysctl/sysctl.sh sysctl_test_0006()

Quoted 22 lines · Vlastimil Babka

----8<----
From a999e993a89e521b152bbd4b1466f69e62879c30 Mon Sep 17 00:00:00 2001
From: Vlastimil Babka <[email protected]>
Date: Mon, 11 May 2020 12:59:49 +0200
Subject: [PATCH] lib/test_sysctl: support testing of sysctl. boot parameter -
fix

Skip the new test if boot_int sysctl is not present, otherwise, per Luis, "This would fail if someone uses this script to test an older kernel, and the scripts in selftests are supposed to work with older kernels."

---
 tools/testing/selftests/sysctl/sysctl.sh | 5 +++++
 1 file changed, 5 insertions(+)
tools/testing/selftests/sysctl/sysctl.sh +0 -0
index ef6417b8067b..148704f465b5 100755
@@ -756,6 +756,11 @@ sysctl_test_0006()

You want to:

# Kselftest framework requirement - SKIP code is 4. ksft_skip=4

Quoted 9 lines

tools/testing/selftests/sysctl/sysctl.sh sysctl_test_0006() · line 762

Quoted 7 lines
tools/testing/selftests/sysctl/sysctl.sh +4 -0
 sysctl_test_0007()
 {
 	TARGET="${SYSCTL}/boot_int"
+	if [ ! -f $TARGET ]; then
+		echo "Skipping test for $TARGET as it is not present ..."
+		return 0
+	fi

And return 4 instead.

If I return it from the function, nobody will care, AFAICS. If I 'exit $ksft_skip', is that correct if it's just a single test out of 7? What's the proper way?

Thanks

Quoted 9 lines

Luis

Quoted 7 lines
tools/testing/selftests/sysctl/sysctl.sh +1 -0
+
 	if [ -d $DIR ]; then
 		echo "Boot param test only possible sysctl_test is built-in, not module:"
 		cat $TEST_DIR/config >&2
Signature · 1 line
2.26.2
#14 lore raw

Re: [PATCH v3 5/5] lib/test_sysctl: support testing of sysctl. boot parameter

Quoted 41 lines · Vlastimil Babka
Quoted 30 lines · Luis Chamberlain

tools/testing/selftests/sysctl/sysctl.sh sysctl_test_0006()

Quoted 22 lines · Vlastimil Babka

----8<----
From a999e993a89e521b152bbd4b1466f69e62879c30 Mon Sep 17 00:00:00 2001
From: Vlastimil Babka <[email protected]>
Date: Mon, 11 May 2020 12:59:49 +0200
Subject: [PATCH] lib/test_sysctl: support testing of sysctl. boot parameter -
fix

Skip the new test if boot_int sysctl is not present, otherwise, per Luis, "This would fail if someone uses this script to test an older kernel, and the scripts in selftests are supposed to work with older kernels."

---
 tools/testing/selftests/sysctl/sysctl.sh | 5 +++++
 1 file changed, 5 insertions(+)
tools/testing/selftests/sysctl/sysctl.sh +0 -0
index ef6417b8067b..148704f465b5 100755
@@ -756,6 +756,11 @@ sysctl_test_0006()

You want to:

# Kselftest framework requirement - SKIP code is 4. ksft_skip=4

Quoted 9 lines

tools/testing/selftests/sysctl/sysctl.sh sysctl_test_0006() · line 762

Quoted 7 lines
tools/testing/selftests/sysctl/sysctl.sh +4 -0
 sysctl_test_0007()
 {
 	TARGET="${SYSCTL}/boot_int"
+	if [ ! -f $TARGET ]; then
+		echo "Skipping test for $TARGET as it is not present ..."
+		return 0
+	fi

And return 4 instead.

Quoted 2 lines

If I return it from the function, nobody will care, AFAICS. If I 'exit $ksft_skip', is that correct if it's just a single test out of 7?

yes please do that.

Luis

#15 lore raw

Re: [PATCH v3 5/5] lib/test_sysctl: support testing of sysctl. boot parameter

Quoted 41 lines · Luis Chamberlain
Quoted 39 lines · Vlastimil Babka
Quoted 38 lines · Luis Chamberlain

tools/testing/selftests/sysctl/sysctl.sh sysctl_test_0006()

Quoted 22 lines · Vlastimil Babka

----8<----
From a999e993a89e521b152bbd4b1466f69e62879c30 Mon Sep 17 00:00:00 2001
From: Vlastimil Babka <[email protected]>
Date: Mon, 11 May 2020 12:59:49 +0200
Subject: [PATCH] lib/test_sysctl: support testing of sysctl. boot parameter -
fix

Skip the new test if boot_int sysctl is not present, otherwise, per Luis, "This would fail if someone uses this script to test an older kernel, and the scripts in selftests are supposed to work with older kernels."

---
 tools/testing/selftests/sysctl/sysctl.sh | 5 +++++
 1 file changed, 5 insertions(+)
tools/testing/selftests/sysctl/sysctl.sh +0 -0
index ef6417b8067b..148704f465b5 100755
@@ -756,6 +756,11 @@ sysctl_test_0006()

You want to:

# Kselftest framework requirement - SKIP code is 4. ksft_skip=4

Quoted 7 lines
tools/testing/selftests/sysctl/sysctl.sh +4 -0
 sysctl_test_0007()
 {
   TARGET="${SYSCTL}/boot_int"
+  if [ ! -f $TARGET ]; then
+          echo "Skipping test for $TARGET as it is not present ..."
+          return 0
+  fi
Quoted 6 lines
Quoted 4 lines
Quoted 1 line

And return 4 instead.

If I return it from the function, nobody will care, AFAICS. If I 'exit $ksft_skip', is that correct if it's just a single test out of 7?

yes please do that.

Ah but once we add test_0008() it may be supported.. so I think return would be OK

#16 lore raw

Re: [PATCH v3 5/5] lib/test_sysctl: support testing of sysctl. boot parameter

Quoted 18 lines · Luis Chamberlain
Quoted 17 lines · Luis Chamberlain
Quoted 17 lines
Quoted 16 lines

You want to:

# Kselftest framework requirement - SKIP code is 4. ksft_skip=4

Review

Quoted 7 lines

sysctl_test_0007()

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

And return 4 instead.

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

If I return it from the function, nobody will care, AFAICS. If I 'exit $ksft_skip', is that correct if it's just a single test out of 7?

yes please do that.

Ah but once we add test_0008() it may be supported.. so I think return would be OK

OK

----8<----
From 4311b356f177aaa4e21bd3d2a2169e5bd50ab62d Mon Sep 17 00:00:00 2001
From: Vlastimil Babka <[email protected]>
Date: Mon, 11 May 2020 12:59:49 +0200
Subject: [PATCH] lib/test_sysctl: support testing of sysctl. boot parameter -
fix

Skip the new test if boot_int sysctl is not present, otherwise, per Luis, "This would fail if someone uses this script to test an older kernel, and the scripts in selftests are supposed to work with older kernels."

---
 tools/testing/selftests/sysctl/sysctl.sh | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
tools/testing/selftests/sysctl/sysctl.sh +7 -1
index ef6417b8067b..ab44d3e65986 100755
@@ -756,10 +756,15 @@ sysctl_test_0006()
 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 0
+		return $ksft_skip
 	fi
 
 	echo -n "Testing if $TARGET is set to 1 ..."
@@ -785,6 +790,7 @@ sysctl_test_0007()
 
 	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()
Signature · 1 line
2.26.2