xref: /openbmc/linux/fs/proc/array.c (revision 1587e2b1)
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>
761da177e4SLinus Torvalds #include <linux/times.h>
771da177e4SLinus Torvalds #include <linux/cpuset.h>
784fb3a538SDipankar Sarma #include <linux/rcupdate.h>
7925890454SShailabh Nagar #include <linux/delayacct.h>
80b488893aSPavel Emelyanov #include <linux/pid_namespace.h>
811da177e4SLinus Torvalds 
821da177e4SLinus Torvalds #include <asm/pgtable.h>
831da177e4SLinus Torvalds #include <asm/processor.h>
841da177e4SLinus Torvalds #include "internal.h"
851da177e4SLinus Torvalds 
861da177e4SLinus Torvalds /* Gcc optimizes away "strlen(x)" for constant x */
871da177e4SLinus Torvalds #define ADDBUF(buffer, string) \
881da177e4SLinus Torvalds do { memcpy(buffer, string, strlen(string)); \
891da177e4SLinus Torvalds      buffer += strlen(string); } while (0)
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds static inline char *task_name(struct task_struct *p, char *buf)
921da177e4SLinus Torvalds {
931da177e4SLinus Torvalds 	int i;
941da177e4SLinus Torvalds 	char *name;
951da177e4SLinus Torvalds 	char tcomm[sizeof(p->comm)];
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds 	get_task_comm(tcomm, p);
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds 	ADDBUF(buf, "Name:\t");
1001da177e4SLinus Torvalds 	name = tcomm;
1011da177e4SLinus Torvalds 	i = sizeof(tcomm);
1021da177e4SLinus Torvalds 	do {
1031da177e4SLinus Torvalds 		unsigned char c = *name;
1041da177e4SLinus Torvalds 		name++;
1051da177e4SLinus Torvalds 		i--;
1061da177e4SLinus Torvalds 		*buf = c;
1071da177e4SLinus Torvalds 		if (!c)
1081da177e4SLinus Torvalds 			break;
1091da177e4SLinus Torvalds 		if (c == '\\') {
1101da177e4SLinus Torvalds 			buf[1] = c;
1111da177e4SLinus Torvalds 			buf += 2;
1121da177e4SLinus Torvalds 			continue;
1131da177e4SLinus Torvalds 		}
1141da177e4SLinus Torvalds 		if (c == '\n') {
1151da177e4SLinus Torvalds 			buf[0] = '\\';
1161da177e4SLinus Torvalds 			buf[1] = 'n';
1171da177e4SLinus Torvalds 			buf += 2;
1181da177e4SLinus Torvalds 			continue;
1191da177e4SLinus Torvalds 		}
1201da177e4SLinus Torvalds 		buf++;
1211da177e4SLinus Torvalds 	} while (i);
1221da177e4SLinus Torvalds 	*buf = '\n';
1231da177e4SLinus Torvalds 	return buf+1;
1241da177e4SLinus Torvalds }
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds /*
1271da177e4SLinus Torvalds  * The task state array is a strange "bitmap" of
1281da177e4SLinus Torvalds  * reasons to sleep. Thus "running" is zero, and
1291da177e4SLinus Torvalds  * you can test for combinations of others with
1301da177e4SLinus Torvalds  * simple bit tests.
1311da177e4SLinus Torvalds  */
1321da177e4SLinus Torvalds static const char *task_state_array[] = {
1331da177e4SLinus Torvalds 	"R (running)",		/*  0 */
1341da177e4SLinus Torvalds 	"S (sleeping)",		/*  1 */
1351da177e4SLinus Torvalds 	"D (disk sleep)",	/*  2 */
1361da177e4SLinus Torvalds 	"T (stopped)",		/*  4 */
1371da177e4SLinus Torvalds 	"T (tracing stop)",	/*  8 */
1381da177e4SLinus Torvalds 	"Z (zombie)",		/* 16 */
1391da177e4SLinus Torvalds 	"X (dead)"		/* 32 */
1401da177e4SLinus Torvalds };
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds static inline const char *get_task_state(struct task_struct *tsk)
1431da177e4SLinus Torvalds {
1441587e2b1SMatthew Wilcox 	unsigned int state = (tsk->state & TASK_REPORT) | tsk->exit_state;
1451da177e4SLinus Torvalds 	const char **p = &task_state_array[0];
1461da177e4SLinus Torvalds 
1471da177e4SLinus Torvalds 	while (state) {
1481da177e4SLinus Torvalds 		p++;
1491da177e4SLinus Torvalds 		state >>= 1;
1501da177e4SLinus Torvalds 	}
1511da177e4SLinus Torvalds 	return *p;
1521da177e4SLinus Torvalds }
1531da177e4SLinus Torvalds 
1541da177e4SLinus Torvalds static inline char *task_state(struct task_struct *p, char *buffer)
1551da177e4SLinus Torvalds {
1561da177e4SLinus Torvalds 	struct group_info *group_info;
1571da177e4SLinus Torvalds 	int g;
158badf1662SDipankar Sarma 	struct fdtable *fdt = NULL;
159b488893aSPavel Emelyanov 	struct pid_namespace *ns;
160b488893aSPavel Emelyanov 	pid_t ppid, tpid;
1611da177e4SLinus Torvalds 
162b488893aSPavel Emelyanov 	ns = current->nsproxy->pid_ns;
163b0fa9db6SOleg Nesterov 	rcu_read_lock();
164b488893aSPavel Emelyanov 	ppid = pid_alive(p) ?
165b488893aSPavel Emelyanov 		task_tgid_nr_ns(rcu_dereference(p->real_parent), ns) : 0;
166b488893aSPavel Emelyanov 	tpid = pid_alive(p) && p->ptrace ?
167b488893aSPavel Emelyanov 		task_ppid_nr_ns(rcu_dereference(p->parent), ns) : 0;
1681da177e4SLinus Torvalds 	buffer += sprintf(buffer,
1691da177e4SLinus Torvalds 		"State:\t%s\n"
1701da177e4SLinus Torvalds 		"Tgid:\t%d\n"
1711da177e4SLinus Torvalds 		"Pid:\t%d\n"
1721da177e4SLinus Torvalds 		"PPid:\t%d\n"
1731da177e4SLinus Torvalds 		"TracerPid:\t%d\n"
1741da177e4SLinus Torvalds 		"Uid:\t%d\t%d\t%d\t%d\n"
1751da177e4SLinus Torvalds 		"Gid:\t%d\t%d\t%d\t%d\n",
1761da177e4SLinus Torvalds 		get_task_state(p),
177b488893aSPavel Emelyanov 		task_tgid_nr_ns(p, ns),
178b488893aSPavel Emelyanov 		task_pid_nr_ns(p, ns),
179b488893aSPavel Emelyanov 		ppid, tpid,
1801da177e4SLinus Torvalds 		p->uid, p->euid, p->suid, p->fsuid,
1811da177e4SLinus Torvalds 		p->gid, p->egid, p->sgid, p->fsgid);
182b0fa9db6SOleg Nesterov 
1831da177e4SLinus Torvalds 	task_lock(p);
184badf1662SDipankar Sarma 	if (p->files)
185badf1662SDipankar Sarma 		fdt = files_fdtable(p->files);
1861da177e4SLinus Torvalds 	buffer += sprintf(buffer,
1871da177e4SLinus Torvalds 		"FDSize:\t%d\n"
1881da177e4SLinus Torvalds 		"Groups:\t",
189badf1662SDipankar Sarma 		fdt ? fdt->max_fds : 0);
1904fb3a538SDipankar Sarma 	rcu_read_unlock();
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds 	group_info = p->group_info;
1931da177e4SLinus Torvalds 	get_group_info(group_info);
1941da177e4SLinus Torvalds 	task_unlock(p);
1951da177e4SLinus Torvalds 
1961da177e4SLinus Torvalds 	for (g = 0; g < min(group_info->ngroups, NGROUPS_SMALL); g++)
1971da177e4SLinus Torvalds 		buffer += sprintf(buffer, "%d ", GROUP_AT(group_info, g));
1981da177e4SLinus Torvalds 	put_group_info(group_info);
1991da177e4SLinus Torvalds 
2001da177e4SLinus Torvalds 	buffer += sprintf(buffer, "\n");
2011da177e4SLinus Torvalds 	return buffer;
2021da177e4SLinus Torvalds }
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds static char *render_sigset_t(const char *header, sigset_t *set, char *buffer)
2051da177e4SLinus Torvalds {
2061da177e4SLinus Torvalds 	int i, len;
2071da177e4SLinus Torvalds 
2081da177e4SLinus Torvalds 	len = strlen(header);
2091da177e4SLinus Torvalds 	memcpy(buffer, header, len);
2101da177e4SLinus Torvalds 	buffer += len;
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds 	i = _NSIG;
2131da177e4SLinus Torvalds 	do {
2141da177e4SLinus Torvalds 		int x = 0;
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds 		i -= 4;
2171da177e4SLinus Torvalds 		if (sigismember(set, i+1)) x |= 1;
2181da177e4SLinus Torvalds 		if (sigismember(set, i+2)) x |= 2;
2191da177e4SLinus Torvalds 		if (sigismember(set, i+3)) x |= 4;
2201da177e4SLinus Torvalds 		if (sigismember(set, i+4)) x |= 8;
2211da177e4SLinus Torvalds 		*buffer++ = (x < 10 ? '0' : 'a' - 10) + x;
2221da177e4SLinus Torvalds 	} while (i >= 4);
2231da177e4SLinus Torvalds 
2241da177e4SLinus Torvalds 	*buffer++ = '\n';
2251da177e4SLinus Torvalds 	*buffer = 0;
2261da177e4SLinus Torvalds 	return buffer;
2271da177e4SLinus Torvalds }
2281da177e4SLinus Torvalds 
2291da177e4SLinus Torvalds static void collect_sigign_sigcatch(struct task_struct *p, sigset_t *ign,
2301da177e4SLinus Torvalds 				    sigset_t *catch)
2311da177e4SLinus Torvalds {
2321da177e4SLinus Torvalds 	struct k_sigaction *k;
2331da177e4SLinus Torvalds 	int i;
2341da177e4SLinus Torvalds 
2351da177e4SLinus Torvalds 	k = p->sighand->action;
2361da177e4SLinus Torvalds 	for (i = 1; i <= _NSIG; ++i, ++k) {
2371da177e4SLinus Torvalds 		if (k->sa.sa_handler == SIG_IGN)
2381da177e4SLinus Torvalds 			sigaddset(ign, i);
2391da177e4SLinus Torvalds 		else if (k->sa.sa_handler != SIG_DFL)
2401da177e4SLinus Torvalds 			sigaddset(catch, i);
2411da177e4SLinus Torvalds 	}
2421da177e4SLinus Torvalds }
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds static inline char *task_sig(struct task_struct *p, char *buffer)
2451da177e4SLinus Torvalds {
2465e6b3f42SOleg Nesterov 	unsigned long flags;
2471da177e4SLinus Torvalds 	sigset_t pending, shpending, blocked, ignored, caught;
2481da177e4SLinus Torvalds 	int num_threads = 0;
2491da177e4SLinus Torvalds 	unsigned long qsize = 0;
2501da177e4SLinus Torvalds 	unsigned long qlim = 0;
2511da177e4SLinus Torvalds 
2521da177e4SLinus Torvalds 	sigemptyset(&pending);
2531da177e4SLinus Torvalds 	sigemptyset(&shpending);
2541da177e4SLinus Torvalds 	sigemptyset(&blocked);
2551da177e4SLinus Torvalds 	sigemptyset(&ignored);
2561da177e4SLinus Torvalds 	sigemptyset(&caught);
2571da177e4SLinus Torvalds 
2585e6b3f42SOleg Nesterov 	rcu_read_lock();
2595e6b3f42SOleg Nesterov 	if (lock_task_sighand(p, &flags)) {
2601da177e4SLinus Torvalds 		pending = p->pending.signal;
2611da177e4SLinus Torvalds 		shpending = p->signal->shared_pending.signal;
2621da177e4SLinus Torvalds 		blocked = p->blocked;
2631da177e4SLinus Torvalds 		collect_sigign_sigcatch(p, &ignored, &caught);
2641da177e4SLinus Torvalds 		num_threads = atomic_read(&p->signal->count);
2651da177e4SLinus Torvalds 		qsize = atomic_read(&p->user->sigpending);
2661da177e4SLinus Torvalds 		qlim = p->signal->rlim[RLIMIT_SIGPENDING].rlim_cur;
2675e6b3f42SOleg Nesterov 		unlock_task_sighand(p, &flags);
2681da177e4SLinus Torvalds 	}
2695e6b3f42SOleg Nesterov 	rcu_read_unlock();
2701da177e4SLinus Torvalds 
2711da177e4SLinus Torvalds 	buffer += sprintf(buffer, "Threads:\t%d\n", num_threads);
2721da177e4SLinus Torvalds 	buffer += sprintf(buffer, "SigQ:\t%lu/%lu\n", qsize, qlim);
2731da177e4SLinus Torvalds 
2741da177e4SLinus Torvalds 	/* render them all */
2751da177e4SLinus Torvalds 	buffer = render_sigset_t("SigPnd:\t", &pending, buffer);
2761da177e4SLinus Torvalds 	buffer = render_sigset_t("ShdPnd:\t", &shpending, buffer);
2771da177e4SLinus Torvalds 	buffer = render_sigset_t("SigBlk:\t", &blocked, buffer);
2781da177e4SLinus Torvalds 	buffer = render_sigset_t("SigIgn:\t", &ignored, buffer);
2791da177e4SLinus Torvalds 	buffer = render_sigset_t("SigCgt:\t", &caught, buffer);
2801da177e4SLinus Torvalds 
2811da177e4SLinus Torvalds 	return buffer;
2821da177e4SLinus Torvalds }
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds static inline char *task_cap(struct task_struct *p, char *buffer)
2851da177e4SLinus Torvalds {
2861da177e4SLinus Torvalds     return buffer + sprintf(buffer, "CapInh:\t%016x\n"
2871da177e4SLinus Torvalds 			    "CapPrm:\t%016x\n"
2881da177e4SLinus Torvalds 			    "CapEff:\t%016x\n",
2891da177e4SLinus Torvalds 			    cap_t(p->cap_inheritable),
2901da177e4SLinus Torvalds 			    cap_t(p->cap_permitted),
2911da177e4SLinus Torvalds 			    cap_t(p->cap_effective));
2921da177e4SLinus Torvalds }
2931da177e4SLinus Torvalds 
294b663a79cSMaxim Uvarov static inline char *task_context_switch_counts(struct task_struct *p,
295b663a79cSMaxim Uvarov 						char *buffer)
296b663a79cSMaxim Uvarov {
297b663a79cSMaxim Uvarov 	return buffer + sprintf(buffer, "voluntary_ctxt_switches:\t%lu\n"
298b663a79cSMaxim Uvarov 			    "nonvoluntary_ctxt_switches:\t%lu\n",
299b663a79cSMaxim Uvarov 			    p->nvcsw,
300b663a79cSMaxim Uvarov 			    p->nivcsw);
301b663a79cSMaxim Uvarov }
302b663a79cSMaxim Uvarov 
3031da177e4SLinus Torvalds int proc_pid_status(struct task_struct *task, char *buffer)
3041da177e4SLinus Torvalds {
3051da177e4SLinus Torvalds 	char *orig = buffer;
3061da177e4SLinus Torvalds 	struct mm_struct *mm = get_task_mm(task);
3071da177e4SLinus Torvalds 
3081da177e4SLinus Torvalds 	buffer = task_name(task, buffer);
3091da177e4SLinus Torvalds 	buffer = task_state(task, buffer);
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds 	if (mm) {
3121da177e4SLinus Torvalds 		buffer = task_mem(mm, buffer);
3131da177e4SLinus Torvalds 		mmput(mm);
3141da177e4SLinus Torvalds 	}
3151da177e4SLinus Torvalds 	buffer = task_sig(task, buffer);
3161da177e4SLinus Torvalds 	buffer = task_cap(task, buffer);
3171da177e4SLinus Torvalds 	buffer = cpuset_task_status_allowed(task, buffer);
318347a8dc3SMartin Schwidefsky #if defined(CONFIG_S390)
3191da177e4SLinus Torvalds 	buffer = task_show_regs(task, buffer);
3201da177e4SLinus Torvalds #endif
321b663a79cSMaxim Uvarov 	buffer = task_context_switch_counts(task, buffer);
3221da177e4SLinus Torvalds 	return buffer - orig;
3231da177e4SLinus Torvalds }
3241da177e4SLinus Torvalds 
325efe567fcSChristian Borntraeger /*
326efe567fcSChristian Borntraeger  * Use precise platform statistics if available:
327efe567fcSChristian Borntraeger  */
328efe567fcSChristian Borntraeger #ifdef CONFIG_VIRT_CPU_ACCOUNTING
329efe567fcSChristian Borntraeger static cputime_t task_utime(struct task_struct *p)
330efe567fcSChristian Borntraeger {
331efe567fcSChristian Borntraeger 	return p->utime;
332efe567fcSChristian Borntraeger }
333efe567fcSChristian Borntraeger 
334efe567fcSChristian Borntraeger static cputime_t task_stime(struct task_struct *p)
335efe567fcSChristian Borntraeger {
336efe567fcSChristian Borntraeger 	return p->stime;
337efe567fcSChristian Borntraeger }
338efe567fcSChristian Borntraeger #else
339efe567fcSChristian Borntraeger static cputime_t task_utime(struct task_struct *p)
340b27f03d4SIngo Molnar {
341b27f03d4SIngo Molnar 	clock_t utime = cputime_to_clock_t(p->utime),
342b27f03d4SIngo Molnar 		total = utime + cputime_to_clock_t(p->stime);
343b27f03d4SIngo Molnar 	u64 temp;
344b27f03d4SIngo Molnar 
345b27f03d4SIngo Molnar 	/*
346b27f03d4SIngo Molnar 	 * Use CFS's precise accounting:
347b27f03d4SIngo Molnar 	 */
348b27f03d4SIngo Molnar 	temp = (u64)nsec_to_clock_t(p->se.sum_exec_runtime);
349b27f03d4SIngo Molnar 
350b27f03d4SIngo Molnar 	if (total) {
351b27f03d4SIngo Molnar 		temp *= utime;
352b27f03d4SIngo Molnar 		do_div(temp, total);
353b27f03d4SIngo Molnar 	}
354b27f03d4SIngo Molnar 	utime = (clock_t)temp;
355b27f03d4SIngo Molnar 
35673a2bcb0SPeter Zijlstra 	p->prev_utime = max(p->prev_utime, clock_t_to_cputime(utime));
35773a2bcb0SPeter Zijlstra 	return p->prev_utime;
358b27f03d4SIngo Molnar }
359b27f03d4SIngo Molnar 
360efe567fcSChristian Borntraeger static cputime_t task_stime(struct task_struct *p)
361b27f03d4SIngo Molnar {
3625926c50bSIngo Molnar 	clock_t stime;
363b27f03d4SIngo Molnar 
364b27f03d4SIngo Molnar 	/*
365b27f03d4SIngo Molnar 	 * Use CFS's precise accounting. (we subtract utime from
366b27f03d4SIngo Molnar 	 * the total, to make sure the total observed by userspace
367b27f03d4SIngo Molnar 	 * grows monotonically - apps rely on that):
368b27f03d4SIngo Molnar 	 */
369efe567fcSChristian Borntraeger 	stime = nsec_to_clock_t(p->se.sum_exec_runtime) -
370efe567fcSChristian Borntraeger 			cputime_to_clock_t(task_utime(p));
371b27f03d4SIngo Molnar 
37208e4570aSIngo Molnar 	if (stime >= 0)
3739301899bSBalbir Singh 		p->prev_stime = max(p->prev_stime, clock_t_to_cputime(stime));
37408e4570aSIngo Molnar 
3759301899bSBalbir Singh 	return p->prev_stime;
376b27f03d4SIngo Molnar }
377efe567fcSChristian Borntraeger #endif
378b27f03d4SIngo Molnar 
3799ac52315SLaurent Vivier static cputime_t task_gtime(struct task_struct *p)
3809ac52315SLaurent Vivier {
3819ac52315SLaurent Vivier 	return p->gtime;
3829ac52315SLaurent Vivier }
3839ac52315SLaurent Vivier 
3841da177e4SLinus Torvalds static int do_task_stat(struct task_struct *task, char *buffer, int whole)
3851da177e4SLinus Torvalds {
3861da177e4SLinus Torvalds 	unsigned long vsize, eip, esp, wchan = ~0UL;
3871da177e4SLinus Torvalds 	long priority, nice;
3881da177e4SLinus Torvalds 	int tty_pgrp = -1, tty_nr = 0;
3891da177e4SLinus Torvalds 	sigset_t sigign, sigcatch;
3901da177e4SLinus Torvalds 	char state;
3911da177e4SLinus Torvalds 	int res;
392a593d6edSOleg Nesterov 	pid_t ppid = 0, pgid = -1, sid = -1;
3931da177e4SLinus Torvalds 	int num_threads = 0;
3941da177e4SLinus Torvalds 	struct mm_struct *mm;
3951da177e4SLinus Torvalds 	unsigned long long start_time;
3961da177e4SLinus Torvalds 	unsigned long cmin_flt = 0, cmaj_flt = 0;
3971da177e4SLinus Torvalds 	unsigned long  min_flt = 0,  maj_flt = 0;
398efe567fcSChristian Borntraeger 	cputime_t cutime, cstime, utime, stime;
3999ac52315SLaurent Vivier 	cputime_t cgtime, gtime;
4001da177e4SLinus Torvalds 	unsigned long rsslim = 0;
4011da177e4SLinus Torvalds 	char tcomm[sizeof(task->comm)];
402a593d6edSOleg Nesterov 	unsigned long flags;
403b488893aSPavel Emelyanov 	struct pid_namespace *ns;
404b488893aSPavel Emelyanov 
405b488893aSPavel Emelyanov 	ns = current->nsproxy->pid_ns;
4061da177e4SLinus Torvalds 
4071da177e4SLinus Torvalds 	state = *get_task_state(task);
4081da177e4SLinus Torvalds 	vsize = eip = esp = 0;
4091da177e4SLinus Torvalds 	mm = get_task_mm(task);
4101da177e4SLinus Torvalds 	if (mm) {
4111da177e4SLinus Torvalds 		vsize = task_vsize(mm);
4121da177e4SLinus Torvalds 		eip = KSTK_EIP(task);
4131da177e4SLinus Torvalds 		esp = KSTK_ESP(task);
4141da177e4SLinus Torvalds 	}
4151da177e4SLinus Torvalds 
4161da177e4SLinus Torvalds 	get_task_comm(tcomm, task);
4171da177e4SLinus Torvalds 
4181da177e4SLinus Torvalds 	sigemptyset(&sigign);
4191da177e4SLinus Torvalds 	sigemptyset(&sigcatch);
420efe567fcSChristian Borntraeger 	cutime = cstime = utime = stime = cputime_zero;
4219ac52315SLaurent Vivier 	cgtime = gtime = cputime_zero;
4223cfd0885SAlan Cox 
423a593d6edSOleg Nesterov 	rcu_read_lock();
424a593d6edSOleg Nesterov 	if (lock_task_sighand(task, &flags)) {
425a593d6edSOleg Nesterov 		struct signal_struct *sig = task->signal;
426a593d6edSOleg Nesterov 
42791593504SOleg Nesterov 		if (sig->tty) {
428b488893aSPavel Emelyanov 			tty_pgrp = pid_nr_ns(sig->tty->pgrp, ns);
42991593504SOleg Nesterov 			tty_nr = new_encode_dev(tty_devnum(sig->tty));
430a593d6edSOleg Nesterov 		}
431a593d6edSOleg Nesterov 
432a593d6edSOleg Nesterov 		num_threads = atomic_read(&sig->count);
4331da177e4SLinus Torvalds 		collect_sigign_sigcatch(task, &sigign, &sigcatch);
4341da177e4SLinus Torvalds 
435a593d6edSOleg Nesterov 		cmin_flt = sig->cmin_flt;
436a593d6edSOleg Nesterov 		cmaj_flt = sig->cmaj_flt;
437a593d6edSOleg Nesterov 		cutime = sig->cutime;
438a593d6edSOleg Nesterov 		cstime = sig->cstime;
4399ac52315SLaurent Vivier 		cgtime = sig->cgtime;
440a593d6edSOleg Nesterov 		rsslim = sig->rlim[RLIMIT_RSS].rlim_cur;
441a593d6edSOleg Nesterov 
4421da177e4SLinus Torvalds 		/* add up live thread stats at the group level */
4431da177e4SLinus Torvalds 		if (whole) {
444a593d6edSOleg Nesterov 			struct task_struct *t = task;
4451da177e4SLinus Torvalds 			do {
4461da177e4SLinus Torvalds 				min_flt += t->min_flt;
4471da177e4SLinus Torvalds 				maj_flt += t->maj_flt;
448efe567fcSChristian Borntraeger 				utime = cputime_add(utime, task_utime(t));
449efe567fcSChristian Borntraeger 				stime = cputime_add(stime, task_stime(t));
4509ac52315SLaurent Vivier 				gtime = cputime_add(gtime, task_gtime(t));
4511da177e4SLinus Torvalds 				t = next_thread(t);
4521da177e4SLinus Torvalds 			} while (t != task);
453a593d6edSOleg Nesterov 
454a593d6edSOleg Nesterov 			min_flt += sig->min_flt;
455a593d6edSOleg Nesterov 			maj_flt += sig->maj_flt;
456efe567fcSChristian Borntraeger 			utime = cputime_add(utime, sig->utime);
457efe567fcSChristian Borntraeger 			stime = cputime_add(stime, sig->stime);
458f9e26291SChristian Borntraeger 			gtime = cputime_add(gtime, sig->gtime);
4591da177e4SLinus Torvalds 		}
4601da177e4SLinus Torvalds 
461b488893aSPavel Emelyanov 		sid = task_session_nr_ns(task, ns);
462b488893aSPavel Emelyanov 		pgid = task_pgrp_nr_ns(task, ns);
463b488893aSPavel Emelyanov 		ppid = task_ppid_nr_ns(task, ns);
464a593d6edSOleg Nesterov 
465a593d6edSOleg Nesterov 		unlock_task_sighand(task, &flags);
4661da177e4SLinus Torvalds 	}
467a593d6edSOleg Nesterov 	rcu_read_unlock();
4681da177e4SLinus Torvalds 
4691da177e4SLinus Torvalds 	if (!whole || num_threads < 2)
4701da177e4SLinus Torvalds 		wchan = get_wchan(task);
4711da177e4SLinus Torvalds 	if (!whole) {
4721da177e4SLinus Torvalds 		min_flt = task->min_flt;
4731da177e4SLinus Torvalds 		maj_flt = task->maj_flt;
474b27f03d4SIngo Molnar 		utime = task_utime(task);
475b27f03d4SIngo Molnar 		stime = task_stime(task);
4769ac52315SLaurent Vivier 		gtime = task_gtime(task);
4771da177e4SLinus Torvalds 	}
4781da177e4SLinus Torvalds 
4791da177e4SLinus Torvalds 	/* scale priority and nice values from timeslices to -20..20 */
4801da177e4SLinus Torvalds 	/* to make it look like a "normal" Unix priority/nice value  */
4811da177e4SLinus Torvalds 	priority = task_prio(task);
4821da177e4SLinus Torvalds 	nice = task_nice(task);
4831da177e4SLinus Torvalds 
4841da177e4SLinus Torvalds 	/* Temporary variable needed for gcc-2.96 */
4851da177e4SLinus Torvalds 	/* convert timespec -> nsec*/
486924b42d5STomas Janousek 	start_time =
487924b42d5STomas Janousek 		(unsigned long long)task->real_start_time.tv_sec * NSEC_PER_SEC
488924b42d5STomas Janousek 				+ task->real_start_time.tv_nsec;
4891da177e4SLinus Torvalds 	/* convert nsec -> ticks */
4901da177e4SLinus Torvalds 	start_time = nsec_to_clock_t(start_time);
4911da177e4SLinus Torvalds 
49297dc32cdSWilliam Cohen 	res = sprintf(buffer, "%d (%s) %c %d %d %d %d %d %u %lu \
4934dee26b7SRoman Zippel %lu %lu %lu %lu %lu %ld %ld %ld %ld %d 0 %llu %lu %ld %lu %lu %lu %lu %lu \
4949ac52315SLaurent Vivier %lu %lu %lu %lu %lu %lu %lu %lu %d %d %u %u %llu %lu %ld\n",
495b488893aSPavel Emelyanov 		task_pid_nr_ns(task, ns),
4961da177e4SLinus Torvalds 		tcomm,
4971da177e4SLinus Torvalds 		state,
4981da177e4SLinus Torvalds 		ppid,
4991da177e4SLinus Torvalds 		pgid,
5001da177e4SLinus Torvalds 		sid,
5011da177e4SLinus Torvalds 		tty_nr,
5021da177e4SLinus Torvalds 		tty_pgrp,
5031da177e4SLinus Torvalds 		task->flags,
5041da177e4SLinus Torvalds 		min_flt,
5051da177e4SLinus Torvalds 		cmin_flt,
5061da177e4SLinus Torvalds 		maj_flt,
5071da177e4SLinus Torvalds 		cmaj_flt,
508efe567fcSChristian Borntraeger 		cputime_to_clock_t(utime),
509efe567fcSChristian Borntraeger 		cputime_to_clock_t(stime),
5101da177e4SLinus Torvalds 		cputime_to_clock_t(cutime),
5111da177e4SLinus Torvalds 		cputime_to_clock_t(cstime),
5121da177e4SLinus Torvalds 		priority,
5131da177e4SLinus Torvalds 		nice,
5141da177e4SLinus Torvalds 		num_threads,
5151da177e4SLinus Torvalds 		start_time,
5161da177e4SLinus Torvalds 		vsize,
5174294621fSHugh Dickins 		mm ? get_mm_rss(mm) : 0,
5181da177e4SLinus Torvalds 		rsslim,
5191da177e4SLinus Torvalds 		mm ? mm->start_code : 0,
5201da177e4SLinus Torvalds 		mm ? mm->end_code : 0,
5211da177e4SLinus Torvalds 		mm ? mm->start_stack : 0,
5221da177e4SLinus Torvalds 		esp,
5231da177e4SLinus Torvalds 		eip,
5241da177e4SLinus Torvalds 		/* The signal information here is obsolete.
5251da177e4SLinus Torvalds 		 * It must be decimal for Linux 2.0 compatibility.
5261da177e4SLinus Torvalds 		 * Use /proc/#/status for real-time signals.
5271da177e4SLinus Torvalds 		 */
5281da177e4SLinus Torvalds 		task->pending.signal.sig[0] & 0x7fffffffUL,
5291da177e4SLinus Torvalds 		task->blocked.sig[0] & 0x7fffffffUL,
5301da177e4SLinus Torvalds 		sigign      .sig[0] & 0x7fffffffUL,
5311da177e4SLinus Torvalds 		sigcatch    .sig[0] & 0x7fffffffUL,
5321da177e4SLinus Torvalds 		wchan,
5331da177e4SLinus Torvalds 		0UL,
5341da177e4SLinus Torvalds 		0UL,
5351da177e4SLinus Torvalds 		task->exit_signal,
5361da177e4SLinus Torvalds 		task_cpu(task),
5371da177e4SLinus Torvalds 		task->rt_priority,
53825890454SShailabh Nagar 		task->policy,
5399ac52315SLaurent Vivier 		(unsigned long long)delayacct_blkio_ticks(task),
5409ac52315SLaurent Vivier 		cputime_to_clock_t(gtime),
5419ac52315SLaurent Vivier 		cputime_to_clock_t(cgtime));
5421da177e4SLinus Torvalds 	if (mm)
5431da177e4SLinus Torvalds 		mmput(mm);
5441da177e4SLinus Torvalds 	return res;
5451da177e4SLinus Torvalds }
5461da177e4SLinus Torvalds 
5471da177e4SLinus Torvalds int proc_tid_stat(struct task_struct *task, char *buffer)
5481da177e4SLinus Torvalds {
5491da177e4SLinus Torvalds 	return do_task_stat(task, buffer, 0);
5501da177e4SLinus Torvalds }
5511da177e4SLinus Torvalds 
5521da177e4SLinus Torvalds int proc_tgid_stat(struct task_struct *task, char *buffer)
5531da177e4SLinus Torvalds {
5541da177e4SLinus Torvalds 	return do_task_stat(task, buffer, 1);
5551da177e4SLinus Torvalds }
5561da177e4SLinus Torvalds 
5571da177e4SLinus Torvalds int proc_pid_statm(struct task_struct *task, char *buffer)
5581da177e4SLinus Torvalds {
5591da177e4SLinus Torvalds 	int size = 0, resident = 0, shared = 0, text = 0, lib = 0, data = 0;
5601da177e4SLinus Torvalds 	struct mm_struct *mm = get_task_mm(task);
5611da177e4SLinus Torvalds 
5621da177e4SLinus Torvalds 	if (mm) {
5631da177e4SLinus Torvalds 		size = task_statm(mm, &shared, &text, &data, &resident);
5641da177e4SLinus Torvalds 		mmput(mm);
5651da177e4SLinus Torvalds 	}
5661da177e4SLinus Torvalds 
5671da177e4SLinus Torvalds 	return sprintf(buffer, "%d %d %d %d %d %d %d\n",
5681da177e4SLinus Torvalds 		       size, resident, shared, text, lib, data, 0);
5691da177e4SLinus Torvalds }
570