xref: /openbmc/linux/fs/proc/array.c (revision cf25e24d)
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>
591da177e4SLinus Torvalds #include <linux/kernel.h>
601da177e4SLinus Torvalds #include <linux/kernel_stat.h>
611da177e4SLinus Torvalds #include <linux/tty.h>
621da177e4SLinus Torvalds #include <linux/string.h>
631da177e4SLinus Torvalds #include <linux/mman.h>
646e84f315SIngo Molnar #include <linux/sched/mm.h>
656a3827d7SIngo Molnar #include <linux/sched/numa_balancing.h>
66fd7d5627SJohn Ogness #include <linux/sched/task_stack.h>
6729930025SIngo Molnar #include <linux/sched/task.h>
6832ef5517SIngo Molnar #include <linux/sched/cputime.h>
691da177e4SLinus Torvalds #include <linux/proc_fs.h>
701da177e4SLinus Torvalds #include <linux/ioport.h>
718ea02606SIngo Molnar #include <linux/uaccess.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>
900d094efeSRoland McGrath #include <linux/tracehook.h>
91edc924e0SAndy Shevchenko #include <linux/string_helpers.h>
92ae2975bcSEric W. Biederman #include <linux/user_namespace.h>
933e42979eSRichard W.M. Jones #include <linux/fs_struct.h>
941da177e4SLinus Torvalds 
951da177e4SLinus Torvalds #include <asm/pgtable.h>
961da177e4SLinus Torvalds #include <asm/processor.h>
971da177e4SLinus Torvalds #include "internal.h"
981da177e4SLinus Torvalds 
9988b72b31STejun Heo void proc_task_name(struct seq_file *m, struct task_struct *p, bool escape)
1001da177e4SLinus Torvalds {
101edc924e0SAndy Shevchenko 	char *buf;
1023a49f3d2SAndy Shevchenko 	size_t size;
1036b59808bSTejun Heo 	char tcomm[64];
1043a49f3d2SAndy Shevchenko 	int ret;
1051da177e4SLinus Torvalds 
1066b59808bSTejun Heo 	if (p->flags & PF_WQ_WORKER)
1076b59808bSTejun Heo 		wq_worker_comm(tcomm, sizeof(tcomm), p);
1086b59808bSTejun Heo 	else
1096b59808bSTejun Heo 		__get_task_comm(tcomm, sizeof(tcomm), p);
110edc924e0SAndy Shevchenko 
1113a49f3d2SAndy Shevchenko 	size = seq_get_buf(m, &buf);
11288b72b31STejun Heo 	if (escape) {
11388b72b31STejun Heo 		ret = string_escape_str(tcomm, buf, size,
11488b72b31STejun Heo 					ESCAPE_SPACE | ESCAPE_SPECIAL, "\n\\");
11588b72b31STejun Heo 		if (ret >= size)
11688b72b31STejun Heo 			ret = -1;
11788b72b31STejun Heo 	} else {
11888b72b31STejun Heo 		ret = strscpy(buf, tcomm, size);
11988b72b31STejun Heo 	}
120edc924e0SAndy Shevchenko 
12188b72b31STejun Heo 	seq_commit(m, ret);
1221da177e4SLinus Torvalds }
1231da177e4SLinus Torvalds 
1241da177e4SLinus Torvalds /*
1251da177e4SLinus Torvalds  * The task state array is a strange "bitmap" of
1261da177e4SLinus Torvalds  * reasons to sleep. Thus "running" is zero, and
1271da177e4SLinus Torvalds  * you can test for combinations of others with
1281da177e4SLinus Torvalds  * simple bit tests.
1291da177e4SLinus Torvalds  */
130e130aa70SMike Frysinger static const char * const task_state_array[] = {
13106eb6184SPeter Zijlstra 
13206eb6184SPeter Zijlstra 	/* states in TASK_REPORT: */
13306eb6184SPeter Zijlstra 	"R (running)",		/* 0x00 */
13406eb6184SPeter Zijlstra 	"S (sleeping)",		/* 0x01 */
13506eb6184SPeter Zijlstra 	"D (disk sleep)",	/* 0x02 */
13606eb6184SPeter Zijlstra 	"T (stopped)",		/* 0x04 */
13706eb6184SPeter Zijlstra 	"t (tracing stop)",	/* 0x08 */
13806eb6184SPeter Zijlstra 	"X (dead)",		/* 0x10 */
13906eb6184SPeter Zijlstra 	"Z (zombie)",		/* 0x20 */
1408ef9925bSPeter Zijlstra 	"P (parked)",		/* 0x40 */
14106eb6184SPeter Zijlstra 
14206eb6184SPeter Zijlstra 	/* states beyond TASK_REPORT: */
1438ef9925bSPeter Zijlstra 	"I (idle)",		/* 0x80 */
1441da177e4SLinus Torvalds };
1451da177e4SLinus Torvalds 
1461da177e4SLinus Torvalds static inline const char *get_task_state(struct task_struct *tsk)
1471da177e4SLinus Torvalds {
14806eb6184SPeter Zijlstra 	BUILD_BUG_ON(1 + ilog2(TASK_REPORT_MAX) != ARRAY_SIZE(task_state_array));
1491d48b080SPeter Zijlstra 	return task_state_array[task_state_index(tsk)];
1501da177e4SLinus Torvalds }
1511da177e4SLinus Torvalds 
152df5f8314SEric W. Biederman static inline void task_state(struct seq_file *m, struct pid_namespace *ns,
153df5f8314SEric W. Biederman 				struct pid *pid, struct task_struct *p)
1541da177e4SLinus Torvalds {
155e9f238c3SEric W. Biederman 	struct user_namespace *user_ns = seq_user_ns(m);
1561da177e4SLinus Torvalds 	struct group_info *group_info;
15768c3411fSMateusz Guzik 	int g, umask = -1;
158abdba6e9SOleg Nesterov 	struct task_struct *tracer;
159c69e8d9cSDavid Howells 	const struct cred *cred;
160abdba6e9SOleg Nesterov 	pid_t ppid, tpid = 0, tgid, ngid;
1610f4a0d53SOleg Nesterov 	unsigned int max_fds = 0;
1621da177e4SLinus Torvalds 
163b0fa9db6SOleg Nesterov 	rcu_read_lock();
164b488893aSPavel Emelyanov 	ppid = pid_alive(p) ?
165b488893aSPavel Emelyanov 		task_tgid_nr_ns(rcu_dereference(p->real_parent), ns) : 0;
166abdba6e9SOleg Nesterov 
167abdba6e9SOleg Nesterov 	tracer = ptrace_parent(p);
1680d094efeSRoland McGrath 	if (tracer)
1690d094efeSRoland McGrath 		tpid = task_pid_nr_ns(tracer, ns);
170b0fafc11SOleg Nesterov 
171b0fafc11SOleg Nesterov 	tgid = task_tgid_nr_ns(p, ns);
172b0fafc11SOleg Nesterov 	ngid = task_numa_group_id(p);
173de09a977SDavid Howells 	cred = get_task_cred(p);
1740f4a0d53SOleg Nesterov 
1750f4a0d53SOleg Nesterov 	task_lock(p);
17668c3411fSMateusz Guzik 	if (p->fs)
17768c3411fSMateusz Guzik 		umask = p->fs->umask;
1780f4a0d53SOleg Nesterov 	if (p->files)
1790f4a0d53SOleg Nesterov 		max_fds = files_fdtable(p->files)->max_fds;
1800f4a0d53SOleg Nesterov 	task_unlock(p);
181b0fafc11SOleg Nesterov 	rcu_read_unlock();
1820f4a0d53SOleg Nesterov 
18368c3411fSMateusz Guzik 	if (umask >= 0)
18468c3411fSMateusz Guzik 		seq_printf(m, "Umask:\t%#04o\n", umask);
185d0f02231SAndrei Vagin 	seq_puts(m, "State:\t");
186d0f02231SAndrei Vagin 	seq_puts(m, get_task_state(p));
1871da177e4SLinus Torvalds 
18875ba1d07SJoe Perches 	seq_put_decimal_ull(m, "\nTgid:\t", tgid);
18975ba1d07SJoe Perches 	seq_put_decimal_ull(m, "\nNgid:\t", ngid);
19075ba1d07SJoe Perches 	seq_put_decimal_ull(m, "\nPid:\t", pid_nr_ns(pid, ns));
19175ba1d07SJoe Perches 	seq_put_decimal_ull(m, "\nPPid:\t", ppid);
19275ba1d07SJoe Perches 	seq_put_decimal_ull(m, "\nTracerPid:\t", tpid);
19375ba1d07SJoe Perches 	seq_put_decimal_ull(m, "\nUid:\t", from_kuid_munged(user_ns, cred->uid));
19475ba1d07SJoe Perches 	seq_put_decimal_ull(m, "\t", from_kuid_munged(user_ns, cred->euid));
19575ba1d07SJoe Perches 	seq_put_decimal_ull(m, "\t", from_kuid_munged(user_ns, cred->suid));
19675ba1d07SJoe Perches 	seq_put_decimal_ull(m, "\t", from_kuid_munged(user_ns, cred->fsuid));
19775ba1d07SJoe Perches 	seq_put_decimal_ull(m, "\nGid:\t", from_kgid_munged(user_ns, cred->gid));
19875ba1d07SJoe Perches 	seq_put_decimal_ull(m, "\t", from_kgid_munged(user_ns, cred->egid));
19975ba1d07SJoe Perches 	seq_put_decimal_ull(m, "\t", from_kgid_munged(user_ns, cred->sgid));
20075ba1d07SJoe Perches 	seq_put_decimal_ull(m, "\t", from_kgid_munged(user_ns, cred->fsgid));
20175ba1d07SJoe Perches 	seq_put_decimal_ull(m, "\nFDSize:\t", max_fds);
202f7a5f132SAlexey Dobriyan 
203f7a5f132SAlexey Dobriyan 	seq_puts(m, "\nGroups:\t");
204c69e8d9cSDavid Howells 	group_info = cred->group_info;
2058d238027SArtem Bityutskiy 	for (g = 0; g < group_info->ngroups; g++)
20675ba1d07SJoe Perches 		seq_put_decimal_ull(m, g ? " " : "",
20781243eacSAlexey Dobriyan 				from_kgid_munged(user_ns, group_info->gid[g]));
208c69e8d9cSDavid Howells 	put_cred(cred);
209f7a5f132SAlexey Dobriyan 	/* Trailing space shouldn't have been added in the first place. */
210f7a5f132SAlexey Dobriyan 	seq_putc(m, ' ');
2111da177e4SLinus Torvalds 
212e4bc3324SChen Hanxiao #ifdef CONFIG_PID_NS
213e4bc3324SChen Hanxiao 	seq_puts(m, "\nNStgid:");
214e4bc3324SChen Hanxiao 	for (g = ns->level; g <= pid->level; g++)
21575ba1d07SJoe Perches 		seq_put_decimal_ull(m, "\t", task_tgid_nr_ns(p, pid->numbers[g].ns));
216e4bc3324SChen Hanxiao 	seq_puts(m, "\nNSpid:");
217e4bc3324SChen Hanxiao 	for (g = ns->level; g <= pid->level; g++)
21875ba1d07SJoe Perches 		seq_put_decimal_ull(m, "\t", task_pid_nr_ns(p, pid->numbers[g].ns));
219e4bc3324SChen Hanxiao 	seq_puts(m, "\nNSpgid:");
220e4bc3324SChen Hanxiao 	for (g = ns->level; g <= pid->level; g++)
22175ba1d07SJoe Perches 		seq_put_decimal_ull(m, "\t", task_pgrp_nr_ns(p, pid->numbers[g].ns));
222e4bc3324SChen Hanxiao 	seq_puts(m, "\nNSsid:");
223e4bc3324SChen Hanxiao 	for (g = ns->level; g <= pid->level; g++)
22475ba1d07SJoe Perches 		seq_put_decimal_ull(m, "\t", task_session_nr_ns(p, pid->numbers[g].ns));
225e4bc3324SChen Hanxiao #endif
2269d6de12fSAlexey Dobriyan 	seq_putc(m, '\n');
2271da177e4SLinus Torvalds }
2281da177e4SLinus Torvalds 
229138d22b5SCyrill Gorcunov void render_sigset_t(struct seq_file *m, const char *header,
230df5f8314SEric W. Biederman 				sigset_t *set)
2311da177e4SLinus Torvalds {
232df5f8314SEric W. Biederman 	int i;
2331da177e4SLinus Torvalds 
2349d6de12fSAlexey Dobriyan 	seq_puts(m, header);
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds 	i = _NSIG;
2371da177e4SLinus Torvalds 	do {
2381da177e4SLinus Torvalds 		int x = 0;
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds 		i -= 4;
2411da177e4SLinus Torvalds 		if (sigismember(set, i+1)) x |= 1;
2421da177e4SLinus Torvalds 		if (sigismember(set, i+2)) x |= 2;
2431da177e4SLinus Torvalds 		if (sigismember(set, i+3)) x |= 4;
2441da177e4SLinus Torvalds 		if (sigismember(set, i+4)) x |= 8;
245209b14dcSRasmus Villemoes 		seq_putc(m, hex_asc[x]);
2461da177e4SLinus Torvalds 	} while (i >= 4);
2471da177e4SLinus Torvalds 
2489d6de12fSAlexey Dobriyan 	seq_putc(m, '\n');
2491da177e4SLinus Torvalds }
2501da177e4SLinus Torvalds 
2511da177e4SLinus Torvalds static void collect_sigign_sigcatch(struct task_struct *p, sigset_t *ign,
2521da177e4SLinus Torvalds 				    sigset_t *catch)
2531da177e4SLinus Torvalds {
2541da177e4SLinus Torvalds 	struct k_sigaction *k;
2551da177e4SLinus Torvalds 	int i;
2561da177e4SLinus Torvalds 
2571da177e4SLinus Torvalds 	k = p->sighand->action;
2581da177e4SLinus Torvalds 	for (i = 1; i <= _NSIG; ++i, ++k) {
2591da177e4SLinus Torvalds 		if (k->sa.sa_handler == SIG_IGN)
2601da177e4SLinus Torvalds 			sigaddset(ign, i);
2611da177e4SLinus Torvalds 		else if (k->sa.sa_handler != SIG_DFL)
2621da177e4SLinus Torvalds 			sigaddset(catch, i);
2631da177e4SLinus Torvalds 	}
2641da177e4SLinus Torvalds }
2651da177e4SLinus Torvalds 
266df5f8314SEric W. Biederman static inline void task_sig(struct seq_file *m, struct task_struct *p)
2671da177e4SLinus Torvalds {
2685e6b3f42SOleg Nesterov 	unsigned long flags;
2691da177e4SLinus Torvalds 	sigset_t pending, shpending, blocked, ignored, caught;
2701da177e4SLinus Torvalds 	int num_threads = 0;
271197850a1SAlexey Dobriyan 	unsigned int qsize = 0;
2721da177e4SLinus Torvalds 	unsigned long qlim = 0;
2731da177e4SLinus Torvalds 
2741da177e4SLinus Torvalds 	sigemptyset(&pending);
2751da177e4SLinus Torvalds 	sigemptyset(&shpending);
2761da177e4SLinus Torvalds 	sigemptyset(&blocked);
2771da177e4SLinus Torvalds 	sigemptyset(&ignored);
2781da177e4SLinus Torvalds 	sigemptyset(&caught);
2791da177e4SLinus Torvalds 
2805e6b3f42SOleg Nesterov 	if (lock_task_sighand(p, &flags)) {
2811da177e4SLinus Torvalds 		pending = p->pending.signal;
2821da177e4SLinus Torvalds 		shpending = p->signal->shared_pending.signal;
2831da177e4SLinus Torvalds 		blocked = p->blocked;
2841da177e4SLinus Torvalds 		collect_sigign_sigcatch(p, &ignored, &caught);
2857e49827cSOleg Nesterov 		num_threads = get_nr_threads(p);
2867dc52157SPaul E. McKenney 		rcu_read_lock();  /* FIXME: is this correct? */
287c69e8d9cSDavid Howells 		qsize = atomic_read(&__task_cred(p)->user->sigpending);
2887dc52157SPaul E. McKenney 		rcu_read_unlock();
289d554ed89SJiri Slaby 		qlim = task_rlimit(p, RLIMIT_SIGPENDING);
2905e6b3f42SOleg Nesterov 		unlock_task_sighand(p, &flags);
2911da177e4SLinus Torvalds 	}
2921da177e4SLinus Torvalds 
29375ba1d07SJoe Perches 	seq_put_decimal_ull(m, "Threads:\t", num_threads);
29475ba1d07SJoe Perches 	seq_put_decimal_ull(m, "\nSigQ:\t", qsize);
29575ba1d07SJoe Perches 	seq_put_decimal_ull(m, "/", qlim);
2961da177e4SLinus Torvalds 
2971da177e4SLinus Torvalds 	/* render them all */
298f7a5f132SAlexey Dobriyan 	render_sigset_t(m, "\nSigPnd:\t", &pending);
299df5f8314SEric W. Biederman 	render_sigset_t(m, "ShdPnd:\t", &shpending);
300df5f8314SEric W. Biederman 	render_sigset_t(m, "SigBlk:\t", &blocked);
301df5f8314SEric W. Biederman 	render_sigset_t(m, "SigIgn:\t", &ignored);
302df5f8314SEric W. Biederman 	render_sigset_t(m, "SigCgt:\t", &caught);
3031da177e4SLinus Torvalds }
3041da177e4SLinus Torvalds 
305df5f8314SEric W. Biederman static void render_cap_t(struct seq_file *m, const char *header,
306df5f8314SEric W. Biederman 			kernel_cap_t *a)
307e338d263SAndrew Morgan {
308e338d263SAndrew Morgan 	unsigned __capi;
309e338d263SAndrew Morgan 
3109d6de12fSAlexey Dobriyan 	seq_puts(m, header);
311e338d263SAndrew Morgan 	CAP_FOR_EACH_U32(__capi) {
312d0f02231SAndrei Vagin 		seq_put_hex_ll(m, NULL,
313d0f02231SAndrei Vagin 			   a->cap[CAP_LAST_U32 - __capi], 8);
314e338d263SAndrew Morgan 	}
3159d6de12fSAlexey Dobriyan 	seq_putc(m, '\n');
316e338d263SAndrew Morgan }
317e338d263SAndrew Morgan 
318df5f8314SEric W. Biederman static inline void task_cap(struct seq_file *m, struct task_struct *p)
3191da177e4SLinus Torvalds {
320c69e8d9cSDavid Howells 	const struct cred *cred;
32158319057SAndy Lutomirski 	kernel_cap_t cap_inheritable, cap_permitted, cap_effective,
32258319057SAndy Lutomirski 			cap_bset, cap_ambient;
323b6dff3ecSDavid Howells 
324c69e8d9cSDavid Howells 	rcu_read_lock();
325c69e8d9cSDavid Howells 	cred = __task_cred(p);
326c69e8d9cSDavid Howells 	cap_inheritable	= cred->cap_inheritable;
327c69e8d9cSDavid Howells 	cap_permitted	= cred->cap_permitted;
328c69e8d9cSDavid Howells 	cap_effective	= cred->cap_effective;
329c69e8d9cSDavid Howells 	cap_bset	= cred->cap_bset;
33058319057SAndy Lutomirski 	cap_ambient	= cred->cap_ambient;
331c69e8d9cSDavid Howells 	rcu_read_unlock();
332c69e8d9cSDavid Howells 
333c69e8d9cSDavid Howells 	render_cap_t(m, "CapInh:\t", &cap_inheritable);
334c69e8d9cSDavid Howells 	render_cap_t(m, "CapPrm:\t", &cap_permitted);
335c69e8d9cSDavid Howells 	render_cap_t(m, "CapEff:\t", &cap_effective);
336c69e8d9cSDavid Howells 	render_cap_t(m, "CapBnd:\t", &cap_bset);
33758319057SAndy Lutomirski 	render_cap_t(m, "CapAmb:\t", &cap_ambient);
3381da177e4SLinus Torvalds }
3391da177e4SLinus Torvalds 
3402f4b3bf6SKees Cook static inline void task_seccomp(struct seq_file *m, struct task_struct *p)
3412f4b3bf6SKees Cook {
342af884cd4SKees Cook 	seq_put_decimal_ull(m, "NoNewPrivs:\t", task_no_new_privs(p));
3432f4b3bf6SKees Cook #ifdef CONFIG_SECCOMP
344af884cd4SKees Cook 	seq_put_decimal_ull(m, "\nSeccomp:\t", p->seccomp.mode);
3452f4b3bf6SKees Cook #endif
34608b55775SAlexey Dobriyan 	seq_puts(m, "\nSpeculation_Store_Bypass:\t");
347fae1fa0fSKees Cook 	switch (arch_prctl_spec_ctrl_get(p, PR_SPEC_STORE_BYPASS)) {
348fae1fa0fSKees Cook 	case -EINVAL:
34908b55775SAlexey Dobriyan 		seq_puts(m, "unknown");
350fae1fa0fSKees Cook 		break;
351fae1fa0fSKees Cook 	case PR_SPEC_NOT_AFFECTED:
35208b55775SAlexey Dobriyan 		seq_puts(m, "not vulnerable");
353fae1fa0fSKees Cook 		break;
354356e4bffSThomas Gleixner 	case PR_SPEC_PRCTL | PR_SPEC_FORCE_DISABLE:
35508b55775SAlexey Dobriyan 		seq_puts(m, "thread force mitigated");
356356e4bffSThomas Gleixner 		break;
357fae1fa0fSKees Cook 	case PR_SPEC_PRCTL | PR_SPEC_DISABLE:
35808b55775SAlexey Dobriyan 		seq_puts(m, "thread mitigated");
359fae1fa0fSKees Cook 		break;
360fae1fa0fSKees Cook 	case PR_SPEC_PRCTL | PR_SPEC_ENABLE:
36108b55775SAlexey Dobriyan 		seq_puts(m, "thread vulnerable");
362fae1fa0fSKees Cook 		break;
363fae1fa0fSKees Cook 	case PR_SPEC_DISABLE:
36408b55775SAlexey Dobriyan 		seq_puts(m, "globally mitigated");
365fae1fa0fSKees Cook 		break;
366fae1fa0fSKees Cook 	default:
36708b55775SAlexey Dobriyan 		seq_puts(m, "vulnerable");
368fae1fa0fSKees Cook 		break;
369fae1fa0fSKees Cook 	}
370af884cd4SKees Cook 	seq_putc(m, '\n');
3712f4b3bf6SKees Cook }
3722f4b3bf6SKees Cook 
373df5f8314SEric W. Biederman static inline void task_context_switch_counts(struct seq_file *m,
374df5f8314SEric W. Biederman 						struct task_struct *p)
375b663a79cSMaxim Uvarov {
37675ba1d07SJoe Perches 	seq_put_decimal_ull(m, "voluntary_ctxt_switches:\t", p->nvcsw);
37775ba1d07SJoe Perches 	seq_put_decimal_ull(m, "\nnonvoluntary_ctxt_switches:\t", p->nivcsw);
378f7a5f132SAlexey Dobriyan 	seq_putc(m, '\n');
379b663a79cSMaxim Uvarov }
380b663a79cSMaxim Uvarov 
381d01d4827SHeiko Carstens static void task_cpus_allowed(struct seq_file *m, struct task_struct *task)
382d01d4827SHeiko Carstens {
383a0c2e07dSTejun Heo 	seq_printf(m, "Cpus_allowed:\t%*pb\n",
3843bd37062SSebastian Andrzej Siewior 		   cpumask_pr_args(task->cpus_ptr));
385a0c2e07dSTejun Heo 	seq_printf(m, "Cpus_allowed_list:\t%*pbl\n",
3863bd37062SSebastian Andrzej Siewior 		   cpumask_pr_args(task->cpus_ptr));
387d01d4827SHeiko Carstens }
388d01d4827SHeiko Carstens 
389c6434012SRoman Gushchin static inline void task_core_dumping(struct seq_file *m, struct mm_struct *mm)
390c6434012SRoman Gushchin {
391d0f02231SAndrei Vagin 	seq_put_decimal_ull(m, "CoreDumping:\t", !!mm->core_state);
392d0f02231SAndrei Vagin 	seq_putc(m, '\n');
393c6434012SRoman Gushchin }
394c6434012SRoman Gushchin 
395a1400af7SMichal Hocko static inline void task_thp_status(struct seq_file *m, struct mm_struct *mm)
396a1400af7SMichal Hocko {
397a1400af7SMichal Hocko 	bool thp_enabled = IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE);
398a1400af7SMichal Hocko 
399a1400af7SMichal Hocko 	if (thp_enabled)
400a1400af7SMichal Hocko 		thp_enabled = !test_bit(MMF_DISABLE_THP, &mm->flags);
401a1400af7SMichal Hocko 	seq_printf(m, "THP_enabled:\t%d\n", thp_enabled);
402a1400af7SMichal Hocko }
403a1400af7SMichal Hocko 
404df5f8314SEric W. Biederman int proc_pid_status(struct seq_file *m, struct pid_namespace *ns,
405df5f8314SEric W. Biederman 			struct pid *pid, struct task_struct *task)
4061da177e4SLinus Torvalds {
4071da177e4SLinus Torvalds 	struct mm_struct *mm = get_task_mm(task);
4081da177e4SLinus Torvalds 
40988b72b31STejun Heo 	seq_puts(m, "Name:\t");
41088b72b31STejun Heo 	proc_task_name(m, task, true);
41188b72b31STejun Heo 	seq_putc(m, '\n');
41288b72b31STejun Heo 
413df5f8314SEric W. Biederman 	task_state(m, ns, pid, task);
4141da177e4SLinus Torvalds 
4151da177e4SLinus Torvalds 	if (mm) {
416df5f8314SEric W. Biederman 		task_mem(m, mm);
417c6434012SRoman Gushchin 		task_core_dumping(m, mm);
418a1400af7SMichal Hocko 		task_thp_status(m, mm);
4191da177e4SLinus Torvalds 		mmput(mm);
4201da177e4SLinus Torvalds 	}
421df5f8314SEric W. Biederman 	task_sig(m, task);
422df5f8314SEric W. Biederman 	task_cap(m, task);
4232f4b3bf6SKees Cook 	task_seccomp(m, task);
424d01d4827SHeiko Carstens 	task_cpus_allowed(m, task);
425df5f8314SEric W. Biederman 	cpuset_task_status_allowed(m, task);
426df5f8314SEric W. Biederman 	task_context_switch_counts(m, task);
427df5f8314SEric W. Biederman 	return 0;
4281da177e4SLinus Torvalds }
4291da177e4SLinus Torvalds 
430ee992744SEric W. Biederman static int do_task_stat(struct seq_file *m, struct pid_namespace *ns,
431ee992744SEric W. Biederman 			struct pid *pid, struct task_struct *task, int whole)
4321da177e4SLinus Torvalds {
433b2f73922SIngo Molnar 	unsigned long vsize, eip, esp, wchan = 0;
434715be1fcSJan Engelhardt 	int priority, nice;
4351da177e4SLinus Torvalds 	int tty_pgrp = -1, tty_nr = 0;
4361da177e4SLinus Torvalds 	sigset_t sigign, sigcatch;
4371da177e4SLinus Torvalds 	char state;
438a593d6edSOleg Nesterov 	pid_t ppid = 0, pgid = -1, sid = -1;
4391da177e4SLinus Torvalds 	int num_threads = 0;
440f83ce3e6SJake Edge 	int permitted;
4411da177e4SLinus Torvalds 	struct mm_struct *mm;
4421da177e4SLinus Torvalds 	unsigned long long start_time;
4431da177e4SLinus Torvalds 	unsigned long cmin_flt = 0, cmaj_flt = 0;
4441da177e4SLinus Torvalds 	unsigned long  min_flt = 0,  maj_flt = 0;
4455613fda9SFrederic Weisbecker 	u64 cutime, cstime, utime, stime;
44616a6d9beSFrederic Weisbecker 	u64 cgtime, gtime;
4471da177e4SLinus Torvalds 	unsigned long rsslim = 0;
448a593d6edSOleg Nesterov 	unsigned long flags;
4491da177e4SLinus Torvalds 
4501da177e4SLinus Torvalds 	state = *get_task_state(task);
4511da177e4SLinus Torvalds 	vsize = eip = esp = 0;
452caaee623SJann Horn 	permitted = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS | PTRACE_MODE_NOAUDIT);
4531da177e4SLinus Torvalds 	mm = get_task_mm(task);
4541da177e4SLinus Torvalds 	if (mm) {
4551da177e4SLinus Torvalds 		vsize = task_vsize(mm);
4560a1eb2d4SAndy Lutomirski 		/*
4570a1eb2d4SAndy Lutomirski 		 * esp and eip are intentionally zeroed out.  There is no
4580a1eb2d4SAndy Lutomirski 		 * non-racy way to read them without freezing the task.
4590a1eb2d4SAndy Lutomirski 		 * Programs that need reliable values can use ptrace(2).
460fd7d5627SJohn Ogness 		 *
461fd7d5627SJohn Ogness 		 * The only exception is if the task is core dumping because
462fd7d5627SJohn Ogness 		 * a program is not able to use ptrace(2) in that case. It is
463fd7d5627SJohn Ogness 		 * safe because the task has stopped executing permanently.
4640a1eb2d4SAndy Lutomirski 		 */
465cb8f381fSJohn Ogness 		if (permitted && (task->flags & (PF_EXITING|PF_DUMPCORE))) {
4668bb2ee19SAlexey Dobriyan 			if (try_get_task_stack(task)) {
467fd7d5627SJohn Ogness 				eip = KSTK_EIP(task);
468fd7d5627SJohn Ogness 				esp = KSTK_ESP(task);
4698bb2ee19SAlexey Dobriyan 				put_task_stack(task);
4708bb2ee19SAlexey Dobriyan 			}
471fd7d5627SJohn Ogness 		}
472f83ce3e6SJake Edge 	}
4731da177e4SLinus Torvalds 
4741da177e4SLinus Torvalds 	sigemptyset(&sigign);
4751da177e4SLinus Torvalds 	sigemptyset(&sigcatch);
47664861634SMartin Schwidefsky 	cutime = cstime = utime = stime = 0;
47764861634SMartin Schwidefsky 	cgtime = gtime = 0;
4783cfd0885SAlan Cox 
479a593d6edSOleg Nesterov 	if (lock_task_sighand(task, &flags)) {
480a593d6edSOleg Nesterov 		struct signal_struct *sig = task->signal;
481a593d6edSOleg Nesterov 
48291593504SOleg Nesterov 		if (sig->tty) {
4835d0fdf1eSAlan Cox 			struct pid *pgrp = tty_get_pgrp(sig->tty);
4845d0fdf1eSAlan Cox 			tty_pgrp = pid_nr_ns(pgrp, ns);
4855d0fdf1eSAlan Cox 			put_pid(pgrp);
48691593504SOleg Nesterov 			tty_nr = new_encode_dev(tty_devnum(sig->tty));
487a593d6edSOleg Nesterov 		}
488a593d6edSOleg Nesterov 
4897e49827cSOleg Nesterov 		num_threads = get_nr_threads(task);
4901da177e4SLinus Torvalds 		collect_sigign_sigcatch(task, &sigign, &sigcatch);
4911da177e4SLinus Torvalds 
492a593d6edSOleg Nesterov 		cmin_flt = sig->cmin_flt;
493a593d6edSOleg Nesterov 		cmaj_flt = sig->cmaj_flt;
494a593d6edSOleg Nesterov 		cutime = sig->cutime;
495a593d6edSOleg Nesterov 		cstime = sig->cstime;
4969ac52315SLaurent Vivier 		cgtime = sig->cgtime;
4976aa7de05SMark Rutland 		rsslim = READ_ONCE(sig->rlim[RLIMIT_RSS].rlim_cur);
498a593d6edSOleg Nesterov 
4991da177e4SLinus Torvalds 		/* add up live thread stats at the group level */
5001da177e4SLinus Torvalds 		if (whole) {
501a593d6edSOleg Nesterov 			struct task_struct *t = task;
5021da177e4SLinus Torvalds 			do {
5031da177e4SLinus Torvalds 				min_flt += t->min_flt;
5041da177e4SLinus Torvalds 				maj_flt += t->maj_flt;
5056fac4829SFrederic Weisbecker 				gtime += task_gtime(t);
506185ee40eSOleg Nesterov 			} while_each_thread(task, t);
507a593d6edSOleg Nesterov 
508a593d6edSOleg Nesterov 			min_flt += sig->min_flt;
509a593d6edSOleg Nesterov 			maj_flt += sig->maj_flt;
510e80d0a1aSFrederic Weisbecker 			thread_group_cputime_adjusted(task, &utime, &stime);
51164861634SMartin Schwidefsky 			gtime += sig->gtime;
5121da177e4SLinus Torvalds 		}
5131da177e4SLinus Torvalds 
514b488893aSPavel Emelyanov 		sid = task_session_nr_ns(task, ns);
515a98fdcefSOleg Nesterov 		ppid = task_tgid_nr_ns(task->real_parent, ns);
516b488893aSPavel Emelyanov 		pgid = task_pgrp_nr_ns(task, ns);
517a593d6edSOleg Nesterov 
518a593d6edSOleg Nesterov 		unlock_task_sighand(task, &flags);
5191da177e4SLinus Torvalds 	}
5201da177e4SLinus Torvalds 
521f83ce3e6SJake Edge 	if (permitted && (!whole || num_threads < 2))
5221da177e4SLinus Torvalds 		wchan = get_wchan(task);
5231da177e4SLinus Torvalds 	if (!whole) {
5241da177e4SLinus Torvalds 		min_flt = task->min_flt;
5251da177e4SLinus Torvalds 		maj_flt = task->maj_flt;
526e80d0a1aSFrederic Weisbecker 		task_cputime_adjusted(task, &utime, &stime);
5276fac4829SFrederic Weisbecker 		gtime = task_gtime(task);
5281da177e4SLinus Torvalds 	}
5291da177e4SLinus Torvalds 
5301da177e4SLinus Torvalds 	/* scale priority and nice values from timeslices to -20..20 */
5311da177e4SLinus Torvalds 	/* to make it look like a "normal" Unix priority/nice value  */
5321da177e4SLinus Torvalds 	priority = task_prio(task);
5331da177e4SLinus Torvalds 	nice = task_nice(task);
5341da177e4SLinus Torvalds 
5351da177e4SLinus Torvalds 	/* convert nsec -> ticks */
536cf25e24dSPeter Zijlstra 	start_time = nsec_to_clock_t(task->start_boottime);
5371da177e4SLinus Torvalds 
538d0f02231SAndrei Vagin 	seq_put_decimal_ull(m, "", pid_nr_ns(pid, ns));
539d0f02231SAndrei Vagin 	seq_puts(m, " (");
54088b72b31STejun Heo 	proc_task_name(m, task, false);
541d0f02231SAndrei Vagin 	seq_puts(m, ") ");
542d0f02231SAndrei Vagin 	seq_putc(m, state);
54375ba1d07SJoe Perches 	seq_put_decimal_ll(m, " ", ppid);
54475ba1d07SJoe Perches 	seq_put_decimal_ll(m, " ", pgid);
54575ba1d07SJoe Perches 	seq_put_decimal_ll(m, " ", sid);
54675ba1d07SJoe Perches 	seq_put_decimal_ll(m, " ", tty_nr);
54775ba1d07SJoe Perches 	seq_put_decimal_ll(m, " ", tty_pgrp);
54875ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", task->flags);
54975ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", min_flt);
55075ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", cmin_flt);
55175ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", maj_flt);
55275ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", cmaj_flt);
5535613fda9SFrederic Weisbecker 	seq_put_decimal_ull(m, " ", nsec_to_clock_t(utime));
5545613fda9SFrederic Weisbecker 	seq_put_decimal_ull(m, " ", nsec_to_clock_t(stime));
5555613fda9SFrederic Weisbecker 	seq_put_decimal_ll(m, " ", nsec_to_clock_t(cutime));
5565613fda9SFrederic Weisbecker 	seq_put_decimal_ll(m, " ", nsec_to_clock_t(cstime));
55775ba1d07SJoe Perches 	seq_put_decimal_ll(m, " ", priority);
55875ba1d07SJoe Perches 	seq_put_decimal_ll(m, " ", nice);
55975ba1d07SJoe Perches 	seq_put_decimal_ll(m, " ", num_threads);
56075ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", 0);
56175ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", start_time);
56275ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", vsize);
56375ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", mm ? get_mm_rss(mm) : 0);
56475ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", rsslim);
56575ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", mm ? (permitted ? mm->start_code : 1) : 0);
56675ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", mm ? (permitted ? mm->end_code : 1) : 0);
56775ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", (permitted && mm) ? mm->start_stack : 0);
56875ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", esp);
56975ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", eip);
5701da177e4SLinus Torvalds 	/* The signal information here is obsolete.
5711da177e4SLinus Torvalds 	 * It must be decimal for Linux 2.0 compatibility.
5721da177e4SLinus Torvalds 	 * Use /proc/#/status for real-time signals.
5731da177e4SLinus Torvalds 	 */
57475ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", task->pending.signal.sig[0] & 0x7fffffffUL);
57575ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", task->blocked.sig[0] & 0x7fffffffUL);
57675ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", sigign.sig[0] & 0x7fffffffUL);
57775ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", sigcatch.sig[0] & 0x7fffffffUL);
578b2f73922SIngo Molnar 
579b2f73922SIngo Molnar 	/*
580b2f73922SIngo Molnar 	 * We used to output the absolute kernel address, but that's an
581b2f73922SIngo Molnar 	 * information leak - so instead we show a 0/1 flag here, to signal
582b2f73922SIngo Molnar 	 * to user-space whether there's a wchan field in /proc/PID/wchan.
583b2f73922SIngo Molnar 	 *
584b2f73922SIngo Molnar 	 * This works with older implementations of procps as well.
585b2f73922SIngo Molnar 	 */
586b2f73922SIngo Molnar 	if (wchan)
587b2f73922SIngo Molnar 		seq_puts(m, " 1");
588b2f73922SIngo Molnar 	else
589b2f73922SIngo Molnar 		seq_puts(m, " 0");
590b2f73922SIngo Molnar 
59175ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", 0);
59275ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", 0);
59375ba1d07SJoe Perches 	seq_put_decimal_ll(m, " ", task->exit_signal);
59475ba1d07SJoe Perches 	seq_put_decimal_ll(m, " ", task_cpu(task));
59575ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", task->rt_priority);
59675ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", task->policy);
59775ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", delayacct_blkio_ticks(task));
59816a6d9beSFrederic Weisbecker 	seq_put_decimal_ull(m, " ", nsec_to_clock_t(gtime));
59916a6d9beSFrederic Weisbecker 	seq_put_decimal_ll(m, " ", nsec_to_clock_t(cgtime));
6005b172087SCyrill Gorcunov 
6015b172087SCyrill Gorcunov 	if (mm && permitted) {
60275ba1d07SJoe Perches 		seq_put_decimal_ull(m, " ", mm->start_data);
60375ba1d07SJoe Perches 		seq_put_decimal_ull(m, " ", mm->end_data);
60475ba1d07SJoe Perches 		seq_put_decimal_ull(m, " ", mm->start_brk);
60575ba1d07SJoe Perches 		seq_put_decimal_ull(m, " ", mm->arg_start);
60675ba1d07SJoe Perches 		seq_put_decimal_ull(m, " ", mm->arg_end);
60775ba1d07SJoe Perches 		seq_put_decimal_ull(m, " ", mm->env_start);
60875ba1d07SJoe Perches 		seq_put_decimal_ull(m, " ", mm->env_end);
6095b172087SCyrill Gorcunov 	} else
61075ba1d07SJoe Perches 		seq_puts(m, " 0 0 0 0 0 0 0");
6115b172087SCyrill Gorcunov 
6125b172087SCyrill Gorcunov 	if (permitted)
61375ba1d07SJoe Perches 		seq_put_decimal_ll(m, " ", task->exit_code);
6145b172087SCyrill Gorcunov 	else
61575ba1d07SJoe Perches 		seq_puts(m, " 0");
6165b172087SCyrill Gorcunov 
617bda7bad6SKAMEZAWA Hiroyuki 	seq_putc(m, '\n');
6181da177e4SLinus Torvalds 	if (mm)
6191da177e4SLinus Torvalds 		mmput(mm);
620ee992744SEric W. Biederman 	return 0;
6211da177e4SLinus Torvalds }
6221da177e4SLinus Torvalds 
623ee992744SEric W. Biederman int proc_tid_stat(struct seq_file *m, struct pid_namespace *ns,
624ee992744SEric W. Biederman 			struct pid *pid, struct task_struct *task)
6251da177e4SLinus Torvalds {
626ee992744SEric W. Biederman 	return do_task_stat(m, ns, pid, task, 0);
6271da177e4SLinus Torvalds }
6281da177e4SLinus Torvalds 
629ee992744SEric W. Biederman int proc_tgid_stat(struct seq_file *m, struct pid_namespace *ns,
630ee992744SEric W. Biederman 			struct pid *pid, struct task_struct *task)
6311da177e4SLinus Torvalds {
632ee992744SEric W. Biederman 	return do_task_stat(m, ns, pid, task, 1);
6331da177e4SLinus Torvalds }
6341da177e4SLinus Torvalds 
635a56d3fc7SEric W. Biederman int proc_pid_statm(struct seq_file *m, struct pid_namespace *ns,
636a56d3fc7SEric W. Biederman 			struct pid *pid, struct task_struct *task)
6371da177e4SLinus Torvalds {
638a2ade7b6SAlexey Dobriyan 	unsigned long size = 0, resident = 0, shared = 0, text = 0, data = 0;
6391da177e4SLinus Torvalds 	struct mm_struct *mm = get_task_mm(task);
6401da177e4SLinus Torvalds 
6411da177e4SLinus Torvalds 	if (mm) {
6421da177e4SLinus Torvalds 		size = task_statm(mm, &shared, &text, &data, &resident);
6431da177e4SLinus Torvalds 		mmput(mm);
6441da177e4SLinus Torvalds 	}
645bda7bad6SKAMEZAWA Hiroyuki 	/*
646bda7bad6SKAMEZAWA Hiroyuki 	 * For quick read, open code by putting numbers directly
647bda7bad6SKAMEZAWA Hiroyuki 	 * expected format is
648bda7bad6SKAMEZAWA Hiroyuki 	 * seq_printf(m, "%lu %lu %lu %lu 0 %lu 0\n",
649bda7bad6SKAMEZAWA Hiroyuki 	 *               size, resident, shared, text, data);
650bda7bad6SKAMEZAWA Hiroyuki 	 */
65175ba1d07SJoe Perches 	seq_put_decimal_ull(m, "", size);
65275ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", resident);
65375ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", shared);
65475ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", text);
65575ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", 0);
65675ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", data);
65775ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", 0);
658bda7bad6SKAMEZAWA Hiroyuki 	seq_putc(m, '\n');
659a56d3fc7SEric W. Biederman 
660a56d3fc7SEric W. Biederman 	return 0;
6611da177e4SLinus Torvalds }
66281841161SCyrill Gorcunov 
6632e13ba54SIago López Galeiras #ifdef CONFIG_PROC_CHILDREN
66481841161SCyrill Gorcunov static struct pid *
66581841161SCyrill Gorcunov get_children_pid(struct inode *inode, struct pid *pid_prev, loff_t pos)
66681841161SCyrill Gorcunov {
66781841161SCyrill Gorcunov 	struct task_struct *start, *task;
66881841161SCyrill Gorcunov 	struct pid *pid = NULL;
66981841161SCyrill Gorcunov 
67081841161SCyrill Gorcunov 	read_lock(&tasklist_lock);
67181841161SCyrill Gorcunov 
67281841161SCyrill Gorcunov 	start = pid_task(proc_pid(inode), PIDTYPE_PID);
67381841161SCyrill Gorcunov 	if (!start)
67481841161SCyrill Gorcunov 		goto out;
67581841161SCyrill Gorcunov 
67681841161SCyrill Gorcunov 	/*
67781841161SCyrill Gorcunov 	 * Lets try to continue searching first, this gives
67881841161SCyrill Gorcunov 	 * us significant speedup on children-rich processes.
67981841161SCyrill Gorcunov 	 */
68081841161SCyrill Gorcunov 	if (pid_prev) {
68181841161SCyrill Gorcunov 		task = pid_task(pid_prev, PIDTYPE_PID);
68281841161SCyrill Gorcunov 		if (task && task->real_parent == start &&
68381841161SCyrill Gorcunov 		    !(list_empty(&task->sibling))) {
68481841161SCyrill Gorcunov 			if (list_is_last(&task->sibling, &start->children))
68581841161SCyrill Gorcunov 				goto out;
68681841161SCyrill Gorcunov 			task = list_first_entry(&task->sibling,
68781841161SCyrill Gorcunov 						struct task_struct, sibling);
68881841161SCyrill Gorcunov 			pid = get_pid(task_pid(task));
68981841161SCyrill Gorcunov 			goto out;
69081841161SCyrill Gorcunov 		}
69181841161SCyrill Gorcunov 	}
69281841161SCyrill Gorcunov 
69381841161SCyrill Gorcunov 	/*
69481841161SCyrill Gorcunov 	 * Slow search case.
69581841161SCyrill Gorcunov 	 *
69681841161SCyrill Gorcunov 	 * We might miss some children here if children
69781841161SCyrill Gorcunov 	 * are exited while we were not holding the lock,
69881841161SCyrill Gorcunov 	 * but it was never promised to be accurate that
69981841161SCyrill Gorcunov 	 * much.
70081841161SCyrill Gorcunov 	 *
70181841161SCyrill Gorcunov 	 * "Just suppose that the parent sleeps, but N children
70281841161SCyrill Gorcunov 	 *  exit after we printed their tids. Now the slow paths
70381841161SCyrill Gorcunov 	 *  skips N extra children, we miss N tasks." (c)
70481841161SCyrill Gorcunov 	 *
70581841161SCyrill Gorcunov 	 * So one need to stop or freeze the leader and all
70681841161SCyrill Gorcunov 	 * its children to get a precise result.
70781841161SCyrill Gorcunov 	 */
70881841161SCyrill Gorcunov 	list_for_each_entry(task, &start->children, sibling) {
70981841161SCyrill Gorcunov 		if (pos-- == 0) {
71081841161SCyrill Gorcunov 			pid = get_pid(task_pid(task));
71181841161SCyrill Gorcunov 			break;
71281841161SCyrill Gorcunov 		}
71381841161SCyrill Gorcunov 	}
71481841161SCyrill Gorcunov 
71581841161SCyrill Gorcunov out:
71681841161SCyrill Gorcunov 	read_unlock(&tasklist_lock);
71781841161SCyrill Gorcunov 	return pid;
71881841161SCyrill Gorcunov }
71981841161SCyrill Gorcunov 
72081841161SCyrill Gorcunov static int children_seq_show(struct seq_file *seq, void *v)
72181841161SCyrill Gorcunov {
72204015e3fSChristoph Hellwig 	struct inode *inode = file_inode(seq->file);
72381841161SCyrill Gorcunov 
72404015e3fSChristoph Hellwig 	seq_printf(seq, "%d ", pid_nr_ns(v, proc_pid_ns(inode)));
72525ce3191SJoe Perches 	return 0;
72681841161SCyrill Gorcunov }
72781841161SCyrill Gorcunov 
72881841161SCyrill Gorcunov static void *children_seq_start(struct seq_file *seq, loff_t *pos)
72981841161SCyrill Gorcunov {
73004015e3fSChristoph Hellwig 	return get_children_pid(file_inode(seq->file), NULL, *pos);
73181841161SCyrill Gorcunov }
73281841161SCyrill Gorcunov 
73381841161SCyrill Gorcunov static void *children_seq_next(struct seq_file *seq, void *v, loff_t *pos)
73481841161SCyrill Gorcunov {
73581841161SCyrill Gorcunov 	struct pid *pid;
73681841161SCyrill Gorcunov 
73704015e3fSChristoph Hellwig 	pid = get_children_pid(file_inode(seq->file), v, *pos + 1);
73881841161SCyrill Gorcunov 	put_pid(v);
73981841161SCyrill Gorcunov 
74081841161SCyrill Gorcunov 	++*pos;
74181841161SCyrill Gorcunov 	return pid;
74281841161SCyrill Gorcunov }
74381841161SCyrill Gorcunov 
74481841161SCyrill Gorcunov static void children_seq_stop(struct seq_file *seq, void *v)
74581841161SCyrill Gorcunov {
74681841161SCyrill Gorcunov 	put_pid(v);
74781841161SCyrill Gorcunov }
74881841161SCyrill Gorcunov 
74981841161SCyrill Gorcunov static const struct seq_operations children_seq_ops = {
75081841161SCyrill Gorcunov 	.start	= children_seq_start,
75181841161SCyrill Gorcunov 	.next	= children_seq_next,
75281841161SCyrill Gorcunov 	.stop	= children_seq_stop,
75381841161SCyrill Gorcunov 	.show	= children_seq_show,
75481841161SCyrill Gorcunov };
75581841161SCyrill Gorcunov 
75681841161SCyrill Gorcunov static int children_seq_open(struct inode *inode, struct file *file)
75781841161SCyrill Gorcunov {
75804015e3fSChristoph Hellwig 	return seq_open(file, &children_seq_ops);
75981841161SCyrill Gorcunov }
76081841161SCyrill Gorcunov 
76181841161SCyrill Gorcunov const struct file_operations proc_tid_children_operations = {
76281841161SCyrill Gorcunov 	.open    = children_seq_open,
76381841161SCyrill Gorcunov 	.read    = seq_read,
76481841161SCyrill Gorcunov 	.llseek  = seq_lseek,
765171ef917SAlexey Dobriyan 	.release = seq_release,
76681841161SCyrill Gorcunov };
7672e13ba54SIago López Galeiras #endif /* CONFIG_PROC_CHILDREN */
768