xref: /openbmc/linux/fs/proc/base.c (revision bbd5192412fdedbae00888316bfe350bf89d0458)
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 
3798d8b97baSAl Viro #ifdef CONFIG_CGROUPS
3808d8b97baSAl Viro static int cgroup_open(struct inode *inode, struct file *file)
3818d8b97baSAl Viro {
3828d8b97baSAl Viro 	struct pid *pid = PROC_I(inode)->pid;
3838d8b97baSAl Viro 	return single_open(file, proc_cgroup_show, pid);
3848d8b97baSAl Viro }
3858d8b97baSAl Viro 
3868d8b97baSAl Viro static const struct file_operations proc_cgroup_operations = {
3878d8b97baSAl Viro 	.open		= cgroup_open,
3888d8b97baSAl Viro 	.read		= seq_read,
3898d8b97baSAl Viro 	.llseek		= seq_lseek,
3908d8b97baSAl Viro 	.release	= single_release,
3918d8b97baSAl Viro };
3928d8b97baSAl Viro #endif
3938d8b97baSAl Viro 
3948d8b97baSAl Viro #ifdef CONFIG_PROC_PID_CPUSET
3958d8b97baSAl Viro 
3968d8b97baSAl Viro static int cpuset_open(struct inode *inode, struct file *file)
3978d8b97baSAl Viro {
3988d8b97baSAl Viro 	struct pid *pid = PROC_I(inode)->pid;
3998d8b97baSAl Viro 	return single_open(file, proc_cpuset_show, pid);
4008d8b97baSAl Viro }
4018d8b97baSAl Viro 
4028d8b97baSAl Viro static const struct file_operations proc_cpuset_operations = {
4038d8b97baSAl Viro 	.open		= cpuset_open,
4048d8b97baSAl Viro 	.read		= seq_read,
4058d8b97baSAl Viro 	.llseek		= seq_lseek,
4068d8b97baSAl Viro 	.release	= single_release,
4078d8b97baSAl Viro };
4088d8b97baSAl Viro #endif
4098d8b97baSAl Viro 
4106ba51e37SAlexey Dobriyan static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns,
4116ba51e37SAlexey Dobriyan 			  struct pid *pid, struct task_struct *task)
4121da177e4SLinus Torvalds {
413a7f638f9SDavid Rientjes 	unsigned long totalpages = totalram_pages + total_swap_pages;
414b95c35e7SOleg Nesterov 	unsigned long points = 0;
4151da177e4SLinus Torvalds 
41619c5d45aSAlexey Dobriyan 	read_lock(&tasklist_lock);
417b95c35e7SOleg Nesterov 	if (pid_alive(task))
418a7f638f9SDavid Rientjes 		points = oom_badness(task, NULL, NULL, totalpages) *
419a7f638f9SDavid Rientjes 						1000 / totalpages;
42019c5d45aSAlexey Dobriyan 	read_unlock(&tasklist_lock);
4216ba51e37SAlexey Dobriyan 	return seq_printf(m, "%lu\n", points);
4221da177e4SLinus Torvalds }
4231da177e4SLinus Torvalds 
424d85f50d5SNeil Horman struct limit_names {
425cedbccabSAlexey Dobriyan 	const char *name;
426cedbccabSAlexey Dobriyan 	const char *unit;
427d85f50d5SNeil Horman };
428d85f50d5SNeil Horman 
429d85f50d5SNeil Horman static const struct limit_names lnames[RLIM_NLIMITS] = {
430cff4edb5SKees Cook 	[RLIMIT_CPU] = {"Max cpu time", "seconds"},
431d85f50d5SNeil Horman 	[RLIMIT_FSIZE] = {"Max file size", "bytes"},
432d85f50d5SNeil Horman 	[RLIMIT_DATA] = {"Max data size", "bytes"},
433d85f50d5SNeil Horman 	[RLIMIT_STACK] = {"Max stack size", "bytes"},
434d85f50d5SNeil Horman 	[RLIMIT_CORE] = {"Max core file size", "bytes"},
435d85f50d5SNeil Horman 	[RLIMIT_RSS] = {"Max resident set", "bytes"},
436d85f50d5SNeil Horman 	[RLIMIT_NPROC] = {"Max processes", "processes"},
437d85f50d5SNeil Horman 	[RLIMIT_NOFILE] = {"Max open files", "files"},
438d85f50d5SNeil Horman 	[RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
439d85f50d5SNeil Horman 	[RLIMIT_AS] = {"Max address space", "bytes"},
440d85f50d5SNeil Horman 	[RLIMIT_LOCKS] = {"Max file locks", "locks"},
441d85f50d5SNeil Horman 	[RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
442d85f50d5SNeil Horman 	[RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
443d85f50d5SNeil Horman 	[RLIMIT_NICE] = {"Max nice priority", NULL},
444d85f50d5SNeil Horman 	[RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
4458808117cSEugene Teo 	[RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
446d85f50d5SNeil Horman };
447d85f50d5SNeil Horman 
448d85f50d5SNeil Horman /* Display limits for a process */
4491c963eb1SAlexey Dobriyan static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
4501c963eb1SAlexey Dobriyan 			   struct pid *pid, struct task_struct *task)
451d85f50d5SNeil Horman {
452d85f50d5SNeil Horman 	unsigned int i;
453d85f50d5SNeil Horman 	unsigned long flags;
454d85f50d5SNeil Horman 
455d85f50d5SNeil Horman 	struct rlimit rlim[RLIM_NLIMITS];
456d85f50d5SNeil Horman 
457a6bebbc8SLai Jiangshan 	if (!lock_task_sighand(task, &flags))
458d85f50d5SNeil Horman 		return 0;
459d85f50d5SNeil Horman 	memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
460d85f50d5SNeil Horman 	unlock_task_sighand(task, &flags);
461d85f50d5SNeil Horman 
462d85f50d5SNeil Horman 	/*
463d85f50d5SNeil Horman 	 * print the file header
464d85f50d5SNeil Horman 	 */
4651c963eb1SAlexey Dobriyan        seq_printf(m, "%-25s %-20s %-20s %-10s\n",
466d85f50d5SNeil Horman 			"Limit", "Soft Limit", "Hard Limit", "Units");
467d85f50d5SNeil Horman 
468d85f50d5SNeil Horman 	for (i = 0; i < RLIM_NLIMITS; i++) {
469d85f50d5SNeil Horman 		if (rlim[i].rlim_cur == RLIM_INFINITY)
4701c963eb1SAlexey Dobriyan 			seq_printf(m, "%-25s %-20s ",
471d85f50d5SNeil Horman 					 lnames[i].name, "unlimited");
472d85f50d5SNeil Horman 		else
4731c963eb1SAlexey Dobriyan 			seq_printf(m, "%-25s %-20lu ",
474d85f50d5SNeil Horman 					 lnames[i].name, rlim[i].rlim_cur);
475d85f50d5SNeil Horman 
476d85f50d5SNeil Horman 		if (rlim[i].rlim_max == RLIM_INFINITY)
4771c963eb1SAlexey Dobriyan 			seq_printf(m, "%-20s ", "unlimited");
478d85f50d5SNeil Horman 		else
4791c963eb1SAlexey Dobriyan 			seq_printf(m, "%-20lu ", rlim[i].rlim_max);
480d85f50d5SNeil Horman 
481d85f50d5SNeil Horman 		if (lnames[i].unit)
4821c963eb1SAlexey Dobriyan 			seq_printf(m, "%-10s\n", lnames[i].unit);
483d85f50d5SNeil Horman 		else
4841c963eb1SAlexey Dobriyan 			seq_putc(m, '\n');
485d85f50d5SNeil Horman 	}
486d85f50d5SNeil Horman 
4871c963eb1SAlexey Dobriyan 	return 0;
488d85f50d5SNeil Horman }
489d85f50d5SNeil Horman 
490ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
49109d93bd6SAlexey Dobriyan static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
49209d93bd6SAlexey Dobriyan 			    struct pid *pid, struct task_struct *task)
493ebcb6734SRoland McGrath {
494ebcb6734SRoland McGrath 	long nr;
495ebcb6734SRoland McGrath 	unsigned long args[6], sp, pc;
496a9712bc1SAl Viro 	int res = lock_trace(task);
497a9712bc1SAl Viro 	if (res)
498a9712bc1SAl Viro 		return res;
499ebcb6734SRoland McGrath 
500ebcb6734SRoland McGrath 	if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
50109d93bd6SAlexey Dobriyan 		seq_puts(m, "running\n");
502a9712bc1SAl Viro 	else if (nr < 0)
50309d93bd6SAlexey Dobriyan 		seq_printf(m, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
504a9712bc1SAl Viro 	else
50509d93bd6SAlexey Dobriyan 		seq_printf(m,
506ebcb6734SRoland McGrath 		       "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
507ebcb6734SRoland McGrath 		       nr,
508ebcb6734SRoland McGrath 		       args[0], args[1], args[2], args[3], args[4], args[5],
509ebcb6734SRoland McGrath 		       sp, pc);
510a9712bc1SAl Viro 	unlock_trace(task);
511a9712bc1SAl Viro 	return res;
512ebcb6734SRoland McGrath }
513ebcb6734SRoland McGrath #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
514ebcb6734SRoland McGrath 
5151da177e4SLinus Torvalds /************************************************************************/
5161da177e4SLinus Torvalds /*                       Here the fs part begins                        */
5171da177e4SLinus Torvalds /************************************************************************/
5181da177e4SLinus Torvalds 
5191da177e4SLinus Torvalds /* permission checks */
520778c1144SEric W. Biederman static int proc_fd_access_allowed(struct inode *inode)
5211da177e4SLinus Torvalds {
522778c1144SEric W. Biederman 	struct task_struct *task;
523778c1144SEric W. Biederman 	int allowed = 0;
524df26c40eSEric W. Biederman 	/* Allow access to a task's file descriptors if it is us or we
525df26c40eSEric W. Biederman 	 * may use ptrace attach to the process and find out that
526df26c40eSEric W. Biederman 	 * information.
527778c1144SEric W. Biederman 	 */
528778c1144SEric W. Biederman 	task = get_proc_task(inode);
529df26c40eSEric W. Biederman 	if (task) {
530006ebb40SStephen Smalley 		allowed = ptrace_may_access(task, PTRACE_MODE_READ);
531778c1144SEric W. Biederman 		put_task_struct(task);
532df26c40eSEric W. Biederman 	}
533778c1144SEric W. Biederman 	return allowed;
5341da177e4SLinus Torvalds }
5351da177e4SLinus Torvalds 
5366b4e306aSEric W. Biederman int proc_setattr(struct dentry *dentry, struct iattr *attr)
5376d76fa58SLinus Torvalds {
5386d76fa58SLinus Torvalds 	int error;
5396d76fa58SLinus Torvalds 	struct inode *inode = dentry->d_inode;
5406d76fa58SLinus Torvalds 
5416d76fa58SLinus Torvalds 	if (attr->ia_valid & ATTR_MODE)
5426d76fa58SLinus Torvalds 		return -EPERM;
5436d76fa58SLinus Torvalds 
5446d76fa58SLinus Torvalds 	error = inode_change_ok(inode, attr);
5451025774cSChristoph Hellwig 	if (error)
5466d76fa58SLinus Torvalds 		return error;
5471025774cSChristoph Hellwig 
5481025774cSChristoph Hellwig 	setattr_copy(inode, attr);
5491025774cSChristoph Hellwig 	mark_inode_dirty(inode);
5501025774cSChristoph Hellwig 	return 0;
5516d76fa58SLinus Torvalds }
5526d76fa58SLinus Torvalds 
5530499680aSVasiliy Kulikov /*
5540499680aSVasiliy Kulikov  * May current process learn task's sched/cmdline info (for hide_pid_min=1)
5550499680aSVasiliy Kulikov  * or euid/egid (for hide_pid_min=2)?
5560499680aSVasiliy Kulikov  */
5570499680aSVasiliy Kulikov static bool has_pid_permissions(struct pid_namespace *pid,
5580499680aSVasiliy Kulikov 				 struct task_struct *task,
5590499680aSVasiliy Kulikov 				 int hide_pid_min)
5600499680aSVasiliy Kulikov {
5610499680aSVasiliy Kulikov 	if (pid->hide_pid < hide_pid_min)
5620499680aSVasiliy Kulikov 		return true;
5630499680aSVasiliy Kulikov 	if (in_group_p(pid->pid_gid))
5640499680aSVasiliy Kulikov 		return true;
5650499680aSVasiliy Kulikov 	return ptrace_may_access(task, PTRACE_MODE_READ);
5660499680aSVasiliy Kulikov }
5670499680aSVasiliy Kulikov 
5680499680aSVasiliy Kulikov 
5690499680aSVasiliy Kulikov static int proc_pid_permission(struct inode *inode, int mask)
5700499680aSVasiliy Kulikov {
5710499680aSVasiliy Kulikov 	struct pid_namespace *pid = inode->i_sb->s_fs_info;
5720499680aSVasiliy Kulikov 	struct task_struct *task;
5730499680aSVasiliy Kulikov 	bool has_perms;
5740499680aSVasiliy Kulikov 
5750499680aSVasiliy Kulikov 	task = get_proc_task(inode);
576a2ef990aSXiaotian Feng 	if (!task)
577a2ef990aSXiaotian Feng 		return -ESRCH;
5780499680aSVasiliy Kulikov 	has_perms = has_pid_permissions(pid, task, 1);
5790499680aSVasiliy Kulikov 	put_task_struct(task);
5800499680aSVasiliy Kulikov 
5810499680aSVasiliy Kulikov 	if (!has_perms) {
5820499680aSVasiliy Kulikov 		if (pid->hide_pid == 2) {
5830499680aSVasiliy Kulikov 			/*
5840499680aSVasiliy Kulikov 			 * Let's make getdents(), stat(), and open()
5850499680aSVasiliy Kulikov 			 * consistent with each other.  If a process
5860499680aSVasiliy Kulikov 			 * may not stat() a file, it shouldn't be seen
5870499680aSVasiliy Kulikov 			 * in procfs at all.
5880499680aSVasiliy Kulikov 			 */
5890499680aSVasiliy Kulikov 			return -ENOENT;
5900499680aSVasiliy Kulikov 		}
5910499680aSVasiliy Kulikov 
5920499680aSVasiliy Kulikov 		return -EPERM;
5930499680aSVasiliy Kulikov 	}
5940499680aSVasiliy Kulikov 	return generic_permission(inode, mask);
5950499680aSVasiliy Kulikov }
5960499680aSVasiliy Kulikov 
5970499680aSVasiliy Kulikov 
5980499680aSVasiliy Kulikov 
599c5ef1c42SArjan van de Ven static const struct inode_operations proc_def_inode_operations = {
6006d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
6016d76fa58SLinus Torvalds };
6026d76fa58SLinus Torvalds 
603be614086SEric W. Biederman static int proc_single_show(struct seq_file *m, void *v)
604be614086SEric W. Biederman {
605be614086SEric W. Biederman 	struct inode *inode = m->private;
606be614086SEric W. Biederman 	struct pid_namespace *ns;
607be614086SEric W. Biederman 	struct pid *pid;
608be614086SEric W. Biederman 	struct task_struct *task;
609be614086SEric W. Biederman 	int ret;
610be614086SEric W. Biederman 
611be614086SEric W. Biederman 	ns = inode->i_sb->s_fs_info;
612be614086SEric W. Biederman 	pid = proc_pid(inode);
613be614086SEric W. Biederman 	task = get_pid_task(pid, PIDTYPE_PID);
614be614086SEric W. Biederman 	if (!task)
615be614086SEric W. Biederman 		return -ESRCH;
616be614086SEric W. Biederman 
617be614086SEric W. Biederman 	ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
618be614086SEric W. Biederman 
619be614086SEric W. Biederman 	put_task_struct(task);
620be614086SEric W. Biederman 	return ret;
621be614086SEric W. Biederman }
622be614086SEric W. Biederman 
623be614086SEric W. Biederman static int proc_single_open(struct inode *inode, struct file *filp)
624be614086SEric W. Biederman {
625c6a34058SJovi Zhang 	return single_open(filp, proc_single_show, inode);
626be614086SEric W. Biederman }
627be614086SEric W. Biederman 
628be614086SEric W. Biederman static const struct file_operations proc_single_file_operations = {
629be614086SEric W. Biederman 	.open		= proc_single_open,
630be614086SEric W. Biederman 	.read		= seq_read,
631be614086SEric W. Biederman 	.llseek		= seq_lseek,
632be614086SEric W. Biederman 	.release	= single_release,
633be614086SEric W. Biederman };
634be614086SEric W. Biederman 
635b409e578SCong Wang static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
6361da177e4SLinus Torvalds {
637496ad9aaSAl Viro 	struct task_struct *task = get_proc_task(file_inode(file));
638e268337dSLinus Torvalds 	struct mm_struct *mm;
639e268337dSLinus Torvalds 
640e268337dSLinus Torvalds 	if (!task)
641e268337dSLinus Torvalds 		return -ESRCH;
642e268337dSLinus Torvalds 
643b409e578SCong Wang 	mm = mm_access(task, mode);
644e268337dSLinus Torvalds 	put_task_struct(task);
645e268337dSLinus Torvalds 
646e268337dSLinus Torvalds 	if (IS_ERR(mm))
647e268337dSLinus Torvalds 		return PTR_ERR(mm);
648e268337dSLinus Torvalds 
6496d08f2c7SOleg Nesterov 	if (mm) {
6506d08f2c7SOleg Nesterov 		/* ensure this mm_struct can't be freed */
6516d08f2c7SOleg Nesterov 		atomic_inc(&mm->mm_count);
6526d08f2c7SOleg Nesterov 		/* but do not pin its memory */
6536d08f2c7SOleg Nesterov 		mmput(mm);
6546d08f2c7SOleg Nesterov 	}
6556d08f2c7SOleg Nesterov 
656e268337dSLinus Torvalds 	file->private_data = mm;
657e268337dSLinus Torvalds 
6581da177e4SLinus Torvalds 	return 0;
6591da177e4SLinus Torvalds }
6601da177e4SLinus Torvalds 
661b409e578SCong Wang static int mem_open(struct inode *inode, struct file *file)
662b409e578SCong Wang {
663bc452b4bSDjalal Harouni 	int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
664bc452b4bSDjalal Harouni 
665bc452b4bSDjalal Harouni 	/* OK to pass negative loff_t, we can catch out-of-range */
666bc452b4bSDjalal Harouni 	file->f_mode |= FMODE_UNSIGNED_OFFSET;
667bc452b4bSDjalal Harouni 
668bc452b4bSDjalal Harouni 	return ret;
669b409e578SCong Wang }
670b409e578SCong Wang 
671572d34b9SOleg Nesterov static ssize_t mem_rw(struct file *file, char __user *buf,
672572d34b9SOleg Nesterov 			size_t count, loff_t *ppos, int write)
6731da177e4SLinus Torvalds {
674e268337dSLinus Torvalds 	struct mm_struct *mm = file->private_data;
675572d34b9SOleg Nesterov 	unsigned long addr = *ppos;
676572d34b9SOleg Nesterov 	ssize_t copied;
6771da177e4SLinus Torvalds 	char *page;
6781da177e4SLinus Torvalds 
679e268337dSLinus Torvalds 	if (!mm)
680e268337dSLinus Torvalds 		return 0;
6811da177e4SLinus Torvalds 
682e12ba74dSMel Gorman 	page = (char *)__get_free_page(GFP_TEMPORARY);
6831da177e4SLinus Torvalds 	if (!page)
684e268337dSLinus Torvalds 		return -ENOMEM;
6851da177e4SLinus Torvalds 
686f7ca54f4SFrederik Deweerdt 	copied = 0;
6876d08f2c7SOleg Nesterov 	if (!atomic_inc_not_zero(&mm->mm_users))
6886d08f2c7SOleg Nesterov 		goto free;
6896d08f2c7SOleg Nesterov 
6901da177e4SLinus Torvalds 	while (count > 0) {
691572d34b9SOleg Nesterov 		int this_len = min_t(int, count, PAGE_SIZE);
6921da177e4SLinus Torvalds 
693572d34b9SOleg Nesterov 		if (write && copy_from_user(page, buf, this_len)) {
6941da177e4SLinus Torvalds 			copied = -EFAULT;
6951da177e4SLinus Torvalds 			break;
6961da177e4SLinus Torvalds 		}
697572d34b9SOleg Nesterov 
698572d34b9SOleg Nesterov 		this_len = access_remote_vm(mm, addr, page, this_len, write);
699572d34b9SOleg Nesterov 		if (!this_len) {
7001da177e4SLinus Torvalds 			if (!copied)
7011da177e4SLinus Torvalds 				copied = -EIO;
7021da177e4SLinus Torvalds 			break;
7031da177e4SLinus Torvalds 		}
704572d34b9SOleg Nesterov 
705572d34b9SOleg Nesterov 		if (!write && copy_to_user(buf, page, this_len)) {
706572d34b9SOleg Nesterov 			copied = -EFAULT;
707572d34b9SOleg Nesterov 			break;
7081da177e4SLinus Torvalds 		}
709572d34b9SOleg Nesterov 
710572d34b9SOleg Nesterov 		buf += this_len;
711572d34b9SOleg Nesterov 		addr += this_len;
712572d34b9SOleg Nesterov 		copied += this_len;
713572d34b9SOleg Nesterov 		count -= this_len;
714572d34b9SOleg Nesterov 	}
715572d34b9SOleg Nesterov 	*ppos = addr;
71630cd8903SKOSAKI Motohiro 
7176d08f2c7SOleg Nesterov 	mmput(mm);
7186d08f2c7SOleg Nesterov free:
71930cd8903SKOSAKI Motohiro 	free_page((unsigned long) page);
7201da177e4SLinus Torvalds 	return copied;
7211da177e4SLinus Torvalds }
7221da177e4SLinus Torvalds 
723572d34b9SOleg Nesterov static ssize_t mem_read(struct file *file, char __user *buf,
724572d34b9SOleg Nesterov 			size_t count, loff_t *ppos)
725572d34b9SOleg Nesterov {
726572d34b9SOleg Nesterov 	return mem_rw(file, buf, count, ppos, 0);
727572d34b9SOleg Nesterov }
728572d34b9SOleg Nesterov 
729572d34b9SOleg Nesterov static ssize_t mem_write(struct file *file, const char __user *buf,
730572d34b9SOleg Nesterov 			 size_t count, loff_t *ppos)
731572d34b9SOleg Nesterov {
732572d34b9SOleg Nesterov 	return mem_rw(file, (char __user*)buf, count, ppos, 1);
733572d34b9SOleg Nesterov }
734572d34b9SOleg Nesterov 
73585863e47SMatt Mackall loff_t mem_lseek(struct file *file, loff_t offset, int orig)
7361da177e4SLinus Torvalds {
7371da177e4SLinus Torvalds 	switch (orig) {
7381da177e4SLinus Torvalds 	case 0:
7391da177e4SLinus Torvalds 		file->f_pos = offset;
7401da177e4SLinus Torvalds 		break;
7411da177e4SLinus Torvalds 	case 1:
7421da177e4SLinus Torvalds 		file->f_pos += offset;
7431da177e4SLinus Torvalds 		break;
7441da177e4SLinus Torvalds 	default:
7451da177e4SLinus Torvalds 		return -EINVAL;
7461da177e4SLinus Torvalds 	}
7471da177e4SLinus Torvalds 	force_successful_syscall_return();
7481da177e4SLinus Torvalds 	return file->f_pos;
7491da177e4SLinus Torvalds }
7501da177e4SLinus Torvalds 
751e268337dSLinus Torvalds static int mem_release(struct inode *inode, struct file *file)
752e268337dSLinus Torvalds {
753e268337dSLinus Torvalds 	struct mm_struct *mm = file->private_data;
75471879d3cSOleg Nesterov 	if (mm)
7556d08f2c7SOleg Nesterov 		mmdrop(mm);
756e268337dSLinus Torvalds 	return 0;
757e268337dSLinus Torvalds }
758e268337dSLinus Torvalds 
75900977a59SArjan van de Ven static const struct file_operations proc_mem_operations = {
7601da177e4SLinus Torvalds 	.llseek		= mem_lseek,
7611da177e4SLinus Torvalds 	.read		= mem_read,
7621da177e4SLinus Torvalds 	.write		= mem_write,
7631da177e4SLinus Torvalds 	.open		= mem_open,
764e268337dSLinus Torvalds 	.release	= mem_release,
7651da177e4SLinus Torvalds };
7661da177e4SLinus Torvalds 
767b409e578SCong Wang static int environ_open(struct inode *inode, struct file *file)
768b409e578SCong Wang {
769b409e578SCong Wang 	return __mem_open(inode, file, PTRACE_MODE_READ);
770b409e578SCong Wang }
771b409e578SCong Wang 
772315e28c8SJames Pearson static ssize_t environ_read(struct file *file, char __user *buf,
773315e28c8SJames Pearson 			size_t count, loff_t *ppos)
774315e28c8SJames Pearson {
775315e28c8SJames Pearson 	char *page;
776315e28c8SJames Pearson 	unsigned long src = *ppos;
777b409e578SCong Wang 	int ret = 0;
778b409e578SCong Wang 	struct mm_struct *mm = file->private_data;
779315e28c8SJames Pearson 
780b409e578SCong Wang 	if (!mm)
781b409e578SCong Wang 		return 0;
782315e28c8SJames Pearson 
783315e28c8SJames Pearson 	page = (char *)__get_free_page(GFP_TEMPORARY);
784315e28c8SJames Pearson 	if (!page)
785b409e578SCong Wang 		return -ENOMEM;
786315e28c8SJames Pearson 
787d6f64b89SAl Viro 	ret = 0;
788b409e578SCong Wang 	if (!atomic_inc_not_zero(&mm->mm_users))
789b409e578SCong Wang 		goto free;
790315e28c8SJames Pearson 	while (count > 0) {
791e8905ec2SDjalal Harouni 		size_t this_len, max_len;
792e8905ec2SDjalal Harouni 		int retval;
793e8905ec2SDjalal Harouni 
794e8905ec2SDjalal Harouni 		if (src >= (mm->env_end - mm->env_start))
795e8905ec2SDjalal Harouni 			break;
796315e28c8SJames Pearson 
797315e28c8SJames Pearson 		this_len = mm->env_end - (mm->env_start + src);
798315e28c8SJames Pearson 
799e8905ec2SDjalal Harouni 		max_len = min_t(size_t, PAGE_SIZE, count);
800e8905ec2SDjalal Harouni 		this_len = min(max_len, this_len);
801315e28c8SJames Pearson 
802b409e578SCong Wang 		retval = access_remote_vm(mm, (mm->env_start + src),
803315e28c8SJames Pearson 			page, this_len, 0);
804315e28c8SJames Pearson 
805315e28c8SJames Pearson 		if (retval <= 0) {
806315e28c8SJames Pearson 			ret = retval;
807315e28c8SJames Pearson 			break;
808315e28c8SJames Pearson 		}
809315e28c8SJames Pearson 
810315e28c8SJames Pearson 		if (copy_to_user(buf, page, retval)) {
811315e28c8SJames Pearson 			ret = -EFAULT;
812315e28c8SJames Pearson 			break;
813315e28c8SJames Pearson 		}
814315e28c8SJames Pearson 
815315e28c8SJames Pearson 		ret += retval;
816315e28c8SJames Pearson 		src += retval;
817315e28c8SJames Pearson 		buf += retval;
818315e28c8SJames Pearson 		count -= retval;
819315e28c8SJames Pearson 	}
820315e28c8SJames Pearson 	*ppos = src;
821315e28c8SJames Pearson 	mmput(mm);
822b409e578SCong Wang 
823b409e578SCong Wang free:
824315e28c8SJames Pearson 	free_page((unsigned long) page);
825315e28c8SJames Pearson 	return ret;
826315e28c8SJames Pearson }
827315e28c8SJames Pearson 
828315e28c8SJames Pearson static const struct file_operations proc_environ_operations = {
829b409e578SCong Wang 	.open		= environ_open,
830315e28c8SJames Pearson 	.read		= environ_read,
83187df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
832b409e578SCong Wang 	.release	= mem_release,
833315e28c8SJames Pearson };
834315e28c8SJames Pearson 
835fa0cbbf1SDavid Rientjes static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
836fa0cbbf1SDavid Rientjes 			    loff_t *ppos)
837fa0cbbf1SDavid Rientjes {
838496ad9aaSAl Viro 	struct task_struct *task = get_proc_task(file_inode(file));
839fa0cbbf1SDavid Rientjes 	char buffer[PROC_NUMBUF];
840fa0cbbf1SDavid Rientjes 	int oom_adj = OOM_ADJUST_MIN;
841fa0cbbf1SDavid Rientjes 	size_t len;
842fa0cbbf1SDavid Rientjes 	unsigned long flags;
843fa0cbbf1SDavid Rientjes 
844fa0cbbf1SDavid Rientjes 	if (!task)
845fa0cbbf1SDavid Rientjes 		return -ESRCH;
846fa0cbbf1SDavid Rientjes 	if (lock_task_sighand(task, &flags)) {
847fa0cbbf1SDavid Rientjes 		if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
848fa0cbbf1SDavid Rientjes 			oom_adj = OOM_ADJUST_MAX;
849fa0cbbf1SDavid Rientjes 		else
850fa0cbbf1SDavid Rientjes 			oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
851fa0cbbf1SDavid Rientjes 				  OOM_SCORE_ADJ_MAX;
852fa0cbbf1SDavid Rientjes 		unlock_task_sighand(task, &flags);
853fa0cbbf1SDavid Rientjes 	}
854fa0cbbf1SDavid Rientjes 	put_task_struct(task);
855fa0cbbf1SDavid Rientjes 	len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
856fa0cbbf1SDavid Rientjes 	return simple_read_from_buffer(buf, count, ppos, buffer, len);
857fa0cbbf1SDavid Rientjes }
858fa0cbbf1SDavid Rientjes 
859fa0cbbf1SDavid Rientjes static ssize_t oom_adj_write(struct file *file, const char __user *buf,
860fa0cbbf1SDavid Rientjes 			     size_t count, loff_t *ppos)
861fa0cbbf1SDavid Rientjes {
862fa0cbbf1SDavid Rientjes 	struct task_struct *task;
863fa0cbbf1SDavid Rientjes 	char buffer[PROC_NUMBUF];
864fa0cbbf1SDavid Rientjes 	int oom_adj;
865fa0cbbf1SDavid Rientjes 	unsigned long flags;
866fa0cbbf1SDavid Rientjes 	int err;
867fa0cbbf1SDavid Rientjes 
868fa0cbbf1SDavid Rientjes 	memset(buffer, 0, sizeof(buffer));
869fa0cbbf1SDavid Rientjes 	if (count > sizeof(buffer) - 1)
870fa0cbbf1SDavid Rientjes 		count = sizeof(buffer) - 1;
871fa0cbbf1SDavid Rientjes 	if (copy_from_user(buffer, buf, count)) {
872fa0cbbf1SDavid Rientjes 		err = -EFAULT;
873fa0cbbf1SDavid Rientjes 		goto out;
874fa0cbbf1SDavid Rientjes 	}
875fa0cbbf1SDavid Rientjes 
876fa0cbbf1SDavid Rientjes 	err = kstrtoint(strstrip(buffer), 0, &oom_adj);
877fa0cbbf1SDavid Rientjes 	if (err)
878fa0cbbf1SDavid Rientjes 		goto out;
879fa0cbbf1SDavid Rientjes 	if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
880fa0cbbf1SDavid Rientjes 	     oom_adj != OOM_DISABLE) {
881fa0cbbf1SDavid Rientjes 		err = -EINVAL;
882fa0cbbf1SDavid Rientjes 		goto out;
883fa0cbbf1SDavid Rientjes 	}
884fa0cbbf1SDavid Rientjes 
885496ad9aaSAl Viro 	task = get_proc_task(file_inode(file));
886fa0cbbf1SDavid Rientjes 	if (!task) {
887fa0cbbf1SDavid Rientjes 		err = -ESRCH;
888fa0cbbf1SDavid Rientjes 		goto out;
889fa0cbbf1SDavid Rientjes 	}
890fa0cbbf1SDavid Rientjes 
891fa0cbbf1SDavid Rientjes 	task_lock(task);
892fa0cbbf1SDavid Rientjes 	if (!task->mm) {
893fa0cbbf1SDavid Rientjes 		err = -EINVAL;
894fa0cbbf1SDavid Rientjes 		goto err_task_lock;
895fa0cbbf1SDavid Rientjes 	}
896fa0cbbf1SDavid Rientjes 
897fa0cbbf1SDavid Rientjes 	if (!lock_task_sighand(task, &flags)) {
898fa0cbbf1SDavid Rientjes 		err = -ESRCH;
899fa0cbbf1SDavid Rientjes 		goto err_task_lock;
900fa0cbbf1SDavid Rientjes 	}
901fa0cbbf1SDavid Rientjes 
902fa0cbbf1SDavid Rientjes 	/*
903fa0cbbf1SDavid Rientjes 	 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
904fa0cbbf1SDavid Rientjes 	 * value is always attainable.
905fa0cbbf1SDavid Rientjes 	 */
906fa0cbbf1SDavid Rientjes 	if (oom_adj == OOM_ADJUST_MAX)
907fa0cbbf1SDavid Rientjes 		oom_adj = OOM_SCORE_ADJ_MAX;
908fa0cbbf1SDavid Rientjes 	else
909fa0cbbf1SDavid Rientjes 		oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
910fa0cbbf1SDavid Rientjes 
911fa0cbbf1SDavid Rientjes 	if (oom_adj < task->signal->oom_score_adj &&
912fa0cbbf1SDavid Rientjes 	    !capable(CAP_SYS_RESOURCE)) {
913fa0cbbf1SDavid Rientjes 		err = -EACCES;
914fa0cbbf1SDavid Rientjes 		goto err_sighand;
915fa0cbbf1SDavid Rientjes 	}
916fa0cbbf1SDavid Rientjes 
917fa0cbbf1SDavid Rientjes 	/*
918fa0cbbf1SDavid Rientjes 	 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
919fa0cbbf1SDavid Rientjes 	 * /proc/pid/oom_score_adj instead.
920fa0cbbf1SDavid Rientjes 	 */
92187ebdc00SAndrew Morton 	pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
922fa0cbbf1SDavid Rientjes 		  current->comm, task_pid_nr(current), task_pid_nr(task),
923fa0cbbf1SDavid Rientjes 		  task_pid_nr(task));
924fa0cbbf1SDavid Rientjes 
925fa0cbbf1SDavid Rientjes 	task->signal->oom_score_adj = oom_adj;
926fa0cbbf1SDavid Rientjes 	trace_oom_score_adj_update(task);
927fa0cbbf1SDavid Rientjes err_sighand:
928fa0cbbf1SDavid Rientjes 	unlock_task_sighand(task, &flags);
929fa0cbbf1SDavid Rientjes err_task_lock:
930fa0cbbf1SDavid Rientjes 	task_unlock(task);
931fa0cbbf1SDavid Rientjes 	put_task_struct(task);
932fa0cbbf1SDavid Rientjes out:
933fa0cbbf1SDavid Rientjes 	return err < 0 ? err : count;
934fa0cbbf1SDavid Rientjes }
935fa0cbbf1SDavid Rientjes 
936fa0cbbf1SDavid Rientjes static const struct file_operations proc_oom_adj_operations = {
937fa0cbbf1SDavid Rientjes 	.read		= oom_adj_read,
938fa0cbbf1SDavid Rientjes 	.write		= oom_adj_write,
939fa0cbbf1SDavid Rientjes 	.llseek		= generic_file_llseek,
940fa0cbbf1SDavid Rientjes };
941fa0cbbf1SDavid Rientjes 
942a63d83f4SDavid Rientjes static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
943a63d83f4SDavid Rientjes 					size_t count, loff_t *ppos)
944a63d83f4SDavid Rientjes {
945496ad9aaSAl Viro 	struct task_struct *task = get_proc_task(file_inode(file));
946a63d83f4SDavid Rientjes 	char buffer[PROC_NUMBUF];
947a9c58b90SDavid Rientjes 	short oom_score_adj = OOM_SCORE_ADJ_MIN;
948a63d83f4SDavid Rientjes 	unsigned long flags;
949a63d83f4SDavid Rientjes 	size_t len;
950a63d83f4SDavid Rientjes 
951a63d83f4SDavid Rientjes 	if (!task)
952a63d83f4SDavid Rientjes 		return -ESRCH;
953a63d83f4SDavid Rientjes 	if (lock_task_sighand(task, &flags)) {
954a63d83f4SDavid Rientjes 		oom_score_adj = task->signal->oom_score_adj;
955a63d83f4SDavid Rientjes 		unlock_task_sighand(task, &flags);
956a63d83f4SDavid Rientjes 	}
957a63d83f4SDavid Rientjes 	put_task_struct(task);
958a9c58b90SDavid Rientjes 	len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
959a63d83f4SDavid Rientjes 	return simple_read_from_buffer(buf, count, ppos, buffer, len);
960a63d83f4SDavid Rientjes }
961a63d83f4SDavid Rientjes 
962a63d83f4SDavid Rientjes static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
963a63d83f4SDavid Rientjes 					size_t count, loff_t *ppos)
964a63d83f4SDavid Rientjes {
965a63d83f4SDavid Rientjes 	struct task_struct *task;
966a63d83f4SDavid Rientjes 	char buffer[PROC_NUMBUF];
967a63d83f4SDavid Rientjes 	unsigned long flags;
9680a8cb8e3SAlexey Dobriyan 	int oom_score_adj;
969a63d83f4SDavid Rientjes 	int err;
970a63d83f4SDavid Rientjes 
971a63d83f4SDavid Rientjes 	memset(buffer, 0, sizeof(buffer));
972a63d83f4SDavid Rientjes 	if (count > sizeof(buffer) - 1)
973a63d83f4SDavid Rientjes 		count = sizeof(buffer) - 1;
974723548bfSDavid Rientjes 	if (copy_from_user(buffer, buf, count)) {
975723548bfSDavid Rientjes 		err = -EFAULT;
976723548bfSDavid Rientjes 		goto out;
977723548bfSDavid Rientjes 	}
978a63d83f4SDavid Rientjes 
9790a8cb8e3SAlexey Dobriyan 	err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
980a63d83f4SDavid Rientjes 	if (err)
981723548bfSDavid Rientjes 		goto out;
982a63d83f4SDavid Rientjes 	if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
983723548bfSDavid Rientjes 			oom_score_adj > OOM_SCORE_ADJ_MAX) {
984723548bfSDavid Rientjes 		err = -EINVAL;
985723548bfSDavid Rientjes 		goto out;
986723548bfSDavid Rientjes 	}
987a63d83f4SDavid Rientjes 
988496ad9aaSAl Viro 	task = get_proc_task(file_inode(file));
989723548bfSDavid Rientjes 	if (!task) {
990723548bfSDavid Rientjes 		err = -ESRCH;
991723548bfSDavid Rientjes 		goto out;
992723548bfSDavid Rientjes 	}
993a63d83f4SDavid Rientjes 
9943d5992d2SYing Han 	task_lock(task);
9953d5992d2SYing Han 	if (!task->mm) {
996723548bfSDavid Rientjes 		err = -EINVAL;
997723548bfSDavid Rientjes 		goto err_task_lock;
9983d5992d2SYing Han 	}
999d19d5476SDavid Rientjes 
1000d19d5476SDavid Rientjes 	if (!lock_task_sighand(task, &flags)) {
1001d19d5476SDavid Rientjes 		err = -ESRCH;
1002d19d5476SDavid Rientjes 		goto err_task_lock;
1003d19d5476SDavid Rientjes 	}
1004d19d5476SDavid Rientjes 
1005a9c58b90SDavid Rientjes 	if ((short)oom_score_adj < task->signal->oom_score_adj_min &&
1006d19d5476SDavid Rientjes 			!capable(CAP_SYS_RESOURCE)) {
1007d19d5476SDavid Rientjes 		err = -EACCES;
1008d19d5476SDavid Rientjes 		goto err_sighand;
1009d19d5476SDavid Rientjes 	}
1010d19d5476SDavid Rientjes 
1011a9c58b90SDavid Rientjes 	task->signal->oom_score_adj = (short)oom_score_adj;
1012dabb16f6SMandeep Singh Baines 	if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
1013a9c58b90SDavid Rientjes 		task->signal->oom_score_adj_min = (short)oom_score_adj;
101443d2b113SKAMEZAWA Hiroyuki 	trace_oom_score_adj_update(task);
101501dc52ebSDavidlohr Bueso 
1016723548bfSDavid Rientjes err_sighand:
1017a63d83f4SDavid Rientjes 	unlock_task_sighand(task, &flags);
1018d19d5476SDavid Rientjes err_task_lock:
1019d19d5476SDavid Rientjes 	task_unlock(task);
1020a63d83f4SDavid Rientjes 	put_task_struct(task);
1021723548bfSDavid Rientjes out:
1022723548bfSDavid Rientjes 	return err < 0 ? err : count;
1023a63d83f4SDavid Rientjes }
1024a63d83f4SDavid Rientjes 
1025a63d83f4SDavid Rientjes static const struct file_operations proc_oom_score_adj_operations = {
1026a63d83f4SDavid Rientjes 	.read		= oom_score_adj_read,
1027a63d83f4SDavid Rientjes 	.write		= oom_score_adj_write,
10286038f373SArnd Bergmann 	.llseek		= default_llseek,
1029a63d83f4SDavid Rientjes };
1030a63d83f4SDavid Rientjes 
10311da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL
10321da177e4SLinus Torvalds #define TMPBUFLEN 21
10331da177e4SLinus Torvalds static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
10341da177e4SLinus Torvalds 				  size_t count, loff_t *ppos)
10351da177e4SLinus Torvalds {
1036496ad9aaSAl Viro 	struct inode * inode = file_inode(file);
103799f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
10381da177e4SLinus Torvalds 	ssize_t length;
10391da177e4SLinus Torvalds 	char tmpbuf[TMPBUFLEN];
10401da177e4SLinus Torvalds 
104199f89551SEric W. Biederman 	if (!task)
104299f89551SEric W. Biederman 		return -ESRCH;
10431da177e4SLinus Torvalds 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1044e1760bd5SEric W. Biederman 			   from_kuid(file->f_cred->user_ns,
1045e1760bd5SEric W. Biederman 				     audit_get_loginuid(task)));
104699f89551SEric W. Biederman 	put_task_struct(task);
10471da177e4SLinus Torvalds 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
10481da177e4SLinus Torvalds }
10491da177e4SLinus Torvalds 
10501da177e4SLinus Torvalds static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
10511da177e4SLinus Torvalds 				   size_t count, loff_t *ppos)
10521da177e4SLinus Torvalds {
1053496ad9aaSAl Viro 	struct inode * inode = file_inode(file);
10541da177e4SLinus Torvalds 	char *page, *tmp;
10551da177e4SLinus Torvalds 	ssize_t length;
10561da177e4SLinus Torvalds 	uid_t loginuid;
1057e1760bd5SEric W. Biederman 	kuid_t kloginuid;
10581da177e4SLinus Torvalds 
10597dc52157SPaul E. McKenney 	rcu_read_lock();
10607dc52157SPaul E. McKenney 	if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
10617dc52157SPaul E. McKenney 		rcu_read_unlock();
10621da177e4SLinus Torvalds 		return -EPERM;
10637dc52157SPaul E. McKenney 	}
10647dc52157SPaul E. McKenney 	rcu_read_unlock();
10651da177e4SLinus Torvalds 
1066e0182909SAl Viro 	if (count >= PAGE_SIZE)
1067e0182909SAl Viro 		count = PAGE_SIZE - 1;
10681da177e4SLinus Torvalds 
10691da177e4SLinus Torvalds 	if (*ppos != 0) {
10701da177e4SLinus Torvalds 		/* No partial writes. */
10711da177e4SLinus Torvalds 		return -EINVAL;
10721da177e4SLinus Torvalds 	}
1073e12ba74dSMel Gorman 	page = (char*)__get_free_page(GFP_TEMPORARY);
10741da177e4SLinus Torvalds 	if (!page)
10751da177e4SLinus Torvalds 		return -ENOMEM;
10761da177e4SLinus Torvalds 	length = -EFAULT;
10771da177e4SLinus Torvalds 	if (copy_from_user(page, buf, count))
10781da177e4SLinus Torvalds 		goto out_free_page;
10791da177e4SLinus Torvalds 
1080e0182909SAl Viro 	page[count] = '\0';
10811da177e4SLinus Torvalds 	loginuid = simple_strtoul(page, &tmp, 10);
10821da177e4SLinus Torvalds 	if (tmp == page) {
10831da177e4SLinus Torvalds 		length = -EINVAL;
10841da177e4SLinus Torvalds 		goto out_free_page;
10851da177e4SLinus Torvalds 
10861da177e4SLinus Torvalds 	}
108781407c84SEric Paris 
108881407c84SEric Paris 	/* is userspace tring to explicitly UNSET the loginuid? */
108981407c84SEric Paris 	if (loginuid == AUDIT_UID_UNSET) {
109081407c84SEric Paris 		kloginuid = INVALID_UID;
109181407c84SEric Paris 	} else {
1092e1760bd5SEric W. Biederman 		kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
1093e1760bd5SEric W. Biederman 		if (!uid_valid(kloginuid)) {
1094e1760bd5SEric W. Biederman 			length = -EINVAL;
1095e1760bd5SEric W. Biederman 			goto out_free_page;
1096e1760bd5SEric W. Biederman 		}
109781407c84SEric Paris 	}
1098e1760bd5SEric W. Biederman 
1099e1760bd5SEric W. Biederman 	length = audit_set_loginuid(kloginuid);
11001da177e4SLinus Torvalds 	if (likely(length == 0))
11011da177e4SLinus Torvalds 		length = count;
11021da177e4SLinus Torvalds 
11031da177e4SLinus Torvalds out_free_page:
11041da177e4SLinus Torvalds 	free_page((unsigned long) page);
11051da177e4SLinus Torvalds 	return length;
11061da177e4SLinus Torvalds }
11071da177e4SLinus Torvalds 
110800977a59SArjan van de Ven static const struct file_operations proc_loginuid_operations = {
11091da177e4SLinus Torvalds 	.read		= proc_loginuid_read,
11101da177e4SLinus Torvalds 	.write		= proc_loginuid_write,
111187df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
11121da177e4SLinus Torvalds };
11131e0bd755SEric Paris 
11141e0bd755SEric Paris static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
11151e0bd755SEric Paris 				  size_t count, loff_t *ppos)
11161e0bd755SEric Paris {
1117496ad9aaSAl Viro 	struct inode * inode = file_inode(file);
11181e0bd755SEric Paris 	struct task_struct *task = get_proc_task(inode);
11191e0bd755SEric Paris 	ssize_t length;
11201e0bd755SEric Paris 	char tmpbuf[TMPBUFLEN];
11211e0bd755SEric Paris 
11221e0bd755SEric Paris 	if (!task)
11231e0bd755SEric Paris 		return -ESRCH;
11241e0bd755SEric Paris 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
11251e0bd755SEric Paris 				audit_get_sessionid(task));
11261e0bd755SEric Paris 	put_task_struct(task);
11271e0bd755SEric Paris 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
11281e0bd755SEric Paris }
11291e0bd755SEric Paris 
11301e0bd755SEric Paris static const struct file_operations proc_sessionid_operations = {
11311e0bd755SEric Paris 	.read		= proc_sessionid_read,
113287df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
11331e0bd755SEric Paris };
11341da177e4SLinus Torvalds #endif
11351da177e4SLinus Torvalds 
1136f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
1137f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1138f4f154fdSAkinobu Mita 				      size_t count, loff_t *ppos)
1139f4f154fdSAkinobu Mita {
1140496ad9aaSAl Viro 	struct task_struct *task = get_proc_task(file_inode(file));
1141f4f154fdSAkinobu Mita 	char buffer[PROC_NUMBUF];
1142f4f154fdSAkinobu Mita 	size_t len;
1143f4f154fdSAkinobu Mita 	int make_it_fail;
1144f4f154fdSAkinobu Mita 
1145f4f154fdSAkinobu Mita 	if (!task)
1146f4f154fdSAkinobu Mita 		return -ESRCH;
1147f4f154fdSAkinobu Mita 	make_it_fail = task->make_it_fail;
1148f4f154fdSAkinobu Mita 	put_task_struct(task);
1149f4f154fdSAkinobu Mita 
1150f4f154fdSAkinobu Mita 	len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
11510c28f287SAkinobu Mita 
11520c28f287SAkinobu Mita 	return simple_read_from_buffer(buf, count, ppos, buffer, len);
1153f4f154fdSAkinobu Mita }
1154f4f154fdSAkinobu Mita 
1155f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_write(struct file * file,
1156f4f154fdSAkinobu Mita 			const char __user * buf, size_t count, loff_t *ppos)
1157f4f154fdSAkinobu Mita {
1158f4f154fdSAkinobu Mita 	struct task_struct *task;
1159f4f154fdSAkinobu Mita 	char buffer[PROC_NUMBUF], *end;
1160f4f154fdSAkinobu Mita 	int make_it_fail;
1161f4f154fdSAkinobu Mita 
1162f4f154fdSAkinobu Mita 	if (!capable(CAP_SYS_RESOURCE))
1163f4f154fdSAkinobu Mita 		return -EPERM;
1164f4f154fdSAkinobu Mita 	memset(buffer, 0, sizeof(buffer));
1165f4f154fdSAkinobu Mita 	if (count > sizeof(buffer) - 1)
1166f4f154fdSAkinobu Mita 		count = sizeof(buffer) - 1;
1167f4f154fdSAkinobu Mita 	if (copy_from_user(buffer, buf, count))
1168f4f154fdSAkinobu Mita 		return -EFAULT;
1169cba8aafeSVincent Li 	make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1170cba8aafeSVincent Li 	if (*end)
1171cba8aafeSVincent Li 		return -EINVAL;
117216caed31SDave Jones 	if (make_it_fail < 0 || make_it_fail > 1)
117316caed31SDave Jones 		return -EINVAL;
117416caed31SDave Jones 
1175496ad9aaSAl Viro 	task = get_proc_task(file_inode(file));
1176f4f154fdSAkinobu Mita 	if (!task)
1177f4f154fdSAkinobu Mita 		return -ESRCH;
1178f4f154fdSAkinobu Mita 	task->make_it_fail = make_it_fail;
1179f4f154fdSAkinobu Mita 	put_task_struct(task);
1180cba8aafeSVincent Li 
1181cba8aafeSVincent Li 	return count;
1182f4f154fdSAkinobu Mita }
1183f4f154fdSAkinobu Mita 
118400977a59SArjan van de Ven static const struct file_operations proc_fault_inject_operations = {
1185f4f154fdSAkinobu Mita 	.read		= proc_fault_inject_read,
1186f4f154fdSAkinobu Mita 	.write		= proc_fault_inject_write,
118787df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
1188f4f154fdSAkinobu Mita };
1189f4f154fdSAkinobu Mita #endif
1190f4f154fdSAkinobu Mita 
11919745512cSArjan van de Ven 
119243ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
119343ae34cbSIngo Molnar /*
119443ae34cbSIngo Molnar  * Print out various scheduling related per-task fields:
119543ae34cbSIngo Molnar  */
119643ae34cbSIngo Molnar static int sched_show(struct seq_file *m, void *v)
119743ae34cbSIngo Molnar {
119843ae34cbSIngo Molnar 	struct inode *inode = m->private;
119943ae34cbSIngo Molnar 	struct task_struct *p;
120043ae34cbSIngo Molnar 
120143ae34cbSIngo Molnar 	p = get_proc_task(inode);
120243ae34cbSIngo Molnar 	if (!p)
120343ae34cbSIngo Molnar 		return -ESRCH;
120443ae34cbSIngo Molnar 	proc_sched_show_task(p, m);
120543ae34cbSIngo Molnar 
120643ae34cbSIngo Molnar 	put_task_struct(p);
120743ae34cbSIngo Molnar 
120843ae34cbSIngo Molnar 	return 0;
120943ae34cbSIngo Molnar }
121043ae34cbSIngo Molnar 
121143ae34cbSIngo Molnar static ssize_t
121243ae34cbSIngo Molnar sched_write(struct file *file, const char __user *buf,
121343ae34cbSIngo Molnar 	    size_t count, loff_t *offset)
121443ae34cbSIngo Molnar {
1215496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
121643ae34cbSIngo Molnar 	struct task_struct *p;
121743ae34cbSIngo Molnar 
121843ae34cbSIngo Molnar 	p = get_proc_task(inode);
121943ae34cbSIngo Molnar 	if (!p)
122043ae34cbSIngo Molnar 		return -ESRCH;
122143ae34cbSIngo Molnar 	proc_sched_set_task(p);
122243ae34cbSIngo Molnar 
122343ae34cbSIngo Molnar 	put_task_struct(p);
122443ae34cbSIngo Molnar 
122543ae34cbSIngo Molnar 	return count;
122643ae34cbSIngo Molnar }
122743ae34cbSIngo Molnar 
122843ae34cbSIngo Molnar static int sched_open(struct inode *inode, struct file *filp)
122943ae34cbSIngo Molnar {
1230c6a34058SJovi Zhang 	return single_open(filp, sched_show, inode);
123143ae34cbSIngo Molnar }
123243ae34cbSIngo Molnar 
123343ae34cbSIngo Molnar static const struct file_operations proc_pid_sched_operations = {
123443ae34cbSIngo Molnar 	.open		= sched_open,
123543ae34cbSIngo Molnar 	.read		= seq_read,
123643ae34cbSIngo Molnar 	.write		= sched_write,
123743ae34cbSIngo Molnar 	.llseek		= seq_lseek,
12385ea473a1SAlexey Dobriyan 	.release	= single_release,
123943ae34cbSIngo Molnar };
124043ae34cbSIngo Molnar 
124143ae34cbSIngo Molnar #endif
124243ae34cbSIngo Molnar 
12435091faa4SMike Galbraith #ifdef CONFIG_SCHED_AUTOGROUP
12445091faa4SMike Galbraith /*
12455091faa4SMike Galbraith  * Print out autogroup related information:
12465091faa4SMike Galbraith  */
12475091faa4SMike Galbraith static int sched_autogroup_show(struct seq_file *m, void *v)
12485091faa4SMike Galbraith {
12495091faa4SMike Galbraith 	struct inode *inode = m->private;
12505091faa4SMike Galbraith 	struct task_struct *p;
12515091faa4SMike Galbraith 
12525091faa4SMike Galbraith 	p = get_proc_task(inode);
12535091faa4SMike Galbraith 	if (!p)
12545091faa4SMike Galbraith 		return -ESRCH;
12555091faa4SMike Galbraith 	proc_sched_autogroup_show_task(p, m);
12565091faa4SMike Galbraith 
12575091faa4SMike Galbraith 	put_task_struct(p);
12585091faa4SMike Galbraith 
12595091faa4SMike Galbraith 	return 0;
12605091faa4SMike Galbraith }
12615091faa4SMike Galbraith 
12625091faa4SMike Galbraith static ssize_t
12635091faa4SMike Galbraith sched_autogroup_write(struct file *file, const char __user *buf,
12645091faa4SMike Galbraith 	    size_t count, loff_t *offset)
12655091faa4SMike Galbraith {
1266496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
12675091faa4SMike Galbraith 	struct task_struct *p;
12685091faa4SMike Galbraith 	char buffer[PROC_NUMBUF];
12690a8cb8e3SAlexey Dobriyan 	int nice;
12705091faa4SMike Galbraith 	int err;
12715091faa4SMike Galbraith 
12725091faa4SMike Galbraith 	memset(buffer, 0, sizeof(buffer));
12735091faa4SMike Galbraith 	if (count > sizeof(buffer) - 1)
12745091faa4SMike Galbraith 		count = sizeof(buffer) - 1;
12755091faa4SMike Galbraith 	if (copy_from_user(buffer, buf, count))
12765091faa4SMike Galbraith 		return -EFAULT;
12775091faa4SMike Galbraith 
12780a8cb8e3SAlexey Dobriyan 	err = kstrtoint(strstrip(buffer), 0, &nice);
12790a8cb8e3SAlexey Dobriyan 	if (err < 0)
12800a8cb8e3SAlexey Dobriyan 		return err;
12815091faa4SMike Galbraith 
12825091faa4SMike Galbraith 	p = get_proc_task(inode);
12835091faa4SMike Galbraith 	if (!p)
12845091faa4SMike Galbraith 		return -ESRCH;
12855091faa4SMike Galbraith 
12862e5b5b3aSHiroshi Shimamoto 	err = proc_sched_autogroup_set_nice(p, nice);
12875091faa4SMike Galbraith 	if (err)
12885091faa4SMike Galbraith 		count = err;
12895091faa4SMike Galbraith 
12905091faa4SMike Galbraith 	put_task_struct(p);
12915091faa4SMike Galbraith 
12925091faa4SMike Galbraith 	return count;
12935091faa4SMike Galbraith }
12945091faa4SMike Galbraith 
12955091faa4SMike Galbraith static int sched_autogroup_open(struct inode *inode, struct file *filp)
12965091faa4SMike Galbraith {
12975091faa4SMike Galbraith 	int ret;
12985091faa4SMike Galbraith 
12995091faa4SMike Galbraith 	ret = single_open(filp, sched_autogroup_show, NULL);
13005091faa4SMike Galbraith 	if (!ret) {
13015091faa4SMike Galbraith 		struct seq_file *m = filp->private_data;
13025091faa4SMike Galbraith 
13035091faa4SMike Galbraith 		m->private = inode;
13045091faa4SMike Galbraith 	}
13055091faa4SMike Galbraith 	return ret;
13065091faa4SMike Galbraith }
13075091faa4SMike Galbraith 
13085091faa4SMike Galbraith static const struct file_operations proc_pid_sched_autogroup_operations = {
13095091faa4SMike Galbraith 	.open		= sched_autogroup_open,
13105091faa4SMike Galbraith 	.read		= seq_read,
13115091faa4SMike Galbraith 	.write		= sched_autogroup_write,
13125091faa4SMike Galbraith 	.llseek		= seq_lseek,
13135091faa4SMike Galbraith 	.release	= single_release,
13145091faa4SMike Galbraith };
13155091faa4SMike Galbraith 
13165091faa4SMike Galbraith #endif /* CONFIG_SCHED_AUTOGROUP */
13175091faa4SMike Galbraith 
13184614a696Sjohn stultz static ssize_t comm_write(struct file *file, const char __user *buf,
13194614a696Sjohn stultz 				size_t count, loff_t *offset)
13204614a696Sjohn stultz {
1321496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
13224614a696Sjohn stultz 	struct task_struct *p;
13234614a696Sjohn stultz 	char buffer[TASK_COMM_LEN];
1324830e0fc9SDavid Rientjes 	const size_t maxlen = sizeof(buffer) - 1;
13254614a696Sjohn stultz 
13264614a696Sjohn stultz 	memset(buffer, 0, sizeof(buffer));
1327830e0fc9SDavid Rientjes 	if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
13284614a696Sjohn stultz 		return -EFAULT;
13294614a696Sjohn stultz 
13304614a696Sjohn stultz 	p = get_proc_task(inode);
13314614a696Sjohn stultz 	if (!p)
13324614a696Sjohn stultz 		return -ESRCH;
13334614a696Sjohn stultz 
13344614a696Sjohn stultz 	if (same_thread_group(current, p))
13354614a696Sjohn stultz 		set_task_comm(p, buffer);
13364614a696Sjohn stultz 	else
13374614a696Sjohn stultz 		count = -EINVAL;
13384614a696Sjohn stultz 
13394614a696Sjohn stultz 	put_task_struct(p);
13404614a696Sjohn stultz 
13414614a696Sjohn stultz 	return count;
13424614a696Sjohn stultz }
13434614a696Sjohn stultz 
13444614a696Sjohn stultz static int comm_show(struct seq_file *m, void *v)
13454614a696Sjohn stultz {
13464614a696Sjohn stultz 	struct inode *inode = m->private;
13474614a696Sjohn stultz 	struct task_struct *p;
13484614a696Sjohn stultz 
13494614a696Sjohn stultz 	p = get_proc_task(inode);
13504614a696Sjohn stultz 	if (!p)
13514614a696Sjohn stultz 		return -ESRCH;
13524614a696Sjohn stultz 
13534614a696Sjohn stultz 	task_lock(p);
13544614a696Sjohn stultz 	seq_printf(m, "%s\n", p->comm);
13554614a696Sjohn stultz 	task_unlock(p);
13564614a696Sjohn stultz 
13574614a696Sjohn stultz 	put_task_struct(p);
13584614a696Sjohn stultz 
13594614a696Sjohn stultz 	return 0;
13604614a696Sjohn stultz }
13614614a696Sjohn stultz 
13624614a696Sjohn stultz static int comm_open(struct inode *inode, struct file *filp)
13634614a696Sjohn stultz {
1364c6a34058SJovi Zhang 	return single_open(filp, comm_show, inode);
13654614a696Sjohn stultz }
13664614a696Sjohn stultz 
13674614a696Sjohn stultz static const struct file_operations proc_pid_set_comm_operations = {
13684614a696Sjohn stultz 	.open		= comm_open,
13694614a696Sjohn stultz 	.read		= seq_read,
13704614a696Sjohn stultz 	.write		= comm_write,
13714614a696Sjohn stultz 	.llseek		= seq_lseek,
13724614a696Sjohn stultz 	.release	= single_release,
13734614a696Sjohn stultz };
13744614a696Sjohn stultz 
13757773fbc5SCyrill Gorcunov static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
1376925d1c40SMatt Helsley {
1377925d1c40SMatt Helsley 	struct task_struct *task;
1378925d1c40SMatt Helsley 	struct mm_struct *mm;
1379925d1c40SMatt Helsley 	struct file *exe_file;
1380925d1c40SMatt Helsley 
13817773fbc5SCyrill Gorcunov 	task = get_proc_task(dentry->d_inode);
1382925d1c40SMatt Helsley 	if (!task)
1383925d1c40SMatt Helsley 		return -ENOENT;
1384925d1c40SMatt Helsley 	mm = get_task_mm(task);
1385925d1c40SMatt Helsley 	put_task_struct(task);
1386925d1c40SMatt Helsley 	if (!mm)
1387925d1c40SMatt Helsley 		return -ENOENT;
1388925d1c40SMatt Helsley 	exe_file = get_mm_exe_file(mm);
1389925d1c40SMatt Helsley 	mmput(mm);
1390925d1c40SMatt Helsley 	if (exe_file) {
1391925d1c40SMatt Helsley 		*exe_path = exe_file->f_path;
1392925d1c40SMatt Helsley 		path_get(&exe_file->f_path);
1393925d1c40SMatt Helsley 		fput(exe_file);
1394925d1c40SMatt Helsley 		return 0;
1395925d1c40SMatt Helsley 	} else
1396925d1c40SMatt Helsley 		return -ENOENT;
1397925d1c40SMatt Helsley }
1398925d1c40SMatt Helsley 
1399008b150aSAl Viro static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
14001da177e4SLinus Torvalds {
14011da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
1402408ef013SChristoph Hellwig 	struct path path;
14031da177e4SLinus Torvalds 	int error = -EACCES;
14041da177e4SLinus Torvalds 
1405778c1144SEric W. Biederman 	/* Are we allowed to snoop on the tasks file descriptors? */
1406778c1144SEric W. Biederman 	if (!proc_fd_access_allowed(inode))
14071da177e4SLinus Torvalds 		goto out;
14081da177e4SLinus Torvalds 
1409408ef013SChristoph Hellwig 	error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1410408ef013SChristoph Hellwig 	if (error)
1411408ef013SChristoph Hellwig 		goto out;
1412408ef013SChristoph Hellwig 
1413b5fb63c1SChristoph Hellwig 	nd_jump_link(nd, &path);
1414408ef013SChristoph Hellwig 	return NULL;
14151da177e4SLinus Torvalds out:
1416008b150aSAl Viro 	return ERR_PTR(error);
14171da177e4SLinus Torvalds }
14181da177e4SLinus Torvalds 
14193dcd25f3SJan Blunck static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
14201da177e4SLinus Torvalds {
1421e12ba74dSMel Gorman 	char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
14223dcd25f3SJan Blunck 	char *pathname;
14231da177e4SLinus Torvalds 	int len;
14241da177e4SLinus Torvalds 
14251da177e4SLinus Torvalds 	if (!tmp)
14261da177e4SLinus Torvalds 		return -ENOMEM;
14271da177e4SLinus Torvalds 
14287b2a69baSEric W. Biederman 	pathname = d_path(path, tmp, PAGE_SIZE);
14293dcd25f3SJan Blunck 	len = PTR_ERR(pathname);
14303dcd25f3SJan Blunck 	if (IS_ERR(pathname))
14311da177e4SLinus Torvalds 		goto out;
14323dcd25f3SJan Blunck 	len = tmp + PAGE_SIZE - 1 - pathname;
14331da177e4SLinus Torvalds 
14341da177e4SLinus Torvalds 	if (len > buflen)
14351da177e4SLinus Torvalds 		len = buflen;
14363dcd25f3SJan Blunck 	if (copy_to_user(buffer, pathname, len))
14371da177e4SLinus Torvalds 		len = -EFAULT;
14381da177e4SLinus Torvalds  out:
14391da177e4SLinus Torvalds 	free_page((unsigned long)tmp);
14401da177e4SLinus Torvalds 	return len;
14411da177e4SLinus Torvalds }
14421da177e4SLinus Torvalds 
14431da177e4SLinus Torvalds static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
14441da177e4SLinus Torvalds {
14451da177e4SLinus Torvalds 	int error = -EACCES;
14461da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
14473dcd25f3SJan Blunck 	struct path path;
14481da177e4SLinus Torvalds 
1449778c1144SEric W. Biederman 	/* Are we allowed to snoop on the tasks file descriptors? */
1450778c1144SEric W. Biederman 	if (!proc_fd_access_allowed(inode))
14511da177e4SLinus Torvalds 		goto out;
14521da177e4SLinus Torvalds 
14537773fbc5SCyrill Gorcunov 	error = PROC_I(inode)->op.proc_get_link(dentry, &path);
14541da177e4SLinus Torvalds 	if (error)
14551da177e4SLinus Torvalds 		goto out;
14561da177e4SLinus Torvalds 
14573dcd25f3SJan Blunck 	error = do_proc_readlink(&path, buffer, buflen);
14583dcd25f3SJan Blunck 	path_put(&path);
14591da177e4SLinus Torvalds out:
14601da177e4SLinus Torvalds 	return error;
14611da177e4SLinus Torvalds }
14621da177e4SLinus Torvalds 
1463faf60af1SCyrill Gorcunov const struct inode_operations proc_pid_link_inode_operations = {
14641da177e4SLinus Torvalds 	.readlink	= proc_pid_readlink,
14656d76fa58SLinus Torvalds 	.follow_link	= proc_pid_follow_link,
14666d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
14671da177e4SLinus Torvalds };
14681da177e4SLinus Torvalds 
146928a6d671SEric W. Biederman 
147028a6d671SEric W. Biederman /* building an inode */
147128a6d671SEric W. Biederman 
14726b4e306aSEric W. Biederman struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
147328a6d671SEric W. Biederman {
147428a6d671SEric W. Biederman 	struct inode * inode;
147528a6d671SEric W. Biederman 	struct proc_inode *ei;
1476c69e8d9cSDavid Howells 	const struct cred *cred;
147728a6d671SEric W. Biederman 
147828a6d671SEric W. Biederman 	/* We need a new inode */
147928a6d671SEric W. Biederman 
148028a6d671SEric W. Biederman 	inode = new_inode(sb);
148128a6d671SEric W. Biederman 	if (!inode)
148228a6d671SEric W. Biederman 		goto out;
148328a6d671SEric W. Biederman 
148428a6d671SEric W. Biederman 	/* Common stuff */
148528a6d671SEric W. Biederman 	ei = PROC_I(inode);
148685fe4025SChristoph Hellwig 	inode->i_ino = get_next_ino();
148728a6d671SEric W. Biederman 	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
148828a6d671SEric W. Biederman 	inode->i_op = &proc_def_inode_operations;
148928a6d671SEric W. Biederman 
149028a6d671SEric W. Biederman 	/*
149128a6d671SEric W. Biederman 	 * grab the reference to task.
149228a6d671SEric W. Biederman 	 */
14931a657f78SOleg Nesterov 	ei->pid = get_task_pid(task, PIDTYPE_PID);
149428a6d671SEric W. Biederman 	if (!ei->pid)
149528a6d671SEric W. Biederman 		goto out_unlock;
149628a6d671SEric W. Biederman 
149728a6d671SEric W. Biederman 	if (task_dumpable(task)) {
1498c69e8d9cSDavid Howells 		rcu_read_lock();
1499c69e8d9cSDavid Howells 		cred = __task_cred(task);
1500c69e8d9cSDavid Howells 		inode->i_uid = cred->euid;
1501c69e8d9cSDavid Howells 		inode->i_gid = cred->egid;
1502c69e8d9cSDavid Howells 		rcu_read_unlock();
150328a6d671SEric W. Biederman 	}
150428a6d671SEric W. Biederman 	security_task_to_inode(task, inode);
150528a6d671SEric W. Biederman 
150628a6d671SEric W. Biederman out:
150728a6d671SEric W. Biederman 	return inode;
150828a6d671SEric W. Biederman 
150928a6d671SEric W. Biederman out_unlock:
151028a6d671SEric W. Biederman 	iput(inode);
151128a6d671SEric W. Biederman 	return NULL;
151228a6d671SEric W. Biederman }
151328a6d671SEric W. Biederman 
15146b4e306aSEric W. Biederman int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
151528a6d671SEric W. Biederman {
151628a6d671SEric W. Biederman 	struct inode *inode = dentry->d_inode;
151728a6d671SEric W. Biederman 	struct task_struct *task;
1518c69e8d9cSDavid Howells 	const struct cred *cred;
15190499680aSVasiliy Kulikov 	struct pid_namespace *pid = dentry->d_sb->s_fs_info;
1520c69e8d9cSDavid Howells 
152128a6d671SEric W. Biederman 	generic_fillattr(inode, stat);
152228a6d671SEric W. Biederman 
152328a6d671SEric W. Biederman 	rcu_read_lock();
1524dcb0f222SEric W. Biederman 	stat->uid = GLOBAL_ROOT_UID;
1525dcb0f222SEric W. Biederman 	stat->gid = GLOBAL_ROOT_GID;
152628a6d671SEric W. Biederman 	task = pid_task(proc_pid(inode), PIDTYPE_PID);
152728a6d671SEric W. Biederman 	if (task) {
15280499680aSVasiliy Kulikov 		if (!has_pid_permissions(pid, task, 2)) {
15290499680aSVasiliy Kulikov 			rcu_read_unlock();
15300499680aSVasiliy Kulikov 			/*
15310499680aSVasiliy Kulikov 			 * This doesn't prevent learning whether PID exists,
15320499680aSVasiliy Kulikov 			 * it only makes getattr() consistent with readdir().
15330499680aSVasiliy Kulikov 			 */
15340499680aSVasiliy Kulikov 			return -ENOENT;
15350499680aSVasiliy Kulikov 		}
153628a6d671SEric W. Biederman 		if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
153728a6d671SEric W. Biederman 		    task_dumpable(task)) {
1538c69e8d9cSDavid Howells 			cred = __task_cred(task);
1539c69e8d9cSDavid Howells 			stat->uid = cred->euid;
1540c69e8d9cSDavid Howells 			stat->gid = cred->egid;
154128a6d671SEric W. Biederman 		}
154228a6d671SEric W. Biederman 	}
154328a6d671SEric W. Biederman 	rcu_read_unlock();
154428a6d671SEric W. Biederman 	return 0;
154528a6d671SEric W. Biederman }
154628a6d671SEric W. Biederman 
154728a6d671SEric W. Biederman /* dentry stuff */
154828a6d671SEric W. Biederman 
154928a6d671SEric W. Biederman /*
155028a6d671SEric W. Biederman  *	Exceptional case: normally we are not allowed to unhash a busy
155128a6d671SEric W. Biederman  * directory. In this case, however, we can do it - no aliasing problems
155228a6d671SEric W. Biederman  * due to the way we treat inodes.
155328a6d671SEric W. Biederman  *
155428a6d671SEric W. Biederman  * Rewrite the inode's ownerships here because the owning task may have
155528a6d671SEric W. Biederman  * performed a setuid(), etc.
155628a6d671SEric W. Biederman  *
155728a6d671SEric W. Biederman  * Before the /proc/pid/status file was created the only way to read
155828a6d671SEric W. Biederman  * the effective uid of a /process was to stat /proc/pid.  Reading
155928a6d671SEric W. Biederman  * /proc/pid/status is slow enough that procps and other packages
156028a6d671SEric W. Biederman  * kept stating /proc/pid.  To keep the rules in /proc simple I have
156128a6d671SEric W. Biederman  * made this apply to all per process world readable and executable
156228a6d671SEric W. Biederman  * directories.
156328a6d671SEric W. Biederman  */
15640b728e19SAl Viro int pid_revalidate(struct dentry *dentry, unsigned int flags)
156528a6d671SEric W. Biederman {
156634286d66SNick Piggin 	struct inode *inode;
156734286d66SNick Piggin 	struct task_struct *task;
1568c69e8d9cSDavid Howells 	const struct cred *cred;
1569c69e8d9cSDavid Howells 
15700b728e19SAl Viro 	if (flags & LOOKUP_RCU)
157134286d66SNick Piggin 		return -ECHILD;
157234286d66SNick Piggin 
157334286d66SNick Piggin 	inode = dentry->d_inode;
157434286d66SNick Piggin 	task = get_proc_task(inode);
157534286d66SNick Piggin 
157628a6d671SEric W. Biederman 	if (task) {
157728a6d671SEric W. Biederman 		if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
157828a6d671SEric W. Biederman 		    task_dumpable(task)) {
1579c69e8d9cSDavid Howells 			rcu_read_lock();
1580c69e8d9cSDavid Howells 			cred = __task_cred(task);
1581c69e8d9cSDavid Howells 			inode->i_uid = cred->euid;
1582c69e8d9cSDavid Howells 			inode->i_gid = cred->egid;
1583c69e8d9cSDavid Howells 			rcu_read_unlock();
158428a6d671SEric W. Biederman 		} else {
1585dcb0f222SEric W. Biederman 			inode->i_uid = GLOBAL_ROOT_UID;
1586dcb0f222SEric W. Biederman 			inode->i_gid = GLOBAL_ROOT_GID;
158728a6d671SEric W. Biederman 		}
158828a6d671SEric W. Biederman 		inode->i_mode &= ~(S_ISUID | S_ISGID);
158928a6d671SEric W. Biederman 		security_task_to_inode(task, inode);
159028a6d671SEric W. Biederman 		put_task_struct(task);
159128a6d671SEric W. Biederman 		return 1;
159228a6d671SEric W. Biederman 	}
159328a6d671SEric W. Biederman 	return 0;
159428a6d671SEric W. Biederman }
159528a6d671SEric W. Biederman 
1596d855a4b7SOleg Nesterov static inline bool proc_inode_is_dead(struct inode *inode)
1597d855a4b7SOleg Nesterov {
1598d855a4b7SOleg Nesterov 	return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1599d855a4b7SOleg Nesterov }
1600d855a4b7SOleg Nesterov 
16011dd704b6SDavid Howells int pid_delete_dentry(const struct dentry *dentry)
16021dd704b6SDavid Howells {
16031dd704b6SDavid Howells 	/* Is the task we represent dead?
16041dd704b6SDavid Howells 	 * If so, then don't put the dentry on the lru list,
16051dd704b6SDavid Howells 	 * kill it immediately.
16061dd704b6SDavid Howells 	 */
1607d855a4b7SOleg Nesterov 	return proc_inode_is_dead(dentry->d_inode);
16081dd704b6SDavid Howells }
16091dd704b6SDavid Howells 
16106b4e306aSEric W. Biederman const struct dentry_operations pid_dentry_operations =
161128a6d671SEric W. Biederman {
161228a6d671SEric W. Biederman 	.d_revalidate	= pid_revalidate,
161328a6d671SEric W. Biederman 	.d_delete	= pid_delete_dentry,
161428a6d671SEric W. Biederman };
161528a6d671SEric W. Biederman 
161628a6d671SEric W. Biederman /* Lookups */
161728a6d671SEric W. Biederman 
16181c0d04c9SEric W. Biederman /*
16191c0d04c9SEric W. Biederman  * Fill a directory entry.
16201c0d04c9SEric W. Biederman  *
16211c0d04c9SEric W. Biederman  * If possible create the dcache entry and derive our inode number and
16221c0d04c9SEric W. Biederman  * file type from dcache entry.
16231c0d04c9SEric W. Biederman  *
16241c0d04c9SEric W. Biederman  * Since all of the proc inode numbers are dynamically generated, the inode
16251c0d04c9SEric W. Biederman  * numbers do not exist until the inode is cache.  This means creating the
16261c0d04c9SEric W. Biederman  * the dcache entry in readdir is necessary to keep the inode numbers
16271c0d04c9SEric W. Biederman  * reported by readdir in sync with the inode numbers reported
16281c0d04c9SEric W. Biederman  * by stat.
16291c0d04c9SEric W. Biederman  */
1630f0c3b509SAl Viro bool proc_fill_cache(struct file *file, struct dir_context *ctx,
16316b4e306aSEric W. Biederman 	const char *name, int len,
1632c5141e6dSEric Dumazet 	instantiate_t instantiate, struct task_struct *task, const void *ptr)
163361a28784SEric W. Biederman {
1634f0c3b509SAl Viro 	struct dentry *child, *dir = file->f_path.dentry;
16351df98b8bSAl Viro 	struct qstr qname = QSTR_INIT(name, len);
163661a28784SEric W. Biederman 	struct inode *inode;
16371df98b8bSAl Viro 	unsigned type;
16381df98b8bSAl Viro 	ino_t ino;
163961a28784SEric W. Biederman 
16401df98b8bSAl Viro 	child = d_hash_and_lookup(dir, &qname);
164161a28784SEric W. Biederman 	if (!child) {
16421df98b8bSAl Viro 		child = d_alloc(dir, &qname);
16431df98b8bSAl Viro 		if (!child)
164461a28784SEric W. Biederman 			goto end_instantiate;
16451df98b8bSAl Viro 		if (instantiate(dir->d_inode, child, task, ptr) < 0) {
16461df98b8bSAl Viro 			dput(child);
16471df98b8bSAl Viro 			goto end_instantiate;
16481df98b8bSAl Viro 		}
16491df98b8bSAl Viro 	}
165061a28784SEric W. Biederman 	inode = child->d_inode;
165161a28784SEric W. Biederman 	ino = inode->i_ino;
165261a28784SEric W. Biederman 	type = inode->i_mode >> 12;
165361a28784SEric W. Biederman 	dput(child);
1654f0c3b509SAl Viro 	return dir_emit(ctx, name, len, ino, type);
16551df98b8bSAl Viro 
16561df98b8bSAl Viro end_instantiate:
16571df98b8bSAl Viro 	return dir_emit(ctx, name, len, 1, DT_UNKNOWN);
165861a28784SEric W. Biederman }
165961a28784SEric W. Biederman 
1660640708a2SPavel Emelyanov #ifdef CONFIG_CHECKPOINT_RESTORE
1661640708a2SPavel Emelyanov 
1662640708a2SPavel Emelyanov /*
1663640708a2SPavel Emelyanov  * dname_to_vma_addr - maps a dentry name into two unsigned longs
1664640708a2SPavel Emelyanov  * which represent vma start and end addresses.
1665640708a2SPavel Emelyanov  */
1666640708a2SPavel Emelyanov static int dname_to_vma_addr(struct dentry *dentry,
1667640708a2SPavel Emelyanov 			     unsigned long *start, unsigned long *end)
1668640708a2SPavel Emelyanov {
1669640708a2SPavel Emelyanov 	if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2)
1670640708a2SPavel Emelyanov 		return -EINVAL;
1671640708a2SPavel Emelyanov 
1672640708a2SPavel Emelyanov 	return 0;
1673640708a2SPavel Emelyanov }
1674640708a2SPavel Emelyanov 
16750b728e19SAl Viro static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
1676640708a2SPavel Emelyanov {
1677640708a2SPavel Emelyanov 	unsigned long vm_start, vm_end;
1678640708a2SPavel Emelyanov 	bool exact_vma_exists = false;
1679640708a2SPavel Emelyanov 	struct mm_struct *mm = NULL;
1680640708a2SPavel Emelyanov 	struct task_struct *task;
1681640708a2SPavel Emelyanov 	const struct cred *cred;
1682640708a2SPavel Emelyanov 	struct inode *inode;
1683640708a2SPavel Emelyanov 	int status = 0;
1684640708a2SPavel Emelyanov 
16850b728e19SAl Viro 	if (flags & LOOKUP_RCU)
1686640708a2SPavel Emelyanov 		return -ECHILD;
1687640708a2SPavel Emelyanov 
1688640708a2SPavel Emelyanov 	if (!capable(CAP_SYS_ADMIN)) {
168941735818SZhao Hongjiang 		status = -EPERM;
1690640708a2SPavel Emelyanov 		goto out_notask;
1691640708a2SPavel Emelyanov 	}
1692640708a2SPavel Emelyanov 
1693640708a2SPavel Emelyanov 	inode = dentry->d_inode;
1694640708a2SPavel Emelyanov 	task = get_proc_task(inode);
1695640708a2SPavel Emelyanov 	if (!task)
1696640708a2SPavel Emelyanov 		goto out_notask;
1697640708a2SPavel Emelyanov 
16982344bec7SCong Wang 	mm = mm_access(task, PTRACE_MODE_READ);
16992344bec7SCong Wang 	if (IS_ERR_OR_NULL(mm))
1700640708a2SPavel Emelyanov 		goto out;
1701640708a2SPavel Emelyanov 
1702640708a2SPavel Emelyanov 	if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1703640708a2SPavel Emelyanov 		down_read(&mm->mmap_sem);
1704640708a2SPavel Emelyanov 		exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1705640708a2SPavel Emelyanov 		up_read(&mm->mmap_sem);
1706640708a2SPavel Emelyanov 	}
1707640708a2SPavel Emelyanov 
1708640708a2SPavel Emelyanov 	mmput(mm);
1709640708a2SPavel Emelyanov 
1710640708a2SPavel Emelyanov 	if (exact_vma_exists) {
1711640708a2SPavel Emelyanov 		if (task_dumpable(task)) {
1712640708a2SPavel Emelyanov 			rcu_read_lock();
1713640708a2SPavel Emelyanov 			cred = __task_cred(task);
1714640708a2SPavel Emelyanov 			inode->i_uid = cred->euid;
1715640708a2SPavel Emelyanov 			inode->i_gid = cred->egid;
1716640708a2SPavel Emelyanov 			rcu_read_unlock();
1717640708a2SPavel Emelyanov 		} else {
1718dcb0f222SEric W. Biederman 			inode->i_uid = GLOBAL_ROOT_UID;
1719dcb0f222SEric W. Biederman 			inode->i_gid = GLOBAL_ROOT_GID;
1720640708a2SPavel Emelyanov 		}
1721640708a2SPavel Emelyanov 		security_task_to_inode(task, inode);
1722640708a2SPavel Emelyanov 		status = 1;
1723640708a2SPavel Emelyanov 	}
1724640708a2SPavel Emelyanov 
1725640708a2SPavel Emelyanov out:
1726640708a2SPavel Emelyanov 	put_task_struct(task);
1727640708a2SPavel Emelyanov 
1728640708a2SPavel Emelyanov out_notask:
1729640708a2SPavel Emelyanov 	return status;
1730640708a2SPavel Emelyanov }
1731640708a2SPavel Emelyanov 
1732640708a2SPavel Emelyanov static const struct dentry_operations tid_map_files_dentry_operations = {
1733640708a2SPavel Emelyanov 	.d_revalidate	= map_files_d_revalidate,
1734640708a2SPavel Emelyanov 	.d_delete	= pid_delete_dentry,
1735640708a2SPavel Emelyanov };
1736640708a2SPavel Emelyanov 
1737640708a2SPavel Emelyanov static int proc_map_files_get_link(struct dentry *dentry, struct path *path)
1738640708a2SPavel Emelyanov {
1739640708a2SPavel Emelyanov 	unsigned long vm_start, vm_end;
1740640708a2SPavel Emelyanov 	struct vm_area_struct *vma;
1741640708a2SPavel Emelyanov 	struct task_struct *task;
1742640708a2SPavel Emelyanov 	struct mm_struct *mm;
1743640708a2SPavel Emelyanov 	int rc;
1744640708a2SPavel Emelyanov 
1745640708a2SPavel Emelyanov 	rc = -ENOENT;
1746640708a2SPavel Emelyanov 	task = get_proc_task(dentry->d_inode);
1747640708a2SPavel Emelyanov 	if (!task)
1748640708a2SPavel Emelyanov 		goto out;
1749640708a2SPavel Emelyanov 
1750640708a2SPavel Emelyanov 	mm = get_task_mm(task);
1751640708a2SPavel Emelyanov 	put_task_struct(task);
1752640708a2SPavel Emelyanov 	if (!mm)
1753640708a2SPavel Emelyanov 		goto out;
1754640708a2SPavel Emelyanov 
1755640708a2SPavel Emelyanov 	rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
1756640708a2SPavel Emelyanov 	if (rc)
1757640708a2SPavel Emelyanov 		goto out_mmput;
1758640708a2SPavel Emelyanov 
175970335abbSArtem Fetishev 	rc = -ENOENT;
1760640708a2SPavel Emelyanov 	down_read(&mm->mmap_sem);
1761640708a2SPavel Emelyanov 	vma = find_exact_vma(mm, vm_start, vm_end);
1762640708a2SPavel Emelyanov 	if (vma && vma->vm_file) {
1763640708a2SPavel Emelyanov 		*path = vma->vm_file->f_path;
1764640708a2SPavel Emelyanov 		path_get(path);
1765640708a2SPavel Emelyanov 		rc = 0;
1766640708a2SPavel Emelyanov 	}
1767640708a2SPavel Emelyanov 	up_read(&mm->mmap_sem);
1768640708a2SPavel Emelyanov 
1769640708a2SPavel Emelyanov out_mmput:
1770640708a2SPavel Emelyanov 	mmput(mm);
1771640708a2SPavel Emelyanov out:
1772640708a2SPavel Emelyanov 	return rc;
1773640708a2SPavel Emelyanov }
1774640708a2SPavel Emelyanov 
1775640708a2SPavel Emelyanov struct map_files_info {
17767b540d06SAl Viro 	fmode_t		mode;
1777640708a2SPavel Emelyanov 	unsigned long	len;
1778640708a2SPavel Emelyanov 	unsigned char	name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
1779640708a2SPavel Emelyanov };
1780640708a2SPavel Emelyanov 
1781c52a47acSAl Viro static int
1782640708a2SPavel Emelyanov proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
1783640708a2SPavel Emelyanov 			   struct task_struct *task, const void *ptr)
1784640708a2SPavel Emelyanov {
17857b540d06SAl Viro 	fmode_t mode = (fmode_t)(unsigned long)ptr;
1786640708a2SPavel Emelyanov 	struct proc_inode *ei;
1787640708a2SPavel Emelyanov 	struct inode *inode;
1788640708a2SPavel Emelyanov 
1789640708a2SPavel Emelyanov 	inode = proc_pid_make_inode(dir->i_sb, task);
1790640708a2SPavel Emelyanov 	if (!inode)
1791c52a47acSAl Viro 		return -ENOENT;
1792640708a2SPavel Emelyanov 
1793640708a2SPavel Emelyanov 	ei = PROC_I(inode);
1794640708a2SPavel Emelyanov 	ei->op.proc_get_link = proc_map_files_get_link;
1795640708a2SPavel Emelyanov 
1796640708a2SPavel Emelyanov 	inode->i_op = &proc_pid_link_inode_operations;
1797640708a2SPavel Emelyanov 	inode->i_size = 64;
1798640708a2SPavel Emelyanov 	inode->i_mode = S_IFLNK;
1799640708a2SPavel Emelyanov 
18007b540d06SAl Viro 	if (mode & FMODE_READ)
1801640708a2SPavel Emelyanov 		inode->i_mode |= S_IRUSR;
18027b540d06SAl Viro 	if (mode & FMODE_WRITE)
1803640708a2SPavel Emelyanov 		inode->i_mode |= S_IWUSR;
1804640708a2SPavel Emelyanov 
1805640708a2SPavel Emelyanov 	d_set_d_op(dentry, &tid_map_files_dentry_operations);
1806640708a2SPavel Emelyanov 	d_add(dentry, inode);
1807640708a2SPavel Emelyanov 
1808c52a47acSAl Viro 	return 0;
1809640708a2SPavel Emelyanov }
1810640708a2SPavel Emelyanov 
1811640708a2SPavel Emelyanov static struct dentry *proc_map_files_lookup(struct inode *dir,
181200cd8dd3SAl Viro 		struct dentry *dentry, unsigned int flags)
1813640708a2SPavel Emelyanov {
1814640708a2SPavel Emelyanov 	unsigned long vm_start, vm_end;
1815640708a2SPavel Emelyanov 	struct vm_area_struct *vma;
1816640708a2SPavel Emelyanov 	struct task_struct *task;
1817c52a47acSAl Viro 	int result;
1818640708a2SPavel Emelyanov 	struct mm_struct *mm;
1819640708a2SPavel Emelyanov 
1820c52a47acSAl Viro 	result = -EPERM;
1821640708a2SPavel Emelyanov 	if (!capable(CAP_SYS_ADMIN))
1822640708a2SPavel Emelyanov 		goto out;
1823640708a2SPavel Emelyanov 
1824c52a47acSAl Viro 	result = -ENOENT;
1825640708a2SPavel Emelyanov 	task = get_proc_task(dir);
1826640708a2SPavel Emelyanov 	if (!task)
1827640708a2SPavel Emelyanov 		goto out;
1828640708a2SPavel Emelyanov 
1829c52a47acSAl Viro 	result = -EACCES;
1830eb94cd96SCyrill Gorcunov 	if (!ptrace_may_access(task, PTRACE_MODE_READ))
1831640708a2SPavel Emelyanov 		goto out_put_task;
1832640708a2SPavel Emelyanov 
1833c52a47acSAl Viro 	result = -ENOENT;
1834640708a2SPavel Emelyanov 	if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
1835eb94cd96SCyrill Gorcunov 		goto out_put_task;
1836640708a2SPavel Emelyanov 
1837640708a2SPavel Emelyanov 	mm = get_task_mm(task);
1838640708a2SPavel Emelyanov 	if (!mm)
1839eb94cd96SCyrill Gorcunov 		goto out_put_task;
1840640708a2SPavel Emelyanov 
1841640708a2SPavel Emelyanov 	down_read(&mm->mmap_sem);
1842640708a2SPavel Emelyanov 	vma = find_exact_vma(mm, vm_start, vm_end);
1843640708a2SPavel Emelyanov 	if (!vma)
1844640708a2SPavel Emelyanov 		goto out_no_vma;
1845640708a2SPavel Emelyanov 
184605f56484SStanislav Kinsbursky 	if (vma->vm_file)
18477b540d06SAl Viro 		result = proc_map_files_instantiate(dir, dentry, task,
18487b540d06SAl Viro 				(void *)(unsigned long)vma->vm_file->f_mode);
1849640708a2SPavel Emelyanov 
1850640708a2SPavel Emelyanov out_no_vma:
1851640708a2SPavel Emelyanov 	up_read(&mm->mmap_sem);
1852640708a2SPavel Emelyanov 	mmput(mm);
1853640708a2SPavel Emelyanov out_put_task:
1854640708a2SPavel Emelyanov 	put_task_struct(task);
1855640708a2SPavel Emelyanov out:
1856c52a47acSAl Viro 	return ERR_PTR(result);
1857640708a2SPavel Emelyanov }
1858640708a2SPavel Emelyanov 
1859640708a2SPavel Emelyanov static const struct inode_operations proc_map_files_inode_operations = {
1860640708a2SPavel Emelyanov 	.lookup		= proc_map_files_lookup,
1861640708a2SPavel Emelyanov 	.permission	= proc_fd_permission,
1862640708a2SPavel Emelyanov 	.setattr	= proc_setattr,
1863640708a2SPavel Emelyanov };
1864640708a2SPavel Emelyanov 
1865640708a2SPavel Emelyanov static int
1866f0c3b509SAl Viro proc_map_files_readdir(struct file *file, struct dir_context *ctx)
1867640708a2SPavel Emelyanov {
1868640708a2SPavel Emelyanov 	struct vm_area_struct *vma;
1869640708a2SPavel Emelyanov 	struct task_struct *task;
1870640708a2SPavel Emelyanov 	struct mm_struct *mm;
1871f0c3b509SAl Viro 	unsigned long nr_files, pos, i;
1872f0c3b509SAl Viro 	struct flex_array *fa = NULL;
1873f0c3b509SAl Viro 	struct map_files_info info;
1874f0c3b509SAl Viro 	struct map_files_info *p;
1875640708a2SPavel Emelyanov 	int ret;
1876640708a2SPavel Emelyanov 
187741735818SZhao Hongjiang 	ret = -EPERM;
1878640708a2SPavel Emelyanov 	if (!capable(CAP_SYS_ADMIN))
1879640708a2SPavel Emelyanov 		goto out;
1880640708a2SPavel Emelyanov 
1881640708a2SPavel Emelyanov 	ret = -ENOENT;
1882f0c3b509SAl Viro 	task = get_proc_task(file_inode(file));
1883640708a2SPavel Emelyanov 	if (!task)
1884640708a2SPavel Emelyanov 		goto out;
1885640708a2SPavel Emelyanov 
1886640708a2SPavel Emelyanov 	ret = -EACCES;
1887eb94cd96SCyrill Gorcunov 	if (!ptrace_may_access(task, PTRACE_MODE_READ))
1888640708a2SPavel Emelyanov 		goto out_put_task;
1889640708a2SPavel Emelyanov 
1890640708a2SPavel Emelyanov 	ret = 0;
1891f0c3b509SAl Viro 	if (!dir_emit_dots(file, ctx))
1892eb94cd96SCyrill Gorcunov 		goto out_put_task;
1893640708a2SPavel Emelyanov 
1894640708a2SPavel Emelyanov 	mm = get_task_mm(task);
1895640708a2SPavel Emelyanov 	if (!mm)
1896eb94cd96SCyrill Gorcunov 		goto out_put_task;
1897640708a2SPavel Emelyanov 	down_read(&mm->mmap_sem);
1898640708a2SPavel Emelyanov 
1899640708a2SPavel Emelyanov 	nr_files = 0;
1900640708a2SPavel Emelyanov 
1901640708a2SPavel Emelyanov 	/*
1902640708a2SPavel Emelyanov 	 * We need two passes here:
1903640708a2SPavel Emelyanov 	 *
1904640708a2SPavel Emelyanov 	 *  1) Collect vmas of mapped files with mmap_sem taken
1905640708a2SPavel Emelyanov 	 *  2) Release mmap_sem and instantiate entries
1906640708a2SPavel Emelyanov 	 *
1907640708a2SPavel Emelyanov 	 * otherwise we get lockdep complained, since filldir()
1908640708a2SPavel Emelyanov 	 * routine might require mmap_sem taken in might_fault().
1909640708a2SPavel Emelyanov 	 */
1910640708a2SPavel Emelyanov 
1911640708a2SPavel Emelyanov 	for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
1912f0c3b509SAl Viro 		if (vma->vm_file && ++pos > ctx->pos)
1913640708a2SPavel Emelyanov 			nr_files++;
1914640708a2SPavel Emelyanov 	}
1915640708a2SPavel Emelyanov 
1916640708a2SPavel Emelyanov 	if (nr_files) {
1917640708a2SPavel Emelyanov 		fa = flex_array_alloc(sizeof(info), nr_files,
1918640708a2SPavel Emelyanov 					GFP_KERNEL);
1919640708a2SPavel Emelyanov 		if (!fa || flex_array_prealloc(fa, 0, nr_files,
1920640708a2SPavel Emelyanov 						GFP_KERNEL)) {
1921640708a2SPavel Emelyanov 			ret = -ENOMEM;
1922640708a2SPavel Emelyanov 			if (fa)
1923640708a2SPavel Emelyanov 				flex_array_free(fa);
1924640708a2SPavel Emelyanov 			up_read(&mm->mmap_sem);
1925640708a2SPavel Emelyanov 			mmput(mm);
1926eb94cd96SCyrill Gorcunov 			goto out_put_task;
1927640708a2SPavel Emelyanov 		}
1928640708a2SPavel Emelyanov 		for (i = 0, vma = mm->mmap, pos = 2; vma;
1929640708a2SPavel Emelyanov 				vma = vma->vm_next) {
1930640708a2SPavel Emelyanov 			if (!vma->vm_file)
1931640708a2SPavel Emelyanov 				continue;
1932f0c3b509SAl Viro 			if (++pos <= ctx->pos)
1933640708a2SPavel Emelyanov 				continue;
1934640708a2SPavel Emelyanov 
19357b540d06SAl Viro 			info.mode = vma->vm_file->f_mode;
1936640708a2SPavel Emelyanov 			info.len = snprintf(info.name,
1937640708a2SPavel Emelyanov 					sizeof(info.name), "%lx-%lx",
1938640708a2SPavel Emelyanov 					vma->vm_start, vma->vm_end);
1939640708a2SPavel Emelyanov 			if (flex_array_put(fa, i++, &info, GFP_KERNEL))
1940640708a2SPavel Emelyanov 				BUG();
1941640708a2SPavel Emelyanov 		}
1942640708a2SPavel Emelyanov 	}
1943640708a2SPavel Emelyanov 	up_read(&mm->mmap_sem);
1944640708a2SPavel Emelyanov 
1945640708a2SPavel Emelyanov 	for (i = 0; i < nr_files; i++) {
1946640708a2SPavel Emelyanov 		p = flex_array_get(fa, i);
1947f0c3b509SAl Viro 		if (!proc_fill_cache(file, ctx,
1948640708a2SPavel Emelyanov 				      p->name, p->len,
1949640708a2SPavel Emelyanov 				      proc_map_files_instantiate,
19507b540d06SAl Viro 				      task,
1951f0c3b509SAl Viro 				      (void *)(unsigned long)p->mode))
1952640708a2SPavel Emelyanov 			break;
1953f0c3b509SAl Viro 		ctx->pos++;
1954640708a2SPavel Emelyanov 	}
1955640708a2SPavel Emelyanov 	if (fa)
1956640708a2SPavel Emelyanov 		flex_array_free(fa);
1957640708a2SPavel Emelyanov 	mmput(mm);
1958640708a2SPavel Emelyanov 
1959640708a2SPavel Emelyanov out_put_task:
1960640708a2SPavel Emelyanov 	put_task_struct(task);
1961640708a2SPavel Emelyanov out:
1962640708a2SPavel Emelyanov 	return ret;
1963640708a2SPavel Emelyanov }
1964640708a2SPavel Emelyanov 
1965640708a2SPavel Emelyanov static const struct file_operations proc_map_files_operations = {
1966640708a2SPavel Emelyanov 	.read		= generic_read_dir,
1967f0c3b509SAl Viro 	.iterate	= proc_map_files_readdir,
1968640708a2SPavel Emelyanov 	.llseek		= default_llseek,
1969640708a2SPavel Emelyanov };
1970640708a2SPavel Emelyanov 
197148f6a7a5SPavel Emelyanov struct timers_private {
197248f6a7a5SPavel Emelyanov 	struct pid *pid;
197348f6a7a5SPavel Emelyanov 	struct task_struct *task;
197448f6a7a5SPavel Emelyanov 	struct sighand_struct *sighand;
197557b8015eSPavel Emelyanov 	struct pid_namespace *ns;
197648f6a7a5SPavel Emelyanov 	unsigned long flags;
197748f6a7a5SPavel Emelyanov };
197848f6a7a5SPavel Emelyanov 
197948f6a7a5SPavel Emelyanov static void *timers_start(struct seq_file *m, loff_t *pos)
198048f6a7a5SPavel Emelyanov {
198148f6a7a5SPavel Emelyanov 	struct timers_private *tp = m->private;
198248f6a7a5SPavel Emelyanov 
198348f6a7a5SPavel Emelyanov 	tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
198448f6a7a5SPavel Emelyanov 	if (!tp->task)
198548f6a7a5SPavel Emelyanov 		return ERR_PTR(-ESRCH);
198648f6a7a5SPavel Emelyanov 
198748f6a7a5SPavel Emelyanov 	tp->sighand = lock_task_sighand(tp->task, &tp->flags);
198848f6a7a5SPavel Emelyanov 	if (!tp->sighand)
198948f6a7a5SPavel Emelyanov 		return ERR_PTR(-ESRCH);
199048f6a7a5SPavel Emelyanov 
199148f6a7a5SPavel Emelyanov 	return seq_list_start(&tp->task->signal->posix_timers, *pos);
199248f6a7a5SPavel Emelyanov }
199348f6a7a5SPavel Emelyanov 
199448f6a7a5SPavel Emelyanov static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
199548f6a7a5SPavel Emelyanov {
199648f6a7a5SPavel Emelyanov 	struct timers_private *tp = m->private;
199748f6a7a5SPavel Emelyanov 	return seq_list_next(v, &tp->task->signal->posix_timers, pos);
199848f6a7a5SPavel Emelyanov }
199948f6a7a5SPavel Emelyanov 
200048f6a7a5SPavel Emelyanov static void timers_stop(struct seq_file *m, void *v)
200148f6a7a5SPavel Emelyanov {
200248f6a7a5SPavel Emelyanov 	struct timers_private *tp = m->private;
200348f6a7a5SPavel Emelyanov 
200448f6a7a5SPavel Emelyanov 	if (tp->sighand) {
200548f6a7a5SPavel Emelyanov 		unlock_task_sighand(tp->task, &tp->flags);
200648f6a7a5SPavel Emelyanov 		tp->sighand = NULL;
200748f6a7a5SPavel Emelyanov 	}
200848f6a7a5SPavel Emelyanov 
200948f6a7a5SPavel Emelyanov 	if (tp->task) {
201048f6a7a5SPavel Emelyanov 		put_task_struct(tp->task);
201148f6a7a5SPavel Emelyanov 		tp->task = NULL;
201248f6a7a5SPavel Emelyanov 	}
201348f6a7a5SPavel Emelyanov }
201448f6a7a5SPavel Emelyanov 
201548f6a7a5SPavel Emelyanov static int show_timer(struct seq_file *m, void *v)
201648f6a7a5SPavel Emelyanov {
201748f6a7a5SPavel Emelyanov 	struct k_itimer *timer;
201857b8015eSPavel Emelyanov 	struct timers_private *tp = m->private;
201957b8015eSPavel Emelyanov 	int notify;
2020cedbccabSAlexey Dobriyan 	static const char * const nstr[] = {
202157b8015eSPavel Emelyanov 		[SIGEV_SIGNAL] = "signal",
202257b8015eSPavel Emelyanov 		[SIGEV_NONE] = "none",
202357b8015eSPavel Emelyanov 		[SIGEV_THREAD] = "thread",
202457b8015eSPavel Emelyanov 	};
202548f6a7a5SPavel Emelyanov 
202648f6a7a5SPavel Emelyanov 	timer = list_entry((struct list_head *)v, struct k_itimer, list);
202757b8015eSPavel Emelyanov 	notify = timer->it_sigev_notify;
202857b8015eSPavel Emelyanov 
202948f6a7a5SPavel Emelyanov 	seq_printf(m, "ID: %d\n", timer->it_id);
203057b8015eSPavel Emelyanov 	seq_printf(m, "signal: %d/%p\n", timer->sigq->info.si_signo,
203157b8015eSPavel Emelyanov 			timer->sigq->info.si_value.sival_ptr);
203257b8015eSPavel Emelyanov 	seq_printf(m, "notify: %s/%s.%d\n",
203357b8015eSPavel Emelyanov 		nstr[notify & ~SIGEV_THREAD_ID],
203457b8015eSPavel Emelyanov 		(notify & SIGEV_THREAD_ID) ? "tid" : "pid",
203557b8015eSPavel Emelyanov 		pid_nr_ns(timer->it_pid, tp->ns));
203615ef0298SPavel Tikhomirov 	seq_printf(m, "ClockID: %d\n", timer->it_clock);
203748f6a7a5SPavel Emelyanov 
203848f6a7a5SPavel Emelyanov 	return 0;
203948f6a7a5SPavel Emelyanov }
204048f6a7a5SPavel Emelyanov 
204148f6a7a5SPavel Emelyanov static const struct seq_operations proc_timers_seq_ops = {
204248f6a7a5SPavel Emelyanov 	.start	= timers_start,
204348f6a7a5SPavel Emelyanov 	.next	= timers_next,
204448f6a7a5SPavel Emelyanov 	.stop	= timers_stop,
204548f6a7a5SPavel Emelyanov 	.show	= show_timer,
204648f6a7a5SPavel Emelyanov };
204748f6a7a5SPavel Emelyanov 
204848f6a7a5SPavel Emelyanov static int proc_timers_open(struct inode *inode, struct file *file)
204948f6a7a5SPavel Emelyanov {
205048f6a7a5SPavel Emelyanov 	struct timers_private *tp;
205148f6a7a5SPavel Emelyanov 
205248f6a7a5SPavel Emelyanov 	tp = __seq_open_private(file, &proc_timers_seq_ops,
205348f6a7a5SPavel Emelyanov 			sizeof(struct timers_private));
205448f6a7a5SPavel Emelyanov 	if (!tp)
205548f6a7a5SPavel Emelyanov 		return -ENOMEM;
205648f6a7a5SPavel Emelyanov 
205748f6a7a5SPavel Emelyanov 	tp->pid = proc_pid(inode);
205857b8015eSPavel Emelyanov 	tp->ns = inode->i_sb->s_fs_info;
205948f6a7a5SPavel Emelyanov 	return 0;
206048f6a7a5SPavel Emelyanov }
206148f6a7a5SPavel Emelyanov 
206248f6a7a5SPavel Emelyanov static const struct file_operations proc_timers_operations = {
206348f6a7a5SPavel Emelyanov 	.open		= proc_timers_open,
206448f6a7a5SPavel Emelyanov 	.read		= seq_read,
206548f6a7a5SPavel Emelyanov 	.llseek		= seq_lseek,
206648f6a7a5SPavel Emelyanov 	.release	= seq_release_private,
206748f6a7a5SPavel Emelyanov };
2068640708a2SPavel Emelyanov #endif /* CONFIG_CHECKPOINT_RESTORE */
2069640708a2SPavel Emelyanov 
2070c52a47acSAl Viro static int proc_pident_instantiate(struct inode *dir,
2071c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
2072444ceed8SEric W. Biederman {
2073c5141e6dSEric Dumazet 	const struct pid_entry *p = ptr;
2074444ceed8SEric W. Biederman 	struct inode *inode;
2075444ceed8SEric W. Biederman 	struct proc_inode *ei;
2076444ceed8SEric W. Biederman 
207761a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
2078444ceed8SEric W. Biederman 	if (!inode)
2079444ceed8SEric W. Biederman 		goto out;
2080444ceed8SEric W. Biederman 
2081444ceed8SEric W. Biederman 	ei = PROC_I(inode);
2082444ceed8SEric W. Biederman 	inode->i_mode = p->mode;
2083444ceed8SEric W. Biederman 	if (S_ISDIR(inode->i_mode))
2084bfe86848SMiklos Szeredi 		set_nlink(inode, 2);	/* Use getattr to fix if necessary */
2085444ceed8SEric W. Biederman 	if (p->iop)
2086444ceed8SEric W. Biederman 		inode->i_op = p->iop;
2087444ceed8SEric W. Biederman 	if (p->fop)
2088444ceed8SEric W. Biederman 		inode->i_fop = p->fop;
2089444ceed8SEric W. Biederman 	ei->op = p->op;
2090fb045adbSNick Piggin 	d_set_d_op(dentry, &pid_dentry_operations);
2091444ceed8SEric W. Biederman 	d_add(dentry, inode);
2092444ceed8SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
20930b728e19SAl Viro 	if (pid_revalidate(dentry, 0))
2094c52a47acSAl Viro 		return 0;
2095444ceed8SEric W. Biederman out:
2096c52a47acSAl Viro 	return -ENOENT;
2097444ceed8SEric W. Biederman }
2098444ceed8SEric W. Biederman 
20991da177e4SLinus Torvalds static struct dentry *proc_pident_lookup(struct inode *dir,
21001da177e4SLinus Torvalds 					 struct dentry *dentry,
2101c5141e6dSEric Dumazet 					 const struct pid_entry *ents,
21027bcd6b0eSEric W. Biederman 					 unsigned int nents)
21031da177e4SLinus Torvalds {
2104c52a47acSAl Viro 	int error;
210599f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(dir);
2106c5141e6dSEric Dumazet 	const struct pid_entry *p, *last;
21071da177e4SLinus Torvalds 
2108c52a47acSAl Viro 	error = -ENOENT;
21091da177e4SLinus Torvalds 
211099f89551SEric W. Biederman 	if (!task)
211199f89551SEric W. Biederman 		goto out_no_task;
21121da177e4SLinus Torvalds 
211320cdc894SEric W. Biederman 	/*
211420cdc894SEric W. Biederman 	 * Yes, it does not scale. And it should not. Don't add
211520cdc894SEric W. Biederman 	 * new entries into /proc/<tgid>/ without very good reasons.
211620cdc894SEric W. Biederman 	 */
21177bcd6b0eSEric W. Biederman 	last = &ents[nents - 1];
21187bcd6b0eSEric W. Biederman 	for (p = ents; p <= last; p++) {
21191da177e4SLinus Torvalds 		if (p->len != dentry->d_name.len)
21201da177e4SLinus Torvalds 			continue;
21211da177e4SLinus Torvalds 		if (!memcmp(dentry->d_name.name, p->name, p->len))
21221da177e4SLinus Torvalds 			break;
21231da177e4SLinus Torvalds 	}
21247bcd6b0eSEric W. Biederman 	if (p > last)
21251da177e4SLinus Torvalds 		goto out;
21261da177e4SLinus Torvalds 
2127444ceed8SEric W. Biederman 	error = proc_pident_instantiate(dir, dentry, task, p);
21281da177e4SLinus Torvalds out:
212999f89551SEric W. Biederman 	put_task_struct(task);
213099f89551SEric W. Biederman out_no_task:
2131c52a47acSAl Viro 	return ERR_PTR(error);
21321da177e4SLinus Torvalds }
21331da177e4SLinus Torvalds 
2134f0c3b509SAl Viro static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
2135c5141e6dSEric Dumazet 		const struct pid_entry *ents, unsigned int nents)
213628a6d671SEric W. Biederman {
2137f0c3b509SAl Viro 	struct task_struct *task = get_proc_task(file_inode(file));
2138f0c3b509SAl Viro 	const struct pid_entry *p;
213928a6d671SEric W. Biederman 
214028a6d671SEric W. Biederman 	if (!task)
2141f0c3b509SAl Viro 		return -ENOENT;
214228a6d671SEric W. Biederman 
2143f0c3b509SAl Viro 	if (!dir_emit_dots(file, ctx))
214428a6d671SEric W. Biederman 		goto out;
21451da177e4SLinus Torvalds 
2146f0c3b509SAl Viro 	if (ctx->pos >= nents + 2)
2147f0c3b509SAl Viro 		goto out;
2148f0c3b509SAl Viro 
2149f0c3b509SAl Viro 	for (p = ents + (ctx->pos - 2); p <= ents + nents - 1; p++) {
2150f0c3b509SAl Viro 		if (!proc_fill_cache(file, ctx, p->name, p->len,
2151f0c3b509SAl Viro 				proc_pident_instantiate, task, p))
2152f0c3b509SAl Viro 			break;
2153f0c3b509SAl Viro 		ctx->pos++;
2154f0c3b509SAl Viro 	}
215528a6d671SEric W. Biederman out:
215661a28784SEric W. Biederman 	put_task_struct(task);
2157f0c3b509SAl Viro 	return 0;
21581da177e4SLinus Torvalds }
21591da177e4SLinus Torvalds 
21601da177e4SLinus Torvalds #ifdef CONFIG_SECURITY
216128a6d671SEric W. Biederman static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
216228a6d671SEric W. Biederman 				  size_t count, loff_t *ppos)
216328a6d671SEric W. Biederman {
2164496ad9aaSAl Viro 	struct inode * inode = file_inode(file);
216504ff9708SAl Viro 	char *p = NULL;
216628a6d671SEric W. Biederman 	ssize_t length;
216728a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
216828a6d671SEric W. Biederman 
216928a6d671SEric W. Biederman 	if (!task)
217004ff9708SAl Viro 		return -ESRCH;
217128a6d671SEric W. Biederman 
217228a6d671SEric W. Biederman 	length = security_getprocattr(task,
21732fddfeefSJosef "Jeff" Sipek 				      (char*)file->f_path.dentry->d_name.name,
217404ff9708SAl Viro 				      &p);
217528a6d671SEric W. Biederman 	put_task_struct(task);
217604ff9708SAl Viro 	if (length > 0)
217704ff9708SAl Viro 		length = simple_read_from_buffer(buf, count, ppos, p, length);
217804ff9708SAl Viro 	kfree(p);
217928a6d671SEric W. Biederman 	return length;
218028a6d671SEric W. Biederman }
218128a6d671SEric W. Biederman 
218228a6d671SEric W. Biederman static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
218328a6d671SEric W. Biederman 				   size_t count, loff_t *ppos)
218428a6d671SEric W. Biederman {
2185496ad9aaSAl Viro 	struct inode * inode = file_inode(file);
218628a6d671SEric W. Biederman 	char *page;
218728a6d671SEric W. Biederman 	ssize_t length;
218828a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
218928a6d671SEric W. Biederman 
219028a6d671SEric W. Biederman 	length = -ESRCH;
219128a6d671SEric W. Biederman 	if (!task)
219228a6d671SEric W. Biederman 		goto out_no_task;
219328a6d671SEric W. Biederman 	if (count > PAGE_SIZE)
219428a6d671SEric W. Biederman 		count = PAGE_SIZE;
219528a6d671SEric W. Biederman 
219628a6d671SEric W. Biederman 	/* No partial writes. */
219728a6d671SEric W. Biederman 	length = -EINVAL;
219828a6d671SEric W. Biederman 	if (*ppos != 0)
219928a6d671SEric W. Biederman 		goto out;
220028a6d671SEric W. Biederman 
220128a6d671SEric W. Biederman 	length = -ENOMEM;
2202e12ba74dSMel Gorman 	page = (char*)__get_free_page(GFP_TEMPORARY);
220328a6d671SEric W. Biederman 	if (!page)
220428a6d671SEric W. Biederman 		goto out;
220528a6d671SEric W. Biederman 
220628a6d671SEric W. Biederman 	length = -EFAULT;
220728a6d671SEric W. Biederman 	if (copy_from_user(page, buf, count))
220828a6d671SEric W. Biederman 		goto out_free;
220928a6d671SEric W. Biederman 
2210107db7c7SDavid Howells 	/* Guard against adverse ptrace interaction */
22119b1bf12dSKOSAKI Motohiro 	length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
2212107db7c7SDavid Howells 	if (length < 0)
2213107db7c7SDavid Howells 		goto out_free;
2214107db7c7SDavid Howells 
221528a6d671SEric W. Biederman 	length = security_setprocattr(task,
22162fddfeefSJosef "Jeff" Sipek 				      (char*)file->f_path.dentry->d_name.name,
221728a6d671SEric W. Biederman 				      (void*)page, count);
22189b1bf12dSKOSAKI Motohiro 	mutex_unlock(&task->signal->cred_guard_mutex);
221928a6d671SEric W. Biederman out_free:
222028a6d671SEric W. Biederman 	free_page((unsigned long) page);
222128a6d671SEric W. Biederman out:
222228a6d671SEric W. Biederman 	put_task_struct(task);
222328a6d671SEric W. Biederman out_no_task:
222428a6d671SEric W. Biederman 	return length;
222528a6d671SEric W. Biederman }
222628a6d671SEric W. Biederman 
222700977a59SArjan van de Ven static const struct file_operations proc_pid_attr_operations = {
222828a6d671SEric W. Biederman 	.read		= proc_pid_attr_read,
222928a6d671SEric W. Biederman 	.write		= proc_pid_attr_write,
223087df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
223128a6d671SEric W. Biederman };
223228a6d671SEric W. Biederman 
2233c5141e6dSEric Dumazet static const struct pid_entry attr_dir_stuff[] = {
2234631f9c18SAlexey Dobriyan 	REG("current",    S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2235631f9c18SAlexey Dobriyan 	REG("prev",       S_IRUGO,	   proc_pid_attr_operations),
2236631f9c18SAlexey Dobriyan 	REG("exec",       S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2237631f9c18SAlexey Dobriyan 	REG("fscreate",   S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2238631f9c18SAlexey Dobriyan 	REG("keycreate",  S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2239631f9c18SAlexey Dobriyan 	REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
224028a6d671SEric W. Biederman };
224128a6d671SEric W. Biederman 
2242f0c3b509SAl Viro static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
22431da177e4SLinus Torvalds {
2244f0c3b509SAl Viro 	return proc_pident_readdir(file, ctx,
224572d9dcfcSEric W. Biederman 				   attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
22461da177e4SLinus Torvalds }
22471da177e4SLinus Torvalds 
224800977a59SArjan van de Ven static const struct file_operations proc_attr_dir_operations = {
22491da177e4SLinus Torvalds 	.read		= generic_read_dir,
2250f0c3b509SAl Viro 	.iterate	= proc_attr_dir_readdir,
22516038f373SArnd Bergmann 	.llseek		= default_llseek,
22521da177e4SLinus Torvalds };
22531da177e4SLinus Torvalds 
225472d9dcfcSEric W. Biederman static struct dentry *proc_attr_dir_lookup(struct inode *dir,
225500cd8dd3SAl Viro 				struct dentry *dentry, unsigned int flags)
22561da177e4SLinus Torvalds {
22577bcd6b0eSEric W. Biederman 	return proc_pident_lookup(dir, dentry,
22587bcd6b0eSEric W. Biederman 				  attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
22591da177e4SLinus Torvalds }
22601da177e4SLinus Torvalds 
2261c5ef1c42SArjan van de Ven static const struct inode_operations proc_attr_dir_inode_operations = {
226272d9dcfcSEric W. Biederman 	.lookup		= proc_attr_dir_lookup,
226399f89551SEric W. Biederman 	.getattr	= pid_getattr,
22646d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
22651da177e4SLinus Torvalds };
22661da177e4SLinus Torvalds 
22671da177e4SLinus Torvalds #endif
22681da177e4SLinus Torvalds 
2269698ba7b5SChristoph Hellwig #ifdef CONFIG_ELF_CORE
22703cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
22713cb4a0bbSKawai, Hidehiro 					 size_t count, loff_t *ppos)
22723cb4a0bbSKawai, Hidehiro {
2273496ad9aaSAl Viro 	struct task_struct *task = get_proc_task(file_inode(file));
22743cb4a0bbSKawai, Hidehiro 	struct mm_struct *mm;
22753cb4a0bbSKawai, Hidehiro 	char buffer[PROC_NUMBUF];
22763cb4a0bbSKawai, Hidehiro 	size_t len;
22773cb4a0bbSKawai, Hidehiro 	int ret;
22783cb4a0bbSKawai, Hidehiro 
22793cb4a0bbSKawai, Hidehiro 	if (!task)
22803cb4a0bbSKawai, Hidehiro 		return -ESRCH;
22813cb4a0bbSKawai, Hidehiro 
22823cb4a0bbSKawai, Hidehiro 	ret = 0;
22833cb4a0bbSKawai, Hidehiro 	mm = get_task_mm(task);
22843cb4a0bbSKawai, Hidehiro 	if (mm) {
22853cb4a0bbSKawai, Hidehiro 		len = snprintf(buffer, sizeof(buffer), "%08lx\n",
22863cb4a0bbSKawai, Hidehiro 			       ((mm->flags & MMF_DUMP_FILTER_MASK) >>
22873cb4a0bbSKawai, Hidehiro 				MMF_DUMP_FILTER_SHIFT));
22883cb4a0bbSKawai, Hidehiro 		mmput(mm);
22893cb4a0bbSKawai, Hidehiro 		ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
22903cb4a0bbSKawai, Hidehiro 	}
22913cb4a0bbSKawai, Hidehiro 
22923cb4a0bbSKawai, Hidehiro 	put_task_struct(task);
22933cb4a0bbSKawai, Hidehiro 
22943cb4a0bbSKawai, Hidehiro 	return ret;
22953cb4a0bbSKawai, Hidehiro }
22963cb4a0bbSKawai, Hidehiro 
22973cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_write(struct file *file,
22983cb4a0bbSKawai, Hidehiro 					  const char __user *buf,
22993cb4a0bbSKawai, Hidehiro 					  size_t count,
23003cb4a0bbSKawai, Hidehiro 					  loff_t *ppos)
23013cb4a0bbSKawai, Hidehiro {
23023cb4a0bbSKawai, Hidehiro 	struct task_struct *task;
23033cb4a0bbSKawai, Hidehiro 	struct mm_struct *mm;
23043cb4a0bbSKawai, Hidehiro 	char buffer[PROC_NUMBUF], *end;
23053cb4a0bbSKawai, Hidehiro 	unsigned int val;
23063cb4a0bbSKawai, Hidehiro 	int ret;
23073cb4a0bbSKawai, Hidehiro 	int i;
23083cb4a0bbSKawai, Hidehiro 	unsigned long mask;
23093cb4a0bbSKawai, Hidehiro 
23103cb4a0bbSKawai, Hidehiro 	ret = -EFAULT;
23113cb4a0bbSKawai, Hidehiro 	memset(buffer, 0, sizeof(buffer));
23123cb4a0bbSKawai, Hidehiro 	if (count > sizeof(buffer) - 1)
23133cb4a0bbSKawai, Hidehiro 		count = sizeof(buffer) - 1;
23143cb4a0bbSKawai, Hidehiro 	if (copy_from_user(buffer, buf, count))
23153cb4a0bbSKawai, Hidehiro 		goto out_no_task;
23163cb4a0bbSKawai, Hidehiro 
23173cb4a0bbSKawai, Hidehiro 	ret = -EINVAL;
23183cb4a0bbSKawai, Hidehiro 	val = (unsigned int)simple_strtoul(buffer, &end, 0);
23193cb4a0bbSKawai, Hidehiro 	if (*end == '\n')
23203cb4a0bbSKawai, Hidehiro 		end++;
23213cb4a0bbSKawai, Hidehiro 	if (end - buffer == 0)
23223cb4a0bbSKawai, Hidehiro 		goto out_no_task;
23233cb4a0bbSKawai, Hidehiro 
23243cb4a0bbSKawai, Hidehiro 	ret = -ESRCH;
2325496ad9aaSAl Viro 	task = get_proc_task(file_inode(file));
23263cb4a0bbSKawai, Hidehiro 	if (!task)
23273cb4a0bbSKawai, Hidehiro 		goto out_no_task;
23283cb4a0bbSKawai, Hidehiro 
23293cb4a0bbSKawai, Hidehiro 	ret = end - buffer;
23303cb4a0bbSKawai, Hidehiro 	mm = get_task_mm(task);
23313cb4a0bbSKawai, Hidehiro 	if (!mm)
23323cb4a0bbSKawai, Hidehiro 		goto out_no_mm;
23333cb4a0bbSKawai, Hidehiro 
23343cb4a0bbSKawai, Hidehiro 	for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
23353cb4a0bbSKawai, Hidehiro 		if (val & mask)
23363cb4a0bbSKawai, Hidehiro 			set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
23373cb4a0bbSKawai, Hidehiro 		else
23383cb4a0bbSKawai, Hidehiro 			clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
23393cb4a0bbSKawai, Hidehiro 	}
23403cb4a0bbSKawai, Hidehiro 
23413cb4a0bbSKawai, Hidehiro 	mmput(mm);
23423cb4a0bbSKawai, Hidehiro  out_no_mm:
23433cb4a0bbSKawai, Hidehiro 	put_task_struct(task);
23443cb4a0bbSKawai, Hidehiro  out_no_task:
23453cb4a0bbSKawai, Hidehiro 	return ret;
23463cb4a0bbSKawai, Hidehiro }
23473cb4a0bbSKawai, Hidehiro 
23483cb4a0bbSKawai, Hidehiro static const struct file_operations proc_coredump_filter_operations = {
23493cb4a0bbSKawai, Hidehiro 	.read		= proc_coredump_filter_read,
23503cb4a0bbSKawai, Hidehiro 	.write		= proc_coredump_filter_write,
235187df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
23523cb4a0bbSKawai, Hidehiro };
23533cb4a0bbSKawai, Hidehiro #endif
23543cb4a0bbSKawai, Hidehiro 
2355aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING
235619aadc98SAlexey Dobriyan static int do_io_accounting(struct task_struct *task, struct seq_file *m, int whole)
2357aba76fdbSAndrew Morton {
2358940389b8SAndrea Righi 	struct task_io_accounting acct = task->ioac;
2359297c5d92SAndrea Righi 	unsigned long flags;
2360293eb1e7SVasiliy Kulikov 	int result;
2361297c5d92SAndrea Righi 
2362293eb1e7SVasiliy Kulikov 	result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2363293eb1e7SVasiliy Kulikov 	if (result)
2364293eb1e7SVasiliy Kulikov 		return result;
2365293eb1e7SVasiliy Kulikov 
2366293eb1e7SVasiliy Kulikov 	if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
2367293eb1e7SVasiliy Kulikov 		result = -EACCES;
2368293eb1e7SVasiliy Kulikov 		goto out_unlock;
2369293eb1e7SVasiliy Kulikov 	}
23701d1221f3SVasiliy Kulikov 
23715995477aSAndrea Righi 	if (whole && lock_task_sighand(task, &flags)) {
2372b2d002dbSAndrea Righi 		struct task_struct *t = task;
2373297c5d92SAndrea Righi 
23745995477aSAndrea Righi 		task_io_accounting_add(&acct, &task->signal->ioac);
23755995477aSAndrea Righi 		while_each_thread(task, t)
23765995477aSAndrea Righi 			task_io_accounting_add(&acct, &t->ioac);
2377297c5d92SAndrea Righi 
2378297c5d92SAndrea Righi 		unlock_task_sighand(task, &flags);
2379297c5d92SAndrea Righi 	}
238019aadc98SAlexey Dobriyan 	result = seq_printf(m,
2381aba76fdbSAndrew Morton 			"rchar: %llu\n"
2382aba76fdbSAndrew Morton 			"wchar: %llu\n"
2383aba76fdbSAndrew Morton 			"syscr: %llu\n"
2384aba76fdbSAndrew Morton 			"syscw: %llu\n"
2385aba76fdbSAndrew Morton 			"read_bytes: %llu\n"
2386aba76fdbSAndrew Morton 			"write_bytes: %llu\n"
2387aba76fdbSAndrew Morton 			"cancelled_write_bytes: %llu\n",
23887c44319dSAlexander Beregalov 			(unsigned long long)acct.rchar,
23897c44319dSAlexander Beregalov 			(unsigned long long)acct.wchar,
23907c44319dSAlexander Beregalov 			(unsigned long long)acct.syscr,
23917c44319dSAlexander Beregalov 			(unsigned long long)acct.syscw,
23927c44319dSAlexander Beregalov 			(unsigned long long)acct.read_bytes,
23937c44319dSAlexander Beregalov 			(unsigned long long)acct.write_bytes,
23947c44319dSAlexander Beregalov 			(unsigned long long)acct.cancelled_write_bytes);
2395293eb1e7SVasiliy Kulikov out_unlock:
2396293eb1e7SVasiliy Kulikov 	mutex_unlock(&task->signal->cred_guard_mutex);
2397293eb1e7SVasiliy Kulikov 	return result;
2398aba76fdbSAndrew Morton }
2399297c5d92SAndrea Righi 
240019aadc98SAlexey Dobriyan static int proc_tid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
240119aadc98SAlexey Dobriyan 				  struct pid *pid, struct task_struct *task)
2402297c5d92SAndrea Righi {
240319aadc98SAlexey Dobriyan 	return do_io_accounting(task, m, 0);
2404297c5d92SAndrea Righi }
2405297c5d92SAndrea Righi 
240619aadc98SAlexey Dobriyan static int proc_tgid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
240719aadc98SAlexey Dobriyan 				   struct pid *pid, struct task_struct *task)
2408297c5d92SAndrea Righi {
240919aadc98SAlexey Dobriyan 	return do_io_accounting(task, m, 1);
2410297c5d92SAndrea Righi }
2411297c5d92SAndrea Righi #endif /* CONFIG_TASK_IO_ACCOUNTING */
2412aba76fdbSAndrew Morton 
241322d917d8SEric W. Biederman #ifdef CONFIG_USER_NS
241422d917d8SEric W. Biederman static int proc_id_map_open(struct inode *inode, struct file *file,
2415ccf94f1bSFabian Frederick 	const struct seq_operations *seq_ops)
241622d917d8SEric W. Biederman {
241722d917d8SEric W. Biederman 	struct user_namespace *ns = NULL;
241822d917d8SEric W. Biederman 	struct task_struct *task;
241922d917d8SEric W. Biederman 	struct seq_file *seq;
242022d917d8SEric W. Biederman 	int ret = -EINVAL;
242122d917d8SEric W. Biederman 
242222d917d8SEric W. Biederman 	task = get_proc_task(inode);
242322d917d8SEric W. Biederman 	if (task) {
242422d917d8SEric W. Biederman 		rcu_read_lock();
242522d917d8SEric W. Biederman 		ns = get_user_ns(task_cred_xxx(task, user_ns));
242622d917d8SEric W. Biederman 		rcu_read_unlock();
242722d917d8SEric W. Biederman 		put_task_struct(task);
242822d917d8SEric W. Biederman 	}
242922d917d8SEric W. Biederman 	if (!ns)
243022d917d8SEric W. Biederman 		goto err;
243122d917d8SEric W. Biederman 
243222d917d8SEric W. Biederman 	ret = seq_open(file, seq_ops);
243322d917d8SEric W. Biederman 	if (ret)
243422d917d8SEric W. Biederman 		goto err_put_ns;
243522d917d8SEric W. Biederman 
243622d917d8SEric W. Biederman 	seq = file->private_data;
243722d917d8SEric W. Biederman 	seq->private = ns;
243822d917d8SEric W. Biederman 
243922d917d8SEric W. Biederman 	return 0;
244022d917d8SEric W. Biederman err_put_ns:
244122d917d8SEric W. Biederman 	put_user_ns(ns);
244222d917d8SEric W. Biederman err:
244322d917d8SEric W. Biederman 	return ret;
244422d917d8SEric W. Biederman }
244522d917d8SEric W. Biederman 
244622d917d8SEric W. Biederman static int proc_id_map_release(struct inode *inode, struct file *file)
244722d917d8SEric W. Biederman {
244822d917d8SEric W. Biederman 	struct seq_file *seq = file->private_data;
244922d917d8SEric W. Biederman 	struct user_namespace *ns = seq->private;
245022d917d8SEric W. Biederman 	put_user_ns(ns);
245122d917d8SEric W. Biederman 	return seq_release(inode, file);
245222d917d8SEric W. Biederman }
245322d917d8SEric W. Biederman 
245422d917d8SEric W. Biederman static int proc_uid_map_open(struct inode *inode, struct file *file)
245522d917d8SEric W. Biederman {
245622d917d8SEric W. Biederman 	return proc_id_map_open(inode, file, &proc_uid_seq_operations);
245722d917d8SEric W. Biederman }
245822d917d8SEric W. Biederman 
245922d917d8SEric W. Biederman static int proc_gid_map_open(struct inode *inode, struct file *file)
246022d917d8SEric W. Biederman {
246122d917d8SEric W. Biederman 	return proc_id_map_open(inode, file, &proc_gid_seq_operations);
246222d917d8SEric W. Biederman }
246322d917d8SEric W. Biederman 
2464f76d207aSEric W. Biederman static int proc_projid_map_open(struct inode *inode, struct file *file)
2465f76d207aSEric W. Biederman {
2466f76d207aSEric W. Biederman 	return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2467f76d207aSEric W. Biederman }
2468f76d207aSEric W. Biederman 
246922d917d8SEric W. Biederman static const struct file_operations proc_uid_map_operations = {
247022d917d8SEric W. Biederman 	.open		= proc_uid_map_open,
247122d917d8SEric W. Biederman 	.write		= proc_uid_map_write,
247222d917d8SEric W. Biederman 	.read		= seq_read,
247322d917d8SEric W. Biederman 	.llseek		= seq_lseek,
247422d917d8SEric W. Biederman 	.release	= proc_id_map_release,
247522d917d8SEric W. Biederman };
247622d917d8SEric W. Biederman 
247722d917d8SEric W. Biederman static const struct file_operations proc_gid_map_operations = {
247822d917d8SEric W. Biederman 	.open		= proc_gid_map_open,
247922d917d8SEric W. Biederman 	.write		= proc_gid_map_write,
248022d917d8SEric W. Biederman 	.read		= seq_read,
248122d917d8SEric W. Biederman 	.llseek		= seq_lseek,
248222d917d8SEric W. Biederman 	.release	= proc_id_map_release,
248322d917d8SEric W. Biederman };
2484f76d207aSEric W. Biederman 
2485f76d207aSEric W. Biederman static const struct file_operations proc_projid_map_operations = {
2486f76d207aSEric W. Biederman 	.open		= proc_projid_map_open,
2487f76d207aSEric W. Biederman 	.write		= proc_projid_map_write,
2488f76d207aSEric W. Biederman 	.read		= seq_read,
2489f76d207aSEric W. Biederman 	.llseek		= seq_lseek,
2490f76d207aSEric W. Biederman 	.release	= proc_id_map_release,
2491f76d207aSEric W. Biederman };
249222d917d8SEric W. Biederman #endif /* CONFIG_USER_NS */
249322d917d8SEric W. Biederman 
249447830723SKees Cook static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
249547830723SKees Cook 				struct pid *pid, struct task_struct *task)
249647830723SKees Cook {
2497a9712bc1SAl Viro 	int err = lock_trace(task);
2498a9712bc1SAl Viro 	if (!err) {
249947830723SKees Cook 		seq_printf(m, "%08x\n", task->personality);
2500a9712bc1SAl Viro 		unlock_trace(task);
2501a9712bc1SAl Viro 	}
2502a9712bc1SAl Viro 	return err;
250347830723SKees Cook }
250447830723SKees Cook 
2505801199ceSEric W. Biederman /*
250628a6d671SEric W. Biederman  * Thread groups
250728a6d671SEric W. Biederman  */
250800977a59SArjan van de Ven static const struct file_operations proc_task_operations;
2509c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations;
251020cdc894SEric W. Biederman 
2511c5141e6dSEric Dumazet static const struct pid_entry tgid_base_stuff[] = {
2512631f9c18SAlexey Dobriyan 	DIR("task",       S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2513631f9c18SAlexey Dobriyan 	DIR("fd",         S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2514640708a2SPavel Emelyanov #ifdef CONFIG_CHECKPOINT_RESTORE
2515640708a2SPavel Emelyanov 	DIR("map_files",  S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
2516640708a2SPavel Emelyanov #endif
2517631f9c18SAlexey Dobriyan 	DIR("fdinfo",     S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
25186b4e306aSEric W. Biederman 	DIR("ns",	  S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
2519b2211a36SAndrew Morton #ifdef CONFIG_NET
2520631f9c18SAlexey Dobriyan 	DIR("net",        S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2521b2211a36SAndrew Morton #endif
2522631f9c18SAlexey Dobriyan 	REG("environ",    S_IRUSR, proc_environ_operations),
2523f9ea536eSAlexey Dobriyan 	ONE("auxv",       S_IRUSR, proc_pid_auxv),
2524631f9c18SAlexey Dobriyan 	ONE("status",     S_IRUGO, proc_pid_status),
252535a35046SDjalal Harouni 	ONE("personality", S_IRUSR, proc_pid_personality),
25261c963eb1SAlexey Dobriyan 	ONE("limits",	  S_IRUGO, proc_pid_limits),
252743ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
2528631f9c18SAlexey Dobriyan 	REG("sched",      S_IRUGO|S_IWUSR, proc_pid_sched_operations),
252943ae34cbSIngo Molnar #endif
25305091faa4SMike Galbraith #ifdef CONFIG_SCHED_AUTOGROUP
25315091faa4SMike Galbraith 	REG("autogroup",  S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
25325091faa4SMike Galbraith #endif
25334614a696Sjohn stultz 	REG("comm",      S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2534ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
253509d93bd6SAlexey Dobriyan 	ONE("syscall",    S_IRUSR, proc_pid_syscall),
2536ebcb6734SRoland McGrath #endif
25372ca66ff7SAlexey Dobriyan 	ONE("cmdline",    S_IRUGO, proc_pid_cmdline),
2538631f9c18SAlexey Dobriyan 	ONE("stat",       S_IRUGO, proc_tgid_stat),
2539631f9c18SAlexey Dobriyan 	ONE("statm",      S_IRUGO, proc_pid_statm),
2540b7643757SSiddhesh Poyarekar 	REG("maps",       S_IRUGO, proc_pid_maps_operations),
254128a6d671SEric W. Biederman #ifdef CONFIG_NUMA
2542b7643757SSiddhesh Poyarekar 	REG("numa_maps",  S_IRUGO, proc_pid_numa_maps_operations),
254328a6d671SEric W. Biederman #endif
2544631f9c18SAlexey Dobriyan 	REG("mem",        S_IRUSR|S_IWUSR, proc_mem_operations),
2545631f9c18SAlexey Dobriyan 	LNK("cwd",        proc_cwd_link),
2546631f9c18SAlexey Dobriyan 	LNK("root",       proc_root_link),
2547631f9c18SAlexey Dobriyan 	LNK("exe",        proc_exe_link),
2548631f9c18SAlexey Dobriyan 	REG("mounts",     S_IRUGO, proc_mounts_operations),
2549631f9c18SAlexey Dobriyan 	REG("mountinfo",  S_IRUGO, proc_mountinfo_operations),
2550631f9c18SAlexey Dobriyan 	REG("mountstats", S_IRUSR, proc_mountstats_operations),
25511e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR
2552631f9c18SAlexey Dobriyan 	REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2553b7643757SSiddhesh Poyarekar 	REG("smaps",      S_IRUGO, proc_pid_smaps_operations),
255432ed74a4SDjalal Harouni 	REG("pagemap",    S_IRUSR, proc_pagemap_operations),
255528a6d671SEric W. Biederman #endif
255628a6d671SEric W. Biederman #ifdef CONFIG_SECURITY
2557631f9c18SAlexey Dobriyan 	DIR("attr",       S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
255828a6d671SEric W. Biederman #endif
255928a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS
2560edfcd606SAlexey Dobriyan 	ONE("wchan",      S_IRUGO, proc_pid_wchan),
256128a6d671SEric W. Biederman #endif
25622ec220e2SKen Chen #ifdef CONFIG_STACKTRACE
256335a35046SDjalal Harouni 	ONE("stack",      S_IRUSR, proc_pid_stack),
256428a6d671SEric W. Biederman #endif
256528a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS
2566f6e826caSAlexey Dobriyan 	ONE("schedstat",  S_IRUGO, proc_pid_schedstat),
256728a6d671SEric W. Biederman #endif
25689745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP
2569631f9c18SAlexey Dobriyan 	REG("latency",  S_IRUGO, proc_lstats_operations),
25709745512cSArjan van de Ven #endif
25718793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET
2572631f9c18SAlexey Dobriyan 	REG("cpuset",     S_IRUGO, proc_cpuset_operations),
257328a6d671SEric W. Biederman #endif
2574a424316cSPaul Menage #ifdef CONFIG_CGROUPS
2575631f9c18SAlexey Dobriyan 	REG("cgroup",  S_IRUGO, proc_cgroup_operations),
2576a424316cSPaul Menage #endif
25776ba51e37SAlexey Dobriyan 	ONE("oom_score",  S_IRUGO, proc_oom_score),
2578fa0cbbf1SDavid Rientjes 	REG("oom_adj",    S_IRUGO|S_IWUSR, proc_oom_adj_operations),
2579a63d83f4SDavid Rientjes 	REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
258028a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL
2581631f9c18SAlexey Dobriyan 	REG("loginuid",   S_IWUSR|S_IRUGO, proc_loginuid_operations),
2582631f9c18SAlexey Dobriyan 	REG("sessionid",  S_IRUGO, proc_sessionid_operations),
258328a6d671SEric W. Biederman #endif
2584f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
2585631f9c18SAlexey Dobriyan 	REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2586f4f154fdSAkinobu Mita #endif
2587698ba7b5SChristoph Hellwig #ifdef CONFIG_ELF_CORE
2588631f9c18SAlexey Dobriyan 	REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
25893cb4a0bbSKawai, Hidehiro #endif
2590aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING
259119aadc98SAlexey Dobriyan 	ONE("io",	S_IRUSR, proc_tgid_io_accounting),
2592aba76fdbSAndrew Morton #endif
2593f133eccaSChris Metcalf #ifdef CONFIG_HARDWALL
2594d962c144SAlexey Dobriyan 	ONE("hardwall",   S_IRUGO, proc_pid_hardwall),
2595f133eccaSChris Metcalf #endif
259622d917d8SEric W. Biederman #ifdef CONFIG_USER_NS
259722d917d8SEric W. Biederman 	REG("uid_map",    S_IRUGO|S_IWUSR, proc_uid_map_operations),
259822d917d8SEric W. Biederman 	REG("gid_map",    S_IRUGO|S_IWUSR, proc_gid_map_operations),
2599f76d207aSEric W. Biederman 	REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
260022d917d8SEric W. Biederman #endif
260148f6a7a5SPavel Emelyanov #ifdef CONFIG_CHECKPOINT_RESTORE
260248f6a7a5SPavel Emelyanov 	REG("timers",	  S_IRUGO, proc_timers_operations),
260348f6a7a5SPavel Emelyanov #endif
260428a6d671SEric W. Biederman };
260528a6d671SEric W. Biederman 
2606f0c3b509SAl Viro static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
260728a6d671SEric W. Biederman {
2608f0c3b509SAl Viro 	return proc_pident_readdir(file, ctx,
260928a6d671SEric W. Biederman 				   tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
261028a6d671SEric W. Biederman }
261128a6d671SEric W. Biederman 
261200977a59SArjan van de Ven static const struct file_operations proc_tgid_base_operations = {
261328a6d671SEric W. Biederman 	.read		= generic_read_dir,
2614f0c3b509SAl Viro 	.iterate	= proc_tgid_base_readdir,
26156038f373SArnd Bergmann 	.llseek		= default_llseek,
261628a6d671SEric W. Biederman };
261728a6d671SEric W. Biederman 
261800cd8dd3SAl Viro static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
261900cd8dd3SAl Viro {
26207bcd6b0eSEric W. Biederman 	return proc_pident_lookup(dir, dentry,
26217bcd6b0eSEric W. Biederman 				  tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
262228a6d671SEric W. Biederman }
262328a6d671SEric W. Biederman 
2624c5ef1c42SArjan van de Ven static const struct inode_operations proc_tgid_base_inode_operations = {
262528a6d671SEric W. Biederman 	.lookup		= proc_tgid_base_lookup,
262628a6d671SEric W. Biederman 	.getattr	= pid_getattr,
262728a6d671SEric W. Biederman 	.setattr	= proc_setattr,
26280499680aSVasiliy Kulikov 	.permission	= proc_pid_permission,
262928a6d671SEric W. Biederman };
263028a6d671SEric W. Biederman 
263160347f67SPavel Emelyanov static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
26321da177e4SLinus Torvalds {
263348e6484dSEric W. Biederman 	struct dentry *dentry, *leader, *dir;
26348578cea7SEric W. Biederman 	char buf[PROC_NUMBUF];
263548e6484dSEric W. Biederman 	struct qstr name;
26361da177e4SLinus Torvalds 
263748e6484dSEric W. Biederman 	name.name = buf;
263860347f67SPavel Emelyanov 	name.len = snprintf(buf, sizeof(buf), "%d", pid);
26394f522a24SAl Viro 	/* no ->d_hash() rejects on procfs */
264060347f67SPavel Emelyanov 	dentry = d_hash_and_lookup(mnt->mnt_root, &name);
264148e6484dSEric W. Biederman 	if (dentry) {
2642*bbd51924SEric W. Biederman 		d_invalidate(dentry);
264348e6484dSEric W. Biederman 		dput(dentry);
26441da177e4SLinus Torvalds 	}
26451da177e4SLinus Torvalds 
264648e6484dSEric W. Biederman 	name.name = buf;
264760347f67SPavel Emelyanov 	name.len = snprintf(buf, sizeof(buf), "%d", tgid);
264860347f67SPavel Emelyanov 	leader = d_hash_and_lookup(mnt->mnt_root, &name);
264948e6484dSEric W. Biederman 	if (!leader)
265048e6484dSEric W. Biederman 		goto out;
265148e6484dSEric W. Biederman 
265248e6484dSEric W. Biederman 	name.name = "task";
265348e6484dSEric W. Biederman 	name.len = strlen(name.name);
265448e6484dSEric W. Biederman 	dir = d_hash_and_lookup(leader, &name);
265548e6484dSEric W. Biederman 	if (!dir)
265648e6484dSEric W. Biederman 		goto out_put_leader;
265748e6484dSEric W. Biederman 
265848e6484dSEric W. Biederman 	name.name = buf;
265960347f67SPavel Emelyanov 	name.len = snprintf(buf, sizeof(buf), "%d", pid);
266048e6484dSEric W. Biederman 	dentry = d_hash_and_lookup(dir, &name);
266148e6484dSEric W. Biederman 	if (dentry) {
2662*bbd51924SEric W. Biederman 		d_invalidate(dentry);
266348e6484dSEric W. Biederman 		dput(dentry);
26641da177e4SLinus Torvalds 	}
266548e6484dSEric W. Biederman 
266648e6484dSEric W. Biederman 	dput(dir);
266748e6484dSEric W. Biederman out_put_leader:
266848e6484dSEric W. Biederman 	dput(leader);
266948e6484dSEric W. Biederman out:
267048e6484dSEric W. Biederman 	return;
26711da177e4SLinus Torvalds }
26721da177e4SLinus Torvalds 
26730895e91dSRandy Dunlap /**
26740895e91dSRandy Dunlap  * proc_flush_task -  Remove dcache entries for @task from the /proc dcache.
26750895e91dSRandy Dunlap  * @task: task that should be flushed.
26760895e91dSRandy Dunlap  *
26770895e91dSRandy Dunlap  * When flushing dentries from proc, one needs to flush them from global
267860347f67SPavel Emelyanov  * proc (proc_mnt) and from all the namespaces' procs this task was seen
26790895e91dSRandy Dunlap  * in. This call is supposed to do all of this job.
26800895e91dSRandy Dunlap  *
26810895e91dSRandy Dunlap  * Looks in the dcache for
26820895e91dSRandy Dunlap  * /proc/@pid
26830895e91dSRandy Dunlap  * /proc/@tgid/task/@pid
26840895e91dSRandy Dunlap  * if either directory is present flushes it and all of it'ts children
26850895e91dSRandy Dunlap  * from the dcache.
26860895e91dSRandy Dunlap  *
26870895e91dSRandy Dunlap  * It is safe and reasonable to cache /proc entries for a task until
26880895e91dSRandy Dunlap  * that task exits.  After that they just clog up the dcache with
26890895e91dSRandy Dunlap  * useless entries, possibly causing useful dcache entries to be
26900895e91dSRandy Dunlap  * flushed instead.  This routine is proved to flush those useless
26910895e91dSRandy Dunlap  * dcache entries at process exit time.
26920895e91dSRandy Dunlap  *
26930895e91dSRandy Dunlap  * NOTE: This routine is just an optimization so it does not guarantee
26940895e91dSRandy Dunlap  *       that no dcache entries will exist at process exit time it
26950895e91dSRandy Dunlap  *       just makes it very unlikely that any will persist.
269660347f67SPavel Emelyanov  */
269760347f67SPavel Emelyanov 
269860347f67SPavel Emelyanov void proc_flush_task(struct task_struct *task)
269960347f67SPavel Emelyanov {
27009fcc2d15SEric W. Biederman 	int i;
27019b4d1cbeSOleg Nesterov 	struct pid *pid, *tgid;
2702130f77ecSPavel Emelyanov 	struct upid *upid;
2703130f77ecSPavel Emelyanov 
2704130f77ecSPavel Emelyanov 	pid = task_pid(task);
2705130f77ecSPavel Emelyanov 	tgid = task_tgid(task);
27069fcc2d15SEric W. Biederman 
27079fcc2d15SEric W. Biederman 	for (i = 0; i <= pid->level; i++) {
2708130f77ecSPavel Emelyanov 		upid = &pid->numbers[i];
2709130f77ecSPavel Emelyanov 		proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
27109b4d1cbeSOleg Nesterov 					tgid->numbers[i].nr);
2711130f77ecSPavel Emelyanov 	}
271260347f67SPavel Emelyanov }
271360347f67SPavel Emelyanov 
2714c52a47acSAl Viro static int proc_pid_instantiate(struct inode *dir,
27159711ef99SAdrian Bunk 				   struct dentry * dentry,
2716c5141e6dSEric Dumazet 				   struct task_struct *task, const void *ptr)
2717444ceed8SEric W. Biederman {
2718444ceed8SEric W. Biederman 	struct inode *inode;
2719444ceed8SEric W. Biederman 
272061a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
2721444ceed8SEric W. Biederman 	if (!inode)
2722444ceed8SEric W. Biederman 		goto out;
2723444ceed8SEric W. Biederman 
2724444ceed8SEric W. Biederman 	inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2725444ceed8SEric W. Biederman 	inode->i_op = &proc_tgid_base_inode_operations;
2726444ceed8SEric W. Biederman 	inode->i_fop = &proc_tgid_base_operations;
2727444ceed8SEric W. Biederman 	inode->i_flags|=S_IMMUTABLE;
2728aed54175SVegard Nossum 
2729bfe86848SMiklos Szeredi 	set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
2730bfe86848SMiklos Szeredi 						  ARRAY_SIZE(tgid_base_stuff)));
2731444ceed8SEric W. Biederman 
2732fb045adbSNick Piggin 	d_set_d_op(dentry, &pid_dentry_operations);
2733444ceed8SEric W. Biederman 
2734444ceed8SEric W. Biederman 	d_add(dentry, inode);
2735444ceed8SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
27360b728e19SAl Viro 	if (pid_revalidate(dentry, 0))
2737c52a47acSAl Viro 		return 0;
2738444ceed8SEric W. Biederman out:
2739c52a47acSAl Viro 	return -ENOENT;
2740444ceed8SEric W. Biederman }
2741444ceed8SEric W. Biederman 
274200cd8dd3SAl Viro struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
27431da177e4SLinus Torvalds {
2744335eb531SAlexey Dobriyan 	int result = -ENOENT;
27451da177e4SLinus Torvalds 	struct task_struct *task;
27461da177e4SLinus Torvalds 	unsigned tgid;
2747b488893aSPavel Emelyanov 	struct pid_namespace *ns;
27481da177e4SLinus Torvalds 
2749dbcdb504SAlexey Dobriyan 	tgid = name_to_int(&dentry->d_name);
27501da177e4SLinus Torvalds 	if (tgid == ~0U)
27511da177e4SLinus Torvalds 		goto out;
27521da177e4SLinus Torvalds 
2753b488893aSPavel Emelyanov 	ns = dentry->d_sb->s_fs_info;
2754de758734SEric W. Biederman 	rcu_read_lock();
2755b488893aSPavel Emelyanov 	task = find_task_by_pid_ns(tgid, ns);
27561da177e4SLinus Torvalds 	if (task)
27571da177e4SLinus Torvalds 		get_task_struct(task);
2758de758734SEric W. Biederman 	rcu_read_unlock();
27591da177e4SLinus Torvalds 	if (!task)
27601da177e4SLinus Torvalds 		goto out;
27611da177e4SLinus Torvalds 
2762444ceed8SEric W. Biederman 	result = proc_pid_instantiate(dir, dentry, task, NULL);
276348e6484dSEric W. Biederman 	put_task_struct(task);
27641da177e4SLinus Torvalds out:
2765c52a47acSAl Viro 	return ERR_PTR(result);
27661da177e4SLinus Torvalds }
27671da177e4SLinus Torvalds 
27681da177e4SLinus Torvalds /*
27690804ef4bSEric W. Biederman  * Find the first task with tgid >= tgid
27700bc58a91SEric W. Biederman  *
27711da177e4SLinus Torvalds  */
277219fd4bb2SEric W. Biederman struct tgid_iter {
277319fd4bb2SEric W. Biederman 	unsigned int tgid;
27740804ef4bSEric W. Biederman 	struct task_struct *task;
277519fd4bb2SEric W. Biederman };
277619fd4bb2SEric W. Biederman static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
277719fd4bb2SEric W. Biederman {
27780804ef4bSEric W. Biederman 	struct pid *pid;
27791da177e4SLinus Torvalds 
278019fd4bb2SEric W. Biederman 	if (iter.task)
278119fd4bb2SEric W. Biederman 		put_task_struct(iter.task);
27820804ef4bSEric W. Biederman 	rcu_read_lock();
27830804ef4bSEric W. Biederman retry:
278419fd4bb2SEric W. Biederman 	iter.task = NULL;
278519fd4bb2SEric W. Biederman 	pid = find_ge_pid(iter.tgid, ns);
27860804ef4bSEric W. Biederman 	if (pid) {
278719fd4bb2SEric W. Biederman 		iter.tgid = pid_nr_ns(pid, ns);
278819fd4bb2SEric W. Biederman 		iter.task = pid_task(pid, PIDTYPE_PID);
27890804ef4bSEric W. Biederman 		/* What we to know is if the pid we have find is the
27900804ef4bSEric W. Biederman 		 * pid of a thread_group_leader.  Testing for task
27910804ef4bSEric W. Biederman 		 * being a thread_group_leader is the obvious thing
27920804ef4bSEric W. Biederman 		 * todo but there is a window when it fails, due to
27930804ef4bSEric W. Biederman 		 * the pid transfer logic in de_thread.
27940804ef4bSEric W. Biederman 		 *
27950804ef4bSEric W. Biederman 		 * So we perform the straight forward test of seeing
27960804ef4bSEric W. Biederman 		 * if the pid we have found is the pid of a thread
27970804ef4bSEric W. Biederman 		 * group leader, and don't worry if the task we have
27980804ef4bSEric W. Biederman 		 * found doesn't happen to be a thread group leader.
27990804ef4bSEric W. Biederman 		 * As we don't care in the case of readdir.
28000bc58a91SEric W. Biederman 		 */
280119fd4bb2SEric W. Biederman 		if (!iter.task || !has_group_leader_pid(iter.task)) {
280219fd4bb2SEric W. Biederman 			iter.tgid += 1;
28030804ef4bSEric W. Biederman 			goto retry;
280419fd4bb2SEric W. Biederman 		}
280519fd4bb2SEric W. Biederman 		get_task_struct(iter.task);
28061da177e4SLinus Torvalds 	}
2807454cc105SEric W. Biederman 	rcu_read_unlock();
280819fd4bb2SEric W. Biederman 	return iter;
28091da177e4SLinus Torvalds }
28101da177e4SLinus Torvalds 
28110097875bSEric W. Biederman #define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
28121da177e4SLinus Torvalds 
28131da177e4SLinus Torvalds /* for the /proc/ directory itself, after non-process stuff has been done */
2814f0c3b509SAl Viro int proc_pid_readdir(struct file *file, struct dir_context *ctx)
28151da177e4SLinus Torvalds {
281619fd4bb2SEric W. Biederman 	struct tgid_iter iter;
2817db963164SAl Viro 	struct pid_namespace *ns = file->f_dentry->d_sb->s_fs_info;
2818f0c3b509SAl Viro 	loff_t pos = ctx->pos;
28191da177e4SLinus Torvalds 
2820021ada7dSAl Viro 	if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
2821f0c3b509SAl Viro 		return 0;
28221da177e4SLinus Torvalds 
28230097875bSEric W. Biederman 	if (pos == TGID_OFFSET - 2) {
2824db963164SAl Viro 		struct inode *inode = ns->proc_self->d_inode;
2825db963164SAl Viro 		if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
2826f0c3b509SAl Viro 			return 0;
28270097875bSEric W. Biederman 		ctx->pos = pos = pos + 1;
2828021ada7dSAl Viro 	}
28290097875bSEric W. Biederman 	if (pos == TGID_OFFSET - 1) {
28300097875bSEric W. Biederman 		struct inode *inode = ns->proc_thread_self->d_inode;
28310097875bSEric W. Biederman 		if (!dir_emit(ctx, "thread-self", 11, inode->i_ino, DT_LNK))
28320097875bSEric W. Biederman 			return 0;
28330097875bSEric W. Biederman 		ctx->pos = pos = pos + 1;
28340097875bSEric W. Biederman 	}
28350097875bSEric W. Biederman 	iter.tgid = pos - TGID_OFFSET;
283619fd4bb2SEric W. Biederman 	iter.task = NULL;
283719fd4bb2SEric W. Biederman 	for (iter = next_tgid(ns, iter);
283819fd4bb2SEric W. Biederman 	     iter.task;
283919fd4bb2SEric W. Biederman 	     iter.tgid += 1, iter = next_tgid(ns, iter)) {
2840f0c3b509SAl Viro 		char name[PROC_NUMBUF];
2841f0c3b509SAl Viro 		int len;
2842f0c3b509SAl Viro 		if (!has_pid_permissions(ns, iter.task, 2))
2843f0c3b509SAl Viro 			continue;
28440499680aSVasiliy Kulikov 
2845f0c3b509SAl Viro 		len = snprintf(name, sizeof(name), "%d", iter.tgid);
2846f0c3b509SAl Viro 		ctx->pos = iter.tgid + TGID_OFFSET;
2847f0c3b509SAl Viro 		if (!proc_fill_cache(file, ctx, name, len,
2848f0c3b509SAl Viro 				     proc_pid_instantiate, iter.task, NULL)) {
284919fd4bb2SEric W. Biederman 			put_task_struct(iter.task);
2850f0c3b509SAl Viro 			return 0;
28511da177e4SLinus Torvalds 		}
28521da177e4SLinus Torvalds 	}
2853f0c3b509SAl Viro 	ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
28541da177e4SLinus Torvalds 	return 0;
28551da177e4SLinus Torvalds }
28561da177e4SLinus Torvalds 
28570bc58a91SEric W. Biederman /*
285828a6d671SEric W. Biederman  * Tasks
285928a6d671SEric W. Biederman  */
2860c5141e6dSEric Dumazet static const struct pid_entry tid_base_stuff[] = {
2861631f9c18SAlexey Dobriyan 	DIR("fd",        S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
28623835541dSJerome Marchand 	DIR("fdinfo",    S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
28636b4e306aSEric W. Biederman 	DIR("ns",	 S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
28646ba8ed79SEric W. Biederman #ifdef CONFIG_NET
28656ba8ed79SEric W. Biederman 	DIR("net",        S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
28666ba8ed79SEric W. Biederman #endif
2867631f9c18SAlexey Dobriyan 	REG("environ",   S_IRUSR, proc_environ_operations),
2868f9ea536eSAlexey Dobriyan 	ONE("auxv",      S_IRUSR, proc_pid_auxv),
2869631f9c18SAlexey Dobriyan 	ONE("status",    S_IRUGO, proc_pid_status),
287035a35046SDjalal Harouni 	ONE("personality", S_IRUSR, proc_pid_personality),
28711c963eb1SAlexey Dobriyan 	ONE("limits",	 S_IRUGO, proc_pid_limits),
287243ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
2873631f9c18SAlexey Dobriyan 	REG("sched",     S_IRUGO|S_IWUSR, proc_pid_sched_operations),
287443ae34cbSIngo Molnar #endif
28754614a696Sjohn stultz 	REG("comm",      S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2876ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
287709d93bd6SAlexey Dobriyan 	ONE("syscall",   S_IRUSR, proc_pid_syscall),
2878ebcb6734SRoland McGrath #endif
28792ca66ff7SAlexey Dobriyan 	ONE("cmdline",   S_IRUGO, proc_pid_cmdline),
2880631f9c18SAlexey Dobriyan 	ONE("stat",      S_IRUGO, proc_tid_stat),
2881631f9c18SAlexey Dobriyan 	ONE("statm",     S_IRUGO, proc_pid_statm),
2882b7643757SSiddhesh Poyarekar 	REG("maps",      S_IRUGO, proc_tid_maps_operations),
288381841161SCyrill Gorcunov #ifdef CONFIG_CHECKPOINT_RESTORE
288481841161SCyrill Gorcunov 	REG("children",  S_IRUGO, proc_tid_children_operations),
288581841161SCyrill Gorcunov #endif
288628a6d671SEric W. Biederman #ifdef CONFIG_NUMA
2887b7643757SSiddhesh Poyarekar 	REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
288828a6d671SEric W. Biederman #endif
2889631f9c18SAlexey Dobriyan 	REG("mem",       S_IRUSR|S_IWUSR, proc_mem_operations),
2890631f9c18SAlexey Dobriyan 	LNK("cwd",       proc_cwd_link),
2891631f9c18SAlexey Dobriyan 	LNK("root",      proc_root_link),
2892631f9c18SAlexey Dobriyan 	LNK("exe",       proc_exe_link),
2893631f9c18SAlexey Dobriyan 	REG("mounts",    S_IRUGO, proc_mounts_operations),
2894631f9c18SAlexey Dobriyan 	REG("mountinfo",  S_IRUGO, proc_mountinfo_operations),
28951e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR
2896631f9c18SAlexey Dobriyan 	REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2897b7643757SSiddhesh Poyarekar 	REG("smaps",     S_IRUGO, proc_tid_smaps_operations),
289832ed74a4SDjalal Harouni 	REG("pagemap",    S_IRUSR, proc_pagemap_operations),
289928a6d671SEric W. Biederman #endif
290028a6d671SEric W. Biederman #ifdef CONFIG_SECURITY
2901631f9c18SAlexey Dobriyan 	DIR("attr",      S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
290228a6d671SEric W. Biederman #endif
290328a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS
2904edfcd606SAlexey Dobriyan 	ONE("wchan",     S_IRUGO, proc_pid_wchan),
290528a6d671SEric W. Biederman #endif
29062ec220e2SKen Chen #ifdef CONFIG_STACKTRACE
290735a35046SDjalal Harouni 	ONE("stack",      S_IRUSR, proc_pid_stack),
290828a6d671SEric W. Biederman #endif
290928a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS
2910f6e826caSAlexey Dobriyan 	ONE("schedstat", S_IRUGO, proc_pid_schedstat),
291128a6d671SEric W. Biederman #endif
29129745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP
2913631f9c18SAlexey Dobriyan 	REG("latency",  S_IRUGO, proc_lstats_operations),
29149745512cSArjan van de Ven #endif
29158793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET
2916631f9c18SAlexey Dobriyan 	REG("cpuset",    S_IRUGO, proc_cpuset_operations),
291728a6d671SEric W. Biederman #endif
2918a424316cSPaul Menage #ifdef CONFIG_CGROUPS
2919631f9c18SAlexey Dobriyan 	REG("cgroup",  S_IRUGO, proc_cgroup_operations),
2920a424316cSPaul Menage #endif
29216ba51e37SAlexey Dobriyan 	ONE("oom_score", S_IRUGO, proc_oom_score),
2922fa0cbbf1SDavid Rientjes 	REG("oom_adj",   S_IRUGO|S_IWUSR, proc_oom_adj_operations),
2923a63d83f4SDavid Rientjes 	REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
292428a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL
2925631f9c18SAlexey Dobriyan 	REG("loginuid",  S_IWUSR|S_IRUGO, proc_loginuid_operations),
292626ec3c64SAl Viro 	REG("sessionid",  S_IRUGO, proc_sessionid_operations),
292728a6d671SEric W. Biederman #endif
2928f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
2929631f9c18SAlexey Dobriyan 	REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2930f4f154fdSAkinobu Mita #endif
2931297c5d92SAndrea Righi #ifdef CONFIG_TASK_IO_ACCOUNTING
293219aadc98SAlexey Dobriyan 	ONE("io",	S_IRUSR, proc_tid_io_accounting),
2933297c5d92SAndrea Righi #endif
2934f133eccaSChris Metcalf #ifdef CONFIG_HARDWALL
2935d962c144SAlexey Dobriyan 	ONE("hardwall",   S_IRUGO, proc_pid_hardwall),
2936f133eccaSChris Metcalf #endif
293722d917d8SEric W. Biederman #ifdef CONFIG_USER_NS
293822d917d8SEric W. Biederman 	REG("uid_map",    S_IRUGO|S_IWUSR, proc_uid_map_operations),
293922d917d8SEric W. Biederman 	REG("gid_map",    S_IRUGO|S_IWUSR, proc_gid_map_operations),
2940f76d207aSEric W. Biederman 	REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
294122d917d8SEric W. Biederman #endif
294228a6d671SEric W. Biederman };
294328a6d671SEric W. Biederman 
2944f0c3b509SAl Viro static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
294528a6d671SEric W. Biederman {
2946f0c3b509SAl Viro 	return proc_pident_readdir(file, ctx,
294728a6d671SEric W. Biederman 				   tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
294828a6d671SEric W. Biederman }
294928a6d671SEric W. Biederman 
295000cd8dd3SAl Viro static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
295100cd8dd3SAl Viro {
29527bcd6b0eSEric W. Biederman 	return proc_pident_lookup(dir, dentry,
29537bcd6b0eSEric W. Biederman 				  tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
295428a6d671SEric W. Biederman }
295528a6d671SEric W. Biederman 
295600977a59SArjan van de Ven static const struct file_operations proc_tid_base_operations = {
295728a6d671SEric W. Biederman 	.read		= generic_read_dir,
2958f0c3b509SAl Viro 	.iterate	= proc_tid_base_readdir,
29596038f373SArnd Bergmann 	.llseek		= default_llseek,
296028a6d671SEric W. Biederman };
296128a6d671SEric W. Biederman 
2962c5ef1c42SArjan van de Ven static const struct inode_operations proc_tid_base_inode_operations = {
296328a6d671SEric W. Biederman 	.lookup		= proc_tid_base_lookup,
296428a6d671SEric W. Biederman 	.getattr	= pid_getattr,
296528a6d671SEric W. Biederman 	.setattr	= proc_setattr,
296628a6d671SEric W. Biederman };
296728a6d671SEric W. Biederman 
2968c52a47acSAl Viro static int proc_task_instantiate(struct inode *dir,
2969c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
2970444ceed8SEric W. Biederman {
2971444ceed8SEric W. Biederman 	struct inode *inode;
297261a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
2973444ceed8SEric W. Biederman 
2974444ceed8SEric W. Biederman 	if (!inode)
2975444ceed8SEric W. Biederman 		goto out;
2976444ceed8SEric W. Biederman 	inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2977444ceed8SEric W. Biederman 	inode->i_op = &proc_tid_base_inode_operations;
2978444ceed8SEric W. Biederman 	inode->i_fop = &proc_tid_base_operations;
2979444ceed8SEric W. Biederman 	inode->i_flags|=S_IMMUTABLE;
2980aed54175SVegard Nossum 
2981bfe86848SMiklos Szeredi 	set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
2982bfe86848SMiklos Szeredi 						  ARRAY_SIZE(tid_base_stuff)));
2983444ceed8SEric W. Biederman 
2984fb045adbSNick Piggin 	d_set_d_op(dentry, &pid_dentry_operations);
2985444ceed8SEric W. Biederman 
2986444ceed8SEric W. Biederman 	d_add(dentry, inode);
2987444ceed8SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
29880b728e19SAl Viro 	if (pid_revalidate(dentry, 0))
2989c52a47acSAl Viro 		return 0;
2990444ceed8SEric W. Biederman out:
2991c52a47acSAl Viro 	return -ENOENT;
2992444ceed8SEric W. Biederman }
2993444ceed8SEric W. Biederman 
299400cd8dd3SAl Viro static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
299528a6d671SEric W. Biederman {
2996c52a47acSAl Viro 	int result = -ENOENT;
299728a6d671SEric W. Biederman 	struct task_struct *task;
299828a6d671SEric W. Biederman 	struct task_struct *leader = get_proc_task(dir);
299928a6d671SEric W. Biederman 	unsigned tid;
3000b488893aSPavel Emelyanov 	struct pid_namespace *ns;
300128a6d671SEric W. Biederman 
300228a6d671SEric W. Biederman 	if (!leader)
300328a6d671SEric W. Biederman 		goto out_no_task;
300428a6d671SEric W. Biederman 
3005dbcdb504SAlexey Dobriyan 	tid = name_to_int(&dentry->d_name);
300628a6d671SEric W. Biederman 	if (tid == ~0U)
300728a6d671SEric W. Biederman 		goto out;
300828a6d671SEric W. Biederman 
3009b488893aSPavel Emelyanov 	ns = dentry->d_sb->s_fs_info;
301028a6d671SEric W. Biederman 	rcu_read_lock();
3011b488893aSPavel Emelyanov 	task = find_task_by_pid_ns(tid, ns);
301228a6d671SEric W. Biederman 	if (task)
301328a6d671SEric W. Biederman 		get_task_struct(task);
301428a6d671SEric W. Biederman 	rcu_read_unlock();
301528a6d671SEric W. Biederman 	if (!task)
301628a6d671SEric W. Biederman 		goto out;
3017bac0abd6SPavel Emelyanov 	if (!same_thread_group(leader, task))
301828a6d671SEric W. Biederman 		goto out_drop_task;
301928a6d671SEric W. Biederman 
3020444ceed8SEric W. Biederman 	result = proc_task_instantiate(dir, dentry, task, NULL);
302128a6d671SEric W. Biederman out_drop_task:
302228a6d671SEric W. Biederman 	put_task_struct(task);
302328a6d671SEric W. Biederman out:
302428a6d671SEric W. Biederman 	put_task_struct(leader);
302528a6d671SEric W. Biederman out_no_task:
3026c52a47acSAl Viro 	return ERR_PTR(result);
302728a6d671SEric W. Biederman }
302828a6d671SEric W. Biederman 
302928a6d671SEric W. Biederman /*
30300bc58a91SEric W. Biederman  * Find the first tid of a thread group to return to user space.
30310bc58a91SEric W. Biederman  *
30320bc58a91SEric W. Biederman  * Usually this is just the thread group leader, but if the users
30330bc58a91SEric W. Biederman  * buffer was too small or there was a seek into the middle of the
30340bc58a91SEric W. Biederman  * directory we have more work todo.
30350bc58a91SEric W. Biederman  *
30360bc58a91SEric W. Biederman  * In the case of a short read we start with find_task_by_pid.
30370bc58a91SEric W. Biederman  *
30380bc58a91SEric W. Biederman  * In the case of a seek we start with the leader and walk nr
30390bc58a91SEric W. Biederman  * threads past it.
30400bc58a91SEric W. Biederman  */
30419f6e963fSOleg Nesterov static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
30429f6e963fSOleg Nesterov 					struct pid_namespace *ns)
30430bc58a91SEric W. Biederman {
3044d855a4b7SOleg Nesterov 	struct task_struct *pos, *task;
30459f6e963fSOleg Nesterov 	unsigned long nr = f_pos;
30469f6e963fSOleg Nesterov 
30479f6e963fSOleg Nesterov 	if (nr != f_pos)	/* 32bit overflow? */
30489f6e963fSOleg Nesterov 		return NULL;
30490bc58a91SEric W. Biederman 
3050cc288738SEric W. Biederman 	rcu_read_lock();
3051d855a4b7SOleg Nesterov 	task = pid_task(pid, PIDTYPE_PID);
3052d855a4b7SOleg Nesterov 	if (!task)
3053d855a4b7SOleg Nesterov 		goto fail;
3054d855a4b7SOleg Nesterov 
3055d855a4b7SOleg Nesterov 	/* Attempt to start with the tid of a thread */
30569f6e963fSOleg Nesterov 	if (tid && nr) {
3057b488893aSPavel Emelyanov 		pos = find_task_by_pid_ns(tid, ns);
3058d855a4b7SOleg Nesterov 		if (pos && same_thread_group(pos, task))
3059a872ff0cSOleg Nesterov 			goto found;
30600bc58a91SEric W. Biederman 	}
30610bc58a91SEric W. Biederman 
30620bc58a91SEric W. Biederman 	/* If nr exceeds the number of threads there is nothing todo */
30639f6e963fSOleg Nesterov 	if (nr >= get_nr_threads(task))
3064c986c14aSOleg Nesterov 		goto fail;
3065a872ff0cSOleg Nesterov 
3066a872ff0cSOleg Nesterov 	/* If we haven't found our starting place yet start
3067a872ff0cSOleg Nesterov 	 * with the leader and walk nr threads forward.
3068a872ff0cSOleg Nesterov 	 */
3069d855a4b7SOleg Nesterov 	pos = task = task->group_leader;
3070c986c14aSOleg Nesterov 	do {
30719f6e963fSOleg Nesterov 		if (!nr--)
3072c986c14aSOleg Nesterov 			goto found;
3073d855a4b7SOleg Nesterov 	} while_each_thread(task, pos);
3074c986c14aSOleg Nesterov fail:
3075a872ff0cSOleg Nesterov 	pos = NULL;
3076a872ff0cSOleg Nesterov 	goto out;
3077a872ff0cSOleg Nesterov found:
3078a872ff0cSOleg Nesterov 	get_task_struct(pos);
3079a872ff0cSOleg Nesterov out:
3080cc288738SEric W. Biederman 	rcu_read_unlock();
30810bc58a91SEric W. Biederman 	return pos;
30820bc58a91SEric W. Biederman }
30830bc58a91SEric W. Biederman 
30840bc58a91SEric W. Biederman /*
30850bc58a91SEric W. Biederman  * Find the next thread in the thread list.
30860bc58a91SEric W. Biederman  * Return NULL if there is an error or no next thread.
30870bc58a91SEric W. Biederman  *
30880bc58a91SEric W. Biederman  * The reference to the input task_struct is released.
30890bc58a91SEric W. Biederman  */
30900bc58a91SEric W. Biederman static struct task_struct *next_tid(struct task_struct *start)
30910bc58a91SEric W. Biederman {
3092c1df7fb8SOleg Nesterov 	struct task_struct *pos = NULL;
3093cc288738SEric W. Biederman 	rcu_read_lock();
3094c1df7fb8SOleg Nesterov 	if (pid_alive(start)) {
30950bc58a91SEric W. Biederman 		pos = next_thread(start);
3096c1df7fb8SOleg Nesterov 		if (thread_group_leader(pos))
30970bc58a91SEric W. Biederman 			pos = NULL;
3098c1df7fb8SOleg Nesterov 		else
3099c1df7fb8SOleg Nesterov 			get_task_struct(pos);
3100c1df7fb8SOleg Nesterov 	}
3101cc288738SEric W. Biederman 	rcu_read_unlock();
31020bc58a91SEric W. Biederman 	put_task_struct(start);
31030bc58a91SEric W. Biederman 	return pos;
31040bc58a91SEric W. Biederman }
31050bc58a91SEric W. Biederman 
31061da177e4SLinus Torvalds /* for the /proc/TGID/task/ directories */
3107f0c3b509SAl Viro static int proc_task_readdir(struct file *file, struct dir_context *ctx)
31081da177e4SLinus Torvalds {
3109d855a4b7SOleg Nesterov 	struct inode *inode = file_inode(file);
3110d855a4b7SOleg Nesterov 	struct task_struct *task;
3111b488893aSPavel Emelyanov 	struct pid_namespace *ns;
3112f0c3b509SAl Viro 	int tid;
31131da177e4SLinus Torvalds 
3114d855a4b7SOleg Nesterov 	if (proc_inode_is_dead(inode))
3115f0c3b509SAl Viro 		return -ENOENT;
31161da177e4SLinus Torvalds 
3117f0c3b509SAl Viro 	if (!dir_emit_dots(file, ctx))
3118d855a4b7SOleg Nesterov 		return 0;
31191da177e4SLinus Torvalds 
31200bc58a91SEric W. Biederman 	/* f_version caches the tgid value that the last readdir call couldn't
31210bc58a91SEric W. Biederman 	 * return. lseek aka telldir automagically resets f_version to 0.
31220bc58a91SEric W. Biederman 	 */
3123f0c3b509SAl Viro 	ns = file->f_dentry->d_sb->s_fs_info;
3124f0c3b509SAl Viro 	tid = (int)file->f_version;
3125f0c3b509SAl Viro 	file->f_version = 0;
3126d855a4b7SOleg Nesterov 	for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
31270bc58a91SEric W. Biederman 	     task;
3128f0c3b509SAl Viro 	     task = next_tid(task), ctx->pos++) {
3129f0c3b509SAl Viro 		char name[PROC_NUMBUF];
3130f0c3b509SAl Viro 		int len;
3131b488893aSPavel Emelyanov 		tid = task_pid_nr_ns(task, ns);
3132f0c3b509SAl Viro 		len = snprintf(name, sizeof(name), "%d", tid);
3133f0c3b509SAl Viro 		if (!proc_fill_cache(file, ctx, name, len,
3134f0c3b509SAl Viro 				proc_task_instantiate, task, NULL)) {
31350bc58a91SEric W. Biederman 			/* returning this tgid failed, save it as the first
31360bc58a91SEric W. Biederman 			 * pid for the next readir call */
3137f0c3b509SAl Viro 			file->f_version = (u64)tid;
31380bc58a91SEric W. Biederman 			put_task_struct(task);
31391da177e4SLinus Torvalds 			break;
31400bc58a91SEric W. Biederman 		}
31411da177e4SLinus Torvalds 	}
3142d855a4b7SOleg Nesterov 
3143f0c3b509SAl Viro 	return 0;
31441da177e4SLinus Torvalds }
31456e66b52bSEric W. Biederman 
31466e66b52bSEric W. Biederman static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
31476e66b52bSEric W. Biederman {
31486e66b52bSEric W. Biederman 	struct inode *inode = dentry->d_inode;
314999f89551SEric W. Biederman 	struct task_struct *p = get_proc_task(inode);
31506e66b52bSEric W. Biederman 	generic_fillattr(inode, stat);
31516e66b52bSEric W. Biederman 
315299f89551SEric W. Biederman 	if (p) {
315399f89551SEric W. Biederman 		stat->nlink += get_nr_threads(p);
315499f89551SEric W. Biederman 		put_task_struct(p);
31556e66b52bSEric W. Biederman 	}
31566e66b52bSEric W. Biederman 
31576e66b52bSEric W. Biederman 	return 0;
31586e66b52bSEric W. Biederman }
315928a6d671SEric W. Biederman 
3160c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations = {
316128a6d671SEric W. Biederman 	.lookup		= proc_task_lookup,
316228a6d671SEric W. Biederman 	.getattr	= proc_task_getattr,
316328a6d671SEric W. Biederman 	.setattr	= proc_setattr,
31640499680aSVasiliy Kulikov 	.permission	= proc_pid_permission,
316528a6d671SEric W. Biederman };
316628a6d671SEric W. Biederman 
316700977a59SArjan van de Ven static const struct file_operations proc_task_operations = {
316828a6d671SEric W. Biederman 	.read		= generic_read_dir,
3169f0c3b509SAl Viro 	.iterate	= proc_task_readdir,
31706038f373SArnd Bergmann 	.llseek		= default_llseek,
317128a6d671SEric W. Biederman };
3172