xref: /openbmc/linux/fs/proc/array.c (revision 5d858e2d)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  linux/fs/proc/array.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *  Copyright (C) 1992  by Linus Torvalds
61da177e4SLinus Torvalds  *  based on ideas by Darren Senn
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * Fixes:
91da177e4SLinus Torvalds  * Michael. K. Johnson: stat,statm extensions.
101da177e4SLinus Torvalds  *                      <johnsonm@stolaf.edu>
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  * Pauline Middelink :  Made cmdline,envline only break at '\0's, to
131da177e4SLinus Torvalds  *                      make sure SET_PROCTITLE works. Also removed
141da177e4SLinus Torvalds  *                      bad '!' which forced address recalculation for
151da177e4SLinus Torvalds  *                      EVERY character on the current page.
161da177e4SLinus Torvalds  *                      <middelin@polyware.iaf.nl>
171da177e4SLinus Torvalds  *
181da177e4SLinus Torvalds  * Danny ter Haar    :	added cpuinfo
191da177e4SLinus Torvalds  *			<dth@cistron.nl>
201da177e4SLinus Torvalds  *
211da177e4SLinus Torvalds  * Alessandro Rubini :  profile extension.
221da177e4SLinus Torvalds  *                      <rubini@ipvvis.unipv.it>
231da177e4SLinus Torvalds  *
241da177e4SLinus Torvalds  * Jeff Tranter      :  added BogoMips field to cpuinfo
251da177e4SLinus Torvalds  *                      <Jeff_Tranter@Mitel.COM>
261da177e4SLinus Torvalds  *
271da177e4SLinus Torvalds  * Bruno Haible      :  remove 4K limit for the maps file
281da177e4SLinus Torvalds  *			<haible@ma2s2.mathematik.uni-karlsruhe.de>
291da177e4SLinus Torvalds  *
301da177e4SLinus Torvalds  * Yves Arrouye      :  remove removal of trailing spaces in get_array.
311da177e4SLinus Torvalds  *			<Yves.Arrouye@marin.fdn.fr>
321da177e4SLinus Torvalds  *
331da177e4SLinus Torvalds  * Jerome Forissier  :  added per-CPU time information to /proc/stat
341da177e4SLinus Torvalds  *                      and /proc/<pid>/cpu extension
351da177e4SLinus Torvalds  *                      <forissier@isia.cma.fr>
361da177e4SLinus Torvalds  *			- Incorporation and non-SMP safe operation
371da177e4SLinus Torvalds  *			of forissier patch in 2.1.78 by
381da177e4SLinus Torvalds  *			Hans Marcus <crowbar@concepts.nl>
391da177e4SLinus Torvalds  *
401da177e4SLinus Torvalds  * aeb@cwi.nl        :  /proc/partitions
411da177e4SLinus Torvalds  *
421da177e4SLinus Torvalds  *
431da177e4SLinus Torvalds  * Alan Cox	     :  security fixes.
44526719baSAlan Cox  *			<alan@lxorguk.ukuu.org.uk>
451da177e4SLinus Torvalds  *
461da177e4SLinus Torvalds  * Al Viro           :  safe handling of mm_struct
471da177e4SLinus Torvalds  *
481da177e4SLinus Torvalds  * Gerhard Wichert   :  added BIGMEM support
491da177e4SLinus Torvalds  * Siemens AG           <Gerhard.Wichert@pdb.siemens.de>
501da177e4SLinus Torvalds  *
511da177e4SLinus Torvalds  * Al Viro & Jeff Garzik :  moved most of the thing into base.c and
521da177e4SLinus Torvalds  *			 :  proc_misc.c. The rest may eventually go into
531da177e4SLinus Torvalds  *			 :  base.c too.
541da177e4SLinus Torvalds  */
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds #include <linux/types.h>
571da177e4SLinus Torvalds #include <linux/errno.h>
581da177e4SLinus Torvalds #include <linux/time.h>
593ae700ecSMichael Weiß #include <linux/time_namespace.h>
601da177e4SLinus Torvalds #include <linux/kernel.h>
611da177e4SLinus Torvalds #include <linux/kernel_stat.h>
621da177e4SLinus Torvalds #include <linux/tty.h>
631da177e4SLinus Torvalds #include <linux/string.h>
641da177e4SLinus Torvalds #include <linux/mman.h>
656e84f315SIngo Molnar #include <linux/sched/mm.h>
666a3827d7SIngo Molnar #include <linux/sched/numa_balancing.h>
67fd7d5627SJohn Ogness #include <linux/sched/task_stack.h>
6829930025SIngo Molnar #include <linux/sched/task.h>
6932ef5517SIngo Molnar #include <linux/sched/cputime.h>
701da177e4SLinus Torvalds #include <linux/proc_fs.h>
711da177e4SLinus Torvalds #include <linux/ioport.h>
728ea02606SIngo Molnar #include <linux/io.h>
731da177e4SLinus Torvalds #include <linux/mm.h>
741da177e4SLinus Torvalds #include <linux/hugetlb.h>
751da177e4SLinus Torvalds #include <linux/pagemap.h>
761da177e4SLinus Torvalds #include <linux/swap.h>
771da177e4SLinus Torvalds #include <linux/smp.h>
781da177e4SLinus Torvalds #include <linux/signal.h>
791da177e4SLinus Torvalds #include <linux/highmem.h>
801da177e4SLinus Torvalds #include <linux/file.h>
819f3acc31SAl Viro #include <linux/fdtable.h>
821da177e4SLinus Torvalds #include <linux/times.h>
831da177e4SLinus Torvalds #include <linux/cpuset.h>
844fb3a538SDipankar Sarma #include <linux/rcupdate.h>
8525890454SShailabh Nagar #include <linux/delayacct.h>
86ee992744SEric W. Biederman #include <linux/seq_file.h>
87b488893aSPavel Emelyanov #include <linux/pid_namespace.h>
88fae1fa0fSKees Cook #include <linux/prctl.h>
89f83ce3e6SJake Edge #include <linux/ptrace.h>
90edc924e0SAndy Shevchenko #include <linux/string_helpers.h>
91ae2975bcSEric W. Biederman #include <linux/user_namespace.h>
923e42979eSRichard W.M. Jones #include <linux/fs_struct.h>
93d6986ce2SYafang Shao #include <linux/kthread.h>
94f7d30434SKirill A. Shutemov #include <linux/mmu_context.h>
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds #include <asm/processor.h>
971da177e4SLinus Torvalds #include "internal.h"
981da177e4SLinus Torvalds 
proc_task_name(struct seq_file * m,struct task_struct * p,bool escape)9988b72b31STejun Heo void proc_task_name(struct seq_file *m, struct task_struct *p, bool escape)
1001da177e4SLinus Torvalds {
1016b59808bSTejun Heo 	char tcomm[64];
1021da177e4SLinus Torvalds 
103ed8fb78dSAlexey Dobriyan 	/*
104ed8fb78dSAlexey Dobriyan 	 * Test before PF_KTHREAD because all workqueue worker threads are
105ed8fb78dSAlexey Dobriyan 	 * kernel threads.
106ed8fb78dSAlexey Dobriyan 	 */
1076b59808bSTejun Heo 	if (p->flags & PF_WQ_WORKER)
1086b59808bSTejun Heo 		wq_worker_comm(tcomm, sizeof(tcomm), p);
109d6986ce2SYafang Shao 	else if (p->flags & PF_KTHREAD)
110d6986ce2SYafang Shao 		get_kthread_comm(tcomm, sizeof(tcomm), p);
1116b59808bSTejun Heo 	else
1126b59808bSTejun Heo 		__get_task_comm(tcomm, sizeof(tcomm), p);
113edc924e0SAndy Shevchenko 
1148d23b208SChristoph Hellwig 	if (escape)
1158d23b208SChristoph Hellwig 		seq_escape_str(m, tcomm, ESCAPE_SPACE | ESCAPE_SPECIAL, "\n\\");
1168d23b208SChristoph Hellwig 	else
1178d23b208SChristoph Hellwig 		seq_printf(m, "%.64s", tcomm);
1181da177e4SLinus Torvalds }
1191da177e4SLinus Torvalds 
1201da177e4SLinus Torvalds /*
1211da177e4SLinus Torvalds  * The task state array is a strange "bitmap" of
1221da177e4SLinus Torvalds  * reasons to sleep. Thus "running" is zero, and
1231da177e4SLinus Torvalds  * you can test for combinations of others with
1241da177e4SLinus Torvalds  * simple bit tests.
1251da177e4SLinus Torvalds  */
126e130aa70SMike Frysinger static const char * const task_state_array[] = {
12706eb6184SPeter Zijlstra 
12806eb6184SPeter Zijlstra 	/* states in TASK_REPORT: */
12906eb6184SPeter Zijlstra 	"R (running)",		/* 0x00 */
13006eb6184SPeter Zijlstra 	"S (sleeping)",		/* 0x01 */
13106eb6184SPeter Zijlstra 	"D (disk sleep)",	/* 0x02 */
13206eb6184SPeter Zijlstra 	"T (stopped)",		/* 0x04 */
13306eb6184SPeter Zijlstra 	"t (tracing stop)",	/* 0x08 */
13406eb6184SPeter Zijlstra 	"X (dead)",		/* 0x10 */
13506eb6184SPeter Zijlstra 	"Z (zombie)",		/* 0x20 */
1368ef9925bSPeter Zijlstra 	"P (parked)",		/* 0x40 */
13706eb6184SPeter Zijlstra 
13806eb6184SPeter Zijlstra 	/* states beyond TASK_REPORT: */
1398ef9925bSPeter Zijlstra 	"I (idle)",		/* 0x80 */
1401da177e4SLinus Torvalds };
1411da177e4SLinus Torvalds 
get_task_state(struct task_struct * tsk)1421da177e4SLinus Torvalds static inline const char *get_task_state(struct task_struct *tsk)
1431da177e4SLinus Torvalds {
14406eb6184SPeter Zijlstra 	BUILD_BUG_ON(1 + ilog2(TASK_REPORT_MAX) != ARRAY_SIZE(task_state_array));
1451d48b080SPeter Zijlstra 	return task_state_array[task_state_index(tsk)];
1461da177e4SLinus Torvalds }
1471da177e4SLinus Torvalds 
task_state(struct seq_file * m,struct pid_namespace * ns,struct pid * pid,struct task_struct * p)148df5f8314SEric W. Biederman static inline void task_state(struct seq_file *m, struct pid_namespace *ns,
149df5f8314SEric W. Biederman 				struct pid *pid, struct task_struct *p)
1501da177e4SLinus Torvalds {
151e9f238c3SEric W. Biederman 	struct user_namespace *user_ns = seq_user_ns(m);
1521da177e4SLinus Torvalds 	struct group_info *group_info;
15368c3411fSMateusz Guzik 	int g, umask = -1;
154abdba6e9SOleg Nesterov 	struct task_struct *tracer;
155c69e8d9cSDavid Howells 	const struct cred *cred;
156abdba6e9SOleg Nesterov 	pid_t ppid, tpid = 0, tgid, ngid;
1570f4a0d53SOleg Nesterov 	unsigned int max_fds = 0;
1581da177e4SLinus Torvalds 
159b0fa9db6SOleg Nesterov 	rcu_read_lock();
160b488893aSPavel Emelyanov 	ppid = pid_alive(p) ?
161b488893aSPavel Emelyanov 		task_tgid_nr_ns(rcu_dereference(p->real_parent), ns) : 0;
162abdba6e9SOleg Nesterov 
163abdba6e9SOleg Nesterov 	tracer = ptrace_parent(p);
1640d094efeSRoland McGrath 	if (tracer)
1650d094efeSRoland McGrath 		tpid = task_pid_nr_ns(tracer, ns);
166b0fafc11SOleg Nesterov 
167b0fafc11SOleg Nesterov 	tgid = task_tgid_nr_ns(p, ns);
168b0fafc11SOleg Nesterov 	ngid = task_numa_group_id(p);
169de09a977SDavid Howells 	cred = get_task_cred(p);
1700f4a0d53SOleg Nesterov 
1710f4a0d53SOleg Nesterov 	task_lock(p);
17268c3411fSMateusz Guzik 	if (p->fs)
17368c3411fSMateusz Guzik 		umask = p->fs->umask;
1740f4a0d53SOleg Nesterov 	if (p->files)
1750f4a0d53SOleg Nesterov 		max_fds = files_fdtable(p->files)->max_fds;
1760f4a0d53SOleg Nesterov 	task_unlock(p);
177b0fafc11SOleg Nesterov 	rcu_read_unlock();
1780f4a0d53SOleg Nesterov 
17968c3411fSMateusz Guzik 	if (umask >= 0)
18068c3411fSMateusz Guzik 		seq_printf(m, "Umask:\t%#04o\n", umask);
181d0f02231SAndrei Vagin 	seq_puts(m, "State:\t");
182d0f02231SAndrei Vagin 	seq_puts(m, get_task_state(p));
1831da177e4SLinus Torvalds 
18475ba1d07SJoe Perches 	seq_put_decimal_ull(m, "\nTgid:\t", tgid);
18575ba1d07SJoe Perches 	seq_put_decimal_ull(m, "\nNgid:\t", ngid);
18675ba1d07SJoe Perches 	seq_put_decimal_ull(m, "\nPid:\t", pid_nr_ns(pid, ns));
18775ba1d07SJoe Perches 	seq_put_decimal_ull(m, "\nPPid:\t", ppid);
18875ba1d07SJoe Perches 	seq_put_decimal_ull(m, "\nTracerPid:\t", tpid);
18975ba1d07SJoe Perches 	seq_put_decimal_ull(m, "\nUid:\t", from_kuid_munged(user_ns, cred->uid));
19075ba1d07SJoe Perches 	seq_put_decimal_ull(m, "\t", from_kuid_munged(user_ns, cred->euid));
19175ba1d07SJoe Perches 	seq_put_decimal_ull(m, "\t", from_kuid_munged(user_ns, cred->suid));
19275ba1d07SJoe Perches 	seq_put_decimal_ull(m, "\t", from_kuid_munged(user_ns, cred->fsuid));
19375ba1d07SJoe Perches 	seq_put_decimal_ull(m, "\nGid:\t", from_kgid_munged(user_ns, cred->gid));
19475ba1d07SJoe Perches 	seq_put_decimal_ull(m, "\t", from_kgid_munged(user_ns, cred->egid));
19575ba1d07SJoe Perches 	seq_put_decimal_ull(m, "\t", from_kgid_munged(user_ns, cred->sgid));
19675ba1d07SJoe Perches 	seq_put_decimal_ull(m, "\t", from_kgid_munged(user_ns, cred->fsgid));
19775ba1d07SJoe Perches 	seq_put_decimal_ull(m, "\nFDSize:\t", max_fds);
198f7a5f132SAlexey Dobriyan 
199f7a5f132SAlexey Dobriyan 	seq_puts(m, "\nGroups:\t");
200c69e8d9cSDavid Howells 	group_info = cred->group_info;
2018d238027SArtem Bityutskiy 	for (g = 0; g < group_info->ngroups; g++)
20275ba1d07SJoe Perches 		seq_put_decimal_ull(m, g ? " " : "",
20381243eacSAlexey Dobriyan 				from_kgid_munged(user_ns, group_info->gid[g]));
204c69e8d9cSDavid Howells 	put_cred(cred);
205f7a5f132SAlexey Dobriyan 	/* Trailing space shouldn't have been added in the first place. */
206f7a5f132SAlexey Dobriyan 	seq_putc(m, ' ');
2071da177e4SLinus Torvalds 
208e4bc3324SChen Hanxiao #ifdef CONFIG_PID_NS
209e4bc3324SChen Hanxiao 	seq_puts(m, "\nNStgid:");
210e4bc3324SChen Hanxiao 	for (g = ns->level; g <= pid->level; g++)
21175ba1d07SJoe Perches 		seq_put_decimal_ull(m, "\t", task_tgid_nr_ns(p, pid->numbers[g].ns));
212e4bc3324SChen Hanxiao 	seq_puts(m, "\nNSpid:");
213e4bc3324SChen Hanxiao 	for (g = ns->level; g <= pid->level; g++)
21475ba1d07SJoe Perches 		seq_put_decimal_ull(m, "\t", task_pid_nr_ns(p, pid->numbers[g].ns));
215e4bc3324SChen Hanxiao 	seq_puts(m, "\nNSpgid:");
216e4bc3324SChen Hanxiao 	for (g = ns->level; g <= pid->level; g++)
21775ba1d07SJoe Perches 		seq_put_decimal_ull(m, "\t", task_pgrp_nr_ns(p, pid->numbers[g].ns));
218e4bc3324SChen Hanxiao 	seq_puts(m, "\nNSsid:");
219e4bc3324SChen Hanxiao 	for (g = ns->level; g <= pid->level; g++)
22075ba1d07SJoe Perches 		seq_put_decimal_ull(m, "\t", task_session_nr_ns(p, pid->numbers[g].ns));
221e4bc3324SChen Hanxiao #endif
2229d6de12fSAlexey Dobriyan 	seq_putc(m, '\n');
223522dc4e5SChunguang Wu 
224522dc4e5SChunguang Wu 	seq_printf(m, "Kthread:\t%c\n", p->flags & PF_KTHREAD ? '1' : '0');
2251da177e4SLinus Torvalds }
2261da177e4SLinus Torvalds 
render_sigset_t(struct seq_file * m,const char * header,sigset_t * set)227138d22b5SCyrill Gorcunov void render_sigset_t(struct seq_file *m, const char *header,
228df5f8314SEric W. Biederman 				sigset_t *set)
2291da177e4SLinus Torvalds {
230df5f8314SEric W. Biederman 	int i;
2311da177e4SLinus Torvalds 
2329d6de12fSAlexey Dobriyan 	seq_puts(m, header);
2331da177e4SLinus Torvalds 
2341da177e4SLinus Torvalds 	i = _NSIG;
2351da177e4SLinus Torvalds 	do {
2361da177e4SLinus Torvalds 		int x = 0;
2371da177e4SLinus Torvalds 
2381da177e4SLinus Torvalds 		i -= 4;
2391da177e4SLinus Torvalds 		if (sigismember(set, i+1)) x |= 1;
2401da177e4SLinus Torvalds 		if (sigismember(set, i+2)) x |= 2;
2411da177e4SLinus Torvalds 		if (sigismember(set, i+3)) x |= 4;
2421da177e4SLinus Torvalds 		if (sigismember(set, i+4)) x |= 8;
243209b14dcSRasmus Villemoes 		seq_putc(m, hex_asc[x]);
2441da177e4SLinus Torvalds 	} while (i >= 4);
2451da177e4SLinus Torvalds 
2469d6de12fSAlexey Dobriyan 	seq_putc(m, '\n');
2471da177e4SLinus Torvalds }
2481da177e4SLinus Torvalds 
collect_sigign_sigcatch(struct task_struct * p,sigset_t * sigign,sigset_t * sigcatch)2498977a27bSAlexey Dobriyan static void collect_sigign_sigcatch(struct task_struct *p, sigset_t *sigign,
2508977a27bSAlexey Dobriyan 				    sigset_t *sigcatch)
2511da177e4SLinus Torvalds {
2521da177e4SLinus Torvalds 	struct k_sigaction *k;
2531da177e4SLinus Torvalds 	int i;
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds 	k = p->sighand->action;
2561da177e4SLinus Torvalds 	for (i = 1; i <= _NSIG; ++i, ++k) {
2571da177e4SLinus Torvalds 		if (k->sa.sa_handler == SIG_IGN)
2588977a27bSAlexey Dobriyan 			sigaddset(sigign, i);
2591da177e4SLinus Torvalds 		else if (k->sa.sa_handler != SIG_DFL)
2608977a27bSAlexey Dobriyan 			sigaddset(sigcatch, i);
2611da177e4SLinus Torvalds 	}
2621da177e4SLinus Torvalds }
2631da177e4SLinus Torvalds 
task_sig(struct seq_file * m,struct task_struct * p)264df5f8314SEric W. Biederman static inline void task_sig(struct seq_file *m, struct task_struct *p)
2651da177e4SLinus Torvalds {
2665e6b3f42SOleg Nesterov 	unsigned long flags;
2671da177e4SLinus Torvalds 	sigset_t pending, shpending, blocked, ignored, caught;
2681da177e4SLinus Torvalds 	int num_threads = 0;
269197850a1SAlexey Dobriyan 	unsigned int qsize = 0;
2701da177e4SLinus Torvalds 	unsigned long qlim = 0;
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds 	sigemptyset(&pending);
2731da177e4SLinus Torvalds 	sigemptyset(&shpending);
2741da177e4SLinus Torvalds 	sigemptyset(&blocked);
2751da177e4SLinus Torvalds 	sigemptyset(&ignored);
2761da177e4SLinus Torvalds 	sigemptyset(&caught);
2771da177e4SLinus Torvalds 
2785e6b3f42SOleg Nesterov 	if (lock_task_sighand(p, &flags)) {
2791da177e4SLinus Torvalds 		pending = p->pending.signal;
2801da177e4SLinus Torvalds 		shpending = p->signal->shared_pending.signal;
2811da177e4SLinus Torvalds 		blocked = p->blocked;
2821da177e4SLinus Torvalds 		collect_sigign_sigcatch(p, &ignored, &caught);
2837e49827cSOleg Nesterov 		num_threads = get_nr_threads(p);
2847dc52157SPaul E. McKenney 		rcu_read_lock();  /* FIXME: is this correct? */
285de399236SAlexey Gladkov 		qsize = get_rlimit_value(task_ucounts(p), UCOUNT_RLIMIT_SIGPENDING);
2867dc52157SPaul E. McKenney 		rcu_read_unlock();
287d554ed89SJiri Slaby 		qlim = task_rlimit(p, RLIMIT_SIGPENDING);
2885e6b3f42SOleg Nesterov 		unlock_task_sighand(p, &flags);
2891da177e4SLinus Torvalds 	}
2901da177e4SLinus Torvalds 
29175ba1d07SJoe Perches 	seq_put_decimal_ull(m, "Threads:\t", num_threads);
29275ba1d07SJoe Perches 	seq_put_decimal_ull(m, "\nSigQ:\t", qsize);
29375ba1d07SJoe Perches 	seq_put_decimal_ull(m, "/", qlim);
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds 	/* render them all */
296f7a5f132SAlexey Dobriyan 	render_sigset_t(m, "\nSigPnd:\t", &pending);
297df5f8314SEric W. Biederman 	render_sigset_t(m, "ShdPnd:\t", &shpending);
298df5f8314SEric W. Biederman 	render_sigset_t(m, "SigBlk:\t", &blocked);
299df5f8314SEric W. Biederman 	render_sigset_t(m, "SigIgn:\t", &ignored);
300df5f8314SEric W. Biederman 	render_sigset_t(m, "SigCgt:\t", &caught);
3011da177e4SLinus Torvalds }
3021da177e4SLinus Torvalds 
render_cap_t(struct seq_file * m,const char * header,kernel_cap_t * a)303df5f8314SEric W. Biederman static void render_cap_t(struct seq_file *m, const char *header,
304df5f8314SEric W. Biederman 			kernel_cap_t *a)
305e338d263SAndrew Morgan {
3069d6de12fSAlexey Dobriyan 	seq_puts(m, header);
307f122a08bSLinus Torvalds 	seq_put_hex_ll(m, NULL, a->val, 16);
3089d6de12fSAlexey Dobriyan 	seq_putc(m, '\n');
309e338d263SAndrew Morgan }
310e338d263SAndrew Morgan 
task_cap(struct seq_file * m,struct task_struct * p)311df5f8314SEric W. Biederman static inline void task_cap(struct seq_file *m, struct task_struct *p)
3121da177e4SLinus Torvalds {
313c69e8d9cSDavid Howells 	const struct cred *cred;
31458319057SAndy Lutomirski 	kernel_cap_t cap_inheritable, cap_permitted, cap_effective,
31558319057SAndy Lutomirski 			cap_bset, cap_ambient;
316b6dff3ecSDavid Howells 
317c69e8d9cSDavid Howells 	rcu_read_lock();
318c69e8d9cSDavid Howells 	cred = __task_cred(p);
319c69e8d9cSDavid Howells 	cap_inheritable	= cred->cap_inheritable;
320c69e8d9cSDavid Howells 	cap_permitted	= cred->cap_permitted;
321c69e8d9cSDavid Howells 	cap_effective	= cred->cap_effective;
322c69e8d9cSDavid Howells 	cap_bset	= cred->cap_bset;
32358319057SAndy Lutomirski 	cap_ambient	= cred->cap_ambient;
324c69e8d9cSDavid Howells 	rcu_read_unlock();
325c69e8d9cSDavid Howells 
326c69e8d9cSDavid Howells 	render_cap_t(m, "CapInh:\t", &cap_inheritable);
327c69e8d9cSDavid Howells 	render_cap_t(m, "CapPrm:\t", &cap_permitted);
328c69e8d9cSDavid Howells 	render_cap_t(m, "CapEff:\t", &cap_effective);
329c69e8d9cSDavid Howells 	render_cap_t(m, "CapBnd:\t", &cap_bset);
33058319057SAndy Lutomirski 	render_cap_t(m, "CapAmb:\t", &cap_ambient);
3311da177e4SLinus Torvalds }
3321da177e4SLinus Torvalds 
task_seccomp(struct seq_file * m,struct task_struct * p)3332f4b3bf6SKees Cook static inline void task_seccomp(struct seq_file *m, struct task_struct *p)
3342f4b3bf6SKees Cook {
335af884cd4SKees Cook 	seq_put_decimal_ull(m, "NoNewPrivs:\t", task_no_new_privs(p));
3362f4b3bf6SKees Cook #ifdef CONFIG_SECCOMP
337af884cd4SKees Cook 	seq_put_decimal_ull(m, "\nSeccomp:\t", p->seccomp.mode);
33864bdc024SKenta.Tada@sony.com #ifdef CONFIG_SECCOMP_FILTER
339c818c03bSKees Cook 	seq_put_decimal_ull(m, "\nSeccomp_filters:\t",
340c818c03bSKees Cook 			    atomic_read(&p->seccomp.filter_count));
3412f4b3bf6SKees Cook #endif
34264bdc024SKenta.Tada@sony.com #endif
34308b55775SAlexey Dobriyan 	seq_puts(m, "\nSpeculation_Store_Bypass:\t");
344fae1fa0fSKees Cook 	switch (arch_prctl_spec_ctrl_get(p, PR_SPEC_STORE_BYPASS)) {
345fae1fa0fSKees Cook 	case -EINVAL:
34608b55775SAlexey Dobriyan 		seq_puts(m, "unknown");
347fae1fa0fSKees Cook 		break;
348fae1fa0fSKees Cook 	case PR_SPEC_NOT_AFFECTED:
34908b55775SAlexey Dobriyan 		seq_puts(m, "not vulnerable");
350fae1fa0fSKees Cook 		break;
351356e4bffSThomas Gleixner 	case PR_SPEC_PRCTL | PR_SPEC_FORCE_DISABLE:
35208b55775SAlexey Dobriyan 		seq_puts(m, "thread force mitigated");
353356e4bffSThomas Gleixner 		break;
354fae1fa0fSKees Cook 	case PR_SPEC_PRCTL | PR_SPEC_DISABLE:
35508b55775SAlexey Dobriyan 		seq_puts(m, "thread mitigated");
356fae1fa0fSKees Cook 		break;
357fae1fa0fSKees Cook 	case PR_SPEC_PRCTL | PR_SPEC_ENABLE:
35808b55775SAlexey Dobriyan 		seq_puts(m, "thread vulnerable");
359fae1fa0fSKees Cook 		break;
360fae1fa0fSKees Cook 	case PR_SPEC_DISABLE:
36108b55775SAlexey Dobriyan 		seq_puts(m, "globally mitigated");
362fae1fa0fSKees Cook 		break;
363fae1fa0fSKees Cook 	default:
36408b55775SAlexey Dobriyan 		seq_puts(m, "vulnerable");
365fae1fa0fSKees Cook 		break;
366fae1fa0fSKees Cook 	}
367fe719888SAnand K Mistry 
368fe719888SAnand K Mistry 	seq_puts(m, "\nSpeculationIndirectBranch:\t");
369fe719888SAnand K Mistry 	switch (arch_prctl_spec_ctrl_get(p, PR_SPEC_INDIRECT_BRANCH)) {
370fe719888SAnand K Mistry 	case -EINVAL:
371fe719888SAnand K Mistry 		seq_puts(m, "unsupported");
372fe719888SAnand K Mistry 		break;
373fe719888SAnand K Mistry 	case PR_SPEC_NOT_AFFECTED:
374fe719888SAnand K Mistry 		seq_puts(m, "not affected");
375fe719888SAnand K Mistry 		break;
376fe719888SAnand K Mistry 	case PR_SPEC_PRCTL | PR_SPEC_FORCE_DISABLE:
377fe719888SAnand K Mistry 		seq_puts(m, "conditional force disabled");
378fe719888SAnand K Mistry 		break;
379fe719888SAnand K Mistry 	case PR_SPEC_PRCTL | PR_SPEC_DISABLE:
380fe719888SAnand K Mistry 		seq_puts(m, "conditional disabled");
381fe719888SAnand K Mistry 		break;
382fe719888SAnand K Mistry 	case PR_SPEC_PRCTL | PR_SPEC_ENABLE:
383fe719888SAnand K Mistry 		seq_puts(m, "conditional enabled");
384fe719888SAnand K Mistry 		break;
385fe719888SAnand K Mistry 	case PR_SPEC_ENABLE:
386fe719888SAnand K Mistry 		seq_puts(m, "always enabled");
387fe719888SAnand K Mistry 		break;
388fe719888SAnand K Mistry 	case PR_SPEC_DISABLE:
389fe719888SAnand K Mistry 		seq_puts(m, "always disabled");
390fe719888SAnand K Mistry 		break;
391fe719888SAnand K Mistry 	default:
392fe719888SAnand K Mistry 		seq_puts(m, "unknown");
393fe719888SAnand K Mistry 		break;
394fe719888SAnand K Mistry 	}
395af884cd4SKees Cook 	seq_putc(m, '\n');
3962f4b3bf6SKees Cook }
3972f4b3bf6SKees Cook 
task_context_switch_counts(struct seq_file * m,struct task_struct * p)398df5f8314SEric W. Biederman static inline void task_context_switch_counts(struct seq_file *m,
399df5f8314SEric W. Biederman 						struct task_struct *p)
400b663a79cSMaxim Uvarov {
40175ba1d07SJoe Perches 	seq_put_decimal_ull(m, "voluntary_ctxt_switches:\t", p->nvcsw);
40275ba1d07SJoe Perches 	seq_put_decimal_ull(m, "\nnonvoluntary_ctxt_switches:\t", p->nivcsw);
403f7a5f132SAlexey Dobriyan 	seq_putc(m, '\n');
404b663a79cSMaxim Uvarov }
405b663a79cSMaxim Uvarov 
task_cpus_allowed(struct seq_file * m,struct task_struct * task)406d01d4827SHeiko Carstens static void task_cpus_allowed(struct seq_file *m, struct task_struct *task)
407d01d4827SHeiko Carstens {
408a0c2e07dSTejun Heo 	seq_printf(m, "Cpus_allowed:\t%*pb\n",
40986fbcd3bSPeter Zijlstra 		   cpumask_pr_args(&task->cpus_mask));
410a0c2e07dSTejun Heo 	seq_printf(m, "Cpus_allowed_list:\t%*pbl\n",
41186fbcd3bSPeter Zijlstra 		   cpumask_pr_args(&task->cpus_mask));
412d01d4827SHeiko Carstens }
413d01d4827SHeiko Carstens 
task_core_dumping(struct seq_file * m,struct task_struct * task)4140258b5fdSEric W. Biederman static inline void task_core_dumping(struct seq_file *m, struct task_struct *task)
415c6434012SRoman Gushchin {
4160258b5fdSEric W. Biederman 	seq_put_decimal_ull(m, "CoreDumping:\t", !!task->signal->core_state);
417d0f02231SAndrei Vagin 	seq_putc(m, '\n');
418c6434012SRoman Gushchin }
419c6434012SRoman Gushchin 
task_thp_status(struct seq_file * m,struct mm_struct * mm)420a1400af7SMichal Hocko static inline void task_thp_status(struct seq_file *m, struct mm_struct *mm)
421a1400af7SMichal Hocko {
422a1400af7SMichal Hocko 	bool thp_enabled = IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE);
423a1400af7SMichal Hocko 
424a1400af7SMichal Hocko 	if (thp_enabled)
425a1400af7SMichal Hocko 		thp_enabled = !test_bit(MMF_DISABLE_THP, &mm->flags);
426a1400af7SMichal Hocko 	seq_printf(m, "THP_enabled:\t%d\n", thp_enabled);
427a1400af7SMichal Hocko }
428a1400af7SMichal Hocko 
task_untag_mask(struct seq_file * m,struct mm_struct * mm)429f7d30434SKirill A. Shutemov static inline void task_untag_mask(struct seq_file *m, struct mm_struct *mm)
430f7d30434SKirill A. Shutemov {
431f7d30434SKirill A. Shutemov 	seq_printf(m, "untag_mask:\t%#lx\n", mm_untag_mask(mm));
432f7d30434SKirill A. Shutemov }
433f7d30434SKirill A. Shutemov 
arch_proc_pid_thread_features(struct seq_file * m,struct task_struct * task)4340ee44885SRick Edgecombe __weak void arch_proc_pid_thread_features(struct seq_file *m,
4350ee44885SRick Edgecombe 					  struct task_struct *task)
4360ee44885SRick Edgecombe {
4370ee44885SRick Edgecombe }
4380ee44885SRick Edgecombe 
proc_pid_status(struct seq_file * m,struct pid_namespace * ns,struct pid * pid,struct task_struct * task)439df5f8314SEric W. Biederman int proc_pid_status(struct seq_file *m, struct pid_namespace *ns,
440df5f8314SEric W. Biederman 			struct pid *pid, struct task_struct *task)
4411da177e4SLinus Torvalds {
4421da177e4SLinus Torvalds 	struct mm_struct *mm = get_task_mm(task);
4431da177e4SLinus Torvalds 
44488b72b31STejun Heo 	seq_puts(m, "Name:\t");
44588b72b31STejun Heo 	proc_task_name(m, task, true);
44688b72b31STejun Heo 	seq_putc(m, '\n');
44788b72b31STejun Heo 
448df5f8314SEric W. Biederman 	task_state(m, ns, pid, task);
4491da177e4SLinus Torvalds 
4501da177e4SLinus Torvalds 	if (mm) {
451df5f8314SEric W. Biederman 		task_mem(m, mm);
4520258b5fdSEric W. Biederman 		task_core_dumping(m, task);
453a1400af7SMichal Hocko 		task_thp_status(m, mm);
454f7d30434SKirill A. Shutemov 		task_untag_mask(m, mm);
4551da177e4SLinus Torvalds 		mmput(mm);
4561da177e4SLinus Torvalds 	}
457df5f8314SEric W. Biederman 	task_sig(m, task);
458df5f8314SEric W. Biederman 	task_cap(m, task);
4592f4b3bf6SKees Cook 	task_seccomp(m, task);
460d01d4827SHeiko Carstens 	task_cpus_allowed(m, task);
461df5f8314SEric W. Biederman 	cpuset_task_status_allowed(m, task);
462df5f8314SEric W. Biederman 	task_context_switch_counts(m, task);
4630ee44885SRick Edgecombe 	arch_proc_pid_thread_features(m, task);
464df5f8314SEric W. Biederman 	return 0;
4651da177e4SLinus Torvalds }
4661da177e4SLinus Torvalds 
do_task_stat(struct seq_file * m,struct pid_namespace * ns,struct pid * pid,struct task_struct * task,int whole)467ee992744SEric W. Biederman static int do_task_stat(struct seq_file *m, struct pid_namespace *ns,
468ee992744SEric W. Biederman 			struct pid *pid, struct task_struct *task, int whole)
4691da177e4SLinus Torvalds {
470b2f73922SIngo Molnar 	unsigned long vsize, eip, esp, wchan = 0;
471715be1fcSJan Engelhardt 	int priority, nice;
4721da177e4SLinus Torvalds 	int tty_pgrp = -1, tty_nr = 0;
4731da177e4SLinus Torvalds 	sigset_t sigign, sigcatch;
4741da177e4SLinus Torvalds 	char state;
475a593d6edSOleg Nesterov 	pid_t ppid = 0, pgid = -1, sid = -1;
4761da177e4SLinus Torvalds 	int num_threads = 0;
477f83ce3e6SJake Edge 	int permitted;
4781da177e4SLinus Torvalds 	struct mm_struct *mm;
4791da177e4SLinus Torvalds 	unsigned long long start_time;
4801da177e4SLinus Torvalds 	unsigned long cmin_flt = 0, cmaj_flt = 0;
4811da177e4SLinus Torvalds 	unsigned long  min_flt = 0,  maj_flt = 0;
4825613fda9SFrederic Weisbecker 	u64 cutime, cstime, utime, stime;
48316a6d9beSFrederic Weisbecker 	u64 cgtime, gtime;
4841da177e4SLinus Torvalds 	unsigned long rsslim = 0;
485a593d6edSOleg Nesterov 	unsigned long flags;
4862d18f7f4SEric W. Biederman 	int exit_code = task->exit_code;
4871da177e4SLinus Torvalds 
4881da177e4SLinus Torvalds 	state = *get_task_state(task);
4891da177e4SLinus Torvalds 	vsize = eip = esp = 0;
490caaee623SJann Horn 	permitted = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS | PTRACE_MODE_NOAUDIT);
4911da177e4SLinus Torvalds 	mm = get_task_mm(task);
4921da177e4SLinus Torvalds 	if (mm) {
4931da177e4SLinus Torvalds 		vsize = task_vsize(mm);
4940a1eb2d4SAndy Lutomirski 		/*
4950a1eb2d4SAndy Lutomirski 		 * esp and eip are intentionally zeroed out.  There is no
4960a1eb2d4SAndy Lutomirski 		 * non-racy way to read them without freezing the task.
4970a1eb2d4SAndy Lutomirski 		 * Programs that need reliable values can use ptrace(2).
498fd7d5627SJohn Ogness 		 *
499fd7d5627SJohn Ogness 		 * The only exception is if the task is core dumping because
500fd7d5627SJohn Ogness 		 * a program is not able to use ptrace(2) in that case. It is
501fd7d5627SJohn Ogness 		 * safe because the task has stopped executing permanently.
5020a1eb2d4SAndy Lutomirski 		 */
503cb8f381fSJohn Ogness 		if (permitted && (task->flags & (PF_EXITING|PF_DUMPCORE))) {
5048bb2ee19SAlexey Dobriyan 			if (try_get_task_stack(task)) {
505fd7d5627SJohn Ogness 				eip = KSTK_EIP(task);
506fd7d5627SJohn Ogness 				esp = KSTK_ESP(task);
5078bb2ee19SAlexey Dobriyan 				put_task_stack(task);
5088bb2ee19SAlexey Dobriyan 			}
509fd7d5627SJohn Ogness 		}
510f83ce3e6SJake Edge 	}
5111da177e4SLinus Torvalds 
5121da177e4SLinus Torvalds 	sigemptyset(&sigign);
5131da177e4SLinus Torvalds 	sigemptyset(&sigcatch);
514*5d858e2dSOleg Nesterov 	cutime = cstime = 0;
51564861634SMartin Schwidefsky 	cgtime = gtime = 0;
5163cfd0885SAlan Cox 
517a593d6edSOleg Nesterov 	if (lock_task_sighand(task, &flags)) {
518a593d6edSOleg Nesterov 		struct signal_struct *sig = task->signal;
519a593d6edSOleg Nesterov 
52091593504SOleg Nesterov 		if (sig->tty) {
5215d0fdf1eSAlan Cox 			struct pid *pgrp = tty_get_pgrp(sig->tty);
5225d0fdf1eSAlan Cox 			tty_pgrp = pid_nr_ns(pgrp, ns);
5235d0fdf1eSAlan Cox 			put_pid(pgrp);
52491593504SOleg Nesterov 			tty_nr = new_encode_dev(tty_devnum(sig->tty));
525a593d6edSOleg Nesterov 		}
526a593d6edSOleg Nesterov 
5277e49827cSOleg Nesterov 		num_threads = get_nr_threads(task);
5281da177e4SLinus Torvalds 		collect_sigign_sigcatch(task, &sigign, &sigcatch);
5291da177e4SLinus Torvalds 
530a593d6edSOleg Nesterov 		cmin_flt = sig->cmin_flt;
531a593d6edSOleg Nesterov 		cmaj_flt = sig->cmaj_flt;
532a593d6edSOleg Nesterov 		cutime = sig->cutime;
533a593d6edSOleg Nesterov 		cstime = sig->cstime;
5349ac52315SLaurent Vivier 		cgtime = sig->cgtime;
5356aa7de05SMark Rutland 		rsslim = READ_ONCE(sig->rlim[RLIMIT_RSS].rlim_cur);
536a593d6edSOleg Nesterov 
5371da177e4SLinus Torvalds 		/* add up live thread stats at the group level */
5381da177e4SLinus Torvalds 		if (whole) {
539a593d6edSOleg Nesterov 			struct task_struct *t = task;
5401da177e4SLinus Torvalds 			do {
5411da177e4SLinus Torvalds 				min_flt += t->min_flt;
5421da177e4SLinus Torvalds 				maj_flt += t->maj_flt;
5436fac4829SFrederic Weisbecker 				gtime += task_gtime(t);
544185ee40eSOleg Nesterov 			} while_each_thread(task, t);
545a593d6edSOleg Nesterov 
546a593d6edSOleg Nesterov 			min_flt += sig->min_flt;
547a593d6edSOleg Nesterov 			maj_flt += sig->maj_flt;
54864861634SMartin Schwidefsky 			gtime += sig->gtime;
5492d18f7f4SEric W. Biederman 
5502d18f7f4SEric W. Biederman 			if (sig->flags & (SIGNAL_GROUP_EXIT | SIGNAL_STOP_STOPPED))
5512d18f7f4SEric W. Biederman 				exit_code = sig->group_exit_code;
5521da177e4SLinus Torvalds 		}
5531da177e4SLinus Torvalds 
554b488893aSPavel Emelyanov 		sid = task_session_nr_ns(task, ns);
555a98fdcefSOleg Nesterov 		ppid = task_tgid_nr_ns(task->real_parent, ns);
556b488893aSPavel Emelyanov 		pgid = task_pgrp_nr_ns(task, ns);
557a593d6edSOleg Nesterov 
558a593d6edSOleg Nesterov 		unlock_task_sighand(task, &flags);
5591da177e4SLinus Torvalds 	}
5601da177e4SLinus Torvalds 
561f83ce3e6SJake Edge 	if (permitted && (!whole || num_threads < 2))
5624e046156SKees Cook 		wchan = !task_is_running(task);
563*5d858e2dSOleg Nesterov 
564*5d858e2dSOleg Nesterov 	if (whole) {
565*5d858e2dSOleg Nesterov 		thread_group_cputime_adjusted(task, &utime, &stime);
566*5d858e2dSOleg Nesterov 	} else {
567*5d858e2dSOleg Nesterov 		task_cputime_adjusted(task, &utime, &stime);
5681da177e4SLinus Torvalds 		min_flt = task->min_flt;
5691da177e4SLinus Torvalds 		maj_flt = task->maj_flt;
5706fac4829SFrederic Weisbecker 		gtime = task_gtime(task);
5711da177e4SLinus Torvalds 	}
5721da177e4SLinus Torvalds 
5731da177e4SLinus Torvalds 	/* scale priority and nice values from timeslices to -20..20 */
5741da177e4SLinus Torvalds 	/* to make it look like a "normal" Unix priority/nice value  */
5751da177e4SLinus Torvalds 	priority = task_prio(task);
5761da177e4SLinus Torvalds 	nice = task_nice(task);
5771da177e4SLinus Torvalds 
5783ae700ecSMichael Weiß 	/* apply timens offset for boottime and convert nsec -> ticks */
5793ae700ecSMichael Weiß 	start_time =
5803ae700ecSMichael Weiß 		nsec_to_clock_t(timens_add_boottime_ns(task->start_boottime));
5811da177e4SLinus Torvalds 
582d0f02231SAndrei Vagin 	seq_put_decimal_ull(m, "", pid_nr_ns(pid, ns));
583d0f02231SAndrei Vagin 	seq_puts(m, " (");
58488b72b31STejun Heo 	proc_task_name(m, task, false);
585d0f02231SAndrei Vagin 	seq_puts(m, ") ");
586d0f02231SAndrei Vagin 	seq_putc(m, state);
58775ba1d07SJoe Perches 	seq_put_decimal_ll(m, " ", ppid);
58875ba1d07SJoe Perches 	seq_put_decimal_ll(m, " ", pgid);
58975ba1d07SJoe Perches 	seq_put_decimal_ll(m, " ", sid);
59075ba1d07SJoe Perches 	seq_put_decimal_ll(m, " ", tty_nr);
59175ba1d07SJoe Perches 	seq_put_decimal_ll(m, " ", tty_pgrp);
59275ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", task->flags);
59375ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", min_flt);
59475ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", cmin_flt);
59575ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", maj_flt);
59675ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", cmaj_flt);
5975613fda9SFrederic Weisbecker 	seq_put_decimal_ull(m, " ", nsec_to_clock_t(utime));
5985613fda9SFrederic Weisbecker 	seq_put_decimal_ull(m, " ", nsec_to_clock_t(stime));
5995613fda9SFrederic Weisbecker 	seq_put_decimal_ll(m, " ", nsec_to_clock_t(cutime));
6005613fda9SFrederic Weisbecker 	seq_put_decimal_ll(m, " ", nsec_to_clock_t(cstime));
60175ba1d07SJoe Perches 	seq_put_decimal_ll(m, " ", priority);
60275ba1d07SJoe Perches 	seq_put_decimal_ll(m, " ", nice);
60375ba1d07SJoe Perches 	seq_put_decimal_ll(m, " ", num_threads);
60475ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", 0);
60575ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", start_time);
60675ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", vsize);
60775ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", mm ? get_mm_rss(mm) : 0);
60875ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", rsslim);
60975ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", mm ? (permitted ? mm->start_code : 1) : 0);
61075ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", mm ? (permitted ? mm->end_code : 1) : 0);
61175ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", (permitted && mm) ? mm->start_stack : 0);
61275ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", esp);
61375ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", eip);
6141da177e4SLinus Torvalds 	/* The signal information here is obsolete.
6151da177e4SLinus Torvalds 	 * It must be decimal for Linux 2.0 compatibility.
6161da177e4SLinus Torvalds 	 * Use /proc/#/status for real-time signals.
6171da177e4SLinus Torvalds 	 */
61875ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", task->pending.signal.sig[0] & 0x7fffffffUL);
61975ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", task->blocked.sig[0] & 0x7fffffffUL);
62075ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", sigign.sig[0] & 0x7fffffffUL);
62175ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", sigcatch.sig[0] & 0x7fffffffUL);
622b2f73922SIngo Molnar 
623b2f73922SIngo Molnar 	/*
624b2f73922SIngo Molnar 	 * We used to output the absolute kernel address, but that's an
625b2f73922SIngo Molnar 	 * information leak - so instead we show a 0/1 flag here, to signal
626b2f73922SIngo Molnar 	 * to user-space whether there's a wchan field in /proc/PID/wchan.
627b2f73922SIngo Molnar 	 *
628b2f73922SIngo Molnar 	 * This works with older implementations of procps as well.
629b2f73922SIngo Molnar 	 */
6304e046156SKees Cook 	seq_put_decimal_ull(m, " ", wchan);
631b2f73922SIngo Molnar 
63275ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", 0);
63375ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", 0);
63475ba1d07SJoe Perches 	seq_put_decimal_ll(m, " ", task->exit_signal);
63575ba1d07SJoe Perches 	seq_put_decimal_ll(m, " ", task_cpu(task));
63675ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", task->rt_priority);
63775ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", task->policy);
63875ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", delayacct_blkio_ticks(task));
63916a6d9beSFrederic Weisbecker 	seq_put_decimal_ull(m, " ", nsec_to_clock_t(gtime));
64016a6d9beSFrederic Weisbecker 	seq_put_decimal_ll(m, " ", nsec_to_clock_t(cgtime));
6415b172087SCyrill Gorcunov 
6425b172087SCyrill Gorcunov 	if (mm && permitted) {
64375ba1d07SJoe Perches 		seq_put_decimal_ull(m, " ", mm->start_data);
64475ba1d07SJoe Perches 		seq_put_decimal_ull(m, " ", mm->end_data);
64575ba1d07SJoe Perches 		seq_put_decimal_ull(m, " ", mm->start_brk);
64675ba1d07SJoe Perches 		seq_put_decimal_ull(m, " ", mm->arg_start);
64775ba1d07SJoe Perches 		seq_put_decimal_ull(m, " ", mm->arg_end);
64875ba1d07SJoe Perches 		seq_put_decimal_ull(m, " ", mm->env_start);
64975ba1d07SJoe Perches 		seq_put_decimal_ull(m, " ", mm->env_end);
6505b172087SCyrill Gorcunov 	} else
65175ba1d07SJoe Perches 		seq_puts(m, " 0 0 0 0 0 0 0");
6525b172087SCyrill Gorcunov 
6535b172087SCyrill Gorcunov 	if (permitted)
6542d18f7f4SEric W. Biederman 		seq_put_decimal_ll(m, " ", exit_code);
6555b172087SCyrill Gorcunov 	else
65675ba1d07SJoe Perches 		seq_puts(m, " 0");
6575b172087SCyrill Gorcunov 
658bda7bad6SKAMEZAWA Hiroyuki 	seq_putc(m, '\n');
6591da177e4SLinus Torvalds 	if (mm)
6601da177e4SLinus Torvalds 		mmput(mm);
661ee992744SEric W. Biederman 	return 0;
6621da177e4SLinus Torvalds }
6631da177e4SLinus Torvalds 
proc_tid_stat(struct seq_file * m,struct pid_namespace * ns,struct pid * pid,struct task_struct * task)664ee992744SEric W. Biederman int proc_tid_stat(struct seq_file *m, struct pid_namespace *ns,
665ee992744SEric W. Biederman 			struct pid *pid, struct task_struct *task)
6661da177e4SLinus Torvalds {
667ee992744SEric W. Biederman 	return do_task_stat(m, ns, pid, task, 0);
6681da177e4SLinus Torvalds }
6691da177e4SLinus Torvalds 
proc_tgid_stat(struct seq_file * m,struct pid_namespace * ns,struct pid * pid,struct task_struct * task)670ee992744SEric W. Biederman int proc_tgid_stat(struct seq_file *m, struct pid_namespace *ns,
671ee992744SEric W. Biederman 			struct pid *pid, struct task_struct *task)
6721da177e4SLinus Torvalds {
673ee992744SEric W. Biederman 	return do_task_stat(m, ns, pid, task, 1);
6741da177e4SLinus Torvalds }
6751da177e4SLinus Torvalds 
proc_pid_statm(struct seq_file * m,struct pid_namespace * ns,struct pid * pid,struct task_struct * task)676a56d3fc7SEric W. Biederman int proc_pid_statm(struct seq_file *m, struct pid_namespace *ns,
677a56d3fc7SEric W. Biederman 			struct pid *pid, struct task_struct *task)
6781da177e4SLinus Torvalds {
6791da177e4SLinus Torvalds 	struct mm_struct *mm = get_task_mm(task);
6801da177e4SLinus Torvalds 
6811da177e4SLinus Torvalds 	if (mm) {
6825c5ab971SAlexey Dobriyan 		unsigned long size;
6835c5ab971SAlexey Dobriyan 		unsigned long resident = 0;
6845c5ab971SAlexey Dobriyan 		unsigned long shared = 0;
6855c5ab971SAlexey Dobriyan 		unsigned long text = 0;
6865c5ab971SAlexey Dobriyan 		unsigned long data = 0;
6875c5ab971SAlexey Dobriyan 
6881da177e4SLinus Torvalds 		size = task_statm(mm, &shared, &text, &data, &resident);
6891da177e4SLinus Torvalds 		mmput(mm);
6905c5ab971SAlexey Dobriyan 
691bda7bad6SKAMEZAWA Hiroyuki 		/*
692bda7bad6SKAMEZAWA Hiroyuki 		 * For quick read, open code by putting numbers directly
693bda7bad6SKAMEZAWA Hiroyuki 		 * expected format is
694bda7bad6SKAMEZAWA Hiroyuki 		 * seq_printf(m, "%lu %lu %lu %lu 0 %lu 0\n",
695bda7bad6SKAMEZAWA Hiroyuki 		 *               size, resident, shared, text, data);
696bda7bad6SKAMEZAWA Hiroyuki 		 */
69775ba1d07SJoe Perches 		seq_put_decimal_ull(m, "", size);
69875ba1d07SJoe Perches 		seq_put_decimal_ull(m, " ", resident);
69975ba1d07SJoe Perches 		seq_put_decimal_ull(m, " ", shared);
70075ba1d07SJoe Perches 		seq_put_decimal_ull(m, " ", text);
70175ba1d07SJoe Perches 		seq_put_decimal_ull(m, " ", 0);
70275ba1d07SJoe Perches 		seq_put_decimal_ull(m, " ", data);
70375ba1d07SJoe Perches 		seq_put_decimal_ull(m, " ", 0);
704bda7bad6SKAMEZAWA Hiroyuki 		seq_putc(m, '\n');
7055c5ab971SAlexey Dobriyan 	} else {
7065c5ab971SAlexey Dobriyan 		seq_write(m, "0 0 0 0 0 0 0\n", 14);
7075c5ab971SAlexey Dobriyan 	}
708a56d3fc7SEric W. Biederman 	return 0;
7091da177e4SLinus Torvalds }
71081841161SCyrill Gorcunov 
7112e13ba54SIago López Galeiras #ifdef CONFIG_PROC_CHILDREN
71281841161SCyrill Gorcunov static struct pid *
get_children_pid(struct inode * inode,struct pid * pid_prev,loff_t pos)71381841161SCyrill Gorcunov get_children_pid(struct inode *inode, struct pid *pid_prev, loff_t pos)
71481841161SCyrill Gorcunov {
71581841161SCyrill Gorcunov 	struct task_struct *start, *task;
71681841161SCyrill Gorcunov 	struct pid *pid = NULL;
71781841161SCyrill Gorcunov 
71881841161SCyrill Gorcunov 	read_lock(&tasklist_lock);
71981841161SCyrill Gorcunov 
72081841161SCyrill Gorcunov 	start = pid_task(proc_pid(inode), PIDTYPE_PID);
72181841161SCyrill Gorcunov 	if (!start)
72281841161SCyrill Gorcunov 		goto out;
72381841161SCyrill Gorcunov 
72481841161SCyrill Gorcunov 	/*
72581841161SCyrill Gorcunov 	 * Lets try to continue searching first, this gives
72681841161SCyrill Gorcunov 	 * us significant speedup on children-rich processes.
72781841161SCyrill Gorcunov 	 */
72881841161SCyrill Gorcunov 	if (pid_prev) {
72981841161SCyrill Gorcunov 		task = pid_task(pid_prev, PIDTYPE_PID);
73081841161SCyrill Gorcunov 		if (task && task->real_parent == start &&
73181841161SCyrill Gorcunov 		    !(list_empty(&task->sibling))) {
73281841161SCyrill Gorcunov 			if (list_is_last(&task->sibling, &start->children))
73381841161SCyrill Gorcunov 				goto out;
73481841161SCyrill Gorcunov 			task = list_first_entry(&task->sibling,
73581841161SCyrill Gorcunov 						struct task_struct, sibling);
73681841161SCyrill Gorcunov 			pid = get_pid(task_pid(task));
73781841161SCyrill Gorcunov 			goto out;
73881841161SCyrill Gorcunov 		}
73981841161SCyrill Gorcunov 	}
74081841161SCyrill Gorcunov 
74181841161SCyrill Gorcunov 	/*
74281841161SCyrill Gorcunov 	 * Slow search case.
74381841161SCyrill Gorcunov 	 *
74481841161SCyrill Gorcunov 	 * We might miss some children here if children
74581841161SCyrill Gorcunov 	 * are exited while we were not holding the lock,
74681841161SCyrill Gorcunov 	 * but it was never promised to be accurate that
74781841161SCyrill Gorcunov 	 * much.
74881841161SCyrill Gorcunov 	 *
74981841161SCyrill Gorcunov 	 * "Just suppose that the parent sleeps, but N children
75081841161SCyrill Gorcunov 	 *  exit after we printed their tids. Now the slow paths
75181841161SCyrill Gorcunov 	 *  skips N extra children, we miss N tasks." (c)
75281841161SCyrill Gorcunov 	 *
75381841161SCyrill Gorcunov 	 * So one need to stop or freeze the leader and all
75481841161SCyrill Gorcunov 	 * its children to get a precise result.
75581841161SCyrill Gorcunov 	 */
75681841161SCyrill Gorcunov 	list_for_each_entry(task, &start->children, sibling) {
75781841161SCyrill Gorcunov 		if (pos-- == 0) {
75881841161SCyrill Gorcunov 			pid = get_pid(task_pid(task));
75981841161SCyrill Gorcunov 			break;
76081841161SCyrill Gorcunov 		}
76181841161SCyrill Gorcunov 	}
76281841161SCyrill Gorcunov 
76381841161SCyrill Gorcunov out:
76481841161SCyrill Gorcunov 	read_unlock(&tasklist_lock);
76581841161SCyrill Gorcunov 	return pid;
76681841161SCyrill Gorcunov }
76781841161SCyrill Gorcunov 
children_seq_show(struct seq_file * seq,void * v)76881841161SCyrill Gorcunov static int children_seq_show(struct seq_file *seq, void *v)
76981841161SCyrill Gorcunov {
77004015e3fSChristoph Hellwig 	struct inode *inode = file_inode(seq->file);
77181841161SCyrill Gorcunov 
7729d78edeaSAlexey Gladkov 	seq_printf(seq, "%d ", pid_nr_ns(v, proc_pid_ns(inode->i_sb)));
77325ce3191SJoe Perches 	return 0;
77481841161SCyrill Gorcunov }
77581841161SCyrill Gorcunov 
children_seq_start(struct seq_file * seq,loff_t * pos)77681841161SCyrill Gorcunov static void *children_seq_start(struct seq_file *seq, loff_t *pos)
77781841161SCyrill Gorcunov {
77804015e3fSChristoph Hellwig 	return get_children_pid(file_inode(seq->file), NULL, *pos);
77981841161SCyrill Gorcunov }
78081841161SCyrill Gorcunov 
children_seq_next(struct seq_file * seq,void * v,loff_t * pos)78181841161SCyrill Gorcunov static void *children_seq_next(struct seq_file *seq, void *v, loff_t *pos)
78281841161SCyrill Gorcunov {
78381841161SCyrill Gorcunov 	struct pid *pid;
78481841161SCyrill Gorcunov 
78504015e3fSChristoph Hellwig 	pid = get_children_pid(file_inode(seq->file), v, *pos + 1);
78681841161SCyrill Gorcunov 	put_pid(v);
78781841161SCyrill Gorcunov 
78881841161SCyrill Gorcunov 	++*pos;
78981841161SCyrill Gorcunov 	return pid;
79081841161SCyrill Gorcunov }
79181841161SCyrill Gorcunov 
children_seq_stop(struct seq_file * seq,void * v)79281841161SCyrill Gorcunov static void children_seq_stop(struct seq_file *seq, void *v)
79381841161SCyrill Gorcunov {
79481841161SCyrill Gorcunov 	put_pid(v);
79581841161SCyrill Gorcunov }
79681841161SCyrill Gorcunov 
79781841161SCyrill Gorcunov static const struct seq_operations children_seq_ops = {
79881841161SCyrill Gorcunov 	.start	= children_seq_start,
79981841161SCyrill Gorcunov 	.next	= children_seq_next,
80081841161SCyrill Gorcunov 	.stop	= children_seq_stop,
80181841161SCyrill Gorcunov 	.show	= children_seq_show,
80281841161SCyrill Gorcunov };
80381841161SCyrill Gorcunov 
children_seq_open(struct inode * inode,struct file * file)80481841161SCyrill Gorcunov static int children_seq_open(struct inode *inode, struct file *file)
80581841161SCyrill Gorcunov {
80604015e3fSChristoph Hellwig 	return seq_open(file, &children_seq_ops);
80781841161SCyrill Gorcunov }
80881841161SCyrill Gorcunov 
80981841161SCyrill Gorcunov const struct file_operations proc_tid_children_operations = {
81081841161SCyrill Gorcunov 	.open    = children_seq_open,
81181841161SCyrill Gorcunov 	.read    = seq_read,
81281841161SCyrill Gorcunov 	.llseek  = seq_lseek,
813171ef917SAlexey Dobriyan 	.release = seq_release,
81481841161SCyrill Gorcunov };
8152e13ba54SIago López Galeiras #endif /* CONFIG_PROC_CHILDREN */
816