902-debloat_proc.patch 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. From 9e3f1d0805b2d919904dd9a4ff0d956314cc3cba Mon Sep 17 00:00:00 2001
  2. From: Felix Fietkau <[email protected]>
  3. Date: Sat, 8 Jul 2017 08:20:09 +0200
  4. Subject: debloat: procfs
  5. Signed-off-by: Felix Fietkau <[email protected]>
  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. @@ -2971,6 +2971,8 @@ static const struct seq_operations locks
  31. static int __init proc_locks_init(void)
  32. {
  33. + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
  34. + return 0;
  35. proc_create_seq_private("locks", 0, NULL, &locks_seq_operations,
  36. sizeof(struct locks_iterator), NULL);
  37. return 0;
  38. --- a/fs/proc/Kconfig
  39. +++ b/fs/proc/Kconfig
  40. @@ -101,6 +101,11 @@ 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. +config PROC_STRIPPED
  44. + default n
  45. + depends on EXPERT
  46. + bool "Strip non-essential /proc functionality to reduce code size"
  47. +
  48. config PROC_PID_ARCH_STATUS
  49. def_bool n
  50. depends on PROC_FS
  51. --- a/fs/proc/consoles.c
  52. +++ b/fs/proc/consoles.c
  53. @@ -110,6 +110,9 @@ static const struct seq_operations conso
  54. static int __init proc_consoles_init(void)
  55. {
  56. + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
  57. + return 0;
  58. +
  59. proc_create_seq("consoles", 0, NULL, &consoles_op);
  60. return 0;
  61. }
  62. --- a/fs/proc/proc_tty.c
  63. +++ b/fs/proc/proc_tty.c
  64. @@ -131,7 +131,10 @@ static const struct seq_operations tty_d
  65. void proc_tty_register_driver(struct tty_driver *driver)
  66. {
  67. struct proc_dir_entry *ent;
  68. -
  69. +
  70. + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
  71. + return;
  72. +
  73. if (!driver->driver_name || driver->proc_entry ||
  74. !driver->ops->proc_show)
  75. return;
  76. @@ -148,6 +151,9 @@ void proc_tty_unregister_driver(struct t
  77. {
  78. struct proc_dir_entry *ent;
  79. + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
  80. + return;
  81. +
  82. ent = driver->proc_entry;
  83. if (!ent)
  84. return;
  85. @@ -162,6 +168,9 @@ void proc_tty_unregister_driver(struct t
  86. */
  87. void __init proc_tty_init(void)
  88. {
  89. + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
  90. + return;
  91. +
  92. if (!proc_mkdir("tty", NULL))
  93. return;
  94. proc_mkdir("tty/ldisc", NULL); /* Preserved: it's userspace visible */
  95. --- a/include/net/snmp.h
  96. +++ b/include/net/snmp.h
  97. @@ -124,6 +124,21 @@ struct linux_tls_mib {
  98. #define DECLARE_SNMP_STAT(type, name) \
  99. extern __typeof__(type) __percpu *name
  100. +#ifdef CONFIG_PROC_STRIPPED
  101. +#define __SNMP_STATS_DUMMY(mib) \
  102. + do { (void) mib->mibs[0]; } while(0)
  103. +
  104. +#define __SNMP_INC_STATS(mib, field) __SNMP_STATS_DUMMY(mib)
  105. +#define SNMP_INC_STATS_ATOMIC_LONG(mib, field) __SNMP_STATS_DUMMY(mib)
  106. +#define SNMP_INC_STATS(mib, field) __SNMP_STATS_DUMMY(mib)
  107. +#define SNMP_DEC_STATS(mib, field) __SNMP_STATS_DUMMY(mib)
  108. +#define __SNMP_ADD_STATS(mib, field, addend) __SNMP_STATS_DUMMY(mib)
  109. +#define SNMP_ADD_STATS(mib, field, addend) __SNMP_STATS_DUMMY(mib)
  110. +#define SNMP_UPD_PO_STATS(mib, basefield, addend) __SNMP_STATS_DUMMY(mib)
  111. +#define __SNMP_UPD_PO_STATS(mib, basefield, addend) __SNMP_STATS_DUMMY(mib)
  112. +
  113. +#else
  114. +
  115. #define __SNMP_INC_STATS(mib, field) \
  116. __this_cpu_inc(mib->mibs[field])
  117. @@ -154,8 +169,9 @@ struct linux_tls_mib {
  118. __this_cpu_add(ptr[basefield##OCTETS], addend); \
  119. } while (0)
  120. +#endif
  121. -#if BITS_PER_LONG==32
  122. +#if (BITS_PER_LONG==32) && !defined(CONFIG_PROC_STRIPPED)
  123. #define __SNMP_ADD_STATS64(mib, field, addend) \
  124. do { \
  125. --- a/ipc/msg.c
  126. +++ b/ipc/msg.c
  127. @@ -1370,6 +1370,9 @@ void __init msg_init(void)
  128. {
  129. msg_init_ns(&init_ipc_ns);
  130. + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
  131. + return;
  132. +
  133. ipc_init_proc_interface("sysvipc/msg",
  134. " key msqid perms cbytes qnum lspid lrpid uid gid cuid cgid stime rtime ctime\n",
  135. IPC_MSG_IDS, sysvipc_msg_proc_show);
  136. --- a/ipc/sem.c
  137. +++ b/ipc/sem.c
  138. @@ -268,6 +268,8 @@ void sem_exit_ns(struct ipc_namespace *n
  139. void __init sem_init(void)
  140. {
  141. sem_init_ns(&init_ipc_ns);
  142. + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
  143. + return;
  144. ipc_init_proc_interface("sysvipc/sem",
  145. " key semid perms nsems uid gid cuid cgid otime ctime\n",
  146. IPC_SEM_IDS, sysvipc_sem_proc_show);
  147. --- a/ipc/shm.c
  148. +++ b/ipc/shm.c
  149. @@ -155,6 +155,8 @@ pure_initcall(ipc_ns_init);
  150. void __init shm_init(void)
  151. {
  152. + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
  153. + return;
  154. ipc_init_proc_interface("sysvipc/shm",
  155. #if BITS_PER_LONG <= 32
  156. " key shmid perms size cpid lpid nattch uid gid cuid cgid atime dtime ctime rss swap\n",
  157. --- a/ipc/util.c
  158. +++ b/ipc/util.c
  159. @@ -141,6 +141,9 @@ void __init ipc_init_proc_interface(cons
  160. struct proc_dir_entry *pde;
  161. struct ipc_proc_iface *iface;
  162. + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
  163. + return;
  164. +
  165. iface = kmalloc(sizeof(*iface), GFP_KERNEL);
  166. if (!iface)
  167. return;
  168. --- a/kernel/exec_domain.c
  169. +++ b/kernel/exec_domain.c
  170. @@ -29,6 +29,8 @@ static int execdomains_proc_show(struct
  171. static int __init proc_execdomains_init(void)
  172. {
  173. + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
  174. + return 0;
  175. proc_create_single("execdomains", 0, NULL, execdomains_proc_show);
  176. return 0;
  177. }
  178. --- a/kernel/irq/proc.c
  179. +++ b/kernel/irq/proc.c
  180. @@ -339,6 +339,9 @@ void register_irq_proc(unsigned int irq,
  181. void __maybe_unused *irqp = (void *)(unsigned long) irq;
  182. char name [MAX_NAMELEN];
  183. + if (IS_ENABLED(CONFIG_PROC_STRIPPED) && !IS_ENABLED(CONFIG_SMP))
  184. + return;
  185. +
  186. if (!root_irq_dir || (desc->irq_data.chip == &no_irq_chip))
  187. return;
  188. @@ -397,6 +400,9 @@ void unregister_irq_proc(unsigned int ir
  189. {
  190. char name [MAX_NAMELEN];
  191. + if (IS_ENABLED(CONFIG_PROC_STRIPPED) && !IS_ENABLED(CONFIG_SMP))
  192. + return;
  193. +
  194. if (!root_irq_dir || !desc->dir)
  195. return;
  196. #ifdef CONFIG_SMP
  197. @@ -435,6 +441,9 @@ void init_irq_proc(void)
  198. unsigned int irq;
  199. struct irq_desc *desc;
  200. + if (IS_ENABLED(CONFIG_PROC_STRIPPED) && !IS_ENABLED(CONFIG_SMP))
  201. + return;
  202. +
  203. /* create /proc/irq */
  204. root_irq_dir = proc_mkdir("irq", NULL);
  205. if (!root_irq_dir)
  206. --- a/kernel/time/timer_list.c
  207. +++ b/kernel/time/timer_list.c
  208. @@ -354,6 +354,8 @@ static int __init init_timer_list_procfs
  209. {
  210. struct proc_dir_entry *pe;
  211. + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
  212. + return 0;
  213. pe = proc_create_seq_private("timer_list", 0400, NULL, &timer_list_sops,
  214. sizeof(struct timer_list_iter), NULL);
  215. if (!pe)
  216. --- a/mm/vmalloc.c
  217. +++ b/mm/vmalloc.c
  218. @@ -5034,6 +5034,8 @@ static int __init proc_vmalloc_init(void
  219. {
  220. void *priv_data = NULL;
  221. + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
  222. + return 0;
  223. if (IS_ENABLED(CONFIG_NUMA))
  224. priv_data = kmalloc(nr_node_ids * sizeof(unsigned int), GFP_KERNEL);
  225. --- a/mm/vmstat.c
  226. +++ b/mm/vmstat.c
  227. @@ -2195,10 +2195,12 @@ void __init init_mm_internals(void)
  228. start_shepherd_timer();
  229. #endif
  230. #ifdef CONFIG_PROC_FS
  231. - proc_create_seq("buddyinfo", 0444, NULL, &fragmentation_op);
  232. - proc_create_seq("pagetypeinfo", 0400, NULL, &pagetypeinfo_op);
  233. + if (!IS_ENABLED(CONFIG_PROC_STRIPPED)) {
  234. + proc_create_seq("buddyinfo", 0444, NULL, &fragmentation_op);
  235. + proc_create_seq("pagetypeinfo", 0400, NULL, &pagetypeinfo_op);
  236. + proc_create_seq("zoneinfo", 0444, NULL, &zoneinfo_op);
  237. + }
  238. proc_create_seq("vmstat", 0444, NULL, &vmstat_op);
  239. - proc_create_seq("zoneinfo", 0444, NULL, &zoneinfo_op);
  240. #endif
  241. }
  242. --- a/net/8021q/vlanproc.c
  243. +++ b/net/8021q/vlanproc.c
  244. @@ -93,6 +93,9 @@ void vlan_proc_cleanup(struct net *net)
  245. {
  246. struct vlan_net *vn = net_generic(net, vlan_net_id);
  247. + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
  248. + return;
  249. +
  250. if (vn->proc_vlan_conf)
  251. remove_proc_entry(name_conf, vn->proc_vlan_dir);
  252. @@ -112,6 +115,9 @@ int __net_init vlan_proc_init(struct net
  253. {
  254. struct vlan_net *vn = net_generic(net, vlan_net_id);
  255. + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
  256. + return 0;
  257. +
  258. vn->proc_vlan_dir = proc_net_mkdir(net, name_root, net->proc_net);
  259. if (!vn->proc_vlan_dir)
  260. goto err;
  261. --- a/net/core/net-procfs.c
  262. +++ b/net/core/net-procfs.c
  263. @@ -295,10 +295,12 @@ static int __net_init dev_proc_net_init(
  264. if (!proc_create_net("dev", 0444, net->proc_net, &dev_seq_ops,
  265. sizeof(struct seq_net_private)))
  266. goto out;
  267. - if (!proc_create_seq("softnet_stat", 0444, net->proc_net,
  268. + if (!IS_ENABLED(CONFIG_PROC_STRIPPED) &&
  269. + !proc_create_seq("softnet_stat", 0444, net->proc_net,
  270. &softnet_seq_ops))
  271. goto out_dev;
  272. - if (!proc_create_net("ptype", 0444, net->proc_net, &ptype_seq_ops,
  273. + if (!IS_ENABLED(CONFIG_PROC_STRIPPED) &&
  274. + !proc_create_net("ptype", 0444, net->proc_net, &ptype_seq_ops,
  275. sizeof(struct seq_net_private)))
  276. goto out_softnet;
  277. @@ -308,9 +310,11 @@ static int __net_init dev_proc_net_init(
  278. out:
  279. return rc;
  280. out_ptype:
  281. - remove_proc_entry("ptype", net->proc_net);
  282. + if (!IS_ENABLED(CONFIG_PROC_STRIPPED))
  283. + remove_proc_entry("ptype", net->proc_net);
  284. out_softnet:
  285. - remove_proc_entry("softnet_stat", net->proc_net);
  286. + if (!IS_ENABLED(CONFIG_PROC_STRIPPED))
  287. + remove_proc_entry("softnet_stat", net->proc_net);
  288. out_dev:
  289. remove_proc_entry("dev", net->proc_net);
  290. goto out;
  291. @@ -320,8 +324,10 @@ static void __net_exit dev_proc_net_exit
  292. {
  293. wext_proc_exit(net);
  294. - remove_proc_entry("ptype", net->proc_net);
  295. - remove_proc_entry("softnet_stat", net->proc_net);
  296. + if (!IS_ENABLED(CONFIG_PROC_STRIPPED)) {
  297. + remove_proc_entry("ptype", net->proc_net);
  298. + remove_proc_entry("softnet_stat", net->proc_net);
  299. + }
  300. remove_proc_entry("dev", net->proc_net);
  301. }
  302. --- a/net/core/sock.c
  303. +++ b/net/core/sock.c
  304. @@ -4256,6 +4256,8 @@ static __net_initdata struct pernet_oper
  305. static int __init proto_init(void)
  306. {
  307. + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
  308. + return 0;
  309. return register_pernet_subsys(&proto_net_ops);
  310. }
  311. --- a/net/ipv4/fib_trie.c
  312. +++ b/net/ipv4/fib_trie.c
  313. @@ -3037,11 +3037,13 @@ static const struct seq_operations fib_r
  314. int __net_init fib_proc_init(struct net *net)
  315. {
  316. - if (!proc_create_net("fib_trie", 0444, net->proc_net, &fib_trie_seq_ops,
  317. + if (!IS_ENABLED(CONFIG_PROC_STRIPPED) &&
  318. + !proc_create_net("fib_trie", 0444, net->proc_net, &fib_trie_seq_ops,
  319. sizeof(struct fib_trie_iter)))
  320. goto out1;
  321. - if (!proc_create_net_single("fib_triestat", 0444, net->proc_net,
  322. + if (!IS_ENABLED(CONFIG_PROC_STRIPPED) &&
  323. + !proc_create_net_single("fib_triestat", 0444, net->proc_net,
  324. fib_triestat_seq_show, NULL))
  325. goto out2;
  326. @@ -3052,17 +3054,21 @@ int __net_init fib_proc_init(struct net
  327. return 0;
  328. out3:
  329. - remove_proc_entry("fib_triestat", net->proc_net);
  330. + if (!IS_ENABLED(CONFIG_PROC_STRIPPED))
  331. + remove_proc_entry("fib_triestat", net->proc_net);
  332. out2:
  333. - remove_proc_entry("fib_trie", net->proc_net);
  334. + if (!IS_ENABLED(CONFIG_PROC_STRIPPED))
  335. + remove_proc_entry("fib_trie", net->proc_net);
  336. out1:
  337. return -ENOMEM;
  338. }
  339. void __net_exit fib_proc_exit(struct net *net)
  340. {
  341. - remove_proc_entry("fib_trie", net->proc_net);
  342. - remove_proc_entry("fib_triestat", net->proc_net);
  343. + if (!IS_ENABLED(CONFIG_PROC_STRIPPED)) {
  344. + remove_proc_entry("fib_trie", net->proc_net);
  345. + remove_proc_entry("fib_triestat", net->proc_net);
  346. + }
  347. remove_proc_entry("route", net->proc_net);
  348. }
  349. --- a/net/ipv4/proc.c
  350. +++ b/net/ipv4/proc.c
  351. @@ -563,5 +563,8 @@ static __net_initdata struct pernet_oper
  352. int __init ip_misc_proc_init(void)
  353. {
  354. + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
  355. + return 0;
  356. +
  357. return register_pernet_subsys(&ip_proc_ops);
  358. }
  359. --- a/net/ipv4/route.c
  360. +++ b/net/ipv4/route.c
  361. @@ -378,6 +378,9 @@ static struct pernet_operations ip_rt_pr
  362. static int __init ip_rt_proc_init(void)
  363. {
  364. + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
  365. + return 0;
  366. +
  367. return register_pernet_subsys(&ip_rt_proc_ops);
  368. }
  369. --- a/net/ipv4/inet_timewait_sock.c
  370. +++ b/net/ipv4/inet_timewait_sock.c
  371. @@ -296,7 +296,7 @@ void __inet_twsk_schedule(struct inet_ti
  372. */
  373. if (!rearm) {
  374. - bool kill = timeo <= 4*HZ;
  375. + bool __maybe_unused kill = timeo <= 4*HZ;
  376. __NET_INC_STATS(twsk_net(tw), kill ? LINUX_MIB_TIMEWAITKILLED :
  377. LINUX_MIB_TIMEWAITED);