kernel: Bump to 4.14.277
[librecmc/librecmc.git] / target / linux / generic / hack-4.14 / 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 --- a/fs/locks.c
31 +++ b/fs/locks.c
32 @@ -2812,6 +2812,8 @@ static const struct file_operations proc
33  
34  static int __init proc_locks_init(void)
35  {
36 +       if (IS_ENABLED(CONFIG_PROC_STRIPPED))
37 +               return 0;
38         proc_create("locks", 0, NULL, &proc_locks_operations);
39         return 0;
40  }
41 --- a/fs/proc/Kconfig
42 +++ b/fs/proc/Kconfig
43 @@ -81,3 +81,8 @@ config PROC_CHILDREN
44  
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.
47 +
48 +config PROC_STRIPPED
49 +       default n
50 +       depends on EXPERT
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
55  
56  static int __init proc_consoles_init(void)
57  {
58 +       if (IS_ENABLED(CONFIG_PROC_STRIPPED))
59 +               return 0;
60 +
61         proc_create("consoles", 0, NULL, &proc_consoles_operations);
62         return 0;
63  }
64 --- a/fs/proc/proc_tty.c
65 +++ b/fs/proc/proc_tty.c
66 @@ -145,7 +145,10 @@ static const struct file_operations proc
67  void proc_tty_register_driver(struct tty_driver *driver)
68  {
69         struct proc_dir_entry *ent;
70 -               
71 +
72 +       if (IS_ENABLED(CONFIG_PROC_STRIPPED))
73 +               return;
74 +
75         if (!driver->driver_name || driver->proc_entry ||
76             !driver->ops->proc_fops)
77                 return;
78 @@ -162,6 +165,9 @@ void proc_tty_unregister_driver(struct t
79  {
80         struct proc_dir_entry *ent;
81  
82 +       if (IS_ENABLED(CONFIG_PROC_STRIPPED))
83 +               return;
84 +
85         ent = driver->proc_entry;
86         if (!ent)
87                 return;
88 @@ -176,6 +182,9 @@ void proc_tty_unregister_driver(struct t
89   */
90  void __init proc_tty_init(void)
91  {
92 +       if (IS_ENABLED(CONFIG_PROC_STRIPPED))
93 +               return;
94 +
95         if (!proc_mkdir("tty", NULL))
96                 return;
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
103  
104 +#ifdef CONFIG_PROC_STRIPPED
105 +#define __SNMP_STATS_DUMMY(mib)        \
106 +       do { (void) mib->mibs[0]; } while(0)
107 +
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)
116 +
117 +#else
118 +
119  #define __SNMP_INC_STATS(mib, field)   \
120                         __this_cpu_inc(mib->mibs[field])
121  
122 @@ -153,8 +168,9 @@ struct linux_xfrm_mib {
123                 __this_cpu_add(ptr[basefield##OCTETS], addend); \
124         } while (0)
125  
126 +#endif
127  
128 -#if BITS_PER_LONG==32
129 +#if (BITS_PER_LONG==32) && !defined(CONFIG_PROC_STRIPPED)
130  
131  #define __SNMP_ADD_STATS64(mib, field, addend)                                 \
132         do {                                                            \
133 --- a/ipc/msg.c
134 +++ b/ipc/msg.c
135 @@ -1208,6 +1208,9 @@ int __init msg_init(void)
136  {
137         const int err = msg_init_ns(&init_ipc_ns);
138  
139 +       if (IS_ENABLED(CONFIG_PROC_STRIPPED))
140 +               return err;
141 +
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);
145 --- a/ipc/sem.c
146 +++ b/ipc/sem.c
147 @@ -207,6 +207,8 @@ int __init sem_init(void)
148  {
149         const int err = sem_init_ns(&init_ipc_ns);
150  
151 +       if (IS_ENABLED(CONFIG_PROC_STRIPPED))
152 +               return err;
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);
156 --- a/ipc/shm.c
157 +++ b/ipc/shm.c
158 @@ -123,6 +123,8 @@ pure_initcall(ipc_ns_init);
159  
160  void __init shm_init(void)
161  {
162 +       if (IS_ENABLED(CONFIG_PROC_STRIPPED))
163 +               return;
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",
167 --- a/ipc/util.c
168 +++ b/ipc/util.c
169 @@ -141,6 +141,9 @@ void __init ipc_init_proc_interface(cons
170         struct proc_dir_entry *pde;
171         struct ipc_proc_iface *iface;
172  
173 +       if (IS_ENABLED(CONFIG_PROC_STRIPPED))
174 +               return;
175 +
176         iface = kmalloc(sizeof(*iface), GFP_KERNEL);
177         if (!iface)
178                 return;
179 --- a/kernel/exec_domain.c
180 +++ b/kernel/exec_domain.c
181 @@ -42,6 +42,8 @@ static const struct file_operations exec
182  
183  static int __init proc_execdomains_init(void)
184  {
185 +       if (IS_ENABLED(CONFIG_PROC_STRIPPED))
186 +               return 0;
187         proc_create("execdomains", 0, NULL, &execdomains_proc_fops);
188         return 0;
189  }
190 --- a/kernel/irq/proc.c
191 +++ b/kernel/irq/proc.c
192 @@ -418,6 +418,9 @@ void register_irq_proc(unsigned int irq,
193         void __maybe_unused *irqp = (void *)(unsigned long) irq;
194         char name [MAX_NAMELEN];
195  
196 +       if (IS_ENABLED(CONFIG_PROC_STRIPPED) && !IS_ENABLED(CONFIG_SMP))
197 +               return;
198 +
199         if (!root_irq_dir || (desc->irq_data.chip == &no_irq_chip))
200                 return;
201  
202 @@ -471,6 +474,9 @@ void unregister_irq_proc(unsigned int ir
203  {
204         char name [MAX_NAMELEN];
205  
206 +       if (IS_ENABLED(CONFIG_PROC_STRIPPED) && !IS_ENABLED(CONFIG_SMP))
207 +               return;
208 +
209         if (!root_irq_dir || !desc->dir)
210                 return;
211  #ifdef CONFIG_SMP
212 @@ -509,6 +515,9 @@ void init_irq_proc(void)
213         unsigned int irq;
214         struct irq_desc *desc;
215  
216 +       if (IS_ENABLED(CONFIG_PROC_STRIPPED) && !IS_ENABLED(CONFIG_SMP))
217 +               return;
218 +
219         /* create /proc/irq */
220         root_irq_dir = proc_mkdir("irq", NULL);
221         if (!root_irq_dir)
222 --- a/kernel/time/timer_list.c
223 +++ b/kernel/time/timer_list.c
224 @@ -390,6 +390,8 @@ static int __init init_timer_list_procfs
225  {
226         struct proc_dir_entry *pe;
227  
228 +       if (IS_ENABLED(CONFIG_PROC_STRIPPED))
229 +               return 0;
230         pe = proc_create("timer_list", 0400, NULL, &timer_list_fops);
231         if (!pe)
232                 return -ENOMEM;
233 --- a/mm/vmalloc.c
234 +++ b/mm/vmalloc.c
235 @@ -2798,6 +2798,8 @@ static const struct file_operations proc
236  
237  static int __init proc_vmalloc_init(void)
238  {
239 +       if (IS_ENABLED(CONFIG_PROC_STRIPPED))
240 +               return 0;
241         proc_create("vmallocinfo", S_IRUSR, NULL, &proc_vmalloc_operations);
242         return 0;
243  }
244 --- a/mm/vmstat.c
245 +++ b/mm/vmstat.c
246 @@ -1954,10 +1954,12 @@ void __init init_mm_internals(void)
247         start_shepherd_timer();
248  #endif
249  #ifdef CONFIG_PROC_FS
250 -       proc_create("buddyinfo", 0444, NULL, &buddyinfo_file_operations);
251 -       proc_create("pagetypeinfo", 0400, NULL, &pagetypeinfo_file_operations);
252 +       if (!IS_ENABLED(CONFIG_PROC_STRIPPED)) {
253 +               proc_create("buddyinfo", 0444, NULL, &buddyinfo_file_operations);
254 +               proc_create("pagetypeinfo", 0400, NULL, &pagetypeinfo_file_operations);
255 +               proc_create("zoneinfo", 0444, NULL, &zoneinfo_file_operations);
256 +       }
257         proc_create("vmstat", 0444, NULL, &vmstat_file_operations);
258 -       proc_create("zoneinfo", 0444, NULL, &zoneinfo_file_operations);
259  #endif
260  }
261  
262 --- a/net/8021q/vlanproc.c
263 +++ b/net/8021q/vlanproc.c
264 @@ -127,6 +127,9 @@ void vlan_proc_cleanup(struct net *net)
265  {
266         struct vlan_net *vn = net_generic(net, vlan_net_id);
267  
268 +       if (IS_ENABLED(CONFIG_PROC_STRIPPED))
269 +               return;
270 +
271         if (vn->proc_vlan_conf)
272                 remove_proc_entry(name_conf, vn->proc_vlan_dir);
273  
274 @@ -146,6 +149,9 @@ int __net_init vlan_proc_init(struct net
275  {
276         struct vlan_net *vn = net_generic(net, vlan_net_id);
277  
278 +       if (IS_ENABLED(CONFIG_PROC_STRIPPED))
279 +               return 0;
280 +
281         vn->proc_vlan_dir = proc_net_mkdir(net, name_root, net->proc_net);
282         if (!vn->proc_vlan_dir)
283                 goto err;
284 --- a/net/core/net-procfs.c
285 +++ b/net/core/net-procfs.c
286 @@ -350,10 +350,12 @@ static int __net_init dev_proc_net_init(
287  
288         if (!proc_create("dev", S_IRUGO, net->proc_net, &dev_seq_fops))
289                 goto out;
290 -       if (!proc_create("softnet_stat", S_IRUGO, net->proc_net,
291 +       if (!IS_ENABLED(CONFIG_PROC_STRIPPED) &&
292 +               !proc_create("softnet_stat", S_IRUGO, net->proc_net,
293                          &softnet_seq_fops))
294                 goto out_dev;
295 -       if (!proc_create("ptype", S_IRUGO, net->proc_net, &ptype_seq_fops))
296 +       if (!IS_ENABLED(CONFIG_PROC_STRIPPED) &&
297 +               !proc_create("ptype", S_IRUGO, net->proc_net, &ptype_seq_fops))
298                 goto out_softnet;
299  
300         if (wext_proc_init(net))
301 @@ -362,9 +364,11 @@ static int __net_init dev_proc_net_init(
302  out:
303         return rc;
304  out_ptype:
305 -       remove_proc_entry("ptype", net->proc_net);
306 +       if (!IS_ENABLED(CONFIG_PROC_STRIPPED))
307 +               remove_proc_entry("ptype", net->proc_net);
308  out_softnet:
309 -       remove_proc_entry("softnet_stat", net->proc_net);
310 +       if (!IS_ENABLED(CONFIG_PROC_STRIPPED))
311 +               remove_proc_entry("softnet_stat", net->proc_net);
312  out_dev:
313         remove_proc_entry("dev", net->proc_net);
314         goto out;
315 @@ -374,8 +378,10 @@ static void __net_exit dev_proc_net_exit
316  {
317         wext_proc_exit(net);
318  
319 -       remove_proc_entry("ptype", net->proc_net);
320 -       remove_proc_entry("softnet_stat", net->proc_net);
321 +       if (!IS_ENABLED(CONFIG_PROC_STRIPPED)) {
322 +               remove_proc_entry("ptype", net->proc_net);
323 +               remove_proc_entry("softnet_stat", net->proc_net);
324 +       }
325         remove_proc_entry("dev", net->proc_net);
326  }
327  
328 --- a/net/core/sock.c
329 +++ b/net/core/sock.c
330 @@ -3427,6 +3427,8 @@ static __net_initdata struct pernet_oper
331  
332  static int __init proto_init(void)
333  {
334 +       if (IS_ENABLED(CONFIG_PROC_STRIPPED))
335 +               return 0;
336         return register_pernet_subsys(&proto_net_ops);
337  }
338  
339 --- a/net/ipv4/fib_trie.c
340 +++ b/net/ipv4/fib_trie.c
341 @@ -2743,10 +2743,12 @@ static const struct file_operations fib_
342  
343  int __net_init fib_proc_init(struct net *net)
344  {
345 -       if (!proc_create("fib_trie", S_IRUGO, net->proc_net, &fib_trie_fops))
346 +       if (!IS_ENABLED(CONFIG_PROC_STRIPPED) &&
347 +               !proc_create("fib_trie", S_IRUGO, net->proc_net, &fib_trie_fops))
348                 goto out1;
349  
350 -       if (!proc_create("fib_triestat", S_IRUGO, net->proc_net,
351 +       if (!IS_ENABLED(CONFIG_PROC_STRIPPED) &&
352 +               !proc_create("fib_triestat", S_IRUGO, net->proc_net,
353                          &fib_triestat_fops))
354                 goto out2;
355  
356 @@ -2756,17 +2758,21 @@ int __net_init fib_proc_init(struct net
357         return 0;
358  
359  out3:
360 -       remove_proc_entry("fib_triestat", net->proc_net);
361 +       if (!IS_ENABLED(CONFIG_PROC_STRIPPED))
362 +               remove_proc_entry("fib_triestat", net->proc_net);
363  out2:
364 -       remove_proc_entry("fib_trie", net->proc_net);
365 +       if (!IS_ENABLED(CONFIG_PROC_STRIPPED))
366 +               remove_proc_entry("fib_trie", net->proc_net);
367  out1:
368         return -ENOMEM;
369  }
370  
371  void __net_exit fib_proc_exit(struct net *net)
372  {
373 -       remove_proc_entry("fib_trie", net->proc_net);
374 -       remove_proc_entry("fib_triestat", net->proc_net);
375 +       if (!IS_ENABLED(CONFIG_PROC_STRIPPED)) {
376 +               remove_proc_entry("fib_trie", net->proc_net);
377 +               remove_proc_entry("fib_triestat", net->proc_net);
378 +       }
379         remove_proc_entry("route", net->proc_net);
380  }
381  
382 --- a/net/ipv4/proc.c
383 +++ b/net/ipv4/proc.c
384 @@ -559,6 +559,9 @@ static __net_initdata struct pernet_oper
385  
386  int __init ip_misc_proc_init(void)
387  {
388 +       if (IS_ENABLED(CONFIG_PROC_STRIPPED))
389 +               return 0;
390 +
391         return register_pernet_subsys(&ip_proc_ops);
392  }
393  
394 --- a/net/ipv4/route.c
395 +++ b/net/ipv4/route.c
396 @@ -428,6 +428,9 @@ static struct pernet_operations ip_rt_pr
397  
398  static int __init ip_rt_proc_init(void)
399  {
400 +       if (IS_ENABLED(CONFIG_PROC_STRIPPED))
401 +               return 0;
402 +
403         return register_pernet_subsys(&ip_rt_proc_ops);
404  }
405