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