2 * linux/fs/proc/array.c
4 * Copyright (C) 1992 by Linus Torvalds
5 * based on ideas by Darren Senn
8 * Michael. K. Johnson: stat,statm extensions.
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.
17 * Danny ter Haar : added cpuinfo
20 * Alessandro Rubini : profile extension.
23 * Jeff Tranter : added BogoMips field to cpuinfo
26 * Bruno Haible : remove 4K limit for the maps file
29 * Yves Arrouye : remove removal of trailing spaces in get_array.
32 * Jerome Forissier : added per-CPU time information to /proc/stat
33 * and /proc/<pid>/cpu extension
35 * - Incorporation and non-SMP safe operation
36 * of forissier patch in 2.1.78 by
42 * Alan Cox : security fixes.
45 * Al Viro : safe handling of mm_struct
47 * Gerhard Wichert : added BIGMEM support
50 * Al Viro & Jeff Garzik : moved most of the thing into base.c and
51 * : proc_misc.c. The rest may eventually go into
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>
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/ptrace.h>
88 #include <linux/tracehook.h>
89 #include <linux/string_helpers.h>
90 #include <linux/user_namespace.h>
91 #include <linux/fs_struct.h>
93 #include <asm/pgtable.h>
94 #include <asm/processor.h>
97 static inline void task_name(struct seq_file *m, struct task_struct *p)
101 char tcomm[sizeof(p->comm)];
104 get_task_comm(tcomm, p);
106 seq_puts(m, "Name:\t");
108 size = seq_get_buf(m, &buf);
109 ret = string_escape_str(tcomm, buf, size, ESCAPE_SPACE | ESCAPE_SPECIAL, "\n\\");
110 seq_commit(m, ret < size ? ret : -1);
116 * The task state array is a strange "bitmap" of
117 * reasons to sleep. Thus "running" is zero, and
118 * you can test for combinations of others with
121 static const char * const task_state_array[] = {
122 "R (running)", /* 0 */
123 "S (sleeping)", /* 1 */
124 "D (disk sleep)", /* 2 */
125 "T (stopped)", /* 4 */
126 "t (tracing stop)", /* 8 */
128 "Z (zombie)", /* 32 */
131 static inline const char *get_task_state(struct task_struct *tsk)
133 unsigned int state = (tsk->state | tsk->exit_state) & TASK_REPORT;
136 * Parked tasks do not run; they sit in __kthread_parkme().
137 * Without this check, we would report them as running, which is
138 * clearly wrong, so we report them as sleeping instead.
140 if (tsk->state == TASK_PARKED)
141 state = TASK_INTERRUPTIBLE;
143 BUILD_BUG_ON(1 + ilog2(TASK_REPORT) != ARRAY_SIZE(task_state_array)-1);
145 return task_state_array[fls(state)];
148 static inline int get_task_umask(struct task_struct *tsk)
150 struct fs_struct *fs;
161 static inline void task_state(struct seq_file *m, struct pid_namespace *ns,
162 struct pid *pid, struct task_struct *p)
164 struct user_namespace *user_ns = seq_user_ns(m);
165 struct group_info *group_info;
167 struct task_struct *tracer;
168 const struct cred *cred;
169 pid_t ppid, tpid = 0, tgid, ngid;
170 unsigned int max_fds = 0;
173 ppid = pid_alive(p) ?
174 task_tgid_nr_ns(rcu_dereference(p->real_parent), ns) : 0;
176 tracer = ptrace_parent(p);
178 tpid = task_pid_nr_ns(tracer, ns);
180 tgid = task_tgid_nr_ns(p, ns);
181 ngid = task_numa_group_id(p);
182 cred = get_task_cred(p);
184 umask = get_task_umask(p);
186 seq_printf(m, "Umask:\t%#04o\n", umask);
190 max_fds = files_fdtable(p->files)->max_fds;
194 seq_printf(m, "State:\t%s", get_task_state(p));
196 seq_put_decimal_ull(m, "\nTgid:\t", tgid);
197 seq_put_decimal_ull(m, "\nNgid:\t", ngid);
198 seq_put_decimal_ull(m, "\nPid:\t", pid_nr_ns(pid, ns));
199 seq_put_decimal_ull(m, "\nPPid:\t", ppid);
200 seq_put_decimal_ull(m, "\nTracerPid:\t", tpid);
201 seq_put_decimal_ull(m, "\nUid:\t", from_kuid_munged(user_ns, cred->uid));
202 seq_put_decimal_ull(m, "\t", from_kuid_munged(user_ns, cred->euid));
203 seq_put_decimal_ull(m, "\t", from_kuid_munged(user_ns, cred->suid));
204 seq_put_decimal_ull(m, "\t", from_kuid_munged(user_ns, cred->fsuid));
205 seq_put_decimal_ull(m, "\nGid:\t", from_kgid_munged(user_ns, cred->gid));
206 seq_put_decimal_ull(m, "\t", from_kgid_munged(user_ns, cred->egid));
207 seq_put_decimal_ull(m, "\t", from_kgid_munged(user_ns, cred->sgid));
208 seq_put_decimal_ull(m, "\t", from_kgid_munged(user_ns, cred->fsgid));
209 seq_put_decimal_ull(m, "\nFDSize:\t", max_fds);
211 seq_puts(m, "\nGroups:\t");
212 group_info = cred->group_info;
213 for (g = 0; g < group_info->ngroups; g++)
214 seq_put_decimal_ull(m, g ? " " : "",
215 from_kgid_munged(user_ns, group_info->gid[g]));
217 /* Trailing space shouldn't have been added in the first place. */
221 seq_puts(m, "\nNStgid:");
222 for (g = ns->level; g <= pid->level; g++)
223 seq_put_decimal_ull(m, "\t", task_tgid_nr_ns(p, pid->numbers[g].ns));
224 seq_puts(m, "\nNSpid:");
225 for (g = ns->level; g <= pid->level; g++)
226 seq_put_decimal_ull(m, "\t", task_pid_nr_ns(p, pid->numbers[g].ns));
227 seq_puts(m, "\nNSpgid:");
228 for (g = ns->level; g <= pid->level; g++)
229 seq_put_decimal_ull(m, "\t", task_pgrp_nr_ns(p, pid->numbers[g].ns));
230 seq_puts(m, "\nNSsid:");
231 for (g = ns->level; g <= pid->level; g++)
232 seq_put_decimal_ull(m, "\t", task_session_nr_ns(p, pid->numbers[g].ns));
237 void render_sigset_t(struct seq_file *m, const char *header,
249 if (sigismember(set, i+1)) x |= 1;
250 if (sigismember(set, i+2)) x |= 2;
251 if (sigismember(set, i+3)) x |= 4;
252 if (sigismember(set, i+4)) x |= 8;
253 seq_putc(m, hex_asc[x]);
259 static void collect_sigign_sigcatch(struct task_struct *p, sigset_t *ign,
262 struct k_sigaction *k;
265 k = p->sighand->action;
266 for (i = 1; i <= _NSIG; ++i, ++k) {
267 if (k->sa.sa_handler == SIG_IGN)
269 else if (k->sa.sa_handler != SIG_DFL)
274 static inline void task_sig(struct seq_file *m, struct task_struct *p)
277 sigset_t pending, shpending, blocked, ignored, caught;
279 unsigned long qsize = 0;
280 unsigned long qlim = 0;
282 sigemptyset(&pending);
283 sigemptyset(&shpending);
284 sigemptyset(&blocked);
285 sigemptyset(&ignored);
286 sigemptyset(&caught);
288 if (lock_task_sighand(p, &flags)) {
289 pending = p->pending.signal;
290 shpending = p->signal->shared_pending.signal;
291 blocked = p->blocked;
292 collect_sigign_sigcatch(p, &ignored, &caught);
293 num_threads = get_nr_threads(p);
294 rcu_read_lock(); /* FIXME: is this correct? */
295 qsize = atomic_read(&__task_cred(p)->user->sigpending);
297 qlim = task_rlimit(p, RLIMIT_SIGPENDING);
298 unlock_task_sighand(p, &flags);
301 seq_put_decimal_ull(m, "Threads:\t", num_threads);
302 seq_put_decimal_ull(m, "\nSigQ:\t", qsize);
303 seq_put_decimal_ull(m, "/", qlim);
305 /* render them all */
306 render_sigset_t(m, "\nSigPnd:\t", &pending);
307 render_sigset_t(m, "ShdPnd:\t", &shpending);
308 render_sigset_t(m, "SigBlk:\t", &blocked);
309 render_sigset_t(m, "SigIgn:\t", &ignored);
310 render_sigset_t(m, "SigCgt:\t", &caught);
313 static void render_cap_t(struct seq_file *m, const char *header,
319 CAP_FOR_EACH_U32(__capi) {
320 seq_printf(m, "%08x",
321 a->cap[CAP_LAST_U32 - __capi]);
326 static inline void task_cap(struct seq_file *m, struct task_struct *p)
328 const struct cred *cred;
329 kernel_cap_t cap_inheritable, cap_permitted, cap_effective,
330 cap_bset, cap_ambient;
333 cred = __task_cred(p);
334 cap_inheritable = cred->cap_inheritable;
335 cap_permitted = cred->cap_permitted;
336 cap_effective = cred->cap_effective;
337 cap_bset = cred->cap_bset;
338 cap_ambient = cred->cap_ambient;
341 render_cap_t(m, "CapInh:\t", &cap_inheritable);
342 render_cap_t(m, "CapPrm:\t", &cap_permitted);
343 render_cap_t(m, "CapEff:\t", &cap_effective);
344 render_cap_t(m, "CapBnd:\t", &cap_bset);
345 render_cap_t(m, "CapAmb:\t", &cap_ambient);
348 static inline void task_seccomp(struct seq_file *m, struct task_struct *p)
350 seq_put_decimal_ull(m, "NoNewPrivs:\t", task_no_new_privs(p));
351 #ifdef CONFIG_SECCOMP
352 seq_put_decimal_ull(m, "\nSeccomp:\t", p->seccomp.mode);
357 static inline void task_context_switch_counts(struct seq_file *m,
358 struct task_struct *p)
360 seq_put_decimal_ull(m, "voluntary_ctxt_switches:\t", p->nvcsw);
361 seq_put_decimal_ull(m, "\nnonvoluntary_ctxt_switches:\t", p->nivcsw);
365 static void task_cpus_allowed(struct seq_file *m, struct task_struct *task)
367 seq_printf(m, "Cpus_allowed:\t%*pb\n",
368 cpumask_pr_args(&task->cpus_allowed));
369 seq_printf(m, "Cpus_allowed_list:\t%*pbl\n",
370 cpumask_pr_args(&task->cpus_allowed));
373 int proc_pid_status(struct seq_file *m, struct pid_namespace *ns,
374 struct pid *pid, struct task_struct *task)
376 struct mm_struct *mm = get_task_mm(task);
379 task_state(m, ns, pid, task);
387 task_seccomp(m, task);
388 task_cpus_allowed(m, task);
389 cpuset_task_status_allowed(m, task);
390 task_context_switch_counts(m, task);
394 static int do_task_stat(struct seq_file *m, struct pid_namespace *ns,
395 struct pid *pid, struct task_struct *task, int whole)
397 unsigned long vsize, eip, esp, wchan = 0;
399 int tty_pgrp = -1, tty_nr = 0;
400 sigset_t sigign, sigcatch;
402 pid_t ppid = 0, pgid = -1, sid = -1;
405 struct mm_struct *mm;
406 unsigned long long start_time;
407 unsigned long cmin_flt = 0, cmaj_flt = 0;
408 unsigned long min_flt = 0, maj_flt = 0;
409 u64 cutime, cstime, utime, stime;
411 unsigned long rsslim = 0;
412 char tcomm[sizeof(task->comm)];
415 state = *get_task_state(task);
416 vsize = eip = esp = 0;
417 permitted = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS | PTRACE_MODE_NOAUDIT);
418 mm = get_task_mm(task);
420 vsize = task_vsize(mm);
422 * esp and eip are intentionally zeroed out. There is no
423 * non-racy way to read them without freezing the task.
424 * Programs that need reliable values can use ptrace(2).
426 * The only exception is if the task is core dumping because
427 * a program is not able to use ptrace(2) in that case. It is
428 * safe because the task has stopped executing permanently.
430 if (permitted && (task->flags & PF_DUMPCORE)) {
431 eip = KSTK_EIP(task);
432 esp = KSTK_ESP(task);
436 get_task_comm(tcomm, task);
438 sigemptyset(&sigign);
439 sigemptyset(&sigcatch);
440 cutime = cstime = utime = stime = 0;
443 if (lock_task_sighand(task, &flags)) {
444 struct signal_struct *sig = task->signal;
447 struct pid *pgrp = tty_get_pgrp(sig->tty);
448 tty_pgrp = pid_nr_ns(pgrp, ns);
450 tty_nr = new_encode_dev(tty_devnum(sig->tty));
453 num_threads = get_nr_threads(task);
454 collect_sigign_sigcatch(task, &sigign, &sigcatch);
456 cmin_flt = sig->cmin_flt;
457 cmaj_flt = sig->cmaj_flt;
458 cutime = sig->cutime;
459 cstime = sig->cstime;
460 cgtime = sig->cgtime;
461 rsslim = ACCESS_ONCE(sig->rlim[RLIMIT_RSS].rlim_cur);
463 /* add up live thread stats at the group level */
465 struct task_struct *t = task;
467 min_flt += t->min_flt;
468 maj_flt += t->maj_flt;
469 gtime += task_gtime(t);
470 } while_each_thread(task, t);
472 min_flt += sig->min_flt;
473 maj_flt += sig->maj_flt;
474 thread_group_cputime_adjusted(task, &utime, &stime);
478 sid = task_session_nr_ns(task, ns);
479 ppid = task_tgid_nr_ns(task->real_parent, ns);
480 pgid = task_pgrp_nr_ns(task, ns);
482 unlock_task_sighand(task, &flags);
485 if (permitted && (!whole || num_threads < 2))
486 wchan = get_wchan(task);
488 min_flt = task->min_flt;
489 maj_flt = task->maj_flt;
490 task_cputime_adjusted(task, &utime, &stime);
491 gtime = task_gtime(task);
494 /* scale priority and nice values from timeslices to -20..20 */
495 /* to make it look like a "normal" Unix priority/nice value */
496 priority = task_prio(task);
497 nice = task_nice(task);
499 /* convert nsec -> ticks */
500 start_time = nsec_to_clock_t(task->real_start_time);
502 seq_printf(m, "%d (%s) %c", pid_nr_ns(pid, ns), tcomm, state);
503 seq_put_decimal_ll(m, " ", ppid);
504 seq_put_decimal_ll(m, " ", pgid);
505 seq_put_decimal_ll(m, " ", sid);
506 seq_put_decimal_ll(m, " ", tty_nr);
507 seq_put_decimal_ll(m, " ", tty_pgrp);
508 seq_put_decimal_ull(m, " ", task->flags);
509 seq_put_decimal_ull(m, " ", min_flt);
510 seq_put_decimal_ull(m, " ", cmin_flt);
511 seq_put_decimal_ull(m, " ", maj_flt);
512 seq_put_decimal_ull(m, " ", cmaj_flt);
513 seq_put_decimal_ull(m, " ", nsec_to_clock_t(utime));
514 seq_put_decimal_ull(m, " ", nsec_to_clock_t(stime));
515 seq_put_decimal_ll(m, " ", nsec_to_clock_t(cutime));
516 seq_put_decimal_ll(m, " ", nsec_to_clock_t(cstime));
517 seq_put_decimal_ll(m, " ", priority);
518 seq_put_decimal_ll(m, " ", nice);
519 seq_put_decimal_ll(m, " ", num_threads);
520 seq_put_decimal_ull(m, " ", 0);
521 seq_put_decimal_ull(m, " ", start_time);
522 seq_put_decimal_ull(m, " ", vsize);
523 seq_put_decimal_ull(m, " ", mm ? get_mm_rss(mm) : 0);
524 seq_put_decimal_ull(m, " ", rsslim);
525 seq_put_decimal_ull(m, " ", mm ? (permitted ? mm->start_code : 1) : 0);
526 seq_put_decimal_ull(m, " ", mm ? (permitted ? mm->end_code : 1) : 0);
527 seq_put_decimal_ull(m, " ", (permitted && mm) ? mm->start_stack : 0);
528 seq_put_decimal_ull(m, " ", esp);
529 seq_put_decimal_ull(m, " ", eip);
530 /* The signal information here is obsolete.
531 * It must be decimal for Linux 2.0 compatibility.
532 * Use /proc/#/status for real-time signals.
534 seq_put_decimal_ull(m, " ", task->pending.signal.sig[0] & 0x7fffffffUL);
535 seq_put_decimal_ull(m, " ", task->blocked.sig[0] & 0x7fffffffUL);
536 seq_put_decimal_ull(m, " ", sigign.sig[0] & 0x7fffffffUL);
537 seq_put_decimal_ull(m, " ", sigcatch.sig[0] & 0x7fffffffUL);
540 * We used to output the absolute kernel address, but that's an
541 * information leak - so instead we show a 0/1 flag here, to signal
542 * to user-space whether there's a wchan field in /proc/PID/wchan.
544 * This works with older implementations of procps as well.
551 seq_put_decimal_ull(m, " ", 0);
552 seq_put_decimal_ull(m, " ", 0);
553 seq_put_decimal_ll(m, " ", task->exit_signal);
554 seq_put_decimal_ll(m, " ", task_cpu(task));
555 seq_put_decimal_ull(m, " ", task->rt_priority);
556 seq_put_decimal_ull(m, " ", task->policy);
557 seq_put_decimal_ull(m, " ", delayacct_blkio_ticks(task));
558 seq_put_decimal_ull(m, " ", nsec_to_clock_t(gtime));
559 seq_put_decimal_ll(m, " ", nsec_to_clock_t(cgtime));
561 if (mm && permitted) {
562 seq_put_decimal_ull(m, " ", mm->start_data);
563 seq_put_decimal_ull(m, " ", mm->end_data);
564 seq_put_decimal_ull(m, " ", mm->start_brk);
565 seq_put_decimal_ull(m, " ", mm->arg_start);
566 seq_put_decimal_ull(m, " ", mm->arg_end);
567 seq_put_decimal_ull(m, " ", mm->env_start);
568 seq_put_decimal_ull(m, " ", mm->env_end);
570 seq_puts(m, " 0 0 0 0 0 0 0");
573 seq_put_decimal_ll(m, " ", task->exit_code);
583 int proc_tid_stat(struct seq_file *m, struct pid_namespace *ns,
584 struct pid *pid, struct task_struct *task)
586 return do_task_stat(m, ns, pid, task, 0);
589 int proc_tgid_stat(struct seq_file *m, struct pid_namespace *ns,
590 struct pid *pid, struct task_struct *task)
592 return do_task_stat(m, ns, pid, task, 1);
595 int proc_pid_statm(struct seq_file *m, struct pid_namespace *ns,
596 struct pid *pid, struct task_struct *task)
598 unsigned long size = 0, resident = 0, shared = 0, text = 0, data = 0;
599 struct mm_struct *mm = get_task_mm(task);
602 size = task_statm(mm, &shared, &text, &data, &resident);
606 * For quick read, open code by putting numbers directly
608 * seq_printf(m, "%lu %lu %lu %lu 0 %lu 0\n",
609 * size, resident, shared, text, data);
611 seq_put_decimal_ull(m, "", size);
612 seq_put_decimal_ull(m, " ", resident);
613 seq_put_decimal_ull(m, " ", shared);
614 seq_put_decimal_ull(m, " ", text);
615 seq_put_decimal_ull(m, " ", 0);
616 seq_put_decimal_ull(m, " ", data);
617 seq_put_decimal_ull(m, " ", 0);
623 #ifdef CONFIG_PROC_CHILDREN
625 get_children_pid(struct inode *inode, struct pid *pid_prev, loff_t pos)
627 struct task_struct *start, *task;
628 struct pid *pid = NULL;
630 read_lock(&tasklist_lock);
632 start = pid_task(proc_pid(inode), PIDTYPE_PID);
637 * Lets try to continue searching first, this gives
638 * us significant speedup on children-rich processes.
641 task = pid_task(pid_prev, PIDTYPE_PID);
642 if (task && task->real_parent == start &&
643 !(list_empty(&task->sibling))) {
644 if (list_is_last(&task->sibling, &start->children))
646 task = list_first_entry(&task->sibling,
647 struct task_struct, sibling);
648 pid = get_pid(task_pid(task));
656 * We might miss some children here if children
657 * are exited while we were not holding the lock,
658 * but it was never promised to be accurate that
661 * "Just suppose that the parent sleeps, but N children
662 * exit after we printed their tids. Now the slow paths
663 * skips N extra children, we miss N tasks." (c)
665 * So one need to stop or freeze the leader and all
666 * its children to get a precise result.
668 list_for_each_entry(task, &start->children, sibling) {
670 pid = get_pid(task_pid(task));
676 read_unlock(&tasklist_lock);
680 static int children_seq_show(struct seq_file *seq, void *v)
682 struct inode *inode = seq->private;
685 pid = pid_nr_ns(v, inode->i_sb->s_fs_info);
686 seq_printf(seq, "%d ", pid);
691 static void *children_seq_start(struct seq_file *seq, loff_t *pos)
693 return get_children_pid(seq->private, NULL, *pos);
696 static void *children_seq_next(struct seq_file *seq, void *v, loff_t *pos)
700 pid = get_children_pid(seq->private, v, *pos + 1);
707 static void children_seq_stop(struct seq_file *seq, void *v)
712 static const struct seq_operations children_seq_ops = {
713 .start = children_seq_start,
714 .next = children_seq_next,
715 .stop = children_seq_stop,
716 .show = children_seq_show,
719 static int children_seq_open(struct inode *inode, struct file *file)
724 ret = seq_open(file, &children_seq_ops);
728 m = file->private_data;
734 int children_seq_release(struct inode *inode, struct file *file)
736 seq_release(inode, file);
740 const struct file_operations proc_tid_children_operations = {
741 .open = children_seq_open,
744 .release = children_seq_release,
746 #endif /* CONFIG_PROC_CHILDREN */