902-debloat_proc.patch 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. @@ -2974,6 +2974,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. @@ -120,6 +120,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. @@ -119,6 +119,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. @@ -149,8 +164,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. @@ -157,6 +157,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. @@ -331,6 +331,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. @@ -384,6 +387,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. @@ -422,6 +428,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. @@ -352,6 +352,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. @@ -5133,6 +5133,9 @@ static int vmalloc_info_show(struct seq_
  219. static int __init proc_vmalloc_init(void)
  220. {
  221. + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
  222. + return 0;
  223. +
  224. proc_create_single("vmallocinfo", 0400, NULL, vmalloc_info_show);
  225. return 0;
  226. }
  227. --- a/mm/vmstat.c
  228. +++ b/mm/vmstat.c
  229. @@ -2288,10 +2288,12 @@ void __init init_mm_internals(void)
  230. start_shepherd_timer();
  231. #endif
  232. #ifdef CONFIG_PROC_FS
  233. - proc_create_seq("buddyinfo", 0444, NULL, &fragmentation_op);
  234. - proc_create_seq("pagetypeinfo", 0400, NULL, &pagetypeinfo_op);
  235. + if (!IS_ENABLED(CONFIG_PROC_STRIPPED)) {
  236. + proc_create_seq("buddyinfo", 0444, NULL, &fragmentation_op);
  237. + proc_create_seq("pagetypeinfo", 0400, NULL, &pagetypeinfo_op);
  238. + proc_create_seq("zoneinfo", 0444, NULL, &zoneinfo_op);
  239. + }
  240. proc_create_seq("vmstat", 0444, NULL, &vmstat_op);
  241. - proc_create_seq("zoneinfo", 0444, NULL, &zoneinfo_op);
  242. register_sysctl_init("vm", vmstat_table);
  243. #endif
  244. }
  245. --- a/net/8021q/vlanproc.c
  246. +++ b/net/8021q/vlanproc.c
  247. @@ -93,6 +93,9 @@ void vlan_proc_cleanup(struct net *net)
  248. {
  249. struct vlan_net *vn = net_generic(net, vlan_net_id);
  250. + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
  251. + return;
  252. +
  253. if (vn->proc_vlan_conf)
  254. remove_proc_entry(name_conf, vn->proc_vlan_dir);
  255. @@ -112,6 +115,9 @@ int __net_init vlan_proc_init(struct net
  256. {
  257. struct vlan_net *vn = net_generic(net, vlan_net_id);
  258. + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
  259. + return 0;
  260. +
  261. vn->proc_vlan_dir = proc_net_mkdir(net, name_root, net->proc_net);
  262. if (!vn->proc_vlan_dir)
  263. goto err;
  264. --- a/net/core/net-procfs.c
  265. +++ b/net/core/net-procfs.c
  266. @@ -329,10 +329,12 @@ static int __net_init dev_proc_net_init(
  267. if (!proc_create_net("dev", 0444, net->proc_net, &dev_seq_ops,
  268. sizeof(struct seq_net_private)))
  269. goto out;
  270. - if (!proc_create_seq("softnet_stat", 0444, net->proc_net,
  271. + if (!IS_ENABLED(CONFIG_PROC_STRIPPED) &&
  272. + !proc_create_seq("softnet_stat", 0444, net->proc_net,
  273. &softnet_seq_ops))
  274. goto out_dev;
  275. - if (!proc_create_net("ptype", 0444, net->proc_net, &ptype_seq_ops,
  276. + if (!IS_ENABLED(CONFIG_PROC_STRIPPED) &&
  277. + !proc_create_net("ptype", 0444, net->proc_net, &ptype_seq_ops,
  278. sizeof(struct ptype_iter_state)))
  279. goto out_softnet;
  280. @@ -342,9 +344,11 @@ static int __net_init dev_proc_net_init(
  281. out:
  282. return rc;
  283. out_ptype:
  284. - remove_proc_entry("ptype", net->proc_net);
  285. + if (!IS_ENABLED(CONFIG_PROC_STRIPPED))
  286. + remove_proc_entry("ptype", net->proc_net);
  287. out_softnet:
  288. - remove_proc_entry("softnet_stat", net->proc_net);
  289. + if (!IS_ENABLED(CONFIG_PROC_STRIPPED))
  290. + remove_proc_entry("softnet_stat", net->proc_net);
  291. out_dev:
  292. remove_proc_entry("dev", net->proc_net);
  293. goto out;
  294. @@ -354,8 +358,10 @@ static void __net_exit dev_proc_net_exit
  295. {
  296. wext_proc_exit(net);
  297. - remove_proc_entry("ptype", net->proc_net);
  298. - remove_proc_entry("softnet_stat", net->proc_net);
  299. + if (!IS_ENABLED(CONFIG_PROC_STRIPPED)) {
  300. + remove_proc_entry("ptype", net->proc_net);
  301. + remove_proc_entry("softnet_stat", net->proc_net);
  302. + }
  303. remove_proc_entry("dev", net->proc_net);
  304. }
  305. --- a/net/core/sock.c
  306. +++ b/net/core/sock.c
  307. @@ -4349,6 +4349,8 @@ static __net_initdata struct pernet_oper
  308. static int __init proto_init(void)
  309. {
  310. + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
  311. + return 0;
  312. return register_pernet_subsys(&proto_net_ops);
  313. }
  314. --- a/net/ipv4/fib_trie.c
  315. +++ b/net/ipv4/fib_trie.c
  316. @@ -3010,11 +3010,13 @@ static const struct seq_operations fib_r
  317. int __net_init fib_proc_init(struct net *net)
  318. {
  319. - if (!proc_create_net("fib_trie", 0444, net->proc_net, &fib_trie_seq_ops,
  320. + if (!IS_ENABLED(CONFIG_PROC_STRIPPED) &&
  321. + !proc_create_net("fib_trie", 0444, net->proc_net, &fib_trie_seq_ops,
  322. sizeof(struct fib_trie_iter)))
  323. goto out1;
  324. - if (!proc_create_net_single("fib_triestat", 0444, net->proc_net,
  325. + if (!IS_ENABLED(CONFIG_PROC_STRIPPED) &&
  326. + !proc_create_net_single("fib_triestat", 0444, net->proc_net,
  327. fib_triestat_seq_show, NULL))
  328. goto out2;
  329. @@ -3025,17 +3027,21 @@ int __net_init fib_proc_init(struct net
  330. return 0;
  331. out3:
  332. - remove_proc_entry("fib_triestat", net->proc_net);
  333. + if (!IS_ENABLED(CONFIG_PROC_STRIPPED))
  334. + remove_proc_entry("fib_triestat", net->proc_net);
  335. out2:
  336. - remove_proc_entry("fib_trie", net->proc_net);
  337. + if (!IS_ENABLED(CONFIG_PROC_STRIPPED))
  338. + remove_proc_entry("fib_trie", net->proc_net);
  339. out1:
  340. return -ENOMEM;
  341. }
  342. void __net_exit fib_proc_exit(struct net *net)
  343. {
  344. - remove_proc_entry("fib_trie", net->proc_net);
  345. - remove_proc_entry("fib_triestat", net->proc_net);
  346. + if (!IS_ENABLED(CONFIG_PROC_STRIPPED)) {
  347. + remove_proc_entry("fib_trie", net->proc_net);
  348. + remove_proc_entry("fib_triestat", net->proc_net);
  349. + }
  350. remove_proc_entry("route", net->proc_net);
  351. }
  352. --- a/net/ipv4/proc.c
  353. +++ b/net/ipv4/proc.c
  354. @@ -568,5 +568,8 @@ static __net_initdata struct pernet_oper
  355. int __init ip_misc_proc_init(void)
  356. {
  357. + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
  358. + return 0;
  359. +
  360. return register_pernet_subsys(&ip_proc_ops);
  361. }
  362. --- a/net/ipv4/route.c
  363. +++ b/net/ipv4/route.c
  364. @@ -383,6 +383,9 @@ static struct pernet_operations ip_rt_pr
  365. static int __init ip_rt_proc_init(void)
  366. {
  367. + if (IS_ENABLED(CONFIG_PROC_STRIPPED))
  368. + return 0;
  369. +
  370. return register_pernet_subsys(&ip_rt_proc_ops);
  371. }
  372. --- a/net/ipv4/inet_timewait_sock.c
  373. +++ b/net/ipv4/inet_timewait_sock.c
  374. @@ -301,7 +301,7 @@ void __inet_twsk_schedule(struct inet_ti
  375. */
  376. if (!rearm) {
  377. - bool kill = timeo <= 4*HZ;
  378. + bool __maybe_unused kill = timeo <= 4*HZ;
  379. __NET_INC_STATS(twsk_net(tw), kill ? LINUX_MIB_TIMEWAITKILLED :
  380. LINUX_MIB_TIMEWAITED);