xref: /openbmc/linux/fs/proc/base.c (revision 15ef0298deb3929eb6ad6d2334fd2059fd53807c)
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 {
1081da177e4SLinus Torvalds 	char *name;
109c5141e6dSEric Dumazet 	int len;
110d161a13fSAl Viro 	umode_t mode;
111c5ef1c42SArjan van de Ven 	const struct inode_operations *iop;
11200977a59SArjan van de Ven 	const struct file_operations *fop;
11320cdc894SEric W. Biederman 	union proc_op op;
1141da177e4SLinus Torvalds };
1151da177e4SLinus Torvalds 
11661a28784SEric W. Biederman #define NOD(NAME, MODE, IOP, FOP, OP) {			\
11720cdc894SEric W. Biederman 	.name = (NAME),					\
118c5141e6dSEric Dumazet 	.len  = sizeof(NAME) - 1,			\
11920cdc894SEric W. Biederman 	.mode = MODE,					\
12020cdc894SEric W. Biederman 	.iop  = IOP,					\
12120cdc894SEric W. Biederman 	.fop  = FOP,					\
12220cdc894SEric W. Biederman 	.op   = OP,					\
12320cdc894SEric W. Biederman }
12420cdc894SEric W. Biederman 
125631f9c18SAlexey Dobriyan #define DIR(NAME, MODE, iops, fops)	\
126631f9c18SAlexey Dobriyan 	NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
127631f9c18SAlexey Dobriyan #define LNK(NAME, get_link)					\
12861a28784SEric W. Biederman 	NOD(NAME, (S_IFLNK|S_IRWXUGO),				\
12920cdc894SEric W. Biederman 		&proc_pid_link_inode_operations, NULL,		\
130631f9c18SAlexey Dobriyan 		{ .proc_get_link = get_link } )
131631f9c18SAlexey Dobriyan #define REG(NAME, MODE, fops)				\
132631f9c18SAlexey Dobriyan 	NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
133631f9c18SAlexey Dobriyan #define INF(NAME, MODE, read)				\
13461a28784SEric W. Biederman 	NOD(NAME, (S_IFREG|(MODE)), 			\
13520cdc894SEric W. Biederman 		NULL, &proc_info_file_operations,	\
136631f9c18SAlexey Dobriyan 		{ .proc_read = read } )
137631f9c18SAlexey Dobriyan #define ONE(NAME, MODE, show)				\
138be614086SEric W. Biederman 	NOD(NAME, (S_IFREG|(MODE)), 			\
139be614086SEric W. Biederman 		NULL, &proc_single_file_operations,	\
140631f9c18SAlexey Dobriyan 		{ .proc_show = show } )
1411da177e4SLinus Torvalds 
142aed54175SVegard Nossum /*
143aed54175SVegard Nossum  * Count the number of hardlinks for the pid_entry table, excluding the .
144aed54175SVegard Nossum  * and .. links.
145aed54175SVegard Nossum  */
146aed54175SVegard Nossum static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
147aed54175SVegard Nossum 	unsigned int n)
148aed54175SVegard Nossum {
149aed54175SVegard Nossum 	unsigned int i;
150aed54175SVegard Nossum 	unsigned int count;
151aed54175SVegard Nossum 
152aed54175SVegard Nossum 	count = 0;
153aed54175SVegard Nossum 	for (i = 0; i < n; ++i) {
154aed54175SVegard Nossum 		if (S_ISDIR(entries[i].mode))
155aed54175SVegard Nossum 			++count;
156aed54175SVegard Nossum 	}
157aed54175SVegard Nossum 
158aed54175SVegard Nossum 	return count;
159aed54175SVegard Nossum }
160aed54175SVegard Nossum 
161f7ad3c6bSMiklos Szeredi static int get_task_root(struct task_struct *task, struct path *root)
1621da177e4SLinus Torvalds {
1637c2c7d99SHugh Dickins 	int result = -ENOENT;
1647c2c7d99SHugh Dickins 
1650494f6ecSMiklos Szeredi 	task_lock(task);
166f7ad3c6bSMiklos Szeredi 	if (task->fs) {
167f7ad3c6bSMiklos Szeredi 		get_fs_root(task->fs, root);
1687c2c7d99SHugh Dickins 		result = 0;
1697c2c7d99SHugh Dickins 	}
1700494f6ecSMiklos Szeredi 	task_unlock(task);
1717c2c7d99SHugh Dickins 	return result;
1720494f6ecSMiklos Szeredi }
1730494f6ecSMiklos Szeredi 
1747773fbc5SCyrill Gorcunov static int proc_cwd_link(struct dentry *dentry, struct path *path)
1750494f6ecSMiklos Szeredi {
1767773fbc5SCyrill Gorcunov 	struct task_struct *task = get_proc_task(dentry->d_inode);
1770494f6ecSMiklos Szeredi 	int result = -ENOENT;
17899f89551SEric W. Biederman 
17999f89551SEric W. Biederman 	if (task) {
180f7ad3c6bSMiklos Szeredi 		task_lock(task);
181f7ad3c6bSMiklos Szeredi 		if (task->fs) {
182f7ad3c6bSMiklos Szeredi 			get_fs_pwd(task->fs, path);
183f7ad3c6bSMiklos Szeredi 			result = 0;
184f7ad3c6bSMiklos Szeredi 		}
185f7ad3c6bSMiklos Szeredi 		task_unlock(task);
18699f89551SEric W. Biederman 		put_task_struct(task);
18799f89551SEric W. Biederman 	}
1881da177e4SLinus Torvalds 	return result;
1891da177e4SLinus Torvalds }
1901da177e4SLinus Torvalds 
1917773fbc5SCyrill Gorcunov static int proc_root_link(struct dentry *dentry, struct path *path)
1921da177e4SLinus Torvalds {
1937773fbc5SCyrill Gorcunov 	struct task_struct *task = get_proc_task(dentry->d_inode);
1941da177e4SLinus Torvalds 	int result = -ENOENT;
19599f89551SEric W. Biederman 
19699f89551SEric W. Biederman 	if (task) {
197f7ad3c6bSMiklos Szeredi 		result = get_task_root(task, path);
19899f89551SEric W. Biederman 		put_task_struct(task);
19999f89551SEric W. Biederman 	}
2001da177e4SLinus Torvalds 	return result;
2011da177e4SLinus Torvalds }
2021da177e4SLinus Torvalds 
2031da177e4SLinus Torvalds static int proc_pid_cmdline(struct task_struct *task, char * buffer)
2041da177e4SLinus Torvalds {
2051da177e4SLinus Torvalds 	int res = 0;
2061da177e4SLinus Torvalds 	unsigned int len;
2071da177e4SLinus Torvalds 	struct mm_struct *mm = get_task_mm(task);
2081da177e4SLinus Torvalds 	if (!mm)
2091da177e4SLinus Torvalds 		goto out;
2101da177e4SLinus Torvalds 	if (!mm->arg_end)
2111da177e4SLinus Torvalds 		goto out_mm;	/* Shh! No looking before we're done */
2121da177e4SLinus Torvalds 
2131da177e4SLinus Torvalds  	len = mm->arg_end - mm->arg_start;
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds 	if (len > PAGE_SIZE)
2161da177e4SLinus Torvalds 		len = PAGE_SIZE;
2171da177e4SLinus Torvalds 
2181da177e4SLinus Torvalds 	res = access_process_vm(task, mm->arg_start, buffer, len, 0);
2191da177e4SLinus Torvalds 
2201da177e4SLinus Torvalds 	// If the nul at the end of args has been overwritten, then
2211da177e4SLinus Torvalds 	// assume application is using setproctitle(3).
2221da177e4SLinus Torvalds 	if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
2231da177e4SLinus Torvalds 		len = strnlen(buffer, res);
2241da177e4SLinus Torvalds 		if (len < res) {
2251da177e4SLinus Torvalds 		    res = len;
2261da177e4SLinus Torvalds 		} else {
2271da177e4SLinus Torvalds 			len = mm->env_end - mm->env_start;
2281da177e4SLinus Torvalds 			if (len > PAGE_SIZE - res)
2291da177e4SLinus Torvalds 				len = PAGE_SIZE - res;
2301da177e4SLinus Torvalds 			res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
2311da177e4SLinus Torvalds 			res = strnlen(buffer, res);
2321da177e4SLinus Torvalds 		}
2331da177e4SLinus Torvalds 	}
2341da177e4SLinus Torvalds out_mm:
2351da177e4SLinus Torvalds 	mmput(mm);
2361da177e4SLinus Torvalds out:
2371da177e4SLinus Torvalds 	return res;
2381da177e4SLinus Torvalds }
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds static int proc_pid_auxv(struct task_struct *task, char *buffer)
2411da177e4SLinus Torvalds {
242e7dcd999SCong Wang 	struct mm_struct *mm = mm_access(task, PTRACE_MODE_READ);
2432fadaef4SAl Viro 	int res = PTR_ERR(mm);
2442fadaef4SAl Viro 	if (mm && !IS_ERR(mm)) {
2451da177e4SLinus Torvalds 		unsigned int nwords = 0;
246dfe6b7d9SHannes Eder 		do {
2471da177e4SLinus Torvalds 			nwords += 2;
248dfe6b7d9SHannes Eder 		} while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
2491da177e4SLinus Torvalds 		res = nwords * sizeof(mm->saved_auxv[0]);
2501da177e4SLinus Torvalds 		if (res > PAGE_SIZE)
2511da177e4SLinus Torvalds 			res = PAGE_SIZE;
2521da177e4SLinus Torvalds 		memcpy(buffer, mm->saved_auxv, res);
2531da177e4SLinus Torvalds 		mmput(mm);
2541da177e4SLinus Torvalds 	}
2551da177e4SLinus Torvalds 	return res;
2561da177e4SLinus Torvalds }
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds 
2591da177e4SLinus Torvalds #ifdef CONFIG_KALLSYMS
2601da177e4SLinus Torvalds /*
2611da177e4SLinus Torvalds  * Provides a wchan file via kallsyms in a proper one-value-per-file format.
2621da177e4SLinus Torvalds  * Returns the resolved symbol.  If that fails, simply return the address.
2631da177e4SLinus Torvalds  */
2641da177e4SLinus Torvalds static int proc_pid_wchan(struct task_struct *task, char *buffer)
2651da177e4SLinus Torvalds {
266ffb45122SAlexey Dobriyan 	unsigned long wchan;
2679281aceaSTejun Heo 	char symname[KSYM_NAME_LEN];
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds 	wchan = get_wchan(task);
2701da177e4SLinus Torvalds 
2719d65cb4aSAlexey Dobriyan 	if (lookup_symbol_name(wchan, symname) < 0)
272f83ce3e6SJake Edge 		if (!ptrace_may_access(task, PTRACE_MODE_READ))
273f83ce3e6SJake Edge 			return 0;
274f83ce3e6SJake Edge 		else
2751da177e4SLinus Torvalds 			return sprintf(buffer, "%lu", wchan);
2769d65cb4aSAlexey Dobriyan 	else
2779d65cb4aSAlexey Dobriyan 		return sprintf(buffer, "%s", symname);
2781da177e4SLinus Torvalds }
2791da177e4SLinus Torvalds #endif /* CONFIG_KALLSYMS */
2801da177e4SLinus Torvalds 
281a9712bc1SAl Viro static int lock_trace(struct task_struct *task)
282a9712bc1SAl Viro {
283a9712bc1SAl Viro 	int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
284a9712bc1SAl Viro 	if (err)
285a9712bc1SAl Viro 		return err;
286a9712bc1SAl Viro 	if (!ptrace_may_access(task, PTRACE_MODE_ATTACH)) {
287a9712bc1SAl Viro 		mutex_unlock(&task->signal->cred_guard_mutex);
288a9712bc1SAl Viro 		return -EPERM;
289a9712bc1SAl Viro 	}
290a9712bc1SAl Viro 	return 0;
291a9712bc1SAl Viro }
292a9712bc1SAl Viro 
293a9712bc1SAl Viro static void unlock_trace(struct task_struct *task)
294a9712bc1SAl Viro {
295a9712bc1SAl Viro 	mutex_unlock(&task->signal->cred_guard_mutex);
296a9712bc1SAl Viro }
297a9712bc1SAl Viro 
2982ec220e2SKen Chen #ifdef CONFIG_STACKTRACE
2992ec220e2SKen Chen 
3002ec220e2SKen Chen #define MAX_STACK_TRACE_DEPTH	64
3012ec220e2SKen Chen 
3022ec220e2SKen Chen static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
3032ec220e2SKen Chen 			  struct pid *pid, struct task_struct *task)
3042ec220e2SKen Chen {
3052ec220e2SKen Chen 	struct stack_trace trace;
3062ec220e2SKen Chen 	unsigned long *entries;
307a9712bc1SAl Viro 	int err;
3082ec220e2SKen Chen 	int i;
3092ec220e2SKen Chen 
3102ec220e2SKen Chen 	entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
3112ec220e2SKen Chen 	if (!entries)
3122ec220e2SKen Chen 		return -ENOMEM;
3132ec220e2SKen Chen 
3142ec220e2SKen Chen 	trace.nr_entries	= 0;
3152ec220e2SKen Chen 	trace.max_entries	= MAX_STACK_TRACE_DEPTH;
3162ec220e2SKen Chen 	trace.entries		= entries;
3172ec220e2SKen Chen 	trace.skip		= 0;
318a9712bc1SAl Viro 
319a9712bc1SAl Viro 	err = lock_trace(task);
320a9712bc1SAl Viro 	if (!err) {
3212ec220e2SKen Chen 		save_stack_trace_tsk(task, &trace);
3222ec220e2SKen Chen 
3232ec220e2SKen Chen 		for (i = 0; i < trace.nr_entries; i++) {
32451e03149SKonstantin Khlebnikov 			seq_printf(m, "[<%pK>] %pS\n",
3252ec220e2SKen Chen 				   (void *)entries[i], (void *)entries[i]);
3262ec220e2SKen Chen 		}
327a9712bc1SAl Viro 		unlock_trace(task);
328a9712bc1SAl Viro 	}
3292ec220e2SKen Chen 	kfree(entries);
3302ec220e2SKen Chen 
331a9712bc1SAl Viro 	return err;
3322ec220e2SKen Chen }
3332ec220e2SKen Chen #endif
3342ec220e2SKen Chen 
3351da177e4SLinus Torvalds #ifdef CONFIG_SCHEDSTATS
3361da177e4SLinus Torvalds /*
3371da177e4SLinus Torvalds  * Provides /proc/PID/schedstat
3381da177e4SLinus Torvalds  */
3391da177e4SLinus Torvalds static int proc_pid_schedstat(struct task_struct *task, char *buffer)
3401da177e4SLinus Torvalds {
341172ba844SBalbir Singh 	return sprintf(buffer, "%llu %llu %lu\n",
342826e08b0SIngo Molnar 			(unsigned long long)task->se.sum_exec_runtime,
343826e08b0SIngo Molnar 			(unsigned long long)task->sched_info.run_delay,
3442d72376bSIngo Molnar 			task->sched_info.pcount);
3451da177e4SLinus Torvalds }
3461da177e4SLinus Torvalds #endif
3471da177e4SLinus Torvalds 
3489745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP
3499745512cSArjan van de Ven static int lstats_show_proc(struct seq_file *m, void *v)
3509745512cSArjan van de Ven {
3519745512cSArjan van de Ven 	int i;
35213d77c37SHiroshi Shimamoto 	struct inode *inode = m->private;
35313d77c37SHiroshi Shimamoto 	struct task_struct *task = get_proc_task(inode);
3549745512cSArjan van de Ven 
35513d77c37SHiroshi Shimamoto 	if (!task)
35613d77c37SHiroshi Shimamoto 		return -ESRCH;
35713d77c37SHiroshi Shimamoto 	seq_puts(m, "Latency Top version : v0.1\n");
3589745512cSArjan van de Ven 	for (i = 0; i < 32; i++) {
35934e49d4fSJoe Perches 		struct latency_record *lr = &task->latency_record[i];
36034e49d4fSJoe Perches 		if (lr->backtrace[0]) {
3619745512cSArjan van de Ven 			int q;
3629745512cSArjan van de Ven 			seq_printf(m, "%i %li %li",
36334e49d4fSJoe Perches 				   lr->count, lr->time, lr->max);
3649745512cSArjan van de Ven 			for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
36534e49d4fSJoe Perches 				unsigned long bt = lr->backtrace[q];
36634e49d4fSJoe Perches 				if (!bt)
3679745512cSArjan van de Ven 					break;
36834e49d4fSJoe Perches 				if (bt == ULONG_MAX)
3699745512cSArjan van de Ven 					break;
37034e49d4fSJoe Perches 				seq_printf(m, " %ps", (void *)bt);
3719745512cSArjan van de Ven 			}
3729d6de12fSAlexey Dobriyan 			seq_putc(m, '\n');
3739745512cSArjan van de Ven 		}
3749745512cSArjan van de Ven 
3759745512cSArjan van de Ven 	}
37613d77c37SHiroshi Shimamoto 	put_task_struct(task);
3779745512cSArjan van de Ven 	return 0;
3789745512cSArjan van de Ven }
3799745512cSArjan van de Ven 
3809745512cSArjan van de Ven static int lstats_open(struct inode *inode, struct file *file)
3819745512cSArjan van de Ven {
38213d77c37SHiroshi Shimamoto 	return single_open(file, lstats_show_proc, inode);
383d6643d12SHiroshi Shimamoto }
384d6643d12SHiroshi Shimamoto 
3859745512cSArjan van de Ven static ssize_t lstats_write(struct file *file, const char __user *buf,
3869745512cSArjan van de Ven 			    size_t count, loff_t *offs)
3879745512cSArjan van de Ven {
388496ad9aaSAl Viro 	struct task_struct *task = get_proc_task(file_inode(file));
3899745512cSArjan van de Ven 
39013d77c37SHiroshi Shimamoto 	if (!task)
39113d77c37SHiroshi Shimamoto 		return -ESRCH;
3929745512cSArjan van de Ven 	clear_all_latency_tracing(task);
39313d77c37SHiroshi Shimamoto 	put_task_struct(task);
3949745512cSArjan van de Ven 
3959745512cSArjan van de Ven 	return count;
3969745512cSArjan van de Ven }
3979745512cSArjan van de Ven 
3989745512cSArjan van de Ven static const struct file_operations proc_lstats_operations = {
3999745512cSArjan van de Ven 	.open		= lstats_open,
4009745512cSArjan van de Ven 	.read		= seq_read,
4019745512cSArjan van de Ven 	.write		= lstats_write,
4029745512cSArjan van de Ven 	.llseek		= seq_lseek,
40313d77c37SHiroshi Shimamoto 	.release	= single_release,
4049745512cSArjan van de Ven };
4059745512cSArjan van de Ven 
4069745512cSArjan van de Ven #endif
4079745512cSArjan van de Ven 
4088d8b97baSAl Viro #ifdef CONFIG_CGROUPS
4098d8b97baSAl Viro static int cgroup_open(struct inode *inode, struct file *file)
4108d8b97baSAl Viro {
4118d8b97baSAl Viro 	struct pid *pid = PROC_I(inode)->pid;
4128d8b97baSAl Viro 	return single_open(file, proc_cgroup_show, pid);
4138d8b97baSAl Viro }
4148d8b97baSAl Viro 
4158d8b97baSAl Viro static const struct file_operations proc_cgroup_operations = {
4168d8b97baSAl Viro 	.open		= cgroup_open,
4178d8b97baSAl Viro 	.read		= seq_read,
4188d8b97baSAl Viro 	.llseek		= seq_lseek,
4198d8b97baSAl Viro 	.release	= single_release,
4208d8b97baSAl Viro };
4218d8b97baSAl Viro #endif
4228d8b97baSAl Viro 
4238d8b97baSAl Viro #ifdef CONFIG_PROC_PID_CPUSET
4248d8b97baSAl Viro 
4258d8b97baSAl Viro static int cpuset_open(struct inode *inode, struct file *file)
4268d8b97baSAl Viro {
4278d8b97baSAl Viro 	struct pid *pid = PROC_I(inode)->pid;
4288d8b97baSAl Viro 	return single_open(file, proc_cpuset_show, pid);
4298d8b97baSAl Viro }
4308d8b97baSAl Viro 
4318d8b97baSAl Viro static const struct file_operations proc_cpuset_operations = {
4328d8b97baSAl Viro 	.open		= cpuset_open,
4338d8b97baSAl Viro 	.read		= seq_read,
4348d8b97baSAl Viro 	.llseek		= seq_lseek,
4358d8b97baSAl Viro 	.release	= single_release,
4368d8b97baSAl Viro };
4378d8b97baSAl Viro #endif
4388d8b97baSAl Viro 
4391da177e4SLinus Torvalds static int proc_oom_score(struct task_struct *task, char *buffer)
4401da177e4SLinus Torvalds {
441a7f638f9SDavid Rientjes 	unsigned long totalpages = totalram_pages + total_swap_pages;
442b95c35e7SOleg Nesterov 	unsigned long points = 0;
4431da177e4SLinus Torvalds 
44419c5d45aSAlexey Dobriyan 	read_lock(&tasklist_lock);
445b95c35e7SOleg Nesterov 	if (pid_alive(task))
446a7f638f9SDavid Rientjes 		points = oom_badness(task, NULL, NULL, totalpages) *
447a7f638f9SDavid Rientjes 						1000 / totalpages;
44819c5d45aSAlexey Dobriyan 	read_unlock(&tasklist_lock);
4491da177e4SLinus Torvalds 	return sprintf(buffer, "%lu\n", points);
4501da177e4SLinus Torvalds }
4511da177e4SLinus Torvalds 
452d85f50d5SNeil Horman struct limit_names {
453d85f50d5SNeil Horman 	char *name;
454d85f50d5SNeil Horman 	char *unit;
455d85f50d5SNeil Horman };
456d85f50d5SNeil Horman 
457d85f50d5SNeil Horman static const struct limit_names lnames[RLIM_NLIMITS] = {
458cff4edb5SKees Cook 	[RLIMIT_CPU] = {"Max cpu time", "seconds"},
459d85f50d5SNeil Horman 	[RLIMIT_FSIZE] = {"Max file size", "bytes"},
460d85f50d5SNeil Horman 	[RLIMIT_DATA] = {"Max data size", "bytes"},
461d85f50d5SNeil Horman 	[RLIMIT_STACK] = {"Max stack size", "bytes"},
462d85f50d5SNeil Horman 	[RLIMIT_CORE] = {"Max core file size", "bytes"},
463d85f50d5SNeil Horman 	[RLIMIT_RSS] = {"Max resident set", "bytes"},
464d85f50d5SNeil Horman 	[RLIMIT_NPROC] = {"Max processes", "processes"},
465d85f50d5SNeil Horman 	[RLIMIT_NOFILE] = {"Max open files", "files"},
466d85f50d5SNeil Horman 	[RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
467d85f50d5SNeil Horman 	[RLIMIT_AS] = {"Max address space", "bytes"},
468d85f50d5SNeil Horman 	[RLIMIT_LOCKS] = {"Max file locks", "locks"},
469d85f50d5SNeil Horman 	[RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
470d85f50d5SNeil Horman 	[RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
471d85f50d5SNeil Horman 	[RLIMIT_NICE] = {"Max nice priority", NULL},
472d85f50d5SNeil Horman 	[RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
4738808117cSEugene Teo 	[RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
474d85f50d5SNeil Horman };
475d85f50d5SNeil Horman 
476d85f50d5SNeil Horman /* Display limits for a process */
477d85f50d5SNeil Horman static int proc_pid_limits(struct task_struct *task, char *buffer)
478d85f50d5SNeil Horman {
479d85f50d5SNeil Horman 	unsigned int i;
480d85f50d5SNeil Horman 	int count = 0;
481d85f50d5SNeil Horman 	unsigned long flags;
482d85f50d5SNeil Horman 	char *bufptr = buffer;
483d85f50d5SNeil Horman 
484d85f50d5SNeil Horman 	struct rlimit rlim[RLIM_NLIMITS];
485d85f50d5SNeil Horman 
486a6bebbc8SLai Jiangshan 	if (!lock_task_sighand(task, &flags))
487d85f50d5SNeil Horman 		return 0;
488d85f50d5SNeil Horman 	memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
489d85f50d5SNeil Horman 	unlock_task_sighand(task, &flags);
490d85f50d5SNeil Horman 
491d85f50d5SNeil Horman 	/*
492d85f50d5SNeil Horman 	 * print the file header
493d85f50d5SNeil Horman 	 */
494d85f50d5SNeil Horman 	count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
495d85f50d5SNeil Horman 			"Limit", "Soft Limit", "Hard Limit", "Units");
496d85f50d5SNeil Horman 
497d85f50d5SNeil Horman 	for (i = 0; i < RLIM_NLIMITS; i++) {
498d85f50d5SNeil Horman 		if (rlim[i].rlim_cur == RLIM_INFINITY)
499d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-25s %-20s ",
500d85f50d5SNeil Horman 					 lnames[i].name, "unlimited");
501d85f50d5SNeil Horman 		else
502d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-25s %-20lu ",
503d85f50d5SNeil Horman 					 lnames[i].name, rlim[i].rlim_cur);
504d85f50d5SNeil Horman 
505d85f50d5SNeil Horman 		if (rlim[i].rlim_max == RLIM_INFINITY)
506d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-20s ", "unlimited");
507d85f50d5SNeil Horman 		else
508d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-20lu ",
509d85f50d5SNeil Horman 					 rlim[i].rlim_max);
510d85f50d5SNeil Horman 
511d85f50d5SNeil Horman 		if (lnames[i].unit)
512d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-10s\n",
513d85f50d5SNeil Horman 					 lnames[i].unit);
514d85f50d5SNeil Horman 		else
515d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "\n");
516d85f50d5SNeil Horman 	}
517d85f50d5SNeil Horman 
518d85f50d5SNeil Horman 	return count;
519d85f50d5SNeil Horman }
520d85f50d5SNeil Horman 
521ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
522ebcb6734SRoland McGrath static int proc_pid_syscall(struct task_struct *task, char *buffer)
523ebcb6734SRoland McGrath {
524ebcb6734SRoland McGrath 	long nr;
525ebcb6734SRoland McGrath 	unsigned long args[6], sp, pc;
526a9712bc1SAl Viro 	int res = lock_trace(task);
527a9712bc1SAl Viro 	if (res)
528a9712bc1SAl Viro 		return res;
529ebcb6734SRoland McGrath 
530ebcb6734SRoland McGrath 	if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
531a9712bc1SAl Viro 		res = sprintf(buffer, "running\n");
532a9712bc1SAl Viro 	else if (nr < 0)
533a9712bc1SAl Viro 		res = sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
534a9712bc1SAl Viro 	else
535a9712bc1SAl Viro 		res = sprintf(buffer,
536ebcb6734SRoland McGrath 		       "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
537ebcb6734SRoland McGrath 		       nr,
538ebcb6734SRoland McGrath 		       args[0], args[1], args[2], args[3], args[4], args[5],
539ebcb6734SRoland McGrath 		       sp, pc);
540a9712bc1SAl Viro 	unlock_trace(task);
541a9712bc1SAl Viro 	return res;
542ebcb6734SRoland McGrath }
543ebcb6734SRoland McGrath #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
544ebcb6734SRoland McGrath 
5451da177e4SLinus Torvalds /************************************************************************/
5461da177e4SLinus Torvalds /*                       Here the fs part begins                        */
5471da177e4SLinus Torvalds /************************************************************************/
5481da177e4SLinus Torvalds 
5491da177e4SLinus Torvalds /* permission checks */
550778c1144SEric W. Biederman static int proc_fd_access_allowed(struct inode *inode)
5511da177e4SLinus Torvalds {
552778c1144SEric W. Biederman 	struct task_struct *task;
553778c1144SEric W. Biederman 	int allowed = 0;
554df26c40eSEric W. Biederman 	/* Allow access to a task's file descriptors if it is us or we
555df26c40eSEric W. Biederman 	 * may use ptrace attach to the process and find out that
556df26c40eSEric W. Biederman 	 * information.
557778c1144SEric W. Biederman 	 */
558778c1144SEric W. Biederman 	task = get_proc_task(inode);
559df26c40eSEric W. Biederman 	if (task) {
560006ebb40SStephen Smalley 		allowed = ptrace_may_access(task, PTRACE_MODE_READ);
561778c1144SEric W. Biederman 		put_task_struct(task);
562df26c40eSEric W. Biederman 	}
563778c1144SEric W. Biederman 	return allowed;
5641da177e4SLinus Torvalds }
5651da177e4SLinus Torvalds 
5666b4e306aSEric W. Biederman int proc_setattr(struct dentry *dentry, struct iattr *attr)
5676d76fa58SLinus Torvalds {
5686d76fa58SLinus Torvalds 	int error;
5696d76fa58SLinus Torvalds 	struct inode *inode = dentry->d_inode;
5706d76fa58SLinus Torvalds 
5716d76fa58SLinus Torvalds 	if (attr->ia_valid & ATTR_MODE)
5726d76fa58SLinus Torvalds 		return -EPERM;
5736d76fa58SLinus Torvalds 
5746d76fa58SLinus Torvalds 	error = inode_change_ok(inode, attr);
5751025774cSChristoph Hellwig 	if (error)
5766d76fa58SLinus Torvalds 		return error;
5771025774cSChristoph Hellwig 
5781025774cSChristoph Hellwig 	setattr_copy(inode, attr);
5791025774cSChristoph Hellwig 	mark_inode_dirty(inode);
5801025774cSChristoph Hellwig 	return 0;
5816d76fa58SLinus Torvalds }
5826d76fa58SLinus Torvalds 
5830499680aSVasiliy Kulikov /*
5840499680aSVasiliy Kulikov  * May current process learn task's sched/cmdline info (for hide_pid_min=1)
5850499680aSVasiliy Kulikov  * or euid/egid (for hide_pid_min=2)?
5860499680aSVasiliy Kulikov  */
5870499680aSVasiliy Kulikov static bool has_pid_permissions(struct pid_namespace *pid,
5880499680aSVasiliy Kulikov 				 struct task_struct *task,
5890499680aSVasiliy Kulikov 				 int hide_pid_min)
5900499680aSVasiliy Kulikov {
5910499680aSVasiliy Kulikov 	if (pid->hide_pid < hide_pid_min)
5920499680aSVasiliy Kulikov 		return true;
5930499680aSVasiliy Kulikov 	if (in_group_p(pid->pid_gid))
5940499680aSVasiliy Kulikov 		return true;
5950499680aSVasiliy Kulikov 	return ptrace_may_access(task, PTRACE_MODE_READ);
5960499680aSVasiliy Kulikov }
5970499680aSVasiliy Kulikov 
5980499680aSVasiliy Kulikov 
5990499680aSVasiliy Kulikov static int proc_pid_permission(struct inode *inode, int mask)
6000499680aSVasiliy Kulikov {
6010499680aSVasiliy Kulikov 	struct pid_namespace *pid = inode->i_sb->s_fs_info;
6020499680aSVasiliy Kulikov 	struct task_struct *task;
6030499680aSVasiliy Kulikov 	bool has_perms;
6040499680aSVasiliy Kulikov 
6050499680aSVasiliy Kulikov 	task = get_proc_task(inode);
606a2ef990aSXiaotian Feng 	if (!task)
607a2ef990aSXiaotian Feng 		return -ESRCH;
6080499680aSVasiliy Kulikov 	has_perms = has_pid_permissions(pid, task, 1);
6090499680aSVasiliy Kulikov 	put_task_struct(task);
6100499680aSVasiliy Kulikov 
6110499680aSVasiliy Kulikov 	if (!has_perms) {
6120499680aSVasiliy Kulikov 		if (pid->hide_pid == 2) {
6130499680aSVasiliy Kulikov 			/*
6140499680aSVasiliy Kulikov 			 * Let's make getdents(), stat(), and open()
6150499680aSVasiliy Kulikov 			 * consistent with each other.  If a process
6160499680aSVasiliy Kulikov 			 * may not stat() a file, it shouldn't be seen
6170499680aSVasiliy Kulikov 			 * in procfs at all.
6180499680aSVasiliy Kulikov 			 */
6190499680aSVasiliy Kulikov 			return -ENOENT;
6200499680aSVasiliy Kulikov 		}
6210499680aSVasiliy Kulikov 
6220499680aSVasiliy Kulikov 		return -EPERM;
6230499680aSVasiliy Kulikov 	}
6240499680aSVasiliy Kulikov 	return generic_permission(inode, mask);
6250499680aSVasiliy Kulikov }
6260499680aSVasiliy Kulikov 
6270499680aSVasiliy Kulikov 
6280499680aSVasiliy Kulikov 
629c5ef1c42SArjan van de Ven static const struct inode_operations proc_def_inode_operations = {
6306d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
6316d76fa58SLinus Torvalds };
6326d76fa58SLinus Torvalds 
6331da177e4SLinus Torvalds #define PROC_BLOCK_SIZE	(3*1024)		/* 4K page size but our output routines use some slack for overruns */
6341da177e4SLinus Torvalds 
6351da177e4SLinus Torvalds static ssize_t proc_info_read(struct file * file, char __user * buf,
6361da177e4SLinus Torvalds 			  size_t count, loff_t *ppos)
6371da177e4SLinus Torvalds {
638496ad9aaSAl Viro 	struct inode * inode = file_inode(file);
6391da177e4SLinus Torvalds 	unsigned long page;
6401da177e4SLinus Torvalds 	ssize_t length;
64199f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
64299f89551SEric W. Biederman 
64399f89551SEric W. Biederman 	length = -ESRCH;
64499f89551SEric W. Biederman 	if (!task)
64599f89551SEric W. Biederman 		goto out_no_task;
6461da177e4SLinus Torvalds 
6471da177e4SLinus Torvalds 	if (count > PROC_BLOCK_SIZE)
6481da177e4SLinus Torvalds 		count = PROC_BLOCK_SIZE;
64999f89551SEric W. Biederman 
65099f89551SEric W. Biederman 	length = -ENOMEM;
651e12ba74dSMel Gorman 	if (!(page = __get_free_page(GFP_TEMPORARY)))
65299f89551SEric W. Biederman 		goto out;
6531da177e4SLinus Torvalds 
6541da177e4SLinus Torvalds 	length = PROC_I(inode)->op.proc_read(task, (char*)page);
6551da177e4SLinus Torvalds 
6561da177e4SLinus Torvalds 	if (length >= 0)
6571da177e4SLinus Torvalds 		length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
6581da177e4SLinus Torvalds 	free_page(page);
65999f89551SEric W. Biederman out:
66099f89551SEric W. Biederman 	put_task_struct(task);
66199f89551SEric W. Biederman out_no_task:
6621da177e4SLinus Torvalds 	return length;
6631da177e4SLinus Torvalds }
6641da177e4SLinus Torvalds 
66500977a59SArjan van de Ven static const struct file_operations proc_info_file_operations = {
6661da177e4SLinus Torvalds 	.read		= proc_info_read,
66787df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
6681da177e4SLinus Torvalds };
6691da177e4SLinus Torvalds 
670be614086SEric W. Biederman static int proc_single_show(struct seq_file *m, void *v)
671be614086SEric W. Biederman {
672be614086SEric W. Biederman 	struct inode *inode = m->private;
673be614086SEric W. Biederman 	struct pid_namespace *ns;
674be614086SEric W. Biederman 	struct pid *pid;
675be614086SEric W. Biederman 	struct task_struct *task;
676be614086SEric W. Biederman 	int ret;
677be614086SEric W. Biederman 
678be614086SEric W. Biederman 	ns = inode->i_sb->s_fs_info;
679be614086SEric W. Biederman 	pid = proc_pid(inode);
680be614086SEric W. Biederman 	task = get_pid_task(pid, PIDTYPE_PID);
681be614086SEric W. Biederman 	if (!task)
682be614086SEric W. Biederman 		return -ESRCH;
683be614086SEric W. Biederman 
684be614086SEric W. Biederman 	ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
685be614086SEric W. Biederman 
686be614086SEric W. Biederman 	put_task_struct(task);
687be614086SEric W. Biederman 	return ret;
688be614086SEric W. Biederman }
689be614086SEric W. Biederman 
690be614086SEric W. Biederman static int proc_single_open(struct inode *inode, struct file *filp)
691be614086SEric W. Biederman {
692c6a34058SJovi Zhang 	return single_open(filp, proc_single_show, inode);
693be614086SEric W. Biederman }
694be614086SEric W. Biederman 
695be614086SEric W. Biederman static const struct file_operations proc_single_file_operations = {
696be614086SEric W. Biederman 	.open		= proc_single_open,
697be614086SEric W. Biederman 	.read		= seq_read,
698be614086SEric W. Biederman 	.llseek		= seq_lseek,
699be614086SEric W. Biederman 	.release	= single_release,
700be614086SEric W. Biederman };
701be614086SEric W. Biederman 
702b409e578SCong Wang static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
7031da177e4SLinus Torvalds {
704496ad9aaSAl Viro 	struct task_struct *task = get_proc_task(file_inode(file));
705e268337dSLinus Torvalds 	struct mm_struct *mm;
706e268337dSLinus Torvalds 
707e268337dSLinus Torvalds 	if (!task)
708e268337dSLinus Torvalds 		return -ESRCH;
709e268337dSLinus Torvalds 
710b409e578SCong Wang 	mm = mm_access(task, mode);
711e268337dSLinus Torvalds 	put_task_struct(task);
712e268337dSLinus Torvalds 
713e268337dSLinus Torvalds 	if (IS_ERR(mm))
714e268337dSLinus Torvalds 		return PTR_ERR(mm);
715e268337dSLinus Torvalds 
7166d08f2c7SOleg Nesterov 	if (mm) {
7176d08f2c7SOleg Nesterov 		/* ensure this mm_struct can't be freed */
7186d08f2c7SOleg Nesterov 		atomic_inc(&mm->mm_count);
7196d08f2c7SOleg Nesterov 		/* but do not pin its memory */
7206d08f2c7SOleg Nesterov 		mmput(mm);
7216d08f2c7SOleg Nesterov 	}
7226d08f2c7SOleg Nesterov 
723e268337dSLinus Torvalds 	file->private_data = mm;
724e268337dSLinus Torvalds 
7251da177e4SLinus Torvalds 	return 0;
7261da177e4SLinus Torvalds }
7271da177e4SLinus Torvalds 
728b409e578SCong Wang static int mem_open(struct inode *inode, struct file *file)
729b409e578SCong Wang {
730bc452b4bSDjalal Harouni 	int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
731bc452b4bSDjalal Harouni 
732bc452b4bSDjalal Harouni 	/* OK to pass negative loff_t, we can catch out-of-range */
733bc452b4bSDjalal Harouni 	file->f_mode |= FMODE_UNSIGNED_OFFSET;
734bc452b4bSDjalal Harouni 
735bc452b4bSDjalal Harouni 	return ret;
736b409e578SCong Wang }
737b409e578SCong Wang 
738572d34b9SOleg Nesterov static ssize_t mem_rw(struct file *file, char __user *buf,
739572d34b9SOleg Nesterov 			size_t count, loff_t *ppos, int write)
7401da177e4SLinus Torvalds {
741e268337dSLinus Torvalds 	struct mm_struct *mm = file->private_data;
742572d34b9SOleg Nesterov 	unsigned long addr = *ppos;
743572d34b9SOleg Nesterov 	ssize_t copied;
7441da177e4SLinus Torvalds 	char *page;
7451da177e4SLinus Torvalds 
746e268337dSLinus Torvalds 	if (!mm)
747e268337dSLinus Torvalds 		return 0;
7481da177e4SLinus Torvalds 
749e12ba74dSMel Gorman 	page = (char *)__get_free_page(GFP_TEMPORARY);
7501da177e4SLinus Torvalds 	if (!page)
751e268337dSLinus Torvalds 		return -ENOMEM;
7521da177e4SLinus Torvalds 
753f7ca54f4SFrederik Deweerdt 	copied = 0;
7546d08f2c7SOleg Nesterov 	if (!atomic_inc_not_zero(&mm->mm_users))
7556d08f2c7SOleg Nesterov 		goto free;
7566d08f2c7SOleg Nesterov 
7571da177e4SLinus Torvalds 	while (count > 0) {
758572d34b9SOleg Nesterov 		int this_len = min_t(int, count, PAGE_SIZE);
7591da177e4SLinus Torvalds 
760572d34b9SOleg Nesterov 		if (write && copy_from_user(page, buf, this_len)) {
7611da177e4SLinus Torvalds 			copied = -EFAULT;
7621da177e4SLinus Torvalds 			break;
7631da177e4SLinus Torvalds 		}
764572d34b9SOleg Nesterov 
765572d34b9SOleg Nesterov 		this_len = access_remote_vm(mm, addr, page, this_len, write);
766572d34b9SOleg Nesterov 		if (!this_len) {
7671da177e4SLinus Torvalds 			if (!copied)
7681da177e4SLinus Torvalds 				copied = -EIO;
7691da177e4SLinus Torvalds 			break;
7701da177e4SLinus Torvalds 		}
771572d34b9SOleg Nesterov 
772572d34b9SOleg Nesterov 		if (!write && copy_to_user(buf, page, this_len)) {
773572d34b9SOleg Nesterov 			copied = -EFAULT;
774572d34b9SOleg Nesterov 			break;
7751da177e4SLinus Torvalds 		}
776572d34b9SOleg Nesterov 
777572d34b9SOleg Nesterov 		buf += this_len;
778572d34b9SOleg Nesterov 		addr += this_len;
779572d34b9SOleg Nesterov 		copied += this_len;
780572d34b9SOleg Nesterov 		count -= this_len;
781572d34b9SOleg Nesterov 	}
782572d34b9SOleg Nesterov 	*ppos = addr;
78330cd8903SKOSAKI Motohiro 
7846d08f2c7SOleg Nesterov 	mmput(mm);
7856d08f2c7SOleg Nesterov free:
78630cd8903SKOSAKI Motohiro 	free_page((unsigned long) page);
7871da177e4SLinus Torvalds 	return copied;
7881da177e4SLinus Torvalds }
7891da177e4SLinus Torvalds 
790572d34b9SOleg Nesterov static ssize_t mem_read(struct file *file, char __user *buf,
791572d34b9SOleg Nesterov 			size_t count, loff_t *ppos)
792572d34b9SOleg Nesterov {
793572d34b9SOleg Nesterov 	return mem_rw(file, buf, count, ppos, 0);
794572d34b9SOleg Nesterov }
795572d34b9SOleg Nesterov 
796572d34b9SOleg Nesterov static ssize_t mem_write(struct file *file, const char __user *buf,
797572d34b9SOleg Nesterov 			 size_t count, loff_t *ppos)
798572d34b9SOleg Nesterov {
799572d34b9SOleg Nesterov 	return mem_rw(file, (char __user*)buf, count, ppos, 1);
800572d34b9SOleg Nesterov }
801572d34b9SOleg Nesterov 
80285863e47SMatt Mackall loff_t mem_lseek(struct file *file, loff_t offset, int orig)
8031da177e4SLinus Torvalds {
8041da177e4SLinus Torvalds 	switch (orig) {
8051da177e4SLinus Torvalds 	case 0:
8061da177e4SLinus Torvalds 		file->f_pos = offset;
8071da177e4SLinus Torvalds 		break;
8081da177e4SLinus Torvalds 	case 1:
8091da177e4SLinus Torvalds 		file->f_pos += offset;
8101da177e4SLinus Torvalds 		break;
8111da177e4SLinus Torvalds 	default:
8121da177e4SLinus Torvalds 		return -EINVAL;
8131da177e4SLinus Torvalds 	}
8141da177e4SLinus Torvalds 	force_successful_syscall_return();
8151da177e4SLinus Torvalds 	return file->f_pos;
8161da177e4SLinus Torvalds }
8171da177e4SLinus Torvalds 
818e268337dSLinus Torvalds static int mem_release(struct inode *inode, struct file *file)
819e268337dSLinus Torvalds {
820e268337dSLinus Torvalds 	struct mm_struct *mm = file->private_data;
82171879d3cSOleg Nesterov 	if (mm)
8226d08f2c7SOleg Nesterov 		mmdrop(mm);
823e268337dSLinus Torvalds 	return 0;
824e268337dSLinus Torvalds }
825e268337dSLinus Torvalds 
82600977a59SArjan van de Ven static const struct file_operations proc_mem_operations = {
8271da177e4SLinus Torvalds 	.llseek		= mem_lseek,
8281da177e4SLinus Torvalds 	.read		= mem_read,
8291da177e4SLinus Torvalds 	.write		= mem_write,
8301da177e4SLinus Torvalds 	.open		= mem_open,
831e268337dSLinus Torvalds 	.release	= mem_release,
8321da177e4SLinus Torvalds };
8331da177e4SLinus Torvalds 
834b409e578SCong Wang static int environ_open(struct inode *inode, struct file *file)
835b409e578SCong Wang {
836b409e578SCong Wang 	return __mem_open(inode, file, PTRACE_MODE_READ);
837b409e578SCong Wang }
838b409e578SCong Wang 
839315e28c8SJames Pearson static ssize_t environ_read(struct file *file, char __user *buf,
840315e28c8SJames Pearson 			size_t count, loff_t *ppos)
841315e28c8SJames Pearson {
842315e28c8SJames Pearson 	char *page;
843315e28c8SJames Pearson 	unsigned long src = *ppos;
844b409e578SCong Wang 	int ret = 0;
845b409e578SCong Wang 	struct mm_struct *mm = file->private_data;
846315e28c8SJames Pearson 
847b409e578SCong Wang 	if (!mm)
848b409e578SCong Wang 		return 0;
849315e28c8SJames Pearson 
850315e28c8SJames Pearson 	page = (char *)__get_free_page(GFP_TEMPORARY);
851315e28c8SJames Pearson 	if (!page)
852b409e578SCong Wang 		return -ENOMEM;
853315e28c8SJames Pearson 
854d6f64b89SAl Viro 	ret = 0;
855b409e578SCong Wang 	if (!atomic_inc_not_zero(&mm->mm_users))
856b409e578SCong Wang 		goto free;
857315e28c8SJames Pearson 	while (count > 0) {
858e8905ec2SDjalal Harouni 		size_t this_len, max_len;
859e8905ec2SDjalal Harouni 		int retval;
860e8905ec2SDjalal Harouni 
861e8905ec2SDjalal Harouni 		if (src >= (mm->env_end - mm->env_start))
862e8905ec2SDjalal Harouni 			break;
863315e28c8SJames Pearson 
864315e28c8SJames Pearson 		this_len = mm->env_end - (mm->env_start + src);
865315e28c8SJames Pearson 
866e8905ec2SDjalal Harouni 		max_len = min_t(size_t, PAGE_SIZE, count);
867e8905ec2SDjalal Harouni 		this_len = min(max_len, this_len);
868315e28c8SJames Pearson 
869b409e578SCong Wang 		retval = access_remote_vm(mm, (mm->env_start + src),
870315e28c8SJames Pearson 			page, this_len, 0);
871315e28c8SJames Pearson 
872315e28c8SJames Pearson 		if (retval <= 0) {
873315e28c8SJames Pearson 			ret = retval;
874315e28c8SJames Pearson 			break;
875315e28c8SJames Pearson 		}
876315e28c8SJames Pearson 
877315e28c8SJames Pearson 		if (copy_to_user(buf, page, retval)) {
878315e28c8SJames Pearson 			ret = -EFAULT;
879315e28c8SJames Pearson 			break;
880315e28c8SJames Pearson 		}
881315e28c8SJames Pearson 
882315e28c8SJames Pearson 		ret += retval;
883315e28c8SJames Pearson 		src += retval;
884315e28c8SJames Pearson 		buf += retval;
885315e28c8SJames Pearson 		count -= retval;
886315e28c8SJames Pearson 	}
887315e28c8SJames Pearson 	*ppos = src;
888315e28c8SJames Pearson 	mmput(mm);
889b409e578SCong Wang 
890b409e578SCong Wang free:
891315e28c8SJames Pearson 	free_page((unsigned long) page);
892315e28c8SJames Pearson 	return ret;
893315e28c8SJames Pearson }
894315e28c8SJames Pearson 
895315e28c8SJames Pearson static const struct file_operations proc_environ_operations = {
896b409e578SCong Wang 	.open		= environ_open,
897315e28c8SJames Pearson 	.read		= environ_read,
89887df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
899b409e578SCong Wang 	.release	= mem_release,
900315e28c8SJames Pearson };
901315e28c8SJames Pearson 
902fa0cbbf1SDavid Rientjes static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
903fa0cbbf1SDavid Rientjes 			    loff_t *ppos)
904fa0cbbf1SDavid Rientjes {
905496ad9aaSAl Viro 	struct task_struct *task = get_proc_task(file_inode(file));
906fa0cbbf1SDavid Rientjes 	char buffer[PROC_NUMBUF];
907fa0cbbf1SDavid Rientjes 	int oom_adj = OOM_ADJUST_MIN;
908fa0cbbf1SDavid Rientjes 	size_t len;
909fa0cbbf1SDavid Rientjes 	unsigned long flags;
910fa0cbbf1SDavid Rientjes 
911fa0cbbf1SDavid Rientjes 	if (!task)
912fa0cbbf1SDavid Rientjes 		return -ESRCH;
913fa0cbbf1SDavid Rientjes 	if (lock_task_sighand(task, &flags)) {
914fa0cbbf1SDavid Rientjes 		if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
915fa0cbbf1SDavid Rientjes 			oom_adj = OOM_ADJUST_MAX;
916fa0cbbf1SDavid Rientjes 		else
917fa0cbbf1SDavid Rientjes 			oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
918fa0cbbf1SDavid Rientjes 				  OOM_SCORE_ADJ_MAX;
919fa0cbbf1SDavid Rientjes 		unlock_task_sighand(task, &flags);
920fa0cbbf1SDavid Rientjes 	}
921fa0cbbf1SDavid Rientjes 	put_task_struct(task);
922fa0cbbf1SDavid Rientjes 	len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
923fa0cbbf1SDavid Rientjes 	return simple_read_from_buffer(buf, count, ppos, buffer, len);
924fa0cbbf1SDavid Rientjes }
925fa0cbbf1SDavid Rientjes 
926fa0cbbf1SDavid Rientjes static ssize_t oom_adj_write(struct file *file, const char __user *buf,
927fa0cbbf1SDavid Rientjes 			     size_t count, loff_t *ppos)
928fa0cbbf1SDavid Rientjes {
929fa0cbbf1SDavid Rientjes 	struct task_struct *task;
930fa0cbbf1SDavid Rientjes 	char buffer[PROC_NUMBUF];
931fa0cbbf1SDavid Rientjes 	int oom_adj;
932fa0cbbf1SDavid Rientjes 	unsigned long flags;
933fa0cbbf1SDavid Rientjes 	int err;
934fa0cbbf1SDavid Rientjes 
935fa0cbbf1SDavid Rientjes 	memset(buffer, 0, sizeof(buffer));
936fa0cbbf1SDavid Rientjes 	if (count > sizeof(buffer) - 1)
937fa0cbbf1SDavid Rientjes 		count = sizeof(buffer) - 1;
938fa0cbbf1SDavid Rientjes 	if (copy_from_user(buffer, buf, count)) {
939fa0cbbf1SDavid Rientjes 		err = -EFAULT;
940fa0cbbf1SDavid Rientjes 		goto out;
941fa0cbbf1SDavid Rientjes 	}
942fa0cbbf1SDavid Rientjes 
943fa0cbbf1SDavid Rientjes 	err = kstrtoint(strstrip(buffer), 0, &oom_adj);
944fa0cbbf1SDavid Rientjes 	if (err)
945fa0cbbf1SDavid Rientjes 		goto out;
946fa0cbbf1SDavid Rientjes 	if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
947fa0cbbf1SDavid Rientjes 	     oom_adj != OOM_DISABLE) {
948fa0cbbf1SDavid Rientjes 		err = -EINVAL;
949fa0cbbf1SDavid Rientjes 		goto out;
950fa0cbbf1SDavid Rientjes 	}
951fa0cbbf1SDavid Rientjes 
952496ad9aaSAl Viro 	task = get_proc_task(file_inode(file));
953fa0cbbf1SDavid Rientjes 	if (!task) {
954fa0cbbf1SDavid Rientjes 		err = -ESRCH;
955fa0cbbf1SDavid Rientjes 		goto out;
956fa0cbbf1SDavid Rientjes 	}
957fa0cbbf1SDavid Rientjes 
958fa0cbbf1SDavid Rientjes 	task_lock(task);
959fa0cbbf1SDavid Rientjes 	if (!task->mm) {
960fa0cbbf1SDavid Rientjes 		err = -EINVAL;
961fa0cbbf1SDavid Rientjes 		goto err_task_lock;
962fa0cbbf1SDavid Rientjes 	}
963fa0cbbf1SDavid Rientjes 
964fa0cbbf1SDavid Rientjes 	if (!lock_task_sighand(task, &flags)) {
965fa0cbbf1SDavid Rientjes 		err = -ESRCH;
966fa0cbbf1SDavid Rientjes 		goto err_task_lock;
967fa0cbbf1SDavid Rientjes 	}
968fa0cbbf1SDavid Rientjes 
969fa0cbbf1SDavid Rientjes 	/*
970fa0cbbf1SDavid Rientjes 	 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
971fa0cbbf1SDavid Rientjes 	 * value is always attainable.
972fa0cbbf1SDavid Rientjes 	 */
973fa0cbbf1SDavid Rientjes 	if (oom_adj == OOM_ADJUST_MAX)
974fa0cbbf1SDavid Rientjes 		oom_adj = OOM_SCORE_ADJ_MAX;
975fa0cbbf1SDavid Rientjes 	else
976fa0cbbf1SDavid Rientjes 		oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
977fa0cbbf1SDavid Rientjes 
978fa0cbbf1SDavid Rientjes 	if (oom_adj < task->signal->oom_score_adj &&
979fa0cbbf1SDavid Rientjes 	    !capable(CAP_SYS_RESOURCE)) {
980fa0cbbf1SDavid Rientjes 		err = -EACCES;
981fa0cbbf1SDavid Rientjes 		goto err_sighand;
982fa0cbbf1SDavid Rientjes 	}
983fa0cbbf1SDavid Rientjes 
984fa0cbbf1SDavid Rientjes 	/*
985fa0cbbf1SDavid Rientjes 	 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
986fa0cbbf1SDavid Rientjes 	 * /proc/pid/oom_score_adj instead.
987fa0cbbf1SDavid Rientjes 	 */
98887ebdc00SAndrew Morton 	pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
989fa0cbbf1SDavid Rientjes 		  current->comm, task_pid_nr(current), task_pid_nr(task),
990fa0cbbf1SDavid Rientjes 		  task_pid_nr(task));
991fa0cbbf1SDavid Rientjes 
992fa0cbbf1SDavid Rientjes 	task->signal->oom_score_adj = oom_adj;
993fa0cbbf1SDavid Rientjes 	trace_oom_score_adj_update(task);
994fa0cbbf1SDavid Rientjes err_sighand:
995fa0cbbf1SDavid Rientjes 	unlock_task_sighand(task, &flags);
996fa0cbbf1SDavid Rientjes err_task_lock:
997fa0cbbf1SDavid Rientjes 	task_unlock(task);
998fa0cbbf1SDavid Rientjes 	put_task_struct(task);
999fa0cbbf1SDavid Rientjes out:
1000fa0cbbf1SDavid Rientjes 	return err < 0 ? err : count;
1001fa0cbbf1SDavid Rientjes }
1002fa0cbbf1SDavid Rientjes 
1003fa0cbbf1SDavid Rientjes static const struct file_operations proc_oom_adj_operations = {
1004fa0cbbf1SDavid Rientjes 	.read		= oom_adj_read,
1005fa0cbbf1SDavid Rientjes 	.write		= oom_adj_write,
1006fa0cbbf1SDavid Rientjes 	.llseek		= generic_file_llseek,
1007fa0cbbf1SDavid Rientjes };
1008fa0cbbf1SDavid Rientjes 
1009a63d83f4SDavid Rientjes static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1010a63d83f4SDavid Rientjes 					size_t count, loff_t *ppos)
1011a63d83f4SDavid Rientjes {
1012496ad9aaSAl Viro 	struct task_struct *task = get_proc_task(file_inode(file));
1013a63d83f4SDavid Rientjes 	char buffer[PROC_NUMBUF];
1014a9c58b90SDavid Rientjes 	short oom_score_adj = OOM_SCORE_ADJ_MIN;
1015a63d83f4SDavid Rientjes 	unsigned long flags;
1016a63d83f4SDavid Rientjes 	size_t len;
1017a63d83f4SDavid Rientjes 
1018a63d83f4SDavid Rientjes 	if (!task)
1019a63d83f4SDavid Rientjes 		return -ESRCH;
1020a63d83f4SDavid Rientjes 	if (lock_task_sighand(task, &flags)) {
1021a63d83f4SDavid Rientjes 		oom_score_adj = task->signal->oom_score_adj;
1022a63d83f4SDavid Rientjes 		unlock_task_sighand(task, &flags);
1023a63d83f4SDavid Rientjes 	}
1024a63d83f4SDavid Rientjes 	put_task_struct(task);
1025a9c58b90SDavid Rientjes 	len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
1026a63d83f4SDavid Rientjes 	return simple_read_from_buffer(buf, count, ppos, buffer, len);
1027a63d83f4SDavid Rientjes }
1028a63d83f4SDavid Rientjes 
1029a63d83f4SDavid Rientjes static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1030a63d83f4SDavid Rientjes 					size_t count, loff_t *ppos)
1031a63d83f4SDavid Rientjes {
1032a63d83f4SDavid Rientjes 	struct task_struct *task;
1033a63d83f4SDavid Rientjes 	char buffer[PROC_NUMBUF];
1034a63d83f4SDavid Rientjes 	unsigned long flags;
10350a8cb8e3SAlexey Dobriyan 	int oom_score_adj;
1036a63d83f4SDavid Rientjes 	int err;
1037a63d83f4SDavid Rientjes 
1038a63d83f4SDavid Rientjes 	memset(buffer, 0, sizeof(buffer));
1039a63d83f4SDavid Rientjes 	if (count > sizeof(buffer) - 1)
1040a63d83f4SDavid Rientjes 		count = sizeof(buffer) - 1;
1041723548bfSDavid Rientjes 	if (copy_from_user(buffer, buf, count)) {
1042723548bfSDavid Rientjes 		err = -EFAULT;
1043723548bfSDavid Rientjes 		goto out;
1044723548bfSDavid Rientjes 	}
1045a63d83f4SDavid Rientjes 
10460a8cb8e3SAlexey Dobriyan 	err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
1047a63d83f4SDavid Rientjes 	if (err)
1048723548bfSDavid Rientjes 		goto out;
1049a63d83f4SDavid Rientjes 	if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
1050723548bfSDavid Rientjes 			oom_score_adj > OOM_SCORE_ADJ_MAX) {
1051723548bfSDavid Rientjes 		err = -EINVAL;
1052723548bfSDavid Rientjes 		goto out;
1053723548bfSDavid Rientjes 	}
1054a63d83f4SDavid Rientjes 
1055496ad9aaSAl Viro 	task = get_proc_task(file_inode(file));
1056723548bfSDavid Rientjes 	if (!task) {
1057723548bfSDavid Rientjes 		err = -ESRCH;
1058723548bfSDavid Rientjes 		goto out;
1059723548bfSDavid Rientjes 	}
1060a63d83f4SDavid Rientjes 
10613d5992d2SYing Han 	task_lock(task);
10623d5992d2SYing Han 	if (!task->mm) {
1063723548bfSDavid Rientjes 		err = -EINVAL;
1064723548bfSDavid Rientjes 		goto err_task_lock;
10653d5992d2SYing Han 	}
1066d19d5476SDavid Rientjes 
1067d19d5476SDavid Rientjes 	if (!lock_task_sighand(task, &flags)) {
1068d19d5476SDavid Rientjes 		err = -ESRCH;
1069d19d5476SDavid Rientjes 		goto err_task_lock;
1070d19d5476SDavid Rientjes 	}
1071d19d5476SDavid Rientjes 
1072a9c58b90SDavid Rientjes 	if ((short)oom_score_adj < task->signal->oom_score_adj_min &&
1073d19d5476SDavid Rientjes 			!capable(CAP_SYS_RESOURCE)) {
1074d19d5476SDavid Rientjes 		err = -EACCES;
1075d19d5476SDavid Rientjes 		goto err_sighand;
1076d19d5476SDavid Rientjes 	}
1077d19d5476SDavid Rientjes 
1078a9c58b90SDavid Rientjes 	task->signal->oom_score_adj = (short)oom_score_adj;
1079dabb16f6SMandeep Singh Baines 	if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
1080a9c58b90SDavid Rientjes 		task->signal->oom_score_adj_min = (short)oom_score_adj;
108143d2b113SKAMEZAWA Hiroyuki 	trace_oom_score_adj_update(task);
108201dc52ebSDavidlohr Bueso 
1083723548bfSDavid Rientjes err_sighand:
1084a63d83f4SDavid Rientjes 	unlock_task_sighand(task, &flags);
1085d19d5476SDavid Rientjes err_task_lock:
1086d19d5476SDavid Rientjes 	task_unlock(task);
1087a63d83f4SDavid Rientjes 	put_task_struct(task);
1088723548bfSDavid Rientjes out:
1089723548bfSDavid Rientjes 	return err < 0 ? err : count;
1090a63d83f4SDavid Rientjes }
1091a63d83f4SDavid Rientjes 
1092a63d83f4SDavid Rientjes static const struct file_operations proc_oom_score_adj_operations = {
1093a63d83f4SDavid Rientjes 	.read		= oom_score_adj_read,
1094a63d83f4SDavid Rientjes 	.write		= oom_score_adj_write,
10956038f373SArnd Bergmann 	.llseek		= default_llseek,
1096a63d83f4SDavid Rientjes };
1097a63d83f4SDavid Rientjes 
10981da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL
10991da177e4SLinus Torvalds #define TMPBUFLEN 21
11001da177e4SLinus Torvalds static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
11011da177e4SLinus Torvalds 				  size_t count, loff_t *ppos)
11021da177e4SLinus Torvalds {
1103496ad9aaSAl Viro 	struct inode * inode = file_inode(file);
110499f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
11051da177e4SLinus Torvalds 	ssize_t length;
11061da177e4SLinus Torvalds 	char tmpbuf[TMPBUFLEN];
11071da177e4SLinus Torvalds 
110899f89551SEric W. Biederman 	if (!task)
110999f89551SEric W. Biederman 		return -ESRCH;
11101da177e4SLinus Torvalds 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1111e1760bd5SEric W. Biederman 			   from_kuid(file->f_cred->user_ns,
1112e1760bd5SEric W. Biederman 				     audit_get_loginuid(task)));
111399f89551SEric W. Biederman 	put_task_struct(task);
11141da177e4SLinus Torvalds 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
11151da177e4SLinus Torvalds }
11161da177e4SLinus Torvalds 
11171da177e4SLinus Torvalds static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
11181da177e4SLinus Torvalds 				   size_t count, loff_t *ppos)
11191da177e4SLinus Torvalds {
1120496ad9aaSAl Viro 	struct inode * inode = file_inode(file);
11211da177e4SLinus Torvalds 	char *page, *tmp;
11221da177e4SLinus Torvalds 	ssize_t length;
11231da177e4SLinus Torvalds 	uid_t loginuid;
1124e1760bd5SEric W. Biederman 	kuid_t kloginuid;
11251da177e4SLinus Torvalds 
11267dc52157SPaul E. McKenney 	rcu_read_lock();
11277dc52157SPaul E. McKenney 	if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
11287dc52157SPaul E. McKenney 		rcu_read_unlock();
11291da177e4SLinus Torvalds 		return -EPERM;
11307dc52157SPaul E. McKenney 	}
11317dc52157SPaul E. McKenney 	rcu_read_unlock();
11321da177e4SLinus Torvalds 
1133e0182909SAl Viro 	if (count >= PAGE_SIZE)
1134e0182909SAl Viro 		count = PAGE_SIZE - 1;
11351da177e4SLinus Torvalds 
11361da177e4SLinus Torvalds 	if (*ppos != 0) {
11371da177e4SLinus Torvalds 		/* No partial writes. */
11381da177e4SLinus Torvalds 		return -EINVAL;
11391da177e4SLinus Torvalds 	}
1140e12ba74dSMel Gorman 	page = (char*)__get_free_page(GFP_TEMPORARY);
11411da177e4SLinus Torvalds 	if (!page)
11421da177e4SLinus Torvalds 		return -ENOMEM;
11431da177e4SLinus Torvalds 	length = -EFAULT;
11441da177e4SLinus Torvalds 	if (copy_from_user(page, buf, count))
11451da177e4SLinus Torvalds 		goto out_free_page;
11461da177e4SLinus Torvalds 
1147e0182909SAl Viro 	page[count] = '\0';
11481da177e4SLinus Torvalds 	loginuid = simple_strtoul(page, &tmp, 10);
11491da177e4SLinus Torvalds 	if (tmp == page) {
11501da177e4SLinus Torvalds 		length = -EINVAL;
11511da177e4SLinus Torvalds 		goto out_free_page;
11521da177e4SLinus Torvalds 
11531da177e4SLinus Torvalds 	}
1154e1760bd5SEric W. Biederman 	kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
1155e1760bd5SEric W. Biederman 	if (!uid_valid(kloginuid)) {
1156e1760bd5SEric W. Biederman 		length = -EINVAL;
1157e1760bd5SEric W. Biederman 		goto out_free_page;
1158e1760bd5SEric W. Biederman 	}
1159e1760bd5SEric W. Biederman 
1160e1760bd5SEric W. Biederman 	length = audit_set_loginuid(kloginuid);
11611da177e4SLinus Torvalds 	if (likely(length == 0))
11621da177e4SLinus Torvalds 		length = count;
11631da177e4SLinus Torvalds 
11641da177e4SLinus Torvalds out_free_page:
11651da177e4SLinus Torvalds 	free_page((unsigned long) page);
11661da177e4SLinus Torvalds 	return length;
11671da177e4SLinus Torvalds }
11681da177e4SLinus Torvalds 
116900977a59SArjan van de Ven static const struct file_operations proc_loginuid_operations = {
11701da177e4SLinus Torvalds 	.read		= proc_loginuid_read,
11711da177e4SLinus Torvalds 	.write		= proc_loginuid_write,
117287df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
11731da177e4SLinus Torvalds };
11741e0bd755SEric Paris 
11751e0bd755SEric Paris static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
11761e0bd755SEric Paris 				  size_t count, loff_t *ppos)
11771e0bd755SEric Paris {
1178496ad9aaSAl Viro 	struct inode * inode = file_inode(file);
11791e0bd755SEric Paris 	struct task_struct *task = get_proc_task(inode);
11801e0bd755SEric Paris 	ssize_t length;
11811e0bd755SEric Paris 	char tmpbuf[TMPBUFLEN];
11821e0bd755SEric Paris 
11831e0bd755SEric Paris 	if (!task)
11841e0bd755SEric Paris 		return -ESRCH;
11851e0bd755SEric Paris 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
11861e0bd755SEric Paris 				audit_get_sessionid(task));
11871e0bd755SEric Paris 	put_task_struct(task);
11881e0bd755SEric Paris 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
11891e0bd755SEric Paris }
11901e0bd755SEric Paris 
11911e0bd755SEric Paris static const struct file_operations proc_sessionid_operations = {
11921e0bd755SEric Paris 	.read		= proc_sessionid_read,
119387df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
11941e0bd755SEric Paris };
11951da177e4SLinus Torvalds #endif
11961da177e4SLinus Torvalds 
1197f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
1198f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1199f4f154fdSAkinobu Mita 				      size_t count, loff_t *ppos)
1200f4f154fdSAkinobu Mita {
1201496ad9aaSAl Viro 	struct task_struct *task = get_proc_task(file_inode(file));
1202f4f154fdSAkinobu Mita 	char buffer[PROC_NUMBUF];
1203f4f154fdSAkinobu Mita 	size_t len;
1204f4f154fdSAkinobu Mita 	int make_it_fail;
1205f4f154fdSAkinobu Mita 
1206f4f154fdSAkinobu Mita 	if (!task)
1207f4f154fdSAkinobu Mita 		return -ESRCH;
1208f4f154fdSAkinobu Mita 	make_it_fail = task->make_it_fail;
1209f4f154fdSAkinobu Mita 	put_task_struct(task);
1210f4f154fdSAkinobu Mita 
1211f4f154fdSAkinobu Mita 	len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
12120c28f287SAkinobu Mita 
12130c28f287SAkinobu Mita 	return simple_read_from_buffer(buf, count, ppos, buffer, len);
1214f4f154fdSAkinobu Mita }
1215f4f154fdSAkinobu Mita 
1216f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_write(struct file * file,
1217f4f154fdSAkinobu Mita 			const char __user * buf, size_t count, loff_t *ppos)
1218f4f154fdSAkinobu Mita {
1219f4f154fdSAkinobu Mita 	struct task_struct *task;
1220f4f154fdSAkinobu Mita 	char buffer[PROC_NUMBUF], *end;
1221f4f154fdSAkinobu Mita 	int make_it_fail;
1222f4f154fdSAkinobu Mita 
1223f4f154fdSAkinobu Mita 	if (!capable(CAP_SYS_RESOURCE))
1224f4f154fdSAkinobu Mita 		return -EPERM;
1225f4f154fdSAkinobu Mita 	memset(buffer, 0, sizeof(buffer));
1226f4f154fdSAkinobu Mita 	if (count > sizeof(buffer) - 1)
1227f4f154fdSAkinobu Mita 		count = sizeof(buffer) - 1;
1228f4f154fdSAkinobu Mita 	if (copy_from_user(buffer, buf, count))
1229f4f154fdSAkinobu Mita 		return -EFAULT;
1230cba8aafeSVincent Li 	make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1231cba8aafeSVincent Li 	if (*end)
1232cba8aafeSVincent Li 		return -EINVAL;
1233496ad9aaSAl Viro 	task = get_proc_task(file_inode(file));
1234f4f154fdSAkinobu Mita 	if (!task)
1235f4f154fdSAkinobu Mita 		return -ESRCH;
1236f4f154fdSAkinobu Mita 	task->make_it_fail = make_it_fail;
1237f4f154fdSAkinobu Mita 	put_task_struct(task);
1238cba8aafeSVincent Li 
1239cba8aafeSVincent Li 	return count;
1240f4f154fdSAkinobu Mita }
1241f4f154fdSAkinobu Mita 
124200977a59SArjan van de Ven static const struct file_operations proc_fault_inject_operations = {
1243f4f154fdSAkinobu Mita 	.read		= proc_fault_inject_read,
1244f4f154fdSAkinobu Mita 	.write		= proc_fault_inject_write,
124587df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
1246f4f154fdSAkinobu Mita };
1247f4f154fdSAkinobu Mita #endif
1248f4f154fdSAkinobu Mita 
12499745512cSArjan van de Ven 
125043ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
125143ae34cbSIngo Molnar /*
125243ae34cbSIngo Molnar  * Print out various scheduling related per-task fields:
125343ae34cbSIngo Molnar  */
125443ae34cbSIngo Molnar static int sched_show(struct seq_file *m, void *v)
125543ae34cbSIngo Molnar {
125643ae34cbSIngo Molnar 	struct inode *inode = m->private;
125743ae34cbSIngo Molnar 	struct task_struct *p;
125843ae34cbSIngo Molnar 
125943ae34cbSIngo Molnar 	p = get_proc_task(inode);
126043ae34cbSIngo Molnar 	if (!p)
126143ae34cbSIngo Molnar 		return -ESRCH;
126243ae34cbSIngo Molnar 	proc_sched_show_task(p, m);
126343ae34cbSIngo Molnar 
126443ae34cbSIngo Molnar 	put_task_struct(p);
126543ae34cbSIngo Molnar 
126643ae34cbSIngo Molnar 	return 0;
126743ae34cbSIngo Molnar }
126843ae34cbSIngo Molnar 
126943ae34cbSIngo Molnar static ssize_t
127043ae34cbSIngo Molnar sched_write(struct file *file, const char __user *buf,
127143ae34cbSIngo Molnar 	    size_t count, loff_t *offset)
127243ae34cbSIngo Molnar {
1273496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
127443ae34cbSIngo Molnar 	struct task_struct *p;
127543ae34cbSIngo Molnar 
127643ae34cbSIngo Molnar 	p = get_proc_task(inode);
127743ae34cbSIngo Molnar 	if (!p)
127843ae34cbSIngo Molnar 		return -ESRCH;
127943ae34cbSIngo Molnar 	proc_sched_set_task(p);
128043ae34cbSIngo Molnar 
128143ae34cbSIngo Molnar 	put_task_struct(p);
128243ae34cbSIngo Molnar 
128343ae34cbSIngo Molnar 	return count;
128443ae34cbSIngo Molnar }
128543ae34cbSIngo Molnar 
128643ae34cbSIngo Molnar static int sched_open(struct inode *inode, struct file *filp)
128743ae34cbSIngo Molnar {
1288c6a34058SJovi Zhang 	return single_open(filp, sched_show, inode);
128943ae34cbSIngo Molnar }
129043ae34cbSIngo Molnar 
129143ae34cbSIngo Molnar static const struct file_operations proc_pid_sched_operations = {
129243ae34cbSIngo Molnar 	.open		= sched_open,
129343ae34cbSIngo Molnar 	.read		= seq_read,
129443ae34cbSIngo Molnar 	.write		= sched_write,
129543ae34cbSIngo Molnar 	.llseek		= seq_lseek,
12965ea473a1SAlexey Dobriyan 	.release	= single_release,
129743ae34cbSIngo Molnar };
129843ae34cbSIngo Molnar 
129943ae34cbSIngo Molnar #endif
130043ae34cbSIngo Molnar 
13015091faa4SMike Galbraith #ifdef CONFIG_SCHED_AUTOGROUP
13025091faa4SMike Galbraith /*
13035091faa4SMike Galbraith  * Print out autogroup related information:
13045091faa4SMike Galbraith  */
13055091faa4SMike Galbraith static int sched_autogroup_show(struct seq_file *m, void *v)
13065091faa4SMike Galbraith {
13075091faa4SMike Galbraith 	struct inode *inode = m->private;
13085091faa4SMike Galbraith 	struct task_struct *p;
13095091faa4SMike Galbraith 
13105091faa4SMike Galbraith 	p = get_proc_task(inode);
13115091faa4SMike Galbraith 	if (!p)
13125091faa4SMike Galbraith 		return -ESRCH;
13135091faa4SMike Galbraith 	proc_sched_autogroup_show_task(p, m);
13145091faa4SMike Galbraith 
13155091faa4SMike Galbraith 	put_task_struct(p);
13165091faa4SMike Galbraith 
13175091faa4SMike Galbraith 	return 0;
13185091faa4SMike Galbraith }
13195091faa4SMike Galbraith 
13205091faa4SMike Galbraith static ssize_t
13215091faa4SMike Galbraith sched_autogroup_write(struct file *file, const char __user *buf,
13225091faa4SMike Galbraith 	    size_t count, loff_t *offset)
13235091faa4SMike Galbraith {
1324496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
13255091faa4SMike Galbraith 	struct task_struct *p;
13265091faa4SMike Galbraith 	char buffer[PROC_NUMBUF];
13270a8cb8e3SAlexey Dobriyan 	int nice;
13285091faa4SMike Galbraith 	int err;
13295091faa4SMike Galbraith 
13305091faa4SMike Galbraith 	memset(buffer, 0, sizeof(buffer));
13315091faa4SMike Galbraith 	if (count > sizeof(buffer) - 1)
13325091faa4SMike Galbraith 		count = sizeof(buffer) - 1;
13335091faa4SMike Galbraith 	if (copy_from_user(buffer, buf, count))
13345091faa4SMike Galbraith 		return -EFAULT;
13355091faa4SMike Galbraith 
13360a8cb8e3SAlexey Dobriyan 	err = kstrtoint(strstrip(buffer), 0, &nice);
13370a8cb8e3SAlexey Dobriyan 	if (err < 0)
13380a8cb8e3SAlexey Dobriyan 		return err;
13395091faa4SMike Galbraith 
13405091faa4SMike Galbraith 	p = get_proc_task(inode);
13415091faa4SMike Galbraith 	if (!p)
13425091faa4SMike Galbraith 		return -ESRCH;
13435091faa4SMike Galbraith 
13442e5b5b3aSHiroshi Shimamoto 	err = proc_sched_autogroup_set_nice(p, nice);
13455091faa4SMike Galbraith 	if (err)
13465091faa4SMike Galbraith 		count = err;
13475091faa4SMike Galbraith 
13485091faa4SMike Galbraith 	put_task_struct(p);
13495091faa4SMike Galbraith 
13505091faa4SMike Galbraith 	return count;
13515091faa4SMike Galbraith }
13525091faa4SMike Galbraith 
13535091faa4SMike Galbraith static int sched_autogroup_open(struct inode *inode, struct file *filp)
13545091faa4SMike Galbraith {
13555091faa4SMike Galbraith 	int ret;
13565091faa4SMike Galbraith 
13575091faa4SMike Galbraith 	ret = single_open(filp, sched_autogroup_show, NULL);
13585091faa4SMike Galbraith 	if (!ret) {
13595091faa4SMike Galbraith 		struct seq_file *m = filp->private_data;
13605091faa4SMike Galbraith 
13615091faa4SMike Galbraith 		m->private = inode;
13625091faa4SMike Galbraith 	}
13635091faa4SMike Galbraith 	return ret;
13645091faa4SMike Galbraith }
13655091faa4SMike Galbraith 
13665091faa4SMike Galbraith static const struct file_operations proc_pid_sched_autogroup_operations = {
13675091faa4SMike Galbraith 	.open		= sched_autogroup_open,
13685091faa4SMike Galbraith 	.read		= seq_read,
13695091faa4SMike Galbraith 	.write		= sched_autogroup_write,
13705091faa4SMike Galbraith 	.llseek		= seq_lseek,
13715091faa4SMike Galbraith 	.release	= single_release,
13725091faa4SMike Galbraith };
13735091faa4SMike Galbraith 
13745091faa4SMike Galbraith #endif /* CONFIG_SCHED_AUTOGROUP */
13755091faa4SMike Galbraith 
13764614a696Sjohn stultz static ssize_t comm_write(struct file *file, const char __user *buf,
13774614a696Sjohn stultz 				size_t count, loff_t *offset)
13784614a696Sjohn stultz {
1379496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
13804614a696Sjohn stultz 	struct task_struct *p;
13814614a696Sjohn stultz 	char buffer[TASK_COMM_LEN];
1382830e0fc9SDavid Rientjes 	const size_t maxlen = sizeof(buffer) - 1;
13834614a696Sjohn stultz 
13844614a696Sjohn stultz 	memset(buffer, 0, sizeof(buffer));
1385830e0fc9SDavid Rientjes 	if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
13864614a696Sjohn stultz 		return -EFAULT;
13874614a696Sjohn stultz 
13884614a696Sjohn stultz 	p = get_proc_task(inode);
13894614a696Sjohn stultz 	if (!p)
13904614a696Sjohn stultz 		return -ESRCH;
13914614a696Sjohn stultz 
13924614a696Sjohn stultz 	if (same_thread_group(current, p))
13934614a696Sjohn stultz 		set_task_comm(p, buffer);
13944614a696Sjohn stultz 	else
13954614a696Sjohn stultz 		count = -EINVAL;
13964614a696Sjohn stultz 
13974614a696Sjohn stultz 	put_task_struct(p);
13984614a696Sjohn stultz 
13994614a696Sjohn stultz 	return count;
14004614a696Sjohn stultz }
14014614a696Sjohn stultz 
14024614a696Sjohn stultz static int comm_show(struct seq_file *m, void *v)
14034614a696Sjohn stultz {
14044614a696Sjohn stultz 	struct inode *inode = m->private;
14054614a696Sjohn stultz 	struct task_struct *p;
14064614a696Sjohn stultz 
14074614a696Sjohn stultz 	p = get_proc_task(inode);
14084614a696Sjohn stultz 	if (!p)
14094614a696Sjohn stultz 		return -ESRCH;
14104614a696Sjohn stultz 
14114614a696Sjohn stultz 	task_lock(p);
14124614a696Sjohn stultz 	seq_printf(m, "%s\n", p->comm);
14134614a696Sjohn stultz 	task_unlock(p);
14144614a696Sjohn stultz 
14154614a696Sjohn stultz 	put_task_struct(p);
14164614a696Sjohn stultz 
14174614a696Sjohn stultz 	return 0;
14184614a696Sjohn stultz }
14194614a696Sjohn stultz 
14204614a696Sjohn stultz static int comm_open(struct inode *inode, struct file *filp)
14214614a696Sjohn stultz {
1422c6a34058SJovi Zhang 	return single_open(filp, comm_show, inode);
14234614a696Sjohn stultz }
14244614a696Sjohn stultz 
14254614a696Sjohn stultz static const struct file_operations proc_pid_set_comm_operations = {
14264614a696Sjohn stultz 	.open		= comm_open,
14274614a696Sjohn stultz 	.read		= seq_read,
14284614a696Sjohn stultz 	.write		= comm_write,
14294614a696Sjohn stultz 	.llseek		= seq_lseek,
14304614a696Sjohn stultz 	.release	= single_release,
14314614a696Sjohn stultz };
14324614a696Sjohn stultz 
14337773fbc5SCyrill Gorcunov static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
1434925d1c40SMatt Helsley {
1435925d1c40SMatt Helsley 	struct task_struct *task;
1436925d1c40SMatt Helsley 	struct mm_struct *mm;
1437925d1c40SMatt Helsley 	struct file *exe_file;
1438925d1c40SMatt Helsley 
14397773fbc5SCyrill Gorcunov 	task = get_proc_task(dentry->d_inode);
1440925d1c40SMatt Helsley 	if (!task)
1441925d1c40SMatt Helsley 		return -ENOENT;
1442925d1c40SMatt Helsley 	mm = get_task_mm(task);
1443925d1c40SMatt Helsley 	put_task_struct(task);
1444925d1c40SMatt Helsley 	if (!mm)
1445925d1c40SMatt Helsley 		return -ENOENT;
1446925d1c40SMatt Helsley 	exe_file = get_mm_exe_file(mm);
1447925d1c40SMatt Helsley 	mmput(mm);
1448925d1c40SMatt Helsley 	if (exe_file) {
1449925d1c40SMatt Helsley 		*exe_path = exe_file->f_path;
1450925d1c40SMatt Helsley 		path_get(&exe_file->f_path);
1451925d1c40SMatt Helsley 		fput(exe_file);
1452925d1c40SMatt Helsley 		return 0;
1453925d1c40SMatt Helsley 	} else
1454925d1c40SMatt Helsley 		return -ENOENT;
1455925d1c40SMatt Helsley }
1456925d1c40SMatt Helsley 
1457008b150aSAl Viro static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
14581da177e4SLinus Torvalds {
14591da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
1460408ef013SChristoph Hellwig 	struct path path;
14611da177e4SLinus Torvalds 	int error = -EACCES;
14621da177e4SLinus Torvalds 
1463778c1144SEric W. Biederman 	/* Are we allowed to snoop on the tasks file descriptors? */
1464778c1144SEric W. Biederman 	if (!proc_fd_access_allowed(inode))
14651da177e4SLinus Torvalds 		goto out;
14661da177e4SLinus Torvalds 
1467408ef013SChristoph Hellwig 	error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1468408ef013SChristoph Hellwig 	if (error)
1469408ef013SChristoph Hellwig 		goto out;
1470408ef013SChristoph Hellwig 
1471b5fb63c1SChristoph Hellwig 	nd_jump_link(nd, &path);
1472408ef013SChristoph Hellwig 	return NULL;
14731da177e4SLinus Torvalds out:
1474008b150aSAl Viro 	return ERR_PTR(error);
14751da177e4SLinus Torvalds }
14761da177e4SLinus Torvalds 
14773dcd25f3SJan Blunck static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
14781da177e4SLinus Torvalds {
1479e12ba74dSMel Gorman 	char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
14803dcd25f3SJan Blunck 	char *pathname;
14811da177e4SLinus Torvalds 	int len;
14821da177e4SLinus Torvalds 
14831da177e4SLinus Torvalds 	if (!tmp)
14841da177e4SLinus Torvalds 		return -ENOMEM;
14851da177e4SLinus Torvalds 
14867b2a69baSEric W. Biederman 	pathname = d_path(path, tmp, PAGE_SIZE);
14873dcd25f3SJan Blunck 	len = PTR_ERR(pathname);
14883dcd25f3SJan Blunck 	if (IS_ERR(pathname))
14891da177e4SLinus Torvalds 		goto out;
14903dcd25f3SJan Blunck 	len = tmp + PAGE_SIZE - 1 - pathname;
14911da177e4SLinus Torvalds 
14921da177e4SLinus Torvalds 	if (len > buflen)
14931da177e4SLinus Torvalds 		len = buflen;
14943dcd25f3SJan Blunck 	if (copy_to_user(buffer, pathname, len))
14951da177e4SLinus Torvalds 		len = -EFAULT;
14961da177e4SLinus Torvalds  out:
14971da177e4SLinus Torvalds 	free_page((unsigned long)tmp);
14981da177e4SLinus Torvalds 	return len;
14991da177e4SLinus Torvalds }
15001da177e4SLinus Torvalds 
15011da177e4SLinus Torvalds static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
15021da177e4SLinus Torvalds {
15031da177e4SLinus Torvalds 	int error = -EACCES;
15041da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
15053dcd25f3SJan Blunck 	struct path path;
15061da177e4SLinus Torvalds 
1507778c1144SEric W. Biederman 	/* Are we allowed to snoop on the tasks file descriptors? */
1508778c1144SEric W. Biederman 	if (!proc_fd_access_allowed(inode))
15091da177e4SLinus Torvalds 		goto out;
15101da177e4SLinus Torvalds 
15117773fbc5SCyrill Gorcunov 	error = PROC_I(inode)->op.proc_get_link(dentry, &path);
15121da177e4SLinus Torvalds 	if (error)
15131da177e4SLinus Torvalds 		goto out;
15141da177e4SLinus Torvalds 
15153dcd25f3SJan Blunck 	error = do_proc_readlink(&path, buffer, buflen);
15163dcd25f3SJan Blunck 	path_put(&path);
15171da177e4SLinus Torvalds out:
15181da177e4SLinus Torvalds 	return error;
15191da177e4SLinus Torvalds }
15201da177e4SLinus Torvalds 
1521faf60af1SCyrill Gorcunov const struct inode_operations proc_pid_link_inode_operations = {
15221da177e4SLinus Torvalds 	.readlink	= proc_pid_readlink,
15236d76fa58SLinus Torvalds 	.follow_link	= proc_pid_follow_link,
15246d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
15251da177e4SLinus Torvalds };
15261da177e4SLinus Torvalds 
152728a6d671SEric W. Biederman 
152828a6d671SEric W. Biederman /* building an inode */
152928a6d671SEric W. Biederman 
15306b4e306aSEric W. Biederman struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
153128a6d671SEric W. Biederman {
153228a6d671SEric W. Biederman 	struct inode * inode;
153328a6d671SEric W. Biederman 	struct proc_inode *ei;
1534c69e8d9cSDavid Howells 	const struct cred *cred;
153528a6d671SEric W. Biederman 
153628a6d671SEric W. Biederman 	/* We need a new inode */
153728a6d671SEric W. Biederman 
153828a6d671SEric W. Biederman 	inode = new_inode(sb);
153928a6d671SEric W. Biederman 	if (!inode)
154028a6d671SEric W. Biederman 		goto out;
154128a6d671SEric W. Biederman 
154228a6d671SEric W. Biederman 	/* Common stuff */
154328a6d671SEric W. Biederman 	ei = PROC_I(inode);
154485fe4025SChristoph Hellwig 	inode->i_ino = get_next_ino();
154528a6d671SEric W. Biederman 	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
154628a6d671SEric W. Biederman 	inode->i_op = &proc_def_inode_operations;
154728a6d671SEric W. Biederman 
154828a6d671SEric W. Biederman 	/*
154928a6d671SEric W. Biederman 	 * grab the reference to task.
155028a6d671SEric W. Biederman 	 */
15511a657f78SOleg Nesterov 	ei->pid = get_task_pid(task, PIDTYPE_PID);
155228a6d671SEric W. Biederman 	if (!ei->pid)
155328a6d671SEric W. Biederman 		goto out_unlock;
155428a6d671SEric W. Biederman 
155528a6d671SEric W. Biederman 	if (task_dumpable(task)) {
1556c69e8d9cSDavid Howells 		rcu_read_lock();
1557c69e8d9cSDavid Howells 		cred = __task_cred(task);
1558c69e8d9cSDavid Howells 		inode->i_uid = cred->euid;
1559c69e8d9cSDavid Howells 		inode->i_gid = cred->egid;
1560c69e8d9cSDavid Howells 		rcu_read_unlock();
156128a6d671SEric W. Biederman 	}
156228a6d671SEric W. Biederman 	security_task_to_inode(task, inode);
156328a6d671SEric W. Biederman 
156428a6d671SEric W. Biederman out:
156528a6d671SEric W. Biederman 	return inode;
156628a6d671SEric W. Biederman 
156728a6d671SEric W. Biederman out_unlock:
156828a6d671SEric W. Biederman 	iput(inode);
156928a6d671SEric W. Biederman 	return NULL;
157028a6d671SEric W. Biederman }
157128a6d671SEric W. Biederman 
15726b4e306aSEric W. Biederman int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
157328a6d671SEric W. Biederman {
157428a6d671SEric W. Biederman 	struct inode *inode = dentry->d_inode;
157528a6d671SEric W. Biederman 	struct task_struct *task;
1576c69e8d9cSDavid Howells 	const struct cred *cred;
15770499680aSVasiliy Kulikov 	struct pid_namespace *pid = dentry->d_sb->s_fs_info;
1578c69e8d9cSDavid Howells 
157928a6d671SEric W. Biederman 	generic_fillattr(inode, stat);
158028a6d671SEric W. Biederman 
158128a6d671SEric W. Biederman 	rcu_read_lock();
1582dcb0f222SEric W. Biederman 	stat->uid = GLOBAL_ROOT_UID;
1583dcb0f222SEric W. Biederman 	stat->gid = GLOBAL_ROOT_GID;
158428a6d671SEric W. Biederman 	task = pid_task(proc_pid(inode), PIDTYPE_PID);
158528a6d671SEric W. Biederman 	if (task) {
15860499680aSVasiliy Kulikov 		if (!has_pid_permissions(pid, task, 2)) {
15870499680aSVasiliy Kulikov 			rcu_read_unlock();
15880499680aSVasiliy Kulikov 			/*
15890499680aSVasiliy Kulikov 			 * This doesn't prevent learning whether PID exists,
15900499680aSVasiliy Kulikov 			 * it only makes getattr() consistent with readdir().
15910499680aSVasiliy Kulikov 			 */
15920499680aSVasiliy Kulikov 			return -ENOENT;
15930499680aSVasiliy Kulikov 		}
159428a6d671SEric W. Biederman 		if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
159528a6d671SEric W. Biederman 		    task_dumpable(task)) {
1596c69e8d9cSDavid Howells 			cred = __task_cred(task);
1597c69e8d9cSDavid Howells 			stat->uid = cred->euid;
1598c69e8d9cSDavid Howells 			stat->gid = cred->egid;
159928a6d671SEric W. Biederman 		}
160028a6d671SEric W. Biederman 	}
160128a6d671SEric W. Biederman 	rcu_read_unlock();
160228a6d671SEric W. Biederman 	return 0;
160328a6d671SEric W. Biederman }
160428a6d671SEric W. Biederman 
160528a6d671SEric W. Biederman /* dentry stuff */
160628a6d671SEric W. Biederman 
160728a6d671SEric W. Biederman /*
160828a6d671SEric W. Biederman  *	Exceptional case: normally we are not allowed to unhash a busy
160928a6d671SEric W. Biederman  * directory. In this case, however, we can do it - no aliasing problems
161028a6d671SEric W. Biederman  * due to the way we treat inodes.
161128a6d671SEric W. Biederman  *
161228a6d671SEric W. Biederman  * Rewrite the inode's ownerships here because the owning task may have
161328a6d671SEric W. Biederman  * performed a setuid(), etc.
161428a6d671SEric W. Biederman  *
161528a6d671SEric W. Biederman  * Before the /proc/pid/status file was created the only way to read
161628a6d671SEric W. Biederman  * the effective uid of a /process was to stat /proc/pid.  Reading
161728a6d671SEric W. Biederman  * /proc/pid/status is slow enough that procps and other packages
161828a6d671SEric W. Biederman  * kept stating /proc/pid.  To keep the rules in /proc simple I have
161928a6d671SEric W. Biederman  * made this apply to all per process world readable and executable
162028a6d671SEric W. Biederman  * directories.
162128a6d671SEric W. Biederman  */
16220b728e19SAl Viro int pid_revalidate(struct dentry *dentry, unsigned int flags)
162328a6d671SEric W. Biederman {
162434286d66SNick Piggin 	struct inode *inode;
162534286d66SNick Piggin 	struct task_struct *task;
1626c69e8d9cSDavid Howells 	const struct cred *cred;
1627c69e8d9cSDavid Howells 
16280b728e19SAl Viro 	if (flags & LOOKUP_RCU)
162934286d66SNick Piggin 		return -ECHILD;
163034286d66SNick Piggin 
163134286d66SNick Piggin 	inode = dentry->d_inode;
163234286d66SNick Piggin 	task = get_proc_task(inode);
163334286d66SNick Piggin 
163428a6d671SEric W. Biederman 	if (task) {
163528a6d671SEric W. Biederman 		if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
163628a6d671SEric W. Biederman 		    task_dumpable(task)) {
1637c69e8d9cSDavid Howells 			rcu_read_lock();
1638c69e8d9cSDavid Howells 			cred = __task_cred(task);
1639c69e8d9cSDavid Howells 			inode->i_uid = cred->euid;
1640c69e8d9cSDavid Howells 			inode->i_gid = cred->egid;
1641c69e8d9cSDavid Howells 			rcu_read_unlock();
164228a6d671SEric W. Biederman 		} else {
1643dcb0f222SEric W. Biederman 			inode->i_uid = GLOBAL_ROOT_UID;
1644dcb0f222SEric W. Biederman 			inode->i_gid = GLOBAL_ROOT_GID;
164528a6d671SEric W. Biederman 		}
164628a6d671SEric W. Biederman 		inode->i_mode &= ~(S_ISUID | S_ISGID);
164728a6d671SEric W. Biederman 		security_task_to_inode(task, inode);
164828a6d671SEric W. Biederman 		put_task_struct(task);
164928a6d671SEric W. Biederman 		return 1;
165028a6d671SEric W. Biederman 	}
165128a6d671SEric W. Biederman 	d_drop(dentry);
165228a6d671SEric W. Biederman 	return 0;
165328a6d671SEric W. Biederman }
165428a6d671SEric W. Biederman 
16551dd704b6SDavid Howells int pid_delete_dentry(const struct dentry *dentry)
16561dd704b6SDavid Howells {
16571dd704b6SDavid Howells 	/* Is the task we represent dead?
16581dd704b6SDavid Howells 	 * If so, then don't put the dentry on the lru list,
16591dd704b6SDavid Howells 	 * kill it immediately.
16601dd704b6SDavid Howells 	 */
16611dd704b6SDavid Howells 	return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
16621dd704b6SDavid Howells }
16631dd704b6SDavid Howells 
16646b4e306aSEric W. Biederman const struct dentry_operations pid_dentry_operations =
166528a6d671SEric W. Biederman {
166628a6d671SEric W. Biederman 	.d_revalidate	= pid_revalidate,
166728a6d671SEric W. Biederman 	.d_delete	= pid_delete_dentry,
166828a6d671SEric W. Biederman };
166928a6d671SEric W. Biederman 
167028a6d671SEric W. Biederman /* Lookups */
167128a6d671SEric W. Biederman 
16721c0d04c9SEric W. Biederman /*
16731c0d04c9SEric W. Biederman  * Fill a directory entry.
16741c0d04c9SEric W. Biederman  *
16751c0d04c9SEric W. Biederman  * If possible create the dcache entry and derive our inode number and
16761c0d04c9SEric W. Biederman  * file type from dcache entry.
16771c0d04c9SEric W. Biederman  *
16781c0d04c9SEric W. Biederman  * Since all of the proc inode numbers are dynamically generated, the inode
16791c0d04c9SEric W. Biederman  * numbers do not exist until the inode is cache.  This means creating the
16801c0d04c9SEric W. Biederman  * the dcache entry in readdir is necessary to keep the inode numbers
16811c0d04c9SEric W. Biederman  * reported by readdir in sync with the inode numbers reported
16821c0d04c9SEric W. Biederman  * by stat.
16831c0d04c9SEric W. Biederman  */
16846b4e306aSEric W. Biederman int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
16856b4e306aSEric W. Biederman 	const char *name, int len,
1686c5141e6dSEric Dumazet 	instantiate_t instantiate, struct task_struct *task, const void *ptr)
168761a28784SEric W. Biederman {
16882fddfeefSJosef "Jeff" Sipek 	struct dentry *child, *dir = filp->f_path.dentry;
168961a28784SEric W. Biederman 	struct inode *inode;
169061a28784SEric W. Biederman 	struct qstr qname;
169161a28784SEric W. Biederman 	ino_t ino = 0;
169261a28784SEric W. Biederman 	unsigned type = DT_UNKNOWN;
169361a28784SEric W. Biederman 
169461a28784SEric W. Biederman 	qname.name = name;
169561a28784SEric W. Biederman 	qname.len  = len;
169661a28784SEric W. Biederman 	qname.hash = full_name_hash(name, len);
169761a28784SEric W. Biederman 
169861a28784SEric W. Biederman 	child = d_lookup(dir, &qname);
169961a28784SEric W. Biederman 	if (!child) {
170061a28784SEric W. Biederman 		struct dentry *new;
170161a28784SEric W. Biederman 		new = d_alloc(dir, &qname);
170261a28784SEric W. Biederman 		if (new) {
170361a28784SEric W. Biederman 			child = instantiate(dir->d_inode, new, task, ptr);
170461a28784SEric W. Biederman 			if (child)
170561a28784SEric W. Biederman 				dput(new);
170661a28784SEric W. Biederman 			else
170761a28784SEric W. Biederman 				child = new;
170861a28784SEric W. Biederman 		}
170961a28784SEric W. Biederman 	}
171061a28784SEric W. Biederman 	if (!child || IS_ERR(child) || !child->d_inode)
171161a28784SEric W. Biederman 		goto end_instantiate;
171261a28784SEric W. Biederman 	inode = child->d_inode;
171361a28784SEric W. Biederman 	if (inode) {
171461a28784SEric W. Biederman 		ino = inode->i_ino;
171561a28784SEric W. Biederman 		type = inode->i_mode >> 12;
171661a28784SEric W. Biederman 	}
171761a28784SEric W. Biederman 	dput(child);
171861a28784SEric W. Biederman end_instantiate:
171961a28784SEric W. Biederman 	if (!ino)
172061a28784SEric W. Biederman 		ino = find_inode_number(dir, &qname);
172161a28784SEric W. Biederman 	if (!ino)
172261a28784SEric W. Biederman 		ino = 1;
172361a28784SEric W. Biederman 	return filldir(dirent, name, len, filp->f_pos, ino, type);
172461a28784SEric W. Biederman }
172561a28784SEric W. Biederman 
1726640708a2SPavel Emelyanov #ifdef CONFIG_CHECKPOINT_RESTORE
1727640708a2SPavel Emelyanov 
1728640708a2SPavel Emelyanov /*
1729640708a2SPavel Emelyanov  * dname_to_vma_addr - maps a dentry name into two unsigned longs
1730640708a2SPavel Emelyanov  * which represent vma start and end addresses.
1731640708a2SPavel Emelyanov  */
1732640708a2SPavel Emelyanov static int dname_to_vma_addr(struct dentry *dentry,
1733640708a2SPavel Emelyanov 			     unsigned long *start, unsigned long *end)
1734640708a2SPavel Emelyanov {
1735640708a2SPavel Emelyanov 	if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2)
1736640708a2SPavel Emelyanov 		return -EINVAL;
1737640708a2SPavel Emelyanov 
1738640708a2SPavel Emelyanov 	return 0;
1739640708a2SPavel Emelyanov }
1740640708a2SPavel Emelyanov 
17410b728e19SAl Viro static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
1742640708a2SPavel Emelyanov {
1743640708a2SPavel Emelyanov 	unsigned long vm_start, vm_end;
1744640708a2SPavel Emelyanov 	bool exact_vma_exists = false;
1745640708a2SPavel Emelyanov 	struct mm_struct *mm = NULL;
1746640708a2SPavel Emelyanov 	struct task_struct *task;
1747640708a2SPavel Emelyanov 	const struct cred *cred;
1748640708a2SPavel Emelyanov 	struct inode *inode;
1749640708a2SPavel Emelyanov 	int status = 0;
1750640708a2SPavel Emelyanov 
17510b728e19SAl Viro 	if (flags & LOOKUP_RCU)
1752640708a2SPavel Emelyanov 		return -ECHILD;
1753640708a2SPavel Emelyanov 
1754640708a2SPavel Emelyanov 	if (!capable(CAP_SYS_ADMIN)) {
175541735818SZhao Hongjiang 		status = -EPERM;
1756640708a2SPavel Emelyanov 		goto out_notask;
1757640708a2SPavel Emelyanov 	}
1758640708a2SPavel Emelyanov 
1759640708a2SPavel Emelyanov 	inode = dentry->d_inode;
1760640708a2SPavel Emelyanov 	task = get_proc_task(inode);
1761640708a2SPavel Emelyanov 	if (!task)
1762640708a2SPavel Emelyanov 		goto out_notask;
1763640708a2SPavel Emelyanov 
17642344bec7SCong Wang 	mm = mm_access(task, PTRACE_MODE_READ);
17652344bec7SCong Wang 	if (IS_ERR_OR_NULL(mm))
1766640708a2SPavel Emelyanov 		goto out;
1767640708a2SPavel Emelyanov 
1768640708a2SPavel Emelyanov 	if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1769640708a2SPavel Emelyanov 		down_read(&mm->mmap_sem);
1770640708a2SPavel Emelyanov 		exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1771640708a2SPavel Emelyanov 		up_read(&mm->mmap_sem);
1772640708a2SPavel Emelyanov 	}
1773640708a2SPavel Emelyanov 
1774640708a2SPavel Emelyanov 	mmput(mm);
1775640708a2SPavel Emelyanov 
1776640708a2SPavel Emelyanov 	if (exact_vma_exists) {
1777640708a2SPavel Emelyanov 		if (task_dumpable(task)) {
1778640708a2SPavel Emelyanov 			rcu_read_lock();
1779640708a2SPavel Emelyanov 			cred = __task_cred(task);
1780640708a2SPavel Emelyanov 			inode->i_uid = cred->euid;
1781640708a2SPavel Emelyanov 			inode->i_gid = cred->egid;
1782640708a2SPavel Emelyanov 			rcu_read_unlock();
1783640708a2SPavel Emelyanov 		} else {
1784dcb0f222SEric W. Biederman 			inode->i_uid = GLOBAL_ROOT_UID;
1785dcb0f222SEric W. Biederman 			inode->i_gid = GLOBAL_ROOT_GID;
1786640708a2SPavel Emelyanov 		}
1787640708a2SPavel Emelyanov 		security_task_to_inode(task, inode);
1788640708a2SPavel Emelyanov 		status = 1;
1789640708a2SPavel Emelyanov 	}
1790640708a2SPavel Emelyanov 
1791640708a2SPavel Emelyanov out:
1792640708a2SPavel Emelyanov 	put_task_struct(task);
1793640708a2SPavel Emelyanov 
1794640708a2SPavel Emelyanov out_notask:
1795640708a2SPavel Emelyanov 	if (status <= 0)
1796640708a2SPavel Emelyanov 		d_drop(dentry);
1797640708a2SPavel Emelyanov 
1798640708a2SPavel Emelyanov 	return status;
1799640708a2SPavel Emelyanov }
1800640708a2SPavel Emelyanov 
1801640708a2SPavel Emelyanov static const struct dentry_operations tid_map_files_dentry_operations = {
1802640708a2SPavel Emelyanov 	.d_revalidate	= map_files_d_revalidate,
1803640708a2SPavel Emelyanov 	.d_delete	= pid_delete_dentry,
1804640708a2SPavel Emelyanov };
1805640708a2SPavel Emelyanov 
1806640708a2SPavel Emelyanov static int proc_map_files_get_link(struct dentry *dentry, struct path *path)
1807640708a2SPavel Emelyanov {
1808640708a2SPavel Emelyanov 	unsigned long vm_start, vm_end;
1809640708a2SPavel Emelyanov 	struct vm_area_struct *vma;
1810640708a2SPavel Emelyanov 	struct task_struct *task;
1811640708a2SPavel Emelyanov 	struct mm_struct *mm;
1812640708a2SPavel Emelyanov 	int rc;
1813640708a2SPavel Emelyanov 
1814640708a2SPavel Emelyanov 	rc = -ENOENT;
1815640708a2SPavel Emelyanov 	task = get_proc_task(dentry->d_inode);
1816640708a2SPavel Emelyanov 	if (!task)
1817640708a2SPavel Emelyanov 		goto out;
1818640708a2SPavel Emelyanov 
1819640708a2SPavel Emelyanov 	mm = get_task_mm(task);
1820640708a2SPavel Emelyanov 	put_task_struct(task);
1821640708a2SPavel Emelyanov 	if (!mm)
1822640708a2SPavel Emelyanov 		goto out;
1823640708a2SPavel Emelyanov 
1824640708a2SPavel Emelyanov 	rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
1825640708a2SPavel Emelyanov 	if (rc)
1826640708a2SPavel Emelyanov 		goto out_mmput;
1827640708a2SPavel Emelyanov 
1828640708a2SPavel Emelyanov 	down_read(&mm->mmap_sem);
1829640708a2SPavel Emelyanov 	vma = find_exact_vma(mm, vm_start, vm_end);
1830640708a2SPavel Emelyanov 	if (vma && vma->vm_file) {
1831640708a2SPavel Emelyanov 		*path = vma->vm_file->f_path;
1832640708a2SPavel Emelyanov 		path_get(path);
1833640708a2SPavel Emelyanov 		rc = 0;
1834640708a2SPavel Emelyanov 	}
1835640708a2SPavel Emelyanov 	up_read(&mm->mmap_sem);
1836640708a2SPavel Emelyanov 
1837640708a2SPavel Emelyanov out_mmput:
1838640708a2SPavel Emelyanov 	mmput(mm);
1839640708a2SPavel Emelyanov out:
1840640708a2SPavel Emelyanov 	return rc;
1841640708a2SPavel Emelyanov }
1842640708a2SPavel Emelyanov 
1843640708a2SPavel Emelyanov struct map_files_info {
18447b540d06SAl Viro 	fmode_t		mode;
1845640708a2SPavel Emelyanov 	unsigned long	len;
1846640708a2SPavel Emelyanov 	unsigned char	name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
1847640708a2SPavel Emelyanov };
1848640708a2SPavel Emelyanov 
1849640708a2SPavel Emelyanov static struct dentry *
1850640708a2SPavel Emelyanov proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
1851640708a2SPavel Emelyanov 			   struct task_struct *task, const void *ptr)
1852640708a2SPavel Emelyanov {
18537b540d06SAl Viro 	fmode_t mode = (fmode_t)(unsigned long)ptr;
1854640708a2SPavel Emelyanov 	struct proc_inode *ei;
1855640708a2SPavel Emelyanov 	struct inode *inode;
1856640708a2SPavel Emelyanov 
1857640708a2SPavel Emelyanov 	inode = proc_pid_make_inode(dir->i_sb, task);
1858640708a2SPavel Emelyanov 	if (!inode)
1859640708a2SPavel Emelyanov 		return ERR_PTR(-ENOENT);
1860640708a2SPavel Emelyanov 
1861640708a2SPavel Emelyanov 	ei = PROC_I(inode);
1862640708a2SPavel Emelyanov 	ei->op.proc_get_link = proc_map_files_get_link;
1863640708a2SPavel Emelyanov 
1864640708a2SPavel Emelyanov 	inode->i_op = &proc_pid_link_inode_operations;
1865640708a2SPavel Emelyanov 	inode->i_size = 64;
1866640708a2SPavel Emelyanov 	inode->i_mode = S_IFLNK;
1867640708a2SPavel Emelyanov 
18687b540d06SAl Viro 	if (mode & FMODE_READ)
1869640708a2SPavel Emelyanov 		inode->i_mode |= S_IRUSR;
18707b540d06SAl Viro 	if (mode & FMODE_WRITE)
1871640708a2SPavel Emelyanov 		inode->i_mode |= S_IWUSR;
1872640708a2SPavel Emelyanov 
1873640708a2SPavel Emelyanov 	d_set_d_op(dentry, &tid_map_files_dentry_operations);
1874640708a2SPavel Emelyanov 	d_add(dentry, inode);
1875640708a2SPavel Emelyanov 
1876640708a2SPavel Emelyanov 	return NULL;
1877640708a2SPavel Emelyanov }
1878640708a2SPavel Emelyanov 
1879640708a2SPavel Emelyanov static struct dentry *proc_map_files_lookup(struct inode *dir,
188000cd8dd3SAl Viro 		struct dentry *dentry, unsigned int flags)
1881640708a2SPavel Emelyanov {
1882640708a2SPavel Emelyanov 	unsigned long vm_start, vm_end;
1883640708a2SPavel Emelyanov 	struct vm_area_struct *vma;
1884640708a2SPavel Emelyanov 	struct task_struct *task;
1885640708a2SPavel Emelyanov 	struct dentry *result;
1886640708a2SPavel Emelyanov 	struct mm_struct *mm;
1887640708a2SPavel Emelyanov 
188841735818SZhao Hongjiang 	result = ERR_PTR(-EPERM);
1889640708a2SPavel Emelyanov 	if (!capable(CAP_SYS_ADMIN))
1890640708a2SPavel Emelyanov 		goto out;
1891640708a2SPavel Emelyanov 
1892640708a2SPavel Emelyanov 	result = ERR_PTR(-ENOENT);
1893640708a2SPavel Emelyanov 	task = get_proc_task(dir);
1894640708a2SPavel Emelyanov 	if (!task)
1895640708a2SPavel Emelyanov 		goto out;
1896640708a2SPavel Emelyanov 
1897640708a2SPavel Emelyanov 	result = ERR_PTR(-EACCES);
1898eb94cd96SCyrill Gorcunov 	if (!ptrace_may_access(task, PTRACE_MODE_READ))
1899640708a2SPavel Emelyanov 		goto out_put_task;
1900640708a2SPavel Emelyanov 
1901640708a2SPavel Emelyanov 	result = ERR_PTR(-ENOENT);
1902640708a2SPavel Emelyanov 	if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
1903eb94cd96SCyrill Gorcunov 		goto out_put_task;
1904640708a2SPavel Emelyanov 
1905640708a2SPavel Emelyanov 	mm = get_task_mm(task);
1906640708a2SPavel Emelyanov 	if (!mm)
1907eb94cd96SCyrill Gorcunov 		goto out_put_task;
1908640708a2SPavel Emelyanov 
1909640708a2SPavel Emelyanov 	down_read(&mm->mmap_sem);
1910640708a2SPavel Emelyanov 	vma = find_exact_vma(mm, vm_start, vm_end);
1911640708a2SPavel Emelyanov 	if (!vma)
1912640708a2SPavel Emelyanov 		goto out_no_vma;
1913640708a2SPavel Emelyanov 
191405f56484SStanislav Kinsbursky 	if (vma->vm_file)
19157b540d06SAl Viro 		result = proc_map_files_instantiate(dir, dentry, task,
19167b540d06SAl Viro 				(void *)(unsigned long)vma->vm_file->f_mode);
1917640708a2SPavel Emelyanov 
1918640708a2SPavel Emelyanov out_no_vma:
1919640708a2SPavel Emelyanov 	up_read(&mm->mmap_sem);
1920640708a2SPavel Emelyanov 	mmput(mm);
1921640708a2SPavel Emelyanov out_put_task:
1922640708a2SPavel Emelyanov 	put_task_struct(task);
1923640708a2SPavel Emelyanov out:
1924640708a2SPavel Emelyanov 	return result;
1925640708a2SPavel Emelyanov }
1926640708a2SPavel Emelyanov 
1927640708a2SPavel Emelyanov static const struct inode_operations proc_map_files_inode_operations = {
1928640708a2SPavel Emelyanov 	.lookup		= proc_map_files_lookup,
1929640708a2SPavel Emelyanov 	.permission	= proc_fd_permission,
1930640708a2SPavel Emelyanov 	.setattr	= proc_setattr,
1931640708a2SPavel Emelyanov };
1932640708a2SPavel Emelyanov 
1933640708a2SPavel Emelyanov static int
1934640708a2SPavel Emelyanov proc_map_files_readdir(struct file *filp, void *dirent, filldir_t filldir)
1935640708a2SPavel Emelyanov {
1936640708a2SPavel Emelyanov 	struct dentry *dentry = filp->f_path.dentry;
1937640708a2SPavel Emelyanov 	struct inode *inode = dentry->d_inode;
1938640708a2SPavel Emelyanov 	struct vm_area_struct *vma;
1939640708a2SPavel Emelyanov 	struct task_struct *task;
1940640708a2SPavel Emelyanov 	struct mm_struct *mm;
1941640708a2SPavel Emelyanov 	ino_t ino;
1942640708a2SPavel Emelyanov 	int ret;
1943640708a2SPavel Emelyanov 
194441735818SZhao Hongjiang 	ret = -EPERM;
1945640708a2SPavel Emelyanov 	if (!capable(CAP_SYS_ADMIN))
1946640708a2SPavel Emelyanov 		goto out;
1947640708a2SPavel Emelyanov 
1948640708a2SPavel Emelyanov 	ret = -ENOENT;
1949640708a2SPavel Emelyanov 	task = get_proc_task(inode);
1950640708a2SPavel Emelyanov 	if (!task)
1951640708a2SPavel Emelyanov 		goto out;
1952640708a2SPavel Emelyanov 
1953640708a2SPavel Emelyanov 	ret = -EACCES;
1954eb94cd96SCyrill Gorcunov 	if (!ptrace_may_access(task, PTRACE_MODE_READ))
1955640708a2SPavel Emelyanov 		goto out_put_task;
1956640708a2SPavel Emelyanov 
1957640708a2SPavel Emelyanov 	ret = 0;
1958640708a2SPavel Emelyanov 	switch (filp->f_pos) {
1959640708a2SPavel Emelyanov 	case 0:
1960640708a2SPavel Emelyanov 		ino = inode->i_ino;
1961640708a2SPavel Emelyanov 		if (filldir(dirent, ".", 1, 0, ino, DT_DIR) < 0)
1962eb94cd96SCyrill Gorcunov 			goto out_put_task;
1963640708a2SPavel Emelyanov 		filp->f_pos++;
1964640708a2SPavel Emelyanov 	case 1:
1965640708a2SPavel Emelyanov 		ino = parent_ino(dentry);
1966640708a2SPavel Emelyanov 		if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1967eb94cd96SCyrill Gorcunov 			goto out_put_task;
1968640708a2SPavel Emelyanov 		filp->f_pos++;
1969640708a2SPavel Emelyanov 	default:
1970640708a2SPavel Emelyanov 	{
1971640708a2SPavel Emelyanov 		unsigned long nr_files, pos, i;
1972640708a2SPavel Emelyanov 		struct flex_array *fa = NULL;
1973640708a2SPavel Emelyanov 		struct map_files_info info;
1974640708a2SPavel Emelyanov 		struct map_files_info *p;
1975640708a2SPavel Emelyanov 
1976640708a2SPavel Emelyanov 		mm = get_task_mm(task);
1977640708a2SPavel Emelyanov 		if (!mm)
1978eb94cd96SCyrill Gorcunov 			goto out_put_task;
1979640708a2SPavel Emelyanov 		down_read(&mm->mmap_sem);
1980640708a2SPavel Emelyanov 
1981640708a2SPavel Emelyanov 		nr_files = 0;
1982640708a2SPavel Emelyanov 
1983640708a2SPavel Emelyanov 		/*
1984640708a2SPavel Emelyanov 		 * We need two passes here:
1985640708a2SPavel Emelyanov 		 *
1986640708a2SPavel Emelyanov 		 *  1) Collect vmas of mapped files with mmap_sem taken
1987640708a2SPavel Emelyanov 		 *  2) Release mmap_sem and instantiate entries
1988640708a2SPavel Emelyanov 		 *
1989640708a2SPavel Emelyanov 		 * otherwise we get lockdep complained, since filldir()
1990640708a2SPavel Emelyanov 		 * routine might require mmap_sem taken in might_fault().
1991640708a2SPavel Emelyanov 		 */
1992640708a2SPavel Emelyanov 
1993640708a2SPavel Emelyanov 		for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
1994640708a2SPavel Emelyanov 			if (vma->vm_file && ++pos > filp->f_pos)
1995640708a2SPavel Emelyanov 				nr_files++;
1996640708a2SPavel Emelyanov 		}
1997640708a2SPavel Emelyanov 
1998640708a2SPavel Emelyanov 		if (nr_files) {
1999640708a2SPavel Emelyanov 			fa = flex_array_alloc(sizeof(info), nr_files,
2000640708a2SPavel Emelyanov 						GFP_KERNEL);
2001640708a2SPavel Emelyanov 			if (!fa || flex_array_prealloc(fa, 0, nr_files,
2002640708a2SPavel Emelyanov 							GFP_KERNEL)) {
2003640708a2SPavel Emelyanov 				ret = -ENOMEM;
2004640708a2SPavel Emelyanov 				if (fa)
2005640708a2SPavel Emelyanov 					flex_array_free(fa);
2006640708a2SPavel Emelyanov 				up_read(&mm->mmap_sem);
2007640708a2SPavel Emelyanov 				mmput(mm);
2008eb94cd96SCyrill Gorcunov 				goto out_put_task;
2009640708a2SPavel Emelyanov 			}
2010640708a2SPavel Emelyanov 			for (i = 0, vma = mm->mmap, pos = 2; vma;
2011640708a2SPavel Emelyanov 					vma = vma->vm_next) {
2012640708a2SPavel Emelyanov 				if (!vma->vm_file)
2013640708a2SPavel Emelyanov 					continue;
2014640708a2SPavel Emelyanov 				if (++pos <= filp->f_pos)
2015640708a2SPavel Emelyanov 					continue;
2016640708a2SPavel Emelyanov 
20177b540d06SAl Viro 				info.mode = vma->vm_file->f_mode;
2018640708a2SPavel Emelyanov 				info.len = snprintf(info.name,
2019640708a2SPavel Emelyanov 						sizeof(info.name), "%lx-%lx",
2020640708a2SPavel Emelyanov 						vma->vm_start, vma->vm_end);
2021640708a2SPavel Emelyanov 				if (flex_array_put(fa, i++, &info, GFP_KERNEL))
2022640708a2SPavel Emelyanov 					BUG();
2023640708a2SPavel Emelyanov 			}
2024640708a2SPavel Emelyanov 		}
2025640708a2SPavel Emelyanov 		up_read(&mm->mmap_sem);
2026640708a2SPavel Emelyanov 
2027640708a2SPavel Emelyanov 		for (i = 0; i < nr_files; i++) {
2028640708a2SPavel Emelyanov 			p = flex_array_get(fa, i);
2029640708a2SPavel Emelyanov 			ret = proc_fill_cache(filp, dirent, filldir,
2030640708a2SPavel Emelyanov 					      p->name, p->len,
2031640708a2SPavel Emelyanov 					      proc_map_files_instantiate,
20327b540d06SAl Viro 					      task,
20337b540d06SAl Viro 					      (void *)(unsigned long)p->mode);
2034640708a2SPavel Emelyanov 			if (ret)
2035640708a2SPavel Emelyanov 				break;
2036640708a2SPavel Emelyanov 			filp->f_pos++;
2037640708a2SPavel Emelyanov 		}
2038640708a2SPavel Emelyanov 		if (fa)
2039640708a2SPavel Emelyanov 			flex_array_free(fa);
2040640708a2SPavel Emelyanov 		mmput(mm);
2041640708a2SPavel Emelyanov 	}
2042640708a2SPavel Emelyanov 	}
2043640708a2SPavel Emelyanov 
2044640708a2SPavel Emelyanov out_put_task:
2045640708a2SPavel Emelyanov 	put_task_struct(task);
2046640708a2SPavel Emelyanov out:
2047640708a2SPavel Emelyanov 	return ret;
2048640708a2SPavel Emelyanov }
2049640708a2SPavel Emelyanov 
2050640708a2SPavel Emelyanov static const struct file_operations proc_map_files_operations = {
2051640708a2SPavel Emelyanov 	.read		= generic_read_dir,
2052640708a2SPavel Emelyanov 	.readdir	= proc_map_files_readdir,
2053640708a2SPavel Emelyanov 	.llseek		= default_llseek,
2054640708a2SPavel Emelyanov };
2055640708a2SPavel Emelyanov 
205648f6a7a5SPavel Emelyanov struct timers_private {
205748f6a7a5SPavel Emelyanov 	struct pid *pid;
205848f6a7a5SPavel Emelyanov 	struct task_struct *task;
205948f6a7a5SPavel Emelyanov 	struct sighand_struct *sighand;
206057b8015eSPavel Emelyanov 	struct pid_namespace *ns;
206148f6a7a5SPavel Emelyanov 	unsigned long flags;
206248f6a7a5SPavel Emelyanov };
206348f6a7a5SPavel Emelyanov 
206448f6a7a5SPavel Emelyanov static void *timers_start(struct seq_file *m, loff_t *pos)
206548f6a7a5SPavel Emelyanov {
206648f6a7a5SPavel Emelyanov 	struct timers_private *tp = m->private;
206748f6a7a5SPavel Emelyanov 
206848f6a7a5SPavel Emelyanov 	tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
206948f6a7a5SPavel Emelyanov 	if (!tp->task)
207048f6a7a5SPavel Emelyanov 		return ERR_PTR(-ESRCH);
207148f6a7a5SPavel Emelyanov 
207248f6a7a5SPavel Emelyanov 	tp->sighand = lock_task_sighand(tp->task, &tp->flags);
207348f6a7a5SPavel Emelyanov 	if (!tp->sighand)
207448f6a7a5SPavel Emelyanov 		return ERR_PTR(-ESRCH);
207548f6a7a5SPavel Emelyanov 
207648f6a7a5SPavel Emelyanov 	return seq_list_start(&tp->task->signal->posix_timers, *pos);
207748f6a7a5SPavel Emelyanov }
207848f6a7a5SPavel Emelyanov 
207948f6a7a5SPavel Emelyanov static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
208048f6a7a5SPavel Emelyanov {
208148f6a7a5SPavel Emelyanov 	struct timers_private *tp = m->private;
208248f6a7a5SPavel Emelyanov 	return seq_list_next(v, &tp->task->signal->posix_timers, pos);
208348f6a7a5SPavel Emelyanov }
208448f6a7a5SPavel Emelyanov 
208548f6a7a5SPavel Emelyanov static void timers_stop(struct seq_file *m, void *v)
208648f6a7a5SPavel Emelyanov {
208748f6a7a5SPavel Emelyanov 	struct timers_private *tp = m->private;
208848f6a7a5SPavel Emelyanov 
208948f6a7a5SPavel Emelyanov 	if (tp->sighand) {
209048f6a7a5SPavel Emelyanov 		unlock_task_sighand(tp->task, &tp->flags);
209148f6a7a5SPavel Emelyanov 		tp->sighand = NULL;
209248f6a7a5SPavel Emelyanov 	}
209348f6a7a5SPavel Emelyanov 
209448f6a7a5SPavel Emelyanov 	if (tp->task) {
209548f6a7a5SPavel Emelyanov 		put_task_struct(tp->task);
209648f6a7a5SPavel Emelyanov 		tp->task = NULL;
209748f6a7a5SPavel Emelyanov 	}
209848f6a7a5SPavel Emelyanov }
209948f6a7a5SPavel Emelyanov 
210048f6a7a5SPavel Emelyanov static int show_timer(struct seq_file *m, void *v)
210148f6a7a5SPavel Emelyanov {
210248f6a7a5SPavel Emelyanov 	struct k_itimer *timer;
210357b8015eSPavel Emelyanov 	struct timers_private *tp = m->private;
210457b8015eSPavel Emelyanov 	int notify;
210557b8015eSPavel Emelyanov 	static char *nstr[] = {
210657b8015eSPavel Emelyanov 		[SIGEV_SIGNAL] = "signal",
210757b8015eSPavel Emelyanov 		[SIGEV_NONE] = "none",
210857b8015eSPavel Emelyanov 		[SIGEV_THREAD] = "thread",
210957b8015eSPavel Emelyanov 	};
211048f6a7a5SPavel Emelyanov 
211148f6a7a5SPavel Emelyanov 	timer = list_entry((struct list_head *)v, struct k_itimer, list);
211257b8015eSPavel Emelyanov 	notify = timer->it_sigev_notify;
211357b8015eSPavel Emelyanov 
211448f6a7a5SPavel Emelyanov 	seq_printf(m, "ID: %d\n", timer->it_id);
211557b8015eSPavel Emelyanov 	seq_printf(m, "signal: %d/%p\n", timer->sigq->info.si_signo,
211657b8015eSPavel Emelyanov 			timer->sigq->info.si_value.sival_ptr);
211757b8015eSPavel Emelyanov 	seq_printf(m, "notify: %s/%s.%d\n",
211857b8015eSPavel Emelyanov 		nstr[notify & ~SIGEV_THREAD_ID],
211957b8015eSPavel Emelyanov 		(notify & SIGEV_THREAD_ID) ? "tid" : "pid",
212057b8015eSPavel Emelyanov 		pid_nr_ns(timer->it_pid, tp->ns));
2121*15ef0298SPavel Tikhomirov 	seq_printf(m, "ClockID: %d\n", timer->it_clock);
212248f6a7a5SPavel Emelyanov 
212348f6a7a5SPavel Emelyanov 	return 0;
212448f6a7a5SPavel Emelyanov }
212548f6a7a5SPavel Emelyanov 
212648f6a7a5SPavel Emelyanov static const struct seq_operations proc_timers_seq_ops = {
212748f6a7a5SPavel Emelyanov 	.start	= timers_start,
212848f6a7a5SPavel Emelyanov 	.next	= timers_next,
212948f6a7a5SPavel Emelyanov 	.stop	= timers_stop,
213048f6a7a5SPavel Emelyanov 	.show	= show_timer,
213148f6a7a5SPavel Emelyanov };
213248f6a7a5SPavel Emelyanov 
213348f6a7a5SPavel Emelyanov static int proc_timers_open(struct inode *inode, struct file *file)
213448f6a7a5SPavel Emelyanov {
213548f6a7a5SPavel Emelyanov 	struct timers_private *tp;
213648f6a7a5SPavel Emelyanov 
213748f6a7a5SPavel Emelyanov 	tp = __seq_open_private(file, &proc_timers_seq_ops,
213848f6a7a5SPavel Emelyanov 			sizeof(struct timers_private));
213948f6a7a5SPavel Emelyanov 	if (!tp)
214048f6a7a5SPavel Emelyanov 		return -ENOMEM;
214148f6a7a5SPavel Emelyanov 
214248f6a7a5SPavel Emelyanov 	tp->pid = proc_pid(inode);
214357b8015eSPavel Emelyanov 	tp->ns = inode->i_sb->s_fs_info;
214448f6a7a5SPavel Emelyanov 	return 0;
214548f6a7a5SPavel Emelyanov }
214648f6a7a5SPavel Emelyanov 
214748f6a7a5SPavel Emelyanov static const struct file_operations proc_timers_operations = {
214848f6a7a5SPavel Emelyanov 	.open		= proc_timers_open,
214948f6a7a5SPavel Emelyanov 	.read		= seq_read,
215048f6a7a5SPavel Emelyanov 	.llseek		= seq_lseek,
215148f6a7a5SPavel Emelyanov 	.release	= seq_release_private,
215248f6a7a5SPavel Emelyanov };
2153640708a2SPavel Emelyanov #endif /* CONFIG_CHECKPOINT_RESTORE */
2154640708a2SPavel Emelyanov 
2155444ceed8SEric W. Biederman static struct dentry *proc_pident_instantiate(struct inode *dir,
2156c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
2157444ceed8SEric W. Biederman {
2158c5141e6dSEric Dumazet 	const struct pid_entry *p = ptr;
2159444ceed8SEric W. Biederman 	struct inode *inode;
2160444ceed8SEric W. Biederman 	struct proc_inode *ei;
2161bd6daba9SKOSAKI Motohiro 	struct dentry *error = ERR_PTR(-ENOENT);
2162444ceed8SEric W. Biederman 
216361a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
2164444ceed8SEric W. Biederman 	if (!inode)
2165444ceed8SEric W. Biederman 		goto out;
2166444ceed8SEric W. Biederman 
2167444ceed8SEric W. Biederman 	ei = PROC_I(inode);
2168444ceed8SEric W. Biederman 	inode->i_mode = p->mode;
2169444ceed8SEric W. Biederman 	if (S_ISDIR(inode->i_mode))
2170bfe86848SMiklos Szeredi 		set_nlink(inode, 2);	/* Use getattr to fix if necessary */
2171444ceed8SEric W. Biederman 	if (p->iop)
2172444ceed8SEric W. Biederman 		inode->i_op = p->iop;
2173444ceed8SEric W. Biederman 	if (p->fop)
2174444ceed8SEric W. Biederman 		inode->i_fop = p->fop;
2175444ceed8SEric W. Biederman 	ei->op = p->op;
2176fb045adbSNick Piggin 	d_set_d_op(dentry, &pid_dentry_operations);
2177444ceed8SEric W. Biederman 	d_add(dentry, inode);
2178444ceed8SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
21790b728e19SAl Viro 	if (pid_revalidate(dentry, 0))
2180444ceed8SEric W. Biederman 		error = NULL;
2181444ceed8SEric W. Biederman out:
2182444ceed8SEric W. Biederman 	return error;
2183444ceed8SEric W. Biederman }
2184444ceed8SEric W. Biederman 
21851da177e4SLinus Torvalds static struct dentry *proc_pident_lookup(struct inode *dir,
21861da177e4SLinus Torvalds 					 struct dentry *dentry,
2187c5141e6dSEric Dumazet 					 const struct pid_entry *ents,
21887bcd6b0eSEric W. Biederman 					 unsigned int nents)
21891da177e4SLinus Torvalds {
2190cd6a3ce9SEric W. Biederman 	struct dentry *error;
219199f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(dir);
2192c5141e6dSEric Dumazet 	const struct pid_entry *p, *last;
21931da177e4SLinus Torvalds 
2194cd6a3ce9SEric W. Biederman 	error = ERR_PTR(-ENOENT);
21951da177e4SLinus Torvalds 
219699f89551SEric W. Biederman 	if (!task)
219799f89551SEric W. Biederman 		goto out_no_task;
21981da177e4SLinus Torvalds 
219920cdc894SEric W. Biederman 	/*
220020cdc894SEric W. Biederman 	 * Yes, it does not scale. And it should not. Don't add
220120cdc894SEric W. Biederman 	 * new entries into /proc/<tgid>/ without very good reasons.
220220cdc894SEric W. Biederman 	 */
22037bcd6b0eSEric W. Biederman 	last = &ents[nents - 1];
22047bcd6b0eSEric W. Biederman 	for (p = ents; p <= last; p++) {
22051da177e4SLinus Torvalds 		if (p->len != dentry->d_name.len)
22061da177e4SLinus Torvalds 			continue;
22071da177e4SLinus Torvalds 		if (!memcmp(dentry->d_name.name, p->name, p->len))
22081da177e4SLinus Torvalds 			break;
22091da177e4SLinus Torvalds 	}
22107bcd6b0eSEric W. Biederman 	if (p > last)
22111da177e4SLinus Torvalds 		goto out;
22121da177e4SLinus Torvalds 
2213444ceed8SEric W. Biederman 	error = proc_pident_instantiate(dir, dentry, task, p);
22141da177e4SLinus Torvalds out:
221599f89551SEric W. Biederman 	put_task_struct(task);
221699f89551SEric W. Biederman out_no_task:
2217cd6a3ce9SEric W. Biederman 	return error;
22181da177e4SLinus Torvalds }
22191da177e4SLinus Torvalds 
2220c5141e6dSEric Dumazet static int proc_pident_fill_cache(struct file *filp, void *dirent,
2221c5141e6dSEric Dumazet 	filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
222261a28784SEric W. Biederman {
222361a28784SEric W. Biederman 	return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
222461a28784SEric W. Biederman 				proc_pident_instantiate, task, p);
222561a28784SEric W. Biederman }
222661a28784SEric W. Biederman 
222728a6d671SEric W. Biederman static int proc_pident_readdir(struct file *filp,
222828a6d671SEric W. Biederman 		void *dirent, filldir_t filldir,
2229c5141e6dSEric Dumazet 		const struct pid_entry *ents, unsigned int nents)
223028a6d671SEric W. Biederman {
223128a6d671SEric W. Biederman 	int i;
22322fddfeefSJosef "Jeff" Sipek 	struct dentry *dentry = filp->f_path.dentry;
223328a6d671SEric W. Biederman 	struct inode *inode = dentry->d_inode;
223428a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
2235c5141e6dSEric Dumazet 	const struct pid_entry *p, *last;
223628a6d671SEric W. Biederman 	ino_t ino;
223728a6d671SEric W. Biederman 	int ret;
223828a6d671SEric W. Biederman 
223928a6d671SEric W. Biederman 	ret = -ENOENT;
224028a6d671SEric W. Biederman 	if (!task)
224161a28784SEric W. Biederman 		goto out_no_task;
224228a6d671SEric W. Biederman 
224328a6d671SEric W. Biederman 	ret = 0;
224428a6d671SEric W. Biederman 	i = filp->f_pos;
224528a6d671SEric W. Biederman 	switch (i) {
224628a6d671SEric W. Biederman 	case 0:
224728a6d671SEric W. Biederman 		ino = inode->i_ino;
224828a6d671SEric W. Biederman 		if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
224928a6d671SEric W. Biederman 			goto out;
225028a6d671SEric W. Biederman 		i++;
225128a6d671SEric W. Biederman 		filp->f_pos++;
225228a6d671SEric W. Biederman 		/* fall through */
225328a6d671SEric W. Biederman 	case 1:
225428a6d671SEric W. Biederman 		ino = parent_ino(dentry);
225528a6d671SEric W. Biederman 		if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
225628a6d671SEric W. Biederman 			goto out;
225728a6d671SEric W. Biederman 		i++;
225828a6d671SEric W. Biederman 		filp->f_pos++;
225928a6d671SEric W. Biederman 		/* fall through */
226028a6d671SEric W. Biederman 	default:
226128a6d671SEric W. Biederman 		i -= 2;
226228a6d671SEric W. Biederman 		if (i >= nents) {
226328a6d671SEric W. Biederman 			ret = 1;
226428a6d671SEric W. Biederman 			goto out;
226528a6d671SEric W. Biederman 		}
226628a6d671SEric W. Biederman 		p = ents + i;
22677bcd6b0eSEric W. Biederman 		last = &ents[nents - 1];
22687bcd6b0eSEric W. Biederman 		while (p <= last) {
226961a28784SEric W. Biederman 			if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
227028a6d671SEric W. Biederman 				goto out;
227128a6d671SEric W. Biederman 			filp->f_pos++;
227228a6d671SEric W. Biederman 			p++;
227328a6d671SEric W. Biederman 		}
22741da177e4SLinus Torvalds 	}
22751da177e4SLinus Torvalds 
227628a6d671SEric W. Biederman 	ret = 1;
227728a6d671SEric W. Biederman out:
227861a28784SEric W. Biederman 	put_task_struct(task);
227961a28784SEric W. Biederman out_no_task:
228028a6d671SEric W. Biederman 	return ret;
22811da177e4SLinus Torvalds }
22821da177e4SLinus Torvalds 
22831da177e4SLinus Torvalds #ifdef CONFIG_SECURITY
228428a6d671SEric W. Biederman static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
228528a6d671SEric W. Biederman 				  size_t count, loff_t *ppos)
228628a6d671SEric W. Biederman {
2287496ad9aaSAl Viro 	struct inode * inode = file_inode(file);
228804ff9708SAl Viro 	char *p = NULL;
228928a6d671SEric W. Biederman 	ssize_t length;
229028a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
229128a6d671SEric W. Biederman 
229228a6d671SEric W. Biederman 	if (!task)
229304ff9708SAl Viro 		return -ESRCH;
229428a6d671SEric W. Biederman 
229528a6d671SEric W. Biederman 	length = security_getprocattr(task,
22962fddfeefSJosef "Jeff" Sipek 				      (char*)file->f_path.dentry->d_name.name,
229704ff9708SAl Viro 				      &p);
229828a6d671SEric W. Biederman 	put_task_struct(task);
229904ff9708SAl Viro 	if (length > 0)
230004ff9708SAl Viro 		length = simple_read_from_buffer(buf, count, ppos, p, length);
230104ff9708SAl Viro 	kfree(p);
230228a6d671SEric W. Biederman 	return length;
230328a6d671SEric W. Biederman }
230428a6d671SEric W. Biederman 
230528a6d671SEric W. Biederman static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
230628a6d671SEric W. Biederman 				   size_t count, loff_t *ppos)
230728a6d671SEric W. Biederman {
2308496ad9aaSAl Viro 	struct inode * inode = file_inode(file);
230928a6d671SEric W. Biederman 	char *page;
231028a6d671SEric W. Biederman 	ssize_t length;
231128a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
231228a6d671SEric W. Biederman 
231328a6d671SEric W. Biederman 	length = -ESRCH;
231428a6d671SEric W. Biederman 	if (!task)
231528a6d671SEric W. Biederman 		goto out_no_task;
231628a6d671SEric W. Biederman 	if (count > PAGE_SIZE)
231728a6d671SEric W. Biederman 		count = PAGE_SIZE;
231828a6d671SEric W. Biederman 
231928a6d671SEric W. Biederman 	/* No partial writes. */
232028a6d671SEric W. Biederman 	length = -EINVAL;
232128a6d671SEric W. Biederman 	if (*ppos != 0)
232228a6d671SEric W. Biederman 		goto out;
232328a6d671SEric W. Biederman 
232428a6d671SEric W. Biederman 	length = -ENOMEM;
2325e12ba74dSMel Gorman 	page = (char*)__get_free_page(GFP_TEMPORARY);
232628a6d671SEric W. Biederman 	if (!page)
232728a6d671SEric W. Biederman 		goto out;
232828a6d671SEric W. Biederman 
232928a6d671SEric W. Biederman 	length = -EFAULT;
233028a6d671SEric W. Biederman 	if (copy_from_user(page, buf, count))
233128a6d671SEric W. Biederman 		goto out_free;
233228a6d671SEric W. Biederman 
2333107db7c7SDavid Howells 	/* Guard against adverse ptrace interaction */
23349b1bf12dSKOSAKI Motohiro 	length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
2335107db7c7SDavid Howells 	if (length < 0)
2336107db7c7SDavid Howells 		goto out_free;
2337107db7c7SDavid Howells 
233828a6d671SEric W. Biederman 	length = security_setprocattr(task,
23392fddfeefSJosef "Jeff" Sipek 				      (char*)file->f_path.dentry->d_name.name,
234028a6d671SEric W. Biederman 				      (void*)page, count);
23419b1bf12dSKOSAKI Motohiro 	mutex_unlock(&task->signal->cred_guard_mutex);
234228a6d671SEric W. Biederman out_free:
234328a6d671SEric W. Biederman 	free_page((unsigned long) page);
234428a6d671SEric W. Biederman out:
234528a6d671SEric W. Biederman 	put_task_struct(task);
234628a6d671SEric W. Biederman out_no_task:
234728a6d671SEric W. Biederman 	return length;
234828a6d671SEric W. Biederman }
234928a6d671SEric W. Biederman 
235000977a59SArjan van de Ven static const struct file_operations proc_pid_attr_operations = {
235128a6d671SEric W. Biederman 	.read		= proc_pid_attr_read,
235228a6d671SEric W. Biederman 	.write		= proc_pid_attr_write,
235387df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
235428a6d671SEric W. Biederman };
235528a6d671SEric W. Biederman 
2356c5141e6dSEric Dumazet static const struct pid_entry attr_dir_stuff[] = {
2357631f9c18SAlexey Dobriyan 	REG("current",    S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2358631f9c18SAlexey Dobriyan 	REG("prev",       S_IRUGO,	   proc_pid_attr_operations),
2359631f9c18SAlexey Dobriyan 	REG("exec",       S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2360631f9c18SAlexey Dobriyan 	REG("fscreate",   S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2361631f9c18SAlexey Dobriyan 	REG("keycreate",  S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2362631f9c18SAlexey Dobriyan 	REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
236328a6d671SEric W. Biederman };
236428a6d671SEric W. Biederman 
236572d9dcfcSEric W. Biederman static int proc_attr_dir_readdir(struct file * filp,
23661da177e4SLinus Torvalds 			     void * dirent, filldir_t filldir)
23671da177e4SLinus Torvalds {
23681da177e4SLinus Torvalds 	return proc_pident_readdir(filp,dirent,filldir,
236972d9dcfcSEric W. Biederman 				   attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
23701da177e4SLinus Torvalds }
23711da177e4SLinus Torvalds 
237200977a59SArjan van de Ven static const struct file_operations proc_attr_dir_operations = {
23731da177e4SLinus Torvalds 	.read		= generic_read_dir,
237472d9dcfcSEric W. Biederman 	.readdir	= proc_attr_dir_readdir,
23756038f373SArnd Bergmann 	.llseek		= default_llseek,
23761da177e4SLinus Torvalds };
23771da177e4SLinus Torvalds 
237872d9dcfcSEric W. Biederman static struct dentry *proc_attr_dir_lookup(struct inode *dir,
237900cd8dd3SAl Viro 				struct dentry *dentry, unsigned int flags)
23801da177e4SLinus Torvalds {
23817bcd6b0eSEric W. Biederman 	return proc_pident_lookup(dir, dentry,
23827bcd6b0eSEric W. Biederman 				  attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
23831da177e4SLinus Torvalds }
23841da177e4SLinus Torvalds 
2385c5ef1c42SArjan van de Ven static const struct inode_operations proc_attr_dir_inode_operations = {
238672d9dcfcSEric W. Biederman 	.lookup		= proc_attr_dir_lookup,
238799f89551SEric W. Biederman 	.getattr	= pid_getattr,
23886d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
23891da177e4SLinus Torvalds };
23901da177e4SLinus Torvalds 
23911da177e4SLinus Torvalds #endif
23921da177e4SLinus Torvalds 
2393698ba7b5SChristoph Hellwig #ifdef CONFIG_ELF_CORE
23943cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
23953cb4a0bbSKawai, Hidehiro 					 size_t count, loff_t *ppos)
23963cb4a0bbSKawai, Hidehiro {
2397496ad9aaSAl Viro 	struct task_struct *task = get_proc_task(file_inode(file));
23983cb4a0bbSKawai, Hidehiro 	struct mm_struct *mm;
23993cb4a0bbSKawai, Hidehiro 	char buffer[PROC_NUMBUF];
24003cb4a0bbSKawai, Hidehiro 	size_t len;
24013cb4a0bbSKawai, Hidehiro 	int ret;
24023cb4a0bbSKawai, Hidehiro 
24033cb4a0bbSKawai, Hidehiro 	if (!task)
24043cb4a0bbSKawai, Hidehiro 		return -ESRCH;
24053cb4a0bbSKawai, Hidehiro 
24063cb4a0bbSKawai, Hidehiro 	ret = 0;
24073cb4a0bbSKawai, Hidehiro 	mm = get_task_mm(task);
24083cb4a0bbSKawai, Hidehiro 	if (mm) {
24093cb4a0bbSKawai, Hidehiro 		len = snprintf(buffer, sizeof(buffer), "%08lx\n",
24103cb4a0bbSKawai, Hidehiro 			       ((mm->flags & MMF_DUMP_FILTER_MASK) >>
24113cb4a0bbSKawai, Hidehiro 				MMF_DUMP_FILTER_SHIFT));
24123cb4a0bbSKawai, Hidehiro 		mmput(mm);
24133cb4a0bbSKawai, Hidehiro 		ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
24143cb4a0bbSKawai, Hidehiro 	}
24153cb4a0bbSKawai, Hidehiro 
24163cb4a0bbSKawai, Hidehiro 	put_task_struct(task);
24173cb4a0bbSKawai, Hidehiro 
24183cb4a0bbSKawai, Hidehiro 	return ret;
24193cb4a0bbSKawai, Hidehiro }
24203cb4a0bbSKawai, Hidehiro 
24213cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_write(struct file *file,
24223cb4a0bbSKawai, Hidehiro 					  const char __user *buf,
24233cb4a0bbSKawai, Hidehiro 					  size_t count,
24243cb4a0bbSKawai, Hidehiro 					  loff_t *ppos)
24253cb4a0bbSKawai, Hidehiro {
24263cb4a0bbSKawai, Hidehiro 	struct task_struct *task;
24273cb4a0bbSKawai, Hidehiro 	struct mm_struct *mm;
24283cb4a0bbSKawai, Hidehiro 	char buffer[PROC_NUMBUF], *end;
24293cb4a0bbSKawai, Hidehiro 	unsigned int val;
24303cb4a0bbSKawai, Hidehiro 	int ret;
24313cb4a0bbSKawai, Hidehiro 	int i;
24323cb4a0bbSKawai, Hidehiro 	unsigned long mask;
24333cb4a0bbSKawai, Hidehiro 
24343cb4a0bbSKawai, Hidehiro 	ret = -EFAULT;
24353cb4a0bbSKawai, Hidehiro 	memset(buffer, 0, sizeof(buffer));
24363cb4a0bbSKawai, Hidehiro 	if (count > sizeof(buffer) - 1)
24373cb4a0bbSKawai, Hidehiro 		count = sizeof(buffer) - 1;
24383cb4a0bbSKawai, Hidehiro 	if (copy_from_user(buffer, buf, count))
24393cb4a0bbSKawai, Hidehiro 		goto out_no_task;
24403cb4a0bbSKawai, Hidehiro 
24413cb4a0bbSKawai, Hidehiro 	ret = -EINVAL;
24423cb4a0bbSKawai, Hidehiro 	val = (unsigned int)simple_strtoul(buffer, &end, 0);
24433cb4a0bbSKawai, Hidehiro 	if (*end == '\n')
24443cb4a0bbSKawai, Hidehiro 		end++;
24453cb4a0bbSKawai, Hidehiro 	if (end - buffer == 0)
24463cb4a0bbSKawai, Hidehiro 		goto out_no_task;
24473cb4a0bbSKawai, Hidehiro 
24483cb4a0bbSKawai, Hidehiro 	ret = -ESRCH;
2449496ad9aaSAl Viro 	task = get_proc_task(file_inode(file));
24503cb4a0bbSKawai, Hidehiro 	if (!task)
24513cb4a0bbSKawai, Hidehiro 		goto out_no_task;
24523cb4a0bbSKawai, Hidehiro 
24533cb4a0bbSKawai, Hidehiro 	ret = end - buffer;
24543cb4a0bbSKawai, Hidehiro 	mm = get_task_mm(task);
24553cb4a0bbSKawai, Hidehiro 	if (!mm)
24563cb4a0bbSKawai, Hidehiro 		goto out_no_mm;
24573cb4a0bbSKawai, Hidehiro 
24583cb4a0bbSKawai, Hidehiro 	for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
24593cb4a0bbSKawai, Hidehiro 		if (val & mask)
24603cb4a0bbSKawai, Hidehiro 			set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
24613cb4a0bbSKawai, Hidehiro 		else
24623cb4a0bbSKawai, Hidehiro 			clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
24633cb4a0bbSKawai, Hidehiro 	}
24643cb4a0bbSKawai, Hidehiro 
24653cb4a0bbSKawai, Hidehiro 	mmput(mm);
24663cb4a0bbSKawai, Hidehiro  out_no_mm:
24673cb4a0bbSKawai, Hidehiro 	put_task_struct(task);
24683cb4a0bbSKawai, Hidehiro  out_no_task:
24693cb4a0bbSKawai, Hidehiro 	return ret;
24703cb4a0bbSKawai, Hidehiro }
24713cb4a0bbSKawai, Hidehiro 
24723cb4a0bbSKawai, Hidehiro static const struct file_operations proc_coredump_filter_operations = {
24733cb4a0bbSKawai, Hidehiro 	.read		= proc_coredump_filter_read,
24743cb4a0bbSKawai, Hidehiro 	.write		= proc_coredump_filter_write,
247587df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
24763cb4a0bbSKawai, Hidehiro };
24773cb4a0bbSKawai, Hidehiro #endif
24783cb4a0bbSKawai, Hidehiro 
2479aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING
2480297c5d92SAndrea Righi static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
2481aba76fdbSAndrew Morton {
2482940389b8SAndrea Righi 	struct task_io_accounting acct = task->ioac;
2483297c5d92SAndrea Righi 	unsigned long flags;
2484293eb1e7SVasiliy Kulikov 	int result;
2485297c5d92SAndrea Righi 
2486293eb1e7SVasiliy Kulikov 	result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2487293eb1e7SVasiliy Kulikov 	if (result)
2488293eb1e7SVasiliy Kulikov 		return result;
2489293eb1e7SVasiliy Kulikov 
2490293eb1e7SVasiliy Kulikov 	if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
2491293eb1e7SVasiliy Kulikov 		result = -EACCES;
2492293eb1e7SVasiliy Kulikov 		goto out_unlock;
2493293eb1e7SVasiliy Kulikov 	}
24941d1221f3SVasiliy Kulikov 
24955995477aSAndrea Righi 	if (whole && lock_task_sighand(task, &flags)) {
2496b2d002dbSAndrea Righi 		struct task_struct *t = task;
2497297c5d92SAndrea Righi 
24985995477aSAndrea Righi 		task_io_accounting_add(&acct, &task->signal->ioac);
24995995477aSAndrea Righi 		while_each_thread(task, t)
25005995477aSAndrea Righi 			task_io_accounting_add(&acct, &t->ioac);
2501297c5d92SAndrea Righi 
2502297c5d92SAndrea Righi 		unlock_task_sighand(task, &flags);
2503297c5d92SAndrea Righi 	}
2504293eb1e7SVasiliy Kulikov 	result = sprintf(buffer,
2505aba76fdbSAndrew Morton 			"rchar: %llu\n"
2506aba76fdbSAndrew Morton 			"wchar: %llu\n"
2507aba76fdbSAndrew Morton 			"syscr: %llu\n"
2508aba76fdbSAndrew Morton 			"syscw: %llu\n"
2509aba76fdbSAndrew Morton 			"read_bytes: %llu\n"
2510aba76fdbSAndrew Morton 			"write_bytes: %llu\n"
2511aba76fdbSAndrew Morton 			"cancelled_write_bytes: %llu\n",
25127c44319dSAlexander Beregalov 			(unsigned long long)acct.rchar,
25137c44319dSAlexander Beregalov 			(unsigned long long)acct.wchar,
25147c44319dSAlexander Beregalov 			(unsigned long long)acct.syscr,
25157c44319dSAlexander Beregalov 			(unsigned long long)acct.syscw,
25167c44319dSAlexander Beregalov 			(unsigned long long)acct.read_bytes,
25177c44319dSAlexander Beregalov 			(unsigned long long)acct.write_bytes,
25187c44319dSAlexander Beregalov 			(unsigned long long)acct.cancelled_write_bytes);
2519293eb1e7SVasiliy Kulikov out_unlock:
2520293eb1e7SVasiliy Kulikov 	mutex_unlock(&task->signal->cred_guard_mutex);
2521293eb1e7SVasiliy Kulikov 	return result;
2522aba76fdbSAndrew Morton }
2523297c5d92SAndrea Righi 
2524297c5d92SAndrea Righi static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2525297c5d92SAndrea Righi {
2526297c5d92SAndrea Righi 	return do_io_accounting(task, buffer, 0);
2527297c5d92SAndrea Righi }
2528297c5d92SAndrea Righi 
2529297c5d92SAndrea Righi static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2530297c5d92SAndrea Righi {
2531297c5d92SAndrea Righi 	return do_io_accounting(task, buffer, 1);
2532297c5d92SAndrea Righi }
2533297c5d92SAndrea Righi #endif /* CONFIG_TASK_IO_ACCOUNTING */
2534aba76fdbSAndrew Morton 
253522d917d8SEric W. Biederman #ifdef CONFIG_USER_NS
253622d917d8SEric W. Biederman static int proc_id_map_open(struct inode *inode, struct file *file,
253722d917d8SEric W. Biederman 	struct seq_operations *seq_ops)
253822d917d8SEric W. Biederman {
253922d917d8SEric W. Biederman 	struct user_namespace *ns = NULL;
254022d917d8SEric W. Biederman 	struct task_struct *task;
254122d917d8SEric W. Biederman 	struct seq_file *seq;
254222d917d8SEric W. Biederman 	int ret = -EINVAL;
254322d917d8SEric W. Biederman 
254422d917d8SEric W. Biederman 	task = get_proc_task(inode);
254522d917d8SEric W. Biederman 	if (task) {
254622d917d8SEric W. Biederman 		rcu_read_lock();
254722d917d8SEric W. Biederman 		ns = get_user_ns(task_cred_xxx(task, user_ns));
254822d917d8SEric W. Biederman 		rcu_read_unlock();
254922d917d8SEric W. Biederman 		put_task_struct(task);
255022d917d8SEric W. Biederman 	}
255122d917d8SEric W. Biederman 	if (!ns)
255222d917d8SEric W. Biederman 		goto err;
255322d917d8SEric W. Biederman 
255422d917d8SEric W. Biederman 	ret = seq_open(file, seq_ops);
255522d917d8SEric W. Biederman 	if (ret)
255622d917d8SEric W. Biederman 		goto err_put_ns;
255722d917d8SEric W. Biederman 
255822d917d8SEric W. Biederman 	seq = file->private_data;
255922d917d8SEric W. Biederman 	seq->private = ns;
256022d917d8SEric W. Biederman 
256122d917d8SEric W. Biederman 	return 0;
256222d917d8SEric W. Biederman err_put_ns:
256322d917d8SEric W. Biederman 	put_user_ns(ns);
256422d917d8SEric W. Biederman err:
256522d917d8SEric W. Biederman 	return ret;
256622d917d8SEric W. Biederman }
256722d917d8SEric W. Biederman 
256822d917d8SEric W. Biederman static int proc_id_map_release(struct inode *inode, struct file *file)
256922d917d8SEric W. Biederman {
257022d917d8SEric W. Biederman 	struct seq_file *seq = file->private_data;
257122d917d8SEric W. Biederman 	struct user_namespace *ns = seq->private;
257222d917d8SEric W. Biederman 	put_user_ns(ns);
257322d917d8SEric W. Biederman 	return seq_release(inode, file);
257422d917d8SEric W. Biederman }
257522d917d8SEric W. Biederman 
257622d917d8SEric W. Biederman static int proc_uid_map_open(struct inode *inode, struct file *file)
257722d917d8SEric W. Biederman {
257822d917d8SEric W. Biederman 	return proc_id_map_open(inode, file, &proc_uid_seq_operations);
257922d917d8SEric W. Biederman }
258022d917d8SEric W. Biederman 
258122d917d8SEric W. Biederman static int proc_gid_map_open(struct inode *inode, struct file *file)
258222d917d8SEric W. Biederman {
258322d917d8SEric W. Biederman 	return proc_id_map_open(inode, file, &proc_gid_seq_operations);
258422d917d8SEric W. Biederman }
258522d917d8SEric W. Biederman 
2586f76d207aSEric W. Biederman static int proc_projid_map_open(struct inode *inode, struct file *file)
2587f76d207aSEric W. Biederman {
2588f76d207aSEric W. Biederman 	return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2589f76d207aSEric W. Biederman }
2590f76d207aSEric W. Biederman 
259122d917d8SEric W. Biederman static const struct file_operations proc_uid_map_operations = {
259222d917d8SEric W. Biederman 	.open		= proc_uid_map_open,
259322d917d8SEric W. Biederman 	.write		= proc_uid_map_write,
259422d917d8SEric W. Biederman 	.read		= seq_read,
259522d917d8SEric W. Biederman 	.llseek		= seq_lseek,
259622d917d8SEric W. Biederman 	.release	= proc_id_map_release,
259722d917d8SEric W. Biederman };
259822d917d8SEric W. Biederman 
259922d917d8SEric W. Biederman static const struct file_operations proc_gid_map_operations = {
260022d917d8SEric W. Biederman 	.open		= proc_gid_map_open,
260122d917d8SEric W. Biederman 	.write		= proc_gid_map_write,
260222d917d8SEric W. Biederman 	.read		= seq_read,
260322d917d8SEric W. Biederman 	.llseek		= seq_lseek,
260422d917d8SEric W. Biederman 	.release	= proc_id_map_release,
260522d917d8SEric W. Biederman };
2606f76d207aSEric W. Biederman 
2607f76d207aSEric W. Biederman static const struct file_operations proc_projid_map_operations = {
2608f76d207aSEric W. Biederman 	.open		= proc_projid_map_open,
2609f76d207aSEric W. Biederman 	.write		= proc_projid_map_write,
2610f76d207aSEric W. Biederman 	.read		= seq_read,
2611f76d207aSEric W. Biederman 	.llseek		= seq_lseek,
2612f76d207aSEric W. Biederman 	.release	= proc_id_map_release,
2613f76d207aSEric W. Biederman };
261422d917d8SEric W. Biederman #endif /* CONFIG_USER_NS */
261522d917d8SEric W. Biederman 
261647830723SKees Cook static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
261747830723SKees Cook 				struct pid *pid, struct task_struct *task)
261847830723SKees Cook {
2619a9712bc1SAl Viro 	int err = lock_trace(task);
2620a9712bc1SAl Viro 	if (!err) {
262147830723SKees Cook 		seq_printf(m, "%08x\n", task->personality);
2622a9712bc1SAl Viro 		unlock_trace(task);
2623a9712bc1SAl Viro 	}
2624a9712bc1SAl Viro 	return err;
262547830723SKees Cook }
262647830723SKees Cook 
2627801199ceSEric W. Biederman /*
262828a6d671SEric W. Biederman  * Thread groups
262928a6d671SEric W. Biederman  */
263000977a59SArjan van de Ven static const struct file_operations proc_task_operations;
2631c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations;
263220cdc894SEric W. Biederman 
2633c5141e6dSEric Dumazet static const struct pid_entry tgid_base_stuff[] = {
2634631f9c18SAlexey Dobriyan 	DIR("task",       S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2635631f9c18SAlexey Dobriyan 	DIR("fd",         S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2636640708a2SPavel Emelyanov #ifdef CONFIG_CHECKPOINT_RESTORE
2637640708a2SPavel Emelyanov 	DIR("map_files",  S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
2638640708a2SPavel Emelyanov #endif
2639631f9c18SAlexey Dobriyan 	DIR("fdinfo",     S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
26406b4e306aSEric W. Biederman 	DIR("ns",	  S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
2641b2211a36SAndrew Morton #ifdef CONFIG_NET
2642631f9c18SAlexey Dobriyan 	DIR("net",        S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2643b2211a36SAndrew Morton #endif
2644631f9c18SAlexey Dobriyan 	REG("environ",    S_IRUSR, proc_environ_operations),
2645631f9c18SAlexey Dobriyan 	INF("auxv",       S_IRUSR, proc_pid_auxv),
2646631f9c18SAlexey Dobriyan 	ONE("status",     S_IRUGO, proc_pid_status),
2647a9712bc1SAl Viro 	ONE("personality", S_IRUGO, proc_pid_personality),
26483036e7b4SJiri Olsa 	INF("limits",	  S_IRUGO, proc_pid_limits),
264943ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
2650631f9c18SAlexey Dobriyan 	REG("sched",      S_IRUGO|S_IWUSR, proc_pid_sched_operations),
265143ae34cbSIngo Molnar #endif
26525091faa4SMike Galbraith #ifdef CONFIG_SCHED_AUTOGROUP
26535091faa4SMike Galbraith 	REG("autogroup",  S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
26545091faa4SMike Galbraith #endif
26554614a696Sjohn stultz 	REG("comm",      S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2656ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2657a9712bc1SAl Viro 	INF("syscall",    S_IRUGO, proc_pid_syscall),
2658ebcb6734SRoland McGrath #endif
2659631f9c18SAlexey Dobriyan 	INF("cmdline",    S_IRUGO, proc_pid_cmdline),
2660631f9c18SAlexey Dobriyan 	ONE("stat",       S_IRUGO, proc_tgid_stat),
2661631f9c18SAlexey Dobriyan 	ONE("statm",      S_IRUGO, proc_pid_statm),
2662b7643757SSiddhesh Poyarekar 	REG("maps",       S_IRUGO, proc_pid_maps_operations),
266328a6d671SEric W. Biederman #ifdef CONFIG_NUMA
2664b7643757SSiddhesh Poyarekar 	REG("numa_maps",  S_IRUGO, proc_pid_numa_maps_operations),
266528a6d671SEric W. Biederman #endif
2666631f9c18SAlexey Dobriyan 	REG("mem",        S_IRUSR|S_IWUSR, proc_mem_operations),
2667631f9c18SAlexey Dobriyan 	LNK("cwd",        proc_cwd_link),
2668631f9c18SAlexey Dobriyan 	LNK("root",       proc_root_link),
2669631f9c18SAlexey Dobriyan 	LNK("exe",        proc_exe_link),
2670631f9c18SAlexey Dobriyan 	REG("mounts",     S_IRUGO, proc_mounts_operations),
2671631f9c18SAlexey Dobriyan 	REG("mountinfo",  S_IRUGO, proc_mountinfo_operations),
2672631f9c18SAlexey Dobriyan 	REG("mountstats", S_IRUSR, proc_mountstats_operations),
26731e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR
2674631f9c18SAlexey Dobriyan 	REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2675b7643757SSiddhesh Poyarekar 	REG("smaps",      S_IRUGO, proc_pid_smaps_operations),
2676ca6b0bf0SAl Viro 	REG("pagemap",    S_IRUGO, proc_pagemap_operations),
267728a6d671SEric W. Biederman #endif
267828a6d671SEric W. Biederman #ifdef CONFIG_SECURITY
2679631f9c18SAlexey Dobriyan 	DIR("attr",       S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
268028a6d671SEric W. Biederman #endif
268128a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS
2682631f9c18SAlexey Dobriyan 	INF("wchan",      S_IRUGO, proc_pid_wchan),
268328a6d671SEric W. Biederman #endif
26842ec220e2SKen Chen #ifdef CONFIG_STACKTRACE
2685a9712bc1SAl Viro 	ONE("stack",      S_IRUGO, proc_pid_stack),
268628a6d671SEric W. Biederman #endif
268728a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS
2688631f9c18SAlexey Dobriyan 	INF("schedstat",  S_IRUGO, proc_pid_schedstat),
268928a6d671SEric W. Biederman #endif
26909745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP
2691631f9c18SAlexey Dobriyan 	REG("latency",  S_IRUGO, proc_lstats_operations),
26929745512cSArjan van de Ven #endif
26938793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET
2694631f9c18SAlexey Dobriyan 	REG("cpuset",     S_IRUGO, proc_cpuset_operations),
269528a6d671SEric W. Biederman #endif
2696a424316cSPaul Menage #ifdef CONFIG_CGROUPS
2697631f9c18SAlexey Dobriyan 	REG("cgroup",  S_IRUGO, proc_cgroup_operations),
2698a424316cSPaul Menage #endif
2699631f9c18SAlexey Dobriyan 	INF("oom_score",  S_IRUGO, proc_oom_score),
2700fa0cbbf1SDavid Rientjes 	REG("oom_adj",    S_IRUGO|S_IWUSR, proc_oom_adj_operations),
2701a63d83f4SDavid Rientjes 	REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
270228a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL
2703631f9c18SAlexey Dobriyan 	REG("loginuid",   S_IWUSR|S_IRUGO, proc_loginuid_operations),
2704631f9c18SAlexey Dobriyan 	REG("sessionid",  S_IRUGO, proc_sessionid_operations),
270528a6d671SEric W. Biederman #endif
2706f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
2707631f9c18SAlexey Dobriyan 	REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2708f4f154fdSAkinobu Mita #endif
2709698ba7b5SChristoph Hellwig #ifdef CONFIG_ELF_CORE
2710631f9c18SAlexey Dobriyan 	REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
27113cb4a0bbSKawai, Hidehiro #endif
2712aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING
27131d1221f3SVasiliy Kulikov 	INF("io",	S_IRUSR, proc_tgid_io_accounting),
2714aba76fdbSAndrew Morton #endif
2715f133eccaSChris Metcalf #ifdef CONFIG_HARDWALL
2716f133eccaSChris Metcalf 	INF("hardwall",   S_IRUGO, proc_pid_hardwall),
2717f133eccaSChris Metcalf #endif
271822d917d8SEric W. Biederman #ifdef CONFIG_USER_NS
271922d917d8SEric W. Biederman 	REG("uid_map",    S_IRUGO|S_IWUSR, proc_uid_map_operations),
272022d917d8SEric W. Biederman 	REG("gid_map",    S_IRUGO|S_IWUSR, proc_gid_map_operations),
2721f76d207aSEric W. Biederman 	REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
272222d917d8SEric W. Biederman #endif
272348f6a7a5SPavel Emelyanov #ifdef CONFIG_CHECKPOINT_RESTORE
272448f6a7a5SPavel Emelyanov 	REG("timers",	  S_IRUGO, proc_timers_operations),
272548f6a7a5SPavel Emelyanov #endif
272628a6d671SEric W. Biederman };
272728a6d671SEric W. Biederman 
272828a6d671SEric W. Biederman static int proc_tgid_base_readdir(struct file * filp,
272928a6d671SEric W. Biederman 			     void * dirent, filldir_t filldir)
273028a6d671SEric W. Biederman {
273128a6d671SEric W. Biederman 	return proc_pident_readdir(filp,dirent,filldir,
273228a6d671SEric W. Biederman 				   tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
273328a6d671SEric W. Biederman }
273428a6d671SEric W. Biederman 
273500977a59SArjan van de Ven static const struct file_operations proc_tgid_base_operations = {
273628a6d671SEric W. Biederman 	.read		= generic_read_dir,
273728a6d671SEric W. Biederman 	.readdir	= proc_tgid_base_readdir,
27386038f373SArnd Bergmann 	.llseek		= default_llseek,
273928a6d671SEric W. Biederman };
274028a6d671SEric W. Biederman 
274100cd8dd3SAl Viro static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
274200cd8dd3SAl Viro {
27437bcd6b0eSEric W. Biederman 	return proc_pident_lookup(dir, dentry,
27447bcd6b0eSEric W. Biederman 				  tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
274528a6d671SEric W. Biederman }
274628a6d671SEric W. Biederman 
2747c5ef1c42SArjan van de Ven static const struct inode_operations proc_tgid_base_inode_operations = {
274828a6d671SEric W. Biederman 	.lookup		= proc_tgid_base_lookup,
274928a6d671SEric W. Biederman 	.getattr	= pid_getattr,
275028a6d671SEric W. Biederman 	.setattr	= proc_setattr,
27510499680aSVasiliy Kulikov 	.permission	= proc_pid_permission,
275228a6d671SEric W. Biederman };
275328a6d671SEric W. Biederman 
275460347f67SPavel Emelyanov static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
27551da177e4SLinus Torvalds {
275648e6484dSEric W. Biederman 	struct dentry *dentry, *leader, *dir;
27578578cea7SEric W. Biederman 	char buf[PROC_NUMBUF];
275848e6484dSEric W. Biederman 	struct qstr name;
27591da177e4SLinus Torvalds 
276048e6484dSEric W. Biederman 	name.name = buf;
276160347f67SPavel Emelyanov 	name.len = snprintf(buf, sizeof(buf), "%d", pid);
27624f522a24SAl Viro 	/* no ->d_hash() rejects on procfs */
276360347f67SPavel Emelyanov 	dentry = d_hash_and_lookup(mnt->mnt_root, &name);
276448e6484dSEric W. Biederman 	if (dentry) {
276548e6484dSEric W. Biederman 		shrink_dcache_parent(dentry);
276648e6484dSEric W. Biederman 		d_drop(dentry);
276748e6484dSEric W. Biederman 		dput(dentry);
27681da177e4SLinus Torvalds 	}
27691da177e4SLinus Torvalds 
277048e6484dSEric W. Biederman 	name.name = buf;
277160347f67SPavel Emelyanov 	name.len = snprintf(buf, sizeof(buf), "%d", tgid);
277260347f67SPavel Emelyanov 	leader = d_hash_and_lookup(mnt->mnt_root, &name);
277348e6484dSEric W. Biederman 	if (!leader)
277448e6484dSEric W. Biederman 		goto out;
277548e6484dSEric W. Biederman 
277648e6484dSEric W. Biederman 	name.name = "task";
277748e6484dSEric W. Biederman 	name.len = strlen(name.name);
277848e6484dSEric W. Biederman 	dir = d_hash_and_lookup(leader, &name);
277948e6484dSEric W. Biederman 	if (!dir)
278048e6484dSEric W. Biederman 		goto out_put_leader;
278148e6484dSEric W. Biederman 
278248e6484dSEric W. Biederman 	name.name = buf;
278360347f67SPavel Emelyanov 	name.len = snprintf(buf, sizeof(buf), "%d", pid);
278448e6484dSEric W. Biederman 	dentry = d_hash_and_lookup(dir, &name);
278548e6484dSEric W. Biederman 	if (dentry) {
278648e6484dSEric W. Biederman 		shrink_dcache_parent(dentry);
278748e6484dSEric W. Biederman 		d_drop(dentry);
278848e6484dSEric W. Biederman 		dput(dentry);
27891da177e4SLinus Torvalds 	}
279048e6484dSEric W. Biederman 
279148e6484dSEric W. Biederman 	dput(dir);
279248e6484dSEric W. Biederman out_put_leader:
279348e6484dSEric W. Biederman 	dput(leader);
279448e6484dSEric W. Biederman out:
279548e6484dSEric W. Biederman 	return;
27961da177e4SLinus Torvalds }
27971da177e4SLinus Torvalds 
27980895e91dSRandy Dunlap /**
27990895e91dSRandy Dunlap  * proc_flush_task -  Remove dcache entries for @task from the /proc dcache.
28000895e91dSRandy Dunlap  * @task: task that should be flushed.
28010895e91dSRandy Dunlap  *
28020895e91dSRandy Dunlap  * When flushing dentries from proc, one needs to flush them from global
280360347f67SPavel Emelyanov  * proc (proc_mnt) and from all the namespaces' procs this task was seen
28040895e91dSRandy Dunlap  * in. This call is supposed to do all of this job.
28050895e91dSRandy Dunlap  *
28060895e91dSRandy Dunlap  * Looks in the dcache for
28070895e91dSRandy Dunlap  * /proc/@pid
28080895e91dSRandy Dunlap  * /proc/@tgid/task/@pid
28090895e91dSRandy Dunlap  * if either directory is present flushes it and all of it'ts children
28100895e91dSRandy Dunlap  * from the dcache.
28110895e91dSRandy Dunlap  *
28120895e91dSRandy Dunlap  * It is safe and reasonable to cache /proc entries for a task until
28130895e91dSRandy Dunlap  * that task exits.  After that they just clog up the dcache with
28140895e91dSRandy Dunlap  * useless entries, possibly causing useful dcache entries to be
28150895e91dSRandy Dunlap  * flushed instead.  This routine is proved to flush those useless
28160895e91dSRandy Dunlap  * dcache entries at process exit time.
28170895e91dSRandy Dunlap  *
28180895e91dSRandy Dunlap  * NOTE: This routine is just an optimization so it does not guarantee
28190895e91dSRandy Dunlap  *       that no dcache entries will exist at process exit time it
28200895e91dSRandy Dunlap  *       just makes it very unlikely that any will persist.
282160347f67SPavel Emelyanov  */
282260347f67SPavel Emelyanov 
282360347f67SPavel Emelyanov void proc_flush_task(struct task_struct *task)
282460347f67SPavel Emelyanov {
28259fcc2d15SEric W. Biederman 	int i;
28269b4d1cbeSOleg Nesterov 	struct pid *pid, *tgid;
2827130f77ecSPavel Emelyanov 	struct upid *upid;
2828130f77ecSPavel Emelyanov 
2829130f77ecSPavel Emelyanov 	pid = task_pid(task);
2830130f77ecSPavel Emelyanov 	tgid = task_tgid(task);
28319fcc2d15SEric W. Biederman 
28329fcc2d15SEric W. Biederman 	for (i = 0; i <= pid->level; i++) {
2833130f77ecSPavel Emelyanov 		upid = &pid->numbers[i];
2834130f77ecSPavel Emelyanov 		proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
28359b4d1cbeSOleg Nesterov 					tgid->numbers[i].nr);
2836130f77ecSPavel Emelyanov 	}
283760347f67SPavel Emelyanov }
283860347f67SPavel Emelyanov 
28399711ef99SAdrian Bunk static struct dentry *proc_pid_instantiate(struct inode *dir,
28409711ef99SAdrian Bunk 					   struct dentry * dentry,
2841c5141e6dSEric Dumazet 					   struct task_struct *task, const void *ptr)
2842444ceed8SEric W. Biederman {
2843444ceed8SEric W. Biederman 	struct dentry *error = ERR_PTR(-ENOENT);
2844444ceed8SEric W. Biederman 	struct inode *inode;
2845444ceed8SEric W. Biederman 
284661a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
2847444ceed8SEric W. Biederman 	if (!inode)
2848444ceed8SEric W. Biederman 		goto out;
2849444ceed8SEric W. Biederman 
2850444ceed8SEric W. Biederman 	inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2851444ceed8SEric W. Biederman 	inode->i_op = &proc_tgid_base_inode_operations;
2852444ceed8SEric W. Biederman 	inode->i_fop = &proc_tgid_base_operations;
2853444ceed8SEric W. Biederman 	inode->i_flags|=S_IMMUTABLE;
2854aed54175SVegard Nossum 
2855bfe86848SMiklos Szeredi 	set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
2856bfe86848SMiklos Szeredi 						  ARRAY_SIZE(tgid_base_stuff)));
2857444ceed8SEric W. Biederman 
2858fb045adbSNick Piggin 	d_set_d_op(dentry, &pid_dentry_operations);
2859444ceed8SEric W. Biederman 
2860444ceed8SEric W. Biederman 	d_add(dentry, inode);
2861444ceed8SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
28620b728e19SAl Viro 	if (pid_revalidate(dentry, 0))
2863444ceed8SEric W. Biederman 		error = NULL;
2864444ceed8SEric W. Biederman out:
2865444ceed8SEric W. Biederman 	return error;
2866444ceed8SEric W. Biederman }
2867444ceed8SEric W. Biederman 
286800cd8dd3SAl Viro struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
28691da177e4SLinus Torvalds {
2870e656d8a6SEric W. Biederman 	struct dentry *result = NULL;
28711da177e4SLinus Torvalds 	struct task_struct *task;
28721da177e4SLinus Torvalds 	unsigned tgid;
2873b488893aSPavel Emelyanov 	struct pid_namespace *ns;
28741da177e4SLinus Torvalds 
28751da177e4SLinus Torvalds 	tgid = name_to_int(dentry);
28761da177e4SLinus Torvalds 	if (tgid == ~0U)
28771da177e4SLinus Torvalds 		goto out;
28781da177e4SLinus Torvalds 
2879b488893aSPavel Emelyanov 	ns = dentry->d_sb->s_fs_info;
2880de758734SEric W. Biederman 	rcu_read_lock();
2881b488893aSPavel Emelyanov 	task = find_task_by_pid_ns(tgid, ns);
28821da177e4SLinus Torvalds 	if (task)
28831da177e4SLinus Torvalds 		get_task_struct(task);
2884de758734SEric W. Biederman 	rcu_read_unlock();
28851da177e4SLinus Torvalds 	if (!task)
28861da177e4SLinus Torvalds 		goto out;
28871da177e4SLinus Torvalds 
2888444ceed8SEric W. Biederman 	result = proc_pid_instantiate(dir, dentry, task, NULL);
288948e6484dSEric W. Biederman 	put_task_struct(task);
28901da177e4SLinus Torvalds out:
2891cd6a3ce9SEric W. Biederman 	return result;
28921da177e4SLinus Torvalds }
28931da177e4SLinus Torvalds 
28941da177e4SLinus Torvalds /*
28950804ef4bSEric W. Biederman  * Find the first task with tgid >= tgid
28960bc58a91SEric W. Biederman  *
28971da177e4SLinus Torvalds  */
289819fd4bb2SEric W. Biederman struct tgid_iter {
289919fd4bb2SEric W. Biederman 	unsigned int tgid;
29000804ef4bSEric W. Biederman 	struct task_struct *task;
290119fd4bb2SEric W. Biederman };
290219fd4bb2SEric W. Biederman static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
290319fd4bb2SEric W. Biederman {
29040804ef4bSEric W. Biederman 	struct pid *pid;
29051da177e4SLinus Torvalds 
290619fd4bb2SEric W. Biederman 	if (iter.task)
290719fd4bb2SEric W. Biederman 		put_task_struct(iter.task);
29080804ef4bSEric W. Biederman 	rcu_read_lock();
29090804ef4bSEric W. Biederman retry:
291019fd4bb2SEric W. Biederman 	iter.task = NULL;
291119fd4bb2SEric W. Biederman 	pid = find_ge_pid(iter.tgid, ns);
29120804ef4bSEric W. Biederman 	if (pid) {
291319fd4bb2SEric W. Biederman 		iter.tgid = pid_nr_ns(pid, ns);
291419fd4bb2SEric W. Biederman 		iter.task = pid_task(pid, PIDTYPE_PID);
29150804ef4bSEric W. Biederman 		/* What we to know is if the pid we have find is the
29160804ef4bSEric W. Biederman 		 * pid of a thread_group_leader.  Testing for task
29170804ef4bSEric W. Biederman 		 * being a thread_group_leader is the obvious thing
29180804ef4bSEric W. Biederman 		 * todo but there is a window when it fails, due to
29190804ef4bSEric W. Biederman 		 * the pid transfer logic in de_thread.
29200804ef4bSEric W. Biederman 		 *
29210804ef4bSEric W. Biederman 		 * So we perform the straight forward test of seeing
29220804ef4bSEric W. Biederman 		 * if the pid we have found is the pid of a thread
29230804ef4bSEric W. Biederman 		 * group leader, and don't worry if the task we have
29240804ef4bSEric W. Biederman 		 * found doesn't happen to be a thread group leader.
29250804ef4bSEric W. Biederman 		 * As we don't care in the case of readdir.
29260bc58a91SEric W. Biederman 		 */
292719fd4bb2SEric W. Biederman 		if (!iter.task || !has_group_leader_pid(iter.task)) {
292819fd4bb2SEric W. Biederman 			iter.tgid += 1;
29290804ef4bSEric W. Biederman 			goto retry;
293019fd4bb2SEric W. Biederman 		}
293119fd4bb2SEric W. Biederman 		get_task_struct(iter.task);
29321da177e4SLinus Torvalds 	}
2933454cc105SEric W. Biederman 	rcu_read_unlock();
293419fd4bb2SEric W. Biederman 	return iter;
29351da177e4SLinus Torvalds }
29361da177e4SLinus Torvalds 
2937021ada7dSAl Viro #define TGID_OFFSET (FIRST_PROCESS_ENTRY + 1)
29381da177e4SLinus Torvalds 
293961a28784SEric W. Biederman static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
294019fd4bb2SEric W. Biederman 	struct tgid_iter iter)
294161a28784SEric W. Biederman {
294261a28784SEric W. Biederman 	char name[PROC_NUMBUF];
294319fd4bb2SEric W. Biederman 	int len = snprintf(name, sizeof(name), "%d", iter.tgid);
294461a28784SEric W. Biederman 	return proc_fill_cache(filp, dirent, filldir, name, len,
294519fd4bb2SEric W. Biederman 				proc_pid_instantiate, iter.task, NULL);
294661a28784SEric W. Biederman }
294761a28784SEric W. Biederman 
29480499680aSVasiliy Kulikov static int fake_filldir(void *buf, const char *name, int namelen,
29490499680aSVasiliy Kulikov 			loff_t offset, u64 ino, unsigned d_type)
29500499680aSVasiliy Kulikov {
29510499680aSVasiliy Kulikov 	return 0;
29520499680aSVasiliy Kulikov }
29530499680aSVasiliy Kulikov 
29541da177e4SLinus Torvalds /* for the /proc/ directory itself, after non-process stuff has been done */
29551da177e4SLinus Torvalds int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
29561da177e4SLinus Torvalds {
295719fd4bb2SEric W. Biederman 	struct tgid_iter iter;
2958b488893aSPavel Emelyanov 	struct pid_namespace *ns;
29590499680aSVasiliy Kulikov 	filldir_t __filldir;
2960021ada7dSAl Viro 	loff_t pos = filp->f_pos;
29611da177e4SLinus Torvalds 
2962021ada7dSAl Viro 	if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
2963801199ceSEric W. Biederman 		goto out;
29641da177e4SLinus Torvalds 
2965021ada7dSAl Viro 	if (pos == TGID_OFFSET - 1) {
2966021ada7dSAl Viro 		if (proc_fill_cache(filp, dirent, filldir, "self", 4,
2967021ada7dSAl Viro 					NULL, NULL, NULL) < 0)
2968021ada7dSAl Viro 			goto out;
2969021ada7dSAl Viro 		iter.tgid = 0;
2970021ada7dSAl Viro 	} else {
2971021ada7dSAl Viro 		iter.tgid = pos - TGID_OFFSET;
2972021ada7dSAl Viro 	}
297319fd4bb2SEric W. Biederman 	iter.task = NULL;
2974021ada7dSAl Viro 	ns = filp->f_dentry->d_sb->s_fs_info;
297519fd4bb2SEric W. Biederman 	for (iter = next_tgid(ns, iter);
297619fd4bb2SEric W. Biederman 	     iter.task;
297719fd4bb2SEric W. Biederman 	     iter.tgid += 1, iter = next_tgid(ns, iter)) {
29780499680aSVasiliy Kulikov 		if (has_pid_permissions(ns, iter.task, 2))
29790499680aSVasiliy Kulikov 			__filldir = filldir;
29800499680aSVasiliy Kulikov 		else
29810499680aSVasiliy Kulikov 			__filldir = fake_filldir;
29820499680aSVasiliy Kulikov 
298319fd4bb2SEric W. Biederman 		filp->f_pos = iter.tgid + TGID_OFFSET;
29840499680aSVasiliy Kulikov 		if (proc_pid_fill_cache(filp, dirent, __filldir, iter) < 0) {
298519fd4bb2SEric W. Biederman 			put_task_struct(iter.task);
29860804ef4bSEric W. Biederman 			goto out;
29871da177e4SLinus Torvalds 		}
29881da177e4SLinus Torvalds 	}
29890804ef4bSEric W. Biederman 	filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
29900804ef4bSEric W. Biederman out:
29911da177e4SLinus Torvalds 	return 0;
29921da177e4SLinus Torvalds }
29931da177e4SLinus Torvalds 
29940bc58a91SEric W. Biederman /*
299528a6d671SEric W. Biederman  * Tasks
299628a6d671SEric W. Biederman  */
2997c5141e6dSEric Dumazet static const struct pid_entry tid_base_stuff[] = {
2998631f9c18SAlexey Dobriyan 	DIR("fd",        S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
29993835541dSJerome Marchand 	DIR("fdinfo",    S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
30006b4e306aSEric W. Biederman 	DIR("ns",	 S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
3001631f9c18SAlexey Dobriyan 	REG("environ",   S_IRUSR, proc_environ_operations),
3002631f9c18SAlexey Dobriyan 	INF("auxv",      S_IRUSR, proc_pid_auxv),
3003631f9c18SAlexey Dobriyan 	ONE("status",    S_IRUGO, proc_pid_status),
3004a9712bc1SAl Viro 	ONE("personality", S_IRUGO, proc_pid_personality),
30053036e7b4SJiri Olsa 	INF("limits",	 S_IRUGO, proc_pid_limits),
300643ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
3007631f9c18SAlexey Dobriyan 	REG("sched",     S_IRUGO|S_IWUSR, proc_pid_sched_operations),
300843ae34cbSIngo Molnar #endif
30094614a696Sjohn stultz 	REG("comm",      S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
3010ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3011a9712bc1SAl Viro 	INF("syscall",   S_IRUGO, proc_pid_syscall),
3012ebcb6734SRoland McGrath #endif
3013631f9c18SAlexey Dobriyan 	INF("cmdline",   S_IRUGO, proc_pid_cmdline),
3014631f9c18SAlexey Dobriyan 	ONE("stat",      S_IRUGO, proc_tid_stat),
3015631f9c18SAlexey Dobriyan 	ONE("statm",     S_IRUGO, proc_pid_statm),
3016b7643757SSiddhesh Poyarekar 	REG("maps",      S_IRUGO, proc_tid_maps_operations),
301781841161SCyrill Gorcunov #ifdef CONFIG_CHECKPOINT_RESTORE
301881841161SCyrill Gorcunov 	REG("children",  S_IRUGO, proc_tid_children_operations),
301981841161SCyrill Gorcunov #endif
302028a6d671SEric W. Biederman #ifdef CONFIG_NUMA
3021b7643757SSiddhesh Poyarekar 	REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
302228a6d671SEric W. Biederman #endif
3023631f9c18SAlexey Dobriyan 	REG("mem",       S_IRUSR|S_IWUSR, proc_mem_operations),
3024631f9c18SAlexey Dobriyan 	LNK("cwd",       proc_cwd_link),
3025631f9c18SAlexey Dobriyan 	LNK("root",      proc_root_link),
3026631f9c18SAlexey Dobriyan 	LNK("exe",       proc_exe_link),
3027631f9c18SAlexey Dobriyan 	REG("mounts",    S_IRUGO, proc_mounts_operations),
3028631f9c18SAlexey Dobriyan 	REG("mountinfo",  S_IRUGO, proc_mountinfo_operations),
30291e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR
3030631f9c18SAlexey Dobriyan 	REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3031b7643757SSiddhesh Poyarekar 	REG("smaps",     S_IRUGO, proc_tid_smaps_operations),
3032ca6b0bf0SAl Viro 	REG("pagemap",    S_IRUGO, proc_pagemap_operations),
303328a6d671SEric W. Biederman #endif
303428a6d671SEric W. Biederman #ifdef CONFIG_SECURITY
3035631f9c18SAlexey Dobriyan 	DIR("attr",      S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
303628a6d671SEric W. Biederman #endif
303728a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS
3038631f9c18SAlexey Dobriyan 	INF("wchan",     S_IRUGO, proc_pid_wchan),
303928a6d671SEric W. Biederman #endif
30402ec220e2SKen Chen #ifdef CONFIG_STACKTRACE
3041a9712bc1SAl Viro 	ONE("stack",      S_IRUGO, proc_pid_stack),
304228a6d671SEric W. Biederman #endif
304328a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS
3044631f9c18SAlexey Dobriyan 	INF("schedstat", S_IRUGO, proc_pid_schedstat),
304528a6d671SEric W. Biederman #endif
30469745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP
3047631f9c18SAlexey Dobriyan 	REG("latency",  S_IRUGO, proc_lstats_operations),
30489745512cSArjan van de Ven #endif
30498793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET
3050631f9c18SAlexey Dobriyan 	REG("cpuset",    S_IRUGO, proc_cpuset_operations),
305128a6d671SEric W. Biederman #endif
3052a424316cSPaul Menage #ifdef CONFIG_CGROUPS
3053631f9c18SAlexey Dobriyan 	REG("cgroup",  S_IRUGO, proc_cgroup_operations),
3054a424316cSPaul Menage #endif
3055631f9c18SAlexey Dobriyan 	INF("oom_score", S_IRUGO, proc_oom_score),
3056fa0cbbf1SDavid Rientjes 	REG("oom_adj",   S_IRUGO|S_IWUSR, proc_oom_adj_operations),
3057a63d83f4SDavid Rientjes 	REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
305828a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL
3059631f9c18SAlexey Dobriyan 	REG("loginuid",  S_IWUSR|S_IRUGO, proc_loginuid_operations),
306026ec3c64SAl Viro 	REG("sessionid",  S_IRUGO, proc_sessionid_operations),
306128a6d671SEric W. Biederman #endif
3062f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
3063631f9c18SAlexey Dobriyan 	REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3064f4f154fdSAkinobu Mita #endif
3065297c5d92SAndrea Righi #ifdef CONFIG_TASK_IO_ACCOUNTING
30661d1221f3SVasiliy Kulikov 	INF("io",	S_IRUSR, proc_tid_io_accounting),
3067297c5d92SAndrea Righi #endif
3068f133eccaSChris Metcalf #ifdef CONFIG_HARDWALL
3069f133eccaSChris Metcalf 	INF("hardwall",   S_IRUGO, proc_pid_hardwall),
3070f133eccaSChris Metcalf #endif
307122d917d8SEric W. Biederman #ifdef CONFIG_USER_NS
307222d917d8SEric W. Biederman 	REG("uid_map",    S_IRUGO|S_IWUSR, proc_uid_map_operations),
307322d917d8SEric W. Biederman 	REG("gid_map",    S_IRUGO|S_IWUSR, proc_gid_map_operations),
3074f76d207aSEric W. Biederman 	REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
307522d917d8SEric W. Biederman #endif
307628a6d671SEric W. Biederman };
307728a6d671SEric W. Biederman 
307828a6d671SEric W. Biederman static int proc_tid_base_readdir(struct file * filp,
307928a6d671SEric W. Biederman 			     void * dirent, filldir_t filldir)
308028a6d671SEric W. Biederman {
308128a6d671SEric W. Biederman 	return proc_pident_readdir(filp,dirent,filldir,
308228a6d671SEric W. Biederman 				   tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
308328a6d671SEric W. Biederman }
308428a6d671SEric W. Biederman 
308500cd8dd3SAl Viro static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
308600cd8dd3SAl Viro {
30877bcd6b0eSEric W. Biederman 	return proc_pident_lookup(dir, dentry,
30887bcd6b0eSEric W. Biederman 				  tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
308928a6d671SEric W. Biederman }
309028a6d671SEric W. Biederman 
309100977a59SArjan van de Ven static const struct file_operations proc_tid_base_operations = {
309228a6d671SEric W. Biederman 	.read		= generic_read_dir,
309328a6d671SEric W. Biederman 	.readdir	= proc_tid_base_readdir,
30946038f373SArnd Bergmann 	.llseek		= default_llseek,
309528a6d671SEric W. Biederman };
309628a6d671SEric W. Biederman 
3097c5ef1c42SArjan van de Ven static const struct inode_operations proc_tid_base_inode_operations = {
309828a6d671SEric W. Biederman 	.lookup		= proc_tid_base_lookup,
309928a6d671SEric W. Biederman 	.getattr	= pid_getattr,
310028a6d671SEric W. Biederman 	.setattr	= proc_setattr,
310128a6d671SEric W. Biederman };
310228a6d671SEric W. Biederman 
3103444ceed8SEric W. Biederman static struct dentry *proc_task_instantiate(struct inode *dir,
3104c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
3105444ceed8SEric W. Biederman {
3106444ceed8SEric W. Biederman 	struct dentry *error = ERR_PTR(-ENOENT);
3107444ceed8SEric W. Biederman 	struct inode *inode;
310861a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
3109444ceed8SEric W. Biederman 
3110444ceed8SEric W. Biederman 	if (!inode)
3111444ceed8SEric W. Biederman 		goto out;
3112444ceed8SEric W. Biederman 	inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3113444ceed8SEric W. Biederman 	inode->i_op = &proc_tid_base_inode_operations;
3114444ceed8SEric W. Biederman 	inode->i_fop = &proc_tid_base_operations;
3115444ceed8SEric W. Biederman 	inode->i_flags|=S_IMMUTABLE;
3116aed54175SVegard Nossum 
3117bfe86848SMiklos Szeredi 	set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3118bfe86848SMiklos Szeredi 						  ARRAY_SIZE(tid_base_stuff)));
3119444ceed8SEric W. Biederman 
3120fb045adbSNick Piggin 	d_set_d_op(dentry, &pid_dentry_operations);
3121444ceed8SEric W. Biederman 
3122444ceed8SEric W. Biederman 	d_add(dentry, inode);
3123444ceed8SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
31240b728e19SAl Viro 	if (pid_revalidate(dentry, 0))
3125444ceed8SEric W. Biederman 		error = NULL;
3126444ceed8SEric W. Biederman out:
3127444ceed8SEric W. Biederman 	return error;
3128444ceed8SEric W. Biederman }
3129444ceed8SEric W. Biederman 
313000cd8dd3SAl Viro static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
313128a6d671SEric W. Biederman {
313228a6d671SEric W. Biederman 	struct dentry *result = ERR_PTR(-ENOENT);
313328a6d671SEric W. Biederman 	struct task_struct *task;
313428a6d671SEric W. Biederman 	struct task_struct *leader = get_proc_task(dir);
313528a6d671SEric W. Biederman 	unsigned tid;
3136b488893aSPavel Emelyanov 	struct pid_namespace *ns;
313728a6d671SEric W. Biederman 
313828a6d671SEric W. Biederman 	if (!leader)
313928a6d671SEric W. Biederman 		goto out_no_task;
314028a6d671SEric W. Biederman 
314128a6d671SEric W. Biederman 	tid = name_to_int(dentry);
314228a6d671SEric W. Biederman 	if (tid == ~0U)
314328a6d671SEric W. Biederman 		goto out;
314428a6d671SEric W. Biederman 
3145b488893aSPavel Emelyanov 	ns = dentry->d_sb->s_fs_info;
314628a6d671SEric W. Biederman 	rcu_read_lock();
3147b488893aSPavel Emelyanov 	task = find_task_by_pid_ns(tid, ns);
314828a6d671SEric W. Biederman 	if (task)
314928a6d671SEric W. Biederman 		get_task_struct(task);
315028a6d671SEric W. Biederman 	rcu_read_unlock();
315128a6d671SEric W. Biederman 	if (!task)
315228a6d671SEric W. Biederman 		goto out;
3153bac0abd6SPavel Emelyanov 	if (!same_thread_group(leader, task))
315428a6d671SEric W. Biederman 		goto out_drop_task;
315528a6d671SEric W. Biederman 
3156444ceed8SEric W. Biederman 	result = proc_task_instantiate(dir, dentry, task, NULL);
315728a6d671SEric W. Biederman out_drop_task:
315828a6d671SEric W. Biederman 	put_task_struct(task);
315928a6d671SEric W. Biederman out:
316028a6d671SEric W. Biederman 	put_task_struct(leader);
316128a6d671SEric W. Biederman out_no_task:
316228a6d671SEric W. Biederman 	return result;
316328a6d671SEric W. Biederman }
316428a6d671SEric W. Biederman 
316528a6d671SEric W. Biederman /*
31660bc58a91SEric W. Biederman  * Find the first tid of a thread group to return to user space.
31670bc58a91SEric W. Biederman  *
31680bc58a91SEric W. Biederman  * Usually this is just the thread group leader, but if the users
31690bc58a91SEric W. Biederman  * buffer was too small or there was a seek into the middle of the
31700bc58a91SEric W. Biederman  * directory we have more work todo.
31710bc58a91SEric W. Biederman  *
31720bc58a91SEric W. Biederman  * In the case of a short read we start with find_task_by_pid.
31730bc58a91SEric W. Biederman  *
31740bc58a91SEric W. Biederman  * In the case of a seek we start with the leader and walk nr
31750bc58a91SEric W. Biederman  * threads past it.
31760bc58a91SEric W. Biederman  */
3177cc288738SEric W. Biederman static struct task_struct *first_tid(struct task_struct *leader,
3178b488893aSPavel Emelyanov 		int tid, int nr, struct pid_namespace *ns)
31790bc58a91SEric W. Biederman {
3180a872ff0cSOleg Nesterov 	struct task_struct *pos;
31810bc58a91SEric W. Biederman 
3182cc288738SEric W. Biederman 	rcu_read_lock();
31830bc58a91SEric W. Biederman 	/* Attempt to start with the pid of a thread */
31840bc58a91SEric W. Biederman 	if (tid && (nr > 0)) {
3185b488893aSPavel Emelyanov 		pos = find_task_by_pid_ns(tid, ns);
3186a872ff0cSOleg Nesterov 		if (pos && (pos->group_leader == leader))
3187a872ff0cSOleg Nesterov 			goto found;
31880bc58a91SEric W. Biederman 	}
31890bc58a91SEric W. Biederman 
31900bc58a91SEric W. Biederman 	/* If nr exceeds the number of threads there is nothing todo */
31910bc58a91SEric W. Biederman 	pos = NULL;
3192a872ff0cSOleg Nesterov 	if (nr && nr >= get_nr_threads(leader))
3193a872ff0cSOleg Nesterov 		goto out;
3194a872ff0cSOleg Nesterov 
3195a872ff0cSOleg Nesterov 	/* If we haven't found our starting place yet start
3196a872ff0cSOleg Nesterov 	 * with the leader and walk nr threads forward.
3197a872ff0cSOleg Nesterov 	 */
3198a872ff0cSOleg Nesterov 	for (pos = leader; nr > 0; --nr) {
3199a872ff0cSOleg Nesterov 		pos = next_thread(pos);
3200a872ff0cSOleg Nesterov 		if (pos == leader) {
3201a872ff0cSOleg Nesterov 			pos = NULL;
3202a872ff0cSOleg Nesterov 			goto out;
3203a872ff0cSOleg Nesterov 		}
3204a872ff0cSOleg Nesterov 	}
3205a872ff0cSOleg Nesterov found:
3206a872ff0cSOleg Nesterov 	get_task_struct(pos);
3207a872ff0cSOleg Nesterov out:
3208cc288738SEric W. Biederman 	rcu_read_unlock();
32090bc58a91SEric W. Biederman 	return pos;
32100bc58a91SEric W. Biederman }
32110bc58a91SEric W. Biederman 
32120bc58a91SEric W. Biederman /*
32130bc58a91SEric W. Biederman  * Find the next thread in the thread list.
32140bc58a91SEric W. Biederman  * Return NULL if there is an error or no next thread.
32150bc58a91SEric W. Biederman  *
32160bc58a91SEric W. Biederman  * The reference to the input task_struct is released.
32170bc58a91SEric W. Biederman  */
32180bc58a91SEric W. Biederman static struct task_struct *next_tid(struct task_struct *start)
32190bc58a91SEric W. Biederman {
3220c1df7fb8SOleg Nesterov 	struct task_struct *pos = NULL;
3221cc288738SEric W. Biederman 	rcu_read_lock();
3222c1df7fb8SOleg Nesterov 	if (pid_alive(start)) {
32230bc58a91SEric W. Biederman 		pos = next_thread(start);
3224c1df7fb8SOleg Nesterov 		if (thread_group_leader(pos))
32250bc58a91SEric W. Biederman 			pos = NULL;
3226c1df7fb8SOleg Nesterov 		else
3227c1df7fb8SOleg Nesterov 			get_task_struct(pos);
3228c1df7fb8SOleg Nesterov 	}
3229cc288738SEric W. Biederman 	rcu_read_unlock();
32300bc58a91SEric W. Biederman 	put_task_struct(start);
32310bc58a91SEric W. Biederman 	return pos;
32320bc58a91SEric W. Biederman }
32330bc58a91SEric W. Biederman 
323461a28784SEric W. Biederman static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
323561a28784SEric W. Biederman 	struct task_struct *task, int tid)
323661a28784SEric W. Biederman {
323761a28784SEric W. Biederman 	char name[PROC_NUMBUF];
323861a28784SEric W. Biederman 	int len = snprintf(name, sizeof(name), "%d", tid);
323961a28784SEric W. Biederman 	return proc_fill_cache(filp, dirent, filldir, name, len,
324061a28784SEric W. Biederman 				proc_task_instantiate, task, NULL);
324161a28784SEric W. Biederman }
324261a28784SEric W. Biederman 
32431da177e4SLinus Torvalds /* for the /proc/TGID/task/ directories */
32441da177e4SLinus Torvalds static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
32451da177e4SLinus Torvalds {
32462fddfeefSJosef "Jeff" Sipek 	struct dentry *dentry = filp->f_path.dentry;
32471da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
32487d895244SGuillaume Chazarain 	struct task_struct *leader = NULL;
32490bc58a91SEric W. Biederman 	struct task_struct *task;
32501da177e4SLinus Torvalds 	int retval = -ENOENT;
32511da177e4SLinus Torvalds 	ino_t ino;
32520bc58a91SEric W. Biederman 	int tid;
3253b488893aSPavel Emelyanov 	struct pid_namespace *ns;
32541da177e4SLinus Torvalds 
32557d895244SGuillaume Chazarain 	task = get_proc_task(inode);
32567d895244SGuillaume Chazarain 	if (!task)
32577d895244SGuillaume Chazarain 		goto out_no_task;
32587d895244SGuillaume Chazarain 	rcu_read_lock();
32597d895244SGuillaume Chazarain 	if (pid_alive(task)) {
32607d895244SGuillaume Chazarain 		leader = task->group_leader;
32617d895244SGuillaume Chazarain 		get_task_struct(leader);
32627d895244SGuillaume Chazarain 	}
32637d895244SGuillaume Chazarain 	rcu_read_unlock();
32647d895244SGuillaume Chazarain 	put_task_struct(task);
326599f89551SEric W. Biederman 	if (!leader)
326699f89551SEric W. Biederman 		goto out_no_task;
32671da177e4SLinus Torvalds 	retval = 0;
32681da177e4SLinus Torvalds 
3269ee568b25SLinus Torvalds 	switch ((unsigned long)filp->f_pos) {
32701da177e4SLinus Torvalds 	case 0:
32711da177e4SLinus Torvalds 		ino = inode->i_ino;
3272ee6f779bSZhang Le 		if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) < 0)
32731da177e4SLinus Torvalds 			goto out;
3274ee6f779bSZhang Le 		filp->f_pos++;
32751da177e4SLinus Torvalds 		/* fall through */
32761da177e4SLinus Torvalds 	case 1:
32771da177e4SLinus Torvalds 		ino = parent_ino(dentry);
3278ee6f779bSZhang Le 		if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) < 0)
32791da177e4SLinus Torvalds 			goto out;
3280ee6f779bSZhang Le 		filp->f_pos++;
32811da177e4SLinus Torvalds 		/* fall through */
32821da177e4SLinus Torvalds 	}
32831da177e4SLinus Torvalds 
32840bc58a91SEric W. Biederman 	/* f_version caches the tgid value that the last readdir call couldn't
32850bc58a91SEric W. Biederman 	 * return. lseek aka telldir automagically resets f_version to 0.
32860bc58a91SEric W. Biederman 	 */
3287b488893aSPavel Emelyanov 	ns = filp->f_dentry->d_sb->s_fs_info;
32882b47c361SMathieu Desnoyers 	tid = (int)filp->f_version;
32890bc58a91SEric W. Biederman 	filp->f_version = 0;
3290ee6f779bSZhang Le 	for (task = first_tid(leader, tid, filp->f_pos - 2, ns);
32910bc58a91SEric W. Biederman 	     task;
3292ee6f779bSZhang Le 	     task = next_tid(task), filp->f_pos++) {
3293b488893aSPavel Emelyanov 		tid = task_pid_nr_ns(task, ns);
329461a28784SEric W. Biederman 		if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
32950bc58a91SEric W. Biederman 			/* returning this tgid failed, save it as the first
32960bc58a91SEric W. Biederman 			 * pid for the next readir call */
32972b47c361SMathieu Desnoyers 			filp->f_version = (u64)tid;
32980bc58a91SEric W. Biederman 			put_task_struct(task);
32991da177e4SLinus Torvalds 			break;
33000bc58a91SEric W. Biederman 		}
33011da177e4SLinus Torvalds 	}
33021da177e4SLinus Torvalds out:
330399f89551SEric W. Biederman 	put_task_struct(leader);
330499f89551SEric W. Biederman out_no_task:
33051da177e4SLinus Torvalds 	return retval;
33061da177e4SLinus Torvalds }
33076e66b52bSEric W. Biederman 
33086e66b52bSEric W. Biederman static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
33096e66b52bSEric W. Biederman {
33106e66b52bSEric W. Biederman 	struct inode *inode = dentry->d_inode;
331199f89551SEric W. Biederman 	struct task_struct *p = get_proc_task(inode);
33126e66b52bSEric W. Biederman 	generic_fillattr(inode, stat);
33136e66b52bSEric W. Biederman 
331499f89551SEric W. Biederman 	if (p) {
331599f89551SEric W. Biederman 		stat->nlink += get_nr_threads(p);
331699f89551SEric W. Biederman 		put_task_struct(p);
33176e66b52bSEric W. Biederman 	}
33186e66b52bSEric W. Biederman 
33196e66b52bSEric W. Biederman 	return 0;
33206e66b52bSEric W. Biederman }
332128a6d671SEric W. Biederman 
3322c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations = {
332328a6d671SEric W. Biederman 	.lookup		= proc_task_lookup,
332428a6d671SEric W. Biederman 	.getattr	= proc_task_getattr,
332528a6d671SEric W. Biederman 	.setattr	= proc_setattr,
33260499680aSVasiliy Kulikov 	.permission	= proc_pid_permission,
332728a6d671SEric W. Biederman };
332828a6d671SEric W. Biederman 
332900977a59SArjan van de Ven static const struct file_operations proc_task_operations = {
333028a6d671SEric W. Biederman 	.read		= generic_read_dir,
333128a6d671SEric W. Biederman 	.readdir	= proc_task_readdir,
33326038f373SArnd Bergmann 	.llseek		= default_llseek,
333328a6d671SEric W. Biederman };
3334