array.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/proc/array.c
  4. *
  5. * Copyright (C) 1992 by Linus Torvalds
  6. * based on ideas by Darren Senn
  7. *
  8. * Fixes:
  9. * Michael. K. Johnson: stat,statm extensions.
  10. * <johnsonm@stolaf.edu>
  11. *
  12. * Pauline Middelink : Made cmdline,envline only break at '\0's, to
  13. * make sure SET_PROCTITLE works. Also removed
  14. * bad '!' which forced address recalculation for
  15. * EVERY character on the current page.
  16. * <middelin@polyware.iaf.nl>
  17. *
  18. * Danny ter Haar : added cpuinfo
  19. * <dth@cistron.nl>
  20. *
  21. * Alessandro Rubini : profile extension.
  22. * <rubini@ipvvis.unipv.it>
  23. *
  24. * Jeff Tranter : added BogoMips field to cpuinfo
  25. * <Jeff_Tranter@Mitel.COM>
  26. *
  27. * Bruno Haible : remove 4K limit for the maps file
  28. * <haible@ma2s2.mathematik.uni-karlsruhe.de>
  29. *
  30. * Yves Arrouye : remove removal of trailing spaces in get_array.
  31. * <Yves.Arrouye@marin.fdn.fr>
  32. *
  33. * Jerome Forissier : added per-CPU time information to /proc/stat
  34. * and /proc/<pid>/cpu extension
  35. * <forissier@isia.cma.fr>
  36. * - Incorporation and non-SMP safe operation
  37. * of forissier patch in 2.1.78 by
  38. * Hans Marcus <crowbar@concepts.nl>
  39. *
  40. * aeb@cwi.nl : /proc/partitions
  41. *
  42. *
  43. * Alan Cox : security fixes.
  44. * <alan@lxorguk.ukuu.org.uk>
  45. *
  46. * Al Viro : safe handling of mm_struct
  47. *
  48. * Gerhard Wichert : added BIGMEM support
  49. * Siemens AG <Gerhard.Wichert@pdb.siemens.de>
  50. *
  51. * Al Viro & Jeff Garzik : moved most of the thing into base.c and
  52. * : proc_misc.c. The rest may eventually go into
  53. * : base.c too.
  54. */
  55. #include <linux/types.h>
  56. #include <linux/errno.h>
  57. #include <linux/time.h>
  58. #include <linux/time_namespace.h>
  59. #include <linux/kernel.h>
  60. #include <linux/kernel_stat.h>
  61. #include <linux/tty.h>
  62. #include <linux/string.h>
  63. #include <linux/mman.h>
  64. #include <linux/sched/mm.h>
  65. #include <linux/sched/numa_balancing.h>
  66. #include <linux/sched/task_stack.h>
  67. #include <linux/sched/task.h>
  68. #include <linux/sched/cputime.h>
  69. #include <linux/proc_fs.h>
  70. #include <linux/ioport.h>
  71. #include <linux/io.h>
  72. #include <linux/mm.h>
  73. #include <linux/hugetlb.h>
  74. #include <linux/pagemap.h>
  75. #include <linux/swap.h>
  76. #include <linux/smp.h>
  77. #include <linux/signal.h>
  78. #include <linux/highmem.h>
  79. #include <linux/file.h>
  80. #include <linux/fdtable.h>
  81. #include <linux/times.h>
  82. #include <linux/cpuset.h>
  83. #include <linux/rcupdate.h>
  84. #include <linux/delayacct.h>
  85. #include <linux/seq_file.h>
  86. #include <linux/pid_namespace.h>
  87. #include <linux/prctl.h>
  88. #include <linux/ptrace.h>
  89. #include <linux/string_helpers.h>
  90. #include <linux/user_namespace.h>
  91. #include <linux/fs_struct.h>
  92. #include <linux/kthread.h>
  93. #include <linux/mmu_context.h>
  94. #include <asm/processor.h>
  95. #include "internal.h"
  96. void proc_task_name(struct seq_file *m, struct task_struct *p, bool escape)
  97. {
  98. char tcomm[64];
  99. /*
  100. * Test before PF_KTHREAD because all workqueue worker threads are
  101. * kernel threads.
  102. */
  103. if (p->flags & PF_WQ_WORKER)
  104. wq_worker_comm(tcomm, sizeof(tcomm), p);
  105. else if (p->flags & PF_KTHREAD)
  106. get_kthread_comm(tcomm, sizeof(tcomm), p);
  107. else
  108. __get_task_comm(tcomm, sizeof(tcomm), p);
  109. if (escape)
  110. seq_escape_str(m, tcomm, ESCAPE_SPACE | ESCAPE_SPECIAL, "\n\\");
  111. else
  112. seq_printf(m, "%.64s", tcomm);
  113. }
  114. /*
  115. * The task state array is a strange "bitmap" of
  116. * reasons to sleep. Thus "running" is zero, and
  117. * you can test for combinations of others with
  118. * simple bit tests.
  119. */
  120. static const char * const task_state_array[] = {
  121. /* states in TASK_REPORT: */
  122. "R (running)", /* 0x00 */
  123. "S (sleeping)", /* 0x01 */
  124. "D (disk sleep)", /* 0x02 */
  125. "T (stopped)", /* 0x04 */
  126. "t (tracing stop)", /* 0x08 */
  127. "X (dead)", /* 0x10 */
  128. "Z (zombie)", /* 0x20 */
  129. "P (parked)", /* 0x40 */
  130. /* states beyond TASK_REPORT: */
  131. "I (idle)", /* 0x80 */
  132. };
  133. static inline const char *get_task_state(struct task_struct *tsk)
  134. {
  135. BUILD_BUG_ON(1 + ilog2(TASK_REPORT_MAX) != ARRAY_SIZE(task_state_array));
  136. return task_state_array[task_state_index(tsk)];
  137. }
  138. static inline void task_state(struct seq_file *m, struct pid_namespace *ns,
  139. struct pid *pid, struct task_struct *p)
  140. {
  141. struct user_namespace *user_ns = seq_user_ns(m);
  142. struct group_info *group_info;
  143. int g, umask = -1;
  144. struct task_struct *tracer;
  145. const struct cred *cred;
  146. pid_t ppid, tpid = 0, tgid, ngid;
  147. unsigned int max_fds = 0;
  148. rcu_read_lock();
  149. ppid = pid_alive(p) ?
  150. task_tgid_nr_ns(rcu_dereference(p->real_parent), ns) : 0;
  151. tracer = ptrace_parent(p);
  152. if (tracer)
  153. tpid = task_pid_nr_ns(tracer, ns);
  154. tgid = task_tgid_nr_ns(p, ns);
  155. ngid = task_numa_group_id(p);
  156. cred = get_task_cred(p);
  157. task_lock(p);
  158. if (p->fs)
  159. umask = p->fs->umask;
  160. if (p->files)
  161. max_fds = files_fdtable(p->files)->max_fds;
  162. task_unlock(p);
  163. rcu_read_unlock();
  164. if (umask >= 0)
  165. seq_printf(m, "Umask:\t%#04o\n", umask);
  166. seq_puts(m, "State:\t");
  167. seq_puts(m, get_task_state(p));
  168. seq_put_decimal_ull(m, "\nTgid:\t", tgid);
  169. seq_put_decimal_ull(m, "\nNgid:\t", ngid);
  170. seq_put_decimal_ull(m, "\nPid:\t", pid_nr_ns(pid, ns));
  171. seq_put_decimal_ull(m, "\nPPid:\t", ppid);
  172. seq_put_decimal_ull(m, "\nTracerPid:\t", tpid);
  173. seq_put_decimal_ull(m, "\nUid:\t", from_kuid_munged(user_ns, cred->uid));
  174. seq_put_decimal_ull(m, "\t", from_kuid_munged(user_ns, cred->euid));
  175. seq_put_decimal_ull(m, "\t", from_kuid_munged(user_ns, cred->suid));
  176. seq_put_decimal_ull(m, "\t", from_kuid_munged(user_ns, cred->fsuid));
  177. seq_put_decimal_ull(m, "\nGid:\t", from_kgid_munged(user_ns, cred->gid));
  178. seq_put_decimal_ull(m, "\t", from_kgid_munged(user_ns, cred->egid));
  179. seq_put_decimal_ull(m, "\t", from_kgid_munged(user_ns, cred->sgid));
  180. seq_put_decimal_ull(m, "\t", from_kgid_munged(user_ns, cred->fsgid));
  181. seq_put_decimal_ull(m, "\nFDSize:\t", max_fds);
  182. seq_puts(m, "\nGroups:\t");
  183. group_info = cred->group_info;
  184. for (g = 0; g < group_info->ngroups; g++)
  185. seq_put_decimal_ull(m, g ? " " : "",
  186. from_kgid_munged(user_ns, group_info->gid[g]));
  187. put_cred(cred);
  188. /* Trailing space shouldn't have been added in the first place. */
  189. seq_putc(m, ' ');
  190. #ifdef CONFIG_PID_NS
  191. seq_puts(m, "\nNStgid:");
  192. for (g = ns->level; g <= pid->level; g++)
  193. seq_put_decimal_ull(m, "\t", task_tgid_nr_ns(p, pid->numbers[g].ns));
  194. seq_puts(m, "\nNSpid:");
  195. for (g = ns->level; g <= pid->level; g++)
  196. seq_put_decimal_ull(m, "\t", task_pid_nr_ns(p, pid->numbers[g].ns));
  197. seq_puts(m, "\nNSpgid:");
  198. for (g = ns->level; g <= pid->level; g++)
  199. seq_put_decimal_ull(m, "\t", task_pgrp_nr_ns(p, pid->numbers[g].ns));
  200. seq_puts(m, "\nNSsid:");
  201. for (g = ns->level; g <= pid->level; g++)
  202. seq_put_decimal_ull(m, "\t", task_session_nr_ns(p, pid->numbers[g].ns));
  203. #endif
  204. seq_putc(m, '\n');
  205. seq_printf(m, "Kthread:\t%c\n", p->flags & PF_KTHREAD ? '1' : '0');
  206. }
  207. void render_sigset_t(struct seq_file *m, const char *header,
  208. sigset_t *set)
  209. {
  210. int i;
  211. seq_puts(m, header);
  212. i = _NSIG;
  213. do {
  214. int x = 0;
  215. i -= 4;
  216. if (sigismember(set, i+1)) x |= 1;
  217. if (sigismember(set, i+2)) x |= 2;
  218. if (sigismember(set, i+3)) x |= 4;
  219. if (sigismember(set, i+4)) x |= 8;
  220. seq_putc(m, hex_asc[x]);
  221. } while (i >= 4);
  222. seq_putc(m, '\n');
  223. }
  224. static void collect_sigign_sigcatch(struct task_struct *p, sigset_t *sigign,
  225. sigset_t *sigcatch)
  226. {
  227. struct k_sigaction *k;
  228. int i;
  229. k = p->sighand->action;
  230. for (i = 1; i <= _NSIG; ++i, ++k) {
  231. if (k->sa.sa_handler == SIG_IGN)
  232. sigaddset(sigign, i);
  233. else if (k->sa.sa_handler != SIG_DFL)
  234. sigaddset(sigcatch, i);
  235. }
  236. }
  237. static inline void task_sig(struct seq_file *m, struct task_struct *p)
  238. {
  239. unsigned long flags;
  240. sigset_t pending, shpending, blocked, ignored, caught;
  241. int num_threads = 0;
  242. unsigned int qsize = 0;
  243. unsigned long qlim = 0;
  244. sigemptyset(&pending);
  245. sigemptyset(&shpending);
  246. sigemptyset(&blocked);
  247. sigemptyset(&ignored);
  248. sigemptyset(&caught);
  249. if (lock_task_sighand(p, &flags)) {
  250. pending = p->pending.signal;
  251. shpending = p->signal->shared_pending.signal;
  252. blocked = p->blocked;
  253. collect_sigign_sigcatch(p, &ignored, &caught);
  254. num_threads = get_nr_threads(p);
  255. rcu_read_lock(); /* FIXME: is this correct? */
  256. qsize = get_rlimit_value(task_ucounts(p), UCOUNT_RLIMIT_SIGPENDING);
  257. rcu_read_unlock();
  258. qlim = task_rlimit(p, RLIMIT_SIGPENDING);
  259. unlock_task_sighand(p, &flags);
  260. }
  261. seq_put_decimal_ull(m, "Threads:\t", num_threads);
  262. seq_put_decimal_ull(m, "\nSigQ:\t", qsize);
  263. seq_put_decimal_ull(m, "/", qlim);
  264. /* render them all */
  265. render_sigset_t(m, "\nSigPnd:\t", &pending);
  266. render_sigset_t(m, "ShdPnd:\t", &shpending);
  267. render_sigset_t(m, "SigBlk:\t", &blocked);
  268. render_sigset_t(m, "SigIgn:\t", &ignored);
  269. render_sigset_t(m, "SigCgt:\t", &caught);
  270. }
  271. static void render_cap_t(struct seq_file *m, const char *header,
  272. kernel_cap_t *a)
  273. {
  274. seq_puts(m, header);
  275. seq_put_hex_ll(m, NULL, a->val, 16);
  276. seq_putc(m, '\n');
  277. }
  278. static inline void task_cap(struct seq_file *m, struct task_struct *p)
  279. {
  280. const struct cred *cred;
  281. kernel_cap_t cap_inheritable, cap_permitted, cap_effective,
  282. cap_bset, cap_ambient;
  283. rcu_read_lock();
  284. cred = __task_cred(p);
  285. cap_inheritable = cred->cap_inheritable;
  286. cap_permitted = cred->cap_permitted;
  287. cap_effective = cred->cap_effective;
  288. cap_bset = cred->cap_bset;
  289. cap_ambient = cred->cap_ambient;
  290. rcu_read_unlock();
  291. render_cap_t(m, "CapInh:\t", &cap_inheritable);
  292. render_cap_t(m, "CapPrm:\t", &cap_permitted);
  293. render_cap_t(m, "CapEff:\t", &cap_effective);
  294. render_cap_t(m, "CapBnd:\t", &cap_bset);
  295. render_cap_t(m, "CapAmb:\t", &cap_ambient);
  296. }
  297. static inline void task_seccomp(struct seq_file *m, struct task_struct *p)
  298. {
  299. seq_put_decimal_ull(m, "NoNewPrivs:\t", task_no_new_privs(p));
  300. #ifdef CONFIG_SECCOMP
  301. seq_put_decimal_ull(m, "\nSeccomp:\t", p->seccomp.mode);
  302. #ifdef CONFIG_SECCOMP_FILTER
  303. seq_put_decimal_ull(m, "\nSeccomp_filters:\t",
  304. atomic_read(&p->seccomp.filter_count));
  305. #endif
  306. #endif
  307. seq_puts(m, "\nSpeculation_Store_Bypass:\t");
  308. switch (arch_prctl_spec_ctrl_get(p, PR_SPEC_STORE_BYPASS)) {
  309. case -EINVAL:
  310. seq_puts(m, "unknown");
  311. break;
  312. case PR_SPEC_NOT_AFFECTED:
  313. seq_puts(m, "not vulnerable");
  314. break;
  315. case PR_SPEC_PRCTL | PR_SPEC_FORCE_DISABLE:
  316. seq_puts(m, "thread force mitigated");
  317. break;
  318. case PR_SPEC_PRCTL | PR_SPEC_DISABLE:
  319. seq_puts(m, "thread mitigated");
  320. break;
  321. case PR_SPEC_PRCTL | PR_SPEC_ENABLE:
  322. seq_puts(m, "thread vulnerable");
  323. break;
  324. case PR_SPEC_DISABLE:
  325. seq_puts(m, "globally mitigated");
  326. break;
  327. default:
  328. seq_puts(m, "vulnerable");
  329. break;
  330. }
  331. seq_puts(m, "\nSpeculationIndirectBranch:\t");
  332. switch (arch_prctl_spec_ctrl_get(p, PR_SPEC_INDIRECT_BRANCH)) {
  333. case -EINVAL:
  334. seq_puts(m, "unsupported");
  335. break;
  336. case PR_SPEC_NOT_AFFECTED:
  337. seq_puts(m, "not affected");
  338. break;
  339. case PR_SPEC_PRCTL | PR_SPEC_FORCE_DISABLE:
  340. seq_puts(m, "conditional force disabled");
  341. break;
  342. case PR_SPEC_PRCTL | PR_SPEC_DISABLE:
  343. seq_puts(m, "conditional disabled");
  344. break;
  345. case PR_SPEC_PRCTL | PR_SPEC_ENABLE:
  346. seq_puts(m, "conditional enabled");
  347. break;
  348. case PR_SPEC_ENABLE:
  349. seq_puts(m, "always enabled");
  350. break;
  351. case PR_SPEC_DISABLE:
  352. seq_puts(m, "always disabled");
  353. break;
  354. default:
  355. seq_puts(m, "unknown");
  356. break;
  357. }
  358. seq_putc(m, '\n');
  359. }
  360. static inline void task_context_switch_counts(struct seq_file *m,
  361. struct task_struct *p)
  362. {
  363. seq_put_decimal_ull(m, "voluntary_ctxt_switches:\t", p->nvcsw);
  364. seq_put_decimal_ull(m, "\nnonvoluntary_ctxt_switches:\t", p->nivcsw);
  365. seq_putc(m, '\n');
  366. }
  367. static void task_cpus_allowed(struct seq_file *m, struct task_struct *task)
  368. {
  369. seq_printf(m, "Cpus_allowed:\t%*pb\n",
  370. cpumask_pr_args(&task->cpus_mask));
  371. seq_printf(m, "Cpus_allowed_list:\t%*pbl\n",
  372. cpumask_pr_args(&task->cpus_mask));
  373. }
  374. static inline void task_core_dumping(struct seq_file *m, struct task_struct *task)
  375. {
  376. seq_put_decimal_ull(m, "CoreDumping:\t", !!task->signal->core_state);
  377. seq_putc(m, '\n');
  378. }
  379. static inline void task_thp_status(struct seq_file *m, struct mm_struct *mm)
  380. {
  381. bool thp_enabled = IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE);
  382. if (thp_enabled)
  383. thp_enabled = !test_bit(MMF_DISABLE_THP, &mm->flags);
  384. seq_printf(m, "THP_enabled:\t%d\n", thp_enabled);
  385. }
  386. static inline void task_untag_mask(struct seq_file *m, struct mm_struct *mm)
  387. {
  388. seq_printf(m, "untag_mask:\t%#lx\n", mm_untag_mask(mm));
  389. }
  390. __weak void arch_proc_pid_thread_features(struct seq_file *m,
  391. struct task_struct *task)
  392. {
  393. }
  394. int proc_pid_status(struct seq_file *m, struct pid_namespace *ns,
  395. struct pid *pid, struct task_struct *task)
  396. {
  397. struct mm_struct *mm = get_task_mm(task);
  398. seq_puts(m, "Name:\t");
  399. proc_task_name(m, task, true);
  400. seq_putc(m, '\n');
  401. task_state(m, ns, pid, task);
  402. if (mm) {
  403. task_mem(m, mm);
  404. task_core_dumping(m, task);
  405. task_thp_status(m, mm);
  406. task_untag_mask(m, mm);
  407. mmput(mm);
  408. }
  409. task_sig(m, task);
  410. task_cap(m, task);
  411. task_seccomp(m, task);
  412. task_cpus_allowed(m, task);
  413. cpuset_task_status_allowed(m, task);
  414. task_context_switch_counts(m, task);
  415. arch_proc_pid_thread_features(m, task);
  416. return 0;
  417. }
  418. static int do_task_stat(struct seq_file *m, struct pid_namespace *ns,
  419. struct pid *pid, struct task_struct *task, int whole)
  420. {
  421. unsigned long vsize, eip, esp, wchan = 0;
  422. int priority, nice;
  423. int tty_pgrp = -1, tty_nr = 0;
  424. sigset_t sigign, sigcatch;
  425. char state;
  426. pid_t ppid = 0, pgid = -1, sid = -1;
  427. int num_threads = 0;
  428. int permitted;
  429. struct mm_struct *mm;
  430. unsigned long long start_time;
  431. unsigned long cmin_flt, cmaj_flt, min_flt, maj_flt;
  432. u64 cutime, cstime, cgtime, utime, stime, gtime;
  433. unsigned long rsslim = 0;
  434. unsigned long flags;
  435. int exit_code = task->exit_code;
  436. struct signal_struct *sig = task->signal;
  437. unsigned int seq = 1;
  438. state = *get_task_state(task);
  439. vsize = eip = esp = 0;
  440. permitted = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS | PTRACE_MODE_NOAUDIT);
  441. mm = get_task_mm(task);
  442. if (mm) {
  443. vsize = task_vsize(mm);
  444. /*
  445. * esp and eip are intentionally zeroed out. There is no
  446. * non-racy way to read them without freezing the task.
  447. * Programs that need reliable values can use ptrace(2).
  448. *
  449. * The only exception is if the task is core dumping because
  450. * a program is not able to use ptrace(2) in that case. It is
  451. * safe because the task has stopped executing permanently.
  452. */
  453. if (permitted && (task->flags & (PF_EXITING|PF_DUMPCORE|PF_POSTCOREDUMP))) {
  454. if (try_get_task_stack(task)) {
  455. eip = KSTK_EIP(task);
  456. esp = KSTK_ESP(task);
  457. put_task_stack(task);
  458. }
  459. }
  460. }
  461. sigemptyset(&sigign);
  462. sigemptyset(&sigcatch);
  463. if (lock_task_sighand(task, &flags)) {
  464. if (sig->tty) {
  465. struct pid *pgrp = tty_get_pgrp(sig->tty);
  466. tty_pgrp = pid_nr_ns(pgrp, ns);
  467. put_pid(pgrp);
  468. tty_nr = new_encode_dev(tty_devnum(sig->tty));
  469. }
  470. num_threads = get_nr_threads(task);
  471. collect_sigign_sigcatch(task, &sigign, &sigcatch);
  472. rsslim = READ_ONCE(sig->rlim[RLIMIT_RSS].rlim_cur);
  473. if (whole) {
  474. if (sig->flags & (SIGNAL_GROUP_EXIT | SIGNAL_STOP_STOPPED))
  475. exit_code = sig->group_exit_code;
  476. }
  477. sid = task_session_nr_ns(task, ns);
  478. ppid = task_tgid_nr_ns(task->real_parent, ns);
  479. pgid = task_pgrp_nr_ns(task, ns);
  480. unlock_task_sighand(task, &flags);
  481. }
  482. if (permitted && (!whole || num_threads < 2))
  483. wchan = !task_is_running(task);
  484. do {
  485. seq++; /* 2 on the 1st/lockless path, otherwise odd */
  486. flags = read_seqbegin_or_lock_irqsave(&sig->stats_lock, &seq);
  487. cmin_flt = sig->cmin_flt;
  488. cmaj_flt = sig->cmaj_flt;
  489. cutime = sig->cutime;
  490. cstime = sig->cstime;
  491. cgtime = sig->cgtime;
  492. if (whole) {
  493. struct task_struct *t;
  494. min_flt = sig->min_flt;
  495. maj_flt = sig->maj_flt;
  496. gtime = sig->gtime;
  497. rcu_read_lock();
  498. __for_each_thread(sig, t) {
  499. min_flt += t->min_flt;
  500. maj_flt += t->maj_flt;
  501. gtime += task_gtime(t);
  502. }
  503. rcu_read_unlock();
  504. }
  505. } while (need_seqretry(&sig->stats_lock, seq));
  506. done_seqretry_irqrestore(&sig->stats_lock, seq, flags);
  507. if (whole) {
  508. thread_group_cputime_adjusted(task, &utime, &stime);
  509. } else {
  510. task_cputime_adjusted(task, &utime, &stime);
  511. min_flt = task->min_flt;
  512. maj_flt = task->maj_flt;
  513. gtime = task_gtime(task);
  514. }
  515. /* scale priority and nice values from timeslices to -20..20 */
  516. /* to make it look like a "normal" Unix priority/nice value */
  517. priority = task_prio(task);
  518. nice = task_nice(task);
  519. /* apply timens offset for boottime and convert nsec -> ticks */
  520. start_time =
  521. nsec_to_clock_t(timens_add_boottime_ns(task->start_boottime));
  522. seq_put_decimal_ull(m, "", pid_nr_ns(pid, ns));
  523. seq_puts(m, " (");
  524. proc_task_name(m, task, false);
  525. seq_puts(m, ") ");
  526. seq_putc(m, state);
  527. seq_put_decimal_ll(m, " ", ppid);
  528. seq_put_decimal_ll(m, " ", pgid);
  529. seq_put_decimal_ll(m, " ", sid);
  530. seq_put_decimal_ll(m, " ", tty_nr);
  531. seq_put_decimal_ll(m, " ", tty_pgrp);
  532. seq_put_decimal_ull(m, " ", task->flags);
  533. seq_put_decimal_ull(m, " ", min_flt);
  534. seq_put_decimal_ull(m, " ", cmin_flt);
  535. seq_put_decimal_ull(m, " ", maj_flt);
  536. seq_put_decimal_ull(m, " ", cmaj_flt);
  537. seq_put_decimal_ull(m, " ", nsec_to_clock_t(utime));
  538. seq_put_decimal_ull(m, " ", nsec_to_clock_t(stime));
  539. seq_put_decimal_ll(m, " ", nsec_to_clock_t(cutime));
  540. seq_put_decimal_ll(m, " ", nsec_to_clock_t(cstime));
  541. seq_put_decimal_ll(m, " ", priority);
  542. seq_put_decimal_ll(m, " ", nice);
  543. seq_put_decimal_ll(m, " ", num_threads);
  544. seq_put_decimal_ull(m, " ", 0);
  545. seq_put_decimal_ull(m, " ", start_time);
  546. seq_put_decimal_ull(m, " ", vsize);
  547. seq_put_decimal_ull(m, " ", mm ? get_mm_rss(mm) : 0);
  548. seq_put_decimal_ull(m, " ", rsslim);
  549. seq_put_decimal_ull(m, " ", mm ? (permitted ? mm->start_code : 1) : 0);
  550. seq_put_decimal_ull(m, " ", mm ? (permitted ? mm->end_code : 1) : 0);
  551. seq_put_decimal_ull(m, " ", (permitted && mm) ? mm->start_stack : 0);
  552. seq_put_decimal_ull(m, " ", esp);
  553. seq_put_decimal_ull(m, " ", eip);
  554. /* The signal information here is obsolete.
  555. * It must be decimal for Linux 2.0 compatibility.
  556. * Use /proc/#/status for real-time signals.
  557. */
  558. seq_put_decimal_ull(m, " ", task->pending.signal.sig[0] & 0x7fffffffUL);
  559. seq_put_decimal_ull(m, " ", task->blocked.sig[0] & 0x7fffffffUL);
  560. seq_put_decimal_ull(m, " ", sigign.sig[0] & 0x7fffffffUL);
  561. seq_put_decimal_ull(m, " ", sigcatch.sig[0] & 0x7fffffffUL);
  562. /*
  563. * We used to output the absolute kernel address, but that's an
  564. * information leak - so instead we show a 0/1 flag here, to signal
  565. * to user-space whether there's a wchan field in /proc/PID/wchan.
  566. *
  567. * This works with older implementations of procps as well.
  568. */
  569. seq_put_decimal_ull(m, " ", wchan);
  570. seq_put_decimal_ull(m, " ", 0);
  571. seq_put_decimal_ull(m, " ", 0);
  572. seq_put_decimal_ll(m, " ", task->exit_signal);
  573. seq_put_decimal_ll(m, " ", task_cpu(task));
  574. seq_put_decimal_ull(m, " ", task->rt_priority);
  575. seq_put_decimal_ull(m, " ", task->policy);
  576. seq_put_decimal_ull(m, " ", delayacct_blkio_ticks(task));
  577. seq_put_decimal_ull(m, " ", nsec_to_clock_t(gtime));
  578. seq_put_decimal_ll(m, " ", nsec_to_clock_t(cgtime));
  579. if (mm && permitted) {
  580. seq_put_decimal_ull(m, " ", mm->start_data);
  581. seq_put_decimal_ull(m, " ", mm->end_data);
  582. seq_put_decimal_ull(m, " ", mm->start_brk);
  583. seq_put_decimal_ull(m, " ", mm->arg_start);
  584. seq_put_decimal_ull(m, " ", mm->arg_end);
  585. seq_put_decimal_ull(m, " ", mm->env_start);
  586. seq_put_decimal_ull(m, " ", mm->env_end);
  587. } else
  588. seq_puts(m, " 0 0 0 0 0 0 0");
  589. if (permitted)
  590. seq_put_decimal_ll(m, " ", exit_code);
  591. else
  592. seq_puts(m, " 0");
  593. seq_putc(m, '\n');
  594. if (mm)
  595. mmput(mm);
  596. return 0;
  597. }
  598. int proc_tid_stat(struct seq_file *m, struct pid_namespace *ns,
  599. struct pid *pid, struct task_struct *task)
  600. {
  601. return do_task_stat(m, ns, pid, task, 0);
  602. }
  603. int proc_tgid_stat(struct seq_file *m, struct pid_namespace *ns,
  604. struct pid *pid, struct task_struct *task)
  605. {
  606. return do_task_stat(m, ns, pid, task, 1);
  607. }
  608. int proc_pid_statm(struct seq_file *m, struct pid_namespace *ns,
  609. struct pid *pid, struct task_struct *task)
  610. {
  611. struct mm_struct *mm = get_task_mm(task);
  612. if (mm) {
  613. unsigned long size;
  614. unsigned long resident = 0;
  615. unsigned long shared = 0;
  616. unsigned long text = 0;
  617. unsigned long data = 0;
  618. size = task_statm(mm, &shared, &text, &data, &resident);
  619. mmput(mm);
  620. /*
  621. * For quick read, open code by putting numbers directly
  622. * expected format is
  623. * seq_printf(m, "%lu %lu %lu %lu 0 %lu 0\n",
  624. * size, resident, shared, text, data);
  625. */
  626. seq_put_decimal_ull(m, "", size);
  627. seq_put_decimal_ull(m, " ", resident);
  628. seq_put_decimal_ull(m, " ", shared);
  629. seq_put_decimal_ull(m, " ", text);
  630. seq_put_decimal_ull(m, " ", 0);
  631. seq_put_decimal_ull(m, " ", data);
  632. seq_put_decimal_ull(m, " ", 0);
  633. seq_putc(m, '\n');
  634. } else {
  635. seq_write(m, "0 0 0 0 0 0 0\n", 14);
  636. }
  637. return 0;
  638. }
  639. #ifdef CONFIG_PROC_CHILDREN
  640. static struct pid *
  641. get_children_pid(struct inode *inode, struct pid *pid_prev, loff_t pos)
  642. {
  643. struct task_struct *start, *task;
  644. struct pid *pid = NULL;
  645. read_lock(&tasklist_lock);
  646. start = pid_task(proc_pid(inode), PIDTYPE_PID);
  647. if (!start)
  648. goto out;
  649. /*
  650. * Lets try to continue searching first, this gives
  651. * us significant speedup on children-rich processes.
  652. */
  653. if (pid_prev) {
  654. task = pid_task(pid_prev, PIDTYPE_PID);
  655. if (task && task->real_parent == start &&
  656. !(list_empty(&task->sibling))) {
  657. if (list_is_last(&task->sibling, &start->children))
  658. goto out;
  659. task = list_first_entry(&task->sibling,
  660. struct task_struct, sibling);
  661. pid = get_pid(task_pid(task));
  662. goto out;
  663. }
  664. }
  665. /*
  666. * Slow search case.
  667. *
  668. * We might miss some children here if children
  669. * are exited while we were not holding the lock,
  670. * but it was never promised to be accurate that
  671. * much.
  672. *
  673. * "Just suppose that the parent sleeps, but N children
  674. * exit after we printed their tids. Now the slow paths
  675. * skips N extra children, we miss N tasks." (c)
  676. *
  677. * So one need to stop or freeze the leader and all
  678. * its children to get a precise result.
  679. */
  680. list_for_each_entry(task, &start->children, sibling) {
  681. if (pos-- == 0) {
  682. pid = get_pid(task_pid(task));
  683. break;
  684. }
  685. }
  686. out:
  687. read_unlock(&tasklist_lock);
  688. return pid;
  689. }
  690. static int children_seq_show(struct seq_file *seq, void *v)
  691. {
  692. struct inode *inode = file_inode(seq->file);
  693. seq_printf(seq, "%d ", pid_nr_ns(v, proc_pid_ns(inode->i_sb)));
  694. return 0;
  695. }
  696. static void *children_seq_start(struct seq_file *seq, loff_t *pos)
  697. {
  698. return get_children_pid(file_inode(seq->file), NULL, *pos);
  699. }
  700. static void *children_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  701. {
  702. struct pid *pid;
  703. pid = get_children_pid(file_inode(seq->file), v, *pos + 1);
  704. put_pid(v);
  705. ++*pos;
  706. return pid;
  707. }
  708. static void children_seq_stop(struct seq_file *seq, void *v)
  709. {
  710. put_pid(v);
  711. }
  712. static const struct seq_operations children_seq_ops = {
  713. .start = children_seq_start,
  714. .next = children_seq_next,
  715. .stop = children_seq_stop,
  716. .show = children_seq_show,
  717. };
  718. static int children_seq_open(struct inode *inode, struct file *file)
  719. {
  720. return seq_open(file, &children_seq_ops);
  721. }
  722. const struct file_operations proc_tid_children_operations = {
  723. .open = children_seq_open,
  724. .read = seq_read,
  725. .llseek = seq_lseek,
  726. .release = seq_release,
  727. };
  728. #endif /* CONFIG_PROC_CHILDREN */