diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index c07815d230bc..5076e288f93f 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -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. Currently a subset of sysctl
+ parameters is supported that is not registered
+ dynamically. Unrecognized parameters and invalid values
+ are reported in the kernel log.
+ 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/include/linux/sysctl.h b/include/linux/sysctl.h
index 02fa84493f23..62ae963a5c0c 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);
+int process_sysctl_arg(char *param, char *val, const char *unused, void *arg);
extern struct ctl_table sysctl_mount_point[];
diff --git a/init/main.c b/init/main.c
index ee4947af823f..74a094c6b8b9 100644
--- a/init/main.c
+++ b/init/main.c
@@ -1345,6 +1345,25 @@ void __weak free_initmem(void)
free_initmem_default(POISON_FREE_INITMEM);
}
+static void do_sysctl_args(void)
+{
+#ifdef CONFIG_SYSCTL
+ size_t len = strlen(saved_command_line) + 1;
+ char *command_line;
+
+ command_line = kzalloc(len, GFP_KERNEL);
+ if (!command_line)
+ panic("%s: Failed to allocate %zu bytes\n", __func__, len);
+
+ strcpy(command_line, saved_command_line);
+
+ parse_args("Setting sysctl args", command_line,
+ NULL, 0, -1, -1, NULL, process_sysctl_arg);
+
+ kfree(command_line);
+#endif
+}
+
static int __ref kernel_init(void *unused)
{
int ret;@@ -1367,6 +1386,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)diff --git a/kernel/sysctl.c b/kernel/sysctl.c
index ad5b88a53c5a..18c7f5606d55 100644
--- a/kernel/sysctl.c
+++ b/kernel/sysctl.c
@@ -1980,6 +1980,68 @@ int __init sysctl_init(void)
return 0;
}
+/* Set sysctl value passed on kernel command line. */
+int process_sysctl_arg(char *param, char *val,
+ const char *unused, void *arg)
+{
+ size_t count;
+ char *remaining;
+ int err;
+ loff_t ppos = 0;
+ struct ctl_table *ctl, *found = NULL;
+
+ if (strncmp(param, "sysctl.", sizeof("sysctl.") - 1))
+ return 0;
+
+ param += sizeof("sysctl.") - 1;
+
+ remaining = param;
+ ctl = &sysctl_base_table[0];
+
+ while(ctl->procname != 0) {
+ int len = strlen(ctl->procname);
+ if (strncmp(remaining, ctl->procname, len)) {
+ ctl++;
+ continue;
+ }
+ if (ctl->child) {
+ if (remaining[len] == '.') {
+ remaining += len + 1;
+ ctl = ctl->child;
+ continue;
+ }
+ } else {
+ if (remaining[len] == '\0') {
+ found = ctl;
+ break;
+ }
+ }
+ ctl++;
+ }
+
+ if (!found) {
+ pr_warn("Unknown sysctl param '%s' on command line", param);
+ return 0;
+ }
+
+ if (!(found->mode & 0200)) {
+ pr_warn("Cannot set sysctl '%s=%s' from command line - not writable",
+ param, val);
+ return 0;
+ }
+
+ count = strlen(val);
+ err = found->proc_handler(found, 1, val, &count, &ppos);
+
+ if (err)
+ pr_warn("Error %d setting sysctl '%s=%s' from command line",
+ err, param, val);
+
+ pr_debug("Set sysctl '%s=%s' from command line", param, val);
+
+ return 0;
+}
+
#endif /* CONFIG_SYSCTL */
/*--
2.25.1