xref: /openbmc/linux/fs/proc/base.c (revision e1760bd5ffae8cb98cffb030ee8e631eba28f3d8)
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>
8422d917d8SEric W. Biederman #include <linux/user_namespace.h>
855ad4e53bSAl Viro #include <linux/fs_struct.h>
865a0e3ad6STejun Heo #include <linux/slab.h>
87640708a2SPavel Emelyanov #include <linux/flex_array.h>
88f133eccaSChris Metcalf #ifdef CONFIG_HARDWALL
89f133eccaSChris Metcalf #include <asm/hardwall.h>
90f133eccaSChris Metcalf #endif
9143d2b113SKAMEZAWA Hiroyuki #include <trace/events/oom.h>
921da177e4SLinus Torvalds #include "internal.h"
931da177e4SLinus Torvalds 
940f2fe20fSEric W. Biederman /* NOTE:
950f2fe20fSEric W. Biederman  *	Implementing inode permission operations in /proc is almost
960f2fe20fSEric W. Biederman  *	certainly an error.  Permission checks need to happen during
970f2fe20fSEric W. Biederman  *	each system call not at open time.  The reason is that most of
980f2fe20fSEric W. Biederman  *	what we wish to check for permissions in /proc varies at runtime.
990f2fe20fSEric W. Biederman  *
1000f2fe20fSEric W. Biederman  *	The classic example of a problem is opening file descriptors
1010f2fe20fSEric W. Biederman  *	in /proc for a task before it execs a suid executable.
1020f2fe20fSEric W. Biederman  */
1030f2fe20fSEric W. Biederman 
1041da177e4SLinus Torvalds struct pid_entry {
1051da177e4SLinus Torvalds 	char *name;
106c5141e6dSEric Dumazet 	int len;
107d161a13fSAl Viro 	umode_t mode;
108c5ef1c42SArjan van de Ven 	const struct inode_operations *iop;
10900977a59SArjan van de Ven 	const struct file_operations *fop;
11020cdc894SEric W. Biederman 	union proc_op op;
1111da177e4SLinus Torvalds };
1121da177e4SLinus Torvalds 
11361a28784SEric W. Biederman #define NOD(NAME, MODE, IOP, FOP, OP) {			\
11420cdc894SEric W. Biederman 	.name = (NAME),					\
115c5141e6dSEric Dumazet 	.len  = sizeof(NAME) - 1,			\
11620cdc894SEric W. Biederman 	.mode = MODE,					\
11720cdc894SEric W. Biederman 	.iop  = IOP,					\
11820cdc894SEric W. Biederman 	.fop  = FOP,					\
11920cdc894SEric W. Biederman 	.op   = OP,					\
12020cdc894SEric W. Biederman }
12120cdc894SEric W. Biederman 
122631f9c18SAlexey Dobriyan #define DIR(NAME, MODE, iops, fops)	\
123631f9c18SAlexey Dobriyan 	NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
124631f9c18SAlexey Dobriyan #define LNK(NAME, get_link)					\
12561a28784SEric W. Biederman 	NOD(NAME, (S_IFLNK|S_IRWXUGO),				\
12620cdc894SEric W. Biederman 		&proc_pid_link_inode_operations, NULL,		\
127631f9c18SAlexey Dobriyan 		{ .proc_get_link = get_link } )
128631f9c18SAlexey Dobriyan #define REG(NAME, MODE, fops)				\
129631f9c18SAlexey Dobriyan 	NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
130631f9c18SAlexey Dobriyan #define INF(NAME, MODE, read)				\
13161a28784SEric W. Biederman 	NOD(NAME, (S_IFREG|(MODE)), 			\
13220cdc894SEric W. Biederman 		NULL, &proc_info_file_operations,	\
133631f9c18SAlexey Dobriyan 		{ .proc_read = read } )
134631f9c18SAlexey Dobriyan #define ONE(NAME, MODE, show)				\
135be614086SEric W. Biederman 	NOD(NAME, (S_IFREG|(MODE)), 			\
136be614086SEric W. Biederman 		NULL, &proc_single_file_operations,	\
137631f9c18SAlexey Dobriyan 		{ .proc_show = show } )
1381da177e4SLinus Torvalds 
139640708a2SPavel Emelyanov static int proc_fd_permission(struct inode *inode, int mask);
140640708a2SPavel Emelyanov 
141aed54175SVegard Nossum /*
142aed54175SVegard Nossum  * Count the number of hardlinks for the pid_entry table, excluding the .
143aed54175SVegard Nossum  * and .. links.
144aed54175SVegard Nossum  */
145aed54175SVegard Nossum static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
146aed54175SVegard Nossum 	unsigned int n)
147aed54175SVegard Nossum {
148aed54175SVegard Nossum 	unsigned int i;
149aed54175SVegard Nossum 	unsigned int count;
150aed54175SVegard Nossum 
151aed54175SVegard Nossum 	count = 0;
152aed54175SVegard Nossum 	for (i = 0; i < n; ++i) {
153aed54175SVegard Nossum 		if (S_ISDIR(entries[i].mode))
154aed54175SVegard Nossum 			++count;
155aed54175SVegard Nossum 	}
156aed54175SVegard Nossum 
157aed54175SVegard Nossum 	return count;
158aed54175SVegard Nossum }
159aed54175SVegard Nossum 
160f7ad3c6bSMiklos Szeredi static int get_task_root(struct task_struct *task, struct path *root)
1611da177e4SLinus Torvalds {
1627c2c7d99SHugh Dickins 	int result = -ENOENT;
1637c2c7d99SHugh Dickins 
1640494f6ecSMiklos Szeredi 	task_lock(task);
165f7ad3c6bSMiklos Szeredi 	if (task->fs) {
166f7ad3c6bSMiklos Szeredi 		get_fs_root(task->fs, root);
1677c2c7d99SHugh Dickins 		result = 0;
1687c2c7d99SHugh Dickins 	}
1690494f6ecSMiklos Szeredi 	task_unlock(task);
1707c2c7d99SHugh Dickins 	return result;
1710494f6ecSMiklos Szeredi }
1720494f6ecSMiklos Szeredi 
1737773fbc5SCyrill Gorcunov static int proc_cwd_link(struct dentry *dentry, struct path *path)
1740494f6ecSMiklos Szeredi {
1757773fbc5SCyrill Gorcunov 	struct task_struct *task = get_proc_task(dentry->d_inode);
1760494f6ecSMiklos Szeredi 	int result = -ENOENT;
17799f89551SEric W. Biederman 
17899f89551SEric W. Biederman 	if (task) {
179f7ad3c6bSMiklos Szeredi 		task_lock(task);
180f7ad3c6bSMiklos Szeredi 		if (task->fs) {
181f7ad3c6bSMiklos Szeredi 			get_fs_pwd(task->fs, path);
182f7ad3c6bSMiklos Szeredi 			result = 0;
183f7ad3c6bSMiklos Szeredi 		}
184f7ad3c6bSMiklos Szeredi 		task_unlock(task);
18599f89551SEric W. Biederman 		put_task_struct(task);
18699f89551SEric W. Biederman 	}
1871da177e4SLinus Torvalds 	return result;
1881da177e4SLinus Torvalds }
1891da177e4SLinus Torvalds 
1907773fbc5SCyrill Gorcunov static int proc_root_link(struct dentry *dentry, struct path *path)
1911da177e4SLinus Torvalds {
1927773fbc5SCyrill Gorcunov 	struct task_struct *task = get_proc_task(dentry->d_inode);
1931da177e4SLinus Torvalds 	int result = -ENOENT;
19499f89551SEric W. Biederman 
19599f89551SEric W. Biederman 	if (task) {
196f7ad3c6bSMiklos Szeredi 		result = get_task_root(task, path);
19799f89551SEric W. Biederman 		put_task_struct(task);
19899f89551SEric W. Biederman 	}
1991da177e4SLinus Torvalds 	return result;
2001da177e4SLinus Torvalds }
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds static int proc_pid_cmdline(struct task_struct *task, char * buffer)
2031da177e4SLinus Torvalds {
2041da177e4SLinus Torvalds 	int res = 0;
2051da177e4SLinus Torvalds 	unsigned int len;
2061da177e4SLinus Torvalds 	struct mm_struct *mm = get_task_mm(task);
2071da177e4SLinus Torvalds 	if (!mm)
2081da177e4SLinus Torvalds 		goto out;
2091da177e4SLinus Torvalds 	if (!mm->arg_end)
2101da177e4SLinus Torvalds 		goto out_mm;	/* Shh! No looking before we're done */
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds  	len = mm->arg_end - mm->arg_start;
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds 	if (len > PAGE_SIZE)
2151da177e4SLinus Torvalds 		len = PAGE_SIZE;
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds 	res = access_process_vm(task, mm->arg_start, buffer, len, 0);
2181da177e4SLinus Torvalds 
2191da177e4SLinus Torvalds 	// If the nul at the end of args has been overwritten, then
2201da177e4SLinus Torvalds 	// assume application is using setproctitle(3).
2211da177e4SLinus Torvalds 	if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
2221da177e4SLinus Torvalds 		len = strnlen(buffer, res);
2231da177e4SLinus Torvalds 		if (len < res) {
2241da177e4SLinus Torvalds 		    res = len;
2251da177e4SLinus Torvalds 		} else {
2261da177e4SLinus Torvalds 			len = mm->env_end - mm->env_start;
2271da177e4SLinus Torvalds 			if (len > PAGE_SIZE - res)
2281da177e4SLinus Torvalds 				len = PAGE_SIZE - res;
2291da177e4SLinus Torvalds 			res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
2301da177e4SLinus Torvalds 			res = strnlen(buffer, res);
2311da177e4SLinus Torvalds 		}
2321da177e4SLinus Torvalds 	}
2331da177e4SLinus Torvalds out_mm:
2341da177e4SLinus Torvalds 	mmput(mm);
2351da177e4SLinus Torvalds out:
2361da177e4SLinus Torvalds 	return res;
2371da177e4SLinus Torvalds }
2381da177e4SLinus Torvalds 
2391da177e4SLinus Torvalds static int proc_pid_auxv(struct task_struct *task, char *buffer)
2401da177e4SLinus Torvalds {
241e7dcd999SCong Wang 	struct mm_struct *mm = mm_access(task, PTRACE_MODE_READ);
2422fadaef4SAl Viro 	int res = PTR_ERR(mm);
2432fadaef4SAl Viro 	if (mm && !IS_ERR(mm)) {
2441da177e4SLinus Torvalds 		unsigned int nwords = 0;
245dfe6b7d9SHannes Eder 		do {
2461da177e4SLinus Torvalds 			nwords += 2;
247dfe6b7d9SHannes Eder 		} while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
2481da177e4SLinus Torvalds 		res = nwords * sizeof(mm->saved_auxv[0]);
2491da177e4SLinus Torvalds 		if (res > PAGE_SIZE)
2501da177e4SLinus Torvalds 			res = PAGE_SIZE;
2511da177e4SLinus Torvalds 		memcpy(buffer, mm->saved_auxv, res);
2521da177e4SLinus Torvalds 		mmput(mm);
2531da177e4SLinus Torvalds 	}
2541da177e4SLinus Torvalds 	return res;
2551da177e4SLinus Torvalds }
2561da177e4SLinus Torvalds 
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds #ifdef CONFIG_KALLSYMS
2591da177e4SLinus Torvalds /*
2601da177e4SLinus Torvalds  * Provides a wchan file via kallsyms in a proper one-value-per-file format.
2611da177e4SLinus Torvalds  * Returns the resolved symbol.  If that fails, simply return the address.
2621da177e4SLinus Torvalds  */
2631da177e4SLinus Torvalds static int proc_pid_wchan(struct task_struct *task, char *buffer)
2641da177e4SLinus Torvalds {
265ffb45122SAlexey Dobriyan 	unsigned long wchan;
2669281aceaSTejun Heo 	char symname[KSYM_NAME_LEN];
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds 	wchan = get_wchan(task);
2691da177e4SLinus Torvalds 
2709d65cb4aSAlexey Dobriyan 	if (lookup_symbol_name(wchan, symname) < 0)
271f83ce3e6SJake Edge 		if (!ptrace_may_access(task, PTRACE_MODE_READ))
272f83ce3e6SJake Edge 			return 0;
273f83ce3e6SJake Edge 		else
2741da177e4SLinus Torvalds 			return sprintf(buffer, "%lu", wchan);
2759d65cb4aSAlexey Dobriyan 	else
2769d65cb4aSAlexey Dobriyan 		return sprintf(buffer, "%s", symname);
2771da177e4SLinus Torvalds }
2781da177e4SLinus Torvalds #endif /* CONFIG_KALLSYMS */
2791da177e4SLinus Torvalds 
280a9712bc1SAl Viro static int lock_trace(struct task_struct *task)
281a9712bc1SAl Viro {
282a9712bc1SAl Viro 	int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
283a9712bc1SAl Viro 	if (err)
284a9712bc1SAl Viro 		return err;
285a9712bc1SAl Viro 	if (!ptrace_may_access(task, PTRACE_MODE_ATTACH)) {
286a9712bc1SAl Viro 		mutex_unlock(&task->signal->cred_guard_mutex);
287a9712bc1SAl Viro 		return -EPERM;
288a9712bc1SAl Viro 	}
289a9712bc1SAl Viro 	return 0;
290a9712bc1SAl Viro }
291a9712bc1SAl Viro 
292a9712bc1SAl Viro static void unlock_trace(struct task_struct *task)
293a9712bc1SAl Viro {
294a9712bc1SAl Viro 	mutex_unlock(&task->signal->cred_guard_mutex);
295a9712bc1SAl Viro }
296a9712bc1SAl Viro 
2972ec220e2SKen Chen #ifdef CONFIG_STACKTRACE
2982ec220e2SKen Chen 
2992ec220e2SKen Chen #define MAX_STACK_TRACE_DEPTH	64
3002ec220e2SKen Chen 
3012ec220e2SKen Chen static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
3022ec220e2SKen Chen 			  struct pid *pid, struct task_struct *task)
3032ec220e2SKen Chen {
3042ec220e2SKen Chen 	struct stack_trace trace;
3052ec220e2SKen Chen 	unsigned long *entries;
306a9712bc1SAl Viro 	int err;
3072ec220e2SKen Chen 	int i;
3082ec220e2SKen Chen 
3092ec220e2SKen Chen 	entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
3102ec220e2SKen Chen 	if (!entries)
3112ec220e2SKen Chen 		return -ENOMEM;
3122ec220e2SKen Chen 
3132ec220e2SKen Chen 	trace.nr_entries	= 0;
3142ec220e2SKen Chen 	trace.max_entries	= MAX_STACK_TRACE_DEPTH;
3152ec220e2SKen Chen 	trace.entries		= entries;
3162ec220e2SKen Chen 	trace.skip		= 0;
317a9712bc1SAl Viro 
318a9712bc1SAl Viro 	err = lock_trace(task);
319a9712bc1SAl Viro 	if (!err) {
3202ec220e2SKen Chen 		save_stack_trace_tsk(task, &trace);
3212ec220e2SKen Chen 
3222ec220e2SKen Chen 		for (i = 0; i < trace.nr_entries; i++) {
32351e03149SKonstantin Khlebnikov 			seq_printf(m, "[<%pK>] %pS\n",
3242ec220e2SKen Chen 				   (void *)entries[i], (void *)entries[i]);
3252ec220e2SKen Chen 		}
326a9712bc1SAl Viro 		unlock_trace(task);
327a9712bc1SAl Viro 	}
3282ec220e2SKen Chen 	kfree(entries);
3292ec220e2SKen Chen 
330a9712bc1SAl Viro 	return err;
3312ec220e2SKen Chen }
3322ec220e2SKen Chen #endif
3332ec220e2SKen Chen 
3341da177e4SLinus Torvalds #ifdef CONFIG_SCHEDSTATS
3351da177e4SLinus Torvalds /*
3361da177e4SLinus Torvalds  * Provides /proc/PID/schedstat
3371da177e4SLinus Torvalds  */
3381da177e4SLinus Torvalds static int proc_pid_schedstat(struct task_struct *task, char *buffer)
3391da177e4SLinus Torvalds {
340172ba844SBalbir Singh 	return sprintf(buffer, "%llu %llu %lu\n",
341826e08b0SIngo Molnar 			(unsigned long long)task->se.sum_exec_runtime,
342826e08b0SIngo Molnar 			(unsigned long long)task->sched_info.run_delay,
3432d72376bSIngo Molnar 			task->sched_info.pcount);
3441da177e4SLinus Torvalds }
3451da177e4SLinus Torvalds #endif
3461da177e4SLinus Torvalds 
3479745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP
3489745512cSArjan van de Ven static int lstats_show_proc(struct seq_file *m, void *v)
3499745512cSArjan van de Ven {
3509745512cSArjan van de Ven 	int i;
35113d77c37SHiroshi Shimamoto 	struct inode *inode = m->private;
35213d77c37SHiroshi Shimamoto 	struct task_struct *task = get_proc_task(inode);
3539745512cSArjan van de Ven 
35413d77c37SHiroshi Shimamoto 	if (!task)
35513d77c37SHiroshi Shimamoto 		return -ESRCH;
35613d77c37SHiroshi Shimamoto 	seq_puts(m, "Latency Top version : v0.1\n");
3579745512cSArjan van de Ven 	for (i = 0; i < 32; i++) {
35834e49d4fSJoe Perches 		struct latency_record *lr = &task->latency_record[i];
35934e49d4fSJoe Perches 		if (lr->backtrace[0]) {
3609745512cSArjan van de Ven 			int q;
3619745512cSArjan van de Ven 			seq_printf(m, "%i %li %li",
36234e49d4fSJoe Perches 				   lr->count, lr->time, lr->max);
3639745512cSArjan van de Ven 			for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
36434e49d4fSJoe Perches 				unsigned long bt = lr->backtrace[q];
36534e49d4fSJoe Perches 				if (!bt)
3669745512cSArjan van de Ven 					break;
36734e49d4fSJoe Perches 				if (bt == ULONG_MAX)
3689745512cSArjan van de Ven 					break;
36934e49d4fSJoe Perches 				seq_printf(m, " %ps", (void *)bt);
3709745512cSArjan van de Ven 			}
3719d6de12fSAlexey Dobriyan 			seq_putc(m, '\n');
3729745512cSArjan van de Ven 		}
3739745512cSArjan van de Ven 
3749745512cSArjan van de Ven 	}
37513d77c37SHiroshi Shimamoto 	put_task_struct(task);
3769745512cSArjan van de Ven 	return 0;
3779745512cSArjan van de Ven }
3789745512cSArjan van de Ven 
3799745512cSArjan van de Ven static int lstats_open(struct inode *inode, struct file *file)
3809745512cSArjan van de Ven {
38113d77c37SHiroshi Shimamoto 	return single_open(file, lstats_show_proc, inode);
382d6643d12SHiroshi Shimamoto }
383d6643d12SHiroshi Shimamoto 
3849745512cSArjan van de Ven static ssize_t lstats_write(struct file *file, const char __user *buf,
3859745512cSArjan van de Ven 			    size_t count, loff_t *offs)
3869745512cSArjan van de Ven {
38713d77c37SHiroshi Shimamoto 	struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
3889745512cSArjan van de Ven 
38913d77c37SHiroshi Shimamoto 	if (!task)
39013d77c37SHiroshi Shimamoto 		return -ESRCH;
3919745512cSArjan van de Ven 	clear_all_latency_tracing(task);
39213d77c37SHiroshi Shimamoto 	put_task_struct(task);
3939745512cSArjan van de Ven 
3949745512cSArjan van de Ven 	return count;
3959745512cSArjan van de Ven }
3969745512cSArjan van de Ven 
3979745512cSArjan van de Ven static const struct file_operations proc_lstats_operations = {
3989745512cSArjan van de Ven 	.open		= lstats_open,
3999745512cSArjan van de Ven 	.read		= seq_read,
4009745512cSArjan van de Ven 	.write		= lstats_write,
4019745512cSArjan van de Ven 	.llseek		= seq_lseek,
40213d77c37SHiroshi Shimamoto 	.release	= single_release,
4039745512cSArjan van de Ven };
4049745512cSArjan van de Ven 
4059745512cSArjan van de Ven #endif
4069745512cSArjan van de Ven 
4071da177e4SLinus Torvalds static int proc_oom_score(struct task_struct *task, char *buffer)
4081da177e4SLinus Torvalds {
409a7f638f9SDavid Rientjes 	unsigned long totalpages = totalram_pages + total_swap_pages;
410b95c35e7SOleg Nesterov 	unsigned long points = 0;
4111da177e4SLinus Torvalds 
41219c5d45aSAlexey Dobriyan 	read_lock(&tasklist_lock);
413b95c35e7SOleg Nesterov 	if (pid_alive(task))
414a7f638f9SDavid Rientjes 		points = oom_badness(task, NULL, NULL, totalpages) *
415a7f638f9SDavid Rientjes 						1000 / totalpages;
41619c5d45aSAlexey Dobriyan 	read_unlock(&tasklist_lock);
4171da177e4SLinus Torvalds 	return sprintf(buffer, "%lu\n", points);
4181da177e4SLinus Torvalds }
4191da177e4SLinus Torvalds 
420d85f50d5SNeil Horman struct limit_names {
421d85f50d5SNeil Horman 	char *name;
422d85f50d5SNeil Horman 	char *unit;
423d85f50d5SNeil Horman };
424d85f50d5SNeil Horman 
425d85f50d5SNeil Horman static const struct limit_names lnames[RLIM_NLIMITS] = {
426cff4edb5SKees Cook 	[RLIMIT_CPU] = {"Max cpu time", "seconds"},
427d85f50d5SNeil Horman 	[RLIMIT_FSIZE] = {"Max file size", "bytes"},
428d85f50d5SNeil Horman 	[RLIMIT_DATA] = {"Max data size", "bytes"},
429d85f50d5SNeil Horman 	[RLIMIT_STACK] = {"Max stack size", "bytes"},
430d85f50d5SNeil Horman 	[RLIMIT_CORE] = {"Max core file size", "bytes"},
431d85f50d5SNeil Horman 	[RLIMIT_RSS] = {"Max resident set", "bytes"},
432d85f50d5SNeil Horman 	[RLIMIT_NPROC] = {"Max processes", "processes"},
433d85f50d5SNeil Horman 	[RLIMIT_NOFILE] = {"Max open files", "files"},
434d85f50d5SNeil Horman 	[RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
435d85f50d5SNeil Horman 	[RLIMIT_AS] = {"Max address space", "bytes"},
436d85f50d5SNeil Horman 	[RLIMIT_LOCKS] = {"Max file locks", "locks"},
437d85f50d5SNeil Horman 	[RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
438d85f50d5SNeil Horman 	[RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
439d85f50d5SNeil Horman 	[RLIMIT_NICE] = {"Max nice priority", NULL},
440d85f50d5SNeil Horman 	[RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
4418808117cSEugene Teo 	[RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
442d85f50d5SNeil Horman };
443d85f50d5SNeil Horman 
444d85f50d5SNeil Horman /* Display limits for a process */
445d85f50d5SNeil Horman static int proc_pid_limits(struct task_struct *task, char *buffer)
446d85f50d5SNeil Horman {
447d85f50d5SNeil Horman 	unsigned int i;
448d85f50d5SNeil Horman 	int count = 0;
449d85f50d5SNeil Horman 	unsigned long flags;
450d85f50d5SNeil Horman 	char *bufptr = buffer;
451d85f50d5SNeil Horman 
452d85f50d5SNeil Horman 	struct rlimit rlim[RLIM_NLIMITS];
453d85f50d5SNeil Horman 
454a6bebbc8SLai Jiangshan 	if (!lock_task_sighand(task, &flags))
455d85f50d5SNeil Horman 		return 0;
456d85f50d5SNeil Horman 	memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
457d85f50d5SNeil Horman 	unlock_task_sighand(task, &flags);
458d85f50d5SNeil Horman 
459d85f50d5SNeil Horman 	/*
460d85f50d5SNeil Horman 	 * print the file header
461d85f50d5SNeil Horman 	 */
462d85f50d5SNeil Horman 	count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
463d85f50d5SNeil Horman 			"Limit", "Soft Limit", "Hard Limit", "Units");
464d85f50d5SNeil Horman 
465d85f50d5SNeil Horman 	for (i = 0; i < RLIM_NLIMITS; i++) {
466d85f50d5SNeil Horman 		if (rlim[i].rlim_cur == RLIM_INFINITY)
467d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-25s %-20s ",
468d85f50d5SNeil Horman 					 lnames[i].name, "unlimited");
469d85f50d5SNeil Horman 		else
470d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-25s %-20lu ",
471d85f50d5SNeil Horman 					 lnames[i].name, rlim[i].rlim_cur);
472d85f50d5SNeil Horman 
473d85f50d5SNeil Horman 		if (rlim[i].rlim_max == RLIM_INFINITY)
474d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-20s ", "unlimited");
475d85f50d5SNeil Horman 		else
476d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-20lu ",
477d85f50d5SNeil Horman 					 rlim[i].rlim_max);
478d85f50d5SNeil Horman 
479d85f50d5SNeil Horman 		if (lnames[i].unit)
480d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-10s\n",
481d85f50d5SNeil Horman 					 lnames[i].unit);
482d85f50d5SNeil Horman 		else
483d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "\n");
484d85f50d5SNeil Horman 	}
485d85f50d5SNeil Horman 
486d85f50d5SNeil Horman 	return count;
487d85f50d5SNeil Horman }
488d85f50d5SNeil Horman 
489ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
490ebcb6734SRoland McGrath static int proc_pid_syscall(struct task_struct *task, char *buffer)
491ebcb6734SRoland McGrath {
492ebcb6734SRoland McGrath 	long nr;
493ebcb6734SRoland McGrath 	unsigned long args[6], sp, pc;
494a9712bc1SAl Viro 	int res = lock_trace(task);
495a9712bc1SAl Viro 	if (res)
496a9712bc1SAl Viro 		return res;
497ebcb6734SRoland McGrath 
498ebcb6734SRoland McGrath 	if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
499a9712bc1SAl Viro 		res = sprintf(buffer, "running\n");
500a9712bc1SAl Viro 	else if (nr < 0)
501a9712bc1SAl Viro 		res = sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
502a9712bc1SAl Viro 	else
503a9712bc1SAl Viro 		res = sprintf(buffer,
504ebcb6734SRoland McGrath 		       "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
505ebcb6734SRoland McGrath 		       nr,
506ebcb6734SRoland McGrath 		       args[0], args[1], args[2], args[3], args[4], args[5],
507ebcb6734SRoland McGrath 		       sp, pc);
508a9712bc1SAl Viro 	unlock_trace(task);
509a9712bc1SAl Viro 	return res;
510ebcb6734SRoland McGrath }
511ebcb6734SRoland McGrath #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
512ebcb6734SRoland McGrath 
5131da177e4SLinus Torvalds /************************************************************************/
5141da177e4SLinus Torvalds /*                       Here the fs part begins                        */
5151da177e4SLinus Torvalds /************************************************************************/
5161da177e4SLinus Torvalds 
5171da177e4SLinus Torvalds /* permission checks */
518778c1144SEric W. Biederman static int proc_fd_access_allowed(struct inode *inode)
5191da177e4SLinus Torvalds {
520778c1144SEric W. Biederman 	struct task_struct *task;
521778c1144SEric W. Biederman 	int allowed = 0;
522df26c40eSEric W. Biederman 	/* Allow access to a task's file descriptors if it is us or we
523df26c40eSEric W. Biederman 	 * may use ptrace attach to the process and find out that
524df26c40eSEric W. Biederman 	 * information.
525778c1144SEric W. Biederman 	 */
526778c1144SEric W. Biederman 	task = get_proc_task(inode);
527df26c40eSEric W. Biederman 	if (task) {
528006ebb40SStephen Smalley 		allowed = ptrace_may_access(task, PTRACE_MODE_READ);
529778c1144SEric W. Biederman 		put_task_struct(task);
530df26c40eSEric W. Biederman 	}
531778c1144SEric W. Biederman 	return allowed;
5321da177e4SLinus Torvalds }
5331da177e4SLinus Torvalds 
5346b4e306aSEric W. Biederman int proc_setattr(struct dentry *dentry, struct iattr *attr)
5356d76fa58SLinus Torvalds {
5366d76fa58SLinus Torvalds 	int error;
5376d76fa58SLinus Torvalds 	struct inode *inode = dentry->d_inode;
5386d76fa58SLinus Torvalds 
5396d76fa58SLinus Torvalds 	if (attr->ia_valid & ATTR_MODE)
5406d76fa58SLinus Torvalds 		return -EPERM;
5416d76fa58SLinus Torvalds 
5426d76fa58SLinus Torvalds 	error = inode_change_ok(inode, attr);
5431025774cSChristoph Hellwig 	if (error)
5446d76fa58SLinus Torvalds 		return error;
5451025774cSChristoph Hellwig 
5461025774cSChristoph Hellwig 	if ((attr->ia_valid & ATTR_SIZE) &&
5471025774cSChristoph Hellwig 	    attr->ia_size != i_size_read(inode)) {
5481025774cSChristoph Hellwig 		error = vmtruncate(inode, attr->ia_size);
5491025774cSChristoph Hellwig 		if (error)
5501025774cSChristoph Hellwig 			return error;
5511025774cSChristoph Hellwig 	}
5521025774cSChristoph Hellwig 
5531025774cSChristoph Hellwig 	setattr_copy(inode, attr);
5541025774cSChristoph Hellwig 	mark_inode_dirty(inode);
5551025774cSChristoph Hellwig 	return 0;
5566d76fa58SLinus Torvalds }
5576d76fa58SLinus Torvalds 
5580499680aSVasiliy Kulikov /*
5590499680aSVasiliy Kulikov  * May current process learn task's sched/cmdline info (for hide_pid_min=1)
5600499680aSVasiliy Kulikov  * or euid/egid (for hide_pid_min=2)?
5610499680aSVasiliy Kulikov  */
5620499680aSVasiliy Kulikov static bool has_pid_permissions(struct pid_namespace *pid,
5630499680aSVasiliy Kulikov 				 struct task_struct *task,
5640499680aSVasiliy Kulikov 				 int hide_pid_min)
5650499680aSVasiliy Kulikov {
5660499680aSVasiliy Kulikov 	if (pid->hide_pid < hide_pid_min)
5670499680aSVasiliy Kulikov 		return true;
5680499680aSVasiliy Kulikov 	if (in_group_p(pid->pid_gid))
5690499680aSVasiliy Kulikov 		return true;
5700499680aSVasiliy Kulikov 	return ptrace_may_access(task, PTRACE_MODE_READ);
5710499680aSVasiliy Kulikov }
5720499680aSVasiliy Kulikov 
5730499680aSVasiliy Kulikov 
5740499680aSVasiliy Kulikov static int proc_pid_permission(struct inode *inode, int mask)
5750499680aSVasiliy Kulikov {
5760499680aSVasiliy Kulikov 	struct pid_namespace *pid = inode->i_sb->s_fs_info;
5770499680aSVasiliy Kulikov 	struct task_struct *task;
5780499680aSVasiliy Kulikov 	bool has_perms;
5790499680aSVasiliy Kulikov 
5800499680aSVasiliy Kulikov 	task = get_proc_task(inode);
581a2ef990aSXiaotian Feng 	if (!task)
582a2ef990aSXiaotian Feng 		return -ESRCH;
5830499680aSVasiliy Kulikov 	has_perms = has_pid_permissions(pid, task, 1);
5840499680aSVasiliy Kulikov 	put_task_struct(task);
5850499680aSVasiliy Kulikov 
5860499680aSVasiliy Kulikov 	if (!has_perms) {
5870499680aSVasiliy Kulikov 		if (pid->hide_pid == 2) {
5880499680aSVasiliy Kulikov 			/*
5890499680aSVasiliy Kulikov 			 * Let's make getdents(), stat(), and open()
5900499680aSVasiliy Kulikov 			 * consistent with each other.  If a process
5910499680aSVasiliy Kulikov 			 * may not stat() a file, it shouldn't be seen
5920499680aSVasiliy Kulikov 			 * in procfs at all.
5930499680aSVasiliy Kulikov 			 */
5940499680aSVasiliy Kulikov 			return -ENOENT;
5950499680aSVasiliy Kulikov 		}
5960499680aSVasiliy Kulikov 
5970499680aSVasiliy Kulikov 		return -EPERM;
5980499680aSVasiliy Kulikov 	}
5990499680aSVasiliy Kulikov 	return generic_permission(inode, mask);
6000499680aSVasiliy Kulikov }
6010499680aSVasiliy Kulikov 
6020499680aSVasiliy Kulikov 
6030499680aSVasiliy Kulikov 
604c5ef1c42SArjan van de Ven static const struct inode_operations proc_def_inode_operations = {
6056d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
6066d76fa58SLinus Torvalds };
6076d76fa58SLinus Torvalds 
6081da177e4SLinus Torvalds #define PROC_BLOCK_SIZE	(3*1024)		/* 4K page size but our output routines use some slack for overruns */
6091da177e4SLinus Torvalds 
6101da177e4SLinus Torvalds static ssize_t proc_info_read(struct file * file, char __user * buf,
6111da177e4SLinus Torvalds 			  size_t count, loff_t *ppos)
6121da177e4SLinus Torvalds {
6132fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
6141da177e4SLinus Torvalds 	unsigned long page;
6151da177e4SLinus Torvalds 	ssize_t length;
61699f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
61799f89551SEric W. Biederman 
61899f89551SEric W. Biederman 	length = -ESRCH;
61999f89551SEric W. Biederman 	if (!task)
62099f89551SEric W. Biederman 		goto out_no_task;
6211da177e4SLinus Torvalds 
6221da177e4SLinus Torvalds 	if (count > PROC_BLOCK_SIZE)
6231da177e4SLinus Torvalds 		count = PROC_BLOCK_SIZE;
62499f89551SEric W. Biederman 
62599f89551SEric W. Biederman 	length = -ENOMEM;
626e12ba74dSMel Gorman 	if (!(page = __get_free_page(GFP_TEMPORARY)))
62799f89551SEric W. Biederman 		goto out;
6281da177e4SLinus Torvalds 
6291da177e4SLinus Torvalds 	length = PROC_I(inode)->op.proc_read(task, (char*)page);
6301da177e4SLinus Torvalds 
6311da177e4SLinus Torvalds 	if (length >= 0)
6321da177e4SLinus Torvalds 		length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
6331da177e4SLinus Torvalds 	free_page(page);
63499f89551SEric W. Biederman out:
63599f89551SEric W. Biederman 	put_task_struct(task);
63699f89551SEric W. Biederman out_no_task:
6371da177e4SLinus Torvalds 	return length;
6381da177e4SLinus Torvalds }
6391da177e4SLinus Torvalds 
64000977a59SArjan van de Ven static const struct file_operations proc_info_file_operations = {
6411da177e4SLinus Torvalds 	.read		= proc_info_read,
64287df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
6431da177e4SLinus Torvalds };
6441da177e4SLinus Torvalds 
645be614086SEric W. Biederman static int proc_single_show(struct seq_file *m, void *v)
646be614086SEric W. Biederman {
647be614086SEric W. Biederman 	struct inode *inode = m->private;
648be614086SEric W. Biederman 	struct pid_namespace *ns;
649be614086SEric W. Biederman 	struct pid *pid;
650be614086SEric W. Biederman 	struct task_struct *task;
651be614086SEric W. Biederman 	int ret;
652be614086SEric W. Biederman 
653be614086SEric W. Biederman 	ns = inode->i_sb->s_fs_info;
654be614086SEric W. Biederman 	pid = proc_pid(inode);
655be614086SEric W. Biederman 	task = get_pid_task(pid, PIDTYPE_PID);
656be614086SEric W. Biederman 	if (!task)
657be614086SEric W. Biederman 		return -ESRCH;
658be614086SEric W. Biederman 
659be614086SEric W. Biederman 	ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
660be614086SEric W. Biederman 
661be614086SEric W. Biederman 	put_task_struct(task);
662be614086SEric W. Biederman 	return ret;
663be614086SEric W. Biederman }
664be614086SEric W. Biederman 
665be614086SEric W. Biederman static int proc_single_open(struct inode *inode, struct file *filp)
666be614086SEric W. Biederman {
667c6a34058SJovi Zhang 	return single_open(filp, proc_single_show, inode);
668be614086SEric W. Biederman }
669be614086SEric W. Biederman 
670be614086SEric W. Biederman static const struct file_operations proc_single_file_operations = {
671be614086SEric W. Biederman 	.open		= proc_single_open,
672be614086SEric W. Biederman 	.read		= seq_read,
673be614086SEric W. Biederman 	.llseek		= seq_lseek,
674be614086SEric W. Biederman 	.release	= single_release,
675be614086SEric W. Biederman };
676be614086SEric W. Biederman 
677b409e578SCong Wang static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
6781da177e4SLinus Torvalds {
679e268337dSLinus Torvalds 	struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
680e268337dSLinus Torvalds 	struct mm_struct *mm;
681e268337dSLinus Torvalds 
682e268337dSLinus Torvalds 	if (!task)
683e268337dSLinus Torvalds 		return -ESRCH;
684e268337dSLinus Torvalds 
685b409e578SCong Wang 	mm = mm_access(task, mode);
686e268337dSLinus Torvalds 	put_task_struct(task);
687e268337dSLinus Torvalds 
688e268337dSLinus Torvalds 	if (IS_ERR(mm))
689e268337dSLinus Torvalds 		return PTR_ERR(mm);
690e268337dSLinus Torvalds 
6916d08f2c7SOleg Nesterov 	if (mm) {
6926d08f2c7SOleg Nesterov 		/* ensure this mm_struct can't be freed */
6936d08f2c7SOleg Nesterov 		atomic_inc(&mm->mm_count);
6946d08f2c7SOleg Nesterov 		/* but do not pin its memory */
6956d08f2c7SOleg Nesterov 		mmput(mm);
6966d08f2c7SOleg Nesterov 	}
6976d08f2c7SOleg Nesterov 
698e268337dSLinus Torvalds 	file->private_data = mm;
699e268337dSLinus Torvalds 
7001da177e4SLinus Torvalds 	return 0;
7011da177e4SLinus Torvalds }
7021da177e4SLinus Torvalds 
703b409e578SCong Wang static int mem_open(struct inode *inode, struct file *file)
704b409e578SCong Wang {
705bc452b4bSDjalal Harouni 	int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
706bc452b4bSDjalal Harouni 
707bc452b4bSDjalal Harouni 	/* OK to pass negative loff_t, we can catch out-of-range */
708bc452b4bSDjalal Harouni 	file->f_mode |= FMODE_UNSIGNED_OFFSET;
709bc452b4bSDjalal Harouni 
710bc452b4bSDjalal Harouni 	return ret;
711b409e578SCong Wang }
712b409e578SCong Wang 
713572d34b9SOleg Nesterov static ssize_t mem_rw(struct file *file, char __user *buf,
714572d34b9SOleg Nesterov 			size_t count, loff_t *ppos, int write)
7151da177e4SLinus Torvalds {
716e268337dSLinus Torvalds 	struct mm_struct *mm = file->private_data;
717572d34b9SOleg Nesterov 	unsigned long addr = *ppos;
718572d34b9SOleg Nesterov 	ssize_t copied;
7191da177e4SLinus Torvalds 	char *page;
7201da177e4SLinus Torvalds 
721e268337dSLinus Torvalds 	if (!mm)
722e268337dSLinus Torvalds 		return 0;
7231da177e4SLinus Torvalds 
724e12ba74dSMel Gorman 	page = (char *)__get_free_page(GFP_TEMPORARY);
7251da177e4SLinus Torvalds 	if (!page)
726e268337dSLinus Torvalds 		return -ENOMEM;
7271da177e4SLinus Torvalds 
728f7ca54f4SFrederik Deweerdt 	copied = 0;
7296d08f2c7SOleg Nesterov 	if (!atomic_inc_not_zero(&mm->mm_users))
7306d08f2c7SOleg Nesterov 		goto free;
7316d08f2c7SOleg Nesterov 
7321da177e4SLinus Torvalds 	while (count > 0) {
733572d34b9SOleg Nesterov 		int this_len = min_t(int, count, PAGE_SIZE);
7341da177e4SLinus Torvalds 
735572d34b9SOleg Nesterov 		if (write && copy_from_user(page, buf, this_len)) {
7361da177e4SLinus Torvalds 			copied = -EFAULT;
7371da177e4SLinus Torvalds 			break;
7381da177e4SLinus Torvalds 		}
739572d34b9SOleg Nesterov 
740572d34b9SOleg Nesterov 		this_len = access_remote_vm(mm, addr, page, this_len, write);
741572d34b9SOleg Nesterov 		if (!this_len) {
7421da177e4SLinus Torvalds 			if (!copied)
7431da177e4SLinus Torvalds 				copied = -EIO;
7441da177e4SLinus Torvalds 			break;
7451da177e4SLinus Torvalds 		}
746572d34b9SOleg Nesterov 
747572d34b9SOleg Nesterov 		if (!write && copy_to_user(buf, page, this_len)) {
748572d34b9SOleg Nesterov 			copied = -EFAULT;
749572d34b9SOleg Nesterov 			break;
7501da177e4SLinus Torvalds 		}
751572d34b9SOleg Nesterov 
752572d34b9SOleg Nesterov 		buf += this_len;
753572d34b9SOleg Nesterov 		addr += this_len;
754572d34b9SOleg Nesterov 		copied += this_len;
755572d34b9SOleg Nesterov 		count -= this_len;
756572d34b9SOleg Nesterov 	}
757572d34b9SOleg Nesterov 	*ppos = addr;
75830cd8903SKOSAKI Motohiro 
7596d08f2c7SOleg Nesterov 	mmput(mm);
7606d08f2c7SOleg Nesterov free:
76130cd8903SKOSAKI Motohiro 	free_page((unsigned long) page);
7621da177e4SLinus Torvalds 	return copied;
7631da177e4SLinus Torvalds }
7641da177e4SLinus Torvalds 
765572d34b9SOleg Nesterov static ssize_t mem_read(struct file *file, char __user *buf,
766572d34b9SOleg Nesterov 			size_t count, loff_t *ppos)
767572d34b9SOleg Nesterov {
768572d34b9SOleg Nesterov 	return mem_rw(file, buf, count, ppos, 0);
769572d34b9SOleg Nesterov }
770572d34b9SOleg Nesterov 
771572d34b9SOleg Nesterov static ssize_t mem_write(struct file *file, const char __user *buf,
772572d34b9SOleg Nesterov 			 size_t count, loff_t *ppos)
773572d34b9SOleg Nesterov {
774572d34b9SOleg Nesterov 	return mem_rw(file, (char __user*)buf, count, ppos, 1);
775572d34b9SOleg Nesterov }
776572d34b9SOleg Nesterov 
77785863e47SMatt Mackall loff_t mem_lseek(struct file *file, loff_t offset, int orig)
7781da177e4SLinus Torvalds {
7791da177e4SLinus Torvalds 	switch (orig) {
7801da177e4SLinus Torvalds 	case 0:
7811da177e4SLinus Torvalds 		file->f_pos = offset;
7821da177e4SLinus Torvalds 		break;
7831da177e4SLinus Torvalds 	case 1:
7841da177e4SLinus Torvalds 		file->f_pos += offset;
7851da177e4SLinus Torvalds 		break;
7861da177e4SLinus Torvalds 	default:
7871da177e4SLinus Torvalds 		return -EINVAL;
7881da177e4SLinus Torvalds 	}
7891da177e4SLinus Torvalds 	force_successful_syscall_return();
7901da177e4SLinus Torvalds 	return file->f_pos;
7911da177e4SLinus Torvalds }
7921da177e4SLinus Torvalds 
793e268337dSLinus Torvalds static int mem_release(struct inode *inode, struct file *file)
794e268337dSLinus Torvalds {
795e268337dSLinus Torvalds 	struct mm_struct *mm = file->private_data;
79671879d3cSOleg Nesterov 	if (mm)
7976d08f2c7SOleg Nesterov 		mmdrop(mm);
798e268337dSLinus Torvalds 	return 0;
799e268337dSLinus Torvalds }
800e268337dSLinus Torvalds 
80100977a59SArjan van de Ven static const struct file_operations proc_mem_operations = {
8021da177e4SLinus Torvalds 	.llseek		= mem_lseek,
8031da177e4SLinus Torvalds 	.read		= mem_read,
8041da177e4SLinus Torvalds 	.write		= mem_write,
8051da177e4SLinus Torvalds 	.open		= mem_open,
806e268337dSLinus Torvalds 	.release	= mem_release,
8071da177e4SLinus Torvalds };
8081da177e4SLinus Torvalds 
809b409e578SCong Wang static int environ_open(struct inode *inode, struct file *file)
810b409e578SCong Wang {
811b409e578SCong Wang 	return __mem_open(inode, file, PTRACE_MODE_READ);
812b409e578SCong Wang }
813b409e578SCong Wang 
814315e28c8SJames Pearson static ssize_t environ_read(struct file *file, char __user *buf,
815315e28c8SJames Pearson 			size_t count, loff_t *ppos)
816315e28c8SJames Pearson {
817315e28c8SJames Pearson 	char *page;
818315e28c8SJames Pearson 	unsigned long src = *ppos;
819b409e578SCong Wang 	int ret = 0;
820b409e578SCong Wang 	struct mm_struct *mm = file->private_data;
821315e28c8SJames Pearson 
822b409e578SCong Wang 	if (!mm)
823b409e578SCong Wang 		return 0;
824315e28c8SJames Pearson 
825315e28c8SJames Pearson 	page = (char *)__get_free_page(GFP_TEMPORARY);
826315e28c8SJames Pearson 	if (!page)
827b409e578SCong Wang 		return -ENOMEM;
828315e28c8SJames Pearson 
829d6f64b89SAl Viro 	ret = 0;
830b409e578SCong Wang 	if (!atomic_inc_not_zero(&mm->mm_users))
831b409e578SCong Wang 		goto free;
832315e28c8SJames Pearson 	while (count > 0) {
833e8905ec2SDjalal Harouni 		size_t this_len, max_len;
834e8905ec2SDjalal Harouni 		int retval;
835e8905ec2SDjalal Harouni 
836e8905ec2SDjalal Harouni 		if (src >= (mm->env_end - mm->env_start))
837e8905ec2SDjalal Harouni 			break;
838315e28c8SJames Pearson 
839315e28c8SJames Pearson 		this_len = mm->env_end - (mm->env_start + src);
840315e28c8SJames Pearson 
841e8905ec2SDjalal Harouni 		max_len = min_t(size_t, PAGE_SIZE, count);
842e8905ec2SDjalal Harouni 		this_len = min(max_len, this_len);
843315e28c8SJames Pearson 
844b409e578SCong Wang 		retval = access_remote_vm(mm, (mm->env_start + src),
845315e28c8SJames Pearson 			page, this_len, 0);
846315e28c8SJames Pearson 
847315e28c8SJames Pearson 		if (retval <= 0) {
848315e28c8SJames Pearson 			ret = retval;
849315e28c8SJames Pearson 			break;
850315e28c8SJames Pearson 		}
851315e28c8SJames Pearson 
852315e28c8SJames Pearson 		if (copy_to_user(buf, page, retval)) {
853315e28c8SJames Pearson 			ret = -EFAULT;
854315e28c8SJames Pearson 			break;
855315e28c8SJames Pearson 		}
856315e28c8SJames Pearson 
857315e28c8SJames Pearson 		ret += retval;
858315e28c8SJames Pearson 		src += retval;
859315e28c8SJames Pearson 		buf += retval;
860315e28c8SJames Pearson 		count -= retval;
861315e28c8SJames Pearson 	}
862315e28c8SJames Pearson 	*ppos = src;
863315e28c8SJames Pearson 	mmput(mm);
864b409e578SCong Wang 
865b409e578SCong Wang free:
866315e28c8SJames Pearson 	free_page((unsigned long) page);
867315e28c8SJames Pearson 	return ret;
868315e28c8SJames Pearson }
869315e28c8SJames Pearson 
870315e28c8SJames Pearson static const struct file_operations proc_environ_operations = {
871b409e578SCong Wang 	.open		= environ_open,
872315e28c8SJames Pearson 	.read		= environ_read,
87387df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
874b409e578SCong Wang 	.release	= mem_release,
875315e28c8SJames Pearson };
876315e28c8SJames Pearson 
8771da177e4SLinus Torvalds static ssize_t oom_adjust_read(struct file *file, char __user *buf,
8781da177e4SLinus Torvalds 				size_t count, loff_t *ppos)
8791da177e4SLinus Torvalds {
8802fddfeefSJosef "Jeff" Sipek 	struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
8818578cea7SEric W. Biederman 	char buffer[PROC_NUMBUF];
8821da177e4SLinus Torvalds 	size_t len;
88328b83c51SKOSAKI Motohiro 	int oom_adjust = OOM_DISABLE;
88428b83c51SKOSAKI Motohiro 	unsigned long flags;
8851da177e4SLinus Torvalds 
88699f89551SEric W. Biederman 	if (!task)
88799f89551SEric W. Biederman 		return -ESRCH;
88828b83c51SKOSAKI Motohiro 
88928b83c51SKOSAKI Motohiro 	if (lock_task_sighand(task, &flags)) {
89028b83c51SKOSAKI Motohiro 		oom_adjust = task->signal->oom_adj;
89128b83c51SKOSAKI Motohiro 		unlock_task_sighand(task, &flags);
89228b83c51SKOSAKI Motohiro 	}
89328b83c51SKOSAKI Motohiro 
89499f89551SEric W. Biederman 	put_task_struct(task);
89599f89551SEric W. Biederman 
8968578cea7SEric W. Biederman 	len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
8970c28f287SAkinobu Mita 
8980c28f287SAkinobu Mita 	return simple_read_from_buffer(buf, count, ppos, buffer, len);
8991da177e4SLinus Torvalds }
9001da177e4SLinus Torvalds 
9011da177e4SLinus Torvalds static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
9021da177e4SLinus Torvalds 				size_t count, loff_t *ppos)
9031da177e4SLinus Torvalds {
90499f89551SEric W. Biederman 	struct task_struct *task;
9055d863b89SKOSAKI Motohiro 	char buffer[PROC_NUMBUF];
9060a8cb8e3SAlexey Dobriyan 	int oom_adjust;
90728b83c51SKOSAKI Motohiro 	unsigned long flags;
9085d863b89SKOSAKI Motohiro 	int err;
9091da177e4SLinus Torvalds 
9108578cea7SEric W. Biederman 	memset(buffer, 0, sizeof(buffer));
9118578cea7SEric W. Biederman 	if (count > sizeof(buffer) - 1)
9128578cea7SEric W. Biederman 		count = sizeof(buffer) - 1;
913723548bfSDavid Rientjes 	if (copy_from_user(buffer, buf, count)) {
914723548bfSDavid Rientjes 		err = -EFAULT;
915723548bfSDavid Rientjes 		goto out;
916723548bfSDavid Rientjes 	}
9175d863b89SKOSAKI Motohiro 
9180a8cb8e3SAlexey Dobriyan 	err = kstrtoint(strstrip(buffer), 0, &oom_adjust);
9195d863b89SKOSAKI Motohiro 	if (err)
920723548bfSDavid Rientjes 		goto out;
9218ac773b4SAlexey Dobriyan 	if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
922723548bfSDavid Rientjes 	     oom_adjust != OOM_DISABLE) {
923723548bfSDavid Rientjes 		err = -EINVAL;
924723548bfSDavid Rientjes 		goto out;
925723548bfSDavid Rientjes 	}
9265d863b89SKOSAKI Motohiro 
9272fddfeefSJosef "Jeff" Sipek 	task = get_proc_task(file->f_path.dentry->d_inode);
928723548bfSDavid Rientjes 	if (!task) {
929723548bfSDavid Rientjes 		err = -ESRCH;
930723548bfSDavid Rientjes 		goto out;
931723548bfSDavid Rientjes 	}
93228b83c51SKOSAKI Motohiro 
9333d5992d2SYing Han 	task_lock(task);
9343d5992d2SYing Han 	if (!task->mm) {
935723548bfSDavid Rientjes 		err = -EINVAL;
936723548bfSDavid Rientjes 		goto err_task_lock;
9373d5992d2SYing Han 	}
9383d5992d2SYing Han 
939d19d5476SDavid Rientjes 	if (!lock_task_sighand(task, &flags)) {
940d19d5476SDavid Rientjes 		err = -ESRCH;
941d19d5476SDavid Rientjes 		goto err_task_lock;
942d19d5476SDavid Rientjes 	}
943d19d5476SDavid Rientjes 
944d19d5476SDavid Rientjes 	if (oom_adjust < task->signal->oom_adj && !capable(CAP_SYS_RESOURCE)) {
945d19d5476SDavid Rientjes 		err = -EACCES;
946d19d5476SDavid Rientjes 		goto err_sighand;
947d19d5476SDavid Rientjes 	}
948d19d5476SDavid Rientjes 
94951b1bd2aSDavid Rientjes 	/*
95051b1bd2aSDavid Rientjes 	 * Warn that /proc/pid/oom_adj is deprecated, see
95151b1bd2aSDavid Rientjes 	 * Documentation/feature-removal-schedule.txt.
95251b1bd2aSDavid Rientjes 	 */
953c2142704SLinus Torvalds 	printk_once(KERN_WARNING "%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
954be8f684dSDavid Rientjes 		  current->comm, task_pid_nr(current), task_pid_nr(task),
955be8f684dSDavid Rientjes 		  task_pid_nr(task));
95628b83c51SKOSAKI Motohiro 	task->signal->oom_adj = oom_adjust;
957a63d83f4SDavid Rientjes 	/*
958a63d83f4SDavid Rientjes 	 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
959a63d83f4SDavid Rientjes 	 * value is always attainable.
960a63d83f4SDavid Rientjes 	 */
961a63d83f4SDavid Rientjes 	if (task->signal->oom_adj == OOM_ADJUST_MAX)
962a63d83f4SDavid Rientjes 		task->signal->oom_score_adj = OOM_SCORE_ADJ_MAX;
963a63d83f4SDavid Rientjes 	else
964a63d83f4SDavid Rientjes 		task->signal->oom_score_adj = (oom_adjust * OOM_SCORE_ADJ_MAX) /
965a63d83f4SDavid Rientjes 								-OOM_DISABLE;
96643d2b113SKAMEZAWA Hiroyuki 	trace_oom_score_adj_update(task);
967723548bfSDavid Rientjes err_sighand:
96828b83c51SKOSAKI Motohiro 	unlock_task_sighand(task, &flags);
969d19d5476SDavid Rientjes err_task_lock:
970d19d5476SDavid Rientjes 	task_unlock(task);
97199f89551SEric W. Biederman 	put_task_struct(task);
972723548bfSDavid Rientjes out:
973723548bfSDavid Rientjes 	return err < 0 ? err : count;
9741da177e4SLinus Torvalds }
9751da177e4SLinus Torvalds 
97600977a59SArjan van de Ven static const struct file_operations proc_oom_adjust_operations = {
9771da177e4SLinus Torvalds 	.read		= oom_adjust_read,
9781da177e4SLinus Torvalds 	.write		= oom_adjust_write,
97987df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
9801da177e4SLinus Torvalds };
9811da177e4SLinus Torvalds 
982a63d83f4SDavid Rientjes static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
983a63d83f4SDavid Rientjes 					size_t count, loff_t *ppos)
984a63d83f4SDavid Rientjes {
985a63d83f4SDavid Rientjes 	struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
986a63d83f4SDavid Rientjes 	char buffer[PROC_NUMBUF];
987a63d83f4SDavid Rientjes 	int oom_score_adj = OOM_SCORE_ADJ_MIN;
988a63d83f4SDavid Rientjes 	unsigned long flags;
989a63d83f4SDavid Rientjes 	size_t len;
990a63d83f4SDavid Rientjes 
991a63d83f4SDavid Rientjes 	if (!task)
992a63d83f4SDavid Rientjes 		return -ESRCH;
993a63d83f4SDavid Rientjes 	if (lock_task_sighand(task, &flags)) {
994a63d83f4SDavid Rientjes 		oom_score_adj = task->signal->oom_score_adj;
995a63d83f4SDavid Rientjes 		unlock_task_sighand(task, &flags);
996a63d83f4SDavid Rientjes 	}
997a63d83f4SDavid Rientjes 	put_task_struct(task);
998a63d83f4SDavid Rientjes 	len = snprintf(buffer, sizeof(buffer), "%d\n", oom_score_adj);
999a63d83f4SDavid Rientjes 	return simple_read_from_buffer(buf, count, ppos, buffer, len);
1000a63d83f4SDavid Rientjes }
1001a63d83f4SDavid Rientjes 
1002a63d83f4SDavid Rientjes static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1003a63d83f4SDavid Rientjes 					size_t count, loff_t *ppos)
1004a63d83f4SDavid Rientjes {
1005a63d83f4SDavid Rientjes 	struct task_struct *task;
1006a63d83f4SDavid Rientjes 	char buffer[PROC_NUMBUF];
1007a63d83f4SDavid Rientjes 	unsigned long flags;
10080a8cb8e3SAlexey Dobriyan 	int oom_score_adj;
1009a63d83f4SDavid Rientjes 	int err;
1010a63d83f4SDavid Rientjes 
1011a63d83f4SDavid Rientjes 	memset(buffer, 0, sizeof(buffer));
1012a63d83f4SDavid Rientjes 	if (count > sizeof(buffer) - 1)
1013a63d83f4SDavid Rientjes 		count = sizeof(buffer) - 1;
1014723548bfSDavid Rientjes 	if (copy_from_user(buffer, buf, count)) {
1015723548bfSDavid Rientjes 		err = -EFAULT;
1016723548bfSDavid Rientjes 		goto out;
1017723548bfSDavid Rientjes 	}
1018a63d83f4SDavid Rientjes 
10190a8cb8e3SAlexey Dobriyan 	err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
1020a63d83f4SDavid Rientjes 	if (err)
1021723548bfSDavid Rientjes 		goto out;
1022a63d83f4SDavid Rientjes 	if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
1023723548bfSDavid Rientjes 			oom_score_adj > OOM_SCORE_ADJ_MAX) {
1024723548bfSDavid Rientjes 		err = -EINVAL;
1025723548bfSDavid Rientjes 		goto out;
1026723548bfSDavid Rientjes 	}
1027a63d83f4SDavid Rientjes 
1028a63d83f4SDavid Rientjes 	task = get_proc_task(file->f_path.dentry->d_inode);
1029723548bfSDavid Rientjes 	if (!task) {
1030723548bfSDavid Rientjes 		err = -ESRCH;
1031723548bfSDavid Rientjes 		goto out;
1032723548bfSDavid Rientjes 	}
1033a63d83f4SDavid Rientjes 
10343d5992d2SYing Han 	task_lock(task);
10353d5992d2SYing Han 	if (!task->mm) {
1036723548bfSDavid Rientjes 		err = -EINVAL;
1037723548bfSDavid Rientjes 		goto err_task_lock;
10383d5992d2SYing Han 	}
1039d19d5476SDavid Rientjes 
1040d19d5476SDavid Rientjes 	if (!lock_task_sighand(task, &flags)) {
1041d19d5476SDavid Rientjes 		err = -ESRCH;
1042d19d5476SDavid Rientjes 		goto err_task_lock;
1043d19d5476SDavid Rientjes 	}
1044d19d5476SDavid Rientjes 
1045dabb16f6SMandeep Singh Baines 	if (oom_score_adj < task->signal->oom_score_adj_min &&
1046d19d5476SDavid Rientjes 			!capable(CAP_SYS_RESOURCE)) {
1047d19d5476SDavid Rientjes 		err = -EACCES;
1048d19d5476SDavid Rientjes 		goto err_sighand;
1049d19d5476SDavid Rientjes 	}
1050d19d5476SDavid Rientjes 
1051a63d83f4SDavid Rientjes 	task->signal->oom_score_adj = oom_score_adj;
1052dabb16f6SMandeep Singh Baines 	if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
1053dabb16f6SMandeep Singh Baines 		task->signal->oom_score_adj_min = oom_score_adj;
105443d2b113SKAMEZAWA Hiroyuki 	trace_oom_score_adj_update(task);
1055a63d83f4SDavid Rientjes 	/*
1056a63d83f4SDavid Rientjes 	 * Scale /proc/pid/oom_adj appropriately ensuring that OOM_DISABLE is
1057a63d83f4SDavid Rientjes 	 * always attainable.
1058a63d83f4SDavid Rientjes 	 */
1059a63d83f4SDavid Rientjes 	if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MIN)
1060a63d83f4SDavid Rientjes 		task->signal->oom_adj = OOM_DISABLE;
1061a63d83f4SDavid Rientjes 	else
1062a63d83f4SDavid Rientjes 		task->signal->oom_adj = (oom_score_adj * OOM_ADJUST_MAX) /
1063a63d83f4SDavid Rientjes 							OOM_SCORE_ADJ_MAX;
1064723548bfSDavid Rientjes err_sighand:
1065a63d83f4SDavid Rientjes 	unlock_task_sighand(task, &flags);
1066d19d5476SDavid Rientjes err_task_lock:
1067d19d5476SDavid Rientjes 	task_unlock(task);
1068a63d83f4SDavid Rientjes 	put_task_struct(task);
1069723548bfSDavid Rientjes out:
1070723548bfSDavid Rientjes 	return err < 0 ? err : count;
1071a63d83f4SDavid Rientjes }
1072a63d83f4SDavid Rientjes 
1073a63d83f4SDavid Rientjes static const struct file_operations proc_oom_score_adj_operations = {
1074a63d83f4SDavid Rientjes 	.read		= oom_score_adj_read,
1075a63d83f4SDavid Rientjes 	.write		= oom_score_adj_write,
10766038f373SArnd Bergmann 	.llseek		= default_llseek,
1077a63d83f4SDavid Rientjes };
1078a63d83f4SDavid Rientjes 
10791da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL
10801da177e4SLinus Torvalds #define TMPBUFLEN 21
10811da177e4SLinus Torvalds static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
10821da177e4SLinus Torvalds 				  size_t count, loff_t *ppos)
10831da177e4SLinus Torvalds {
10842fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
108599f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
10861da177e4SLinus Torvalds 	ssize_t length;
10871da177e4SLinus Torvalds 	char tmpbuf[TMPBUFLEN];
10881da177e4SLinus Torvalds 
108999f89551SEric W. Biederman 	if (!task)
109099f89551SEric W. Biederman 		return -ESRCH;
10911da177e4SLinus Torvalds 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1092*e1760bd5SEric W. Biederman 			   from_kuid(file->f_cred->user_ns,
1093*e1760bd5SEric W. Biederman 				     audit_get_loginuid(task)));
109499f89551SEric W. Biederman 	put_task_struct(task);
10951da177e4SLinus Torvalds 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
10961da177e4SLinus Torvalds }
10971da177e4SLinus Torvalds 
10981da177e4SLinus Torvalds static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
10991da177e4SLinus Torvalds 				   size_t count, loff_t *ppos)
11001da177e4SLinus Torvalds {
11012fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
11021da177e4SLinus Torvalds 	char *page, *tmp;
11031da177e4SLinus Torvalds 	ssize_t length;
11041da177e4SLinus Torvalds 	uid_t loginuid;
1105*e1760bd5SEric W. Biederman 	kuid_t kloginuid;
11061da177e4SLinus Torvalds 
11077dc52157SPaul E. McKenney 	rcu_read_lock();
11087dc52157SPaul E. McKenney 	if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
11097dc52157SPaul E. McKenney 		rcu_read_unlock();
11101da177e4SLinus Torvalds 		return -EPERM;
11117dc52157SPaul E. McKenney 	}
11127dc52157SPaul E. McKenney 	rcu_read_unlock();
11131da177e4SLinus Torvalds 
1114e0182909SAl Viro 	if (count >= PAGE_SIZE)
1115e0182909SAl Viro 		count = PAGE_SIZE - 1;
11161da177e4SLinus Torvalds 
11171da177e4SLinus Torvalds 	if (*ppos != 0) {
11181da177e4SLinus Torvalds 		/* No partial writes. */
11191da177e4SLinus Torvalds 		return -EINVAL;
11201da177e4SLinus Torvalds 	}
1121e12ba74dSMel Gorman 	page = (char*)__get_free_page(GFP_TEMPORARY);
11221da177e4SLinus Torvalds 	if (!page)
11231da177e4SLinus Torvalds 		return -ENOMEM;
11241da177e4SLinus Torvalds 	length = -EFAULT;
11251da177e4SLinus Torvalds 	if (copy_from_user(page, buf, count))
11261da177e4SLinus Torvalds 		goto out_free_page;
11271da177e4SLinus Torvalds 
1128e0182909SAl Viro 	page[count] = '\0';
11291da177e4SLinus Torvalds 	loginuid = simple_strtoul(page, &tmp, 10);
11301da177e4SLinus Torvalds 	if (tmp == page) {
11311da177e4SLinus Torvalds 		length = -EINVAL;
11321da177e4SLinus Torvalds 		goto out_free_page;
11331da177e4SLinus Torvalds 
11341da177e4SLinus Torvalds 	}
1135*e1760bd5SEric W. Biederman 	kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
1136*e1760bd5SEric W. Biederman 	if (!uid_valid(kloginuid)) {
1137*e1760bd5SEric W. Biederman 		length = -EINVAL;
1138*e1760bd5SEric W. Biederman 		goto out_free_page;
1139*e1760bd5SEric W. Biederman 	}
1140*e1760bd5SEric W. Biederman 
1141*e1760bd5SEric W. Biederman 	length = audit_set_loginuid(kloginuid);
11421da177e4SLinus Torvalds 	if (likely(length == 0))
11431da177e4SLinus Torvalds 		length = count;
11441da177e4SLinus Torvalds 
11451da177e4SLinus Torvalds out_free_page:
11461da177e4SLinus Torvalds 	free_page((unsigned long) page);
11471da177e4SLinus Torvalds 	return length;
11481da177e4SLinus Torvalds }
11491da177e4SLinus Torvalds 
115000977a59SArjan van de Ven static const struct file_operations proc_loginuid_operations = {
11511da177e4SLinus Torvalds 	.read		= proc_loginuid_read,
11521da177e4SLinus Torvalds 	.write		= proc_loginuid_write,
115387df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
11541da177e4SLinus Torvalds };
11551e0bd755SEric Paris 
11561e0bd755SEric Paris static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
11571e0bd755SEric Paris 				  size_t count, loff_t *ppos)
11581e0bd755SEric Paris {
11591e0bd755SEric Paris 	struct inode * inode = file->f_path.dentry->d_inode;
11601e0bd755SEric Paris 	struct task_struct *task = get_proc_task(inode);
11611e0bd755SEric Paris 	ssize_t length;
11621e0bd755SEric Paris 	char tmpbuf[TMPBUFLEN];
11631e0bd755SEric Paris 
11641e0bd755SEric Paris 	if (!task)
11651e0bd755SEric Paris 		return -ESRCH;
11661e0bd755SEric Paris 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
11671e0bd755SEric Paris 				audit_get_sessionid(task));
11681e0bd755SEric Paris 	put_task_struct(task);
11691e0bd755SEric Paris 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
11701e0bd755SEric Paris }
11711e0bd755SEric Paris 
11721e0bd755SEric Paris static const struct file_operations proc_sessionid_operations = {
11731e0bd755SEric Paris 	.read		= proc_sessionid_read,
117487df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
11751e0bd755SEric Paris };
11761da177e4SLinus Torvalds #endif
11771da177e4SLinus Torvalds 
1178f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
1179f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1180f4f154fdSAkinobu Mita 				      size_t count, loff_t *ppos)
1181f4f154fdSAkinobu Mita {
1182f4f154fdSAkinobu Mita 	struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1183f4f154fdSAkinobu Mita 	char buffer[PROC_NUMBUF];
1184f4f154fdSAkinobu Mita 	size_t len;
1185f4f154fdSAkinobu Mita 	int make_it_fail;
1186f4f154fdSAkinobu Mita 
1187f4f154fdSAkinobu Mita 	if (!task)
1188f4f154fdSAkinobu Mita 		return -ESRCH;
1189f4f154fdSAkinobu Mita 	make_it_fail = task->make_it_fail;
1190f4f154fdSAkinobu Mita 	put_task_struct(task);
1191f4f154fdSAkinobu Mita 
1192f4f154fdSAkinobu Mita 	len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
11930c28f287SAkinobu Mita 
11940c28f287SAkinobu Mita 	return simple_read_from_buffer(buf, count, ppos, buffer, len);
1195f4f154fdSAkinobu Mita }
1196f4f154fdSAkinobu Mita 
1197f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_write(struct file * file,
1198f4f154fdSAkinobu Mita 			const char __user * buf, size_t count, loff_t *ppos)
1199f4f154fdSAkinobu Mita {
1200f4f154fdSAkinobu Mita 	struct task_struct *task;
1201f4f154fdSAkinobu Mita 	char buffer[PROC_NUMBUF], *end;
1202f4f154fdSAkinobu Mita 	int make_it_fail;
1203f4f154fdSAkinobu Mita 
1204f4f154fdSAkinobu Mita 	if (!capable(CAP_SYS_RESOURCE))
1205f4f154fdSAkinobu Mita 		return -EPERM;
1206f4f154fdSAkinobu Mita 	memset(buffer, 0, sizeof(buffer));
1207f4f154fdSAkinobu Mita 	if (count > sizeof(buffer) - 1)
1208f4f154fdSAkinobu Mita 		count = sizeof(buffer) - 1;
1209f4f154fdSAkinobu Mita 	if (copy_from_user(buffer, buf, count))
1210f4f154fdSAkinobu Mita 		return -EFAULT;
1211cba8aafeSVincent Li 	make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1212cba8aafeSVincent Li 	if (*end)
1213cba8aafeSVincent Li 		return -EINVAL;
1214f4f154fdSAkinobu Mita 	task = get_proc_task(file->f_dentry->d_inode);
1215f4f154fdSAkinobu Mita 	if (!task)
1216f4f154fdSAkinobu Mita 		return -ESRCH;
1217f4f154fdSAkinobu Mita 	task->make_it_fail = make_it_fail;
1218f4f154fdSAkinobu Mita 	put_task_struct(task);
1219cba8aafeSVincent Li 
1220cba8aafeSVincent Li 	return count;
1221f4f154fdSAkinobu Mita }
1222f4f154fdSAkinobu Mita 
122300977a59SArjan van de Ven static const struct file_operations proc_fault_inject_operations = {
1224f4f154fdSAkinobu Mita 	.read		= proc_fault_inject_read,
1225f4f154fdSAkinobu Mita 	.write		= proc_fault_inject_write,
122687df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
1227f4f154fdSAkinobu Mita };
1228f4f154fdSAkinobu Mita #endif
1229f4f154fdSAkinobu Mita 
12309745512cSArjan van de Ven 
123143ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
123243ae34cbSIngo Molnar /*
123343ae34cbSIngo Molnar  * Print out various scheduling related per-task fields:
123443ae34cbSIngo Molnar  */
123543ae34cbSIngo Molnar static int sched_show(struct seq_file *m, void *v)
123643ae34cbSIngo Molnar {
123743ae34cbSIngo Molnar 	struct inode *inode = m->private;
123843ae34cbSIngo Molnar 	struct task_struct *p;
123943ae34cbSIngo Molnar 
124043ae34cbSIngo Molnar 	p = get_proc_task(inode);
124143ae34cbSIngo Molnar 	if (!p)
124243ae34cbSIngo Molnar 		return -ESRCH;
124343ae34cbSIngo Molnar 	proc_sched_show_task(p, m);
124443ae34cbSIngo Molnar 
124543ae34cbSIngo Molnar 	put_task_struct(p);
124643ae34cbSIngo Molnar 
124743ae34cbSIngo Molnar 	return 0;
124843ae34cbSIngo Molnar }
124943ae34cbSIngo Molnar 
125043ae34cbSIngo Molnar static ssize_t
125143ae34cbSIngo Molnar sched_write(struct file *file, const char __user *buf,
125243ae34cbSIngo Molnar 	    size_t count, loff_t *offset)
125343ae34cbSIngo Molnar {
125443ae34cbSIngo Molnar 	struct inode *inode = file->f_path.dentry->d_inode;
125543ae34cbSIngo Molnar 	struct task_struct *p;
125643ae34cbSIngo Molnar 
125743ae34cbSIngo Molnar 	p = get_proc_task(inode);
125843ae34cbSIngo Molnar 	if (!p)
125943ae34cbSIngo Molnar 		return -ESRCH;
126043ae34cbSIngo Molnar 	proc_sched_set_task(p);
126143ae34cbSIngo Molnar 
126243ae34cbSIngo Molnar 	put_task_struct(p);
126343ae34cbSIngo Molnar 
126443ae34cbSIngo Molnar 	return count;
126543ae34cbSIngo Molnar }
126643ae34cbSIngo Molnar 
126743ae34cbSIngo Molnar static int sched_open(struct inode *inode, struct file *filp)
126843ae34cbSIngo Molnar {
1269c6a34058SJovi Zhang 	return single_open(filp, sched_show, inode);
127043ae34cbSIngo Molnar }
127143ae34cbSIngo Molnar 
127243ae34cbSIngo Molnar static const struct file_operations proc_pid_sched_operations = {
127343ae34cbSIngo Molnar 	.open		= sched_open,
127443ae34cbSIngo Molnar 	.read		= seq_read,
127543ae34cbSIngo Molnar 	.write		= sched_write,
127643ae34cbSIngo Molnar 	.llseek		= seq_lseek,
12775ea473a1SAlexey Dobriyan 	.release	= single_release,
127843ae34cbSIngo Molnar };
127943ae34cbSIngo Molnar 
128043ae34cbSIngo Molnar #endif
128143ae34cbSIngo Molnar 
12825091faa4SMike Galbraith #ifdef CONFIG_SCHED_AUTOGROUP
12835091faa4SMike Galbraith /*
12845091faa4SMike Galbraith  * Print out autogroup related information:
12855091faa4SMike Galbraith  */
12865091faa4SMike Galbraith static int sched_autogroup_show(struct seq_file *m, void *v)
12875091faa4SMike Galbraith {
12885091faa4SMike Galbraith 	struct inode *inode = m->private;
12895091faa4SMike Galbraith 	struct task_struct *p;
12905091faa4SMike Galbraith 
12915091faa4SMike Galbraith 	p = get_proc_task(inode);
12925091faa4SMike Galbraith 	if (!p)
12935091faa4SMike Galbraith 		return -ESRCH;
12945091faa4SMike Galbraith 	proc_sched_autogroup_show_task(p, m);
12955091faa4SMike Galbraith 
12965091faa4SMike Galbraith 	put_task_struct(p);
12975091faa4SMike Galbraith 
12985091faa4SMike Galbraith 	return 0;
12995091faa4SMike Galbraith }
13005091faa4SMike Galbraith 
13015091faa4SMike Galbraith static ssize_t
13025091faa4SMike Galbraith sched_autogroup_write(struct file *file, const char __user *buf,
13035091faa4SMike Galbraith 	    size_t count, loff_t *offset)
13045091faa4SMike Galbraith {
13055091faa4SMike Galbraith 	struct inode *inode = file->f_path.dentry->d_inode;
13065091faa4SMike Galbraith 	struct task_struct *p;
13075091faa4SMike Galbraith 	char buffer[PROC_NUMBUF];
13080a8cb8e3SAlexey Dobriyan 	int nice;
13095091faa4SMike Galbraith 	int err;
13105091faa4SMike Galbraith 
13115091faa4SMike Galbraith 	memset(buffer, 0, sizeof(buffer));
13125091faa4SMike Galbraith 	if (count > sizeof(buffer) - 1)
13135091faa4SMike Galbraith 		count = sizeof(buffer) - 1;
13145091faa4SMike Galbraith 	if (copy_from_user(buffer, buf, count))
13155091faa4SMike Galbraith 		return -EFAULT;
13165091faa4SMike Galbraith 
13170a8cb8e3SAlexey Dobriyan 	err = kstrtoint(strstrip(buffer), 0, &nice);
13180a8cb8e3SAlexey Dobriyan 	if (err < 0)
13190a8cb8e3SAlexey Dobriyan 		return err;
13205091faa4SMike Galbraith 
13215091faa4SMike Galbraith 	p = get_proc_task(inode);
13225091faa4SMike Galbraith 	if (!p)
13235091faa4SMike Galbraith 		return -ESRCH;
13245091faa4SMike Galbraith 
13252e5b5b3aSHiroshi Shimamoto 	err = proc_sched_autogroup_set_nice(p, nice);
13265091faa4SMike Galbraith 	if (err)
13275091faa4SMike Galbraith 		count = err;
13285091faa4SMike Galbraith 
13295091faa4SMike Galbraith 	put_task_struct(p);
13305091faa4SMike Galbraith 
13315091faa4SMike Galbraith 	return count;
13325091faa4SMike Galbraith }
13335091faa4SMike Galbraith 
13345091faa4SMike Galbraith static int sched_autogroup_open(struct inode *inode, struct file *filp)
13355091faa4SMike Galbraith {
13365091faa4SMike Galbraith 	int ret;
13375091faa4SMike Galbraith 
13385091faa4SMike Galbraith 	ret = single_open(filp, sched_autogroup_show, NULL);
13395091faa4SMike Galbraith 	if (!ret) {
13405091faa4SMike Galbraith 		struct seq_file *m = filp->private_data;
13415091faa4SMike Galbraith 
13425091faa4SMike Galbraith 		m->private = inode;
13435091faa4SMike Galbraith 	}
13445091faa4SMike Galbraith 	return ret;
13455091faa4SMike Galbraith }
13465091faa4SMike Galbraith 
13475091faa4SMike Galbraith static const struct file_operations proc_pid_sched_autogroup_operations = {
13485091faa4SMike Galbraith 	.open		= sched_autogroup_open,
13495091faa4SMike Galbraith 	.read		= seq_read,
13505091faa4SMike Galbraith 	.write		= sched_autogroup_write,
13515091faa4SMike Galbraith 	.llseek		= seq_lseek,
13525091faa4SMike Galbraith 	.release	= single_release,
13535091faa4SMike Galbraith };
13545091faa4SMike Galbraith 
13555091faa4SMike Galbraith #endif /* CONFIG_SCHED_AUTOGROUP */
13565091faa4SMike Galbraith 
13574614a696Sjohn stultz static ssize_t comm_write(struct file *file, const char __user *buf,
13584614a696Sjohn stultz 				size_t count, loff_t *offset)
13594614a696Sjohn stultz {
13604614a696Sjohn stultz 	struct inode *inode = file->f_path.dentry->d_inode;
13614614a696Sjohn stultz 	struct task_struct *p;
13624614a696Sjohn stultz 	char buffer[TASK_COMM_LEN];
13634614a696Sjohn stultz 
13644614a696Sjohn stultz 	memset(buffer, 0, sizeof(buffer));
13654614a696Sjohn stultz 	if (count > sizeof(buffer) - 1)
13664614a696Sjohn stultz 		count = sizeof(buffer) - 1;
13674614a696Sjohn stultz 	if (copy_from_user(buffer, buf, count))
13684614a696Sjohn stultz 		return -EFAULT;
13694614a696Sjohn stultz 
13704614a696Sjohn stultz 	p = get_proc_task(inode);
13714614a696Sjohn stultz 	if (!p)
13724614a696Sjohn stultz 		return -ESRCH;
13734614a696Sjohn stultz 
13744614a696Sjohn stultz 	if (same_thread_group(current, p))
13754614a696Sjohn stultz 		set_task_comm(p, buffer);
13764614a696Sjohn stultz 	else
13774614a696Sjohn stultz 		count = -EINVAL;
13784614a696Sjohn stultz 
13794614a696Sjohn stultz 	put_task_struct(p);
13804614a696Sjohn stultz 
13814614a696Sjohn stultz 	return count;
13824614a696Sjohn stultz }
13834614a696Sjohn stultz 
13844614a696Sjohn stultz static int comm_show(struct seq_file *m, void *v)
13854614a696Sjohn stultz {
13864614a696Sjohn stultz 	struct inode *inode = m->private;
13874614a696Sjohn stultz 	struct task_struct *p;
13884614a696Sjohn stultz 
13894614a696Sjohn stultz 	p = get_proc_task(inode);
13904614a696Sjohn stultz 	if (!p)
13914614a696Sjohn stultz 		return -ESRCH;
13924614a696Sjohn stultz 
13934614a696Sjohn stultz 	task_lock(p);
13944614a696Sjohn stultz 	seq_printf(m, "%s\n", p->comm);
13954614a696Sjohn stultz 	task_unlock(p);
13964614a696Sjohn stultz 
13974614a696Sjohn stultz 	put_task_struct(p);
13984614a696Sjohn stultz 
13994614a696Sjohn stultz 	return 0;
14004614a696Sjohn stultz }
14014614a696Sjohn stultz 
14024614a696Sjohn stultz static int comm_open(struct inode *inode, struct file *filp)
14034614a696Sjohn stultz {
1404c6a34058SJovi Zhang 	return single_open(filp, comm_show, inode);
14054614a696Sjohn stultz }
14064614a696Sjohn stultz 
14074614a696Sjohn stultz static const struct file_operations proc_pid_set_comm_operations = {
14084614a696Sjohn stultz 	.open		= comm_open,
14094614a696Sjohn stultz 	.read		= seq_read,
14104614a696Sjohn stultz 	.write		= comm_write,
14114614a696Sjohn stultz 	.llseek		= seq_lseek,
14124614a696Sjohn stultz 	.release	= single_release,
14134614a696Sjohn stultz };
14144614a696Sjohn stultz 
14157773fbc5SCyrill Gorcunov static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
1416925d1c40SMatt Helsley {
1417925d1c40SMatt Helsley 	struct task_struct *task;
1418925d1c40SMatt Helsley 	struct mm_struct *mm;
1419925d1c40SMatt Helsley 	struct file *exe_file;
1420925d1c40SMatt Helsley 
14217773fbc5SCyrill Gorcunov 	task = get_proc_task(dentry->d_inode);
1422925d1c40SMatt Helsley 	if (!task)
1423925d1c40SMatt Helsley 		return -ENOENT;
1424925d1c40SMatt Helsley 	mm = get_task_mm(task);
1425925d1c40SMatt Helsley 	put_task_struct(task);
1426925d1c40SMatt Helsley 	if (!mm)
1427925d1c40SMatt Helsley 		return -ENOENT;
1428925d1c40SMatt Helsley 	exe_file = get_mm_exe_file(mm);
1429925d1c40SMatt Helsley 	mmput(mm);
1430925d1c40SMatt Helsley 	if (exe_file) {
1431925d1c40SMatt Helsley 		*exe_path = exe_file->f_path;
1432925d1c40SMatt Helsley 		path_get(&exe_file->f_path);
1433925d1c40SMatt Helsley 		fput(exe_file);
1434925d1c40SMatt Helsley 		return 0;
1435925d1c40SMatt Helsley 	} else
1436925d1c40SMatt Helsley 		return -ENOENT;
1437925d1c40SMatt Helsley }
1438925d1c40SMatt Helsley 
1439008b150aSAl Viro static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
14401da177e4SLinus Torvalds {
14411da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
1442408ef013SChristoph Hellwig 	struct path path;
14431da177e4SLinus Torvalds 	int error = -EACCES;
14441da177e4SLinus Torvalds 
1445778c1144SEric W. Biederman 	/* Are we allowed to snoop on the tasks file descriptors? */
1446778c1144SEric W. Biederman 	if (!proc_fd_access_allowed(inode))
14471da177e4SLinus Torvalds 		goto out;
14481da177e4SLinus Torvalds 
1449408ef013SChristoph Hellwig 	error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1450408ef013SChristoph Hellwig 	if (error)
1451408ef013SChristoph Hellwig 		goto out;
1452408ef013SChristoph Hellwig 
1453b5fb63c1SChristoph Hellwig 	nd_jump_link(nd, &path);
1454408ef013SChristoph Hellwig 	return NULL;
14551da177e4SLinus Torvalds out:
1456008b150aSAl Viro 	return ERR_PTR(error);
14571da177e4SLinus Torvalds }
14581da177e4SLinus Torvalds 
14593dcd25f3SJan Blunck static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
14601da177e4SLinus Torvalds {
1461e12ba74dSMel Gorman 	char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
14623dcd25f3SJan Blunck 	char *pathname;
14631da177e4SLinus Torvalds 	int len;
14641da177e4SLinus Torvalds 
14651da177e4SLinus Torvalds 	if (!tmp)
14661da177e4SLinus Torvalds 		return -ENOMEM;
14671da177e4SLinus Torvalds 
14687b2a69baSEric W. Biederman 	pathname = d_path(path, tmp, PAGE_SIZE);
14693dcd25f3SJan Blunck 	len = PTR_ERR(pathname);
14703dcd25f3SJan Blunck 	if (IS_ERR(pathname))
14711da177e4SLinus Torvalds 		goto out;
14723dcd25f3SJan Blunck 	len = tmp + PAGE_SIZE - 1 - pathname;
14731da177e4SLinus Torvalds 
14741da177e4SLinus Torvalds 	if (len > buflen)
14751da177e4SLinus Torvalds 		len = buflen;
14763dcd25f3SJan Blunck 	if (copy_to_user(buffer, pathname, len))
14771da177e4SLinus Torvalds 		len = -EFAULT;
14781da177e4SLinus Torvalds  out:
14791da177e4SLinus Torvalds 	free_page((unsigned long)tmp);
14801da177e4SLinus Torvalds 	return len;
14811da177e4SLinus Torvalds }
14821da177e4SLinus Torvalds 
14831da177e4SLinus Torvalds static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
14841da177e4SLinus Torvalds {
14851da177e4SLinus Torvalds 	int error = -EACCES;
14861da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
14873dcd25f3SJan Blunck 	struct path path;
14881da177e4SLinus Torvalds 
1489778c1144SEric W. Biederman 	/* Are we allowed to snoop on the tasks file descriptors? */
1490778c1144SEric W. Biederman 	if (!proc_fd_access_allowed(inode))
14911da177e4SLinus Torvalds 		goto out;
14921da177e4SLinus Torvalds 
14937773fbc5SCyrill Gorcunov 	error = PROC_I(inode)->op.proc_get_link(dentry, &path);
14941da177e4SLinus Torvalds 	if (error)
14951da177e4SLinus Torvalds 		goto out;
14961da177e4SLinus Torvalds 
14973dcd25f3SJan Blunck 	error = do_proc_readlink(&path, buffer, buflen);
14983dcd25f3SJan Blunck 	path_put(&path);
14991da177e4SLinus Torvalds out:
15001da177e4SLinus Torvalds 	return error;
15011da177e4SLinus Torvalds }
15021da177e4SLinus Torvalds 
1503c5ef1c42SArjan van de Ven static const struct inode_operations proc_pid_link_inode_operations = {
15041da177e4SLinus Torvalds 	.readlink	= proc_pid_readlink,
15056d76fa58SLinus Torvalds 	.follow_link	= proc_pid_follow_link,
15066d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
15071da177e4SLinus Torvalds };
15081da177e4SLinus Torvalds 
150928a6d671SEric W. Biederman 
151028a6d671SEric W. Biederman /* building an inode */
151128a6d671SEric W. Biederman 
151228a6d671SEric W. Biederman static int task_dumpable(struct task_struct *task)
151328a6d671SEric W. Biederman {
151428a6d671SEric W. Biederman 	int dumpable = 0;
151528a6d671SEric W. Biederman 	struct mm_struct *mm;
151628a6d671SEric W. Biederman 
151728a6d671SEric W. Biederman 	task_lock(task);
151828a6d671SEric W. Biederman 	mm = task->mm;
151928a6d671SEric W. Biederman 	if (mm)
15206c5d5238SKawai, Hidehiro 		dumpable = get_dumpable(mm);
152128a6d671SEric W. Biederman 	task_unlock(task);
152228a6d671SEric W. Biederman 	if(dumpable == 1)
152328a6d671SEric W. Biederman 		return 1;
152428a6d671SEric W. Biederman 	return 0;
152528a6d671SEric W. Biederman }
152628a6d671SEric W. Biederman 
15276b4e306aSEric W. Biederman struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
152828a6d671SEric W. Biederman {
152928a6d671SEric W. Biederman 	struct inode * inode;
153028a6d671SEric W. Biederman 	struct proc_inode *ei;
1531c69e8d9cSDavid Howells 	const struct cred *cred;
153228a6d671SEric W. Biederman 
153328a6d671SEric W. Biederman 	/* We need a new inode */
153428a6d671SEric W. Biederman 
153528a6d671SEric W. Biederman 	inode = new_inode(sb);
153628a6d671SEric W. Biederman 	if (!inode)
153728a6d671SEric W. Biederman 		goto out;
153828a6d671SEric W. Biederman 
153928a6d671SEric W. Biederman 	/* Common stuff */
154028a6d671SEric W. Biederman 	ei = PROC_I(inode);
154185fe4025SChristoph Hellwig 	inode->i_ino = get_next_ino();
154228a6d671SEric W. Biederman 	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
154328a6d671SEric W. Biederman 	inode->i_op = &proc_def_inode_operations;
154428a6d671SEric W. Biederman 
154528a6d671SEric W. Biederman 	/*
154628a6d671SEric W. Biederman 	 * grab the reference to task.
154728a6d671SEric W. Biederman 	 */
15481a657f78SOleg Nesterov 	ei->pid = get_task_pid(task, PIDTYPE_PID);
154928a6d671SEric W. Biederman 	if (!ei->pid)
155028a6d671SEric W. Biederman 		goto out_unlock;
155128a6d671SEric W. Biederman 
155228a6d671SEric W. Biederman 	if (task_dumpable(task)) {
1553c69e8d9cSDavid Howells 		rcu_read_lock();
1554c69e8d9cSDavid Howells 		cred = __task_cred(task);
1555c69e8d9cSDavid Howells 		inode->i_uid = cred->euid;
1556c69e8d9cSDavid Howells 		inode->i_gid = cred->egid;
1557c69e8d9cSDavid Howells 		rcu_read_unlock();
155828a6d671SEric W. Biederman 	}
155928a6d671SEric W. Biederman 	security_task_to_inode(task, inode);
156028a6d671SEric W. Biederman 
156128a6d671SEric W. Biederman out:
156228a6d671SEric W. Biederman 	return inode;
156328a6d671SEric W. Biederman 
156428a6d671SEric W. Biederman out_unlock:
156528a6d671SEric W. Biederman 	iput(inode);
156628a6d671SEric W. Biederman 	return NULL;
156728a6d671SEric W. Biederman }
156828a6d671SEric W. Biederman 
15696b4e306aSEric W. Biederman int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
157028a6d671SEric W. Biederman {
157128a6d671SEric W. Biederman 	struct inode *inode = dentry->d_inode;
157228a6d671SEric W. Biederman 	struct task_struct *task;
1573c69e8d9cSDavid Howells 	const struct cred *cred;
15740499680aSVasiliy Kulikov 	struct pid_namespace *pid = dentry->d_sb->s_fs_info;
1575c69e8d9cSDavid Howells 
157628a6d671SEric W. Biederman 	generic_fillattr(inode, stat);
157728a6d671SEric W. Biederman 
157828a6d671SEric W. Biederman 	rcu_read_lock();
1579dcb0f222SEric W. Biederman 	stat->uid = GLOBAL_ROOT_UID;
1580dcb0f222SEric W. Biederman 	stat->gid = GLOBAL_ROOT_GID;
158128a6d671SEric W. Biederman 	task = pid_task(proc_pid(inode), PIDTYPE_PID);
158228a6d671SEric W. Biederman 	if (task) {
15830499680aSVasiliy Kulikov 		if (!has_pid_permissions(pid, task, 2)) {
15840499680aSVasiliy Kulikov 			rcu_read_unlock();
15850499680aSVasiliy Kulikov 			/*
15860499680aSVasiliy Kulikov 			 * This doesn't prevent learning whether PID exists,
15870499680aSVasiliy Kulikov 			 * it only makes getattr() consistent with readdir().
15880499680aSVasiliy Kulikov 			 */
15890499680aSVasiliy Kulikov 			return -ENOENT;
15900499680aSVasiliy Kulikov 		}
159128a6d671SEric W. Biederman 		if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
159228a6d671SEric W. Biederman 		    task_dumpable(task)) {
1593c69e8d9cSDavid Howells 			cred = __task_cred(task);
1594c69e8d9cSDavid Howells 			stat->uid = cred->euid;
1595c69e8d9cSDavid Howells 			stat->gid = cred->egid;
159628a6d671SEric W. Biederman 		}
159728a6d671SEric W. Biederman 	}
159828a6d671SEric W. Biederman 	rcu_read_unlock();
159928a6d671SEric W. Biederman 	return 0;
160028a6d671SEric W. Biederman }
160128a6d671SEric W. Biederman 
160228a6d671SEric W. Biederman /* dentry stuff */
160328a6d671SEric W. Biederman 
160428a6d671SEric W. Biederman /*
160528a6d671SEric W. Biederman  *	Exceptional case: normally we are not allowed to unhash a busy
160628a6d671SEric W. Biederman  * directory. In this case, however, we can do it - no aliasing problems
160728a6d671SEric W. Biederman  * due to the way we treat inodes.
160828a6d671SEric W. Biederman  *
160928a6d671SEric W. Biederman  * Rewrite the inode's ownerships here because the owning task may have
161028a6d671SEric W. Biederman  * performed a setuid(), etc.
161128a6d671SEric W. Biederman  *
161228a6d671SEric W. Biederman  * Before the /proc/pid/status file was created the only way to read
161328a6d671SEric W. Biederman  * the effective uid of a /process was to stat /proc/pid.  Reading
161428a6d671SEric W. Biederman  * /proc/pid/status is slow enough that procps and other packages
161528a6d671SEric W. Biederman  * kept stating /proc/pid.  To keep the rules in /proc simple I have
161628a6d671SEric W. Biederman  * made this apply to all per process world readable and executable
161728a6d671SEric W. Biederman  * directories.
161828a6d671SEric W. Biederman  */
16190b728e19SAl Viro int pid_revalidate(struct dentry *dentry, unsigned int flags)
162028a6d671SEric W. Biederman {
162134286d66SNick Piggin 	struct inode *inode;
162234286d66SNick Piggin 	struct task_struct *task;
1623c69e8d9cSDavid Howells 	const struct cred *cred;
1624c69e8d9cSDavid Howells 
16250b728e19SAl Viro 	if (flags & LOOKUP_RCU)
162634286d66SNick Piggin 		return -ECHILD;
162734286d66SNick Piggin 
162834286d66SNick Piggin 	inode = dentry->d_inode;
162934286d66SNick Piggin 	task = get_proc_task(inode);
163034286d66SNick Piggin 
163128a6d671SEric W. Biederman 	if (task) {
163228a6d671SEric W. Biederman 		if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
163328a6d671SEric W. Biederman 		    task_dumpable(task)) {
1634c69e8d9cSDavid Howells 			rcu_read_lock();
1635c69e8d9cSDavid Howells 			cred = __task_cred(task);
1636c69e8d9cSDavid Howells 			inode->i_uid = cred->euid;
1637c69e8d9cSDavid Howells 			inode->i_gid = cred->egid;
1638c69e8d9cSDavid Howells 			rcu_read_unlock();
163928a6d671SEric W. Biederman 		} else {
1640dcb0f222SEric W. Biederman 			inode->i_uid = GLOBAL_ROOT_UID;
1641dcb0f222SEric W. Biederman 			inode->i_gid = GLOBAL_ROOT_GID;
164228a6d671SEric W. Biederman 		}
164328a6d671SEric W. Biederman 		inode->i_mode &= ~(S_ISUID | S_ISGID);
164428a6d671SEric W. Biederman 		security_task_to_inode(task, inode);
164528a6d671SEric W. Biederman 		put_task_struct(task);
164628a6d671SEric W. Biederman 		return 1;
164728a6d671SEric W. Biederman 	}
164828a6d671SEric W. Biederman 	d_drop(dentry);
164928a6d671SEric W. Biederman 	return 0;
165028a6d671SEric W. Biederman }
165128a6d671SEric W. Biederman 
1652fe15ce44SNick Piggin static int pid_delete_dentry(const struct dentry * dentry)
165328a6d671SEric W. Biederman {
165428a6d671SEric W. Biederman 	/* Is the task we represent dead?
165528a6d671SEric W. Biederman 	 * If so, then don't put the dentry on the lru list,
165628a6d671SEric W. Biederman 	 * kill it immediately.
165728a6d671SEric W. Biederman 	 */
165828a6d671SEric W. Biederman 	return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
165928a6d671SEric W. Biederman }
166028a6d671SEric W. Biederman 
16616b4e306aSEric W. Biederman const struct dentry_operations pid_dentry_operations =
166228a6d671SEric W. Biederman {
166328a6d671SEric W. Biederman 	.d_revalidate	= pid_revalidate,
166428a6d671SEric W. Biederman 	.d_delete	= pid_delete_dentry,
166528a6d671SEric W. Biederman };
166628a6d671SEric W. Biederman 
166728a6d671SEric W. Biederman /* Lookups */
166828a6d671SEric W. Biederman 
16691c0d04c9SEric W. Biederman /*
16701c0d04c9SEric W. Biederman  * Fill a directory entry.
16711c0d04c9SEric W. Biederman  *
16721c0d04c9SEric W. Biederman  * If possible create the dcache entry and derive our inode number and
16731c0d04c9SEric W. Biederman  * file type from dcache entry.
16741c0d04c9SEric W. Biederman  *
16751c0d04c9SEric W. Biederman  * Since all of the proc inode numbers are dynamically generated, the inode
16761c0d04c9SEric W. Biederman  * numbers do not exist until the inode is cache.  This means creating the
16771c0d04c9SEric W. Biederman  * the dcache entry in readdir is necessary to keep the inode numbers
16781c0d04c9SEric W. Biederman  * reported by readdir in sync with the inode numbers reported
16791c0d04c9SEric W. Biederman  * by stat.
16801c0d04c9SEric W. Biederman  */
16816b4e306aSEric W. Biederman int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
16826b4e306aSEric W. Biederman 	const char *name, int len,
1683c5141e6dSEric Dumazet 	instantiate_t instantiate, struct task_struct *task, const void *ptr)
168461a28784SEric W. Biederman {
16852fddfeefSJosef "Jeff" Sipek 	struct dentry *child, *dir = filp->f_path.dentry;
168661a28784SEric W. Biederman 	struct inode *inode;
168761a28784SEric W. Biederman 	struct qstr qname;
168861a28784SEric W. Biederman 	ino_t ino = 0;
168961a28784SEric W. Biederman 	unsigned type = DT_UNKNOWN;
169061a28784SEric W. Biederman 
169161a28784SEric W. Biederman 	qname.name = name;
169261a28784SEric W. Biederman 	qname.len  = len;
169361a28784SEric W. Biederman 	qname.hash = full_name_hash(name, len);
169461a28784SEric W. Biederman 
169561a28784SEric W. Biederman 	child = d_lookup(dir, &qname);
169661a28784SEric W. Biederman 	if (!child) {
169761a28784SEric W. Biederman 		struct dentry *new;
169861a28784SEric W. Biederman 		new = d_alloc(dir, &qname);
169961a28784SEric W. Biederman 		if (new) {
170061a28784SEric W. Biederman 			child = instantiate(dir->d_inode, new, task, ptr);
170161a28784SEric W. Biederman 			if (child)
170261a28784SEric W. Biederman 				dput(new);
170361a28784SEric W. Biederman 			else
170461a28784SEric W. Biederman 				child = new;
170561a28784SEric W. Biederman 		}
170661a28784SEric W. Biederman 	}
170761a28784SEric W. Biederman 	if (!child || IS_ERR(child) || !child->d_inode)
170861a28784SEric W. Biederman 		goto end_instantiate;
170961a28784SEric W. Biederman 	inode = child->d_inode;
171061a28784SEric W. Biederman 	if (inode) {
171161a28784SEric W. Biederman 		ino = inode->i_ino;
171261a28784SEric W. Biederman 		type = inode->i_mode >> 12;
171361a28784SEric W. Biederman 	}
171461a28784SEric W. Biederman 	dput(child);
171561a28784SEric W. Biederman end_instantiate:
171661a28784SEric W. Biederman 	if (!ino)
171761a28784SEric W. Biederman 		ino = find_inode_number(dir, &qname);
171861a28784SEric W. Biederman 	if (!ino)
171961a28784SEric W. Biederman 		ino = 1;
172061a28784SEric W. Biederman 	return filldir(dirent, name, len, filp->f_pos, ino, type);
172161a28784SEric W. Biederman }
172261a28784SEric W. Biederman 
172328a6d671SEric W. Biederman static unsigned name_to_int(struct dentry *dentry)
172428a6d671SEric W. Biederman {
172528a6d671SEric W. Biederman 	const char *name = dentry->d_name.name;
172628a6d671SEric W. Biederman 	int len = dentry->d_name.len;
172728a6d671SEric W. Biederman 	unsigned n = 0;
172828a6d671SEric W. Biederman 
172928a6d671SEric W. Biederman 	if (len > 1 && *name == '0')
173028a6d671SEric W. Biederman 		goto out;
173128a6d671SEric W. Biederman 	while (len-- > 0) {
173228a6d671SEric W. Biederman 		unsigned c = *name++ - '0';
173328a6d671SEric W. Biederman 		if (c > 9)
173428a6d671SEric W. Biederman 			goto out;
173528a6d671SEric W. Biederman 		if (n >= (~0U-9)/10)
173628a6d671SEric W. Biederman 			goto out;
173728a6d671SEric W. Biederman 		n *= 10;
173828a6d671SEric W. Biederman 		n += c;
173928a6d671SEric W. Biederman 	}
174028a6d671SEric W. Biederman 	return n;
174128a6d671SEric W. Biederman out:
174228a6d671SEric W. Biederman 	return ~0U;
174328a6d671SEric W. Biederman }
174428a6d671SEric W. Biederman 
174527932742SMiklos Szeredi #define PROC_FDINFO_MAX 64
174627932742SMiklos Szeredi 
17473dcd25f3SJan Blunck static int proc_fd_info(struct inode *inode, struct path *path, char *info)
174828a6d671SEric W. Biederman {
17495e442a49SLinus Torvalds 	struct task_struct *task = get_proc_task(inode);
17505e442a49SLinus Torvalds 	struct files_struct *files = NULL;
175128a6d671SEric W. Biederman 	struct file *file;
175228a6d671SEric W. Biederman 	int fd = proc_fd(inode);
175328a6d671SEric W. Biederman 
17545e442a49SLinus Torvalds 	if (task) {
175528a6d671SEric W. Biederman 		files = get_files_struct(task);
17565e442a49SLinus Torvalds 		put_task_struct(task);
17575e442a49SLinus Torvalds 	}
17585e442a49SLinus Torvalds 	if (files) {
175928a6d671SEric W. Biederman 		/*
176028a6d671SEric W. Biederman 		 * We are not taking a ref to the file structure, so we must
176128a6d671SEric W. Biederman 		 * hold ->file_lock.
176228a6d671SEric W. Biederman 		 */
176328a6d671SEric W. Biederman 		spin_lock(&files->file_lock);
176428a6d671SEric W. Biederman 		file = fcheck_files(files, fd);
176528a6d671SEric W. Biederman 		if (file) {
17661117f72eSLinus Torvalds 			unsigned int f_flags;
17671117f72eSLinus Torvalds 			struct fdtable *fdt;
17681117f72eSLinus Torvalds 
17691117f72eSLinus Torvalds 			fdt = files_fdtable(files);
17701117f72eSLinus Torvalds 			f_flags = file->f_flags & ~O_CLOEXEC;
17711dce27c5SDavid Howells 			if (close_on_exec(fd, fdt))
17721117f72eSLinus Torvalds 				f_flags |= O_CLOEXEC;
17731117f72eSLinus Torvalds 
17743dcd25f3SJan Blunck 			if (path) {
17753dcd25f3SJan Blunck 				*path = file->f_path;
17763dcd25f3SJan Blunck 				path_get(&file->f_path);
17773dcd25f3SJan Blunck 			}
177827932742SMiklos Szeredi 			if (info)
177927932742SMiklos Szeredi 				snprintf(info, PROC_FDINFO_MAX,
178027932742SMiklos Szeredi 					 "pos:\t%lli\n"
178127932742SMiklos Szeredi 					 "flags:\t0%o\n",
178227932742SMiklos Szeredi 					 (long long) file->f_pos,
17831117f72eSLinus Torvalds 					 f_flags);
178428a6d671SEric W. Biederman 			spin_unlock(&files->file_lock);
178528a6d671SEric W. Biederman 			put_files_struct(files);
17865e442a49SLinus Torvalds 			return 0;
17875e442a49SLinus Torvalds 		}
17885e442a49SLinus Torvalds 		spin_unlock(&files->file_lock);
17895e442a49SLinus Torvalds 		put_files_struct(files);
17905e442a49SLinus Torvalds 	}
17915e442a49SLinus Torvalds 	return -ENOENT;
179228a6d671SEric W. Biederman }
179328a6d671SEric W. Biederman 
17947773fbc5SCyrill Gorcunov static int proc_fd_link(struct dentry *dentry, struct path *path)
179527932742SMiklos Szeredi {
17967773fbc5SCyrill Gorcunov 	return proc_fd_info(dentry->d_inode, path, NULL);
179727932742SMiklos Szeredi }
179827932742SMiklos Szeredi 
17990b728e19SAl Viro static int tid_fd_revalidate(struct dentry *dentry, unsigned int flags)
180028a6d671SEric W. Biederman {
180134286d66SNick Piggin 	struct inode *inode;
180234286d66SNick Piggin 	struct task_struct *task;
180334286d66SNick Piggin 	int fd;
180428a6d671SEric W. Biederman 	struct files_struct *files;
1805c69e8d9cSDavid Howells 	const struct cred *cred;
180628a6d671SEric W. Biederman 
18070b728e19SAl Viro 	if (flags & LOOKUP_RCU)
180834286d66SNick Piggin 		return -ECHILD;
180934286d66SNick Piggin 
181034286d66SNick Piggin 	inode = dentry->d_inode;
181134286d66SNick Piggin 	task = get_proc_task(inode);
181234286d66SNick Piggin 	fd = proc_fd(inode);
181334286d66SNick Piggin 
181428a6d671SEric W. Biederman 	if (task) {
181528a6d671SEric W. Biederman 		files = get_files_struct(task);
181628a6d671SEric W. Biederman 		if (files) {
181730a08bf2SLinus Torvalds 			struct file *file;
181828a6d671SEric W. Biederman 			rcu_read_lock();
181930a08bf2SLinus Torvalds 			file = fcheck_files(files, fd);
182030a08bf2SLinus Torvalds 			if (file) {
18210640113bSLinus Torvalds 				unsigned f_mode = file->f_mode;
182230a08bf2SLinus Torvalds 
182328a6d671SEric W. Biederman 				rcu_read_unlock();
182428a6d671SEric W. Biederman 				put_files_struct(files);
182530a08bf2SLinus Torvalds 
182628a6d671SEric W. Biederman 				if (task_dumpable(task)) {
1827c69e8d9cSDavid Howells 					rcu_read_lock();
1828c69e8d9cSDavid Howells 					cred = __task_cred(task);
1829c69e8d9cSDavid Howells 					inode->i_uid = cred->euid;
1830c69e8d9cSDavid Howells 					inode->i_gid = cred->egid;
1831c69e8d9cSDavid Howells 					rcu_read_unlock();
183228a6d671SEric W. Biederman 				} else {
1833dcb0f222SEric W. Biederman 					inode->i_uid = GLOBAL_ROOT_UID;
1834dcb0f222SEric W. Biederman 					inode->i_gid = GLOBAL_ROOT_GID;
183528a6d671SEric W. Biederman 				}
183630a08bf2SLinus Torvalds 
18370640113bSLinus Torvalds 				if (S_ISLNK(inode->i_mode)) {
18380640113bSLinus Torvalds 					unsigned i_mode = S_IFLNK;
183930a08bf2SLinus Torvalds 					if (f_mode & FMODE_READ)
184030a08bf2SLinus Torvalds 						i_mode |= S_IRUSR | S_IXUSR;
184130a08bf2SLinus Torvalds 					if (f_mode & FMODE_WRITE)
184230a08bf2SLinus Torvalds 						i_mode |= S_IWUSR | S_IXUSR;
184330a08bf2SLinus Torvalds 					inode->i_mode = i_mode;
18440640113bSLinus Torvalds 				}
184530a08bf2SLinus Torvalds 
184628a6d671SEric W. Biederman 				security_task_to_inode(task, inode);
184728a6d671SEric W. Biederman 				put_task_struct(task);
184828a6d671SEric W. Biederman 				return 1;
184928a6d671SEric W. Biederman 			}
185028a6d671SEric W. Biederman 			rcu_read_unlock();
185128a6d671SEric W. Biederman 			put_files_struct(files);
185228a6d671SEric W. Biederman 		}
185328a6d671SEric W. Biederman 		put_task_struct(task);
185428a6d671SEric W. Biederman 	}
185528a6d671SEric W. Biederman 	d_drop(dentry);
185628a6d671SEric W. Biederman 	return 0;
185728a6d671SEric W. Biederman }
185828a6d671SEric W. Biederman 
1859d72f71ebSAl Viro static const struct dentry_operations tid_fd_dentry_operations =
186028a6d671SEric W. Biederman {
186128a6d671SEric W. Biederman 	.d_revalidate	= tid_fd_revalidate,
186228a6d671SEric W. Biederman 	.d_delete	= pid_delete_dentry,
186328a6d671SEric W. Biederman };
186428a6d671SEric W. Biederman 
1865444ceed8SEric W. Biederman static struct dentry *proc_fd_instantiate(struct inode *dir,
1866c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
186728a6d671SEric W. Biederman {
1868af5e6171SAlexey Dobriyan 	unsigned fd = (unsigned long)ptr;
186928a6d671SEric W. Biederman  	struct inode *inode;
187028a6d671SEric W. Biederman  	struct proc_inode *ei;
1871444ceed8SEric W. Biederman 	struct dentry *error = ERR_PTR(-ENOENT);
187228a6d671SEric W. Biederman 
187361a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
187428a6d671SEric W. Biederman 	if (!inode)
187528a6d671SEric W. Biederman 		goto out;
187628a6d671SEric W. Biederman 	ei = PROC_I(inode);
187728a6d671SEric W. Biederman 	ei->fd = fd;
1878444ceed8SEric W. Biederman 
18790640113bSLinus Torvalds 	inode->i_mode = S_IFLNK;
18805e442a49SLinus Torvalds 	inode->i_op = &proc_pid_link_inode_operations;
188128a6d671SEric W. Biederman 	inode->i_size = 64;
188228a6d671SEric W. Biederman 	ei->op.proc_get_link = proc_fd_link;
1883fb045adbSNick Piggin 	d_set_d_op(dentry, &tid_fd_dentry_operations);
188428a6d671SEric W. Biederman 	d_add(dentry, inode);
188528a6d671SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
18860b728e19SAl Viro 	if (tid_fd_revalidate(dentry, 0))
1887444ceed8SEric W. Biederman 		error = NULL;
1888444ceed8SEric W. Biederman 
1889444ceed8SEric W. Biederman  out:
1890444ceed8SEric W. Biederman 	return error;
1891444ceed8SEric W. Biederman }
1892444ceed8SEric W. Biederman 
189327932742SMiklos Szeredi static struct dentry *proc_lookupfd_common(struct inode *dir,
189427932742SMiklos Szeredi 					   struct dentry *dentry,
189527932742SMiklos Szeredi 					   instantiate_t instantiate)
1896444ceed8SEric W. Biederman {
1897444ceed8SEric W. Biederman 	struct task_struct *task = get_proc_task(dir);
1898444ceed8SEric W. Biederman 	unsigned fd = name_to_int(dentry);
1899444ceed8SEric W. Biederman 	struct dentry *result = ERR_PTR(-ENOENT);
1900444ceed8SEric W. Biederman 
1901444ceed8SEric W. Biederman 	if (!task)
1902444ceed8SEric W. Biederman 		goto out_no_task;
1903444ceed8SEric W. Biederman 	if (fd == ~0U)
1904444ceed8SEric W. Biederman 		goto out;
1905444ceed8SEric W. Biederman 
1906af5e6171SAlexey Dobriyan 	result = instantiate(dir, dentry, task, (void *)(unsigned long)fd);
190728a6d671SEric W. Biederman out:
190828a6d671SEric W. Biederman 	put_task_struct(task);
190928a6d671SEric W. Biederman out_no_task:
191028a6d671SEric W. Biederman 	return result;
191128a6d671SEric W. Biederman }
191228a6d671SEric W. Biederman 
191327932742SMiklos Szeredi static int proc_readfd_common(struct file * filp, void * dirent,
191427932742SMiklos Szeredi 			      filldir_t filldir, instantiate_t instantiate)
19151da177e4SLinus Torvalds {
19162fddfeefSJosef "Jeff" Sipek 	struct dentry *dentry = filp->f_path.dentry;
19175634708bSEric W. Biederman 	struct inode *inode = dentry->d_inode;
191899f89551SEric W. Biederman 	struct task_struct *p = get_proc_task(inode);
1919457c2510SPavel Emelyanov 	unsigned int fd, ino;
19201da177e4SLinus Torvalds 	int retval;
19211da177e4SLinus Torvalds 	struct files_struct * files;
19221da177e4SLinus Torvalds 
19231da177e4SLinus Torvalds 	retval = -ENOENT;
192499f89551SEric W. Biederman 	if (!p)
192599f89551SEric W. Biederman 		goto out_no_task;
19261da177e4SLinus Torvalds 	retval = 0;
19271da177e4SLinus Torvalds 
19281da177e4SLinus Torvalds 	fd = filp->f_pos;
19291da177e4SLinus Torvalds 	switch (fd) {
19301da177e4SLinus Torvalds 		case 0:
19311da177e4SLinus Torvalds 			if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
19325e442a49SLinus Torvalds 				goto out;
19331da177e4SLinus Torvalds 			filp->f_pos++;
19341da177e4SLinus Torvalds 		case 1:
19355634708bSEric W. Biederman 			ino = parent_ino(dentry);
19361da177e4SLinus Torvalds 			if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
19375e442a49SLinus Torvalds 				goto out;
19381da177e4SLinus Torvalds 			filp->f_pos++;
19391da177e4SLinus Torvalds 		default:
19401da177e4SLinus Torvalds 			files = get_files_struct(p);
19411da177e4SLinus Torvalds 			if (!files)
19425e442a49SLinus Torvalds 				goto out;
1943b835996fSDipankar Sarma 			rcu_read_lock();
19441da177e4SLinus Torvalds 			for (fd = filp->f_pos-2;
19459b4f526cSAl Viro 			     fd < files_fdtable(files)->max_fds;
19461da177e4SLinus Torvalds 			     fd++, filp->f_pos++) {
194727932742SMiklos Szeredi 				char name[PROC_NUMBUF];
194827932742SMiklos Szeredi 				int len;
1949f05ed3f1SAlexey Dobriyan 				int rv;
19501da177e4SLinus Torvalds 
19511da177e4SLinus Torvalds 				if (!fcheck_files(files, fd))
19521da177e4SLinus Torvalds 					continue;
1953b835996fSDipankar Sarma 				rcu_read_unlock();
19541da177e4SLinus Torvalds 
195527932742SMiklos Szeredi 				len = snprintf(name, sizeof(name), "%d", fd);
1956f05ed3f1SAlexey Dobriyan 				rv = proc_fill_cache(filp, dirent, filldir,
1957f05ed3f1SAlexey Dobriyan 						     name, len, instantiate, p,
1958af5e6171SAlexey Dobriyan 						     (void *)(unsigned long)fd);
1959f05ed3f1SAlexey Dobriyan 				if (rv < 0)
1960f05ed3f1SAlexey Dobriyan 					goto out_fd_loop;
1961b835996fSDipankar Sarma 				rcu_read_lock();
19621da177e4SLinus Torvalds 			}
1963b835996fSDipankar Sarma 			rcu_read_unlock();
1964f05ed3f1SAlexey Dobriyan out_fd_loop:
19651da177e4SLinus Torvalds 			put_files_struct(files);
19661da177e4SLinus Torvalds 	}
19671da177e4SLinus Torvalds out:
196899f89551SEric W. Biederman 	put_task_struct(p);
196999f89551SEric W. Biederman out_no_task:
19701da177e4SLinus Torvalds 	return retval;
19711da177e4SLinus Torvalds }
19721da177e4SLinus Torvalds 
197327932742SMiklos Szeredi static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
197400cd8dd3SAl Viro 				    unsigned int flags)
197527932742SMiklos Szeredi {
197627932742SMiklos Szeredi 	return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
197727932742SMiklos Szeredi }
197827932742SMiklos Szeredi 
197927932742SMiklos Szeredi static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
198027932742SMiklos Szeredi {
198127932742SMiklos Szeredi 	return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
198227932742SMiklos Szeredi }
198327932742SMiklos Szeredi 
198427932742SMiklos Szeredi static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
198527932742SMiklos Szeredi 				      size_t len, loff_t *ppos)
198627932742SMiklos Szeredi {
198727932742SMiklos Szeredi 	char tmp[PROC_FDINFO_MAX];
19883dcd25f3SJan Blunck 	int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
198927932742SMiklos Szeredi 	if (!err)
199027932742SMiklos Szeredi 		err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
199127932742SMiklos Szeredi 	return err;
199227932742SMiklos Szeredi }
199327932742SMiklos Szeredi 
199427932742SMiklos Szeredi static const struct file_operations proc_fdinfo_file_operations = {
199527932742SMiklos Szeredi 	.open           = nonseekable_open,
199627932742SMiklos Szeredi 	.read		= proc_fdinfo_read,
19976038f373SArnd Bergmann 	.llseek		= no_llseek,
199827932742SMiklos Szeredi };
199927932742SMiklos Szeredi 
200000977a59SArjan van de Ven static const struct file_operations proc_fd_operations = {
20011da177e4SLinus Torvalds 	.read		= generic_read_dir,
20021da177e4SLinus Torvalds 	.readdir	= proc_readfd,
20036038f373SArnd Bergmann 	.llseek		= default_llseek,
20041da177e4SLinus Torvalds };
20051da177e4SLinus Torvalds 
2006640708a2SPavel Emelyanov #ifdef CONFIG_CHECKPOINT_RESTORE
2007640708a2SPavel Emelyanov 
2008640708a2SPavel Emelyanov /*
2009640708a2SPavel Emelyanov  * dname_to_vma_addr - maps a dentry name into two unsigned longs
2010640708a2SPavel Emelyanov  * which represent vma start and end addresses.
2011640708a2SPavel Emelyanov  */
2012640708a2SPavel Emelyanov static int dname_to_vma_addr(struct dentry *dentry,
2013640708a2SPavel Emelyanov 			     unsigned long *start, unsigned long *end)
2014640708a2SPavel Emelyanov {
2015640708a2SPavel Emelyanov 	if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2)
2016640708a2SPavel Emelyanov 		return -EINVAL;
2017640708a2SPavel Emelyanov 
2018640708a2SPavel Emelyanov 	return 0;
2019640708a2SPavel Emelyanov }
2020640708a2SPavel Emelyanov 
20210b728e19SAl Viro static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
2022640708a2SPavel Emelyanov {
2023640708a2SPavel Emelyanov 	unsigned long vm_start, vm_end;
2024640708a2SPavel Emelyanov 	bool exact_vma_exists = false;
2025640708a2SPavel Emelyanov 	struct mm_struct *mm = NULL;
2026640708a2SPavel Emelyanov 	struct task_struct *task;
2027640708a2SPavel Emelyanov 	const struct cred *cred;
2028640708a2SPavel Emelyanov 	struct inode *inode;
2029640708a2SPavel Emelyanov 	int status = 0;
2030640708a2SPavel Emelyanov 
20310b728e19SAl Viro 	if (flags & LOOKUP_RCU)
2032640708a2SPavel Emelyanov 		return -ECHILD;
2033640708a2SPavel Emelyanov 
2034640708a2SPavel Emelyanov 	if (!capable(CAP_SYS_ADMIN)) {
2035640708a2SPavel Emelyanov 		status = -EACCES;
2036640708a2SPavel Emelyanov 		goto out_notask;
2037640708a2SPavel Emelyanov 	}
2038640708a2SPavel Emelyanov 
2039640708a2SPavel Emelyanov 	inode = dentry->d_inode;
2040640708a2SPavel Emelyanov 	task = get_proc_task(inode);
2041640708a2SPavel Emelyanov 	if (!task)
2042640708a2SPavel Emelyanov 		goto out_notask;
2043640708a2SPavel Emelyanov 
20442344bec7SCong Wang 	mm = mm_access(task, PTRACE_MODE_READ);
20452344bec7SCong Wang 	if (IS_ERR_OR_NULL(mm))
2046640708a2SPavel Emelyanov 		goto out;
2047640708a2SPavel Emelyanov 
2048640708a2SPavel Emelyanov 	if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
2049640708a2SPavel Emelyanov 		down_read(&mm->mmap_sem);
2050640708a2SPavel Emelyanov 		exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
2051640708a2SPavel Emelyanov 		up_read(&mm->mmap_sem);
2052640708a2SPavel Emelyanov 	}
2053640708a2SPavel Emelyanov 
2054640708a2SPavel Emelyanov 	mmput(mm);
2055640708a2SPavel Emelyanov 
2056640708a2SPavel Emelyanov 	if (exact_vma_exists) {
2057640708a2SPavel Emelyanov 		if (task_dumpable(task)) {
2058640708a2SPavel Emelyanov 			rcu_read_lock();
2059640708a2SPavel Emelyanov 			cred = __task_cred(task);
2060640708a2SPavel Emelyanov 			inode->i_uid = cred->euid;
2061640708a2SPavel Emelyanov 			inode->i_gid = cred->egid;
2062640708a2SPavel Emelyanov 			rcu_read_unlock();
2063640708a2SPavel Emelyanov 		} else {
2064dcb0f222SEric W. Biederman 			inode->i_uid = GLOBAL_ROOT_UID;
2065dcb0f222SEric W. Biederman 			inode->i_gid = GLOBAL_ROOT_GID;
2066640708a2SPavel Emelyanov 		}
2067640708a2SPavel Emelyanov 		security_task_to_inode(task, inode);
2068640708a2SPavel Emelyanov 		status = 1;
2069640708a2SPavel Emelyanov 	}
2070640708a2SPavel Emelyanov 
2071640708a2SPavel Emelyanov out:
2072640708a2SPavel Emelyanov 	put_task_struct(task);
2073640708a2SPavel Emelyanov 
2074640708a2SPavel Emelyanov out_notask:
2075640708a2SPavel Emelyanov 	if (status <= 0)
2076640708a2SPavel Emelyanov 		d_drop(dentry);
2077640708a2SPavel Emelyanov 
2078640708a2SPavel Emelyanov 	return status;
2079640708a2SPavel Emelyanov }
2080640708a2SPavel Emelyanov 
2081640708a2SPavel Emelyanov static const struct dentry_operations tid_map_files_dentry_operations = {
2082640708a2SPavel Emelyanov 	.d_revalidate	= map_files_d_revalidate,
2083640708a2SPavel Emelyanov 	.d_delete	= pid_delete_dentry,
2084640708a2SPavel Emelyanov };
2085640708a2SPavel Emelyanov 
2086640708a2SPavel Emelyanov static int proc_map_files_get_link(struct dentry *dentry, struct path *path)
2087640708a2SPavel Emelyanov {
2088640708a2SPavel Emelyanov 	unsigned long vm_start, vm_end;
2089640708a2SPavel Emelyanov 	struct vm_area_struct *vma;
2090640708a2SPavel Emelyanov 	struct task_struct *task;
2091640708a2SPavel Emelyanov 	struct mm_struct *mm;
2092640708a2SPavel Emelyanov 	int rc;
2093640708a2SPavel Emelyanov 
2094640708a2SPavel Emelyanov 	rc = -ENOENT;
2095640708a2SPavel Emelyanov 	task = get_proc_task(dentry->d_inode);
2096640708a2SPavel Emelyanov 	if (!task)
2097640708a2SPavel Emelyanov 		goto out;
2098640708a2SPavel Emelyanov 
2099640708a2SPavel Emelyanov 	mm = get_task_mm(task);
2100640708a2SPavel Emelyanov 	put_task_struct(task);
2101640708a2SPavel Emelyanov 	if (!mm)
2102640708a2SPavel Emelyanov 		goto out;
2103640708a2SPavel Emelyanov 
2104640708a2SPavel Emelyanov 	rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
2105640708a2SPavel Emelyanov 	if (rc)
2106640708a2SPavel Emelyanov 		goto out_mmput;
2107640708a2SPavel Emelyanov 
2108640708a2SPavel Emelyanov 	down_read(&mm->mmap_sem);
2109640708a2SPavel Emelyanov 	vma = find_exact_vma(mm, vm_start, vm_end);
2110640708a2SPavel Emelyanov 	if (vma && vma->vm_file) {
2111640708a2SPavel Emelyanov 		*path = vma->vm_file->f_path;
2112640708a2SPavel Emelyanov 		path_get(path);
2113640708a2SPavel Emelyanov 		rc = 0;
2114640708a2SPavel Emelyanov 	}
2115640708a2SPavel Emelyanov 	up_read(&mm->mmap_sem);
2116640708a2SPavel Emelyanov 
2117640708a2SPavel Emelyanov out_mmput:
2118640708a2SPavel Emelyanov 	mmput(mm);
2119640708a2SPavel Emelyanov out:
2120640708a2SPavel Emelyanov 	return rc;
2121640708a2SPavel Emelyanov }
2122640708a2SPavel Emelyanov 
2123640708a2SPavel Emelyanov struct map_files_info {
2124640708a2SPavel Emelyanov 	struct file	*file;
2125640708a2SPavel Emelyanov 	unsigned long	len;
2126640708a2SPavel Emelyanov 	unsigned char	name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
2127640708a2SPavel Emelyanov };
2128640708a2SPavel Emelyanov 
2129640708a2SPavel Emelyanov static struct dentry *
2130640708a2SPavel Emelyanov proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
2131640708a2SPavel Emelyanov 			   struct task_struct *task, const void *ptr)
2132640708a2SPavel Emelyanov {
2133640708a2SPavel Emelyanov 	const struct file *file = ptr;
2134640708a2SPavel Emelyanov 	struct proc_inode *ei;
2135640708a2SPavel Emelyanov 	struct inode *inode;
2136640708a2SPavel Emelyanov 
2137640708a2SPavel Emelyanov 	if (!file)
2138640708a2SPavel Emelyanov 		return ERR_PTR(-ENOENT);
2139640708a2SPavel Emelyanov 
2140640708a2SPavel Emelyanov 	inode = proc_pid_make_inode(dir->i_sb, task);
2141640708a2SPavel Emelyanov 	if (!inode)
2142640708a2SPavel Emelyanov 		return ERR_PTR(-ENOENT);
2143640708a2SPavel Emelyanov 
2144640708a2SPavel Emelyanov 	ei = PROC_I(inode);
2145640708a2SPavel Emelyanov 	ei->op.proc_get_link = proc_map_files_get_link;
2146640708a2SPavel Emelyanov 
2147640708a2SPavel Emelyanov 	inode->i_op = &proc_pid_link_inode_operations;
2148640708a2SPavel Emelyanov 	inode->i_size = 64;
2149640708a2SPavel Emelyanov 	inode->i_mode = S_IFLNK;
2150640708a2SPavel Emelyanov 
2151640708a2SPavel Emelyanov 	if (file->f_mode & FMODE_READ)
2152640708a2SPavel Emelyanov 		inode->i_mode |= S_IRUSR;
2153640708a2SPavel Emelyanov 	if (file->f_mode & FMODE_WRITE)
2154640708a2SPavel Emelyanov 		inode->i_mode |= S_IWUSR;
2155640708a2SPavel Emelyanov 
2156640708a2SPavel Emelyanov 	d_set_d_op(dentry, &tid_map_files_dentry_operations);
2157640708a2SPavel Emelyanov 	d_add(dentry, inode);
2158640708a2SPavel Emelyanov 
2159640708a2SPavel Emelyanov 	return NULL;
2160640708a2SPavel Emelyanov }
2161640708a2SPavel Emelyanov 
2162640708a2SPavel Emelyanov static struct dentry *proc_map_files_lookup(struct inode *dir,
216300cd8dd3SAl Viro 		struct dentry *dentry, unsigned int flags)
2164640708a2SPavel Emelyanov {
2165640708a2SPavel Emelyanov 	unsigned long vm_start, vm_end;
2166640708a2SPavel Emelyanov 	struct vm_area_struct *vma;
2167640708a2SPavel Emelyanov 	struct task_struct *task;
2168640708a2SPavel Emelyanov 	struct dentry *result;
2169640708a2SPavel Emelyanov 	struct mm_struct *mm;
2170640708a2SPavel Emelyanov 
2171640708a2SPavel Emelyanov 	result = ERR_PTR(-EACCES);
2172640708a2SPavel Emelyanov 	if (!capable(CAP_SYS_ADMIN))
2173640708a2SPavel Emelyanov 		goto out;
2174640708a2SPavel Emelyanov 
2175640708a2SPavel Emelyanov 	result = ERR_PTR(-ENOENT);
2176640708a2SPavel Emelyanov 	task = get_proc_task(dir);
2177640708a2SPavel Emelyanov 	if (!task)
2178640708a2SPavel Emelyanov 		goto out;
2179640708a2SPavel Emelyanov 
2180640708a2SPavel Emelyanov 	result = ERR_PTR(-EACCES);
2181eb94cd96SCyrill Gorcunov 	if (!ptrace_may_access(task, PTRACE_MODE_READ))
2182640708a2SPavel Emelyanov 		goto out_put_task;
2183640708a2SPavel Emelyanov 
2184640708a2SPavel Emelyanov 	result = ERR_PTR(-ENOENT);
2185640708a2SPavel Emelyanov 	if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
2186eb94cd96SCyrill Gorcunov 		goto out_put_task;
2187640708a2SPavel Emelyanov 
2188640708a2SPavel Emelyanov 	mm = get_task_mm(task);
2189640708a2SPavel Emelyanov 	if (!mm)
2190eb94cd96SCyrill Gorcunov 		goto out_put_task;
2191640708a2SPavel Emelyanov 
2192640708a2SPavel Emelyanov 	down_read(&mm->mmap_sem);
2193640708a2SPavel Emelyanov 	vma = find_exact_vma(mm, vm_start, vm_end);
2194640708a2SPavel Emelyanov 	if (!vma)
2195640708a2SPavel Emelyanov 		goto out_no_vma;
2196640708a2SPavel Emelyanov 
2197640708a2SPavel Emelyanov 	result = proc_map_files_instantiate(dir, dentry, task, vma->vm_file);
2198640708a2SPavel Emelyanov 
2199640708a2SPavel Emelyanov out_no_vma:
2200640708a2SPavel Emelyanov 	up_read(&mm->mmap_sem);
2201640708a2SPavel Emelyanov 	mmput(mm);
2202640708a2SPavel Emelyanov out_put_task:
2203640708a2SPavel Emelyanov 	put_task_struct(task);
2204640708a2SPavel Emelyanov out:
2205640708a2SPavel Emelyanov 	return result;
2206640708a2SPavel Emelyanov }
2207640708a2SPavel Emelyanov 
2208640708a2SPavel Emelyanov static const struct inode_operations proc_map_files_inode_operations = {
2209640708a2SPavel Emelyanov 	.lookup		= proc_map_files_lookup,
2210640708a2SPavel Emelyanov 	.permission	= proc_fd_permission,
2211640708a2SPavel Emelyanov 	.setattr	= proc_setattr,
2212640708a2SPavel Emelyanov };
2213640708a2SPavel Emelyanov 
2214640708a2SPavel Emelyanov static int
2215640708a2SPavel Emelyanov proc_map_files_readdir(struct file *filp, void *dirent, filldir_t filldir)
2216640708a2SPavel Emelyanov {
2217640708a2SPavel Emelyanov 	struct dentry *dentry = filp->f_path.dentry;
2218640708a2SPavel Emelyanov 	struct inode *inode = dentry->d_inode;
2219640708a2SPavel Emelyanov 	struct vm_area_struct *vma;
2220640708a2SPavel Emelyanov 	struct task_struct *task;
2221640708a2SPavel Emelyanov 	struct mm_struct *mm;
2222640708a2SPavel Emelyanov 	ino_t ino;
2223640708a2SPavel Emelyanov 	int ret;
2224640708a2SPavel Emelyanov 
2225640708a2SPavel Emelyanov 	ret = -EACCES;
2226640708a2SPavel Emelyanov 	if (!capable(CAP_SYS_ADMIN))
2227640708a2SPavel Emelyanov 		goto out;
2228640708a2SPavel Emelyanov 
2229640708a2SPavel Emelyanov 	ret = -ENOENT;
2230640708a2SPavel Emelyanov 	task = get_proc_task(inode);
2231640708a2SPavel Emelyanov 	if (!task)
2232640708a2SPavel Emelyanov 		goto out;
2233640708a2SPavel Emelyanov 
2234640708a2SPavel Emelyanov 	ret = -EACCES;
2235eb94cd96SCyrill Gorcunov 	if (!ptrace_may_access(task, PTRACE_MODE_READ))
2236640708a2SPavel Emelyanov 		goto out_put_task;
2237640708a2SPavel Emelyanov 
2238640708a2SPavel Emelyanov 	ret = 0;
2239640708a2SPavel Emelyanov 	switch (filp->f_pos) {
2240640708a2SPavel Emelyanov 	case 0:
2241640708a2SPavel Emelyanov 		ino = inode->i_ino;
2242640708a2SPavel Emelyanov 		if (filldir(dirent, ".", 1, 0, ino, DT_DIR) < 0)
2243eb94cd96SCyrill Gorcunov 			goto out_put_task;
2244640708a2SPavel Emelyanov 		filp->f_pos++;
2245640708a2SPavel Emelyanov 	case 1:
2246640708a2SPavel Emelyanov 		ino = parent_ino(dentry);
2247640708a2SPavel Emelyanov 		if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
2248eb94cd96SCyrill Gorcunov 			goto out_put_task;
2249640708a2SPavel Emelyanov 		filp->f_pos++;
2250640708a2SPavel Emelyanov 	default:
2251640708a2SPavel Emelyanov 	{
2252640708a2SPavel Emelyanov 		unsigned long nr_files, pos, i;
2253640708a2SPavel Emelyanov 		struct flex_array *fa = NULL;
2254640708a2SPavel Emelyanov 		struct map_files_info info;
2255640708a2SPavel Emelyanov 		struct map_files_info *p;
2256640708a2SPavel Emelyanov 
2257640708a2SPavel Emelyanov 		mm = get_task_mm(task);
2258640708a2SPavel Emelyanov 		if (!mm)
2259eb94cd96SCyrill Gorcunov 			goto out_put_task;
2260640708a2SPavel Emelyanov 		down_read(&mm->mmap_sem);
2261640708a2SPavel Emelyanov 
2262640708a2SPavel Emelyanov 		nr_files = 0;
2263640708a2SPavel Emelyanov 
2264640708a2SPavel Emelyanov 		/*
2265640708a2SPavel Emelyanov 		 * We need two passes here:
2266640708a2SPavel Emelyanov 		 *
2267640708a2SPavel Emelyanov 		 *  1) Collect vmas of mapped files with mmap_sem taken
2268640708a2SPavel Emelyanov 		 *  2) Release mmap_sem and instantiate entries
2269640708a2SPavel Emelyanov 		 *
2270640708a2SPavel Emelyanov 		 * otherwise we get lockdep complained, since filldir()
2271640708a2SPavel Emelyanov 		 * routine might require mmap_sem taken in might_fault().
2272640708a2SPavel Emelyanov 		 */
2273640708a2SPavel Emelyanov 
2274640708a2SPavel Emelyanov 		for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
2275640708a2SPavel Emelyanov 			if (vma->vm_file && ++pos > filp->f_pos)
2276640708a2SPavel Emelyanov 				nr_files++;
2277640708a2SPavel Emelyanov 		}
2278640708a2SPavel Emelyanov 
2279640708a2SPavel Emelyanov 		if (nr_files) {
2280640708a2SPavel Emelyanov 			fa = flex_array_alloc(sizeof(info), nr_files,
2281640708a2SPavel Emelyanov 						GFP_KERNEL);
2282640708a2SPavel Emelyanov 			if (!fa || flex_array_prealloc(fa, 0, nr_files,
2283640708a2SPavel Emelyanov 							GFP_KERNEL)) {
2284640708a2SPavel Emelyanov 				ret = -ENOMEM;
2285640708a2SPavel Emelyanov 				if (fa)
2286640708a2SPavel Emelyanov 					flex_array_free(fa);
2287640708a2SPavel Emelyanov 				up_read(&mm->mmap_sem);
2288640708a2SPavel Emelyanov 				mmput(mm);
2289eb94cd96SCyrill Gorcunov 				goto out_put_task;
2290640708a2SPavel Emelyanov 			}
2291640708a2SPavel Emelyanov 			for (i = 0, vma = mm->mmap, pos = 2; vma;
2292640708a2SPavel Emelyanov 					vma = vma->vm_next) {
2293640708a2SPavel Emelyanov 				if (!vma->vm_file)
2294640708a2SPavel Emelyanov 					continue;
2295640708a2SPavel Emelyanov 				if (++pos <= filp->f_pos)
2296640708a2SPavel Emelyanov 					continue;
2297640708a2SPavel Emelyanov 
2298640708a2SPavel Emelyanov 				get_file(vma->vm_file);
2299640708a2SPavel Emelyanov 				info.file = vma->vm_file;
2300640708a2SPavel Emelyanov 				info.len = snprintf(info.name,
2301640708a2SPavel Emelyanov 						sizeof(info.name), "%lx-%lx",
2302640708a2SPavel Emelyanov 						vma->vm_start, vma->vm_end);
2303640708a2SPavel Emelyanov 				if (flex_array_put(fa, i++, &info, GFP_KERNEL))
2304640708a2SPavel Emelyanov 					BUG();
2305640708a2SPavel Emelyanov 			}
2306640708a2SPavel Emelyanov 		}
2307640708a2SPavel Emelyanov 		up_read(&mm->mmap_sem);
2308640708a2SPavel Emelyanov 
2309640708a2SPavel Emelyanov 		for (i = 0; i < nr_files; i++) {
2310640708a2SPavel Emelyanov 			p = flex_array_get(fa, i);
2311640708a2SPavel Emelyanov 			ret = proc_fill_cache(filp, dirent, filldir,
2312640708a2SPavel Emelyanov 					      p->name, p->len,
2313640708a2SPavel Emelyanov 					      proc_map_files_instantiate,
2314640708a2SPavel Emelyanov 					      task, p->file);
2315640708a2SPavel Emelyanov 			if (ret)
2316640708a2SPavel Emelyanov 				break;
2317640708a2SPavel Emelyanov 			filp->f_pos++;
2318640708a2SPavel Emelyanov 			fput(p->file);
2319640708a2SPavel Emelyanov 		}
2320640708a2SPavel Emelyanov 		for (; i < nr_files; i++) {
2321640708a2SPavel Emelyanov 			/*
2322640708a2SPavel Emelyanov 			 * In case of error don't forget
2323640708a2SPavel Emelyanov 			 * to put rest of file refs.
2324640708a2SPavel Emelyanov 			 */
2325640708a2SPavel Emelyanov 			p = flex_array_get(fa, i);
2326640708a2SPavel Emelyanov 			fput(p->file);
2327640708a2SPavel Emelyanov 		}
2328640708a2SPavel Emelyanov 		if (fa)
2329640708a2SPavel Emelyanov 			flex_array_free(fa);
2330640708a2SPavel Emelyanov 		mmput(mm);
2331640708a2SPavel Emelyanov 	}
2332640708a2SPavel Emelyanov 	}
2333640708a2SPavel Emelyanov 
2334640708a2SPavel Emelyanov out_put_task:
2335640708a2SPavel Emelyanov 	put_task_struct(task);
2336640708a2SPavel Emelyanov out:
2337640708a2SPavel Emelyanov 	return ret;
2338640708a2SPavel Emelyanov }
2339640708a2SPavel Emelyanov 
2340640708a2SPavel Emelyanov static const struct file_operations proc_map_files_operations = {
2341640708a2SPavel Emelyanov 	.read		= generic_read_dir,
2342640708a2SPavel Emelyanov 	.readdir	= proc_map_files_readdir,
2343640708a2SPavel Emelyanov 	.llseek		= default_llseek,
2344640708a2SPavel Emelyanov };
2345640708a2SPavel Emelyanov 
2346640708a2SPavel Emelyanov #endif /* CONFIG_CHECKPOINT_RESTORE */
2347640708a2SPavel Emelyanov 
23481da177e4SLinus Torvalds /*
23498948e11fSAlexey Dobriyan  * /proc/pid/fd needs a special permission handler so that a process can still
23508948e11fSAlexey Dobriyan  * access /proc/self/fd after it has executed a setuid().
23518948e11fSAlexey Dobriyan  */
235210556cb2SAl Viro static int proc_fd_permission(struct inode *inode, int mask)
23538948e11fSAlexey Dobriyan {
23542830ba7fSAl Viro 	int rv = generic_permission(inode, mask);
23558948e11fSAlexey Dobriyan 	if (rv == 0)
23568948e11fSAlexey Dobriyan 		return 0;
23578948e11fSAlexey Dobriyan 	if (task_pid(current) == proc_pid(inode))
23588948e11fSAlexey Dobriyan 		rv = 0;
23598948e11fSAlexey Dobriyan 	return rv;
23608948e11fSAlexey Dobriyan }
23618948e11fSAlexey Dobriyan 
23628948e11fSAlexey Dobriyan /*
23631da177e4SLinus Torvalds  * proc directories can do almost nothing..
23641da177e4SLinus Torvalds  */
2365c5ef1c42SArjan van de Ven static const struct inode_operations proc_fd_inode_operations = {
23661da177e4SLinus Torvalds 	.lookup		= proc_lookupfd,
23678948e11fSAlexey Dobriyan 	.permission	= proc_fd_permission,
23686d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
23691da177e4SLinus Torvalds };
23701da177e4SLinus Torvalds 
237127932742SMiklos Szeredi static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
237227932742SMiklos Szeredi 	struct dentry *dentry, struct task_struct *task, const void *ptr)
237327932742SMiklos Szeredi {
2374af5e6171SAlexey Dobriyan 	unsigned fd = (unsigned long)ptr;
237527932742SMiklos Szeredi  	struct inode *inode;
237627932742SMiklos Szeredi  	struct proc_inode *ei;
237727932742SMiklos Szeredi 	struct dentry *error = ERR_PTR(-ENOENT);
237827932742SMiklos Szeredi 
237927932742SMiklos Szeredi 	inode = proc_pid_make_inode(dir->i_sb, task);
238027932742SMiklos Szeredi 	if (!inode)
238127932742SMiklos Szeredi 		goto out;
238227932742SMiklos Szeredi 	ei = PROC_I(inode);
238327932742SMiklos Szeredi 	ei->fd = fd;
238427932742SMiklos Szeredi 	inode->i_mode = S_IFREG | S_IRUSR;
238527932742SMiklos Szeredi 	inode->i_fop = &proc_fdinfo_file_operations;
2386fb045adbSNick Piggin 	d_set_d_op(dentry, &tid_fd_dentry_operations);
238727932742SMiklos Szeredi 	d_add(dentry, inode);
238827932742SMiklos Szeredi 	/* Close the race of the process dying before we return the dentry */
23890b728e19SAl Viro 	if (tid_fd_revalidate(dentry, 0))
239027932742SMiklos Szeredi 		error = NULL;
239127932742SMiklos Szeredi 
239227932742SMiklos Szeredi  out:
239327932742SMiklos Szeredi 	return error;
239427932742SMiklos Szeredi }
239527932742SMiklos Szeredi 
239627932742SMiklos Szeredi static struct dentry *proc_lookupfdinfo(struct inode *dir,
239727932742SMiklos Szeredi 					struct dentry *dentry,
239800cd8dd3SAl Viro 					unsigned int flags)
239927932742SMiklos Szeredi {
240027932742SMiklos Szeredi 	return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
240127932742SMiklos Szeredi }
240227932742SMiklos Szeredi 
240327932742SMiklos Szeredi static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
240427932742SMiklos Szeredi {
240527932742SMiklos Szeredi 	return proc_readfd_common(filp, dirent, filldir,
240627932742SMiklos Szeredi 				  proc_fdinfo_instantiate);
240727932742SMiklos Szeredi }
240827932742SMiklos Szeredi 
240927932742SMiklos Szeredi static const struct file_operations proc_fdinfo_operations = {
241027932742SMiklos Szeredi 	.read		= generic_read_dir,
241127932742SMiklos Szeredi 	.readdir	= proc_readfdinfo,
24126038f373SArnd Bergmann 	.llseek		= default_llseek,
241327932742SMiklos Szeredi };
241427932742SMiklos Szeredi 
241527932742SMiklos Szeredi /*
241627932742SMiklos Szeredi  * proc directories can do almost nothing..
241727932742SMiklos Szeredi  */
241827932742SMiklos Szeredi static const struct inode_operations proc_fdinfo_inode_operations = {
241927932742SMiklos Szeredi 	.lookup		= proc_lookupfdinfo,
242027932742SMiklos Szeredi 	.setattr	= proc_setattr,
242127932742SMiklos Szeredi };
242227932742SMiklos Szeredi 
242327932742SMiklos Szeredi 
2424444ceed8SEric W. Biederman static struct dentry *proc_pident_instantiate(struct inode *dir,
2425c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
2426444ceed8SEric W. Biederman {
2427c5141e6dSEric Dumazet 	const struct pid_entry *p = ptr;
2428444ceed8SEric W. Biederman 	struct inode *inode;
2429444ceed8SEric W. Biederman 	struct proc_inode *ei;
2430bd6daba9SKOSAKI Motohiro 	struct dentry *error = ERR_PTR(-ENOENT);
2431444ceed8SEric W. Biederman 
243261a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
2433444ceed8SEric W. Biederman 	if (!inode)
2434444ceed8SEric W. Biederman 		goto out;
2435444ceed8SEric W. Biederman 
2436444ceed8SEric W. Biederman 	ei = PROC_I(inode);
2437444ceed8SEric W. Biederman 	inode->i_mode = p->mode;
2438444ceed8SEric W. Biederman 	if (S_ISDIR(inode->i_mode))
2439bfe86848SMiklos Szeredi 		set_nlink(inode, 2);	/* Use getattr to fix if necessary */
2440444ceed8SEric W. Biederman 	if (p->iop)
2441444ceed8SEric W. Biederman 		inode->i_op = p->iop;
2442444ceed8SEric W. Biederman 	if (p->fop)
2443444ceed8SEric W. Biederman 		inode->i_fop = p->fop;
2444444ceed8SEric W. Biederman 	ei->op = p->op;
2445fb045adbSNick Piggin 	d_set_d_op(dentry, &pid_dentry_operations);
2446444ceed8SEric W. Biederman 	d_add(dentry, inode);
2447444ceed8SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
24480b728e19SAl Viro 	if (pid_revalidate(dentry, 0))
2449444ceed8SEric W. Biederman 		error = NULL;
2450444ceed8SEric W. Biederman out:
2451444ceed8SEric W. Biederman 	return error;
2452444ceed8SEric W. Biederman }
2453444ceed8SEric W. Biederman 
24541da177e4SLinus Torvalds static struct dentry *proc_pident_lookup(struct inode *dir,
24551da177e4SLinus Torvalds 					 struct dentry *dentry,
2456c5141e6dSEric Dumazet 					 const struct pid_entry *ents,
24577bcd6b0eSEric W. Biederman 					 unsigned int nents)
24581da177e4SLinus Torvalds {
2459cd6a3ce9SEric W. Biederman 	struct dentry *error;
246099f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(dir);
2461c5141e6dSEric Dumazet 	const struct pid_entry *p, *last;
24621da177e4SLinus Torvalds 
2463cd6a3ce9SEric W. Biederman 	error = ERR_PTR(-ENOENT);
24641da177e4SLinus Torvalds 
246599f89551SEric W. Biederman 	if (!task)
246699f89551SEric W. Biederman 		goto out_no_task;
24671da177e4SLinus Torvalds 
246820cdc894SEric W. Biederman 	/*
246920cdc894SEric W. Biederman 	 * Yes, it does not scale. And it should not. Don't add
247020cdc894SEric W. Biederman 	 * new entries into /proc/<tgid>/ without very good reasons.
247120cdc894SEric W. Biederman 	 */
24727bcd6b0eSEric W. Biederman 	last = &ents[nents - 1];
24737bcd6b0eSEric W. Biederman 	for (p = ents; p <= last; p++) {
24741da177e4SLinus Torvalds 		if (p->len != dentry->d_name.len)
24751da177e4SLinus Torvalds 			continue;
24761da177e4SLinus Torvalds 		if (!memcmp(dentry->d_name.name, p->name, p->len))
24771da177e4SLinus Torvalds 			break;
24781da177e4SLinus Torvalds 	}
24797bcd6b0eSEric W. Biederman 	if (p > last)
24801da177e4SLinus Torvalds 		goto out;
24811da177e4SLinus Torvalds 
2482444ceed8SEric W. Biederman 	error = proc_pident_instantiate(dir, dentry, task, p);
24831da177e4SLinus Torvalds out:
248499f89551SEric W. Biederman 	put_task_struct(task);
248599f89551SEric W. Biederman out_no_task:
2486cd6a3ce9SEric W. Biederman 	return error;
24871da177e4SLinus Torvalds }
24881da177e4SLinus Torvalds 
2489c5141e6dSEric Dumazet static int proc_pident_fill_cache(struct file *filp, void *dirent,
2490c5141e6dSEric Dumazet 	filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
249161a28784SEric W. Biederman {
249261a28784SEric W. Biederman 	return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
249361a28784SEric W. Biederman 				proc_pident_instantiate, task, p);
249461a28784SEric W. Biederman }
249561a28784SEric W. Biederman 
249628a6d671SEric W. Biederman static int proc_pident_readdir(struct file *filp,
249728a6d671SEric W. Biederman 		void *dirent, filldir_t filldir,
2498c5141e6dSEric Dumazet 		const struct pid_entry *ents, unsigned int nents)
249928a6d671SEric W. Biederman {
250028a6d671SEric W. Biederman 	int i;
25012fddfeefSJosef "Jeff" Sipek 	struct dentry *dentry = filp->f_path.dentry;
250228a6d671SEric W. Biederman 	struct inode *inode = dentry->d_inode;
250328a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
2504c5141e6dSEric Dumazet 	const struct pid_entry *p, *last;
250528a6d671SEric W. Biederman 	ino_t ino;
250628a6d671SEric W. Biederman 	int ret;
250728a6d671SEric W. Biederman 
250828a6d671SEric W. Biederman 	ret = -ENOENT;
250928a6d671SEric W. Biederman 	if (!task)
251061a28784SEric W. Biederman 		goto out_no_task;
251128a6d671SEric W. Biederman 
251228a6d671SEric W. Biederman 	ret = 0;
251328a6d671SEric W. Biederman 	i = filp->f_pos;
251428a6d671SEric W. Biederman 	switch (i) {
251528a6d671SEric W. Biederman 	case 0:
251628a6d671SEric W. Biederman 		ino = inode->i_ino;
251728a6d671SEric W. Biederman 		if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
251828a6d671SEric W. Biederman 			goto out;
251928a6d671SEric W. Biederman 		i++;
252028a6d671SEric W. Biederman 		filp->f_pos++;
252128a6d671SEric W. Biederman 		/* fall through */
252228a6d671SEric W. Biederman 	case 1:
252328a6d671SEric W. Biederman 		ino = parent_ino(dentry);
252428a6d671SEric W. Biederman 		if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
252528a6d671SEric W. Biederman 			goto out;
252628a6d671SEric W. Biederman 		i++;
252728a6d671SEric W. Biederman 		filp->f_pos++;
252828a6d671SEric W. Biederman 		/* fall through */
252928a6d671SEric W. Biederman 	default:
253028a6d671SEric W. Biederman 		i -= 2;
253128a6d671SEric W. Biederman 		if (i >= nents) {
253228a6d671SEric W. Biederman 			ret = 1;
253328a6d671SEric W. Biederman 			goto out;
253428a6d671SEric W. Biederman 		}
253528a6d671SEric W. Biederman 		p = ents + i;
25367bcd6b0eSEric W. Biederman 		last = &ents[nents - 1];
25377bcd6b0eSEric W. Biederman 		while (p <= last) {
253861a28784SEric W. Biederman 			if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
253928a6d671SEric W. Biederman 				goto out;
254028a6d671SEric W. Biederman 			filp->f_pos++;
254128a6d671SEric W. Biederman 			p++;
254228a6d671SEric W. Biederman 		}
25431da177e4SLinus Torvalds 	}
25441da177e4SLinus Torvalds 
254528a6d671SEric W. Biederman 	ret = 1;
254628a6d671SEric W. Biederman out:
254761a28784SEric W. Biederman 	put_task_struct(task);
254861a28784SEric W. Biederman out_no_task:
254928a6d671SEric W. Biederman 	return ret;
25501da177e4SLinus Torvalds }
25511da177e4SLinus Torvalds 
25521da177e4SLinus Torvalds #ifdef CONFIG_SECURITY
255328a6d671SEric W. Biederman static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
255428a6d671SEric W. Biederman 				  size_t count, loff_t *ppos)
255528a6d671SEric W. Biederman {
25562fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
255704ff9708SAl Viro 	char *p = NULL;
255828a6d671SEric W. Biederman 	ssize_t length;
255928a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
256028a6d671SEric W. Biederman 
256128a6d671SEric W. Biederman 	if (!task)
256204ff9708SAl Viro 		return -ESRCH;
256328a6d671SEric W. Biederman 
256428a6d671SEric W. Biederman 	length = security_getprocattr(task,
25652fddfeefSJosef "Jeff" Sipek 				      (char*)file->f_path.dentry->d_name.name,
256604ff9708SAl Viro 				      &p);
256728a6d671SEric W. Biederman 	put_task_struct(task);
256804ff9708SAl Viro 	if (length > 0)
256904ff9708SAl Viro 		length = simple_read_from_buffer(buf, count, ppos, p, length);
257004ff9708SAl Viro 	kfree(p);
257128a6d671SEric W. Biederman 	return length;
257228a6d671SEric W. Biederman }
257328a6d671SEric W. Biederman 
257428a6d671SEric W. Biederman static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
257528a6d671SEric W. Biederman 				   size_t count, loff_t *ppos)
257628a6d671SEric W. Biederman {
25772fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
257828a6d671SEric W. Biederman 	char *page;
257928a6d671SEric W. Biederman 	ssize_t length;
258028a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
258128a6d671SEric W. Biederman 
258228a6d671SEric W. Biederman 	length = -ESRCH;
258328a6d671SEric W. Biederman 	if (!task)
258428a6d671SEric W. Biederman 		goto out_no_task;
258528a6d671SEric W. Biederman 	if (count > PAGE_SIZE)
258628a6d671SEric W. Biederman 		count = PAGE_SIZE;
258728a6d671SEric W. Biederman 
258828a6d671SEric W. Biederman 	/* No partial writes. */
258928a6d671SEric W. Biederman 	length = -EINVAL;
259028a6d671SEric W. Biederman 	if (*ppos != 0)
259128a6d671SEric W. Biederman 		goto out;
259228a6d671SEric W. Biederman 
259328a6d671SEric W. Biederman 	length = -ENOMEM;
2594e12ba74dSMel Gorman 	page = (char*)__get_free_page(GFP_TEMPORARY);
259528a6d671SEric W. Biederman 	if (!page)
259628a6d671SEric W. Biederman 		goto out;
259728a6d671SEric W. Biederman 
259828a6d671SEric W. Biederman 	length = -EFAULT;
259928a6d671SEric W. Biederman 	if (copy_from_user(page, buf, count))
260028a6d671SEric W. Biederman 		goto out_free;
260128a6d671SEric W. Biederman 
2602107db7c7SDavid Howells 	/* Guard against adverse ptrace interaction */
26039b1bf12dSKOSAKI Motohiro 	length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
2604107db7c7SDavid Howells 	if (length < 0)
2605107db7c7SDavid Howells 		goto out_free;
2606107db7c7SDavid Howells 
260728a6d671SEric W. Biederman 	length = security_setprocattr(task,
26082fddfeefSJosef "Jeff" Sipek 				      (char*)file->f_path.dentry->d_name.name,
260928a6d671SEric W. Biederman 				      (void*)page, count);
26109b1bf12dSKOSAKI Motohiro 	mutex_unlock(&task->signal->cred_guard_mutex);
261128a6d671SEric W. Biederman out_free:
261228a6d671SEric W. Biederman 	free_page((unsigned long) page);
261328a6d671SEric W. Biederman out:
261428a6d671SEric W. Biederman 	put_task_struct(task);
261528a6d671SEric W. Biederman out_no_task:
261628a6d671SEric W. Biederman 	return length;
261728a6d671SEric W. Biederman }
261828a6d671SEric W. Biederman 
261900977a59SArjan van de Ven static const struct file_operations proc_pid_attr_operations = {
262028a6d671SEric W. Biederman 	.read		= proc_pid_attr_read,
262128a6d671SEric W. Biederman 	.write		= proc_pid_attr_write,
262287df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
262328a6d671SEric W. Biederman };
262428a6d671SEric W. Biederman 
2625c5141e6dSEric Dumazet static const struct pid_entry attr_dir_stuff[] = {
2626631f9c18SAlexey Dobriyan 	REG("current",    S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2627631f9c18SAlexey Dobriyan 	REG("prev",       S_IRUGO,	   proc_pid_attr_operations),
2628631f9c18SAlexey Dobriyan 	REG("exec",       S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2629631f9c18SAlexey Dobriyan 	REG("fscreate",   S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2630631f9c18SAlexey Dobriyan 	REG("keycreate",  S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2631631f9c18SAlexey Dobriyan 	REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
263228a6d671SEric W. Biederman };
263328a6d671SEric W. Biederman 
263472d9dcfcSEric W. Biederman static int proc_attr_dir_readdir(struct file * filp,
26351da177e4SLinus Torvalds 			     void * dirent, filldir_t filldir)
26361da177e4SLinus Torvalds {
26371da177e4SLinus Torvalds 	return proc_pident_readdir(filp,dirent,filldir,
263872d9dcfcSEric W. Biederman 				   attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
26391da177e4SLinus Torvalds }
26401da177e4SLinus Torvalds 
264100977a59SArjan van de Ven static const struct file_operations proc_attr_dir_operations = {
26421da177e4SLinus Torvalds 	.read		= generic_read_dir,
264372d9dcfcSEric W. Biederman 	.readdir	= proc_attr_dir_readdir,
26446038f373SArnd Bergmann 	.llseek		= default_llseek,
26451da177e4SLinus Torvalds };
26461da177e4SLinus Torvalds 
264772d9dcfcSEric W. Biederman static struct dentry *proc_attr_dir_lookup(struct inode *dir,
264800cd8dd3SAl Viro 				struct dentry *dentry, unsigned int flags)
26491da177e4SLinus Torvalds {
26507bcd6b0eSEric W. Biederman 	return proc_pident_lookup(dir, dentry,
26517bcd6b0eSEric W. Biederman 				  attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
26521da177e4SLinus Torvalds }
26531da177e4SLinus Torvalds 
2654c5ef1c42SArjan van de Ven static const struct inode_operations proc_attr_dir_inode_operations = {
265572d9dcfcSEric W. Biederman 	.lookup		= proc_attr_dir_lookup,
265699f89551SEric W. Biederman 	.getattr	= pid_getattr,
26576d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
26581da177e4SLinus Torvalds };
26591da177e4SLinus Torvalds 
26601da177e4SLinus Torvalds #endif
26611da177e4SLinus Torvalds 
2662698ba7b5SChristoph Hellwig #ifdef CONFIG_ELF_CORE
26633cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
26643cb4a0bbSKawai, Hidehiro 					 size_t count, loff_t *ppos)
26653cb4a0bbSKawai, Hidehiro {
26663cb4a0bbSKawai, Hidehiro 	struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
26673cb4a0bbSKawai, Hidehiro 	struct mm_struct *mm;
26683cb4a0bbSKawai, Hidehiro 	char buffer[PROC_NUMBUF];
26693cb4a0bbSKawai, Hidehiro 	size_t len;
26703cb4a0bbSKawai, Hidehiro 	int ret;
26713cb4a0bbSKawai, Hidehiro 
26723cb4a0bbSKawai, Hidehiro 	if (!task)
26733cb4a0bbSKawai, Hidehiro 		return -ESRCH;
26743cb4a0bbSKawai, Hidehiro 
26753cb4a0bbSKawai, Hidehiro 	ret = 0;
26763cb4a0bbSKawai, Hidehiro 	mm = get_task_mm(task);
26773cb4a0bbSKawai, Hidehiro 	if (mm) {
26783cb4a0bbSKawai, Hidehiro 		len = snprintf(buffer, sizeof(buffer), "%08lx\n",
26793cb4a0bbSKawai, Hidehiro 			       ((mm->flags & MMF_DUMP_FILTER_MASK) >>
26803cb4a0bbSKawai, Hidehiro 				MMF_DUMP_FILTER_SHIFT));
26813cb4a0bbSKawai, Hidehiro 		mmput(mm);
26823cb4a0bbSKawai, Hidehiro 		ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
26833cb4a0bbSKawai, Hidehiro 	}
26843cb4a0bbSKawai, Hidehiro 
26853cb4a0bbSKawai, Hidehiro 	put_task_struct(task);
26863cb4a0bbSKawai, Hidehiro 
26873cb4a0bbSKawai, Hidehiro 	return ret;
26883cb4a0bbSKawai, Hidehiro }
26893cb4a0bbSKawai, Hidehiro 
26903cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_write(struct file *file,
26913cb4a0bbSKawai, Hidehiro 					  const char __user *buf,
26923cb4a0bbSKawai, Hidehiro 					  size_t count,
26933cb4a0bbSKawai, Hidehiro 					  loff_t *ppos)
26943cb4a0bbSKawai, Hidehiro {
26953cb4a0bbSKawai, Hidehiro 	struct task_struct *task;
26963cb4a0bbSKawai, Hidehiro 	struct mm_struct *mm;
26973cb4a0bbSKawai, Hidehiro 	char buffer[PROC_NUMBUF], *end;
26983cb4a0bbSKawai, Hidehiro 	unsigned int val;
26993cb4a0bbSKawai, Hidehiro 	int ret;
27003cb4a0bbSKawai, Hidehiro 	int i;
27013cb4a0bbSKawai, Hidehiro 	unsigned long mask;
27023cb4a0bbSKawai, Hidehiro 
27033cb4a0bbSKawai, Hidehiro 	ret = -EFAULT;
27043cb4a0bbSKawai, Hidehiro 	memset(buffer, 0, sizeof(buffer));
27053cb4a0bbSKawai, Hidehiro 	if (count > sizeof(buffer) - 1)
27063cb4a0bbSKawai, Hidehiro 		count = sizeof(buffer) - 1;
27073cb4a0bbSKawai, Hidehiro 	if (copy_from_user(buffer, buf, count))
27083cb4a0bbSKawai, Hidehiro 		goto out_no_task;
27093cb4a0bbSKawai, Hidehiro 
27103cb4a0bbSKawai, Hidehiro 	ret = -EINVAL;
27113cb4a0bbSKawai, Hidehiro 	val = (unsigned int)simple_strtoul(buffer, &end, 0);
27123cb4a0bbSKawai, Hidehiro 	if (*end == '\n')
27133cb4a0bbSKawai, Hidehiro 		end++;
27143cb4a0bbSKawai, Hidehiro 	if (end - buffer == 0)
27153cb4a0bbSKawai, Hidehiro 		goto out_no_task;
27163cb4a0bbSKawai, Hidehiro 
27173cb4a0bbSKawai, Hidehiro 	ret = -ESRCH;
27183cb4a0bbSKawai, Hidehiro 	task = get_proc_task(file->f_dentry->d_inode);
27193cb4a0bbSKawai, Hidehiro 	if (!task)
27203cb4a0bbSKawai, Hidehiro 		goto out_no_task;
27213cb4a0bbSKawai, Hidehiro 
27223cb4a0bbSKawai, Hidehiro 	ret = end - buffer;
27233cb4a0bbSKawai, Hidehiro 	mm = get_task_mm(task);
27243cb4a0bbSKawai, Hidehiro 	if (!mm)
27253cb4a0bbSKawai, Hidehiro 		goto out_no_mm;
27263cb4a0bbSKawai, Hidehiro 
27273cb4a0bbSKawai, Hidehiro 	for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
27283cb4a0bbSKawai, Hidehiro 		if (val & mask)
27293cb4a0bbSKawai, Hidehiro 			set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
27303cb4a0bbSKawai, Hidehiro 		else
27313cb4a0bbSKawai, Hidehiro 			clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
27323cb4a0bbSKawai, Hidehiro 	}
27333cb4a0bbSKawai, Hidehiro 
27343cb4a0bbSKawai, Hidehiro 	mmput(mm);
27353cb4a0bbSKawai, Hidehiro  out_no_mm:
27363cb4a0bbSKawai, Hidehiro 	put_task_struct(task);
27373cb4a0bbSKawai, Hidehiro  out_no_task:
27383cb4a0bbSKawai, Hidehiro 	return ret;
27393cb4a0bbSKawai, Hidehiro }
27403cb4a0bbSKawai, Hidehiro 
27413cb4a0bbSKawai, Hidehiro static const struct file_operations proc_coredump_filter_operations = {
27423cb4a0bbSKawai, Hidehiro 	.read		= proc_coredump_filter_read,
27433cb4a0bbSKawai, Hidehiro 	.write		= proc_coredump_filter_write,
274487df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
27453cb4a0bbSKawai, Hidehiro };
27463cb4a0bbSKawai, Hidehiro #endif
27473cb4a0bbSKawai, Hidehiro 
27481da177e4SLinus Torvalds /*
27491da177e4SLinus Torvalds  * /proc/self:
27501da177e4SLinus Torvalds  */
27511da177e4SLinus Torvalds static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
27521da177e4SLinus Torvalds 			      int buflen)
27531da177e4SLinus Torvalds {
2754488e5bc4SEric W. Biederman 	struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2755b55fcb22SAndrew Morton 	pid_t tgid = task_tgid_nr_ns(current, ns);
27568578cea7SEric W. Biederman 	char tmp[PROC_NUMBUF];
2757b55fcb22SAndrew Morton 	if (!tgid)
2758488e5bc4SEric W. Biederman 		return -ENOENT;
2759b55fcb22SAndrew Morton 	sprintf(tmp, "%d", tgid);
27601da177e4SLinus Torvalds 	return vfs_readlink(dentry,buffer,buflen,tmp);
27611da177e4SLinus Torvalds }
27621da177e4SLinus Torvalds 
2763008b150aSAl Viro static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
27641da177e4SLinus Torvalds {
2765488e5bc4SEric W. Biederman 	struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2766b55fcb22SAndrew Morton 	pid_t tgid = task_tgid_nr_ns(current, ns);
27677fee4868SAl Viro 	char *name = ERR_PTR(-ENOENT);
27687fee4868SAl Viro 	if (tgid) {
27697fee4868SAl Viro 		name = __getname();
27707fee4868SAl Viro 		if (!name)
27717fee4868SAl Viro 			name = ERR_PTR(-ENOMEM);
27727fee4868SAl Viro 		else
27737fee4868SAl Viro 			sprintf(name, "%d", tgid);
27747fee4868SAl Viro 	}
27757fee4868SAl Viro 	nd_set_link(nd, name);
27767fee4868SAl Viro 	return NULL;
27777fee4868SAl Viro }
27787fee4868SAl Viro 
27797fee4868SAl Viro static void proc_self_put_link(struct dentry *dentry, struct nameidata *nd,
27807fee4868SAl Viro 				void *cookie)
27817fee4868SAl Viro {
27827fee4868SAl Viro 	char *s = nd_get_link(nd);
27837fee4868SAl Viro 	if (!IS_ERR(s))
27847fee4868SAl Viro 		__putname(s);
27851da177e4SLinus Torvalds }
27861da177e4SLinus Torvalds 
2787c5ef1c42SArjan van de Ven static const struct inode_operations proc_self_inode_operations = {
27881da177e4SLinus Torvalds 	.readlink	= proc_self_readlink,
27891da177e4SLinus Torvalds 	.follow_link	= proc_self_follow_link,
27907fee4868SAl Viro 	.put_link	= proc_self_put_link,
27911da177e4SLinus Torvalds };
27921da177e4SLinus Torvalds 
279328a6d671SEric W. Biederman /*
2794801199ceSEric W. Biederman  * proc base
2795801199ceSEric W. Biederman  *
2796801199ceSEric W. Biederman  * These are the directory entries in the root directory of /proc
2797801199ceSEric W. Biederman  * that properly belong to the /proc filesystem, as they describe
2798801199ceSEric W. Biederman  * describe something that is process related.
2799801199ceSEric W. Biederman  */
2800c5141e6dSEric Dumazet static const struct pid_entry proc_base_stuff[] = {
280161a28784SEric W. Biederman 	NOD("self", S_IFLNK|S_IRWXUGO,
2802801199ceSEric W. Biederman 		&proc_self_inode_operations, NULL, {}),
2803801199ceSEric W. Biederman };
2804801199ceSEric W. Biederman 
2805444ceed8SEric W. Biederman static struct dentry *proc_base_instantiate(struct inode *dir,
2806c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
2807801199ceSEric W. Biederman {
2808c5141e6dSEric Dumazet 	const struct pid_entry *p = ptr;
2809801199ceSEric W. Biederman 	struct inode *inode;
2810801199ceSEric W. Biederman 	struct proc_inode *ei;
281173d36460SDan Carpenter 	struct dentry *error;
2812801199ceSEric W. Biederman 
2813801199ceSEric W. Biederman 	/* Allocate the inode */
2814801199ceSEric W. Biederman 	error = ERR_PTR(-ENOMEM);
2815801199ceSEric W. Biederman 	inode = new_inode(dir->i_sb);
2816801199ceSEric W. Biederman 	if (!inode)
2817801199ceSEric W. Biederman 		goto out;
2818801199ceSEric W. Biederman 
2819801199ceSEric W. Biederman 	/* Initialize the inode */
2820801199ceSEric W. Biederman 	ei = PROC_I(inode);
282185fe4025SChristoph Hellwig 	inode->i_ino = get_next_ino();
2822801199ceSEric W. Biederman 	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2823801199ceSEric W. Biederman 
2824801199ceSEric W. Biederman 	/*
2825801199ceSEric W. Biederman 	 * grab the reference to the task.
2826801199ceSEric W. Biederman 	 */
28271a657f78SOleg Nesterov 	ei->pid = get_task_pid(task, PIDTYPE_PID);
2828801199ceSEric W. Biederman 	if (!ei->pid)
2829801199ceSEric W. Biederman 		goto out_iput;
2830801199ceSEric W. Biederman 
2831801199ceSEric W. Biederman 	inode->i_mode = p->mode;
2832801199ceSEric W. Biederman 	if (S_ISDIR(inode->i_mode))
2833bfe86848SMiklos Szeredi 		set_nlink(inode, 2);
2834801199ceSEric W. Biederman 	if (S_ISLNK(inode->i_mode))
2835801199ceSEric W. Biederman 		inode->i_size = 64;
2836801199ceSEric W. Biederman 	if (p->iop)
2837801199ceSEric W. Biederman 		inode->i_op = p->iop;
2838801199ceSEric W. Biederman 	if (p->fop)
2839801199ceSEric W. Biederman 		inode->i_fop = p->fop;
2840801199ceSEric W. Biederman 	ei->op = p->op;
2841801199ceSEric W. Biederman 	d_add(dentry, inode);
2842801199ceSEric W. Biederman 	error = NULL;
2843801199ceSEric W. Biederman out:
2844801199ceSEric W. Biederman 	return error;
2845801199ceSEric W. Biederman out_iput:
2846801199ceSEric W. Biederman 	iput(inode);
2847801199ceSEric W. Biederman 	goto out;
2848801199ceSEric W. Biederman }
2849801199ceSEric W. Biederman 
2850444ceed8SEric W. Biederman static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2851444ceed8SEric W. Biederman {
2852444ceed8SEric W. Biederman 	struct dentry *error;
2853444ceed8SEric W. Biederman 	struct task_struct *task = get_proc_task(dir);
2854c5141e6dSEric Dumazet 	const struct pid_entry *p, *last;
2855444ceed8SEric W. Biederman 
2856444ceed8SEric W. Biederman 	error = ERR_PTR(-ENOENT);
2857444ceed8SEric W. Biederman 
2858444ceed8SEric W. Biederman 	if (!task)
2859444ceed8SEric W. Biederman 		goto out_no_task;
2860444ceed8SEric W. Biederman 
2861444ceed8SEric W. Biederman 	/* Lookup the directory entry */
28627bcd6b0eSEric W. Biederman 	last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
28637bcd6b0eSEric W. Biederman 	for (p = proc_base_stuff; p <= last; p++) {
2864444ceed8SEric W. Biederman 		if (p->len != dentry->d_name.len)
2865444ceed8SEric W. Biederman 			continue;
2866444ceed8SEric W. Biederman 		if (!memcmp(dentry->d_name.name, p->name, p->len))
2867444ceed8SEric W. Biederman 			break;
2868444ceed8SEric W. Biederman 	}
28697bcd6b0eSEric W. Biederman 	if (p > last)
2870444ceed8SEric W. Biederman 		goto out;
2871444ceed8SEric W. Biederman 
2872444ceed8SEric W. Biederman 	error = proc_base_instantiate(dir, dentry, task, p);
2873444ceed8SEric W. Biederman 
2874444ceed8SEric W. Biederman out:
2875444ceed8SEric W. Biederman 	put_task_struct(task);
2876444ceed8SEric W. Biederman out_no_task:
2877444ceed8SEric W. Biederman 	return error;
2878444ceed8SEric W. Biederman }
2879444ceed8SEric W. Biederman 
2880c5141e6dSEric Dumazet static int proc_base_fill_cache(struct file *filp, void *dirent,
2881c5141e6dSEric Dumazet 	filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
288261a28784SEric W. Biederman {
288361a28784SEric W. Biederman 	return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
288461a28784SEric W. Biederman 				proc_base_instantiate, task, p);
288561a28784SEric W. Biederman }
288661a28784SEric W. Biederman 
2887aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING
2888297c5d92SAndrea Righi static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
2889aba76fdbSAndrew Morton {
2890940389b8SAndrea Righi 	struct task_io_accounting acct = task->ioac;
2891297c5d92SAndrea Righi 	unsigned long flags;
2892293eb1e7SVasiliy Kulikov 	int result;
2893297c5d92SAndrea Righi 
2894293eb1e7SVasiliy Kulikov 	result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2895293eb1e7SVasiliy Kulikov 	if (result)
2896293eb1e7SVasiliy Kulikov 		return result;
2897293eb1e7SVasiliy Kulikov 
2898293eb1e7SVasiliy Kulikov 	if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
2899293eb1e7SVasiliy Kulikov 		result = -EACCES;
2900293eb1e7SVasiliy Kulikov 		goto out_unlock;
2901293eb1e7SVasiliy Kulikov 	}
29021d1221f3SVasiliy Kulikov 
29035995477aSAndrea Righi 	if (whole && lock_task_sighand(task, &flags)) {
2904b2d002dbSAndrea Righi 		struct task_struct *t = task;
2905297c5d92SAndrea Righi 
29065995477aSAndrea Righi 		task_io_accounting_add(&acct, &task->signal->ioac);
29075995477aSAndrea Righi 		while_each_thread(task, t)
29085995477aSAndrea Righi 			task_io_accounting_add(&acct, &t->ioac);
2909297c5d92SAndrea Righi 
2910297c5d92SAndrea Righi 		unlock_task_sighand(task, &flags);
2911297c5d92SAndrea Righi 	}
2912293eb1e7SVasiliy Kulikov 	result = sprintf(buffer,
2913aba76fdbSAndrew Morton 			"rchar: %llu\n"
2914aba76fdbSAndrew Morton 			"wchar: %llu\n"
2915aba76fdbSAndrew Morton 			"syscr: %llu\n"
2916aba76fdbSAndrew Morton 			"syscw: %llu\n"
2917aba76fdbSAndrew Morton 			"read_bytes: %llu\n"
2918aba76fdbSAndrew Morton 			"write_bytes: %llu\n"
2919aba76fdbSAndrew Morton 			"cancelled_write_bytes: %llu\n",
29207c44319dSAlexander Beregalov 			(unsigned long long)acct.rchar,
29217c44319dSAlexander Beregalov 			(unsigned long long)acct.wchar,
29227c44319dSAlexander Beregalov 			(unsigned long long)acct.syscr,
29237c44319dSAlexander Beregalov 			(unsigned long long)acct.syscw,
29247c44319dSAlexander Beregalov 			(unsigned long long)acct.read_bytes,
29257c44319dSAlexander Beregalov 			(unsigned long long)acct.write_bytes,
29267c44319dSAlexander Beregalov 			(unsigned long long)acct.cancelled_write_bytes);
2927293eb1e7SVasiliy Kulikov out_unlock:
2928293eb1e7SVasiliy Kulikov 	mutex_unlock(&task->signal->cred_guard_mutex);
2929293eb1e7SVasiliy Kulikov 	return result;
2930aba76fdbSAndrew Morton }
2931297c5d92SAndrea Righi 
2932297c5d92SAndrea Righi static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2933297c5d92SAndrea Righi {
2934297c5d92SAndrea Righi 	return do_io_accounting(task, buffer, 0);
2935297c5d92SAndrea Righi }
2936297c5d92SAndrea Righi 
2937297c5d92SAndrea Righi static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2938297c5d92SAndrea Righi {
2939297c5d92SAndrea Righi 	return do_io_accounting(task, buffer, 1);
2940297c5d92SAndrea Righi }
2941297c5d92SAndrea Righi #endif /* CONFIG_TASK_IO_ACCOUNTING */
2942aba76fdbSAndrew Morton 
294322d917d8SEric W. Biederman #ifdef CONFIG_USER_NS
294422d917d8SEric W. Biederman static int proc_id_map_open(struct inode *inode, struct file *file,
294522d917d8SEric W. Biederman 	struct seq_operations *seq_ops)
294622d917d8SEric W. Biederman {
294722d917d8SEric W. Biederman 	struct user_namespace *ns = NULL;
294822d917d8SEric W. Biederman 	struct task_struct *task;
294922d917d8SEric W. Biederman 	struct seq_file *seq;
295022d917d8SEric W. Biederman 	int ret = -EINVAL;
295122d917d8SEric W. Biederman 
295222d917d8SEric W. Biederman 	task = get_proc_task(inode);
295322d917d8SEric W. Biederman 	if (task) {
295422d917d8SEric W. Biederman 		rcu_read_lock();
295522d917d8SEric W. Biederman 		ns = get_user_ns(task_cred_xxx(task, user_ns));
295622d917d8SEric W. Biederman 		rcu_read_unlock();
295722d917d8SEric W. Biederman 		put_task_struct(task);
295822d917d8SEric W. Biederman 	}
295922d917d8SEric W. Biederman 	if (!ns)
296022d917d8SEric W. Biederman 		goto err;
296122d917d8SEric W. Biederman 
296222d917d8SEric W. Biederman 	ret = seq_open(file, seq_ops);
296322d917d8SEric W. Biederman 	if (ret)
296422d917d8SEric W. Biederman 		goto err_put_ns;
296522d917d8SEric W. Biederman 
296622d917d8SEric W. Biederman 	seq = file->private_data;
296722d917d8SEric W. Biederman 	seq->private = ns;
296822d917d8SEric W. Biederman 
296922d917d8SEric W. Biederman 	return 0;
297022d917d8SEric W. Biederman err_put_ns:
297122d917d8SEric W. Biederman 	put_user_ns(ns);
297222d917d8SEric W. Biederman err:
297322d917d8SEric W. Biederman 	return ret;
297422d917d8SEric W. Biederman }
297522d917d8SEric W. Biederman 
297622d917d8SEric W. Biederman static int proc_id_map_release(struct inode *inode, struct file *file)
297722d917d8SEric W. Biederman {
297822d917d8SEric W. Biederman 	struct seq_file *seq = file->private_data;
297922d917d8SEric W. Biederman 	struct user_namespace *ns = seq->private;
298022d917d8SEric W. Biederman 	put_user_ns(ns);
298122d917d8SEric W. Biederman 	return seq_release(inode, file);
298222d917d8SEric W. Biederman }
298322d917d8SEric W. Biederman 
298422d917d8SEric W. Biederman static int proc_uid_map_open(struct inode *inode, struct file *file)
298522d917d8SEric W. Biederman {
298622d917d8SEric W. Biederman 	return proc_id_map_open(inode, file, &proc_uid_seq_operations);
298722d917d8SEric W. Biederman }
298822d917d8SEric W. Biederman 
298922d917d8SEric W. Biederman static int proc_gid_map_open(struct inode *inode, struct file *file)
299022d917d8SEric W. Biederman {
299122d917d8SEric W. Biederman 	return proc_id_map_open(inode, file, &proc_gid_seq_operations);
299222d917d8SEric W. Biederman }
299322d917d8SEric W. Biederman 
299422d917d8SEric W. Biederman static const struct file_operations proc_uid_map_operations = {
299522d917d8SEric W. Biederman 	.open		= proc_uid_map_open,
299622d917d8SEric W. Biederman 	.write		= proc_uid_map_write,
299722d917d8SEric W. Biederman 	.read		= seq_read,
299822d917d8SEric W. Biederman 	.llseek		= seq_lseek,
299922d917d8SEric W. Biederman 	.release	= proc_id_map_release,
300022d917d8SEric W. Biederman };
300122d917d8SEric W. Biederman 
300222d917d8SEric W. Biederman static const struct file_operations proc_gid_map_operations = {
300322d917d8SEric W. Biederman 	.open		= proc_gid_map_open,
300422d917d8SEric W. Biederman 	.write		= proc_gid_map_write,
300522d917d8SEric W. Biederman 	.read		= seq_read,
300622d917d8SEric W. Biederman 	.llseek		= seq_lseek,
300722d917d8SEric W. Biederman 	.release	= proc_id_map_release,
300822d917d8SEric W. Biederman };
300922d917d8SEric W. Biederman #endif /* CONFIG_USER_NS */
301022d917d8SEric W. Biederman 
301147830723SKees Cook static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
301247830723SKees Cook 				struct pid *pid, struct task_struct *task)
301347830723SKees Cook {
3014a9712bc1SAl Viro 	int err = lock_trace(task);
3015a9712bc1SAl Viro 	if (!err) {
301647830723SKees Cook 		seq_printf(m, "%08x\n", task->personality);
3017a9712bc1SAl Viro 		unlock_trace(task);
3018a9712bc1SAl Viro 	}
3019a9712bc1SAl Viro 	return err;
302047830723SKees Cook }
302147830723SKees Cook 
3022801199ceSEric W. Biederman /*
302328a6d671SEric W. Biederman  * Thread groups
302428a6d671SEric W. Biederman  */
302500977a59SArjan van de Ven static const struct file_operations proc_task_operations;
3026c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations;
302720cdc894SEric W. Biederman 
3028c5141e6dSEric Dumazet static const struct pid_entry tgid_base_stuff[] = {
3029631f9c18SAlexey Dobriyan 	DIR("task",       S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
3030631f9c18SAlexey Dobriyan 	DIR("fd",         S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
3031640708a2SPavel Emelyanov #ifdef CONFIG_CHECKPOINT_RESTORE
3032640708a2SPavel Emelyanov 	DIR("map_files",  S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
3033640708a2SPavel Emelyanov #endif
3034631f9c18SAlexey Dobriyan 	DIR("fdinfo",     S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
30356b4e306aSEric W. Biederman 	DIR("ns",	  S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
3036b2211a36SAndrew Morton #ifdef CONFIG_NET
3037631f9c18SAlexey Dobriyan 	DIR("net",        S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
3038b2211a36SAndrew Morton #endif
3039631f9c18SAlexey Dobriyan 	REG("environ",    S_IRUSR, proc_environ_operations),
3040631f9c18SAlexey Dobriyan 	INF("auxv",       S_IRUSR, proc_pid_auxv),
3041631f9c18SAlexey Dobriyan 	ONE("status",     S_IRUGO, proc_pid_status),
3042a9712bc1SAl Viro 	ONE("personality", S_IRUGO, proc_pid_personality),
30433036e7b4SJiri Olsa 	INF("limits",	  S_IRUGO, proc_pid_limits),
304443ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
3045631f9c18SAlexey Dobriyan 	REG("sched",      S_IRUGO|S_IWUSR, proc_pid_sched_operations),
304643ae34cbSIngo Molnar #endif
30475091faa4SMike Galbraith #ifdef CONFIG_SCHED_AUTOGROUP
30485091faa4SMike Galbraith 	REG("autogroup",  S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
30495091faa4SMike Galbraith #endif
30504614a696Sjohn stultz 	REG("comm",      S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
3051ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3052a9712bc1SAl Viro 	INF("syscall",    S_IRUGO, proc_pid_syscall),
3053ebcb6734SRoland McGrath #endif
3054631f9c18SAlexey Dobriyan 	INF("cmdline",    S_IRUGO, proc_pid_cmdline),
3055631f9c18SAlexey Dobriyan 	ONE("stat",       S_IRUGO, proc_tgid_stat),
3056631f9c18SAlexey Dobriyan 	ONE("statm",      S_IRUGO, proc_pid_statm),
3057b7643757SSiddhesh Poyarekar 	REG("maps",       S_IRUGO, proc_pid_maps_operations),
305828a6d671SEric W. Biederman #ifdef CONFIG_NUMA
3059b7643757SSiddhesh Poyarekar 	REG("numa_maps",  S_IRUGO, proc_pid_numa_maps_operations),
306028a6d671SEric W. Biederman #endif
3061631f9c18SAlexey Dobriyan 	REG("mem",        S_IRUSR|S_IWUSR, proc_mem_operations),
3062631f9c18SAlexey Dobriyan 	LNK("cwd",        proc_cwd_link),
3063631f9c18SAlexey Dobriyan 	LNK("root",       proc_root_link),
3064631f9c18SAlexey Dobriyan 	LNK("exe",        proc_exe_link),
3065631f9c18SAlexey Dobriyan 	REG("mounts",     S_IRUGO, proc_mounts_operations),
3066631f9c18SAlexey Dobriyan 	REG("mountinfo",  S_IRUGO, proc_mountinfo_operations),
3067631f9c18SAlexey Dobriyan 	REG("mountstats", S_IRUSR, proc_mountstats_operations),
30681e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR
3069631f9c18SAlexey Dobriyan 	REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3070b7643757SSiddhesh Poyarekar 	REG("smaps",      S_IRUGO, proc_pid_smaps_operations),
3071ca6b0bf0SAl Viro 	REG("pagemap",    S_IRUGO, proc_pagemap_operations),
307228a6d671SEric W. Biederman #endif
307328a6d671SEric W. Biederman #ifdef CONFIG_SECURITY
3074631f9c18SAlexey Dobriyan 	DIR("attr",       S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
307528a6d671SEric W. Biederman #endif
307628a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS
3077631f9c18SAlexey Dobriyan 	INF("wchan",      S_IRUGO, proc_pid_wchan),
307828a6d671SEric W. Biederman #endif
30792ec220e2SKen Chen #ifdef CONFIG_STACKTRACE
3080a9712bc1SAl Viro 	ONE("stack",      S_IRUGO, proc_pid_stack),
308128a6d671SEric W. Biederman #endif
308228a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS
3083631f9c18SAlexey Dobriyan 	INF("schedstat",  S_IRUGO, proc_pid_schedstat),
308428a6d671SEric W. Biederman #endif
30859745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP
3086631f9c18SAlexey Dobriyan 	REG("latency",  S_IRUGO, proc_lstats_operations),
30879745512cSArjan van de Ven #endif
30888793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET
3089631f9c18SAlexey Dobriyan 	REG("cpuset",     S_IRUGO, proc_cpuset_operations),
309028a6d671SEric W. Biederman #endif
3091a424316cSPaul Menage #ifdef CONFIG_CGROUPS
3092631f9c18SAlexey Dobriyan 	REG("cgroup",  S_IRUGO, proc_cgroup_operations),
3093a424316cSPaul Menage #endif
3094631f9c18SAlexey Dobriyan 	INF("oom_score",  S_IRUGO, proc_oom_score),
3095631f9c18SAlexey Dobriyan 	REG("oom_adj",    S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
3096a63d83f4SDavid Rientjes 	REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
309728a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL
3098631f9c18SAlexey Dobriyan 	REG("loginuid",   S_IWUSR|S_IRUGO, proc_loginuid_operations),
3099631f9c18SAlexey Dobriyan 	REG("sessionid",  S_IRUGO, proc_sessionid_operations),
310028a6d671SEric W. Biederman #endif
3101f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
3102631f9c18SAlexey Dobriyan 	REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3103f4f154fdSAkinobu Mita #endif
3104698ba7b5SChristoph Hellwig #ifdef CONFIG_ELF_CORE
3105631f9c18SAlexey Dobriyan 	REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
31063cb4a0bbSKawai, Hidehiro #endif
3107aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING
31081d1221f3SVasiliy Kulikov 	INF("io",	S_IRUSR, proc_tgid_io_accounting),
3109aba76fdbSAndrew Morton #endif
3110f133eccaSChris Metcalf #ifdef CONFIG_HARDWALL
3111f133eccaSChris Metcalf 	INF("hardwall",   S_IRUGO, proc_pid_hardwall),
3112f133eccaSChris Metcalf #endif
311322d917d8SEric W. Biederman #ifdef CONFIG_USER_NS
311422d917d8SEric W. Biederman 	REG("uid_map",    S_IRUGO|S_IWUSR, proc_uid_map_operations),
311522d917d8SEric W. Biederman 	REG("gid_map",    S_IRUGO|S_IWUSR, proc_gid_map_operations),
311622d917d8SEric W. Biederman #endif
311728a6d671SEric W. Biederman };
311828a6d671SEric W. Biederman 
311928a6d671SEric W. Biederman static int proc_tgid_base_readdir(struct file * filp,
312028a6d671SEric W. Biederman 			     void * dirent, filldir_t filldir)
312128a6d671SEric W. Biederman {
312228a6d671SEric W. Biederman 	return proc_pident_readdir(filp,dirent,filldir,
312328a6d671SEric W. Biederman 				   tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
312428a6d671SEric W. Biederman }
312528a6d671SEric W. Biederman 
312600977a59SArjan van de Ven static const struct file_operations proc_tgid_base_operations = {
312728a6d671SEric W. Biederman 	.read		= generic_read_dir,
312828a6d671SEric W. Biederman 	.readdir	= proc_tgid_base_readdir,
31296038f373SArnd Bergmann 	.llseek		= default_llseek,
313028a6d671SEric W. Biederman };
313128a6d671SEric W. Biederman 
313200cd8dd3SAl Viro static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
313300cd8dd3SAl Viro {
31347bcd6b0eSEric W. Biederman 	return proc_pident_lookup(dir, dentry,
31357bcd6b0eSEric W. Biederman 				  tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
313628a6d671SEric W. Biederman }
313728a6d671SEric W. Biederman 
3138c5ef1c42SArjan van de Ven static const struct inode_operations proc_tgid_base_inode_operations = {
313928a6d671SEric W. Biederman 	.lookup		= proc_tgid_base_lookup,
314028a6d671SEric W. Biederman 	.getattr	= pid_getattr,
314128a6d671SEric W. Biederman 	.setattr	= proc_setattr,
31420499680aSVasiliy Kulikov 	.permission	= proc_pid_permission,
314328a6d671SEric W. Biederman };
314428a6d671SEric W. Biederman 
314560347f67SPavel Emelyanov static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
31461da177e4SLinus Torvalds {
314748e6484dSEric W. Biederman 	struct dentry *dentry, *leader, *dir;
31488578cea7SEric W. Biederman 	char buf[PROC_NUMBUF];
314948e6484dSEric W. Biederman 	struct qstr name;
31501da177e4SLinus Torvalds 
315148e6484dSEric W. Biederman 	name.name = buf;
315260347f67SPavel Emelyanov 	name.len = snprintf(buf, sizeof(buf), "%d", pid);
315360347f67SPavel Emelyanov 	dentry = d_hash_and_lookup(mnt->mnt_root, &name);
315448e6484dSEric W. Biederman 	if (dentry) {
315548e6484dSEric W. Biederman 		shrink_dcache_parent(dentry);
315648e6484dSEric W. Biederman 		d_drop(dentry);
315748e6484dSEric W. Biederman 		dput(dentry);
31581da177e4SLinus Torvalds 	}
31591da177e4SLinus Torvalds 
316048e6484dSEric W. Biederman 	name.name = buf;
316160347f67SPavel Emelyanov 	name.len = snprintf(buf, sizeof(buf), "%d", tgid);
316260347f67SPavel Emelyanov 	leader = d_hash_and_lookup(mnt->mnt_root, &name);
316348e6484dSEric W. Biederman 	if (!leader)
316448e6484dSEric W. Biederman 		goto out;
316548e6484dSEric W. Biederman 
316648e6484dSEric W. Biederman 	name.name = "task";
316748e6484dSEric W. Biederman 	name.len = strlen(name.name);
316848e6484dSEric W. Biederman 	dir = d_hash_and_lookup(leader, &name);
316948e6484dSEric W. Biederman 	if (!dir)
317048e6484dSEric W. Biederman 		goto out_put_leader;
317148e6484dSEric W. Biederman 
317248e6484dSEric W. Biederman 	name.name = buf;
317360347f67SPavel Emelyanov 	name.len = snprintf(buf, sizeof(buf), "%d", pid);
317448e6484dSEric W. Biederman 	dentry = d_hash_and_lookup(dir, &name);
317548e6484dSEric W. Biederman 	if (dentry) {
317648e6484dSEric W. Biederman 		shrink_dcache_parent(dentry);
317748e6484dSEric W. Biederman 		d_drop(dentry);
317848e6484dSEric W. Biederman 		dput(dentry);
31791da177e4SLinus Torvalds 	}
318048e6484dSEric W. Biederman 
318148e6484dSEric W. Biederman 	dput(dir);
318248e6484dSEric W. Biederman out_put_leader:
318348e6484dSEric W. Biederman 	dput(leader);
318448e6484dSEric W. Biederman out:
318548e6484dSEric W. Biederman 	return;
31861da177e4SLinus Torvalds }
31871da177e4SLinus Torvalds 
31880895e91dSRandy Dunlap /**
31890895e91dSRandy Dunlap  * proc_flush_task -  Remove dcache entries for @task from the /proc dcache.
31900895e91dSRandy Dunlap  * @task: task that should be flushed.
31910895e91dSRandy Dunlap  *
31920895e91dSRandy Dunlap  * When flushing dentries from proc, one needs to flush them from global
319360347f67SPavel Emelyanov  * proc (proc_mnt) and from all the namespaces' procs this task was seen
31940895e91dSRandy Dunlap  * in. This call is supposed to do all of this job.
31950895e91dSRandy Dunlap  *
31960895e91dSRandy Dunlap  * Looks in the dcache for
31970895e91dSRandy Dunlap  * /proc/@pid
31980895e91dSRandy Dunlap  * /proc/@tgid/task/@pid
31990895e91dSRandy Dunlap  * if either directory is present flushes it and all of it'ts children
32000895e91dSRandy Dunlap  * from the dcache.
32010895e91dSRandy Dunlap  *
32020895e91dSRandy Dunlap  * It is safe and reasonable to cache /proc entries for a task until
32030895e91dSRandy Dunlap  * that task exits.  After that they just clog up the dcache with
32040895e91dSRandy Dunlap  * useless entries, possibly causing useful dcache entries to be
32050895e91dSRandy Dunlap  * flushed instead.  This routine is proved to flush those useless
32060895e91dSRandy Dunlap  * dcache entries at process exit time.
32070895e91dSRandy Dunlap  *
32080895e91dSRandy Dunlap  * NOTE: This routine is just an optimization so it does not guarantee
32090895e91dSRandy Dunlap  *       that no dcache entries will exist at process exit time it
32100895e91dSRandy Dunlap  *       just makes it very unlikely that any will persist.
321160347f67SPavel Emelyanov  */
321260347f67SPavel Emelyanov 
321360347f67SPavel Emelyanov void proc_flush_task(struct task_struct *task)
321460347f67SPavel Emelyanov {
32159fcc2d15SEric W. Biederman 	int i;
32169b4d1cbeSOleg Nesterov 	struct pid *pid, *tgid;
3217130f77ecSPavel Emelyanov 	struct upid *upid;
3218130f77ecSPavel Emelyanov 
3219130f77ecSPavel Emelyanov 	pid = task_pid(task);
3220130f77ecSPavel Emelyanov 	tgid = task_tgid(task);
32219fcc2d15SEric W. Biederman 
32229fcc2d15SEric W. Biederman 	for (i = 0; i <= pid->level; i++) {
3223130f77ecSPavel Emelyanov 		upid = &pid->numbers[i];
3224130f77ecSPavel Emelyanov 		proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
32259b4d1cbeSOleg Nesterov 					tgid->numbers[i].nr);
3226130f77ecSPavel Emelyanov 	}
32276f4e6433SPavel Emelyanov 
32286f4e6433SPavel Emelyanov 	upid = &pid->numbers[pid->level];
32296f4e6433SPavel Emelyanov 	if (upid->nr == 1)
32306f4e6433SPavel Emelyanov 		pid_ns_release_proc(upid->ns);
323160347f67SPavel Emelyanov }
323260347f67SPavel Emelyanov 
32339711ef99SAdrian Bunk static struct dentry *proc_pid_instantiate(struct inode *dir,
32349711ef99SAdrian Bunk 					   struct dentry * dentry,
3235c5141e6dSEric Dumazet 					   struct task_struct *task, const void *ptr)
3236444ceed8SEric W. Biederman {
3237444ceed8SEric W. Biederman 	struct dentry *error = ERR_PTR(-ENOENT);
3238444ceed8SEric W. Biederman 	struct inode *inode;
3239444ceed8SEric W. Biederman 
324061a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
3241444ceed8SEric W. Biederman 	if (!inode)
3242444ceed8SEric W. Biederman 		goto out;
3243444ceed8SEric W. Biederman 
3244444ceed8SEric W. Biederman 	inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3245444ceed8SEric W. Biederman 	inode->i_op = &proc_tgid_base_inode_operations;
3246444ceed8SEric W. Biederman 	inode->i_fop = &proc_tgid_base_operations;
3247444ceed8SEric W. Biederman 	inode->i_flags|=S_IMMUTABLE;
3248aed54175SVegard Nossum 
3249bfe86848SMiklos Szeredi 	set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
3250bfe86848SMiklos Szeredi 						  ARRAY_SIZE(tgid_base_stuff)));
3251444ceed8SEric W. Biederman 
3252fb045adbSNick Piggin 	d_set_d_op(dentry, &pid_dentry_operations);
3253444ceed8SEric W. Biederman 
3254444ceed8SEric W. Biederman 	d_add(dentry, inode);
3255444ceed8SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
32560b728e19SAl Viro 	if (pid_revalidate(dentry, 0))
3257444ceed8SEric W. Biederman 		error = NULL;
3258444ceed8SEric W. Biederman out:
3259444ceed8SEric W. Biederman 	return error;
3260444ceed8SEric W. Biederman }
3261444ceed8SEric W. Biederman 
326200cd8dd3SAl Viro struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
32631da177e4SLinus Torvalds {
326473d36460SDan Carpenter 	struct dentry *result;
32651da177e4SLinus Torvalds 	struct task_struct *task;
32661da177e4SLinus Torvalds 	unsigned tgid;
3267b488893aSPavel Emelyanov 	struct pid_namespace *ns;
32681da177e4SLinus Torvalds 
3269801199ceSEric W. Biederman 	result = proc_base_lookup(dir, dentry);
3270801199ceSEric W. Biederman 	if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
3271801199ceSEric W. Biederman 		goto out;
3272801199ceSEric W. Biederman 
32731da177e4SLinus Torvalds 	tgid = name_to_int(dentry);
32741da177e4SLinus Torvalds 	if (tgid == ~0U)
32751da177e4SLinus Torvalds 		goto out;
32761da177e4SLinus Torvalds 
3277b488893aSPavel Emelyanov 	ns = dentry->d_sb->s_fs_info;
3278de758734SEric W. Biederman 	rcu_read_lock();
3279b488893aSPavel Emelyanov 	task = find_task_by_pid_ns(tgid, ns);
32801da177e4SLinus Torvalds 	if (task)
32811da177e4SLinus Torvalds 		get_task_struct(task);
3282de758734SEric W. Biederman 	rcu_read_unlock();
32831da177e4SLinus Torvalds 	if (!task)
32841da177e4SLinus Torvalds 		goto out;
32851da177e4SLinus Torvalds 
3286444ceed8SEric W. Biederman 	result = proc_pid_instantiate(dir, dentry, task, NULL);
328748e6484dSEric W. Biederman 	put_task_struct(task);
32881da177e4SLinus Torvalds out:
3289cd6a3ce9SEric W. Biederman 	return result;
32901da177e4SLinus Torvalds }
32911da177e4SLinus Torvalds 
32921da177e4SLinus Torvalds /*
32930804ef4bSEric W. Biederman  * Find the first task with tgid >= tgid
32940bc58a91SEric W. Biederman  *
32951da177e4SLinus Torvalds  */
329619fd4bb2SEric W. Biederman struct tgid_iter {
329719fd4bb2SEric W. Biederman 	unsigned int tgid;
32980804ef4bSEric W. Biederman 	struct task_struct *task;
329919fd4bb2SEric W. Biederman };
330019fd4bb2SEric W. Biederman static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
330119fd4bb2SEric W. Biederman {
33020804ef4bSEric W. Biederman 	struct pid *pid;
33031da177e4SLinus Torvalds 
330419fd4bb2SEric W. Biederman 	if (iter.task)
330519fd4bb2SEric W. Biederman 		put_task_struct(iter.task);
33060804ef4bSEric W. Biederman 	rcu_read_lock();
33070804ef4bSEric W. Biederman retry:
330819fd4bb2SEric W. Biederman 	iter.task = NULL;
330919fd4bb2SEric W. Biederman 	pid = find_ge_pid(iter.tgid, ns);
33100804ef4bSEric W. Biederman 	if (pid) {
331119fd4bb2SEric W. Biederman 		iter.tgid = pid_nr_ns(pid, ns);
331219fd4bb2SEric W. Biederman 		iter.task = pid_task(pid, PIDTYPE_PID);
33130804ef4bSEric W. Biederman 		/* What we to know is if the pid we have find is the
33140804ef4bSEric W. Biederman 		 * pid of a thread_group_leader.  Testing for task
33150804ef4bSEric W. Biederman 		 * being a thread_group_leader is the obvious thing
33160804ef4bSEric W. Biederman 		 * todo but there is a window when it fails, due to
33170804ef4bSEric W. Biederman 		 * the pid transfer logic in de_thread.
33180804ef4bSEric W. Biederman 		 *
33190804ef4bSEric W. Biederman 		 * So we perform the straight forward test of seeing
33200804ef4bSEric W. Biederman 		 * if the pid we have found is the pid of a thread
33210804ef4bSEric W. Biederman 		 * group leader, and don't worry if the task we have
33220804ef4bSEric W. Biederman 		 * found doesn't happen to be a thread group leader.
33230804ef4bSEric W. Biederman 		 * As we don't care in the case of readdir.
33240bc58a91SEric W. Biederman 		 */
332519fd4bb2SEric W. Biederman 		if (!iter.task || !has_group_leader_pid(iter.task)) {
332619fd4bb2SEric W. Biederman 			iter.tgid += 1;
33270804ef4bSEric W. Biederman 			goto retry;
332819fd4bb2SEric W. Biederman 		}
332919fd4bb2SEric W. Biederman 		get_task_struct(iter.task);
33301da177e4SLinus Torvalds 	}
3331454cc105SEric W. Biederman 	rcu_read_unlock();
333219fd4bb2SEric W. Biederman 	return iter;
33331da177e4SLinus Torvalds }
33341da177e4SLinus Torvalds 
33357bcd6b0eSEric W. Biederman #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
33361da177e4SLinus Torvalds 
333761a28784SEric W. Biederman static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
333819fd4bb2SEric W. Biederman 	struct tgid_iter iter)
333961a28784SEric W. Biederman {
334061a28784SEric W. Biederman 	char name[PROC_NUMBUF];
334119fd4bb2SEric W. Biederman 	int len = snprintf(name, sizeof(name), "%d", iter.tgid);
334261a28784SEric W. Biederman 	return proc_fill_cache(filp, dirent, filldir, name, len,
334319fd4bb2SEric W. Biederman 				proc_pid_instantiate, iter.task, NULL);
334461a28784SEric W. Biederman }
334561a28784SEric W. Biederman 
33460499680aSVasiliy Kulikov static int fake_filldir(void *buf, const char *name, int namelen,
33470499680aSVasiliy Kulikov 			loff_t offset, u64 ino, unsigned d_type)
33480499680aSVasiliy Kulikov {
33490499680aSVasiliy Kulikov 	return 0;
33500499680aSVasiliy Kulikov }
33510499680aSVasiliy Kulikov 
33521da177e4SLinus Torvalds /* for the /proc/ directory itself, after non-process stuff has been done */
33531da177e4SLinus Torvalds int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
33541da177e4SLinus Torvalds {
3355d8bdc59fSLinus Torvalds 	unsigned int nr;
3356d8bdc59fSLinus Torvalds 	struct task_struct *reaper;
335719fd4bb2SEric W. Biederman 	struct tgid_iter iter;
3358b488893aSPavel Emelyanov 	struct pid_namespace *ns;
33590499680aSVasiliy Kulikov 	filldir_t __filldir;
33601da177e4SLinus Torvalds 
3361d8bdc59fSLinus Torvalds 	if (filp->f_pos >= PID_MAX_LIMIT + TGID_OFFSET)
3362d8bdc59fSLinus Torvalds 		goto out_no_task;
3363d8bdc59fSLinus Torvalds 	nr = filp->f_pos - FIRST_PROCESS_ENTRY;
3364d8bdc59fSLinus Torvalds 
3365d8bdc59fSLinus Torvalds 	reaper = get_proc_task(filp->f_path.dentry->d_inode);
336661a28784SEric W. Biederman 	if (!reaper)
336761a28784SEric W. Biederman 		goto out_no_task;
336861a28784SEric W. Biederman 
33697bcd6b0eSEric W. Biederman 	for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
3370c5141e6dSEric Dumazet 		const struct pid_entry *p = &proc_base_stuff[nr];
337161a28784SEric W. Biederman 		if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
3372801199ceSEric W. Biederman 			goto out;
33731da177e4SLinus Torvalds 	}
33741da177e4SLinus Torvalds 
3375b488893aSPavel Emelyanov 	ns = filp->f_dentry->d_sb->s_fs_info;
337619fd4bb2SEric W. Biederman 	iter.task = NULL;
337719fd4bb2SEric W. Biederman 	iter.tgid = filp->f_pos - TGID_OFFSET;
337819fd4bb2SEric W. Biederman 	for (iter = next_tgid(ns, iter);
337919fd4bb2SEric W. Biederman 	     iter.task;
338019fd4bb2SEric W. Biederman 	     iter.tgid += 1, iter = next_tgid(ns, iter)) {
33810499680aSVasiliy Kulikov 		if (has_pid_permissions(ns, iter.task, 2))
33820499680aSVasiliy Kulikov 			__filldir = filldir;
33830499680aSVasiliy Kulikov 		else
33840499680aSVasiliy Kulikov 			__filldir = fake_filldir;
33850499680aSVasiliy Kulikov 
338619fd4bb2SEric W. Biederman 		filp->f_pos = iter.tgid + TGID_OFFSET;
33870499680aSVasiliy Kulikov 		if (proc_pid_fill_cache(filp, dirent, __filldir, iter) < 0) {
338819fd4bb2SEric W. Biederman 			put_task_struct(iter.task);
33890804ef4bSEric W. Biederman 			goto out;
33901da177e4SLinus Torvalds 		}
33911da177e4SLinus Torvalds 	}
33920804ef4bSEric W. Biederman 	filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
33930804ef4bSEric W. Biederman out:
339461a28784SEric W. Biederman 	put_task_struct(reaper);
339561a28784SEric W. Biederman out_no_task:
33961da177e4SLinus Torvalds 	return 0;
33971da177e4SLinus Torvalds }
33981da177e4SLinus Torvalds 
33990bc58a91SEric W. Biederman /*
340028a6d671SEric W. Biederman  * Tasks
340128a6d671SEric W. Biederman  */
3402c5141e6dSEric Dumazet static const struct pid_entry tid_base_stuff[] = {
3403631f9c18SAlexey Dobriyan 	DIR("fd",        S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
34043835541dSJerome Marchand 	DIR("fdinfo",    S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
34056b4e306aSEric W. Biederman 	DIR("ns",	 S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
3406631f9c18SAlexey Dobriyan 	REG("environ",   S_IRUSR, proc_environ_operations),
3407631f9c18SAlexey Dobriyan 	INF("auxv",      S_IRUSR, proc_pid_auxv),
3408631f9c18SAlexey Dobriyan 	ONE("status",    S_IRUGO, proc_pid_status),
3409a9712bc1SAl Viro 	ONE("personality", S_IRUGO, proc_pid_personality),
34103036e7b4SJiri Olsa 	INF("limits",	 S_IRUGO, proc_pid_limits),
341143ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
3412631f9c18SAlexey Dobriyan 	REG("sched",     S_IRUGO|S_IWUSR, proc_pid_sched_operations),
341343ae34cbSIngo Molnar #endif
34144614a696Sjohn stultz 	REG("comm",      S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
3415ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3416a9712bc1SAl Viro 	INF("syscall",   S_IRUGO, proc_pid_syscall),
3417ebcb6734SRoland McGrath #endif
3418631f9c18SAlexey Dobriyan 	INF("cmdline",   S_IRUGO, proc_pid_cmdline),
3419631f9c18SAlexey Dobriyan 	ONE("stat",      S_IRUGO, proc_tid_stat),
3420631f9c18SAlexey Dobriyan 	ONE("statm",     S_IRUGO, proc_pid_statm),
3421b7643757SSiddhesh Poyarekar 	REG("maps",      S_IRUGO, proc_tid_maps_operations),
342281841161SCyrill Gorcunov #ifdef CONFIG_CHECKPOINT_RESTORE
342381841161SCyrill Gorcunov 	REG("children",  S_IRUGO, proc_tid_children_operations),
342481841161SCyrill Gorcunov #endif
342528a6d671SEric W. Biederman #ifdef CONFIG_NUMA
3426b7643757SSiddhesh Poyarekar 	REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
342728a6d671SEric W. Biederman #endif
3428631f9c18SAlexey Dobriyan 	REG("mem",       S_IRUSR|S_IWUSR, proc_mem_operations),
3429631f9c18SAlexey Dobriyan 	LNK("cwd",       proc_cwd_link),
3430631f9c18SAlexey Dobriyan 	LNK("root",      proc_root_link),
3431631f9c18SAlexey Dobriyan 	LNK("exe",       proc_exe_link),
3432631f9c18SAlexey Dobriyan 	REG("mounts",    S_IRUGO, proc_mounts_operations),
3433631f9c18SAlexey Dobriyan 	REG("mountinfo",  S_IRUGO, proc_mountinfo_operations),
34341e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR
3435631f9c18SAlexey Dobriyan 	REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3436b7643757SSiddhesh Poyarekar 	REG("smaps",     S_IRUGO, proc_tid_smaps_operations),
3437ca6b0bf0SAl Viro 	REG("pagemap",    S_IRUGO, proc_pagemap_operations),
343828a6d671SEric W. Biederman #endif
343928a6d671SEric W. Biederman #ifdef CONFIG_SECURITY
3440631f9c18SAlexey Dobriyan 	DIR("attr",      S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
344128a6d671SEric W. Biederman #endif
344228a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS
3443631f9c18SAlexey Dobriyan 	INF("wchan",     S_IRUGO, proc_pid_wchan),
344428a6d671SEric W. Biederman #endif
34452ec220e2SKen Chen #ifdef CONFIG_STACKTRACE
3446a9712bc1SAl Viro 	ONE("stack",      S_IRUGO, proc_pid_stack),
344728a6d671SEric W. Biederman #endif
344828a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS
3449631f9c18SAlexey Dobriyan 	INF("schedstat", S_IRUGO, proc_pid_schedstat),
345028a6d671SEric W. Biederman #endif
34519745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP
3452631f9c18SAlexey Dobriyan 	REG("latency",  S_IRUGO, proc_lstats_operations),
34539745512cSArjan van de Ven #endif
34548793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET
3455631f9c18SAlexey Dobriyan 	REG("cpuset",    S_IRUGO, proc_cpuset_operations),
345628a6d671SEric W. Biederman #endif
3457a424316cSPaul Menage #ifdef CONFIG_CGROUPS
3458631f9c18SAlexey Dobriyan 	REG("cgroup",  S_IRUGO, proc_cgroup_operations),
3459a424316cSPaul Menage #endif
3460631f9c18SAlexey Dobriyan 	INF("oom_score", S_IRUGO, proc_oom_score),
3461631f9c18SAlexey Dobriyan 	REG("oom_adj",   S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
3462a63d83f4SDavid Rientjes 	REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
346328a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL
3464631f9c18SAlexey Dobriyan 	REG("loginuid",  S_IWUSR|S_IRUGO, proc_loginuid_operations),
346526ec3c64SAl Viro 	REG("sessionid",  S_IRUGO, proc_sessionid_operations),
346628a6d671SEric W. Biederman #endif
3467f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
3468631f9c18SAlexey Dobriyan 	REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3469f4f154fdSAkinobu Mita #endif
3470297c5d92SAndrea Righi #ifdef CONFIG_TASK_IO_ACCOUNTING
34711d1221f3SVasiliy Kulikov 	INF("io",	S_IRUSR, proc_tid_io_accounting),
3472297c5d92SAndrea Righi #endif
3473f133eccaSChris Metcalf #ifdef CONFIG_HARDWALL
3474f133eccaSChris Metcalf 	INF("hardwall",   S_IRUGO, proc_pid_hardwall),
3475f133eccaSChris Metcalf #endif
347622d917d8SEric W. Biederman #ifdef CONFIG_USER_NS
347722d917d8SEric W. Biederman 	REG("uid_map",    S_IRUGO|S_IWUSR, proc_uid_map_operations),
347822d917d8SEric W. Biederman 	REG("gid_map",    S_IRUGO|S_IWUSR, proc_gid_map_operations),
347922d917d8SEric W. Biederman #endif
348028a6d671SEric W. Biederman };
348128a6d671SEric W. Biederman 
348228a6d671SEric W. Biederman static int proc_tid_base_readdir(struct file * filp,
348328a6d671SEric W. Biederman 			     void * dirent, filldir_t filldir)
348428a6d671SEric W. Biederman {
348528a6d671SEric W. Biederman 	return proc_pident_readdir(filp,dirent,filldir,
348628a6d671SEric W. Biederman 				   tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
348728a6d671SEric W. Biederman }
348828a6d671SEric W. Biederman 
348900cd8dd3SAl Viro static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
349000cd8dd3SAl Viro {
34917bcd6b0eSEric W. Biederman 	return proc_pident_lookup(dir, dentry,
34927bcd6b0eSEric W. Biederman 				  tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
349328a6d671SEric W. Biederman }
349428a6d671SEric W. Biederman 
349500977a59SArjan van de Ven static const struct file_operations proc_tid_base_operations = {
349628a6d671SEric W. Biederman 	.read		= generic_read_dir,
349728a6d671SEric W. Biederman 	.readdir	= proc_tid_base_readdir,
34986038f373SArnd Bergmann 	.llseek		= default_llseek,
349928a6d671SEric W. Biederman };
350028a6d671SEric W. Biederman 
3501c5ef1c42SArjan van de Ven static const struct inode_operations proc_tid_base_inode_operations = {
350228a6d671SEric W. Biederman 	.lookup		= proc_tid_base_lookup,
350328a6d671SEric W. Biederman 	.getattr	= pid_getattr,
350428a6d671SEric W. Biederman 	.setattr	= proc_setattr,
350528a6d671SEric W. Biederman };
350628a6d671SEric W. Biederman 
3507444ceed8SEric W. Biederman static struct dentry *proc_task_instantiate(struct inode *dir,
3508c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
3509444ceed8SEric W. Biederman {
3510444ceed8SEric W. Biederman 	struct dentry *error = ERR_PTR(-ENOENT);
3511444ceed8SEric W. Biederman 	struct inode *inode;
351261a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
3513444ceed8SEric W. Biederman 
3514444ceed8SEric W. Biederman 	if (!inode)
3515444ceed8SEric W. Biederman 		goto out;
3516444ceed8SEric W. Biederman 	inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3517444ceed8SEric W. Biederman 	inode->i_op = &proc_tid_base_inode_operations;
3518444ceed8SEric W. Biederman 	inode->i_fop = &proc_tid_base_operations;
3519444ceed8SEric W. Biederman 	inode->i_flags|=S_IMMUTABLE;
3520aed54175SVegard Nossum 
3521bfe86848SMiklos Szeredi 	set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3522bfe86848SMiklos Szeredi 						  ARRAY_SIZE(tid_base_stuff)));
3523444ceed8SEric W. Biederman 
3524fb045adbSNick Piggin 	d_set_d_op(dentry, &pid_dentry_operations);
3525444ceed8SEric W. Biederman 
3526444ceed8SEric W. Biederman 	d_add(dentry, inode);
3527444ceed8SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
35280b728e19SAl Viro 	if (pid_revalidate(dentry, 0))
3529444ceed8SEric W. Biederman 		error = NULL;
3530444ceed8SEric W. Biederman out:
3531444ceed8SEric W. Biederman 	return error;
3532444ceed8SEric W. Biederman }
3533444ceed8SEric W. Biederman 
353400cd8dd3SAl Viro static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
353528a6d671SEric W. Biederman {
353628a6d671SEric W. Biederman 	struct dentry *result = ERR_PTR(-ENOENT);
353728a6d671SEric W. Biederman 	struct task_struct *task;
353828a6d671SEric W. Biederman 	struct task_struct *leader = get_proc_task(dir);
353928a6d671SEric W. Biederman 	unsigned tid;
3540b488893aSPavel Emelyanov 	struct pid_namespace *ns;
354128a6d671SEric W. Biederman 
354228a6d671SEric W. Biederman 	if (!leader)
354328a6d671SEric W. Biederman 		goto out_no_task;
354428a6d671SEric W. Biederman 
354528a6d671SEric W. Biederman 	tid = name_to_int(dentry);
354628a6d671SEric W. Biederman 	if (tid == ~0U)
354728a6d671SEric W. Biederman 		goto out;
354828a6d671SEric W. Biederman 
3549b488893aSPavel Emelyanov 	ns = dentry->d_sb->s_fs_info;
355028a6d671SEric W. Biederman 	rcu_read_lock();
3551b488893aSPavel Emelyanov 	task = find_task_by_pid_ns(tid, ns);
355228a6d671SEric W. Biederman 	if (task)
355328a6d671SEric W. Biederman 		get_task_struct(task);
355428a6d671SEric W. Biederman 	rcu_read_unlock();
355528a6d671SEric W. Biederman 	if (!task)
355628a6d671SEric W. Biederman 		goto out;
3557bac0abd6SPavel Emelyanov 	if (!same_thread_group(leader, task))
355828a6d671SEric W. Biederman 		goto out_drop_task;
355928a6d671SEric W. Biederman 
3560444ceed8SEric W. Biederman 	result = proc_task_instantiate(dir, dentry, task, NULL);
356128a6d671SEric W. Biederman out_drop_task:
356228a6d671SEric W. Biederman 	put_task_struct(task);
356328a6d671SEric W. Biederman out:
356428a6d671SEric W. Biederman 	put_task_struct(leader);
356528a6d671SEric W. Biederman out_no_task:
356628a6d671SEric W. Biederman 	return result;
356728a6d671SEric W. Biederman }
356828a6d671SEric W. Biederman 
356928a6d671SEric W. Biederman /*
35700bc58a91SEric W. Biederman  * Find the first tid of a thread group to return to user space.
35710bc58a91SEric W. Biederman  *
35720bc58a91SEric W. Biederman  * Usually this is just the thread group leader, but if the users
35730bc58a91SEric W. Biederman  * buffer was too small or there was a seek into the middle of the
35740bc58a91SEric W. Biederman  * directory we have more work todo.
35750bc58a91SEric W. Biederman  *
35760bc58a91SEric W. Biederman  * In the case of a short read we start with find_task_by_pid.
35770bc58a91SEric W. Biederman  *
35780bc58a91SEric W. Biederman  * In the case of a seek we start with the leader and walk nr
35790bc58a91SEric W. Biederman  * threads past it.
35800bc58a91SEric W. Biederman  */
3581cc288738SEric W. Biederman static struct task_struct *first_tid(struct task_struct *leader,
3582b488893aSPavel Emelyanov 		int tid, int nr, struct pid_namespace *ns)
35830bc58a91SEric W. Biederman {
3584a872ff0cSOleg Nesterov 	struct task_struct *pos;
35850bc58a91SEric W. Biederman 
3586cc288738SEric W. Biederman 	rcu_read_lock();
35870bc58a91SEric W. Biederman 	/* Attempt to start with the pid of a thread */
35880bc58a91SEric W. Biederman 	if (tid && (nr > 0)) {
3589b488893aSPavel Emelyanov 		pos = find_task_by_pid_ns(tid, ns);
3590a872ff0cSOleg Nesterov 		if (pos && (pos->group_leader == leader))
3591a872ff0cSOleg Nesterov 			goto found;
35920bc58a91SEric W. Biederman 	}
35930bc58a91SEric W. Biederman 
35940bc58a91SEric W. Biederman 	/* If nr exceeds the number of threads there is nothing todo */
35950bc58a91SEric W. Biederman 	pos = NULL;
3596a872ff0cSOleg Nesterov 	if (nr && nr >= get_nr_threads(leader))
3597a872ff0cSOleg Nesterov 		goto out;
3598a872ff0cSOleg Nesterov 
3599a872ff0cSOleg Nesterov 	/* If we haven't found our starting place yet start
3600a872ff0cSOleg Nesterov 	 * with the leader and walk nr threads forward.
3601a872ff0cSOleg Nesterov 	 */
3602a872ff0cSOleg Nesterov 	for (pos = leader; nr > 0; --nr) {
3603a872ff0cSOleg Nesterov 		pos = next_thread(pos);
3604a872ff0cSOleg Nesterov 		if (pos == leader) {
3605a872ff0cSOleg Nesterov 			pos = NULL;
3606a872ff0cSOleg Nesterov 			goto out;
3607a872ff0cSOleg Nesterov 		}
3608a872ff0cSOleg Nesterov 	}
3609a872ff0cSOleg Nesterov found:
3610a872ff0cSOleg Nesterov 	get_task_struct(pos);
3611a872ff0cSOleg Nesterov out:
3612cc288738SEric W. Biederman 	rcu_read_unlock();
36130bc58a91SEric W. Biederman 	return pos;
36140bc58a91SEric W. Biederman }
36150bc58a91SEric W. Biederman 
36160bc58a91SEric W. Biederman /*
36170bc58a91SEric W. Biederman  * Find the next thread in the thread list.
36180bc58a91SEric W. Biederman  * Return NULL if there is an error or no next thread.
36190bc58a91SEric W. Biederman  *
36200bc58a91SEric W. Biederman  * The reference to the input task_struct is released.
36210bc58a91SEric W. Biederman  */
36220bc58a91SEric W. Biederman static struct task_struct *next_tid(struct task_struct *start)
36230bc58a91SEric W. Biederman {
3624c1df7fb8SOleg Nesterov 	struct task_struct *pos = NULL;
3625cc288738SEric W. Biederman 	rcu_read_lock();
3626c1df7fb8SOleg Nesterov 	if (pid_alive(start)) {
36270bc58a91SEric W. Biederman 		pos = next_thread(start);
3628c1df7fb8SOleg Nesterov 		if (thread_group_leader(pos))
36290bc58a91SEric W. Biederman 			pos = NULL;
3630c1df7fb8SOleg Nesterov 		else
3631c1df7fb8SOleg Nesterov 			get_task_struct(pos);
3632c1df7fb8SOleg Nesterov 	}
3633cc288738SEric W. Biederman 	rcu_read_unlock();
36340bc58a91SEric W. Biederman 	put_task_struct(start);
36350bc58a91SEric W. Biederman 	return pos;
36360bc58a91SEric W. Biederman }
36370bc58a91SEric W. Biederman 
363861a28784SEric W. Biederman static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
363961a28784SEric W. Biederman 	struct task_struct *task, int tid)
364061a28784SEric W. Biederman {
364161a28784SEric W. Biederman 	char name[PROC_NUMBUF];
364261a28784SEric W. Biederman 	int len = snprintf(name, sizeof(name), "%d", tid);
364361a28784SEric W. Biederman 	return proc_fill_cache(filp, dirent, filldir, name, len,
364461a28784SEric W. Biederman 				proc_task_instantiate, task, NULL);
364561a28784SEric W. Biederman }
364661a28784SEric W. Biederman 
36471da177e4SLinus Torvalds /* for the /proc/TGID/task/ directories */
36481da177e4SLinus Torvalds static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
36491da177e4SLinus Torvalds {
36502fddfeefSJosef "Jeff" Sipek 	struct dentry *dentry = filp->f_path.dentry;
36511da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
36527d895244SGuillaume Chazarain 	struct task_struct *leader = NULL;
36530bc58a91SEric W. Biederman 	struct task_struct *task;
36541da177e4SLinus Torvalds 	int retval = -ENOENT;
36551da177e4SLinus Torvalds 	ino_t ino;
36560bc58a91SEric W. Biederman 	int tid;
3657b488893aSPavel Emelyanov 	struct pid_namespace *ns;
36581da177e4SLinus Torvalds 
36597d895244SGuillaume Chazarain 	task = get_proc_task(inode);
36607d895244SGuillaume Chazarain 	if (!task)
36617d895244SGuillaume Chazarain 		goto out_no_task;
36627d895244SGuillaume Chazarain 	rcu_read_lock();
36637d895244SGuillaume Chazarain 	if (pid_alive(task)) {
36647d895244SGuillaume Chazarain 		leader = task->group_leader;
36657d895244SGuillaume Chazarain 		get_task_struct(leader);
36667d895244SGuillaume Chazarain 	}
36677d895244SGuillaume Chazarain 	rcu_read_unlock();
36687d895244SGuillaume Chazarain 	put_task_struct(task);
366999f89551SEric W. Biederman 	if (!leader)
367099f89551SEric W. Biederman 		goto out_no_task;
36711da177e4SLinus Torvalds 	retval = 0;
36721da177e4SLinus Torvalds 
3673ee568b25SLinus Torvalds 	switch ((unsigned long)filp->f_pos) {
36741da177e4SLinus Torvalds 	case 0:
36751da177e4SLinus Torvalds 		ino = inode->i_ino;
3676ee6f779bSZhang Le 		if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) < 0)
36771da177e4SLinus Torvalds 			goto out;
3678ee6f779bSZhang Le 		filp->f_pos++;
36791da177e4SLinus Torvalds 		/* fall through */
36801da177e4SLinus Torvalds 	case 1:
36811da177e4SLinus Torvalds 		ino = parent_ino(dentry);
3682ee6f779bSZhang Le 		if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) < 0)
36831da177e4SLinus Torvalds 			goto out;
3684ee6f779bSZhang Le 		filp->f_pos++;
36851da177e4SLinus Torvalds 		/* fall through */
36861da177e4SLinus Torvalds 	}
36871da177e4SLinus Torvalds 
36880bc58a91SEric W. Biederman 	/* f_version caches the tgid value that the last readdir call couldn't
36890bc58a91SEric W. Biederman 	 * return. lseek aka telldir automagically resets f_version to 0.
36900bc58a91SEric W. Biederman 	 */
3691b488893aSPavel Emelyanov 	ns = filp->f_dentry->d_sb->s_fs_info;
36922b47c361SMathieu Desnoyers 	tid = (int)filp->f_version;
36930bc58a91SEric W. Biederman 	filp->f_version = 0;
3694ee6f779bSZhang Le 	for (task = first_tid(leader, tid, filp->f_pos - 2, ns);
36950bc58a91SEric W. Biederman 	     task;
3696ee6f779bSZhang Le 	     task = next_tid(task), filp->f_pos++) {
3697b488893aSPavel Emelyanov 		tid = task_pid_nr_ns(task, ns);
369861a28784SEric W. Biederman 		if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
36990bc58a91SEric W. Biederman 			/* returning this tgid failed, save it as the first
37000bc58a91SEric W. Biederman 			 * pid for the next readir call */
37012b47c361SMathieu Desnoyers 			filp->f_version = (u64)tid;
37020bc58a91SEric W. Biederman 			put_task_struct(task);
37031da177e4SLinus Torvalds 			break;
37040bc58a91SEric W. Biederman 		}
37051da177e4SLinus Torvalds 	}
37061da177e4SLinus Torvalds out:
370799f89551SEric W. Biederman 	put_task_struct(leader);
370899f89551SEric W. Biederman out_no_task:
37091da177e4SLinus Torvalds 	return retval;
37101da177e4SLinus Torvalds }
37116e66b52bSEric W. Biederman 
37126e66b52bSEric W. Biederman static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
37136e66b52bSEric W. Biederman {
37146e66b52bSEric W. Biederman 	struct inode *inode = dentry->d_inode;
371599f89551SEric W. Biederman 	struct task_struct *p = get_proc_task(inode);
37166e66b52bSEric W. Biederman 	generic_fillattr(inode, stat);
37176e66b52bSEric W. Biederman 
371899f89551SEric W. Biederman 	if (p) {
371999f89551SEric W. Biederman 		stat->nlink += get_nr_threads(p);
372099f89551SEric W. Biederman 		put_task_struct(p);
37216e66b52bSEric W. Biederman 	}
37226e66b52bSEric W. Biederman 
37236e66b52bSEric W. Biederman 	return 0;
37246e66b52bSEric W. Biederman }
372528a6d671SEric W. Biederman 
3726c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations = {
372728a6d671SEric W. Biederman 	.lookup		= proc_task_lookup,
372828a6d671SEric W. Biederman 	.getattr	= proc_task_getattr,
372928a6d671SEric W. Biederman 	.setattr	= proc_setattr,
37300499680aSVasiliy Kulikov 	.permission	= proc_pid_permission,
373128a6d671SEric W. Biederman };
373228a6d671SEric W. Biederman 
373300977a59SArjan van de Ven static const struct file_operations proc_task_operations = {
373428a6d671SEric W. Biederman 	.read		= generic_read_dir,
373528a6d671SEric W. Biederman 	.readdir	= proc_task_readdir,
37366038f373SArnd Bergmann 	.llseek		= default_llseek,
373728a6d671SEric W. Biederman };
3738