xref: /openbmc/linux/fs/proc/base.c (revision 9d6de12f70d2fb1487c4f482a21fed25fe74e0fd)
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 
2299b1bf12dSKOSAKI Motohiro 	if (mutex_lock_killable(&task->signal->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 	}
2389b1bf12dSKOSAKI Motohiro 	mutex_unlock(&task->signal->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++) {
37634e49d4fSJoe Perches 		struct latency_record *lr = &task->latency_record[i];
37734e49d4fSJoe Perches 		if (lr->backtrace[0]) {
3789745512cSArjan van de Ven 			int q;
3799745512cSArjan van de Ven 			seq_printf(m, "%i %li %li",
38034e49d4fSJoe Perches 				   lr->count, lr->time, lr->max);
3819745512cSArjan van de Ven 			for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
38234e49d4fSJoe Perches 				unsigned long bt = lr->backtrace[q];
38334e49d4fSJoe Perches 				if (!bt)
3849745512cSArjan van de Ven 					break;
38534e49d4fSJoe Perches 				if (bt == ULONG_MAX)
3869745512cSArjan van de Ven 					break;
38734e49d4fSJoe Perches 				seq_printf(m, " %ps", (void *)bt);
3889745512cSArjan van de Ven 			}
389*9d6de12fSAlexey Dobriyan 			seq_putc(m, '\n');
3909745512cSArjan van de Ven 		}
3919745512cSArjan van de Ven 
3929745512cSArjan van de Ven 	}
39313d77c37SHiroshi Shimamoto 	put_task_struct(task);
3949745512cSArjan van de Ven 	return 0;
3959745512cSArjan van de Ven }
3969745512cSArjan van de Ven 
3979745512cSArjan van de Ven static int lstats_open(struct inode *inode, struct file *file)
3989745512cSArjan van de Ven {
39913d77c37SHiroshi Shimamoto 	return single_open(file, lstats_show_proc, inode);
400d6643d12SHiroshi Shimamoto }
401d6643d12SHiroshi Shimamoto 
4029745512cSArjan van de Ven static ssize_t lstats_write(struct file *file, const char __user *buf,
4039745512cSArjan van de Ven 			    size_t count, loff_t *offs)
4049745512cSArjan van de Ven {
40513d77c37SHiroshi Shimamoto 	struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
4069745512cSArjan van de Ven 
40713d77c37SHiroshi Shimamoto 	if (!task)
40813d77c37SHiroshi Shimamoto 		return -ESRCH;
4099745512cSArjan van de Ven 	clear_all_latency_tracing(task);
41013d77c37SHiroshi Shimamoto 	put_task_struct(task);
4119745512cSArjan van de Ven 
4129745512cSArjan van de Ven 	return count;
4139745512cSArjan van de Ven }
4149745512cSArjan van de Ven 
4159745512cSArjan van de Ven static const struct file_operations proc_lstats_operations = {
4169745512cSArjan van de Ven 	.open		= lstats_open,
4179745512cSArjan van de Ven 	.read		= seq_read,
4189745512cSArjan van de Ven 	.write		= lstats_write,
4199745512cSArjan van de Ven 	.llseek		= seq_lseek,
42013d77c37SHiroshi Shimamoto 	.release	= single_release,
4219745512cSArjan van de Ven };
4229745512cSArjan van de Ven 
4239745512cSArjan van de Ven #endif
4249745512cSArjan van de Ven 
4251da177e4SLinus Torvalds static int proc_oom_score(struct task_struct *task, char *buffer)
4261da177e4SLinus Torvalds {
427b95c35e7SOleg Nesterov 	unsigned long points = 0;
4281da177e4SLinus Torvalds 
42919c5d45aSAlexey Dobriyan 	read_lock(&tasklist_lock);
430b95c35e7SOleg Nesterov 	if (pid_alive(task))
431a63d83f4SDavid Rientjes 		points = oom_badness(task, NULL, NULL,
432a63d83f4SDavid Rientjes 					totalram_pages + total_swap_pages);
43319c5d45aSAlexey Dobriyan 	read_unlock(&tasklist_lock);
4341da177e4SLinus Torvalds 	return sprintf(buffer, "%lu\n", points);
4351da177e4SLinus Torvalds }
4361da177e4SLinus Torvalds 
437d85f50d5SNeil Horman struct limit_names {
438d85f50d5SNeil Horman 	char *name;
439d85f50d5SNeil Horman 	char *unit;
440d85f50d5SNeil Horman };
441d85f50d5SNeil Horman 
442d85f50d5SNeil Horman static const struct limit_names lnames[RLIM_NLIMITS] = {
443cff4edb5SKees Cook 	[RLIMIT_CPU] = {"Max cpu time", "seconds"},
444d85f50d5SNeil Horman 	[RLIMIT_FSIZE] = {"Max file size", "bytes"},
445d85f50d5SNeil Horman 	[RLIMIT_DATA] = {"Max data size", "bytes"},
446d85f50d5SNeil Horman 	[RLIMIT_STACK] = {"Max stack size", "bytes"},
447d85f50d5SNeil Horman 	[RLIMIT_CORE] = {"Max core file size", "bytes"},
448d85f50d5SNeil Horman 	[RLIMIT_RSS] = {"Max resident set", "bytes"},
449d85f50d5SNeil Horman 	[RLIMIT_NPROC] = {"Max processes", "processes"},
450d85f50d5SNeil Horman 	[RLIMIT_NOFILE] = {"Max open files", "files"},
451d85f50d5SNeil Horman 	[RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
452d85f50d5SNeil Horman 	[RLIMIT_AS] = {"Max address space", "bytes"},
453d85f50d5SNeil Horman 	[RLIMIT_LOCKS] = {"Max file locks", "locks"},
454d85f50d5SNeil Horman 	[RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
455d85f50d5SNeil Horman 	[RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
456d85f50d5SNeil Horman 	[RLIMIT_NICE] = {"Max nice priority", NULL},
457d85f50d5SNeil Horman 	[RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
4588808117cSEugene Teo 	[RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
459d85f50d5SNeil Horman };
460d85f50d5SNeil Horman 
461d85f50d5SNeil Horman /* Display limits for a process */
462d85f50d5SNeil Horman static int proc_pid_limits(struct task_struct *task, char *buffer)
463d85f50d5SNeil Horman {
464d85f50d5SNeil Horman 	unsigned int i;
465d85f50d5SNeil Horman 	int count = 0;
466d85f50d5SNeil Horman 	unsigned long flags;
467d85f50d5SNeil Horman 	char *bufptr = buffer;
468d85f50d5SNeil Horman 
469d85f50d5SNeil Horman 	struct rlimit rlim[RLIM_NLIMITS];
470d85f50d5SNeil Horman 
471a6bebbc8SLai Jiangshan 	if (!lock_task_sighand(task, &flags))
472d85f50d5SNeil Horman 		return 0;
473d85f50d5SNeil Horman 	memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
474d85f50d5SNeil Horman 	unlock_task_sighand(task, &flags);
475d85f50d5SNeil Horman 
476d85f50d5SNeil Horman 	/*
477d85f50d5SNeil Horman 	 * print the file header
478d85f50d5SNeil Horman 	 */
479d85f50d5SNeil Horman 	count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
480d85f50d5SNeil Horman 			"Limit", "Soft Limit", "Hard Limit", "Units");
481d85f50d5SNeil Horman 
482d85f50d5SNeil Horman 	for (i = 0; i < RLIM_NLIMITS; i++) {
483d85f50d5SNeil Horman 		if (rlim[i].rlim_cur == RLIM_INFINITY)
484d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-25s %-20s ",
485d85f50d5SNeil Horman 					 lnames[i].name, "unlimited");
486d85f50d5SNeil Horman 		else
487d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-25s %-20lu ",
488d85f50d5SNeil Horman 					 lnames[i].name, rlim[i].rlim_cur);
489d85f50d5SNeil Horman 
490d85f50d5SNeil Horman 		if (rlim[i].rlim_max == RLIM_INFINITY)
491d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-20s ", "unlimited");
492d85f50d5SNeil Horman 		else
493d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-20lu ",
494d85f50d5SNeil Horman 					 rlim[i].rlim_max);
495d85f50d5SNeil Horman 
496d85f50d5SNeil Horman 		if (lnames[i].unit)
497d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-10s\n",
498d85f50d5SNeil Horman 					 lnames[i].unit);
499d85f50d5SNeil Horman 		else
500d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "\n");
501d85f50d5SNeil Horman 	}
502d85f50d5SNeil Horman 
503d85f50d5SNeil Horman 	return count;
504d85f50d5SNeil Horman }
505d85f50d5SNeil Horman 
506ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
507ebcb6734SRoland McGrath static int proc_pid_syscall(struct task_struct *task, char *buffer)
508ebcb6734SRoland McGrath {
509ebcb6734SRoland McGrath 	long nr;
510ebcb6734SRoland McGrath 	unsigned long args[6], sp, pc;
511ebcb6734SRoland McGrath 
512ebcb6734SRoland McGrath 	if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
513ebcb6734SRoland McGrath 		return sprintf(buffer, "running\n");
514ebcb6734SRoland McGrath 
515ebcb6734SRoland McGrath 	if (nr < 0)
516ebcb6734SRoland McGrath 		return sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
517ebcb6734SRoland McGrath 
518ebcb6734SRoland McGrath 	return sprintf(buffer,
519ebcb6734SRoland McGrath 		       "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
520ebcb6734SRoland McGrath 		       nr,
521ebcb6734SRoland McGrath 		       args[0], args[1], args[2], args[3], args[4], args[5],
522ebcb6734SRoland McGrath 		       sp, pc);
523ebcb6734SRoland McGrath }
524ebcb6734SRoland McGrath #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
525ebcb6734SRoland McGrath 
5261da177e4SLinus Torvalds /************************************************************************/
5271da177e4SLinus Torvalds /*                       Here the fs part begins                        */
5281da177e4SLinus Torvalds /************************************************************************/
5291da177e4SLinus Torvalds 
5301da177e4SLinus Torvalds /* permission checks */
531778c1144SEric W. Biederman static int proc_fd_access_allowed(struct inode *inode)
5321da177e4SLinus Torvalds {
533778c1144SEric W. Biederman 	struct task_struct *task;
534778c1144SEric W. Biederman 	int allowed = 0;
535df26c40eSEric W. Biederman 	/* Allow access to a task's file descriptors if it is us or we
536df26c40eSEric W. Biederman 	 * may use ptrace attach to the process and find out that
537df26c40eSEric W. Biederman 	 * information.
538778c1144SEric W. Biederman 	 */
539778c1144SEric W. Biederman 	task = get_proc_task(inode);
540df26c40eSEric W. Biederman 	if (task) {
541006ebb40SStephen Smalley 		allowed = ptrace_may_access(task, PTRACE_MODE_READ);
542778c1144SEric W. Biederman 		put_task_struct(task);
543df26c40eSEric W. Biederman 	}
544778c1144SEric W. Biederman 	return allowed;
5451da177e4SLinus Torvalds }
5461da177e4SLinus Torvalds 
5476d76fa58SLinus Torvalds static int proc_setattr(struct dentry *dentry, struct iattr *attr)
5486d76fa58SLinus Torvalds {
5496d76fa58SLinus Torvalds 	int error;
5506d76fa58SLinus Torvalds 	struct inode *inode = dentry->d_inode;
5516d76fa58SLinus Torvalds 
5526d76fa58SLinus Torvalds 	if (attr->ia_valid & ATTR_MODE)
5536d76fa58SLinus Torvalds 		return -EPERM;
5546d76fa58SLinus Torvalds 
5556d76fa58SLinus Torvalds 	error = inode_change_ok(inode, attr);
5561025774cSChristoph Hellwig 	if (error)
5576d76fa58SLinus Torvalds 		return error;
5581025774cSChristoph Hellwig 
5591025774cSChristoph Hellwig 	if ((attr->ia_valid & ATTR_SIZE) &&
5601025774cSChristoph Hellwig 	    attr->ia_size != i_size_read(inode)) {
5611025774cSChristoph Hellwig 		error = vmtruncate(inode, attr->ia_size);
5621025774cSChristoph Hellwig 		if (error)
5631025774cSChristoph Hellwig 			return error;
5641025774cSChristoph Hellwig 	}
5651025774cSChristoph Hellwig 
5661025774cSChristoph Hellwig 	setattr_copy(inode, attr);
5671025774cSChristoph Hellwig 	mark_inode_dirty(inode);
5681025774cSChristoph Hellwig 	return 0;
5696d76fa58SLinus Torvalds }
5706d76fa58SLinus Torvalds 
571c5ef1c42SArjan van de Ven static const struct inode_operations proc_def_inode_operations = {
5726d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
5736d76fa58SLinus Torvalds };
5746d76fa58SLinus Torvalds 
575a1a2c409SMiklos Szeredi static int mounts_open_common(struct inode *inode, struct file *file,
576a1a2c409SMiklos Szeredi 			      const struct seq_operations *op)
5771da177e4SLinus Torvalds {
57899f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
579cf7b708cSPavel Emelyanov 	struct nsproxy *nsp;
5806b3286edSKirill Korotaev 	struct mnt_namespace *ns = NULL;
581a1a2c409SMiklos Szeredi 	struct path root;
5825addc5ddSAl Viro 	struct proc_mounts *p;
5835addc5ddSAl Viro 	int ret = -EINVAL;
5845addc5ddSAl Viro 
58599f89551SEric W. Biederman 	if (task) {
586cf7b708cSPavel Emelyanov 		rcu_read_lock();
587cf7b708cSPavel Emelyanov 		nsp = task_nsproxy(task);
588cf7b708cSPavel Emelyanov 		if (nsp) {
589cf7b708cSPavel Emelyanov 			ns = nsp->mnt_ns;
5906b3286edSKirill Korotaev 			if (ns)
5916b3286edSKirill Korotaev 				get_mnt_ns(ns);
592863c4702SAlexey Dobriyan 		}
593cf7b708cSPavel Emelyanov 		rcu_read_unlock();
594f7ad3c6bSMiklos Szeredi 		if (ns && get_task_root(task, &root) == 0)
5957c2c7d99SHugh Dickins 			ret = 0;
59699f89551SEric W. Biederman 		put_task_struct(task);
59799f89551SEric W. Biederman 	}
5981da177e4SLinus Torvalds 
599a1a2c409SMiklos Szeredi 	if (!ns)
600a1a2c409SMiklos Szeredi 		goto err;
6017c2c7d99SHugh Dickins 	if (ret)
602a1a2c409SMiklos Szeredi 		goto err_put_ns;
603a1a2c409SMiklos Szeredi 
6045addc5ddSAl Viro 	ret = -ENOMEM;
6055addc5ddSAl Viro 	p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
606a1a2c409SMiklos Szeredi 	if (!p)
607a1a2c409SMiklos Szeredi 		goto err_put_path;
608a1a2c409SMiklos Szeredi 
6095addc5ddSAl Viro 	file->private_data = &p->m;
610a1a2c409SMiklos Szeredi 	ret = seq_open(file, op);
611a1a2c409SMiklos Szeredi 	if (ret)
612a1a2c409SMiklos Szeredi 		goto err_free;
613a1a2c409SMiklos Szeredi 
614a1a2c409SMiklos Szeredi 	p->m.private = p;
615a1a2c409SMiklos Szeredi 	p->ns = ns;
616a1a2c409SMiklos Szeredi 	p->root = root;
6176b3286edSKirill Korotaev 	p->event = ns->event;
618a1a2c409SMiklos Szeredi 
6195addc5ddSAl Viro 	return 0;
620a1a2c409SMiklos Szeredi 
621a1a2c409SMiklos Szeredi  err_free:
6225addc5ddSAl Viro 	kfree(p);
623a1a2c409SMiklos Szeredi  err_put_path:
624a1a2c409SMiklos Szeredi 	path_put(&root);
625a1a2c409SMiklos Szeredi  err_put_ns:
6266b3286edSKirill Korotaev 	put_mnt_ns(ns);
627a1a2c409SMiklos Szeredi  err:
6281da177e4SLinus Torvalds 	return ret;
6291da177e4SLinus Torvalds }
6301da177e4SLinus Torvalds 
6311da177e4SLinus Torvalds static int mounts_release(struct inode *inode, struct file *file)
6321da177e4SLinus Torvalds {
633a1a2c409SMiklos Szeredi 	struct proc_mounts *p = file->private_data;
634a1a2c409SMiklos Szeredi 	path_put(&p->root);
635a1a2c409SMiklos Szeredi 	put_mnt_ns(p->ns);
6361da177e4SLinus Torvalds 	return seq_release(inode, file);
6371da177e4SLinus Torvalds }
6381da177e4SLinus Torvalds 
6395addc5ddSAl Viro static unsigned mounts_poll(struct file *file, poll_table *wait)
6405addc5ddSAl Viro {
6415addc5ddSAl Viro 	struct proc_mounts *p = file->private_data;
64231b07093SKOSAKI Motohiro 	unsigned res = POLLIN | POLLRDNORM;
6435addc5ddSAl Viro 
6449f5596afSAl Viro 	poll_wait(file, &p->ns->poll, wait);
6459f5596afSAl Viro 	if (mnt_had_events(p))
64631b07093SKOSAKI Motohiro 		res |= POLLERR | POLLPRI;
6475addc5ddSAl Viro 
6485addc5ddSAl Viro 	return res;
6495addc5ddSAl Viro }
6505addc5ddSAl Viro 
651a1a2c409SMiklos Szeredi static int mounts_open(struct inode *inode, struct file *file)
652a1a2c409SMiklos Szeredi {
653a1a2c409SMiklos Szeredi 	return mounts_open_common(inode, file, &mounts_op);
654a1a2c409SMiklos Szeredi }
655a1a2c409SMiklos Szeredi 
65600977a59SArjan van de Ven static const struct file_operations proc_mounts_operations = {
6571da177e4SLinus Torvalds 	.open		= mounts_open,
6581da177e4SLinus Torvalds 	.read		= seq_read,
6591da177e4SLinus Torvalds 	.llseek		= seq_lseek,
6601da177e4SLinus Torvalds 	.release	= mounts_release,
6615addc5ddSAl Viro 	.poll		= mounts_poll,
6621da177e4SLinus Torvalds };
6631da177e4SLinus Torvalds 
6642d4d4864SRam Pai static int mountinfo_open(struct inode *inode, struct file *file)
6652d4d4864SRam Pai {
6662d4d4864SRam Pai 	return mounts_open_common(inode, file, &mountinfo_op);
6672d4d4864SRam Pai }
6682d4d4864SRam Pai 
6692d4d4864SRam Pai static const struct file_operations proc_mountinfo_operations = {
6702d4d4864SRam Pai 	.open		= mountinfo_open,
6712d4d4864SRam Pai 	.read		= seq_read,
6722d4d4864SRam Pai 	.llseek		= seq_lseek,
6732d4d4864SRam Pai 	.release	= mounts_release,
6742d4d4864SRam Pai 	.poll		= mounts_poll,
6752d4d4864SRam Pai };
6762d4d4864SRam Pai 
677b4629fe2SChuck Lever static int mountstats_open(struct inode *inode, struct file *file)
678b4629fe2SChuck Lever {
679a1a2c409SMiklos Szeredi 	return mounts_open_common(inode, file, &mountstats_op);
680b4629fe2SChuck Lever }
681b4629fe2SChuck Lever 
68200977a59SArjan van de Ven static const struct file_operations proc_mountstats_operations = {
683b4629fe2SChuck Lever 	.open		= mountstats_open,
684b4629fe2SChuck Lever 	.read		= seq_read,
685b4629fe2SChuck Lever 	.llseek		= seq_lseek,
686b4629fe2SChuck Lever 	.release	= mounts_release,
687b4629fe2SChuck Lever };
688b4629fe2SChuck Lever 
6891da177e4SLinus Torvalds #define PROC_BLOCK_SIZE	(3*1024)		/* 4K page size but our output routines use some slack for overruns */
6901da177e4SLinus Torvalds 
6911da177e4SLinus Torvalds static ssize_t proc_info_read(struct file * file, char __user * buf,
6921da177e4SLinus Torvalds 			  size_t count, loff_t *ppos)
6931da177e4SLinus Torvalds {
6942fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
6951da177e4SLinus Torvalds 	unsigned long page;
6961da177e4SLinus Torvalds 	ssize_t length;
69799f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
69899f89551SEric W. Biederman 
69999f89551SEric W. Biederman 	length = -ESRCH;
70099f89551SEric W. Biederman 	if (!task)
70199f89551SEric W. Biederman 		goto out_no_task;
7021da177e4SLinus Torvalds 
7031da177e4SLinus Torvalds 	if (count > PROC_BLOCK_SIZE)
7041da177e4SLinus Torvalds 		count = PROC_BLOCK_SIZE;
70599f89551SEric W. Biederman 
70699f89551SEric W. Biederman 	length = -ENOMEM;
707e12ba74dSMel Gorman 	if (!(page = __get_free_page(GFP_TEMPORARY)))
70899f89551SEric W. Biederman 		goto out;
7091da177e4SLinus Torvalds 
7101da177e4SLinus Torvalds 	length = PROC_I(inode)->op.proc_read(task, (char*)page);
7111da177e4SLinus Torvalds 
7121da177e4SLinus Torvalds 	if (length >= 0)
7131da177e4SLinus Torvalds 		length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
7141da177e4SLinus Torvalds 	free_page(page);
71599f89551SEric W. Biederman out:
71699f89551SEric W. Biederman 	put_task_struct(task);
71799f89551SEric W. Biederman out_no_task:
7181da177e4SLinus Torvalds 	return length;
7191da177e4SLinus Torvalds }
7201da177e4SLinus Torvalds 
72100977a59SArjan van de Ven static const struct file_operations proc_info_file_operations = {
7221da177e4SLinus Torvalds 	.read		= proc_info_read,
72387df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
7241da177e4SLinus Torvalds };
7251da177e4SLinus Torvalds 
726be614086SEric W. Biederman static int proc_single_show(struct seq_file *m, void *v)
727be614086SEric W. Biederman {
728be614086SEric W. Biederman 	struct inode *inode = m->private;
729be614086SEric W. Biederman 	struct pid_namespace *ns;
730be614086SEric W. Biederman 	struct pid *pid;
731be614086SEric W. Biederman 	struct task_struct *task;
732be614086SEric W. Biederman 	int ret;
733be614086SEric W. Biederman 
734be614086SEric W. Biederman 	ns = inode->i_sb->s_fs_info;
735be614086SEric W. Biederman 	pid = proc_pid(inode);
736be614086SEric W. Biederman 	task = get_pid_task(pid, PIDTYPE_PID);
737be614086SEric W. Biederman 	if (!task)
738be614086SEric W. Biederman 		return -ESRCH;
739be614086SEric W. Biederman 
740be614086SEric W. Biederman 	ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
741be614086SEric W. Biederman 
742be614086SEric W. Biederman 	put_task_struct(task);
743be614086SEric W. Biederman 	return ret;
744be614086SEric W. Biederman }
745be614086SEric W. Biederman 
746be614086SEric W. Biederman static int proc_single_open(struct inode *inode, struct file *filp)
747be614086SEric W. Biederman {
748be614086SEric W. Biederman 	int ret;
749be614086SEric W. Biederman 	ret = single_open(filp, proc_single_show, NULL);
750be614086SEric W. Biederman 	if (!ret) {
751be614086SEric W. Biederman 		struct seq_file *m = filp->private_data;
752be614086SEric W. Biederman 
753be614086SEric W. Biederman 		m->private = inode;
754be614086SEric W. Biederman 	}
755be614086SEric W. Biederman 	return ret;
756be614086SEric W. Biederman }
757be614086SEric W. Biederman 
758be614086SEric W. Biederman static const struct file_operations proc_single_file_operations = {
759be614086SEric W. Biederman 	.open		= proc_single_open,
760be614086SEric W. Biederman 	.read		= seq_read,
761be614086SEric W. Biederman 	.llseek		= seq_lseek,
762be614086SEric W. Biederman 	.release	= single_release,
763be614086SEric W. Biederman };
764be614086SEric W. Biederman 
7651da177e4SLinus Torvalds static int mem_open(struct inode* inode, struct file* file)
7661da177e4SLinus Torvalds {
7671da177e4SLinus Torvalds 	file->private_data = (void*)((long)current->self_exec_id);
7684a3956c7SKAMEZAWA Hiroyuki 	/* OK to pass negative loff_t, we can catch out-of-range */
7694a3956c7SKAMEZAWA Hiroyuki 	file->f_mode |= FMODE_UNSIGNED_OFFSET;
7701da177e4SLinus Torvalds 	return 0;
7711da177e4SLinus Torvalds }
7721da177e4SLinus Torvalds 
7731da177e4SLinus Torvalds static ssize_t mem_read(struct file * file, char __user * buf,
7741da177e4SLinus Torvalds 			size_t count, loff_t *ppos)
7751da177e4SLinus Torvalds {
7762fddfeefSJosef "Jeff" Sipek 	struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
7771da177e4SLinus Torvalds 	char *page;
7781da177e4SLinus Torvalds 	unsigned long src = *ppos;
7791da177e4SLinus Torvalds 	int ret = -ESRCH;
7801da177e4SLinus Torvalds 	struct mm_struct *mm;
7811da177e4SLinus Torvalds 
78299f89551SEric W. Biederman 	if (!task)
78399f89551SEric W. Biederman 		goto out_no_task;
78499f89551SEric W. Biederman 
785638fa202SRoland McGrath 	if (check_mem_permission(task))
7861da177e4SLinus Torvalds 		goto out;
7871da177e4SLinus Torvalds 
7881da177e4SLinus Torvalds 	ret = -ENOMEM;
789e12ba74dSMel Gorman 	page = (char *)__get_free_page(GFP_TEMPORARY);
7901da177e4SLinus Torvalds 	if (!page)
7911da177e4SLinus Torvalds 		goto out;
7921da177e4SLinus Torvalds 
7931da177e4SLinus Torvalds 	ret = 0;
7941da177e4SLinus Torvalds 
7951da177e4SLinus Torvalds 	mm = get_task_mm(task);
7961da177e4SLinus Torvalds 	if (!mm)
7971da177e4SLinus Torvalds 		goto out_free;
7981da177e4SLinus Torvalds 
7991da177e4SLinus Torvalds 	ret = -EIO;
8001da177e4SLinus Torvalds 
8011da177e4SLinus Torvalds 	if (file->private_data != (void*)((long)current->self_exec_id))
8021da177e4SLinus Torvalds 		goto out_put;
8031da177e4SLinus Torvalds 
8041da177e4SLinus Torvalds 	ret = 0;
8051da177e4SLinus Torvalds 
8061da177e4SLinus Torvalds 	while (count > 0) {
8071da177e4SLinus Torvalds 		int this_len, retval;
8081da177e4SLinus Torvalds 
8091da177e4SLinus Torvalds 		this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
8101da177e4SLinus Torvalds 		retval = access_process_vm(task, src, page, this_len, 0);
811638fa202SRoland McGrath 		if (!retval || check_mem_permission(task)) {
8121da177e4SLinus Torvalds 			if (!ret)
8131da177e4SLinus Torvalds 				ret = -EIO;
8141da177e4SLinus Torvalds 			break;
8151da177e4SLinus Torvalds 		}
8161da177e4SLinus Torvalds 
8171da177e4SLinus Torvalds 		if (copy_to_user(buf, page, retval)) {
8181da177e4SLinus Torvalds 			ret = -EFAULT;
8191da177e4SLinus Torvalds 			break;
8201da177e4SLinus Torvalds 		}
8211da177e4SLinus Torvalds 
8221da177e4SLinus Torvalds 		ret += retval;
8231da177e4SLinus Torvalds 		src += retval;
8241da177e4SLinus Torvalds 		buf += retval;
8251da177e4SLinus Torvalds 		count -= retval;
8261da177e4SLinus Torvalds 	}
8271da177e4SLinus Torvalds 	*ppos = src;
8281da177e4SLinus Torvalds 
8291da177e4SLinus Torvalds out_put:
8301da177e4SLinus Torvalds 	mmput(mm);
8311da177e4SLinus Torvalds out_free:
8321da177e4SLinus Torvalds 	free_page((unsigned long) page);
8331da177e4SLinus Torvalds out:
83499f89551SEric W. Biederman 	put_task_struct(task);
83599f89551SEric W. Biederman out_no_task:
8361da177e4SLinus Torvalds 	return ret;
8371da177e4SLinus Torvalds }
8381da177e4SLinus Torvalds 
8391da177e4SLinus Torvalds #define mem_write NULL
8401da177e4SLinus Torvalds 
8411da177e4SLinus Torvalds #ifndef mem_write
8421da177e4SLinus Torvalds /* This is a security hazard */
84363967fa9SGlauber de Oliveira Costa static ssize_t mem_write(struct file * file, const char __user *buf,
8441da177e4SLinus Torvalds 			 size_t count, loff_t *ppos)
8451da177e4SLinus Torvalds {
846f7ca54f4SFrederik Deweerdt 	int copied;
8471da177e4SLinus Torvalds 	char *page;
8482fddfeefSJosef "Jeff" Sipek 	struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
8491da177e4SLinus Torvalds 	unsigned long dst = *ppos;
8501da177e4SLinus Torvalds 
85199f89551SEric W. Biederman 	copied = -ESRCH;
85299f89551SEric W. Biederman 	if (!task)
85399f89551SEric W. Biederman 		goto out_no_task;
8541da177e4SLinus Torvalds 
855638fa202SRoland McGrath 	if (check_mem_permission(task))
85699f89551SEric W. Biederman 		goto out;
85799f89551SEric W. Biederman 
85899f89551SEric W. Biederman 	copied = -ENOMEM;
859e12ba74dSMel Gorman 	page = (char *)__get_free_page(GFP_TEMPORARY);
8601da177e4SLinus Torvalds 	if (!page)
86199f89551SEric W. Biederman 		goto out;
8621da177e4SLinus Torvalds 
863f7ca54f4SFrederik Deweerdt 	copied = 0;
8641da177e4SLinus Torvalds 	while (count > 0) {
8651da177e4SLinus Torvalds 		int this_len, retval;
8661da177e4SLinus Torvalds 
8671da177e4SLinus Torvalds 		this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
8681da177e4SLinus Torvalds 		if (copy_from_user(page, buf, this_len)) {
8691da177e4SLinus Torvalds 			copied = -EFAULT;
8701da177e4SLinus Torvalds 			break;
8711da177e4SLinus Torvalds 		}
8721da177e4SLinus Torvalds 		retval = access_process_vm(task, dst, page, this_len, 1);
8731da177e4SLinus Torvalds 		if (!retval) {
8741da177e4SLinus Torvalds 			if (!copied)
8751da177e4SLinus Torvalds 				copied = -EIO;
8761da177e4SLinus Torvalds 			break;
8771da177e4SLinus Torvalds 		}
8781da177e4SLinus Torvalds 		copied += retval;
8791da177e4SLinus Torvalds 		buf += retval;
8801da177e4SLinus Torvalds 		dst += retval;
8811da177e4SLinus Torvalds 		count -= retval;
8821da177e4SLinus Torvalds 	}
8831da177e4SLinus Torvalds 	*ppos = dst;
8841da177e4SLinus Torvalds 	free_page((unsigned long) page);
88599f89551SEric W. Biederman out:
88699f89551SEric W. Biederman 	put_task_struct(task);
88799f89551SEric W. Biederman out_no_task:
8881da177e4SLinus Torvalds 	return copied;
8891da177e4SLinus Torvalds }
8901da177e4SLinus Torvalds #endif
8911da177e4SLinus Torvalds 
89285863e47SMatt Mackall loff_t mem_lseek(struct file *file, loff_t offset, int orig)
8931da177e4SLinus Torvalds {
8941da177e4SLinus Torvalds 	switch (orig) {
8951da177e4SLinus Torvalds 	case 0:
8961da177e4SLinus Torvalds 		file->f_pos = offset;
8971da177e4SLinus Torvalds 		break;
8981da177e4SLinus Torvalds 	case 1:
8991da177e4SLinus Torvalds 		file->f_pos += offset;
9001da177e4SLinus Torvalds 		break;
9011da177e4SLinus Torvalds 	default:
9021da177e4SLinus Torvalds 		return -EINVAL;
9031da177e4SLinus Torvalds 	}
9041da177e4SLinus Torvalds 	force_successful_syscall_return();
9051da177e4SLinus Torvalds 	return file->f_pos;
9061da177e4SLinus Torvalds }
9071da177e4SLinus Torvalds 
90800977a59SArjan van de Ven static const struct file_operations proc_mem_operations = {
9091da177e4SLinus Torvalds 	.llseek		= mem_lseek,
9101da177e4SLinus Torvalds 	.read		= mem_read,
9111da177e4SLinus Torvalds 	.write		= mem_write,
9121da177e4SLinus Torvalds 	.open		= mem_open,
9131da177e4SLinus Torvalds };
9141da177e4SLinus Torvalds 
915315e28c8SJames Pearson static ssize_t environ_read(struct file *file, char __user *buf,
916315e28c8SJames Pearson 			size_t count, loff_t *ppos)
917315e28c8SJames Pearson {
918315e28c8SJames Pearson 	struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
919315e28c8SJames Pearson 	char *page;
920315e28c8SJames Pearson 	unsigned long src = *ppos;
921315e28c8SJames Pearson 	int ret = -ESRCH;
922315e28c8SJames Pearson 	struct mm_struct *mm;
923315e28c8SJames Pearson 
924315e28c8SJames Pearson 	if (!task)
925315e28c8SJames Pearson 		goto out_no_task;
926315e28c8SJames Pearson 
927006ebb40SStephen Smalley 	if (!ptrace_may_access(task, PTRACE_MODE_READ))
928315e28c8SJames Pearson 		goto out;
929315e28c8SJames Pearson 
930315e28c8SJames Pearson 	ret = -ENOMEM;
931315e28c8SJames Pearson 	page = (char *)__get_free_page(GFP_TEMPORARY);
932315e28c8SJames Pearson 	if (!page)
933315e28c8SJames Pearson 		goto out;
934315e28c8SJames Pearson 
935315e28c8SJames Pearson 	ret = 0;
936315e28c8SJames Pearson 
937315e28c8SJames Pearson 	mm = get_task_mm(task);
938315e28c8SJames Pearson 	if (!mm)
939315e28c8SJames Pearson 		goto out_free;
940315e28c8SJames Pearson 
941315e28c8SJames Pearson 	while (count > 0) {
942315e28c8SJames Pearson 		int this_len, retval, max_len;
943315e28c8SJames Pearson 
944315e28c8SJames Pearson 		this_len = mm->env_end - (mm->env_start + src);
945315e28c8SJames Pearson 
946315e28c8SJames Pearson 		if (this_len <= 0)
947315e28c8SJames Pearson 			break;
948315e28c8SJames Pearson 
949315e28c8SJames Pearson 		max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
950315e28c8SJames Pearson 		this_len = (this_len > max_len) ? max_len : this_len;
951315e28c8SJames Pearson 
952315e28c8SJames Pearson 		retval = access_process_vm(task, (mm->env_start + src),
953315e28c8SJames Pearson 			page, this_len, 0);
954315e28c8SJames Pearson 
955315e28c8SJames Pearson 		if (retval <= 0) {
956315e28c8SJames Pearson 			ret = retval;
957315e28c8SJames Pearson 			break;
958315e28c8SJames Pearson 		}
959315e28c8SJames Pearson 
960315e28c8SJames Pearson 		if (copy_to_user(buf, page, retval)) {
961315e28c8SJames Pearson 			ret = -EFAULT;
962315e28c8SJames Pearson 			break;
963315e28c8SJames Pearson 		}
964315e28c8SJames Pearson 
965315e28c8SJames Pearson 		ret += retval;
966315e28c8SJames Pearson 		src += retval;
967315e28c8SJames Pearson 		buf += retval;
968315e28c8SJames Pearson 		count -= retval;
969315e28c8SJames Pearson 	}
970315e28c8SJames Pearson 	*ppos = src;
971315e28c8SJames Pearson 
972315e28c8SJames Pearson 	mmput(mm);
973315e28c8SJames Pearson out_free:
974315e28c8SJames Pearson 	free_page((unsigned long) page);
975315e28c8SJames Pearson out:
976315e28c8SJames Pearson 	put_task_struct(task);
977315e28c8SJames Pearson out_no_task:
978315e28c8SJames Pearson 	return ret;
979315e28c8SJames Pearson }
980315e28c8SJames Pearson 
981315e28c8SJames Pearson static const struct file_operations proc_environ_operations = {
982315e28c8SJames Pearson 	.read		= environ_read,
98387df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
984315e28c8SJames Pearson };
985315e28c8SJames Pearson 
9861da177e4SLinus Torvalds static ssize_t oom_adjust_read(struct file *file, char __user *buf,
9871da177e4SLinus Torvalds 				size_t count, loff_t *ppos)
9881da177e4SLinus Torvalds {
9892fddfeefSJosef "Jeff" Sipek 	struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
9908578cea7SEric W. Biederman 	char buffer[PROC_NUMBUF];
9911da177e4SLinus Torvalds 	size_t len;
99228b83c51SKOSAKI Motohiro 	int oom_adjust = OOM_DISABLE;
99328b83c51SKOSAKI Motohiro 	unsigned long flags;
9941da177e4SLinus Torvalds 
99599f89551SEric W. Biederman 	if (!task)
99699f89551SEric W. Biederman 		return -ESRCH;
99728b83c51SKOSAKI Motohiro 
99828b83c51SKOSAKI Motohiro 	if (lock_task_sighand(task, &flags)) {
99928b83c51SKOSAKI Motohiro 		oom_adjust = task->signal->oom_adj;
100028b83c51SKOSAKI Motohiro 		unlock_task_sighand(task, &flags);
100128b83c51SKOSAKI Motohiro 	}
100228b83c51SKOSAKI Motohiro 
100399f89551SEric W. Biederman 	put_task_struct(task);
100499f89551SEric W. Biederman 
10058578cea7SEric W. Biederman 	len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
10060c28f287SAkinobu Mita 
10070c28f287SAkinobu Mita 	return simple_read_from_buffer(buf, count, ppos, buffer, len);
10081da177e4SLinus Torvalds }
10091da177e4SLinus Torvalds 
10101da177e4SLinus Torvalds static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
10111da177e4SLinus Torvalds 				size_t count, loff_t *ppos)
10121da177e4SLinus Torvalds {
101399f89551SEric W. Biederman 	struct task_struct *task;
10145d863b89SKOSAKI Motohiro 	char buffer[PROC_NUMBUF];
10155d863b89SKOSAKI Motohiro 	long oom_adjust;
101628b83c51SKOSAKI Motohiro 	unsigned long flags;
10175d863b89SKOSAKI Motohiro 	int err;
10181da177e4SLinus Torvalds 
10198578cea7SEric W. Biederman 	memset(buffer, 0, sizeof(buffer));
10208578cea7SEric W. Biederman 	if (count > sizeof(buffer) - 1)
10218578cea7SEric W. Biederman 		count = sizeof(buffer) - 1;
1022723548bfSDavid Rientjes 	if (copy_from_user(buffer, buf, count)) {
1023723548bfSDavid Rientjes 		err = -EFAULT;
1024723548bfSDavid Rientjes 		goto out;
1025723548bfSDavid Rientjes 	}
10265d863b89SKOSAKI Motohiro 
10275d863b89SKOSAKI Motohiro 	err = strict_strtol(strstrip(buffer), 0, &oom_adjust);
10285d863b89SKOSAKI Motohiro 	if (err)
1029723548bfSDavid Rientjes 		goto out;
10308ac773b4SAlexey Dobriyan 	if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
1031723548bfSDavid Rientjes 	     oom_adjust != OOM_DISABLE) {
1032723548bfSDavid Rientjes 		err = -EINVAL;
1033723548bfSDavid Rientjes 		goto out;
1034723548bfSDavid Rientjes 	}
10355d863b89SKOSAKI Motohiro 
10362fddfeefSJosef "Jeff" Sipek 	task = get_proc_task(file->f_path.dentry->d_inode);
1037723548bfSDavid Rientjes 	if (!task) {
1038723548bfSDavid Rientjes 		err = -ESRCH;
1039723548bfSDavid Rientjes 		goto out;
1040723548bfSDavid Rientjes 	}
104128b83c51SKOSAKI Motohiro 
10423d5992d2SYing Han 	task_lock(task);
10433d5992d2SYing Han 	if (!task->mm) {
1044723548bfSDavid Rientjes 		err = -EINVAL;
1045723548bfSDavid Rientjes 		goto err_task_lock;
10463d5992d2SYing Han 	}
10473d5992d2SYing Han 
1048d19d5476SDavid Rientjes 	if (!lock_task_sighand(task, &flags)) {
1049d19d5476SDavid Rientjes 		err = -ESRCH;
1050d19d5476SDavid Rientjes 		goto err_task_lock;
1051d19d5476SDavid Rientjes 	}
1052d19d5476SDavid Rientjes 
1053d19d5476SDavid Rientjes 	if (oom_adjust < task->signal->oom_adj && !capable(CAP_SYS_RESOURCE)) {
1054d19d5476SDavid Rientjes 		err = -EACCES;
1055d19d5476SDavid Rientjes 		goto err_sighand;
1056d19d5476SDavid Rientjes 	}
1057d19d5476SDavid Rientjes 
10583d5992d2SYing Han 	if (oom_adjust != task->signal->oom_adj) {
10593d5992d2SYing Han 		if (oom_adjust == OOM_DISABLE)
10603d5992d2SYing Han 			atomic_inc(&task->mm->oom_disable_count);
10613d5992d2SYing Han 		if (task->signal->oom_adj == OOM_DISABLE)
10623d5992d2SYing Han 			atomic_dec(&task->mm->oom_disable_count);
10633d5992d2SYing Han 	}
10643d5992d2SYing 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;
1083723548bfSDavid Rientjes err_sighand:
108428b83c51SKOSAKI Motohiro 	unlock_task_sighand(task, &flags);
1085d19d5476SDavid Rientjes err_task_lock:
1086d19d5476SDavid Rientjes 	task_unlock(task);
108799f89551SEric W. Biederman 	put_task_struct(task);
1088723548bfSDavid Rientjes out:
1089723548bfSDavid Rientjes 	return err < 0 ? err : count;
10901da177e4SLinus Torvalds }
10911da177e4SLinus Torvalds 
109200977a59SArjan van de Ven static const struct file_operations proc_oom_adjust_operations = {
10931da177e4SLinus Torvalds 	.read		= oom_adjust_read,
10941da177e4SLinus Torvalds 	.write		= oom_adjust_write,
109587df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
10961da177e4SLinus Torvalds };
10971da177e4SLinus Torvalds 
1098a63d83f4SDavid Rientjes static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1099a63d83f4SDavid Rientjes 					size_t count, loff_t *ppos)
1100a63d83f4SDavid Rientjes {
1101a63d83f4SDavid Rientjes 	struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
1102a63d83f4SDavid Rientjes 	char buffer[PROC_NUMBUF];
1103a63d83f4SDavid Rientjes 	int oom_score_adj = OOM_SCORE_ADJ_MIN;
1104a63d83f4SDavid Rientjes 	unsigned long flags;
1105a63d83f4SDavid Rientjes 	size_t len;
1106a63d83f4SDavid Rientjes 
1107a63d83f4SDavid Rientjes 	if (!task)
1108a63d83f4SDavid Rientjes 		return -ESRCH;
1109a63d83f4SDavid Rientjes 	if (lock_task_sighand(task, &flags)) {
1110a63d83f4SDavid Rientjes 		oom_score_adj = task->signal->oom_score_adj;
1111a63d83f4SDavid Rientjes 		unlock_task_sighand(task, &flags);
1112a63d83f4SDavid Rientjes 	}
1113a63d83f4SDavid Rientjes 	put_task_struct(task);
1114a63d83f4SDavid Rientjes 	len = snprintf(buffer, sizeof(buffer), "%d\n", oom_score_adj);
1115a63d83f4SDavid Rientjes 	return simple_read_from_buffer(buf, count, ppos, buffer, len);
1116a63d83f4SDavid Rientjes }
1117a63d83f4SDavid Rientjes 
1118a63d83f4SDavid Rientjes static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1119a63d83f4SDavid Rientjes 					size_t count, loff_t *ppos)
1120a63d83f4SDavid Rientjes {
1121a63d83f4SDavid Rientjes 	struct task_struct *task;
1122a63d83f4SDavid Rientjes 	char buffer[PROC_NUMBUF];
1123a63d83f4SDavid Rientjes 	unsigned long flags;
1124a63d83f4SDavid Rientjes 	long oom_score_adj;
1125a63d83f4SDavid Rientjes 	int err;
1126a63d83f4SDavid Rientjes 
1127a63d83f4SDavid Rientjes 	memset(buffer, 0, sizeof(buffer));
1128a63d83f4SDavid Rientjes 	if (count > sizeof(buffer) - 1)
1129a63d83f4SDavid Rientjes 		count = sizeof(buffer) - 1;
1130723548bfSDavid Rientjes 	if (copy_from_user(buffer, buf, count)) {
1131723548bfSDavid Rientjes 		err = -EFAULT;
1132723548bfSDavid Rientjes 		goto out;
1133723548bfSDavid Rientjes 	}
1134a63d83f4SDavid Rientjes 
1135a63d83f4SDavid Rientjes 	err = strict_strtol(strstrip(buffer), 0, &oom_score_adj);
1136a63d83f4SDavid Rientjes 	if (err)
1137723548bfSDavid Rientjes 		goto out;
1138a63d83f4SDavid Rientjes 	if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
1139723548bfSDavid Rientjes 			oom_score_adj > OOM_SCORE_ADJ_MAX) {
1140723548bfSDavid Rientjes 		err = -EINVAL;
1141723548bfSDavid Rientjes 		goto out;
1142723548bfSDavid Rientjes 	}
1143a63d83f4SDavid Rientjes 
1144a63d83f4SDavid Rientjes 	task = get_proc_task(file->f_path.dentry->d_inode);
1145723548bfSDavid Rientjes 	if (!task) {
1146723548bfSDavid Rientjes 		err = -ESRCH;
1147723548bfSDavid Rientjes 		goto out;
1148723548bfSDavid Rientjes 	}
1149a63d83f4SDavid Rientjes 
11503d5992d2SYing Han 	task_lock(task);
11513d5992d2SYing Han 	if (!task->mm) {
1152723548bfSDavid Rientjes 		err = -EINVAL;
1153723548bfSDavid Rientjes 		goto err_task_lock;
11543d5992d2SYing Han 	}
1155d19d5476SDavid Rientjes 
1156d19d5476SDavid Rientjes 	if (!lock_task_sighand(task, &flags)) {
1157d19d5476SDavid Rientjes 		err = -ESRCH;
1158d19d5476SDavid Rientjes 		goto err_task_lock;
1159d19d5476SDavid Rientjes 	}
1160d19d5476SDavid Rientjes 
1161d19d5476SDavid Rientjes 	if (oom_score_adj < task->signal->oom_score_adj &&
1162d19d5476SDavid Rientjes 			!capable(CAP_SYS_RESOURCE)) {
1163d19d5476SDavid Rientjes 		err = -EACCES;
1164d19d5476SDavid Rientjes 		goto err_sighand;
1165d19d5476SDavid Rientjes 	}
1166d19d5476SDavid Rientjes 
11673d5992d2SYing Han 	if (oom_score_adj != task->signal->oom_score_adj) {
11683d5992d2SYing Han 		if (oom_score_adj == OOM_SCORE_ADJ_MIN)
11693d5992d2SYing Han 			atomic_inc(&task->mm->oom_disable_count);
11703d5992d2SYing Han 		if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MIN)
11713d5992d2SYing Han 			atomic_dec(&task->mm->oom_disable_count);
11723d5992d2SYing Han 	}
1173a63d83f4SDavid Rientjes 	task->signal->oom_score_adj = oom_score_adj;
1174a63d83f4SDavid Rientjes 	/*
1175a63d83f4SDavid Rientjes 	 * Scale /proc/pid/oom_adj appropriately ensuring that OOM_DISABLE is
1176a63d83f4SDavid Rientjes 	 * always attainable.
1177a63d83f4SDavid Rientjes 	 */
1178a63d83f4SDavid Rientjes 	if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MIN)
1179a63d83f4SDavid Rientjes 		task->signal->oom_adj = OOM_DISABLE;
1180a63d83f4SDavid Rientjes 	else
1181a63d83f4SDavid Rientjes 		task->signal->oom_adj = (oom_score_adj * OOM_ADJUST_MAX) /
1182a63d83f4SDavid Rientjes 							OOM_SCORE_ADJ_MAX;
1183723548bfSDavid Rientjes err_sighand:
1184a63d83f4SDavid Rientjes 	unlock_task_sighand(task, &flags);
1185d19d5476SDavid Rientjes err_task_lock:
1186d19d5476SDavid Rientjes 	task_unlock(task);
1187a63d83f4SDavid Rientjes 	put_task_struct(task);
1188723548bfSDavid Rientjes out:
1189723548bfSDavid Rientjes 	return err < 0 ? err : count;
1190a63d83f4SDavid Rientjes }
1191a63d83f4SDavid Rientjes 
1192a63d83f4SDavid Rientjes static const struct file_operations proc_oom_score_adj_operations = {
1193a63d83f4SDavid Rientjes 	.read		= oom_score_adj_read,
1194a63d83f4SDavid Rientjes 	.write		= oom_score_adj_write,
11956038f373SArnd Bergmann 	.llseek		= default_llseek,
1196a63d83f4SDavid Rientjes };
1197a63d83f4SDavid Rientjes 
11981da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL
11991da177e4SLinus Torvalds #define TMPBUFLEN 21
12001da177e4SLinus Torvalds static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
12011da177e4SLinus Torvalds 				  size_t count, loff_t *ppos)
12021da177e4SLinus Torvalds {
12032fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
120499f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
12051da177e4SLinus Torvalds 	ssize_t length;
12061da177e4SLinus Torvalds 	char tmpbuf[TMPBUFLEN];
12071da177e4SLinus Torvalds 
120899f89551SEric W. Biederman 	if (!task)
120999f89551SEric W. Biederman 		return -ESRCH;
12101da177e4SLinus Torvalds 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
12110c11b942SAl Viro 				audit_get_loginuid(task));
121299f89551SEric W. Biederman 	put_task_struct(task);
12131da177e4SLinus Torvalds 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
12141da177e4SLinus Torvalds }
12151da177e4SLinus Torvalds 
12161da177e4SLinus Torvalds static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
12171da177e4SLinus Torvalds 				   size_t count, loff_t *ppos)
12181da177e4SLinus Torvalds {
12192fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
12201da177e4SLinus Torvalds 	char *page, *tmp;
12211da177e4SLinus Torvalds 	ssize_t length;
12221da177e4SLinus Torvalds 	uid_t loginuid;
12231da177e4SLinus Torvalds 
12241da177e4SLinus Torvalds 	if (!capable(CAP_AUDIT_CONTROL))
12251da177e4SLinus Torvalds 		return -EPERM;
12261da177e4SLinus Torvalds 
12277dc52157SPaul E. McKenney 	rcu_read_lock();
12287dc52157SPaul E. McKenney 	if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
12297dc52157SPaul E. McKenney 		rcu_read_unlock();
12301da177e4SLinus Torvalds 		return -EPERM;
12317dc52157SPaul E. McKenney 	}
12327dc52157SPaul E. McKenney 	rcu_read_unlock();
12331da177e4SLinus Torvalds 
1234e0182909SAl Viro 	if (count >= PAGE_SIZE)
1235e0182909SAl Viro 		count = PAGE_SIZE - 1;
12361da177e4SLinus Torvalds 
12371da177e4SLinus Torvalds 	if (*ppos != 0) {
12381da177e4SLinus Torvalds 		/* No partial writes. */
12391da177e4SLinus Torvalds 		return -EINVAL;
12401da177e4SLinus Torvalds 	}
1241e12ba74dSMel Gorman 	page = (char*)__get_free_page(GFP_TEMPORARY);
12421da177e4SLinus Torvalds 	if (!page)
12431da177e4SLinus Torvalds 		return -ENOMEM;
12441da177e4SLinus Torvalds 	length = -EFAULT;
12451da177e4SLinus Torvalds 	if (copy_from_user(page, buf, count))
12461da177e4SLinus Torvalds 		goto out_free_page;
12471da177e4SLinus Torvalds 
1248e0182909SAl Viro 	page[count] = '\0';
12491da177e4SLinus Torvalds 	loginuid = simple_strtoul(page, &tmp, 10);
12501da177e4SLinus Torvalds 	if (tmp == page) {
12511da177e4SLinus Torvalds 		length = -EINVAL;
12521da177e4SLinus Torvalds 		goto out_free_page;
12531da177e4SLinus Torvalds 
12541da177e4SLinus Torvalds 	}
125599f89551SEric W. Biederman 	length = audit_set_loginuid(current, loginuid);
12561da177e4SLinus Torvalds 	if (likely(length == 0))
12571da177e4SLinus Torvalds 		length = count;
12581da177e4SLinus Torvalds 
12591da177e4SLinus Torvalds out_free_page:
12601da177e4SLinus Torvalds 	free_page((unsigned long) page);
12611da177e4SLinus Torvalds 	return length;
12621da177e4SLinus Torvalds }
12631da177e4SLinus Torvalds 
126400977a59SArjan van de Ven static const struct file_operations proc_loginuid_operations = {
12651da177e4SLinus Torvalds 	.read		= proc_loginuid_read,
12661da177e4SLinus Torvalds 	.write		= proc_loginuid_write,
126787df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
12681da177e4SLinus Torvalds };
12691e0bd755SEric Paris 
12701e0bd755SEric Paris static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
12711e0bd755SEric Paris 				  size_t count, loff_t *ppos)
12721e0bd755SEric Paris {
12731e0bd755SEric Paris 	struct inode * inode = file->f_path.dentry->d_inode;
12741e0bd755SEric Paris 	struct task_struct *task = get_proc_task(inode);
12751e0bd755SEric Paris 	ssize_t length;
12761e0bd755SEric Paris 	char tmpbuf[TMPBUFLEN];
12771e0bd755SEric Paris 
12781e0bd755SEric Paris 	if (!task)
12791e0bd755SEric Paris 		return -ESRCH;
12801e0bd755SEric Paris 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
12811e0bd755SEric Paris 				audit_get_sessionid(task));
12821e0bd755SEric Paris 	put_task_struct(task);
12831e0bd755SEric Paris 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
12841e0bd755SEric Paris }
12851e0bd755SEric Paris 
12861e0bd755SEric Paris static const struct file_operations proc_sessionid_operations = {
12871e0bd755SEric Paris 	.read		= proc_sessionid_read,
128887df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
12891e0bd755SEric Paris };
12901da177e4SLinus Torvalds #endif
12911da177e4SLinus Torvalds 
1292f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
1293f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1294f4f154fdSAkinobu Mita 				      size_t count, loff_t *ppos)
1295f4f154fdSAkinobu Mita {
1296f4f154fdSAkinobu Mita 	struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1297f4f154fdSAkinobu Mita 	char buffer[PROC_NUMBUF];
1298f4f154fdSAkinobu Mita 	size_t len;
1299f4f154fdSAkinobu Mita 	int make_it_fail;
1300f4f154fdSAkinobu Mita 
1301f4f154fdSAkinobu Mita 	if (!task)
1302f4f154fdSAkinobu Mita 		return -ESRCH;
1303f4f154fdSAkinobu Mita 	make_it_fail = task->make_it_fail;
1304f4f154fdSAkinobu Mita 	put_task_struct(task);
1305f4f154fdSAkinobu Mita 
1306f4f154fdSAkinobu Mita 	len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
13070c28f287SAkinobu Mita 
13080c28f287SAkinobu Mita 	return simple_read_from_buffer(buf, count, ppos, buffer, len);
1309f4f154fdSAkinobu Mita }
1310f4f154fdSAkinobu Mita 
1311f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_write(struct file * file,
1312f4f154fdSAkinobu Mita 			const char __user * buf, size_t count, loff_t *ppos)
1313f4f154fdSAkinobu Mita {
1314f4f154fdSAkinobu Mita 	struct task_struct *task;
1315f4f154fdSAkinobu Mita 	char buffer[PROC_NUMBUF], *end;
1316f4f154fdSAkinobu Mita 	int make_it_fail;
1317f4f154fdSAkinobu Mita 
1318f4f154fdSAkinobu Mita 	if (!capable(CAP_SYS_RESOURCE))
1319f4f154fdSAkinobu Mita 		return -EPERM;
1320f4f154fdSAkinobu Mita 	memset(buffer, 0, sizeof(buffer));
1321f4f154fdSAkinobu Mita 	if (count > sizeof(buffer) - 1)
1322f4f154fdSAkinobu Mita 		count = sizeof(buffer) - 1;
1323f4f154fdSAkinobu Mita 	if (copy_from_user(buffer, buf, count))
1324f4f154fdSAkinobu Mita 		return -EFAULT;
1325cba8aafeSVincent Li 	make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1326cba8aafeSVincent Li 	if (*end)
1327cba8aafeSVincent Li 		return -EINVAL;
1328f4f154fdSAkinobu Mita 	task = get_proc_task(file->f_dentry->d_inode);
1329f4f154fdSAkinobu Mita 	if (!task)
1330f4f154fdSAkinobu Mita 		return -ESRCH;
1331f4f154fdSAkinobu Mita 	task->make_it_fail = make_it_fail;
1332f4f154fdSAkinobu Mita 	put_task_struct(task);
1333cba8aafeSVincent Li 
1334cba8aafeSVincent Li 	return count;
1335f4f154fdSAkinobu Mita }
1336f4f154fdSAkinobu Mita 
133700977a59SArjan van de Ven static const struct file_operations proc_fault_inject_operations = {
1338f4f154fdSAkinobu Mita 	.read		= proc_fault_inject_read,
1339f4f154fdSAkinobu Mita 	.write		= proc_fault_inject_write,
134087df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
1341f4f154fdSAkinobu Mita };
1342f4f154fdSAkinobu Mita #endif
1343f4f154fdSAkinobu Mita 
13449745512cSArjan van de Ven 
134543ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
134643ae34cbSIngo Molnar /*
134743ae34cbSIngo Molnar  * Print out various scheduling related per-task fields:
134843ae34cbSIngo Molnar  */
134943ae34cbSIngo Molnar static int sched_show(struct seq_file *m, void *v)
135043ae34cbSIngo Molnar {
135143ae34cbSIngo Molnar 	struct inode *inode = m->private;
135243ae34cbSIngo Molnar 	struct task_struct *p;
135343ae34cbSIngo Molnar 
135443ae34cbSIngo Molnar 	p = get_proc_task(inode);
135543ae34cbSIngo Molnar 	if (!p)
135643ae34cbSIngo Molnar 		return -ESRCH;
135743ae34cbSIngo Molnar 	proc_sched_show_task(p, m);
135843ae34cbSIngo Molnar 
135943ae34cbSIngo Molnar 	put_task_struct(p);
136043ae34cbSIngo Molnar 
136143ae34cbSIngo Molnar 	return 0;
136243ae34cbSIngo Molnar }
136343ae34cbSIngo Molnar 
136443ae34cbSIngo Molnar static ssize_t
136543ae34cbSIngo Molnar sched_write(struct file *file, const char __user *buf,
136643ae34cbSIngo Molnar 	    size_t count, loff_t *offset)
136743ae34cbSIngo Molnar {
136843ae34cbSIngo Molnar 	struct inode *inode = file->f_path.dentry->d_inode;
136943ae34cbSIngo Molnar 	struct task_struct *p;
137043ae34cbSIngo Molnar 
137143ae34cbSIngo Molnar 	p = get_proc_task(inode);
137243ae34cbSIngo Molnar 	if (!p)
137343ae34cbSIngo Molnar 		return -ESRCH;
137443ae34cbSIngo Molnar 	proc_sched_set_task(p);
137543ae34cbSIngo Molnar 
137643ae34cbSIngo Molnar 	put_task_struct(p);
137743ae34cbSIngo Molnar 
137843ae34cbSIngo Molnar 	return count;
137943ae34cbSIngo Molnar }
138043ae34cbSIngo Molnar 
138143ae34cbSIngo Molnar static int sched_open(struct inode *inode, struct file *filp)
138243ae34cbSIngo Molnar {
138343ae34cbSIngo Molnar 	int ret;
138443ae34cbSIngo Molnar 
138543ae34cbSIngo Molnar 	ret = single_open(filp, sched_show, NULL);
138643ae34cbSIngo Molnar 	if (!ret) {
138743ae34cbSIngo Molnar 		struct seq_file *m = filp->private_data;
138843ae34cbSIngo Molnar 
138943ae34cbSIngo Molnar 		m->private = inode;
139043ae34cbSIngo Molnar 	}
139143ae34cbSIngo Molnar 	return ret;
139243ae34cbSIngo Molnar }
139343ae34cbSIngo Molnar 
139443ae34cbSIngo Molnar static const struct file_operations proc_pid_sched_operations = {
139543ae34cbSIngo Molnar 	.open		= sched_open,
139643ae34cbSIngo Molnar 	.read		= seq_read,
139743ae34cbSIngo Molnar 	.write		= sched_write,
139843ae34cbSIngo Molnar 	.llseek		= seq_lseek,
13995ea473a1SAlexey Dobriyan 	.release	= single_release,
140043ae34cbSIngo Molnar };
140143ae34cbSIngo Molnar 
140243ae34cbSIngo Molnar #endif
140343ae34cbSIngo Molnar 
14045091faa4SMike Galbraith #ifdef CONFIG_SCHED_AUTOGROUP
14055091faa4SMike Galbraith /*
14065091faa4SMike Galbraith  * Print out autogroup related information:
14075091faa4SMike Galbraith  */
14085091faa4SMike Galbraith static int sched_autogroup_show(struct seq_file *m, void *v)
14095091faa4SMike Galbraith {
14105091faa4SMike Galbraith 	struct inode *inode = m->private;
14115091faa4SMike Galbraith 	struct task_struct *p;
14125091faa4SMike Galbraith 
14135091faa4SMike Galbraith 	p = get_proc_task(inode);
14145091faa4SMike Galbraith 	if (!p)
14155091faa4SMike Galbraith 		return -ESRCH;
14165091faa4SMike Galbraith 	proc_sched_autogroup_show_task(p, m);
14175091faa4SMike Galbraith 
14185091faa4SMike Galbraith 	put_task_struct(p);
14195091faa4SMike Galbraith 
14205091faa4SMike Galbraith 	return 0;
14215091faa4SMike Galbraith }
14225091faa4SMike Galbraith 
14235091faa4SMike Galbraith static ssize_t
14245091faa4SMike Galbraith sched_autogroup_write(struct file *file, const char __user *buf,
14255091faa4SMike Galbraith 	    size_t count, loff_t *offset)
14265091faa4SMike Galbraith {
14275091faa4SMike Galbraith 	struct inode *inode = file->f_path.dentry->d_inode;
14285091faa4SMike Galbraith 	struct task_struct *p;
14295091faa4SMike Galbraith 	char buffer[PROC_NUMBUF];
14305091faa4SMike Galbraith 	long nice;
14315091faa4SMike Galbraith 	int err;
14325091faa4SMike Galbraith 
14335091faa4SMike Galbraith 	memset(buffer, 0, sizeof(buffer));
14345091faa4SMike Galbraith 	if (count > sizeof(buffer) - 1)
14355091faa4SMike Galbraith 		count = sizeof(buffer) - 1;
14365091faa4SMike Galbraith 	if (copy_from_user(buffer, buf, count))
14375091faa4SMike Galbraith 		return -EFAULT;
14385091faa4SMike Galbraith 
14395091faa4SMike Galbraith 	err = strict_strtol(strstrip(buffer), 0, &nice);
14405091faa4SMike Galbraith 	if (err)
14415091faa4SMike Galbraith 		return -EINVAL;
14425091faa4SMike Galbraith 
14435091faa4SMike Galbraith 	p = get_proc_task(inode);
14445091faa4SMike Galbraith 	if (!p)
14455091faa4SMike Galbraith 		return -ESRCH;
14465091faa4SMike Galbraith 
14475091faa4SMike Galbraith 	err = nice;
14485091faa4SMike Galbraith 	err = proc_sched_autogroup_set_nice(p, &err);
14495091faa4SMike Galbraith 	if (err)
14505091faa4SMike Galbraith 		count = err;
14515091faa4SMike Galbraith 
14525091faa4SMike Galbraith 	put_task_struct(p);
14535091faa4SMike Galbraith 
14545091faa4SMike Galbraith 	return count;
14555091faa4SMike Galbraith }
14565091faa4SMike Galbraith 
14575091faa4SMike Galbraith static int sched_autogroup_open(struct inode *inode, struct file *filp)
14585091faa4SMike Galbraith {
14595091faa4SMike Galbraith 	int ret;
14605091faa4SMike Galbraith 
14615091faa4SMike Galbraith 	ret = single_open(filp, sched_autogroup_show, NULL);
14625091faa4SMike Galbraith 	if (!ret) {
14635091faa4SMike Galbraith 		struct seq_file *m = filp->private_data;
14645091faa4SMike Galbraith 
14655091faa4SMike Galbraith 		m->private = inode;
14665091faa4SMike Galbraith 	}
14675091faa4SMike Galbraith 	return ret;
14685091faa4SMike Galbraith }
14695091faa4SMike Galbraith 
14705091faa4SMike Galbraith static const struct file_operations proc_pid_sched_autogroup_operations = {
14715091faa4SMike Galbraith 	.open		= sched_autogroup_open,
14725091faa4SMike Galbraith 	.read		= seq_read,
14735091faa4SMike Galbraith 	.write		= sched_autogroup_write,
14745091faa4SMike Galbraith 	.llseek		= seq_lseek,
14755091faa4SMike Galbraith 	.release	= single_release,
14765091faa4SMike Galbraith };
14775091faa4SMike Galbraith 
14785091faa4SMike Galbraith #endif /* CONFIG_SCHED_AUTOGROUP */
14795091faa4SMike Galbraith 
14804614a696Sjohn stultz static ssize_t comm_write(struct file *file, const char __user *buf,
14814614a696Sjohn stultz 				size_t count, loff_t *offset)
14824614a696Sjohn stultz {
14834614a696Sjohn stultz 	struct inode *inode = file->f_path.dentry->d_inode;
14844614a696Sjohn stultz 	struct task_struct *p;
14854614a696Sjohn stultz 	char buffer[TASK_COMM_LEN];
14864614a696Sjohn stultz 
14874614a696Sjohn stultz 	memset(buffer, 0, sizeof(buffer));
14884614a696Sjohn stultz 	if (count > sizeof(buffer) - 1)
14894614a696Sjohn stultz 		count = sizeof(buffer) - 1;
14904614a696Sjohn stultz 	if (copy_from_user(buffer, buf, count))
14914614a696Sjohn stultz 		return -EFAULT;
14924614a696Sjohn stultz 
14934614a696Sjohn stultz 	p = get_proc_task(inode);
14944614a696Sjohn stultz 	if (!p)
14954614a696Sjohn stultz 		return -ESRCH;
14964614a696Sjohn stultz 
14974614a696Sjohn stultz 	if (same_thread_group(current, p))
14984614a696Sjohn stultz 		set_task_comm(p, buffer);
14994614a696Sjohn stultz 	else
15004614a696Sjohn stultz 		count = -EINVAL;
15014614a696Sjohn stultz 
15024614a696Sjohn stultz 	put_task_struct(p);
15034614a696Sjohn stultz 
15044614a696Sjohn stultz 	return count;
15054614a696Sjohn stultz }
15064614a696Sjohn stultz 
15074614a696Sjohn stultz static int comm_show(struct seq_file *m, void *v)
15084614a696Sjohn stultz {
15094614a696Sjohn stultz 	struct inode *inode = m->private;
15104614a696Sjohn stultz 	struct task_struct *p;
15114614a696Sjohn stultz 
15124614a696Sjohn stultz 	p = get_proc_task(inode);
15134614a696Sjohn stultz 	if (!p)
15144614a696Sjohn stultz 		return -ESRCH;
15154614a696Sjohn stultz 
15164614a696Sjohn stultz 	task_lock(p);
15174614a696Sjohn stultz 	seq_printf(m, "%s\n", p->comm);
15184614a696Sjohn stultz 	task_unlock(p);
15194614a696Sjohn stultz 
15204614a696Sjohn stultz 	put_task_struct(p);
15214614a696Sjohn stultz 
15224614a696Sjohn stultz 	return 0;
15234614a696Sjohn stultz }
15244614a696Sjohn stultz 
15254614a696Sjohn stultz static int comm_open(struct inode *inode, struct file *filp)
15264614a696Sjohn stultz {
15274614a696Sjohn stultz 	int ret;
15284614a696Sjohn stultz 
15294614a696Sjohn stultz 	ret = single_open(filp, comm_show, NULL);
15304614a696Sjohn stultz 	if (!ret) {
15314614a696Sjohn stultz 		struct seq_file *m = filp->private_data;
15324614a696Sjohn stultz 
15334614a696Sjohn stultz 		m->private = inode;
15344614a696Sjohn stultz 	}
15354614a696Sjohn stultz 	return ret;
15364614a696Sjohn stultz }
15374614a696Sjohn stultz 
15384614a696Sjohn stultz static const struct file_operations proc_pid_set_comm_operations = {
15394614a696Sjohn stultz 	.open		= comm_open,
15404614a696Sjohn stultz 	.read		= seq_read,
15414614a696Sjohn stultz 	.write		= comm_write,
15424614a696Sjohn stultz 	.llseek		= seq_lseek,
15434614a696Sjohn stultz 	.release	= single_release,
15444614a696Sjohn stultz };
15454614a696Sjohn stultz 
1546925d1c40SMatt Helsley /*
1547925d1c40SMatt Helsley  * We added or removed a vma mapping the executable. The vmas are only mapped
1548925d1c40SMatt Helsley  * during exec and are not mapped with the mmap system call.
1549925d1c40SMatt Helsley  * Callers must hold down_write() on the mm's mmap_sem for these
1550925d1c40SMatt Helsley  */
1551925d1c40SMatt Helsley void added_exe_file_vma(struct mm_struct *mm)
1552925d1c40SMatt Helsley {
1553925d1c40SMatt Helsley 	mm->num_exe_file_vmas++;
1554925d1c40SMatt Helsley }
1555925d1c40SMatt Helsley 
1556925d1c40SMatt Helsley void removed_exe_file_vma(struct mm_struct *mm)
1557925d1c40SMatt Helsley {
1558925d1c40SMatt Helsley 	mm->num_exe_file_vmas--;
1559925d1c40SMatt Helsley 	if ((mm->num_exe_file_vmas == 0) && mm->exe_file){
1560925d1c40SMatt Helsley 		fput(mm->exe_file);
1561925d1c40SMatt Helsley 		mm->exe_file = NULL;
1562925d1c40SMatt Helsley 	}
1563925d1c40SMatt Helsley 
1564925d1c40SMatt Helsley }
1565925d1c40SMatt Helsley 
1566925d1c40SMatt Helsley void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file)
1567925d1c40SMatt Helsley {
1568925d1c40SMatt Helsley 	if (new_exe_file)
1569925d1c40SMatt Helsley 		get_file(new_exe_file);
1570925d1c40SMatt Helsley 	if (mm->exe_file)
1571925d1c40SMatt Helsley 		fput(mm->exe_file);
1572925d1c40SMatt Helsley 	mm->exe_file = new_exe_file;
1573925d1c40SMatt Helsley 	mm->num_exe_file_vmas = 0;
1574925d1c40SMatt Helsley }
1575925d1c40SMatt Helsley 
1576925d1c40SMatt Helsley struct file *get_mm_exe_file(struct mm_struct *mm)
1577925d1c40SMatt Helsley {
1578925d1c40SMatt Helsley 	struct file *exe_file;
1579925d1c40SMatt Helsley 
1580925d1c40SMatt Helsley 	/* We need mmap_sem to protect against races with removal of
1581925d1c40SMatt Helsley 	 * VM_EXECUTABLE vmas */
1582925d1c40SMatt Helsley 	down_read(&mm->mmap_sem);
1583925d1c40SMatt Helsley 	exe_file = mm->exe_file;
1584925d1c40SMatt Helsley 	if (exe_file)
1585925d1c40SMatt Helsley 		get_file(exe_file);
1586925d1c40SMatt Helsley 	up_read(&mm->mmap_sem);
1587925d1c40SMatt Helsley 	return exe_file;
1588925d1c40SMatt Helsley }
1589925d1c40SMatt Helsley 
1590925d1c40SMatt Helsley void dup_mm_exe_file(struct mm_struct *oldmm, struct mm_struct *newmm)
1591925d1c40SMatt Helsley {
1592925d1c40SMatt Helsley 	/* It's safe to write the exe_file pointer without exe_file_lock because
1593925d1c40SMatt Helsley 	 * this is called during fork when the task is not yet in /proc */
1594925d1c40SMatt Helsley 	newmm->exe_file = get_mm_exe_file(oldmm);
1595925d1c40SMatt Helsley }
1596925d1c40SMatt Helsley 
1597925d1c40SMatt Helsley static int proc_exe_link(struct inode *inode, struct path *exe_path)
1598925d1c40SMatt Helsley {
1599925d1c40SMatt Helsley 	struct task_struct *task;
1600925d1c40SMatt Helsley 	struct mm_struct *mm;
1601925d1c40SMatt Helsley 	struct file *exe_file;
1602925d1c40SMatt Helsley 
1603925d1c40SMatt Helsley 	task = get_proc_task(inode);
1604925d1c40SMatt Helsley 	if (!task)
1605925d1c40SMatt Helsley 		return -ENOENT;
1606925d1c40SMatt Helsley 	mm = get_task_mm(task);
1607925d1c40SMatt Helsley 	put_task_struct(task);
1608925d1c40SMatt Helsley 	if (!mm)
1609925d1c40SMatt Helsley 		return -ENOENT;
1610925d1c40SMatt Helsley 	exe_file = get_mm_exe_file(mm);
1611925d1c40SMatt Helsley 	mmput(mm);
1612925d1c40SMatt Helsley 	if (exe_file) {
1613925d1c40SMatt Helsley 		*exe_path = exe_file->f_path;
1614925d1c40SMatt Helsley 		path_get(&exe_file->f_path);
1615925d1c40SMatt Helsley 		fput(exe_file);
1616925d1c40SMatt Helsley 		return 0;
1617925d1c40SMatt Helsley 	} else
1618925d1c40SMatt Helsley 		return -ENOENT;
1619925d1c40SMatt Helsley }
1620925d1c40SMatt Helsley 
1621008b150aSAl Viro static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
16221da177e4SLinus Torvalds {
16231da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
16241da177e4SLinus Torvalds 	int error = -EACCES;
16251da177e4SLinus Torvalds 
16261da177e4SLinus Torvalds 	/* We don't need a base pointer in the /proc filesystem */
16271d957f9bSJan Blunck 	path_put(&nd->path);
16281da177e4SLinus Torvalds 
1629778c1144SEric W. Biederman 	/* Are we allowed to snoop on the tasks file descriptors? */
1630778c1144SEric W. Biederman 	if (!proc_fd_access_allowed(inode))
16311da177e4SLinus Torvalds 		goto out;
16321da177e4SLinus Torvalds 
16333dcd25f3SJan Blunck 	error = PROC_I(inode)->op.proc_get_link(inode, &nd->path);
16341da177e4SLinus Torvalds out:
1635008b150aSAl Viro 	return ERR_PTR(error);
16361da177e4SLinus Torvalds }
16371da177e4SLinus Torvalds 
16383dcd25f3SJan Blunck static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
16391da177e4SLinus Torvalds {
1640e12ba74dSMel Gorman 	char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
16413dcd25f3SJan Blunck 	char *pathname;
16421da177e4SLinus Torvalds 	int len;
16431da177e4SLinus Torvalds 
16441da177e4SLinus Torvalds 	if (!tmp)
16451da177e4SLinus Torvalds 		return -ENOMEM;
16461da177e4SLinus Torvalds 
16477b2a69baSEric W. Biederman 	pathname = d_path(path, tmp, PAGE_SIZE);
16483dcd25f3SJan Blunck 	len = PTR_ERR(pathname);
16493dcd25f3SJan Blunck 	if (IS_ERR(pathname))
16501da177e4SLinus Torvalds 		goto out;
16513dcd25f3SJan Blunck 	len = tmp + PAGE_SIZE - 1 - pathname;
16521da177e4SLinus Torvalds 
16531da177e4SLinus Torvalds 	if (len > buflen)
16541da177e4SLinus Torvalds 		len = buflen;
16553dcd25f3SJan Blunck 	if (copy_to_user(buffer, pathname, len))
16561da177e4SLinus Torvalds 		len = -EFAULT;
16571da177e4SLinus Torvalds  out:
16581da177e4SLinus Torvalds 	free_page((unsigned long)tmp);
16591da177e4SLinus Torvalds 	return len;
16601da177e4SLinus Torvalds }
16611da177e4SLinus Torvalds 
16621da177e4SLinus Torvalds static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
16631da177e4SLinus Torvalds {
16641da177e4SLinus Torvalds 	int error = -EACCES;
16651da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
16663dcd25f3SJan Blunck 	struct path path;
16671da177e4SLinus Torvalds 
1668778c1144SEric W. Biederman 	/* Are we allowed to snoop on the tasks file descriptors? */
1669778c1144SEric W. Biederman 	if (!proc_fd_access_allowed(inode))
16701da177e4SLinus Torvalds 		goto out;
16711da177e4SLinus Torvalds 
16723dcd25f3SJan Blunck 	error = PROC_I(inode)->op.proc_get_link(inode, &path);
16731da177e4SLinus Torvalds 	if (error)
16741da177e4SLinus Torvalds 		goto out;
16751da177e4SLinus Torvalds 
16763dcd25f3SJan Blunck 	error = do_proc_readlink(&path, buffer, buflen);
16773dcd25f3SJan Blunck 	path_put(&path);
16781da177e4SLinus Torvalds out:
16791da177e4SLinus Torvalds 	return error;
16801da177e4SLinus Torvalds }
16811da177e4SLinus Torvalds 
1682c5ef1c42SArjan van de Ven static const struct inode_operations proc_pid_link_inode_operations = {
16831da177e4SLinus Torvalds 	.readlink	= proc_pid_readlink,
16846d76fa58SLinus Torvalds 	.follow_link	= proc_pid_follow_link,
16856d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
16861da177e4SLinus Torvalds };
16871da177e4SLinus Torvalds 
168828a6d671SEric W. Biederman 
168928a6d671SEric W. Biederman /* building an inode */
169028a6d671SEric W. Biederman 
169128a6d671SEric W. Biederman static int task_dumpable(struct task_struct *task)
169228a6d671SEric W. Biederman {
169328a6d671SEric W. Biederman 	int dumpable = 0;
169428a6d671SEric W. Biederman 	struct mm_struct *mm;
169528a6d671SEric W. Biederman 
169628a6d671SEric W. Biederman 	task_lock(task);
169728a6d671SEric W. Biederman 	mm = task->mm;
169828a6d671SEric W. Biederman 	if (mm)
16996c5d5238SKawai, Hidehiro 		dumpable = get_dumpable(mm);
170028a6d671SEric W. Biederman 	task_unlock(task);
170128a6d671SEric W. Biederman 	if(dumpable == 1)
170228a6d671SEric W. Biederman 		return 1;
170328a6d671SEric W. Biederman 	return 0;
170428a6d671SEric W. Biederman }
170528a6d671SEric W. Biederman 
170628a6d671SEric W. Biederman 
170761a28784SEric W. Biederman static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
170828a6d671SEric W. Biederman {
170928a6d671SEric W. Biederman 	struct inode * inode;
171028a6d671SEric W. Biederman 	struct proc_inode *ei;
1711c69e8d9cSDavid Howells 	const struct cred *cred;
171228a6d671SEric W. Biederman 
171328a6d671SEric W. Biederman 	/* We need a new inode */
171428a6d671SEric W. Biederman 
171528a6d671SEric W. Biederman 	inode = new_inode(sb);
171628a6d671SEric W. Biederman 	if (!inode)
171728a6d671SEric W. Biederman 		goto out;
171828a6d671SEric W. Biederman 
171928a6d671SEric W. Biederman 	/* Common stuff */
172028a6d671SEric W. Biederman 	ei = PROC_I(inode);
172185fe4025SChristoph Hellwig 	inode->i_ino = get_next_ino();
172228a6d671SEric W. Biederman 	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
172328a6d671SEric W. Biederman 	inode->i_op = &proc_def_inode_operations;
172428a6d671SEric W. Biederman 
172528a6d671SEric W. Biederman 	/*
172628a6d671SEric W. Biederman 	 * grab the reference to task.
172728a6d671SEric W. Biederman 	 */
17281a657f78SOleg Nesterov 	ei->pid = get_task_pid(task, PIDTYPE_PID);
172928a6d671SEric W. Biederman 	if (!ei->pid)
173028a6d671SEric W. Biederman 		goto out_unlock;
173128a6d671SEric W. Biederman 
173228a6d671SEric W. Biederman 	if (task_dumpable(task)) {
1733c69e8d9cSDavid Howells 		rcu_read_lock();
1734c69e8d9cSDavid Howells 		cred = __task_cred(task);
1735c69e8d9cSDavid Howells 		inode->i_uid = cred->euid;
1736c69e8d9cSDavid Howells 		inode->i_gid = cred->egid;
1737c69e8d9cSDavid Howells 		rcu_read_unlock();
173828a6d671SEric W. Biederman 	}
173928a6d671SEric W. Biederman 	security_task_to_inode(task, inode);
174028a6d671SEric W. Biederman 
174128a6d671SEric W. Biederman out:
174228a6d671SEric W. Biederman 	return inode;
174328a6d671SEric W. Biederman 
174428a6d671SEric W. Biederman out_unlock:
174528a6d671SEric W. Biederman 	iput(inode);
174628a6d671SEric W. Biederman 	return NULL;
174728a6d671SEric W. Biederman }
174828a6d671SEric W. Biederman 
174928a6d671SEric W. Biederman static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
175028a6d671SEric W. Biederman {
175128a6d671SEric W. Biederman 	struct inode *inode = dentry->d_inode;
175228a6d671SEric W. Biederman 	struct task_struct *task;
1753c69e8d9cSDavid Howells 	const struct cred *cred;
1754c69e8d9cSDavid Howells 
175528a6d671SEric W. Biederman 	generic_fillattr(inode, stat);
175628a6d671SEric W. Biederman 
175728a6d671SEric W. Biederman 	rcu_read_lock();
175828a6d671SEric W. Biederman 	stat->uid = 0;
175928a6d671SEric W. Biederman 	stat->gid = 0;
176028a6d671SEric W. Biederman 	task = pid_task(proc_pid(inode), PIDTYPE_PID);
176128a6d671SEric W. Biederman 	if (task) {
176228a6d671SEric W. Biederman 		if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
176328a6d671SEric W. Biederman 		    task_dumpable(task)) {
1764c69e8d9cSDavid Howells 			cred = __task_cred(task);
1765c69e8d9cSDavid Howells 			stat->uid = cred->euid;
1766c69e8d9cSDavid Howells 			stat->gid = cred->egid;
176728a6d671SEric W. Biederman 		}
176828a6d671SEric W. Biederman 	}
176928a6d671SEric W. Biederman 	rcu_read_unlock();
177028a6d671SEric W. Biederman 	return 0;
177128a6d671SEric W. Biederman }
177228a6d671SEric W. Biederman 
177328a6d671SEric W. Biederman /* dentry stuff */
177428a6d671SEric W. Biederman 
177528a6d671SEric W. Biederman /*
177628a6d671SEric W. Biederman  *	Exceptional case: normally we are not allowed to unhash a busy
177728a6d671SEric W. Biederman  * directory. In this case, however, we can do it - no aliasing problems
177828a6d671SEric W. Biederman  * due to the way we treat inodes.
177928a6d671SEric W. Biederman  *
178028a6d671SEric W. Biederman  * Rewrite the inode's ownerships here because the owning task may have
178128a6d671SEric W. Biederman  * performed a setuid(), etc.
178228a6d671SEric W. Biederman  *
178328a6d671SEric W. Biederman  * Before the /proc/pid/status file was created the only way to read
178428a6d671SEric W. Biederman  * the effective uid of a /process was to stat /proc/pid.  Reading
178528a6d671SEric W. Biederman  * /proc/pid/status is slow enough that procps and other packages
178628a6d671SEric W. Biederman  * kept stating /proc/pid.  To keep the rules in /proc simple I have
178728a6d671SEric W. Biederman  * made this apply to all per process world readable and executable
178828a6d671SEric W. Biederman  * directories.
178928a6d671SEric W. Biederman  */
179028a6d671SEric W. Biederman static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
179128a6d671SEric W. Biederman {
179234286d66SNick Piggin 	struct inode *inode;
179334286d66SNick Piggin 	struct task_struct *task;
1794c69e8d9cSDavid Howells 	const struct cred *cred;
1795c69e8d9cSDavid Howells 
179634286d66SNick Piggin 	if (nd && nd->flags & LOOKUP_RCU)
179734286d66SNick Piggin 		return -ECHILD;
179834286d66SNick Piggin 
179934286d66SNick Piggin 	inode = dentry->d_inode;
180034286d66SNick Piggin 	task = get_proc_task(inode);
180134286d66SNick Piggin 
180228a6d671SEric W. Biederman 	if (task) {
180328a6d671SEric W. Biederman 		if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
180428a6d671SEric W. Biederman 		    task_dumpable(task)) {
1805c69e8d9cSDavid Howells 			rcu_read_lock();
1806c69e8d9cSDavid Howells 			cred = __task_cred(task);
1807c69e8d9cSDavid Howells 			inode->i_uid = cred->euid;
1808c69e8d9cSDavid Howells 			inode->i_gid = cred->egid;
1809c69e8d9cSDavid Howells 			rcu_read_unlock();
181028a6d671SEric W. Biederman 		} else {
181128a6d671SEric W. Biederman 			inode->i_uid = 0;
181228a6d671SEric W. Biederman 			inode->i_gid = 0;
181328a6d671SEric W. Biederman 		}
181428a6d671SEric W. Biederman 		inode->i_mode &= ~(S_ISUID | S_ISGID);
181528a6d671SEric W. Biederman 		security_task_to_inode(task, inode);
181628a6d671SEric W. Biederman 		put_task_struct(task);
181728a6d671SEric W. Biederman 		return 1;
181828a6d671SEric W. Biederman 	}
181928a6d671SEric W. Biederman 	d_drop(dentry);
182028a6d671SEric W. Biederman 	return 0;
182128a6d671SEric W. Biederman }
182228a6d671SEric W. Biederman 
1823fe15ce44SNick Piggin static int pid_delete_dentry(const struct dentry * dentry)
182428a6d671SEric W. Biederman {
182528a6d671SEric W. Biederman 	/* Is the task we represent dead?
182628a6d671SEric W. Biederman 	 * If so, then don't put the dentry on the lru list,
182728a6d671SEric W. Biederman 	 * kill it immediately.
182828a6d671SEric W. Biederman 	 */
182928a6d671SEric W. Biederman 	return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
183028a6d671SEric W. Biederman }
183128a6d671SEric W. Biederman 
1832d72f71ebSAl Viro static const struct dentry_operations pid_dentry_operations =
183328a6d671SEric W. Biederman {
183428a6d671SEric W. Biederman 	.d_revalidate	= pid_revalidate,
183528a6d671SEric W. Biederman 	.d_delete	= pid_delete_dentry,
183628a6d671SEric W. Biederman };
183728a6d671SEric W. Biederman 
183828a6d671SEric W. Biederman /* Lookups */
183928a6d671SEric W. Biederman 
1840c5141e6dSEric Dumazet typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1841c5141e6dSEric Dumazet 				struct task_struct *, const void *);
184261a28784SEric W. Biederman 
18431c0d04c9SEric W. Biederman /*
18441c0d04c9SEric W. Biederman  * Fill a directory entry.
18451c0d04c9SEric W. Biederman  *
18461c0d04c9SEric W. Biederman  * If possible create the dcache entry and derive our inode number and
18471c0d04c9SEric W. Biederman  * file type from dcache entry.
18481c0d04c9SEric W. Biederman  *
18491c0d04c9SEric W. Biederman  * Since all of the proc inode numbers are dynamically generated, the inode
18501c0d04c9SEric W. Biederman  * numbers do not exist until the inode is cache.  This means creating the
18511c0d04c9SEric W. Biederman  * the dcache entry in readdir is necessary to keep the inode numbers
18521c0d04c9SEric W. Biederman  * reported by readdir in sync with the inode numbers reported
18531c0d04c9SEric W. Biederman  * by stat.
18541c0d04c9SEric W. Biederman  */
185561a28784SEric W. Biederman static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
185661a28784SEric W. Biederman 	char *name, int len,
1857c5141e6dSEric Dumazet 	instantiate_t instantiate, struct task_struct *task, const void *ptr)
185861a28784SEric W. Biederman {
18592fddfeefSJosef "Jeff" Sipek 	struct dentry *child, *dir = filp->f_path.dentry;
186061a28784SEric W. Biederman 	struct inode *inode;
186161a28784SEric W. Biederman 	struct qstr qname;
186261a28784SEric W. Biederman 	ino_t ino = 0;
186361a28784SEric W. Biederman 	unsigned type = DT_UNKNOWN;
186461a28784SEric W. Biederman 
186561a28784SEric W. Biederman 	qname.name = name;
186661a28784SEric W. Biederman 	qname.len  = len;
186761a28784SEric W. Biederman 	qname.hash = full_name_hash(name, len);
186861a28784SEric W. Biederman 
186961a28784SEric W. Biederman 	child = d_lookup(dir, &qname);
187061a28784SEric W. Biederman 	if (!child) {
187161a28784SEric W. Biederman 		struct dentry *new;
187261a28784SEric W. Biederman 		new = d_alloc(dir, &qname);
187361a28784SEric W. Biederman 		if (new) {
187461a28784SEric W. Biederman 			child = instantiate(dir->d_inode, new, task, ptr);
187561a28784SEric W. Biederman 			if (child)
187661a28784SEric W. Biederman 				dput(new);
187761a28784SEric W. Biederman 			else
187861a28784SEric W. Biederman 				child = new;
187961a28784SEric W. Biederman 		}
188061a28784SEric W. Biederman 	}
188161a28784SEric W. Biederman 	if (!child || IS_ERR(child) || !child->d_inode)
188261a28784SEric W. Biederman 		goto end_instantiate;
188361a28784SEric W. Biederman 	inode = child->d_inode;
188461a28784SEric W. Biederman 	if (inode) {
188561a28784SEric W. Biederman 		ino = inode->i_ino;
188661a28784SEric W. Biederman 		type = inode->i_mode >> 12;
188761a28784SEric W. Biederman 	}
188861a28784SEric W. Biederman 	dput(child);
188961a28784SEric W. Biederman end_instantiate:
189061a28784SEric W. Biederman 	if (!ino)
189161a28784SEric W. Biederman 		ino = find_inode_number(dir, &qname);
189261a28784SEric W. Biederman 	if (!ino)
189361a28784SEric W. Biederman 		ino = 1;
189461a28784SEric W. Biederman 	return filldir(dirent, name, len, filp->f_pos, ino, type);
189561a28784SEric W. Biederman }
189661a28784SEric W. Biederman 
189728a6d671SEric W. Biederman static unsigned name_to_int(struct dentry *dentry)
189828a6d671SEric W. Biederman {
189928a6d671SEric W. Biederman 	const char *name = dentry->d_name.name;
190028a6d671SEric W. Biederman 	int len = dentry->d_name.len;
190128a6d671SEric W. Biederman 	unsigned n = 0;
190228a6d671SEric W. Biederman 
190328a6d671SEric W. Biederman 	if (len > 1 && *name == '0')
190428a6d671SEric W. Biederman 		goto out;
190528a6d671SEric W. Biederman 	while (len-- > 0) {
190628a6d671SEric W. Biederman 		unsigned c = *name++ - '0';
190728a6d671SEric W. Biederman 		if (c > 9)
190828a6d671SEric W. Biederman 			goto out;
190928a6d671SEric W. Biederman 		if (n >= (~0U-9)/10)
191028a6d671SEric W. Biederman 			goto out;
191128a6d671SEric W. Biederman 		n *= 10;
191228a6d671SEric W. Biederman 		n += c;
191328a6d671SEric W. Biederman 	}
191428a6d671SEric W. Biederman 	return n;
191528a6d671SEric W. Biederman out:
191628a6d671SEric W. Biederman 	return ~0U;
191728a6d671SEric W. Biederman }
191828a6d671SEric W. Biederman 
191927932742SMiklos Szeredi #define PROC_FDINFO_MAX 64
192027932742SMiklos Szeredi 
19213dcd25f3SJan Blunck static int proc_fd_info(struct inode *inode, struct path *path, char *info)
192228a6d671SEric W. Biederman {
192328a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
192428a6d671SEric W. Biederman 	struct files_struct *files = NULL;
192528a6d671SEric W. Biederman 	struct file *file;
192628a6d671SEric W. Biederman 	int fd = proc_fd(inode);
192728a6d671SEric W. Biederman 
192828a6d671SEric W. Biederman 	if (task) {
192928a6d671SEric W. Biederman 		files = get_files_struct(task);
193028a6d671SEric W. Biederman 		put_task_struct(task);
193128a6d671SEric W. Biederman 	}
193228a6d671SEric W. Biederman 	if (files) {
193328a6d671SEric W. Biederman 		/*
193428a6d671SEric W. Biederman 		 * We are not taking a ref to the file structure, so we must
193528a6d671SEric W. Biederman 		 * hold ->file_lock.
193628a6d671SEric W. Biederman 		 */
193728a6d671SEric W. Biederman 		spin_lock(&files->file_lock);
193828a6d671SEric W. Biederman 		file = fcheck_files(files, fd);
193928a6d671SEric W. Biederman 		if (file) {
19403dcd25f3SJan Blunck 			if (path) {
19413dcd25f3SJan Blunck 				*path = file->f_path;
19423dcd25f3SJan Blunck 				path_get(&file->f_path);
19433dcd25f3SJan Blunck 			}
194427932742SMiklos Szeredi 			if (info)
194527932742SMiklos Szeredi 				snprintf(info, PROC_FDINFO_MAX,
194627932742SMiklos Szeredi 					 "pos:\t%lli\n"
194727932742SMiklos Szeredi 					 "flags:\t0%o\n",
194827932742SMiklos Szeredi 					 (long long) file->f_pos,
194927932742SMiklos Szeredi 					 file->f_flags);
195028a6d671SEric W. Biederman 			spin_unlock(&files->file_lock);
195128a6d671SEric W. Biederman 			put_files_struct(files);
195228a6d671SEric W. Biederman 			return 0;
195328a6d671SEric W. Biederman 		}
195428a6d671SEric W. Biederman 		spin_unlock(&files->file_lock);
195528a6d671SEric W. Biederman 		put_files_struct(files);
195628a6d671SEric W. Biederman 	}
195728a6d671SEric W. Biederman 	return -ENOENT;
195828a6d671SEric W. Biederman }
195928a6d671SEric W. Biederman 
19603dcd25f3SJan Blunck static int proc_fd_link(struct inode *inode, struct path *path)
196127932742SMiklos Szeredi {
19623dcd25f3SJan Blunck 	return proc_fd_info(inode, path, NULL);
196327932742SMiklos Szeredi }
196427932742SMiklos Szeredi 
196528a6d671SEric W. Biederman static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
196628a6d671SEric W. Biederman {
196734286d66SNick Piggin 	struct inode *inode;
196834286d66SNick Piggin 	struct task_struct *task;
196934286d66SNick Piggin 	int fd;
197028a6d671SEric W. Biederman 	struct files_struct *files;
1971c69e8d9cSDavid Howells 	const struct cred *cred;
197228a6d671SEric W. Biederman 
197334286d66SNick Piggin 	if (nd && nd->flags & LOOKUP_RCU)
197434286d66SNick Piggin 		return -ECHILD;
197534286d66SNick Piggin 
197634286d66SNick Piggin 	inode = dentry->d_inode;
197734286d66SNick Piggin 	task = get_proc_task(inode);
197834286d66SNick Piggin 	fd = proc_fd(inode);
197934286d66SNick Piggin 
198028a6d671SEric W. Biederman 	if (task) {
198128a6d671SEric W. Biederman 		files = get_files_struct(task);
198228a6d671SEric W. Biederman 		if (files) {
198328a6d671SEric W. Biederman 			rcu_read_lock();
198428a6d671SEric W. Biederman 			if (fcheck_files(files, fd)) {
198528a6d671SEric W. Biederman 				rcu_read_unlock();
198628a6d671SEric W. Biederman 				put_files_struct(files);
198728a6d671SEric W. Biederman 				if (task_dumpable(task)) {
1988c69e8d9cSDavid Howells 					rcu_read_lock();
1989c69e8d9cSDavid Howells 					cred = __task_cred(task);
1990c69e8d9cSDavid Howells 					inode->i_uid = cred->euid;
1991c69e8d9cSDavid Howells 					inode->i_gid = cred->egid;
1992c69e8d9cSDavid Howells 					rcu_read_unlock();
199328a6d671SEric W. Biederman 				} else {
199428a6d671SEric W. Biederman 					inode->i_uid = 0;
199528a6d671SEric W. Biederman 					inode->i_gid = 0;
199628a6d671SEric W. Biederman 				}
199728a6d671SEric W. Biederman 				inode->i_mode &= ~(S_ISUID | S_ISGID);
199828a6d671SEric W. Biederman 				security_task_to_inode(task, inode);
199928a6d671SEric W. Biederman 				put_task_struct(task);
200028a6d671SEric W. Biederman 				return 1;
200128a6d671SEric W. Biederman 			}
200228a6d671SEric W. Biederman 			rcu_read_unlock();
200328a6d671SEric W. Biederman 			put_files_struct(files);
200428a6d671SEric W. Biederman 		}
200528a6d671SEric W. Biederman 		put_task_struct(task);
200628a6d671SEric W. Biederman 	}
200728a6d671SEric W. Biederman 	d_drop(dentry);
200828a6d671SEric W. Biederman 	return 0;
200928a6d671SEric W. Biederman }
201028a6d671SEric W. Biederman 
2011d72f71ebSAl Viro static const struct dentry_operations tid_fd_dentry_operations =
201228a6d671SEric W. Biederman {
201328a6d671SEric W. Biederman 	.d_revalidate	= tid_fd_revalidate,
201428a6d671SEric W. Biederman 	.d_delete	= pid_delete_dentry,
201528a6d671SEric W. Biederman };
201628a6d671SEric W. Biederman 
2017444ceed8SEric W. Biederman static struct dentry *proc_fd_instantiate(struct inode *dir,
2018c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
201928a6d671SEric W. Biederman {
2020c5141e6dSEric Dumazet 	unsigned fd = *(const unsigned *)ptr;
202128a6d671SEric W. Biederman 	struct file *file;
202228a6d671SEric W. Biederman 	struct files_struct *files;
202328a6d671SEric W. Biederman  	struct inode *inode;
202428a6d671SEric W. Biederman  	struct proc_inode *ei;
2025444ceed8SEric W. Biederman 	struct dentry *error = ERR_PTR(-ENOENT);
202628a6d671SEric W. Biederman 
202761a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
202828a6d671SEric W. Biederman 	if (!inode)
202928a6d671SEric W. Biederman 		goto out;
203028a6d671SEric W. Biederman 	ei = PROC_I(inode);
203128a6d671SEric W. Biederman 	ei->fd = fd;
203228a6d671SEric W. Biederman 	files = get_files_struct(task);
203328a6d671SEric W. Biederman 	if (!files)
2034444ceed8SEric W. Biederman 		goto out_iput;
203528a6d671SEric W. Biederman 	inode->i_mode = S_IFLNK;
203628a6d671SEric W. Biederman 
203728a6d671SEric W. Biederman 	/*
203828a6d671SEric W. Biederman 	 * We are not taking a ref to the file structure, so we must
203928a6d671SEric W. Biederman 	 * hold ->file_lock.
204028a6d671SEric W. Biederman 	 */
204128a6d671SEric W. Biederman 	spin_lock(&files->file_lock);
204228a6d671SEric W. Biederman 	file = fcheck_files(files, fd);
204328a6d671SEric W. Biederman 	if (!file)
2044444ceed8SEric W. Biederman 		goto out_unlock;
2045aeb5d727SAl Viro 	if (file->f_mode & FMODE_READ)
204628a6d671SEric W. Biederman 		inode->i_mode |= S_IRUSR | S_IXUSR;
2047aeb5d727SAl Viro 	if (file->f_mode & FMODE_WRITE)
204828a6d671SEric W. Biederman 		inode->i_mode |= S_IWUSR | S_IXUSR;
204928a6d671SEric W. Biederman 	spin_unlock(&files->file_lock);
205028a6d671SEric W. Biederman 	put_files_struct(files);
2051444ceed8SEric W. Biederman 
205228a6d671SEric W. Biederman 	inode->i_op = &proc_pid_link_inode_operations;
205328a6d671SEric W. Biederman 	inode->i_size = 64;
205428a6d671SEric W. Biederman 	ei->op.proc_get_link = proc_fd_link;
2055fb045adbSNick Piggin 	d_set_d_op(dentry, &tid_fd_dentry_operations);
205628a6d671SEric W. Biederman 	d_add(dentry, inode);
205728a6d671SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
205828a6d671SEric W. Biederman 	if (tid_fd_revalidate(dentry, NULL))
2059444ceed8SEric W. Biederman 		error = NULL;
2060444ceed8SEric W. Biederman 
2061444ceed8SEric W. Biederman  out:
2062444ceed8SEric W. Biederman 	return error;
2063444ceed8SEric W. Biederman out_unlock:
2064444ceed8SEric W. Biederman 	spin_unlock(&files->file_lock);
2065444ceed8SEric W. Biederman 	put_files_struct(files);
2066444ceed8SEric W. Biederman out_iput:
2067444ceed8SEric W. Biederman 	iput(inode);
2068444ceed8SEric W. Biederman 	goto out;
2069444ceed8SEric W. Biederman }
2070444ceed8SEric W. Biederman 
207127932742SMiklos Szeredi static struct dentry *proc_lookupfd_common(struct inode *dir,
207227932742SMiklos Szeredi 					   struct dentry *dentry,
207327932742SMiklos Szeredi 					   instantiate_t instantiate)
2074444ceed8SEric W. Biederman {
2075444ceed8SEric W. Biederman 	struct task_struct *task = get_proc_task(dir);
2076444ceed8SEric W. Biederman 	unsigned fd = name_to_int(dentry);
2077444ceed8SEric W. Biederman 	struct dentry *result = ERR_PTR(-ENOENT);
2078444ceed8SEric W. Biederman 
2079444ceed8SEric W. Biederman 	if (!task)
2080444ceed8SEric W. Biederman 		goto out_no_task;
2081444ceed8SEric W. Biederman 	if (fd == ~0U)
2082444ceed8SEric W. Biederman 		goto out;
2083444ceed8SEric W. Biederman 
208427932742SMiklos Szeredi 	result = instantiate(dir, dentry, task, &fd);
208528a6d671SEric W. Biederman out:
208628a6d671SEric W. Biederman 	put_task_struct(task);
208728a6d671SEric W. Biederman out_no_task:
208828a6d671SEric W. Biederman 	return result;
208928a6d671SEric W. Biederman }
209028a6d671SEric W. Biederman 
209127932742SMiklos Szeredi static int proc_readfd_common(struct file * filp, void * dirent,
209227932742SMiklos Szeredi 			      filldir_t filldir, instantiate_t instantiate)
20931da177e4SLinus Torvalds {
20942fddfeefSJosef "Jeff" Sipek 	struct dentry *dentry = filp->f_path.dentry;
20955634708bSEric W. Biederman 	struct inode *inode = dentry->d_inode;
209699f89551SEric W. Biederman 	struct task_struct *p = get_proc_task(inode);
2097457c2510SPavel Emelyanov 	unsigned int fd, ino;
20981da177e4SLinus Torvalds 	int retval;
20991da177e4SLinus Torvalds 	struct files_struct * files;
21001da177e4SLinus Torvalds 
21011da177e4SLinus Torvalds 	retval = -ENOENT;
210299f89551SEric W. Biederman 	if (!p)
210399f89551SEric W. Biederman 		goto out_no_task;
21041da177e4SLinus Torvalds 	retval = 0;
21051da177e4SLinus Torvalds 
21061da177e4SLinus Torvalds 	fd = filp->f_pos;
21071da177e4SLinus Torvalds 	switch (fd) {
21081da177e4SLinus Torvalds 		case 0:
21091da177e4SLinus Torvalds 			if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
21101da177e4SLinus Torvalds 				goto out;
21111da177e4SLinus Torvalds 			filp->f_pos++;
21121da177e4SLinus Torvalds 		case 1:
21135634708bSEric W. Biederman 			ino = parent_ino(dentry);
21141da177e4SLinus Torvalds 			if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
21151da177e4SLinus Torvalds 				goto out;
21161da177e4SLinus Torvalds 			filp->f_pos++;
21171da177e4SLinus Torvalds 		default:
21181da177e4SLinus Torvalds 			files = get_files_struct(p);
21191da177e4SLinus Torvalds 			if (!files)
21201da177e4SLinus Torvalds 				goto out;
2121b835996fSDipankar Sarma 			rcu_read_lock();
21221da177e4SLinus Torvalds 			for (fd = filp->f_pos-2;
21239b4f526cSAl Viro 			     fd < files_fdtable(files)->max_fds;
21241da177e4SLinus Torvalds 			     fd++, filp->f_pos++) {
212527932742SMiklos Szeredi 				char name[PROC_NUMBUF];
212627932742SMiklos Szeredi 				int len;
21271da177e4SLinus Torvalds 
21281da177e4SLinus Torvalds 				if (!fcheck_files(files, fd))
21291da177e4SLinus Torvalds 					continue;
2130b835996fSDipankar Sarma 				rcu_read_unlock();
21311da177e4SLinus Torvalds 
213227932742SMiklos Szeredi 				len = snprintf(name, sizeof(name), "%d", fd);
213327932742SMiklos Szeredi 				if (proc_fill_cache(filp, dirent, filldir,
213427932742SMiklos Szeredi 						    name, len, instantiate,
213527932742SMiklos Szeredi 						    p, &fd) < 0) {
2136b835996fSDipankar Sarma 					rcu_read_lock();
21371da177e4SLinus Torvalds 					break;
21381da177e4SLinus Torvalds 				}
2139b835996fSDipankar Sarma 				rcu_read_lock();
21401da177e4SLinus Torvalds 			}
2141b835996fSDipankar Sarma 			rcu_read_unlock();
21421da177e4SLinus Torvalds 			put_files_struct(files);
21431da177e4SLinus Torvalds 	}
21441da177e4SLinus Torvalds out:
214599f89551SEric W. Biederman 	put_task_struct(p);
214699f89551SEric W. Biederman out_no_task:
21471da177e4SLinus Torvalds 	return retval;
21481da177e4SLinus Torvalds }
21491da177e4SLinus Torvalds 
215027932742SMiklos Szeredi static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
215127932742SMiklos Szeredi 				    struct nameidata *nd)
215227932742SMiklos Szeredi {
215327932742SMiklos Szeredi 	return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
215427932742SMiklos Szeredi }
215527932742SMiklos Szeredi 
215627932742SMiklos Szeredi static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
215727932742SMiklos Szeredi {
215827932742SMiklos Szeredi 	return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
215927932742SMiklos Szeredi }
216027932742SMiklos Szeredi 
216127932742SMiklos Szeredi static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
216227932742SMiklos Szeredi 				      size_t len, loff_t *ppos)
216327932742SMiklos Szeredi {
216427932742SMiklos Szeredi 	char tmp[PROC_FDINFO_MAX];
21653dcd25f3SJan Blunck 	int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
216627932742SMiklos Szeredi 	if (!err)
216727932742SMiklos Szeredi 		err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
216827932742SMiklos Szeredi 	return err;
216927932742SMiklos Szeredi }
217027932742SMiklos Szeredi 
217127932742SMiklos Szeredi static const struct file_operations proc_fdinfo_file_operations = {
217227932742SMiklos Szeredi 	.open           = nonseekable_open,
217327932742SMiklos Szeredi 	.read		= proc_fdinfo_read,
21746038f373SArnd Bergmann 	.llseek		= no_llseek,
217527932742SMiklos Szeredi };
217627932742SMiklos Szeredi 
217700977a59SArjan van de Ven static const struct file_operations proc_fd_operations = {
21781da177e4SLinus Torvalds 	.read		= generic_read_dir,
21791da177e4SLinus Torvalds 	.readdir	= proc_readfd,
21806038f373SArnd Bergmann 	.llseek		= default_llseek,
21811da177e4SLinus Torvalds };
21821da177e4SLinus Torvalds 
21831da177e4SLinus Torvalds /*
21848948e11fSAlexey Dobriyan  * /proc/pid/fd needs a special permission handler so that a process can still
21858948e11fSAlexey Dobriyan  * access /proc/self/fd after it has executed a setuid().
21868948e11fSAlexey Dobriyan  */
2187b74c79e9SNick Piggin static int proc_fd_permission(struct inode *inode, int mask, unsigned int flags)
21888948e11fSAlexey Dobriyan {
21898948e11fSAlexey Dobriyan 	int rv;
21908948e11fSAlexey Dobriyan 
2191b74c79e9SNick Piggin 	if (flags & IPERM_FLAG_RCU)
2192b74c79e9SNick Piggin 		return -ECHILD;
2193b74c79e9SNick Piggin 	rv = generic_permission(inode, mask, flags, NULL);
21948948e11fSAlexey Dobriyan 	if (rv == 0)
21958948e11fSAlexey Dobriyan 		return 0;
21968948e11fSAlexey Dobriyan 	if (task_pid(current) == proc_pid(inode))
21978948e11fSAlexey Dobriyan 		rv = 0;
21988948e11fSAlexey Dobriyan 	return rv;
21998948e11fSAlexey Dobriyan }
22008948e11fSAlexey Dobriyan 
22018948e11fSAlexey Dobriyan /*
22021da177e4SLinus Torvalds  * proc directories can do almost nothing..
22031da177e4SLinus Torvalds  */
2204c5ef1c42SArjan van de Ven static const struct inode_operations proc_fd_inode_operations = {
22051da177e4SLinus Torvalds 	.lookup		= proc_lookupfd,
22068948e11fSAlexey Dobriyan 	.permission	= proc_fd_permission,
22076d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
22081da177e4SLinus Torvalds };
22091da177e4SLinus Torvalds 
221027932742SMiklos Szeredi static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
221127932742SMiklos Szeredi 	struct dentry *dentry, struct task_struct *task, const void *ptr)
221227932742SMiklos Szeredi {
221327932742SMiklos Szeredi 	unsigned fd = *(unsigned *)ptr;
221427932742SMiklos Szeredi  	struct inode *inode;
221527932742SMiklos Szeredi  	struct proc_inode *ei;
221627932742SMiklos Szeredi 	struct dentry *error = ERR_PTR(-ENOENT);
221727932742SMiklos Szeredi 
221827932742SMiklos Szeredi 	inode = proc_pid_make_inode(dir->i_sb, task);
221927932742SMiklos Szeredi 	if (!inode)
222027932742SMiklos Szeredi 		goto out;
222127932742SMiklos Szeredi 	ei = PROC_I(inode);
222227932742SMiklos Szeredi 	ei->fd = fd;
222327932742SMiklos Szeredi 	inode->i_mode = S_IFREG | S_IRUSR;
222427932742SMiklos Szeredi 	inode->i_fop = &proc_fdinfo_file_operations;
2225fb045adbSNick Piggin 	d_set_d_op(dentry, &tid_fd_dentry_operations);
222627932742SMiklos Szeredi 	d_add(dentry, inode);
222727932742SMiklos Szeredi 	/* Close the race of the process dying before we return the dentry */
222827932742SMiklos Szeredi 	if (tid_fd_revalidate(dentry, NULL))
222927932742SMiklos Szeredi 		error = NULL;
223027932742SMiklos Szeredi 
223127932742SMiklos Szeredi  out:
223227932742SMiklos Szeredi 	return error;
223327932742SMiklos Szeredi }
223427932742SMiklos Szeredi 
223527932742SMiklos Szeredi static struct dentry *proc_lookupfdinfo(struct inode *dir,
223627932742SMiklos Szeredi 					struct dentry *dentry,
223727932742SMiklos Szeredi 					struct nameidata *nd)
223827932742SMiklos Szeredi {
223927932742SMiklos Szeredi 	return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
224027932742SMiklos Szeredi }
224127932742SMiklos Szeredi 
224227932742SMiklos Szeredi static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
224327932742SMiklos Szeredi {
224427932742SMiklos Szeredi 	return proc_readfd_common(filp, dirent, filldir,
224527932742SMiklos Szeredi 				  proc_fdinfo_instantiate);
224627932742SMiklos Szeredi }
224727932742SMiklos Szeredi 
224827932742SMiklos Szeredi static const struct file_operations proc_fdinfo_operations = {
224927932742SMiklos Szeredi 	.read		= generic_read_dir,
225027932742SMiklos Szeredi 	.readdir	= proc_readfdinfo,
22516038f373SArnd Bergmann 	.llseek		= default_llseek,
225227932742SMiklos Szeredi };
225327932742SMiklos Szeredi 
225427932742SMiklos Szeredi /*
225527932742SMiklos Szeredi  * proc directories can do almost nothing..
225627932742SMiklos Szeredi  */
225727932742SMiklos Szeredi static const struct inode_operations proc_fdinfo_inode_operations = {
225827932742SMiklos Szeredi 	.lookup		= proc_lookupfdinfo,
225927932742SMiklos Szeredi 	.setattr	= proc_setattr,
226027932742SMiklos Szeredi };
226127932742SMiklos Szeredi 
226227932742SMiklos Szeredi 
2263444ceed8SEric W. Biederman static struct dentry *proc_pident_instantiate(struct inode *dir,
2264c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
2265444ceed8SEric W. Biederman {
2266c5141e6dSEric Dumazet 	const struct pid_entry *p = ptr;
2267444ceed8SEric W. Biederman 	struct inode *inode;
2268444ceed8SEric W. Biederman 	struct proc_inode *ei;
2269bd6daba9SKOSAKI Motohiro 	struct dentry *error = ERR_PTR(-ENOENT);
2270444ceed8SEric W. Biederman 
227161a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
2272444ceed8SEric W. Biederman 	if (!inode)
2273444ceed8SEric W. Biederman 		goto out;
2274444ceed8SEric W. Biederman 
2275444ceed8SEric W. Biederman 	ei = PROC_I(inode);
2276444ceed8SEric W. Biederman 	inode->i_mode = p->mode;
2277444ceed8SEric W. Biederman 	if (S_ISDIR(inode->i_mode))
2278444ceed8SEric W. Biederman 		inode->i_nlink = 2;	/* Use getattr to fix if necessary */
2279444ceed8SEric W. Biederman 	if (p->iop)
2280444ceed8SEric W. Biederman 		inode->i_op = p->iop;
2281444ceed8SEric W. Biederman 	if (p->fop)
2282444ceed8SEric W. Biederman 		inode->i_fop = p->fop;
2283444ceed8SEric W. Biederman 	ei->op = p->op;
2284fb045adbSNick Piggin 	d_set_d_op(dentry, &pid_dentry_operations);
2285444ceed8SEric W. Biederman 	d_add(dentry, inode);
2286444ceed8SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
2287444ceed8SEric W. Biederman 	if (pid_revalidate(dentry, NULL))
2288444ceed8SEric W. Biederman 		error = NULL;
2289444ceed8SEric W. Biederman out:
2290444ceed8SEric W. Biederman 	return error;
2291444ceed8SEric W. Biederman }
2292444ceed8SEric W. Biederman 
22931da177e4SLinus Torvalds static struct dentry *proc_pident_lookup(struct inode *dir,
22941da177e4SLinus Torvalds 					 struct dentry *dentry,
2295c5141e6dSEric Dumazet 					 const struct pid_entry *ents,
22967bcd6b0eSEric W. Biederman 					 unsigned int nents)
22971da177e4SLinus Torvalds {
2298cd6a3ce9SEric W. Biederman 	struct dentry *error;
229999f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(dir);
2300c5141e6dSEric Dumazet 	const struct pid_entry *p, *last;
23011da177e4SLinus Torvalds 
2302cd6a3ce9SEric W. Biederman 	error = ERR_PTR(-ENOENT);
23031da177e4SLinus Torvalds 
230499f89551SEric W. Biederman 	if (!task)
230599f89551SEric W. Biederman 		goto out_no_task;
23061da177e4SLinus Torvalds 
230720cdc894SEric W. Biederman 	/*
230820cdc894SEric W. Biederman 	 * Yes, it does not scale. And it should not. Don't add
230920cdc894SEric W. Biederman 	 * new entries into /proc/<tgid>/ without very good reasons.
231020cdc894SEric W. Biederman 	 */
23117bcd6b0eSEric W. Biederman 	last = &ents[nents - 1];
23127bcd6b0eSEric W. Biederman 	for (p = ents; p <= last; p++) {
23131da177e4SLinus Torvalds 		if (p->len != dentry->d_name.len)
23141da177e4SLinus Torvalds 			continue;
23151da177e4SLinus Torvalds 		if (!memcmp(dentry->d_name.name, p->name, p->len))
23161da177e4SLinus Torvalds 			break;
23171da177e4SLinus Torvalds 	}
23187bcd6b0eSEric W. Biederman 	if (p > last)
23191da177e4SLinus Torvalds 		goto out;
23201da177e4SLinus Torvalds 
2321444ceed8SEric W. Biederman 	error = proc_pident_instantiate(dir, dentry, task, p);
23221da177e4SLinus Torvalds out:
232399f89551SEric W. Biederman 	put_task_struct(task);
232499f89551SEric W. Biederman out_no_task:
2325cd6a3ce9SEric W. Biederman 	return error;
23261da177e4SLinus Torvalds }
23271da177e4SLinus Torvalds 
2328c5141e6dSEric Dumazet static int proc_pident_fill_cache(struct file *filp, void *dirent,
2329c5141e6dSEric Dumazet 	filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
233061a28784SEric W. Biederman {
233161a28784SEric W. Biederman 	return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
233261a28784SEric W. Biederman 				proc_pident_instantiate, task, p);
233361a28784SEric W. Biederman }
233461a28784SEric W. Biederman 
233528a6d671SEric W. Biederman static int proc_pident_readdir(struct file *filp,
233628a6d671SEric W. Biederman 		void *dirent, filldir_t filldir,
2337c5141e6dSEric Dumazet 		const struct pid_entry *ents, unsigned int nents)
233828a6d671SEric W. Biederman {
233928a6d671SEric W. Biederman 	int i;
23402fddfeefSJosef "Jeff" Sipek 	struct dentry *dentry = filp->f_path.dentry;
234128a6d671SEric W. Biederman 	struct inode *inode = dentry->d_inode;
234228a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
2343c5141e6dSEric Dumazet 	const struct pid_entry *p, *last;
234428a6d671SEric W. Biederman 	ino_t ino;
234528a6d671SEric W. Biederman 	int ret;
234628a6d671SEric W. Biederman 
234728a6d671SEric W. Biederman 	ret = -ENOENT;
234828a6d671SEric W. Biederman 	if (!task)
234961a28784SEric W. Biederman 		goto out_no_task;
235028a6d671SEric W. Biederman 
235128a6d671SEric W. Biederman 	ret = 0;
235228a6d671SEric W. Biederman 	i = filp->f_pos;
235328a6d671SEric W. Biederman 	switch (i) {
235428a6d671SEric W. Biederman 	case 0:
235528a6d671SEric W. Biederman 		ino = inode->i_ino;
235628a6d671SEric W. Biederman 		if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
235728a6d671SEric W. Biederman 			goto out;
235828a6d671SEric W. Biederman 		i++;
235928a6d671SEric W. Biederman 		filp->f_pos++;
236028a6d671SEric W. Biederman 		/* fall through */
236128a6d671SEric W. Biederman 	case 1:
236228a6d671SEric W. Biederman 		ino = parent_ino(dentry);
236328a6d671SEric W. Biederman 		if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
236428a6d671SEric W. Biederman 			goto out;
236528a6d671SEric W. Biederman 		i++;
236628a6d671SEric W. Biederman 		filp->f_pos++;
236728a6d671SEric W. Biederman 		/* fall through */
236828a6d671SEric W. Biederman 	default:
236928a6d671SEric W. Biederman 		i -= 2;
237028a6d671SEric W. Biederman 		if (i >= nents) {
237128a6d671SEric W. Biederman 			ret = 1;
237228a6d671SEric W. Biederman 			goto out;
237328a6d671SEric W. Biederman 		}
237428a6d671SEric W. Biederman 		p = ents + i;
23757bcd6b0eSEric W. Biederman 		last = &ents[nents - 1];
23767bcd6b0eSEric W. Biederman 		while (p <= last) {
237761a28784SEric W. Biederman 			if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
237828a6d671SEric W. Biederman 				goto out;
237928a6d671SEric W. Biederman 			filp->f_pos++;
238028a6d671SEric W. Biederman 			p++;
238128a6d671SEric W. Biederman 		}
23821da177e4SLinus Torvalds 	}
23831da177e4SLinus Torvalds 
238428a6d671SEric W. Biederman 	ret = 1;
238528a6d671SEric W. Biederman out:
238661a28784SEric W. Biederman 	put_task_struct(task);
238761a28784SEric W. Biederman out_no_task:
238828a6d671SEric W. Biederman 	return ret;
23891da177e4SLinus Torvalds }
23901da177e4SLinus Torvalds 
23911da177e4SLinus Torvalds #ifdef CONFIG_SECURITY
239228a6d671SEric W. Biederman static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
239328a6d671SEric W. Biederman 				  size_t count, loff_t *ppos)
239428a6d671SEric W. Biederman {
23952fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
239604ff9708SAl Viro 	char *p = NULL;
239728a6d671SEric W. Biederman 	ssize_t length;
239828a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
239928a6d671SEric W. Biederman 
240028a6d671SEric W. Biederman 	if (!task)
240104ff9708SAl Viro 		return -ESRCH;
240228a6d671SEric W. Biederman 
240328a6d671SEric W. Biederman 	length = security_getprocattr(task,
24042fddfeefSJosef "Jeff" Sipek 				      (char*)file->f_path.dentry->d_name.name,
240504ff9708SAl Viro 				      &p);
240628a6d671SEric W. Biederman 	put_task_struct(task);
240704ff9708SAl Viro 	if (length > 0)
240804ff9708SAl Viro 		length = simple_read_from_buffer(buf, count, ppos, p, length);
240904ff9708SAl Viro 	kfree(p);
241028a6d671SEric W. Biederman 	return length;
241128a6d671SEric W. Biederman }
241228a6d671SEric W. Biederman 
241328a6d671SEric W. Biederman static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
241428a6d671SEric W. Biederman 				   size_t count, loff_t *ppos)
241528a6d671SEric W. Biederman {
24162fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
241728a6d671SEric W. Biederman 	char *page;
241828a6d671SEric W. Biederman 	ssize_t length;
241928a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
242028a6d671SEric W. Biederman 
242128a6d671SEric W. Biederman 	length = -ESRCH;
242228a6d671SEric W. Biederman 	if (!task)
242328a6d671SEric W. Biederman 		goto out_no_task;
242428a6d671SEric W. Biederman 	if (count > PAGE_SIZE)
242528a6d671SEric W. Biederman 		count = PAGE_SIZE;
242628a6d671SEric W. Biederman 
242728a6d671SEric W. Biederman 	/* No partial writes. */
242828a6d671SEric W. Biederman 	length = -EINVAL;
242928a6d671SEric W. Biederman 	if (*ppos != 0)
243028a6d671SEric W. Biederman 		goto out;
243128a6d671SEric W. Biederman 
243228a6d671SEric W. Biederman 	length = -ENOMEM;
2433e12ba74dSMel Gorman 	page = (char*)__get_free_page(GFP_TEMPORARY);
243428a6d671SEric W. Biederman 	if (!page)
243528a6d671SEric W. Biederman 		goto out;
243628a6d671SEric W. Biederman 
243728a6d671SEric W. Biederman 	length = -EFAULT;
243828a6d671SEric W. Biederman 	if (copy_from_user(page, buf, count))
243928a6d671SEric W. Biederman 		goto out_free;
244028a6d671SEric W. Biederman 
2441107db7c7SDavid Howells 	/* Guard against adverse ptrace interaction */
24429b1bf12dSKOSAKI Motohiro 	length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
2443107db7c7SDavid Howells 	if (length < 0)
2444107db7c7SDavid Howells 		goto out_free;
2445107db7c7SDavid Howells 
244628a6d671SEric W. Biederman 	length = security_setprocattr(task,
24472fddfeefSJosef "Jeff" Sipek 				      (char*)file->f_path.dentry->d_name.name,
244828a6d671SEric W. Biederman 				      (void*)page, count);
24499b1bf12dSKOSAKI Motohiro 	mutex_unlock(&task->signal->cred_guard_mutex);
245028a6d671SEric W. Biederman out_free:
245128a6d671SEric W. Biederman 	free_page((unsigned long) page);
245228a6d671SEric W. Biederman out:
245328a6d671SEric W. Biederman 	put_task_struct(task);
245428a6d671SEric W. Biederman out_no_task:
245528a6d671SEric W. Biederman 	return length;
245628a6d671SEric W. Biederman }
245728a6d671SEric W. Biederman 
245800977a59SArjan van de Ven static const struct file_operations proc_pid_attr_operations = {
245928a6d671SEric W. Biederman 	.read		= proc_pid_attr_read,
246028a6d671SEric W. Biederman 	.write		= proc_pid_attr_write,
246187df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
246228a6d671SEric W. Biederman };
246328a6d671SEric W. Biederman 
2464c5141e6dSEric Dumazet static const struct pid_entry attr_dir_stuff[] = {
2465631f9c18SAlexey Dobriyan 	REG("current",    S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2466631f9c18SAlexey Dobriyan 	REG("prev",       S_IRUGO,	   proc_pid_attr_operations),
2467631f9c18SAlexey Dobriyan 	REG("exec",       S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2468631f9c18SAlexey Dobriyan 	REG("fscreate",   S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2469631f9c18SAlexey Dobriyan 	REG("keycreate",  S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2470631f9c18SAlexey Dobriyan 	REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
247128a6d671SEric W. Biederman };
247228a6d671SEric W. Biederman 
247372d9dcfcSEric W. Biederman static int proc_attr_dir_readdir(struct file * filp,
24741da177e4SLinus Torvalds 			     void * dirent, filldir_t filldir)
24751da177e4SLinus Torvalds {
24761da177e4SLinus Torvalds 	return proc_pident_readdir(filp,dirent,filldir,
247772d9dcfcSEric W. Biederman 				   attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
24781da177e4SLinus Torvalds }
24791da177e4SLinus Torvalds 
248000977a59SArjan van de Ven static const struct file_operations proc_attr_dir_operations = {
24811da177e4SLinus Torvalds 	.read		= generic_read_dir,
248272d9dcfcSEric W. Biederman 	.readdir	= proc_attr_dir_readdir,
24836038f373SArnd Bergmann 	.llseek		= default_llseek,
24841da177e4SLinus Torvalds };
24851da177e4SLinus Torvalds 
248672d9dcfcSEric W. Biederman static struct dentry *proc_attr_dir_lookup(struct inode *dir,
24871da177e4SLinus Torvalds 				struct dentry *dentry, struct nameidata *nd)
24881da177e4SLinus Torvalds {
24897bcd6b0eSEric W. Biederman 	return proc_pident_lookup(dir, dentry,
24907bcd6b0eSEric W. Biederman 				  attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
24911da177e4SLinus Torvalds }
24921da177e4SLinus Torvalds 
2493c5ef1c42SArjan van de Ven static const struct inode_operations proc_attr_dir_inode_operations = {
249472d9dcfcSEric W. Biederman 	.lookup		= proc_attr_dir_lookup,
249599f89551SEric W. Biederman 	.getattr	= pid_getattr,
24966d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
24971da177e4SLinus Torvalds };
24981da177e4SLinus Torvalds 
24991da177e4SLinus Torvalds #endif
25001da177e4SLinus Torvalds 
2501698ba7b5SChristoph Hellwig #ifdef CONFIG_ELF_CORE
25023cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
25033cb4a0bbSKawai, Hidehiro 					 size_t count, loff_t *ppos)
25043cb4a0bbSKawai, Hidehiro {
25053cb4a0bbSKawai, Hidehiro 	struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
25063cb4a0bbSKawai, Hidehiro 	struct mm_struct *mm;
25073cb4a0bbSKawai, Hidehiro 	char buffer[PROC_NUMBUF];
25083cb4a0bbSKawai, Hidehiro 	size_t len;
25093cb4a0bbSKawai, Hidehiro 	int ret;
25103cb4a0bbSKawai, Hidehiro 
25113cb4a0bbSKawai, Hidehiro 	if (!task)
25123cb4a0bbSKawai, Hidehiro 		return -ESRCH;
25133cb4a0bbSKawai, Hidehiro 
25143cb4a0bbSKawai, Hidehiro 	ret = 0;
25153cb4a0bbSKawai, Hidehiro 	mm = get_task_mm(task);
25163cb4a0bbSKawai, Hidehiro 	if (mm) {
25173cb4a0bbSKawai, Hidehiro 		len = snprintf(buffer, sizeof(buffer), "%08lx\n",
25183cb4a0bbSKawai, Hidehiro 			       ((mm->flags & MMF_DUMP_FILTER_MASK) >>
25193cb4a0bbSKawai, Hidehiro 				MMF_DUMP_FILTER_SHIFT));
25203cb4a0bbSKawai, Hidehiro 		mmput(mm);
25213cb4a0bbSKawai, Hidehiro 		ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
25223cb4a0bbSKawai, Hidehiro 	}
25233cb4a0bbSKawai, Hidehiro 
25243cb4a0bbSKawai, Hidehiro 	put_task_struct(task);
25253cb4a0bbSKawai, Hidehiro 
25263cb4a0bbSKawai, Hidehiro 	return ret;
25273cb4a0bbSKawai, Hidehiro }
25283cb4a0bbSKawai, Hidehiro 
25293cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_write(struct file *file,
25303cb4a0bbSKawai, Hidehiro 					  const char __user *buf,
25313cb4a0bbSKawai, Hidehiro 					  size_t count,
25323cb4a0bbSKawai, Hidehiro 					  loff_t *ppos)
25333cb4a0bbSKawai, Hidehiro {
25343cb4a0bbSKawai, Hidehiro 	struct task_struct *task;
25353cb4a0bbSKawai, Hidehiro 	struct mm_struct *mm;
25363cb4a0bbSKawai, Hidehiro 	char buffer[PROC_NUMBUF], *end;
25373cb4a0bbSKawai, Hidehiro 	unsigned int val;
25383cb4a0bbSKawai, Hidehiro 	int ret;
25393cb4a0bbSKawai, Hidehiro 	int i;
25403cb4a0bbSKawai, Hidehiro 	unsigned long mask;
25413cb4a0bbSKawai, Hidehiro 
25423cb4a0bbSKawai, Hidehiro 	ret = -EFAULT;
25433cb4a0bbSKawai, Hidehiro 	memset(buffer, 0, sizeof(buffer));
25443cb4a0bbSKawai, Hidehiro 	if (count > sizeof(buffer) - 1)
25453cb4a0bbSKawai, Hidehiro 		count = sizeof(buffer) - 1;
25463cb4a0bbSKawai, Hidehiro 	if (copy_from_user(buffer, buf, count))
25473cb4a0bbSKawai, Hidehiro 		goto out_no_task;
25483cb4a0bbSKawai, Hidehiro 
25493cb4a0bbSKawai, Hidehiro 	ret = -EINVAL;
25503cb4a0bbSKawai, Hidehiro 	val = (unsigned int)simple_strtoul(buffer, &end, 0);
25513cb4a0bbSKawai, Hidehiro 	if (*end == '\n')
25523cb4a0bbSKawai, Hidehiro 		end++;
25533cb4a0bbSKawai, Hidehiro 	if (end - buffer == 0)
25543cb4a0bbSKawai, Hidehiro 		goto out_no_task;
25553cb4a0bbSKawai, Hidehiro 
25563cb4a0bbSKawai, Hidehiro 	ret = -ESRCH;
25573cb4a0bbSKawai, Hidehiro 	task = get_proc_task(file->f_dentry->d_inode);
25583cb4a0bbSKawai, Hidehiro 	if (!task)
25593cb4a0bbSKawai, Hidehiro 		goto out_no_task;
25603cb4a0bbSKawai, Hidehiro 
25613cb4a0bbSKawai, Hidehiro 	ret = end - buffer;
25623cb4a0bbSKawai, Hidehiro 	mm = get_task_mm(task);
25633cb4a0bbSKawai, Hidehiro 	if (!mm)
25643cb4a0bbSKawai, Hidehiro 		goto out_no_mm;
25653cb4a0bbSKawai, Hidehiro 
25663cb4a0bbSKawai, Hidehiro 	for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
25673cb4a0bbSKawai, Hidehiro 		if (val & mask)
25683cb4a0bbSKawai, Hidehiro 			set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
25693cb4a0bbSKawai, Hidehiro 		else
25703cb4a0bbSKawai, Hidehiro 			clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
25713cb4a0bbSKawai, Hidehiro 	}
25723cb4a0bbSKawai, Hidehiro 
25733cb4a0bbSKawai, Hidehiro 	mmput(mm);
25743cb4a0bbSKawai, Hidehiro  out_no_mm:
25753cb4a0bbSKawai, Hidehiro 	put_task_struct(task);
25763cb4a0bbSKawai, Hidehiro  out_no_task:
25773cb4a0bbSKawai, Hidehiro 	return ret;
25783cb4a0bbSKawai, Hidehiro }
25793cb4a0bbSKawai, Hidehiro 
25803cb4a0bbSKawai, Hidehiro static const struct file_operations proc_coredump_filter_operations = {
25813cb4a0bbSKawai, Hidehiro 	.read		= proc_coredump_filter_read,
25823cb4a0bbSKawai, Hidehiro 	.write		= proc_coredump_filter_write,
258387df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
25843cb4a0bbSKawai, Hidehiro };
25853cb4a0bbSKawai, Hidehiro #endif
25863cb4a0bbSKawai, Hidehiro 
25871da177e4SLinus Torvalds /*
25881da177e4SLinus Torvalds  * /proc/self:
25891da177e4SLinus Torvalds  */
25901da177e4SLinus Torvalds static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
25911da177e4SLinus Torvalds 			      int buflen)
25921da177e4SLinus Torvalds {
2593488e5bc4SEric W. Biederman 	struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2594b55fcb22SAndrew Morton 	pid_t tgid = task_tgid_nr_ns(current, ns);
25958578cea7SEric W. Biederman 	char tmp[PROC_NUMBUF];
2596b55fcb22SAndrew Morton 	if (!tgid)
2597488e5bc4SEric W. Biederman 		return -ENOENT;
2598b55fcb22SAndrew Morton 	sprintf(tmp, "%d", tgid);
25991da177e4SLinus Torvalds 	return vfs_readlink(dentry,buffer,buflen,tmp);
26001da177e4SLinus Torvalds }
26011da177e4SLinus Torvalds 
2602008b150aSAl Viro static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
26031da177e4SLinus Torvalds {
2604488e5bc4SEric W. Biederman 	struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2605b55fcb22SAndrew Morton 	pid_t tgid = task_tgid_nr_ns(current, ns);
26067fee4868SAl Viro 	char *name = ERR_PTR(-ENOENT);
26077fee4868SAl Viro 	if (tgid) {
26087fee4868SAl Viro 		name = __getname();
26097fee4868SAl Viro 		if (!name)
26107fee4868SAl Viro 			name = ERR_PTR(-ENOMEM);
26117fee4868SAl Viro 		else
26127fee4868SAl Viro 			sprintf(name, "%d", tgid);
26137fee4868SAl Viro 	}
26147fee4868SAl Viro 	nd_set_link(nd, name);
26157fee4868SAl Viro 	return NULL;
26167fee4868SAl Viro }
26177fee4868SAl Viro 
26187fee4868SAl Viro static void proc_self_put_link(struct dentry *dentry, struct nameidata *nd,
26197fee4868SAl Viro 				void *cookie)
26207fee4868SAl Viro {
26217fee4868SAl Viro 	char *s = nd_get_link(nd);
26227fee4868SAl Viro 	if (!IS_ERR(s))
26237fee4868SAl Viro 		__putname(s);
26241da177e4SLinus Torvalds }
26251da177e4SLinus Torvalds 
2626c5ef1c42SArjan van de Ven static const struct inode_operations proc_self_inode_operations = {
26271da177e4SLinus Torvalds 	.readlink	= proc_self_readlink,
26281da177e4SLinus Torvalds 	.follow_link	= proc_self_follow_link,
26297fee4868SAl Viro 	.put_link	= proc_self_put_link,
26301da177e4SLinus Torvalds };
26311da177e4SLinus Torvalds 
263228a6d671SEric W. Biederman /*
2633801199ceSEric W. Biederman  * proc base
2634801199ceSEric W. Biederman  *
2635801199ceSEric W. Biederman  * These are the directory entries in the root directory of /proc
2636801199ceSEric W. Biederman  * that properly belong to the /proc filesystem, as they describe
2637801199ceSEric W. Biederman  * describe something that is process related.
2638801199ceSEric W. Biederman  */
2639c5141e6dSEric Dumazet static const struct pid_entry proc_base_stuff[] = {
264061a28784SEric W. Biederman 	NOD("self", S_IFLNK|S_IRWXUGO,
2641801199ceSEric W. Biederman 		&proc_self_inode_operations, NULL, {}),
2642801199ceSEric W. Biederman };
2643801199ceSEric W. Biederman 
2644801199ceSEric W. Biederman /*
2645801199ceSEric W. Biederman  *	Exceptional case: normally we are not allowed to unhash a busy
2646801199ceSEric W. Biederman  * directory. In this case, however, we can do it - no aliasing problems
2647801199ceSEric W. Biederman  * due to the way we treat inodes.
2648801199ceSEric W. Biederman  */
2649801199ceSEric W. Biederman static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
2650801199ceSEric W. Biederman {
265134286d66SNick Piggin 	struct inode *inode;
265234286d66SNick Piggin 	struct task_struct *task;
265334286d66SNick Piggin 
265434286d66SNick Piggin 	if (nd->flags & LOOKUP_RCU)
265534286d66SNick Piggin 		return -ECHILD;
265634286d66SNick Piggin 
265734286d66SNick Piggin 	inode = dentry->d_inode;
265834286d66SNick Piggin 	task = get_proc_task(inode);
2659801199ceSEric W. Biederman 	if (task) {
2660801199ceSEric W. Biederman 		put_task_struct(task);
2661801199ceSEric W. Biederman 		return 1;
2662801199ceSEric W. Biederman 	}
2663801199ceSEric W. Biederman 	d_drop(dentry);
2664801199ceSEric W. Biederman 	return 0;
2665801199ceSEric W. Biederman }
2666801199ceSEric W. Biederman 
2667d72f71ebSAl Viro static const struct dentry_operations proc_base_dentry_operations =
2668801199ceSEric W. Biederman {
2669801199ceSEric W. Biederman 	.d_revalidate	= proc_base_revalidate,
2670801199ceSEric W. Biederman 	.d_delete	= pid_delete_dentry,
2671801199ceSEric W. Biederman };
2672801199ceSEric W. Biederman 
2673444ceed8SEric W. Biederman static struct dentry *proc_base_instantiate(struct inode *dir,
2674c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
2675801199ceSEric W. Biederman {
2676c5141e6dSEric Dumazet 	const struct pid_entry *p = ptr;
2677801199ceSEric W. Biederman 	struct inode *inode;
2678801199ceSEric W. Biederman 	struct proc_inode *ei;
267973d36460SDan Carpenter 	struct dentry *error;
2680801199ceSEric W. Biederman 
2681801199ceSEric W. Biederman 	/* Allocate the inode */
2682801199ceSEric W. Biederman 	error = ERR_PTR(-ENOMEM);
2683801199ceSEric W. Biederman 	inode = new_inode(dir->i_sb);
2684801199ceSEric W. Biederman 	if (!inode)
2685801199ceSEric W. Biederman 		goto out;
2686801199ceSEric W. Biederman 
2687801199ceSEric W. Biederman 	/* Initialize the inode */
2688801199ceSEric W. Biederman 	ei = PROC_I(inode);
268985fe4025SChristoph Hellwig 	inode->i_ino = get_next_ino();
2690801199ceSEric W. Biederman 	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2691801199ceSEric W. Biederman 
2692801199ceSEric W. Biederman 	/*
2693801199ceSEric W. Biederman 	 * grab the reference to the task.
2694801199ceSEric W. Biederman 	 */
26951a657f78SOleg Nesterov 	ei->pid = get_task_pid(task, PIDTYPE_PID);
2696801199ceSEric W. Biederman 	if (!ei->pid)
2697801199ceSEric W. Biederman 		goto out_iput;
2698801199ceSEric W. Biederman 
2699801199ceSEric W. Biederman 	inode->i_mode = p->mode;
2700801199ceSEric W. Biederman 	if (S_ISDIR(inode->i_mode))
2701801199ceSEric W. Biederman 		inode->i_nlink = 2;
2702801199ceSEric W. Biederman 	if (S_ISLNK(inode->i_mode))
2703801199ceSEric W. Biederman 		inode->i_size = 64;
2704801199ceSEric W. Biederman 	if (p->iop)
2705801199ceSEric W. Biederman 		inode->i_op = p->iop;
2706801199ceSEric W. Biederman 	if (p->fop)
2707801199ceSEric W. Biederman 		inode->i_fop = p->fop;
2708801199ceSEric W. Biederman 	ei->op = p->op;
2709fb045adbSNick Piggin 	d_set_d_op(dentry, &proc_base_dentry_operations);
2710801199ceSEric W. Biederman 	d_add(dentry, inode);
2711801199ceSEric W. Biederman 	error = NULL;
2712801199ceSEric W. Biederman out:
2713801199ceSEric W. Biederman 	return error;
2714801199ceSEric W. Biederman out_iput:
2715801199ceSEric W. Biederman 	iput(inode);
2716801199ceSEric W. Biederman 	goto out;
2717801199ceSEric W. Biederman }
2718801199ceSEric W. Biederman 
2719444ceed8SEric W. Biederman static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2720444ceed8SEric W. Biederman {
2721444ceed8SEric W. Biederman 	struct dentry *error;
2722444ceed8SEric W. Biederman 	struct task_struct *task = get_proc_task(dir);
2723c5141e6dSEric Dumazet 	const struct pid_entry *p, *last;
2724444ceed8SEric W. Biederman 
2725444ceed8SEric W. Biederman 	error = ERR_PTR(-ENOENT);
2726444ceed8SEric W. Biederman 
2727444ceed8SEric W. Biederman 	if (!task)
2728444ceed8SEric W. Biederman 		goto out_no_task;
2729444ceed8SEric W. Biederman 
2730444ceed8SEric W. Biederman 	/* Lookup the directory entry */
27317bcd6b0eSEric W. Biederman 	last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
27327bcd6b0eSEric W. Biederman 	for (p = proc_base_stuff; p <= last; p++) {
2733444ceed8SEric W. Biederman 		if (p->len != dentry->d_name.len)
2734444ceed8SEric W. Biederman 			continue;
2735444ceed8SEric W. Biederman 		if (!memcmp(dentry->d_name.name, p->name, p->len))
2736444ceed8SEric W. Biederman 			break;
2737444ceed8SEric W. Biederman 	}
27387bcd6b0eSEric W. Biederman 	if (p > last)
2739444ceed8SEric W. Biederman 		goto out;
2740444ceed8SEric W. Biederman 
2741444ceed8SEric W. Biederman 	error = proc_base_instantiate(dir, dentry, task, p);
2742444ceed8SEric W. Biederman 
2743444ceed8SEric W. Biederman out:
2744444ceed8SEric W. Biederman 	put_task_struct(task);
2745444ceed8SEric W. Biederman out_no_task:
2746444ceed8SEric W. Biederman 	return error;
2747444ceed8SEric W. Biederman }
2748444ceed8SEric W. Biederman 
2749c5141e6dSEric Dumazet static int proc_base_fill_cache(struct file *filp, void *dirent,
2750c5141e6dSEric Dumazet 	filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
275161a28784SEric W. Biederman {
275261a28784SEric W. Biederman 	return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
275361a28784SEric W. Biederman 				proc_base_instantiate, task, p);
275461a28784SEric W. Biederman }
275561a28784SEric W. Biederman 
2756aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING
2757297c5d92SAndrea Righi static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
2758aba76fdbSAndrew Morton {
2759940389b8SAndrea Righi 	struct task_io_accounting acct = task->ioac;
2760297c5d92SAndrea Righi 	unsigned long flags;
2761297c5d92SAndrea Righi 
27625995477aSAndrea Righi 	if (whole && lock_task_sighand(task, &flags)) {
2763b2d002dbSAndrea Righi 		struct task_struct *t = task;
2764297c5d92SAndrea Righi 
27655995477aSAndrea Righi 		task_io_accounting_add(&acct, &task->signal->ioac);
27665995477aSAndrea Righi 		while_each_thread(task, t)
27675995477aSAndrea Righi 			task_io_accounting_add(&acct, &t->ioac);
2768297c5d92SAndrea Righi 
2769297c5d92SAndrea Righi 		unlock_task_sighand(task, &flags);
2770297c5d92SAndrea Righi 	}
2771aba76fdbSAndrew Morton 	return sprintf(buffer,
2772aba76fdbSAndrew Morton 			"rchar: %llu\n"
2773aba76fdbSAndrew Morton 			"wchar: %llu\n"
2774aba76fdbSAndrew Morton 			"syscr: %llu\n"
2775aba76fdbSAndrew Morton 			"syscw: %llu\n"
2776aba76fdbSAndrew Morton 			"read_bytes: %llu\n"
2777aba76fdbSAndrew Morton 			"write_bytes: %llu\n"
2778aba76fdbSAndrew Morton 			"cancelled_write_bytes: %llu\n",
27797c44319dSAlexander Beregalov 			(unsigned long long)acct.rchar,
27807c44319dSAlexander Beregalov 			(unsigned long long)acct.wchar,
27817c44319dSAlexander Beregalov 			(unsigned long long)acct.syscr,
27827c44319dSAlexander Beregalov 			(unsigned long long)acct.syscw,
27837c44319dSAlexander Beregalov 			(unsigned long long)acct.read_bytes,
27847c44319dSAlexander Beregalov 			(unsigned long long)acct.write_bytes,
27857c44319dSAlexander Beregalov 			(unsigned long long)acct.cancelled_write_bytes);
2786aba76fdbSAndrew Morton }
2787297c5d92SAndrea Righi 
2788297c5d92SAndrea Righi static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2789297c5d92SAndrea Righi {
2790297c5d92SAndrea Righi 	return do_io_accounting(task, buffer, 0);
2791297c5d92SAndrea Righi }
2792297c5d92SAndrea Righi 
2793297c5d92SAndrea Righi static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2794297c5d92SAndrea Righi {
2795297c5d92SAndrea Righi 	return do_io_accounting(task, buffer, 1);
2796297c5d92SAndrea Righi }
2797297c5d92SAndrea Righi #endif /* CONFIG_TASK_IO_ACCOUNTING */
2798aba76fdbSAndrew Morton 
279947830723SKees Cook static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
280047830723SKees Cook 				struct pid *pid, struct task_struct *task)
280147830723SKees Cook {
280247830723SKees Cook 	seq_printf(m, "%08x\n", task->personality);
280347830723SKees Cook 	return 0;
280447830723SKees Cook }
280547830723SKees Cook 
2806801199ceSEric W. Biederman /*
280728a6d671SEric W. Biederman  * Thread groups
280828a6d671SEric W. Biederman  */
280900977a59SArjan van de Ven static const struct file_operations proc_task_operations;
2810c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations;
281120cdc894SEric W. Biederman 
2812c5141e6dSEric Dumazet static const struct pid_entry tgid_base_stuff[] = {
2813631f9c18SAlexey Dobriyan 	DIR("task",       S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2814631f9c18SAlexey Dobriyan 	DIR("fd",         S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2815631f9c18SAlexey Dobriyan 	DIR("fdinfo",     S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2816b2211a36SAndrew Morton #ifdef CONFIG_NET
2817631f9c18SAlexey Dobriyan 	DIR("net",        S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2818b2211a36SAndrew Morton #endif
2819631f9c18SAlexey Dobriyan 	REG("environ",    S_IRUSR, proc_environ_operations),
2820631f9c18SAlexey Dobriyan 	INF("auxv",       S_IRUSR, proc_pid_auxv),
2821631f9c18SAlexey Dobriyan 	ONE("status",     S_IRUGO, proc_pid_status),
2822631f9c18SAlexey Dobriyan 	ONE("personality", S_IRUSR, proc_pid_personality),
28233036e7b4SJiri Olsa 	INF("limits",	  S_IRUGO, proc_pid_limits),
282443ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
2825631f9c18SAlexey Dobriyan 	REG("sched",      S_IRUGO|S_IWUSR, proc_pid_sched_operations),
282643ae34cbSIngo Molnar #endif
28275091faa4SMike Galbraith #ifdef CONFIG_SCHED_AUTOGROUP
28285091faa4SMike Galbraith 	REG("autogroup",  S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
28295091faa4SMike Galbraith #endif
28304614a696Sjohn stultz 	REG("comm",      S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2831ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2832631f9c18SAlexey Dobriyan 	INF("syscall",    S_IRUSR, proc_pid_syscall),
2833ebcb6734SRoland McGrath #endif
2834631f9c18SAlexey Dobriyan 	INF("cmdline",    S_IRUGO, proc_pid_cmdline),
2835631f9c18SAlexey Dobriyan 	ONE("stat",       S_IRUGO, proc_tgid_stat),
2836631f9c18SAlexey Dobriyan 	ONE("statm",      S_IRUGO, proc_pid_statm),
2837631f9c18SAlexey Dobriyan 	REG("maps",       S_IRUGO, proc_maps_operations),
283828a6d671SEric W. Biederman #ifdef CONFIG_NUMA
2839631f9c18SAlexey Dobriyan 	REG("numa_maps",  S_IRUGO, proc_numa_maps_operations),
284028a6d671SEric W. Biederman #endif
2841631f9c18SAlexey Dobriyan 	REG("mem",        S_IRUSR|S_IWUSR, proc_mem_operations),
2842631f9c18SAlexey Dobriyan 	LNK("cwd",        proc_cwd_link),
2843631f9c18SAlexey Dobriyan 	LNK("root",       proc_root_link),
2844631f9c18SAlexey Dobriyan 	LNK("exe",        proc_exe_link),
2845631f9c18SAlexey Dobriyan 	REG("mounts",     S_IRUGO, proc_mounts_operations),
2846631f9c18SAlexey Dobriyan 	REG("mountinfo",  S_IRUGO, proc_mountinfo_operations),
2847631f9c18SAlexey Dobriyan 	REG("mountstats", S_IRUSR, proc_mountstats_operations),
28481e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR
2849631f9c18SAlexey Dobriyan 	REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2850631f9c18SAlexey Dobriyan 	REG("smaps",      S_IRUGO, proc_smaps_operations),
2851631f9c18SAlexey Dobriyan 	REG("pagemap",    S_IRUSR, proc_pagemap_operations),
285228a6d671SEric W. Biederman #endif
285328a6d671SEric W. Biederman #ifdef CONFIG_SECURITY
2854631f9c18SAlexey Dobriyan 	DIR("attr",       S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
285528a6d671SEric W. Biederman #endif
285628a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS
2857631f9c18SAlexey Dobriyan 	INF("wchan",      S_IRUGO, proc_pid_wchan),
285828a6d671SEric W. Biederman #endif
28592ec220e2SKen Chen #ifdef CONFIG_STACKTRACE
28602ec220e2SKen Chen 	ONE("stack",      S_IRUSR, proc_pid_stack),
286128a6d671SEric W. Biederman #endif
286228a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS
2863631f9c18SAlexey Dobriyan 	INF("schedstat",  S_IRUGO, proc_pid_schedstat),
286428a6d671SEric W. Biederman #endif
28659745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP
2866631f9c18SAlexey Dobriyan 	REG("latency",  S_IRUGO, proc_lstats_operations),
28679745512cSArjan van de Ven #endif
28688793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET
2869631f9c18SAlexey Dobriyan 	REG("cpuset",     S_IRUGO, proc_cpuset_operations),
287028a6d671SEric W. Biederman #endif
2871a424316cSPaul Menage #ifdef CONFIG_CGROUPS
2872631f9c18SAlexey Dobriyan 	REG("cgroup",  S_IRUGO, proc_cgroup_operations),
2873a424316cSPaul Menage #endif
2874631f9c18SAlexey Dobriyan 	INF("oom_score",  S_IRUGO, proc_oom_score),
2875631f9c18SAlexey Dobriyan 	REG("oom_adj",    S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
2876a63d83f4SDavid Rientjes 	REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
287728a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL
2878631f9c18SAlexey Dobriyan 	REG("loginuid",   S_IWUSR|S_IRUGO, proc_loginuid_operations),
2879631f9c18SAlexey Dobriyan 	REG("sessionid",  S_IRUGO, proc_sessionid_operations),
288028a6d671SEric W. Biederman #endif
2881f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
2882631f9c18SAlexey Dobriyan 	REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2883f4f154fdSAkinobu Mita #endif
2884698ba7b5SChristoph Hellwig #ifdef CONFIG_ELF_CORE
2885631f9c18SAlexey Dobriyan 	REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
28863cb4a0bbSKawai, Hidehiro #endif
2887aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING
2888631f9c18SAlexey Dobriyan 	INF("io",	S_IRUGO, proc_tgid_io_accounting),
2889aba76fdbSAndrew Morton #endif
289028a6d671SEric W. Biederman };
289128a6d671SEric W. Biederman 
289228a6d671SEric W. Biederman static int proc_tgid_base_readdir(struct file * filp,
289328a6d671SEric W. Biederman 			     void * dirent, filldir_t filldir)
289428a6d671SEric W. Biederman {
289528a6d671SEric W. Biederman 	return proc_pident_readdir(filp,dirent,filldir,
289628a6d671SEric W. Biederman 				   tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
289728a6d671SEric W. Biederman }
289828a6d671SEric W. Biederman 
289900977a59SArjan van de Ven static const struct file_operations proc_tgid_base_operations = {
290028a6d671SEric W. Biederman 	.read		= generic_read_dir,
290128a6d671SEric W. Biederman 	.readdir	= proc_tgid_base_readdir,
29026038f373SArnd Bergmann 	.llseek		= default_llseek,
290328a6d671SEric W. Biederman };
290428a6d671SEric W. Biederman 
290528a6d671SEric W. Biederman static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
29067bcd6b0eSEric W. Biederman 	return proc_pident_lookup(dir, dentry,
29077bcd6b0eSEric W. Biederman 				  tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
290828a6d671SEric W. Biederman }
290928a6d671SEric W. Biederman 
2910c5ef1c42SArjan van de Ven static const struct inode_operations proc_tgid_base_inode_operations = {
291128a6d671SEric W. Biederman 	.lookup		= proc_tgid_base_lookup,
291228a6d671SEric W. Biederman 	.getattr	= pid_getattr,
291328a6d671SEric W. Biederman 	.setattr	= proc_setattr,
291428a6d671SEric W. Biederman };
291528a6d671SEric W. Biederman 
291660347f67SPavel Emelyanov static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
29171da177e4SLinus Torvalds {
291848e6484dSEric W. Biederman 	struct dentry *dentry, *leader, *dir;
29198578cea7SEric W. Biederman 	char buf[PROC_NUMBUF];
292048e6484dSEric W. Biederman 	struct qstr name;
29211da177e4SLinus Torvalds 
292248e6484dSEric W. Biederman 	name.name = buf;
292360347f67SPavel Emelyanov 	name.len = snprintf(buf, sizeof(buf), "%d", pid);
292460347f67SPavel Emelyanov 	dentry = d_hash_and_lookup(mnt->mnt_root, &name);
292548e6484dSEric W. Biederman 	if (dentry) {
292648e6484dSEric W. Biederman 		shrink_dcache_parent(dentry);
292748e6484dSEric W. Biederman 		d_drop(dentry);
292848e6484dSEric W. Biederman 		dput(dentry);
29291da177e4SLinus Torvalds 	}
29301da177e4SLinus Torvalds 
293148e6484dSEric W. Biederman 	name.name = buf;
293260347f67SPavel Emelyanov 	name.len = snprintf(buf, sizeof(buf), "%d", tgid);
293360347f67SPavel Emelyanov 	leader = d_hash_and_lookup(mnt->mnt_root, &name);
293448e6484dSEric W. Biederman 	if (!leader)
293548e6484dSEric W. Biederman 		goto out;
293648e6484dSEric W. Biederman 
293748e6484dSEric W. Biederman 	name.name = "task";
293848e6484dSEric W. Biederman 	name.len = strlen(name.name);
293948e6484dSEric W. Biederman 	dir = d_hash_and_lookup(leader, &name);
294048e6484dSEric W. Biederman 	if (!dir)
294148e6484dSEric W. Biederman 		goto out_put_leader;
294248e6484dSEric W. Biederman 
294348e6484dSEric W. Biederman 	name.name = buf;
294460347f67SPavel Emelyanov 	name.len = snprintf(buf, sizeof(buf), "%d", pid);
294548e6484dSEric W. Biederman 	dentry = d_hash_and_lookup(dir, &name);
294648e6484dSEric W. Biederman 	if (dentry) {
294748e6484dSEric W. Biederman 		shrink_dcache_parent(dentry);
294848e6484dSEric W. Biederman 		d_drop(dentry);
294948e6484dSEric W. Biederman 		dput(dentry);
29501da177e4SLinus Torvalds 	}
295148e6484dSEric W. Biederman 
295248e6484dSEric W. Biederman 	dput(dir);
295348e6484dSEric W. Biederman out_put_leader:
295448e6484dSEric W. Biederman 	dput(leader);
295548e6484dSEric W. Biederman out:
295648e6484dSEric W. Biederman 	return;
29571da177e4SLinus Torvalds }
29581da177e4SLinus Torvalds 
29590895e91dSRandy Dunlap /**
29600895e91dSRandy Dunlap  * proc_flush_task -  Remove dcache entries for @task from the /proc dcache.
29610895e91dSRandy Dunlap  * @task: task that should be flushed.
29620895e91dSRandy Dunlap  *
29630895e91dSRandy Dunlap  * When flushing dentries from proc, one needs to flush them from global
296460347f67SPavel Emelyanov  * proc (proc_mnt) and from all the namespaces' procs this task was seen
29650895e91dSRandy Dunlap  * in. This call is supposed to do all of this job.
29660895e91dSRandy Dunlap  *
29670895e91dSRandy Dunlap  * Looks in the dcache for
29680895e91dSRandy Dunlap  * /proc/@pid
29690895e91dSRandy Dunlap  * /proc/@tgid/task/@pid
29700895e91dSRandy Dunlap  * if either directory is present flushes it and all of it'ts children
29710895e91dSRandy Dunlap  * from the dcache.
29720895e91dSRandy Dunlap  *
29730895e91dSRandy Dunlap  * It is safe and reasonable to cache /proc entries for a task until
29740895e91dSRandy Dunlap  * that task exits.  After that they just clog up the dcache with
29750895e91dSRandy Dunlap  * useless entries, possibly causing useful dcache entries to be
29760895e91dSRandy Dunlap  * flushed instead.  This routine is proved to flush those useless
29770895e91dSRandy Dunlap  * dcache entries at process exit time.
29780895e91dSRandy Dunlap  *
29790895e91dSRandy Dunlap  * NOTE: This routine is just an optimization so it does not guarantee
29800895e91dSRandy Dunlap  *       that no dcache entries will exist at process exit time it
29810895e91dSRandy Dunlap  *       just makes it very unlikely that any will persist.
298260347f67SPavel Emelyanov  */
298360347f67SPavel Emelyanov 
298460347f67SPavel Emelyanov void proc_flush_task(struct task_struct *task)
298560347f67SPavel Emelyanov {
29869fcc2d15SEric W. Biederman 	int i;
29879b4d1cbeSOleg Nesterov 	struct pid *pid, *tgid;
2988130f77ecSPavel Emelyanov 	struct upid *upid;
2989130f77ecSPavel Emelyanov 
2990130f77ecSPavel Emelyanov 	pid = task_pid(task);
2991130f77ecSPavel Emelyanov 	tgid = task_tgid(task);
29929fcc2d15SEric W. Biederman 
29939fcc2d15SEric W. Biederman 	for (i = 0; i <= pid->level; i++) {
2994130f77ecSPavel Emelyanov 		upid = &pid->numbers[i];
2995130f77ecSPavel Emelyanov 		proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
29969b4d1cbeSOleg Nesterov 					tgid->numbers[i].nr);
2997130f77ecSPavel Emelyanov 	}
29986f4e6433SPavel Emelyanov 
29996f4e6433SPavel Emelyanov 	upid = &pid->numbers[pid->level];
30006f4e6433SPavel Emelyanov 	if (upid->nr == 1)
30016f4e6433SPavel Emelyanov 		pid_ns_release_proc(upid->ns);
300260347f67SPavel Emelyanov }
300360347f67SPavel Emelyanov 
30049711ef99SAdrian Bunk static struct dentry *proc_pid_instantiate(struct inode *dir,
30059711ef99SAdrian Bunk 					   struct dentry * dentry,
3006c5141e6dSEric Dumazet 					   struct task_struct *task, const void *ptr)
3007444ceed8SEric W. Biederman {
3008444ceed8SEric W. Biederman 	struct dentry *error = ERR_PTR(-ENOENT);
3009444ceed8SEric W. Biederman 	struct inode *inode;
3010444ceed8SEric W. Biederman 
301161a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
3012444ceed8SEric W. Biederman 	if (!inode)
3013444ceed8SEric W. Biederman 		goto out;
3014444ceed8SEric W. Biederman 
3015444ceed8SEric W. Biederman 	inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3016444ceed8SEric W. Biederman 	inode->i_op = &proc_tgid_base_inode_operations;
3017444ceed8SEric W. Biederman 	inode->i_fop = &proc_tgid_base_operations;
3018444ceed8SEric W. Biederman 	inode->i_flags|=S_IMMUTABLE;
3019aed54175SVegard Nossum 
3020aed54175SVegard Nossum 	inode->i_nlink = 2 + pid_entry_count_dirs(tgid_base_stuff,
3021aed54175SVegard Nossum 		ARRAY_SIZE(tgid_base_stuff));
3022444ceed8SEric W. Biederman 
3023fb045adbSNick Piggin 	d_set_d_op(dentry, &pid_dentry_operations);
3024444ceed8SEric W. Biederman 
3025444ceed8SEric W. Biederman 	d_add(dentry, inode);
3026444ceed8SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
3027444ceed8SEric W. Biederman 	if (pid_revalidate(dentry, NULL))
3028444ceed8SEric W. Biederman 		error = NULL;
3029444ceed8SEric W. Biederman out:
3030444ceed8SEric W. Biederman 	return error;
3031444ceed8SEric W. Biederman }
3032444ceed8SEric W. Biederman 
30331da177e4SLinus Torvalds struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
30341da177e4SLinus Torvalds {
303573d36460SDan Carpenter 	struct dentry *result;
30361da177e4SLinus Torvalds 	struct task_struct *task;
30371da177e4SLinus Torvalds 	unsigned tgid;
3038b488893aSPavel Emelyanov 	struct pid_namespace *ns;
30391da177e4SLinus Torvalds 
3040801199ceSEric W. Biederman 	result = proc_base_lookup(dir, dentry);
3041801199ceSEric W. Biederman 	if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
3042801199ceSEric W. Biederman 		goto out;
3043801199ceSEric W. Biederman 
30441da177e4SLinus Torvalds 	tgid = name_to_int(dentry);
30451da177e4SLinus Torvalds 	if (tgid == ~0U)
30461da177e4SLinus Torvalds 		goto out;
30471da177e4SLinus Torvalds 
3048b488893aSPavel Emelyanov 	ns = dentry->d_sb->s_fs_info;
3049de758734SEric W. Biederman 	rcu_read_lock();
3050b488893aSPavel Emelyanov 	task = find_task_by_pid_ns(tgid, ns);
30511da177e4SLinus Torvalds 	if (task)
30521da177e4SLinus Torvalds 		get_task_struct(task);
3053de758734SEric W. Biederman 	rcu_read_unlock();
30541da177e4SLinus Torvalds 	if (!task)
30551da177e4SLinus Torvalds 		goto out;
30561da177e4SLinus Torvalds 
3057444ceed8SEric W. Biederman 	result = proc_pid_instantiate(dir, dentry, task, NULL);
305848e6484dSEric W. Biederman 	put_task_struct(task);
30591da177e4SLinus Torvalds out:
3060cd6a3ce9SEric W. Biederman 	return result;
30611da177e4SLinus Torvalds }
30621da177e4SLinus Torvalds 
30631da177e4SLinus Torvalds /*
30640804ef4bSEric W. Biederman  * Find the first task with tgid >= tgid
30650bc58a91SEric W. Biederman  *
30661da177e4SLinus Torvalds  */
306719fd4bb2SEric W. Biederman struct tgid_iter {
306819fd4bb2SEric W. Biederman 	unsigned int tgid;
30690804ef4bSEric W. Biederman 	struct task_struct *task;
307019fd4bb2SEric W. Biederman };
307119fd4bb2SEric W. Biederman static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
307219fd4bb2SEric W. Biederman {
30730804ef4bSEric W. Biederman 	struct pid *pid;
30741da177e4SLinus Torvalds 
307519fd4bb2SEric W. Biederman 	if (iter.task)
307619fd4bb2SEric W. Biederman 		put_task_struct(iter.task);
30770804ef4bSEric W. Biederman 	rcu_read_lock();
30780804ef4bSEric W. Biederman retry:
307919fd4bb2SEric W. Biederman 	iter.task = NULL;
308019fd4bb2SEric W. Biederman 	pid = find_ge_pid(iter.tgid, ns);
30810804ef4bSEric W. Biederman 	if (pid) {
308219fd4bb2SEric W. Biederman 		iter.tgid = pid_nr_ns(pid, ns);
308319fd4bb2SEric W. Biederman 		iter.task = pid_task(pid, PIDTYPE_PID);
30840804ef4bSEric W. Biederman 		/* What we to know is if the pid we have find is the
30850804ef4bSEric W. Biederman 		 * pid of a thread_group_leader.  Testing for task
30860804ef4bSEric W. Biederman 		 * being a thread_group_leader is the obvious thing
30870804ef4bSEric W. Biederman 		 * todo but there is a window when it fails, due to
30880804ef4bSEric W. Biederman 		 * the pid transfer logic in de_thread.
30890804ef4bSEric W. Biederman 		 *
30900804ef4bSEric W. Biederman 		 * So we perform the straight forward test of seeing
30910804ef4bSEric W. Biederman 		 * if the pid we have found is the pid of a thread
30920804ef4bSEric W. Biederman 		 * group leader, and don't worry if the task we have
30930804ef4bSEric W. Biederman 		 * found doesn't happen to be a thread group leader.
30940804ef4bSEric W. Biederman 		 * As we don't care in the case of readdir.
30950bc58a91SEric W. Biederman 		 */
309619fd4bb2SEric W. Biederman 		if (!iter.task || !has_group_leader_pid(iter.task)) {
309719fd4bb2SEric W. Biederman 			iter.tgid += 1;
30980804ef4bSEric W. Biederman 			goto retry;
309919fd4bb2SEric W. Biederman 		}
310019fd4bb2SEric W. Biederman 		get_task_struct(iter.task);
31011da177e4SLinus Torvalds 	}
3102454cc105SEric W. Biederman 	rcu_read_unlock();
310319fd4bb2SEric W. Biederman 	return iter;
31041da177e4SLinus Torvalds }
31051da177e4SLinus Torvalds 
31067bcd6b0eSEric W. Biederman #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
31071da177e4SLinus Torvalds 
310861a28784SEric W. Biederman static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
310919fd4bb2SEric W. Biederman 	struct tgid_iter iter)
311061a28784SEric W. Biederman {
311161a28784SEric W. Biederman 	char name[PROC_NUMBUF];
311219fd4bb2SEric W. Biederman 	int len = snprintf(name, sizeof(name), "%d", iter.tgid);
311361a28784SEric W. Biederman 	return proc_fill_cache(filp, dirent, filldir, name, len,
311419fd4bb2SEric W. Biederman 				proc_pid_instantiate, iter.task, NULL);
311561a28784SEric W. Biederman }
311661a28784SEric W. Biederman 
31171da177e4SLinus Torvalds /* for the /proc/ directory itself, after non-process stuff has been done */
31181da177e4SLinus Torvalds int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
31191da177e4SLinus Torvalds {
31201da177e4SLinus Torvalds 	unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
31212fddfeefSJosef "Jeff" Sipek 	struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
312219fd4bb2SEric W. Biederman 	struct tgid_iter iter;
3123b488893aSPavel Emelyanov 	struct pid_namespace *ns;
31241da177e4SLinus Torvalds 
312561a28784SEric W. Biederman 	if (!reaper)
312661a28784SEric W. Biederman 		goto out_no_task;
312761a28784SEric W. Biederman 
31287bcd6b0eSEric W. Biederman 	for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
3129c5141e6dSEric Dumazet 		const struct pid_entry *p = &proc_base_stuff[nr];
313061a28784SEric W. Biederman 		if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
3131801199ceSEric W. Biederman 			goto out;
31321da177e4SLinus Torvalds 	}
31331da177e4SLinus Torvalds 
3134b488893aSPavel Emelyanov 	ns = filp->f_dentry->d_sb->s_fs_info;
313519fd4bb2SEric W. Biederman 	iter.task = NULL;
313619fd4bb2SEric W. Biederman 	iter.tgid = filp->f_pos - TGID_OFFSET;
313719fd4bb2SEric W. Biederman 	for (iter = next_tgid(ns, iter);
313819fd4bb2SEric W. Biederman 	     iter.task;
313919fd4bb2SEric W. Biederman 	     iter.tgid += 1, iter = next_tgid(ns, iter)) {
314019fd4bb2SEric W. Biederman 		filp->f_pos = iter.tgid + TGID_OFFSET;
314119fd4bb2SEric W. Biederman 		if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
314219fd4bb2SEric W. Biederman 			put_task_struct(iter.task);
31430804ef4bSEric W. Biederman 			goto out;
31441da177e4SLinus Torvalds 		}
31451da177e4SLinus Torvalds 	}
31460804ef4bSEric W. Biederman 	filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
31470804ef4bSEric W. Biederman out:
314861a28784SEric W. Biederman 	put_task_struct(reaper);
314961a28784SEric W. Biederman out_no_task:
31501da177e4SLinus Torvalds 	return 0;
31511da177e4SLinus Torvalds }
31521da177e4SLinus Torvalds 
31530bc58a91SEric W. Biederman /*
315428a6d671SEric W. Biederman  * Tasks
315528a6d671SEric W. Biederman  */
3156c5141e6dSEric Dumazet static const struct pid_entry tid_base_stuff[] = {
3157631f9c18SAlexey Dobriyan 	DIR("fd",        S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
31583835541dSJerome Marchand 	DIR("fdinfo",    S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
3159631f9c18SAlexey Dobriyan 	REG("environ",   S_IRUSR, proc_environ_operations),
3160631f9c18SAlexey Dobriyan 	INF("auxv",      S_IRUSR, proc_pid_auxv),
3161631f9c18SAlexey Dobriyan 	ONE("status",    S_IRUGO, proc_pid_status),
3162631f9c18SAlexey Dobriyan 	ONE("personality", S_IRUSR, proc_pid_personality),
31633036e7b4SJiri Olsa 	INF("limits",	 S_IRUGO, proc_pid_limits),
316443ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
3165631f9c18SAlexey Dobriyan 	REG("sched",     S_IRUGO|S_IWUSR, proc_pid_sched_operations),
316643ae34cbSIngo Molnar #endif
31674614a696Sjohn stultz 	REG("comm",      S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
3168ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3169631f9c18SAlexey Dobriyan 	INF("syscall",   S_IRUSR, proc_pid_syscall),
3170ebcb6734SRoland McGrath #endif
3171631f9c18SAlexey Dobriyan 	INF("cmdline",   S_IRUGO, proc_pid_cmdline),
3172631f9c18SAlexey Dobriyan 	ONE("stat",      S_IRUGO, proc_tid_stat),
3173631f9c18SAlexey Dobriyan 	ONE("statm",     S_IRUGO, proc_pid_statm),
3174631f9c18SAlexey Dobriyan 	REG("maps",      S_IRUGO, proc_maps_operations),
317528a6d671SEric W. Biederman #ifdef CONFIG_NUMA
3176631f9c18SAlexey Dobriyan 	REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
317728a6d671SEric W. Biederman #endif
3178631f9c18SAlexey Dobriyan 	REG("mem",       S_IRUSR|S_IWUSR, proc_mem_operations),
3179631f9c18SAlexey Dobriyan 	LNK("cwd",       proc_cwd_link),
3180631f9c18SAlexey Dobriyan 	LNK("root",      proc_root_link),
3181631f9c18SAlexey Dobriyan 	LNK("exe",       proc_exe_link),
3182631f9c18SAlexey Dobriyan 	REG("mounts",    S_IRUGO, proc_mounts_operations),
3183631f9c18SAlexey Dobriyan 	REG("mountinfo",  S_IRUGO, proc_mountinfo_operations),
31841e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR
3185631f9c18SAlexey Dobriyan 	REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3186631f9c18SAlexey Dobriyan 	REG("smaps",     S_IRUGO, proc_smaps_operations),
3187631f9c18SAlexey Dobriyan 	REG("pagemap",    S_IRUSR, proc_pagemap_operations),
318828a6d671SEric W. Biederman #endif
318928a6d671SEric W. Biederman #ifdef CONFIG_SECURITY
3190631f9c18SAlexey Dobriyan 	DIR("attr",      S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
319128a6d671SEric W. Biederman #endif
319228a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS
3193631f9c18SAlexey Dobriyan 	INF("wchan",     S_IRUGO, proc_pid_wchan),
319428a6d671SEric W. Biederman #endif
31952ec220e2SKen Chen #ifdef CONFIG_STACKTRACE
31962ec220e2SKen Chen 	ONE("stack",      S_IRUSR, proc_pid_stack),
319728a6d671SEric W. Biederman #endif
319828a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS
3199631f9c18SAlexey Dobriyan 	INF("schedstat", S_IRUGO, proc_pid_schedstat),
320028a6d671SEric W. Biederman #endif
32019745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP
3202631f9c18SAlexey Dobriyan 	REG("latency",  S_IRUGO, proc_lstats_operations),
32039745512cSArjan van de Ven #endif
32048793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET
3205631f9c18SAlexey Dobriyan 	REG("cpuset",    S_IRUGO, proc_cpuset_operations),
320628a6d671SEric W. Biederman #endif
3207a424316cSPaul Menage #ifdef CONFIG_CGROUPS
3208631f9c18SAlexey Dobriyan 	REG("cgroup",  S_IRUGO, proc_cgroup_operations),
3209a424316cSPaul Menage #endif
3210631f9c18SAlexey Dobriyan 	INF("oom_score", S_IRUGO, proc_oom_score),
3211631f9c18SAlexey Dobriyan 	REG("oom_adj",   S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
3212a63d83f4SDavid Rientjes 	REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
321328a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL
3214631f9c18SAlexey Dobriyan 	REG("loginuid",  S_IWUSR|S_IRUGO, proc_loginuid_operations),
3215631f9c18SAlexey Dobriyan 	REG("sessionid",  S_IRUSR, proc_sessionid_operations),
321628a6d671SEric W. Biederman #endif
3217f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
3218631f9c18SAlexey Dobriyan 	REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3219f4f154fdSAkinobu Mita #endif
3220297c5d92SAndrea Righi #ifdef CONFIG_TASK_IO_ACCOUNTING
3221631f9c18SAlexey Dobriyan 	INF("io",	S_IRUGO, proc_tid_io_accounting),
3222297c5d92SAndrea Righi #endif
322328a6d671SEric W. Biederman };
322428a6d671SEric W. Biederman 
322528a6d671SEric W. Biederman static int proc_tid_base_readdir(struct file * filp,
322628a6d671SEric W. Biederman 			     void * dirent, filldir_t filldir)
322728a6d671SEric W. Biederman {
322828a6d671SEric W. Biederman 	return proc_pident_readdir(filp,dirent,filldir,
322928a6d671SEric W. Biederman 				   tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
323028a6d671SEric W. Biederman }
323128a6d671SEric W. Biederman 
323228a6d671SEric W. Biederman static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
32337bcd6b0eSEric W. Biederman 	return proc_pident_lookup(dir, dentry,
32347bcd6b0eSEric W. Biederman 				  tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
323528a6d671SEric W. Biederman }
323628a6d671SEric W. Biederman 
323700977a59SArjan van de Ven static const struct file_operations proc_tid_base_operations = {
323828a6d671SEric W. Biederman 	.read		= generic_read_dir,
323928a6d671SEric W. Biederman 	.readdir	= proc_tid_base_readdir,
32406038f373SArnd Bergmann 	.llseek		= default_llseek,
324128a6d671SEric W. Biederman };
324228a6d671SEric W. Biederman 
3243c5ef1c42SArjan van de Ven static const struct inode_operations proc_tid_base_inode_operations = {
324428a6d671SEric W. Biederman 	.lookup		= proc_tid_base_lookup,
324528a6d671SEric W. Biederman 	.getattr	= pid_getattr,
324628a6d671SEric W. Biederman 	.setattr	= proc_setattr,
324728a6d671SEric W. Biederman };
324828a6d671SEric W. Biederman 
3249444ceed8SEric W. Biederman static struct dentry *proc_task_instantiate(struct inode *dir,
3250c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
3251444ceed8SEric W. Biederman {
3252444ceed8SEric W. Biederman 	struct dentry *error = ERR_PTR(-ENOENT);
3253444ceed8SEric W. Biederman 	struct inode *inode;
325461a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
3255444ceed8SEric W. Biederman 
3256444ceed8SEric W. Biederman 	if (!inode)
3257444ceed8SEric W. Biederman 		goto out;
3258444ceed8SEric W. Biederman 	inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3259444ceed8SEric W. Biederman 	inode->i_op = &proc_tid_base_inode_operations;
3260444ceed8SEric W. Biederman 	inode->i_fop = &proc_tid_base_operations;
3261444ceed8SEric W. Biederman 	inode->i_flags|=S_IMMUTABLE;
3262aed54175SVegard Nossum 
3263aed54175SVegard Nossum 	inode->i_nlink = 2 + pid_entry_count_dirs(tid_base_stuff,
3264aed54175SVegard Nossum 		ARRAY_SIZE(tid_base_stuff));
3265444ceed8SEric W. Biederman 
3266fb045adbSNick Piggin 	d_set_d_op(dentry, &pid_dentry_operations);
3267444ceed8SEric W. Biederman 
3268444ceed8SEric W. Biederman 	d_add(dentry, inode);
3269444ceed8SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
3270444ceed8SEric W. Biederman 	if (pid_revalidate(dentry, NULL))
3271444ceed8SEric W. Biederman 		error = NULL;
3272444ceed8SEric W. Biederman out:
3273444ceed8SEric W. Biederman 	return error;
3274444ceed8SEric W. Biederman }
3275444ceed8SEric W. Biederman 
327628a6d671SEric W. Biederman static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
327728a6d671SEric W. Biederman {
327828a6d671SEric W. Biederman 	struct dentry *result = ERR_PTR(-ENOENT);
327928a6d671SEric W. Biederman 	struct task_struct *task;
328028a6d671SEric W. Biederman 	struct task_struct *leader = get_proc_task(dir);
328128a6d671SEric W. Biederman 	unsigned tid;
3282b488893aSPavel Emelyanov 	struct pid_namespace *ns;
328328a6d671SEric W. Biederman 
328428a6d671SEric W. Biederman 	if (!leader)
328528a6d671SEric W. Biederman 		goto out_no_task;
328628a6d671SEric W. Biederman 
328728a6d671SEric W. Biederman 	tid = name_to_int(dentry);
328828a6d671SEric W. Biederman 	if (tid == ~0U)
328928a6d671SEric W. Biederman 		goto out;
329028a6d671SEric W. Biederman 
3291b488893aSPavel Emelyanov 	ns = dentry->d_sb->s_fs_info;
329228a6d671SEric W. Biederman 	rcu_read_lock();
3293b488893aSPavel Emelyanov 	task = find_task_by_pid_ns(tid, ns);
329428a6d671SEric W. Biederman 	if (task)
329528a6d671SEric W. Biederman 		get_task_struct(task);
329628a6d671SEric W. Biederman 	rcu_read_unlock();
329728a6d671SEric W. Biederman 	if (!task)
329828a6d671SEric W. Biederman 		goto out;
3299bac0abd6SPavel Emelyanov 	if (!same_thread_group(leader, task))
330028a6d671SEric W. Biederman 		goto out_drop_task;
330128a6d671SEric W. Biederman 
3302444ceed8SEric W. Biederman 	result = proc_task_instantiate(dir, dentry, task, NULL);
330328a6d671SEric W. Biederman out_drop_task:
330428a6d671SEric W. Biederman 	put_task_struct(task);
330528a6d671SEric W. Biederman out:
330628a6d671SEric W. Biederman 	put_task_struct(leader);
330728a6d671SEric W. Biederman out_no_task:
330828a6d671SEric W. Biederman 	return result;
330928a6d671SEric W. Biederman }
331028a6d671SEric W. Biederman 
331128a6d671SEric W. Biederman /*
33120bc58a91SEric W. Biederman  * Find the first tid of a thread group to return to user space.
33130bc58a91SEric W. Biederman  *
33140bc58a91SEric W. Biederman  * Usually this is just the thread group leader, but if the users
33150bc58a91SEric W. Biederman  * buffer was too small or there was a seek into the middle of the
33160bc58a91SEric W. Biederman  * directory we have more work todo.
33170bc58a91SEric W. Biederman  *
33180bc58a91SEric W. Biederman  * In the case of a short read we start with find_task_by_pid.
33190bc58a91SEric W. Biederman  *
33200bc58a91SEric W. Biederman  * In the case of a seek we start with the leader and walk nr
33210bc58a91SEric W. Biederman  * threads past it.
33220bc58a91SEric W. Biederman  */
3323cc288738SEric W. Biederman static struct task_struct *first_tid(struct task_struct *leader,
3324b488893aSPavel Emelyanov 		int tid, int nr, struct pid_namespace *ns)
33250bc58a91SEric W. Biederman {
3326a872ff0cSOleg Nesterov 	struct task_struct *pos;
33270bc58a91SEric W. Biederman 
3328cc288738SEric W. Biederman 	rcu_read_lock();
33290bc58a91SEric W. Biederman 	/* Attempt to start with the pid of a thread */
33300bc58a91SEric W. Biederman 	if (tid && (nr > 0)) {
3331b488893aSPavel Emelyanov 		pos = find_task_by_pid_ns(tid, ns);
3332a872ff0cSOleg Nesterov 		if (pos && (pos->group_leader == leader))
3333a872ff0cSOleg Nesterov 			goto found;
33340bc58a91SEric W. Biederman 	}
33350bc58a91SEric W. Biederman 
33360bc58a91SEric W. Biederman 	/* If nr exceeds the number of threads there is nothing todo */
33370bc58a91SEric W. Biederman 	pos = NULL;
3338a872ff0cSOleg Nesterov 	if (nr && nr >= get_nr_threads(leader))
3339a872ff0cSOleg Nesterov 		goto out;
3340a872ff0cSOleg Nesterov 
3341a872ff0cSOleg Nesterov 	/* If we haven't found our starting place yet start
3342a872ff0cSOleg Nesterov 	 * with the leader and walk nr threads forward.
3343a872ff0cSOleg Nesterov 	 */
3344a872ff0cSOleg Nesterov 	for (pos = leader; nr > 0; --nr) {
3345a872ff0cSOleg Nesterov 		pos = next_thread(pos);
3346a872ff0cSOleg Nesterov 		if (pos == leader) {
3347a872ff0cSOleg Nesterov 			pos = NULL;
3348a872ff0cSOleg Nesterov 			goto out;
3349a872ff0cSOleg Nesterov 		}
3350a872ff0cSOleg Nesterov 	}
3351a872ff0cSOleg Nesterov found:
3352a872ff0cSOleg Nesterov 	get_task_struct(pos);
3353a872ff0cSOleg Nesterov out:
3354cc288738SEric W. Biederman 	rcu_read_unlock();
33550bc58a91SEric W. Biederman 	return pos;
33560bc58a91SEric W. Biederman }
33570bc58a91SEric W. Biederman 
33580bc58a91SEric W. Biederman /*
33590bc58a91SEric W. Biederman  * Find the next thread in the thread list.
33600bc58a91SEric W. Biederman  * Return NULL if there is an error or no next thread.
33610bc58a91SEric W. Biederman  *
33620bc58a91SEric W. Biederman  * The reference to the input task_struct is released.
33630bc58a91SEric W. Biederman  */
33640bc58a91SEric W. Biederman static struct task_struct *next_tid(struct task_struct *start)
33650bc58a91SEric W. Biederman {
3366c1df7fb8SOleg Nesterov 	struct task_struct *pos = NULL;
3367cc288738SEric W. Biederman 	rcu_read_lock();
3368c1df7fb8SOleg Nesterov 	if (pid_alive(start)) {
33690bc58a91SEric W. Biederman 		pos = next_thread(start);
3370c1df7fb8SOleg Nesterov 		if (thread_group_leader(pos))
33710bc58a91SEric W. Biederman 			pos = NULL;
3372c1df7fb8SOleg Nesterov 		else
3373c1df7fb8SOleg Nesterov 			get_task_struct(pos);
3374c1df7fb8SOleg Nesterov 	}
3375cc288738SEric W. Biederman 	rcu_read_unlock();
33760bc58a91SEric W. Biederman 	put_task_struct(start);
33770bc58a91SEric W. Biederman 	return pos;
33780bc58a91SEric W. Biederman }
33790bc58a91SEric W. Biederman 
338061a28784SEric W. Biederman static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
338161a28784SEric W. Biederman 	struct task_struct *task, int tid)
338261a28784SEric W. Biederman {
338361a28784SEric W. Biederman 	char name[PROC_NUMBUF];
338461a28784SEric W. Biederman 	int len = snprintf(name, sizeof(name), "%d", tid);
338561a28784SEric W. Biederman 	return proc_fill_cache(filp, dirent, filldir, name, len,
338661a28784SEric W. Biederman 				proc_task_instantiate, task, NULL);
338761a28784SEric W. Biederman }
338861a28784SEric W. Biederman 
33891da177e4SLinus Torvalds /* for the /proc/TGID/task/ directories */
33901da177e4SLinus Torvalds static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
33911da177e4SLinus Torvalds {
33922fddfeefSJosef "Jeff" Sipek 	struct dentry *dentry = filp->f_path.dentry;
33931da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
33947d895244SGuillaume Chazarain 	struct task_struct *leader = NULL;
33950bc58a91SEric W. Biederman 	struct task_struct *task;
33961da177e4SLinus Torvalds 	int retval = -ENOENT;
33971da177e4SLinus Torvalds 	ino_t ino;
33980bc58a91SEric W. Biederman 	int tid;
3399b488893aSPavel Emelyanov 	struct pid_namespace *ns;
34001da177e4SLinus Torvalds 
34017d895244SGuillaume Chazarain 	task = get_proc_task(inode);
34027d895244SGuillaume Chazarain 	if (!task)
34037d895244SGuillaume Chazarain 		goto out_no_task;
34047d895244SGuillaume Chazarain 	rcu_read_lock();
34057d895244SGuillaume Chazarain 	if (pid_alive(task)) {
34067d895244SGuillaume Chazarain 		leader = task->group_leader;
34077d895244SGuillaume Chazarain 		get_task_struct(leader);
34087d895244SGuillaume Chazarain 	}
34097d895244SGuillaume Chazarain 	rcu_read_unlock();
34107d895244SGuillaume Chazarain 	put_task_struct(task);
341199f89551SEric W. Biederman 	if (!leader)
341299f89551SEric W. Biederman 		goto out_no_task;
34131da177e4SLinus Torvalds 	retval = 0;
34141da177e4SLinus Torvalds 
3415ee568b25SLinus Torvalds 	switch ((unsigned long)filp->f_pos) {
34161da177e4SLinus Torvalds 	case 0:
34171da177e4SLinus Torvalds 		ino = inode->i_ino;
3418ee6f779bSZhang Le 		if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) < 0)
34191da177e4SLinus Torvalds 			goto out;
3420ee6f779bSZhang Le 		filp->f_pos++;
34211da177e4SLinus Torvalds 		/* fall through */
34221da177e4SLinus Torvalds 	case 1:
34231da177e4SLinus Torvalds 		ino = parent_ino(dentry);
3424ee6f779bSZhang Le 		if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) < 0)
34251da177e4SLinus Torvalds 			goto out;
3426ee6f779bSZhang Le 		filp->f_pos++;
34271da177e4SLinus Torvalds 		/* fall through */
34281da177e4SLinus Torvalds 	}
34291da177e4SLinus Torvalds 
34300bc58a91SEric W. Biederman 	/* f_version caches the tgid value that the last readdir call couldn't
34310bc58a91SEric W. Biederman 	 * return. lseek aka telldir automagically resets f_version to 0.
34320bc58a91SEric W. Biederman 	 */
3433b488893aSPavel Emelyanov 	ns = filp->f_dentry->d_sb->s_fs_info;
34342b47c361SMathieu Desnoyers 	tid = (int)filp->f_version;
34350bc58a91SEric W. Biederman 	filp->f_version = 0;
3436ee6f779bSZhang Le 	for (task = first_tid(leader, tid, filp->f_pos - 2, ns);
34370bc58a91SEric W. Biederman 	     task;
3438ee6f779bSZhang Le 	     task = next_tid(task), filp->f_pos++) {
3439b488893aSPavel Emelyanov 		tid = task_pid_nr_ns(task, ns);
344061a28784SEric W. Biederman 		if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
34410bc58a91SEric W. Biederman 			/* returning this tgid failed, save it as the first
34420bc58a91SEric W. Biederman 			 * pid for the next readir call */
34432b47c361SMathieu Desnoyers 			filp->f_version = (u64)tid;
34440bc58a91SEric W. Biederman 			put_task_struct(task);
34451da177e4SLinus Torvalds 			break;
34460bc58a91SEric W. Biederman 		}
34471da177e4SLinus Torvalds 	}
34481da177e4SLinus Torvalds out:
344999f89551SEric W. Biederman 	put_task_struct(leader);
345099f89551SEric W. Biederman out_no_task:
34511da177e4SLinus Torvalds 	return retval;
34521da177e4SLinus Torvalds }
34536e66b52bSEric W. Biederman 
34546e66b52bSEric W. Biederman static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
34556e66b52bSEric W. Biederman {
34566e66b52bSEric W. Biederman 	struct inode *inode = dentry->d_inode;
345799f89551SEric W. Biederman 	struct task_struct *p = get_proc_task(inode);
34586e66b52bSEric W. Biederman 	generic_fillattr(inode, stat);
34596e66b52bSEric W. Biederman 
346099f89551SEric W. Biederman 	if (p) {
346199f89551SEric W. Biederman 		stat->nlink += get_nr_threads(p);
346299f89551SEric W. Biederman 		put_task_struct(p);
34636e66b52bSEric W. Biederman 	}
34646e66b52bSEric W. Biederman 
34656e66b52bSEric W. Biederman 	return 0;
34666e66b52bSEric W. Biederman }
346728a6d671SEric W. Biederman 
3468c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations = {
346928a6d671SEric W. Biederman 	.lookup		= proc_task_lookup,
347028a6d671SEric W. Biederman 	.getattr	= proc_task_getattr,
347128a6d671SEric W. Biederman 	.setattr	= proc_setattr,
347228a6d671SEric W. Biederman };
347328a6d671SEric W. Biederman 
347400977a59SArjan van de Ven static const struct file_operations proc_task_operations = {
347528a6d671SEric W. Biederman 	.read		= generic_read_dir,
347628a6d671SEric W. Biederman 	.readdir	= proc_task_readdir,
34776038f373SArnd Bergmann 	.llseek		= default_llseek,
347828a6d671SEric W. Biederman };
3479