1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 1991, 1992 Linus Torvalds
7 * proc base directory handling functions
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.
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>
25 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
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.
36 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
37 * Pud inclusion in the page table walking.
41 * 10LE Instituto Nokia de Tecnologia - INdT:
42 * A better way to walks through the page table as suggested by Hugh Dickins.
44 * Simo Piiroinen <simo.piiroinen@nokia.com>:
45 * Smaps information related to shared, private, clean and dirty pages.
47 * Paul Mundt <paul.mundt@nokia.com>:
48 * Overall revision about smaps.
51 #include <linux/uaccess.h>
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/string.h>
63 #include <linux/seq_file.h>
64 #include <linux/namei.h>
65 #include <linux/mnt_namespace.h>
67 #include <linux/swap.h>
68 #include <linux/rcupdate.h>
69 #include <linux/kallsyms.h>
70 #include <linux/stacktrace.h>
71 #include <linux/resource.h>
72 #include <linux/module.h>
73 #include <linux/mount.h>
74 #include <linux/security.h>
75 #include <linux/ptrace.h>
76 #include <linux/tracehook.h>
77 #include <linux/printk.h>
78 #include <linux/cache.h>
79 #include <linux/cgroup.h>
80 #include <linux/cpuset.h>
81 #include <linux/audit.h>
82 #include <linux/poll.h>
83 #include <linux/nsproxy.h>
84 #include <linux/oom.h>
85 #include <linux/elf.h>
86 #include <linux/pid_namespace.h>
87 #include <linux/user_namespace.h>
88 #include <linux/fs_struct.h>
89 #include <linux/slab.h>
90 #include <linux/sched/autogroup.h>
91 #include <linux/sched/mm.h>
92 #include <linux/sched/coredump.h>
93 #include <linux/sched/debug.h>
94 #include <linux/sched/stat.h>
95 #include <linux/flex_array.h>
96 #include <linux/posix-timers.h>
97 #include <trace/events/oom.h>
101 #include "../../lib/kstrtox.h"
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.
109 * The classic example of a problem is opening file descriptors
110 * in /proc for a task before it execs a suid executable.
113 static u8 nlink_tid __ro_after_init;
114 static u8 nlink_tgid __ro_after_init;
120 const struct inode_operations *iop;
121 const struct file_operations *fop;
125 #define NOD(NAME, MODE, IOP, FOP, OP) { \
127 .len = sizeof(NAME) - 1, \
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 } )
148 * Count the number of hardlinks for the pid_entry table, excluding the .
151 static unsigned int __init pid_entry_nlink(const struct pid_entry *entries,
158 for (i = 0; i < n; ++i) {
159 if (S_ISDIR(entries[i].mode))
166 static int get_task_root(struct task_struct *task, struct path *root)
168 int result = -ENOENT;
172 get_fs_root(task->fs, root);
179 static int proc_cwd_link(struct dentry *dentry, struct path *path)
181 struct task_struct *task = get_proc_task(d_inode(dentry));
182 int result = -ENOENT;
187 get_fs_pwd(task->fs, path);
191 put_task_struct(task);
196 static int proc_root_link(struct dentry *dentry, struct path *path)
198 struct task_struct *task = get_proc_task(d_inode(dentry));
199 int result = -ENOENT;
202 result = get_task_root(task, path);
203 put_task_struct(task);
208 static ssize_t get_mm_cmdline(struct mm_struct *mm, char __user *buf,
209 size_t count, loff_t *ppos)
211 unsigned long arg_start, arg_end, env_start, env_end;
212 unsigned long pos, len;
215 /* Check if process spawned far enough to have cmdline. */
219 spin_lock(&mm->arg_lock);
220 arg_start = mm->arg_start;
221 arg_end = mm->arg_end;
222 env_start = mm->env_start;
223 env_end = mm->env_end;
224 spin_unlock(&mm->arg_lock);
226 if (arg_start >= arg_end)
230 * We have traditionally allowed the user to re-write
231 * the argument strings and overflow the end result
232 * into the environment section. But only do that if
233 * the environment area is contiguous to the arguments.
235 if (env_start != arg_end || env_start >= env_end)
236 env_start = env_end = arg_end;
238 /* .. and limit it to a maximum of one page of slop */
239 if (env_end >= arg_end + PAGE_SIZE)
240 env_end = arg_end + PAGE_SIZE - 1;
242 /* We're not going to care if "*ppos" has high bits set */
243 pos = arg_start + *ppos;
245 /* .. but we do check the result is in the proper range */
246 if (pos < arg_start || pos >= env_end)
249 /* .. and we never go past env_end */
250 if (env_end - pos < count)
251 count = env_end - pos;
253 page = (char *)__get_free_page(GFP_KERNEL);
260 size_t size = min_t(size_t, PAGE_SIZE, count);
264 * Are we already starting past the official end?
265 * We always include the last byte that is *supposed*
268 offset = (pos >= arg_end) ? pos - arg_end + 1 : 0;
270 got = access_remote_vm(mm, pos - offset, page, size + offset, FOLL_ANON);
275 /* Don't walk past a NUL character once you hit arg_end */
276 if (pos + got >= arg_end) {
280 * If we started before 'arg_end' but ended up
281 * at or after it, we start the NUL character
282 * check at arg_end-1 (where we expect the normal
285 * NOTE! This is smaller than 'got', because
286 * pos + got >= arg_end
289 n = arg_end - pos - 1;
291 /* Cut off at first NUL after 'n' */
292 got = n + strnlen(page+n, offset+got-n);
297 /* Include the NUL if it existed */
302 got -= copy_to_user(buf, page+offset, got);
303 if (unlikely(!got)) {
314 free_page((unsigned long)page);
318 static ssize_t get_task_cmdline(struct task_struct *tsk, char __user *buf,
319 size_t count, loff_t *pos)
321 struct mm_struct *mm;
324 mm = get_task_mm(tsk);
328 ret = get_mm_cmdline(mm, buf, count, pos);
333 static ssize_t proc_pid_cmdline_read(struct file *file, char __user *buf,
334 size_t count, loff_t *pos)
336 struct task_struct *tsk;
341 tsk = get_proc_task(file_inode(file));
344 ret = get_task_cmdline(tsk, buf, count, pos);
345 put_task_struct(tsk);
351 static const struct file_operations proc_pid_cmdline_ops = {
352 .read = proc_pid_cmdline_read,
353 .llseek = generic_file_llseek,
356 #ifdef CONFIG_KALLSYMS
358 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
359 * Returns the resolved symbol. If that fails, simply return the address.
361 static int proc_pid_wchan(struct seq_file *m, struct pid_namespace *ns,
362 struct pid *pid, struct task_struct *task)
365 char symname[KSYM_NAME_LEN];
367 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
370 wchan = get_wchan(task);
371 if (wchan && !lookup_symbol_name(wchan, symname)) {
372 seq_puts(m, symname);
380 #endif /* CONFIG_KALLSYMS */
382 static int lock_trace(struct task_struct *task)
384 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
387 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH_FSCREDS)) {
388 mutex_unlock(&task->signal->cred_guard_mutex);
394 static void unlock_trace(struct task_struct *task)
396 mutex_unlock(&task->signal->cred_guard_mutex);
399 #ifdef CONFIG_STACKTRACE
401 #define MAX_STACK_TRACE_DEPTH 64
403 static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
404 struct pid *pid, struct task_struct *task)
406 struct stack_trace trace;
407 unsigned long *entries;
410 entries = kmalloc_array(MAX_STACK_TRACE_DEPTH, sizeof(*entries),
415 trace.nr_entries = 0;
416 trace.max_entries = MAX_STACK_TRACE_DEPTH;
417 trace.entries = entries;
420 err = lock_trace(task);
424 save_stack_trace_tsk(task, &trace);
426 for (i = 0; i < trace.nr_entries; i++) {
427 seq_printf(m, "[<0>] %pB\n", (void *)entries[i]);
437 #ifdef CONFIG_SCHED_INFO
439 * Provides /proc/PID/schedstat
441 static int proc_pid_schedstat(struct seq_file *m, struct pid_namespace *ns,
442 struct pid *pid, struct task_struct *task)
444 if (unlikely(!sched_info_on()))
445 seq_printf(m, "0 0 0\n");
447 seq_printf(m, "%llu %llu %lu\n",
448 (unsigned long long)task->se.sum_exec_runtime,
449 (unsigned long long)task->sched_info.run_delay,
450 task->sched_info.pcount);
456 #ifdef CONFIG_LATENCYTOP
457 static int lstats_show_proc(struct seq_file *m, void *v)
460 struct inode *inode = m->private;
461 struct task_struct *task = get_proc_task(inode);
465 seq_puts(m, "Latency Top version : v0.1\n");
466 for (i = 0; i < 32; i++) {
467 struct latency_record *lr = &task->latency_record[i];
468 if (lr->backtrace[0]) {
470 seq_printf(m, "%i %li %li",
471 lr->count, lr->time, lr->max);
472 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
473 unsigned long bt = lr->backtrace[q];
478 seq_printf(m, " %ps", (void *)bt);
484 put_task_struct(task);
488 static int lstats_open(struct inode *inode, struct file *file)
490 return single_open(file, lstats_show_proc, inode);
493 static ssize_t lstats_write(struct file *file, const char __user *buf,
494 size_t count, loff_t *offs)
496 struct task_struct *task = get_proc_task(file_inode(file));
500 clear_all_latency_tracing(task);
501 put_task_struct(task);
506 static const struct file_operations proc_lstats_operations = {
509 .write = lstats_write,
511 .release = single_release,
516 static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns,
517 struct pid *pid, struct task_struct *task)
519 unsigned long totalpages = totalram_pages + total_swap_pages;
520 unsigned long points = 0;
522 points = oom_badness(task, NULL, NULL, totalpages) *
524 seq_printf(m, "%lu\n", points);
534 static const struct limit_names lnames[RLIM_NLIMITS] = {
535 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
536 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
537 [RLIMIT_DATA] = {"Max data size", "bytes"},
538 [RLIMIT_STACK] = {"Max stack size", "bytes"},
539 [RLIMIT_CORE] = {"Max core file size", "bytes"},
540 [RLIMIT_RSS] = {"Max resident set", "bytes"},
541 [RLIMIT_NPROC] = {"Max processes", "processes"},
542 [RLIMIT_NOFILE] = {"Max open files", "files"},
543 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
544 [RLIMIT_AS] = {"Max address space", "bytes"},
545 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
546 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
547 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
548 [RLIMIT_NICE] = {"Max nice priority", NULL},
549 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
550 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
553 /* Display limits for a process */
554 static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
555 struct pid *pid, struct task_struct *task)
560 struct rlimit rlim[RLIM_NLIMITS];
562 if (!lock_task_sighand(task, &flags))
564 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
565 unlock_task_sighand(task, &flags);
568 * print the file header
570 seq_printf(m, "%-25s %-20s %-20s %-10s\n",
571 "Limit", "Soft Limit", "Hard Limit", "Units");
573 for (i = 0; i < RLIM_NLIMITS; i++) {
574 if (rlim[i].rlim_cur == RLIM_INFINITY)
575 seq_printf(m, "%-25s %-20s ",
576 lnames[i].name, "unlimited");
578 seq_printf(m, "%-25s %-20lu ",
579 lnames[i].name, rlim[i].rlim_cur);
581 if (rlim[i].rlim_max == RLIM_INFINITY)
582 seq_printf(m, "%-20s ", "unlimited");
584 seq_printf(m, "%-20lu ", rlim[i].rlim_max);
587 seq_printf(m, "%-10s\n", lnames[i].unit);
595 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
596 static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
597 struct pid *pid, struct task_struct *task)
600 unsigned long args[6], sp, pc;
603 res = lock_trace(task);
607 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
608 seq_puts(m, "running\n");
610 seq_printf(m, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
613 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
615 args[0], args[1], args[2], args[3], args[4], args[5],
621 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
623 /************************************************************************/
624 /* Here the fs part begins */
625 /************************************************************************/
627 /* permission checks */
628 static int proc_fd_access_allowed(struct inode *inode)
630 struct task_struct *task;
632 /* Allow access to a task's file descriptors if it is us or we
633 * may use ptrace attach to the process and find out that
636 task = get_proc_task(inode);
638 allowed = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
639 put_task_struct(task);
644 int proc_setattr(struct dentry *dentry, struct iattr *attr)
647 struct inode *inode = d_inode(dentry);
649 if (attr->ia_valid & ATTR_MODE)
652 error = setattr_prepare(dentry, attr);
656 setattr_copy(inode, attr);
657 mark_inode_dirty(inode);
662 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
663 * or euid/egid (for hide_pid_min=2)?
665 static bool has_pid_permissions(struct pid_namespace *pid,
666 struct task_struct *task,
669 if (pid->hide_pid < hide_pid_min)
671 if (in_group_p(pid->pid_gid))
673 return ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
677 static int proc_pid_permission(struct inode *inode, int mask)
679 struct pid_namespace *pid = proc_pid_ns(inode);
680 struct task_struct *task;
683 task = get_proc_task(inode);
686 has_perms = has_pid_permissions(pid, task, HIDEPID_NO_ACCESS);
687 put_task_struct(task);
690 if (pid->hide_pid == HIDEPID_INVISIBLE) {
692 * Let's make getdents(), stat(), and open()
693 * consistent with each other. If a process
694 * may not stat() a file, it shouldn't be seen
702 return generic_permission(inode, mask);
707 static const struct inode_operations proc_def_inode_operations = {
708 .setattr = proc_setattr,
711 static int proc_single_show(struct seq_file *m, void *v)
713 struct inode *inode = m->private;
714 struct pid_namespace *ns = proc_pid_ns(inode);
715 struct pid *pid = proc_pid(inode);
716 struct task_struct *task;
719 task = get_pid_task(pid, PIDTYPE_PID);
723 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
725 put_task_struct(task);
729 static int proc_single_open(struct inode *inode, struct file *filp)
731 return single_open(filp, proc_single_show, inode);
734 static const struct file_operations proc_single_file_operations = {
735 .open = proc_single_open,
738 .release = single_release,
742 struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode)
744 struct task_struct *task = get_proc_task(inode);
745 struct mm_struct *mm = ERR_PTR(-ESRCH);
748 mm = mm_access(task, mode | PTRACE_MODE_FSCREDS);
749 put_task_struct(task);
751 if (!IS_ERR_OR_NULL(mm)) {
752 /* ensure this mm_struct can't be freed */
754 /* but do not pin its memory */
762 static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
764 struct mm_struct *mm = proc_mem_open(inode, mode);
769 file->private_data = mm;
773 static int mem_open(struct inode *inode, struct file *file)
775 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
777 /* OK to pass negative loff_t, we can catch out-of-range */
778 file->f_mode |= FMODE_UNSIGNED_OFFSET;
783 static ssize_t mem_rw(struct file *file, char __user *buf,
784 size_t count, loff_t *ppos, int write)
786 struct mm_struct *mm = file->private_data;
787 unsigned long addr = *ppos;
795 page = (char *)__get_free_page(GFP_KERNEL);
800 if (!mmget_not_zero(mm))
803 flags = FOLL_FORCE | (write ? FOLL_WRITE : 0);
806 int this_len = min_t(int, count, PAGE_SIZE);
808 if (write && copy_from_user(page, buf, this_len)) {
813 this_len = access_remote_vm(mm, addr, page, this_len, flags);
820 if (!write && copy_to_user(buf, page, this_len)) {
834 free_page((unsigned long) page);
838 static ssize_t mem_read(struct file *file, char __user *buf,
839 size_t count, loff_t *ppos)
841 return mem_rw(file, buf, count, ppos, 0);
844 static ssize_t mem_write(struct file *file, const char __user *buf,
845 size_t count, loff_t *ppos)
847 return mem_rw(file, (char __user*)buf, count, ppos, 1);
850 loff_t mem_lseek(struct file *file, loff_t offset, int orig)
854 file->f_pos = offset;
857 file->f_pos += offset;
862 force_successful_syscall_return();
866 static int mem_release(struct inode *inode, struct file *file)
868 struct mm_struct *mm = file->private_data;
874 static const struct file_operations proc_mem_operations = {
879 .release = mem_release,
882 static int environ_open(struct inode *inode, struct file *file)
884 return __mem_open(inode, file, PTRACE_MODE_READ);
887 static ssize_t environ_read(struct file *file, char __user *buf,
888 size_t count, loff_t *ppos)
891 unsigned long src = *ppos;
893 struct mm_struct *mm = file->private_data;
894 unsigned long env_start, env_end;
896 /* Ensure the process spawned far enough to have an environment. */
897 if (!mm || !mm->env_end)
900 page = (char *)__get_free_page(GFP_KERNEL);
905 if (!mmget_not_zero(mm))
908 spin_lock(&mm->arg_lock);
909 env_start = mm->env_start;
910 env_end = mm->env_end;
911 spin_unlock(&mm->arg_lock);
914 size_t this_len, max_len;
917 if (src >= (env_end - env_start))
920 this_len = env_end - (env_start + src);
922 max_len = min_t(size_t, PAGE_SIZE, count);
923 this_len = min(max_len, this_len);
925 retval = access_remote_vm(mm, (env_start + src), page, this_len, FOLL_ANON);
932 if (copy_to_user(buf, page, retval)) {
946 free_page((unsigned long) page);
950 static const struct file_operations proc_environ_operations = {
951 .open = environ_open,
952 .read = environ_read,
953 .llseek = generic_file_llseek,
954 .release = mem_release,
957 static int auxv_open(struct inode *inode, struct file *file)
959 return __mem_open(inode, file, PTRACE_MODE_READ_FSCREDS);
962 static ssize_t auxv_read(struct file *file, char __user *buf,
963 size_t count, loff_t *ppos)
965 struct mm_struct *mm = file->private_data;
966 unsigned int nwords = 0;
972 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
973 return simple_read_from_buffer(buf, count, ppos, mm->saved_auxv,
974 nwords * sizeof(mm->saved_auxv[0]));
977 static const struct file_operations proc_auxv_operations = {
980 .llseek = generic_file_llseek,
981 .release = mem_release,
984 static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
987 struct task_struct *task = get_proc_task(file_inode(file));
988 char buffer[PROC_NUMBUF];
989 int oom_adj = OOM_ADJUST_MIN;
994 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
995 oom_adj = OOM_ADJUST_MAX;
997 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
999 put_task_struct(task);
1000 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
1001 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1004 static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
1006 static DEFINE_MUTEX(oom_adj_mutex);
1007 struct mm_struct *mm = NULL;
1008 struct task_struct *task;
1011 task = get_proc_task(file_inode(file));
1015 mutex_lock(&oom_adj_mutex);
1017 if (oom_adj < task->signal->oom_score_adj &&
1018 !capable(CAP_SYS_RESOURCE)) {
1023 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
1024 * /proc/pid/oom_score_adj instead.
1026 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
1027 current->comm, task_pid_nr(current), task_pid_nr(task),
1030 if ((short)oom_adj < task->signal->oom_score_adj_min &&
1031 !capable(CAP_SYS_RESOURCE)) {
1038 * Make sure we will check other processes sharing the mm if this is
1039 * not vfrok which wants its own oom_score_adj.
1040 * pin the mm so it doesn't go away and get reused after task_unlock
1042 if (!task->vfork_done) {
1043 struct task_struct *p = find_lock_task_mm(task);
1046 if (atomic_read(&p->mm->mm_users) > 1) {
1054 task->signal->oom_score_adj = oom_adj;
1055 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1056 task->signal->oom_score_adj_min = (short)oom_adj;
1057 trace_oom_score_adj_update(task);
1060 struct task_struct *p;
1063 for_each_process(p) {
1064 if (same_thread_group(task, p))
1067 /* do not touch kernel threads or the global init */
1068 if (p->flags & PF_KTHREAD || is_global_init(p))
1072 if (!p->vfork_done && process_shares_mm(p, mm)) {
1073 pr_info("updating oom_score_adj for %d (%s) from %d to %d because it shares mm with %d (%s). Report if this is unexpected.\n",
1074 task_pid_nr(p), p->comm,
1075 p->signal->oom_score_adj, oom_adj,
1076 task_pid_nr(task), task->comm);
1077 p->signal->oom_score_adj = oom_adj;
1078 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1079 p->signal->oom_score_adj_min = (short)oom_adj;
1087 mutex_unlock(&oom_adj_mutex);
1088 put_task_struct(task);
1093 * /proc/pid/oom_adj exists solely for backwards compatibility with previous
1094 * kernels. The effective policy is defined by oom_score_adj, which has a
1095 * different scale: oom_adj grew exponentially and oom_score_adj grows linearly.
1096 * Values written to oom_adj are simply mapped linearly to oom_score_adj.
1097 * Processes that become oom disabled via oom_adj will still be oom disabled
1098 * with this implementation.
1100 * oom_adj cannot be removed since existing userspace binaries use it.
1102 static ssize_t oom_adj_write(struct file *file, const char __user *buf,
1103 size_t count, loff_t *ppos)
1105 char buffer[PROC_NUMBUF];
1109 memset(buffer, 0, sizeof(buffer));
1110 if (count > sizeof(buffer) - 1)
1111 count = sizeof(buffer) - 1;
1112 if (copy_from_user(buffer, buf, count)) {
1117 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
1120 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
1121 oom_adj != OOM_DISABLE) {
1127 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1128 * value is always attainable.
1130 if (oom_adj == OOM_ADJUST_MAX)
1131 oom_adj = OOM_SCORE_ADJ_MAX;
1133 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
1135 err = __set_oom_adj(file, oom_adj, true);
1137 return err < 0 ? err : count;
1140 static const struct file_operations proc_oom_adj_operations = {
1141 .read = oom_adj_read,
1142 .write = oom_adj_write,
1143 .llseek = generic_file_llseek,
1146 static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1147 size_t count, loff_t *ppos)
1149 struct task_struct *task = get_proc_task(file_inode(file));
1150 char buffer[PROC_NUMBUF];
1151 short oom_score_adj = OOM_SCORE_ADJ_MIN;
1156 oom_score_adj = task->signal->oom_score_adj;
1157 put_task_struct(task);
1158 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
1159 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1162 static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1163 size_t count, loff_t *ppos)
1165 char buffer[PROC_NUMBUF];
1169 memset(buffer, 0, sizeof(buffer));
1170 if (count > sizeof(buffer) - 1)
1171 count = sizeof(buffer) - 1;
1172 if (copy_from_user(buffer, buf, count)) {
1177 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
1180 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
1181 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1186 err = __set_oom_adj(file, oom_score_adj, false);
1188 return err < 0 ? err : count;
1191 static const struct file_operations proc_oom_score_adj_operations = {
1192 .read = oom_score_adj_read,
1193 .write = oom_score_adj_write,
1194 .llseek = default_llseek,
1197 #ifdef CONFIG_AUDITSYSCALL
1198 #define TMPBUFLEN 11
1199 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1200 size_t count, loff_t *ppos)
1202 struct inode * inode = file_inode(file);
1203 struct task_struct *task = get_proc_task(inode);
1205 char tmpbuf[TMPBUFLEN];
1209 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1210 from_kuid(file->f_cred->user_ns,
1211 audit_get_loginuid(task)));
1212 put_task_struct(task);
1213 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1216 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1217 size_t count, loff_t *ppos)
1219 struct inode * inode = file_inode(file);
1225 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1232 /* No partial writes. */
1236 rv = kstrtou32_from_user(buf, count, 10, &loginuid);
1240 /* is userspace tring to explicitly UNSET the loginuid? */
1241 if (loginuid == AUDIT_UID_UNSET) {
1242 kloginuid = INVALID_UID;
1244 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
1245 if (!uid_valid(kloginuid))
1249 rv = audit_set_loginuid(kloginuid);
1255 static const struct file_operations proc_loginuid_operations = {
1256 .read = proc_loginuid_read,
1257 .write = proc_loginuid_write,
1258 .llseek = generic_file_llseek,
1261 static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1262 size_t count, loff_t *ppos)
1264 struct inode * inode = file_inode(file);
1265 struct task_struct *task = get_proc_task(inode);
1267 char tmpbuf[TMPBUFLEN];
1271 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1272 audit_get_sessionid(task));
1273 put_task_struct(task);
1274 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1277 static const struct file_operations proc_sessionid_operations = {
1278 .read = proc_sessionid_read,
1279 .llseek = generic_file_llseek,
1283 #ifdef CONFIG_FAULT_INJECTION
1284 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1285 size_t count, loff_t *ppos)
1287 struct task_struct *task = get_proc_task(file_inode(file));
1288 char buffer[PROC_NUMBUF];
1294 make_it_fail = task->make_it_fail;
1295 put_task_struct(task);
1297 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1299 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1302 static ssize_t proc_fault_inject_write(struct file * file,
1303 const char __user * buf, size_t count, loff_t *ppos)
1305 struct task_struct *task;
1306 char buffer[PROC_NUMBUF];
1310 if (!capable(CAP_SYS_RESOURCE))
1312 memset(buffer, 0, sizeof(buffer));
1313 if (count > sizeof(buffer) - 1)
1314 count = sizeof(buffer) - 1;
1315 if (copy_from_user(buffer, buf, count))
1317 rv = kstrtoint(strstrip(buffer), 0, &make_it_fail);
1320 if (make_it_fail < 0 || make_it_fail > 1)
1323 task = get_proc_task(file_inode(file));
1326 task->make_it_fail = make_it_fail;
1327 put_task_struct(task);
1332 static const struct file_operations proc_fault_inject_operations = {
1333 .read = proc_fault_inject_read,
1334 .write = proc_fault_inject_write,
1335 .llseek = generic_file_llseek,
1338 static ssize_t proc_fail_nth_write(struct file *file, const char __user *buf,
1339 size_t count, loff_t *ppos)
1341 struct task_struct *task;
1345 err = kstrtouint_from_user(buf, count, 0, &n);
1349 task = get_proc_task(file_inode(file));
1353 put_task_struct(task);
1358 static ssize_t proc_fail_nth_read(struct file *file, char __user *buf,
1359 size_t count, loff_t *ppos)
1361 struct task_struct *task;
1362 char numbuf[PROC_NUMBUF];
1365 task = get_proc_task(file_inode(file));
1368 len = snprintf(numbuf, sizeof(numbuf), "%u\n", task->fail_nth);
1369 len = simple_read_from_buffer(buf, count, ppos, numbuf, len);
1370 put_task_struct(task);
1375 static const struct file_operations proc_fail_nth_operations = {
1376 .read = proc_fail_nth_read,
1377 .write = proc_fail_nth_write,
1382 #ifdef CONFIG_SCHED_DEBUG
1384 * Print out various scheduling related per-task fields:
1386 static int sched_show(struct seq_file *m, void *v)
1388 struct inode *inode = m->private;
1389 struct pid_namespace *ns = proc_pid_ns(inode);
1390 struct task_struct *p;
1392 p = get_proc_task(inode);
1395 proc_sched_show_task(p, ns, m);
1403 sched_write(struct file *file, const char __user *buf,
1404 size_t count, loff_t *offset)
1406 struct inode *inode = file_inode(file);
1407 struct task_struct *p;
1409 p = get_proc_task(inode);
1412 proc_sched_set_task(p);
1419 static int sched_open(struct inode *inode, struct file *filp)
1421 return single_open(filp, sched_show, inode);
1424 static const struct file_operations proc_pid_sched_operations = {
1427 .write = sched_write,
1428 .llseek = seq_lseek,
1429 .release = single_release,
1434 #ifdef CONFIG_SCHED_AUTOGROUP
1436 * Print out autogroup related information:
1438 static int sched_autogroup_show(struct seq_file *m, void *v)
1440 struct inode *inode = m->private;
1441 struct task_struct *p;
1443 p = get_proc_task(inode);
1446 proc_sched_autogroup_show_task(p, m);
1454 sched_autogroup_write(struct file *file, const char __user *buf,
1455 size_t count, loff_t *offset)
1457 struct inode *inode = file_inode(file);
1458 struct task_struct *p;
1459 char buffer[PROC_NUMBUF];
1463 memset(buffer, 0, sizeof(buffer));
1464 if (count > sizeof(buffer) - 1)
1465 count = sizeof(buffer) - 1;
1466 if (copy_from_user(buffer, buf, count))
1469 err = kstrtoint(strstrip(buffer), 0, &nice);
1473 p = get_proc_task(inode);
1477 err = proc_sched_autogroup_set_nice(p, nice);
1486 static int sched_autogroup_open(struct inode *inode, struct file *filp)
1490 ret = single_open(filp, sched_autogroup_show, NULL);
1492 struct seq_file *m = filp->private_data;
1499 static const struct file_operations proc_pid_sched_autogroup_operations = {
1500 .open = sched_autogroup_open,
1502 .write = sched_autogroup_write,
1503 .llseek = seq_lseek,
1504 .release = single_release,
1507 #endif /* CONFIG_SCHED_AUTOGROUP */
1509 static ssize_t comm_write(struct file *file, const char __user *buf,
1510 size_t count, loff_t *offset)
1512 struct inode *inode = file_inode(file);
1513 struct task_struct *p;
1514 char buffer[TASK_COMM_LEN];
1515 const size_t maxlen = sizeof(buffer) - 1;
1517 memset(buffer, 0, sizeof(buffer));
1518 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
1521 p = get_proc_task(inode);
1525 if (same_thread_group(current, p))
1526 set_task_comm(p, buffer);
1535 static int comm_show(struct seq_file *m, void *v)
1537 struct inode *inode = m->private;
1538 struct task_struct *p;
1540 p = get_proc_task(inode);
1544 proc_task_name(m, p, false);
1552 static int comm_open(struct inode *inode, struct file *filp)
1554 return single_open(filp, comm_show, inode);
1557 static const struct file_operations proc_pid_set_comm_operations = {
1560 .write = comm_write,
1561 .llseek = seq_lseek,
1562 .release = single_release,
1565 static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
1567 struct task_struct *task;
1568 struct file *exe_file;
1570 task = get_proc_task(d_inode(dentry));
1573 exe_file = get_task_exe_file(task);
1574 put_task_struct(task);
1576 *exe_path = exe_file->f_path;
1577 path_get(&exe_file->f_path);
1584 static const char *proc_pid_get_link(struct dentry *dentry,
1585 struct inode *inode,
1586 struct delayed_call *done)
1589 int error = -EACCES;
1592 return ERR_PTR(-ECHILD);
1594 /* Are we allowed to snoop on the tasks file descriptors? */
1595 if (!proc_fd_access_allowed(inode))
1598 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1602 nd_jump_link(&path);
1605 return ERR_PTR(error);
1608 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1610 char *tmp = (char *)__get_free_page(GFP_KERNEL);
1617 pathname = d_path(path, tmp, PAGE_SIZE);
1618 len = PTR_ERR(pathname);
1619 if (IS_ERR(pathname))
1621 len = tmp + PAGE_SIZE - 1 - pathname;
1625 if (copy_to_user(buffer, pathname, len))
1628 free_page((unsigned long)tmp);
1632 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1634 int error = -EACCES;
1635 struct inode *inode = d_inode(dentry);
1638 /* Are we allowed to snoop on the tasks file descriptors? */
1639 if (!proc_fd_access_allowed(inode))
1642 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1646 error = do_proc_readlink(&path, buffer, buflen);
1652 const struct inode_operations proc_pid_link_inode_operations = {
1653 .readlink = proc_pid_readlink,
1654 .get_link = proc_pid_get_link,
1655 .setattr = proc_setattr,
1659 /* building an inode */
1661 void task_dump_owner(struct task_struct *task, umode_t mode,
1662 kuid_t *ruid, kgid_t *rgid)
1664 /* Depending on the state of dumpable compute who should own a
1665 * proc file for a task.
1667 const struct cred *cred;
1671 if (unlikely(task->flags & PF_KTHREAD)) {
1672 *ruid = GLOBAL_ROOT_UID;
1673 *rgid = GLOBAL_ROOT_GID;
1677 /* Default to the tasks effective ownership */
1679 cred = __task_cred(task);
1685 * Before the /proc/pid/status file was created the only way to read
1686 * the effective uid of a /process was to stat /proc/pid. Reading
1687 * /proc/pid/status is slow enough that procps and other packages
1688 * kept stating /proc/pid. To keep the rules in /proc simple I have
1689 * made this apply to all per process world readable and executable
1692 if (mode != (S_IFDIR|S_IRUGO|S_IXUGO)) {
1693 struct mm_struct *mm;
1696 /* Make non-dumpable tasks owned by some root */
1698 if (get_dumpable(mm) != SUID_DUMP_USER) {
1699 struct user_namespace *user_ns = mm->user_ns;
1701 uid = make_kuid(user_ns, 0);
1702 if (!uid_valid(uid))
1703 uid = GLOBAL_ROOT_UID;
1705 gid = make_kgid(user_ns, 0);
1706 if (!gid_valid(gid))
1707 gid = GLOBAL_ROOT_GID;
1710 uid = GLOBAL_ROOT_UID;
1711 gid = GLOBAL_ROOT_GID;
1719 struct inode *proc_pid_make_inode(struct super_block * sb,
1720 struct task_struct *task, umode_t mode)
1722 struct inode * inode;
1723 struct proc_inode *ei;
1725 /* We need a new inode */
1727 inode = new_inode(sb);
1733 inode->i_mode = mode;
1734 inode->i_ino = get_next_ino();
1735 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
1736 inode->i_op = &proc_def_inode_operations;
1739 * grab the reference to task.
1741 ei->pid = get_task_pid(task, PIDTYPE_PID);
1745 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
1746 security_task_to_inode(task, inode);
1756 int pid_getattr(const struct path *path, struct kstat *stat,
1757 u32 request_mask, unsigned int query_flags)
1759 struct inode *inode = d_inode(path->dentry);
1760 struct pid_namespace *pid = proc_pid_ns(inode);
1761 struct task_struct *task;
1763 generic_fillattr(inode, stat);
1765 stat->uid = GLOBAL_ROOT_UID;
1766 stat->gid = GLOBAL_ROOT_GID;
1768 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1770 if (!has_pid_permissions(pid, task, HIDEPID_INVISIBLE)) {
1773 * This doesn't prevent learning whether PID exists,
1774 * it only makes getattr() consistent with readdir().
1778 task_dump_owner(task, inode->i_mode, &stat->uid, &stat->gid);
1787 * Set <pid>/... inode ownership (can change due to setuid(), etc.)
1789 void pid_update_inode(struct task_struct *task, struct inode *inode)
1791 task_dump_owner(task, inode->i_mode, &inode->i_uid, &inode->i_gid);
1793 inode->i_mode &= ~(S_ISUID | S_ISGID);
1794 security_task_to_inode(task, inode);
1798 * Rewrite the inode's ownerships here because the owning task may have
1799 * performed a setuid(), etc.
1802 static int pid_revalidate(struct dentry *dentry, unsigned int flags)
1804 struct inode *inode;
1805 struct task_struct *task;
1807 if (flags & LOOKUP_RCU)
1810 inode = d_inode(dentry);
1811 task = get_proc_task(inode);
1814 pid_update_inode(task, inode);
1815 put_task_struct(task);
1821 static inline bool proc_inode_is_dead(struct inode *inode)
1823 return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1826 int pid_delete_dentry(const struct dentry *dentry)
1828 /* Is the task we represent dead?
1829 * If so, then don't put the dentry on the lru list,
1830 * kill it immediately.
1832 return proc_inode_is_dead(d_inode(dentry));
1835 const struct dentry_operations pid_dentry_operations =
1837 .d_revalidate = pid_revalidate,
1838 .d_delete = pid_delete_dentry,
1844 * Fill a directory entry.
1846 * If possible create the dcache entry and derive our inode number and
1847 * file type from dcache entry.
1849 * Since all of the proc inode numbers are dynamically generated, the inode
1850 * numbers do not exist until the inode is cache. This means creating the
1851 * the dcache entry in readdir is necessary to keep the inode numbers
1852 * reported by readdir in sync with the inode numbers reported
1855 bool proc_fill_cache(struct file *file, struct dir_context *ctx,
1856 const char *name, unsigned int len,
1857 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1859 struct dentry *child, *dir = file->f_path.dentry;
1860 struct qstr qname = QSTR_INIT(name, len);
1861 struct inode *inode;
1862 unsigned type = DT_UNKNOWN;
1865 child = d_hash_and_lookup(dir, &qname);
1867 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1868 child = d_alloc_parallel(dir, &qname, &wq);
1870 goto end_instantiate;
1871 if (d_in_lookup(child)) {
1873 res = instantiate(child, task, ptr);
1874 d_lookup_done(child);
1875 if (unlikely(res)) {
1879 goto end_instantiate;
1883 inode = d_inode(child);
1885 type = inode->i_mode >> 12;
1888 return dir_emit(ctx, name, len, ino, type);
1892 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1893 * which represent vma start and end addresses.
1895 static int dname_to_vma_addr(struct dentry *dentry,
1896 unsigned long *start, unsigned long *end)
1898 const char *str = dentry->d_name.name;
1899 unsigned long long sval, eval;
1902 if (str[0] == '0' && str[1] != '-')
1904 len = _parse_integer(str, 16, &sval);
1905 if (len & KSTRTOX_OVERFLOW)
1907 if (sval != (unsigned long)sval)
1915 if (str[0] == '0' && str[1])
1917 len = _parse_integer(str, 16, &eval);
1918 if (len & KSTRTOX_OVERFLOW)
1920 if (eval != (unsigned long)eval)
1933 static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
1935 unsigned long vm_start, vm_end;
1936 bool exact_vma_exists = false;
1937 struct mm_struct *mm = NULL;
1938 struct task_struct *task;
1939 struct inode *inode;
1942 if (flags & LOOKUP_RCU)
1945 inode = d_inode(dentry);
1946 task = get_proc_task(inode);
1950 mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
1951 if (IS_ERR_OR_NULL(mm))
1954 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1955 down_read(&mm->mmap_sem);
1956 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1957 up_read(&mm->mmap_sem);
1962 if (exact_vma_exists) {
1963 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
1965 security_task_to_inode(task, inode);
1970 put_task_struct(task);
1976 static const struct dentry_operations tid_map_files_dentry_operations = {
1977 .d_revalidate = map_files_d_revalidate,
1978 .d_delete = pid_delete_dentry,
1981 static int map_files_get_link(struct dentry *dentry, struct path *path)
1983 unsigned long vm_start, vm_end;
1984 struct vm_area_struct *vma;
1985 struct task_struct *task;
1986 struct mm_struct *mm;
1990 task = get_proc_task(d_inode(dentry));
1994 mm = get_task_mm(task);
1995 put_task_struct(task);
1999 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
2004 down_read(&mm->mmap_sem);
2005 vma = find_exact_vma(mm, vm_start, vm_end);
2006 if (vma && vma->vm_file) {
2007 *path = vma->vm_file->f_path;
2011 up_read(&mm->mmap_sem);
2019 struct map_files_info {
2020 unsigned long start;
2026 * Only allow CAP_SYS_ADMIN to follow the links, due to concerns about how the
2027 * symlinks may be used to bypass permissions on ancestor directories in the
2028 * path to the file in question.
2031 proc_map_files_get_link(struct dentry *dentry,
2032 struct inode *inode,
2033 struct delayed_call *done)
2035 if (!capable(CAP_SYS_ADMIN))
2036 return ERR_PTR(-EPERM);
2038 return proc_pid_get_link(dentry, inode, done);
2042 * Identical to proc_pid_link_inode_operations except for get_link()
2044 static const struct inode_operations proc_map_files_link_inode_operations = {
2045 .readlink = proc_pid_readlink,
2046 .get_link = proc_map_files_get_link,
2047 .setattr = proc_setattr,
2050 static struct dentry *
2051 proc_map_files_instantiate(struct dentry *dentry,
2052 struct task_struct *task, const void *ptr)
2054 fmode_t mode = (fmode_t)(unsigned long)ptr;
2055 struct proc_inode *ei;
2056 struct inode *inode;
2058 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFLNK |
2059 ((mode & FMODE_READ ) ? S_IRUSR : 0) |
2060 ((mode & FMODE_WRITE) ? S_IWUSR : 0));
2062 return ERR_PTR(-ENOENT);
2065 ei->op.proc_get_link = map_files_get_link;
2067 inode->i_op = &proc_map_files_link_inode_operations;
2070 d_set_d_op(dentry, &tid_map_files_dentry_operations);
2071 return d_splice_alias(inode, dentry);
2074 static struct dentry *proc_map_files_lookup(struct inode *dir,
2075 struct dentry *dentry, unsigned int flags)
2077 unsigned long vm_start, vm_end;
2078 struct vm_area_struct *vma;
2079 struct task_struct *task;
2080 struct dentry *result;
2081 struct mm_struct *mm;
2083 result = ERR_PTR(-ENOENT);
2084 task = get_proc_task(dir);
2088 result = ERR_PTR(-EACCES);
2089 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
2092 result = ERR_PTR(-ENOENT);
2093 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
2096 mm = get_task_mm(task);
2100 down_read(&mm->mmap_sem);
2101 vma = find_exact_vma(mm, vm_start, vm_end);
2106 result = proc_map_files_instantiate(dentry, task,
2107 (void *)(unsigned long)vma->vm_file->f_mode);
2110 up_read(&mm->mmap_sem);
2113 put_task_struct(task);
2118 static const struct inode_operations proc_map_files_inode_operations = {
2119 .lookup = proc_map_files_lookup,
2120 .permission = proc_fd_permission,
2121 .setattr = proc_setattr,
2125 proc_map_files_readdir(struct file *file, struct dir_context *ctx)
2127 struct vm_area_struct *vma;
2128 struct task_struct *task;
2129 struct mm_struct *mm;
2130 unsigned long nr_files, pos, i;
2131 struct flex_array *fa = NULL;
2132 struct map_files_info info;
2133 struct map_files_info *p;
2137 task = get_proc_task(file_inode(file));
2142 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
2146 if (!dir_emit_dots(file, ctx))
2149 mm = get_task_mm(task);
2152 down_read(&mm->mmap_sem);
2157 * We need two passes here:
2159 * 1) Collect vmas of mapped files with mmap_sem taken
2160 * 2) Release mmap_sem and instantiate entries
2162 * otherwise we get lockdep complained, since filldir()
2163 * routine might require mmap_sem taken in might_fault().
2166 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
2167 if (vma->vm_file && ++pos > ctx->pos)
2172 fa = flex_array_alloc(sizeof(info), nr_files,
2174 if (!fa || flex_array_prealloc(fa, 0, nr_files,
2178 flex_array_free(fa);
2179 up_read(&mm->mmap_sem);
2183 for (i = 0, vma = mm->mmap, pos = 2; vma;
2184 vma = vma->vm_next) {
2187 if (++pos <= ctx->pos)
2190 info.start = vma->vm_start;
2191 info.end = vma->vm_end;
2192 info.mode = vma->vm_file->f_mode;
2193 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
2197 up_read(&mm->mmap_sem);
2200 for (i = 0; i < nr_files; i++) {
2201 char buf[4 * sizeof(long) + 2]; /* max: %lx-%lx\0 */
2204 p = flex_array_get(fa, i);
2205 len = snprintf(buf, sizeof(buf), "%lx-%lx", p->start, p->end);
2206 if (!proc_fill_cache(file, ctx,
2208 proc_map_files_instantiate,
2210 (void *)(unsigned long)p->mode))
2215 flex_array_free(fa);
2218 put_task_struct(task);
2223 static const struct file_operations proc_map_files_operations = {
2224 .read = generic_read_dir,
2225 .iterate_shared = proc_map_files_readdir,
2226 .llseek = generic_file_llseek,
2229 #if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
2230 struct timers_private {
2232 struct task_struct *task;
2233 struct sighand_struct *sighand;
2234 struct pid_namespace *ns;
2235 unsigned long flags;
2238 static void *timers_start(struct seq_file *m, loff_t *pos)
2240 struct timers_private *tp = m->private;
2242 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2244 return ERR_PTR(-ESRCH);
2246 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2248 return ERR_PTR(-ESRCH);
2250 return seq_list_start(&tp->task->signal->posix_timers, *pos);
2253 static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2255 struct timers_private *tp = m->private;
2256 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2259 static void timers_stop(struct seq_file *m, void *v)
2261 struct timers_private *tp = m->private;
2264 unlock_task_sighand(tp->task, &tp->flags);
2269 put_task_struct(tp->task);
2274 static int show_timer(struct seq_file *m, void *v)
2276 struct k_itimer *timer;
2277 struct timers_private *tp = m->private;
2279 static const char * const nstr[] = {
2280 [SIGEV_SIGNAL] = "signal",
2281 [SIGEV_NONE] = "none",
2282 [SIGEV_THREAD] = "thread",
2285 timer = list_entry((struct list_head *)v, struct k_itimer, list);
2286 notify = timer->it_sigev_notify;
2288 seq_printf(m, "ID: %d\n", timer->it_id);
2289 seq_printf(m, "signal: %d/%px\n",
2290 timer->sigq->info.si_signo,
2291 timer->sigq->info.si_value.sival_ptr);
2292 seq_printf(m, "notify: %s/%s.%d\n",
2293 nstr[notify & ~SIGEV_THREAD_ID],
2294 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2295 pid_nr_ns(timer->it_pid, tp->ns));
2296 seq_printf(m, "ClockID: %d\n", timer->it_clock);
2301 static const struct seq_operations proc_timers_seq_ops = {
2302 .start = timers_start,
2303 .next = timers_next,
2304 .stop = timers_stop,
2308 static int proc_timers_open(struct inode *inode, struct file *file)
2310 struct timers_private *tp;
2312 tp = __seq_open_private(file, &proc_timers_seq_ops,
2313 sizeof(struct timers_private));
2317 tp->pid = proc_pid(inode);
2318 tp->ns = proc_pid_ns(inode);
2322 static const struct file_operations proc_timers_operations = {
2323 .open = proc_timers_open,
2325 .llseek = seq_lseek,
2326 .release = seq_release_private,
2330 static ssize_t timerslack_ns_write(struct file *file, const char __user *buf,
2331 size_t count, loff_t *offset)
2333 struct inode *inode = file_inode(file);
2334 struct task_struct *p;
2338 err = kstrtoull_from_user(buf, count, 10, &slack_ns);
2342 p = get_proc_task(inode);
2347 if (!capable(CAP_SYS_NICE)) {
2352 err = security_task_setscheduler(p);
2361 p->timer_slack_ns = p->default_timer_slack_ns;
2363 p->timer_slack_ns = slack_ns;
2372 static int timerslack_ns_show(struct seq_file *m, void *v)
2374 struct inode *inode = m->private;
2375 struct task_struct *p;
2378 p = get_proc_task(inode);
2384 if (!capable(CAP_SYS_NICE)) {
2388 err = security_task_getscheduler(p);
2394 seq_printf(m, "%llu\n", p->timer_slack_ns);
2403 static int timerslack_ns_open(struct inode *inode, struct file *filp)
2405 return single_open(filp, timerslack_ns_show, inode);
2408 static const struct file_operations proc_pid_set_timerslack_ns_operations = {
2409 .open = timerslack_ns_open,
2411 .write = timerslack_ns_write,
2412 .llseek = seq_lseek,
2413 .release = single_release,
2416 static struct dentry *proc_pident_instantiate(struct dentry *dentry,
2417 struct task_struct *task, const void *ptr)
2419 const struct pid_entry *p = ptr;
2420 struct inode *inode;
2421 struct proc_inode *ei;
2423 inode = proc_pid_make_inode(dentry->d_sb, task, p->mode);
2425 return ERR_PTR(-ENOENT);
2428 if (S_ISDIR(inode->i_mode))
2429 set_nlink(inode, 2); /* Use getattr to fix if necessary */
2431 inode->i_op = p->iop;
2433 inode->i_fop = p->fop;
2435 pid_update_inode(task, inode);
2436 d_set_d_op(dentry, &pid_dentry_operations);
2437 return d_splice_alias(inode, dentry);
2440 static struct dentry *proc_pident_lookup(struct inode *dir,
2441 struct dentry *dentry,
2442 const struct pid_entry *ents,
2445 struct task_struct *task = get_proc_task(dir);
2446 const struct pid_entry *p, *last;
2447 struct dentry *res = ERR_PTR(-ENOENT);
2453 * Yes, it does not scale. And it should not. Don't add
2454 * new entries into /proc/<tgid>/ without very good reasons.
2456 last = &ents[nents];
2457 for (p = ents; p < last; p++) {
2458 if (p->len != dentry->d_name.len)
2460 if (!memcmp(dentry->d_name.name, p->name, p->len)) {
2461 res = proc_pident_instantiate(dentry, task, p);
2465 put_task_struct(task);
2470 static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
2471 const struct pid_entry *ents, unsigned int nents)
2473 struct task_struct *task = get_proc_task(file_inode(file));
2474 const struct pid_entry *p;
2479 if (!dir_emit_dots(file, ctx))
2482 if (ctx->pos >= nents + 2)
2485 for (p = ents + (ctx->pos - 2); p < ents + nents; p++) {
2486 if (!proc_fill_cache(file, ctx, p->name, p->len,
2487 proc_pident_instantiate, task, p))
2492 put_task_struct(task);
2496 #ifdef CONFIG_SECURITY
2497 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2498 size_t count, loff_t *ppos)
2500 struct inode * inode = file_inode(file);
2503 struct task_struct *task = get_proc_task(inode);
2508 length = security_getprocattr(task,
2509 (char*)file->f_path.dentry->d_name.name,
2511 put_task_struct(task);
2513 length = simple_read_from_buffer(buf, count, ppos, p, length);
2518 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2519 size_t count, loff_t *ppos)
2521 struct inode * inode = file_inode(file);
2524 struct task_struct *task = get_proc_task(inode);
2530 /* A task may only write its own attributes. */
2532 if (current != task)
2535 if (count > PAGE_SIZE)
2538 /* No partial writes. */
2543 page = memdup_user(buf, count);
2545 length = PTR_ERR(page);
2549 /* Guard against adverse ptrace interaction */
2550 length = mutex_lock_interruptible(¤t->signal->cred_guard_mutex);
2554 length = security_setprocattr(file->f_path.dentry->d_name.name,
2556 mutex_unlock(¤t->signal->cred_guard_mutex);
2560 put_task_struct(task);
2565 static const struct file_operations proc_pid_attr_operations = {
2566 .read = proc_pid_attr_read,
2567 .write = proc_pid_attr_write,
2568 .llseek = generic_file_llseek,
2571 static const struct pid_entry attr_dir_stuff[] = {
2572 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2573 REG("prev", S_IRUGO, proc_pid_attr_operations),
2574 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2575 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2576 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2577 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2580 static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
2582 return proc_pident_readdir(file, ctx,
2583 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2586 static const struct file_operations proc_attr_dir_operations = {
2587 .read = generic_read_dir,
2588 .iterate_shared = proc_attr_dir_readdir,
2589 .llseek = generic_file_llseek,
2592 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2593 struct dentry *dentry, unsigned int flags)
2595 return proc_pident_lookup(dir, dentry,
2596 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2599 static const struct inode_operations proc_attr_dir_inode_operations = {
2600 .lookup = proc_attr_dir_lookup,
2601 .getattr = pid_getattr,
2602 .setattr = proc_setattr,
2607 #ifdef CONFIG_ELF_CORE
2608 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2609 size_t count, loff_t *ppos)
2611 struct task_struct *task = get_proc_task(file_inode(file));
2612 struct mm_struct *mm;
2613 char buffer[PROC_NUMBUF];
2621 mm = get_task_mm(task);
2623 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2624 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2625 MMF_DUMP_FILTER_SHIFT));
2627 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2630 put_task_struct(task);
2635 static ssize_t proc_coredump_filter_write(struct file *file,
2636 const char __user *buf,
2640 struct task_struct *task;
2641 struct mm_struct *mm;
2647 ret = kstrtouint_from_user(buf, count, 0, &val);
2652 task = get_proc_task(file_inode(file));
2656 mm = get_task_mm(task);
2661 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2663 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2665 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2670 put_task_struct(task);
2677 static const struct file_operations proc_coredump_filter_operations = {
2678 .read = proc_coredump_filter_read,
2679 .write = proc_coredump_filter_write,
2680 .llseek = generic_file_llseek,
2684 #ifdef CONFIG_TASK_IO_ACCOUNTING
2685 static int do_io_accounting(struct task_struct *task, struct seq_file *m, int whole)
2687 struct task_io_accounting acct = task->ioac;
2688 unsigned long flags;
2691 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2695 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
2700 if (whole && lock_task_sighand(task, &flags)) {
2701 struct task_struct *t = task;
2703 task_io_accounting_add(&acct, &task->signal->ioac);
2704 while_each_thread(task, t)
2705 task_io_accounting_add(&acct, &t->ioac);
2707 unlock_task_sighand(task, &flags);
2714 "read_bytes: %llu\n"
2715 "write_bytes: %llu\n"
2716 "cancelled_write_bytes: %llu\n",
2717 (unsigned long long)acct.rchar,
2718 (unsigned long long)acct.wchar,
2719 (unsigned long long)acct.syscr,
2720 (unsigned long long)acct.syscw,
2721 (unsigned long long)acct.read_bytes,
2722 (unsigned long long)acct.write_bytes,
2723 (unsigned long long)acct.cancelled_write_bytes);
2727 mutex_unlock(&task->signal->cred_guard_mutex);
2731 static int proc_tid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2732 struct pid *pid, struct task_struct *task)
2734 return do_io_accounting(task, m, 0);
2737 static int proc_tgid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2738 struct pid *pid, struct task_struct *task)
2740 return do_io_accounting(task, m, 1);
2742 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2744 #ifdef CONFIG_USER_NS
2745 static int proc_id_map_open(struct inode *inode, struct file *file,
2746 const struct seq_operations *seq_ops)
2748 struct user_namespace *ns = NULL;
2749 struct task_struct *task;
2750 struct seq_file *seq;
2753 task = get_proc_task(inode);
2756 ns = get_user_ns(task_cred_xxx(task, user_ns));
2758 put_task_struct(task);
2763 ret = seq_open(file, seq_ops);
2767 seq = file->private_data;
2777 static int proc_id_map_release(struct inode *inode, struct file *file)
2779 struct seq_file *seq = file->private_data;
2780 struct user_namespace *ns = seq->private;
2782 return seq_release(inode, file);
2785 static int proc_uid_map_open(struct inode *inode, struct file *file)
2787 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2790 static int proc_gid_map_open(struct inode *inode, struct file *file)
2792 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2795 static int proc_projid_map_open(struct inode *inode, struct file *file)
2797 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2800 static const struct file_operations proc_uid_map_operations = {
2801 .open = proc_uid_map_open,
2802 .write = proc_uid_map_write,
2804 .llseek = seq_lseek,
2805 .release = proc_id_map_release,
2808 static const struct file_operations proc_gid_map_operations = {
2809 .open = proc_gid_map_open,
2810 .write = proc_gid_map_write,
2812 .llseek = seq_lseek,
2813 .release = proc_id_map_release,
2816 static const struct file_operations proc_projid_map_operations = {
2817 .open = proc_projid_map_open,
2818 .write = proc_projid_map_write,
2820 .llseek = seq_lseek,
2821 .release = proc_id_map_release,
2824 static int proc_setgroups_open(struct inode *inode, struct file *file)
2826 struct user_namespace *ns = NULL;
2827 struct task_struct *task;
2831 task = get_proc_task(inode);
2834 ns = get_user_ns(task_cred_xxx(task, user_ns));
2836 put_task_struct(task);
2841 if (file->f_mode & FMODE_WRITE) {
2843 if (!ns_capable(ns, CAP_SYS_ADMIN))
2847 ret = single_open(file, &proc_setgroups_show, ns);
2858 static int proc_setgroups_release(struct inode *inode, struct file *file)
2860 struct seq_file *seq = file->private_data;
2861 struct user_namespace *ns = seq->private;
2862 int ret = single_release(inode, file);
2867 static const struct file_operations proc_setgroups_operations = {
2868 .open = proc_setgroups_open,
2869 .write = proc_setgroups_write,
2871 .llseek = seq_lseek,
2872 .release = proc_setgroups_release,
2874 #endif /* CONFIG_USER_NS */
2876 static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2877 struct pid *pid, struct task_struct *task)
2879 int err = lock_trace(task);
2881 seq_printf(m, "%08x\n", task->personality);
2887 #ifdef CONFIG_LIVEPATCH
2888 static int proc_pid_patch_state(struct seq_file *m, struct pid_namespace *ns,
2889 struct pid *pid, struct task_struct *task)
2891 seq_printf(m, "%d\n", task->patch_state);
2894 #endif /* CONFIG_LIVEPATCH */
2899 static const struct file_operations proc_task_operations;
2900 static const struct inode_operations proc_task_inode_operations;
2902 static const struct pid_entry tgid_base_stuff[] = {
2903 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2904 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2905 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
2906 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2907 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
2909 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2911 REG("environ", S_IRUSR, proc_environ_operations),
2912 REG("auxv", S_IRUSR, proc_auxv_operations),
2913 ONE("status", S_IRUGO, proc_pid_status),
2914 ONE("personality", S_IRUSR, proc_pid_personality),
2915 ONE("limits", S_IRUGO, proc_pid_limits),
2916 #ifdef CONFIG_SCHED_DEBUG
2917 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2919 #ifdef CONFIG_SCHED_AUTOGROUP
2920 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2922 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2923 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2924 ONE("syscall", S_IRUSR, proc_pid_syscall),
2926 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
2927 ONE("stat", S_IRUGO, proc_tgid_stat),
2928 ONE("statm", S_IRUGO, proc_pid_statm),
2929 REG("maps", S_IRUGO, proc_pid_maps_operations),
2931 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
2933 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2934 LNK("cwd", proc_cwd_link),
2935 LNK("root", proc_root_link),
2936 LNK("exe", proc_exe_link),
2937 REG("mounts", S_IRUGO, proc_mounts_operations),
2938 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2939 REG("mountstats", S_IRUSR, proc_mountstats_operations),
2940 #ifdef CONFIG_PROC_PAGE_MONITOR
2941 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2942 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
2943 REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
2944 REG("pagemap", S_IRUSR, proc_pagemap_operations),
2946 #ifdef CONFIG_SECURITY
2947 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
2949 #ifdef CONFIG_KALLSYMS
2950 ONE("wchan", S_IRUGO, proc_pid_wchan),
2952 #ifdef CONFIG_STACKTRACE
2953 ONE("stack", S_IRUSR, proc_pid_stack),
2955 #ifdef CONFIG_SCHED_INFO
2956 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
2958 #ifdef CONFIG_LATENCYTOP
2959 REG("latency", S_IRUGO, proc_lstats_operations),
2961 #ifdef CONFIG_PROC_PID_CPUSET
2962 ONE("cpuset", S_IRUGO, proc_cpuset_show),
2964 #ifdef CONFIG_CGROUPS
2965 ONE("cgroup", S_IRUGO, proc_cgroup_show),
2967 ONE("oom_score", S_IRUGO, proc_oom_score),
2968 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
2969 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
2970 #ifdef CONFIG_AUDITSYSCALL
2971 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2972 REG("sessionid", S_IRUGO, proc_sessionid_operations),
2974 #ifdef CONFIG_FAULT_INJECTION
2975 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2976 REG("fail-nth", 0644, proc_fail_nth_operations),
2978 #ifdef CONFIG_ELF_CORE
2979 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
2981 #ifdef CONFIG_TASK_IO_ACCOUNTING
2982 ONE("io", S_IRUSR, proc_tgid_io_accounting),
2984 #ifdef CONFIG_USER_NS
2985 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2986 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
2987 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
2988 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
2990 #if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
2991 REG("timers", S_IRUGO, proc_timers_operations),
2993 REG("timerslack_ns", S_IRUGO|S_IWUGO, proc_pid_set_timerslack_ns_operations),
2994 #ifdef CONFIG_LIVEPATCH
2995 ONE("patch_state", S_IRUSR, proc_pid_patch_state),
2999 static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
3001 return proc_pident_readdir(file, ctx,
3002 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
3005 static const struct file_operations proc_tgid_base_operations = {
3006 .read = generic_read_dir,
3007 .iterate_shared = proc_tgid_base_readdir,
3008 .llseek = generic_file_llseek,
3011 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3013 return proc_pident_lookup(dir, dentry,
3014 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
3017 static const struct inode_operations proc_tgid_base_inode_operations = {
3018 .lookup = proc_tgid_base_lookup,
3019 .getattr = pid_getattr,
3020 .setattr = proc_setattr,
3021 .permission = proc_pid_permission,
3024 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
3026 struct dentry *dentry, *leader, *dir;
3031 name.len = snprintf(buf, sizeof(buf), "%u", pid);
3032 /* no ->d_hash() rejects on procfs */
3033 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
3035 d_invalidate(dentry);
3043 name.len = snprintf(buf, sizeof(buf), "%u", tgid);
3044 leader = d_hash_and_lookup(mnt->mnt_root, &name);
3049 name.len = strlen(name.name);
3050 dir = d_hash_and_lookup(leader, &name);
3052 goto out_put_leader;
3055 name.len = snprintf(buf, sizeof(buf), "%u", pid);
3056 dentry = d_hash_and_lookup(dir, &name);
3058 d_invalidate(dentry);
3070 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
3071 * @task: task that should be flushed.
3073 * When flushing dentries from proc, one needs to flush them from global
3074 * proc (proc_mnt) and from all the namespaces' procs this task was seen
3075 * in. This call is supposed to do all of this job.
3077 * Looks in the dcache for
3079 * /proc/@tgid/task/@pid
3080 * if either directory is present flushes it and all of it'ts children
3083 * It is safe and reasonable to cache /proc entries for a task until
3084 * that task exits. After that they just clog up the dcache with
3085 * useless entries, possibly causing useful dcache entries to be
3086 * flushed instead. This routine is proved to flush those useless
3087 * dcache entries at process exit time.
3089 * NOTE: This routine is just an optimization so it does not guarantee
3090 * that no dcache entries will exist at process exit time it
3091 * just makes it very unlikely that any will persist.
3094 void proc_flush_task(struct task_struct *task)
3097 struct pid *pid, *tgid;
3100 pid = task_pid(task);
3101 tgid = task_tgid(task);
3103 for (i = 0; i <= pid->level; i++) {
3104 upid = &pid->numbers[i];
3105 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
3106 tgid->numbers[i].nr);
3110 static struct dentry *proc_pid_instantiate(struct dentry * dentry,
3111 struct task_struct *task, const void *ptr)
3113 struct inode *inode;
3115 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
3117 return ERR_PTR(-ENOENT);
3119 inode->i_op = &proc_tgid_base_inode_operations;
3120 inode->i_fop = &proc_tgid_base_operations;
3121 inode->i_flags|=S_IMMUTABLE;
3123 set_nlink(inode, nlink_tgid);
3124 pid_update_inode(task, inode);
3126 d_set_d_op(dentry, &pid_dentry_operations);
3127 return d_splice_alias(inode, dentry);
3130 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
3132 struct task_struct *task;
3134 struct pid_namespace *ns;
3135 struct dentry *result = ERR_PTR(-ENOENT);
3137 tgid = name_to_int(&dentry->d_name);
3141 ns = dentry->d_sb->s_fs_info;
3143 task = find_task_by_pid_ns(tgid, ns);
3145 get_task_struct(task);
3150 result = proc_pid_instantiate(dentry, task, NULL);
3151 put_task_struct(task);
3157 * Find the first task with tgid >= tgid
3162 struct task_struct *task;
3164 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3169 put_task_struct(iter.task);
3173 pid = find_ge_pid(iter.tgid, ns);
3175 iter.tgid = pid_nr_ns(pid, ns);
3176 iter.task = pid_task(pid, PIDTYPE_PID);
3177 /* What we to know is if the pid we have find is the
3178 * pid of a thread_group_leader. Testing for task
3179 * being a thread_group_leader is the obvious thing
3180 * todo but there is a window when it fails, due to
3181 * the pid transfer logic in de_thread.
3183 * So we perform the straight forward test of seeing
3184 * if the pid we have found is the pid of a thread
3185 * group leader, and don't worry if the task we have
3186 * found doesn't happen to be a thread group leader.
3187 * As we don't care in the case of readdir.
3189 if (!iter.task || !has_group_leader_pid(iter.task)) {
3193 get_task_struct(iter.task);
3199 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
3201 /* for the /proc/ directory itself, after non-process stuff has been done */
3202 int proc_pid_readdir(struct file *file, struct dir_context *ctx)
3204 struct tgid_iter iter;
3205 struct pid_namespace *ns = proc_pid_ns(file_inode(file));
3206 loff_t pos = ctx->pos;
3208 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
3211 if (pos == TGID_OFFSET - 2) {
3212 struct inode *inode = d_inode(ns->proc_self);
3213 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
3215 ctx->pos = pos = pos + 1;
3217 if (pos == TGID_OFFSET - 1) {
3218 struct inode *inode = d_inode(ns->proc_thread_self);
3219 if (!dir_emit(ctx, "thread-self", 11, inode->i_ino, DT_LNK))
3221 ctx->pos = pos = pos + 1;
3223 iter.tgid = pos - TGID_OFFSET;
3225 for (iter = next_tgid(ns, iter);
3227 iter.tgid += 1, iter = next_tgid(ns, iter)) {
3232 if (!has_pid_permissions(ns, iter.task, HIDEPID_INVISIBLE))
3235 len = snprintf(name, sizeof(name), "%u", iter.tgid);
3236 ctx->pos = iter.tgid + TGID_OFFSET;
3237 if (!proc_fill_cache(file, ctx, name, len,
3238 proc_pid_instantiate, iter.task, NULL)) {
3239 put_task_struct(iter.task);
3243 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
3248 * proc_tid_comm_permission is a special permission function exclusively
3249 * used for the node /proc/<pid>/task/<tid>/comm.
3250 * It bypasses generic permission checks in the case where a task of the same
3251 * task group attempts to access the node.
3252 * The rationale behind this is that glibc and bionic access this node for
3253 * cross thread naming (pthread_set/getname_np(!self)). However, if
3254 * PR_SET_DUMPABLE gets set to 0 this node among others becomes uid=0 gid=0,
3255 * which locks out the cross thread naming implementation.
3256 * This function makes sure that the node is always accessible for members of
3257 * same thread group.
3259 static int proc_tid_comm_permission(struct inode *inode, int mask)
3261 bool is_same_tgroup;
3262 struct task_struct *task;
3264 task = get_proc_task(inode);
3267 is_same_tgroup = same_thread_group(current, task);
3268 put_task_struct(task);
3270 if (likely(is_same_tgroup && !(mask & MAY_EXEC))) {
3271 /* This file (/proc/<pid>/task/<tid>/comm) can always be
3272 * read or written by the members of the corresponding
3278 return generic_permission(inode, mask);
3281 static const struct inode_operations proc_tid_comm_inode_operations = {
3282 .permission = proc_tid_comm_permission,
3288 static const struct pid_entry tid_base_stuff[] = {
3289 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
3290 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
3291 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
3293 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
3295 REG("environ", S_IRUSR, proc_environ_operations),
3296 REG("auxv", S_IRUSR, proc_auxv_operations),
3297 ONE("status", S_IRUGO, proc_pid_status),
3298 ONE("personality", S_IRUSR, proc_pid_personality),
3299 ONE("limits", S_IRUGO, proc_pid_limits),
3300 #ifdef CONFIG_SCHED_DEBUG
3301 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
3303 NOD("comm", S_IFREG|S_IRUGO|S_IWUSR,
3304 &proc_tid_comm_inode_operations,
3305 &proc_pid_set_comm_operations, {}),
3306 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3307 ONE("syscall", S_IRUSR, proc_pid_syscall),
3309 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
3310 ONE("stat", S_IRUGO, proc_tid_stat),
3311 ONE("statm", S_IRUGO, proc_pid_statm),
3312 REG("maps", S_IRUGO, proc_tid_maps_operations),
3313 #ifdef CONFIG_PROC_CHILDREN
3314 REG("children", S_IRUGO, proc_tid_children_operations),
3317 REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
3319 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3320 LNK("cwd", proc_cwd_link),
3321 LNK("root", proc_root_link),
3322 LNK("exe", proc_exe_link),
3323 REG("mounts", S_IRUGO, proc_mounts_operations),
3324 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
3325 #ifdef CONFIG_PROC_PAGE_MONITOR
3326 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3327 REG("smaps", S_IRUGO, proc_tid_smaps_operations),
3328 REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
3329 REG("pagemap", S_IRUSR, proc_pagemap_operations),
3331 #ifdef CONFIG_SECURITY
3332 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
3334 #ifdef CONFIG_KALLSYMS
3335 ONE("wchan", S_IRUGO, proc_pid_wchan),
3337 #ifdef CONFIG_STACKTRACE
3338 ONE("stack", S_IRUSR, proc_pid_stack),
3340 #ifdef CONFIG_SCHED_INFO
3341 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
3343 #ifdef CONFIG_LATENCYTOP
3344 REG("latency", S_IRUGO, proc_lstats_operations),
3346 #ifdef CONFIG_PROC_PID_CPUSET
3347 ONE("cpuset", S_IRUGO, proc_cpuset_show),
3349 #ifdef CONFIG_CGROUPS
3350 ONE("cgroup", S_IRUGO, proc_cgroup_show),
3352 ONE("oom_score", S_IRUGO, proc_oom_score),
3353 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
3354 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
3355 #ifdef CONFIG_AUDITSYSCALL
3356 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3357 REG("sessionid", S_IRUGO, proc_sessionid_operations),
3359 #ifdef CONFIG_FAULT_INJECTION
3360 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3361 REG("fail-nth", 0644, proc_fail_nth_operations),
3363 #ifdef CONFIG_TASK_IO_ACCOUNTING
3364 ONE("io", S_IRUSR, proc_tid_io_accounting),
3366 #ifdef CONFIG_USER_NS
3367 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3368 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
3369 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
3370 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
3372 #ifdef CONFIG_LIVEPATCH
3373 ONE("patch_state", S_IRUSR, proc_pid_patch_state),
3377 static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
3379 return proc_pident_readdir(file, ctx,
3380 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3383 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3385 return proc_pident_lookup(dir, dentry,
3386 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3389 static const struct file_operations proc_tid_base_operations = {
3390 .read = generic_read_dir,
3391 .iterate_shared = proc_tid_base_readdir,
3392 .llseek = generic_file_llseek,
3395 static const struct inode_operations proc_tid_base_inode_operations = {
3396 .lookup = proc_tid_base_lookup,
3397 .getattr = pid_getattr,
3398 .setattr = proc_setattr,
3401 static struct dentry *proc_task_instantiate(struct dentry *dentry,
3402 struct task_struct *task, const void *ptr)
3404 struct inode *inode;
3405 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
3407 return ERR_PTR(-ENOENT);
3409 inode->i_op = &proc_tid_base_inode_operations;
3410 inode->i_fop = &proc_tid_base_operations;
3411 inode->i_flags |= S_IMMUTABLE;
3413 set_nlink(inode, nlink_tid);
3414 pid_update_inode(task, inode);
3416 d_set_d_op(dentry, &pid_dentry_operations);
3417 return d_splice_alias(inode, dentry);
3420 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
3422 struct task_struct *task;
3423 struct task_struct *leader = get_proc_task(dir);
3425 struct pid_namespace *ns;
3426 struct dentry *result = ERR_PTR(-ENOENT);
3431 tid = name_to_int(&dentry->d_name);
3435 ns = dentry->d_sb->s_fs_info;
3437 task = find_task_by_pid_ns(tid, ns);
3439 get_task_struct(task);
3443 if (!same_thread_group(leader, task))
3446 result = proc_task_instantiate(dentry, task, NULL);
3448 put_task_struct(task);
3450 put_task_struct(leader);
3456 * Find the first tid of a thread group to return to user space.
3458 * Usually this is just the thread group leader, but if the users
3459 * buffer was too small or there was a seek into the middle of the
3460 * directory we have more work todo.
3462 * In the case of a short read we start with find_task_by_pid.
3464 * In the case of a seek we start with the leader and walk nr
3467 static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
3468 struct pid_namespace *ns)
3470 struct task_struct *pos, *task;
3471 unsigned long nr = f_pos;
3473 if (nr != f_pos) /* 32bit overflow? */
3477 task = pid_task(pid, PIDTYPE_PID);
3481 /* Attempt to start with the tid of a thread */
3483 pos = find_task_by_pid_ns(tid, ns);
3484 if (pos && same_thread_group(pos, task))
3488 /* If nr exceeds the number of threads there is nothing todo */
3489 if (nr >= get_nr_threads(task))
3492 /* If we haven't found our starting place yet start
3493 * with the leader and walk nr threads forward.
3495 pos = task = task->group_leader;
3499 } while_each_thread(task, pos);
3504 get_task_struct(pos);
3511 * Find the next thread in the thread list.
3512 * Return NULL if there is an error or no next thread.
3514 * The reference to the input task_struct is released.
3516 static struct task_struct *next_tid(struct task_struct *start)
3518 struct task_struct *pos = NULL;
3520 if (pid_alive(start)) {
3521 pos = next_thread(start);
3522 if (thread_group_leader(pos))
3525 get_task_struct(pos);
3528 put_task_struct(start);
3532 /* for the /proc/TGID/task/ directories */
3533 static int proc_task_readdir(struct file *file, struct dir_context *ctx)
3535 struct inode *inode = file_inode(file);
3536 struct task_struct *task;
3537 struct pid_namespace *ns;
3540 if (proc_inode_is_dead(inode))
3543 if (!dir_emit_dots(file, ctx))
3546 /* f_version caches the tgid value that the last readdir call couldn't
3547 * return. lseek aka telldir automagically resets f_version to 0.
3549 ns = proc_pid_ns(inode);
3550 tid = (int)file->f_version;
3551 file->f_version = 0;
3552 for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
3554 task = next_tid(task), ctx->pos++) {
3557 tid = task_pid_nr_ns(task, ns);
3558 len = snprintf(name, sizeof(name), "%u", tid);
3559 if (!proc_fill_cache(file, ctx, name, len,
3560 proc_task_instantiate, task, NULL)) {
3561 /* returning this tgid failed, save it as the first
3562 * pid for the next readir call */
3563 file->f_version = (u64)tid;
3564 put_task_struct(task);
3572 static int proc_task_getattr(const struct path *path, struct kstat *stat,
3573 u32 request_mask, unsigned int query_flags)
3575 struct inode *inode = d_inode(path->dentry);
3576 struct task_struct *p = get_proc_task(inode);
3577 generic_fillattr(inode, stat);
3580 stat->nlink += get_nr_threads(p);
3587 static const struct inode_operations proc_task_inode_operations = {
3588 .lookup = proc_task_lookup,
3589 .getattr = proc_task_getattr,
3590 .setattr = proc_setattr,
3591 .permission = proc_pid_permission,
3594 static const struct file_operations proc_task_operations = {
3595 .read = generic_read_dir,
3596 .iterate_shared = proc_task_readdir,
3597 .llseek = generic_file_llseek,
3600 void __init set_proc_pid_nlink(void)
3602 nlink_tid = pid_entry_nlink(tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3603 nlink_tgid = pid_entry_nlink(tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));