xref: /openbmc/linux/fs/proc/base.c (revision 640708a2cff7f81e246243b0073c66e6ece7e53e)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/fs/proc/base.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 1991, 1992 Linus Torvalds
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *  proc base directory handling functions
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  *  1999, Al Viro. Rewritten. Now it covers the whole per-process part.
91da177e4SLinus Torvalds  *  Instead of using magical inumbers to determine the kind of object
101da177e4SLinus Torvalds  *  we allocate and fill in-core inodes upon lookup. They don't even
111da177e4SLinus Torvalds  *  go into icache. We cache the reference to task_struct upon lookup too.
121da177e4SLinus Torvalds  *  Eventually it should become a filesystem in its own. We don't use the
131da177e4SLinus Torvalds  *  rest of procfs anymore.
14e070ad49SMauricio Lin  *
15e070ad49SMauricio Lin  *
16e070ad49SMauricio Lin  *  Changelog:
17e070ad49SMauricio Lin  *  17-Jan-2005
18e070ad49SMauricio Lin  *  Allan Bezerra
19e070ad49SMauricio Lin  *  Bruna Moreira <bruna.moreira@indt.org.br>
20e070ad49SMauricio Lin  *  Edjard Mota <edjard.mota@indt.org.br>
21e070ad49SMauricio Lin  *  Ilias Biris <ilias.biris@indt.org.br>
22e070ad49SMauricio Lin  *  Mauricio Lin <mauricio.lin@indt.org.br>
23e070ad49SMauricio Lin  *
24e070ad49SMauricio Lin  *  Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
25e070ad49SMauricio Lin  *
26e070ad49SMauricio Lin  *  A new process specific entry (smaps) included in /proc. It shows the
27e070ad49SMauricio Lin  *  size of rss for each memory area. The maps entry lacks information
28e070ad49SMauricio Lin  *  about physical memory size (rss) for each mapped file, i.e.,
29e070ad49SMauricio Lin  *  rss information for executables and library files.
30e070ad49SMauricio Lin  *  This additional information is useful for any tools that need to know
31e070ad49SMauricio Lin  *  about physical memory consumption for a process specific library.
32e070ad49SMauricio Lin  *
33e070ad49SMauricio Lin  *  Changelog:
34e070ad49SMauricio Lin  *  21-Feb-2005
35e070ad49SMauricio Lin  *  Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36e070ad49SMauricio Lin  *  Pud inclusion in the page table walking.
37e070ad49SMauricio Lin  *
38e070ad49SMauricio Lin  *  ChangeLog:
39e070ad49SMauricio Lin  *  10-Mar-2005
40e070ad49SMauricio Lin  *  10LE Instituto Nokia de Tecnologia - INdT:
41e070ad49SMauricio Lin  *  A better way to walks through the page table as suggested by Hugh Dickins.
42e070ad49SMauricio Lin  *
43e070ad49SMauricio Lin  *  Simo Piiroinen <simo.piiroinen@nokia.com>:
44e070ad49SMauricio Lin  *  Smaps information related to shared, private, clean and dirty pages.
45e070ad49SMauricio Lin  *
46e070ad49SMauricio Lin  *  Paul Mundt <paul.mundt@nokia.com>:
47e070ad49SMauricio Lin  *  Overall revision about smaps.
481da177e4SLinus Torvalds  */
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds #include <asm/uaccess.h>
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds #include <linux/errno.h>
531da177e4SLinus Torvalds #include <linux/time.h>
541da177e4SLinus Torvalds #include <linux/proc_fs.h>
551da177e4SLinus Torvalds #include <linux/stat.h>
565995477aSAndrea Righi #include <linux/task_io_accounting_ops.h>
571da177e4SLinus Torvalds #include <linux/init.h>
5816f7e0feSRandy Dunlap #include <linux/capability.h>
591da177e4SLinus Torvalds #include <linux/file.h>
609f3acc31SAl Viro #include <linux/fdtable.h>
611da177e4SLinus Torvalds #include <linux/string.h>
621da177e4SLinus Torvalds #include <linux/seq_file.h>
631da177e4SLinus Torvalds #include <linux/namei.h>
646b3286edSKirill Korotaev #include <linux/mnt_namespace.h>
651da177e4SLinus Torvalds #include <linux/mm.h>
66a63d83f4SDavid Rientjes #include <linux/swap.h>
67b835996fSDipankar Sarma #include <linux/rcupdate.h>
681da177e4SLinus Torvalds #include <linux/kallsyms.h>
692ec220e2SKen Chen #include <linux/stacktrace.h>
70d85f50d5SNeil Horman #include <linux/resource.h>
715096add8SKees Cook #include <linux/module.h>
721da177e4SLinus Torvalds #include <linux/mount.h>
731da177e4SLinus Torvalds #include <linux/security.h>
741da177e4SLinus Torvalds #include <linux/ptrace.h>
750d094efeSRoland McGrath #include <linux/tracehook.h>
76a424316cSPaul Menage #include <linux/cgroup.h>
771da177e4SLinus Torvalds #include <linux/cpuset.h>
781da177e4SLinus Torvalds #include <linux/audit.h>
795addc5ddSAl Viro #include <linux/poll.h>
801651e14eSSerge E. Hallyn #include <linux/nsproxy.h>
818ac773b4SAlexey Dobriyan #include <linux/oom.h>
823cb4a0bbSKawai, Hidehiro #include <linux/elf.h>
8360347f67SPavel Emelyanov #include <linux/pid_namespace.h>
845ad4e53bSAl Viro #include <linux/fs_struct.h>
855a0e3ad6STejun Heo #include <linux/slab.h>
86*640708a2SPavel Emelyanov #include <linux/flex_array.h>
87f133eccaSChris Metcalf #ifdef CONFIG_HARDWALL
88f133eccaSChris Metcalf #include <asm/hardwall.h>
89f133eccaSChris Metcalf #endif
9043d2b113SKAMEZAWA Hiroyuki #include <trace/events/oom.h>
911da177e4SLinus Torvalds #include "internal.h"
921da177e4SLinus Torvalds 
930f2fe20fSEric W. Biederman /* NOTE:
940f2fe20fSEric W. Biederman  *	Implementing inode permission operations in /proc is almost
950f2fe20fSEric W. Biederman  *	certainly an error.  Permission checks need to happen during
960f2fe20fSEric W. Biederman  *	each system call not at open time.  The reason is that most of
970f2fe20fSEric W. Biederman  *	what we wish to check for permissions in /proc varies at runtime.
980f2fe20fSEric W. Biederman  *
990f2fe20fSEric W. Biederman  *	The classic example of a problem is opening file descriptors
1000f2fe20fSEric W. Biederman  *	in /proc for a task before it execs a suid executable.
1010f2fe20fSEric W. Biederman  */
1020f2fe20fSEric W. Biederman 
1031da177e4SLinus Torvalds struct pid_entry {
1041da177e4SLinus Torvalds 	char *name;
105c5141e6dSEric Dumazet 	int len;
106d161a13fSAl Viro 	umode_t mode;
107c5ef1c42SArjan van de Ven 	const struct inode_operations *iop;
10800977a59SArjan van de Ven 	const struct file_operations *fop;
10920cdc894SEric W. Biederman 	union proc_op op;
1101da177e4SLinus Torvalds };
1111da177e4SLinus Torvalds 
11261a28784SEric W. Biederman #define NOD(NAME, MODE, IOP, FOP, OP) {			\
11320cdc894SEric W. Biederman 	.name = (NAME),					\
114c5141e6dSEric Dumazet 	.len  = sizeof(NAME) - 1,			\
11520cdc894SEric W. Biederman 	.mode = MODE,					\
11620cdc894SEric W. Biederman 	.iop  = IOP,					\
11720cdc894SEric W. Biederman 	.fop  = FOP,					\
11820cdc894SEric W. Biederman 	.op   = OP,					\
11920cdc894SEric W. Biederman }
12020cdc894SEric W. Biederman 
121631f9c18SAlexey Dobriyan #define DIR(NAME, MODE, iops, fops)	\
122631f9c18SAlexey Dobriyan 	NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
123631f9c18SAlexey Dobriyan #define LNK(NAME, get_link)					\
12461a28784SEric W. Biederman 	NOD(NAME, (S_IFLNK|S_IRWXUGO),				\
12520cdc894SEric W. Biederman 		&proc_pid_link_inode_operations, NULL,		\
126631f9c18SAlexey Dobriyan 		{ .proc_get_link = get_link } )
127631f9c18SAlexey Dobriyan #define REG(NAME, MODE, fops)				\
128631f9c18SAlexey Dobriyan 	NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
129631f9c18SAlexey Dobriyan #define INF(NAME, MODE, read)				\
13061a28784SEric W. Biederman 	NOD(NAME, (S_IFREG|(MODE)), 			\
13120cdc894SEric W. Biederman 		NULL, &proc_info_file_operations,	\
132631f9c18SAlexey Dobriyan 		{ .proc_read = read } )
133631f9c18SAlexey Dobriyan #define ONE(NAME, MODE, show)				\
134be614086SEric W. Biederman 	NOD(NAME, (S_IFREG|(MODE)), 			\
135be614086SEric W. Biederman 		NULL, &proc_single_file_operations,	\
136631f9c18SAlexey Dobriyan 		{ .proc_show = show } )
1371da177e4SLinus Torvalds 
138*640708a2SPavel Emelyanov static int proc_fd_permission(struct inode *inode, int mask);
139*640708a2SPavel Emelyanov 
140aed54175SVegard Nossum /*
141aed54175SVegard Nossum  * Count the number of hardlinks for the pid_entry table, excluding the .
142aed54175SVegard Nossum  * and .. links.
143aed54175SVegard Nossum  */
144aed54175SVegard Nossum static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
145aed54175SVegard Nossum 	unsigned int n)
146aed54175SVegard Nossum {
147aed54175SVegard Nossum 	unsigned int i;
148aed54175SVegard Nossum 	unsigned int count;
149aed54175SVegard Nossum 
150aed54175SVegard Nossum 	count = 0;
151aed54175SVegard Nossum 	for (i = 0; i < n; ++i) {
152aed54175SVegard Nossum 		if (S_ISDIR(entries[i].mode))
153aed54175SVegard Nossum 			++count;
154aed54175SVegard Nossum 	}
155aed54175SVegard Nossum 
156aed54175SVegard Nossum 	return count;
157aed54175SVegard Nossum }
158aed54175SVegard Nossum 
159f7ad3c6bSMiklos Szeredi static int get_task_root(struct task_struct *task, struct path *root)
1601da177e4SLinus Torvalds {
1617c2c7d99SHugh Dickins 	int result = -ENOENT;
1627c2c7d99SHugh Dickins 
1630494f6ecSMiklos Szeredi 	task_lock(task);
164f7ad3c6bSMiklos Szeredi 	if (task->fs) {
165f7ad3c6bSMiklos Szeredi 		get_fs_root(task->fs, root);
1667c2c7d99SHugh Dickins 		result = 0;
1677c2c7d99SHugh Dickins 	}
1680494f6ecSMiklos Szeredi 	task_unlock(task);
1697c2c7d99SHugh Dickins 	return result;
1700494f6ecSMiklos Szeredi }
1710494f6ecSMiklos Szeredi 
1727773fbc5SCyrill Gorcunov static int proc_cwd_link(struct dentry *dentry, struct path *path)
1730494f6ecSMiklos Szeredi {
1747773fbc5SCyrill Gorcunov 	struct task_struct *task = get_proc_task(dentry->d_inode);
1750494f6ecSMiklos Szeredi 	int result = -ENOENT;
17699f89551SEric W. Biederman 
17799f89551SEric W. Biederman 	if (task) {
178f7ad3c6bSMiklos Szeredi 		task_lock(task);
179f7ad3c6bSMiklos Szeredi 		if (task->fs) {
180f7ad3c6bSMiklos Szeredi 			get_fs_pwd(task->fs, path);
181f7ad3c6bSMiklos Szeredi 			result = 0;
182f7ad3c6bSMiklos Szeredi 		}
183f7ad3c6bSMiklos Szeredi 		task_unlock(task);
18499f89551SEric W. Biederman 		put_task_struct(task);
18599f89551SEric W. Biederman 	}
1861da177e4SLinus Torvalds 	return result;
1871da177e4SLinus Torvalds }
1881da177e4SLinus Torvalds 
1897773fbc5SCyrill Gorcunov static int proc_root_link(struct dentry *dentry, struct path *path)
1901da177e4SLinus Torvalds {
1917773fbc5SCyrill Gorcunov 	struct task_struct *task = get_proc_task(dentry->d_inode);
1921da177e4SLinus Torvalds 	int result = -ENOENT;
19399f89551SEric W. Biederman 
19499f89551SEric W. Biederman 	if (task) {
195f7ad3c6bSMiklos Szeredi 		result = get_task_root(task, path);
19699f89551SEric W. Biederman 		put_task_struct(task);
19799f89551SEric W. Biederman 	}
1981da177e4SLinus Torvalds 	return result;
1991da177e4SLinus Torvalds }
2001da177e4SLinus Torvalds 
2018b0db9dbSStephen Wilson static struct mm_struct *__check_mem_permission(struct task_struct *task)
202638fa202SRoland McGrath {
2038b0db9dbSStephen Wilson 	struct mm_struct *mm;
2048b0db9dbSStephen Wilson 
2058b0db9dbSStephen Wilson 	mm = get_task_mm(task);
2068b0db9dbSStephen Wilson 	if (!mm)
2078b0db9dbSStephen Wilson 		return ERR_PTR(-EINVAL);
2088b0db9dbSStephen Wilson 
209638fa202SRoland McGrath 	/*
210638fa202SRoland McGrath 	 * A task can always look at itself, in case it chooses
211638fa202SRoland McGrath 	 * to use system calls instead of load instructions.
212638fa202SRoland McGrath 	 */
213638fa202SRoland McGrath 	if (task == current)
2148b0db9dbSStephen Wilson 		return mm;
215638fa202SRoland McGrath 
216638fa202SRoland McGrath 	/*
217638fa202SRoland McGrath 	 * If current is actively ptrace'ing, and would also be
218638fa202SRoland McGrath 	 * permitted to freshly attach with ptrace now, permit it.
219638fa202SRoland McGrath 	 */
2200d094efeSRoland McGrath 	if (task_is_stopped_or_traced(task)) {
2210d094efeSRoland McGrath 		int match;
2220d094efeSRoland McGrath 		rcu_read_lock();
22306d98473STejun Heo 		match = (ptrace_parent(task) == current);
2240d094efeSRoland McGrath 		rcu_read_unlock();
2250d094efeSRoland McGrath 		if (match && ptrace_may_access(task, PTRACE_MODE_ATTACH))
2268b0db9dbSStephen Wilson 			return mm;
2270d094efeSRoland McGrath 	}
228638fa202SRoland McGrath 
229638fa202SRoland McGrath 	/*
230638fa202SRoland McGrath 	 * No one else is allowed.
231638fa202SRoland McGrath 	 */
2328b0db9dbSStephen Wilson 	mmput(mm);
2338b0db9dbSStephen Wilson 	return ERR_PTR(-EPERM);
234638fa202SRoland McGrath }
2351da177e4SLinus Torvalds 
23618f661bcSStephen Wilson /*
2378b0db9dbSStephen Wilson  * If current may access user memory in @task return a reference to the
2388b0db9dbSStephen Wilson  * corresponding mm, otherwise ERR_PTR.
23918f661bcSStephen Wilson  */
2408b0db9dbSStephen Wilson static struct mm_struct *check_mem_permission(struct task_struct *task)
24118f661bcSStephen Wilson {
2428b0db9dbSStephen Wilson 	struct mm_struct *mm;
24318f661bcSStephen Wilson 	int err;
24418f661bcSStephen Wilson 
24518f661bcSStephen Wilson 	/*
24618f661bcSStephen Wilson 	 * Avoid racing if task exec's as we might get a new mm but validate
24718f661bcSStephen Wilson 	 * against old credentials.
24818f661bcSStephen Wilson 	 */
24918f661bcSStephen Wilson 	err = mutex_lock_killable(&task->signal->cred_guard_mutex);
25018f661bcSStephen Wilson 	if (err)
2518b0db9dbSStephen Wilson 		return ERR_PTR(err);
25218f661bcSStephen Wilson 
2538b0db9dbSStephen Wilson 	mm = __check_mem_permission(task);
25418f661bcSStephen Wilson 	mutex_unlock(&task->signal->cred_guard_mutex);
25518f661bcSStephen Wilson 
2568b0db9dbSStephen Wilson 	return mm;
2571da177e4SLinus Torvalds }
2581da177e4SLinus Torvalds 
259831830b5SAl Viro struct mm_struct *mm_for_maps(struct task_struct *task)
260831830b5SAl Viro {
261704b836cSOleg Nesterov 	struct mm_struct *mm;
262ec6fd8a4SAl Viro 	int err;
26300f89d21SOleg Nesterov 
264ec6fd8a4SAl Viro 	err =  mutex_lock_killable(&task->signal->cred_guard_mutex);
265ec6fd8a4SAl Viro 	if (err)
266ec6fd8a4SAl Viro 		return ERR_PTR(err);
267704b836cSOleg Nesterov 
268704b836cSOleg Nesterov 	mm = get_task_mm(task);
269704b836cSOleg Nesterov 	if (mm && mm != current->mm &&
270704b836cSOleg Nesterov 			!ptrace_may_access(task, PTRACE_MODE_READ)) {
271831830b5SAl Viro 		mmput(mm);
272ec6fd8a4SAl Viro 		mm = ERR_PTR(-EACCES);
273831830b5SAl Viro 	}
2749b1bf12dSKOSAKI Motohiro 	mutex_unlock(&task->signal->cred_guard_mutex);
275704b836cSOleg Nesterov 
27613f0feafSOleg Nesterov 	return mm;
27713f0feafSOleg Nesterov }
278831830b5SAl Viro 
2791da177e4SLinus Torvalds static int proc_pid_cmdline(struct task_struct *task, char * buffer)
2801da177e4SLinus Torvalds {
2811da177e4SLinus Torvalds 	int res = 0;
2821da177e4SLinus Torvalds 	unsigned int len;
2831da177e4SLinus Torvalds 	struct mm_struct *mm = get_task_mm(task);
2841da177e4SLinus Torvalds 	if (!mm)
2851da177e4SLinus Torvalds 		goto out;
2861da177e4SLinus Torvalds 	if (!mm->arg_end)
2871da177e4SLinus Torvalds 		goto out_mm;	/* Shh! No looking before we're done */
2881da177e4SLinus Torvalds 
2891da177e4SLinus Torvalds  	len = mm->arg_end - mm->arg_start;
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds 	if (len > PAGE_SIZE)
2921da177e4SLinus Torvalds 		len = PAGE_SIZE;
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds 	res = access_process_vm(task, mm->arg_start, buffer, len, 0);
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds 	// If the nul at the end of args has been overwritten, then
2971da177e4SLinus Torvalds 	// assume application is using setproctitle(3).
2981da177e4SLinus Torvalds 	if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
2991da177e4SLinus Torvalds 		len = strnlen(buffer, res);
3001da177e4SLinus Torvalds 		if (len < res) {
3011da177e4SLinus Torvalds 		    res = len;
3021da177e4SLinus Torvalds 		} else {
3031da177e4SLinus Torvalds 			len = mm->env_end - mm->env_start;
3041da177e4SLinus Torvalds 			if (len > PAGE_SIZE - res)
3051da177e4SLinus Torvalds 				len = PAGE_SIZE - res;
3061da177e4SLinus Torvalds 			res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
3071da177e4SLinus Torvalds 			res = strnlen(buffer, res);
3081da177e4SLinus Torvalds 		}
3091da177e4SLinus Torvalds 	}
3101da177e4SLinus Torvalds out_mm:
3111da177e4SLinus Torvalds 	mmput(mm);
3121da177e4SLinus Torvalds out:
3131da177e4SLinus Torvalds 	return res;
3141da177e4SLinus Torvalds }
3151da177e4SLinus Torvalds 
3161da177e4SLinus Torvalds static int proc_pid_auxv(struct task_struct *task, char *buffer)
3171da177e4SLinus Torvalds {
3182fadaef4SAl Viro 	struct mm_struct *mm = mm_for_maps(task);
3192fadaef4SAl Viro 	int res = PTR_ERR(mm);
3202fadaef4SAl Viro 	if (mm && !IS_ERR(mm)) {
3211da177e4SLinus Torvalds 		unsigned int nwords = 0;
322dfe6b7d9SHannes Eder 		do {
3231da177e4SLinus Torvalds 			nwords += 2;
324dfe6b7d9SHannes Eder 		} while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
3251da177e4SLinus Torvalds 		res = nwords * sizeof(mm->saved_auxv[0]);
3261da177e4SLinus Torvalds 		if (res > PAGE_SIZE)
3271da177e4SLinus Torvalds 			res = PAGE_SIZE;
3281da177e4SLinus Torvalds 		memcpy(buffer, mm->saved_auxv, res);
3291da177e4SLinus Torvalds 		mmput(mm);
3301da177e4SLinus Torvalds 	}
3311da177e4SLinus Torvalds 	return res;
3321da177e4SLinus Torvalds }
3331da177e4SLinus Torvalds 
3341da177e4SLinus Torvalds 
3351da177e4SLinus Torvalds #ifdef CONFIG_KALLSYMS
3361da177e4SLinus Torvalds /*
3371da177e4SLinus Torvalds  * Provides a wchan file via kallsyms in a proper one-value-per-file format.
3381da177e4SLinus Torvalds  * Returns the resolved symbol.  If that fails, simply return the address.
3391da177e4SLinus Torvalds  */
3401da177e4SLinus Torvalds static int proc_pid_wchan(struct task_struct *task, char *buffer)
3411da177e4SLinus Torvalds {
342ffb45122SAlexey Dobriyan 	unsigned long wchan;
3439281aceaSTejun Heo 	char symname[KSYM_NAME_LEN];
3441da177e4SLinus Torvalds 
3451da177e4SLinus Torvalds 	wchan = get_wchan(task);
3461da177e4SLinus Torvalds 
3479d65cb4aSAlexey Dobriyan 	if (lookup_symbol_name(wchan, symname) < 0)
348f83ce3e6SJake Edge 		if (!ptrace_may_access(task, PTRACE_MODE_READ))
349f83ce3e6SJake Edge 			return 0;
350f83ce3e6SJake Edge 		else
3511da177e4SLinus Torvalds 			return sprintf(buffer, "%lu", wchan);
3529d65cb4aSAlexey Dobriyan 	else
3539d65cb4aSAlexey Dobriyan 		return sprintf(buffer, "%s", symname);
3541da177e4SLinus Torvalds }
3551da177e4SLinus Torvalds #endif /* CONFIG_KALLSYMS */
3561da177e4SLinus Torvalds 
357a9712bc1SAl Viro static int lock_trace(struct task_struct *task)
358a9712bc1SAl Viro {
359a9712bc1SAl Viro 	int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
360a9712bc1SAl Viro 	if (err)
361a9712bc1SAl Viro 		return err;
362a9712bc1SAl Viro 	if (!ptrace_may_access(task, PTRACE_MODE_ATTACH)) {
363a9712bc1SAl Viro 		mutex_unlock(&task->signal->cred_guard_mutex);
364a9712bc1SAl Viro 		return -EPERM;
365a9712bc1SAl Viro 	}
366a9712bc1SAl Viro 	return 0;
367a9712bc1SAl Viro }
368a9712bc1SAl Viro 
369a9712bc1SAl Viro static void unlock_trace(struct task_struct *task)
370a9712bc1SAl Viro {
371a9712bc1SAl Viro 	mutex_unlock(&task->signal->cred_guard_mutex);
372a9712bc1SAl Viro }
373a9712bc1SAl Viro 
3742ec220e2SKen Chen #ifdef CONFIG_STACKTRACE
3752ec220e2SKen Chen 
3762ec220e2SKen Chen #define MAX_STACK_TRACE_DEPTH	64
3772ec220e2SKen Chen 
3782ec220e2SKen Chen static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
3792ec220e2SKen Chen 			  struct pid *pid, struct task_struct *task)
3802ec220e2SKen Chen {
3812ec220e2SKen Chen 	struct stack_trace trace;
3822ec220e2SKen Chen 	unsigned long *entries;
383a9712bc1SAl Viro 	int err;
3842ec220e2SKen Chen 	int i;
3852ec220e2SKen Chen 
3862ec220e2SKen Chen 	entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
3872ec220e2SKen Chen 	if (!entries)
3882ec220e2SKen Chen 		return -ENOMEM;
3892ec220e2SKen Chen 
3902ec220e2SKen Chen 	trace.nr_entries	= 0;
3912ec220e2SKen Chen 	trace.max_entries	= MAX_STACK_TRACE_DEPTH;
3922ec220e2SKen Chen 	trace.entries		= entries;
3932ec220e2SKen Chen 	trace.skip		= 0;
394a9712bc1SAl Viro 
395a9712bc1SAl Viro 	err = lock_trace(task);
396a9712bc1SAl Viro 	if (!err) {
3972ec220e2SKen Chen 		save_stack_trace_tsk(task, &trace);
3982ec220e2SKen Chen 
3992ec220e2SKen Chen 		for (i = 0; i < trace.nr_entries; i++) {
40051e03149SKonstantin Khlebnikov 			seq_printf(m, "[<%pK>] %pS\n",
4012ec220e2SKen Chen 				   (void *)entries[i], (void *)entries[i]);
4022ec220e2SKen Chen 		}
403a9712bc1SAl Viro 		unlock_trace(task);
404a9712bc1SAl Viro 	}
4052ec220e2SKen Chen 	kfree(entries);
4062ec220e2SKen Chen 
407a9712bc1SAl Viro 	return err;
4082ec220e2SKen Chen }
4092ec220e2SKen Chen #endif
4102ec220e2SKen Chen 
4111da177e4SLinus Torvalds #ifdef CONFIG_SCHEDSTATS
4121da177e4SLinus Torvalds /*
4131da177e4SLinus Torvalds  * Provides /proc/PID/schedstat
4141da177e4SLinus Torvalds  */
4151da177e4SLinus Torvalds static int proc_pid_schedstat(struct task_struct *task, char *buffer)
4161da177e4SLinus Torvalds {
417172ba844SBalbir Singh 	return sprintf(buffer, "%llu %llu %lu\n",
418826e08b0SIngo Molnar 			(unsigned long long)task->se.sum_exec_runtime,
419826e08b0SIngo Molnar 			(unsigned long long)task->sched_info.run_delay,
4202d72376bSIngo Molnar 			task->sched_info.pcount);
4211da177e4SLinus Torvalds }
4221da177e4SLinus Torvalds #endif
4231da177e4SLinus Torvalds 
4249745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP
4259745512cSArjan van de Ven static int lstats_show_proc(struct seq_file *m, void *v)
4269745512cSArjan van de Ven {
4279745512cSArjan van de Ven 	int i;
42813d77c37SHiroshi Shimamoto 	struct inode *inode = m->private;
42913d77c37SHiroshi Shimamoto 	struct task_struct *task = get_proc_task(inode);
4309745512cSArjan van de Ven 
43113d77c37SHiroshi Shimamoto 	if (!task)
43213d77c37SHiroshi Shimamoto 		return -ESRCH;
43313d77c37SHiroshi Shimamoto 	seq_puts(m, "Latency Top version : v0.1\n");
4349745512cSArjan van de Ven 	for (i = 0; i < 32; i++) {
43534e49d4fSJoe Perches 		struct latency_record *lr = &task->latency_record[i];
43634e49d4fSJoe Perches 		if (lr->backtrace[0]) {
4379745512cSArjan van de Ven 			int q;
4389745512cSArjan van de Ven 			seq_printf(m, "%i %li %li",
43934e49d4fSJoe Perches 				   lr->count, lr->time, lr->max);
4409745512cSArjan van de Ven 			for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
44134e49d4fSJoe Perches 				unsigned long bt = lr->backtrace[q];
44234e49d4fSJoe Perches 				if (!bt)
4439745512cSArjan van de Ven 					break;
44434e49d4fSJoe Perches 				if (bt == ULONG_MAX)
4459745512cSArjan van de Ven 					break;
44634e49d4fSJoe Perches 				seq_printf(m, " %ps", (void *)bt);
4479745512cSArjan van de Ven 			}
4489d6de12fSAlexey Dobriyan 			seq_putc(m, '\n');
4499745512cSArjan van de Ven 		}
4509745512cSArjan van de Ven 
4519745512cSArjan van de Ven 	}
45213d77c37SHiroshi Shimamoto 	put_task_struct(task);
4539745512cSArjan van de Ven 	return 0;
4549745512cSArjan van de Ven }
4559745512cSArjan van de Ven 
4569745512cSArjan van de Ven static int lstats_open(struct inode *inode, struct file *file)
4579745512cSArjan van de Ven {
45813d77c37SHiroshi Shimamoto 	return single_open(file, lstats_show_proc, inode);
459d6643d12SHiroshi Shimamoto }
460d6643d12SHiroshi Shimamoto 
4619745512cSArjan van de Ven static ssize_t lstats_write(struct file *file, const char __user *buf,
4629745512cSArjan van de Ven 			    size_t count, loff_t *offs)
4639745512cSArjan van de Ven {
46413d77c37SHiroshi Shimamoto 	struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
4659745512cSArjan van de Ven 
46613d77c37SHiroshi Shimamoto 	if (!task)
46713d77c37SHiroshi Shimamoto 		return -ESRCH;
4689745512cSArjan van de Ven 	clear_all_latency_tracing(task);
46913d77c37SHiroshi Shimamoto 	put_task_struct(task);
4709745512cSArjan van de Ven 
4719745512cSArjan van de Ven 	return count;
4729745512cSArjan van de Ven }
4739745512cSArjan van de Ven 
4749745512cSArjan van de Ven static const struct file_operations proc_lstats_operations = {
4759745512cSArjan van de Ven 	.open		= lstats_open,
4769745512cSArjan van de Ven 	.read		= seq_read,
4779745512cSArjan van de Ven 	.write		= lstats_write,
4789745512cSArjan van de Ven 	.llseek		= seq_lseek,
47913d77c37SHiroshi Shimamoto 	.release	= single_release,
4809745512cSArjan van de Ven };
4819745512cSArjan van de Ven 
4829745512cSArjan van de Ven #endif
4839745512cSArjan van de Ven 
4841da177e4SLinus Torvalds static int proc_oom_score(struct task_struct *task, char *buffer)
4851da177e4SLinus Torvalds {
486b95c35e7SOleg Nesterov 	unsigned long points = 0;
4871da177e4SLinus Torvalds 
48819c5d45aSAlexey Dobriyan 	read_lock(&tasklist_lock);
489b95c35e7SOleg Nesterov 	if (pid_alive(task))
490a63d83f4SDavid Rientjes 		points = oom_badness(task, NULL, NULL,
491a63d83f4SDavid Rientjes 					totalram_pages + total_swap_pages);
49219c5d45aSAlexey Dobriyan 	read_unlock(&tasklist_lock);
4931da177e4SLinus Torvalds 	return sprintf(buffer, "%lu\n", points);
4941da177e4SLinus Torvalds }
4951da177e4SLinus Torvalds 
496d85f50d5SNeil Horman struct limit_names {
497d85f50d5SNeil Horman 	char *name;
498d85f50d5SNeil Horman 	char *unit;
499d85f50d5SNeil Horman };
500d85f50d5SNeil Horman 
501d85f50d5SNeil Horman static const struct limit_names lnames[RLIM_NLIMITS] = {
502cff4edb5SKees Cook 	[RLIMIT_CPU] = {"Max cpu time", "seconds"},
503d85f50d5SNeil Horman 	[RLIMIT_FSIZE] = {"Max file size", "bytes"},
504d85f50d5SNeil Horman 	[RLIMIT_DATA] = {"Max data size", "bytes"},
505d85f50d5SNeil Horman 	[RLIMIT_STACK] = {"Max stack size", "bytes"},
506d85f50d5SNeil Horman 	[RLIMIT_CORE] = {"Max core file size", "bytes"},
507d85f50d5SNeil Horman 	[RLIMIT_RSS] = {"Max resident set", "bytes"},
508d85f50d5SNeil Horman 	[RLIMIT_NPROC] = {"Max processes", "processes"},
509d85f50d5SNeil Horman 	[RLIMIT_NOFILE] = {"Max open files", "files"},
510d85f50d5SNeil Horman 	[RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
511d85f50d5SNeil Horman 	[RLIMIT_AS] = {"Max address space", "bytes"},
512d85f50d5SNeil Horman 	[RLIMIT_LOCKS] = {"Max file locks", "locks"},
513d85f50d5SNeil Horman 	[RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
514d85f50d5SNeil Horman 	[RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
515d85f50d5SNeil Horman 	[RLIMIT_NICE] = {"Max nice priority", NULL},
516d85f50d5SNeil Horman 	[RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
5178808117cSEugene Teo 	[RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
518d85f50d5SNeil Horman };
519d85f50d5SNeil Horman 
520d85f50d5SNeil Horman /* Display limits for a process */
521d85f50d5SNeil Horman static int proc_pid_limits(struct task_struct *task, char *buffer)
522d85f50d5SNeil Horman {
523d85f50d5SNeil Horman 	unsigned int i;
524d85f50d5SNeil Horman 	int count = 0;
525d85f50d5SNeil Horman 	unsigned long flags;
526d85f50d5SNeil Horman 	char *bufptr = buffer;
527d85f50d5SNeil Horman 
528d85f50d5SNeil Horman 	struct rlimit rlim[RLIM_NLIMITS];
529d85f50d5SNeil Horman 
530a6bebbc8SLai Jiangshan 	if (!lock_task_sighand(task, &flags))
531d85f50d5SNeil Horman 		return 0;
532d85f50d5SNeil Horman 	memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
533d85f50d5SNeil Horman 	unlock_task_sighand(task, &flags);
534d85f50d5SNeil Horman 
535d85f50d5SNeil Horman 	/*
536d85f50d5SNeil Horman 	 * print the file header
537d85f50d5SNeil Horman 	 */
538d85f50d5SNeil Horman 	count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
539d85f50d5SNeil Horman 			"Limit", "Soft Limit", "Hard Limit", "Units");
540d85f50d5SNeil Horman 
541d85f50d5SNeil Horman 	for (i = 0; i < RLIM_NLIMITS; i++) {
542d85f50d5SNeil Horman 		if (rlim[i].rlim_cur == RLIM_INFINITY)
543d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-25s %-20s ",
544d85f50d5SNeil Horman 					 lnames[i].name, "unlimited");
545d85f50d5SNeil Horman 		else
546d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-25s %-20lu ",
547d85f50d5SNeil Horman 					 lnames[i].name, rlim[i].rlim_cur);
548d85f50d5SNeil Horman 
549d85f50d5SNeil Horman 		if (rlim[i].rlim_max == RLIM_INFINITY)
550d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-20s ", "unlimited");
551d85f50d5SNeil Horman 		else
552d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-20lu ",
553d85f50d5SNeil Horman 					 rlim[i].rlim_max);
554d85f50d5SNeil Horman 
555d85f50d5SNeil Horman 		if (lnames[i].unit)
556d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-10s\n",
557d85f50d5SNeil Horman 					 lnames[i].unit);
558d85f50d5SNeil Horman 		else
559d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "\n");
560d85f50d5SNeil Horman 	}
561d85f50d5SNeil Horman 
562d85f50d5SNeil Horman 	return count;
563d85f50d5SNeil Horman }
564d85f50d5SNeil Horman 
565ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
566ebcb6734SRoland McGrath static int proc_pid_syscall(struct task_struct *task, char *buffer)
567ebcb6734SRoland McGrath {
568ebcb6734SRoland McGrath 	long nr;
569ebcb6734SRoland McGrath 	unsigned long args[6], sp, pc;
570a9712bc1SAl Viro 	int res = lock_trace(task);
571a9712bc1SAl Viro 	if (res)
572a9712bc1SAl Viro 		return res;
573ebcb6734SRoland McGrath 
574ebcb6734SRoland McGrath 	if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
575a9712bc1SAl Viro 		res = sprintf(buffer, "running\n");
576a9712bc1SAl Viro 	else if (nr < 0)
577a9712bc1SAl Viro 		res = sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
578a9712bc1SAl Viro 	else
579a9712bc1SAl Viro 		res = sprintf(buffer,
580ebcb6734SRoland McGrath 		       "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
581ebcb6734SRoland McGrath 		       nr,
582ebcb6734SRoland McGrath 		       args[0], args[1], args[2], args[3], args[4], args[5],
583ebcb6734SRoland McGrath 		       sp, pc);
584a9712bc1SAl Viro 	unlock_trace(task);
585a9712bc1SAl Viro 	return res;
586ebcb6734SRoland McGrath }
587ebcb6734SRoland McGrath #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
588ebcb6734SRoland McGrath 
5891da177e4SLinus Torvalds /************************************************************************/
5901da177e4SLinus Torvalds /*                       Here the fs part begins                        */
5911da177e4SLinus Torvalds /************************************************************************/
5921da177e4SLinus Torvalds 
5931da177e4SLinus Torvalds /* permission checks */
594778c1144SEric W. Biederman static int proc_fd_access_allowed(struct inode *inode)
5951da177e4SLinus Torvalds {
596778c1144SEric W. Biederman 	struct task_struct *task;
597778c1144SEric W. Biederman 	int allowed = 0;
598df26c40eSEric W. Biederman 	/* Allow access to a task's file descriptors if it is us or we
599df26c40eSEric W. Biederman 	 * may use ptrace attach to the process and find out that
600df26c40eSEric W. Biederman 	 * information.
601778c1144SEric W. Biederman 	 */
602778c1144SEric W. Biederman 	task = get_proc_task(inode);
603df26c40eSEric W. Biederman 	if (task) {
604006ebb40SStephen Smalley 		allowed = ptrace_may_access(task, PTRACE_MODE_READ);
605778c1144SEric W. Biederman 		put_task_struct(task);
606df26c40eSEric W. Biederman 	}
607778c1144SEric W. Biederman 	return allowed;
6081da177e4SLinus Torvalds }
6091da177e4SLinus Torvalds 
6106b4e306aSEric W. Biederman int proc_setattr(struct dentry *dentry, struct iattr *attr)
6116d76fa58SLinus Torvalds {
6126d76fa58SLinus Torvalds 	int error;
6136d76fa58SLinus Torvalds 	struct inode *inode = dentry->d_inode;
6146d76fa58SLinus Torvalds 
6156d76fa58SLinus Torvalds 	if (attr->ia_valid & ATTR_MODE)
6166d76fa58SLinus Torvalds 		return -EPERM;
6176d76fa58SLinus Torvalds 
6186d76fa58SLinus Torvalds 	error = inode_change_ok(inode, attr);
6191025774cSChristoph Hellwig 	if (error)
6206d76fa58SLinus Torvalds 		return error;
6211025774cSChristoph Hellwig 
6221025774cSChristoph Hellwig 	if ((attr->ia_valid & ATTR_SIZE) &&
6231025774cSChristoph Hellwig 	    attr->ia_size != i_size_read(inode)) {
6241025774cSChristoph Hellwig 		error = vmtruncate(inode, attr->ia_size);
6251025774cSChristoph Hellwig 		if (error)
6261025774cSChristoph Hellwig 			return error;
6271025774cSChristoph Hellwig 	}
6281025774cSChristoph Hellwig 
6291025774cSChristoph Hellwig 	setattr_copy(inode, attr);
6301025774cSChristoph Hellwig 	mark_inode_dirty(inode);
6311025774cSChristoph Hellwig 	return 0;
6326d76fa58SLinus Torvalds }
6336d76fa58SLinus Torvalds 
634c5ef1c42SArjan van de Ven static const struct inode_operations proc_def_inode_operations = {
6356d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
6366d76fa58SLinus Torvalds };
6376d76fa58SLinus Torvalds 
6381da177e4SLinus Torvalds #define PROC_BLOCK_SIZE	(3*1024)		/* 4K page size but our output routines use some slack for overruns */
6391da177e4SLinus Torvalds 
6401da177e4SLinus Torvalds static ssize_t proc_info_read(struct file * file, char __user * buf,
6411da177e4SLinus Torvalds 			  size_t count, loff_t *ppos)
6421da177e4SLinus Torvalds {
6432fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
6441da177e4SLinus Torvalds 	unsigned long page;
6451da177e4SLinus Torvalds 	ssize_t length;
64699f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
64799f89551SEric W. Biederman 
64899f89551SEric W. Biederman 	length = -ESRCH;
64999f89551SEric W. Biederman 	if (!task)
65099f89551SEric W. Biederman 		goto out_no_task;
6511da177e4SLinus Torvalds 
6521da177e4SLinus Torvalds 	if (count > PROC_BLOCK_SIZE)
6531da177e4SLinus Torvalds 		count = PROC_BLOCK_SIZE;
65499f89551SEric W. Biederman 
65599f89551SEric W. Biederman 	length = -ENOMEM;
656e12ba74dSMel Gorman 	if (!(page = __get_free_page(GFP_TEMPORARY)))
65799f89551SEric W. Biederman 		goto out;
6581da177e4SLinus Torvalds 
6591da177e4SLinus Torvalds 	length = PROC_I(inode)->op.proc_read(task, (char*)page);
6601da177e4SLinus Torvalds 
6611da177e4SLinus Torvalds 	if (length >= 0)
6621da177e4SLinus Torvalds 		length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
6631da177e4SLinus Torvalds 	free_page(page);
66499f89551SEric W. Biederman out:
66599f89551SEric W. Biederman 	put_task_struct(task);
66699f89551SEric W. Biederman out_no_task:
6671da177e4SLinus Torvalds 	return length;
6681da177e4SLinus Torvalds }
6691da177e4SLinus Torvalds 
67000977a59SArjan van de Ven static const struct file_operations proc_info_file_operations = {
6711da177e4SLinus Torvalds 	.read		= proc_info_read,
67287df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
6731da177e4SLinus Torvalds };
6741da177e4SLinus Torvalds 
675be614086SEric W. Biederman static int proc_single_show(struct seq_file *m, void *v)
676be614086SEric W. Biederman {
677be614086SEric W. Biederman 	struct inode *inode = m->private;
678be614086SEric W. Biederman 	struct pid_namespace *ns;
679be614086SEric W. Biederman 	struct pid *pid;
680be614086SEric W. Biederman 	struct task_struct *task;
681be614086SEric W. Biederman 	int ret;
682be614086SEric W. Biederman 
683be614086SEric W. Biederman 	ns = inode->i_sb->s_fs_info;
684be614086SEric W. Biederman 	pid = proc_pid(inode);
685be614086SEric W. Biederman 	task = get_pid_task(pid, PIDTYPE_PID);
686be614086SEric W. Biederman 	if (!task)
687be614086SEric W. Biederman 		return -ESRCH;
688be614086SEric W. Biederman 
689be614086SEric W. Biederman 	ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
690be614086SEric W. Biederman 
691be614086SEric W. Biederman 	put_task_struct(task);
692be614086SEric W. Biederman 	return ret;
693be614086SEric W. Biederman }
694be614086SEric W. Biederman 
695be614086SEric W. Biederman static int proc_single_open(struct inode *inode, struct file *filp)
696be614086SEric W. Biederman {
697c6a34058SJovi Zhang 	return single_open(filp, proc_single_show, inode);
698be614086SEric W. Biederman }
699be614086SEric W. Biederman 
700be614086SEric W. Biederman static const struct file_operations proc_single_file_operations = {
701be614086SEric W. Biederman 	.open		= proc_single_open,
702be614086SEric W. Biederman 	.read		= seq_read,
703be614086SEric W. Biederman 	.llseek		= seq_lseek,
704be614086SEric W. Biederman 	.release	= single_release,
705be614086SEric W. Biederman };
706be614086SEric W. Biederman 
7071da177e4SLinus Torvalds static int mem_open(struct inode* inode, struct file* file)
7081da177e4SLinus Torvalds {
7091da177e4SLinus Torvalds 	file->private_data = (void*)((long)current->self_exec_id);
7104a3956c7SKAMEZAWA Hiroyuki 	/* OK to pass negative loff_t, we can catch out-of-range */
7114a3956c7SKAMEZAWA Hiroyuki 	file->f_mode |= FMODE_UNSIGNED_OFFSET;
7121da177e4SLinus Torvalds 	return 0;
7131da177e4SLinus Torvalds }
7141da177e4SLinus Torvalds 
7151da177e4SLinus Torvalds static ssize_t mem_read(struct file * file, char __user * buf,
7161da177e4SLinus Torvalds 			size_t count, loff_t *ppos)
7171da177e4SLinus Torvalds {
7182fddfeefSJosef "Jeff" Sipek 	struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
7191da177e4SLinus Torvalds 	char *page;
7201da177e4SLinus Torvalds 	unsigned long src = *ppos;
7211da177e4SLinus Torvalds 	int ret = -ESRCH;
7221da177e4SLinus Torvalds 	struct mm_struct *mm;
7231da177e4SLinus Torvalds 
72499f89551SEric W. Biederman 	if (!task)
72599f89551SEric W. Biederman 		goto out_no_task;
72699f89551SEric W. Biederman 
7271da177e4SLinus Torvalds 	ret = -ENOMEM;
728e12ba74dSMel Gorman 	page = (char *)__get_free_page(GFP_TEMPORARY);
7291da177e4SLinus Torvalds 	if (!page)
7301da177e4SLinus Torvalds 		goto out;
7311da177e4SLinus Torvalds 
7328b0db9dbSStephen Wilson 	mm = check_mem_permission(task);
7338b0db9dbSStephen Wilson 	ret = PTR_ERR(mm);
7348b0db9dbSStephen Wilson 	if (IS_ERR(mm))
7351da177e4SLinus Torvalds 		goto out_free;
7361da177e4SLinus Torvalds 
7371da177e4SLinus Torvalds 	ret = -EIO;
7381da177e4SLinus Torvalds 
7391da177e4SLinus Torvalds 	if (file->private_data != (void*)((long)current->self_exec_id))
7401da177e4SLinus Torvalds 		goto out_put;
7411da177e4SLinus Torvalds 
7421da177e4SLinus Torvalds 	ret = 0;
7431da177e4SLinus Torvalds 
7441da177e4SLinus Torvalds 	while (count > 0) {
7451da177e4SLinus Torvalds 		int this_len, retval;
7461da177e4SLinus Torvalds 
7471da177e4SLinus Torvalds 		this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
7488b0db9dbSStephen Wilson 		retval = access_remote_vm(mm, src, page, this_len, 0);
7498b0db9dbSStephen Wilson 		if (!retval) {
7501da177e4SLinus Torvalds 			if (!ret)
7511da177e4SLinus Torvalds 				ret = -EIO;
7521da177e4SLinus Torvalds 			break;
7531da177e4SLinus Torvalds 		}
7541da177e4SLinus Torvalds 
7551da177e4SLinus Torvalds 		if (copy_to_user(buf, page, retval)) {
7561da177e4SLinus Torvalds 			ret = -EFAULT;
7571da177e4SLinus Torvalds 			break;
7581da177e4SLinus Torvalds 		}
7591da177e4SLinus Torvalds 
7601da177e4SLinus Torvalds 		ret += retval;
7611da177e4SLinus Torvalds 		src += retval;
7621da177e4SLinus Torvalds 		buf += retval;
7631da177e4SLinus Torvalds 		count -= retval;
7641da177e4SLinus Torvalds 	}
7651da177e4SLinus Torvalds 	*ppos = src;
7661da177e4SLinus Torvalds 
7671da177e4SLinus Torvalds out_put:
7681da177e4SLinus Torvalds 	mmput(mm);
7691da177e4SLinus Torvalds out_free:
7701da177e4SLinus Torvalds 	free_page((unsigned long) page);
7711da177e4SLinus Torvalds out:
77299f89551SEric W. Biederman 	put_task_struct(task);
77399f89551SEric W. Biederman out_no_task:
7741da177e4SLinus Torvalds 	return ret;
7751da177e4SLinus Torvalds }
7761da177e4SLinus Torvalds 
77763967fa9SGlauber de Oliveira Costa static ssize_t mem_write(struct file * file, const char __user *buf,
7781da177e4SLinus Torvalds 			 size_t count, loff_t *ppos)
7791da177e4SLinus Torvalds {
780f7ca54f4SFrederik Deweerdt 	int copied;
7811da177e4SLinus Torvalds 	char *page;
7822fddfeefSJosef "Jeff" Sipek 	struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
7831da177e4SLinus Torvalds 	unsigned long dst = *ppos;
7848b0db9dbSStephen Wilson 	struct mm_struct *mm;
7851da177e4SLinus Torvalds 
78699f89551SEric W. Biederman 	copied = -ESRCH;
78799f89551SEric W. Biederman 	if (!task)
78899f89551SEric W. Biederman 		goto out_no_task;
7891da177e4SLinus Torvalds 
79099f89551SEric W. Biederman 	copied = -ENOMEM;
791e12ba74dSMel Gorman 	page = (char *)__get_free_page(GFP_TEMPORARY);
7921da177e4SLinus Torvalds 	if (!page)
79330cd8903SKOSAKI Motohiro 		goto out_task;
79430cd8903SKOSAKI Motohiro 
79530cd8903SKOSAKI Motohiro 	mm = check_mem_permission(task);
79630cd8903SKOSAKI Motohiro 	copied = PTR_ERR(mm);
79730cd8903SKOSAKI Motohiro 	if (IS_ERR(mm))
79830cd8903SKOSAKI Motohiro 		goto out_free;
79930cd8903SKOSAKI Motohiro 
80030cd8903SKOSAKI Motohiro 	copied = -EIO;
80130cd8903SKOSAKI Motohiro 	if (file->private_data != (void *)((long)current->self_exec_id))
8028b0db9dbSStephen Wilson 		goto out_mm;
8031da177e4SLinus Torvalds 
804f7ca54f4SFrederik Deweerdt 	copied = 0;
8051da177e4SLinus Torvalds 	while (count > 0) {
8061da177e4SLinus Torvalds 		int this_len, retval;
8071da177e4SLinus Torvalds 
8081da177e4SLinus Torvalds 		this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
8091da177e4SLinus Torvalds 		if (copy_from_user(page, buf, this_len)) {
8101da177e4SLinus Torvalds 			copied = -EFAULT;
8111da177e4SLinus Torvalds 			break;
8121da177e4SLinus Torvalds 		}
8138b0db9dbSStephen Wilson 		retval = access_remote_vm(mm, dst, page, this_len, 1);
8141da177e4SLinus Torvalds 		if (!retval) {
8151da177e4SLinus Torvalds 			if (!copied)
8161da177e4SLinus Torvalds 				copied = -EIO;
8171da177e4SLinus Torvalds 			break;
8181da177e4SLinus Torvalds 		}
8191da177e4SLinus Torvalds 		copied += retval;
8201da177e4SLinus Torvalds 		buf += retval;
8211da177e4SLinus Torvalds 		dst += retval;
8221da177e4SLinus Torvalds 		count -= retval;
8231da177e4SLinus Torvalds 	}
8241da177e4SLinus Torvalds 	*ppos = dst;
82530cd8903SKOSAKI Motohiro 
8268b0db9dbSStephen Wilson out_mm:
8278b0db9dbSStephen Wilson 	mmput(mm);
82830cd8903SKOSAKI Motohiro out_free:
82930cd8903SKOSAKI Motohiro 	free_page((unsigned long) page);
8308b0db9dbSStephen Wilson out_task:
83199f89551SEric W. Biederman 	put_task_struct(task);
83299f89551SEric W. Biederman out_no_task:
8331da177e4SLinus Torvalds 	return copied;
8341da177e4SLinus Torvalds }
8351da177e4SLinus Torvalds 
83685863e47SMatt Mackall loff_t mem_lseek(struct file *file, loff_t offset, int orig)
8371da177e4SLinus Torvalds {
8381da177e4SLinus Torvalds 	switch (orig) {
8391da177e4SLinus Torvalds 	case 0:
8401da177e4SLinus Torvalds 		file->f_pos = offset;
8411da177e4SLinus Torvalds 		break;
8421da177e4SLinus Torvalds 	case 1:
8431da177e4SLinus Torvalds 		file->f_pos += offset;
8441da177e4SLinus Torvalds 		break;
8451da177e4SLinus Torvalds 	default:
8461da177e4SLinus Torvalds 		return -EINVAL;
8471da177e4SLinus Torvalds 	}
8481da177e4SLinus Torvalds 	force_successful_syscall_return();
8491da177e4SLinus Torvalds 	return file->f_pos;
8501da177e4SLinus Torvalds }
8511da177e4SLinus Torvalds 
85200977a59SArjan van de Ven static const struct file_operations proc_mem_operations = {
8531da177e4SLinus Torvalds 	.llseek		= mem_lseek,
8541da177e4SLinus Torvalds 	.read		= mem_read,
8551da177e4SLinus Torvalds 	.write		= mem_write,
8561da177e4SLinus Torvalds 	.open		= mem_open,
8571da177e4SLinus Torvalds };
8581da177e4SLinus Torvalds 
859315e28c8SJames Pearson static ssize_t environ_read(struct file *file, char __user *buf,
860315e28c8SJames Pearson 			size_t count, loff_t *ppos)
861315e28c8SJames Pearson {
862315e28c8SJames Pearson 	struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
863315e28c8SJames Pearson 	char *page;
864315e28c8SJames Pearson 	unsigned long src = *ppos;
865315e28c8SJames Pearson 	int ret = -ESRCH;
866315e28c8SJames Pearson 	struct mm_struct *mm;
867315e28c8SJames Pearson 
868315e28c8SJames Pearson 	if (!task)
869315e28c8SJames Pearson 		goto out_no_task;
870315e28c8SJames Pearson 
871315e28c8SJames Pearson 	ret = -ENOMEM;
872315e28c8SJames Pearson 	page = (char *)__get_free_page(GFP_TEMPORARY);
873315e28c8SJames Pearson 	if (!page)
874315e28c8SJames Pearson 		goto out;
875315e28c8SJames Pearson 
876315e28c8SJames Pearson 
877d6f64b89SAl Viro 	mm = mm_for_maps(task);
878d6f64b89SAl Viro 	ret = PTR_ERR(mm);
879d6f64b89SAl Viro 	if (!mm || IS_ERR(mm))
880315e28c8SJames Pearson 		goto out_free;
881315e28c8SJames Pearson 
882d6f64b89SAl Viro 	ret = 0;
883315e28c8SJames Pearson 	while (count > 0) {
884315e28c8SJames Pearson 		int this_len, retval, max_len;
885315e28c8SJames Pearson 
886315e28c8SJames Pearson 		this_len = mm->env_end - (mm->env_start + src);
887315e28c8SJames Pearson 
888315e28c8SJames Pearson 		if (this_len <= 0)
889315e28c8SJames Pearson 			break;
890315e28c8SJames Pearson 
891315e28c8SJames Pearson 		max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
892315e28c8SJames Pearson 		this_len = (this_len > max_len) ? max_len : this_len;
893315e28c8SJames Pearson 
894315e28c8SJames Pearson 		retval = access_process_vm(task, (mm->env_start + src),
895315e28c8SJames Pearson 			page, this_len, 0);
896315e28c8SJames Pearson 
897315e28c8SJames Pearson 		if (retval <= 0) {
898315e28c8SJames Pearson 			ret = retval;
899315e28c8SJames Pearson 			break;
900315e28c8SJames Pearson 		}
901315e28c8SJames Pearson 
902315e28c8SJames Pearson 		if (copy_to_user(buf, page, retval)) {
903315e28c8SJames Pearson 			ret = -EFAULT;
904315e28c8SJames Pearson 			break;
905315e28c8SJames Pearson 		}
906315e28c8SJames Pearson 
907315e28c8SJames Pearson 		ret += retval;
908315e28c8SJames Pearson 		src += retval;
909315e28c8SJames Pearson 		buf += retval;
910315e28c8SJames Pearson 		count -= retval;
911315e28c8SJames Pearson 	}
912315e28c8SJames Pearson 	*ppos = src;
913315e28c8SJames Pearson 
914315e28c8SJames Pearson 	mmput(mm);
915315e28c8SJames Pearson out_free:
916315e28c8SJames Pearson 	free_page((unsigned long) page);
917315e28c8SJames Pearson out:
918315e28c8SJames Pearson 	put_task_struct(task);
919315e28c8SJames Pearson out_no_task:
920315e28c8SJames Pearson 	return ret;
921315e28c8SJames Pearson }
922315e28c8SJames Pearson 
923315e28c8SJames Pearson static const struct file_operations proc_environ_operations = {
924315e28c8SJames Pearson 	.read		= environ_read,
92587df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
926315e28c8SJames Pearson };
927315e28c8SJames Pearson 
9281da177e4SLinus Torvalds static ssize_t oom_adjust_read(struct file *file, char __user *buf,
9291da177e4SLinus Torvalds 				size_t count, loff_t *ppos)
9301da177e4SLinus Torvalds {
9312fddfeefSJosef "Jeff" Sipek 	struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
9328578cea7SEric W. Biederman 	char buffer[PROC_NUMBUF];
9331da177e4SLinus Torvalds 	size_t len;
93428b83c51SKOSAKI Motohiro 	int oom_adjust = OOM_DISABLE;
93528b83c51SKOSAKI Motohiro 	unsigned long flags;
9361da177e4SLinus Torvalds 
93799f89551SEric W. Biederman 	if (!task)
93899f89551SEric W. Biederman 		return -ESRCH;
93928b83c51SKOSAKI Motohiro 
94028b83c51SKOSAKI Motohiro 	if (lock_task_sighand(task, &flags)) {
94128b83c51SKOSAKI Motohiro 		oom_adjust = task->signal->oom_adj;
94228b83c51SKOSAKI Motohiro 		unlock_task_sighand(task, &flags);
94328b83c51SKOSAKI Motohiro 	}
94428b83c51SKOSAKI Motohiro 
94599f89551SEric W. Biederman 	put_task_struct(task);
94699f89551SEric W. Biederman 
9478578cea7SEric W. Biederman 	len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
9480c28f287SAkinobu Mita 
9490c28f287SAkinobu Mita 	return simple_read_from_buffer(buf, count, ppos, buffer, len);
9501da177e4SLinus Torvalds }
9511da177e4SLinus Torvalds 
9521da177e4SLinus Torvalds static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
9531da177e4SLinus Torvalds 				size_t count, loff_t *ppos)
9541da177e4SLinus Torvalds {
95599f89551SEric W. Biederman 	struct task_struct *task;
9565d863b89SKOSAKI Motohiro 	char buffer[PROC_NUMBUF];
9570a8cb8e3SAlexey Dobriyan 	int oom_adjust;
95828b83c51SKOSAKI Motohiro 	unsigned long flags;
9595d863b89SKOSAKI Motohiro 	int err;
9601da177e4SLinus Torvalds 
9618578cea7SEric W. Biederman 	memset(buffer, 0, sizeof(buffer));
9628578cea7SEric W. Biederman 	if (count > sizeof(buffer) - 1)
9638578cea7SEric W. Biederman 		count = sizeof(buffer) - 1;
964723548bfSDavid Rientjes 	if (copy_from_user(buffer, buf, count)) {
965723548bfSDavid Rientjes 		err = -EFAULT;
966723548bfSDavid Rientjes 		goto out;
967723548bfSDavid Rientjes 	}
9685d863b89SKOSAKI Motohiro 
9690a8cb8e3SAlexey Dobriyan 	err = kstrtoint(strstrip(buffer), 0, &oom_adjust);
9705d863b89SKOSAKI Motohiro 	if (err)
971723548bfSDavid Rientjes 		goto out;
9728ac773b4SAlexey Dobriyan 	if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
973723548bfSDavid Rientjes 	     oom_adjust != OOM_DISABLE) {
974723548bfSDavid Rientjes 		err = -EINVAL;
975723548bfSDavid Rientjes 		goto out;
976723548bfSDavid Rientjes 	}
9775d863b89SKOSAKI Motohiro 
9782fddfeefSJosef "Jeff" Sipek 	task = get_proc_task(file->f_path.dentry->d_inode);
979723548bfSDavid Rientjes 	if (!task) {
980723548bfSDavid Rientjes 		err = -ESRCH;
981723548bfSDavid Rientjes 		goto out;
982723548bfSDavid Rientjes 	}
98328b83c51SKOSAKI Motohiro 
9843d5992d2SYing Han 	task_lock(task);
9853d5992d2SYing Han 	if (!task->mm) {
986723548bfSDavid Rientjes 		err = -EINVAL;
987723548bfSDavid Rientjes 		goto err_task_lock;
9883d5992d2SYing Han 	}
9893d5992d2SYing Han 
990d19d5476SDavid Rientjes 	if (!lock_task_sighand(task, &flags)) {
991d19d5476SDavid Rientjes 		err = -ESRCH;
992d19d5476SDavid Rientjes 		goto err_task_lock;
993d19d5476SDavid Rientjes 	}
994d19d5476SDavid Rientjes 
995d19d5476SDavid Rientjes 	if (oom_adjust < task->signal->oom_adj && !capable(CAP_SYS_RESOURCE)) {
996d19d5476SDavid Rientjes 		err = -EACCES;
997d19d5476SDavid Rientjes 		goto err_sighand;
998d19d5476SDavid Rientjes 	}
999d19d5476SDavid Rientjes 
100051b1bd2aSDavid Rientjes 	/*
100151b1bd2aSDavid Rientjes 	 * Warn that /proc/pid/oom_adj is deprecated, see
100251b1bd2aSDavid Rientjes 	 * Documentation/feature-removal-schedule.txt.
100351b1bd2aSDavid Rientjes 	 */
1004c2142704SLinus Torvalds 	printk_once(KERN_WARNING "%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
1005be8f684dSDavid Rientjes 		  current->comm, task_pid_nr(current), task_pid_nr(task),
1006be8f684dSDavid Rientjes 		  task_pid_nr(task));
100728b83c51SKOSAKI Motohiro 	task->signal->oom_adj = oom_adjust;
1008a63d83f4SDavid Rientjes 	/*
1009a63d83f4SDavid Rientjes 	 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1010a63d83f4SDavid Rientjes 	 * value is always attainable.
1011a63d83f4SDavid Rientjes 	 */
1012a63d83f4SDavid Rientjes 	if (task->signal->oom_adj == OOM_ADJUST_MAX)
1013a63d83f4SDavid Rientjes 		task->signal->oom_score_adj = OOM_SCORE_ADJ_MAX;
1014a63d83f4SDavid Rientjes 	else
1015a63d83f4SDavid Rientjes 		task->signal->oom_score_adj = (oom_adjust * OOM_SCORE_ADJ_MAX) /
1016a63d83f4SDavid Rientjes 								-OOM_DISABLE;
101743d2b113SKAMEZAWA Hiroyuki 	trace_oom_score_adj_update(task);
1018723548bfSDavid Rientjes err_sighand:
101928b83c51SKOSAKI Motohiro 	unlock_task_sighand(task, &flags);
1020d19d5476SDavid Rientjes err_task_lock:
1021d19d5476SDavid Rientjes 	task_unlock(task);
102299f89551SEric W. Biederman 	put_task_struct(task);
1023723548bfSDavid Rientjes out:
1024723548bfSDavid Rientjes 	return err < 0 ? err : count;
10251da177e4SLinus Torvalds }
10261da177e4SLinus Torvalds 
102700977a59SArjan van de Ven static const struct file_operations proc_oom_adjust_operations = {
10281da177e4SLinus Torvalds 	.read		= oom_adjust_read,
10291da177e4SLinus Torvalds 	.write		= oom_adjust_write,
103087df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
10311da177e4SLinus Torvalds };
10321da177e4SLinus Torvalds 
1033a63d83f4SDavid Rientjes static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1034a63d83f4SDavid Rientjes 					size_t count, loff_t *ppos)
1035a63d83f4SDavid Rientjes {
1036a63d83f4SDavid Rientjes 	struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
1037a63d83f4SDavid Rientjes 	char buffer[PROC_NUMBUF];
1038a63d83f4SDavid Rientjes 	int oom_score_adj = OOM_SCORE_ADJ_MIN;
1039a63d83f4SDavid Rientjes 	unsigned long flags;
1040a63d83f4SDavid Rientjes 	size_t len;
1041a63d83f4SDavid Rientjes 
1042a63d83f4SDavid Rientjes 	if (!task)
1043a63d83f4SDavid Rientjes 		return -ESRCH;
1044a63d83f4SDavid Rientjes 	if (lock_task_sighand(task, &flags)) {
1045a63d83f4SDavid Rientjes 		oom_score_adj = task->signal->oom_score_adj;
1046a63d83f4SDavid Rientjes 		unlock_task_sighand(task, &flags);
1047a63d83f4SDavid Rientjes 	}
1048a63d83f4SDavid Rientjes 	put_task_struct(task);
1049a63d83f4SDavid Rientjes 	len = snprintf(buffer, sizeof(buffer), "%d\n", oom_score_adj);
1050a63d83f4SDavid Rientjes 	return simple_read_from_buffer(buf, count, ppos, buffer, len);
1051a63d83f4SDavid Rientjes }
1052a63d83f4SDavid Rientjes 
1053a63d83f4SDavid Rientjes static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1054a63d83f4SDavid Rientjes 					size_t count, loff_t *ppos)
1055a63d83f4SDavid Rientjes {
1056a63d83f4SDavid Rientjes 	struct task_struct *task;
1057a63d83f4SDavid Rientjes 	char buffer[PROC_NUMBUF];
1058a63d83f4SDavid Rientjes 	unsigned long flags;
10590a8cb8e3SAlexey Dobriyan 	int oom_score_adj;
1060a63d83f4SDavid Rientjes 	int err;
1061a63d83f4SDavid Rientjes 
1062a63d83f4SDavid Rientjes 	memset(buffer, 0, sizeof(buffer));
1063a63d83f4SDavid Rientjes 	if (count > sizeof(buffer) - 1)
1064a63d83f4SDavid Rientjes 		count = sizeof(buffer) - 1;
1065723548bfSDavid Rientjes 	if (copy_from_user(buffer, buf, count)) {
1066723548bfSDavid Rientjes 		err = -EFAULT;
1067723548bfSDavid Rientjes 		goto out;
1068723548bfSDavid Rientjes 	}
1069a63d83f4SDavid Rientjes 
10700a8cb8e3SAlexey Dobriyan 	err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
1071a63d83f4SDavid Rientjes 	if (err)
1072723548bfSDavid Rientjes 		goto out;
1073a63d83f4SDavid Rientjes 	if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
1074723548bfSDavid Rientjes 			oom_score_adj > OOM_SCORE_ADJ_MAX) {
1075723548bfSDavid Rientjes 		err = -EINVAL;
1076723548bfSDavid Rientjes 		goto out;
1077723548bfSDavid Rientjes 	}
1078a63d83f4SDavid Rientjes 
1079a63d83f4SDavid Rientjes 	task = get_proc_task(file->f_path.dentry->d_inode);
1080723548bfSDavid Rientjes 	if (!task) {
1081723548bfSDavid Rientjes 		err = -ESRCH;
1082723548bfSDavid Rientjes 		goto out;
1083723548bfSDavid Rientjes 	}
1084a63d83f4SDavid Rientjes 
10853d5992d2SYing Han 	task_lock(task);
10863d5992d2SYing Han 	if (!task->mm) {
1087723548bfSDavid Rientjes 		err = -EINVAL;
1088723548bfSDavid Rientjes 		goto err_task_lock;
10893d5992d2SYing Han 	}
1090d19d5476SDavid Rientjes 
1091d19d5476SDavid Rientjes 	if (!lock_task_sighand(task, &flags)) {
1092d19d5476SDavid Rientjes 		err = -ESRCH;
1093d19d5476SDavid Rientjes 		goto err_task_lock;
1094d19d5476SDavid Rientjes 	}
1095d19d5476SDavid Rientjes 
1096dabb16f6SMandeep Singh Baines 	if (oom_score_adj < task->signal->oom_score_adj_min &&
1097d19d5476SDavid Rientjes 			!capable(CAP_SYS_RESOURCE)) {
1098d19d5476SDavid Rientjes 		err = -EACCES;
1099d19d5476SDavid Rientjes 		goto err_sighand;
1100d19d5476SDavid Rientjes 	}
1101d19d5476SDavid Rientjes 
1102a63d83f4SDavid Rientjes 	task->signal->oom_score_adj = oom_score_adj;
1103dabb16f6SMandeep Singh Baines 	if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
1104dabb16f6SMandeep Singh Baines 		task->signal->oom_score_adj_min = oom_score_adj;
110543d2b113SKAMEZAWA Hiroyuki 	trace_oom_score_adj_update(task);
1106a63d83f4SDavid Rientjes 	/*
1107a63d83f4SDavid Rientjes 	 * Scale /proc/pid/oom_adj appropriately ensuring that OOM_DISABLE is
1108a63d83f4SDavid Rientjes 	 * always attainable.
1109a63d83f4SDavid Rientjes 	 */
1110a63d83f4SDavid Rientjes 	if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MIN)
1111a63d83f4SDavid Rientjes 		task->signal->oom_adj = OOM_DISABLE;
1112a63d83f4SDavid Rientjes 	else
1113a63d83f4SDavid Rientjes 		task->signal->oom_adj = (oom_score_adj * OOM_ADJUST_MAX) /
1114a63d83f4SDavid Rientjes 							OOM_SCORE_ADJ_MAX;
1115723548bfSDavid Rientjes err_sighand:
1116a63d83f4SDavid Rientjes 	unlock_task_sighand(task, &flags);
1117d19d5476SDavid Rientjes err_task_lock:
1118d19d5476SDavid Rientjes 	task_unlock(task);
1119a63d83f4SDavid Rientjes 	put_task_struct(task);
1120723548bfSDavid Rientjes out:
1121723548bfSDavid Rientjes 	return err < 0 ? err : count;
1122a63d83f4SDavid Rientjes }
1123a63d83f4SDavid Rientjes 
1124a63d83f4SDavid Rientjes static const struct file_operations proc_oom_score_adj_operations = {
1125a63d83f4SDavid Rientjes 	.read		= oom_score_adj_read,
1126a63d83f4SDavid Rientjes 	.write		= oom_score_adj_write,
11276038f373SArnd Bergmann 	.llseek		= default_llseek,
1128a63d83f4SDavid Rientjes };
1129a63d83f4SDavid Rientjes 
11301da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL
11311da177e4SLinus Torvalds #define TMPBUFLEN 21
11321da177e4SLinus Torvalds static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
11331da177e4SLinus Torvalds 				  size_t count, loff_t *ppos)
11341da177e4SLinus Torvalds {
11352fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
113699f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
11371da177e4SLinus Torvalds 	ssize_t length;
11381da177e4SLinus Torvalds 	char tmpbuf[TMPBUFLEN];
11391da177e4SLinus Torvalds 
114099f89551SEric W. Biederman 	if (!task)
114199f89551SEric W. Biederman 		return -ESRCH;
11421da177e4SLinus Torvalds 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
11430c11b942SAl Viro 				audit_get_loginuid(task));
114499f89551SEric W. Biederman 	put_task_struct(task);
11451da177e4SLinus Torvalds 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
11461da177e4SLinus Torvalds }
11471da177e4SLinus Torvalds 
11481da177e4SLinus Torvalds static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
11491da177e4SLinus Torvalds 				   size_t count, loff_t *ppos)
11501da177e4SLinus Torvalds {
11512fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
11521da177e4SLinus Torvalds 	char *page, *tmp;
11531da177e4SLinus Torvalds 	ssize_t length;
11541da177e4SLinus Torvalds 	uid_t loginuid;
11551da177e4SLinus Torvalds 
11561da177e4SLinus Torvalds 	if (!capable(CAP_AUDIT_CONTROL))
11571da177e4SLinus Torvalds 		return -EPERM;
11581da177e4SLinus Torvalds 
11597dc52157SPaul E. McKenney 	rcu_read_lock();
11607dc52157SPaul E. McKenney 	if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
11617dc52157SPaul E. McKenney 		rcu_read_unlock();
11621da177e4SLinus Torvalds 		return -EPERM;
11637dc52157SPaul E. McKenney 	}
11647dc52157SPaul E. McKenney 	rcu_read_unlock();
11651da177e4SLinus Torvalds 
1166e0182909SAl Viro 	if (count >= PAGE_SIZE)
1167e0182909SAl Viro 		count = PAGE_SIZE - 1;
11681da177e4SLinus Torvalds 
11691da177e4SLinus Torvalds 	if (*ppos != 0) {
11701da177e4SLinus Torvalds 		/* No partial writes. */
11711da177e4SLinus Torvalds 		return -EINVAL;
11721da177e4SLinus Torvalds 	}
1173e12ba74dSMel Gorman 	page = (char*)__get_free_page(GFP_TEMPORARY);
11741da177e4SLinus Torvalds 	if (!page)
11751da177e4SLinus Torvalds 		return -ENOMEM;
11761da177e4SLinus Torvalds 	length = -EFAULT;
11771da177e4SLinus Torvalds 	if (copy_from_user(page, buf, count))
11781da177e4SLinus Torvalds 		goto out_free_page;
11791da177e4SLinus Torvalds 
1180e0182909SAl Viro 	page[count] = '\0';
11811da177e4SLinus Torvalds 	loginuid = simple_strtoul(page, &tmp, 10);
11821da177e4SLinus Torvalds 	if (tmp == page) {
11831da177e4SLinus Torvalds 		length = -EINVAL;
11841da177e4SLinus Torvalds 		goto out_free_page;
11851da177e4SLinus Torvalds 
11861da177e4SLinus Torvalds 	}
118799f89551SEric W. Biederman 	length = audit_set_loginuid(current, loginuid);
11881da177e4SLinus Torvalds 	if (likely(length == 0))
11891da177e4SLinus Torvalds 		length = count;
11901da177e4SLinus Torvalds 
11911da177e4SLinus Torvalds out_free_page:
11921da177e4SLinus Torvalds 	free_page((unsigned long) page);
11931da177e4SLinus Torvalds 	return length;
11941da177e4SLinus Torvalds }
11951da177e4SLinus Torvalds 
119600977a59SArjan van de Ven static const struct file_operations proc_loginuid_operations = {
11971da177e4SLinus Torvalds 	.read		= proc_loginuid_read,
11981da177e4SLinus Torvalds 	.write		= proc_loginuid_write,
119987df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
12001da177e4SLinus Torvalds };
12011e0bd755SEric Paris 
12021e0bd755SEric Paris static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
12031e0bd755SEric Paris 				  size_t count, loff_t *ppos)
12041e0bd755SEric Paris {
12051e0bd755SEric Paris 	struct inode * inode = file->f_path.dentry->d_inode;
12061e0bd755SEric Paris 	struct task_struct *task = get_proc_task(inode);
12071e0bd755SEric Paris 	ssize_t length;
12081e0bd755SEric Paris 	char tmpbuf[TMPBUFLEN];
12091e0bd755SEric Paris 
12101e0bd755SEric Paris 	if (!task)
12111e0bd755SEric Paris 		return -ESRCH;
12121e0bd755SEric Paris 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
12131e0bd755SEric Paris 				audit_get_sessionid(task));
12141e0bd755SEric Paris 	put_task_struct(task);
12151e0bd755SEric Paris 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
12161e0bd755SEric Paris }
12171e0bd755SEric Paris 
12181e0bd755SEric Paris static const struct file_operations proc_sessionid_operations = {
12191e0bd755SEric Paris 	.read		= proc_sessionid_read,
122087df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
12211e0bd755SEric Paris };
12221da177e4SLinus Torvalds #endif
12231da177e4SLinus Torvalds 
1224f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
1225f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1226f4f154fdSAkinobu Mita 				      size_t count, loff_t *ppos)
1227f4f154fdSAkinobu Mita {
1228f4f154fdSAkinobu Mita 	struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1229f4f154fdSAkinobu Mita 	char buffer[PROC_NUMBUF];
1230f4f154fdSAkinobu Mita 	size_t len;
1231f4f154fdSAkinobu Mita 	int make_it_fail;
1232f4f154fdSAkinobu Mita 
1233f4f154fdSAkinobu Mita 	if (!task)
1234f4f154fdSAkinobu Mita 		return -ESRCH;
1235f4f154fdSAkinobu Mita 	make_it_fail = task->make_it_fail;
1236f4f154fdSAkinobu Mita 	put_task_struct(task);
1237f4f154fdSAkinobu Mita 
1238f4f154fdSAkinobu Mita 	len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
12390c28f287SAkinobu Mita 
12400c28f287SAkinobu Mita 	return simple_read_from_buffer(buf, count, ppos, buffer, len);
1241f4f154fdSAkinobu Mita }
1242f4f154fdSAkinobu Mita 
1243f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_write(struct file * file,
1244f4f154fdSAkinobu Mita 			const char __user * buf, size_t count, loff_t *ppos)
1245f4f154fdSAkinobu Mita {
1246f4f154fdSAkinobu Mita 	struct task_struct *task;
1247f4f154fdSAkinobu Mita 	char buffer[PROC_NUMBUF], *end;
1248f4f154fdSAkinobu Mita 	int make_it_fail;
1249f4f154fdSAkinobu Mita 
1250f4f154fdSAkinobu Mita 	if (!capable(CAP_SYS_RESOURCE))
1251f4f154fdSAkinobu Mita 		return -EPERM;
1252f4f154fdSAkinobu Mita 	memset(buffer, 0, sizeof(buffer));
1253f4f154fdSAkinobu Mita 	if (count > sizeof(buffer) - 1)
1254f4f154fdSAkinobu Mita 		count = sizeof(buffer) - 1;
1255f4f154fdSAkinobu Mita 	if (copy_from_user(buffer, buf, count))
1256f4f154fdSAkinobu Mita 		return -EFAULT;
1257cba8aafeSVincent Li 	make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1258cba8aafeSVincent Li 	if (*end)
1259cba8aafeSVincent Li 		return -EINVAL;
1260f4f154fdSAkinobu Mita 	task = get_proc_task(file->f_dentry->d_inode);
1261f4f154fdSAkinobu Mita 	if (!task)
1262f4f154fdSAkinobu Mita 		return -ESRCH;
1263f4f154fdSAkinobu Mita 	task->make_it_fail = make_it_fail;
1264f4f154fdSAkinobu Mita 	put_task_struct(task);
1265cba8aafeSVincent Li 
1266cba8aafeSVincent Li 	return count;
1267f4f154fdSAkinobu Mita }
1268f4f154fdSAkinobu Mita 
126900977a59SArjan van de Ven static const struct file_operations proc_fault_inject_operations = {
1270f4f154fdSAkinobu Mita 	.read		= proc_fault_inject_read,
1271f4f154fdSAkinobu Mita 	.write		= proc_fault_inject_write,
127287df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
1273f4f154fdSAkinobu Mita };
1274f4f154fdSAkinobu Mita #endif
1275f4f154fdSAkinobu Mita 
12769745512cSArjan van de Ven 
127743ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
127843ae34cbSIngo Molnar /*
127943ae34cbSIngo Molnar  * Print out various scheduling related per-task fields:
128043ae34cbSIngo Molnar  */
128143ae34cbSIngo Molnar static int sched_show(struct seq_file *m, void *v)
128243ae34cbSIngo Molnar {
128343ae34cbSIngo Molnar 	struct inode *inode = m->private;
128443ae34cbSIngo Molnar 	struct task_struct *p;
128543ae34cbSIngo Molnar 
128643ae34cbSIngo Molnar 	p = get_proc_task(inode);
128743ae34cbSIngo Molnar 	if (!p)
128843ae34cbSIngo Molnar 		return -ESRCH;
128943ae34cbSIngo Molnar 	proc_sched_show_task(p, m);
129043ae34cbSIngo Molnar 
129143ae34cbSIngo Molnar 	put_task_struct(p);
129243ae34cbSIngo Molnar 
129343ae34cbSIngo Molnar 	return 0;
129443ae34cbSIngo Molnar }
129543ae34cbSIngo Molnar 
129643ae34cbSIngo Molnar static ssize_t
129743ae34cbSIngo Molnar sched_write(struct file *file, const char __user *buf,
129843ae34cbSIngo Molnar 	    size_t count, loff_t *offset)
129943ae34cbSIngo Molnar {
130043ae34cbSIngo Molnar 	struct inode *inode = file->f_path.dentry->d_inode;
130143ae34cbSIngo Molnar 	struct task_struct *p;
130243ae34cbSIngo Molnar 
130343ae34cbSIngo Molnar 	p = get_proc_task(inode);
130443ae34cbSIngo Molnar 	if (!p)
130543ae34cbSIngo Molnar 		return -ESRCH;
130643ae34cbSIngo Molnar 	proc_sched_set_task(p);
130743ae34cbSIngo Molnar 
130843ae34cbSIngo Molnar 	put_task_struct(p);
130943ae34cbSIngo Molnar 
131043ae34cbSIngo Molnar 	return count;
131143ae34cbSIngo Molnar }
131243ae34cbSIngo Molnar 
131343ae34cbSIngo Molnar static int sched_open(struct inode *inode, struct file *filp)
131443ae34cbSIngo Molnar {
1315c6a34058SJovi Zhang 	return single_open(filp, sched_show, inode);
131643ae34cbSIngo Molnar }
131743ae34cbSIngo Molnar 
131843ae34cbSIngo Molnar static const struct file_operations proc_pid_sched_operations = {
131943ae34cbSIngo Molnar 	.open		= sched_open,
132043ae34cbSIngo Molnar 	.read		= seq_read,
132143ae34cbSIngo Molnar 	.write		= sched_write,
132243ae34cbSIngo Molnar 	.llseek		= seq_lseek,
13235ea473a1SAlexey Dobriyan 	.release	= single_release,
132443ae34cbSIngo Molnar };
132543ae34cbSIngo Molnar 
132643ae34cbSIngo Molnar #endif
132743ae34cbSIngo Molnar 
13285091faa4SMike Galbraith #ifdef CONFIG_SCHED_AUTOGROUP
13295091faa4SMike Galbraith /*
13305091faa4SMike Galbraith  * Print out autogroup related information:
13315091faa4SMike Galbraith  */
13325091faa4SMike Galbraith static int sched_autogroup_show(struct seq_file *m, void *v)
13335091faa4SMike Galbraith {
13345091faa4SMike Galbraith 	struct inode *inode = m->private;
13355091faa4SMike Galbraith 	struct task_struct *p;
13365091faa4SMike Galbraith 
13375091faa4SMike Galbraith 	p = get_proc_task(inode);
13385091faa4SMike Galbraith 	if (!p)
13395091faa4SMike Galbraith 		return -ESRCH;
13405091faa4SMike Galbraith 	proc_sched_autogroup_show_task(p, m);
13415091faa4SMike Galbraith 
13425091faa4SMike Galbraith 	put_task_struct(p);
13435091faa4SMike Galbraith 
13445091faa4SMike Galbraith 	return 0;
13455091faa4SMike Galbraith }
13465091faa4SMike Galbraith 
13475091faa4SMike Galbraith static ssize_t
13485091faa4SMike Galbraith sched_autogroup_write(struct file *file, const char __user *buf,
13495091faa4SMike Galbraith 	    size_t count, loff_t *offset)
13505091faa4SMike Galbraith {
13515091faa4SMike Galbraith 	struct inode *inode = file->f_path.dentry->d_inode;
13525091faa4SMike Galbraith 	struct task_struct *p;
13535091faa4SMike Galbraith 	char buffer[PROC_NUMBUF];
13540a8cb8e3SAlexey Dobriyan 	int nice;
13555091faa4SMike Galbraith 	int err;
13565091faa4SMike Galbraith 
13575091faa4SMike Galbraith 	memset(buffer, 0, sizeof(buffer));
13585091faa4SMike Galbraith 	if (count > sizeof(buffer) - 1)
13595091faa4SMike Galbraith 		count = sizeof(buffer) - 1;
13605091faa4SMike Galbraith 	if (copy_from_user(buffer, buf, count))
13615091faa4SMike Galbraith 		return -EFAULT;
13625091faa4SMike Galbraith 
13630a8cb8e3SAlexey Dobriyan 	err = kstrtoint(strstrip(buffer), 0, &nice);
13640a8cb8e3SAlexey Dobriyan 	if (err < 0)
13650a8cb8e3SAlexey Dobriyan 		return err;
13665091faa4SMike Galbraith 
13675091faa4SMike Galbraith 	p = get_proc_task(inode);
13685091faa4SMike Galbraith 	if (!p)
13695091faa4SMike Galbraith 		return -ESRCH;
13705091faa4SMike Galbraith 
13715091faa4SMike Galbraith 	err = nice;
13725091faa4SMike Galbraith 	err = proc_sched_autogroup_set_nice(p, &err);
13735091faa4SMike Galbraith 	if (err)
13745091faa4SMike Galbraith 		count = err;
13755091faa4SMike Galbraith 
13765091faa4SMike Galbraith 	put_task_struct(p);
13775091faa4SMike Galbraith 
13785091faa4SMike Galbraith 	return count;
13795091faa4SMike Galbraith }
13805091faa4SMike Galbraith 
13815091faa4SMike Galbraith static int sched_autogroup_open(struct inode *inode, struct file *filp)
13825091faa4SMike Galbraith {
13835091faa4SMike Galbraith 	int ret;
13845091faa4SMike Galbraith 
13855091faa4SMike Galbraith 	ret = single_open(filp, sched_autogroup_show, NULL);
13865091faa4SMike Galbraith 	if (!ret) {
13875091faa4SMike Galbraith 		struct seq_file *m = filp->private_data;
13885091faa4SMike Galbraith 
13895091faa4SMike Galbraith 		m->private = inode;
13905091faa4SMike Galbraith 	}
13915091faa4SMike Galbraith 	return ret;
13925091faa4SMike Galbraith }
13935091faa4SMike Galbraith 
13945091faa4SMike Galbraith static const struct file_operations proc_pid_sched_autogroup_operations = {
13955091faa4SMike Galbraith 	.open		= sched_autogroup_open,
13965091faa4SMike Galbraith 	.read		= seq_read,
13975091faa4SMike Galbraith 	.write		= sched_autogroup_write,
13985091faa4SMike Galbraith 	.llseek		= seq_lseek,
13995091faa4SMike Galbraith 	.release	= single_release,
14005091faa4SMike Galbraith };
14015091faa4SMike Galbraith 
14025091faa4SMike Galbraith #endif /* CONFIG_SCHED_AUTOGROUP */
14035091faa4SMike Galbraith 
14044614a696Sjohn stultz static ssize_t comm_write(struct file *file, const char __user *buf,
14054614a696Sjohn stultz 				size_t count, loff_t *offset)
14064614a696Sjohn stultz {
14074614a696Sjohn stultz 	struct inode *inode = file->f_path.dentry->d_inode;
14084614a696Sjohn stultz 	struct task_struct *p;
14094614a696Sjohn stultz 	char buffer[TASK_COMM_LEN];
14104614a696Sjohn stultz 
14114614a696Sjohn stultz 	memset(buffer, 0, sizeof(buffer));
14124614a696Sjohn stultz 	if (count > sizeof(buffer) - 1)
14134614a696Sjohn stultz 		count = sizeof(buffer) - 1;
14144614a696Sjohn stultz 	if (copy_from_user(buffer, buf, count))
14154614a696Sjohn stultz 		return -EFAULT;
14164614a696Sjohn stultz 
14174614a696Sjohn stultz 	p = get_proc_task(inode);
14184614a696Sjohn stultz 	if (!p)
14194614a696Sjohn stultz 		return -ESRCH;
14204614a696Sjohn stultz 
14214614a696Sjohn stultz 	if (same_thread_group(current, p))
14224614a696Sjohn stultz 		set_task_comm(p, buffer);
14234614a696Sjohn stultz 	else
14244614a696Sjohn stultz 		count = -EINVAL;
14254614a696Sjohn stultz 
14264614a696Sjohn stultz 	put_task_struct(p);
14274614a696Sjohn stultz 
14284614a696Sjohn stultz 	return count;
14294614a696Sjohn stultz }
14304614a696Sjohn stultz 
14314614a696Sjohn stultz static int comm_show(struct seq_file *m, void *v)
14324614a696Sjohn stultz {
14334614a696Sjohn stultz 	struct inode *inode = m->private;
14344614a696Sjohn stultz 	struct task_struct *p;
14354614a696Sjohn stultz 
14364614a696Sjohn stultz 	p = get_proc_task(inode);
14374614a696Sjohn stultz 	if (!p)
14384614a696Sjohn stultz 		return -ESRCH;
14394614a696Sjohn stultz 
14404614a696Sjohn stultz 	task_lock(p);
14414614a696Sjohn stultz 	seq_printf(m, "%s\n", p->comm);
14424614a696Sjohn stultz 	task_unlock(p);
14434614a696Sjohn stultz 
14444614a696Sjohn stultz 	put_task_struct(p);
14454614a696Sjohn stultz 
14464614a696Sjohn stultz 	return 0;
14474614a696Sjohn stultz }
14484614a696Sjohn stultz 
14494614a696Sjohn stultz static int comm_open(struct inode *inode, struct file *filp)
14504614a696Sjohn stultz {
1451c6a34058SJovi Zhang 	return single_open(filp, comm_show, inode);
14524614a696Sjohn stultz }
14534614a696Sjohn stultz 
14544614a696Sjohn stultz static const struct file_operations proc_pid_set_comm_operations = {
14554614a696Sjohn stultz 	.open		= comm_open,
14564614a696Sjohn stultz 	.read		= seq_read,
14574614a696Sjohn stultz 	.write		= comm_write,
14584614a696Sjohn stultz 	.llseek		= seq_lseek,
14594614a696Sjohn stultz 	.release	= single_release,
14604614a696Sjohn stultz };
14614614a696Sjohn stultz 
14627773fbc5SCyrill Gorcunov static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
1463925d1c40SMatt Helsley {
1464925d1c40SMatt Helsley 	struct task_struct *task;
1465925d1c40SMatt Helsley 	struct mm_struct *mm;
1466925d1c40SMatt Helsley 	struct file *exe_file;
1467925d1c40SMatt Helsley 
14687773fbc5SCyrill Gorcunov 	task = get_proc_task(dentry->d_inode);
1469925d1c40SMatt Helsley 	if (!task)
1470925d1c40SMatt Helsley 		return -ENOENT;
1471925d1c40SMatt Helsley 	mm = get_task_mm(task);
1472925d1c40SMatt Helsley 	put_task_struct(task);
1473925d1c40SMatt Helsley 	if (!mm)
1474925d1c40SMatt Helsley 		return -ENOENT;
1475925d1c40SMatt Helsley 	exe_file = get_mm_exe_file(mm);
1476925d1c40SMatt Helsley 	mmput(mm);
1477925d1c40SMatt Helsley 	if (exe_file) {
1478925d1c40SMatt Helsley 		*exe_path = exe_file->f_path;
1479925d1c40SMatt Helsley 		path_get(&exe_file->f_path);
1480925d1c40SMatt Helsley 		fput(exe_file);
1481925d1c40SMatt Helsley 		return 0;
1482925d1c40SMatt Helsley 	} else
1483925d1c40SMatt Helsley 		return -ENOENT;
1484925d1c40SMatt Helsley }
1485925d1c40SMatt Helsley 
1486008b150aSAl Viro static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
14871da177e4SLinus Torvalds {
14881da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
14891da177e4SLinus Torvalds 	int error = -EACCES;
14901da177e4SLinus Torvalds 
14911da177e4SLinus Torvalds 	/* We don't need a base pointer in the /proc filesystem */
14921d957f9bSJan Blunck 	path_put(&nd->path);
14931da177e4SLinus Torvalds 
1494778c1144SEric W. Biederman 	/* Are we allowed to snoop on the tasks file descriptors? */
1495778c1144SEric W. Biederman 	if (!proc_fd_access_allowed(inode))
14961da177e4SLinus Torvalds 		goto out;
14971da177e4SLinus Torvalds 
14987773fbc5SCyrill Gorcunov 	error = PROC_I(inode)->op.proc_get_link(dentry, &nd->path);
14991da177e4SLinus Torvalds out:
1500008b150aSAl Viro 	return ERR_PTR(error);
15011da177e4SLinus Torvalds }
15021da177e4SLinus Torvalds 
15033dcd25f3SJan Blunck static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
15041da177e4SLinus Torvalds {
1505e12ba74dSMel Gorman 	char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
15063dcd25f3SJan Blunck 	char *pathname;
15071da177e4SLinus Torvalds 	int len;
15081da177e4SLinus Torvalds 
15091da177e4SLinus Torvalds 	if (!tmp)
15101da177e4SLinus Torvalds 		return -ENOMEM;
15111da177e4SLinus Torvalds 
15127b2a69baSEric W. Biederman 	pathname = d_path(path, tmp, PAGE_SIZE);
15133dcd25f3SJan Blunck 	len = PTR_ERR(pathname);
15143dcd25f3SJan Blunck 	if (IS_ERR(pathname))
15151da177e4SLinus Torvalds 		goto out;
15163dcd25f3SJan Blunck 	len = tmp + PAGE_SIZE - 1 - pathname;
15171da177e4SLinus Torvalds 
15181da177e4SLinus Torvalds 	if (len > buflen)
15191da177e4SLinus Torvalds 		len = buflen;
15203dcd25f3SJan Blunck 	if (copy_to_user(buffer, pathname, len))
15211da177e4SLinus Torvalds 		len = -EFAULT;
15221da177e4SLinus Torvalds  out:
15231da177e4SLinus Torvalds 	free_page((unsigned long)tmp);
15241da177e4SLinus Torvalds 	return len;
15251da177e4SLinus Torvalds }
15261da177e4SLinus Torvalds 
15271da177e4SLinus Torvalds static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
15281da177e4SLinus Torvalds {
15291da177e4SLinus Torvalds 	int error = -EACCES;
15301da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
15313dcd25f3SJan Blunck 	struct path path;
15321da177e4SLinus Torvalds 
1533778c1144SEric W. Biederman 	/* Are we allowed to snoop on the tasks file descriptors? */
1534778c1144SEric W. Biederman 	if (!proc_fd_access_allowed(inode))
15351da177e4SLinus Torvalds 		goto out;
15361da177e4SLinus Torvalds 
15377773fbc5SCyrill Gorcunov 	error = PROC_I(inode)->op.proc_get_link(dentry, &path);
15381da177e4SLinus Torvalds 	if (error)
15391da177e4SLinus Torvalds 		goto out;
15401da177e4SLinus Torvalds 
15413dcd25f3SJan Blunck 	error = do_proc_readlink(&path, buffer, buflen);
15423dcd25f3SJan Blunck 	path_put(&path);
15431da177e4SLinus Torvalds out:
15441da177e4SLinus Torvalds 	return error;
15451da177e4SLinus Torvalds }
15461da177e4SLinus Torvalds 
1547c5ef1c42SArjan van de Ven static const struct inode_operations proc_pid_link_inode_operations = {
15481da177e4SLinus Torvalds 	.readlink	= proc_pid_readlink,
15496d76fa58SLinus Torvalds 	.follow_link	= proc_pid_follow_link,
15506d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
15511da177e4SLinus Torvalds };
15521da177e4SLinus Torvalds 
155328a6d671SEric W. Biederman 
155428a6d671SEric W. Biederman /* building an inode */
155528a6d671SEric W. Biederman 
155628a6d671SEric W. Biederman static int task_dumpable(struct task_struct *task)
155728a6d671SEric W. Biederman {
155828a6d671SEric W. Biederman 	int dumpable = 0;
155928a6d671SEric W. Biederman 	struct mm_struct *mm;
156028a6d671SEric W. Biederman 
156128a6d671SEric W. Biederman 	task_lock(task);
156228a6d671SEric W. Biederman 	mm = task->mm;
156328a6d671SEric W. Biederman 	if (mm)
15646c5d5238SKawai, Hidehiro 		dumpable = get_dumpable(mm);
156528a6d671SEric W. Biederman 	task_unlock(task);
156628a6d671SEric W. Biederman 	if(dumpable == 1)
156728a6d671SEric W. Biederman 		return 1;
156828a6d671SEric W. Biederman 	return 0;
156928a6d671SEric W. Biederman }
157028a6d671SEric W. Biederman 
15716b4e306aSEric W. Biederman struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
157228a6d671SEric W. Biederman {
157328a6d671SEric W. Biederman 	struct inode * inode;
157428a6d671SEric W. Biederman 	struct proc_inode *ei;
1575c69e8d9cSDavid Howells 	const struct cred *cred;
157628a6d671SEric W. Biederman 
157728a6d671SEric W. Biederman 	/* We need a new inode */
157828a6d671SEric W. Biederman 
157928a6d671SEric W. Biederman 	inode = new_inode(sb);
158028a6d671SEric W. Biederman 	if (!inode)
158128a6d671SEric W. Biederman 		goto out;
158228a6d671SEric W. Biederman 
158328a6d671SEric W. Biederman 	/* Common stuff */
158428a6d671SEric W. Biederman 	ei = PROC_I(inode);
158585fe4025SChristoph Hellwig 	inode->i_ino = get_next_ino();
158628a6d671SEric W. Biederman 	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
158728a6d671SEric W. Biederman 	inode->i_op = &proc_def_inode_operations;
158828a6d671SEric W. Biederman 
158928a6d671SEric W. Biederman 	/*
159028a6d671SEric W. Biederman 	 * grab the reference to task.
159128a6d671SEric W. Biederman 	 */
15921a657f78SOleg Nesterov 	ei->pid = get_task_pid(task, PIDTYPE_PID);
159328a6d671SEric W. Biederman 	if (!ei->pid)
159428a6d671SEric W. Biederman 		goto out_unlock;
159528a6d671SEric W. Biederman 
159628a6d671SEric W. Biederman 	if (task_dumpable(task)) {
1597c69e8d9cSDavid Howells 		rcu_read_lock();
1598c69e8d9cSDavid Howells 		cred = __task_cred(task);
1599c69e8d9cSDavid Howells 		inode->i_uid = cred->euid;
1600c69e8d9cSDavid Howells 		inode->i_gid = cred->egid;
1601c69e8d9cSDavid Howells 		rcu_read_unlock();
160228a6d671SEric W. Biederman 	}
160328a6d671SEric W. Biederman 	security_task_to_inode(task, inode);
160428a6d671SEric W. Biederman 
160528a6d671SEric W. Biederman out:
160628a6d671SEric W. Biederman 	return inode;
160728a6d671SEric W. Biederman 
160828a6d671SEric W. Biederman out_unlock:
160928a6d671SEric W. Biederman 	iput(inode);
161028a6d671SEric W. Biederman 	return NULL;
161128a6d671SEric W. Biederman }
161228a6d671SEric W. Biederman 
16136b4e306aSEric W. Biederman int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
161428a6d671SEric W. Biederman {
161528a6d671SEric W. Biederman 	struct inode *inode = dentry->d_inode;
161628a6d671SEric W. Biederman 	struct task_struct *task;
1617c69e8d9cSDavid Howells 	const struct cred *cred;
1618c69e8d9cSDavid Howells 
161928a6d671SEric W. Biederman 	generic_fillattr(inode, stat);
162028a6d671SEric W. Biederman 
162128a6d671SEric W. Biederman 	rcu_read_lock();
162228a6d671SEric W. Biederman 	stat->uid = 0;
162328a6d671SEric W. Biederman 	stat->gid = 0;
162428a6d671SEric W. Biederman 	task = pid_task(proc_pid(inode), PIDTYPE_PID);
162528a6d671SEric W. Biederman 	if (task) {
162628a6d671SEric W. Biederman 		if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
162728a6d671SEric W. Biederman 		    task_dumpable(task)) {
1628c69e8d9cSDavid Howells 			cred = __task_cred(task);
1629c69e8d9cSDavid Howells 			stat->uid = cred->euid;
1630c69e8d9cSDavid Howells 			stat->gid = cred->egid;
163128a6d671SEric W. Biederman 		}
163228a6d671SEric W. Biederman 	}
163328a6d671SEric W. Biederman 	rcu_read_unlock();
163428a6d671SEric W. Biederman 	return 0;
163528a6d671SEric W. Biederman }
163628a6d671SEric W. Biederman 
163728a6d671SEric W. Biederman /* dentry stuff */
163828a6d671SEric W. Biederman 
163928a6d671SEric W. Biederman /*
164028a6d671SEric W. Biederman  *	Exceptional case: normally we are not allowed to unhash a busy
164128a6d671SEric W. Biederman  * directory. In this case, however, we can do it - no aliasing problems
164228a6d671SEric W. Biederman  * due to the way we treat inodes.
164328a6d671SEric W. Biederman  *
164428a6d671SEric W. Biederman  * Rewrite the inode's ownerships here because the owning task may have
164528a6d671SEric W. Biederman  * performed a setuid(), etc.
164628a6d671SEric W. Biederman  *
164728a6d671SEric W. Biederman  * Before the /proc/pid/status file was created the only way to read
164828a6d671SEric W. Biederman  * the effective uid of a /process was to stat /proc/pid.  Reading
164928a6d671SEric W. Biederman  * /proc/pid/status is slow enough that procps and other packages
165028a6d671SEric W. Biederman  * kept stating /proc/pid.  To keep the rules in /proc simple I have
165128a6d671SEric W. Biederman  * made this apply to all per process world readable and executable
165228a6d671SEric W. Biederman  * directories.
165328a6d671SEric W. Biederman  */
16546b4e306aSEric W. Biederman int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
165528a6d671SEric W. Biederman {
165634286d66SNick Piggin 	struct inode *inode;
165734286d66SNick Piggin 	struct task_struct *task;
1658c69e8d9cSDavid Howells 	const struct cred *cred;
1659c69e8d9cSDavid Howells 
166034286d66SNick Piggin 	if (nd && nd->flags & LOOKUP_RCU)
166134286d66SNick Piggin 		return -ECHILD;
166234286d66SNick Piggin 
166334286d66SNick Piggin 	inode = dentry->d_inode;
166434286d66SNick Piggin 	task = get_proc_task(inode);
166534286d66SNick Piggin 
166628a6d671SEric W. Biederman 	if (task) {
166728a6d671SEric W. Biederman 		if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
166828a6d671SEric W. Biederman 		    task_dumpable(task)) {
1669c69e8d9cSDavid Howells 			rcu_read_lock();
1670c69e8d9cSDavid Howells 			cred = __task_cred(task);
1671c69e8d9cSDavid Howells 			inode->i_uid = cred->euid;
1672c69e8d9cSDavid Howells 			inode->i_gid = cred->egid;
1673c69e8d9cSDavid Howells 			rcu_read_unlock();
167428a6d671SEric W. Biederman 		} else {
167528a6d671SEric W. Biederman 			inode->i_uid = 0;
167628a6d671SEric W. Biederman 			inode->i_gid = 0;
167728a6d671SEric W. Biederman 		}
167828a6d671SEric W. Biederman 		inode->i_mode &= ~(S_ISUID | S_ISGID);
167928a6d671SEric W. Biederman 		security_task_to_inode(task, inode);
168028a6d671SEric W. Biederman 		put_task_struct(task);
168128a6d671SEric W. Biederman 		return 1;
168228a6d671SEric W. Biederman 	}
168328a6d671SEric W. Biederman 	d_drop(dentry);
168428a6d671SEric W. Biederman 	return 0;
168528a6d671SEric W. Biederman }
168628a6d671SEric W. Biederman 
1687fe15ce44SNick Piggin static int pid_delete_dentry(const struct dentry * dentry)
168828a6d671SEric W. Biederman {
168928a6d671SEric W. Biederman 	/* Is the task we represent dead?
169028a6d671SEric W. Biederman 	 * If so, then don't put the dentry on the lru list,
169128a6d671SEric W. Biederman 	 * kill it immediately.
169228a6d671SEric W. Biederman 	 */
169328a6d671SEric W. Biederman 	return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
169428a6d671SEric W. Biederman }
169528a6d671SEric W. Biederman 
16966b4e306aSEric W. Biederman const struct dentry_operations pid_dentry_operations =
169728a6d671SEric W. Biederman {
169828a6d671SEric W. Biederman 	.d_revalidate	= pid_revalidate,
169928a6d671SEric W. Biederman 	.d_delete	= pid_delete_dentry,
170028a6d671SEric W. Biederman };
170128a6d671SEric W. Biederman 
170228a6d671SEric W. Biederman /* Lookups */
170328a6d671SEric W. Biederman 
17041c0d04c9SEric W. Biederman /*
17051c0d04c9SEric W. Biederman  * Fill a directory entry.
17061c0d04c9SEric W. Biederman  *
17071c0d04c9SEric W. Biederman  * If possible create the dcache entry and derive our inode number and
17081c0d04c9SEric W. Biederman  * file type from dcache entry.
17091c0d04c9SEric W. Biederman  *
17101c0d04c9SEric W. Biederman  * Since all of the proc inode numbers are dynamically generated, the inode
17111c0d04c9SEric W. Biederman  * numbers do not exist until the inode is cache.  This means creating the
17121c0d04c9SEric W. Biederman  * the dcache entry in readdir is necessary to keep the inode numbers
17131c0d04c9SEric W. Biederman  * reported by readdir in sync with the inode numbers reported
17141c0d04c9SEric W. Biederman  * by stat.
17151c0d04c9SEric W. Biederman  */
17166b4e306aSEric W. Biederman int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
17176b4e306aSEric W. Biederman 	const char *name, int len,
1718c5141e6dSEric Dumazet 	instantiate_t instantiate, struct task_struct *task, const void *ptr)
171961a28784SEric W. Biederman {
17202fddfeefSJosef "Jeff" Sipek 	struct dentry *child, *dir = filp->f_path.dentry;
172161a28784SEric W. Biederman 	struct inode *inode;
172261a28784SEric W. Biederman 	struct qstr qname;
172361a28784SEric W. Biederman 	ino_t ino = 0;
172461a28784SEric W. Biederman 	unsigned type = DT_UNKNOWN;
172561a28784SEric W. Biederman 
172661a28784SEric W. Biederman 	qname.name = name;
172761a28784SEric W. Biederman 	qname.len  = len;
172861a28784SEric W. Biederman 	qname.hash = full_name_hash(name, len);
172961a28784SEric W. Biederman 
173061a28784SEric W. Biederman 	child = d_lookup(dir, &qname);
173161a28784SEric W. Biederman 	if (!child) {
173261a28784SEric W. Biederman 		struct dentry *new;
173361a28784SEric W. Biederman 		new = d_alloc(dir, &qname);
173461a28784SEric W. Biederman 		if (new) {
173561a28784SEric W. Biederman 			child = instantiate(dir->d_inode, new, task, ptr);
173661a28784SEric W. Biederman 			if (child)
173761a28784SEric W. Biederman 				dput(new);
173861a28784SEric W. Biederman 			else
173961a28784SEric W. Biederman 				child = new;
174061a28784SEric W. Biederman 		}
174161a28784SEric W. Biederman 	}
174261a28784SEric W. Biederman 	if (!child || IS_ERR(child) || !child->d_inode)
174361a28784SEric W. Biederman 		goto end_instantiate;
174461a28784SEric W. Biederman 	inode = child->d_inode;
174561a28784SEric W. Biederman 	if (inode) {
174661a28784SEric W. Biederman 		ino = inode->i_ino;
174761a28784SEric W. Biederman 		type = inode->i_mode >> 12;
174861a28784SEric W. Biederman 	}
174961a28784SEric W. Biederman 	dput(child);
175061a28784SEric W. Biederman end_instantiate:
175161a28784SEric W. Biederman 	if (!ino)
175261a28784SEric W. Biederman 		ino = find_inode_number(dir, &qname);
175361a28784SEric W. Biederman 	if (!ino)
175461a28784SEric W. Biederman 		ino = 1;
175561a28784SEric W. Biederman 	return filldir(dirent, name, len, filp->f_pos, ino, type);
175661a28784SEric W. Biederman }
175761a28784SEric W. Biederman 
175828a6d671SEric W. Biederman static unsigned name_to_int(struct dentry *dentry)
175928a6d671SEric W. Biederman {
176028a6d671SEric W. Biederman 	const char *name = dentry->d_name.name;
176128a6d671SEric W. Biederman 	int len = dentry->d_name.len;
176228a6d671SEric W. Biederman 	unsigned n = 0;
176328a6d671SEric W. Biederman 
176428a6d671SEric W. Biederman 	if (len > 1 && *name == '0')
176528a6d671SEric W. Biederman 		goto out;
176628a6d671SEric W. Biederman 	while (len-- > 0) {
176728a6d671SEric W. Biederman 		unsigned c = *name++ - '0';
176828a6d671SEric W. Biederman 		if (c > 9)
176928a6d671SEric W. Biederman 			goto out;
177028a6d671SEric W. Biederman 		if (n >= (~0U-9)/10)
177128a6d671SEric W. Biederman 			goto out;
177228a6d671SEric W. Biederman 		n *= 10;
177328a6d671SEric W. Biederman 		n += c;
177428a6d671SEric W. Biederman 	}
177528a6d671SEric W. Biederman 	return n;
177628a6d671SEric W. Biederman out:
177728a6d671SEric W. Biederman 	return ~0U;
177828a6d671SEric W. Biederman }
177928a6d671SEric W. Biederman 
178027932742SMiklos Szeredi #define PROC_FDINFO_MAX 64
178127932742SMiklos Szeredi 
17823dcd25f3SJan Blunck static int proc_fd_info(struct inode *inode, struct path *path, char *info)
178328a6d671SEric W. Biederman {
17845e442a49SLinus Torvalds 	struct task_struct *task = get_proc_task(inode);
17855e442a49SLinus Torvalds 	struct files_struct *files = NULL;
178628a6d671SEric W. Biederman 	struct file *file;
178728a6d671SEric W. Biederman 	int fd = proc_fd(inode);
178828a6d671SEric W. Biederman 
17895e442a49SLinus Torvalds 	if (task) {
179028a6d671SEric W. Biederman 		files = get_files_struct(task);
17915e442a49SLinus Torvalds 		put_task_struct(task);
17925e442a49SLinus Torvalds 	}
17935e442a49SLinus Torvalds 	if (files) {
179428a6d671SEric W. Biederman 		/*
179528a6d671SEric W. Biederman 		 * We are not taking a ref to the file structure, so we must
179628a6d671SEric W. Biederman 		 * hold ->file_lock.
179728a6d671SEric W. Biederman 		 */
179828a6d671SEric W. Biederman 		spin_lock(&files->file_lock);
179928a6d671SEric W. Biederman 		file = fcheck_files(files, fd);
180028a6d671SEric W. Biederman 		if (file) {
18011117f72eSLinus Torvalds 			unsigned int f_flags;
18021117f72eSLinus Torvalds 			struct fdtable *fdt;
18031117f72eSLinus Torvalds 
18041117f72eSLinus Torvalds 			fdt = files_fdtable(files);
18051117f72eSLinus Torvalds 			f_flags = file->f_flags & ~O_CLOEXEC;
18061117f72eSLinus Torvalds 			if (FD_ISSET(fd, fdt->close_on_exec))
18071117f72eSLinus Torvalds 				f_flags |= O_CLOEXEC;
18081117f72eSLinus Torvalds 
18093dcd25f3SJan Blunck 			if (path) {
18103dcd25f3SJan Blunck 				*path = file->f_path;
18113dcd25f3SJan Blunck 				path_get(&file->f_path);
18123dcd25f3SJan Blunck 			}
181327932742SMiklos Szeredi 			if (info)
181427932742SMiklos Szeredi 				snprintf(info, PROC_FDINFO_MAX,
181527932742SMiklos Szeredi 					 "pos:\t%lli\n"
181627932742SMiklos Szeredi 					 "flags:\t0%o\n",
181727932742SMiklos Szeredi 					 (long long) file->f_pos,
18181117f72eSLinus Torvalds 					 f_flags);
181928a6d671SEric W. Biederman 			spin_unlock(&files->file_lock);
182028a6d671SEric W. Biederman 			put_files_struct(files);
18215e442a49SLinus Torvalds 			return 0;
18225e442a49SLinus Torvalds 		}
18235e442a49SLinus Torvalds 		spin_unlock(&files->file_lock);
18245e442a49SLinus Torvalds 		put_files_struct(files);
18255e442a49SLinus Torvalds 	}
18265e442a49SLinus Torvalds 	return -ENOENT;
182728a6d671SEric W. Biederman }
182828a6d671SEric W. Biederman 
18297773fbc5SCyrill Gorcunov static int proc_fd_link(struct dentry *dentry, struct path *path)
183027932742SMiklos Szeredi {
18317773fbc5SCyrill Gorcunov 	return proc_fd_info(dentry->d_inode, path, NULL);
183227932742SMiklos Szeredi }
183327932742SMiklos Szeredi 
183428a6d671SEric W. Biederman static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
183528a6d671SEric W. Biederman {
183634286d66SNick Piggin 	struct inode *inode;
183734286d66SNick Piggin 	struct task_struct *task;
183834286d66SNick Piggin 	int fd;
183928a6d671SEric W. Biederman 	struct files_struct *files;
1840c69e8d9cSDavid Howells 	const struct cred *cred;
184128a6d671SEric W. Biederman 
184234286d66SNick Piggin 	if (nd && nd->flags & LOOKUP_RCU)
184334286d66SNick Piggin 		return -ECHILD;
184434286d66SNick Piggin 
184534286d66SNick Piggin 	inode = dentry->d_inode;
184634286d66SNick Piggin 	task = get_proc_task(inode);
184734286d66SNick Piggin 	fd = proc_fd(inode);
184834286d66SNick Piggin 
184928a6d671SEric W. Biederman 	if (task) {
185028a6d671SEric W. Biederman 		files = get_files_struct(task);
185128a6d671SEric W. Biederman 		if (files) {
185228a6d671SEric W. Biederman 			rcu_read_lock();
185328a6d671SEric W. Biederman 			if (fcheck_files(files, fd)) {
185428a6d671SEric W. Biederman 				rcu_read_unlock();
185528a6d671SEric W. Biederman 				put_files_struct(files);
185628a6d671SEric W. Biederman 				if (task_dumpable(task)) {
1857c69e8d9cSDavid Howells 					rcu_read_lock();
1858c69e8d9cSDavid Howells 					cred = __task_cred(task);
1859c69e8d9cSDavid Howells 					inode->i_uid = cred->euid;
1860c69e8d9cSDavid Howells 					inode->i_gid = cred->egid;
1861c69e8d9cSDavid Howells 					rcu_read_unlock();
186228a6d671SEric W. Biederman 				} else {
186328a6d671SEric W. Biederman 					inode->i_uid = 0;
186428a6d671SEric W. Biederman 					inode->i_gid = 0;
186528a6d671SEric W. Biederman 				}
186628a6d671SEric W. Biederman 				inode->i_mode &= ~(S_ISUID | S_ISGID);
186728a6d671SEric W. Biederman 				security_task_to_inode(task, inode);
186828a6d671SEric W. Biederman 				put_task_struct(task);
186928a6d671SEric W. Biederman 				return 1;
187028a6d671SEric W. Biederman 			}
187128a6d671SEric W. Biederman 			rcu_read_unlock();
187228a6d671SEric W. Biederman 			put_files_struct(files);
187328a6d671SEric W. Biederman 		}
187428a6d671SEric W. Biederman 		put_task_struct(task);
187528a6d671SEric W. Biederman 	}
187628a6d671SEric W. Biederman 	d_drop(dentry);
187728a6d671SEric W. Biederman 	return 0;
187828a6d671SEric W. Biederman }
187928a6d671SEric W. Biederman 
1880d72f71ebSAl Viro static const struct dentry_operations tid_fd_dentry_operations =
188128a6d671SEric W. Biederman {
188228a6d671SEric W. Biederman 	.d_revalidate	= tid_fd_revalidate,
188328a6d671SEric W. Biederman 	.d_delete	= pid_delete_dentry,
188428a6d671SEric W. Biederman };
188528a6d671SEric W. Biederman 
1886444ceed8SEric W. Biederman static struct dentry *proc_fd_instantiate(struct inode *dir,
1887c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
188828a6d671SEric W. Biederman {
1889c5141e6dSEric Dumazet 	unsigned fd = *(const unsigned *)ptr;
189028a6d671SEric W. Biederman 	struct file *file;
189128a6d671SEric W. Biederman 	struct files_struct *files;
189228a6d671SEric W. Biederman  	struct inode *inode;
189328a6d671SEric W. Biederman  	struct proc_inode *ei;
1894444ceed8SEric W. Biederman 	struct dentry *error = ERR_PTR(-ENOENT);
189528a6d671SEric W. Biederman 
189661a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
189728a6d671SEric W. Biederman 	if (!inode)
189828a6d671SEric W. Biederman 		goto out;
189928a6d671SEric W. Biederman 	ei = PROC_I(inode);
190028a6d671SEric W. Biederman 	ei->fd = fd;
190128a6d671SEric W. Biederman 	files = get_files_struct(task);
190228a6d671SEric W. Biederman 	if (!files)
1903444ceed8SEric W. Biederman 		goto out_iput;
190428a6d671SEric W. Biederman 	inode->i_mode = S_IFLNK;
190528a6d671SEric W. Biederman 
190628a6d671SEric W. Biederman 	/*
190728a6d671SEric W. Biederman 	 * We are not taking a ref to the file structure, so we must
190828a6d671SEric W. Biederman 	 * hold ->file_lock.
190928a6d671SEric W. Biederman 	 */
191028a6d671SEric W. Biederman 	spin_lock(&files->file_lock);
191128a6d671SEric W. Biederman 	file = fcheck_files(files, fd);
191228a6d671SEric W. Biederman 	if (!file)
1913444ceed8SEric W. Biederman 		goto out_unlock;
1914aeb5d727SAl Viro 	if (file->f_mode & FMODE_READ)
191528a6d671SEric W. Biederman 		inode->i_mode |= S_IRUSR | S_IXUSR;
1916aeb5d727SAl Viro 	if (file->f_mode & FMODE_WRITE)
191728a6d671SEric W. Biederman 		inode->i_mode |= S_IWUSR | S_IXUSR;
191828a6d671SEric W. Biederman 	spin_unlock(&files->file_lock);
191928a6d671SEric W. Biederman 	put_files_struct(files);
1920444ceed8SEric W. Biederman 
19215e442a49SLinus Torvalds 	inode->i_op = &proc_pid_link_inode_operations;
192228a6d671SEric W. Biederman 	inode->i_size = 64;
192328a6d671SEric W. Biederman 	ei->op.proc_get_link = proc_fd_link;
1924fb045adbSNick Piggin 	d_set_d_op(dentry, &tid_fd_dentry_operations);
192528a6d671SEric W. Biederman 	d_add(dentry, inode);
192628a6d671SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
192728a6d671SEric W. Biederman 	if (tid_fd_revalidate(dentry, NULL))
1928444ceed8SEric W. Biederman 		error = NULL;
1929444ceed8SEric W. Biederman 
1930444ceed8SEric W. Biederman  out:
1931444ceed8SEric W. Biederman 	return error;
1932444ceed8SEric W. Biederman out_unlock:
1933444ceed8SEric W. Biederman 	spin_unlock(&files->file_lock);
1934444ceed8SEric W. Biederman 	put_files_struct(files);
1935444ceed8SEric W. Biederman out_iput:
1936444ceed8SEric W. Biederman 	iput(inode);
1937444ceed8SEric W. Biederman 	goto out;
1938444ceed8SEric W. Biederman }
1939444ceed8SEric W. Biederman 
194027932742SMiklos Szeredi static struct dentry *proc_lookupfd_common(struct inode *dir,
194127932742SMiklos Szeredi 					   struct dentry *dentry,
194227932742SMiklos Szeredi 					   instantiate_t instantiate)
1943444ceed8SEric W. Biederman {
1944444ceed8SEric W. Biederman 	struct task_struct *task = get_proc_task(dir);
1945444ceed8SEric W. Biederman 	unsigned fd = name_to_int(dentry);
1946444ceed8SEric W. Biederman 	struct dentry *result = ERR_PTR(-ENOENT);
1947444ceed8SEric W. Biederman 
1948444ceed8SEric W. Biederman 	if (!task)
1949444ceed8SEric W. Biederman 		goto out_no_task;
1950444ceed8SEric W. Biederman 	if (fd == ~0U)
1951444ceed8SEric W. Biederman 		goto out;
1952444ceed8SEric W. Biederman 
195327932742SMiklos Szeredi 	result = instantiate(dir, dentry, task, &fd);
195428a6d671SEric W. Biederman out:
195528a6d671SEric W. Biederman 	put_task_struct(task);
195628a6d671SEric W. Biederman out_no_task:
195728a6d671SEric W. Biederman 	return result;
195828a6d671SEric W. Biederman }
195928a6d671SEric W. Biederman 
196027932742SMiklos Szeredi static int proc_readfd_common(struct file * filp, void * dirent,
196127932742SMiklos Szeredi 			      filldir_t filldir, instantiate_t instantiate)
19621da177e4SLinus Torvalds {
19632fddfeefSJosef "Jeff" Sipek 	struct dentry *dentry = filp->f_path.dentry;
19645634708bSEric W. Biederman 	struct inode *inode = dentry->d_inode;
196599f89551SEric W. Biederman 	struct task_struct *p = get_proc_task(inode);
1966457c2510SPavel Emelyanov 	unsigned int fd, ino;
19671da177e4SLinus Torvalds 	int retval;
19681da177e4SLinus Torvalds 	struct files_struct * files;
19691da177e4SLinus Torvalds 
19701da177e4SLinus Torvalds 	retval = -ENOENT;
197199f89551SEric W. Biederman 	if (!p)
197299f89551SEric W. Biederman 		goto out_no_task;
19731da177e4SLinus Torvalds 	retval = 0;
19741da177e4SLinus Torvalds 
19751da177e4SLinus Torvalds 	fd = filp->f_pos;
19761da177e4SLinus Torvalds 	switch (fd) {
19771da177e4SLinus Torvalds 		case 0:
19781da177e4SLinus Torvalds 			if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
19795e442a49SLinus Torvalds 				goto out;
19801da177e4SLinus Torvalds 			filp->f_pos++;
19811da177e4SLinus Torvalds 		case 1:
19825634708bSEric W. Biederman 			ino = parent_ino(dentry);
19831da177e4SLinus Torvalds 			if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
19845e442a49SLinus Torvalds 				goto out;
19851da177e4SLinus Torvalds 			filp->f_pos++;
19861da177e4SLinus Torvalds 		default:
19871da177e4SLinus Torvalds 			files = get_files_struct(p);
19881da177e4SLinus Torvalds 			if (!files)
19895e442a49SLinus Torvalds 				goto out;
1990b835996fSDipankar Sarma 			rcu_read_lock();
19911da177e4SLinus Torvalds 			for (fd = filp->f_pos-2;
19929b4f526cSAl Viro 			     fd < files_fdtable(files)->max_fds;
19931da177e4SLinus Torvalds 			     fd++, filp->f_pos++) {
199427932742SMiklos Szeredi 				char name[PROC_NUMBUF];
199527932742SMiklos Szeredi 				int len;
19961da177e4SLinus Torvalds 
19971da177e4SLinus Torvalds 				if (!fcheck_files(files, fd))
19981da177e4SLinus Torvalds 					continue;
1999b835996fSDipankar Sarma 				rcu_read_unlock();
20001da177e4SLinus Torvalds 
200127932742SMiklos Szeredi 				len = snprintf(name, sizeof(name), "%d", fd);
200227932742SMiklos Szeredi 				if (proc_fill_cache(filp, dirent, filldir,
200327932742SMiklos Szeredi 						    name, len, instantiate,
200427932742SMiklos Szeredi 						    p, &fd) < 0) {
2005b835996fSDipankar Sarma 					rcu_read_lock();
20061da177e4SLinus Torvalds 					break;
20071da177e4SLinus Torvalds 				}
2008b835996fSDipankar Sarma 				rcu_read_lock();
20091da177e4SLinus Torvalds 			}
2010b835996fSDipankar Sarma 			rcu_read_unlock();
20111da177e4SLinus Torvalds 			put_files_struct(files);
20121da177e4SLinus Torvalds 	}
20131da177e4SLinus Torvalds out:
201499f89551SEric W. Biederman 	put_task_struct(p);
201599f89551SEric W. Biederman out_no_task:
20161da177e4SLinus Torvalds 	return retval;
20171da177e4SLinus Torvalds }
20181da177e4SLinus Torvalds 
201927932742SMiklos Szeredi static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
202027932742SMiklos Szeredi 				    struct nameidata *nd)
202127932742SMiklos Szeredi {
202227932742SMiklos Szeredi 	return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
202327932742SMiklos Szeredi }
202427932742SMiklos Szeredi 
202527932742SMiklos Szeredi static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
202627932742SMiklos Szeredi {
202727932742SMiklos Szeredi 	return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
202827932742SMiklos Szeredi }
202927932742SMiklos Szeredi 
203027932742SMiklos Szeredi static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
203127932742SMiklos Szeredi 				      size_t len, loff_t *ppos)
203227932742SMiklos Szeredi {
203327932742SMiklos Szeredi 	char tmp[PROC_FDINFO_MAX];
20343dcd25f3SJan Blunck 	int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
203527932742SMiklos Szeredi 	if (!err)
203627932742SMiklos Szeredi 		err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
203727932742SMiklos Szeredi 	return err;
203827932742SMiklos Szeredi }
203927932742SMiklos Szeredi 
204027932742SMiklos Szeredi static const struct file_operations proc_fdinfo_file_operations = {
204127932742SMiklos Szeredi 	.open           = nonseekable_open,
204227932742SMiklos Szeredi 	.read		= proc_fdinfo_read,
20436038f373SArnd Bergmann 	.llseek		= no_llseek,
204427932742SMiklos Szeredi };
204527932742SMiklos Szeredi 
204600977a59SArjan van de Ven static const struct file_operations proc_fd_operations = {
20471da177e4SLinus Torvalds 	.read		= generic_read_dir,
20481da177e4SLinus Torvalds 	.readdir	= proc_readfd,
20496038f373SArnd Bergmann 	.llseek		= default_llseek,
20501da177e4SLinus Torvalds };
20511da177e4SLinus Torvalds 
2052*640708a2SPavel Emelyanov #ifdef CONFIG_CHECKPOINT_RESTORE
2053*640708a2SPavel Emelyanov 
2054*640708a2SPavel Emelyanov /*
2055*640708a2SPavel Emelyanov  * dname_to_vma_addr - maps a dentry name into two unsigned longs
2056*640708a2SPavel Emelyanov  * which represent vma start and end addresses.
2057*640708a2SPavel Emelyanov  */
2058*640708a2SPavel Emelyanov static int dname_to_vma_addr(struct dentry *dentry,
2059*640708a2SPavel Emelyanov 			     unsigned long *start, unsigned long *end)
2060*640708a2SPavel Emelyanov {
2061*640708a2SPavel Emelyanov 	if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2)
2062*640708a2SPavel Emelyanov 		return -EINVAL;
2063*640708a2SPavel Emelyanov 
2064*640708a2SPavel Emelyanov 	return 0;
2065*640708a2SPavel Emelyanov }
2066*640708a2SPavel Emelyanov 
2067*640708a2SPavel Emelyanov static int map_files_d_revalidate(struct dentry *dentry, struct nameidata *nd)
2068*640708a2SPavel Emelyanov {
2069*640708a2SPavel Emelyanov 	unsigned long vm_start, vm_end;
2070*640708a2SPavel Emelyanov 	bool exact_vma_exists = false;
2071*640708a2SPavel Emelyanov 	struct mm_struct *mm = NULL;
2072*640708a2SPavel Emelyanov 	struct task_struct *task;
2073*640708a2SPavel Emelyanov 	const struct cred *cred;
2074*640708a2SPavel Emelyanov 	struct inode *inode;
2075*640708a2SPavel Emelyanov 	int status = 0;
2076*640708a2SPavel Emelyanov 
2077*640708a2SPavel Emelyanov 	if (nd && nd->flags & LOOKUP_RCU)
2078*640708a2SPavel Emelyanov 		return -ECHILD;
2079*640708a2SPavel Emelyanov 
2080*640708a2SPavel Emelyanov 	if (!capable(CAP_SYS_ADMIN)) {
2081*640708a2SPavel Emelyanov 		status = -EACCES;
2082*640708a2SPavel Emelyanov 		goto out_notask;
2083*640708a2SPavel Emelyanov 	}
2084*640708a2SPavel Emelyanov 
2085*640708a2SPavel Emelyanov 	inode = dentry->d_inode;
2086*640708a2SPavel Emelyanov 	task = get_proc_task(inode);
2087*640708a2SPavel Emelyanov 	if (!task)
2088*640708a2SPavel Emelyanov 		goto out_notask;
2089*640708a2SPavel Emelyanov 
2090*640708a2SPavel Emelyanov 	if (!ptrace_may_access(task, PTRACE_MODE_READ))
2091*640708a2SPavel Emelyanov 		goto out;
2092*640708a2SPavel Emelyanov 
2093*640708a2SPavel Emelyanov 	mm = get_task_mm(task);
2094*640708a2SPavel Emelyanov 	if (!mm)
2095*640708a2SPavel Emelyanov 		goto out;
2096*640708a2SPavel Emelyanov 
2097*640708a2SPavel Emelyanov 	if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
2098*640708a2SPavel Emelyanov 		down_read(&mm->mmap_sem);
2099*640708a2SPavel Emelyanov 		exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
2100*640708a2SPavel Emelyanov 		up_read(&mm->mmap_sem);
2101*640708a2SPavel Emelyanov 	}
2102*640708a2SPavel Emelyanov 
2103*640708a2SPavel Emelyanov 	mmput(mm);
2104*640708a2SPavel Emelyanov 
2105*640708a2SPavel Emelyanov 	if (exact_vma_exists) {
2106*640708a2SPavel Emelyanov 		if (task_dumpable(task)) {
2107*640708a2SPavel Emelyanov 			rcu_read_lock();
2108*640708a2SPavel Emelyanov 			cred = __task_cred(task);
2109*640708a2SPavel Emelyanov 			inode->i_uid = cred->euid;
2110*640708a2SPavel Emelyanov 			inode->i_gid = cred->egid;
2111*640708a2SPavel Emelyanov 			rcu_read_unlock();
2112*640708a2SPavel Emelyanov 		} else {
2113*640708a2SPavel Emelyanov 			inode->i_uid = 0;
2114*640708a2SPavel Emelyanov 			inode->i_gid = 0;
2115*640708a2SPavel Emelyanov 		}
2116*640708a2SPavel Emelyanov 		security_task_to_inode(task, inode);
2117*640708a2SPavel Emelyanov 		status = 1;
2118*640708a2SPavel Emelyanov 	}
2119*640708a2SPavel Emelyanov 
2120*640708a2SPavel Emelyanov out:
2121*640708a2SPavel Emelyanov 	put_task_struct(task);
2122*640708a2SPavel Emelyanov 
2123*640708a2SPavel Emelyanov out_notask:
2124*640708a2SPavel Emelyanov 	if (status <= 0)
2125*640708a2SPavel Emelyanov 		d_drop(dentry);
2126*640708a2SPavel Emelyanov 
2127*640708a2SPavel Emelyanov 	return status;
2128*640708a2SPavel Emelyanov }
2129*640708a2SPavel Emelyanov 
2130*640708a2SPavel Emelyanov static const struct dentry_operations tid_map_files_dentry_operations = {
2131*640708a2SPavel Emelyanov 	.d_revalidate	= map_files_d_revalidate,
2132*640708a2SPavel Emelyanov 	.d_delete	= pid_delete_dentry,
2133*640708a2SPavel Emelyanov };
2134*640708a2SPavel Emelyanov 
2135*640708a2SPavel Emelyanov static int proc_map_files_get_link(struct dentry *dentry, struct path *path)
2136*640708a2SPavel Emelyanov {
2137*640708a2SPavel Emelyanov 	unsigned long vm_start, vm_end;
2138*640708a2SPavel Emelyanov 	struct vm_area_struct *vma;
2139*640708a2SPavel Emelyanov 	struct task_struct *task;
2140*640708a2SPavel Emelyanov 	struct mm_struct *mm;
2141*640708a2SPavel Emelyanov 	int rc;
2142*640708a2SPavel Emelyanov 
2143*640708a2SPavel Emelyanov 	rc = -ENOENT;
2144*640708a2SPavel Emelyanov 	task = get_proc_task(dentry->d_inode);
2145*640708a2SPavel Emelyanov 	if (!task)
2146*640708a2SPavel Emelyanov 		goto out;
2147*640708a2SPavel Emelyanov 
2148*640708a2SPavel Emelyanov 	mm = get_task_mm(task);
2149*640708a2SPavel Emelyanov 	put_task_struct(task);
2150*640708a2SPavel Emelyanov 	if (!mm)
2151*640708a2SPavel Emelyanov 		goto out;
2152*640708a2SPavel Emelyanov 
2153*640708a2SPavel Emelyanov 	rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
2154*640708a2SPavel Emelyanov 	if (rc)
2155*640708a2SPavel Emelyanov 		goto out_mmput;
2156*640708a2SPavel Emelyanov 
2157*640708a2SPavel Emelyanov 	down_read(&mm->mmap_sem);
2158*640708a2SPavel Emelyanov 	vma = find_exact_vma(mm, vm_start, vm_end);
2159*640708a2SPavel Emelyanov 	if (vma && vma->vm_file) {
2160*640708a2SPavel Emelyanov 		*path = vma->vm_file->f_path;
2161*640708a2SPavel Emelyanov 		path_get(path);
2162*640708a2SPavel Emelyanov 		rc = 0;
2163*640708a2SPavel Emelyanov 	}
2164*640708a2SPavel Emelyanov 	up_read(&mm->mmap_sem);
2165*640708a2SPavel Emelyanov 
2166*640708a2SPavel Emelyanov out_mmput:
2167*640708a2SPavel Emelyanov 	mmput(mm);
2168*640708a2SPavel Emelyanov out:
2169*640708a2SPavel Emelyanov 	return rc;
2170*640708a2SPavel Emelyanov }
2171*640708a2SPavel Emelyanov 
2172*640708a2SPavel Emelyanov struct map_files_info {
2173*640708a2SPavel Emelyanov 	struct file	*file;
2174*640708a2SPavel Emelyanov 	unsigned long	len;
2175*640708a2SPavel Emelyanov 	unsigned char	name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
2176*640708a2SPavel Emelyanov };
2177*640708a2SPavel Emelyanov 
2178*640708a2SPavel Emelyanov static struct dentry *
2179*640708a2SPavel Emelyanov proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
2180*640708a2SPavel Emelyanov 			   struct task_struct *task, const void *ptr)
2181*640708a2SPavel Emelyanov {
2182*640708a2SPavel Emelyanov 	const struct file *file = ptr;
2183*640708a2SPavel Emelyanov 	struct proc_inode *ei;
2184*640708a2SPavel Emelyanov 	struct inode *inode;
2185*640708a2SPavel Emelyanov 
2186*640708a2SPavel Emelyanov 	if (!file)
2187*640708a2SPavel Emelyanov 		return ERR_PTR(-ENOENT);
2188*640708a2SPavel Emelyanov 
2189*640708a2SPavel Emelyanov 	inode = proc_pid_make_inode(dir->i_sb, task);
2190*640708a2SPavel Emelyanov 	if (!inode)
2191*640708a2SPavel Emelyanov 		return ERR_PTR(-ENOENT);
2192*640708a2SPavel Emelyanov 
2193*640708a2SPavel Emelyanov 	ei = PROC_I(inode);
2194*640708a2SPavel Emelyanov 	ei->op.proc_get_link = proc_map_files_get_link;
2195*640708a2SPavel Emelyanov 
2196*640708a2SPavel Emelyanov 	inode->i_op = &proc_pid_link_inode_operations;
2197*640708a2SPavel Emelyanov 	inode->i_size = 64;
2198*640708a2SPavel Emelyanov 	inode->i_mode = S_IFLNK;
2199*640708a2SPavel Emelyanov 
2200*640708a2SPavel Emelyanov 	if (file->f_mode & FMODE_READ)
2201*640708a2SPavel Emelyanov 		inode->i_mode |= S_IRUSR;
2202*640708a2SPavel Emelyanov 	if (file->f_mode & FMODE_WRITE)
2203*640708a2SPavel Emelyanov 		inode->i_mode |= S_IWUSR;
2204*640708a2SPavel Emelyanov 
2205*640708a2SPavel Emelyanov 	d_set_d_op(dentry, &tid_map_files_dentry_operations);
2206*640708a2SPavel Emelyanov 	d_add(dentry, inode);
2207*640708a2SPavel Emelyanov 
2208*640708a2SPavel Emelyanov 	return NULL;
2209*640708a2SPavel Emelyanov }
2210*640708a2SPavel Emelyanov 
2211*640708a2SPavel Emelyanov static struct dentry *proc_map_files_lookup(struct inode *dir,
2212*640708a2SPavel Emelyanov 		struct dentry *dentry, struct nameidata *nd)
2213*640708a2SPavel Emelyanov {
2214*640708a2SPavel Emelyanov 	unsigned long vm_start, vm_end;
2215*640708a2SPavel Emelyanov 	struct vm_area_struct *vma;
2216*640708a2SPavel Emelyanov 	struct task_struct *task;
2217*640708a2SPavel Emelyanov 	struct dentry *result;
2218*640708a2SPavel Emelyanov 	struct mm_struct *mm;
2219*640708a2SPavel Emelyanov 
2220*640708a2SPavel Emelyanov 	result = ERR_PTR(-EACCES);
2221*640708a2SPavel Emelyanov 	if (!capable(CAP_SYS_ADMIN))
2222*640708a2SPavel Emelyanov 		goto out;
2223*640708a2SPavel Emelyanov 
2224*640708a2SPavel Emelyanov 	result = ERR_PTR(-ENOENT);
2225*640708a2SPavel Emelyanov 	task = get_proc_task(dir);
2226*640708a2SPavel Emelyanov 	if (!task)
2227*640708a2SPavel Emelyanov 		goto out;
2228*640708a2SPavel Emelyanov 
2229*640708a2SPavel Emelyanov 	result = ERR_PTR(-EACCES);
2230*640708a2SPavel Emelyanov 	if (lock_trace(task))
2231*640708a2SPavel Emelyanov 		goto out_put_task;
2232*640708a2SPavel Emelyanov 
2233*640708a2SPavel Emelyanov 	result = ERR_PTR(-ENOENT);
2234*640708a2SPavel Emelyanov 	if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
2235*640708a2SPavel Emelyanov 		goto out_unlock;
2236*640708a2SPavel Emelyanov 
2237*640708a2SPavel Emelyanov 	mm = get_task_mm(task);
2238*640708a2SPavel Emelyanov 	if (!mm)
2239*640708a2SPavel Emelyanov 		goto out_unlock;
2240*640708a2SPavel Emelyanov 
2241*640708a2SPavel Emelyanov 	down_read(&mm->mmap_sem);
2242*640708a2SPavel Emelyanov 	vma = find_exact_vma(mm, vm_start, vm_end);
2243*640708a2SPavel Emelyanov 	if (!vma)
2244*640708a2SPavel Emelyanov 		goto out_no_vma;
2245*640708a2SPavel Emelyanov 
2246*640708a2SPavel Emelyanov 	result = proc_map_files_instantiate(dir, dentry, task, vma->vm_file);
2247*640708a2SPavel Emelyanov 
2248*640708a2SPavel Emelyanov out_no_vma:
2249*640708a2SPavel Emelyanov 	up_read(&mm->mmap_sem);
2250*640708a2SPavel Emelyanov 	mmput(mm);
2251*640708a2SPavel Emelyanov out_unlock:
2252*640708a2SPavel Emelyanov 	unlock_trace(task);
2253*640708a2SPavel Emelyanov out_put_task:
2254*640708a2SPavel Emelyanov 	put_task_struct(task);
2255*640708a2SPavel Emelyanov out:
2256*640708a2SPavel Emelyanov 	return result;
2257*640708a2SPavel Emelyanov }
2258*640708a2SPavel Emelyanov 
2259*640708a2SPavel Emelyanov static const struct inode_operations proc_map_files_inode_operations = {
2260*640708a2SPavel Emelyanov 	.lookup		= proc_map_files_lookup,
2261*640708a2SPavel Emelyanov 	.permission	= proc_fd_permission,
2262*640708a2SPavel Emelyanov 	.setattr	= proc_setattr,
2263*640708a2SPavel Emelyanov };
2264*640708a2SPavel Emelyanov 
2265*640708a2SPavel Emelyanov static int
2266*640708a2SPavel Emelyanov proc_map_files_readdir(struct file *filp, void *dirent, filldir_t filldir)
2267*640708a2SPavel Emelyanov {
2268*640708a2SPavel Emelyanov 	struct dentry *dentry = filp->f_path.dentry;
2269*640708a2SPavel Emelyanov 	struct inode *inode = dentry->d_inode;
2270*640708a2SPavel Emelyanov 	struct vm_area_struct *vma;
2271*640708a2SPavel Emelyanov 	struct task_struct *task;
2272*640708a2SPavel Emelyanov 	struct mm_struct *mm;
2273*640708a2SPavel Emelyanov 	ino_t ino;
2274*640708a2SPavel Emelyanov 	int ret;
2275*640708a2SPavel Emelyanov 
2276*640708a2SPavel Emelyanov 	ret = -EACCES;
2277*640708a2SPavel Emelyanov 	if (!capable(CAP_SYS_ADMIN))
2278*640708a2SPavel Emelyanov 		goto out;
2279*640708a2SPavel Emelyanov 
2280*640708a2SPavel Emelyanov 	ret = -ENOENT;
2281*640708a2SPavel Emelyanov 	task = get_proc_task(inode);
2282*640708a2SPavel Emelyanov 	if (!task)
2283*640708a2SPavel Emelyanov 		goto out;
2284*640708a2SPavel Emelyanov 
2285*640708a2SPavel Emelyanov 	ret = -EACCES;
2286*640708a2SPavel Emelyanov 	if (lock_trace(task))
2287*640708a2SPavel Emelyanov 		goto out_put_task;
2288*640708a2SPavel Emelyanov 
2289*640708a2SPavel Emelyanov 	ret = 0;
2290*640708a2SPavel Emelyanov 	switch (filp->f_pos) {
2291*640708a2SPavel Emelyanov 	case 0:
2292*640708a2SPavel Emelyanov 		ino = inode->i_ino;
2293*640708a2SPavel Emelyanov 		if (filldir(dirent, ".", 1, 0, ino, DT_DIR) < 0)
2294*640708a2SPavel Emelyanov 			goto out_unlock;
2295*640708a2SPavel Emelyanov 		filp->f_pos++;
2296*640708a2SPavel Emelyanov 	case 1:
2297*640708a2SPavel Emelyanov 		ino = parent_ino(dentry);
2298*640708a2SPavel Emelyanov 		if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
2299*640708a2SPavel Emelyanov 			goto out_unlock;
2300*640708a2SPavel Emelyanov 		filp->f_pos++;
2301*640708a2SPavel Emelyanov 	default:
2302*640708a2SPavel Emelyanov 	{
2303*640708a2SPavel Emelyanov 		unsigned long nr_files, pos, i;
2304*640708a2SPavel Emelyanov 		struct flex_array *fa = NULL;
2305*640708a2SPavel Emelyanov 		struct map_files_info info;
2306*640708a2SPavel Emelyanov 		struct map_files_info *p;
2307*640708a2SPavel Emelyanov 
2308*640708a2SPavel Emelyanov 		mm = get_task_mm(task);
2309*640708a2SPavel Emelyanov 		if (!mm)
2310*640708a2SPavel Emelyanov 			goto out_unlock;
2311*640708a2SPavel Emelyanov 		down_read(&mm->mmap_sem);
2312*640708a2SPavel Emelyanov 
2313*640708a2SPavel Emelyanov 		nr_files = 0;
2314*640708a2SPavel Emelyanov 
2315*640708a2SPavel Emelyanov 		/*
2316*640708a2SPavel Emelyanov 		 * We need two passes here:
2317*640708a2SPavel Emelyanov 		 *
2318*640708a2SPavel Emelyanov 		 *  1) Collect vmas of mapped files with mmap_sem taken
2319*640708a2SPavel Emelyanov 		 *  2) Release mmap_sem and instantiate entries
2320*640708a2SPavel Emelyanov 		 *
2321*640708a2SPavel Emelyanov 		 * otherwise we get lockdep complained, since filldir()
2322*640708a2SPavel Emelyanov 		 * routine might require mmap_sem taken in might_fault().
2323*640708a2SPavel Emelyanov 		 */
2324*640708a2SPavel Emelyanov 
2325*640708a2SPavel Emelyanov 		for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
2326*640708a2SPavel Emelyanov 			if (vma->vm_file && ++pos > filp->f_pos)
2327*640708a2SPavel Emelyanov 				nr_files++;
2328*640708a2SPavel Emelyanov 		}
2329*640708a2SPavel Emelyanov 
2330*640708a2SPavel Emelyanov 		if (nr_files) {
2331*640708a2SPavel Emelyanov 			fa = flex_array_alloc(sizeof(info), nr_files,
2332*640708a2SPavel Emelyanov 						GFP_KERNEL);
2333*640708a2SPavel Emelyanov 			if (!fa || flex_array_prealloc(fa, 0, nr_files,
2334*640708a2SPavel Emelyanov 							GFP_KERNEL)) {
2335*640708a2SPavel Emelyanov 				ret = -ENOMEM;
2336*640708a2SPavel Emelyanov 				if (fa)
2337*640708a2SPavel Emelyanov 					flex_array_free(fa);
2338*640708a2SPavel Emelyanov 				up_read(&mm->mmap_sem);
2339*640708a2SPavel Emelyanov 				mmput(mm);
2340*640708a2SPavel Emelyanov 				goto out_unlock;
2341*640708a2SPavel Emelyanov 			}
2342*640708a2SPavel Emelyanov 			for (i = 0, vma = mm->mmap, pos = 2; vma;
2343*640708a2SPavel Emelyanov 					vma = vma->vm_next) {
2344*640708a2SPavel Emelyanov 				if (!vma->vm_file)
2345*640708a2SPavel Emelyanov 					continue;
2346*640708a2SPavel Emelyanov 				if (++pos <= filp->f_pos)
2347*640708a2SPavel Emelyanov 					continue;
2348*640708a2SPavel Emelyanov 
2349*640708a2SPavel Emelyanov 				get_file(vma->vm_file);
2350*640708a2SPavel Emelyanov 				info.file = vma->vm_file;
2351*640708a2SPavel Emelyanov 				info.len = snprintf(info.name,
2352*640708a2SPavel Emelyanov 						sizeof(info.name), "%lx-%lx",
2353*640708a2SPavel Emelyanov 						vma->vm_start, vma->vm_end);
2354*640708a2SPavel Emelyanov 				if (flex_array_put(fa, i++, &info, GFP_KERNEL))
2355*640708a2SPavel Emelyanov 					BUG();
2356*640708a2SPavel Emelyanov 			}
2357*640708a2SPavel Emelyanov 		}
2358*640708a2SPavel Emelyanov 		up_read(&mm->mmap_sem);
2359*640708a2SPavel Emelyanov 
2360*640708a2SPavel Emelyanov 		for (i = 0; i < nr_files; i++) {
2361*640708a2SPavel Emelyanov 			p = flex_array_get(fa, i);
2362*640708a2SPavel Emelyanov 			ret = proc_fill_cache(filp, dirent, filldir,
2363*640708a2SPavel Emelyanov 					      p->name, p->len,
2364*640708a2SPavel Emelyanov 					      proc_map_files_instantiate,
2365*640708a2SPavel Emelyanov 					      task, p->file);
2366*640708a2SPavel Emelyanov 			if (ret)
2367*640708a2SPavel Emelyanov 				break;
2368*640708a2SPavel Emelyanov 			filp->f_pos++;
2369*640708a2SPavel Emelyanov 			fput(p->file);
2370*640708a2SPavel Emelyanov 		}
2371*640708a2SPavel Emelyanov 		for (; i < nr_files; i++) {
2372*640708a2SPavel Emelyanov 			/*
2373*640708a2SPavel Emelyanov 			 * In case of error don't forget
2374*640708a2SPavel Emelyanov 			 * to put rest of file refs.
2375*640708a2SPavel Emelyanov 			 */
2376*640708a2SPavel Emelyanov 			p = flex_array_get(fa, i);
2377*640708a2SPavel Emelyanov 			fput(p->file);
2378*640708a2SPavel Emelyanov 		}
2379*640708a2SPavel Emelyanov 		if (fa)
2380*640708a2SPavel Emelyanov 			flex_array_free(fa);
2381*640708a2SPavel Emelyanov 		mmput(mm);
2382*640708a2SPavel Emelyanov 	}
2383*640708a2SPavel Emelyanov 	}
2384*640708a2SPavel Emelyanov 
2385*640708a2SPavel Emelyanov out_unlock:
2386*640708a2SPavel Emelyanov 	unlock_trace(task);
2387*640708a2SPavel Emelyanov out_put_task:
2388*640708a2SPavel Emelyanov 	put_task_struct(task);
2389*640708a2SPavel Emelyanov out:
2390*640708a2SPavel Emelyanov 	return ret;
2391*640708a2SPavel Emelyanov }
2392*640708a2SPavel Emelyanov 
2393*640708a2SPavel Emelyanov static const struct file_operations proc_map_files_operations = {
2394*640708a2SPavel Emelyanov 	.read		= generic_read_dir,
2395*640708a2SPavel Emelyanov 	.readdir	= proc_map_files_readdir,
2396*640708a2SPavel Emelyanov 	.llseek		= default_llseek,
2397*640708a2SPavel Emelyanov };
2398*640708a2SPavel Emelyanov 
2399*640708a2SPavel Emelyanov #endif /* CONFIG_CHECKPOINT_RESTORE */
2400*640708a2SPavel Emelyanov 
24011da177e4SLinus Torvalds /*
24028948e11fSAlexey Dobriyan  * /proc/pid/fd needs a special permission handler so that a process can still
24038948e11fSAlexey Dobriyan  * access /proc/self/fd after it has executed a setuid().
24048948e11fSAlexey Dobriyan  */
240510556cb2SAl Viro static int proc_fd_permission(struct inode *inode, int mask)
24068948e11fSAlexey Dobriyan {
24072830ba7fSAl Viro 	int rv = generic_permission(inode, mask);
24088948e11fSAlexey Dobriyan 	if (rv == 0)
24098948e11fSAlexey Dobriyan 		return 0;
24108948e11fSAlexey Dobriyan 	if (task_pid(current) == proc_pid(inode))
24118948e11fSAlexey Dobriyan 		rv = 0;
24128948e11fSAlexey Dobriyan 	return rv;
24138948e11fSAlexey Dobriyan }
24148948e11fSAlexey Dobriyan 
24158948e11fSAlexey Dobriyan /*
24161da177e4SLinus Torvalds  * proc directories can do almost nothing..
24171da177e4SLinus Torvalds  */
2418c5ef1c42SArjan van de Ven static const struct inode_operations proc_fd_inode_operations = {
24191da177e4SLinus Torvalds 	.lookup		= proc_lookupfd,
24208948e11fSAlexey Dobriyan 	.permission	= proc_fd_permission,
24216d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
24221da177e4SLinus Torvalds };
24231da177e4SLinus Torvalds 
242427932742SMiklos Szeredi static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
242527932742SMiklos Szeredi 	struct dentry *dentry, struct task_struct *task, const void *ptr)
242627932742SMiklos Szeredi {
242727932742SMiklos Szeredi 	unsigned fd = *(unsigned *)ptr;
242827932742SMiklos Szeredi  	struct inode *inode;
242927932742SMiklos Szeredi  	struct proc_inode *ei;
243027932742SMiklos Szeredi 	struct dentry *error = ERR_PTR(-ENOENT);
243127932742SMiklos Szeredi 
243227932742SMiklos Szeredi 	inode = proc_pid_make_inode(dir->i_sb, task);
243327932742SMiklos Szeredi 	if (!inode)
243427932742SMiklos Szeredi 		goto out;
243527932742SMiklos Szeredi 	ei = PROC_I(inode);
243627932742SMiklos Szeredi 	ei->fd = fd;
243727932742SMiklos Szeredi 	inode->i_mode = S_IFREG | S_IRUSR;
243827932742SMiklos Szeredi 	inode->i_fop = &proc_fdinfo_file_operations;
2439fb045adbSNick Piggin 	d_set_d_op(dentry, &tid_fd_dentry_operations);
244027932742SMiklos Szeredi 	d_add(dentry, inode);
244127932742SMiklos Szeredi 	/* Close the race of the process dying before we return the dentry */
244227932742SMiklos Szeredi 	if (tid_fd_revalidate(dentry, NULL))
244327932742SMiklos Szeredi 		error = NULL;
244427932742SMiklos Szeredi 
244527932742SMiklos Szeredi  out:
244627932742SMiklos Szeredi 	return error;
244727932742SMiklos Szeredi }
244827932742SMiklos Szeredi 
244927932742SMiklos Szeredi static struct dentry *proc_lookupfdinfo(struct inode *dir,
245027932742SMiklos Szeredi 					struct dentry *dentry,
245127932742SMiklos Szeredi 					struct nameidata *nd)
245227932742SMiklos Szeredi {
245327932742SMiklos Szeredi 	return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
245427932742SMiklos Szeredi }
245527932742SMiklos Szeredi 
245627932742SMiklos Szeredi static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
245727932742SMiklos Szeredi {
245827932742SMiklos Szeredi 	return proc_readfd_common(filp, dirent, filldir,
245927932742SMiklos Szeredi 				  proc_fdinfo_instantiate);
246027932742SMiklos Szeredi }
246127932742SMiklos Szeredi 
246227932742SMiklos Szeredi static const struct file_operations proc_fdinfo_operations = {
246327932742SMiklos Szeredi 	.read		= generic_read_dir,
246427932742SMiklos Szeredi 	.readdir	= proc_readfdinfo,
24656038f373SArnd Bergmann 	.llseek		= default_llseek,
246627932742SMiklos Szeredi };
246727932742SMiklos Szeredi 
246827932742SMiklos Szeredi /*
246927932742SMiklos Szeredi  * proc directories can do almost nothing..
247027932742SMiklos Szeredi  */
247127932742SMiklos Szeredi static const struct inode_operations proc_fdinfo_inode_operations = {
247227932742SMiklos Szeredi 	.lookup		= proc_lookupfdinfo,
247327932742SMiklos Szeredi 	.setattr	= proc_setattr,
247427932742SMiklos Szeredi };
247527932742SMiklos Szeredi 
247627932742SMiklos Szeredi 
2477444ceed8SEric W. Biederman static struct dentry *proc_pident_instantiate(struct inode *dir,
2478c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
2479444ceed8SEric W. Biederman {
2480c5141e6dSEric Dumazet 	const struct pid_entry *p = ptr;
2481444ceed8SEric W. Biederman 	struct inode *inode;
2482444ceed8SEric W. Biederman 	struct proc_inode *ei;
2483bd6daba9SKOSAKI Motohiro 	struct dentry *error = ERR_PTR(-ENOENT);
2484444ceed8SEric W. Biederman 
248561a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
2486444ceed8SEric W. Biederman 	if (!inode)
2487444ceed8SEric W. Biederman 		goto out;
2488444ceed8SEric W. Biederman 
2489444ceed8SEric W. Biederman 	ei = PROC_I(inode);
2490444ceed8SEric W. Biederman 	inode->i_mode = p->mode;
2491444ceed8SEric W. Biederman 	if (S_ISDIR(inode->i_mode))
2492bfe86848SMiklos Szeredi 		set_nlink(inode, 2);	/* Use getattr to fix if necessary */
2493444ceed8SEric W. Biederman 	if (p->iop)
2494444ceed8SEric W. Biederman 		inode->i_op = p->iop;
2495444ceed8SEric W. Biederman 	if (p->fop)
2496444ceed8SEric W. Biederman 		inode->i_fop = p->fop;
2497444ceed8SEric W. Biederman 	ei->op = p->op;
2498fb045adbSNick Piggin 	d_set_d_op(dentry, &pid_dentry_operations);
2499444ceed8SEric W. Biederman 	d_add(dentry, inode);
2500444ceed8SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
2501444ceed8SEric W. Biederman 	if (pid_revalidate(dentry, NULL))
2502444ceed8SEric W. Biederman 		error = NULL;
2503444ceed8SEric W. Biederman out:
2504444ceed8SEric W. Biederman 	return error;
2505444ceed8SEric W. Biederman }
2506444ceed8SEric W. Biederman 
25071da177e4SLinus Torvalds static struct dentry *proc_pident_lookup(struct inode *dir,
25081da177e4SLinus Torvalds 					 struct dentry *dentry,
2509c5141e6dSEric Dumazet 					 const struct pid_entry *ents,
25107bcd6b0eSEric W. Biederman 					 unsigned int nents)
25111da177e4SLinus Torvalds {
2512cd6a3ce9SEric W. Biederman 	struct dentry *error;
251399f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(dir);
2514c5141e6dSEric Dumazet 	const struct pid_entry *p, *last;
25151da177e4SLinus Torvalds 
2516cd6a3ce9SEric W. Biederman 	error = ERR_PTR(-ENOENT);
25171da177e4SLinus Torvalds 
251899f89551SEric W. Biederman 	if (!task)
251999f89551SEric W. Biederman 		goto out_no_task;
25201da177e4SLinus Torvalds 
252120cdc894SEric W. Biederman 	/*
252220cdc894SEric W. Biederman 	 * Yes, it does not scale. And it should not. Don't add
252320cdc894SEric W. Biederman 	 * new entries into /proc/<tgid>/ without very good reasons.
252420cdc894SEric W. Biederman 	 */
25257bcd6b0eSEric W. Biederman 	last = &ents[nents - 1];
25267bcd6b0eSEric W. Biederman 	for (p = ents; p <= last; p++) {
25271da177e4SLinus Torvalds 		if (p->len != dentry->d_name.len)
25281da177e4SLinus Torvalds 			continue;
25291da177e4SLinus Torvalds 		if (!memcmp(dentry->d_name.name, p->name, p->len))
25301da177e4SLinus Torvalds 			break;
25311da177e4SLinus Torvalds 	}
25327bcd6b0eSEric W. Biederman 	if (p > last)
25331da177e4SLinus Torvalds 		goto out;
25341da177e4SLinus Torvalds 
2535444ceed8SEric W. Biederman 	error = proc_pident_instantiate(dir, dentry, task, p);
25361da177e4SLinus Torvalds out:
253799f89551SEric W. Biederman 	put_task_struct(task);
253899f89551SEric W. Biederman out_no_task:
2539cd6a3ce9SEric W. Biederman 	return error;
25401da177e4SLinus Torvalds }
25411da177e4SLinus Torvalds 
2542c5141e6dSEric Dumazet static int proc_pident_fill_cache(struct file *filp, void *dirent,
2543c5141e6dSEric Dumazet 	filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
254461a28784SEric W. Biederman {
254561a28784SEric W. Biederman 	return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
254661a28784SEric W. Biederman 				proc_pident_instantiate, task, p);
254761a28784SEric W. Biederman }
254861a28784SEric W. Biederman 
254928a6d671SEric W. Biederman static int proc_pident_readdir(struct file *filp,
255028a6d671SEric W. Biederman 		void *dirent, filldir_t filldir,
2551c5141e6dSEric Dumazet 		const struct pid_entry *ents, unsigned int nents)
255228a6d671SEric W. Biederman {
255328a6d671SEric W. Biederman 	int i;
25542fddfeefSJosef "Jeff" Sipek 	struct dentry *dentry = filp->f_path.dentry;
255528a6d671SEric W. Biederman 	struct inode *inode = dentry->d_inode;
255628a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
2557c5141e6dSEric Dumazet 	const struct pid_entry *p, *last;
255828a6d671SEric W. Biederman 	ino_t ino;
255928a6d671SEric W. Biederman 	int ret;
256028a6d671SEric W. Biederman 
256128a6d671SEric W. Biederman 	ret = -ENOENT;
256228a6d671SEric W. Biederman 	if (!task)
256361a28784SEric W. Biederman 		goto out_no_task;
256428a6d671SEric W. Biederman 
256528a6d671SEric W. Biederman 	ret = 0;
256628a6d671SEric W. Biederman 	i = filp->f_pos;
256728a6d671SEric W. Biederman 	switch (i) {
256828a6d671SEric W. Biederman 	case 0:
256928a6d671SEric W. Biederman 		ino = inode->i_ino;
257028a6d671SEric W. Biederman 		if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
257128a6d671SEric W. Biederman 			goto out;
257228a6d671SEric W. Biederman 		i++;
257328a6d671SEric W. Biederman 		filp->f_pos++;
257428a6d671SEric W. Biederman 		/* fall through */
257528a6d671SEric W. Biederman 	case 1:
257628a6d671SEric W. Biederman 		ino = parent_ino(dentry);
257728a6d671SEric W. Biederman 		if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
257828a6d671SEric W. Biederman 			goto out;
257928a6d671SEric W. Biederman 		i++;
258028a6d671SEric W. Biederman 		filp->f_pos++;
258128a6d671SEric W. Biederman 		/* fall through */
258228a6d671SEric W. Biederman 	default:
258328a6d671SEric W. Biederman 		i -= 2;
258428a6d671SEric W. Biederman 		if (i >= nents) {
258528a6d671SEric W. Biederman 			ret = 1;
258628a6d671SEric W. Biederman 			goto out;
258728a6d671SEric W. Biederman 		}
258828a6d671SEric W. Biederman 		p = ents + i;
25897bcd6b0eSEric W. Biederman 		last = &ents[nents - 1];
25907bcd6b0eSEric W. Biederman 		while (p <= last) {
259161a28784SEric W. Biederman 			if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
259228a6d671SEric W. Biederman 				goto out;
259328a6d671SEric W. Biederman 			filp->f_pos++;
259428a6d671SEric W. Biederman 			p++;
259528a6d671SEric W. Biederman 		}
25961da177e4SLinus Torvalds 	}
25971da177e4SLinus Torvalds 
259828a6d671SEric W. Biederman 	ret = 1;
259928a6d671SEric W. Biederman out:
260061a28784SEric W. Biederman 	put_task_struct(task);
260161a28784SEric W. Biederman out_no_task:
260228a6d671SEric W. Biederman 	return ret;
26031da177e4SLinus Torvalds }
26041da177e4SLinus Torvalds 
26051da177e4SLinus Torvalds #ifdef CONFIG_SECURITY
260628a6d671SEric W. Biederman static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
260728a6d671SEric W. Biederman 				  size_t count, loff_t *ppos)
260828a6d671SEric W. Biederman {
26092fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
261004ff9708SAl Viro 	char *p = NULL;
261128a6d671SEric W. Biederman 	ssize_t length;
261228a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
261328a6d671SEric W. Biederman 
261428a6d671SEric W. Biederman 	if (!task)
261504ff9708SAl Viro 		return -ESRCH;
261628a6d671SEric W. Biederman 
261728a6d671SEric W. Biederman 	length = security_getprocattr(task,
26182fddfeefSJosef "Jeff" Sipek 				      (char*)file->f_path.dentry->d_name.name,
261904ff9708SAl Viro 				      &p);
262028a6d671SEric W. Biederman 	put_task_struct(task);
262104ff9708SAl Viro 	if (length > 0)
262204ff9708SAl Viro 		length = simple_read_from_buffer(buf, count, ppos, p, length);
262304ff9708SAl Viro 	kfree(p);
262428a6d671SEric W. Biederman 	return length;
262528a6d671SEric W. Biederman }
262628a6d671SEric W. Biederman 
262728a6d671SEric W. Biederman static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
262828a6d671SEric W. Biederman 				   size_t count, loff_t *ppos)
262928a6d671SEric W. Biederman {
26302fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
263128a6d671SEric W. Biederman 	char *page;
263228a6d671SEric W. Biederman 	ssize_t length;
263328a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
263428a6d671SEric W. Biederman 
263528a6d671SEric W. Biederman 	length = -ESRCH;
263628a6d671SEric W. Biederman 	if (!task)
263728a6d671SEric W. Biederman 		goto out_no_task;
263828a6d671SEric W. Biederman 	if (count > PAGE_SIZE)
263928a6d671SEric W. Biederman 		count = PAGE_SIZE;
264028a6d671SEric W. Biederman 
264128a6d671SEric W. Biederman 	/* No partial writes. */
264228a6d671SEric W. Biederman 	length = -EINVAL;
264328a6d671SEric W. Biederman 	if (*ppos != 0)
264428a6d671SEric W. Biederman 		goto out;
264528a6d671SEric W. Biederman 
264628a6d671SEric W. Biederman 	length = -ENOMEM;
2647e12ba74dSMel Gorman 	page = (char*)__get_free_page(GFP_TEMPORARY);
264828a6d671SEric W. Biederman 	if (!page)
264928a6d671SEric W. Biederman 		goto out;
265028a6d671SEric W. Biederman 
265128a6d671SEric W. Biederman 	length = -EFAULT;
265228a6d671SEric W. Biederman 	if (copy_from_user(page, buf, count))
265328a6d671SEric W. Biederman 		goto out_free;
265428a6d671SEric W. Biederman 
2655107db7c7SDavid Howells 	/* Guard against adverse ptrace interaction */
26569b1bf12dSKOSAKI Motohiro 	length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
2657107db7c7SDavid Howells 	if (length < 0)
2658107db7c7SDavid Howells 		goto out_free;
2659107db7c7SDavid Howells 
266028a6d671SEric W. Biederman 	length = security_setprocattr(task,
26612fddfeefSJosef "Jeff" Sipek 				      (char*)file->f_path.dentry->d_name.name,
266228a6d671SEric W. Biederman 				      (void*)page, count);
26639b1bf12dSKOSAKI Motohiro 	mutex_unlock(&task->signal->cred_guard_mutex);
266428a6d671SEric W. Biederman out_free:
266528a6d671SEric W. Biederman 	free_page((unsigned long) page);
266628a6d671SEric W. Biederman out:
266728a6d671SEric W. Biederman 	put_task_struct(task);
266828a6d671SEric W. Biederman out_no_task:
266928a6d671SEric W. Biederman 	return length;
267028a6d671SEric W. Biederman }
267128a6d671SEric W. Biederman 
267200977a59SArjan van de Ven static const struct file_operations proc_pid_attr_operations = {
267328a6d671SEric W. Biederman 	.read		= proc_pid_attr_read,
267428a6d671SEric W. Biederman 	.write		= proc_pid_attr_write,
267587df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
267628a6d671SEric W. Biederman };
267728a6d671SEric W. Biederman 
2678c5141e6dSEric Dumazet static const struct pid_entry attr_dir_stuff[] = {
2679631f9c18SAlexey Dobriyan 	REG("current",    S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2680631f9c18SAlexey Dobriyan 	REG("prev",       S_IRUGO,	   proc_pid_attr_operations),
2681631f9c18SAlexey Dobriyan 	REG("exec",       S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2682631f9c18SAlexey Dobriyan 	REG("fscreate",   S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2683631f9c18SAlexey Dobriyan 	REG("keycreate",  S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2684631f9c18SAlexey Dobriyan 	REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
268528a6d671SEric W. Biederman };
268628a6d671SEric W. Biederman 
268772d9dcfcSEric W. Biederman static int proc_attr_dir_readdir(struct file * filp,
26881da177e4SLinus Torvalds 			     void * dirent, filldir_t filldir)
26891da177e4SLinus Torvalds {
26901da177e4SLinus Torvalds 	return proc_pident_readdir(filp,dirent,filldir,
269172d9dcfcSEric W. Biederman 				   attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
26921da177e4SLinus Torvalds }
26931da177e4SLinus Torvalds 
269400977a59SArjan van de Ven static const struct file_operations proc_attr_dir_operations = {
26951da177e4SLinus Torvalds 	.read		= generic_read_dir,
269672d9dcfcSEric W. Biederman 	.readdir	= proc_attr_dir_readdir,
26976038f373SArnd Bergmann 	.llseek		= default_llseek,
26981da177e4SLinus Torvalds };
26991da177e4SLinus Torvalds 
270072d9dcfcSEric W. Biederman static struct dentry *proc_attr_dir_lookup(struct inode *dir,
27011da177e4SLinus Torvalds 				struct dentry *dentry, struct nameidata *nd)
27021da177e4SLinus Torvalds {
27037bcd6b0eSEric W. Biederman 	return proc_pident_lookup(dir, dentry,
27047bcd6b0eSEric W. Biederman 				  attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
27051da177e4SLinus Torvalds }
27061da177e4SLinus Torvalds 
2707c5ef1c42SArjan van de Ven static const struct inode_operations proc_attr_dir_inode_operations = {
270872d9dcfcSEric W. Biederman 	.lookup		= proc_attr_dir_lookup,
270999f89551SEric W. Biederman 	.getattr	= pid_getattr,
27106d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
27111da177e4SLinus Torvalds };
27121da177e4SLinus Torvalds 
27131da177e4SLinus Torvalds #endif
27141da177e4SLinus Torvalds 
2715698ba7b5SChristoph Hellwig #ifdef CONFIG_ELF_CORE
27163cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
27173cb4a0bbSKawai, Hidehiro 					 size_t count, loff_t *ppos)
27183cb4a0bbSKawai, Hidehiro {
27193cb4a0bbSKawai, Hidehiro 	struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
27203cb4a0bbSKawai, Hidehiro 	struct mm_struct *mm;
27213cb4a0bbSKawai, Hidehiro 	char buffer[PROC_NUMBUF];
27223cb4a0bbSKawai, Hidehiro 	size_t len;
27233cb4a0bbSKawai, Hidehiro 	int ret;
27243cb4a0bbSKawai, Hidehiro 
27253cb4a0bbSKawai, Hidehiro 	if (!task)
27263cb4a0bbSKawai, Hidehiro 		return -ESRCH;
27273cb4a0bbSKawai, Hidehiro 
27283cb4a0bbSKawai, Hidehiro 	ret = 0;
27293cb4a0bbSKawai, Hidehiro 	mm = get_task_mm(task);
27303cb4a0bbSKawai, Hidehiro 	if (mm) {
27313cb4a0bbSKawai, Hidehiro 		len = snprintf(buffer, sizeof(buffer), "%08lx\n",
27323cb4a0bbSKawai, Hidehiro 			       ((mm->flags & MMF_DUMP_FILTER_MASK) >>
27333cb4a0bbSKawai, Hidehiro 				MMF_DUMP_FILTER_SHIFT));
27343cb4a0bbSKawai, Hidehiro 		mmput(mm);
27353cb4a0bbSKawai, Hidehiro 		ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
27363cb4a0bbSKawai, Hidehiro 	}
27373cb4a0bbSKawai, Hidehiro 
27383cb4a0bbSKawai, Hidehiro 	put_task_struct(task);
27393cb4a0bbSKawai, Hidehiro 
27403cb4a0bbSKawai, Hidehiro 	return ret;
27413cb4a0bbSKawai, Hidehiro }
27423cb4a0bbSKawai, Hidehiro 
27433cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_write(struct file *file,
27443cb4a0bbSKawai, Hidehiro 					  const char __user *buf,
27453cb4a0bbSKawai, Hidehiro 					  size_t count,
27463cb4a0bbSKawai, Hidehiro 					  loff_t *ppos)
27473cb4a0bbSKawai, Hidehiro {
27483cb4a0bbSKawai, Hidehiro 	struct task_struct *task;
27493cb4a0bbSKawai, Hidehiro 	struct mm_struct *mm;
27503cb4a0bbSKawai, Hidehiro 	char buffer[PROC_NUMBUF], *end;
27513cb4a0bbSKawai, Hidehiro 	unsigned int val;
27523cb4a0bbSKawai, Hidehiro 	int ret;
27533cb4a0bbSKawai, Hidehiro 	int i;
27543cb4a0bbSKawai, Hidehiro 	unsigned long mask;
27553cb4a0bbSKawai, Hidehiro 
27563cb4a0bbSKawai, Hidehiro 	ret = -EFAULT;
27573cb4a0bbSKawai, Hidehiro 	memset(buffer, 0, sizeof(buffer));
27583cb4a0bbSKawai, Hidehiro 	if (count > sizeof(buffer) - 1)
27593cb4a0bbSKawai, Hidehiro 		count = sizeof(buffer) - 1;
27603cb4a0bbSKawai, Hidehiro 	if (copy_from_user(buffer, buf, count))
27613cb4a0bbSKawai, Hidehiro 		goto out_no_task;
27623cb4a0bbSKawai, Hidehiro 
27633cb4a0bbSKawai, Hidehiro 	ret = -EINVAL;
27643cb4a0bbSKawai, Hidehiro 	val = (unsigned int)simple_strtoul(buffer, &end, 0);
27653cb4a0bbSKawai, Hidehiro 	if (*end == '\n')
27663cb4a0bbSKawai, Hidehiro 		end++;
27673cb4a0bbSKawai, Hidehiro 	if (end - buffer == 0)
27683cb4a0bbSKawai, Hidehiro 		goto out_no_task;
27693cb4a0bbSKawai, Hidehiro 
27703cb4a0bbSKawai, Hidehiro 	ret = -ESRCH;
27713cb4a0bbSKawai, Hidehiro 	task = get_proc_task(file->f_dentry->d_inode);
27723cb4a0bbSKawai, Hidehiro 	if (!task)
27733cb4a0bbSKawai, Hidehiro 		goto out_no_task;
27743cb4a0bbSKawai, Hidehiro 
27753cb4a0bbSKawai, Hidehiro 	ret = end - buffer;
27763cb4a0bbSKawai, Hidehiro 	mm = get_task_mm(task);
27773cb4a0bbSKawai, Hidehiro 	if (!mm)
27783cb4a0bbSKawai, Hidehiro 		goto out_no_mm;
27793cb4a0bbSKawai, Hidehiro 
27803cb4a0bbSKawai, Hidehiro 	for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
27813cb4a0bbSKawai, Hidehiro 		if (val & mask)
27823cb4a0bbSKawai, Hidehiro 			set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
27833cb4a0bbSKawai, Hidehiro 		else
27843cb4a0bbSKawai, Hidehiro 			clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
27853cb4a0bbSKawai, Hidehiro 	}
27863cb4a0bbSKawai, Hidehiro 
27873cb4a0bbSKawai, Hidehiro 	mmput(mm);
27883cb4a0bbSKawai, Hidehiro  out_no_mm:
27893cb4a0bbSKawai, Hidehiro 	put_task_struct(task);
27903cb4a0bbSKawai, Hidehiro  out_no_task:
27913cb4a0bbSKawai, Hidehiro 	return ret;
27923cb4a0bbSKawai, Hidehiro }
27933cb4a0bbSKawai, Hidehiro 
27943cb4a0bbSKawai, Hidehiro static const struct file_operations proc_coredump_filter_operations = {
27953cb4a0bbSKawai, Hidehiro 	.read		= proc_coredump_filter_read,
27963cb4a0bbSKawai, Hidehiro 	.write		= proc_coredump_filter_write,
279787df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
27983cb4a0bbSKawai, Hidehiro };
27993cb4a0bbSKawai, Hidehiro #endif
28003cb4a0bbSKawai, Hidehiro 
28011da177e4SLinus Torvalds /*
28021da177e4SLinus Torvalds  * /proc/self:
28031da177e4SLinus Torvalds  */
28041da177e4SLinus Torvalds static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
28051da177e4SLinus Torvalds 			      int buflen)
28061da177e4SLinus Torvalds {
2807488e5bc4SEric W. Biederman 	struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2808b55fcb22SAndrew Morton 	pid_t tgid = task_tgid_nr_ns(current, ns);
28098578cea7SEric W. Biederman 	char tmp[PROC_NUMBUF];
2810b55fcb22SAndrew Morton 	if (!tgid)
2811488e5bc4SEric W. Biederman 		return -ENOENT;
2812b55fcb22SAndrew Morton 	sprintf(tmp, "%d", tgid);
28131da177e4SLinus Torvalds 	return vfs_readlink(dentry,buffer,buflen,tmp);
28141da177e4SLinus Torvalds }
28151da177e4SLinus Torvalds 
2816008b150aSAl Viro static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
28171da177e4SLinus Torvalds {
2818488e5bc4SEric W. Biederman 	struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2819b55fcb22SAndrew Morton 	pid_t tgid = task_tgid_nr_ns(current, ns);
28207fee4868SAl Viro 	char *name = ERR_PTR(-ENOENT);
28217fee4868SAl Viro 	if (tgid) {
28227fee4868SAl Viro 		name = __getname();
28237fee4868SAl Viro 		if (!name)
28247fee4868SAl Viro 			name = ERR_PTR(-ENOMEM);
28257fee4868SAl Viro 		else
28267fee4868SAl Viro 			sprintf(name, "%d", tgid);
28277fee4868SAl Viro 	}
28287fee4868SAl Viro 	nd_set_link(nd, name);
28297fee4868SAl Viro 	return NULL;
28307fee4868SAl Viro }
28317fee4868SAl Viro 
28327fee4868SAl Viro static void proc_self_put_link(struct dentry *dentry, struct nameidata *nd,
28337fee4868SAl Viro 				void *cookie)
28347fee4868SAl Viro {
28357fee4868SAl Viro 	char *s = nd_get_link(nd);
28367fee4868SAl Viro 	if (!IS_ERR(s))
28377fee4868SAl Viro 		__putname(s);
28381da177e4SLinus Torvalds }
28391da177e4SLinus Torvalds 
2840c5ef1c42SArjan van de Ven static const struct inode_operations proc_self_inode_operations = {
28411da177e4SLinus Torvalds 	.readlink	= proc_self_readlink,
28421da177e4SLinus Torvalds 	.follow_link	= proc_self_follow_link,
28437fee4868SAl Viro 	.put_link	= proc_self_put_link,
28441da177e4SLinus Torvalds };
28451da177e4SLinus Torvalds 
284628a6d671SEric W. Biederman /*
2847801199ceSEric W. Biederman  * proc base
2848801199ceSEric W. Biederman  *
2849801199ceSEric W. Biederman  * These are the directory entries in the root directory of /proc
2850801199ceSEric W. Biederman  * that properly belong to the /proc filesystem, as they describe
2851801199ceSEric W. Biederman  * describe something that is process related.
2852801199ceSEric W. Biederman  */
2853c5141e6dSEric Dumazet static const struct pid_entry proc_base_stuff[] = {
285461a28784SEric W. Biederman 	NOD("self", S_IFLNK|S_IRWXUGO,
2855801199ceSEric W. Biederman 		&proc_self_inode_operations, NULL, {}),
2856801199ceSEric W. Biederman };
2857801199ceSEric W. Biederman 
2858444ceed8SEric W. Biederman static struct dentry *proc_base_instantiate(struct inode *dir,
2859c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
2860801199ceSEric W. Biederman {
2861c5141e6dSEric Dumazet 	const struct pid_entry *p = ptr;
2862801199ceSEric W. Biederman 	struct inode *inode;
2863801199ceSEric W. Biederman 	struct proc_inode *ei;
286473d36460SDan Carpenter 	struct dentry *error;
2865801199ceSEric W. Biederman 
2866801199ceSEric W. Biederman 	/* Allocate the inode */
2867801199ceSEric W. Biederman 	error = ERR_PTR(-ENOMEM);
2868801199ceSEric W. Biederman 	inode = new_inode(dir->i_sb);
2869801199ceSEric W. Biederman 	if (!inode)
2870801199ceSEric W. Biederman 		goto out;
2871801199ceSEric W. Biederman 
2872801199ceSEric W. Biederman 	/* Initialize the inode */
2873801199ceSEric W. Biederman 	ei = PROC_I(inode);
287485fe4025SChristoph Hellwig 	inode->i_ino = get_next_ino();
2875801199ceSEric W. Biederman 	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2876801199ceSEric W. Biederman 
2877801199ceSEric W. Biederman 	/*
2878801199ceSEric W. Biederman 	 * grab the reference to the task.
2879801199ceSEric W. Biederman 	 */
28801a657f78SOleg Nesterov 	ei->pid = get_task_pid(task, PIDTYPE_PID);
2881801199ceSEric W. Biederman 	if (!ei->pid)
2882801199ceSEric W. Biederman 		goto out_iput;
2883801199ceSEric W. Biederman 
2884801199ceSEric W. Biederman 	inode->i_mode = p->mode;
2885801199ceSEric W. Biederman 	if (S_ISDIR(inode->i_mode))
2886bfe86848SMiklos Szeredi 		set_nlink(inode, 2);
2887801199ceSEric W. Biederman 	if (S_ISLNK(inode->i_mode))
2888801199ceSEric W. Biederman 		inode->i_size = 64;
2889801199ceSEric W. Biederman 	if (p->iop)
2890801199ceSEric W. Biederman 		inode->i_op = p->iop;
2891801199ceSEric W. Biederman 	if (p->fop)
2892801199ceSEric W. Biederman 		inode->i_fop = p->fop;
2893801199ceSEric W. Biederman 	ei->op = p->op;
2894801199ceSEric W. Biederman 	d_add(dentry, inode);
2895801199ceSEric W. Biederman 	error = NULL;
2896801199ceSEric W. Biederman out:
2897801199ceSEric W. Biederman 	return error;
2898801199ceSEric W. Biederman out_iput:
2899801199ceSEric W. Biederman 	iput(inode);
2900801199ceSEric W. Biederman 	goto out;
2901801199ceSEric W. Biederman }
2902801199ceSEric W. Biederman 
2903444ceed8SEric W. Biederman static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2904444ceed8SEric W. Biederman {
2905444ceed8SEric W. Biederman 	struct dentry *error;
2906444ceed8SEric W. Biederman 	struct task_struct *task = get_proc_task(dir);
2907c5141e6dSEric Dumazet 	const struct pid_entry *p, *last;
2908444ceed8SEric W. Biederman 
2909444ceed8SEric W. Biederman 	error = ERR_PTR(-ENOENT);
2910444ceed8SEric W. Biederman 
2911444ceed8SEric W. Biederman 	if (!task)
2912444ceed8SEric W. Biederman 		goto out_no_task;
2913444ceed8SEric W. Biederman 
2914444ceed8SEric W. Biederman 	/* Lookup the directory entry */
29157bcd6b0eSEric W. Biederman 	last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
29167bcd6b0eSEric W. Biederman 	for (p = proc_base_stuff; p <= last; p++) {
2917444ceed8SEric W. Biederman 		if (p->len != dentry->d_name.len)
2918444ceed8SEric W. Biederman 			continue;
2919444ceed8SEric W. Biederman 		if (!memcmp(dentry->d_name.name, p->name, p->len))
2920444ceed8SEric W. Biederman 			break;
2921444ceed8SEric W. Biederman 	}
29227bcd6b0eSEric W. Biederman 	if (p > last)
2923444ceed8SEric W. Biederman 		goto out;
2924444ceed8SEric W. Biederman 
2925444ceed8SEric W. Biederman 	error = proc_base_instantiate(dir, dentry, task, p);
2926444ceed8SEric W. Biederman 
2927444ceed8SEric W. Biederman out:
2928444ceed8SEric W. Biederman 	put_task_struct(task);
2929444ceed8SEric W. Biederman out_no_task:
2930444ceed8SEric W. Biederman 	return error;
2931444ceed8SEric W. Biederman }
2932444ceed8SEric W. Biederman 
2933c5141e6dSEric Dumazet static int proc_base_fill_cache(struct file *filp, void *dirent,
2934c5141e6dSEric Dumazet 	filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
293561a28784SEric W. Biederman {
293661a28784SEric W. Biederman 	return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
293761a28784SEric W. Biederman 				proc_base_instantiate, task, p);
293861a28784SEric W. Biederman }
293961a28784SEric W. Biederman 
2940aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING
2941297c5d92SAndrea Righi static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
2942aba76fdbSAndrew Morton {
2943940389b8SAndrea Righi 	struct task_io_accounting acct = task->ioac;
2944297c5d92SAndrea Righi 	unsigned long flags;
2945293eb1e7SVasiliy Kulikov 	int result;
2946297c5d92SAndrea Righi 
2947293eb1e7SVasiliy Kulikov 	result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2948293eb1e7SVasiliy Kulikov 	if (result)
2949293eb1e7SVasiliy Kulikov 		return result;
2950293eb1e7SVasiliy Kulikov 
2951293eb1e7SVasiliy Kulikov 	if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
2952293eb1e7SVasiliy Kulikov 		result = -EACCES;
2953293eb1e7SVasiliy Kulikov 		goto out_unlock;
2954293eb1e7SVasiliy Kulikov 	}
29551d1221f3SVasiliy Kulikov 
29565995477aSAndrea Righi 	if (whole && lock_task_sighand(task, &flags)) {
2957b2d002dbSAndrea Righi 		struct task_struct *t = task;
2958297c5d92SAndrea Righi 
29595995477aSAndrea Righi 		task_io_accounting_add(&acct, &task->signal->ioac);
29605995477aSAndrea Righi 		while_each_thread(task, t)
29615995477aSAndrea Righi 			task_io_accounting_add(&acct, &t->ioac);
2962297c5d92SAndrea Righi 
2963297c5d92SAndrea Righi 		unlock_task_sighand(task, &flags);
2964297c5d92SAndrea Righi 	}
2965293eb1e7SVasiliy Kulikov 	result = sprintf(buffer,
2966aba76fdbSAndrew Morton 			"rchar: %llu\n"
2967aba76fdbSAndrew Morton 			"wchar: %llu\n"
2968aba76fdbSAndrew Morton 			"syscr: %llu\n"
2969aba76fdbSAndrew Morton 			"syscw: %llu\n"
2970aba76fdbSAndrew Morton 			"read_bytes: %llu\n"
2971aba76fdbSAndrew Morton 			"write_bytes: %llu\n"
2972aba76fdbSAndrew Morton 			"cancelled_write_bytes: %llu\n",
29737c44319dSAlexander Beregalov 			(unsigned long long)acct.rchar,
29747c44319dSAlexander Beregalov 			(unsigned long long)acct.wchar,
29757c44319dSAlexander Beregalov 			(unsigned long long)acct.syscr,
29767c44319dSAlexander Beregalov 			(unsigned long long)acct.syscw,
29777c44319dSAlexander Beregalov 			(unsigned long long)acct.read_bytes,
29787c44319dSAlexander Beregalov 			(unsigned long long)acct.write_bytes,
29797c44319dSAlexander Beregalov 			(unsigned long long)acct.cancelled_write_bytes);
2980293eb1e7SVasiliy Kulikov out_unlock:
2981293eb1e7SVasiliy Kulikov 	mutex_unlock(&task->signal->cred_guard_mutex);
2982293eb1e7SVasiliy Kulikov 	return result;
2983aba76fdbSAndrew Morton }
2984297c5d92SAndrea Righi 
2985297c5d92SAndrea Righi static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2986297c5d92SAndrea Righi {
2987297c5d92SAndrea Righi 	return do_io_accounting(task, buffer, 0);
2988297c5d92SAndrea Righi }
2989297c5d92SAndrea Righi 
2990297c5d92SAndrea Righi static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2991297c5d92SAndrea Righi {
2992297c5d92SAndrea Righi 	return do_io_accounting(task, buffer, 1);
2993297c5d92SAndrea Righi }
2994297c5d92SAndrea Righi #endif /* CONFIG_TASK_IO_ACCOUNTING */
2995aba76fdbSAndrew Morton 
299647830723SKees Cook static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
299747830723SKees Cook 				struct pid *pid, struct task_struct *task)
299847830723SKees Cook {
2999a9712bc1SAl Viro 	int err = lock_trace(task);
3000a9712bc1SAl Viro 	if (!err) {
300147830723SKees Cook 		seq_printf(m, "%08x\n", task->personality);
3002a9712bc1SAl Viro 		unlock_trace(task);
3003a9712bc1SAl Viro 	}
3004a9712bc1SAl Viro 	return err;
300547830723SKees Cook }
300647830723SKees Cook 
3007801199ceSEric W. Biederman /*
300828a6d671SEric W. Biederman  * Thread groups
300928a6d671SEric W. Biederman  */
301000977a59SArjan van de Ven static const struct file_operations proc_task_operations;
3011c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations;
301220cdc894SEric W. Biederman 
3013c5141e6dSEric Dumazet static const struct pid_entry tgid_base_stuff[] = {
3014631f9c18SAlexey Dobriyan 	DIR("task",       S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
3015631f9c18SAlexey Dobriyan 	DIR("fd",         S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
3016*640708a2SPavel Emelyanov #ifdef CONFIG_CHECKPOINT_RESTORE
3017*640708a2SPavel Emelyanov 	DIR("map_files",  S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
3018*640708a2SPavel Emelyanov #endif
3019631f9c18SAlexey Dobriyan 	DIR("fdinfo",     S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
30206b4e306aSEric W. Biederman 	DIR("ns",	  S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
3021b2211a36SAndrew Morton #ifdef CONFIG_NET
3022631f9c18SAlexey Dobriyan 	DIR("net",        S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
3023b2211a36SAndrew Morton #endif
3024631f9c18SAlexey Dobriyan 	REG("environ",    S_IRUSR, proc_environ_operations),
3025631f9c18SAlexey Dobriyan 	INF("auxv",       S_IRUSR, proc_pid_auxv),
3026631f9c18SAlexey Dobriyan 	ONE("status",     S_IRUGO, proc_pid_status),
3027a9712bc1SAl Viro 	ONE("personality", S_IRUGO, proc_pid_personality),
30283036e7b4SJiri Olsa 	INF("limits",	  S_IRUGO, proc_pid_limits),
302943ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
3030631f9c18SAlexey Dobriyan 	REG("sched",      S_IRUGO|S_IWUSR, proc_pid_sched_operations),
303143ae34cbSIngo Molnar #endif
30325091faa4SMike Galbraith #ifdef CONFIG_SCHED_AUTOGROUP
30335091faa4SMike Galbraith 	REG("autogroup",  S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
30345091faa4SMike Galbraith #endif
30354614a696Sjohn stultz 	REG("comm",      S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
3036ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3037a9712bc1SAl Viro 	INF("syscall",    S_IRUGO, proc_pid_syscall),
3038ebcb6734SRoland McGrath #endif
3039631f9c18SAlexey Dobriyan 	INF("cmdline",    S_IRUGO, proc_pid_cmdline),
3040631f9c18SAlexey Dobriyan 	ONE("stat",       S_IRUGO, proc_tgid_stat),
3041631f9c18SAlexey Dobriyan 	ONE("statm",      S_IRUGO, proc_pid_statm),
3042631f9c18SAlexey Dobriyan 	REG("maps",       S_IRUGO, proc_maps_operations),
304328a6d671SEric W. Biederman #ifdef CONFIG_NUMA
3044631f9c18SAlexey Dobriyan 	REG("numa_maps",  S_IRUGO, proc_numa_maps_operations),
304528a6d671SEric W. Biederman #endif
3046631f9c18SAlexey Dobriyan 	REG("mem",        S_IRUSR|S_IWUSR, proc_mem_operations),
3047631f9c18SAlexey Dobriyan 	LNK("cwd",        proc_cwd_link),
3048631f9c18SAlexey Dobriyan 	LNK("root",       proc_root_link),
3049631f9c18SAlexey Dobriyan 	LNK("exe",        proc_exe_link),
3050631f9c18SAlexey Dobriyan 	REG("mounts",     S_IRUGO, proc_mounts_operations),
3051631f9c18SAlexey Dobriyan 	REG("mountinfo",  S_IRUGO, proc_mountinfo_operations),
3052631f9c18SAlexey Dobriyan 	REG("mountstats", S_IRUSR, proc_mountstats_operations),
30531e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR
3054631f9c18SAlexey Dobriyan 	REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3055631f9c18SAlexey Dobriyan 	REG("smaps",      S_IRUGO, proc_smaps_operations),
3056ca6b0bf0SAl Viro 	REG("pagemap",    S_IRUGO, proc_pagemap_operations),
305728a6d671SEric W. Biederman #endif
305828a6d671SEric W. Biederman #ifdef CONFIG_SECURITY
3059631f9c18SAlexey Dobriyan 	DIR("attr",       S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
306028a6d671SEric W. Biederman #endif
306128a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS
3062631f9c18SAlexey Dobriyan 	INF("wchan",      S_IRUGO, proc_pid_wchan),
306328a6d671SEric W. Biederman #endif
30642ec220e2SKen Chen #ifdef CONFIG_STACKTRACE
3065a9712bc1SAl Viro 	ONE("stack",      S_IRUGO, proc_pid_stack),
306628a6d671SEric W. Biederman #endif
306728a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS
3068631f9c18SAlexey Dobriyan 	INF("schedstat",  S_IRUGO, proc_pid_schedstat),
306928a6d671SEric W. Biederman #endif
30709745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP
3071631f9c18SAlexey Dobriyan 	REG("latency",  S_IRUGO, proc_lstats_operations),
30729745512cSArjan van de Ven #endif
30738793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET
3074631f9c18SAlexey Dobriyan 	REG("cpuset",     S_IRUGO, proc_cpuset_operations),
307528a6d671SEric W. Biederman #endif
3076a424316cSPaul Menage #ifdef CONFIG_CGROUPS
3077631f9c18SAlexey Dobriyan 	REG("cgroup",  S_IRUGO, proc_cgroup_operations),
3078a424316cSPaul Menage #endif
3079631f9c18SAlexey Dobriyan 	INF("oom_score",  S_IRUGO, proc_oom_score),
3080631f9c18SAlexey Dobriyan 	REG("oom_adj",    S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
3081a63d83f4SDavid Rientjes 	REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
308228a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL
3083631f9c18SAlexey Dobriyan 	REG("loginuid",   S_IWUSR|S_IRUGO, proc_loginuid_operations),
3084631f9c18SAlexey Dobriyan 	REG("sessionid",  S_IRUGO, proc_sessionid_operations),
308528a6d671SEric W. Biederman #endif
3086f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
3087631f9c18SAlexey Dobriyan 	REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3088f4f154fdSAkinobu Mita #endif
3089698ba7b5SChristoph Hellwig #ifdef CONFIG_ELF_CORE
3090631f9c18SAlexey Dobriyan 	REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
30913cb4a0bbSKawai, Hidehiro #endif
3092aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING
30931d1221f3SVasiliy Kulikov 	INF("io",	S_IRUSR, proc_tgid_io_accounting),
3094aba76fdbSAndrew Morton #endif
3095f133eccaSChris Metcalf #ifdef CONFIG_HARDWALL
3096f133eccaSChris Metcalf 	INF("hardwall",   S_IRUGO, proc_pid_hardwall),
3097f133eccaSChris Metcalf #endif
309828a6d671SEric W. Biederman };
309928a6d671SEric W. Biederman 
310028a6d671SEric W. Biederman static int proc_tgid_base_readdir(struct file * filp,
310128a6d671SEric W. Biederman 			     void * dirent, filldir_t filldir)
310228a6d671SEric W. Biederman {
310328a6d671SEric W. Biederman 	return proc_pident_readdir(filp,dirent,filldir,
310428a6d671SEric W. Biederman 				   tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
310528a6d671SEric W. Biederman }
310628a6d671SEric W. Biederman 
310700977a59SArjan van de Ven static const struct file_operations proc_tgid_base_operations = {
310828a6d671SEric W. Biederman 	.read		= generic_read_dir,
310928a6d671SEric W. Biederman 	.readdir	= proc_tgid_base_readdir,
31106038f373SArnd Bergmann 	.llseek		= default_llseek,
311128a6d671SEric W. Biederman };
311228a6d671SEric W. Biederman 
311328a6d671SEric W. Biederman static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
31147bcd6b0eSEric W. Biederman 	return proc_pident_lookup(dir, dentry,
31157bcd6b0eSEric W. Biederman 				  tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
311628a6d671SEric W. Biederman }
311728a6d671SEric W. Biederman 
3118c5ef1c42SArjan van de Ven static const struct inode_operations proc_tgid_base_inode_operations = {
311928a6d671SEric W. Biederman 	.lookup		= proc_tgid_base_lookup,
312028a6d671SEric W. Biederman 	.getattr	= pid_getattr,
312128a6d671SEric W. Biederman 	.setattr	= proc_setattr,
312228a6d671SEric W. Biederman };
312328a6d671SEric W. Biederman 
312460347f67SPavel Emelyanov static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
31251da177e4SLinus Torvalds {
312648e6484dSEric W. Biederman 	struct dentry *dentry, *leader, *dir;
31278578cea7SEric W. Biederman 	char buf[PROC_NUMBUF];
312848e6484dSEric W. Biederman 	struct qstr name;
31291da177e4SLinus Torvalds 
313048e6484dSEric W. Biederman 	name.name = buf;
313160347f67SPavel Emelyanov 	name.len = snprintf(buf, sizeof(buf), "%d", pid);
313260347f67SPavel Emelyanov 	dentry = d_hash_and_lookup(mnt->mnt_root, &name);
313348e6484dSEric W. Biederman 	if (dentry) {
313448e6484dSEric W. Biederman 		shrink_dcache_parent(dentry);
313548e6484dSEric W. Biederman 		d_drop(dentry);
313648e6484dSEric W. Biederman 		dput(dentry);
31371da177e4SLinus Torvalds 	}
31381da177e4SLinus Torvalds 
313948e6484dSEric W. Biederman 	name.name = buf;
314060347f67SPavel Emelyanov 	name.len = snprintf(buf, sizeof(buf), "%d", tgid);
314160347f67SPavel Emelyanov 	leader = d_hash_and_lookup(mnt->mnt_root, &name);
314248e6484dSEric W. Biederman 	if (!leader)
314348e6484dSEric W. Biederman 		goto out;
314448e6484dSEric W. Biederman 
314548e6484dSEric W. Biederman 	name.name = "task";
314648e6484dSEric W. Biederman 	name.len = strlen(name.name);
314748e6484dSEric W. Biederman 	dir = d_hash_and_lookup(leader, &name);
314848e6484dSEric W. Biederman 	if (!dir)
314948e6484dSEric W. Biederman 		goto out_put_leader;
315048e6484dSEric W. Biederman 
315148e6484dSEric W. Biederman 	name.name = buf;
315260347f67SPavel Emelyanov 	name.len = snprintf(buf, sizeof(buf), "%d", pid);
315348e6484dSEric W. Biederman 	dentry = d_hash_and_lookup(dir, &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 	}
315948e6484dSEric W. Biederman 
316048e6484dSEric W. Biederman 	dput(dir);
316148e6484dSEric W. Biederman out_put_leader:
316248e6484dSEric W. Biederman 	dput(leader);
316348e6484dSEric W. Biederman out:
316448e6484dSEric W. Biederman 	return;
31651da177e4SLinus Torvalds }
31661da177e4SLinus Torvalds 
31670895e91dSRandy Dunlap /**
31680895e91dSRandy Dunlap  * proc_flush_task -  Remove dcache entries for @task from the /proc dcache.
31690895e91dSRandy Dunlap  * @task: task that should be flushed.
31700895e91dSRandy Dunlap  *
31710895e91dSRandy Dunlap  * When flushing dentries from proc, one needs to flush them from global
317260347f67SPavel Emelyanov  * proc (proc_mnt) and from all the namespaces' procs this task was seen
31730895e91dSRandy Dunlap  * in. This call is supposed to do all of this job.
31740895e91dSRandy Dunlap  *
31750895e91dSRandy Dunlap  * Looks in the dcache for
31760895e91dSRandy Dunlap  * /proc/@pid
31770895e91dSRandy Dunlap  * /proc/@tgid/task/@pid
31780895e91dSRandy Dunlap  * if either directory is present flushes it and all of it'ts children
31790895e91dSRandy Dunlap  * from the dcache.
31800895e91dSRandy Dunlap  *
31810895e91dSRandy Dunlap  * It is safe and reasonable to cache /proc entries for a task until
31820895e91dSRandy Dunlap  * that task exits.  After that they just clog up the dcache with
31830895e91dSRandy Dunlap  * useless entries, possibly causing useful dcache entries to be
31840895e91dSRandy Dunlap  * flushed instead.  This routine is proved to flush those useless
31850895e91dSRandy Dunlap  * dcache entries at process exit time.
31860895e91dSRandy Dunlap  *
31870895e91dSRandy Dunlap  * NOTE: This routine is just an optimization so it does not guarantee
31880895e91dSRandy Dunlap  *       that no dcache entries will exist at process exit time it
31890895e91dSRandy Dunlap  *       just makes it very unlikely that any will persist.
319060347f67SPavel Emelyanov  */
319160347f67SPavel Emelyanov 
319260347f67SPavel Emelyanov void proc_flush_task(struct task_struct *task)
319360347f67SPavel Emelyanov {
31949fcc2d15SEric W. Biederman 	int i;
31959b4d1cbeSOleg Nesterov 	struct pid *pid, *tgid;
3196130f77ecSPavel Emelyanov 	struct upid *upid;
3197130f77ecSPavel Emelyanov 
3198130f77ecSPavel Emelyanov 	pid = task_pid(task);
3199130f77ecSPavel Emelyanov 	tgid = task_tgid(task);
32009fcc2d15SEric W. Biederman 
32019fcc2d15SEric W. Biederman 	for (i = 0; i <= pid->level; i++) {
3202130f77ecSPavel Emelyanov 		upid = &pid->numbers[i];
3203130f77ecSPavel Emelyanov 		proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
32049b4d1cbeSOleg Nesterov 					tgid->numbers[i].nr);
3205130f77ecSPavel Emelyanov 	}
32066f4e6433SPavel Emelyanov 
32076f4e6433SPavel Emelyanov 	upid = &pid->numbers[pid->level];
32086f4e6433SPavel Emelyanov 	if (upid->nr == 1)
32096f4e6433SPavel Emelyanov 		pid_ns_release_proc(upid->ns);
321060347f67SPavel Emelyanov }
321160347f67SPavel Emelyanov 
32129711ef99SAdrian Bunk static struct dentry *proc_pid_instantiate(struct inode *dir,
32139711ef99SAdrian Bunk 					   struct dentry * dentry,
3214c5141e6dSEric Dumazet 					   struct task_struct *task, const void *ptr)
3215444ceed8SEric W. Biederman {
3216444ceed8SEric W. Biederman 	struct dentry *error = ERR_PTR(-ENOENT);
3217444ceed8SEric W. Biederman 	struct inode *inode;
3218444ceed8SEric W. Biederman 
321961a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
3220444ceed8SEric W. Biederman 	if (!inode)
3221444ceed8SEric W. Biederman 		goto out;
3222444ceed8SEric W. Biederman 
3223444ceed8SEric W. Biederman 	inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3224444ceed8SEric W. Biederman 	inode->i_op = &proc_tgid_base_inode_operations;
3225444ceed8SEric W. Biederman 	inode->i_fop = &proc_tgid_base_operations;
3226444ceed8SEric W. Biederman 	inode->i_flags|=S_IMMUTABLE;
3227aed54175SVegard Nossum 
3228bfe86848SMiklos Szeredi 	set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
3229bfe86848SMiklos Szeredi 						  ARRAY_SIZE(tgid_base_stuff)));
3230444ceed8SEric W. Biederman 
3231fb045adbSNick Piggin 	d_set_d_op(dentry, &pid_dentry_operations);
3232444ceed8SEric W. Biederman 
3233444ceed8SEric W. Biederman 	d_add(dentry, inode);
3234444ceed8SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
3235444ceed8SEric W. Biederman 	if (pid_revalidate(dentry, NULL))
3236444ceed8SEric W. Biederman 		error = NULL;
3237444ceed8SEric W. Biederman out:
3238444ceed8SEric W. Biederman 	return error;
3239444ceed8SEric W. Biederman }
3240444ceed8SEric W. Biederman 
32411da177e4SLinus Torvalds struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
32421da177e4SLinus Torvalds {
324373d36460SDan Carpenter 	struct dentry *result;
32441da177e4SLinus Torvalds 	struct task_struct *task;
32451da177e4SLinus Torvalds 	unsigned tgid;
3246b488893aSPavel Emelyanov 	struct pid_namespace *ns;
32471da177e4SLinus Torvalds 
3248801199ceSEric W. Biederman 	result = proc_base_lookup(dir, dentry);
3249801199ceSEric W. Biederman 	if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
3250801199ceSEric W. Biederman 		goto out;
3251801199ceSEric W. Biederman 
32521da177e4SLinus Torvalds 	tgid = name_to_int(dentry);
32531da177e4SLinus Torvalds 	if (tgid == ~0U)
32541da177e4SLinus Torvalds 		goto out;
32551da177e4SLinus Torvalds 
3256b488893aSPavel Emelyanov 	ns = dentry->d_sb->s_fs_info;
3257de758734SEric W. Biederman 	rcu_read_lock();
3258b488893aSPavel Emelyanov 	task = find_task_by_pid_ns(tgid, ns);
32591da177e4SLinus Torvalds 	if (task)
32601da177e4SLinus Torvalds 		get_task_struct(task);
3261de758734SEric W. Biederman 	rcu_read_unlock();
32621da177e4SLinus Torvalds 	if (!task)
32631da177e4SLinus Torvalds 		goto out;
32641da177e4SLinus Torvalds 
3265444ceed8SEric W. Biederman 	result = proc_pid_instantiate(dir, dentry, task, NULL);
326648e6484dSEric W. Biederman 	put_task_struct(task);
32671da177e4SLinus Torvalds out:
3268cd6a3ce9SEric W. Biederman 	return result;
32691da177e4SLinus Torvalds }
32701da177e4SLinus Torvalds 
32711da177e4SLinus Torvalds /*
32720804ef4bSEric W. Biederman  * Find the first task with tgid >= tgid
32730bc58a91SEric W. Biederman  *
32741da177e4SLinus Torvalds  */
327519fd4bb2SEric W. Biederman struct tgid_iter {
327619fd4bb2SEric W. Biederman 	unsigned int tgid;
32770804ef4bSEric W. Biederman 	struct task_struct *task;
327819fd4bb2SEric W. Biederman };
327919fd4bb2SEric W. Biederman static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
328019fd4bb2SEric W. Biederman {
32810804ef4bSEric W. Biederman 	struct pid *pid;
32821da177e4SLinus Torvalds 
328319fd4bb2SEric W. Biederman 	if (iter.task)
328419fd4bb2SEric W. Biederman 		put_task_struct(iter.task);
32850804ef4bSEric W. Biederman 	rcu_read_lock();
32860804ef4bSEric W. Biederman retry:
328719fd4bb2SEric W. Biederman 	iter.task = NULL;
328819fd4bb2SEric W. Biederman 	pid = find_ge_pid(iter.tgid, ns);
32890804ef4bSEric W. Biederman 	if (pid) {
329019fd4bb2SEric W. Biederman 		iter.tgid = pid_nr_ns(pid, ns);
329119fd4bb2SEric W. Biederman 		iter.task = pid_task(pid, PIDTYPE_PID);
32920804ef4bSEric W. Biederman 		/* What we to know is if the pid we have find is the
32930804ef4bSEric W. Biederman 		 * pid of a thread_group_leader.  Testing for task
32940804ef4bSEric W. Biederman 		 * being a thread_group_leader is the obvious thing
32950804ef4bSEric W. Biederman 		 * todo but there is a window when it fails, due to
32960804ef4bSEric W. Biederman 		 * the pid transfer logic in de_thread.
32970804ef4bSEric W. Biederman 		 *
32980804ef4bSEric W. Biederman 		 * So we perform the straight forward test of seeing
32990804ef4bSEric W. Biederman 		 * if the pid we have found is the pid of a thread
33000804ef4bSEric W. Biederman 		 * group leader, and don't worry if the task we have
33010804ef4bSEric W. Biederman 		 * found doesn't happen to be a thread group leader.
33020804ef4bSEric W. Biederman 		 * As we don't care in the case of readdir.
33030bc58a91SEric W. Biederman 		 */
330419fd4bb2SEric W. Biederman 		if (!iter.task || !has_group_leader_pid(iter.task)) {
330519fd4bb2SEric W. Biederman 			iter.tgid += 1;
33060804ef4bSEric W. Biederman 			goto retry;
330719fd4bb2SEric W. Biederman 		}
330819fd4bb2SEric W. Biederman 		get_task_struct(iter.task);
33091da177e4SLinus Torvalds 	}
3310454cc105SEric W. Biederman 	rcu_read_unlock();
331119fd4bb2SEric W. Biederman 	return iter;
33121da177e4SLinus Torvalds }
33131da177e4SLinus Torvalds 
33147bcd6b0eSEric W. Biederman #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
33151da177e4SLinus Torvalds 
331661a28784SEric W. Biederman static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
331719fd4bb2SEric W. Biederman 	struct tgid_iter iter)
331861a28784SEric W. Biederman {
331961a28784SEric W. Biederman 	char name[PROC_NUMBUF];
332019fd4bb2SEric W. Biederman 	int len = snprintf(name, sizeof(name), "%d", iter.tgid);
332161a28784SEric W. Biederman 	return proc_fill_cache(filp, dirent, filldir, name, len,
332219fd4bb2SEric W. Biederman 				proc_pid_instantiate, iter.task, NULL);
332361a28784SEric W. Biederman }
332461a28784SEric W. Biederman 
33251da177e4SLinus Torvalds /* for the /proc/ directory itself, after non-process stuff has been done */
33261da177e4SLinus Torvalds int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
33271da177e4SLinus Torvalds {
3328d8bdc59fSLinus Torvalds 	unsigned int nr;
3329d8bdc59fSLinus Torvalds 	struct task_struct *reaper;
333019fd4bb2SEric W. Biederman 	struct tgid_iter iter;
3331b488893aSPavel Emelyanov 	struct pid_namespace *ns;
33321da177e4SLinus Torvalds 
3333d8bdc59fSLinus Torvalds 	if (filp->f_pos >= PID_MAX_LIMIT + TGID_OFFSET)
3334d8bdc59fSLinus Torvalds 		goto out_no_task;
3335d8bdc59fSLinus Torvalds 	nr = filp->f_pos - FIRST_PROCESS_ENTRY;
3336d8bdc59fSLinus Torvalds 
3337d8bdc59fSLinus Torvalds 	reaper = get_proc_task(filp->f_path.dentry->d_inode);
333861a28784SEric W. Biederman 	if (!reaper)
333961a28784SEric W. Biederman 		goto out_no_task;
334061a28784SEric W. Biederman 
33417bcd6b0eSEric W. Biederman 	for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
3342c5141e6dSEric Dumazet 		const struct pid_entry *p = &proc_base_stuff[nr];
334361a28784SEric W. Biederman 		if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
3344801199ceSEric W. Biederman 			goto out;
33451da177e4SLinus Torvalds 	}
33461da177e4SLinus Torvalds 
3347b488893aSPavel Emelyanov 	ns = filp->f_dentry->d_sb->s_fs_info;
334819fd4bb2SEric W. Biederman 	iter.task = NULL;
334919fd4bb2SEric W. Biederman 	iter.tgid = filp->f_pos - TGID_OFFSET;
335019fd4bb2SEric W. Biederman 	for (iter = next_tgid(ns, iter);
335119fd4bb2SEric W. Biederman 	     iter.task;
335219fd4bb2SEric W. Biederman 	     iter.tgid += 1, iter = next_tgid(ns, iter)) {
335319fd4bb2SEric W. Biederman 		filp->f_pos = iter.tgid + TGID_OFFSET;
335419fd4bb2SEric W. Biederman 		if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
335519fd4bb2SEric W. Biederman 			put_task_struct(iter.task);
33560804ef4bSEric W. Biederman 			goto out;
33571da177e4SLinus Torvalds 		}
33581da177e4SLinus Torvalds 	}
33590804ef4bSEric W. Biederman 	filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
33600804ef4bSEric W. Biederman out:
336161a28784SEric W. Biederman 	put_task_struct(reaper);
336261a28784SEric W. Biederman out_no_task:
33631da177e4SLinus Torvalds 	return 0;
33641da177e4SLinus Torvalds }
33651da177e4SLinus Torvalds 
33660bc58a91SEric W. Biederman /*
336728a6d671SEric W. Biederman  * Tasks
336828a6d671SEric W. Biederman  */
3369c5141e6dSEric Dumazet static const struct pid_entry tid_base_stuff[] = {
3370631f9c18SAlexey Dobriyan 	DIR("fd",        S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
33713835541dSJerome Marchand 	DIR("fdinfo",    S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
33726b4e306aSEric W. Biederman 	DIR("ns",	 S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
3373631f9c18SAlexey Dobriyan 	REG("environ",   S_IRUSR, proc_environ_operations),
3374631f9c18SAlexey Dobriyan 	INF("auxv",      S_IRUSR, proc_pid_auxv),
3375631f9c18SAlexey Dobriyan 	ONE("status",    S_IRUGO, proc_pid_status),
3376a9712bc1SAl Viro 	ONE("personality", S_IRUGO, proc_pid_personality),
33773036e7b4SJiri Olsa 	INF("limits",	 S_IRUGO, proc_pid_limits),
337843ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
3379631f9c18SAlexey Dobriyan 	REG("sched",     S_IRUGO|S_IWUSR, proc_pid_sched_operations),
338043ae34cbSIngo Molnar #endif
33814614a696Sjohn stultz 	REG("comm",      S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
3382ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3383a9712bc1SAl Viro 	INF("syscall",   S_IRUGO, proc_pid_syscall),
3384ebcb6734SRoland McGrath #endif
3385631f9c18SAlexey Dobriyan 	INF("cmdline",   S_IRUGO, proc_pid_cmdline),
3386631f9c18SAlexey Dobriyan 	ONE("stat",      S_IRUGO, proc_tid_stat),
3387631f9c18SAlexey Dobriyan 	ONE("statm",     S_IRUGO, proc_pid_statm),
3388631f9c18SAlexey Dobriyan 	REG("maps",      S_IRUGO, proc_maps_operations),
338928a6d671SEric W. Biederman #ifdef CONFIG_NUMA
3390631f9c18SAlexey Dobriyan 	REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
339128a6d671SEric W. Biederman #endif
3392631f9c18SAlexey Dobriyan 	REG("mem",       S_IRUSR|S_IWUSR, proc_mem_operations),
3393631f9c18SAlexey Dobriyan 	LNK("cwd",       proc_cwd_link),
3394631f9c18SAlexey Dobriyan 	LNK("root",      proc_root_link),
3395631f9c18SAlexey Dobriyan 	LNK("exe",       proc_exe_link),
3396631f9c18SAlexey Dobriyan 	REG("mounts",    S_IRUGO, proc_mounts_operations),
3397631f9c18SAlexey Dobriyan 	REG("mountinfo",  S_IRUGO, proc_mountinfo_operations),
33981e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR
3399631f9c18SAlexey Dobriyan 	REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3400631f9c18SAlexey Dobriyan 	REG("smaps",     S_IRUGO, proc_smaps_operations),
3401ca6b0bf0SAl Viro 	REG("pagemap",    S_IRUGO, proc_pagemap_operations),
340228a6d671SEric W. Biederman #endif
340328a6d671SEric W. Biederman #ifdef CONFIG_SECURITY
3404631f9c18SAlexey Dobriyan 	DIR("attr",      S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
340528a6d671SEric W. Biederman #endif
340628a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS
3407631f9c18SAlexey Dobriyan 	INF("wchan",     S_IRUGO, proc_pid_wchan),
340828a6d671SEric W. Biederman #endif
34092ec220e2SKen Chen #ifdef CONFIG_STACKTRACE
3410a9712bc1SAl Viro 	ONE("stack",      S_IRUGO, proc_pid_stack),
341128a6d671SEric W. Biederman #endif
341228a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS
3413631f9c18SAlexey Dobriyan 	INF("schedstat", S_IRUGO, proc_pid_schedstat),
341428a6d671SEric W. Biederman #endif
34159745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP
3416631f9c18SAlexey Dobriyan 	REG("latency",  S_IRUGO, proc_lstats_operations),
34179745512cSArjan van de Ven #endif
34188793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET
3419631f9c18SAlexey Dobriyan 	REG("cpuset",    S_IRUGO, proc_cpuset_operations),
342028a6d671SEric W. Biederman #endif
3421a424316cSPaul Menage #ifdef CONFIG_CGROUPS
3422631f9c18SAlexey Dobriyan 	REG("cgroup",  S_IRUGO, proc_cgroup_operations),
3423a424316cSPaul Menage #endif
3424631f9c18SAlexey Dobriyan 	INF("oom_score", S_IRUGO, proc_oom_score),
3425631f9c18SAlexey Dobriyan 	REG("oom_adj",   S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
3426a63d83f4SDavid Rientjes 	REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
342728a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL
3428631f9c18SAlexey Dobriyan 	REG("loginuid",  S_IWUSR|S_IRUGO, proc_loginuid_operations),
342926ec3c64SAl Viro 	REG("sessionid",  S_IRUGO, proc_sessionid_operations),
343028a6d671SEric W. Biederman #endif
3431f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
3432631f9c18SAlexey Dobriyan 	REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3433f4f154fdSAkinobu Mita #endif
3434297c5d92SAndrea Righi #ifdef CONFIG_TASK_IO_ACCOUNTING
34351d1221f3SVasiliy Kulikov 	INF("io",	S_IRUSR, proc_tid_io_accounting),
3436297c5d92SAndrea Righi #endif
3437f133eccaSChris Metcalf #ifdef CONFIG_HARDWALL
3438f133eccaSChris Metcalf 	INF("hardwall",   S_IRUGO, proc_pid_hardwall),
3439f133eccaSChris Metcalf #endif
344028a6d671SEric W. Biederman };
344128a6d671SEric W. Biederman 
344228a6d671SEric W. Biederman static int proc_tid_base_readdir(struct file * filp,
344328a6d671SEric W. Biederman 			     void * dirent, filldir_t filldir)
344428a6d671SEric W. Biederman {
344528a6d671SEric W. Biederman 	return proc_pident_readdir(filp,dirent,filldir,
344628a6d671SEric W. Biederman 				   tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
344728a6d671SEric W. Biederman }
344828a6d671SEric W. Biederman 
344928a6d671SEric W. Biederman static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
34507bcd6b0eSEric W. Biederman 	return proc_pident_lookup(dir, dentry,
34517bcd6b0eSEric W. Biederman 				  tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
345228a6d671SEric W. Biederman }
345328a6d671SEric W. Biederman 
345400977a59SArjan van de Ven static const struct file_operations proc_tid_base_operations = {
345528a6d671SEric W. Biederman 	.read		= generic_read_dir,
345628a6d671SEric W. Biederman 	.readdir	= proc_tid_base_readdir,
34576038f373SArnd Bergmann 	.llseek		= default_llseek,
345828a6d671SEric W. Biederman };
345928a6d671SEric W. Biederman 
3460c5ef1c42SArjan van de Ven static const struct inode_operations proc_tid_base_inode_operations = {
346128a6d671SEric W. Biederman 	.lookup		= proc_tid_base_lookup,
346228a6d671SEric W. Biederman 	.getattr	= pid_getattr,
346328a6d671SEric W. Biederman 	.setattr	= proc_setattr,
346428a6d671SEric W. Biederman };
346528a6d671SEric W. Biederman 
3466444ceed8SEric W. Biederman static struct dentry *proc_task_instantiate(struct inode *dir,
3467c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
3468444ceed8SEric W. Biederman {
3469444ceed8SEric W. Biederman 	struct dentry *error = ERR_PTR(-ENOENT);
3470444ceed8SEric W. Biederman 	struct inode *inode;
347161a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
3472444ceed8SEric W. Biederman 
3473444ceed8SEric W. Biederman 	if (!inode)
3474444ceed8SEric W. Biederman 		goto out;
3475444ceed8SEric W. Biederman 	inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3476444ceed8SEric W. Biederman 	inode->i_op = &proc_tid_base_inode_operations;
3477444ceed8SEric W. Biederman 	inode->i_fop = &proc_tid_base_operations;
3478444ceed8SEric W. Biederman 	inode->i_flags|=S_IMMUTABLE;
3479aed54175SVegard Nossum 
3480bfe86848SMiklos Szeredi 	set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3481bfe86848SMiklos Szeredi 						  ARRAY_SIZE(tid_base_stuff)));
3482444ceed8SEric W. Biederman 
3483fb045adbSNick Piggin 	d_set_d_op(dentry, &pid_dentry_operations);
3484444ceed8SEric W. Biederman 
3485444ceed8SEric W. Biederman 	d_add(dentry, inode);
3486444ceed8SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
3487444ceed8SEric W. Biederman 	if (pid_revalidate(dentry, NULL))
3488444ceed8SEric W. Biederman 		error = NULL;
3489444ceed8SEric W. Biederman out:
3490444ceed8SEric W. Biederman 	return error;
3491444ceed8SEric W. Biederman }
3492444ceed8SEric W. Biederman 
349328a6d671SEric W. Biederman static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
349428a6d671SEric W. Biederman {
349528a6d671SEric W. Biederman 	struct dentry *result = ERR_PTR(-ENOENT);
349628a6d671SEric W. Biederman 	struct task_struct *task;
349728a6d671SEric W. Biederman 	struct task_struct *leader = get_proc_task(dir);
349828a6d671SEric W. Biederman 	unsigned tid;
3499b488893aSPavel Emelyanov 	struct pid_namespace *ns;
350028a6d671SEric W. Biederman 
350128a6d671SEric W. Biederman 	if (!leader)
350228a6d671SEric W. Biederman 		goto out_no_task;
350328a6d671SEric W. Biederman 
350428a6d671SEric W. Biederman 	tid = name_to_int(dentry);
350528a6d671SEric W. Biederman 	if (tid == ~0U)
350628a6d671SEric W. Biederman 		goto out;
350728a6d671SEric W. Biederman 
3508b488893aSPavel Emelyanov 	ns = dentry->d_sb->s_fs_info;
350928a6d671SEric W. Biederman 	rcu_read_lock();
3510b488893aSPavel Emelyanov 	task = find_task_by_pid_ns(tid, ns);
351128a6d671SEric W. Biederman 	if (task)
351228a6d671SEric W. Biederman 		get_task_struct(task);
351328a6d671SEric W. Biederman 	rcu_read_unlock();
351428a6d671SEric W. Biederman 	if (!task)
351528a6d671SEric W. Biederman 		goto out;
3516bac0abd6SPavel Emelyanov 	if (!same_thread_group(leader, task))
351728a6d671SEric W. Biederman 		goto out_drop_task;
351828a6d671SEric W. Biederman 
3519444ceed8SEric W. Biederman 	result = proc_task_instantiate(dir, dentry, task, NULL);
352028a6d671SEric W. Biederman out_drop_task:
352128a6d671SEric W. Biederman 	put_task_struct(task);
352228a6d671SEric W. Biederman out:
352328a6d671SEric W. Biederman 	put_task_struct(leader);
352428a6d671SEric W. Biederman out_no_task:
352528a6d671SEric W. Biederman 	return result;
352628a6d671SEric W. Biederman }
352728a6d671SEric W. Biederman 
352828a6d671SEric W. Biederman /*
35290bc58a91SEric W. Biederman  * Find the first tid of a thread group to return to user space.
35300bc58a91SEric W. Biederman  *
35310bc58a91SEric W. Biederman  * Usually this is just the thread group leader, but if the users
35320bc58a91SEric W. Biederman  * buffer was too small or there was a seek into the middle of the
35330bc58a91SEric W. Biederman  * directory we have more work todo.
35340bc58a91SEric W. Biederman  *
35350bc58a91SEric W. Biederman  * In the case of a short read we start with find_task_by_pid.
35360bc58a91SEric W. Biederman  *
35370bc58a91SEric W. Biederman  * In the case of a seek we start with the leader and walk nr
35380bc58a91SEric W. Biederman  * threads past it.
35390bc58a91SEric W. Biederman  */
3540cc288738SEric W. Biederman static struct task_struct *first_tid(struct task_struct *leader,
3541b488893aSPavel Emelyanov 		int tid, int nr, struct pid_namespace *ns)
35420bc58a91SEric W. Biederman {
3543a872ff0cSOleg Nesterov 	struct task_struct *pos;
35440bc58a91SEric W. Biederman 
3545cc288738SEric W. Biederman 	rcu_read_lock();
35460bc58a91SEric W. Biederman 	/* Attempt to start with the pid of a thread */
35470bc58a91SEric W. Biederman 	if (tid && (nr > 0)) {
3548b488893aSPavel Emelyanov 		pos = find_task_by_pid_ns(tid, ns);
3549a872ff0cSOleg Nesterov 		if (pos && (pos->group_leader == leader))
3550a872ff0cSOleg Nesterov 			goto found;
35510bc58a91SEric W. Biederman 	}
35520bc58a91SEric W. Biederman 
35530bc58a91SEric W. Biederman 	/* If nr exceeds the number of threads there is nothing todo */
35540bc58a91SEric W. Biederman 	pos = NULL;
3555a872ff0cSOleg Nesterov 	if (nr && nr >= get_nr_threads(leader))
3556a872ff0cSOleg Nesterov 		goto out;
3557a872ff0cSOleg Nesterov 
3558a872ff0cSOleg Nesterov 	/* If we haven't found our starting place yet start
3559a872ff0cSOleg Nesterov 	 * with the leader and walk nr threads forward.
3560a872ff0cSOleg Nesterov 	 */
3561a872ff0cSOleg Nesterov 	for (pos = leader; nr > 0; --nr) {
3562a872ff0cSOleg Nesterov 		pos = next_thread(pos);
3563a872ff0cSOleg Nesterov 		if (pos == leader) {
3564a872ff0cSOleg Nesterov 			pos = NULL;
3565a872ff0cSOleg Nesterov 			goto out;
3566a872ff0cSOleg Nesterov 		}
3567a872ff0cSOleg Nesterov 	}
3568a872ff0cSOleg Nesterov found:
3569a872ff0cSOleg Nesterov 	get_task_struct(pos);
3570a872ff0cSOleg Nesterov out:
3571cc288738SEric W. Biederman 	rcu_read_unlock();
35720bc58a91SEric W. Biederman 	return pos;
35730bc58a91SEric W. Biederman }
35740bc58a91SEric W. Biederman 
35750bc58a91SEric W. Biederman /*
35760bc58a91SEric W. Biederman  * Find the next thread in the thread list.
35770bc58a91SEric W. Biederman  * Return NULL if there is an error or no next thread.
35780bc58a91SEric W. Biederman  *
35790bc58a91SEric W. Biederman  * The reference to the input task_struct is released.
35800bc58a91SEric W. Biederman  */
35810bc58a91SEric W. Biederman static struct task_struct *next_tid(struct task_struct *start)
35820bc58a91SEric W. Biederman {
3583c1df7fb8SOleg Nesterov 	struct task_struct *pos = NULL;
3584cc288738SEric W. Biederman 	rcu_read_lock();
3585c1df7fb8SOleg Nesterov 	if (pid_alive(start)) {
35860bc58a91SEric W. Biederman 		pos = next_thread(start);
3587c1df7fb8SOleg Nesterov 		if (thread_group_leader(pos))
35880bc58a91SEric W. Biederman 			pos = NULL;
3589c1df7fb8SOleg Nesterov 		else
3590c1df7fb8SOleg Nesterov 			get_task_struct(pos);
3591c1df7fb8SOleg Nesterov 	}
3592cc288738SEric W. Biederman 	rcu_read_unlock();
35930bc58a91SEric W. Biederman 	put_task_struct(start);
35940bc58a91SEric W. Biederman 	return pos;
35950bc58a91SEric W. Biederman }
35960bc58a91SEric W. Biederman 
359761a28784SEric W. Biederman static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
359861a28784SEric W. Biederman 	struct task_struct *task, int tid)
359961a28784SEric W. Biederman {
360061a28784SEric W. Biederman 	char name[PROC_NUMBUF];
360161a28784SEric W. Biederman 	int len = snprintf(name, sizeof(name), "%d", tid);
360261a28784SEric W. Biederman 	return proc_fill_cache(filp, dirent, filldir, name, len,
360361a28784SEric W. Biederman 				proc_task_instantiate, task, NULL);
360461a28784SEric W. Biederman }
360561a28784SEric W. Biederman 
36061da177e4SLinus Torvalds /* for the /proc/TGID/task/ directories */
36071da177e4SLinus Torvalds static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
36081da177e4SLinus Torvalds {
36092fddfeefSJosef "Jeff" Sipek 	struct dentry *dentry = filp->f_path.dentry;
36101da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
36117d895244SGuillaume Chazarain 	struct task_struct *leader = NULL;
36120bc58a91SEric W. Biederman 	struct task_struct *task;
36131da177e4SLinus Torvalds 	int retval = -ENOENT;
36141da177e4SLinus Torvalds 	ino_t ino;
36150bc58a91SEric W. Biederman 	int tid;
3616b488893aSPavel Emelyanov 	struct pid_namespace *ns;
36171da177e4SLinus Torvalds 
36187d895244SGuillaume Chazarain 	task = get_proc_task(inode);
36197d895244SGuillaume Chazarain 	if (!task)
36207d895244SGuillaume Chazarain 		goto out_no_task;
36217d895244SGuillaume Chazarain 	rcu_read_lock();
36227d895244SGuillaume Chazarain 	if (pid_alive(task)) {
36237d895244SGuillaume Chazarain 		leader = task->group_leader;
36247d895244SGuillaume Chazarain 		get_task_struct(leader);
36257d895244SGuillaume Chazarain 	}
36267d895244SGuillaume Chazarain 	rcu_read_unlock();
36277d895244SGuillaume Chazarain 	put_task_struct(task);
362899f89551SEric W. Biederman 	if (!leader)
362999f89551SEric W. Biederman 		goto out_no_task;
36301da177e4SLinus Torvalds 	retval = 0;
36311da177e4SLinus Torvalds 
3632ee568b25SLinus Torvalds 	switch ((unsigned long)filp->f_pos) {
36331da177e4SLinus Torvalds 	case 0:
36341da177e4SLinus Torvalds 		ino = inode->i_ino;
3635ee6f779bSZhang Le 		if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) < 0)
36361da177e4SLinus Torvalds 			goto out;
3637ee6f779bSZhang Le 		filp->f_pos++;
36381da177e4SLinus Torvalds 		/* fall through */
36391da177e4SLinus Torvalds 	case 1:
36401da177e4SLinus Torvalds 		ino = parent_ino(dentry);
3641ee6f779bSZhang Le 		if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) < 0)
36421da177e4SLinus Torvalds 			goto out;
3643ee6f779bSZhang Le 		filp->f_pos++;
36441da177e4SLinus Torvalds 		/* fall through */
36451da177e4SLinus Torvalds 	}
36461da177e4SLinus Torvalds 
36470bc58a91SEric W. Biederman 	/* f_version caches the tgid value that the last readdir call couldn't
36480bc58a91SEric W. Biederman 	 * return. lseek aka telldir automagically resets f_version to 0.
36490bc58a91SEric W. Biederman 	 */
3650b488893aSPavel Emelyanov 	ns = filp->f_dentry->d_sb->s_fs_info;
36512b47c361SMathieu Desnoyers 	tid = (int)filp->f_version;
36520bc58a91SEric W. Biederman 	filp->f_version = 0;
3653ee6f779bSZhang Le 	for (task = first_tid(leader, tid, filp->f_pos - 2, ns);
36540bc58a91SEric W. Biederman 	     task;
3655ee6f779bSZhang Le 	     task = next_tid(task), filp->f_pos++) {
3656b488893aSPavel Emelyanov 		tid = task_pid_nr_ns(task, ns);
365761a28784SEric W. Biederman 		if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
36580bc58a91SEric W. Biederman 			/* returning this tgid failed, save it as the first
36590bc58a91SEric W. Biederman 			 * pid for the next readir call */
36602b47c361SMathieu Desnoyers 			filp->f_version = (u64)tid;
36610bc58a91SEric W. Biederman 			put_task_struct(task);
36621da177e4SLinus Torvalds 			break;
36630bc58a91SEric W. Biederman 		}
36641da177e4SLinus Torvalds 	}
36651da177e4SLinus Torvalds out:
366699f89551SEric W. Biederman 	put_task_struct(leader);
366799f89551SEric W. Biederman out_no_task:
36681da177e4SLinus Torvalds 	return retval;
36691da177e4SLinus Torvalds }
36706e66b52bSEric W. Biederman 
36716e66b52bSEric W. Biederman static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
36726e66b52bSEric W. Biederman {
36736e66b52bSEric W. Biederman 	struct inode *inode = dentry->d_inode;
367499f89551SEric W. Biederman 	struct task_struct *p = get_proc_task(inode);
36756e66b52bSEric W. Biederman 	generic_fillattr(inode, stat);
36766e66b52bSEric W. Biederman 
367799f89551SEric W. Biederman 	if (p) {
367899f89551SEric W. Biederman 		stat->nlink += get_nr_threads(p);
367999f89551SEric W. Biederman 		put_task_struct(p);
36806e66b52bSEric W. Biederman 	}
36816e66b52bSEric W. Biederman 
36826e66b52bSEric W. Biederman 	return 0;
36836e66b52bSEric W. Biederman }
368428a6d671SEric W. Biederman 
3685c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations = {
368628a6d671SEric W. Biederman 	.lookup		= proc_task_lookup,
368728a6d671SEric W. Biederman 	.getattr	= proc_task_getattr,
368828a6d671SEric W. Biederman 	.setattr	= proc_setattr,
368928a6d671SEric W. Biederman };
369028a6d671SEric W. Biederman 
369100977a59SArjan van de Ven static const struct file_operations proc_task_operations = {
369228a6d671SEric W. Biederman 	.read		= generic_read_dir,
369328a6d671SEric W. Biederman 	.readdir	= proc_task_readdir,
36946038f373SArnd Bergmann 	.llseek		= default_llseek,
369528a6d671SEric W. Biederman };
3696