kernel: split patches folder up into backport, pending and hack folders
[openwrt/openwrt.git] / target / linux / generic / hack-4.9 / 902-debloat_proc.patch
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
5
6 Signed-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 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
35
36 static int __init proc_locks_init(void)
37 {
38 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
39 + return 0;
40 proc_create("locks", 0, NULL, &proc_locks_operations);
41 return 0;
42 }
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
48
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.
51 +
52 +config PROC_STRIPPED
53 + default n
54 + depends on EXPERT
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
61
62 static int __init proc_consoles_init(void)
63 {
64 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
65 + return 0;
66 +
67 proc_create("consoles", 0, NULL, &proc_consoles_operations);
68 return 0;
69 }
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)
76 {
77 struct proc_dir_entry *ent;
78 -
79 +
80 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
81 + return;
82 +
83 if (!driver->driver_name || driver->proc_entry ||
84 !driver->ops->proc_fops)
85 return;
86 @@ -160,6 +163,9 @@ void proc_tty_unregister_driver(struct t
87 {
88 struct proc_dir_entry *ent;
89
90 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
91 + return;
92 +
93 ent = driver->proc_entry;
94 if (!ent)
95 return;
96 @@ -174,6 +180,9 @@ void proc_tty_unregister_driver(struct t
97 */
98 void __init proc_tty_init(void)
99 {
100 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
101 + return;
102 +
103 if (!proc_mkdir("tty", NULL))
104 return;
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
113
114 +#ifdef CONFIG_PROC_STRIPPED
115 +#define __SNMP_STATS_DUMMY(mib) \
116 + do { (void) mib->mibs[0]; } while(0)
117 +
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)
126 +
127 +#else
128 +
129 #define __SNMP_INC_STATS(mib, field) \
130 __this_cpu_inc(mib->mibs[field])
131
132 @@ -153,8 +168,9 @@ struct linux_xfrm_mib {
133 __this_cpu_add(ptr[basefield##OCTETS], addend); \
134 } while (0)
135
136 +#endif
137
138 -#if BITS_PER_LONG==32
139 +#if (BITS_PER_LONG==32) && !defined(CONFIG_PROC_STRIPPED)
140
141 #define __SNMP_ADD_STATS64(mib, field, addend) \
142 do { \
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)
148 {
149 msg_init_ns(&init_ipc_ns);
150
151 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
152 + return;
153 +
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)
163 {
164 sem_init_ns(&init_ipc_ns);
165 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
166 + return;
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);
175
176 void __init shm_init(void)
177 {
178 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
179 + return;
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;
190
191 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
192 + return;
193 +
194 iface = kmalloc(sizeof(*iface), GFP_KERNEL);
195 if (!iface)
196 return;
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
202
203 static int __init proc_execdomains_init(void)
204 {
205 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
206 + return 0;
207 proc_create("execdomains", 0, NULL, &execdomains_proc_fops);
208 return 0;
209 }
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];
217
218 + if (IS_ENABLED(CONFIG_PROC_STRIPPED) && !IS_ENABLED(CONFIG_SMP))
219 + return;
220 +
221 if (!root_irq_dir || (desc->irq_data.chip == &no_irq_chip))
222 return;
223
224 @@ -374,6 +377,9 @@ void unregister_irq_proc(unsigned int ir
225 {
226 char name [MAX_NAMELEN];
227
228 + if (IS_ENABLED(CONFIG_PROC_STRIPPED) && !IS_ENABLED(CONFIG_SMP))
229 + return;
230 +
231 if (!root_irq_dir || !desc->dir)
232 return;
233 #ifdef CONFIG_SMP
234 @@ -408,6 +414,9 @@ void init_irq_proc(void)
235 unsigned int irq;
236 struct irq_desc *desc;
237
238 + if (IS_ENABLED(CONFIG_PROC_STRIPPED) && !IS_ENABLED(CONFIG_SMP))
239 + return;
240 +
241 /* create /proc/irq */
242 root_irq_dir = proc_mkdir("irq", NULL);
243 if (!root_irq_dir)
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
249 {
250 struct proc_dir_entry *pe;
251
252 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
253 + return 0;
254 pe = proc_create("timer_list", 0444, NULL, &timer_list_fops);
255 if (!pe)
256 return -ENOMEM;
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
262
263 static int __init proc_vmalloc_init(void)
264 {
265 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
266 + return 0;
267 proc_create("vmallocinfo", S_IRUSR, NULL, &proc_vmalloc_operations);
268 return 0;
269 }
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();
276 #endif
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);
284 + }
285 proc_create("vmstat", S_IRUGO, NULL, &proc_vmstat_file_operations);
286 - proc_create("zoneinfo", S_IRUGO, NULL, &proc_zoneinfo_file_operations);
287 #endif
288 return 0;
289 }
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)
295 {
296 struct vlan_net *vn = net_generic(net, vlan_net_id);
297
298 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
299 + return;
300 +
301 if (vn->proc_vlan_conf)
302 remove_proc_entry(name_conf, vn->proc_vlan_dir);
303
304 @@ -146,6 +149,9 @@ int __net_init vlan_proc_init(struct net
305 {
306 struct vlan_net *vn = net_generic(net, vlan_net_id);
307
308 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
309 + return 0;
310 +
311 vn->proc_vlan_dir = proc_net_mkdir(net, name_root, net->proc_net);
312 if (!vn->proc_vlan_dir)
313 goto err;
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(
319
320 if (!proc_create("dev", S_IRUGO, net->proc_net, &dev_seq_fops))
321 goto out;
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,
325 &softnet_seq_fops))
326 goto out_dev;
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))
330 goto out_softnet;
331
332 if (wext_proc_init(net))
333 @@ -331,9 +333,11 @@ static int __net_init dev_proc_net_init(
334 out:
335 return rc;
336 out_ptype:
337 - remove_proc_entry("ptype", net->proc_net);
338 + if (!IS_ENABLED(CONFIG_PROC_STRIPPED))
339 + remove_proc_entry("ptype", net->proc_net);
340 out_softnet:
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);
344 out_dev:
345 remove_proc_entry("dev", net->proc_net);
346 goto out;
347 @@ -343,8 +347,10 @@ static void __net_exit dev_proc_net_exit
348 {
349 wext_proc_exit(net);
350
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);
356 + }
357 remove_proc_entry("dev", net->proc_net);
358 }
359
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
365
366 static int __init proto_init(void)
367 {
368 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
369 + return 0;
370 return register_pernet_subsys(&proto_net_ops);
371 }
372
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_
378
379 int __net_init fib_proc_init(struct net *net)
380 {
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))
384 goto out1;
385
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,
389 &fib_triestat_fops))
390 goto out2;
391
392 @@ -2680,17 +2682,21 @@ int __net_init fib_proc_init(struct net
393 return 0;
394
395 out3:
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);
399 out2:
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);
403 out1:
404 return -ENOMEM;
405 }
406
407 void __net_exit fib_proc_exit(struct net *net)
408 {
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);
414 + }
415 remove_proc_entry("route", net->proc_net);
416 }
417
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
423
424 int __init ip_misc_proc_init(void)
425 {
426 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
427 + return 0;
428 +
429 return register_pernet_subsys(&ip_proc_ops);
430 }
431
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
437
438 static int __init ip_rt_proc_init(void)
439 {
440 + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
441 + return 0;
442 +
443 return register_pernet_subsys(&ip_rt_proc_ops);
444 }
445