sysctl: 5th set of kernel/sysctl cleanups
Subsystems (8)
cover sysctl: 5th set of kernel/sysctl cleanups
This is my 5th set of sysctl cleanups for kernel/sysctl. In this patch series we start addressing base directories, and so we start with the "fs" sysctls. The end goal is we end up completely moving all "fs" sysctl knobs out from kernel/sysctl.
My queue of patches is done with this patch series, and so help from others on trimming down kernel/sysctl.c further would be greatly appreciated now that we have a path to move the rest of the stuff out.
Luis Chamberlain (3):
sysctl: add and use base directory declarer and registration helper
fs: move namespace sysctls and declare fs base directory kernel/sysctl.c: rename sysctl_init() to sysctl_init_bases()
Xiaoming Ni (3):
printk: fix build warning when CONFIG_PRINTK=n fs/coredump: move coredump sysctls into its own file
kprobe: move sysctl_kprobes_optimization to kprobes.c
arch/arm/mm/alignment.c | 2 +- arch/sh/mm/alignment.c | 2 +- fs/Makefile | 3 +- fs/coredump.c | 66 ++++++++++++++++++++++++++++++++++++--- fs/exec.c | 55 --------------------------------- fs/namespace.c | 24 +++++++++++++- fs/proc/proc_sysctl.c | 13 ++++++-- fs/sysctls.c | 9 +++--- include/linux/coredump.h | 10 +++--- include/linux/kprobes.h | 6 ---- include/linux/mount.h | 3 -- include/linux/printk.h | 4 --- include/linux/sysctl.h | 25 ++++++++++++++- kernel/kprobes.c | 30 +++++++++++++++--- kernel/printk/internal.h | 2 ++ kernel/printk/printk.c | 3 +- kernel/sysctl.c | 67 ++++++---------------------------------- 17 files changed, 173 insertions(+), 151 deletions(-)
Signature · 1 line
2.33.0
PATCH 1/6 sysctl: add and use base directory declarer and registration helper
Add a set of helpers which can be used to declare and register base directory sysctls on their own. We do this so we can later move each of the base sysctl directories like "fs", "kernel", etc, to their own respective files instead of shoving the declarations and registrations all on kernel/sysctl.c. The lazy approach has caught up and with this, we just end up extending the list of base directories / sysctls on one file and this makes maintenance difficult due to merge conflicts from many developers.
The declarations is used first by kernel/sysctl.c for registration its own base which over time we'll try to clean up. It will be used in the next patch to demonstrate how to cleanly deal with base sysctl directories.
--- fs/proc/proc_sysctl.c | 9 +++++++++ include/linux/sysctl.h | 23 +++++++++++++++++++++++ kernel/sysctl.c | 41 ++++++++++------------------------------- 3 files changed, 42 insertions(+), 31 deletions(-)
fs/proc/proc_sysctl.c +9 -0
@@ -1646,6 +1646,15 @@ struct ctl_table_header *register_sysctl_table(struct ctl_table *table) }EXPORT_SYMBOL (register_sysctl_table); +int __register_sysctl_base (struct ctl_table *base_table) +{ +struct ctl_table_header *hdr; + + hdr =register_sysctl_table (base_table); +kmemleak_not_leak (hdr); +return 0 ; +} +static void put_links (struct ctl_table_header *header) {struct ctl_table_set *root_set = &sysctl_table_root.default_set;
include/linux/sysctl.h +23 -0
@@ -194,6 +194,19 @@ struct ctl_path {#ifdef CONFIG_SYSCTL +#define DECLARE_SYSCTL_BASE (_name, _table) \ +static struct ctl_table _name##_base_table[] = { \ + { \ + .procname = #_name, \ + .mode = 0555, \ + .child = _table, \ + }, \ +} + +extern int __register_sysctl_base (struct ctl_table *base_table); + +#define register_sysctl_base (_name) __register_sysctl_base(_name##_base_table) +void proc_sys_poll_notify (struct ctl_table_poll *poll); extern void setup_sysctl_set(struct ctl_table_set *p,
@@ -236,6 +249,16 @@ extern int no_unaligned_warning;extern struct ctl_table sysctl_mount_point[];#else /* CONFIG_SYSCTL */ + +#define DECLARE_SYSCTL_BASE (_name, _table) + +static inline int __register_sysctl_base (struct ctl_table *base_table) +{ +return 0 ; +} + +#define register_sysctl_base (table) __register_sysctl_base(table) +static inline struct ctl_table_header *register_sysctl_table (struct ctl_table * table) {return NULL ;
kernel/sysctl.c +10 -31
51 lines not on this page · raw message
Signature · 1 line
2.33.0
PATCH 2/6 fs: move namespace sysctls and declare fs base directory
In reply to Luis ChamberlainThis moves the namespace sysctls to its own file as part of the kernel/sysctl.c spring cleaning
Since we have now removed all sysctls for "fs", we now have to declare it on the filesystem code, we do that using the new helper, which reduces boiler plate code.
We rename init_fs_shared_sysctls() to init_fs_sysctls() to reflect that now fs/sysctls.c is taking on the burden of being the first to register the base directory as well.
Lastly, since init code will load in the order in which we link it we have to move the sysctl code to be linked in early, so that its early init routine runs prior to other fs code. This way, other filesystem code can register their own sysctls using the helpers after this:
* register_sysctl_init()
* register_sysctl()
--- fs/Makefile | 3 ++- fs/namespace.c | 24 +++++++++++++++++++++++- fs/sysctls.c | 9 +++++---- include/linux/mount.h | 3 --- kernel/sysctl.c | 14 -------------- 5 files changed, 30 insertions(+), 23 deletions(-)
fs/Makefile +2 -1
@@ -6,6 +6,8 @@ # Rewritten to use lists instead of if-statements. # +obj-$(CONFIG_SYSCTL) += sysctls.o + obj-y := open.o read_write.o file_table.o super.o \ char_dev.o stat.o exec.o pipe.o namei.o fcntl.o \ ioctl.o readdir.o select.o dcache.o inode.o \
@@ -28,7 +30,6 @@ obj-y += notify/ obj-$(CONFIG_EPOLL) += eventpoll.o obj-y += anon_inodes.o obj-$(CONFIG_SIGNALFD) += signalfd.o -obj-$(CONFIG_SYSCTL) += sysctls.o obj-$(CONFIG_TIMERFD) += timerfd.o obj-$(CONFIG_EVENTFD) += eventfd.o obj-$(CONFIG_USERFAULTFD) += userfaultfd.o
fs/namespace.c +23 -1
@@ -36,7 +36,7 @@#include "internal.h" /* Maximum number of mounts in a mount namespace */ -unsigned int sysctl_mount_max__read_mostly =100000 ; +static unsigned int sysctl_mount_max__read_mostly =100000 ;static unsigned int m_hash_mask__read_mostly ;static unsigned int m_hash_shift__read_mostly ;
@@ -4612,3 +4612,25 @@ const struct proc_ns_operations mntns_operations = { .install = mntns_install, .owner = mntns_owner, }; + +#ifdef CONFIG_SYSCTL +static struct ctl_table fs_namespace_sysctls[] = { + { + .procname ="mount-max" , + .data = &sysctl_mount_max, + .maxlen =sizeof (unsigned int ), + .mode =0644 , + .proc_handler = proc_dointvec_minmax, + .extra1 =SYSCTL_ONE , + }, + { } +}; + +static int __init init_fs_namespace_sysctls (void ) +{ +register_sysctl_init ("fs" , fs_namespace_sysctls); +return 0 ; +} +fs_initcall (init_fs_namespace_sysctls); + +#endif /* CONFIG_SYSCTL */
fs/sysctls.c +5 -4
@@ -29,10 +29,11 @@ static struct ctl_table fs_shared_sysctls[] = { { } }; -static int __init init_fs_shared_sysctls (void ) +DECLARE_SYSCTL_BASE (fs, fs_shared_sysctls); + +static int __init init_fs_sysctls (void ) { -register_sysctl_init ("fs" , fs_shared_sysctls); -return 0 ; +return register_sysctl_base (fs); } -early_initcall (init_fs_shared_sysctls); +early_initcall (init_fs_sysctls);
include/linux/mount.h +0 -3
@@ -113,9 +113,6 @@ extern void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list);extern void mark_mounts_for_expiry (struct list_head *mounts);extern dev_t name_to_dev_t (const char *name); - -extern unsigned int sysctl_mount_max; -extern bool path_is_mountpoint (const struct path *path);extern void kern_unmount_array (struct vfsmount *mnt[],unsigned int num);
kernel/sysctl.c +0 -14
@@ -2810,18 +2810,6 @@ static struct ctl_table vm_table[] = { { } }; -static struct ctl_table fs_table[] = { - { - .procname ="mount-max" , - .data = &sysctl_mount_max, - .maxlen =sizeof (unsigned int ), - .mode =0644 , - .proc_handler = proc_dointvec_minmax, - .extra1 =SYSCTL_ONE , - }, - { } -}; - static struct ctl_table debug_table[] = { #ifdef CONFIG_SYSCTL_EXCEPTION_TRACE {
@@ -2852,7 +2840,6 @@ static struct ctl_table dev_table[] = {DECLARE_SYSCTL_BASE (kernel, kern_table);DECLARE_SYSCTL_BASE (vm, vm_table); -DECLARE_SYSCTL_BASE (fs, fs_table);DECLARE_SYSCTL_BASE (debug, debug_table);DECLARE_SYSCTL_BASE (dev, dev_table);
@@ -2860,7 +2847,6 @@ int __init sysctl_init(void) {register_sysctl_base (kernel);register_sysctl_base (vm); -register_sysctl_base (fs);register_sysctl_base (debug);register_sysctl_base (dev);
Signature · 1 line
2.33.0
PATCH 3/6 kernel/sysctl.c: rename sysctl_init() to sysctl_init_bases()
In reply to Luis ChamberlainRename sysctl_init() to sysctl_init_bases() so to reflect exactly what this is doing.
--- arch/arm/mm/alignment.c | 2 +- arch/sh/mm/alignment.c | 2 +- fs/proc/proc_sysctl.c | 4 ++-- include/linux/sysctl.h | 2 +- kernel/sysctl.c | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-)
arch/arm/mm/alignment.c +1 -1
@@ -1005,7 +1005,7 @@ static int __init noalign_setup(char *__unused)__setup ("noalign" , noalign_setup);/* -* This needs to be done after sysctl_init, otherwise sys/ will be +* This needs to be done after sysctl_init_bases(), otherwise sys/ will be * overwritten. Actually, this shouldn't be in sys/ at all since * it isn't a sysctl, and it doesn't contain sysctl information. * We now locate it in /proc/cpu/alignment instead.
arch/sh/mm/alignment.c +1 -1
@@ -161,7 +161,7 @@ static const struct proc_ops alignment_proc_ops = { };/* -* This needs to be done after sysctl_init, otherwise sys/ will be +* This needs to be done after sysctl_init_bases(), otherwise sys/ will be * overwritten. Actually, this shouldn't be in sys/ at all since * it isn't a sysctl, and it doesn't contain sysctl information. * We now locate it in /proc/cpu/alignment instead.
fs/proc/proc_sysctl.c +2 -2
@@ -1419,7 +1419,7 @@ EXPORT_SYMBOL(register_sysctl);* 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(). +* sysctl_init_bases(). */ void __init __register_sysctl_init (const char *path,struct ctl_table *table,const char *table_name)
@@ -1768,7 +1768,7 @@ int __init proc_sys_init(void) proc_sys_root->proc_dir_ops = &proc_sys_dir_file_operations; proc_sys_root->nlink =0 ; -return sysctl_init (); +return sysctl_init_bases (); }struct sysctl_alias {
include/linux/sysctl.h +1 -1
@@ -227,7 +227,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 ); +extern int sysctl_init_bases (void );extern void __register_sysctl_init (const char *path,struct ctl_table *table,const char *table_name);#define register_sysctl_init (path, table) __register_sysctl_init(path, table, #table)
kernel/sysctl.c +1 -1
@@ -2843,7 +2843,7 @@ DECLARE_SYSCTL_BASE(vm, vm_table);DECLARE_SYSCTL_BASE (debug, debug_table);DECLARE_SYSCTL_BASE (dev, dev_table); -int __init sysctl_init (void ) +int __init sysctl_init_bases (void ) {register_sysctl_base (kernel);register_sysctl_base (vm);
Signature · 1 line
2.33.0
PATCH 4/6 printk: fix build warning when CONFIG_PRINTK=n
In reply to Luis ChamberlainFrom: Xiaoming Ni <[email protected]>
build warning when CONFIG_PRINTK=n
kernel/printk/printk.c:175:5: warning: no previous prototype for
'devkmsg_sysctl_set_loglvl' [-Wmissing-prototypes]
devkmsg_sysctl_set_loglvl() is only used in sysctl.c when CONFIG_PRINTK=y, but it participates in the build when CONFIG_PRINTK=n. So add compile dependency CONFIG_PRINTK=y && CONFIG_SYSCTL=y to fix the build warning.
--- include/linux/printk.h | 4 ---- kernel/printk/internal.h | 2 ++ kernel/printk/printk.c | 3 ++- 3 files changed, 4 insertions(+), 5 deletions(-)
include/linux/printk.h +0 -4
@@ -183,10 +183,6 @@ extern bool printk_timed_ratelimit(unsigned long *caller_jiffies,extern int printk_delay_msec;extern int dmesg_restrict; -extern int -devkmsg_sysctl_set_loglvl (struct ctl_table *table,int write,void *buf, -size_t *lenp,loff_t *ppos); -extern void wake_up_klogd (void );char *log_buf_addr_get (void );
kernel/printk/internal.h +2 -0
@@ -6,6 +6,8 @@#if defined(CONFIG_PRINTK ) && defined(CONFIG_SYSCTL )void __init printk_sysctl_init (void ); +int devkmsg_sysctl_set_loglvl (struct ctl_table *table,int write, +void *buffer,size_t *lenp,loff_t *ppos);#else #define printk_sysctl_init () do { } while (0)#endif
kernel/printk/printk.c +2 -1
@@ -171,7 +171,7 @@ static int __init control_devkmsg(char *str)__setup ("printk.devkmsg=" , control_devkmsg);char devkmsg_log_str[DEVKMSG_STR_MAX_SIZE ] ="ratelimit" ; - +#if defined(CONFIG_PRINTK ) && defined(CONFIG_SYSCTL )int devkmsg_sysctl_set_loglvl (struct ctl_table *table,int write,void *buffer,size_t *lenp,loff_t *ppos) {
@@ -210,6 +210,7 @@ int devkmsg_sysctl_set_loglvl(struct ctl_table *table, int write, return 0; } +#endif /* CONFIG_PRINTK && CONFIG_SYSCTL */ /* Number of registered extended console drivers. */ static int nr_ext_console_drivers;
Signature · 1 line
2.33.0
PATCH 5/6 fs/coredump: move coredump sysctls into its own file
In reply to Luis ChamberlainFrom: Xiaoming Ni <[email protected]>
This moves the fs/coredump.c respective sysctls to its own file.
--- fs/coredump.c | 66 +++++++++++++++++++++++++++++++++++++--- fs/exec.c | 55 --------------------------------- include/linux/coredump.h | 10 +++--- kernel/sysctl.c | 2 -- 4 files changed, 67 insertions(+), 66 deletions(-)
fs/coredump.c +61 -5
@@ -41,6 +41,7 @@#include <linux/fs.h> #include <linux/path.h> #include <linux/timekeeping.h> +#include <linux/sysctl.h> #include <linux/uaccess.h> #include <asm/mmu_context.h>
@@ -52,9 +53,9 @@#include <trace/events/sched.h> -int core_uses_pid; -unsigned int core_pipe_limit; -char core_pattern[CORENAME_MAX_SIZE ] ="core" ; +static int core_uses_pid; +static unsigned int core_pipe_limit; +static char core_pattern[CORENAME_MAX_SIZE ] ="core" ;static int core_name_size =CORENAME_MAX_SIZE ;struct core_name {
@@ -62,8 +63,6 @@ struct core_name {int used, size; }; -/* The maximal length of core_pattern is also specified in sysctl.c */ -static int expand_corename (struct core_name *cn,int size) {char *corename =krealloc (cn->corename, size,GFP_KERNEL );
@@ -895,6 +894,63 @@ int dump_align(struct coredump_params *cprm, int align) }EXPORT_SYMBOL (dump_align); +#ifdef CONFIG_SYSCTL + +void validate_coredump_safety (void ) +{ +if (suid_dumpable ==SUID_DUMP_ROOT && + core_pattern[0 ] !='/' && core_pattern[0 ] !='|' ) { +pr_warn ( +"Unsafe core_pattern used with fs.suid_dumpable=2.\n" +"Pipe handler or fully qualified core dump path required.\n" +"Set kernel.core_pattern before fs.suid_dumpable.\n" + ); + } +} + +static int proc_dostring_coredump (struct ctl_table *table,int write, +void *buffer,size_t *lenp,loff_t *ppos) +{ +int error =proc_dostring (table, write, buffer, lenp, ppos); + +if (!error) +validate_coredump_safety (); +return error; +} + +static struct ctl_table coredump_sysctls[] = { + { + .procname ="core_uses_pid" , + .data = &core_uses_pid, + .maxlen =sizeof (int ), + .mode =0644 , + .proc_handler = proc_dointvec, + }, + { + .procname ="core_pattern" , + .data = core_pattern, + .maxlen =CORENAME_MAX_SIZE , + .mode =0644 , + .proc_handler = proc_dostring_coredump, + }, + { + .procname ="core_pipe_limit" , + .data = &core_pipe_limit, + .maxlen =sizeof (unsigned int ), + .mode =0644 , + .proc_handler = proc_dointvec, + }, + { } +}; + +static int __init init_fs_coredump_sysctls (void ) +{ +register_sysctl_init ("kernel" , coredump_sysctls); +return 0 ; +} +fs_initcall (init_fs_coredump_sysctls); +#endif /* CONFIG_SYSCTL */ +/* * The purpose of always_dump_vma() is to make sure that special kernel mappings * that are useful for post-mortem analysis are included in every core dump.
fs/exec.c +0 -55
70 lines not on this page · raw message
include/linux/coredump.h +6 -4
@@ -14,10 +14,6 @@ struct core_vma_metadata {unsigned long dump_size; }; -extern int core_uses_pid; -extern char core_pattern[]; -extern unsigned int core_pipe_limit; -/* * These are the only things you should do on a core-file: use only these * functions to write out all the necessary info.
@@ -37,4 +33,10 @@ extern void do_coredump(const kernel_siginfo_t *siginfo);static inline void do_coredump (const kernel_siginfo_t *siginfo) {}#endif +#if defined(CONFIG_COREDUMP ) && defined(CONFIG_SYSCTL ) +extern void validate_coredump_safety (void ); +#else +static inline void validate_coredump_safety (void ) {} +#endif +#endif /* _LINUX_COREDUMP_H */
kernel/sysctl.c +0 -2
@@ -61,12 +61,10 @@#include <linux/capability.h> #include <linux/binfmts.h> #include <linux/sched/sysctl.h> -#include <linux/sched/coredump.h> #include <linux/kexec.h> #include <linux/bpf.h> #include <linux/mount.h> #include <linux/userfaultfd_k.h> -#include <linux/coredump.h> #include <linux/latencytop.h> #include <linux/pid.h> #include <linux/delayacct.h>
Signature · 1 line
2.33.0
PATCH 6/6 kprobe: move sysctl_kprobes_optimization to kprobes.c
In reply to Luis ChamberlainFrom: Xiaoming Ni <[email protected]>
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.
Move sysctl_kprobes_optimization from kernel/sysctl.c to kernel/kprobes.c. Use register_sysctl() to register the sysctl interface.
--- include/linux/kprobes.h | 6 ------ kernel/kprobes.c | 30 ++++++++++++++++++++++++++---- kernel/sysctl.c | 12 ------------ 3 files changed, 26 insertions(+), 22 deletions(-)
include/linux/kprobes.h +0 -6
@@ -346,12 +346,6 @@ extern void opt_pre_handler(struct kprobe *p, struct pt_regs *regs);DEFINE_INSN_CACHE_OPS (optinsn); -#ifdef CONFIG_SYSCTL -extern int sysctl_kprobes_optimization; -extern int proc_kprobes_optimization_handler (struct ctl_table *table, -int write,void *buffer, -size_t *length,loff_t *ppos); -#endif /* CONFIG_SYSCTL */ extern void wait_for_kprobe_optimizer (void );#else /* !CONFIG_OPTPROBES */ static inline void wait_for_kprobe_optimizer (void ) { }
kernel/kprobes.c +26 -4
@@ -48,6 +48,9 @@#define KPROBE_HASH_BITS 6#define KPROBE_TABLE_SIZE (1 << KPROBE_HASH_BITS) +#if !defined(CONFIG_OPTPROBES ) || !defined(CONFIG_SYSCTL ) +#define kprobe_sysctls_init () do { } while (0) +#endif static int kprobes_initialized;/* kprobe_table can be accessed by
@@ -938,10 +941,10 @@ static void unoptimize_all_kprobes(void) }static DEFINE_MUTEX (kprobe_sysctl_mutex); -int sysctl_kprobes_optimization; -int proc_kprobes_optimization_handler (struct ctl_table *table,int write, -void *buffer,size_t *length, -loff_t *ppos) +static int sysctl_kprobes_optimization; +static int proc_kprobes_optimization_handler (struct ctl_table *table, +int write,void *buffer, +size_t *length,loff_t *ppos) {int ret;
@@ -957,6 +960,24 @@ int proc_kprobes_optimization_handler(struct ctl_table *table, int write, return ret; } + +static struct ctl_table kprobe_sysctls[] = { + { + .procname ="kprobes-optimization" , + .data = &sysctl_kprobes_optimization, + .maxlen =sizeof (int ), + .mode =0644 , + .proc_handler = proc_kprobes_optimization_handler, + .extra1 =SYSCTL_ZERO , + .extra2 =SYSCTL_ONE , + }, + {} +}; + +static void __init kprobe_sysctls_init (void ) +{ +register_sysctl_init ("debug" , kprobe_sysctls); +}#endif /* CONFIG_SYSCTL */ /* Put a breakpoint for a probe. */
@@ -2581,6 +2602,7 @@ static int __init init_kprobes(void) err =register_module_notifier (&kprobe_module_nb); kprobes_initialized = (err ==0 ); +kprobe_sysctls_init ();return err; }early_initcall (init_kprobes);
kernel/sysctl.c +0 -12
@@ -55,7 +55,6 @@#include <linux/reboot.h> #include <linux/ftrace.h> #include <linux/perf_event.h> -#include <linux/kprobes.h> #include <linux/oom.h> #include <linux/kmod.h> #include <linux/capability.h>
@@ -2817,17 +2816,6 @@ static struct ctl_table debug_table[] = { .mode = 0644, .proc_handler = proc_dointvec }, -#endif -#if defined(CONFIG_OPTPROBES ) - { - .procname = "kprobes-optimization", - .data = &sysctl_kprobes_optimization, - .maxlen = sizeof(int), - .mode = 0644, - .proc_handler = proc_kprobes_optimization_handler, - .extra1 = SYSCTL_ZERO, - .extra2 = SYSCTL_ONE, - },#endif { } };
Signature · 1 line
2.33.0