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 @@ -2961,6 +2961,8 @@ static const struct seq_operations locks
34 static int __init proc_locks_init(void)
36 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
38 proc_create_seq_private("locks", 0, NULL, &locks_seq_operations,
39 sizeof(struct locks_iterator), NULL);
43 @@ -100,6 +100,11 @@ config PROC_CHILDREN
44 Say Y if you are running any user-space software which takes benefit from
45 this interface. For example, rkt is such a piece of software.
50 + bool "Strip non-essential /proc functionality to reduce code size"
52 config PROC_PID_ARCH_STATUS
55 --- a/fs/proc/consoles.c
56 +++ b/fs/proc/consoles.c
57 @@ -92,6 +92,9 @@ static const struct seq_operations conso
59 static int __init proc_consoles_init(void)
61 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
64 proc_create_seq("consoles", 0, NULL, &consoles_op);
67 --- a/fs/proc/proc_tty.c
68 +++ b/fs/proc/proc_tty.c
69 @@ -133,7 +133,10 @@ static const struct seq_operations tty_d
70 void proc_tty_register_driver(struct tty_driver *driver)
72 struct proc_dir_entry *ent;
75 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
78 if (!driver->driver_name || driver->proc_entry ||
79 !driver->ops->proc_show)
81 @@ -150,6 +153,9 @@ void proc_tty_unregister_driver(struct t
83 struct proc_dir_entry *ent;
85 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
88 ent = driver->proc_entry;
91 @@ -164,6 +170,9 @@ void proc_tty_unregister_driver(struct t
93 void __init proc_tty_init(void)
95 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
98 if (!proc_mkdir("tty", NULL))
100 proc_mkdir("tty/ldisc", NULL); /* Preserved: it's userspace visible */
101 --- a/include/net/snmp.h
102 +++ b/include/net/snmp.h
103 @@ -118,6 +118,21 @@ struct linux_xfrm_mib {
104 #define DECLARE_SNMP_STAT(type, name) \
105 extern __typeof__(type) __percpu *name
107 +#ifdef CONFIG_PROC_STRIPPED
108 +#define __SNMP_STATS_DUMMY(mib) \
109 + do { (void) mib->mibs[0]; } while(0)
111 +#define __SNMP_INC_STATS(mib, field) __SNMP_STATS_DUMMY(mib)
112 +#define SNMP_INC_STATS_ATOMIC_LONG(mib, field) __SNMP_STATS_DUMMY(mib)
113 +#define SNMP_INC_STATS(mib, field) __SNMP_STATS_DUMMY(mib)
114 +#define SNMP_DEC_STATS(mib, field) __SNMP_STATS_DUMMY(mib)
115 +#define __SNMP_ADD_STATS(mib, field, addend) __SNMP_STATS_DUMMY(mib)
116 +#define SNMP_ADD_STATS(mib, field, addend) __SNMP_STATS_DUMMY(mib)
117 +#define SNMP_UPD_PO_STATS(mib, basefield, addend) __SNMP_STATS_DUMMY(mib)
118 +#define __SNMP_UPD_PO_STATS(mib, basefield, addend) __SNMP_STATS_DUMMY(mib)
122 #define __SNMP_INC_STATS(mib, field) \
123 __this_cpu_inc(mib->mibs[field])
125 @@ -148,8 +163,9 @@ struct linux_xfrm_mib {
126 __this_cpu_add(ptr[basefield##OCTETS], addend); \
131 -#if BITS_PER_LONG==32
132 +#if (BITS_PER_LONG==32) && !defined(CONFIG_PROC_STRIPPED)
134 #define __SNMP_ADD_STATS64(mib, field, addend) \
138 @@ -1317,6 +1317,9 @@ void __init msg_init(void)
140 msg_init_ns(&init_ipc_ns);
142 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
145 ipc_init_proc_interface("sysvipc/msg",
146 " key msqid perms cbytes qnum lspid lrpid uid gid cuid cgid stime rtime ctime\n",
147 IPC_MSG_IDS, sysvipc_msg_proc_show);
150 @@ -243,6 +243,8 @@ void sem_exit_ns(struct ipc_namespace *n
151 void __init sem_init(void)
153 sem_init_ns(&init_ipc_ns);
154 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
156 ipc_init_proc_interface("sysvipc/sem",
157 " key semid perms nsems uid gid cuid cgid otime ctime\n",
158 IPC_SEM_IDS, sysvipc_sem_proc_show);
161 @@ -144,6 +144,8 @@ pure_initcall(ipc_ns_init);
163 void __init shm_init(void)
165 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
167 ipc_init_proc_interface("sysvipc/shm",
168 #if BITS_PER_LONG <= 32
169 " key shmid perms size cpid lpid nattch uid gid cuid cgid atime dtime ctime rss swap\n",
172 @@ -140,6 +140,9 @@ void __init ipc_init_proc_interface(cons
173 struct proc_dir_entry *pde;
174 struct ipc_proc_iface *iface;
176 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
179 iface = kmalloc(sizeof(*iface), GFP_KERNEL);
182 --- a/kernel/exec_domain.c
183 +++ b/kernel/exec_domain.c
184 @@ -29,6 +29,8 @@ static int execdomains_proc_show(struct
186 static int __init proc_execdomains_init(void)
188 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
190 proc_create_single("execdomains", 0, NULL, execdomains_proc_show);
193 --- a/kernel/irq/proc.c
194 +++ b/kernel/irq/proc.c
195 @@ -319,6 +319,9 @@ void register_irq_proc(unsigned int irq,
196 void __maybe_unused *irqp = (void *)(unsigned long) irq;
197 char name [MAX_NAMELEN];
199 + if (IS_ENABLED(CONFIG_PROC_STRIPPED) && !IS_ENABLED(CONFIG_SMP))
202 if (!root_irq_dir || (desc->irq_data.chip == &no_irq_chip))
205 @@ -372,6 +375,9 @@ void unregister_irq_proc(unsigned int ir
207 char name [MAX_NAMELEN];
209 + if (IS_ENABLED(CONFIG_PROC_STRIPPED) && !IS_ENABLED(CONFIG_SMP))
212 if (!root_irq_dir || !desc->dir)
215 @@ -410,6 +416,9 @@ void init_irq_proc(void)
217 struct irq_desc *desc;
219 + if (IS_ENABLED(CONFIG_PROC_STRIPPED) && !IS_ENABLED(CONFIG_SMP))
222 /* create /proc/irq */
223 root_irq_dir = proc_mkdir("irq", NULL);
225 --- a/kernel/time/timer_list.c
226 +++ b/kernel/time/timer_list.c
227 @@ -370,6 +370,8 @@ static int __init init_timer_list_procfs
229 struct proc_dir_entry *pe;
231 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
233 pe = proc_create_seq_private("timer_list", 0400, NULL, &timer_list_sops,
234 sizeof(struct timer_list_iter), NULL);
238 @@ -3549,6 +3549,8 @@ static const struct seq_operations vmall
240 static int __init proc_vmalloc_init(void)
242 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
244 if (IS_ENABLED(CONFIG_NUMA))
245 proc_create_seq_private("vmallocinfo", 0400, NULL,
249 @@ -1988,10 +1988,12 @@ void __init init_mm_internals(void)
250 start_shepherd_timer();
252 #ifdef CONFIG_PROC_FS
253 - proc_create_seq("buddyinfo", 0444, NULL, &fragmentation_op);
254 - proc_create_seq("pagetypeinfo", 0400, NULL, &pagetypeinfo_op);
255 + if (!IS_ENABLED(CONFIG_PROC_STRIPPED)) {
256 + proc_create_seq("buddyinfo", 0444, NULL, &fragmentation_op);
257 + proc_create_seq("pagetypeinfo", 0400, NULL, &pagetypeinfo_op);
258 + proc_create_seq("zoneinfo", 0444, NULL, &zoneinfo_op);
260 proc_create_seq("vmstat", 0444, NULL, &vmstat_op);
261 - proc_create_seq("zoneinfo", 0444, NULL, &zoneinfo_op);
265 --- a/net/8021q/vlanproc.c
266 +++ b/net/8021q/vlanproc.c
267 @@ -93,6 +93,9 @@ void vlan_proc_cleanup(struct net *net)
269 struct vlan_net *vn = net_generic(net, vlan_net_id);
271 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
274 if (vn->proc_vlan_conf)
275 remove_proc_entry(name_conf, vn->proc_vlan_dir);
277 @@ -112,6 +115,9 @@ int __net_init vlan_proc_init(struct net
279 struct vlan_net *vn = net_generic(net, vlan_net_id);
281 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
284 vn->proc_vlan_dir = proc_net_mkdir(net, name_root, net->proc_net);
285 if (!vn->proc_vlan_dir)
287 --- a/net/core/net-procfs.c
288 +++ b/net/core/net-procfs.c
289 @@ -279,10 +279,12 @@ static int __net_init dev_proc_net_init(
290 if (!proc_create_net("dev", 0444, net->proc_net, &dev_seq_ops,
291 sizeof(struct seq_net_private)))
293 - if (!proc_create_seq("softnet_stat", 0444, net->proc_net,
294 + if (!IS_ENABLED(CONFIG_PROC_STRIPPED) &&
295 + !proc_create_seq("softnet_stat", 0444, net->proc_net,
298 - if (!proc_create_net("ptype", 0444, net->proc_net, &ptype_seq_ops,
299 + if (!IS_ENABLED(CONFIG_PROC_STRIPPED) &&
300 + !proc_create_net("ptype", 0444, net->proc_net, &ptype_seq_ops,
301 sizeof(struct seq_net_private)))
304 @@ -292,9 +294,11 @@ static int __net_init dev_proc_net_init(
308 - remove_proc_entry("ptype", net->proc_net);
309 + if (!IS_ENABLED(CONFIG_PROC_STRIPPED))
310 + remove_proc_entry("ptype", net->proc_net);
312 - remove_proc_entry("softnet_stat", net->proc_net);
313 + if (!IS_ENABLED(CONFIG_PROC_STRIPPED))
314 + remove_proc_entry("softnet_stat", net->proc_net);
316 remove_proc_entry("dev", net->proc_net);
318 @@ -304,8 +308,10 @@ static void __net_exit dev_proc_net_exit
322 - remove_proc_entry("ptype", net->proc_net);
323 - remove_proc_entry("softnet_stat", net->proc_net);
324 + if (!IS_ENABLED(CONFIG_PROC_STRIPPED)) {
325 + remove_proc_entry("ptype", net->proc_net);
326 + remove_proc_entry("softnet_stat", net->proc_net);
328 remove_proc_entry("dev", net->proc_net);
331 --- a/net/core/sock.c
332 +++ b/net/core/sock.c
333 @@ -3603,6 +3603,8 @@ static __net_initdata struct pernet_oper
335 static int __init proto_init(void)
337 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
339 return register_pernet_subsys(&proto_net_ops);
342 --- a/net/ipv4/fib_trie.c
343 +++ b/net/ipv4/fib_trie.c
344 @@ -2844,11 +2844,13 @@ static const struct seq_operations fib_r
346 int __net_init fib_proc_init(struct net *net)
348 - if (!proc_create_net("fib_trie", 0444, net->proc_net, &fib_trie_seq_ops,
349 + if (!IS_ENABLED(CONFIG_PROC_STRIPPED) &&
350 + !proc_create_net("fib_trie", 0444, net->proc_net, &fib_trie_seq_ops,
351 sizeof(struct fib_trie_iter)))
354 - if (!proc_create_net_single("fib_triestat", 0444, net->proc_net,
355 + if (!IS_ENABLED(CONFIG_PROC_STRIPPED) &&
356 + !proc_create_net_single("fib_triestat", 0444, net->proc_net,
357 fib_triestat_seq_show, NULL))
360 @@ -2859,17 +2861,21 @@ int __net_init fib_proc_init(struct net
364 - remove_proc_entry("fib_triestat", net->proc_net);
365 + if (!IS_ENABLED(CONFIG_PROC_STRIPPED))
366 + remove_proc_entry("fib_triestat", net->proc_net);
368 - remove_proc_entry("fib_trie", net->proc_net);
369 + if (!IS_ENABLED(CONFIG_PROC_STRIPPED))
370 + remove_proc_entry("fib_trie", net->proc_net);
375 void __net_exit fib_proc_exit(struct net *net)
377 - remove_proc_entry("fib_trie", net->proc_net);
378 - remove_proc_entry("fib_triestat", net->proc_net);
379 + if (!IS_ENABLED(CONFIG_PROC_STRIPPED)) {
380 + remove_proc_entry("fib_trie", net->proc_net);
381 + remove_proc_entry("fib_triestat", net->proc_net);
383 remove_proc_entry("route", net->proc_net);
386 --- a/net/ipv4/proc.c
387 +++ b/net/ipv4/proc.c
388 @@ -522,5 +522,8 @@ static __net_initdata struct pernet_oper
390 int __init ip_misc_proc_init(void)
392 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
395 return register_pernet_subsys(&ip_proc_ops);
397 --- a/net/ipv4/route.c
398 +++ b/net/ipv4/route.c
399 @@ -408,6 +408,9 @@ static struct pernet_operations ip_rt_pr
401 static int __init ip_rt_proc_init(void)
403 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
406 return register_pernet_subsys(&ip_rt_proc_ops);