xref: /openbmc/linux/fs/proc/base.c (revision bfe8684869601dacfcb2cd69ef8cfd9045f62170)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/fs/proc/base.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 1991, 1992 Linus Torvalds
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *  proc base directory handling functions
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  *  1999, Al Viro. Rewritten. Now it covers the whole per-process part.
91da177e4SLinus Torvalds  *  Instead of using magical inumbers to determine the kind of object
101da177e4SLinus Torvalds  *  we allocate and fill in-core inodes upon lookup. They don't even
111da177e4SLinus Torvalds  *  go into icache. We cache the reference to task_struct upon lookup too.
121da177e4SLinus Torvalds  *  Eventually it should become a filesystem in its own. We don't use the
131da177e4SLinus Torvalds  *  rest of procfs anymore.
14e070ad49SMauricio Lin  *
15e070ad49SMauricio Lin  *
16e070ad49SMauricio Lin  *  Changelog:
17e070ad49SMauricio Lin  *  17-Jan-2005
18e070ad49SMauricio Lin  *  Allan Bezerra
19e070ad49SMauricio Lin  *  Bruna Moreira <bruna.moreira@indt.org.br>
20e070ad49SMauricio Lin  *  Edjard Mota <edjard.mota@indt.org.br>
21e070ad49SMauricio Lin  *  Ilias Biris <ilias.biris@indt.org.br>
22e070ad49SMauricio Lin  *  Mauricio Lin <mauricio.lin@indt.org.br>
23e070ad49SMauricio Lin  *
24e070ad49SMauricio Lin  *  Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
25e070ad49SMauricio Lin  *
26e070ad49SMauricio Lin  *  A new process specific entry (smaps) included in /proc. It shows the
27e070ad49SMauricio Lin  *  size of rss for each memory area. The maps entry lacks information
28e070ad49SMauricio Lin  *  about physical memory size (rss) for each mapped file, i.e.,
29e070ad49SMauricio Lin  *  rss information for executables and library files.
30e070ad49SMauricio Lin  *  This additional information is useful for any tools that need to know
31e070ad49SMauricio Lin  *  about physical memory consumption for a process specific library.
32e070ad49SMauricio Lin  *
33e070ad49SMauricio Lin  *  Changelog:
34e070ad49SMauricio Lin  *  21-Feb-2005
35e070ad49SMauricio Lin  *  Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36e070ad49SMauricio Lin  *  Pud inclusion in the page table walking.
37e070ad49SMauricio Lin  *
38e070ad49SMauricio Lin  *  ChangeLog:
39e070ad49SMauricio Lin  *  10-Mar-2005
40e070ad49SMauricio Lin  *  10LE Instituto Nokia de Tecnologia - INdT:
41e070ad49SMauricio Lin  *  A better way to walks through the page table as suggested by Hugh Dickins.
42e070ad49SMauricio Lin  *
43e070ad49SMauricio Lin  *  Simo Piiroinen <simo.piiroinen@nokia.com>:
44e070ad49SMauricio Lin  *  Smaps information related to shared, private, clean and dirty pages.
45e070ad49SMauricio Lin  *
46e070ad49SMauricio Lin  *  Paul Mundt <paul.mundt@nokia.com>:
47e070ad49SMauricio Lin  *  Overall revision about smaps.
481da177e4SLinus Torvalds  */
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds #include <asm/uaccess.h>
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds #include <linux/errno.h>
531da177e4SLinus Torvalds #include <linux/time.h>
541da177e4SLinus Torvalds #include <linux/proc_fs.h>
551da177e4SLinus Torvalds #include <linux/stat.h>
565995477aSAndrea Righi #include <linux/task_io_accounting_ops.h>
571da177e4SLinus Torvalds #include <linux/init.h>
5816f7e0feSRandy Dunlap #include <linux/capability.h>
591da177e4SLinus Torvalds #include <linux/file.h>
609f3acc31SAl Viro #include <linux/fdtable.h>
611da177e4SLinus Torvalds #include <linux/string.h>
621da177e4SLinus Torvalds #include <linux/seq_file.h>
631da177e4SLinus Torvalds #include <linux/namei.h>
646b3286edSKirill Korotaev #include <linux/mnt_namespace.h>
651da177e4SLinus Torvalds #include <linux/mm.h>
66a63d83f4SDavid Rientjes #include <linux/swap.h>
67b835996fSDipankar Sarma #include <linux/rcupdate.h>
681da177e4SLinus Torvalds #include <linux/kallsyms.h>
692ec220e2SKen Chen #include <linux/stacktrace.h>
70d85f50d5SNeil Horman #include <linux/resource.h>
715096add8SKees Cook #include <linux/module.h>
721da177e4SLinus Torvalds #include <linux/mount.h>
731da177e4SLinus Torvalds #include <linux/security.h>
741da177e4SLinus Torvalds #include <linux/ptrace.h>
750d094efeSRoland McGrath #include <linux/tracehook.h>
76a424316cSPaul Menage #include <linux/cgroup.h>
771da177e4SLinus Torvalds #include <linux/cpuset.h>
781da177e4SLinus Torvalds #include <linux/audit.h>
795addc5ddSAl Viro #include <linux/poll.h>
801651e14eSSerge E. Hallyn #include <linux/nsproxy.h>
818ac773b4SAlexey Dobriyan #include <linux/oom.h>
823cb4a0bbSKawai, Hidehiro #include <linux/elf.h>
8360347f67SPavel Emelyanov #include <linux/pid_namespace.h>
845ad4e53bSAl Viro #include <linux/fs_struct.h>
855a0e3ad6STejun Heo #include <linux/slab.h>
86f133eccaSChris Metcalf #ifdef CONFIG_HARDWALL
87f133eccaSChris Metcalf #include <asm/hardwall.h>
88f133eccaSChris Metcalf #endif
891da177e4SLinus Torvalds #include "internal.h"
901da177e4SLinus Torvalds 
910f2fe20fSEric W. Biederman /* NOTE:
920f2fe20fSEric W. Biederman  *	Implementing inode permission operations in /proc is almost
930f2fe20fSEric W. Biederman  *	certainly an error.  Permission checks need to happen during
940f2fe20fSEric W. Biederman  *	each system call not at open time.  The reason is that most of
950f2fe20fSEric W. Biederman  *	what we wish to check for permissions in /proc varies at runtime.
960f2fe20fSEric W. Biederman  *
970f2fe20fSEric W. Biederman  *	The classic example of a problem is opening file descriptors
980f2fe20fSEric W. Biederman  *	in /proc for a task before it execs a suid executable.
990f2fe20fSEric W. Biederman  */
1000f2fe20fSEric W. Biederman 
1011da177e4SLinus Torvalds struct pid_entry {
1021da177e4SLinus Torvalds 	char *name;
103c5141e6dSEric Dumazet 	int len;
1041da177e4SLinus Torvalds 	mode_t mode;
105c5ef1c42SArjan van de Ven 	const struct inode_operations *iop;
10600977a59SArjan van de Ven 	const struct file_operations *fop;
10720cdc894SEric W. Biederman 	union proc_op op;
1081da177e4SLinus Torvalds };
1091da177e4SLinus Torvalds 
11061a28784SEric W. Biederman #define NOD(NAME, MODE, IOP, FOP, OP) {			\
11120cdc894SEric W. Biederman 	.name = (NAME),					\
112c5141e6dSEric Dumazet 	.len  = sizeof(NAME) - 1,			\
11320cdc894SEric W. Biederman 	.mode = MODE,					\
11420cdc894SEric W. Biederman 	.iop  = IOP,					\
11520cdc894SEric W. Biederman 	.fop  = FOP,					\
11620cdc894SEric W. Biederman 	.op   = OP,					\
11720cdc894SEric W. Biederman }
11820cdc894SEric W. Biederman 
119631f9c18SAlexey Dobriyan #define DIR(NAME, MODE, iops, fops)	\
120631f9c18SAlexey Dobriyan 	NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
121631f9c18SAlexey Dobriyan #define LNK(NAME, get_link)					\
12261a28784SEric W. Biederman 	NOD(NAME, (S_IFLNK|S_IRWXUGO),				\
12320cdc894SEric W. Biederman 		&proc_pid_link_inode_operations, NULL,		\
124631f9c18SAlexey Dobriyan 		{ .proc_get_link = get_link } )
125631f9c18SAlexey Dobriyan #define REG(NAME, MODE, fops)				\
126631f9c18SAlexey Dobriyan 	NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
127631f9c18SAlexey Dobriyan #define INF(NAME, MODE, read)				\
12861a28784SEric W. Biederman 	NOD(NAME, (S_IFREG|(MODE)), 			\
12920cdc894SEric W. Biederman 		NULL, &proc_info_file_operations,	\
130631f9c18SAlexey Dobriyan 		{ .proc_read = read } )
131631f9c18SAlexey Dobriyan #define ONE(NAME, MODE, show)				\
132be614086SEric W. Biederman 	NOD(NAME, (S_IFREG|(MODE)), 			\
133be614086SEric W. Biederman 		NULL, &proc_single_file_operations,	\
134631f9c18SAlexey Dobriyan 		{ .proc_show = show } )
1351da177e4SLinus Torvalds 
136aed54175SVegard Nossum /*
137aed54175SVegard Nossum  * Count the number of hardlinks for the pid_entry table, excluding the .
138aed54175SVegard Nossum  * and .. links.
139aed54175SVegard Nossum  */
140aed54175SVegard Nossum static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
141aed54175SVegard Nossum 	unsigned int n)
142aed54175SVegard Nossum {
143aed54175SVegard Nossum 	unsigned int i;
144aed54175SVegard Nossum 	unsigned int count;
145aed54175SVegard Nossum 
146aed54175SVegard Nossum 	count = 0;
147aed54175SVegard Nossum 	for (i = 0; i < n; ++i) {
148aed54175SVegard Nossum 		if (S_ISDIR(entries[i].mode))
149aed54175SVegard Nossum 			++count;
150aed54175SVegard Nossum 	}
151aed54175SVegard Nossum 
152aed54175SVegard Nossum 	return count;
153aed54175SVegard Nossum }
154aed54175SVegard Nossum 
155f7ad3c6bSMiklos Szeredi static int get_task_root(struct task_struct *task, struct path *root)
1561da177e4SLinus Torvalds {
1577c2c7d99SHugh Dickins 	int result = -ENOENT;
1587c2c7d99SHugh Dickins 
1590494f6ecSMiklos Szeredi 	task_lock(task);
160f7ad3c6bSMiklos Szeredi 	if (task->fs) {
161f7ad3c6bSMiklos Szeredi 		get_fs_root(task->fs, root);
1627c2c7d99SHugh Dickins 		result = 0;
1637c2c7d99SHugh Dickins 	}
1640494f6ecSMiklos Szeredi 	task_unlock(task);
1657c2c7d99SHugh Dickins 	return result;
1660494f6ecSMiklos Szeredi }
1670494f6ecSMiklos Szeredi 
1683dcd25f3SJan Blunck static int proc_cwd_link(struct inode *inode, struct path *path)
1690494f6ecSMiklos Szeredi {
17099f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
1710494f6ecSMiklos Szeredi 	int result = -ENOENT;
17299f89551SEric W. Biederman 
17399f89551SEric W. Biederman 	if (task) {
174f7ad3c6bSMiklos Szeredi 		task_lock(task);
175f7ad3c6bSMiklos Szeredi 		if (task->fs) {
176f7ad3c6bSMiklos Szeredi 			get_fs_pwd(task->fs, path);
177f7ad3c6bSMiklos Szeredi 			result = 0;
178f7ad3c6bSMiklos Szeredi 		}
179f7ad3c6bSMiklos Szeredi 		task_unlock(task);
18099f89551SEric W. Biederman 		put_task_struct(task);
18199f89551SEric W. Biederman 	}
1821da177e4SLinus Torvalds 	return result;
1831da177e4SLinus Torvalds }
1841da177e4SLinus Torvalds 
1853dcd25f3SJan Blunck static int proc_root_link(struct inode *inode, struct path *path)
1861da177e4SLinus Torvalds {
18799f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
1881da177e4SLinus Torvalds 	int result = -ENOENT;
18999f89551SEric W. Biederman 
19099f89551SEric W. Biederman 	if (task) {
191f7ad3c6bSMiklos Szeredi 		result = get_task_root(task, path);
19299f89551SEric W. Biederman 		put_task_struct(task);
19399f89551SEric W. Biederman 	}
1941da177e4SLinus Torvalds 	return result;
1951da177e4SLinus Torvalds }
1961da177e4SLinus Torvalds 
1978b0db9dbSStephen Wilson static struct mm_struct *__check_mem_permission(struct task_struct *task)
198638fa202SRoland McGrath {
1998b0db9dbSStephen Wilson 	struct mm_struct *mm;
2008b0db9dbSStephen Wilson 
2018b0db9dbSStephen Wilson 	mm = get_task_mm(task);
2028b0db9dbSStephen Wilson 	if (!mm)
2038b0db9dbSStephen Wilson 		return ERR_PTR(-EINVAL);
2048b0db9dbSStephen Wilson 
205638fa202SRoland McGrath 	/*
206638fa202SRoland McGrath 	 * A task can always look at itself, in case it chooses
207638fa202SRoland McGrath 	 * to use system calls instead of load instructions.
208638fa202SRoland McGrath 	 */
209638fa202SRoland McGrath 	if (task == current)
2108b0db9dbSStephen Wilson 		return mm;
211638fa202SRoland McGrath 
212638fa202SRoland McGrath 	/*
213638fa202SRoland McGrath 	 * If current is actively ptrace'ing, and would also be
214638fa202SRoland McGrath 	 * permitted to freshly attach with ptrace now, permit it.
215638fa202SRoland McGrath 	 */
2160d094efeSRoland McGrath 	if (task_is_stopped_or_traced(task)) {
2170d094efeSRoland McGrath 		int match;
2180d094efeSRoland McGrath 		rcu_read_lock();
21906d98473STejun Heo 		match = (ptrace_parent(task) == current);
2200d094efeSRoland McGrath 		rcu_read_unlock();
2210d094efeSRoland McGrath 		if (match && ptrace_may_access(task, PTRACE_MODE_ATTACH))
2228b0db9dbSStephen Wilson 			return mm;
2230d094efeSRoland McGrath 	}
224638fa202SRoland McGrath 
225638fa202SRoland McGrath 	/*
226638fa202SRoland McGrath 	 * No one else is allowed.
227638fa202SRoland McGrath 	 */
2288b0db9dbSStephen Wilson 	mmput(mm);
2298b0db9dbSStephen Wilson 	return ERR_PTR(-EPERM);
230638fa202SRoland McGrath }
2311da177e4SLinus Torvalds 
23218f661bcSStephen Wilson /*
2338b0db9dbSStephen Wilson  * If current may access user memory in @task return a reference to the
2348b0db9dbSStephen Wilson  * corresponding mm, otherwise ERR_PTR.
23518f661bcSStephen Wilson  */
2368b0db9dbSStephen Wilson static struct mm_struct *check_mem_permission(struct task_struct *task)
23718f661bcSStephen Wilson {
2388b0db9dbSStephen Wilson 	struct mm_struct *mm;
23918f661bcSStephen Wilson 	int err;
24018f661bcSStephen Wilson 
24118f661bcSStephen Wilson 	/*
24218f661bcSStephen Wilson 	 * Avoid racing if task exec's as we might get a new mm but validate
24318f661bcSStephen Wilson 	 * against old credentials.
24418f661bcSStephen Wilson 	 */
24518f661bcSStephen Wilson 	err = mutex_lock_killable(&task->signal->cred_guard_mutex);
24618f661bcSStephen Wilson 	if (err)
2478b0db9dbSStephen Wilson 		return ERR_PTR(err);
24818f661bcSStephen Wilson 
2498b0db9dbSStephen Wilson 	mm = __check_mem_permission(task);
25018f661bcSStephen Wilson 	mutex_unlock(&task->signal->cred_guard_mutex);
25118f661bcSStephen Wilson 
2528b0db9dbSStephen Wilson 	return mm;
2531da177e4SLinus Torvalds }
2541da177e4SLinus Torvalds 
255831830b5SAl Viro struct mm_struct *mm_for_maps(struct task_struct *task)
256831830b5SAl Viro {
257704b836cSOleg Nesterov 	struct mm_struct *mm;
258ec6fd8a4SAl Viro 	int err;
25900f89d21SOleg Nesterov 
260ec6fd8a4SAl Viro 	err =  mutex_lock_killable(&task->signal->cred_guard_mutex);
261ec6fd8a4SAl Viro 	if (err)
262ec6fd8a4SAl Viro 		return ERR_PTR(err);
263704b836cSOleg Nesterov 
264704b836cSOleg Nesterov 	mm = get_task_mm(task);
265704b836cSOleg Nesterov 	if (mm && mm != current->mm &&
266704b836cSOleg Nesterov 			!ptrace_may_access(task, PTRACE_MODE_READ)) {
267831830b5SAl Viro 		mmput(mm);
268ec6fd8a4SAl Viro 		mm = ERR_PTR(-EACCES);
269831830b5SAl Viro 	}
2709b1bf12dSKOSAKI Motohiro 	mutex_unlock(&task->signal->cred_guard_mutex);
271704b836cSOleg Nesterov 
27213f0feafSOleg Nesterov 	return mm;
27313f0feafSOleg Nesterov }
274831830b5SAl Viro 
2751da177e4SLinus Torvalds static int proc_pid_cmdline(struct task_struct *task, char * buffer)
2761da177e4SLinus Torvalds {
2771da177e4SLinus Torvalds 	int res = 0;
2781da177e4SLinus Torvalds 	unsigned int len;
2791da177e4SLinus Torvalds 	struct mm_struct *mm = get_task_mm(task);
2801da177e4SLinus Torvalds 	if (!mm)
2811da177e4SLinus Torvalds 		goto out;
2821da177e4SLinus Torvalds 	if (!mm->arg_end)
2831da177e4SLinus Torvalds 		goto out_mm;	/* Shh! No looking before we're done */
2841da177e4SLinus Torvalds 
2851da177e4SLinus Torvalds  	len = mm->arg_end - mm->arg_start;
2861da177e4SLinus Torvalds 
2871da177e4SLinus Torvalds 	if (len > PAGE_SIZE)
2881da177e4SLinus Torvalds 		len = PAGE_SIZE;
2891da177e4SLinus Torvalds 
2901da177e4SLinus Torvalds 	res = access_process_vm(task, mm->arg_start, buffer, len, 0);
2911da177e4SLinus Torvalds 
2921da177e4SLinus Torvalds 	// If the nul at the end of args has been overwritten, then
2931da177e4SLinus Torvalds 	// assume application is using setproctitle(3).
2941da177e4SLinus Torvalds 	if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
2951da177e4SLinus Torvalds 		len = strnlen(buffer, res);
2961da177e4SLinus Torvalds 		if (len < res) {
2971da177e4SLinus Torvalds 		    res = len;
2981da177e4SLinus Torvalds 		} else {
2991da177e4SLinus Torvalds 			len = mm->env_end - mm->env_start;
3001da177e4SLinus Torvalds 			if (len > PAGE_SIZE - res)
3011da177e4SLinus Torvalds 				len = PAGE_SIZE - res;
3021da177e4SLinus Torvalds 			res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
3031da177e4SLinus Torvalds 			res = strnlen(buffer, res);
3041da177e4SLinus Torvalds 		}
3051da177e4SLinus Torvalds 	}
3061da177e4SLinus Torvalds out_mm:
3071da177e4SLinus Torvalds 	mmput(mm);
3081da177e4SLinus Torvalds out:
3091da177e4SLinus Torvalds 	return res;
3101da177e4SLinus Torvalds }
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds static int proc_pid_auxv(struct task_struct *task, char *buffer)
3131da177e4SLinus Torvalds {
3142fadaef4SAl Viro 	struct mm_struct *mm = mm_for_maps(task);
3152fadaef4SAl Viro 	int res = PTR_ERR(mm);
3162fadaef4SAl Viro 	if (mm && !IS_ERR(mm)) {
3171da177e4SLinus Torvalds 		unsigned int nwords = 0;
318dfe6b7d9SHannes Eder 		do {
3191da177e4SLinus Torvalds 			nwords += 2;
320dfe6b7d9SHannes Eder 		} while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
3211da177e4SLinus Torvalds 		res = nwords * sizeof(mm->saved_auxv[0]);
3221da177e4SLinus Torvalds 		if (res > PAGE_SIZE)
3231da177e4SLinus Torvalds 			res = PAGE_SIZE;
3241da177e4SLinus Torvalds 		memcpy(buffer, mm->saved_auxv, res);
3251da177e4SLinus Torvalds 		mmput(mm);
3261da177e4SLinus Torvalds 	}
3271da177e4SLinus Torvalds 	return res;
3281da177e4SLinus Torvalds }
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds 
3311da177e4SLinus Torvalds #ifdef CONFIG_KALLSYMS
3321da177e4SLinus Torvalds /*
3331da177e4SLinus Torvalds  * Provides a wchan file via kallsyms in a proper one-value-per-file format.
3341da177e4SLinus Torvalds  * Returns the resolved symbol.  If that fails, simply return the address.
3351da177e4SLinus Torvalds  */
3361da177e4SLinus Torvalds static int proc_pid_wchan(struct task_struct *task, char *buffer)
3371da177e4SLinus Torvalds {
338ffb45122SAlexey Dobriyan 	unsigned long wchan;
3399281aceaSTejun Heo 	char symname[KSYM_NAME_LEN];
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds 	wchan = get_wchan(task);
3421da177e4SLinus Torvalds 
3439d65cb4aSAlexey Dobriyan 	if (lookup_symbol_name(wchan, symname) < 0)
344f83ce3e6SJake Edge 		if (!ptrace_may_access(task, PTRACE_MODE_READ))
345f83ce3e6SJake Edge 			return 0;
346f83ce3e6SJake Edge 		else
3471da177e4SLinus Torvalds 			return sprintf(buffer, "%lu", wchan);
3489d65cb4aSAlexey Dobriyan 	else
3499d65cb4aSAlexey Dobriyan 		return sprintf(buffer, "%s", symname);
3501da177e4SLinus Torvalds }
3511da177e4SLinus Torvalds #endif /* CONFIG_KALLSYMS */
3521da177e4SLinus Torvalds 
353a9712bc1SAl Viro static int lock_trace(struct task_struct *task)
354a9712bc1SAl Viro {
355a9712bc1SAl Viro 	int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
356a9712bc1SAl Viro 	if (err)
357a9712bc1SAl Viro 		return err;
358a9712bc1SAl Viro 	if (!ptrace_may_access(task, PTRACE_MODE_ATTACH)) {
359a9712bc1SAl Viro 		mutex_unlock(&task->signal->cred_guard_mutex);
360a9712bc1SAl Viro 		return -EPERM;
361a9712bc1SAl Viro 	}
362a9712bc1SAl Viro 	return 0;
363a9712bc1SAl Viro }
364a9712bc1SAl Viro 
365a9712bc1SAl Viro static void unlock_trace(struct task_struct *task)
366a9712bc1SAl Viro {
367a9712bc1SAl Viro 	mutex_unlock(&task->signal->cred_guard_mutex);
368a9712bc1SAl Viro }
369a9712bc1SAl Viro 
3702ec220e2SKen Chen #ifdef CONFIG_STACKTRACE
3712ec220e2SKen Chen 
3722ec220e2SKen Chen #define MAX_STACK_TRACE_DEPTH	64
3732ec220e2SKen Chen 
3742ec220e2SKen Chen static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
3752ec220e2SKen Chen 			  struct pid *pid, struct task_struct *task)
3762ec220e2SKen Chen {
3772ec220e2SKen Chen 	struct stack_trace trace;
3782ec220e2SKen Chen 	unsigned long *entries;
379a9712bc1SAl Viro 	int err;
3802ec220e2SKen Chen 	int i;
3812ec220e2SKen Chen 
3822ec220e2SKen Chen 	entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
3832ec220e2SKen Chen 	if (!entries)
3842ec220e2SKen Chen 		return -ENOMEM;
3852ec220e2SKen Chen 
3862ec220e2SKen Chen 	trace.nr_entries	= 0;
3872ec220e2SKen Chen 	trace.max_entries	= MAX_STACK_TRACE_DEPTH;
3882ec220e2SKen Chen 	trace.entries		= entries;
3892ec220e2SKen Chen 	trace.skip		= 0;
390a9712bc1SAl Viro 
391a9712bc1SAl Viro 	err = lock_trace(task);
392a9712bc1SAl Viro 	if (!err) {
3932ec220e2SKen Chen 		save_stack_trace_tsk(task, &trace);
3942ec220e2SKen Chen 
3952ec220e2SKen Chen 		for (i = 0; i < trace.nr_entries; i++) {
39651e03149SKonstantin Khlebnikov 			seq_printf(m, "[<%pK>] %pS\n",
3972ec220e2SKen Chen 				   (void *)entries[i], (void *)entries[i]);
3982ec220e2SKen Chen 		}
399a9712bc1SAl Viro 		unlock_trace(task);
400a9712bc1SAl Viro 	}
4012ec220e2SKen Chen 	kfree(entries);
4022ec220e2SKen Chen 
403a9712bc1SAl Viro 	return err;
4042ec220e2SKen Chen }
4052ec220e2SKen Chen #endif
4062ec220e2SKen Chen 
4071da177e4SLinus Torvalds #ifdef CONFIG_SCHEDSTATS
4081da177e4SLinus Torvalds /*
4091da177e4SLinus Torvalds  * Provides /proc/PID/schedstat
4101da177e4SLinus Torvalds  */
4111da177e4SLinus Torvalds static int proc_pid_schedstat(struct task_struct *task, char *buffer)
4121da177e4SLinus Torvalds {
413172ba844SBalbir Singh 	return sprintf(buffer, "%llu %llu %lu\n",
414826e08b0SIngo Molnar 			(unsigned long long)task->se.sum_exec_runtime,
415826e08b0SIngo Molnar 			(unsigned long long)task->sched_info.run_delay,
4162d72376bSIngo Molnar 			task->sched_info.pcount);
4171da177e4SLinus Torvalds }
4181da177e4SLinus Torvalds #endif
4191da177e4SLinus Torvalds 
4209745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP
4219745512cSArjan van de Ven static int lstats_show_proc(struct seq_file *m, void *v)
4229745512cSArjan van de Ven {
4239745512cSArjan van de Ven 	int i;
42413d77c37SHiroshi Shimamoto 	struct inode *inode = m->private;
42513d77c37SHiroshi Shimamoto 	struct task_struct *task = get_proc_task(inode);
4269745512cSArjan van de Ven 
42713d77c37SHiroshi Shimamoto 	if (!task)
42813d77c37SHiroshi Shimamoto 		return -ESRCH;
42913d77c37SHiroshi Shimamoto 	seq_puts(m, "Latency Top version : v0.1\n");
4309745512cSArjan van de Ven 	for (i = 0; i < 32; i++) {
43134e49d4fSJoe Perches 		struct latency_record *lr = &task->latency_record[i];
43234e49d4fSJoe Perches 		if (lr->backtrace[0]) {
4339745512cSArjan van de Ven 			int q;
4349745512cSArjan van de Ven 			seq_printf(m, "%i %li %li",
43534e49d4fSJoe Perches 				   lr->count, lr->time, lr->max);
4369745512cSArjan van de Ven 			for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
43734e49d4fSJoe Perches 				unsigned long bt = lr->backtrace[q];
43834e49d4fSJoe Perches 				if (!bt)
4399745512cSArjan van de Ven 					break;
44034e49d4fSJoe Perches 				if (bt == ULONG_MAX)
4419745512cSArjan van de Ven 					break;
44234e49d4fSJoe Perches 				seq_printf(m, " %ps", (void *)bt);
4439745512cSArjan van de Ven 			}
4449d6de12fSAlexey Dobriyan 			seq_putc(m, '\n');
4459745512cSArjan van de Ven 		}
4469745512cSArjan van de Ven 
4479745512cSArjan van de Ven 	}
44813d77c37SHiroshi Shimamoto 	put_task_struct(task);
4499745512cSArjan van de Ven 	return 0;
4509745512cSArjan van de Ven }
4519745512cSArjan van de Ven 
4529745512cSArjan van de Ven static int lstats_open(struct inode *inode, struct file *file)
4539745512cSArjan van de Ven {
45413d77c37SHiroshi Shimamoto 	return single_open(file, lstats_show_proc, inode);
455d6643d12SHiroshi Shimamoto }
456d6643d12SHiroshi Shimamoto 
4579745512cSArjan van de Ven static ssize_t lstats_write(struct file *file, const char __user *buf,
4589745512cSArjan van de Ven 			    size_t count, loff_t *offs)
4599745512cSArjan van de Ven {
46013d77c37SHiroshi Shimamoto 	struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
4619745512cSArjan van de Ven 
46213d77c37SHiroshi Shimamoto 	if (!task)
46313d77c37SHiroshi Shimamoto 		return -ESRCH;
4649745512cSArjan van de Ven 	clear_all_latency_tracing(task);
46513d77c37SHiroshi Shimamoto 	put_task_struct(task);
4669745512cSArjan van de Ven 
4679745512cSArjan van de Ven 	return count;
4689745512cSArjan van de Ven }
4699745512cSArjan van de Ven 
4709745512cSArjan van de Ven static const struct file_operations proc_lstats_operations = {
4719745512cSArjan van de Ven 	.open		= lstats_open,
4729745512cSArjan van de Ven 	.read		= seq_read,
4739745512cSArjan van de Ven 	.write		= lstats_write,
4749745512cSArjan van de Ven 	.llseek		= seq_lseek,
47513d77c37SHiroshi Shimamoto 	.release	= single_release,
4769745512cSArjan van de Ven };
4779745512cSArjan van de Ven 
4789745512cSArjan van de Ven #endif
4799745512cSArjan van de Ven 
4801da177e4SLinus Torvalds static int proc_oom_score(struct task_struct *task, char *buffer)
4811da177e4SLinus Torvalds {
482b95c35e7SOleg Nesterov 	unsigned long points = 0;
4831da177e4SLinus Torvalds 
48419c5d45aSAlexey Dobriyan 	read_lock(&tasklist_lock);
485b95c35e7SOleg Nesterov 	if (pid_alive(task))
486a63d83f4SDavid Rientjes 		points = oom_badness(task, NULL, NULL,
487a63d83f4SDavid Rientjes 					totalram_pages + total_swap_pages);
48819c5d45aSAlexey Dobriyan 	read_unlock(&tasklist_lock);
4891da177e4SLinus Torvalds 	return sprintf(buffer, "%lu\n", points);
4901da177e4SLinus Torvalds }
4911da177e4SLinus Torvalds 
492d85f50d5SNeil Horman struct limit_names {
493d85f50d5SNeil Horman 	char *name;
494d85f50d5SNeil Horman 	char *unit;
495d85f50d5SNeil Horman };
496d85f50d5SNeil Horman 
497d85f50d5SNeil Horman static const struct limit_names lnames[RLIM_NLIMITS] = {
498cff4edb5SKees Cook 	[RLIMIT_CPU] = {"Max cpu time", "seconds"},
499d85f50d5SNeil Horman 	[RLIMIT_FSIZE] = {"Max file size", "bytes"},
500d85f50d5SNeil Horman 	[RLIMIT_DATA] = {"Max data size", "bytes"},
501d85f50d5SNeil Horman 	[RLIMIT_STACK] = {"Max stack size", "bytes"},
502d85f50d5SNeil Horman 	[RLIMIT_CORE] = {"Max core file size", "bytes"},
503d85f50d5SNeil Horman 	[RLIMIT_RSS] = {"Max resident set", "bytes"},
504d85f50d5SNeil Horman 	[RLIMIT_NPROC] = {"Max processes", "processes"},
505d85f50d5SNeil Horman 	[RLIMIT_NOFILE] = {"Max open files", "files"},
506d85f50d5SNeil Horman 	[RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
507d85f50d5SNeil Horman 	[RLIMIT_AS] = {"Max address space", "bytes"},
508d85f50d5SNeil Horman 	[RLIMIT_LOCKS] = {"Max file locks", "locks"},
509d85f50d5SNeil Horman 	[RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
510d85f50d5SNeil Horman 	[RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
511d85f50d5SNeil Horman 	[RLIMIT_NICE] = {"Max nice priority", NULL},
512d85f50d5SNeil Horman 	[RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
5138808117cSEugene Teo 	[RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
514d85f50d5SNeil Horman };
515d85f50d5SNeil Horman 
516d85f50d5SNeil Horman /* Display limits for a process */
517d85f50d5SNeil Horman static int proc_pid_limits(struct task_struct *task, char *buffer)
518d85f50d5SNeil Horman {
519d85f50d5SNeil Horman 	unsigned int i;
520d85f50d5SNeil Horman 	int count = 0;
521d85f50d5SNeil Horman 	unsigned long flags;
522d85f50d5SNeil Horman 	char *bufptr = buffer;
523d85f50d5SNeil Horman 
524d85f50d5SNeil Horman 	struct rlimit rlim[RLIM_NLIMITS];
525d85f50d5SNeil Horman 
526a6bebbc8SLai Jiangshan 	if (!lock_task_sighand(task, &flags))
527d85f50d5SNeil Horman 		return 0;
528d85f50d5SNeil Horman 	memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
529d85f50d5SNeil Horman 	unlock_task_sighand(task, &flags);
530d85f50d5SNeil Horman 
531d85f50d5SNeil Horman 	/*
532d85f50d5SNeil Horman 	 * print the file header
533d85f50d5SNeil Horman 	 */
534d85f50d5SNeil Horman 	count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
535d85f50d5SNeil Horman 			"Limit", "Soft Limit", "Hard Limit", "Units");
536d85f50d5SNeil Horman 
537d85f50d5SNeil Horman 	for (i = 0; i < RLIM_NLIMITS; i++) {
538d85f50d5SNeil Horman 		if (rlim[i].rlim_cur == RLIM_INFINITY)
539d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-25s %-20s ",
540d85f50d5SNeil Horman 					 lnames[i].name, "unlimited");
541d85f50d5SNeil Horman 		else
542d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-25s %-20lu ",
543d85f50d5SNeil Horman 					 lnames[i].name, rlim[i].rlim_cur);
544d85f50d5SNeil Horman 
545d85f50d5SNeil Horman 		if (rlim[i].rlim_max == RLIM_INFINITY)
546d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-20s ", "unlimited");
547d85f50d5SNeil Horman 		else
548d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-20lu ",
549d85f50d5SNeil Horman 					 rlim[i].rlim_max);
550d85f50d5SNeil Horman 
551d85f50d5SNeil Horman 		if (lnames[i].unit)
552d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-10s\n",
553d85f50d5SNeil Horman 					 lnames[i].unit);
554d85f50d5SNeil Horman 		else
555d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "\n");
556d85f50d5SNeil Horman 	}
557d85f50d5SNeil Horman 
558d85f50d5SNeil Horman 	return count;
559d85f50d5SNeil Horman }
560d85f50d5SNeil Horman 
561ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
562ebcb6734SRoland McGrath static int proc_pid_syscall(struct task_struct *task, char *buffer)
563ebcb6734SRoland McGrath {
564ebcb6734SRoland McGrath 	long nr;
565ebcb6734SRoland McGrath 	unsigned long args[6], sp, pc;
566a9712bc1SAl Viro 	int res = lock_trace(task);
567a9712bc1SAl Viro 	if (res)
568a9712bc1SAl Viro 		return res;
569ebcb6734SRoland McGrath 
570ebcb6734SRoland McGrath 	if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
571a9712bc1SAl Viro 		res = sprintf(buffer, "running\n");
572a9712bc1SAl Viro 	else if (nr < 0)
573a9712bc1SAl Viro 		res = sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
574a9712bc1SAl Viro 	else
575a9712bc1SAl Viro 		res = sprintf(buffer,
576ebcb6734SRoland McGrath 		       "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
577ebcb6734SRoland McGrath 		       nr,
578ebcb6734SRoland McGrath 		       args[0], args[1], args[2], args[3], args[4], args[5],
579ebcb6734SRoland McGrath 		       sp, pc);
580a9712bc1SAl Viro 	unlock_trace(task);
581a9712bc1SAl Viro 	return res;
582ebcb6734SRoland McGrath }
583ebcb6734SRoland McGrath #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
584ebcb6734SRoland McGrath 
5851da177e4SLinus Torvalds /************************************************************************/
5861da177e4SLinus Torvalds /*                       Here the fs part begins                        */
5871da177e4SLinus Torvalds /************************************************************************/
5881da177e4SLinus Torvalds 
5891da177e4SLinus Torvalds /* permission checks */
590778c1144SEric W. Biederman static int proc_fd_access_allowed(struct inode *inode)
5911da177e4SLinus Torvalds {
592778c1144SEric W. Biederman 	struct task_struct *task;
593778c1144SEric W. Biederman 	int allowed = 0;
594df26c40eSEric W. Biederman 	/* Allow access to a task's file descriptors if it is us or we
595df26c40eSEric W. Biederman 	 * may use ptrace attach to the process and find out that
596df26c40eSEric W. Biederman 	 * information.
597778c1144SEric W. Biederman 	 */
598778c1144SEric W. Biederman 	task = get_proc_task(inode);
599df26c40eSEric W. Biederman 	if (task) {
600006ebb40SStephen Smalley 		allowed = ptrace_may_access(task, PTRACE_MODE_READ);
601778c1144SEric W. Biederman 		put_task_struct(task);
602df26c40eSEric W. Biederman 	}
603778c1144SEric W. Biederman 	return allowed;
6041da177e4SLinus Torvalds }
6051da177e4SLinus Torvalds 
6066b4e306aSEric W. Biederman int proc_setattr(struct dentry *dentry, struct iattr *attr)
6076d76fa58SLinus Torvalds {
6086d76fa58SLinus Torvalds 	int error;
6096d76fa58SLinus Torvalds 	struct inode *inode = dentry->d_inode;
6106d76fa58SLinus Torvalds 
6116d76fa58SLinus Torvalds 	if (attr->ia_valid & ATTR_MODE)
6126d76fa58SLinus Torvalds 		return -EPERM;
6136d76fa58SLinus Torvalds 
6146d76fa58SLinus Torvalds 	error = inode_change_ok(inode, attr);
6151025774cSChristoph Hellwig 	if (error)
6166d76fa58SLinus Torvalds 		return error;
6171025774cSChristoph Hellwig 
6181025774cSChristoph Hellwig 	if ((attr->ia_valid & ATTR_SIZE) &&
6191025774cSChristoph Hellwig 	    attr->ia_size != i_size_read(inode)) {
6201025774cSChristoph Hellwig 		error = vmtruncate(inode, attr->ia_size);
6211025774cSChristoph Hellwig 		if (error)
6221025774cSChristoph Hellwig 			return error;
6231025774cSChristoph Hellwig 	}
6241025774cSChristoph Hellwig 
6251025774cSChristoph Hellwig 	setattr_copy(inode, attr);
6261025774cSChristoph Hellwig 	mark_inode_dirty(inode);
6271025774cSChristoph Hellwig 	return 0;
6286d76fa58SLinus Torvalds }
6296d76fa58SLinus Torvalds 
630c5ef1c42SArjan van de Ven static const struct inode_operations proc_def_inode_operations = {
6316d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
6326d76fa58SLinus Torvalds };
6336d76fa58SLinus Torvalds 
634a1a2c409SMiklos Szeredi static int mounts_open_common(struct inode *inode, struct file *file,
635a1a2c409SMiklos Szeredi 			      const struct seq_operations *op)
6361da177e4SLinus Torvalds {
63799f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
638cf7b708cSPavel Emelyanov 	struct nsproxy *nsp;
6396b3286edSKirill Korotaev 	struct mnt_namespace *ns = NULL;
640a1a2c409SMiklos Szeredi 	struct path root;
6415addc5ddSAl Viro 	struct proc_mounts *p;
6425addc5ddSAl Viro 	int ret = -EINVAL;
6435addc5ddSAl Viro 
64499f89551SEric W. Biederman 	if (task) {
645cf7b708cSPavel Emelyanov 		rcu_read_lock();
646cf7b708cSPavel Emelyanov 		nsp = task_nsproxy(task);
647cf7b708cSPavel Emelyanov 		if (nsp) {
648cf7b708cSPavel Emelyanov 			ns = nsp->mnt_ns;
6496b3286edSKirill Korotaev 			if (ns)
6506b3286edSKirill Korotaev 				get_mnt_ns(ns);
651863c4702SAlexey Dobriyan 		}
652cf7b708cSPavel Emelyanov 		rcu_read_unlock();
653f7ad3c6bSMiklos Szeredi 		if (ns && get_task_root(task, &root) == 0)
6547c2c7d99SHugh Dickins 			ret = 0;
65599f89551SEric W. Biederman 		put_task_struct(task);
65699f89551SEric W. Biederman 	}
6571da177e4SLinus Torvalds 
658a1a2c409SMiklos Szeredi 	if (!ns)
659a1a2c409SMiklos Szeredi 		goto err;
6607c2c7d99SHugh Dickins 	if (ret)
661a1a2c409SMiklos Szeredi 		goto err_put_ns;
662a1a2c409SMiklos Szeredi 
6635addc5ddSAl Viro 	ret = -ENOMEM;
6645addc5ddSAl Viro 	p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
665a1a2c409SMiklos Szeredi 	if (!p)
666a1a2c409SMiklos Szeredi 		goto err_put_path;
667a1a2c409SMiklos Szeredi 
6685addc5ddSAl Viro 	file->private_data = &p->m;
669a1a2c409SMiklos Szeredi 	ret = seq_open(file, op);
670a1a2c409SMiklos Szeredi 	if (ret)
671a1a2c409SMiklos Szeredi 		goto err_free;
672a1a2c409SMiklos Szeredi 
673a1a2c409SMiklos Szeredi 	p->m.private = p;
674a1a2c409SMiklos Szeredi 	p->ns = ns;
675a1a2c409SMiklos Szeredi 	p->root = root;
676f1514638SKay Sievers 	p->m.poll_event = ns->event;
677a1a2c409SMiklos Szeredi 
6785addc5ddSAl Viro 	return 0;
679a1a2c409SMiklos Szeredi 
680a1a2c409SMiklos Szeredi  err_free:
6815addc5ddSAl Viro 	kfree(p);
682a1a2c409SMiklos Szeredi  err_put_path:
683a1a2c409SMiklos Szeredi 	path_put(&root);
684a1a2c409SMiklos Szeredi  err_put_ns:
6856b3286edSKirill Korotaev 	put_mnt_ns(ns);
686a1a2c409SMiklos Szeredi  err:
6871da177e4SLinus Torvalds 	return ret;
6881da177e4SLinus Torvalds }
6891da177e4SLinus Torvalds 
6901da177e4SLinus Torvalds static int mounts_release(struct inode *inode, struct file *file)
6911da177e4SLinus Torvalds {
692a1a2c409SMiklos Szeredi 	struct proc_mounts *p = file->private_data;
693a1a2c409SMiklos Szeredi 	path_put(&p->root);
694a1a2c409SMiklos Szeredi 	put_mnt_ns(p->ns);
6951da177e4SLinus Torvalds 	return seq_release(inode, file);
6961da177e4SLinus Torvalds }
6971da177e4SLinus Torvalds 
6985addc5ddSAl Viro static unsigned mounts_poll(struct file *file, poll_table *wait)
6995addc5ddSAl Viro {
7005addc5ddSAl Viro 	struct proc_mounts *p = file->private_data;
70131b07093SKOSAKI Motohiro 	unsigned res = POLLIN | POLLRDNORM;
7025addc5ddSAl Viro 
7039f5596afSAl Viro 	poll_wait(file, &p->ns->poll, wait);
7049f5596afSAl Viro 	if (mnt_had_events(p))
70531b07093SKOSAKI Motohiro 		res |= POLLERR | POLLPRI;
7065addc5ddSAl Viro 
7075addc5ddSAl Viro 	return res;
7085addc5ddSAl Viro }
7095addc5ddSAl Viro 
710a1a2c409SMiklos Szeredi static int mounts_open(struct inode *inode, struct file *file)
711a1a2c409SMiklos Szeredi {
712a1a2c409SMiklos Szeredi 	return mounts_open_common(inode, file, &mounts_op);
713a1a2c409SMiklos Szeredi }
714a1a2c409SMiklos Szeredi 
71500977a59SArjan van de Ven static const struct file_operations proc_mounts_operations = {
7161da177e4SLinus Torvalds 	.open		= mounts_open,
7171da177e4SLinus Torvalds 	.read		= seq_read,
7181da177e4SLinus Torvalds 	.llseek		= seq_lseek,
7191da177e4SLinus Torvalds 	.release	= mounts_release,
7205addc5ddSAl Viro 	.poll		= mounts_poll,
7211da177e4SLinus Torvalds };
7221da177e4SLinus Torvalds 
7232d4d4864SRam Pai static int mountinfo_open(struct inode *inode, struct file *file)
7242d4d4864SRam Pai {
7252d4d4864SRam Pai 	return mounts_open_common(inode, file, &mountinfo_op);
7262d4d4864SRam Pai }
7272d4d4864SRam Pai 
7282d4d4864SRam Pai static const struct file_operations proc_mountinfo_operations = {
7292d4d4864SRam Pai 	.open		= mountinfo_open,
7302d4d4864SRam Pai 	.read		= seq_read,
7312d4d4864SRam Pai 	.llseek		= seq_lseek,
7322d4d4864SRam Pai 	.release	= mounts_release,
7332d4d4864SRam Pai 	.poll		= mounts_poll,
7342d4d4864SRam Pai };
7352d4d4864SRam Pai 
736b4629fe2SChuck Lever static int mountstats_open(struct inode *inode, struct file *file)
737b4629fe2SChuck Lever {
738a1a2c409SMiklos Szeredi 	return mounts_open_common(inode, file, &mountstats_op);
739b4629fe2SChuck Lever }
740b4629fe2SChuck Lever 
74100977a59SArjan van de Ven static const struct file_operations proc_mountstats_operations = {
742b4629fe2SChuck Lever 	.open		= mountstats_open,
743b4629fe2SChuck Lever 	.read		= seq_read,
744b4629fe2SChuck Lever 	.llseek		= seq_lseek,
745b4629fe2SChuck Lever 	.release	= mounts_release,
746b4629fe2SChuck Lever };
747b4629fe2SChuck Lever 
7481da177e4SLinus Torvalds #define PROC_BLOCK_SIZE	(3*1024)		/* 4K page size but our output routines use some slack for overruns */
7491da177e4SLinus Torvalds 
7501da177e4SLinus Torvalds static ssize_t proc_info_read(struct file * file, char __user * buf,
7511da177e4SLinus Torvalds 			  size_t count, loff_t *ppos)
7521da177e4SLinus Torvalds {
7532fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
7541da177e4SLinus Torvalds 	unsigned long page;
7551da177e4SLinus Torvalds 	ssize_t length;
75699f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
75799f89551SEric W. Biederman 
75899f89551SEric W. Biederman 	length = -ESRCH;
75999f89551SEric W. Biederman 	if (!task)
76099f89551SEric W. Biederman 		goto out_no_task;
7611da177e4SLinus Torvalds 
7621da177e4SLinus Torvalds 	if (count > PROC_BLOCK_SIZE)
7631da177e4SLinus Torvalds 		count = PROC_BLOCK_SIZE;
76499f89551SEric W. Biederman 
76599f89551SEric W. Biederman 	length = -ENOMEM;
766e12ba74dSMel Gorman 	if (!(page = __get_free_page(GFP_TEMPORARY)))
76799f89551SEric W. Biederman 		goto out;
7681da177e4SLinus Torvalds 
7691da177e4SLinus Torvalds 	length = PROC_I(inode)->op.proc_read(task, (char*)page);
7701da177e4SLinus Torvalds 
7711da177e4SLinus Torvalds 	if (length >= 0)
7721da177e4SLinus Torvalds 		length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
7731da177e4SLinus Torvalds 	free_page(page);
77499f89551SEric W. Biederman out:
77599f89551SEric W. Biederman 	put_task_struct(task);
77699f89551SEric W. Biederman out_no_task:
7771da177e4SLinus Torvalds 	return length;
7781da177e4SLinus Torvalds }
7791da177e4SLinus Torvalds 
78000977a59SArjan van de Ven static const struct file_operations proc_info_file_operations = {
7811da177e4SLinus Torvalds 	.read		= proc_info_read,
78287df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
7831da177e4SLinus Torvalds };
7841da177e4SLinus Torvalds 
785be614086SEric W. Biederman static int proc_single_show(struct seq_file *m, void *v)
786be614086SEric W. Biederman {
787be614086SEric W. Biederman 	struct inode *inode = m->private;
788be614086SEric W. Biederman 	struct pid_namespace *ns;
789be614086SEric W. Biederman 	struct pid *pid;
790be614086SEric W. Biederman 	struct task_struct *task;
791be614086SEric W. Biederman 	int ret;
792be614086SEric W. Biederman 
793be614086SEric W. Biederman 	ns = inode->i_sb->s_fs_info;
794be614086SEric W. Biederman 	pid = proc_pid(inode);
795be614086SEric W. Biederman 	task = get_pid_task(pid, PIDTYPE_PID);
796be614086SEric W. Biederman 	if (!task)
797be614086SEric W. Biederman 		return -ESRCH;
798be614086SEric W. Biederman 
799be614086SEric W. Biederman 	ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
800be614086SEric W. Biederman 
801be614086SEric W. Biederman 	put_task_struct(task);
802be614086SEric W. Biederman 	return ret;
803be614086SEric W. Biederman }
804be614086SEric W. Biederman 
805be614086SEric W. Biederman static int proc_single_open(struct inode *inode, struct file *filp)
806be614086SEric W. Biederman {
807c6a34058SJovi Zhang 	return single_open(filp, proc_single_show, inode);
808be614086SEric W. Biederman }
809be614086SEric W. Biederman 
810be614086SEric W. Biederman static const struct file_operations proc_single_file_operations = {
811be614086SEric W. Biederman 	.open		= proc_single_open,
812be614086SEric W. Biederman 	.read		= seq_read,
813be614086SEric W. Biederman 	.llseek		= seq_lseek,
814be614086SEric W. Biederman 	.release	= single_release,
815be614086SEric W. Biederman };
816be614086SEric W. Biederman 
8171da177e4SLinus Torvalds static int mem_open(struct inode* inode, struct file* file)
8181da177e4SLinus Torvalds {
8191da177e4SLinus Torvalds 	file->private_data = (void*)((long)current->self_exec_id);
8204a3956c7SKAMEZAWA Hiroyuki 	/* OK to pass negative loff_t, we can catch out-of-range */
8214a3956c7SKAMEZAWA Hiroyuki 	file->f_mode |= FMODE_UNSIGNED_OFFSET;
8221da177e4SLinus Torvalds 	return 0;
8231da177e4SLinus Torvalds }
8241da177e4SLinus Torvalds 
8251da177e4SLinus Torvalds static ssize_t mem_read(struct file * file, char __user * buf,
8261da177e4SLinus Torvalds 			size_t count, loff_t *ppos)
8271da177e4SLinus Torvalds {
8282fddfeefSJosef "Jeff" Sipek 	struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
8291da177e4SLinus Torvalds 	char *page;
8301da177e4SLinus Torvalds 	unsigned long src = *ppos;
8311da177e4SLinus Torvalds 	int ret = -ESRCH;
8321da177e4SLinus Torvalds 	struct mm_struct *mm;
8331da177e4SLinus Torvalds 
83499f89551SEric W. Biederman 	if (!task)
83599f89551SEric W. Biederman 		goto out_no_task;
83699f89551SEric W. Biederman 
8371da177e4SLinus Torvalds 	ret = -ENOMEM;
838e12ba74dSMel Gorman 	page = (char *)__get_free_page(GFP_TEMPORARY);
8391da177e4SLinus Torvalds 	if (!page)
8401da177e4SLinus Torvalds 		goto out;
8411da177e4SLinus Torvalds 
8428b0db9dbSStephen Wilson 	mm = check_mem_permission(task);
8438b0db9dbSStephen Wilson 	ret = PTR_ERR(mm);
8448b0db9dbSStephen Wilson 	if (IS_ERR(mm))
8451da177e4SLinus Torvalds 		goto out_free;
8461da177e4SLinus Torvalds 
8471da177e4SLinus Torvalds 	ret = -EIO;
8481da177e4SLinus Torvalds 
8491da177e4SLinus Torvalds 	if (file->private_data != (void*)((long)current->self_exec_id))
8501da177e4SLinus Torvalds 		goto out_put;
8511da177e4SLinus Torvalds 
8521da177e4SLinus Torvalds 	ret = 0;
8531da177e4SLinus Torvalds 
8541da177e4SLinus Torvalds 	while (count > 0) {
8551da177e4SLinus Torvalds 		int this_len, retval;
8561da177e4SLinus Torvalds 
8571da177e4SLinus Torvalds 		this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
8588b0db9dbSStephen Wilson 		retval = access_remote_vm(mm, src, page, this_len, 0);
8598b0db9dbSStephen Wilson 		if (!retval) {
8601da177e4SLinus Torvalds 			if (!ret)
8611da177e4SLinus Torvalds 				ret = -EIO;
8621da177e4SLinus Torvalds 			break;
8631da177e4SLinus Torvalds 		}
8641da177e4SLinus Torvalds 
8651da177e4SLinus Torvalds 		if (copy_to_user(buf, page, retval)) {
8661da177e4SLinus Torvalds 			ret = -EFAULT;
8671da177e4SLinus Torvalds 			break;
8681da177e4SLinus Torvalds 		}
8691da177e4SLinus Torvalds 
8701da177e4SLinus Torvalds 		ret += retval;
8711da177e4SLinus Torvalds 		src += retval;
8721da177e4SLinus Torvalds 		buf += retval;
8731da177e4SLinus Torvalds 		count -= retval;
8741da177e4SLinus Torvalds 	}
8751da177e4SLinus Torvalds 	*ppos = src;
8761da177e4SLinus Torvalds 
8771da177e4SLinus Torvalds out_put:
8781da177e4SLinus Torvalds 	mmput(mm);
8791da177e4SLinus Torvalds out_free:
8801da177e4SLinus Torvalds 	free_page((unsigned long) page);
8811da177e4SLinus Torvalds out:
88299f89551SEric W. Biederman 	put_task_struct(task);
88399f89551SEric W. Biederman out_no_task:
8841da177e4SLinus Torvalds 	return ret;
8851da177e4SLinus Torvalds }
8861da177e4SLinus Torvalds 
88763967fa9SGlauber de Oliveira Costa static ssize_t mem_write(struct file * file, const char __user *buf,
8881da177e4SLinus Torvalds 			 size_t count, loff_t *ppos)
8891da177e4SLinus Torvalds {
890f7ca54f4SFrederik Deweerdt 	int copied;
8911da177e4SLinus Torvalds 	char *page;
8922fddfeefSJosef "Jeff" Sipek 	struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
8931da177e4SLinus Torvalds 	unsigned long dst = *ppos;
8948b0db9dbSStephen Wilson 	struct mm_struct *mm;
8951da177e4SLinus Torvalds 
89699f89551SEric W. Biederman 	copied = -ESRCH;
89799f89551SEric W. Biederman 	if (!task)
89899f89551SEric W. Biederman 		goto out_no_task;
8991da177e4SLinus Torvalds 
90099f89551SEric W. Biederman 	copied = -ENOMEM;
901e12ba74dSMel Gorman 	page = (char *)__get_free_page(GFP_TEMPORARY);
9021da177e4SLinus Torvalds 	if (!page)
90330cd8903SKOSAKI Motohiro 		goto out_task;
90430cd8903SKOSAKI Motohiro 
90530cd8903SKOSAKI Motohiro 	mm = check_mem_permission(task);
90630cd8903SKOSAKI Motohiro 	copied = PTR_ERR(mm);
90730cd8903SKOSAKI Motohiro 	if (IS_ERR(mm))
90830cd8903SKOSAKI Motohiro 		goto out_free;
90930cd8903SKOSAKI Motohiro 
91030cd8903SKOSAKI Motohiro 	copied = -EIO;
91130cd8903SKOSAKI Motohiro 	if (file->private_data != (void *)((long)current->self_exec_id))
9128b0db9dbSStephen Wilson 		goto out_mm;
9131da177e4SLinus Torvalds 
914f7ca54f4SFrederik Deweerdt 	copied = 0;
9151da177e4SLinus Torvalds 	while (count > 0) {
9161da177e4SLinus Torvalds 		int this_len, retval;
9171da177e4SLinus Torvalds 
9181da177e4SLinus Torvalds 		this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
9191da177e4SLinus Torvalds 		if (copy_from_user(page, buf, this_len)) {
9201da177e4SLinus Torvalds 			copied = -EFAULT;
9211da177e4SLinus Torvalds 			break;
9221da177e4SLinus Torvalds 		}
9238b0db9dbSStephen Wilson 		retval = access_remote_vm(mm, dst, page, this_len, 1);
9241da177e4SLinus Torvalds 		if (!retval) {
9251da177e4SLinus Torvalds 			if (!copied)
9261da177e4SLinus Torvalds 				copied = -EIO;
9271da177e4SLinus Torvalds 			break;
9281da177e4SLinus Torvalds 		}
9291da177e4SLinus Torvalds 		copied += retval;
9301da177e4SLinus Torvalds 		buf += retval;
9311da177e4SLinus Torvalds 		dst += retval;
9321da177e4SLinus Torvalds 		count -= retval;
9331da177e4SLinus Torvalds 	}
9341da177e4SLinus Torvalds 	*ppos = dst;
93530cd8903SKOSAKI Motohiro 
9368b0db9dbSStephen Wilson out_mm:
9378b0db9dbSStephen Wilson 	mmput(mm);
93830cd8903SKOSAKI Motohiro out_free:
93930cd8903SKOSAKI Motohiro 	free_page((unsigned long) page);
9408b0db9dbSStephen Wilson out_task:
94199f89551SEric W. Biederman 	put_task_struct(task);
94299f89551SEric W. Biederman out_no_task:
9431da177e4SLinus Torvalds 	return copied;
9441da177e4SLinus Torvalds }
9451da177e4SLinus Torvalds 
94685863e47SMatt Mackall loff_t mem_lseek(struct file *file, loff_t offset, int orig)
9471da177e4SLinus Torvalds {
9481da177e4SLinus Torvalds 	switch (orig) {
9491da177e4SLinus Torvalds 	case 0:
9501da177e4SLinus Torvalds 		file->f_pos = offset;
9511da177e4SLinus Torvalds 		break;
9521da177e4SLinus Torvalds 	case 1:
9531da177e4SLinus Torvalds 		file->f_pos += offset;
9541da177e4SLinus Torvalds 		break;
9551da177e4SLinus Torvalds 	default:
9561da177e4SLinus Torvalds 		return -EINVAL;
9571da177e4SLinus Torvalds 	}
9581da177e4SLinus Torvalds 	force_successful_syscall_return();
9591da177e4SLinus Torvalds 	return file->f_pos;
9601da177e4SLinus Torvalds }
9611da177e4SLinus Torvalds 
96200977a59SArjan van de Ven static const struct file_operations proc_mem_operations = {
9631da177e4SLinus Torvalds 	.llseek		= mem_lseek,
9641da177e4SLinus Torvalds 	.read		= mem_read,
9651da177e4SLinus Torvalds 	.write		= mem_write,
9661da177e4SLinus Torvalds 	.open		= mem_open,
9671da177e4SLinus Torvalds };
9681da177e4SLinus Torvalds 
969315e28c8SJames Pearson static ssize_t environ_read(struct file *file, char __user *buf,
970315e28c8SJames Pearson 			size_t count, loff_t *ppos)
971315e28c8SJames Pearson {
972315e28c8SJames Pearson 	struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
973315e28c8SJames Pearson 	char *page;
974315e28c8SJames Pearson 	unsigned long src = *ppos;
975315e28c8SJames Pearson 	int ret = -ESRCH;
976315e28c8SJames Pearson 	struct mm_struct *mm;
977315e28c8SJames Pearson 
978315e28c8SJames Pearson 	if (!task)
979315e28c8SJames Pearson 		goto out_no_task;
980315e28c8SJames Pearson 
981315e28c8SJames Pearson 	ret = -ENOMEM;
982315e28c8SJames Pearson 	page = (char *)__get_free_page(GFP_TEMPORARY);
983315e28c8SJames Pearson 	if (!page)
984315e28c8SJames Pearson 		goto out;
985315e28c8SJames Pearson 
986315e28c8SJames Pearson 
987d6f64b89SAl Viro 	mm = mm_for_maps(task);
988d6f64b89SAl Viro 	ret = PTR_ERR(mm);
989d6f64b89SAl Viro 	if (!mm || IS_ERR(mm))
990315e28c8SJames Pearson 		goto out_free;
991315e28c8SJames Pearson 
992d6f64b89SAl Viro 	ret = 0;
993315e28c8SJames Pearson 	while (count > 0) {
994315e28c8SJames Pearson 		int this_len, retval, max_len;
995315e28c8SJames Pearson 
996315e28c8SJames Pearson 		this_len = mm->env_end - (mm->env_start + src);
997315e28c8SJames Pearson 
998315e28c8SJames Pearson 		if (this_len <= 0)
999315e28c8SJames Pearson 			break;
1000315e28c8SJames Pearson 
1001315e28c8SJames Pearson 		max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
1002315e28c8SJames Pearson 		this_len = (this_len > max_len) ? max_len : this_len;
1003315e28c8SJames Pearson 
1004315e28c8SJames Pearson 		retval = access_process_vm(task, (mm->env_start + src),
1005315e28c8SJames Pearson 			page, this_len, 0);
1006315e28c8SJames Pearson 
1007315e28c8SJames Pearson 		if (retval <= 0) {
1008315e28c8SJames Pearson 			ret = retval;
1009315e28c8SJames Pearson 			break;
1010315e28c8SJames Pearson 		}
1011315e28c8SJames Pearson 
1012315e28c8SJames Pearson 		if (copy_to_user(buf, page, retval)) {
1013315e28c8SJames Pearson 			ret = -EFAULT;
1014315e28c8SJames Pearson 			break;
1015315e28c8SJames Pearson 		}
1016315e28c8SJames Pearson 
1017315e28c8SJames Pearson 		ret += retval;
1018315e28c8SJames Pearson 		src += retval;
1019315e28c8SJames Pearson 		buf += retval;
1020315e28c8SJames Pearson 		count -= retval;
1021315e28c8SJames Pearson 	}
1022315e28c8SJames Pearson 	*ppos = src;
1023315e28c8SJames Pearson 
1024315e28c8SJames Pearson 	mmput(mm);
1025315e28c8SJames Pearson out_free:
1026315e28c8SJames Pearson 	free_page((unsigned long) page);
1027315e28c8SJames Pearson out:
1028315e28c8SJames Pearson 	put_task_struct(task);
1029315e28c8SJames Pearson out_no_task:
1030315e28c8SJames Pearson 	return ret;
1031315e28c8SJames Pearson }
1032315e28c8SJames Pearson 
1033315e28c8SJames Pearson static const struct file_operations proc_environ_operations = {
1034315e28c8SJames Pearson 	.read		= environ_read,
103587df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
1036315e28c8SJames Pearson };
1037315e28c8SJames Pearson 
10381da177e4SLinus Torvalds static ssize_t oom_adjust_read(struct file *file, char __user *buf,
10391da177e4SLinus Torvalds 				size_t count, loff_t *ppos)
10401da177e4SLinus Torvalds {
10412fddfeefSJosef "Jeff" Sipek 	struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
10428578cea7SEric W. Biederman 	char buffer[PROC_NUMBUF];
10431da177e4SLinus Torvalds 	size_t len;
104428b83c51SKOSAKI Motohiro 	int oom_adjust = OOM_DISABLE;
104528b83c51SKOSAKI Motohiro 	unsigned long flags;
10461da177e4SLinus Torvalds 
104799f89551SEric W. Biederman 	if (!task)
104899f89551SEric W. Biederman 		return -ESRCH;
104928b83c51SKOSAKI Motohiro 
105028b83c51SKOSAKI Motohiro 	if (lock_task_sighand(task, &flags)) {
105128b83c51SKOSAKI Motohiro 		oom_adjust = task->signal->oom_adj;
105228b83c51SKOSAKI Motohiro 		unlock_task_sighand(task, &flags);
105328b83c51SKOSAKI Motohiro 	}
105428b83c51SKOSAKI Motohiro 
105599f89551SEric W. Biederman 	put_task_struct(task);
105699f89551SEric W. Biederman 
10578578cea7SEric W. Biederman 	len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
10580c28f287SAkinobu Mita 
10590c28f287SAkinobu Mita 	return simple_read_from_buffer(buf, count, ppos, buffer, len);
10601da177e4SLinus Torvalds }
10611da177e4SLinus Torvalds 
10621da177e4SLinus Torvalds static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
10631da177e4SLinus Torvalds 				size_t count, loff_t *ppos)
10641da177e4SLinus Torvalds {
106599f89551SEric W. Biederman 	struct task_struct *task;
10665d863b89SKOSAKI Motohiro 	char buffer[PROC_NUMBUF];
10670a8cb8e3SAlexey Dobriyan 	int oom_adjust;
106828b83c51SKOSAKI Motohiro 	unsigned long flags;
10695d863b89SKOSAKI Motohiro 	int err;
10701da177e4SLinus Torvalds 
10718578cea7SEric W. Biederman 	memset(buffer, 0, sizeof(buffer));
10728578cea7SEric W. Biederman 	if (count > sizeof(buffer) - 1)
10738578cea7SEric W. Biederman 		count = sizeof(buffer) - 1;
1074723548bfSDavid Rientjes 	if (copy_from_user(buffer, buf, count)) {
1075723548bfSDavid Rientjes 		err = -EFAULT;
1076723548bfSDavid Rientjes 		goto out;
1077723548bfSDavid Rientjes 	}
10785d863b89SKOSAKI Motohiro 
10790a8cb8e3SAlexey Dobriyan 	err = kstrtoint(strstrip(buffer), 0, &oom_adjust);
10805d863b89SKOSAKI Motohiro 	if (err)
1081723548bfSDavid Rientjes 		goto out;
10828ac773b4SAlexey Dobriyan 	if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
1083723548bfSDavid Rientjes 	     oom_adjust != OOM_DISABLE) {
1084723548bfSDavid Rientjes 		err = -EINVAL;
1085723548bfSDavid Rientjes 		goto out;
1086723548bfSDavid Rientjes 	}
10875d863b89SKOSAKI Motohiro 
10882fddfeefSJosef "Jeff" Sipek 	task = get_proc_task(file->f_path.dentry->d_inode);
1089723548bfSDavid Rientjes 	if (!task) {
1090723548bfSDavid Rientjes 		err = -ESRCH;
1091723548bfSDavid Rientjes 		goto out;
1092723548bfSDavid Rientjes 	}
109328b83c51SKOSAKI Motohiro 
10943d5992d2SYing Han 	task_lock(task);
10953d5992d2SYing Han 	if (!task->mm) {
1096723548bfSDavid Rientjes 		err = -EINVAL;
1097723548bfSDavid Rientjes 		goto err_task_lock;
10983d5992d2SYing Han 	}
10993d5992d2SYing Han 
1100d19d5476SDavid Rientjes 	if (!lock_task_sighand(task, &flags)) {
1101d19d5476SDavid Rientjes 		err = -ESRCH;
1102d19d5476SDavid Rientjes 		goto err_task_lock;
1103d19d5476SDavid Rientjes 	}
1104d19d5476SDavid Rientjes 
1105d19d5476SDavid Rientjes 	if (oom_adjust < task->signal->oom_adj && !capable(CAP_SYS_RESOURCE)) {
1106d19d5476SDavid Rientjes 		err = -EACCES;
1107d19d5476SDavid Rientjes 		goto err_sighand;
1108d19d5476SDavid Rientjes 	}
1109d19d5476SDavid Rientjes 
111051b1bd2aSDavid Rientjes 	/*
111151b1bd2aSDavid Rientjes 	 * Warn that /proc/pid/oom_adj is deprecated, see
111251b1bd2aSDavid Rientjes 	 * Documentation/feature-removal-schedule.txt.
111351b1bd2aSDavid Rientjes 	 */
1114c2142704SLinus Torvalds 	printk_once(KERN_WARNING "%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
1115be8f684dSDavid Rientjes 		  current->comm, task_pid_nr(current), task_pid_nr(task),
1116be8f684dSDavid Rientjes 		  task_pid_nr(task));
111728b83c51SKOSAKI Motohiro 	task->signal->oom_adj = oom_adjust;
1118a63d83f4SDavid Rientjes 	/*
1119a63d83f4SDavid Rientjes 	 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1120a63d83f4SDavid Rientjes 	 * value is always attainable.
1121a63d83f4SDavid Rientjes 	 */
1122a63d83f4SDavid Rientjes 	if (task->signal->oom_adj == OOM_ADJUST_MAX)
1123a63d83f4SDavid Rientjes 		task->signal->oom_score_adj = OOM_SCORE_ADJ_MAX;
1124a63d83f4SDavid Rientjes 	else
1125a63d83f4SDavid Rientjes 		task->signal->oom_score_adj = (oom_adjust * OOM_SCORE_ADJ_MAX) /
1126a63d83f4SDavid Rientjes 								-OOM_DISABLE;
1127723548bfSDavid Rientjes err_sighand:
112828b83c51SKOSAKI Motohiro 	unlock_task_sighand(task, &flags);
1129d19d5476SDavid Rientjes err_task_lock:
1130d19d5476SDavid Rientjes 	task_unlock(task);
113199f89551SEric W. Biederman 	put_task_struct(task);
1132723548bfSDavid Rientjes out:
1133723548bfSDavid Rientjes 	return err < 0 ? err : count;
11341da177e4SLinus Torvalds }
11351da177e4SLinus Torvalds 
113600977a59SArjan van de Ven static const struct file_operations proc_oom_adjust_operations = {
11371da177e4SLinus Torvalds 	.read		= oom_adjust_read,
11381da177e4SLinus Torvalds 	.write		= oom_adjust_write,
113987df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
11401da177e4SLinus Torvalds };
11411da177e4SLinus Torvalds 
1142a63d83f4SDavid Rientjes static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1143a63d83f4SDavid Rientjes 					size_t count, loff_t *ppos)
1144a63d83f4SDavid Rientjes {
1145a63d83f4SDavid Rientjes 	struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
1146a63d83f4SDavid Rientjes 	char buffer[PROC_NUMBUF];
1147a63d83f4SDavid Rientjes 	int oom_score_adj = OOM_SCORE_ADJ_MIN;
1148a63d83f4SDavid Rientjes 	unsigned long flags;
1149a63d83f4SDavid Rientjes 	size_t len;
1150a63d83f4SDavid Rientjes 
1151a63d83f4SDavid Rientjes 	if (!task)
1152a63d83f4SDavid Rientjes 		return -ESRCH;
1153a63d83f4SDavid Rientjes 	if (lock_task_sighand(task, &flags)) {
1154a63d83f4SDavid Rientjes 		oom_score_adj = task->signal->oom_score_adj;
1155a63d83f4SDavid Rientjes 		unlock_task_sighand(task, &flags);
1156a63d83f4SDavid Rientjes 	}
1157a63d83f4SDavid Rientjes 	put_task_struct(task);
1158a63d83f4SDavid Rientjes 	len = snprintf(buffer, sizeof(buffer), "%d\n", oom_score_adj);
1159a63d83f4SDavid Rientjes 	return simple_read_from_buffer(buf, count, ppos, buffer, len);
1160a63d83f4SDavid Rientjes }
1161a63d83f4SDavid Rientjes 
1162a63d83f4SDavid Rientjes static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1163a63d83f4SDavid Rientjes 					size_t count, loff_t *ppos)
1164a63d83f4SDavid Rientjes {
1165a63d83f4SDavid Rientjes 	struct task_struct *task;
1166a63d83f4SDavid Rientjes 	char buffer[PROC_NUMBUF];
1167a63d83f4SDavid Rientjes 	unsigned long flags;
11680a8cb8e3SAlexey Dobriyan 	int oom_score_adj;
1169a63d83f4SDavid Rientjes 	int err;
1170a63d83f4SDavid Rientjes 
1171a63d83f4SDavid Rientjes 	memset(buffer, 0, sizeof(buffer));
1172a63d83f4SDavid Rientjes 	if (count > sizeof(buffer) - 1)
1173a63d83f4SDavid Rientjes 		count = sizeof(buffer) - 1;
1174723548bfSDavid Rientjes 	if (copy_from_user(buffer, buf, count)) {
1175723548bfSDavid Rientjes 		err = -EFAULT;
1176723548bfSDavid Rientjes 		goto out;
1177723548bfSDavid Rientjes 	}
1178a63d83f4SDavid Rientjes 
11790a8cb8e3SAlexey Dobriyan 	err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
1180a63d83f4SDavid Rientjes 	if (err)
1181723548bfSDavid Rientjes 		goto out;
1182a63d83f4SDavid Rientjes 	if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
1183723548bfSDavid Rientjes 			oom_score_adj > OOM_SCORE_ADJ_MAX) {
1184723548bfSDavid Rientjes 		err = -EINVAL;
1185723548bfSDavid Rientjes 		goto out;
1186723548bfSDavid Rientjes 	}
1187a63d83f4SDavid Rientjes 
1188a63d83f4SDavid Rientjes 	task = get_proc_task(file->f_path.dentry->d_inode);
1189723548bfSDavid Rientjes 	if (!task) {
1190723548bfSDavid Rientjes 		err = -ESRCH;
1191723548bfSDavid Rientjes 		goto out;
1192723548bfSDavid Rientjes 	}
1193a63d83f4SDavid Rientjes 
11943d5992d2SYing Han 	task_lock(task);
11953d5992d2SYing Han 	if (!task->mm) {
1196723548bfSDavid Rientjes 		err = -EINVAL;
1197723548bfSDavid Rientjes 		goto err_task_lock;
11983d5992d2SYing Han 	}
1199d19d5476SDavid Rientjes 
1200d19d5476SDavid Rientjes 	if (!lock_task_sighand(task, &flags)) {
1201d19d5476SDavid Rientjes 		err = -ESRCH;
1202d19d5476SDavid Rientjes 		goto err_task_lock;
1203d19d5476SDavid Rientjes 	}
1204d19d5476SDavid Rientjes 
1205dabb16f6SMandeep Singh Baines 	if (oom_score_adj < task->signal->oom_score_adj_min &&
1206d19d5476SDavid Rientjes 			!capable(CAP_SYS_RESOURCE)) {
1207d19d5476SDavid Rientjes 		err = -EACCES;
1208d19d5476SDavid Rientjes 		goto err_sighand;
1209d19d5476SDavid Rientjes 	}
1210d19d5476SDavid Rientjes 
1211a63d83f4SDavid Rientjes 	task->signal->oom_score_adj = oom_score_adj;
1212dabb16f6SMandeep Singh Baines 	if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
1213dabb16f6SMandeep Singh Baines 		task->signal->oom_score_adj_min = oom_score_adj;
1214a63d83f4SDavid Rientjes 	/*
1215a63d83f4SDavid Rientjes 	 * Scale /proc/pid/oom_adj appropriately ensuring that OOM_DISABLE is
1216a63d83f4SDavid Rientjes 	 * always attainable.
1217a63d83f4SDavid Rientjes 	 */
1218a63d83f4SDavid Rientjes 	if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MIN)
1219a63d83f4SDavid Rientjes 		task->signal->oom_adj = OOM_DISABLE;
1220a63d83f4SDavid Rientjes 	else
1221a63d83f4SDavid Rientjes 		task->signal->oom_adj = (oom_score_adj * OOM_ADJUST_MAX) /
1222a63d83f4SDavid Rientjes 							OOM_SCORE_ADJ_MAX;
1223723548bfSDavid Rientjes err_sighand:
1224a63d83f4SDavid Rientjes 	unlock_task_sighand(task, &flags);
1225d19d5476SDavid Rientjes err_task_lock:
1226d19d5476SDavid Rientjes 	task_unlock(task);
1227a63d83f4SDavid Rientjes 	put_task_struct(task);
1228723548bfSDavid Rientjes out:
1229723548bfSDavid Rientjes 	return err < 0 ? err : count;
1230a63d83f4SDavid Rientjes }
1231a63d83f4SDavid Rientjes 
1232a63d83f4SDavid Rientjes static const struct file_operations proc_oom_score_adj_operations = {
1233a63d83f4SDavid Rientjes 	.read		= oom_score_adj_read,
1234a63d83f4SDavid Rientjes 	.write		= oom_score_adj_write,
12356038f373SArnd Bergmann 	.llseek		= default_llseek,
1236a63d83f4SDavid Rientjes };
1237a63d83f4SDavid Rientjes 
12381da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL
12391da177e4SLinus Torvalds #define TMPBUFLEN 21
12401da177e4SLinus Torvalds static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
12411da177e4SLinus Torvalds 				  size_t count, loff_t *ppos)
12421da177e4SLinus Torvalds {
12432fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
124499f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
12451da177e4SLinus Torvalds 	ssize_t length;
12461da177e4SLinus Torvalds 	char tmpbuf[TMPBUFLEN];
12471da177e4SLinus Torvalds 
124899f89551SEric W. Biederman 	if (!task)
124999f89551SEric W. Biederman 		return -ESRCH;
12501da177e4SLinus Torvalds 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
12510c11b942SAl Viro 				audit_get_loginuid(task));
125299f89551SEric W. Biederman 	put_task_struct(task);
12531da177e4SLinus Torvalds 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
12541da177e4SLinus Torvalds }
12551da177e4SLinus Torvalds 
12561da177e4SLinus Torvalds static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
12571da177e4SLinus Torvalds 				   size_t count, loff_t *ppos)
12581da177e4SLinus Torvalds {
12592fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
12601da177e4SLinus Torvalds 	char *page, *tmp;
12611da177e4SLinus Torvalds 	ssize_t length;
12621da177e4SLinus Torvalds 	uid_t loginuid;
12631da177e4SLinus Torvalds 
12641da177e4SLinus Torvalds 	if (!capable(CAP_AUDIT_CONTROL))
12651da177e4SLinus Torvalds 		return -EPERM;
12661da177e4SLinus Torvalds 
12677dc52157SPaul E. McKenney 	rcu_read_lock();
12687dc52157SPaul E. McKenney 	if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
12697dc52157SPaul E. McKenney 		rcu_read_unlock();
12701da177e4SLinus Torvalds 		return -EPERM;
12717dc52157SPaul E. McKenney 	}
12727dc52157SPaul E. McKenney 	rcu_read_unlock();
12731da177e4SLinus Torvalds 
1274e0182909SAl Viro 	if (count >= PAGE_SIZE)
1275e0182909SAl Viro 		count = PAGE_SIZE - 1;
12761da177e4SLinus Torvalds 
12771da177e4SLinus Torvalds 	if (*ppos != 0) {
12781da177e4SLinus Torvalds 		/* No partial writes. */
12791da177e4SLinus Torvalds 		return -EINVAL;
12801da177e4SLinus Torvalds 	}
1281e12ba74dSMel Gorman 	page = (char*)__get_free_page(GFP_TEMPORARY);
12821da177e4SLinus Torvalds 	if (!page)
12831da177e4SLinus Torvalds 		return -ENOMEM;
12841da177e4SLinus Torvalds 	length = -EFAULT;
12851da177e4SLinus Torvalds 	if (copy_from_user(page, buf, count))
12861da177e4SLinus Torvalds 		goto out_free_page;
12871da177e4SLinus Torvalds 
1288e0182909SAl Viro 	page[count] = '\0';
12891da177e4SLinus Torvalds 	loginuid = simple_strtoul(page, &tmp, 10);
12901da177e4SLinus Torvalds 	if (tmp == page) {
12911da177e4SLinus Torvalds 		length = -EINVAL;
12921da177e4SLinus Torvalds 		goto out_free_page;
12931da177e4SLinus Torvalds 
12941da177e4SLinus Torvalds 	}
129599f89551SEric W. Biederman 	length = audit_set_loginuid(current, loginuid);
12961da177e4SLinus Torvalds 	if (likely(length == 0))
12971da177e4SLinus Torvalds 		length = count;
12981da177e4SLinus Torvalds 
12991da177e4SLinus Torvalds out_free_page:
13001da177e4SLinus Torvalds 	free_page((unsigned long) page);
13011da177e4SLinus Torvalds 	return length;
13021da177e4SLinus Torvalds }
13031da177e4SLinus Torvalds 
130400977a59SArjan van de Ven static const struct file_operations proc_loginuid_operations = {
13051da177e4SLinus Torvalds 	.read		= proc_loginuid_read,
13061da177e4SLinus Torvalds 	.write		= proc_loginuid_write,
130787df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
13081da177e4SLinus Torvalds };
13091e0bd755SEric Paris 
13101e0bd755SEric Paris static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
13111e0bd755SEric Paris 				  size_t count, loff_t *ppos)
13121e0bd755SEric Paris {
13131e0bd755SEric Paris 	struct inode * inode = file->f_path.dentry->d_inode;
13141e0bd755SEric Paris 	struct task_struct *task = get_proc_task(inode);
13151e0bd755SEric Paris 	ssize_t length;
13161e0bd755SEric Paris 	char tmpbuf[TMPBUFLEN];
13171e0bd755SEric Paris 
13181e0bd755SEric Paris 	if (!task)
13191e0bd755SEric Paris 		return -ESRCH;
13201e0bd755SEric Paris 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
13211e0bd755SEric Paris 				audit_get_sessionid(task));
13221e0bd755SEric Paris 	put_task_struct(task);
13231e0bd755SEric Paris 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
13241e0bd755SEric Paris }
13251e0bd755SEric Paris 
13261e0bd755SEric Paris static const struct file_operations proc_sessionid_operations = {
13271e0bd755SEric Paris 	.read		= proc_sessionid_read,
132887df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
13291e0bd755SEric Paris };
13301da177e4SLinus Torvalds #endif
13311da177e4SLinus Torvalds 
1332f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
1333f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1334f4f154fdSAkinobu Mita 				      size_t count, loff_t *ppos)
1335f4f154fdSAkinobu Mita {
1336f4f154fdSAkinobu Mita 	struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1337f4f154fdSAkinobu Mita 	char buffer[PROC_NUMBUF];
1338f4f154fdSAkinobu Mita 	size_t len;
1339f4f154fdSAkinobu Mita 	int make_it_fail;
1340f4f154fdSAkinobu Mita 
1341f4f154fdSAkinobu Mita 	if (!task)
1342f4f154fdSAkinobu Mita 		return -ESRCH;
1343f4f154fdSAkinobu Mita 	make_it_fail = task->make_it_fail;
1344f4f154fdSAkinobu Mita 	put_task_struct(task);
1345f4f154fdSAkinobu Mita 
1346f4f154fdSAkinobu Mita 	len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
13470c28f287SAkinobu Mita 
13480c28f287SAkinobu Mita 	return simple_read_from_buffer(buf, count, ppos, buffer, len);
1349f4f154fdSAkinobu Mita }
1350f4f154fdSAkinobu Mita 
1351f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_write(struct file * file,
1352f4f154fdSAkinobu Mita 			const char __user * buf, size_t count, loff_t *ppos)
1353f4f154fdSAkinobu Mita {
1354f4f154fdSAkinobu Mita 	struct task_struct *task;
1355f4f154fdSAkinobu Mita 	char buffer[PROC_NUMBUF], *end;
1356f4f154fdSAkinobu Mita 	int make_it_fail;
1357f4f154fdSAkinobu Mita 
1358f4f154fdSAkinobu Mita 	if (!capable(CAP_SYS_RESOURCE))
1359f4f154fdSAkinobu Mita 		return -EPERM;
1360f4f154fdSAkinobu Mita 	memset(buffer, 0, sizeof(buffer));
1361f4f154fdSAkinobu Mita 	if (count > sizeof(buffer) - 1)
1362f4f154fdSAkinobu Mita 		count = sizeof(buffer) - 1;
1363f4f154fdSAkinobu Mita 	if (copy_from_user(buffer, buf, count))
1364f4f154fdSAkinobu Mita 		return -EFAULT;
1365cba8aafeSVincent Li 	make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1366cba8aafeSVincent Li 	if (*end)
1367cba8aafeSVincent Li 		return -EINVAL;
1368f4f154fdSAkinobu Mita 	task = get_proc_task(file->f_dentry->d_inode);
1369f4f154fdSAkinobu Mita 	if (!task)
1370f4f154fdSAkinobu Mita 		return -ESRCH;
1371f4f154fdSAkinobu Mita 	task->make_it_fail = make_it_fail;
1372f4f154fdSAkinobu Mita 	put_task_struct(task);
1373cba8aafeSVincent Li 
1374cba8aafeSVincent Li 	return count;
1375f4f154fdSAkinobu Mita }
1376f4f154fdSAkinobu Mita 
137700977a59SArjan van de Ven static const struct file_operations proc_fault_inject_operations = {
1378f4f154fdSAkinobu Mita 	.read		= proc_fault_inject_read,
1379f4f154fdSAkinobu Mita 	.write		= proc_fault_inject_write,
138087df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
1381f4f154fdSAkinobu Mita };
1382f4f154fdSAkinobu Mita #endif
1383f4f154fdSAkinobu Mita 
13849745512cSArjan van de Ven 
138543ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
138643ae34cbSIngo Molnar /*
138743ae34cbSIngo Molnar  * Print out various scheduling related per-task fields:
138843ae34cbSIngo Molnar  */
138943ae34cbSIngo Molnar static int sched_show(struct seq_file *m, void *v)
139043ae34cbSIngo Molnar {
139143ae34cbSIngo Molnar 	struct inode *inode = m->private;
139243ae34cbSIngo Molnar 	struct task_struct *p;
139343ae34cbSIngo Molnar 
139443ae34cbSIngo Molnar 	p = get_proc_task(inode);
139543ae34cbSIngo Molnar 	if (!p)
139643ae34cbSIngo Molnar 		return -ESRCH;
139743ae34cbSIngo Molnar 	proc_sched_show_task(p, m);
139843ae34cbSIngo Molnar 
139943ae34cbSIngo Molnar 	put_task_struct(p);
140043ae34cbSIngo Molnar 
140143ae34cbSIngo Molnar 	return 0;
140243ae34cbSIngo Molnar }
140343ae34cbSIngo Molnar 
140443ae34cbSIngo Molnar static ssize_t
140543ae34cbSIngo Molnar sched_write(struct file *file, const char __user *buf,
140643ae34cbSIngo Molnar 	    size_t count, loff_t *offset)
140743ae34cbSIngo Molnar {
140843ae34cbSIngo Molnar 	struct inode *inode = file->f_path.dentry->d_inode;
140943ae34cbSIngo Molnar 	struct task_struct *p;
141043ae34cbSIngo Molnar 
141143ae34cbSIngo Molnar 	p = get_proc_task(inode);
141243ae34cbSIngo Molnar 	if (!p)
141343ae34cbSIngo Molnar 		return -ESRCH;
141443ae34cbSIngo Molnar 	proc_sched_set_task(p);
141543ae34cbSIngo Molnar 
141643ae34cbSIngo Molnar 	put_task_struct(p);
141743ae34cbSIngo Molnar 
141843ae34cbSIngo Molnar 	return count;
141943ae34cbSIngo Molnar }
142043ae34cbSIngo Molnar 
142143ae34cbSIngo Molnar static int sched_open(struct inode *inode, struct file *filp)
142243ae34cbSIngo Molnar {
1423c6a34058SJovi Zhang 	return single_open(filp, sched_show, inode);
142443ae34cbSIngo Molnar }
142543ae34cbSIngo Molnar 
142643ae34cbSIngo Molnar static const struct file_operations proc_pid_sched_operations = {
142743ae34cbSIngo Molnar 	.open		= sched_open,
142843ae34cbSIngo Molnar 	.read		= seq_read,
142943ae34cbSIngo Molnar 	.write		= sched_write,
143043ae34cbSIngo Molnar 	.llseek		= seq_lseek,
14315ea473a1SAlexey Dobriyan 	.release	= single_release,
143243ae34cbSIngo Molnar };
143343ae34cbSIngo Molnar 
143443ae34cbSIngo Molnar #endif
143543ae34cbSIngo Molnar 
14365091faa4SMike Galbraith #ifdef CONFIG_SCHED_AUTOGROUP
14375091faa4SMike Galbraith /*
14385091faa4SMike Galbraith  * Print out autogroup related information:
14395091faa4SMike Galbraith  */
14405091faa4SMike Galbraith static int sched_autogroup_show(struct seq_file *m, void *v)
14415091faa4SMike Galbraith {
14425091faa4SMike Galbraith 	struct inode *inode = m->private;
14435091faa4SMike Galbraith 	struct task_struct *p;
14445091faa4SMike Galbraith 
14455091faa4SMike Galbraith 	p = get_proc_task(inode);
14465091faa4SMike Galbraith 	if (!p)
14475091faa4SMike Galbraith 		return -ESRCH;
14485091faa4SMike Galbraith 	proc_sched_autogroup_show_task(p, m);
14495091faa4SMike Galbraith 
14505091faa4SMike Galbraith 	put_task_struct(p);
14515091faa4SMike Galbraith 
14525091faa4SMike Galbraith 	return 0;
14535091faa4SMike Galbraith }
14545091faa4SMike Galbraith 
14555091faa4SMike Galbraith static ssize_t
14565091faa4SMike Galbraith sched_autogroup_write(struct file *file, const char __user *buf,
14575091faa4SMike Galbraith 	    size_t count, loff_t *offset)
14585091faa4SMike Galbraith {
14595091faa4SMike Galbraith 	struct inode *inode = file->f_path.dentry->d_inode;
14605091faa4SMike Galbraith 	struct task_struct *p;
14615091faa4SMike Galbraith 	char buffer[PROC_NUMBUF];
14620a8cb8e3SAlexey Dobriyan 	int nice;
14635091faa4SMike Galbraith 	int err;
14645091faa4SMike Galbraith 
14655091faa4SMike Galbraith 	memset(buffer, 0, sizeof(buffer));
14665091faa4SMike Galbraith 	if (count > sizeof(buffer) - 1)
14675091faa4SMike Galbraith 		count = sizeof(buffer) - 1;
14685091faa4SMike Galbraith 	if (copy_from_user(buffer, buf, count))
14695091faa4SMike Galbraith 		return -EFAULT;
14705091faa4SMike Galbraith 
14710a8cb8e3SAlexey Dobriyan 	err = kstrtoint(strstrip(buffer), 0, &nice);
14720a8cb8e3SAlexey Dobriyan 	if (err < 0)
14730a8cb8e3SAlexey Dobriyan 		return err;
14745091faa4SMike Galbraith 
14755091faa4SMike Galbraith 	p = get_proc_task(inode);
14765091faa4SMike Galbraith 	if (!p)
14775091faa4SMike Galbraith 		return -ESRCH;
14785091faa4SMike Galbraith 
14795091faa4SMike Galbraith 	err = nice;
14805091faa4SMike Galbraith 	err = proc_sched_autogroup_set_nice(p, &err);
14815091faa4SMike Galbraith 	if (err)
14825091faa4SMike Galbraith 		count = err;
14835091faa4SMike Galbraith 
14845091faa4SMike Galbraith 	put_task_struct(p);
14855091faa4SMike Galbraith 
14865091faa4SMike Galbraith 	return count;
14875091faa4SMike Galbraith }
14885091faa4SMike Galbraith 
14895091faa4SMike Galbraith static int sched_autogroup_open(struct inode *inode, struct file *filp)
14905091faa4SMike Galbraith {
14915091faa4SMike Galbraith 	int ret;
14925091faa4SMike Galbraith 
14935091faa4SMike Galbraith 	ret = single_open(filp, sched_autogroup_show, NULL);
14945091faa4SMike Galbraith 	if (!ret) {
14955091faa4SMike Galbraith 		struct seq_file *m = filp->private_data;
14965091faa4SMike Galbraith 
14975091faa4SMike Galbraith 		m->private = inode;
14985091faa4SMike Galbraith 	}
14995091faa4SMike Galbraith 	return ret;
15005091faa4SMike Galbraith }
15015091faa4SMike Galbraith 
15025091faa4SMike Galbraith static const struct file_operations proc_pid_sched_autogroup_operations = {
15035091faa4SMike Galbraith 	.open		= sched_autogroup_open,
15045091faa4SMike Galbraith 	.read		= seq_read,
15055091faa4SMike Galbraith 	.write		= sched_autogroup_write,
15065091faa4SMike Galbraith 	.llseek		= seq_lseek,
15075091faa4SMike Galbraith 	.release	= single_release,
15085091faa4SMike Galbraith };
15095091faa4SMike Galbraith 
15105091faa4SMike Galbraith #endif /* CONFIG_SCHED_AUTOGROUP */
15115091faa4SMike Galbraith 
15124614a696Sjohn stultz static ssize_t comm_write(struct file *file, const char __user *buf,
15134614a696Sjohn stultz 				size_t count, loff_t *offset)
15144614a696Sjohn stultz {
15154614a696Sjohn stultz 	struct inode *inode = file->f_path.dentry->d_inode;
15164614a696Sjohn stultz 	struct task_struct *p;
15174614a696Sjohn stultz 	char buffer[TASK_COMM_LEN];
15184614a696Sjohn stultz 
15194614a696Sjohn stultz 	memset(buffer, 0, sizeof(buffer));
15204614a696Sjohn stultz 	if (count > sizeof(buffer) - 1)
15214614a696Sjohn stultz 		count = sizeof(buffer) - 1;
15224614a696Sjohn stultz 	if (copy_from_user(buffer, buf, count))
15234614a696Sjohn stultz 		return -EFAULT;
15244614a696Sjohn stultz 
15254614a696Sjohn stultz 	p = get_proc_task(inode);
15264614a696Sjohn stultz 	if (!p)
15274614a696Sjohn stultz 		return -ESRCH;
15284614a696Sjohn stultz 
15294614a696Sjohn stultz 	if (same_thread_group(current, p))
15304614a696Sjohn stultz 		set_task_comm(p, buffer);
15314614a696Sjohn stultz 	else
15324614a696Sjohn stultz 		count = -EINVAL;
15334614a696Sjohn stultz 
15344614a696Sjohn stultz 	put_task_struct(p);
15354614a696Sjohn stultz 
15364614a696Sjohn stultz 	return count;
15374614a696Sjohn stultz }
15384614a696Sjohn stultz 
15394614a696Sjohn stultz static int comm_show(struct seq_file *m, void *v)
15404614a696Sjohn stultz {
15414614a696Sjohn stultz 	struct inode *inode = m->private;
15424614a696Sjohn stultz 	struct task_struct *p;
15434614a696Sjohn stultz 
15444614a696Sjohn stultz 	p = get_proc_task(inode);
15454614a696Sjohn stultz 	if (!p)
15464614a696Sjohn stultz 		return -ESRCH;
15474614a696Sjohn stultz 
15484614a696Sjohn stultz 	task_lock(p);
15494614a696Sjohn stultz 	seq_printf(m, "%s\n", p->comm);
15504614a696Sjohn stultz 	task_unlock(p);
15514614a696Sjohn stultz 
15524614a696Sjohn stultz 	put_task_struct(p);
15534614a696Sjohn stultz 
15544614a696Sjohn stultz 	return 0;
15554614a696Sjohn stultz }
15564614a696Sjohn stultz 
15574614a696Sjohn stultz static int comm_open(struct inode *inode, struct file *filp)
15584614a696Sjohn stultz {
1559c6a34058SJovi Zhang 	return single_open(filp, comm_show, inode);
15604614a696Sjohn stultz }
15614614a696Sjohn stultz 
15624614a696Sjohn stultz static const struct file_operations proc_pid_set_comm_operations = {
15634614a696Sjohn stultz 	.open		= comm_open,
15644614a696Sjohn stultz 	.read		= seq_read,
15654614a696Sjohn stultz 	.write		= comm_write,
15664614a696Sjohn stultz 	.llseek		= seq_lseek,
15674614a696Sjohn stultz 	.release	= single_release,
15684614a696Sjohn stultz };
15694614a696Sjohn stultz 
1570925d1c40SMatt Helsley static int proc_exe_link(struct inode *inode, struct path *exe_path)
1571925d1c40SMatt Helsley {
1572925d1c40SMatt Helsley 	struct task_struct *task;
1573925d1c40SMatt Helsley 	struct mm_struct *mm;
1574925d1c40SMatt Helsley 	struct file *exe_file;
1575925d1c40SMatt Helsley 
1576925d1c40SMatt Helsley 	task = get_proc_task(inode);
1577925d1c40SMatt Helsley 	if (!task)
1578925d1c40SMatt Helsley 		return -ENOENT;
1579925d1c40SMatt Helsley 	mm = get_task_mm(task);
1580925d1c40SMatt Helsley 	put_task_struct(task);
1581925d1c40SMatt Helsley 	if (!mm)
1582925d1c40SMatt Helsley 		return -ENOENT;
1583925d1c40SMatt Helsley 	exe_file = get_mm_exe_file(mm);
1584925d1c40SMatt Helsley 	mmput(mm);
1585925d1c40SMatt Helsley 	if (exe_file) {
1586925d1c40SMatt Helsley 		*exe_path = exe_file->f_path;
1587925d1c40SMatt Helsley 		path_get(&exe_file->f_path);
1588925d1c40SMatt Helsley 		fput(exe_file);
1589925d1c40SMatt Helsley 		return 0;
1590925d1c40SMatt Helsley 	} else
1591925d1c40SMatt Helsley 		return -ENOENT;
1592925d1c40SMatt Helsley }
1593925d1c40SMatt Helsley 
1594008b150aSAl Viro static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
15951da177e4SLinus Torvalds {
15961da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
15971da177e4SLinus Torvalds 	int error = -EACCES;
15981da177e4SLinus Torvalds 
15991da177e4SLinus Torvalds 	/* We don't need a base pointer in the /proc filesystem */
16001d957f9bSJan Blunck 	path_put(&nd->path);
16011da177e4SLinus Torvalds 
1602778c1144SEric W. Biederman 	/* Are we allowed to snoop on the tasks file descriptors? */
1603778c1144SEric W. Biederman 	if (!proc_fd_access_allowed(inode))
16041da177e4SLinus Torvalds 		goto out;
16051da177e4SLinus Torvalds 
16063dcd25f3SJan Blunck 	error = PROC_I(inode)->op.proc_get_link(inode, &nd->path);
16071da177e4SLinus Torvalds out:
1608008b150aSAl Viro 	return ERR_PTR(error);
16091da177e4SLinus Torvalds }
16101da177e4SLinus Torvalds 
16113dcd25f3SJan Blunck static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
16121da177e4SLinus Torvalds {
1613e12ba74dSMel Gorman 	char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
16143dcd25f3SJan Blunck 	char *pathname;
16151da177e4SLinus Torvalds 	int len;
16161da177e4SLinus Torvalds 
16171da177e4SLinus Torvalds 	if (!tmp)
16181da177e4SLinus Torvalds 		return -ENOMEM;
16191da177e4SLinus Torvalds 
16207b2a69baSEric W. Biederman 	pathname = d_path(path, tmp, PAGE_SIZE);
16213dcd25f3SJan Blunck 	len = PTR_ERR(pathname);
16223dcd25f3SJan Blunck 	if (IS_ERR(pathname))
16231da177e4SLinus Torvalds 		goto out;
16243dcd25f3SJan Blunck 	len = tmp + PAGE_SIZE - 1 - pathname;
16251da177e4SLinus Torvalds 
16261da177e4SLinus Torvalds 	if (len > buflen)
16271da177e4SLinus Torvalds 		len = buflen;
16283dcd25f3SJan Blunck 	if (copy_to_user(buffer, pathname, len))
16291da177e4SLinus Torvalds 		len = -EFAULT;
16301da177e4SLinus Torvalds  out:
16311da177e4SLinus Torvalds 	free_page((unsigned long)tmp);
16321da177e4SLinus Torvalds 	return len;
16331da177e4SLinus Torvalds }
16341da177e4SLinus Torvalds 
16351da177e4SLinus Torvalds static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
16361da177e4SLinus Torvalds {
16371da177e4SLinus Torvalds 	int error = -EACCES;
16381da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
16393dcd25f3SJan Blunck 	struct path path;
16401da177e4SLinus Torvalds 
1641778c1144SEric W. Biederman 	/* Are we allowed to snoop on the tasks file descriptors? */
1642778c1144SEric W. Biederman 	if (!proc_fd_access_allowed(inode))
16431da177e4SLinus Torvalds 		goto out;
16441da177e4SLinus Torvalds 
16453dcd25f3SJan Blunck 	error = PROC_I(inode)->op.proc_get_link(inode, &path);
16461da177e4SLinus Torvalds 	if (error)
16471da177e4SLinus Torvalds 		goto out;
16481da177e4SLinus Torvalds 
16493dcd25f3SJan Blunck 	error = do_proc_readlink(&path, buffer, buflen);
16503dcd25f3SJan Blunck 	path_put(&path);
16511da177e4SLinus Torvalds out:
16521da177e4SLinus Torvalds 	return error;
16531da177e4SLinus Torvalds }
16541da177e4SLinus Torvalds 
1655c5ef1c42SArjan van de Ven static const struct inode_operations proc_pid_link_inode_operations = {
16561da177e4SLinus Torvalds 	.readlink	= proc_pid_readlink,
16576d76fa58SLinus Torvalds 	.follow_link	= proc_pid_follow_link,
16586d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
16591da177e4SLinus Torvalds };
16601da177e4SLinus Torvalds 
166128a6d671SEric W. Biederman 
166228a6d671SEric W. Biederman /* building an inode */
166328a6d671SEric W. Biederman 
166428a6d671SEric W. Biederman static int task_dumpable(struct task_struct *task)
166528a6d671SEric W. Biederman {
166628a6d671SEric W. Biederman 	int dumpable = 0;
166728a6d671SEric W. Biederman 	struct mm_struct *mm;
166828a6d671SEric W. Biederman 
166928a6d671SEric W. Biederman 	task_lock(task);
167028a6d671SEric W. Biederman 	mm = task->mm;
167128a6d671SEric W. Biederman 	if (mm)
16726c5d5238SKawai, Hidehiro 		dumpable = get_dumpable(mm);
167328a6d671SEric W. Biederman 	task_unlock(task);
167428a6d671SEric W. Biederman 	if(dumpable == 1)
167528a6d671SEric W. Biederman 		return 1;
167628a6d671SEric W. Biederman 	return 0;
167728a6d671SEric W. Biederman }
167828a6d671SEric W. Biederman 
16796b4e306aSEric W. Biederman struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
168028a6d671SEric W. Biederman {
168128a6d671SEric W. Biederman 	struct inode * inode;
168228a6d671SEric W. Biederman 	struct proc_inode *ei;
1683c69e8d9cSDavid Howells 	const struct cred *cred;
168428a6d671SEric W. Biederman 
168528a6d671SEric W. Biederman 	/* We need a new inode */
168628a6d671SEric W. Biederman 
168728a6d671SEric W. Biederman 	inode = new_inode(sb);
168828a6d671SEric W. Biederman 	if (!inode)
168928a6d671SEric W. Biederman 		goto out;
169028a6d671SEric W. Biederman 
169128a6d671SEric W. Biederman 	/* Common stuff */
169228a6d671SEric W. Biederman 	ei = PROC_I(inode);
169385fe4025SChristoph Hellwig 	inode->i_ino = get_next_ino();
169428a6d671SEric W. Biederman 	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
169528a6d671SEric W. Biederman 	inode->i_op = &proc_def_inode_operations;
169628a6d671SEric W. Biederman 
169728a6d671SEric W. Biederman 	/*
169828a6d671SEric W. Biederman 	 * grab the reference to task.
169928a6d671SEric W. Biederman 	 */
17001a657f78SOleg Nesterov 	ei->pid = get_task_pid(task, PIDTYPE_PID);
170128a6d671SEric W. Biederman 	if (!ei->pid)
170228a6d671SEric W. Biederman 		goto out_unlock;
170328a6d671SEric W. Biederman 
170428a6d671SEric W. Biederman 	if (task_dumpable(task)) {
1705c69e8d9cSDavid Howells 		rcu_read_lock();
1706c69e8d9cSDavid Howells 		cred = __task_cred(task);
1707c69e8d9cSDavid Howells 		inode->i_uid = cred->euid;
1708c69e8d9cSDavid Howells 		inode->i_gid = cred->egid;
1709c69e8d9cSDavid Howells 		rcu_read_unlock();
171028a6d671SEric W. Biederman 	}
171128a6d671SEric W. Biederman 	security_task_to_inode(task, inode);
171228a6d671SEric W. Biederman 
171328a6d671SEric W. Biederman out:
171428a6d671SEric W. Biederman 	return inode;
171528a6d671SEric W. Biederman 
171628a6d671SEric W. Biederman out_unlock:
171728a6d671SEric W. Biederman 	iput(inode);
171828a6d671SEric W. Biederman 	return NULL;
171928a6d671SEric W. Biederman }
172028a6d671SEric W. Biederman 
17216b4e306aSEric W. Biederman int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
172228a6d671SEric W. Biederman {
172328a6d671SEric W. Biederman 	struct inode *inode = dentry->d_inode;
172428a6d671SEric W. Biederman 	struct task_struct *task;
1725c69e8d9cSDavid Howells 	const struct cred *cred;
1726c69e8d9cSDavid Howells 
172728a6d671SEric W. Biederman 	generic_fillattr(inode, stat);
172828a6d671SEric W. Biederman 
172928a6d671SEric W. Biederman 	rcu_read_lock();
173028a6d671SEric W. Biederman 	stat->uid = 0;
173128a6d671SEric W. Biederman 	stat->gid = 0;
173228a6d671SEric W. Biederman 	task = pid_task(proc_pid(inode), PIDTYPE_PID);
173328a6d671SEric W. Biederman 	if (task) {
173428a6d671SEric W. Biederman 		if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
173528a6d671SEric W. Biederman 		    task_dumpable(task)) {
1736c69e8d9cSDavid Howells 			cred = __task_cred(task);
1737c69e8d9cSDavid Howells 			stat->uid = cred->euid;
1738c69e8d9cSDavid Howells 			stat->gid = cred->egid;
173928a6d671SEric W. Biederman 		}
174028a6d671SEric W. Biederman 	}
174128a6d671SEric W. Biederman 	rcu_read_unlock();
174228a6d671SEric W. Biederman 	return 0;
174328a6d671SEric W. Biederman }
174428a6d671SEric W. Biederman 
174528a6d671SEric W. Biederman /* dentry stuff */
174628a6d671SEric W. Biederman 
174728a6d671SEric W. Biederman /*
174828a6d671SEric W. Biederman  *	Exceptional case: normally we are not allowed to unhash a busy
174928a6d671SEric W. Biederman  * directory. In this case, however, we can do it - no aliasing problems
175028a6d671SEric W. Biederman  * due to the way we treat inodes.
175128a6d671SEric W. Biederman  *
175228a6d671SEric W. Biederman  * Rewrite the inode's ownerships here because the owning task may have
175328a6d671SEric W. Biederman  * performed a setuid(), etc.
175428a6d671SEric W. Biederman  *
175528a6d671SEric W. Biederman  * Before the /proc/pid/status file was created the only way to read
175628a6d671SEric W. Biederman  * the effective uid of a /process was to stat /proc/pid.  Reading
175728a6d671SEric W. Biederman  * /proc/pid/status is slow enough that procps and other packages
175828a6d671SEric W. Biederman  * kept stating /proc/pid.  To keep the rules in /proc simple I have
175928a6d671SEric W. Biederman  * made this apply to all per process world readable and executable
176028a6d671SEric W. Biederman  * directories.
176128a6d671SEric W. Biederman  */
17626b4e306aSEric W. Biederman int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
176328a6d671SEric W. Biederman {
176434286d66SNick Piggin 	struct inode *inode;
176534286d66SNick Piggin 	struct task_struct *task;
1766c69e8d9cSDavid Howells 	const struct cred *cred;
1767c69e8d9cSDavid Howells 
176834286d66SNick Piggin 	if (nd && nd->flags & LOOKUP_RCU)
176934286d66SNick Piggin 		return -ECHILD;
177034286d66SNick Piggin 
177134286d66SNick Piggin 	inode = dentry->d_inode;
177234286d66SNick Piggin 	task = get_proc_task(inode);
177334286d66SNick Piggin 
177428a6d671SEric W. Biederman 	if (task) {
177528a6d671SEric W. Biederman 		if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
177628a6d671SEric W. Biederman 		    task_dumpable(task)) {
1777c69e8d9cSDavid Howells 			rcu_read_lock();
1778c69e8d9cSDavid Howells 			cred = __task_cred(task);
1779c69e8d9cSDavid Howells 			inode->i_uid = cred->euid;
1780c69e8d9cSDavid Howells 			inode->i_gid = cred->egid;
1781c69e8d9cSDavid Howells 			rcu_read_unlock();
178228a6d671SEric W. Biederman 		} else {
178328a6d671SEric W. Biederman 			inode->i_uid = 0;
178428a6d671SEric W. Biederman 			inode->i_gid = 0;
178528a6d671SEric W. Biederman 		}
178628a6d671SEric W. Biederman 		inode->i_mode &= ~(S_ISUID | S_ISGID);
178728a6d671SEric W. Biederman 		security_task_to_inode(task, inode);
178828a6d671SEric W. Biederman 		put_task_struct(task);
178928a6d671SEric W. Biederman 		return 1;
179028a6d671SEric W. Biederman 	}
179128a6d671SEric W. Biederman 	d_drop(dentry);
179228a6d671SEric W. Biederman 	return 0;
179328a6d671SEric W. Biederman }
179428a6d671SEric W. Biederman 
1795fe15ce44SNick Piggin static int pid_delete_dentry(const struct dentry * dentry)
179628a6d671SEric W. Biederman {
179728a6d671SEric W. Biederman 	/* Is the task we represent dead?
179828a6d671SEric W. Biederman 	 * If so, then don't put the dentry on the lru list,
179928a6d671SEric W. Biederman 	 * kill it immediately.
180028a6d671SEric W. Biederman 	 */
180128a6d671SEric W. Biederman 	return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
180228a6d671SEric W. Biederman }
180328a6d671SEric W. Biederman 
18046b4e306aSEric W. Biederman const struct dentry_operations pid_dentry_operations =
180528a6d671SEric W. Biederman {
180628a6d671SEric W. Biederman 	.d_revalidate	= pid_revalidate,
180728a6d671SEric W. Biederman 	.d_delete	= pid_delete_dentry,
180828a6d671SEric W. Biederman };
180928a6d671SEric W. Biederman 
181028a6d671SEric W. Biederman /* Lookups */
181128a6d671SEric W. Biederman 
18121c0d04c9SEric W. Biederman /*
18131c0d04c9SEric W. Biederman  * Fill a directory entry.
18141c0d04c9SEric W. Biederman  *
18151c0d04c9SEric W. Biederman  * If possible create the dcache entry and derive our inode number and
18161c0d04c9SEric W. Biederman  * file type from dcache entry.
18171c0d04c9SEric W. Biederman  *
18181c0d04c9SEric W. Biederman  * Since all of the proc inode numbers are dynamically generated, the inode
18191c0d04c9SEric W. Biederman  * numbers do not exist until the inode is cache.  This means creating the
18201c0d04c9SEric W. Biederman  * the dcache entry in readdir is necessary to keep the inode numbers
18211c0d04c9SEric W. Biederman  * reported by readdir in sync with the inode numbers reported
18221c0d04c9SEric W. Biederman  * by stat.
18231c0d04c9SEric W. Biederman  */
18246b4e306aSEric W. Biederman int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
18256b4e306aSEric W. Biederman 	const char *name, int len,
1826c5141e6dSEric Dumazet 	instantiate_t instantiate, struct task_struct *task, const void *ptr)
182761a28784SEric W. Biederman {
18282fddfeefSJosef "Jeff" Sipek 	struct dentry *child, *dir = filp->f_path.dentry;
182961a28784SEric W. Biederman 	struct inode *inode;
183061a28784SEric W. Biederman 	struct qstr qname;
183161a28784SEric W. Biederman 	ino_t ino = 0;
183261a28784SEric W. Biederman 	unsigned type = DT_UNKNOWN;
183361a28784SEric W. Biederman 
183461a28784SEric W. Biederman 	qname.name = name;
183561a28784SEric W. Biederman 	qname.len  = len;
183661a28784SEric W. Biederman 	qname.hash = full_name_hash(name, len);
183761a28784SEric W. Biederman 
183861a28784SEric W. Biederman 	child = d_lookup(dir, &qname);
183961a28784SEric W. Biederman 	if (!child) {
184061a28784SEric W. Biederman 		struct dentry *new;
184161a28784SEric W. Biederman 		new = d_alloc(dir, &qname);
184261a28784SEric W. Biederman 		if (new) {
184361a28784SEric W. Biederman 			child = instantiate(dir->d_inode, new, task, ptr);
184461a28784SEric W. Biederman 			if (child)
184561a28784SEric W. Biederman 				dput(new);
184661a28784SEric W. Biederman 			else
184761a28784SEric W. Biederman 				child = new;
184861a28784SEric W. Biederman 		}
184961a28784SEric W. Biederman 	}
185061a28784SEric W. Biederman 	if (!child || IS_ERR(child) || !child->d_inode)
185161a28784SEric W. Biederman 		goto end_instantiate;
185261a28784SEric W. Biederman 	inode = child->d_inode;
185361a28784SEric W. Biederman 	if (inode) {
185461a28784SEric W. Biederman 		ino = inode->i_ino;
185561a28784SEric W. Biederman 		type = inode->i_mode >> 12;
185661a28784SEric W. Biederman 	}
185761a28784SEric W. Biederman 	dput(child);
185861a28784SEric W. Biederman end_instantiate:
185961a28784SEric W. Biederman 	if (!ino)
186061a28784SEric W. Biederman 		ino = find_inode_number(dir, &qname);
186161a28784SEric W. Biederman 	if (!ino)
186261a28784SEric W. Biederman 		ino = 1;
186361a28784SEric W. Biederman 	return filldir(dirent, name, len, filp->f_pos, ino, type);
186461a28784SEric W. Biederman }
186561a28784SEric W. Biederman 
186628a6d671SEric W. Biederman static unsigned name_to_int(struct dentry *dentry)
186728a6d671SEric W. Biederman {
186828a6d671SEric W. Biederman 	const char *name = dentry->d_name.name;
186928a6d671SEric W. Biederman 	int len = dentry->d_name.len;
187028a6d671SEric W. Biederman 	unsigned n = 0;
187128a6d671SEric W. Biederman 
187228a6d671SEric W. Biederman 	if (len > 1 && *name == '0')
187328a6d671SEric W. Biederman 		goto out;
187428a6d671SEric W. Biederman 	while (len-- > 0) {
187528a6d671SEric W. Biederman 		unsigned c = *name++ - '0';
187628a6d671SEric W. Biederman 		if (c > 9)
187728a6d671SEric W. Biederman 			goto out;
187828a6d671SEric W. Biederman 		if (n >= (~0U-9)/10)
187928a6d671SEric W. Biederman 			goto out;
188028a6d671SEric W. Biederman 		n *= 10;
188128a6d671SEric W. Biederman 		n += c;
188228a6d671SEric W. Biederman 	}
188328a6d671SEric W. Biederman 	return n;
188428a6d671SEric W. Biederman out:
188528a6d671SEric W. Biederman 	return ~0U;
188628a6d671SEric W. Biederman }
188728a6d671SEric W. Biederman 
188827932742SMiklos Szeredi #define PROC_FDINFO_MAX 64
188927932742SMiklos Szeredi 
18903dcd25f3SJan Blunck static int proc_fd_info(struct inode *inode, struct path *path, char *info)
189128a6d671SEric W. Biederman {
189228a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
189328a6d671SEric W. Biederman 	struct files_struct *files = NULL;
189428a6d671SEric W. Biederman 	struct file *file;
189528a6d671SEric W. Biederman 	int fd = proc_fd(inode);
189628a6d671SEric W. Biederman 
189728a6d671SEric W. Biederman 	if (task) {
189828a6d671SEric W. Biederman 		files = get_files_struct(task);
189928a6d671SEric W. Biederman 		put_task_struct(task);
190028a6d671SEric W. Biederman 	}
190128a6d671SEric W. Biederman 	if (files) {
190228a6d671SEric W. Biederman 		/*
190328a6d671SEric W. Biederman 		 * We are not taking a ref to the file structure, so we must
190428a6d671SEric W. Biederman 		 * hold ->file_lock.
190528a6d671SEric W. Biederman 		 */
190628a6d671SEric W. Biederman 		spin_lock(&files->file_lock);
190728a6d671SEric W. Biederman 		file = fcheck_files(files, fd);
190828a6d671SEric W. Biederman 		if (file) {
19091117f72eSLinus Torvalds 			unsigned int f_flags;
19101117f72eSLinus Torvalds 			struct fdtable *fdt;
19111117f72eSLinus Torvalds 
19121117f72eSLinus Torvalds 			fdt = files_fdtable(files);
19131117f72eSLinus Torvalds 			f_flags = file->f_flags & ~O_CLOEXEC;
19141117f72eSLinus Torvalds 			if (FD_ISSET(fd, fdt->close_on_exec))
19151117f72eSLinus Torvalds 				f_flags |= O_CLOEXEC;
19161117f72eSLinus Torvalds 
19173dcd25f3SJan Blunck 			if (path) {
19183dcd25f3SJan Blunck 				*path = file->f_path;
19193dcd25f3SJan Blunck 				path_get(&file->f_path);
19203dcd25f3SJan Blunck 			}
192127932742SMiklos Szeredi 			if (info)
192227932742SMiklos Szeredi 				snprintf(info, PROC_FDINFO_MAX,
192327932742SMiklos Szeredi 					 "pos:\t%lli\n"
192427932742SMiklos Szeredi 					 "flags:\t0%o\n",
192527932742SMiklos Szeredi 					 (long long) file->f_pos,
19261117f72eSLinus Torvalds 					 f_flags);
192728a6d671SEric W. Biederman 			spin_unlock(&files->file_lock);
192828a6d671SEric W. Biederman 			put_files_struct(files);
192928a6d671SEric W. Biederman 			return 0;
193028a6d671SEric W. Biederman 		}
193128a6d671SEric W. Biederman 		spin_unlock(&files->file_lock);
193228a6d671SEric W. Biederman 		put_files_struct(files);
193328a6d671SEric W. Biederman 	}
193428a6d671SEric W. Biederman 	return -ENOENT;
193528a6d671SEric W. Biederman }
193628a6d671SEric W. Biederman 
19373dcd25f3SJan Blunck static int proc_fd_link(struct inode *inode, struct path *path)
193827932742SMiklos Szeredi {
19393dcd25f3SJan Blunck 	return proc_fd_info(inode, path, NULL);
194027932742SMiklos Szeredi }
194127932742SMiklos Szeredi 
194228a6d671SEric W. Biederman static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
194328a6d671SEric W. Biederman {
194434286d66SNick Piggin 	struct inode *inode;
194534286d66SNick Piggin 	struct task_struct *task;
194634286d66SNick Piggin 	int fd;
194728a6d671SEric W. Biederman 	struct files_struct *files;
1948c69e8d9cSDavid Howells 	const struct cred *cred;
194928a6d671SEric W. Biederman 
195034286d66SNick Piggin 	if (nd && nd->flags & LOOKUP_RCU)
195134286d66SNick Piggin 		return -ECHILD;
195234286d66SNick Piggin 
195334286d66SNick Piggin 	inode = dentry->d_inode;
195434286d66SNick Piggin 	task = get_proc_task(inode);
195534286d66SNick Piggin 	fd = proc_fd(inode);
195634286d66SNick Piggin 
195728a6d671SEric W. Biederman 	if (task) {
195828a6d671SEric W. Biederman 		files = get_files_struct(task);
195928a6d671SEric W. Biederman 		if (files) {
196028a6d671SEric W. Biederman 			rcu_read_lock();
196128a6d671SEric W. Biederman 			if (fcheck_files(files, fd)) {
196228a6d671SEric W. Biederman 				rcu_read_unlock();
196328a6d671SEric W. Biederman 				put_files_struct(files);
196428a6d671SEric W. Biederman 				if (task_dumpable(task)) {
1965c69e8d9cSDavid Howells 					rcu_read_lock();
1966c69e8d9cSDavid Howells 					cred = __task_cred(task);
1967c69e8d9cSDavid Howells 					inode->i_uid = cred->euid;
1968c69e8d9cSDavid Howells 					inode->i_gid = cred->egid;
1969c69e8d9cSDavid Howells 					rcu_read_unlock();
197028a6d671SEric W. Biederman 				} else {
197128a6d671SEric W. Biederman 					inode->i_uid = 0;
197228a6d671SEric W. Biederman 					inode->i_gid = 0;
197328a6d671SEric W. Biederman 				}
197428a6d671SEric W. Biederman 				inode->i_mode &= ~(S_ISUID | S_ISGID);
197528a6d671SEric W. Biederman 				security_task_to_inode(task, inode);
197628a6d671SEric W. Biederman 				put_task_struct(task);
197728a6d671SEric W. Biederman 				return 1;
197828a6d671SEric W. Biederman 			}
197928a6d671SEric W. Biederman 			rcu_read_unlock();
198028a6d671SEric W. Biederman 			put_files_struct(files);
198128a6d671SEric W. Biederman 		}
198228a6d671SEric W. Biederman 		put_task_struct(task);
198328a6d671SEric W. Biederman 	}
198428a6d671SEric W. Biederman 	d_drop(dentry);
198528a6d671SEric W. Biederman 	return 0;
198628a6d671SEric W. Biederman }
198728a6d671SEric W. Biederman 
1988d72f71ebSAl Viro static const struct dentry_operations tid_fd_dentry_operations =
198928a6d671SEric W. Biederman {
199028a6d671SEric W. Biederman 	.d_revalidate	= tid_fd_revalidate,
199128a6d671SEric W. Biederman 	.d_delete	= pid_delete_dentry,
199228a6d671SEric W. Biederman };
199328a6d671SEric W. Biederman 
1994444ceed8SEric W. Biederman static struct dentry *proc_fd_instantiate(struct inode *dir,
1995c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
199628a6d671SEric W. Biederman {
1997c5141e6dSEric Dumazet 	unsigned fd = *(const unsigned *)ptr;
199828a6d671SEric W. Biederman 	struct file *file;
199928a6d671SEric W. Biederman 	struct files_struct *files;
200028a6d671SEric W. Biederman  	struct inode *inode;
200128a6d671SEric W. Biederman  	struct proc_inode *ei;
2002444ceed8SEric W. Biederman 	struct dentry *error = ERR_PTR(-ENOENT);
200328a6d671SEric W. Biederman 
200461a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
200528a6d671SEric W. Biederman 	if (!inode)
200628a6d671SEric W. Biederman 		goto out;
200728a6d671SEric W. Biederman 	ei = PROC_I(inode);
200828a6d671SEric W. Biederman 	ei->fd = fd;
200928a6d671SEric W. Biederman 	files = get_files_struct(task);
201028a6d671SEric W. Biederman 	if (!files)
2011444ceed8SEric W. Biederman 		goto out_iput;
201228a6d671SEric W. Biederman 	inode->i_mode = S_IFLNK;
201328a6d671SEric W. Biederman 
201428a6d671SEric W. Biederman 	/*
201528a6d671SEric W. Biederman 	 * We are not taking a ref to the file structure, so we must
201628a6d671SEric W. Biederman 	 * hold ->file_lock.
201728a6d671SEric W. Biederman 	 */
201828a6d671SEric W. Biederman 	spin_lock(&files->file_lock);
201928a6d671SEric W. Biederman 	file = fcheck_files(files, fd);
202028a6d671SEric W. Biederman 	if (!file)
2021444ceed8SEric W. Biederman 		goto out_unlock;
2022aeb5d727SAl Viro 	if (file->f_mode & FMODE_READ)
202328a6d671SEric W. Biederman 		inode->i_mode |= S_IRUSR | S_IXUSR;
2024aeb5d727SAl Viro 	if (file->f_mode & FMODE_WRITE)
202528a6d671SEric W. Biederman 		inode->i_mode |= S_IWUSR | S_IXUSR;
202628a6d671SEric W. Biederman 	spin_unlock(&files->file_lock);
202728a6d671SEric W. Biederman 	put_files_struct(files);
2028444ceed8SEric W. Biederman 
202928a6d671SEric W. Biederman 	inode->i_op = &proc_pid_link_inode_operations;
203028a6d671SEric W. Biederman 	inode->i_size = 64;
203128a6d671SEric W. Biederman 	ei->op.proc_get_link = proc_fd_link;
2032fb045adbSNick Piggin 	d_set_d_op(dentry, &tid_fd_dentry_operations);
203328a6d671SEric W. Biederman 	d_add(dentry, inode);
203428a6d671SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
203528a6d671SEric W. Biederman 	if (tid_fd_revalidate(dentry, NULL))
2036444ceed8SEric W. Biederman 		error = NULL;
2037444ceed8SEric W. Biederman 
2038444ceed8SEric W. Biederman  out:
2039444ceed8SEric W. Biederman 	return error;
2040444ceed8SEric W. Biederman out_unlock:
2041444ceed8SEric W. Biederman 	spin_unlock(&files->file_lock);
2042444ceed8SEric W. Biederman 	put_files_struct(files);
2043444ceed8SEric W. Biederman out_iput:
2044444ceed8SEric W. Biederman 	iput(inode);
2045444ceed8SEric W. Biederman 	goto out;
2046444ceed8SEric W. Biederman }
2047444ceed8SEric W. Biederman 
204827932742SMiklos Szeredi static struct dentry *proc_lookupfd_common(struct inode *dir,
204927932742SMiklos Szeredi 					   struct dentry *dentry,
205027932742SMiklos Szeredi 					   instantiate_t instantiate)
2051444ceed8SEric W. Biederman {
2052444ceed8SEric W. Biederman 	struct task_struct *task = get_proc_task(dir);
2053444ceed8SEric W. Biederman 	unsigned fd = name_to_int(dentry);
2054444ceed8SEric W. Biederman 	struct dentry *result = ERR_PTR(-ENOENT);
2055444ceed8SEric W. Biederman 
2056444ceed8SEric W. Biederman 	if (!task)
2057444ceed8SEric W. Biederman 		goto out_no_task;
2058444ceed8SEric W. Biederman 	if (fd == ~0U)
2059444ceed8SEric W. Biederman 		goto out;
2060444ceed8SEric W. Biederman 
206127932742SMiklos Szeredi 	result = instantiate(dir, dentry, task, &fd);
206228a6d671SEric W. Biederman out:
206328a6d671SEric W. Biederman 	put_task_struct(task);
206428a6d671SEric W. Biederman out_no_task:
206528a6d671SEric W. Biederman 	return result;
206628a6d671SEric W. Biederman }
206728a6d671SEric W. Biederman 
206827932742SMiklos Szeredi static int proc_readfd_common(struct file * filp, void * dirent,
206927932742SMiklos Szeredi 			      filldir_t filldir, instantiate_t instantiate)
20701da177e4SLinus Torvalds {
20712fddfeefSJosef "Jeff" Sipek 	struct dentry *dentry = filp->f_path.dentry;
20725634708bSEric W. Biederman 	struct inode *inode = dentry->d_inode;
207399f89551SEric W. Biederman 	struct task_struct *p = get_proc_task(inode);
2074457c2510SPavel Emelyanov 	unsigned int fd, ino;
20751da177e4SLinus Torvalds 	int retval;
20761da177e4SLinus Torvalds 	struct files_struct * files;
20771da177e4SLinus Torvalds 
20781da177e4SLinus Torvalds 	retval = -ENOENT;
207999f89551SEric W. Biederman 	if (!p)
208099f89551SEric W. Biederman 		goto out_no_task;
20811da177e4SLinus Torvalds 	retval = 0;
20821da177e4SLinus Torvalds 
20831da177e4SLinus Torvalds 	fd = filp->f_pos;
20841da177e4SLinus Torvalds 	switch (fd) {
20851da177e4SLinus Torvalds 		case 0:
20861da177e4SLinus Torvalds 			if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
20871da177e4SLinus Torvalds 				goto out;
20881da177e4SLinus Torvalds 			filp->f_pos++;
20891da177e4SLinus Torvalds 		case 1:
20905634708bSEric W. Biederman 			ino = parent_ino(dentry);
20911da177e4SLinus Torvalds 			if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
20921da177e4SLinus Torvalds 				goto out;
20931da177e4SLinus Torvalds 			filp->f_pos++;
20941da177e4SLinus Torvalds 		default:
20951da177e4SLinus Torvalds 			files = get_files_struct(p);
20961da177e4SLinus Torvalds 			if (!files)
20971da177e4SLinus Torvalds 				goto out;
2098b835996fSDipankar Sarma 			rcu_read_lock();
20991da177e4SLinus Torvalds 			for (fd = filp->f_pos-2;
21009b4f526cSAl Viro 			     fd < files_fdtable(files)->max_fds;
21011da177e4SLinus Torvalds 			     fd++, filp->f_pos++) {
210227932742SMiklos Szeredi 				char name[PROC_NUMBUF];
210327932742SMiklos Szeredi 				int len;
21041da177e4SLinus Torvalds 
21051da177e4SLinus Torvalds 				if (!fcheck_files(files, fd))
21061da177e4SLinus Torvalds 					continue;
2107b835996fSDipankar Sarma 				rcu_read_unlock();
21081da177e4SLinus Torvalds 
210927932742SMiklos Szeredi 				len = snprintf(name, sizeof(name), "%d", fd);
211027932742SMiklos Szeredi 				if (proc_fill_cache(filp, dirent, filldir,
211127932742SMiklos Szeredi 						    name, len, instantiate,
211227932742SMiklos Szeredi 						    p, &fd) < 0) {
2113b835996fSDipankar Sarma 					rcu_read_lock();
21141da177e4SLinus Torvalds 					break;
21151da177e4SLinus Torvalds 				}
2116b835996fSDipankar Sarma 				rcu_read_lock();
21171da177e4SLinus Torvalds 			}
2118b835996fSDipankar Sarma 			rcu_read_unlock();
21191da177e4SLinus Torvalds 			put_files_struct(files);
21201da177e4SLinus Torvalds 	}
21211da177e4SLinus Torvalds out:
212299f89551SEric W. Biederman 	put_task_struct(p);
212399f89551SEric W. Biederman out_no_task:
21241da177e4SLinus Torvalds 	return retval;
21251da177e4SLinus Torvalds }
21261da177e4SLinus Torvalds 
212727932742SMiklos Szeredi static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
212827932742SMiklos Szeredi 				    struct nameidata *nd)
212927932742SMiklos Szeredi {
213027932742SMiklos Szeredi 	return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
213127932742SMiklos Szeredi }
213227932742SMiklos Szeredi 
213327932742SMiklos Szeredi static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
213427932742SMiklos Szeredi {
213527932742SMiklos Szeredi 	return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
213627932742SMiklos Szeredi }
213727932742SMiklos Szeredi 
213827932742SMiklos Szeredi static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
213927932742SMiklos Szeredi 				      size_t len, loff_t *ppos)
214027932742SMiklos Szeredi {
214127932742SMiklos Szeredi 	char tmp[PROC_FDINFO_MAX];
21423dcd25f3SJan Blunck 	int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
214327932742SMiklos Szeredi 	if (!err)
214427932742SMiklos Szeredi 		err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
214527932742SMiklos Szeredi 	return err;
214627932742SMiklos Szeredi }
214727932742SMiklos Szeredi 
214827932742SMiklos Szeredi static const struct file_operations proc_fdinfo_file_operations = {
214927932742SMiklos Szeredi 	.open           = nonseekable_open,
215027932742SMiklos Szeredi 	.read		= proc_fdinfo_read,
21516038f373SArnd Bergmann 	.llseek		= no_llseek,
215227932742SMiklos Szeredi };
215327932742SMiklos Szeredi 
215400977a59SArjan van de Ven static const struct file_operations proc_fd_operations = {
21551da177e4SLinus Torvalds 	.read		= generic_read_dir,
21561da177e4SLinus Torvalds 	.readdir	= proc_readfd,
21576038f373SArnd Bergmann 	.llseek		= default_llseek,
21581da177e4SLinus Torvalds };
21591da177e4SLinus Torvalds 
21601da177e4SLinus Torvalds /*
21618948e11fSAlexey Dobriyan  * /proc/pid/fd needs a special permission handler so that a process can still
21628948e11fSAlexey Dobriyan  * access /proc/self/fd after it has executed a setuid().
21638948e11fSAlexey Dobriyan  */
216410556cb2SAl Viro static int proc_fd_permission(struct inode *inode, int mask)
21658948e11fSAlexey Dobriyan {
21662830ba7fSAl Viro 	int rv = generic_permission(inode, mask);
21678948e11fSAlexey Dobriyan 	if (rv == 0)
21688948e11fSAlexey Dobriyan 		return 0;
21698948e11fSAlexey Dobriyan 	if (task_pid(current) == proc_pid(inode))
21708948e11fSAlexey Dobriyan 		rv = 0;
21718948e11fSAlexey Dobriyan 	return rv;
21728948e11fSAlexey Dobriyan }
21738948e11fSAlexey Dobriyan 
21748948e11fSAlexey Dobriyan /*
21751da177e4SLinus Torvalds  * proc directories can do almost nothing..
21761da177e4SLinus Torvalds  */
2177c5ef1c42SArjan van de Ven static const struct inode_operations proc_fd_inode_operations = {
21781da177e4SLinus Torvalds 	.lookup		= proc_lookupfd,
21798948e11fSAlexey Dobriyan 	.permission	= proc_fd_permission,
21806d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
21811da177e4SLinus Torvalds };
21821da177e4SLinus Torvalds 
218327932742SMiklos Szeredi static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
218427932742SMiklos Szeredi 	struct dentry *dentry, struct task_struct *task, const void *ptr)
218527932742SMiklos Szeredi {
218627932742SMiklos Szeredi 	unsigned fd = *(unsigned *)ptr;
218727932742SMiklos Szeredi  	struct inode *inode;
218827932742SMiklos Szeredi  	struct proc_inode *ei;
218927932742SMiklos Szeredi 	struct dentry *error = ERR_PTR(-ENOENT);
219027932742SMiklos Szeredi 
219127932742SMiklos Szeredi 	inode = proc_pid_make_inode(dir->i_sb, task);
219227932742SMiklos Szeredi 	if (!inode)
219327932742SMiklos Szeredi 		goto out;
219427932742SMiklos Szeredi 	ei = PROC_I(inode);
219527932742SMiklos Szeredi 	ei->fd = fd;
219627932742SMiklos Szeredi 	inode->i_mode = S_IFREG | S_IRUSR;
219727932742SMiklos Szeredi 	inode->i_fop = &proc_fdinfo_file_operations;
2198fb045adbSNick Piggin 	d_set_d_op(dentry, &tid_fd_dentry_operations);
219927932742SMiklos Szeredi 	d_add(dentry, inode);
220027932742SMiklos Szeredi 	/* Close the race of the process dying before we return the dentry */
220127932742SMiklos Szeredi 	if (tid_fd_revalidate(dentry, NULL))
220227932742SMiklos Szeredi 		error = NULL;
220327932742SMiklos Szeredi 
220427932742SMiklos Szeredi  out:
220527932742SMiklos Szeredi 	return error;
220627932742SMiklos Szeredi }
220727932742SMiklos Szeredi 
220827932742SMiklos Szeredi static struct dentry *proc_lookupfdinfo(struct inode *dir,
220927932742SMiklos Szeredi 					struct dentry *dentry,
221027932742SMiklos Szeredi 					struct nameidata *nd)
221127932742SMiklos Szeredi {
221227932742SMiklos Szeredi 	return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
221327932742SMiklos Szeredi }
221427932742SMiklos Szeredi 
221527932742SMiklos Szeredi static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
221627932742SMiklos Szeredi {
221727932742SMiklos Szeredi 	return proc_readfd_common(filp, dirent, filldir,
221827932742SMiklos Szeredi 				  proc_fdinfo_instantiate);
221927932742SMiklos Szeredi }
222027932742SMiklos Szeredi 
222127932742SMiklos Szeredi static const struct file_operations proc_fdinfo_operations = {
222227932742SMiklos Szeredi 	.read		= generic_read_dir,
222327932742SMiklos Szeredi 	.readdir	= proc_readfdinfo,
22246038f373SArnd Bergmann 	.llseek		= default_llseek,
222527932742SMiklos Szeredi };
222627932742SMiklos Szeredi 
222727932742SMiklos Szeredi /*
222827932742SMiklos Szeredi  * proc directories can do almost nothing..
222927932742SMiklos Szeredi  */
223027932742SMiklos Szeredi static const struct inode_operations proc_fdinfo_inode_operations = {
223127932742SMiklos Szeredi 	.lookup		= proc_lookupfdinfo,
223227932742SMiklos Szeredi 	.setattr	= proc_setattr,
223327932742SMiklos Szeredi };
223427932742SMiklos Szeredi 
223527932742SMiklos Szeredi 
2236444ceed8SEric W. Biederman static struct dentry *proc_pident_instantiate(struct inode *dir,
2237c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
2238444ceed8SEric W. Biederman {
2239c5141e6dSEric Dumazet 	const struct pid_entry *p = ptr;
2240444ceed8SEric W. Biederman 	struct inode *inode;
2241444ceed8SEric W. Biederman 	struct proc_inode *ei;
2242bd6daba9SKOSAKI Motohiro 	struct dentry *error = ERR_PTR(-ENOENT);
2243444ceed8SEric W. Biederman 
224461a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
2245444ceed8SEric W. Biederman 	if (!inode)
2246444ceed8SEric W. Biederman 		goto out;
2247444ceed8SEric W. Biederman 
2248444ceed8SEric W. Biederman 	ei = PROC_I(inode);
2249444ceed8SEric W. Biederman 	inode->i_mode = p->mode;
2250444ceed8SEric W. Biederman 	if (S_ISDIR(inode->i_mode))
2251*bfe86848SMiklos Szeredi 		set_nlink(inode, 2);	/* Use getattr to fix if necessary */
2252444ceed8SEric W. Biederman 	if (p->iop)
2253444ceed8SEric W. Biederman 		inode->i_op = p->iop;
2254444ceed8SEric W. Biederman 	if (p->fop)
2255444ceed8SEric W. Biederman 		inode->i_fop = p->fop;
2256444ceed8SEric W. Biederman 	ei->op = p->op;
2257fb045adbSNick Piggin 	d_set_d_op(dentry, &pid_dentry_operations);
2258444ceed8SEric W. Biederman 	d_add(dentry, inode);
2259444ceed8SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
2260444ceed8SEric W. Biederman 	if (pid_revalidate(dentry, NULL))
2261444ceed8SEric W. Biederman 		error = NULL;
2262444ceed8SEric W. Biederman out:
2263444ceed8SEric W. Biederman 	return error;
2264444ceed8SEric W. Biederman }
2265444ceed8SEric W. Biederman 
22661da177e4SLinus Torvalds static struct dentry *proc_pident_lookup(struct inode *dir,
22671da177e4SLinus Torvalds 					 struct dentry *dentry,
2268c5141e6dSEric Dumazet 					 const struct pid_entry *ents,
22697bcd6b0eSEric W. Biederman 					 unsigned int nents)
22701da177e4SLinus Torvalds {
2271cd6a3ce9SEric W. Biederman 	struct dentry *error;
227299f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(dir);
2273c5141e6dSEric Dumazet 	const struct pid_entry *p, *last;
22741da177e4SLinus Torvalds 
2275cd6a3ce9SEric W. Biederman 	error = ERR_PTR(-ENOENT);
22761da177e4SLinus Torvalds 
227799f89551SEric W. Biederman 	if (!task)
227899f89551SEric W. Biederman 		goto out_no_task;
22791da177e4SLinus Torvalds 
228020cdc894SEric W. Biederman 	/*
228120cdc894SEric W. Biederman 	 * Yes, it does not scale. And it should not. Don't add
228220cdc894SEric W. Biederman 	 * new entries into /proc/<tgid>/ without very good reasons.
228320cdc894SEric W. Biederman 	 */
22847bcd6b0eSEric W. Biederman 	last = &ents[nents - 1];
22857bcd6b0eSEric W. Biederman 	for (p = ents; p <= last; p++) {
22861da177e4SLinus Torvalds 		if (p->len != dentry->d_name.len)
22871da177e4SLinus Torvalds 			continue;
22881da177e4SLinus Torvalds 		if (!memcmp(dentry->d_name.name, p->name, p->len))
22891da177e4SLinus Torvalds 			break;
22901da177e4SLinus Torvalds 	}
22917bcd6b0eSEric W. Biederman 	if (p > last)
22921da177e4SLinus Torvalds 		goto out;
22931da177e4SLinus Torvalds 
2294444ceed8SEric W. Biederman 	error = proc_pident_instantiate(dir, dentry, task, p);
22951da177e4SLinus Torvalds out:
229699f89551SEric W. Biederman 	put_task_struct(task);
229799f89551SEric W. Biederman out_no_task:
2298cd6a3ce9SEric W. Biederman 	return error;
22991da177e4SLinus Torvalds }
23001da177e4SLinus Torvalds 
2301c5141e6dSEric Dumazet static int proc_pident_fill_cache(struct file *filp, void *dirent,
2302c5141e6dSEric Dumazet 	filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
230361a28784SEric W. Biederman {
230461a28784SEric W. Biederman 	return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
230561a28784SEric W. Biederman 				proc_pident_instantiate, task, p);
230661a28784SEric W. Biederman }
230761a28784SEric W. Biederman 
230828a6d671SEric W. Biederman static int proc_pident_readdir(struct file *filp,
230928a6d671SEric W. Biederman 		void *dirent, filldir_t filldir,
2310c5141e6dSEric Dumazet 		const struct pid_entry *ents, unsigned int nents)
231128a6d671SEric W. Biederman {
231228a6d671SEric W. Biederman 	int i;
23132fddfeefSJosef "Jeff" Sipek 	struct dentry *dentry = filp->f_path.dentry;
231428a6d671SEric W. Biederman 	struct inode *inode = dentry->d_inode;
231528a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
2316c5141e6dSEric Dumazet 	const struct pid_entry *p, *last;
231728a6d671SEric W. Biederman 	ino_t ino;
231828a6d671SEric W. Biederman 	int ret;
231928a6d671SEric W. Biederman 
232028a6d671SEric W. Biederman 	ret = -ENOENT;
232128a6d671SEric W. Biederman 	if (!task)
232261a28784SEric W. Biederman 		goto out_no_task;
232328a6d671SEric W. Biederman 
232428a6d671SEric W. Biederman 	ret = 0;
232528a6d671SEric W. Biederman 	i = filp->f_pos;
232628a6d671SEric W. Biederman 	switch (i) {
232728a6d671SEric W. Biederman 	case 0:
232828a6d671SEric W. Biederman 		ino = inode->i_ino;
232928a6d671SEric W. Biederman 		if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
233028a6d671SEric W. Biederman 			goto out;
233128a6d671SEric W. Biederman 		i++;
233228a6d671SEric W. Biederman 		filp->f_pos++;
233328a6d671SEric W. Biederman 		/* fall through */
233428a6d671SEric W. Biederman 	case 1:
233528a6d671SEric W. Biederman 		ino = parent_ino(dentry);
233628a6d671SEric W. Biederman 		if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
233728a6d671SEric W. Biederman 			goto out;
233828a6d671SEric W. Biederman 		i++;
233928a6d671SEric W. Biederman 		filp->f_pos++;
234028a6d671SEric W. Biederman 		/* fall through */
234128a6d671SEric W. Biederman 	default:
234228a6d671SEric W. Biederman 		i -= 2;
234328a6d671SEric W. Biederman 		if (i >= nents) {
234428a6d671SEric W. Biederman 			ret = 1;
234528a6d671SEric W. Biederman 			goto out;
234628a6d671SEric W. Biederman 		}
234728a6d671SEric W. Biederman 		p = ents + i;
23487bcd6b0eSEric W. Biederman 		last = &ents[nents - 1];
23497bcd6b0eSEric W. Biederman 		while (p <= last) {
235061a28784SEric W. Biederman 			if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
235128a6d671SEric W. Biederman 				goto out;
235228a6d671SEric W. Biederman 			filp->f_pos++;
235328a6d671SEric W. Biederman 			p++;
235428a6d671SEric W. Biederman 		}
23551da177e4SLinus Torvalds 	}
23561da177e4SLinus Torvalds 
235728a6d671SEric W. Biederman 	ret = 1;
235828a6d671SEric W. Biederman out:
235961a28784SEric W. Biederman 	put_task_struct(task);
236061a28784SEric W. Biederman out_no_task:
236128a6d671SEric W. Biederman 	return ret;
23621da177e4SLinus Torvalds }
23631da177e4SLinus Torvalds 
23641da177e4SLinus Torvalds #ifdef CONFIG_SECURITY
236528a6d671SEric W. Biederman static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
236628a6d671SEric W. Biederman 				  size_t count, loff_t *ppos)
236728a6d671SEric W. Biederman {
23682fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
236904ff9708SAl Viro 	char *p = NULL;
237028a6d671SEric W. Biederman 	ssize_t length;
237128a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
237228a6d671SEric W. Biederman 
237328a6d671SEric W. Biederman 	if (!task)
237404ff9708SAl Viro 		return -ESRCH;
237528a6d671SEric W. Biederman 
237628a6d671SEric W. Biederman 	length = security_getprocattr(task,
23772fddfeefSJosef "Jeff" Sipek 				      (char*)file->f_path.dentry->d_name.name,
237804ff9708SAl Viro 				      &p);
237928a6d671SEric W. Biederman 	put_task_struct(task);
238004ff9708SAl Viro 	if (length > 0)
238104ff9708SAl Viro 		length = simple_read_from_buffer(buf, count, ppos, p, length);
238204ff9708SAl Viro 	kfree(p);
238328a6d671SEric W. Biederman 	return length;
238428a6d671SEric W. Biederman }
238528a6d671SEric W. Biederman 
238628a6d671SEric W. Biederman static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
238728a6d671SEric W. Biederman 				   size_t count, loff_t *ppos)
238828a6d671SEric W. Biederman {
23892fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
239028a6d671SEric W. Biederman 	char *page;
239128a6d671SEric W. Biederman 	ssize_t length;
239228a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
239328a6d671SEric W. Biederman 
239428a6d671SEric W. Biederman 	length = -ESRCH;
239528a6d671SEric W. Biederman 	if (!task)
239628a6d671SEric W. Biederman 		goto out_no_task;
239728a6d671SEric W. Biederman 	if (count > PAGE_SIZE)
239828a6d671SEric W. Biederman 		count = PAGE_SIZE;
239928a6d671SEric W. Biederman 
240028a6d671SEric W. Biederman 	/* No partial writes. */
240128a6d671SEric W. Biederman 	length = -EINVAL;
240228a6d671SEric W. Biederman 	if (*ppos != 0)
240328a6d671SEric W. Biederman 		goto out;
240428a6d671SEric W. Biederman 
240528a6d671SEric W. Biederman 	length = -ENOMEM;
2406e12ba74dSMel Gorman 	page = (char*)__get_free_page(GFP_TEMPORARY);
240728a6d671SEric W. Biederman 	if (!page)
240828a6d671SEric W. Biederman 		goto out;
240928a6d671SEric W. Biederman 
241028a6d671SEric W. Biederman 	length = -EFAULT;
241128a6d671SEric W. Biederman 	if (copy_from_user(page, buf, count))
241228a6d671SEric W. Biederman 		goto out_free;
241328a6d671SEric W. Biederman 
2414107db7c7SDavid Howells 	/* Guard against adverse ptrace interaction */
24159b1bf12dSKOSAKI Motohiro 	length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
2416107db7c7SDavid Howells 	if (length < 0)
2417107db7c7SDavid Howells 		goto out_free;
2418107db7c7SDavid Howells 
241928a6d671SEric W. Biederman 	length = security_setprocattr(task,
24202fddfeefSJosef "Jeff" Sipek 				      (char*)file->f_path.dentry->d_name.name,
242128a6d671SEric W. Biederman 				      (void*)page, count);
24229b1bf12dSKOSAKI Motohiro 	mutex_unlock(&task->signal->cred_guard_mutex);
242328a6d671SEric W. Biederman out_free:
242428a6d671SEric W. Biederman 	free_page((unsigned long) page);
242528a6d671SEric W. Biederman out:
242628a6d671SEric W. Biederman 	put_task_struct(task);
242728a6d671SEric W. Biederman out_no_task:
242828a6d671SEric W. Biederman 	return length;
242928a6d671SEric W. Biederman }
243028a6d671SEric W. Biederman 
243100977a59SArjan van de Ven static const struct file_operations proc_pid_attr_operations = {
243228a6d671SEric W. Biederman 	.read		= proc_pid_attr_read,
243328a6d671SEric W. Biederman 	.write		= proc_pid_attr_write,
243487df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
243528a6d671SEric W. Biederman };
243628a6d671SEric W. Biederman 
2437c5141e6dSEric Dumazet static const struct pid_entry attr_dir_stuff[] = {
2438631f9c18SAlexey Dobriyan 	REG("current",    S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2439631f9c18SAlexey Dobriyan 	REG("prev",       S_IRUGO,	   proc_pid_attr_operations),
2440631f9c18SAlexey Dobriyan 	REG("exec",       S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2441631f9c18SAlexey Dobriyan 	REG("fscreate",   S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2442631f9c18SAlexey Dobriyan 	REG("keycreate",  S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2443631f9c18SAlexey Dobriyan 	REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
244428a6d671SEric W. Biederman };
244528a6d671SEric W. Biederman 
244672d9dcfcSEric W. Biederman static int proc_attr_dir_readdir(struct file * filp,
24471da177e4SLinus Torvalds 			     void * dirent, filldir_t filldir)
24481da177e4SLinus Torvalds {
24491da177e4SLinus Torvalds 	return proc_pident_readdir(filp,dirent,filldir,
245072d9dcfcSEric W. Biederman 				   attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
24511da177e4SLinus Torvalds }
24521da177e4SLinus Torvalds 
245300977a59SArjan van de Ven static const struct file_operations proc_attr_dir_operations = {
24541da177e4SLinus Torvalds 	.read		= generic_read_dir,
245572d9dcfcSEric W. Biederman 	.readdir	= proc_attr_dir_readdir,
24566038f373SArnd Bergmann 	.llseek		= default_llseek,
24571da177e4SLinus Torvalds };
24581da177e4SLinus Torvalds 
245972d9dcfcSEric W. Biederman static struct dentry *proc_attr_dir_lookup(struct inode *dir,
24601da177e4SLinus Torvalds 				struct dentry *dentry, struct nameidata *nd)
24611da177e4SLinus Torvalds {
24627bcd6b0eSEric W. Biederman 	return proc_pident_lookup(dir, dentry,
24637bcd6b0eSEric W. Biederman 				  attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
24641da177e4SLinus Torvalds }
24651da177e4SLinus Torvalds 
2466c5ef1c42SArjan van de Ven static const struct inode_operations proc_attr_dir_inode_operations = {
246772d9dcfcSEric W. Biederman 	.lookup		= proc_attr_dir_lookup,
246899f89551SEric W. Biederman 	.getattr	= pid_getattr,
24696d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
24701da177e4SLinus Torvalds };
24711da177e4SLinus Torvalds 
24721da177e4SLinus Torvalds #endif
24731da177e4SLinus Torvalds 
2474698ba7b5SChristoph Hellwig #ifdef CONFIG_ELF_CORE
24753cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
24763cb4a0bbSKawai, Hidehiro 					 size_t count, loff_t *ppos)
24773cb4a0bbSKawai, Hidehiro {
24783cb4a0bbSKawai, Hidehiro 	struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
24793cb4a0bbSKawai, Hidehiro 	struct mm_struct *mm;
24803cb4a0bbSKawai, Hidehiro 	char buffer[PROC_NUMBUF];
24813cb4a0bbSKawai, Hidehiro 	size_t len;
24823cb4a0bbSKawai, Hidehiro 	int ret;
24833cb4a0bbSKawai, Hidehiro 
24843cb4a0bbSKawai, Hidehiro 	if (!task)
24853cb4a0bbSKawai, Hidehiro 		return -ESRCH;
24863cb4a0bbSKawai, Hidehiro 
24873cb4a0bbSKawai, Hidehiro 	ret = 0;
24883cb4a0bbSKawai, Hidehiro 	mm = get_task_mm(task);
24893cb4a0bbSKawai, Hidehiro 	if (mm) {
24903cb4a0bbSKawai, Hidehiro 		len = snprintf(buffer, sizeof(buffer), "%08lx\n",
24913cb4a0bbSKawai, Hidehiro 			       ((mm->flags & MMF_DUMP_FILTER_MASK) >>
24923cb4a0bbSKawai, Hidehiro 				MMF_DUMP_FILTER_SHIFT));
24933cb4a0bbSKawai, Hidehiro 		mmput(mm);
24943cb4a0bbSKawai, Hidehiro 		ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
24953cb4a0bbSKawai, Hidehiro 	}
24963cb4a0bbSKawai, Hidehiro 
24973cb4a0bbSKawai, Hidehiro 	put_task_struct(task);
24983cb4a0bbSKawai, Hidehiro 
24993cb4a0bbSKawai, Hidehiro 	return ret;
25003cb4a0bbSKawai, Hidehiro }
25013cb4a0bbSKawai, Hidehiro 
25023cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_write(struct file *file,
25033cb4a0bbSKawai, Hidehiro 					  const char __user *buf,
25043cb4a0bbSKawai, Hidehiro 					  size_t count,
25053cb4a0bbSKawai, Hidehiro 					  loff_t *ppos)
25063cb4a0bbSKawai, Hidehiro {
25073cb4a0bbSKawai, Hidehiro 	struct task_struct *task;
25083cb4a0bbSKawai, Hidehiro 	struct mm_struct *mm;
25093cb4a0bbSKawai, Hidehiro 	char buffer[PROC_NUMBUF], *end;
25103cb4a0bbSKawai, Hidehiro 	unsigned int val;
25113cb4a0bbSKawai, Hidehiro 	int ret;
25123cb4a0bbSKawai, Hidehiro 	int i;
25133cb4a0bbSKawai, Hidehiro 	unsigned long mask;
25143cb4a0bbSKawai, Hidehiro 
25153cb4a0bbSKawai, Hidehiro 	ret = -EFAULT;
25163cb4a0bbSKawai, Hidehiro 	memset(buffer, 0, sizeof(buffer));
25173cb4a0bbSKawai, Hidehiro 	if (count > sizeof(buffer) - 1)
25183cb4a0bbSKawai, Hidehiro 		count = sizeof(buffer) - 1;
25193cb4a0bbSKawai, Hidehiro 	if (copy_from_user(buffer, buf, count))
25203cb4a0bbSKawai, Hidehiro 		goto out_no_task;
25213cb4a0bbSKawai, Hidehiro 
25223cb4a0bbSKawai, Hidehiro 	ret = -EINVAL;
25233cb4a0bbSKawai, Hidehiro 	val = (unsigned int)simple_strtoul(buffer, &end, 0);
25243cb4a0bbSKawai, Hidehiro 	if (*end == '\n')
25253cb4a0bbSKawai, Hidehiro 		end++;
25263cb4a0bbSKawai, Hidehiro 	if (end - buffer == 0)
25273cb4a0bbSKawai, Hidehiro 		goto out_no_task;
25283cb4a0bbSKawai, Hidehiro 
25293cb4a0bbSKawai, Hidehiro 	ret = -ESRCH;
25303cb4a0bbSKawai, Hidehiro 	task = get_proc_task(file->f_dentry->d_inode);
25313cb4a0bbSKawai, Hidehiro 	if (!task)
25323cb4a0bbSKawai, Hidehiro 		goto out_no_task;
25333cb4a0bbSKawai, Hidehiro 
25343cb4a0bbSKawai, Hidehiro 	ret = end - buffer;
25353cb4a0bbSKawai, Hidehiro 	mm = get_task_mm(task);
25363cb4a0bbSKawai, Hidehiro 	if (!mm)
25373cb4a0bbSKawai, Hidehiro 		goto out_no_mm;
25383cb4a0bbSKawai, Hidehiro 
25393cb4a0bbSKawai, Hidehiro 	for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
25403cb4a0bbSKawai, Hidehiro 		if (val & mask)
25413cb4a0bbSKawai, Hidehiro 			set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
25423cb4a0bbSKawai, Hidehiro 		else
25433cb4a0bbSKawai, Hidehiro 			clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
25443cb4a0bbSKawai, Hidehiro 	}
25453cb4a0bbSKawai, Hidehiro 
25463cb4a0bbSKawai, Hidehiro 	mmput(mm);
25473cb4a0bbSKawai, Hidehiro  out_no_mm:
25483cb4a0bbSKawai, Hidehiro 	put_task_struct(task);
25493cb4a0bbSKawai, Hidehiro  out_no_task:
25503cb4a0bbSKawai, Hidehiro 	return ret;
25513cb4a0bbSKawai, Hidehiro }
25523cb4a0bbSKawai, Hidehiro 
25533cb4a0bbSKawai, Hidehiro static const struct file_operations proc_coredump_filter_operations = {
25543cb4a0bbSKawai, Hidehiro 	.read		= proc_coredump_filter_read,
25553cb4a0bbSKawai, Hidehiro 	.write		= proc_coredump_filter_write,
255687df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
25573cb4a0bbSKawai, Hidehiro };
25583cb4a0bbSKawai, Hidehiro #endif
25593cb4a0bbSKawai, Hidehiro 
25601da177e4SLinus Torvalds /*
25611da177e4SLinus Torvalds  * /proc/self:
25621da177e4SLinus Torvalds  */
25631da177e4SLinus Torvalds static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
25641da177e4SLinus Torvalds 			      int buflen)
25651da177e4SLinus Torvalds {
2566488e5bc4SEric W. Biederman 	struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2567b55fcb22SAndrew Morton 	pid_t tgid = task_tgid_nr_ns(current, ns);
25688578cea7SEric W. Biederman 	char tmp[PROC_NUMBUF];
2569b55fcb22SAndrew Morton 	if (!tgid)
2570488e5bc4SEric W. Biederman 		return -ENOENT;
2571b55fcb22SAndrew Morton 	sprintf(tmp, "%d", tgid);
25721da177e4SLinus Torvalds 	return vfs_readlink(dentry,buffer,buflen,tmp);
25731da177e4SLinus Torvalds }
25741da177e4SLinus Torvalds 
2575008b150aSAl Viro static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
25761da177e4SLinus Torvalds {
2577488e5bc4SEric W. Biederman 	struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2578b55fcb22SAndrew Morton 	pid_t tgid = task_tgid_nr_ns(current, ns);
25797fee4868SAl Viro 	char *name = ERR_PTR(-ENOENT);
25807fee4868SAl Viro 	if (tgid) {
25817fee4868SAl Viro 		name = __getname();
25827fee4868SAl Viro 		if (!name)
25837fee4868SAl Viro 			name = ERR_PTR(-ENOMEM);
25847fee4868SAl Viro 		else
25857fee4868SAl Viro 			sprintf(name, "%d", tgid);
25867fee4868SAl Viro 	}
25877fee4868SAl Viro 	nd_set_link(nd, name);
25887fee4868SAl Viro 	return NULL;
25897fee4868SAl Viro }
25907fee4868SAl Viro 
25917fee4868SAl Viro static void proc_self_put_link(struct dentry *dentry, struct nameidata *nd,
25927fee4868SAl Viro 				void *cookie)
25937fee4868SAl Viro {
25947fee4868SAl Viro 	char *s = nd_get_link(nd);
25957fee4868SAl Viro 	if (!IS_ERR(s))
25967fee4868SAl Viro 		__putname(s);
25971da177e4SLinus Torvalds }
25981da177e4SLinus Torvalds 
2599c5ef1c42SArjan van de Ven static const struct inode_operations proc_self_inode_operations = {
26001da177e4SLinus Torvalds 	.readlink	= proc_self_readlink,
26011da177e4SLinus Torvalds 	.follow_link	= proc_self_follow_link,
26027fee4868SAl Viro 	.put_link	= proc_self_put_link,
26031da177e4SLinus Torvalds };
26041da177e4SLinus Torvalds 
260528a6d671SEric W. Biederman /*
2606801199ceSEric W. Biederman  * proc base
2607801199ceSEric W. Biederman  *
2608801199ceSEric W. Biederman  * These are the directory entries in the root directory of /proc
2609801199ceSEric W. Biederman  * that properly belong to the /proc filesystem, as they describe
2610801199ceSEric W. Biederman  * describe something that is process related.
2611801199ceSEric W. Biederman  */
2612c5141e6dSEric Dumazet static const struct pid_entry proc_base_stuff[] = {
261361a28784SEric W. Biederman 	NOD("self", S_IFLNK|S_IRWXUGO,
2614801199ceSEric W. Biederman 		&proc_self_inode_operations, NULL, {}),
2615801199ceSEric W. Biederman };
2616801199ceSEric W. Biederman 
2617444ceed8SEric W. Biederman static struct dentry *proc_base_instantiate(struct inode *dir,
2618c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
2619801199ceSEric W. Biederman {
2620c5141e6dSEric Dumazet 	const struct pid_entry *p = ptr;
2621801199ceSEric W. Biederman 	struct inode *inode;
2622801199ceSEric W. Biederman 	struct proc_inode *ei;
262373d36460SDan Carpenter 	struct dentry *error;
2624801199ceSEric W. Biederman 
2625801199ceSEric W. Biederman 	/* Allocate the inode */
2626801199ceSEric W. Biederman 	error = ERR_PTR(-ENOMEM);
2627801199ceSEric W. Biederman 	inode = new_inode(dir->i_sb);
2628801199ceSEric W. Biederman 	if (!inode)
2629801199ceSEric W. Biederman 		goto out;
2630801199ceSEric W. Biederman 
2631801199ceSEric W. Biederman 	/* Initialize the inode */
2632801199ceSEric W. Biederman 	ei = PROC_I(inode);
263385fe4025SChristoph Hellwig 	inode->i_ino = get_next_ino();
2634801199ceSEric W. Biederman 	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2635801199ceSEric W. Biederman 
2636801199ceSEric W. Biederman 	/*
2637801199ceSEric W. Biederman 	 * grab the reference to the task.
2638801199ceSEric W. Biederman 	 */
26391a657f78SOleg Nesterov 	ei->pid = get_task_pid(task, PIDTYPE_PID);
2640801199ceSEric W. Biederman 	if (!ei->pid)
2641801199ceSEric W. Biederman 		goto out_iput;
2642801199ceSEric W. Biederman 
2643801199ceSEric W. Biederman 	inode->i_mode = p->mode;
2644801199ceSEric W. Biederman 	if (S_ISDIR(inode->i_mode))
2645*bfe86848SMiklos Szeredi 		set_nlink(inode, 2);
2646801199ceSEric W. Biederman 	if (S_ISLNK(inode->i_mode))
2647801199ceSEric W. Biederman 		inode->i_size = 64;
2648801199ceSEric W. Biederman 	if (p->iop)
2649801199ceSEric W. Biederman 		inode->i_op = p->iop;
2650801199ceSEric W. Biederman 	if (p->fop)
2651801199ceSEric W. Biederman 		inode->i_fop = p->fop;
2652801199ceSEric W. Biederman 	ei->op = p->op;
2653801199ceSEric W. Biederman 	d_add(dentry, inode);
2654801199ceSEric W. Biederman 	error = NULL;
2655801199ceSEric W. Biederman out:
2656801199ceSEric W. Biederman 	return error;
2657801199ceSEric W. Biederman out_iput:
2658801199ceSEric W. Biederman 	iput(inode);
2659801199ceSEric W. Biederman 	goto out;
2660801199ceSEric W. Biederman }
2661801199ceSEric W. Biederman 
2662444ceed8SEric W. Biederman static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2663444ceed8SEric W. Biederman {
2664444ceed8SEric W. Biederman 	struct dentry *error;
2665444ceed8SEric W. Biederman 	struct task_struct *task = get_proc_task(dir);
2666c5141e6dSEric Dumazet 	const struct pid_entry *p, *last;
2667444ceed8SEric W. Biederman 
2668444ceed8SEric W. Biederman 	error = ERR_PTR(-ENOENT);
2669444ceed8SEric W. Biederman 
2670444ceed8SEric W. Biederman 	if (!task)
2671444ceed8SEric W. Biederman 		goto out_no_task;
2672444ceed8SEric W. Biederman 
2673444ceed8SEric W. Biederman 	/* Lookup the directory entry */
26747bcd6b0eSEric W. Biederman 	last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
26757bcd6b0eSEric W. Biederman 	for (p = proc_base_stuff; p <= last; p++) {
2676444ceed8SEric W. Biederman 		if (p->len != dentry->d_name.len)
2677444ceed8SEric W. Biederman 			continue;
2678444ceed8SEric W. Biederman 		if (!memcmp(dentry->d_name.name, p->name, p->len))
2679444ceed8SEric W. Biederman 			break;
2680444ceed8SEric W. Biederman 	}
26817bcd6b0eSEric W. Biederman 	if (p > last)
2682444ceed8SEric W. Biederman 		goto out;
2683444ceed8SEric W. Biederman 
2684444ceed8SEric W. Biederman 	error = proc_base_instantiate(dir, dentry, task, p);
2685444ceed8SEric W. Biederman 
2686444ceed8SEric W. Biederman out:
2687444ceed8SEric W. Biederman 	put_task_struct(task);
2688444ceed8SEric W. Biederman out_no_task:
2689444ceed8SEric W. Biederman 	return error;
2690444ceed8SEric W. Biederman }
2691444ceed8SEric W. Biederman 
2692c5141e6dSEric Dumazet static int proc_base_fill_cache(struct file *filp, void *dirent,
2693c5141e6dSEric Dumazet 	filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
269461a28784SEric W. Biederman {
269561a28784SEric W. Biederman 	return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
269661a28784SEric W. Biederman 				proc_base_instantiate, task, p);
269761a28784SEric W. Biederman }
269861a28784SEric W. Biederman 
2699aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING
2700297c5d92SAndrea Righi static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
2701aba76fdbSAndrew Morton {
2702940389b8SAndrea Righi 	struct task_io_accounting acct = task->ioac;
2703297c5d92SAndrea Righi 	unsigned long flags;
2704293eb1e7SVasiliy Kulikov 	int result;
2705297c5d92SAndrea Righi 
2706293eb1e7SVasiliy Kulikov 	result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2707293eb1e7SVasiliy Kulikov 	if (result)
2708293eb1e7SVasiliy Kulikov 		return result;
2709293eb1e7SVasiliy Kulikov 
2710293eb1e7SVasiliy Kulikov 	if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
2711293eb1e7SVasiliy Kulikov 		result = -EACCES;
2712293eb1e7SVasiliy Kulikov 		goto out_unlock;
2713293eb1e7SVasiliy Kulikov 	}
27141d1221f3SVasiliy Kulikov 
27155995477aSAndrea Righi 	if (whole && lock_task_sighand(task, &flags)) {
2716b2d002dbSAndrea Righi 		struct task_struct *t = task;
2717297c5d92SAndrea Righi 
27185995477aSAndrea Righi 		task_io_accounting_add(&acct, &task->signal->ioac);
27195995477aSAndrea Righi 		while_each_thread(task, t)
27205995477aSAndrea Righi 			task_io_accounting_add(&acct, &t->ioac);
2721297c5d92SAndrea Righi 
2722297c5d92SAndrea Righi 		unlock_task_sighand(task, &flags);
2723297c5d92SAndrea Righi 	}
2724293eb1e7SVasiliy Kulikov 	result = sprintf(buffer,
2725aba76fdbSAndrew Morton 			"rchar: %llu\n"
2726aba76fdbSAndrew Morton 			"wchar: %llu\n"
2727aba76fdbSAndrew Morton 			"syscr: %llu\n"
2728aba76fdbSAndrew Morton 			"syscw: %llu\n"
2729aba76fdbSAndrew Morton 			"read_bytes: %llu\n"
2730aba76fdbSAndrew Morton 			"write_bytes: %llu\n"
2731aba76fdbSAndrew Morton 			"cancelled_write_bytes: %llu\n",
27327c44319dSAlexander Beregalov 			(unsigned long long)acct.rchar,
27337c44319dSAlexander Beregalov 			(unsigned long long)acct.wchar,
27347c44319dSAlexander Beregalov 			(unsigned long long)acct.syscr,
27357c44319dSAlexander Beregalov 			(unsigned long long)acct.syscw,
27367c44319dSAlexander Beregalov 			(unsigned long long)acct.read_bytes,
27377c44319dSAlexander Beregalov 			(unsigned long long)acct.write_bytes,
27387c44319dSAlexander Beregalov 			(unsigned long long)acct.cancelled_write_bytes);
2739293eb1e7SVasiliy Kulikov out_unlock:
2740293eb1e7SVasiliy Kulikov 	mutex_unlock(&task->signal->cred_guard_mutex);
2741293eb1e7SVasiliy Kulikov 	return result;
2742aba76fdbSAndrew Morton }
2743297c5d92SAndrea Righi 
2744297c5d92SAndrea Righi static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2745297c5d92SAndrea Righi {
2746297c5d92SAndrea Righi 	return do_io_accounting(task, buffer, 0);
2747297c5d92SAndrea Righi }
2748297c5d92SAndrea Righi 
2749297c5d92SAndrea Righi static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2750297c5d92SAndrea Righi {
2751297c5d92SAndrea Righi 	return do_io_accounting(task, buffer, 1);
2752297c5d92SAndrea Righi }
2753297c5d92SAndrea Righi #endif /* CONFIG_TASK_IO_ACCOUNTING */
2754aba76fdbSAndrew Morton 
275547830723SKees Cook static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
275647830723SKees Cook 				struct pid *pid, struct task_struct *task)
275747830723SKees Cook {
2758a9712bc1SAl Viro 	int err = lock_trace(task);
2759a9712bc1SAl Viro 	if (!err) {
276047830723SKees Cook 		seq_printf(m, "%08x\n", task->personality);
2761a9712bc1SAl Viro 		unlock_trace(task);
2762a9712bc1SAl Viro 	}
2763a9712bc1SAl Viro 	return err;
276447830723SKees Cook }
276547830723SKees Cook 
2766801199ceSEric W. Biederman /*
276728a6d671SEric W. Biederman  * Thread groups
276828a6d671SEric W. Biederman  */
276900977a59SArjan van de Ven static const struct file_operations proc_task_operations;
2770c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations;
277120cdc894SEric W. Biederman 
2772c5141e6dSEric Dumazet static const struct pid_entry tgid_base_stuff[] = {
2773631f9c18SAlexey Dobriyan 	DIR("task",       S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2774631f9c18SAlexey Dobriyan 	DIR("fd",         S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2775631f9c18SAlexey Dobriyan 	DIR("fdinfo",     S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
27766b4e306aSEric W. Biederman 	DIR("ns",	  S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
2777b2211a36SAndrew Morton #ifdef CONFIG_NET
2778631f9c18SAlexey Dobriyan 	DIR("net",        S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2779b2211a36SAndrew Morton #endif
2780631f9c18SAlexey Dobriyan 	REG("environ",    S_IRUSR, proc_environ_operations),
2781631f9c18SAlexey Dobriyan 	INF("auxv",       S_IRUSR, proc_pid_auxv),
2782631f9c18SAlexey Dobriyan 	ONE("status",     S_IRUGO, proc_pid_status),
2783a9712bc1SAl Viro 	ONE("personality", S_IRUGO, proc_pid_personality),
27843036e7b4SJiri Olsa 	INF("limits",	  S_IRUGO, proc_pid_limits),
278543ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
2786631f9c18SAlexey Dobriyan 	REG("sched",      S_IRUGO|S_IWUSR, proc_pid_sched_operations),
278743ae34cbSIngo Molnar #endif
27885091faa4SMike Galbraith #ifdef CONFIG_SCHED_AUTOGROUP
27895091faa4SMike Galbraith 	REG("autogroup",  S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
27905091faa4SMike Galbraith #endif
27914614a696Sjohn stultz 	REG("comm",      S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2792ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2793a9712bc1SAl Viro 	INF("syscall",    S_IRUGO, proc_pid_syscall),
2794ebcb6734SRoland McGrath #endif
2795631f9c18SAlexey Dobriyan 	INF("cmdline",    S_IRUGO, proc_pid_cmdline),
2796631f9c18SAlexey Dobriyan 	ONE("stat",       S_IRUGO, proc_tgid_stat),
2797631f9c18SAlexey Dobriyan 	ONE("statm",      S_IRUGO, proc_pid_statm),
2798631f9c18SAlexey Dobriyan 	REG("maps",       S_IRUGO, proc_maps_operations),
279928a6d671SEric W. Biederman #ifdef CONFIG_NUMA
2800631f9c18SAlexey Dobriyan 	REG("numa_maps",  S_IRUGO, proc_numa_maps_operations),
280128a6d671SEric W. Biederman #endif
2802631f9c18SAlexey Dobriyan 	REG("mem",        S_IRUSR|S_IWUSR, proc_mem_operations),
2803631f9c18SAlexey Dobriyan 	LNK("cwd",        proc_cwd_link),
2804631f9c18SAlexey Dobriyan 	LNK("root",       proc_root_link),
2805631f9c18SAlexey Dobriyan 	LNK("exe",        proc_exe_link),
2806631f9c18SAlexey Dobriyan 	REG("mounts",     S_IRUGO, proc_mounts_operations),
2807631f9c18SAlexey Dobriyan 	REG("mountinfo",  S_IRUGO, proc_mountinfo_operations),
2808631f9c18SAlexey Dobriyan 	REG("mountstats", S_IRUSR, proc_mountstats_operations),
28091e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR
2810631f9c18SAlexey Dobriyan 	REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2811631f9c18SAlexey Dobriyan 	REG("smaps",      S_IRUGO, proc_smaps_operations),
2812ca6b0bf0SAl Viro 	REG("pagemap",    S_IRUGO, proc_pagemap_operations),
281328a6d671SEric W. Biederman #endif
281428a6d671SEric W. Biederman #ifdef CONFIG_SECURITY
2815631f9c18SAlexey Dobriyan 	DIR("attr",       S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
281628a6d671SEric W. Biederman #endif
281728a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS
2818631f9c18SAlexey Dobriyan 	INF("wchan",      S_IRUGO, proc_pid_wchan),
281928a6d671SEric W. Biederman #endif
28202ec220e2SKen Chen #ifdef CONFIG_STACKTRACE
2821a9712bc1SAl Viro 	ONE("stack",      S_IRUGO, proc_pid_stack),
282228a6d671SEric W. Biederman #endif
282328a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS
2824631f9c18SAlexey Dobriyan 	INF("schedstat",  S_IRUGO, proc_pid_schedstat),
282528a6d671SEric W. Biederman #endif
28269745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP
2827631f9c18SAlexey Dobriyan 	REG("latency",  S_IRUGO, proc_lstats_operations),
28289745512cSArjan van de Ven #endif
28298793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET
2830631f9c18SAlexey Dobriyan 	REG("cpuset",     S_IRUGO, proc_cpuset_operations),
283128a6d671SEric W. Biederman #endif
2832a424316cSPaul Menage #ifdef CONFIG_CGROUPS
2833631f9c18SAlexey Dobriyan 	REG("cgroup",  S_IRUGO, proc_cgroup_operations),
2834a424316cSPaul Menage #endif
2835631f9c18SAlexey Dobriyan 	INF("oom_score",  S_IRUGO, proc_oom_score),
2836631f9c18SAlexey Dobriyan 	REG("oom_adj",    S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
2837a63d83f4SDavid Rientjes 	REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
283828a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL
2839631f9c18SAlexey Dobriyan 	REG("loginuid",   S_IWUSR|S_IRUGO, proc_loginuid_operations),
2840631f9c18SAlexey Dobriyan 	REG("sessionid",  S_IRUGO, proc_sessionid_operations),
284128a6d671SEric W. Biederman #endif
2842f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
2843631f9c18SAlexey Dobriyan 	REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2844f4f154fdSAkinobu Mita #endif
2845698ba7b5SChristoph Hellwig #ifdef CONFIG_ELF_CORE
2846631f9c18SAlexey Dobriyan 	REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
28473cb4a0bbSKawai, Hidehiro #endif
2848aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING
28491d1221f3SVasiliy Kulikov 	INF("io",	S_IRUSR, proc_tgid_io_accounting),
2850aba76fdbSAndrew Morton #endif
2851f133eccaSChris Metcalf #ifdef CONFIG_HARDWALL
2852f133eccaSChris Metcalf 	INF("hardwall",   S_IRUGO, proc_pid_hardwall),
2853f133eccaSChris Metcalf #endif
285428a6d671SEric W. Biederman };
285528a6d671SEric W. Biederman 
285628a6d671SEric W. Biederman static int proc_tgid_base_readdir(struct file * filp,
285728a6d671SEric W. Biederman 			     void * dirent, filldir_t filldir)
285828a6d671SEric W. Biederman {
285928a6d671SEric W. Biederman 	return proc_pident_readdir(filp,dirent,filldir,
286028a6d671SEric W. Biederman 				   tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
286128a6d671SEric W. Biederman }
286228a6d671SEric W. Biederman 
286300977a59SArjan van de Ven static const struct file_operations proc_tgid_base_operations = {
286428a6d671SEric W. Biederman 	.read		= generic_read_dir,
286528a6d671SEric W. Biederman 	.readdir	= proc_tgid_base_readdir,
28666038f373SArnd Bergmann 	.llseek		= default_llseek,
286728a6d671SEric W. Biederman };
286828a6d671SEric W. Biederman 
286928a6d671SEric W. Biederman static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
28707bcd6b0eSEric W. Biederman 	return proc_pident_lookup(dir, dentry,
28717bcd6b0eSEric W. Biederman 				  tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
287228a6d671SEric W. Biederman }
287328a6d671SEric W. Biederman 
2874c5ef1c42SArjan van de Ven static const struct inode_operations proc_tgid_base_inode_operations = {
287528a6d671SEric W. Biederman 	.lookup		= proc_tgid_base_lookup,
287628a6d671SEric W. Biederman 	.getattr	= pid_getattr,
287728a6d671SEric W. Biederman 	.setattr	= proc_setattr,
287828a6d671SEric W. Biederman };
287928a6d671SEric W. Biederman 
288060347f67SPavel Emelyanov static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
28811da177e4SLinus Torvalds {
288248e6484dSEric W. Biederman 	struct dentry *dentry, *leader, *dir;
28838578cea7SEric W. Biederman 	char buf[PROC_NUMBUF];
288448e6484dSEric W. Biederman 	struct qstr name;
28851da177e4SLinus Torvalds 
288648e6484dSEric W. Biederman 	name.name = buf;
288760347f67SPavel Emelyanov 	name.len = snprintf(buf, sizeof(buf), "%d", pid);
288860347f67SPavel Emelyanov 	dentry = d_hash_and_lookup(mnt->mnt_root, &name);
288948e6484dSEric W. Biederman 	if (dentry) {
289048e6484dSEric W. Biederman 		shrink_dcache_parent(dentry);
289148e6484dSEric W. Biederman 		d_drop(dentry);
289248e6484dSEric W. Biederman 		dput(dentry);
28931da177e4SLinus Torvalds 	}
28941da177e4SLinus Torvalds 
289548e6484dSEric W. Biederman 	name.name = buf;
289660347f67SPavel Emelyanov 	name.len = snprintf(buf, sizeof(buf), "%d", tgid);
289760347f67SPavel Emelyanov 	leader = d_hash_and_lookup(mnt->mnt_root, &name);
289848e6484dSEric W. Biederman 	if (!leader)
289948e6484dSEric W. Biederman 		goto out;
290048e6484dSEric W. Biederman 
290148e6484dSEric W. Biederman 	name.name = "task";
290248e6484dSEric W. Biederman 	name.len = strlen(name.name);
290348e6484dSEric W. Biederman 	dir = d_hash_and_lookup(leader, &name);
290448e6484dSEric W. Biederman 	if (!dir)
290548e6484dSEric W. Biederman 		goto out_put_leader;
290648e6484dSEric W. Biederman 
290748e6484dSEric W. Biederman 	name.name = buf;
290860347f67SPavel Emelyanov 	name.len = snprintf(buf, sizeof(buf), "%d", pid);
290948e6484dSEric W. Biederman 	dentry = d_hash_and_lookup(dir, &name);
291048e6484dSEric W. Biederman 	if (dentry) {
291148e6484dSEric W. Biederman 		shrink_dcache_parent(dentry);
291248e6484dSEric W. Biederman 		d_drop(dentry);
291348e6484dSEric W. Biederman 		dput(dentry);
29141da177e4SLinus Torvalds 	}
291548e6484dSEric W. Biederman 
291648e6484dSEric W. Biederman 	dput(dir);
291748e6484dSEric W. Biederman out_put_leader:
291848e6484dSEric W. Biederman 	dput(leader);
291948e6484dSEric W. Biederman out:
292048e6484dSEric W. Biederman 	return;
29211da177e4SLinus Torvalds }
29221da177e4SLinus Torvalds 
29230895e91dSRandy Dunlap /**
29240895e91dSRandy Dunlap  * proc_flush_task -  Remove dcache entries for @task from the /proc dcache.
29250895e91dSRandy Dunlap  * @task: task that should be flushed.
29260895e91dSRandy Dunlap  *
29270895e91dSRandy Dunlap  * When flushing dentries from proc, one needs to flush them from global
292860347f67SPavel Emelyanov  * proc (proc_mnt) and from all the namespaces' procs this task was seen
29290895e91dSRandy Dunlap  * in. This call is supposed to do all of this job.
29300895e91dSRandy Dunlap  *
29310895e91dSRandy Dunlap  * Looks in the dcache for
29320895e91dSRandy Dunlap  * /proc/@pid
29330895e91dSRandy Dunlap  * /proc/@tgid/task/@pid
29340895e91dSRandy Dunlap  * if either directory is present flushes it and all of it'ts children
29350895e91dSRandy Dunlap  * from the dcache.
29360895e91dSRandy Dunlap  *
29370895e91dSRandy Dunlap  * It is safe and reasonable to cache /proc entries for a task until
29380895e91dSRandy Dunlap  * that task exits.  After that they just clog up the dcache with
29390895e91dSRandy Dunlap  * useless entries, possibly causing useful dcache entries to be
29400895e91dSRandy Dunlap  * flushed instead.  This routine is proved to flush those useless
29410895e91dSRandy Dunlap  * dcache entries at process exit time.
29420895e91dSRandy Dunlap  *
29430895e91dSRandy Dunlap  * NOTE: This routine is just an optimization so it does not guarantee
29440895e91dSRandy Dunlap  *       that no dcache entries will exist at process exit time it
29450895e91dSRandy Dunlap  *       just makes it very unlikely that any will persist.
294660347f67SPavel Emelyanov  */
294760347f67SPavel Emelyanov 
294860347f67SPavel Emelyanov void proc_flush_task(struct task_struct *task)
294960347f67SPavel Emelyanov {
29509fcc2d15SEric W. Biederman 	int i;
29519b4d1cbeSOleg Nesterov 	struct pid *pid, *tgid;
2952130f77ecSPavel Emelyanov 	struct upid *upid;
2953130f77ecSPavel Emelyanov 
2954130f77ecSPavel Emelyanov 	pid = task_pid(task);
2955130f77ecSPavel Emelyanov 	tgid = task_tgid(task);
29569fcc2d15SEric W. Biederman 
29579fcc2d15SEric W. Biederman 	for (i = 0; i <= pid->level; i++) {
2958130f77ecSPavel Emelyanov 		upid = &pid->numbers[i];
2959130f77ecSPavel Emelyanov 		proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
29609b4d1cbeSOleg Nesterov 					tgid->numbers[i].nr);
2961130f77ecSPavel Emelyanov 	}
29626f4e6433SPavel Emelyanov 
29636f4e6433SPavel Emelyanov 	upid = &pid->numbers[pid->level];
29646f4e6433SPavel Emelyanov 	if (upid->nr == 1)
29656f4e6433SPavel Emelyanov 		pid_ns_release_proc(upid->ns);
296660347f67SPavel Emelyanov }
296760347f67SPavel Emelyanov 
29689711ef99SAdrian Bunk static struct dentry *proc_pid_instantiate(struct inode *dir,
29699711ef99SAdrian Bunk 					   struct dentry * dentry,
2970c5141e6dSEric Dumazet 					   struct task_struct *task, const void *ptr)
2971444ceed8SEric W. Biederman {
2972444ceed8SEric W. Biederman 	struct dentry *error = ERR_PTR(-ENOENT);
2973444ceed8SEric W. Biederman 	struct inode *inode;
2974444ceed8SEric W. Biederman 
297561a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
2976444ceed8SEric W. Biederman 	if (!inode)
2977444ceed8SEric W. Biederman 		goto out;
2978444ceed8SEric W. Biederman 
2979444ceed8SEric W. Biederman 	inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2980444ceed8SEric W. Biederman 	inode->i_op = &proc_tgid_base_inode_operations;
2981444ceed8SEric W. Biederman 	inode->i_fop = &proc_tgid_base_operations;
2982444ceed8SEric W. Biederman 	inode->i_flags|=S_IMMUTABLE;
2983aed54175SVegard Nossum 
2984*bfe86848SMiklos Szeredi 	set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
2985*bfe86848SMiklos Szeredi 						  ARRAY_SIZE(tgid_base_stuff)));
2986444ceed8SEric W. Biederman 
2987fb045adbSNick Piggin 	d_set_d_op(dentry, &pid_dentry_operations);
2988444ceed8SEric W. Biederman 
2989444ceed8SEric W. Biederman 	d_add(dentry, inode);
2990444ceed8SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
2991444ceed8SEric W. Biederman 	if (pid_revalidate(dentry, NULL))
2992444ceed8SEric W. Biederman 		error = NULL;
2993444ceed8SEric W. Biederman out:
2994444ceed8SEric W. Biederman 	return error;
2995444ceed8SEric W. Biederman }
2996444ceed8SEric W. Biederman 
29971da177e4SLinus Torvalds struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
29981da177e4SLinus Torvalds {
299973d36460SDan Carpenter 	struct dentry *result;
30001da177e4SLinus Torvalds 	struct task_struct *task;
30011da177e4SLinus Torvalds 	unsigned tgid;
3002b488893aSPavel Emelyanov 	struct pid_namespace *ns;
30031da177e4SLinus Torvalds 
3004801199ceSEric W. Biederman 	result = proc_base_lookup(dir, dentry);
3005801199ceSEric W. Biederman 	if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
3006801199ceSEric W. Biederman 		goto out;
3007801199ceSEric W. Biederman 
30081da177e4SLinus Torvalds 	tgid = name_to_int(dentry);
30091da177e4SLinus Torvalds 	if (tgid == ~0U)
30101da177e4SLinus Torvalds 		goto out;
30111da177e4SLinus Torvalds 
3012b488893aSPavel Emelyanov 	ns = dentry->d_sb->s_fs_info;
3013de758734SEric W. Biederman 	rcu_read_lock();
3014b488893aSPavel Emelyanov 	task = find_task_by_pid_ns(tgid, ns);
30151da177e4SLinus Torvalds 	if (task)
30161da177e4SLinus Torvalds 		get_task_struct(task);
3017de758734SEric W. Biederman 	rcu_read_unlock();
30181da177e4SLinus Torvalds 	if (!task)
30191da177e4SLinus Torvalds 		goto out;
30201da177e4SLinus Torvalds 
3021444ceed8SEric W. Biederman 	result = proc_pid_instantiate(dir, dentry, task, NULL);
302248e6484dSEric W. Biederman 	put_task_struct(task);
30231da177e4SLinus Torvalds out:
3024cd6a3ce9SEric W. Biederman 	return result;
30251da177e4SLinus Torvalds }
30261da177e4SLinus Torvalds 
30271da177e4SLinus Torvalds /*
30280804ef4bSEric W. Biederman  * Find the first task with tgid >= tgid
30290bc58a91SEric W. Biederman  *
30301da177e4SLinus Torvalds  */
303119fd4bb2SEric W. Biederman struct tgid_iter {
303219fd4bb2SEric W. Biederman 	unsigned int tgid;
30330804ef4bSEric W. Biederman 	struct task_struct *task;
303419fd4bb2SEric W. Biederman };
303519fd4bb2SEric W. Biederman static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
303619fd4bb2SEric W. Biederman {
30370804ef4bSEric W. Biederman 	struct pid *pid;
30381da177e4SLinus Torvalds 
303919fd4bb2SEric W. Biederman 	if (iter.task)
304019fd4bb2SEric W. Biederman 		put_task_struct(iter.task);
30410804ef4bSEric W. Biederman 	rcu_read_lock();
30420804ef4bSEric W. Biederman retry:
304319fd4bb2SEric W. Biederman 	iter.task = NULL;
304419fd4bb2SEric W. Biederman 	pid = find_ge_pid(iter.tgid, ns);
30450804ef4bSEric W. Biederman 	if (pid) {
304619fd4bb2SEric W. Biederman 		iter.tgid = pid_nr_ns(pid, ns);
304719fd4bb2SEric W. Biederman 		iter.task = pid_task(pid, PIDTYPE_PID);
30480804ef4bSEric W. Biederman 		/* What we to know is if the pid we have find is the
30490804ef4bSEric W. Biederman 		 * pid of a thread_group_leader.  Testing for task
30500804ef4bSEric W. Biederman 		 * being a thread_group_leader is the obvious thing
30510804ef4bSEric W. Biederman 		 * todo but there is a window when it fails, due to
30520804ef4bSEric W. Biederman 		 * the pid transfer logic in de_thread.
30530804ef4bSEric W. Biederman 		 *
30540804ef4bSEric W. Biederman 		 * So we perform the straight forward test of seeing
30550804ef4bSEric W. Biederman 		 * if the pid we have found is the pid of a thread
30560804ef4bSEric W. Biederman 		 * group leader, and don't worry if the task we have
30570804ef4bSEric W. Biederman 		 * found doesn't happen to be a thread group leader.
30580804ef4bSEric W. Biederman 		 * As we don't care in the case of readdir.
30590bc58a91SEric W. Biederman 		 */
306019fd4bb2SEric W. Biederman 		if (!iter.task || !has_group_leader_pid(iter.task)) {
306119fd4bb2SEric W. Biederman 			iter.tgid += 1;
30620804ef4bSEric W. Biederman 			goto retry;
306319fd4bb2SEric W. Biederman 		}
306419fd4bb2SEric W. Biederman 		get_task_struct(iter.task);
30651da177e4SLinus Torvalds 	}
3066454cc105SEric W. Biederman 	rcu_read_unlock();
306719fd4bb2SEric W. Biederman 	return iter;
30681da177e4SLinus Torvalds }
30691da177e4SLinus Torvalds 
30707bcd6b0eSEric W. Biederman #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
30711da177e4SLinus Torvalds 
307261a28784SEric W. Biederman static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
307319fd4bb2SEric W. Biederman 	struct tgid_iter iter)
307461a28784SEric W. Biederman {
307561a28784SEric W. Biederman 	char name[PROC_NUMBUF];
307619fd4bb2SEric W. Biederman 	int len = snprintf(name, sizeof(name), "%d", iter.tgid);
307761a28784SEric W. Biederman 	return proc_fill_cache(filp, dirent, filldir, name, len,
307819fd4bb2SEric W. Biederman 				proc_pid_instantiate, iter.task, NULL);
307961a28784SEric W. Biederman }
308061a28784SEric W. Biederman 
30811da177e4SLinus Torvalds /* for the /proc/ directory itself, after non-process stuff has been done */
30821da177e4SLinus Torvalds int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
30831da177e4SLinus Torvalds {
3084d8bdc59fSLinus Torvalds 	unsigned int nr;
3085d8bdc59fSLinus Torvalds 	struct task_struct *reaper;
308619fd4bb2SEric W. Biederman 	struct tgid_iter iter;
3087b488893aSPavel Emelyanov 	struct pid_namespace *ns;
30881da177e4SLinus Torvalds 
3089d8bdc59fSLinus Torvalds 	if (filp->f_pos >= PID_MAX_LIMIT + TGID_OFFSET)
3090d8bdc59fSLinus Torvalds 		goto out_no_task;
3091d8bdc59fSLinus Torvalds 	nr = filp->f_pos - FIRST_PROCESS_ENTRY;
3092d8bdc59fSLinus Torvalds 
3093d8bdc59fSLinus Torvalds 	reaper = get_proc_task(filp->f_path.dentry->d_inode);
309461a28784SEric W. Biederman 	if (!reaper)
309561a28784SEric W. Biederman 		goto out_no_task;
309661a28784SEric W. Biederman 
30977bcd6b0eSEric W. Biederman 	for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
3098c5141e6dSEric Dumazet 		const struct pid_entry *p = &proc_base_stuff[nr];
309961a28784SEric W. Biederman 		if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
3100801199ceSEric W. Biederman 			goto out;
31011da177e4SLinus Torvalds 	}
31021da177e4SLinus Torvalds 
3103b488893aSPavel Emelyanov 	ns = filp->f_dentry->d_sb->s_fs_info;
310419fd4bb2SEric W. Biederman 	iter.task = NULL;
310519fd4bb2SEric W. Biederman 	iter.tgid = filp->f_pos - TGID_OFFSET;
310619fd4bb2SEric W. Biederman 	for (iter = next_tgid(ns, iter);
310719fd4bb2SEric W. Biederman 	     iter.task;
310819fd4bb2SEric W. Biederman 	     iter.tgid += 1, iter = next_tgid(ns, iter)) {
310919fd4bb2SEric W. Biederman 		filp->f_pos = iter.tgid + TGID_OFFSET;
311019fd4bb2SEric W. Biederman 		if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
311119fd4bb2SEric W. Biederman 			put_task_struct(iter.task);
31120804ef4bSEric W. Biederman 			goto out;
31131da177e4SLinus Torvalds 		}
31141da177e4SLinus Torvalds 	}
31150804ef4bSEric W. Biederman 	filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
31160804ef4bSEric W. Biederman out:
311761a28784SEric W. Biederman 	put_task_struct(reaper);
311861a28784SEric W. Biederman out_no_task:
31191da177e4SLinus Torvalds 	return 0;
31201da177e4SLinus Torvalds }
31211da177e4SLinus Torvalds 
31220bc58a91SEric W. Biederman /*
312328a6d671SEric W. Biederman  * Tasks
312428a6d671SEric W. Biederman  */
3125c5141e6dSEric Dumazet static const struct pid_entry tid_base_stuff[] = {
3126631f9c18SAlexey Dobriyan 	DIR("fd",        S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
31273835541dSJerome Marchand 	DIR("fdinfo",    S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
31286b4e306aSEric W. Biederman 	DIR("ns",	 S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
3129631f9c18SAlexey Dobriyan 	REG("environ",   S_IRUSR, proc_environ_operations),
3130631f9c18SAlexey Dobriyan 	INF("auxv",      S_IRUSR, proc_pid_auxv),
3131631f9c18SAlexey Dobriyan 	ONE("status",    S_IRUGO, proc_pid_status),
3132a9712bc1SAl Viro 	ONE("personality", S_IRUGO, proc_pid_personality),
31333036e7b4SJiri Olsa 	INF("limits",	 S_IRUGO, proc_pid_limits),
313443ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
3135631f9c18SAlexey Dobriyan 	REG("sched",     S_IRUGO|S_IWUSR, proc_pid_sched_operations),
313643ae34cbSIngo Molnar #endif
31374614a696Sjohn stultz 	REG("comm",      S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
3138ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3139a9712bc1SAl Viro 	INF("syscall",   S_IRUGO, proc_pid_syscall),
3140ebcb6734SRoland McGrath #endif
3141631f9c18SAlexey Dobriyan 	INF("cmdline",   S_IRUGO, proc_pid_cmdline),
3142631f9c18SAlexey Dobriyan 	ONE("stat",      S_IRUGO, proc_tid_stat),
3143631f9c18SAlexey Dobriyan 	ONE("statm",     S_IRUGO, proc_pid_statm),
3144631f9c18SAlexey Dobriyan 	REG("maps",      S_IRUGO, proc_maps_operations),
314528a6d671SEric W. Biederman #ifdef CONFIG_NUMA
3146631f9c18SAlexey Dobriyan 	REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
314728a6d671SEric W. Biederman #endif
3148631f9c18SAlexey Dobriyan 	REG("mem",       S_IRUSR|S_IWUSR, proc_mem_operations),
3149631f9c18SAlexey Dobriyan 	LNK("cwd",       proc_cwd_link),
3150631f9c18SAlexey Dobriyan 	LNK("root",      proc_root_link),
3151631f9c18SAlexey Dobriyan 	LNK("exe",       proc_exe_link),
3152631f9c18SAlexey Dobriyan 	REG("mounts",    S_IRUGO, proc_mounts_operations),
3153631f9c18SAlexey Dobriyan 	REG("mountinfo",  S_IRUGO, proc_mountinfo_operations),
31541e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR
3155631f9c18SAlexey Dobriyan 	REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3156631f9c18SAlexey Dobriyan 	REG("smaps",     S_IRUGO, proc_smaps_operations),
3157ca6b0bf0SAl Viro 	REG("pagemap",    S_IRUGO, proc_pagemap_operations),
315828a6d671SEric W. Biederman #endif
315928a6d671SEric W. Biederman #ifdef CONFIG_SECURITY
3160631f9c18SAlexey Dobriyan 	DIR("attr",      S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
316128a6d671SEric W. Biederman #endif
316228a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS
3163631f9c18SAlexey Dobriyan 	INF("wchan",     S_IRUGO, proc_pid_wchan),
316428a6d671SEric W. Biederman #endif
31652ec220e2SKen Chen #ifdef CONFIG_STACKTRACE
3166a9712bc1SAl Viro 	ONE("stack",      S_IRUGO, proc_pid_stack),
316728a6d671SEric W. Biederman #endif
316828a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS
3169631f9c18SAlexey Dobriyan 	INF("schedstat", S_IRUGO, proc_pid_schedstat),
317028a6d671SEric W. Biederman #endif
31719745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP
3172631f9c18SAlexey Dobriyan 	REG("latency",  S_IRUGO, proc_lstats_operations),
31739745512cSArjan van de Ven #endif
31748793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET
3175631f9c18SAlexey Dobriyan 	REG("cpuset",    S_IRUGO, proc_cpuset_operations),
317628a6d671SEric W. Biederman #endif
3177a424316cSPaul Menage #ifdef CONFIG_CGROUPS
3178631f9c18SAlexey Dobriyan 	REG("cgroup",  S_IRUGO, proc_cgroup_operations),
3179a424316cSPaul Menage #endif
3180631f9c18SAlexey Dobriyan 	INF("oom_score", S_IRUGO, proc_oom_score),
3181631f9c18SAlexey Dobriyan 	REG("oom_adj",   S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
3182a63d83f4SDavid Rientjes 	REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
318328a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL
3184631f9c18SAlexey Dobriyan 	REG("loginuid",  S_IWUSR|S_IRUGO, proc_loginuid_operations),
318526ec3c64SAl Viro 	REG("sessionid",  S_IRUGO, proc_sessionid_operations),
318628a6d671SEric W. Biederman #endif
3187f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
3188631f9c18SAlexey Dobriyan 	REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3189f4f154fdSAkinobu Mita #endif
3190297c5d92SAndrea Righi #ifdef CONFIG_TASK_IO_ACCOUNTING
31911d1221f3SVasiliy Kulikov 	INF("io",	S_IRUSR, proc_tid_io_accounting),
3192297c5d92SAndrea Righi #endif
3193f133eccaSChris Metcalf #ifdef CONFIG_HARDWALL
3194f133eccaSChris Metcalf 	INF("hardwall",   S_IRUGO, proc_pid_hardwall),
3195f133eccaSChris Metcalf #endif
319628a6d671SEric W. Biederman };
319728a6d671SEric W. Biederman 
319828a6d671SEric W. Biederman static int proc_tid_base_readdir(struct file * filp,
319928a6d671SEric W. Biederman 			     void * dirent, filldir_t filldir)
320028a6d671SEric W. Biederman {
320128a6d671SEric W. Biederman 	return proc_pident_readdir(filp,dirent,filldir,
320228a6d671SEric W. Biederman 				   tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
320328a6d671SEric W. Biederman }
320428a6d671SEric W. Biederman 
320528a6d671SEric W. Biederman static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
32067bcd6b0eSEric W. Biederman 	return proc_pident_lookup(dir, dentry,
32077bcd6b0eSEric W. Biederman 				  tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
320828a6d671SEric W. Biederman }
320928a6d671SEric W. Biederman 
321000977a59SArjan van de Ven static const struct file_operations proc_tid_base_operations = {
321128a6d671SEric W. Biederman 	.read		= generic_read_dir,
321228a6d671SEric W. Biederman 	.readdir	= proc_tid_base_readdir,
32136038f373SArnd Bergmann 	.llseek		= default_llseek,
321428a6d671SEric W. Biederman };
321528a6d671SEric W. Biederman 
3216c5ef1c42SArjan van de Ven static const struct inode_operations proc_tid_base_inode_operations = {
321728a6d671SEric W. Biederman 	.lookup		= proc_tid_base_lookup,
321828a6d671SEric W. Biederman 	.getattr	= pid_getattr,
321928a6d671SEric W. Biederman 	.setattr	= proc_setattr,
322028a6d671SEric W. Biederman };
322128a6d671SEric W. Biederman 
3222444ceed8SEric W. Biederman static struct dentry *proc_task_instantiate(struct inode *dir,
3223c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
3224444ceed8SEric W. Biederman {
3225444ceed8SEric W. Biederman 	struct dentry *error = ERR_PTR(-ENOENT);
3226444ceed8SEric W. Biederman 	struct inode *inode;
322761a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
3228444ceed8SEric W. Biederman 
3229444ceed8SEric W. Biederman 	if (!inode)
3230444ceed8SEric W. Biederman 		goto out;
3231444ceed8SEric W. Biederman 	inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3232444ceed8SEric W. Biederman 	inode->i_op = &proc_tid_base_inode_operations;
3233444ceed8SEric W. Biederman 	inode->i_fop = &proc_tid_base_operations;
3234444ceed8SEric W. Biederman 	inode->i_flags|=S_IMMUTABLE;
3235aed54175SVegard Nossum 
3236*bfe86848SMiklos Szeredi 	set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3237*bfe86848SMiklos Szeredi 						  ARRAY_SIZE(tid_base_stuff)));
3238444ceed8SEric W. Biederman 
3239fb045adbSNick Piggin 	d_set_d_op(dentry, &pid_dentry_operations);
3240444ceed8SEric W. Biederman 
3241444ceed8SEric W. Biederman 	d_add(dentry, inode);
3242444ceed8SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
3243444ceed8SEric W. Biederman 	if (pid_revalidate(dentry, NULL))
3244444ceed8SEric W. Biederman 		error = NULL;
3245444ceed8SEric W. Biederman out:
3246444ceed8SEric W. Biederman 	return error;
3247444ceed8SEric W. Biederman }
3248444ceed8SEric W. Biederman 
324928a6d671SEric W. Biederman static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
325028a6d671SEric W. Biederman {
325128a6d671SEric W. Biederman 	struct dentry *result = ERR_PTR(-ENOENT);
325228a6d671SEric W. Biederman 	struct task_struct *task;
325328a6d671SEric W. Biederman 	struct task_struct *leader = get_proc_task(dir);
325428a6d671SEric W. Biederman 	unsigned tid;
3255b488893aSPavel Emelyanov 	struct pid_namespace *ns;
325628a6d671SEric W. Biederman 
325728a6d671SEric W. Biederman 	if (!leader)
325828a6d671SEric W. Biederman 		goto out_no_task;
325928a6d671SEric W. Biederman 
326028a6d671SEric W. Biederman 	tid = name_to_int(dentry);
326128a6d671SEric W. Biederman 	if (tid == ~0U)
326228a6d671SEric W. Biederman 		goto out;
326328a6d671SEric W. Biederman 
3264b488893aSPavel Emelyanov 	ns = dentry->d_sb->s_fs_info;
326528a6d671SEric W. Biederman 	rcu_read_lock();
3266b488893aSPavel Emelyanov 	task = find_task_by_pid_ns(tid, ns);
326728a6d671SEric W. Biederman 	if (task)
326828a6d671SEric W. Biederman 		get_task_struct(task);
326928a6d671SEric W. Biederman 	rcu_read_unlock();
327028a6d671SEric W. Biederman 	if (!task)
327128a6d671SEric W. Biederman 		goto out;
3272bac0abd6SPavel Emelyanov 	if (!same_thread_group(leader, task))
327328a6d671SEric W. Biederman 		goto out_drop_task;
327428a6d671SEric W. Biederman 
3275444ceed8SEric W. Biederman 	result = proc_task_instantiate(dir, dentry, task, NULL);
327628a6d671SEric W. Biederman out_drop_task:
327728a6d671SEric W. Biederman 	put_task_struct(task);
327828a6d671SEric W. Biederman out:
327928a6d671SEric W. Biederman 	put_task_struct(leader);
328028a6d671SEric W. Biederman out_no_task:
328128a6d671SEric W. Biederman 	return result;
328228a6d671SEric W. Biederman }
328328a6d671SEric W. Biederman 
328428a6d671SEric W. Biederman /*
32850bc58a91SEric W. Biederman  * Find the first tid of a thread group to return to user space.
32860bc58a91SEric W. Biederman  *
32870bc58a91SEric W. Biederman  * Usually this is just the thread group leader, but if the users
32880bc58a91SEric W. Biederman  * buffer was too small or there was a seek into the middle of the
32890bc58a91SEric W. Biederman  * directory we have more work todo.
32900bc58a91SEric W. Biederman  *
32910bc58a91SEric W. Biederman  * In the case of a short read we start with find_task_by_pid.
32920bc58a91SEric W. Biederman  *
32930bc58a91SEric W. Biederman  * In the case of a seek we start with the leader and walk nr
32940bc58a91SEric W. Biederman  * threads past it.
32950bc58a91SEric W. Biederman  */
3296cc288738SEric W. Biederman static struct task_struct *first_tid(struct task_struct *leader,
3297b488893aSPavel Emelyanov 		int tid, int nr, struct pid_namespace *ns)
32980bc58a91SEric W. Biederman {
3299a872ff0cSOleg Nesterov 	struct task_struct *pos;
33000bc58a91SEric W. Biederman 
3301cc288738SEric W. Biederman 	rcu_read_lock();
33020bc58a91SEric W. Biederman 	/* Attempt to start with the pid of a thread */
33030bc58a91SEric W. Biederman 	if (tid && (nr > 0)) {
3304b488893aSPavel Emelyanov 		pos = find_task_by_pid_ns(tid, ns);
3305a872ff0cSOleg Nesterov 		if (pos && (pos->group_leader == leader))
3306a872ff0cSOleg Nesterov 			goto found;
33070bc58a91SEric W. Biederman 	}
33080bc58a91SEric W. Biederman 
33090bc58a91SEric W. Biederman 	/* If nr exceeds the number of threads there is nothing todo */
33100bc58a91SEric W. Biederman 	pos = NULL;
3311a872ff0cSOleg Nesterov 	if (nr && nr >= get_nr_threads(leader))
3312a872ff0cSOleg Nesterov 		goto out;
3313a872ff0cSOleg Nesterov 
3314a872ff0cSOleg Nesterov 	/* If we haven't found our starting place yet start
3315a872ff0cSOleg Nesterov 	 * with the leader and walk nr threads forward.
3316a872ff0cSOleg Nesterov 	 */
3317a872ff0cSOleg Nesterov 	for (pos = leader; nr > 0; --nr) {
3318a872ff0cSOleg Nesterov 		pos = next_thread(pos);
3319a872ff0cSOleg Nesterov 		if (pos == leader) {
3320a872ff0cSOleg Nesterov 			pos = NULL;
3321a872ff0cSOleg Nesterov 			goto out;
3322a872ff0cSOleg Nesterov 		}
3323a872ff0cSOleg Nesterov 	}
3324a872ff0cSOleg Nesterov found:
3325a872ff0cSOleg Nesterov 	get_task_struct(pos);
3326a872ff0cSOleg Nesterov out:
3327cc288738SEric W. Biederman 	rcu_read_unlock();
33280bc58a91SEric W. Biederman 	return pos;
33290bc58a91SEric W. Biederman }
33300bc58a91SEric W. Biederman 
33310bc58a91SEric W. Biederman /*
33320bc58a91SEric W. Biederman  * Find the next thread in the thread list.
33330bc58a91SEric W. Biederman  * Return NULL if there is an error or no next thread.
33340bc58a91SEric W. Biederman  *
33350bc58a91SEric W. Biederman  * The reference to the input task_struct is released.
33360bc58a91SEric W. Biederman  */
33370bc58a91SEric W. Biederman static struct task_struct *next_tid(struct task_struct *start)
33380bc58a91SEric W. Biederman {
3339c1df7fb8SOleg Nesterov 	struct task_struct *pos = NULL;
3340cc288738SEric W. Biederman 	rcu_read_lock();
3341c1df7fb8SOleg Nesterov 	if (pid_alive(start)) {
33420bc58a91SEric W. Biederman 		pos = next_thread(start);
3343c1df7fb8SOleg Nesterov 		if (thread_group_leader(pos))
33440bc58a91SEric W. Biederman 			pos = NULL;
3345c1df7fb8SOleg Nesterov 		else
3346c1df7fb8SOleg Nesterov 			get_task_struct(pos);
3347c1df7fb8SOleg Nesterov 	}
3348cc288738SEric W. Biederman 	rcu_read_unlock();
33490bc58a91SEric W. Biederman 	put_task_struct(start);
33500bc58a91SEric W. Biederman 	return pos;
33510bc58a91SEric W. Biederman }
33520bc58a91SEric W. Biederman 
335361a28784SEric W. Biederman static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
335461a28784SEric W. Biederman 	struct task_struct *task, int tid)
335561a28784SEric W. Biederman {
335661a28784SEric W. Biederman 	char name[PROC_NUMBUF];
335761a28784SEric W. Biederman 	int len = snprintf(name, sizeof(name), "%d", tid);
335861a28784SEric W. Biederman 	return proc_fill_cache(filp, dirent, filldir, name, len,
335961a28784SEric W. Biederman 				proc_task_instantiate, task, NULL);
336061a28784SEric W. Biederman }
336161a28784SEric W. Biederman 
33621da177e4SLinus Torvalds /* for the /proc/TGID/task/ directories */
33631da177e4SLinus Torvalds static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
33641da177e4SLinus Torvalds {
33652fddfeefSJosef "Jeff" Sipek 	struct dentry *dentry = filp->f_path.dentry;
33661da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
33677d895244SGuillaume Chazarain 	struct task_struct *leader = NULL;
33680bc58a91SEric W. Biederman 	struct task_struct *task;
33691da177e4SLinus Torvalds 	int retval = -ENOENT;
33701da177e4SLinus Torvalds 	ino_t ino;
33710bc58a91SEric W. Biederman 	int tid;
3372b488893aSPavel Emelyanov 	struct pid_namespace *ns;
33731da177e4SLinus Torvalds 
33747d895244SGuillaume Chazarain 	task = get_proc_task(inode);
33757d895244SGuillaume Chazarain 	if (!task)
33767d895244SGuillaume Chazarain 		goto out_no_task;
33777d895244SGuillaume Chazarain 	rcu_read_lock();
33787d895244SGuillaume Chazarain 	if (pid_alive(task)) {
33797d895244SGuillaume Chazarain 		leader = task->group_leader;
33807d895244SGuillaume Chazarain 		get_task_struct(leader);
33817d895244SGuillaume Chazarain 	}
33827d895244SGuillaume Chazarain 	rcu_read_unlock();
33837d895244SGuillaume Chazarain 	put_task_struct(task);
338499f89551SEric W. Biederman 	if (!leader)
338599f89551SEric W. Biederman 		goto out_no_task;
33861da177e4SLinus Torvalds 	retval = 0;
33871da177e4SLinus Torvalds 
3388ee568b25SLinus Torvalds 	switch ((unsigned long)filp->f_pos) {
33891da177e4SLinus Torvalds 	case 0:
33901da177e4SLinus Torvalds 		ino = inode->i_ino;
3391ee6f779bSZhang Le 		if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) < 0)
33921da177e4SLinus Torvalds 			goto out;
3393ee6f779bSZhang Le 		filp->f_pos++;
33941da177e4SLinus Torvalds 		/* fall through */
33951da177e4SLinus Torvalds 	case 1:
33961da177e4SLinus Torvalds 		ino = parent_ino(dentry);
3397ee6f779bSZhang Le 		if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) < 0)
33981da177e4SLinus Torvalds 			goto out;
3399ee6f779bSZhang Le 		filp->f_pos++;
34001da177e4SLinus Torvalds 		/* fall through */
34011da177e4SLinus Torvalds 	}
34021da177e4SLinus Torvalds 
34030bc58a91SEric W. Biederman 	/* f_version caches the tgid value that the last readdir call couldn't
34040bc58a91SEric W. Biederman 	 * return. lseek aka telldir automagically resets f_version to 0.
34050bc58a91SEric W. Biederman 	 */
3406b488893aSPavel Emelyanov 	ns = filp->f_dentry->d_sb->s_fs_info;
34072b47c361SMathieu Desnoyers 	tid = (int)filp->f_version;
34080bc58a91SEric W. Biederman 	filp->f_version = 0;
3409ee6f779bSZhang Le 	for (task = first_tid(leader, tid, filp->f_pos - 2, ns);
34100bc58a91SEric W. Biederman 	     task;
3411ee6f779bSZhang Le 	     task = next_tid(task), filp->f_pos++) {
3412b488893aSPavel Emelyanov 		tid = task_pid_nr_ns(task, ns);
341361a28784SEric W. Biederman 		if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
34140bc58a91SEric W. Biederman 			/* returning this tgid failed, save it as the first
34150bc58a91SEric W. Biederman 			 * pid for the next readir call */
34162b47c361SMathieu Desnoyers 			filp->f_version = (u64)tid;
34170bc58a91SEric W. Biederman 			put_task_struct(task);
34181da177e4SLinus Torvalds 			break;
34190bc58a91SEric W. Biederman 		}
34201da177e4SLinus Torvalds 	}
34211da177e4SLinus Torvalds out:
342299f89551SEric W. Biederman 	put_task_struct(leader);
342399f89551SEric W. Biederman out_no_task:
34241da177e4SLinus Torvalds 	return retval;
34251da177e4SLinus Torvalds }
34266e66b52bSEric W. Biederman 
34276e66b52bSEric W. Biederman static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
34286e66b52bSEric W. Biederman {
34296e66b52bSEric W. Biederman 	struct inode *inode = dentry->d_inode;
343099f89551SEric W. Biederman 	struct task_struct *p = get_proc_task(inode);
34316e66b52bSEric W. Biederman 	generic_fillattr(inode, stat);
34326e66b52bSEric W. Biederman 
343399f89551SEric W. Biederman 	if (p) {
343499f89551SEric W. Biederman 		stat->nlink += get_nr_threads(p);
343599f89551SEric W. Biederman 		put_task_struct(p);
34366e66b52bSEric W. Biederman 	}
34376e66b52bSEric W. Biederman 
34386e66b52bSEric W. Biederman 	return 0;
34396e66b52bSEric W. Biederman }
344028a6d671SEric W. Biederman 
3441c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations = {
344228a6d671SEric W. Biederman 	.lookup		= proc_task_lookup,
344328a6d671SEric W. Biederman 	.getattr	= proc_task_getattr,
344428a6d671SEric W. Biederman 	.setattr	= proc_setattr,
344528a6d671SEric W. Biederman };
344628a6d671SEric W. Biederman 
344700977a59SArjan van de Ven static const struct file_operations proc_task_operations = {
344828a6d671SEric W. Biederman 	.read		= generic_read_dir,
344928a6d671SEric W. Biederman 	.readdir	= proc_task_readdir,
34506038f373SArnd Bergmann 	.llseek		= default_llseek,
345128a6d671SEric W. Biederman };
3452