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