From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.8 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 35424C54FD3 for ; Wed, 25 Mar 2020 12:03:59 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id F075C20771 for ; Wed, 25 Mar 2020 12:03:58 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org F075C20771 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=suse.cz Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 8D69D6B000D; Wed, 25 Mar 2020 08:03:57 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 885CE6B000E; Wed, 25 Mar 2020 08:03:57 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7744C6B0010; Wed, 25 Mar 2020 08:03:57 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0237.hostedemail.com [216.40.44.237]) by kanga.kvack.org (Postfix) with ESMTP id 60A036B000E for ; Wed, 25 Mar 2020 08:03:57 -0400 (EDT) Received: from smtpin07.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 36F4D4FE6 for ; Wed, 25 Mar 2020 12:03:57 +0000 (UTC) X-FDA: 76633750914.07.burn84_79bcdc75bfe4e X-HE-Tag: burn84_79bcdc75bfe4e X-Filterd-Recvd-Size: 7883 Received: from mx2.suse.de (mx2.suse.de [195.135.220.15]) by imf03.hostedemail.com (Postfix) with ESMTP for ; Wed, 25 Mar 2020 12:03:56 +0000 (UTC) X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 55705AC1D; Wed, 25 Mar 2020 12:03:54 +0000 (UTC) From: Vlastimil Babka To: Luis Chamberlain , Kees Cook , Iurii Zaikin Cc: linux-kernel@vger.kernel.org, linux-api@vger.kernel.org, linux-mm@kvack.org, Ivan Teterevkov , Michal Hocko , David Rientjes , Matthew Wilcox , "Eric W . Biederman" , "Guilherme G . Piccoli" , Vlastimil Babka Subject: [RFC v2 1/2] kernel/sysctl: support setting sysctl parameters from kernel command line Date: Wed, 25 Mar 2020 13:03:44 +0100 Message-Id: <20200325120345.12946-1-vbabka@suse.cz> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 genera= l support for passing sysctl parameters via command line. Googling found on= ly somebody else wondering the same [2], but I haven't found any prior discu= ssion with reasons why not to do this. Settings the vm_swappiness issue aside (the underlying issue might be sol= ved in a different way), quick search of kernel-parameters.txt shows there are a= lready some that exist as both sysctl and kernel parameter - hung_task_panic, nmi_watchdog, numa_zonelist_order, traceoff_on_warning. A general mechani= sm 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. Also after 61a47c1ad3a4 ("sysctl: Remove the sysctl system call") the onl= y way to set sysctl is via procfs, so this would eventually allow small systems= to be built without CONFIG_PROC_SYSCTL and still be able to change sysctl param= eters. Hence, this patch adds a new parse_args() pass that looks for parameters prefixed by 'sysctl.' and searches for them in the sysctl ctl_tables. Whe= n found, the respective proc handler is invoked. The search is just a naive linear one, to avoid using the whole procfs layer. It should be acceptabl= e, as the cost depends on number of sysctl. parameters passed. The main limitation of avoiding the procfs layer is however that sysctls dynamically registered by register_sysctl_table() or register_sysctl_path= s() cannot currently be set by this method. The processing is hooked right before the init process is loaded, as some handlers might be more complicated than simple setters and might need som= e subsystems to be initialized. At the moment the init process can be start= ed and eventually execute a process writing to /proc/sys/ then it should be also= fine to do that from the kernel. [1] https://lore.kernel.org/linux-doc/BL0PR02MB560167492CA4094C91589930E9= FC0@BL0PR02MB5601.namprd02.prod.outlook.com/ [2] https://unix.stackexchange.com/questions/558802/how-to-set-sysctl-usi= ng-kernel-command-line-parameter Signed-off-by: Vlastimil Babka --- v2: - handle any nesting level of parameter name - add Documentation/admin-guide/kernel-parameters.txt blurb - alias support for legacy one-off parameters, with first conversion (pa= tch 2) - still no support for dynamically registed sysctls .../admin-guide/kernel-parameters.txt | 9 +++ include/linux/sysctl.h | 1 + init/main.c | 21 +++++++ kernel/sysctl.c | 62 +++++++++++++++++++ 4 files changed, 93 insertions(+) diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentat= ion/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 @@ =20 switches=3D [HW,M68k] =20 + sysctl.*=3D [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=3D40 + sysfs.deprecated=3D0|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); =20 extern int sysctl_init(void); +int process_sysctl_arg(char *param, char *val, const char *unused, void = *arg); =20 extern struct ctl_table sysctl_mount_point[]; =20 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); } =20 +static void do_sysctl_args(void) +{ +#ifdef CONFIG_SYSCTL + size_t len =3D strlen(saved_command_line) + 1; + char *command_line; + + command_line =3D 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) =20 rcu_end_inkernel_boot(); =20 + do_sysctl_args(); + if (ramdisk_execute_command) { ret =3D 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; } =20 +/* 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 =3D 0; + struct ctl_table *ctl, *found =3D NULL; + + if (strncmp(param, "sysctl.", sizeof("sysctl.") - 1)) + return 0; + + param +=3D sizeof("sysctl.") - 1; + + remaining =3D param; + ctl =3D &sysctl_base_table[0]; + + while(ctl->procname !=3D 0) { + int len =3D strlen(ctl->procname); + if (strncmp(remaining, ctl->procname, len)) { + ctl++; + continue; + } + if (ctl->child) { + if (remaining[len] =3D=3D '.') { + remaining +=3D len + 1; + ctl =3D ctl->child; + continue; + } + } else { + if (remaining[len] =3D=3D '\0') { + found =3D 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=3D%s' from command line - not writable"= , + param, val); + return 0; + } + + count =3D strlen(val); + err =3D found->proc_handler(found, 1, val, &count, &ppos); + + if (err) + pr_warn("Error %d setting sysctl '%s=3D%s' from command line", + err, param, val); + + pr_debug("Set sysctl '%s=3D%s' from command line", param, val); + + return 0; +} + #endif /* CONFIG_SYSCTL */ =20 /* --=20 2.25.1