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(-)
30 Index: linux-4.9.34/fs/locks.c
31 ===================================================================
32 --- linux-4.9.34.orig/fs/locks.c
33 +++ linux-4.9.34/fs/locks.c
34 @@ -2802,6 +2802,8 @@ static const struct file_operations proc
36 static int __init proc_locks_init(void)
38 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
40 proc_create("locks", 0, NULL, &proc_locks_operations);
43 Index: linux-4.9.34/fs/proc/Kconfig
44 ===================================================================
45 --- linux-4.9.34.orig/fs/proc/Kconfig
46 +++ linux-4.9.34/fs/proc/Kconfig
47 @@ -81,3 +81,8 @@ config PROC_CHILDREN
49 Say Y if you are running any user-space software which takes benefit from
50 this interface. For example, rkt is such a piece of software.
55 + bool "Strip non-essential /proc functionality to reduce code size"
56 Index: linux-4.9.34/fs/proc/consoles.c
57 ===================================================================
58 --- linux-4.9.34.orig/fs/proc/consoles.c
59 +++ linux-4.9.34/fs/proc/consoles.c
60 @@ -106,6 +106,9 @@ static const struct file_operations proc
62 static int __init proc_consoles_init(void)
64 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
67 proc_create("consoles", 0, NULL, &proc_consoles_operations);
70 Index: linux-4.9.34/fs/proc/proc_tty.c
71 ===================================================================
72 --- linux-4.9.34.orig/fs/proc/proc_tty.c
73 +++ linux-4.9.34/fs/proc/proc_tty.c
74 @@ -143,7 +143,10 @@ static const struct file_operations proc
75 void proc_tty_register_driver(struct tty_driver *driver)
77 struct proc_dir_entry *ent;
80 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
83 if (!driver->driver_name || driver->proc_entry ||
84 !driver->ops->proc_fops)
86 @@ -160,6 +163,9 @@ void proc_tty_unregister_driver(struct t
88 struct proc_dir_entry *ent;
90 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
93 ent = driver->proc_entry;
96 @@ -174,6 +180,9 @@ void proc_tty_unregister_driver(struct t
98 void __init proc_tty_init(void)
100 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
103 if (!proc_mkdir("tty", NULL))
105 proc_mkdir("tty/ldisc", NULL); /* Preserved: it's userspace visible */
106 Index: linux-4.9.34/include/net/snmp.h
107 ===================================================================
108 --- linux-4.9.34.orig/include/net/snmp.h
109 +++ linux-4.9.34/include/net/snmp.h
110 @@ -123,6 +123,21 @@ struct linux_xfrm_mib {
111 #define DECLARE_SNMP_STAT(type, name) \
112 extern __typeof__(type) __percpu *name
114 +#ifdef CONFIG_PROC_STRIPPED
115 +#define __SNMP_STATS_DUMMY(mib) \
116 + do { (void) mib->mibs[0]; } while(0)
118 +#define __SNMP_INC_STATS(mib, field) __SNMP_STATS_DUMMY(mib)
119 +#define SNMP_INC_STATS_ATOMIC_LONG(mib, field) __SNMP_STATS_DUMMY(mib)
120 +#define SNMP_INC_STATS(mib, field) __SNMP_STATS_DUMMY(mib)
121 +#define SNMP_DEC_STATS(mib, field) __SNMP_STATS_DUMMY(mib)
122 +#define __SNMP_ADD_STATS(mib, field, addend) __SNMP_STATS_DUMMY(mib)
123 +#define SNMP_ADD_STATS(mib, field, addend) __SNMP_STATS_DUMMY(mib)
124 +#define SNMP_UPD_PO_STATS(mib, basefield, addend) __SNMP_STATS_DUMMY(mib)
125 +#define __SNMP_UPD_PO_STATS(mib, basefield, addend) __SNMP_STATS_DUMMY(mib)
129 #define __SNMP_INC_STATS(mib, field) \
130 __this_cpu_inc(mib->mibs[field])
132 @@ -153,8 +168,9 @@ struct linux_xfrm_mib {
133 __this_cpu_add(ptr[basefield##OCTETS], addend); \
138 -#if BITS_PER_LONG==32
139 +#if (BITS_PER_LONG==32) && !defined(CONFIG_PROC_STRIPPED)
141 #define __SNMP_ADD_STATS64(mib, field, addend) \
143 Index: linux-4.9.34/ipc/msg.c
144 ===================================================================
145 --- linux-4.9.34.orig/ipc/msg.c
146 +++ linux-4.9.34/ipc/msg.c
147 @@ -1058,6 +1058,9 @@ void __init msg_init(void)
149 msg_init_ns(&init_ipc_ns);
151 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
154 ipc_init_proc_interface("sysvipc/msg",
155 " key msqid perms cbytes qnum lspid lrpid uid gid cuid cgid stime rtime ctime\n",
156 IPC_MSG_IDS, sysvipc_msg_proc_show);
157 Index: linux-4.9.34/ipc/sem.c
158 ===================================================================
159 --- linux-4.9.34.orig/ipc/sem.c
160 +++ linux-4.9.34/ipc/sem.c
161 @@ -205,6 +205,8 @@ void sem_exit_ns(struct ipc_namespace *n
162 void __init sem_init(void)
164 sem_init_ns(&init_ipc_ns);
165 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
167 ipc_init_proc_interface("sysvipc/sem",
168 " key semid perms nsems uid gid cuid cgid otime ctime\n",
169 IPC_SEM_IDS, sysvipc_sem_proc_show);
170 Index: linux-4.9.34/ipc/shm.c
171 ===================================================================
172 --- linux-4.9.34.orig/ipc/shm.c
173 +++ linux-4.9.34/ipc/shm.c
174 @@ -118,6 +118,8 @@ pure_initcall(ipc_ns_init);
176 void __init shm_init(void)
178 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
180 ipc_init_proc_interface("sysvipc/shm",
181 #if BITS_PER_LONG <= 32
182 " key shmid perms size cpid lpid nattch uid gid cuid cgid atime dtime ctime rss swap\n",
183 Index: linux-4.9.34/ipc/util.c
184 ===================================================================
185 --- linux-4.9.34.orig/ipc/util.c
186 +++ linux-4.9.34/ipc/util.c
187 @@ -121,6 +121,9 @@ void __init ipc_init_proc_interface(cons
188 struct proc_dir_entry *pde;
189 struct ipc_proc_iface *iface;
191 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
194 iface = kmalloc(sizeof(*iface), GFP_KERNEL);
197 Index: linux-4.9.34/kernel/exec_domain.c
198 ===================================================================
199 --- linux-4.9.34.orig/kernel/exec_domain.c
200 +++ linux-4.9.34/kernel/exec_domain.c
201 @@ -41,6 +41,8 @@ static const struct file_operations exec
203 static int __init proc_execdomains_init(void)
205 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
207 proc_create("execdomains", 0, NULL, &execdomains_proc_fops);
210 Index: linux-4.9.34/kernel/irq/proc.c
211 ===================================================================
212 --- linux-4.9.34.orig/kernel/irq/proc.c
213 +++ linux-4.9.34/kernel/irq/proc.c
214 @@ -326,6 +326,9 @@ void register_irq_proc(unsigned int irq,
215 static DEFINE_MUTEX(register_lock);
216 char name [MAX_NAMELEN];
218 + if (IS_ENABLED(CONFIG_PROC_STRIPPED) && !IS_ENABLED(CONFIG_SMP))
221 if (!root_irq_dir || (desc->irq_data.chip == &no_irq_chip))
224 @@ -374,6 +377,9 @@ void unregister_irq_proc(unsigned int ir
226 char name [MAX_NAMELEN];
228 + if (IS_ENABLED(CONFIG_PROC_STRIPPED) && !IS_ENABLED(CONFIG_SMP))
231 if (!root_irq_dir || !desc->dir)
234 @@ -408,6 +414,9 @@ void init_irq_proc(void)
236 struct irq_desc *desc;
238 + if (IS_ENABLED(CONFIG_PROC_STRIPPED) && !IS_ENABLED(CONFIG_SMP))
241 /* create /proc/irq */
242 root_irq_dir = proc_mkdir("irq", NULL);
244 Index: linux-4.9.34/kernel/time/timer_list.c
245 ===================================================================
246 --- linux-4.9.34.orig/kernel/time/timer_list.c
247 +++ linux-4.9.34/kernel/time/timer_list.c
248 @@ -393,6 +393,8 @@ static int __init init_timer_list_procfs
250 struct proc_dir_entry *pe;
252 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
254 pe = proc_create("timer_list", 0444, NULL, &timer_list_fops);
257 Index: linux-4.9.34/mm/vmalloc.c
258 ===================================================================
259 --- linux-4.9.34.orig/mm/vmalloc.c
260 +++ linux-4.9.34/mm/vmalloc.c
261 @@ -2703,6 +2703,8 @@ static const struct file_operations proc
263 static int __init proc_vmalloc_init(void)
265 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
267 proc_create("vmallocinfo", S_IRUSR, NULL, &proc_vmalloc_operations);
270 Index: linux-4.9.34/mm/vmstat.c
271 ===================================================================
272 --- linux-4.9.34.orig/mm/vmstat.c
273 +++ linux-4.9.34/mm/vmstat.c
274 @@ -1791,10 +1791,12 @@ static int __init setup_vmstat(void)
275 cpu_notifier_register_done();
277 #ifdef CONFIG_PROC_FS
278 - proc_create("buddyinfo", S_IRUGO, NULL, &fragmentation_file_operations);
279 - proc_create("pagetypeinfo", S_IRUGO, NULL, &pagetypeinfo_file_ops);
280 + if (!IS_ENABLED(CONFIG_PROC_STRIPPED)) {
281 + proc_create("buddyinfo", S_IRUGO, NULL, &fragmentation_file_operations);
282 + proc_create("pagetypeinfo", S_IRUGO, NULL, &pagetypeinfo_file_ops);
283 + proc_create("zoneinfo", S_IRUGO, NULL, &proc_zoneinfo_file_operations);
285 proc_create("vmstat", S_IRUGO, NULL, &proc_vmstat_file_operations);
286 - proc_create("zoneinfo", S_IRUGO, NULL, &proc_zoneinfo_file_operations);
290 Index: linux-4.9.34/net/8021q/vlanproc.c
291 ===================================================================
292 --- linux-4.9.34.orig/net/8021q/vlanproc.c
293 +++ linux-4.9.34/net/8021q/vlanproc.c
294 @@ -127,6 +127,9 @@ void vlan_proc_cleanup(struct net *net)
296 struct vlan_net *vn = net_generic(net, vlan_net_id);
298 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
301 if (vn->proc_vlan_conf)
302 remove_proc_entry(name_conf, vn->proc_vlan_dir);
304 @@ -146,6 +149,9 @@ int __net_init vlan_proc_init(struct net
306 struct vlan_net *vn = net_generic(net, vlan_net_id);
308 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
311 vn->proc_vlan_dir = proc_net_mkdir(net, name_root, net->proc_net);
312 if (!vn->proc_vlan_dir)
314 Index: linux-4.9.34/net/core/net-procfs.c
315 ===================================================================
316 --- linux-4.9.34.orig/net/core/net-procfs.c
317 +++ linux-4.9.34/net/core/net-procfs.c
318 @@ -319,10 +319,12 @@ static int __net_init dev_proc_net_init(
320 if (!proc_create("dev", S_IRUGO, net->proc_net, &dev_seq_fops))
322 - if (!proc_create("softnet_stat", S_IRUGO, net->proc_net,
323 + if (!IS_ENABLED(CONFIG_PROC_STRIPPED) &&
324 + !proc_create("softnet_stat", S_IRUGO, net->proc_net,
327 - if (!proc_create("ptype", S_IRUGO, net->proc_net, &ptype_seq_fops))
328 + if (!IS_ENABLED(CONFIG_PROC_STRIPPED) &&
329 + !proc_create("ptype", S_IRUGO, net->proc_net, &ptype_seq_fops))
332 if (wext_proc_init(net))
333 @@ -331,9 +333,11 @@ static int __net_init dev_proc_net_init(
337 - remove_proc_entry("ptype", net->proc_net);
338 + if (!IS_ENABLED(CONFIG_PROC_STRIPPED))
339 + remove_proc_entry("ptype", net->proc_net);
341 - remove_proc_entry("softnet_stat", net->proc_net);
342 + if (!IS_ENABLED(CONFIG_PROC_STRIPPED))
343 + remove_proc_entry("softnet_stat", net->proc_net);
345 remove_proc_entry("dev", net->proc_net);
347 @@ -343,8 +347,10 @@ static void __net_exit dev_proc_net_exit
351 - remove_proc_entry("ptype", net->proc_net);
352 - remove_proc_entry("softnet_stat", net->proc_net);
353 + if (!IS_ENABLED(CONFIG_PROC_STRIPPED)) {
354 + remove_proc_entry("ptype", net->proc_net);
355 + remove_proc_entry("softnet_stat", net->proc_net);
357 remove_proc_entry("dev", net->proc_net);
360 Index: linux-4.9.34/net/core/sock.c
361 ===================================================================
362 --- linux-4.9.34.orig/net/core/sock.c
363 +++ linux-4.9.34/net/core/sock.c
364 @@ -3082,6 +3082,8 @@ static __net_initdata struct pernet_oper
366 static int __init proto_init(void)
368 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
370 return register_pernet_subsys(&proto_net_ops);
373 Index: linux-4.9.34/net/ipv4/fib_trie.c
374 ===================================================================
375 --- linux-4.9.34.orig/net/ipv4/fib_trie.c
376 +++ linux-4.9.34/net/ipv4/fib_trie.c
377 @@ -2667,10 +2667,12 @@ static const struct file_operations fib_
379 int __net_init fib_proc_init(struct net *net)
381 - if (!proc_create("fib_trie", S_IRUGO, net->proc_net, &fib_trie_fops))
382 + if (!IS_ENABLED(CONFIG_PROC_STRIPPED) &&
383 + !proc_create("fib_trie", S_IRUGO, net->proc_net, &fib_trie_fops))
386 - if (!proc_create("fib_triestat", S_IRUGO, net->proc_net,
387 + if (!IS_ENABLED(CONFIG_PROC_STRIPPED) &&
388 + !proc_create("fib_triestat", S_IRUGO, net->proc_net,
392 @@ -2680,17 +2682,21 @@ int __net_init fib_proc_init(struct net
396 - remove_proc_entry("fib_triestat", net->proc_net);
397 + if (!IS_ENABLED(CONFIG_PROC_STRIPPED))
398 + remove_proc_entry("fib_triestat", net->proc_net);
400 - remove_proc_entry("fib_trie", net->proc_net);
401 + if (!IS_ENABLED(CONFIG_PROC_STRIPPED))
402 + remove_proc_entry("fib_trie", net->proc_net);
407 void __net_exit fib_proc_exit(struct net *net)
409 - remove_proc_entry("fib_trie", net->proc_net);
410 - remove_proc_entry("fib_triestat", net->proc_net);
411 + if (!IS_ENABLED(CONFIG_PROC_STRIPPED)) {
412 + remove_proc_entry("fib_trie", net->proc_net);
413 + remove_proc_entry("fib_triestat", net->proc_net);
415 remove_proc_entry("route", net->proc_net);
418 Index: linux-4.9.34/net/ipv4/proc.c
419 ===================================================================
420 --- linux-4.9.34.orig/net/ipv4/proc.c
421 +++ linux-4.9.34/net/ipv4/proc.c
422 @@ -564,6 +564,9 @@ static __net_initdata struct pernet_oper
424 int __init ip_misc_proc_init(void)
426 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
429 return register_pernet_subsys(&ip_proc_ops);
432 Index: linux-4.9.34/net/ipv4/route.c
433 ===================================================================
434 --- linux-4.9.34.orig/net/ipv4/route.c
435 +++ linux-4.9.34/net/ipv4/route.c
436 @@ -420,6 +420,9 @@ static struct pernet_operations ip_rt_pr
438 static int __init ip_rt_proc_init(void)
440 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
443 return register_pernet_subsys(&ip_rt_proc_ops);