linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3 0/5] support setting sysctl parameters from kernel command line
@ 2020-04-27 18:04 Vlastimil Babka
  2020-04-27 18:04 ` [PATCH v3 1/5] kernel/sysctl: " Vlastimil Babka
                   ` (4 more replies)
  0 siblings, 5 replies; 17+ messages in thread
From: Vlastimil Babka @ 2020-04-27 18:04 UTC (permalink / raw)
  To: Andrew Morton, Luis Chamberlain, Kees Cook, Iurii Zaikin
  Cc: linux-kernel, linux-api, linux-mm, Ivan Teterevkov, Michal Hocko,
	David Rientjes, Matthew Wilcox, Eric W . Biederman,
	Guilherme G . Piccoli, Alexey Dobriyan, Thomas Gleixner,
	Greg Kroah-Hartman, Christian Brauner, Masami Hiramatsu,
	Vlastimil Babka

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(-)

-- 
2.26.0



^ permalink raw reply	[flat|nested] 17+ messages in thread

* [PATCH v3 1/5] kernel/sysctl: support setting sysctl parameters from kernel command line
  2020-04-27 18:04 [PATCH v3 0/5] support setting sysctl parameters from kernel command line Vlastimil Babka
@ 2020-04-27 18:04 ` Vlastimil Babka
  2020-04-27 18:33   ` Andrew Morton
  2020-04-27 18:04 ` [PATCH v3 2/5] kernel/sysctl: support handling command line aliases Vlastimil Babka
                   ` (3 subsequent siblings)
  4 siblings, 1 reply; 17+ messages in thread
From: Vlastimil Babka @ 2020-04-27 18:04 UTC (permalink / raw)
  To: Andrew Morton, Luis Chamberlain, Kees Cook, Iurii Zaikin
  Cc: linux-kernel, linux-api, linux-mm, Ivan Teterevkov, Michal Hocko,
	David Rientjes, Matthew Wilcox, Eric W . Biederman,
	Guilherme G . Piccoli, Alexey Dobriyan, Thomas Gleixner,
	Greg Kroah-Hartman, Christian Brauner, Masami Hiramatsu,
	Vlastimil Babka, Michal Hocko

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/87bloj2skm.fsf@x220.int.ebiederm.org/

Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
Reviewed-by: Luis Chamberlain <mcgrof@kernel.org>
Reviewed-by: Masami Hiramatsu <mhiramat@kernel.org>
Acked-by: Kees Cook <keescook@chromium.org>
Acked-by: Michal Hocko <mhocko@suse.com>
---
 .../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(+)

diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index 7bc83f3d9bdf..73dbbf942431 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -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
diff --git a/fs/proc/proc_sysctl.c b/fs/proc/proc_sysctl.c
index b6f5d459b087..7804da5f5be0 100644
--- a/fs/proc/proc_sysctl.c
+++ b/fs/proc/proc_sysctl.c
@@ -14,6 +14,7 @@
 #include <linux/mm.h>
 #include <linux/module.h>
 #include <linux/bpf-cgroup.h>
+#include <linux/mount.h>
 #include "internal.h"
 
 static const struct dentry_operations proc_sys_dentry_operations;
@@ -1692,3 +1693,109 @@ int __init proc_sys_init(void)
 
 	return sysctl_init();
 }
+
+/* Set sysctl value passed on kernel command line. */
+static int process_sysctl_arg(char *param, char *val,
+			       const char *unused, void *arg)
+{
+	char *path;
+	struct vfsmount **proc_mnt = arg;
+	struct file_system_type *proc_fs_type;
+	struct file *file;
+	int len;
+	int err;
+	loff_t pos = 0;
+	ssize_t wret;
+
+	if (strncmp(param, "sysctl", sizeof("sysctl") - 1))
+		return 0;
+
+	param += sizeof("sysctl") - 1;
+
+	if (param[0] != '/' && param[0] != '.')
+		return 0;
+
+	param++;
+
+	/*
+	 * To set sysctl options, we use a temporary mount of proc, look up the
+	 * respective sys/ file and write to it. To avoid mounting it when no
+	 * options were given, we mount it only when the first sysctl option is
+	 * found. Why not a persistent mount? There are problems with a
+	 * persistent mount of proc in that it forces userspace not to use any
+	 * proc mount options.
+	 */
+	if (!*proc_mnt) {
+		proc_fs_type = get_fs_type("proc");
+		if (!proc_fs_type) {
+			pr_err("Failed to find procfs to set sysctl from command line\n");
+			return 0;
+		}
+		*proc_mnt = kern_mount(proc_fs_type);
+		put_filesystem(proc_fs_type);
+		if (IS_ERR(*proc_mnt)) {
+			pr_err("Failed to mount procfs to set sysctl from command line\n");
+			return 0;
+		}
+	}
+
+	path = kasprintf(GFP_KERNEL, "sys/%s", param);
+	if (!path)
+		panic("%s: Failed to allocate path for %s\n", __func__, param);
+	strreplace(path, '.', '/');
+
+	file = file_open_root((*proc_mnt)->mnt_root, *proc_mnt, path, O_WRONLY, 0);
+	if (IS_ERR(file)) {
+		err = PTR_ERR(file);
+		if (err == -ENOENT)
+			pr_err("Failed to set sysctl parameter '%s=%s': parameter not found\n",
+				param, val);
+		else if (err == -EACCES)
+			pr_err("Failed to set sysctl parameter '%s=%s': permission denied (read-only?)\n",
+				param, val);
+		else
+			pr_err("Error %pe opening proc file to set sysctl parameter '%s=%s'\n",
+				file, param, val);
+		goto out;
+	}
+	len = strlen(val);
+	wret = kernel_write(file, val, len, &pos);
+	if (wret < 0) {
+		err = wret;
+		if (err == -EINVAL)
+			pr_err("Failed to set sysctl parameter '%s=%s': invalid value\n",
+				param, val);
+		else
+			pr_err("Error %pe writing to proc file to set sysctl parameter '%s=%s'\n",
+				ERR_PTR(err), param, val);
+	} else if (wret != len) {
+		pr_err("Wrote only %zd bytes of %d writing to proc file %s to set sysctl parameter '%s=%s\n",
+			wret, len, path, param, val);
+	}
+
+	err = filp_close(file, NULL);
+	if (err)
+		pr_err("Error %pe closing proc file to set sysctl parameter '%s=%s\n",
+			ERR_PTR(err), param, val);
+out:
+	kfree(path);
+	return 0;
+}
+
+void do_sysctl_args(void)
+{
+	char *command_line;
+	struct vfsmount *proc_mnt = NULL;
+
+	command_line = kstrdup(saved_command_line, GFP_KERNEL);
+	if (!command_line)
+		panic("%s: Failed to allocate copy of command line\n", __func__);
+
+	parse_args("Setting sysctl args", command_line,
+		   NULL, 0, -1, -1, &proc_mnt, process_sysctl_arg);
+
+	if (proc_mnt)
+		kern_unmount(proc_mnt);
+
+	kfree(command_line);
+}
diff --git a/include/linux/sysctl.h b/include/linux/sysctl.h
index 02fa84493f23..bee62d30ebdb 100644
--- a/include/linux/sysctl.h
+++ b/include/linux/sysctl.h
@@ -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,
diff --git a/init/main.c b/init/main.c
index a48617f2e5e5..7b43118215d6 100644
--- a/init/main.c
+++ b/init/main.c
@@ -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)
-- 
2.26.0



^ permalink raw reply	[flat|nested] 17+ messages in thread

* [PATCH v3 2/5] kernel/sysctl: support handling command line aliases
  2020-04-27 18:04 [PATCH v3 0/5] support setting sysctl parameters from kernel command line Vlastimil Babka
  2020-04-27 18:04 ` [PATCH v3 1/5] kernel/sysctl: " Vlastimil Babka
@ 2020-04-27 18:04 ` Vlastimil Babka
  2020-04-27 18:04 ` [PATCH v3 3/5] kernel/hung_task convert hung_task_panic boot parameter to sysctl Vlastimil Babka
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 17+ messages in thread
From: Vlastimil Babka @ 2020-04-27 18:04 UTC (permalink / raw)
  To: Andrew Morton, Luis Chamberlain, Kees Cook, Iurii Zaikin
  Cc: linux-kernel, linux-api, linux-mm, Ivan Teterevkov, Michal Hocko,
	David Rientjes, Matthew Wilcox, Eric W . Biederman,
	Guilherme G . Piccoli, Alexey Dobriyan, Thomas Gleixner,
	Greg Kroah-Hartman, Christian Brauner, Masami Hiramatsu,
	Vlastimil Babka, Michal Hocko

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.

Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
Reviewed-by: Luis Chamberlain <mcgrof@kernel.org>
Acked-by: Kees Cook <keescook@chromium.org>
Acked-by: Michal Hocko <mhocko@suse.com>
---
 fs/proc/proc_sysctl.c | 48 ++++++++++++++++++++++++++++++++++++-------
 mm/page_alloc.c       |  9 --------
 2 files changed, 41 insertions(+), 16 deletions(-)

diff --git a/fs/proc/proc_sysctl.c b/fs/proc/proc_sysctl.c
index 7804da5f5be0..209ad03e1b82 100644
--- a/fs/proc/proc_sysctl.c
+++ b/fs/proc/proc_sysctl.c
@@ -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
diff --git a/mm/page_alloc.c b/mm/page_alloc.c
index 69827d4fa052..80b2788711c9 100644
--- a/mm/page_alloc.c
+++ b/mm/page_alloc.c
@@ -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";
 
 /*
-- 
2.26.0



^ permalink raw reply	[flat|nested] 17+ messages in thread

* [PATCH v3 3/5] kernel/hung_task convert hung_task_panic boot parameter to sysctl
  2020-04-27 18:04 [PATCH v3 0/5] support setting sysctl parameters from kernel command line Vlastimil Babka
  2020-04-27 18:04 ` [PATCH v3 1/5] kernel/sysctl: " Vlastimil Babka
  2020-04-27 18:04 ` [PATCH v3 2/5] kernel/sysctl: support handling command line aliases Vlastimil Babka
@ 2020-04-27 18:04 ` Vlastimil Babka
  2020-04-27 18:04 ` [PATCH v3 4/5] tools/testing/selftests/sysctl/sysctl.sh: support CONFIG_TEST_SYSCTL=y Vlastimil Babka
  2020-04-27 18:04 ` [PATCH v3 5/5] lib/test_sysctl: support testing of sysctl. boot parameter Vlastimil Babka
  4 siblings, 0 replies; 17+ messages in thread
From: Vlastimil Babka @ 2020-04-27 18:04 UTC (permalink / raw)
  To: Andrew Morton, Luis Chamberlain, Kees Cook, Iurii Zaikin
  Cc: linux-kernel, linux-api, linux-mm, Ivan Teterevkov, Michal Hocko,
	David Rientjes, Matthew Wilcox, Eric W . Biederman,
	Guilherme G . Piccoli, Alexey Dobriyan, Thomas Gleixner,
	Greg Kroah-Hartman, Christian Brauner, Masami Hiramatsu,
	Vlastimil Babka, Michal Hocko

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.

Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
Reviewed-by: Kees Cook <keescook@chromium.org>
Acked-by: Michal Hocko <mhocko@suse.com>
---
 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(-)

diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index 73dbbf942431..1b6e710b7a8a 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -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
diff --git a/fs/proc/proc_sysctl.c b/fs/proc/proc_sysctl.c
index 209ad03e1b82..e8f97bd65909 100644
--- a/fs/proc/proc_sysctl.c
+++ b/fs/proc/proc_sysctl.c
@@ -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" },
 	{ }
 };
 
diff --git a/kernel/hung_task.c b/kernel/hung_task.c
index 14a625c16cb3..b22b5eeab3cb 100644
--- a/kernel/hung_task.c
+++ b/kernel/hung_task.c
@@ -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)
 {
-- 
2.26.0



^ permalink raw reply	[flat|nested] 17+ messages in thread

* [PATCH v3 4/5] tools/testing/selftests/sysctl/sysctl.sh: support CONFIG_TEST_SYSCTL=y
  2020-04-27 18:04 [PATCH v3 0/5] support setting sysctl parameters from kernel command line Vlastimil Babka
                   ` (2 preceding siblings ...)
  2020-04-27 18:04 ` [PATCH v3 3/5] kernel/hung_task convert hung_task_panic boot parameter to sysctl Vlastimil Babka
@ 2020-04-27 18:04 ` Vlastimil Babka
  2020-04-27 18:39   ` Luis Chamberlain
  2020-04-27 18:04 ` [PATCH v3 5/5] lib/test_sysctl: support testing of sysctl. boot parameter Vlastimil Babka
  4 siblings, 1 reply; 17+ messages in thread
From: Vlastimil Babka @ 2020-04-27 18:04 UTC (permalink / raw)
  To: Andrew Morton, Luis Chamberlain, Kees Cook, Iurii Zaikin
  Cc: linux-kernel, linux-api, linux-mm, Ivan Teterevkov, Michal Hocko,
	David Rientjes, Matthew Wilcox, Eric W . Biederman,
	Guilherme G . Piccoli, Alexey Dobriyan, Thomas Gleixner,
	Greg Kroah-Hartman, Christian Brauner, Masami Hiramatsu,
	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.

Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
---
 tools/testing/selftests/sysctl/sysctl.sh | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/tools/testing/selftests/sysctl/sysctl.sh b/tools/testing/selftests/sysctl/sysctl.sh
index 6a970b127c9b..ce1eeea6f769 100755
--- a/tools/testing/selftests/sysctl/sysctl.sh
+++ b/tools/testing/selftests/sysctl/sysctl.sh
@@ -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
-- 
2.26.0



^ permalink raw reply	[flat|nested] 17+ messages in thread

* [PATCH v3 5/5] lib/test_sysctl: support testing of sysctl. boot parameter
  2020-04-27 18:04 [PATCH v3 0/5] support setting sysctl parameters from kernel command line Vlastimil Babka
                   ` (3 preceding siblings ...)
  2020-04-27 18:04 ` [PATCH v3 4/5] tools/testing/selftests/sysctl/sysctl.sh: support CONFIG_TEST_SYSCTL=y Vlastimil Babka
@ 2020-04-27 18:04 ` Vlastimil Babka
  2020-04-27 18:39   ` Luis Chamberlain
  4 siblings, 1 reply; 17+ messages in thread
From: Vlastimil Babka @ 2020-04-27 18:04 UTC (permalink / raw)
  To: Andrew Morton, Luis Chamberlain, Kees Cook, Iurii Zaikin
  Cc: linux-kernel, linux-api, linux-mm, Ivan Teterevkov, Michal Hocko,
	David Rientjes, Matthew Wilcox, Eric W . Biederman,
	Guilherme G . Piccoli, Alexey Dobriyan, Thomas Gleixner,
	Greg Kroah-Hartman, Christian Brauner, Masami Hiramatsu,
	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.

Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
---
 lib/test_sysctl.c                        | 13 +++++++++
 tools/testing/selftests/sysctl/sysctl.sh | 36 ++++++++++++++++++++++++
 2 files changed, 49 insertions(+)

diff --git a/lib/test_sysctl.c b/lib/test_sysctl.c
index 566dad3f4196..84eaae22d3a6 100644
--- a/lib/test_sysctl.c
+++ b/lib/test_sysctl.c
@@ -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,
diff --git a/tools/testing/selftests/sysctl/sysctl.sh b/tools/testing/selftests/sysctl/sysctl.sh
index ce1eeea6f769..ef6417b8067b 100755
--- a/tools/testing/selftests/sysctl/sysctl.sh
+++ b/tools/testing/selftests/sysctl/sysctl.sh
@@ -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()
-- 
2.26.0



^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH v3 1/5] kernel/sysctl: support setting sysctl parameters from kernel command line
  2020-04-27 18:04 ` [PATCH v3 1/5] kernel/sysctl: " Vlastimil Babka
@ 2020-04-27 18:33   ` Andrew Morton
  2020-04-28  8:09     ` Vlastimil Babka
  0 siblings, 1 reply; 17+ messages in thread
From: Andrew Morton @ 2020-04-27 18:33 UTC (permalink / raw)
  To: Vlastimil Babka
  Cc: Luis Chamberlain, Kees Cook, Iurii Zaikin, linux-kernel,
	linux-api, linux-mm, Ivan Teterevkov, Michal Hocko,
	David Rientjes, Matthew Wilcox, Eric W . Biederman,
	Guilherme G . Piccoli, Alexey Dobriyan, Thomas Gleixner,
	Greg Kroah-Hartman, Christian Brauner, Masami Hiramatsu,
	Michal Hocko

On Mon, 27 Apr 2020 20:04:29 +0200 Vlastimil Babka <vbabka@suse.cz> 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.
> 
> ...
>  
> +	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?



^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH v3 5/5] lib/test_sysctl: support testing of sysctl. boot parameter
  2020-04-27 18:04 ` [PATCH v3 5/5] lib/test_sysctl: support testing of sysctl. boot parameter Vlastimil Babka
@ 2020-04-27 18:39   ` Luis Chamberlain
  2020-05-11 11:05     ` Vlastimil Babka
  0 siblings, 1 reply; 17+ messages in thread
From: Luis Chamberlain @ 2020-04-27 18:39 UTC (permalink / raw)
  To: Vlastimil Babka
  Cc: Andrew Morton, Kees Cook, Iurii Zaikin, linux-kernel, linux-api,
	linux-mm, Ivan Teterevkov, Michal Hocko, David Rientjes,
	Matthew Wilcox, Eric W . Biederman, Guilherme G . Piccoli,
	Alexey Dobriyan, Thomas Gleixner, Greg Kroah-Hartman,
	Christian Brauner, Masami Hiramatsu

On Mon, Apr 27, 2020 at 08:04:33PM +0200, Vlastimil Babka wrote:
> 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.
> 
> Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
> ---
>  lib/test_sysctl.c                        | 13 +++++++++
>  tools/testing/selftests/sysctl/sysctl.sh | 36 ++++++++++++++++++++++++
>  2 files changed, 49 insertions(+)
> 
> diff --git a/lib/test_sysctl.c b/lib/test_sysctl.c
> index 566dad3f4196..84eaae22d3a6 100644
> --- a/lib/test_sysctl.c
> +++ b/lib/test_sysctl.c
> @@ -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,
> diff --git a/tools/testing/selftests/sysctl/sysctl.sh b/tools/testing/selftests/sysctl/sysctl.sh
> index ce1eeea6f769..ef6417b8067b 100755
> --- a/tools/testing/selftests/sysctl/sysctl.sh
> +++ b/tools/testing/selftests/sysctl/sysctl.sh
> @@ -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

Nice, also we could just require

diff --git a/tools/testing/selftests/sysctl/config b/tools/testing/selftests/sysctl/config
index 6ca14800d755..34461cc99a2b 100644
--- a/tools/testing/selftests/sysctl/config
+++ b/tools/testing/selftests/sysctl/config
@@ -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.

> +
> +	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


^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH v3 4/5] tools/testing/selftests/sysctl/sysctl.sh: support CONFIG_TEST_SYSCTL=y
  2020-04-27 18:04 ` [PATCH v3 4/5] tools/testing/selftests/sysctl/sysctl.sh: support CONFIG_TEST_SYSCTL=y Vlastimil Babka
@ 2020-04-27 18:39   ` Luis Chamberlain
  0 siblings, 0 replies; 17+ messages in thread
From: Luis Chamberlain @ 2020-04-27 18:39 UTC (permalink / raw)
  To: Vlastimil Babka
  Cc: Andrew Morton, Kees Cook, Iurii Zaikin, linux-kernel, linux-api,
	linux-mm, Ivan Teterevkov, Michal Hocko, David Rientjes,
	Matthew Wilcox, Eric W . Biederman, Guilherme G . Piccoli,
	Alexey Dobriyan, Thomas Gleixner, Greg Kroah-Hartman,
	Christian Brauner, Masami Hiramatsu

On Mon, Apr 27, 2020 at 08:04:32PM +0200, Vlastimil Babka wrote:
> 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.
> 
> Signed-off-by: Vlastimil Babka <vbabka@suse.cz>

Acked-by: Luis Chamberlain <mcgrof@kernel.org>

  Luis


^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH v3 1/5] kernel/sysctl: support setting sysctl parameters from kernel command line
  2020-04-27 18:33   ` Andrew Morton
@ 2020-04-28  8:09     ` Vlastimil Babka
  2020-04-28 10:18       ` Michal Hocko
  0 siblings, 1 reply; 17+ messages in thread
From: Vlastimil Babka @ 2020-04-28  8:09 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Luis Chamberlain, Kees Cook, Iurii Zaikin, linux-kernel,
	linux-api, linux-mm, Ivan Teterevkov, Michal Hocko,
	David Rientjes, Matthew Wilcox, Eric W . Biederman,
	Guilherme G . Piccoli, Alexey Dobriyan, Thomas Gleixner,
	Greg Kroah-Hartman, Christian Brauner, Masami Hiramatsu,
	Michal Hocko

On 4/27/20 8:33 PM, Andrew Morton wrote:
> On Mon, 27 Apr 2020 20:04:29 +0200 Vlastimil Babka <vbabka@suse.cz> wrote:
> 
>> ...
>>  
>> +	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


^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH v3 1/5] kernel/sysctl: support setting sysctl parameters from kernel command line
  2020-04-28  8:09     ` Vlastimil Babka
@ 2020-04-28 10:18       ` Michal Hocko
  0 siblings, 0 replies; 17+ messages in thread
From: Michal Hocko @ 2020-04-28 10:18 UTC (permalink / raw)
  To: Vlastimil Babka
  Cc: Andrew Morton, Luis Chamberlain, Kees Cook, Iurii Zaikin,
	linux-kernel, linux-api, linux-mm, Ivan Teterevkov,
	David Rientjes, Matthew Wilcox, Eric W . Biederman,
	Guilherme G . Piccoli, Alexey Dobriyan, Thomas Gleixner,
	Greg Kroah-Hartman, Christian Brauner, Masami Hiramatsu

On Tue 28-04-20 10:09:37, Vlastimil Babka wrote:
> On 4/27/20 8:33 PM, Andrew Morton wrote:
> > On Mon, 27 Apr 2020 20:04:29 +0200 Vlastimil Babka <vbabka@suse.cz> wrote:
> > 
> > > ...
> > > +	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?

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.
-- 
Michal Hocko
SUSE Labs


^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH v3 5/5] lib/test_sysctl: support testing of sysctl. boot parameter
  2020-04-27 18:39   ` Luis Chamberlain
@ 2020-05-11 11:05     ` Vlastimil Babka
  2020-05-11 18:31       ` Luis Chamberlain
  0 siblings, 1 reply; 17+ messages in thread
From: Vlastimil Babka @ 2020-05-11 11:05 UTC (permalink / raw)
  To: Luis Chamberlain
  Cc: Andrew Morton, Kees Cook, Iurii Zaikin, linux-kernel, linux-api,
	linux-mm, Ivan Teterevkov, Michal Hocko, David Rientjes,
	Matthew Wilcox, Eric W . Biederman, Guilherme G . Piccoli,
	Alexey Dobriyan, Thomas Gleixner, Greg Kroah-Hartman,
	Christian Brauner, Masami Hiramatsu


On 4/27/20 8:39 PM, Luis Chamberlain wrote:
> On Mon, Apr 27, 2020 at 08:04:33PM +0200, Vlastimil Babka wrote:
> Nice, also we could just require
> 
> diff --git a/tools/testing/selftests/sysctl/config b/tools/testing/selftests/sysctl/config
> index 6ca14800d755..34461cc99a2b 100644
> --- a/tools/testing/selftests/sysctl/config
> +++ b/tools/testing/selftests/sysctl/config
> @@ -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.

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. 

>> +
>> +	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.

> 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 <vbabka@suse.cz>
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."

Suggested-by: Luis Chamberlain <mcgrof@kernel.org>
Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
---
 tools/testing/selftests/sysctl/sysctl.sh | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/tools/testing/selftests/sysctl/sysctl.sh b/tools/testing/selftests/sysctl/sysctl.sh
index ef6417b8067b..148704f465b5 100755
--- a/tools/testing/selftests/sysctl/sysctl.sh
+++ b/tools/testing/selftests/sysctl/sysctl.sh
@@ -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
-- 
2.26.2



^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH v3 5/5] lib/test_sysctl: support testing of sysctl. boot parameter
  2020-05-11 11:05     ` Vlastimil Babka
@ 2020-05-11 18:31       ` Luis Chamberlain
  2020-05-13  8:58         ` Vlastimil Babka
  0 siblings, 1 reply; 17+ messages in thread
From: Luis Chamberlain @ 2020-05-11 18:31 UTC (permalink / raw)
  To: Vlastimil Babka
  Cc: Andrew Morton, Kees Cook, Iurii Zaikin, linux-kernel, linux-api,
	linux-mm, Ivan Teterevkov, Michal Hocko, David Rientjes,
	Matthew Wilcox, Eric W . Biederman, Guilherme G . Piccoli,
	Alexey Dobriyan, Thomas Gleixner, Greg Kroah-Hartman,
	Christian Brauner, Masami Hiramatsu

On Mon, May 11, 2020 at 01:05:22PM +0200, Vlastimil Babka wrote:
> 
> On 4/27/20 8:39 PM, Luis Chamberlain wrote:
> > On Mon, Apr 27, 2020 at 08:04:33PM +0200, Vlastimil Babka wrote:
> > Nice, also we could just require
> > 
> > diff --git a/tools/testing/selftests/sysctl/config b/tools/testing/selftests/sysctl/config
> > index 6ca14800d755..34461cc99a2b 100644
> > --- a/tools/testing/selftests/sysctl/config
> > +++ b/tools/testing/selftests/sysctl/config
> > @@ -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.
> 
> 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.

> >> +
> >> +	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.

> > 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 <vbabka@suse.cz>
> 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."
> 
> Suggested-by: Luis Chamberlain <mcgrof@kernel.org>
> Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
> ---
>  tools/testing/selftests/sysctl/sysctl.sh | 5 +++++
>  1 file changed, 5 insertions(+)
> 
> diff --git a/tools/testing/selftests/sysctl/sysctl.sh b/tools/testing/selftests/sysctl/sysctl.sh
> index ef6417b8067b..148704f465b5 100755
> --- a/tools/testing/selftests/sysctl/sysctl.sh
> +++ b/tools/testing/selftests/sysctl/sysctl.sh
> @@ -756,6 +756,11 @@ sysctl_test_0006()

You want to:

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

>  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
> +
>  	if [ -d $DIR ]; then
>  		echo "Boot param test only possible sysctl_test is built-in, not module:"
>  		cat $TEST_DIR/config >&2
> -- 
> 2.26.2
> 


^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH v3 5/5] lib/test_sysctl: support testing of sysctl. boot parameter
  2020-05-11 18:31       ` Luis Chamberlain
@ 2020-05-13  8:58         ` Vlastimil Babka
  2020-05-13 13:15           ` Luis Chamberlain
  0 siblings, 1 reply; 17+ messages in thread
From: Vlastimil Babka @ 2020-05-13  8:58 UTC (permalink / raw)
  To: Luis Chamberlain
  Cc: Andrew Morton, Kees Cook, Iurii Zaikin, linux-kernel, linux-api,
	linux-mm, Ivan Teterevkov, Michal Hocko, David Rientjes,
	Matthew Wilcox, Eric W . Biederman, Guilherme G . Piccoli,
	Alexey Dobriyan, Thomas Gleixner, Greg Kroah-Hartman,
	Christian Brauner, Masami Hiramatsu

On 5/11/20 8:31 PM, Luis Chamberlain wrote:
> On Mon, May 11, 2020 at 01:05:22PM +0200, Vlastimil Babka wrote:
>> ----8<----
>> From a999e993a89e521b152bbd4b1466f69e62879c30 Mon Sep 17 00:00:00 2001
>> From: Vlastimil Babka <vbabka@suse.cz>
>> 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."
>> 
>> Suggested-by: Luis Chamberlain <mcgrof@kernel.org>
>> Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
>> ---
>>  tools/testing/selftests/sysctl/sysctl.sh | 5 +++++
>>  1 file changed, 5 insertions(+)
>> 
>> diff --git a/tools/testing/selftests/sysctl/sysctl.sh b/tools/testing/selftests/sysctl/sysctl.sh
>> index ef6417b8067b..148704f465b5 100755
>> --- a/tools/testing/selftests/sysctl/sysctl.sh
>> +++ b/tools/testing/selftests/sysctl/sysctl.sh
>> @@ -756,6 +756,11 @@ sysctl_test_0006()
> 
> You want to:
> 
>                                                                                 
> # Kselftest framework requirement - SKIP code is 4.                             
> ksft_skip=4 
> 
>>  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

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



^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH v3 5/5] lib/test_sysctl: support testing of sysctl. boot parameter
  2020-05-13  8:58         ` Vlastimil Babka
@ 2020-05-13 13:15           ` Luis Chamberlain
  2020-05-13 13:17             ` Luis Chamberlain
  0 siblings, 1 reply; 17+ messages in thread
From: Luis Chamberlain @ 2020-05-13 13:15 UTC (permalink / raw)
  To: Vlastimil Babka
  Cc: Andrew Morton, Kees Cook, Iurii Zaikin, linux-kernel, linux-api,
	linux-mm, Ivan Teterevkov, Michal Hocko, David Rientjes,
	Matthew Wilcox, Eric W . Biederman, Guilherme G . Piccoli,
	Alexey Dobriyan, Thomas Gleixner, Greg Kroah-Hartman,
	Christian Brauner, Masami Hiramatsu

On Wed, May 13, 2020 at 10:58:16AM +0200, Vlastimil Babka wrote:
> On 5/11/20 8:31 PM, Luis Chamberlain wrote:
> > On Mon, May 11, 2020 at 01:05:22PM +0200, Vlastimil Babka wrote:
> >> ----8<----
> >> From a999e993a89e521b152bbd4b1466f69e62879c30 Mon Sep 17 00:00:00 2001
> >> From: Vlastimil Babka <vbabka@suse.cz>
> >> 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."
> >> 
> >> Suggested-by: Luis Chamberlain <mcgrof@kernel.org>
> >> Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
> >> ---
> >>  tools/testing/selftests/sysctl/sysctl.sh | 5 +++++
> >>  1 file changed, 5 insertions(+)
> >> 
> >> diff --git a/tools/testing/selftests/sysctl/sysctl.sh b/tools/testing/selftests/sysctl/sysctl.sh
> >> index ef6417b8067b..148704f465b5 100755
> >> --- a/tools/testing/selftests/sysctl/sysctl.sh
> >> +++ b/tools/testing/selftests/sysctl/sysctl.sh
> >> @@ -756,6 +756,11 @@ sysctl_test_0006()
> > 
> > You want to:
> > 
> >                                                                                 
> > # Kselftest framework requirement - SKIP code is 4.                             
> > ksft_skip=4 
> > 
> >>  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?

yes please do that.

  Luis


^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH v3 5/5] lib/test_sysctl: support testing of sysctl. boot parameter
  2020-05-13 13:15           ` Luis Chamberlain
@ 2020-05-13 13:17             ` Luis Chamberlain
  2020-05-15 16:02               ` Vlastimil Babka
  0 siblings, 1 reply; 17+ messages in thread
From: Luis Chamberlain @ 2020-05-13 13:17 UTC (permalink / raw)
  To: Vlastimil Babka
  Cc: Andrew Morton, Kees Cook, Iurii Zaikin, linux-kernel, Linux API,
	linux-mm, Ivan Teterevkov, Michal Hocko, David Rientjes,
	Matthew Wilcox, Eric W . Biederman, Guilherme G . Piccoli,
	Alexey Dobriyan, Thomas Gleixner, Greg Kroah-Hartman,
	Christian Brauner, Masami Hiramatsu

On Wed, May 13, 2020 at 7:15 AM Luis Chamberlain <mcgrof@kernel.org> wrote:
>
> On Wed, May 13, 2020 at 10:58:16AM +0200, Vlastimil Babka wrote:
> > On 5/11/20 8:31 PM, Luis Chamberlain wrote:
> > > On Mon, May 11, 2020 at 01:05:22PM +0200, Vlastimil Babka wrote:
> > >> ----8<----
> > >> From a999e993a89e521b152bbd4b1466f69e62879c30 Mon Sep 17 00:00:00 2001
> > >> From: Vlastimil Babka <vbabka@suse.cz>
> > >> 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."
> > >>
> > >> Suggested-by: Luis Chamberlain <mcgrof@kernel.org>
> > >> Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
> > >> ---
> > >>  tools/testing/selftests/sysctl/sysctl.sh | 5 +++++
> > >>  1 file changed, 5 insertions(+)
> > >>
> > >> diff --git a/tools/testing/selftests/sysctl/sysctl.sh b/tools/testing/selftests/sysctl/sysctl.sh
> > >> index ef6417b8067b..148704f465b5 100755
> > >> --- a/tools/testing/selftests/sysctl/sysctl.sh
> > >> +++ b/tools/testing/selftests/sysctl/sysctl.sh
> > >> @@ -756,6 +756,11 @@ sysctl_test_0006()
> > >
> > > You want to:
> > >
> > >
> > > # Kselftest framework requirement - SKIP code is 4.
> > > ksft_skip=4
> > >
> > >>  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?
>
> yes please do that.

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


^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH v3 5/5] lib/test_sysctl: support testing of sysctl. boot parameter
  2020-05-13 13:17             ` Luis Chamberlain
@ 2020-05-15 16:02               ` Vlastimil Babka
  0 siblings, 0 replies; 17+ messages in thread
From: Vlastimil Babka @ 2020-05-15 16:02 UTC (permalink / raw)
  To: Luis Chamberlain
  Cc: Andrew Morton, Kees Cook, Iurii Zaikin, linux-kernel, Linux API,
	linux-mm, Ivan Teterevkov, Michal Hocko, David Rientjes,
	Matthew Wilcox, Eric W . Biederman, Guilherme G . Piccoli,
	Alexey Dobriyan, Thomas Gleixner, Greg Kroah-Hartman,
	Christian Brauner, Masami Hiramatsu

On 5/13/20 3:17 PM, Luis Chamberlain wrote:
> On Wed, May 13, 2020 at 7:15 AM Luis Chamberlain <mcgrof@kernel.org> wrote:
>> > >
>> > > You want to:
>> > >
>> > >
>> > > # Kselftest framework requirement - SKIP code is 4.
>> > > ksft_skip=4
>> > >
>> > >>  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?
>>
>> 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 <vbabka@suse.cz>
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."

Suggested-by: Luis Chamberlain <mcgrof@kernel.org>
Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
---
 tools/testing/selftests/sysctl/sysctl.sh | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/tools/testing/selftests/sysctl/sysctl.sh b/tools/testing/selftests/sysctl/sysctl.sh
index ef6417b8067b..ab44d3e65986 100755
--- a/tools/testing/selftests/sysctl/sysctl.sh
+++ b/tools/testing/selftests/sysctl/sysctl.sh
@@ -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()
-- 
2.26.2




^ permalink raw reply	[flat|nested] 17+ messages in thread

end of thread, other threads:[~2020-05-15 16:02 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-27 18:04 [PATCH v3 0/5] support setting sysctl parameters from kernel command line Vlastimil Babka
2020-04-27 18:04 ` [PATCH v3 1/5] kernel/sysctl: " Vlastimil Babka
2020-04-27 18:33   ` Andrew Morton
2020-04-28  8:09     ` Vlastimil Babka
2020-04-28 10:18       ` Michal Hocko
2020-04-27 18:04 ` [PATCH v3 2/5] kernel/sysctl: support handling command line aliases Vlastimil Babka
2020-04-27 18:04 ` [PATCH v3 3/5] kernel/hung_task convert hung_task_panic boot parameter to sysctl Vlastimil Babka
2020-04-27 18:04 ` [PATCH v3 4/5] tools/testing/selftests/sysctl/sysctl.sh: support CONFIG_TEST_SYSCTL=y Vlastimil Babka
2020-04-27 18:39   ` Luis Chamberlain
2020-04-27 18:04 ` [PATCH v3 5/5] lib/test_sysctl: support testing of sysctl. boot parameter Vlastimil Babka
2020-04-27 18:39   ` Luis Chamberlain
2020-05-11 11:05     ` Vlastimil Babka
2020-05-11 18:31       ` Luis Chamberlain
2020-05-13  8:58         ` Vlastimil Babka
2020-05-13 13:15           ` Luis Chamberlain
2020-05-13 13:17             ` Luis Chamberlain
2020-05-15 16:02               ` Vlastimil Babka

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).