]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - fs/proc/array.c
proc: Provide details on speculation flaw mitigations
[mirror_ubuntu-artful-kernel.git] / fs / proc / array.c
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
55 #include <linux/types.h>
56 #include <linux/errno.h>
57 #include <linux/time.h>
58 #include <linux/kernel.h>
59 #include <linux/kernel_stat.h>
60 #include <linux/tty.h>
61 #include <linux/string.h>
62 #include <linux/mman.h>
63 #include <linux/sched/mm.h>
64 #include <linux/sched/numa_balancing.h>
65 #include <linux/sched/task_stack.h>
66 #include <linux/sched/task.h>
67 #include <linux/sched/cputime.h>
68 #include <linux/proc_fs.h>
69 #include <linux/ioport.h>
70 #include <linux/uaccess.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/nospec.h>
88 #include <linux/prctl.h>
89 #include <linux/ptrace.h>
90 #include <linux/tracehook.h>
91 #include <linux/string_helpers.h>
92 #include <linux/user_namespace.h>
93 #include <linux/fs_struct.h>
94
95 #include <asm/pgtable.h>
96 #include <asm/processor.h>
97 #include "internal.h"
98
99 static inline void task_name(struct seq_file *m, struct task_struct *p)
100 {
101 char *buf;
102 size_t size;
103 char tcomm[sizeof(p->comm)];
104 int ret;
105
106 get_task_comm(tcomm, p);
107
108 seq_puts(m, "Name:\t");
109
110 size = seq_get_buf(m, &buf);
111 ret = string_escape_str(tcomm, buf, size, ESCAPE_SPACE | ESCAPE_SPECIAL, "\n\\");
112 seq_commit(m, ret < size ? ret : -1);
113
114 seq_putc(m, '\n');
115 }
116
117 /*
118 * The task state array is a strange "bitmap" of
119 * reasons to sleep. Thus "running" is zero, and
120 * you can test for combinations of others with
121 * simple bit tests.
122 */
123 static const char * const task_state_array[] = {
124 "R (running)", /* 0 */
125 "S (sleeping)", /* 1 */
126 "D (disk sleep)", /* 2 */
127 "T (stopped)", /* 4 */
128 "t (tracing stop)", /* 8 */
129 "X (dead)", /* 16 */
130 "Z (zombie)", /* 32 */
131 };
132
133 static inline const char *get_task_state(struct task_struct *tsk)
134 {
135 unsigned int state = (tsk->state | tsk->exit_state) & TASK_REPORT;
136
137 /*
138 * Parked tasks do not run; they sit in __kthread_parkme().
139 * Without this check, we would report them as running, which is
140 * clearly wrong, so we report them as sleeping instead.
141 */
142 if (tsk->state == TASK_PARKED)
143 state = TASK_INTERRUPTIBLE;
144
145 BUILD_BUG_ON(1 + ilog2(TASK_REPORT) != ARRAY_SIZE(task_state_array)-1);
146
147 return task_state_array[fls(state)];
148 }
149
150 static inline int get_task_umask(struct task_struct *tsk)
151 {
152 struct fs_struct *fs;
153 int umask = -ENOENT;
154
155 task_lock(tsk);
156 fs = tsk->fs;
157 if (fs)
158 umask = fs->umask;
159 task_unlock(tsk);
160 return umask;
161 }
162
163 static inline void task_state(struct seq_file *m, struct pid_namespace *ns,
164 struct pid *pid, struct task_struct *p)
165 {
166 struct user_namespace *user_ns = seq_user_ns(m);
167 struct group_info *group_info;
168 int g, umask;
169 struct task_struct *tracer;
170 const struct cred *cred;
171 pid_t ppid, tpid = 0, tgid, ngid;
172 unsigned int max_fds = 0;
173
174 rcu_read_lock();
175 ppid = pid_alive(p) ?
176 task_tgid_nr_ns(rcu_dereference(p->real_parent), ns) : 0;
177
178 tracer = ptrace_parent(p);
179 if (tracer)
180 tpid = task_pid_nr_ns(tracer, ns);
181
182 tgid = task_tgid_nr_ns(p, ns);
183 ngid = task_numa_group_id(p);
184 cred = get_task_cred(p);
185
186 umask = get_task_umask(p);
187 if (umask >= 0)
188 seq_printf(m, "Umask:\t%#04o\n", umask);
189
190 task_lock(p);
191 if (p->files)
192 max_fds = files_fdtable(p->files)->max_fds;
193 task_unlock(p);
194 rcu_read_unlock();
195
196 seq_printf(m, "State:\t%s", get_task_state(p));
197
198 seq_put_decimal_ull(m, "\nTgid:\t", tgid);
199 seq_put_decimal_ull(m, "\nNgid:\t", ngid);
200 seq_put_decimal_ull(m, "\nPid:\t", pid_nr_ns(pid, ns));
201 seq_put_decimal_ull(m, "\nPPid:\t", ppid);
202 seq_put_decimal_ull(m, "\nTracerPid:\t", tpid);
203 seq_put_decimal_ull(m, "\nUid:\t", from_kuid_munged(user_ns, cred->uid));
204 seq_put_decimal_ull(m, "\t", from_kuid_munged(user_ns, cred->euid));
205 seq_put_decimal_ull(m, "\t", from_kuid_munged(user_ns, cred->suid));
206 seq_put_decimal_ull(m, "\t", from_kuid_munged(user_ns, cred->fsuid));
207 seq_put_decimal_ull(m, "\nGid:\t", from_kgid_munged(user_ns, cred->gid));
208 seq_put_decimal_ull(m, "\t", from_kgid_munged(user_ns, cred->egid));
209 seq_put_decimal_ull(m, "\t", from_kgid_munged(user_ns, cred->sgid));
210 seq_put_decimal_ull(m, "\t", from_kgid_munged(user_ns, cred->fsgid));
211 seq_put_decimal_ull(m, "\nFDSize:\t", max_fds);
212
213 seq_puts(m, "\nGroups:\t");
214 group_info = cred->group_info;
215 for (g = 0; g < group_info->ngroups; g++)
216 seq_put_decimal_ull(m, g ? " " : "",
217 from_kgid_munged(user_ns, group_info->gid[g]));
218 put_cred(cred);
219 /* Trailing space shouldn't have been added in the first place. */
220 seq_putc(m, ' ');
221
222 #ifdef CONFIG_PID_NS
223 seq_puts(m, "\nNStgid:");
224 for (g = ns->level; g <= pid->level; g++)
225 seq_put_decimal_ull(m, "\t", task_tgid_nr_ns(p, pid->numbers[g].ns));
226 seq_puts(m, "\nNSpid:");
227 for (g = ns->level; g <= pid->level; g++)
228 seq_put_decimal_ull(m, "\t", task_pid_nr_ns(p, pid->numbers[g].ns));
229 seq_puts(m, "\nNSpgid:");
230 for (g = ns->level; g <= pid->level; g++)
231 seq_put_decimal_ull(m, "\t", task_pgrp_nr_ns(p, pid->numbers[g].ns));
232 seq_puts(m, "\nNSsid:");
233 for (g = ns->level; g <= pid->level; g++)
234 seq_put_decimal_ull(m, "\t", task_session_nr_ns(p, pid->numbers[g].ns));
235 #endif
236 seq_putc(m, '\n');
237 }
238
239 void render_sigset_t(struct seq_file *m, const char *header,
240 sigset_t *set)
241 {
242 int i;
243
244 seq_puts(m, header);
245
246 i = _NSIG;
247 do {
248 int x = 0;
249
250 i -= 4;
251 if (sigismember(set, i+1)) x |= 1;
252 if (sigismember(set, i+2)) x |= 2;
253 if (sigismember(set, i+3)) x |= 4;
254 if (sigismember(set, i+4)) x |= 8;
255 seq_putc(m, hex_asc[x]);
256 } while (i >= 4);
257
258 seq_putc(m, '\n');
259 }
260
261 static void collect_sigign_sigcatch(struct task_struct *p, sigset_t *ign,
262 sigset_t *catch)
263 {
264 struct k_sigaction *k;
265 int i;
266
267 k = p->sighand->action;
268 for (i = 1; i <= _NSIG; ++i, ++k) {
269 if (k->sa.sa_handler == SIG_IGN)
270 sigaddset(ign, i);
271 else if (k->sa.sa_handler != SIG_DFL)
272 sigaddset(catch, i);
273 }
274 }
275
276 static inline void task_sig(struct seq_file *m, struct task_struct *p)
277 {
278 unsigned long flags;
279 sigset_t pending, shpending, blocked, ignored, caught;
280 int num_threads = 0;
281 unsigned long qsize = 0;
282 unsigned long qlim = 0;
283
284 sigemptyset(&pending);
285 sigemptyset(&shpending);
286 sigemptyset(&blocked);
287 sigemptyset(&ignored);
288 sigemptyset(&caught);
289
290 if (lock_task_sighand(p, &flags)) {
291 pending = p->pending.signal;
292 shpending = p->signal->shared_pending.signal;
293 blocked = p->blocked;
294 collect_sigign_sigcatch(p, &ignored, &caught);
295 num_threads = get_nr_threads(p);
296 rcu_read_lock(); /* FIXME: is this correct? */
297 qsize = atomic_read(&__task_cred(p)->user->sigpending);
298 rcu_read_unlock();
299 qlim = task_rlimit(p, RLIMIT_SIGPENDING);
300 unlock_task_sighand(p, &flags);
301 }
302
303 seq_put_decimal_ull(m, "Threads:\t", num_threads);
304 seq_put_decimal_ull(m, "\nSigQ:\t", qsize);
305 seq_put_decimal_ull(m, "/", qlim);
306
307 /* render them all */
308 render_sigset_t(m, "\nSigPnd:\t", &pending);
309 render_sigset_t(m, "ShdPnd:\t", &shpending);
310 render_sigset_t(m, "SigBlk:\t", &blocked);
311 render_sigset_t(m, "SigIgn:\t", &ignored);
312 render_sigset_t(m, "SigCgt:\t", &caught);
313 }
314
315 static void render_cap_t(struct seq_file *m, const char *header,
316 kernel_cap_t *a)
317 {
318 unsigned __capi;
319
320 seq_puts(m, header);
321 CAP_FOR_EACH_U32(__capi) {
322 seq_printf(m, "%08x",
323 a->cap[CAP_LAST_U32 - __capi]);
324 }
325 seq_putc(m, '\n');
326 }
327
328 static inline void task_cap(struct seq_file *m, struct task_struct *p)
329 {
330 const struct cred *cred;
331 kernel_cap_t cap_inheritable, cap_permitted, cap_effective,
332 cap_bset, cap_ambient;
333
334 rcu_read_lock();
335 cred = __task_cred(p);
336 cap_inheritable = cred->cap_inheritable;
337 cap_permitted = cred->cap_permitted;
338 cap_effective = cred->cap_effective;
339 cap_bset = cred->cap_bset;
340 cap_ambient = cred->cap_ambient;
341 rcu_read_unlock();
342
343 render_cap_t(m, "CapInh:\t", &cap_inheritable);
344 render_cap_t(m, "CapPrm:\t", &cap_permitted);
345 render_cap_t(m, "CapEff:\t", &cap_effective);
346 render_cap_t(m, "CapBnd:\t", &cap_bset);
347 render_cap_t(m, "CapAmb:\t", &cap_ambient);
348 }
349
350 static inline void task_seccomp(struct seq_file *m, struct task_struct *p)
351 {
352 seq_put_decimal_ull(m, "NoNewPrivs:\t", task_no_new_privs(p));
353 #ifdef CONFIG_SECCOMP
354 seq_put_decimal_ull(m, "\nSeccomp:\t", p->seccomp.mode);
355 #endif
356 seq_printf(m, "\nSpeculation Store Bypass:\t");
357 switch (arch_prctl_spec_ctrl_get(p, PR_SPEC_STORE_BYPASS)) {
358 case -EINVAL:
359 seq_printf(m, "unknown");
360 break;
361 case PR_SPEC_NOT_AFFECTED:
362 seq_printf(m, "not vulnerable");
363 break;
364 case PR_SPEC_PRCTL | PR_SPEC_DISABLE:
365 seq_printf(m, "thread mitigated");
366 break;
367 case PR_SPEC_PRCTL | PR_SPEC_ENABLE:
368 seq_printf(m, "thread vulnerable");
369 break;
370 case PR_SPEC_DISABLE:
371 seq_printf(m, "globally mitigated");
372 break;
373 default:
374 seq_printf(m, "vulnerable");
375 break;
376 }
377 seq_putc(m, '\n');
378 }
379
380 static inline void task_context_switch_counts(struct seq_file *m,
381 struct task_struct *p)
382 {
383 seq_put_decimal_ull(m, "voluntary_ctxt_switches:\t", p->nvcsw);
384 seq_put_decimal_ull(m, "\nnonvoluntary_ctxt_switches:\t", p->nivcsw);
385 seq_putc(m, '\n');
386 }
387
388 static void task_cpus_allowed(struct seq_file *m, struct task_struct *task)
389 {
390 seq_printf(m, "Cpus_allowed:\t%*pb\n",
391 cpumask_pr_args(&task->cpus_allowed));
392 seq_printf(m, "Cpus_allowed_list:\t%*pbl\n",
393 cpumask_pr_args(&task->cpus_allowed));
394 }
395
396 int proc_pid_status(struct seq_file *m, struct pid_namespace *ns,
397 struct pid *pid, struct task_struct *task)
398 {
399 struct mm_struct *mm = get_task_mm(task);
400
401 task_name(m, task);
402 task_state(m, ns, pid, task);
403
404 if (mm) {
405 task_mem(m, mm);
406 mmput(mm);
407 }
408 task_sig(m, task);
409 task_cap(m, task);
410 task_seccomp(m, task);
411 task_cpus_allowed(m, task);
412 cpuset_task_status_allowed(m, task);
413 task_context_switch_counts(m, task);
414 return 0;
415 }
416
417 static int do_task_stat(struct seq_file *m, struct pid_namespace *ns,
418 struct pid *pid, struct task_struct *task, int whole)
419 {
420 unsigned long vsize, eip, esp, wchan = 0;
421 int priority, nice;
422 int tty_pgrp = -1, tty_nr = 0;
423 sigset_t sigign, sigcatch;
424 char state;
425 pid_t ppid = 0, pgid = -1, sid = -1;
426 int num_threads = 0;
427 int permitted;
428 struct mm_struct *mm;
429 unsigned long long start_time;
430 unsigned long cmin_flt = 0, cmaj_flt = 0;
431 unsigned long min_flt = 0, maj_flt = 0;
432 u64 cutime, cstime, utime, stime;
433 u64 cgtime, gtime;
434 unsigned long rsslim = 0;
435 char tcomm[sizeof(task->comm)];
436 unsigned long flags;
437
438 state = *get_task_state(task);
439 vsize = eip = esp = 0;
440 permitted = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS | PTRACE_MODE_NOAUDIT);
441 mm = get_task_mm(task);
442 if (mm) {
443 vsize = task_vsize(mm);
444 /*
445 * esp and eip are intentionally zeroed out. There is no
446 * non-racy way to read them without freezing the task.
447 * Programs that need reliable values can use ptrace(2).
448 *
449 * The only exception is if the task is core dumping because
450 * a program is not able to use ptrace(2) in that case. It is
451 * safe because the task has stopped executing permanently.
452 */
453 if (permitted && (task->flags & PF_DUMPCORE)) {
454 eip = KSTK_EIP(task);
455 esp = KSTK_ESP(task);
456 }
457 }
458
459 get_task_comm(tcomm, task);
460
461 sigemptyset(&sigign);
462 sigemptyset(&sigcatch);
463 cutime = cstime = utime = stime = 0;
464 cgtime = gtime = 0;
465
466 if (lock_task_sighand(task, &flags)) {
467 struct signal_struct *sig = task->signal;
468
469 if (sig->tty) {
470 struct pid *pgrp = tty_get_pgrp(sig->tty);
471 tty_pgrp = pid_nr_ns(pgrp, ns);
472 put_pid(pgrp);
473 tty_nr = new_encode_dev(tty_devnum(sig->tty));
474 }
475
476 num_threads = get_nr_threads(task);
477 collect_sigign_sigcatch(task, &sigign, &sigcatch);
478
479 cmin_flt = sig->cmin_flt;
480 cmaj_flt = sig->cmaj_flt;
481 cutime = sig->cutime;
482 cstime = sig->cstime;
483 cgtime = sig->cgtime;
484 rsslim = ACCESS_ONCE(sig->rlim[RLIMIT_RSS].rlim_cur);
485
486 /* add up live thread stats at the group level */
487 if (whole) {
488 struct task_struct *t = task;
489 do {
490 min_flt += t->min_flt;
491 maj_flt += t->maj_flt;
492 gtime += task_gtime(t);
493 } while_each_thread(task, t);
494
495 min_flt += sig->min_flt;
496 maj_flt += sig->maj_flt;
497 thread_group_cputime_adjusted(task, &utime, &stime);
498 gtime += sig->gtime;
499 }
500
501 sid = task_session_nr_ns(task, ns);
502 ppid = task_tgid_nr_ns(task->real_parent, ns);
503 pgid = task_pgrp_nr_ns(task, ns);
504
505 unlock_task_sighand(task, &flags);
506 }
507
508 if (permitted && (!whole || num_threads < 2))
509 wchan = get_wchan(task);
510 if (!whole) {
511 min_flt = task->min_flt;
512 maj_flt = task->maj_flt;
513 task_cputime_adjusted(task, &utime, &stime);
514 gtime = task_gtime(task);
515 }
516
517 /* scale priority and nice values from timeslices to -20..20 */
518 /* to make it look like a "normal" Unix priority/nice value */
519 priority = task_prio(task);
520 nice = task_nice(task);
521
522 /* convert nsec -> ticks */
523 start_time = nsec_to_clock_t(task->real_start_time);
524
525 seq_printf(m, "%d (%s) %c", pid_nr_ns(pid, ns), tcomm, state);
526 seq_put_decimal_ll(m, " ", ppid);
527 seq_put_decimal_ll(m, " ", pgid);
528 seq_put_decimal_ll(m, " ", sid);
529 seq_put_decimal_ll(m, " ", tty_nr);
530 seq_put_decimal_ll(m, " ", tty_pgrp);
531 seq_put_decimal_ull(m, " ", task->flags);
532 seq_put_decimal_ull(m, " ", min_flt);
533 seq_put_decimal_ull(m, " ", cmin_flt);
534 seq_put_decimal_ull(m, " ", maj_flt);
535 seq_put_decimal_ull(m, " ", cmaj_flt);
536 seq_put_decimal_ull(m, " ", nsec_to_clock_t(utime));
537 seq_put_decimal_ull(m, " ", nsec_to_clock_t(stime));
538 seq_put_decimal_ll(m, " ", nsec_to_clock_t(cutime));
539 seq_put_decimal_ll(m, " ", nsec_to_clock_t(cstime));
540 seq_put_decimal_ll(m, " ", priority);
541 seq_put_decimal_ll(m, " ", nice);
542 seq_put_decimal_ll(m, " ", num_threads);
543 seq_put_decimal_ull(m, " ", 0);
544 seq_put_decimal_ull(m, " ", start_time);
545 seq_put_decimal_ull(m, " ", vsize);
546 seq_put_decimal_ull(m, " ", mm ? get_mm_rss(mm) : 0);
547 seq_put_decimal_ull(m, " ", rsslim);
548 seq_put_decimal_ull(m, " ", mm ? (permitted ? mm->start_code : 1) : 0);
549 seq_put_decimal_ull(m, " ", mm ? (permitted ? mm->end_code : 1) : 0);
550 seq_put_decimal_ull(m, " ", (permitted && mm) ? mm->start_stack : 0);
551 seq_put_decimal_ull(m, " ", esp);
552 seq_put_decimal_ull(m, " ", eip);
553 /* The signal information here is obsolete.
554 * It must be decimal for Linux 2.0 compatibility.
555 * Use /proc/#/status for real-time signals.
556 */
557 seq_put_decimal_ull(m, " ", task->pending.signal.sig[0] & 0x7fffffffUL);
558 seq_put_decimal_ull(m, " ", task->blocked.sig[0] & 0x7fffffffUL);
559 seq_put_decimal_ull(m, " ", sigign.sig[0] & 0x7fffffffUL);
560 seq_put_decimal_ull(m, " ", sigcatch.sig[0] & 0x7fffffffUL);
561
562 /*
563 * We used to output the absolute kernel address, but that's an
564 * information leak - so instead we show a 0/1 flag here, to signal
565 * to user-space whether there's a wchan field in /proc/PID/wchan.
566 *
567 * This works with older implementations of procps as well.
568 */
569 if (wchan)
570 seq_puts(m, " 1");
571 else
572 seq_puts(m, " 0");
573
574 seq_put_decimal_ull(m, " ", 0);
575 seq_put_decimal_ull(m, " ", 0);
576 seq_put_decimal_ll(m, " ", task->exit_signal);
577 seq_put_decimal_ll(m, " ", task_cpu(task));
578 seq_put_decimal_ull(m, " ", task->rt_priority);
579 seq_put_decimal_ull(m, " ", task->policy);
580 seq_put_decimal_ull(m, " ", delayacct_blkio_ticks(task));
581 seq_put_decimal_ull(m, " ", nsec_to_clock_t(gtime));
582 seq_put_decimal_ll(m, " ", nsec_to_clock_t(cgtime));
583
584 if (mm && permitted) {
585 seq_put_decimal_ull(m, " ", mm->start_data);
586 seq_put_decimal_ull(m, " ", mm->end_data);
587 seq_put_decimal_ull(m, " ", mm->start_brk);
588 seq_put_decimal_ull(m, " ", mm->arg_start);
589 seq_put_decimal_ull(m, " ", mm->arg_end);
590 seq_put_decimal_ull(m, " ", mm->env_start);
591 seq_put_decimal_ull(m, " ", mm->env_end);
592 } else
593 seq_puts(m, " 0 0 0 0 0 0 0");
594
595 if (permitted)
596 seq_put_decimal_ll(m, " ", task->exit_code);
597 else
598 seq_puts(m, " 0");
599
600 seq_putc(m, '\n');
601 if (mm)
602 mmput(mm);
603 return 0;
604 }
605
606 int proc_tid_stat(struct seq_file *m, struct pid_namespace *ns,
607 struct pid *pid, struct task_struct *task)
608 {
609 return do_task_stat(m, ns, pid, task, 0);
610 }
611
612 int proc_tgid_stat(struct seq_file *m, struct pid_namespace *ns,
613 struct pid *pid, struct task_struct *task)
614 {
615 return do_task_stat(m, ns, pid, task, 1);
616 }
617
618 int proc_pid_statm(struct seq_file *m, struct pid_namespace *ns,
619 struct pid *pid, struct task_struct *task)
620 {
621 unsigned long size = 0, resident = 0, shared = 0, text = 0, data = 0;
622 struct mm_struct *mm = get_task_mm(task);
623
624 if (mm) {
625 size = task_statm(mm, &shared, &text, &data, &resident);
626 mmput(mm);
627 }
628 /*
629 * For quick read, open code by putting numbers directly
630 * expected format is
631 * seq_printf(m, "%lu %lu %lu %lu 0 %lu 0\n",
632 * size, resident, shared, text, data);
633 */
634 seq_put_decimal_ull(m, "", size);
635 seq_put_decimal_ull(m, " ", resident);
636 seq_put_decimal_ull(m, " ", shared);
637 seq_put_decimal_ull(m, " ", text);
638 seq_put_decimal_ull(m, " ", 0);
639 seq_put_decimal_ull(m, " ", data);
640 seq_put_decimal_ull(m, " ", 0);
641 seq_putc(m, '\n');
642
643 return 0;
644 }
645
646 #ifdef CONFIG_PROC_CHILDREN
647 static struct pid *
648 get_children_pid(struct inode *inode, struct pid *pid_prev, loff_t pos)
649 {
650 struct task_struct *start, *task;
651 struct pid *pid = NULL;
652
653 read_lock(&tasklist_lock);
654
655 start = pid_task(proc_pid(inode), PIDTYPE_PID);
656 if (!start)
657 goto out;
658
659 /*
660 * Lets try to continue searching first, this gives
661 * us significant speedup on children-rich processes.
662 */
663 if (pid_prev) {
664 task = pid_task(pid_prev, PIDTYPE_PID);
665 if (task && task->real_parent == start &&
666 !(list_empty(&task->sibling))) {
667 if (list_is_last(&task->sibling, &start->children))
668 goto out;
669 task = list_first_entry(&task->sibling,
670 struct task_struct, sibling);
671 pid = get_pid(task_pid(task));
672 goto out;
673 }
674 }
675
676 /*
677 * Slow search case.
678 *
679 * We might miss some children here if children
680 * are exited while we were not holding the lock,
681 * but it was never promised to be accurate that
682 * much.
683 *
684 * "Just suppose that the parent sleeps, but N children
685 * exit after we printed their tids. Now the slow paths
686 * skips N extra children, we miss N tasks." (c)
687 *
688 * So one need to stop or freeze the leader and all
689 * its children to get a precise result.
690 */
691 list_for_each_entry(task, &start->children, sibling) {
692 if (pos-- == 0) {
693 pid = get_pid(task_pid(task));
694 break;
695 }
696 }
697
698 out:
699 read_unlock(&tasklist_lock);
700 return pid;
701 }
702
703 static int children_seq_show(struct seq_file *seq, void *v)
704 {
705 struct inode *inode = seq->private;
706 pid_t pid;
707
708 pid = pid_nr_ns(v, inode->i_sb->s_fs_info);
709 seq_printf(seq, "%d ", pid);
710
711 return 0;
712 }
713
714 static void *children_seq_start(struct seq_file *seq, loff_t *pos)
715 {
716 return get_children_pid(seq->private, NULL, *pos);
717 }
718
719 static void *children_seq_next(struct seq_file *seq, void *v, loff_t *pos)
720 {
721 struct pid *pid;
722
723 pid = get_children_pid(seq->private, v, *pos + 1);
724 put_pid(v);
725
726 ++*pos;
727 return pid;
728 }
729
730 static void children_seq_stop(struct seq_file *seq, void *v)
731 {
732 put_pid(v);
733 }
734
735 static const struct seq_operations children_seq_ops = {
736 .start = children_seq_start,
737 .next = children_seq_next,
738 .stop = children_seq_stop,
739 .show = children_seq_show,
740 };
741
742 static int children_seq_open(struct inode *inode, struct file *file)
743 {
744 struct seq_file *m;
745 int ret;
746
747 ret = seq_open(file, &children_seq_ops);
748 if (ret)
749 return ret;
750
751 m = file->private_data;
752 m->private = inode;
753
754 return ret;
755 }
756
757 int children_seq_release(struct inode *inode, struct file *file)
758 {
759 seq_release(inode, file);
760 return 0;
761 }
762
763 const struct file_operations proc_tid_children_operations = {
764 .open = children_seq_open,
765 .read = seq_read,
766 .llseek = seq_lseek,
767 .release = children_seq_release,
768 };
769 #endif /* CONFIG_PROC_CHILDREN */