xref: /openbmc/linux/fs/proc/base.c (revision 572d34b946bae070debd42db1143034d9687e13f)
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>
86640708a2SPavel 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 
138640708a2SPavel Emelyanov static int proc_fd_permission(struct inode *inode, int mask);
139640708a2SPavel 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 
201e268337dSLinus Torvalds static struct mm_struct *mm_access(struct task_struct *task, unsigned int mode)
202831830b5SAl Viro {
203704b836cSOleg Nesterov 	struct mm_struct *mm;
204ec6fd8a4SAl Viro 	int err;
20500f89d21SOleg Nesterov 
206ec6fd8a4SAl Viro 	err =  mutex_lock_killable(&task->signal->cred_guard_mutex);
207ec6fd8a4SAl Viro 	if (err)
208ec6fd8a4SAl Viro 		return ERR_PTR(err);
209704b836cSOleg Nesterov 
210704b836cSOleg Nesterov 	mm = get_task_mm(task);
211704b836cSOleg Nesterov 	if (mm && mm != current->mm &&
212e268337dSLinus Torvalds 			!ptrace_may_access(task, mode)) {
213831830b5SAl Viro 		mmput(mm);
214ec6fd8a4SAl Viro 		mm = ERR_PTR(-EACCES);
215831830b5SAl Viro 	}
2169b1bf12dSKOSAKI Motohiro 	mutex_unlock(&task->signal->cred_guard_mutex);
217704b836cSOleg Nesterov 
21813f0feafSOleg Nesterov 	return mm;
21913f0feafSOleg Nesterov }
220831830b5SAl Viro 
221e268337dSLinus Torvalds struct mm_struct *mm_for_maps(struct task_struct *task)
222e268337dSLinus Torvalds {
223e268337dSLinus Torvalds 	return mm_access(task, PTRACE_MODE_READ);
224e268337dSLinus Torvalds }
225e268337dSLinus Torvalds 
2261da177e4SLinus Torvalds static int proc_pid_cmdline(struct task_struct *task, char * buffer)
2271da177e4SLinus Torvalds {
2281da177e4SLinus Torvalds 	int res = 0;
2291da177e4SLinus Torvalds 	unsigned int len;
2301da177e4SLinus Torvalds 	struct mm_struct *mm = get_task_mm(task);
2311da177e4SLinus Torvalds 	if (!mm)
2321da177e4SLinus Torvalds 		goto out;
2331da177e4SLinus Torvalds 	if (!mm->arg_end)
2341da177e4SLinus Torvalds 		goto out_mm;	/* Shh! No looking before we're done */
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds  	len = mm->arg_end - mm->arg_start;
2371da177e4SLinus Torvalds 
2381da177e4SLinus Torvalds 	if (len > PAGE_SIZE)
2391da177e4SLinus Torvalds 		len = PAGE_SIZE;
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds 	res = access_process_vm(task, mm->arg_start, buffer, len, 0);
2421da177e4SLinus Torvalds 
2431da177e4SLinus Torvalds 	// If the nul at the end of args has been overwritten, then
2441da177e4SLinus Torvalds 	// assume application is using setproctitle(3).
2451da177e4SLinus Torvalds 	if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
2461da177e4SLinus Torvalds 		len = strnlen(buffer, res);
2471da177e4SLinus Torvalds 		if (len < res) {
2481da177e4SLinus Torvalds 		    res = len;
2491da177e4SLinus Torvalds 		} else {
2501da177e4SLinus Torvalds 			len = mm->env_end - mm->env_start;
2511da177e4SLinus Torvalds 			if (len > PAGE_SIZE - res)
2521da177e4SLinus Torvalds 				len = PAGE_SIZE - res;
2531da177e4SLinus Torvalds 			res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
2541da177e4SLinus Torvalds 			res = strnlen(buffer, res);
2551da177e4SLinus Torvalds 		}
2561da177e4SLinus Torvalds 	}
2571da177e4SLinus Torvalds out_mm:
2581da177e4SLinus Torvalds 	mmput(mm);
2591da177e4SLinus Torvalds out:
2601da177e4SLinus Torvalds 	return res;
2611da177e4SLinus Torvalds }
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds static int proc_pid_auxv(struct task_struct *task, char *buffer)
2641da177e4SLinus Torvalds {
2652fadaef4SAl Viro 	struct mm_struct *mm = mm_for_maps(task);
2662fadaef4SAl Viro 	int res = PTR_ERR(mm);
2672fadaef4SAl Viro 	if (mm && !IS_ERR(mm)) {
2681da177e4SLinus Torvalds 		unsigned int nwords = 0;
269dfe6b7d9SHannes Eder 		do {
2701da177e4SLinus Torvalds 			nwords += 2;
271dfe6b7d9SHannes Eder 		} while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
2721da177e4SLinus Torvalds 		res = nwords * sizeof(mm->saved_auxv[0]);
2731da177e4SLinus Torvalds 		if (res > PAGE_SIZE)
2741da177e4SLinus Torvalds 			res = PAGE_SIZE;
2751da177e4SLinus Torvalds 		memcpy(buffer, mm->saved_auxv, res);
2761da177e4SLinus Torvalds 		mmput(mm);
2771da177e4SLinus Torvalds 	}
2781da177e4SLinus Torvalds 	return res;
2791da177e4SLinus Torvalds }
2801da177e4SLinus Torvalds 
2811da177e4SLinus Torvalds 
2821da177e4SLinus Torvalds #ifdef CONFIG_KALLSYMS
2831da177e4SLinus Torvalds /*
2841da177e4SLinus Torvalds  * Provides a wchan file via kallsyms in a proper one-value-per-file format.
2851da177e4SLinus Torvalds  * Returns the resolved symbol.  If that fails, simply return the address.
2861da177e4SLinus Torvalds  */
2871da177e4SLinus Torvalds static int proc_pid_wchan(struct task_struct *task, char *buffer)
2881da177e4SLinus Torvalds {
289ffb45122SAlexey Dobriyan 	unsigned long wchan;
2909281aceaSTejun Heo 	char symname[KSYM_NAME_LEN];
2911da177e4SLinus Torvalds 
2921da177e4SLinus Torvalds 	wchan = get_wchan(task);
2931da177e4SLinus Torvalds 
2949d65cb4aSAlexey Dobriyan 	if (lookup_symbol_name(wchan, symname) < 0)
295f83ce3e6SJake Edge 		if (!ptrace_may_access(task, PTRACE_MODE_READ))
296f83ce3e6SJake Edge 			return 0;
297f83ce3e6SJake Edge 		else
2981da177e4SLinus Torvalds 			return sprintf(buffer, "%lu", wchan);
2999d65cb4aSAlexey Dobriyan 	else
3009d65cb4aSAlexey Dobriyan 		return sprintf(buffer, "%s", symname);
3011da177e4SLinus Torvalds }
3021da177e4SLinus Torvalds #endif /* CONFIG_KALLSYMS */
3031da177e4SLinus Torvalds 
304a9712bc1SAl Viro static int lock_trace(struct task_struct *task)
305a9712bc1SAl Viro {
306a9712bc1SAl Viro 	int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
307a9712bc1SAl Viro 	if (err)
308a9712bc1SAl Viro 		return err;
309a9712bc1SAl Viro 	if (!ptrace_may_access(task, PTRACE_MODE_ATTACH)) {
310a9712bc1SAl Viro 		mutex_unlock(&task->signal->cred_guard_mutex);
311a9712bc1SAl Viro 		return -EPERM;
312a9712bc1SAl Viro 	}
313a9712bc1SAl Viro 	return 0;
314a9712bc1SAl Viro }
315a9712bc1SAl Viro 
316a9712bc1SAl Viro static void unlock_trace(struct task_struct *task)
317a9712bc1SAl Viro {
318a9712bc1SAl Viro 	mutex_unlock(&task->signal->cred_guard_mutex);
319a9712bc1SAl Viro }
320a9712bc1SAl Viro 
3212ec220e2SKen Chen #ifdef CONFIG_STACKTRACE
3222ec220e2SKen Chen 
3232ec220e2SKen Chen #define MAX_STACK_TRACE_DEPTH	64
3242ec220e2SKen Chen 
3252ec220e2SKen Chen static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
3262ec220e2SKen Chen 			  struct pid *pid, struct task_struct *task)
3272ec220e2SKen Chen {
3282ec220e2SKen Chen 	struct stack_trace trace;
3292ec220e2SKen Chen 	unsigned long *entries;
330a9712bc1SAl Viro 	int err;
3312ec220e2SKen Chen 	int i;
3322ec220e2SKen Chen 
3332ec220e2SKen Chen 	entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
3342ec220e2SKen Chen 	if (!entries)
3352ec220e2SKen Chen 		return -ENOMEM;
3362ec220e2SKen Chen 
3372ec220e2SKen Chen 	trace.nr_entries	= 0;
3382ec220e2SKen Chen 	trace.max_entries	= MAX_STACK_TRACE_DEPTH;
3392ec220e2SKen Chen 	trace.entries		= entries;
3402ec220e2SKen Chen 	trace.skip		= 0;
341a9712bc1SAl Viro 
342a9712bc1SAl Viro 	err = lock_trace(task);
343a9712bc1SAl Viro 	if (!err) {
3442ec220e2SKen Chen 		save_stack_trace_tsk(task, &trace);
3452ec220e2SKen Chen 
3462ec220e2SKen Chen 		for (i = 0; i < trace.nr_entries; i++) {
34751e03149SKonstantin Khlebnikov 			seq_printf(m, "[<%pK>] %pS\n",
3482ec220e2SKen Chen 				   (void *)entries[i], (void *)entries[i]);
3492ec220e2SKen Chen 		}
350a9712bc1SAl Viro 		unlock_trace(task);
351a9712bc1SAl Viro 	}
3522ec220e2SKen Chen 	kfree(entries);
3532ec220e2SKen Chen 
354a9712bc1SAl Viro 	return err;
3552ec220e2SKen Chen }
3562ec220e2SKen Chen #endif
3572ec220e2SKen Chen 
3581da177e4SLinus Torvalds #ifdef CONFIG_SCHEDSTATS
3591da177e4SLinus Torvalds /*
3601da177e4SLinus Torvalds  * Provides /proc/PID/schedstat
3611da177e4SLinus Torvalds  */
3621da177e4SLinus Torvalds static int proc_pid_schedstat(struct task_struct *task, char *buffer)
3631da177e4SLinus Torvalds {
364172ba844SBalbir Singh 	return sprintf(buffer, "%llu %llu %lu\n",
365826e08b0SIngo Molnar 			(unsigned long long)task->se.sum_exec_runtime,
366826e08b0SIngo Molnar 			(unsigned long long)task->sched_info.run_delay,
3672d72376bSIngo Molnar 			task->sched_info.pcount);
3681da177e4SLinus Torvalds }
3691da177e4SLinus Torvalds #endif
3701da177e4SLinus Torvalds 
3719745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP
3729745512cSArjan van de Ven static int lstats_show_proc(struct seq_file *m, void *v)
3739745512cSArjan van de Ven {
3749745512cSArjan van de Ven 	int i;
37513d77c37SHiroshi Shimamoto 	struct inode *inode = m->private;
37613d77c37SHiroshi Shimamoto 	struct task_struct *task = get_proc_task(inode);
3779745512cSArjan van de Ven 
37813d77c37SHiroshi Shimamoto 	if (!task)
37913d77c37SHiroshi Shimamoto 		return -ESRCH;
38013d77c37SHiroshi Shimamoto 	seq_puts(m, "Latency Top version : v0.1\n");
3819745512cSArjan van de Ven 	for (i = 0; i < 32; i++) {
38234e49d4fSJoe Perches 		struct latency_record *lr = &task->latency_record[i];
38334e49d4fSJoe Perches 		if (lr->backtrace[0]) {
3849745512cSArjan van de Ven 			int q;
3859745512cSArjan van de Ven 			seq_printf(m, "%i %li %li",
38634e49d4fSJoe Perches 				   lr->count, lr->time, lr->max);
3879745512cSArjan van de Ven 			for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
38834e49d4fSJoe Perches 				unsigned long bt = lr->backtrace[q];
38934e49d4fSJoe Perches 				if (!bt)
3909745512cSArjan van de Ven 					break;
39134e49d4fSJoe Perches 				if (bt == ULONG_MAX)
3929745512cSArjan van de Ven 					break;
39334e49d4fSJoe Perches 				seq_printf(m, " %ps", (void *)bt);
3949745512cSArjan van de Ven 			}
3959d6de12fSAlexey Dobriyan 			seq_putc(m, '\n');
3969745512cSArjan van de Ven 		}
3979745512cSArjan van de Ven 
3989745512cSArjan van de Ven 	}
39913d77c37SHiroshi Shimamoto 	put_task_struct(task);
4009745512cSArjan van de Ven 	return 0;
4019745512cSArjan van de Ven }
4029745512cSArjan van de Ven 
4039745512cSArjan van de Ven static int lstats_open(struct inode *inode, struct file *file)
4049745512cSArjan van de Ven {
40513d77c37SHiroshi Shimamoto 	return single_open(file, lstats_show_proc, inode);
406d6643d12SHiroshi Shimamoto }
407d6643d12SHiroshi Shimamoto 
4089745512cSArjan van de Ven static ssize_t lstats_write(struct file *file, const char __user *buf,
4099745512cSArjan van de Ven 			    size_t count, loff_t *offs)
4109745512cSArjan van de Ven {
41113d77c37SHiroshi Shimamoto 	struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
4129745512cSArjan van de Ven 
41313d77c37SHiroshi Shimamoto 	if (!task)
41413d77c37SHiroshi Shimamoto 		return -ESRCH;
4159745512cSArjan van de Ven 	clear_all_latency_tracing(task);
41613d77c37SHiroshi Shimamoto 	put_task_struct(task);
4179745512cSArjan van de Ven 
4189745512cSArjan van de Ven 	return count;
4199745512cSArjan van de Ven }
4209745512cSArjan van de Ven 
4219745512cSArjan van de Ven static const struct file_operations proc_lstats_operations = {
4229745512cSArjan van de Ven 	.open		= lstats_open,
4239745512cSArjan van de Ven 	.read		= seq_read,
4249745512cSArjan van de Ven 	.write		= lstats_write,
4259745512cSArjan van de Ven 	.llseek		= seq_lseek,
42613d77c37SHiroshi Shimamoto 	.release	= single_release,
4279745512cSArjan van de Ven };
4289745512cSArjan van de Ven 
4299745512cSArjan van de Ven #endif
4309745512cSArjan van de Ven 
4311da177e4SLinus Torvalds static int proc_oom_score(struct task_struct *task, char *buffer)
4321da177e4SLinus Torvalds {
433b95c35e7SOleg Nesterov 	unsigned long points = 0;
4341da177e4SLinus Torvalds 
43519c5d45aSAlexey Dobriyan 	read_lock(&tasklist_lock);
436b95c35e7SOleg Nesterov 	if (pid_alive(task))
437a63d83f4SDavid Rientjes 		points = oom_badness(task, NULL, NULL,
438a63d83f4SDavid Rientjes 					totalram_pages + total_swap_pages);
43919c5d45aSAlexey Dobriyan 	read_unlock(&tasklist_lock);
4401da177e4SLinus Torvalds 	return sprintf(buffer, "%lu\n", points);
4411da177e4SLinus Torvalds }
4421da177e4SLinus Torvalds 
443d85f50d5SNeil Horman struct limit_names {
444d85f50d5SNeil Horman 	char *name;
445d85f50d5SNeil Horman 	char *unit;
446d85f50d5SNeil Horman };
447d85f50d5SNeil Horman 
448d85f50d5SNeil Horman static const struct limit_names lnames[RLIM_NLIMITS] = {
449cff4edb5SKees Cook 	[RLIMIT_CPU] = {"Max cpu time", "seconds"},
450d85f50d5SNeil Horman 	[RLIMIT_FSIZE] = {"Max file size", "bytes"},
451d85f50d5SNeil Horman 	[RLIMIT_DATA] = {"Max data size", "bytes"},
452d85f50d5SNeil Horman 	[RLIMIT_STACK] = {"Max stack size", "bytes"},
453d85f50d5SNeil Horman 	[RLIMIT_CORE] = {"Max core file size", "bytes"},
454d85f50d5SNeil Horman 	[RLIMIT_RSS] = {"Max resident set", "bytes"},
455d85f50d5SNeil Horman 	[RLIMIT_NPROC] = {"Max processes", "processes"},
456d85f50d5SNeil Horman 	[RLIMIT_NOFILE] = {"Max open files", "files"},
457d85f50d5SNeil Horman 	[RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
458d85f50d5SNeil Horman 	[RLIMIT_AS] = {"Max address space", "bytes"},
459d85f50d5SNeil Horman 	[RLIMIT_LOCKS] = {"Max file locks", "locks"},
460d85f50d5SNeil Horman 	[RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
461d85f50d5SNeil Horman 	[RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
462d85f50d5SNeil Horman 	[RLIMIT_NICE] = {"Max nice priority", NULL},
463d85f50d5SNeil Horman 	[RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
4648808117cSEugene Teo 	[RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
465d85f50d5SNeil Horman };
466d85f50d5SNeil Horman 
467d85f50d5SNeil Horman /* Display limits for a process */
468d85f50d5SNeil Horman static int proc_pid_limits(struct task_struct *task, char *buffer)
469d85f50d5SNeil Horman {
470d85f50d5SNeil Horman 	unsigned int i;
471d85f50d5SNeil Horman 	int count = 0;
472d85f50d5SNeil Horman 	unsigned long flags;
473d85f50d5SNeil Horman 	char *bufptr = buffer;
474d85f50d5SNeil Horman 
475d85f50d5SNeil Horman 	struct rlimit rlim[RLIM_NLIMITS];
476d85f50d5SNeil Horman 
477a6bebbc8SLai Jiangshan 	if (!lock_task_sighand(task, &flags))
478d85f50d5SNeil Horman 		return 0;
479d85f50d5SNeil Horman 	memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
480d85f50d5SNeil Horman 	unlock_task_sighand(task, &flags);
481d85f50d5SNeil Horman 
482d85f50d5SNeil Horman 	/*
483d85f50d5SNeil Horman 	 * print the file header
484d85f50d5SNeil Horman 	 */
485d85f50d5SNeil Horman 	count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
486d85f50d5SNeil Horman 			"Limit", "Soft Limit", "Hard Limit", "Units");
487d85f50d5SNeil Horman 
488d85f50d5SNeil Horman 	for (i = 0; i < RLIM_NLIMITS; i++) {
489d85f50d5SNeil Horman 		if (rlim[i].rlim_cur == RLIM_INFINITY)
490d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-25s %-20s ",
491d85f50d5SNeil Horman 					 lnames[i].name, "unlimited");
492d85f50d5SNeil Horman 		else
493d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-25s %-20lu ",
494d85f50d5SNeil Horman 					 lnames[i].name, rlim[i].rlim_cur);
495d85f50d5SNeil Horman 
496d85f50d5SNeil Horman 		if (rlim[i].rlim_max == RLIM_INFINITY)
497d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-20s ", "unlimited");
498d85f50d5SNeil Horman 		else
499d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-20lu ",
500d85f50d5SNeil Horman 					 rlim[i].rlim_max);
501d85f50d5SNeil Horman 
502d85f50d5SNeil Horman 		if (lnames[i].unit)
503d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-10s\n",
504d85f50d5SNeil Horman 					 lnames[i].unit);
505d85f50d5SNeil Horman 		else
506d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "\n");
507d85f50d5SNeil Horman 	}
508d85f50d5SNeil Horman 
509d85f50d5SNeil Horman 	return count;
510d85f50d5SNeil Horman }
511d85f50d5SNeil Horman 
512ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
513ebcb6734SRoland McGrath static int proc_pid_syscall(struct task_struct *task, char *buffer)
514ebcb6734SRoland McGrath {
515ebcb6734SRoland McGrath 	long nr;
516ebcb6734SRoland McGrath 	unsigned long args[6], sp, pc;
517a9712bc1SAl Viro 	int res = lock_trace(task);
518a9712bc1SAl Viro 	if (res)
519a9712bc1SAl Viro 		return res;
520ebcb6734SRoland McGrath 
521ebcb6734SRoland McGrath 	if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
522a9712bc1SAl Viro 		res = sprintf(buffer, "running\n");
523a9712bc1SAl Viro 	else if (nr < 0)
524a9712bc1SAl Viro 		res = sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
525a9712bc1SAl Viro 	else
526a9712bc1SAl Viro 		res = sprintf(buffer,
527ebcb6734SRoland McGrath 		       "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
528ebcb6734SRoland McGrath 		       nr,
529ebcb6734SRoland McGrath 		       args[0], args[1], args[2], args[3], args[4], args[5],
530ebcb6734SRoland McGrath 		       sp, pc);
531a9712bc1SAl Viro 	unlock_trace(task);
532a9712bc1SAl Viro 	return res;
533ebcb6734SRoland McGrath }
534ebcb6734SRoland McGrath #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
535ebcb6734SRoland McGrath 
5361da177e4SLinus Torvalds /************************************************************************/
5371da177e4SLinus Torvalds /*                       Here the fs part begins                        */
5381da177e4SLinus Torvalds /************************************************************************/
5391da177e4SLinus Torvalds 
5401da177e4SLinus Torvalds /* permission checks */
541778c1144SEric W. Biederman static int proc_fd_access_allowed(struct inode *inode)
5421da177e4SLinus Torvalds {
543778c1144SEric W. Biederman 	struct task_struct *task;
544778c1144SEric W. Biederman 	int allowed = 0;
545df26c40eSEric W. Biederman 	/* Allow access to a task's file descriptors if it is us or we
546df26c40eSEric W. Biederman 	 * may use ptrace attach to the process and find out that
547df26c40eSEric W. Biederman 	 * information.
548778c1144SEric W. Biederman 	 */
549778c1144SEric W. Biederman 	task = get_proc_task(inode);
550df26c40eSEric W. Biederman 	if (task) {
551006ebb40SStephen Smalley 		allowed = ptrace_may_access(task, PTRACE_MODE_READ);
552778c1144SEric W. Biederman 		put_task_struct(task);
553df26c40eSEric W. Biederman 	}
554778c1144SEric W. Biederman 	return allowed;
5551da177e4SLinus Torvalds }
5561da177e4SLinus Torvalds 
5576b4e306aSEric W. Biederman int proc_setattr(struct dentry *dentry, struct iattr *attr)
5586d76fa58SLinus Torvalds {
5596d76fa58SLinus Torvalds 	int error;
5606d76fa58SLinus Torvalds 	struct inode *inode = dentry->d_inode;
5616d76fa58SLinus Torvalds 
5626d76fa58SLinus Torvalds 	if (attr->ia_valid & ATTR_MODE)
5636d76fa58SLinus Torvalds 		return -EPERM;
5646d76fa58SLinus Torvalds 
5656d76fa58SLinus Torvalds 	error = inode_change_ok(inode, attr);
5661025774cSChristoph Hellwig 	if (error)
5676d76fa58SLinus Torvalds 		return error;
5681025774cSChristoph Hellwig 
5691025774cSChristoph Hellwig 	if ((attr->ia_valid & ATTR_SIZE) &&
5701025774cSChristoph Hellwig 	    attr->ia_size != i_size_read(inode)) {
5711025774cSChristoph Hellwig 		error = vmtruncate(inode, attr->ia_size);
5721025774cSChristoph Hellwig 		if (error)
5731025774cSChristoph Hellwig 			return error;
5741025774cSChristoph Hellwig 	}
5751025774cSChristoph Hellwig 
5761025774cSChristoph Hellwig 	setattr_copy(inode, attr);
5771025774cSChristoph Hellwig 	mark_inode_dirty(inode);
5781025774cSChristoph Hellwig 	return 0;
5796d76fa58SLinus Torvalds }
5806d76fa58SLinus Torvalds 
5810499680aSVasiliy Kulikov /*
5820499680aSVasiliy Kulikov  * May current process learn task's sched/cmdline info (for hide_pid_min=1)
5830499680aSVasiliy Kulikov  * or euid/egid (for hide_pid_min=2)?
5840499680aSVasiliy Kulikov  */
5850499680aSVasiliy Kulikov static bool has_pid_permissions(struct pid_namespace *pid,
5860499680aSVasiliy Kulikov 				 struct task_struct *task,
5870499680aSVasiliy Kulikov 				 int hide_pid_min)
5880499680aSVasiliy Kulikov {
5890499680aSVasiliy Kulikov 	if (pid->hide_pid < hide_pid_min)
5900499680aSVasiliy Kulikov 		return true;
5910499680aSVasiliy Kulikov 	if (in_group_p(pid->pid_gid))
5920499680aSVasiliy Kulikov 		return true;
5930499680aSVasiliy Kulikov 	return ptrace_may_access(task, PTRACE_MODE_READ);
5940499680aSVasiliy Kulikov }
5950499680aSVasiliy Kulikov 
5960499680aSVasiliy Kulikov 
5970499680aSVasiliy Kulikov static int proc_pid_permission(struct inode *inode, int mask)
5980499680aSVasiliy Kulikov {
5990499680aSVasiliy Kulikov 	struct pid_namespace *pid = inode->i_sb->s_fs_info;
6000499680aSVasiliy Kulikov 	struct task_struct *task;
6010499680aSVasiliy Kulikov 	bool has_perms;
6020499680aSVasiliy Kulikov 
6030499680aSVasiliy Kulikov 	task = get_proc_task(inode);
604a2ef990aSXiaotian Feng 	if (!task)
605a2ef990aSXiaotian Feng 		return -ESRCH;
6060499680aSVasiliy Kulikov 	has_perms = has_pid_permissions(pid, task, 1);
6070499680aSVasiliy Kulikov 	put_task_struct(task);
6080499680aSVasiliy Kulikov 
6090499680aSVasiliy Kulikov 	if (!has_perms) {
6100499680aSVasiliy Kulikov 		if (pid->hide_pid == 2) {
6110499680aSVasiliy Kulikov 			/*
6120499680aSVasiliy Kulikov 			 * Let's make getdents(), stat(), and open()
6130499680aSVasiliy Kulikov 			 * consistent with each other.  If a process
6140499680aSVasiliy Kulikov 			 * may not stat() a file, it shouldn't be seen
6150499680aSVasiliy Kulikov 			 * in procfs at all.
6160499680aSVasiliy Kulikov 			 */
6170499680aSVasiliy Kulikov 			return -ENOENT;
6180499680aSVasiliy Kulikov 		}
6190499680aSVasiliy Kulikov 
6200499680aSVasiliy Kulikov 		return -EPERM;
6210499680aSVasiliy Kulikov 	}
6220499680aSVasiliy Kulikov 	return generic_permission(inode, mask);
6230499680aSVasiliy Kulikov }
6240499680aSVasiliy Kulikov 
6250499680aSVasiliy Kulikov 
6260499680aSVasiliy Kulikov 
627c5ef1c42SArjan van de Ven static const struct inode_operations proc_def_inode_operations = {
6286d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
6296d76fa58SLinus Torvalds };
6306d76fa58SLinus Torvalds 
6311da177e4SLinus Torvalds #define PROC_BLOCK_SIZE	(3*1024)		/* 4K page size but our output routines use some slack for overruns */
6321da177e4SLinus Torvalds 
6331da177e4SLinus Torvalds static ssize_t proc_info_read(struct file * file, char __user * buf,
6341da177e4SLinus Torvalds 			  size_t count, loff_t *ppos)
6351da177e4SLinus Torvalds {
6362fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
6371da177e4SLinus Torvalds 	unsigned long page;
6381da177e4SLinus Torvalds 	ssize_t length;
63999f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
64099f89551SEric W. Biederman 
64199f89551SEric W. Biederman 	length = -ESRCH;
64299f89551SEric W. Biederman 	if (!task)
64399f89551SEric W. Biederman 		goto out_no_task;
6441da177e4SLinus Torvalds 
6451da177e4SLinus Torvalds 	if (count > PROC_BLOCK_SIZE)
6461da177e4SLinus Torvalds 		count = PROC_BLOCK_SIZE;
64799f89551SEric W. Biederman 
64899f89551SEric W. Biederman 	length = -ENOMEM;
649e12ba74dSMel Gorman 	if (!(page = __get_free_page(GFP_TEMPORARY)))
65099f89551SEric W. Biederman 		goto out;
6511da177e4SLinus Torvalds 
6521da177e4SLinus Torvalds 	length = PROC_I(inode)->op.proc_read(task, (char*)page);
6531da177e4SLinus Torvalds 
6541da177e4SLinus Torvalds 	if (length >= 0)
6551da177e4SLinus Torvalds 		length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
6561da177e4SLinus Torvalds 	free_page(page);
65799f89551SEric W. Biederman out:
65899f89551SEric W. Biederman 	put_task_struct(task);
65999f89551SEric W. Biederman out_no_task:
6601da177e4SLinus Torvalds 	return length;
6611da177e4SLinus Torvalds }
6621da177e4SLinus Torvalds 
66300977a59SArjan van de Ven static const struct file_operations proc_info_file_operations = {
6641da177e4SLinus Torvalds 	.read		= proc_info_read,
66587df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
6661da177e4SLinus Torvalds };
6671da177e4SLinus Torvalds 
668be614086SEric W. Biederman static int proc_single_show(struct seq_file *m, void *v)
669be614086SEric W. Biederman {
670be614086SEric W. Biederman 	struct inode *inode = m->private;
671be614086SEric W. Biederman 	struct pid_namespace *ns;
672be614086SEric W. Biederman 	struct pid *pid;
673be614086SEric W. Biederman 	struct task_struct *task;
674be614086SEric W. Biederman 	int ret;
675be614086SEric W. Biederman 
676be614086SEric W. Biederman 	ns = inode->i_sb->s_fs_info;
677be614086SEric W. Biederman 	pid = proc_pid(inode);
678be614086SEric W. Biederman 	task = get_pid_task(pid, PIDTYPE_PID);
679be614086SEric W. Biederman 	if (!task)
680be614086SEric W. Biederman 		return -ESRCH;
681be614086SEric W. Biederman 
682be614086SEric W. Biederman 	ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
683be614086SEric W. Biederman 
684be614086SEric W. Biederman 	put_task_struct(task);
685be614086SEric W. Biederman 	return ret;
686be614086SEric W. Biederman }
687be614086SEric W. Biederman 
688be614086SEric W. Biederman static int proc_single_open(struct inode *inode, struct file *filp)
689be614086SEric W. Biederman {
690c6a34058SJovi Zhang 	return single_open(filp, proc_single_show, inode);
691be614086SEric W. Biederman }
692be614086SEric W. Biederman 
693be614086SEric W. Biederman static const struct file_operations proc_single_file_operations = {
694be614086SEric W. Biederman 	.open		= proc_single_open,
695be614086SEric W. Biederman 	.read		= seq_read,
696be614086SEric W. Biederman 	.llseek		= seq_lseek,
697be614086SEric W. Biederman 	.release	= single_release,
698be614086SEric W. Biederman };
699be614086SEric W. Biederman 
7001da177e4SLinus Torvalds static int mem_open(struct inode* inode, struct file* file)
7011da177e4SLinus Torvalds {
702e268337dSLinus Torvalds 	struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
703e268337dSLinus Torvalds 	struct mm_struct *mm;
704e268337dSLinus Torvalds 
705e268337dSLinus Torvalds 	if (!task)
706e268337dSLinus Torvalds 		return -ESRCH;
707e268337dSLinus Torvalds 
708e268337dSLinus Torvalds 	mm = mm_access(task, PTRACE_MODE_ATTACH);
709e268337dSLinus Torvalds 	put_task_struct(task);
710e268337dSLinus Torvalds 
711e268337dSLinus Torvalds 	if (IS_ERR(mm))
712e268337dSLinus Torvalds 		return PTR_ERR(mm);
713e268337dSLinus Torvalds 
7144a3956c7SKAMEZAWA Hiroyuki 	/* OK to pass negative loff_t, we can catch out-of-range */
7154a3956c7SKAMEZAWA Hiroyuki 	file->f_mode |= FMODE_UNSIGNED_OFFSET;
716e268337dSLinus Torvalds 	file->private_data = mm;
717e268337dSLinus Torvalds 
7181da177e4SLinus Torvalds 	return 0;
7191da177e4SLinus Torvalds }
7201da177e4SLinus Torvalds 
721*572d34b9SOleg Nesterov static ssize_t mem_rw(struct file *file, char __user *buf,
722*572d34b9SOleg Nesterov 			size_t count, loff_t *ppos, int write)
7231da177e4SLinus Torvalds {
724e268337dSLinus Torvalds 	struct mm_struct *mm = file->private_data;
725*572d34b9SOleg Nesterov 	unsigned long addr = *ppos;
726*572d34b9SOleg Nesterov 	ssize_t copied;
7271da177e4SLinus Torvalds 	char *page;
7281da177e4SLinus Torvalds 
729e268337dSLinus Torvalds 	if (!mm)
730e268337dSLinus Torvalds 		return 0;
7311da177e4SLinus Torvalds 
732e12ba74dSMel Gorman 	page = (char *)__get_free_page(GFP_TEMPORARY);
7331da177e4SLinus Torvalds 	if (!page)
734e268337dSLinus Torvalds 		return -ENOMEM;
7351da177e4SLinus Torvalds 
736f7ca54f4SFrederik Deweerdt 	copied = 0;
7371da177e4SLinus Torvalds 	while (count > 0) {
738*572d34b9SOleg Nesterov 		int this_len = min_t(int, count, PAGE_SIZE);
7391da177e4SLinus Torvalds 
740*572d34b9SOleg Nesterov 		if (write && copy_from_user(page, buf, this_len)) {
7411da177e4SLinus Torvalds 			copied = -EFAULT;
7421da177e4SLinus Torvalds 			break;
7431da177e4SLinus Torvalds 		}
744*572d34b9SOleg Nesterov 
745*572d34b9SOleg Nesterov 		this_len = access_remote_vm(mm, addr, page, this_len, write);
746*572d34b9SOleg Nesterov 		if (!this_len) {
7471da177e4SLinus Torvalds 			if (!copied)
7481da177e4SLinus Torvalds 				copied = -EIO;
7491da177e4SLinus Torvalds 			break;
7501da177e4SLinus Torvalds 		}
751*572d34b9SOleg Nesterov 
752*572d34b9SOleg Nesterov 		if (!write && copy_to_user(buf, page, this_len)) {
753*572d34b9SOleg Nesterov 			copied = -EFAULT;
754*572d34b9SOleg Nesterov 			break;
7551da177e4SLinus Torvalds 		}
756*572d34b9SOleg Nesterov 
757*572d34b9SOleg Nesterov 		buf += this_len;
758*572d34b9SOleg Nesterov 		addr += this_len;
759*572d34b9SOleg Nesterov 		copied += this_len;
760*572d34b9SOleg Nesterov 		count -= this_len;
761*572d34b9SOleg Nesterov 	}
762*572d34b9SOleg Nesterov 	*ppos = addr;
76330cd8903SKOSAKI Motohiro 
76430cd8903SKOSAKI Motohiro 	free_page((unsigned long) page);
7651da177e4SLinus Torvalds 	return copied;
7661da177e4SLinus Torvalds }
7671da177e4SLinus Torvalds 
768*572d34b9SOleg Nesterov static ssize_t mem_read(struct file *file, char __user *buf,
769*572d34b9SOleg Nesterov 			size_t count, loff_t *ppos)
770*572d34b9SOleg Nesterov {
771*572d34b9SOleg Nesterov 	return mem_rw(file, buf, count, ppos, 0);
772*572d34b9SOleg Nesterov }
773*572d34b9SOleg Nesterov 
774*572d34b9SOleg Nesterov static ssize_t mem_write(struct file *file, const char __user *buf,
775*572d34b9SOleg Nesterov 			 size_t count, loff_t *ppos)
776*572d34b9SOleg Nesterov {
777*572d34b9SOleg Nesterov 	return mem_rw(file, (char __user*)buf, count, ppos, 1);
778*572d34b9SOleg Nesterov }
779*572d34b9SOleg Nesterov 
78085863e47SMatt Mackall loff_t mem_lseek(struct file *file, loff_t offset, int orig)
7811da177e4SLinus Torvalds {
7821da177e4SLinus Torvalds 	switch (orig) {
7831da177e4SLinus Torvalds 	case 0:
7841da177e4SLinus Torvalds 		file->f_pos = offset;
7851da177e4SLinus Torvalds 		break;
7861da177e4SLinus Torvalds 	case 1:
7871da177e4SLinus Torvalds 		file->f_pos += offset;
7881da177e4SLinus Torvalds 		break;
7891da177e4SLinus Torvalds 	default:
7901da177e4SLinus Torvalds 		return -EINVAL;
7911da177e4SLinus Torvalds 	}
7921da177e4SLinus Torvalds 	force_successful_syscall_return();
7931da177e4SLinus Torvalds 	return file->f_pos;
7941da177e4SLinus Torvalds }
7951da177e4SLinus Torvalds 
796e268337dSLinus Torvalds static int mem_release(struct inode *inode, struct file *file)
797e268337dSLinus Torvalds {
798e268337dSLinus Torvalds 	struct mm_struct *mm = file->private_data;
79971879d3cSOleg Nesterov 	if (mm)
800e268337dSLinus Torvalds 		mmput(mm);
801e268337dSLinus Torvalds 	return 0;
802e268337dSLinus Torvalds }
803e268337dSLinus Torvalds 
80400977a59SArjan van de Ven static const struct file_operations proc_mem_operations = {
8051da177e4SLinus Torvalds 	.llseek		= mem_lseek,
8061da177e4SLinus Torvalds 	.read		= mem_read,
8071da177e4SLinus Torvalds 	.write		= mem_write,
8081da177e4SLinus Torvalds 	.open		= mem_open,
809e268337dSLinus Torvalds 	.release	= mem_release,
8101da177e4SLinus Torvalds };
8111da177e4SLinus Torvalds 
812315e28c8SJames Pearson static ssize_t environ_read(struct file *file, char __user *buf,
813315e28c8SJames Pearson 			size_t count, loff_t *ppos)
814315e28c8SJames Pearson {
815315e28c8SJames Pearson 	struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
816315e28c8SJames Pearson 	char *page;
817315e28c8SJames Pearson 	unsigned long src = *ppos;
818315e28c8SJames Pearson 	int ret = -ESRCH;
819315e28c8SJames Pearson 	struct mm_struct *mm;
820315e28c8SJames Pearson 
821315e28c8SJames Pearson 	if (!task)
822315e28c8SJames Pearson 		goto out_no_task;
823315e28c8SJames Pearson 
824315e28c8SJames Pearson 	ret = -ENOMEM;
825315e28c8SJames Pearson 	page = (char *)__get_free_page(GFP_TEMPORARY);
826315e28c8SJames Pearson 	if (!page)
827315e28c8SJames Pearson 		goto out;
828315e28c8SJames Pearson 
829315e28c8SJames Pearson 
830d6f64b89SAl Viro 	mm = mm_for_maps(task);
831d6f64b89SAl Viro 	ret = PTR_ERR(mm);
832d6f64b89SAl Viro 	if (!mm || IS_ERR(mm))
833315e28c8SJames Pearson 		goto out_free;
834315e28c8SJames Pearson 
835d6f64b89SAl Viro 	ret = 0;
836315e28c8SJames Pearson 	while (count > 0) {
837315e28c8SJames Pearson 		int this_len, retval, max_len;
838315e28c8SJames Pearson 
839315e28c8SJames Pearson 		this_len = mm->env_end - (mm->env_start + src);
840315e28c8SJames Pearson 
841315e28c8SJames Pearson 		if (this_len <= 0)
842315e28c8SJames Pearson 			break;
843315e28c8SJames Pearson 
844315e28c8SJames Pearson 		max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
845315e28c8SJames Pearson 		this_len = (this_len > max_len) ? max_len : this_len;
846315e28c8SJames Pearson 
847315e28c8SJames Pearson 		retval = access_process_vm(task, (mm->env_start + src),
848315e28c8SJames Pearson 			page, this_len, 0);
849315e28c8SJames Pearson 
850315e28c8SJames Pearson 		if (retval <= 0) {
851315e28c8SJames Pearson 			ret = retval;
852315e28c8SJames Pearson 			break;
853315e28c8SJames Pearson 		}
854315e28c8SJames Pearson 
855315e28c8SJames Pearson 		if (copy_to_user(buf, page, retval)) {
856315e28c8SJames Pearson 			ret = -EFAULT;
857315e28c8SJames Pearson 			break;
858315e28c8SJames Pearson 		}
859315e28c8SJames Pearson 
860315e28c8SJames Pearson 		ret += retval;
861315e28c8SJames Pearson 		src += retval;
862315e28c8SJames Pearson 		buf += retval;
863315e28c8SJames Pearson 		count -= retval;
864315e28c8SJames Pearson 	}
865315e28c8SJames Pearson 	*ppos = src;
866315e28c8SJames Pearson 
867315e28c8SJames Pearson 	mmput(mm);
868315e28c8SJames Pearson out_free:
869315e28c8SJames Pearson 	free_page((unsigned long) page);
870315e28c8SJames Pearson out:
871315e28c8SJames Pearson 	put_task_struct(task);
872315e28c8SJames Pearson out_no_task:
873315e28c8SJames Pearson 	return ret;
874315e28c8SJames Pearson }
875315e28c8SJames Pearson 
876315e28c8SJames Pearson static const struct file_operations proc_environ_operations = {
877315e28c8SJames Pearson 	.read		= environ_read,
87887df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
879315e28c8SJames Pearson };
880315e28c8SJames Pearson 
8811da177e4SLinus Torvalds static ssize_t oom_adjust_read(struct file *file, char __user *buf,
8821da177e4SLinus Torvalds 				size_t count, loff_t *ppos)
8831da177e4SLinus Torvalds {
8842fddfeefSJosef "Jeff" Sipek 	struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
8858578cea7SEric W. Biederman 	char buffer[PROC_NUMBUF];
8861da177e4SLinus Torvalds 	size_t len;
88728b83c51SKOSAKI Motohiro 	int oom_adjust = OOM_DISABLE;
88828b83c51SKOSAKI Motohiro 	unsigned long flags;
8891da177e4SLinus Torvalds 
89099f89551SEric W. Biederman 	if (!task)
89199f89551SEric W. Biederman 		return -ESRCH;
89228b83c51SKOSAKI Motohiro 
89328b83c51SKOSAKI Motohiro 	if (lock_task_sighand(task, &flags)) {
89428b83c51SKOSAKI Motohiro 		oom_adjust = task->signal->oom_adj;
89528b83c51SKOSAKI Motohiro 		unlock_task_sighand(task, &flags);
89628b83c51SKOSAKI Motohiro 	}
89728b83c51SKOSAKI Motohiro 
89899f89551SEric W. Biederman 	put_task_struct(task);
89999f89551SEric W. Biederman 
9008578cea7SEric W. Biederman 	len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
9010c28f287SAkinobu Mita 
9020c28f287SAkinobu Mita 	return simple_read_from_buffer(buf, count, ppos, buffer, len);
9031da177e4SLinus Torvalds }
9041da177e4SLinus Torvalds 
9051da177e4SLinus Torvalds static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
9061da177e4SLinus Torvalds 				size_t count, loff_t *ppos)
9071da177e4SLinus Torvalds {
90899f89551SEric W. Biederman 	struct task_struct *task;
9095d863b89SKOSAKI Motohiro 	char buffer[PROC_NUMBUF];
9100a8cb8e3SAlexey Dobriyan 	int oom_adjust;
91128b83c51SKOSAKI Motohiro 	unsigned long flags;
9125d863b89SKOSAKI Motohiro 	int err;
9131da177e4SLinus Torvalds 
9148578cea7SEric W. Biederman 	memset(buffer, 0, sizeof(buffer));
9158578cea7SEric W. Biederman 	if (count > sizeof(buffer) - 1)
9168578cea7SEric W. Biederman 		count = sizeof(buffer) - 1;
917723548bfSDavid Rientjes 	if (copy_from_user(buffer, buf, count)) {
918723548bfSDavid Rientjes 		err = -EFAULT;
919723548bfSDavid Rientjes 		goto out;
920723548bfSDavid Rientjes 	}
9215d863b89SKOSAKI Motohiro 
9220a8cb8e3SAlexey Dobriyan 	err = kstrtoint(strstrip(buffer), 0, &oom_adjust);
9235d863b89SKOSAKI Motohiro 	if (err)
924723548bfSDavid Rientjes 		goto out;
9258ac773b4SAlexey Dobriyan 	if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
926723548bfSDavid Rientjes 	     oom_adjust != OOM_DISABLE) {
927723548bfSDavid Rientjes 		err = -EINVAL;
928723548bfSDavid Rientjes 		goto out;
929723548bfSDavid Rientjes 	}
9305d863b89SKOSAKI Motohiro 
9312fddfeefSJosef "Jeff" Sipek 	task = get_proc_task(file->f_path.dentry->d_inode);
932723548bfSDavid Rientjes 	if (!task) {
933723548bfSDavid Rientjes 		err = -ESRCH;
934723548bfSDavid Rientjes 		goto out;
935723548bfSDavid Rientjes 	}
93628b83c51SKOSAKI Motohiro 
9373d5992d2SYing Han 	task_lock(task);
9383d5992d2SYing Han 	if (!task->mm) {
939723548bfSDavid Rientjes 		err = -EINVAL;
940723548bfSDavid Rientjes 		goto err_task_lock;
9413d5992d2SYing Han 	}
9423d5992d2SYing Han 
943d19d5476SDavid Rientjes 	if (!lock_task_sighand(task, &flags)) {
944d19d5476SDavid Rientjes 		err = -ESRCH;
945d19d5476SDavid Rientjes 		goto err_task_lock;
946d19d5476SDavid Rientjes 	}
947d19d5476SDavid Rientjes 
948d19d5476SDavid Rientjes 	if (oom_adjust < task->signal->oom_adj && !capable(CAP_SYS_RESOURCE)) {
949d19d5476SDavid Rientjes 		err = -EACCES;
950d19d5476SDavid Rientjes 		goto err_sighand;
951d19d5476SDavid Rientjes 	}
952d19d5476SDavid Rientjes 
95351b1bd2aSDavid Rientjes 	/*
95451b1bd2aSDavid Rientjes 	 * Warn that /proc/pid/oom_adj is deprecated, see
95551b1bd2aSDavid Rientjes 	 * Documentation/feature-removal-schedule.txt.
95651b1bd2aSDavid Rientjes 	 */
957c2142704SLinus Torvalds 	printk_once(KERN_WARNING "%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
958be8f684dSDavid Rientjes 		  current->comm, task_pid_nr(current), task_pid_nr(task),
959be8f684dSDavid Rientjes 		  task_pid_nr(task));
96028b83c51SKOSAKI Motohiro 	task->signal->oom_adj = oom_adjust;
961a63d83f4SDavid Rientjes 	/*
962a63d83f4SDavid Rientjes 	 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
963a63d83f4SDavid Rientjes 	 * value is always attainable.
964a63d83f4SDavid Rientjes 	 */
965a63d83f4SDavid Rientjes 	if (task->signal->oom_adj == OOM_ADJUST_MAX)
966a63d83f4SDavid Rientjes 		task->signal->oom_score_adj = OOM_SCORE_ADJ_MAX;
967a63d83f4SDavid Rientjes 	else
968a63d83f4SDavid Rientjes 		task->signal->oom_score_adj = (oom_adjust * OOM_SCORE_ADJ_MAX) /
969a63d83f4SDavid Rientjes 								-OOM_DISABLE;
97043d2b113SKAMEZAWA Hiroyuki 	trace_oom_score_adj_update(task);
971723548bfSDavid Rientjes err_sighand:
97228b83c51SKOSAKI Motohiro 	unlock_task_sighand(task, &flags);
973d19d5476SDavid Rientjes err_task_lock:
974d19d5476SDavid Rientjes 	task_unlock(task);
97599f89551SEric W. Biederman 	put_task_struct(task);
976723548bfSDavid Rientjes out:
977723548bfSDavid Rientjes 	return err < 0 ? err : count;
9781da177e4SLinus Torvalds }
9791da177e4SLinus Torvalds 
98000977a59SArjan van de Ven static const struct file_operations proc_oom_adjust_operations = {
9811da177e4SLinus Torvalds 	.read		= oom_adjust_read,
9821da177e4SLinus Torvalds 	.write		= oom_adjust_write,
98387df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
9841da177e4SLinus Torvalds };
9851da177e4SLinus Torvalds 
986a63d83f4SDavid Rientjes static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
987a63d83f4SDavid Rientjes 					size_t count, loff_t *ppos)
988a63d83f4SDavid Rientjes {
989a63d83f4SDavid Rientjes 	struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
990a63d83f4SDavid Rientjes 	char buffer[PROC_NUMBUF];
991a63d83f4SDavid Rientjes 	int oom_score_adj = OOM_SCORE_ADJ_MIN;
992a63d83f4SDavid Rientjes 	unsigned long flags;
993a63d83f4SDavid Rientjes 	size_t len;
994a63d83f4SDavid Rientjes 
995a63d83f4SDavid Rientjes 	if (!task)
996a63d83f4SDavid Rientjes 		return -ESRCH;
997a63d83f4SDavid Rientjes 	if (lock_task_sighand(task, &flags)) {
998a63d83f4SDavid Rientjes 		oom_score_adj = task->signal->oom_score_adj;
999a63d83f4SDavid Rientjes 		unlock_task_sighand(task, &flags);
1000a63d83f4SDavid Rientjes 	}
1001a63d83f4SDavid Rientjes 	put_task_struct(task);
1002a63d83f4SDavid Rientjes 	len = snprintf(buffer, sizeof(buffer), "%d\n", oom_score_adj);
1003a63d83f4SDavid Rientjes 	return simple_read_from_buffer(buf, count, ppos, buffer, len);
1004a63d83f4SDavid Rientjes }
1005a63d83f4SDavid Rientjes 
1006a63d83f4SDavid Rientjes static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1007a63d83f4SDavid Rientjes 					size_t count, loff_t *ppos)
1008a63d83f4SDavid Rientjes {
1009a63d83f4SDavid Rientjes 	struct task_struct *task;
1010a63d83f4SDavid Rientjes 	char buffer[PROC_NUMBUF];
1011a63d83f4SDavid Rientjes 	unsigned long flags;
10120a8cb8e3SAlexey Dobriyan 	int oom_score_adj;
1013a63d83f4SDavid Rientjes 	int err;
1014a63d83f4SDavid Rientjes 
1015a63d83f4SDavid Rientjes 	memset(buffer, 0, sizeof(buffer));
1016a63d83f4SDavid Rientjes 	if (count > sizeof(buffer) - 1)
1017a63d83f4SDavid Rientjes 		count = sizeof(buffer) - 1;
1018723548bfSDavid Rientjes 	if (copy_from_user(buffer, buf, count)) {
1019723548bfSDavid Rientjes 		err = -EFAULT;
1020723548bfSDavid Rientjes 		goto out;
1021723548bfSDavid Rientjes 	}
1022a63d83f4SDavid Rientjes 
10230a8cb8e3SAlexey Dobriyan 	err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
1024a63d83f4SDavid Rientjes 	if (err)
1025723548bfSDavid Rientjes 		goto out;
1026a63d83f4SDavid Rientjes 	if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
1027723548bfSDavid Rientjes 			oom_score_adj > OOM_SCORE_ADJ_MAX) {
1028723548bfSDavid Rientjes 		err = -EINVAL;
1029723548bfSDavid Rientjes 		goto out;
1030723548bfSDavid Rientjes 	}
1031a63d83f4SDavid Rientjes 
1032a63d83f4SDavid Rientjes 	task = get_proc_task(file->f_path.dentry->d_inode);
1033723548bfSDavid Rientjes 	if (!task) {
1034723548bfSDavid Rientjes 		err = -ESRCH;
1035723548bfSDavid Rientjes 		goto out;
1036723548bfSDavid Rientjes 	}
1037a63d83f4SDavid Rientjes 
10383d5992d2SYing Han 	task_lock(task);
10393d5992d2SYing Han 	if (!task->mm) {
1040723548bfSDavid Rientjes 		err = -EINVAL;
1041723548bfSDavid Rientjes 		goto err_task_lock;
10423d5992d2SYing Han 	}
1043d19d5476SDavid Rientjes 
1044d19d5476SDavid Rientjes 	if (!lock_task_sighand(task, &flags)) {
1045d19d5476SDavid Rientjes 		err = -ESRCH;
1046d19d5476SDavid Rientjes 		goto err_task_lock;
1047d19d5476SDavid Rientjes 	}
1048d19d5476SDavid Rientjes 
1049dabb16f6SMandeep Singh Baines 	if (oom_score_adj < task->signal->oom_score_adj_min &&
1050d19d5476SDavid Rientjes 			!capable(CAP_SYS_RESOURCE)) {
1051d19d5476SDavid Rientjes 		err = -EACCES;
1052d19d5476SDavid Rientjes 		goto err_sighand;
1053d19d5476SDavid Rientjes 	}
1054d19d5476SDavid Rientjes 
1055a63d83f4SDavid Rientjes 	task->signal->oom_score_adj = oom_score_adj;
1056dabb16f6SMandeep Singh Baines 	if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
1057dabb16f6SMandeep Singh Baines 		task->signal->oom_score_adj_min = oom_score_adj;
105843d2b113SKAMEZAWA Hiroyuki 	trace_oom_score_adj_update(task);
1059a63d83f4SDavid Rientjes 	/*
1060a63d83f4SDavid Rientjes 	 * Scale /proc/pid/oom_adj appropriately ensuring that OOM_DISABLE is
1061a63d83f4SDavid Rientjes 	 * always attainable.
1062a63d83f4SDavid Rientjes 	 */
1063a63d83f4SDavid Rientjes 	if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MIN)
1064a63d83f4SDavid Rientjes 		task->signal->oom_adj = OOM_DISABLE;
1065a63d83f4SDavid Rientjes 	else
1066a63d83f4SDavid Rientjes 		task->signal->oom_adj = (oom_score_adj * OOM_ADJUST_MAX) /
1067a63d83f4SDavid Rientjes 							OOM_SCORE_ADJ_MAX;
1068723548bfSDavid Rientjes err_sighand:
1069a63d83f4SDavid Rientjes 	unlock_task_sighand(task, &flags);
1070d19d5476SDavid Rientjes err_task_lock:
1071d19d5476SDavid Rientjes 	task_unlock(task);
1072a63d83f4SDavid Rientjes 	put_task_struct(task);
1073723548bfSDavid Rientjes out:
1074723548bfSDavid Rientjes 	return err < 0 ? err : count;
1075a63d83f4SDavid Rientjes }
1076a63d83f4SDavid Rientjes 
1077a63d83f4SDavid Rientjes static const struct file_operations proc_oom_score_adj_operations = {
1078a63d83f4SDavid Rientjes 	.read		= oom_score_adj_read,
1079a63d83f4SDavid Rientjes 	.write		= oom_score_adj_write,
10806038f373SArnd Bergmann 	.llseek		= default_llseek,
1081a63d83f4SDavid Rientjes };
1082a63d83f4SDavid Rientjes 
10831da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL
10841da177e4SLinus Torvalds #define TMPBUFLEN 21
10851da177e4SLinus Torvalds static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
10861da177e4SLinus Torvalds 				  size_t count, loff_t *ppos)
10871da177e4SLinus Torvalds {
10882fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
108999f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
10901da177e4SLinus Torvalds 	ssize_t length;
10911da177e4SLinus Torvalds 	char tmpbuf[TMPBUFLEN];
10921da177e4SLinus Torvalds 
109399f89551SEric W. Biederman 	if (!task)
109499f89551SEric W. Biederman 		return -ESRCH;
10951da177e4SLinus Torvalds 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
10960c11b942SAl Viro 				audit_get_loginuid(task));
109799f89551SEric W. Biederman 	put_task_struct(task);
10981da177e4SLinus Torvalds 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
10991da177e4SLinus Torvalds }
11001da177e4SLinus Torvalds 
11011da177e4SLinus Torvalds static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
11021da177e4SLinus Torvalds 				   size_t count, loff_t *ppos)
11031da177e4SLinus Torvalds {
11042fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
11051da177e4SLinus Torvalds 	char *page, *tmp;
11061da177e4SLinus Torvalds 	ssize_t length;
11071da177e4SLinus Torvalds 	uid_t loginuid;
11081da177e4SLinus Torvalds 
11097dc52157SPaul E. McKenney 	rcu_read_lock();
11107dc52157SPaul E. McKenney 	if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
11117dc52157SPaul E. McKenney 		rcu_read_unlock();
11121da177e4SLinus Torvalds 		return -EPERM;
11137dc52157SPaul E. McKenney 	}
11147dc52157SPaul E. McKenney 	rcu_read_unlock();
11151da177e4SLinus Torvalds 
1116e0182909SAl Viro 	if (count >= PAGE_SIZE)
1117e0182909SAl Viro 		count = PAGE_SIZE - 1;
11181da177e4SLinus Torvalds 
11191da177e4SLinus Torvalds 	if (*ppos != 0) {
11201da177e4SLinus Torvalds 		/* No partial writes. */
11211da177e4SLinus Torvalds 		return -EINVAL;
11221da177e4SLinus Torvalds 	}
1123e12ba74dSMel Gorman 	page = (char*)__get_free_page(GFP_TEMPORARY);
11241da177e4SLinus Torvalds 	if (!page)
11251da177e4SLinus Torvalds 		return -ENOMEM;
11261da177e4SLinus Torvalds 	length = -EFAULT;
11271da177e4SLinus Torvalds 	if (copy_from_user(page, buf, count))
11281da177e4SLinus Torvalds 		goto out_free_page;
11291da177e4SLinus Torvalds 
1130e0182909SAl Viro 	page[count] = '\0';
11311da177e4SLinus Torvalds 	loginuid = simple_strtoul(page, &tmp, 10);
11321da177e4SLinus Torvalds 	if (tmp == page) {
11331da177e4SLinus Torvalds 		length = -EINVAL;
11341da177e4SLinus Torvalds 		goto out_free_page;
11351da177e4SLinus Torvalds 
11361da177e4SLinus Torvalds 	}
11370a300be6SEric Paris 	length = audit_set_loginuid(loginuid);
11381da177e4SLinus Torvalds 	if (likely(length == 0))
11391da177e4SLinus Torvalds 		length = count;
11401da177e4SLinus Torvalds 
11411da177e4SLinus Torvalds out_free_page:
11421da177e4SLinus Torvalds 	free_page((unsigned long) page);
11431da177e4SLinus Torvalds 	return length;
11441da177e4SLinus Torvalds }
11451da177e4SLinus Torvalds 
114600977a59SArjan van de Ven static const struct file_operations proc_loginuid_operations = {
11471da177e4SLinus Torvalds 	.read		= proc_loginuid_read,
11481da177e4SLinus Torvalds 	.write		= proc_loginuid_write,
114987df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
11501da177e4SLinus Torvalds };
11511e0bd755SEric Paris 
11521e0bd755SEric Paris static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
11531e0bd755SEric Paris 				  size_t count, loff_t *ppos)
11541e0bd755SEric Paris {
11551e0bd755SEric Paris 	struct inode * inode = file->f_path.dentry->d_inode;
11561e0bd755SEric Paris 	struct task_struct *task = get_proc_task(inode);
11571e0bd755SEric Paris 	ssize_t length;
11581e0bd755SEric Paris 	char tmpbuf[TMPBUFLEN];
11591e0bd755SEric Paris 
11601e0bd755SEric Paris 	if (!task)
11611e0bd755SEric Paris 		return -ESRCH;
11621e0bd755SEric Paris 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
11631e0bd755SEric Paris 				audit_get_sessionid(task));
11641e0bd755SEric Paris 	put_task_struct(task);
11651e0bd755SEric Paris 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
11661e0bd755SEric Paris }
11671e0bd755SEric Paris 
11681e0bd755SEric Paris static const struct file_operations proc_sessionid_operations = {
11691e0bd755SEric Paris 	.read		= proc_sessionid_read,
117087df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
11711e0bd755SEric Paris };
11721da177e4SLinus Torvalds #endif
11731da177e4SLinus Torvalds 
1174f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
1175f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1176f4f154fdSAkinobu Mita 				      size_t count, loff_t *ppos)
1177f4f154fdSAkinobu Mita {
1178f4f154fdSAkinobu Mita 	struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1179f4f154fdSAkinobu Mita 	char buffer[PROC_NUMBUF];
1180f4f154fdSAkinobu Mita 	size_t len;
1181f4f154fdSAkinobu Mita 	int make_it_fail;
1182f4f154fdSAkinobu Mita 
1183f4f154fdSAkinobu Mita 	if (!task)
1184f4f154fdSAkinobu Mita 		return -ESRCH;
1185f4f154fdSAkinobu Mita 	make_it_fail = task->make_it_fail;
1186f4f154fdSAkinobu Mita 	put_task_struct(task);
1187f4f154fdSAkinobu Mita 
1188f4f154fdSAkinobu Mita 	len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
11890c28f287SAkinobu Mita 
11900c28f287SAkinobu Mita 	return simple_read_from_buffer(buf, count, ppos, buffer, len);
1191f4f154fdSAkinobu Mita }
1192f4f154fdSAkinobu Mita 
1193f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_write(struct file * file,
1194f4f154fdSAkinobu Mita 			const char __user * buf, size_t count, loff_t *ppos)
1195f4f154fdSAkinobu Mita {
1196f4f154fdSAkinobu Mita 	struct task_struct *task;
1197f4f154fdSAkinobu Mita 	char buffer[PROC_NUMBUF], *end;
1198f4f154fdSAkinobu Mita 	int make_it_fail;
1199f4f154fdSAkinobu Mita 
1200f4f154fdSAkinobu Mita 	if (!capable(CAP_SYS_RESOURCE))
1201f4f154fdSAkinobu Mita 		return -EPERM;
1202f4f154fdSAkinobu Mita 	memset(buffer, 0, sizeof(buffer));
1203f4f154fdSAkinobu Mita 	if (count > sizeof(buffer) - 1)
1204f4f154fdSAkinobu Mita 		count = sizeof(buffer) - 1;
1205f4f154fdSAkinobu Mita 	if (copy_from_user(buffer, buf, count))
1206f4f154fdSAkinobu Mita 		return -EFAULT;
1207cba8aafeSVincent Li 	make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1208cba8aafeSVincent Li 	if (*end)
1209cba8aafeSVincent Li 		return -EINVAL;
1210f4f154fdSAkinobu Mita 	task = get_proc_task(file->f_dentry->d_inode);
1211f4f154fdSAkinobu Mita 	if (!task)
1212f4f154fdSAkinobu Mita 		return -ESRCH;
1213f4f154fdSAkinobu Mita 	task->make_it_fail = make_it_fail;
1214f4f154fdSAkinobu Mita 	put_task_struct(task);
1215cba8aafeSVincent Li 
1216cba8aafeSVincent Li 	return count;
1217f4f154fdSAkinobu Mita }
1218f4f154fdSAkinobu Mita 
121900977a59SArjan van de Ven static const struct file_operations proc_fault_inject_operations = {
1220f4f154fdSAkinobu Mita 	.read		= proc_fault_inject_read,
1221f4f154fdSAkinobu Mita 	.write		= proc_fault_inject_write,
122287df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
1223f4f154fdSAkinobu Mita };
1224f4f154fdSAkinobu Mita #endif
1225f4f154fdSAkinobu Mita 
12269745512cSArjan van de Ven 
122743ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
122843ae34cbSIngo Molnar /*
122943ae34cbSIngo Molnar  * Print out various scheduling related per-task fields:
123043ae34cbSIngo Molnar  */
123143ae34cbSIngo Molnar static int sched_show(struct seq_file *m, void *v)
123243ae34cbSIngo Molnar {
123343ae34cbSIngo Molnar 	struct inode *inode = m->private;
123443ae34cbSIngo Molnar 	struct task_struct *p;
123543ae34cbSIngo Molnar 
123643ae34cbSIngo Molnar 	p = get_proc_task(inode);
123743ae34cbSIngo Molnar 	if (!p)
123843ae34cbSIngo Molnar 		return -ESRCH;
123943ae34cbSIngo Molnar 	proc_sched_show_task(p, m);
124043ae34cbSIngo Molnar 
124143ae34cbSIngo Molnar 	put_task_struct(p);
124243ae34cbSIngo Molnar 
124343ae34cbSIngo Molnar 	return 0;
124443ae34cbSIngo Molnar }
124543ae34cbSIngo Molnar 
124643ae34cbSIngo Molnar static ssize_t
124743ae34cbSIngo Molnar sched_write(struct file *file, const char __user *buf,
124843ae34cbSIngo Molnar 	    size_t count, loff_t *offset)
124943ae34cbSIngo Molnar {
125043ae34cbSIngo Molnar 	struct inode *inode = file->f_path.dentry->d_inode;
125143ae34cbSIngo Molnar 	struct task_struct *p;
125243ae34cbSIngo Molnar 
125343ae34cbSIngo Molnar 	p = get_proc_task(inode);
125443ae34cbSIngo Molnar 	if (!p)
125543ae34cbSIngo Molnar 		return -ESRCH;
125643ae34cbSIngo Molnar 	proc_sched_set_task(p);
125743ae34cbSIngo Molnar 
125843ae34cbSIngo Molnar 	put_task_struct(p);
125943ae34cbSIngo Molnar 
126043ae34cbSIngo Molnar 	return count;
126143ae34cbSIngo Molnar }
126243ae34cbSIngo Molnar 
126343ae34cbSIngo Molnar static int sched_open(struct inode *inode, struct file *filp)
126443ae34cbSIngo Molnar {
1265c6a34058SJovi Zhang 	return single_open(filp, sched_show, inode);
126643ae34cbSIngo Molnar }
126743ae34cbSIngo Molnar 
126843ae34cbSIngo Molnar static const struct file_operations proc_pid_sched_operations = {
126943ae34cbSIngo Molnar 	.open		= sched_open,
127043ae34cbSIngo Molnar 	.read		= seq_read,
127143ae34cbSIngo Molnar 	.write		= sched_write,
127243ae34cbSIngo Molnar 	.llseek		= seq_lseek,
12735ea473a1SAlexey Dobriyan 	.release	= single_release,
127443ae34cbSIngo Molnar };
127543ae34cbSIngo Molnar 
127643ae34cbSIngo Molnar #endif
127743ae34cbSIngo Molnar 
12785091faa4SMike Galbraith #ifdef CONFIG_SCHED_AUTOGROUP
12795091faa4SMike Galbraith /*
12805091faa4SMike Galbraith  * Print out autogroup related information:
12815091faa4SMike Galbraith  */
12825091faa4SMike Galbraith static int sched_autogroup_show(struct seq_file *m, void *v)
12835091faa4SMike Galbraith {
12845091faa4SMike Galbraith 	struct inode *inode = m->private;
12855091faa4SMike Galbraith 	struct task_struct *p;
12865091faa4SMike Galbraith 
12875091faa4SMike Galbraith 	p = get_proc_task(inode);
12885091faa4SMike Galbraith 	if (!p)
12895091faa4SMike Galbraith 		return -ESRCH;
12905091faa4SMike Galbraith 	proc_sched_autogroup_show_task(p, m);
12915091faa4SMike Galbraith 
12925091faa4SMike Galbraith 	put_task_struct(p);
12935091faa4SMike Galbraith 
12945091faa4SMike Galbraith 	return 0;
12955091faa4SMike Galbraith }
12965091faa4SMike Galbraith 
12975091faa4SMike Galbraith static ssize_t
12985091faa4SMike Galbraith sched_autogroup_write(struct file *file, const char __user *buf,
12995091faa4SMike Galbraith 	    size_t count, loff_t *offset)
13005091faa4SMike Galbraith {
13015091faa4SMike Galbraith 	struct inode *inode = file->f_path.dentry->d_inode;
13025091faa4SMike Galbraith 	struct task_struct *p;
13035091faa4SMike Galbraith 	char buffer[PROC_NUMBUF];
13040a8cb8e3SAlexey Dobriyan 	int nice;
13055091faa4SMike Galbraith 	int err;
13065091faa4SMike Galbraith 
13075091faa4SMike Galbraith 	memset(buffer, 0, sizeof(buffer));
13085091faa4SMike Galbraith 	if (count > sizeof(buffer) - 1)
13095091faa4SMike Galbraith 		count = sizeof(buffer) - 1;
13105091faa4SMike Galbraith 	if (copy_from_user(buffer, buf, count))
13115091faa4SMike Galbraith 		return -EFAULT;
13125091faa4SMike Galbraith 
13130a8cb8e3SAlexey Dobriyan 	err = kstrtoint(strstrip(buffer), 0, &nice);
13140a8cb8e3SAlexey Dobriyan 	if (err < 0)
13150a8cb8e3SAlexey Dobriyan 		return err;
13165091faa4SMike Galbraith 
13175091faa4SMike Galbraith 	p = get_proc_task(inode);
13185091faa4SMike Galbraith 	if (!p)
13195091faa4SMike Galbraith 		return -ESRCH;
13205091faa4SMike Galbraith 
13215091faa4SMike Galbraith 	err = nice;
13225091faa4SMike Galbraith 	err = proc_sched_autogroup_set_nice(p, &err);
13235091faa4SMike Galbraith 	if (err)
13245091faa4SMike Galbraith 		count = err;
13255091faa4SMike Galbraith 
13265091faa4SMike Galbraith 	put_task_struct(p);
13275091faa4SMike Galbraith 
13285091faa4SMike Galbraith 	return count;
13295091faa4SMike Galbraith }
13305091faa4SMike Galbraith 
13315091faa4SMike Galbraith static int sched_autogroup_open(struct inode *inode, struct file *filp)
13325091faa4SMike Galbraith {
13335091faa4SMike Galbraith 	int ret;
13345091faa4SMike Galbraith 
13355091faa4SMike Galbraith 	ret = single_open(filp, sched_autogroup_show, NULL);
13365091faa4SMike Galbraith 	if (!ret) {
13375091faa4SMike Galbraith 		struct seq_file *m = filp->private_data;
13385091faa4SMike Galbraith 
13395091faa4SMike Galbraith 		m->private = inode;
13405091faa4SMike Galbraith 	}
13415091faa4SMike Galbraith 	return ret;
13425091faa4SMike Galbraith }
13435091faa4SMike Galbraith 
13445091faa4SMike Galbraith static const struct file_operations proc_pid_sched_autogroup_operations = {
13455091faa4SMike Galbraith 	.open		= sched_autogroup_open,
13465091faa4SMike Galbraith 	.read		= seq_read,
13475091faa4SMike Galbraith 	.write		= sched_autogroup_write,
13485091faa4SMike Galbraith 	.llseek		= seq_lseek,
13495091faa4SMike Galbraith 	.release	= single_release,
13505091faa4SMike Galbraith };
13515091faa4SMike Galbraith 
13525091faa4SMike Galbraith #endif /* CONFIG_SCHED_AUTOGROUP */
13535091faa4SMike Galbraith 
13544614a696Sjohn stultz static ssize_t comm_write(struct file *file, const char __user *buf,
13554614a696Sjohn stultz 				size_t count, loff_t *offset)
13564614a696Sjohn stultz {
13574614a696Sjohn stultz 	struct inode *inode = file->f_path.dentry->d_inode;
13584614a696Sjohn stultz 	struct task_struct *p;
13594614a696Sjohn stultz 	char buffer[TASK_COMM_LEN];
13604614a696Sjohn stultz 
13614614a696Sjohn stultz 	memset(buffer, 0, sizeof(buffer));
13624614a696Sjohn stultz 	if (count > sizeof(buffer) - 1)
13634614a696Sjohn stultz 		count = sizeof(buffer) - 1;
13644614a696Sjohn stultz 	if (copy_from_user(buffer, buf, count))
13654614a696Sjohn stultz 		return -EFAULT;
13664614a696Sjohn stultz 
13674614a696Sjohn stultz 	p = get_proc_task(inode);
13684614a696Sjohn stultz 	if (!p)
13694614a696Sjohn stultz 		return -ESRCH;
13704614a696Sjohn stultz 
13714614a696Sjohn stultz 	if (same_thread_group(current, p))
13724614a696Sjohn stultz 		set_task_comm(p, buffer);
13734614a696Sjohn stultz 	else
13744614a696Sjohn stultz 		count = -EINVAL;
13754614a696Sjohn stultz 
13764614a696Sjohn stultz 	put_task_struct(p);
13774614a696Sjohn stultz 
13784614a696Sjohn stultz 	return count;
13794614a696Sjohn stultz }
13804614a696Sjohn stultz 
13814614a696Sjohn stultz static int comm_show(struct seq_file *m, void *v)
13824614a696Sjohn stultz {
13834614a696Sjohn stultz 	struct inode *inode = m->private;
13844614a696Sjohn stultz 	struct task_struct *p;
13854614a696Sjohn stultz 
13864614a696Sjohn stultz 	p = get_proc_task(inode);
13874614a696Sjohn stultz 	if (!p)
13884614a696Sjohn stultz 		return -ESRCH;
13894614a696Sjohn stultz 
13904614a696Sjohn stultz 	task_lock(p);
13914614a696Sjohn stultz 	seq_printf(m, "%s\n", p->comm);
13924614a696Sjohn stultz 	task_unlock(p);
13934614a696Sjohn stultz 
13944614a696Sjohn stultz 	put_task_struct(p);
13954614a696Sjohn stultz 
13964614a696Sjohn stultz 	return 0;
13974614a696Sjohn stultz }
13984614a696Sjohn stultz 
13994614a696Sjohn stultz static int comm_open(struct inode *inode, struct file *filp)
14004614a696Sjohn stultz {
1401c6a34058SJovi Zhang 	return single_open(filp, comm_show, inode);
14024614a696Sjohn stultz }
14034614a696Sjohn stultz 
14044614a696Sjohn stultz static const struct file_operations proc_pid_set_comm_operations = {
14054614a696Sjohn stultz 	.open		= comm_open,
14064614a696Sjohn stultz 	.read		= seq_read,
14074614a696Sjohn stultz 	.write		= comm_write,
14084614a696Sjohn stultz 	.llseek		= seq_lseek,
14094614a696Sjohn stultz 	.release	= single_release,
14104614a696Sjohn stultz };
14114614a696Sjohn stultz 
14127773fbc5SCyrill Gorcunov static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
1413925d1c40SMatt Helsley {
1414925d1c40SMatt Helsley 	struct task_struct *task;
1415925d1c40SMatt Helsley 	struct mm_struct *mm;
1416925d1c40SMatt Helsley 	struct file *exe_file;
1417925d1c40SMatt Helsley 
14187773fbc5SCyrill Gorcunov 	task = get_proc_task(dentry->d_inode);
1419925d1c40SMatt Helsley 	if (!task)
1420925d1c40SMatt Helsley 		return -ENOENT;
1421925d1c40SMatt Helsley 	mm = get_task_mm(task);
1422925d1c40SMatt Helsley 	put_task_struct(task);
1423925d1c40SMatt Helsley 	if (!mm)
1424925d1c40SMatt Helsley 		return -ENOENT;
1425925d1c40SMatt Helsley 	exe_file = get_mm_exe_file(mm);
1426925d1c40SMatt Helsley 	mmput(mm);
1427925d1c40SMatt Helsley 	if (exe_file) {
1428925d1c40SMatt Helsley 		*exe_path = exe_file->f_path;
1429925d1c40SMatt Helsley 		path_get(&exe_file->f_path);
1430925d1c40SMatt Helsley 		fput(exe_file);
1431925d1c40SMatt Helsley 		return 0;
1432925d1c40SMatt Helsley 	} else
1433925d1c40SMatt Helsley 		return -ENOENT;
1434925d1c40SMatt Helsley }
1435925d1c40SMatt Helsley 
1436008b150aSAl Viro static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
14371da177e4SLinus Torvalds {
14381da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
14391da177e4SLinus Torvalds 	int error = -EACCES;
14401da177e4SLinus Torvalds 
14411da177e4SLinus Torvalds 	/* We don't need a base pointer in the /proc filesystem */
14421d957f9bSJan Blunck 	path_put(&nd->path);
14431da177e4SLinus Torvalds 
1444778c1144SEric W. Biederman 	/* Are we allowed to snoop on the tasks file descriptors? */
1445778c1144SEric W. Biederman 	if (!proc_fd_access_allowed(inode))
14461da177e4SLinus Torvalds 		goto out;
14471da177e4SLinus Torvalds 
14487773fbc5SCyrill Gorcunov 	error = PROC_I(inode)->op.proc_get_link(dentry, &nd->path);
14491da177e4SLinus Torvalds out:
1450008b150aSAl Viro 	return ERR_PTR(error);
14511da177e4SLinus Torvalds }
14521da177e4SLinus Torvalds 
14533dcd25f3SJan Blunck static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
14541da177e4SLinus Torvalds {
1455e12ba74dSMel Gorman 	char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
14563dcd25f3SJan Blunck 	char *pathname;
14571da177e4SLinus Torvalds 	int len;
14581da177e4SLinus Torvalds 
14591da177e4SLinus Torvalds 	if (!tmp)
14601da177e4SLinus Torvalds 		return -ENOMEM;
14611da177e4SLinus Torvalds 
14627b2a69baSEric W. Biederman 	pathname = d_path(path, tmp, PAGE_SIZE);
14633dcd25f3SJan Blunck 	len = PTR_ERR(pathname);
14643dcd25f3SJan Blunck 	if (IS_ERR(pathname))
14651da177e4SLinus Torvalds 		goto out;
14663dcd25f3SJan Blunck 	len = tmp + PAGE_SIZE - 1 - pathname;
14671da177e4SLinus Torvalds 
14681da177e4SLinus Torvalds 	if (len > buflen)
14691da177e4SLinus Torvalds 		len = buflen;
14703dcd25f3SJan Blunck 	if (copy_to_user(buffer, pathname, len))
14711da177e4SLinus Torvalds 		len = -EFAULT;
14721da177e4SLinus Torvalds  out:
14731da177e4SLinus Torvalds 	free_page((unsigned long)tmp);
14741da177e4SLinus Torvalds 	return len;
14751da177e4SLinus Torvalds }
14761da177e4SLinus Torvalds 
14771da177e4SLinus Torvalds static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
14781da177e4SLinus Torvalds {
14791da177e4SLinus Torvalds 	int error = -EACCES;
14801da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
14813dcd25f3SJan Blunck 	struct path path;
14821da177e4SLinus Torvalds 
1483778c1144SEric W. Biederman 	/* Are we allowed to snoop on the tasks file descriptors? */
1484778c1144SEric W. Biederman 	if (!proc_fd_access_allowed(inode))
14851da177e4SLinus Torvalds 		goto out;
14861da177e4SLinus Torvalds 
14877773fbc5SCyrill Gorcunov 	error = PROC_I(inode)->op.proc_get_link(dentry, &path);
14881da177e4SLinus Torvalds 	if (error)
14891da177e4SLinus Torvalds 		goto out;
14901da177e4SLinus Torvalds 
14913dcd25f3SJan Blunck 	error = do_proc_readlink(&path, buffer, buflen);
14923dcd25f3SJan Blunck 	path_put(&path);
14931da177e4SLinus Torvalds out:
14941da177e4SLinus Torvalds 	return error;
14951da177e4SLinus Torvalds }
14961da177e4SLinus Torvalds 
1497c5ef1c42SArjan van de Ven static const struct inode_operations proc_pid_link_inode_operations = {
14981da177e4SLinus Torvalds 	.readlink	= proc_pid_readlink,
14996d76fa58SLinus Torvalds 	.follow_link	= proc_pid_follow_link,
15006d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
15011da177e4SLinus Torvalds };
15021da177e4SLinus Torvalds 
150328a6d671SEric W. Biederman 
150428a6d671SEric W. Biederman /* building an inode */
150528a6d671SEric W. Biederman 
150628a6d671SEric W. Biederman static int task_dumpable(struct task_struct *task)
150728a6d671SEric W. Biederman {
150828a6d671SEric W. Biederman 	int dumpable = 0;
150928a6d671SEric W. Biederman 	struct mm_struct *mm;
151028a6d671SEric W. Biederman 
151128a6d671SEric W. Biederman 	task_lock(task);
151228a6d671SEric W. Biederman 	mm = task->mm;
151328a6d671SEric W. Biederman 	if (mm)
15146c5d5238SKawai, Hidehiro 		dumpable = get_dumpable(mm);
151528a6d671SEric W. Biederman 	task_unlock(task);
151628a6d671SEric W. Biederman 	if(dumpable == 1)
151728a6d671SEric W. Biederman 		return 1;
151828a6d671SEric W. Biederman 	return 0;
151928a6d671SEric W. Biederman }
152028a6d671SEric W. Biederman 
15216b4e306aSEric W. Biederman struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
152228a6d671SEric W. Biederman {
152328a6d671SEric W. Biederman 	struct inode * inode;
152428a6d671SEric W. Biederman 	struct proc_inode *ei;
1525c69e8d9cSDavid Howells 	const struct cred *cred;
152628a6d671SEric W. Biederman 
152728a6d671SEric W. Biederman 	/* We need a new inode */
152828a6d671SEric W. Biederman 
152928a6d671SEric W. Biederman 	inode = new_inode(sb);
153028a6d671SEric W. Biederman 	if (!inode)
153128a6d671SEric W. Biederman 		goto out;
153228a6d671SEric W. Biederman 
153328a6d671SEric W. Biederman 	/* Common stuff */
153428a6d671SEric W. Biederman 	ei = PROC_I(inode);
153585fe4025SChristoph Hellwig 	inode->i_ino = get_next_ino();
153628a6d671SEric W. Biederman 	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
153728a6d671SEric W. Biederman 	inode->i_op = &proc_def_inode_operations;
153828a6d671SEric W. Biederman 
153928a6d671SEric W. Biederman 	/*
154028a6d671SEric W. Biederman 	 * grab the reference to task.
154128a6d671SEric W. Biederman 	 */
15421a657f78SOleg Nesterov 	ei->pid = get_task_pid(task, PIDTYPE_PID);
154328a6d671SEric W. Biederman 	if (!ei->pid)
154428a6d671SEric W. Biederman 		goto out_unlock;
154528a6d671SEric W. Biederman 
154628a6d671SEric W. Biederman 	if (task_dumpable(task)) {
1547c69e8d9cSDavid Howells 		rcu_read_lock();
1548c69e8d9cSDavid Howells 		cred = __task_cred(task);
1549c69e8d9cSDavid Howells 		inode->i_uid = cred->euid;
1550c69e8d9cSDavid Howells 		inode->i_gid = cred->egid;
1551c69e8d9cSDavid Howells 		rcu_read_unlock();
155228a6d671SEric W. Biederman 	}
155328a6d671SEric W. Biederman 	security_task_to_inode(task, inode);
155428a6d671SEric W. Biederman 
155528a6d671SEric W. Biederman out:
155628a6d671SEric W. Biederman 	return inode;
155728a6d671SEric W. Biederman 
155828a6d671SEric W. Biederman out_unlock:
155928a6d671SEric W. Biederman 	iput(inode);
156028a6d671SEric W. Biederman 	return NULL;
156128a6d671SEric W. Biederman }
156228a6d671SEric W. Biederman 
15636b4e306aSEric W. Biederman int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
156428a6d671SEric W. Biederman {
156528a6d671SEric W. Biederman 	struct inode *inode = dentry->d_inode;
156628a6d671SEric W. Biederman 	struct task_struct *task;
1567c69e8d9cSDavid Howells 	const struct cred *cred;
15680499680aSVasiliy Kulikov 	struct pid_namespace *pid = dentry->d_sb->s_fs_info;
1569c69e8d9cSDavid Howells 
157028a6d671SEric W. Biederman 	generic_fillattr(inode, stat);
157128a6d671SEric W. Biederman 
157228a6d671SEric W. Biederman 	rcu_read_lock();
157328a6d671SEric W. Biederman 	stat->uid = 0;
157428a6d671SEric W. Biederman 	stat->gid = 0;
157528a6d671SEric W. Biederman 	task = pid_task(proc_pid(inode), PIDTYPE_PID);
157628a6d671SEric W. Biederman 	if (task) {
15770499680aSVasiliy Kulikov 		if (!has_pid_permissions(pid, task, 2)) {
15780499680aSVasiliy Kulikov 			rcu_read_unlock();
15790499680aSVasiliy Kulikov 			/*
15800499680aSVasiliy Kulikov 			 * This doesn't prevent learning whether PID exists,
15810499680aSVasiliy Kulikov 			 * it only makes getattr() consistent with readdir().
15820499680aSVasiliy Kulikov 			 */
15830499680aSVasiliy Kulikov 			return -ENOENT;
15840499680aSVasiliy Kulikov 		}
158528a6d671SEric W. Biederman 		if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
158628a6d671SEric W. Biederman 		    task_dumpable(task)) {
1587c69e8d9cSDavid Howells 			cred = __task_cred(task);
1588c69e8d9cSDavid Howells 			stat->uid = cred->euid;
1589c69e8d9cSDavid Howells 			stat->gid = cred->egid;
159028a6d671SEric W. Biederman 		}
159128a6d671SEric W. Biederman 	}
159228a6d671SEric W. Biederman 	rcu_read_unlock();
159328a6d671SEric W. Biederman 	return 0;
159428a6d671SEric W. Biederman }
159528a6d671SEric W. Biederman 
159628a6d671SEric W. Biederman /* dentry stuff */
159728a6d671SEric W. Biederman 
159828a6d671SEric W. Biederman /*
159928a6d671SEric W. Biederman  *	Exceptional case: normally we are not allowed to unhash a busy
160028a6d671SEric W. Biederman  * directory. In this case, however, we can do it - no aliasing problems
160128a6d671SEric W. Biederman  * due to the way we treat inodes.
160228a6d671SEric W. Biederman  *
160328a6d671SEric W. Biederman  * Rewrite the inode's ownerships here because the owning task may have
160428a6d671SEric W. Biederman  * performed a setuid(), etc.
160528a6d671SEric W. Biederman  *
160628a6d671SEric W. Biederman  * Before the /proc/pid/status file was created the only way to read
160728a6d671SEric W. Biederman  * the effective uid of a /process was to stat /proc/pid.  Reading
160828a6d671SEric W. Biederman  * /proc/pid/status is slow enough that procps and other packages
160928a6d671SEric W. Biederman  * kept stating /proc/pid.  To keep the rules in /proc simple I have
161028a6d671SEric W. Biederman  * made this apply to all per process world readable and executable
161128a6d671SEric W. Biederman  * directories.
161228a6d671SEric W. Biederman  */
16136b4e306aSEric W. Biederman int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
161428a6d671SEric W. Biederman {
161534286d66SNick Piggin 	struct inode *inode;
161634286d66SNick Piggin 	struct task_struct *task;
1617c69e8d9cSDavid Howells 	const struct cred *cred;
1618c69e8d9cSDavid Howells 
161934286d66SNick Piggin 	if (nd && nd->flags & LOOKUP_RCU)
162034286d66SNick Piggin 		return -ECHILD;
162134286d66SNick Piggin 
162234286d66SNick Piggin 	inode = dentry->d_inode;
162334286d66SNick Piggin 	task = get_proc_task(inode);
162434286d66SNick Piggin 
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 			rcu_read_lock();
1629c69e8d9cSDavid Howells 			cred = __task_cred(task);
1630c69e8d9cSDavid Howells 			inode->i_uid = cred->euid;
1631c69e8d9cSDavid Howells 			inode->i_gid = cred->egid;
1632c69e8d9cSDavid Howells 			rcu_read_unlock();
163328a6d671SEric W. Biederman 		} else {
163428a6d671SEric W. Biederman 			inode->i_uid = 0;
163528a6d671SEric W. Biederman 			inode->i_gid = 0;
163628a6d671SEric W. Biederman 		}
163728a6d671SEric W. Biederman 		inode->i_mode &= ~(S_ISUID | S_ISGID);
163828a6d671SEric W. Biederman 		security_task_to_inode(task, inode);
163928a6d671SEric W. Biederman 		put_task_struct(task);
164028a6d671SEric W. Biederman 		return 1;
164128a6d671SEric W. Biederman 	}
164228a6d671SEric W. Biederman 	d_drop(dentry);
164328a6d671SEric W. Biederman 	return 0;
164428a6d671SEric W. Biederman }
164528a6d671SEric W. Biederman 
1646fe15ce44SNick Piggin static int pid_delete_dentry(const struct dentry * dentry)
164728a6d671SEric W. Biederman {
164828a6d671SEric W. Biederman 	/* Is the task we represent dead?
164928a6d671SEric W. Biederman 	 * If so, then don't put the dentry on the lru list,
165028a6d671SEric W. Biederman 	 * kill it immediately.
165128a6d671SEric W. Biederman 	 */
165228a6d671SEric W. Biederman 	return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
165328a6d671SEric W. Biederman }
165428a6d671SEric W. Biederman 
16556b4e306aSEric W. Biederman const struct dentry_operations pid_dentry_operations =
165628a6d671SEric W. Biederman {
165728a6d671SEric W. Biederman 	.d_revalidate	= pid_revalidate,
165828a6d671SEric W. Biederman 	.d_delete	= pid_delete_dentry,
165928a6d671SEric W. Biederman };
166028a6d671SEric W. Biederman 
166128a6d671SEric W. Biederman /* Lookups */
166228a6d671SEric W. Biederman 
16631c0d04c9SEric W. Biederman /*
16641c0d04c9SEric W. Biederman  * Fill a directory entry.
16651c0d04c9SEric W. Biederman  *
16661c0d04c9SEric W. Biederman  * If possible create the dcache entry and derive our inode number and
16671c0d04c9SEric W. Biederman  * file type from dcache entry.
16681c0d04c9SEric W. Biederman  *
16691c0d04c9SEric W. Biederman  * Since all of the proc inode numbers are dynamically generated, the inode
16701c0d04c9SEric W. Biederman  * numbers do not exist until the inode is cache.  This means creating the
16711c0d04c9SEric W. Biederman  * the dcache entry in readdir is necessary to keep the inode numbers
16721c0d04c9SEric W. Biederman  * reported by readdir in sync with the inode numbers reported
16731c0d04c9SEric W. Biederman  * by stat.
16741c0d04c9SEric W. Biederman  */
16756b4e306aSEric W. Biederman int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
16766b4e306aSEric W. Biederman 	const char *name, int len,
1677c5141e6dSEric Dumazet 	instantiate_t instantiate, struct task_struct *task, const void *ptr)
167861a28784SEric W. Biederman {
16792fddfeefSJosef "Jeff" Sipek 	struct dentry *child, *dir = filp->f_path.dentry;
168061a28784SEric W. Biederman 	struct inode *inode;
168161a28784SEric W. Biederman 	struct qstr qname;
168261a28784SEric W. Biederman 	ino_t ino = 0;
168361a28784SEric W. Biederman 	unsigned type = DT_UNKNOWN;
168461a28784SEric W. Biederman 
168561a28784SEric W. Biederman 	qname.name = name;
168661a28784SEric W. Biederman 	qname.len  = len;
168761a28784SEric W. Biederman 	qname.hash = full_name_hash(name, len);
168861a28784SEric W. Biederman 
168961a28784SEric W. Biederman 	child = d_lookup(dir, &qname);
169061a28784SEric W. Biederman 	if (!child) {
169161a28784SEric W. Biederman 		struct dentry *new;
169261a28784SEric W. Biederman 		new = d_alloc(dir, &qname);
169361a28784SEric W. Biederman 		if (new) {
169461a28784SEric W. Biederman 			child = instantiate(dir->d_inode, new, task, ptr);
169561a28784SEric W. Biederman 			if (child)
169661a28784SEric W. Biederman 				dput(new);
169761a28784SEric W. Biederman 			else
169861a28784SEric W. Biederman 				child = new;
169961a28784SEric W. Biederman 		}
170061a28784SEric W. Biederman 	}
170161a28784SEric W. Biederman 	if (!child || IS_ERR(child) || !child->d_inode)
170261a28784SEric W. Biederman 		goto end_instantiate;
170361a28784SEric W. Biederman 	inode = child->d_inode;
170461a28784SEric W. Biederman 	if (inode) {
170561a28784SEric W. Biederman 		ino = inode->i_ino;
170661a28784SEric W. Biederman 		type = inode->i_mode >> 12;
170761a28784SEric W. Biederman 	}
170861a28784SEric W. Biederman 	dput(child);
170961a28784SEric W. Biederman end_instantiate:
171061a28784SEric W. Biederman 	if (!ino)
171161a28784SEric W. Biederman 		ino = find_inode_number(dir, &qname);
171261a28784SEric W. Biederman 	if (!ino)
171361a28784SEric W. Biederman 		ino = 1;
171461a28784SEric W. Biederman 	return filldir(dirent, name, len, filp->f_pos, ino, type);
171561a28784SEric W. Biederman }
171661a28784SEric W. Biederman 
171728a6d671SEric W. Biederman static unsigned name_to_int(struct dentry *dentry)
171828a6d671SEric W. Biederman {
171928a6d671SEric W. Biederman 	const char *name = dentry->d_name.name;
172028a6d671SEric W. Biederman 	int len = dentry->d_name.len;
172128a6d671SEric W. Biederman 	unsigned n = 0;
172228a6d671SEric W. Biederman 
172328a6d671SEric W. Biederman 	if (len > 1 && *name == '0')
172428a6d671SEric W. Biederman 		goto out;
172528a6d671SEric W. Biederman 	while (len-- > 0) {
172628a6d671SEric W. Biederman 		unsigned c = *name++ - '0';
172728a6d671SEric W. Biederman 		if (c > 9)
172828a6d671SEric W. Biederman 			goto out;
172928a6d671SEric W. Biederman 		if (n >= (~0U-9)/10)
173028a6d671SEric W. Biederman 			goto out;
173128a6d671SEric W. Biederman 		n *= 10;
173228a6d671SEric W. Biederman 		n += c;
173328a6d671SEric W. Biederman 	}
173428a6d671SEric W. Biederman 	return n;
173528a6d671SEric W. Biederman out:
173628a6d671SEric W. Biederman 	return ~0U;
173728a6d671SEric W. Biederman }
173828a6d671SEric W. Biederman 
173927932742SMiklos Szeredi #define PROC_FDINFO_MAX 64
174027932742SMiklos Szeredi 
17413dcd25f3SJan Blunck static int proc_fd_info(struct inode *inode, struct path *path, char *info)
174228a6d671SEric W. Biederman {
17435e442a49SLinus Torvalds 	struct task_struct *task = get_proc_task(inode);
17445e442a49SLinus Torvalds 	struct files_struct *files = NULL;
174528a6d671SEric W. Biederman 	struct file *file;
174628a6d671SEric W. Biederman 	int fd = proc_fd(inode);
174728a6d671SEric W. Biederman 
17485e442a49SLinus Torvalds 	if (task) {
174928a6d671SEric W. Biederman 		files = get_files_struct(task);
17505e442a49SLinus Torvalds 		put_task_struct(task);
17515e442a49SLinus Torvalds 	}
17525e442a49SLinus Torvalds 	if (files) {
175328a6d671SEric W. Biederman 		/*
175428a6d671SEric W. Biederman 		 * We are not taking a ref to the file structure, so we must
175528a6d671SEric W. Biederman 		 * hold ->file_lock.
175628a6d671SEric W. Biederman 		 */
175728a6d671SEric W. Biederman 		spin_lock(&files->file_lock);
175828a6d671SEric W. Biederman 		file = fcheck_files(files, fd);
175928a6d671SEric W. Biederman 		if (file) {
17601117f72eSLinus Torvalds 			unsigned int f_flags;
17611117f72eSLinus Torvalds 			struct fdtable *fdt;
17621117f72eSLinus Torvalds 
17631117f72eSLinus Torvalds 			fdt = files_fdtable(files);
17641117f72eSLinus Torvalds 			f_flags = file->f_flags & ~O_CLOEXEC;
17651117f72eSLinus Torvalds 			if (FD_ISSET(fd, fdt->close_on_exec))
17661117f72eSLinus Torvalds 				f_flags |= O_CLOEXEC;
17671117f72eSLinus Torvalds 
17683dcd25f3SJan Blunck 			if (path) {
17693dcd25f3SJan Blunck 				*path = file->f_path;
17703dcd25f3SJan Blunck 				path_get(&file->f_path);
17713dcd25f3SJan Blunck 			}
177227932742SMiklos Szeredi 			if (info)
177327932742SMiklos Szeredi 				snprintf(info, PROC_FDINFO_MAX,
177427932742SMiklos Szeredi 					 "pos:\t%lli\n"
177527932742SMiklos Szeredi 					 "flags:\t0%o\n",
177627932742SMiklos Szeredi 					 (long long) file->f_pos,
17771117f72eSLinus Torvalds 					 f_flags);
177828a6d671SEric W. Biederman 			spin_unlock(&files->file_lock);
177928a6d671SEric W. Biederman 			put_files_struct(files);
17805e442a49SLinus Torvalds 			return 0;
17815e442a49SLinus Torvalds 		}
17825e442a49SLinus Torvalds 		spin_unlock(&files->file_lock);
17835e442a49SLinus Torvalds 		put_files_struct(files);
17845e442a49SLinus Torvalds 	}
17855e442a49SLinus Torvalds 	return -ENOENT;
178628a6d671SEric W. Biederman }
178728a6d671SEric W. Biederman 
17887773fbc5SCyrill Gorcunov static int proc_fd_link(struct dentry *dentry, struct path *path)
178927932742SMiklos Szeredi {
17907773fbc5SCyrill Gorcunov 	return proc_fd_info(dentry->d_inode, path, NULL);
179127932742SMiklos Szeredi }
179227932742SMiklos Szeredi 
179328a6d671SEric W. Biederman static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
179428a6d671SEric W. Biederman {
179534286d66SNick Piggin 	struct inode *inode;
179634286d66SNick Piggin 	struct task_struct *task;
179734286d66SNick Piggin 	int fd;
179828a6d671SEric W. Biederman 	struct files_struct *files;
1799c69e8d9cSDavid Howells 	const struct cred *cred;
180028a6d671SEric W. Biederman 
180134286d66SNick Piggin 	if (nd && nd->flags & LOOKUP_RCU)
180234286d66SNick Piggin 		return -ECHILD;
180334286d66SNick Piggin 
180434286d66SNick Piggin 	inode = dentry->d_inode;
180534286d66SNick Piggin 	task = get_proc_task(inode);
180634286d66SNick Piggin 	fd = proc_fd(inode);
180734286d66SNick Piggin 
180828a6d671SEric W. Biederman 	if (task) {
180928a6d671SEric W. Biederman 		files = get_files_struct(task);
181028a6d671SEric W. Biederman 		if (files) {
181128a6d671SEric W. Biederman 			rcu_read_lock();
181228a6d671SEric W. Biederman 			if (fcheck_files(files, fd)) {
181328a6d671SEric W. Biederman 				rcu_read_unlock();
181428a6d671SEric W. Biederman 				put_files_struct(files);
181528a6d671SEric W. Biederman 				if (task_dumpable(task)) {
1816c69e8d9cSDavid Howells 					rcu_read_lock();
1817c69e8d9cSDavid Howells 					cred = __task_cred(task);
1818c69e8d9cSDavid Howells 					inode->i_uid = cred->euid;
1819c69e8d9cSDavid Howells 					inode->i_gid = cred->egid;
1820c69e8d9cSDavid Howells 					rcu_read_unlock();
182128a6d671SEric W. Biederman 				} else {
182228a6d671SEric W. Biederman 					inode->i_uid = 0;
182328a6d671SEric W. Biederman 					inode->i_gid = 0;
182428a6d671SEric W. Biederman 				}
182528a6d671SEric W. Biederman 				inode->i_mode &= ~(S_ISUID | S_ISGID);
182628a6d671SEric W. Biederman 				security_task_to_inode(task, inode);
182728a6d671SEric W. Biederman 				put_task_struct(task);
182828a6d671SEric W. Biederman 				return 1;
182928a6d671SEric W. Biederman 			}
183028a6d671SEric W. Biederman 			rcu_read_unlock();
183128a6d671SEric W. Biederman 			put_files_struct(files);
183228a6d671SEric W. Biederman 		}
183328a6d671SEric W. Biederman 		put_task_struct(task);
183428a6d671SEric W. Biederman 	}
183528a6d671SEric W. Biederman 	d_drop(dentry);
183628a6d671SEric W. Biederman 	return 0;
183728a6d671SEric W. Biederman }
183828a6d671SEric W. Biederman 
1839d72f71ebSAl Viro static const struct dentry_operations tid_fd_dentry_operations =
184028a6d671SEric W. Biederman {
184128a6d671SEric W. Biederman 	.d_revalidate	= tid_fd_revalidate,
184228a6d671SEric W. Biederman 	.d_delete	= pid_delete_dentry,
184328a6d671SEric W. Biederman };
184428a6d671SEric W. Biederman 
1845444ceed8SEric W. Biederman static struct dentry *proc_fd_instantiate(struct inode *dir,
1846c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
184728a6d671SEric W. Biederman {
1848c5141e6dSEric Dumazet 	unsigned fd = *(const unsigned *)ptr;
184928a6d671SEric W. Biederman 	struct file *file;
185028a6d671SEric W. Biederman 	struct files_struct *files;
185128a6d671SEric W. Biederman  	struct inode *inode;
185228a6d671SEric W. Biederman  	struct proc_inode *ei;
1853444ceed8SEric W. Biederman 	struct dentry *error = ERR_PTR(-ENOENT);
185428a6d671SEric W. Biederman 
185561a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
185628a6d671SEric W. Biederman 	if (!inode)
185728a6d671SEric W. Biederman 		goto out;
185828a6d671SEric W. Biederman 	ei = PROC_I(inode);
185928a6d671SEric W. Biederman 	ei->fd = fd;
186028a6d671SEric W. Biederman 	files = get_files_struct(task);
186128a6d671SEric W. Biederman 	if (!files)
1862444ceed8SEric W. Biederman 		goto out_iput;
186328a6d671SEric W. Biederman 	inode->i_mode = S_IFLNK;
186428a6d671SEric W. Biederman 
186528a6d671SEric W. Biederman 	/*
186628a6d671SEric W. Biederman 	 * We are not taking a ref to the file structure, so we must
186728a6d671SEric W. Biederman 	 * hold ->file_lock.
186828a6d671SEric W. Biederman 	 */
186928a6d671SEric W. Biederman 	spin_lock(&files->file_lock);
187028a6d671SEric W. Biederman 	file = fcheck_files(files, fd);
187128a6d671SEric W. Biederman 	if (!file)
1872444ceed8SEric W. Biederman 		goto out_unlock;
1873aeb5d727SAl Viro 	if (file->f_mode & FMODE_READ)
187428a6d671SEric W. Biederman 		inode->i_mode |= S_IRUSR | S_IXUSR;
1875aeb5d727SAl Viro 	if (file->f_mode & FMODE_WRITE)
187628a6d671SEric W. Biederman 		inode->i_mode |= S_IWUSR | S_IXUSR;
187728a6d671SEric W. Biederman 	spin_unlock(&files->file_lock);
187828a6d671SEric W. Biederman 	put_files_struct(files);
1879444ceed8SEric W. Biederman 
18805e442a49SLinus Torvalds 	inode->i_op = &proc_pid_link_inode_operations;
188128a6d671SEric W. Biederman 	inode->i_size = 64;
188228a6d671SEric W. Biederman 	ei->op.proc_get_link = proc_fd_link;
1883fb045adbSNick Piggin 	d_set_d_op(dentry, &tid_fd_dentry_operations);
188428a6d671SEric W. Biederman 	d_add(dentry, inode);
188528a6d671SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
188628a6d671SEric W. Biederman 	if (tid_fd_revalidate(dentry, NULL))
1887444ceed8SEric W. Biederman 		error = NULL;
1888444ceed8SEric W. Biederman 
1889444ceed8SEric W. Biederman  out:
1890444ceed8SEric W. Biederman 	return error;
1891444ceed8SEric W. Biederman out_unlock:
1892444ceed8SEric W. Biederman 	spin_unlock(&files->file_lock);
1893444ceed8SEric W. Biederman 	put_files_struct(files);
1894444ceed8SEric W. Biederman out_iput:
1895444ceed8SEric W. Biederman 	iput(inode);
1896444ceed8SEric W. Biederman 	goto out;
1897444ceed8SEric W. Biederman }
1898444ceed8SEric W. Biederman 
189927932742SMiklos Szeredi static struct dentry *proc_lookupfd_common(struct inode *dir,
190027932742SMiklos Szeredi 					   struct dentry *dentry,
190127932742SMiklos Szeredi 					   instantiate_t instantiate)
1902444ceed8SEric W. Biederman {
1903444ceed8SEric W. Biederman 	struct task_struct *task = get_proc_task(dir);
1904444ceed8SEric W. Biederman 	unsigned fd = name_to_int(dentry);
1905444ceed8SEric W. Biederman 	struct dentry *result = ERR_PTR(-ENOENT);
1906444ceed8SEric W. Biederman 
1907444ceed8SEric W. Biederman 	if (!task)
1908444ceed8SEric W. Biederman 		goto out_no_task;
1909444ceed8SEric W. Biederman 	if (fd == ~0U)
1910444ceed8SEric W. Biederman 		goto out;
1911444ceed8SEric W. Biederman 
191227932742SMiklos Szeredi 	result = instantiate(dir, dentry, task, &fd);
191328a6d671SEric W. Biederman out:
191428a6d671SEric W. Biederman 	put_task_struct(task);
191528a6d671SEric W. Biederman out_no_task:
191628a6d671SEric W. Biederman 	return result;
191728a6d671SEric W. Biederman }
191828a6d671SEric W. Biederman 
191927932742SMiklos Szeredi static int proc_readfd_common(struct file * filp, void * dirent,
192027932742SMiklos Szeredi 			      filldir_t filldir, instantiate_t instantiate)
19211da177e4SLinus Torvalds {
19222fddfeefSJosef "Jeff" Sipek 	struct dentry *dentry = filp->f_path.dentry;
19235634708bSEric W. Biederman 	struct inode *inode = dentry->d_inode;
192499f89551SEric W. Biederman 	struct task_struct *p = get_proc_task(inode);
1925457c2510SPavel Emelyanov 	unsigned int fd, ino;
19261da177e4SLinus Torvalds 	int retval;
19271da177e4SLinus Torvalds 	struct files_struct * files;
19281da177e4SLinus Torvalds 
19291da177e4SLinus Torvalds 	retval = -ENOENT;
193099f89551SEric W. Biederman 	if (!p)
193199f89551SEric W. Biederman 		goto out_no_task;
19321da177e4SLinus Torvalds 	retval = 0;
19331da177e4SLinus Torvalds 
19341da177e4SLinus Torvalds 	fd = filp->f_pos;
19351da177e4SLinus Torvalds 	switch (fd) {
19361da177e4SLinus Torvalds 		case 0:
19371da177e4SLinus Torvalds 			if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
19385e442a49SLinus Torvalds 				goto out;
19391da177e4SLinus Torvalds 			filp->f_pos++;
19401da177e4SLinus Torvalds 		case 1:
19415634708bSEric W. Biederman 			ino = parent_ino(dentry);
19421da177e4SLinus Torvalds 			if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
19435e442a49SLinus Torvalds 				goto out;
19441da177e4SLinus Torvalds 			filp->f_pos++;
19451da177e4SLinus Torvalds 		default:
19461da177e4SLinus Torvalds 			files = get_files_struct(p);
19471da177e4SLinus Torvalds 			if (!files)
19485e442a49SLinus Torvalds 				goto out;
1949b835996fSDipankar Sarma 			rcu_read_lock();
19501da177e4SLinus Torvalds 			for (fd = filp->f_pos-2;
19519b4f526cSAl Viro 			     fd < files_fdtable(files)->max_fds;
19521da177e4SLinus Torvalds 			     fd++, filp->f_pos++) {
195327932742SMiklos Szeredi 				char name[PROC_NUMBUF];
195427932742SMiklos Szeredi 				int len;
19551da177e4SLinus Torvalds 
19561da177e4SLinus Torvalds 				if (!fcheck_files(files, fd))
19571da177e4SLinus Torvalds 					continue;
1958b835996fSDipankar Sarma 				rcu_read_unlock();
19591da177e4SLinus Torvalds 
196027932742SMiklos Szeredi 				len = snprintf(name, sizeof(name), "%d", fd);
196127932742SMiklos Szeredi 				if (proc_fill_cache(filp, dirent, filldir,
196227932742SMiklos Szeredi 						    name, len, instantiate,
196327932742SMiklos Szeredi 						    p, &fd) < 0) {
1964b835996fSDipankar Sarma 					rcu_read_lock();
19651da177e4SLinus Torvalds 					break;
19661da177e4SLinus Torvalds 				}
1967b835996fSDipankar Sarma 				rcu_read_lock();
19681da177e4SLinus Torvalds 			}
1969b835996fSDipankar Sarma 			rcu_read_unlock();
19701da177e4SLinus Torvalds 			put_files_struct(files);
19711da177e4SLinus Torvalds 	}
19721da177e4SLinus Torvalds out:
197399f89551SEric W. Biederman 	put_task_struct(p);
197499f89551SEric W. Biederman out_no_task:
19751da177e4SLinus Torvalds 	return retval;
19761da177e4SLinus Torvalds }
19771da177e4SLinus Torvalds 
197827932742SMiklos Szeredi static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
197927932742SMiklos Szeredi 				    struct nameidata *nd)
198027932742SMiklos Szeredi {
198127932742SMiklos Szeredi 	return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
198227932742SMiklos Szeredi }
198327932742SMiklos Szeredi 
198427932742SMiklos Szeredi static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
198527932742SMiklos Szeredi {
198627932742SMiklos Szeredi 	return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
198727932742SMiklos Szeredi }
198827932742SMiklos Szeredi 
198927932742SMiklos Szeredi static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
199027932742SMiklos Szeredi 				      size_t len, loff_t *ppos)
199127932742SMiklos Szeredi {
199227932742SMiklos Szeredi 	char tmp[PROC_FDINFO_MAX];
19933dcd25f3SJan Blunck 	int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
199427932742SMiklos Szeredi 	if (!err)
199527932742SMiklos Szeredi 		err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
199627932742SMiklos Szeredi 	return err;
199727932742SMiklos Szeredi }
199827932742SMiklos Szeredi 
199927932742SMiklos Szeredi static const struct file_operations proc_fdinfo_file_operations = {
200027932742SMiklos Szeredi 	.open           = nonseekable_open,
200127932742SMiklos Szeredi 	.read		= proc_fdinfo_read,
20026038f373SArnd Bergmann 	.llseek		= no_llseek,
200327932742SMiklos Szeredi };
200427932742SMiklos Szeredi 
200500977a59SArjan van de Ven static const struct file_operations proc_fd_operations = {
20061da177e4SLinus Torvalds 	.read		= generic_read_dir,
20071da177e4SLinus Torvalds 	.readdir	= proc_readfd,
20086038f373SArnd Bergmann 	.llseek		= default_llseek,
20091da177e4SLinus Torvalds };
20101da177e4SLinus Torvalds 
2011640708a2SPavel Emelyanov #ifdef CONFIG_CHECKPOINT_RESTORE
2012640708a2SPavel Emelyanov 
2013640708a2SPavel Emelyanov /*
2014640708a2SPavel Emelyanov  * dname_to_vma_addr - maps a dentry name into two unsigned longs
2015640708a2SPavel Emelyanov  * which represent vma start and end addresses.
2016640708a2SPavel Emelyanov  */
2017640708a2SPavel Emelyanov static int dname_to_vma_addr(struct dentry *dentry,
2018640708a2SPavel Emelyanov 			     unsigned long *start, unsigned long *end)
2019640708a2SPavel Emelyanov {
2020640708a2SPavel Emelyanov 	if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2)
2021640708a2SPavel Emelyanov 		return -EINVAL;
2022640708a2SPavel Emelyanov 
2023640708a2SPavel Emelyanov 	return 0;
2024640708a2SPavel Emelyanov }
2025640708a2SPavel Emelyanov 
2026640708a2SPavel Emelyanov static int map_files_d_revalidate(struct dentry *dentry, struct nameidata *nd)
2027640708a2SPavel Emelyanov {
2028640708a2SPavel Emelyanov 	unsigned long vm_start, vm_end;
2029640708a2SPavel Emelyanov 	bool exact_vma_exists = false;
2030640708a2SPavel Emelyanov 	struct mm_struct *mm = NULL;
2031640708a2SPavel Emelyanov 	struct task_struct *task;
2032640708a2SPavel Emelyanov 	const struct cred *cred;
2033640708a2SPavel Emelyanov 	struct inode *inode;
2034640708a2SPavel Emelyanov 	int status = 0;
2035640708a2SPavel Emelyanov 
2036640708a2SPavel Emelyanov 	if (nd && nd->flags & LOOKUP_RCU)
2037640708a2SPavel Emelyanov 		return -ECHILD;
2038640708a2SPavel Emelyanov 
2039640708a2SPavel Emelyanov 	if (!capable(CAP_SYS_ADMIN)) {
2040640708a2SPavel Emelyanov 		status = -EACCES;
2041640708a2SPavel Emelyanov 		goto out_notask;
2042640708a2SPavel Emelyanov 	}
2043640708a2SPavel Emelyanov 
2044640708a2SPavel Emelyanov 	inode = dentry->d_inode;
2045640708a2SPavel Emelyanov 	task = get_proc_task(inode);
2046640708a2SPavel Emelyanov 	if (!task)
2047640708a2SPavel Emelyanov 		goto out_notask;
2048640708a2SPavel Emelyanov 
2049640708a2SPavel Emelyanov 	if (!ptrace_may_access(task, PTRACE_MODE_READ))
2050640708a2SPavel Emelyanov 		goto out;
2051640708a2SPavel Emelyanov 
2052640708a2SPavel Emelyanov 	mm = get_task_mm(task);
2053640708a2SPavel Emelyanov 	if (!mm)
2054640708a2SPavel Emelyanov 		goto out;
2055640708a2SPavel Emelyanov 
2056640708a2SPavel Emelyanov 	if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
2057640708a2SPavel Emelyanov 		down_read(&mm->mmap_sem);
2058640708a2SPavel Emelyanov 		exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
2059640708a2SPavel Emelyanov 		up_read(&mm->mmap_sem);
2060640708a2SPavel Emelyanov 	}
2061640708a2SPavel Emelyanov 
2062640708a2SPavel Emelyanov 	mmput(mm);
2063640708a2SPavel Emelyanov 
2064640708a2SPavel Emelyanov 	if (exact_vma_exists) {
2065640708a2SPavel Emelyanov 		if (task_dumpable(task)) {
2066640708a2SPavel Emelyanov 			rcu_read_lock();
2067640708a2SPavel Emelyanov 			cred = __task_cred(task);
2068640708a2SPavel Emelyanov 			inode->i_uid = cred->euid;
2069640708a2SPavel Emelyanov 			inode->i_gid = cred->egid;
2070640708a2SPavel Emelyanov 			rcu_read_unlock();
2071640708a2SPavel Emelyanov 		} else {
2072640708a2SPavel Emelyanov 			inode->i_uid = 0;
2073640708a2SPavel Emelyanov 			inode->i_gid = 0;
2074640708a2SPavel Emelyanov 		}
2075640708a2SPavel Emelyanov 		security_task_to_inode(task, inode);
2076640708a2SPavel Emelyanov 		status = 1;
2077640708a2SPavel Emelyanov 	}
2078640708a2SPavel Emelyanov 
2079640708a2SPavel Emelyanov out:
2080640708a2SPavel Emelyanov 	put_task_struct(task);
2081640708a2SPavel Emelyanov 
2082640708a2SPavel Emelyanov out_notask:
2083640708a2SPavel Emelyanov 	if (status <= 0)
2084640708a2SPavel Emelyanov 		d_drop(dentry);
2085640708a2SPavel Emelyanov 
2086640708a2SPavel Emelyanov 	return status;
2087640708a2SPavel Emelyanov }
2088640708a2SPavel Emelyanov 
2089640708a2SPavel Emelyanov static const struct dentry_operations tid_map_files_dentry_operations = {
2090640708a2SPavel Emelyanov 	.d_revalidate	= map_files_d_revalidate,
2091640708a2SPavel Emelyanov 	.d_delete	= pid_delete_dentry,
2092640708a2SPavel Emelyanov };
2093640708a2SPavel Emelyanov 
2094640708a2SPavel Emelyanov static int proc_map_files_get_link(struct dentry *dentry, struct path *path)
2095640708a2SPavel Emelyanov {
2096640708a2SPavel Emelyanov 	unsigned long vm_start, vm_end;
2097640708a2SPavel Emelyanov 	struct vm_area_struct *vma;
2098640708a2SPavel Emelyanov 	struct task_struct *task;
2099640708a2SPavel Emelyanov 	struct mm_struct *mm;
2100640708a2SPavel Emelyanov 	int rc;
2101640708a2SPavel Emelyanov 
2102640708a2SPavel Emelyanov 	rc = -ENOENT;
2103640708a2SPavel Emelyanov 	task = get_proc_task(dentry->d_inode);
2104640708a2SPavel Emelyanov 	if (!task)
2105640708a2SPavel Emelyanov 		goto out;
2106640708a2SPavel Emelyanov 
2107640708a2SPavel Emelyanov 	mm = get_task_mm(task);
2108640708a2SPavel Emelyanov 	put_task_struct(task);
2109640708a2SPavel Emelyanov 	if (!mm)
2110640708a2SPavel Emelyanov 		goto out;
2111640708a2SPavel Emelyanov 
2112640708a2SPavel Emelyanov 	rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
2113640708a2SPavel Emelyanov 	if (rc)
2114640708a2SPavel Emelyanov 		goto out_mmput;
2115640708a2SPavel Emelyanov 
2116640708a2SPavel Emelyanov 	down_read(&mm->mmap_sem);
2117640708a2SPavel Emelyanov 	vma = find_exact_vma(mm, vm_start, vm_end);
2118640708a2SPavel Emelyanov 	if (vma && vma->vm_file) {
2119640708a2SPavel Emelyanov 		*path = vma->vm_file->f_path;
2120640708a2SPavel Emelyanov 		path_get(path);
2121640708a2SPavel Emelyanov 		rc = 0;
2122640708a2SPavel Emelyanov 	}
2123640708a2SPavel Emelyanov 	up_read(&mm->mmap_sem);
2124640708a2SPavel Emelyanov 
2125640708a2SPavel Emelyanov out_mmput:
2126640708a2SPavel Emelyanov 	mmput(mm);
2127640708a2SPavel Emelyanov out:
2128640708a2SPavel Emelyanov 	return rc;
2129640708a2SPavel Emelyanov }
2130640708a2SPavel Emelyanov 
2131640708a2SPavel Emelyanov struct map_files_info {
2132640708a2SPavel Emelyanov 	struct file	*file;
2133640708a2SPavel Emelyanov 	unsigned long	len;
2134640708a2SPavel Emelyanov 	unsigned char	name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
2135640708a2SPavel Emelyanov };
2136640708a2SPavel Emelyanov 
2137640708a2SPavel Emelyanov static struct dentry *
2138640708a2SPavel Emelyanov proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
2139640708a2SPavel Emelyanov 			   struct task_struct *task, const void *ptr)
2140640708a2SPavel Emelyanov {
2141640708a2SPavel Emelyanov 	const struct file *file = ptr;
2142640708a2SPavel Emelyanov 	struct proc_inode *ei;
2143640708a2SPavel Emelyanov 	struct inode *inode;
2144640708a2SPavel Emelyanov 
2145640708a2SPavel Emelyanov 	if (!file)
2146640708a2SPavel Emelyanov 		return ERR_PTR(-ENOENT);
2147640708a2SPavel Emelyanov 
2148640708a2SPavel Emelyanov 	inode = proc_pid_make_inode(dir->i_sb, task);
2149640708a2SPavel Emelyanov 	if (!inode)
2150640708a2SPavel Emelyanov 		return ERR_PTR(-ENOENT);
2151640708a2SPavel Emelyanov 
2152640708a2SPavel Emelyanov 	ei = PROC_I(inode);
2153640708a2SPavel Emelyanov 	ei->op.proc_get_link = proc_map_files_get_link;
2154640708a2SPavel Emelyanov 
2155640708a2SPavel Emelyanov 	inode->i_op = &proc_pid_link_inode_operations;
2156640708a2SPavel Emelyanov 	inode->i_size = 64;
2157640708a2SPavel Emelyanov 	inode->i_mode = S_IFLNK;
2158640708a2SPavel Emelyanov 
2159640708a2SPavel Emelyanov 	if (file->f_mode & FMODE_READ)
2160640708a2SPavel Emelyanov 		inode->i_mode |= S_IRUSR;
2161640708a2SPavel Emelyanov 	if (file->f_mode & FMODE_WRITE)
2162640708a2SPavel Emelyanov 		inode->i_mode |= S_IWUSR;
2163640708a2SPavel Emelyanov 
2164640708a2SPavel Emelyanov 	d_set_d_op(dentry, &tid_map_files_dentry_operations);
2165640708a2SPavel Emelyanov 	d_add(dentry, inode);
2166640708a2SPavel Emelyanov 
2167640708a2SPavel Emelyanov 	return NULL;
2168640708a2SPavel Emelyanov }
2169640708a2SPavel Emelyanov 
2170640708a2SPavel Emelyanov static struct dentry *proc_map_files_lookup(struct inode *dir,
2171640708a2SPavel Emelyanov 		struct dentry *dentry, struct nameidata *nd)
2172640708a2SPavel Emelyanov {
2173640708a2SPavel Emelyanov 	unsigned long vm_start, vm_end;
2174640708a2SPavel Emelyanov 	struct vm_area_struct *vma;
2175640708a2SPavel Emelyanov 	struct task_struct *task;
2176640708a2SPavel Emelyanov 	struct dentry *result;
2177640708a2SPavel Emelyanov 	struct mm_struct *mm;
2178640708a2SPavel Emelyanov 
2179640708a2SPavel Emelyanov 	result = ERR_PTR(-EACCES);
2180640708a2SPavel Emelyanov 	if (!capable(CAP_SYS_ADMIN))
2181640708a2SPavel Emelyanov 		goto out;
2182640708a2SPavel Emelyanov 
2183640708a2SPavel Emelyanov 	result = ERR_PTR(-ENOENT);
2184640708a2SPavel Emelyanov 	task = get_proc_task(dir);
2185640708a2SPavel Emelyanov 	if (!task)
2186640708a2SPavel Emelyanov 		goto out;
2187640708a2SPavel Emelyanov 
2188640708a2SPavel Emelyanov 	result = ERR_PTR(-EACCES);
2189640708a2SPavel Emelyanov 	if (lock_trace(task))
2190640708a2SPavel Emelyanov 		goto out_put_task;
2191640708a2SPavel Emelyanov 
2192640708a2SPavel Emelyanov 	result = ERR_PTR(-ENOENT);
2193640708a2SPavel Emelyanov 	if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
2194640708a2SPavel Emelyanov 		goto out_unlock;
2195640708a2SPavel Emelyanov 
2196640708a2SPavel Emelyanov 	mm = get_task_mm(task);
2197640708a2SPavel Emelyanov 	if (!mm)
2198640708a2SPavel Emelyanov 		goto out_unlock;
2199640708a2SPavel Emelyanov 
2200640708a2SPavel Emelyanov 	down_read(&mm->mmap_sem);
2201640708a2SPavel Emelyanov 	vma = find_exact_vma(mm, vm_start, vm_end);
2202640708a2SPavel Emelyanov 	if (!vma)
2203640708a2SPavel Emelyanov 		goto out_no_vma;
2204640708a2SPavel Emelyanov 
2205640708a2SPavel Emelyanov 	result = proc_map_files_instantiate(dir, dentry, task, vma->vm_file);
2206640708a2SPavel Emelyanov 
2207640708a2SPavel Emelyanov out_no_vma:
2208640708a2SPavel Emelyanov 	up_read(&mm->mmap_sem);
2209640708a2SPavel Emelyanov 	mmput(mm);
2210640708a2SPavel Emelyanov out_unlock:
2211640708a2SPavel Emelyanov 	unlock_trace(task);
2212640708a2SPavel Emelyanov out_put_task:
2213640708a2SPavel Emelyanov 	put_task_struct(task);
2214640708a2SPavel Emelyanov out:
2215640708a2SPavel Emelyanov 	return result;
2216640708a2SPavel Emelyanov }
2217640708a2SPavel Emelyanov 
2218640708a2SPavel Emelyanov static const struct inode_operations proc_map_files_inode_operations = {
2219640708a2SPavel Emelyanov 	.lookup		= proc_map_files_lookup,
2220640708a2SPavel Emelyanov 	.permission	= proc_fd_permission,
2221640708a2SPavel Emelyanov 	.setattr	= proc_setattr,
2222640708a2SPavel Emelyanov };
2223640708a2SPavel Emelyanov 
2224640708a2SPavel Emelyanov static int
2225640708a2SPavel Emelyanov proc_map_files_readdir(struct file *filp, void *dirent, filldir_t filldir)
2226640708a2SPavel Emelyanov {
2227640708a2SPavel Emelyanov 	struct dentry *dentry = filp->f_path.dentry;
2228640708a2SPavel Emelyanov 	struct inode *inode = dentry->d_inode;
2229640708a2SPavel Emelyanov 	struct vm_area_struct *vma;
2230640708a2SPavel Emelyanov 	struct task_struct *task;
2231640708a2SPavel Emelyanov 	struct mm_struct *mm;
2232640708a2SPavel Emelyanov 	ino_t ino;
2233640708a2SPavel Emelyanov 	int ret;
2234640708a2SPavel Emelyanov 
2235640708a2SPavel Emelyanov 	ret = -EACCES;
2236640708a2SPavel Emelyanov 	if (!capable(CAP_SYS_ADMIN))
2237640708a2SPavel Emelyanov 		goto out;
2238640708a2SPavel Emelyanov 
2239640708a2SPavel Emelyanov 	ret = -ENOENT;
2240640708a2SPavel Emelyanov 	task = get_proc_task(inode);
2241640708a2SPavel Emelyanov 	if (!task)
2242640708a2SPavel Emelyanov 		goto out;
2243640708a2SPavel Emelyanov 
2244640708a2SPavel Emelyanov 	ret = -EACCES;
2245640708a2SPavel Emelyanov 	if (lock_trace(task))
2246640708a2SPavel Emelyanov 		goto out_put_task;
2247640708a2SPavel Emelyanov 
2248640708a2SPavel Emelyanov 	ret = 0;
2249640708a2SPavel Emelyanov 	switch (filp->f_pos) {
2250640708a2SPavel Emelyanov 	case 0:
2251640708a2SPavel Emelyanov 		ino = inode->i_ino;
2252640708a2SPavel Emelyanov 		if (filldir(dirent, ".", 1, 0, ino, DT_DIR) < 0)
2253640708a2SPavel Emelyanov 			goto out_unlock;
2254640708a2SPavel Emelyanov 		filp->f_pos++;
2255640708a2SPavel Emelyanov 	case 1:
2256640708a2SPavel Emelyanov 		ino = parent_ino(dentry);
2257640708a2SPavel Emelyanov 		if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
2258640708a2SPavel Emelyanov 			goto out_unlock;
2259640708a2SPavel Emelyanov 		filp->f_pos++;
2260640708a2SPavel Emelyanov 	default:
2261640708a2SPavel Emelyanov 	{
2262640708a2SPavel Emelyanov 		unsigned long nr_files, pos, i;
2263640708a2SPavel Emelyanov 		struct flex_array *fa = NULL;
2264640708a2SPavel Emelyanov 		struct map_files_info info;
2265640708a2SPavel Emelyanov 		struct map_files_info *p;
2266640708a2SPavel Emelyanov 
2267640708a2SPavel Emelyanov 		mm = get_task_mm(task);
2268640708a2SPavel Emelyanov 		if (!mm)
2269640708a2SPavel Emelyanov 			goto out_unlock;
2270640708a2SPavel Emelyanov 		down_read(&mm->mmap_sem);
2271640708a2SPavel Emelyanov 
2272640708a2SPavel Emelyanov 		nr_files = 0;
2273640708a2SPavel Emelyanov 
2274640708a2SPavel Emelyanov 		/*
2275640708a2SPavel Emelyanov 		 * We need two passes here:
2276640708a2SPavel Emelyanov 		 *
2277640708a2SPavel Emelyanov 		 *  1) Collect vmas of mapped files with mmap_sem taken
2278640708a2SPavel Emelyanov 		 *  2) Release mmap_sem and instantiate entries
2279640708a2SPavel Emelyanov 		 *
2280640708a2SPavel Emelyanov 		 * otherwise we get lockdep complained, since filldir()
2281640708a2SPavel Emelyanov 		 * routine might require mmap_sem taken in might_fault().
2282640708a2SPavel Emelyanov 		 */
2283640708a2SPavel Emelyanov 
2284640708a2SPavel Emelyanov 		for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
2285640708a2SPavel Emelyanov 			if (vma->vm_file && ++pos > filp->f_pos)
2286640708a2SPavel Emelyanov 				nr_files++;
2287640708a2SPavel Emelyanov 		}
2288640708a2SPavel Emelyanov 
2289640708a2SPavel Emelyanov 		if (nr_files) {
2290640708a2SPavel Emelyanov 			fa = flex_array_alloc(sizeof(info), nr_files,
2291640708a2SPavel Emelyanov 						GFP_KERNEL);
2292640708a2SPavel Emelyanov 			if (!fa || flex_array_prealloc(fa, 0, nr_files,
2293640708a2SPavel Emelyanov 							GFP_KERNEL)) {
2294640708a2SPavel Emelyanov 				ret = -ENOMEM;
2295640708a2SPavel Emelyanov 				if (fa)
2296640708a2SPavel Emelyanov 					flex_array_free(fa);
2297640708a2SPavel Emelyanov 				up_read(&mm->mmap_sem);
2298640708a2SPavel Emelyanov 				mmput(mm);
2299640708a2SPavel Emelyanov 				goto out_unlock;
2300640708a2SPavel Emelyanov 			}
2301640708a2SPavel Emelyanov 			for (i = 0, vma = mm->mmap, pos = 2; vma;
2302640708a2SPavel Emelyanov 					vma = vma->vm_next) {
2303640708a2SPavel Emelyanov 				if (!vma->vm_file)
2304640708a2SPavel Emelyanov 					continue;
2305640708a2SPavel Emelyanov 				if (++pos <= filp->f_pos)
2306640708a2SPavel Emelyanov 					continue;
2307640708a2SPavel Emelyanov 
2308640708a2SPavel Emelyanov 				get_file(vma->vm_file);
2309640708a2SPavel Emelyanov 				info.file = vma->vm_file;
2310640708a2SPavel Emelyanov 				info.len = snprintf(info.name,
2311640708a2SPavel Emelyanov 						sizeof(info.name), "%lx-%lx",
2312640708a2SPavel Emelyanov 						vma->vm_start, vma->vm_end);
2313640708a2SPavel Emelyanov 				if (flex_array_put(fa, i++, &info, GFP_KERNEL))
2314640708a2SPavel Emelyanov 					BUG();
2315640708a2SPavel Emelyanov 			}
2316640708a2SPavel Emelyanov 		}
2317640708a2SPavel Emelyanov 		up_read(&mm->mmap_sem);
2318640708a2SPavel Emelyanov 
2319640708a2SPavel Emelyanov 		for (i = 0; i < nr_files; i++) {
2320640708a2SPavel Emelyanov 			p = flex_array_get(fa, i);
2321640708a2SPavel Emelyanov 			ret = proc_fill_cache(filp, dirent, filldir,
2322640708a2SPavel Emelyanov 					      p->name, p->len,
2323640708a2SPavel Emelyanov 					      proc_map_files_instantiate,
2324640708a2SPavel Emelyanov 					      task, p->file);
2325640708a2SPavel Emelyanov 			if (ret)
2326640708a2SPavel Emelyanov 				break;
2327640708a2SPavel Emelyanov 			filp->f_pos++;
2328640708a2SPavel Emelyanov 			fput(p->file);
2329640708a2SPavel Emelyanov 		}
2330640708a2SPavel Emelyanov 		for (; i < nr_files; i++) {
2331640708a2SPavel Emelyanov 			/*
2332640708a2SPavel Emelyanov 			 * In case of error don't forget
2333640708a2SPavel Emelyanov 			 * to put rest of file refs.
2334640708a2SPavel Emelyanov 			 */
2335640708a2SPavel Emelyanov 			p = flex_array_get(fa, i);
2336640708a2SPavel Emelyanov 			fput(p->file);
2337640708a2SPavel Emelyanov 		}
2338640708a2SPavel Emelyanov 		if (fa)
2339640708a2SPavel Emelyanov 			flex_array_free(fa);
2340640708a2SPavel Emelyanov 		mmput(mm);
2341640708a2SPavel Emelyanov 	}
2342640708a2SPavel Emelyanov 	}
2343640708a2SPavel Emelyanov 
2344640708a2SPavel Emelyanov out_unlock:
2345640708a2SPavel Emelyanov 	unlock_trace(task);
2346640708a2SPavel Emelyanov out_put_task:
2347640708a2SPavel Emelyanov 	put_task_struct(task);
2348640708a2SPavel Emelyanov out:
2349640708a2SPavel Emelyanov 	return ret;
2350640708a2SPavel Emelyanov }
2351640708a2SPavel Emelyanov 
2352640708a2SPavel Emelyanov static const struct file_operations proc_map_files_operations = {
2353640708a2SPavel Emelyanov 	.read		= generic_read_dir,
2354640708a2SPavel Emelyanov 	.readdir	= proc_map_files_readdir,
2355640708a2SPavel Emelyanov 	.llseek		= default_llseek,
2356640708a2SPavel Emelyanov };
2357640708a2SPavel Emelyanov 
2358640708a2SPavel Emelyanov #endif /* CONFIG_CHECKPOINT_RESTORE */
2359640708a2SPavel Emelyanov 
23601da177e4SLinus Torvalds /*
23618948e11fSAlexey Dobriyan  * /proc/pid/fd needs a special permission handler so that a process can still
23628948e11fSAlexey Dobriyan  * access /proc/self/fd after it has executed a setuid().
23638948e11fSAlexey Dobriyan  */
236410556cb2SAl Viro static int proc_fd_permission(struct inode *inode, int mask)
23658948e11fSAlexey Dobriyan {
23662830ba7fSAl Viro 	int rv = generic_permission(inode, mask);
23678948e11fSAlexey Dobriyan 	if (rv == 0)
23688948e11fSAlexey Dobriyan 		return 0;
23698948e11fSAlexey Dobriyan 	if (task_pid(current) == proc_pid(inode))
23708948e11fSAlexey Dobriyan 		rv = 0;
23718948e11fSAlexey Dobriyan 	return rv;
23728948e11fSAlexey Dobriyan }
23738948e11fSAlexey Dobriyan 
23748948e11fSAlexey Dobriyan /*
23751da177e4SLinus Torvalds  * proc directories can do almost nothing..
23761da177e4SLinus Torvalds  */
2377c5ef1c42SArjan van de Ven static const struct inode_operations proc_fd_inode_operations = {
23781da177e4SLinus Torvalds 	.lookup		= proc_lookupfd,
23798948e11fSAlexey Dobriyan 	.permission	= proc_fd_permission,
23806d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
23811da177e4SLinus Torvalds };
23821da177e4SLinus Torvalds 
238327932742SMiklos Szeredi static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
238427932742SMiklos Szeredi 	struct dentry *dentry, struct task_struct *task, const void *ptr)
238527932742SMiklos Szeredi {
238627932742SMiklos Szeredi 	unsigned fd = *(unsigned *)ptr;
238727932742SMiklos Szeredi  	struct inode *inode;
238827932742SMiklos Szeredi  	struct proc_inode *ei;
238927932742SMiklos Szeredi 	struct dentry *error = ERR_PTR(-ENOENT);
239027932742SMiklos Szeredi 
239127932742SMiklos Szeredi 	inode = proc_pid_make_inode(dir->i_sb, task);
239227932742SMiklos Szeredi 	if (!inode)
239327932742SMiklos Szeredi 		goto out;
239427932742SMiklos Szeredi 	ei = PROC_I(inode);
239527932742SMiklos Szeredi 	ei->fd = fd;
239627932742SMiklos Szeredi 	inode->i_mode = S_IFREG | S_IRUSR;
239727932742SMiklos Szeredi 	inode->i_fop = &proc_fdinfo_file_operations;
2398fb045adbSNick Piggin 	d_set_d_op(dentry, &tid_fd_dentry_operations);
239927932742SMiklos Szeredi 	d_add(dentry, inode);
240027932742SMiklos Szeredi 	/* Close the race of the process dying before we return the dentry */
240127932742SMiklos Szeredi 	if (tid_fd_revalidate(dentry, NULL))
240227932742SMiklos Szeredi 		error = NULL;
240327932742SMiklos Szeredi 
240427932742SMiklos Szeredi  out:
240527932742SMiklos Szeredi 	return error;
240627932742SMiklos Szeredi }
240727932742SMiklos Szeredi 
240827932742SMiklos Szeredi static struct dentry *proc_lookupfdinfo(struct inode *dir,
240927932742SMiklos Szeredi 					struct dentry *dentry,
241027932742SMiklos Szeredi 					struct nameidata *nd)
241127932742SMiklos Szeredi {
241227932742SMiklos Szeredi 	return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
241327932742SMiklos Szeredi }
241427932742SMiklos Szeredi 
241527932742SMiklos Szeredi static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
241627932742SMiklos Szeredi {
241727932742SMiklos Szeredi 	return proc_readfd_common(filp, dirent, filldir,
241827932742SMiklos Szeredi 				  proc_fdinfo_instantiate);
241927932742SMiklos Szeredi }
242027932742SMiklos Szeredi 
242127932742SMiklos Szeredi static const struct file_operations proc_fdinfo_operations = {
242227932742SMiklos Szeredi 	.read		= generic_read_dir,
242327932742SMiklos Szeredi 	.readdir	= proc_readfdinfo,
24246038f373SArnd Bergmann 	.llseek		= default_llseek,
242527932742SMiklos Szeredi };
242627932742SMiklos Szeredi 
242727932742SMiklos Szeredi /*
242827932742SMiklos Szeredi  * proc directories can do almost nothing..
242927932742SMiklos Szeredi  */
243027932742SMiklos Szeredi static const struct inode_operations proc_fdinfo_inode_operations = {
243127932742SMiklos Szeredi 	.lookup		= proc_lookupfdinfo,
243227932742SMiklos Szeredi 	.setattr	= proc_setattr,
243327932742SMiklos Szeredi };
243427932742SMiklos Szeredi 
243527932742SMiklos Szeredi 
2436444ceed8SEric W. Biederman static struct dentry *proc_pident_instantiate(struct inode *dir,
2437c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
2438444ceed8SEric W. Biederman {
2439c5141e6dSEric Dumazet 	const struct pid_entry *p = ptr;
2440444ceed8SEric W. Biederman 	struct inode *inode;
2441444ceed8SEric W. Biederman 	struct proc_inode *ei;
2442bd6daba9SKOSAKI Motohiro 	struct dentry *error = ERR_PTR(-ENOENT);
2443444ceed8SEric W. Biederman 
244461a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
2445444ceed8SEric W. Biederman 	if (!inode)
2446444ceed8SEric W. Biederman 		goto out;
2447444ceed8SEric W. Biederman 
2448444ceed8SEric W. Biederman 	ei = PROC_I(inode);
2449444ceed8SEric W. Biederman 	inode->i_mode = p->mode;
2450444ceed8SEric W. Biederman 	if (S_ISDIR(inode->i_mode))
2451bfe86848SMiklos Szeredi 		set_nlink(inode, 2);	/* Use getattr to fix if necessary */
2452444ceed8SEric W. Biederman 	if (p->iop)
2453444ceed8SEric W. Biederman 		inode->i_op = p->iop;
2454444ceed8SEric W. Biederman 	if (p->fop)
2455444ceed8SEric W. Biederman 		inode->i_fop = p->fop;
2456444ceed8SEric W. Biederman 	ei->op = p->op;
2457fb045adbSNick Piggin 	d_set_d_op(dentry, &pid_dentry_operations);
2458444ceed8SEric W. Biederman 	d_add(dentry, inode);
2459444ceed8SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
2460444ceed8SEric W. Biederman 	if (pid_revalidate(dentry, NULL))
2461444ceed8SEric W. Biederman 		error = NULL;
2462444ceed8SEric W. Biederman out:
2463444ceed8SEric W. Biederman 	return error;
2464444ceed8SEric W. Biederman }
2465444ceed8SEric W. Biederman 
24661da177e4SLinus Torvalds static struct dentry *proc_pident_lookup(struct inode *dir,
24671da177e4SLinus Torvalds 					 struct dentry *dentry,
2468c5141e6dSEric Dumazet 					 const struct pid_entry *ents,
24697bcd6b0eSEric W. Biederman 					 unsigned int nents)
24701da177e4SLinus Torvalds {
2471cd6a3ce9SEric W. Biederman 	struct dentry *error;
247299f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(dir);
2473c5141e6dSEric Dumazet 	const struct pid_entry *p, *last;
24741da177e4SLinus Torvalds 
2475cd6a3ce9SEric W. Biederman 	error = ERR_PTR(-ENOENT);
24761da177e4SLinus Torvalds 
247799f89551SEric W. Biederman 	if (!task)
247899f89551SEric W. Biederman 		goto out_no_task;
24791da177e4SLinus Torvalds 
248020cdc894SEric W. Biederman 	/*
248120cdc894SEric W. Biederman 	 * Yes, it does not scale. And it should not. Don't add
248220cdc894SEric W. Biederman 	 * new entries into /proc/<tgid>/ without very good reasons.
248320cdc894SEric W. Biederman 	 */
24847bcd6b0eSEric W. Biederman 	last = &ents[nents - 1];
24857bcd6b0eSEric W. Biederman 	for (p = ents; p <= last; p++) {
24861da177e4SLinus Torvalds 		if (p->len != dentry->d_name.len)
24871da177e4SLinus Torvalds 			continue;
24881da177e4SLinus Torvalds 		if (!memcmp(dentry->d_name.name, p->name, p->len))
24891da177e4SLinus Torvalds 			break;
24901da177e4SLinus Torvalds 	}
24917bcd6b0eSEric W. Biederman 	if (p > last)
24921da177e4SLinus Torvalds 		goto out;
24931da177e4SLinus Torvalds 
2494444ceed8SEric W. Biederman 	error = proc_pident_instantiate(dir, dentry, task, p);
24951da177e4SLinus Torvalds out:
249699f89551SEric W. Biederman 	put_task_struct(task);
249799f89551SEric W. Biederman out_no_task:
2498cd6a3ce9SEric W. Biederman 	return error;
24991da177e4SLinus Torvalds }
25001da177e4SLinus Torvalds 
2501c5141e6dSEric Dumazet static int proc_pident_fill_cache(struct file *filp, void *dirent,
2502c5141e6dSEric Dumazet 	filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
250361a28784SEric W. Biederman {
250461a28784SEric W. Biederman 	return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
250561a28784SEric W. Biederman 				proc_pident_instantiate, task, p);
250661a28784SEric W. Biederman }
250761a28784SEric W. Biederman 
250828a6d671SEric W. Biederman static int proc_pident_readdir(struct file *filp,
250928a6d671SEric W. Biederman 		void *dirent, filldir_t filldir,
2510c5141e6dSEric Dumazet 		const struct pid_entry *ents, unsigned int nents)
251128a6d671SEric W. Biederman {
251228a6d671SEric W. Biederman 	int i;
25132fddfeefSJosef "Jeff" Sipek 	struct dentry *dentry = filp->f_path.dentry;
251428a6d671SEric W. Biederman 	struct inode *inode = dentry->d_inode;
251528a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
2516c5141e6dSEric Dumazet 	const struct pid_entry *p, *last;
251728a6d671SEric W. Biederman 	ino_t ino;
251828a6d671SEric W. Biederman 	int ret;
251928a6d671SEric W. Biederman 
252028a6d671SEric W. Biederman 	ret = -ENOENT;
252128a6d671SEric W. Biederman 	if (!task)
252261a28784SEric W. Biederman 		goto out_no_task;
252328a6d671SEric W. Biederman 
252428a6d671SEric W. Biederman 	ret = 0;
252528a6d671SEric W. Biederman 	i = filp->f_pos;
252628a6d671SEric W. Biederman 	switch (i) {
252728a6d671SEric W. Biederman 	case 0:
252828a6d671SEric W. Biederman 		ino = inode->i_ino;
252928a6d671SEric W. Biederman 		if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
253028a6d671SEric W. Biederman 			goto out;
253128a6d671SEric W. Biederman 		i++;
253228a6d671SEric W. Biederman 		filp->f_pos++;
253328a6d671SEric W. Biederman 		/* fall through */
253428a6d671SEric W. Biederman 	case 1:
253528a6d671SEric W. Biederman 		ino = parent_ino(dentry);
253628a6d671SEric W. Biederman 		if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
253728a6d671SEric W. Biederman 			goto out;
253828a6d671SEric W. Biederman 		i++;
253928a6d671SEric W. Biederman 		filp->f_pos++;
254028a6d671SEric W. Biederman 		/* fall through */
254128a6d671SEric W. Biederman 	default:
254228a6d671SEric W. Biederman 		i -= 2;
254328a6d671SEric W. Biederman 		if (i >= nents) {
254428a6d671SEric W. Biederman 			ret = 1;
254528a6d671SEric W. Biederman 			goto out;
254628a6d671SEric W. Biederman 		}
254728a6d671SEric W. Biederman 		p = ents + i;
25487bcd6b0eSEric W. Biederman 		last = &ents[nents - 1];
25497bcd6b0eSEric W. Biederman 		while (p <= last) {
255061a28784SEric W. Biederman 			if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
255128a6d671SEric W. Biederman 				goto out;
255228a6d671SEric W. Biederman 			filp->f_pos++;
255328a6d671SEric W. Biederman 			p++;
255428a6d671SEric W. Biederman 		}
25551da177e4SLinus Torvalds 	}
25561da177e4SLinus Torvalds 
255728a6d671SEric W. Biederman 	ret = 1;
255828a6d671SEric W. Biederman out:
255961a28784SEric W. Biederman 	put_task_struct(task);
256061a28784SEric W. Biederman out_no_task:
256128a6d671SEric W. Biederman 	return ret;
25621da177e4SLinus Torvalds }
25631da177e4SLinus Torvalds 
25641da177e4SLinus Torvalds #ifdef CONFIG_SECURITY
256528a6d671SEric W. Biederman static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
256628a6d671SEric W. Biederman 				  size_t count, loff_t *ppos)
256728a6d671SEric W. Biederman {
25682fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
256904ff9708SAl Viro 	char *p = NULL;
257028a6d671SEric W. Biederman 	ssize_t length;
257128a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
257228a6d671SEric W. Biederman 
257328a6d671SEric W. Biederman 	if (!task)
257404ff9708SAl Viro 		return -ESRCH;
257528a6d671SEric W. Biederman 
257628a6d671SEric W. Biederman 	length = security_getprocattr(task,
25772fddfeefSJosef "Jeff" Sipek 				      (char*)file->f_path.dentry->d_name.name,
257804ff9708SAl Viro 				      &p);
257928a6d671SEric W. Biederman 	put_task_struct(task);
258004ff9708SAl Viro 	if (length > 0)
258104ff9708SAl Viro 		length = simple_read_from_buffer(buf, count, ppos, p, length);
258204ff9708SAl Viro 	kfree(p);
258328a6d671SEric W. Biederman 	return length;
258428a6d671SEric W. Biederman }
258528a6d671SEric W. Biederman 
258628a6d671SEric W. Biederman static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
258728a6d671SEric W. Biederman 				   size_t count, loff_t *ppos)
258828a6d671SEric W. Biederman {
25892fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
259028a6d671SEric W. Biederman 	char *page;
259128a6d671SEric W. Biederman 	ssize_t length;
259228a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
259328a6d671SEric W. Biederman 
259428a6d671SEric W. Biederman 	length = -ESRCH;
259528a6d671SEric W. Biederman 	if (!task)
259628a6d671SEric W. Biederman 		goto out_no_task;
259728a6d671SEric W. Biederman 	if (count > PAGE_SIZE)
259828a6d671SEric W. Biederman 		count = PAGE_SIZE;
259928a6d671SEric W. Biederman 
260028a6d671SEric W. Biederman 	/* No partial writes. */
260128a6d671SEric W. Biederman 	length = -EINVAL;
260228a6d671SEric W. Biederman 	if (*ppos != 0)
260328a6d671SEric W. Biederman 		goto out;
260428a6d671SEric W. Biederman 
260528a6d671SEric W. Biederman 	length = -ENOMEM;
2606e12ba74dSMel Gorman 	page = (char*)__get_free_page(GFP_TEMPORARY);
260728a6d671SEric W. Biederman 	if (!page)
260828a6d671SEric W. Biederman 		goto out;
260928a6d671SEric W. Biederman 
261028a6d671SEric W. Biederman 	length = -EFAULT;
261128a6d671SEric W. Biederman 	if (copy_from_user(page, buf, count))
261228a6d671SEric W. Biederman 		goto out_free;
261328a6d671SEric W. Biederman 
2614107db7c7SDavid Howells 	/* Guard against adverse ptrace interaction */
26159b1bf12dSKOSAKI Motohiro 	length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
2616107db7c7SDavid Howells 	if (length < 0)
2617107db7c7SDavid Howells 		goto out_free;
2618107db7c7SDavid Howells 
261928a6d671SEric W. Biederman 	length = security_setprocattr(task,
26202fddfeefSJosef "Jeff" Sipek 				      (char*)file->f_path.dentry->d_name.name,
262128a6d671SEric W. Biederman 				      (void*)page, count);
26229b1bf12dSKOSAKI Motohiro 	mutex_unlock(&task->signal->cred_guard_mutex);
262328a6d671SEric W. Biederman out_free:
262428a6d671SEric W. Biederman 	free_page((unsigned long) page);
262528a6d671SEric W. Biederman out:
262628a6d671SEric W. Biederman 	put_task_struct(task);
262728a6d671SEric W. Biederman out_no_task:
262828a6d671SEric W. Biederman 	return length;
262928a6d671SEric W. Biederman }
263028a6d671SEric W. Biederman 
263100977a59SArjan van de Ven static const struct file_operations proc_pid_attr_operations = {
263228a6d671SEric W. Biederman 	.read		= proc_pid_attr_read,
263328a6d671SEric W. Biederman 	.write		= proc_pid_attr_write,
263487df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
263528a6d671SEric W. Biederman };
263628a6d671SEric W. Biederman 
2637c5141e6dSEric Dumazet static const struct pid_entry attr_dir_stuff[] = {
2638631f9c18SAlexey Dobriyan 	REG("current",    S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2639631f9c18SAlexey Dobriyan 	REG("prev",       S_IRUGO,	   proc_pid_attr_operations),
2640631f9c18SAlexey Dobriyan 	REG("exec",       S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2641631f9c18SAlexey Dobriyan 	REG("fscreate",   S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2642631f9c18SAlexey Dobriyan 	REG("keycreate",  S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2643631f9c18SAlexey Dobriyan 	REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
264428a6d671SEric W. Biederman };
264528a6d671SEric W. Biederman 
264672d9dcfcSEric W. Biederman static int proc_attr_dir_readdir(struct file * filp,
26471da177e4SLinus Torvalds 			     void * dirent, filldir_t filldir)
26481da177e4SLinus Torvalds {
26491da177e4SLinus Torvalds 	return proc_pident_readdir(filp,dirent,filldir,
265072d9dcfcSEric W. Biederman 				   attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
26511da177e4SLinus Torvalds }
26521da177e4SLinus Torvalds 
265300977a59SArjan van de Ven static const struct file_operations proc_attr_dir_operations = {
26541da177e4SLinus Torvalds 	.read		= generic_read_dir,
265572d9dcfcSEric W. Biederman 	.readdir	= proc_attr_dir_readdir,
26566038f373SArnd Bergmann 	.llseek		= default_llseek,
26571da177e4SLinus Torvalds };
26581da177e4SLinus Torvalds 
265972d9dcfcSEric W. Biederman static struct dentry *proc_attr_dir_lookup(struct inode *dir,
26601da177e4SLinus Torvalds 				struct dentry *dentry, struct nameidata *nd)
26611da177e4SLinus Torvalds {
26627bcd6b0eSEric W. Biederman 	return proc_pident_lookup(dir, dentry,
26637bcd6b0eSEric W. Biederman 				  attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
26641da177e4SLinus Torvalds }
26651da177e4SLinus Torvalds 
2666c5ef1c42SArjan van de Ven static const struct inode_operations proc_attr_dir_inode_operations = {
266772d9dcfcSEric W. Biederman 	.lookup		= proc_attr_dir_lookup,
266899f89551SEric W. Biederman 	.getattr	= pid_getattr,
26696d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
26701da177e4SLinus Torvalds };
26711da177e4SLinus Torvalds 
26721da177e4SLinus Torvalds #endif
26731da177e4SLinus Torvalds 
2674698ba7b5SChristoph Hellwig #ifdef CONFIG_ELF_CORE
26753cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
26763cb4a0bbSKawai, Hidehiro 					 size_t count, loff_t *ppos)
26773cb4a0bbSKawai, Hidehiro {
26783cb4a0bbSKawai, Hidehiro 	struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
26793cb4a0bbSKawai, Hidehiro 	struct mm_struct *mm;
26803cb4a0bbSKawai, Hidehiro 	char buffer[PROC_NUMBUF];
26813cb4a0bbSKawai, Hidehiro 	size_t len;
26823cb4a0bbSKawai, Hidehiro 	int ret;
26833cb4a0bbSKawai, Hidehiro 
26843cb4a0bbSKawai, Hidehiro 	if (!task)
26853cb4a0bbSKawai, Hidehiro 		return -ESRCH;
26863cb4a0bbSKawai, Hidehiro 
26873cb4a0bbSKawai, Hidehiro 	ret = 0;
26883cb4a0bbSKawai, Hidehiro 	mm = get_task_mm(task);
26893cb4a0bbSKawai, Hidehiro 	if (mm) {
26903cb4a0bbSKawai, Hidehiro 		len = snprintf(buffer, sizeof(buffer), "%08lx\n",
26913cb4a0bbSKawai, Hidehiro 			       ((mm->flags & MMF_DUMP_FILTER_MASK) >>
26923cb4a0bbSKawai, Hidehiro 				MMF_DUMP_FILTER_SHIFT));
26933cb4a0bbSKawai, Hidehiro 		mmput(mm);
26943cb4a0bbSKawai, Hidehiro 		ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
26953cb4a0bbSKawai, Hidehiro 	}
26963cb4a0bbSKawai, Hidehiro 
26973cb4a0bbSKawai, Hidehiro 	put_task_struct(task);
26983cb4a0bbSKawai, Hidehiro 
26993cb4a0bbSKawai, Hidehiro 	return ret;
27003cb4a0bbSKawai, Hidehiro }
27013cb4a0bbSKawai, Hidehiro 
27023cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_write(struct file *file,
27033cb4a0bbSKawai, Hidehiro 					  const char __user *buf,
27043cb4a0bbSKawai, Hidehiro 					  size_t count,
27053cb4a0bbSKawai, Hidehiro 					  loff_t *ppos)
27063cb4a0bbSKawai, Hidehiro {
27073cb4a0bbSKawai, Hidehiro 	struct task_struct *task;
27083cb4a0bbSKawai, Hidehiro 	struct mm_struct *mm;
27093cb4a0bbSKawai, Hidehiro 	char buffer[PROC_NUMBUF], *end;
27103cb4a0bbSKawai, Hidehiro 	unsigned int val;
27113cb4a0bbSKawai, Hidehiro 	int ret;
27123cb4a0bbSKawai, Hidehiro 	int i;
27133cb4a0bbSKawai, Hidehiro 	unsigned long mask;
27143cb4a0bbSKawai, Hidehiro 
27153cb4a0bbSKawai, Hidehiro 	ret = -EFAULT;
27163cb4a0bbSKawai, Hidehiro 	memset(buffer, 0, sizeof(buffer));
27173cb4a0bbSKawai, Hidehiro 	if (count > sizeof(buffer) - 1)
27183cb4a0bbSKawai, Hidehiro 		count = sizeof(buffer) - 1;
27193cb4a0bbSKawai, Hidehiro 	if (copy_from_user(buffer, buf, count))
27203cb4a0bbSKawai, Hidehiro 		goto out_no_task;
27213cb4a0bbSKawai, Hidehiro 
27223cb4a0bbSKawai, Hidehiro 	ret = -EINVAL;
27233cb4a0bbSKawai, Hidehiro 	val = (unsigned int)simple_strtoul(buffer, &end, 0);
27243cb4a0bbSKawai, Hidehiro 	if (*end == '\n')
27253cb4a0bbSKawai, Hidehiro 		end++;
27263cb4a0bbSKawai, Hidehiro 	if (end - buffer == 0)
27273cb4a0bbSKawai, Hidehiro 		goto out_no_task;
27283cb4a0bbSKawai, Hidehiro 
27293cb4a0bbSKawai, Hidehiro 	ret = -ESRCH;
27303cb4a0bbSKawai, Hidehiro 	task = get_proc_task(file->f_dentry->d_inode);
27313cb4a0bbSKawai, Hidehiro 	if (!task)
27323cb4a0bbSKawai, Hidehiro 		goto out_no_task;
27333cb4a0bbSKawai, Hidehiro 
27343cb4a0bbSKawai, Hidehiro 	ret = end - buffer;
27353cb4a0bbSKawai, Hidehiro 	mm = get_task_mm(task);
27363cb4a0bbSKawai, Hidehiro 	if (!mm)
27373cb4a0bbSKawai, Hidehiro 		goto out_no_mm;
27383cb4a0bbSKawai, Hidehiro 
27393cb4a0bbSKawai, Hidehiro 	for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
27403cb4a0bbSKawai, Hidehiro 		if (val & mask)
27413cb4a0bbSKawai, Hidehiro 			set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
27423cb4a0bbSKawai, Hidehiro 		else
27433cb4a0bbSKawai, Hidehiro 			clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
27443cb4a0bbSKawai, Hidehiro 	}
27453cb4a0bbSKawai, Hidehiro 
27463cb4a0bbSKawai, Hidehiro 	mmput(mm);
27473cb4a0bbSKawai, Hidehiro  out_no_mm:
27483cb4a0bbSKawai, Hidehiro 	put_task_struct(task);
27493cb4a0bbSKawai, Hidehiro  out_no_task:
27503cb4a0bbSKawai, Hidehiro 	return ret;
27513cb4a0bbSKawai, Hidehiro }
27523cb4a0bbSKawai, Hidehiro 
27533cb4a0bbSKawai, Hidehiro static const struct file_operations proc_coredump_filter_operations = {
27543cb4a0bbSKawai, Hidehiro 	.read		= proc_coredump_filter_read,
27553cb4a0bbSKawai, Hidehiro 	.write		= proc_coredump_filter_write,
275687df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
27573cb4a0bbSKawai, Hidehiro };
27583cb4a0bbSKawai, Hidehiro #endif
27593cb4a0bbSKawai, Hidehiro 
27601da177e4SLinus Torvalds /*
27611da177e4SLinus Torvalds  * /proc/self:
27621da177e4SLinus Torvalds  */
27631da177e4SLinus Torvalds static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
27641da177e4SLinus Torvalds 			      int buflen)
27651da177e4SLinus Torvalds {
2766488e5bc4SEric W. Biederman 	struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2767b55fcb22SAndrew Morton 	pid_t tgid = task_tgid_nr_ns(current, ns);
27688578cea7SEric W. Biederman 	char tmp[PROC_NUMBUF];
2769b55fcb22SAndrew Morton 	if (!tgid)
2770488e5bc4SEric W. Biederman 		return -ENOENT;
2771b55fcb22SAndrew Morton 	sprintf(tmp, "%d", tgid);
27721da177e4SLinus Torvalds 	return vfs_readlink(dentry,buffer,buflen,tmp);
27731da177e4SLinus Torvalds }
27741da177e4SLinus Torvalds 
2775008b150aSAl Viro static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
27761da177e4SLinus Torvalds {
2777488e5bc4SEric W. Biederman 	struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2778b55fcb22SAndrew Morton 	pid_t tgid = task_tgid_nr_ns(current, ns);
27797fee4868SAl Viro 	char *name = ERR_PTR(-ENOENT);
27807fee4868SAl Viro 	if (tgid) {
27817fee4868SAl Viro 		name = __getname();
27827fee4868SAl Viro 		if (!name)
27837fee4868SAl Viro 			name = ERR_PTR(-ENOMEM);
27847fee4868SAl Viro 		else
27857fee4868SAl Viro 			sprintf(name, "%d", tgid);
27867fee4868SAl Viro 	}
27877fee4868SAl Viro 	nd_set_link(nd, name);
27887fee4868SAl Viro 	return NULL;
27897fee4868SAl Viro }
27907fee4868SAl Viro 
27917fee4868SAl Viro static void proc_self_put_link(struct dentry *dentry, struct nameidata *nd,
27927fee4868SAl Viro 				void *cookie)
27937fee4868SAl Viro {
27947fee4868SAl Viro 	char *s = nd_get_link(nd);
27957fee4868SAl Viro 	if (!IS_ERR(s))
27967fee4868SAl Viro 		__putname(s);
27971da177e4SLinus Torvalds }
27981da177e4SLinus Torvalds 
2799c5ef1c42SArjan van de Ven static const struct inode_operations proc_self_inode_operations = {
28001da177e4SLinus Torvalds 	.readlink	= proc_self_readlink,
28011da177e4SLinus Torvalds 	.follow_link	= proc_self_follow_link,
28027fee4868SAl Viro 	.put_link	= proc_self_put_link,
28031da177e4SLinus Torvalds };
28041da177e4SLinus Torvalds 
280528a6d671SEric W. Biederman /*
2806801199ceSEric W. Biederman  * proc base
2807801199ceSEric W. Biederman  *
2808801199ceSEric W. Biederman  * These are the directory entries in the root directory of /proc
2809801199ceSEric W. Biederman  * that properly belong to the /proc filesystem, as they describe
2810801199ceSEric W. Biederman  * describe something that is process related.
2811801199ceSEric W. Biederman  */
2812c5141e6dSEric Dumazet static const struct pid_entry proc_base_stuff[] = {
281361a28784SEric W. Biederman 	NOD("self", S_IFLNK|S_IRWXUGO,
2814801199ceSEric W. Biederman 		&proc_self_inode_operations, NULL, {}),
2815801199ceSEric W. Biederman };
2816801199ceSEric W. Biederman 
2817444ceed8SEric W. Biederman static struct dentry *proc_base_instantiate(struct inode *dir,
2818c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
2819801199ceSEric W. Biederman {
2820c5141e6dSEric Dumazet 	const struct pid_entry *p = ptr;
2821801199ceSEric W. Biederman 	struct inode *inode;
2822801199ceSEric W. Biederman 	struct proc_inode *ei;
282373d36460SDan Carpenter 	struct dentry *error;
2824801199ceSEric W. Biederman 
2825801199ceSEric W. Biederman 	/* Allocate the inode */
2826801199ceSEric W. Biederman 	error = ERR_PTR(-ENOMEM);
2827801199ceSEric W. Biederman 	inode = new_inode(dir->i_sb);
2828801199ceSEric W. Biederman 	if (!inode)
2829801199ceSEric W. Biederman 		goto out;
2830801199ceSEric W. Biederman 
2831801199ceSEric W. Biederman 	/* Initialize the inode */
2832801199ceSEric W. Biederman 	ei = PROC_I(inode);
283385fe4025SChristoph Hellwig 	inode->i_ino = get_next_ino();
2834801199ceSEric W. Biederman 	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2835801199ceSEric W. Biederman 
2836801199ceSEric W. Biederman 	/*
2837801199ceSEric W. Biederman 	 * grab the reference to the task.
2838801199ceSEric W. Biederman 	 */
28391a657f78SOleg Nesterov 	ei->pid = get_task_pid(task, PIDTYPE_PID);
2840801199ceSEric W. Biederman 	if (!ei->pid)
2841801199ceSEric W. Biederman 		goto out_iput;
2842801199ceSEric W. Biederman 
2843801199ceSEric W. Biederman 	inode->i_mode = p->mode;
2844801199ceSEric W. Biederman 	if (S_ISDIR(inode->i_mode))
2845bfe86848SMiklos Szeredi 		set_nlink(inode, 2);
2846801199ceSEric W. Biederman 	if (S_ISLNK(inode->i_mode))
2847801199ceSEric W. Biederman 		inode->i_size = 64;
2848801199ceSEric W. Biederman 	if (p->iop)
2849801199ceSEric W. Biederman 		inode->i_op = p->iop;
2850801199ceSEric W. Biederman 	if (p->fop)
2851801199ceSEric W. Biederman 		inode->i_fop = p->fop;
2852801199ceSEric W. Biederman 	ei->op = p->op;
2853801199ceSEric W. Biederman 	d_add(dentry, inode);
2854801199ceSEric W. Biederman 	error = NULL;
2855801199ceSEric W. Biederman out:
2856801199ceSEric W. Biederman 	return error;
2857801199ceSEric W. Biederman out_iput:
2858801199ceSEric W. Biederman 	iput(inode);
2859801199ceSEric W. Biederman 	goto out;
2860801199ceSEric W. Biederman }
2861801199ceSEric W. Biederman 
2862444ceed8SEric W. Biederman static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2863444ceed8SEric W. Biederman {
2864444ceed8SEric W. Biederman 	struct dentry *error;
2865444ceed8SEric W. Biederman 	struct task_struct *task = get_proc_task(dir);
2866c5141e6dSEric Dumazet 	const struct pid_entry *p, *last;
2867444ceed8SEric W. Biederman 
2868444ceed8SEric W. Biederman 	error = ERR_PTR(-ENOENT);
2869444ceed8SEric W. Biederman 
2870444ceed8SEric W. Biederman 	if (!task)
2871444ceed8SEric W. Biederman 		goto out_no_task;
2872444ceed8SEric W. Biederman 
2873444ceed8SEric W. Biederman 	/* Lookup the directory entry */
28747bcd6b0eSEric W. Biederman 	last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
28757bcd6b0eSEric W. Biederman 	for (p = proc_base_stuff; p <= last; p++) {
2876444ceed8SEric W. Biederman 		if (p->len != dentry->d_name.len)
2877444ceed8SEric W. Biederman 			continue;
2878444ceed8SEric W. Biederman 		if (!memcmp(dentry->d_name.name, p->name, p->len))
2879444ceed8SEric W. Biederman 			break;
2880444ceed8SEric W. Biederman 	}
28817bcd6b0eSEric W. Biederman 	if (p > last)
2882444ceed8SEric W. Biederman 		goto out;
2883444ceed8SEric W. Biederman 
2884444ceed8SEric W. Biederman 	error = proc_base_instantiate(dir, dentry, task, p);
2885444ceed8SEric W. Biederman 
2886444ceed8SEric W. Biederman out:
2887444ceed8SEric W. Biederman 	put_task_struct(task);
2888444ceed8SEric W. Biederman out_no_task:
2889444ceed8SEric W. Biederman 	return error;
2890444ceed8SEric W. Biederman }
2891444ceed8SEric W. Biederman 
2892c5141e6dSEric Dumazet static int proc_base_fill_cache(struct file *filp, void *dirent,
2893c5141e6dSEric Dumazet 	filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
289461a28784SEric W. Biederman {
289561a28784SEric W. Biederman 	return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
289661a28784SEric W. Biederman 				proc_base_instantiate, task, p);
289761a28784SEric W. Biederman }
289861a28784SEric W. Biederman 
2899aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING
2900297c5d92SAndrea Righi static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
2901aba76fdbSAndrew Morton {
2902940389b8SAndrea Righi 	struct task_io_accounting acct = task->ioac;
2903297c5d92SAndrea Righi 	unsigned long flags;
2904293eb1e7SVasiliy Kulikov 	int result;
2905297c5d92SAndrea Righi 
2906293eb1e7SVasiliy Kulikov 	result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2907293eb1e7SVasiliy Kulikov 	if (result)
2908293eb1e7SVasiliy Kulikov 		return result;
2909293eb1e7SVasiliy Kulikov 
2910293eb1e7SVasiliy Kulikov 	if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
2911293eb1e7SVasiliy Kulikov 		result = -EACCES;
2912293eb1e7SVasiliy Kulikov 		goto out_unlock;
2913293eb1e7SVasiliy Kulikov 	}
29141d1221f3SVasiliy Kulikov 
29155995477aSAndrea Righi 	if (whole && lock_task_sighand(task, &flags)) {
2916b2d002dbSAndrea Righi 		struct task_struct *t = task;
2917297c5d92SAndrea Righi 
29185995477aSAndrea Righi 		task_io_accounting_add(&acct, &task->signal->ioac);
29195995477aSAndrea Righi 		while_each_thread(task, t)
29205995477aSAndrea Righi 			task_io_accounting_add(&acct, &t->ioac);
2921297c5d92SAndrea Righi 
2922297c5d92SAndrea Righi 		unlock_task_sighand(task, &flags);
2923297c5d92SAndrea Righi 	}
2924293eb1e7SVasiliy Kulikov 	result = sprintf(buffer,
2925aba76fdbSAndrew Morton 			"rchar: %llu\n"
2926aba76fdbSAndrew Morton 			"wchar: %llu\n"
2927aba76fdbSAndrew Morton 			"syscr: %llu\n"
2928aba76fdbSAndrew Morton 			"syscw: %llu\n"
2929aba76fdbSAndrew Morton 			"read_bytes: %llu\n"
2930aba76fdbSAndrew Morton 			"write_bytes: %llu\n"
2931aba76fdbSAndrew Morton 			"cancelled_write_bytes: %llu\n",
29327c44319dSAlexander Beregalov 			(unsigned long long)acct.rchar,
29337c44319dSAlexander Beregalov 			(unsigned long long)acct.wchar,
29347c44319dSAlexander Beregalov 			(unsigned long long)acct.syscr,
29357c44319dSAlexander Beregalov 			(unsigned long long)acct.syscw,
29367c44319dSAlexander Beregalov 			(unsigned long long)acct.read_bytes,
29377c44319dSAlexander Beregalov 			(unsigned long long)acct.write_bytes,
29387c44319dSAlexander Beregalov 			(unsigned long long)acct.cancelled_write_bytes);
2939293eb1e7SVasiliy Kulikov out_unlock:
2940293eb1e7SVasiliy Kulikov 	mutex_unlock(&task->signal->cred_guard_mutex);
2941293eb1e7SVasiliy Kulikov 	return result;
2942aba76fdbSAndrew Morton }
2943297c5d92SAndrea Righi 
2944297c5d92SAndrea Righi static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2945297c5d92SAndrea Righi {
2946297c5d92SAndrea Righi 	return do_io_accounting(task, buffer, 0);
2947297c5d92SAndrea Righi }
2948297c5d92SAndrea Righi 
2949297c5d92SAndrea Righi static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2950297c5d92SAndrea Righi {
2951297c5d92SAndrea Righi 	return do_io_accounting(task, buffer, 1);
2952297c5d92SAndrea Righi }
2953297c5d92SAndrea Righi #endif /* CONFIG_TASK_IO_ACCOUNTING */
2954aba76fdbSAndrew Morton 
295547830723SKees Cook static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
295647830723SKees Cook 				struct pid *pid, struct task_struct *task)
295747830723SKees Cook {
2958a9712bc1SAl Viro 	int err = lock_trace(task);
2959a9712bc1SAl Viro 	if (!err) {
296047830723SKees Cook 		seq_printf(m, "%08x\n", task->personality);
2961a9712bc1SAl Viro 		unlock_trace(task);
2962a9712bc1SAl Viro 	}
2963a9712bc1SAl Viro 	return err;
296447830723SKees Cook }
296547830723SKees Cook 
2966801199ceSEric W. Biederman /*
296728a6d671SEric W. Biederman  * Thread groups
296828a6d671SEric W. Biederman  */
296900977a59SArjan van de Ven static const struct file_operations proc_task_operations;
2970c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations;
297120cdc894SEric W. Biederman 
2972c5141e6dSEric Dumazet static const struct pid_entry tgid_base_stuff[] = {
2973631f9c18SAlexey Dobriyan 	DIR("task",       S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2974631f9c18SAlexey Dobriyan 	DIR("fd",         S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2975640708a2SPavel Emelyanov #ifdef CONFIG_CHECKPOINT_RESTORE
2976640708a2SPavel Emelyanov 	DIR("map_files",  S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
2977640708a2SPavel Emelyanov #endif
2978631f9c18SAlexey Dobriyan 	DIR("fdinfo",     S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
29796b4e306aSEric W. Biederman 	DIR("ns",	  S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
2980b2211a36SAndrew Morton #ifdef CONFIG_NET
2981631f9c18SAlexey Dobriyan 	DIR("net",        S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2982b2211a36SAndrew Morton #endif
2983631f9c18SAlexey Dobriyan 	REG("environ",    S_IRUSR, proc_environ_operations),
2984631f9c18SAlexey Dobriyan 	INF("auxv",       S_IRUSR, proc_pid_auxv),
2985631f9c18SAlexey Dobriyan 	ONE("status",     S_IRUGO, proc_pid_status),
2986a9712bc1SAl Viro 	ONE("personality", S_IRUGO, proc_pid_personality),
29873036e7b4SJiri Olsa 	INF("limits",	  S_IRUGO, proc_pid_limits),
298843ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
2989631f9c18SAlexey Dobriyan 	REG("sched",      S_IRUGO|S_IWUSR, proc_pid_sched_operations),
299043ae34cbSIngo Molnar #endif
29915091faa4SMike Galbraith #ifdef CONFIG_SCHED_AUTOGROUP
29925091faa4SMike Galbraith 	REG("autogroup",  S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
29935091faa4SMike Galbraith #endif
29944614a696Sjohn stultz 	REG("comm",      S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2995ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2996a9712bc1SAl Viro 	INF("syscall",    S_IRUGO, proc_pid_syscall),
2997ebcb6734SRoland McGrath #endif
2998631f9c18SAlexey Dobriyan 	INF("cmdline",    S_IRUGO, proc_pid_cmdline),
2999631f9c18SAlexey Dobriyan 	ONE("stat",       S_IRUGO, proc_tgid_stat),
3000631f9c18SAlexey Dobriyan 	ONE("statm",      S_IRUGO, proc_pid_statm),
3001631f9c18SAlexey Dobriyan 	REG("maps",       S_IRUGO, proc_maps_operations),
300228a6d671SEric W. Biederman #ifdef CONFIG_NUMA
3003631f9c18SAlexey Dobriyan 	REG("numa_maps",  S_IRUGO, proc_numa_maps_operations),
300428a6d671SEric W. Biederman #endif
3005631f9c18SAlexey Dobriyan 	REG("mem",        S_IRUSR|S_IWUSR, proc_mem_operations),
3006631f9c18SAlexey Dobriyan 	LNK("cwd",        proc_cwd_link),
3007631f9c18SAlexey Dobriyan 	LNK("root",       proc_root_link),
3008631f9c18SAlexey Dobriyan 	LNK("exe",        proc_exe_link),
3009631f9c18SAlexey Dobriyan 	REG("mounts",     S_IRUGO, proc_mounts_operations),
3010631f9c18SAlexey Dobriyan 	REG("mountinfo",  S_IRUGO, proc_mountinfo_operations),
3011631f9c18SAlexey Dobriyan 	REG("mountstats", S_IRUSR, proc_mountstats_operations),
30121e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR
3013631f9c18SAlexey Dobriyan 	REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3014631f9c18SAlexey Dobriyan 	REG("smaps",      S_IRUGO, proc_smaps_operations),
3015ca6b0bf0SAl Viro 	REG("pagemap",    S_IRUGO, proc_pagemap_operations),
301628a6d671SEric W. Biederman #endif
301728a6d671SEric W. Biederman #ifdef CONFIG_SECURITY
3018631f9c18SAlexey Dobriyan 	DIR("attr",       S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
301928a6d671SEric W. Biederman #endif
302028a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS
3021631f9c18SAlexey Dobriyan 	INF("wchan",      S_IRUGO, proc_pid_wchan),
302228a6d671SEric W. Biederman #endif
30232ec220e2SKen Chen #ifdef CONFIG_STACKTRACE
3024a9712bc1SAl Viro 	ONE("stack",      S_IRUGO, proc_pid_stack),
302528a6d671SEric W. Biederman #endif
302628a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS
3027631f9c18SAlexey Dobriyan 	INF("schedstat",  S_IRUGO, proc_pid_schedstat),
302828a6d671SEric W. Biederman #endif
30299745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP
3030631f9c18SAlexey Dobriyan 	REG("latency",  S_IRUGO, proc_lstats_operations),
30319745512cSArjan van de Ven #endif
30328793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET
3033631f9c18SAlexey Dobriyan 	REG("cpuset",     S_IRUGO, proc_cpuset_operations),
303428a6d671SEric W. Biederman #endif
3035a424316cSPaul Menage #ifdef CONFIG_CGROUPS
3036631f9c18SAlexey Dobriyan 	REG("cgroup",  S_IRUGO, proc_cgroup_operations),
3037a424316cSPaul Menage #endif
3038631f9c18SAlexey Dobriyan 	INF("oom_score",  S_IRUGO, proc_oom_score),
3039631f9c18SAlexey Dobriyan 	REG("oom_adj",    S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
3040a63d83f4SDavid Rientjes 	REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
304128a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL
3042631f9c18SAlexey Dobriyan 	REG("loginuid",   S_IWUSR|S_IRUGO, proc_loginuid_operations),
3043631f9c18SAlexey Dobriyan 	REG("sessionid",  S_IRUGO, proc_sessionid_operations),
304428a6d671SEric W. Biederman #endif
3045f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
3046631f9c18SAlexey Dobriyan 	REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3047f4f154fdSAkinobu Mita #endif
3048698ba7b5SChristoph Hellwig #ifdef CONFIG_ELF_CORE
3049631f9c18SAlexey Dobriyan 	REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
30503cb4a0bbSKawai, Hidehiro #endif
3051aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING
30521d1221f3SVasiliy Kulikov 	INF("io",	S_IRUSR, proc_tgid_io_accounting),
3053aba76fdbSAndrew Morton #endif
3054f133eccaSChris Metcalf #ifdef CONFIG_HARDWALL
3055f133eccaSChris Metcalf 	INF("hardwall",   S_IRUGO, proc_pid_hardwall),
3056f133eccaSChris Metcalf #endif
305728a6d671SEric W. Biederman };
305828a6d671SEric W. Biederman 
305928a6d671SEric W. Biederman static int proc_tgid_base_readdir(struct file * filp,
306028a6d671SEric W. Biederman 			     void * dirent, filldir_t filldir)
306128a6d671SEric W. Biederman {
306228a6d671SEric W. Biederman 	return proc_pident_readdir(filp,dirent,filldir,
306328a6d671SEric W. Biederman 				   tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
306428a6d671SEric W. Biederman }
306528a6d671SEric W. Biederman 
306600977a59SArjan van de Ven static const struct file_operations proc_tgid_base_operations = {
306728a6d671SEric W. Biederman 	.read		= generic_read_dir,
306828a6d671SEric W. Biederman 	.readdir	= proc_tgid_base_readdir,
30696038f373SArnd Bergmann 	.llseek		= default_llseek,
307028a6d671SEric W. Biederman };
307128a6d671SEric W. Biederman 
307228a6d671SEric W. Biederman static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
30737bcd6b0eSEric W. Biederman 	return proc_pident_lookup(dir, dentry,
30747bcd6b0eSEric W. Biederman 				  tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
307528a6d671SEric W. Biederman }
307628a6d671SEric W. Biederman 
3077c5ef1c42SArjan van de Ven static const struct inode_operations proc_tgid_base_inode_operations = {
307828a6d671SEric W. Biederman 	.lookup		= proc_tgid_base_lookup,
307928a6d671SEric W. Biederman 	.getattr	= pid_getattr,
308028a6d671SEric W. Biederman 	.setattr	= proc_setattr,
30810499680aSVasiliy Kulikov 	.permission	= proc_pid_permission,
308228a6d671SEric W. Biederman };
308328a6d671SEric W. Biederman 
308460347f67SPavel Emelyanov static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
30851da177e4SLinus Torvalds {
308648e6484dSEric W. Biederman 	struct dentry *dentry, *leader, *dir;
30878578cea7SEric W. Biederman 	char buf[PROC_NUMBUF];
308848e6484dSEric W. Biederman 	struct qstr name;
30891da177e4SLinus Torvalds 
309048e6484dSEric W. Biederman 	name.name = buf;
309160347f67SPavel Emelyanov 	name.len = snprintf(buf, sizeof(buf), "%d", pid);
309260347f67SPavel Emelyanov 	dentry = d_hash_and_lookup(mnt->mnt_root, &name);
309348e6484dSEric W. Biederman 	if (dentry) {
309448e6484dSEric W. Biederman 		shrink_dcache_parent(dentry);
309548e6484dSEric W. Biederman 		d_drop(dentry);
309648e6484dSEric W. Biederman 		dput(dentry);
30971da177e4SLinus Torvalds 	}
30981da177e4SLinus Torvalds 
309948e6484dSEric W. Biederman 	name.name = buf;
310060347f67SPavel Emelyanov 	name.len = snprintf(buf, sizeof(buf), "%d", tgid);
310160347f67SPavel Emelyanov 	leader = d_hash_and_lookup(mnt->mnt_root, &name);
310248e6484dSEric W. Biederman 	if (!leader)
310348e6484dSEric W. Biederman 		goto out;
310448e6484dSEric W. Biederman 
310548e6484dSEric W. Biederman 	name.name = "task";
310648e6484dSEric W. Biederman 	name.len = strlen(name.name);
310748e6484dSEric W. Biederman 	dir = d_hash_and_lookup(leader, &name);
310848e6484dSEric W. Biederman 	if (!dir)
310948e6484dSEric W. Biederman 		goto out_put_leader;
311048e6484dSEric W. Biederman 
311148e6484dSEric W. Biederman 	name.name = buf;
311260347f67SPavel Emelyanov 	name.len = snprintf(buf, sizeof(buf), "%d", pid);
311348e6484dSEric W. Biederman 	dentry = d_hash_and_lookup(dir, &name);
311448e6484dSEric W. Biederman 	if (dentry) {
311548e6484dSEric W. Biederman 		shrink_dcache_parent(dentry);
311648e6484dSEric W. Biederman 		d_drop(dentry);
311748e6484dSEric W. Biederman 		dput(dentry);
31181da177e4SLinus Torvalds 	}
311948e6484dSEric W. Biederman 
312048e6484dSEric W. Biederman 	dput(dir);
312148e6484dSEric W. Biederman out_put_leader:
312248e6484dSEric W. Biederman 	dput(leader);
312348e6484dSEric W. Biederman out:
312448e6484dSEric W. Biederman 	return;
31251da177e4SLinus Torvalds }
31261da177e4SLinus Torvalds 
31270895e91dSRandy Dunlap /**
31280895e91dSRandy Dunlap  * proc_flush_task -  Remove dcache entries for @task from the /proc dcache.
31290895e91dSRandy Dunlap  * @task: task that should be flushed.
31300895e91dSRandy Dunlap  *
31310895e91dSRandy Dunlap  * When flushing dentries from proc, one needs to flush them from global
313260347f67SPavel Emelyanov  * proc (proc_mnt) and from all the namespaces' procs this task was seen
31330895e91dSRandy Dunlap  * in. This call is supposed to do all of this job.
31340895e91dSRandy Dunlap  *
31350895e91dSRandy Dunlap  * Looks in the dcache for
31360895e91dSRandy Dunlap  * /proc/@pid
31370895e91dSRandy Dunlap  * /proc/@tgid/task/@pid
31380895e91dSRandy Dunlap  * if either directory is present flushes it and all of it'ts children
31390895e91dSRandy Dunlap  * from the dcache.
31400895e91dSRandy Dunlap  *
31410895e91dSRandy Dunlap  * It is safe and reasonable to cache /proc entries for a task until
31420895e91dSRandy Dunlap  * that task exits.  After that they just clog up the dcache with
31430895e91dSRandy Dunlap  * useless entries, possibly causing useful dcache entries to be
31440895e91dSRandy Dunlap  * flushed instead.  This routine is proved to flush those useless
31450895e91dSRandy Dunlap  * dcache entries at process exit time.
31460895e91dSRandy Dunlap  *
31470895e91dSRandy Dunlap  * NOTE: This routine is just an optimization so it does not guarantee
31480895e91dSRandy Dunlap  *       that no dcache entries will exist at process exit time it
31490895e91dSRandy Dunlap  *       just makes it very unlikely that any will persist.
315060347f67SPavel Emelyanov  */
315160347f67SPavel Emelyanov 
315260347f67SPavel Emelyanov void proc_flush_task(struct task_struct *task)
315360347f67SPavel Emelyanov {
31549fcc2d15SEric W. Biederman 	int i;
31559b4d1cbeSOleg Nesterov 	struct pid *pid, *tgid;
3156130f77ecSPavel Emelyanov 	struct upid *upid;
3157130f77ecSPavel Emelyanov 
3158130f77ecSPavel Emelyanov 	pid = task_pid(task);
3159130f77ecSPavel Emelyanov 	tgid = task_tgid(task);
31609fcc2d15SEric W. Biederman 
31619fcc2d15SEric W. Biederman 	for (i = 0; i <= pid->level; i++) {
3162130f77ecSPavel Emelyanov 		upid = &pid->numbers[i];
3163130f77ecSPavel Emelyanov 		proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
31649b4d1cbeSOleg Nesterov 					tgid->numbers[i].nr);
3165130f77ecSPavel Emelyanov 	}
31666f4e6433SPavel Emelyanov 
31676f4e6433SPavel Emelyanov 	upid = &pid->numbers[pid->level];
31686f4e6433SPavel Emelyanov 	if (upid->nr == 1)
31696f4e6433SPavel Emelyanov 		pid_ns_release_proc(upid->ns);
317060347f67SPavel Emelyanov }
317160347f67SPavel Emelyanov 
31729711ef99SAdrian Bunk static struct dentry *proc_pid_instantiate(struct inode *dir,
31739711ef99SAdrian Bunk 					   struct dentry * dentry,
3174c5141e6dSEric Dumazet 					   struct task_struct *task, const void *ptr)
3175444ceed8SEric W. Biederman {
3176444ceed8SEric W. Biederman 	struct dentry *error = ERR_PTR(-ENOENT);
3177444ceed8SEric W. Biederman 	struct inode *inode;
3178444ceed8SEric W. Biederman 
317961a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
3180444ceed8SEric W. Biederman 	if (!inode)
3181444ceed8SEric W. Biederman 		goto out;
3182444ceed8SEric W. Biederman 
3183444ceed8SEric W. Biederman 	inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3184444ceed8SEric W. Biederman 	inode->i_op = &proc_tgid_base_inode_operations;
3185444ceed8SEric W. Biederman 	inode->i_fop = &proc_tgid_base_operations;
3186444ceed8SEric W. Biederman 	inode->i_flags|=S_IMMUTABLE;
3187aed54175SVegard Nossum 
3188bfe86848SMiklos Szeredi 	set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
3189bfe86848SMiklos Szeredi 						  ARRAY_SIZE(tgid_base_stuff)));
3190444ceed8SEric W. Biederman 
3191fb045adbSNick Piggin 	d_set_d_op(dentry, &pid_dentry_operations);
3192444ceed8SEric W. Biederman 
3193444ceed8SEric W. Biederman 	d_add(dentry, inode);
3194444ceed8SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
3195444ceed8SEric W. Biederman 	if (pid_revalidate(dentry, NULL))
3196444ceed8SEric W. Biederman 		error = NULL;
3197444ceed8SEric W. Biederman out:
3198444ceed8SEric W. Biederman 	return error;
3199444ceed8SEric W. Biederman }
3200444ceed8SEric W. Biederman 
32011da177e4SLinus Torvalds struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
32021da177e4SLinus Torvalds {
320373d36460SDan Carpenter 	struct dentry *result;
32041da177e4SLinus Torvalds 	struct task_struct *task;
32051da177e4SLinus Torvalds 	unsigned tgid;
3206b488893aSPavel Emelyanov 	struct pid_namespace *ns;
32071da177e4SLinus Torvalds 
3208801199ceSEric W. Biederman 	result = proc_base_lookup(dir, dentry);
3209801199ceSEric W. Biederman 	if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
3210801199ceSEric W. Biederman 		goto out;
3211801199ceSEric W. Biederman 
32121da177e4SLinus Torvalds 	tgid = name_to_int(dentry);
32131da177e4SLinus Torvalds 	if (tgid == ~0U)
32141da177e4SLinus Torvalds 		goto out;
32151da177e4SLinus Torvalds 
3216b488893aSPavel Emelyanov 	ns = dentry->d_sb->s_fs_info;
3217de758734SEric W. Biederman 	rcu_read_lock();
3218b488893aSPavel Emelyanov 	task = find_task_by_pid_ns(tgid, ns);
32191da177e4SLinus Torvalds 	if (task)
32201da177e4SLinus Torvalds 		get_task_struct(task);
3221de758734SEric W. Biederman 	rcu_read_unlock();
32221da177e4SLinus Torvalds 	if (!task)
32231da177e4SLinus Torvalds 		goto out;
32241da177e4SLinus Torvalds 
3225444ceed8SEric W. Biederman 	result = proc_pid_instantiate(dir, dentry, task, NULL);
322648e6484dSEric W. Biederman 	put_task_struct(task);
32271da177e4SLinus Torvalds out:
3228cd6a3ce9SEric W. Biederman 	return result;
32291da177e4SLinus Torvalds }
32301da177e4SLinus Torvalds 
32311da177e4SLinus Torvalds /*
32320804ef4bSEric W. Biederman  * Find the first task with tgid >= tgid
32330bc58a91SEric W. Biederman  *
32341da177e4SLinus Torvalds  */
323519fd4bb2SEric W. Biederman struct tgid_iter {
323619fd4bb2SEric W. Biederman 	unsigned int tgid;
32370804ef4bSEric W. Biederman 	struct task_struct *task;
323819fd4bb2SEric W. Biederman };
323919fd4bb2SEric W. Biederman static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
324019fd4bb2SEric W. Biederman {
32410804ef4bSEric W. Biederman 	struct pid *pid;
32421da177e4SLinus Torvalds 
324319fd4bb2SEric W. Biederman 	if (iter.task)
324419fd4bb2SEric W. Biederman 		put_task_struct(iter.task);
32450804ef4bSEric W. Biederman 	rcu_read_lock();
32460804ef4bSEric W. Biederman retry:
324719fd4bb2SEric W. Biederman 	iter.task = NULL;
324819fd4bb2SEric W. Biederman 	pid = find_ge_pid(iter.tgid, ns);
32490804ef4bSEric W. Biederman 	if (pid) {
325019fd4bb2SEric W. Biederman 		iter.tgid = pid_nr_ns(pid, ns);
325119fd4bb2SEric W. Biederman 		iter.task = pid_task(pid, PIDTYPE_PID);
32520804ef4bSEric W. Biederman 		/* What we to know is if the pid we have find is the
32530804ef4bSEric W. Biederman 		 * pid of a thread_group_leader.  Testing for task
32540804ef4bSEric W. Biederman 		 * being a thread_group_leader is the obvious thing
32550804ef4bSEric W. Biederman 		 * todo but there is a window when it fails, due to
32560804ef4bSEric W. Biederman 		 * the pid transfer logic in de_thread.
32570804ef4bSEric W. Biederman 		 *
32580804ef4bSEric W. Biederman 		 * So we perform the straight forward test of seeing
32590804ef4bSEric W. Biederman 		 * if the pid we have found is the pid of a thread
32600804ef4bSEric W. Biederman 		 * group leader, and don't worry if the task we have
32610804ef4bSEric W. Biederman 		 * found doesn't happen to be a thread group leader.
32620804ef4bSEric W. Biederman 		 * As we don't care in the case of readdir.
32630bc58a91SEric W. Biederman 		 */
326419fd4bb2SEric W. Biederman 		if (!iter.task || !has_group_leader_pid(iter.task)) {
326519fd4bb2SEric W. Biederman 			iter.tgid += 1;
32660804ef4bSEric W. Biederman 			goto retry;
326719fd4bb2SEric W. Biederman 		}
326819fd4bb2SEric W. Biederman 		get_task_struct(iter.task);
32691da177e4SLinus Torvalds 	}
3270454cc105SEric W. Biederman 	rcu_read_unlock();
327119fd4bb2SEric W. Biederman 	return iter;
32721da177e4SLinus Torvalds }
32731da177e4SLinus Torvalds 
32747bcd6b0eSEric W. Biederman #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
32751da177e4SLinus Torvalds 
327661a28784SEric W. Biederman static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
327719fd4bb2SEric W. Biederman 	struct tgid_iter iter)
327861a28784SEric W. Biederman {
327961a28784SEric W. Biederman 	char name[PROC_NUMBUF];
328019fd4bb2SEric W. Biederman 	int len = snprintf(name, sizeof(name), "%d", iter.tgid);
328161a28784SEric W. Biederman 	return proc_fill_cache(filp, dirent, filldir, name, len,
328219fd4bb2SEric W. Biederman 				proc_pid_instantiate, iter.task, NULL);
328361a28784SEric W. Biederman }
328461a28784SEric W. Biederman 
32850499680aSVasiliy Kulikov static int fake_filldir(void *buf, const char *name, int namelen,
32860499680aSVasiliy Kulikov 			loff_t offset, u64 ino, unsigned d_type)
32870499680aSVasiliy Kulikov {
32880499680aSVasiliy Kulikov 	return 0;
32890499680aSVasiliy Kulikov }
32900499680aSVasiliy Kulikov 
32911da177e4SLinus Torvalds /* for the /proc/ directory itself, after non-process stuff has been done */
32921da177e4SLinus Torvalds int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
32931da177e4SLinus Torvalds {
3294d8bdc59fSLinus Torvalds 	unsigned int nr;
3295d8bdc59fSLinus Torvalds 	struct task_struct *reaper;
329619fd4bb2SEric W. Biederman 	struct tgid_iter iter;
3297b488893aSPavel Emelyanov 	struct pid_namespace *ns;
32980499680aSVasiliy Kulikov 	filldir_t __filldir;
32991da177e4SLinus Torvalds 
3300d8bdc59fSLinus Torvalds 	if (filp->f_pos >= PID_MAX_LIMIT + TGID_OFFSET)
3301d8bdc59fSLinus Torvalds 		goto out_no_task;
3302d8bdc59fSLinus Torvalds 	nr = filp->f_pos - FIRST_PROCESS_ENTRY;
3303d8bdc59fSLinus Torvalds 
3304d8bdc59fSLinus Torvalds 	reaper = get_proc_task(filp->f_path.dentry->d_inode);
330561a28784SEric W. Biederman 	if (!reaper)
330661a28784SEric W. Biederman 		goto out_no_task;
330761a28784SEric W. Biederman 
33087bcd6b0eSEric W. Biederman 	for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
3309c5141e6dSEric Dumazet 		const struct pid_entry *p = &proc_base_stuff[nr];
331061a28784SEric W. Biederman 		if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
3311801199ceSEric W. Biederman 			goto out;
33121da177e4SLinus Torvalds 	}
33131da177e4SLinus Torvalds 
3314b488893aSPavel Emelyanov 	ns = filp->f_dentry->d_sb->s_fs_info;
331519fd4bb2SEric W. Biederman 	iter.task = NULL;
331619fd4bb2SEric W. Biederman 	iter.tgid = filp->f_pos - TGID_OFFSET;
331719fd4bb2SEric W. Biederman 	for (iter = next_tgid(ns, iter);
331819fd4bb2SEric W. Biederman 	     iter.task;
331919fd4bb2SEric W. Biederman 	     iter.tgid += 1, iter = next_tgid(ns, iter)) {
33200499680aSVasiliy Kulikov 		if (has_pid_permissions(ns, iter.task, 2))
33210499680aSVasiliy Kulikov 			__filldir = filldir;
33220499680aSVasiliy Kulikov 		else
33230499680aSVasiliy Kulikov 			__filldir = fake_filldir;
33240499680aSVasiliy Kulikov 
332519fd4bb2SEric W. Biederman 		filp->f_pos = iter.tgid + TGID_OFFSET;
33260499680aSVasiliy Kulikov 		if (proc_pid_fill_cache(filp, dirent, __filldir, iter) < 0) {
332719fd4bb2SEric W. Biederman 			put_task_struct(iter.task);
33280804ef4bSEric W. Biederman 			goto out;
33291da177e4SLinus Torvalds 		}
33301da177e4SLinus Torvalds 	}
33310804ef4bSEric W. Biederman 	filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
33320804ef4bSEric W. Biederman out:
333361a28784SEric W. Biederman 	put_task_struct(reaper);
333461a28784SEric W. Biederman out_no_task:
33351da177e4SLinus Torvalds 	return 0;
33361da177e4SLinus Torvalds }
33371da177e4SLinus Torvalds 
33380bc58a91SEric W. Biederman /*
333928a6d671SEric W. Biederman  * Tasks
334028a6d671SEric W. Biederman  */
3341c5141e6dSEric Dumazet static const struct pid_entry tid_base_stuff[] = {
3342631f9c18SAlexey Dobriyan 	DIR("fd",        S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
33433835541dSJerome Marchand 	DIR("fdinfo",    S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
33446b4e306aSEric W. Biederman 	DIR("ns",	 S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
3345631f9c18SAlexey Dobriyan 	REG("environ",   S_IRUSR, proc_environ_operations),
3346631f9c18SAlexey Dobriyan 	INF("auxv",      S_IRUSR, proc_pid_auxv),
3347631f9c18SAlexey Dobriyan 	ONE("status",    S_IRUGO, proc_pid_status),
3348a9712bc1SAl Viro 	ONE("personality", S_IRUGO, proc_pid_personality),
33493036e7b4SJiri Olsa 	INF("limits",	 S_IRUGO, proc_pid_limits),
335043ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
3351631f9c18SAlexey Dobriyan 	REG("sched",     S_IRUGO|S_IWUSR, proc_pid_sched_operations),
335243ae34cbSIngo Molnar #endif
33534614a696Sjohn stultz 	REG("comm",      S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
3354ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3355a9712bc1SAl Viro 	INF("syscall",   S_IRUGO, proc_pid_syscall),
3356ebcb6734SRoland McGrath #endif
3357631f9c18SAlexey Dobriyan 	INF("cmdline",   S_IRUGO, proc_pid_cmdline),
3358631f9c18SAlexey Dobriyan 	ONE("stat",      S_IRUGO, proc_tid_stat),
3359631f9c18SAlexey Dobriyan 	ONE("statm",     S_IRUGO, proc_pid_statm),
3360631f9c18SAlexey Dobriyan 	REG("maps",      S_IRUGO, proc_maps_operations),
336128a6d671SEric W. Biederman #ifdef CONFIG_NUMA
3362631f9c18SAlexey Dobriyan 	REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
336328a6d671SEric W. Biederman #endif
3364631f9c18SAlexey Dobriyan 	REG("mem",       S_IRUSR|S_IWUSR, proc_mem_operations),
3365631f9c18SAlexey Dobriyan 	LNK("cwd",       proc_cwd_link),
3366631f9c18SAlexey Dobriyan 	LNK("root",      proc_root_link),
3367631f9c18SAlexey Dobriyan 	LNK("exe",       proc_exe_link),
3368631f9c18SAlexey Dobriyan 	REG("mounts",    S_IRUGO, proc_mounts_operations),
3369631f9c18SAlexey Dobriyan 	REG("mountinfo",  S_IRUGO, proc_mountinfo_operations),
33701e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR
3371631f9c18SAlexey Dobriyan 	REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3372631f9c18SAlexey Dobriyan 	REG("smaps",     S_IRUGO, proc_smaps_operations),
3373ca6b0bf0SAl Viro 	REG("pagemap",    S_IRUGO, proc_pagemap_operations),
337428a6d671SEric W. Biederman #endif
337528a6d671SEric W. Biederman #ifdef CONFIG_SECURITY
3376631f9c18SAlexey Dobriyan 	DIR("attr",      S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
337728a6d671SEric W. Biederman #endif
337828a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS
3379631f9c18SAlexey Dobriyan 	INF("wchan",     S_IRUGO, proc_pid_wchan),
338028a6d671SEric W. Biederman #endif
33812ec220e2SKen Chen #ifdef CONFIG_STACKTRACE
3382a9712bc1SAl Viro 	ONE("stack",      S_IRUGO, proc_pid_stack),
338328a6d671SEric W. Biederman #endif
338428a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS
3385631f9c18SAlexey Dobriyan 	INF("schedstat", S_IRUGO, proc_pid_schedstat),
338628a6d671SEric W. Biederman #endif
33879745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP
3388631f9c18SAlexey Dobriyan 	REG("latency",  S_IRUGO, proc_lstats_operations),
33899745512cSArjan van de Ven #endif
33908793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET
3391631f9c18SAlexey Dobriyan 	REG("cpuset",    S_IRUGO, proc_cpuset_operations),
339228a6d671SEric W. Biederman #endif
3393a424316cSPaul Menage #ifdef CONFIG_CGROUPS
3394631f9c18SAlexey Dobriyan 	REG("cgroup",  S_IRUGO, proc_cgroup_operations),
3395a424316cSPaul Menage #endif
3396631f9c18SAlexey Dobriyan 	INF("oom_score", S_IRUGO, proc_oom_score),
3397631f9c18SAlexey Dobriyan 	REG("oom_adj",   S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
3398a63d83f4SDavid Rientjes 	REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
339928a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL
3400631f9c18SAlexey Dobriyan 	REG("loginuid",  S_IWUSR|S_IRUGO, proc_loginuid_operations),
340126ec3c64SAl Viro 	REG("sessionid",  S_IRUGO, proc_sessionid_operations),
340228a6d671SEric W. Biederman #endif
3403f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
3404631f9c18SAlexey Dobriyan 	REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3405f4f154fdSAkinobu Mita #endif
3406297c5d92SAndrea Righi #ifdef CONFIG_TASK_IO_ACCOUNTING
34071d1221f3SVasiliy Kulikov 	INF("io",	S_IRUSR, proc_tid_io_accounting),
3408297c5d92SAndrea Righi #endif
3409f133eccaSChris Metcalf #ifdef CONFIG_HARDWALL
3410f133eccaSChris Metcalf 	INF("hardwall",   S_IRUGO, proc_pid_hardwall),
3411f133eccaSChris Metcalf #endif
341228a6d671SEric W. Biederman };
341328a6d671SEric W. Biederman 
341428a6d671SEric W. Biederman static int proc_tid_base_readdir(struct file * filp,
341528a6d671SEric W. Biederman 			     void * dirent, filldir_t filldir)
341628a6d671SEric W. Biederman {
341728a6d671SEric W. Biederman 	return proc_pident_readdir(filp,dirent,filldir,
341828a6d671SEric W. Biederman 				   tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
341928a6d671SEric W. Biederman }
342028a6d671SEric W. Biederman 
342128a6d671SEric W. Biederman static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
34227bcd6b0eSEric W. Biederman 	return proc_pident_lookup(dir, dentry,
34237bcd6b0eSEric W. Biederman 				  tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
342428a6d671SEric W. Biederman }
342528a6d671SEric W. Biederman 
342600977a59SArjan van de Ven static const struct file_operations proc_tid_base_operations = {
342728a6d671SEric W. Biederman 	.read		= generic_read_dir,
342828a6d671SEric W. Biederman 	.readdir	= proc_tid_base_readdir,
34296038f373SArnd Bergmann 	.llseek		= default_llseek,
343028a6d671SEric W. Biederman };
343128a6d671SEric W. Biederman 
3432c5ef1c42SArjan van de Ven static const struct inode_operations proc_tid_base_inode_operations = {
343328a6d671SEric W. Biederman 	.lookup		= proc_tid_base_lookup,
343428a6d671SEric W. Biederman 	.getattr	= pid_getattr,
343528a6d671SEric W. Biederman 	.setattr	= proc_setattr,
343628a6d671SEric W. Biederman };
343728a6d671SEric W. Biederman 
3438444ceed8SEric W. Biederman static struct dentry *proc_task_instantiate(struct inode *dir,
3439c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
3440444ceed8SEric W. Biederman {
3441444ceed8SEric W. Biederman 	struct dentry *error = ERR_PTR(-ENOENT);
3442444ceed8SEric W. Biederman 	struct inode *inode;
344361a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
3444444ceed8SEric W. Biederman 
3445444ceed8SEric W. Biederman 	if (!inode)
3446444ceed8SEric W. Biederman 		goto out;
3447444ceed8SEric W. Biederman 	inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3448444ceed8SEric W. Biederman 	inode->i_op = &proc_tid_base_inode_operations;
3449444ceed8SEric W. Biederman 	inode->i_fop = &proc_tid_base_operations;
3450444ceed8SEric W. Biederman 	inode->i_flags|=S_IMMUTABLE;
3451aed54175SVegard Nossum 
3452bfe86848SMiklos Szeredi 	set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3453bfe86848SMiklos Szeredi 						  ARRAY_SIZE(tid_base_stuff)));
3454444ceed8SEric W. Biederman 
3455fb045adbSNick Piggin 	d_set_d_op(dentry, &pid_dentry_operations);
3456444ceed8SEric W. Biederman 
3457444ceed8SEric W. Biederman 	d_add(dentry, inode);
3458444ceed8SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
3459444ceed8SEric W. Biederman 	if (pid_revalidate(dentry, NULL))
3460444ceed8SEric W. Biederman 		error = NULL;
3461444ceed8SEric W. Biederman out:
3462444ceed8SEric W. Biederman 	return error;
3463444ceed8SEric W. Biederman }
3464444ceed8SEric W. Biederman 
346528a6d671SEric W. Biederman static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
346628a6d671SEric W. Biederman {
346728a6d671SEric W. Biederman 	struct dentry *result = ERR_PTR(-ENOENT);
346828a6d671SEric W. Biederman 	struct task_struct *task;
346928a6d671SEric W. Biederman 	struct task_struct *leader = get_proc_task(dir);
347028a6d671SEric W. Biederman 	unsigned tid;
3471b488893aSPavel Emelyanov 	struct pid_namespace *ns;
347228a6d671SEric W. Biederman 
347328a6d671SEric W. Biederman 	if (!leader)
347428a6d671SEric W. Biederman 		goto out_no_task;
347528a6d671SEric W. Biederman 
347628a6d671SEric W. Biederman 	tid = name_to_int(dentry);
347728a6d671SEric W. Biederman 	if (tid == ~0U)
347828a6d671SEric W. Biederman 		goto out;
347928a6d671SEric W. Biederman 
3480b488893aSPavel Emelyanov 	ns = dentry->d_sb->s_fs_info;
348128a6d671SEric W. Biederman 	rcu_read_lock();
3482b488893aSPavel Emelyanov 	task = find_task_by_pid_ns(tid, ns);
348328a6d671SEric W. Biederman 	if (task)
348428a6d671SEric W. Biederman 		get_task_struct(task);
348528a6d671SEric W. Biederman 	rcu_read_unlock();
348628a6d671SEric W. Biederman 	if (!task)
348728a6d671SEric W. Biederman 		goto out;
3488bac0abd6SPavel Emelyanov 	if (!same_thread_group(leader, task))
348928a6d671SEric W. Biederman 		goto out_drop_task;
349028a6d671SEric W. Biederman 
3491444ceed8SEric W. Biederman 	result = proc_task_instantiate(dir, dentry, task, NULL);
349228a6d671SEric W. Biederman out_drop_task:
349328a6d671SEric W. Biederman 	put_task_struct(task);
349428a6d671SEric W. Biederman out:
349528a6d671SEric W. Biederman 	put_task_struct(leader);
349628a6d671SEric W. Biederman out_no_task:
349728a6d671SEric W. Biederman 	return result;
349828a6d671SEric W. Biederman }
349928a6d671SEric W. Biederman 
350028a6d671SEric W. Biederman /*
35010bc58a91SEric W. Biederman  * Find the first tid of a thread group to return to user space.
35020bc58a91SEric W. Biederman  *
35030bc58a91SEric W. Biederman  * Usually this is just the thread group leader, but if the users
35040bc58a91SEric W. Biederman  * buffer was too small or there was a seek into the middle of the
35050bc58a91SEric W. Biederman  * directory we have more work todo.
35060bc58a91SEric W. Biederman  *
35070bc58a91SEric W. Biederman  * In the case of a short read we start with find_task_by_pid.
35080bc58a91SEric W. Biederman  *
35090bc58a91SEric W. Biederman  * In the case of a seek we start with the leader and walk nr
35100bc58a91SEric W. Biederman  * threads past it.
35110bc58a91SEric W. Biederman  */
3512cc288738SEric W. Biederman static struct task_struct *first_tid(struct task_struct *leader,
3513b488893aSPavel Emelyanov 		int tid, int nr, struct pid_namespace *ns)
35140bc58a91SEric W. Biederman {
3515a872ff0cSOleg Nesterov 	struct task_struct *pos;
35160bc58a91SEric W. Biederman 
3517cc288738SEric W. Biederman 	rcu_read_lock();
35180bc58a91SEric W. Biederman 	/* Attempt to start with the pid of a thread */
35190bc58a91SEric W. Biederman 	if (tid && (nr > 0)) {
3520b488893aSPavel Emelyanov 		pos = find_task_by_pid_ns(tid, ns);
3521a872ff0cSOleg Nesterov 		if (pos && (pos->group_leader == leader))
3522a872ff0cSOleg Nesterov 			goto found;
35230bc58a91SEric W. Biederman 	}
35240bc58a91SEric W. Biederman 
35250bc58a91SEric W. Biederman 	/* If nr exceeds the number of threads there is nothing todo */
35260bc58a91SEric W. Biederman 	pos = NULL;
3527a872ff0cSOleg Nesterov 	if (nr && nr >= get_nr_threads(leader))
3528a872ff0cSOleg Nesterov 		goto out;
3529a872ff0cSOleg Nesterov 
3530a872ff0cSOleg Nesterov 	/* If we haven't found our starting place yet start
3531a872ff0cSOleg Nesterov 	 * with the leader and walk nr threads forward.
3532a872ff0cSOleg Nesterov 	 */
3533a872ff0cSOleg Nesterov 	for (pos = leader; nr > 0; --nr) {
3534a872ff0cSOleg Nesterov 		pos = next_thread(pos);
3535a872ff0cSOleg Nesterov 		if (pos == leader) {
3536a872ff0cSOleg Nesterov 			pos = NULL;
3537a872ff0cSOleg Nesterov 			goto out;
3538a872ff0cSOleg Nesterov 		}
3539a872ff0cSOleg Nesterov 	}
3540a872ff0cSOleg Nesterov found:
3541a872ff0cSOleg Nesterov 	get_task_struct(pos);
3542a872ff0cSOleg Nesterov out:
3543cc288738SEric W. Biederman 	rcu_read_unlock();
35440bc58a91SEric W. Biederman 	return pos;
35450bc58a91SEric W. Biederman }
35460bc58a91SEric W. Biederman 
35470bc58a91SEric W. Biederman /*
35480bc58a91SEric W. Biederman  * Find the next thread in the thread list.
35490bc58a91SEric W. Biederman  * Return NULL if there is an error or no next thread.
35500bc58a91SEric W. Biederman  *
35510bc58a91SEric W. Biederman  * The reference to the input task_struct is released.
35520bc58a91SEric W. Biederman  */
35530bc58a91SEric W. Biederman static struct task_struct *next_tid(struct task_struct *start)
35540bc58a91SEric W. Biederman {
3555c1df7fb8SOleg Nesterov 	struct task_struct *pos = NULL;
3556cc288738SEric W. Biederman 	rcu_read_lock();
3557c1df7fb8SOleg Nesterov 	if (pid_alive(start)) {
35580bc58a91SEric W. Biederman 		pos = next_thread(start);
3559c1df7fb8SOleg Nesterov 		if (thread_group_leader(pos))
35600bc58a91SEric W. Biederman 			pos = NULL;
3561c1df7fb8SOleg Nesterov 		else
3562c1df7fb8SOleg Nesterov 			get_task_struct(pos);
3563c1df7fb8SOleg Nesterov 	}
3564cc288738SEric W. Biederman 	rcu_read_unlock();
35650bc58a91SEric W. Biederman 	put_task_struct(start);
35660bc58a91SEric W. Biederman 	return pos;
35670bc58a91SEric W. Biederman }
35680bc58a91SEric W. Biederman 
356961a28784SEric W. Biederman static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
357061a28784SEric W. Biederman 	struct task_struct *task, int tid)
357161a28784SEric W. Biederman {
357261a28784SEric W. Biederman 	char name[PROC_NUMBUF];
357361a28784SEric W. Biederman 	int len = snprintf(name, sizeof(name), "%d", tid);
357461a28784SEric W. Biederman 	return proc_fill_cache(filp, dirent, filldir, name, len,
357561a28784SEric W. Biederman 				proc_task_instantiate, task, NULL);
357661a28784SEric W. Biederman }
357761a28784SEric W. Biederman 
35781da177e4SLinus Torvalds /* for the /proc/TGID/task/ directories */
35791da177e4SLinus Torvalds static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
35801da177e4SLinus Torvalds {
35812fddfeefSJosef "Jeff" Sipek 	struct dentry *dentry = filp->f_path.dentry;
35821da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
35837d895244SGuillaume Chazarain 	struct task_struct *leader = NULL;
35840bc58a91SEric W. Biederman 	struct task_struct *task;
35851da177e4SLinus Torvalds 	int retval = -ENOENT;
35861da177e4SLinus Torvalds 	ino_t ino;
35870bc58a91SEric W. Biederman 	int tid;
3588b488893aSPavel Emelyanov 	struct pid_namespace *ns;
35891da177e4SLinus Torvalds 
35907d895244SGuillaume Chazarain 	task = get_proc_task(inode);
35917d895244SGuillaume Chazarain 	if (!task)
35927d895244SGuillaume Chazarain 		goto out_no_task;
35937d895244SGuillaume Chazarain 	rcu_read_lock();
35947d895244SGuillaume Chazarain 	if (pid_alive(task)) {
35957d895244SGuillaume Chazarain 		leader = task->group_leader;
35967d895244SGuillaume Chazarain 		get_task_struct(leader);
35977d895244SGuillaume Chazarain 	}
35987d895244SGuillaume Chazarain 	rcu_read_unlock();
35997d895244SGuillaume Chazarain 	put_task_struct(task);
360099f89551SEric W. Biederman 	if (!leader)
360199f89551SEric W. Biederman 		goto out_no_task;
36021da177e4SLinus Torvalds 	retval = 0;
36031da177e4SLinus Torvalds 
3604ee568b25SLinus Torvalds 	switch ((unsigned long)filp->f_pos) {
36051da177e4SLinus Torvalds 	case 0:
36061da177e4SLinus Torvalds 		ino = inode->i_ino;
3607ee6f779bSZhang Le 		if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) < 0)
36081da177e4SLinus Torvalds 			goto out;
3609ee6f779bSZhang Le 		filp->f_pos++;
36101da177e4SLinus Torvalds 		/* fall through */
36111da177e4SLinus Torvalds 	case 1:
36121da177e4SLinus Torvalds 		ino = parent_ino(dentry);
3613ee6f779bSZhang Le 		if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) < 0)
36141da177e4SLinus Torvalds 			goto out;
3615ee6f779bSZhang Le 		filp->f_pos++;
36161da177e4SLinus Torvalds 		/* fall through */
36171da177e4SLinus Torvalds 	}
36181da177e4SLinus Torvalds 
36190bc58a91SEric W. Biederman 	/* f_version caches the tgid value that the last readdir call couldn't
36200bc58a91SEric W. Biederman 	 * return. lseek aka telldir automagically resets f_version to 0.
36210bc58a91SEric W. Biederman 	 */
3622b488893aSPavel Emelyanov 	ns = filp->f_dentry->d_sb->s_fs_info;
36232b47c361SMathieu Desnoyers 	tid = (int)filp->f_version;
36240bc58a91SEric W. Biederman 	filp->f_version = 0;
3625ee6f779bSZhang Le 	for (task = first_tid(leader, tid, filp->f_pos - 2, ns);
36260bc58a91SEric W. Biederman 	     task;
3627ee6f779bSZhang Le 	     task = next_tid(task), filp->f_pos++) {
3628b488893aSPavel Emelyanov 		tid = task_pid_nr_ns(task, ns);
362961a28784SEric W. Biederman 		if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
36300bc58a91SEric W. Biederman 			/* returning this tgid failed, save it as the first
36310bc58a91SEric W. Biederman 			 * pid for the next readir call */
36322b47c361SMathieu Desnoyers 			filp->f_version = (u64)tid;
36330bc58a91SEric W. Biederman 			put_task_struct(task);
36341da177e4SLinus Torvalds 			break;
36350bc58a91SEric W. Biederman 		}
36361da177e4SLinus Torvalds 	}
36371da177e4SLinus Torvalds out:
363899f89551SEric W. Biederman 	put_task_struct(leader);
363999f89551SEric W. Biederman out_no_task:
36401da177e4SLinus Torvalds 	return retval;
36411da177e4SLinus Torvalds }
36426e66b52bSEric W. Biederman 
36436e66b52bSEric W. Biederman static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
36446e66b52bSEric W. Biederman {
36456e66b52bSEric W. Biederman 	struct inode *inode = dentry->d_inode;
364699f89551SEric W. Biederman 	struct task_struct *p = get_proc_task(inode);
36476e66b52bSEric W. Biederman 	generic_fillattr(inode, stat);
36486e66b52bSEric W. Biederman 
364999f89551SEric W. Biederman 	if (p) {
365099f89551SEric W. Biederman 		stat->nlink += get_nr_threads(p);
365199f89551SEric W. Biederman 		put_task_struct(p);
36526e66b52bSEric W. Biederman 	}
36536e66b52bSEric W. Biederman 
36546e66b52bSEric W. Biederman 	return 0;
36556e66b52bSEric W. Biederman }
365628a6d671SEric W. Biederman 
3657c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations = {
365828a6d671SEric W. Biederman 	.lookup		= proc_task_lookup,
365928a6d671SEric W. Biederman 	.getattr	= proc_task_getattr,
366028a6d671SEric W. Biederman 	.setattr	= proc_setattr,
36610499680aSVasiliy Kulikov 	.permission	= proc_pid_permission,
366228a6d671SEric W. Biederman };
366328a6d671SEric W. Biederman 
366400977a59SArjan van de Ven static const struct file_operations proc_task_operations = {
366528a6d671SEric W. Biederman 	.read		= generic_read_dir,
366628a6d671SEric W. Biederman 	.readdir	= proc_task_readdir,
36676038f373SArnd Bergmann 	.llseek		= default_llseek,
366828a6d671SEric W. Biederman };
3669