xref: /openbmc/linux/fs/proc/base.c (revision 3d5992d2ac7dc09aed8ab537cba074589f0f0a52)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/fs/proc/base.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 1991, 1992 Linus Torvalds
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *  proc base directory handling functions
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  *  1999, Al Viro. Rewritten. Now it covers the whole per-process part.
91da177e4SLinus Torvalds  *  Instead of using magical inumbers to determine the kind of object
101da177e4SLinus Torvalds  *  we allocate and fill in-core inodes upon lookup. They don't even
111da177e4SLinus Torvalds  *  go into icache. We cache the reference to task_struct upon lookup too.
121da177e4SLinus Torvalds  *  Eventually it should become a filesystem in its own. We don't use the
131da177e4SLinus Torvalds  *  rest of procfs anymore.
14e070ad49SMauricio Lin  *
15e070ad49SMauricio Lin  *
16e070ad49SMauricio Lin  *  Changelog:
17e070ad49SMauricio Lin  *  17-Jan-2005
18e070ad49SMauricio Lin  *  Allan Bezerra
19e070ad49SMauricio Lin  *  Bruna Moreira <bruna.moreira@indt.org.br>
20e070ad49SMauricio Lin  *  Edjard Mota <edjard.mota@indt.org.br>
21e070ad49SMauricio Lin  *  Ilias Biris <ilias.biris@indt.org.br>
22e070ad49SMauricio Lin  *  Mauricio Lin <mauricio.lin@indt.org.br>
23e070ad49SMauricio Lin  *
24e070ad49SMauricio Lin  *  Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
25e070ad49SMauricio Lin  *
26e070ad49SMauricio Lin  *  A new process specific entry (smaps) included in /proc. It shows the
27e070ad49SMauricio Lin  *  size of rss for each memory area. The maps entry lacks information
28e070ad49SMauricio Lin  *  about physical memory size (rss) for each mapped file, i.e.,
29e070ad49SMauricio Lin  *  rss information for executables and library files.
30e070ad49SMauricio Lin  *  This additional information is useful for any tools that need to know
31e070ad49SMauricio Lin  *  about physical memory consumption for a process specific library.
32e070ad49SMauricio Lin  *
33e070ad49SMauricio Lin  *  Changelog:
34e070ad49SMauricio Lin  *  21-Feb-2005
35e070ad49SMauricio Lin  *  Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36e070ad49SMauricio Lin  *  Pud inclusion in the page table walking.
37e070ad49SMauricio Lin  *
38e070ad49SMauricio Lin  *  ChangeLog:
39e070ad49SMauricio Lin  *  10-Mar-2005
40e070ad49SMauricio Lin  *  10LE Instituto Nokia de Tecnologia - INdT:
41e070ad49SMauricio Lin  *  A better way to walks through the page table as suggested by Hugh Dickins.
42e070ad49SMauricio Lin  *
43e070ad49SMauricio Lin  *  Simo Piiroinen <simo.piiroinen@nokia.com>:
44e070ad49SMauricio Lin  *  Smaps information related to shared, private, clean and dirty pages.
45e070ad49SMauricio Lin  *
46e070ad49SMauricio Lin  *  Paul Mundt <paul.mundt@nokia.com>:
47e070ad49SMauricio Lin  *  Overall revision about smaps.
481da177e4SLinus Torvalds  */
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds #include <asm/uaccess.h>
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds #include <linux/errno.h>
531da177e4SLinus Torvalds #include <linux/time.h>
541da177e4SLinus Torvalds #include <linux/proc_fs.h>
551da177e4SLinus Torvalds #include <linux/stat.h>
565995477aSAndrea Righi #include <linux/task_io_accounting_ops.h>
571da177e4SLinus Torvalds #include <linux/init.h>
5816f7e0feSRandy Dunlap #include <linux/capability.h>
591da177e4SLinus Torvalds #include <linux/file.h>
609f3acc31SAl Viro #include <linux/fdtable.h>
611da177e4SLinus Torvalds #include <linux/string.h>
621da177e4SLinus Torvalds #include <linux/seq_file.h>
631da177e4SLinus Torvalds #include <linux/namei.h>
646b3286edSKirill Korotaev #include <linux/mnt_namespace.h>
651da177e4SLinus Torvalds #include <linux/mm.h>
66a63d83f4SDavid Rientjes #include <linux/swap.h>
67b835996fSDipankar Sarma #include <linux/rcupdate.h>
681da177e4SLinus Torvalds #include <linux/kallsyms.h>
692ec220e2SKen Chen #include <linux/stacktrace.h>
70d85f50d5SNeil Horman #include <linux/resource.h>
715096add8SKees Cook #include <linux/module.h>
721da177e4SLinus Torvalds #include <linux/mount.h>
731da177e4SLinus Torvalds #include <linux/security.h>
741da177e4SLinus Torvalds #include <linux/ptrace.h>
750d094efeSRoland McGrath #include <linux/tracehook.h>
76a424316cSPaul Menage #include <linux/cgroup.h>
771da177e4SLinus Torvalds #include <linux/cpuset.h>
781da177e4SLinus Torvalds #include <linux/audit.h>
795addc5ddSAl Viro #include <linux/poll.h>
801651e14eSSerge E. Hallyn #include <linux/nsproxy.h>
818ac773b4SAlexey Dobriyan #include <linux/oom.h>
823cb4a0bbSKawai, Hidehiro #include <linux/elf.h>
8360347f67SPavel Emelyanov #include <linux/pid_namespace.h>
845ad4e53bSAl Viro #include <linux/fs_struct.h>
855a0e3ad6STejun Heo #include <linux/slab.h>
861da177e4SLinus Torvalds #include "internal.h"
871da177e4SLinus Torvalds 
880f2fe20fSEric W. Biederman /* NOTE:
890f2fe20fSEric W. Biederman  *	Implementing inode permission operations in /proc is almost
900f2fe20fSEric W. Biederman  *	certainly an error.  Permission checks need to happen during
910f2fe20fSEric W. Biederman  *	each system call not at open time.  The reason is that most of
920f2fe20fSEric W. Biederman  *	what we wish to check for permissions in /proc varies at runtime.
930f2fe20fSEric W. Biederman  *
940f2fe20fSEric W. Biederman  *	The classic example of a problem is opening file descriptors
950f2fe20fSEric W. Biederman  *	in /proc for a task before it execs a suid executable.
960f2fe20fSEric W. Biederman  */
970f2fe20fSEric W. Biederman 
981da177e4SLinus Torvalds struct pid_entry {
991da177e4SLinus Torvalds 	char *name;
100c5141e6dSEric Dumazet 	int len;
1011da177e4SLinus Torvalds 	mode_t mode;
102c5ef1c42SArjan van de Ven 	const struct inode_operations *iop;
10300977a59SArjan van de Ven 	const struct file_operations *fop;
10420cdc894SEric W. Biederman 	union proc_op op;
1051da177e4SLinus Torvalds };
1061da177e4SLinus Torvalds 
10761a28784SEric W. Biederman #define NOD(NAME, MODE, IOP, FOP, OP) {			\
10820cdc894SEric W. Biederman 	.name = (NAME),					\
109c5141e6dSEric Dumazet 	.len  = sizeof(NAME) - 1,			\
11020cdc894SEric W. Biederman 	.mode = MODE,					\
11120cdc894SEric W. Biederman 	.iop  = IOP,					\
11220cdc894SEric W. Biederman 	.fop  = FOP,					\
11320cdc894SEric W. Biederman 	.op   = OP,					\
11420cdc894SEric W. Biederman }
11520cdc894SEric W. Biederman 
116631f9c18SAlexey Dobriyan #define DIR(NAME, MODE, iops, fops)	\
117631f9c18SAlexey Dobriyan 	NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
118631f9c18SAlexey Dobriyan #define LNK(NAME, get_link)					\
11961a28784SEric W. Biederman 	NOD(NAME, (S_IFLNK|S_IRWXUGO),				\
12020cdc894SEric W. Biederman 		&proc_pid_link_inode_operations, NULL,		\
121631f9c18SAlexey Dobriyan 		{ .proc_get_link = get_link } )
122631f9c18SAlexey Dobriyan #define REG(NAME, MODE, fops)				\
123631f9c18SAlexey Dobriyan 	NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
124631f9c18SAlexey Dobriyan #define INF(NAME, MODE, read)				\
12561a28784SEric W. Biederman 	NOD(NAME, (S_IFREG|(MODE)), 			\
12620cdc894SEric W. Biederman 		NULL, &proc_info_file_operations,	\
127631f9c18SAlexey Dobriyan 		{ .proc_read = read } )
128631f9c18SAlexey Dobriyan #define ONE(NAME, MODE, show)				\
129be614086SEric W. Biederman 	NOD(NAME, (S_IFREG|(MODE)), 			\
130be614086SEric W. Biederman 		NULL, &proc_single_file_operations,	\
131631f9c18SAlexey Dobriyan 		{ .proc_show = show } )
1321da177e4SLinus Torvalds 
133aed54175SVegard Nossum /*
134aed54175SVegard Nossum  * Count the number of hardlinks for the pid_entry table, excluding the .
135aed54175SVegard Nossum  * and .. links.
136aed54175SVegard Nossum  */
137aed54175SVegard Nossum static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
138aed54175SVegard Nossum 	unsigned int n)
139aed54175SVegard Nossum {
140aed54175SVegard Nossum 	unsigned int i;
141aed54175SVegard Nossum 	unsigned int count;
142aed54175SVegard Nossum 
143aed54175SVegard Nossum 	count = 0;
144aed54175SVegard Nossum 	for (i = 0; i < n; ++i) {
145aed54175SVegard Nossum 		if (S_ISDIR(entries[i].mode))
146aed54175SVegard Nossum 			++count;
147aed54175SVegard Nossum 	}
148aed54175SVegard Nossum 
149aed54175SVegard Nossum 	return count;
150aed54175SVegard Nossum }
151aed54175SVegard Nossum 
152f7ad3c6bSMiklos Szeredi static int get_task_root(struct task_struct *task, struct path *root)
1531da177e4SLinus Torvalds {
1547c2c7d99SHugh Dickins 	int result = -ENOENT;
1557c2c7d99SHugh Dickins 
1560494f6ecSMiklos Szeredi 	task_lock(task);
157f7ad3c6bSMiklos Szeredi 	if (task->fs) {
158f7ad3c6bSMiklos Szeredi 		get_fs_root(task->fs, root);
1597c2c7d99SHugh Dickins 		result = 0;
1607c2c7d99SHugh Dickins 	}
1610494f6ecSMiklos Szeredi 	task_unlock(task);
1627c2c7d99SHugh Dickins 	return result;
1630494f6ecSMiklos Szeredi }
1640494f6ecSMiklos Szeredi 
1653dcd25f3SJan Blunck static int proc_cwd_link(struct inode *inode, struct path *path)
1660494f6ecSMiklos Szeredi {
16799f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
1680494f6ecSMiklos Szeredi 	int result = -ENOENT;
16999f89551SEric W. Biederman 
17099f89551SEric W. Biederman 	if (task) {
171f7ad3c6bSMiklos Szeredi 		task_lock(task);
172f7ad3c6bSMiklos Szeredi 		if (task->fs) {
173f7ad3c6bSMiklos Szeredi 			get_fs_pwd(task->fs, path);
174f7ad3c6bSMiklos Szeredi 			result = 0;
175f7ad3c6bSMiklos Szeredi 		}
176f7ad3c6bSMiklos Szeredi 		task_unlock(task);
17799f89551SEric W. Biederman 		put_task_struct(task);
17899f89551SEric W. Biederman 	}
1791da177e4SLinus Torvalds 	return result;
1801da177e4SLinus Torvalds }
1811da177e4SLinus Torvalds 
1823dcd25f3SJan Blunck static int proc_root_link(struct inode *inode, struct path *path)
1831da177e4SLinus Torvalds {
18499f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
1851da177e4SLinus Torvalds 	int result = -ENOENT;
18699f89551SEric W. Biederman 
18799f89551SEric W. Biederman 	if (task) {
188f7ad3c6bSMiklos Szeredi 		result = get_task_root(task, path);
18999f89551SEric W. Biederman 		put_task_struct(task);
19099f89551SEric W. Biederman 	}
1911da177e4SLinus Torvalds 	return result;
1921da177e4SLinus Torvalds }
1931da177e4SLinus Torvalds 
194638fa202SRoland McGrath /*
195638fa202SRoland McGrath  * Return zero if current may access user memory in @task, -error if not.
196638fa202SRoland McGrath  */
197638fa202SRoland McGrath static int check_mem_permission(struct task_struct *task)
198638fa202SRoland McGrath {
199638fa202SRoland McGrath 	/*
200638fa202SRoland McGrath 	 * A task can always look at itself, in case it chooses
201638fa202SRoland McGrath 	 * to use system calls instead of load instructions.
202638fa202SRoland McGrath 	 */
203638fa202SRoland McGrath 	if (task == current)
204638fa202SRoland McGrath 		return 0;
205638fa202SRoland McGrath 
206638fa202SRoland McGrath 	/*
207638fa202SRoland McGrath 	 * If current is actively ptrace'ing, and would also be
208638fa202SRoland McGrath 	 * permitted to freshly attach with ptrace now, permit it.
209638fa202SRoland McGrath 	 */
2100d094efeSRoland McGrath 	if (task_is_stopped_or_traced(task)) {
2110d094efeSRoland McGrath 		int match;
2120d094efeSRoland McGrath 		rcu_read_lock();
2130d094efeSRoland McGrath 		match = (tracehook_tracer_task(task) == current);
2140d094efeSRoland McGrath 		rcu_read_unlock();
2150d094efeSRoland McGrath 		if (match && ptrace_may_access(task, PTRACE_MODE_ATTACH))
216638fa202SRoland McGrath 			return 0;
2170d094efeSRoland McGrath 	}
218638fa202SRoland McGrath 
219638fa202SRoland McGrath 	/*
220638fa202SRoland McGrath 	 * Noone else is allowed.
221638fa202SRoland McGrath 	 */
222638fa202SRoland McGrath 	return -EPERM;
223638fa202SRoland McGrath }
2241da177e4SLinus Torvalds 
225831830b5SAl Viro struct mm_struct *mm_for_maps(struct task_struct *task)
226831830b5SAl Viro {
227704b836cSOleg Nesterov 	struct mm_struct *mm;
22800f89d21SOleg Nesterov 
229704b836cSOleg Nesterov 	if (mutex_lock_killable(&task->cred_guard_mutex))
230704b836cSOleg Nesterov 		return NULL;
231704b836cSOleg Nesterov 
232704b836cSOleg Nesterov 	mm = get_task_mm(task);
233704b836cSOleg Nesterov 	if (mm && mm != current->mm &&
234704b836cSOleg Nesterov 			!ptrace_may_access(task, PTRACE_MODE_READ)) {
235831830b5SAl Viro 		mmput(mm);
23600f89d21SOleg Nesterov 		mm = NULL;
237831830b5SAl Viro 	}
238704b836cSOleg Nesterov 	mutex_unlock(&task->cred_guard_mutex);
239704b836cSOleg Nesterov 
24013f0feafSOleg Nesterov 	return mm;
24113f0feafSOleg Nesterov }
242831830b5SAl Viro 
2431da177e4SLinus Torvalds static int proc_pid_cmdline(struct task_struct *task, char * buffer)
2441da177e4SLinus Torvalds {
2451da177e4SLinus Torvalds 	int res = 0;
2461da177e4SLinus Torvalds 	unsigned int len;
2471da177e4SLinus Torvalds 	struct mm_struct *mm = get_task_mm(task);
2481da177e4SLinus Torvalds 	if (!mm)
2491da177e4SLinus Torvalds 		goto out;
2501da177e4SLinus Torvalds 	if (!mm->arg_end)
2511da177e4SLinus Torvalds 		goto out_mm;	/* Shh! No looking before we're done */
2521da177e4SLinus Torvalds 
2531da177e4SLinus Torvalds  	len = mm->arg_end - mm->arg_start;
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds 	if (len > PAGE_SIZE)
2561da177e4SLinus Torvalds 		len = PAGE_SIZE;
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds 	res = access_process_vm(task, mm->arg_start, buffer, len, 0);
2591da177e4SLinus Torvalds 
2601da177e4SLinus Torvalds 	// If the nul at the end of args has been overwritten, then
2611da177e4SLinus Torvalds 	// assume application is using setproctitle(3).
2621da177e4SLinus Torvalds 	if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
2631da177e4SLinus Torvalds 		len = strnlen(buffer, res);
2641da177e4SLinus Torvalds 		if (len < res) {
2651da177e4SLinus Torvalds 		    res = len;
2661da177e4SLinus Torvalds 		} else {
2671da177e4SLinus Torvalds 			len = mm->env_end - mm->env_start;
2681da177e4SLinus Torvalds 			if (len > PAGE_SIZE - res)
2691da177e4SLinus Torvalds 				len = PAGE_SIZE - res;
2701da177e4SLinus Torvalds 			res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
2711da177e4SLinus Torvalds 			res = strnlen(buffer, res);
2721da177e4SLinus Torvalds 		}
2731da177e4SLinus Torvalds 	}
2741da177e4SLinus Torvalds out_mm:
2751da177e4SLinus Torvalds 	mmput(mm);
2761da177e4SLinus Torvalds out:
2771da177e4SLinus Torvalds 	return res;
2781da177e4SLinus Torvalds }
2791da177e4SLinus Torvalds 
2801da177e4SLinus Torvalds static int proc_pid_auxv(struct task_struct *task, char *buffer)
2811da177e4SLinus Torvalds {
2821da177e4SLinus Torvalds 	int res = 0;
2831da177e4SLinus Torvalds 	struct mm_struct *mm = get_task_mm(task);
2841da177e4SLinus Torvalds 	if (mm) {
2851da177e4SLinus Torvalds 		unsigned int nwords = 0;
286dfe6b7d9SHannes Eder 		do {
2871da177e4SLinus Torvalds 			nwords += 2;
288dfe6b7d9SHannes Eder 		} while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
2891da177e4SLinus Torvalds 		res = nwords * sizeof(mm->saved_auxv[0]);
2901da177e4SLinus Torvalds 		if (res > PAGE_SIZE)
2911da177e4SLinus Torvalds 			res = PAGE_SIZE;
2921da177e4SLinus Torvalds 		memcpy(buffer, mm->saved_auxv, res);
2931da177e4SLinus Torvalds 		mmput(mm);
2941da177e4SLinus Torvalds 	}
2951da177e4SLinus Torvalds 	return res;
2961da177e4SLinus Torvalds }
2971da177e4SLinus Torvalds 
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds #ifdef CONFIG_KALLSYMS
3001da177e4SLinus Torvalds /*
3011da177e4SLinus Torvalds  * Provides a wchan file via kallsyms in a proper one-value-per-file format.
3021da177e4SLinus Torvalds  * Returns the resolved symbol.  If that fails, simply return the address.
3031da177e4SLinus Torvalds  */
3041da177e4SLinus Torvalds static int proc_pid_wchan(struct task_struct *task, char *buffer)
3051da177e4SLinus Torvalds {
306ffb45122SAlexey Dobriyan 	unsigned long wchan;
3079281aceaSTejun Heo 	char symname[KSYM_NAME_LEN];
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds 	wchan = get_wchan(task);
3101da177e4SLinus Torvalds 
3119d65cb4aSAlexey Dobriyan 	if (lookup_symbol_name(wchan, symname) < 0)
312f83ce3e6SJake Edge 		if (!ptrace_may_access(task, PTRACE_MODE_READ))
313f83ce3e6SJake Edge 			return 0;
314f83ce3e6SJake Edge 		else
3151da177e4SLinus Torvalds 			return sprintf(buffer, "%lu", wchan);
3169d65cb4aSAlexey Dobriyan 	else
3179d65cb4aSAlexey Dobriyan 		return sprintf(buffer, "%s", symname);
3181da177e4SLinus Torvalds }
3191da177e4SLinus Torvalds #endif /* CONFIG_KALLSYMS */
3201da177e4SLinus Torvalds 
3212ec220e2SKen Chen #ifdef CONFIG_STACKTRACE
3222ec220e2SKen Chen 
3232ec220e2SKen Chen #define MAX_STACK_TRACE_DEPTH	64
3242ec220e2SKen Chen 
3252ec220e2SKen Chen static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
3262ec220e2SKen Chen 			  struct pid *pid, struct task_struct *task)
3272ec220e2SKen Chen {
3282ec220e2SKen Chen 	struct stack_trace trace;
3292ec220e2SKen Chen 	unsigned long *entries;
3302ec220e2SKen Chen 	int i;
3312ec220e2SKen Chen 
3322ec220e2SKen Chen 	entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
3332ec220e2SKen Chen 	if (!entries)
3342ec220e2SKen Chen 		return -ENOMEM;
3352ec220e2SKen Chen 
3362ec220e2SKen Chen 	trace.nr_entries	= 0;
3372ec220e2SKen Chen 	trace.max_entries	= MAX_STACK_TRACE_DEPTH;
3382ec220e2SKen Chen 	trace.entries		= entries;
3392ec220e2SKen Chen 	trace.skip		= 0;
3402ec220e2SKen Chen 	save_stack_trace_tsk(task, &trace);
3412ec220e2SKen Chen 
3422ec220e2SKen Chen 	for (i = 0; i < trace.nr_entries; i++) {
3432ec220e2SKen Chen 		seq_printf(m, "[<%p>] %pS\n",
3442ec220e2SKen Chen 			   (void *)entries[i], (void *)entries[i]);
3452ec220e2SKen Chen 	}
3462ec220e2SKen Chen 	kfree(entries);
3472ec220e2SKen Chen 
3482ec220e2SKen Chen 	return 0;
3492ec220e2SKen Chen }
3502ec220e2SKen Chen #endif
3512ec220e2SKen Chen 
3521da177e4SLinus Torvalds #ifdef CONFIG_SCHEDSTATS
3531da177e4SLinus Torvalds /*
3541da177e4SLinus Torvalds  * Provides /proc/PID/schedstat
3551da177e4SLinus Torvalds  */
3561da177e4SLinus Torvalds static int proc_pid_schedstat(struct task_struct *task, char *buffer)
3571da177e4SLinus Torvalds {
358172ba844SBalbir Singh 	return sprintf(buffer, "%llu %llu %lu\n",
359826e08b0SIngo Molnar 			(unsigned long long)task->se.sum_exec_runtime,
360826e08b0SIngo Molnar 			(unsigned long long)task->sched_info.run_delay,
3612d72376bSIngo Molnar 			task->sched_info.pcount);
3621da177e4SLinus Torvalds }
3631da177e4SLinus Torvalds #endif
3641da177e4SLinus Torvalds 
3659745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP
3669745512cSArjan van de Ven static int lstats_show_proc(struct seq_file *m, void *v)
3679745512cSArjan van de Ven {
3689745512cSArjan van de Ven 	int i;
36913d77c37SHiroshi Shimamoto 	struct inode *inode = m->private;
37013d77c37SHiroshi Shimamoto 	struct task_struct *task = get_proc_task(inode);
3719745512cSArjan van de Ven 
37213d77c37SHiroshi Shimamoto 	if (!task)
37313d77c37SHiroshi Shimamoto 		return -ESRCH;
37413d77c37SHiroshi Shimamoto 	seq_puts(m, "Latency Top version : v0.1\n");
3759745512cSArjan van de Ven 	for (i = 0; i < 32; i++) {
3769745512cSArjan van de Ven 		if (task->latency_record[i].backtrace[0]) {
3779745512cSArjan van de Ven 			int q;
3789745512cSArjan van de Ven 			seq_printf(m, "%i %li %li ",
3799745512cSArjan van de Ven 				task->latency_record[i].count,
3809745512cSArjan van de Ven 				task->latency_record[i].time,
3819745512cSArjan van de Ven 				task->latency_record[i].max);
3829745512cSArjan van de Ven 			for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
3839c246247SHugh Dickins 				char sym[KSYM_SYMBOL_LEN];
3849745512cSArjan van de Ven 				char *c;
3859745512cSArjan van de Ven 				if (!task->latency_record[i].backtrace[q])
3869745512cSArjan van de Ven 					break;
3879745512cSArjan van de Ven 				if (task->latency_record[i].backtrace[q] == ULONG_MAX)
3889745512cSArjan van de Ven 					break;
3899745512cSArjan van de Ven 				sprint_symbol(sym, task->latency_record[i].backtrace[q]);
3909745512cSArjan van de Ven 				c = strchr(sym, '+');
3919745512cSArjan van de Ven 				if (c)
3929745512cSArjan van de Ven 					*c = 0;
3939745512cSArjan van de Ven 				seq_printf(m, "%s ", sym);
3949745512cSArjan van de Ven 			}
3959745512cSArjan van de Ven 			seq_printf(m, "\n");
3969745512cSArjan van de Ven 		}
3979745512cSArjan van de Ven 
3989745512cSArjan van de Ven 	}
39913d77c37SHiroshi Shimamoto 	put_task_struct(task);
4009745512cSArjan van de Ven 	return 0;
4019745512cSArjan van de Ven }
4029745512cSArjan van de Ven 
4039745512cSArjan van de Ven static int lstats_open(struct inode *inode, struct file *file)
4049745512cSArjan van de Ven {
40513d77c37SHiroshi Shimamoto 	return single_open(file, lstats_show_proc, inode);
406d6643d12SHiroshi Shimamoto }
407d6643d12SHiroshi Shimamoto 
4089745512cSArjan van de Ven static ssize_t lstats_write(struct file *file, const char __user *buf,
4099745512cSArjan van de Ven 			    size_t count, loff_t *offs)
4109745512cSArjan van de Ven {
41113d77c37SHiroshi Shimamoto 	struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
4129745512cSArjan van de Ven 
41313d77c37SHiroshi Shimamoto 	if (!task)
41413d77c37SHiroshi Shimamoto 		return -ESRCH;
4159745512cSArjan van de Ven 	clear_all_latency_tracing(task);
41613d77c37SHiroshi Shimamoto 	put_task_struct(task);
4179745512cSArjan van de Ven 
4189745512cSArjan van de Ven 	return count;
4199745512cSArjan van de Ven }
4209745512cSArjan van de Ven 
4219745512cSArjan van de Ven static const struct file_operations proc_lstats_operations = {
4229745512cSArjan van de Ven 	.open		= lstats_open,
4239745512cSArjan van de Ven 	.read		= seq_read,
4249745512cSArjan van de Ven 	.write		= lstats_write,
4259745512cSArjan van de Ven 	.llseek		= seq_lseek,
42613d77c37SHiroshi Shimamoto 	.release	= single_release,
4279745512cSArjan van de Ven };
4289745512cSArjan van de Ven 
4299745512cSArjan van de Ven #endif
4309745512cSArjan van de Ven 
4311da177e4SLinus Torvalds static int proc_oom_score(struct task_struct *task, char *buffer)
4321da177e4SLinus Torvalds {
433b95c35e7SOleg Nesterov 	unsigned long points = 0;
4341da177e4SLinus Torvalds 
43519c5d45aSAlexey Dobriyan 	read_lock(&tasklist_lock);
436b95c35e7SOleg Nesterov 	if (pid_alive(task))
437a63d83f4SDavid Rientjes 		points = oom_badness(task, NULL, NULL,
438a63d83f4SDavid Rientjes 					totalram_pages + total_swap_pages);
43919c5d45aSAlexey Dobriyan 	read_unlock(&tasklist_lock);
4401da177e4SLinus Torvalds 	return sprintf(buffer, "%lu\n", points);
4411da177e4SLinus Torvalds }
4421da177e4SLinus Torvalds 
443d85f50d5SNeil Horman struct limit_names {
444d85f50d5SNeil Horman 	char *name;
445d85f50d5SNeil Horman 	char *unit;
446d85f50d5SNeil Horman };
447d85f50d5SNeil Horman 
448d85f50d5SNeil Horman static const struct limit_names lnames[RLIM_NLIMITS] = {
449cff4edb5SKees Cook 	[RLIMIT_CPU] = {"Max cpu time", "seconds"},
450d85f50d5SNeil Horman 	[RLIMIT_FSIZE] = {"Max file size", "bytes"},
451d85f50d5SNeil Horman 	[RLIMIT_DATA] = {"Max data size", "bytes"},
452d85f50d5SNeil Horman 	[RLIMIT_STACK] = {"Max stack size", "bytes"},
453d85f50d5SNeil Horman 	[RLIMIT_CORE] = {"Max core file size", "bytes"},
454d85f50d5SNeil Horman 	[RLIMIT_RSS] = {"Max resident set", "bytes"},
455d85f50d5SNeil Horman 	[RLIMIT_NPROC] = {"Max processes", "processes"},
456d85f50d5SNeil Horman 	[RLIMIT_NOFILE] = {"Max open files", "files"},
457d85f50d5SNeil Horman 	[RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
458d85f50d5SNeil Horman 	[RLIMIT_AS] = {"Max address space", "bytes"},
459d85f50d5SNeil Horman 	[RLIMIT_LOCKS] = {"Max file locks", "locks"},
460d85f50d5SNeil Horman 	[RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
461d85f50d5SNeil Horman 	[RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
462d85f50d5SNeil Horman 	[RLIMIT_NICE] = {"Max nice priority", NULL},
463d85f50d5SNeil Horman 	[RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
4648808117cSEugene Teo 	[RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
465d85f50d5SNeil Horman };
466d85f50d5SNeil Horman 
467d85f50d5SNeil Horman /* Display limits for a process */
468d85f50d5SNeil Horman static int proc_pid_limits(struct task_struct *task, char *buffer)
469d85f50d5SNeil Horman {
470d85f50d5SNeil Horman 	unsigned int i;
471d85f50d5SNeil Horman 	int count = 0;
472d85f50d5SNeil Horman 	unsigned long flags;
473d85f50d5SNeil Horman 	char *bufptr = buffer;
474d85f50d5SNeil Horman 
475d85f50d5SNeil Horman 	struct rlimit rlim[RLIM_NLIMITS];
476d85f50d5SNeil Horman 
477a6bebbc8SLai Jiangshan 	if (!lock_task_sighand(task, &flags))
478d85f50d5SNeil Horman 		return 0;
479d85f50d5SNeil Horman 	memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
480d85f50d5SNeil Horman 	unlock_task_sighand(task, &flags);
481d85f50d5SNeil Horman 
482d85f50d5SNeil Horman 	/*
483d85f50d5SNeil Horman 	 * print the file header
484d85f50d5SNeil Horman 	 */
485d85f50d5SNeil Horman 	count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
486d85f50d5SNeil Horman 			"Limit", "Soft Limit", "Hard Limit", "Units");
487d85f50d5SNeil Horman 
488d85f50d5SNeil Horman 	for (i = 0; i < RLIM_NLIMITS; i++) {
489d85f50d5SNeil Horman 		if (rlim[i].rlim_cur == RLIM_INFINITY)
490d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-25s %-20s ",
491d85f50d5SNeil Horman 					 lnames[i].name, "unlimited");
492d85f50d5SNeil Horman 		else
493d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-25s %-20lu ",
494d85f50d5SNeil Horman 					 lnames[i].name, rlim[i].rlim_cur);
495d85f50d5SNeil Horman 
496d85f50d5SNeil Horman 		if (rlim[i].rlim_max == RLIM_INFINITY)
497d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-20s ", "unlimited");
498d85f50d5SNeil Horman 		else
499d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-20lu ",
500d85f50d5SNeil Horman 					 rlim[i].rlim_max);
501d85f50d5SNeil Horman 
502d85f50d5SNeil Horman 		if (lnames[i].unit)
503d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-10s\n",
504d85f50d5SNeil Horman 					 lnames[i].unit);
505d85f50d5SNeil Horman 		else
506d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "\n");
507d85f50d5SNeil Horman 	}
508d85f50d5SNeil Horman 
509d85f50d5SNeil Horman 	return count;
510d85f50d5SNeil Horman }
511d85f50d5SNeil Horman 
512ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
513ebcb6734SRoland McGrath static int proc_pid_syscall(struct task_struct *task, char *buffer)
514ebcb6734SRoland McGrath {
515ebcb6734SRoland McGrath 	long nr;
516ebcb6734SRoland McGrath 	unsigned long args[6], sp, pc;
517ebcb6734SRoland McGrath 
518ebcb6734SRoland McGrath 	if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
519ebcb6734SRoland McGrath 		return sprintf(buffer, "running\n");
520ebcb6734SRoland McGrath 
521ebcb6734SRoland McGrath 	if (nr < 0)
522ebcb6734SRoland McGrath 		return sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
523ebcb6734SRoland McGrath 
524ebcb6734SRoland McGrath 	return sprintf(buffer,
525ebcb6734SRoland McGrath 		       "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
526ebcb6734SRoland McGrath 		       nr,
527ebcb6734SRoland McGrath 		       args[0], args[1], args[2], args[3], args[4], args[5],
528ebcb6734SRoland McGrath 		       sp, pc);
529ebcb6734SRoland McGrath }
530ebcb6734SRoland McGrath #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
531ebcb6734SRoland McGrath 
5321da177e4SLinus Torvalds /************************************************************************/
5331da177e4SLinus Torvalds /*                       Here the fs part begins                        */
5341da177e4SLinus Torvalds /************************************************************************/
5351da177e4SLinus Torvalds 
5361da177e4SLinus Torvalds /* permission checks */
537778c1144SEric W. Biederman static int proc_fd_access_allowed(struct inode *inode)
5381da177e4SLinus Torvalds {
539778c1144SEric W. Biederman 	struct task_struct *task;
540778c1144SEric W. Biederman 	int allowed = 0;
541df26c40eSEric W. Biederman 	/* Allow access to a task's file descriptors if it is us or we
542df26c40eSEric W. Biederman 	 * may use ptrace attach to the process and find out that
543df26c40eSEric W. Biederman 	 * information.
544778c1144SEric W. Biederman 	 */
545778c1144SEric W. Biederman 	task = get_proc_task(inode);
546df26c40eSEric W. Biederman 	if (task) {
547006ebb40SStephen Smalley 		allowed = ptrace_may_access(task, PTRACE_MODE_READ);
548778c1144SEric W. Biederman 		put_task_struct(task);
549df26c40eSEric W. Biederman 	}
550778c1144SEric W. Biederman 	return allowed;
5511da177e4SLinus Torvalds }
5521da177e4SLinus Torvalds 
5536d76fa58SLinus Torvalds static int proc_setattr(struct dentry *dentry, struct iattr *attr)
5546d76fa58SLinus Torvalds {
5556d76fa58SLinus Torvalds 	int error;
5566d76fa58SLinus Torvalds 	struct inode *inode = dentry->d_inode;
5576d76fa58SLinus Torvalds 
5586d76fa58SLinus Torvalds 	if (attr->ia_valid & ATTR_MODE)
5596d76fa58SLinus Torvalds 		return -EPERM;
5606d76fa58SLinus Torvalds 
5616d76fa58SLinus Torvalds 	error = inode_change_ok(inode, attr);
5621025774cSChristoph Hellwig 	if (error)
5636d76fa58SLinus Torvalds 		return error;
5641025774cSChristoph Hellwig 
5651025774cSChristoph Hellwig 	if ((attr->ia_valid & ATTR_SIZE) &&
5661025774cSChristoph Hellwig 	    attr->ia_size != i_size_read(inode)) {
5671025774cSChristoph Hellwig 		error = vmtruncate(inode, attr->ia_size);
5681025774cSChristoph Hellwig 		if (error)
5691025774cSChristoph Hellwig 			return error;
5701025774cSChristoph Hellwig 	}
5711025774cSChristoph Hellwig 
5721025774cSChristoph Hellwig 	setattr_copy(inode, attr);
5731025774cSChristoph Hellwig 	mark_inode_dirty(inode);
5741025774cSChristoph Hellwig 	return 0;
5756d76fa58SLinus Torvalds }
5766d76fa58SLinus Torvalds 
577c5ef1c42SArjan van de Ven static const struct inode_operations proc_def_inode_operations = {
5786d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
5796d76fa58SLinus Torvalds };
5806d76fa58SLinus Torvalds 
581a1a2c409SMiklos Szeredi static int mounts_open_common(struct inode *inode, struct file *file,
582a1a2c409SMiklos Szeredi 			      const struct seq_operations *op)
5831da177e4SLinus Torvalds {
58499f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
585cf7b708cSPavel Emelyanov 	struct nsproxy *nsp;
5866b3286edSKirill Korotaev 	struct mnt_namespace *ns = NULL;
587a1a2c409SMiklos Szeredi 	struct path root;
5885addc5ddSAl Viro 	struct proc_mounts *p;
5895addc5ddSAl Viro 	int ret = -EINVAL;
5905addc5ddSAl Viro 
59199f89551SEric W. Biederman 	if (task) {
592cf7b708cSPavel Emelyanov 		rcu_read_lock();
593cf7b708cSPavel Emelyanov 		nsp = task_nsproxy(task);
594cf7b708cSPavel Emelyanov 		if (nsp) {
595cf7b708cSPavel Emelyanov 			ns = nsp->mnt_ns;
5966b3286edSKirill Korotaev 			if (ns)
5976b3286edSKirill Korotaev 				get_mnt_ns(ns);
598863c4702SAlexey Dobriyan 		}
599cf7b708cSPavel Emelyanov 		rcu_read_unlock();
600f7ad3c6bSMiklos Szeredi 		if (ns && get_task_root(task, &root) == 0)
6017c2c7d99SHugh Dickins 			ret = 0;
60299f89551SEric W. Biederman 		put_task_struct(task);
60399f89551SEric W. Biederman 	}
6041da177e4SLinus Torvalds 
605a1a2c409SMiklos Szeredi 	if (!ns)
606a1a2c409SMiklos Szeredi 		goto err;
6077c2c7d99SHugh Dickins 	if (ret)
608a1a2c409SMiklos Szeredi 		goto err_put_ns;
609a1a2c409SMiklos Szeredi 
6105addc5ddSAl Viro 	ret = -ENOMEM;
6115addc5ddSAl Viro 	p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
612a1a2c409SMiklos Szeredi 	if (!p)
613a1a2c409SMiklos Szeredi 		goto err_put_path;
614a1a2c409SMiklos Szeredi 
6155addc5ddSAl Viro 	file->private_data = &p->m;
616a1a2c409SMiklos Szeredi 	ret = seq_open(file, op);
617a1a2c409SMiklos Szeredi 	if (ret)
618a1a2c409SMiklos Szeredi 		goto err_free;
619a1a2c409SMiklos Szeredi 
620a1a2c409SMiklos Szeredi 	p->m.private = p;
621a1a2c409SMiklos Szeredi 	p->ns = ns;
622a1a2c409SMiklos Szeredi 	p->root = root;
6236b3286edSKirill Korotaev 	p->event = ns->event;
624a1a2c409SMiklos Szeredi 
6255addc5ddSAl Viro 	return 0;
626a1a2c409SMiklos Szeredi 
627a1a2c409SMiklos Szeredi  err_free:
6285addc5ddSAl Viro 	kfree(p);
629a1a2c409SMiklos Szeredi  err_put_path:
630a1a2c409SMiklos Szeredi 	path_put(&root);
631a1a2c409SMiklos Szeredi  err_put_ns:
6326b3286edSKirill Korotaev 	put_mnt_ns(ns);
633a1a2c409SMiklos Szeredi  err:
6341da177e4SLinus Torvalds 	return ret;
6351da177e4SLinus Torvalds }
6361da177e4SLinus Torvalds 
6371da177e4SLinus Torvalds static int mounts_release(struct inode *inode, struct file *file)
6381da177e4SLinus Torvalds {
639a1a2c409SMiklos Szeredi 	struct proc_mounts *p = file->private_data;
640a1a2c409SMiklos Szeredi 	path_put(&p->root);
641a1a2c409SMiklos Szeredi 	put_mnt_ns(p->ns);
6421da177e4SLinus Torvalds 	return seq_release(inode, file);
6431da177e4SLinus Torvalds }
6441da177e4SLinus Torvalds 
6455addc5ddSAl Viro static unsigned mounts_poll(struct file *file, poll_table *wait)
6465addc5ddSAl Viro {
6475addc5ddSAl Viro 	struct proc_mounts *p = file->private_data;
64831b07093SKOSAKI Motohiro 	unsigned res = POLLIN | POLLRDNORM;
6495addc5ddSAl Viro 
6509f5596afSAl Viro 	poll_wait(file, &p->ns->poll, wait);
6519f5596afSAl Viro 	if (mnt_had_events(p))
65231b07093SKOSAKI Motohiro 		res |= POLLERR | POLLPRI;
6535addc5ddSAl Viro 
6545addc5ddSAl Viro 	return res;
6555addc5ddSAl Viro }
6565addc5ddSAl Viro 
657a1a2c409SMiklos Szeredi static int mounts_open(struct inode *inode, struct file *file)
658a1a2c409SMiklos Szeredi {
659a1a2c409SMiklos Szeredi 	return mounts_open_common(inode, file, &mounts_op);
660a1a2c409SMiklos Szeredi }
661a1a2c409SMiklos Szeredi 
66200977a59SArjan van de Ven static const struct file_operations proc_mounts_operations = {
6631da177e4SLinus Torvalds 	.open		= mounts_open,
6641da177e4SLinus Torvalds 	.read		= seq_read,
6651da177e4SLinus Torvalds 	.llseek		= seq_lseek,
6661da177e4SLinus Torvalds 	.release	= mounts_release,
6675addc5ddSAl Viro 	.poll		= mounts_poll,
6681da177e4SLinus Torvalds };
6691da177e4SLinus Torvalds 
6702d4d4864SRam Pai static int mountinfo_open(struct inode *inode, struct file *file)
6712d4d4864SRam Pai {
6722d4d4864SRam Pai 	return mounts_open_common(inode, file, &mountinfo_op);
6732d4d4864SRam Pai }
6742d4d4864SRam Pai 
6752d4d4864SRam Pai static const struct file_operations proc_mountinfo_operations = {
6762d4d4864SRam Pai 	.open		= mountinfo_open,
6772d4d4864SRam Pai 	.read		= seq_read,
6782d4d4864SRam Pai 	.llseek		= seq_lseek,
6792d4d4864SRam Pai 	.release	= mounts_release,
6802d4d4864SRam Pai 	.poll		= mounts_poll,
6812d4d4864SRam Pai };
6822d4d4864SRam Pai 
683b4629fe2SChuck Lever static int mountstats_open(struct inode *inode, struct file *file)
684b4629fe2SChuck Lever {
685a1a2c409SMiklos Szeredi 	return mounts_open_common(inode, file, &mountstats_op);
686b4629fe2SChuck Lever }
687b4629fe2SChuck Lever 
68800977a59SArjan van de Ven static const struct file_operations proc_mountstats_operations = {
689b4629fe2SChuck Lever 	.open		= mountstats_open,
690b4629fe2SChuck Lever 	.read		= seq_read,
691b4629fe2SChuck Lever 	.llseek		= seq_lseek,
692b4629fe2SChuck Lever 	.release	= mounts_release,
693b4629fe2SChuck Lever };
694b4629fe2SChuck Lever 
6951da177e4SLinus Torvalds #define PROC_BLOCK_SIZE	(3*1024)		/* 4K page size but our output routines use some slack for overruns */
6961da177e4SLinus Torvalds 
6971da177e4SLinus Torvalds static ssize_t proc_info_read(struct file * file, char __user * buf,
6981da177e4SLinus Torvalds 			  size_t count, loff_t *ppos)
6991da177e4SLinus Torvalds {
7002fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
7011da177e4SLinus Torvalds 	unsigned long page;
7021da177e4SLinus Torvalds 	ssize_t length;
70399f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
70499f89551SEric W. Biederman 
70599f89551SEric W. Biederman 	length = -ESRCH;
70699f89551SEric W. Biederman 	if (!task)
70799f89551SEric W. Biederman 		goto out_no_task;
7081da177e4SLinus Torvalds 
7091da177e4SLinus Torvalds 	if (count > PROC_BLOCK_SIZE)
7101da177e4SLinus Torvalds 		count = PROC_BLOCK_SIZE;
71199f89551SEric W. Biederman 
71299f89551SEric W. Biederman 	length = -ENOMEM;
713e12ba74dSMel Gorman 	if (!(page = __get_free_page(GFP_TEMPORARY)))
71499f89551SEric W. Biederman 		goto out;
7151da177e4SLinus Torvalds 
7161da177e4SLinus Torvalds 	length = PROC_I(inode)->op.proc_read(task, (char*)page);
7171da177e4SLinus Torvalds 
7181da177e4SLinus Torvalds 	if (length >= 0)
7191da177e4SLinus Torvalds 		length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
7201da177e4SLinus Torvalds 	free_page(page);
72199f89551SEric W. Biederman out:
72299f89551SEric W. Biederman 	put_task_struct(task);
72399f89551SEric W. Biederman out_no_task:
7241da177e4SLinus Torvalds 	return length;
7251da177e4SLinus Torvalds }
7261da177e4SLinus Torvalds 
72700977a59SArjan van de Ven static const struct file_operations proc_info_file_operations = {
7281da177e4SLinus Torvalds 	.read		= proc_info_read,
72987df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
7301da177e4SLinus Torvalds };
7311da177e4SLinus Torvalds 
732be614086SEric W. Biederman static int proc_single_show(struct seq_file *m, void *v)
733be614086SEric W. Biederman {
734be614086SEric W. Biederman 	struct inode *inode = m->private;
735be614086SEric W. Biederman 	struct pid_namespace *ns;
736be614086SEric W. Biederman 	struct pid *pid;
737be614086SEric W. Biederman 	struct task_struct *task;
738be614086SEric W. Biederman 	int ret;
739be614086SEric W. Biederman 
740be614086SEric W. Biederman 	ns = inode->i_sb->s_fs_info;
741be614086SEric W. Biederman 	pid = proc_pid(inode);
742be614086SEric W. Biederman 	task = get_pid_task(pid, PIDTYPE_PID);
743be614086SEric W. Biederman 	if (!task)
744be614086SEric W. Biederman 		return -ESRCH;
745be614086SEric W. Biederman 
746be614086SEric W. Biederman 	ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
747be614086SEric W. Biederman 
748be614086SEric W. Biederman 	put_task_struct(task);
749be614086SEric W. Biederman 	return ret;
750be614086SEric W. Biederman }
751be614086SEric W. Biederman 
752be614086SEric W. Biederman static int proc_single_open(struct inode *inode, struct file *filp)
753be614086SEric W. Biederman {
754be614086SEric W. Biederman 	int ret;
755be614086SEric W. Biederman 	ret = single_open(filp, proc_single_show, NULL);
756be614086SEric W. Biederman 	if (!ret) {
757be614086SEric W. Biederman 		struct seq_file *m = filp->private_data;
758be614086SEric W. Biederman 
759be614086SEric W. Biederman 		m->private = inode;
760be614086SEric W. Biederman 	}
761be614086SEric W. Biederman 	return ret;
762be614086SEric W. Biederman }
763be614086SEric W. Biederman 
764be614086SEric W. Biederman static const struct file_operations proc_single_file_operations = {
765be614086SEric W. Biederman 	.open		= proc_single_open,
766be614086SEric W. Biederman 	.read		= seq_read,
767be614086SEric W. Biederman 	.llseek		= seq_lseek,
768be614086SEric W. Biederman 	.release	= single_release,
769be614086SEric W. Biederman };
770be614086SEric W. Biederman 
7711da177e4SLinus Torvalds static int mem_open(struct inode* inode, struct file* file)
7721da177e4SLinus Torvalds {
7731da177e4SLinus Torvalds 	file->private_data = (void*)((long)current->self_exec_id);
7741da177e4SLinus Torvalds 	return 0;
7751da177e4SLinus Torvalds }
7761da177e4SLinus Torvalds 
7771da177e4SLinus Torvalds static ssize_t mem_read(struct file * file, char __user * buf,
7781da177e4SLinus Torvalds 			size_t count, loff_t *ppos)
7791da177e4SLinus Torvalds {
7802fddfeefSJosef "Jeff" Sipek 	struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
7811da177e4SLinus Torvalds 	char *page;
7821da177e4SLinus Torvalds 	unsigned long src = *ppos;
7831da177e4SLinus Torvalds 	int ret = -ESRCH;
7841da177e4SLinus Torvalds 	struct mm_struct *mm;
7851da177e4SLinus Torvalds 
78699f89551SEric W. Biederman 	if (!task)
78799f89551SEric W. Biederman 		goto out_no_task;
78899f89551SEric W. Biederman 
789638fa202SRoland McGrath 	if (check_mem_permission(task))
7901da177e4SLinus Torvalds 		goto out;
7911da177e4SLinus Torvalds 
7921da177e4SLinus Torvalds 	ret = -ENOMEM;
793e12ba74dSMel Gorman 	page = (char *)__get_free_page(GFP_TEMPORARY);
7941da177e4SLinus Torvalds 	if (!page)
7951da177e4SLinus Torvalds 		goto out;
7961da177e4SLinus Torvalds 
7971da177e4SLinus Torvalds 	ret = 0;
7981da177e4SLinus Torvalds 
7991da177e4SLinus Torvalds 	mm = get_task_mm(task);
8001da177e4SLinus Torvalds 	if (!mm)
8011da177e4SLinus Torvalds 		goto out_free;
8021da177e4SLinus Torvalds 
8031da177e4SLinus Torvalds 	ret = -EIO;
8041da177e4SLinus Torvalds 
8051da177e4SLinus Torvalds 	if (file->private_data != (void*)((long)current->self_exec_id))
8061da177e4SLinus Torvalds 		goto out_put;
8071da177e4SLinus Torvalds 
8081da177e4SLinus Torvalds 	ret = 0;
8091da177e4SLinus Torvalds 
8101da177e4SLinus Torvalds 	while (count > 0) {
8111da177e4SLinus Torvalds 		int this_len, retval;
8121da177e4SLinus Torvalds 
8131da177e4SLinus Torvalds 		this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
8141da177e4SLinus Torvalds 		retval = access_process_vm(task, src, page, this_len, 0);
815638fa202SRoland McGrath 		if (!retval || check_mem_permission(task)) {
8161da177e4SLinus Torvalds 			if (!ret)
8171da177e4SLinus Torvalds 				ret = -EIO;
8181da177e4SLinus Torvalds 			break;
8191da177e4SLinus Torvalds 		}
8201da177e4SLinus Torvalds 
8211da177e4SLinus Torvalds 		if (copy_to_user(buf, page, retval)) {
8221da177e4SLinus Torvalds 			ret = -EFAULT;
8231da177e4SLinus Torvalds 			break;
8241da177e4SLinus Torvalds 		}
8251da177e4SLinus Torvalds 
8261da177e4SLinus Torvalds 		ret += retval;
8271da177e4SLinus Torvalds 		src += retval;
8281da177e4SLinus Torvalds 		buf += retval;
8291da177e4SLinus Torvalds 		count -= retval;
8301da177e4SLinus Torvalds 	}
8311da177e4SLinus Torvalds 	*ppos = src;
8321da177e4SLinus Torvalds 
8331da177e4SLinus Torvalds out_put:
8341da177e4SLinus Torvalds 	mmput(mm);
8351da177e4SLinus Torvalds out_free:
8361da177e4SLinus Torvalds 	free_page((unsigned long) page);
8371da177e4SLinus Torvalds out:
83899f89551SEric W. Biederman 	put_task_struct(task);
83999f89551SEric W. Biederman out_no_task:
8401da177e4SLinus Torvalds 	return ret;
8411da177e4SLinus Torvalds }
8421da177e4SLinus Torvalds 
8431da177e4SLinus Torvalds #define mem_write NULL
8441da177e4SLinus Torvalds 
8451da177e4SLinus Torvalds #ifndef mem_write
8461da177e4SLinus Torvalds /* This is a security hazard */
84763967fa9SGlauber de Oliveira Costa static ssize_t mem_write(struct file * file, const char __user *buf,
8481da177e4SLinus Torvalds 			 size_t count, loff_t *ppos)
8491da177e4SLinus Torvalds {
850f7ca54f4SFrederik Deweerdt 	int copied;
8511da177e4SLinus Torvalds 	char *page;
8522fddfeefSJosef "Jeff" Sipek 	struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
8531da177e4SLinus Torvalds 	unsigned long dst = *ppos;
8541da177e4SLinus Torvalds 
85599f89551SEric W. Biederman 	copied = -ESRCH;
85699f89551SEric W. Biederman 	if (!task)
85799f89551SEric W. Biederman 		goto out_no_task;
8581da177e4SLinus Torvalds 
859638fa202SRoland McGrath 	if (check_mem_permission(task))
86099f89551SEric W. Biederman 		goto out;
86199f89551SEric W. Biederman 
86299f89551SEric W. Biederman 	copied = -ENOMEM;
863e12ba74dSMel Gorman 	page = (char *)__get_free_page(GFP_TEMPORARY);
8641da177e4SLinus Torvalds 	if (!page)
86599f89551SEric W. Biederman 		goto out;
8661da177e4SLinus Torvalds 
867f7ca54f4SFrederik Deweerdt 	copied = 0;
8681da177e4SLinus Torvalds 	while (count > 0) {
8691da177e4SLinus Torvalds 		int this_len, retval;
8701da177e4SLinus Torvalds 
8711da177e4SLinus Torvalds 		this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
8721da177e4SLinus Torvalds 		if (copy_from_user(page, buf, this_len)) {
8731da177e4SLinus Torvalds 			copied = -EFAULT;
8741da177e4SLinus Torvalds 			break;
8751da177e4SLinus Torvalds 		}
8761da177e4SLinus Torvalds 		retval = access_process_vm(task, dst, page, this_len, 1);
8771da177e4SLinus Torvalds 		if (!retval) {
8781da177e4SLinus Torvalds 			if (!copied)
8791da177e4SLinus Torvalds 				copied = -EIO;
8801da177e4SLinus Torvalds 			break;
8811da177e4SLinus Torvalds 		}
8821da177e4SLinus Torvalds 		copied += retval;
8831da177e4SLinus Torvalds 		buf += retval;
8841da177e4SLinus Torvalds 		dst += retval;
8851da177e4SLinus Torvalds 		count -= retval;
8861da177e4SLinus Torvalds 	}
8871da177e4SLinus Torvalds 	*ppos = dst;
8881da177e4SLinus Torvalds 	free_page((unsigned long) page);
88999f89551SEric W. Biederman out:
89099f89551SEric W. Biederman 	put_task_struct(task);
89199f89551SEric W. Biederman out_no_task:
8921da177e4SLinus Torvalds 	return copied;
8931da177e4SLinus Torvalds }
8941da177e4SLinus Torvalds #endif
8951da177e4SLinus Torvalds 
89685863e47SMatt Mackall loff_t mem_lseek(struct file *file, loff_t offset, int orig)
8971da177e4SLinus Torvalds {
8981da177e4SLinus Torvalds 	switch (orig) {
8991da177e4SLinus Torvalds 	case 0:
9001da177e4SLinus Torvalds 		file->f_pos = offset;
9011da177e4SLinus Torvalds 		break;
9021da177e4SLinus Torvalds 	case 1:
9031da177e4SLinus Torvalds 		file->f_pos += offset;
9041da177e4SLinus Torvalds 		break;
9051da177e4SLinus Torvalds 	default:
9061da177e4SLinus Torvalds 		return -EINVAL;
9071da177e4SLinus Torvalds 	}
9081da177e4SLinus Torvalds 	force_successful_syscall_return();
9091da177e4SLinus Torvalds 	return file->f_pos;
9101da177e4SLinus Torvalds }
9111da177e4SLinus Torvalds 
91200977a59SArjan van de Ven static const struct file_operations proc_mem_operations = {
9131da177e4SLinus Torvalds 	.llseek		= mem_lseek,
9141da177e4SLinus Torvalds 	.read		= mem_read,
9151da177e4SLinus Torvalds 	.write		= mem_write,
9161da177e4SLinus Torvalds 	.open		= mem_open,
9171da177e4SLinus Torvalds };
9181da177e4SLinus Torvalds 
919315e28c8SJames Pearson static ssize_t environ_read(struct file *file, char __user *buf,
920315e28c8SJames Pearson 			size_t count, loff_t *ppos)
921315e28c8SJames Pearson {
922315e28c8SJames Pearson 	struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
923315e28c8SJames Pearson 	char *page;
924315e28c8SJames Pearson 	unsigned long src = *ppos;
925315e28c8SJames Pearson 	int ret = -ESRCH;
926315e28c8SJames Pearson 	struct mm_struct *mm;
927315e28c8SJames Pearson 
928315e28c8SJames Pearson 	if (!task)
929315e28c8SJames Pearson 		goto out_no_task;
930315e28c8SJames Pearson 
931006ebb40SStephen Smalley 	if (!ptrace_may_access(task, PTRACE_MODE_READ))
932315e28c8SJames Pearson 		goto out;
933315e28c8SJames Pearson 
934315e28c8SJames Pearson 	ret = -ENOMEM;
935315e28c8SJames Pearson 	page = (char *)__get_free_page(GFP_TEMPORARY);
936315e28c8SJames Pearson 	if (!page)
937315e28c8SJames Pearson 		goto out;
938315e28c8SJames Pearson 
939315e28c8SJames Pearson 	ret = 0;
940315e28c8SJames Pearson 
941315e28c8SJames Pearson 	mm = get_task_mm(task);
942315e28c8SJames Pearson 	if (!mm)
943315e28c8SJames Pearson 		goto out_free;
944315e28c8SJames Pearson 
945315e28c8SJames Pearson 	while (count > 0) {
946315e28c8SJames Pearson 		int this_len, retval, max_len;
947315e28c8SJames Pearson 
948315e28c8SJames Pearson 		this_len = mm->env_end - (mm->env_start + src);
949315e28c8SJames Pearson 
950315e28c8SJames Pearson 		if (this_len <= 0)
951315e28c8SJames Pearson 			break;
952315e28c8SJames Pearson 
953315e28c8SJames Pearson 		max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
954315e28c8SJames Pearson 		this_len = (this_len > max_len) ? max_len : this_len;
955315e28c8SJames Pearson 
956315e28c8SJames Pearson 		retval = access_process_vm(task, (mm->env_start + src),
957315e28c8SJames Pearson 			page, this_len, 0);
958315e28c8SJames Pearson 
959315e28c8SJames Pearson 		if (retval <= 0) {
960315e28c8SJames Pearson 			ret = retval;
961315e28c8SJames Pearson 			break;
962315e28c8SJames Pearson 		}
963315e28c8SJames Pearson 
964315e28c8SJames Pearson 		if (copy_to_user(buf, page, retval)) {
965315e28c8SJames Pearson 			ret = -EFAULT;
966315e28c8SJames Pearson 			break;
967315e28c8SJames Pearson 		}
968315e28c8SJames Pearson 
969315e28c8SJames Pearson 		ret += retval;
970315e28c8SJames Pearson 		src += retval;
971315e28c8SJames Pearson 		buf += retval;
972315e28c8SJames Pearson 		count -= retval;
973315e28c8SJames Pearson 	}
974315e28c8SJames Pearson 	*ppos = src;
975315e28c8SJames Pearson 
976315e28c8SJames Pearson 	mmput(mm);
977315e28c8SJames Pearson out_free:
978315e28c8SJames Pearson 	free_page((unsigned long) page);
979315e28c8SJames Pearson out:
980315e28c8SJames Pearson 	put_task_struct(task);
981315e28c8SJames Pearson out_no_task:
982315e28c8SJames Pearson 	return ret;
983315e28c8SJames Pearson }
984315e28c8SJames Pearson 
985315e28c8SJames Pearson static const struct file_operations proc_environ_operations = {
986315e28c8SJames Pearson 	.read		= environ_read,
98787df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
988315e28c8SJames Pearson };
989315e28c8SJames Pearson 
9901da177e4SLinus Torvalds static ssize_t oom_adjust_read(struct file *file, char __user *buf,
9911da177e4SLinus Torvalds 				size_t count, loff_t *ppos)
9921da177e4SLinus Torvalds {
9932fddfeefSJosef "Jeff" Sipek 	struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
9948578cea7SEric W. Biederman 	char buffer[PROC_NUMBUF];
9951da177e4SLinus Torvalds 	size_t len;
99628b83c51SKOSAKI Motohiro 	int oom_adjust = OOM_DISABLE;
99728b83c51SKOSAKI Motohiro 	unsigned long flags;
9981da177e4SLinus Torvalds 
99999f89551SEric W. Biederman 	if (!task)
100099f89551SEric W. Biederman 		return -ESRCH;
100128b83c51SKOSAKI Motohiro 
100228b83c51SKOSAKI Motohiro 	if (lock_task_sighand(task, &flags)) {
100328b83c51SKOSAKI Motohiro 		oom_adjust = task->signal->oom_adj;
100428b83c51SKOSAKI Motohiro 		unlock_task_sighand(task, &flags);
100528b83c51SKOSAKI Motohiro 	}
100628b83c51SKOSAKI Motohiro 
100799f89551SEric W. Biederman 	put_task_struct(task);
100899f89551SEric W. Biederman 
10098578cea7SEric W. Biederman 	len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
10100c28f287SAkinobu Mita 
10110c28f287SAkinobu Mita 	return simple_read_from_buffer(buf, count, ppos, buffer, len);
10121da177e4SLinus Torvalds }
10131da177e4SLinus Torvalds 
10141da177e4SLinus Torvalds static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
10151da177e4SLinus Torvalds 				size_t count, loff_t *ppos)
10161da177e4SLinus Torvalds {
101799f89551SEric W. Biederman 	struct task_struct *task;
10185d863b89SKOSAKI Motohiro 	char buffer[PROC_NUMBUF];
10195d863b89SKOSAKI Motohiro 	long oom_adjust;
102028b83c51SKOSAKI Motohiro 	unsigned long flags;
10215d863b89SKOSAKI Motohiro 	int err;
10221da177e4SLinus Torvalds 
10238578cea7SEric W. Biederman 	memset(buffer, 0, sizeof(buffer));
10248578cea7SEric W. Biederman 	if (count > sizeof(buffer) - 1)
10258578cea7SEric W. Biederman 		count = sizeof(buffer) - 1;
10261da177e4SLinus Torvalds 	if (copy_from_user(buffer, buf, count))
10271da177e4SLinus Torvalds 		return -EFAULT;
10285d863b89SKOSAKI Motohiro 
10295d863b89SKOSAKI Motohiro 	err = strict_strtol(strstrip(buffer), 0, &oom_adjust);
10305d863b89SKOSAKI Motohiro 	if (err)
10315d863b89SKOSAKI Motohiro 		return -EINVAL;
10328ac773b4SAlexey Dobriyan 	if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
10338ac773b4SAlexey Dobriyan 	     oom_adjust != OOM_DISABLE)
10341da177e4SLinus Torvalds 		return -EINVAL;
10355d863b89SKOSAKI Motohiro 
10362fddfeefSJosef "Jeff" Sipek 	task = get_proc_task(file->f_path.dentry->d_inode);
103799f89551SEric W. Biederman 	if (!task)
103899f89551SEric W. Biederman 		return -ESRCH;
103928b83c51SKOSAKI Motohiro 	if (!lock_task_sighand(task, &flags)) {
104028b83c51SKOSAKI Motohiro 		put_task_struct(task);
104128b83c51SKOSAKI Motohiro 		return -ESRCH;
104228b83c51SKOSAKI Motohiro 	}
104328b83c51SKOSAKI Motohiro 
104428b83c51SKOSAKI Motohiro 	if (oom_adjust < task->signal->oom_adj && !capable(CAP_SYS_RESOURCE)) {
104528b83c51SKOSAKI Motohiro 		unlock_task_sighand(task, &flags);
10468fb4fc68SGuillem Jover 		put_task_struct(task);
10478fb4fc68SGuillem Jover 		return -EACCES;
10488fb4fc68SGuillem Jover 	}
104928b83c51SKOSAKI Motohiro 
1050*3d5992d2SYing Han 	task_lock(task);
1051*3d5992d2SYing Han 	if (!task->mm) {
1052*3d5992d2SYing Han 		task_unlock(task);
1053*3d5992d2SYing Han 		unlock_task_sighand(task, &flags);
1054*3d5992d2SYing Han 		put_task_struct(task);
1055*3d5992d2SYing Han 		return -EINVAL;
1056*3d5992d2SYing Han 	}
1057*3d5992d2SYing Han 
1058*3d5992d2SYing Han 	if (oom_adjust != task->signal->oom_adj) {
1059*3d5992d2SYing Han 		if (oom_adjust == OOM_DISABLE)
1060*3d5992d2SYing Han 			atomic_inc(&task->mm->oom_disable_count);
1061*3d5992d2SYing Han 		if (task->signal->oom_adj == OOM_DISABLE)
1062*3d5992d2SYing Han 			atomic_dec(&task->mm->oom_disable_count);
1063*3d5992d2SYing Han 	}
1064*3d5992d2SYing Han 
106551b1bd2aSDavid Rientjes 	/*
106651b1bd2aSDavid Rientjes 	 * Warn that /proc/pid/oom_adj is deprecated, see
106751b1bd2aSDavid Rientjes 	 * Documentation/feature-removal-schedule.txt.
106851b1bd2aSDavid Rientjes 	 */
106951b1bd2aSDavid Rientjes 	printk_once(KERN_WARNING "%s (%d): /proc/%d/oom_adj is deprecated, "
107051b1bd2aSDavid Rientjes 			"please use /proc/%d/oom_score_adj instead.\n",
107151b1bd2aSDavid Rientjes 			current->comm, task_pid_nr(current),
107251b1bd2aSDavid Rientjes 			task_pid_nr(task), task_pid_nr(task));
107328b83c51SKOSAKI Motohiro 	task->signal->oom_adj = oom_adjust;
1074a63d83f4SDavid Rientjes 	/*
1075a63d83f4SDavid Rientjes 	 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1076a63d83f4SDavid Rientjes 	 * value is always attainable.
1077a63d83f4SDavid Rientjes 	 */
1078a63d83f4SDavid Rientjes 	if (task->signal->oom_adj == OOM_ADJUST_MAX)
1079a63d83f4SDavid Rientjes 		task->signal->oom_score_adj = OOM_SCORE_ADJ_MAX;
1080a63d83f4SDavid Rientjes 	else
1081a63d83f4SDavid Rientjes 		task->signal->oom_score_adj = (oom_adjust * OOM_SCORE_ADJ_MAX) /
1082a63d83f4SDavid Rientjes 								-OOM_DISABLE;
1083*3d5992d2SYing Han 	task_unlock(task);
108428b83c51SKOSAKI Motohiro 	unlock_task_sighand(task, &flags);
108599f89551SEric W. Biederman 	put_task_struct(task);
10865d863b89SKOSAKI Motohiro 
10875d863b89SKOSAKI Motohiro 	return count;
10881da177e4SLinus Torvalds }
10891da177e4SLinus Torvalds 
109000977a59SArjan van de Ven static const struct file_operations proc_oom_adjust_operations = {
10911da177e4SLinus Torvalds 	.read		= oom_adjust_read,
10921da177e4SLinus Torvalds 	.write		= oom_adjust_write,
109387df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
10941da177e4SLinus Torvalds };
10951da177e4SLinus Torvalds 
1096a63d83f4SDavid Rientjes static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1097a63d83f4SDavid Rientjes 					size_t count, loff_t *ppos)
1098a63d83f4SDavid Rientjes {
1099a63d83f4SDavid Rientjes 	struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
1100a63d83f4SDavid Rientjes 	char buffer[PROC_NUMBUF];
1101a63d83f4SDavid Rientjes 	int oom_score_adj = OOM_SCORE_ADJ_MIN;
1102a63d83f4SDavid Rientjes 	unsigned long flags;
1103a63d83f4SDavid Rientjes 	size_t len;
1104a63d83f4SDavid Rientjes 
1105a63d83f4SDavid Rientjes 	if (!task)
1106a63d83f4SDavid Rientjes 		return -ESRCH;
1107a63d83f4SDavid Rientjes 	if (lock_task_sighand(task, &flags)) {
1108a63d83f4SDavid Rientjes 		oom_score_adj = task->signal->oom_score_adj;
1109a63d83f4SDavid Rientjes 		unlock_task_sighand(task, &flags);
1110a63d83f4SDavid Rientjes 	}
1111a63d83f4SDavid Rientjes 	put_task_struct(task);
1112a63d83f4SDavid Rientjes 	len = snprintf(buffer, sizeof(buffer), "%d\n", oom_score_adj);
1113a63d83f4SDavid Rientjes 	return simple_read_from_buffer(buf, count, ppos, buffer, len);
1114a63d83f4SDavid Rientjes }
1115a63d83f4SDavid Rientjes 
1116a63d83f4SDavid Rientjes static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1117a63d83f4SDavid Rientjes 					size_t count, loff_t *ppos)
1118a63d83f4SDavid Rientjes {
1119a63d83f4SDavid Rientjes 	struct task_struct *task;
1120a63d83f4SDavid Rientjes 	char buffer[PROC_NUMBUF];
1121a63d83f4SDavid Rientjes 	unsigned long flags;
1122a63d83f4SDavid Rientjes 	long oom_score_adj;
1123a63d83f4SDavid Rientjes 	int err;
1124a63d83f4SDavid Rientjes 
1125a63d83f4SDavid Rientjes 	memset(buffer, 0, sizeof(buffer));
1126a63d83f4SDavid Rientjes 	if (count > sizeof(buffer) - 1)
1127a63d83f4SDavid Rientjes 		count = sizeof(buffer) - 1;
1128a63d83f4SDavid Rientjes 	if (copy_from_user(buffer, buf, count))
1129a63d83f4SDavid Rientjes 		return -EFAULT;
1130a63d83f4SDavid Rientjes 
1131a63d83f4SDavid Rientjes 	err = strict_strtol(strstrip(buffer), 0, &oom_score_adj);
1132a63d83f4SDavid Rientjes 	if (err)
1133a63d83f4SDavid Rientjes 		return -EINVAL;
1134a63d83f4SDavid Rientjes 	if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
1135a63d83f4SDavid Rientjes 			oom_score_adj > OOM_SCORE_ADJ_MAX)
1136a63d83f4SDavid Rientjes 		return -EINVAL;
1137a63d83f4SDavid Rientjes 
1138a63d83f4SDavid Rientjes 	task = get_proc_task(file->f_path.dentry->d_inode);
1139a63d83f4SDavid Rientjes 	if (!task)
1140a63d83f4SDavid Rientjes 		return -ESRCH;
1141a63d83f4SDavid Rientjes 	if (!lock_task_sighand(task, &flags)) {
1142a63d83f4SDavid Rientjes 		put_task_struct(task);
1143a63d83f4SDavid Rientjes 		return -ESRCH;
1144a63d83f4SDavid Rientjes 	}
1145a63d83f4SDavid Rientjes 	if (oom_score_adj < task->signal->oom_score_adj &&
1146a63d83f4SDavid Rientjes 			!capable(CAP_SYS_RESOURCE)) {
1147a63d83f4SDavid Rientjes 		unlock_task_sighand(task, &flags);
1148a63d83f4SDavid Rientjes 		put_task_struct(task);
1149a63d83f4SDavid Rientjes 		return -EACCES;
1150a63d83f4SDavid Rientjes 	}
1151a63d83f4SDavid Rientjes 
1152*3d5992d2SYing Han 	task_lock(task);
1153*3d5992d2SYing Han 	if (!task->mm) {
1154*3d5992d2SYing Han 		task_unlock(task);
1155*3d5992d2SYing Han 		unlock_task_sighand(task, &flags);
1156*3d5992d2SYing Han 		put_task_struct(task);
1157*3d5992d2SYing Han 		return -EINVAL;
1158*3d5992d2SYing Han 	}
1159*3d5992d2SYing Han 	if (oom_score_adj != task->signal->oom_score_adj) {
1160*3d5992d2SYing Han 		if (oom_score_adj == OOM_SCORE_ADJ_MIN)
1161*3d5992d2SYing Han 			atomic_inc(&task->mm->oom_disable_count);
1162*3d5992d2SYing Han 		if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MIN)
1163*3d5992d2SYing Han 			atomic_dec(&task->mm->oom_disable_count);
1164*3d5992d2SYing Han 	}
1165a63d83f4SDavid Rientjes 	task->signal->oom_score_adj = oom_score_adj;
1166a63d83f4SDavid Rientjes 	/*
1167a63d83f4SDavid Rientjes 	 * Scale /proc/pid/oom_adj appropriately ensuring that OOM_DISABLE is
1168a63d83f4SDavid Rientjes 	 * always attainable.
1169a63d83f4SDavid Rientjes 	 */
1170a63d83f4SDavid Rientjes 	if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MIN)
1171a63d83f4SDavid Rientjes 		task->signal->oom_adj = OOM_DISABLE;
1172a63d83f4SDavid Rientjes 	else
1173a63d83f4SDavid Rientjes 		task->signal->oom_adj = (oom_score_adj * OOM_ADJUST_MAX) /
1174a63d83f4SDavid Rientjes 							OOM_SCORE_ADJ_MAX;
1175*3d5992d2SYing Han 	task_unlock(task);
1176a63d83f4SDavid Rientjes 	unlock_task_sighand(task, &flags);
1177a63d83f4SDavid Rientjes 	put_task_struct(task);
1178a63d83f4SDavid Rientjes 	return count;
1179a63d83f4SDavid Rientjes }
1180a63d83f4SDavid Rientjes 
1181a63d83f4SDavid Rientjes static const struct file_operations proc_oom_score_adj_operations = {
1182a63d83f4SDavid Rientjes 	.read		= oom_score_adj_read,
1183a63d83f4SDavid Rientjes 	.write		= oom_score_adj_write,
11846038f373SArnd Bergmann 	.llseek		= default_llseek,
1185a63d83f4SDavid Rientjes };
1186a63d83f4SDavid Rientjes 
11871da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL
11881da177e4SLinus Torvalds #define TMPBUFLEN 21
11891da177e4SLinus Torvalds static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
11901da177e4SLinus Torvalds 				  size_t count, loff_t *ppos)
11911da177e4SLinus Torvalds {
11922fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
119399f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
11941da177e4SLinus Torvalds 	ssize_t length;
11951da177e4SLinus Torvalds 	char tmpbuf[TMPBUFLEN];
11961da177e4SLinus Torvalds 
119799f89551SEric W. Biederman 	if (!task)
119899f89551SEric W. Biederman 		return -ESRCH;
11991da177e4SLinus Torvalds 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
12000c11b942SAl Viro 				audit_get_loginuid(task));
120199f89551SEric W. Biederman 	put_task_struct(task);
12021da177e4SLinus Torvalds 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
12031da177e4SLinus Torvalds }
12041da177e4SLinus Torvalds 
12051da177e4SLinus Torvalds static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
12061da177e4SLinus Torvalds 				   size_t count, loff_t *ppos)
12071da177e4SLinus Torvalds {
12082fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
12091da177e4SLinus Torvalds 	char *page, *tmp;
12101da177e4SLinus Torvalds 	ssize_t length;
12111da177e4SLinus Torvalds 	uid_t loginuid;
12121da177e4SLinus Torvalds 
12131da177e4SLinus Torvalds 	if (!capable(CAP_AUDIT_CONTROL))
12141da177e4SLinus Torvalds 		return -EPERM;
12151da177e4SLinus Torvalds 
12167dc52157SPaul E. McKenney 	rcu_read_lock();
12177dc52157SPaul E. McKenney 	if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
12187dc52157SPaul E. McKenney 		rcu_read_unlock();
12191da177e4SLinus Torvalds 		return -EPERM;
12207dc52157SPaul E. McKenney 	}
12217dc52157SPaul E. McKenney 	rcu_read_unlock();
12221da177e4SLinus Torvalds 
1223e0182909SAl Viro 	if (count >= PAGE_SIZE)
1224e0182909SAl Viro 		count = PAGE_SIZE - 1;
12251da177e4SLinus Torvalds 
12261da177e4SLinus Torvalds 	if (*ppos != 0) {
12271da177e4SLinus Torvalds 		/* No partial writes. */
12281da177e4SLinus Torvalds 		return -EINVAL;
12291da177e4SLinus Torvalds 	}
1230e12ba74dSMel Gorman 	page = (char*)__get_free_page(GFP_TEMPORARY);
12311da177e4SLinus Torvalds 	if (!page)
12321da177e4SLinus Torvalds 		return -ENOMEM;
12331da177e4SLinus Torvalds 	length = -EFAULT;
12341da177e4SLinus Torvalds 	if (copy_from_user(page, buf, count))
12351da177e4SLinus Torvalds 		goto out_free_page;
12361da177e4SLinus Torvalds 
1237e0182909SAl Viro 	page[count] = '\0';
12381da177e4SLinus Torvalds 	loginuid = simple_strtoul(page, &tmp, 10);
12391da177e4SLinus Torvalds 	if (tmp == page) {
12401da177e4SLinus Torvalds 		length = -EINVAL;
12411da177e4SLinus Torvalds 		goto out_free_page;
12421da177e4SLinus Torvalds 
12431da177e4SLinus Torvalds 	}
124499f89551SEric W. Biederman 	length = audit_set_loginuid(current, loginuid);
12451da177e4SLinus Torvalds 	if (likely(length == 0))
12461da177e4SLinus Torvalds 		length = count;
12471da177e4SLinus Torvalds 
12481da177e4SLinus Torvalds out_free_page:
12491da177e4SLinus Torvalds 	free_page((unsigned long) page);
12501da177e4SLinus Torvalds 	return length;
12511da177e4SLinus Torvalds }
12521da177e4SLinus Torvalds 
125300977a59SArjan van de Ven static const struct file_operations proc_loginuid_operations = {
12541da177e4SLinus Torvalds 	.read		= proc_loginuid_read,
12551da177e4SLinus Torvalds 	.write		= proc_loginuid_write,
125687df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
12571da177e4SLinus Torvalds };
12581e0bd755SEric Paris 
12591e0bd755SEric Paris static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
12601e0bd755SEric Paris 				  size_t count, loff_t *ppos)
12611e0bd755SEric Paris {
12621e0bd755SEric Paris 	struct inode * inode = file->f_path.dentry->d_inode;
12631e0bd755SEric Paris 	struct task_struct *task = get_proc_task(inode);
12641e0bd755SEric Paris 	ssize_t length;
12651e0bd755SEric Paris 	char tmpbuf[TMPBUFLEN];
12661e0bd755SEric Paris 
12671e0bd755SEric Paris 	if (!task)
12681e0bd755SEric Paris 		return -ESRCH;
12691e0bd755SEric Paris 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
12701e0bd755SEric Paris 				audit_get_sessionid(task));
12711e0bd755SEric Paris 	put_task_struct(task);
12721e0bd755SEric Paris 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
12731e0bd755SEric Paris }
12741e0bd755SEric Paris 
12751e0bd755SEric Paris static const struct file_operations proc_sessionid_operations = {
12761e0bd755SEric Paris 	.read		= proc_sessionid_read,
127787df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
12781e0bd755SEric Paris };
12791da177e4SLinus Torvalds #endif
12801da177e4SLinus Torvalds 
1281f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
1282f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1283f4f154fdSAkinobu Mita 				      size_t count, loff_t *ppos)
1284f4f154fdSAkinobu Mita {
1285f4f154fdSAkinobu Mita 	struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1286f4f154fdSAkinobu Mita 	char buffer[PROC_NUMBUF];
1287f4f154fdSAkinobu Mita 	size_t len;
1288f4f154fdSAkinobu Mita 	int make_it_fail;
1289f4f154fdSAkinobu Mita 
1290f4f154fdSAkinobu Mita 	if (!task)
1291f4f154fdSAkinobu Mita 		return -ESRCH;
1292f4f154fdSAkinobu Mita 	make_it_fail = task->make_it_fail;
1293f4f154fdSAkinobu Mita 	put_task_struct(task);
1294f4f154fdSAkinobu Mita 
1295f4f154fdSAkinobu Mita 	len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
12960c28f287SAkinobu Mita 
12970c28f287SAkinobu Mita 	return simple_read_from_buffer(buf, count, ppos, buffer, len);
1298f4f154fdSAkinobu Mita }
1299f4f154fdSAkinobu Mita 
1300f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_write(struct file * file,
1301f4f154fdSAkinobu Mita 			const char __user * buf, size_t count, loff_t *ppos)
1302f4f154fdSAkinobu Mita {
1303f4f154fdSAkinobu Mita 	struct task_struct *task;
1304f4f154fdSAkinobu Mita 	char buffer[PROC_NUMBUF], *end;
1305f4f154fdSAkinobu Mita 	int make_it_fail;
1306f4f154fdSAkinobu Mita 
1307f4f154fdSAkinobu Mita 	if (!capable(CAP_SYS_RESOURCE))
1308f4f154fdSAkinobu Mita 		return -EPERM;
1309f4f154fdSAkinobu Mita 	memset(buffer, 0, sizeof(buffer));
1310f4f154fdSAkinobu Mita 	if (count > sizeof(buffer) - 1)
1311f4f154fdSAkinobu Mita 		count = sizeof(buffer) - 1;
1312f4f154fdSAkinobu Mita 	if (copy_from_user(buffer, buf, count))
1313f4f154fdSAkinobu Mita 		return -EFAULT;
1314cba8aafeSVincent Li 	make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1315cba8aafeSVincent Li 	if (*end)
1316cba8aafeSVincent Li 		return -EINVAL;
1317f4f154fdSAkinobu Mita 	task = get_proc_task(file->f_dentry->d_inode);
1318f4f154fdSAkinobu Mita 	if (!task)
1319f4f154fdSAkinobu Mita 		return -ESRCH;
1320f4f154fdSAkinobu Mita 	task->make_it_fail = make_it_fail;
1321f4f154fdSAkinobu Mita 	put_task_struct(task);
1322cba8aafeSVincent Li 
1323cba8aafeSVincent Li 	return count;
1324f4f154fdSAkinobu Mita }
1325f4f154fdSAkinobu Mita 
132600977a59SArjan van de Ven static const struct file_operations proc_fault_inject_operations = {
1327f4f154fdSAkinobu Mita 	.read		= proc_fault_inject_read,
1328f4f154fdSAkinobu Mita 	.write		= proc_fault_inject_write,
132987df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
1330f4f154fdSAkinobu Mita };
1331f4f154fdSAkinobu Mita #endif
1332f4f154fdSAkinobu Mita 
13339745512cSArjan van de Ven 
133443ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
133543ae34cbSIngo Molnar /*
133643ae34cbSIngo Molnar  * Print out various scheduling related per-task fields:
133743ae34cbSIngo Molnar  */
133843ae34cbSIngo Molnar static int sched_show(struct seq_file *m, void *v)
133943ae34cbSIngo Molnar {
134043ae34cbSIngo Molnar 	struct inode *inode = m->private;
134143ae34cbSIngo Molnar 	struct task_struct *p;
134243ae34cbSIngo Molnar 
134343ae34cbSIngo Molnar 	p = get_proc_task(inode);
134443ae34cbSIngo Molnar 	if (!p)
134543ae34cbSIngo Molnar 		return -ESRCH;
134643ae34cbSIngo Molnar 	proc_sched_show_task(p, m);
134743ae34cbSIngo Molnar 
134843ae34cbSIngo Molnar 	put_task_struct(p);
134943ae34cbSIngo Molnar 
135043ae34cbSIngo Molnar 	return 0;
135143ae34cbSIngo Molnar }
135243ae34cbSIngo Molnar 
135343ae34cbSIngo Molnar static ssize_t
135443ae34cbSIngo Molnar sched_write(struct file *file, const char __user *buf,
135543ae34cbSIngo Molnar 	    size_t count, loff_t *offset)
135643ae34cbSIngo Molnar {
135743ae34cbSIngo Molnar 	struct inode *inode = file->f_path.dentry->d_inode;
135843ae34cbSIngo Molnar 	struct task_struct *p;
135943ae34cbSIngo Molnar 
136043ae34cbSIngo Molnar 	p = get_proc_task(inode);
136143ae34cbSIngo Molnar 	if (!p)
136243ae34cbSIngo Molnar 		return -ESRCH;
136343ae34cbSIngo Molnar 	proc_sched_set_task(p);
136443ae34cbSIngo Molnar 
136543ae34cbSIngo Molnar 	put_task_struct(p);
136643ae34cbSIngo Molnar 
136743ae34cbSIngo Molnar 	return count;
136843ae34cbSIngo Molnar }
136943ae34cbSIngo Molnar 
137043ae34cbSIngo Molnar static int sched_open(struct inode *inode, struct file *filp)
137143ae34cbSIngo Molnar {
137243ae34cbSIngo Molnar 	int ret;
137343ae34cbSIngo Molnar 
137443ae34cbSIngo Molnar 	ret = single_open(filp, sched_show, NULL);
137543ae34cbSIngo Molnar 	if (!ret) {
137643ae34cbSIngo Molnar 		struct seq_file *m = filp->private_data;
137743ae34cbSIngo Molnar 
137843ae34cbSIngo Molnar 		m->private = inode;
137943ae34cbSIngo Molnar 	}
138043ae34cbSIngo Molnar 	return ret;
138143ae34cbSIngo Molnar }
138243ae34cbSIngo Molnar 
138343ae34cbSIngo Molnar static const struct file_operations proc_pid_sched_operations = {
138443ae34cbSIngo Molnar 	.open		= sched_open,
138543ae34cbSIngo Molnar 	.read		= seq_read,
138643ae34cbSIngo Molnar 	.write		= sched_write,
138743ae34cbSIngo Molnar 	.llseek		= seq_lseek,
13885ea473a1SAlexey Dobriyan 	.release	= single_release,
138943ae34cbSIngo Molnar };
139043ae34cbSIngo Molnar 
139143ae34cbSIngo Molnar #endif
139243ae34cbSIngo Molnar 
13934614a696Sjohn stultz static ssize_t comm_write(struct file *file, const char __user *buf,
13944614a696Sjohn stultz 				size_t count, loff_t *offset)
13954614a696Sjohn stultz {
13964614a696Sjohn stultz 	struct inode *inode = file->f_path.dentry->d_inode;
13974614a696Sjohn stultz 	struct task_struct *p;
13984614a696Sjohn stultz 	char buffer[TASK_COMM_LEN];
13994614a696Sjohn stultz 
14004614a696Sjohn stultz 	memset(buffer, 0, sizeof(buffer));
14014614a696Sjohn stultz 	if (count > sizeof(buffer) - 1)
14024614a696Sjohn stultz 		count = sizeof(buffer) - 1;
14034614a696Sjohn stultz 	if (copy_from_user(buffer, buf, count))
14044614a696Sjohn stultz 		return -EFAULT;
14054614a696Sjohn stultz 
14064614a696Sjohn stultz 	p = get_proc_task(inode);
14074614a696Sjohn stultz 	if (!p)
14084614a696Sjohn stultz 		return -ESRCH;
14094614a696Sjohn stultz 
14104614a696Sjohn stultz 	if (same_thread_group(current, p))
14114614a696Sjohn stultz 		set_task_comm(p, buffer);
14124614a696Sjohn stultz 	else
14134614a696Sjohn stultz 		count = -EINVAL;
14144614a696Sjohn stultz 
14154614a696Sjohn stultz 	put_task_struct(p);
14164614a696Sjohn stultz 
14174614a696Sjohn stultz 	return count;
14184614a696Sjohn stultz }
14194614a696Sjohn stultz 
14204614a696Sjohn stultz static int comm_show(struct seq_file *m, void *v)
14214614a696Sjohn stultz {
14224614a696Sjohn stultz 	struct inode *inode = m->private;
14234614a696Sjohn stultz 	struct task_struct *p;
14244614a696Sjohn stultz 
14254614a696Sjohn stultz 	p = get_proc_task(inode);
14264614a696Sjohn stultz 	if (!p)
14274614a696Sjohn stultz 		return -ESRCH;
14284614a696Sjohn stultz 
14294614a696Sjohn stultz 	task_lock(p);
14304614a696Sjohn stultz 	seq_printf(m, "%s\n", p->comm);
14314614a696Sjohn stultz 	task_unlock(p);
14324614a696Sjohn stultz 
14334614a696Sjohn stultz 	put_task_struct(p);
14344614a696Sjohn stultz 
14354614a696Sjohn stultz 	return 0;
14364614a696Sjohn stultz }
14374614a696Sjohn stultz 
14384614a696Sjohn stultz static int comm_open(struct inode *inode, struct file *filp)
14394614a696Sjohn stultz {
14404614a696Sjohn stultz 	int ret;
14414614a696Sjohn stultz 
14424614a696Sjohn stultz 	ret = single_open(filp, comm_show, NULL);
14434614a696Sjohn stultz 	if (!ret) {
14444614a696Sjohn stultz 		struct seq_file *m = filp->private_data;
14454614a696Sjohn stultz 
14464614a696Sjohn stultz 		m->private = inode;
14474614a696Sjohn stultz 	}
14484614a696Sjohn stultz 	return ret;
14494614a696Sjohn stultz }
14504614a696Sjohn stultz 
14514614a696Sjohn stultz static const struct file_operations proc_pid_set_comm_operations = {
14524614a696Sjohn stultz 	.open		= comm_open,
14534614a696Sjohn stultz 	.read		= seq_read,
14544614a696Sjohn stultz 	.write		= comm_write,
14554614a696Sjohn stultz 	.llseek		= seq_lseek,
14564614a696Sjohn stultz 	.release	= single_release,
14574614a696Sjohn stultz };
14584614a696Sjohn stultz 
1459925d1c40SMatt Helsley /*
1460925d1c40SMatt Helsley  * We added or removed a vma mapping the executable. The vmas are only mapped
1461925d1c40SMatt Helsley  * during exec and are not mapped with the mmap system call.
1462925d1c40SMatt Helsley  * Callers must hold down_write() on the mm's mmap_sem for these
1463925d1c40SMatt Helsley  */
1464925d1c40SMatt Helsley void added_exe_file_vma(struct mm_struct *mm)
1465925d1c40SMatt Helsley {
1466925d1c40SMatt Helsley 	mm->num_exe_file_vmas++;
1467925d1c40SMatt Helsley }
1468925d1c40SMatt Helsley 
1469925d1c40SMatt Helsley void removed_exe_file_vma(struct mm_struct *mm)
1470925d1c40SMatt Helsley {
1471925d1c40SMatt Helsley 	mm->num_exe_file_vmas--;
1472925d1c40SMatt Helsley 	if ((mm->num_exe_file_vmas == 0) && mm->exe_file){
1473925d1c40SMatt Helsley 		fput(mm->exe_file);
1474925d1c40SMatt Helsley 		mm->exe_file = NULL;
1475925d1c40SMatt Helsley 	}
1476925d1c40SMatt Helsley 
1477925d1c40SMatt Helsley }
1478925d1c40SMatt Helsley 
1479925d1c40SMatt Helsley void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file)
1480925d1c40SMatt Helsley {
1481925d1c40SMatt Helsley 	if (new_exe_file)
1482925d1c40SMatt Helsley 		get_file(new_exe_file);
1483925d1c40SMatt Helsley 	if (mm->exe_file)
1484925d1c40SMatt Helsley 		fput(mm->exe_file);
1485925d1c40SMatt Helsley 	mm->exe_file = new_exe_file;
1486925d1c40SMatt Helsley 	mm->num_exe_file_vmas = 0;
1487925d1c40SMatt Helsley }
1488925d1c40SMatt Helsley 
1489925d1c40SMatt Helsley struct file *get_mm_exe_file(struct mm_struct *mm)
1490925d1c40SMatt Helsley {
1491925d1c40SMatt Helsley 	struct file *exe_file;
1492925d1c40SMatt Helsley 
1493925d1c40SMatt Helsley 	/* We need mmap_sem to protect against races with removal of
1494925d1c40SMatt Helsley 	 * VM_EXECUTABLE vmas */
1495925d1c40SMatt Helsley 	down_read(&mm->mmap_sem);
1496925d1c40SMatt Helsley 	exe_file = mm->exe_file;
1497925d1c40SMatt Helsley 	if (exe_file)
1498925d1c40SMatt Helsley 		get_file(exe_file);
1499925d1c40SMatt Helsley 	up_read(&mm->mmap_sem);
1500925d1c40SMatt Helsley 	return exe_file;
1501925d1c40SMatt Helsley }
1502925d1c40SMatt Helsley 
1503925d1c40SMatt Helsley void dup_mm_exe_file(struct mm_struct *oldmm, struct mm_struct *newmm)
1504925d1c40SMatt Helsley {
1505925d1c40SMatt Helsley 	/* It's safe to write the exe_file pointer without exe_file_lock because
1506925d1c40SMatt Helsley 	 * this is called during fork when the task is not yet in /proc */
1507925d1c40SMatt Helsley 	newmm->exe_file = get_mm_exe_file(oldmm);
1508925d1c40SMatt Helsley }
1509925d1c40SMatt Helsley 
1510925d1c40SMatt Helsley static int proc_exe_link(struct inode *inode, struct path *exe_path)
1511925d1c40SMatt Helsley {
1512925d1c40SMatt Helsley 	struct task_struct *task;
1513925d1c40SMatt Helsley 	struct mm_struct *mm;
1514925d1c40SMatt Helsley 	struct file *exe_file;
1515925d1c40SMatt Helsley 
1516925d1c40SMatt Helsley 	task = get_proc_task(inode);
1517925d1c40SMatt Helsley 	if (!task)
1518925d1c40SMatt Helsley 		return -ENOENT;
1519925d1c40SMatt Helsley 	mm = get_task_mm(task);
1520925d1c40SMatt Helsley 	put_task_struct(task);
1521925d1c40SMatt Helsley 	if (!mm)
1522925d1c40SMatt Helsley 		return -ENOENT;
1523925d1c40SMatt Helsley 	exe_file = get_mm_exe_file(mm);
1524925d1c40SMatt Helsley 	mmput(mm);
1525925d1c40SMatt Helsley 	if (exe_file) {
1526925d1c40SMatt Helsley 		*exe_path = exe_file->f_path;
1527925d1c40SMatt Helsley 		path_get(&exe_file->f_path);
1528925d1c40SMatt Helsley 		fput(exe_file);
1529925d1c40SMatt Helsley 		return 0;
1530925d1c40SMatt Helsley 	} else
1531925d1c40SMatt Helsley 		return -ENOENT;
1532925d1c40SMatt Helsley }
1533925d1c40SMatt Helsley 
1534008b150aSAl Viro static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
15351da177e4SLinus Torvalds {
15361da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
15371da177e4SLinus Torvalds 	int error = -EACCES;
15381da177e4SLinus Torvalds 
15391da177e4SLinus Torvalds 	/* We don't need a base pointer in the /proc filesystem */
15401d957f9bSJan Blunck 	path_put(&nd->path);
15411da177e4SLinus Torvalds 
1542778c1144SEric W. Biederman 	/* Are we allowed to snoop on the tasks file descriptors? */
1543778c1144SEric W. Biederman 	if (!proc_fd_access_allowed(inode))
15441da177e4SLinus Torvalds 		goto out;
15451da177e4SLinus Torvalds 
15463dcd25f3SJan Blunck 	error = PROC_I(inode)->op.proc_get_link(inode, &nd->path);
15471da177e4SLinus Torvalds out:
1548008b150aSAl Viro 	return ERR_PTR(error);
15491da177e4SLinus Torvalds }
15501da177e4SLinus Torvalds 
15513dcd25f3SJan Blunck static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
15521da177e4SLinus Torvalds {
1553e12ba74dSMel Gorman 	char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
15543dcd25f3SJan Blunck 	char *pathname;
15551da177e4SLinus Torvalds 	int len;
15561da177e4SLinus Torvalds 
15571da177e4SLinus Torvalds 	if (!tmp)
15581da177e4SLinus Torvalds 		return -ENOMEM;
15591da177e4SLinus Torvalds 
15608df9d1a4SMiklos Szeredi 	pathname = d_path_with_unreachable(path, tmp, PAGE_SIZE);
15613dcd25f3SJan Blunck 	len = PTR_ERR(pathname);
15623dcd25f3SJan Blunck 	if (IS_ERR(pathname))
15631da177e4SLinus Torvalds 		goto out;
15643dcd25f3SJan Blunck 	len = tmp + PAGE_SIZE - 1 - pathname;
15651da177e4SLinus Torvalds 
15661da177e4SLinus Torvalds 	if (len > buflen)
15671da177e4SLinus Torvalds 		len = buflen;
15683dcd25f3SJan Blunck 	if (copy_to_user(buffer, pathname, len))
15691da177e4SLinus Torvalds 		len = -EFAULT;
15701da177e4SLinus Torvalds  out:
15711da177e4SLinus Torvalds 	free_page((unsigned long)tmp);
15721da177e4SLinus Torvalds 	return len;
15731da177e4SLinus Torvalds }
15741da177e4SLinus Torvalds 
15751da177e4SLinus Torvalds static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
15761da177e4SLinus Torvalds {
15771da177e4SLinus Torvalds 	int error = -EACCES;
15781da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
15793dcd25f3SJan Blunck 	struct path path;
15801da177e4SLinus Torvalds 
1581778c1144SEric W. Biederman 	/* Are we allowed to snoop on the tasks file descriptors? */
1582778c1144SEric W. Biederman 	if (!proc_fd_access_allowed(inode))
15831da177e4SLinus Torvalds 		goto out;
15841da177e4SLinus Torvalds 
15853dcd25f3SJan Blunck 	error = PROC_I(inode)->op.proc_get_link(inode, &path);
15861da177e4SLinus Torvalds 	if (error)
15871da177e4SLinus Torvalds 		goto out;
15881da177e4SLinus Torvalds 
15893dcd25f3SJan Blunck 	error = do_proc_readlink(&path, buffer, buflen);
15903dcd25f3SJan Blunck 	path_put(&path);
15911da177e4SLinus Torvalds out:
15921da177e4SLinus Torvalds 	return error;
15931da177e4SLinus Torvalds }
15941da177e4SLinus Torvalds 
1595c5ef1c42SArjan van de Ven static const struct inode_operations proc_pid_link_inode_operations = {
15961da177e4SLinus Torvalds 	.readlink	= proc_pid_readlink,
15976d76fa58SLinus Torvalds 	.follow_link	= proc_pid_follow_link,
15986d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
15991da177e4SLinus Torvalds };
16001da177e4SLinus Torvalds 
160128a6d671SEric W. Biederman 
160228a6d671SEric W. Biederman /* building an inode */
160328a6d671SEric W. Biederman 
160428a6d671SEric W. Biederman static int task_dumpable(struct task_struct *task)
160528a6d671SEric W. Biederman {
160628a6d671SEric W. Biederman 	int dumpable = 0;
160728a6d671SEric W. Biederman 	struct mm_struct *mm;
160828a6d671SEric W. Biederman 
160928a6d671SEric W. Biederman 	task_lock(task);
161028a6d671SEric W. Biederman 	mm = task->mm;
161128a6d671SEric W. Biederman 	if (mm)
16126c5d5238SKawai, Hidehiro 		dumpable = get_dumpable(mm);
161328a6d671SEric W. Biederman 	task_unlock(task);
161428a6d671SEric W. Biederman 	if(dumpable == 1)
161528a6d671SEric W. Biederman 		return 1;
161628a6d671SEric W. Biederman 	return 0;
161728a6d671SEric W. Biederman }
161828a6d671SEric W. Biederman 
161928a6d671SEric W. Biederman 
162061a28784SEric W. Biederman static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
162128a6d671SEric W. Biederman {
162228a6d671SEric W. Biederman 	struct inode * inode;
162328a6d671SEric W. Biederman 	struct proc_inode *ei;
1624c69e8d9cSDavid Howells 	const struct cred *cred;
162528a6d671SEric W. Biederman 
162628a6d671SEric W. Biederman 	/* We need a new inode */
162728a6d671SEric W. Biederman 
162828a6d671SEric W. Biederman 	inode = new_inode(sb);
162928a6d671SEric W. Biederman 	if (!inode)
163028a6d671SEric W. Biederman 		goto out;
163128a6d671SEric W. Biederman 
163228a6d671SEric W. Biederman 	/* Common stuff */
163328a6d671SEric W. Biederman 	ei = PROC_I(inode);
163428a6d671SEric W. Biederman 	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
163528a6d671SEric W. Biederman 	inode->i_op = &proc_def_inode_operations;
163628a6d671SEric W. Biederman 
163728a6d671SEric W. Biederman 	/*
163828a6d671SEric W. Biederman 	 * grab the reference to task.
163928a6d671SEric W. Biederman 	 */
16401a657f78SOleg Nesterov 	ei->pid = get_task_pid(task, PIDTYPE_PID);
164128a6d671SEric W. Biederman 	if (!ei->pid)
164228a6d671SEric W. Biederman 		goto out_unlock;
164328a6d671SEric W. Biederman 
164428a6d671SEric W. Biederman 	if (task_dumpable(task)) {
1645c69e8d9cSDavid Howells 		rcu_read_lock();
1646c69e8d9cSDavid Howells 		cred = __task_cred(task);
1647c69e8d9cSDavid Howells 		inode->i_uid = cred->euid;
1648c69e8d9cSDavid Howells 		inode->i_gid = cred->egid;
1649c69e8d9cSDavid Howells 		rcu_read_unlock();
165028a6d671SEric W. Biederman 	}
165128a6d671SEric W. Biederman 	security_task_to_inode(task, inode);
165228a6d671SEric W. Biederman 
165328a6d671SEric W. Biederman out:
165428a6d671SEric W. Biederman 	return inode;
165528a6d671SEric W. Biederman 
165628a6d671SEric W. Biederman out_unlock:
165728a6d671SEric W. Biederman 	iput(inode);
165828a6d671SEric W. Biederman 	return NULL;
165928a6d671SEric W. Biederman }
166028a6d671SEric W. Biederman 
166128a6d671SEric W. Biederman static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
166228a6d671SEric W. Biederman {
166328a6d671SEric W. Biederman 	struct inode *inode = dentry->d_inode;
166428a6d671SEric W. Biederman 	struct task_struct *task;
1665c69e8d9cSDavid Howells 	const struct cred *cred;
1666c69e8d9cSDavid Howells 
166728a6d671SEric W. Biederman 	generic_fillattr(inode, stat);
166828a6d671SEric W. Biederman 
166928a6d671SEric W. Biederman 	rcu_read_lock();
167028a6d671SEric W. Biederman 	stat->uid = 0;
167128a6d671SEric W. Biederman 	stat->gid = 0;
167228a6d671SEric W. Biederman 	task = pid_task(proc_pid(inode), PIDTYPE_PID);
167328a6d671SEric W. Biederman 	if (task) {
167428a6d671SEric W. Biederman 		if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
167528a6d671SEric W. Biederman 		    task_dumpable(task)) {
1676c69e8d9cSDavid Howells 			cred = __task_cred(task);
1677c69e8d9cSDavid Howells 			stat->uid = cred->euid;
1678c69e8d9cSDavid Howells 			stat->gid = cred->egid;
167928a6d671SEric W. Biederman 		}
168028a6d671SEric W. Biederman 	}
168128a6d671SEric W. Biederman 	rcu_read_unlock();
168228a6d671SEric W. Biederman 	return 0;
168328a6d671SEric W. Biederman }
168428a6d671SEric W. Biederman 
168528a6d671SEric W. Biederman /* dentry stuff */
168628a6d671SEric W. Biederman 
168728a6d671SEric W. Biederman /*
168828a6d671SEric W. Biederman  *	Exceptional case: normally we are not allowed to unhash a busy
168928a6d671SEric W. Biederman  * directory. In this case, however, we can do it - no aliasing problems
169028a6d671SEric W. Biederman  * due to the way we treat inodes.
169128a6d671SEric W. Biederman  *
169228a6d671SEric W. Biederman  * Rewrite the inode's ownerships here because the owning task may have
169328a6d671SEric W. Biederman  * performed a setuid(), etc.
169428a6d671SEric W. Biederman  *
169528a6d671SEric W. Biederman  * Before the /proc/pid/status file was created the only way to read
169628a6d671SEric W. Biederman  * the effective uid of a /process was to stat /proc/pid.  Reading
169728a6d671SEric W. Biederman  * /proc/pid/status is slow enough that procps and other packages
169828a6d671SEric W. Biederman  * kept stating /proc/pid.  To keep the rules in /proc simple I have
169928a6d671SEric W. Biederman  * made this apply to all per process world readable and executable
170028a6d671SEric W. Biederman  * directories.
170128a6d671SEric W. Biederman  */
170228a6d671SEric W. Biederman static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
170328a6d671SEric W. Biederman {
170428a6d671SEric W. Biederman 	struct inode *inode = dentry->d_inode;
170528a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
1706c69e8d9cSDavid Howells 	const struct cred *cred;
1707c69e8d9cSDavid Howells 
170828a6d671SEric W. Biederman 	if (task) {
170928a6d671SEric W. Biederman 		if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
171028a6d671SEric W. Biederman 		    task_dumpable(task)) {
1711c69e8d9cSDavid Howells 			rcu_read_lock();
1712c69e8d9cSDavid Howells 			cred = __task_cred(task);
1713c69e8d9cSDavid Howells 			inode->i_uid = cred->euid;
1714c69e8d9cSDavid Howells 			inode->i_gid = cred->egid;
1715c69e8d9cSDavid Howells 			rcu_read_unlock();
171628a6d671SEric W. Biederman 		} else {
171728a6d671SEric W. Biederman 			inode->i_uid = 0;
171828a6d671SEric W. Biederman 			inode->i_gid = 0;
171928a6d671SEric W. Biederman 		}
172028a6d671SEric W. Biederman 		inode->i_mode &= ~(S_ISUID | S_ISGID);
172128a6d671SEric W. Biederman 		security_task_to_inode(task, inode);
172228a6d671SEric W. Biederman 		put_task_struct(task);
172328a6d671SEric W. Biederman 		return 1;
172428a6d671SEric W. Biederman 	}
172528a6d671SEric W. Biederman 	d_drop(dentry);
172628a6d671SEric W. Biederman 	return 0;
172728a6d671SEric W. Biederman }
172828a6d671SEric W. Biederman 
172928a6d671SEric W. Biederman static int pid_delete_dentry(struct dentry * dentry)
173028a6d671SEric W. Biederman {
173128a6d671SEric W. Biederman 	/* Is the task we represent dead?
173228a6d671SEric W. Biederman 	 * If so, then don't put the dentry on the lru list,
173328a6d671SEric W. Biederman 	 * kill it immediately.
173428a6d671SEric W. Biederman 	 */
173528a6d671SEric W. Biederman 	return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
173628a6d671SEric W. Biederman }
173728a6d671SEric W. Biederman 
1738d72f71ebSAl Viro static const struct dentry_operations pid_dentry_operations =
173928a6d671SEric W. Biederman {
174028a6d671SEric W. Biederman 	.d_revalidate	= pid_revalidate,
174128a6d671SEric W. Biederman 	.d_delete	= pid_delete_dentry,
174228a6d671SEric W. Biederman };
174328a6d671SEric W. Biederman 
174428a6d671SEric W. Biederman /* Lookups */
174528a6d671SEric W. Biederman 
1746c5141e6dSEric Dumazet typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1747c5141e6dSEric Dumazet 				struct task_struct *, const void *);
174861a28784SEric W. Biederman 
17491c0d04c9SEric W. Biederman /*
17501c0d04c9SEric W. Biederman  * Fill a directory entry.
17511c0d04c9SEric W. Biederman  *
17521c0d04c9SEric W. Biederman  * If possible create the dcache entry and derive our inode number and
17531c0d04c9SEric W. Biederman  * file type from dcache entry.
17541c0d04c9SEric W. Biederman  *
17551c0d04c9SEric W. Biederman  * Since all of the proc inode numbers are dynamically generated, the inode
17561c0d04c9SEric W. Biederman  * numbers do not exist until the inode is cache.  This means creating the
17571c0d04c9SEric W. Biederman  * the dcache entry in readdir is necessary to keep the inode numbers
17581c0d04c9SEric W. Biederman  * reported by readdir in sync with the inode numbers reported
17591c0d04c9SEric W. Biederman  * by stat.
17601c0d04c9SEric W. Biederman  */
176161a28784SEric W. Biederman static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
176261a28784SEric W. Biederman 	char *name, int len,
1763c5141e6dSEric Dumazet 	instantiate_t instantiate, struct task_struct *task, const void *ptr)
176461a28784SEric W. Biederman {
17652fddfeefSJosef "Jeff" Sipek 	struct dentry *child, *dir = filp->f_path.dentry;
176661a28784SEric W. Biederman 	struct inode *inode;
176761a28784SEric W. Biederman 	struct qstr qname;
176861a28784SEric W. Biederman 	ino_t ino = 0;
176961a28784SEric W. Biederman 	unsigned type = DT_UNKNOWN;
177061a28784SEric W. Biederman 
177161a28784SEric W. Biederman 	qname.name = name;
177261a28784SEric W. Biederman 	qname.len  = len;
177361a28784SEric W. Biederman 	qname.hash = full_name_hash(name, len);
177461a28784SEric W. Biederman 
177561a28784SEric W. Biederman 	child = d_lookup(dir, &qname);
177661a28784SEric W. Biederman 	if (!child) {
177761a28784SEric W. Biederman 		struct dentry *new;
177861a28784SEric W. Biederman 		new = d_alloc(dir, &qname);
177961a28784SEric W. Biederman 		if (new) {
178061a28784SEric W. Biederman 			child = instantiate(dir->d_inode, new, task, ptr);
178161a28784SEric W. Biederman 			if (child)
178261a28784SEric W. Biederman 				dput(new);
178361a28784SEric W. Biederman 			else
178461a28784SEric W. Biederman 				child = new;
178561a28784SEric W. Biederman 		}
178661a28784SEric W. Biederman 	}
178761a28784SEric W. Biederman 	if (!child || IS_ERR(child) || !child->d_inode)
178861a28784SEric W. Biederman 		goto end_instantiate;
178961a28784SEric W. Biederman 	inode = child->d_inode;
179061a28784SEric W. Biederman 	if (inode) {
179161a28784SEric W. Biederman 		ino = inode->i_ino;
179261a28784SEric W. Biederman 		type = inode->i_mode >> 12;
179361a28784SEric W. Biederman 	}
179461a28784SEric W. Biederman 	dput(child);
179561a28784SEric W. Biederman end_instantiate:
179661a28784SEric W. Biederman 	if (!ino)
179761a28784SEric W. Biederman 		ino = find_inode_number(dir, &qname);
179861a28784SEric W. Biederman 	if (!ino)
179961a28784SEric W. Biederman 		ino = 1;
180061a28784SEric W. Biederman 	return filldir(dirent, name, len, filp->f_pos, ino, type);
180161a28784SEric W. Biederman }
180261a28784SEric W. Biederman 
180328a6d671SEric W. Biederman static unsigned name_to_int(struct dentry *dentry)
180428a6d671SEric W. Biederman {
180528a6d671SEric W. Biederman 	const char *name = dentry->d_name.name;
180628a6d671SEric W. Biederman 	int len = dentry->d_name.len;
180728a6d671SEric W. Biederman 	unsigned n = 0;
180828a6d671SEric W. Biederman 
180928a6d671SEric W. Biederman 	if (len > 1 && *name == '0')
181028a6d671SEric W. Biederman 		goto out;
181128a6d671SEric W. Biederman 	while (len-- > 0) {
181228a6d671SEric W. Biederman 		unsigned c = *name++ - '0';
181328a6d671SEric W. Biederman 		if (c > 9)
181428a6d671SEric W. Biederman 			goto out;
181528a6d671SEric W. Biederman 		if (n >= (~0U-9)/10)
181628a6d671SEric W. Biederman 			goto out;
181728a6d671SEric W. Biederman 		n *= 10;
181828a6d671SEric W. Biederman 		n += c;
181928a6d671SEric W. Biederman 	}
182028a6d671SEric W. Biederman 	return n;
182128a6d671SEric W. Biederman out:
182228a6d671SEric W. Biederman 	return ~0U;
182328a6d671SEric W. Biederman }
182428a6d671SEric W. Biederman 
182527932742SMiklos Szeredi #define PROC_FDINFO_MAX 64
182627932742SMiklos Szeredi 
18273dcd25f3SJan Blunck static int proc_fd_info(struct inode *inode, struct path *path, char *info)
182828a6d671SEric W. Biederman {
182928a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
183028a6d671SEric W. Biederman 	struct files_struct *files = NULL;
183128a6d671SEric W. Biederman 	struct file *file;
183228a6d671SEric W. Biederman 	int fd = proc_fd(inode);
183328a6d671SEric W. Biederman 
183428a6d671SEric W. Biederman 	if (task) {
183528a6d671SEric W. Biederman 		files = get_files_struct(task);
183628a6d671SEric W. Biederman 		put_task_struct(task);
183728a6d671SEric W. Biederman 	}
183828a6d671SEric W. Biederman 	if (files) {
183928a6d671SEric W. Biederman 		/*
184028a6d671SEric W. Biederman 		 * We are not taking a ref to the file structure, so we must
184128a6d671SEric W. Biederman 		 * hold ->file_lock.
184228a6d671SEric W. Biederman 		 */
184328a6d671SEric W. Biederman 		spin_lock(&files->file_lock);
184428a6d671SEric W. Biederman 		file = fcheck_files(files, fd);
184528a6d671SEric W. Biederman 		if (file) {
18463dcd25f3SJan Blunck 			if (path) {
18473dcd25f3SJan Blunck 				*path = file->f_path;
18483dcd25f3SJan Blunck 				path_get(&file->f_path);
18493dcd25f3SJan Blunck 			}
185027932742SMiklos Szeredi 			if (info)
185127932742SMiklos Szeredi 				snprintf(info, PROC_FDINFO_MAX,
185227932742SMiklos Szeredi 					 "pos:\t%lli\n"
185327932742SMiklos Szeredi 					 "flags:\t0%o\n",
185427932742SMiklos Szeredi 					 (long long) file->f_pos,
185527932742SMiklos Szeredi 					 file->f_flags);
185628a6d671SEric W. Biederman 			spin_unlock(&files->file_lock);
185728a6d671SEric W. Biederman 			put_files_struct(files);
185828a6d671SEric W. Biederman 			return 0;
185928a6d671SEric W. Biederman 		}
186028a6d671SEric W. Biederman 		spin_unlock(&files->file_lock);
186128a6d671SEric W. Biederman 		put_files_struct(files);
186228a6d671SEric W. Biederman 	}
186328a6d671SEric W. Biederman 	return -ENOENT;
186428a6d671SEric W. Biederman }
186528a6d671SEric W. Biederman 
18663dcd25f3SJan Blunck static int proc_fd_link(struct inode *inode, struct path *path)
186727932742SMiklos Szeredi {
18683dcd25f3SJan Blunck 	return proc_fd_info(inode, path, NULL);
186927932742SMiklos Szeredi }
187027932742SMiklos Szeredi 
187128a6d671SEric W. Biederman static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
187228a6d671SEric W. Biederman {
187328a6d671SEric W. Biederman 	struct inode *inode = dentry->d_inode;
187428a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
187528a6d671SEric W. Biederman 	int fd = proc_fd(inode);
187628a6d671SEric W. Biederman 	struct files_struct *files;
1877c69e8d9cSDavid Howells 	const struct cred *cred;
187828a6d671SEric W. Biederman 
187928a6d671SEric W. Biederman 	if (task) {
188028a6d671SEric W. Biederman 		files = get_files_struct(task);
188128a6d671SEric W. Biederman 		if (files) {
188228a6d671SEric W. Biederman 			rcu_read_lock();
188328a6d671SEric W. Biederman 			if (fcheck_files(files, fd)) {
188428a6d671SEric W. Biederman 				rcu_read_unlock();
188528a6d671SEric W. Biederman 				put_files_struct(files);
188628a6d671SEric W. Biederman 				if (task_dumpable(task)) {
1887c69e8d9cSDavid Howells 					rcu_read_lock();
1888c69e8d9cSDavid Howells 					cred = __task_cred(task);
1889c69e8d9cSDavid Howells 					inode->i_uid = cred->euid;
1890c69e8d9cSDavid Howells 					inode->i_gid = cred->egid;
1891c69e8d9cSDavid Howells 					rcu_read_unlock();
189228a6d671SEric W. Biederman 				} else {
189328a6d671SEric W. Biederman 					inode->i_uid = 0;
189428a6d671SEric W. Biederman 					inode->i_gid = 0;
189528a6d671SEric W. Biederman 				}
189628a6d671SEric W. Biederman 				inode->i_mode &= ~(S_ISUID | S_ISGID);
189728a6d671SEric W. Biederman 				security_task_to_inode(task, inode);
189828a6d671SEric W. Biederman 				put_task_struct(task);
189928a6d671SEric W. Biederman 				return 1;
190028a6d671SEric W. Biederman 			}
190128a6d671SEric W. Biederman 			rcu_read_unlock();
190228a6d671SEric W. Biederman 			put_files_struct(files);
190328a6d671SEric W. Biederman 		}
190428a6d671SEric W. Biederman 		put_task_struct(task);
190528a6d671SEric W. Biederman 	}
190628a6d671SEric W. Biederman 	d_drop(dentry);
190728a6d671SEric W. Biederman 	return 0;
190828a6d671SEric W. Biederman }
190928a6d671SEric W. Biederman 
1910d72f71ebSAl Viro static const struct dentry_operations tid_fd_dentry_operations =
191128a6d671SEric W. Biederman {
191228a6d671SEric W. Biederman 	.d_revalidate	= tid_fd_revalidate,
191328a6d671SEric W. Biederman 	.d_delete	= pid_delete_dentry,
191428a6d671SEric W. Biederman };
191528a6d671SEric W. Biederman 
1916444ceed8SEric W. Biederman static struct dentry *proc_fd_instantiate(struct inode *dir,
1917c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
191828a6d671SEric W. Biederman {
1919c5141e6dSEric Dumazet 	unsigned fd = *(const unsigned *)ptr;
192028a6d671SEric W. Biederman 	struct file *file;
192128a6d671SEric W. Biederman 	struct files_struct *files;
192228a6d671SEric W. Biederman  	struct inode *inode;
192328a6d671SEric W. Biederman  	struct proc_inode *ei;
1924444ceed8SEric W. Biederman 	struct dentry *error = ERR_PTR(-ENOENT);
192528a6d671SEric W. Biederman 
192661a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
192728a6d671SEric W. Biederman 	if (!inode)
192828a6d671SEric W. Biederman 		goto out;
192928a6d671SEric W. Biederman 	ei = PROC_I(inode);
193028a6d671SEric W. Biederman 	ei->fd = fd;
193128a6d671SEric W. Biederman 	files = get_files_struct(task);
193228a6d671SEric W. Biederman 	if (!files)
1933444ceed8SEric W. Biederman 		goto out_iput;
193428a6d671SEric W. Biederman 	inode->i_mode = S_IFLNK;
193528a6d671SEric W. Biederman 
193628a6d671SEric W. Biederman 	/*
193728a6d671SEric W. Biederman 	 * We are not taking a ref to the file structure, so we must
193828a6d671SEric W. Biederman 	 * hold ->file_lock.
193928a6d671SEric W. Biederman 	 */
194028a6d671SEric W. Biederman 	spin_lock(&files->file_lock);
194128a6d671SEric W. Biederman 	file = fcheck_files(files, fd);
194228a6d671SEric W. Biederman 	if (!file)
1943444ceed8SEric W. Biederman 		goto out_unlock;
1944aeb5d727SAl Viro 	if (file->f_mode & FMODE_READ)
194528a6d671SEric W. Biederman 		inode->i_mode |= S_IRUSR | S_IXUSR;
1946aeb5d727SAl Viro 	if (file->f_mode & FMODE_WRITE)
194728a6d671SEric W. Biederman 		inode->i_mode |= S_IWUSR | S_IXUSR;
194828a6d671SEric W. Biederman 	spin_unlock(&files->file_lock);
194928a6d671SEric W. Biederman 	put_files_struct(files);
1950444ceed8SEric W. Biederman 
195128a6d671SEric W. Biederman 	inode->i_op = &proc_pid_link_inode_operations;
195228a6d671SEric W. Biederman 	inode->i_size = 64;
195328a6d671SEric W. Biederman 	ei->op.proc_get_link = proc_fd_link;
195428a6d671SEric W. Biederman 	dentry->d_op = &tid_fd_dentry_operations;
195528a6d671SEric W. Biederman 	d_add(dentry, inode);
195628a6d671SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
195728a6d671SEric W. Biederman 	if (tid_fd_revalidate(dentry, NULL))
1958444ceed8SEric W. Biederman 		error = NULL;
1959444ceed8SEric W. Biederman 
1960444ceed8SEric W. Biederman  out:
1961444ceed8SEric W. Biederman 	return error;
1962444ceed8SEric W. Biederman out_unlock:
1963444ceed8SEric W. Biederman 	spin_unlock(&files->file_lock);
1964444ceed8SEric W. Biederman 	put_files_struct(files);
1965444ceed8SEric W. Biederman out_iput:
1966444ceed8SEric W. Biederman 	iput(inode);
1967444ceed8SEric W. Biederman 	goto out;
1968444ceed8SEric W. Biederman }
1969444ceed8SEric W. Biederman 
197027932742SMiklos Szeredi static struct dentry *proc_lookupfd_common(struct inode *dir,
197127932742SMiklos Szeredi 					   struct dentry *dentry,
197227932742SMiklos Szeredi 					   instantiate_t instantiate)
1973444ceed8SEric W. Biederman {
1974444ceed8SEric W. Biederman 	struct task_struct *task = get_proc_task(dir);
1975444ceed8SEric W. Biederman 	unsigned fd = name_to_int(dentry);
1976444ceed8SEric W. Biederman 	struct dentry *result = ERR_PTR(-ENOENT);
1977444ceed8SEric W. Biederman 
1978444ceed8SEric W. Biederman 	if (!task)
1979444ceed8SEric W. Biederman 		goto out_no_task;
1980444ceed8SEric W. Biederman 	if (fd == ~0U)
1981444ceed8SEric W. Biederman 		goto out;
1982444ceed8SEric W. Biederman 
198327932742SMiklos Szeredi 	result = instantiate(dir, dentry, task, &fd);
198428a6d671SEric W. Biederman out:
198528a6d671SEric W. Biederman 	put_task_struct(task);
198628a6d671SEric W. Biederman out_no_task:
198728a6d671SEric W. Biederman 	return result;
198828a6d671SEric W. Biederman }
198928a6d671SEric W. Biederman 
199027932742SMiklos Szeredi static int proc_readfd_common(struct file * filp, void * dirent,
199127932742SMiklos Szeredi 			      filldir_t filldir, instantiate_t instantiate)
19921da177e4SLinus Torvalds {
19932fddfeefSJosef "Jeff" Sipek 	struct dentry *dentry = filp->f_path.dentry;
19945634708bSEric W. Biederman 	struct inode *inode = dentry->d_inode;
199599f89551SEric W. Biederman 	struct task_struct *p = get_proc_task(inode);
1996457c2510SPavel Emelyanov 	unsigned int fd, ino;
19971da177e4SLinus Torvalds 	int retval;
19981da177e4SLinus Torvalds 	struct files_struct * files;
19991da177e4SLinus Torvalds 
20001da177e4SLinus Torvalds 	retval = -ENOENT;
200199f89551SEric W. Biederman 	if (!p)
200299f89551SEric W. Biederman 		goto out_no_task;
20031da177e4SLinus Torvalds 	retval = 0;
20041da177e4SLinus Torvalds 
20051da177e4SLinus Torvalds 	fd = filp->f_pos;
20061da177e4SLinus Torvalds 	switch (fd) {
20071da177e4SLinus Torvalds 		case 0:
20081da177e4SLinus Torvalds 			if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
20091da177e4SLinus Torvalds 				goto out;
20101da177e4SLinus Torvalds 			filp->f_pos++;
20111da177e4SLinus Torvalds 		case 1:
20125634708bSEric W. Biederman 			ino = parent_ino(dentry);
20131da177e4SLinus Torvalds 			if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
20141da177e4SLinus Torvalds 				goto out;
20151da177e4SLinus Torvalds 			filp->f_pos++;
20161da177e4SLinus Torvalds 		default:
20171da177e4SLinus Torvalds 			files = get_files_struct(p);
20181da177e4SLinus Torvalds 			if (!files)
20191da177e4SLinus Torvalds 				goto out;
2020b835996fSDipankar Sarma 			rcu_read_lock();
20211da177e4SLinus Torvalds 			for (fd = filp->f_pos-2;
20229b4f526cSAl Viro 			     fd < files_fdtable(files)->max_fds;
20231da177e4SLinus Torvalds 			     fd++, filp->f_pos++) {
202427932742SMiklos Szeredi 				char name[PROC_NUMBUF];
202527932742SMiklos Szeredi 				int len;
20261da177e4SLinus Torvalds 
20271da177e4SLinus Torvalds 				if (!fcheck_files(files, fd))
20281da177e4SLinus Torvalds 					continue;
2029b835996fSDipankar Sarma 				rcu_read_unlock();
20301da177e4SLinus Torvalds 
203127932742SMiklos Szeredi 				len = snprintf(name, sizeof(name), "%d", fd);
203227932742SMiklos Szeredi 				if (proc_fill_cache(filp, dirent, filldir,
203327932742SMiklos Szeredi 						    name, len, instantiate,
203427932742SMiklos Szeredi 						    p, &fd) < 0) {
2035b835996fSDipankar Sarma 					rcu_read_lock();
20361da177e4SLinus Torvalds 					break;
20371da177e4SLinus Torvalds 				}
2038b835996fSDipankar Sarma 				rcu_read_lock();
20391da177e4SLinus Torvalds 			}
2040b835996fSDipankar Sarma 			rcu_read_unlock();
20411da177e4SLinus Torvalds 			put_files_struct(files);
20421da177e4SLinus Torvalds 	}
20431da177e4SLinus Torvalds out:
204499f89551SEric W. Biederman 	put_task_struct(p);
204599f89551SEric W. Biederman out_no_task:
20461da177e4SLinus Torvalds 	return retval;
20471da177e4SLinus Torvalds }
20481da177e4SLinus Torvalds 
204927932742SMiklos Szeredi static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
205027932742SMiklos Szeredi 				    struct nameidata *nd)
205127932742SMiklos Szeredi {
205227932742SMiklos Szeredi 	return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
205327932742SMiklos Szeredi }
205427932742SMiklos Szeredi 
205527932742SMiklos Szeredi static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
205627932742SMiklos Szeredi {
205727932742SMiklos Szeredi 	return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
205827932742SMiklos Szeredi }
205927932742SMiklos Szeredi 
206027932742SMiklos Szeredi static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
206127932742SMiklos Szeredi 				      size_t len, loff_t *ppos)
206227932742SMiklos Szeredi {
206327932742SMiklos Szeredi 	char tmp[PROC_FDINFO_MAX];
20643dcd25f3SJan Blunck 	int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
206527932742SMiklos Szeredi 	if (!err)
206627932742SMiklos Szeredi 		err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
206727932742SMiklos Szeredi 	return err;
206827932742SMiklos Szeredi }
206927932742SMiklos Szeredi 
207027932742SMiklos Szeredi static const struct file_operations proc_fdinfo_file_operations = {
207127932742SMiklos Szeredi 	.open           = nonseekable_open,
207227932742SMiklos Szeredi 	.read		= proc_fdinfo_read,
20736038f373SArnd Bergmann 	.llseek		= no_llseek,
207427932742SMiklos Szeredi };
207527932742SMiklos Szeredi 
207600977a59SArjan van de Ven static const struct file_operations proc_fd_operations = {
20771da177e4SLinus Torvalds 	.read		= generic_read_dir,
20781da177e4SLinus Torvalds 	.readdir	= proc_readfd,
20796038f373SArnd Bergmann 	.llseek		= default_llseek,
20801da177e4SLinus Torvalds };
20811da177e4SLinus Torvalds 
20821da177e4SLinus Torvalds /*
20838948e11fSAlexey Dobriyan  * /proc/pid/fd needs a special permission handler so that a process can still
20848948e11fSAlexey Dobriyan  * access /proc/self/fd after it has executed a setuid().
20858948e11fSAlexey Dobriyan  */
2086e6305c43SAl Viro static int proc_fd_permission(struct inode *inode, int mask)
20878948e11fSAlexey Dobriyan {
20888948e11fSAlexey Dobriyan 	int rv;
20898948e11fSAlexey Dobriyan 
20908948e11fSAlexey Dobriyan 	rv = generic_permission(inode, mask, NULL);
20918948e11fSAlexey Dobriyan 	if (rv == 0)
20928948e11fSAlexey Dobriyan 		return 0;
20938948e11fSAlexey Dobriyan 	if (task_pid(current) == proc_pid(inode))
20948948e11fSAlexey Dobriyan 		rv = 0;
20958948e11fSAlexey Dobriyan 	return rv;
20968948e11fSAlexey Dobriyan }
20978948e11fSAlexey Dobriyan 
20988948e11fSAlexey Dobriyan /*
20991da177e4SLinus Torvalds  * proc directories can do almost nothing..
21001da177e4SLinus Torvalds  */
2101c5ef1c42SArjan van de Ven static const struct inode_operations proc_fd_inode_operations = {
21021da177e4SLinus Torvalds 	.lookup		= proc_lookupfd,
21038948e11fSAlexey Dobriyan 	.permission	= proc_fd_permission,
21046d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
21051da177e4SLinus Torvalds };
21061da177e4SLinus Torvalds 
210727932742SMiklos Szeredi static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
210827932742SMiklos Szeredi 	struct dentry *dentry, struct task_struct *task, const void *ptr)
210927932742SMiklos Szeredi {
211027932742SMiklos Szeredi 	unsigned fd = *(unsigned *)ptr;
211127932742SMiklos Szeredi  	struct inode *inode;
211227932742SMiklos Szeredi  	struct proc_inode *ei;
211327932742SMiklos Szeredi 	struct dentry *error = ERR_PTR(-ENOENT);
211427932742SMiklos Szeredi 
211527932742SMiklos Szeredi 	inode = proc_pid_make_inode(dir->i_sb, task);
211627932742SMiklos Szeredi 	if (!inode)
211727932742SMiklos Szeredi 		goto out;
211827932742SMiklos Szeredi 	ei = PROC_I(inode);
211927932742SMiklos Szeredi 	ei->fd = fd;
212027932742SMiklos Szeredi 	inode->i_mode = S_IFREG | S_IRUSR;
212127932742SMiklos Szeredi 	inode->i_fop = &proc_fdinfo_file_operations;
212227932742SMiklos Szeredi 	dentry->d_op = &tid_fd_dentry_operations;
212327932742SMiklos Szeredi 	d_add(dentry, inode);
212427932742SMiklos Szeredi 	/* Close the race of the process dying before we return the dentry */
212527932742SMiklos Szeredi 	if (tid_fd_revalidate(dentry, NULL))
212627932742SMiklos Szeredi 		error = NULL;
212727932742SMiklos Szeredi 
212827932742SMiklos Szeredi  out:
212927932742SMiklos Szeredi 	return error;
213027932742SMiklos Szeredi }
213127932742SMiklos Szeredi 
213227932742SMiklos Szeredi static struct dentry *proc_lookupfdinfo(struct inode *dir,
213327932742SMiklos Szeredi 					struct dentry *dentry,
213427932742SMiklos Szeredi 					struct nameidata *nd)
213527932742SMiklos Szeredi {
213627932742SMiklos Szeredi 	return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
213727932742SMiklos Szeredi }
213827932742SMiklos Szeredi 
213927932742SMiklos Szeredi static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
214027932742SMiklos Szeredi {
214127932742SMiklos Szeredi 	return proc_readfd_common(filp, dirent, filldir,
214227932742SMiklos Szeredi 				  proc_fdinfo_instantiate);
214327932742SMiklos Szeredi }
214427932742SMiklos Szeredi 
214527932742SMiklos Szeredi static const struct file_operations proc_fdinfo_operations = {
214627932742SMiklos Szeredi 	.read		= generic_read_dir,
214727932742SMiklos Szeredi 	.readdir	= proc_readfdinfo,
21486038f373SArnd Bergmann 	.llseek		= default_llseek,
214927932742SMiklos Szeredi };
215027932742SMiklos Szeredi 
215127932742SMiklos Szeredi /*
215227932742SMiklos Szeredi  * proc directories can do almost nothing..
215327932742SMiklos Szeredi  */
215427932742SMiklos Szeredi static const struct inode_operations proc_fdinfo_inode_operations = {
215527932742SMiklos Szeredi 	.lookup		= proc_lookupfdinfo,
215627932742SMiklos Szeredi 	.setattr	= proc_setattr,
215727932742SMiklos Szeredi };
215827932742SMiklos Szeredi 
215927932742SMiklos Szeredi 
2160444ceed8SEric W. Biederman static struct dentry *proc_pident_instantiate(struct inode *dir,
2161c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
2162444ceed8SEric W. Biederman {
2163c5141e6dSEric Dumazet 	const struct pid_entry *p = ptr;
2164444ceed8SEric W. Biederman 	struct inode *inode;
2165444ceed8SEric W. Biederman 	struct proc_inode *ei;
2166bd6daba9SKOSAKI Motohiro 	struct dentry *error = ERR_PTR(-ENOENT);
2167444ceed8SEric W. Biederman 
216861a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
2169444ceed8SEric W. Biederman 	if (!inode)
2170444ceed8SEric W. Biederman 		goto out;
2171444ceed8SEric W. Biederman 
2172444ceed8SEric W. Biederman 	ei = PROC_I(inode);
2173444ceed8SEric W. Biederman 	inode->i_mode = p->mode;
2174444ceed8SEric W. Biederman 	if (S_ISDIR(inode->i_mode))
2175444ceed8SEric W. Biederman 		inode->i_nlink = 2;	/* Use getattr to fix if necessary */
2176444ceed8SEric W. Biederman 	if (p->iop)
2177444ceed8SEric W. Biederman 		inode->i_op = p->iop;
2178444ceed8SEric W. Biederman 	if (p->fop)
2179444ceed8SEric W. Biederman 		inode->i_fop = p->fop;
2180444ceed8SEric W. Biederman 	ei->op = p->op;
2181444ceed8SEric W. Biederman 	dentry->d_op = &pid_dentry_operations;
2182444ceed8SEric W. Biederman 	d_add(dentry, inode);
2183444ceed8SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
2184444ceed8SEric W. Biederman 	if (pid_revalidate(dentry, NULL))
2185444ceed8SEric W. Biederman 		error = NULL;
2186444ceed8SEric W. Biederman out:
2187444ceed8SEric W. Biederman 	return error;
2188444ceed8SEric W. Biederman }
2189444ceed8SEric W. Biederman 
21901da177e4SLinus Torvalds static struct dentry *proc_pident_lookup(struct inode *dir,
21911da177e4SLinus Torvalds 					 struct dentry *dentry,
2192c5141e6dSEric Dumazet 					 const struct pid_entry *ents,
21937bcd6b0eSEric W. Biederman 					 unsigned int nents)
21941da177e4SLinus Torvalds {
2195cd6a3ce9SEric W. Biederman 	struct dentry *error;
219699f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(dir);
2197c5141e6dSEric Dumazet 	const struct pid_entry *p, *last;
21981da177e4SLinus Torvalds 
2199cd6a3ce9SEric W. Biederman 	error = ERR_PTR(-ENOENT);
22001da177e4SLinus Torvalds 
220199f89551SEric W. Biederman 	if (!task)
220299f89551SEric W. Biederman 		goto out_no_task;
22031da177e4SLinus Torvalds 
220420cdc894SEric W. Biederman 	/*
220520cdc894SEric W. Biederman 	 * Yes, it does not scale. And it should not. Don't add
220620cdc894SEric W. Biederman 	 * new entries into /proc/<tgid>/ without very good reasons.
220720cdc894SEric W. Biederman 	 */
22087bcd6b0eSEric W. Biederman 	last = &ents[nents - 1];
22097bcd6b0eSEric W. Biederman 	for (p = ents; p <= last; p++) {
22101da177e4SLinus Torvalds 		if (p->len != dentry->d_name.len)
22111da177e4SLinus Torvalds 			continue;
22121da177e4SLinus Torvalds 		if (!memcmp(dentry->d_name.name, p->name, p->len))
22131da177e4SLinus Torvalds 			break;
22141da177e4SLinus Torvalds 	}
22157bcd6b0eSEric W. Biederman 	if (p > last)
22161da177e4SLinus Torvalds 		goto out;
22171da177e4SLinus Torvalds 
2218444ceed8SEric W. Biederman 	error = proc_pident_instantiate(dir, dentry, task, p);
22191da177e4SLinus Torvalds out:
222099f89551SEric W. Biederman 	put_task_struct(task);
222199f89551SEric W. Biederman out_no_task:
2222cd6a3ce9SEric W. Biederman 	return error;
22231da177e4SLinus Torvalds }
22241da177e4SLinus Torvalds 
2225c5141e6dSEric Dumazet static int proc_pident_fill_cache(struct file *filp, void *dirent,
2226c5141e6dSEric Dumazet 	filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
222761a28784SEric W. Biederman {
222861a28784SEric W. Biederman 	return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
222961a28784SEric W. Biederman 				proc_pident_instantiate, task, p);
223061a28784SEric W. Biederman }
223161a28784SEric W. Biederman 
223228a6d671SEric W. Biederman static int proc_pident_readdir(struct file *filp,
223328a6d671SEric W. Biederman 		void *dirent, filldir_t filldir,
2234c5141e6dSEric Dumazet 		const struct pid_entry *ents, unsigned int nents)
223528a6d671SEric W. Biederman {
223628a6d671SEric W. Biederman 	int i;
22372fddfeefSJosef "Jeff" Sipek 	struct dentry *dentry = filp->f_path.dentry;
223828a6d671SEric W. Biederman 	struct inode *inode = dentry->d_inode;
223928a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
2240c5141e6dSEric Dumazet 	const struct pid_entry *p, *last;
224128a6d671SEric W. Biederman 	ino_t ino;
224228a6d671SEric W. Biederman 	int ret;
224328a6d671SEric W. Biederman 
224428a6d671SEric W. Biederman 	ret = -ENOENT;
224528a6d671SEric W. Biederman 	if (!task)
224661a28784SEric W. Biederman 		goto out_no_task;
224728a6d671SEric W. Biederman 
224828a6d671SEric W. Biederman 	ret = 0;
224928a6d671SEric W. Biederman 	i = filp->f_pos;
225028a6d671SEric W. Biederman 	switch (i) {
225128a6d671SEric W. Biederman 	case 0:
225228a6d671SEric W. Biederman 		ino = inode->i_ino;
225328a6d671SEric W. Biederman 		if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
225428a6d671SEric W. Biederman 			goto out;
225528a6d671SEric W. Biederman 		i++;
225628a6d671SEric W. Biederman 		filp->f_pos++;
225728a6d671SEric W. Biederman 		/* fall through */
225828a6d671SEric W. Biederman 	case 1:
225928a6d671SEric W. Biederman 		ino = parent_ino(dentry);
226028a6d671SEric W. Biederman 		if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
226128a6d671SEric W. Biederman 			goto out;
226228a6d671SEric W. Biederman 		i++;
226328a6d671SEric W. Biederman 		filp->f_pos++;
226428a6d671SEric W. Biederman 		/* fall through */
226528a6d671SEric W. Biederman 	default:
226628a6d671SEric W. Biederman 		i -= 2;
226728a6d671SEric W. Biederman 		if (i >= nents) {
226828a6d671SEric W. Biederman 			ret = 1;
226928a6d671SEric W. Biederman 			goto out;
227028a6d671SEric W. Biederman 		}
227128a6d671SEric W. Biederman 		p = ents + i;
22727bcd6b0eSEric W. Biederman 		last = &ents[nents - 1];
22737bcd6b0eSEric W. Biederman 		while (p <= last) {
227461a28784SEric W. Biederman 			if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
227528a6d671SEric W. Biederman 				goto out;
227628a6d671SEric W. Biederman 			filp->f_pos++;
227728a6d671SEric W. Biederman 			p++;
227828a6d671SEric W. Biederman 		}
22791da177e4SLinus Torvalds 	}
22801da177e4SLinus Torvalds 
228128a6d671SEric W. Biederman 	ret = 1;
228228a6d671SEric W. Biederman out:
228361a28784SEric W. Biederman 	put_task_struct(task);
228461a28784SEric W. Biederman out_no_task:
228528a6d671SEric W. Biederman 	return ret;
22861da177e4SLinus Torvalds }
22871da177e4SLinus Torvalds 
22881da177e4SLinus Torvalds #ifdef CONFIG_SECURITY
228928a6d671SEric W. Biederman static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
229028a6d671SEric W. Biederman 				  size_t count, loff_t *ppos)
229128a6d671SEric W. Biederman {
22922fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
229304ff9708SAl Viro 	char *p = NULL;
229428a6d671SEric W. Biederman 	ssize_t length;
229528a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
229628a6d671SEric W. Biederman 
229728a6d671SEric W. Biederman 	if (!task)
229804ff9708SAl Viro 		return -ESRCH;
229928a6d671SEric W. Biederman 
230028a6d671SEric W. Biederman 	length = security_getprocattr(task,
23012fddfeefSJosef "Jeff" Sipek 				      (char*)file->f_path.dentry->d_name.name,
230204ff9708SAl Viro 				      &p);
230328a6d671SEric W. Biederman 	put_task_struct(task);
230404ff9708SAl Viro 	if (length > 0)
230504ff9708SAl Viro 		length = simple_read_from_buffer(buf, count, ppos, p, length);
230604ff9708SAl Viro 	kfree(p);
230728a6d671SEric W. Biederman 	return length;
230828a6d671SEric W. Biederman }
230928a6d671SEric W. Biederman 
231028a6d671SEric W. Biederman static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
231128a6d671SEric W. Biederman 				   size_t count, loff_t *ppos)
231228a6d671SEric W. Biederman {
23132fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
231428a6d671SEric W. Biederman 	char *page;
231528a6d671SEric W. Biederman 	ssize_t length;
231628a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
231728a6d671SEric W. Biederman 
231828a6d671SEric W. Biederman 	length = -ESRCH;
231928a6d671SEric W. Biederman 	if (!task)
232028a6d671SEric W. Biederman 		goto out_no_task;
232128a6d671SEric W. Biederman 	if (count > PAGE_SIZE)
232228a6d671SEric W. Biederman 		count = PAGE_SIZE;
232328a6d671SEric W. Biederman 
232428a6d671SEric W. Biederman 	/* No partial writes. */
232528a6d671SEric W. Biederman 	length = -EINVAL;
232628a6d671SEric W. Biederman 	if (*ppos != 0)
232728a6d671SEric W. Biederman 		goto out;
232828a6d671SEric W. Biederman 
232928a6d671SEric W. Biederman 	length = -ENOMEM;
2330e12ba74dSMel Gorman 	page = (char*)__get_free_page(GFP_TEMPORARY);
233128a6d671SEric W. Biederman 	if (!page)
233228a6d671SEric W. Biederman 		goto out;
233328a6d671SEric W. Biederman 
233428a6d671SEric W. Biederman 	length = -EFAULT;
233528a6d671SEric W. Biederman 	if (copy_from_user(page, buf, count))
233628a6d671SEric W. Biederman 		goto out_free;
233728a6d671SEric W. Biederman 
2338107db7c7SDavid Howells 	/* Guard against adverse ptrace interaction */
2339107db7c7SDavid Howells 	length = mutex_lock_interruptible(&task->cred_guard_mutex);
2340107db7c7SDavid Howells 	if (length < 0)
2341107db7c7SDavid Howells 		goto out_free;
2342107db7c7SDavid Howells 
234328a6d671SEric W. Biederman 	length = security_setprocattr(task,
23442fddfeefSJosef "Jeff" Sipek 				      (char*)file->f_path.dentry->d_name.name,
234528a6d671SEric W. Biederman 				      (void*)page, count);
2346107db7c7SDavid Howells 	mutex_unlock(&task->cred_guard_mutex);
234728a6d671SEric W. Biederman out_free:
234828a6d671SEric W. Biederman 	free_page((unsigned long) page);
234928a6d671SEric W. Biederman out:
235028a6d671SEric W. Biederman 	put_task_struct(task);
235128a6d671SEric W. Biederman out_no_task:
235228a6d671SEric W. Biederman 	return length;
235328a6d671SEric W. Biederman }
235428a6d671SEric W. Biederman 
235500977a59SArjan van de Ven static const struct file_operations proc_pid_attr_operations = {
235628a6d671SEric W. Biederman 	.read		= proc_pid_attr_read,
235728a6d671SEric W. Biederman 	.write		= proc_pid_attr_write,
235887df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
235928a6d671SEric W. Biederman };
236028a6d671SEric W. Biederman 
2361c5141e6dSEric Dumazet static const struct pid_entry attr_dir_stuff[] = {
2362631f9c18SAlexey Dobriyan 	REG("current",    S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2363631f9c18SAlexey Dobriyan 	REG("prev",       S_IRUGO,	   proc_pid_attr_operations),
2364631f9c18SAlexey Dobriyan 	REG("exec",       S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2365631f9c18SAlexey Dobriyan 	REG("fscreate",   S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2366631f9c18SAlexey Dobriyan 	REG("keycreate",  S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2367631f9c18SAlexey Dobriyan 	REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
236828a6d671SEric W. Biederman };
236928a6d671SEric W. Biederman 
237072d9dcfcSEric W. Biederman static int proc_attr_dir_readdir(struct file * filp,
23711da177e4SLinus Torvalds 			     void * dirent, filldir_t filldir)
23721da177e4SLinus Torvalds {
23731da177e4SLinus Torvalds 	return proc_pident_readdir(filp,dirent,filldir,
237472d9dcfcSEric W. Biederman 				   attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
23751da177e4SLinus Torvalds }
23761da177e4SLinus Torvalds 
237700977a59SArjan van de Ven static const struct file_operations proc_attr_dir_operations = {
23781da177e4SLinus Torvalds 	.read		= generic_read_dir,
237972d9dcfcSEric W. Biederman 	.readdir	= proc_attr_dir_readdir,
23806038f373SArnd Bergmann 	.llseek		= default_llseek,
23811da177e4SLinus Torvalds };
23821da177e4SLinus Torvalds 
238372d9dcfcSEric W. Biederman static struct dentry *proc_attr_dir_lookup(struct inode *dir,
23841da177e4SLinus Torvalds 				struct dentry *dentry, struct nameidata *nd)
23851da177e4SLinus Torvalds {
23867bcd6b0eSEric W. Biederman 	return proc_pident_lookup(dir, dentry,
23877bcd6b0eSEric W. Biederman 				  attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
23881da177e4SLinus Torvalds }
23891da177e4SLinus Torvalds 
2390c5ef1c42SArjan van de Ven static const struct inode_operations proc_attr_dir_inode_operations = {
239172d9dcfcSEric W. Biederman 	.lookup		= proc_attr_dir_lookup,
239299f89551SEric W. Biederman 	.getattr	= pid_getattr,
23936d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
23941da177e4SLinus Torvalds };
23951da177e4SLinus Torvalds 
23961da177e4SLinus Torvalds #endif
23971da177e4SLinus Torvalds 
2398698ba7b5SChristoph Hellwig #ifdef CONFIG_ELF_CORE
23993cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
24003cb4a0bbSKawai, Hidehiro 					 size_t count, loff_t *ppos)
24013cb4a0bbSKawai, Hidehiro {
24023cb4a0bbSKawai, Hidehiro 	struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
24033cb4a0bbSKawai, Hidehiro 	struct mm_struct *mm;
24043cb4a0bbSKawai, Hidehiro 	char buffer[PROC_NUMBUF];
24053cb4a0bbSKawai, Hidehiro 	size_t len;
24063cb4a0bbSKawai, Hidehiro 	int ret;
24073cb4a0bbSKawai, Hidehiro 
24083cb4a0bbSKawai, Hidehiro 	if (!task)
24093cb4a0bbSKawai, Hidehiro 		return -ESRCH;
24103cb4a0bbSKawai, Hidehiro 
24113cb4a0bbSKawai, Hidehiro 	ret = 0;
24123cb4a0bbSKawai, Hidehiro 	mm = get_task_mm(task);
24133cb4a0bbSKawai, Hidehiro 	if (mm) {
24143cb4a0bbSKawai, Hidehiro 		len = snprintf(buffer, sizeof(buffer), "%08lx\n",
24153cb4a0bbSKawai, Hidehiro 			       ((mm->flags & MMF_DUMP_FILTER_MASK) >>
24163cb4a0bbSKawai, Hidehiro 				MMF_DUMP_FILTER_SHIFT));
24173cb4a0bbSKawai, Hidehiro 		mmput(mm);
24183cb4a0bbSKawai, Hidehiro 		ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
24193cb4a0bbSKawai, Hidehiro 	}
24203cb4a0bbSKawai, Hidehiro 
24213cb4a0bbSKawai, Hidehiro 	put_task_struct(task);
24223cb4a0bbSKawai, Hidehiro 
24233cb4a0bbSKawai, Hidehiro 	return ret;
24243cb4a0bbSKawai, Hidehiro }
24253cb4a0bbSKawai, Hidehiro 
24263cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_write(struct file *file,
24273cb4a0bbSKawai, Hidehiro 					  const char __user *buf,
24283cb4a0bbSKawai, Hidehiro 					  size_t count,
24293cb4a0bbSKawai, Hidehiro 					  loff_t *ppos)
24303cb4a0bbSKawai, Hidehiro {
24313cb4a0bbSKawai, Hidehiro 	struct task_struct *task;
24323cb4a0bbSKawai, Hidehiro 	struct mm_struct *mm;
24333cb4a0bbSKawai, Hidehiro 	char buffer[PROC_NUMBUF], *end;
24343cb4a0bbSKawai, Hidehiro 	unsigned int val;
24353cb4a0bbSKawai, Hidehiro 	int ret;
24363cb4a0bbSKawai, Hidehiro 	int i;
24373cb4a0bbSKawai, Hidehiro 	unsigned long mask;
24383cb4a0bbSKawai, Hidehiro 
24393cb4a0bbSKawai, Hidehiro 	ret = -EFAULT;
24403cb4a0bbSKawai, Hidehiro 	memset(buffer, 0, sizeof(buffer));
24413cb4a0bbSKawai, Hidehiro 	if (count > sizeof(buffer) - 1)
24423cb4a0bbSKawai, Hidehiro 		count = sizeof(buffer) - 1;
24433cb4a0bbSKawai, Hidehiro 	if (copy_from_user(buffer, buf, count))
24443cb4a0bbSKawai, Hidehiro 		goto out_no_task;
24453cb4a0bbSKawai, Hidehiro 
24463cb4a0bbSKawai, Hidehiro 	ret = -EINVAL;
24473cb4a0bbSKawai, Hidehiro 	val = (unsigned int)simple_strtoul(buffer, &end, 0);
24483cb4a0bbSKawai, Hidehiro 	if (*end == '\n')
24493cb4a0bbSKawai, Hidehiro 		end++;
24503cb4a0bbSKawai, Hidehiro 	if (end - buffer == 0)
24513cb4a0bbSKawai, Hidehiro 		goto out_no_task;
24523cb4a0bbSKawai, Hidehiro 
24533cb4a0bbSKawai, Hidehiro 	ret = -ESRCH;
24543cb4a0bbSKawai, Hidehiro 	task = get_proc_task(file->f_dentry->d_inode);
24553cb4a0bbSKawai, Hidehiro 	if (!task)
24563cb4a0bbSKawai, Hidehiro 		goto out_no_task;
24573cb4a0bbSKawai, Hidehiro 
24583cb4a0bbSKawai, Hidehiro 	ret = end - buffer;
24593cb4a0bbSKawai, Hidehiro 	mm = get_task_mm(task);
24603cb4a0bbSKawai, Hidehiro 	if (!mm)
24613cb4a0bbSKawai, Hidehiro 		goto out_no_mm;
24623cb4a0bbSKawai, Hidehiro 
24633cb4a0bbSKawai, Hidehiro 	for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
24643cb4a0bbSKawai, Hidehiro 		if (val & mask)
24653cb4a0bbSKawai, Hidehiro 			set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
24663cb4a0bbSKawai, Hidehiro 		else
24673cb4a0bbSKawai, Hidehiro 			clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
24683cb4a0bbSKawai, Hidehiro 	}
24693cb4a0bbSKawai, Hidehiro 
24703cb4a0bbSKawai, Hidehiro 	mmput(mm);
24713cb4a0bbSKawai, Hidehiro  out_no_mm:
24723cb4a0bbSKawai, Hidehiro 	put_task_struct(task);
24733cb4a0bbSKawai, Hidehiro  out_no_task:
24743cb4a0bbSKawai, Hidehiro 	return ret;
24753cb4a0bbSKawai, Hidehiro }
24763cb4a0bbSKawai, Hidehiro 
24773cb4a0bbSKawai, Hidehiro static const struct file_operations proc_coredump_filter_operations = {
24783cb4a0bbSKawai, Hidehiro 	.read		= proc_coredump_filter_read,
24793cb4a0bbSKawai, Hidehiro 	.write		= proc_coredump_filter_write,
248087df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
24813cb4a0bbSKawai, Hidehiro };
24823cb4a0bbSKawai, Hidehiro #endif
24833cb4a0bbSKawai, Hidehiro 
24841da177e4SLinus Torvalds /*
24851da177e4SLinus Torvalds  * /proc/self:
24861da177e4SLinus Torvalds  */
24871da177e4SLinus Torvalds static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
24881da177e4SLinus Torvalds 			      int buflen)
24891da177e4SLinus Torvalds {
2490488e5bc4SEric W. Biederman 	struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2491b55fcb22SAndrew Morton 	pid_t tgid = task_tgid_nr_ns(current, ns);
24928578cea7SEric W. Biederman 	char tmp[PROC_NUMBUF];
2493b55fcb22SAndrew Morton 	if (!tgid)
2494488e5bc4SEric W. Biederman 		return -ENOENT;
2495b55fcb22SAndrew Morton 	sprintf(tmp, "%d", tgid);
24961da177e4SLinus Torvalds 	return vfs_readlink(dentry,buffer,buflen,tmp);
24971da177e4SLinus Torvalds }
24981da177e4SLinus Torvalds 
2499008b150aSAl Viro static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
25001da177e4SLinus Torvalds {
2501488e5bc4SEric W. Biederman 	struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2502b55fcb22SAndrew Morton 	pid_t tgid = task_tgid_nr_ns(current, ns);
25037fee4868SAl Viro 	char *name = ERR_PTR(-ENOENT);
25047fee4868SAl Viro 	if (tgid) {
25057fee4868SAl Viro 		name = __getname();
25067fee4868SAl Viro 		if (!name)
25077fee4868SAl Viro 			name = ERR_PTR(-ENOMEM);
25087fee4868SAl Viro 		else
25097fee4868SAl Viro 			sprintf(name, "%d", tgid);
25107fee4868SAl Viro 	}
25117fee4868SAl Viro 	nd_set_link(nd, name);
25127fee4868SAl Viro 	return NULL;
25137fee4868SAl Viro }
25147fee4868SAl Viro 
25157fee4868SAl Viro static void proc_self_put_link(struct dentry *dentry, struct nameidata *nd,
25167fee4868SAl Viro 				void *cookie)
25177fee4868SAl Viro {
25187fee4868SAl Viro 	char *s = nd_get_link(nd);
25197fee4868SAl Viro 	if (!IS_ERR(s))
25207fee4868SAl Viro 		__putname(s);
25211da177e4SLinus Torvalds }
25221da177e4SLinus Torvalds 
2523c5ef1c42SArjan van de Ven static const struct inode_operations proc_self_inode_operations = {
25241da177e4SLinus Torvalds 	.readlink	= proc_self_readlink,
25251da177e4SLinus Torvalds 	.follow_link	= proc_self_follow_link,
25267fee4868SAl Viro 	.put_link	= proc_self_put_link,
25271da177e4SLinus Torvalds };
25281da177e4SLinus Torvalds 
252928a6d671SEric W. Biederman /*
2530801199ceSEric W. Biederman  * proc base
2531801199ceSEric W. Biederman  *
2532801199ceSEric W. Biederman  * These are the directory entries in the root directory of /proc
2533801199ceSEric W. Biederman  * that properly belong to the /proc filesystem, as they describe
2534801199ceSEric W. Biederman  * describe something that is process related.
2535801199ceSEric W. Biederman  */
2536c5141e6dSEric Dumazet static const struct pid_entry proc_base_stuff[] = {
253761a28784SEric W. Biederman 	NOD("self", S_IFLNK|S_IRWXUGO,
2538801199ceSEric W. Biederman 		&proc_self_inode_operations, NULL, {}),
2539801199ceSEric W. Biederman };
2540801199ceSEric W. Biederman 
2541801199ceSEric W. Biederman /*
2542801199ceSEric W. Biederman  *	Exceptional case: normally we are not allowed to unhash a busy
2543801199ceSEric W. Biederman  * directory. In this case, however, we can do it - no aliasing problems
2544801199ceSEric W. Biederman  * due to the way we treat inodes.
2545801199ceSEric W. Biederman  */
2546801199ceSEric W. Biederman static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
2547801199ceSEric W. Biederman {
2548801199ceSEric W. Biederman 	struct inode *inode = dentry->d_inode;
2549801199ceSEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
2550801199ceSEric W. Biederman 	if (task) {
2551801199ceSEric W. Biederman 		put_task_struct(task);
2552801199ceSEric W. Biederman 		return 1;
2553801199ceSEric W. Biederman 	}
2554801199ceSEric W. Biederman 	d_drop(dentry);
2555801199ceSEric W. Biederman 	return 0;
2556801199ceSEric W. Biederman }
2557801199ceSEric W. Biederman 
2558d72f71ebSAl Viro static const struct dentry_operations proc_base_dentry_operations =
2559801199ceSEric W. Biederman {
2560801199ceSEric W. Biederman 	.d_revalidate	= proc_base_revalidate,
2561801199ceSEric W. Biederman 	.d_delete	= pid_delete_dentry,
2562801199ceSEric W. Biederman };
2563801199ceSEric W. Biederman 
2564444ceed8SEric W. Biederman static struct dentry *proc_base_instantiate(struct inode *dir,
2565c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
2566801199ceSEric W. Biederman {
2567c5141e6dSEric Dumazet 	const struct pid_entry *p = ptr;
2568801199ceSEric W. Biederman 	struct inode *inode;
2569801199ceSEric W. Biederman 	struct proc_inode *ei;
257073d36460SDan Carpenter 	struct dentry *error;
2571801199ceSEric W. Biederman 
2572801199ceSEric W. Biederman 	/* Allocate the inode */
2573801199ceSEric W. Biederman 	error = ERR_PTR(-ENOMEM);
2574801199ceSEric W. Biederman 	inode = new_inode(dir->i_sb);
2575801199ceSEric W. Biederman 	if (!inode)
2576801199ceSEric W. Biederman 		goto out;
2577801199ceSEric W. Biederman 
2578801199ceSEric W. Biederman 	/* Initialize the inode */
2579801199ceSEric W. Biederman 	ei = PROC_I(inode);
2580801199ceSEric W. Biederman 	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2581801199ceSEric W. Biederman 
2582801199ceSEric W. Biederman 	/*
2583801199ceSEric W. Biederman 	 * grab the reference to the task.
2584801199ceSEric W. Biederman 	 */
25851a657f78SOleg Nesterov 	ei->pid = get_task_pid(task, PIDTYPE_PID);
2586801199ceSEric W. Biederman 	if (!ei->pid)
2587801199ceSEric W. Biederman 		goto out_iput;
2588801199ceSEric W. Biederman 
2589801199ceSEric W. Biederman 	inode->i_mode = p->mode;
2590801199ceSEric W. Biederman 	if (S_ISDIR(inode->i_mode))
2591801199ceSEric W. Biederman 		inode->i_nlink = 2;
2592801199ceSEric W. Biederman 	if (S_ISLNK(inode->i_mode))
2593801199ceSEric W. Biederman 		inode->i_size = 64;
2594801199ceSEric W. Biederman 	if (p->iop)
2595801199ceSEric W. Biederman 		inode->i_op = p->iop;
2596801199ceSEric W. Biederman 	if (p->fop)
2597801199ceSEric W. Biederman 		inode->i_fop = p->fop;
2598801199ceSEric W. Biederman 	ei->op = p->op;
2599801199ceSEric W. Biederman 	dentry->d_op = &proc_base_dentry_operations;
2600801199ceSEric W. Biederman 	d_add(dentry, inode);
2601801199ceSEric W. Biederman 	error = NULL;
2602801199ceSEric W. Biederman out:
2603801199ceSEric W. Biederman 	return error;
2604801199ceSEric W. Biederman out_iput:
2605801199ceSEric W. Biederman 	iput(inode);
2606801199ceSEric W. Biederman 	goto out;
2607801199ceSEric W. Biederman }
2608801199ceSEric W. Biederman 
2609444ceed8SEric W. Biederman static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2610444ceed8SEric W. Biederman {
2611444ceed8SEric W. Biederman 	struct dentry *error;
2612444ceed8SEric W. Biederman 	struct task_struct *task = get_proc_task(dir);
2613c5141e6dSEric Dumazet 	const struct pid_entry *p, *last;
2614444ceed8SEric W. Biederman 
2615444ceed8SEric W. Biederman 	error = ERR_PTR(-ENOENT);
2616444ceed8SEric W. Biederman 
2617444ceed8SEric W. Biederman 	if (!task)
2618444ceed8SEric W. Biederman 		goto out_no_task;
2619444ceed8SEric W. Biederman 
2620444ceed8SEric W. Biederman 	/* Lookup the directory entry */
26217bcd6b0eSEric W. Biederman 	last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
26227bcd6b0eSEric W. Biederman 	for (p = proc_base_stuff; p <= last; p++) {
2623444ceed8SEric W. Biederman 		if (p->len != dentry->d_name.len)
2624444ceed8SEric W. Biederman 			continue;
2625444ceed8SEric W. Biederman 		if (!memcmp(dentry->d_name.name, p->name, p->len))
2626444ceed8SEric W. Biederman 			break;
2627444ceed8SEric W. Biederman 	}
26287bcd6b0eSEric W. Biederman 	if (p > last)
2629444ceed8SEric W. Biederman 		goto out;
2630444ceed8SEric W. Biederman 
2631444ceed8SEric W. Biederman 	error = proc_base_instantiate(dir, dentry, task, p);
2632444ceed8SEric W. Biederman 
2633444ceed8SEric W. Biederman out:
2634444ceed8SEric W. Biederman 	put_task_struct(task);
2635444ceed8SEric W. Biederman out_no_task:
2636444ceed8SEric W. Biederman 	return error;
2637444ceed8SEric W. Biederman }
2638444ceed8SEric W. Biederman 
2639c5141e6dSEric Dumazet static int proc_base_fill_cache(struct file *filp, void *dirent,
2640c5141e6dSEric Dumazet 	filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
264161a28784SEric W. Biederman {
264261a28784SEric W. Biederman 	return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
264361a28784SEric W. Biederman 				proc_base_instantiate, task, p);
264461a28784SEric W. Biederman }
264561a28784SEric W. Biederman 
2646aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING
2647297c5d92SAndrea Righi static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
2648aba76fdbSAndrew Morton {
2649940389b8SAndrea Righi 	struct task_io_accounting acct = task->ioac;
2650297c5d92SAndrea Righi 	unsigned long flags;
2651297c5d92SAndrea Righi 
26525995477aSAndrea Righi 	if (whole && lock_task_sighand(task, &flags)) {
2653b2d002dbSAndrea Righi 		struct task_struct *t = task;
2654297c5d92SAndrea Righi 
26555995477aSAndrea Righi 		task_io_accounting_add(&acct, &task->signal->ioac);
26565995477aSAndrea Righi 		while_each_thread(task, t)
26575995477aSAndrea Righi 			task_io_accounting_add(&acct, &t->ioac);
2658297c5d92SAndrea Righi 
2659297c5d92SAndrea Righi 		unlock_task_sighand(task, &flags);
2660297c5d92SAndrea Righi 	}
2661aba76fdbSAndrew Morton 	return sprintf(buffer,
2662aba76fdbSAndrew Morton 			"rchar: %llu\n"
2663aba76fdbSAndrew Morton 			"wchar: %llu\n"
2664aba76fdbSAndrew Morton 			"syscr: %llu\n"
2665aba76fdbSAndrew Morton 			"syscw: %llu\n"
2666aba76fdbSAndrew Morton 			"read_bytes: %llu\n"
2667aba76fdbSAndrew Morton 			"write_bytes: %llu\n"
2668aba76fdbSAndrew Morton 			"cancelled_write_bytes: %llu\n",
26697c44319dSAlexander Beregalov 			(unsigned long long)acct.rchar,
26707c44319dSAlexander Beregalov 			(unsigned long long)acct.wchar,
26717c44319dSAlexander Beregalov 			(unsigned long long)acct.syscr,
26727c44319dSAlexander Beregalov 			(unsigned long long)acct.syscw,
26737c44319dSAlexander Beregalov 			(unsigned long long)acct.read_bytes,
26747c44319dSAlexander Beregalov 			(unsigned long long)acct.write_bytes,
26757c44319dSAlexander Beregalov 			(unsigned long long)acct.cancelled_write_bytes);
2676aba76fdbSAndrew Morton }
2677297c5d92SAndrea Righi 
2678297c5d92SAndrea Righi static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2679297c5d92SAndrea Righi {
2680297c5d92SAndrea Righi 	return do_io_accounting(task, buffer, 0);
2681297c5d92SAndrea Righi }
2682297c5d92SAndrea Righi 
2683297c5d92SAndrea Righi static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2684297c5d92SAndrea Righi {
2685297c5d92SAndrea Righi 	return do_io_accounting(task, buffer, 1);
2686297c5d92SAndrea Righi }
2687297c5d92SAndrea Righi #endif /* CONFIG_TASK_IO_ACCOUNTING */
2688aba76fdbSAndrew Morton 
268947830723SKees Cook static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
269047830723SKees Cook 				struct pid *pid, struct task_struct *task)
269147830723SKees Cook {
269247830723SKees Cook 	seq_printf(m, "%08x\n", task->personality);
269347830723SKees Cook 	return 0;
269447830723SKees Cook }
269547830723SKees Cook 
2696801199ceSEric W. Biederman /*
269728a6d671SEric W. Biederman  * Thread groups
269828a6d671SEric W. Biederman  */
269900977a59SArjan van de Ven static const struct file_operations proc_task_operations;
2700c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations;
270120cdc894SEric W. Biederman 
2702c5141e6dSEric Dumazet static const struct pid_entry tgid_base_stuff[] = {
2703631f9c18SAlexey Dobriyan 	DIR("task",       S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2704631f9c18SAlexey Dobriyan 	DIR("fd",         S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2705631f9c18SAlexey Dobriyan 	DIR("fdinfo",     S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2706b2211a36SAndrew Morton #ifdef CONFIG_NET
2707631f9c18SAlexey Dobriyan 	DIR("net",        S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2708b2211a36SAndrew Morton #endif
2709631f9c18SAlexey Dobriyan 	REG("environ",    S_IRUSR, proc_environ_operations),
2710631f9c18SAlexey Dobriyan 	INF("auxv",       S_IRUSR, proc_pid_auxv),
2711631f9c18SAlexey Dobriyan 	ONE("status",     S_IRUGO, proc_pid_status),
2712631f9c18SAlexey Dobriyan 	ONE("personality", S_IRUSR, proc_pid_personality),
27133036e7b4SJiri Olsa 	INF("limits",	  S_IRUGO, proc_pid_limits),
271443ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
2715631f9c18SAlexey Dobriyan 	REG("sched",      S_IRUGO|S_IWUSR, proc_pid_sched_operations),
271643ae34cbSIngo Molnar #endif
27174614a696Sjohn stultz 	REG("comm",      S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2718ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2719631f9c18SAlexey Dobriyan 	INF("syscall",    S_IRUSR, proc_pid_syscall),
2720ebcb6734SRoland McGrath #endif
2721631f9c18SAlexey Dobriyan 	INF("cmdline",    S_IRUGO, proc_pid_cmdline),
2722631f9c18SAlexey Dobriyan 	ONE("stat",       S_IRUGO, proc_tgid_stat),
2723631f9c18SAlexey Dobriyan 	ONE("statm",      S_IRUGO, proc_pid_statm),
2724631f9c18SAlexey Dobriyan 	REG("maps",       S_IRUGO, proc_maps_operations),
272528a6d671SEric W. Biederman #ifdef CONFIG_NUMA
2726631f9c18SAlexey Dobriyan 	REG("numa_maps",  S_IRUGO, proc_numa_maps_operations),
272728a6d671SEric W. Biederman #endif
2728631f9c18SAlexey Dobriyan 	REG("mem",        S_IRUSR|S_IWUSR, proc_mem_operations),
2729631f9c18SAlexey Dobriyan 	LNK("cwd",        proc_cwd_link),
2730631f9c18SAlexey Dobriyan 	LNK("root",       proc_root_link),
2731631f9c18SAlexey Dobriyan 	LNK("exe",        proc_exe_link),
2732631f9c18SAlexey Dobriyan 	REG("mounts",     S_IRUGO, proc_mounts_operations),
2733631f9c18SAlexey Dobriyan 	REG("mountinfo",  S_IRUGO, proc_mountinfo_operations),
2734631f9c18SAlexey Dobriyan 	REG("mountstats", S_IRUSR, proc_mountstats_operations),
27351e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR
2736631f9c18SAlexey Dobriyan 	REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2737631f9c18SAlexey Dobriyan 	REG("smaps",      S_IRUGO, proc_smaps_operations),
2738631f9c18SAlexey Dobriyan 	REG("pagemap",    S_IRUSR, proc_pagemap_operations),
273928a6d671SEric W. Biederman #endif
274028a6d671SEric W. Biederman #ifdef CONFIG_SECURITY
2741631f9c18SAlexey Dobriyan 	DIR("attr",       S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
274228a6d671SEric W. Biederman #endif
274328a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS
2744631f9c18SAlexey Dobriyan 	INF("wchan",      S_IRUGO, proc_pid_wchan),
274528a6d671SEric W. Biederman #endif
27462ec220e2SKen Chen #ifdef CONFIG_STACKTRACE
27472ec220e2SKen Chen 	ONE("stack",      S_IRUSR, proc_pid_stack),
274828a6d671SEric W. Biederman #endif
274928a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS
2750631f9c18SAlexey Dobriyan 	INF("schedstat",  S_IRUGO, proc_pid_schedstat),
275128a6d671SEric W. Biederman #endif
27529745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP
2753631f9c18SAlexey Dobriyan 	REG("latency",  S_IRUGO, proc_lstats_operations),
27549745512cSArjan van de Ven #endif
27558793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET
2756631f9c18SAlexey Dobriyan 	REG("cpuset",     S_IRUGO, proc_cpuset_operations),
275728a6d671SEric W. Biederman #endif
2758a424316cSPaul Menage #ifdef CONFIG_CGROUPS
2759631f9c18SAlexey Dobriyan 	REG("cgroup",  S_IRUGO, proc_cgroup_operations),
2760a424316cSPaul Menage #endif
2761631f9c18SAlexey Dobriyan 	INF("oom_score",  S_IRUGO, proc_oom_score),
2762631f9c18SAlexey Dobriyan 	REG("oom_adj",    S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
2763a63d83f4SDavid Rientjes 	REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
276428a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL
2765631f9c18SAlexey Dobriyan 	REG("loginuid",   S_IWUSR|S_IRUGO, proc_loginuid_operations),
2766631f9c18SAlexey Dobriyan 	REG("sessionid",  S_IRUGO, proc_sessionid_operations),
276728a6d671SEric W. Biederman #endif
2768f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
2769631f9c18SAlexey Dobriyan 	REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2770f4f154fdSAkinobu Mita #endif
2771698ba7b5SChristoph Hellwig #ifdef CONFIG_ELF_CORE
2772631f9c18SAlexey Dobriyan 	REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
27733cb4a0bbSKawai, Hidehiro #endif
2774aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING
2775631f9c18SAlexey Dobriyan 	INF("io",	S_IRUGO, proc_tgid_io_accounting),
2776aba76fdbSAndrew Morton #endif
277728a6d671SEric W. Biederman };
277828a6d671SEric W. Biederman 
277928a6d671SEric W. Biederman static int proc_tgid_base_readdir(struct file * filp,
278028a6d671SEric W. Biederman 			     void * dirent, filldir_t filldir)
278128a6d671SEric W. Biederman {
278228a6d671SEric W. Biederman 	return proc_pident_readdir(filp,dirent,filldir,
278328a6d671SEric W. Biederman 				   tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
278428a6d671SEric W. Biederman }
278528a6d671SEric W. Biederman 
278600977a59SArjan van de Ven static const struct file_operations proc_tgid_base_operations = {
278728a6d671SEric W. Biederman 	.read		= generic_read_dir,
278828a6d671SEric W. Biederman 	.readdir	= proc_tgid_base_readdir,
27896038f373SArnd Bergmann 	.llseek		= default_llseek,
279028a6d671SEric W. Biederman };
279128a6d671SEric W. Biederman 
279228a6d671SEric W. Biederman static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
27937bcd6b0eSEric W. Biederman 	return proc_pident_lookup(dir, dentry,
27947bcd6b0eSEric W. Biederman 				  tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
279528a6d671SEric W. Biederman }
279628a6d671SEric W. Biederman 
2797c5ef1c42SArjan van de Ven static const struct inode_operations proc_tgid_base_inode_operations = {
279828a6d671SEric W. Biederman 	.lookup		= proc_tgid_base_lookup,
279928a6d671SEric W. Biederman 	.getattr	= pid_getattr,
280028a6d671SEric W. Biederman 	.setattr	= proc_setattr,
280128a6d671SEric W. Biederman };
280228a6d671SEric W. Biederman 
280360347f67SPavel Emelyanov static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
28041da177e4SLinus Torvalds {
280548e6484dSEric W. Biederman 	struct dentry *dentry, *leader, *dir;
28068578cea7SEric W. Biederman 	char buf[PROC_NUMBUF];
280748e6484dSEric W. Biederman 	struct qstr name;
28081da177e4SLinus Torvalds 
280948e6484dSEric W. Biederman 	name.name = buf;
281060347f67SPavel Emelyanov 	name.len = snprintf(buf, sizeof(buf), "%d", pid);
281160347f67SPavel Emelyanov 	dentry = d_hash_and_lookup(mnt->mnt_root, &name);
281248e6484dSEric W. Biederman 	if (dentry) {
281348e6484dSEric W. Biederman 		shrink_dcache_parent(dentry);
281448e6484dSEric W. Biederman 		d_drop(dentry);
281548e6484dSEric W. Biederman 		dput(dentry);
28161da177e4SLinus Torvalds 	}
28171da177e4SLinus Torvalds 
281848e6484dSEric W. Biederman 	name.name = buf;
281960347f67SPavel Emelyanov 	name.len = snprintf(buf, sizeof(buf), "%d", tgid);
282060347f67SPavel Emelyanov 	leader = d_hash_and_lookup(mnt->mnt_root, &name);
282148e6484dSEric W. Biederman 	if (!leader)
282248e6484dSEric W. Biederman 		goto out;
282348e6484dSEric W. Biederman 
282448e6484dSEric W. Biederman 	name.name = "task";
282548e6484dSEric W. Biederman 	name.len = strlen(name.name);
282648e6484dSEric W. Biederman 	dir = d_hash_and_lookup(leader, &name);
282748e6484dSEric W. Biederman 	if (!dir)
282848e6484dSEric W. Biederman 		goto out_put_leader;
282948e6484dSEric W. Biederman 
283048e6484dSEric W. Biederman 	name.name = buf;
283160347f67SPavel Emelyanov 	name.len = snprintf(buf, sizeof(buf), "%d", pid);
283248e6484dSEric W. Biederman 	dentry = d_hash_and_lookup(dir, &name);
283348e6484dSEric W. Biederman 	if (dentry) {
283448e6484dSEric W. Biederman 		shrink_dcache_parent(dentry);
283548e6484dSEric W. Biederman 		d_drop(dentry);
283648e6484dSEric W. Biederman 		dput(dentry);
28371da177e4SLinus Torvalds 	}
283848e6484dSEric W. Biederman 
283948e6484dSEric W. Biederman 	dput(dir);
284048e6484dSEric W. Biederman out_put_leader:
284148e6484dSEric W. Biederman 	dput(leader);
284248e6484dSEric W. Biederman out:
284348e6484dSEric W. Biederman 	return;
28441da177e4SLinus Torvalds }
28451da177e4SLinus Torvalds 
28460895e91dSRandy Dunlap /**
28470895e91dSRandy Dunlap  * proc_flush_task -  Remove dcache entries for @task from the /proc dcache.
28480895e91dSRandy Dunlap  * @task: task that should be flushed.
28490895e91dSRandy Dunlap  *
28500895e91dSRandy Dunlap  * When flushing dentries from proc, one needs to flush them from global
285160347f67SPavel Emelyanov  * proc (proc_mnt) and from all the namespaces' procs this task was seen
28520895e91dSRandy Dunlap  * in. This call is supposed to do all of this job.
28530895e91dSRandy Dunlap  *
28540895e91dSRandy Dunlap  * Looks in the dcache for
28550895e91dSRandy Dunlap  * /proc/@pid
28560895e91dSRandy Dunlap  * /proc/@tgid/task/@pid
28570895e91dSRandy Dunlap  * if either directory is present flushes it and all of it'ts children
28580895e91dSRandy Dunlap  * from the dcache.
28590895e91dSRandy Dunlap  *
28600895e91dSRandy Dunlap  * It is safe and reasonable to cache /proc entries for a task until
28610895e91dSRandy Dunlap  * that task exits.  After that they just clog up the dcache with
28620895e91dSRandy Dunlap  * useless entries, possibly causing useful dcache entries to be
28630895e91dSRandy Dunlap  * flushed instead.  This routine is proved to flush those useless
28640895e91dSRandy Dunlap  * dcache entries at process exit time.
28650895e91dSRandy Dunlap  *
28660895e91dSRandy Dunlap  * NOTE: This routine is just an optimization so it does not guarantee
28670895e91dSRandy Dunlap  *       that no dcache entries will exist at process exit time it
28680895e91dSRandy Dunlap  *       just makes it very unlikely that any will persist.
286960347f67SPavel Emelyanov  */
287060347f67SPavel Emelyanov 
287160347f67SPavel Emelyanov void proc_flush_task(struct task_struct *task)
287260347f67SPavel Emelyanov {
28739fcc2d15SEric W. Biederman 	int i;
28749b4d1cbeSOleg Nesterov 	struct pid *pid, *tgid;
2875130f77ecSPavel Emelyanov 	struct upid *upid;
2876130f77ecSPavel Emelyanov 
2877130f77ecSPavel Emelyanov 	pid = task_pid(task);
2878130f77ecSPavel Emelyanov 	tgid = task_tgid(task);
28799fcc2d15SEric W. Biederman 
28809fcc2d15SEric W. Biederman 	for (i = 0; i <= pid->level; i++) {
2881130f77ecSPavel Emelyanov 		upid = &pid->numbers[i];
2882130f77ecSPavel Emelyanov 		proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
28839b4d1cbeSOleg Nesterov 					tgid->numbers[i].nr);
2884130f77ecSPavel Emelyanov 	}
28856f4e6433SPavel Emelyanov 
28866f4e6433SPavel Emelyanov 	upid = &pid->numbers[pid->level];
28876f4e6433SPavel Emelyanov 	if (upid->nr == 1)
28886f4e6433SPavel Emelyanov 		pid_ns_release_proc(upid->ns);
288960347f67SPavel Emelyanov }
289060347f67SPavel Emelyanov 
28919711ef99SAdrian Bunk static struct dentry *proc_pid_instantiate(struct inode *dir,
28929711ef99SAdrian Bunk 					   struct dentry * dentry,
2893c5141e6dSEric Dumazet 					   struct task_struct *task, const void *ptr)
2894444ceed8SEric W. Biederman {
2895444ceed8SEric W. Biederman 	struct dentry *error = ERR_PTR(-ENOENT);
2896444ceed8SEric W. Biederman 	struct inode *inode;
2897444ceed8SEric W. Biederman 
289861a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
2899444ceed8SEric W. Biederman 	if (!inode)
2900444ceed8SEric W. Biederman 		goto out;
2901444ceed8SEric W. Biederman 
2902444ceed8SEric W. Biederman 	inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2903444ceed8SEric W. Biederman 	inode->i_op = &proc_tgid_base_inode_operations;
2904444ceed8SEric W. Biederman 	inode->i_fop = &proc_tgid_base_operations;
2905444ceed8SEric W. Biederman 	inode->i_flags|=S_IMMUTABLE;
2906aed54175SVegard Nossum 
2907aed54175SVegard Nossum 	inode->i_nlink = 2 + pid_entry_count_dirs(tgid_base_stuff,
2908aed54175SVegard Nossum 		ARRAY_SIZE(tgid_base_stuff));
2909444ceed8SEric W. Biederman 
2910444ceed8SEric W. Biederman 	dentry->d_op = &pid_dentry_operations;
2911444ceed8SEric W. Biederman 
2912444ceed8SEric W. Biederman 	d_add(dentry, inode);
2913444ceed8SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
2914444ceed8SEric W. Biederman 	if (pid_revalidate(dentry, NULL))
2915444ceed8SEric W. Biederman 		error = NULL;
2916444ceed8SEric W. Biederman out:
2917444ceed8SEric W. Biederman 	return error;
2918444ceed8SEric W. Biederman }
2919444ceed8SEric W. Biederman 
29201da177e4SLinus Torvalds struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
29211da177e4SLinus Torvalds {
292273d36460SDan Carpenter 	struct dentry *result;
29231da177e4SLinus Torvalds 	struct task_struct *task;
29241da177e4SLinus Torvalds 	unsigned tgid;
2925b488893aSPavel Emelyanov 	struct pid_namespace *ns;
29261da177e4SLinus Torvalds 
2927801199ceSEric W. Biederman 	result = proc_base_lookup(dir, dentry);
2928801199ceSEric W. Biederman 	if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2929801199ceSEric W. Biederman 		goto out;
2930801199ceSEric W. Biederman 
29311da177e4SLinus Torvalds 	tgid = name_to_int(dentry);
29321da177e4SLinus Torvalds 	if (tgid == ~0U)
29331da177e4SLinus Torvalds 		goto out;
29341da177e4SLinus Torvalds 
2935b488893aSPavel Emelyanov 	ns = dentry->d_sb->s_fs_info;
2936de758734SEric W. Biederman 	rcu_read_lock();
2937b488893aSPavel Emelyanov 	task = find_task_by_pid_ns(tgid, ns);
29381da177e4SLinus Torvalds 	if (task)
29391da177e4SLinus Torvalds 		get_task_struct(task);
2940de758734SEric W. Biederman 	rcu_read_unlock();
29411da177e4SLinus Torvalds 	if (!task)
29421da177e4SLinus Torvalds 		goto out;
29431da177e4SLinus Torvalds 
2944444ceed8SEric W. Biederman 	result = proc_pid_instantiate(dir, dentry, task, NULL);
294548e6484dSEric W. Biederman 	put_task_struct(task);
29461da177e4SLinus Torvalds out:
2947cd6a3ce9SEric W. Biederman 	return result;
29481da177e4SLinus Torvalds }
29491da177e4SLinus Torvalds 
29501da177e4SLinus Torvalds /*
29510804ef4bSEric W. Biederman  * Find the first task with tgid >= tgid
29520bc58a91SEric W. Biederman  *
29531da177e4SLinus Torvalds  */
295419fd4bb2SEric W. Biederman struct tgid_iter {
295519fd4bb2SEric W. Biederman 	unsigned int tgid;
29560804ef4bSEric W. Biederman 	struct task_struct *task;
295719fd4bb2SEric W. Biederman };
295819fd4bb2SEric W. Biederman static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
295919fd4bb2SEric W. Biederman {
29600804ef4bSEric W. Biederman 	struct pid *pid;
29611da177e4SLinus Torvalds 
296219fd4bb2SEric W. Biederman 	if (iter.task)
296319fd4bb2SEric W. Biederman 		put_task_struct(iter.task);
29640804ef4bSEric W. Biederman 	rcu_read_lock();
29650804ef4bSEric W. Biederman retry:
296619fd4bb2SEric W. Biederman 	iter.task = NULL;
296719fd4bb2SEric W. Biederman 	pid = find_ge_pid(iter.tgid, ns);
29680804ef4bSEric W. Biederman 	if (pid) {
296919fd4bb2SEric W. Biederman 		iter.tgid = pid_nr_ns(pid, ns);
297019fd4bb2SEric W. Biederman 		iter.task = pid_task(pid, PIDTYPE_PID);
29710804ef4bSEric W. Biederman 		/* What we to know is if the pid we have find is the
29720804ef4bSEric W. Biederman 		 * pid of a thread_group_leader.  Testing for task
29730804ef4bSEric W. Biederman 		 * being a thread_group_leader is the obvious thing
29740804ef4bSEric W. Biederman 		 * todo but there is a window when it fails, due to
29750804ef4bSEric W. Biederman 		 * the pid transfer logic in de_thread.
29760804ef4bSEric W. Biederman 		 *
29770804ef4bSEric W. Biederman 		 * So we perform the straight forward test of seeing
29780804ef4bSEric W. Biederman 		 * if the pid we have found is the pid of a thread
29790804ef4bSEric W. Biederman 		 * group leader, and don't worry if the task we have
29800804ef4bSEric W. Biederman 		 * found doesn't happen to be a thread group leader.
29810804ef4bSEric W. Biederman 		 * As we don't care in the case of readdir.
29820bc58a91SEric W. Biederman 		 */
298319fd4bb2SEric W. Biederman 		if (!iter.task || !has_group_leader_pid(iter.task)) {
298419fd4bb2SEric W. Biederman 			iter.tgid += 1;
29850804ef4bSEric W. Biederman 			goto retry;
298619fd4bb2SEric W. Biederman 		}
298719fd4bb2SEric W. Biederman 		get_task_struct(iter.task);
29881da177e4SLinus Torvalds 	}
2989454cc105SEric W. Biederman 	rcu_read_unlock();
299019fd4bb2SEric W. Biederman 	return iter;
29911da177e4SLinus Torvalds }
29921da177e4SLinus Torvalds 
29937bcd6b0eSEric W. Biederman #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
29941da177e4SLinus Torvalds 
299561a28784SEric W. Biederman static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
299619fd4bb2SEric W. Biederman 	struct tgid_iter iter)
299761a28784SEric W. Biederman {
299861a28784SEric W. Biederman 	char name[PROC_NUMBUF];
299919fd4bb2SEric W. Biederman 	int len = snprintf(name, sizeof(name), "%d", iter.tgid);
300061a28784SEric W. Biederman 	return proc_fill_cache(filp, dirent, filldir, name, len,
300119fd4bb2SEric W. Biederman 				proc_pid_instantiate, iter.task, NULL);
300261a28784SEric W. Biederman }
300361a28784SEric W. Biederman 
30041da177e4SLinus Torvalds /* for the /proc/ directory itself, after non-process stuff has been done */
30051da177e4SLinus Torvalds int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
30061da177e4SLinus Torvalds {
30071da177e4SLinus Torvalds 	unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
30082fddfeefSJosef "Jeff" Sipek 	struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
300919fd4bb2SEric W. Biederman 	struct tgid_iter iter;
3010b488893aSPavel Emelyanov 	struct pid_namespace *ns;
30111da177e4SLinus Torvalds 
301261a28784SEric W. Biederman 	if (!reaper)
301361a28784SEric W. Biederman 		goto out_no_task;
301461a28784SEric W. Biederman 
30157bcd6b0eSEric W. Biederman 	for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
3016c5141e6dSEric Dumazet 		const struct pid_entry *p = &proc_base_stuff[nr];
301761a28784SEric W. Biederman 		if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
3018801199ceSEric W. Biederman 			goto out;
30191da177e4SLinus Torvalds 	}
30201da177e4SLinus Torvalds 
3021b488893aSPavel Emelyanov 	ns = filp->f_dentry->d_sb->s_fs_info;
302219fd4bb2SEric W. Biederman 	iter.task = NULL;
302319fd4bb2SEric W. Biederman 	iter.tgid = filp->f_pos - TGID_OFFSET;
302419fd4bb2SEric W. Biederman 	for (iter = next_tgid(ns, iter);
302519fd4bb2SEric W. Biederman 	     iter.task;
302619fd4bb2SEric W. Biederman 	     iter.tgid += 1, iter = next_tgid(ns, iter)) {
302719fd4bb2SEric W. Biederman 		filp->f_pos = iter.tgid + TGID_OFFSET;
302819fd4bb2SEric W. Biederman 		if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
302919fd4bb2SEric W. Biederman 			put_task_struct(iter.task);
30300804ef4bSEric W. Biederman 			goto out;
30311da177e4SLinus Torvalds 		}
30321da177e4SLinus Torvalds 	}
30330804ef4bSEric W. Biederman 	filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
30340804ef4bSEric W. Biederman out:
303561a28784SEric W. Biederman 	put_task_struct(reaper);
303661a28784SEric W. Biederman out_no_task:
30371da177e4SLinus Torvalds 	return 0;
30381da177e4SLinus Torvalds }
30391da177e4SLinus Torvalds 
30400bc58a91SEric W. Biederman /*
304128a6d671SEric W. Biederman  * Tasks
304228a6d671SEric W. Biederman  */
3043c5141e6dSEric Dumazet static const struct pid_entry tid_base_stuff[] = {
3044631f9c18SAlexey Dobriyan 	DIR("fd",        S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
30453835541dSJerome Marchand 	DIR("fdinfo",    S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
3046631f9c18SAlexey Dobriyan 	REG("environ",   S_IRUSR, proc_environ_operations),
3047631f9c18SAlexey Dobriyan 	INF("auxv",      S_IRUSR, proc_pid_auxv),
3048631f9c18SAlexey Dobriyan 	ONE("status",    S_IRUGO, proc_pid_status),
3049631f9c18SAlexey Dobriyan 	ONE("personality", S_IRUSR, proc_pid_personality),
30503036e7b4SJiri Olsa 	INF("limits",	 S_IRUGO, proc_pid_limits),
305143ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
3052631f9c18SAlexey Dobriyan 	REG("sched",     S_IRUGO|S_IWUSR, proc_pid_sched_operations),
305343ae34cbSIngo Molnar #endif
30544614a696Sjohn stultz 	REG("comm",      S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
3055ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3056631f9c18SAlexey Dobriyan 	INF("syscall",   S_IRUSR, proc_pid_syscall),
3057ebcb6734SRoland McGrath #endif
3058631f9c18SAlexey Dobriyan 	INF("cmdline",   S_IRUGO, proc_pid_cmdline),
3059631f9c18SAlexey Dobriyan 	ONE("stat",      S_IRUGO, proc_tid_stat),
3060631f9c18SAlexey Dobriyan 	ONE("statm",     S_IRUGO, proc_pid_statm),
3061631f9c18SAlexey Dobriyan 	REG("maps",      S_IRUGO, proc_maps_operations),
306228a6d671SEric W. Biederman #ifdef CONFIG_NUMA
3063631f9c18SAlexey Dobriyan 	REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
306428a6d671SEric W. Biederman #endif
3065631f9c18SAlexey Dobriyan 	REG("mem",       S_IRUSR|S_IWUSR, proc_mem_operations),
3066631f9c18SAlexey Dobriyan 	LNK("cwd",       proc_cwd_link),
3067631f9c18SAlexey Dobriyan 	LNK("root",      proc_root_link),
3068631f9c18SAlexey Dobriyan 	LNK("exe",       proc_exe_link),
3069631f9c18SAlexey Dobriyan 	REG("mounts",    S_IRUGO, proc_mounts_operations),
3070631f9c18SAlexey Dobriyan 	REG("mountinfo",  S_IRUGO, proc_mountinfo_operations),
30711e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR
3072631f9c18SAlexey Dobriyan 	REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3073631f9c18SAlexey Dobriyan 	REG("smaps",     S_IRUGO, proc_smaps_operations),
3074631f9c18SAlexey Dobriyan 	REG("pagemap",    S_IRUSR, proc_pagemap_operations),
307528a6d671SEric W. Biederman #endif
307628a6d671SEric W. Biederman #ifdef CONFIG_SECURITY
3077631f9c18SAlexey Dobriyan 	DIR("attr",      S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
307828a6d671SEric W. Biederman #endif
307928a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS
3080631f9c18SAlexey Dobriyan 	INF("wchan",     S_IRUGO, proc_pid_wchan),
308128a6d671SEric W. Biederman #endif
30822ec220e2SKen Chen #ifdef CONFIG_STACKTRACE
30832ec220e2SKen Chen 	ONE("stack",      S_IRUSR, proc_pid_stack),
308428a6d671SEric W. Biederman #endif
308528a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS
3086631f9c18SAlexey Dobriyan 	INF("schedstat", S_IRUGO, proc_pid_schedstat),
308728a6d671SEric W. Biederman #endif
30889745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP
3089631f9c18SAlexey Dobriyan 	REG("latency",  S_IRUGO, proc_lstats_operations),
30909745512cSArjan van de Ven #endif
30918793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET
3092631f9c18SAlexey Dobriyan 	REG("cpuset",    S_IRUGO, proc_cpuset_operations),
309328a6d671SEric W. Biederman #endif
3094a424316cSPaul Menage #ifdef CONFIG_CGROUPS
3095631f9c18SAlexey Dobriyan 	REG("cgroup",  S_IRUGO, proc_cgroup_operations),
3096a424316cSPaul Menage #endif
3097631f9c18SAlexey Dobriyan 	INF("oom_score", S_IRUGO, proc_oom_score),
3098631f9c18SAlexey Dobriyan 	REG("oom_adj",   S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
3099a63d83f4SDavid Rientjes 	REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
310028a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL
3101631f9c18SAlexey Dobriyan 	REG("loginuid",  S_IWUSR|S_IRUGO, proc_loginuid_operations),
3102631f9c18SAlexey Dobriyan 	REG("sessionid",  S_IRUSR, proc_sessionid_operations),
310328a6d671SEric W. Biederman #endif
3104f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
3105631f9c18SAlexey Dobriyan 	REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3106f4f154fdSAkinobu Mita #endif
3107297c5d92SAndrea Righi #ifdef CONFIG_TASK_IO_ACCOUNTING
3108631f9c18SAlexey Dobriyan 	INF("io",	S_IRUGO, proc_tid_io_accounting),
3109297c5d92SAndrea Righi #endif
311028a6d671SEric W. Biederman };
311128a6d671SEric W. Biederman 
311228a6d671SEric W. Biederman static int proc_tid_base_readdir(struct file * filp,
311328a6d671SEric W. Biederman 			     void * dirent, filldir_t filldir)
311428a6d671SEric W. Biederman {
311528a6d671SEric W. Biederman 	return proc_pident_readdir(filp,dirent,filldir,
311628a6d671SEric W. Biederman 				   tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
311728a6d671SEric W. Biederman }
311828a6d671SEric W. Biederman 
311928a6d671SEric W. Biederman static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
31207bcd6b0eSEric W. Biederman 	return proc_pident_lookup(dir, dentry,
31217bcd6b0eSEric W. Biederman 				  tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
312228a6d671SEric W. Biederman }
312328a6d671SEric W. Biederman 
312400977a59SArjan van de Ven static const struct file_operations proc_tid_base_operations = {
312528a6d671SEric W. Biederman 	.read		= generic_read_dir,
312628a6d671SEric W. Biederman 	.readdir	= proc_tid_base_readdir,
31276038f373SArnd Bergmann 	.llseek		= default_llseek,
312828a6d671SEric W. Biederman };
312928a6d671SEric W. Biederman 
3130c5ef1c42SArjan van de Ven static const struct inode_operations proc_tid_base_inode_operations = {
313128a6d671SEric W. Biederman 	.lookup		= proc_tid_base_lookup,
313228a6d671SEric W. Biederman 	.getattr	= pid_getattr,
313328a6d671SEric W. Biederman 	.setattr	= proc_setattr,
313428a6d671SEric W. Biederman };
313528a6d671SEric W. Biederman 
3136444ceed8SEric W. Biederman static struct dentry *proc_task_instantiate(struct inode *dir,
3137c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
3138444ceed8SEric W. Biederman {
3139444ceed8SEric W. Biederman 	struct dentry *error = ERR_PTR(-ENOENT);
3140444ceed8SEric W. Biederman 	struct inode *inode;
314161a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
3142444ceed8SEric W. Biederman 
3143444ceed8SEric W. Biederman 	if (!inode)
3144444ceed8SEric W. Biederman 		goto out;
3145444ceed8SEric W. Biederman 	inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3146444ceed8SEric W. Biederman 	inode->i_op = &proc_tid_base_inode_operations;
3147444ceed8SEric W. Biederman 	inode->i_fop = &proc_tid_base_operations;
3148444ceed8SEric W. Biederman 	inode->i_flags|=S_IMMUTABLE;
3149aed54175SVegard Nossum 
3150aed54175SVegard Nossum 	inode->i_nlink = 2 + pid_entry_count_dirs(tid_base_stuff,
3151aed54175SVegard Nossum 		ARRAY_SIZE(tid_base_stuff));
3152444ceed8SEric W. Biederman 
3153444ceed8SEric W. Biederman 	dentry->d_op = &pid_dentry_operations;
3154444ceed8SEric W. Biederman 
3155444ceed8SEric W. Biederman 	d_add(dentry, inode);
3156444ceed8SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
3157444ceed8SEric W. Biederman 	if (pid_revalidate(dentry, NULL))
3158444ceed8SEric W. Biederman 		error = NULL;
3159444ceed8SEric W. Biederman out:
3160444ceed8SEric W. Biederman 	return error;
3161444ceed8SEric W. Biederman }
3162444ceed8SEric W. Biederman 
316328a6d671SEric W. Biederman static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
316428a6d671SEric W. Biederman {
316528a6d671SEric W. Biederman 	struct dentry *result = ERR_PTR(-ENOENT);
316628a6d671SEric W. Biederman 	struct task_struct *task;
316728a6d671SEric W. Biederman 	struct task_struct *leader = get_proc_task(dir);
316828a6d671SEric W. Biederman 	unsigned tid;
3169b488893aSPavel Emelyanov 	struct pid_namespace *ns;
317028a6d671SEric W. Biederman 
317128a6d671SEric W. Biederman 	if (!leader)
317228a6d671SEric W. Biederman 		goto out_no_task;
317328a6d671SEric W. Biederman 
317428a6d671SEric W. Biederman 	tid = name_to_int(dentry);
317528a6d671SEric W. Biederman 	if (tid == ~0U)
317628a6d671SEric W. Biederman 		goto out;
317728a6d671SEric W. Biederman 
3178b488893aSPavel Emelyanov 	ns = dentry->d_sb->s_fs_info;
317928a6d671SEric W. Biederman 	rcu_read_lock();
3180b488893aSPavel Emelyanov 	task = find_task_by_pid_ns(tid, ns);
318128a6d671SEric W. Biederman 	if (task)
318228a6d671SEric W. Biederman 		get_task_struct(task);
318328a6d671SEric W. Biederman 	rcu_read_unlock();
318428a6d671SEric W. Biederman 	if (!task)
318528a6d671SEric W. Biederman 		goto out;
3186bac0abd6SPavel Emelyanov 	if (!same_thread_group(leader, task))
318728a6d671SEric W. Biederman 		goto out_drop_task;
318828a6d671SEric W. Biederman 
3189444ceed8SEric W. Biederman 	result = proc_task_instantiate(dir, dentry, task, NULL);
319028a6d671SEric W. Biederman out_drop_task:
319128a6d671SEric W. Biederman 	put_task_struct(task);
319228a6d671SEric W. Biederman out:
319328a6d671SEric W. Biederman 	put_task_struct(leader);
319428a6d671SEric W. Biederman out_no_task:
319528a6d671SEric W. Biederman 	return result;
319628a6d671SEric W. Biederman }
319728a6d671SEric W. Biederman 
319828a6d671SEric W. Biederman /*
31990bc58a91SEric W. Biederman  * Find the first tid of a thread group to return to user space.
32000bc58a91SEric W. Biederman  *
32010bc58a91SEric W. Biederman  * Usually this is just the thread group leader, but if the users
32020bc58a91SEric W. Biederman  * buffer was too small or there was a seek into the middle of the
32030bc58a91SEric W. Biederman  * directory we have more work todo.
32040bc58a91SEric W. Biederman  *
32050bc58a91SEric W. Biederman  * In the case of a short read we start with find_task_by_pid.
32060bc58a91SEric W. Biederman  *
32070bc58a91SEric W. Biederman  * In the case of a seek we start with the leader and walk nr
32080bc58a91SEric W. Biederman  * threads past it.
32090bc58a91SEric W. Biederman  */
3210cc288738SEric W. Biederman static struct task_struct *first_tid(struct task_struct *leader,
3211b488893aSPavel Emelyanov 		int tid, int nr, struct pid_namespace *ns)
32120bc58a91SEric W. Biederman {
3213a872ff0cSOleg Nesterov 	struct task_struct *pos;
32140bc58a91SEric W. Biederman 
3215cc288738SEric W. Biederman 	rcu_read_lock();
32160bc58a91SEric W. Biederman 	/* Attempt to start with the pid of a thread */
32170bc58a91SEric W. Biederman 	if (tid && (nr > 0)) {
3218b488893aSPavel Emelyanov 		pos = find_task_by_pid_ns(tid, ns);
3219a872ff0cSOleg Nesterov 		if (pos && (pos->group_leader == leader))
3220a872ff0cSOleg Nesterov 			goto found;
32210bc58a91SEric W. Biederman 	}
32220bc58a91SEric W. Biederman 
32230bc58a91SEric W. Biederman 	/* If nr exceeds the number of threads there is nothing todo */
32240bc58a91SEric W. Biederman 	pos = NULL;
3225a872ff0cSOleg Nesterov 	if (nr && nr >= get_nr_threads(leader))
3226a872ff0cSOleg Nesterov 		goto out;
3227a872ff0cSOleg Nesterov 
3228a872ff0cSOleg Nesterov 	/* If we haven't found our starting place yet start
3229a872ff0cSOleg Nesterov 	 * with the leader and walk nr threads forward.
3230a872ff0cSOleg Nesterov 	 */
3231a872ff0cSOleg Nesterov 	for (pos = leader; nr > 0; --nr) {
3232a872ff0cSOleg Nesterov 		pos = next_thread(pos);
3233a872ff0cSOleg Nesterov 		if (pos == leader) {
3234a872ff0cSOleg Nesterov 			pos = NULL;
3235a872ff0cSOleg Nesterov 			goto out;
3236a872ff0cSOleg Nesterov 		}
3237a872ff0cSOleg Nesterov 	}
3238a872ff0cSOleg Nesterov found:
3239a872ff0cSOleg Nesterov 	get_task_struct(pos);
3240a872ff0cSOleg Nesterov out:
3241cc288738SEric W. Biederman 	rcu_read_unlock();
32420bc58a91SEric W. Biederman 	return pos;
32430bc58a91SEric W. Biederman }
32440bc58a91SEric W. Biederman 
32450bc58a91SEric W. Biederman /*
32460bc58a91SEric W. Biederman  * Find the next thread in the thread list.
32470bc58a91SEric W. Biederman  * Return NULL if there is an error or no next thread.
32480bc58a91SEric W. Biederman  *
32490bc58a91SEric W. Biederman  * The reference to the input task_struct is released.
32500bc58a91SEric W. Biederman  */
32510bc58a91SEric W. Biederman static struct task_struct *next_tid(struct task_struct *start)
32520bc58a91SEric W. Biederman {
3253c1df7fb8SOleg Nesterov 	struct task_struct *pos = NULL;
3254cc288738SEric W. Biederman 	rcu_read_lock();
3255c1df7fb8SOleg Nesterov 	if (pid_alive(start)) {
32560bc58a91SEric W. Biederman 		pos = next_thread(start);
3257c1df7fb8SOleg Nesterov 		if (thread_group_leader(pos))
32580bc58a91SEric W. Biederman 			pos = NULL;
3259c1df7fb8SOleg Nesterov 		else
3260c1df7fb8SOleg Nesterov 			get_task_struct(pos);
3261c1df7fb8SOleg Nesterov 	}
3262cc288738SEric W. Biederman 	rcu_read_unlock();
32630bc58a91SEric W. Biederman 	put_task_struct(start);
32640bc58a91SEric W. Biederman 	return pos;
32650bc58a91SEric W. Biederman }
32660bc58a91SEric W. Biederman 
326761a28784SEric W. Biederman static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
326861a28784SEric W. Biederman 	struct task_struct *task, int tid)
326961a28784SEric W. Biederman {
327061a28784SEric W. Biederman 	char name[PROC_NUMBUF];
327161a28784SEric W. Biederman 	int len = snprintf(name, sizeof(name), "%d", tid);
327261a28784SEric W. Biederman 	return proc_fill_cache(filp, dirent, filldir, name, len,
327361a28784SEric W. Biederman 				proc_task_instantiate, task, NULL);
327461a28784SEric W. Biederman }
327561a28784SEric W. Biederman 
32761da177e4SLinus Torvalds /* for the /proc/TGID/task/ directories */
32771da177e4SLinus Torvalds static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
32781da177e4SLinus Torvalds {
32792fddfeefSJosef "Jeff" Sipek 	struct dentry *dentry = filp->f_path.dentry;
32801da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
32817d895244SGuillaume Chazarain 	struct task_struct *leader = NULL;
32820bc58a91SEric W. Biederman 	struct task_struct *task;
32831da177e4SLinus Torvalds 	int retval = -ENOENT;
32841da177e4SLinus Torvalds 	ino_t ino;
32850bc58a91SEric W. Biederman 	int tid;
3286b488893aSPavel Emelyanov 	struct pid_namespace *ns;
32871da177e4SLinus Torvalds 
32887d895244SGuillaume Chazarain 	task = get_proc_task(inode);
32897d895244SGuillaume Chazarain 	if (!task)
32907d895244SGuillaume Chazarain 		goto out_no_task;
32917d895244SGuillaume Chazarain 	rcu_read_lock();
32927d895244SGuillaume Chazarain 	if (pid_alive(task)) {
32937d895244SGuillaume Chazarain 		leader = task->group_leader;
32947d895244SGuillaume Chazarain 		get_task_struct(leader);
32957d895244SGuillaume Chazarain 	}
32967d895244SGuillaume Chazarain 	rcu_read_unlock();
32977d895244SGuillaume Chazarain 	put_task_struct(task);
329899f89551SEric W. Biederman 	if (!leader)
329999f89551SEric W. Biederman 		goto out_no_task;
33001da177e4SLinus Torvalds 	retval = 0;
33011da177e4SLinus Torvalds 
3302ee568b25SLinus Torvalds 	switch ((unsigned long)filp->f_pos) {
33031da177e4SLinus Torvalds 	case 0:
33041da177e4SLinus Torvalds 		ino = inode->i_ino;
3305ee6f779bSZhang Le 		if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) < 0)
33061da177e4SLinus Torvalds 			goto out;
3307ee6f779bSZhang Le 		filp->f_pos++;
33081da177e4SLinus Torvalds 		/* fall through */
33091da177e4SLinus Torvalds 	case 1:
33101da177e4SLinus Torvalds 		ino = parent_ino(dentry);
3311ee6f779bSZhang Le 		if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) < 0)
33121da177e4SLinus Torvalds 			goto out;
3313ee6f779bSZhang Le 		filp->f_pos++;
33141da177e4SLinus Torvalds 		/* fall through */
33151da177e4SLinus Torvalds 	}
33161da177e4SLinus Torvalds 
33170bc58a91SEric W. Biederman 	/* f_version caches the tgid value that the last readdir call couldn't
33180bc58a91SEric W. Biederman 	 * return. lseek aka telldir automagically resets f_version to 0.
33190bc58a91SEric W. Biederman 	 */
3320b488893aSPavel Emelyanov 	ns = filp->f_dentry->d_sb->s_fs_info;
33212b47c361SMathieu Desnoyers 	tid = (int)filp->f_version;
33220bc58a91SEric W. Biederman 	filp->f_version = 0;
3323ee6f779bSZhang Le 	for (task = first_tid(leader, tid, filp->f_pos - 2, ns);
33240bc58a91SEric W. Biederman 	     task;
3325ee6f779bSZhang Le 	     task = next_tid(task), filp->f_pos++) {
3326b488893aSPavel Emelyanov 		tid = task_pid_nr_ns(task, ns);
332761a28784SEric W. Biederman 		if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
33280bc58a91SEric W. Biederman 			/* returning this tgid failed, save it as the first
33290bc58a91SEric W. Biederman 			 * pid for the next readir call */
33302b47c361SMathieu Desnoyers 			filp->f_version = (u64)tid;
33310bc58a91SEric W. Biederman 			put_task_struct(task);
33321da177e4SLinus Torvalds 			break;
33330bc58a91SEric W. Biederman 		}
33341da177e4SLinus Torvalds 	}
33351da177e4SLinus Torvalds out:
333699f89551SEric W. Biederman 	put_task_struct(leader);
333799f89551SEric W. Biederman out_no_task:
33381da177e4SLinus Torvalds 	return retval;
33391da177e4SLinus Torvalds }
33406e66b52bSEric W. Biederman 
33416e66b52bSEric W. Biederman static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
33426e66b52bSEric W. Biederman {
33436e66b52bSEric W. Biederman 	struct inode *inode = dentry->d_inode;
334499f89551SEric W. Biederman 	struct task_struct *p = get_proc_task(inode);
33456e66b52bSEric W. Biederman 	generic_fillattr(inode, stat);
33466e66b52bSEric W. Biederman 
334799f89551SEric W. Biederman 	if (p) {
334899f89551SEric W. Biederman 		stat->nlink += get_nr_threads(p);
334999f89551SEric W. Biederman 		put_task_struct(p);
33506e66b52bSEric W. Biederman 	}
33516e66b52bSEric W. Biederman 
33526e66b52bSEric W. Biederman 	return 0;
33536e66b52bSEric W. Biederman }
335428a6d671SEric W. Biederman 
3355c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations = {
335628a6d671SEric W. Biederman 	.lookup		= proc_task_lookup,
335728a6d671SEric W. Biederman 	.getattr	= proc_task_getattr,
335828a6d671SEric W. Biederman 	.setattr	= proc_setattr,
335928a6d671SEric W. Biederman };
336028a6d671SEric W. Biederman 
336100977a59SArjan van de Ven static const struct file_operations proc_task_operations = {
336228a6d671SEric W. Biederman 	.read		= generic_read_dir,
336328a6d671SEric W. Biederman 	.readdir	= proc_task_readdir,
33646038f373SArnd Bergmann 	.llseek		= default_llseek,
336528a6d671SEric W. Biederman };
3366