]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - fs/proc/array.c
prctl: Add force disable speculation
[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_FORCE_DISABLE:
365 seq_printf(m, "thread force mitigated");
366 break;
367 case PR_SPEC_PRCTL | PR_SPEC_DISABLE:
368 seq_printf(m, "thread mitigated");
369 break;
370 case PR_SPEC_PRCTL | PR_SPEC_ENABLE:
371 seq_printf(m, "thread vulnerable");
372 break;
373 case PR_SPEC_DISABLE:
374 seq_printf(m, "globally mitigated");
375 break;
376 default:
377 seq_printf(m, "vulnerable");
378 break;
379 }
380 seq_putc(m, '\n');
381 }
382
383 static inline void task_context_switch_counts(struct seq_file *m,
384 struct task_struct *p)
385 {
386 seq_put_decimal_ull(m, "voluntary_ctxt_switches:\t", p->nvcsw);
387 seq_put_decimal_ull(m, "\nnonvoluntary_ctxt_switches:\t", p->nivcsw);
388 seq_putc(m, '\n');
389 }
390
391 static void task_cpus_allowed(struct seq_file *m, struct task_struct *task)
392 {
393 seq_printf(m, "Cpus_allowed:\t%*pb\n",
394 cpumask_pr_args(&task->cpus_allowed));
395 seq_printf(m, "Cpus_allowed_list:\t%*pbl\n",
396 cpumask_pr_args(&task->cpus_allowed));
397 }
398
399 int proc_pid_status(struct seq_file *m, struct pid_namespace *ns,
400 struct pid *pid, struct task_struct *task)
401 {
402 struct mm_struct *mm = get_task_mm(task);
403
404 task_name(m, task);
405 task_state(m, ns, pid, task);
406
407 if (mm) {
408 task_mem(m, mm);
409 mmput(mm);
410 }
411 task_sig(m, task);
412 task_cap(m, task);
413 task_seccomp(m, task);
414 task_cpus_allowed(m, task);
415 cpuset_task_status_allowed(m, task);
416 task_context_switch_counts(m, task);
417 return 0;
418 }
419
420 static int do_task_stat(struct seq_file *m, struct pid_namespace *ns,
421 struct pid *pid, struct task_struct *task, int whole)
422 {
423 unsigned long vsize, eip, esp, wchan = 0;
424 int priority, nice;
425 int tty_pgrp = -1, tty_nr = 0;
426 sigset_t sigign, sigcatch;
427 char state;
428 pid_t ppid = 0, pgid = -1, sid = -1;
429 int num_threads = 0;
430 int permitted;
431 struct mm_struct *mm;
432 unsigned long long start_time;
433 unsigned long cmin_flt = 0, cmaj_flt = 0;
434 unsigned long min_flt = 0, maj_flt = 0;
435 u64 cutime, cstime, utime, stime;
436 u64 cgtime, gtime;
437 unsigned long rsslim = 0;
438 char tcomm[sizeof(task->comm)];
439 unsigned long flags;
440
441 state = *get_task_state(task);
442 vsize = eip = esp = 0;
443 permitted = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS | PTRACE_MODE_NOAUDIT);
444 mm = get_task_mm(task);
445 if (mm) {
446 vsize = task_vsize(mm);
447 /*
448 * esp and eip are intentionally zeroed out. There is no
449 * non-racy way to read them without freezing the task.
450 * Programs that need reliable values can use ptrace(2).
451 *
452 * The only exception is if the task is core dumping because
453 * a program is not able to use ptrace(2) in that case. It is
454 * safe because the task has stopped executing permanently.
455 */
456 if (permitted && (task->flags & PF_DUMPCORE)) {
457 eip = KSTK_EIP(task);
458 esp = KSTK_ESP(task);
459 }
460 }
461
462 get_task_comm(tcomm, task);
463
464 sigemptyset(&sigign);
465 sigemptyset(&sigcatch);
466 cutime = cstime = utime = stime = 0;
467 cgtime = gtime = 0;
468
469 if (lock_task_sighand(task, &flags)) {
470 struct signal_struct *sig = task->signal;
471
472 if (sig->tty) {
473 struct pid *pgrp = tty_get_pgrp(sig->tty);
474 tty_pgrp = pid_nr_ns(pgrp, ns);
475 put_pid(pgrp);
476 tty_nr = new_encode_dev(tty_devnum(sig->tty));
477 }
478
479 num_threads = get_nr_threads(task);
480 collect_sigign_sigcatch(task, &sigign, &sigcatch);
481
482 cmin_flt = sig->cmin_flt;
483 cmaj_flt = sig->cmaj_flt;
484 cutime = sig->cutime;
485 cstime = sig->cstime;
486 cgtime = sig->cgtime;
487 rsslim = ACCESS_ONCE(sig->rlim[RLIMIT_RSS].rlim_cur);
488
489 /* add up live thread stats at the group level */
490 if (whole) {
491 struct task_struct *t = task;
492 do {
493 min_flt += t->min_flt;
494 maj_flt += t->maj_flt;
495 gtime += task_gtime(t);
496 } while_each_thread(task, t);
497
498 min_flt += sig->min_flt;
499 maj_flt += sig->maj_flt;
500 thread_group_cputime_adjusted(task, &utime, &stime);
501 gtime += sig->gtime;
502 }
503
504 sid = task_session_nr_ns(task, ns);
505 ppid = task_tgid_nr_ns(task->real_parent, ns);
506 pgid = task_pgrp_nr_ns(task, ns);
507
508 unlock_task_sighand(task, &flags);
509 }
510
511 if (permitted && (!whole || num_threads < 2))
512 wchan = get_wchan(task);
513 if (!whole) {
514 min_flt = task->min_flt;
515 maj_flt = task->maj_flt;
516 task_cputime_adjusted(task, &utime, &stime);
517 gtime = task_gtime(task);
518 }
519
520 /* scale priority and nice values from timeslices to -20..20 */
521 /* to make it look like a "normal" Unix priority/nice value */
522 priority = task_prio(task);
523 nice = task_nice(task);
524
525 /* convert nsec -> ticks */
526 start_time = nsec_to_clock_t(task->real_start_time);
527
528 seq_printf(m, "%d (%s) %c", pid_nr_ns(pid, ns), tcomm, state);
529 seq_put_decimal_ll(m, " ", ppid);
530 seq_put_decimal_ll(m, " ", pgid);
531 seq_put_decimal_ll(m, " ", sid);
532 seq_put_decimal_ll(m, " ", tty_nr);
533 seq_put_decimal_ll(m, " ", tty_pgrp);
534 seq_put_decimal_ull(m, " ", task->flags);
535 seq_put_decimal_ull(m, " ", min_flt);
536 seq_put_decimal_ull(m, " ", cmin_flt);
537 seq_put_decimal_ull(m, " ", maj_flt);
538 seq_put_decimal_ull(m, " ", cmaj_flt);
539 seq_put_decimal_ull(m, " ", nsec_to_clock_t(utime));
540 seq_put_decimal_ull(m, " ", nsec_to_clock_t(stime));
541 seq_put_decimal_ll(m, " ", nsec_to_clock_t(cutime));
542 seq_put_decimal_ll(m, " ", nsec_to_clock_t(cstime));
543 seq_put_decimal_ll(m, " ", priority);
544 seq_put_decimal_ll(m, " ", nice);
545 seq_put_decimal_ll(m, " ", num_threads);
546 seq_put_decimal_ull(m, " ", 0);
547 seq_put_decimal_ull(m, " ", start_time);
548 seq_put_decimal_ull(m, " ", vsize);
549 seq_put_decimal_ull(m, " ", mm ? get_mm_rss(mm) : 0);
550 seq_put_decimal_ull(m, " ", rsslim);
551 seq_put_decimal_ull(m, " ", mm ? (permitted ? mm->start_code : 1) : 0);
552 seq_put_decimal_ull(m, " ", mm ? (permitted ? mm->end_code : 1) : 0);
553 seq_put_decimal_ull(m, " ", (permitted && mm) ? mm->start_stack : 0);
554 seq_put_decimal_ull(m, " ", esp);
555 seq_put_decimal_ull(m, " ", eip);
556 /* The signal information here is obsolete.
557 * It must be decimal for Linux 2.0 compatibility.
558 * Use /proc/#/status for real-time signals.
559 */
560 seq_put_decimal_ull(m, " ", task->pending.signal.sig[0] & 0x7fffffffUL);
561 seq_put_decimal_ull(m, " ", task->blocked.sig[0] & 0x7fffffffUL);
562 seq_put_decimal_ull(m, " ", sigign.sig[0] & 0x7fffffffUL);
563 seq_put_decimal_ull(m, " ", sigcatch.sig[0] & 0x7fffffffUL);
564
565 /*
566 * We used to output the absolute kernel address, but that's an
567 * information leak - so instead we show a 0/1 flag here, to signal
568 * to user-space whether there's a wchan field in /proc/PID/wchan.
569 *
570 * This works with older implementations of procps as well.
571 */
572 if (wchan)
573 seq_puts(m, " 1");
574 else
575 seq_puts(m, " 0");
576
577 seq_put_decimal_ull(m, " ", 0);
578 seq_put_decimal_ull(m, " ", 0);
579 seq_put_decimal_ll(m, " ", task->exit_signal);
580 seq_put_decimal_ll(m, " ", task_cpu(task));
581 seq_put_decimal_ull(m, " ", task->rt_priority);
582 seq_put_decimal_ull(m, " ", task->policy);
583 seq_put_decimal_ull(m, " ", delayacct_blkio_ticks(task));
584 seq_put_decimal_ull(m, " ", nsec_to_clock_t(gtime));
585 seq_put_decimal_ll(m, " ", nsec_to_clock_t(cgtime));
586
587 if (mm && permitted) {
588 seq_put_decimal_ull(m, " ", mm->start_data);
589 seq_put_decimal_ull(m, " ", mm->end_data);
590 seq_put_decimal_ull(m, " ", mm->start_brk);
591 seq_put_decimal_ull(m, " ", mm->arg_start);
592 seq_put_decimal_ull(m, " ", mm->arg_end);
593 seq_put_decimal_ull(m, " ", mm->env_start);
594 seq_put_decimal_ull(m, " ", mm->env_end);
595 } else
596 seq_puts(m, " 0 0 0 0 0 0 0");
597
598 if (permitted)
599 seq_put_decimal_ll(m, " ", task->exit_code);
600 else
601 seq_puts(m, " 0");
602
603 seq_putc(m, '\n');
604 if (mm)
605 mmput(mm);
606 return 0;
607 }
608
609 int proc_tid_stat(struct seq_file *m, struct pid_namespace *ns,
610 struct pid *pid, struct task_struct *task)
611 {
612 return do_task_stat(m, ns, pid, task, 0);
613 }
614
615 int proc_tgid_stat(struct seq_file *m, struct pid_namespace *ns,
616 struct pid *pid, struct task_struct *task)
617 {
618 return do_task_stat(m, ns, pid, task, 1);
619 }
620
621 int proc_pid_statm(struct seq_file *m, struct pid_namespace *ns,
622 struct pid *pid, struct task_struct *task)
623 {
624 unsigned long size = 0, resident = 0, shared = 0, text = 0, data = 0;
625 struct mm_struct *mm = get_task_mm(task);
626
627 if (mm) {
628 size = task_statm(mm, &shared, &text, &data, &resident);
629 mmput(mm);
630 }
631 /*
632 * For quick read, open code by putting numbers directly
633 * expected format is
634 * seq_printf(m, "%lu %lu %lu %lu 0 %lu 0\n",
635 * size, resident, shared, text, data);
636 */
637 seq_put_decimal_ull(m, "", size);
638 seq_put_decimal_ull(m, " ", resident);
639 seq_put_decimal_ull(m, " ", shared);
640 seq_put_decimal_ull(m, " ", text);
641 seq_put_decimal_ull(m, " ", 0);
642 seq_put_decimal_ull(m, " ", data);
643 seq_put_decimal_ull(m, " ", 0);
644 seq_putc(m, '\n');
645
646 return 0;
647 }
648
649 #ifdef CONFIG_PROC_CHILDREN
650 static struct pid *
651 get_children_pid(struct inode *inode, struct pid *pid_prev, loff_t pos)
652 {
653 struct task_struct *start, *task;
654 struct pid *pid = NULL;
655
656 read_lock(&tasklist_lock);
657
658 start = pid_task(proc_pid(inode), PIDTYPE_PID);
659 if (!start)
660 goto out;
661
662 /*
663 * Lets try to continue searching first, this gives
664 * us significant speedup on children-rich processes.
665 */
666 if (pid_prev) {
667 task = pid_task(pid_prev, PIDTYPE_PID);
668 if (task && task->real_parent == start &&
669 !(list_empty(&task->sibling))) {
670 if (list_is_last(&task->sibling, &start->children))
671 goto out;
672 task = list_first_entry(&task->sibling,
673 struct task_struct, sibling);
674 pid = get_pid(task_pid(task));
675 goto out;
676 }
677 }
678
679 /*
680 * Slow search case.
681 *
682 * We might miss some children here if children
683 * are exited while we were not holding the lock,
684 * but it was never promised to be accurate that
685 * much.
686 *
687 * "Just suppose that the parent sleeps, but N children
688 * exit after we printed their tids. Now the slow paths
689 * skips N extra children, we miss N tasks." (c)
690 *
691 * So one need to stop or freeze the leader and all
692 * its children to get a precise result.
693 */
694 list_for_each_entry(task, &start->children, sibling) {
695 if (pos-- == 0) {
696 pid = get_pid(task_pid(task));
697 break;
698 }
699 }
700
701 out:
702 read_unlock(&tasklist_lock);
703 return pid;
704 }
705
706 static int children_seq_show(struct seq_file *seq, void *v)
707 {
708 struct inode *inode = seq->private;
709 pid_t pid;
710
711 pid = pid_nr_ns(v, inode->i_sb->s_fs_info);
712 seq_printf(seq, "%d ", pid);
713
714 return 0;
715 }
716
717 static void *children_seq_start(struct seq_file *seq, loff_t *pos)
718 {
719 return get_children_pid(seq->private, NULL, *pos);
720 }
721
722 static void *children_seq_next(struct seq_file *seq, void *v, loff_t *pos)
723 {
724 struct pid *pid;
725
726 pid = get_children_pid(seq->private, v, *pos + 1);
727 put_pid(v);
728
729 ++*pos;
730 return pid;
731 }
732
733 static void children_seq_stop(struct seq_file *seq, void *v)
734 {
735 put_pid(v);
736 }
737
738 static const struct seq_operations children_seq_ops = {
739 .start = children_seq_start,
740 .next = children_seq_next,
741 .stop = children_seq_stop,
742 .show = children_seq_show,
743 };
744
745 static int children_seq_open(struct inode *inode, struct file *file)
746 {
747 struct seq_file *m;
748 int ret;
749
750 ret = seq_open(file, &children_seq_ops);
751 if (ret)
752 return ret;
753
754 m = file->private_data;
755 m->private = inode;
756
757 return ret;
758 }
759
760 int children_seq_release(struct inode *inode, struct file *file)
761 {
762 seq_release(inode, file);
763 return 0;
764 }
765
766 const struct file_operations proc_tid_children_operations = {
767 .open = children_seq_open,
768 .read = seq_read,
769 .llseek = seq_lseek,
770 .release = children_seq_release,
771 };
772 #endif /* CONFIG_PROC_CHILDREN */