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/cgroup.h>
79 #include <linux/cpuset.h>
80 #include <linux/audit.h>
81 #include <linux/poll.h>
82 #include <linux/nsproxy.h>
83 #include <linux/oom.h>
84 #include <linux/elf.h>
85 #include <linux/pid_namespace.h>
86 #include <linux/user_namespace.h>
87 #include <linux/fs_struct.h>
88 #include <linux/slab.h>
89 #include <linux/sched/autogroup.h>
90 #include <linux/sched/mm.h>
91 #include <linux/sched/coredump.h>
92 #include <linux/sched/debug.h>
93 #include <linux/sched/stat.h>
94 #include <linux/flex_array.h>
95 #include <linux/posix-timers.h>
96 #ifdef CONFIG_HARDWALL
97 #include <asm/hardwall.h>
99 #include <trace/events/oom.h>
100 #include "internal.h"
103 #include "../../lib/kstrtox.h"
106 * Implementing inode permission operations in /proc is almost
107 * certainly an error. Permission checks need to happen during
108 * each system call not at open time. The reason is that most of
109 * what we wish to check for permissions in /proc varies at runtime.
111 * The classic example of a problem is opening file descriptors
112 * in /proc for a task before it execs a suid executable.
116 static u8 nlink_tgid;
122 const struct inode_operations *iop;
123 const struct file_operations *fop;
127 #define NOD(NAME, MODE, IOP, FOP, OP) { \
129 .len = sizeof(NAME) - 1, \
136 #define DIR(NAME, MODE, iops, fops) \
137 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
138 #define LNK(NAME, get_link) \
139 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
140 &proc_pid_link_inode_operations, NULL, \
141 { .proc_get_link = get_link } )
142 #define REG(NAME, MODE, fops) \
143 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
144 #define ONE(NAME, MODE, show) \
145 NOD(NAME, (S_IFREG|(MODE)), \
146 NULL, &proc_single_file_operations, \
147 { .proc_show = show } )
150 * Count the number of hardlinks for the pid_entry table, excluding the .
153 static unsigned int __init pid_entry_nlink(const struct pid_entry *entries,
160 for (i = 0; i < n; ++i) {
161 if (S_ISDIR(entries[i].mode))
168 static int get_task_root(struct task_struct *task, struct path *root)
170 int result = -ENOENT;
174 get_fs_root(task->fs, root);
181 static int proc_cwd_link(struct dentry *dentry, struct path *path)
183 struct task_struct *task = get_proc_task(d_inode(dentry));
184 int result = -ENOENT;
189 get_fs_pwd(task->fs, path);
193 put_task_struct(task);
198 static int proc_root_link(struct dentry *dentry, struct path *path)
200 struct task_struct *task = get_proc_task(d_inode(dentry));
201 int result = -ENOENT;
204 result = get_task_root(task, path);
205 put_task_struct(task);
210 static ssize_t proc_pid_cmdline_read(struct file *file, char __user *buf,
211 size_t _count, loff_t *pos)
213 struct task_struct *tsk;
214 struct mm_struct *mm;
216 unsigned long count = _count;
217 unsigned long arg_start, arg_end, env_start, env_end;
218 unsigned long len1, len2, len;
225 tsk = get_proc_task(file_inode(file));
228 mm = get_task_mm(tsk);
229 put_task_struct(tsk);
232 /* Check if process spawned far enough to have cmdline. */
238 page = (char *)__get_free_page(GFP_KERNEL);
244 down_read(&mm->mmap_sem);
245 arg_start = mm->arg_start;
246 arg_end = mm->arg_end;
247 env_start = mm->env_start;
248 env_end = mm->env_end;
249 up_read(&mm->mmap_sem);
251 BUG_ON(arg_start > arg_end);
252 BUG_ON(env_start > env_end);
254 len1 = arg_end - arg_start;
255 len2 = env_end - env_start;
263 * Inherently racy -- command line shares address space
264 * with code and data.
266 rv = access_remote_vm(mm, arg_end - 1, &c, 1, FOLL_ANON);
273 /* Command line (set of strings) occupies whole ARGV. */
277 p = arg_start + *pos;
279 while (count > 0 && len > 0) {
283 _count = min3(count, len, PAGE_SIZE);
284 nr_read = access_remote_vm(mm, p, page, _count, FOLL_ANON);
290 if (copy_to_user(buf, page, nr_read)) {
303 * Command line (1 string) occupies ARGV and
310 { .p = arg_start, .len = len1 },
311 { .p = env_start, .len = len2 },
317 while (i < 2 && pos1 >= cmdline[i].len) {
318 pos1 -= cmdline[i].len;
322 p = cmdline[i].p + pos1;
323 len = cmdline[i].len - pos1;
324 while (count > 0 && len > 0) {
325 unsigned int _count, l;
329 _count = min3(count, len, PAGE_SIZE);
330 nr_read = access_remote_vm(mm, p, page, _count, FOLL_ANON);
337 * Command line can be shorter than whole ARGV
338 * even if last "marker" byte says it is not.
341 l = strnlen(page, nr_read);
347 if (copy_to_user(buf, page, nr_read)) {
362 /* Only first chunk can be read partially. */
369 free_page((unsigned long)page);
377 static const struct file_operations proc_pid_cmdline_ops = {
378 .read = proc_pid_cmdline_read,
379 .llseek = generic_file_llseek,
382 #ifdef CONFIG_KALLSYMS
384 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
385 * Returns the resolved symbol. If that fails, simply return the address.
387 static int proc_pid_wchan(struct seq_file *m, struct pid_namespace *ns,
388 struct pid *pid, struct task_struct *task)
391 char symname[KSYM_NAME_LEN];
393 wchan = get_wchan(task);
395 if (wchan && ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)
396 && !lookup_symbol_name(wchan, symname))
397 seq_printf(m, "%s", symname);
403 #endif /* CONFIG_KALLSYMS */
405 static int lock_trace(struct task_struct *task)
407 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
410 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH_FSCREDS)) {
411 mutex_unlock(&task->signal->cred_guard_mutex);
417 static void unlock_trace(struct task_struct *task)
419 mutex_unlock(&task->signal->cred_guard_mutex);
422 #ifdef CONFIG_STACKTRACE
424 #define MAX_STACK_TRACE_DEPTH 64
426 static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
427 struct pid *pid, struct task_struct *task)
429 struct stack_trace trace;
430 unsigned long *entries;
435 * The ability to racily run the kernel stack unwinder on a running task
436 * and then observe the unwinder output is scary; while it is useful for
437 * debugging kernel issues, it can also allow an attacker to leak kernel
439 * Doing this in a manner that is at least safe from races would require
440 * some work to ensure that the remote task can not be scheduled; and
441 * even then, this would still expose the unwinder as local attack
443 * Therefore, this interface is restricted to root.
445 if (!file_ns_capable(m->file, &init_user_ns, CAP_SYS_ADMIN))
448 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
452 trace.nr_entries = 0;
453 trace.max_entries = MAX_STACK_TRACE_DEPTH;
454 trace.entries = entries;
457 err = lock_trace(task);
459 save_stack_trace_tsk(task, &trace);
461 for (i = 0; i < trace.nr_entries; i++) {
462 seq_printf(m, "[<%pK>] %pB\n",
463 (void *)entries[i], (void *)entries[i]);
473 #ifdef CONFIG_SCHED_INFO
475 * Provides /proc/PID/schedstat
477 static int proc_pid_schedstat(struct seq_file *m, struct pid_namespace *ns,
478 struct pid *pid, struct task_struct *task)
480 if (unlikely(!sched_info_on()))
481 seq_printf(m, "0 0 0\n");
483 seq_printf(m, "%llu %llu %lu\n",
484 (unsigned long long)task->se.sum_exec_runtime,
485 (unsigned long long)task->sched_info.run_delay,
486 task->sched_info.pcount);
492 #ifdef CONFIG_LATENCYTOP
493 static int lstats_show_proc(struct seq_file *m, void *v)
496 struct inode *inode = m->private;
497 struct task_struct *task = get_proc_task(inode);
501 seq_puts(m, "Latency Top version : v0.1\n");
502 for (i = 0; i < 32; i++) {
503 struct latency_record *lr = &task->latency_record[i];
504 if (lr->backtrace[0]) {
506 seq_printf(m, "%i %li %li",
507 lr->count, lr->time, lr->max);
508 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
509 unsigned long bt = lr->backtrace[q];
514 seq_printf(m, " %ps", (void *)bt);
520 put_task_struct(task);
524 static int lstats_open(struct inode *inode, struct file *file)
526 return single_open(file, lstats_show_proc, inode);
529 static ssize_t lstats_write(struct file *file, const char __user *buf,
530 size_t count, loff_t *offs)
532 struct task_struct *task = get_proc_task(file_inode(file));
536 clear_all_latency_tracing(task);
537 put_task_struct(task);
542 static const struct file_operations proc_lstats_operations = {
545 .write = lstats_write,
547 .release = single_release,
552 static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns,
553 struct pid *pid, struct task_struct *task)
555 unsigned long totalpages = totalram_pages + total_swap_pages;
556 unsigned long points = 0;
558 points = oom_badness(task, NULL, NULL, totalpages) *
560 seq_printf(m, "%lu\n", points);
570 static const struct limit_names lnames[RLIM_NLIMITS] = {
571 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
572 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
573 [RLIMIT_DATA] = {"Max data size", "bytes"},
574 [RLIMIT_STACK] = {"Max stack size", "bytes"},
575 [RLIMIT_CORE] = {"Max core file size", "bytes"},
576 [RLIMIT_RSS] = {"Max resident set", "bytes"},
577 [RLIMIT_NPROC] = {"Max processes", "processes"},
578 [RLIMIT_NOFILE] = {"Max open files", "files"},
579 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
580 [RLIMIT_AS] = {"Max address space", "bytes"},
581 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
582 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
583 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
584 [RLIMIT_NICE] = {"Max nice priority", NULL},
585 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
586 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
589 /* Display limits for a process */
590 static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
591 struct pid *pid, struct task_struct *task)
596 struct rlimit rlim[RLIM_NLIMITS];
598 if (!lock_task_sighand(task, &flags))
600 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
601 unlock_task_sighand(task, &flags);
604 * print the file header
606 seq_printf(m, "%-25s %-20s %-20s %-10s\n",
607 "Limit", "Soft Limit", "Hard Limit", "Units");
609 for (i = 0; i < RLIM_NLIMITS; i++) {
610 if (rlim[i].rlim_cur == RLIM_INFINITY)
611 seq_printf(m, "%-25s %-20s ",
612 lnames[i].name, "unlimited");
614 seq_printf(m, "%-25s %-20lu ",
615 lnames[i].name, rlim[i].rlim_cur);
617 if (rlim[i].rlim_max == RLIM_INFINITY)
618 seq_printf(m, "%-20s ", "unlimited");
620 seq_printf(m, "%-20lu ", rlim[i].rlim_max);
623 seq_printf(m, "%-10s\n", lnames[i].unit);
631 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
632 static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
633 struct pid *pid, struct task_struct *task)
636 unsigned long args[6], sp, pc;
639 res = lock_trace(task);
643 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
644 seq_puts(m, "running\n");
646 seq_printf(m, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
649 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
651 args[0], args[1], args[2], args[3], args[4], args[5],
657 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
659 /************************************************************************/
660 /* Here the fs part begins */
661 /************************************************************************/
663 /* permission checks */
664 static int proc_fd_access_allowed(struct inode *inode)
666 struct task_struct *task;
668 /* Allow access to a task's file descriptors if it is us or we
669 * may use ptrace attach to the process and find out that
672 task = get_proc_task(inode);
674 allowed = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
675 put_task_struct(task);
680 int proc_setattr(struct dentry *dentry, struct iattr *attr)
683 struct inode *inode = d_inode(dentry);
685 if (attr->ia_valid & ATTR_MODE)
688 error = setattr_prepare(dentry, attr);
692 setattr_copy(inode, attr);
693 mark_inode_dirty(inode);
698 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
699 * or euid/egid (for hide_pid_min=2)?
701 static bool has_pid_permissions(struct pid_namespace *pid,
702 struct task_struct *task,
705 if (pid->hide_pid < hide_pid_min)
707 if (in_group_p(pid->pid_gid))
709 return ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
713 static int proc_pid_permission(struct inode *inode, int mask)
715 struct pid_namespace *pid = inode->i_sb->s_fs_info;
716 struct task_struct *task;
719 task = get_proc_task(inode);
722 has_perms = has_pid_permissions(pid, task, HIDEPID_NO_ACCESS);
723 put_task_struct(task);
726 if (pid->hide_pid == HIDEPID_INVISIBLE) {
728 * Let's make getdents(), stat(), and open()
729 * consistent with each other. If a process
730 * may not stat() a file, it shouldn't be seen
738 return generic_permission(inode, mask);
743 static const struct inode_operations proc_def_inode_operations = {
744 .setattr = proc_setattr,
747 static int proc_single_show(struct seq_file *m, void *v)
749 struct inode *inode = m->private;
750 struct pid_namespace *ns;
752 struct task_struct *task;
755 ns = inode->i_sb->s_fs_info;
756 pid = proc_pid(inode);
757 task = get_pid_task(pid, PIDTYPE_PID);
761 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
763 put_task_struct(task);
767 static int proc_single_open(struct inode *inode, struct file *filp)
769 return single_open(filp, proc_single_show, inode);
772 static const struct file_operations proc_single_file_operations = {
773 .open = proc_single_open,
776 .release = single_release,
780 struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode)
782 struct task_struct *task = get_proc_task(inode);
783 struct mm_struct *mm = ERR_PTR(-ESRCH);
786 mm = mm_access(task, mode | PTRACE_MODE_FSCREDS);
787 put_task_struct(task);
789 if (!IS_ERR_OR_NULL(mm)) {
790 /* ensure this mm_struct can't be freed */
792 /* but do not pin its memory */
800 static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
802 struct mm_struct *mm = proc_mem_open(inode, mode);
807 file->private_data = mm;
811 static int mem_open(struct inode *inode, struct file *file)
813 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
815 /* OK to pass negative loff_t, we can catch out-of-range */
816 file->f_mode |= FMODE_UNSIGNED_OFFSET;
821 static ssize_t mem_rw(struct file *file, char __user *buf,
822 size_t count, loff_t *ppos, int write)
824 struct mm_struct *mm = file->private_data;
825 unsigned long addr = *ppos;
833 page = (char *)__get_free_page(GFP_KERNEL);
838 if (!mmget_not_zero(mm))
841 flags = FOLL_FORCE | (write ? FOLL_WRITE : 0);
844 size_t this_len = min_t(size_t, count, PAGE_SIZE);
846 if (write && copy_from_user(page, buf, this_len)) {
851 this_len = access_remote_vm(mm, addr, page, this_len, flags);
858 if (!write && copy_to_user(buf, page, this_len)) {
872 free_page((unsigned long) page);
876 static ssize_t mem_read(struct file *file, char __user *buf,
877 size_t count, loff_t *ppos)
879 return mem_rw(file, buf, count, ppos, 0);
882 static ssize_t mem_write(struct file *file, const char __user *buf,
883 size_t count, loff_t *ppos)
885 return mem_rw(file, (char __user*)buf, count, ppos, 1);
888 loff_t mem_lseek(struct file *file, loff_t offset, int orig)
892 file->f_pos = offset;
895 file->f_pos += offset;
900 force_successful_syscall_return();
904 static int mem_release(struct inode *inode, struct file *file)
906 struct mm_struct *mm = file->private_data;
912 static const struct file_operations proc_mem_operations = {
917 .release = mem_release,
920 static int environ_open(struct inode *inode, struct file *file)
922 return __mem_open(inode, file, PTRACE_MODE_READ);
925 static ssize_t environ_read(struct file *file, char __user *buf,
926 size_t count, loff_t *ppos)
929 unsigned long src = *ppos;
931 struct mm_struct *mm = file->private_data;
932 unsigned long env_start, env_end;
934 /* Ensure the process spawned far enough to have an environment. */
935 if (!mm || !mm->env_end)
938 page = (char *)__get_free_page(GFP_KERNEL);
943 if (!mmget_not_zero(mm))
946 down_read(&mm->mmap_sem);
947 env_start = mm->env_start;
948 env_end = mm->env_end;
949 up_read(&mm->mmap_sem);
952 size_t this_len, max_len;
955 if (src >= (env_end - env_start))
958 this_len = env_end - (env_start + src);
960 max_len = min_t(size_t, PAGE_SIZE, count);
961 this_len = min(max_len, this_len);
963 retval = access_remote_vm(mm, (env_start + src), page, this_len, FOLL_ANON);
970 if (copy_to_user(buf, page, retval)) {
984 free_page((unsigned long) page);
988 static const struct file_operations proc_environ_operations = {
989 .open = environ_open,
990 .read = environ_read,
991 .llseek = generic_file_llseek,
992 .release = mem_release,
995 static int auxv_open(struct inode *inode, struct file *file)
997 return __mem_open(inode, file, PTRACE_MODE_READ_FSCREDS);
1000 static ssize_t auxv_read(struct file *file, char __user *buf,
1001 size_t count, loff_t *ppos)
1003 struct mm_struct *mm = file->private_data;
1004 unsigned int nwords = 0;
1010 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
1011 return simple_read_from_buffer(buf, count, ppos, mm->saved_auxv,
1012 nwords * sizeof(mm->saved_auxv[0]));
1015 static const struct file_operations proc_auxv_operations = {
1018 .llseek = generic_file_llseek,
1019 .release = mem_release,
1022 static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
1025 struct task_struct *task = get_proc_task(file_inode(file));
1026 char buffer[PROC_NUMBUF];
1027 int oom_adj = OOM_ADJUST_MIN;
1032 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
1033 oom_adj = OOM_ADJUST_MAX;
1035 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
1037 put_task_struct(task);
1038 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
1039 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1042 static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
1044 struct mm_struct *mm = NULL;
1045 struct task_struct *task;
1048 task = get_proc_task(file_inode(file));
1052 mutex_lock(&oom_adj_mutex);
1054 if (oom_adj < task->signal->oom_score_adj &&
1055 !capable(CAP_SYS_RESOURCE)) {
1060 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
1061 * /proc/pid/oom_score_adj instead.
1063 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
1064 current->comm, task_pid_nr(current), task_pid_nr(task),
1067 if ((short)oom_adj < task->signal->oom_score_adj_min &&
1068 !capable(CAP_SYS_RESOURCE)) {
1075 * Make sure we will check other processes sharing the mm if this is
1076 * not vfrok which wants its own oom_score_adj.
1077 * pin the mm so it doesn't go away and get reused after task_unlock
1079 if (!task->vfork_done) {
1080 struct task_struct *p = find_lock_task_mm(task);
1083 if (test_bit(MMF_MULTIPROCESS, &p->mm->flags)) {
1091 task->signal->oom_score_adj = oom_adj;
1092 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1093 task->signal->oom_score_adj_min = (short)oom_adj;
1094 trace_oom_score_adj_update(task);
1097 struct task_struct *p;
1100 for_each_process(p) {
1101 if (same_thread_group(task, p))
1104 /* do not touch kernel threads or the global init */
1105 if (p->flags & PF_KTHREAD || is_global_init(p))
1109 if (!p->vfork_done && process_shares_mm(p, mm)) {
1110 p->signal->oom_score_adj = oom_adj;
1111 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1112 p->signal->oom_score_adj_min = (short)oom_adj;
1120 mutex_unlock(&oom_adj_mutex);
1121 put_task_struct(task);
1126 * /proc/pid/oom_adj exists solely for backwards compatibility with previous
1127 * kernels. The effective policy is defined by oom_score_adj, which has a
1128 * different scale: oom_adj grew exponentially and oom_score_adj grows linearly.
1129 * Values written to oom_adj are simply mapped linearly to oom_score_adj.
1130 * Processes that become oom disabled via oom_adj will still be oom disabled
1131 * with this implementation.
1133 * oom_adj cannot be removed since existing userspace binaries use it.
1135 static ssize_t oom_adj_write(struct file *file, const char __user *buf,
1136 size_t count, loff_t *ppos)
1138 char buffer[PROC_NUMBUF];
1142 memset(buffer, 0, sizeof(buffer));
1143 if (count > sizeof(buffer) - 1)
1144 count = sizeof(buffer) - 1;
1145 if (copy_from_user(buffer, buf, count)) {
1150 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
1153 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
1154 oom_adj != OOM_DISABLE) {
1160 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1161 * value is always attainable.
1163 if (oom_adj == OOM_ADJUST_MAX)
1164 oom_adj = OOM_SCORE_ADJ_MAX;
1166 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
1168 err = __set_oom_adj(file, oom_adj, true);
1170 return err < 0 ? err : count;
1173 static const struct file_operations proc_oom_adj_operations = {
1174 .read = oom_adj_read,
1175 .write = oom_adj_write,
1176 .llseek = generic_file_llseek,
1179 static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1180 size_t count, loff_t *ppos)
1182 struct task_struct *task = get_proc_task(file_inode(file));
1183 char buffer[PROC_NUMBUF];
1184 short oom_score_adj = OOM_SCORE_ADJ_MIN;
1189 oom_score_adj = task->signal->oom_score_adj;
1190 put_task_struct(task);
1191 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
1192 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1195 static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1196 size_t count, loff_t *ppos)
1198 char buffer[PROC_NUMBUF];
1202 memset(buffer, 0, sizeof(buffer));
1203 if (count > sizeof(buffer) - 1)
1204 count = sizeof(buffer) - 1;
1205 if (copy_from_user(buffer, buf, count)) {
1210 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
1213 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
1214 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1219 err = __set_oom_adj(file, oom_score_adj, false);
1221 return err < 0 ? err : count;
1224 static const struct file_operations proc_oom_score_adj_operations = {
1225 .read = oom_score_adj_read,
1226 .write = oom_score_adj_write,
1227 .llseek = default_llseek,
1230 #ifdef CONFIG_AUDITSYSCALL
1231 #define TMPBUFLEN 11
1232 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1233 size_t count, loff_t *ppos)
1235 struct inode * inode = file_inode(file);
1236 struct task_struct *task = get_proc_task(inode);
1238 char tmpbuf[TMPBUFLEN];
1242 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1243 from_kuid(file->f_cred->user_ns,
1244 audit_get_loginuid(task)));
1245 put_task_struct(task);
1246 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1249 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1250 size_t count, loff_t *ppos)
1252 struct inode * inode = file_inode(file);
1258 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1265 /* No partial writes. */
1269 rv = kstrtou32_from_user(buf, count, 10, &loginuid);
1273 /* is userspace tring to explicitly UNSET the loginuid? */
1274 if (loginuid == AUDIT_UID_UNSET) {
1275 kloginuid = INVALID_UID;
1277 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
1278 if (!uid_valid(kloginuid))
1282 rv = audit_set_loginuid(kloginuid);
1288 static const struct file_operations proc_loginuid_operations = {
1289 .read = proc_loginuid_read,
1290 .write = proc_loginuid_write,
1291 .llseek = generic_file_llseek,
1294 static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1295 size_t count, loff_t *ppos)
1297 struct inode * inode = file_inode(file);
1298 struct task_struct *task = get_proc_task(inode);
1300 char tmpbuf[TMPBUFLEN];
1304 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1305 audit_get_sessionid(task));
1306 put_task_struct(task);
1307 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1310 static const struct file_operations proc_sessionid_operations = {
1311 .read = proc_sessionid_read,
1312 .llseek = generic_file_llseek,
1316 #ifdef CONFIG_FAULT_INJECTION
1317 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1318 size_t count, loff_t *ppos)
1320 struct task_struct *task = get_proc_task(file_inode(file));
1321 char buffer[PROC_NUMBUF];
1327 make_it_fail = task->make_it_fail;
1328 put_task_struct(task);
1330 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1332 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1335 static ssize_t proc_fault_inject_write(struct file * file,
1336 const char __user * buf, size_t count, loff_t *ppos)
1338 struct task_struct *task;
1339 char buffer[PROC_NUMBUF];
1343 if (!capable(CAP_SYS_RESOURCE))
1345 memset(buffer, 0, sizeof(buffer));
1346 if (count > sizeof(buffer) - 1)
1347 count = sizeof(buffer) - 1;
1348 if (copy_from_user(buffer, buf, count))
1350 rv = kstrtoint(strstrip(buffer), 0, &make_it_fail);
1353 if (make_it_fail < 0 || make_it_fail > 1)
1356 task = get_proc_task(file_inode(file));
1359 task->make_it_fail = make_it_fail;
1360 put_task_struct(task);
1365 static const struct file_operations proc_fault_inject_operations = {
1366 .read = proc_fault_inject_read,
1367 .write = proc_fault_inject_write,
1368 .llseek = generic_file_llseek,
1371 static ssize_t proc_fail_nth_write(struct file *file, const char __user *buf,
1372 size_t count, loff_t *ppos)
1374 struct task_struct *task;
1378 err = kstrtouint_from_user(buf, count, 0, &n);
1382 task = get_proc_task(file_inode(file));
1385 WRITE_ONCE(task->fail_nth, n);
1386 put_task_struct(task);
1391 static ssize_t proc_fail_nth_read(struct file *file, char __user *buf,
1392 size_t count, loff_t *ppos)
1394 struct task_struct *task;
1395 char numbuf[PROC_NUMBUF];
1398 task = get_proc_task(file_inode(file));
1401 len = snprintf(numbuf, sizeof(numbuf), "%u\n",
1402 READ_ONCE(task->fail_nth));
1403 len = simple_read_from_buffer(buf, count, ppos, numbuf, len);
1404 put_task_struct(task);
1409 static const struct file_operations proc_fail_nth_operations = {
1410 .read = proc_fail_nth_read,
1411 .write = proc_fail_nth_write,
1416 #ifdef CONFIG_SCHED_DEBUG
1418 * Print out various scheduling related per-task fields:
1420 static int sched_show(struct seq_file *m, void *v)
1422 struct inode *inode = m->private;
1423 struct pid_namespace *ns = inode->i_sb->s_fs_info;
1424 struct task_struct *p;
1426 p = get_proc_task(inode);
1429 proc_sched_show_task(p, ns, m);
1437 sched_write(struct file *file, const char __user *buf,
1438 size_t count, loff_t *offset)
1440 struct inode *inode = file_inode(file);
1441 struct task_struct *p;
1443 p = get_proc_task(inode);
1446 proc_sched_set_task(p);
1453 static int sched_open(struct inode *inode, struct file *filp)
1455 return single_open(filp, sched_show, inode);
1458 static const struct file_operations proc_pid_sched_operations = {
1461 .write = sched_write,
1462 .llseek = seq_lseek,
1463 .release = single_release,
1468 #ifdef CONFIG_SCHED_AUTOGROUP
1470 * Print out autogroup related information:
1472 static int sched_autogroup_show(struct seq_file *m, void *v)
1474 struct inode *inode = m->private;
1475 struct task_struct *p;
1477 p = get_proc_task(inode);
1480 proc_sched_autogroup_show_task(p, m);
1488 sched_autogroup_write(struct file *file, const char __user *buf,
1489 size_t count, loff_t *offset)
1491 struct inode *inode = file_inode(file);
1492 struct task_struct *p;
1493 char buffer[PROC_NUMBUF];
1497 memset(buffer, 0, sizeof(buffer));
1498 if (count > sizeof(buffer) - 1)
1499 count = sizeof(buffer) - 1;
1500 if (copy_from_user(buffer, buf, count))
1503 err = kstrtoint(strstrip(buffer), 0, &nice);
1507 p = get_proc_task(inode);
1511 err = proc_sched_autogroup_set_nice(p, nice);
1520 static int sched_autogroup_open(struct inode *inode, struct file *filp)
1524 ret = single_open(filp, sched_autogroup_show, NULL);
1526 struct seq_file *m = filp->private_data;
1533 static const struct file_operations proc_pid_sched_autogroup_operations = {
1534 .open = sched_autogroup_open,
1536 .write = sched_autogroup_write,
1537 .llseek = seq_lseek,
1538 .release = single_release,
1541 #endif /* CONFIG_SCHED_AUTOGROUP */
1543 static ssize_t comm_write(struct file *file, const char __user *buf,
1544 size_t count, loff_t *offset)
1546 struct inode *inode = file_inode(file);
1547 struct task_struct *p;
1548 char buffer[TASK_COMM_LEN];
1549 const size_t maxlen = sizeof(buffer) - 1;
1551 memset(buffer, 0, sizeof(buffer));
1552 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
1555 p = get_proc_task(inode);
1559 if (same_thread_group(current, p))
1560 set_task_comm(p, buffer);
1569 static int comm_show(struct seq_file *m, void *v)
1571 struct inode *inode = m->private;
1572 struct task_struct *p;
1574 p = get_proc_task(inode);
1579 seq_printf(m, "%s\n", p->comm);
1587 static int comm_open(struct inode *inode, struct file *filp)
1589 return single_open(filp, comm_show, inode);
1592 static const struct file_operations proc_pid_set_comm_operations = {
1595 .write = comm_write,
1596 .llseek = seq_lseek,
1597 .release = single_release,
1600 static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
1602 struct task_struct *task;
1603 struct file *exe_file;
1605 task = get_proc_task(d_inode(dentry));
1608 exe_file = get_task_exe_file(task);
1609 put_task_struct(task);
1611 *exe_path = exe_file->f_path;
1612 path_get(&exe_file->f_path);
1619 static const char *proc_pid_get_link(struct dentry *dentry,
1620 struct inode *inode,
1621 struct delayed_call *done)
1624 int error = -EACCES;
1627 return ERR_PTR(-ECHILD);
1629 /* Are we allowed to snoop on the tasks file descriptors? */
1630 if (!proc_fd_access_allowed(inode))
1633 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1637 nd_jump_link(&path);
1640 return ERR_PTR(error);
1643 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1645 char *tmp = (char *)__get_free_page(GFP_KERNEL);
1652 pathname = d_path(path, tmp, PAGE_SIZE);
1653 len = PTR_ERR(pathname);
1654 if (IS_ERR(pathname))
1656 len = tmp + PAGE_SIZE - 1 - pathname;
1660 if (copy_to_user(buffer, pathname, len))
1663 free_page((unsigned long)tmp);
1667 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1669 int error = -EACCES;
1670 struct inode *inode = d_inode(dentry);
1673 /* Are we allowed to snoop on the tasks file descriptors? */
1674 if (!proc_fd_access_allowed(inode))
1677 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1681 error = do_proc_readlink(&path, buffer, buflen);
1687 const struct inode_operations proc_pid_link_inode_operations = {
1688 .readlink = proc_pid_readlink,
1689 .get_link = proc_pid_get_link,
1690 .setattr = proc_setattr,
1694 /* building an inode */
1696 void task_dump_owner(struct task_struct *task, mode_t mode,
1697 kuid_t *ruid, kgid_t *rgid)
1699 /* Depending on the state of dumpable compute who should own a
1700 * proc file for a task.
1702 const struct cred *cred;
1706 if (unlikely(task->flags & PF_KTHREAD)) {
1707 *ruid = GLOBAL_ROOT_UID;
1708 *rgid = GLOBAL_ROOT_GID;
1712 /* Default to the tasks effective ownership */
1714 cred = __task_cred(task);
1720 * Before the /proc/pid/status file was created the only way to read
1721 * the effective uid of a /process was to stat /proc/pid. Reading
1722 * /proc/pid/status is slow enough that procps and other packages
1723 * kept stating /proc/pid. To keep the rules in /proc simple I have
1724 * made this apply to all per process world readable and executable
1727 if (mode != (S_IFDIR|S_IRUGO|S_IXUGO)) {
1728 struct mm_struct *mm;
1731 /* Make non-dumpable tasks owned by some root */
1733 if (get_dumpable(mm) != SUID_DUMP_USER) {
1734 struct user_namespace *user_ns = mm->user_ns;
1736 uid = make_kuid(user_ns, 0);
1737 if (!uid_valid(uid))
1738 uid = GLOBAL_ROOT_UID;
1740 gid = make_kgid(user_ns, 0);
1741 if (!gid_valid(gid))
1742 gid = GLOBAL_ROOT_GID;
1745 uid = GLOBAL_ROOT_UID;
1746 gid = GLOBAL_ROOT_GID;
1754 struct inode *proc_pid_make_inode(struct super_block * sb,
1755 struct task_struct *task, umode_t mode)
1757 struct inode * inode;
1758 struct proc_inode *ei;
1760 /* We need a new inode */
1762 inode = new_inode(sb);
1768 inode->i_mode = mode;
1769 inode->i_ino = get_next_ino();
1770 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
1771 inode->i_op = &proc_def_inode_operations;
1774 * grab the reference to task.
1776 ei->pid = get_task_pid(task, PIDTYPE_PID);
1780 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
1781 security_task_to_inode(task, inode);
1791 int pid_getattr(const struct path *path, struct kstat *stat,
1792 u32 request_mask, unsigned int query_flags)
1794 struct inode *inode = d_inode(path->dentry);
1795 struct task_struct *task;
1796 struct pid_namespace *pid = path->dentry->d_sb->s_fs_info;
1798 generic_fillattr(inode, stat);
1801 stat->uid = GLOBAL_ROOT_UID;
1802 stat->gid = GLOBAL_ROOT_GID;
1803 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1805 if (!has_pid_permissions(pid, task, HIDEPID_INVISIBLE)) {
1808 * This doesn't prevent learning whether PID exists,
1809 * it only makes getattr() consistent with readdir().
1813 task_dump_owner(task, inode->i_mode, &stat->uid, &stat->gid);
1822 * Exceptional case: normally we are not allowed to unhash a busy
1823 * directory. In this case, however, we can do it - no aliasing problems
1824 * due to the way we treat inodes.
1826 * Rewrite the inode's ownerships here because the owning task may have
1827 * performed a setuid(), etc.
1830 int pid_revalidate(struct dentry *dentry, unsigned int flags)
1832 struct inode *inode;
1833 struct task_struct *task;
1835 if (flags & LOOKUP_RCU)
1838 inode = d_inode(dentry);
1839 task = get_proc_task(inode);
1842 task_dump_owner(task, inode->i_mode, &inode->i_uid, &inode->i_gid);
1844 inode->i_mode &= ~(S_ISUID | S_ISGID);
1845 security_task_to_inode(task, inode);
1846 put_task_struct(task);
1852 static inline bool proc_inode_is_dead(struct inode *inode)
1854 return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1857 int pid_delete_dentry(const struct dentry *dentry)
1859 /* Is the task we represent dead?
1860 * If so, then don't put the dentry on the lru list,
1861 * kill it immediately.
1863 return proc_inode_is_dead(d_inode(dentry));
1866 const struct dentry_operations pid_dentry_operations =
1868 .d_revalidate = pid_revalidate,
1869 .d_delete = pid_delete_dentry,
1875 * Fill a directory entry.
1877 * If possible create the dcache entry and derive our inode number and
1878 * file type from dcache entry.
1880 * Since all of the proc inode numbers are dynamically generated, the inode
1881 * numbers do not exist until the inode is cache. This means creating the
1882 * the dcache entry in readdir is necessary to keep the inode numbers
1883 * reported by readdir in sync with the inode numbers reported
1886 bool proc_fill_cache(struct file *file, struct dir_context *ctx,
1887 const char *name, int len,
1888 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1890 struct dentry *child, *dir = file->f_path.dentry;
1891 struct qstr qname = QSTR_INIT(name, len);
1892 struct inode *inode;
1896 child = d_hash_and_lookup(dir, &qname);
1898 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1899 child = d_alloc_parallel(dir, &qname, &wq);
1901 goto end_instantiate;
1902 if (d_in_lookup(child)) {
1903 int err = instantiate(d_inode(dir), child, task, ptr);
1904 d_lookup_done(child);
1907 goto end_instantiate;
1911 inode = d_inode(child);
1913 type = inode->i_mode >> 12;
1915 return dir_emit(ctx, name, len, ino, type);
1918 return dir_emit(ctx, name, len, 1, DT_UNKNOWN);
1922 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1923 * which represent vma start and end addresses.
1925 static int dname_to_vma_addr(struct dentry *dentry,
1926 unsigned long *start, unsigned long *end)
1928 const char *str = dentry->d_name.name;
1929 unsigned long long sval, eval;
1932 len = _parse_integer(str, 16, &sval);
1933 if (len & KSTRTOX_OVERFLOW)
1935 if (sval != (unsigned long)sval)
1943 len = _parse_integer(str, 16, &eval);
1944 if (len & KSTRTOX_OVERFLOW)
1946 if (eval != (unsigned long)eval)
1959 static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
1961 unsigned long vm_start, vm_end;
1962 bool exact_vma_exists = false;
1963 struct mm_struct *mm = NULL;
1964 struct task_struct *task;
1965 struct inode *inode;
1968 if (flags & LOOKUP_RCU)
1971 inode = d_inode(dentry);
1972 task = get_proc_task(inode);
1976 mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
1977 if (IS_ERR_OR_NULL(mm))
1980 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1981 down_read(&mm->mmap_sem);
1982 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1983 up_read(&mm->mmap_sem);
1988 if (exact_vma_exists) {
1989 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
1991 security_task_to_inode(task, inode);
1996 put_task_struct(task);
2002 static const struct dentry_operations tid_map_files_dentry_operations = {
2003 .d_revalidate = map_files_d_revalidate,
2004 .d_delete = pid_delete_dentry,
2007 static int map_files_get_link(struct dentry *dentry, struct path *path)
2009 unsigned long vm_start, vm_end;
2010 struct vm_area_struct *vma;
2011 struct task_struct *task;
2012 struct mm_struct *mm;
2016 task = get_proc_task(d_inode(dentry));
2020 mm = get_task_mm(task);
2021 put_task_struct(task);
2025 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
2030 down_read(&mm->mmap_sem);
2031 vma = find_exact_vma(mm, vm_start, vm_end);
2032 if (vma && vma->vm_file) {
2033 *path = vma->vm_file->f_path;
2037 up_read(&mm->mmap_sem);
2045 struct map_files_info {
2048 unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
2052 * Only allow CAP_SYS_ADMIN to follow the links, due to concerns about how the
2053 * symlinks may be used to bypass permissions on ancestor directories in the
2054 * path to the file in question.
2057 proc_map_files_get_link(struct dentry *dentry,
2058 struct inode *inode,
2059 struct delayed_call *done)
2061 if (!capable(CAP_SYS_ADMIN))
2062 return ERR_PTR(-EPERM);
2064 return proc_pid_get_link(dentry, inode, done);
2068 * Identical to proc_pid_link_inode_operations except for get_link()
2070 static const struct inode_operations proc_map_files_link_inode_operations = {
2071 .readlink = proc_pid_readlink,
2072 .get_link = proc_map_files_get_link,
2073 .setattr = proc_setattr,
2077 proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
2078 struct task_struct *task, const void *ptr)
2080 fmode_t mode = (fmode_t)(unsigned long)ptr;
2081 struct proc_inode *ei;
2082 struct inode *inode;
2084 inode = proc_pid_make_inode(dir->i_sb, task, S_IFLNK |
2085 ((mode & FMODE_READ ) ? S_IRUSR : 0) |
2086 ((mode & FMODE_WRITE) ? S_IWUSR : 0));
2091 ei->op.proc_get_link = map_files_get_link;
2093 inode->i_op = &proc_map_files_link_inode_operations;
2096 d_set_d_op(dentry, &tid_map_files_dentry_operations);
2097 d_add(dentry, inode);
2102 static struct dentry *proc_map_files_lookup(struct inode *dir,
2103 struct dentry *dentry, unsigned int flags)
2105 unsigned long vm_start, vm_end;
2106 struct vm_area_struct *vma;
2107 struct task_struct *task;
2109 struct mm_struct *mm;
2112 task = get_proc_task(dir);
2117 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
2121 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
2124 mm = get_task_mm(task);
2128 down_read(&mm->mmap_sem);
2129 vma = find_exact_vma(mm, vm_start, vm_end);
2134 result = proc_map_files_instantiate(dir, dentry, task,
2135 (void *)(unsigned long)vma->vm_file->f_mode);
2138 up_read(&mm->mmap_sem);
2141 put_task_struct(task);
2143 return ERR_PTR(result);
2146 static const struct inode_operations proc_map_files_inode_operations = {
2147 .lookup = proc_map_files_lookup,
2148 .permission = proc_fd_permission,
2149 .setattr = proc_setattr,
2153 proc_map_files_readdir(struct file *file, struct dir_context *ctx)
2155 struct vm_area_struct *vma;
2156 struct task_struct *task;
2157 struct mm_struct *mm;
2158 unsigned long nr_files, pos, i;
2159 struct flex_array *fa = NULL;
2160 struct map_files_info info;
2161 struct map_files_info *p;
2165 task = get_proc_task(file_inode(file));
2170 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
2174 if (!dir_emit_dots(file, ctx))
2177 mm = get_task_mm(task);
2180 down_read(&mm->mmap_sem);
2185 * We need two passes here:
2187 * 1) Collect vmas of mapped files with mmap_sem taken
2188 * 2) Release mmap_sem and instantiate entries
2190 * otherwise we get lockdep complained, since filldir()
2191 * routine might require mmap_sem taken in might_fault().
2194 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
2195 if (vma->vm_file && ++pos > ctx->pos)
2200 fa = flex_array_alloc(sizeof(info), nr_files,
2202 if (!fa || flex_array_prealloc(fa, 0, nr_files,
2206 flex_array_free(fa);
2207 up_read(&mm->mmap_sem);
2211 for (i = 0, vma = mm->mmap, pos = 2; vma;
2212 vma = vma->vm_next) {
2215 if (++pos <= ctx->pos)
2218 info.mode = vma->vm_file->f_mode;
2219 info.len = snprintf(info.name,
2220 sizeof(info.name), "%lx-%lx",
2221 vma->vm_start, vma->vm_end);
2222 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
2226 up_read(&mm->mmap_sem);
2228 for (i = 0; i < nr_files; i++) {
2229 p = flex_array_get(fa, i);
2230 if (!proc_fill_cache(file, ctx,
2232 proc_map_files_instantiate,
2234 (void *)(unsigned long)p->mode))
2239 flex_array_free(fa);
2243 put_task_struct(task);
2248 static const struct file_operations proc_map_files_operations = {
2249 .read = generic_read_dir,
2250 .iterate_shared = proc_map_files_readdir,
2251 .llseek = generic_file_llseek,
2254 #if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
2255 struct timers_private {
2257 struct task_struct *task;
2258 struct sighand_struct *sighand;
2259 struct pid_namespace *ns;
2260 unsigned long flags;
2263 static void *timers_start(struct seq_file *m, loff_t *pos)
2265 struct timers_private *tp = m->private;
2267 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2269 return ERR_PTR(-ESRCH);
2271 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2273 return ERR_PTR(-ESRCH);
2275 return seq_list_start(&tp->task->signal->posix_timers, *pos);
2278 static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2280 struct timers_private *tp = m->private;
2281 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2284 static void timers_stop(struct seq_file *m, void *v)
2286 struct timers_private *tp = m->private;
2289 unlock_task_sighand(tp->task, &tp->flags);
2294 put_task_struct(tp->task);
2299 static int show_timer(struct seq_file *m, void *v)
2301 struct k_itimer *timer;
2302 struct timers_private *tp = m->private;
2304 static const char * const nstr[] = {
2305 [SIGEV_SIGNAL] = "signal",
2306 [SIGEV_NONE] = "none",
2307 [SIGEV_THREAD] = "thread",
2310 timer = list_entry((struct list_head *)v, struct k_itimer, list);
2311 notify = timer->it_sigev_notify;
2313 seq_printf(m, "ID: %d\n", timer->it_id);
2314 seq_printf(m, "signal: %d/%p\n",
2315 timer->sigq->info.si_signo,
2316 timer->sigq->info.si_value.sival_ptr);
2317 seq_printf(m, "notify: %s/%s.%d\n",
2318 nstr[notify & ~SIGEV_THREAD_ID],
2319 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2320 pid_nr_ns(timer->it_pid, tp->ns));
2321 seq_printf(m, "ClockID: %d\n", timer->it_clock);
2326 static const struct seq_operations proc_timers_seq_ops = {
2327 .start = timers_start,
2328 .next = timers_next,
2329 .stop = timers_stop,
2333 static int proc_timers_open(struct inode *inode, struct file *file)
2335 struct timers_private *tp;
2337 tp = __seq_open_private(file, &proc_timers_seq_ops,
2338 sizeof(struct timers_private));
2342 tp->pid = proc_pid(inode);
2343 tp->ns = inode->i_sb->s_fs_info;
2347 static const struct file_operations proc_timers_operations = {
2348 .open = proc_timers_open,
2350 .llseek = seq_lseek,
2351 .release = seq_release_private,
2355 static ssize_t timerslack_ns_write(struct file *file, const char __user *buf,
2356 size_t count, loff_t *offset)
2358 struct inode *inode = file_inode(file);
2359 struct task_struct *p;
2363 err = kstrtoull_from_user(buf, count, 10, &slack_ns);
2367 p = get_proc_task(inode);
2372 if (!capable(CAP_SYS_NICE)) {
2377 err = security_task_setscheduler(p);
2386 p->timer_slack_ns = p->default_timer_slack_ns;
2388 p->timer_slack_ns = slack_ns;
2397 static int timerslack_ns_show(struct seq_file *m, void *v)
2399 struct inode *inode = m->private;
2400 struct task_struct *p;
2403 p = get_proc_task(inode);
2409 if (!capable(CAP_SYS_NICE)) {
2413 err = security_task_getscheduler(p);
2419 seq_printf(m, "%llu\n", p->timer_slack_ns);
2428 static int timerslack_ns_open(struct inode *inode, struct file *filp)
2430 return single_open(filp, timerslack_ns_show, inode);
2433 static const struct file_operations proc_pid_set_timerslack_ns_operations = {
2434 .open = timerslack_ns_open,
2436 .write = timerslack_ns_write,
2437 .llseek = seq_lseek,
2438 .release = single_release,
2441 static int proc_pident_instantiate(struct inode *dir,
2442 struct dentry *dentry, struct task_struct *task, const void *ptr)
2444 const struct pid_entry *p = ptr;
2445 struct inode *inode;
2446 struct proc_inode *ei;
2448 inode = proc_pid_make_inode(dir->i_sb, task, p->mode);
2453 if (S_ISDIR(inode->i_mode))
2454 set_nlink(inode, 2); /* Use getattr to fix if necessary */
2456 inode->i_op = p->iop;
2458 inode->i_fop = p->fop;
2460 d_set_d_op(dentry, &pid_dentry_operations);
2461 d_add(dentry, inode);
2462 /* Close the race of the process dying before we return the dentry */
2463 if (pid_revalidate(dentry, 0))
2469 static struct dentry *proc_pident_lookup(struct inode *dir,
2470 struct dentry *dentry,
2471 const struct pid_entry *ents,
2475 struct task_struct *task = get_proc_task(dir);
2476 const struct pid_entry *p, *last;
2484 * Yes, it does not scale. And it should not. Don't add
2485 * new entries into /proc/<tgid>/ without very good reasons.
2487 last = &ents[nents];
2488 for (p = ents; p < last; p++) {
2489 if (p->len != dentry->d_name.len)
2491 if (!memcmp(dentry->d_name.name, p->name, p->len))
2497 error = proc_pident_instantiate(dir, dentry, task, p);
2499 put_task_struct(task);
2501 return ERR_PTR(error);
2504 static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
2505 const struct pid_entry *ents, unsigned int nents)
2507 struct task_struct *task = get_proc_task(file_inode(file));
2508 const struct pid_entry *p;
2513 if (!dir_emit_dots(file, ctx))
2516 if (ctx->pos >= nents + 2)
2519 for (p = ents + (ctx->pos - 2); p < ents + nents; p++) {
2520 if (!proc_fill_cache(file, ctx, p->name, p->len,
2521 proc_pident_instantiate, task, p))
2526 put_task_struct(task);
2530 #ifdef CONFIG_SECURITY
2531 static int proc_pid_attr_open(struct inode *inode, struct file *file)
2533 file->private_data = NULL;
2534 __mem_open(inode, file, PTRACE_MODE_READ_FSCREDS);
2538 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2539 size_t count, loff_t *ppos)
2541 struct inode * inode = file_inode(file);
2544 struct task_struct *task = get_proc_task(inode);
2549 length = security_getprocattr(task,
2550 (char*)file->f_path.dentry->d_name.name,
2552 put_task_struct(task);
2554 length = simple_read_from_buffer(buf, count, ppos, p, length);
2559 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2560 size_t count, loff_t *ppos)
2562 struct inode * inode = file_inode(file);
2565 struct task_struct *task = get_proc_task(inode);
2567 /* A task may only write when it was the opener. */
2568 if (file->private_data != current->mm)
2575 /* A task may only write its own attributes. */
2577 if (current != task)
2580 if (count > PAGE_SIZE)
2583 /* No partial writes. */
2588 page = memdup_user(buf, count);
2590 length = PTR_ERR(page);
2594 /* Guard against adverse ptrace interaction */
2595 length = mutex_lock_interruptible(¤t->signal->cred_guard_mutex);
2599 length = security_setprocattr(file->f_path.dentry->d_name.name,
2601 mutex_unlock(¤t->signal->cred_guard_mutex);
2605 put_task_struct(task);
2610 static const struct file_operations proc_pid_attr_operations = {
2611 .open = proc_pid_attr_open,
2612 .read = proc_pid_attr_read,
2613 .write = proc_pid_attr_write,
2614 .llseek = generic_file_llseek,
2615 .release = mem_release,
2618 static const struct pid_entry attr_dir_stuff[] = {
2619 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2620 REG("prev", S_IRUGO, proc_pid_attr_operations),
2621 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2622 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2623 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2624 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2627 static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
2629 return proc_pident_readdir(file, ctx,
2630 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
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,
2639 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2640 struct dentry *dentry, unsigned int flags)
2642 return proc_pident_lookup(dir, dentry,
2643 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2646 static const struct inode_operations proc_attr_dir_inode_operations = {
2647 .lookup = proc_attr_dir_lookup,
2648 .getattr = pid_getattr,
2649 .setattr = proc_setattr,
2654 #ifdef CONFIG_ELF_CORE
2655 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2656 size_t count, loff_t *ppos)
2658 struct task_struct *task = get_proc_task(file_inode(file));
2659 struct mm_struct *mm;
2660 char buffer[PROC_NUMBUF];
2668 mm = get_task_mm(task);
2670 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2671 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2672 MMF_DUMP_FILTER_SHIFT));
2674 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2677 put_task_struct(task);
2682 static ssize_t proc_coredump_filter_write(struct file *file,
2683 const char __user *buf,
2687 struct task_struct *task;
2688 struct mm_struct *mm;
2694 ret = kstrtouint_from_user(buf, count, 0, &val);
2699 task = get_proc_task(file_inode(file));
2703 mm = get_task_mm(task);
2708 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2710 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2712 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2717 put_task_struct(task);
2724 static const struct file_operations proc_coredump_filter_operations = {
2725 .read = proc_coredump_filter_read,
2726 .write = proc_coredump_filter_write,
2727 .llseek = generic_file_llseek,
2731 #ifdef CONFIG_TASK_IO_ACCOUNTING
2732 static int do_io_accounting(struct task_struct *task, struct seq_file *m, int whole)
2734 struct task_io_accounting acct = task->ioac;
2735 unsigned long flags;
2738 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2742 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
2747 if (whole && lock_task_sighand(task, &flags)) {
2748 struct task_struct *t = task;
2750 task_io_accounting_add(&acct, &task->signal->ioac);
2751 while_each_thread(task, t)
2752 task_io_accounting_add(&acct, &t->ioac);
2754 unlock_task_sighand(task, &flags);
2761 "read_bytes: %llu\n"
2762 "write_bytes: %llu\n"
2763 "cancelled_write_bytes: %llu\n",
2764 (unsigned long long)acct.rchar,
2765 (unsigned long long)acct.wchar,
2766 (unsigned long long)acct.syscr,
2767 (unsigned long long)acct.syscw,
2768 (unsigned long long)acct.read_bytes,
2769 (unsigned long long)acct.write_bytes,
2770 (unsigned long long)acct.cancelled_write_bytes);
2774 mutex_unlock(&task->signal->cred_guard_mutex);
2778 static int proc_tid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2779 struct pid *pid, struct task_struct *task)
2781 return do_io_accounting(task, m, 0);
2784 static int proc_tgid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2785 struct pid *pid, struct task_struct *task)
2787 return do_io_accounting(task, m, 1);
2789 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2791 #ifdef CONFIG_USER_NS
2792 static int proc_id_map_open(struct inode *inode, struct file *file,
2793 const struct seq_operations *seq_ops)
2795 struct user_namespace *ns = NULL;
2796 struct task_struct *task;
2797 struct seq_file *seq;
2800 task = get_proc_task(inode);
2803 ns = get_user_ns(task_cred_xxx(task, user_ns));
2805 put_task_struct(task);
2810 ret = seq_open(file, seq_ops);
2814 seq = file->private_data;
2824 static int proc_id_map_release(struct inode *inode, struct file *file)
2826 struct seq_file *seq = file->private_data;
2827 struct user_namespace *ns = seq->private;
2829 return seq_release(inode, file);
2832 static int proc_uid_map_open(struct inode *inode, struct file *file)
2834 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2837 static int proc_gid_map_open(struct inode *inode, struct file *file)
2839 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2842 static int proc_projid_map_open(struct inode *inode, struct file *file)
2844 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2847 static const struct file_operations proc_uid_map_operations = {
2848 .open = proc_uid_map_open,
2849 .write = proc_uid_map_write,
2851 .llseek = seq_lseek,
2852 .release = proc_id_map_release,
2855 static const struct file_operations proc_gid_map_operations = {
2856 .open = proc_gid_map_open,
2857 .write = proc_gid_map_write,
2859 .llseek = seq_lseek,
2860 .release = proc_id_map_release,
2863 static const struct file_operations proc_projid_map_operations = {
2864 .open = proc_projid_map_open,
2865 .write = proc_projid_map_write,
2867 .llseek = seq_lseek,
2868 .release = proc_id_map_release,
2871 static int proc_setgroups_open(struct inode *inode, struct file *file)
2873 struct user_namespace *ns = NULL;
2874 struct task_struct *task;
2878 task = get_proc_task(inode);
2881 ns = get_user_ns(task_cred_xxx(task, user_ns));
2883 put_task_struct(task);
2888 if (file->f_mode & FMODE_WRITE) {
2890 if (!ns_capable(ns, CAP_SYS_ADMIN))
2894 ret = single_open(file, &proc_setgroups_show, ns);
2905 static int proc_setgroups_release(struct inode *inode, struct file *file)
2907 struct seq_file *seq = file->private_data;
2908 struct user_namespace *ns = seq->private;
2909 int ret = single_release(inode, file);
2914 static const struct file_operations proc_setgroups_operations = {
2915 .open = proc_setgroups_open,
2916 .write = proc_setgroups_write,
2918 .llseek = seq_lseek,
2919 .release = proc_setgroups_release,
2921 #endif /* CONFIG_USER_NS */
2923 static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2924 struct pid *pid, struct task_struct *task)
2926 int err = lock_trace(task);
2928 seq_printf(m, "%08x\n", task->personality);
2934 #ifdef CONFIG_LIVEPATCH
2935 static int proc_pid_patch_state(struct seq_file *m, struct pid_namespace *ns,
2936 struct pid *pid, struct task_struct *task)
2938 seq_printf(m, "%d\n", task->patch_state);
2941 #endif /* CONFIG_LIVEPATCH */
2946 static const struct file_operations proc_task_operations;
2947 static const struct inode_operations proc_task_inode_operations;
2949 static const struct pid_entry tgid_base_stuff[] = {
2950 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2951 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2952 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
2953 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2954 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
2956 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2958 REG("environ", S_IRUSR, proc_environ_operations),
2959 REG("auxv", S_IRUSR, proc_auxv_operations),
2960 ONE("status", S_IRUGO, proc_pid_status),
2961 ONE("personality", S_IRUSR, proc_pid_personality),
2962 ONE("limits", S_IRUGO, proc_pid_limits),
2963 #ifdef CONFIG_SCHED_DEBUG
2964 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2966 #ifdef CONFIG_SCHED_AUTOGROUP
2967 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2969 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2970 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2971 ONE("syscall", S_IRUSR, proc_pid_syscall),
2973 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
2974 ONE("stat", S_IRUGO, proc_tgid_stat),
2975 ONE("statm", S_IRUGO, proc_pid_statm),
2976 REG("maps", S_IRUGO, proc_pid_maps_operations),
2978 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
2980 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2981 LNK("cwd", proc_cwd_link),
2982 LNK("root", proc_root_link),
2983 LNK("exe", proc_exe_link),
2984 REG("mounts", S_IRUGO, proc_mounts_operations),
2985 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2986 REG("mountstats", S_IRUSR, proc_mountstats_operations),
2987 #ifdef CONFIG_PROC_PAGE_MONITOR
2988 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2989 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
2990 REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
2991 REG("pagemap", S_IRUSR, proc_pagemap_operations),
2993 #ifdef CONFIG_SECURITY
2994 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
2996 #ifdef CONFIG_KALLSYMS
2997 ONE("wchan", S_IRUGO, proc_pid_wchan),
2999 #ifdef CONFIG_STACKTRACE
3000 ONE("stack", S_IRUSR, proc_pid_stack),
3002 #ifdef CONFIG_SCHED_INFO
3003 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
3005 #ifdef CONFIG_LATENCYTOP
3006 REG("latency", S_IRUGO, proc_lstats_operations),
3008 #ifdef CONFIG_PROC_PID_CPUSET
3009 ONE("cpuset", S_IRUGO, proc_cpuset_show),
3011 #ifdef CONFIG_CGROUPS
3012 ONE("cgroup", S_IRUGO, proc_cgroup_show),
3014 ONE("oom_score", S_IRUGO, proc_oom_score),
3015 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
3016 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
3017 #ifdef CONFIG_AUDITSYSCALL
3018 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3019 REG("sessionid", S_IRUGO, proc_sessionid_operations),
3021 #ifdef CONFIG_FAULT_INJECTION
3022 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3023 REG("fail-nth", 0644, proc_fail_nth_operations),
3025 #ifdef CONFIG_ELF_CORE
3026 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
3028 #ifdef CONFIG_TASK_IO_ACCOUNTING
3029 ONE("io", S_IRUSR, proc_tgid_io_accounting),
3031 #ifdef CONFIG_HARDWALL
3032 ONE("hardwall", S_IRUGO, proc_pid_hardwall),
3034 #ifdef CONFIG_USER_NS
3035 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3036 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
3037 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
3038 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
3040 #if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
3041 REG("timers", S_IRUGO, proc_timers_operations),
3043 REG("timerslack_ns", S_IRUGO|S_IWUGO, proc_pid_set_timerslack_ns_operations),
3044 #ifdef CONFIG_LIVEPATCH
3045 ONE("patch_state", S_IRUSR, proc_pid_patch_state),
3049 static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
3051 return proc_pident_readdir(file, ctx,
3052 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
3055 static const struct file_operations proc_tgid_base_operations = {
3056 .read = generic_read_dir,
3057 .iterate_shared = proc_tgid_base_readdir,
3058 .llseek = generic_file_llseek,
3061 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3063 return proc_pident_lookup(dir, dentry,
3064 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
3067 static const struct inode_operations proc_tgid_base_inode_operations = {
3068 .lookup = proc_tgid_base_lookup,
3069 .getattr = pid_getattr,
3070 .setattr = proc_setattr,
3071 .permission = proc_pid_permission,
3074 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
3076 struct dentry *dentry, *leader, *dir;
3077 char buf[PROC_NUMBUF];
3081 name.len = snprintf(buf, sizeof(buf), "%d", pid);
3082 /* no ->d_hash() rejects on procfs */
3083 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
3085 d_invalidate(dentry);
3093 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
3094 leader = d_hash_and_lookup(mnt->mnt_root, &name);
3099 name.len = strlen(name.name);
3100 dir = d_hash_and_lookup(leader, &name);
3102 goto out_put_leader;
3105 name.len = snprintf(buf, sizeof(buf), "%d", pid);
3106 dentry = d_hash_and_lookup(dir, &name);
3108 d_invalidate(dentry);
3120 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
3121 * @task: task that should be flushed.
3123 * When flushing dentries from proc, one needs to flush them from global
3124 * proc (proc_mnt) and from all the namespaces' procs this task was seen
3125 * in. This call is supposed to do all of this job.
3127 * Looks in the dcache for
3129 * /proc/@tgid/task/@pid
3130 * if either directory is present flushes it and all of it'ts children
3133 * It is safe and reasonable to cache /proc entries for a task until
3134 * that task exits. After that they just clog up the dcache with
3135 * useless entries, possibly causing useful dcache entries to be
3136 * flushed instead. This routine is proved to flush those useless
3137 * dcache entries at process exit time.
3139 * NOTE: This routine is just an optimization so it does not guarantee
3140 * that no dcache entries will exist at process exit time it
3141 * just makes it very unlikely that any will persist.
3144 void proc_flush_task(struct task_struct *task)
3147 struct pid *pid, *tgid;
3150 pid = task_pid(task);
3151 tgid = task_tgid(task);
3153 for (i = 0; i <= pid->level; i++) {
3154 upid = &pid->numbers[i];
3155 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
3156 tgid->numbers[i].nr);
3160 static int proc_pid_instantiate(struct inode *dir,
3161 struct dentry * dentry,
3162 struct task_struct *task, const void *ptr)
3164 struct inode *inode;
3166 inode = proc_pid_make_inode(dir->i_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
3170 inode->i_op = &proc_tgid_base_inode_operations;
3171 inode->i_fop = &proc_tgid_base_operations;
3172 inode->i_flags|=S_IMMUTABLE;
3174 set_nlink(inode, nlink_tgid);
3176 d_set_d_op(dentry, &pid_dentry_operations);
3178 d_add(dentry, inode);
3179 /* Close the race of the process dying before we return the dentry */
3180 if (pid_revalidate(dentry, 0))
3186 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
3188 int result = -ENOENT;
3189 struct task_struct *task;
3191 struct pid_namespace *ns;
3193 tgid = name_to_int(&dentry->d_name);
3197 ns = dentry->d_sb->s_fs_info;
3199 task = find_task_by_pid_ns(tgid, ns);
3201 get_task_struct(task);
3206 result = proc_pid_instantiate(dir, dentry, task, NULL);
3207 put_task_struct(task);
3209 return ERR_PTR(result);
3213 * Find the first task with tgid >= tgid
3218 struct task_struct *task;
3220 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3225 put_task_struct(iter.task);
3229 pid = find_ge_pid(iter.tgid, ns);
3231 iter.tgid = pid_nr_ns(pid, ns);
3232 iter.task = pid_task(pid, PIDTYPE_PID);
3233 /* What we to know is if the pid we have find is the
3234 * pid of a thread_group_leader. Testing for task
3235 * being a thread_group_leader is the obvious thing
3236 * todo but there is a window when it fails, due to
3237 * the pid transfer logic in de_thread.
3239 * So we perform the straight forward test of seeing
3240 * if the pid we have found is the pid of a thread
3241 * group leader, and don't worry if the task we have
3242 * found doesn't happen to be a thread group leader.
3243 * As we don't care in the case of readdir.
3245 if (!iter.task || !has_group_leader_pid(iter.task)) {
3249 get_task_struct(iter.task);
3255 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
3257 /* for the /proc/ directory itself, after non-process stuff has been done */
3258 int proc_pid_readdir(struct file *file, struct dir_context *ctx)
3260 struct tgid_iter iter;
3261 struct pid_namespace *ns = file_inode(file)->i_sb->s_fs_info;
3262 loff_t pos = ctx->pos;
3264 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
3267 if (pos == TGID_OFFSET - 2) {
3268 struct inode *inode = d_inode(ns->proc_self);
3269 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
3271 ctx->pos = pos = pos + 1;
3273 if (pos == TGID_OFFSET - 1) {
3274 struct inode *inode = d_inode(ns->proc_thread_self);
3275 if (!dir_emit(ctx, "thread-self", 11, inode->i_ino, DT_LNK))
3277 ctx->pos = pos = pos + 1;
3279 iter.tgid = pos - TGID_OFFSET;
3281 for (iter = next_tgid(ns, iter);
3283 iter.tgid += 1, iter = next_tgid(ns, iter)) {
3284 char name[PROC_NUMBUF];
3288 if (!has_pid_permissions(ns, iter.task, HIDEPID_INVISIBLE))
3291 len = snprintf(name, sizeof(name), "%d", iter.tgid);
3292 ctx->pos = iter.tgid + TGID_OFFSET;
3293 if (!proc_fill_cache(file, ctx, name, len,
3294 proc_pid_instantiate, iter.task, NULL)) {
3295 put_task_struct(iter.task);
3299 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
3304 * proc_tid_comm_permission is a special permission function exclusively
3305 * used for the node /proc/<pid>/task/<tid>/comm.
3306 * It bypasses generic permission checks in the case where a task of the same
3307 * task group attempts to access the node.
3308 * The rationale behind this is that glibc and bionic access this node for
3309 * cross thread naming (pthread_set/getname_np(!self)). However, if
3310 * PR_SET_DUMPABLE gets set to 0 this node among others becomes uid=0 gid=0,
3311 * which locks out the cross thread naming implementation.
3312 * This function makes sure that the node is always accessible for members of
3313 * same thread group.
3315 static int proc_tid_comm_permission(struct inode *inode, int mask)
3317 bool is_same_tgroup;
3318 struct task_struct *task;
3320 task = get_proc_task(inode);
3323 is_same_tgroup = same_thread_group(current, task);
3324 put_task_struct(task);
3326 if (likely(is_same_tgroup && !(mask & MAY_EXEC))) {
3327 /* This file (/proc/<pid>/task/<tid>/comm) can always be
3328 * read or written by the members of the corresponding
3334 return generic_permission(inode, mask);
3337 static const struct inode_operations proc_tid_comm_inode_operations = {
3338 .permission = proc_tid_comm_permission,
3344 static const struct pid_entry tid_base_stuff[] = {
3345 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
3346 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
3347 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
3349 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
3351 REG("environ", S_IRUSR, proc_environ_operations),
3352 REG("auxv", S_IRUSR, proc_auxv_operations),
3353 ONE("status", S_IRUGO, proc_pid_status),
3354 ONE("personality", S_IRUSR, proc_pid_personality),
3355 ONE("limits", S_IRUGO, proc_pid_limits),
3356 #ifdef CONFIG_SCHED_DEBUG
3357 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
3359 NOD("comm", S_IFREG|S_IRUGO|S_IWUSR,
3360 &proc_tid_comm_inode_operations,
3361 &proc_pid_set_comm_operations, {}),
3362 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3363 ONE("syscall", S_IRUSR, proc_pid_syscall),
3365 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
3366 ONE("stat", S_IRUGO, proc_tid_stat),
3367 ONE("statm", S_IRUGO, proc_pid_statm),
3368 REG("maps", S_IRUGO, proc_tid_maps_operations),
3369 #ifdef CONFIG_PROC_CHILDREN
3370 REG("children", S_IRUGO, proc_tid_children_operations),
3373 REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
3375 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3376 LNK("cwd", proc_cwd_link),
3377 LNK("root", proc_root_link),
3378 LNK("exe", proc_exe_link),
3379 REG("mounts", S_IRUGO, proc_mounts_operations),
3380 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
3381 #ifdef CONFIG_PROC_PAGE_MONITOR
3382 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3383 REG("smaps", S_IRUGO, proc_tid_smaps_operations),
3384 REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
3385 REG("pagemap", S_IRUSR, proc_pagemap_operations),
3387 #ifdef CONFIG_SECURITY
3388 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
3390 #ifdef CONFIG_KALLSYMS
3391 ONE("wchan", S_IRUGO, proc_pid_wchan),
3393 #ifdef CONFIG_STACKTRACE
3394 ONE("stack", S_IRUSR, proc_pid_stack),
3396 #ifdef CONFIG_SCHED_INFO
3397 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
3399 #ifdef CONFIG_LATENCYTOP
3400 REG("latency", S_IRUGO, proc_lstats_operations),
3402 #ifdef CONFIG_PROC_PID_CPUSET
3403 ONE("cpuset", S_IRUGO, proc_cpuset_show),
3405 #ifdef CONFIG_CGROUPS
3406 ONE("cgroup", S_IRUGO, proc_cgroup_show),
3408 ONE("oom_score", S_IRUGO, proc_oom_score),
3409 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
3410 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
3411 #ifdef CONFIG_AUDITSYSCALL
3412 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3413 REG("sessionid", S_IRUGO, proc_sessionid_operations),
3415 #ifdef CONFIG_FAULT_INJECTION
3416 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3417 REG("fail-nth", 0644, proc_fail_nth_operations),
3419 #ifdef CONFIG_TASK_IO_ACCOUNTING
3420 ONE("io", S_IRUSR, proc_tid_io_accounting),
3422 #ifdef CONFIG_HARDWALL
3423 ONE("hardwall", S_IRUGO, proc_pid_hardwall),
3425 #ifdef CONFIG_USER_NS
3426 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3427 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
3428 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
3429 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
3431 #ifdef CONFIG_LIVEPATCH
3432 ONE("patch_state", S_IRUSR, proc_pid_patch_state),
3436 static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
3438 return proc_pident_readdir(file, ctx,
3439 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3442 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3444 return proc_pident_lookup(dir, dentry,
3445 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3448 static const struct file_operations proc_tid_base_operations = {
3449 .read = generic_read_dir,
3450 .iterate_shared = proc_tid_base_readdir,
3451 .llseek = generic_file_llseek,
3454 static const struct inode_operations proc_tid_base_inode_operations = {
3455 .lookup = proc_tid_base_lookup,
3456 .getattr = pid_getattr,
3457 .setattr = proc_setattr,
3460 static int proc_task_instantiate(struct inode *dir,
3461 struct dentry *dentry, struct task_struct *task, const void *ptr)
3463 struct inode *inode;
3464 inode = proc_pid_make_inode(dir->i_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
3468 inode->i_op = &proc_tid_base_inode_operations;
3469 inode->i_fop = &proc_tid_base_operations;
3470 inode->i_flags|=S_IMMUTABLE;
3472 set_nlink(inode, nlink_tid);
3474 d_set_d_op(dentry, &pid_dentry_operations);
3476 d_add(dentry, inode);
3477 /* Close the race of the process dying before we return the dentry */
3478 if (pid_revalidate(dentry, 0))
3484 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
3486 int result = -ENOENT;
3487 struct task_struct *task;
3488 struct task_struct *leader = get_proc_task(dir);
3490 struct pid_namespace *ns;
3495 tid = name_to_int(&dentry->d_name);
3499 ns = dentry->d_sb->s_fs_info;
3501 task = find_task_by_pid_ns(tid, ns);
3503 get_task_struct(task);
3507 if (!same_thread_group(leader, task))
3510 result = proc_task_instantiate(dir, dentry, task, NULL);
3512 put_task_struct(task);
3514 put_task_struct(leader);
3516 return ERR_PTR(result);
3520 * Find the first tid of a thread group to return to user space.
3522 * Usually this is just the thread group leader, but if the users
3523 * buffer was too small or there was a seek into the middle of the
3524 * directory we have more work todo.
3526 * In the case of a short read we start with find_task_by_pid.
3528 * In the case of a seek we start with the leader and walk nr
3531 static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
3532 struct pid_namespace *ns)
3534 struct task_struct *pos, *task;
3535 unsigned long nr = f_pos;
3537 if (nr != f_pos) /* 32bit overflow? */
3541 task = pid_task(pid, PIDTYPE_PID);
3545 /* Attempt to start with the tid of a thread */
3547 pos = find_task_by_pid_ns(tid, ns);
3548 if (pos && same_thread_group(pos, task))
3552 /* If nr exceeds the number of threads there is nothing todo */
3553 if (nr >= get_nr_threads(task))
3556 /* If we haven't found our starting place yet start
3557 * with the leader and walk nr threads forward.
3559 pos = task = task->group_leader;
3563 } while_each_thread(task, pos);
3568 get_task_struct(pos);
3575 * Find the next thread in the thread list.
3576 * Return NULL if there is an error or no next thread.
3578 * The reference to the input task_struct is released.
3580 static struct task_struct *next_tid(struct task_struct *start)
3582 struct task_struct *pos = NULL;
3584 if (pid_alive(start)) {
3585 pos = next_thread(start);
3586 if (thread_group_leader(pos))
3589 get_task_struct(pos);
3592 put_task_struct(start);
3596 /* for the /proc/TGID/task/ directories */
3597 static int proc_task_readdir(struct file *file, struct dir_context *ctx)
3599 struct inode *inode = file_inode(file);
3600 struct task_struct *task;
3601 struct pid_namespace *ns;
3604 if (proc_inode_is_dead(inode))
3607 if (!dir_emit_dots(file, ctx))
3610 /* f_version caches the tgid value that the last readdir call couldn't
3611 * return. lseek aka telldir automagically resets f_version to 0.
3613 ns = inode->i_sb->s_fs_info;
3614 tid = (int)file->f_version;
3615 file->f_version = 0;
3616 for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
3618 task = next_tid(task), ctx->pos++) {
3619 char name[PROC_NUMBUF];
3621 tid = task_pid_nr_ns(task, ns);
3622 len = snprintf(name, sizeof(name), "%d", tid);
3623 if (!proc_fill_cache(file, ctx, name, len,
3624 proc_task_instantiate, task, NULL)) {
3625 /* returning this tgid failed, save it as the first
3626 * pid for the next readir call */
3627 file->f_version = (u64)tid;
3628 put_task_struct(task);
3636 static int proc_task_getattr(const struct path *path, struct kstat *stat,
3637 u32 request_mask, unsigned int query_flags)
3639 struct inode *inode = d_inode(path->dentry);
3640 struct task_struct *p = get_proc_task(inode);
3641 generic_fillattr(inode, stat);
3644 stat->nlink += get_nr_threads(p);
3651 static const struct inode_operations proc_task_inode_operations = {
3652 .lookup = proc_task_lookup,
3653 .getattr = proc_task_getattr,
3654 .setattr = proc_setattr,
3655 .permission = proc_pid_permission,
3658 static const struct file_operations proc_task_operations = {
3659 .read = generic_read_dir,
3660 .iterate_shared = proc_task_readdir,
3661 .llseek = generic_file_llseek,
3664 void __init set_proc_pid_nlink(void)
3666 nlink_tid = pid_entry_nlink(tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3667 nlink_tgid = pid_entry_nlink(tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));