xref: /openbmc/linux/fs/proc/array.c (revision 289f8e27)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/fs/proc/array.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 1992  by Linus Torvalds
51da177e4SLinus Torvalds  *  based on ideas by Darren Senn
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * Fixes:
81da177e4SLinus Torvalds  * Michael. K. Johnson: stat,statm extensions.
91da177e4SLinus Torvalds  *                      <johnsonm@stolaf.edu>
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  * Pauline Middelink :  Made cmdline,envline only break at '\0's, to
121da177e4SLinus Torvalds  *                      make sure SET_PROCTITLE works. Also removed
131da177e4SLinus Torvalds  *                      bad '!' which forced address recalculation for
141da177e4SLinus Torvalds  *                      EVERY character on the current page.
151da177e4SLinus Torvalds  *                      <middelin@polyware.iaf.nl>
161da177e4SLinus Torvalds  *
171da177e4SLinus Torvalds  * Danny ter Haar    :	added cpuinfo
181da177e4SLinus Torvalds  *			<dth@cistron.nl>
191da177e4SLinus Torvalds  *
201da177e4SLinus Torvalds  * Alessandro Rubini :  profile extension.
211da177e4SLinus Torvalds  *                      <rubini@ipvvis.unipv.it>
221da177e4SLinus Torvalds  *
231da177e4SLinus Torvalds  * Jeff Tranter      :  added BogoMips field to cpuinfo
241da177e4SLinus Torvalds  *                      <Jeff_Tranter@Mitel.COM>
251da177e4SLinus Torvalds  *
261da177e4SLinus Torvalds  * Bruno Haible      :  remove 4K limit for the maps file
271da177e4SLinus Torvalds  *			<haible@ma2s2.mathematik.uni-karlsruhe.de>
281da177e4SLinus Torvalds  *
291da177e4SLinus Torvalds  * Yves Arrouye      :  remove removal of trailing spaces in get_array.
301da177e4SLinus Torvalds  *			<Yves.Arrouye@marin.fdn.fr>
311da177e4SLinus Torvalds  *
321da177e4SLinus Torvalds  * Jerome Forissier  :  added per-CPU time information to /proc/stat
331da177e4SLinus Torvalds  *                      and /proc/<pid>/cpu extension
341da177e4SLinus Torvalds  *                      <forissier@isia.cma.fr>
351da177e4SLinus Torvalds  *			- Incorporation and non-SMP safe operation
361da177e4SLinus Torvalds  *			of forissier patch in 2.1.78 by
371da177e4SLinus Torvalds  *			Hans Marcus <crowbar@concepts.nl>
381da177e4SLinus Torvalds  *
391da177e4SLinus Torvalds  * aeb@cwi.nl        :  /proc/partitions
401da177e4SLinus Torvalds  *
411da177e4SLinus Torvalds  *
421da177e4SLinus Torvalds  * Alan Cox	     :  security fixes.
431da177e4SLinus Torvalds  *			<Alan.Cox@linux.org>
441da177e4SLinus Torvalds  *
451da177e4SLinus Torvalds  * Al Viro           :  safe handling of mm_struct
461da177e4SLinus Torvalds  *
471da177e4SLinus Torvalds  * Gerhard Wichert   :  added BIGMEM support
481da177e4SLinus Torvalds  * Siemens AG           <Gerhard.Wichert@pdb.siemens.de>
491da177e4SLinus Torvalds  *
501da177e4SLinus Torvalds  * Al Viro & Jeff Garzik :  moved most of the thing into base.c and
511da177e4SLinus Torvalds  *			 :  proc_misc.c. The rest may eventually go into
521da177e4SLinus Torvalds  *			 :  base.c too.
531da177e4SLinus Torvalds  */
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds #include <linux/types.h>
561da177e4SLinus Torvalds #include <linux/errno.h>
571da177e4SLinus Torvalds #include <linux/time.h>
581da177e4SLinus Torvalds #include <linux/kernel.h>
591da177e4SLinus Torvalds #include <linux/kernel_stat.h>
601da177e4SLinus Torvalds #include <linux/tty.h>
611da177e4SLinus Torvalds #include <linux/string.h>
621da177e4SLinus Torvalds #include <linux/mman.h>
631da177e4SLinus Torvalds #include <linux/proc_fs.h>
641da177e4SLinus Torvalds #include <linux/ioport.h>
658ea02606SIngo Molnar #include <linux/uaccess.h>
668ea02606SIngo Molnar #include <linux/io.h>
671da177e4SLinus Torvalds #include <linux/mm.h>
681da177e4SLinus Torvalds #include <linux/hugetlb.h>
691da177e4SLinus Torvalds #include <linux/pagemap.h>
701da177e4SLinus Torvalds #include <linux/swap.h>
711da177e4SLinus Torvalds #include <linux/slab.h>
721da177e4SLinus Torvalds #include <linux/smp.h>
731da177e4SLinus Torvalds #include <linux/signal.h>
741da177e4SLinus Torvalds #include <linux/highmem.h>
751da177e4SLinus Torvalds #include <linux/file.h>
769f3acc31SAl Viro #include <linux/fdtable.h>
771da177e4SLinus Torvalds #include <linux/times.h>
781da177e4SLinus Torvalds #include <linux/cpuset.h>
794fb3a538SDipankar Sarma #include <linux/rcupdate.h>
8025890454SShailabh Nagar #include <linux/delayacct.h>
81ee992744SEric W. Biederman #include <linux/seq_file.h>
82b488893aSPavel Emelyanov #include <linux/pid_namespace.h>
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds #include <asm/pgtable.h>
851da177e4SLinus Torvalds #include <asm/processor.h>
861da177e4SLinus Torvalds #include "internal.h"
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds /* Gcc optimizes away "strlen(x)" for constant x */
891da177e4SLinus Torvalds #define ADDBUF(buffer, string) \
901da177e4SLinus Torvalds do { memcpy(buffer, string, strlen(string)); \
911da177e4SLinus Torvalds      buffer += strlen(string); } while (0)
921da177e4SLinus Torvalds 
93df5f8314SEric W. Biederman static inline void task_name(struct seq_file *m, struct task_struct *p)
941da177e4SLinus Torvalds {
951da177e4SLinus Torvalds 	int i;
96df5f8314SEric W. Biederman 	char *buf, *end;
971da177e4SLinus Torvalds 	char *name;
981da177e4SLinus Torvalds 	char tcomm[sizeof(p->comm)];
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds 	get_task_comm(tcomm, p);
1011da177e4SLinus Torvalds 
102df5f8314SEric W. Biederman 	seq_printf(m, "Name:\t");
103df5f8314SEric W. Biederman 	end = m->buf + m->size;
104df5f8314SEric W. Biederman 	buf = m->buf + m->count;
1051da177e4SLinus Torvalds 	name = tcomm;
1061da177e4SLinus Torvalds 	i = sizeof(tcomm);
107df5f8314SEric W. Biederman 	while (i && (buf < end)) {
1081da177e4SLinus Torvalds 		unsigned char c = *name;
1091da177e4SLinus Torvalds 		name++;
1101da177e4SLinus Torvalds 		i--;
1111da177e4SLinus Torvalds 		*buf = c;
1121da177e4SLinus Torvalds 		if (!c)
1131da177e4SLinus Torvalds 			break;
1141da177e4SLinus Torvalds 		if (c == '\\') {
115df5f8314SEric W. Biederman 			buf++;
116df5f8314SEric W. Biederman 			if (buf < end)
117df5f8314SEric W. Biederman 				*buf++ = c;
1181da177e4SLinus Torvalds 			continue;
1191da177e4SLinus Torvalds 		}
1201da177e4SLinus Torvalds 		if (c == '\n') {
121df5f8314SEric W. Biederman 			*buf++ = '\\';
122df5f8314SEric W. Biederman 			if (buf < end)
123df5f8314SEric W. Biederman 				*buf++ = 'n';
1241da177e4SLinus Torvalds 			continue;
1251da177e4SLinus Torvalds 		}
1261da177e4SLinus Torvalds 		buf++;
127df5f8314SEric W. Biederman 	}
128df5f8314SEric W. Biederman 	m->count = buf - m->buf;
129df5f8314SEric W. Biederman 	seq_printf(m, "\n");
1301da177e4SLinus Torvalds }
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds /*
1331da177e4SLinus Torvalds  * The task state array is a strange "bitmap" of
1341da177e4SLinus Torvalds  * reasons to sleep. Thus "running" is zero, and
1351da177e4SLinus Torvalds  * you can test for combinations of others with
1361da177e4SLinus Torvalds  * simple bit tests.
1371da177e4SLinus Torvalds  */
1381da177e4SLinus Torvalds static const char *task_state_array[] = {
1391da177e4SLinus Torvalds 	"R (running)",		/*  0 */
1401da177e4SLinus Torvalds 	"S (sleeping)",		/*  1 */
1411da177e4SLinus Torvalds 	"D (disk sleep)",	/*  2 */
1421da177e4SLinus Torvalds 	"T (stopped)",		/*  4 */
1431da177e4SLinus Torvalds 	"T (tracing stop)",	/*  8 */
1441da177e4SLinus Torvalds 	"Z (zombie)",		/* 16 */
1451da177e4SLinus Torvalds 	"X (dead)"		/* 32 */
1461da177e4SLinus Torvalds };
1471da177e4SLinus Torvalds 
1481da177e4SLinus Torvalds static inline const char *get_task_state(struct task_struct *tsk)
1491da177e4SLinus Torvalds {
1501587e2b1SMatthew Wilcox 	unsigned int state = (tsk->state & TASK_REPORT) | tsk->exit_state;
1511da177e4SLinus Torvalds 	const char **p = &task_state_array[0];
1521da177e4SLinus Torvalds 
1531da177e4SLinus Torvalds 	while (state) {
1541da177e4SLinus Torvalds 		p++;
1551da177e4SLinus Torvalds 		state >>= 1;
1561da177e4SLinus Torvalds 	}
1571da177e4SLinus Torvalds 	return *p;
1581da177e4SLinus Torvalds }
1591da177e4SLinus Torvalds 
160df5f8314SEric W. Biederman static inline void task_state(struct seq_file *m, struct pid_namespace *ns,
161df5f8314SEric W. Biederman 				struct pid *pid, struct task_struct *p)
1621da177e4SLinus Torvalds {
1631da177e4SLinus Torvalds 	struct group_info *group_info;
1641da177e4SLinus Torvalds 	int g;
165badf1662SDipankar Sarma 	struct fdtable *fdt = NULL;
166b488893aSPavel Emelyanov 	pid_t ppid, tpid;
1671da177e4SLinus Torvalds 
168b0fa9db6SOleg Nesterov 	rcu_read_lock();
169b488893aSPavel Emelyanov 	ppid = pid_alive(p) ?
170b488893aSPavel Emelyanov 		task_tgid_nr_ns(rcu_dereference(p->real_parent), ns) : 0;
171b488893aSPavel Emelyanov 	tpid = pid_alive(p) && p->ptrace ?
17284427eaeSRoland McGrath 		task_pid_nr_ns(rcu_dereference(p->parent), ns) : 0;
173df5f8314SEric W. Biederman 	seq_printf(m,
1741da177e4SLinus Torvalds 		"State:\t%s\n"
1751da177e4SLinus Torvalds 		"Tgid:\t%d\n"
1761da177e4SLinus Torvalds 		"Pid:\t%d\n"
1771da177e4SLinus Torvalds 		"PPid:\t%d\n"
1781da177e4SLinus Torvalds 		"TracerPid:\t%d\n"
1791da177e4SLinus Torvalds 		"Uid:\t%d\t%d\t%d\t%d\n"
1801da177e4SLinus Torvalds 		"Gid:\t%d\t%d\t%d\t%d\n",
1811da177e4SLinus Torvalds 		get_task_state(p),
182b488893aSPavel Emelyanov 		task_tgid_nr_ns(p, ns),
183df5f8314SEric W. Biederman 		pid_nr_ns(pid, ns),
184b488893aSPavel Emelyanov 		ppid, tpid,
1851da177e4SLinus Torvalds 		p->uid, p->euid, p->suid, p->fsuid,
1861da177e4SLinus Torvalds 		p->gid, p->egid, p->sgid, p->fsgid);
187b0fa9db6SOleg Nesterov 
1881da177e4SLinus Torvalds 	task_lock(p);
189badf1662SDipankar Sarma 	if (p->files)
190badf1662SDipankar Sarma 		fdt = files_fdtable(p->files);
191df5f8314SEric W. Biederman 	seq_printf(m,
1921da177e4SLinus Torvalds 		"FDSize:\t%d\n"
1931da177e4SLinus Torvalds 		"Groups:\t",
194badf1662SDipankar Sarma 		fdt ? fdt->max_fds : 0);
1954fb3a538SDipankar Sarma 	rcu_read_unlock();
1961da177e4SLinus Torvalds 
1971da177e4SLinus Torvalds 	group_info = p->group_info;
1981da177e4SLinus Torvalds 	get_group_info(group_info);
1991da177e4SLinus Torvalds 	task_unlock(p);
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds 	for (g = 0; g < min(group_info->ngroups, NGROUPS_SMALL); g++)
202df5f8314SEric W. Biederman 		seq_printf(m, "%d ", GROUP_AT(group_info, g));
2031da177e4SLinus Torvalds 	put_group_info(group_info);
2041da177e4SLinus Torvalds 
205df5f8314SEric W. Biederman 	seq_printf(m, "\n");
2061da177e4SLinus Torvalds }
2071da177e4SLinus Torvalds 
208df5f8314SEric W. Biederman static void render_sigset_t(struct seq_file *m, const char *header,
209df5f8314SEric W. Biederman 				sigset_t *set)
2101da177e4SLinus Torvalds {
211df5f8314SEric W. Biederman 	int i;
2121da177e4SLinus Torvalds 
213df5f8314SEric W. Biederman 	seq_printf(m, "%s", header);
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds 	i = _NSIG;
2161da177e4SLinus Torvalds 	do {
2171da177e4SLinus Torvalds 		int x = 0;
2181da177e4SLinus Torvalds 
2191da177e4SLinus Torvalds 		i -= 4;
2201da177e4SLinus Torvalds 		if (sigismember(set, i+1)) x |= 1;
2211da177e4SLinus Torvalds 		if (sigismember(set, i+2)) x |= 2;
2221da177e4SLinus Torvalds 		if (sigismember(set, i+3)) x |= 4;
2231da177e4SLinus Torvalds 		if (sigismember(set, i+4)) x |= 8;
224df5f8314SEric W. Biederman 		seq_printf(m, "%x", x);
2251da177e4SLinus Torvalds 	} while (i >= 4);
2261da177e4SLinus Torvalds 
227df5f8314SEric W. Biederman 	seq_printf(m, "\n");
2281da177e4SLinus Torvalds }
2291da177e4SLinus Torvalds 
2301da177e4SLinus Torvalds static void collect_sigign_sigcatch(struct task_struct *p, sigset_t *ign,
2311da177e4SLinus Torvalds 				    sigset_t *catch)
2321da177e4SLinus Torvalds {
2331da177e4SLinus Torvalds 	struct k_sigaction *k;
2341da177e4SLinus Torvalds 	int i;
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds 	k = p->sighand->action;
2371da177e4SLinus Torvalds 	for (i = 1; i <= _NSIG; ++i, ++k) {
2381da177e4SLinus Torvalds 		if (k->sa.sa_handler == SIG_IGN)
2391da177e4SLinus Torvalds 			sigaddset(ign, i);
2401da177e4SLinus Torvalds 		else if (k->sa.sa_handler != SIG_DFL)
2411da177e4SLinus Torvalds 			sigaddset(catch, i);
2421da177e4SLinus Torvalds 	}
2431da177e4SLinus Torvalds }
2441da177e4SLinus Torvalds 
245df5f8314SEric W. Biederman static inline void task_sig(struct seq_file *m, struct task_struct *p)
2461da177e4SLinus Torvalds {
2475e6b3f42SOleg Nesterov 	unsigned long flags;
2481da177e4SLinus Torvalds 	sigset_t pending, shpending, blocked, ignored, caught;
2491da177e4SLinus Torvalds 	int num_threads = 0;
2501da177e4SLinus Torvalds 	unsigned long qsize = 0;
2511da177e4SLinus Torvalds 	unsigned long qlim = 0;
2521da177e4SLinus Torvalds 
2531da177e4SLinus Torvalds 	sigemptyset(&pending);
2541da177e4SLinus Torvalds 	sigemptyset(&shpending);
2551da177e4SLinus Torvalds 	sigemptyset(&blocked);
2561da177e4SLinus Torvalds 	sigemptyset(&ignored);
2571da177e4SLinus Torvalds 	sigemptyset(&caught);
2581da177e4SLinus Torvalds 
2595e6b3f42SOleg Nesterov 	rcu_read_lock();
2605e6b3f42SOleg Nesterov 	if (lock_task_sighand(p, &flags)) {
2611da177e4SLinus Torvalds 		pending = p->pending.signal;
2621da177e4SLinus Torvalds 		shpending = p->signal->shared_pending.signal;
2631da177e4SLinus Torvalds 		blocked = p->blocked;
2641da177e4SLinus Torvalds 		collect_sigign_sigcatch(p, &ignored, &caught);
2651da177e4SLinus Torvalds 		num_threads = atomic_read(&p->signal->count);
2661da177e4SLinus Torvalds 		qsize = atomic_read(&p->user->sigpending);
2671da177e4SLinus Torvalds 		qlim = p->signal->rlim[RLIMIT_SIGPENDING].rlim_cur;
2685e6b3f42SOleg Nesterov 		unlock_task_sighand(p, &flags);
2691da177e4SLinus Torvalds 	}
2705e6b3f42SOleg Nesterov 	rcu_read_unlock();
2711da177e4SLinus Torvalds 
272df5f8314SEric W. Biederman 	seq_printf(m, "Threads:\t%d\n", num_threads);
273df5f8314SEric W. Biederman 	seq_printf(m, "SigQ:\t%lu/%lu\n", qsize, qlim);
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds 	/* render them all */
276df5f8314SEric W. Biederman 	render_sigset_t(m, "SigPnd:\t", &pending);
277df5f8314SEric W. Biederman 	render_sigset_t(m, "ShdPnd:\t", &shpending);
278df5f8314SEric W. Biederman 	render_sigset_t(m, "SigBlk:\t", &blocked);
279df5f8314SEric W. Biederman 	render_sigset_t(m, "SigIgn:\t", &ignored);
280df5f8314SEric W. Biederman 	render_sigset_t(m, "SigCgt:\t", &caught);
2811da177e4SLinus Torvalds }
2821da177e4SLinus Torvalds 
283df5f8314SEric W. Biederman static void render_cap_t(struct seq_file *m, const char *header,
284df5f8314SEric W. Biederman 			kernel_cap_t *a)
285e338d263SAndrew Morgan {
286e338d263SAndrew Morgan 	unsigned __capi;
287e338d263SAndrew Morgan 
288df5f8314SEric W. Biederman 	seq_printf(m, "%s", header);
289e338d263SAndrew Morgan 	CAP_FOR_EACH_U32(__capi) {
290df5f8314SEric W. Biederman 		seq_printf(m, "%08x",
291e338d263SAndrew Morgan 			   a->cap[(_LINUX_CAPABILITY_U32S-1) - __capi]);
292e338d263SAndrew Morgan 	}
293df5f8314SEric W. Biederman 	seq_printf(m, "\n");
294e338d263SAndrew Morgan }
295e338d263SAndrew Morgan 
296df5f8314SEric W. Biederman static inline void task_cap(struct seq_file *m, struct task_struct *p)
2971da177e4SLinus Torvalds {
298df5f8314SEric W. Biederman 	render_cap_t(m, "CapInh:\t", &p->cap_inheritable);
299df5f8314SEric W. Biederman 	render_cap_t(m, "CapPrm:\t", &p->cap_permitted);
300df5f8314SEric W. Biederman 	render_cap_t(m, "CapEff:\t", &p->cap_effective);
301289f8e27SSerge E. Hallyn 	render_cap_t(m, "CapBnd:\t", &p->cap_bset);
3021da177e4SLinus Torvalds }
3031da177e4SLinus Torvalds 
304df5f8314SEric W. Biederman static inline void task_context_switch_counts(struct seq_file *m,
305df5f8314SEric W. Biederman 						struct task_struct *p)
306b663a79cSMaxim Uvarov {
307df5f8314SEric W. Biederman 	seq_printf(m,	"voluntary_ctxt_switches:\t%lu\n"
308b663a79cSMaxim Uvarov 			"nonvoluntary_ctxt_switches:\t%lu\n",
309b663a79cSMaxim Uvarov 			p->nvcsw,
310b663a79cSMaxim Uvarov 			p->nivcsw);
311b663a79cSMaxim Uvarov }
312b663a79cSMaxim Uvarov 
313df5f8314SEric W. Biederman int proc_pid_status(struct seq_file *m, struct pid_namespace *ns,
314df5f8314SEric W. Biederman 			struct pid *pid, struct task_struct *task)
3151da177e4SLinus Torvalds {
3161da177e4SLinus Torvalds 	struct mm_struct *mm = get_task_mm(task);
3171da177e4SLinus Torvalds 
318df5f8314SEric W. Biederman 	task_name(m, task);
319df5f8314SEric W. Biederman 	task_state(m, ns, pid, task);
3201da177e4SLinus Torvalds 
3211da177e4SLinus Torvalds 	if (mm) {
322df5f8314SEric W. Biederman 		task_mem(m, mm);
3231da177e4SLinus Torvalds 		mmput(mm);
3241da177e4SLinus Torvalds 	}
325df5f8314SEric W. Biederman 	task_sig(m, task);
326df5f8314SEric W. Biederman 	task_cap(m, task);
327df5f8314SEric W. Biederman 	cpuset_task_status_allowed(m, task);
328347a8dc3SMartin Schwidefsky #if defined(CONFIG_S390)
329df5f8314SEric W. Biederman 	task_show_regs(m, task);
3301da177e4SLinus Torvalds #endif
331df5f8314SEric W. Biederman 	task_context_switch_counts(m, task);
332df5f8314SEric W. Biederman 	return 0;
3331da177e4SLinus Torvalds }
3341da177e4SLinus Torvalds 
335efe567fcSChristian Borntraeger /*
336efe567fcSChristian Borntraeger  * Use precise platform statistics if available:
337efe567fcSChristian Borntraeger  */
338efe567fcSChristian Borntraeger #ifdef CONFIG_VIRT_CPU_ACCOUNTING
339efe567fcSChristian Borntraeger static cputime_t task_utime(struct task_struct *p)
340efe567fcSChristian Borntraeger {
341efe567fcSChristian Borntraeger 	return p->utime;
342efe567fcSChristian Borntraeger }
343efe567fcSChristian Borntraeger 
344efe567fcSChristian Borntraeger static cputime_t task_stime(struct task_struct *p)
345efe567fcSChristian Borntraeger {
346efe567fcSChristian Borntraeger 	return p->stime;
347efe567fcSChristian Borntraeger }
348efe567fcSChristian Borntraeger #else
349efe567fcSChristian Borntraeger static cputime_t task_utime(struct task_struct *p)
350b27f03d4SIngo Molnar {
351b27f03d4SIngo Molnar 	clock_t utime = cputime_to_clock_t(p->utime),
352b27f03d4SIngo Molnar 		total = utime + cputime_to_clock_t(p->stime);
353b27f03d4SIngo Molnar 	u64 temp;
354b27f03d4SIngo Molnar 
355b27f03d4SIngo Molnar 	/*
356b27f03d4SIngo Molnar 	 * Use CFS's precise accounting:
357b27f03d4SIngo Molnar 	 */
358b27f03d4SIngo Molnar 	temp = (u64)nsec_to_clock_t(p->se.sum_exec_runtime);
359b27f03d4SIngo Molnar 
360b27f03d4SIngo Molnar 	if (total) {
361b27f03d4SIngo Molnar 		temp *= utime;
362b27f03d4SIngo Molnar 		do_div(temp, total);
363b27f03d4SIngo Molnar 	}
364b27f03d4SIngo Molnar 	utime = (clock_t)temp;
365b27f03d4SIngo Molnar 
36673a2bcb0SPeter Zijlstra 	p->prev_utime = max(p->prev_utime, clock_t_to_cputime(utime));
36773a2bcb0SPeter Zijlstra 	return p->prev_utime;
368b27f03d4SIngo Molnar }
369b27f03d4SIngo Molnar 
370efe567fcSChristian Borntraeger static cputime_t task_stime(struct task_struct *p)
371b27f03d4SIngo Molnar {
3725926c50bSIngo Molnar 	clock_t stime;
373b27f03d4SIngo Molnar 
374b27f03d4SIngo Molnar 	/*
375b27f03d4SIngo Molnar 	 * Use CFS's precise accounting. (we subtract utime from
376b27f03d4SIngo Molnar 	 * the total, to make sure the total observed by userspace
377b27f03d4SIngo Molnar 	 * grows monotonically - apps rely on that):
378b27f03d4SIngo Molnar 	 */
379efe567fcSChristian Borntraeger 	stime = nsec_to_clock_t(p->se.sum_exec_runtime) -
380efe567fcSChristian Borntraeger 			cputime_to_clock_t(task_utime(p));
381b27f03d4SIngo Molnar 
38208e4570aSIngo Molnar 	if (stime >= 0)
3839301899bSBalbir Singh 		p->prev_stime = max(p->prev_stime, clock_t_to_cputime(stime));
38408e4570aSIngo Molnar 
3859301899bSBalbir Singh 	return p->prev_stime;
386b27f03d4SIngo Molnar }
387efe567fcSChristian Borntraeger #endif
388b27f03d4SIngo Molnar 
3899ac52315SLaurent Vivier static cputime_t task_gtime(struct task_struct *p)
3909ac52315SLaurent Vivier {
3919ac52315SLaurent Vivier 	return p->gtime;
3929ac52315SLaurent Vivier }
3939ac52315SLaurent Vivier 
394ee992744SEric W. Biederman static int do_task_stat(struct seq_file *m, struct pid_namespace *ns,
395ee992744SEric W. Biederman 			struct pid *pid, struct task_struct *task, int whole)
3961da177e4SLinus Torvalds {
3971da177e4SLinus Torvalds 	unsigned long vsize, eip, esp, wchan = ~0UL;
3981da177e4SLinus Torvalds 	long priority, nice;
3991da177e4SLinus Torvalds 	int tty_pgrp = -1, tty_nr = 0;
4001da177e4SLinus Torvalds 	sigset_t sigign, sigcatch;
4011da177e4SLinus Torvalds 	char state;
402a593d6edSOleg Nesterov 	pid_t ppid = 0, pgid = -1, sid = -1;
4031da177e4SLinus Torvalds 	int num_threads = 0;
4041da177e4SLinus Torvalds 	struct mm_struct *mm;
4051da177e4SLinus Torvalds 	unsigned long long start_time;
4061da177e4SLinus Torvalds 	unsigned long cmin_flt = 0, cmaj_flt = 0;
4071da177e4SLinus Torvalds 	unsigned long  min_flt = 0,  maj_flt = 0;
408efe567fcSChristian Borntraeger 	cputime_t cutime, cstime, utime, stime;
4099ac52315SLaurent Vivier 	cputime_t cgtime, gtime;
4101da177e4SLinus Torvalds 	unsigned long rsslim = 0;
4111da177e4SLinus Torvalds 	char tcomm[sizeof(task->comm)];
412a593d6edSOleg Nesterov 	unsigned long flags;
4131da177e4SLinus Torvalds 
4141da177e4SLinus Torvalds 	state = *get_task_state(task);
4151da177e4SLinus Torvalds 	vsize = eip = esp = 0;
4161da177e4SLinus Torvalds 	mm = get_task_mm(task);
4171da177e4SLinus Torvalds 	if (mm) {
4181da177e4SLinus Torvalds 		vsize = task_vsize(mm);
4191da177e4SLinus Torvalds 		eip = KSTK_EIP(task);
4201da177e4SLinus Torvalds 		esp = KSTK_ESP(task);
4211da177e4SLinus Torvalds 	}
4221da177e4SLinus Torvalds 
4231da177e4SLinus Torvalds 	get_task_comm(tcomm, task);
4241da177e4SLinus Torvalds 
4251da177e4SLinus Torvalds 	sigemptyset(&sigign);
4261da177e4SLinus Torvalds 	sigemptyset(&sigcatch);
427efe567fcSChristian Borntraeger 	cutime = cstime = utime = stime = cputime_zero;
4289ac52315SLaurent Vivier 	cgtime = gtime = cputime_zero;
4293cfd0885SAlan Cox 
430a593d6edSOleg Nesterov 	if (lock_task_sighand(task, &flags)) {
431a593d6edSOleg Nesterov 		struct signal_struct *sig = task->signal;
432a593d6edSOleg Nesterov 
43391593504SOleg Nesterov 		if (sig->tty) {
4345d0fdf1eSAlan Cox 			struct pid *pgrp = tty_get_pgrp(sig->tty);
4355d0fdf1eSAlan Cox 			tty_pgrp = pid_nr_ns(pgrp, ns);
4365d0fdf1eSAlan Cox 			put_pid(pgrp);
43791593504SOleg Nesterov 			tty_nr = new_encode_dev(tty_devnum(sig->tty));
438a593d6edSOleg Nesterov 		}
439a593d6edSOleg Nesterov 
440a593d6edSOleg Nesterov 		num_threads = atomic_read(&sig->count);
4411da177e4SLinus Torvalds 		collect_sigign_sigcatch(task, &sigign, &sigcatch);
4421da177e4SLinus Torvalds 
443a593d6edSOleg Nesterov 		cmin_flt = sig->cmin_flt;
444a593d6edSOleg Nesterov 		cmaj_flt = sig->cmaj_flt;
445a593d6edSOleg Nesterov 		cutime = sig->cutime;
446a593d6edSOleg Nesterov 		cstime = sig->cstime;
4479ac52315SLaurent Vivier 		cgtime = sig->cgtime;
448a593d6edSOleg Nesterov 		rsslim = sig->rlim[RLIMIT_RSS].rlim_cur;
449a593d6edSOleg Nesterov 
4501da177e4SLinus Torvalds 		/* add up live thread stats at the group level */
4511da177e4SLinus Torvalds 		if (whole) {
452a593d6edSOleg Nesterov 			struct task_struct *t = task;
4531da177e4SLinus Torvalds 			do {
4541da177e4SLinus Torvalds 				min_flt += t->min_flt;
4551da177e4SLinus Torvalds 				maj_flt += t->maj_flt;
456efe567fcSChristian Borntraeger 				utime = cputime_add(utime, task_utime(t));
457efe567fcSChristian Borntraeger 				stime = cputime_add(stime, task_stime(t));
4589ac52315SLaurent Vivier 				gtime = cputime_add(gtime, task_gtime(t));
4591da177e4SLinus Torvalds 				t = next_thread(t);
4601da177e4SLinus Torvalds 			} while (t != task);
461a593d6edSOleg Nesterov 
462a593d6edSOleg Nesterov 			min_flt += sig->min_flt;
463a593d6edSOleg Nesterov 			maj_flt += sig->maj_flt;
464efe567fcSChristian Borntraeger 			utime = cputime_add(utime, sig->utime);
465efe567fcSChristian Borntraeger 			stime = cputime_add(stime, sig->stime);
466f9e26291SChristian Borntraeger 			gtime = cputime_add(gtime, sig->gtime);
4671da177e4SLinus Torvalds 		}
4681da177e4SLinus Torvalds 
469b488893aSPavel Emelyanov 		sid = task_session_nr_ns(task, ns);
470a98fdcefSOleg Nesterov 		ppid = task_tgid_nr_ns(task->real_parent, ns);
471b488893aSPavel Emelyanov 		pgid = task_pgrp_nr_ns(task, ns);
472a593d6edSOleg Nesterov 
473a593d6edSOleg Nesterov 		unlock_task_sighand(task, &flags);
4741da177e4SLinus Torvalds 	}
4751da177e4SLinus Torvalds 
4761da177e4SLinus Torvalds 	if (!whole || num_threads < 2)
4771da177e4SLinus Torvalds 		wchan = get_wchan(task);
4781da177e4SLinus Torvalds 	if (!whole) {
4791da177e4SLinus Torvalds 		min_flt = task->min_flt;
4801da177e4SLinus Torvalds 		maj_flt = task->maj_flt;
481b27f03d4SIngo Molnar 		utime = task_utime(task);
482b27f03d4SIngo Molnar 		stime = task_stime(task);
4839ac52315SLaurent Vivier 		gtime = task_gtime(task);
4841da177e4SLinus Torvalds 	}
4851da177e4SLinus Torvalds 
4861da177e4SLinus Torvalds 	/* scale priority and nice values from timeslices to -20..20 */
4871da177e4SLinus Torvalds 	/* to make it look like a "normal" Unix priority/nice value  */
4881da177e4SLinus Torvalds 	priority = task_prio(task);
4891da177e4SLinus Torvalds 	nice = task_nice(task);
4901da177e4SLinus Torvalds 
4911da177e4SLinus Torvalds 	/* Temporary variable needed for gcc-2.96 */
4921da177e4SLinus Torvalds 	/* convert timespec -> nsec*/
493924b42d5STomas Janousek 	start_time =
494924b42d5STomas Janousek 		(unsigned long long)task->real_start_time.tv_sec * NSEC_PER_SEC
495924b42d5STomas Janousek 				+ task->real_start_time.tv_nsec;
4961da177e4SLinus Torvalds 	/* convert nsec -> ticks */
4971da177e4SLinus Torvalds 	start_time = nsec_to_clock_t(start_time);
4981da177e4SLinus Torvalds 
499ee992744SEric W. Biederman 	seq_printf(m, "%d (%s) %c %d %d %d %d %d %u %lu \
5004dee26b7SRoman Zippel %lu %lu %lu %lu %lu %ld %ld %ld %ld %d 0 %llu %lu %ld %lu %lu %lu %lu %lu \
5019ac52315SLaurent Vivier %lu %lu %lu %lu %lu %lu %lu %lu %d %d %u %u %llu %lu %ld\n",
502ee992744SEric W. Biederman 		pid_nr_ns(pid, ns),
5031da177e4SLinus Torvalds 		tcomm,
5041da177e4SLinus Torvalds 		state,
5051da177e4SLinus Torvalds 		ppid,
5061da177e4SLinus Torvalds 		pgid,
5071da177e4SLinus Torvalds 		sid,
5081da177e4SLinus Torvalds 		tty_nr,
5091da177e4SLinus Torvalds 		tty_pgrp,
5101da177e4SLinus Torvalds 		task->flags,
5111da177e4SLinus Torvalds 		min_flt,
5121da177e4SLinus Torvalds 		cmin_flt,
5131da177e4SLinus Torvalds 		maj_flt,
5141da177e4SLinus Torvalds 		cmaj_flt,
515efe567fcSChristian Borntraeger 		cputime_to_clock_t(utime),
516efe567fcSChristian Borntraeger 		cputime_to_clock_t(stime),
5171da177e4SLinus Torvalds 		cputime_to_clock_t(cutime),
5181da177e4SLinus Torvalds 		cputime_to_clock_t(cstime),
5191da177e4SLinus Torvalds 		priority,
5201da177e4SLinus Torvalds 		nice,
5211da177e4SLinus Torvalds 		num_threads,
5221da177e4SLinus Torvalds 		start_time,
5231da177e4SLinus Torvalds 		vsize,
5244294621fSHugh Dickins 		mm ? get_mm_rss(mm) : 0,
5251da177e4SLinus Torvalds 		rsslim,
5261da177e4SLinus Torvalds 		mm ? mm->start_code : 0,
5271da177e4SLinus Torvalds 		mm ? mm->end_code : 0,
5281da177e4SLinus Torvalds 		mm ? mm->start_stack : 0,
5291da177e4SLinus Torvalds 		esp,
5301da177e4SLinus Torvalds 		eip,
5311da177e4SLinus Torvalds 		/* The signal information here is obsolete.
5321da177e4SLinus Torvalds 		 * It must be decimal for Linux 2.0 compatibility.
5331da177e4SLinus Torvalds 		 * Use /proc/#/status for real-time signals.
5341da177e4SLinus Torvalds 		 */
5351da177e4SLinus Torvalds 		task->pending.signal.sig[0] & 0x7fffffffUL,
5361da177e4SLinus Torvalds 		task->blocked.sig[0] & 0x7fffffffUL,
5371da177e4SLinus Torvalds 		sigign      .sig[0] & 0x7fffffffUL,
5381da177e4SLinus Torvalds 		sigcatch    .sig[0] & 0x7fffffffUL,
5391da177e4SLinus Torvalds 		wchan,
5401da177e4SLinus Torvalds 		0UL,
5411da177e4SLinus Torvalds 		0UL,
5421da177e4SLinus Torvalds 		task->exit_signal,
5431da177e4SLinus Torvalds 		task_cpu(task),
5441da177e4SLinus Torvalds 		task->rt_priority,
54525890454SShailabh Nagar 		task->policy,
5469ac52315SLaurent Vivier 		(unsigned long long)delayacct_blkio_ticks(task),
5479ac52315SLaurent Vivier 		cputime_to_clock_t(gtime),
5489ac52315SLaurent Vivier 		cputime_to_clock_t(cgtime));
5491da177e4SLinus Torvalds 	if (mm)
5501da177e4SLinus Torvalds 		mmput(mm);
551ee992744SEric W. Biederman 	return 0;
5521da177e4SLinus Torvalds }
5531da177e4SLinus Torvalds 
554ee992744SEric W. Biederman int proc_tid_stat(struct seq_file *m, struct pid_namespace *ns,
555ee992744SEric W. Biederman 			struct pid *pid, struct task_struct *task)
5561da177e4SLinus Torvalds {
557ee992744SEric W. Biederman 	return do_task_stat(m, ns, pid, task, 0);
5581da177e4SLinus Torvalds }
5591da177e4SLinus Torvalds 
560ee992744SEric W. Biederman int proc_tgid_stat(struct seq_file *m, struct pid_namespace *ns,
561ee992744SEric W. Biederman 			struct pid *pid, struct task_struct *task)
5621da177e4SLinus Torvalds {
563ee992744SEric W. Biederman 	return do_task_stat(m, ns, pid, task, 1);
5641da177e4SLinus Torvalds }
5651da177e4SLinus Torvalds 
566a56d3fc7SEric W. Biederman int proc_pid_statm(struct seq_file *m, struct pid_namespace *ns,
567a56d3fc7SEric W. Biederman 			struct pid *pid, struct task_struct *task)
5681da177e4SLinus Torvalds {
5691da177e4SLinus Torvalds 	int size = 0, resident = 0, shared = 0, text = 0, lib = 0, data = 0;
5701da177e4SLinus Torvalds 	struct mm_struct *mm = get_task_mm(task);
5711da177e4SLinus Torvalds 
5721da177e4SLinus Torvalds 	if (mm) {
5731da177e4SLinus Torvalds 		size = task_statm(mm, &shared, &text, &data, &resident);
5741da177e4SLinus Torvalds 		mmput(mm);
5751da177e4SLinus Torvalds 	}
576a56d3fc7SEric W. Biederman 	seq_printf(m, "%d %d %d %d %d %d %d\n",
5771da177e4SLinus Torvalds 			size, resident, shared, text, lib, data, 0);
578a56d3fc7SEric W. Biederman 
579a56d3fc7SEric W. Biederman 	return 0;
5801da177e4SLinus Torvalds }
581