array.c 22 KB

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