xref: /openbmc/linux/fs/proc/base.c (revision c35a7f18a0b237261dad57c9abd3adfa73f315e1)
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 ONE(NAME, MODE, show)				\
134be614086SEric W. Biederman 	NOD(NAME, (S_IFREG|(MODE)), 			\
135be614086SEric W. Biederman 		NULL, &proc_single_file_operations,	\
136631f9c18SAlexey Dobriyan 		{ .proc_show = show } )
1371da177e4SLinus Torvalds 
138aed54175SVegard Nossum /*
139aed54175SVegard Nossum  * Count the number of hardlinks for the pid_entry table, excluding the .
140aed54175SVegard Nossum  * and .. links.
141aed54175SVegard Nossum  */
142aed54175SVegard Nossum static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
143aed54175SVegard Nossum 	unsigned int n)
144aed54175SVegard Nossum {
145aed54175SVegard Nossum 	unsigned int i;
146aed54175SVegard Nossum 	unsigned int count;
147aed54175SVegard Nossum 
148aed54175SVegard Nossum 	count = 0;
149aed54175SVegard Nossum 	for (i = 0; i < n; ++i) {
150aed54175SVegard Nossum 		if (S_ISDIR(entries[i].mode))
151aed54175SVegard Nossum 			++count;
152aed54175SVegard Nossum 	}
153aed54175SVegard Nossum 
154aed54175SVegard Nossum 	return count;
155aed54175SVegard Nossum }
156aed54175SVegard Nossum 
157f7ad3c6bSMiklos Szeredi static int get_task_root(struct task_struct *task, struct path *root)
1581da177e4SLinus Torvalds {
1597c2c7d99SHugh Dickins 	int result = -ENOENT;
1607c2c7d99SHugh Dickins 
1610494f6ecSMiklos Szeredi 	task_lock(task);
162f7ad3c6bSMiklos Szeredi 	if (task->fs) {
163f7ad3c6bSMiklos Szeredi 		get_fs_root(task->fs, root);
1647c2c7d99SHugh Dickins 		result = 0;
1657c2c7d99SHugh Dickins 	}
1660494f6ecSMiklos Szeredi 	task_unlock(task);
1677c2c7d99SHugh Dickins 	return result;
1680494f6ecSMiklos Szeredi }
1690494f6ecSMiklos Szeredi 
1707773fbc5SCyrill Gorcunov static int proc_cwd_link(struct dentry *dentry, struct path *path)
1710494f6ecSMiklos Szeredi {
1727773fbc5SCyrill Gorcunov 	struct task_struct *task = get_proc_task(dentry->d_inode);
1730494f6ecSMiklos Szeredi 	int result = -ENOENT;
17499f89551SEric W. Biederman 
17599f89551SEric W. Biederman 	if (task) {
176f7ad3c6bSMiklos Szeredi 		task_lock(task);
177f7ad3c6bSMiklos Szeredi 		if (task->fs) {
178f7ad3c6bSMiklos Szeredi 			get_fs_pwd(task->fs, path);
179f7ad3c6bSMiklos Szeredi 			result = 0;
180f7ad3c6bSMiklos Szeredi 		}
181f7ad3c6bSMiklos Szeredi 		task_unlock(task);
18299f89551SEric W. Biederman 		put_task_struct(task);
18399f89551SEric W. Biederman 	}
1841da177e4SLinus Torvalds 	return result;
1851da177e4SLinus Torvalds }
1861da177e4SLinus Torvalds 
1877773fbc5SCyrill Gorcunov static int proc_root_link(struct dentry *dentry, struct path *path)
1881da177e4SLinus Torvalds {
1897773fbc5SCyrill Gorcunov 	struct task_struct *task = get_proc_task(dentry->d_inode);
1901da177e4SLinus Torvalds 	int result = -ENOENT;
19199f89551SEric W. Biederman 
19299f89551SEric W. Biederman 	if (task) {
193f7ad3c6bSMiklos Szeredi 		result = get_task_root(task, path);
19499f89551SEric W. Biederman 		put_task_struct(task);
19599f89551SEric W. Biederman 	}
1961da177e4SLinus Torvalds 	return result;
1971da177e4SLinus Torvalds }
1981da177e4SLinus Torvalds 
1992ca66ff7SAlexey Dobriyan static int proc_pid_cmdline(struct seq_file *m, struct pid_namespace *ns,
2002ca66ff7SAlexey Dobriyan 			    struct pid *pid, struct task_struct *task)
2011da177e4SLinus Torvalds {
2022ca66ff7SAlexey Dobriyan 	/*
2032ca66ff7SAlexey Dobriyan 	 * Rely on struct seq_operations::show() being called once
2042ca66ff7SAlexey Dobriyan 	 * per internal buffer allocation. See single_open(), traverse().
2052ca66ff7SAlexey Dobriyan 	 */
2062ca66ff7SAlexey Dobriyan 	BUG_ON(m->size < PAGE_SIZE);
2072ca66ff7SAlexey Dobriyan 	m->count += get_cmdline(task, m->buf, PAGE_SIZE);
2082ca66ff7SAlexey Dobriyan 	return 0;
2091da177e4SLinus Torvalds }
2101da177e4SLinus Torvalds 
211f9ea536eSAlexey Dobriyan static int proc_pid_auxv(struct seq_file *m, struct pid_namespace *ns,
212f9ea536eSAlexey Dobriyan 			 struct pid *pid, struct task_struct *task)
2131da177e4SLinus Torvalds {
214e7dcd999SCong Wang 	struct mm_struct *mm = mm_access(task, PTRACE_MODE_READ);
2152fadaef4SAl Viro 	if (mm && !IS_ERR(mm)) {
2161da177e4SLinus Torvalds 		unsigned int nwords = 0;
217dfe6b7d9SHannes Eder 		do {
2181da177e4SLinus Torvalds 			nwords += 2;
219dfe6b7d9SHannes Eder 		} while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
220f9ea536eSAlexey Dobriyan 		seq_write(m, mm->saved_auxv, nwords * sizeof(mm->saved_auxv[0]));
2211da177e4SLinus Torvalds 		mmput(mm);
222f9ea536eSAlexey Dobriyan 		return 0;
223f9ea536eSAlexey Dobriyan 	} else
224f9ea536eSAlexey Dobriyan 		return PTR_ERR(mm);
2251da177e4SLinus Torvalds }
2261da177e4SLinus Torvalds 
2271da177e4SLinus Torvalds 
2281da177e4SLinus Torvalds #ifdef CONFIG_KALLSYMS
2291da177e4SLinus Torvalds /*
2301da177e4SLinus Torvalds  * Provides a wchan file via kallsyms in a proper one-value-per-file format.
2311da177e4SLinus Torvalds  * Returns the resolved symbol.  If that fails, simply return the address.
2321da177e4SLinus Torvalds  */
233edfcd606SAlexey Dobriyan static int proc_pid_wchan(struct seq_file *m, struct pid_namespace *ns,
234edfcd606SAlexey Dobriyan 			  struct pid *pid, struct task_struct *task)
2351da177e4SLinus Torvalds {
236ffb45122SAlexey Dobriyan 	unsigned long wchan;
2379281aceaSTejun Heo 	char symname[KSYM_NAME_LEN];
2381da177e4SLinus Torvalds 
2391da177e4SLinus Torvalds 	wchan = get_wchan(task);
2401da177e4SLinus Torvalds 
2419d65cb4aSAlexey Dobriyan 	if (lookup_symbol_name(wchan, symname) < 0)
242f83ce3e6SJake Edge 		if (!ptrace_may_access(task, PTRACE_MODE_READ))
243f83ce3e6SJake Edge 			return 0;
244f83ce3e6SJake Edge 		else
245edfcd606SAlexey Dobriyan 			return seq_printf(m, "%lu", wchan);
2469d65cb4aSAlexey Dobriyan 	else
247edfcd606SAlexey Dobriyan 		return seq_printf(m, "%s", symname);
2481da177e4SLinus Torvalds }
2491da177e4SLinus Torvalds #endif /* CONFIG_KALLSYMS */
2501da177e4SLinus Torvalds 
251a9712bc1SAl Viro static int lock_trace(struct task_struct *task)
252a9712bc1SAl Viro {
253a9712bc1SAl Viro 	int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
254a9712bc1SAl Viro 	if (err)
255a9712bc1SAl Viro 		return err;
256a9712bc1SAl Viro 	if (!ptrace_may_access(task, PTRACE_MODE_ATTACH)) {
257a9712bc1SAl Viro 		mutex_unlock(&task->signal->cred_guard_mutex);
258a9712bc1SAl Viro 		return -EPERM;
259a9712bc1SAl Viro 	}
260a9712bc1SAl Viro 	return 0;
261a9712bc1SAl Viro }
262a9712bc1SAl Viro 
263a9712bc1SAl Viro static void unlock_trace(struct task_struct *task)
264a9712bc1SAl Viro {
265a9712bc1SAl Viro 	mutex_unlock(&task->signal->cred_guard_mutex);
266a9712bc1SAl Viro }
267a9712bc1SAl Viro 
2682ec220e2SKen Chen #ifdef CONFIG_STACKTRACE
2692ec220e2SKen Chen 
2702ec220e2SKen Chen #define MAX_STACK_TRACE_DEPTH	64
2712ec220e2SKen Chen 
2722ec220e2SKen Chen static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
2732ec220e2SKen Chen 			  struct pid *pid, struct task_struct *task)
2742ec220e2SKen Chen {
2752ec220e2SKen Chen 	struct stack_trace trace;
2762ec220e2SKen Chen 	unsigned long *entries;
277a9712bc1SAl Viro 	int err;
2782ec220e2SKen Chen 	int i;
2792ec220e2SKen Chen 
2802ec220e2SKen Chen 	entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
2812ec220e2SKen Chen 	if (!entries)
2822ec220e2SKen Chen 		return -ENOMEM;
2832ec220e2SKen Chen 
2842ec220e2SKen Chen 	trace.nr_entries	= 0;
2852ec220e2SKen Chen 	trace.max_entries	= MAX_STACK_TRACE_DEPTH;
2862ec220e2SKen Chen 	trace.entries		= entries;
2872ec220e2SKen Chen 	trace.skip		= 0;
288a9712bc1SAl Viro 
289a9712bc1SAl Viro 	err = lock_trace(task);
290a9712bc1SAl Viro 	if (!err) {
2912ec220e2SKen Chen 		save_stack_trace_tsk(task, &trace);
2922ec220e2SKen Chen 
2932ec220e2SKen Chen 		for (i = 0; i < trace.nr_entries; i++) {
29451e03149SKonstantin Khlebnikov 			seq_printf(m, "[<%pK>] %pS\n",
2952ec220e2SKen Chen 				   (void *)entries[i], (void *)entries[i]);
2962ec220e2SKen Chen 		}
297a9712bc1SAl Viro 		unlock_trace(task);
298a9712bc1SAl Viro 	}
2992ec220e2SKen Chen 	kfree(entries);
3002ec220e2SKen Chen 
301a9712bc1SAl Viro 	return err;
3022ec220e2SKen Chen }
3032ec220e2SKen Chen #endif
3042ec220e2SKen Chen 
3051da177e4SLinus Torvalds #ifdef CONFIG_SCHEDSTATS
3061da177e4SLinus Torvalds /*
3071da177e4SLinus Torvalds  * Provides /proc/PID/schedstat
3081da177e4SLinus Torvalds  */
309f6e826caSAlexey Dobriyan static int proc_pid_schedstat(struct seq_file *m, struct pid_namespace *ns,
310f6e826caSAlexey Dobriyan 			      struct pid *pid, struct task_struct *task)
3111da177e4SLinus Torvalds {
312f6e826caSAlexey Dobriyan 	return seq_printf(m, "%llu %llu %lu\n",
313826e08b0SIngo Molnar 			(unsigned long long)task->se.sum_exec_runtime,
314826e08b0SIngo Molnar 			(unsigned long long)task->sched_info.run_delay,
3152d72376bSIngo Molnar 			task->sched_info.pcount);
3161da177e4SLinus Torvalds }
3171da177e4SLinus Torvalds #endif
3181da177e4SLinus Torvalds 
3199745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP
3209745512cSArjan van de Ven static int lstats_show_proc(struct seq_file *m, void *v)
3219745512cSArjan van de Ven {
3229745512cSArjan van de Ven 	int i;
32313d77c37SHiroshi Shimamoto 	struct inode *inode = m->private;
32413d77c37SHiroshi Shimamoto 	struct task_struct *task = get_proc_task(inode);
3259745512cSArjan van de Ven 
32613d77c37SHiroshi Shimamoto 	if (!task)
32713d77c37SHiroshi Shimamoto 		return -ESRCH;
32813d77c37SHiroshi Shimamoto 	seq_puts(m, "Latency Top version : v0.1\n");
3299745512cSArjan van de Ven 	for (i = 0; i < 32; i++) {
33034e49d4fSJoe Perches 		struct latency_record *lr = &task->latency_record[i];
33134e49d4fSJoe Perches 		if (lr->backtrace[0]) {
3329745512cSArjan van de Ven 			int q;
3339745512cSArjan van de Ven 			seq_printf(m, "%i %li %li",
33434e49d4fSJoe Perches 				   lr->count, lr->time, lr->max);
3359745512cSArjan van de Ven 			for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
33634e49d4fSJoe Perches 				unsigned long bt = lr->backtrace[q];
33734e49d4fSJoe Perches 				if (!bt)
3389745512cSArjan van de Ven 					break;
33934e49d4fSJoe Perches 				if (bt == ULONG_MAX)
3409745512cSArjan van de Ven 					break;
34134e49d4fSJoe Perches 				seq_printf(m, " %ps", (void *)bt);
3429745512cSArjan van de Ven 			}
3439d6de12fSAlexey Dobriyan 			seq_putc(m, '\n');
3449745512cSArjan van de Ven 		}
3459745512cSArjan van de Ven 
3469745512cSArjan van de Ven 	}
34713d77c37SHiroshi Shimamoto 	put_task_struct(task);
3489745512cSArjan van de Ven 	return 0;
3499745512cSArjan van de Ven }
3509745512cSArjan van de Ven 
3519745512cSArjan van de Ven static int lstats_open(struct inode *inode, struct file *file)
3529745512cSArjan van de Ven {
35313d77c37SHiroshi Shimamoto 	return single_open(file, lstats_show_proc, inode);
354d6643d12SHiroshi Shimamoto }
355d6643d12SHiroshi Shimamoto 
3569745512cSArjan van de Ven static ssize_t lstats_write(struct file *file, const char __user *buf,
3579745512cSArjan van de Ven 			    size_t count, loff_t *offs)
3589745512cSArjan van de Ven {
359496ad9aaSAl Viro 	struct task_struct *task = get_proc_task(file_inode(file));
3609745512cSArjan van de Ven 
36113d77c37SHiroshi Shimamoto 	if (!task)
36213d77c37SHiroshi Shimamoto 		return -ESRCH;
3639745512cSArjan van de Ven 	clear_all_latency_tracing(task);
36413d77c37SHiroshi Shimamoto 	put_task_struct(task);
3659745512cSArjan van de Ven 
3669745512cSArjan van de Ven 	return count;
3679745512cSArjan van de Ven }
3689745512cSArjan van de Ven 
3699745512cSArjan van de Ven static const struct file_operations proc_lstats_operations = {
3709745512cSArjan van de Ven 	.open		= lstats_open,
3719745512cSArjan van de Ven 	.read		= seq_read,
3729745512cSArjan van de Ven 	.write		= lstats_write,
3739745512cSArjan van de Ven 	.llseek		= seq_lseek,
37413d77c37SHiroshi Shimamoto 	.release	= single_release,
3759745512cSArjan van de Ven };
3769745512cSArjan van de Ven 
3779745512cSArjan van de Ven #endif
3789745512cSArjan van de Ven 
3796ba51e37SAlexey Dobriyan static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns,
3806ba51e37SAlexey Dobriyan 			  struct pid *pid, struct task_struct *task)
3811da177e4SLinus Torvalds {
382a7f638f9SDavid Rientjes 	unsigned long totalpages = totalram_pages + total_swap_pages;
383b95c35e7SOleg Nesterov 	unsigned long points = 0;
3841da177e4SLinus Torvalds 
38519c5d45aSAlexey Dobriyan 	read_lock(&tasklist_lock);
386b95c35e7SOleg Nesterov 	if (pid_alive(task))
387a7f638f9SDavid Rientjes 		points = oom_badness(task, NULL, NULL, totalpages) *
388a7f638f9SDavid Rientjes 						1000 / totalpages;
38919c5d45aSAlexey Dobriyan 	read_unlock(&tasklist_lock);
3906ba51e37SAlexey Dobriyan 	return seq_printf(m, "%lu\n", points);
3911da177e4SLinus Torvalds }
3921da177e4SLinus Torvalds 
393d85f50d5SNeil Horman struct limit_names {
394cedbccabSAlexey Dobriyan 	const char *name;
395cedbccabSAlexey Dobriyan 	const char *unit;
396d85f50d5SNeil Horman };
397d85f50d5SNeil Horman 
398d85f50d5SNeil Horman static const struct limit_names lnames[RLIM_NLIMITS] = {
399cff4edb5SKees Cook 	[RLIMIT_CPU] = {"Max cpu time", "seconds"},
400d85f50d5SNeil Horman 	[RLIMIT_FSIZE] = {"Max file size", "bytes"},
401d85f50d5SNeil Horman 	[RLIMIT_DATA] = {"Max data size", "bytes"},
402d85f50d5SNeil Horman 	[RLIMIT_STACK] = {"Max stack size", "bytes"},
403d85f50d5SNeil Horman 	[RLIMIT_CORE] = {"Max core file size", "bytes"},
404d85f50d5SNeil Horman 	[RLIMIT_RSS] = {"Max resident set", "bytes"},
405d85f50d5SNeil Horman 	[RLIMIT_NPROC] = {"Max processes", "processes"},
406d85f50d5SNeil Horman 	[RLIMIT_NOFILE] = {"Max open files", "files"},
407d85f50d5SNeil Horman 	[RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
408d85f50d5SNeil Horman 	[RLIMIT_AS] = {"Max address space", "bytes"},
409d85f50d5SNeil Horman 	[RLIMIT_LOCKS] = {"Max file locks", "locks"},
410d85f50d5SNeil Horman 	[RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
411d85f50d5SNeil Horman 	[RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
412d85f50d5SNeil Horman 	[RLIMIT_NICE] = {"Max nice priority", NULL},
413d85f50d5SNeil Horman 	[RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
4148808117cSEugene Teo 	[RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
415d85f50d5SNeil Horman };
416d85f50d5SNeil Horman 
417d85f50d5SNeil Horman /* Display limits for a process */
4181c963eb1SAlexey Dobriyan static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
4191c963eb1SAlexey Dobriyan 			   struct pid *pid, struct task_struct *task)
420d85f50d5SNeil Horman {
421d85f50d5SNeil Horman 	unsigned int i;
422d85f50d5SNeil Horman 	unsigned long flags;
423d85f50d5SNeil Horman 
424d85f50d5SNeil Horman 	struct rlimit rlim[RLIM_NLIMITS];
425d85f50d5SNeil Horman 
426a6bebbc8SLai Jiangshan 	if (!lock_task_sighand(task, &flags))
427d85f50d5SNeil Horman 		return 0;
428d85f50d5SNeil Horman 	memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
429d85f50d5SNeil Horman 	unlock_task_sighand(task, &flags);
430d85f50d5SNeil Horman 
431d85f50d5SNeil Horman 	/*
432d85f50d5SNeil Horman 	 * print the file header
433d85f50d5SNeil Horman 	 */
4341c963eb1SAlexey Dobriyan        seq_printf(m, "%-25s %-20s %-20s %-10s\n",
435d85f50d5SNeil Horman 			"Limit", "Soft Limit", "Hard Limit", "Units");
436d85f50d5SNeil Horman 
437d85f50d5SNeil Horman 	for (i = 0; i < RLIM_NLIMITS; i++) {
438d85f50d5SNeil Horman 		if (rlim[i].rlim_cur == RLIM_INFINITY)
4391c963eb1SAlexey Dobriyan 			seq_printf(m, "%-25s %-20s ",
440d85f50d5SNeil Horman 					 lnames[i].name, "unlimited");
441d85f50d5SNeil Horman 		else
4421c963eb1SAlexey Dobriyan 			seq_printf(m, "%-25s %-20lu ",
443d85f50d5SNeil Horman 					 lnames[i].name, rlim[i].rlim_cur);
444d85f50d5SNeil Horman 
445d85f50d5SNeil Horman 		if (rlim[i].rlim_max == RLIM_INFINITY)
4461c963eb1SAlexey Dobriyan 			seq_printf(m, "%-20s ", "unlimited");
447d85f50d5SNeil Horman 		else
4481c963eb1SAlexey Dobriyan 			seq_printf(m, "%-20lu ", rlim[i].rlim_max);
449d85f50d5SNeil Horman 
450d85f50d5SNeil Horman 		if (lnames[i].unit)
4511c963eb1SAlexey Dobriyan 			seq_printf(m, "%-10s\n", lnames[i].unit);
452d85f50d5SNeil Horman 		else
4531c963eb1SAlexey Dobriyan 			seq_putc(m, '\n');
454d85f50d5SNeil Horman 	}
455d85f50d5SNeil Horman 
4561c963eb1SAlexey Dobriyan 	return 0;
457d85f50d5SNeil Horman }
458d85f50d5SNeil Horman 
459ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
46009d93bd6SAlexey Dobriyan static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
46109d93bd6SAlexey Dobriyan 			    struct pid *pid, struct task_struct *task)
462ebcb6734SRoland McGrath {
463ebcb6734SRoland McGrath 	long nr;
464ebcb6734SRoland McGrath 	unsigned long args[6], sp, pc;
465a9712bc1SAl Viro 	int res = lock_trace(task);
466a9712bc1SAl Viro 	if (res)
467a9712bc1SAl Viro 		return res;
468ebcb6734SRoland McGrath 
469ebcb6734SRoland McGrath 	if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
47009d93bd6SAlexey Dobriyan 		seq_puts(m, "running\n");
471a9712bc1SAl Viro 	else if (nr < 0)
47209d93bd6SAlexey Dobriyan 		seq_printf(m, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
473a9712bc1SAl Viro 	else
47409d93bd6SAlexey Dobriyan 		seq_printf(m,
475ebcb6734SRoland McGrath 		       "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
476ebcb6734SRoland McGrath 		       nr,
477ebcb6734SRoland McGrath 		       args[0], args[1], args[2], args[3], args[4], args[5],
478ebcb6734SRoland McGrath 		       sp, pc);
479a9712bc1SAl Viro 	unlock_trace(task);
480a9712bc1SAl Viro 	return res;
481ebcb6734SRoland McGrath }
482ebcb6734SRoland McGrath #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
483ebcb6734SRoland McGrath 
4841da177e4SLinus Torvalds /************************************************************************/
4851da177e4SLinus Torvalds /*                       Here the fs part begins                        */
4861da177e4SLinus Torvalds /************************************************************************/
4871da177e4SLinus Torvalds 
4881da177e4SLinus Torvalds /* permission checks */
489778c1144SEric W. Biederman static int proc_fd_access_allowed(struct inode *inode)
4901da177e4SLinus Torvalds {
491778c1144SEric W. Biederman 	struct task_struct *task;
492778c1144SEric W. Biederman 	int allowed = 0;
493df26c40eSEric W. Biederman 	/* Allow access to a task's file descriptors if it is us or we
494df26c40eSEric W. Biederman 	 * may use ptrace attach to the process and find out that
495df26c40eSEric W. Biederman 	 * information.
496778c1144SEric W. Biederman 	 */
497778c1144SEric W. Biederman 	task = get_proc_task(inode);
498df26c40eSEric W. Biederman 	if (task) {
499006ebb40SStephen Smalley 		allowed = ptrace_may_access(task, PTRACE_MODE_READ);
500778c1144SEric W. Biederman 		put_task_struct(task);
501df26c40eSEric W. Biederman 	}
502778c1144SEric W. Biederman 	return allowed;
5031da177e4SLinus Torvalds }
5041da177e4SLinus Torvalds 
5056b4e306aSEric W. Biederman int proc_setattr(struct dentry *dentry, struct iattr *attr)
5066d76fa58SLinus Torvalds {
5076d76fa58SLinus Torvalds 	int error;
5086d76fa58SLinus Torvalds 	struct inode *inode = dentry->d_inode;
5096d76fa58SLinus Torvalds 
5106d76fa58SLinus Torvalds 	if (attr->ia_valid & ATTR_MODE)
5116d76fa58SLinus Torvalds 		return -EPERM;
5126d76fa58SLinus Torvalds 
5136d76fa58SLinus Torvalds 	error = inode_change_ok(inode, attr);
5141025774cSChristoph Hellwig 	if (error)
5156d76fa58SLinus Torvalds 		return error;
5161025774cSChristoph Hellwig 
5171025774cSChristoph Hellwig 	setattr_copy(inode, attr);
5181025774cSChristoph Hellwig 	mark_inode_dirty(inode);
5191025774cSChristoph Hellwig 	return 0;
5206d76fa58SLinus Torvalds }
5216d76fa58SLinus Torvalds 
5220499680aSVasiliy Kulikov /*
5230499680aSVasiliy Kulikov  * May current process learn task's sched/cmdline info (for hide_pid_min=1)
5240499680aSVasiliy Kulikov  * or euid/egid (for hide_pid_min=2)?
5250499680aSVasiliy Kulikov  */
5260499680aSVasiliy Kulikov static bool has_pid_permissions(struct pid_namespace *pid,
5270499680aSVasiliy Kulikov 				 struct task_struct *task,
5280499680aSVasiliy Kulikov 				 int hide_pid_min)
5290499680aSVasiliy Kulikov {
5300499680aSVasiliy Kulikov 	if (pid->hide_pid < hide_pid_min)
5310499680aSVasiliy Kulikov 		return true;
5320499680aSVasiliy Kulikov 	if (in_group_p(pid->pid_gid))
5330499680aSVasiliy Kulikov 		return true;
5340499680aSVasiliy Kulikov 	return ptrace_may_access(task, PTRACE_MODE_READ);
5350499680aSVasiliy Kulikov }
5360499680aSVasiliy Kulikov 
5370499680aSVasiliy Kulikov 
5380499680aSVasiliy Kulikov static int proc_pid_permission(struct inode *inode, int mask)
5390499680aSVasiliy Kulikov {
5400499680aSVasiliy Kulikov 	struct pid_namespace *pid = inode->i_sb->s_fs_info;
5410499680aSVasiliy Kulikov 	struct task_struct *task;
5420499680aSVasiliy Kulikov 	bool has_perms;
5430499680aSVasiliy Kulikov 
5440499680aSVasiliy Kulikov 	task = get_proc_task(inode);
545a2ef990aSXiaotian Feng 	if (!task)
546a2ef990aSXiaotian Feng 		return -ESRCH;
5470499680aSVasiliy Kulikov 	has_perms = has_pid_permissions(pid, task, 1);
5480499680aSVasiliy Kulikov 	put_task_struct(task);
5490499680aSVasiliy Kulikov 
5500499680aSVasiliy Kulikov 	if (!has_perms) {
5510499680aSVasiliy Kulikov 		if (pid->hide_pid == 2) {
5520499680aSVasiliy Kulikov 			/*
5530499680aSVasiliy Kulikov 			 * Let's make getdents(), stat(), and open()
5540499680aSVasiliy Kulikov 			 * consistent with each other.  If a process
5550499680aSVasiliy Kulikov 			 * may not stat() a file, it shouldn't be seen
5560499680aSVasiliy Kulikov 			 * in procfs at all.
5570499680aSVasiliy Kulikov 			 */
5580499680aSVasiliy Kulikov 			return -ENOENT;
5590499680aSVasiliy Kulikov 		}
5600499680aSVasiliy Kulikov 
5610499680aSVasiliy Kulikov 		return -EPERM;
5620499680aSVasiliy Kulikov 	}
5630499680aSVasiliy Kulikov 	return generic_permission(inode, mask);
5640499680aSVasiliy Kulikov }
5650499680aSVasiliy Kulikov 
5660499680aSVasiliy Kulikov 
5670499680aSVasiliy Kulikov 
568c5ef1c42SArjan van de Ven static const struct inode_operations proc_def_inode_operations = {
5696d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
5706d76fa58SLinus Torvalds };
5716d76fa58SLinus Torvalds 
572be614086SEric W. Biederman static int proc_single_show(struct seq_file *m, void *v)
573be614086SEric W. Biederman {
574be614086SEric W. Biederman 	struct inode *inode = m->private;
575be614086SEric W. Biederman 	struct pid_namespace *ns;
576be614086SEric W. Biederman 	struct pid *pid;
577be614086SEric W. Biederman 	struct task_struct *task;
578be614086SEric W. Biederman 	int ret;
579be614086SEric W. Biederman 
580be614086SEric W. Biederman 	ns = inode->i_sb->s_fs_info;
581be614086SEric W. Biederman 	pid = proc_pid(inode);
582be614086SEric W. Biederman 	task = get_pid_task(pid, PIDTYPE_PID);
583be614086SEric W. Biederman 	if (!task)
584be614086SEric W. Biederman 		return -ESRCH;
585be614086SEric W. Biederman 
586be614086SEric W. Biederman 	ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
587be614086SEric W. Biederman 
588be614086SEric W. Biederman 	put_task_struct(task);
589be614086SEric W. Biederman 	return ret;
590be614086SEric W. Biederman }
591be614086SEric W. Biederman 
592be614086SEric W. Biederman static int proc_single_open(struct inode *inode, struct file *filp)
593be614086SEric W. Biederman {
594c6a34058SJovi Zhang 	return single_open(filp, proc_single_show, inode);
595be614086SEric W. Biederman }
596be614086SEric W. Biederman 
597be614086SEric W. Biederman static const struct file_operations proc_single_file_operations = {
598be614086SEric W. Biederman 	.open		= proc_single_open,
599be614086SEric W. Biederman 	.read		= seq_read,
600be614086SEric W. Biederman 	.llseek		= seq_lseek,
601be614086SEric W. Biederman 	.release	= single_release,
602be614086SEric W. Biederman };
603be614086SEric W. Biederman 
6045381e169SOleg Nesterov 
6055381e169SOleg Nesterov struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode)
6061da177e4SLinus Torvalds {
6075381e169SOleg Nesterov 	struct task_struct *task = get_proc_task(inode);
6085381e169SOleg Nesterov 	struct mm_struct *mm = ERR_PTR(-ESRCH);
609e268337dSLinus Torvalds 
6105381e169SOleg Nesterov 	if (task) {
611b409e578SCong Wang 		mm = mm_access(task, mode);
612e268337dSLinus Torvalds 		put_task_struct(task);
613e268337dSLinus Torvalds 
6145381e169SOleg Nesterov 		if (!IS_ERR_OR_NULL(mm)) {
6156d08f2c7SOleg Nesterov 			/* ensure this mm_struct can't be freed */
6166d08f2c7SOleg Nesterov 			atomic_inc(&mm->mm_count);
6176d08f2c7SOleg Nesterov 			/* but do not pin its memory */
6186d08f2c7SOleg Nesterov 			mmput(mm);
6196d08f2c7SOleg Nesterov 		}
6205381e169SOleg Nesterov 	}
6215381e169SOleg Nesterov 
6225381e169SOleg Nesterov 	return mm;
6235381e169SOleg Nesterov }
6245381e169SOleg Nesterov 
6255381e169SOleg Nesterov static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
6265381e169SOleg Nesterov {
6275381e169SOleg Nesterov 	struct mm_struct *mm = proc_mem_open(inode, mode);
6285381e169SOleg Nesterov 
6295381e169SOleg Nesterov 	if (IS_ERR(mm))
6305381e169SOleg Nesterov 		return PTR_ERR(mm);
6316d08f2c7SOleg Nesterov 
632e268337dSLinus Torvalds 	file->private_data = mm;
6331da177e4SLinus Torvalds 	return 0;
6341da177e4SLinus Torvalds }
6351da177e4SLinus Torvalds 
636b409e578SCong Wang static int mem_open(struct inode *inode, struct file *file)
637b409e578SCong Wang {
638bc452b4bSDjalal Harouni 	int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
639bc452b4bSDjalal Harouni 
640bc452b4bSDjalal Harouni 	/* OK to pass negative loff_t, we can catch out-of-range */
641bc452b4bSDjalal Harouni 	file->f_mode |= FMODE_UNSIGNED_OFFSET;
642bc452b4bSDjalal Harouni 
643bc452b4bSDjalal Harouni 	return ret;
644b409e578SCong Wang }
645b409e578SCong Wang 
646572d34b9SOleg Nesterov static ssize_t mem_rw(struct file *file, char __user *buf,
647572d34b9SOleg Nesterov 			size_t count, loff_t *ppos, int write)
6481da177e4SLinus Torvalds {
649e268337dSLinus Torvalds 	struct mm_struct *mm = file->private_data;
650572d34b9SOleg Nesterov 	unsigned long addr = *ppos;
651572d34b9SOleg Nesterov 	ssize_t copied;
6521da177e4SLinus Torvalds 	char *page;
6531da177e4SLinus Torvalds 
654e268337dSLinus Torvalds 	if (!mm)
655e268337dSLinus Torvalds 		return 0;
6561da177e4SLinus Torvalds 
657e12ba74dSMel Gorman 	page = (char *)__get_free_page(GFP_TEMPORARY);
6581da177e4SLinus Torvalds 	if (!page)
659e268337dSLinus Torvalds 		return -ENOMEM;
6601da177e4SLinus Torvalds 
661f7ca54f4SFrederik Deweerdt 	copied = 0;
6626d08f2c7SOleg Nesterov 	if (!atomic_inc_not_zero(&mm->mm_users))
6636d08f2c7SOleg Nesterov 		goto free;
6646d08f2c7SOleg Nesterov 
6651da177e4SLinus Torvalds 	while (count > 0) {
666572d34b9SOleg Nesterov 		int this_len = min_t(int, count, PAGE_SIZE);
6671da177e4SLinus Torvalds 
668572d34b9SOleg Nesterov 		if (write && copy_from_user(page, buf, this_len)) {
6691da177e4SLinus Torvalds 			copied = -EFAULT;
6701da177e4SLinus Torvalds 			break;
6711da177e4SLinus Torvalds 		}
672572d34b9SOleg Nesterov 
673572d34b9SOleg Nesterov 		this_len = access_remote_vm(mm, addr, page, this_len, write);
674572d34b9SOleg Nesterov 		if (!this_len) {
6751da177e4SLinus Torvalds 			if (!copied)
6761da177e4SLinus Torvalds 				copied = -EIO;
6771da177e4SLinus Torvalds 			break;
6781da177e4SLinus Torvalds 		}
679572d34b9SOleg Nesterov 
680572d34b9SOleg Nesterov 		if (!write && copy_to_user(buf, page, this_len)) {
681572d34b9SOleg Nesterov 			copied = -EFAULT;
682572d34b9SOleg Nesterov 			break;
6831da177e4SLinus Torvalds 		}
684572d34b9SOleg Nesterov 
685572d34b9SOleg Nesterov 		buf += this_len;
686572d34b9SOleg Nesterov 		addr += this_len;
687572d34b9SOleg Nesterov 		copied += this_len;
688572d34b9SOleg Nesterov 		count -= this_len;
689572d34b9SOleg Nesterov 	}
690572d34b9SOleg Nesterov 	*ppos = addr;
69130cd8903SKOSAKI Motohiro 
6926d08f2c7SOleg Nesterov 	mmput(mm);
6936d08f2c7SOleg Nesterov free:
69430cd8903SKOSAKI Motohiro 	free_page((unsigned long) page);
6951da177e4SLinus Torvalds 	return copied;
6961da177e4SLinus Torvalds }
6971da177e4SLinus Torvalds 
698572d34b9SOleg Nesterov static ssize_t mem_read(struct file *file, char __user *buf,
699572d34b9SOleg Nesterov 			size_t count, loff_t *ppos)
700572d34b9SOleg Nesterov {
701572d34b9SOleg Nesterov 	return mem_rw(file, buf, count, ppos, 0);
702572d34b9SOleg Nesterov }
703572d34b9SOleg Nesterov 
704572d34b9SOleg Nesterov static ssize_t mem_write(struct file *file, const char __user *buf,
705572d34b9SOleg Nesterov 			 size_t count, loff_t *ppos)
706572d34b9SOleg Nesterov {
707572d34b9SOleg Nesterov 	return mem_rw(file, (char __user*)buf, count, ppos, 1);
708572d34b9SOleg Nesterov }
709572d34b9SOleg Nesterov 
71085863e47SMatt Mackall loff_t mem_lseek(struct file *file, loff_t offset, int orig)
7111da177e4SLinus Torvalds {
7121da177e4SLinus Torvalds 	switch (orig) {
7131da177e4SLinus Torvalds 	case 0:
7141da177e4SLinus Torvalds 		file->f_pos = offset;
7151da177e4SLinus Torvalds 		break;
7161da177e4SLinus Torvalds 	case 1:
7171da177e4SLinus Torvalds 		file->f_pos += offset;
7181da177e4SLinus Torvalds 		break;
7191da177e4SLinus Torvalds 	default:
7201da177e4SLinus Torvalds 		return -EINVAL;
7211da177e4SLinus Torvalds 	}
7221da177e4SLinus Torvalds 	force_successful_syscall_return();
7231da177e4SLinus Torvalds 	return file->f_pos;
7241da177e4SLinus Torvalds }
7251da177e4SLinus Torvalds 
726e268337dSLinus Torvalds static int mem_release(struct inode *inode, struct file *file)
727e268337dSLinus Torvalds {
728e268337dSLinus Torvalds 	struct mm_struct *mm = file->private_data;
72971879d3cSOleg Nesterov 	if (mm)
7306d08f2c7SOleg Nesterov 		mmdrop(mm);
731e268337dSLinus Torvalds 	return 0;
732e268337dSLinus Torvalds }
733e268337dSLinus Torvalds 
73400977a59SArjan van de Ven static const struct file_operations proc_mem_operations = {
7351da177e4SLinus Torvalds 	.llseek		= mem_lseek,
7361da177e4SLinus Torvalds 	.read		= mem_read,
7371da177e4SLinus Torvalds 	.write		= mem_write,
7381da177e4SLinus Torvalds 	.open		= mem_open,
739e268337dSLinus Torvalds 	.release	= mem_release,
7401da177e4SLinus Torvalds };
7411da177e4SLinus Torvalds 
742b409e578SCong Wang static int environ_open(struct inode *inode, struct file *file)
743b409e578SCong Wang {
744b409e578SCong Wang 	return __mem_open(inode, file, PTRACE_MODE_READ);
745b409e578SCong Wang }
746b409e578SCong Wang 
747315e28c8SJames Pearson static ssize_t environ_read(struct file *file, char __user *buf,
748315e28c8SJames Pearson 			size_t count, loff_t *ppos)
749315e28c8SJames Pearson {
750315e28c8SJames Pearson 	char *page;
751315e28c8SJames Pearson 	unsigned long src = *ppos;
752b409e578SCong Wang 	int ret = 0;
753b409e578SCong Wang 	struct mm_struct *mm = file->private_data;
754315e28c8SJames Pearson 
755b409e578SCong Wang 	if (!mm)
756b409e578SCong Wang 		return 0;
757315e28c8SJames Pearson 
758315e28c8SJames Pearson 	page = (char *)__get_free_page(GFP_TEMPORARY);
759315e28c8SJames Pearson 	if (!page)
760b409e578SCong Wang 		return -ENOMEM;
761315e28c8SJames Pearson 
762d6f64b89SAl Viro 	ret = 0;
763b409e578SCong Wang 	if (!atomic_inc_not_zero(&mm->mm_users))
764b409e578SCong Wang 		goto free;
765315e28c8SJames Pearson 	while (count > 0) {
766e8905ec2SDjalal Harouni 		size_t this_len, max_len;
767e8905ec2SDjalal Harouni 		int retval;
768e8905ec2SDjalal Harouni 
769e8905ec2SDjalal Harouni 		if (src >= (mm->env_end - mm->env_start))
770e8905ec2SDjalal Harouni 			break;
771315e28c8SJames Pearson 
772315e28c8SJames Pearson 		this_len = mm->env_end - (mm->env_start + src);
773315e28c8SJames Pearson 
774e8905ec2SDjalal Harouni 		max_len = min_t(size_t, PAGE_SIZE, count);
775e8905ec2SDjalal Harouni 		this_len = min(max_len, this_len);
776315e28c8SJames Pearson 
777b409e578SCong Wang 		retval = access_remote_vm(mm, (mm->env_start + src),
778315e28c8SJames Pearson 			page, this_len, 0);
779315e28c8SJames Pearson 
780315e28c8SJames Pearson 		if (retval <= 0) {
781315e28c8SJames Pearson 			ret = retval;
782315e28c8SJames Pearson 			break;
783315e28c8SJames Pearson 		}
784315e28c8SJames Pearson 
785315e28c8SJames Pearson 		if (copy_to_user(buf, page, retval)) {
786315e28c8SJames Pearson 			ret = -EFAULT;
787315e28c8SJames Pearson 			break;
788315e28c8SJames Pearson 		}
789315e28c8SJames Pearson 
790315e28c8SJames Pearson 		ret += retval;
791315e28c8SJames Pearson 		src += retval;
792315e28c8SJames Pearson 		buf += retval;
793315e28c8SJames Pearson 		count -= retval;
794315e28c8SJames Pearson 	}
795315e28c8SJames Pearson 	*ppos = src;
796315e28c8SJames Pearson 	mmput(mm);
797b409e578SCong Wang 
798b409e578SCong Wang free:
799315e28c8SJames Pearson 	free_page((unsigned long) page);
800315e28c8SJames Pearson 	return ret;
801315e28c8SJames Pearson }
802315e28c8SJames Pearson 
803315e28c8SJames Pearson static const struct file_operations proc_environ_operations = {
804b409e578SCong Wang 	.open		= environ_open,
805315e28c8SJames Pearson 	.read		= environ_read,
80687df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
807b409e578SCong Wang 	.release	= mem_release,
808315e28c8SJames Pearson };
809315e28c8SJames Pearson 
810fa0cbbf1SDavid Rientjes static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
811fa0cbbf1SDavid Rientjes 			    loff_t *ppos)
812fa0cbbf1SDavid Rientjes {
813496ad9aaSAl Viro 	struct task_struct *task = get_proc_task(file_inode(file));
814fa0cbbf1SDavid Rientjes 	char buffer[PROC_NUMBUF];
815fa0cbbf1SDavid Rientjes 	int oom_adj = OOM_ADJUST_MIN;
816fa0cbbf1SDavid Rientjes 	size_t len;
817fa0cbbf1SDavid Rientjes 	unsigned long flags;
818fa0cbbf1SDavid Rientjes 
819fa0cbbf1SDavid Rientjes 	if (!task)
820fa0cbbf1SDavid Rientjes 		return -ESRCH;
821fa0cbbf1SDavid Rientjes 	if (lock_task_sighand(task, &flags)) {
822fa0cbbf1SDavid Rientjes 		if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
823fa0cbbf1SDavid Rientjes 			oom_adj = OOM_ADJUST_MAX;
824fa0cbbf1SDavid Rientjes 		else
825fa0cbbf1SDavid Rientjes 			oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
826fa0cbbf1SDavid Rientjes 				  OOM_SCORE_ADJ_MAX;
827fa0cbbf1SDavid Rientjes 		unlock_task_sighand(task, &flags);
828fa0cbbf1SDavid Rientjes 	}
829fa0cbbf1SDavid Rientjes 	put_task_struct(task);
830fa0cbbf1SDavid Rientjes 	len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
831fa0cbbf1SDavid Rientjes 	return simple_read_from_buffer(buf, count, ppos, buffer, len);
832fa0cbbf1SDavid Rientjes }
833fa0cbbf1SDavid Rientjes 
834fa0cbbf1SDavid Rientjes static ssize_t oom_adj_write(struct file *file, const char __user *buf,
835fa0cbbf1SDavid Rientjes 			     size_t count, loff_t *ppos)
836fa0cbbf1SDavid Rientjes {
837fa0cbbf1SDavid Rientjes 	struct task_struct *task;
838fa0cbbf1SDavid Rientjes 	char buffer[PROC_NUMBUF];
839fa0cbbf1SDavid Rientjes 	int oom_adj;
840fa0cbbf1SDavid Rientjes 	unsigned long flags;
841fa0cbbf1SDavid Rientjes 	int err;
842fa0cbbf1SDavid Rientjes 
843fa0cbbf1SDavid Rientjes 	memset(buffer, 0, sizeof(buffer));
844fa0cbbf1SDavid Rientjes 	if (count > sizeof(buffer) - 1)
845fa0cbbf1SDavid Rientjes 		count = sizeof(buffer) - 1;
846fa0cbbf1SDavid Rientjes 	if (copy_from_user(buffer, buf, count)) {
847fa0cbbf1SDavid Rientjes 		err = -EFAULT;
848fa0cbbf1SDavid Rientjes 		goto out;
849fa0cbbf1SDavid Rientjes 	}
850fa0cbbf1SDavid Rientjes 
851fa0cbbf1SDavid Rientjes 	err = kstrtoint(strstrip(buffer), 0, &oom_adj);
852fa0cbbf1SDavid Rientjes 	if (err)
853fa0cbbf1SDavid Rientjes 		goto out;
854fa0cbbf1SDavid Rientjes 	if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
855fa0cbbf1SDavid Rientjes 	     oom_adj != OOM_DISABLE) {
856fa0cbbf1SDavid Rientjes 		err = -EINVAL;
857fa0cbbf1SDavid Rientjes 		goto out;
858fa0cbbf1SDavid Rientjes 	}
859fa0cbbf1SDavid Rientjes 
860496ad9aaSAl Viro 	task = get_proc_task(file_inode(file));
861fa0cbbf1SDavid Rientjes 	if (!task) {
862fa0cbbf1SDavid Rientjes 		err = -ESRCH;
863fa0cbbf1SDavid Rientjes 		goto out;
864fa0cbbf1SDavid Rientjes 	}
865fa0cbbf1SDavid Rientjes 
866fa0cbbf1SDavid Rientjes 	task_lock(task);
867fa0cbbf1SDavid Rientjes 	if (!task->mm) {
868fa0cbbf1SDavid Rientjes 		err = -EINVAL;
869fa0cbbf1SDavid Rientjes 		goto err_task_lock;
870fa0cbbf1SDavid Rientjes 	}
871fa0cbbf1SDavid Rientjes 
872fa0cbbf1SDavid Rientjes 	if (!lock_task_sighand(task, &flags)) {
873fa0cbbf1SDavid Rientjes 		err = -ESRCH;
874fa0cbbf1SDavid Rientjes 		goto err_task_lock;
875fa0cbbf1SDavid Rientjes 	}
876fa0cbbf1SDavid Rientjes 
877fa0cbbf1SDavid Rientjes 	/*
878fa0cbbf1SDavid Rientjes 	 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
879fa0cbbf1SDavid Rientjes 	 * value is always attainable.
880fa0cbbf1SDavid Rientjes 	 */
881fa0cbbf1SDavid Rientjes 	if (oom_adj == OOM_ADJUST_MAX)
882fa0cbbf1SDavid Rientjes 		oom_adj = OOM_SCORE_ADJ_MAX;
883fa0cbbf1SDavid Rientjes 	else
884fa0cbbf1SDavid Rientjes 		oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
885fa0cbbf1SDavid Rientjes 
886fa0cbbf1SDavid Rientjes 	if (oom_adj < task->signal->oom_score_adj &&
887fa0cbbf1SDavid Rientjes 	    !capable(CAP_SYS_RESOURCE)) {
888fa0cbbf1SDavid Rientjes 		err = -EACCES;
889fa0cbbf1SDavid Rientjes 		goto err_sighand;
890fa0cbbf1SDavid Rientjes 	}
891fa0cbbf1SDavid Rientjes 
892fa0cbbf1SDavid Rientjes 	/*
893fa0cbbf1SDavid Rientjes 	 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
894fa0cbbf1SDavid Rientjes 	 * /proc/pid/oom_score_adj instead.
895fa0cbbf1SDavid Rientjes 	 */
89687ebdc00SAndrew Morton 	pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
897fa0cbbf1SDavid Rientjes 		  current->comm, task_pid_nr(current), task_pid_nr(task),
898fa0cbbf1SDavid Rientjes 		  task_pid_nr(task));
899fa0cbbf1SDavid Rientjes 
900fa0cbbf1SDavid Rientjes 	task->signal->oom_score_adj = oom_adj;
901fa0cbbf1SDavid Rientjes 	trace_oom_score_adj_update(task);
902fa0cbbf1SDavid Rientjes err_sighand:
903fa0cbbf1SDavid Rientjes 	unlock_task_sighand(task, &flags);
904fa0cbbf1SDavid Rientjes err_task_lock:
905fa0cbbf1SDavid Rientjes 	task_unlock(task);
906fa0cbbf1SDavid Rientjes 	put_task_struct(task);
907fa0cbbf1SDavid Rientjes out:
908fa0cbbf1SDavid Rientjes 	return err < 0 ? err : count;
909fa0cbbf1SDavid Rientjes }
910fa0cbbf1SDavid Rientjes 
911fa0cbbf1SDavid Rientjes static const struct file_operations proc_oom_adj_operations = {
912fa0cbbf1SDavid Rientjes 	.read		= oom_adj_read,
913fa0cbbf1SDavid Rientjes 	.write		= oom_adj_write,
914fa0cbbf1SDavid Rientjes 	.llseek		= generic_file_llseek,
915fa0cbbf1SDavid Rientjes };
916fa0cbbf1SDavid Rientjes 
917a63d83f4SDavid Rientjes static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
918a63d83f4SDavid Rientjes 					size_t count, loff_t *ppos)
919a63d83f4SDavid Rientjes {
920496ad9aaSAl Viro 	struct task_struct *task = get_proc_task(file_inode(file));
921a63d83f4SDavid Rientjes 	char buffer[PROC_NUMBUF];
922a9c58b90SDavid Rientjes 	short oom_score_adj = OOM_SCORE_ADJ_MIN;
923a63d83f4SDavid Rientjes 	unsigned long flags;
924a63d83f4SDavid Rientjes 	size_t len;
925a63d83f4SDavid Rientjes 
926a63d83f4SDavid Rientjes 	if (!task)
927a63d83f4SDavid Rientjes 		return -ESRCH;
928a63d83f4SDavid Rientjes 	if (lock_task_sighand(task, &flags)) {
929a63d83f4SDavid Rientjes 		oom_score_adj = task->signal->oom_score_adj;
930a63d83f4SDavid Rientjes 		unlock_task_sighand(task, &flags);
931a63d83f4SDavid Rientjes 	}
932a63d83f4SDavid Rientjes 	put_task_struct(task);
933a9c58b90SDavid Rientjes 	len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
934a63d83f4SDavid Rientjes 	return simple_read_from_buffer(buf, count, ppos, buffer, len);
935a63d83f4SDavid Rientjes }
936a63d83f4SDavid Rientjes 
937a63d83f4SDavid Rientjes static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
938a63d83f4SDavid Rientjes 					size_t count, loff_t *ppos)
939a63d83f4SDavid Rientjes {
940a63d83f4SDavid Rientjes 	struct task_struct *task;
941a63d83f4SDavid Rientjes 	char buffer[PROC_NUMBUF];
942a63d83f4SDavid Rientjes 	unsigned long flags;
9430a8cb8e3SAlexey Dobriyan 	int oom_score_adj;
944a63d83f4SDavid Rientjes 	int err;
945a63d83f4SDavid Rientjes 
946a63d83f4SDavid Rientjes 	memset(buffer, 0, sizeof(buffer));
947a63d83f4SDavid Rientjes 	if (count > sizeof(buffer) - 1)
948a63d83f4SDavid Rientjes 		count = sizeof(buffer) - 1;
949723548bfSDavid Rientjes 	if (copy_from_user(buffer, buf, count)) {
950723548bfSDavid Rientjes 		err = -EFAULT;
951723548bfSDavid Rientjes 		goto out;
952723548bfSDavid Rientjes 	}
953a63d83f4SDavid Rientjes 
9540a8cb8e3SAlexey Dobriyan 	err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
955a63d83f4SDavid Rientjes 	if (err)
956723548bfSDavid Rientjes 		goto out;
957a63d83f4SDavid Rientjes 	if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
958723548bfSDavid Rientjes 			oom_score_adj > OOM_SCORE_ADJ_MAX) {
959723548bfSDavid Rientjes 		err = -EINVAL;
960723548bfSDavid Rientjes 		goto out;
961723548bfSDavid Rientjes 	}
962a63d83f4SDavid Rientjes 
963496ad9aaSAl Viro 	task = get_proc_task(file_inode(file));
964723548bfSDavid Rientjes 	if (!task) {
965723548bfSDavid Rientjes 		err = -ESRCH;
966723548bfSDavid Rientjes 		goto out;
967723548bfSDavid Rientjes 	}
968a63d83f4SDavid Rientjes 
9693d5992d2SYing Han 	task_lock(task);
9703d5992d2SYing Han 	if (!task->mm) {
971723548bfSDavid Rientjes 		err = -EINVAL;
972723548bfSDavid Rientjes 		goto err_task_lock;
9733d5992d2SYing Han 	}
974d19d5476SDavid Rientjes 
975d19d5476SDavid Rientjes 	if (!lock_task_sighand(task, &flags)) {
976d19d5476SDavid Rientjes 		err = -ESRCH;
977d19d5476SDavid Rientjes 		goto err_task_lock;
978d19d5476SDavid Rientjes 	}
979d19d5476SDavid Rientjes 
980a9c58b90SDavid Rientjes 	if ((short)oom_score_adj < task->signal->oom_score_adj_min &&
981d19d5476SDavid Rientjes 			!capable(CAP_SYS_RESOURCE)) {
982d19d5476SDavid Rientjes 		err = -EACCES;
983d19d5476SDavid Rientjes 		goto err_sighand;
984d19d5476SDavid Rientjes 	}
985d19d5476SDavid Rientjes 
986a9c58b90SDavid Rientjes 	task->signal->oom_score_adj = (short)oom_score_adj;
987dabb16f6SMandeep Singh Baines 	if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
988a9c58b90SDavid Rientjes 		task->signal->oom_score_adj_min = (short)oom_score_adj;
98943d2b113SKAMEZAWA Hiroyuki 	trace_oom_score_adj_update(task);
99001dc52ebSDavidlohr Bueso 
991723548bfSDavid Rientjes err_sighand:
992a63d83f4SDavid Rientjes 	unlock_task_sighand(task, &flags);
993d19d5476SDavid Rientjes err_task_lock:
994d19d5476SDavid Rientjes 	task_unlock(task);
995a63d83f4SDavid Rientjes 	put_task_struct(task);
996723548bfSDavid Rientjes out:
997723548bfSDavid Rientjes 	return err < 0 ? err : count;
998a63d83f4SDavid Rientjes }
999a63d83f4SDavid Rientjes 
1000a63d83f4SDavid Rientjes static const struct file_operations proc_oom_score_adj_operations = {
1001a63d83f4SDavid Rientjes 	.read		= oom_score_adj_read,
1002a63d83f4SDavid Rientjes 	.write		= oom_score_adj_write,
10036038f373SArnd Bergmann 	.llseek		= default_llseek,
1004a63d83f4SDavid Rientjes };
1005a63d83f4SDavid Rientjes 
10061da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL
10071da177e4SLinus Torvalds #define TMPBUFLEN 21
10081da177e4SLinus Torvalds static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
10091da177e4SLinus Torvalds 				  size_t count, loff_t *ppos)
10101da177e4SLinus Torvalds {
1011496ad9aaSAl Viro 	struct inode * inode = file_inode(file);
101299f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
10131da177e4SLinus Torvalds 	ssize_t length;
10141da177e4SLinus Torvalds 	char tmpbuf[TMPBUFLEN];
10151da177e4SLinus Torvalds 
101699f89551SEric W. Biederman 	if (!task)
101799f89551SEric W. Biederman 		return -ESRCH;
10181da177e4SLinus Torvalds 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1019e1760bd5SEric W. Biederman 			   from_kuid(file->f_cred->user_ns,
1020e1760bd5SEric W. Biederman 				     audit_get_loginuid(task)));
102199f89551SEric W. Biederman 	put_task_struct(task);
10221da177e4SLinus Torvalds 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
10231da177e4SLinus Torvalds }
10241da177e4SLinus Torvalds 
10251da177e4SLinus Torvalds static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
10261da177e4SLinus Torvalds 				   size_t count, loff_t *ppos)
10271da177e4SLinus Torvalds {
1028496ad9aaSAl Viro 	struct inode * inode = file_inode(file);
10291da177e4SLinus Torvalds 	char *page, *tmp;
10301da177e4SLinus Torvalds 	ssize_t length;
10311da177e4SLinus Torvalds 	uid_t loginuid;
1032e1760bd5SEric W. Biederman 	kuid_t kloginuid;
10331da177e4SLinus Torvalds 
10347dc52157SPaul E. McKenney 	rcu_read_lock();
10357dc52157SPaul E. McKenney 	if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
10367dc52157SPaul E. McKenney 		rcu_read_unlock();
10371da177e4SLinus Torvalds 		return -EPERM;
10387dc52157SPaul E. McKenney 	}
10397dc52157SPaul E. McKenney 	rcu_read_unlock();
10401da177e4SLinus Torvalds 
1041e0182909SAl Viro 	if (count >= PAGE_SIZE)
1042e0182909SAl Viro 		count = PAGE_SIZE - 1;
10431da177e4SLinus Torvalds 
10441da177e4SLinus Torvalds 	if (*ppos != 0) {
10451da177e4SLinus Torvalds 		/* No partial writes. */
10461da177e4SLinus Torvalds 		return -EINVAL;
10471da177e4SLinus Torvalds 	}
1048e12ba74dSMel Gorman 	page = (char*)__get_free_page(GFP_TEMPORARY);
10491da177e4SLinus Torvalds 	if (!page)
10501da177e4SLinus Torvalds 		return -ENOMEM;
10511da177e4SLinus Torvalds 	length = -EFAULT;
10521da177e4SLinus Torvalds 	if (copy_from_user(page, buf, count))
10531da177e4SLinus Torvalds 		goto out_free_page;
10541da177e4SLinus Torvalds 
1055e0182909SAl Viro 	page[count] = '\0';
10561da177e4SLinus Torvalds 	loginuid = simple_strtoul(page, &tmp, 10);
10571da177e4SLinus Torvalds 	if (tmp == page) {
10581da177e4SLinus Torvalds 		length = -EINVAL;
10591da177e4SLinus Torvalds 		goto out_free_page;
10601da177e4SLinus Torvalds 
10611da177e4SLinus Torvalds 	}
106281407c84SEric Paris 
106381407c84SEric Paris 	/* is userspace tring to explicitly UNSET the loginuid? */
106481407c84SEric Paris 	if (loginuid == AUDIT_UID_UNSET) {
106581407c84SEric Paris 		kloginuid = INVALID_UID;
106681407c84SEric Paris 	} else {
1067e1760bd5SEric W. Biederman 		kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
1068e1760bd5SEric W. Biederman 		if (!uid_valid(kloginuid)) {
1069e1760bd5SEric W. Biederman 			length = -EINVAL;
1070e1760bd5SEric W. Biederman 			goto out_free_page;
1071e1760bd5SEric W. Biederman 		}
107281407c84SEric Paris 	}
1073e1760bd5SEric W. Biederman 
1074e1760bd5SEric W. Biederman 	length = audit_set_loginuid(kloginuid);
10751da177e4SLinus Torvalds 	if (likely(length == 0))
10761da177e4SLinus Torvalds 		length = count;
10771da177e4SLinus Torvalds 
10781da177e4SLinus Torvalds out_free_page:
10791da177e4SLinus Torvalds 	free_page((unsigned long) page);
10801da177e4SLinus Torvalds 	return length;
10811da177e4SLinus Torvalds }
10821da177e4SLinus Torvalds 
108300977a59SArjan van de Ven static const struct file_operations proc_loginuid_operations = {
10841da177e4SLinus Torvalds 	.read		= proc_loginuid_read,
10851da177e4SLinus Torvalds 	.write		= proc_loginuid_write,
108687df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
10871da177e4SLinus Torvalds };
10881e0bd755SEric Paris 
10891e0bd755SEric Paris static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
10901e0bd755SEric Paris 				  size_t count, loff_t *ppos)
10911e0bd755SEric Paris {
1092496ad9aaSAl Viro 	struct inode * inode = file_inode(file);
10931e0bd755SEric Paris 	struct task_struct *task = get_proc_task(inode);
10941e0bd755SEric Paris 	ssize_t length;
10951e0bd755SEric Paris 	char tmpbuf[TMPBUFLEN];
10961e0bd755SEric Paris 
10971e0bd755SEric Paris 	if (!task)
10981e0bd755SEric Paris 		return -ESRCH;
10991e0bd755SEric Paris 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
11001e0bd755SEric Paris 				audit_get_sessionid(task));
11011e0bd755SEric Paris 	put_task_struct(task);
11021e0bd755SEric Paris 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
11031e0bd755SEric Paris }
11041e0bd755SEric Paris 
11051e0bd755SEric Paris static const struct file_operations proc_sessionid_operations = {
11061e0bd755SEric Paris 	.read		= proc_sessionid_read,
110787df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
11081e0bd755SEric Paris };
11091da177e4SLinus Torvalds #endif
11101da177e4SLinus Torvalds 
1111f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
1112f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1113f4f154fdSAkinobu Mita 				      size_t count, loff_t *ppos)
1114f4f154fdSAkinobu Mita {
1115496ad9aaSAl Viro 	struct task_struct *task = get_proc_task(file_inode(file));
1116f4f154fdSAkinobu Mita 	char buffer[PROC_NUMBUF];
1117f4f154fdSAkinobu Mita 	size_t len;
1118f4f154fdSAkinobu Mita 	int make_it_fail;
1119f4f154fdSAkinobu Mita 
1120f4f154fdSAkinobu Mita 	if (!task)
1121f4f154fdSAkinobu Mita 		return -ESRCH;
1122f4f154fdSAkinobu Mita 	make_it_fail = task->make_it_fail;
1123f4f154fdSAkinobu Mita 	put_task_struct(task);
1124f4f154fdSAkinobu Mita 
1125f4f154fdSAkinobu Mita 	len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
11260c28f287SAkinobu Mita 
11270c28f287SAkinobu Mita 	return simple_read_from_buffer(buf, count, ppos, buffer, len);
1128f4f154fdSAkinobu Mita }
1129f4f154fdSAkinobu Mita 
1130f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_write(struct file * file,
1131f4f154fdSAkinobu Mita 			const char __user * buf, size_t count, loff_t *ppos)
1132f4f154fdSAkinobu Mita {
1133f4f154fdSAkinobu Mita 	struct task_struct *task;
1134f4f154fdSAkinobu Mita 	char buffer[PROC_NUMBUF], *end;
1135f4f154fdSAkinobu Mita 	int make_it_fail;
1136f4f154fdSAkinobu Mita 
1137f4f154fdSAkinobu Mita 	if (!capable(CAP_SYS_RESOURCE))
1138f4f154fdSAkinobu Mita 		return -EPERM;
1139f4f154fdSAkinobu Mita 	memset(buffer, 0, sizeof(buffer));
1140f4f154fdSAkinobu Mita 	if (count > sizeof(buffer) - 1)
1141f4f154fdSAkinobu Mita 		count = sizeof(buffer) - 1;
1142f4f154fdSAkinobu Mita 	if (copy_from_user(buffer, buf, count))
1143f4f154fdSAkinobu Mita 		return -EFAULT;
1144cba8aafeSVincent Li 	make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1145cba8aafeSVincent Li 	if (*end)
1146cba8aafeSVincent Li 		return -EINVAL;
114716caed31SDave Jones 	if (make_it_fail < 0 || make_it_fail > 1)
114816caed31SDave Jones 		return -EINVAL;
114916caed31SDave Jones 
1150496ad9aaSAl Viro 	task = get_proc_task(file_inode(file));
1151f4f154fdSAkinobu Mita 	if (!task)
1152f4f154fdSAkinobu Mita 		return -ESRCH;
1153f4f154fdSAkinobu Mita 	task->make_it_fail = make_it_fail;
1154f4f154fdSAkinobu Mita 	put_task_struct(task);
1155cba8aafeSVincent Li 
1156cba8aafeSVincent Li 	return count;
1157f4f154fdSAkinobu Mita }
1158f4f154fdSAkinobu Mita 
115900977a59SArjan van de Ven static const struct file_operations proc_fault_inject_operations = {
1160f4f154fdSAkinobu Mita 	.read		= proc_fault_inject_read,
1161f4f154fdSAkinobu Mita 	.write		= proc_fault_inject_write,
116287df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
1163f4f154fdSAkinobu Mita };
1164f4f154fdSAkinobu Mita #endif
1165f4f154fdSAkinobu Mita 
11669745512cSArjan van de Ven 
116743ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
116843ae34cbSIngo Molnar /*
116943ae34cbSIngo Molnar  * Print out various scheduling related per-task fields:
117043ae34cbSIngo Molnar  */
117143ae34cbSIngo Molnar static int sched_show(struct seq_file *m, void *v)
117243ae34cbSIngo Molnar {
117343ae34cbSIngo Molnar 	struct inode *inode = m->private;
117443ae34cbSIngo Molnar 	struct task_struct *p;
117543ae34cbSIngo Molnar 
117643ae34cbSIngo Molnar 	p = get_proc_task(inode);
117743ae34cbSIngo Molnar 	if (!p)
117843ae34cbSIngo Molnar 		return -ESRCH;
117943ae34cbSIngo Molnar 	proc_sched_show_task(p, m);
118043ae34cbSIngo Molnar 
118143ae34cbSIngo Molnar 	put_task_struct(p);
118243ae34cbSIngo Molnar 
118343ae34cbSIngo Molnar 	return 0;
118443ae34cbSIngo Molnar }
118543ae34cbSIngo Molnar 
118643ae34cbSIngo Molnar static ssize_t
118743ae34cbSIngo Molnar sched_write(struct file *file, const char __user *buf,
118843ae34cbSIngo Molnar 	    size_t count, loff_t *offset)
118943ae34cbSIngo Molnar {
1190496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
119143ae34cbSIngo Molnar 	struct task_struct *p;
119243ae34cbSIngo Molnar 
119343ae34cbSIngo Molnar 	p = get_proc_task(inode);
119443ae34cbSIngo Molnar 	if (!p)
119543ae34cbSIngo Molnar 		return -ESRCH;
119643ae34cbSIngo Molnar 	proc_sched_set_task(p);
119743ae34cbSIngo Molnar 
119843ae34cbSIngo Molnar 	put_task_struct(p);
119943ae34cbSIngo Molnar 
120043ae34cbSIngo Molnar 	return count;
120143ae34cbSIngo Molnar }
120243ae34cbSIngo Molnar 
120343ae34cbSIngo Molnar static int sched_open(struct inode *inode, struct file *filp)
120443ae34cbSIngo Molnar {
1205c6a34058SJovi Zhang 	return single_open(filp, sched_show, inode);
120643ae34cbSIngo Molnar }
120743ae34cbSIngo Molnar 
120843ae34cbSIngo Molnar static const struct file_operations proc_pid_sched_operations = {
120943ae34cbSIngo Molnar 	.open		= sched_open,
121043ae34cbSIngo Molnar 	.read		= seq_read,
121143ae34cbSIngo Molnar 	.write		= sched_write,
121243ae34cbSIngo Molnar 	.llseek		= seq_lseek,
12135ea473a1SAlexey Dobriyan 	.release	= single_release,
121443ae34cbSIngo Molnar };
121543ae34cbSIngo Molnar 
121643ae34cbSIngo Molnar #endif
121743ae34cbSIngo Molnar 
12185091faa4SMike Galbraith #ifdef CONFIG_SCHED_AUTOGROUP
12195091faa4SMike Galbraith /*
12205091faa4SMike Galbraith  * Print out autogroup related information:
12215091faa4SMike Galbraith  */
12225091faa4SMike Galbraith static int sched_autogroup_show(struct seq_file *m, void *v)
12235091faa4SMike Galbraith {
12245091faa4SMike Galbraith 	struct inode *inode = m->private;
12255091faa4SMike Galbraith 	struct task_struct *p;
12265091faa4SMike Galbraith 
12275091faa4SMike Galbraith 	p = get_proc_task(inode);
12285091faa4SMike Galbraith 	if (!p)
12295091faa4SMike Galbraith 		return -ESRCH;
12305091faa4SMike Galbraith 	proc_sched_autogroup_show_task(p, m);
12315091faa4SMike Galbraith 
12325091faa4SMike Galbraith 	put_task_struct(p);
12335091faa4SMike Galbraith 
12345091faa4SMike Galbraith 	return 0;
12355091faa4SMike Galbraith }
12365091faa4SMike Galbraith 
12375091faa4SMike Galbraith static ssize_t
12385091faa4SMike Galbraith sched_autogroup_write(struct file *file, const char __user *buf,
12395091faa4SMike Galbraith 	    size_t count, loff_t *offset)
12405091faa4SMike Galbraith {
1241496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
12425091faa4SMike Galbraith 	struct task_struct *p;
12435091faa4SMike Galbraith 	char buffer[PROC_NUMBUF];
12440a8cb8e3SAlexey Dobriyan 	int nice;
12455091faa4SMike Galbraith 	int err;
12465091faa4SMike Galbraith 
12475091faa4SMike Galbraith 	memset(buffer, 0, sizeof(buffer));
12485091faa4SMike Galbraith 	if (count > sizeof(buffer) - 1)
12495091faa4SMike Galbraith 		count = sizeof(buffer) - 1;
12505091faa4SMike Galbraith 	if (copy_from_user(buffer, buf, count))
12515091faa4SMike Galbraith 		return -EFAULT;
12525091faa4SMike Galbraith 
12530a8cb8e3SAlexey Dobriyan 	err = kstrtoint(strstrip(buffer), 0, &nice);
12540a8cb8e3SAlexey Dobriyan 	if (err < 0)
12550a8cb8e3SAlexey Dobriyan 		return err;
12565091faa4SMike Galbraith 
12575091faa4SMike Galbraith 	p = get_proc_task(inode);
12585091faa4SMike Galbraith 	if (!p)
12595091faa4SMike Galbraith 		return -ESRCH;
12605091faa4SMike Galbraith 
12612e5b5b3aSHiroshi Shimamoto 	err = proc_sched_autogroup_set_nice(p, nice);
12625091faa4SMike Galbraith 	if (err)
12635091faa4SMike Galbraith 		count = err;
12645091faa4SMike Galbraith 
12655091faa4SMike Galbraith 	put_task_struct(p);
12665091faa4SMike Galbraith 
12675091faa4SMike Galbraith 	return count;
12685091faa4SMike Galbraith }
12695091faa4SMike Galbraith 
12705091faa4SMike Galbraith static int sched_autogroup_open(struct inode *inode, struct file *filp)
12715091faa4SMike Galbraith {
12725091faa4SMike Galbraith 	int ret;
12735091faa4SMike Galbraith 
12745091faa4SMike Galbraith 	ret = single_open(filp, sched_autogroup_show, NULL);
12755091faa4SMike Galbraith 	if (!ret) {
12765091faa4SMike Galbraith 		struct seq_file *m = filp->private_data;
12775091faa4SMike Galbraith 
12785091faa4SMike Galbraith 		m->private = inode;
12795091faa4SMike Galbraith 	}
12805091faa4SMike Galbraith 	return ret;
12815091faa4SMike Galbraith }
12825091faa4SMike Galbraith 
12835091faa4SMike Galbraith static const struct file_operations proc_pid_sched_autogroup_operations = {
12845091faa4SMike Galbraith 	.open		= sched_autogroup_open,
12855091faa4SMike Galbraith 	.read		= seq_read,
12865091faa4SMike Galbraith 	.write		= sched_autogroup_write,
12875091faa4SMike Galbraith 	.llseek		= seq_lseek,
12885091faa4SMike Galbraith 	.release	= single_release,
12895091faa4SMike Galbraith };
12905091faa4SMike Galbraith 
12915091faa4SMike Galbraith #endif /* CONFIG_SCHED_AUTOGROUP */
12925091faa4SMike Galbraith 
12934614a696Sjohn stultz static ssize_t comm_write(struct file *file, const char __user *buf,
12944614a696Sjohn stultz 				size_t count, loff_t *offset)
12954614a696Sjohn stultz {
1296496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
12974614a696Sjohn stultz 	struct task_struct *p;
12984614a696Sjohn stultz 	char buffer[TASK_COMM_LEN];
1299830e0fc9SDavid Rientjes 	const size_t maxlen = sizeof(buffer) - 1;
13004614a696Sjohn stultz 
13014614a696Sjohn stultz 	memset(buffer, 0, sizeof(buffer));
1302830e0fc9SDavid Rientjes 	if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
13034614a696Sjohn stultz 		return -EFAULT;
13044614a696Sjohn stultz 
13054614a696Sjohn stultz 	p = get_proc_task(inode);
13064614a696Sjohn stultz 	if (!p)
13074614a696Sjohn stultz 		return -ESRCH;
13084614a696Sjohn stultz 
13094614a696Sjohn stultz 	if (same_thread_group(current, p))
13104614a696Sjohn stultz 		set_task_comm(p, buffer);
13114614a696Sjohn stultz 	else
13124614a696Sjohn stultz 		count = -EINVAL;
13134614a696Sjohn stultz 
13144614a696Sjohn stultz 	put_task_struct(p);
13154614a696Sjohn stultz 
13164614a696Sjohn stultz 	return count;
13174614a696Sjohn stultz }
13184614a696Sjohn stultz 
13194614a696Sjohn stultz static int comm_show(struct seq_file *m, void *v)
13204614a696Sjohn stultz {
13214614a696Sjohn stultz 	struct inode *inode = m->private;
13224614a696Sjohn stultz 	struct task_struct *p;
13234614a696Sjohn stultz 
13244614a696Sjohn stultz 	p = get_proc_task(inode);
13254614a696Sjohn stultz 	if (!p)
13264614a696Sjohn stultz 		return -ESRCH;
13274614a696Sjohn stultz 
13284614a696Sjohn stultz 	task_lock(p);
13294614a696Sjohn stultz 	seq_printf(m, "%s\n", p->comm);
13304614a696Sjohn stultz 	task_unlock(p);
13314614a696Sjohn stultz 
13324614a696Sjohn stultz 	put_task_struct(p);
13334614a696Sjohn stultz 
13344614a696Sjohn stultz 	return 0;
13354614a696Sjohn stultz }
13364614a696Sjohn stultz 
13374614a696Sjohn stultz static int comm_open(struct inode *inode, struct file *filp)
13384614a696Sjohn stultz {
1339c6a34058SJovi Zhang 	return single_open(filp, comm_show, inode);
13404614a696Sjohn stultz }
13414614a696Sjohn stultz 
13424614a696Sjohn stultz static const struct file_operations proc_pid_set_comm_operations = {
13434614a696Sjohn stultz 	.open		= comm_open,
13444614a696Sjohn stultz 	.read		= seq_read,
13454614a696Sjohn stultz 	.write		= comm_write,
13464614a696Sjohn stultz 	.llseek		= seq_lseek,
13474614a696Sjohn stultz 	.release	= single_release,
13484614a696Sjohn stultz };
13494614a696Sjohn stultz 
13507773fbc5SCyrill Gorcunov static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
1351925d1c40SMatt Helsley {
1352925d1c40SMatt Helsley 	struct task_struct *task;
1353925d1c40SMatt Helsley 	struct mm_struct *mm;
1354925d1c40SMatt Helsley 	struct file *exe_file;
1355925d1c40SMatt Helsley 
13567773fbc5SCyrill Gorcunov 	task = get_proc_task(dentry->d_inode);
1357925d1c40SMatt Helsley 	if (!task)
1358925d1c40SMatt Helsley 		return -ENOENT;
1359925d1c40SMatt Helsley 	mm = get_task_mm(task);
1360925d1c40SMatt Helsley 	put_task_struct(task);
1361925d1c40SMatt Helsley 	if (!mm)
1362925d1c40SMatt Helsley 		return -ENOENT;
1363925d1c40SMatt Helsley 	exe_file = get_mm_exe_file(mm);
1364925d1c40SMatt Helsley 	mmput(mm);
1365925d1c40SMatt Helsley 	if (exe_file) {
1366925d1c40SMatt Helsley 		*exe_path = exe_file->f_path;
1367925d1c40SMatt Helsley 		path_get(&exe_file->f_path);
1368925d1c40SMatt Helsley 		fput(exe_file);
1369925d1c40SMatt Helsley 		return 0;
1370925d1c40SMatt Helsley 	} else
1371925d1c40SMatt Helsley 		return -ENOENT;
1372925d1c40SMatt Helsley }
1373925d1c40SMatt Helsley 
1374008b150aSAl Viro static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
13751da177e4SLinus Torvalds {
13761da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
1377408ef013SChristoph Hellwig 	struct path path;
13781da177e4SLinus Torvalds 	int error = -EACCES;
13791da177e4SLinus Torvalds 
1380778c1144SEric W. Biederman 	/* Are we allowed to snoop on the tasks file descriptors? */
1381778c1144SEric W. Biederman 	if (!proc_fd_access_allowed(inode))
13821da177e4SLinus Torvalds 		goto out;
13831da177e4SLinus Torvalds 
1384408ef013SChristoph Hellwig 	error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1385408ef013SChristoph Hellwig 	if (error)
1386408ef013SChristoph Hellwig 		goto out;
1387408ef013SChristoph Hellwig 
1388b5fb63c1SChristoph Hellwig 	nd_jump_link(nd, &path);
1389408ef013SChristoph Hellwig 	return NULL;
13901da177e4SLinus Torvalds out:
1391008b150aSAl Viro 	return ERR_PTR(error);
13921da177e4SLinus Torvalds }
13931da177e4SLinus Torvalds 
13943dcd25f3SJan Blunck static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
13951da177e4SLinus Torvalds {
1396e12ba74dSMel Gorman 	char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
13973dcd25f3SJan Blunck 	char *pathname;
13981da177e4SLinus Torvalds 	int len;
13991da177e4SLinus Torvalds 
14001da177e4SLinus Torvalds 	if (!tmp)
14011da177e4SLinus Torvalds 		return -ENOMEM;
14021da177e4SLinus Torvalds 
14037b2a69baSEric W. Biederman 	pathname = d_path(path, tmp, PAGE_SIZE);
14043dcd25f3SJan Blunck 	len = PTR_ERR(pathname);
14053dcd25f3SJan Blunck 	if (IS_ERR(pathname))
14061da177e4SLinus Torvalds 		goto out;
14073dcd25f3SJan Blunck 	len = tmp + PAGE_SIZE - 1 - pathname;
14081da177e4SLinus Torvalds 
14091da177e4SLinus Torvalds 	if (len > buflen)
14101da177e4SLinus Torvalds 		len = buflen;
14113dcd25f3SJan Blunck 	if (copy_to_user(buffer, pathname, len))
14121da177e4SLinus Torvalds 		len = -EFAULT;
14131da177e4SLinus Torvalds  out:
14141da177e4SLinus Torvalds 	free_page((unsigned long)tmp);
14151da177e4SLinus Torvalds 	return len;
14161da177e4SLinus Torvalds }
14171da177e4SLinus Torvalds 
14181da177e4SLinus Torvalds static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
14191da177e4SLinus Torvalds {
14201da177e4SLinus Torvalds 	int error = -EACCES;
14211da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
14223dcd25f3SJan Blunck 	struct path path;
14231da177e4SLinus Torvalds 
1424778c1144SEric W. Biederman 	/* Are we allowed to snoop on the tasks file descriptors? */
1425778c1144SEric W. Biederman 	if (!proc_fd_access_allowed(inode))
14261da177e4SLinus Torvalds 		goto out;
14271da177e4SLinus Torvalds 
14287773fbc5SCyrill Gorcunov 	error = PROC_I(inode)->op.proc_get_link(dentry, &path);
14291da177e4SLinus Torvalds 	if (error)
14301da177e4SLinus Torvalds 		goto out;
14311da177e4SLinus Torvalds 
14323dcd25f3SJan Blunck 	error = do_proc_readlink(&path, buffer, buflen);
14333dcd25f3SJan Blunck 	path_put(&path);
14341da177e4SLinus Torvalds out:
14351da177e4SLinus Torvalds 	return error;
14361da177e4SLinus Torvalds }
14371da177e4SLinus Torvalds 
1438faf60af1SCyrill Gorcunov const struct inode_operations proc_pid_link_inode_operations = {
14391da177e4SLinus Torvalds 	.readlink	= proc_pid_readlink,
14406d76fa58SLinus Torvalds 	.follow_link	= proc_pid_follow_link,
14416d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
14421da177e4SLinus Torvalds };
14431da177e4SLinus Torvalds 
144428a6d671SEric W. Biederman 
144528a6d671SEric W. Biederman /* building an inode */
144628a6d671SEric W. Biederman 
14476b4e306aSEric W. Biederman struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
144828a6d671SEric W. Biederman {
144928a6d671SEric W. Biederman 	struct inode * inode;
145028a6d671SEric W. Biederman 	struct proc_inode *ei;
1451c69e8d9cSDavid Howells 	const struct cred *cred;
145228a6d671SEric W. Biederman 
145328a6d671SEric W. Biederman 	/* We need a new inode */
145428a6d671SEric W. Biederman 
145528a6d671SEric W. Biederman 	inode = new_inode(sb);
145628a6d671SEric W. Biederman 	if (!inode)
145728a6d671SEric W. Biederman 		goto out;
145828a6d671SEric W. Biederman 
145928a6d671SEric W. Biederman 	/* Common stuff */
146028a6d671SEric W. Biederman 	ei = PROC_I(inode);
146185fe4025SChristoph Hellwig 	inode->i_ino = get_next_ino();
146228a6d671SEric W. Biederman 	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
146328a6d671SEric W. Biederman 	inode->i_op = &proc_def_inode_operations;
146428a6d671SEric W. Biederman 
146528a6d671SEric W. Biederman 	/*
146628a6d671SEric W. Biederman 	 * grab the reference to task.
146728a6d671SEric W. Biederman 	 */
14681a657f78SOleg Nesterov 	ei->pid = get_task_pid(task, PIDTYPE_PID);
146928a6d671SEric W. Biederman 	if (!ei->pid)
147028a6d671SEric W. Biederman 		goto out_unlock;
147128a6d671SEric W. Biederman 
147228a6d671SEric W. Biederman 	if (task_dumpable(task)) {
1473c69e8d9cSDavid Howells 		rcu_read_lock();
1474c69e8d9cSDavid Howells 		cred = __task_cred(task);
1475c69e8d9cSDavid Howells 		inode->i_uid = cred->euid;
1476c69e8d9cSDavid Howells 		inode->i_gid = cred->egid;
1477c69e8d9cSDavid Howells 		rcu_read_unlock();
147828a6d671SEric W. Biederman 	}
147928a6d671SEric W. Biederman 	security_task_to_inode(task, inode);
148028a6d671SEric W. Biederman 
148128a6d671SEric W. Biederman out:
148228a6d671SEric W. Biederman 	return inode;
148328a6d671SEric W. Biederman 
148428a6d671SEric W. Biederman out_unlock:
148528a6d671SEric W. Biederman 	iput(inode);
148628a6d671SEric W. Biederman 	return NULL;
148728a6d671SEric W. Biederman }
148828a6d671SEric W. Biederman 
14896b4e306aSEric W. Biederman int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
149028a6d671SEric W. Biederman {
149128a6d671SEric W. Biederman 	struct inode *inode = dentry->d_inode;
149228a6d671SEric W. Biederman 	struct task_struct *task;
1493c69e8d9cSDavid Howells 	const struct cred *cred;
14940499680aSVasiliy Kulikov 	struct pid_namespace *pid = dentry->d_sb->s_fs_info;
1495c69e8d9cSDavid Howells 
149628a6d671SEric W. Biederman 	generic_fillattr(inode, stat);
149728a6d671SEric W. Biederman 
149828a6d671SEric W. Biederman 	rcu_read_lock();
1499dcb0f222SEric W. Biederman 	stat->uid = GLOBAL_ROOT_UID;
1500dcb0f222SEric W. Biederman 	stat->gid = GLOBAL_ROOT_GID;
150128a6d671SEric W. Biederman 	task = pid_task(proc_pid(inode), PIDTYPE_PID);
150228a6d671SEric W. Biederman 	if (task) {
15030499680aSVasiliy Kulikov 		if (!has_pid_permissions(pid, task, 2)) {
15040499680aSVasiliy Kulikov 			rcu_read_unlock();
15050499680aSVasiliy Kulikov 			/*
15060499680aSVasiliy Kulikov 			 * This doesn't prevent learning whether PID exists,
15070499680aSVasiliy Kulikov 			 * it only makes getattr() consistent with readdir().
15080499680aSVasiliy Kulikov 			 */
15090499680aSVasiliy Kulikov 			return -ENOENT;
15100499680aSVasiliy Kulikov 		}
151128a6d671SEric W. Biederman 		if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
151228a6d671SEric W. Biederman 		    task_dumpable(task)) {
1513c69e8d9cSDavid Howells 			cred = __task_cred(task);
1514c69e8d9cSDavid Howells 			stat->uid = cred->euid;
1515c69e8d9cSDavid Howells 			stat->gid = cred->egid;
151628a6d671SEric W. Biederman 		}
151728a6d671SEric W. Biederman 	}
151828a6d671SEric W. Biederman 	rcu_read_unlock();
151928a6d671SEric W. Biederman 	return 0;
152028a6d671SEric W. Biederman }
152128a6d671SEric W. Biederman 
152228a6d671SEric W. Biederman /* dentry stuff */
152328a6d671SEric W. Biederman 
152428a6d671SEric W. Biederman /*
152528a6d671SEric W. Biederman  *	Exceptional case: normally we are not allowed to unhash a busy
152628a6d671SEric W. Biederman  * directory. In this case, however, we can do it - no aliasing problems
152728a6d671SEric W. Biederman  * due to the way we treat inodes.
152828a6d671SEric W. Biederman  *
152928a6d671SEric W. Biederman  * Rewrite the inode's ownerships here because the owning task may have
153028a6d671SEric W. Biederman  * performed a setuid(), etc.
153128a6d671SEric W. Biederman  *
153228a6d671SEric W. Biederman  * Before the /proc/pid/status file was created the only way to read
153328a6d671SEric W. Biederman  * the effective uid of a /process was to stat /proc/pid.  Reading
153428a6d671SEric W. Biederman  * /proc/pid/status is slow enough that procps and other packages
153528a6d671SEric W. Biederman  * kept stating /proc/pid.  To keep the rules in /proc simple I have
153628a6d671SEric W. Biederman  * made this apply to all per process world readable and executable
153728a6d671SEric W. Biederman  * directories.
153828a6d671SEric W. Biederman  */
15390b728e19SAl Viro int pid_revalidate(struct dentry *dentry, unsigned int flags)
154028a6d671SEric W. Biederman {
154134286d66SNick Piggin 	struct inode *inode;
154234286d66SNick Piggin 	struct task_struct *task;
1543c69e8d9cSDavid Howells 	const struct cred *cred;
1544c69e8d9cSDavid Howells 
15450b728e19SAl Viro 	if (flags & LOOKUP_RCU)
154634286d66SNick Piggin 		return -ECHILD;
154734286d66SNick Piggin 
154834286d66SNick Piggin 	inode = dentry->d_inode;
154934286d66SNick Piggin 	task = get_proc_task(inode);
155034286d66SNick Piggin 
155128a6d671SEric W. Biederman 	if (task) {
155228a6d671SEric W. Biederman 		if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
155328a6d671SEric W. Biederman 		    task_dumpable(task)) {
1554c69e8d9cSDavid Howells 			rcu_read_lock();
1555c69e8d9cSDavid Howells 			cred = __task_cred(task);
1556c69e8d9cSDavid Howells 			inode->i_uid = cred->euid;
1557c69e8d9cSDavid Howells 			inode->i_gid = cred->egid;
1558c69e8d9cSDavid Howells 			rcu_read_unlock();
155928a6d671SEric W. Biederman 		} else {
1560dcb0f222SEric W. Biederman 			inode->i_uid = GLOBAL_ROOT_UID;
1561dcb0f222SEric W. Biederman 			inode->i_gid = GLOBAL_ROOT_GID;
156228a6d671SEric W. Biederman 		}
156328a6d671SEric W. Biederman 		inode->i_mode &= ~(S_ISUID | S_ISGID);
156428a6d671SEric W. Biederman 		security_task_to_inode(task, inode);
156528a6d671SEric W. Biederman 		put_task_struct(task);
156628a6d671SEric W. Biederman 		return 1;
156728a6d671SEric W. Biederman 	}
156828a6d671SEric W. Biederman 	return 0;
156928a6d671SEric W. Biederman }
157028a6d671SEric W. Biederman 
1571d855a4b7SOleg Nesterov static inline bool proc_inode_is_dead(struct inode *inode)
1572d855a4b7SOleg Nesterov {
1573d855a4b7SOleg Nesterov 	return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1574d855a4b7SOleg Nesterov }
1575d855a4b7SOleg Nesterov 
15761dd704b6SDavid Howells int pid_delete_dentry(const struct dentry *dentry)
15771dd704b6SDavid Howells {
15781dd704b6SDavid Howells 	/* Is the task we represent dead?
15791dd704b6SDavid Howells 	 * If so, then don't put the dentry on the lru list,
15801dd704b6SDavid Howells 	 * kill it immediately.
15811dd704b6SDavid Howells 	 */
1582d855a4b7SOleg Nesterov 	return proc_inode_is_dead(dentry->d_inode);
15831dd704b6SDavid Howells }
15841dd704b6SDavid Howells 
15856b4e306aSEric W. Biederman const struct dentry_operations pid_dentry_operations =
158628a6d671SEric W. Biederman {
158728a6d671SEric W. Biederman 	.d_revalidate	= pid_revalidate,
158828a6d671SEric W. Biederman 	.d_delete	= pid_delete_dentry,
158928a6d671SEric W. Biederman };
159028a6d671SEric W. Biederman 
159128a6d671SEric W. Biederman /* Lookups */
159228a6d671SEric W. Biederman 
15931c0d04c9SEric W. Biederman /*
15941c0d04c9SEric W. Biederman  * Fill a directory entry.
15951c0d04c9SEric W. Biederman  *
15961c0d04c9SEric W. Biederman  * If possible create the dcache entry and derive our inode number and
15971c0d04c9SEric W. Biederman  * file type from dcache entry.
15981c0d04c9SEric W. Biederman  *
15991c0d04c9SEric W. Biederman  * Since all of the proc inode numbers are dynamically generated, the inode
16001c0d04c9SEric W. Biederman  * numbers do not exist until the inode is cache.  This means creating the
16011c0d04c9SEric W. Biederman  * the dcache entry in readdir is necessary to keep the inode numbers
16021c0d04c9SEric W. Biederman  * reported by readdir in sync with the inode numbers reported
16031c0d04c9SEric W. Biederman  * by stat.
16041c0d04c9SEric W. Biederman  */
1605f0c3b509SAl Viro bool proc_fill_cache(struct file *file, struct dir_context *ctx,
16066b4e306aSEric W. Biederman 	const char *name, int len,
1607c5141e6dSEric Dumazet 	instantiate_t instantiate, struct task_struct *task, const void *ptr)
160861a28784SEric W. Biederman {
1609f0c3b509SAl Viro 	struct dentry *child, *dir = file->f_path.dentry;
16101df98b8bSAl Viro 	struct qstr qname = QSTR_INIT(name, len);
161161a28784SEric W. Biederman 	struct inode *inode;
16121df98b8bSAl Viro 	unsigned type;
16131df98b8bSAl Viro 	ino_t ino;
161461a28784SEric W. Biederman 
16151df98b8bSAl Viro 	child = d_hash_and_lookup(dir, &qname);
161661a28784SEric W. Biederman 	if (!child) {
16171df98b8bSAl Viro 		child = d_alloc(dir, &qname);
16181df98b8bSAl Viro 		if (!child)
161961a28784SEric W. Biederman 			goto end_instantiate;
16201df98b8bSAl Viro 		if (instantiate(dir->d_inode, child, task, ptr) < 0) {
16211df98b8bSAl Viro 			dput(child);
16221df98b8bSAl Viro 			goto end_instantiate;
16231df98b8bSAl Viro 		}
16241df98b8bSAl Viro 	}
162561a28784SEric W. Biederman 	inode = child->d_inode;
162661a28784SEric W. Biederman 	ino = inode->i_ino;
162761a28784SEric W. Biederman 	type = inode->i_mode >> 12;
162861a28784SEric W. Biederman 	dput(child);
1629f0c3b509SAl Viro 	return dir_emit(ctx, name, len, ino, type);
16301df98b8bSAl Viro 
16311df98b8bSAl Viro end_instantiate:
16321df98b8bSAl Viro 	return dir_emit(ctx, name, len, 1, DT_UNKNOWN);
163361a28784SEric W. Biederman }
163461a28784SEric W. Biederman 
1635640708a2SPavel Emelyanov #ifdef CONFIG_CHECKPOINT_RESTORE
1636640708a2SPavel Emelyanov 
1637640708a2SPavel Emelyanov /*
1638640708a2SPavel Emelyanov  * dname_to_vma_addr - maps a dentry name into two unsigned longs
1639640708a2SPavel Emelyanov  * which represent vma start and end addresses.
1640640708a2SPavel Emelyanov  */
1641640708a2SPavel Emelyanov static int dname_to_vma_addr(struct dentry *dentry,
1642640708a2SPavel Emelyanov 			     unsigned long *start, unsigned long *end)
1643640708a2SPavel Emelyanov {
1644640708a2SPavel Emelyanov 	if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2)
1645640708a2SPavel Emelyanov 		return -EINVAL;
1646640708a2SPavel Emelyanov 
1647640708a2SPavel Emelyanov 	return 0;
1648640708a2SPavel Emelyanov }
1649640708a2SPavel Emelyanov 
16500b728e19SAl Viro static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
1651640708a2SPavel Emelyanov {
1652640708a2SPavel Emelyanov 	unsigned long vm_start, vm_end;
1653640708a2SPavel Emelyanov 	bool exact_vma_exists = false;
1654640708a2SPavel Emelyanov 	struct mm_struct *mm = NULL;
1655640708a2SPavel Emelyanov 	struct task_struct *task;
1656640708a2SPavel Emelyanov 	const struct cred *cred;
1657640708a2SPavel Emelyanov 	struct inode *inode;
1658640708a2SPavel Emelyanov 	int status = 0;
1659640708a2SPavel Emelyanov 
16600b728e19SAl Viro 	if (flags & LOOKUP_RCU)
1661640708a2SPavel Emelyanov 		return -ECHILD;
1662640708a2SPavel Emelyanov 
1663640708a2SPavel Emelyanov 	if (!capable(CAP_SYS_ADMIN)) {
166441735818SZhao Hongjiang 		status = -EPERM;
1665640708a2SPavel Emelyanov 		goto out_notask;
1666640708a2SPavel Emelyanov 	}
1667640708a2SPavel Emelyanov 
1668640708a2SPavel Emelyanov 	inode = dentry->d_inode;
1669640708a2SPavel Emelyanov 	task = get_proc_task(inode);
1670640708a2SPavel Emelyanov 	if (!task)
1671640708a2SPavel Emelyanov 		goto out_notask;
1672640708a2SPavel Emelyanov 
16732344bec7SCong Wang 	mm = mm_access(task, PTRACE_MODE_READ);
16742344bec7SCong Wang 	if (IS_ERR_OR_NULL(mm))
1675640708a2SPavel Emelyanov 		goto out;
1676640708a2SPavel Emelyanov 
1677640708a2SPavel Emelyanov 	if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1678640708a2SPavel Emelyanov 		down_read(&mm->mmap_sem);
1679640708a2SPavel Emelyanov 		exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1680640708a2SPavel Emelyanov 		up_read(&mm->mmap_sem);
1681640708a2SPavel Emelyanov 	}
1682640708a2SPavel Emelyanov 
1683640708a2SPavel Emelyanov 	mmput(mm);
1684640708a2SPavel Emelyanov 
1685640708a2SPavel Emelyanov 	if (exact_vma_exists) {
1686640708a2SPavel Emelyanov 		if (task_dumpable(task)) {
1687640708a2SPavel Emelyanov 			rcu_read_lock();
1688640708a2SPavel Emelyanov 			cred = __task_cred(task);
1689640708a2SPavel Emelyanov 			inode->i_uid = cred->euid;
1690640708a2SPavel Emelyanov 			inode->i_gid = cred->egid;
1691640708a2SPavel Emelyanov 			rcu_read_unlock();
1692640708a2SPavel Emelyanov 		} else {
1693dcb0f222SEric W. Biederman 			inode->i_uid = GLOBAL_ROOT_UID;
1694dcb0f222SEric W. Biederman 			inode->i_gid = GLOBAL_ROOT_GID;
1695640708a2SPavel Emelyanov 		}
1696640708a2SPavel Emelyanov 		security_task_to_inode(task, inode);
1697640708a2SPavel Emelyanov 		status = 1;
1698640708a2SPavel Emelyanov 	}
1699640708a2SPavel Emelyanov 
1700640708a2SPavel Emelyanov out:
1701640708a2SPavel Emelyanov 	put_task_struct(task);
1702640708a2SPavel Emelyanov 
1703640708a2SPavel Emelyanov out_notask:
1704640708a2SPavel Emelyanov 	return status;
1705640708a2SPavel Emelyanov }
1706640708a2SPavel Emelyanov 
1707640708a2SPavel Emelyanov static const struct dentry_operations tid_map_files_dentry_operations = {
1708640708a2SPavel Emelyanov 	.d_revalidate	= map_files_d_revalidate,
1709640708a2SPavel Emelyanov 	.d_delete	= pid_delete_dentry,
1710640708a2SPavel Emelyanov };
1711640708a2SPavel Emelyanov 
1712640708a2SPavel Emelyanov static int proc_map_files_get_link(struct dentry *dentry, struct path *path)
1713640708a2SPavel Emelyanov {
1714640708a2SPavel Emelyanov 	unsigned long vm_start, vm_end;
1715640708a2SPavel Emelyanov 	struct vm_area_struct *vma;
1716640708a2SPavel Emelyanov 	struct task_struct *task;
1717640708a2SPavel Emelyanov 	struct mm_struct *mm;
1718640708a2SPavel Emelyanov 	int rc;
1719640708a2SPavel Emelyanov 
1720640708a2SPavel Emelyanov 	rc = -ENOENT;
1721640708a2SPavel Emelyanov 	task = get_proc_task(dentry->d_inode);
1722640708a2SPavel Emelyanov 	if (!task)
1723640708a2SPavel Emelyanov 		goto out;
1724640708a2SPavel Emelyanov 
1725640708a2SPavel Emelyanov 	mm = get_task_mm(task);
1726640708a2SPavel Emelyanov 	put_task_struct(task);
1727640708a2SPavel Emelyanov 	if (!mm)
1728640708a2SPavel Emelyanov 		goto out;
1729640708a2SPavel Emelyanov 
1730640708a2SPavel Emelyanov 	rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
1731640708a2SPavel Emelyanov 	if (rc)
1732640708a2SPavel Emelyanov 		goto out_mmput;
1733640708a2SPavel Emelyanov 
173470335abbSArtem Fetishev 	rc = -ENOENT;
1735640708a2SPavel Emelyanov 	down_read(&mm->mmap_sem);
1736640708a2SPavel Emelyanov 	vma = find_exact_vma(mm, vm_start, vm_end);
1737640708a2SPavel Emelyanov 	if (vma && vma->vm_file) {
1738640708a2SPavel Emelyanov 		*path = vma->vm_file->f_path;
1739640708a2SPavel Emelyanov 		path_get(path);
1740640708a2SPavel Emelyanov 		rc = 0;
1741640708a2SPavel Emelyanov 	}
1742640708a2SPavel Emelyanov 	up_read(&mm->mmap_sem);
1743640708a2SPavel Emelyanov 
1744640708a2SPavel Emelyanov out_mmput:
1745640708a2SPavel Emelyanov 	mmput(mm);
1746640708a2SPavel Emelyanov out:
1747640708a2SPavel Emelyanov 	return rc;
1748640708a2SPavel Emelyanov }
1749640708a2SPavel Emelyanov 
1750640708a2SPavel Emelyanov struct map_files_info {
17517b540d06SAl Viro 	fmode_t		mode;
1752640708a2SPavel Emelyanov 	unsigned long	len;
1753640708a2SPavel Emelyanov 	unsigned char	name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
1754640708a2SPavel Emelyanov };
1755640708a2SPavel Emelyanov 
1756c52a47acSAl Viro static int
1757640708a2SPavel Emelyanov proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
1758640708a2SPavel Emelyanov 			   struct task_struct *task, const void *ptr)
1759640708a2SPavel Emelyanov {
17607b540d06SAl Viro 	fmode_t mode = (fmode_t)(unsigned long)ptr;
1761640708a2SPavel Emelyanov 	struct proc_inode *ei;
1762640708a2SPavel Emelyanov 	struct inode *inode;
1763640708a2SPavel Emelyanov 
1764640708a2SPavel Emelyanov 	inode = proc_pid_make_inode(dir->i_sb, task);
1765640708a2SPavel Emelyanov 	if (!inode)
1766c52a47acSAl Viro 		return -ENOENT;
1767640708a2SPavel Emelyanov 
1768640708a2SPavel Emelyanov 	ei = PROC_I(inode);
1769640708a2SPavel Emelyanov 	ei->op.proc_get_link = proc_map_files_get_link;
1770640708a2SPavel Emelyanov 
1771640708a2SPavel Emelyanov 	inode->i_op = &proc_pid_link_inode_operations;
1772640708a2SPavel Emelyanov 	inode->i_size = 64;
1773640708a2SPavel Emelyanov 	inode->i_mode = S_IFLNK;
1774640708a2SPavel Emelyanov 
17757b540d06SAl Viro 	if (mode & FMODE_READ)
1776640708a2SPavel Emelyanov 		inode->i_mode |= S_IRUSR;
17777b540d06SAl Viro 	if (mode & FMODE_WRITE)
1778640708a2SPavel Emelyanov 		inode->i_mode |= S_IWUSR;
1779640708a2SPavel Emelyanov 
1780640708a2SPavel Emelyanov 	d_set_d_op(dentry, &tid_map_files_dentry_operations);
1781640708a2SPavel Emelyanov 	d_add(dentry, inode);
1782640708a2SPavel Emelyanov 
1783c52a47acSAl Viro 	return 0;
1784640708a2SPavel Emelyanov }
1785640708a2SPavel Emelyanov 
1786640708a2SPavel Emelyanov static struct dentry *proc_map_files_lookup(struct inode *dir,
178700cd8dd3SAl Viro 		struct dentry *dentry, unsigned int flags)
1788640708a2SPavel Emelyanov {
1789640708a2SPavel Emelyanov 	unsigned long vm_start, vm_end;
1790640708a2SPavel Emelyanov 	struct vm_area_struct *vma;
1791640708a2SPavel Emelyanov 	struct task_struct *task;
1792c52a47acSAl Viro 	int result;
1793640708a2SPavel Emelyanov 	struct mm_struct *mm;
1794640708a2SPavel Emelyanov 
1795c52a47acSAl Viro 	result = -EPERM;
1796640708a2SPavel Emelyanov 	if (!capable(CAP_SYS_ADMIN))
1797640708a2SPavel Emelyanov 		goto out;
1798640708a2SPavel Emelyanov 
1799c52a47acSAl Viro 	result = -ENOENT;
1800640708a2SPavel Emelyanov 	task = get_proc_task(dir);
1801640708a2SPavel Emelyanov 	if (!task)
1802640708a2SPavel Emelyanov 		goto out;
1803640708a2SPavel Emelyanov 
1804c52a47acSAl Viro 	result = -EACCES;
1805eb94cd96SCyrill Gorcunov 	if (!ptrace_may_access(task, PTRACE_MODE_READ))
1806640708a2SPavel Emelyanov 		goto out_put_task;
1807640708a2SPavel Emelyanov 
1808c52a47acSAl Viro 	result = -ENOENT;
1809640708a2SPavel Emelyanov 	if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
1810eb94cd96SCyrill Gorcunov 		goto out_put_task;
1811640708a2SPavel Emelyanov 
1812640708a2SPavel Emelyanov 	mm = get_task_mm(task);
1813640708a2SPavel Emelyanov 	if (!mm)
1814eb94cd96SCyrill Gorcunov 		goto out_put_task;
1815640708a2SPavel Emelyanov 
1816640708a2SPavel Emelyanov 	down_read(&mm->mmap_sem);
1817640708a2SPavel Emelyanov 	vma = find_exact_vma(mm, vm_start, vm_end);
1818640708a2SPavel Emelyanov 	if (!vma)
1819640708a2SPavel Emelyanov 		goto out_no_vma;
1820640708a2SPavel Emelyanov 
182105f56484SStanislav Kinsbursky 	if (vma->vm_file)
18227b540d06SAl Viro 		result = proc_map_files_instantiate(dir, dentry, task,
18237b540d06SAl Viro 				(void *)(unsigned long)vma->vm_file->f_mode);
1824640708a2SPavel Emelyanov 
1825640708a2SPavel Emelyanov out_no_vma:
1826640708a2SPavel Emelyanov 	up_read(&mm->mmap_sem);
1827640708a2SPavel Emelyanov 	mmput(mm);
1828640708a2SPavel Emelyanov out_put_task:
1829640708a2SPavel Emelyanov 	put_task_struct(task);
1830640708a2SPavel Emelyanov out:
1831c52a47acSAl Viro 	return ERR_PTR(result);
1832640708a2SPavel Emelyanov }
1833640708a2SPavel Emelyanov 
1834640708a2SPavel Emelyanov static const struct inode_operations proc_map_files_inode_operations = {
1835640708a2SPavel Emelyanov 	.lookup		= proc_map_files_lookup,
1836640708a2SPavel Emelyanov 	.permission	= proc_fd_permission,
1837640708a2SPavel Emelyanov 	.setattr	= proc_setattr,
1838640708a2SPavel Emelyanov };
1839640708a2SPavel Emelyanov 
1840640708a2SPavel Emelyanov static int
1841f0c3b509SAl Viro proc_map_files_readdir(struct file *file, struct dir_context *ctx)
1842640708a2SPavel Emelyanov {
1843640708a2SPavel Emelyanov 	struct vm_area_struct *vma;
1844640708a2SPavel Emelyanov 	struct task_struct *task;
1845640708a2SPavel Emelyanov 	struct mm_struct *mm;
1846f0c3b509SAl Viro 	unsigned long nr_files, pos, i;
1847f0c3b509SAl Viro 	struct flex_array *fa = NULL;
1848f0c3b509SAl Viro 	struct map_files_info info;
1849f0c3b509SAl Viro 	struct map_files_info *p;
1850640708a2SPavel Emelyanov 	int ret;
1851640708a2SPavel Emelyanov 
185241735818SZhao Hongjiang 	ret = -EPERM;
1853640708a2SPavel Emelyanov 	if (!capable(CAP_SYS_ADMIN))
1854640708a2SPavel Emelyanov 		goto out;
1855640708a2SPavel Emelyanov 
1856640708a2SPavel Emelyanov 	ret = -ENOENT;
1857f0c3b509SAl Viro 	task = get_proc_task(file_inode(file));
1858640708a2SPavel Emelyanov 	if (!task)
1859640708a2SPavel Emelyanov 		goto out;
1860640708a2SPavel Emelyanov 
1861640708a2SPavel Emelyanov 	ret = -EACCES;
1862eb94cd96SCyrill Gorcunov 	if (!ptrace_may_access(task, PTRACE_MODE_READ))
1863640708a2SPavel Emelyanov 		goto out_put_task;
1864640708a2SPavel Emelyanov 
1865640708a2SPavel Emelyanov 	ret = 0;
1866f0c3b509SAl Viro 	if (!dir_emit_dots(file, ctx))
1867eb94cd96SCyrill Gorcunov 		goto out_put_task;
1868640708a2SPavel Emelyanov 
1869640708a2SPavel Emelyanov 	mm = get_task_mm(task);
1870640708a2SPavel Emelyanov 	if (!mm)
1871eb94cd96SCyrill Gorcunov 		goto out_put_task;
1872640708a2SPavel Emelyanov 	down_read(&mm->mmap_sem);
1873640708a2SPavel Emelyanov 
1874640708a2SPavel Emelyanov 	nr_files = 0;
1875640708a2SPavel Emelyanov 
1876640708a2SPavel Emelyanov 	/*
1877640708a2SPavel Emelyanov 	 * We need two passes here:
1878640708a2SPavel Emelyanov 	 *
1879640708a2SPavel Emelyanov 	 *  1) Collect vmas of mapped files with mmap_sem taken
1880640708a2SPavel Emelyanov 	 *  2) Release mmap_sem and instantiate entries
1881640708a2SPavel Emelyanov 	 *
1882640708a2SPavel Emelyanov 	 * otherwise we get lockdep complained, since filldir()
1883640708a2SPavel Emelyanov 	 * routine might require mmap_sem taken in might_fault().
1884640708a2SPavel Emelyanov 	 */
1885640708a2SPavel Emelyanov 
1886640708a2SPavel Emelyanov 	for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
1887f0c3b509SAl Viro 		if (vma->vm_file && ++pos > ctx->pos)
1888640708a2SPavel Emelyanov 			nr_files++;
1889640708a2SPavel Emelyanov 	}
1890640708a2SPavel Emelyanov 
1891640708a2SPavel Emelyanov 	if (nr_files) {
1892640708a2SPavel Emelyanov 		fa = flex_array_alloc(sizeof(info), nr_files,
1893640708a2SPavel Emelyanov 					GFP_KERNEL);
1894640708a2SPavel Emelyanov 		if (!fa || flex_array_prealloc(fa, 0, nr_files,
1895640708a2SPavel Emelyanov 						GFP_KERNEL)) {
1896640708a2SPavel Emelyanov 			ret = -ENOMEM;
1897640708a2SPavel Emelyanov 			if (fa)
1898640708a2SPavel Emelyanov 				flex_array_free(fa);
1899640708a2SPavel Emelyanov 			up_read(&mm->mmap_sem);
1900640708a2SPavel Emelyanov 			mmput(mm);
1901eb94cd96SCyrill Gorcunov 			goto out_put_task;
1902640708a2SPavel Emelyanov 		}
1903640708a2SPavel Emelyanov 		for (i = 0, vma = mm->mmap, pos = 2; vma;
1904640708a2SPavel Emelyanov 				vma = vma->vm_next) {
1905640708a2SPavel Emelyanov 			if (!vma->vm_file)
1906640708a2SPavel Emelyanov 				continue;
1907f0c3b509SAl Viro 			if (++pos <= ctx->pos)
1908640708a2SPavel Emelyanov 				continue;
1909640708a2SPavel Emelyanov 
19107b540d06SAl Viro 			info.mode = vma->vm_file->f_mode;
1911640708a2SPavel Emelyanov 			info.len = snprintf(info.name,
1912640708a2SPavel Emelyanov 					sizeof(info.name), "%lx-%lx",
1913640708a2SPavel Emelyanov 					vma->vm_start, vma->vm_end);
1914640708a2SPavel Emelyanov 			if (flex_array_put(fa, i++, &info, GFP_KERNEL))
1915640708a2SPavel Emelyanov 				BUG();
1916640708a2SPavel Emelyanov 		}
1917640708a2SPavel Emelyanov 	}
1918640708a2SPavel Emelyanov 	up_read(&mm->mmap_sem);
1919640708a2SPavel Emelyanov 
1920640708a2SPavel Emelyanov 	for (i = 0; i < nr_files; i++) {
1921640708a2SPavel Emelyanov 		p = flex_array_get(fa, i);
1922f0c3b509SAl Viro 		if (!proc_fill_cache(file, ctx,
1923640708a2SPavel Emelyanov 				      p->name, p->len,
1924640708a2SPavel Emelyanov 				      proc_map_files_instantiate,
19257b540d06SAl Viro 				      task,
1926f0c3b509SAl Viro 				      (void *)(unsigned long)p->mode))
1927640708a2SPavel Emelyanov 			break;
1928f0c3b509SAl Viro 		ctx->pos++;
1929640708a2SPavel Emelyanov 	}
1930640708a2SPavel Emelyanov 	if (fa)
1931640708a2SPavel Emelyanov 		flex_array_free(fa);
1932640708a2SPavel Emelyanov 	mmput(mm);
1933640708a2SPavel Emelyanov 
1934640708a2SPavel Emelyanov out_put_task:
1935640708a2SPavel Emelyanov 	put_task_struct(task);
1936640708a2SPavel Emelyanov out:
1937640708a2SPavel Emelyanov 	return ret;
1938640708a2SPavel Emelyanov }
1939640708a2SPavel Emelyanov 
1940640708a2SPavel Emelyanov static const struct file_operations proc_map_files_operations = {
1941640708a2SPavel Emelyanov 	.read		= generic_read_dir,
1942f0c3b509SAl Viro 	.iterate	= proc_map_files_readdir,
1943640708a2SPavel Emelyanov 	.llseek		= default_llseek,
1944640708a2SPavel Emelyanov };
1945640708a2SPavel Emelyanov 
194648f6a7a5SPavel Emelyanov struct timers_private {
194748f6a7a5SPavel Emelyanov 	struct pid *pid;
194848f6a7a5SPavel Emelyanov 	struct task_struct *task;
194948f6a7a5SPavel Emelyanov 	struct sighand_struct *sighand;
195057b8015eSPavel Emelyanov 	struct pid_namespace *ns;
195148f6a7a5SPavel Emelyanov 	unsigned long flags;
195248f6a7a5SPavel Emelyanov };
195348f6a7a5SPavel Emelyanov 
195448f6a7a5SPavel Emelyanov static void *timers_start(struct seq_file *m, loff_t *pos)
195548f6a7a5SPavel Emelyanov {
195648f6a7a5SPavel Emelyanov 	struct timers_private *tp = m->private;
195748f6a7a5SPavel Emelyanov 
195848f6a7a5SPavel Emelyanov 	tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
195948f6a7a5SPavel Emelyanov 	if (!tp->task)
196048f6a7a5SPavel Emelyanov 		return ERR_PTR(-ESRCH);
196148f6a7a5SPavel Emelyanov 
196248f6a7a5SPavel Emelyanov 	tp->sighand = lock_task_sighand(tp->task, &tp->flags);
196348f6a7a5SPavel Emelyanov 	if (!tp->sighand)
196448f6a7a5SPavel Emelyanov 		return ERR_PTR(-ESRCH);
196548f6a7a5SPavel Emelyanov 
196648f6a7a5SPavel Emelyanov 	return seq_list_start(&tp->task->signal->posix_timers, *pos);
196748f6a7a5SPavel Emelyanov }
196848f6a7a5SPavel Emelyanov 
196948f6a7a5SPavel Emelyanov static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
197048f6a7a5SPavel Emelyanov {
197148f6a7a5SPavel Emelyanov 	struct timers_private *tp = m->private;
197248f6a7a5SPavel Emelyanov 	return seq_list_next(v, &tp->task->signal->posix_timers, pos);
197348f6a7a5SPavel Emelyanov }
197448f6a7a5SPavel Emelyanov 
197548f6a7a5SPavel Emelyanov static void timers_stop(struct seq_file *m, void *v)
197648f6a7a5SPavel Emelyanov {
197748f6a7a5SPavel Emelyanov 	struct timers_private *tp = m->private;
197848f6a7a5SPavel Emelyanov 
197948f6a7a5SPavel Emelyanov 	if (tp->sighand) {
198048f6a7a5SPavel Emelyanov 		unlock_task_sighand(tp->task, &tp->flags);
198148f6a7a5SPavel Emelyanov 		tp->sighand = NULL;
198248f6a7a5SPavel Emelyanov 	}
198348f6a7a5SPavel Emelyanov 
198448f6a7a5SPavel Emelyanov 	if (tp->task) {
198548f6a7a5SPavel Emelyanov 		put_task_struct(tp->task);
198648f6a7a5SPavel Emelyanov 		tp->task = NULL;
198748f6a7a5SPavel Emelyanov 	}
198848f6a7a5SPavel Emelyanov }
198948f6a7a5SPavel Emelyanov 
199048f6a7a5SPavel Emelyanov static int show_timer(struct seq_file *m, void *v)
199148f6a7a5SPavel Emelyanov {
199248f6a7a5SPavel Emelyanov 	struct k_itimer *timer;
199357b8015eSPavel Emelyanov 	struct timers_private *tp = m->private;
199457b8015eSPavel Emelyanov 	int notify;
1995cedbccabSAlexey Dobriyan 	static const char * const nstr[] = {
199657b8015eSPavel Emelyanov 		[SIGEV_SIGNAL] = "signal",
199757b8015eSPavel Emelyanov 		[SIGEV_NONE] = "none",
199857b8015eSPavel Emelyanov 		[SIGEV_THREAD] = "thread",
199957b8015eSPavel Emelyanov 	};
200048f6a7a5SPavel Emelyanov 
200148f6a7a5SPavel Emelyanov 	timer = list_entry((struct list_head *)v, struct k_itimer, list);
200257b8015eSPavel Emelyanov 	notify = timer->it_sigev_notify;
200357b8015eSPavel Emelyanov 
200448f6a7a5SPavel Emelyanov 	seq_printf(m, "ID: %d\n", timer->it_id);
200557b8015eSPavel Emelyanov 	seq_printf(m, "signal: %d/%p\n", timer->sigq->info.si_signo,
200657b8015eSPavel Emelyanov 			timer->sigq->info.si_value.sival_ptr);
200757b8015eSPavel Emelyanov 	seq_printf(m, "notify: %s/%s.%d\n",
200857b8015eSPavel Emelyanov 		nstr[notify & ~SIGEV_THREAD_ID],
200957b8015eSPavel Emelyanov 		(notify & SIGEV_THREAD_ID) ? "tid" : "pid",
201057b8015eSPavel Emelyanov 		pid_nr_ns(timer->it_pid, tp->ns));
201115ef0298SPavel Tikhomirov 	seq_printf(m, "ClockID: %d\n", timer->it_clock);
201248f6a7a5SPavel Emelyanov 
201348f6a7a5SPavel Emelyanov 	return 0;
201448f6a7a5SPavel Emelyanov }
201548f6a7a5SPavel Emelyanov 
201648f6a7a5SPavel Emelyanov static const struct seq_operations proc_timers_seq_ops = {
201748f6a7a5SPavel Emelyanov 	.start	= timers_start,
201848f6a7a5SPavel Emelyanov 	.next	= timers_next,
201948f6a7a5SPavel Emelyanov 	.stop	= timers_stop,
202048f6a7a5SPavel Emelyanov 	.show	= show_timer,
202148f6a7a5SPavel Emelyanov };
202248f6a7a5SPavel Emelyanov 
202348f6a7a5SPavel Emelyanov static int proc_timers_open(struct inode *inode, struct file *file)
202448f6a7a5SPavel Emelyanov {
202548f6a7a5SPavel Emelyanov 	struct timers_private *tp;
202648f6a7a5SPavel Emelyanov 
202748f6a7a5SPavel Emelyanov 	tp = __seq_open_private(file, &proc_timers_seq_ops,
202848f6a7a5SPavel Emelyanov 			sizeof(struct timers_private));
202948f6a7a5SPavel Emelyanov 	if (!tp)
203048f6a7a5SPavel Emelyanov 		return -ENOMEM;
203148f6a7a5SPavel Emelyanov 
203248f6a7a5SPavel Emelyanov 	tp->pid = proc_pid(inode);
203357b8015eSPavel Emelyanov 	tp->ns = inode->i_sb->s_fs_info;
203448f6a7a5SPavel Emelyanov 	return 0;
203548f6a7a5SPavel Emelyanov }
203648f6a7a5SPavel Emelyanov 
203748f6a7a5SPavel Emelyanov static const struct file_operations proc_timers_operations = {
203848f6a7a5SPavel Emelyanov 	.open		= proc_timers_open,
203948f6a7a5SPavel Emelyanov 	.read		= seq_read,
204048f6a7a5SPavel Emelyanov 	.llseek		= seq_lseek,
204148f6a7a5SPavel Emelyanov 	.release	= seq_release_private,
204248f6a7a5SPavel Emelyanov };
2043640708a2SPavel Emelyanov #endif /* CONFIG_CHECKPOINT_RESTORE */
2044640708a2SPavel Emelyanov 
2045c52a47acSAl Viro static int proc_pident_instantiate(struct inode *dir,
2046c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
2047444ceed8SEric W. Biederman {
2048c5141e6dSEric Dumazet 	const struct pid_entry *p = ptr;
2049444ceed8SEric W. Biederman 	struct inode *inode;
2050444ceed8SEric W. Biederman 	struct proc_inode *ei;
2051444ceed8SEric W. Biederman 
205261a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
2053444ceed8SEric W. Biederman 	if (!inode)
2054444ceed8SEric W. Biederman 		goto out;
2055444ceed8SEric W. Biederman 
2056444ceed8SEric W. Biederman 	ei = PROC_I(inode);
2057444ceed8SEric W. Biederman 	inode->i_mode = p->mode;
2058444ceed8SEric W. Biederman 	if (S_ISDIR(inode->i_mode))
2059bfe86848SMiklos Szeredi 		set_nlink(inode, 2);	/* Use getattr to fix if necessary */
2060444ceed8SEric W. Biederman 	if (p->iop)
2061444ceed8SEric W. Biederman 		inode->i_op = p->iop;
2062444ceed8SEric W. Biederman 	if (p->fop)
2063444ceed8SEric W. Biederman 		inode->i_fop = p->fop;
2064444ceed8SEric W. Biederman 	ei->op = p->op;
2065fb045adbSNick Piggin 	d_set_d_op(dentry, &pid_dentry_operations);
2066444ceed8SEric W. Biederman 	d_add(dentry, inode);
2067444ceed8SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
20680b728e19SAl Viro 	if (pid_revalidate(dentry, 0))
2069c52a47acSAl Viro 		return 0;
2070444ceed8SEric W. Biederman out:
2071c52a47acSAl Viro 	return -ENOENT;
2072444ceed8SEric W. Biederman }
2073444ceed8SEric W. Biederman 
20741da177e4SLinus Torvalds static struct dentry *proc_pident_lookup(struct inode *dir,
20751da177e4SLinus Torvalds 					 struct dentry *dentry,
2076c5141e6dSEric Dumazet 					 const struct pid_entry *ents,
20777bcd6b0eSEric W. Biederman 					 unsigned int nents)
20781da177e4SLinus Torvalds {
2079c52a47acSAl Viro 	int error;
208099f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(dir);
2081c5141e6dSEric Dumazet 	const struct pid_entry *p, *last;
20821da177e4SLinus Torvalds 
2083c52a47acSAl Viro 	error = -ENOENT;
20841da177e4SLinus Torvalds 
208599f89551SEric W. Biederman 	if (!task)
208699f89551SEric W. Biederman 		goto out_no_task;
20871da177e4SLinus Torvalds 
208820cdc894SEric W. Biederman 	/*
208920cdc894SEric W. Biederman 	 * Yes, it does not scale. And it should not. Don't add
209020cdc894SEric W. Biederman 	 * new entries into /proc/<tgid>/ without very good reasons.
209120cdc894SEric W. Biederman 	 */
20927bcd6b0eSEric W. Biederman 	last = &ents[nents - 1];
20937bcd6b0eSEric W. Biederman 	for (p = ents; p <= last; p++) {
20941da177e4SLinus Torvalds 		if (p->len != dentry->d_name.len)
20951da177e4SLinus Torvalds 			continue;
20961da177e4SLinus Torvalds 		if (!memcmp(dentry->d_name.name, p->name, p->len))
20971da177e4SLinus Torvalds 			break;
20981da177e4SLinus Torvalds 	}
20997bcd6b0eSEric W. Biederman 	if (p > last)
21001da177e4SLinus Torvalds 		goto out;
21011da177e4SLinus Torvalds 
2102444ceed8SEric W. Biederman 	error = proc_pident_instantiate(dir, dentry, task, p);
21031da177e4SLinus Torvalds out:
210499f89551SEric W. Biederman 	put_task_struct(task);
210599f89551SEric W. Biederman out_no_task:
2106c52a47acSAl Viro 	return ERR_PTR(error);
21071da177e4SLinus Torvalds }
21081da177e4SLinus Torvalds 
2109f0c3b509SAl Viro static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
2110c5141e6dSEric Dumazet 		const struct pid_entry *ents, unsigned int nents)
211128a6d671SEric W. Biederman {
2112f0c3b509SAl Viro 	struct task_struct *task = get_proc_task(file_inode(file));
2113f0c3b509SAl Viro 	const struct pid_entry *p;
211428a6d671SEric W. Biederman 
211528a6d671SEric W. Biederman 	if (!task)
2116f0c3b509SAl Viro 		return -ENOENT;
211728a6d671SEric W. Biederman 
2118f0c3b509SAl Viro 	if (!dir_emit_dots(file, ctx))
211928a6d671SEric W. Biederman 		goto out;
21201da177e4SLinus Torvalds 
2121f0c3b509SAl Viro 	if (ctx->pos >= nents + 2)
2122f0c3b509SAl Viro 		goto out;
2123f0c3b509SAl Viro 
2124f0c3b509SAl Viro 	for (p = ents + (ctx->pos - 2); p <= ents + nents - 1; p++) {
2125f0c3b509SAl Viro 		if (!proc_fill_cache(file, ctx, p->name, p->len,
2126f0c3b509SAl Viro 				proc_pident_instantiate, task, p))
2127f0c3b509SAl Viro 			break;
2128f0c3b509SAl Viro 		ctx->pos++;
2129f0c3b509SAl Viro 	}
213028a6d671SEric W. Biederman out:
213161a28784SEric W. Biederman 	put_task_struct(task);
2132f0c3b509SAl Viro 	return 0;
21331da177e4SLinus Torvalds }
21341da177e4SLinus Torvalds 
21351da177e4SLinus Torvalds #ifdef CONFIG_SECURITY
213628a6d671SEric W. Biederman static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
213728a6d671SEric W. Biederman 				  size_t count, loff_t *ppos)
213828a6d671SEric W. Biederman {
2139496ad9aaSAl Viro 	struct inode * inode = file_inode(file);
214004ff9708SAl Viro 	char *p = NULL;
214128a6d671SEric W. Biederman 	ssize_t length;
214228a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
214328a6d671SEric W. Biederman 
214428a6d671SEric W. Biederman 	if (!task)
214504ff9708SAl Viro 		return -ESRCH;
214628a6d671SEric W. Biederman 
214728a6d671SEric W. Biederman 	length = security_getprocattr(task,
21482fddfeefSJosef "Jeff" Sipek 				      (char*)file->f_path.dentry->d_name.name,
214904ff9708SAl Viro 				      &p);
215028a6d671SEric W. Biederman 	put_task_struct(task);
215104ff9708SAl Viro 	if (length > 0)
215204ff9708SAl Viro 		length = simple_read_from_buffer(buf, count, ppos, p, length);
215304ff9708SAl Viro 	kfree(p);
215428a6d671SEric W. Biederman 	return length;
215528a6d671SEric W. Biederman }
215628a6d671SEric W. Biederman 
215728a6d671SEric W. Biederman static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
215828a6d671SEric W. Biederman 				   size_t count, loff_t *ppos)
215928a6d671SEric W. Biederman {
2160496ad9aaSAl Viro 	struct inode * inode = file_inode(file);
216128a6d671SEric W. Biederman 	char *page;
216228a6d671SEric W. Biederman 	ssize_t length;
216328a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
216428a6d671SEric W. Biederman 
216528a6d671SEric W. Biederman 	length = -ESRCH;
216628a6d671SEric W. Biederman 	if (!task)
216728a6d671SEric W. Biederman 		goto out_no_task;
216828a6d671SEric W. Biederman 	if (count > PAGE_SIZE)
216928a6d671SEric W. Biederman 		count = PAGE_SIZE;
217028a6d671SEric W. Biederman 
217128a6d671SEric W. Biederman 	/* No partial writes. */
217228a6d671SEric W. Biederman 	length = -EINVAL;
217328a6d671SEric W. Biederman 	if (*ppos != 0)
217428a6d671SEric W. Biederman 		goto out;
217528a6d671SEric W. Biederman 
217628a6d671SEric W. Biederman 	length = -ENOMEM;
2177e12ba74dSMel Gorman 	page = (char*)__get_free_page(GFP_TEMPORARY);
217828a6d671SEric W. Biederman 	if (!page)
217928a6d671SEric W. Biederman 		goto out;
218028a6d671SEric W. Biederman 
218128a6d671SEric W. Biederman 	length = -EFAULT;
218228a6d671SEric W. Biederman 	if (copy_from_user(page, buf, count))
218328a6d671SEric W. Biederman 		goto out_free;
218428a6d671SEric W. Biederman 
2185107db7c7SDavid Howells 	/* Guard against adverse ptrace interaction */
21869b1bf12dSKOSAKI Motohiro 	length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
2187107db7c7SDavid Howells 	if (length < 0)
2188107db7c7SDavid Howells 		goto out_free;
2189107db7c7SDavid Howells 
219028a6d671SEric W. Biederman 	length = security_setprocattr(task,
21912fddfeefSJosef "Jeff" Sipek 				      (char*)file->f_path.dentry->d_name.name,
219228a6d671SEric W. Biederman 				      (void*)page, count);
21939b1bf12dSKOSAKI Motohiro 	mutex_unlock(&task->signal->cred_guard_mutex);
219428a6d671SEric W. Biederman out_free:
219528a6d671SEric W. Biederman 	free_page((unsigned long) page);
219628a6d671SEric W. Biederman out:
219728a6d671SEric W. Biederman 	put_task_struct(task);
219828a6d671SEric W. Biederman out_no_task:
219928a6d671SEric W. Biederman 	return length;
220028a6d671SEric W. Biederman }
220128a6d671SEric W. Biederman 
220200977a59SArjan van de Ven static const struct file_operations proc_pid_attr_operations = {
220328a6d671SEric W. Biederman 	.read		= proc_pid_attr_read,
220428a6d671SEric W. Biederman 	.write		= proc_pid_attr_write,
220587df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
220628a6d671SEric W. Biederman };
220728a6d671SEric W. Biederman 
2208c5141e6dSEric Dumazet static const struct pid_entry attr_dir_stuff[] = {
2209631f9c18SAlexey Dobriyan 	REG("current",    S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2210631f9c18SAlexey Dobriyan 	REG("prev",       S_IRUGO,	   proc_pid_attr_operations),
2211631f9c18SAlexey Dobriyan 	REG("exec",       S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2212631f9c18SAlexey Dobriyan 	REG("fscreate",   S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2213631f9c18SAlexey Dobriyan 	REG("keycreate",  S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2214631f9c18SAlexey Dobriyan 	REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
221528a6d671SEric W. Biederman };
221628a6d671SEric W. Biederman 
2217f0c3b509SAl Viro static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
22181da177e4SLinus Torvalds {
2219f0c3b509SAl Viro 	return proc_pident_readdir(file, ctx,
222072d9dcfcSEric W. Biederman 				   attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
22211da177e4SLinus Torvalds }
22221da177e4SLinus Torvalds 
222300977a59SArjan van de Ven static const struct file_operations proc_attr_dir_operations = {
22241da177e4SLinus Torvalds 	.read		= generic_read_dir,
2225f0c3b509SAl Viro 	.iterate	= proc_attr_dir_readdir,
22266038f373SArnd Bergmann 	.llseek		= default_llseek,
22271da177e4SLinus Torvalds };
22281da177e4SLinus Torvalds 
222972d9dcfcSEric W. Biederman static struct dentry *proc_attr_dir_lookup(struct inode *dir,
223000cd8dd3SAl Viro 				struct dentry *dentry, unsigned int flags)
22311da177e4SLinus Torvalds {
22327bcd6b0eSEric W. Biederman 	return proc_pident_lookup(dir, dentry,
22337bcd6b0eSEric W. Biederman 				  attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
22341da177e4SLinus Torvalds }
22351da177e4SLinus Torvalds 
2236c5ef1c42SArjan van de Ven static const struct inode_operations proc_attr_dir_inode_operations = {
223772d9dcfcSEric W. Biederman 	.lookup		= proc_attr_dir_lookup,
223899f89551SEric W. Biederman 	.getattr	= pid_getattr,
22396d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
22401da177e4SLinus Torvalds };
22411da177e4SLinus Torvalds 
22421da177e4SLinus Torvalds #endif
22431da177e4SLinus Torvalds 
2244698ba7b5SChristoph Hellwig #ifdef CONFIG_ELF_CORE
22453cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
22463cb4a0bbSKawai, Hidehiro 					 size_t count, loff_t *ppos)
22473cb4a0bbSKawai, Hidehiro {
2248496ad9aaSAl Viro 	struct task_struct *task = get_proc_task(file_inode(file));
22493cb4a0bbSKawai, Hidehiro 	struct mm_struct *mm;
22503cb4a0bbSKawai, Hidehiro 	char buffer[PROC_NUMBUF];
22513cb4a0bbSKawai, Hidehiro 	size_t len;
22523cb4a0bbSKawai, Hidehiro 	int ret;
22533cb4a0bbSKawai, Hidehiro 
22543cb4a0bbSKawai, Hidehiro 	if (!task)
22553cb4a0bbSKawai, Hidehiro 		return -ESRCH;
22563cb4a0bbSKawai, Hidehiro 
22573cb4a0bbSKawai, Hidehiro 	ret = 0;
22583cb4a0bbSKawai, Hidehiro 	mm = get_task_mm(task);
22593cb4a0bbSKawai, Hidehiro 	if (mm) {
22603cb4a0bbSKawai, Hidehiro 		len = snprintf(buffer, sizeof(buffer), "%08lx\n",
22613cb4a0bbSKawai, Hidehiro 			       ((mm->flags & MMF_DUMP_FILTER_MASK) >>
22623cb4a0bbSKawai, Hidehiro 				MMF_DUMP_FILTER_SHIFT));
22633cb4a0bbSKawai, Hidehiro 		mmput(mm);
22643cb4a0bbSKawai, Hidehiro 		ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
22653cb4a0bbSKawai, Hidehiro 	}
22663cb4a0bbSKawai, Hidehiro 
22673cb4a0bbSKawai, Hidehiro 	put_task_struct(task);
22683cb4a0bbSKawai, Hidehiro 
22693cb4a0bbSKawai, Hidehiro 	return ret;
22703cb4a0bbSKawai, Hidehiro }
22713cb4a0bbSKawai, Hidehiro 
22723cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_write(struct file *file,
22733cb4a0bbSKawai, Hidehiro 					  const char __user *buf,
22743cb4a0bbSKawai, Hidehiro 					  size_t count,
22753cb4a0bbSKawai, Hidehiro 					  loff_t *ppos)
22763cb4a0bbSKawai, Hidehiro {
22773cb4a0bbSKawai, Hidehiro 	struct task_struct *task;
22783cb4a0bbSKawai, Hidehiro 	struct mm_struct *mm;
22793cb4a0bbSKawai, Hidehiro 	char buffer[PROC_NUMBUF], *end;
22803cb4a0bbSKawai, Hidehiro 	unsigned int val;
22813cb4a0bbSKawai, Hidehiro 	int ret;
22823cb4a0bbSKawai, Hidehiro 	int i;
22833cb4a0bbSKawai, Hidehiro 	unsigned long mask;
22843cb4a0bbSKawai, Hidehiro 
22853cb4a0bbSKawai, Hidehiro 	ret = -EFAULT;
22863cb4a0bbSKawai, Hidehiro 	memset(buffer, 0, sizeof(buffer));
22873cb4a0bbSKawai, Hidehiro 	if (count > sizeof(buffer) - 1)
22883cb4a0bbSKawai, Hidehiro 		count = sizeof(buffer) - 1;
22893cb4a0bbSKawai, Hidehiro 	if (copy_from_user(buffer, buf, count))
22903cb4a0bbSKawai, Hidehiro 		goto out_no_task;
22913cb4a0bbSKawai, Hidehiro 
22923cb4a0bbSKawai, Hidehiro 	ret = -EINVAL;
22933cb4a0bbSKawai, Hidehiro 	val = (unsigned int)simple_strtoul(buffer, &end, 0);
22943cb4a0bbSKawai, Hidehiro 	if (*end == '\n')
22953cb4a0bbSKawai, Hidehiro 		end++;
22963cb4a0bbSKawai, Hidehiro 	if (end - buffer == 0)
22973cb4a0bbSKawai, Hidehiro 		goto out_no_task;
22983cb4a0bbSKawai, Hidehiro 
22993cb4a0bbSKawai, Hidehiro 	ret = -ESRCH;
2300496ad9aaSAl Viro 	task = get_proc_task(file_inode(file));
23013cb4a0bbSKawai, Hidehiro 	if (!task)
23023cb4a0bbSKawai, Hidehiro 		goto out_no_task;
23033cb4a0bbSKawai, Hidehiro 
23043cb4a0bbSKawai, Hidehiro 	ret = end - buffer;
23053cb4a0bbSKawai, Hidehiro 	mm = get_task_mm(task);
23063cb4a0bbSKawai, Hidehiro 	if (!mm)
23073cb4a0bbSKawai, Hidehiro 		goto out_no_mm;
23083cb4a0bbSKawai, Hidehiro 
23093cb4a0bbSKawai, Hidehiro 	for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
23103cb4a0bbSKawai, Hidehiro 		if (val & mask)
23113cb4a0bbSKawai, Hidehiro 			set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
23123cb4a0bbSKawai, Hidehiro 		else
23133cb4a0bbSKawai, Hidehiro 			clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
23143cb4a0bbSKawai, Hidehiro 	}
23153cb4a0bbSKawai, Hidehiro 
23163cb4a0bbSKawai, Hidehiro 	mmput(mm);
23173cb4a0bbSKawai, Hidehiro  out_no_mm:
23183cb4a0bbSKawai, Hidehiro 	put_task_struct(task);
23193cb4a0bbSKawai, Hidehiro  out_no_task:
23203cb4a0bbSKawai, Hidehiro 	return ret;
23213cb4a0bbSKawai, Hidehiro }
23223cb4a0bbSKawai, Hidehiro 
23233cb4a0bbSKawai, Hidehiro static const struct file_operations proc_coredump_filter_operations = {
23243cb4a0bbSKawai, Hidehiro 	.read		= proc_coredump_filter_read,
23253cb4a0bbSKawai, Hidehiro 	.write		= proc_coredump_filter_write,
232687df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
23273cb4a0bbSKawai, Hidehiro };
23283cb4a0bbSKawai, Hidehiro #endif
23293cb4a0bbSKawai, Hidehiro 
2330aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING
233119aadc98SAlexey Dobriyan static int do_io_accounting(struct task_struct *task, struct seq_file *m, int whole)
2332aba76fdbSAndrew Morton {
2333940389b8SAndrea Righi 	struct task_io_accounting acct = task->ioac;
2334297c5d92SAndrea Righi 	unsigned long flags;
2335293eb1e7SVasiliy Kulikov 	int result;
2336297c5d92SAndrea Righi 
2337293eb1e7SVasiliy Kulikov 	result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2338293eb1e7SVasiliy Kulikov 	if (result)
2339293eb1e7SVasiliy Kulikov 		return result;
2340293eb1e7SVasiliy Kulikov 
2341293eb1e7SVasiliy Kulikov 	if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
2342293eb1e7SVasiliy Kulikov 		result = -EACCES;
2343293eb1e7SVasiliy Kulikov 		goto out_unlock;
2344293eb1e7SVasiliy Kulikov 	}
23451d1221f3SVasiliy Kulikov 
23465995477aSAndrea Righi 	if (whole && lock_task_sighand(task, &flags)) {
2347b2d002dbSAndrea Righi 		struct task_struct *t = task;
2348297c5d92SAndrea Righi 
23495995477aSAndrea Righi 		task_io_accounting_add(&acct, &task->signal->ioac);
23505995477aSAndrea Righi 		while_each_thread(task, t)
23515995477aSAndrea Righi 			task_io_accounting_add(&acct, &t->ioac);
2352297c5d92SAndrea Righi 
2353297c5d92SAndrea Righi 		unlock_task_sighand(task, &flags);
2354297c5d92SAndrea Righi 	}
235519aadc98SAlexey Dobriyan 	result = seq_printf(m,
2356aba76fdbSAndrew Morton 			"rchar: %llu\n"
2357aba76fdbSAndrew Morton 			"wchar: %llu\n"
2358aba76fdbSAndrew Morton 			"syscr: %llu\n"
2359aba76fdbSAndrew Morton 			"syscw: %llu\n"
2360aba76fdbSAndrew Morton 			"read_bytes: %llu\n"
2361aba76fdbSAndrew Morton 			"write_bytes: %llu\n"
2362aba76fdbSAndrew Morton 			"cancelled_write_bytes: %llu\n",
23637c44319dSAlexander Beregalov 			(unsigned long long)acct.rchar,
23647c44319dSAlexander Beregalov 			(unsigned long long)acct.wchar,
23657c44319dSAlexander Beregalov 			(unsigned long long)acct.syscr,
23667c44319dSAlexander Beregalov 			(unsigned long long)acct.syscw,
23677c44319dSAlexander Beregalov 			(unsigned long long)acct.read_bytes,
23687c44319dSAlexander Beregalov 			(unsigned long long)acct.write_bytes,
23697c44319dSAlexander Beregalov 			(unsigned long long)acct.cancelled_write_bytes);
2370293eb1e7SVasiliy Kulikov out_unlock:
2371293eb1e7SVasiliy Kulikov 	mutex_unlock(&task->signal->cred_guard_mutex);
2372293eb1e7SVasiliy Kulikov 	return result;
2373aba76fdbSAndrew Morton }
2374297c5d92SAndrea Righi 
237519aadc98SAlexey Dobriyan static int proc_tid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
237619aadc98SAlexey Dobriyan 				  struct pid *pid, struct task_struct *task)
2377297c5d92SAndrea Righi {
237819aadc98SAlexey Dobriyan 	return do_io_accounting(task, m, 0);
2379297c5d92SAndrea Righi }
2380297c5d92SAndrea Righi 
238119aadc98SAlexey Dobriyan static int proc_tgid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
238219aadc98SAlexey Dobriyan 				   struct pid *pid, struct task_struct *task)
2383297c5d92SAndrea Righi {
238419aadc98SAlexey Dobriyan 	return do_io_accounting(task, m, 1);
2385297c5d92SAndrea Righi }
2386297c5d92SAndrea Righi #endif /* CONFIG_TASK_IO_ACCOUNTING */
2387aba76fdbSAndrew Morton 
238822d917d8SEric W. Biederman #ifdef CONFIG_USER_NS
238922d917d8SEric W. Biederman static int proc_id_map_open(struct inode *inode, struct file *file,
2390ccf94f1bSFabian Frederick 	const struct seq_operations *seq_ops)
239122d917d8SEric W. Biederman {
239222d917d8SEric W. Biederman 	struct user_namespace *ns = NULL;
239322d917d8SEric W. Biederman 	struct task_struct *task;
239422d917d8SEric W. Biederman 	struct seq_file *seq;
239522d917d8SEric W. Biederman 	int ret = -EINVAL;
239622d917d8SEric W. Biederman 
239722d917d8SEric W. Biederman 	task = get_proc_task(inode);
239822d917d8SEric W. Biederman 	if (task) {
239922d917d8SEric W. Biederman 		rcu_read_lock();
240022d917d8SEric W. Biederman 		ns = get_user_ns(task_cred_xxx(task, user_ns));
240122d917d8SEric W. Biederman 		rcu_read_unlock();
240222d917d8SEric W. Biederman 		put_task_struct(task);
240322d917d8SEric W. Biederman 	}
240422d917d8SEric W. Biederman 	if (!ns)
240522d917d8SEric W. Biederman 		goto err;
240622d917d8SEric W. Biederman 
240722d917d8SEric W. Biederman 	ret = seq_open(file, seq_ops);
240822d917d8SEric W. Biederman 	if (ret)
240922d917d8SEric W. Biederman 		goto err_put_ns;
241022d917d8SEric W. Biederman 
241122d917d8SEric W. Biederman 	seq = file->private_data;
241222d917d8SEric W. Biederman 	seq->private = ns;
241322d917d8SEric W. Biederman 
241422d917d8SEric W. Biederman 	return 0;
241522d917d8SEric W. Biederman err_put_ns:
241622d917d8SEric W. Biederman 	put_user_ns(ns);
241722d917d8SEric W. Biederman err:
241822d917d8SEric W. Biederman 	return ret;
241922d917d8SEric W. Biederman }
242022d917d8SEric W. Biederman 
242122d917d8SEric W. Biederman static int proc_id_map_release(struct inode *inode, struct file *file)
242222d917d8SEric W. Biederman {
242322d917d8SEric W. Biederman 	struct seq_file *seq = file->private_data;
242422d917d8SEric W. Biederman 	struct user_namespace *ns = seq->private;
242522d917d8SEric W. Biederman 	put_user_ns(ns);
242622d917d8SEric W. Biederman 	return seq_release(inode, file);
242722d917d8SEric W. Biederman }
242822d917d8SEric W. Biederman 
242922d917d8SEric W. Biederman static int proc_uid_map_open(struct inode *inode, struct file *file)
243022d917d8SEric W. Biederman {
243122d917d8SEric W. Biederman 	return proc_id_map_open(inode, file, &proc_uid_seq_operations);
243222d917d8SEric W. Biederman }
243322d917d8SEric W. Biederman 
243422d917d8SEric W. Biederman static int proc_gid_map_open(struct inode *inode, struct file *file)
243522d917d8SEric W. Biederman {
243622d917d8SEric W. Biederman 	return proc_id_map_open(inode, file, &proc_gid_seq_operations);
243722d917d8SEric W. Biederman }
243822d917d8SEric W. Biederman 
2439f76d207aSEric W. Biederman static int proc_projid_map_open(struct inode *inode, struct file *file)
2440f76d207aSEric W. Biederman {
2441f76d207aSEric W. Biederman 	return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2442f76d207aSEric W. Biederman }
2443f76d207aSEric W. Biederman 
244422d917d8SEric W. Biederman static const struct file_operations proc_uid_map_operations = {
244522d917d8SEric W. Biederman 	.open		= proc_uid_map_open,
244622d917d8SEric W. Biederman 	.write		= proc_uid_map_write,
244722d917d8SEric W. Biederman 	.read		= seq_read,
244822d917d8SEric W. Biederman 	.llseek		= seq_lseek,
244922d917d8SEric W. Biederman 	.release	= proc_id_map_release,
245022d917d8SEric W. Biederman };
245122d917d8SEric W. Biederman 
245222d917d8SEric W. Biederman static const struct file_operations proc_gid_map_operations = {
245322d917d8SEric W. Biederman 	.open		= proc_gid_map_open,
245422d917d8SEric W. Biederman 	.write		= proc_gid_map_write,
245522d917d8SEric W. Biederman 	.read		= seq_read,
245622d917d8SEric W. Biederman 	.llseek		= seq_lseek,
245722d917d8SEric W. Biederman 	.release	= proc_id_map_release,
245822d917d8SEric W. Biederman };
2459f76d207aSEric W. Biederman 
2460f76d207aSEric W. Biederman static const struct file_operations proc_projid_map_operations = {
2461f76d207aSEric W. Biederman 	.open		= proc_projid_map_open,
2462f76d207aSEric W. Biederman 	.write		= proc_projid_map_write,
2463f76d207aSEric W. Biederman 	.read		= seq_read,
2464f76d207aSEric W. Biederman 	.llseek		= seq_lseek,
2465f76d207aSEric W. Biederman 	.release	= proc_id_map_release,
2466f76d207aSEric W. Biederman };
246722d917d8SEric W. Biederman #endif /* CONFIG_USER_NS */
246822d917d8SEric W. Biederman 
246947830723SKees Cook static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
247047830723SKees Cook 				struct pid *pid, struct task_struct *task)
247147830723SKees Cook {
2472a9712bc1SAl Viro 	int err = lock_trace(task);
2473a9712bc1SAl Viro 	if (!err) {
247447830723SKees Cook 		seq_printf(m, "%08x\n", task->personality);
2475a9712bc1SAl Viro 		unlock_trace(task);
2476a9712bc1SAl Viro 	}
2477a9712bc1SAl Viro 	return err;
247847830723SKees Cook }
247947830723SKees Cook 
2480801199ceSEric W. Biederman /*
248128a6d671SEric W. Biederman  * Thread groups
248228a6d671SEric W. Biederman  */
248300977a59SArjan van de Ven static const struct file_operations proc_task_operations;
2484c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations;
248520cdc894SEric W. Biederman 
2486c5141e6dSEric Dumazet static const struct pid_entry tgid_base_stuff[] = {
2487631f9c18SAlexey Dobriyan 	DIR("task",       S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2488631f9c18SAlexey Dobriyan 	DIR("fd",         S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2489640708a2SPavel Emelyanov #ifdef CONFIG_CHECKPOINT_RESTORE
2490640708a2SPavel Emelyanov 	DIR("map_files",  S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
2491640708a2SPavel Emelyanov #endif
2492631f9c18SAlexey Dobriyan 	DIR("fdinfo",     S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
24936b4e306aSEric W. Biederman 	DIR("ns",	  S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
2494b2211a36SAndrew Morton #ifdef CONFIG_NET
2495631f9c18SAlexey Dobriyan 	DIR("net",        S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2496b2211a36SAndrew Morton #endif
2497631f9c18SAlexey Dobriyan 	REG("environ",    S_IRUSR, proc_environ_operations),
2498f9ea536eSAlexey Dobriyan 	ONE("auxv",       S_IRUSR, proc_pid_auxv),
2499631f9c18SAlexey Dobriyan 	ONE("status",     S_IRUGO, proc_pid_status),
250035a35046SDjalal Harouni 	ONE("personality", S_IRUSR, proc_pid_personality),
25011c963eb1SAlexey Dobriyan 	ONE("limits",	  S_IRUGO, proc_pid_limits),
250243ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
2503631f9c18SAlexey Dobriyan 	REG("sched",      S_IRUGO|S_IWUSR, proc_pid_sched_operations),
250443ae34cbSIngo Molnar #endif
25055091faa4SMike Galbraith #ifdef CONFIG_SCHED_AUTOGROUP
25065091faa4SMike Galbraith 	REG("autogroup",  S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
25075091faa4SMike Galbraith #endif
25084614a696Sjohn stultz 	REG("comm",      S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2509ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
251009d93bd6SAlexey Dobriyan 	ONE("syscall",    S_IRUSR, proc_pid_syscall),
2511ebcb6734SRoland McGrath #endif
25122ca66ff7SAlexey Dobriyan 	ONE("cmdline",    S_IRUGO, proc_pid_cmdline),
2513631f9c18SAlexey Dobriyan 	ONE("stat",       S_IRUGO, proc_tgid_stat),
2514631f9c18SAlexey Dobriyan 	ONE("statm",      S_IRUGO, proc_pid_statm),
2515b7643757SSiddhesh Poyarekar 	REG("maps",       S_IRUGO, proc_pid_maps_operations),
251628a6d671SEric W. Biederman #ifdef CONFIG_NUMA
2517b7643757SSiddhesh Poyarekar 	REG("numa_maps",  S_IRUGO, proc_pid_numa_maps_operations),
251828a6d671SEric W. Biederman #endif
2519631f9c18SAlexey Dobriyan 	REG("mem",        S_IRUSR|S_IWUSR, proc_mem_operations),
2520631f9c18SAlexey Dobriyan 	LNK("cwd",        proc_cwd_link),
2521631f9c18SAlexey Dobriyan 	LNK("root",       proc_root_link),
2522631f9c18SAlexey Dobriyan 	LNK("exe",        proc_exe_link),
2523631f9c18SAlexey Dobriyan 	REG("mounts",     S_IRUGO, proc_mounts_operations),
2524631f9c18SAlexey Dobriyan 	REG("mountinfo",  S_IRUGO, proc_mountinfo_operations),
2525631f9c18SAlexey Dobriyan 	REG("mountstats", S_IRUSR, proc_mountstats_operations),
25261e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR
2527631f9c18SAlexey Dobriyan 	REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2528b7643757SSiddhesh Poyarekar 	REG("smaps",      S_IRUGO, proc_pid_smaps_operations),
252932ed74a4SDjalal Harouni 	REG("pagemap",    S_IRUSR, proc_pagemap_operations),
253028a6d671SEric W. Biederman #endif
253128a6d671SEric W. Biederman #ifdef CONFIG_SECURITY
2532631f9c18SAlexey Dobriyan 	DIR("attr",       S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
253328a6d671SEric W. Biederman #endif
253428a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS
2535edfcd606SAlexey Dobriyan 	ONE("wchan",      S_IRUGO, proc_pid_wchan),
253628a6d671SEric W. Biederman #endif
25372ec220e2SKen Chen #ifdef CONFIG_STACKTRACE
253835a35046SDjalal Harouni 	ONE("stack",      S_IRUSR, proc_pid_stack),
253928a6d671SEric W. Biederman #endif
254028a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS
2541f6e826caSAlexey Dobriyan 	ONE("schedstat",  S_IRUGO, proc_pid_schedstat),
254228a6d671SEric W. Biederman #endif
25439745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP
2544631f9c18SAlexey Dobriyan 	REG("latency",  S_IRUGO, proc_lstats_operations),
25459745512cSArjan van de Ven #endif
25468793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET
254752de4779SZefan Li 	ONE("cpuset",     S_IRUGO, proc_cpuset_show),
254828a6d671SEric W. Biederman #endif
2549a424316cSPaul Menage #ifdef CONFIG_CGROUPS
2550006f4ac4SZefan Li 	ONE("cgroup",  S_IRUGO, proc_cgroup_show),
2551a424316cSPaul Menage #endif
25526ba51e37SAlexey Dobriyan 	ONE("oom_score",  S_IRUGO, proc_oom_score),
2553fa0cbbf1SDavid Rientjes 	REG("oom_adj",    S_IRUGO|S_IWUSR, proc_oom_adj_operations),
2554a63d83f4SDavid Rientjes 	REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
255528a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL
2556631f9c18SAlexey Dobriyan 	REG("loginuid",   S_IWUSR|S_IRUGO, proc_loginuid_operations),
2557631f9c18SAlexey Dobriyan 	REG("sessionid",  S_IRUGO, proc_sessionid_operations),
255828a6d671SEric W. Biederman #endif
2559f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
2560631f9c18SAlexey Dobriyan 	REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2561f4f154fdSAkinobu Mita #endif
2562698ba7b5SChristoph Hellwig #ifdef CONFIG_ELF_CORE
2563631f9c18SAlexey Dobriyan 	REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
25643cb4a0bbSKawai, Hidehiro #endif
2565aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING
256619aadc98SAlexey Dobriyan 	ONE("io",	S_IRUSR, proc_tgid_io_accounting),
2567aba76fdbSAndrew Morton #endif
2568f133eccaSChris Metcalf #ifdef CONFIG_HARDWALL
2569d962c144SAlexey Dobriyan 	ONE("hardwall",   S_IRUGO, proc_pid_hardwall),
2570f133eccaSChris Metcalf #endif
257122d917d8SEric W. Biederman #ifdef CONFIG_USER_NS
257222d917d8SEric W. Biederman 	REG("uid_map",    S_IRUGO|S_IWUSR, proc_uid_map_operations),
257322d917d8SEric W. Biederman 	REG("gid_map",    S_IRUGO|S_IWUSR, proc_gid_map_operations),
2574f76d207aSEric W. Biederman 	REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
257522d917d8SEric W. Biederman #endif
257648f6a7a5SPavel Emelyanov #ifdef CONFIG_CHECKPOINT_RESTORE
257748f6a7a5SPavel Emelyanov 	REG("timers",	  S_IRUGO, proc_timers_operations),
257848f6a7a5SPavel Emelyanov #endif
257928a6d671SEric W. Biederman };
258028a6d671SEric W. Biederman 
2581f0c3b509SAl Viro static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
258228a6d671SEric W. Biederman {
2583f0c3b509SAl Viro 	return proc_pident_readdir(file, ctx,
258428a6d671SEric W. Biederman 				   tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
258528a6d671SEric W. Biederman }
258628a6d671SEric W. Biederman 
258700977a59SArjan van de Ven static const struct file_operations proc_tgid_base_operations = {
258828a6d671SEric W. Biederman 	.read		= generic_read_dir,
2589f0c3b509SAl Viro 	.iterate	= proc_tgid_base_readdir,
25906038f373SArnd Bergmann 	.llseek		= default_llseek,
259128a6d671SEric W. Biederman };
259228a6d671SEric W. Biederman 
259300cd8dd3SAl Viro static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
259400cd8dd3SAl Viro {
25957bcd6b0eSEric W. Biederman 	return proc_pident_lookup(dir, dentry,
25967bcd6b0eSEric W. Biederman 				  tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
259728a6d671SEric W. Biederman }
259828a6d671SEric W. Biederman 
2599c5ef1c42SArjan van de Ven static const struct inode_operations proc_tgid_base_inode_operations = {
260028a6d671SEric W. Biederman 	.lookup		= proc_tgid_base_lookup,
260128a6d671SEric W. Biederman 	.getattr	= pid_getattr,
260228a6d671SEric W. Biederman 	.setattr	= proc_setattr,
26030499680aSVasiliy Kulikov 	.permission	= proc_pid_permission,
260428a6d671SEric W. Biederman };
260528a6d671SEric W. Biederman 
260660347f67SPavel Emelyanov static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
26071da177e4SLinus Torvalds {
260848e6484dSEric W. Biederman 	struct dentry *dentry, *leader, *dir;
26098578cea7SEric W. Biederman 	char buf[PROC_NUMBUF];
261048e6484dSEric W. Biederman 	struct qstr name;
26111da177e4SLinus Torvalds 
261248e6484dSEric W. Biederman 	name.name = buf;
261360347f67SPavel Emelyanov 	name.len = snprintf(buf, sizeof(buf), "%d", pid);
26144f522a24SAl Viro 	/* no ->d_hash() rejects on procfs */
261560347f67SPavel Emelyanov 	dentry = d_hash_and_lookup(mnt->mnt_root, &name);
261648e6484dSEric W. Biederman 	if (dentry) {
2617bbd51924SEric W. Biederman 		d_invalidate(dentry);
261848e6484dSEric W. Biederman 		dput(dentry);
26191da177e4SLinus Torvalds 	}
26201da177e4SLinus Torvalds 
2621*c35a7f18SOleg Nesterov 	if (pid == tgid)
2622*c35a7f18SOleg Nesterov 		return;
2623*c35a7f18SOleg Nesterov 
262448e6484dSEric W. Biederman 	name.name = buf;
262560347f67SPavel Emelyanov 	name.len = snprintf(buf, sizeof(buf), "%d", tgid);
262660347f67SPavel Emelyanov 	leader = d_hash_and_lookup(mnt->mnt_root, &name);
262748e6484dSEric W. Biederman 	if (!leader)
262848e6484dSEric W. Biederman 		goto out;
262948e6484dSEric W. Biederman 
263048e6484dSEric W. Biederman 	name.name = "task";
263148e6484dSEric W. Biederman 	name.len = strlen(name.name);
263248e6484dSEric W. Biederman 	dir = d_hash_and_lookup(leader, &name);
263348e6484dSEric W. Biederman 	if (!dir)
263448e6484dSEric W. Biederman 		goto out_put_leader;
263548e6484dSEric W. Biederman 
263648e6484dSEric W. Biederman 	name.name = buf;
263760347f67SPavel Emelyanov 	name.len = snprintf(buf, sizeof(buf), "%d", pid);
263848e6484dSEric W. Biederman 	dentry = d_hash_and_lookup(dir, &name);
263948e6484dSEric W. Biederman 	if (dentry) {
2640bbd51924SEric W. Biederman 		d_invalidate(dentry);
264148e6484dSEric W. Biederman 		dput(dentry);
26421da177e4SLinus Torvalds 	}
264348e6484dSEric W. Biederman 
264448e6484dSEric W. Biederman 	dput(dir);
264548e6484dSEric W. Biederman out_put_leader:
264648e6484dSEric W. Biederman 	dput(leader);
264748e6484dSEric W. Biederman out:
264848e6484dSEric W. Biederman 	return;
26491da177e4SLinus Torvalds }
26501da177e4SLinus Torvalds 
26510895e91dSRandy Dunlap /**
26520895e91dSRandy Dunlap  * proc_flush_task -  Remove dcache entries for @task from the /proc dcache.
26530895e91dSRandy Dunlap  * @task: task that should be flushed.
26540895e91dSRandy Dunlap  *
26550895e91dSRandy Dunlap  * When flushing dentries from proc, one needs to flush them from global
265660347f67SPavel Emelyanov  * proc (proc_mnt) and from all the namespaces' procs this task was seen
26570895e91dSRandy Dunlap  * in. This call is supposed to do all of this job.
26580895e91dSRandy Dunlap  *
26590895e91dSRandy Dunlap  * Looks in the dcache for
26600895e91dSRandy Dunlap  * /proc/@pid
26610895e91dSRandy Dunlap  * /proc/@tgid/task/@pid
26620895e91dSRandy Dunlap  * if either directory is present flushes it and all of it'ts children
26630895e91dSRandy Dunlap  * from the dcache.
26640895e91dSRandy Dunlap  *
26650895e91dSRandy Dunlap  * It is safe and reasonable to cache /proc entries for a task until
26660895e91dSRandy Dunlap  * that task exits.  After that they just clog up the dcache with
26670895e91dSRandy Dunlap  * useless entries, possibly causing useful dcache entries to be
26680895e91dSRandy Dunlap  * flushed instead.  This routine is proved to flush those useless
26690895e91dSRandy Dunlap  * dcache entries at process exit time.
26700895e91dSRandy Dunlap  *
26710895e91dSRandy Dunlap  * NOTE: This routine is just an optimization so it does not guarantee
26720895e91dSRandy Dunlap  *       that no dcache entries will exist at process exit time it
26730895e91dSRandy Dunlap  *       just makes it very unlikely that any will persist.
267460347f67SPavel Emelyanov  */
267560347f67SPavel Emelyanov 
267660347f67SPavel Emelyanov void proc_flush_task(struct task_struct *task)
267760347f67SPavel Emelyanov {
26789fcc2d15SEric W. Biederman 	int i;
26799b4d1cbeSOleg Nesterov 	struct pid *pid, *tgid;
2680130f77ecSPavel Emelyanov 	struct upid *upid;
2681130f77ecSPavel Emelyanov 
2682130f77ecSPavel Emelyanov 	pid = task_pid(task);
2683130f77ecSPavel Emelyanov 	tgid = task_tgid(task);
26849fcc2d15SEric W. Biederman 
26859fcc2d15SEric W. Biederman 	for (i = 0; i <= pid->level; i++) {
2686130f77ecSPavel Emelyanov 		upid = &pid->numbers[i];
2687130f77ecSPavel Emelyanov 		proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
26889b4d1cbeSOleg Nesterov 					tgid->numbers[i].nr);
2689130f77ecSPavel Emelyanov 	}
269060347f67SPavel Emelyanov }
269160347f67SPavel Emelyanov 
2692c52a47acSAl Viro static int proc_pid_instantiate(struct inode *dir,
26939711ef99SAdrian Bunk 				   struct dentry * dentry,
2694c5141e6dSEric Dumazet 				   struct task_struct *task, const void *ptr)
2695444ceed8SEric W. Biederman {
2696444ceed8SEric W. Biederman 	struct inode *inode;
2697444ceed8SEric W. Biederman 
269861a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
2699444ceed8SEric W. Biederman 	if (!inode)
2700444ceed8SEric W. Biederman 		goto out;
2701444ceed8SEric W. Biederman 
2702444ceed8SEric W. Biederman 	inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2703444ceed8SEric W. Biederman 	inode->i_op = &proc_tgid_base_inode_operations;
2704444ceed8SEric W. Biederman 	inode->i_fop = &proc_tgid_base_operations;
2705444ceed8SEric W. Biederman 	inode->i_flags|=S_IMMUTABLE;
2706aed54175SVegard Nossum 
2707bfe86848SMiklos Szeredi 	set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
2708bfe86848SMiklos Szeredi 						  ARRAY_SIZE(tgid_base_stuff)));
2709444ceed8SEric W. Biederman 
2710fb045adbSNick Piggin 	d_set_d_op(dentry, &pid_dentry_operations);
2711444ceed8SEric W. Biederman 
2712444ceed8SEric W. Biederman 	d_add(dentry, inode);
2713444ceed8SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
27140b728e19SAl Viro 	if (pid_revalidate(dentry, 0))
2715c52a47acSAl Viro 		return 0;
2716444ceed8SEric W. Biederman out:
2717c52a47acSAl Viro 	return -ENOENT;
2718444ceed8SEric W. Biederman }
2719444ceed8SEric W. Biederman 
272000cd8dd3SAl Viro struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
27211da177e4SLinus Torvalds {
2722335eb531SAlexey Dobriyan 	int result = -ENOENT;
27231da177e4SLinus Torvalds 	struct task_struct *task;
27241da177e4SLinus Torvalds 	unsigned tgid;
2725b488893aSPavel Emelyanov 	struct pid_namespace *ns;
27261da177e4SLinus Torvalds 
2727dbcdb504SAlexey Dobriyan 	tgid = name_to_int(&dentry->d_name);
27281da177e4SLinus Torvalds 	if (tgid == ~0U)
27291da177e4SLinus Torvalds 		goto out;
27301da177e4SLinus Torvalds 
2731b488893aSPavel Emelyanov 	ns = dentry->d_sb->s_fs_info;
2732de758734SEric W. Biederman 	rcu_read_lock();
2733b488893aSPavel Emelyanov 	task = find_task_by_pid_ns(tgid, ns);
27341da177e4SLinus Torvalds 	if (task)
27351da177e4SLinus Torvalds 		get_task_struct(task);
2736de758734SEric W. Biederman 	rcu_read_unlock();
27371da177e4SLinus Torvalds 	if (!task)
27381da177e4SLinus Torvalds 		goto out;
27391da177e4SLinus Torvalds 
2740444ceed8SEric W. Biederman 	result = proc_pid_instantiate(dir, dentry, task, NULL);
274148e6484dSEric W. Biederman 	put_task_struct(task);
27421da177e4SLinus Torvalds out:
2743c52a47acSAl Viro 	return ERR_PTR(result);
27441da177e4SLinus Torvalds }
27451da177e4SLinus Torvalds 
27461da177e4SLinus Torvalds /*
27470804ef4bSEric W. Biederman  * Find the first task with tgid >= tgid
27480bc58a91SEric W. Biederman  *
27491da177e4SLinus Torvalds  */
275019fd4bb2SEric W. Biederman struct tgid_iter {
275119fd4bb2SEric W. Biederman 	unsigned int tgid;
27520804ef4bSEric W. Biederman 	struct task_struct *task;
275319fd4bb2SEric W. Biederman };
275419fd4bb2SEric W. Biederman static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
275519fd4bb2SEric W. Biederman {
27560804ef4bSEric W. Biederman 	struct pid *pid;
27571da177e4SLinus Torvalds 
275819fd4bb2SEric W. Biederman 	if (iter.task)
275919fd4bb2SEric W. Biederman 		put_task_struct(iter.task);
27600804ef4bSEric W. Biederman 	rcu_read_lock();
27610804ef4bSEric W. Biederman retry:
276219fd4bb2SEric W. Biederman 	iter.task = NULL;
276319fd4bb2SEric W. Biederman 	pid = find_ge_pid(iter.tgid, ns);
27640804ef4bSEric W. Biederman 	if (pid) {
276519fd4bb2SEric W. Biederman 		iter.tgid = pid_nr_ns(pid, ns);
276619fd4bb2SEric W. Biederman 		iter.task = pid_task(pid, PIDTYPE_PID);
27670804ef4bSEric W. Biederman 		/* What we to know is if the pid we have find is the
27680804ef4bSEric W. Biederman 		 * pid of a thread_group_leader.  Testing for task
27690804ef4bSEric W. Biederman 		 * being a thread_group_leader is the obvious thing
27700804ef4bSEric W. Biederman 		 * todo but there is a window when it fails, due to
27710804ef4bSEric W. Biederman 		 * the pid transfer logic in de_thread.
27720804ef4bSEric W. Biederman 		 *
27730804ef4bSEric W. Biederman 		 * So we perform the straight forward test of seeing
27740804ef4bSEric W. Biederman 		 * if the pid we have found is the pid of a thread
27750804ef4bSEric W. Biederman 		 * group leader, and don't worry if the task we have
27760804ef4bSEric W. Biederman 		 * found doesn't happen to be a thread group leader.
27770804ef4bSEric W. Biederman 		 * As we don't care in the case of readdir.
27780bc58a91SEric W. Biederman 		 */
277919fd4bb2SEric W. Biederman 		if (!iter.task || !has_group_leader_pid(iter.task)) {
278019fd4bb2SEric W. Biederman 			iter.tgid += 1;
27810804ef4bSEric W. Biederman 			goto retry;
278219fd4bb2SEric W. Biederman 		}
278319fd4bb2SEric W. Biederman 		get_task_struct(iter.task);
27841da177e4SLinus Torvalds 	}
2785454cc105SEric W. Biederman 	rcu_read_unlock();
278619fd4bb2SEric W. Biederman 	return iter;
27871da177e4SLinus Torvalds }
27881da177e4SLinus Torvalds 
27890097875bSEric W. Biederman #define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
27901da177e4SLinus Torvalds 
27911da177e4SLinus Torvalds /* for the /proc/ directory itself, after non-process stuff has been done */
2792f0c3b509SAl Viro int proc_pid_readdir(struct file *file, struct dir_context *ctx)
27931da177e4SLinus Torvalds {
279419fd4bb2SEric W. Biederman 	struct tgid_iter iter;
2795db963164SAl Viro 	struct pid_namespace *ns = file->f_dentry->d_sb->s_fs_info;
2796f0c3b509SAl Viro 	loff_t pos = ctx->pos;
27971da177e4SLinus Torvalds 
2798021ada7dSAl Viro 	if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
2799f0c3b509SAl Viro 		return 0;
28001da177e4SLinus Torvalds 
28010097875bSEric W. Biederman 	if (pos == TGID_OFFSET - 2) {
2802db963164SAl Viro 		struct inode *inode = ns->proc_self->d_inode;
2803db963164SAl Viro 		if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
2804f0c3b509SAl Viro 			return 0;
28050097875bSEric W. Biederman 		ctx->pos = pos = pos + 1;
2806021ada7dSAl Viro 	}
28070097875bSEric W. Biederman 	if (pos == TGID_OFFSET - 1) {
28080097875bSEric W. Biederman 		struct inode *inode = ns->proc_thread_self->d_inode;
28090097875bSEric W. Biederman 		if (!dir_emit(ctx, "thread-self", 11, inode->i_ino, DT_LNK))
28100097875bSEric W. Biederman 			return 0;
28110097875bSEric W. Biederman 		ctx->pos = pos = pos + 1;
28120097875bSEric W. Biederman 	}
28130097875bSEric W. Biederman 	iter.tgid = pos - TGID_OFFSET;
281419fd4bb2SEric W. Biederman 	iter.task = NULL;
281519fd4bb2SEric W. Biederman 	for (iter = next_tgid(ns, iter);
281619fd4bb2SEric W. Biederman 	     iter.task;
281719fd4bb2SEric W. Biederman 	     iter.tgid += 1, iter = next_tgid(ns, iter)) {
2818f0c3b509SAl Viro 		char name[PROC_NUMBUF];
2819f0c3b509SAl Viro 		int len;
2820f0c3b509SAl Viro 		if (!has_pid_permissions(ns, iter.task, 2))
2821f0c3b509SAl Viro 			continue;
28220499680aSVasiliy Kulikov 
2823f0c3b509SAl Viro 		len = snprintf(name, sizeof(name), "%d", iter.tgid);
2824f0c3b509SAl Viro 		ctx->pos = iter.tgid + TGID_OFFSET;
2825f0c3b509SAl Viro 		if (!proc_fill_cache(file, ctx, name, len,
2826f0c3b509SAl Viro 				     proc_pid_instantiate, iter.task, NULL)) {
282719fd4bb2SEric W. Biederman 			put_task_struct(iter.task);
2828f0c3b509SAl Viro 			return 0;
28291da177e4SLinus Torvalds 		}
28301da177e4SLinus Torvalds 	}
2831f0c3b509SAl Viro 	ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
28321da177e4SLinus Torvalds 	return 0;
28331da177e4SLinus Torvalds }
28341da177e4SLinus Torvalds 
28350bc58a91SEric W. Biederman /*
283628a6d671SEric W. Biederman  * Tasks
283728a6d671SEric W. Biederman  */
2838c5141e6dSEric Dumazet static const struct pid_entry tid_base_stuff[] = {
2839631f9c18SAlexey Dobriyan 	DIR("fd",        S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
28403835541dSJerome Marchand 	DIR("fdinfo",    S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
28416b4e306aSEric W. Biederman 	DIR("ns",	 S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
28426ba8ed79SEric W. Biederman #ifdef CONFIG_NET
28436ba8ed79SEric W. Biederman 	DIR("net",        S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
28446ba8ed79SEric W. Biederman #endif
2845631f9c18SAlexey Dobriyan 	REG("environ",   S_IRUSR, proc_environ_operations),
2846f9ea536eSAlexey Dobriyan 	ONE("auxv",      S_IRUSR, proc_pid_auxv),
2847631f9c18SAlexey Dobriyan 	ONE("status",    S_IRUGO, proc_pid_status),
284835a35046SDjalal Harouni 	ONE("personality", S_IRUSR, proc_pid_personality),
28491c963eb1SAlexey Dobriyan 	ONE("limits",	 S_IRUGO, proc_pid_limits),
285043ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
2851631f9c18SAlexey Dobriyan 	REG("sched",     S_IRUGO|S_IWUSR, proc_pid_sched_operations),
285243ae34cbSIngo Molnar #endif
28534614a696Sjohn stultz 	REG("comm",      S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2854ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
285509d93bd6SAlexey Dobriyan 	ONE("syscall",   S_IRUSR, proc_pid_syscall),
2856ebcb6734SRoland McGrath #endif
28572ca66ff7SAlexey Dobriyan 	ONE("cmdline",   S_IRUGO, proc_pid_cmdline),
2858631f9c18SAlexey Dobriyan 	ONE("stat",      S_IRUGO, proc_tid_stat),
2859631f9c18SAlexey Dobriyan 	ONE("statm",     S_IRUGO, proc_pid_statm),
2860b7643757SSiddhesh Poyarekar 	REG("maps",      S_IRUGO, proc_tid_maps_operations),
286181841161SCyrill Gorcunov #ifdef CONFIG_CHECKPOINT_RESTORE
286281841161SCyrill Gorcunov 	REG("children",  S_IRUGO, proc_tid_children_operations),
286381841161SCyrill Gorcunov #endif
286428a6d671SEric W. Biederman #ifdef CONFIG_NUMA
2865b7643757SSiddhesh Poyarekar 	REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
286628a6d671SEric W. Biederman #endif
2867631f9c18SAlexey Dobriyan 	REG("mem",       S_IRUSR|S_IWUSR, proc_mem_operations),
2868631f9c18SAlexey Dobriyan 	LNK("cwd",       proc_cwd_link),
2869631f9c18SAlexey Dobriyan 	LNK("root",      proc_root_link),
2870631f9c18SAlexey Dobriyan 	LNK("exe",       proc_exe_link),
2871631f9c18SAlexey Dobriyan 	REG("mounts",    S_IRUGO, proc_mounts_operations),
2872631f9c18SAlexey Dobriyan 	REG("mountinfo",  S_IRUGO, proc_mountinfo_operations),
28731e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR
2874631f9c18SAlexey Dobriyan 	REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2875b7643757SSiddhesh Poyarekar 	REG("smaps",     S_IRUGO, proc_tid_smaps_operations),
287632ed74a4SDjalal Harouni 	REG("pagemap",    S_IRUSR, proc_pagemap_operations),
287728a6d671SEric W. Biederman #endif
287828a6d671SEric W. Biederman #ifdef CONFIG_SECURITY
2879631f9c18SAlexey Dobriyan 	DIR("attr",      S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
288028a6d671SEric W. Biederman #endif
288128a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS
2882edfcd606SAlexey Dobriyan 	ONE("wchan",     S_IRUGO, proc_pid_wchan),
288328a6d671SEric W. Biederman #endif
28842ec220e2SKen Chen #ifdef CONFIG_STACKTRACE
288535a35046SDjalal Harouni 	ONE("stack",      S_IRUSR, proc_pid_stack),
288628a6d671SEric W. Biederman #endif
288728a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS
2888f6e826caSAlexey Dobriyan 	ONE("schedstat", S_IRUGO, proc_pid_schedstat),
288928a6d671SEric W. Biederman #endif
28909745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP
2891631f9c18SAlexey Dobriyan 	REG("latency",  S_IRUGO, proc_lstats_operations),
28929745512cSArjan van de Ven #endif
28938793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET
289452de4779SZefan Li 	ONE("cpuset",    S_IRUGO, proc_cpuset_show),
289528a6d671SEric W. Biederman #endif
2896a424316cSPaul Menage #ifdef CONFIG_CGROUPS
2897006f4ac4SZefan Li 	ONE("cgroup",  S_IRUGO, proc_cgroup_show),
2898a424316cSPaul Menage #endif
28996ba51e37SAlexey Dobriyan 	ONE("oom_score", S_IRUGO, proc_oom_score),
2900fa0cbbf1SDavid Rientjes 	REG("oom_adj",   S_IRUGO|S_IWUSR, proc_oom_adj_operations),
2901a63d83f4SDavid Rientjes 	REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
290228a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL
2903631f9c18SAlexey Dobriyan 	REG("loginuid",  S_IWUSR|S_IRUGO, proc_loginuid_operations),
290426ec3c64SAl Viro 	REG("sessionid",  S_IRUGO, proc_sessionid_operations),
290528a6d671SEric W. Biederman #endif
2906f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
2907631f9c18SAlexey Dobriyan 	REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2908f4f154fdSAkinobu Mita #endif
2909297c5d92SAndrea Righi #ifdef CONFIG_TASK_IO_ACCOUNTING
291019aadc98SAlexey Dobriyan 	ONE("io",	S_IRUSR, proc_tid_io_accounting),
2911297c5d92SAndrea Righi #endif
2912f133eccaSChris Metcalf #ifdef CONFIG_HARDWALL
2913d962c144SAlexey Dobriyan 	ONE("hardwall",   S_IRUGO, proc_pid_hardwall),
2914f133eccaSChris Metcalf #endif
291522d917d8SEric W. Biederman #ifdef CONFIG_USER_NS
291622d917d8SEric W. Biederman 	REG("uid_map",    S_IRUGO|S_IWUSR, proc_uid_map_operations),
291722d917d8SEric W. Biederman 	REG("gid_map",    S_IRUGO|S_IWUSR, proc_gid_map_operations),
2918f76d207aSEric W. Biederman 	REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
291922d917d8SEric W. Biederman #endif
292028a6d671SEric W. Biederman };
292128a6d671SEric W. Biederman 
2922f0c3b509SAl Viro static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
292328a6d671SEric W. Biederman {
2924f0c3b509SAl Viro 	return proc_pident_readdir(file, ctx,
292528a6d671SEric W. Biederman 				   tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
292628a6d671SEric W. Biederman }
292728a6d671SEric W. Biederman 
292800cd8dd3SAl Viro static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
292900cd8dd3SAl Viro {
29307bcd6b0eSEric W. Biederman 	return proc_pident_lookup(dir, dentry,
29317bcd6b0eSEric W. Biederman 				  tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
293228a6d671SEric W. Biederman }
293328a6d671SEric W. Biederman 
293400977a59SArjan van de Ven static const struct file_operations proc_tid_base_operations = {
293528a6d671SEric W. Biederman 	.read		= generic_read_dir,
2936f0c3b509SAl Viro 	.iterate	= proc_tid_base_readdir,
29376038f373SArnd Bergmann 	.llseek		= default_llseek,
293828a6d671SEric W. Biederman };
293928a6d671SEric W. Biederman 
2940c5ef1c42SArjan van de Ven static const struct inode_operations proc_tid_base_inode_operations = {
294128a6d671SEric W. Biederman 	.lookup		= proc_tid_base_lookup,
294228a6d671SEric W. Biederman 	.getattr	= pid_getattr,
294328a6d671SEric W. Biederman 	.setattr	= proc_setattr,
294428a6d671SEric W. Biederman };
294528a6d671SEric W. Biederman 
2946c52a47acSAl Viro static int proc_task_instantiate(struct inode *dir,
2947c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
2948444ceed8SEric W. Biederman {
2949444ceed8SEric W. Biederman 	struct inode *inode;
295061a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
2951444ceed8SEric W. Biederman 
2952444ceed8SEric W. Biederman 	if (!inode)
2953444ceed8SEric W. Biederman 		goto out;
2954444ceed8SEric W. Biederman 	inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2955444ceed8SEric W. Biederman 	inode->i_op = &proc_tid_base_inode_operations;
2956444ceed8SEric W. Biederman 	inode->i_fop = &proc_tid_base_operations;
2957444ceed8SEric W. Biederman 	inode->i_flags|=S_IMMUTABLE;
2958aed54175SVegard Nossum 
2959bfe86848SMiklos Szeredi 	set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
2960bfe86848SMiklos Szeredi 						  ARRAY_SIZE(tid_base_stuff)));
2961444ceed8SEric W. Biederman 
2962fb045adbSNick Piggin 	d_set_d_op(dentry, &pid_dentry_operations);
2963444ceed8SEric W. Biederman 
2964444ceed8SEric W. Biederman 	d_add(dentry, inode);
2965444ceed8SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
29660b728e19SAl Viro 	if (pid_revalidate(dentry, 0))
2967c52a47acSAl Viro 		return 0;
2968444ceed8SEric W. Biederman out:
2969c52a47acSAl Viro 	return -ENOENT;
2970444ceed8SEric W. Biederman }
2971444ceed8SEric W. Biederman 
297200cd8dd3SAl Viro static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
297328a6d671SEric W. Biederman {
2974c52a47acSAl Viro 	int result = -ENOENT;
297528a6d671SEric W. Biederman 	struct task_struct *task;
297628a6d671SEric W. Biederman 	struct task_struct *leader = get_proc_task(dir);
297728a6d671SEric W. Biederman 	unsigned tid;
2978b488893aSPavel Emelyanov 	struct pid_namespace *ns;
297928a6d671SEric W. Biederman 
298028a6d671SEric W. Biederman 	if (!leader)
298128a6d671SEric W. Biederman 		goto out_no_task;
298228a6d671SEric W. Biederman 
2983dbcdb504SAlexey Dobriyan 	tid = name_to_int(&dentry->d_name);
298428a6d671SEric W. Biederman 	if (tid == ~0U)
298528a6d671SEric W. Biederman 		goto out;
298628a6d671SEric W. Biederman 
2987b488893aSPavel Emelyanov 	ns = dentry->d_sb->s_fs_info;
298828a6d671SEric W. Biederman 	rcu_read_lock();
2989b488893aSPavel Emelyanov 	task = find_task_by_pid_ns(tid, ns);
299028a6d671SEric W. Biederman 	if (task)
299128a6d671SEric W. Biederman 		get_task_struct(task);
299228a6d671SEric W. Biederman 	rcu_read_unlock();
299328a6d671SEric W. Biederman 	if (!task)
299428a6d671SEric W. Biederman 		goto out;
2995bac0abd6SPavel Emelyanov 	if (!same_thread_group(leader, task))
299628a6d671SEric W. Biederman 		goto out_drop_task;
299728a6d671SEric W. Biederman 
2998444ceed8SEric W. Biederman 	result = proc_task_instantiate(dir, dentry, task, NULL);
299928a6d671SEric W. Biederman out_drop_task:
300028a6d671SEric W. Biederman 	put_task_struct(task);
300128a6d671SEric W. Biederman out:
300228a6d671SEric W. Biederman 	put_task_struct(leader);
300328a6d671SEric W. Biederman out_no_task:
3004c52a47acSAl Viro 	return ERR_PTR(result);
300528a6d671SEric W. Biederman }
300628a6d671SEric W. Biederman 
300728a6d671SEric W. Biederman /*
30080bc58a91SEric W. Biederman  * Find the first tid of a thread group to return to user space.
30090bc58a91SEric W. Biederman  *
30100bc58a91SEric W. Biederman  * Usually this is just the thread group leader, but if the users
30110bc58a91SEric W. Biederman  * buffer was too small or there was a seek into the middle of the
30120bc58a91SEric W. Biederman  * directory we have more work todo.
30130bc58a91SEric W. Biederman  *
30140bc58a91SEric W. Biederman  * In the case of a short read we start with find_task_by_pid.
30150bc58a91SEric W. Biederman  *
30160bc58a91SEric W. Biederman  * In the case of a seek we start with the leader and walk nr
30170bc58a91SEric W. Biederman  * threads past it.
30180bc58a91SEric W. Biederman  */
30199f6e963fSOleg Nesterov static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
30209f6e963fSOleg Nesterov 					struct pid_namespace *ns)
30210bc58a91SEric W. Biederman {
3022d855a4b7SOleg Nesterov 	struct task_struct *pos, *task;
30239f6e963fSOleg Nesterov 	unsigned long nr = f_pos;
30249f6e963fSOleg Nesterov 
30259f6e963fSOleg Nesterov 	if (nr != f_pos)	/* 32bit overflow? */
30269f6e963fSOleg Nesterov 		return NULL;
30270bc58a91SEric W. Biederman 
3028cc288738SEric W. Biederman 	rcu_read_lock();
3029d855a4b7SOleg Nesterov 	task = pid_task(pid, PIDTYPE_PID);
3030d855a4b7SOleg Nesterov 	if (!task)
3031d855a4b7SOleg Nesterov 		goto fail;
3032d855a4b7SOleg Nesterov 
3033d855a4b7SOleg Nesterov 	/* Attempt to start with the tid of a thread */
30349f6e963fSOleg Nesterov 	if (tid && nr) {
3035b488893aSPavel Emelyanov 		pos = find_task_by_pid_ns(tid, ns);
3036d855a4b7SOleg Nesterov 		if (pos && same_thread_group(pos, task))
3037a872ff0cSOleg Nesterov 			goto found;
30380bc58a91SEric W. Biederman 	}
30390bc58a91SEric W. Biederman 
30400bc58a91SEric W. Biederman 	/* If nr exceeds the number of threads there is nothing todo */
30419f6e963fSOleg Nesterov 	if (nr >= get_nr_threads(task))
3042c986c14aSOleg Nesterov 		goto fail;
3043a872ff0cSOleg Nesterov 
3044a872ff0cSOleg Nesterov 	/* If we haven't found our starting place yet start
3045a872ff0cSOleg Nesterov 	 * with the leader and walk nr threads forward.
3046a872ff0cSOleg Nesterov 	 */
3047d855a4b7SOleg Nesterov 	pos = task = task->group_leader;
3048c986c14aSOleg Nesterov 	do {
30499f6e963fSOleg Nesterov 		if (!nr--)
3050c986c14aSOleg Nesterov 			goto found;
3051d855a4b7SOleg Nesterov 	} while_each_thread(task, pos);
3052c986c14aSOleg Nesterov fail:
3053a872ff0cSOleg Nesterov 	pos = NULL;
3054a872ff0cSOleg Nesterov 	goto out;
3055a872ff0cSOleg Nesterov found:
3056a872ff0cSOleg Nesterov 	get_task_struct(pos);
3057a872ff0cSOleg Nesterov out:
3058cc288738SEric W. Biederman 	rcu_read_unlock();
30590bc58a91SEric W. Biederman 	return pos;
30600bc58a91SEric W. Biederman }
30610bc58a91SEric W. Biederman 
30620bc58a91SEric W. Biederman /*
30630bc58a91SEric W. Biederman  * Find the next thread in the thread list.
30640bc58a91SEric W. Biederman  * Return NULL if there is an error or no next thread.
30650bc58a91SEric W. Biederman  *
30660bc58a91SEric W. Biederman  * The reference to the input task_struct is released.
30670bc58a91SEric W. Biederman  */
30680bc58a91SEric W. Biederman static struct task_struct *next_tid(struct task_struct *start)
30690bc58a91SEric W. Biederman {
3070c1df7fb8SOleg Nesterov 	struct task_struct *pos = NULL;
3071cc288738SEric W. Biederman 	rcu_read_lock();
3072c1df7fb8SOleg Nesterov 	if (pid_alive(start)) {
30730bc58a91SEric W. Biederman 		pos = next_thread(start);
3074c1df7fb8SOleg Nesterov 		if (thread_group_leader(pos))
30750bc58a91SEric W. Biederman 			pos = NULL;
3076c1df7fb8SOleg Nesterov 		else
3077c1df7fb8SOleg Nesterov 			get_task_struct(pos);
3078c1df7fb8SOleg Nesterov 	}
3079cc288738SEric W. Biederman 	rcu_read_unlock();
30800bc58a91SEric W. Biederman 	put_task_struct(start);
30810bc58a91SEric W. Biederman 	return pos;
30820bc58a91SEric W. Biederman }
30830bc58a91SEric W. Biederman 
30841da177e4SLinus Torvalds /* for the /proc/TGID/task/ directories */
3085f0c3b509SAl Viro static int proc_task_readdir(struct file *file, struct dir_context *ctx)
30861da177e4SLinus Torvalds {
3087d855a4b7SOleg Nesterov 	struct inode *inode = file_inode(file);
3088d855a4b7SOleg Nesterov 	struct task_struct *task;
3089b488893aSPavel Emelyanov 	struct pid_namespace *ns;
3090f0c3b509SAl Viro 	int tid;
30911da177e4SLinus Torvalds 
3092d855a4b7SOleg Nesterov 	if (proc_inode_is_dead(inode))
3093f0c3b509SAl Viro 		return -ENOENT;
30941da177e4SLinus Torvalds 
3095f0c3b509SAl Viro 	if (!dir_emit_dots(file, ctx))
3096d855a4b7SOleg Nesterov 		return 0;
30971da177e4SLinus Torvalds 
30980bc58a91SEric W. Biederman 	/* f_version caches the tgid value that the last readdir call couldn't
30990bc58a91SEric W. Biederman 	 * return. lseek aka telldir automagically resets f_version to 0.
31000bc58a91SEric W. Biederman 	 */
3101f0c3b509SAl Viro 	ns = file->f_dentry->d_sb->s_fs_info;
3102f0c3b509SAl Viro 	tid = (int)file->f_version;
3103f0c3b509SAl Viro 	file->f_version = 0;
3104d855a4b7SOleg Nesterov 	for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
31050bc58a91SEric W. Biederman 	     task;
3106f0c3b509SAl Viro 	     task = next_tid(task), ctx->pos++) {
3107f0c3b509SAl Viro 		char name[PROC_NUMBUF];
3108f0c3b509SAl Viro 		int len;
3109b488893aSPavel Emelyanov 		tid = task_pid_nr_ns(task, ns);
3110f0c3b509SAl Viro 		len = snprintf(name, sizeof(name), "%d", tid);
3111f0c3b509SAl Viro 		if (!proc_fill_cache(file, ctx, name, len,
3112f0c3b509SAl Viro 				proc_task_instantiate, task, NULL)) {
31130bc58a91SEric W. Biederman 			/* returning this tgid failed, save it as the first
31140bc58a91SEric W. Biederman 			 * pid for the next readir call */
3115f0c3b509SAl Viro 			file->f_version = (u64)tid;
31160bc58a91SEric W. Biederman 			put_task_struct(task);
31171da177e4SLinus Torvalds 			break;
31180bc58a91SEric W. Biederman 		}
31191da177e4SLinus Torvalds 	}
3120d855a4b7SOleg Nesterov 
3121f0c3b509SAl Viro 	return 0;
31221da177e4SLinus Torvalds }
31236e66b52bSEric W. Biederman 
31246e66b52bSEric W. Biederman static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
31256e66b52bSEric W. Biederman {
31266e66b52bSEric W. Biederman 	struct inode *inode = dentry->d_inode;
312799f89551SEric W. Biederman 	struct task_struct *p = get_proc_task(inode);
31286e66b52bSEric W. Biederman 	generic_fillattr(inode, stat);
31296e66b52bSEric W. Biederman 
313099f89551SEric W. Biederman 	if (p) {
313199f89551SEric W. Biederman 		stat->nlink += get_nr_threads(p);
313299f89551SEric W. Biederman 		put_task_struct(p);
31336e66b52bSEric W. Biederman 	}
31346e66b52bSEric W. Biederman 
31356e66b52bSEric W. Biederman 	return 0;
31366e66b52bSEric W. Biederman }
313728a6d671SEric W. Biederman 
3138c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations = {
313928a6d671SEric W. Biederman 	.lookup		= proc_task_lookup,
314028a6d671SEric W. Biederman 	.getattr	= proc_task_getattr,
314128a6d671SEric W. Biederman 	.setattr	= proc_setattr,
31420499680aSVasiliy Kulikov 	.permission	= proc_pid_permission,
314328a6d671SEric W. Biederman };
314428a6d671SEric W. Biederman 
314500977a59SArjan van de Ven static const struct file_operations proc_task_operations = {
314628a6d671SEric W. Biederman 	.read		= generic_read_dir,
3147f0c3b509SAl Viro 	.iterate	= proc_task_readdir,
31486038f373SArnd Bergmann 	.llseek		= default_llseek,
314928a6d671SEric W. Biederman };
3150