array.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  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/hex.h>
  58. #include <linux/time.h>
  59. #include <linux/time_namespace.h>
  60. #include <linux/kernel.h>
  61. #include <linux/kernel_stat.h>
  62. #include <linux/tty.h>
  63. #include <linux/string.h>
  64. #include <linux/mman.h>
  65. #include <linux/sched/mm.h>
  66. #include <linux/sched/numa_balancing.h>
  67. #include <linux/sched/task_stack.h>
  68. #include <linux/sched/task.h>
  69. #include <linux/sched/cputime.h>
  70. #include <linux/proc_fs.h>
  71. #include <linux/ioport.h>
  72. #include <linux/io.h>
  73. #include <linux/mm.h>
  74. #include <linux/hugetlb.h>
  75. #include <linux/pagemap.h>
  76. #include <linux/swap.h>
  77. #include <linux/smp.h>
  78. #include <linux/signal.h>
  79. #include <linux/highmem.h>
  80. #include <linux/file.h>
  81. #include <linux/fdtable.h>
  82. #include <linux/times.h>
  83. #include <linux/cpuset.h>
  84. #include <linux/rcupdate.h>
  85. #include <linux/delayacct.h>
  86. #include <linux/seq_file.h>
  87. #include <linux/pid_namespace.h>
  88. #include <linux/prctl.h>
  89. #include <linux/ptrace.h>
  90. #include <linux/string_helpers.h>
  91. #include <linux/user_namespace.h>
  92. #include <linux/fs_struct.h>
  93. #include <linux/kthread.h>
  94. #include <linux/mmu_context.h>
  95. #include <asm/processor.h>
  96. #include "internal.h"
  97. void proc_task_name(struct seq_file *m, struct task_struct *p, bool escape)
  98. {
  99. char tcomm[64];
  100. /*
  101. * Test before PF_KTHREAD because all workqueue worker threads are
  102. * kernel threads.
  103. */
  104. if (p->flags & PF_WQ_WORKER)
  105. wq_worker_comm(tcomm, sizeof(tcomm), p);
  106. else if (p->flags & PF_KTHREAD)
  107. get_kthread_comm(tcomm, sizeof(tcomm), p);
  108. else
  109. get_task_comm(tcomm, p);
  110. if (escape)
  111. seq_escape_str(m, tcomm, ESCAPE_SPACE | ESCAPE_SPECIAL, "\n\\");
  112. else
  113. seq_printf(m, "%.64s", tcomm);
  114. }
  115. /*
  116. * The task state array is a strange "bitmap" of
  117. * reasons to sleep. Thus "running" is zero, and
  118. * you can test for combinations of others with
  119. * simple bit tests.
  120. */
  121. static const char * const task_state_array[] = {
  122. /* states in TASK_REPORT: */
  123. "R (running)", /* 0x00 */
  124. "S (sleeping)", /* 0x01 */
  125. "D (disk sleep)", /* 0x02 */
  126. "T (stopped)", /* 0x04 */
  127. "t (tracing stop)", /* 0x08 */
  128. "X (dead)", /* 0x10 */
  129. "Z (zombie)", /* 0x20 */
  130. "P (parked)", /* 0x40 */
  131. /* states beyond TASK_REPORT: */
  132. "I (idle)", /* 0x80 */
  133. };
  134. static inline const char *get_task_state(struct task_struct *tsk)
  135. {
  136. BUILD_BUG_ON(1 + ilog2(TASK_REPORT_MAX) != ARRAY_SIZE(task_state_array));
  137. return task_state_array[task_state_index(tsk)];
  138. }
  139. static inline void task_state(struct seq_file *m, struct pid_namespace *ns,
  140. struct pid *pid, struct task_struct *p)
  141. {
  142. struct user_namespace *user_ns = seq_user_ns(m);
  143. struct group_info *group_info;
  144. int g, umask = -1;
  145. struct task_struct *tracer;
  146. const struct cred *cred;
  147. pid_t ppid, tpid = 0, tgid, ngid;
  148. unsigned int max_fds = 0;
  149. rcu_read_lock();
  150. tracer = ptrace_parent(p);
  151. if (tracer)
  152. tpid = task_pid_nr_ns(tracer, ns);
  153. ppid = task_ppid_nr_ns(p, 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 = !mm_flags_test(MMF_DISABLE_THP_COMPLETELY, mm);
  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. state = *get_task_state(task);
  438. vsize = eip = esp = 0;
  439. permitted = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS | PTRACE_MODE_NOAUDIT);
  440. mm = get_task_mm(task);
  441. if (mm) {
  442. vsize = task_vsize(mm);
  443. /*
  444. * esp and eip are intentionally zeroed out. There is no
  445. * non-racy way to read them without freezing the task.
  446. * Programs that need reliable values can use ptrace(2).
  447. *
  448. * The only exception is if the task is core dumping because
  449. * a program is not able to use ptrace(2) in that case. It is
  450. * safe because the task has stopped executing permanently.
  451. */
  452. if (permitted && (task->flags & (PF_EXITING|PF_DUMPCORE|PF_POSTCOREDUMP))) {
  453. if (try_get_task_stack(task)) {
  454. eip = KSTK_EIP(task);
  455. esp = KSTK_ESP(task);
  456. put_task_stack(task);
  457. }
  458. }
  459. }
  460. sigemptyset(&sigign);
  461. sigemptyset(&sigcatch);
  462. if (lock_task_sighand(task, &flags)) {
  463. if (sig->tty) {
  464. struct pid *pgrp = tty_get_pgrp(sig->tty);
  465. tty_pgrp = pid_nr_ns(pgrp, ns);
  466. put_pid(pgrp);
  467. tty_nr = new_encode_dev(tty_devnum(sig->tty));
  468. }
  469. num_threads = get_nr_threads(task);
  470. collect_sigign_sigcatch(task, &sigign, &sigcatch);
  471. rsslim = READ_ONCE(sig->rlim[RLIMIT_RSS].rlim_cur);
  472. if (whole) {
  473. if (sig->flags & (SIGNAL_GROUP_EXIT | SIGNAL_STOP_STOPPED))
  474. exit_code = sig->group_exit_code;
  475. }
  476. sid = task_session_nr_ns(task, ns);
  477. ppid = task_ppid_nr_ns(task, ns);
  478. pgid = task_pgrp_nr_ns(task, ns);
  479. unlock_task_sighand(task, &flags);
  480. }
  481. if (permitted && (!whole || num_threads < 2))
  482. wchan = !task_is_running(task);
  483. scoped_guard(rcu) {
  484. scoped_seqlock_read (&sig->stats_lock, ss_lock_irqsave) {
  485. cmin_flt = sig->cmin_flt;
  486. cmaj_flt = sig->cmaj_flt;
  487. cutime = sig->cutime;
  488. cstime = sig->cstime;
  489. cgtime = sig->cgtime;
  490. if (whole) {
  491. struct task_struct *t;
  492. min_flt = sig->min_flt;
  493. maj_flt = sig->maj_flt;
  494. gtime = sig->gtime;
  495. __for_each_thread(sig, t) {
  496. min_flt += t->min_flt;
  497. maj_flt += t->maj_flt;
  498. gtime += task_gtime(t);
  499. }
  500. }
  501. }
  502. }
  503. if (whole) {
  504. thread_group_cputime_adjusted(task, &utime, &stime);
  505. } else {
  506. task_cputime_adjusted(task, &utime, &stime);
  507. min_flt = task->min_flt;
  508. maj_flt = task->maj_flt;
  509. gtime = task_gtime(task);
  510. }
  511. /* scale priority and nice values from timeslices to -20..20 */
  512. /* to make it look like a "normal" Unix priority/nice value */
  513. priority = task_prio(task);
  514. nice = task_nice(task);
  515. /* apply timens offset for boottime and convert nsec -> ticks */
  516. start_time =
  517. nsec_to_clock_t(timens_add_boottime_ns(task->start_boottime));
  518. seq_put_decimal_ull(m, "", pid_nr_ns(pid, ns));
  519. seq_puts(m, " (");
  520. proc_task_name(m, task, false);
  521. seq_puts(m, ") ");
  522. seq_putc(m, state);
  523. seq_put_decimal_ll(m, " ", ppid);
  524. seq_put_decimal_ll(m, " ", pgid);
  525. seq_put_decimal_ll(m, " ", sid);
  526. seq_put_decimal_ll(m, " ", tty_nr);
  527. seq_put_decimal_ll(m, " ", tty_pgrp);
  528. seq_put_decimal_ull(m, " ", task->flags);
  529. seq_put_decimal_ull(m, " ", min_flt);
  530. seq_put_decimal_ull(m, " ", cmin_flt);
  531. seq_put_decimal_ull(m, " ", maj_flt);
  532. seq_put_decimal_ull(m, " ", cmaj_flt);
  533. seq_put_decimal_ull(m, " ", nsec_to_clock_t(utime));
  534. seq_put_decimal_ull(m, " ", nsec_to_clock_t(stime));
  535. seq_put_decimal_ll(m, " ", nsec_to_clock_t(cutime));
  536. seq_put_decimal_ll(m, " ", nsec_to_clock_t(cstime));
  537. seq_put_decimal_ll(m, " ", priority);
  538. seq_put_decimal_ll(m, " ", nice);
  539. seq_put_decimal_ll(m, " ", num_threads);
  540. seq_put_decimal_ull(m, " ", 0);
  541. seq_put_decimal_ull(m, " ", start_time);
  542. seq_put_decimal_ull(m, " ", vsize);
  543. seq_put_decimal_ull(m, " ", mm ? get_mm_rss(mm) : 0);
  544. seq_put_decimal_ull(m, " ", rsslim);
  545. seq_put_decimal_ull(m, " ", mm ? (permitted ? mm->start_code : 1) : 0);
  546. seq_put_decimal_ull(m, " ", mm ? (permitted ? mm->end_code : 1) : 0);
  547. seq_put_decimal_ull(m, " ", (permitted && mm) ? mm->start_stack : 0);
  548. seq_put_decimal_ull(m, " ", esp);
  549. seq_put_decimal_ull(m, " ", eip);
  550. /* The signal information here is obsolete.
  551. * It must be decimal for Linux 2.0 compatibility.
  552. * Use /proc/#/status for real-time signals.
  553. */
  554. seq_put_decimal_ull(m, " ", task->pending.signal.sig[0] & 0x7fffffffUL);
  555. seq_put_decimal_ull(m, " ", task->blocked.sig[0] & 0x7fffffffUL);
  556. seq_put_decimal_ull(m, " ", sigign.sig[0] & 0x7fffffffUL);
  557. seq_put_decimal_ull(m, " ", sigcatch.sig[0] & 0x7fffffffUL);
  558. /*
  559. * We used to output the absolute kernel address, but that's an
  560. * information leak - so instead we show a 0/1 flag here, to signal
  561. * to user-space whether there's a wchan field in /proc/PID/wchan.
  562. *
  563. * This works with older implementations of procps as well.
  564. */
  565. seq_put_decimal_ull(m, " ", wchan);
  566. seq_put_decimal_ull(m, " ", 0);
  567. seq_put_decimal_ull(m, " ", 0);
  568. seq_put_decimal_ll(m, " ", task->exit_signal);
  569. seq_put_decimal_ll(m, " ", task_cpu(task));
  570. seq_put_decimal_ull(m, " ", task->rt_priority);
  571. seq_put_decimal_ull(m, " ", task->policy);
  572. seq_put_decimal_ull(m, " ", delayacct_blkio_ticks(task));
  573. seq_put_decimal_ull(m, " ", nsec_to_clock_t(gtime));
  574. seq_put_decimal_ll(m, " ", nsec_to_clock_t(cgtime));
  575. if (mm && permitted) {
  576. seq_put_decimal_ull(m, " ", mm->start_data);
  577. seq_put_decimal_ull(m, " ", mm->end_data);
  578. seq_put_decimal_ull(m, " ", mm->start_brk);
  579. seq_put_decimal_ull(m, " ", mm->arg_start);
  580. seq_put_decimal_ull(m, " ", mm->arg_end);
  581. seq_put_decimal_ull(m, " ", mm->env_start);
  582. seq_put_decimal_ull(m, " ", mm->env_end);
  583. } else
  584. seq_puts(m, " 0 0 0 0 0 0 0");
  585. if (permitted)
  586. seq_put_decimal_ll(m, " ", exit_code);
  587. else
  588. seq_puts(m, " 0");
  589. seq_putc(m, '\n');
  590. if (mm)
  591. mmput(mm);
  592. return 0;
  593. }
  594. int proc_tid_stat(struct seq_file *m, struct pid_namespace *ns,
  595. struct pid *pid, struct task_struct *task)
  596. {
  597. return do_task_stat(m, ns, pid, task, 0);
  598. }
  599. int proc_tgid_stat(struct seq_file *m, struct pid_namespace *ns,
  600. struct pid *pid, struct task_struct *task)
  601. {
  602. return do_task_stat(m, ns, pid, task, 1);
  603. }
  604. int proc_pid_statm(struct seq_file *m, struct pid_namespace *ns,
  605. struct pid *pid, struct task_struct *task)
  606. {
  607. struct mm_struct *mm = get_task_mm(task);
  608. if (mm) {
  609. unsigned long size;
  610. unsigned long resident = 0;
  611. unsigned long shared = 0;
  612. unsigned long text = 0;
  613. unsigned long data = 0;
  614. size = task_statm(mm, &shared, &text, &data, &resident);
  615. mmput(mm);
  616. /*
  617. * For quick read, open code by putting numbers directly
  618. * expected format is
  619. * seq_printf(m, "%lu %lu %lu %lu 0 %lu 0\n",
  620. * size, resident, shared, text, data);
  621. */
  622. seq_put_decimal_ull(m, "", size);
  623. seq_put_decimal_ull(m, " ", resident);
  624. seq_put_decimal_ull(m, " ", shared);
  625. seq_put_decimal_ull(m, " ", text);
  626. seq_put_decimal_ull(m, " ", 0);
  627. seq_put_decimal_ull(m, " ", data);
  628. seq_put_decimal_ull(m, " ", 0);
  629. seq_putc(m, '\n');
  630. } else {
  631. seq_write(m, "0 0 0 0 0 0 0\n", 14);
  632. }
  633. return 0;
  634. }
  635. #ifdef CONFIG_PROC_CHILDREN
  636. static struct pid *
  637. get_children_pid(struct inode *inode, struct pid *pid_prev, loff_t pos)
  638. {
  639. struct task_struct *start, *task;
  640. struct pid *pid = NULL;
  641. read_lock(&tasklist_lock);
  642. start = pid_task(proc_pid(inode), PIDTYPE_PID);
  643. if (!start)
  644. goto out;
  645. /*
  646. * Lets try to continue searching first, this gives
  647. * us significant speedup on children-rich processes.
  648. */
  649. if (pid_prev) {
  650. task = pid_task(pid_prev, PIDTYPE_PID);
  651. if (task && task->real_parent == start &&
  652. !(list_empty(&task->sibling))) {
  653. if (list_is_last(&task->sibling, &start->children))
  654. goto out;
  655. task = list_first_entry(&task->sibling,
  656. struct task_struct, sibling);
  657. pid = get_pid(task_pid(task));
  658. goto out;
  659. }
  660. }
  661. /*
  662. * Slow search case.
  663. *
  664. * We might miss some children here if children
  665. * are exited while we were not holding the lock,
  666. * but it was never promised to be accurate that
  667. * much.
  668. *
  669. * "Just suppose that the parent sleeps, but N children
  670. * exit after we printed their tids. Now the slow paths
  671. * skips N extra children, we miss N tasks." (c)
  672. *
  673. * So one need to stop or freeze the leader and all
  674. * its children to get a precise result.
  675. */
  676. list_for_each_entry(task, &start->children, sibling) {
  677. if (pos-- == 0) {
  678. pid = get_pid(task_pid(task));
  679. break;
  680. }
  681. }
  682. out:
  683. read_unlock(&tasklist_lock);
  684. return pid;
  685. }
  686. static int children_seq_show(struct seq_file *seq, void *v)
  687. {
  688. struct inode *inode = file_inode(seq->file);
  689. seq_printf(seq, "%d ", pid_nr_ns(v, proc_pid_ns(inode->i_sb)));
  690. return 0;
  691. }
  692. static void *children_seq_start(struct seq_file *seq, loff_t *pos)
  693. {
  694. return get_children_pid(file_inode(seq->file), NULL, *pos);
  695. }
  696. static void *children_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  697. {
  698. struct pid *pid;
  699. pid = get_children_pid(file_inode(seq->file), v, *pos + 1);
  700. put_pid(v);
  701. ++*pos;
  702. return pid;
  703. }
  704. static void children_seq_stop(struct seq_file *seq, void *v)
  705. {
  706. put_pid(v);
  707. }
  708. static const struct seq_operations children_seq_ops = {
  709. .start = children_seq_start,
  710. .next = children_seq_next,
  711. .stop = children_seq_stop,
  712. .show = children_seq_show,
  713. };
  714. static int children_seq_open(struct inode *inode, struct file *file)
  715. {
  716. return seq_open(file, &children_seq_ops);
  717. }
  718. const struct file_operations proc_tid_children_operations = {
  719. .open = children_seq_open,
  720. .read = seq_read,
  721. .llseek = seq_lseek,
  722. .release = seq_release,
  723. };
  724. #endif /* CONFIG_PROC_CHILDREN */