array.c 20 KB

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