blob: 44423db0d87f2c8b8eeb0ac5b3dbb43f304cd7df [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001From 9e3f1d0805b2d919904dd9a4ff0d956314cc3cba Mon Sep 17 00:00:00 2001
2From: Felix Fietkau <nbd@nbd.name>
3Date: Sat, 8 Jul 2017 08:20:09 +0200
4Subject: debloat: procfs
5
6Signed-off-by: Felix Fietkau <nbd@nbd.name>
7---
8 fs/locks.c | 2 ++
9 fs/proc/Kconfig | 5 +++++
10 fs/proc/consoles.c | 3 +++
11 fs/proc/proc_tty.c | 11 ++++++++++-
12 include/net/snmp.h | 18 +++++++++++++++++-
13 ipc/msg.c | 3 +++
14 ipc/sem.c | 2 ++
15 ipc/shm.c | 2 ++
16 ipc/util.c | 3 +++
17 kernel/exec_domain.c | 2 ++
18 kernel/irq/proc.c | 9 +++++++++
19 kernel/time/timer_list.c | 2 ++
20 mm/vmalloc.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(-)
29
30--- a/fs/locks.c
31+++ b/fs/locks.c
32@@ -2989,6 +2989,8 @@ static const struct seq_operations locks
33
34 static int __init proc_locks_init(void)
35 {
36+ if (IS_ENABLED(CONFIG_PROC_STRIPPED))
37+ return 0;
38 proc_create_seq_private("locks", 0, NULL, &locks_seq_operations,
39 sizeof(struct locks_iterator), NULL);
40 return 0;
41--- a/fs/proc/Kconfig
42+++ b/fs/proc/Kconfig
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.
46
47+config PROC_STRIPPED
48+ default n
49+ depends on EXPERT
50+ bool "Strip non-essential /proc functionality to reduce code size"
51+
52 config PROC_PID_ARCH_STATUS
53 def_bool n
54 depends on PROC_FS
55--- a/fs/proc/consoles.c
56+++ b/fs/proc/consoles.c
57@@ -92,6 +92,9 @@ static const struct seq_operations conso
58
59 static int __init proc_consoles_init(void)
60 {
61+ if (IS_ENABLED(CONFIG_PROC_STRIPPED))
62+ return 0;
63+
64 proc_create_seq("consoles", 0, NULL, &consoles_op);
65 return 0;
66 }
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)
71 {
72 struct proc_dir_entry *ent;
73-
74+
75+ if (IS_ENABLED(CONFIG_PROC_STRIPPED))
76+ return;
77+
78 if (!driver->driver_name || driver->proc_entry ||
79 !driver->ops->proc_show)
80 return;
81@@ -150,6 +153,9 @@ void proc_tty_unregister_driver(struct t
82 {
83 struct proc_dir_entry *ent;
84
85+ if (IS_ENABLED(CONFIG_PROC_STRIPPED))
86+ return;
87+
88 ent = driver->proc_entry;
89 if (!ent)
90 return;
91@@ -164,6 +170,9 @@ void proc_tty_unregister_driver(struct t
92 */
93 void __init proc_tty_init(void)
94 {
95+ if (IS_ENABLED(CONFIG_PROC_STRIPPED))
96+ return;
97+
98 if (!proc_mkdir("tty", NULL))
99 return;
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
106
107+#ifdef CONFIG_PROC_STRIPPED
108+#define __SNMP_STATS_DUMMY(mib) \
109+ do { (void) mib->mibs[0]; } while(0)
110+
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)
119+
120+#else
121+
122 #define __SNMP_INC_STATS(mib, field) \
123 __this_cpu_inc(mib->mibs[field])
124
125@@ -148,8 +163,9 @@ struct linux_xfrm_mib {
126 __this_cpu_add(ptr[basefield##OCTETS], addend); \
127 } while (0)
128
129+#endif
130
131-#if BITS_PER_LONG==32
132+#if (BITS_PER_LONG==32) && !defined(CONFIG_PROC_STRIPPED)
133
134 #define __SNMP_ADD_STATS64(mib, field, addend) \
135 do { \
136--- a/ipc/msg.c
137+++ b/ipc/msg.c
138@@ -1317,6 +1317,9 @@ void __init msg_init(void)
139 {
140 msg_init_ns(&init_ipc_ns);
141
142+ if (IS_ENABLED(CONFIG_PROC_STRIPPED))
143+ return;
144+
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);
148--- a/ipc/sem.c
149+++ b/ipc/sem.c
150@@ -243,6 +243,8 @@ void sem_exit_ns(struct ipc_namespace *n
151 void __init sem_init(void)
152 {
153 sem_init_ns(&init_ipc_ns);
154+ if (IS_ENABLED(CONFIG_PROC_STRIPPED))
155+ return;
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);
159--- a/ipc/shm.c
160+++ b/ipc/shm.c
161@@ -154,6 +154,8 @@ pure_initcall(ipc_ns_init);
162
163 void __init shm_init(void)
164 {
165+ if (IS_ENABLED(CONFIG_PROC_STRIPPED))
166+ return;
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",
170--- a/ipc/util.c
171+++ b/ipc/util.c
172@@ -140,6 +140,9 @@ void __init ipc_init_proc_interface(cons
173 struct proc_dir_entry *pde;
174 struct ipc_proc_iface *iface;
175
176+ if (IS_ENABLED(CONFIG_PROC_STRIPPED))
177+ return;
178+
179 iface = kmalloc(sizeof(*iface), GFP_KERNEL);
180 if (!iface)
181 return;
182--- a/kernel/exec_domain.c
183+++ b/kernel/exec_domain.c
184@@ -29,6 +29,8 @@ static int execdomains_proc_show(struct
185
186 static int __init proc_execdomains_init(void)
187 {
188+ if (IS_ENABLED(CONFIG_PROC_STRIPPED))
189+ return 0;
190 proc_create_single("execdomains", 0, NULL, execdomains_proc_show);
191 return 0;
192 }
193--- a/kernel/irq/proc.c
194+++ b/kernel/irq/proc.c
195@@ -341,6 +341,9 @@ void register_irq_proc(unsigned int irq,
196 void __maybe_unused *irqp = (void *)(unsigned long) irq;
197 char name [MAX_NAMELEN];
198
199+ if (IS_ENABLED(CONFIG_PROC_STRIPPED) && !IS_ENABLED(CONFIG_SMP))
200+ return;
201+
202 if (!root_irq_dir || (desc->irq_data.chip == &no_irq_chip))
203 return;
204
205@@ -394,6 +397,9 @@ void unregister_irq_proc(unsigned int ir
206 {
207 char name [MAX_NAMELEN];
208
209+ if (IS_ENABLED(CONFIG_PROC_STRIPPED) && !IS_ENABLED(CONFIG_SMP))
210+ return;
211+
212 if (!root_irq_dir || !desc->dir)
213 return;
214 #ifdef CONFIG_SMP
215@@ -432,6 +438,9 @@ void init_irq_proc(void)
216 unsigned int irq;
217 struct irq_desc *desc;
218
219+ if (IS_ENABLED(CONFIG_PROC_STRIPPED) && !IS_ENABLED(CONFIG_SMP))
220+ return;
221+
222 /* create /proc/irq */
223 root_irq_dir = proc_mkdir("irq", NULL);
224 if (!root_irq_dir)
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
228 {
229 struct proc_dir_entry *pe;
230
231+ if (IS_ENABLED(CONFIG_PROC_STRIPPED))
232+ return 0;
233 pe = proc_create_seq_private("timer_list", 0400, NULL, &timer_list_sops,
234 sizeof(struct timer_list_iter), NULL);
235 if (!pe)
236--- a/mm/vmalloc.c
237+++ b/mm/vmalloc.c
238@@ -3564,6 +3564,8 @@ static const struct seq_operations vmall
239
240 static int __init proc_vmalloc_init(void)
241 {
242+ if (IS_ENABLED(CONFIG_PROC_STRIPPED))
243+ return 0;
244 if (IS_ENABLED(CONFIG_NUMA))
245 proc_create_seq_private("vmallocinfo", 0400, NULL,
246 &vmalloc_op,
247--- a/mm/vmstat.c
248+++ b/mm/vmstat.c
249@@ -1984,10 +1984,12 @@ void __init init_mm_internals(void)
250 start_shepherd_timer();
251 #endif
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);
259+ }
260 proc_create_seq("vmstat", 0444, NULL, &vmstat_op);
261- proc_create_seq("zoneinfo", 0444, NULL, &zoneinfo_op);
262 #endif
263 }
264
265--- a/net/8021q/vlanproc.c
266+++ b/net/8021q/vlanproc.c
267@@ -93,6 +93,9 @@ void vlan_proc_cleanup(struct net *net)
268 {
269 struct vlan_net *vn = net_generic(net, vlan_net_id);
270
271+ if (IS_ENABLED(CONFIG_PROC_STRIPPED))
272+ return;
273+
274 if (vn->proc_vlan_conf)
275 remove_proc_entry(name_conf, vn->proc_vlan_dir);
276
277@@ -112,6 +115,9 @@ int __net_init vlan_proc_init(struct net
278 {
279 struct vlan_net *vn = net_generic(net, vlan_net_id);
280
281+ if (IS_ENABLED(CONFIG_PROC_STRIPPED))
282+ return 0;
283+
284 vn->proc_vlan_dir = proc_net_mkdir(net, name_root, net->proc_net);
285 if (!vn->proc_vlan_dir)
286 goto err;
287--- a/net/core/net-procfs.c
288+++ b/net/core/net-procfs.c
289@@ -309,10 +309,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)))
292 goto out;
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,
296 &softnet_seq_ops))
297 goto out_dev;
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)))
302 goto out_softnet;
303
304@@ -322,9 +324,11 @@ static int __net_init dev_proc_net_init(
305 out:
306 return rc;
307 out_ptype:
308- remove_proc_entry("ptype", net->proc_net);
309+ if (!IS_ENABLED(CONFIG_PROC_STRIPPED))
310+ remove_proc_entry("ptype", net->proc_net);
311 out_softnet:
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);
315 out_dev:
316 remove_proc_entry("dev", net->proc_net);
317 goto out;
318@@ -334,8 +338,10 @@ static void __net_exit dev_proc_net_exit
319 {
320 wext_proc_exit(net);
321
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);
327+ }
328 remove_proc_entry("dev", net->proc_net);
329 }
330
331--- a/net/core/sock.c
332+++ b/net/core/sock.c
333@@ -3657,6 +3657,8 @@ static __net_initdata struct pernet_oper
334
335 static int __init proto_init(void)
336 {
337+ if (IS_ENABLED(CONFIG_PROC_STRIPPED))
338+ return 0;
339 return register_pernet_subsys(&proto_net_ops);
340 }
341
342--- a/net/ipv4/fib_trie.c
343+++ b/net/ipv4/fib_trie.c
344@@ -2848,11 +2848,13 @@ static const struct seq_operations fib_r
345
346 int __net_init fib_proc_init(struct net *net)
347 {
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)))
352 goto out1;
353
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))
358 goto out2;
359
360@@ -2863,17 +2865,21 @@ int __net_init fib_proc_init(struct net
361 return 0;
362
363 out3:
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);
367 out2:
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);
371 out1:
372 return -ENOMEM;
373 }
374
375 void __net_exit fib_proc_exit(struct net *net)
376 {
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);
382+ }
383 remove_proc_entry("route", net->proc_net);
384 }
385
386--- a/net/ipv4/proc.c
387+++ b/net/ipv4/proc.c
388@@ -522,5 +522,8 @@ static __net_initdata struct pernet_oper
389
390 int __init ip_misc_proc_init(void)
391 {
392+ if (IS_ENABLED(CONFIG_PROC_STRIPPED))
393+ return 0;
394+
395 return register_pernet_subsys(&ip_proc_ops);
396 }
397--- a/net/ipv4/route.c
398+++ b/net/ipv4/route.c
399@@ -410,6 +410,9 @@ static struct pernet_operations ip_rt_pr
400
401 static int __init ip_rt_proc_init(void)
402 {
403+ if (IS_ENABLED(CONFIG_PROC_STRIPPED))
404+ return 0;
405+
406 return register_pernet_subsys(&ip_rt_proc_ops);
407 }
408