support setting sysctl parameters from kernel command line
Subsystems (7)
PATCH 3/3 kernel/hung_task convert hung_task_panic boot parameter to sysctl
In reply to Vlastimil BabkaWe 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
@@ -1457,7 +1457,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
@@ -1743,6 +1743,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
@@ -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.25.1
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
Anyway I hope that Patch 1 is mature enough to go regardless of the fate of the less important second part.
Changes since RFCv2
- make proc_mnt internal to functions (Kees)
- use kasprintf when building path (Kees)
- improve error reporting - common errnos are translated to more obvious messages and the rest uses %pe
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
.../admin-guide/kernel-parameters.txt | 11 +- fs/proc/proc_sysctl.c | 135 ++++++++++++++++++ include/linux/sysctl.h | 4 + init/main.c | 2 + kernel/hung_task.c | 10 -- mm/page_alloc.c | 9 -- 6 files changed, 151 insertions(+), 20 deletions(-)
Signature · 1 line
2.25.1
PATCH 1/3 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. 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 | 100 ++++++++++++++++++ include/linux/sysctl.h | 4 + init/main.c | 2 + 4 files changed, 115 insertions(+)
Documentation/admin-guide/kernel-parameters.txt +9 -0
@@ -4793,6 +4793,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 +100 -0
109 lines not on this page · raw message
include/linux/sysctl.h +4 -0
@@ -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, { } +void do_sysctl_args (void ) +{ +}#endif /* CONFIG_SYSCTL */ int sysctl_max_threads(struct ctl_table *table, int write,
init/main.c +2 -0
@@ -1367,6 +1367,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.25.1
PATCH 2/3 kernel/sysctl: support handling command line aliases
In reply to Vlastimil BabkaWe 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
@@ -1727,6 +1727,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)
@@ -1740,15 +1771,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 ; + }if (!proc_mnt) { proc_fs_type =get_fs_type ("proc" );
mm/page_alloc.c +0 -9
@@ -5460,15 +5460,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.25.1
Re: [PATCH 1/3] kernel/sysctl: support setting sysctl parameters from kernel command line
In reply to Vlastimil BabkaQuoted 30 lines · Vlastimil Babka
A recently proposed patch to add
vm_swappinesscommand 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_swappinessissue 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. 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.
Quoted 5 lines
[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]/Signed-off-by Vlastimil Babka <[email protected]>
Boo, all the error prints should terminate with \n Will wait for feedback before resend.
Re: [PATCH 1/3] kernel/sysctl: support setting sysctl parameters from kernel command line
In reply to Vlastimil BabkaQuoted 75 lines · Vlastimil Babka
A recently proposed patch to add
vm_swappinesscommand 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_swappinessissue 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. 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]/Signed-off-by Vlastimil Babka <[email protected]>--- .../admin-guide/kernel-parameters.txt | 9 ++ fs/proc/proc_sysctl.c | 100 ++++++++++++++++++ include/linux/sysctl.h | 4 + init/main.c | 2 + 4 files changed, 115 insertions(+)Documentation/admin-guide/kernel-parameters.txt +9
-0@@ -4793,6 +4793,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 enabledfs/proc/proc_sysctl.c +1
-0@@ -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;
fs/proc/proc_sysctl.c int __init proc_sys_init(void) · line 1735
Quoted 10 lines
fs/proc/proc_sysctl.c +7
-0@@ -1725,3 +1726,102 @@ 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 = *((struct vfsmount **)arg);
I would just make this:
struct vfsmount **proc_mnt = (struct vfsmount **)arg;
Quoted 8 lines
fs/proc/proc_sysctl.c +8
-0+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 ))
fs/proc/proc_sysctl.c int __init proc_sys_init(void) · line 1753
Quoted 10 lines
fs/proc/proc_sysctl.c +10
-0+return 0 ; + + param +=sizeof ("sysctl" ) -1 ; + +if (param[0 ] !='/' && param[0 ] !='.' ) +return 0 ; + + param++; + +if (!proc_mnt) {
if (!*proc_mnt) {I would also add a comment here to explain that this is doing an on-demand mount so that it doesn't have to mount proc if there are not sysctl parameters.
fs/proc/proc_sysctl.c int __init proc_sys_init(void) · line 1759
Quoted 6 lines
fs/proc/proc_sysctl.c +6
-0+ proc_fs_type =get_fs_type ("proc" ); +if (!proc_fs_type) { +pr_err ("Failed to find procfs to set sysctl from command line" ); +return 0 ; + } + proc_mnt =kern_mount (proc_fs_type);
*proc_mnt = kern_mount(proc_fs_type);
fs/proc/proc_sysctl.c int __init proc_sys_init(void) · line 1761
Quoted 2 lines
fs/proc/proc_sysctl.c +2
-0+put_filesystem (proc_fs_type); +if (IS_ERR (proc_mnt)) {
if (IS_ERR(*proc_mnt)) {fs/proc/proc_sysctl.c int __init proc_sys_init(void) · line 1765
Quoted 4 lines
fs/proc/proc_sysctl.c +4
-0+pr_err ("Failed to mount procfs to set sysctl from command line" ); +return 0 ; + } + *((struct vfsmount **)arg) = proc_mnt;
Then drop this line.
fs/proc/proc_sysctl.c int __init proc_sys_init(void) · line 1773
Quoted 8 lines
fs/proc/proc_sysctl.c +8
-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 );
file = file_open_root((*proc_mnt)->mnt_root, *proc_mnt, path, O_WRONLY, 0);
Quoted 67 lines
fs/proc/proc_sysctl.c +54
-0+if (IS_ERR (file)) { + err =PTR_ERR (file); +if (err == -ENOENT ) +pr_err ("Failed to set sysctl parameter '%s=%s': parameter not found" , + param, val); +else if (err == -EACCES ) +pr_err ("Failed to set sysctl parameter '%s=%s': permission denied (read-only?)" , + param, val); +else +pr_err ("Error %pe opening proc file to set sysctl parameter '%s=%s'" , + 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" , + param, val); +else +pr_err ("Error %pe writing to proc file to set sysctl parameter '%s=%s'" , +ERR_PTR (err), param, val); + }else if (wret != len) { +pr_err ("Wrote only %ld bytes of %d writing to proc file %s to set sysctl parameter '%s=%s'" , + 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'" , +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); +}include/linux/sysctl.h +1
-0@@ -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[];
include/linux/sysctl.h static inline void setup_sysctl_set(struct ctl_table_set *p, · line 240
Quoted 4 lines
include/linux/sysctl.h +1
-0@@ -236,6 +237,9 @@ static inline void setup_sysctl_set(struct ctl_table_set *p, { } +void do_sysctl_args (void )
As with the others in the no-op case:
static inline void do_sysctl_args(void)
Quoted 21 lines
include/linux/sysctl.h +2
-0+{ +}#endif /* CONFIG_SYSCTL */ int sysctl_max_threads(struct ctl_table *table, int write,init/main.c +2
-0@@ -1367,6 +1367,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.25.1
Otherwise, yes, looks good!
Signature · 1 line
Kees Cook
Re: [PATCH 1/3] kernel/sysctl: support setting sysctl parameters from kernel command line
In reply to Vlastimil BabkaQuoted 2 lines · Vlastimil Babka
Boo, all the error prints should terminate with \n Will wait for feedback before resend.
eek, yes, good catch. :)
Signature · 1 line
Kees Cook
Re: [PATCH 2/3] kernel/sysctl: support handling command line aliases
In reply to Vlastimil BabkaQuoted 10 lines · 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_orderparameter.Signed-off-by Vlastimil Babka <[email protected]>
-Kees
Quoted 96 lines
--- 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@@ -1727,6 +1727,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)@@ -1740,15 +1771,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 ; + }if (!proc_mnt) { proc_fs_type =get_fs_type ("proc" );mm/page_alloc.c +0
-9@@ -5460,15 +5460,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.25.1
Signature · 1 line
Kees Cook
Re: [PATCH 3/3] kernel/hung_task convert hung_task_panic boot parameter to sysctl
In reply to Vlastimil BabkaQuoted 9 lines · 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_panicparameter. 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 <[email protected]>
I'll let others double-check, but I think this change should be okay. If not, we can adjust the sysctl handler to accept an arbitrary int.
-Kees
Quoted 55 lines
--- 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@@ -1457,7 +1457,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 canfs/proc/proc_sysctl.c +1
-0@@ -1743,6 +1743,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@@ -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.25.1
Signature · 1 line
Kees Cook
kbuild test robot
build botRe: [PATCH 1/3] kernel/sysctl: support setting sysctl parameters from kernel command line
In reply to Vlastimil BabkaThank you for the patch! Perhaps something to improve:
Hi Vlastimil,
Thank you for the patch! Perhaps something to improve:
[auto build test WARNING on kees/for-next/pstore]
[also build test WARNING on linus/master linux/master v5.6 next-20200330] [if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please see https://stackoverflow.com/a/37406982]
url: https://github.com/0day-ci/linux/commits/Vlastimil-Babka/support-setting-sysctl-parameters-from-kernel-command-line/20200331-023453 base: https://git.kernel.org/pub/scm/linux/kernel/git/kees/linux.git for-next/pstore config: um-i386_defconfig (attached as .config) compiler: gcc-7 (Debian 7.4.0-6) 7.4.0 reproduce: # save the attached .config to linux build tree make ARCH=um SUBARCH=i386
If you fix the issue, kindly add following tag
All warnings (new ones prefixed by >>):
In file included from include/linux/printk.h:7:0,
from include/linux/kernel.h:15,
from include/linux/list.h:9,
from include/linux/sysctl.h:25,
from fs/proc/proc_sysctl.c:6:
fs/proc/proc_sysctl.c: In function 'process_sysctl_arg':
include/linux/kern_levels.h:5:18: warning: format '%ld' expects argument of type 'long int', but argument 2 has type 'ssize_t {aka int}' [-Wformat=]
#define KERN_SOH "\001" /* ASCII Start Of Header */
^
include/linux/kern_levels.h:11:18: note: in expansion of macro 'KERN_SOH'
#define KERN_ERR KERN_SOH "3" /* error conditions */
^~~~~~~~
include/linux/printk.h:304:9: note: in expansion of macro 'KERN_ERR'
printk(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
^~~~~~~~
>> fs/proc/proc_sysctl.c:1798:3: note: in expansion of macro 'pr_err'
pr_err("Wrote only %ld bytes of %d writing to proc file %s to set sysctl parameter '%s=%s'",
^~~~~~
fs/proc/proc_sysctl.c:1798:24: note: format string is defined here
pr_err("Wrote only %ld bytes of %d writing to proc file %s to set sysctl parameter '%s=%s'",
~~^
%dvim +/pr_err +1798 fs/proc/proc_sysctl.c
1729
1730 /* Set sysctl value passed on kernel command line. */
1731 static int process_sysctl_arg(char *param, char *val,
1732 const char *unused, void *arg)
1733 {
1734 char *path;
1735 struct vfsmount *proc_mnt = *((struct vfsmount **)arg);
1736 struct file_system_type *proc_fs_type;
1737 struct file *file;
1738 int len;
1739 int err;
1740 loff_t pos = 0;
1741 ssize_t wret;
1742
1743 if (strncmp(param, "sysctl", sizeof("sysctl") - 1))
1744 return 0;
1745
1746 param += sizeof("sysctl") - 1;
1747
1748 if (param[0] != '/' && param[0] != '.')
1749 return 0;
1750
1751 param++;
1752
1753 if (!proc_mnt) {
1754 proc_fs_type = get_fs_type("proc");
1755 if (!proc_fs_type) {
1756 pr_err("Failed to find procfs to set sysctl from command line");
1757 return 0;
1758 }
1759 proc_mnt = kern_mount(proc_fs_type);
1760 put_filesystem(proc_fs_type);
1761 if (IS_ERR(proc_mnt)) {
1762 pr_err("Failed to mount procfs to set sysctl from command line");
1763 return 0;
1764 }
1765 *((struct vfsmount **)arg) = proc_mnt;
1766 }
1767
1768 path = kasprintf(GFP_KERNEL, "sys/%s", param);
1769 if (!path)
1770 panic("%s: Failed to allocate path for %s\n", __func__, param);
1771 strreplace(path, '.', '/');
1772
1773 file = file_open_root(proc_mnt->mnt_root, proc_mnt, path, O_WRONLY, 0);
1774 if (IS_ERR(file)) {
1775 err = PTR_ERR(file);
1776 if (err == -ENOENT)
1777 pr_err("Failed to set sysctl parameter '%s=%s': parameter not found",
1778 param, val);
1779 else if (err == -EACCES)
1780 pr_err("Failed to set sysctl parameter '%s=%s': permission denied (read-only?)",
1781 param, val);
1782 else
1783 pr_err("Error %pe opening proc file to set sysctl parameter '%s=%s'",
1784 file, param, val);
1785 goto out;
1786 }
1787 len = strlen(val);
1788 wret = kernel_write(file, val, len, &pos);
1789 if (wret < 0) {
1790 err = wret;
1791 if (err == -EINVAL)
1792 pr_err("Failed to set sysctl parameter '%s=%s': invalid value",
1793 param, val);
1794 else
1795 pr_err("Error %pe writing to proc file to set sysctl parameter '%s=%s'",
1796 ERR_PTR(err), param, val);
1797 } else if (wret != len) {
> 1798 pr_err("Wrote only %ld bytes of %d writing to proc file %s to set sysctl parameter '%s=%s'", 1799 wret, len, path, param, val);
1800 }
1801
1802 err = filp_close(file, NULL);
1803 if (err)
1804 pr_err("Error %pe closing proc file to set sysctl parameter '%s=%s'",
1805 ERR_PTR(err), param, val);
1806 out:
1807 kfree(path);
1808 return 0;
1809 }
1810
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
Re: [PATCH 1/3] kernel/sysctl: support setting sysctl parameters from kernel command line
In reply to Vlastimil BabkaSorry to be late to the apocalypse review party for this, feedback below.
Quoted 13 lines · Vlastimil Babka
A recently proposed patch to add
vm_swappinesscommand 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_swappinessissue 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.
Quoted 5 lines
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" might be safer to add.
Quoted 8 lines
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.
This is wonderful when we think about existing sysctls which have corresponding silly boot params that do the same thing. However, shoving a boot param capability down every possible built-in sysctl brings forward support considerations we should take serious, as this would add a new user interface and we'll have to support it.
Simply put, not all sysctls should be born to be boot params. I suggest we white-list which ones we can process, so that only sysctls we *do* review and agree are good candidates get allowed to also be boot params. Calling a proc hanlder early might seem functional, but if the subsystem defers evaluation of a setting later, then any boot param set would be lifted anyway. I think each syscl would need to be reviewed for this to be supported in a way that doesn't create odd unexpected system settings which we later have to support forever.
Should we not do this, we'll have to live with the consequences of supporting the full swoop of sysctls are boot params, whatever consequences those may be.
Quoted 3 lines
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.
I'm just not sure if its worth supporting these, for modules we have module params, but those with more creative userspace might have a better idea as to why we'd want to support this. I just can't see it yet.
Quoted 126 lines
[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]/Signed-off-by Vlastimil Babka <[email protected]>--- .../admin-guide/kernel-parameters.txt | 9 ++ fs/proc/proc_sysctl.c | 100 ++++++++++++++++++ include/linux/sysctl.h | 4 + init/main.c | 2 + 4 files changed, 115 insertions(+)Documentation/admin-guide/kernel-parameters.txt +9
-0@@ -4793,6 +4793,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 enabledfs/proc/proc_sysctl.c +79
-0@@ -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;@@ -1725,3 +1726,102 @@ 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 = *((struct vfsmount **)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++; + +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" ); +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" ); +return 0 ; + } + *((struct vfsmount **)arg) = proc_mnt; + } + + 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" , + param, val); +else if (err == -EACCES ) +pr_err ("Failed to set sysctl parameter '%s=%s': permission denied (read-only?)" , + param, val); +else +pr_err ("Error %pe opening proc file to set sysctl parameter '%s=%s'" , + 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" , + param, val); +else +pr_err ("Error %pe writing to proc file to set sysctl parameter '%s=%s'" , +ERR_PTR (err), param, val); + }else if (wret != len) { +pr_err ("Wrote only %ld bytes of %d writing to proc file %s to set sysctl parameter '%s=%s'" , + 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'" , +ERR_PTR (err), param, val); +out:
fs/proc/proc_sysctl.c int __init proc_sys_init(void) · line 1816
Quoted 10 lines
fs/proc/proc_sysctl.c +10
-0+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 );
can you use kstrndup() ? And then use kfree_const()? Yes, feel free to move __kstrncpy() to a generic kstrncpy().
fs/proc/proc_sysctl.c int __init proc_sys_init(void) · line 1827
Quoted 11 lines
fs/proc/proc_sysctl.c +11
-0+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); +}
Then, can we get this tested as part of lib/test_sysctl.c with its respective tools/testing/selftests/sysctl/sysctl.sh ?
Luis
Re: [PATCH 3/3] kernel/hung_task convert hung_task_panic boot parameter to sysctl
In reply to Kees CookOn 3/30/20 10:43 AM, Kees Cook wrote:
...
Quoted 8 lines
Quoted 8 lines
Documentation/admin-guide/kernel-parameters.txt +0
-0@@ -1457,7 +1457,7 @@ [KNL] Should the hung task detector generate panics. Format: <integer>
Quoted 13 lines
fs/proc/proc_sysctl.c struct sysctl_alias { · line 1745
Quoted 13 lines
Documentation/admin-guide/kernel-parameters.txt +1
-1- 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 canfs/proc/proc_sysctl.c +0
-0@@ -1743,6 +1743,7 @@ struct sysctl_alias {*/ static const struct sysctl_alias sysctl_aliases[] = { {"numa_zonelist_order" ,"vm.numa_zonelist_order" },
Hi Vlastimil,
Maybe best to delete the above line? Because:
a) it was added as an example, and now that you have a real use case in this patch, the example is no longer required, and
b) numa_zonelist_order is deprecated, as a boot param. Adding support to it in this brand-new mechanism seems to be going a bit in the opposite direction of deprecation.
And, I don't think you really want all the sysctls to be enabled as boot params, right? Your comment right above sysctl_aliases[] (shown in patch 2) sort of indicates that only some items are meant to be both sysctl's and boot params. And that makes sense.
In fact, the sysctl_aliases[] is (or could be) effectively the whitelist that Luis Chamberlain was requesting in another thread. A whitelist makes good sense, for the reasons Luis listed. As such, keeping it limited to items that we want, seems like the way to go, IMHO.
thanks,
Signature · 2 lines
John Hubbard NVIDIA
Quoted 29 lines
Quoted 28 lines
fs/proc/proc_sysctl.c +1
-0+ {"hung_task_panic", "kernel.hung_task_panic" }, { } };kernel/hung_task.c +0
-10@@ -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.25.1
Re: [PATCH 3/3] kernel/hung_task convert hung_task_panic boot parameter to sysctl
Quoted 27 lines · John Hubbard
On 3/30/20 10:43 AM, Kees Cook wrote:
...Quoted 8 lines
Quoted 8 lines
Documentation/admin-guide/kernel-parameters.txt +0
-0@@ -1457,7 +1457,7 @@ [KNL] Should the hung task detector generate panics. Format: <integer>Quoted 13 lines
fs/proc/proc_sysctl.c struct sysctl_alias { · line 1745
Quoted 13 lines
Documentation/admin-guide/kernel-parameters.txt +1
-1- 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 canfs/proc/proc_sysctl.c +0
-0@@ -1743,6 +1743,7 @@ struct sysctl_alias {*/ static const struct sysctl_alias sysctl_aliases[] = { {"numa_zonelist_order" ,"vm.numa_zonelist_order" },Hi Vlastimil,
Quoted 7 lines
Maybe best to delete the above line? Because:
a) it was added as an example, and now that you have a real use case in this patch, the example is no longer required, and
b)
numa_zonelist_orderis deprecated, as a boot param. Adding support to it in this brand-new mechanism seems to be going a bit in the opposite direction of deprecation.
Well, this aliases table is not the brand new mechanism, it's just for handling sysctls that also have a legacy boot param. numa_zonelist_order is such legacy boot param, so we can handle it here instead of its special handler. If we decide to remove it later, we can do that, but there is no user-visible effect on its deprecation by this series.
Quoted 1 line
And, I don't think you really want all the sysctls to be enabled as boot params, right? Your
The point of Patch 1 is very much so that all sysctls can be set using a boot param in the form of sysctl.foo.bar=baz
Quoted 2 lines
comment right above sysctl_aliases[] (shown in patch 2) sort of indicates that only some items are meant to be both sysctl's and boot params. And that makes sense.
Patches 2+3 are only about handling the legacy boot params that have a sysctl counterpart.
Quoted 3 lines
In fact, the sysctl_aliases[] is (or could be) effectively the whitelist that Luis Chamberlain was requesting in another thread. A whitelist makes good sense, for the reasons Luis listed. As such, keeping it limited to items that we want, seems like the way to go, IMHO.
See my reply there once I send it :)
Quoted 2 lines
thanks,
Re: [PATCH 1/3] kernel/sysctl: support setting sysctl parameters from kernel command line
In reply to Luis ChamberlainQuoted 16 lines · Luis Chamberlain
Sorry to be late to the apocalypse review party for this, feedback below.
Quoted 13 lines · Vlastimil Babka
A recently proposed patch to add
vm_swappinesscommand 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_swappinessissue 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.
Quoted 7 lines
Quoted 5 lines
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" might be safer to add.
OK
Quoted 9 lines
Quoted 8 lines
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.
Quoted 5 lines
This is wonderful when we think about existing sysctls which have corresponding silly boot params that do the same thing. However, shoving a boot param capability down every possible built-in sysctl brings forward support considerations we should take serious, as this would add a new user interface and we'll have to support it.
Hmm, if I boot with an initramfs with init process that does mount /proc and set some sysctl there as the very first thing, then this will be almost the same moment as my patch does it. There is no further kernel initialization in between. So with your logic we already do support all non-modular sysctls to be set so early.
Quoted 3 lines
Simply put, not all sysctls should be born to be boot params. I suggest we white-list which ones we can process, so that only sysctls we *do* review and agree are good candidates get allowed to also be boot params.
By above, the nuber of sysctls that will be problematic with this boot param mechanism, but work properly when set from init process immediately, should be near zero, and I would expect truly zero. As such, whitelist approach seems excessive to me and it would take a lot of effort to build it, and it will be a lottery which sysctl would work as boot param on which kernel version. Sounds like a lot of trouble for little benefit to me.
Quoted 3 lines
Calling a proc hanlder early might seem functional, but if the subsystem defers evaluation of a setting later, then any boot param set would be lifted anyway.
I'm not sure I understand, can you show me some example please?
Quoted 3 lines
I think each syscl would need to be reviewed for this to
be supported in a way that doesn't create odd unexpected system settings which we later have to support forever.
We would already do per the initramfs argument.
Quoted 3 lines
Should we not do this, we'll have to live with the consequences of supporting the full swoop of sysctls are boot params, whatever consequences those may be.
Of course when the first user tries to set some particular sysctl as boot param and finds and reports it doesn't work as intended, then it can be fixed or blacklisted and it can't break anyone else?
Quoted 8 lines
Quoted 3 lines
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.
I'm just not sure if its worth supporting these, for modules we have module params, but those with more creative userspace might have a better idea as to why we'd want to support this. I just can't see it yet.
Sure, I can defer that part for later now.
Quoted 8 lines
Quoted 6 lines
[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]/Signed-off-by Vlastimil Babka <[email protected]>---
Re: [PATCH 1/3] kernel/sysctl: support setting sysctl parameters from kernel command line
On Tue 31-03-20 09:42:46, Vlastimil Babka wrote:
[...]
Quoted 7 lines
Quoted 3 lines
Should we not do this, we'll have to live with the consequences of supporting the full swoop of sysctls are boot params, whatever consequences those may be.
Of course when the first user tries to set some particular sysctl as boot param and finds and reports it doesn't work as intended, then it can be fixed or blacklisted and it can't break anyone else?
Absolutely agreed. I would be really careful to not overengineer this whole thing.
Signature · 2 lines
Michal Hocko SUSE Labs
Luis Chamberlain
Reviewed-byRe: [PATCH 1/3] kernel/sysctl: support setting sysctl parameters from kernel command line
In reply to Vlastimil BabkaQuoted 8 lines · Vlastimil Babka
Quoted 6 lines · Luis Chamberlain
This is wonderful when we think about existing sysctls which have corresponding silly boot params that do the same thing. However, shoving a boot param capability down every possible built-in sysctl brings forward support considerations we should take serious, as this would add a new user interface and we'll have to support it.
Quoted 5 lines
Hmm, if I boot with an initramfs with init process that does mount /proc and set some sysctl there as the very first thing, then this will be almost the same moment as my patch does it. There is no further kernel initialization in between. So with your logic we already do support all non-modular sysctls to be set so early.
Yes, true. Then by all means:
Luis
Re: [PATCH 2/3] kernel/sysctl: support handling command line aliases
In reply to Vlastimil BabkaQuoted 10 lines · 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_orderparameter.Signed-off-by Vlastimil Babka <[email protected]>
Luis
Re: [PATCH 3/3] kernel/hung_task convert hung_task_panic boot parameter to sysctl
In reply to Vlastimil BabkaOn 3/31/20 12:27 AM, Vlastimil Babka wrote:
...
Quoted 3 lines
Patches 2+3 are only about handling the legacy boot params that have a sysctl counterpart.
OK, I misread what those were for.
Quoted 6 lines
Quoted 3 lines
In fact, the sysctl_aliases[] is (or could be) effectively the whitelist that Luis Chamberlain was requesting in another thread. A whitelist makes good sense, for the reasons Luis listed. As such, keeping it limited to items that we want, seems like the way to go, IMHO.
See my reply there once I send it :)
Saw that, and it all sounds good now.
thanks,
Signature · 2 lines
John Hubbard NVIDIA
Re: [PATCH 1/3] kernel/sysctl: support setting sysctl parameters from kernel command line
In reply to Michal HockoQuoted 6 lines · Michal Hocko
On Tue 31-03-20 09:42:46, Vlastimil Babka wrote:
[...]Quoted 4 lines
Quoted 3 lines
Should we not do this, we'll have to live with the consequences of supporting the full swoop of sysctls are boot params, whatever consequences those may be.
Quoted 6 lines
Quoted 3 lines
Of course when the first user tries to set some particular sysctl as boot param and finds and reports it doesn't work as intended, then it can be fixed or blacklisted and it can't break anyone else?
Absolutely agreed. I would be really careful to not overengineer this whole thing.
Right -- this is supposed to be _simple_, and I think that's the primary benefit here. If we encounter problems we can fix those cases. The careful place, I think, needs to be with converting existing boot params to be aliases. That's when timing considerations need to be taken into account carefully.
Signature · 1 line
Kees Cook
Re: [PATCH 1/3] kernel/sysctl: support setting sysctl parameters from kernel command line
In reply to kbuild test robotQuoted 23 lines · kbuild test robot
Hi Vlastimil,
Thank you for the patch! Perhaps something to improve:
[auto build test WARNING on kees/for-next/pstore]
[also build test WARNING on linus/master linux/master v5.6 next-20200330] [if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please see https://stackoverflow.com/a/37406982]url: https://github.com/0day-ci/linux/commits/Vlastimil-Babka/support-setting-sysctl-parameters-from-kernel-command-line/20200331-023453 base: https://git.kernel.org/pub/scm/linux/kernel/git/kees/linux.git for-next/pstore config: um-i386_defconfig (attached as .config) compiler: gcc-7 (Debian 7.4.0-6) 7.4.0 reproduce: # save the attached .config to linux build tree make ARCH=um SUBARCH=i386If you fix the issue, kindly add following tag
Reported-by kbuild test robot <[email protected]>All warnings (new ones prefixed by >>):
Quoted 7 lines
In file included from
include/linux/printk.h:7:0,
frominclude/linux/kernel.h:15,
frominclude/linux/list.h:9,
frominclude/linux/sysctl.h:25,
fromfs/proc/proc_sysctl.c:6:
fs/proc/proc_sysctl.c: In function 'process_sysctl_arg':
include/linux/kern_levels.h:5:18: warning: format '%ld' expects argument of type 'long int', but argument 2 has type 'ssize_t{aka int}' [-Wformat=]
Ah yes, this should be %zd I think?
Quoted 2 lines
Quoted 1 line
1798 pr_err("Wrote only %ld bytes of %d writing to proc file %s to set sysctl parameter '%s=%s'",
1799 wret, len, path, param, val);
-Kees
Signature · 1 line
Kees Cook
Re: [PATCH 3/3] kernel/hung_task convert hung_task_panic boot parameter to sysctl
In reply to Vlastimil Babkakernel/hung_task.c static struct task_struct *watchdog_task; · line 74
Quoted 13 lines · Vlastimil Babka
kernel/hung_task.c +0
-9@@ -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);
Can we defer removal of this handler for "one release cycle" (and instead emit a line saying that "this parameter will be replaced by ..." during that cycle) ? I welcome PATCH 1/3, but kernel testing projects (e.g. syzbot) needs to update their settings between PATCH 1/3 was merged into linux.git and PATCH 3/3 is merged into linux.git .
https://lkml.kernel.org/r/CACT4Y+YE-j5ncjTGN6UhngfCNRgVo-QDZ3VCBGACdbs9-v+axQ@mail.gmail.com says "Announcing unmerged changes is too early (as this patch showed). And once it's in linux-next it's already too late.."
Quoted 5 lines
kernel/hung_task.c +0
-1-static int hung_task_panic (struct notifier_block *this,unsigned long event,void *ptr) {
Re: [PATCH 3/3] kernel/hung_task convert hung_task_panic boot parameter to sysctl
Quoted 5 lines · Tetsuo Handa
Quoted 4 lines · Vlastimil Babka
kernel/hung_task.c +0
-0@@ -63,16 +63,6 @@ static struct task_struct *watchdog_task;unsigned int __read_mostly sysctl_hung_task_panic =CONFIG_BOOTPARAM_HUNG_TASK_PANIC_VALUE ;
Quoted 12 lines
kernel/hung_task.c static struct task_struct *watchdog_task; · line 74
Quoted 9 lines
kernel/hung_task.c +0
-9-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);Can we defer removal of this handler for "one release cycle" (and instead emit a line saying that "this parameter will be replaced by ..." during that cycle) ? I welcome
The old parameter is not removed, it's just handled differently, see patch 2. Both old name and new sysctl.name will work.
Quoted 12 lines
PATCH 1/3, but kernel testing projects (e.g. syzbot) needs to update their settings between PATCH 1/3 was merged into linux.git and PATCH 3/3 is merged into linux.git .
https://lkml.kernel.org/r/CACT4Y+YE-j5ncjTGN6UhngfCNRgVo-QDZ3VCBGACdbs9-v+axQ@mail.gmail.com says "Announcing unmerged changes is too early (as this patch showed). And once it's in linux-next it's already too late.."
Quoted 5 lines
kernel/hung_task.c +0
-1-static int hung_task_panic (struct notifier_block *this,unsigned long event,void *ptr) {
Re: [PATCH 1/3] kernel/sysctl: support setting sysctl parameters from kernel command line
In reply to Luis ChamberlainQuoted 14 lines · Luis Chamberlain
Quoted 14 lines
fs/proc/proc_sysctl.c +14
-0+ }else if (wret != len) { +pr_err ("Wrote only %ld bytes of %d writing to proc file %s to set sysctl parameter '%s=%s'" , + 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'" , +ERR_PTR (err), param, val); +out: +kfree (path); +return 0 ; +} +
Quoted 8 lines
fs/proc/proc_sysctl.c int __init proc_sys_init(void) · line 1816
Quoted 6 lines
fs/proc/proc_sysctl.c +6
-0+void do_sysctl_args (void ) +{ +char *command_line; +struct vfsmount *proc_mnt =NULL ; + + command_line =kstrdup (saved_command_line,GFP_KERNEL );can you use
kstrndup()? And then usekfree_const()? Yes, feel free to
I don't follow, what am I missing? Do you mean this?
size_t len = strlen(saved_command_line); command_line = kstrndup(saved_command_line, len, GFP_KERNEL);
What would be the advantage over plain kstrdup()?
As for kfree_const(), when would command_line be .rodata? I don't see using kstrndup() resulting in that.
Quoted 16 lines
move
__kstrncpy()to a generickstrncpy().fs/proc/proc_sysctl.c int __init proc_sys_init(void) · line 1827
Quoted 11 lines
fs/proc/proc_sysctl.c +11
-0+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); +}Then, can we get this tested as part of
lib/test_sysctl.cwith its respectivetools/testing/selftests/sysctl/sysctl.sh?
Hmm so I add some sysctl to the test "module" (in fact the 'config' file says it should be build with 'y', which would be needed anyway) and expand the test instructions so that the test kernel boot has to include it on the command line, and then I verify it has been set? Or do you see a better way?
Thanks,
Vlastimil
Quoted 2 lines
Luis
Re: [PATCH 1/3] kernel/sysctl: support setting sysctl parameters from kernel command line
Quoted 24 lines · Vlastimil Babka
Quoted 14 lines · Luis Chamberlain
Quoted 14 lines
fs/proc/proc_sysctl.c +14
-0+ }else if (wret != len) { +pr_err ("Wrote only %ld bytes of %d writing to proc file %s to set sysctl parameter '%s=%s'" , + 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'" , +ERR_PTR (err), param, val); +out: +kfree (path); +return 0 ; +} +Quoted 8 lines
fs/proc/proc_sysctl.c int __init proc_sys_init(void) · line 1816
Quoted 6 lines
fs/proc/proc_sysctl.c +6
-0+void do_sysctl_args (void ) +{ +char *command_line; +struct vfsmount *proc_mnt =NULL ; + + command_line =kstrdup (saved_command_line,GFP_KERNEL );can you use
kstrndup()? And then usekfree_const()? Yes, feel free to
Quoted 8 lines
I don't follow, what am I missing? Do you mean this?
size_tlen =strlen(saved_command_line);command_line=kstrndup(saved_command_line, len, GFP_KERNEL);What would be the advantage over plain
kstrdup()?
As forkfree_const(), when wouldcommand_linebe .rodata? I don't see usingkstrndup()resulting in that.
The const nature of using kstrdup() comes with using const for your purpose. ie:
const char *const_command_line = saved_command_line;
The point of a kstrncpy() then is to ensure force a const throughout your use if you know you don't need modifications.
Quoted 17 lines
Quoted 16 lines
move
__kstrncpy()to a generickstrncpy().fs/proc/proc_sysctl.c int __init proc_sys_init(void) · line 1827
Quoted 11 lines
fs/proc/proc_sysctl.c +11
-0+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); +}Then, can we get this tested as part of
lib/test_sysctl.cwith its respectivetools/testing/selftests/sysctl/sysctl.sh?
Quoted 4 lines
Hmm so I add some sysctl to the test "module" (in fact the 'config' file says it should be build with 'y', which would be needed anyway) and expand the test instructions so that the test kernel boot has to include it on the command line, and then I verify it has been set? Or do you see a better way?
We don't necessarily have a way to test the use boot params today. That reveals an are which we should eventually put some focus on in the future. In the meantime we have to deal with what we have.
So let's think about this:
You are adding a new cmdline sysctl boot param, and also a wrapper for those old boot bootparams to also work using both new sysctl path and old path. Testing just these both should suffice.
How about this:
For testing the new feature you are adding, can you extend the default boot params *always* if a new CONFIG_TEST_SYSCTL_CMDLINE is set? Then upon boot we can verify the proc handlers for these new boot params got kicked, and likewise some other proc handlers which also can be used from the cmdline are *not* set. For this later set, we already have a series of test syctls you can use. In fact, you can use the existing syctls for both cases already I believe, its just a matter of adding this new CONFIG_TEST_SYSCTL_CMDLINE which would extend the cmdline, and these tests would take place *first* on the script.
That would test both cases with one kernel.
You could then also add a bogus new sysctl which also expands to a silly raw boot param to test the wrapper you are providing. That would be the only new test syctl you would need to add.
If you can think of a way to *break* your new functionality and ensure it doesn't break the kernel, even better.
Luis
Re: [PATCH 1/3] kernel/sysctl: support setting sysctl parameters from kernel command line
Quoted 34 lines · Luis Chamberlain
Quoted 24 lines · Vlastimil Babka
Quoted 14 lines · Luis Chamberlain
Quoted 14 lines
fs/proc/proc_sysctl.c +14
-0+ }else if (wret != len) { +pr_err ("Wrote only %ld bytes of %d writing to proc file %s to set sysctl parameter '%s=%s'" , + 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'" , +ERR_PTR (err), param, val); +out: +kfree (path); +return 0 ; +} +Quoted 8 lines
fs/proc/proc_sysctl.c int __init proc_sys_init(void) · line 1816
Quoted 6 lines
fs/proc/proc_sysctl.c +6
-0+void do_sysctl_args (void ) +{ +char *command_line; +struct vfsmount *proc_mnt =NULL ; + + command_line =kstrdup (saved_command_line,GFP_KERNEL );can you use
kstrndup()? And then usekfree_const()? Yes, feel free toQuoted 8 lines
I don't follow, what am I missing? Do you mean this?
size_tlen =strlen(saved_command_line);command_line=kstrndup(saved_command_line, len, GFP_KERNEL);What would be the advantage over plain
kstrdup()?
As forkfree_const(), when wouldcommand_linebe .rodata? I don't see usingkstrndup()resulting in that.
Quoted 7 lines
The const nature of using
kstrdup()comes with using const for your purpose. ie:const char
*const_command_line=saved_command_line;The point of a
kstrncpy()then is to ensure force a const throughout your use if you know you don't need modifications.
I'm not following this suggestion. It _is_ modifying it. That's why it's making a copy. What am I missing?
Quoted 29 lines
Quoted 12 lines
Quoted 11 lines
fs/proc/proc_sysctl.c int __init proc_sys_init(void) · line 1827
Quoted 8 lines
fs/proc/proc_sysctl.c +8
-0+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); +}Then, can we get this tested as part of
lib/test_sysctl.cwith its respectivetools/testing/selftests/sysctl/sysctl.sh?Quoted 4 lines
Hmm so I add some sysctl to the test "module" (in fact the 'config' file says it should be build with 'y', which would be needed anyway) and expand the test instructions so that the test kernel boot has to include it on the command line, and then I verify it has been set? Or do you see a better way?
We don't necessarily have a way to test the use boot params today. That reveals an are which we should eventually put some focus on in the future. In the meantime we have to deal with what we have.
So let's think about this:
You are adding a new cmdline sysctl boot param, and also a wrapper for those old boot bootparams to also work using both new sysctl path and old path. Testing just these both should suffice.
How about this:
Quoted 9 lines
For testing the new feature you are adding, can you extend the default boot params *always* if a new
CONFIG_TEST_SYSCTL_CMDLINEis set? Then upon boot we can verify the proc handlers for these new boot params got kicked, and likewise some other proc handlers which also can be used from the cmdline are *not* set. For this later set, we already have a series of test syctls you can use. In fact, you can use the existing syctls for both cases already I believe, its just a matter of adding this newCONFIG_TEST_SYSCTL_CMDLINEwhich would extend the cmdline, and these tests would take place *first* on the script.
This seems... messy. I'm all for testing this, but I'd rather this not be internally driven. This is an external interface (boot params), so I'd rather an external driver handle that testing. We don't have a common method to do that with the kernel, though.
Quoted 5 lines
That would test both cases with one kernel.
You could then also add a bogus new sysctl which also expands to a silly raw boot param to test the wrapper you are providing. That would be the only new test syctl you would need to add.
Sure, that seems reasonable. Supporting externally driven testing makes sense for this.
Signature · 1 line
Kees Cook
Re: [PATCH 1/3] kernel/sysctl: support setting sysctl parameters from kernel command line
Quoted 38 lines · Kees Cook
Quoted 37 lines · Luis Chamberlain
Quoted 24 lines · Vlastimil Babka
Quoted 14 lines · Luis Chamberlain
Quoted 14 lines
fs/proc/proc_sysctl.c +14
-0+ }else if (wret != len) { +pr_err ("Wrote only %ld bytes of %d writing to proc file %s to set sysctl parameter '%s=%s'" , + 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'" , +ERR_PTR (err), param, val); +out: +kfree (path); +return 0 ; +} +Quoted 8 lines
fs/proc/proc_sysctl.c int __init proc_sys_init(void) · line 1816
Quoted 6 lines
fs/proc/proc_sysctl.c +6
-0+void do_sysctl_args (void ) +{ +char *command_line; +struct vfsmount *proc_mnt =NULL ; + + command_line =kstrdup (saved_command_line,GFP_KERNEL );can you use
kstrndup()? And then usekfree_const()? Yes, feel free toQuoted 8 lines
I don't follow, what am I missing? Do you mean this?
size_tlen =strlen(saved_command_line);command_line=kstrndup(saved_command_line, len, GFP_KERNEL);What would be the advantage over plain
kstrdup()?
As forkfree_const(), when wouldcommand_linebe .rodata? I don't see usingkstrndup()resulting in that.The const nature of using
kstrdup()comes with using const for your purpose. ie:
Quoted 7 lines
Quoted 4 lines
const char
*const_command_line=saved_command_line;The point of a
kstrncpy()then is to ensure force a const throughout your use if you know you don't need modifications.I'm not following this suggestion. It _is_ modifying it. That's why it's making a copy. What am I missing?
We modify the copied bootparams to allow new sysctls to map to old boot params?
If so, then yes, this cannot be used.
Quoted 39 lines
Quoted 29 lines
Quoted 12 lines
Quoted 11 lines
fs/proc/proc_sysctl.c int __init proc_sys_init(void) · line 1827
Quoted 8 lines
fs/proc/proc_sysctl.c +8
-0+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); +}Then, can we get this tested as part of
lib/test_sysctl.cwith its respectivetools/testing/selftests/sysctl/sysctl.sh?Quoted 4 lines
Hmm so I add some sysctl to the test "module" (in fact the 'config' file says it should be build with 'y', which would be needed anyway) and expand the test instructions so that the test kernel boot has to include it on the command line, and then I verify it has been set? Or do you see a better way?
We don't necessarily have a way to test the use boot params today. That reveals an are which we should eventually put some focus on in the future. In the meantime we have to deal with what we have.
So let's think about this:
You are adding a new cmdline sysctl boot param, and also a wrapper for those old boot bootparams to also work using both new sysctl path and old path. Testing just these both should suffice.
How about this:
Quoted 9 lines
For testing the new feature you are adding, can you extend the default boot params *always* if a new
CONFIG_TEST_SYSCTL_CMDLINEis set? Then upon boot we can verify the proc handlers for these new boot params got kicked, and likewise some other proc handlers which also can be used from the cmdline are *not* set. For this later set, we already have a series of test syctls you can use. In fact, you can use the existing syctls for both cases already I believe, its just a matter of adding this newCONFIG_TEST_SYSCTL_CMDLINEwhich would extend the cmdline, and these tests would take place *first* on the script.
Quoted 1 line
This seems... messy.
It is all we have.
Quoted 1 line
I'm all for testing this,
OK so we do want to test it.
Quoted 1 line
but I'd rather this not be internally driven.
This is the least cumbersome solution I could think of. Other things would require things like using qemu, etc. That seems much more messsy.
Quoted 3 lines
This is an external interface (boot params), so
I'd rather an external driver handle that testing. We don't have a common method to do that with the kernel, though.
Right... which begs the question now -- how do we test this sort of stuff? The above would at least get us coverage while we iron something more generic out for boot params.
Quoted 8 lines
Quoted 5 lines
That would test both cases with one kernel.
You could then also add a bogus new sysctl which also expands to a silly raw boot param to test the wrapper you are providing. That would be the only new test syctl you would need to add.
Sure, that seems reasonable. Supporting externally driven testing makes sense for this.
But again, what exactly?
Luis
Re: [PATCH 1/3] kernel/sysctl: support setting sysctl parameters from kernel command line
Quoted 44 lines · Luis Chamberlain
Quoted 43 lines · Kees Cook
Quoted 41 lines · Luis Chamberlain
Quoted 24 lines · Vlastimil Babka
Quoted 14 lines · Luis Chamberlain
Quoted 14 lines
fs/proc/proc_sysctl.c +14
-0+ }else if (wret != len) { +pr_err ("Wrote only %ld bytes of %d writing to proc file %s to set sysctl parameter '%s=%s'" , + 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'" , +ERR_PTR (err), param, val); +out: +kfree (path); +return 0 ; +} +Quoted 8 lines
fs/proc/proc_sysctl.c int __init proc_sys_init(void) · line 1816
Quoted 6 lines
fs/proc/proc_sysctl.c +6
-0+void do_sysctl_args (void ) +{ +char *command_line; +struct vfsmount *proc_mnt =NULL ; + + command_line =kstrdup (saved_command_line,GFP_KERNEL );can you use
kstrndup()? And then usekfree_const()? Yes, feel free toQuoted 8 lines
I don't follow, what am I missing? Do you mean this?
size_tlen =strlen(saved_command_line);command_line=kstrndup(saved_command_line, len, GFP_KERNEL);What would be the advantage over plain
kstrdup()?
As forkfree_const(), when wouldcommand_linebe .rodata? I don't see usingkstrndup()resulting in that.The const nature of using
kstrdup()comes with using const for your purpose. ie:const char
*const_command_line=saved_command_line;The point of a
kstrncpy()then is to ensure force a const throughout your use if you know you don't need modifications.
Quoted 6 lines
Quoted 2 lines
I'm not following this suggestion. It _is_ modifying it. That's why it's making a copy. What am I missing?
We modify the copied bootparams to allow new sysctls to map to old boot params?
If so, then yes, this cannot be used.
I feel like I've lost track of this thread. This strdup is so that the command line can have '\0's injected while it steps through the args (and for doing the . and / replacement). I don't know what you mean by "map" here: this is standard parse_args() usage.
Quoted 42 lines
Quoted 39 lines
Quoted 29 lines
Quoted 12 lines
Quoted 11 lines
fs/proc/proc_sysctl.c int __init proc_sys_init(void) · line 1827
Quoted 8 lines
fs/proc/proc_sysctl.c +8
-0+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); +}Then, can we get this tested as part of
lib/test_sysctl.cwith its respectivetools/testing/selftests/sysctl/sysctl.sh?Quoted 4 lines
Hmm so I add some sysctl to the test "module" (in fact the 'config' file says it should be build with 'y', which would be needed anyway) and expand the test instructions so that the test kernel boot has to include it on the command line, and then I verify it has been set? Or do you see a better way?
We don't necessarily have a way to test the use boot params today. That reveals an are which we should eventually put some focus on in the future. In the meantime we have to deal with what we have.
So let's think about this:
You are adding a new cmdline sysctl boot param, and also a wrapper for those old boot bootparams to also work using both new sysctl path and old path. Testing just these both should suffice.
How about this:
Quoted 9 lines
For testing the new feature you are adding, can you extend the default boot params *always* if a new
CONFIG_TEST_SYSCTL_CMDLINEis set? Then upon boot we can verify the proc handlers for these new boot params got kicked, and likewise some other proc handlers which also can be used from the cmdline are *not* set. For this later set, we already have a series of test syctls you can use. In fact, you can use the existing syctls for both cases already I believe, its just a matter of adding this newCONFIG_TEST_SYSCTL_CMDLINEwhich would extend the cmdline, and these tests would take place *first* on the script.Quoted 1 line
This seems... messy.
It is all we have.
Quoted 8 lines
Quoted 1 line
I'm all for testing this,
OK so we do want to test it.
Quoted 1 line
but I'd rather this not be internally driven.
This is the least cumbersome solution I could think of. Other things would require things like using qemu, etc. That seems much more messsy.
Yes. Doing an internal extension isn't testing the actual code.
Quoted 11 lines
Quoted 4 lines
This is an external interface (boot params), so
I'd rather an external driver handle that testing. We don't have a common method to do that with the kernel, though.Right... which begs the question now -- how do we test this sort of stuff? The above would at least get us coverage while we iron something more generic out for boot params.
Quoted 2 lines
Quoted 2 lines
That would test both cases with one kernel.
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
You could then also add a bogus new sysctl which also expands to a silly raw boot param to test the wrapper you are providing. That would be the only new test syctl you would need to add.
Sure, that seems reasonable. Supporting externally driven testing makes sense for this.
But again, what exactly?
I don't think anything is needed for this series. It can be boot tested manually.
Signature · 1 line
Kees Cook
Re: [PATCH 1/3] kernel/sysctl: support setting sysctl parameters from kernel command line
Quoted 6 lines · Kees Cook
Quoted 1 line · Luis Chamberlain
We modify the copied bootparams to allow new sysctls to map to old boot params?
This strdup is so that the
command line can have '\0's injected while it steps through the args (and for doing the . and / replacement).
Please ignore the const feedback then.
Quoted 4 lines
Quoted 2 lines
This is the least cumbersome solution I could think of. Other things would require things like using qemu, etc. That seems much more messsy.
Yes. Doing an internal extension isn't testing the actual code.
But it would.
Quoted 14 lines
Quoted 14 lines
Quoted 3 lines
This is an external interface (boot params), so
I'd rather an external driver handle that testing. We don't have a common method to do that with the kernel, though.Right... which begs the question now -- how do we test this sort of stuff? The above would at least get us coverage while we iron something more generic out for boot params.
Quoted 6 lines
Quoted 5 lines
That would test both cases with one kernel.
You could then also add a bogus new sysctl which also expands to a silly raw boot param to test the wrapper you are providing. That would be the only new test syctl you would need to add.
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
Sure, that seems reasonable. Supporting externally driven testing makes sense for this.
But again, what exactly?
I don't think anything is needed for this series. It can be boot tested manually.
Why test it manually when it could be tested automatically with a new kconfig?
Luis
Re: [PATCH 1/3] kernel/sysctl: support setting sysctl parameters from kernel command line
Quoted 9 lines · Luis Chamberlain
Quoted 1 line
Yes. Doing an internal extension isn't testing the actual code.
But it would.
[...]
Quoted 2 lines
I don't think anything is needed for this series. It can be boot tested manually.
Why test it manually when it could be tested automatically with a new kconfig?
So, my impression is that adding code to the internals to test the internals isn't a valid test (or at least makes it fragile) because the test would depend on the changes to the internals (or at least depend on non-default non-production CONFIGs).
Can you send a patch for what you think this should look like? Perhaps I'm not correctly imagining what you're describing?
Regardless of testing, I think this series is ready for -mm.
Signature · 1 line
Kees Cook
Re: [PATCH 1/3] kernel/sysctl: support setting sysctl parameters from kernel command line
Quoted 9 lines · Kees Cook
Quoted 8 lines · Luis Chamberlain
Quoted 1 line
Yes. Doing an internal extension isn't testing the actual code.
But it would.
[...]
Quoted 2 lines
I don't think anything is needed for this series. It can be boot tested manually.
Quoted 6 lines
Quoted 1 line
Why test it manually when it could be tested automatically with a new kconfig?
So, my impression is that adding code to the internals to test the internals isn't a valid test (or at least makes it fragile) because the test would depend on the changes to the internals (or at least depend on non-default non-production CONFIGs).
The *internal* aspect here is an extension to boot params under a kconfig which would simply append to it, as if the user would have added some more params. Since we already have test sysctl params the only one we'd need to add on the test driver would be a dummy one which tests the alias, on the second patch. We should have enough sysctls to already test dummy values.
Nothing else would be needed as the sysctl test driver would just need to test that the values expected when this is enabled is set.
Quoted 2 lines
Can you send a patch for what you think this should look like? Perhaps I'm not correctly imagining what you're describing?
I rather get the person involved in the changes to do the testing so as they're the ones designing the feature. If however it is not clear what I mean I'm happy to elaborate.
Vlastimil do you get what I mean?
Quoted 1 line
Regardless of testing, I think this series is ready for -mm.
I'm happy for it to go in provided we at least devise a follow up plan for testing. Otherwise -- like other things, it won't get done.
Luis
Re: [PATCH 1/3] kernel/sysctl: support setting sysctl parameters from kernel command line
Quoted 12 lines · Luis Chamberlain
Quoted 11 lines · Kees Cook
Quoted 9 lines · Luis Chamberlain
Quoted 1 line
Yes. Doing an internal extension isn't testing the actual code.
But it would.
[...]
Quoted 2 lines
I don't think anything is needed for this series. It can be boot tested manually.
Why test it manually when it could be tested automatically with a new kconfig?
Quoted 7 lines
Quoted 4 lines
So, my impression is that adding code to the internals to test the internals isn't a valid test (or at least makes it fragile) because the test would depend on the changes to the internals (or at least depend on non-default non-production CONFIGs).
The *internal* aspect here is an extension to boot params under a kconfig which would simply append to it, as if the user would have
So there's no such kconfig yet to apply boot parameters specified by configure, right? That would itself be a new feature. Or could we use bootconfig? (CC Masami)
Quoted 4 lines
added some more params. Since we already have test sysctl params the only one we'd need to add on the test driver would be a dummy one which tests the alias, on the second patch. We should have enough sysctls to already test dummy values.
Right.
Quoted 11 lines
Nothing else would be needed as the sysctl test driver would just need to test that the values expected when this is enabled is set.
Quoted 2 lines
Can you send a patch for what you think this should look like? Perhaps I'm not correctly imagining what you're describing?
I rather get the person involved in the changes to do the testing so as they're the ones designing the feature. If however it is not clear what I mean I'm happy to elaborate.
Vlastimil do you get what I mean?
Hopefully :)
Quoted 4 lines
Quoted 1 line
Regardless of testing, I think this series is ready for -mm.
I'm happy for it to go in provided we at least devise a follow up plan for testing. Otherwise -- like other things, it won't get done.
OK I'll send a v2 and we can discuss the test driver details. I don't want to neglect testing, but also not block the new functionality, in case it means testing means adding another new functionality.
Thanks,
Vlastimil
Quoted 2 lines
Luis
Masami Hiramatsu
reviewerRe: [PATCH 1/3] kernel/sysctl: support setting sysctl parameters from kernel command line
Quoted 18 lines · On Tue, 14 Apr 2020 13:25:07 +0200 Vlastimil Babka <[email protected]> wrote:
Quoted 17 lines · Luis Chamberlain
Quoted 15 lines · Kees Cook
Quoted 9 lines · Luis Chamberlain
Quoted 1 line
Yes. Doing an internal extension isn't testing the actual code.
But it would.
[...]
Quoted 2 lines
I don't think anything is needed for this series. It can be boot tested manually.
Why test it manually when it could be tested automatically with a new kconfig?
So, my impression is that adding code to the internals to test the internals isn't a valid test (or at least makes it fragile) because the test would depend on the changes to the internals (or at least depend on non-default non-production CONFIGs).
Quoted 5 lines
Quoted 2 lines
The *internal* aspect here is an extension to boot params under a kconfig which would simply append to it, as if the user would have
So there's no such kconfig yet to apply boot parameters specified by configure, right? That would itself be a new feature. Or could we use bootconfig? (CC Masami)
Yes, I think you can use bootconfig to add this feature more flexibly. I think your patch is easily modified to use bootconfig. :)
Thank you,
Signature · 1 line
Masami Hiramatsu <[email protected]>
Re: [PATCH 1/3] kernel/sysctl: support setting sysctl parameters from kernel command line
In reply to Vlastimil BabkaQuoted 18 lines · Vlastimil Babka
Quoted 17 lines · Luis Chamberlain
Quoted 15 lines · Kees Cook
Quoted 9 lines · Luis Chamberlain
Quoted 1 line
Yes. Doing an internal extension isn't testing the actual code.
But it would.
[...]
Quoted 2 lines
I don't think anything is needed for this series. It can be boot tested manually.
Why test it manually when it could be tested automatically with a new kconfig?
So, my impression is that adding code to the internals to test the internals isn't a valid test (or at least makes it fragile) because the test would depend on the changes to the internals (or at least depend on non-default non-production CONFIGs).
Quoted 5 lines
Quoted 2 lines
The *internal* aspect here is an extension to boot params under a kconfig which would simply append to it, as if the user would have
So there's no such kconfig yet to apply boot parameters specified by configure, right? That would itself be a new feature.
I cannot say, there is no easy grammatical expression for this. For kernel testing, no, I am not aware of one.
Quoted 1 line
Or could we use bootconfig? (CC Masami)
Neat, yeah, that seems like a set of helpers of which could help for sure.
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
Regardless of testing, I think this series is ready for -mm.
I'm happy for it to go in provided we at least devise a follow up plan for testing. Otherwise -- like other things, it won't get done.
OK I'll send a v2 and we can discuss the test driver details. I don't want to neglect testing, but also not block the new functionality,
So long as the testing part gets done, I'm happy :)
Quoted 2 lines
in case it means
testing means adding another new functionality.
Yeah, I can see how some new code may need to be added for that, its a good point.
But think about this for a second, if we *didn't* have such code added, how could it have easily been tested before? The question is rhetorical, as I'm alluding to that a lot of old code wasn't designed for easy unit testing either, and I'd invite one to consider the value of the change in philosophy on first code design when one *does* take that into consideration. There are certain best practices that I'd wager are probably good for us to evaluate for the long term of kernel evolution, and I think that always advocating designing testing around new functionality would be one.
Once and if you do add your testing, and if such testing is expanded to test parsing the cmdline further, and to purposely break it, who knows what other bugs bugs we'll find.
But maybe Masami already uncovered all the fun bugs.
Luis