xref: /linux/fs/proc/base.c (revision 3b64b1881143ce9e461c211cc81acc72d0cdc476)
1 /*
2  *  linux/fs/proc/base.c
3  *
4  *  Copyright (C) 1991, 1992 Linus Torvalds
5  *
6  *  proc base directory handling functions
7  *
8  *  1999, Al Viro. Rewritten. Now it covers the whole per-process part.
9  *  Instead of using magical inumbers to determine the kind of object
10  *  we allocate and fill in-core inodes upon lookup. They don't even
11  *  go into icache. We cache the reference to task_struct upon lookup too.
12  *  Eventually it should become a filesystem in its own. We don't use the
13  *  rest of procfs anymore.
14  *
15  *
16  *  Changelog:
17  *  17-Jan-2005
18  *  Allan Bezerra
19  *  Bruna Moreira <bruna.moreira@indt.org.br>
20  *  Edjard Mota <edjard.mota@indt.org.br>
21  *  Ilias Biris <ilias.biris@indt.org.br>
22  *  Mauricio Lin <mauricio.lin@indt.org.br>
23  *
24  *  Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
25  *
26  *  A new process specific entry (smaps) included in /proc. It shows the
27  *  size of rss for each memory area. The maps entry lacks information
28  *  about physical memory size (rss) for each mapped file, i.e.,
29  *  rss information for executables and library files.
30  *  This additional information is useful for any tools that need to know
31  *  about physical memory consumption for a process specific library.
32  *
33  *  Changelog:
34  *  21-Feb-2005
35  *  Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36  *  Pud inclusion in the page table walking.
37  *
38  *  ChangeLog:
39  *  10-Mar-2005
40  *  10LE Instituto Nokia de Tecnologia - INdT:
41  *  A better way to walks through the page table as suggested by Hugh Dickins.
42  *
43  *  Simo Piiroinen <simo.piiroinen@nokia.com>:
44  *  Smaps information related to shared, private, clean and dirty pages.
45  *
46  *  Paul Mundt <paul.mundt@nokia.com>:
47  *  Overall revision about smaps.
48  */
49 
50 #include <asm/uaccess.h>
51 
52 #include <linux/errno.h>
53 #include <linux/time.h>
54 #include <linux/proc_fs.h>
55 #include <linux/stat.h>
56 #include <linux/task_io_accounting_ops.h>
57 #include <linux/init.h>
58 #include <linux/capability.h>
59 #include <linux/file.h>
60 #include <linux/fdtable.h>
61 #include <linux/string.h>
62 #include <linux/seq_file.h>
63 #include <linux/namei.h>
64 #include <linux/mnt_namespace.h>
65 #include <linux/mm.h>
66 #include <linux/swap.h>
67 #include <linux/rcupdate.h>
68 #include <linux/kallsyms.h>
69 #include <linux/stacktrace.h>
70 #include <linux/resource.h>
71 #include <linux/module.h>
72 #include <linux/mount.h>
73 #include <linux/security.h>
74 #include <linux/ptrace.h>
75 #include <linux/tracehook.h>
76 #include <linux/cgroup.h>
77 #include <linux/cpuset.h>
78 #include <linux/audit.h>
79 #include <linux/poll.h>
80 #include <linux/nsproxy.h>
81 #include <linux/oom.h>
82 #include <linux/elf.h>
83 #include <linux/pid_namespace.h>
84 #include <linux/user_namespace.h>
85 #include <linux/fs_struct.h>
86 #include <linux/slab.h>
87 #include <linux/flex_array.h>
88 #ifdef CONFIG_HARDWALL
89 #include <asm/hardwall.h>
90 #endif
91 #include <trace/events/oom.h>
92 #include "internal.h"
93 #include "fd.h"
94 
95 /* NOTE:
96  *	Implementing inode permission operations in /proc is almost
97  *	certainly an error.  Permission checks need to happen during
98  *	each system call not at open time.  The reason is that most of
99  *	what we wish to check for permissions in /proc varies at runtime.
100  *
101  *	The classic example of a problem is opening file descriptors
102  *	in /proc for a task before it execs a suid executable.
103  */
104 
105 struct pid_entry {
106 	char *name;
107 	int len;
108 	umode_t mode;
109 	const struct inode_operations *iop;
110 	const struct file_operations *fop;
111 	union proc_op op;
112 };
113 
114 #define NOD(NAME, MODE, IOP, FOP, OP) {			\
115 	.name = (NAME),					\
116 	.len  = sizeof(NAME) - 1,			\
117 	.mode = MODE,					\
118 	.iop  = IOP,					\
119 	.fop  = FOP,					\
120 	.op   = OP,					\
121 }
122 
123 #define DIR(NAME, MODE, iops, fops)	\
124 	NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
125 #define LNK(NAME, get_link)					\
126 	NOD(NAME, (S_IFLNK|S_IRWXUGO),				\
127 		&proc_pid_link_inode_operations, NULL,		\
128 		{ .proc_get_link = get_link } )
129 #define REG(NAME, MODE, fops)				\
130 	NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
131 #define INF(NAME, MODE, read)				\
132 	NOD(NAME, (S_IFREG|(MODE)), 			\
133 		NULL, &proc_info_file_operations,	\
134 		{ .proc_read = read } )
135 #define ONE(NAME, MODE, show)				\
136 	NOD(NAME, (S_IFREG|(MODE)), 			\
137 		NULL, &proc_single_file_operations,	\
138 		{ .proc_show = show } )
139 
140 /*
141  * Count the number of hardlinks for the pid_entry table, excluding the .
142  * and .. links.
143  */
144 static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
145 	unsigned int n)
146 {
147 	unsigned int i;
148 	unsigned int count;
149 
150 	count = 0;
151 	for (i = 0; i < n; ++i) {
152 		if (S_ISDIR(entries[i].mode))
153 			++count;
154 	}
155 
156 	return count;
157 }
158 
159 static int get_task_root(struct task_struct *task, struct path *root)
160 {
161 	int result = -ENOENT;
162 
163 	task_lock(task);
164 	if (task->fs) {
165 		get_fs_root(task->fs, root);
166 		result = 0;
167 	}
168 	task_unlock(task);
169 	return result;
170 }
171 
172 static int proc_cwd_link(struct dentry *dentry, struct path *path)
173 {
174 	struct task_struct *task = get_proc_task(dentry->d_inode);
175 	int result = -ENOENT;
176 
177 	if (task) {
178 		task_lock(task);
179 		if (task->fs) {
180 			get_fs_pwd(task->fs, path);
181 			result = 0;
182 		}
183 		task_unlock(task);
184 		put_task_struct(task);
185 	}
186 	return result;
187 }
188 
189 static int proc_root_link(struct dentry *dentry, struct path *path)
190 {
191 	struct task_struct *task = get_proc_task(dentry->d_inode);
192 	int result = -ENOENT;
193 
194 	if (task) {
195 		result = get_task_root(task, path);
196 		put_task_struct(task);
197 	}
198 	return result;
199 }
200 
201 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
202 {
203 	int res = 0;
204 	unsigned int len;
205 	struct mm_struct *mm = get_task_mm(task);
206 	if (!mm)
207 		goto out;
208 	if (!mm->arg_end)
209 		goto out_mm;	/* Shh! No looking before we're done */
210 
211  	len = mm->arg_end - mm->arg_start;
212 
213 	if (len > PAGE_SIZE)
214 		len = PAGE_SIZE;
215 
216 	res = access_process_vm(task, mm->arg_start, buffer, len, 0);
217 
218 	// If the nul at the end of args has been overwritten, then
219 	// assume application is using setproctitle(3).
220 	if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
221 		len = strnlen(buffer, res);
222 		if (len < res) {
223 		    res = len;
224 		} else {
225 			len = mm->env_end - mm->env_start;
226 			if (len > PAGE_SIZE - res)
227 				len = PAGE_SIZE - res;
228 			res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
229 			res = strnlen(buffer, res);
230 		}
231 	}
232 out_mm:
233 	mmput(mm);
234 out:
235 	return res;
236 }
237 
238 static int proc_pid_auxv(struct task_struct *task, char *buffer)
239 {
240 	struct mm_struct *mm = mm_access(task, PTRACE_MODE_READ);
241 	int res = PTR_ERR(mm);
242 	if (mm && !IS_ERR(mm)) {
243 		unsigned int nwords = 0;
244 		do {
245 			nwords += 2;
246 		} while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
247 		res = nwords * sizeof(mm->saved_auxv[0]);
248 		if (res > PAGE_SIZE)
249 			res = PAGE_SIZE;
250 		memcpy(buffer, mm->saved_auxv, res);
251 		mmput(mm);
252 	}
253 	return res;
254 }
255 
256 
257 #ifdef CONFIG_KALLSYMS
258 /*
259  * Provides a wchan file via kallsyms in a proper one-value-per-file format.
260  * Returns the resolved symbol.  If that fails, simply return the address.
261  */
262 static int proc_pid_wchan(struct task_struct *task, char *buffer)
263 {
264 	unsigned long wchan;
265 	char symname[KSYM_NAME_LEN];
266 
267 	wchan = get_wchan(task);
268 
269 	if (lookup_symbol_name(wchan, symname) < 0)
270 		if (!ptrace_may_access(task, PTRACE_MODE_READ))
271 			return 0;
272 		else
273 			return sprintf(buffer, "%lu", wchan);
274 	else
275 		return sprintf(buffer, "%s", symname);
276 }
277 #endif /* CONFIG_KALLSYMS */
278 
279 static int lock_trace(struct task_struct *task)
280 {
281 	int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
282 	if (err)
283 		return err;
284 	if (!ptrace_may_access(task, PTRACE_MODE_ATTACH)) {
285 		mutex_unlock(&task->signal->cred_guard_mutex);
286 		return -EPERM;
287 	}
288 	return 0;
289 }
290 
291 static void unlock_trace(struct task_struct *task)
292 {
293 	mutex_unlock(&task->signal->cred_guard_mutex);
294 }
295 
296 #ifdef CONFIG_STACKTRACE
297 
298 #define MAX_STACK_TRACE_DEPTH	64
299 
300 static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
301 			  struct pid *pid, struct task_struct *task)
302 {
303 	struct stack_trace trace;
304 	unsigned long *entries;
305 	int err;
306 	int i;
307 
308 	entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
309 	if (!entries)
310 		return -ENOMEM;
311 
312 	trace.nr_entries	= 0;
313 	trace.max_entries	= MAX_STACK_TRACE_DEPTH;
314 	trace.entries		= entries;
315 	trace.skip		= 0;
316 
317 	err = lock_trace(task);
318 	if (!err) {
319 		save_stack_trace_tsk(task, &trace);
320 
321 		for (i = 0; i < trace.nr_entries; i++) {
322 			seq_printf(m, "[<%pK>] %pS\n",
323 				   (void *)entries[i], (void *)entries[i]);
324 		}
325 		unlock_trace(task);
326 	}
327 	kfree(entries);
328 
329 	return err;
330 }
331 #endif
332 
333 #ifdef CONFIG_SCHEDSTATS
334 /*
335  * Provides /proc/PID/schedstat
336  */
337 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
338 {
339 	return sprintf(buffer, "%llu %llu %lu\n",
340 			(unsigned long long)task->se.sum_exec_runtime,
341 			(unsigned long long)task->sched_info.run_delay,
342 			task->sched_info.pcount);
343 }
344 #endif
345 
346 #ifdef CONFIG_LATENCYTOP
347 static int lstats_show_proc(struct seq_file *m, void *v)
348 {
349 	int i;
350 	struct inode *inode = m->private;
351 	struct task_struct *task = get_proc_task(inode);
352 
353 	if (!task)
354 		return -ESRCH;
355 	seq_puts(m, "Latency Top version : v0.1\n");
356 	for (i = 0; i < 32; i++) {
357 		struct latency_record *lr = &task->latency_record[i];
358 		if (lr->backtrace[0]) {
359 			int q;
360 			seq_printf(m, "%i %li %li",
361 				   lr->count, lr->time, lr->max);
362 			for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
363 				unsigned long bt = lr->backtrace[q];
364 				if (!bt)
365 					break;
366 				if (bt == ULONG_MAX)
367 					break;
368 				seq_printf(m, " %ps", (void *)bt);
369 			}
370 			seq_putc(m, '\n');
371 		}
372 
373 	}
374 	put_task_struct(task);
375 	return 0;
376 }
377 
378 static int lstats_open(struct inode *inode, struct file *file)
379 {
380 	return single_open(file, lstats_show_proc, inode);
381 }
382 
383 static ssize_t lstats_write(struct file *file, const char __user *buf,
384 			    size_t count, loff_t *offs)
385 {
386 	struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
387 
388 	if (!task)
389 		return -ESRCH;
390 	clear_all_latency_tracing(task);
391 	put_task_struct(task);
392 
393 	return count;
394 }
395 
396 static const struct file_operations proc_lstats_operations = {
397 	.open		= lstats_open,
398 	.read		= seq_read,
399 	.write		= lstats_write,
400 	.llseek		= seq_lseek,
401 	.release	= single_release,
402 };
403 
404 #endif
405 
406 static int proc_oom_score(struct task_struct *task, char *buffer)
407 {
408 	unsigned long totalpages = totalram_pages + total_swap_pages;
409 	unsigned long points = 0;
410 
411 	read_lock(&tasklist_lock);
412 	if (pid_alive(task))
413 		points = oom_badness(task, NULL, NULL, totalpages) *
414 						1000 / totalpages;
415 	read_unlock(&tasklist_lock);
416 	return sprintf(buffer, "%lu\n", points);
417 }
418 
419 struct limit_names {
420 	char *name;
421 	char *unit;
422 };
423 
424 static const struct limit_names lnames[RLIM_NLIMITS] = {
425 	[RLIMIT_CPU] = {"Max cpu time", "seconds"},
426 	[RLIMIT_FSIZE] = {"Max file size", "bytes"},
427 	[RLIMIT_DATA] = {"Max data size", "bytes"},
428 	[RLIMIT_STACK] = {"Max stack size", "bytes"},
429 	[RLIMIT_CORE] = {"Max core file size", "bytes"},
430 	[RLIMIT_RSS] = {"Max resident set", "bytes"},
431 	[RLIMIT_NPROC] = {"Max processes", "processes"},
432 	[RLIMIT_NOFILE] = {"Max open files", "files"},
433 	[RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
434 	[RLIMIT_AS] = {"Max address space", "bytes"},
435 	[RLIMIT_LOCKS] = {"Max file locks", "locks"},
436 	[RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
437 	[RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
438 	[RLIMIT_NICE] = {"Max nice priority", NULL},
439 	[RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
440 	[RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
441 };
442 
443 /* Display limits for a process */
444 static int proc_pid_limits(struct task_struct *task, char *buffer)
445 {
446 	unsigned int i;
447 	int count = 0;
448 	unsigned long flags;
449 	char *bufptr = buffer;
450 
451 	struct rlimit rlim[RLIM_NLIMITS];
452 
453 	if (!lock_task_sighand(task, &flags))
454 		return 0;
455 	memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
456 	unlock_task_sighand(task, &flags);
457 
458 	/*
459 	 * print the file header
460 	 */
461 	count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
462 			"Limit", "Soft Limit", "Hard Limit", "Units");
463 
464 	for (i = 0; i < RLIM_NLIMITS; i++) {
465 		if (rlim[i].rlim_cur == RLIM_INFINITY)
466 			count += sprintf(&bufptr[count], "%-25s %-20s ",
467 					 lnames[i].name, "unlimited");
468 		else
469 			count += sprintf(&bufptr[count], "%-25s %-20lu ",
470 					 lnames[i].name, rlim[i].rlim_cur);
471 
472 		if (rlim[i].rlim_max == RLIM_INFINITY)
473 			count += sprintf(&bufptr[count], "%-20s ", "unlimited");
474 		else
475 			count += sprintf(&bufptr[count], "%-20lu ",
476 					 rlim[i].rlim_max);
477 
478 		if (lnames[i].unit)
479 			count += sprintf(&bufptr[count], "%-10s\n",
480 					 lnames[i].unit);
481 		else
482 			count += sprintf(&bufptr[count], "\n");
483 	}
484 
485 	return count;
486 }
487 
488 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
489 static int proc_pid_syscall(struct task_struct *task, char *buffer)
490 {
491 	long nr;
492 	unsigned long args[6], sp, pc;
493 	int res = lock_trace(task);
494 	if (res)
495 		return res;
496 
497 	if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
498 		res = sprintf(buffer, "running\n");
499 	else if (nr < 0)
500 		res = sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
501 	else
502 		res = sprintf(buffer,
503 		       "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
504 		       nr,
505 		       args[0], args[1], args[2], args[3], args[4], args[5],
506 		       sp, pc);
507 	unlock_trace(task);
508 	return res;
509 }
510 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
511 
512 /************************************************************************/
513 /*                       Here the fs part begins                        */
514 /************************************************************************/
515 
516 /* permission checks */
517 static int proc_fd_access_allowed(struct inode *inode)
518 {
519 	struct task_struct *task;
520 	int allowed = 0;
521 	/* Allow access to a task's file descriptors if it is us or we
522 	 * may use ptrace attach to the process and find out that
523 	 * information.
524 	 */
525 	task = get_proc_task(inode);
526 	if (task) {
527 		allowed = ptrace_may_access(task, PTRACE_MODE_READ);
528 		put_task_struct(task);
529 	}
530 	return allowed;
531 }
532 
533 int proc_setattr(struct dentry *dentry, struct iattr *attr)
534 {
535 	int error;
536 	struct inode *inode = dentry->d_inode;
537 
538 	if (attr->ia_valid & ATTR_MODE)
539 		return -EPERM;
540 
541 	error = inode_change_ok(inode, attr);
542 	if (error)
543 		return error;
544 
545 	if ((attr->ia_valid & ATTR_SIZE) &&
546 	    attr->ia_size != i_size_read(inode)) {
547 		error = vmtruncate(inode, attr->ia_size);
548 		if (error)
549 			return error;
550 	}
551 
552 	setattr_copy(inode, attr);
553 	mark_inode_dirty(inode);
554 	return 0;
555 }
556 
557 /*
558  * May current process learn task's sched/cmdline info (for hide_pid_min=1)
559  * or euid/egid (for hide_pid_min=2)?
560  */
561 static bool has_pid_permissions(struct pid_namespace *pid,
562 				 struct task_struct *task,
563 				 int hide_pid_min)
564 {
565 	if (pid->hide_pid < hide_pid_min)
566 		return true;
567 	if (in_group_p(pid->pid_gid))
568 		return true;
569 	return ptrace_may_access(task, PTRACE_MODE_READ);
570 }
571 
572 
573 static int proc_pid_permission(struct inode *inode, int mask)
574 {
575 	struct pid_namespace *pid = inode->i_sb->s_fs_info;
576 	struct task_struct *task;
577 	bool has_perms;
578 
579 	task = get_proc_task(inode);
580 	if (!task)
581 		return -ESRCH;
582 	has_perms = has_pid_permissions(pid, task, 1);
583 	put_task_struct(task);
584 
585 	if (!has_perms) {
586 		if (pid->hide_pid == 2) {
587 			/*
588 			 * Let's make getdents(), stat(), and open()
589 			 * consistent with each other.  If a process
590 			 * may not stat() a file, it shouldn't be seen
591 			 * in procfs at all.
592 			 */
593 			return -ENOENT;
594 		}
595 
596 		return -EPERM;
597 	}
598 	return generic_permission(inode, mask);
599 }
600 
601 
602 
603 static const struct inode_operations proc_def_inode_operations = {
604 	.setattr	= proc_setattr,
605 };
606 
607 #define PROC_BLOCK_SIZE	(3*1024)		/* 4K page size but our output routines use some slack for overruns */
608 
609 static ssize_t proc_info_read(struct file * file, char __user * buf,
610 			  size_t count, loff_t *ppos)
611 {
612 	struct inode * inode = file->f_path.dentry->d_inode;
613 	unsigned long page;
614 	ssize_t length;
615 	struct task_struct *task = get_proc_task(inode);
616 
617 	length = -ESRCH;
618 	if (!task)
619 		goto out_no_task;
620 
621 	if (count > PROC_BLOCK_SIZE)
622 		count = PROC_BLOCK_SIZE;
623 
624 	length = -ENOMEM;
625 	if (!(page = __get_free_page(GFP_TEMPORARY)))
626 		goto out;
627 
628 	length = PROC_I(inode)->op.proc_read(task, (char*)page);
629 
630 	if (length >= 0)
631 		length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
632 	free_page(page);
633 out:
634 	put_task_struct(task);
635 out_no_task:
636 	return length;
637 }
638 
639 static const struct file_operations proc_info_file_operations = {
640 	.read		= proc_info_read,
641 	.llseek		= generic_file_llseek,
642 };
643 
644 static int proc_single_show(struct seq_file *m, void *v)
645 {
646 	struct inode *inode = m->private;
647 	struct pid_namespace *ns;
648 	struct pid *pid;
649 	struct task_struct *task;
650 	int ret;
651 
652 	ns = inode->i_sb->s_fs_info;
653 	pid = proc_pid(inode);
654 	task = get_pid_task(pid, PIDTYPE_PID);
655 	if (!task)
656 		return -ESRCH;
657 
658 	ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
659 
660 	put_task_struct(task);
661 	return ret;
662 }
663 
664 static int proc_single_open(struct inode *inode, struct file *filp)
665 {
666 	return single_open(filp, proc_single_show, inode);
667 }
668 
669 static const struct file_operations proc_single_file_operations = {
670 	.open		= proc_single_open,
671 	.read		= seq_read,
672 	.llseek		= seq_lseek,
673 	.release	= single_release,
674 };
675 
676 static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
677 {
678 	struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
679 	struct mm_struct *mm;
680 
681 	if (!task)
682 		return -ESRCH;
683 
684 	mm = mm_access(task, mode);
685 	put_task_struct(task);
686 
687 	if (IS_ERR(mm))
688 		return PTR_ERR(mm);
689 
690 	if (mm) {
691 		/* ensure this mm_struct can't be freed */
692 		atomic_inc(&mm->mm_count);
693 		/* but do not pin its memory */
694 		mmput(mm);
695 	}
696 
697 	file->private_data = mm;
698 
699 	return 0;
700 }
701 
702 static int mem_open(struct inode *inode, struct file *file)
703 {
704 	int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
705 
706 	/* OK to pass negative loff_t, we can catch out-of-range */
707 	file->f_mode |= FMODE_UNSIGNED_OFFSET;
708 
709 	return ret;
710 }
711 
712 static ssize_t mem_rw(struct file *file, char __user *buf,
713 			size_t count, loff_t *ppos, int write)
714 {
715 	struct mm_struct *mm = file->private_data;
716 	unsigned long addr = *ppos;
717 	ssize_t copied;
718 	char *page;
719 
720 	if (!mm)
721 		return 0;
722 
723 	page = (char *)__get_free_page(GFP_TEMPORARY);
724 	if (!page)
725 		return -ENOMEM;
726 
727 	copied = 0;
728 	if (!atomic_inc_not_zero(&mm->mm_users))
729 		goto free;
730 
731 	while (count > 0) {
732 		int this_len = min_t(int, count, PAGE_SIZE);
733 
734 		if (write && copy_from_user(page, buf, this_len)) {
735 			copied = -EFAULT;
736 			break;
737 		}
738 
739 		this_len = access_remote_vm(mm, addr, page, this_len, write);
740 		if (!this_len) {
741 			if (!copied)
742 				copied = -EIO;
743 			break;
744 		}
745 
746 		if (!write && copy_to_user(buf, page, this_len)) {
747 			copied = -EFAULT;
748 			break;
749 		}
750 
751 		buf += this_len;
752 		addr += this_len;
753 		copied += this_len;
754 		count -= this_len;
755 	}
756 	*ppos = addr;
757 
758 	mmput(mm);
759 free:
760 	free_page((unsigned long) page);
761 	return copied;
762 }
763 
764 static ssize_t mem_read(struct file *file, char __user *buf,
765 			size_t count, loff_t *ppos)
766 {
767 	return mem_rw(file, buf, count, ppos, 0);
768 }
769 
770 static ssize_t mem_write(struct file *file, const char __user *buf,
771 			 size_t count, loff_t *ppos)
772 {
773 	return mem_rw(file, (char __user*)buf, count, ppos, 1);
774 }
775 
776 loff_t mem_lseek(struct file *file, loff_t offset, int orig)
777 {
778 	switch (orig) {
779 	case 0:
780 		file->f_pos = offset;
781 		break;
782 	case 1:
783 		file->f_pos += offset;
784 		break;
785 	default:
786 		return -EINVAL;
787 	}
788 	force_successful_syscall_return();
789 	return file->f_pos;
790 }
791 
792 static int mem_release(struct inode *inode, struct file *file)
793 {
794 	struct mm_struct *mm = file->private_data;
795 	if (mm)
796 		mmdrop(mm);
797 	return 0;
798 }
799 
800 static const struct file_operations proc_mem_operations = {
801 	.llseek		= mem_lseek,
802 	.read		= mem_read,
803 	.write		= mem_write,
804 	.open		= mem_open,
805 	.release	= mem_release,
806 };
807 
808 static int environ_open(struct inode *inode, struct file *file)
809 {
810 	return __mem_open(inode, file, PTRACE_MODE_READ);
811 }
812 
813 static ssize_t environ_read(struct file *file, char __user *buf,
814 			size_t count, loff_t *ppos)
815 {
816 	char *page;
817 	unsigned long src = *ppos;
818 	int ret = 0;
819 	struct mm_struct *mm = file->private_data;
820 
821 	if (!mm)
822 		return 0;
823 
824 	page = (char *)__get_free_page(GFP_TEMPORARY);
825 	if (!page)
826 		return -ENOMEM;
827 
828 	ret = 0;
829 	if (!atomic_inc_not_zero(&mm->mm_users))
830 		goto free;
831 	while (count > 0) {
832 		size_t this_len, max_len;
833 		int retval;
834 
835 		if (src >= (mm->env_end - mm->env_start))
836 			break;
837 
838 		this_len = mm->env_end - (mm->env_start + src);
839 
840 		max_len = min_t(size_t, PAGE_SIZE, count);
841 		this_len = min(max_len, this_len);
842 
843 		retval = access_remote_vm(mm, (mm->env_start + src),
844 			page, this_len, 0);
845 
846 		if (retval <= 0) {
847 			ret = retval;
848 			break;
849 		}
850 
851 		if (copy_to_user(buf, page, retval)) {
852 			ret = -EFAULT;
853 			break;
854 		}
855 
856 		ret += retval;
857 		src += retval;
858 		buf += retval;
859 		count -= retval;
860 	}
861 	*ppos = src;
862 	mmput(mm);
863 
864 free:
865 	free_page((unsigned long) page);
866 	return ret;
867 }
868 
869 static const struct file_operations proc_environ_operations = {
870 	.open		= environ_open,
871 	.read		= environ_read,
872 	.llseek		= generic_file_llseek,
873 	.release	= mem_release,
874 };
875 
876 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
877 				size_t count, loff_t *ppos)
878 {
879 	struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
880 	char buffer[PROC_NUMBUF];
881 	size_t len;
882 	int oom_adjust = OOM_DISABLE;
883 	unsigned long flags;
884 
885 	if (!task)
886 		return -ESRCH;
887 
888 	if (lock_task_sighand(task, &flags)) {
889 		oom_adjust = task->signal->oom_adj;
890 		unlock_task_sighand(task, &flags);
891 	}
892 
893 	put_task_struct(task);
894 
895 	len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
896 
897 	return simple_read_from_buffer(buf, count, ppos, buffer, len);
898 }
899 
900 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
901 				size_t count, loff_t *ppos)
902 {
903 	struct task_struct *task;
904 	char buffer[PROC_NUMBUF];
905 	int oom_adjust;
906 	unsigned long flags;
907 	int err;
908 
909 	memset(buffer, 0, sizeof(buffer));
910 	if (count > sizeof(buffer) - 1)
911 		count = sizeof(buffer) - 1;
912 	if (copy_from_user(buffer, buf, count)) {
913 		err = -EFAULT;
914 		goto out;
915 	}
916 
917 	err = kstrtoint(strstrip(buffer), 0, &oom_adjust);
918 	if (err)
919 		goto out;
920 	if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
921 	     oom_adjust != OOM_DISABLE) {
922 		err = -EINVAL;
923 		goto out;
924 	}
925 
926 	task = get_proc_task(file->f_path.dentry->d_inode);
927 	if (!task) {
928 		err = -ESRCH;
929 		goto out;
930 	}
931 
932 	task_lock(task);
933 	if (!task->mm) {
934 		err = -EINVAL;
935 		goto err_task_lock;
936 	}
937 
938 	if (!lock_task_sighand(task, &flags)) {
939 		err = -ESRCH;
940 		goto err_task_lock;
941 	}
942 
943 	if (oom_adjust < task->signal->oom_adj && !capable(CAP_SYS_RESOURCE)) {
944 		err = -EACCES;
945 		goto err_sighand;
946 	}
947 
948 	/*
949 	 * Warn that /proc/pid/oom_adj is deprecated, see
950 	 * Documentation/feature-removal-schedule.txt.
951 	 */
952 	printk_once(KERN_WARNING "%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
953 		  current->comm, task_pid_nr(current), task_pid_nr(task),
954 		  task_pid_nr(task));
955 	task->signal->oom_adj = oom_adjust;
956 	/*
957 	 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
958 	 * value is always attainable.
959 	 */
960 	if (task->signal->oom_adj == OOM_ADJUST_MAX)
961 		task->signal->oom_score_adj = OOM_SCORE_ADJ_MAX;
962 	else
963 		task->signal->oom_score_adj = (oom_adjust * OOM_SCORE_ADJ_MAX) /
964 								-OOM_DISABLE;
965 	trace_oom_score_adj_update(task);
966 err_sighand:
967 	unlock_task_sighand(task, &flags);
968 err_task_lock:
969 	task_unlock(task);
970 	put_task_struct(task);
971 out:
972 	return err < 0 ? err : count;
973 }
974 
975 static const struct file_operations proc_oom_adjust_operations = {
976 	.read		= oom_adjust_read,
977 	.write		= oom_adjust_write,
978 	.llseek		= generic_file_llseek,
979 };
980 
981 static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
982 					size_t count, loff_t *ppos)
983 {
984 	struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
985 	char buffer[PROC_NUMBUF];
986 	int oom_score_adj = OOM_SCORE_ADJ_MIN;
987 	unsigned long flags;
988 	size_t len;
989 
990 	if (!task)
991 		return -ESRCH;
992 	if (lock_task_sighand(task, &flags)) {
993 		oom_score_adj = task->signal->oom_score_adj;
994 		unlock_task_sighand(task, &flags);
995 	}
996 	put_task_struct(task);
997 	len = snprintf(buffer, sizeof(buffer), "%d\n", oom_score_adj);
998 	return simple_read_from_buffer(buf, count, ppos, buffer, len);
999 }
1000 
1001 static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1002 					size_t count, loff_t *ppos)
1003 {
1004 	struct task_struct *task;
1005 	char buffer[PROC_NUMBUF];
1006 	unsigned long flags;
1007 	int oom_score_adj;
1008 	int err;
1009 
1010 	memset(buffer, 0, sizeof(buffer));
1011 	if (count > sizeof(buffer) - 1)
1012 		count = sizeof(buffer) - 1;
1013 	if (copy_from_user(buffer, buf, count)) {
1014 		err = -EFAULT;
1015 		goto out;
1016 	}
1017 
1018 	err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
1019 	if (err)
1020 		goto out;
1021 	if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
1022 			oom_score_adj > OOM_SCORE_ADJ_MAX) {
1023 		err = -EINVAL;
1024 		goto out;
1025 	}
1026 
1027 	task = get_proc_task(file->f_path.dentry->d_inode);
1028 	if (!task) {
1029 		err = -ESRCH;
1030 		goto out;
1031 	}
1032 
1033 	task_lock(task);
1034 	if (!task->mm) {
1035 		err = -EINVAL;
1036 		goto err_task_lock;
1037 	}
1038 
1039 	if (!lock_task_sighand(task, &flags)) {
1040 		err = -ESRCH;
1041 		goto err_task_lock;
1042 	}
1043 
1044 	if (oom_score_adj < task->signal->oom_score_adj_min &&
1045 			!capable(CAP_SYS_RESOURCE)) {
1046 		err = -EACCES;
1047 		goto err_sighand;
1048 	}
1049 
1050 	task->signal->oom_score_adj = oom_score_adj;
1051 	if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
1052 		task->signal->oom_score_adj_min = oom_score_adj;
1053 	trace_oom_score_adj_update(task);
1054 	/*
1055 	 * Scale /proc/pid/oom_adj appropriately ensuring that OOM_DISABLE is
1056 	 * always attainable.
1057 	 */
1058 	if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MIN)
1059 		task->signal->oom_adj = OOM_DISABLE;
1060 	else
1061 		task->signal->oom_adj = (oom_score_adj * OOM_ADJUST_MAX) /
1062 							OOM_SCORE_ADJ_MAX;
1063 err_sighand:
1064 	unlock_task_sighand(task, &flags);
1065 err_task_lock:
1066 	task_unlock(task);
1067 	put_task_struct(task);
1068 out:
1069 	return err < 0 ? err : count;
1070 }
1071 
1072 static const struct file_operations proc_oom_score_adj_operations = {
1073 	.read		= oom_score_adj_read,
1074 	.write		= oom_score_adj_write,
1075 	.llseek		= default_llseek,
1076 };
1077 
1078 #ifdef CONFIG_AUDITSYSCALL
1079 #define TMPBUFLEN 21
1080 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1081 				  size_t count, loff_t *ppos)
1082 {
1083 	struct inode * inode = file->f_path.dentry->d_inode;
1084 	struct task_struct *task = get_proc_task(inode);
1085 	ssize_t length;
1086 	char tmpbuf[TMPBUFLEN];
1087 
1088 	if (!task)
1089 		return -ESRCH;
1090 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1091 			   from_kuid(file->f_cred->user_ns,
1092 				     audit_get_loginuid(task)));
1093 	put_task_struct(task);
1094 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1095 }
1096 
1097 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1098 				   size_t count, loff_t *ppos)
1099 {
1100 	struct inode * inode = file->f_path.dentry->d_inode;
1101 	char *page, *tmp;
1102 	ssize_t length;
1103 	uid_t loginuid;
1104 	kuid_t kloginuid;
1105 
1106 	rcu_read_lock();
1107 	if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1108 		rcu_read_unlock();
1109 		return -EPERM;
1110 	}
1111 	rcu_read_unlock();
1112 
1113 	if (count >= PAGE_SIZE)
1114 		count = PAGE_SIZE - 1;
1115 
1116 	if (*ppos != 0) {
1117 		/* No partial writes. */
1118 		return -EINVAL;
1119 	}
1120 	page = (char*)__get_free_page(GFP_TEMPORARY);
1121 	if (!page)
1122 		return -ENOMEM;
1123 	length = -EFAULT;
1124 	if (copy_from_user(page, buf, count))
1125 		goto out_free_page;
1126 
1127 	page[count] = '\0';
1128 	loginuid = simple_strtoul(page, &tmp, 10);
1129 	if (tmp == page) {
1130 		length = -EINVAL;
1131 		goto out_free_page;
1132 
1133 	}
1134 	kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
1135 	if (!uid_valid(kloginuid)) {
1136 		length = -EINVAL;
1137 		goto out_free_page;
1138 	}
1139 
1140 	length = audit_set_loginuid(kloginuid);
1141 	if (likely(length == 0))
1142 		length = count;
1143 
1144 out_free_page:
1145 	free_page((unsigned long) page);
1146 	return length;
1147 }
1148 
1149 static const struct file_operations proc_loginuid_operations = {
1150 	.read		= proc_loginuid_read,
1151 	.write		= proc_loginuid_write,
1152 	.llseek		= generic_file_llseek,
1153 };
1154 
1155 static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1156 				  size_t count, loff_t *ppos)
1157 {
1158 	struct inode * inode = file->f_path.dentry->d_inode;
1159 	struct task_struct *task = get_proc_task(inode);
1160 	ssize_t length;
1161 	char tmpbuf[TMPBUFLEN];
1162 
1163 	if (!task)
1164 		return -ESRCH;
1165 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1166 				audit_get_sessionid(task));
1167 	put_task_struct(task);
1168 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1169 }
1170 
1171 static const struct file_operations proc_sessionid_operations = {
1172 	.read		= proc_sessionid_read,
1173 	.llseek		= generic_file_llseek,
1174 };
1175 #endif
1176 
1177 #ifdef CONFIG_FAULT_INJECTION
1178 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1179 				      size_t count, loff_t *ppos)
1180 {
1181 	struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1182 	char buffer[PROC_NUMBUF];
1183 	size_t len;
1184 	int make_it_fail;
1185 
1186 	if (!task)
1187 		return -ESRCH;
1188 	make_it_fail = task->make_it_fail;
1189 	put_task_struct(task);
1190 
1191 	len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1192 
1193 	return simple_read_from_buffer(buf, count, ppos, buffer, len);
1194 }
1195 
1196 static ssize_t proc_fault_inject_write(struct file * file,
1197 			const char __user * buf, size_t count, loff_t *ppos)
1198 {
1199 	struct task_struct *task;
1200 	char buffer[PROC_NUMBUF], *end;
1201 	int make_it_fail;
1202 
1203 	if (!capable(CAP_SYS_RESOURCE))
1204 		return -EPERM;
1205 	memset(buffer, 0, sizeof(buffer));
1206 	if (count > sizeof(buffer) - 1)
1207 		count = sizeof(buffer) - 1;
1208 	if (copy_from_user(buffer, buf, count))
1209 		return -EFAULT;
1210 	make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1211 	if (*end)
1212 		return -EINVAL;
1213 	task = get_proc_task(file->f_dentry->d_inode);
1214 	if (!task)
1215 		return -ESRCH;
1216 	task->make_it_fail = make_it_fail;
1217 	put_task_struct(task);
1218 
1219 	return count;
1220 }
1221 
1222 static const struct file_operations proc_fault_inject_operations = {
1223 	.read		= proc_fault_inject_read,
1224 	.write		= proc_fault_inject_write,
1225 	.llseek		= generic_file_llseek,
1226 };
1227 #endif
1228 
1229 
1230 #ifdef CONFIG_SCHED_DEBUG
1231 /*
1232  * Print out various scheduling related per-task fields:
1233  */
1234 static int sched_show(struct seq_file *m, void *v)
1235 {
1236 	struct inode *inode = m->private;
1237 	struct task_struct *p;
1238 
1239 	p = get_proc_task(inode);
1240 	if (!p)
1241 		return -ESRCH;
1242 	proc_sched_show_task(p, m);
1243 
1244 	put_task_struct(p);
1245 
1246 	return 0;
1247 }
1248 
1249 static ssize_t
1250 sched_write(struct file *file, const char __user *buf,
1251 	    size_t count, loff_t *offset)
1252 {
1253 	struct inode *inode = file->f_path.dentry->d_inode;
1254 	struct task_struct *p;
1255 
1256 	p = get_proc_task(inode);
1257 	if (!p)
1258 		return -ESRCH;
1259 	proc_sched_set_task(p);
1260 
1261 	put_task_struct(p);
1262 
1263 	return count;
1264 }
1265 
1266 static int sched_open(struct inode *inode, struct file *filp)
1267 {
1268 	return single_open(filp, sched_show, inode);
1269 }
1270 
1271 static const struct file_operations proc_pid_sched_operations = {
1272 	.open		= sched_open,
1273 	.read		= seq_read,
1274 	.write		= sched_write,
1275 	.llseek		= seq_lseek,
1276 	.release	= single_release,
1277 };
1278 
1279 #endif
1280 
1281 #ifdef CONFIG_SCHED_AUTOGROUP
1282 /*
1283  * Print out autogroup related information:
1284  */
1285 static int sched_autogroup_show(struct seq_file *m, void *v)
1286 {
1287 	struct inode *inode = m->private;
1288 	struct task_struct *p;
1289 
1290 	p = get_proc_task(inode);
1291 	if (!p)
1292 		return -ESRCH;
1293 	proc_sched_autogroup_show_task(p, m);
1294 
1295 	put_task_struct(p);
1296 
1297 	return 0;
1298 }
1299 
1300 static ssize_t
1301 sched_autogroup_write(struct file *file, const char __user *buf,
1302 	    size_t count, loff_t *offset)
1303 {
1304 	struct inode *inode = file->f_path.dentry->d_inode;
1305 	struct task_struct *p;
1306 	char buffer[PROC_NUMBUF];
1307 	int nice;
1308 	int err;
1309 
1310 	memset(buffer, 0, sizeof(buffer));
1311 	if (count > sizeof(buffer) - 1)
1312 		count = sizeof(buffer) - 1;
1313 	if (copy_from_user(buffer, buf, count))
1314 		return -EFAULT;
1315 
1316 	err = kstrtoint(strstrip(buffer), 0, &nice);
1317 	if (err < 0)
1318 		return err;
1319 
1320 	p = get_proc_task(inode);
1321 	if (!p)
1322 		return -ESRCH;
1323 
1324 	err = proc_sched_autogroup_set_nice(p, nice);
1325 	if (err)
1326 		count = err;
1327 
1328 	put_task_struct(p);
1329 
1330 	return count;
1331 }
1332 
1333 static int sched_autogroup_open(struct inode *inode, struct file *filp)
1334 {
1335 	int ret;
1336 
1337 	ret = single_open(filp, sched_autogroup_show, NULL);
1338 	if (!ret) {
1339 		struct seq_file *m = filp->private_data;
1340 
1341 		m->private = inode;
1342 	}
1343 	return ret;
1344 }
1345 
1346 static const struct file_operations proc_pid_sched_autogroup_operations = {
1347 	.open		= sched_autogroup_open,
1348 	.read		= seq_read,
1349 	.write		= sched_autogroup_write,
1350 	.llseek		= seq_lseek,
1351 	.release	= single_release,
1352 };
1353 
1354 #endif /* CONFIG_SCHED_AUTOGROUP */
1355 
1356 static ssize_t comm_write(struct file *file, const char __user *buf,
1357 				size_t count, loff_t *offset)
1358 {
1359 	struct inode *inode = file->f_path.dentry->d_inode;
1360 	struct task_struct *p;
1361 	char buffer[TASK_COMM_LEN];
1362 
1363 	memset(buffer, 0, sizeof(buffer));
1364 	if (count > sizeof(buffer) - 1)
1365 		count = sizeof(buffer) - 1;
1366 	if (copy_from_user(buffer, buf, count))
1367 		return -EFAULT;
1368 
1369 	p = get_proc_task(inode);
1370 	if (!p)
1371 		return -ESRCH;
1372 
1373 	if (same_thread_group(current, p))
1374 		set_task_comm(p, buffer);
1375 	else
1376 		count = -EINVAL;
1377 
1378 	put_task_struct(p);
1379 
1380 	return count;
1381 }
1382 
1383 static int comm_show(struct seq_file *m, void *v)
1384 {
1385 	struct inode *inode = m->private;
1386 	struct task_struct *p;
1387 
1388 	p = get_proc_task(inode);
1389 	if (!p)
1390 		return -ESRCH;
1391 
1392 	task_lock(p);
1393 	seq_printf(m, "%s\n", p->comm);
1394 	task_unlock(p);
1395 
1396 	put_task_struct(p);
1397 
1398 	return 0;
1399 }
1400 
1401 static int comm_open(struct inode *inode, struct file *filp)
1402 {
1403 	return single_open(filp, comm_show, inode);
1404 }
1405 
1406 static const struct file_operations proc_pid_set_comm_operations = {
1407 	.open		= comm_open,
1408 	.read		= seq_read,
1409 	.write		= comm_write,
1410 	.llseek		= seq_lseek,
1411 	.release	= single_release,
1412 };
1413 
1414 static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
1415 {
1416 	struct task_struct *task;
1417 	struct mm_struct *mm;
1418 	struct file *exe_file;
1419 
1420 	task = get_proc_task(dentry->d_inode);
1421 	if (!task)
1422 		return -ENOENT;
1423 	mm = get_task_mm(task);
1424 	put_task_struct(task);
1425 	if (!mm)
1426 		return -ENOENT;
1427 	exe_file = get_mm_exe_file(mm);
1428 	mmput(mm);
1429 	if (exe_file) {
1430 		*exe_path = exe_file->f_path;
1431 		path_get(&exe_file->f_path);
1432 		fput(exe_file);
1433 		return 0;
1434 	} else
1435 		return -ENOENT;
1436 }
1437 
1438 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1439 {
1440 	struct inode *inode = dentry->d_inode;
1441 	struct path path;
1442 	int error = -EACCES;
1443 
1444 	/* Are we allowed to snoop on the tasks file descriptors? */
1445 	if (!proc_fd_access_allowed(inode))
1446 		goto out;
1447 
1448 	error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1449 	if (error)
1450 		goto out;
1451 
1452 	nd_jump_link(nd, &path);
1453 	return NULL;
1454 out:
1455 	return ERR_PTR(error);
1456 }
1457 
1458 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1459 {
1460 	char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
1461 	char *pathname;
1462 	int len;
1463 
1464 	if (!tmp)
1465 		return -ENOMEM;
1466 
1467 	pathname = d_path(path, tmp, PAGE_SIZE);
1468 	len = PTR_ERR(pathname);
1469 	if (IS_ERR(pathname))
1470 		goto out;
1471 	len = tmp + PAGE_SIZE - 1 - pathname;
1472 
1473 	if (len > buflen)
1474 		len = buflen;
1475 	if (copy_to_user(buffer, pathname, len))
1476 		len = -EFAULT;
1477  out:
1478 	free_page((unsigned long)tmp);
1479 	return len;
1480 }
1481 
1482 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1483 {
1484 	int error = -EACCES;
1485 	struct inode *inode = dentry->d_inode;
1486 	struct path path;
1487 
1488 	/* Are we allowed to snoop on the tasks file descriptors? */
1489 	if (!proc_fd_access_allowed(inode))
1490 		goto out;
1491 
1492 	error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1493 	if (error)
1494 		goto out;
1495 
1496 	error = do_proc_readlink(&path, buffer, buflen);
1497 	path_put(&path);
1498 out:
1499 	return error;
1500 }
1501 
1502 const struct inode_operations proc_pid_link_inode_operations = {
1503 	.readlink	= proc_pid_readlink,
1504 	.follow_link	= proc_pid_follow_link,
1505 	.setattr	= proc_setattr,
1506 };
1507 
1508 
1509 /* building an inode */
1510 
1511 struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1512 {
1513 	struct inode * inode;
1514 	struct proc_inode *ei;
1515 	const struct cred *cred;
1516 
1517 	/* We need a new inode */
1518 
1519 	inode = new_inode(sb);
1520 	if (!inode)
1521 		goto out;
1522 
1523 	/* Common stuff */
1524 	ei = PROC_I(inode);
1525 	inode->i_ino = get_next_ino();
1526 	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1527 	inode->i_op = &proc_def_inode_operations;
1528 
1529 	/*
1530 	 * grab the reference to task.
1531 	 */
1532 	ei->pid = get_task_pid(task, PIDTYPE_PID);
1533 	if (!ei->pid)
1534 		goto out_unlock;
1535 
1536 	if (task_dumpable(task)) {
1537 		rcu_read_lock();
1538 		cred = __task_cred(task);
1539 		inode->i_uid = cred->euid;
1540 		inode->i_gid = cred->egid;
1541 		rcu_read_unlock();
1542 	}
1543 	security_task_to_inode(task, inode);
1544 
1545 out:
1546 	return inode;
1547 
1548 out_unlock:
1549 	iput(inode);
1550 	return NULL;
1551 }
1552 
1553 int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1554 {
1555 	struct inode *inode = dentry->d_inode;
1556 	struct task_struct *task;
1557 	const struct cred *cred;
1558 	struct pid_namespace *pid = dentry->d_sb->s_fs_info;
1559 
1560 	generic_fillattr(inode, stat);
1561 
1562 	rcu_read_lock();
1563 	stat->uid = GLOBAL_ROOT_UID;
1564 	stat->gid = GLOBAL_ROOT_GID;
1565 	task = pid_task(proc_pid(inode), PIDTYPE_PID);
1566 	if (task) {
1567 		if (!has_pid_permissions(pid, task, 2)) {
1568 			rcu_read_unlock();
1569 			/*
1570 			 * This doesn't prevent learning whether PID exists,
1571 			 * it only makes getattr() consistent with readdir().
1572 			 */
1573 			return -ENOENT;
1574 		}
1575 		if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1576 		    task_dumpable(task)) {
1577 			cred = __task_cred(task);
1578 			stat->uid = cred->euid;
1579 			stat->gid = cred->egid;
1580 		}
1581 	}
1582 	rcu_read_unlock();
1583 	return 0;
1584 }
1585 
1586 /* dentry stuff */
1587 
1588 /*
1589  *	Exceptional case: normally we are not allowed to unhash a busy
1590  * directory. In this case, however, we can do it - no aliasing problems
1591  * due to the way we treat inodes.
1592  *
1593  * Rewrite the inode's ownerships here because the owning task may have
1594  * performed a setuid(), etc.
1595  *
1596  * Before the /proc/pid/status file was created the only way to read
1597  * the effective uid of a /process was to stat /proc/pid.  Reading
1598  * /proc/pid/status is slow enough that procps and other packages
1599  * kept stating /proc/pid.  To keep the rules in /proc simple I have
1600  * made this apply to all per process world readable and executable
1601  * directories.
1602  */
1603 int pid_revalidate(struct dentry *dentry, unsigned int flags)
1604 {
1605 	struct inode *inode;
1606 	struct task_struct *task;
1607 	const struct cred *cred;
1608 
1609 	if (flags & LOOKUP_RCU)
1610 		return -ECHILD;
1611 
1612 	inode = dentry->d_inode;
1613 	task = get_proc_task(inode);
1614 
1615 	if (task) {
1616 		if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1617 		    task_dumpable(task)) {
1618 			rcu_read_lock();
1619 			cred = __task_cred(task);
1620 			inode->i_uid = cred->euid;
1621 			inode->i_gid = cred->egid;
1622 			rcu_read_unlock();
1623 		} else {
1624 			inode->i_uid = GLOBAL_ROOT_UID;
1625 			inode->i_gid = GLOBAL_ROOT_GID;
1626 		}
1627 		inode->i_mode &= ~(S_ISUID | S_ISGID);
1628 		security_task_to_inode(task, inode);
1629 		put_task_struct(task);
1630 		return 1;
1631 	}
1632 	d_drop(dentry);
1633 	return 0;
1634 }
1635 
1636 const struct dentry_operations pid_dentry_operations =
1637 {
1638 	.d_revalidate	= pid_revalidate,
1639 	.d_delete	= pid_delete_dentry,
1640 };
1641 
1642 /* Lookups */
1643 
1644 /*
1645  * Fill a directory entry.
1646  *
1647  * If possible create the dcache entry and derive our inode number and
1648  * file type from dcache entry.
1649  *
1650  * Since all of the proc inode numbers are dynamically generated, the inode
1651  * numbers do not exist until the inode is cache.  This means creating the
1652  * the dcache entry in readdir is necessary to keep the inode numbers
1653  * reported by readdir in sync with the inode numbers reported
1654  * by stat.
1655  */
1656 int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1657 	const char *name, int len,
1658 	instantiate_t instantiate, struct task_struct *task, const void *ptr)
1659 {
1660 	struct dentry *child, *dir = filp->f_path.dentry;
1661 	struct inode *inode;
1662 	struct qstr qname;
1663 	ino_t ino = 0;
1664 	unsigned type = DT_UNKNOWN;
1665 
1666 	qname.name = name;
1667 	qname.len  = len;
1668 	qname.hash = full_name_hash(name, len);
1669 
1670 	child = d_lookup(dir, &qname);
1671 	if (!child) {
1672 		struct dentry *new;
1673 		new = d_alloc(dir, &qname);
1674 		if (new) {
1675 			child = instantiate(dir->d_inode, new, task, ptr);
1676 			if (child)
1677 				dput(new);
1678 			else
1679 				child = new;
1680 		}
1681 	}
1682 	if (!child || IS_ERR(child) || !child->d_inode)
1683 		goto end_instantiate;
1684 	inode = child->d_inode;
1685 	if (inode) {
1686 		ino = inode->i_ino;
1687 		type = inode->i_mode >> 12;
1688 	}
1689 	dput(child);
1690 end_instantiate:
1691 	if (!ino)
1692 		ino = find_inode_number(dir, &qname);
1693 	if (!ino)
1694 		ino = 1;
1695 	return filldir(dirent, name, len, filp->f_pos, ino, type);
1696 }
1697 
1698 #ifdef CONFIG_CHECKPOINT_RESTORE
1699 
1700 /*
1701  * dname_to_vma_addr - maps a dentry name into two unsigned longs
1702  * which represent vma start and end addresses.
1703  */
1704 static int dname_to_vma_addr(struct dentry *dentry,
1705 			     unsigned long *start, unsigned long *end)
1706 {
1707 	if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2)
1708 		return -EINVAL;
1709 
1710 	return 0;
1711 }
1712 
1713 static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
1714 {
1715 	unsigned long vm_start, vm_end;
1716 	bool exact_vma_exists = false;
1717 	struct mm_struct *mm = NULL;
1718 	struct task_struct *task;
1719 	const struct cred *cred;
1720 	struct inode *inode;
1721 	int status = 0;
1722 
1723 	if (flags & LOOKUP_RCU)
1724 		return -ECHILD;
1725 
1726 	if (!capable(CAP_SYS_ADMIN)) {
1727 		status = -EACCES;
1728 		goto out_notask;
1729 	}
1730 
1731 	inode = dentry->d_inode;
1732 	task = get_proc_task(inode);
1733 	if (!task)
1734 		goto out_notask;
1735 
1736 	mm = mm_access(task, PTRACE_MODE_READ);
1737 	if (IS_ERR_OR_NULL(mm))
1738 		goto out;
1739 
1740 	if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1741 		down_read(&mm->mmap_sem);
1742 		exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1743 		up_read(&mm->mmap_sem);
1744 	}
1745 
1746 	mmput(mm);
1747 
1748 	if (exact_vma_exists) {
1749 		if (task_dumpable(task)) {
1750 			rcu_read_lock();
1751 			cred = __task_cred(task);
1752 			inode->i_uid = cred->euid;
1753 			inode->i_gid = cred->egid;
1754 			rcu_read_unlock();
1755 		} else {
1756 			inode->i_uid = GLOBAL_ROOT_UID;
1757 			inode->i_gid = GLOBAL_ROOT_GID;
1758 		}
1759 		security_task_to_inode(task, inode);
1760 		status = 1;
1761 	}
1762 
1763 out:
1764 	put_task_struct(task);
1765 
1766 out_notask:
1767 	if (status <= 0)
1768 		d_drop(dentry);
1769 
1770 	return status;
1771 }
1772 
1773 static const struct dentry_operations tid_map_files_dentry_operations = {
1774 	.d_revalidate	= map_files_d_revalidate,
1775 	.d_delete	= pid_delete_dentry,
1776 };
1777 
1778 static int proc_map_files_get_link(struct dentry *dentry, struct path *path)
1779 {
1780 	unsigned long vm_start, vm_end;
1781 	struct vm_area_struct *vma;
1782 	struct task_struct *task;
1783 	struct mm_struct *mm;
1784 	int rc;
1785 
1786 	rc = -ENOENT;
1787 	task = get_proc_task(dentry->d_inode);
1788 	if (!task)
1789 		goto out;
1790 
1791 	mm = get_task_mm(task);
1792 	put_task_struct(task);
1793 	if (!mm)
1794 		goto out;
1795 
1796 	rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
1797 	if (rc)
1798 		goto out_mmput;
1799 
1800 	down_read(&mm->mmap_sem);
1801 	vma = find_exact_vma(mm, vm_start, vm_end);
1802 	if (vma && vma->vm_file) {
1803 		*path = vma->vm_file->f_path;
1804 		path_get(path);
1805 		rc = 0;
1806 	}
1807 	up_read(&mm->mmap_sem);
1808 
1809 out_mmput:
1810 	mmput(mm);
1811 out:
1812 	return rc;
1813 }
1814 
1815 struct map_files_info {
1816 	fmode_t		mode;
1817 	unsigned long	len;
1818 	unsigned char	name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
1819 };
1820 
1821 static struct dentry *
1822 proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
1823 			   struct task_struct *task, const void *ptr)
1824 {
1825 	fmode_t mode = (fmode_t)(unsigned long)ptr;
1826 	struct proc_inode *ei;
1827 	struct inode *inode;
1828 
1829 	inode = proc_pid_make_inode(dir->i_sb, task);
1830 	if (!inode)
1831 		return ERR_PTR(-ENOENT);
1832 
1833 	ei = PROC_I(inode);
1834 	ei->op.proc_get_link = proc_map_files_get_link;
1835 
1836 	inode->i_op = &proc_pid_link_inode_operations;
1837 	inode->i_size = 64;
1838 	inode->i_mode = S_IFLNK;
1839 
1840 	if (mode & FMODE_READ)
1841 		inode->i_mode |= S_IRUSR;
1842 	if (mode & FMODE_WRITE)
1843 		inode->i_mode |= S_IWUSR;
1844 
1845 	d_set_d_op(dentry, &tid_map_files_dentry_operations);
1846 	d_add(dentry, inode);
1847 
1848 	return NULL;
1849 }
1850 
1851 static struct dentry *proc_map_files_lookup(struct inode *dir,
1852 		struct dentry *dentry, unsigned int flags)
1853 {
1854 	unsigned long vm_start, vm_end;
1855 	struct vm_area_struct *vma;
1856 	struct task_struct *task;
1857 	struct dentry *result;
1858 	struct mm_struct *mm;
1859 
1860 	result = ERR_PTR(-EACCES);
1861 	if (!capable(CAP_SYS_ADMIN))
1862 		goto out;
1863 
1864 	result = ERR_PTR(-ENOENT);
1865 	task = get_proc_task(dir);
1866 	if (!task)
1867 		goto out;
1868 
1869 	result = ERR_PTR(-EACCES);
1870 	if (!ptrace_may_access(task, PTRACE_MODE_READ))
1871 		goto out_put_task;
1872 
1873 	result = ERR_PTR(-ENOENT);
1874 	if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
1875 		goto out_put_task;
1876 
1877 	mm = get_task_mm(task);
1878 	if (!mm)
1879 		goto out_put_task;
1880 
1881 	down_read(&mm->mmap_sem);
1882 	vma = find_exact_vma(mm, vm_start, vm_end);
1883 	if (!vma)
1884 		goto out_no_vma;
1885 
1886 	result = proc_map_files_instantiate(dir, dentry, task,
1887 			(void *)(unsigned long)vma->vm_file->f_mode);
1888 
1889 out_no_vma:
1890 	up_read(&mm->mmap_sem);
1891 	mmput(mm);
1892 out_put_task:
1893 	put_task_struct(task);
1894 out:
1895 	return result;
1896 }
1897 
1898 static const struct inode_operations proc_map_files_inode_operations = {
1899 	.lookup		= proc_map_files_lookup,
1900 	.permission	= proc_fd_permission,
1901 	.setattr	= proc_setattr,
1902 };
1903 
1904 static int
1905 proc_map_files_readdir(struct file *filp, void *dirent, filldir_t filldir)
1906 {
1907 	struct dentry *dentry = filp->f_path.dentry;
1908 	struct inode *inode = dentry->d_inode;
1909 	struct vm_area_struct *vma;
1910 	struct task_struct *task;
1911 	struct mm_struct *mm;
1912 	ino_t ino;
1913 	int ret;
1914 
1915 	ret = -EACCES;
1916 	if (!capable(CAP_SYS_ADMIN))
1917 		goto out;
1918 
1919 	ret = -ENOENT;
1920 	task = get_proc_task(inode);
1921 	if (!task)
1922 		goto out;
1923 
1924 	ret = -EACCES;
1925 	if (!ptrace_may_access(task, PTRACE_MODE_READ))
1926 		goto out_put_task;
1927 
1928 	ret = 0;
1929 	switch (filp->f_pos) {
1930 	case 0:
1931 		ino = inode->i_ino;
1932 		if (filldir(dirent, ".", 1, 0, ino, DT_DIR) < 0)
1933 			goto out_put_task;
1934 		filp->f_pos++;
1935 	case 1:
1936 		ino = parent_ino(dentry);
1937 		if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1938 			goto out_put_task;
1939 		filp->f_pos++;
1940 	default:
1941 	{
1942 		unsigned long nr_files, pos, i;
1943 		struct flex_array *fa = NULL;
1944 		struct map_files_info info;
1945 		struct map_files_info *p;
1946 
1947 		mm = get_task_mm(task);
1948 		if (!mm)
1949 			goto out_put_task;
1950 		down_read(&mm->mmap_sem);
1951 
1952 		nr_files = 0;
1953 
1954 		/*
1955 		 * We need two passes here:
1956 		 *
1957 		 *  1) Collect vmas of mapped files with mmap_sem taken
1958 		 *  2) Release mmap_sem and instantiate entries
1959 		 *
1960 		 * otherwise we get lockdep complained, since filldir()
1961 		 * routine might require mmap_sem taken in might_fault().
1962 		 */
1963 
1964 		for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
1965 			if (vma->vm_file && ++pos > filp->f_pos)
1966 				nr_files++;
1967 		}
1968 
1969 		if (nr_files) {
1970 			fa = flex_array_alloc(sizeof(info), nr_files,
1971 						GFP_KERNEL);
1972 			if (!fa || flex_array_prealloc(fa, 0, nr_files,
1973 							GFP_KERNEL)) {
1974 				ret = -ENOMEM;
1975 				if (fa)
1976 					flex_array_free(fa);
1977 				up_read(&mm->mmap_sem);
1978 				mmput(mm);
1979 				goto out_put_task;
1980 			}
1981 			for (i = 0, vma = mm->mmap, pos = 2; vma;
1982 					vma = vma->vm_next) {
1983 				if (!vma->vm_file)
1984 					continue;
1985 				if (++pos <= filp->f_pos)
1986 					continue;
1987 
1988 				info.mode = vma->vm_file->f_mode;
1989 				info.len = snprintf(info.name,
1990 						sizeof(info.name), "%lx-%lx",
1991 						vma->vm_start, vma->vm_end);
1992 				if (flex_array_put(fa, i++, &info, GFP_KERNEL))
1993 					BUG();
1994 			}
1995 		}
1996 		up_read(&mm->mmap_sem);
1997 
1998 		for (i = 0; i < nr_files; i++) {
1999 			p = flex_array_get(fa, i);
2000 			ret = proc_fill_cache(filp, dirent, filldir,
2001 					      p->name, p->len,
2002 					      proc_map_files_instantiate,
2003 					      task,
2004 					      (void *)(unsigned long)p->mode);
2005 			if (ret)
2006 				break;
2007 			filp->f_pos++;
2008 		}
2009 		if (fa)
2010 			flex_array_free(fa);
2011 		mmput(mm);
2012 	}
2013 	}
2014 
2015 out_put_task:
2016 	put_task_struct(task);
2017 out:
2018 	return ret;
2019 }
2020 
2021 static const struct file_operations proc_map_files_operations = {
2022 	.read		= generic_read_dir,
2023 	.readdir	= proc_map_files_readdir,
2024 	.llseek		= default_llseek,
2025 };
2026 
2027 #endif /* CONFIG_CHECKPOINT_RESTORE */
2028 
2029 static struct dentry *proc_pident_instantiate(struct inode *dir,
2030 	struct dentry *dentry, struct task_struct *task, const void *ptr)
2031 {
2032 	const struct pid_entry *p = ptr;
2033 	struct inode *inode;
2034 	struct proc_inode *ei;
2035 	struct dentry *error = ERR_PTR(-ENOENT);
2036 
2037 	inode = proc_pid_make_inode(dir->i_sb, task);
2038 	if (!inode)
2039 		goto out;
2040 
2041 	ei = PROC_I(inode);
2042 	inode->i_mode = p->mode;
2043 	if (S_ISDIR(inode->i_mode))
2044 		set_nlink(inode, 2);	/* Use getattr to fix if necessary */
2045 	if (p->iop)
2046 		inode->i_op = p->iop;
2047 	if (p->fop)
2048 		inode->i_fop = p->fop;
2049 	ei->op = p->op;
2050 	d_set_d_op(dentry, &pid_dentry_operations);
2051 	d_add(dentry, inode);
2052 	/* Close the race of the process dying before we return the dentry */
2053 	if (pid_revalidate(dentry, 0))
2054 		error = NULL;
2055 out:
2056 	return error;
2057 }
2058 
2059 static struct dentry *proc_pident_lookup(struct inode *dir,
2060 					 struct dentry *dentry,
2061 					 const struct pid_entry *ents,
2062 					 unsigned int nents)
2063 {
2064 	struct dentry *error;
2065 	struct task_struct *task = get_proc_task(dir);
2066 	const struct pid_entry *p, *last;
2067 
2068 	error = ERR_PTR(-ENOENT);
2069 
2070 	if (!task)
2071 		goto out_no_task;
2072 
2073 	/*
2074 	 * Yes, it does not scale. And it should not. Don't add
2075 	 * new entries into /proc/<tgid>/ without very good reasons.
2076 	 */
2077 	last = &ents[nents - 1];
2078 	for (p = ents; p <= last; p++) {
2079 		if (p->len != dentry->d_name.len)
2080 			continue;
2081 		if (!memcmp(dentry->d_name.name, p->name, p->len))
2082 			break;
2083 	}
2084 	if (p > last)
2085 		goto out;
2086 
2087 	error = proc_pident_instantiate(dir, dentry, task, p);
2088 out:
2089 	put_task_struct(task);
2090 out_no_task:
2091 	return error;
2092 }
2093 
2094 static int proc_pident_fill_cache(struct file *filp, void *dirent,
2095 	filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2096 {
2097 	return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2098 				proc_pident_instantiate, task, p);
2099 }
2100 
2101 static int proc_pident_readdir(struct file *filp,
2102 		void *dirent, filldir_t filldir,
2103 		const struct pid_entry *ents, unsigned int nents)
2104 {
2105 	int i;
2106 	struct dentry *dentry = filp->f_path.dentry;
2107 	struct inode *inode = dentry->d_inode;
2108 	struct task_struct *task = get_proc_task(inode);
2109 	const struct pid_entry *p, *last;
2110 	ino_t ino;
2111 	int ret;
2112 
2113 	ret = -ENOENT;
2114 	if (!task)
2115 		goto out_no_task;
2116 
2117 	ret = 0;
2118 	i = filp->f_pos;
2119 	switch (i) {
2120 	case 0:
2121 		ino = inode->i_ino;
2122 		if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
2123 			goto out;
2124 		i++;
2125 		filp->f_pos++;
2126 		/* fall through */
2127 	case 1:
2128 		ino = parent_ino(dentry);
2129 		if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
2130 			goto out;
2131 		i++;
2132 		filp->f_pos++;
2133 		/* fall through */
2134 	default:
2135 		i -= 2;
2136 		if (i >= nents) {
2137 			ret = 1;
2138 			goto out;
2139 		}
2140 		p = ents + i;
2141 		last = &ents[nents - 1];
2142 		while (p <= last) {
2143 			if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
2144 				goto out;
2145 			filp->f_pos++;
2146 			p++;
2147 		}
2148 	}
2149 
2150 	ret = 1;
2151 out:
2152 	put_task_struct(task);
2153 out_no_task:
2154 	return ret;
2155 }
2156 
2157 #ifdef CONFIG_SECURITY
2158 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2159 				  size_t count, loff_t *ppos)
2160 {
2161 	struct inode * inode = file->f_path.dentry->d_inode;
2162 	char *p = NULL;
2163 	ssize_t length;
2164 	struct task_struct *task = get_proc_task(inode);
2165 
2166 	if (!task)
2167 		return -ESRCH;
2168 
2169 	length = security_getprocattr(task,
2170 				      (char*)file->f_path.dentry->d_name.name,
2171 				      &p);
2172 	put_task_struct(task);
2173 	if (length > 0)
2174 		length = simple_read_from_buffer(buf, count, ppos, p, length);
2175 	kfree(p);
2176 	return length;
2177 }
2178 
2179 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2180 				   size_t count, loff_t *ppos)
2181 {
2182 	struct inode * inode = file->f_path.dentry->d_inode;
2183 	char *page;
2184 	ssize_t length;
2185 	struct task_struct *task = get_proc_task(inode);
2186 
2187 	length = -ESRCH;
2188 	if (!task)
2189 		goto out_no_task;
2190 	if (count > PAGE_SIZE)
2191 		count = PAGE_SIZE;
2192 
2193 	/* No partial writes. */
2194 	length = -EINVAL;
2195 	if (*ppos != 0)
2196 		goto out;
2197 
2198 	length = -ENOMEM;
2199 	page = (char*)__get_free_page(GFP_TEMPORARY);
2200 	if (!page)
2201 		goto out;
2202 
2203 	length = -EFAULT;
2204 	if (copy_from_user(page, buf, count))
2205 		goto out_free;
2206 
2207 	/* Guard against adverse ptrace interaction */
2208 	length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
2209 	if (length < 0)
2210 		goto out_free;
2211 
2212 	length = security_setprocattr(task,
2213 				      (char*)file->f_path.dentry->d_name.name,
2214 				      (void*)page, count);
2215 	mutex_unlock(&task->signal->cred_guard_mutex);
2216 out_free:
2217 	free_page((unsigned long) page);
2218 out:
2219 	put_task_struct(task);
2220 out_no_task:
2221 	return length;
2222 }
2223 
2224 static const struct file_operations proc_pid_attr_operations = {
2225 	.read		= proc_pid_attr_read,
2226 	.write		= proc_pid_attr_write,
2227 	.llseek		= generic_file_llseek,
2228 };
2229 
2230 static const struct pid_entry attr_dir_stuff[] = {
2231 	REG("current",    S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2232 	REG("prev",       S_IRUGO,	   proc_pid_attr_operations),
2233 	REG("exec",       S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2234 	REG("fscreate",   S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2235 	REG("keycreate",  S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2236 	REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2237 };
2238 
2239 static int proc_attr_dir_readdir(struct file * filp,
2240 			     void * dirent, filldir_t filldir)
2241 {
2242 	return proc_pident_readdir(filp,dirent,filldir,
2243 				   attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
2244 }
2245 
2246 static const struct file_operations proc_attr_dir_operations = {
2247 	.read		= generic_read_dir,
2248 	.readdir	= proc_attr_dir_readdir,
2249 	.llseek		= default_llseek,
2250 };
2251 
2252 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2253 				struct dentry *dentry, unsigned int flags)
2254 {
2255 	return proc_pident_lookup(dir, dentry,
2256 				  attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2257 }
2258 
2259 static const struct inode_operations proc_attr_dir_inode_operations = {
2260 	.lookup		= proc_attr_dir_lookup,
2261 	.getattr	= pid_getattr,
2262 	.setattr	= proc_setattr,
2263 };
2264 
2265 #endif
2266 
2267 #ifdef CONFIG_ELF_CORE
2268 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2269 					 size_t count, loff_t *ppos)
2270 {
2271 	struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2272 	struct mm_struct *mm;
2273 	char buffer[PROC_NUMBUF];
2274 	size_t len;
2275 	int ret;
2276 
2277 	if (!task)
2278 		return -ESRCH;
2279 
2280 	ret = 0;
2281 	mm = get_task_mm(task);
2282 	if (mm) {
2283 		len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2284 			       ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2285 				MMF_DUMP_FILTER_SHIFT));
2286 		mmput(mm);
2287 		ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2288 	}
2289 
2290 	put_task_struct(task);
2291 
2292 	return ret;
2293 }
2294 
2295 static ssize_t proc_coredump_filter_write(struct file *file,
2296 					  const char __user *buf,
2297 					  size_t count,
2298 					  loff_t *ppos)
2299 {
2300 	struct task_struct *task;
2301 	struct mm_struct *mm;
2302 	char buffer[PROC_NUMBUF], *end;
2303 	unsigned int val;
2304 	int ret;
2305 	int i;
2306 	unsigned long mask;
2307 
2308 	ret = -EFAULT;
2309 	memset(buffer, 0, sizeof(buffer));
2310 	if (count > sizeof(buffer) - 1)
2311 		count = sizeof(buffer) - 1;
2312 	if (copy_from_user(buffer, buf, count))
2313 		goto out_no_task;
2314 
2315 	ret = -EINVAL;
2316 	val = (unsigned int)simple_strtoul(buffer, &end, 0);
2317 	if (*end == '\n')
2318 		end++;
2319 	if (end - buffer == 0)
2320 		goto out_no_task;
2321 
2322 	ret = -ESRCH;
2323 	task = get_proc_task(file->f_dentry->d_inode);
2324 	if (!task)
2325 		goto out_no_task;
2326 
2327 	ret = end - buffer;
2328 	mm = get_task_mm(task);
2329 	if (!mm)
2330 		goto out_no_mm;
2331 
2332 	for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2333 		if (val & mask)
2334 			set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2335 		else
2336 			clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2337 	}
2338 
2339 	mmput(mm);
2340  out_no_mm:
2341 	put_task_struct(task);
2342  out_no_task:
2343 	return ret;
2344 }
2345 
2346 static const struct file_operations proc_coredump_filter_operations = {
2347 	.read		= proc_coredump_filter_read,
2348 	.write		= proc_coredump_filter_write,
2349 	.llseek		= generic_file_llseek,
2350 };
2351 #endif
2352 
2353 /*
2354  * /proc/self:
2355  */
2356 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2357 			      int buflen)
2358 {
2359 	struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2360 	pid_t tgid = task_tgid_nr_ns(current, ns);
2361 	char tmp[PROC_NUMBUF];
2362 	if (!tgid)
2363 		return -ENOENT;
2364 	sprintf(tmp, "%d", tgid);
2365 	return vfs_readlink(dentry,buffer,buflen,tmp);
2366 }
2367 
2368 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
2369 {
2370 	struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2371 	pid_t tgid = task_tgid_nr_ns(current, ns);
2372 	char *name = ERR_PTR(-ENOENT);
2373 	if (tgid) {
2374 		name = __getname();
2375 		if (!name)
2376 			name = ERR_PTR(-ENOMEM);
2377 		else
2378 			sprintf(name, "%d", tgid);
2379 	}
2380 	nd_set_link(nd, name);
2381 	return NULL;
2382 }
2383 
2384 static void proc_self_put_link(struct dentry *dentry, struct nameidata *nd,
2385 				void *cookie)
2386 {
2387 	char *s = nd_get_link(nd);
2388 	if (!IS_ERR(s))
2389 		__putname(s);
2390 }
2391 
2392 static const struct inode_operations proc_self_inode_operations = {
2393 	.readlink	= proc_self_readlink,
2394 	.follow_link	= proc_self_follow_link,
2395 	.put_link	= proc_self_put_link,
2396 };
2397 
2398 /*
2399  * proc base
2400  *
2401  * These are the directory entries in the root directory of /proc
2402  * that properly belong to the /proc filesystem, as they describe
2403  * describe something that is process related.
2404  */
2405 static const struct pid_entry proc_base_stuff[] = {
2406 	NOD("self", S_IFLNK|S_IRWXUGO,
2407 		&proc_self_inode_operations, NULL, {}),
2408 };
2409 
2410 static struct dentry *proc_base_instantiate(struct inode *dir,
2411 	struct dentry *dentry, struct task_struct *task, const void *ptr)
2412 {
2413 	const struct pid_entry *p = ptr;
2414 	struct inode *inode;
2415 	struct proc_inode *ei;
2416 	struct dentry *error;
2417 
2418 	/* Allocate the inode */
2419 	error = ERR_PTR(-ENOMEM);
2420 	inode = new_inode(dir->i_sb);
2421 	if (!inode)
2422 		goto out;
2423 
2424 	/* Initialize the inode */
2425 	ei = PROC_I(inode);
2426 	inode->i_ino = get_next_ino();
2427 	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2428 
2429 	/*
2430 	 * grab the reference to the task.
2431 	 */
2432 	ei->pid = get_task_pid(task, PIDTYPE_PID);
2433 	if (!ei->pid)
2434 		goto out_iput;
2435 
2436 	inode->i_mode = p->mode;
2437 	if (S_ISDIR(inode->i_mode))
2438 		set_nlink(inode, 2);
2439 	if (S_ISLNK(inode->i_mode))
2440 		inode->i_size = 64;
2441 	if (p->iop)
2442 		inode->i_op = p->iop;
2443 	if (p->fop)
2444 		inode->i_fop = p->fop;
2445 	ei->op = p->op;
2446 	d_add(dentry, inode);
2447 	error = NULL;
2448 out:
2449 	return error;
2450 out_iput:
2451 	iput(inode);
2452 	goto out;
2453 }
2454 
2455 static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2456 {
2457 	struct dentry *error;
2458 	struct task_struct *task = get_proc_task(dir);
2459 	const struct pid_entry *p, *last;
2460 
2461 	error = ERR_PTR(-ENOENT);
2462 
2463 	if (!task)
2464 		goto out_no_task;
2465 
2466 	/* Lookup the directory entry */
2467 	last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2468 	for (p = proc_base_stuff; p <= last; p++) {
2469 		if (p->len != dentry->d_name.len)
2470 			continue;
2471 		if (!memcmp(dentry->d_name.name, p->name, p->len))
2472 			break;
2473 	}
2474 	if (p > last)
2475 		goto out;
2476 
2477 	error = proc_base_instantiate(dir, dentry, task, p);
2478 
2479 out:
2480 	put_task_struct(task);
2481 out_no_task:
2482 	return error;
2483 }
2484 
2485 static int proc_base_fill_cache(struct file *filp, void *dirent,
2486 	filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2487 {
2488 	return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2489 				proc_base_instantiate, task, p);
2490 }
2491 
2492 #ifdef CONFIG_TASK_IO_ACCOUNTING
2493 static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
2494 {
2495 	struct task_io_accounting acct = task->ioac;
2496 	unsigned long flags;
2497 	int result;
2498 
2499 	result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2500 	if (result)
2501 		return result;
2502 
2503 	if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
2504 		result = -EACCES;
2505 		goto out_unlock;
2506 	}
2507 
2508 	if (whole && lock_task_sighand(task, &flags)) {
2509 		struct task_struct *t = task;
2510 
2511 		task_io_accounting_add(&acct, &task->signal->ioac);
2512 		while_each_thread(task, t)
2513 			task_io_accounting_add(&acct, &t->ioac);
2514 
2515 		unlock_task_sighand(task, &flags);
2516 	}
2517 	result = sprintf(buffer,
2518 			"rchar: %llu\n"
2519 			"wchar: %llu\n"
2520 			"syscr: %llu\n"
2521 			"syscw: %llu\n"
2522 			"read_bytes: %llu\n"
2523 			"write_bytes: %llu\n"
2524 			"cancelled_write_bytes: %llu\n",
2525 			(unsigned long long)acct.rchar,
2526 			(unsigned long long)acct.wchar,
2527 			(unsigned long long)acct.syscr,
2528 			(unsigned long long)acct.syscw,
2529 			(unsigned long long)acct.read_bytes,
2530 			(unsigned long long)acct.write_bytes,
2531 			(unsigned long long)acct.cancelled_write_bytes);
2532 out_unlock:
2533 	mutex_unlock(&task->signal->cred_guard_mutex);
2534 	return result;
2535 }
2536 
2537 static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2538 {
2539 	return do_io_accounting(task, buffer, 0);
2540 }
2541 
2542 static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2543 {
2544 	return do_io_accounting(task, buffer, 1);
2545 }
2546 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2547 
2548 #ifdef CONFIG_USER_NS
2549 static int proc_id_map_open(struct inode *inode, struct file *file,
2550 	struct seq_operations *seq_ops)
2551 {
2552 	struct user_namespace *ns = NULL;
2553 	struct task_struct *task;
2554 	struct seq_file *seq;
2555 	int ret = -EINVAL;
2556 
2557 	task = get_proc_task(inode);
2558 	if (task) {
2559 		rcu_read_lock();
2560 		ns = get_user_ns(task_cred_xxx(task, user_ns));
2561 		rcu_read_unlock();
2562 		put_task_struct(task);
2563 	}
2564 	if (!ns)
2565 		goto err;
2566 
2567 	ret = seq_open(file, seq_ops);
2568 	if (ret)
2569 		goto err_put_ns;
2570 
2571 	seq = file->private_data;
2572 	seq->private = ns;
2573 
2574 	return 0;
2575 err_put_ns:
2576 	put_user_ns(ns);
2577 err:
2578 	return ret;
2579 }
2580 
2581 static int proc_id_map_release(struct inode *inode, struct file *file)
2582 {
2583 	struct seq_file *seq = file->private_data;
2584 	struct user_namespace *ns = seq->private;
2585 	put_user_ns(ns);
2586 	return seq_release(inode, file);
2587 }
2588 
2589 static int proc_uid_map_open(struct inode *inode, struct file *file)
2590 {
2591 	return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2592 }
2593 
2594 static int proc_gid_map_open(struct inode *inode, struct file *file)
2595 {
2596 	return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2597 }
2598 
2599 static int proc_projid_map_open(struct inode *inode, struct file *file)
2600 {
2601 	return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2602 }
2603 
2604 static const struct file_operations proc_uid_map_operations = {
2605 	.open		= proc_uid_map_open,
2606 	.write		= proc_uid_map_write,
2607 	.read		= seq_read,
2608 	.llseek		= seq_lseek,
2609 	.release	= proc_id_map_release,
2610 };
2611 
2612 static const struct file_operations proc_gid_map_operations = {
2613 	.open		= proc_gid_map_open,
2614 	.write		= proc_gid_map_write,
2615 	.read		= seq_read,
2616 	.llseek		= seq_lseek,
2617 	.release	= proc_id_map_release,
2618 };
2619 
2620 static const struct file_operations proc_projid_map_operations = {
2621 	.open		= proc_projid_map_open,
2622 	.write		= proc_projid_map_write,
2623 	.read		= seq_read,
2624 	.llseek		= seq_lseek,
2625 	.release	= proc_id_map_release,
2626 };
2627 #endif /* CONFIG_USER_NS */
2628 
2629 static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2630 				struct pid *pid, struct task_struct *task)
2631 {
2632 	int err = lock_trace(task);
2633 	if (!err) {
2634 		seq_printf(m, "%08x\n", task->personality);
2635 		unlock_trace(task);
2636 	}
2637 	return err;
2638 }
2639 
2640 /*
2641  * Thread groups
2642  */
2643 static const struct file_operations proc_task_operations;
2644 static const struct inode_operations proc_task_inode_operations;
2645 
2646 static const struct pid_entry tgid_base_stuff[] = {
2647 	DIR("task",       S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2648 	DIR("fd",         S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2649 #ifdef CONFIG_CHECKPOINT_RESTORE
2650 	DIR("map_files",  S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
2651 #endif
2652 	DIR("fdinfo",     S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2653 	DIR("ns",	  S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
2654 #ifdef CONFIG_NET
2655 	DIR("net",        S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2656 #endif
2657 	REG("environ",    S_IRUSR, proc_environ_operations),
2658 	INF("auxv",       S_IRUSR, proc_pid_auxv),
2659 	ONE("status",     S_IRUGO, proc_pid_status),
2660 	ONE("personality", S_IRUGO, proc_pid_personality),
2661 	INF("limits",	  S_IRUGO, proc_pid_limits),
2662 #ifdef CONFIG_SCHED_DEBUG
2663 	REG("sched",      S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2664 #endif
2665 #ifdef CONFIG_SCHED_AUTOGROUP
2666 	REG("autogroup",  S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2667 #endif
2668 	REG("comm",      S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2669 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2670 	INF("syscall",    S_IRUGO, proc_pid_syscall),
2671 #endif
2672 	INF("cmdline",    S_IRUGO, proc_pid_cmdline),
2673 	ONE("stat",       S_IRUGO, proc_tgid_stat),
2674 	ONE("statm",      S_IRUGO, proc_pid_statm),
2675 	REG("maps",       S_IRUGO, proc_pid_maps_operations),
2676 #ifdef CONFIG_NUMA
2677 	REG("numa_maps",  S_IRUGO, proc_pid_numa_maps_operations),
2678 #endif
2679 	REG("mem",        S_IRUSR|S_IWUSR, proc_mem_operations),
2680 	LNK("cwd",        proc_cwd_link),
2681 	LNK("root",       proc_root_link),
2682 	LNK("exe",        proc_exe_link),
2683 	REG("mounts",     S_IRUGO, proc_mounts_operations),
2684 	REG("mountinfo",  S_IRUGO, proc_mountinfo_operations),
2685 	REG("mountstats", S_IRUSR, proc_mountstats_operations),
2686 #ifdef CONFIG_PROC_PAGE_MONITOR
2687 	REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2688 	REG("smaps",      S_IRUGO, proc_pid_smaps_operations),
2689 	REG("pagemap",    S_IRUGO, proc_pagemap_operations),
2690 #endif
2691 #ifdef CONFIG_SECURITY
2692 	DIR("attr",       S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
2693 #endif
2694 #ifdef CONFIG_KALLSYMS
2695 	INF("wchan",      S_IRUGO, proc_pid_wchan),
2696 #endif
2697 #ifdef CONFIG_STACKTRACE
2698 	ONE("stack",      S_IRUGO, proc_pid_stack),
2699 #endif
2700 #ifdef CONFIG_SCHEDSTATS
2701 	INF("schedstat",  S_IRUGO, proc_pid_schedstat),
2702 #endif
2703 #ifdef CONFIG_LATENCYTOP
2704 	REG("latency",  S_IRUGO, proc_lstats_operations),
2705 #endif
2706 #ifdef CONFIG_PROC_PID_CPUSET
2707 	REG("cpuset",     S_IRUGO, proc_cpuset_operations),
2708 #endif
2709 #ifdef CONFIG_CGROUPS
2710 	REG("cgroup",  S_IRUGO, proc_cgroup_operations),
2711 #endif
2712 	INF("oom_score",  S_IRUGO, proc_oom_score),
2713 	REG("oom_adj",    S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
2714 	REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
2715 #ifdef CONFIG_AUDITSYSCALL
2716 	REG("loginuid",   S_IWUSR|S_IRUGO, proc_loginuid_operations),
2717 	REG("sessionid",  S_IRUGO, proc_sessionid_operations),
2718 #endif
2719 #ifdef CONFIG_FAULT_INJECTION
2720 	REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2721 #endif
2722 #ifdef CONFIG_ELF_CORE
2723 	REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
2724 #endif
2725 #ifdef CONFIG_TASK_IO_ACCOUNTING
2726 	INF("io",	S_IRUSR, proc_tgid_io_accounting),
2727 #endif
2728 #ifdef CONFIG_HARDWALL
2729 	INF("hardwall",   S_IRUGO, proc_pid_hardwall),
2730 #endif
2731 #ifdef CONFIG_USER_NS
2732 	REG("uid_map",    S_IRUGO|S_IWUSR, proc_uid_map_operations),
2733 	REG("gid_map",    S_IRUGO|S_IWUSR, proc_gid_map_operations),
2734 	REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
2735 #endif
2736 };
2737 
2738 static int proc_tgid_base_readdir(struct file * filp,
2739 			     void * dirent, filldir_t filldir)
2740 {
2741 	return proc_pident_readdir(filp,dirent,filldir,
2742 				   tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2743 }
2744 
2745 static const struct file_operations proc_tgid_base_operations = {
2746 	.read		= generic_read_dir,
2747 	.readdir	= proc_tgid_base_readdir,
2748 	.llseek		= default_llseek,
2749 };
2750 
2751 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2752 {
2753 	return proc_pident_lookup(dir, dentry,
2754 				  tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2755 }
2756 
2757 static const struct inode_operations proc_tgid_base_inode_operations = {
2758 	.lookup		= proc_tgid_base_lookup,
2759 	.getattr	= pid_getattr,
2760 	.setattr	= proc_setattr,
2761 	.permission	= proc_pid_permission,
2762 };
2763 
2764 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
2765 {
2766 	struct dentry *dentry, *leader, *dir;
2767 	char buf[PROC_NUMBUF];
2768 	struct qstr name;
2769 
2770 	name.name = buf;
2771 	name.len = snprintf(buf, sizeof(buf), "%d", pid);
2772 	dentry = d_hash_and_lookup(mnt->mnt_root, &name);
2773 	if (dentry) {
2774 		shrink_dcache_parent(dentry);
2775 		d_drop(dentry);
2776 		dput(dentry);
2777 	}
2778 
2779 	name.name = buf;
2780 	name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2781 	leader = d_hash_and_lookup(mnt->mnt_root, &name);
2782 	if (!leader)
2783 		goto out;
2784 
2785 	name.name = "task";
2786 	name.len = strlen(name.name);
2787 	dir = d_hash_and_lookup(leader, &name);
2788 	if (!dir)
2789 		goto out_put_leader;
2790 
2791 	name.name = buf;
2792 	name.len = snprintf(buf, sizeof(buf), "%d", pid);
2793 	dentry = d_hash_and_lookup(dir, &name);
2794 	if (dentry) {
2795 		shrink_dcache_parent(dentry);
2796 		d_drop(dentry);
2797 		dput(dentry);
2798 	}
2799 
2800 	dput(dir);
2801 out_put_leader:
2802 	dput(leader);
2803 out:
2804 	return;
2805 }
2806 
2807 /**
2808  * proc_flush_task -  Remove dcache entries for @task from the /proc dcache.
2809  * @task: task that should be flushed.
2810  *
2811  * When flushing dentries from proc, one needs to flush them from global
2812  * proc (proc_mnt) and from all the namespaces' procs this task was seen
2813  * in. This call is supposed to do all of this job.
2814  *
2815  * Looks in the dcache for
2816  * /proc/@pid
2817  * /proc/@tgid/task/@pid
2818  * if either directory is present flushes it and all of it'ts children
2819  * from the dcache.
2820  *
2821  * It is safe and reasonable to cache /proc entries for a task until
2822  * that task exits.  After that they just clog up the dcache with
2823  * useless entries, possibly causing useful dcache entries to be
2824  * flushed instead.  This routine is proved to flush those useless
2825  * dcache entries at process exit time.
2826  *
2827  * NOTE: This routine is just an optimization so it does not guarantee
2828  *       that no dcache entries will exist at process exit time it
2829  *       just makes it very unlikely that any will persist.
2830  */
2831 
2832 void proc_flush_task(struct task_struct *task)
2833 {
2834 	int i;
2835 	struct pid *pid, *tgid;
2836 	struct upid *upid;
2837 
2838 	pid = task_pid(task);
2839 	tgid = task_tgid(task);
2840 
2841 	for (i = 0; i <= pid->level; i++) {
2842 		upid = &pid->numbers[i];
2843 		proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
2844 					tgid->numbers[i].nr);
2845 	}
2846 
2847 	upid = &pid->numbers[pid->level];
2848 	if (upid->nr == 1)
2849 		pid_ns_release_proc(upid->ns);
2850 }
2851 
2852 static struct dentry *proc_pid_instantiate(struct inode *dir,
2853 					   struct dentry * dentry,
2854 					   struct task_struct *task, const void *ptr)
2855 {
2856 	struct dentry *error = ERR_PTR(-ENOENT);
2857 	struct inode *inode;
2858 
2859 	inode = proc_pid_make_inode(dir->i_sb, task);
2860 	if (!inode)
2861 		goto out;
2862 
2863 	inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2864 	inode->i_op = &proc_tgid_base_inode_operations;
2865 	inode->i_fop = &proc_tgid_base_operations;
2866 	inode->i_flags|=S_IMMUTABLE;
2867 
2868 	set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
2869 						  ARRAY_SIZE(tgid_base_stuff)));
2870 
2871 	d_set_d_op(dentry, &pid_dentry_operations);
2872 
2873 	d_add(dentry, inode);
2874 	/* Close the race of the process dying before we return the dentry */
2875 	if (pid_revalidate(dentry, 0))
2876 		error = NULL;
2877 out:
2878 	return error;
2879 }
2880 
2881 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
2882 {
2883 	struct dentry *result;
2884 	struct task_struct *task;
2885 	unsigned tgid;
2886 	struct pid_namespace *ns;
2887 
2888 	result = proc_base_lookup(dir, dentry);
2889 	if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2890 		goto out;
2891 
2892 	tgid = name_to_int(dentry);
2893 	if (tgid == ~0U)
2894 		goto out;
2895 
2896 	ns = dentry->d_sb->s_fs_info;
2897 	rcu_read_lock();
2898 	task = find_task_by_pid_ns(tgid, ns);
2899 	if (task)
2900 		get_task_struct(task);
2901 	rcu_read_unlock();
2902 	if (!task)
2903 		goto out;
2904 
2905 	result = proc_pid_instantiate(dir, dentry, task, NULL);
2906 	put_task_struct(task);
2907 out:
2908 	return result;
2909 }
2910 
2911 /*
2912  * Find the first task with tgid >= tgid
2913  *
2914  */
2915 struct tgid_iter {
2916 	unsigned int tgid;
2917 	struct task_struct *task;
2918 };
2919 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2920 {
2921 	struct pid *pid;
2922 
2923 	if (iter.task)
2924 		put_task_struct(iter.task);
2925 	rcu_read_lock();
2926 retry:
2927 	iter.task = NULL;
2928 	pid = find_ge_pid(iter.tgid, ns);
2929 	if (pid) {
2930 		iter.tgid = pid_nr_ns(pid, ns);
2931 		iter.task = pid_task(pid, PIDTYPE_PID);
2932 		/* What we to know is if the pid we have find is the
2933 		 * pid of a thread_group_leader.  Testing for task
2934 		 * being a thread_group_leader is the obvious thing
2935 		 * todo but there is a window when it fails, due to
2936 		 * the pid transfer logic in de_thread.
2937 		 *
2938 		 * So we perform the straight forward test of seeing
2939 		 * if the pid we have found is the pid of a thread
2940 		 * group leader, and don't worry if the task we have
2941 		 * found doesn't happen to be a thread group leader.
2942 		 * As we don't care in the case of readdir.
2943 		 */
2944 		if (!iter.task || !has_group_leader_pid(iter.task)) {
2945 			iter.tgid += 1;
2946 			goto retry;
2947 		}
2948 		get_task_struct(iter.task);
2949 	}
2950 	rcu_read_unlock();
2951 	return iter;
2952 }
2953 
2954 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
2955 
2956 static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2957 	struct tgid_iter iter)
2958 {
2959 	char name[PROC_NUMBUF];
2960 	int len = snprintf(name, sizeof(name), "%d", iter.tgid);
2961 	return proc_fill_cache(filp, dirent, filldir, name, len,
2962 				proc_pid_instantiate, iter.task, NULL);
2963 }
2964 
2965 static int fake_filldir(void *buf, const char *name, int namelen,
2966 			loff_t offset, u64 ino, unsigned d_type)
2967 {
2968 	return 0;
2969 }
2970 
2971 /* for the /proc/ directory itself, after non-process stuff has been done */
2972 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2973 {
2974 	unsigned int nr;
2975 	struct task_struct *reaper;
2976 	struct tgid_iter iter;
2977 	struct pid_namespace *ns;
2978 	filldir_t __filldir;
2979 
2980 	if (filp->f_pos >= PID_MAX_LIMIT + TGID_OFFSET)
2981 		goto out_no_task;
2982 	nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2983 
2984 	reaper = get_proc_task(filp->f_path.dentry->d_inode);
2985 	if (!reaper)
2986 		goto out_no_task;
2987 
2988 	for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
2989 		const struct pid_entry *p = &proc_base_stuff[nr];
2990 		if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
2991 			goto out;
2992 	}
2993 
2994 	ns = filp->f_dentry->d_sb->s_fs_info;
2995 	iter.task = NULL;
2996 	iter.tgid = filp->f_pos - TGID_OFFSET;
2997 	for (iter = next_tgid(ns, iter);
2998 	     iter.task;
2999 	     iter.tgid += 1, iter = next_tgid(ns, iter)) {
3000 		if (has_pid_permissions(ns, iter.task, 2))
3001 			__filldir = filldir;
3002 		else
3003 			__filldir = fake_filldir;
3004 
3005 		filp->f_pos = iter.tgid + TGID_OFFSET;
3006 		if (proc_pid_fill_cache(filp, dirent, __filldir, iter) < 0) {
3007 			put_task_struct(iter.task);
3008 			goto out;
3009 		}
3010 	}
3011 	filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
3012 out:
3013 	put_task_struct(reaper);
3014 out_no_task:
3015 	return 0;
3016 }
3017 
3018 /*
3019  * Tasks
3020  */
3021 static const struct pid_entry tid_base_stuff[] = {
3022 	DIR("fd",        S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
3023 	DIR("fdinfo",    S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
3024 	DIR("ns",	 S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
3025 	REG("environ",   S_IRUSR, proc_environ_operations),
3026 	INF("auxv",      S_IRUSR, proc_pid_auxv),
3027 	ONE("status",    S_IRUGO, proc_pid_status),
3028 	ONE("personality", S_IRUGO, proc_pid_personality),
3029 	INF("limits",	 S_IRUGO, proc_pid_limits),
3030 #ifdef CONFIG_SCHED_DEBUG
3031 	REG("sched",     S_IRUGO|S_IWUSR, proc_pid_sched_operations),
3032 #endif
3033 	REG("comm",      S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
3034 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3035 	INF("syscall",   S_IRUGO, proc_pid_syscall),
3036 #endif
3037 	INF("cmdline",   S_IRUGO, proc_pid_cmdline),
3038 	ONE("stat",      S_IRUGO, proc_tid_stat),
3039 	ONE("statm",     S_IRUGO, proc_pid_statm),
3040 	REG("maps",      S_IRUGO, proc_tid_maps_operations),
3041 #ifdef CONFIG_CHECKPOINT_RESTORE
3042 	REG("children",  S_IRUGO, proc_tid_children_operations),
3043 #endif
3044 #ifdef CONFIG_NUMA
3045 	REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
3046 #endif
3047 	REG("mem",       S_IRUSR|S_IWUSR, proc_mem_operations),
3048 	LNK("cwd",       proc_cwd_link),
3049 	LNK("root",      proc_root_link),
3050 	LNK("exe",       proc_exe_link),
3051 	REG("mounts",    S_IRUGO, proc_mounts_operations),
3052 	REG("mountinfo",  S_IRUGO, proc_mountinfo_operations),
3053 #ifdef CONFIG_PROC_PAGE_MONITOR
3054 	REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3055 	REG("smaps",     S_IRUGO, proc_tid_smaps_operations),
3056 	REG("pagemap",    S_IRUGO, proc_pagemap_operations),
3057 #endif
3058 #ifdef CONFIG_SECURITY
3059 	DIR("attr",      S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
3060 #endif
3061 #ifdef CONFIG_KALLSYMS
3062 	INF("wchan",     S_IRUGO, proc_pid_wchan),
3063 #endif
3064 #ifdef CONFIG_STACKTRACE
3065 	ONE("stack",      S_IRUGO, proc_pid_stack),
3066 #endif
3067 #ifdef CONFIG_SCHEDSTATS
3068 	INF("schedstat", S_IRUGO, proc_pid_schedstat),
3069 #endif
3070 #ifdef CONFIG_LATENCYTOP
3071 	REG("latency",  S_IRUGO, proc_lstats_operations),
3072 #endif
3073 #ifdef CONFIG_PROC_PID_CPUSET
3074 	REG("cpuset",    S_IRUGO, proc_cpuset_operations),
3075 #endif
3076 #ifdef CONFIG_CGROUPS
3077 	REG("cgroup",  S_IRUGO, proc_cgroup_operations),
3078 #endif
3079 	INF("oom_score", S_IRUGO, proc_oom_score),
3080 	REG("oom_adj",   S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
3081 	REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
3082 #ifdef CONFIG_AUDITSYSCALL
3083 	REG("loginuid",  S_IWUSR|S_IRUGO, proc_loginuid_operations),
3084 	REG("sessionid",  S_IRUGO, proc_sessionid_operations),
3085 #endif
3086 #ifdef CONFIG_FAULT_INJECTION
3087 	REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3088 #endif
3089 #ifdef CONFIG_TASK_IO_ACCOUNTING
3090 	INF("io",	S_IRUSR, proc_tid_io_accounting),
3091 #endif
3092 #ifdef CONFIG_HARDWALL
3093 	INF("hardwall",   S_IRUGO, proc_pid_hardwall),
3094 #endif
3095 #ifdef CONFIG_USER_NS
3096 	REG("uid_map",    S_IRUGO|S_IWUSR, proc_uid_map_operations),
3097 	REG("gid_map",    S_IRUGO|S_IWUSR, proc_gid_map_operations),
3098 	REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
3099 #endif
3100 };
3101 
3102 static int proc_tid_base_readdir(struct file * filp,
3103 			     void * dirent, filldir_t filldir)
3104 {
3105 	return proc_pident_readdir(filp,dirent,filldir,
3106 				   tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
3107 }
3108 
3109 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3110 {
3111 	return proc_pident_lookup(dir, dentry,
3112 				  tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3113 }
3114 
3115 static const struct file_operations proc_tid_base_operations = {
3116 	.read		= generic_read_dir,
3117 	.readdir	= proc_tid_base_readdir,
3118 	.llseek		= default_llseek,
3119 };
3120 
3121 static const struct inode_operations proc_tid_base_inode_operations = {
3122 	.lookup		= proc_tid_base_lookup,
3123 	.getattr	= pid_getattr,
3124 	.setattr	= proc_setattr,
3125 };
3126 
3127 static struct dentry *proc_task_instantiate(struct inode *dir,
3128 	struct dentry *dentry, struct task_struct *task, const void *ptr)
3129 {
3130 	struct dentry *error = ERR_PTR(-ENOENT);
3131 	struct inode *inode;
3132 	inode = proc_pid_make_inode(dir->i_sb, task);
3133 
3134 	if (!inode)
3135 		goto out;
3136 	inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3137 	inode->i_op = &proc_tid_base_inode_operations;
3138 	inode->i_fop = &proc_tid_base_operations;
3139 	inode->i_flags|=S_IMMUTABLE;
3140 
3141 	set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3142 						  ARRAY_SIZE(tid_base_stuff)));
3143 
3144 	d_set_d_op(dentry, &pid_dentry_operations);
3145 
3146 	d_add(dentry, inode);
3147 	/* Close the race of the process dying before we return the dentry */
3148 	if (pid_revalidate(dentry, 0))
3149 		error = NULL;
3150 out:
3151 	return error;
3152 }
3153 
3154 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
3155 {
3156 	struct dentry *result = ERR_PTR(-ENOENT);
3157 	struct task_struct *task;
3158 	struct task_struct *leader = get_proc_task(dir);
3159 	unsigned tid;
3160 	struct pid_namespace *ns;
3161 
3162 	if (!leader)
3163 		goto out_no_task;
3164 
3165 	tid = name_to_int(dentry);
3166 	if (tid == ~0U)
3167 		goto out;
3168 
3169 	ns = dentry->d_sb->s_fs_info;
3170 	rcu_read_lock();
3171 	task = find_task_by_pid_ns(tid, ns);
3172 	if (task)
3173 		get_task_struct(task);
3174 	rcu_read_unlock();
3175 	if (!task)
3176 		goto out;
3177 	if (!same_thread_group(leader, task))
3178 		goto out_drop_task;
3179 
3180 	result = proc_task_instantiate(dir, dentry, task, NULL);
3181 out_drop_task:
3182 	put_task_struct(task);
3183 out:
3184 	put_task_struct(leader);
3185 out_no_task:
3186 	return result;
3187 }
3188 
3189 /*
3190  * Find the first tid of a thread group to return to user space.
3191  *
3192  * Usually this is just the thread group leader, but if the users
3193  * buffer was too small or there was a seek into the middle of the
3194  * directory we have more work todo.
3195  *
3196  * In the case of a short read we start with find_task_by_pid.
3197  *
3198  * In the case of a seek we start with the leader and walk nr
3199  * threads past it.
3200  */
3201 static struct task_struct *first_tid(struct task_struct *leader,
3202 		int tid, int nr, struct pid_namespace *ns)
3203 {
3204 	struct task_struct *pos;
3205 
3206 	rcu_read_lock();
3207 	/* Attempt to start with the pid of a thread */
3208 	if (tid && (nr > 0)) {
3209 		pos = find_task_by_pid_ns(tid, ns);
3210 		if (pos && (pos->group_leader == leader))
3211 			goto found;
3212 	}
3213 
3214 	/* If nr exceeds the number of threads there is nothing todo */
3215 	pos = NULL;
3216 	if (nr && nr >= get_nr_threads(leader))
3217 		goto out;
3218 
3219 	/* If we haven't found our starting place yet start
3220 	 * with the leader and walk nr threads forward.
3221 	 */
3222 	for (pos = leader; nr > 0; --nr) {
3223 		pos = next_thread(pos);
3224 		if (pos == leader) {
3225 			pos = NULL;
3226 			goto out;
3227 		}
3228 	}
3229 found:
3230 	get_task_struct(pos);
3231 out:
3232 	rcu_read_unlock();
3233 	return pos;
3234 }
3235 
3236 /*
3237  * Find the next thread in the thread list.
3238  * Return NULL if there is an error or no next thread.
3239  *
3240  * The reference to the input task_struct is released.
3241  */
3242 static struct task_struct *next_tid(struct task_struct *start)
3243 {
3244 	struct task_struct *pos = NULL;
3245 	rcu_read_lock();
3246 	if (pid_alive(start)) {
3247 		pos = next_thread(start);
3248 		if (thread_group_leader(pos))
3249 			pos = NULL;
3250 		else
3251 			get_task_struct(pos);
3252 	}
3253 	rcu_read_unlock();
3254 	put_task_struct(start);
3255 	return pos;
3256 }
3257 
3258 static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3259 	struct task_struct *task, int tid)
3260 {
3261 	char name[PROC_NUMBUF];
3262 	int len = snprintf(name, sizeof(name), "%d", tid);
3263 	return proc_fill_cache(filp, dirent, filldir, name, len,
3264 				proc_task_instantiate, task, NULL);
3265 }
3266 
3267 /* for the /proc/TGID/task/ directories */
3268 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
3269 {
3270 	struct dentry *dentry = filp->f_path.dentry;
3271 	struct inode *inode = dentry->d_inode;
3272 	struct task_struct *leader = NULL;
3273 	struct task_struct *task;
3274 	int retval = -ENOENT;
3275 	ino_t ino;
3276 	int tid;
3277 	struct pid_namespace *ns;
3278 
3279 	task = get_proc_task(inode);
3280 	if (!task)
3281 		goto out_no_task;
3282 	rcu_read_lock();
3283 	if (pid_alive(task)) {
3284 		leader = task->group_leader;
3285 		get_task_struct(leader);
3286 	}
3287 	rcu_read_unlock();
3288 	put_task_struct(task);
3289 	if (!leader)
3290 		goto out_no_task;
3291 	retval = 0;
3292 
3293 	switch ((unsigned long)filp->f_pos) {
3294 	case 0:
3295 		ino = inode->i_ino;
3296 		if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) < 0)
3297 			goto out;
3298 		filp->f_pos++;
3299 		/* fall through */
3300 	case 1:
3301 		ino = parent_ino(dentry);
3302 		if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) < 0)
3303 			goto out;
3304 		filp->f_pos++;
3305 		/* fall through */
3306 	}
3307 
3308 	/* f_version caches the tgid value that the last readdir call couldn't
3309 	 * return. lseek aka telldir automagically resets f_version to 0.
3310 	 */
3311 	ns = filp->f_dentry->d_sb->s_fs_info;
3312 	tid = (int)filp->f_version;
3313 	filp->f_version = 0;
3314 	for (task = first_tid(leader, tid, filp->f_pos - 2, ns);
3315 	     task;
3316 	     task = next_tid(task), filp->f_pos++) {
3317 		tid = task_pid_nr_ns(task, ns);
3318 		if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
3319 			/* returning this tgid failed, save it as the first
3320 			 * pid for the next readir call */
3321 			filp->f_version = (u64)tid;
3322 			put_task_struct(task);
3323 			break;
3324 		}
3325 	}
3326 out:
3327 	put_task_struct(leader);
3328 out_no_task:
3329 	return retval;
3330 }
3331 
3332 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3333 {
3334 	struct inode *inode = dentry->d_inode;
3335 	struct task_struct *p = get_proc_task(inode);
3336 	generic_fillattr(inode, stat);
3337 
3338 	if (p) {
3339 		stat->nlink += get_nr_threads(p);
3340 		put_task_struct(p);
3341 	}
3342 
3343 	return 0;
3344 }
3345 
3346 static const struct inode_operations proc_task_inode_operations = {
3347 	.lookup		= proc_task_lookup,
3348 	.getattr	= proc_task_getattr,
3349 	.setattr	= proc_setattr,
3350 	.permission	= proc_pid_permission,
3351 };
3352 
3353 static const struct file_operations proc_task_operations = {
3354 	.read		= generic_read_dir,
3355 	.readdir	= proc_task_readdir,
3356 	.llseek		= default_llseek,
3357 };
3358