path: root/fs
diff options
authorXiaoming Ni <>2022-01-21 22:10:50 -0800
committerLinus Torvalds <>2022-01-22 08:33:34 +0200
commit3ddd9a808cee7284931312f2f3e854c9617f44b2 (patch)
treed5ac0a6f2f68168512bf9dc92982cc11365925d4 /fs
parentffa65753c43142f3b803486442813744da71cff2 (diff)
sysctl: add a new register_sysctl_init() interface
Patch series "sysctl: first set of kernel/sysctl cleanups", v2. Finally had time to respin the series of the work we had started last year on cleaning up the kernel/sysct.c kitchen sink. People keeps stuffing their sysctls in that file and this creates a maintenance burden. So this effort is aimed at placing sysctls where they actually belong. I'm going to split patches up into series as there is quite a bit of work. This first set adds register_sysctl_init() for uses of registerting a sysctl on the init path, adds const where missing to a few places, generalizes common values so to be more easy to share, and starts the move of a few kernel/sysctl.c out where they belong. The majority of rework on v2 in this first patch set is 0-day fixes. Eric Biederman's feedback is later addressed in subsequent patch sets. I'll only post the first two patch sets for now. We can address the rest once the first two patch sets get completely reviewed / Acked. This patch (of 9): The kernel/sysctl.c is a kitchen sink where everyone leaves their dirty dishes, this makes it very difficult to maintain. To help with this maintenance let's start by moving sysctls to places where they actually belong. The proc sysctl maintainers do not want to know what sysctl knobs you wish to add for your own piece of code, we just care about the core logic. Today though folks heavily rely on tables on kernel/sysctl.c so they can easily just extend this table with their needed sysctls. In order to help users move their sysctls out we need to provide a helper which can be used during code initialization. We special-case the initialization use of register_sysctl() since it *is* safe to fail, given all that sysctls do is provide a dynamic interface to query or modify at runtime an existing variable. So the use case of register_sysctl() on init should *not* stop if the sysctls don't end up getting registered. It would be counter productive to stop boot if a simple sysctl registration failed. Provide a helper for init then, and document the recommended init levels to use for callers of this routine. We will later use this in subsequent patches to start slimming down kernel/sysctl.c tables and moving sysctl registration to the code which actually needs these sysctls. [ major commit log and documentation rephrasing also moved to fs/proc/proc_sysctl.c ] Link: Link: Signed-off-by: Xiaoming Ni <> Signed-off-by: Luis Chamberlain <> Reviewed-by: Kees Cook <> Cc: Iurii Zaikin <> Cc: "Eric W. Biederman" <> Cc: Peter Zijlstra <> Cc: Greg Kroah-Hartman <> Cc: Paul Turner <> Cc: Andy Shevchenko <> Cc: Sebastian Reichel <> Cc: Tetsuo Handa <> Cc: Petr Mladek <> Cc: Sergey Senozhatsky <> Cc: Qing Wang <> Cc: Benjamin LaHaise <> Cc: Al Viro <> Cc: Jan Kara <> Cc: Amir Goldstein <> Cc: Stephen Kitt <> Cc: Antti Palosaari <> Cc: Arnd Bergmann <> Cc: Benjamin Herrenschmidt <> Cc: Clemens Ladisch <> Cc: David Airlie <> Cc: Jani Nikula <> Cc: Joel Becker <> Cc: Joonas Lahtinen <> Cc: Joseph Qi <> Cc: Julia Lawall <> Cc: Lukas Middendorf <> Cc: Mark Fasheh <> Cc: Phillip Potter <> Cc: Rodrigo Vivi <> Cc: Douglas Gilbert <> Cc: James E.J. Bottomley <> Cc: Jani Nikula <> Cc: John Ogness <> Cc: Martin K. Petersen <> Cc: "Rafael J. Wysocki" <> Cc: Steven Rostedt (VMware) <> Cc: Suren Baghdasaryan <> Cc: "Theodore Ts'o" <> Signed-off-by: Andrew Morton <> Signed-off-by: Linus Torvalds <>
Diffstat (limited to 'fs')
1 files changed, 33 insertions, 0 deletions
diff --git a/fs/proc/proc_sysctl.c b/fs/proc/proc_sysctl.c
index 389e1e42e7d9..55a54973f061 100644
--- a/fs/proc/proc_sysctl.c
+++ b/fs/proc/proc_sysctl.c
@@ -16,6 +16,7 @@
#include <linux/module.h>
#include <linux/bpf-cgroup.h>
#include <linux/mount.h>
+#include <linux/kmemleak.h>
#include "internal.h"
static const struct dentry_operations proc_sys_dentry_operations;
@@ -1383,6 +1384,38 @@ struct ctl_table_header *register_sysctl(const char *path, struct ctl_table *tab
+ * __register_sysctl_init() - register sysctl table to path
+ * @path: path name for sysctl base
+ * @table: This is the sysctl table that needs to be registered to the path
+ * @table_name: The name of sysctl table, only used for log printing when
+ * registration fails
+ *
+ * The sysctl interface is used by userspace to query or modify at runtime
+ * a predefined value set on a variable. These variables however have default
+ * values pre-set. Code which depends on these variables will always work even
+ * if register_sysctl() fails. If register_sysctl() fails you'd just loose the
+ * ability to query or modify the sysctls dynamically at run time. Chances of
+ * register_sysctl() failing on init are extremely low, and so for both reasons
+ * this function does not return any error as it is used by initialization code.
+ *
+ * Context: Can only be called after your respective sysctl base path has been
+ * registered. So for instance, most base directories are registered early on
+ * init before init levels are processed through proc_sys_init() and
+ * sysctl_init().
+ */
+void __init __register_sysctl_init(const char *path, struct ctl_table *table,
+ const char *table_name)
+ struct ctl_table_header *hdr = register_sysctl(path, table);
+ if (unlikely(!hdr)) {
+ pr_err("failed when register_sysctl %s to %s\n", table_name, path);
+ return;
+ }
+ kmemleak_not_leak(hdr);
static char *append_path(const char *path, char *pos, const char *name)
int namelen;