]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/proc/array.c
prctl: Add force disable speculation
[mirror_ubuntu-artful-kernel.git] / fs / proc / array.c
CommitLineData
1da177e4
LT
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.
526719ba 43 * <alan@lxorguk.ukuu.org.uk>
1da177e4
LT
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
1da177e4
LT
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>
6e84f315 63#include <linux/sched/mm.h>
6a3827d7 64#include <linux/sched/numa_balancing.h>
5d370ae1 65#include <linux/sched/task_stack.h>
29930025 66#include <linux/sched/task.h>
32ef5517 67#include <linux/sched/cputime.h>
1da177e4
LT
68#include <linux/proc_fs.h>
69#include <linux/ioport.h>
8ea02606
IM
70#include <linux/uaccess.h>
71#include <linux/io.h>
1da177e4
LT
72#include <linux/mm.h>
73#include <linux/hugetlb.h>
74#include <linux/pagemap.h>
75#include <linux/swap.h>
1da177e4
LT
76#include <linux/smp.h>
77#include <linux/signal.h>
78#include <linux/highmem.h>
79#include <linux/file.h>
9f3acc31 80#include <linux/fdtable.h>
1da177e4
LT
81#include <linux/times.h>
82#include <linux/cpuset.h>
4fb3a538 83#include <linux/rcupdate.h>
25890454 84#include <linux/delayacct.h>
ee992744 85#include <linux/seq_file.h>
b488893a 86#include <linux/pid_namespace.h>
0856e8f2
KC
87#include <linux/nospec.h>
88#include <linux/prctl.h>
f83ce3e6 89#include <linux/ptrace.h>
0d094efe 90#include <linux/tracehook.h>
edc924e0 91#include <linux/string_helpers.h>
ae2975bc 92#include <linux/user_namespace.h>
3e42979e 93#include <linux/fs_struct.h>
1da177e4 94
1da177e4 95#include <asm/pgtable.h>
1da177e4
LT
96#include <asm/processor.h>
97#include "internal.h"
98
df5f8314 99static inline void task_name(struct seq_file *m, struct task_struct *p)
1da177e4 100{
edc924e0 101 char *buf;
3a49f3d2 102 size_t size;
1da177e4 103 char tcomm[sizeof(p->comm)];
3a49f3d2 104 int ret;
1da177e4
LT
105
106 get_task_comm(tcomm, p);
107
9d6de12f 108 seq_puts(m, "Name:\t");
edc924e0 109
3a49f3d2
AS
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);
edc924e0 113
9d6de12f 114 seq_putc(m, '\n');
1da177e4
LT
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 */
e130aa70 123static const char * const task_state_array[] = {
e1781538
PZ
124 "R (running)", /* 0 */
125 "S (sleeping)", /* 1 */
126 "D (disk sleep)", /* 2 */
127 "T (stopped)", /* 4 */
128 "t (tracing stop)", /* 8 */
ad86622b
ON
129 "X (dead)", /* 16 */
130 "Z (zombie)", /* 32 */
1da177e4
LT
131};
132
8ea02606 133static inline const char *get_task_state(struct task_struct *tsk)
1da177e4 134{
74e37200 135 unsigned int state = (tsk->state | tsk->exit_state) & TASK_REPORT;
1da177e4 136
f51c0eae
CM
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
74e37200 145 BUILD_BUG_ON(1 + ilog2(TASK_REPORT) != ARRAY_SIZE(task_state_array)-1);
e1781538 146
74e37200 147 return task_state_array[fls(state)];
1da177e4
LT
148}
149
3e42979e
RJ
150static 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
df5f8314
EB
163static inline void task_state(struct seq_file *m, struct pid_namespace *ns,
164 struct pid *pid, struct task_struct *p)
1da177e4 165{
e9f238c3 166 struct user_namespace *user_ns = seq_user_ns(m);
1da177e4 167 struct group_info *group_info;
3e42979e 168 int g, umask;
abdba6e9 169 struct task_struct *tracer;
c69e8d9c 170 const struct cred *cred;
abdba6e9 171 pid_t ppid, tpid = 0, tgid, ngid;
0f4a0d53 172 unsigned int max_fds = 0;
1da177e4 173
b0fa9db6 174 rcu_read_lock();
b488893a
PE
175 ppid = pid_alive(p) ?
176 task_tgid_nr_ns(rcu_dereference(p->real_parent), ns) : 0;
abdba6e9
ON
177
178 tracer = ptrace_parent(p);
179 if (tracer)
180 tpid = task_pid_nr_ns(tracer, ns);
b0fafc11
ON
181
182 tgid = task_tgid_nr_ns(p, ns);
183 ngid = task_numa_group_id(p);
de09a977 184 cred = get_task_cred(p);
0f4a0d53 185
3e42979e
RJ
186 umask = get_task_umask(p);
187 if (umask >= 0)
188 seq_printf(m, "Umask:\t%#04o\n", umask);
189
0f4a0d53
ON
190 task_lock(p);
191 if (p->files)
192 max_fds = files_fdtable(p->files)->max_fds;
193 task_unlock(p);
b0fafc11 194 rcu_read_unlock();
0f4a0d53 195
f7a5f132
AD
196 seq_printf(m, "State:\t%s", get_task_state(p));
197
75ba1d07
JP
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);
f7a5f132
AD
212
213 seq_puts(m, "\nGroups:\t");
c69e8d9c 214 group_info = cred->group_info;
8d238027 215 for (g = 0; g < group_info->ngroups; g++)
75ba1d07 216 seq_put_decimal_ull(m, g ? " " : "",
81243eac 217 from_kgid_munged(user_ns, group_info->gid[g]));
c69e8d9c 218 put_cred(cred);
f7a5f132
AD
219 /* Trailing space shouldn't have been added in the first place. */
220 seq_putc(m, ' ');
1da177e4 221
e4bc3324
CH
222#ifdef CONFIG_PID_NS
223 seq_puts(m, "\nNStgid:");
224 for (g = ns->level; g <= pid->level; g++)
75ba1d07 225 seq_put_decimal_ull(m, "\t", task_tgid_nr_ns(p, pid->numbers[g].ns));
e4bc3324
CH
226 seq_puts(m, "\nNSpid:");
227 for (g = ns->level; g <= pid->level; g++)
75ba1d07 228 seq_put_decimal_ull(m, "\t", task_pid_nr_ns(p, pid->numbers[g].ns));
e4bc3324
CH
229 seq_puts(m, "\nNSpgid:");
230 for (g = ns->level; g <= pid->level; g++)
75ba1d07 231 seq_put_decimal_ull(m, "\t", task_pgrp_nr_ns(p, pid->numbers[g].ns));
e4bc3324
CH
232 seq_puts(m, "\nNSsid:");
233 for (g = ns->level; g <= pid->level; g++)
75ba1d07 234 seq_put_decimal_ull(m, "\t", task_session_nr_ns(p, pid->numbers[g].ns));
e4bc3324 235#endif
9d6de12f 236 seq_putc(m, '\n');
1da177e4
LT
237}
238
138d22b5 239void render_sigset_t(struct seq_file *m, const char *header,
df5f8314 240 sigset_t *set)
1da177e4 241{
df5f8314 242 int i;
1da177e4 243
9d6de12f 244 seq_puts(m, header);
1da177e4
LT
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;
209b14dc 255 seq_putc(m, hex_asc[x]);
1da177e4
LT
256 } while (i >= 4);
257
9d6de12f 258 seq_putc(m, '\n');
1da177e4
LT
259}
260
261static 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
df5f8314 276static inline void task_sig(struct seq_file *m, struct task_struct *p)
1da177e4 277{
5e6b3f42 278 unsigned long flags;
1da177e4
LT
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
5e6b3f42 290 if (lock_task_sighand(p, &flags)) {
1da177e4
LT
291 pending = p->pending.signal;
292 shpending = p->signal->shared_pending.signal;
293 blocked = p->blocked;
294 collect_sigign_sigcatch(p, &ignored, &caught);
7e49827c 295 num_threads = get_nr_threads(p);
7dc52157 296 rcu_read_lock(); /* FIXME: is this correct? */
c69e8d9c 297 qsize = atomic_read(&__task_cred(p)->user->sigpending);
7dc52157 298 rcu_read_unlock();
d554ed89 299 qlim = task_rlimit(p, RLIMIT_SIGPENDING);
5e6b3f42 300 unlock_task_sighand(p, &flags);
1da177e4 301 }
1da177e4 302
75ba1d07
JP
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);
1da177e4
LT
306
307 /* render them all */
f7a5f132 308 render_sigset_t(m, "\nSigPnd:\t", &pending);
df5f8314
EB
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);
1da177e4
LT
313}
314
df5f8314
EB
315static void render_cap_t(struct seq_file *m, const char *header,
316 kernel_cap_t *a)
e338d263
AM
317{
318 unsigned __capi;
319
9d6de12f 320 seq_puts(m, header);
e338d263 321 CAP_FOR_EACH_U32(__capi) {
df5f8314 322 seq_printf(m, "%08x",
7d8b6c63 323 a->cap[CAP_LAST_U32 - __capi]);
e338d263 324 }
9d6de12f 325 seq_putc(m, '\n');
e338d263
AM
326}
327
df5f8314 328static inline void task_cap(struct seq_file *m, struct task_struct *p)
1da177e4 329{
c69e8d9c 330 const struct cred *cred;
58319057
AL
331 kernel_cap_t cap_inheritable, cap_permitted, cap_effective,
332 cap_bset, cap_ambient;
b6dff3ec 333
c69e8d9c
DH
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;
58319057 340 cap_ambient = cred->cap_ambient;
c69e8d9c
DH
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);
58319057 347 render_cap_t(m, "CapAmb:\t", &cap_ambient);
1da177e4
LT
348}
349
2f4b3bf6
KC
350static inline void task_seccomp(struct seq_file *m, struct task_struct *p)
351{
af884cd4 352 seq_put_decimal_ull(m, "NoNewPrivs:\t", task_no_new_privs(p));
2f4b3bf6 353#ifdef CONFIG_SECCOMP
af884cd4 354 seq_put_decimal_ull(m, "\nSeccomp:\t", p->seccomp.mode);
2f4b3bf6 355#endif
0856e8f2
KC
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;
e29928d4
TG
364 case PR_SPEC_PRCTL | PR_SPEC_FORCE_DISABLE:
365 seq_printf(m, "thread force mitigated");
366 break;
0856e8f2
KC
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 }
af884cd4 380 seq_putc(m, '\n');
2f4b3bf6
KC
381}
382
df5f8314
EB
383static inline void task_context_switch_counts(struct seq_file *m,
384 struct task_struct *p)
b663a79c 385{
75ba1d07
JP
386 seq_put_decimal_ull(m, "voluntary_ctxt_switches:\t", p->nvcsw);
387 seq_put_decimal_ull(m, "\nnonvoluntary_ctxt_switches:\t", p->nivcsw);
f7a5f132 388 seq_putc(m, '\n');
b663a79c
MU
389}
390
d01d4827
HC
391static void task_cpus_allowed(struct seq_file *m, struct task_struct *task)
392{
a0c2e07d
TH
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));
d01d4827
HC
397}
398
df5f8314
EB
399int proc_pid_status(struct seq_file *m, struct pid_namespace *ns,
400 struct pid *pid, struct task_struct *task)
1da177e4 401{
1da177e4
LT
402 struct mm_struct *mm = get_task_mm(task);
403
df5f8314
EB
404 task_name(m, task);
405 task_state(m, ns, pid, task);
8ea02606 406
1da177e4 407 if (mm) {
df5f8314 408 task_mem(m, mm);
1da177e4
LT
409 mmput(mm);
410 }
df5f8314
EB
411 task_sig(m, task);
412 task_cap(m, task);
2f4b3bf6 413 task_seccomp(m, task);
d01d4827 414 task_cpus_allowed(m, task);
df5f8314 415 cpuset_task_status_allowed(m, task);
df5f8314
EB
416 task_context_switch_counts(m, task);
417 return 0;
1da177e4
LT
418}
419
ee992744
EB
420static int do_task_stat(struct seq_file *m, struct pid_namespace *ns,
421 struct pid *pid, struct task_struct *task, int whole)
1da177e4 422{
b2f73922 423 unsigned long vsize, eip, esp, wchan = 0;
715be1fc 424 int priority, nice;
1da177e4
LT
425 int tty_pgrp = -1, tty_nr = 0;
426 sigset_t sigign, sigcatch;
427 char state;
8ea02606 428 pid_t ppid = 0, pgid = -1, sid = -1;
1da177e4 429 int num_threads = 0;
f83ce3e6 430 int permitted;
1da177e4
LT
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;
5613fda9 435 u64 cutime, cstime, utime, stime;
16a6d9be 436 u64 cgtime, gtime;
1da177e4 437 unsigned long rsslim = 0;
1da177e4 438 char tcomm[sizeof(task->comm)];
a593d6ed 439 unsigned long flags;
1da177e4
LT
440
441 state = *get_task_state(task);
442 vsize = eip = esp = 0;
caaee623 443 permitted = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS | PTRACE_MODE_NOAUDIT);
1da177e4
LT
444 mm = get_task_mm(task);
445 if (mm) {
446 vsize = task_vsize(mm);
0a1eb2d4
AL
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).
5d370ae1
JO
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.
0a1eb2d4 455 */
5d370ae1
JO
456 if (permitted && (task->flags & PF_DUMPCORE)) {
457 eip = KSTK_EIP(task);
458 esp = KSTK_ESP(task);
459 }
1da177e4
LT
460 }
461
462 get_task_comm(tcomm, task);
463
464 sigemptyset(&sigign);
465 sigemptyset(&sigcatch);
64861634
MS
466 cutime = cstime = utime = stime = 0;
467 cgtime = gtime = 0;
3cfd0885 468
a593d6ed
ON
469 if (lock_task_sighand(task, &flags)) {
470 struct signal_struct *sig = task->signal;
91593504
ON
471
472 if (sig->tty) {
5d0fdf1e
AC
473 struct pid *pgrp = tty_get_pgrp(sig->tty);
474 tty_pgrp = pid_nr_ns(pgrp, ns);
475 put_pid(pgrp);
91593504 476 tty_nr = new_encode_dev(tty_devnum(sig->tty));
a593d6ed
ON
477 }
478
7e49827c 479 num_threads = get_nr_threads(task);
1da177e4
LT
480 collect_sigign_sigcatch(task, &sigign, &sigcatch);
481
a593d6ed
ON
482 cmin_flt = sig->cmin_flt;
483 cmaj_flt = sig->cmaj_flt;
484 cutime = sig->cutime;
485 cstime = sig->cstime;
9ac52315 486 cgtime = sig->cgtime;
d554ed89 487 rsslim = ACCESS_ONCE(sig->rlim[RLIMIT_RSS].rlim_cur);
a593d6ed 488
1da177e4
LT
489 /* add up live thread stats at the group level */
490 if (whole) {
a593d6ed 491 struct task_struct *t = task;
1da177e4
LT
492 do {
493 min_flt += t->min_flt;
494 maj_flt += t->maj_flt;
6fac4829 495 gtime += task_gtime(t);
185ee40e 496 } while_each_thread(task, t);
1da177e4 497
a593d6ed
ON
498 min_flt += sig->min_flt;
499 maj_flt += sig->maj_flt;
e80d0a1a 500 thread_group_cputime_adjusted(task, &utime, &stime);
64861634 501 gtime += sig->gtime;
1da177e4 502 }
a593d6ed 503
b488893a 504 sid = task_session_nr_ns(task, ns);
a98fdcef 505 ppid = task_tgid_nr_ns(task->real_parent, ns);
b488893a 506 pgid = task_pgrp_nr_ns(task, ns);
a593d6ed
ON
507
508 unlock_task_sighand(task, &flags);
1da177e4 509 }
1da177e4 510
f83ce3e6 511 if (permitted && (!whole || num_threads < 2))
1da177e4
LT
512 wchan = get_wchan(task);
513 if (!whole) {
514 min_flt = task->min_flt;
515 maj_flt = task->maj_flt;
e80d0a1a 516 task_cputime_adjusted(task, &utime, &stime);
6fac4829 517 gtime = task_gtime(task);
1da177e4
LT
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
1da177e4 525 /* convert nsec -> ticks */
57e0be04 526 start_time = nsec_to_clock_t(task->real_start_time);
1da177e4 527
bda7bad6 528 seq_printf(m, "%d (%s) %c", pid_nr_ns(pid, ns), tcomm, state);
75ba1d07
JP
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);
5613fda9
FW
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));
75ba1d07
JP
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);
bda7bad6
KH
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 */
75ba1d07
JP
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);
b2f73922
IM
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
75ba1d07
JP
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));
16a6d9be
FW
584 seq_put_decimal_ull(m, " ", nsec_to_clock_t(gtime));
585 seq_put_decimal_ll(m, " ", nsec_to_clock_t(cgtime));
5b172087
CG
586
587 if (mm && permitted) {
75ba1d07
JP
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);
5b172087 595 } else
75ba1d07 596 seq_puts(m, " 0 0 0 0 0 0 0");
5b172087
CG
597
598 if (permitted)
75ba1d07 599 seq_put_decimal_ll(m, " ", task->exit_code);
5b172087 600 else
75ba1d07 601 seq_puts(m, " 0");
5b172087 602
bda7bad6 603 seq_putc(m, '\n');
8ea02606 604 if (mm)
1da177e4 605 mmput(mm);
ee992744 606 return 0;
1da177e4
LT
607}
608
ee992744
EB
609int proc_tid_stat(struct seq_file *m, struct pid_namespace *ns,
610 struct pid *pid, struct task_struct *task)
1da177e4 611{
ee992744 612 return do_task_stat(m, ns, pid, task, 0);
1da177e4
LT
613}
614
ee992744
EB
615int proc_tgid_stat(struct seq_file *m, struct pid_namespace *ns,
616 struct pid *pid, struct task_struct *task)
1da177e4 617{
ee992744 618 return do_task_stat(m, ns, pid, task, 1);
1da177e4
LT
619}
620
a56d3fc7
EB
621int proc_pid_statm(struct seq_file *m, struct pid_namespace *ns,
622 struct pid *pid, struct task_struct *task)
1da177e4 623{
a2ade7b6 624 unsigned long size = 0, resident = 0, shared = 0, text = 0, data = 0;
1da177e4 625 struct mm_struct *mm = get_task_mm(task);
8ea02606 626
1da177e4
LT
627 if (mm) {
628 size = task_statm(mm, &shared, &text, &data, &resident);
629 mmput(mm);
630 }
bda7bad6
KH
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 */
75ba1d07
JP
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);
bda7bad6 644 seq_putc(m, '\n');
1da177e4 645
a56d3fc7 646 return 0;
1da177e4 647}
81841161 648
2e13ba54 649#ifdef CONFIG_PROC_CHILDREN
81841161
CG
650static struct pid *
651get_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
701out:
702 read_unlock(&tasklist_lock);
703 return pid;
704}
705
706static 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);
25ce3191
JP
712 seq_printf(seq, "%d ", pid);
713
714 return 0;
81841161
CG
715}
716
717static void *children_seq_start(struct seq_file *seq, loff_t *pos)
718{
719 return get_children_pid(seq->private, NULL, *pos);
720}
721
722static 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
733static void children_seq_stop(struct seq_file *seq, void *v)
734{
735 put_pid(v);
736}
737
738static 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
745static 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
760int children_seq_release(struct inode *inode, struct file *file)
761{
762 seq_release(inode, file);
763 return 0;
764}
765
766const 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};
2e13ba54 772#endif /* CONFIG_PROC_CHILDREN */