1 From 9e3f1d0805b2d919904dd9a4ff0d956314cc3cba Mon Sep 17 00:00:00 2001
2 From: Felix Fietkau <nbd@nbd.name>
3 Date: Sat, 8 Jul 2017 08:20:09 +0200
4 Subject: debloat: procfs
6 Signed-off-by: Felix Fietkau <nbd@nbd.name>
9 fs/proc/Kconfig | 5 +++++
10 fs/proc/consoles.c | 3 +++
11 fs/proc/proc_tty.c | 11 ++++++++++-
12 include/net/snmp.h | 18 +++++++++++++++++-
17 kernel/exec_domain.c | 2 ++
18 kernel/irq/proc.c | 9 +++++++++
19 kernel/time/timer_list.c | 2 ++
21 mm/vmstat.c | 8 +++++---
22 net/8021q/vlanproc.c | 6 ++++++
23 net/core/net-procfs.c | 18 ++++++++++++------
24 net/core/sock.c | 2 ++
25 net/ipv4/fib_trie.c | 18 ++++++++++++------
26 net/ipv4/proc.c | 3 +++
27 net/ipv4/route.c | 3 +++
28 20 files changed, 105 insertions(+), 17 deletions(-)
32 @@ -2802,6 +2802,8 @@ static const struct file_operations proc
34 static int __init proc_locks_init(void)
36 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
38 proc_create("locks", 0, NULL, &proc_locks_operations);
43 @@ -81,3 +81,8 @@ config PROC_CHILDREN
45 Say Y if you are running any user-space software which takes benefit from
46 this interface. For example, rkt is such a piece of software.
51 + bool "Strip non-essential /proc functionality to reduce code size"
52 --- a/fs/proc/consoles.c
53 +++ b/fs/proc/consoles.c
54 @@ -106,6 +106,9 @@ static const struct file_operations proc
56 static int __init proc_consoles_init(void)
58 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
61 proc_create("consoles", 0, NULL, &proc_consoles_operations);
64 --- a/fs/proc/proc_tty.c
65 +++ b/fs/proc/proc_tty.c
66 @@ -144,7 +144,10 @@ static const struct file_operations proc
67 void proc_tty_register_driver(struct tty_driver *driver)
69 struct proc_dir_entry *ent;
72 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
75 if (!driver->driver_name || driver->proc_entry ||
76 !driver->ops->proc_fops)
78 @@ -161,6 +164,9 @@ void proc_tty_unregister_driver(struct t
80 struct proc_dir_entry *ent;
82 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
85 ent = driver->proc_entry;
88 @@ -175,6 +181,9 @@ void proc_tty_unregister_driver(struct t
90 void __init proc_tty_init(void)
92 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
95 if (!proc_mkdir("tty", NULL))
97 proc_mkdir("tty/ldisc", NULL); /* Preserved: it's userspace visible */
98 --- a/include/net/snmp.h
99 +++ b/include/net/snmp.h
100 @@ -123,6 +123,21 @@ struct linux_xfrm_mib {
101 #define DECLARE_SNMP_STAT(type, name) \
102 extern __typeof__(type) __percpu *name
104 +#ifdef CONFIG_PROC_STRIPPED
105 +#define __SNMP_STATS_DUMMY(mib) \
106 + do { (void) mib->mibs[0]; } while(0)
108 +#define __SNMP_INC_STATS(mib, field) __SNMP_STATS_DUMMY(mib)
109 +#define SNMP_INC_STATS_ATOMIC_LONG(mib, field) __SNMP_STATS_DUMMY(mib)
110 +#define SNMP_INC_STATS(mib, field) __SNMP_STATS_DUMMY(mib)
111 +#define SNMP_DEC_STATS(mib, field) __SNMP_STATS_DUMMY(mib)
112 +#define __SNMP_ADD_STATS(mib, field, addend) __SNMP_STATS_DUMMY(mib)
113 +#define SNMP_ADD_STATS(mib, field, addend) __SNMP_STATS_DUMMY(mib)
114 +#define SNMP_UPD_PO_STATS(mib, basefield, addend) __SNMP_STATS_DUMMY(mib)
115 +#define __SNMP_UPD_PO_STATS(mib, basefield, addend) __SNMP_STATS_DUMMY(mib)
119 #define __SNMP_INC_STATS(mib, field) \
120 __this_cpu_inc(mib->mibs[field])
122 @@ -153,8 +168,9 @@ struct linux_xfrm_mib {
123 __this_cpu_add(ptr[basefield##OCTETS], addend); \
128 -#if BITS_PER_LONG==32
129 +#if (BITS_PER_LONG==32) && !defined(CONFIG_PROC_STRIPPED)
131 #define __SNMP_ADD_STATS64(mib, field, addend) \
135 @@ -1061,6 +1061,9 @@ void __init msg_init(void)
137 msg_init_ns(&init_ipc_ns);
139 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
142 ipc_init_proc_interface("sysvipc/msg",
143 " key msqid perms cbytes qnum lspid lrpid uid gid cuid cgid stime rtime ctime\n",
144 IPC_MSG_IDS, sysvipc_msg_proc_show);
147 @@ -205,6 +205,8 @@ void sem_exit_ns(struct ipc_namespace *n
148 void __init sem_init(void)
150 sem_init_ns(&init_ipc_ns);
151 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
153 ipc_init_proc_interface("sysvipc/sem",
154 " key semid perms nsems uid gid cuid cgid otime ctime\n",
155 IPC_SEM_IDS, sysvipc_sem_proc_show);
158 @@ -118,6 +118,8 @@ pure_initcall(ipc_ns_init);
160 void __init shm_init(void)
162 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
164 ipc_init_proc_interface("sysvipc/shm",
165 #if BITS_PER_LONG <= 32
166 " key shmid perms size cpid lpid nattch uid gid cuid cgid atime dtime ctime rss swap\n",
169 @@ -121,6 +121,9 @@ void __init ipc_init_proc_interface(cons
170 struct proc_dir_entry *pde;
171 struct ipc_proc_iface *iface;
173 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
176 iface = kmalloc(sizeof(*iface), GFP_KERNEL);
179 --- a/kernel/exec_domain.c
180 +++ b/kernel/exec_domain.c
181 @@ -41,6 +41,8 @@ static const struct file_operations exec
183 static int __init proc_execdomains_init(void)
185 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
187 proc_create("execdomains", 0, NULL, &execdomains_proc_fops);
190 --- a/kernel/irq/proc.c
191 +++ b/kernel/irq/proc.c
192 @@ -326,6 +326,9 @@ void register_irq_proc(unsigned int irq,
193 static DEFINE_MUTEX(register_lock);
194 char name [MAX_NAMELEN];
196 + if (IS_ENABLED(CONFIG_PROC_STRIPPED) && !IS_ENABLED(CONFIG_SMP))
199 if (!root_irq_dir || (desc->irq_data.chip == &no_irq_chip))
202 @@ -374,6 +377,9 @@ void unregister_irq_proc(unsigned int ir
204 char name [MAX_NAMELEN];
206 + if (IS_ENABLED(CONFIG_PROC_STRIPPED) && !IS_ENABLED(CONFIG_SMP))
209 if (!root_irq_dir || !desc->dir)
212 @@ -408,6 +414,9 @@ void init_irq_proc(void)
214 struct irq_desc *desc;
216 + if (IS_ENABLED(CONFIG_PROC_STRIPPED) && !IS_ENABLED(CONFIG_SMP))
219 /* create /proc/irq */
220 root_irq_dir = proc_mkdir("irq", NULL);
222 --- a/kernel/time/timer_list.c
223 +++ b/kernel/time/timer_list.c
224 @@ -400,6 +400,9 @@ static int __init init_timer_list_procfs
226 struct proc_dir_entry *pe;
228 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
231 pe = proc_create("timer_list", 0400, NULL, &timer_list_fops);
236 @@ -2727,6 +2727,8 @@ static const struct file_operations proc
238 static int __init proc_vmalloc_init(void)
240 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
242 proc_create("vmallocinfo", S_IRUSR, NULL, &proc_vmalloc_operations);
247 @@ -1793,10 +1793,12 @@ static int __init setup_vmstat(void)
248 cpu_notifier_register_done();
250 #ifdef CONFIG_PROC_FS
251 - proc_create("buddyinfo", S_IRUGO, NULL, &fragmentation_file_operations);
252 - proc_create("pagetypeinfo", S_IRUGO, NULL, &pagetypeinfo_file_ops);
253 + if (!IS_ENABLED(CONFIG_PROC_STRIPPED)) {
254 + proc_create("buddyinfo", S_IRUGO, NULL, &fragmentation_file_operations);
255 + proc_create("pagetypeinfo", S_IRUGO, NULL, &pagetypeinfo_file_ops);
256 + proc_create("zoneinfo", S_IRUGO, NULL, &proc_zoneinfo_file_operations);
258 proc_create("vmstat", S_IRUGO, NULL, &proc_vmstat_file_operations);
259 - proc_create("zoneinfo", S_IRUGO, NULL, &proc_zoneinfo_file_operations);
263 --- a/net/8021q/vlanproc.c
264 +++ b/net/8021q/vlanproc.c
265 @@ -127,6 +127,9 @@ void vlan_proc_cleanup(struct net *net)
267 struct vlan_net *vn = net_generic(net, vlan_net_id);
269 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
272 if (vn->proc_vlan_conf)
273 remove_proc_entry(name_conf, vn->proc_vlan_dir);
275 @@ -146,6 +149,9 @@ int __net_init vlan_proc_init(struct net
277 struct vlan_net *vn = net_generic(net, vlan_net_id);
279 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
282 vn->proc_vlan_dir = proc_net_mkdir(net, name_root, net->proc_net);
283 if (!vn->proc_vlan_dir)
285 --- a/net/core/net-procfs.c
286 +++ b/net/core/net-procfs.c
287 @@ -319,10 +319,12 @@ static int __net_init dev_proc_net_init(
289 if (!proc_create("dev", S_IRUGO, net->proc_net, &dev_seq_fops))
291 - if (!proc_create("softnet_stat", S_IRUGO, net->proc_net,
292 + if (!IS_ENABLED(CONFIG_PROC_STRIPPED) &&
293 + !proc_create("softnet_stat", S_IRUGO, net->proc_net,
296 - if (!proc_create("ptype", S_IRUGO, net->proc_net, &ptype_seq_fops))
297 + if (!IS_ENABLED(CONFIG_PROC_STRIPPED) &&
298 + !proc_create("ptype", S_IRUGO, net->proc_net, &ptype_seq_fops))
301 if (wext_proc_init(net))
302 @@ -331,9 +333,11 @@ static int __net_init dev_proc_net_init(
306 - remove_proc_entry("ptype", net->proc_net);
307 + if (!IS_ENABLED(CONFIG_PROC_STRIPPED))
308 + remove_proc_entry("ptype", net->proc_net);
310 - remove_proc_entry("softnet_stat", net->proc_net);
311 + if (!IS_ENABLED(CONFIG_PROC_STRIPPED))
312 + remove_proc_entry("softnet_stat", net->proc_net);
314 remove_proc_entry("dev", net->proc_net);
316 @@ -343,8 +347,10 @@ static void __net_exit dev_proc_net_exit
320 - remove_proc_entry("ptype", net->proc_net);
321 - remove_proc_entry("softnet_stat", net->proc_net);
322 + if (!IS_ENABLED(CONFIG_PROC_STRIPPED)) {
323 + remove_proc_entry("ptype", net->proc_net);
324 + remove_proc_entry("softnet_stat", net->proc_net);
326 remove_proc_entry("dev", net->proc_net);
329 --- a/net/core/sock.c
330 +++ b/net/core/sock.c
331 @@ -3093,6 +3093,8 @@ static __net_initdata struct pernet_oper
333 static int __init proto_init(void)
335 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
337 return register_pernet_subsys(&proto_net_ops);
340 --- a/net/ipv4/fib_trie.c
341 +++ b/net/ipv4/fib_trie.c
342 @@ -2677,10 +2677,12 @@ static const struct file_operations fib_
344 int __net_init fib_proc_init(struct net *net)
346 - if (!proc_create("fib_trie", S_IRUGO, net->proc_net, &fib_trie_fops))
347 + if (!IS_ENABLED(CONFIG_PROC_STRIPPED) &&
348 + !proc_create("fib_trie", S_IRUGO, net->proc_net, &fib_trie_fops))
351 - if (!proc_create("fib_triestat", S_IRUGO, net->proc_net,
352 + if (!IS_ENABLED(CONFIG_PROC_STRIPPED) &&
353 + !proc_create("fib_triestat", S_IRUGO, net->proc_net,
357 @@ -2690,17 +2692,21 @@ int __net_init fib_proc_init(struct net
361 - remove_proc_entry("fib_triestat", net->proc_net);
362 + if (!IS_ENABLED(CONFIG_PROC_STRIPPED))
363 + remove_proc_entry("fib_triestat", net->proc_net);
365 - remove_proc_entry("fib_trie", net->proc_net);
366 + if (!IS_ENABLED(CONFIG_PROC_STRIPPED))
367 + remove_proc_entry("fib_trie", net->proc_net);
372 void __net_exit fib_proc_exit(struct net *net)
374 - remove_proc_entry("fib_trie", net->proc_net);
375 - remove_proc_entry("fib_triestat", net->proc_net);
376 + if (!IS_ENABLED(CONFIG_PROC_STRIPPED)) {
377 + remove_proc_entry("fib_trie", net->proc_net);
378 + remove_proc_entry("fib_triestat", net->proc_net);
380 remove_proc_entry("route", net->proc_net);
383 --- a/net/ipv4/proc.c
384 +++ b/net/ipv4/proc.c
385 @@ -566,6 +566,9 @@ static __net_initdata struct pernet_oper
387 int __init ip_misc_proc_init(void)
389 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
392 return register_pernet_subsys(&ip_proc_ops);
395 --- a/net/ipv4/route.c
396 +++ b/net/ipv4/route.c
397 @@ -423,6 +423,9 @@ static struct pernet_operations ip_rt_pr
399 static int __init ip_rt_proc_init(void)
401 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
404 return register_pernet_subsys(&ip_rt_proc_ops);