xref: /openbmc/linux/fs/proc/base.c (revision 6ba51e3751a3543f353a06cd8a6c975bda707c45)
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>
7687ebdc00SAndrew Morton #include <linux/printk.h>
77a424316cSPaul Menage #include <linux/cgroup.h>
781da177e4SLinus Torvalds #include <linux/cpuset.h>
791da177e4SLinus Torvalds #include <linux/audit.h>
805addc5ddSAl Viro #include <linux/poll.h>
811651e14eSSerge E. Hallyn #include <linux/nsproxy.h>
828ac773b4SAlexey Dobriyan #include <linux/oom.h>
833cb4a0bbSKawai, Hidehiro #include <linux/elf.h>
8460347f67SPavel Emelyanov #include <linux/pid_namespace.h>
8522d917d8SEric W. Biederman #include <linux/user_namespace.h>
865ad4e53bSAl Viro #include <linux/fs_struct.h>
875a0e3ad6STejun Heo #include <linux/slab.h>
88640708a2SPavel Emelyanov #include <linux/flex_array.h>
8948f6a7a5SPavel Emelyanov #include <linux/posix-timers.h>
90f133eccaSChris Metcalf #ifdef CONFIG_HARDWALL
91f133eccaSChris Metcalf #include <asm/hardwall.h>
92f133eccaSChris Metcalf #endif
9343d2b113SKAMEZAWA Hiroyuki #include <trace/events/oom.h>
941da177e4SLinus Torvalds #include "internal.h"
95faf60af1SCyrill Gorcunov #include "fd.h"
961da177e4SLinus Torvalds 
970f2fe20fSEric W. Biederman /* NOTE:
980f2fe20fSEric W. Biederman  *	Implementing inode permission operations in /proc is almost
990f2fe20fSEric W. Biederman  *	certainly an error.  Permission checks need to happen during
1000f2fe20fSEric W. Biederman  *	each system call not at open time.  The reason is that most of
1010f2fe20fSEric W. Biederman  *	what we wish to check for permissions in /proc varies at runtime.
1020f2fe20fSEric W. Biederman  *
1030f2fe20fSEric W. Biederman  *	The classic example of a problem is opening file descriptors
1040f2fe20fSEric W. Biederman  *	in /proc for a task before it execs a suid executable.
1050f2fe20fSEric W. Biederman  */
1060f2fe20fSEric W. Biederman 
1071da177e4SLinus Torvalds struct pid_entry {
108cedbccabSAlexey Dobriyan 	const char *name;
109c5141e6dSEric Dumazet 	int len;
110d161a13fSAl Viro 	umode_t mode;
111c5ef1c42SArjan van de Ven 	const struct inode_operations *iop;
11200977a59SArjan van de Ven 	const struct file_operations *fop;
11320cdc894SEric W. Biederman 	union proc_op op;
1141da177e4SLinus Torvalds };
1151da177e4SLinus Torvalds 
11661a28784SEric W. Biederman #define NOD(NAME, MODE, IOP, FOP, OP) {			\
11720cdc894SEric W. Biederman 	.name = (NAME),					\
118c5141e6dSEric Dumazet 	.len  = sizeof(NAME) - 1,			\
11920cdc894SEric W. Biederman 	.mode = MODE,					\
12020cdc894SEric W. Biederman 	.iop  = IOP,					\
12120cdc894SEric W. Biederman 	.fop  = FOP,					\
12220cdc894SEric W. Biederman 	.op   = OP,					\
12320cdc894SEric W. Biederman }
12420cdc894SEric W. Biederman 
125631f9c18SAlexey Dobriyan #define DIR(NAME, MODE, iops, fops)	\
126631f9c18SAlexey Dobriyan 	NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
127631f9c18SAlexey Dobriyan #define LNK(NAME, get_link)					\
12861a28784SEric W. Biederman 	NOD(NAME, (S_IFLNK|S_IRWXUGO),				\
12920cdc894SEric W. Biederman 		&proc_pid_link_inode_operations, NULL,		\
130631f9c18SAlexey Dobriyan 		{ .proc_get_link = get_link } )
131631f9c18SAlexey Dobriyan #define REG(NAME, MODE, fops)				\
132631f9c18SAlexey Dobriyan 	NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
133631f9c18SAlexey Dobriyan #define INF(NAME, MODE, read)				\
13461a28784SEric W. Biederman 	NOD(NAME, (S_IFREG|(MODE)), 			\
13520cdc894SEric W. Biederman 		NULL, &proc_info_file_operations,	\
136631f9c18SAlexey Dobriyan 		{ .proc_read = read } )
137631f9c18SAlexey Dobriyan #define ONE(NAME, MODE, show)				\
138be614086SEric W. Biederman 	NOD(NAME, (S_IFREG|(MODE)), 			\
139be614086SEric W. Biederman 		NULL, &proc_single_file_operations,	\
140631f9c18SAlexey Dobriyan 		{ .proc_show = show } )
1411da177e4SLinus Torvalds 
142aed54175SVegard Nossum /*
143aed54175SVegard Nossum  * Count the number of hardlinks for the pid_entry table, excluding the .
144aed54175SVegard Nossum  * and .. links.
145aed54175SVegard Nossum  */
146aed54175SVegard Nossum static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
147aed54175SVegard Nossum 	unsigned int n)
148aed54175SVegard Nossum {
149aed54175SVegard Nossum 	unsigned int i;
150aed54175SVegard Nossum 	unsigned int count;
151aed54175SVegard Nossum 
152aed54175SVegard Nossum 	count = 0;
153aed54175SVegard Nossum 	for (i = 0; i < n; ++i) {
154aed54175SVegard Nossum 		if (S_ISDIR(entries[i].mode))
155aed54175SVegard Nossum 			++count;
156aed54175SVegard Nossum 	}
157aed54175SVegard Nossum 
158aed54175SVegard Nossum 	return count;
159aed54175SVegard Nossum }
160aed54175SVegard Nossum 
161f7ad3c6bSMiklos Szeredi static int get_task_root(struct task_struct *task, struct path *root)
1621da177e4SLinus Torvalds {
1637c2c7d99SHugh Dickins 	int result = -ENOENT;
1647c2c7d99SHugh Dickins 
1650494f6ecSMiklos Szeredi 	task_lock(task);
166f7ad3c6bSMiklos Szeredi 	if (task->fs) {
167f7ad3c6bSMiklos Szeredi 		get_fs_root(task->fs, root);
1687c2c7d99SHugh Dickins 		result = 0;
1697c2c7d99SHugh Dickins 	}
1700494f6ecSMiklos Szeredi 	task_unlock(task);
1717c2c7d99SHugh Dickins 	return result;
1720494f6ecSMiklos Szeredi }
1730494f6ecSMiklos Szeredi 
1747773fbc5SCyrill Gorcunov static int proc_cwd_link(struct dentry *dentry, struct path *path)
1750494f6ecSMiklos Szeredi {
1767773fbc5SCyrill Gorcunov 	struct task_struct *task = get_proc_task(dentry->d_inode);
1770494f6ecSMiklos Szeredi 	int result = -ENOENT;
17899f89551SEric W. Biederman 
17999f89551SEric W. Biederman 	if (task) {
180f7ad3c6bSMiklos Szeredi 		task_lock(task);
181f7ad3c6bSMiklos Szeredi 		if (task->fs) {
182f7ad3c6bSMiklos Szeredi 			get_fs_pwd(task->fs, path);
183f7ad3c6bSMiklos Szeredi 			result = 0;
184f7ad3c6bSMiklos Szeredi 		}
185f7ad3c6bSMiklos Szeredi 		task_unlock(task);
18699f89551SEric W. Biederman 		put_task_struct(task);
18799f89551SEric W. Biederman 	}
1881da177e4SLinus Torvalds 	return result;
1891da177e4SLinus Torvalds }
1901da177e4SLinus Torvalds 
1917773fbc5SCyrill Gorcunov static int proc_root_link(struct dentry *dentry, struct path *path)
1921da177e4SLinus Torvalds {
1937773fbc5SCyrill Gorcunov 	struct task_struct *task = get_proc_task(dentry->d_inode);
1941da177e4SLinus Torvalds 	int result = -ENOENT;
19599f89551SEric W. Biederman 
19699f89551SEric W. Biederman 	if (task) {
197f7ad3c6bSMiklos Szeredi 		result = get_task_root(task, path);
19899f89551SEric W. Biederman 		put_task_struct(task);
19999f89551SEric W. Biederman 	}
2001da177e4SLinus Torvalds 	return result;
2011da177e4SLinus Torvalds }
2021da177e4SLinus Torvalds 
2032ca66ff7SAlexey Dobriyan static int proc_pid_cmdline(struct seq_file *m, struct pid_namespace *ns,
2042ca66ff7SAlexey Dobriyan 			    struct pid *pid, struct task_struct *task)
2051da177e4SLinus Torvalds {
2062ca66ff7SAlexey Dobriyan 	/*
2072ca66ff7SAlexey Dobriyan 	 * Rely on struct seq_operations::show() being called once
2082ca66ff7SAlexey Dobriyan 	 * per internal buffer allocation. See single_open(), traverse().
2092ca66ff7SAlexey Dobriyan 	 */
2102ca66ff7SAlexey Dobriyan 	BUG_ON(m->size < PAGE_SIZE);
2112ca66ff7SAlexey Dobriyan 	m->count += get_cmdline(task, m->buf, PAGE_SIZE);
2122ca66ff7SAlexey Dobriyan 	return 0;
2131da177e4SLinus Torvalds }
2141da177e4SLinus Torvalds 
215f9ea536eSAlexey Dobriyan static int proc_pid_auxv(struct seq_file *m, struct pid_namespace *ns,
216f9ea536eSAlexey Dobriyan 			 struct pid *pid, struct task_struct *task)
2171da177e4SLinus Torvalds {
218e7dcd999SCong Wang 	struct mm_struct *mm = mm_access(task, PTRACE_MODE_READ);
2192fadaef4SAl Viro 	if (mm && !IS_ERR(mm)) {
2201da177e4SLinus Torvalds 		unsigned int nwords = 0;
221dfe6b7d9SHannes Eder 		do {
2221da177e4SLinus Torvalds 			nwords += 2;
223dfe6b7d9SHannes Eder 		} while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
224f9ea536eSAlexey Dobriyan 		seq_write(m, mm->saved_auxv, nwords * sizeof(mm->saved_auxv[0]));
2251da177e4SLinus Torvalds 		mmput(mm);
226f9ea536eSAlexey Dobriyan 		return 0;
227f9ea536eSAlexey Dobriyan 	} else
228f9ea536eSAlexey Dobriyan 		return PTR_ERR(mm);
2291da177e4SLinus Torvalds }
2301da177e4SLinus Torvalds 
2311da177e4SLinus Torvalds 
2321da177e4SLinus Torvalds #ifdef CONFIG_KALLSYMS
2331da177e4SLinus Torvalds /*
2341da177e4SLinus Torvalds  * Provides a wchan file via kallsyms in a proper one-value-per-file format.
2351da177e4SLinus Torvalds  * Returns the resolved symbol.  If that fails, simply return the address.
2361da177e4SLinus Torvalds  */
237edfcd606SAlexey Dobriyan static int proc_pid_wchan(struct seq_file *m, struct pid_namespace *ns,
238edfcd606SAlexey Dobriyan 			  struct pid *pid, struct task_struct *task)
2391da177e4SLinus Torvalds {
240ffb45122SAlexey Dobriyan 	unsigned long wchan;
2419281aceaSTejun Heo 	char symname[KSYM_NAME_LEN];
2421da177e4SLinus Torvalds 
2431da177e4SLinus Torvalds 	wchan = get_wchan(task);
2441da177e4SLinus Torvalds 
2459d65cb4aSAlexey Dobriyan 	if (lookup_symbol_name(wchan, symname) < 0)
246f83ce3e6SJake Edge 		if (!ptrace_may_access(task, PTRACE_MODE_READ))
247f83ce3e6SJake Edge 			return 0;
248f83ce3e6SJake Edge 		else
249edfcd606SAlexey Dobriyan 			return seq_printf(m, "%lu", wchan);
2509d65cb4aSAlexey Dobriyan 	else
251edfcd606SAlexey Dobriyan 		return seq_printf(m, "%s", symname);
2521da177e4SLinus Torvalds }
2531da177e4SLinus Torvalds #endif /* CONFIG_KALLSYMS */
2541da177e4SLinus Torvalds 
255a9712bc1SAl Viro static int lock_trace(struct task_struct *task)
256a9712bc1SAl Viro {
257a9712bc1SAl Viro 	int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
258a9712bc1SAl Viro 	if (err)
259a9712bc1SAl Viro 		return err;
260a9712bc1SAl Viro 	if (!ptrace_may_access(task, PTRACE_MODE_ATTACH)) {
261a9712bc1SAl Viro 		mutex_unlock(&task->signal->cred_guard_mutex);
262a9712bc1SAl Viro 		return -EPERM;
263a9712bc1SAl Viro 	}
264a9712bc1SAl Viro 	return 0;
265a9712bc1SAl Viro }
266a9712bc1SAl Viro 
267a9712bc1SAl Viro static void unlock_trace(struct task_struct *task)
268a9712bc1SAl Viro {
269a9712bc1SAl Viro 	mutex_unlock(&task->signal->cred_guard_mutex);
270a9712bc1SAl Viro }
271a9712bc1SAl Viro 
2722ec220e2SKen Chen #ifdef CONFIG_STACKTRACE
2732ec220e2SKen Chen 
2742ec220e2SKen Chen #define MAX_STACK_TRACE_DEPTH	64
2752ec220e2SKen Chen 
2762ec220e2SKen Chen static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
2772ec220e2SKen Chen 			  struct pid *pid, struct task_struct *task)
2782ec220e2SKen Chen {
2792ec220e2SKen Chen 	struct stack_trace trace;
2802ec220e2SKen Chen 	unsigned long *entries;
281a9712bc1SAl Viro 	int err;
2822ec220e2SKen Chen 	int i;
2832ec220e2SKen Chen 
2842ec220e2SKen Chen 	entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
2852ec220e2SKen Chen 	if (!entries)
2862ec220e2SKen Chen 		return -ENOMEM;
2872ec220e2SKen Chen 
2882ec220e2SKen Chen 	trace.nr_entries	= 0;
2892ec220e2SKen Chen 	trace.max_entries	= MAX_STACK_TRACE_DEPTH;
2902ec220e2SKen Chen 	trace.entries		= entries;
2912ec220e2SKen Chen 	trace.skip		= 0;
292a9712bc1SAl Viro 
293a9712bc1SAl Viro 	err = lock_trace(task);
294a9712bc1SAl Viro 	if (!err) {
2952ec220e2SKen Chen 		save_stack_trace_tsk(task, &trace);
2962ec220e2SKen Chen 
2972ec220e2SKen Chen 		for (i = 0; i < trace.nr_entries; i++) {
29851e03149SKonstantin Khlebnikov 			seq_printf(m, "[<%pK>] %pS\n",
2992ec220e2SKen Chen 				   (void *)entries[i], (void *)entries[i]);
3002ec220e2SKen Chen 		}
301a9712bc1SAl Viro 		unlock_trace(task);
302a9712bc1SAl Viro 	}
3032ec220e2SKen Chen 	kfree(entries);
3042ec220e2SKen Chen 
305a9712bc1SAl Viro 	return err;
3062ec220e2SKen Chen }
3072ec220e2SKen Chen #endif
3082ec220e2SKen Chen 
3091da177e4SLinus Torvalds #ifdef CONFIG_SCHEDSTATS
3101da177e4SLinus Torvalds /*
3111da177e4SLinus Torvalds  * Provides /proc/PID/schedstat
3121da177e4SLinus Torvalds  */
313f6e826caSAlexey Dobriyan static int proc_pid_schedstat(struct seq_file *m, struct pid_namespace *ns,
314f6e826caSAlexey Dobriyan 			      struct pid *pid, struct task_struct *task)
3151da177e4SLinus Torvalds {
316f6e826caSAlexey Dobriyan 	return seq_printf(m, "%llu %llu %lu\n",
317826e08b0SIngo Molnar 			(unsigned long long)task->se.sum_exec_runtime,
318826e08b0SIngo Molnar 			(unsigned long long)task->sched_info.run_delay,
3192d72376bSIngo Molnar 			task->sched_info.pcount);
3201da177e4SLinus Torvalds }
3211da177e4SLinus Torvalds #endif
3221da177e4SLinus Torvalds 
3239745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP
3249745512cSArjan van de Ven static int lstats_show_proc(struct seq_file *m, void *v)
3259745512cSArjan van de Ven {
3269745512cSArjan van de Ven 	int i;
32713d77c37SHiroshi Shimamoto 	struct inode *inode = m->private;
32813d77c37SHiroshi Shimamoto 	struct task_struct *task = get_proc_task(inode);
3299745512cSArjan van de Ven 
33013d77c37SHiroshi Shimamoto 	if (!task)
33113d77c37SHiroshi Shimamoto 		return -ESRCH;
33213d77c37SHiroshi Shimamoto 	seq_puts(m, "Latency Top version : v0.1\n");
3339745512cSArjan van de Ven 	for (i = 0; i < 32; i++) {
33434e49d4fSJoe Perches 		struct latency_record *lr = &task->latency_record[i];
33534e49d4fSJoe Perches 		if (lr->backtrace[0]) {
3369745512cSArjan van de Ven 			int q;
3379745512cSArjan van de Ven 			seq_printf(m, "%i %li %li",
33834e49d4fSJoe Perches 				   lr->count, lr->time, lr->max);
3399745512cSArjan van de Ven 			for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
34034e49d4fSJoe Perches 				unsigned long bt = lr->backtrace[q];
34134e49d4fSJoe Perches 				if (!bt)
3429745512cSArjan van de Ven 					break;
34334e49d4fSJoe Perches 				if (bt == ULONG_MAX)
3449745512cSArjan van de Ven 					break;
34534e49d4fSJoe Perches 				seq_printf(m, " %ps", (void *)bt);
3469745512cSArjan van de Ven 			}
3479d6de12fSAlexey Dobriyan 			seq_putc(m, '\n');
3489745512cSArjan van de Ven 		}
3499745512cSArjan van de Ven 
3509745512cSArjan van de Ven 	}
35113d77c37SHiroshi Shimamoto 	put_task_struct(task);
3529745512cSArjan van de Ven 	return 0;
3539745512cSArjan van de Ven }
3549745512cSArjan van de Ven 
3559745512cSArjan van de Ven static int lstats_open(struct inode *inode, struct file *file)
3569745512cSArjan van de Ven {
35713d77c37SHiroshi Shimamoto 	return single_open(file, lstats_show_proc, inode);
358d6643d12SHiroshi Shimamoto }
359d6643d12SHiroshi Shimamoto 
3609745512cSArjan van de Ven static ssize_t lstats_write(struct file *file, const char __user *buf,
3619745512cSArjan van de Ven 			    size_t count, loff_t *offs)
3629745512cSArjan van de Ven {
363496ad9aaSAl Viro 	struct task_struct *task = get_proc_task(file_inode(file));
3649745512cSArjan van de Ven 
36513d77c37SHiroshi Shimamoto 	if (!task)
36613d77c37SHiroshi Shimamoto 		return -ESRCH;
3679745512cSArjan van de Ven 	clear_all_latency_tracing(task);
36813d77c37SHiroshi Shimamoto 	put_task_struct(task);
3699745512cSArjan van de Ven 
3709745512cSArjan van de Ven 	return count;
3719745512cSArjan van de Ven }
3729745512cSArjan van de Ven 
3739745512cSArjan van de Ven static const struct file_operations proc_lstats_operations = {
3749745512cSArjan van de Ven 	.open		= lstats_open,
3759745512cSArjan van de Ven 	.read		= seq_read,
3769745512cSArjan van de Ven 	.write		= lstats_write,
3779745512cSArjan van de Ven 	.llseek		= seq_lseek,
37813d77c37SHiroshi Shimamoto 	.release	= single_release,
3799745512cSArjan van de Ven };
3809745512cSArjan van de Ven 
3819745512cSArjan van de Ven #endif
3829745512cSArjan van de Ven 
3838d8b97baSAl Viro #ifdef CONFIG_CGROUPS
3848d8b97baSAl Viro static int cgroup_open(struct inode *inode, struct file *file)
3858d8b97baSAl Viro {
3868d8b97baSAl Viro 	struct pid *pid = PROC_I(inode)->pid;
3878d8b97baSAl Viro 	return single_open(file, proc_cgroup_show, pid);
3888d8b97baSAl Viro }
3898d8b97baSAl Viro 
3908d8b97baSAl Viro static const struct file_operations proc_cgroup_operations = {
3918d8b97baSAl Viro 	.open		= cgroup_open,
3928d8b97baSAl Viro 	.read		= seq_read,
3938d8b97baSAl Viro 	.llseek		= seq_lseek,
3948d8b97baSAl Viro 	.release	= single_release,
3958d8b97baSAl Viro };
3968d8b97baSAl Viro #endif
3978d8b97baSAl Viro 
3988d8b97baSAl Viro #ifdef CONFIG_PROC_PID_CPUSET
3998d8b97baSAl Viro 
4008d8b97baSAl Viro static int cpuset_open(struct inode *inode, struct file *file)
4018d8b97baSAl Viro {
4028d8b97baSAl Viro 	struct pid *pid = PROC_I(inode)->pid;
4038d8b97baSAl Viro 	return single_open(file, proc_cpuset_show, pid);
4048d8b97baSAl Viro }
4058d8b97baSAl Viro 
4068d8b97baSAl Viro static const struct file_operations proc_cpuset_operations = {
4078d8b97baSAl Viro 	.open		= cpuset_open,
4088d8b97baSAl Viro 	.read		= seq_read,
4098d8b97baSAl Viro 	.llseek		= seq_lseek,
4108d8b97baSAl Viro 	.release	= single_release,
4118d8b97baSAl Viro };
4128d8b97baSAl Viro #endif
4138d8b97baSAl Viro 
414*6ba51e37SAlexey Dobriyan static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns,
415*6ba51e37SAlexey Dobriyan 			  struct pid *pid, struct task_struct *task)
4161da177e4SLinus Torvalds {
417a7f638f9SDavid Rientjes 	unsigned long totalpages = totalram_pages + total_swap_pages;
418b95c35e7SOleg Nesterov 	unsigned long points = 0;
4191da177e4SLinus Torvalds 
42019c5d45aSAlexey Dobriyan 	read_lock(&tasklist_lock);
421b95c35e7SOleg Nesterov 	if (pid_alive(task))
422a7f638f9SDavid Rientjes 		points = oom_badness(task, NULL, NULL, totalpages) *
423a7f638f9SDavid Rientjes 						1000 / totalpages;
42419c5d45aSAlexey Dobriyan 	read_unlock(&tasklist_lock);
425*6ba51e37SAlexey Dobriyan 	return seq_printf(m, "%lu\n", points);
4261da177e4SLinus Torvalds }
4271da177e4SLinus Torvalds 
428d85f50d5SNeil Horman struct limit_names {
429cedbccabSAlexey Dobriyan 	const char *name;
430cedbccabSAlexey Dobriyan 	const char *unit;
431d85f50d5SNeil Horman };
432d85f50d5SNeil Horman 
433d85f50d5SNeil Horman static const struct limit_names lnames[RLIM_NLIMITS] = {
434cff4edb5SKees Cook 	[RLIMIT_CPU] = {"Max cpu time", "seconds"},
435d85f50d5SNeil Horman 	[RLIMIT_FSIZE] = {"Max file size", "bytes"},
436d85f50d5SNeil Horman 	[RLIMIT_DATA] = {"Max data size", "bytes"},
437d85f50d5SNeil Horman 	[RLIMIT_STACK] = {"Max stack size", "bytes"},
438d85f50d5SNeil Horman 	[RLIMIT_CORE] = {"Max core file size", "bytes"},
439d85f50d5SNeil Horman 	[RLIMIT_RSS] = {"Max resident set", "bytes"},
440d85f50d5SNeil Horman 	[RLIMIT_NPROC] = {"Max processes", "processes"},
441d85f50d5SNeil Horman 	[RLIMIT_NOFILE] = {"Max open files", "files"},
442d85f50d5SNeil Horman 	[RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
443d85f50d5SNeil Horman 	[RLIMIT_AS] = {"Max address space", "bytes"},
444d85f50d5SNeil Horman 	[RLIMIT_LOCKS] = {"Max file locks", "locks"},
445d85f50d5SNeil Horman 	[RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
446d85f50d5SNeil Horman 	[RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
447d85f50d5SNeil Horman 	[RLIMIT_NICE] = {"Max nice priority", NULL},
448d85f50d5SNeil Horman 	[RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
4498808117cSEugene Teo 	[RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
450d85f50d5SNeil Horman };
451d85f50d5SNeil Horman 
452d85f50d5SNeil Horman /* Display limits for a process */
4531c963eb1SAlexey Dobriyan static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
4541c963eb1SAlexey Dobriyan 			   struct pid *pid, struct task_struct *task)
455d85f50d5SNeil Horman {
456d85f50d5SNeil Horman 	unsigned int i;
457d85f50d5SNeil Horman 	unsigned long flags;
458d85f50d5SNeil Horman 
459d85f50d5SNeil Horman 	struct rlimit rlim[RLIM_NLIMITS];
460d85f50d5SNeil Horman 
461a6bebbc8SLai Jiangshan 	if (!lock_task_sighand(task, &flags))
462d85f50d5SNeil Horman 		return 0;
463d85f50d5SNeil Horman 	memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
464d85f50d5SNeil Horman 	unlock_task_sighand(task, &flags);
465d85f50d5SNeil Horman 
466d85f50d5SNeil Horman 	/*
467d85f50d5SNeil Horman 	 * print the file header
468d85f50d5SNeil Horman 	 */
4691c963eb1SAlexey Dobriyan        seq_printf(m, "%-25s %-20s %-20s %-10s\n",
470d85f50d5SNeil Horman 			"Limit", "Soft Limit", "Hard Limit", "Units");
471d85f50d5SNeil Horman 
472d85f50d5SNeil Horman 	for (i = 0; i < RLIM_NLIMITS; i++) {
473d85f50d5SNeil Horman 		if (rlim[i].rlim_cur == RLIM_INFINITY)
4741c963eb1SAlexey Dobriyan 			seq_printf(m, "%-25s %-20s ",
475d85f50d5SNeil Horman 					 lnames[i].name, "unlimited");
476d85f50d5SNeil Horman 		else
4771c963eb1SAlexey Dobriyan 			seq_printf(m, "%-25s %-20lu ",
478d85f50d5SNeil Horman 					 lnames[i].name, rlim[i].rlim_cur);
479d85f50d5SNeil Horman 
480d85f50d5SNeil Horman 		if (rlim[i].rlim_max == RLIM_INFINITY)
4811c963eb1SAlexey Dobriyan 			seq_printf(m, "%-20s ", "unlimited");
482d85f50d5SNeil Horman 		else
4831c963eb1SAlexey Dobriyan 			seq_printf(m, "%-20lu ", rlim[i].rlim_max);
484d85f50d5SNeil Horman 
485d85f50d5SNeil Horman 		if (lnames[i].unit)
4861c963eb1SAlexey Dobriyan 			seq_printf(m, "%-10s\n", lnames[i].unit);
487d85f50d5SNeil Horman 		else
4881c963eb1SAlexey Dobriyan 			seq_putc(m, '\n');
489d85f50d5SNeil Horman 	}
490d85f50d5SNeil Horman 
4911c963eb1SAlexey Dobriyan 	return 0;
492d85f50d5SNeil Horman }
493d85f50d5SNeil Horman 
494ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
49509d93bd6SAlexey Dobriyan static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
49609d93bd6SAlexey Dobriyan 			    struct pid *pid, struct task_struct *task)
497ebcb6734SRoland McGrath {
498ebcb6734SRoland McGrath 	long nr;
499ebcb6734SRoland McGrath 	unsigned long args[6], sp, pc;
500a9712bc1SAl Viro 	int res = lock_trace(task);
501a9712bc1SAl Viro 	if (res)
502a9712bc1SAl Viro 		return res;
503ebcb6734SRoland McGrath 
504ebcb6734SRoland McGrath 	if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
50509d93bd6SAlexey Dobriyan 		seq_puts(m, "running\n");
506a9712bc1SAl Viro 	else if (nr < 0)
50709d93bd6SAlexey Dobriyan 		seq_printf(m, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
508a9712bc1SAl Viro 	else
50909d93bd6SAlexey Dobriyan 		seq_printf(m,
510ebcb6734SRoland McGrath 		       "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
511ebcb6734SRoland McGrath 		       nr,
512ebcb6734SRoland McGrath 		       args[0], args[1], args[2], args[3], args[4], args[5],
513ebcb6734SRoland McGrath 		       sp, pc);
514a9712bc1SAl Viro 	unlock_trace(task);
515a9712bc1SAl Viro 	return res;
516ebcb6734SRoland McGrath }
517ebcb6734SRoland McGrath #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
518ebcb6734SRoland McGrath 
5191da177e4SLinus Torvalds /************************************************************************/
5201da177e4SLinus Torvalds /*                       Here the fs part begins                        */
5211da177e4SLinus Torvalds /************************************************************************/
5221da177e4SLinus Torvalds 
5231da177e4SLinus Torvalds /* permission checks */
524778c1144SEric W. Biederman static int proc_fd_access_allowed(struct inode *inode)
5251da177e4SLinus Torvalds {
526778c1144SEric W. Biederman 	struct task_struct *task;
527778c1144SEric W. Biederman 	int allowed = 0;
528df26c40eSEric W. Biederman 	/* Allow access to a task's file descriptors if it is us or we
529df26c40eSEric W. Biederman 	 * may use ptrace attach to the process and find out that
530df26c40eSEric W. Biederman 	 * information.
531778c1144SEric W. Biederman 	 */
532778c1144SEric W. Biederman 	task = get_proc_task(inode);
533df26c40eSEric W. Biederman 	if (task) {
534006ebb40SStephen Smalley 		allowed = ptrace_may_access(task, PTRACE_MODE_READ);
535778c1144SEric W. Biederman 		put_task_struct(task);
536df26c40eSEric W. Biederman 	}
537778c1144SEric W. Biederman 	return allowed;
5381da177e4SLinus Torvalds }
5391da177e4SLinus Torvalds 
5406b4e306aSEric W. Biederman int proc_setattr(struct dentry *dentry, struct iattr *attr)
5416d76fa58SLinus Torvalds {
5426d76fa58SLinus Torvalds 	int error;
5436d76fa58SLinus Torvalds 	struct inode *inode = dentry->d_inode;
5446d76fa58SLinus Torvalds 
5456d76fa58SLinus Torvalds 	if (attr->ia_valid & ATTR_MODE)
5466d76fa58SLinus Torvalds 		return -EPERM;
5476d76fa58SLinus Torvalds 
5486d76fa58SLinus Torvalds 	error = inode_change_ok(inode, attr);
5491025774cSChristoph Hellwig 	if (error)
5506d76fa58SLinus Torvalds 		return error;
5511025774cSChristoph Hellwig 
5521025774cSChristoph Hellwig 	setattr_copy(inode, attr);
5531025774cSChristoph Hellwig 	mark_inode_dirty(inode);
5541025774cSChristoph Hellwig 	return 0;
5556d76fa58SLinus Torvalds }
5566d76fa58SLinus Torvalds 
5570499680aSVasiliy Kulikov /*
5580499680aSVasiliy Kulikov  * May current process learn task's sched/cmdline info (for hide_pid_min=1)
5590499680aSVasiliy Kulikov  * or euid/egid (for hide_pid_min=2)?
5600499680aSVasiliy Kulikov  */
5610499680aSVasiliy Kulikov static bool has_pid_permissions(struct pid_namespace *pid,
5620499680aSVasiliy Kulikov 				 struct task_struct *task,
5630499680aSVasiliy Kulikov 				 int hide_pid_min)
5640499680aSVasiliy Kulikov {
5650499680aSVasiliy Kulikov 	if (pid->hide_pid < hide_pid_min)
5660499680aSVasiliy Kulikov 		return true;
5670499680aSVasiliy Kulikov 	if (in_group_p(pid->pid_gid))
5680499680aSVasiliy Kulikov 		return true;
5690499680aSVasiliy Kulikov 	return ptrace_may_access(task, PTRACE_MODE_READ);
5700499680aSVasiliy Kulikov }
5710499680aSVasiliy Kulikov 
5720499680aSVasiliy Kulikov 
5730499680aSVasiliy Kulikov static int proc_pid_permission(struct inode *inode, int mask)
5740499680aSVasiliy Kulikov {
5750499680aSVasiliy Kulikov 	struct pid_namespace *pid = inode->i_sb->s_fs_info;
5760499680aSVasiliy Kulikov 	struct task_struct *task;
5770499680aSVasiliy Kulikov 	bool has_perms;
5780499680aSVasiliy Kulikov 
5790499680aSVasiliy Kulikov 	task = get_proc_task(inode);
580a2ef990aSXiaotian Feng 	if (!task)
581a2ef990aSXiaotian Feng 		return -ESRCH;
5820499680aSVasiliy Kulikov 	has_perms = has_pid_permissions(pid, task, 1);
5830499680aSVasiliy Kulikov 	put_task_struct(task);
5840499680aSVasiliy Kulikov 
5850499680aSVasiliy Kulikov 	if (!has_perms) {
5860499680aSVasiliy Kulikov 		if (pid->hide_pid == 2) {
5870499680aSVasiliy Kulikov 			/*
5880499680aSVasiliy Kulikov 			 * Let's make getdents(), stat(), and open()
5890499680aSVasiliy Kulikov 			 * consistent with each other.  If a process
5900499680aSVasiliy Kulikov 			 * may not stat() a file, it shouldn't be seen
5910499680aSVasiliy Kulikov 			 * in procfs at all.
5920499680aSVasiliy Kulikov 			 */
5930499680aSVasiliy Kulikov 			return -ENOENT;
5940499680aSVasiliy Kulikov 		}
5950499680aSVasiliy Kulikov 
5960499680aSVasiliy Kulikov 		return -EPERM;
5970499680aSVasiliy Kulikov 	}
5980499680aSVasiliy Kulikov 	return generic_permission(inode, mask);
5990499680aSVasiliy Kulikov }
6000499680aSVasiliy Kulikov 
6010499680aSVasiliy Kulikov 
6020499680aSVasiliy Kulikov 
603c5ef1c42SArjan van de Ven static const struct inode_operations proc_def_inode_operations = {
6046d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
6056d76fa58SLinus Torvalds };
6066d76fa58SLinus Torvalds 
6071da177e4SLinus Torvalds #define PROC_BLOCK_SIZE	(3*1024)		/* 4K page size but our output routines use some slack for overruns */
6081da177e4SLinus Torvalds 
6091da177e4SLinus Torvalds static ssize_t proc_info_read(struct file * file, char __user * buf,
6101da177e4SLinus Torvalds 			  size_t count, loff_t *ppos)
6111da177e4SLinus Torvalds {
612496ad9aaSAl Viro 	struct inode * inode = file_inode(file);
6131da177e4SLinus Torvalds 	unsigned long page;
6141da177e4SLinus Torvalds 	ssize_t length;
61599f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
61699f89551SEric W. Biederman 
61799f89551SEric W. Biederman 	length = -ESRCH;
61899f89551SEric W. Biederman 	if (!task)
61999f89551SEric W. Biederman 		goto out_no_task;
6201da177e4SLinus Torvalds 
6211da177e4SLinus Torvalds 	if (count > PROC_BLOCK_SIZE)
6221da177e4SLinus Torvalds 		count = PROC_BLOCK_SIZE;
62399f89551SEric W. Biederman 
62499f89551SEric W. Biederman 	length = -ENOMEM;
625e12ba74dSMel Gorman 	if (!(page = __get_free_page(GFP_TEMPORARY)))
62699f89551SEric W. Biederman 		goto out;
6271da177e4SLinus Torvalds 
6281da177e4SLinus Torvalds 	length = PROC_I(inode)->op.proc_read(task, (char*)page);
6291da177e4SLinus Torvalds 
6301da177e4SLinus Torvalds 	if (length >= 0)
6311da177e4SLinus Torvalds 		length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
6321da177e4SLinus Torvalds 	free_page(page);
63399f89551SEric W. Biederman out:
63499f89551SEric W. Biederman 	put_task_struct(task);
63599f89551SEric W. Biederman out_no_task:
6361da177e4SLinus Torvalds 	return length;
6371da177e4SLinus Torvalds }
6381da177e4SLinus Torvalds 
63900977a59SArjan van de Ven static const struct file_operations proc_info_file_operations = {
6401da177e4SLinus Torvalds 	.read		= proc_info_read,
64187df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
6421da177e4SLinus Torvalds };
6431da177e4SLinus Torvalds 
644be614086SEric W. Biederman static int proc_single_show(struct seq_file *m, void *v)
645be614086SEric W. Biederman {
646be614086SEric W. Biederman 	struct inode *inode = m->private;
647be614086SEric W. Biederman 	struct pid_namespace *ns;
648be614086SEric W. Biederman 	struct pid *pid;
649be614086SEric W. Biederman 	struct task_struct *task;
650be614086SEric W. Biederman 	int ret;
651be614086SEric W. Biederman 
652be614086SEric W. Biederman 	ns = inode->i_sb->s_fs_info;
653be614086SEric W. Biederman 	pid = proc_pid(inode);
654be614086SEric W. Biederman 	task = get_pid_task(pid, PIDTYPE_PID);
655be614086SEric W. Biederman 	if (!task)
656be614086SEric W. Biederman 		return -ESRCH;
657be614086SEric W. Biederman 
658be614086SEric W. Biederman 	ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
659be614086SEric W. Biederman 
660be614086SEric W. Biederman 	put_task_struct(task);
661be614086SEric W. Biederman 	return ret;
662be614086SEric W. Biederman }
663be614086SEric W. Biederman 
664be614086SEric W. Biederman static int proc_single_open(struct inode *inode, struct file *filp)
665be614086SEric W. Biederman {
666c6a34058SJovi Zhang 	return single_open(filp, proc_single_show, inode);
667be614086SEric W. Biederman }
668be614086SEric W. Biederman 
669be614086SEric W. Biederman static const struct file_operations proc_single_file_operations = {
670be614086SEric W. Biederman 	.open		= proc_single_open,
671be614086SEric W. Biederman 	.read		= seq_read,
672be614086SEric W. Biederman 	.llseek		= seq_lseek,
673be614086SEric W. Biederman 	.release	= single_release,
674be614086SEric W. Biederman };
675be614086SEric W. Biederman 
676b409e578SCong Wang static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
6771da177e4SLinus Torvalds {
678496ad9aaSAl Viro 	struct task_struct *task = get_proc_task(file_inode(file));
679e268337dSLinus Torvalds 	struct mm_struct *mm;
680e268337dSLinus Torvalds 
681e268337dSLinus Torvalds 	if (!task)
682e268337dSLinus Torvalds 		return -ESRCH;
683e268337dSLinus Torvalds 
684b409e578SCong Wang 	mm = mm_access(task, mode);
685e268337dSLinus Torvalds 	put_task_struct(task);
686e268337dSLinus Torvalds 
687e268337dSLinus Torvalds 	if (IS_ERR(mm))
688e268337dSLinus Torvalds 		return PTR_ERR(mm);
689e268337dSLinus Torvalds 
6906d08f2c7SOleg Nesterov 	if (mm) {
6916d08f2c7SOleg Nesterov 		/* ensure this mm_struct can't be freed */
6926d08f2c7SOleg Nesterov 		atomic_inc(&mm->mm_count);
6936d08f2c7SOleg Nesterov 		/* but do not pin its memory */
6946d08f2c7SOleg Nesterov 		mmput(mm);
6956d08f2c7SOleg Nesterov 	}
6966d08f2c7SOleg Nesterov 
697e268337dSLinus Torvalds 	file->private_data = mm;
698e268337dSLinus Torvalds 
6991da177e4SLinus Torvalds 	return 0;
7001da177e4SLinus Torvalds }
7011da177e4SLinus Torvalds 
702b409e578SCong Wang static int mem_open(struct inode *inode, struct file *file)
703b409e578SCong Wang {
704bc452b4bSDjalal Harouni 	int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
705bc452b4bSDjalal Harouni 
706bc452b4bSDjalal Harouni 	/* OK to pass negative loff_t, we can catch out-of-range */
707bc452b4bSDjalal Harouni 	file->f_mode |= FMODE_UNSIGNED_OFFSET;
708bc452b4bSDjalal Harouni 
709bc452b4bSDjalal Harouni 	return ret;
710b409e578SCong Wang }
711b409e578SCong Wang 
712572d34b9SOleg Nesterov static ssize_t mem_rw(struct file *file, char __user *buf,
713572d34b9SOleg Nesterov 			size_t count, loff_t *ppos, int write)
7141da177e4SLinus Torvalds {
715e268337dSLinus Torvalds 	struct mm_struct *mm = file->private_data;
716572d34b9SOleg Nesterov 	unsigned long addr = *ppos;
717572d34b9SOleg Nesterov 	ssize_t copied;
7181da177e4SLinus Torvalds 	char *page;
7191da177e4SLinus Torvalds 
720e268337dSLinus Torvalds 	if (!mm)
721e268337dSLinus Torvalds 		return 0;
7221da177e4SLinus Torvalds 
723e12ba74dSMel Gorman 	page = (char *)__get_free_page(GFP_TEMPORARY);
7241da177e4SLinus Torvalds 	if (!page)
725e268337dSLinus Torvalds 		return -ENOMEM;
7261da177e4SLinus Torvalds 
727f7ca54f4SFrederik Deweerdt 	copied = 0;
7286d08f2c7SOleg Nesterov 	if (!atomic_inc_not_zero(&mm->mm_users))
7296d08f2c7SOleg Nesterov 		goto free;
7306d08f2c7SOleg Nesterov 
7311da177e4SLinus Torvalds 	while (count > 0) {
732572d34b9SOleg Nesterov 		int this_len = min_t(int, count, PAGE_SIZE);
7331da177e4SLinus Torvalds 
734572d34b9SOleg Nesterov 		if (write && copy_from_user(page, buf, this_len)) {
7351da177e4SLinus Torvalds 			copied = -EFAULT;
7361da177e4SLinus Torvalds 			break;
7371da177e4SLinus Torvalds 		}
738572d34b9SOleg Nesterov 
739572d34b9SOleg Nesterov 		this_len = access_remote_vm(mm, addr, page, this_len, write);
740572d34b9SOleg Nesterov 		if (!this_len) {
7411da177e4SLinus Torvalds 			if (!copied)
7421da177e4SLinus Torvalds 				copied = -EIO;
7431da177e4SLinus Torvalds 			break;
7441da177e4SLinus Torvalds 		}
745572d34b9SOleg Nesterov 
746572d34b9SOleg Nesterov 		if (!write && copy_to_user(buf, page, this_len)) {
747572d34b9SOleg Nesterov 			copied = -EFAULT;
748572d34b9SOleg Nesterov 			break;
7491da177e4SLinus Torvalds 		}
750572d34b9SOleg Nesterov 
751572d34b9SOleg Nesterov 		buf += this_len;
752572d34b9SOleg Nesterov 		addr += this_len;
753572d34b9SOleg Nesterov 		copied += this_len;
754572d34b9SOleg Nesterov 		count -= this_len;
755572d34b9SOleg Nesterov 	}
756572d34b9SOleg Nesterov 	*ppos = addr;
75730cd8903SKOSAKI Motohiro 
7586d08f2c7SOleg Nesterov 	mmput(mm);
7596d08f2c7SOleg Nesterov free:
76030cd8903SKOSAKI Motohiro 	free_page((unsigned long) page);
7611da177e4SLinus Torvalds 	return copied;
7621da177e4SLinus Torvalds }
7631da177e4SLinus Torvalds 
764572d34b9SOleg Nesterov static ssize_t mem_read(struct file *file, char __user *buf,
765572d34b9SOleg Nesterov 			size_t count, loff_t *ppos)
766572d34b9SOleg Nesterov {
767572d34b9SOleg Nesterov 	return mem_rw(file, buf, count, ppos, 0);
768572d34b9SOleg Nesterov }
769572d34b9SOleg Nesterov 
770572d34b9SOleg Nesterov static ssize_t mem_write(struct file *file, const char __user *buf,
771572d34b9SOleg Nesterov 			 size_t count, loff_t *ppos)
772572d34b9SOleg Nesterov {
773572d34b9SOleg Nesterov 	return mem_rw(file, (char __user*)buf, count, ppos, 1);
774572d34b9SOleg Nesterov }
775572d34b9SOleg Nesterov 
77685863e47SMatt Mackall loff_t mem_lseek(struct file *file, loff_t offset, int orig)
7771da177e4SLinus Torvalds {
7781da177e4SLinus Torvalds 	switch (orig) {
7791da177e4SLinus Torvalds 	case 0:
7801da177e4SLinus Torvalds 		file->f_pos = offset;
7811da177e4SLinus Torvalds 		break;
7821da177e4SLinus Torvalds 	case 1:
7831da177e4SLinus Torvalds 		file->f_pos += offset;
7841da177e4SLinus Torvalds 		break;
7851da177e4SLinus Torvalds 	default:
7861da177e4SLinus Torvalds 		return -EINVAL;
7871da177e4SLinus Torvalds 	}
7881da177e4SLinus Torvalds 	force_successful_syscall_return();
7891da177e4SLinus Torvalds 	return file->f_pos;
7901da177e4SLinus Torvalds }
7911da177e4SLinus Torvalds 
792e268337dSLinus Torvalds static int mem_release(struct inode *inode, struct file *file)
793e268337dSLinus Torvalds {
794e268337dSLinus Torvalds 	struct mm_struct *mm = file->private_data;
79571879d3cSOleg Nesterov 	if (mm)
7966d08f2c7SOleg Nesterov 		mmdrop(mm);
797e268337dSLinus Torvalds 	return 0;
798e268337dSLinus Torvalds }
799e268337dSLinus Torvalds 
80000977a59SArjan van de Ven static const struct file_operations proc_mem_operations = {
8011da177e4SLinus Torvalds 	.llseek		= mem_lseek,
8021da177e4SLinus Torvalds 	.read		= mem_read,
8031da177e4SLinus Torvalds 	.write		= mem_write,
8041da177e4SLinus Torvalds 	.open		= mem_open,
805e268337dSLinus Torvalds 	.release	= mem_release,
8061da177e4SLinus Torvalds };
8071da177e4SLinus Torvalds 
808b409e578SCong Wang static int environ_open(struct inode *inode, struct file *file)
809b409e578SCong Wang {
810b409e578SCong Wang 	return __mem_open(inode, file, PTRACE_MODE_READ);
811b409e578SCong Wang }
812b409e578SCong Wang 
813315e28c8SJames Pearson static ssize_t environ_read(struct file *file, char __user *buf,
814315e28c8SJames Pearson 			size_t count, loff_t *ppos)
815315e28c8SJames Pearson {
816315e28c8SJames Pearson 	char *page;
817315e28c8SJames Pearson 	unsigned long src = *ppos;
818b409e578SCong Wang 	int ret = 0;
819b409e578SCong Wang 	struct mm_struct *mm = file->private_data;
820315e28c8SJames Pearson 
821b409e578SCong Wang 	if (!mm)
822b409e578SCong Wang 		return 0;
823315e28c8SJames Pearson 
824315e28c8SJames Pearson 	page = (char *)__get_free_page(GFP_TEMPORARY);
825315e28c8SJames Pearson 	if (!page)
826b409e578SCong Wang 		return -ENOMEM;
827315e28c8SJames Pearson 
828d6f64b89SAl Viro 	ret = 0;
829b409e578SCong Wang 	if (!atomic_inc_not_zero(&mm->mm_users))
830b409e578SCong Wang 		goto free;
831315e28c8SJames Pearson 	while (count > 0) {
832e8905ec2SDjalal Harouni 		size_t this_len, max_len;
833e8905ec2SDjalal Harouni 		int retval;
834e8905ec2SDjalal Harouni 
835e8905ec2SDjalal Harouni 		if (src >= (mm->env_end - mm->env_start))
836e8905ec2SDjalal Harouni 			break;
837315e28c8SJames Pearson 
838315e28c8SJames Pearson 		this_len = mm->env_end - (mm->env_start + src);
839315e28c8SJames Pearson 
840e8905ec2SDjalal Harouni 		max_len = min_t(size_t, PAGE_SIZE, count);
841e8905ec2SDjalal Harouni 		this_len = min(max_len, this_len);
842315e28c8SJames Pearson 
843b409e578SCong Wang 		retval = access_remote_vm(mm, (mm->env_start + src),
844315e28c8SJames Pearson 			page, this_len, 0);
845315e28c8SJames Pearson 
846315e28c8SJames Pearson 		if (retval <= 0) {
847315e28c8SJames Pearson 			ret = retval;
848315e28c8SJames Pearson 			break;
849315e28c8SJames Pearson 		}
850315e28c8SJames Pearson 
851315e28c8SJames Pearson 		if (copy_to_user(buf, page, retval)) {
852315e28c8SJames Pearson 			ret = -EFAULT;
853315e28c8SJames Pearson 			break;
854315e28c8SJames Pearson 		}
855315e28c8SJames Pearson 
856315e28c8SJames Pearson 		ret += retval;
857315e28c8SJames Pearson 		src += retval;
858315e28c8SJames Pearson 		buf += retval;
859315e28c8SJames Pearson 		count -= retval;
860315e28c8SJames Pearson 	}
861315e28c8SJames Pearson 	*ppos = src;
862315e28c8SJames Pearson 	mmput(mm);
863b409e578SCong Wang 
864b409e578SCong Wang free:
865315e28c8SJames Pearson 	free_page((unsigned long) page);
866315e28c8SJames Pearson 	return ret;
867315e28c8SJames Pearson }
868315e28c8SJames Pearson 
869315e28c8SJames Pearson static const struct file_operations proc_environ_operations = {
870b409e578SCong Wang 	.open		= environ_open,
871315e28c8SJames Pearson 	.read		= environ_read,
87287df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
873b409e578SCong Wang 	.release	= mem_release,
874315e28c8SJames Pearson };
875315e28c8SJames Pearson 
876fa0cbbf1SDavid Rientjes static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
877fa0cbbf1SDavid Rientjes 			    loff_t *ppos)
878fa0cbbf1SDavid Rientjes {
879496ad9aaSAl Viro 	struct task_struct *task = get_proc_task(file_inode(file));
880fa0cbbf1SDavid Rientjes 	char buffer[PROC_NUMBUF];
881fa0cbbf1SDavid Rientjes 	int oom_adj = OOM_ADJUST_MIN;
882fa0cbbf1SDavid Rientjes 	size_t len;
883fa0cbbf1SDavid Rientjes 	unsigned long flags;
884fa0cbbf1SDavid Rientjes 
885fa0cbbf1SDavid Rientjes 	if (!task)
886fa0cbbf1SDavid Rientjes 		return -ESRCH;
887fa0cbbf1SDavid Rientjes 	if (lock_task_sighand(task, &flags)) {
888fa0cbbf1SDavid Rientjes 		if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
889fa0cbbf1SDavid Rientjes 			oom_adj = OOM_ADJUST_MAX;
890fa0cbbf1SDavid Rientjes 		else
891fa0cbbf1SDavid Rientjes 			oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
892fa0cbbf1SDavid Rientjes 				  OOM_SCORE_ADJ_MAX;
893fa0cbbf1SDavid Rientjes 		unlock_task_sighand(task, &flags);
894fa0cbbf1SDavid Rientjes 	}
895fa0cbbf1SDavid Rientjes 	put_task_struct(task);
896fa0cbbf1SDavid Rientjes 	len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
897fa0cbbf1SDavid Rientjes 	return simple_read_from_buffer(buf, count, ppos, buffer, len);
898fa0cbbf1SDavid Rientjes }
899fa0cbbf1SDavid Rientjes 
900fa0cbbf1SDavid Rientjes static ssize_t oom_adj_write(struct file *file, const char __user *buf,
901fa0cbbf1SDavid Rientjes 			     size_t count, loff_t *ppos)
902fa0cbbf1SDavid Rientjes {
903fa0cbbf1SDavid Rientjes 	struct task_struct *task;
904fa0cbbf1SDavid Rientjes 	char buffer[PROC_NUMBUF];
905fa0cbbf1SDavid Rientjes 	int oom_adj;
906fa0cbbf1SDavid Rientjes 	unsigned long flags;
907fa0cbbf1SDavid Rientjes 	int err;
908fa0cbbf1SDavid Rientjes 
909fa0cbbf1SDavid Rientjes 	memset(buffer, 0, sizeof(buffer));
910fa0cbbf1SDavid Rientjes 	if (count > sizeof(buffer) - 1)
911fa0cbbf1SDavid Rientjes 		count = sizeof(buffer) - 1;
912fa0cbbf1SDavid Rientjes 	if (copy_from_user(buffer, buf, count)) {
913fa0cbbf1SDavid Rientjes 		err = -EFAULT;
914fa0cbbf1SDavid Rientjes 		goto out;
915fa0cbbf1SDavid Rientjes 	}
916fa0cbbf1SDavid Rientjes 
917fa0cbbf1SDavid Rientjes 	err = kstrtoint(strstrip(buffer), 0, &oom_adj);
918fa0cbbf1SDavid Rientjes 	if (err)
919fa0cbbf1SDavid Rientjes 		goto out;
920fa0cbbf1SDavid Rientjes 	if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
921fa0cbbf1SDavid Rientjes 	     oom_adj != OOM_DISABLE) {
922fa0cbbf1SDavid Rientjes 		err = -EINVAL;
923fa0cbbf1SDavid Rientjes 		goto out;
924fa0cbbf1SDavid Rientjes 	}
925fa0cbbf1SDavid Rientjes 
926496ad9aaSAl Viro 	task = get_proc_task(file_inode(file));
927fa0cbbf1SDavid Rientjes 	if (!task) {
928fa0cbbf1SDavid Rientjes 		err = -ESRCH;
929fa0cbbf1SDavid Rientjes 		goto out;
930fa0cbbf1SDavid Rientjes 	}
931fa0cbbf1SDavid Rientjes 
932fa0cbbf1SDavid Rientjes 	task_lock(task);
933fa0cbbf1SDavid Rientjes 	if (!task->mm) {
934fa0cbbf1SDavid Rientjes 		err = -EINVAL;
935fa0cbbf1SDavid Rientjes 		goto err_task_lock;
936fa0cbbf1SDavid Rientjes 	}
937fa0cbbf1SDavid Rientjes 
938fa0cbbf1SDavid Rientjes 	if (!lock_task_sighand(task, &flags)) {
939fa0cbbf1SDavid Rientjes 		err = -ESRCH;
940fa0cbbf1SDavid Rientjes 		goto err_task_lock;
941fa0cbbf1SDavid Rientjes 	}
942fa0cbbf1SDavid Rientjes 
943fa0cbbf1SDavid Rientjes 	/*
944fa0cbbf1SDavid Rientjes 	 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
945fa0cbbf1SDavid Rientjes 	 * value is always attainable.
946fa0cbbf1SDavid Rientjes 	 */
947fa0cbbf1SDavid Rientjes 	if (oom_adj == OOM_ADJUST_MAX)
948fa0cbbf1SDavid Rientjes 		oom_adj = OOM_SCORE_ADJ_MAX;
949fa0cbbf1SDavid Rientjes 	else
950fa0cbbf1SDavid Rientjes 		oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
951fa0cbbf1SDavid Rientjes 
952fa0cbbf1SDavid Rientjes 	if (oom_adj < task->signal->oom_score_adj &&
953fa0cbbf1SDavid Rientjes 	    !capable(CAP_SYS_RESOURCE)) {
954fa0cbbf1SDavid Rientjes 		err = -EACCES;
955fa0cbbf1SDavid Rientjes 		goto err_sighand;
956fa0cbbf1SDavid Rientjes 	}
957fa0cbbf1SDavid Rientjes 
958fa0cbbf1SDavid Rientjes 	/*
959fa0cbbf1SDavid Rientjes 	 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
960fa0cbbf1SDavid Rientjes 	 * /proc/pid/oom_score_adj instead.
961fa0cbbf1SDavid Rientjes 	 */
96287ebdc00SAndrew Morton 	pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
963fa0cbbf1SDavid Rientjes 		  current->comm, task_pid_nr(current), task_pid_nr(task),
964fa0cbbf1SDavid Rientjes 		  task_pid_nr(task));
965fa0cbbf1SDavid Rientjes 
966fa0cbbf1SDavid Rientjes 	task->signal->oom_score_adj = oom_adj;
967fa0cbbf1SDavid Rientjes 	trace_oom_score_adj_update(task);
968fa0cbbf1SDavid Rientjes err_sighand:
969fa0cbbf1SDavid Rientjes 	unlock_task_sighand(task, &flags);
970fa0cbbf1SDavid Rientjes err_task_lock:
971fa0cbbf1SDavid Rientjes 	task_unlock(task);
972fa0cbbf1SDavid Rientjes 	put_task_struct(task);
973fa0cbbf1SDavid Rientjes out:
974fa0cbbf1SDavid Rientjes 	return err < 0 ? err : count;
975fa0cbbf1SDavid Rientjes }
976fa0cbbf1SDavid Rientjes 
977fa0cbbf1SDavid Rientjes static const struct file_operations proc_oom_adj_operations = {
978fa0cbbf1SDavid Rientjes 	.read		= oom_adj_read,
979fa0cbbf1SDavid Rientjes 	.write		= oom_adj_write,
980fa0cbbf1SDavid Rientjes 	.llseek		= generic_file_llseek,
981fa0cbbf1SDavid Rientjes };
982fa0cbbf1SDavid Rientjes 
983a63d83f4SDavid Rientjes static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
984a63d83f4SDavid Rientjes 					size_t count, loff_t *ppos)
985a63d83f4SDavid Rientjes {
986496ad9aaSAl Viro 	struct task_struct *task = get_proc_task(file_inode(file));
987a63d83f4SDavid Rientjes 	char buffer[PROC_NUMBUF];
988a9c58b90SDavid Rientjes 	short oom_score_adj = OOM_SCORE_ADJ_MIN;
989a63d83f4SDavid Rientjes 	unsigned long flags;
990a63d83f4SDavid Rientjes 	size_t len;
991a63d83f4SDavid Rientjes 
992a63d83f4SDavid Rientjes 	if (!task)
993a63d83f4SDavid Rientjes 		return -ESRCH;
994a63d83f4SDavid Rientjes 	if (lock_task_sighand(task, &flags)) {
995a63d83f4SDavid Rientjes 		oom_score_adj = task->signal->oom_score_adj;
996a63d83f4SDavid Rientjes 		unlock_task_sighand(task, &flags);
997a63d83f4SDavid Rientjes 	}
998a63d83f4SDavid Rientjes 	put_task_struct(task);
999a9c58b90SDavid Rientjes 	len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
1000a63d83f4SDavid Rientjes 	return simple_read_from_buffer(buf, count, ppos, buffer, len);
1001a63d83f4SDavid Rientjes }
1002a63d83f4SDavid Rientjes 
1003a63d83f4SDavid Rientjes static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1004a63d83f4SDavid Rientjes 					size_t count, loff_t *ppos)
1005a63d83f4SDavid Rientjes {
1006a63d83f4SDavid Rientjes 	struct task_struct *task;
1007a63d83f4SDavid Rientjes 	char buffer[PROC_NUMBUF];
1008a63d83f4SDavid Rientjes 	unsigned long flags;
10090a8cb8e3SAlexey Dobriyan 	int oom_score_adj;
1010a63d83f4SDavid Rientjes 	int err;
1011a63d83f4SDavid Rientjes 
1012a63d83f4SDavid Rientjes 	memset(buffer, 0, sizeof(buffer));
1013a63d83f4SDavid Rientjes 	if (count > sizeof(buffer) - 1)
1014a63d83f4SDavid Rientjes 		count = sizeof(buffer) - 1;
1015723548bfSDavid Rientjes 	if (copy_from_user(buffer, buf, count)) {
1016723548bfSDavid Rientjes 		err = -EFAULT;
1017723548bfSDavid Rientjes 		goto out;
1018723548bfSDavid Rientjes 	}
1019a63d83f4SDavid Rientjes 
10200a8cb8e3SAlexey Dobriyan 	err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
1021a63d83f4SDavid Rientjes 	if (err)
1022723548bfSDavid Rientjes 		goto out;
1023a63d83f4SDavid Rientjes 	if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
1024723548bfSDavid Rientjes 			oom_score_adj > OOM_SCORE_ADJ_MAX) {
1025723548bfSDavid Rientjes 		err = -EINVAL;
1026723548bfSDavid Rientjes 		goto out;
1027723548bfSDavid Rientjes 	}
1028a63d83f4SDavid Rientjes 
1029496ad9aaSAl Viro 	task = get_proc_task(file_inode(file));
1030723548bfSDavid Rientjes 	if (!task) {
1031723548bfSDavid Rientjes 		err = -ESRCH;
1032723548bfSDavid Rientjes 		goto out;
1033723548bfSDavid Rientjes 	}
1034a63d83f4SDavid Rientjes 
10353d5992d2SYing Han 	task_lock(task);
10363d5992d2SYing Han 	if (!task->mm) {
1037723548bfSDavid Rientjes 		err = -EINVAL;
1038723548bfSDavid Rientjes 		goto err_task_lock;
10393d5992d2SYing Han 	}
1040d19d5476SDavid Rientjes 
1041d19d5476SDavid Rientjes 	if (!lock_task_sighand(task, &flags)) {
1042d19d5476SDavid Rientjes 		err = -ESRCH;
1043d19d5476SDavid Rientjes 		goto err_task_lock;
1044d19d5476SDavid Rientjes 	}
1045d19d5476SDavid Rientjes 
1046a9c58b90SDavid Rientjes 	if ((short)oom_score_adj < task->signal->oom_score_adj_min &&
1047d19d5476SDavid Rientjes 			!capable(CAP_SYS_RESOURCE)) {
1048d19d5476SDavid Rientjes 		err = -EACCES;
1049d19d5476SDavid Rientjes 		goto err_sighand;
1050d19d5476SDavid Rientjes 	}
1051d19d5476SDavid Rientjes 
1052a9c58b90SDavid Rientjes 	task->signal->oom_score_adj = (short)oom_score_adj;
1053dabb16f6SMandeep Singh Baines 	if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
1054a9c58b90SDavid Rientjes 		task->signal->oom_score_adj_min = (short)oom_score_adj;
105543d2b113SKAMEZAWA Hiroyuki 	trace_oom_score_adj_update(task);
105601dc52ebSDavidlohr Bueso 
1057723548bfSDavid Rientjes err_sighand:
1058a63d83f4SDavid Rientjes 	unlock_task_sighand(task, &flags);
1059d19d5476SDavid Rientjes err_task_lock:
1060d19d5476SDavid Rientjes 	task_unlock(task);
1061a63d83f4SDavid Rientjes 	put_task_struct(task);
1062723548bfSDavid Rientjes out:
1063723548bfSDavid Rientjes 	return err < 0 ? err : count;
1064a63d83f4SDavid Rientjes }
1065a63d83f4SDavid Rientjes 
1066a63d83f4SDavid Rientjes static const struct file_operations proc_oom_score_adj_operations = {
1067a63d83f4SDavid Rientjes 	.read		= oom_score_adj_read,
1068a63d83f4SDavid Rientjes 	.write		= oom_score_adj_write,
10696038f373SArnd Bergmann 	.llseek		= default_llseek,
1070a63d83f4SDavid Rientjes };
1071a63d83f4SDavid Rientjes 
10721da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL
10731da177e4SLinus Torvalds #define TMPBUFLEN 21
10741da177e4SLinus Torvalds static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
10751da177e4SLinus Torvalds 				  size_t count, loff_t *ppos)
10761da177e4SLinus Torvalds {
1077496ad9aaSAl Viro 	struct inode * inode = file_inode(file);
107899f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
10791da177e4SLinus Torvalds 	ssize_t length;
10801da177e4SLinus Torvalds 	char tmpbuf[TMPBUFLEN];
10811da177e4SLinus Torvalds 
108299f89551SEric W. Biederman 	if (!task)
108399f89551SEric W. Biederman 		return -ESRCH;
10841da177e4SLinus Torvalds 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1085e1760bd5SEric W. Biederman 			   from_kuid(file->f_cred->user_ns,
1086e1760bd5SEric W. Biederman 				     audit_get_loginuid(task)));
108799f89551SEric W. Biederman 	put_task_struct(task);
10881da177e4SLinus Torvalds 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
10891da177e4SLinus Torvalds }
10901da177e4SLinus Torvalds 
10911da177e4SLinus Torvalds static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
10921da177e4SLinus Torvalds 				   size_t count, loff_t *ppos)
10931da177e4SLinus Torvalds {
1094496ad9aaSAl Viro 	struct inode * inode = file_inode(file);
10951da177e4SLinus Torvalds 	char *page, *tmp;
10961da177e4SLinus Torvalds 	ssize_t length;
10971da177e4SLinus Torvalds 	uid_t loginuid;
1098e1760bd5SEric W. Biederman 	kuid_t kloginuid;
10991da177e4SLinus Torvalds 
11007dc52157SPaul E. McKenney 	rcu_read_lock();
11017dc52157SPaul E. McKenney 	if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
11027dc52157SPaul E. McKenney 		rcu_read_unlock();
11031da177e4SLinus Torvalds 		return -EPERM;
11047dc52157SPaul E. McKenney 	}
11057dc52157SPaul E. McKenney 	rcu_read_unlock();
11061da177e4SLinus Torvalds 
1107e0182909SAl Viro 	if (count >= PAGE_SIZE)
1108e0182909SAl Viro 		count = PAGE_SIZE - 1;
11091da177e4SLinus Torvalds 
11101da177e4SLinus Torvalds 	if (*ppos != 0) {
11111da177e4SLinus Torvalds 		/* No partial writes. */
11121da177e4SLinus Torvalds 		return -EINVAL;
11131da177e4SLinus Torvalds 	}
1114e12ba74dSMel Gorman 	page = (char*)__get_free_page(GFP_TEMPORARY);
11151da177e4SLinus Torvalds 	if (!page)
11161da177e4SLinus Torvalds 		return -ENOMEM;
11171da177e4SLinus Torvalds 	length = -EFAULT;
11181da177e4SLinus Torvalds 	if (copy_from_user(page, buf, count))
11191da177e4SLinus Torvalds 		goto out_free_page;
11201da177e4SLinus Torvalds 
1121e0182909SAl Viro 	page[count] = '\0';
11221da177e4SLinus Torvalds 	loginuid = simple_strtoul(page, &tmp, 10);
11231da177e4SLinus Torvalds 	if (tmp == page) {
11241da177e4SLinus Torvalds 		length = -EINVAL;
11251da177e4SLinus Torvalds 		goto out_free_page;
11261da177e4SLinus Torvalds 
11271da177e4SLinus Torvalds 	}
112881407c84SEric Paris 
112981407c84SEric Paris 	/* is userspace tring to explicitly UNSET the loginuid? */
113081407c84SEric Paris 	if (loginuid == AUDIT_UID_UNSET) {
113181407c84SEric Paris 		kloginuid = INVALID_UID;
113281407c84SEric Paris 	} else {
1133e1760bd5SEric W. Biederman 		kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
1134e1760bd5SEric W. Biederman 		if (!uid_valid(kloginuid)) {
1135e1760bd5SEric W. Biederman 			length = -EINVAL;
1136e1760bd5SEric W. Biederman 			goto out_free_page;
1137e1760bd5SEric W. Biederman 		}
113881407c84SEric Paris 	}
1139e1760bd5SEric W. Biederman 
1140e1760bd5SEric W. Biederman 	length = audit_set_loginuid(kloginuid);
11411da177e4SLinus Torvalds 	if (likely(length == 0))
11421da177e4SLinus Torvalds 		length = count;
11431da177e4SLinus Torvalds 
11441da177e4SLinus Torvalds out_free_page:
11451da177e4SLinus Torvalds 	free_page((unsigned long) page);
11461da177e4SLinus Torvalds 	return length;
11471da177e4SLinus Torvalds }
11481da177e4SLinus Torvalds 
114900977a59SArjan van de Ven static const struct file_operations proc_loginuid_operations = {
11501da177e4SLinus Torvalds 	.read		= proc_loginuid_read,
11511da177e4SLinus Torvalds 	.write		= proc_loginuid_write,
115287df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
11531da177e4SLinus Torvalds };
11541e0bd755SEric Paris 
11551e0bd755SEric Paris static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
11561e0bd755SEric Paris 				  size_t count, loff_t *ppos)
11571e0bd755SEric Paris {
1158496ad9aaSAl Viro 	struct inode * inode = file_inode(file);
11591e0bd755SEric Paris 	struct task_struct *task = get_proc_task(inode);
11601e0bd755SEric Paris 	ssize_t length;
11611e0bd755SEric Paris 	char tmpbuf[TMPBUFLEN];
11621e0bd755SEric Paris 
11631e0bd755SEric Paris 	if (!task)
11641e0bd755SEric Paris 		return -ESRCH;
11651e0bd755SEric Paris 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
11661e0bd755SEric Paris 				audit_get_sessionid(task));
11671e0bd755SEric Paris 	put_task_struct(task);
11681e0bd755SEric Paris 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
11691e0bd755SEric Paris }
11701e0bd755SEric Paris 
11711e0bd755SEric Paris static const struct file_operations proc_sessionid_operations = {
11721e0bd755SEric Paris 	.read		= proc_sessionid_read,
117387df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
11741e0bd755SEric Paris };
11751da177e4SLinus Torvalds #endif
11761da177e4SLinus Torvalds 
1177f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
1178f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1179f4f154fdSAkinobu Mita 				      size_t count, loff_t *ppos)
1180f4f154fdSAkinobu Mita {
1181496ad9aaSAl Viro 	struct task_struct *task = get_proc_task(file_inode(file));
1182f4f154fdSAkinobu Mita 	char buffer[PROC_NUMBUF];
1183f4f154fdSAkinobu Mita 	size_t len;
1184f4f154fdSAkinobu Mita 	int make_it_fail;
1185f4f154fdSAkinobu Mita 
1186f4f154fdSAkinobu Mita 	if (!task)
1187f4f154fdSAkinobu Mita 		return -ESRCH;
1188f4f154fdSAkinobu Mita 	make_it_fail = task->make_it_fail;
1189f4f154fdSAkinobu Mita 	put_task_struct(task);
1190f4f154fdSAkinobu Mita 
1191f4f154fdSAkinobu Mita 	len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
11920c28f287SAkinobu Mita 
11930c28f287SAkinobu Mita 	return simple_read_from_buffer(buf, count, ppos, buffer, len);
1194f4f154fdSAkinobu Mita }
1195f4f154fdSAkinobu Mita 
1196f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_write(struct file * file,
1197f4f154fdSAkinobu Mita 			const char __user * buf, size_t count, loff_t *ppos)
1198f4f154fdSAkinobu Mita {
1199f4f154fdSAkinobu Mita 	struct task_struct *task;
1200f4f154fdSAkinobu Mita 	char buffer[PROC_NUMBUF], *end;
1201f4f154fdSAkinobu Mita 	int make_it_fail;
1202f4f154fdSAkinobu Mita 
1203f4f154fdSAkinobu Mita 	if (!capable(CAP_SYS_RESOURCE))
1204f4f154fdSAkinobu Mita 		return -EPERM;
1205f4f154fdSAkinobu Mita 	memset(buffer, 0, sizeof(buffer));
1206f4f154fdSAkinobu Mita 	if (count > sizeof(buffer) - 1)
1207f4f154fdSAkinobu Mita 		count = sizeof(buffer) - 1;
1208f4f154fdSAkinobu Mita 	if (copy_from_user(buffer, buf, count))
1209f4f154fdSAkinobu Mita 		return -EFAULT;
1210cba8aafeSVincent Li 	make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1211cba8aafeSVincent Li 	if (*end)
1212cba8aafeSVincent Li 		return -EINVAL;
121316caed31SDave Jones 	if (make_it_fail < 0 || make_it_fail > 1)
121416caed31SDave Jones 		return -EINVAL;
121516caed31SDave Jones 
1216496ad9aaSAl Viro 	task = get_proc_task(file_inode(file));
1217f4f154fdSAkinobu Mita 	if (!task)
1218f4f154fdSAkinobu Mita 		return -ESRCH;
1219f4f154fdSAkinobu Mita 	task->make_it_fail = make_it_fail;
1220f4f154fdSAkinobu Mita 	put_task_struct(task);
1221cba8aafeSVincent Li 
1222cba8aafeSVincent Li 	return count;
1223f4f154fdSAkinobu Mita }
1224f4f154fdSAkinobu Mita 
122500977a59SArjan van de Ven static const struct file_operations proc_fault_inject_operations = {
1226f4f154fdSAkinobu Mita 	.read		= proc_fault_inject_read,
1227f4f154fdSAkinobu Mita 	.write		= proc_fault_inject_write,
122887df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
1229f4f154fdSAkinobu Mita };
1230f4f154fdSAkinobu Mita #endif
1231f4f154fdSAkinobu Mita 
12329745512cSArjan van de Ven 
123343ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
123443ae34cbSIngo Molnar /*
123543ae34cbSIngo Molnar  * Print out various scheduling related per-task fields:
123643ae34cbSIngo Molnar  */
123743ae34cbSIngo Molnar static int sched_show(struct seq_file *m, void *v)
123843ae34cbSIngo Molnar {
123943ae34cbSIngo Molnar 	struct inode *inode = m->private;
124043ae34cbSIngo Molnar 	struct task_struct *p;
124143ae34cbSIngo Molnar 
124243ae34cbSIngo Molnar 	p = get_proc_task(inode);
124343ae34cbSIngo Molnar 	if (!p)
124443ae34cbSIngo Molnar 		return -ESRCH;
124543ae34cbSIngo Molnar 	proc_sched_show_task(p, m);
124643ae34cbSIngo Molnar 
124743ae34cbSIngo Molnar 	put_task_struct(p);
124843ae34cbSIngo Molnar 
124943ae34cbSIngo Molnar 	return 0;
125043ae34cbSIngo Molnar }
125143ae34cbSIngo Molnar 
125243ae34cbSIngo Molnar static ssize_t
125343ae34cbSIngo Molnar sched_write(struct file *file, const char __user *buf,
125443ae34cbSIngo Molnar 	    size_t count, loff_t *offset)
125543ae34cbSIngo Molnar {
1256496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
125743ae34cbSIngo Molnar 	struct task_struct *p;
125843ae34cbSIngo Molnar 
125943ae34cbSIngo Molnar 	p = get_proc_task(inode);
126043ae34cbSIngo Molnar 	if (!p)
126143ae34cbSIngo Molnar 		return -ESRCH;
126243ae34cbSIngo Molnar 	proc_sched_set_task(p);
126343ae34cbSIngo Molnar 
126443ae34cbSIngo Molnar 	put_task_struct(p);
126543ae34cbSIngo Molnar 
126643ae34cbSIngo Molnar 	return count;
126743ae34cbSIngo Molnar }
126843ae34cbSIngo Molnar 
126943ae34cbSIngo Molnar static int sched_open(struct inode *inode, struct file *filp)
127043ae34cbSIngo Molnar {
1271c6a34058SJovi Zhang 	return single_open(filp, sched_show, inode);
127243ae34cbSIngo Molnar }
127343ae34cbSIngo Molnar 
127443ae34cbSIngo Molnar static const struct file_operations proc_pid_sched_operations = {
127543ae34cbSIngo Molnar 	.open		= sched_open,
127643ae34cbSIngo Molnar 	.read		= seq_read,
127743ae34cbSIngo Molnar 	.write		= sched_write,
127843ae34cbSIngo Molnar 	.llseek		= seq_lseek,
12795ea473a1SAlexey Dobriyan 	.release	= single_release,
128043ae34cbSIngo Molnar };
128143ae34cbSIngo Molnar 
128243ae34cbSIngo Molnar #endif
128343ae34cbSIngo Molnar 
12845091faa4SMike Galbraith #ifdef CONFIG_SCHED_AUTOGROUP
12855091faa4SMike Galbraith /*
12865091faa4SMike Galbraith  * Print out autogroup related information:
12875091faa4SMike Galbraith  */
12885091faa4SMike Galbraith static int sched_autogroup_show(struct seq_file *m, void *v)
12895091faa4SMike Galbraith {
12905091faa4SMike Galbraith 	struct inode *inode = m->private;
12915091faa4SMike Galbraith 	struct task_struct *p;
12925091faa4SMike Galbraith 
12935091faa4SMike Galbraith 	p = get_proc_task(inode);
12945091faa4SMike Galbraith 	if (!p)
12955091faa4SMike Galbraith 		return -ESRCH;
12965091faa4SMike Galbraith 	proc_sched_autogroup_show_task(p, m);
12975091faa4SMike Galbraith 
12985091faa4SMike Galbraith 	put_task_struct(p);
12995091faa4SMike Galbraith 
13005091faa4SMike Galbraith 	return 0;
13015091faa4SMike Galbraith }
13025091faa4SMike Galbraith 
13035091faa4SMike Galbraith static ssize_t
13045091faa4SMike Galbraith sched_autogroup_write(struct file *file, const char __user *buf,
13055091faa4SMike Galbraith 	    size_t count, loff_t *offset)
13065091faa4SMike Galbraith {
1307496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
13085091faa4SMike Galbraith 	struct task_struct *p;
13095091faa4SMike Galbraith 	char buffer[PROC_NUMBUF];
13100a8cb8e3SAlexey Dobriyan 	int nice;
13115091faa4SMike Galbraith 	int err;
13125091faa4SMike Galbraith 
13135091faa4SMike Galbraith 	memset(buffer, 0, sizeof(buffer));
13145091faa4SMike Galbraith 	if (count > sizeof(buffer) - 1)
13155091faa4SMike Galbraith 		count = sizeof(buffer) - 1;
13165091faa4SMike Galbraith 	if (copy_from_user(buffer, buf, count))
13175091faa4SMike Galbraith 		return -EFAULT;
13185091faa4SMike Galbraith 
13190a8cb8e3SAlexey Dobriyan 	err = kstrtoint(strstrip(buffer), 0, &nice);
13200a8cb8e3SAlexey Dobriyan 	if (err < 0)
13210a8cb8e3SAlexey Dobriyan 		return err;
13225091faa4SMike Galbraith 
13235091faa4SMike Galbraith 	p = get_proc_task(inode);
13245091faa4SMike Galbraith 	if (!p)
13255091faa4SMike Galbraith 		return -ESRCH;
13265091faa4SMike Galbraith 
13272e5b5b3aSHiroshi Shimamoto 	err = proc_sched_autogroup_set_nice(p, nice);
13285091faa4SMike Galbraith 	if (err)
13295091faa4SMike Galbraith 		count = err;
13305091faa4SMike Galbraith 
13315091faa4SMike Galbraith 	put_task_struct(p);
13325091faa4SMike Galbraith 
13335091faa4SMike Galbraith 	return count;
13345091faa4SMike Galbraith }
13355091faa4SMike Galbraith 
13365091faa4SMike Galbraith static int sched_autogroup_open(struct inode *inode, struct file *filp)
13375091faa4SMike Galbraith {
13385091faa4SMike Galbraith 	int ret;
13395091faa4SMike Galbraith 
13405091faa4SMike Galbraith 	ret = single_open(filp, sched_autogroup_show, NULL);
13415091faa4SMike Galbraith 	if (!ret) {
13425091faa4SMike Galbraith 		struct seq_file *m = filp->private_data;
13435091faa4SMike Galbraith 
13445091faa4SMike Galbraith 		m->private = inode;
13455091faa4SMike Galbraith 	}
13465091faa4SMike Galbraith 	return ret;
13475091faa4SMike Galbraith }
13485091faa4SMike Galbraith 
13495091faa4SMike Galbraith static const struct file_operations proc_pid_sched_autogroup_operations = {
13505091faa4SMike Galbraith 	.open		= sched_autogroup_open,
13515091faa4SMike Galbraith 	.read		= seq_read,
13525091faa4SMike Galbraith 	.write		= sched_autogroup_write,
13535091faa4SMike Galbraith 	.llseek		= seq_lseek,
13545091faa4SMike Galbraith 	.release	= single_release,
13555091faa4SMike Galbraith };
13565091faa4SMike Galbraith 
13575091faa4SMike Galbraith #endif /* CONFIG_SCHED_AUTOGROUP */
13585091faa4SMike Galbraith 
13594614a696Sjohn stultz static ssize_t comm_write(struct file *file, const char __user *buf,
13604614a696Sjohn stultz 				size_t count, loff_t *offset)
13614614a696Sjohn stultz {
1362496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
13634614a696Sjohn stultz 	struct task_struct *p;
13644614a696Sjohn stultz 	char buffer[TASK_COMM_LEN];
1365830e0fc9SDavid Rientjes 	const size_t maxlen = sizeof(buffer) - 1;
13664614a696Sjohn stultz 
13674614a696Sjohn stultz 	memset(buffer, 0, sizeof(buffer));
1368830e0fc9SDavid Rientjes 	if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
13694614a696Sjohn stultz 		return -EFAULT;
13704614a696Sjohn stultz 
13714614a696Sjohn stultz 	p = get_proc_task(inode);
13724614a696Sjohn stultz 	if (!p)
13734614a696Sjohn stultz 		return -ESRCH;
13744614a696Sjohn stultz 
13754614a696Sjohn stultz 	if (same_thread_group(current, p))
13764614a696Sjohn stultz 		set_task_comm(p, buffer);
13774614a696Sjohn stultz 	else
13784614a696Sjohn stultz 		count = -EINVAL;
13794614a696Sjohn stultz 
13804614a696Sjohn stultz 	put_task_struct(p);
13814614a696Sjohn stultz 
13824614a696Sjohn stultz 	return count;
13834614a696Sjohn stultz }
13844614a696Sjohn stultz 
13854614a696Sjohn stultz static int comm_show(struct seq_file *m, void *v)
13864614a696Sjohn stultz {
13874614a696Sjohn stultz 	struct inode *inode = m->private;
13884614a696Sjohn stultz 	struct task_struct *p;
13894614a696Sjohn stultz 
13904614a696Sjohn stultz 	p = get_proc_task(inode);
13914614a696Sjohn stultz 	if (!p)
13924614a696Sjohn stultz 		return -ESRCH;
13934614a696Sjohn stultz 
13944614a696Sjohn stultz 	task_lock(p);
13954614a696Sjohn stultz 	seq_printf(m, "%s\n", p->comm);
13964614a696Sjohn stultz 	task_unlock(p);
13974614a696Sjohn stultz 
13984614a696Sjohn stultz 	put_task_struct(p);
13994614a696Sjohn stultz 
14004614a696Sjohn stultz 	return 0;
14014614a696Sjohn stultz }
14024614a696Sjohn stultz 
14034614a696Sjohn stultz static int comm_open(struct inode *inode, struct file *filp)
14044614a696Sjohn stultz {
1405c6a34058SJovi Zhang 	return single_open(filp, comm_show, inode);
14064614a696Sjohn stultz }
14074614a696Sjohn stultz 
14084614a696Sjohn stultz static const struct file_operations proc_pid_set_comm_operations = {
14094614a696Sjohn stultz 	.open		= comm_open,
14104614a696Sjohn stultz 	.read		= seq_read,
14114614a696Sjohn stultz 	.write		= comm_write,
14124614a696Sjohn stultz 	.llseek		= seq_lseek,
14134614a696Sjohn stultz 	.release	= single_release,
14144614a696Sjohn stultz };
14154614a696Sjohn stultz 
14167773fbc5SCyrill Gorcunov static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
1417925d1c40SMatt Helsley {
1418925d1c40SMatt Helsley 	struct task_struct *task;
1419925d1c40SMatt Helsley 	struct mm_struct *mm;
1420925d1c40SMatt Helsley 	struct file *exe_file;
1421925d1c40SMatt Helsley 
14227773fbc5SCyrill Gorcunov 	task = get_proc_task(dentry->d_inode);
1423925d1c40SMatt Helsley 	if (!task)
1424925d1c40SMatt Helsley 		return -ENOENT;
1425925d1c40SMatt Helsley 	mm = get_task_mm(task);
1426925d1c40SMatt Helsley 	put_task_struct(task);
1427925d1c40SMatt Helsley 	if (!mm)
1428925d1c40SMatt Helsley 		return -ENOENT;
1429925d1c40SMatt Helsley 	exe_file = get_mm_exe_file(mm);
1430925d1c40SMatt Helsley 	mmput(mm);
1431925d1c40SMatt Helsley 	if (exe_file) {
1432925d1c40SMatt Helsley 		*exe_path = exe_file->f_path;
1433925d1c40SMatt Helsley 		path_get(&exe_file->f_path);
1434925d1c40SMatt Helsley 		fput(exe_file);
1435925d1c40SMatt Helsley 		return 0;
1436925d1c40SMatt Helsley 	} else
1437925d1c40SMatt Helsley 		return -ENOENT;
1438925d1c40SMatt Helsley }
1439925d1c40SMatt Helsley 
1440008b150aSAl Viro static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
14411da177e4SLinus Torvalds {
14421da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
1443408ef013SChristoph Hellwig 	struct path path;
14441da177e4SLinus Torvalds 	int error = -EACCES;
14451da177e4SLinus Torvalds 
1446778c1144SEric W. Biederman 	/* Are we allowed to snoop on the tasks file descriptors? */
1447778c1144SEric W. Biederman 	if (!proc_fd_access_allowed(inode))
14481da177e4SLinus Torvalds 		goto out;
14491da177e4SLinus Torvalds 
1450408ef013SChristoph Hellwig 	error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1451408ef013SChristoph Hellwig 	if (error)
1452408ef013SChristoph Hellwig 		goto out;
1453408ef013SChristoph Hellwig 
1454b5fb63c1SChristoph Hellwig 	nd_jump_link(nd, &path);
1455408ef013SChristoph Hellwig 	return NULL;
14561da177e4SLinus Torvalds out:
1457008b150aSAl Viro 	return ERR_PTR(error);
14581da177e4SLinus Torvalds }
14591da177e4SLinus Torvalds 
14603dcd25f3SJan Blunck static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
14611da177e4SLinus Torvalds {
1462e12ba74dSMel Gorman 	char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
14633dcd25f3SJan Blunck 	char *pathname;
14641da177e4SLinus Torvalds 	int len;
14651da177e4SLinus Torvalds 
14661da177e4SLinus Torvalds 	if (!tmp)
14671da177e4SLinus Torvalds 		return -ENOMEM;
14681da177e4SLinus Torvalds 
14697b2a69baSEric W. Biederman 	pathname = d_path(path, tmp, PAGE_SIZE);
14703dcd25f3SJan Blunck 	len = PTR_ERR(pathname);
14713dcd25f3SJan Blunck 	if (IS_ERR(pathname))
14721da177e4SLinus Torvalds 		goto out;
14733dcd25f3SJan Blunck 	len = tmp + PAGE_SIZE - 1 - pathname;
14741da177e4SLinus Torvalds 
14751da177e4SLinus Torvalds 	if (len > buflen)
14761da177e4SLinus Torvalds 		len = buflen;
14773dcd25f3SJan Blunck 	if (copy_to_user(buffer, pathname, len))
14781da177e4SLinus Torvalds 		len = -EFAULT;
14791da177e4SLinus Torvalds  out:
14801da177e4SLinus Torvalds 	free_page((unsigned long)tmp);
14811da177e4SLinus Torvalds 	return len;
14821da177e4SLinus Torvalds }
14831da177e4SLinus Torvalds 
14841da177e4SLinus Torvalds static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
14851da177e4SLinus Torvalds {
14861da177e4SLinus Torvalds 	int error = -EACCES;
14871da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
14883dcd25f3SJan Blunck 	struct path path;
14891da177e4SLinus Torvalds 
1490778c1144SEric W. Biederman 	/* Are we allowed to snoop on the tasks file descriptors? */
1491778c1144SEric W. Biederman 	if (!proc_fd_access_allowed(inode))
14921da177e4SLinus Torvalds 		goto out;
14931da177e4SLinus Torvalds 
14947773fbc5SCyrill Gorcunov 	error = PROC_I(inode)->op.proc_get_link(dentry, &path);
14951da177e4SLinus Torvalds 	if (error)
14961da177e4SLinus Torvalds 		goto out;
14971da177e4SLinus Torvalds 
14983dcd25f3SJan Blunck 	error = do_proc_readlink(&path, buffer, buflen);
14993dcd25f3SJan Blunck 	path_put(&path);
15001da177e4SLinus Torvalds out:
15011da177e4SLinus Torvalds 	return error;
15021da177e4SLinus Torvalds }
15031da177e4SLinus Torvalds 
1504faf60af1SCyrill Gorcunov const struct inode_operations proc_pid_link_inode_operations = {
15051da177e4SLinus Torvalds 	.readlink	= proc_pid_readlink,
15066d76fa58SLinus Torvalds 	.follow_link	= proc_pid_follow_link,
15076d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
15081da177e4SLinus Torvalds };
15091da177e4SLinus Torvalds 
151028a6d671SEric W. Biederman 
151128a6d671SEric W. Biederman /* building an inode */
151228a6d671SEric W. Biederman 
15136b4e306aSEric W. Biederman struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
151428a6d671SEric W. Biederman {
151528a6d671SEric W. Biederman 	struct inode * inode;
151628a6d671SEric W. Biederman 	struct proc_inode *ei;
1517c69e8d9cSDavid Howells 	const struct cred *cred;
151828a6d671SEric W. Biederman 
151928a6d671SEric W. Biederman 	/* We need a new inode */
152028a6d671SEric W. Biederman 
152128a6d671SEric W. Biederman 	inode = new_inode(sb);
152228a6d671SEric W. Biederman 	if (!inode)
152328a6d671SEric W. Biederman 		goto out;
152428a6d671SEric W. Biederman 
152528a6d671SEric W. Biederman 	/* Common stuff */
152628a6d671SEric W. Biederman 	ei = PROC_I(inode);
152785fe4025SChristoph Hellwig 	inode->i_ino = get_next_ino();
152828a6d671SEric W. Biederman 	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
152928a6d671SEric W. Biederman 	inode->i_op = &proc_def_inode_operations;
153028a6d671SEric W. Biederman 
153128a6d671SEric W. Biederman 	/*
153228a6d671SEric W. Biederman 	 * grab the reference to task.
153328a6d671SEric W. Biederman 	 */
15341a657f78SOleg Nesterov 	ei->pid = get_task_pid(task, PIDTYPE_PID);
153528a6d671SEric W. Biederman 	if (!ei->pid)
153628a6d671SEric W. Biederman 		goto out_unlock;
153728a6d671SEric W. Biederman 
153828a6d671SEric W. Biederman 	if (task_dumpable(task)) {
1539c69e8d9cSDavid Howells 		rcu_read_lock();
1540c69e8d9cSDavid Howells 		cred = __task_cred(task);
1541c69e8d9cSDavid Howells 		inode->i_uid = cred->euid;
1542c69e8d9cSDavid Howells 		inode->i_gid = cred->egid;
1543c69e8d9cSDavid Howells 		rcu_read_unlock();
154428a6d671SEric W. Biederman 	}
154528a6d671SEric W. Biederman 	security_task_to_inode(task, inode);
154628a6d671SEric W. Biederman 
154728a6d671SEric W. Biederman out:
154828a6d671SEric W. Biederman 	return inode;
154928a6d671SEric W. Biederman 
155028a6d671SEric W. Biederman out_unlock:
155128a6d671SEric W. Biederman 	iput(inode);
155228a6d671SEric W. Biederman 	return NULL;
155328a6d671SEric W. Biederman }
155428a6d671SEric W. Biederman 
15556b4e306aSEric W. Biederman int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
155628a6d671SEric W. Biederman {
155728a6d671SEric W. Biederman 	struct inode *inode = dentry->d_inode;
155828a6d671SEric W. Biederman 	struct task_struct *task;
1559c69e8d9cSDavid Howells 	const struct cred *cred;
15600499680aSVasiliy Kulikov 	struct pid_namespace *pid = dentry->d_sb->s_fs_info;
1561c69e8d9cSDavid Howells 
156228a6d671SEric W. Biederman 	generic_fillattr(inode, stat);
156328a6d671SEric W. Biederman 
156428a6d671SEric W. Biederman 	rcu_read_lock();
1565dcb0f222SEric W. Biederman 	stat->uid = GLOBAL_ROOT_UID;
1566dcb0f222SEric W. Biederman 	stat->gid = GLOBAL_ROOT_GID;
156728a6d671SEric W. Biederman 	task = pid_task(proc_pid(inode), PIDTYPE_PID);
156828a6d671SEric W. Biederman 	if (task) {
15690499680aSVasiliy Kulikov 		if (!has_pid_permissions(pid, task, 2)) {
15700499680aSVasiliy Kulikov 			rcu_read_unlock();
15710499680aSVasiliy Kulikov 			/*
15720499680aSVasiliy Kulikov 			 * This doesn't prevent learning whether PID exists,
15730499680aSVasiliy Kulikov 			 * it only makes getattr() consistent with readdir().
15740499680aSVasiliy Kulikov 			 */
15750499680aSVasiliy Kulikov 			return -ENOENT;
15760499680aSVasiliy Kulikov 		}
157728a6d671SEric W. Biederman 		if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
157828a6d671SEric W. Biederman 		    task_dumpable(task)) {
1579c69e8d9cSDavid Howells 			cred = __task_cred(task);
1580c69e8d9cSDavid Howells 			stat->uid = cred->euid;
1581c69e8d9cSDavid Howells 			stat->gid = cred->egid;
158228a6d671SEric W. Biederman 		}
158328a6d671SEric W. Biederman 	}
158428a6d671SEric W. Biederman 	rcu_read_unlock();
158528a6d671SEric W. Biederman 	return 0;
158628a6d671SEric W. Biederman }
158728a6d671SEric W. Biederman 
158828a6d671SEric W. Biederman /* dentry stuff */
158928a6d671SEric W. Biederman 
159028a6d671SEric W. Biederman /*
159128a6d671SEric W. Biederman  *	Exceptional case: normally we are not allowed to unhash a busy
159228a6d671SEric W. Biederman  * directory. In this case, however, we can do it - no aliasing problems
159328a6d671SEric W. Biederman  * due to the way we treat inodes.
159428a6d671SEric W. Biederman  *
159528a6d671SEric W. Biederman  * Rewrite the inode's ownerships here because the owning task may have
159628a6d671SEric W. Biederman  * performed a setuid(), etc.
159728a6d671SEric W. Biederman  *
159828a6d671SEric W. Biederman  * Before the /proc/pid/status file was created the only way to read
159928a6d671SEric W. Biederman  * the effective uid of a /process was to stat /proc/pid.  Reading
160028a6d671SEric W. Biederman  * /proc/pid/status is slow enough that procps and other packages
160128a6d671SEric W. Biederman  * kept stating /proc/pid.  To keep the rules in /proc simple I have
160228a6d671SEric W. Biederman  * made this apply to all per process world readable and executable
160328a6d671SEric W. Biederman  * directories.
160428a6d671SEric W. Biederman  */
16050b728e19SAl Viro int pid_revalidate(struct dentry *dentry, unsigned int flags)
160628a6d671SEric W. Biederman {
160734286d66SNick Piggin 	struct inode *inode;
160834286d66SNick Piggin 	struct task_struct *task;
1609c69e8d9cSDavid Howells 	const struct cred *cred;
1610c69e8d9cSDavid Howells 
16110b728e19SAl Viro 	if (flags & LOOKUP_RCU)
161234286d66SNick Piggin 		return -ECHILD;
161334286d66SNick Piggin 
161434286d66SNick Piggin 	inode = dentry->d_inode;
161534286d66SNick Piggin 	task = get_proc_task(inode);
161634286d66SNick Piggin 
161728a6d671SEric W. Biederman 	if (task) {
161828a6d671SEric W. Biederman 		if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
161928a6d671SEric W. Biederman 		    task_dumpable(task)) {
1620c69e8d9cSDavid Howells 			rcu_read_lock();
1621c69e8d9cSDavid Howells 			cred = __task_cred(task);
1622c69e8d9cSDavid Howells 			inode->i_uid = cred->euid;
1623c69e8d9cSDavid Howells 			inode->i_gid = cred->egid;
1624c69e8d9cSDavid Howells 			rcu_read_unlock();
162528a6d671SEric W. Biederman 		} else {
1626dcb0f222SEric W. Biederman 			inode->i_uid = GLOBAL_ROOT_UID;
1627dcb0f222SEric W. Biederman 			inode->i_gid = GLOBAL_ROOT_GID;
162828a6d671SEric W. Biederman 		}
162928a6d671SEric W. Biederman 		inode->i_mode &= ~(S_ISUID | S_ISGID);
163028a6d671SEric W. Biederman 		security_task_to_inode(task, inode);
163128a6d671SEric W. Biederman 		put_task_struct(task);
163228a6d671SEric W. Biederman 		return 1;
163328a6d671SEric W. Biederman 	}
163428a6d671SEric W. Biederman 	d_drop(dentry);
163528a6d671SEric W. Biederman 	return 0;
163628a6d671SEric W. Biederman }
163728a6d671SEric W. Biederman 
1638d855a4b7SOleg Nesterov static inline bool proc_inode_is_dead(struct inode *inode)
1639d855a4b7SOleg Nesterov {
1640d855a4b7SOleg Nesterov 	return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1641d855a4b7SOleg Nesterov }
1642d855a4b7SOleg Nesterov 
16431dd704b6SDavid Howells int pid_delete_dentry(const struct dentry *dentry)
16441dd704b6SDavid Howells {
16451dd704b6SDavid Howells 	/* Is the task we represent dead?
16461dd704b6SDavid Howells 	 * If so, then don't put the dentry on the lru list,
16471dd704b6SDavid Howells 	 * kill it immediately.
16481dd704b6SDavid Howells 	 */
1649d855a4b7SOleg Nesterov 	return proc_inode_is_dead(dentry->d_inode);
16501dd704b6SDavid Howells }
16511dd704b6SDavid Howells 
16526b4e306aSEric W. Biederman const struct dentry_operations pid_dentry_operations =
165328a6d671SEric W. Biederman {
165428a6d671SEric W. Biederman 	.d_revalidate	= pid_revalidate,
165528a6d671SEric W. Biederman 	.d_delete	= pid_delete_dentry,
165628a6d671SEric W. Biederman };
165728a6d671SEric W. Biederman 
165828a6d671SEric W. Biederman /* Lookups */
165928a6d671SEric W. Biederman 
16601c0d04c9SEric W. Biederman /*
16611c0d04c9SEric W. Biederman  * Fill a directory entry.
16621c0d04c9SEric W. Biederman  *
16631c0d04c9SEric W. Biederman  * If possible create the dcache entry and derive our inode number and
16641c0d04c9SEric W. Biederman  * file type from dcache entry.
16651c0d04c9SEric W. Biederman  *
16661c0d04c9SEric W. Biederman  * Since all of the proc inode numbers are dynamically generated, the inode
16671c0d04c9SEric W. Biederman  * numbers do not exist until the inode is cache.  This means creating the
16681c0d04c9SEric W. Biederman  * the dcache entry in readdir is necessary to keep the inode numbers
16691c0d04c9SEric W. Biederman  * reported by readdir in sync with the inode numbers reported
16701c0d04c9SEric W. Biederman  * by stat.
16711c0d04c9SEric W. Biederman  */
1672f0c3b509SAl Viro bool proc_fill_cache(struct file *file, struct dir_context *ctx,
16736b4e306aSEric W. Biederman 	const char *name, int len,
1674c5141e6dSEric Dumazet 	instantiate_t instantiate, struct task_struct *task, const void *ptr)
167561a28784SEric W. Biederman {
1676f0c3b509SAl Viro 	struct dentry *child, *dir = file->f_path.dentry;
16771df98b8bSAl Viro 	struct qstr qname = QSTR_INIT(name, len);
167861a28784SEric W. Biederman 	struct inode *inode;
16791df98b8bSAl Viro 	unsigned type;
16801df98b8bSAl Viro 	ino_t ino;
168161a28784SEric W. Biederman 
16821df98b8bSAl Viro 	child = d_hash_and_lookup(dir, &qname);
168361a28784SEric W. Biederman 	if (!child) {
16841df98b8bSAl Viro 		child = d_alloc(dir, &qname);
16851df98b8bSAl Viro 		if (!child)
168661a28784SEric W. Biederman 			goto end_instantiate;
16871df98b8bSAl Viro 		if (instantiate(dir->d_inode, child, task, ptr) < 0) {
16881df98b8bSAl Viro 			dput(child);
16891df98b8bSAl Viro 			goto end_instantiate;
16901df98b8bSAl Viro 		}
16911df98b8bSAl Viro 	}
169261a28784SEric W. Biederman 	inode = child->d_inode;
169361a28784SEric W. Biederman 	ino = inode->i_ino;
169461a28784SEric W. Biederman 	type = inode->i_mode >> 12;
169561a28784SEric W. Biederman 	dput(child);
1696f0c3b509SAl Viro 	return dir_emit(ctx, name, len, ino, type);
16971df98b8bSAl Viro 
16981df98b8bSAl Viro end_instantiate:
16991df98b8bSAl Viro 	return dir_emit(ctx, name, len, 1, DT_UNKNOWN);
170061a28784SEric W. Biederman }
170161a28784SEric W. Biederman 
1702640708a2SPavel Emelyanov #ifdef CONFIG_CHECKPOINT_RESTORE
1703640708a2SPavel Emelyanov 
1704640708a2SPavel Emelyanov /*
1705640708a2SPavel Emelyanov  * dname_to_vma_addr - maps a dentry name into two unsigned longs
1706640708a2SPavel Emelyanov  * which represent vma start and end addresses.
1707640708a2SPavel Emelyanov  */
1708640708a2SPavel Emelyanov static int dname_to_vma_addr(struct dentry *dentry,
1709640708a2SPavel Emelyanov 			     unsigned long *start, unsigned long *end)
1710640708a2SPavel Emelyanov {
1711640708a2SPavel Emelyanov 	if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2)
1712640708a2SPavel Emelyanov 		return -EINVAL;
1713640708a2SPavel Emelyanov 
1714640708a2SPavel Emelyanov 	return 0;
1715640708a2SPavel Emelyanov }
1716640708a2SPavel Emelyanov 
17170b728e19SAl Viro static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
1718640708a2SPavel Emelyanov {
1719640708a2SPavel Emelyanov 	unsigned long vm_start, vm_end;
1720640708a2SPavel Emelyanov 	bool exact_vma_exists = false;
1721640708a2SPavel Emelyanov 	struct mm_struct *mm = NULL;
1722640708a2SPavel Emelyanov 	struct task_struct *task;
1723640708a2SPavel Emelyanov 	const struct cred *cred;
1724640708a2SPavel Emelyanov 	struct inode *inode;
1725640708a2SPavel Emelyanov 	int status = 0;
1726640708a2SPavel Emelyanov 
17270b728e19SAl Viro 	if (flags & LOOKUP_RCU)
1728640708a2SPavel Emelyanov 		return -ECHILD;
1729640708a2SPavel Emelyanov 
1730640708a2SPavel Emelyanov 	if (!capable(CAP_SYS_ADMIN)) {
173141735818SZhao Hongjiang 		status = -EPERM;
1732640708a2SPavel Emelyanov 		goto out_notask;
1733640708a2SPavel Emelyanov 	}
1734640708a2SPavel Emelyanov 
1735640708a2SPavel Emelyanov 	inode = dentry->d_inode;
1736640708a2SPavel Emelyanov 	task = get_proc_task(inode);
1737640708a2SPavel Emelyanov 	if (!task)
1738640708a2SPavel Emelyanov 		goto out_notask;
1739640708a2SPavel Emelyanov 
17402344bec7SCong Wang 	mm = mm_access(task, PTRACE_MODE_READ);
17412344bec7SCong Wang 	if (IS_ERR_OR_NULL(mm))
1742640708a2SPavel Emelyanov 		goto out;
1743640708a2SPavel Emelyanov 
1744640708a2SPavel Emelyanov 	if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1745640708a2SPavel Emelyanov 		down_read(&mm->mmap_sem);
1746640708a2SPavel Emelyanov 		exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1747640708a2SPavel Emelyanov 		up_read(&mm->mmap_sem);
1748640708a2SPavel Emelyanov 	}
1749640708a2SPavel Emelyanov 
1750640708a2SPavel Emelyanov 	mmput(mm);
1751640708a2SPavel Emelyanov 
1752640708a2SPavel Emelyanov 	if (exact_vma_exists) {
1753640708a2SPavel Emelyanov 		if (task_dumpable(task)) {
1754640708a2SPavel Emelyanov 			rcu_read_lock();
1755640708a2SPavel Emelyanov 			cred = __task_cred(task);
1756640708a2SPavel Emelyanov 			inode->i_uid = cred->euid;
1757640708a2SPavel Emelyanov 			inode->i_gid = cred->egid;
1758640708a2SPavel Emelyanov 			rcu_read_unlock();
1759640708a2SPavel Emelyanov 		} else {
1760dcb0f222SEric W. Biederman 			inode->i_uid = GLOBAL_ROOT_UID;
1761dcb0f222SEric W. Biederman 			inode->i_gid = GLOBAL_ROOT_GID;
1762640708a2SPavel Emelyanov 		}
1763640708a2SPavel Emelyanov 		security_task_to_inode(task, inode);
1764640708a2SPavel Emelyanov 		status = 1;
1765640708a2SPavel Emelyanov 	}
1766640708a2SPavel Emelyanov 
1767640708a2SPavel Emelyanov out:
1768640708a2SPavel Emelyanov 	put_task_struct(task);
1769640708a2SPavel Emelyanov 
1770640708a2SPavel Emelyanov out_notask:
1771640708a2SPavel Emelyanov 	if (status <= 0)
1772640708a2SPavel Emelyanov 		d_drop(dentry);
1773640708a2SPavel Emelyanov 
1774640708a2SPavel Emelyanov 	return status;
1775640708a2SPavel Emelyanov }
1776640708a2SPavel Emelyanov 
1777640708a2SPavel Emelyanov static const struct dentry_operations tid_map_files_dentry_operations = {
1778640708a2SPavel Emelyanov 	.d_revalidate	= map_files_d_revalidate,
1779640708a2SPavel Emelyanov 	.d_delete	= pid_delete_dentry,
1780640708a2SPavel Emelyanov };
1781640708a2SPavel Emelyanov 
1782640708a2SPavel Emelyanov static int proc_map_files_get_link(struct dentry *dentry, struct path *path)
1783640708a2SPavel Emelyanov {
1784640708a2SPavel Emelyanov 	unsigned long vm_start, vm_end;
1785640708a2SPavel Emelyanov 	struct vm_area_struct *vma;
1786640708a2SPavel Emelyanov 	struct task_struct *task;
1787640708a2SPavel Emelyanov 	struct mm_struct *mm;
1788640708a2SPavel Emelyanov 	int rc;
1789640708a2SPavel Emelyanov 
1790640708a2SPavel Emelyanov 	rc = -ENOENT;
1791640708a2SPavel Emelyanov 	task = get_proc_task(dentry->d_inode);
1792640708a2SPavel Emelyanov 	if (!task)
1793640708a2SPavel Emelyanov 		goto out;
1794640708a2SPavel Emelyanov 
1795640708a2SPavel Emelyanov 	mm = get_task_mm(task);
1796640708a2SPavel Emelyanov 	put_task_struct(task);
1797640708a2SPavel Emelyanov 	if (!mm)
1798640708a2SPavel Emelyanov 		goto out;
1799640708a2SPavel Emelyanov 
1800640708a2SPavel Emelyanov 	rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
1801640708a2SPavel Emelyanov 	if (rc)
1802640708a2SPavel Emelyanov 		goto out_mmput;
1803640708a2SPavel Emelyanov 
180470335abbSArtem Fetishev 	rc = -ENOENT;
1805640708a2SPavel Emelyanov 	down_read(&mm->mmap_sem);
1806640708a2SPavel Emelyanov 	vma = find_exact_vma(mm, vm_start, vm_end);
1807640708a2SPavel Emelyanov 	if (vma && vma->vm_file) {
1808640708a2SPavel Emelyanov 		*path = vma->vm_file->f_path;
1809640708a2SPavel Emelyanov 		path_get(path);
1810640708a2SPavel Emelyanov 		rc = 0;
1811640708a2SPavel Emelyanov 	}
1812640708a2SPavel Emelyanov 	up_read(&mm->mmap_sem);
1813640708a2SPavel Emelyanov 
1814640708a2SPavel Emelyanov out_mmput:
1815640708a2SPavel Emelyanov 	mmput(mm);
1816640708a2SPavel Emelyanov out:
1817640708a2SPavel Emelyanov 	return rc;
1818640708a2SPavel Emelyanov }
1819640708a2SPavel Emelyanov 
1820640708a2SPavel Emelyanov struct map_files_info {
18217b540d06SAl Viro 	fmode_t		mode;
1822640708a2SPavel Emelyanov 	unsigned long	len;
1823640708a2SPavel Emelyanov 	unsigned char	name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
1824640708a2SPavel Emelyanov };
1825640708a2SPavel Emelyanov 
1826c52a47acSAl Viro static int
1827640708a2SPavel Emelyanov proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
1828640708a2SPavel Emelyanov 			   struct task_struct *task, const void *ptr)
1829640708a2SPavel Emelyanov {
18307b540d06SAl Viro 	fmode_t mode = (fmode_t)(unsigned long)ptr;
1831640708a2SPavel Emelyanov 	struct proc_inode *ei;
1832640708a2SPavel Emelyanov 	struct inode *inode;
1833640708a2SPavel Emelyanov 
1834640708a2SPavel Emelyanov 	inode = proc_pid_make_inode(dir->i_sb, task);
1835640708a2SPavel Emelyanov 	if (!inode)
1836c52a47acSAl Viro 		return -ENOENT;
1837640708a2SPavel Emelyanov 
1838640708a2SPavel Emelyanov 	ei = PROC_I(inode);
1839640708a2SPavel Emelyanov 	ei->op.proc_get_link = proc_map_files_get_link;
1840640708a2SPavel Emelyanov 
1841640708a2SPavel Emelyanov 	inode->i_op = &proc_pid_link_inode_operations;
1842640708a2SPavel Emelyanov 	inode->i_size = 64;
1843640708a2SPavel Emelyanov 	inode->i_mode = S_IFLNK;
1844640708a2SPavel Emelyanov 
18457b540d06SAl Viro 	if (mode & FMODE_READ)
1846640708a2SPavel Emelyanov 		inode->i_mode |= S_IRUSR;
18477b540d06SAl Viro 	if (mode & FMODE_WRITE)
1848640708a2SPavel Emelyanov 		inode->i_mode |= S_IWUSR;
1849640708a2SPavel Emelyanov 
1850640708a2SPavel Emelyanov 	d_set_d_op(dentry, &tid_map_files_dentry_operations);
1851640708a2SPavel Emelyanov 	d_add(dentry, inode);
1852640708a2SPavel Emelyanov 
1853c52a47acSAl Viro 	return 0;
1854640708a2SPavel Emelyanov }
1855640708a2SPavel Emelyanov 
1856640708a2SPavel Emelyanov static struct dentry *proc_map_files_lookup(struct inode *dir,
185700cd8dd3SAl Viro 		struct dentry *dentry, unsigned int flags)
1858640708a2SPavel Emelyanov {
1859640708a2SPavel Emelyanov 	unsigned long vm_start, vm_end;
1860640708a2SPavel Emelyanov 	struct vm_area_struct *vma;
1861640708a2SPavel Emelyanov 	struct task_struct *task;
1862c52a47acSAl Viro 	int result;
1863640708a2SPavel Emelyanov 	struct mm_struct *mm;
1864640708a2SPavel Emelyanov 
1865c52a47acSAl Viro 	result = -EPERM;
1866640708a2SPavel Emelyanov 	if (!capable(CAP_SYS_ADMIN))
1867640708a2SPavel Emelyanov 		goto out;
1868640708a2SPavel Emelyanov 
1869c52a47acSAl Viro 	result = -ENOENT;
1870640708a2SPavel Emelyanov 	task = get_proc_task(dir);
1871640708a2SPavel Emelyanov 	if (!task)
1872640708a2SPavel Emelyanov 		goto out;
1873640708a2SPavel Emelyanov 
1874c52a47acSAl Viro 	result = -EACCES;
1875eb94cd96SCyrill Gorcunov 	if (!ptrace_may_access(task, PTRACE_MODE_READ))
1876640708a2SPavel Emelyanov 		goto out_put_task;
1877640708a2SPavel Emelyanov 
1878c52a47acSAl Viro 	result = -ENOENT;
1879640708a2SPavel Emelyanov 	if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
1880eb94cd96SCyrill Gorcunov 		goto out_put_task;
1881640708a2SPavel Emelyanov 
1882640708a2SPavel Emelyanov 	mm = get_task_mm(task);
1883640708a2SPavel Emelyanov 	if (!mm)
1884eb94cd96SCyrill Gorcunov 		goto out_put_task;
1885640708a2SPavel Emelyanov 
1886640708a2SPavel Emelyanov 	down_read(&mm->mmap_sem);
1887640708a2SPavel Emelyanov 	vma = find_exact_vma(mm, vm_start, vm_end);
1888640708a2SPavel Emelyanov 	if (!vma)
1889640708a2SPavel Emelyanov 		goto out_no_vma;
1890640708a2SPavel Emelyanov 
189105f56484SStanislav Kinsbursky 	if (vma->vm_file)
18927b540d06SAl Viro 		result = proc_map_files_instantiate(dir, dentry, task,
18937b540d06SAl Viro 				(void *)(unsigned long)vma->vm_file->f_mode);
1894640708a2SPavel Emelyanov 
1895640708a2SPavel Emelyanov out_no_vma:
1896640708a2SPavel Emelyanov 	up_read(&mm->mmap_sem);
1897640708a2SPavel Emelyanov 	mmput(mm);
1898640708a2SPavel Emelyanov out_put_task:
1899640708a2SPavel Emelyanov 	put_task_struct(task);
1900640708a2SPavel Emelyanov out:
1901c52a47acSAl Viro 	return ERR_PTR(result);
1902640708a2SPavel Emelyanov }
1903640708a2SPavel Emelyanov 
1904640708a2SPavel Emelyanov static const struct inode_operations proc_map_files_inode_operations = {
1905640708a2SPavel Emelyanov 	.lookup		= proc_map_files_lookup,
1906640708a2SPavel Emelyanov 	.permission	= proc_fd_permission,
1907640708a2SPavel Emelyanov 	.setattr	= proc_setattr,
1908640708a2SPavel Emelyanov };
1909640708a2SPavel Emelyanov 
1910640708a2SPavel Emelyanov static int
1911f0c3b509SAl Viro proc_map_files_readdir(struct file *file, struct dir_context *ctx)
1912640708a2SPavel Emelyanov {
1913640708a2SPavel Emelyanov 	struct vm_area_struct *vma;
1914640708a2SPavel Emelyanov 	struct task_struct *task;
1915640708a2SPavel Emelyanov 	struct mm_struct *mm;
1916f0c3b509SAl Viro 	unsigned long nr_files, pos, i;
1917f0c3b509SAl Viro 	struct flex_array *fa = NULL;
1918f0c3b509SAl Viro 	struct map_files_info info;
1919f0c3b509SAl Viro 	struct map_files_info *p;
1920640708a2SPavel Emelyanov 	int ret;
1921640708a2SPavel Emelyanov 
192241735818SZhao Hongjiang 	ret = -EPERM;
1923640708a2SPavel Emelyanov 	if (!capable(CAP_SYS_ADMIN))
1924640708a2SPavel Emelyanov 		goto out;
1925640708a2SPavel Emelyanov 
1926640708a2SPavel Emelyanov 	ret = -ENOENT;
1927f0c3b509SAl Viro 	task = get_proc_task(file_inode(file));
1928640708a2SPavel Emelyanov 	if (!task)
1929640708a2SPavel Emelyanov 		goto out;
1930640708a2SPavel Emelyanov 
1931640708a2SPavel Emelyanov 	ret = -EACCES;
1932eb94cd96SCyrill Gorcunov 	if (!ptrace_may_access(task, PTRACE_MODE_READ))
1933640708a2SPavel Emelyanov 		goto out_put_task;
1934640708a2SPavel Emelyanov 
1935640708a2SPavel Emelyanov 	ret = 0;
1936f0c3b509SAl Viro 	if (!dir_emit_dots(file, ctx))
1937eb94cd96SCyrill Gorcunov 		goto out_put_task;
1938640708a2SPavel Emelyanov 
1939640708a2SPavel Emelyanov 	mm = get_task_mm(task);
1940640708a2SPavel Emelyanov 	if (!mm)
1941eb94cd96SCyrill Gorcunov 		goto out_put_task;
1942640708a2SPavel Emelyanov 	down_read(&mm->mmap_sem);
1943640708a2SPavel Emelyanov 
1944640708a2SPavel Emelyanov 	nr_files = 0;
1945640708a2SPavel Emelyanov 
1946640708a2SPavel Emelyanov 	/*
1947640708a2SPavel Emelyanov 	 * We need two passes here:
1948640708a2SPavel Emelyanov 	 *
1949640708a2SPavel Emelyanov 	 *  1) Collect vmas of mapped files with mmap_sem taken
1950640708a2SPavel Emelyanov 	 *  2) Release mmap_sem and instantiate entries
1951640708a2SPavel Emelyanov 	 *
1952640708a2SPavel Emelyanov 	 * otherwise we get lockdep complained, since filldir()
1953640708a2SPavel Emelyanov 	 * routine might require mmap_sem taken in might_fault().
1954640708a2SPavel Emelyanov 	 */
1955640708a2SPavel Emelyanov 
1956640708a2SPavel Emelyanov 	for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
1957f0c3b509SAl Viro 		if (vma->vm_file && ++pos > ctx->pos)
1958640708a2SPavel Emelyanov 			nr_files++;
1959640708a2SPavel Emelyanov 	}
1960640708a2SPavel Emelyanov 
1961640708a2SPavel Emelyanov 	if (nr_files) {
1962640708a2SPavel Emelyanov 		fa = flex_array_alloc(sizeof(info), nr_files,
1963640708a2SPavel Emelyanov 					GFP_KERNEL);
1964640708a2SPavel Emelyanov 		if (!fa || flex_array_prealloc(fa, 0, nr_files,
1965640708a2SPavel Emelyanov 						GFP_KERNEL)) {
1966640708a2SPavel Emelyanov 			ret = -ENOMEM;
1967640708a2SPavel Emelyanov 			if (fa)
1968640708a2SPavel Emelyanov 				flex_array_free(fa);
1969640708a2SPavel Emelyanov 			up_read(&mm->mmap_sem);
1970640708a2SPavel Emelyanov 			mmput(mm);
1971eb94cd96SCyrill Gorcunov 			goto out_put_task;
1972640708a2SPavel Emelyanov 		}
1973640708a2SPavel Emelyanov 		for (i = 0, vma = mm->mmap, pos = 2; vma;
1974640708a2SPavel Emelyanov 				vma = vma->vm_next) {
1975640708a2SPavel Emelyanov 			if (!vma->vm_file)
1976640708a2SPavel Emelyanov 				continue;
1977f0c3b509SAl Viro 			if (++pos <= ctx->pos)
1978640708a2SPavel Emelyanov 				continue;
1979640708a2SPavel Emelyanov 
19807b540d06SAl Viro 			info.mode = vma->vm_file->f_mode;
1981640708a2SPavel Emelyanov 			info.len = snprintf(info.name,
1982640708a2SPavel Emelyanov 					sizeof(info.name), "%lx-%lx",
1983640708a2SPavel Emelyanov 					vma->vm_start, vma->vm_end);
1984640708a2SPavel Emelyanov 			if (flex_array_put(fa, i++, &info, GFP_KERNEL))
1985640708a2SPavel Emelyanov 				BUG();
1986640708a2SPavel Emelyanov 		}
1987640708a2SPavel Emelyanov 	}
1988640708a2SPavel Emelyanov 	up_read(&mm->mmap_sem);
1989640708a2SPavel Emelyanov 
1990640708a2SPavel Emelyanov 	for (i = 0; i < nr_files; i++) {
1991640708a2SPavel Emelyanov 		p = flex_array_get(fa, i);
1992f0c3b509SAl Viro 		if (!proc_fill_cache(file, ctx,
1993640708a2SPavel Emelyanov 				      p->name, p->len,
1994640708a2SPavel Emelyanov 				      proc_map_files_instantiate,
19957b540d06SAl Viro 				      task,
1996f0c3b509SAl Viro 				      (void *)(unsigned long)p->mode))
1997640708a2SPavel Emelyanov 			break;
1998f0c3b509SAl Viro 		ctx->pos++;
1999640708a2SPavel Emelyanov 	}
2000640708a2SPavel Emelyanov 	if (fa)
2001640708a2SPavel Emelyanov 		flex_array_free(fa);
2002640708a2SPavel Emelyanov 	mmput(mm);
2003640708a2SPavel Emelyanov 
2004640708a2SPavel Emelyanov out_put_task:
2005640708a2SPavel Emelyanov 	put_task_struct(task);
2006640708a2SPavel Emelyanov out:
2007640708a2SPavel Emelyanov 	return ret;
2008640708a2SPavel Emelyanov }
2009640708a2SPavel Emelyanov 
2010640708a2SPavel Emelyanov static const struct file_operations proc_map_files_operations = {
2011640708a2SPavel Emelyanov 	.read		= generic_read_dir,
2012f0c3b509SAl Viro 	.iterate	= proc_map_files_readdir,
2013640708a2SPavel Emelyanov 	.llseek		= default_llseek,
2014640708a2SPavel Emelyanov };
2015640708a2SPavel Emelyanov 
201648f6a7a5SPavel Emelyanov struct timers_private {
201748f6a7a5SPavel Emelyanov 	struct pid *pid;
201848f6a7a5SPavel Emelyanov 	struct task_struct *task;
201948f6a7a5SPavel Emelyanov 	struct sighand_struct *sighand;
202057b8015eSPavel Emelyanov 	struct pid_namespace *ns;
202148f6a7a5SPavel Emelyanov 	unsigned long flags;
202248f6a7a5SPavel Emelyanov };
202348f6a7a5SPavel Emelyanov 
202448f6a7a5SPavel Emelyanov static void *timers_start(struct seq_file *m, loff_t *pos)
202548f6a7a5SPavel Emelyanov {
202648f6a7a5SPavel Emelyanov 	struct timers_private *tp = m->private;
202748f6a7a5SPavel Emelyanov 
202848f6a7a5SPavel Emelyanov 	tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
202948f6a7a5SPavel Emelyanov 	if (!tp->task)
203048f6a7a5SPavel Emelyanov 		return ERR_PTR(-ESRCH);
203148f6a7a5SPavel Emelyanov 
203248f6a7a5SPavel Emelyanov 	tp->sighand = lock_task_sighand(tp->task, &tp->flags);
203348f6a7a5SPavel Emelyanov 	if (!tp->sighand)
203448f6a7a5SPavel Emelyanov 		return ERR_PTR(-ESRCH);
203548f6a7a5SPavel Emelyanov 
203648f6a7a5SPavel Emelyanov 	return seq_list_start(&tp->task->signal->posix_timers, *pos);
203748f6a7a5SPavel Emelyanov }
203848f6a7a5SPavel Emelyanov 
203948f6a7a5SPavel Emelyanov static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
204048f6a7a5SPavel Emelyanov {
204148f6a7a5SPavel Emelyanov 	struct timers_private *tp = m->private;
204248f6a7a5SPavel Emelyanov 	return seq_list_next(v, &tp->task->signal->posix_timers, pos);
204348f6a7a5SPavel Emelyanov }
204448f6a7a5SPavel Emelyanov 
204548f6a7a5SPavel Emelyanov static void timers_stop(struct seq_file *m, void *v)
204648f6a7a5SPavel Emelyanov {
204748f6a7a5SPavel Emelyanov 	struct timers_private *tp = m->private;
204848f6a7a5SPavel Emelyanov 
204948f6a7a5SPavel Emelyanov 	if (tp->sighand) {
205048f6a7a5SPavel Emelyanov 		unlock_task_sighand(tp->task, &tp->flags);
205148f6a7a5SPavel Emelyanov 		tp->sighand = NULL;
205248f6a7a5SPavel Emelyanov 	}
205348f6a7a5SPavel Emelyanov 
205448f6a7a5SPavel Emelyanov 	if (tp->task) {
205548f6a7a5SPavel Emelyanov 		put_task_struct(tp->task);
205648f6a7a5SPavel Emelyanov 		tp->task = NULL;
205748f6a7a5SPavel Emelyanov 	}
205848f6a7a5SPavel Emelyanov }
205948f6a7a5SPavel Emelyanov 
206048f6a7a5SPavel Emelyanov static int show_timer(struct seq_file *m, void *v)
206148f6a7a5SPavel Emelyanov {
206248f6a7a5SPavel Emelyanov 	struct k_itimer *timer;
206357b8015eSPavel Emelyanov 	struct timers_private *tp = m->private;
206457b8015eSPavel Emelyanov 	int notify;
2065cedbccabSAlexey Dobriyan 	static const char * const nstr[] = {
206657b8015eSPavel Emelyanov 		[SIGEV_SIGNAL] = "signal",
206757b8015eSPavel Emelyanov 		[SIGEV_NONE] = "none",
206857b8015eSPavel Emelyanov 		[SIGEV_THREAD] = "thread",
206957b8015eSPavel Emelyanov 	};
207048f6a7a5SPavel Emelyanov 
207148f6a7a5SPavel Emelyanov 	timer = list_entry((struct list_head *)v, struct k_itimer, list);
207257b8015eSPavel Emelyanov 	notify = timer->it_sigev_notify;
207357b8015eSPavel Emelyanov 
207448f6a7a5SPavel Emelyanov 	seq_printf(m, "ID: %d\n", timer->it_id);
207557b8015eSPavel Emelyanov 	seq_printf(m, "signal: %d/%p\n", timer->sigq->info.si_signo,
207657b8015eSPavel Emelyanov 			timer->sigq->info.si_value.sival_ptr);
207757b8015eSPavel Emelyanov 	seq_printf(m, "notify: %s/%s.%d\n",
207857b8015eSPavel Emelyanov 		nstr[notify & ~SIGEV_THREAD_ID],
207957b8015eSPavel Emelyanov 		(notify & SIGEV_THREAD_ID) ? "tid" : "pid",
208057b8015eSPavel Emelyanov 		pid_nr_ns(timer->it_pid, tp->ns));
208115ef0298SPavel Tikhomirov 	seq_printf(m, "ClockID: %d\n", timer->it_clock);
208248f6a7a5SPavel Emelyanov 
208348f6a7a5SPavel Emelyanov 	return 0;
208448f6a7a5SPavel Emelyanov }
208548f6a7a5SPavel Emelyanov 
208648f6a7a5SPavel Emelyanov static const struct seq_operations proc_timers_seq_ops = {
208748f6a7a5SPavel Emelyanov 	.start	= timers_start,
208848f6a7a5SPavel Emelyanov 	.next	= timers_next,
208948f6a7a5SPavel Emelyanov 	.stop	= timers_stop,
209048f6a7a5SPavel Emelyanov 	.show	= show_timer,
209148f6a7a5SPavel Emelyanov };
209248f6a7a5SPavel Emelyanov 
209348f6a7a5SPavel Emelyanov static int proc_timers_open(struct inode *inode, struct file *file)
209448f6a7a5SPavel Emelyanov {
209548f6a7a5SPavel Emelyanov 	struct timers_private *tp;
209648f6a7a5SPavel Emelyanov 
209748f6a7a5SPavel Emelyanov 	tp = __seq_open_private(file, &proc_timers_seq_ops,
209848f6a7a5SPavel Emelyanov 			sizeof(struct timers_private));
209948f6a7a5SPavel Emelyanov 	if (!tp)
210048f6a7a5SPavel Emelyanov 		return -ENOMEM;
210148f6a7a5SPavel Emelyanov 
210248f6a7a5SPavel Emelyanov 	tp->pid = proc_pid(inode);
210357b8015eSPavel Emelyanov 	tp->ns = inode->i_sb->s_fs_info;
210448f6a7a5SPavel Emelyanov 	return 0;
210548f6a7a5SPavel Emelyanov }
210648f6a7a5SPavel Emelyanov 
210748f6a7a5SPavel Emelyanov static const struct file_operations proc_timers_operations = {
210848f6a7a5SPavel Emelyanov 	.open		= proc_timers_open,
210948f6a7a5SPavel Emelyanov 	.read		= seq_read,
211048f6a7a5SPavel Emelyanov 	.llseek		= seq_lseek,
211148f6a7a5SPavel Emelyanov 	.release	= seq_release_private,
211248f6a7a5SPavel Emelyanov };
2113640708a2SPavel Emelyanov #endif /* CONFIG_CHECKPOINT_RESTORE */
2114640708a2SPavel Emelyanov 
2115c52a47acSAl Viro static int proc_pident_instantiate(struct inode *dir,
2116c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
2117444ceed8SEric W. Biederman {
2118c5141e6dSEric Dumazet 	const struct pid_entry *p = ptr;
2119444ceed8SEric W. Biederman 	struct inode *inode;
2120444ceed8SEric W. Biederman 	struct proc_inode *ei;
2121444ceed8SEric W. Biederman 
212261a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
2123444ceed8SEric W. Biederman 	if (!inode)
2124444ceed8SEric W. Biederman 		goto out;
2125444ceed8SEric W. Biederman 
2126444ceed8SEric W. Biederman 	ei = PROC_I(inode);
2127444ceed8SEric W. Biederman 	inode->i_mode = p->mode;
2128444ceed8SEric W. Biederman 	if (S_ISDIR(inode->i_mode))
2129bfe86848SMiklos Szeredi 		set_nlink(inode, 2);	/* Use getattr to fix if necessary */
2130444ceed8SEric W. Biederman 	if (p->iop)
2131444ceed8SEric W. Biederman 		inode->i_op = p->iop;
2132444ceed8SEric W. Biederman 	if (p->fop)
2133444ceed8SEric W. Biederman 		inode->i_fop = p->fop;
2134444ceed8SEric W. Biederman 	ei->op = p->op;
2135fb045adbSNick Piggin 	d_set_d_op(dentry, &pid_dentry_operations);
2136444ceed8SEric W. Biederman 	d_add(dentry, inode);
2137444ceed8SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
21380b728e19SAl Viro 	if (pid_revalidate(dentry, 0))
2139c52a47acSAl Viro 		return 0;
2140444ceed8SEric W. Biederman out:
2141c52a47acSAl Viro 	return -ENOENT;
2142444ceed8SEric W. Biederman }
2143444ceed8SEric W. Biederman 
21441da177e4SLinus Torvalds static struct dentry *proc_pident_lookup(struct inode *dir,
21451da177e4SLinus Torvalds 					 struct dentry *dentry,
2146c5141e6dSEric Dumazet 					 const struct pid_entry *ents,
21477bcd6b0eSEric W. Biederman 					 unsigned int nents)
21481da177e4SLinus Torvalds {
2149c52a47acSAl Viro 	int error;
215099f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(dir);
2151c5141e6dSEric Dumazet 	const struct pid_entry *p, *last;
21521da177e4SLinus Torvalds 
2153c52a47acSAl Viro 	error = -ENOENT;
21541da177e4SLinus Torvalds 
215599f89551SEric W. Biederman 	if (!task)
215699f89551SEric W. Biederman 		goto out_no_task;
21571da177e4SLinus Torvalds 
215820cdc894SEric W. Biederman 	/*
215920cdc894SEric W. Biederman 	 * Yes, it does not scale. And it should not. Don't add
216020cdc894SEric W. Biederman 	 * new entries into /proc/<tgid>/ without very good reasons.
216120cdc894SEric W. Biederman 	 */
21627bcd6b0eSEric W. Biederman 	last = &ents[nents - 1];
21637bcd6b0eSEric W. Biederman 	for (p = ents; p <= last; p++) {
21641da177e4SLinus Torvalds 		if (p->len != dentry->d_name.len)
21651da177e4SLinus Torvalds 			continue;
21661da177e4SLinus Torvalds 		if (!memcmp(dentry->d_name.name, p->name, p->len))
21671da177e4SLinus Torvalds 			break;
21681da177e4SLinus Torvalds 	}
21697bcd6b0eSEric W. Biederman 	if (p > last)
21701da177e4SLinus Torvalds 		goto out;
21711da177e4SLinus Torvalds 
2172444ceed8SEric W. Biederman 	error = proc_pident_instantiate(dir, dentry, task, p);
21731da177e4SLinus Torvalds out:
217499f89551SEric W. Biederman 	put_task_struct(task);
217599f89551SEric W. Biederman out_no_task:
2176c52a47acSAl Viro 	return ERR_PTR(error);
21771da177e4SLinus Torvalds }
21781da177e4SLinus Torvalds 
2179f0c3b509SAl Viro static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
2180c5141e6dSEric Dumazet 		const struct pid_entry *ents, unsigned int nents)
218128a6d671SEric W. Biederman {
2182f0c3b509SAl Viro 	struct task_struct *task = get_proc_task(file_inode(file));
2183f0c3b509SAl Viro 	const struct pid_entry *p;
218428a6d671SEric W. Biederman 
218528a6d671SEric W. Biederman 	if (!task)
2186f0c3b509SAl Viro 		return -ENOENT;
218728a6d671SEric W. Biederman 
2188f0c3b509SAl Viro 	if (!dir_emit_dots(file, ctx))
218928a6d671SEric W. Biederman 		goto out;
21901da177e4SLinus Torvalds 
2191f0c3b509SAl Viro 	if (ctx->pos >= nents + 2)
2192f0c3b509SAl Viro 		goto out;
2193f0c3b509SAl Viro 
2194f0c3b509SAl Viro 	for (p = ents + (ctx->pos - 2); p <= ents + nents - 1; p++) {
2195f0c3b509SAl Viro 		if (!proc_fill_cache(file, ctx, p->name, p->len,
2196f0c3b509SAl Viro 				proc_pident_instantiate, task, p))
2197f0c3b509SAl Viro 			break;
2198f0c3b509SAl Viro 		ctx->pos++;
2199f0c3b509SAl Viro 	}
220028a6d671SEric W. Biederman out:
220161a28784SEric W. Biederman 	put_task_struct(task);
2202f0c3b509SAl Viro 	return 0;
22031da177e4SLinus Torvalds }
22041da177e4SLinus Torvalds 
22051da177e4SLinus Torvalds #ifdef CONFIG_SECURITY
220628a6d671SEric W. Biederman static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
220728a6d671SEric W. Biederman 				  size_t count, loff_t *ppos)
220828a6d671SEric W. Biederman {
2209496ad9aaSAl Viro 	struct inode * inode = file_inode(file);
221004ff9708SAl Viro 	char *p = NULL;
221128a6d671SEric W. Biederman 	ssize_t length;
221228a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
221328a6d671SEric W. Biederman 
221428a6d671SEric W. Biederman 	if (!task)
221504ff9708SAl Viro 		return -ESRCH;
221628a6d671SEric W. Biederman 
221728a6d671SEric W. Biederman 	length = security_getprocattr(task,
22182fddfeefSJosef "Jeff" Sipek 				      (char*)file->f_path.dentry->d_name.name,
221904ff9708SAl Viro 				      &p);
222028a6d671SEric W. Biederman 	put_task_struct(task);
222104ff9708SAl Viro 	if (length > 0)
222204ff9708SAl Viro 		length = simple_read_from_buffer(buf, count, ppos, p, length);
222304ff9708SAl Viro 	kfree(p);
222428a6d671SEric W. Biederman 	return length;
222528a6d671SEric W. Biederman }
222628a6d671SEric W. Biederman 
222728a6d671SEric W. Biederman static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
222828a6d671SEric W. Biederman 				   size_t count, loff_t *ppos)
222928a6d671SEric W. Biederman {
2230496ad9aaSAl Viro 	struct inode * inode = file_inode(file);
223128a6d671SEric W. Biederman 	char *page;
223228a6d671SEric W. Biederman 	ssize_t length;
223328a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
223428a6d671SEric W. Biederman 
223528a6d671SEric W. Biederman 	length = -ESRCH;
223628a6d671SEric W. Biederman 	if (!task)
223728a6d671SEric W. Biederman 		goto out_no_task;
223828a6d671SEric W. Biederman 	if (count > PAGE_SIZE)
223928a6d671SEric W. Biederman 		count = PAGE_SIZE;
224028a6d671SEric W. Biederman 
224128a6d671SEric W. Biederman 	/* No partial writes. */
224228a6d671SEric W. Biederman 	length = -EINVAL;
224328a6d671SEric W. Biederman 	if (*ppos != 0)
224428a6d671SEric W. Biederman 		goto out;
224528a6d671SEric W. Biederman 
224628a6d671SEric W. Biederman 	length = -ENOMEM;
2247e12ba74dSMel Gorman 	page = (char*)__get_free_page(GFP_TEMPORARY);
224828a6d671SEric W. Biederman 	if (!page)
224928a6d671SEric W. Biederman 		goto out;
225028a6d671SEric W. Biederman 
225128a6d671SEric W. Biederman 	length = -EFAULT;
225228a6d671SEric W. Biederman 	if (copy_from_user(page, buf, count))
225328a6d671SEric W. Biederman 		goto out_free;
225428a6d671SEric W. Biederman 
2255107db7c7SDavid Howells 	/* Guard against adverse ptrace interaction */
22569b1bf12dSKOSAKI Motohiro 	length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
2257107db7c7SDavid Howells 	if (length < 0)
2258107db7c7SDavid Howells 		goto out_free;
2259107db7c7SDavid Howells 
226028a6d671SEric W. Biederman 	length = security_setprocattr(task,
22612fddfeefSJosef "Jeff" Sipek 				      (char*)file->f_path.dentry->d_name.name,
226228a6d671SEric W. Biederman 				      (void*)page, count);
22639b1bf12dSKOSAKI Motohiro 	mutex_unlock(&task->signal->cred_guard_mutex);
226428a6d671SEric W. Biederman out_free:
226528a6d671SEric W. Biederman 	free_page((unsigned long) page);
226628a6d671SEric W. Biederman out:
226728a6d671SEric W. Biederman 	put_task_struct(task);
226828a6d671SEric W. Biederman out_no_task:
226928a6d671SEric W. Biederman 	return length;
227028a6d671SEric W. Biederman }
227128a6d671SEric W. Biederman 
227200977a59SArjan van de Ven static const struct file_operations proc_pid_attr_operations = {
227328a6d671SEric W. Biederman 	.read		= proc_pid_attr_read,
227428a6d671SEric W. Biederman 	.write		= proc_pid_attr_write,
227587df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
227628a6d671SEric W. Biederman };
227728a6d671SEric W. Biederman 
2278c5141e6dSEric Dumazet static const struct pid_entry attr_dir_stuff[] = {
2279631f9c18SAlexey Dobriyan 	REG("current",    S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2280631f9c18SAlexey Dobriyan 	REG("prev",       S_IRUGO,	   proc_pid_attr_operations),
2281631f9c18SAlexey Dobriyan 	REG("exec",       S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2282631f9c18SAlexey Dobriyan 	REG("fscreate",   S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2283631f9c18SAlexey Dobriyan 	REG("keycreate",  S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2284631f9c18SAlexey Dobriyan 	REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
228528a6d671SEric W. Biederman };
228628a6d671SEric W. Biederman 
2287f0c3b509SAl Viro static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
22881da177e4SLinus Torvalds {
2289f0c3b509SAl Viro 	return proc_pident_readdir(file, ctx,
229072d9dcfcSEric W. Biederman 				   attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
22911da177e4SLinus Torvalds }
22921da177e4SLinus Torvalds 
229300977a59SArjan van de Ven static const struct file_operations proc_attr_dir_operations = {
22941da177e4SLinus Torvalds 	.read		= generic_read_dir,
2295f0c3b509SAl Viro 	.iterate	= proc_attr_dir_readdir,
22966038f373SArnd Bergmann 	.llseek		= default_llseek,
22971da177e4SLinus Torvalds };
22981da177e4SLinus Torvalds 
229972d9dcfcSEric W. Biederman static struct dentry *proc_attr_dir_lookup(struct inode *dir,
230000cd8dd3SAl Viro 				struct dentry *dentry, unsigned int flags)
23011da177e4SLinus Torvalds {
23027bcd6b0eSEric W. Biederman 	return proc_pident_lookup(dir, dentry,
23037bcd6b0eSEric W. Biederman 				  attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
23041da177e4SLinus Torvalds }
23051da177e4SLinus Torvalds 
2306c5ef1c42SArjan van de Ven static const struct inode_operations proc_attr_dir_inode_operations = {
230772d9dcfcSEric W. Biederman 	.lookup		= proc_attr_dir_lookup,
230899f89551SEric W. Biederman 	.getattr	= pid_getattr,
23096d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
23101da177e4SLinus Torvalds };
23111da177e4SLinus Torvalds 
23121da177e4SLinus Torvalds #endif
23131da177e4SLinus Torvalds 
2314698ba7b5SChristoph Hellwig #ifdef CONFIG_ELF_CORE
23153cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
23163cb4a0bbSKawai, Hidehiro 					 size_t count, loff_t *ppos)
23173cb4a0bbSKawai, Hidehiro {
2318496ad9aaSAl Viro 	struct task_struct *task = get_proc_task(file_inode(file));
23193cb4a0bbSKawai, Hidehiro 	struct mm_struct *mm;
23203cb4a0bbSKawai, Hidehiro 	char buffer[PROC_NUMBUF];
23213cb4a0bbSKawai, Hidehiro 	size_t len;
23223cb4a0bbSKawai, Hidehiro 	int ret;
23233cb4a0bbSKawai, Hidehiro 
23243cb4a0bbSKawai, Hidehiro 	if (!task)
23253cb4a0bbSKawai, Hidehiro 		return -ESRCH;
23263cb4a0bbSKawai, Hidehiro 
23273cb4a0bbSKawai, Hidehiro 	ret = 0;
23283cb4a0bbSKawai, Hidehiro 	mm = get_task_mm(task);
23293cb4a0bbSKawai, Hidehiro 	if (mm) {
23303cb4a0bbSKawai, Hidehiro 		len = snprintf(buffer, sizeof(buffer), "%08lx\n",
23313cb4a0bbSKawai, Hidehiro 			       ((mm->flags & MMF_DUMP_FILTER_MASK) >>
23323cb4a0bbSKawai, Hidehiro 				MMF_DUMP_FILTER_SHIFT));
23333cb4a0bbSKawai, Hidehiro 		mmput(mm);
23343cb4a0bbSKawai, Hidehiro 		ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
23353cb4a0bbSKawai, Hidehiro 	}
23363cb4a0bbSKawai, Hidehiro 
23373cb4a0bbSKawai, Hidehiro 	put_task_struct(task);
23383cb4a0bbSKawai, Hidehiro 
23393cb4a0bbSKawai, Hidehiro 	return ret;
23403cb4a0bbSKawai, Hidehiro }
23413cb4a0bbSKawai, Hidehiro 
23423cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_write(struct file *file,
23433cb4a0bbSKawai, Hidehiro 					  const char __user *buf,
23443cb4a0bbSKawai, Hidehiro 					  size_t count,
23453cb4a0bbSKawai, Hidehiro 					  loff_t *ppos)
23463cb4a0bbSKawai, Hidehiro {
23473cb4a0bbSKawai, Hidehiro 	struct task_struct *task;
23483cb4a0bbSKawai, Hidehiro 	struct mm_struct *mm;
23493cb4a0bbSKawai, Hidehiro 	char buffer[PROC_NUMBUF], *end;
23503cb4a0bbSKawai, Hidehiro 	unsigned int val;
23513cb4a0bbSKawai, Hidehiro 	int ret;
23523cb4a0bbSKawai, Hidehiro 	int i;
23533cb4a0bbSKawai, Hidehiro 	unsigned long mask;
23543cb4a0bbSKawai, Hidehiro 
23553cb4a0bbSKawai, Hidehiro 	ret = -EFAULT;
23563cb4a0bbSKawai, Hidehiro 	memset(buffer, 0, sizeof(buffer));
23573cb4a0bbSKawai, Hidehiro 	if (count > sizeof(buffer) - 1)
23583cb4a0bbSKawai, Hidehiro 		count = sizeof(buffer) - 1;
23593cb4a0bbSKawai, Hidehiro 	if (copy_from_user(buffer, buf, count))
23603cb4a0bbSKawai, Hidehiro 		goto out_no_task;
23613cb4a0bbSKawai, Hidehiro 
23623cb4a0bbSKawai, Hidehiro 	ret = -EINVAL;
23633cb4a0bbSKawai, Hidehiro 	val = (unsigned int)simple_strtoul(buffer, &end, 0);
23643cb4a0bbSKawai, Hidehiro 	if (*end == '\n')
23653cb4a0bbSKawai, Hidehiro 		end++;
23663cb4a0bbSKawai, Hidehiro 	if (end - buffer == 0)
23673cb4a0bbSKawai, Hidehiro 		goto out_no_task;
23683cb4a0bbSKawai, Hidehiro 
23693cb4a0bbSKawai, Hidehiro 	ret = -ESRCH;
2370496ad9aaSAl Viro 	task = get_proc_task(file_inode(file));
23713cb4a0bbSKawai, Hidehiro 	if (!task)
23723cb4a0bbSKawai, Hidehiro 		goto out_no_task;
23733cb4a0bbSKawai, Hidehiro 
23743cb4a0bbSKawai, Hidehiro 	ret = end - buffer;
23753cb4a0bbSKawai, Hidehiro 	mm = get_task_mm(task);
23763cb4a0bbSKawai, Hidehiro 	if (!mm)
23773cb4a0bbSKawai, Hidehiro 		goto out_no_mm;
23783cb4a0bbSKawai, Hidehiro 
23793cb4a0bbSKawai, Hidehiro 	for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
23803cb4a0bbSKawai, Hidehiro 		if (val & mask)
23813cb4a0bbSKawai, Hidehiro 			set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
23823cb4a0bbSKawai, Hidehiro 		else
23833cb4a0bbSKawai, Hidehiro 			clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
23843cb4a0bbSKawai, Hidehiro 	}
23853cb4a0bbSKawai, Hidehiro 
23863cb4a0bbSKawai, Hidehiro 	mmput(mm);
23873cb4a0bbSKawai, Hidehiro  out_no_mm:
23883cb4a0bbSKawai, Hidehiro 	put_task_struct(task);
23893cb4a0bbSKawai, Hidehiro  out_no_task:
23903cb4a0bbSKawai, Hidehiro 	return ret;
23913cb4a0bbSKawai, Hidehiro }
23923cb4a0bbSKawai, Hidehiro 
23933cb4a0bbSKawai, Hidehiro static const struct file_operations proc_coredump_filter_operations = {
23943cb4a0bbSKawai, Hidehiro 	.read		= proc_coredump_filter_read,
23953cb4a0bbSKawai, Hidehiro 	.write		= proc_coredump_filter_write,
239687df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
23973cb4a0bbSKawai, Hidehiro };
23983cb4a0bbSKawai, Hidehiro #endif
23993cb4a0bbSKawai, Hidehiro 
2400aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING
2401297c5d92SAndrea Righi static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
2402aba76fdbSAndrew Morton {
2403940389b8SAndrea Righi 	struct task_io_accounting acct = task->ioac;
2404297c5d92SAndrea Righi 	unsigned long flags;
2405293eb1e7SVasiliy Kulikov 	int result;
2406297c5d92SAndrea Righi 
2407293eb1e7SVasiliy Kulikov 	result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2408293eb1e7SVasiliy Kulikov 	if (result)
2409293eb1e7SVasiliy Kulikov 		return result;
2410293eb1e7SVasiliy Kulikov 
2411293eb1e7SVasiliy Kulikov 	if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
2412293eb1e7SVasiliy Kulikov 		result = -EACCES;
2413293eb1e7SVasiliy Kulikov 		goto out_unlock;
2414293eb1e7SVasiliy Kulikov 	}
24151d1221f3SVasiliy Kulikov 
24165995477aSAndrea Righi 	if (whole && lock_task_sighand(task, &flags)) {
2417b2d002dbSAndrea Righi 		struct task_struct *t = task;
2418297c5d92SAndrea Righi 
24195995477aSAndrea Righi 		task_io_accounting_add(&acct, &task->signal->ioac);
24205995477aSAndrea Righi 		while_each_thread(task, t)
24215995477aSAndrea Righi 			task_io_accounting_add(&acct, &t->ioac);
2422297c5d92SAndrea Righi 
2423297c5d92SAndrea Righi 		unlock_task_sighand(task, &flags);
2424297c5d92SAndrea Righi 	}
2425293eb1e7SVasiliy Kulikov 	result = sprintf(buffer,
2426aba76fdbSAndrew Morton 			"rchar: %llu\n"
2427aba76fdbSAndrew Morton 			"wchar: %llu\n"
2428aba76fdbSAndrew Morton 			"syscr: %llu\n"
2429aba76fdbSAndrew Morton 			"syscw: %llu\n"
2430aba76fdbSAndrew Morton 			"read_bytes: %llu\n"
2431aba76fdbSAndrew Morton 			"write_bytes: %llu\n"
2432aba76fdbSAndrew Morton 			"cancelled_write_bytes: %llu\n",
24337c44319dSAlexander Beregalov 			(unsigned long long)acct.rchar,
24347c44319dSAlexander Beregalov 			(unsigned long long)acct.wchar,
24357c44319dSAlexander Beregalov 			(unsigned long long)acct.syscr,
24367c44319dSAlexander Beregalov 			(unsigned long long)acct.syscw,
24377c44319dSAlexander Beregalov 			(unsigned long long)acct.read_bytes,
24387c44319dSAlexander Beregalov 			(unsigned long long)acct.write_bytes,
24397c44319dSAlexander Beregalov 			(unsigned long long)acct.cancelled_write_bytes);
2440293eb1e7SVasiliy Kulikov out_unlock:
2441293eb1e7SVasiliy Kulikov 	mutex_unlock(&task->signal->cred_guard_mutex);
2442293eb1e7SVasiliy Kulikov 	return result;
2443aba76fdbSAndrew Morton }
2444297c5d92SAndrea Righi 
2445297c5d92SAndrea Righi static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2446297c5d92SAndrea Righi {
2447297c5d92SAndrea Righi 	return do_io_accounting(task, buffer, 0);
2448297c5d92SAndrea Righi }
2449297c5d92SAndrea Righi 
2450297c5d92SAndrea Righi static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2451297c5d92SAndrea Righi {
2452297c5d92SAndrea Righi 	return do_io_accounting(task, buffer, 1);
2453297c5d92SAndrea Righi }
2454297c5d92SAndrea Righi #endif /* CONFIG_TASK_IO_ACCOUNTING */
2455aba76fdbSAndrew Morton 
245622d917d8SEric W. Biederman #ifdef CONFIG_USER_NS
245722d917d8SEric W. Biederman static int proc_id_map_open(struct inode *inode, struct file *file,
2458ccf94f1bSFabian Frederick 	const struct seq_operations *seq_ops)
245922d917d8SEric W. Biederman {
246022d917d8SEric W. Biederman 	struct user_namespace *ns = NULL;
246122d917d8SEric W. Biederman 	struct task_struct *task;
246222d917d8SEric W. Biederman 	struct seq_file *seq;
246322d917d8SEric W. Biederman 	int ret = -EINVAL;
246422d917d8SEric W. Biederman 
246522d917d8SEric W. Biederman 	task = get_proc_task(inode);
246622d917d8SEric W. Biederman 	if (task) {
246722d917d8SEric W. Biederman 		rcu_read_lock();
246822d917d8SEric W. Biederman 		ns = get_user_ns(task_cred_xxx(task, user_ns));
246922d917d8SEric W. Biederman 		rcu_read_unlock();
247022d917d8SEric W. Biederman 		put_task_struct(task);
247122d917d8SEric W. Biederman 	}
247222d917d8SEric W. Biederman 	if (!ns)
247322d917d8SEric W. Biederman 		goto err;
247422d917d8SEric W. Biederman 
247522d917d8SEric W. Biederman 	ret = seq_open(file, seq_ops);
247622d917d8SEric W. Biederman 	if (ret)
247722d917d8SEric W. Biederman 		goto err_put_ns;
247822d917d8SEric W. Biederman 
247922d917d8SEric W. Biederman 	seq = file->private_data;
248022d917d8SEric W. Biederman 	seq->private = ns;
248122d917d8SEric W. Biederman 
248222d917d8SEric W. Biederman 	return 0;
248322d917d8SEric W. Biederman err_put_ns:
248422d917d8SEric W. Biederman 	put_user_ns(ns);
248522d917d8SEric W. Biederman err:
248622d917d8SEric W. Biederman 	return ret;
248722d917d8SEric W. Biederman }
248822d917d8SEric W. Biederman 
248922d917d8SEric W. Biederman static int proc_id_map_release(struct inode *inode, struct file *file)
249022d917d8SEric W. Biederman {
249122d917d8SEric W. Biederman 	struct seq_file *seq = file->private_data;
249222d917d8SEric W. Biederman 	struct user_namespace *ns = seq->private;
249322d917d8SEric W. Biederman 	put_user_ns(ns);
249422d917d8SEric W. Biederman 	return seq_release(inode, file);
249522d917d8SEric W. Biederman }
249622d917d8SEric W. Biederman 
249722d917d8SEric W. Biederman static int proc_uid_map_open(struct inode *inode, struct file *file)
249822d917d8SEric W. Biederman {
249922d917d8SEric W. Biederman 	return proc_id_map_open(inode, file, &proc_uid_seq_operations);
250022d917d8SEric W. Biederman }
250122d917d8SEric W. Biederman 
250222d917d8SEric W. Biederman static int proc_gid_map_open(struct inode *inode, struct file *file)
250322d917d8SEric W. Biederman {
250422d917d8SEric W. Biederman 	return proc_id_map_open(inode, file, &proc_gid_seq_operations);
250522d917d8SEric W. Biederman }
250622d917d8SEric W. Biederman 
2507f76d207aSEric W. Biederman static int proc_projid_map_open(struct inode *inode, struct file *file)
2508f76d207aSEric W. Biederman {
2509f76d207aSEric W. Biederman 	return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2510f76d207aSEric W. Biederman }
2511f76d207aSEric W. Biederman 
251222d917d8SEric W. Biederman static const struct file_operations proc_uid_map_operations = {
251322d917d8SEric W. Biederman 	.open		= proc_uid_map_open,
251422d917d8SEric W. Biederman 	.write		= proc_uid_map_write,
251522d917d8SEric W. Biederman 	.read		= seq_read,
251622d917d8SEric W. Biederman 	.llseek		= seq_lseek,
251722d917d8SEric W. Biederman 	.release	= proc_id_map_release,
251822d917d8SEric W. Biederman };
251922d917d8SEric W. Biederman 
252022d917d8SEric W. Biederman static const struct file_operations proc_gid_map_operations = {
252122d917d8SEric W. Biederman 	.open		= proc_gid_map_open,
252222d917d8SEric W. Biederman 	.write		= proc_gid_map_write,
252322d917d8SEric W. Biederman 	.read		= seq_read,
252422d917d8SEric W. Biederman 	.llseek		= seq_lseek,
252522d917d8SEric W. Biederman 	.release	= proc_id_map_release,
252622d917d8SEric W. Biederman };
2527f76d207aSEric W. Biederman 
2528f76d207aSEric W. Biederman static const struct file_operations proc_projid_map_operations = {
2529f76d207aSEric W. Biederman 	.open		= proc_projid_map_open,
2530f76d207aSEric W. Biederman 	.write		= proc_projid_map_write,
2531f76d207aSEric W. Biederman 	.read		= seq_read,
2532f76d207aSEric W. Biederman 	.llseek		= seq_lseek,
2533f76d207aSEric W. Biederman 	.release	= proc_id_map_release,
2534f76d207aSEric W. Biederman };
253522d917d8SEric W. Biederman #endif /* CONFIG_USER_NS */
253622d917d8SEric W. Biederman 
253747830723SKees Cook static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
253847830723SKees Cook 				struct pid *pid, struct task_struct *task)
253947830723SKees Cook {
2540a9712bc1SAl Viro 	int err = lock_trace(task);
2541a9712bc1SAl Viro 	if (!err) {
254247830723SKees Cook 		seq_printf(m, "%08x\n", task->personality);
2543a9712bc1SAl Viro 		unlock_trace(task);
2544a9712bc1SAl Viro 	}
2545a9712bc1SAl Viro 	return err;
254647830723SKees Cook }
254747830723SKees Cook 
2548801199ceSEric W. Biederman /*
254928a6d671SEric W. Biederman  * Thread groups
255028a6d671SEric W. Biederman  */
255100977a59SArjan van de Ven static const struct file_operations proc_task_operations;
2552c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations;
255320cdc894SEric W. Biederman 
2554c5141e6dSEric Dumazet static const struct pid_entry tgid_base_stuff[] = {
2555631f9c18SAlexey Dobriyan 	DIR("task",       S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2556631f9c18SAlexey Dobriyan 	DIR("fd",         S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2557640708a2SPavel Emelyanov #ifdef CONFIG_CHECKPOINT_RESTORE
2558640708a2SPavel Emelyanov 	DIR("map_files",  S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
2559640708a2SPavel Emelyanov #endif
2560631f9c18SAlexey Dobriyan 	DIR("fdinfo",     S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
25616b4e306aSEric W. Biederman 	DIR("ns",	  S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
2562b2211a36SAndrew Morton #ifdef CONFIG_NET
2563631f9c18SAlexey Dobriyan 	DIR("net",        S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2564b2211a36SAndrew Morton #endif
2565631f9c18SAlexey Dobriyan 	REG("environ",    S_IRUSR, proc_environ_operations),
2566f9ea536eSAlexey Dobriyan 	ONE("auxv",       S_IRUSR, proc_pid_auxv),
2567631f9c18SAlexey Dobriyan 	ONE("status",     S_IRUGO, proc_pid_status),
256835a35046SDjalal Harouni 	ONE("personality", S_IRUSR, proc_pid_personality),
25691c963eb1SAlexey Dobriyan 	ONE("limits",	  S_IRUGO, proc_pid_limits),
257043ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
2571631f9c18SAlexey Dobriyan 	REG("sched",      S_IRUGO|S_IWUSR, proc_pid_sched_operations),
257243ae34cbSIngo Molnar #endif
25735091faa4SMike Galbraith #ifdef CONFIG_SCHED_AUTOGROUP
25745091faa4SMike Galbraith 	REG("autogroup",  S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
25755091faa4SMike Galbraith #endif
25764614a696Sjohn stultz 	REG("comm",      S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2577ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
257809d93bd6SAlexey Dobriyan 	ONE("syscall",    S_IRUSR, proc_pid_syscall),
2579ebcb6734SRoland McGrath #endif
25802ca66ff7SAlexey Dobriyan 	ONE("cmdline",    S_IRUGO, proc_pid_cmdline),
2581631f9c18SAlexey Dobriyan 	ONE("stat",       S_IRUGO, proc_tgid_stat),
2582631f9c18SAlexey Dobriyan 	ONE("statm",      S_IRUGO, proc_pid_statm),
2583b7643757SSiddhesh Poyarekar 	REG("maps",       S_IRUGO, proc_pid_maps_operations),
258428a6d671SEric W. Biederman #ifdef CONFIG_NUMA
2585b7643757SSiddhesh Poyarekar 	REG("numa_maps",  S_IRUGO, proc_pid_numa_maps_operations),
258628a6d671SEric W. Biederman #endif
2587631f9c18SAlexey Dobriyan 	REG("mem",        S_IRUSR|S_IWUSR, proc_mem_operations),
2588631f9c18SAlexey Dobriyan 	LNK("cwd",        proc_cwd_link),
2589631f9c18SAlexey Dobriyan 	LNK("root",       proc_root_link),
2590631f9c18SAlexey Dobriyan 	LNK("exe",        proc_exe_link),
2591631f9c18SAlexey Dobriyan 	REG("mounts",     S_IRUGO, proc_mounts_operations),
2592631f9c18SAlexey Dobriyan 	REG("mountinfo",  S_IRUGO, proc_mountinfo_operations),
2593631f9c18SAlexey Dobriyan 	REG("mountstats", S_IRUSR, proc_mountstats_operations),
25941e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR
2595631f9c18SAlexey Dobriyan 	REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2596b7643757SSiddhesh Poyarekar 	REG("smaps",      S_IRUGO, proc_pid_smaps_operations),
259732ed74a4SDjalal Harouni 	REG("pagemap",    S_IRUSR, proc_pagemap_operations),
259828a6d671SEric W. Biederman #endif
259928a6d671SEric W. Biederman #ifdef CONFIG_SECURITY
2600631f9c18SAlexey Dobriyan 	DIR("attr",       S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
260128a6d671SEric W. Biederman #endif
260228a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS
2603edfcd606SAlexey Dobriyan 	ONE("wchan",      S_IRUGO, proc_pid_wchan),
260428a6d671SEric W. Biederman #endif
26052ec220e2SKen Chen #ifdef CONFIG_STACKTRACE
260635a35046SDjalal Harouni 	ONE("stack",      S_IRUSR, proc_pid_stack),
260728a6d671SEric W. Biederman #endif
260828a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS
2609f6e826caSAlexey Dobriyan 	ONE("schedstat",  S_IRUGO, proc_pid_schedstat),
261028a6d671SEric W. Biederman #endif
26119745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP
2612631f9c18SAlexey Dobriyan 	REG("latency",  S_IRUGO, proc_lstats_operations),
26139745512cSArjan van de Ven #endif
26148793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET
2615631f9c18SAlexey Dobriyan 	REG("cpuset",     S_IRUGO, proc_cpuset_operations),
261628a6d671SEric W. Biederman #endif
2617a424316cSPaul Menage #ifdef CONFIG_CGROUPS
2618631f9c18SAlexey Dobriyan 	REG("cgroup",  S_IRUGO, proc_cgroup_operations),
2619a424316cSPaul Menage #endif
2620*6ba51e37SAlexey Dobriyan 	ONE("oom_score",  S_IRUGO, proc_oom_score),
2621fa0cbbf1SDavid Rientjes 	REG("oom_adj",    S_IRUGO|S_IWUSR, proc_oom_adj_operations),
2622a63d83f4SDavid Rientjes 	REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
262328a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL
2624631f9c18SAlexey Dobriyan 	REG("loginuid",   S_IWUSR|S_IRUGO, proc_loginuid_operations),
2625631f9c18SAlexey Dobriyan 	REG("sessionid",  S_IRUGO, proc_sessionid_operations),
262628a6d671SEric W. Biederman #endif
2627f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
2628631f9c18SAlexey Dobriyan 	REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2629f4f154fdSAkinobu Mita #endif
2630698ba7b5SChristoph Hellwig #ifdef CONFIG_ELF_CORE
2631631f9c18SAlexey Dobriyan 	REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
26323cb4a0bbSKawai, Hidehiro #endif
2633aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING
26341d1221f3SVasiliy Kulikov 	INF("io",	S_IRUSR, proc_tgid_io_accounting),
2635aba76fdbSAndrew Morton #endif
2636f133eccaSChris Metcalf #ifdef CONFIG_HARDWALL
2637f133eccaSChris Metcalf 	INF("hardwall",   S_IRUGO, proc_pid_hardwall),
2638f133eccaSChris Metcalf #endif
263922d917d8SEric W. Biederman #ifdef CONFIG_USER_NS
264022d917d8SEric W. Biederman 	REG("uid_map",    S_IRUGO|S_IWUSR, proc_uid_map_operations),
264122d917d8SEric W. Biederman 	REG("gid_map",    S_IRUGO|S_IWUSR, proc_gid_map_operations),
2642f76d207aSEric W. Biederman 	REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
264322d917d8SEric W. Biederman #endif
264448f6a7a5SPavel Emelyanov #ifdef CONFIG_CHECKPOINT_RESTORE
264548f6a7a5SPavel Emelyanov 	REG("timers",	  S_IRUGO, proc_timers_operations),
264648f6a7a5SPavel Emelyanov #endif
264728a6d671SEric W. Biederman };
264828a6d671SEric W. Biederman 
2649f0c3b509SAl Viro static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
265028a6d671SEric W. Biederman {
2651f0c3b509SAl Viro 	return proc_pident_readdir(file, ctx,
265228a6d671SEric W. Biederman 				   tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
265328a6d671SEric W. Biederman }
265428a6d671SEric W. Biederman 
265500977a59SArjan van de Ven static const struct file_operations proc_tgid_base_operations = {
265628a6d671SEric W. Biederman 	.read		= generic_read_dir,
2657f0c3b509SAl Viro 	.iterate	= proc_tgid_base_readdir,
26586038f373SArnd Bergmann 	.llseek		= default_llseek,
265928a6d671SEric W. Biederman };
266028a6d671SEric W. Biederman 
266100cd8dd3SAl Viro static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
266200cd8dd3SAl Viro {
26637bcd6b0eSEric W. Biederman 	return proc_pident_lookup(dir, dentry,
26647bcd6b0eSEric W. Biederman 				  tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
266528a6d671SEric W. Biederman }
266628a6d671SEric W. Biederman 
2667c5ef1c42SArjan van de Ven static const struct inode_operations proc_tgid_base_inode_operations = {
266828a6d671SEric W. Biederman 	.lookup		= proc_tgid_base_lookup,
266928a6d671SEric W. Biederman 	.getattr	= pid_getattr,
267028a6d671SEric W. Biederman 	.setattr	= proc_setattr,
26710499680aSVasiliy Kulikov 	.permission	= proc_pid_permission,
267228a6d671SEric W. Biederman };
267328a6d671SEric W. Biederman 
267460347f67SPavel Emelyanov static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
26751da177e4SLinus Torvalds {
267648e6484dSEric W. Biederman 	struct dentry *dentry, *leader, *dir;
26778578cea7SEric W. Biederman 	char buf[PROC_NUMBUF];
267848e6484dSEric W. Biederman 	struct qstr name;
26791da177e4SLinus Torvalds 
268048e6484dSEric W. Biederman 	name.name = buf;
268160347f67SPavel Emelyanov 	name.len = snprintf(buf, sizeof(buf), "%d", pid);
26824f522a24SAl Viro 	/* no ->d_hash() rejects on procfs */
268360347f67SPavel Emelyanov 	dentry = d_hash_and_lookup(mnt->mnt_root, &name);
268448e6484dSEric W. Biederman 	if (dentry) {
268548e6484dSEric W. Biederman 		shrink_dcache_parent(dentry);
268648e6484dSEric W. Biederman 		d_drop(dentry);
268748e6484dSEric W. Biederman 		dput(dentry);
26881da177e4SLinus Torvalds 	}
26891da177e4SLinus Torvalds 
269048e6484dSEric W. Biederman 	name.name = buf;
269160347f67SPavel Emelyanov 	name.len = snprintf(buf, sizeof(buf), "%d", tgid);
269260347f67SPavel Emelyanov 	leader = d_hash_and_lookup(mnt->mnt_root, &name);
269348e6484dSEric W. Biederman 	if (!leader)
269448e6484dSEric W. Biederman 		goto out;
269548e6484dSEric W. Biederman 
269648e6484dSEric W. Biederman 	name.name = "task";
269748e6484dSEric W. Biederman 	name.len = strlen(name.name);
269848e6484dSEric W. Biederman 	dir = d_hash_and_lookup(leader, &name);
269948e6484dSEric W. Biederman 	if (!dir)
270048e6484dSEric W. Biederman 		goto out_put_leader;
270148e6484dSEric W. Biederman 
270248e6484dSEric W. Biederman 	name.name = buf;
270360347f67SPavel Emelyanov 	name.len = snprintf(buf, sizeof(buf), "%d", pid);
270448e6484dSEric W. Biederman 	dentry = d_hash_and_lookup(dir, &name);
270548e6484dSEric W. Biederman 	if (dentry) {
270648e6484dSEric W. Biederman 		shrink_dcache_parent(dentry);
270748e6484dSEric W. Biederman 		d_drop(dentry);
270848e6484dSEric W. Biederman 		dput(dentry);
27091da177e4SLinus Torvalds 	}
271048e6484dSEric W. Biederman 
271148e6484dSEric W. Biederman 	dput(dir);
271248e6484dSEric W. Biederman out_put_leader:
271348e6484dSEric W. Biederman 	dput(leader);
271448e6484dSEric W. Biederman out:
271548e6484dSEric W. Biederman 	return;
27161da177e4SLinus Torvalds }
27171da177e4SLinus Torvalds 
27180895e91dSRandy Dunlap /**
27190895e91dSRandy Dunlap  * proc_flush_task -  Remove dcache entries for @task from the /proc dcache.
27200895e91dSRandy Dunlap  * @task: task that should be flushed.
27210895e91dSRandy Dunlap  *
27220895e91dSRandy Dunlap  * When flushing dentries from proc, one needs to flush them from global
272360347f67SPavel Emelyanov  * proc (proc_mnt) and from all the namespaces' procs this task was seen
27240895e91dSRandy Dunlap  * in. This call is supposed to do all of this job.
27250895e91dSRandy Dunlap  *
27260895e91dSRandy Dunlap  * Looks in the dcache for
27270895e91dSRandy Dunlap  * /proc/@pid
27280895e91dSRandy Dunlap  * /proc/@tgid/task/@pid
27290895e91dSRandy Dunlap  * if either directory is present flushes it and all of it'ts children
27300895e91dSRandy Dunlap  * from the dcache.
27310895e91dSRandy Dunlap  *
27320895e91dSRandy Dunlap  * It is safe and reasonable to cache /proc entries for a task until
27330895e91dSRandy Dunlap  * that task exits.  After that they just clog up the dcache with
27340895e91dSRandy Dunlap  * useless entries, possibly causing useful dcache entries to be
27350895e91dSRandy Dunlap  * flushed instead.  This routine is proved to flush those useless
27360895e91dSRandy Dunlap  * dcache entries at process exit time.
27370895e91dSRandy Dunlap  *
27380895e91dSRandy Dunlap  * NOTE: This routine is just an optimization so it does not guarantee
27390895e91dSRandy Dunlap  *       that no dcache entries will exist at process exit time it
27400895e91dSRandy Dunlap  *       just makes it very unlikely that any will persist.
274160347f67SPavel Emelyanov  */
274260347f67SPavel Emelyanov 
274360347f67SPavel Emelyanov void proc_flush_task(struct task_struct *task)
274460347f67SPavel Emelyanov {
27459fcc2d15SEric W. Biederman 	int i;
27469b4d1cbeSOleg Nesterov 	struct pid *pid, *tgid;
2747130f77ecSPavel Emelyanov 	struct upid *upid;
2748130f77ecSPavel Emelyanov 
2749130f77ecSPavel Emelyanov 	pid = task_pid(task);
2750130f77ecSPavel Emelyanov 	tgid = task_tgid(task);
27519fcc2d15SEric W. Biederman 
27529fcc2d15SEric W. Biederman 	for (i = 0; i <= pid->level; i++) {
2753130f77ecSPavel Emelyanov 		upid = &pid->numbers[i];
2754130f77ecSPavel Emelyanov 		proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
27559b4d1cbeSOleg Nesterov 					tgid->numbers[i].nr);
2756130f77ecSPavel Emelyanov 	}
275760347f67SPavel Emelyanov }
275860347f67SPavel Emelyanov 
2759c52a47acSAl Viro static int proc_pid_instantiate(struct inode *dir,
27609711ef99SAdrian Bunk 				   struct dentry * dentry,
2761c5141e6dSEric Dumazet 				   struct task_struct *task, const void *ptr)
2762444ceed8SEric W. Biederman {
2763444ceed8SEric W. Biederman 	struct inode *inode;
2764444ceed8SEric W. Biederman 
276561a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
2766444ceed8SEric W. Biederman 	if (!inode)
2767444ceed8SEric W. Biederman 		goto out;
2768444ceed8SEric W. Biederman 
2769444ceed8SEric W. Biederman 	inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2770444ceed8SEric W. Biederman 	inode->i_op = &proc_tgid_base_inode_operations;
2771444ceed8SEric W. Biederman 	inode->i_fop = &proc_tgid_base_operations;
2772444ceed8SEric W. Biederman 	inode->i_flags|=S_IMMUTABLE;
2773aed54175SVegard Nossum 
2774bfe86848SMiklos Szeredi 	set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
2775bfe86848SMiklos Szeredi 						  ARRAY_SIZE(tgid_base_stuff)));
2776444ceed8SEric W. Biederman 
2777fb045adbSNick Piggin 	d_set_d_op(dentry, &pid_dentry_operations);
2778444ceed8SEric W. Biederman 
2779444ceed8SEric W. Biederman 	d_add(dentry, inode);
2780444ceed8SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
27810b728e19SAl Viro 	if (pid_revalidate(dentry, 0))
2782c52a47acSAl Viro 		return 0;
2783444ceed8SEric W. Biederman out:
2784c52a47acSAl Viro 	return -ENOENT;
2785444ceed8SEric W. Biederman }
2786444ceed8SEric W. Biederman 
278700cd8dd3SAl Viro struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
27881da177e4SLinus Torvalds {
2789335eb531SAlexey Dobriyan 	int result = -ENOENT;
27901da177e4SLinus Torvalds 	struct task_struct *task;
27911da177e4SLinus Torvalds 	unsigned tgid;
2792b488893aSPavel Emelyanov 	struct pid_namespace *ns;
27931da177e4SLinus Torvalds 
2794dbcdb504SAlexey Dobriyan 	tgid = name_to_int(&dentry->d_name);
27951da177e4SLinus Torvalds 	if (tgid == ~0U)
27961da177e4SLinus Torvalds 		goto out;
27971da177e4SLinus Torvalds 
2798b488893aSPavel Emelyanov 	ns = dentry->d_sb->s_fs_info;
2799de758734SEric W. Biederman 	rcu_read_lock();
2800b488893aSPavel Emelyanov 	task = find_task_by_pid_ns(tgid, ns);
28011da177e4SLinus Torvalds 	if (task)
28021da177e4SLinus Torvalds 		get_task_struct(task);
2803de758734SEric W. Biederman 	rcu_read_unlock();
28041da177e4SLinus Torvalds 	if (!task)
28051da177e4SLinus Torvalds 		goto out;
28061da177e4SLinus Torvalds 
2807444ceed8SEric W. Biederman 	result = proc_pid_instantiate(dir, dentry, task, NULL);
280848e6484dSEric W. Biederman 	put_task_struct(task);
28091da177e4SLinus Torvalds out:
2810c52a47acSAl Viro 	return ERR_PTR(result);
28111da177e4SLinus Torvalds }
28121da177e4SLinus Torvalds 
28131da177e4SLinus Torvalds /*
28140804ef4bSEric W. Biederman  * Find the first task with tgid >= tgid
28150bc58a91SEric W. Biederman  *
28161da177e4SLinus Torvalds  */
281719fd4bb2SEric W. Biederman struct tgid_iter {
281819fd4bb2SEric W. Biederman 	unsigned int tgid;
28190804ef4bSEric W. Biederman 	struct task_struct *task;
282019fd4bb2SEric W. Biederman };
282119fd4bb2SEric W. Biederman static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
282219fd4bb2SEric W. Biederman {
28230804ef4bSEric W. Biederman 	struct pid *pid;
28241da177e4SLinus Torvalds 
282519fd4bb2SEric W. Biederman 	if (iter.task)
282619fd4bb2SEric W. Biederman 		put_task_struct(iter.task);
28270804ef4bSEric W. Biederman 	rcu_read_lock();
28280804ef4bSEric W. Biederman retry:
282919fd4bb2SEric W. Biederman 	iter.task = NULL;
283019fd4bb2SEric W. Biederman 	pid = find_ge_pid(iter.tgid, ns);
28310804ef4bSEric W. Biederman 	if (pid) {
283219fd4bb2SEric W. Biederman 		iter.tgid = pid_nr_ns(pid, ns);
283319fd4bb2SEric W. Biederman 		iter.task = pid_task(pid, PIDTYPE_PID);
28340804ef4bSEric W. Biederman 		/* What we to know is if the pid we have find is the
28350804ef4bSEric W. Biederman 		 * pid of a thread_group_leader.  Testing for task
28360804ef4bSEric W. Biederman 		 * being a thread_group_leader is the obvious thing
28370804ef4bSEric W. Biederman 		 * todo but there is a window when it fails, due to
28380804ef4bSEric W. Biederman 		 * the pid transfer logic in de_thread.
28390804ef4bSEric W. Biederman 		 *
28400804ef4bSEric W. Biederman 		 * So we perform the straight forward test of seeing
28410804ef4bSEric W. Biederman 		 * if the pid we have found is the pid of a thread
28420804ef4bSEric W. Biederman 		 * group leader, and don't worry if the task we have
28430804ef4bSEric W. Biederman 		 * found doesn't happen to be a thread group leader.
28440804ef4bSEric W. Biederman 		 * As we don't care in the case of readdir.
28450bc58a91SEric W. Biederman 		 */
284619fd4bb2SEric W. Biederman 		if (!iter.task || !has_group_leader_pid(iter.task)) {
284719fd4bb2SEric W. Biederman 			iter.tgid += 1;
28480804ef4bSEric W. Biederman 			goto retry;
284919fd4bb2SEric W. Biederman 		}
285019fd4bb2SEric W. Biederman 		get_task_struct(iter.task);
28511da177e4SLinus Torvalds 	}
2852454cc105SEric W. Biederman 	rcu_read_unlock();
285319fd4bb2SEric W. Biederman 	return iter;
28541da177e4SLinus Torvalds }
28551da177e4SLinus Torvalds 
2856021ada7dSAl Viro #define TGID_OFFSET (FIRST_PROCESS_ENTRY + 1)
28571da177e4SLinus Torvalds 
28581da177e4SLinus Torvalds /* for the /proc/ directory itself, after non-process stuff has been done */
2859f0c3b509SAl Viro int proc_pid_readdir(struct file *file, struct dir_context *ctx)
28601da177e4SLinus Torvalds {
286119fd4bb2SEric W. Biederman 	struct tgid_iter iter;
2862db963164SAl Viro 	struct pid_namespace *ns = file->f_dentry->d_sb->s_fs_info;
2863f0c3b509SAl Viro 	loff_t pos = ctx->pos;
28641da177e4SLinus Torvalds 
2865021ada7dSAl Viro 	if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
2866f0c3b509SAl Viro 		return 0;
28671da177e4SLinus Torvalds 
2868021ada7dSAl Viro 	if (pos == TGID_OFFSET - 1) {
2869db963164SAl Viro 		struct inode *inode = ns->proc_self->d_inode;
2870db963164SAl Viro 		if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
2871f0c3b509SAl Viro 			return 0;
2872021ada7dSAl Viro 		iter.tgid = 0;
2873021ada7dSAl Viro 	} else {
2874021ada7dSAl Viro 		iter.tgid = pos - TGID_OFFSET;
2875021ada7dSAl Viro 	}
287619fd4bb2SEric W. Biederman 	iter.task = NULL;
287719fd4bb2SEric W. Biederman 	for (iter = next_tgid(ns, iter);
287819fd4bb2SEric W. Biederman 	     iter.task;
287919fd4bb2SEric W. Biederman 	     iter.tgid += 1, iter = next_tgid(ns, iter)) {
2880f0c3b509SAl Viro 		char name[PROC_NUMBUF];
2881f0c3b509SAl Viro 		int len;
2882f0c3b509SAl Viro 		if (!has_pid_permissions(ns, iter.task, 2))
2883f0c3b509SAl Viro 			continue;
28840499680aSVasiliy Kulikov 
2885f0c3b509SAl Viro 		len = snprintf(name, sizeof(name), "%d", iter.tgid);
2886f0c3b509SAl Viro 		ctx->pos = iter.tgid + TGID_OFFSET;
2887f0c3b509SAl Viro 		if (!proc_fill_cache(file, ctx, name, len,
2888f0c3b509SAl Viro 				     proc_pid_instantiate, iter.task, NULL)) {
288919fd4bb2SEric W. Biederman 			put_task_struct(iter.task);
2890f0c3b509SAl Viro 			return 0;
28911da177e4SLinus Torvalds 		}
28921da177e4SLinus Torvalds 	}
2893f0c3b509SAl Viro 	ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
28941da177e4SLinus Torvalds 	return 0;
28951da177e4SLinus Torvalds }
28961da177e4SLinus Torvalds 
28970bc58a91SEric W. Biederman /*
289828a6d671SEric W. Biederman  * Tasks
289928a6d671SEric W. Biederman  */
2900c5141e6dSEric Dumazet static const struct pid_entry tid_base_stuff[] = {
2901631f9c18SAlexey Dobriyan 	DIR("fd",        S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
29023835541dSJerome Marchand 	DIR("fdinfo",    S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
29036b4e306aSEric W. Biederman 	DIR("ns",	 S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
2904631f9c18SAlexey Dobriyan 	REG("environ",   S_IRUSR, proc_environ_operations),
2905f9ea536eSAlexey Dobriyan 	ONE("auxv",      S_IRUSR, proc_pid_auxv),
2906631f9c18SAlexey Dobriyan 	ONE("status",    S_IRUGO, proc_pid_status),
290735a35046SDjalal Harouni 	ONE("personality", S_IRUSR, proc_pid_personality),
29081c963eb1SAlexey Dobriyan 	ONE("limits",	 S_IRUGO, proc_pid_limits),
290943ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
2910631f9c18SAlexey Dobriyan 	REG("sched",     S_IRUGO|S_IWUSR, proc_pid_sched_operations),
291143ae34cbSIngo Molnar #endif
29124614a696Sjohn stultz 	REG("comm",      S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2913ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
291409d93bd6SAlexey Dobriyan 	ONE("syscall",   S_IRUSR, proc_pid_syscall),
2915ebcb6734SRoland McGrath #endif
29162ca66ff7SAlexey Dobriyan 	ONE("cmdline",   S_IRUGO, proc_pid_cmdline),
2917631f9c18SAlexey Dobriyan 	ONE("stat",      S_IRUGO, proc_tid_stat),
2918631f9c18SAlexey Dobriyan 	ONE("statm",     S_IRUGO, proc_pid_statm),
2919b7643757SSiddhesh Poyarekar 	REG("maps",      S_IRUGO, proc_tid_maps_operations),
292081841161SCyrill Gorcunov #ifdef CONFIG_CHECKPOINT_RESTORE
292181841161SCyrill Gorcunov 	REG("children",  S_IRUGO, proc_tid_children_operations),
292281841161SCyrill Gorcunov #endif
292328a6d671SEric W. Biederman #ifdef CONFIG_NUMA
2924b7643757SSiddhesh Poyarekar 	REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
292528a6d671SEric W. Biederman #endif
2926631f9c18SAlexey Dobriyan 	REG("mem",       S_IRUSR|S_IWUSR, proc_mem_operations),
2927631f9c18SAlexey Dobriyan 	LNK("cwd",       proc_cwd_link),
2928631f9c18SAlexey Dobriyan 	LNK("root",      proc_root_link),
2929631f9c18SAlexey Dobriyan 	LNK("exe",       proc_exe_link),
2930631f9c18SAlexey Dobriyan 	REG("mounts",    S_IRUGO, proc_mounts_operations),
2931631f9c18SAlexey Dobriyan 	REG("mountinfo",  S_IRUGO, proc_mountinfo_operations),
29321e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR
2933631f9c18SAlexey Dobriyan 	REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2934b7643757SSiddhesh Poyarekar 	REG("smaps",     S_IRUGO, proc_tid_smaps_operations),
293532ed74a4SDjalal Harouni 	REG("pagemap",    S_IRUSR, proc_pagemap_operations),
293628a6d671SEric W. Biederman #endif
293728a6d671SEric W. Biederman #ifdef CONFIG_SECURITY
2938631f9c18SAlexey Dobriyan 	DIR("attr",      S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
293928a6d671SEric W. Biederman #endif
294028a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS
2941edfcd606SAlexey Dobriyan 	ONE("wchan",     S_IRUGO, proc_pid_wchan),
294228a6d671SEric W. Biederman #endif
29432ec220e2SKen Chen #ifdef CONFIG_STACKTRACE
294435a35046SDjalal Harouni 	ONE("stack",      S_IRUSR, proc_pid_stack),
294528a6d671SEric W. Biederman #endif
294628a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS
2947f6e826caSAlexey Dobriyan 	ONE("schedstat", S_IRUGO, proc_pid_schedstat),
294828a6d671SEric W. Biederman #endif
29499745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP
2950631f9c18SAlexey Dobriyan 	REG("latency",  S_IRUGO, proc_lstats_operations),
29519745512cSArjan van de Ven #endif
29528793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET
2953631f9c18SAlexey Dobriyan 	REG("cpuset",    S_IRUGO, proc_cpuset_operations),
295428a6d671SEric W. Biederman #endif
2955a424316cSPaul Menage #ifdef CONFIG_CGROUPS
2956631f9c18SAlexey Dobriyan 	REG("cgroup",  S_IRUGO, proc_cgroup_operations),
2957a424316cSPaul Menage #endif
2958*6ba51e37SAlexey Dobriyan 	ONE("oom_score", S_IRUGO, proc_oom_score),
2959fa0cbbf1SDavid Rientjes 	REG("oom_adj",   S_IRUGO|S_IWUSR, proc_oom_adj_operations),
2960a63d83f4SDavid Rientjes 	REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
296128a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL
2962631f9c18SAlexey Dobriyan 	REG("loginuid",  S_IWUSR|S_IRUGO, proc_loginuid_operations),
296326ec3c64SAl Viro 	REG("sessionid",  S_IRUGO, proc_sessionid_operations),
296428a6d671SEric W. Biederman #endif
2965f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
2966631f9c18SAlexey Dobriyan 	REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2967f4f154fdSAkinobu Mita #endif
2968297c5d92SAndrea Righi #ifdef CONFIG_TASK_IO_ACCOUNTING
29691d1221f3SVasiliy Kulikov 	INF("io",	S_IRUSR, proc_tid_io_accounting),
2970297c5d92SAndrea Righi #endif
2971f133eccaSChris Metcalf #ifdef CONFIG_HARDWALL
2972f133eccaSChris Metcalf 	INF("hardwall",   S_IRUGO, proc_pid_hardwall),
2973f133eccaSChris Metcalf #endif
297422d917d8SEric W. Biederman #ifdef CONFIG_USER_NS
297522d917d8SEric W. Biederman 	REG("uid_map",    S_IRUGO|S_IWUSR, proc_uid_map_operations),
297622d917d8SEric W. Biederman 	REG("gid_map",    S_IRUGO|S_IWUSR, proc_gid_map_operations),
2977f76d207aSEric W. Biederman 	REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
297822d917d8SEric W. Biederman #endif
297928a6d671SEric W. Biederman };
298028a6d671SEric W. Biederman 
2981f0c3b509SAl Viro static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
298228a6d671SEric W. Biederman {
2983f0c3b509SAl Viro 	return proc_pident_readdir(file, ctx,
298428a6d671SEric W. Biederman 				   tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
298528a6d671SEric W. Biederman }
298628a6d671SEric W. Biederman 
298700cd8dd3SAl Viro static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
298800cd8dd3SAl Viro {
29897bcd6b0eSEric W. Biederman 	return proc_pident_lookup(dir, dentry,
29907bcd6b0eSEric W. Biederman 				  tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
299128a6d671SEric W. Biederman }
299228a6d671SEric W. Biederman 
299300977a59SArjan van de Ven static const struct file_operations proc_tid_base_operations = {
299428a6d671SEric W. Biederman 	.read		= generic_read_dir,
2995f0c3b509SAl Viro 	.iterate	= proc_tid_base_readdir,
29966038f373SArnd Bergmann 	.llseek		= default_llseek,
299728a6d671SEric W. Biederman };
299828a6d671SEric W. Biederman 
2999c5ef1c42SArjan van de Ven static const struct inode_operations proc_tid_base_inode_operations = {
300028a6d671SEric W. Biederman 	.lookup		= proc_tid_base_lookup,
300128a6d671SEric W. Biederman 	.getattr	= pid_getattr,
300228a6d671SEric W. Biederman 	.setattr	= proc_setattr,
300328a6d671SEric W. Biederman };
300428a6d671SEric W. Biederman 
3005c52a47acSAl Viro static int proc_task_instantiate(struct inode *dir,
3006c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
3007444ceed8SEric W. Biederman {
3008444ceed8SEric W. Biederman 	struct inode *inode;
300961a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
3010444ceed8SEric W. Biederman 
3011444ceed8SEric W. Biederman 	if (!inode)
3012444ceed8SEric W. Biederman 		goto out;
3013444ceed8SEric W. Biederman 	inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3014444ceed8SEric W. Biederman 	inode->i_op = &proc_tid_base_inode_operations;
3015444ceed8SEric W. Biederman 	inode->i_fop = &proc_tid_base_operations;
3016444ceed8SEric W. Biederman 	inode->i_flags|=S_IMMUTABLE;
3017aed54175SVegard Nossum 
3018bfe86848SMiklos Szeredi 	set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3019bfe86848SMiklos Szeredi 						  ARRAY_SIZE(tid_base_stuff)));
3020444ceed8SEric W. Biederman 
3021fb045adbSNick Piggin 	d_set_d_op(dentry, &pid_dentry_operations);
3022444ceed8SEric W. Biederman 
3023444ceed8SEric W. Biederman 	d_add(dentry, inode);
3024444ceed8SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
30250b728e19SAl Viro 	if (pid_revalidate(dentry, 0))
3026c52a47acSAl Viro 		return 0;
3027444ceed8SEric W. Biederman out:
3028c52a47acSAl Viro 	return -ENOENT;
3029444ceed8SEric W. Biederman }
3030444ceed8SEric W. Biederman 
303100cd8dd3SAl Viro static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
303228a6d671SEric W. Biederman {
3033c52a47acSAl Viro 	int result = -ENOENT;
303428a6d671SEric W. Biederman 	struct task_struct *task;
303528a6d671SEric W. Biederman 	struct task_struct *leader = get_proc_task(dir);
303628a6d671SEric W. Biederman 	unsigned tid;
3037b488893aSPavel Emelyanov 	struct pid_namespace *ns;
303828a6d671SEric W. Biederman 
303928a6d671SEric W. Biederman 	if (!leader)
304028a6d671SEric W. Biederman 		goto out_no_task;
304128a6d671SEric W. Biederman 
3042dbcdb504SAlexey Dobriyan 	tid = name_to_int(&dentry->d_name);
304328a6d671SEric W. Biederman 	if (tid == ~0U)
304428a6d671SEric W. Biederman 		goto out;
304528a6d671SEric W. Biederman 
3046b488893aSPavel Emelyanov 	ns = dentry->d_sb->s_fs_info;
304728a6d671SEric W. Biederman 	rcu_read_lock();
3048b488893aSPavel Emelyanov 	task = find_task_by_pid_ns(tid, ns);
304928a6d671SEric W. Biederman 	if (task)
305028a6d671SEric W. Biederman 		get_task_struct(task);
305128a6d671SEric W. Biederman 	rcu_read_unlock();
305228a6d671SEric W. Biederman 	if (!task)
305328a6d671SEric W. Biederman 		goto out;
3054bac0abd6SPavel Emelyanov 	if (!same_thread_group(leader, task))
305528a6d671SEric W. Biederman 		goto out_drop_task;
305628a6d671SEric W. Biederman 
3057444ceed8SEric W. Biederman 	result = proc_task_instantiate(dir, dentry, task, NULL);
305828a6d671SEric W. Biederman out_drop_task:
305928a6d671SEric W. Biederman 	put_task_struct(task);
306028a6d671SEric W. Biederman out:
306128a6d671SEric W. Biederman 	put_task_struct(leader);
306228a6d671SEric W. Biederman out_no_task:
3063c52a47acSAl Viro 	return ERR_PTR(result);
306428a6d671SEric W. Biederman }
306528a6d671SEric W. Biederman 
306628a6d671SEric W. Biederman /*
30670bc58a91SEric W. Biederman  * Find the first tid of a thread group to return to user space.
30680bc58a91SEric W. Biederman  *
30690bc58a91SEric W. Biederman  * Usually this is just the thread group leader, but if the users
30700bc58a91SEric W. Biederman  * buffer was too small or there was a seek into the middle of the
30710bc58a91SEric W. Biederman  * directory we have more work todo.
30720bc58a91SEric W. Biederman  *
30730bc58a91SEric W. Biederman  * In the case of a short read we start with find_task_by_pid.
30740bc58a91SEric W. Biederman  *
30750bc58a91SEric W. Biederman  * In the case of a seek we start with the leader and walk nr
30760bc58a91SEric W. Biederman  * threads past it.
30770bc58a91SEric W. Biederman  */
30789f6e963fSOleg Nesterov static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
30799f6e963fSOleg Nesterov 					struct pid_namespace *ns)
30800bc58a91SEric W. Biederman {
3081d855a4b7SOleg Nesterov 	struct task_struct *pos, *task;
30829f6e963fSOleg Nesterov 	unsigned long nr = f_pos;
30839f6e963fSOleg Nesterov 
30849f6e963fSOleg Nesterov 	if (nr != f_pos)	/* 32bit overflow? */
30859f6e963fSOleg Nesterov 		return NULL;
30860bc58a91SEric W. Biederman 
3087cc288738SEric W. Biederman 	rcu_read_lock();
3088d855a4b7SOleg Nesterov 	task = pid_task(pid, PIDTYPE_PID);
3089d855a4b7SOleg Nesterov 	if (!task)
3090d855a4b7SOleg Nesterov 		goto fail;
3091d855a4b7SOleg Nesterov 
3092d855a4b7SOleg Nesterov 	/* Attempt to start with the tid of a thread */
30939f6e963fSOleg Nesterov 	if (tid && nr) {
3094b488893aSPavel Emelyanov 		pos = find_task_by_pid_ns(tid, ns);
3095d855a4b7SOleg Nesterov 		if (pos && same_thread_group(pos, task))
3096a872ff0cSOleg Nesterov 			goto found;
30970bc58a91SEric W. Biederman 	}
30980bc58a91SEric W. Biederman 
30990bc58a91SEric W. Biederman 	/* If nr exceeds the number of threads there is nothing todo */
31009f6e963fSOleg Nesterov 	if (nr >= get_nr_threads(task))
3101c986c14aSOleg Nesterov 		goto fail;
3102a872ff0cSOleg Nesterov 
3103a872ff0cSOleg Nesterov 	/* If we haven't found our starting place yet start
3104a872ff0cSOleg Nesterov 	 * with the leader and walk nr threads forward.
3105a872ff0cSOleg Nesterov 	 */
3106d855a4b7SOleg Nesterov 	pos = task = task->group_leader;
3107c986c14aSOleg Nesterov 	do {
31089f6e963fSOleg Nesterov 		if (!nr--)
3109c986c14aSOleg Nesterov 			goto found;
3110d855a4b7SOleg Nesterov 	} while_each_thread(task, pos);
3111c986c14aSOleg Nesterov fail:
3112a872ff0cSOleg Nesterov 	pos = NULL;
3113a872ff0cSOleg Nesterov 	goto out;
3114a872ff0cSOleg Nesterov found:
3115a872ff0cSOleg Nesterov 	get_task_struct(pos);
3116a872ff0cSOleg Nesterov out:
3117cc288738SEric W. Biederman 	rcu_read_unlock();
31180bc58a91SEric W. Biederman 	return pos;
31190bc58a91SEric W. Biederman }
31200bc58a91SEric W. Biederman 
31210bc58a91SEric W. Biederman /*
31220bc58a91SEric W. Biederman  * Find the next thread in the thread list.
31230bc58a91SEric W. Biederman  * Return NULL if there is an error or no next thread.
31240bc58a91SEric W. Biederman  *
31250bc58a91SEric W. Biederman  * The reference to the input task_struct is released.
31260bc58a91SEric W. Biederman  */
31270bc58a91SEric W. Biederman static struct task_struct *next_tid(struct task_struct *start)
31280bc58a91SEric W. Biederman {
3129c1df7fb8SOleg Nesterov 	struct task_struct *pos = NULL;
3130cc288738SEric W. Biederman 	rcu_read_lock();
3131c1df7fb8SOleg Nesterov 	if (pid_alive(start)) {
31320bc58a91SEric W. Biederman 		pos = next_thread(start);
3133c1df7fb8SOleg Nesterov 		if (thread_group_leader(pos))
31340bc58a91SEric W. Biederman 			pos = NULL;
3135c1df7fb8SOleg Nesterov 		else
3136c1df7fb8SOleg Nesterov 			get_task_struct(pos);
3137c1df7fb8SOleg Nesterov 	}
3138cc288738SEric W. Biederman 	rcu_read_unlock();
31390bc58a91SEric W. Biederman 	put_task_struct(start);
31400bc58a91SEric W. Biederman 	return pos;
31410bc58a91SEric W. Biederman }
31420bc58a91SEric W. Biederman 
31431da177e4SLinus Torvalds /* for the /proc/TGID/task/ directories */
3144f0c3b509SAl Viro static int proc_task_readdir(struct file *file, struct dir_context *ctx)
31451da177e4SLinus Torvalds {
3146d855a4b7SOleg Nesterov 	struct inode *inode = file_inode(file);
3147d855a4b7SOleg Nesterov 	struct task_struct *task;
3148b488893aSPavel Emelyanov 	struct pid_namespace *ns;
3149f0c3b509SAl Viro 	int tid;
31501da177e4SLinus Torvalds 
3151d855a4b7SOleg Nesterov 	if (proc_inode_is_dead(inode))
3152f0c3b509SAl Viro 		return -ENOENT;
31531da177e4SLinus Torvalds 
3154f0c3b509SAl Viro 	if (!dir_emit_dots(file, ctx))
3155d855a4b7SOleg Nesterov 		return 0;
31561da177e4SLinus Torvalds 
31570bc58a91SEric W. Biederman 	/* f_version caches the tgid value that the last readdir call couldn't
31580bc58a91SEric W. Biederman 	 * return. lseek aka telldir automagically resets f_version to 0.
31590bc58a91SEric W. Biederman 	 */
3160f0c3b509SAl Viro 	ns = file->f_dentry->d_sb->s_fs_info;
3161f0c3b509SAl Viro 	tid = (int)file->f_version;
3162f0c3b509SAl Viro 	file->f_version = 0;
3163d855a4b7SOleg Nesterov 	for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
31640bc58a91SEric W. Biederman 	     task;
3165f0c3b509SAl Viro 	     task = next_tid(task), ctx->pos++) {
3166f0c3b509SAl Viro 		char name[PROC_NUMBUF];
3167f0c3b509SAl Viro 		int len;
3168b488893aSPavel Emelyanov 		tid = task_pid_nr_ns(task, ns);
3169f0c3b509SAl Viro 		len = snprintf(name, sizeof(name), "%d", tid);
3170f0c3b509SAl Viro 		if (!proc_fill_cache(file, ctx, name, len,
3171f0c3b509SAl Viro 				proc_task_instantiate, task, NULL)) {
31720bc58a91SEric W. Biederman 			/* returning this tgid failed, save it as the first
31730bc58a91SEric W. Biederman 			 * pid for the next readir call */
3174f0c3b509SAl Viro 			file->f_version = (u64)tid;
31750bc58a91SEric W. Biederman 			put_task_struct(task);
31761da177e4SLinus Torvalds 			break;
31770bc58a91SEric W. Biederman 		}
31781da177e4SLinus Torvalds 	}
3179d855a4b7SOleg Nesterov 
3180f0c3b509SAl Viro 	return 0;
31811da177e4SLinus Torvalds }
31826e66b52bSEric W. Biederman 
31836e66b52bSEric W. Biederman static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
31846e66b52bSEric W. Biederman {
31856e66b52bSEric W. Biederman 	struct inode *inode = dentry->d_inode;
318699f89551SEric W. Biederman 	struct task_struct *p = get_proc_task(inode);
31876e66b52bSEric W. Biederman 	generic_fillattr(inode, stat);
31886e66b52bSEric W. Biederman 
318999f89551SEric W. Biederman 	if (p) {
319099f89551SEric W. Biederman 		stat->nlink += get_nr_threads(p);
319199f89551SEric W. Biederman 		put_task_struct(p);
31926e66b52bSEric W. Biederman 	}
31936e66b52bSEric W. Biederman 
31946e66b52bSEric W. Biederman 	return 0;
31956e66b52bSEric W. Biederman }
319628a6d671SEric W. Biederman 
3197c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations = {
319828a6d671SEric W. Biederman 	.lookup		= proc_task_lookup,
319928a6d671SEric W. Biederman 	.getattr	= proc_task_getattr,
320028a6d671SEric W. Biederman 	.setattr	= proc_setattr,
32010499680aSVasiliy Kulikov 	.permission	= proc_pid_permission,
320228a6d671SEric W. Biederman };
320328a6d671SEric W. Biederman 
320400977a59SArjan van de Ven static const struct file_operations proc_task_operations = {
320528a6d671SEric W. Biederman 	.read		= generic_read_dir,
3206f0c3b509SAl Viro 	.iterate	= proc_task_readdir,
32076038f373SArnd Bergmann 	.llseek		= default_llseek,
320828a6d671SEric W. Biederman };
3209