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