array.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  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/proc_fs.h>
  63. #include <linux/ioport.h>
  64. #include <linux/uaccess.h>
  65. #include <linux/io.h>
  66. #include <linux/mm.h>
  67. #include <linux/hugetlb.h>
  68. #include <linux/pagemap.h>
  69. #include <linux/swap.h>
  70. #include <linux/smp.h>
  71. #include <linux/signal.h>
  72. #include <linux/highmem.h>
  73. #include <linux/file.h>
  74. #include <linux/fdtable.h>
  75. #include <linux/times.h>
  76. #include <linux/cpuset.h>
  77. #include <linux/rcupdate.h>
  78. #include <linux/delayacct.h>
  79. #include <linux/seq_file.h>
  80. #include <linux/pid_namespace.h>
  81. #include <linux/ptrace.h>
  82. #include <linux/tracehook.h>
  83. #include <linux/string_helpers.h>
  84. #include <linux/user_namespace.h>
  85. #include <asm/pgtable.h>
  86. #include <asm/processor.h>
  87. #include "internal.h"
  88. static inline void task_name(struct seq_file *m, struct task_struct *p)
  89. {
  90. char *buf;
  91. char tcomm[sizeof(p->comm)];
  92. get_task_comm(tcomm, p);
  93. seq_puts(m, "Name:\t");
  94. buf = m->buf + m->count;
  95. /* Ignore error for now */
  96. string_escape_str(tcomm, &buf, m->size - m->count,
  97. ESCAPE_SPACE | ESCAPE_SPECIAL, "\n\\");
  98. m->count = buf - m->buf;
  99. seq_putc(m, '\n');
  100. }
  101. /*
  102. * The task state array is a strange "bitmap" of
  103. * reasons to sleep. Thus "running" is zero, and
  104. * you can test for combinations of others with
  105. * simple bit tests.
  106. */
  107. static const char * const task_state_array[] = {
  108. "R (running)", /* 0 */
  109. "S (sleeping)", /* 1 */
  110. "D (disk sleep)", /* 2 */
  111. "T (stopped)", /* 4 */
  112. "t (tracing stop)", /* 8 */
  113. "X (dead)", /* 16 */
  114. "Z (zombie)", /* 32 */
  115. };
  116. static inline const char *get_task_state(struct task_struct *tsk)
  117. {
  118. unsigned int state = (tsk->state | tsk->exit_state) & TASK_REPORT;
  119. BUILD_BUG_ON(1 + ilog2(TASK_REPORT) != ARRAY_SIZE(task_state_array)-1);
  120. return task_state_array[fls(state)];
  121. }
  122. static inline void task_state(struct seq_file *m, struct pid_namespace *ns,
  123. struct pid *pid, struct task_struct *p)
  124. {
  125. struct user_namespace *user_ns = seq_user_ns(m);
  126. struct group_info *group_info;
  127. int g;
  128. struct task_struct *tracer;
  129. const struct cred *cred;
  130. pid_t ppid, tpid = 0, tgid, ngid;
  131. unsigned int max_fds = 0;
  132. rcu_read_lock();
  133. ppid = pid_alive(p) ?
  134. task_tgid_nr_ns(rcu_dereference(p->real_parent), ns) : 0;
  135. tracer = ptrace_parent(p);
  136. if (tracer)
  137. tpid = task_pid_nr_ns(tracer, ns);
  138. tgid = task_tgid_nr_ns(p, ns);
  139. ngid = task_numa_group_id(p);
  140. cred = get_task_cred(p);
  141. task_lock(p);
  142. if (p->files)
  143. max_fds = files_fdtable(p->files)->max_fds;
  144. task_unlock(p);
  145. rcu_read_unlock();
  146. seq_printf(m,
  147. "State:\t%s\n"
  148. "Tgid:\t%d\n"
  149. "Ngid:\t%d\n"
  150. "Pid:\t%d\n"
  151. "PPid:\t%d\n"
  152. "TracerPid:\t%d\n"
  153. "Uid:\t%d\t%d\t%d\t%d\n"
  154. "Gid:\t%d\t%d\t%d\t%d\n"
  155. "FDSize:\t%d\nGroups:\t",
  156. get_task_state(p),
  157. tgid, ngid, pid_nr_ns(pid, ns), ppid, tpid,
  158. from_kuid_munged(user_ns, cred->uid),
  159. from_kuid_munged(user_ns, cred->euid),
  160. from_kuid_munged(user_ns, cred->suid),
  161. from_kuid_munged(user_ns, cred->fsuid),
  162. from_kgid_munged(user_ns, cred->gid),
  163. from_kgid_munged(user_ns, cred->egid),
  164. from_kgid_munged(user_ns, cred->sgid),
  165. from_kgid_munged(user_ns, cred->fsgid),
  166. max_fds);
  167. group_info = cred->group_info;
  168. for (g = 0; g < group_info->ngroups; g++)
  169. seq_printf(m, "%d ",
  170. from_kgid_munged(user_ns, GROUP_AT(group_info, g)));
  171. put_cred(cred);
  172. seq_putc(m, '\n');
  173. }
  174. void render_sigset_t(struct seq_file *m, const char *header,
  175. sigset_t *set)
  176. {
  177. int i;
  178. seq_puts(m, header);
  179. i = _NSIG;
  180. do {
  181. int x = 0;
  182. i -= 4;
  183. if (sigismember(set, i+1)) x |= 1;
  184. if (sigismember(set, i+2)) x |= 2;
  185. if (sigismember(set, i+3)) x |= 4;
  186. if (sigismember(set, i+4)) x |= 8;
  187. seq_printf(m, "%x", x);
  188. } while (i >= 4);
  189. seq_putc(m, '\n');
  190. }
  191. static void collect_sigign_sigcatch(struct task_struct *p, sigset_t *ign,
  192. sigset_t *catch)
  193. {
  194. struct k_sigaction *k;
  195. int i;
  196. k = p->sighand->action;
  197. for (i = 1; i <= _NSIG; ++i, ++k) {
  198. if (k->sa.sa_handler == SIG_IGN)
  199. sigaddset(ign, i);
  200. else if (k->sa.sa_handler != SIG_DFL)
  201. sigaddset(catch, i);
  202. }
  203. }
  204. static inline void task_sig(struct seq_file *m, struct task_struct *p)
  205. {
  206. unsigned long flags;
  207. sigset_t pending, shpending, blocked, ignored, caught;
  208. int num_threads = 0;
  209. unsigned long qsize = 0;
  210. unsigned long qlim = 0;
  211. sigemptyset(&pending);
  212. sigemptyset(&shpending);
  213. sigemptyset(&blocked);
  214. sigemptyset(&ignored);
  215. sigemptyset(&caught);
  216. if (lock_task_sighand(p, &flags)) {
  217. pending = p->pending.signal;
  218. shpending = p->signal->shared_pending.signal;
  219. blocked = p->blocked;
  220. collect_sigign_sigcatch(p, &ignored, &caught);
  221. num_threads = get_nr_threads(p);
  222. rcu_read_lock(); /* FIXME: is this correct? */
  223. qsize = atomic_read(&__task_cred(p)->user->sigpending);
  224. rcu_read_unlock();
  225. qlim = task_rlimit(p, RLIMIT_SIGPENDING);
  226. unlock_task_sighand(p, &flags);
  227. }
  228. seq_printf(m, "Threads:\t%d\n", num_threads);
  229. seq_printf(m, "SigQ:\t%lu/%lu\n", qsize, qlim);
  230. /* render them all */
  231. render_sigset_t(m, "SigPnd:\t", &pending);
  232. render_sigset_t(m, "ShdPnd:\t", &shpending);
  233. render_sigset_t(m, "SigBlk:\t", &blocked);
  234. render_sigset_t(m, "SigIgn:\t", &ignored);
  235. render_sigset_t(m, "SigCgt:\t", &caught);
  236. }
  237. static void render_cap_t(struct seq_file *m, const char *header,
  238. kernel_cap_t *a)
  239. {
  240. unsigned __capi;
  241. seq_puts(m, header);
  242. CAP_FOR_EACH_U32(__capi) {
  243. seq_printf(m, "%08x",
  244. a->cap[CAP_LAST_U32 - __capi]);
  245. }
  246. seq_putc(m, '\n');
  247. }
  248. static inline void task_cap(struct seq_file *m, struct task_struct *p)
  249. {
  250. const struct cred *cred;
  251. kernel_cap_t cap_inheritable, cap_permitted, cap_effective, cap_bset;
  252. rcu_read_lock();
  253. cred = __task_cred(p);
  254. cap_inheritable = cred->cap_inheritable;
  255. cap_permitted = cred->cap_permitted;
  256. cap_effective = cred->cap_effective;
  257. cap_bset = cred->cap_bset;
  258. rcu_read_unlock();
  259. render_cap_t(m, "CapInh:\t", &cap_inheritable);
  260. render_cap_t(m, "CapPrm:\t", &cap_permitted);
  261. render_cap_t(m, "CapEff:\t", &cap_effective);
  262. render_cap_t(m, "CapBnd:\t", &cap_bset);
  263. }
  264. static inline void task_seccomp(struct seq_file *m, struct task_struct *p)
  265. {
  266. #ifdef CONFIG_SECCOMP
  267. seq_printf(m, "Seccomp:\t%d\n", p->seccomp.mode);
  268. #endif
  269. }
  270. static inline void task_context_switch_counts(struct seq_file *m,
  271. struct task_struct *p)
  272. {
  273. seq_printf(m, "voluntary_ctxt_switches:\t%lu\n"
  274. "nonvoluntary_ctxt_switches:\t%lu\n",
  275. p->nvcsw,
  276. p->nivcsw);
  277. }
  278. static void task_cpus_allowed(struct seq_file *m, struct task_struct *task)
  279. {
  280. seq_puts(m, "Cpus_allowed:\t");
  281. seq_cpumask(m, &task->cpus_allowed);
  282. seq_putc(m, '\n');
  283. seq_puts(m, "Cpus_allowed_list:\t");
  284. seq_cpumask_list(m, &task->cpus_allowed);
  285. seq_putc(m, '\n');
  286. }
  287. int proc_pid_status(struct seq_file *m, struct pid_namespace *ns,
  288. struct pid *pid, struct task_struct *task)
  289. {
  290. struct mm_struct *mm = get_task_mm(task);
  291. task_name(m, task);
  292. task_state(m, ns, pid, task);
  293. if (mm) {
  294. task_mem(m, mm);
  295. mmput(mm);
  296. }
  297. task_sig(m, task);
  298. task_cap(m, task);
  299. task_seccomp(m, task);
  300. task_cpus_allowed(m, task);
  301. cpuset_task_status_allowed(m, task);
  302. task_context_switch_counts(m, task);
  303. return 0;
  304. }
  305. static int do_task_stat(struct seq_file *m, struct pid_namespace *ns,
  306. struct pid *pid, struct task_struct *task, int whole)
  307. {
  308. unsigned long vsize, eip, esp, wchan = ~0UL;
  309. int priority, nice;
  310. int tty_pgrp = -1, tty_nr = 0;
  311. sigset_t sigign, sigcatch;
  312. char state;
  313. pid_t ppid = 0, pgid = -1, sid = -1;
  314. int num_threads = 0;
  315. int permitted;
  316. struct mm_struct *mm;
  317. unsigned long long start_time;
  318. unsigned long cmin_flt = 0, cmaj_flt = 0;
  319. unsigned long min_flt = 0, maj_flt = 0;
  320. cputime_t cutime, cstime, utime, stime;
  321. cputime_t cgtime, gtime;
  322. unsigned long rsslim = 0;
  323. char tcomm[sizeof(task->comm)];
  324. unsigned long flags;
  325. state = *get_task_state(task);
  326. vsize = eip = esp = 0;
  327. permitted = ptrace_may_access(task, PTRACE_MODE_READ | PTRACE_MODE_NOAUDIT);
  328. mm = get_task_mm(task);
  329. if (mm) {
  330. vsize = task_vsize(mm);
  331. if (permitted) {
  332. eip = KSTK_EIP(task);
  333. esp = KSTK_ESP(task);
  334. }
  335. }
  336. get_task_comm(tcomm, task);
  337. sigemptyset(&sigign);
  338. sigemptyset(&sigcatch);
  339. cutime = cstime = utime = stime = 0;
  340. cgtime = gtime = 0;
  341. if (lock_task_sighand(task, &flags)) {
  342. struct signal_struct *sig = task->signal;
  343. if (sig->tty) {
  344. struct pid *pgrp = tty_get_pgrp(sig->tty);
  345. tty_pgrp = pid_nr_ns(pgrp, ns);
  346. put_pid(pgrp);
  347. tty_nr = new_encode_dev(tty_devnum(sig->tty));
  348. }
  349. num_threads = get_nr_threads(task);
  350. collect_sigign_sigcatch(task, &sigign, &sigcatch);
  351. cmin_flt = sig->cmin_flt;
  352. cmaj_flt = sig->cmaj_flt;
  353. cutime = sig->cutime;
  354. cstime = sig->cstime;
  355. cgtime = sig->cgtime;
  356. rsslim = ACCESS_ONCE(sig->rlim[RLIMIT_RSS].rlim_cur);
  357. /* add up live thread stats at the group level */
  358. if (whole) {
  359. struct task_struct *t = task;
  360. do {
  361. min_flt += t->min_flt;
  362. maj_flt += t->maj_flt;
  363. gtime += task_gtime(t);
  364. } while_each_thread(task, t);
  365. min_flt += sig->min_flt;
  366. maj_flt += sig->maj_flt;
  367. thread_group_cputime_adjusted(task, &utime, &stime);
  368. gtime += sig->gtime;
  369. }
  370. sid = task_session_nr_ns(task, ns);
  371. ppid = task_tgid_nr_ns(task->real_parent, ns);
  372. pgid = task_pgrp_nr_ns(task, ns);
  373. unlock_task_sighand(task, &flags);
  374. }
  375. if (permitted && (!whole || num_threads < 2))
  376. wchan = get_wchan(task);
  377. if (!whole) {
  378. min_flt = task->min_flt;
  379. maj_flt = task->maj_flt;
  380. task_cputime_adjusted(task, &utime, &stime);
  381. gtime = task_gtime(task);
  382. }
  383. /* scale priority and nice values from timeslices to -20..20 */
  384. /* to make it look like a "normal" Unix priority/nice value */
  385. priority = task_prio(task);
  386. nice = task_nice(task);
  387. /* convert nsec -> ticks */
  388. start_time = nsec_to_clock_t(task->real_start_time);
  389. seq_printf(m, "%d (%s) %c", pid_nr_ns(pid, ns), tcomm, state);
  390. seq_put_decimal_ll(m, ' ', ppid);
  391. seq_put_decimal_ll(m, ' ', pgid);
  392. seq_put_decimal_ll(m, ' ', sid);
  393. seq_put_decimal_ll(m, ' ', tty_nr);
  394. seq_put_decimal_ll(m, ' ', tty_pgrp);
  395. seq_put_decimal_ull(m, ' ', task->flags);
  396. seq_put_decimal_ull(m, ' ', min_flt);
  397. seq_put_decimal_ull(m, ' ', cmin_flt);
  398. seq_put_decimal_ull(m, ' ', maj_flt);
  399. seq_put_decimal_ull(m, ' ', cmaj_flt);
  400. seq_put_decimal_ull(m, ' ', cputime_to_clock_t(utime));
  401. seq_put_decimal_ull(m, ' ', cputime_to_clock_t(stime));
  402. seq_put_decimal_ll(m, ' ', cputime_to_clock_t(cutime));
  403. seq_put_decimal_ll(m, ' ', cputime_to_clock_t(cstime));
  404. seq_put_decimal_ll(m, ' ', priority);
  405. seq_put_decimal_ll(m, ' ', nice);
  406. seq_put_decimal_ll(m, ' ', num_threads);
  407. seq_put_decimal_ull(m, ' ', 0);
  408. seq_put_decimal_ull(m, ' ', start_time);
  409. seq_put_decimal_ull(m, ' ', vsize);
  410. seq_put_decimal_ull(m, ' ', mm ? get_mm_rss(mm) : 0);
  411. seq_put_decimal_ull(m, ' ', rsslim);
  412. seq_put_decimal_ull(m, ' ', mm ? (permitted ? mm->start_code : 1) : 0);
  413. seq_put_decimal_ull(m, ' ', mm ? (permitted ? mm->end_code : 1) : 0);
  414. seq_put_decimal_ull(m, ' ', (permitted && mm) ? mm->start_stack : 0);
  415. seq_put_decimal_ull(m, ' ', esp);
  416. seq_put_decimal_ull(m, ' ', eip);
  417. /* The signal information here is obsolete.
  418. * It must be decimal for Linux 2.0 compatibility.
  419. * Use /proc/#/status for real-time signals.
  420. */
  421. seq_put_decimal_ull(m, ' ', task->pending.signal.sig[0] & 0x7fffffffUL);
  422. seq_put_decimal_ull(m, ' ', task->blocked.sig[0] & 0x7fffffffUL);
  423. seq_put_decimal_ull(m, ' ', sigign.sig[0] & 0x7fffffffUL);
  424. seq_put_decimal_ull(m, ' ', sigcatch.sig[0] & 0x7fffffffUL);
  425. seq_put_decimal_ull(m, ' ', wchan);
  426. seq_put_decimal_ull(m, ' ', 0);
  427. seq_put_decimal_ull(m, ' ', 0);
  428. seq_put_decimal_ll(m, ' ', task->exit_signal);
  429. seq_put_decimal_ll(m, ' ', task_cpu(task));
  430. seq_put_decimal_ull(m, ' ', task->rt_priority);
  431. seq_put_decimal_ull(m, ' ', task->policy);
  432. seq_put_decimal_ull(m, ' ', delayacct_blkio_ticks(task));
  433. seq_put_decimal_ull(m, ' ', cputime_to_clock_t(gtime));
  434. seq_put_decimal_ll(m, ' ', cputime_to_clock_t(cgtime));
  435. if (mm && permitted) {
  436. seq_put_decimal_ull(m, ' ', mm->start_data);
  437. seq_put_decimal_ull(m, ' ', mm->end_data);
  438. seq_put_decimal_ull(m, ' ', mm->start_brk);
  439. seq_put_decimal_ull(m, ' ', mm->arg_start);
  440. seq_put_decimal_ull(m, ' ', mm->arg_end);
  441. seq_put_decimal_ull(m, ' ', mm->env_start);
  442. seq_put_decimal_ull(m, ' ', mm->env_end);
  443. } else
  444. seq_printf(m, " 0 0 0 0 0 0 0");
  445. if (permitted)
  446. seq_put_decimal_ll(m, ' ', task->exit_code);
  447. else
  448. seq_put_decimal_ll(m, ' ', 0);
  449. seq_putc(m, '\n');
  450. if (mm)
  451. mmput(mm);
  452. return 0;
  453. }
  454. int proc_tid_stat(struct seq_file *m, struct pid_namespace *ns,
  455. struct pid *pid, struct task_struct *task)
  456. {
  457. return do_task_stat(m, ns, pid, task, 0);
  458. }
  459. int proc_tgid_stat(struct seq_file *m, struct pid_namespace *ns,
  460. struct pid *pid, struct task_struct *task)
  461. {
  462. return do_task_stat(m, ns, pid, task, 1);
  463. }
  464. int proc_pid_statm(struct seq_file *m, struct pid_namespace *ns,
  465. struct pid *pid, struct task_struct *task)
  466. {
  467. unsigned long size = 0, resident = 0, shared = 0, text = 0, data = 0;
  468. struct mm_struct *mm = get_task_mm(task);
  469. if (mm) {
  470. size = task_statm(mm, &shared, &text, &data, &resident);
  471. mmput(mm);
  472. }
  473. /*
  474. * For quick read, open code by putting numbers directly
  475. * expected format is
  476. * seq_printf(m, "%lu %lu %lu %lu 0 %lu 0\n",
  477. * size, resident, shared, text, data);
  478. */
  479. seq_put_decimal_ull(m, 0, size);
  480. seq_put_decimal_ull(m, ' ', resident);
  481. seq_put_decimal_ull(m, ' ', shared);
  482. seq_put_decimal_ull(m, ' ', text);
  483. seq_put_decimal_ull(m, ' ', 0);
  484. seq_put_decimal_ull(m, ' ', data);
  485. seq_put_decimal_ull(m, ' ', 0);
  486. seq_putc(m, '\n');
  487. return 0;
  488. }
  489. #ifdef CONFIG_CHECKPOINT_RESTORE
  490. static struct pid *
  491. get_children_pid(struct inode *inode, struct pid *pid_prev, loff_t pos)
  492. {
  493. struct task_struct *start, *task;
  494. struct pid *pid = NULL;
  495. read_lock(&tasklist_lock);
  496. start = pid_task(proc_pid(inode), PIDTYPE_PID);
  497. if (!start)
  498. goto out;
  499. /*
  500. * Lets try to continue searching first, this gives
  501. * us significant speedup on children-rich processes.
  502. */
  503. if (pid_prev) {
  504. task = pid_task(pid_prev, PIDTYPE_PID);
  505. if (task && task->real_parent == start &&
  506. !(list_empty(&task->sibling))) {
  507. if (list_is_last(&task->sibling, &start->children))
  508. goto out;
  509. task = list_first_entry(&task->sibling,
  510. struct task_struct, sibling);
  511. pid = get_pid(task_pid(task));
  512. goto out;
  513. }
  514. }
  515. /*
  516. * Slow search case.
  517. *
  518. * We might miss some children here if children
  519. * are exited while we were not holding the lock,
  520. * but it was never promised to be accurate that
  521. * much.
  522. *
  523. * "Just suppose that the parent sleeps, but N children
  524. * exit after we printed their tids. Now the slow paths
  525. * skips N extra children, we miss N tasks." (c)
  526. *
  527. * So one need to stop or freeze the leader and all
  528. * its children to get a precise result.
  529. */
  530. list_for_each_entry(task, &start->children, sibling) {
  531. if (pos-- == 0) {
  532. pid = get_pid(task_pid(task));
  533. break;
  534. }
  535. }
  536. out:
  537. read_unlock(&tasklist_lock);
  538. return pid;
  539. }
  540. static int children_seq_show(struct seq_file *seq, void *v)
  541. {
  542. struct inode *inode = seq->private;
  543. pid_t pid;
  544. pid = pid_nr_ns(v, inode->i_sb->s_fs_info);
  545. return seq_printf(seq, "%d ", pid);
  546. }
  547. static void *children_seq_start(struct seq_file *seq, loff_t *pos)
  548. {
  549. return get_children_pid(seq->private, NULL, *pos);
  550. }
  551. static void *children_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  552. {
  553. struct pid *pid;
  554. pid = get_children_pid(seq->private, v, *pos + 1);
  555. put_pid(v);
  556. ++*pos;
  557. return pid;
  558. }
  559. static void children_seq_stop(struct seq_file *seq, void *v)
  560. {
  561. put_pid(v);
  562. }
  563. static const struct seq_operations children_seq_ops = {
  564. .start = children_seq_start,
  565. .next = children_seq_next,
  566. .stop = children_seq_stop,
  567. .show = children_seq_show,
  568. };
  569. static int children_seq_open(struct inode *inode, struct file *file)
  570. {
  571. struct seq_file *m;
  572. int ret;
  573. ret = seq_open(file, &children_seq_ops);
  574. if (ret)
  575. return ret;
  576. m = file->private_data;
  577. m->private = inode;
  578. return ret;
  579. }
  580. int children_seq_release(struct inode *inode, struct file *file)
  581. {
  582. seq_release(inode, file);
  583. return 0;
  584. }
  585. const struct file_operations proc_tid_children_operations = {
  586. .open = children_seq_open,
  587. .read = seq_read,
  588. .llseek = seq_lseek,
  589. .release = children_seq_release,
  590. };
  591. #endif /* CONFIG_CHECKPOINT_RESTORE */