xref: /openbmc/linux/fs/proc/array.c (revision 6b59808b)
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>
88f83ce3e6SJake Edge #include <linux/ptrace.h>
890d094efeSRoland McGrath #include <linux/tracehook.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>
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds #include <asm/pgtable.h>
951da177e4SLinus Torvalds #include <asm/processor.h>
961da177e4SLinus Torvalds #include "internal.h"
971da177e4SLinus Torvalds 
9888b72b31STejun Heo void proc_task_name(struct seq_file *m, struct task_struct *p, bool escape)
991da177e4SLinus Torvalds {
100edc924e0SAndy Shevchenko 	char *buf;
1013a49f3d2SAndy Shevchenko 	size_t size;
1026b59808bSTejun Heo 	char tcomm[64];
1033a49f3d2SAndy Shevchenko 	int ret;
1041da177e4SLinus Torvalds 
1056b59808bSTejun Heo 	if (p->flags & PF_WQ_WORKER)
1066b59808bSTejun Heo 		wq_worker_comm(tcomm, sizeof(tcomm), p);
1076b59808bSTejun Heo 	else
1086b59808bSTejun Heo 		__get_task_comm(tcomm, sizeof(tcomm), p);
1091da177e4SLinus Torvalds 
1103a49f3d2SAndy Shevchenko 	size = seq_get_buf(m, &buf);
11188b72b31STejun Heo 	if (escape) {
11288b72b31STejun Heo 		ret = string_escape_str(tcomm, buf, size,
11388b72b31STejun Heo 					ESCAPE_SPACE | ESCAPE_SPECIAL, "\n\\");
11488b72b31STejun Heo 		if (ret >= size)
11588b72b31STejun Heo 			ret = -1;
11688b72b31STejun Heo 	} else {
11788b72b31STejun Heo 		ret = strscpy(buf, tcomm, size);
11888b72b31STejun Heo 	}
119edc924e0SAndy Shevchenko 
12088b72b31STejun Heo 	seq_commit(m, ret);
1211da177e4SLinus Torvalds }
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds /*
1241da177e4SLinus Torvalds  * The task state array is a strange "bitmap" of
1251da177e4SLinus Torvalds  * reasons to sleep. Thus "running" is zero, and
1261da177e4SLinus Torvalds  * you can test for combinations of others with
1271da177e4SLinus Torvalds  * simple bit tests.
1281da177e4SLinus Torvalds  */
129e130aa70SMike Frysinger static const char * const task_state_array[] = {
13006eb6184SPeter Zijlstra 
13106eb6184SPeter Zijlstra 	/* states in TASK_REPORT: */
13206eb6184SPeter Zijlstra 	"R (running)",		/* 0x00 */
13306eb6184SPeter Zijlstra 	"S (sleeping)",		/* 0x01 */
13406eb6184SPeter Zijlstra 	"D (disk sleep)",	/* 0x02 */
13506eb6184SPeter Zijlstra 	"T (stopped)",		/* 0x04 */
13606eb6184SPeter Zijlstra 	"t (tracing stop)",	/* 0x08 */
13706eb6184SPeter Zijlstra 	"X (dead)",		/* 0x10 */
13806eb6184SPeter Zijlstra 	"Z (zombie)",		/* 0x20 */
1398ef9925bSPeter Zijlstra 	"P (parked)",		/* 0x40 */
14006eb6184SPeter Zijlstra 
14106eb6184SPeter Zijlstra 	/* states beyond TASK_REPORT: */
1428ef9925bSPeter Zijlstra 	"I (idle)",		/* 0x80 */
1431da177e4SLinus Torvalds };
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds static inline const char *get_task_state(struct task_struct *tsk)
1461da177e4SLinus Torvalds {
14706eb6184SPeter Zijlstra 	BUILD_BUG_ON(1 + ilog2(TASK_REPORT_MAX) != ARRAY_SIZE(task_state_array));
1481d48b080SPeter Zijlstra 	return task_state_array[task_state_index(tsk)];
1491da177e4SLinus Torvalds }
1501da177e4SLinus Torvalds 
151df5f8314SEric W. Biederman static inline void task_state(struct seq_file *m, struct pid_namespace *ns,
152df5f8314SEric W. Biederman 				struct pid *pid, struct task_struct *p)
1531da177e4SLinus Torvalds {
154e9f238c3SEric W. Biederman 	struct user_namespace *user_ns = seq_user_ns(m);
1551da177e4SLinus Torvalds 	struct group_info *group_info;
15668c3411fSMateusz Guzik 	int g, umask = -1;
157abdba6e9SOleg Nesterov 	struct task_struct *tracer;
158c69e8d9cSDavid Howells 	const struct cred *cred;
159abdba6e9SOleg Nesterov 	pid_t ppid, tpid = 0, tgid, ngid;
1600f4a0d53SOleg Nesterov 	unsigned int max_fds = 0;
1611da177e4SLinus Torvalds 
162b0fa9db6SOleg Nesterov 	rcu_read_lock();
163b488893aSPavel Emelyanov 	ppid = pid_alive(p) ?
164b488893aSPavel Emelyanov 		task_tgid_nr_ns(rcu_dereference(p->real_parent), ns) : 0;
165abdba6e9SOleg Nesterov 
166abdba6e9SOleg Nesterov 	tracer = ptrace_parent(p);
1670d094efeSRoland McGrath 	if (tracer)
1680d094efeSRoland McGrath 		tpid = task_pid_nr_ns(tracer, ns);
169b0fafc11SOleg Nesterov 
170b0fafc11SOleg Nesterov 	tgid = task_tgid_nr_ns(p, ns);
171b0fafc11SOleg Nesterov 	ngid = task_numa_group_id(p);
172de09a977SDavid Howells 	cred = get_task_cred(p);
1730f4a0d53SOleg Nesterov 
1740f4a0d53SOleg Nesterov 	task_lock(p);
17568c3411fSMateusz Guzik 	if (p->fs)
17668c3411fSMateusz Guzik 		umask = p->fs->umask;
1770f4a0d53SOleg Nesterov 	if (p->files)
1780f4a0d53SOleg Nesterov 		max_fds = files_fdtable(p->files)->max_fds;
1790f4a0d53SOleg Nesterov 	task_unlock(p);
180b0fafc11SOleg Nesterov 	rcu_read_unlock();
1810f4a0d53SOleg Nesterov 
18268c3411fSMateusz Guzik 	if (umask >= 0)
18368c3411fSMateusz Guzik 		seq_printf(m, "Umask:\t%#04o\n", umask);
184d0f02231SAndrei Vagin 	seq_puts(m, "State:\t");
185d0f02231SAndrei Vagin 	seq_puts(m, get_task_state(p));
1861da177e4SLinus Torvalds 
18775ba1d07SJoe Perches 	seq_put_decimal_ull(m, "\nTgid:\t", tgid);
18875ba1d07SJoe Perches 	seq_put_decimal_ull(m, "\nNgid:\t", ngid);
18975ba1d07SJoe Perches 	seq_put_decimal_ull(m, "\nPid:\t", pid_nr_ns(pid, ns));
19075ba1d07SJoe Perches 	seq_put_decimal_ull(m, "\nPPid:\t", ppid);
19175ba1d07SJoe Perches 	seq_put_decimal_ull(m, "\nTracerPid:\t", tpid);
19275ba1d07SJoe Perches 	seq_put_decimal_ull(m, "\nUid:\t", from_kuid_munged(user_ns, cred->uid));
19375ba1d07SJoe Perches 	seq_put_decimal_ull(m, "\t", from_kuid_munged(user_ns, cred->euid));
19475ba1d07SJoe Perches 	seq_put_decimal_ull(m, "\t", from_kuid_munged(user_ns, cred->suid));
19575ba1d07SJoe Perches 	seq_put_decimal_ull(m, "\t", from_kuid_munged(user_ns, cred->fsuid));
19675ba1d07SJoe Perches 	seq_put_decimal_ull(m, "\nGid:\t", from_kgid_munged(user_ns, cred->gid));
19775ba1d07SJoe Perches 	seq_put_decimal_ull(m, "\t", from_kgid_munged(user_ns, cred->egid));
19875ba1d07SJoe Perches 	seq_put_decimal_ull(m, "\t", from_kgid_munged(user_ns, cred->sgid));
19975ba1d07SJoe Perches 	seq_put_decimal_ull(m, "\t", from_kgid_munged(user_ns, cred->fsgid));
20075ba1d07SJoe Perches 	seq_put_decimal_ull(m, "\nFDSize:\t", max_fds);
201f7a5f132SAlexey Dobriyan 
202f7a5f132SAlexey Dobriyan 	seq_puts(m, "\nGroups:\t");
203c69e8d9cSDavid Howells 	group_info = cred->group_info;
2048d238027SArtem Bityutskiy 	for (g = 0; g < group_info->ngroups; g++)
20575ba1d07SJoe Perches 		seq_put_decimal_ull(m, g ? " " : "",
20681243eacSAlexey Dobriyan 				from_kgid_munged(user_ns, group_info->gid[g]));
207c69e8d9cSDavid Howells 	put_cred(cred);
208f7a5f132SAlexey Dobriyan 	/* Trailing space shouldn't have been added in the first place. */
209f7a5f132SAlexey Dobriyan 	seq_putc(m, ' ');
2101da177e4SLinus Torvalds 
211e4bc3324SChen Hanxiao #ifdef CONFIG_PID_NS
212e4bc3324SChen Hanxiao 	seq_puts(m, "\nNStgid:");
213e4bc3324SChen Hanxiao 	for (g = ns->level; g <= pid->level; g++)
21475ba1d07SJoe Perches 		seq_put_decimal_ull(m, "\t", task_tgid_nr_ns(p, pid->numbers[g].ns));
215e4bc3324SChen Hanxiao 	seq_puts(m, "\nNSpid:");
216e4bc3324SChen Hanxiao 	for (g = ns->level; g <= pid->level; g++)
21775ba1d07SJoe Perches 		seq_put_decimal_ull(m, "\t", task_pid_nr_ns(p, pid->numbers[g].ns));
218e4bc3324SChen Hanxiao 	seq_puts(m, "\nNSpgid:");
219e4bc3324SChen Hanxiao 	for (g = ns->level; g <= pid->level; g++)
22075ba1d07SJoe Perches 		seq_put_decimal_ull(m, "\t", task_pgrp_nr_ns(p, pid->numbers[g].ns));
221e4bc3324SChen Hanxiao 	seq_puts(m, "\nNSsid:");
222e4bc3324SChen Hanxiao 	for (g = ns->level; g <= pid->level; g++)
22375ba1d07SJoe Perches 		seq_put_decimal_ull(m, "\t", task_session_nr_ns(p, pid->numbers[g].ns));
224e4bc3324SChen Hanxiao #endif
2259d6de12fSAlexey Dobriyan 	seq_putc(m, '\n');
2261da177e4SLinus Torvalds }
2271da177e4SLinus Torvalds 
228138d22b5SCyrill Gorcunov void render_sigset_t(struct seq_file *m, const char *header,
229df5f8314SEric W. Biederman 				sigset_t *set)
2301da177e4SLinus Torvalds {
231df5f8314SEric W. Biederman 	int i;
2321da177e4SLinus Torvalds 
2339d6de12fSAlexey Dobriyan 	seq_puts(m, header);
2341da177e4SLinus Torvalds 
2351da177e4SLinus Torvalds 	i = _NSIG;
2361da177e4SLinus Torvalds 	do {
2371da177e4SLinus Torvalds 		int x = 0;
2381da177e4SLinus Torvalds 
2391da177e4SLinus Torvalds 		i -= 4;
2401da177e4SLinus Torvalds 		if (sigismember(set, i+1)) x |= 1;
2411da177e4SLinus Torvalds 		if (sigismember(set, i+2)) x |= 2;
2421da177e4SLinus Torvalds 		if (sigismember(set, i+3)) x |= 4;
2431da177e4SLinus Torvalds 		if (sigismember(set, i+4)) x |= 8;
244209b14dcSRasmus Villemoes 		seq_putc(m, hex_asc[x]);
2451da177e4SLinus Torvalds 	} while (i >= 4);
2461da177e4SLinus Torvalds 
2479d6de12fSAlexey Dobriyan 	seq_putc(m, '\n');
2481da177e4SLinus Torvalds }
2491da177e4SLinus Torvalds 
2501da177e4SLinus Torvalds static void collect_sigign_sigcatch(struct task_struct *p, sigset_t *ign,
2511da177e4SLinus Torvalds 				    sigset_t *catch)
2521da177e4SLinus Torvalds {
2531da177e4SLinus Torvalds 	struct k_sigaction *k;
2541da177e4SLinus Torvalds 	int i;
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds 	k = p->sighand->action;
2571da177e4SLinus Torvalds 	for (i = 1; i <= _NSIG; ++i, ++k) {
2581da177e4SLinus Torvalds 		if (k->sa.sa_handler == SIG_IGN)
2591da177e4SLinus Torvalds 			sigaddset(ign, i);
2601da177e4SLinus Torvalds 		else if (k->sa.sa_handler != SIG_DFL)
2611da177e4SLinus Torvalds 			sigaddset(catch, i);
2621da177e4SLinus Torvalds 	}
2631da177e4SLinus Torvalds }
2641da177e4SLinus Torvalds 
265df5f8314SEric W. Biederman static inline void task_sig(struct seq_file *m, struct task_struct *p)
2661da177e4SLinus Torvalds {
2675e6b3f42SOleg Nesterov 	unsigned long flags;
2681da177e4SLinus Torvalds 	sigset_t pending, shpending, blocked, ignored, caught;
2691da177e4SLinus Torvalds 	int num_threads = 0;
2701da177e4SLinus Torvalds 	unsigned long qsize = 0;
2711da177e4SLinus Torvalds 	unsigned long qlim = 0;
2721da177e4SLinus Torvalds 
2731da177e4SLinus Torvalds 	sigemptyset(&pending);
2741da177e4SLinus Torvalds 	sigemptyset(&shpending);
2751da177e4SLinus Torvalds 	sigemptyset(&blocked);
2761da177e4SLinus Torvalds 	sigemptyset(&ignored);
2771da177e4SLinus Torvalds 	sigemptyset(&caught);
2781da177e4SLinus Torvalds 
2795e6b3f42SOleg Nesterov 	if (lock_task_sighand(p, &flags)) {
2801da177e4SLinus Torvalds 		pending = p->pending.signal;
2811da177e4SLinus Torvalds 		shpending = p->signal->shared_pending.signal;
2821da177e4SLinus Torvalds 		blocked = p->blocked;
2831da177e4SLinus Torvalds 		collect_sigign_sigcatch(p, &ignored, &caught);
2847e49827cSOleg Nesterov 		num_threads = get_nr_threads(p);
2857dc52157SPaul E. McKenney 		rcu_read_lock();  /* FIXME: is this correct? */
286c69e8d9cSDavid Howells 		qsize = atomic_read(&__task_cred(p)->user->sigpending);
2877dc52157SPaul E. McKenney 		rcu_read_unlock();
288d554ed89SJiri Slaby 		qlim = task_rlimit(p, RLIMIT_SIGPENDING);
2895e6b3f42SOleg Nesterov 		unlock_task_sighand(p, &flags);
2901da177e4SLinus Torvalds 	}
2911da177e4SLinus Torvalds 
29275ba1d07SJoe Perches 	seq_put_decimal_ull(m, "Threads:\t", num_threads);
29375ba1d07SJoe Perches 	seq_put_decimal_ull(m, "\nSigQ:\t", qsize);
29475ba1d07SJoe Perches 	seq_put_decimal_ull(m, "/", qlim);
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds 	/* render them all */
297f7a5f132SAlexey Dobriyan 	render_sigset_t(m, "\nSigPnd:\t", &pending);
298df5f8314SEric W. Biederman 	render_sigset_t(m, "ShdPnd:\t", &shpending);
299df5f8314SEric W. Biederman 	render_sigset_t(m, "SigBlk:\t", &blocked);
300df5f8314SEric W. Biederman 	render_sigset_t(m, "SigIgn:\t", &ignored);
301df5f8314SEric W. Biederman 	render_sigset_t(m, "SigCgt:\t", &caught);
3021da177e4SLinus Torvalds }
3031da177e4SLinus Torvalds 
304df5f8314SEric W. Biederman static void render_cap_t(struct seq_file *m, const char *header,
305df5f8314SEric W. Biederman 			kernel_cap_t *a)
306e338d263SAndrew Morgan {
307e338d263SAndrew Morgan 	unsigned __capi;
308e338d263SAndrew Morgan 
3099d6de12fSAlexey Dobriyan 	seq_puts(m, header);
310e338d263SAndrew Morgan 	CAP_FOR_EACH_U32(__capi) {
311d0f02231SAndrei Vagin 		seq_put_hex_ll(m, NULL,
312d0f02231SAndrei Vagin 			   a->cap[CAP_LAST_U32 - __capi], 8);
313e338d263SAndrew Morgan 	}
3149d6de12fSAlexey Dobriyan 	seq_putc(m, '\n');
315e338d263SAndrew Morgan }
316e338d263SAndrew Morgan 
317df5f8314SEric W. Biederman static inline void task_cap(struct seq_file *m, struct task_struct *p)
3181da177e4SLinus Torvalds {
319c69e8d9cSDavid Howells 	const struct cred *cred;
32058319057SAndy Lutomirski 	kernel_cap_t cap_inheritable, cap_permitted, cap_effective,
32158319057SAndy Lutomirski 			cap_bset, cap_ambient;
322b6dff3ecSDavid Howells 
323c69e8d9cSDavid Howells 	rcu_read_lock();
324c69e8d9cSDavid Howells 	cred = __task_cred(p);
325c69e8d9cSDavid Howells 	cap_inheritable	= cred->cap_inheritable;
326c69e8d9cSDavid Howells 	cap_permitted	= cred->cap_permitted;
327c69e8d9cSDavid Howells 	cap_effective	= cred->cap_effective;
328c69e8d9cSDavid Howells 	cap_bset	= cred->cap_bset;
32958319057SAndy Lutomirski 	cap_ambient	= cred->cap_ambient;
330c69e8d9cSDavid Howells 	rcu_read_unlock();
331c69e8d9cSDavid Howells 
332c69e8d9cSDavid Howells 	render_cap_t(m, "CapInh:\t", &cap_inheritable);
333c69e8d9cSDavid Howells 	render_cap_t(m, "CapPrm:\t", &cap_permitted);
334c69e8d9cSDavid Howells 	render_cap_t(m, "CapEff:\t", &cap_effective);
335c69e8d9cSDavid Howells 	render_cap_t(m, "CapBnd:\t", &cap_bset);
33658319057SAndy Lutomirski 	render_cap_t(m, "CapAmb:\t", &cap_ambient);
3371da177e4SLinus Torvalds }
3381da177e4SLinus Torvalds 
3392f4b3bf6SKees Cook static inline void task_seccomp(struct seq_file *m, struct task_struct *p)
3402f4b3bf6SKees Cook {
341af884cd4SKees Cook 	seq_put_decimal_ull(m, "NoNewPrivs:\t", task_no_new_privs(p));
3422f4b3bf6SKees Cook #ifdef CONFIG_SECCOMP
343af884cd4SKees Cook 	seq_put_decimal_ull(m, "\nSeccomp:\t", p->seccomp.mode);
3442f4b3bf6SKees Cook #endif
345af884cd4SKees Cook 	seq_putc(m, '\n');
3462f4b3bf6SKees Cook }
3472f4b3bf6SKees Cook 
348df5f8314SEric W. Biederman static inline void task_context_switch_counts(struct seq_file *m,
349df5f8314SEric W. Biederman 						struct task_struct *p)
350b663a79cSMaxim Uvarov {
35175ba1d07SJoe Perches 	seq_put_decimal_ull(m, "voluntary_ctxt_switches:\t", p->nvcsw);
35275ba1d07SJoe Perches 	seq_put_decimal_ull(m, "\nnonvoluntary_ctxt_switches:\t", p->nivcsw);
353f7a5f132SAlexey Dobriyan 	seq_putc(m, '\n');
354b663a79cSMaxim Uvarov }
355b663a79cSMaxim Uvarov 
356d01d4827SHeiko Carstens static void task_cpus_allowed(struct seq_file *m, struct task_struct *task)
357d01d4827SHeiko Carstens {
358a0c2e07dSTejun Heo 	seq_printf(m, "Cpus_allowed:\t%*pb\n",
359a0c2e07dSTejun Heo 		   cpumask_pr_args(&task->cpus_allowed));
360a0c2e07dSTejun Heo 	seq_printf(m, "Cpus_allowed_list:\t%*pbl\n",
361a0c2e07dSTejun Heo 		   cpumask_pr_args(&task->cpus_allowed));
362d01d4827SHeiko Carstens }
363d01d4827SHeiko Carstens 
364c6434012SRoman Gushchin static inline void task_core_dumping(struct seq_file *m, struct mm_struct *mm)
365c6434012SRoman Gushchin {
366d0f02231SAndrei Vagin 	seq_put_decimal_ull(m, "CoreDumping:\t", !!mm->core_state);
367d0f02231SAndrei Vagin 	seq_putc(m, '\n');
368c6434012SRoman Gushchin }
369c6434012SRoman Gushchin 
370df5f8314SEric W. Biederman int proc_pid_status(struct seq_file *m, struct pid_namespace *ns,
371df5f8314SEric W. Biederman 			struct pid *pid, struct task_struct *task)
3721da177e4SLinus Torvalds {
3731da177e4SLinus Torvalds 	struct mm_struct *mm = get_task_mm(task);
3741da177e4SLinus Torvalds 
37588b72b31STejun Heo 	seq_puts(m, "Name:\t");
37688b72b31STejun Heo 	proc_task_name(m, task, true);
37788b72b31STejun Heo 	seq_putc(m, '\n');
37888b72b31STejun Heo 
379df5f8314SEric W. Biederman 	task_state(m, ns, pid, task);
3801da177e4SLinus Torvalds 
3811da177e4SLinus Torvalds 	if (mm) {
382df5f8314SEric W. Biederman 		task_mem(m, mm);
383c6434012SRoman Gushchin 		task_core_dumping(m, mm);
3841da177e4SLinus Torvalds 		mmput(mm);
3851da177e4SLinus Torvalds 	}
386df5f8314SEric W. Biederman 	task_sig(m, task);
387df5f8314SEric W. Biederman 	task_cap(m, task);
3882f4b3bf6SKees Cook 	task_seccomp(m, task);
389d01d4827SHeiko Carstens 	task_cpus_allowed(m, task);
390df5f8314SEric W. Biederman 	cpuset_task_status_allowed(m, task);
391df5f8314SEric W. Biederman 	task_context_switch_counts(m, task);
392df5f8314SEric W. Biederman 	return 0;
3931da177e4SLinus Torvalds }
3941da177e4SLinus Torvalds 
395ee992744SEric W. Biederman static int do_task_stat(struct seq_file *m, struct pid_namespace *ns,
396ee992744SEric W. Biederman 			struct pid *pid, struct task_struct *task, int whole)
3971da177e4SLinus Torvalds {
398b2f73922SIngo Molnar 	unsigned long vsize, eip, esp, wchan = 0;
399715be1fcSJan Engelhardt 	int priority, nice;
4001da177e4SLinus Torvalds 	int tty_pgrp = -1, tty_nr = 0;
4011da177e4SLinus Torvalds 	sigset_t sigign, sigcatch;
4021da177e4SLinus Torvalds 	char state;
403a593d6edSOleg Nesterov 	pid_t ppid = 0, pgid = -1, sid = -1;
4041da177e4SLinus Torvalds 	int num_threads = 0;
405f83ce3e6SJake Edge 	int permitted;
4061da177e4SLinus Torvalds 	struct mm_struct *mm;
4071da177e4SLinus Torvalds 	unsigned long long start_time;
4081da177e4SLinus Torvalds 	unsigned long cmin_flt = 0, cmaj_flt = 0;
4091da177e4SLinus Torvalds 	unsigned long  min_flt = 0,  maj_flt = 0;
4105613fda9SFrederic Weisbecker 	u64 cutime, cstime, utime, stime;
41116a6d9beSFrederic Weisbecker 	u64 cgtime, gtime;
4121da177e4SLinus Torvalds 	unsigned long rsslim = 0;
413a593d6edSOleg Nesterov 	unsigned long flags;
4141da177e4SLinus Torvalds 
4151da177e4SLinus Torvalds 	state = *get_task_state(task);
4161da177e4SLinus Torvalds 	vsize = eip = esp = 0;
417caaee623SJann Horn 	permitted = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS | PTRACE_MODE_NOAUDIT);
4181da177e4SLinus Torvalds 	mm = get_task_mm(task);
4191da177e4SLinus Torvalds 	if (mm) {
4201da177e4SLinus Torvalds 		vsize = task_vsize(mm);
4210a1eb2d4SAndy Lutomirski 		/*
4220a1eb2d4SAndy Lutomirski 		 * esp and eip are intentionally zeroed out.  There is no
4230a1eb2d4SAndy Lutomirski 		 * non-racy way to read them without freezing the task.
4240a1eb2d4SAndy Lutomirski 		 * Programs that need reliable values can use ptrace(2).
425fd7d5627SJohn Ogness 		 *
426fd7d5627SJohn Ogness 		 * The only exception is if the task is core dumping because
427fd7d5627SJohn Ogness 		 * a program is not able to use ptrace(2) in that case. It is
428fd7d5627SJohn Ogness 		 * safe because the task has stopped executing permanently.
4290a1eb2d4SAndy Lutomirski 		 */
430fd7d5627SJohn Ogness 		if (permitted && (task->flags & PF_DUMPCORE)) {
4318bb2ee19SAlexey Dobriyan 			if (try_get_task_stack(task)) {
432fd7d5627SJohn Ogness 				eip = KSTK_EIP(task);
433fd7d5627SJohn Ogness 				esp = KSTK_ESP(task);
4348bb2ee19SAlexey Dobriyan 				put_task_stack(task);
4358bb2ee19SAlexey Dobriyan 			}
436fd7d5627SJohn Ogness 		}
437f83ce3e6SJake Edge 	}
4381da177e4SLinus Torvalds 
4391da177e4SLinus Torvalds 	sigemptyset(&sigign);
4401da177e4SLinus Torvalds 	sigemptyset(&sigcatch);
44164861634SMartin Schwidefsky 	cutime = cstime = utime = stime = 0;
44264861634SMartin Schwidefsky 	cgtime = gtime = 0;
4433cfd0885SAlan Cox 
444a593d6edSOleg Nesterov 	if (lock_task_sighand(task, &flags)) {
445a593d6edSOleg Nesterov 		struct signal_struct *sig = task->signal;
446a593d6edSOleg Nesterov 
44791593504SOleg Nesterov 		if (sig->tty) {
4485d0fdf1eSAlan Cox 			struct pid *pgrp = tty_get_pgrp(sig->tty);
4495d0fdf1eSAlan Cox 			tty_pgrp = pid_nr_ns(pgrp, ns);
4505d0fdf1eSAlan Cox 			put_pid(pgrp);
45191593504SOleg Nesterov 			tty_nr = new_encode_dev(tty_devnum(sig->tty));
452a593d6edSOleg Nesterov 		}
453a593d6edSOleg Nesterov 
4547e49827cSOleg Nesterov 		num_threads = get_nr_threads(task);
4551da177e4SLinus Torvalds 		collect_sigign_sigcatch(task, &sigign, &sigcatch);
4561da177e4SLinus Torvalds 
457a593d6edSOleg Nesterov 		cmin_flt = sig->cmin_flt;
458a593d6edSOleg Nesterov 		cmaj_flt = sig->cmaj_flt;
459a593d6edSOleg Nesterov 		cutime = sig->cutime;
460a593d6edSOleg Nesterov 		cstime = sig->cstime;
4619ac52315SLaurent Vivier 		cgtime = sig->cgtime;
4626aa7de05SMark Rutland 		rsslim = READ_ONCE(sig->rlim[RLIMIT_RSS].rlim_cur);
463a593d6edSOleg Nesterov 
4641da177e4SLinus Torvalds 		/* add up live thread stats at the group level */
4651da177e4SLinus Torvalds 		if (whole) {
466a593d6edSOleg Nesterov 			struct task_struct *t = task;
4671da177e4SLinus Torvalds 			do {
4681da177e4SLinus Torvalds 				min_flt += t->min_flt;
4691da177e4SLinus Torvalds 				maj_flt += t->maj_flt;
4706fac4829SFrederic Weisbecker 				gtime += task_gtime(t);
471185ee40eSOleg Nesterov 			} while_each_thread(task, t);
472a593d6edSOleg Nesterov 
473a593d6edSOleg Nesterov 			min_flt += sig->min_flt;
474a593d6edSOleg Nesterov 			maj_flt += sig->maj_flt;
475e80d0a1aSFrederic Weisbecker 			thread_group_cputime_adjusted(task, &utime, &stime);
47664861634SMartin Schwidefsky 			gtime += sig->gtime;
4771da177e4SLinus Torvalds 		}
4781da177e4SLinus Torvalds 
479b488893aSPavel Emelyanov 		sid = task_session_nr_ns(task, ns);
480a98fdcefSOleg Nesterov 		ppid = task_tgid_nr_ns(task->real_parent, ns);
481b488893aSPavel Emelyanov 		pgid = task_pgrp_nr_ns(task, ns);
482a593d6edSOleg Nesterov 
483a593d6edSOleg Nesterov 		unlock_task_sighand(task, &flags);
4841da177e4SLinus Torvalds 	}
4851da177e4SLinus Torvalds 
486f83ce3e6SJake Edge 	if (permitted && (!whole || num_threads < 2))
4871da177e4SLinus Torvalds 		wchan = get_wchan(task);
4881da177e4SLinus Torvalds 	if (!whole) {
4891da177e4SLinus Torvalds 		min_flt = task->min_flt;
4901da177e4SLinus Torvalds 		maj_flt = task->maj_flt;
491e80d0a1aSFrederic Weisbecker 		task_cputime_adjusted(task, &utime, &stime);
4926fac4829SFrederic Weisbecker 		gtime = task_gtime(task);
4931da177e4SLinus Torvalds 	}
4941da177e4SLinus Torvalds 
4951da177e4SLinus Torvalds 	/* scale priority and nice values from timeslices to -20..20 */
4961da177e4SLinus Torvalds 	/* to make it look like a "normal" Unix priority/nice value  */
4971da177e4SLinus Torvalds 	priority = task_prio(task);
4981da177e4SLinus Torvalds 	nice = task_nice(task);
4991da177e4SLinus Torvalds 
5001da177e4SLinus Torvalds 	/* convert nsec -> ticks */
50157e0be04SThomas Gleixner 	start_time = nsec_to_clock_t(task->real_start_time);
5021da177e4SLinus Torvalds 
503d0f02231SAndrei Vagin 	seq_put_decimal_ull(m, "", pid_nr_ns(pid, ns));
504d0f02231SAndrei Vagin 	seq_puts(m, " (");
50588b72b31STejun Heo 	proc_task_name(m, task, false);
506d0f02231SAndrei Vagin 	seq_puts(m, ") ");
507d0f02231SAndrei Vagin 	seq_putc(m, state);
50875ba1d07SJoe Perches 	seq_put_decimal_ll(m, " ", ppid);
50975ba1d07SJoe Perches 	seq_put_decimal_ll(m, " ", pgid);
51075ba1d07SJoe Perches 	seq_put_decimal_ll(m, " ", sid);
51175ba1d07SJoe Perches 	seq_put_decimal_ll(m, " ", tty_nr);
51275ba1d07SJoe Perches 	seq_put_decimal_ll(m, " ", tty_pgrp);
51375ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", task->flags);
51475ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", min_flt);
51575ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", cmin_flt);
51675ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", maj_flt);
51775ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", cmaj_flt);
5185613fda9SFrederic Weisbecker 	seq_put_decimal_ull(m, " ", nsec_to_clock_t(utime));
5195613fda9SFrederic Weisbecker 	seq_put_decimal_ull(m, " ", nsec_to_clock_t(stime));
5205613fda9SFrederic Weisbecker 	seq_put_decimal_ll(m, " ", nsec_to_clock_t(cutime));
5215613fda9SFrederic Weisbecker 	seq_put_decimal_ll(m, " ", nsec_to_clock_t(cstime));
52275ba1d07SJoe Perches 	seq_put_decimal_ll(m, " ", priority);
52375ba1d07SJoe Perches 	seq_put_decimal_ll(m, " ", nice);
52475ba1d07SJoe Perches 	seq_put_decimal_ll(m, " ", num_threads);
52575ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", 0);
52675ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", start_time);
52775ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", vsize);
52875ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", mm ? get_mm_rss(mm) : 0);
52975ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", rsslim);
53075ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", mm ? (permitted ? mm->start_code : 1) : 0);
53175ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", mm ? (permitted ? mm->end_code : 1) : 0);
53275ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", (permitted && mm) ? mm->start_stack : 0);
53375ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", esp);
53475ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", eip);
5351da177e4SLinus Torvalds 	/* The signal information here is obsolete.
5361da177e4SLinus Torvalds 	 * It must be decimal for Linux 2.0 compatibility.
5371da177e4SLinus Torvalds 	 * Use /proc/#/status for real-time signals.
5381da177e4SLinus Torvalds 	 */
53975ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", task->pending.signal.sig[0] & 0x7fffffffUL);
54075ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", task->blocked.sig[0] & 0x7fffffffUL);
54175ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", sigign.sig[0] & 0x7fffffffUL);
54275ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", sigcatch.sig[0] & 0x7fffffffUL);
543b2f73922SIngo Molnar 
544b2f73922SIngo Molnar 	/*
545b2f73922SIngo Molnar 	 * We used to output the absolute kernel address, but that's an
546b2f73922SIngo Molnar 	 * information leak - so instead we show a 0/1 flag here, to signal
547b2f73922SIngo Molnar 	 * to user-space whether there's a wchan field in /proc/PID/wchan.
548b2f73922SIngo Molnar 	 *
549b2f73922SIngo Molnar 	 * This works with older implementations of procps as well.
550b2f73922SIngo Molnar 	 */
551b2f73922SIngo Molnar 	if (wchan)
552b2f73922SIngo Molnar 		seq_puts(m, " 1");
553b2f73922SIngo Molnar 	else
554b2f73922SIngo Molnar 		seq_puts(m, " 0");
555b2f73922SIngo Molnar 
55675ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", 0);
55775ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", 0);
55875ba1d07SJoe Perches 	seq_put_decimal_ll(m, " ", task->exit_signal);
55975ba1d07SJoe Perches 	seq_put_decimal_ll(m, " ", task_cpu(task));
56075ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", task->rt_priority);
56175ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", task->policy);
56275ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", delayacct_blkio_ticks(task));
56316a6d9beSFrederic Weisbecker 	seq_put_decimal_ull(m, " ", nsec_to_clock_t(gtime));
56416a6d9beSFrederic Weisbecker 	seq_put_decimal_ll(m, " ", nsec_to_clock_t(cgtime));
5655b172087SCyrill Gorcunov 
5665b172087SCyrill Gorcunov 	if (mm && permitted) {
56775ba1d07SJoe Perches 		seq_put_decimal_ull(m, " ", mm->start_data);
56875ba1d07SJoe Perches 		seq_put_decimal_ull(m, " ", mm->end_data);
56975ba1d07SJoe Perches 		seq_put_decimal_ull(m, " ", mm->start_brk);
57075ba1d07SJoe Perches 		seq_put_decimal_ull(m, " ", mm->arg_start);
57175ba1d07SJoe Perches 		seq_put_decimal_ull(m, " ", mm->arg_end);
57275ba1d07SJoe Perches 		seq_put_decimal_ull(m, " ", mm->env_start);
57375ba1d07SJoe Perches 		seq_put_decimal_ull(m, " ", mm->env_end);
5745b172087SCyrill Gorcunov 	} else
57575ba1d07SJoe Perches 		seq_puts(m, " 0 0 0 0 0 0 0");
5765b172087SCyrill Gorcunov 
5775b172087SCyrill Gorcunov 	if (permitted)
57875ba1d07SJoe Perches 		seq_put_decimal_ll(m, " ", task->exit_code);
5795b172087SCyrill Gorcunov 	else
58075ba1d07SJoe Perches 		seq_puts(m, " 0");
5815b172087SCyrill Gorcunov 
582bda7bad6SKAMEZAWA Hiroyuki 	seq_putc(m, '\n');
5831da177e4SLinus Torvalds 	if (mm)
5841da177e4SLinus Torvalds 		mmput(mm);
585ee992744SEric W. Biederman 	return 0;
5861da177e4SLinus Torvalds }
5871da177e4SLinus Torvalds 
588ee992744SEric W. Biederman int proc_tid_stat(struct seq_file *m, struct pid_namespace *ns,
589ee992744SEric W. Biederman 			struct pid *pid, struct task_struct *task)
5901da177e4SLinus Torvalds {
591ee992744SEric W. Biederman 	return do_task_stat(m, ns, pid, task, 0);
5921da177e4SLinus Torvalds }
5931da177e4SLinus Torvalds 
594ee992744SEric W. Biederman int proc_tgid_stat(struct seq_file *m, struct pid_namespace *ns,
595ee992744SEric W. Biederman 			struct pid *pid, struct task_struct *task)
5961da177e4SLinus Torvalds {
597ee992744SEric W. Biederman 	return do_task_stat(m, ns, pid, task, 1);
5981da177e4SLinus Torvalds }
5991da177e4SLinus Torvalds 
600a56d3fc7SEric W. Biederman int proc_pid_statm(struct seq_file *m, struct pid_namespace *ns,
601a56d3fc7SEric W. Biederman 			struct pid *pid, struct task_struct *task)
6021da177e4SLinus Torvalds {
603a2ade7b6SAlexey Dobriyan 	unsigned long size = 0, resident = 0, shared = 0, text = 0, data = 0;
6041da177e4SLinus Torvalds 	struct mm_struct *mm = get_task_mm(task);
6051da177e4SLinus Torvalds 
6061da177e4SLinus Torvalds 	if (mm) {
6071da177e4SLinus Torvalds 		size = task_statm(mm, &shared, &text, &data, &resident);
6081da177e4SLinus Torvalds 		mmput(mm);
6091da177e4SLinus Torvalds 	}
610bda7bad6SKAMEZAWA Hiroyuki 	/*
611bda7bad6SKAMEZAWA Hiroyuki 	 * For quick read, open code by putting numbers directly
612bda7bad6SKAMEZAWA Hiroyuki 	 * expected format is
613bda7bad6SKAMEZAWA Hiroyuki 	 * seq_printf(m, "%lu %lu %lu %lu 0 %lu 0\n",
614bda7bad6SKAMEZAWA Hiroyuki 	 *               size, resident, shared, text, data);
615bda7bad6SKAMEZAWA Hiroyuki 	 */
61675ba1d07SJoe Perches 	seq_put_decimal_ull(m, "", size);
61775ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", resident);
61875ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", shared);
61975ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", text);
62075ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", 0);
62175ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", data);
62275ba1d07SJoe Perches 	seq_put_decimal_ull(m, " ", 0);
623bda7bad6SKAMEZAWA Hiroyuki 	seq_putc(m, '\n');
624a56d3fc7SEric W. Biederman 
625a56d3fc7SEric W. Biederman 	return 0;
6261da177e4SLinus Torvalds }
62781841161SCyrill Gorcunov 
6282e13ba54SIago López Galeiras #ifdef CONFIG_PROC_CHILDREN
62981841161SCyrill Gorcunov static struct pid *
63081841161SCyrill Gorcunov get_children_pid(struct inode *inode, struct pid *pid_prev, loff_t pos)
63181841161SCyrill Gorcunov {
63281841161SCyrill Gorcunov 	struct task_struct *start, *task;
63381841161SCyrill Gorcunov 	struct pid *pid = NULL;
63481841161SCyrill Gorcunov 
63581841161SCyrill Gorcunov 	read_lock(&tasklist_lock);
63681841161SCyrill Gorcunov 
63781841161SCyrill Gorcunov 	start = pid_task(proc_pid(inode), PIDTYPE_PID);
63881841161SCyrill Gorcunov 	if (!start)
63981841161SCyrill Gorcunov 		goto out;
64081841161SCyrill Gorcunov 
64181841161SCyrill Gorcunov 	/*
64281841161SCyrill Gorcunov 	 * Lets try to continue searching first, this gives
64381841161SCyrill Gorcunov 	 * us significant speedup on children-rich processes.
64481841161SCyrill Gorcunov 	 */
64581841161SCyrill Gorcunov 	if (pid_prev) {
64681841161SCyrill Gorcunov 		task = pid_task(pid_prev, PIDTYPE_PID);
64781841161SCyrill Gorcunov 		if (task && task->real_parent == start &&
64881841161SCyrill Gorcunov 		    !(list_empty(&task->sibling))) {
64981841161SCyrill Gorcunov 			if (list_is_last(&task->sibling, &start->children))
65081841161SCyrill Gorcunov 				goto out;
65181841161SCyrill Gorcunov 			task = list_first_entry(&task->sibling,
65281841161SCyrill Gorcunov 						struct task_struct, sibling);
65381841161SCyrill Gorcunov 			pid = get_pid(task_pid(task));
65481841161SCyrill Gorcunov 			goto out;
65581841161SCyrill Gorcunov 		}
65681841161SCyrill Gorcunov 	}
65781841161SCyrill Gorcunov 
65881841161SCyrill Gorcunov 	/*
65981841161SCyrill Gorcunov 	 * Slow search case.
66081841161SCyrill Gorcunov 	 *
66181841161SCyrill Gorcunov 	 * We might miss some children here if children
66281841161SCyrill Gorcunov 	 * are exited while we were not holding the lock,
66381841161SCyrill Gorcunov 	 * but it was never promised to be accurate that
66481841161SCyrill Gorcunov 	 * much.
66581841161SCyrill Gorcunov 	 *
66681841161SCyrill Gorcunov 	 * "Just suppose that the parent sleeps, but N children
66781841161SCyrill Gorcunov 	 *  exit after we printed their tids. Now the slow paths
66881841161SCyrill Gorcunov 	 *  skips N extra children, we miss N tasks." (c)
66981841161SCyrill Gorcunov 	 *
67081841161SCyrill Gorcunov 	 * So one need to stop or freeze the leader and all
67181841161SCyrill Gorcunov 	 * its children to get a precise result.
67281841161SCyrill Gorcunov 	 */
67381841161SCyrill Gorcunov 	list_for_each_entry(task, &start->children, sibling) {
67481841161SCyrill Gorcunov 		if (pos-- == 0) {
67581841161SCyrill Gorcunov 			pid = get_pid(task_pid(task));
67681841161SCyrill Gorcunov 			break;
67781841161SCyrill Gorcunov 		}
67881841161SCyrill Gorcunov 	}
67981841161SCyrill Gorcunov 
68081841161SCyrill Gorcunov out:
68181841161SCyrill Gorcunov 	read_unlock(&tasklist_lock);
68281841161SCyrill Gorcunov 	return pid;
68381841161SCyrill Gorcunov }
68481841161SCyrill Gorcunov 
68581841161SCyrill Gorcunov static int children_seq_show(struct seq_file *seq, void *v)
68681841161SCyrill Gorcunov {
68781841161SCyrill Gorcunov 	struct inode *inode = seq->private;
68881841161SCyrill Gorcunov 	pid_t pid;
68981841161SCyrill Gorcunov 
69081841161SCyrill Gorcunov 	pid = pid_nr_ns(v, inode->i_sb->s_fs_info);
69125ce3191SJoe Perches 	seq_printf(seq, "%d ", pid);
69225ce3191SJoe Perches 
69325ce3191SJoe Perches 	return 0;
69481841161SCyrill Gorcunov }
69581841161SCyrill Gorcunov 
69681841161SCyrill Gorcunov static void *children_seq_start(struct seq_file *seq, loff_t *pos)
69781841161SCyrill Gorcunov {
69881841161SCyrill Gorcunov 	return get_children_pid(seq->private, NULL, *pos);
69981841161SCyrill Gorcunov }
70081841161SCyrill Gorcunov 
70181841161SCyrill Gorcunov static void *children_seq_next(struct seq_file *seq, void *v, loff_t *pos)
70281841161SCyrill Gorcunov {
70381841161SCyrill Gorcunov 	struct pid *pid;
70481841161SCyrill Gorcunov 
70581841161SCyrill Gorcunov 	pid = get_children_pid(seq->private, v, *pos + 1);
70681841161SCyrill Gorcunov 	put_pid(v);
70781841161SCyrill Gorcunov 
70881841161SCyrill Gorcunov 	++*pos;
70981841161SCyrill Gorcunov 	return pid;
71081841161SCyrill Gorcunov }
71181841161SCyrill Gorcunov 
71281841161SCyrill Gorcunov static void children_seq_stop(struct seq_file *seq, void *v)
71381841161SCyrill Gorcunov {
71481841161SCyrill Gorcunov 	put_pid(v);
71581841161SCyrill Gorcunov }
71681841161SCyrill Gorcunov 
71781841161SCyrill Gorcunov static const struct seq_operations children_seq_ops = {
71881841161SCyrill Gorcunov 	.start	= children_seq_start,
71981841161SCyrill Gorcunov 	.next	= children_seq_next,
72081841161SCyrill Gorcunov 	.stop	= children_seq_stop,
72181841161SCyrill Gorcunov 	.show	= children_seq_show,
72281841161SCyrill Gorcunov };
72381841161SCyrill Gorcunov 
72481841161SCyrill Gorcunov static int children_seq_open(struct inode *inode, struct file *file)
72581841161SCyrill Gorcunov {
72681841161SCyrill Gorcunov 	struct seq_file *m;
72781841161SCyrill Gorcunov 	int ret;
72881841161SCyrill Gorcunov 
72981841161SCyrill Gorcunov 	ret = seq_open(file, &children_seq_ops);
73081841161SCyrill Gorcunov 	if (ret)
73181841161SCyrill Gorcunov 		return ret;
73281841161SCyrill Gorcunov 
73381841161SCyrill Gorcunov 	m = file->private_data;
73481841161SCyrill Gorcunov 	m->private = inode;
73581841161SCyrill Gorcunov 
73681841161SCyrill Gorcunov 	return ret;
73781841161SCyrill Gorcunov }
73881841161SCyrill Gorcunov 
73981841161SCyrill Gorcunov const struct file_operations proc_tid_children_operations = {
74081841161SCyrill Gorcunov 	.open    = children_seq_open,
74181841161SCyrill Gorcunov 	.read    = seq_read,
74281841161SCyrill Gorcunov 	.llseek  = seq_lseek,
743171ef917SAlexey Dobriyan 	.release = seq_release,
74481841161SCyrill Gorcunov };
7452e13ba54SIago López Galeiras #endif /* CONFIG_PROC_CHILDREN */
746