902-debloat_proc.patch 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. Signed-off-by: Felix Fietkau <nbd@nbd.name>
  6. ---
  7. fs/locks.c | 2 ++
  8. fs/proc/Kconfig | 5 +++++
  9. fs/proc/consoles.c | 3 +++
  10. fs/proc/proc_tty.c | 11 ++++++++++-
  11. include/net/snmp.h | 18 +++++++++++++++++-
  12. ipc/msg.c | 3 +++
  13. ipc/sem.c | 2 ++
  14. ipc/shm.c | 2 ++
  15. ipc/util.c | 3 +++
  16. kernel/exec_domain.c | 2 ++
  17. kernel/irq/proc.c | 9 +++++++++
  18. kernel/time/timer_list.c | 2 ++
  19. mm/vmalloc.c | 2 ++
  20. mm/vmstat.c | 8 +++++---
  21. net/8021q/vlanproc.c | 6 ++++++
  22. net/core/net-procfs.c | 18 ++++++++++++------
  23. net/core/sock.c | 2 ++
  24. net/ipv4/fib_trie.c | 18 ++++++++++++------
  25. net/ipv4/proc.c | 3 +++
  26. net/ipv4/route.c | 3 +++
  27. 20 files changed, 105 insertions(+), 17 deletions(-)
  28. --- a/fs/locks.c
  29. +++ b/fs/locks.c
  30. @@ -2812,6 +2812,8 @@ static const struct file_operations proc
  31. static int __init proc_locks_init(void)
  32. {
  33. + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
  34. + return 0;
  35. proc_create("locks", 0, NULL, &proc_locks_operations);
  36. return 0;
  37. }
  38. --- a/fs/proc/Kconfig
  39. +++ b/fs/proc/Kconfig
  40. @@ -81,3 +81,8 @@ config PROC_CHILDREN
  41. Say Y if you are running any user-space software which takes benefit from
  42. this interface. For example, rkt is such a piece of software.
  43. +
  44. +config PROC_STRIPPED
  45. + default n
  46. + depends on EXPERT
  47. + bool "Strip non-essential /proc functionality to reduce code size"
  48. --- a/fs/proc/consoles.c
  49. +++ b/fs/proc/consoles.c
  50. @@ -106,6 +106,9 @@ static const struct file_operations proc
  51. static int __init proc_consoles_init(void)
  52. {
  53. + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
  54. + return 0;
  55. +
  56. proc_create("consoles", 0, NULL, &proc_consoles_operations);
  57. return 0;
  58. }
  59. --- a/fs/proc/proc_tty.c
  60. +++ b/fs/proc/proc_tty.c
  61. @@ -145,7 +145,10 @@ static const struct file_operations proc
  62. void proc_tty_register_driver(struct tty_driver *driver)
  63. {
  64. struct proc_dir_entry *ent;
  65. -
  66. +
  67. + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
  68. + return;
  69. +
  70. if (!driver->driver_name || driver->proc_entry ||
  71. !driver->ops->proc_fops)
  72. return;
  73. @@ -162,6 +165,9 @@ void proc_tty_unregister_driver(struct t
  74. {
  75. struct proc_dir_entry *ent;
  76. + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
  77. + return;
  78. +
  79. ent = driver->proc_entry;
  80. if (!ent)
  81. return;
  82. @@ -176,6 +182,9 @@ void proc_tty_unregister_driver(struct t
  83. */
  84. void __init proc_tty_init(void)
  85. {
  86. + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
  87. + return;
  88. +
  89. if (!proc_mkdir("tty", NULL))
  90. return;
  91. proc_mkdir("tty/ldisc", NULL); /* Preserved: it's userspace visible */
  92. --- a/include/net/snmp.h
  93. +++ b/include/net/snmp.h
  94. @@ -123,6 +123,21 @@ struct linux_xfrm_mib {
  95. #define DECLARE_SNMP_STAT(type, name) \
  96. extern __typeof__(type) __percpu *name
  97. +#ifdef CONFIG_PROC_STRIPPED
  98. +#define __SNMP_STATS_DUMMY(mib) \
  99. + do { (void) mib->mibs[0]; } while(0)
  100. +
  101. +#define __SNMP_INC_STATS(mib, field) __SNMP_STATS_DUMMY(mib)
  102. +#define SNMP_INC_STATS_ATOMIC_LONG(mib, field) __SNMP_STATS_DUMMY(mib)
  103. +#define SNMP_INC_STATS(mib, field) __SNMP_STATS_DUMMY(mib)
  104. +#define SNMP_DEC_STATS(mib, field) __SNMP_STATS_DUMMY(mib)
  105. +#define __SNMP_ADD_STATS(mib, field, addend) __SNMP_STATS_DUMMY(mib)
  106. +#define SNMP_ADD_STATS(mib, field, addend) __SNMP_STATS_DUMMY(mib)
  107. +#define SNMP_UPD_PO_STATS(mib, basefield, addend) __SNMP_STATS_DUMMY(mib)
  108. +#define __SNMP_UPD_PO_STATS(mib, basefield, addend) __SNMP_STATS_DUMMY(mib)
  109. +
  110. +#else
  111. +
  112. #define __SNMP_INC_STATS(mib, field) \
  113. __this_cpu_inc(mib->mibs[field])
  114. @@ -153,8 +168,9 @@ struct linux_xfrm_mib {
  115. __this_cpu_add(ptr[basefield##OCTETS], addend); \
  116. } while (0)
  117. +#endif
  118. -#if BITS_PER_LONG==32
  119. +#if (BITS_PER_LONG==32) && !defined(CONFIG_PROC_STRIPPED)
  120. #define __SNMP_ADD_STATS64(mib, field, addend) \
  121. do { \
  122. --- a/ipc/msg.c
  123. +++ b/ipc/msg.c
  124. @@ -1208,6 +1208,9 @@ int __init msg_init(void)
  125. {
  126. const int err = msg_init_ns(&init_ipc_ns);
  127. + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
  128. + return err;
  129. +
  130. ipc_init_proc_interface("sysvipc/msg",
  131. " key msqid perms cbytes qnum lspid lrpid uid gid cuid cgid stime rtime ctime\n",
  132. IPC_MSG_IDS, sysvipc_msg_proc_show);
  133. --- a/ipc/sem.c
  134. +++ b/ipc/sem.c
  135. @@ -207,6 +207,8 @@ int __init sem_init(void)
  136. {
  137. const int err = sem_init_ns(&init_ipc_ns);
  138. + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
  139. + return err;
  140. ipc_init_proc_interface("sysvipc/sem",
  141. " key semid perms nsems uid gid cuid cgid otime ctime\n",
  142. IPC_SEM_IDS, sysvipc_sem_proc_show);
  143. --- a/ipc/shm.c
  144. +++ b/ipc/shm.c
  145. @@ -122,6 +122,8 @@ pure_initcall(ipc_ns_init);
  146. void __init shm_init(void)
  147. {
  148. + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
  149. + return;
  150. ipc_init_proc_interface("sysvipc/shm",
  151. #if BITS_PER_LONG <= 32
  152. " key shmid perms size cpid lpid nattch uid gid cuid cgid atime dtime ctime rss swap\n",
  153. --- a/ipc/util.c
  154. +++ b/ipc/util.c
  155. @@ -141,6 +141,9 @@ void __init ipc_init_proc_interface(cons
  156. struct proc_dir_entry *pde;
  157. struct ipc_proc_iface *iface;
  158. + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
  159. + return;
  160. +
  161. iface = kmalloc(sizeof(*iface), GFP_KERNEL);
  162. if (!iface)
  163. return;
  164. --- a/kernel/exec_domain.c
  165. +++ b/kernel/exec_domain.c
  166. @@ -42,6 +42,8 @@ static const struct file_operations exec
  167. static int __init proc_execdomains_init(void)
  168. {
  169. + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
  170. + return 0;
  171. proc_create("execdomains", 0, NULL, &execdomains_proc_fops);
  172. return 0;
  173. }
  174. --- a/kernel/irq/proc.c
  175. +++ b/kernel/irq/proc.c
  176. @@ -396,6 +396,9 @@ void register_irq_proc(unsigned int irq,
  177. void __maybe_unused *irqp = (void *)(unsigned long) irq;
  178. char name [MAX_NAMELEN];
  179. + if (IS_ENABLED(CONFIG_PROC_STRIPPED) && !IS_ENABLED(CONFIG_SMP))
  180. + return;
  181. +
  182. if (!root_irq_dir || (desc->irq_data.chip == &no_irq_chip))
  183. return;
  184. @@ -449,6 +452,9 @@ void unregister_irq_proc(unsigned int ir
  185. {
  186. char name [MAX_NAMELEN];
  187. + if (IS_ENABLED(CONFIG_PROC_STRIPPED) && !IS_ENABLED(CONFIG_SMP))
  188. + return;
  189. +
  190. if (!root_irq_dir || !desc->dir)
  191. return;
  192. #ifdef CONFIG_SMP
  193. @@ -487,6 +493,9 @@ void init_irq_proc(void)
  194. unsigned int irq;
  195. struct irq_desc *desc;
  196. + if (IS_ENABLED(CONFIG_PROC_STRIPPED) && !IS_ENABLED(CONFIG_SMP))
  197. + return;
  198. +
  199. /* create /proc/irq */
  200. root_irq_dir = proc_mkdir("irq", NULL);
  201. if (!root_irq_dir)
  202. --- a/kernel/time/timer_list.c
  203. +++ b/kernel/time/timer_list.c
  204. @@ -389,6 +389,8 @@ static int __init init_timer_list_procfs
  205. {
  206. struct proc_dir_entry *pe;
  207. + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
  208. + return 0;
  209. pe = proc_create("timer_list", 0444, NULL, &timer_list_fops);
  210. if (!pe)
  211. return -ENOMEM;
  212. --- a/mm/vmalloc.c
  213. +++ b/mm/vmalloc.c
  214. @@ -2770,6 +2770,8 @@ static const struct file_operations proc
  215. static int __init proc_vmalloc_init(void)
  216. {
  217. + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
  218. + return 0;
  219. proc_create("vmallocinfo", S_IRUSR, NULL, &proc_vmalloc_operations);
  220. return 0;
  221. }
  222. --- a/mm/vmstat.c
  223. +++ b/mm/vmstat.c
  224. @@ -1951,10 +1951,12 @@ void __init init_mm_internals(void)
  225. start_shepherd_timer();
  226. #endif
  227. #ifdef CONFIG_PROC_FS
  228. - proc_create("buddyinfo", 0444, NULL, &buddyinfo_file_operations);
  229. - proc_create("pagetypeinfo", 0444, NULL, &pagetypeinfo_file_operations);
  230. + if (!IS_ENABLED(CONFIG_PROC_STRIPPED)) {
  231. + proc_create("buddyinfo", 0444, NULL, &buddyinfo_file_operations);
  232. + proc_create("pagetypeinfo", 0444, NULL, &pagetypeinfo_file_operations);
  233. + proc_create("zoneinfo", 0444, NULL, &zoneinfo_file_operations);
  234. + }
  235. proc_create("vmstat", 0444, NULL, &vmstat_file_operations);
  236. - proc_create("zoneinfo", 0444, NULL, &zoneinfo_file_operations);
  237. #endif
  238. }
  239. --- a/net/8021q/vlanproc.c
  240. +++ b/net/8021q/vlanproc.c
  241. @@ -127,6 +127,9 @@ void vlan_proc_cleanup(struct net *net)
  242. {
  243. struct vlan_net *vn = net_generic(net, vlan_net_id);
  244. + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
  245. + return;
  246. +
  247. if (vn->proc_vlan_conf)
  248. remove_proc_entry(name_conf, vn->proc_vlan_dir);
  249. @@ -146,6 +149,9 @@ int __net_init vlan_proc_init(struct net
  250. {
  251. struct vlan_net *vn = net_generic(net, vlan_net_id);
  252. + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
  253. + return 0;
  254. +
  255. vn->proc_vlan_dir = proc_net_mkdir(net, name_root, net->proc_net);
  256. if (!vn->proc_vlan_dir)
  257. goto err;
  258. --- a/net/core/net-procfs.c
  259. +++ b/net/core/net-procfs.c
  260. @@ -320,10 +320,12 @@ static int __net_init dev_proc_net_init(
  261. if (!proc_create("dev", S_IRUGO, net->proc_net, &dev_seq_fops))
  262. goto out;
  263. - if (!proc_create("softnet_stat", S_IRUGO, net->proc_net,
  264. + if (!IS_ENABLED(CONFIG_PROC_STRIPPED) &&
  265. + !proc_create("softnet_stat", S_IRUGO, net->proc_net,
  266. &softnet_seq_fops))
  267. goto out_dev;
  268. - if (!proc_create("ptype", S_IRUGO, net->proc_net, &ptype_seq_fops))
  269. + if (!IS_ENABLED(CONFIG_PROC_STRIPPED) &&
  270. + !proc_create("ptype", S_IRUGO, net->proc_net, &ptype_seq_fops))
  271. goto out_softnet;
  272. if (wext_proc_init(net))
  273. @@ -332,9 +334,11 @@ static int __net_init dev_proc_net_init(
  274. out:
  275. return rc;
  276. out_ptype:
  277. - remove_proc_entry("ptype", net->proc_net);
  278. + if (!IS_ENABLED(CONFIG_PROC_STRIPPED))
  279. + remove_proc_entry("ptype", net->proc_net);
  280. out_softnet:
  281. - remove_proc_entry("softnet_stat", net->proc_net);
  282. + if (!IS_ENABLED(CONFIG_PROC_STRIPPED))
  283. + remove_proc_entry("softnet_stat", net->proc_net);
  284. out_dev:
  285. remove_proc_entry("dev", net->proc_net);
  286. goto out;
  287. @@ -344,8 +348,10 @@ static void __net_exit dev_proc_net_exit
  288. {
  289. wext_proc_exit(net);
  290. - remove_proc_entry("ptype", net->proc_net);
  291. - remove_proc_entry("softnet_stat", net->proc_net);
  292. + if (!IS_ENABLED(CONFIG_PROC_STRIPPED)) {
  293. + remove_proc_entry("ptype", net->proc_net);
  294. + remove_proc_entry("softnet_stat", net->proc_net);
  295. + }
  296. remove_proc_entry("dev", net->proc_net);
  297. }
  298. --- a/net/core/sock.c
  299. +++ b/net/core/sock.c
  300. @@ -3378,6 +3378,8 @@ static __net_initdata struct pernet_oper
  301. static int __init proto_init(void)
  302. {
  303. + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
  304. + return 0;
  305. return register_pernet_subsys(&proto_net_ops);
  306. }
  307. --- a/net/ipv4/fib_trie.c
  308. +++ b/net/ipv4/fib_trie.c
  309. @@ -2731,10 +2731,12 @@ static const struct file_operations fib_
  310. int __net_init fib_proc_init(struct net *net)
  311. {
  312. - if (!proc_create("fib_trie", S_IRUGO, net->proc_net, &fib_trie_fops))
  313. + if (!IS_ENABLED(CONFIG_PROC_STRIPPED) &&
  314. + !proc_create("fib_trie", S_IRUGO, net->proc_net, &fib_trie_fops))
  315. goto out1;
  316. - if (!proc_create("fib_triestat", S_IRUGO, net->proc_net,
  317. + if (!IS_ENABLED(CONFIG_PROC_STRIPPED) &&
  318. + !proc_create("fib_triestat", S_IRUGO, net->proc_net,
  319. &fib_triestat_fops))
  320. goto out2;
  321. @@ -2744,17 +2746,21 @@ int __net_init fib_proc_init(struct net
  322. return 0;
  323. out3:
  324. - remove_proc_entry("fib_triestat", net->proc_net);
  325. + if (!IS_ENABLED(CONFIG_PROC_STRIPPED))
  326. + remove_proc_entry("fib_triestat", net->proc_net);
  327. out2:
  328. - remove_proc_entry("fib_trie", net->proc_net);
  329. + if (!IS_ENABLED(CONFIG_PROC_STRIPPED))
  330. + remove_proc_entry("fib_trie", net->proc_net);
  331. out1:
  332. return -ENOMEM;
  333. }
  334. void __net_exit fib_proc_exit(struct net *net)
  335. {
  336. - remove_proc_entry("fib_trie", net->proc_net);
  337. - remove_proc_entry("fib_triestat", net->proc_net);
  338. + if (!IS_ENABLED(CONFIG_PROC_STRIPPED)) {
  339. + remove_proc_entry("fib_trie", net->proc_net);
  340. + remove_proc_entry("fib_triestat", net->proc_net);
  341. + }
  342. remove_proc_entry("route", net->proc_net);
  343. }
  344. --- a/net/ipv4/proc.c
  345. +++ b/net/ipv4/proc.c
  346. @@ -558,6 +558,9 @@ static __net_initdata struct pernet_oper
  347. int __init ip_misc_proc_init(void)
  348. {
  349. + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
  350. + return 0;
  351. +
  352. return register_pernet_subsys(&ip_proc_ops);
  353. }
  354. --- a/net/ipv4/route.c
  355. +++ b/net/ipv4/route.c
  356. @@ -427,6 +427,9 @@ static struct pernet_operations ip_rt_pr
  357. static int __init ip_rt_proc_init(void)
  358. {
  359. + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
  360. + return 0;
  361. +
  362. return register_pernet_subsys(&ip_rt_proc_ops);
  363. }