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/generic-radix-tree.h>
63 #include <linux/string.h>
64 #include <linux/seq_file.h>
65 #include <linux/namei.h>
66 #include <linux/mnt_namespace.h>
68 #include <linux/swap.h>
69 #include <linux/rcupdate.h>
70 #include <linux/kallsyms.h>
71 #include <linux/stacktrace.h>
72 #include <linux/resource.h>
73 #include <linux/module.h>
74 #include <linux/mount.h>
75 #include <linux/security.h>
76 #include <linux/ptrace.h>
77 #include <linux/tracehook.h>
78 #include <linux/printk.h>
79 #include <linux/cache.h>
80 #include <linux/cgroup.h>
81 #include <linux/cpuset.h>
82 #include <linux/audit.h>
83 #include <linux/poll.h>
84 #include <linux/nsproxy.h>
85 #include <linux/oom.h>
86 #include <linux/elf.h>
87 #include <linux/pid_namespace.h>
88 #include <linux/user_namespace.h>
89 #include <linux/fs_struct.h>
90 #include <linux/slab.h>
91 #include <linux/sched/autogroup.h>
92 #include <linux/sched/mm.h>
93 #include <linux/sched/coredump.h>
94 #include <linux/sched/debug.h>
95 #include <linux/sched/stat.h>
96 #include <linux/posix-timers.h>
97 #include <trace/events/oom.h>
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 } )
146 #define ATTR(LSM, NAME, MODE) \
147 NOD(NAME, (S_IFREG|(MODE)), \
148 NULL, &proc_pid_attr_operations, \
152 * Count the number of hardlinks for the pid_entry table, excluding the .
155 static unsigned int __init pid_entry_nlink(const struct pid_entry *entries,
162 for (i = 0; i < n; ++i) {
163 if (S_ISDIR(entries[i].mode))
170 static int get_task_root(struct task_struct *task, struct path *root)
172 int result = -ENOENT;
176 get_fs_root(task->fs, root);
183 static int proc_cwd_link(struct dentry *dentry, struct path *path)
185 struct task_struct *task = get_proc_task(d_inode(dentry));
186 int result = -ENOENT;
191 get_fs_pwd(task->fs, path);
195 put_task_struct(task);
200 static int proc_root_link(struct dentry *dentry, struct path *path)
202 struct task_struct *task = get_proc_task(d_inode(dentry));
203 int result = -ENOENT;
206 result = get_task_root(task, path);
207 put_task_struct(task);
213 * If the user used setproctitle(), we just get the string from
214 * user space at arg_start, and limit it to a maximum of one page.
216 static ssize_t get_mm_proctitle(struct mm_struct *mm, char __user *buf,
217 size_t count, unsigned long pos,
218 unsigned long arg_start)
223 if (pos >= PAGE_SIZE)
226 page = (char *)__get_free_page(GFP_KERNEL);
231 got = access_remote_vm(mm, arg_start, page, PAGE_SIZE, FOLL_ANON);
233 int len = strnlen(page, got);
235 /* Include the NUL character if it was found */
243 len -= copy_to_user(buf, page+pos, len);
249 free_page((unsigned long)page);
253 static ssize_t get_mm_cmdline(struct mm_struct *mm, char __user *buf,
254 size_t count, loff_t *ppos)
256 unsigned long arg_start, arg_end, env_start, env_end;
257 unsigned long pos, len;
260 /* Check if process spawned far enough to have cmdline. */
264 spin_lock(&mm->arg_lock);
265 arg_start = mm->arg_start;
266 arg_end = mm->arg_end;
267 env_start = mm->env_start;
268 env_end = mm->env_end;
269 spin_unlock(&mm->arg_lock);
271 if (arg_start >= arg_end)
275 * We allow setproctitle() to overwrite the argument
276 * strings, and overflow past the original end. But
277 * only when it overflows into the environment area.
279 if (env_start != arg_end || env_end < env_start)
280 env_start = env_end = arg_end;
281 len = env_end - arg_start;
283 /* We're not going to care if "*ppos" has high bits set */
287 if (count > len - pos)
293 * Magical special case: if the argv[] end byte is not
294 * zero, the user has overwritten it with setproctitle(3).
296 * Possible future enhancement: do this only once when
297 * pos is 0, and set a flag in the 'struct file'.
299 if (access_remote_vm(mm, arg_end-1, &c, 1, FOLL_ANON) == 1 && c)
300 return get_mm_proctitle(mm, buf, count, pos, arg_start);
303 * For the non-setproctitle() case we limit things strictly
304 * to the [arg_start, arg_end[ range.
307 if (pos < arg_start || pos >= arg_end)
309 if (count > arg_end - pos)
310 count = arg_end - pos;
312 page = (char *)__get_free_page(GFP_KERNEL);
319 size_t size = min_t(size_t, PAGE_SIZE, count);
321 got = access_remote_vm(mm, pos, page, size, FOLL_ANON);
324 got -= copy_to_user(buf, page, got);
325 if (unlikely(!got)) {
336 free_page((unsigned long)page);
340 static ssize_t get_task_cmdline(struct task_struct *tsk, char __user *buf,
341 size_t count, loff_t *pos)
343 struct mm_struct *mm;
346 mm = get_task_mm(tsk);
350 ret = get_mm_cmdline(mm, buf, count, pos);
355 static ssize_t proc_pid_cmdline_read(struct file *file, char __user *buf,
356 size_t count, loff_t *pos)
358 struct task_struct *tsk;
363 tsk = get_proc_task(file_inode(file));
366 ret = get_task_cmdline(tsk, buf, count, pos);
367 put_task_struct(tsk);
373 static const struct file_operations proc_pid_cmdline_ops = {
374 .read = proc_pid_cmdline_read,
375 .llseek = generic_file_llseek,
378 #ifdef CONFIG_KALLSYMS
380 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
381 * Returns the resolved symbol. If that fails, simply return the address.
383 static int proc_pid_wchan(struct seq_file *m, struct pid_namespace *ns,
384 struct pid *pid, struct task_struct *task)
387 char symname[KSYM_NAME_LEN];
389 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
392 wchan = get_wchan(task);
393 if (wchan && !lookup_symbol_name(wchan, symname)) {
394 seq_puts(m, symname);
402 #endif /* CONFIG_KALLSYMS */
404 static int lock_trace(struct task_struct *task)
406 int err = down_read_killable(&task->signal->exec_update_lock);
409 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH_FSCREDS)) {
410 up_read(&task->signal->exec_update_lock);
416 static void unlock_trace(struct task_struct *task)
418 up_read(&task->signal->exec_update_lock);
421 #ifdef CONFIG_STACKTRACE
423 #define MAX_STACK_TRACE_DEPTH 64
425 static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
426 struct pid *pid, struct task_struct *task)
428 unsigned long *entries;
432 * The ability to racily run the kernel stack unwinder on a running task
433 * and then observe the unwinder output is scary; while it is useful for
434 * debugging kernel issues, it can also allow an attacker to leak kernel
436 * Doing this in a manner that is at least safe from races would require
437 * some work to ensure that the remote task can not be scheduled; and
438 * even then, this would still expose the unwinder as local attack
440 * Therefore, this interface is restricted to root.
442 if (!file_ns_capable(m->file, &init_user_ns, CAP_SYS_ADMIN))
445 entries = kmalloc_array(MAX_STACK_TRACE_DEPTH, sizeof(*entries),
450 err = lock_trace(task);
452 unsigned int i, nr_entries;
454 nr_entries = stack_trace_save_tsk(task, entries,
455 MAX_STACK_TRACE_DEPTH, 0);
457 for (i = 0; i < nr_entries; i++) {
458 seq_printf(m, "[<0>] %pB\n", (void *)entries[i]);
469 #ifdef CONFIG_SCHED_INFO
471 * Provides /proc/PID/schedstat
473 static int proc_pid_schedstat(struct seq_file *m, struct pid_namespace *ns,
474 struct pid *pid, struct task_struct *task)
476 if (unlikely(!sched_info_on()))
477 seq_puts(m, "0 0 0\n");
479 seq_printf(m, "%llu %llu %lu\n",
480 (unsigned long long)task->se.sum_exec_runtime,
481 (unsigned long long)task->sched_info.run_delay,
482 task->sched_info.pcount);
488 #ifdef CONFIG_LATENCYTOP
489 static int lstats_show_proc(struct seq_file *m, void *v)
492 struct inode *inode = m->private;
493 struct task_struct *task = get_proc_task(inode);
497 seq_puts(m, "Latency Top version : v0.1\n");
498 for (i = 0; i < LT_SAVECOUNT; i++) {
499 struct latency_record *lr = &task->latency_record[i];
500 if (lr->backtrace[0]) {
502 seq_printf(m, "%i %li %li",
503 lr->count, lr->time, lr->max);
504 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
505 unsigned long bt = lr->backtrace[q];
509 seq_printf(m, " %ps", (void *)bt);
515 put_task_struct(task);
519 static int lstats_open(struct inode *inode, struct file *file)
521 return single_open(file, lstats_show_proc, inode);
524 static ssize_t lstats_write(struct file *file, const char __user *buf,
525 size_t count, loff_t *offs)
527 struct task_struct *task = get_proc_task(file_inode(file));
531 clear_tsk_latency_tracing(task);
532 put_task_struct(task);
537 static const struct file_operations proc_lstats_operations = {
540 .write = lstats_write,
542 .release = single_release,
547 static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns,
548 struct pid *pid, struct task_struct *task)
550 unsigned long totalpages = totalram_pages() + total_swap_pages;
551 unsigned long points = 0;
554 badness = oom_badness(task, totalpages);
556 * Special case OOM_SCORE_ADJ_MIN for all others scale the
557 * badness value into [0, 2000] range which we have been
558 * exporting for a long time so userspace might depend on it.
560 if (badness != LONG_MIN)
561 points = (1000 + badness * 1000 / (long)totalpages) * 2 / 3;
563 seq_printf(m, "%lu\n", points);
573 static const struct limit_names lnames[RLIM_NLIMITS] = {
574 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
575 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
576 [RLIMIT_DATA] = {"Max data size", "bytes"},
577 [RLIMIT_STACK] = {"Max stack size", "bytes"},
578 [RLIMIT_CORE] = {"Max core file size", "bytes"},
579 [RLIMIT_RSS] = {"Max resident set", "bytes"},
580 [RLIMIT_NPROC] = {"Max processes", "processes"},
581 [RLIMIT_NOFILE] = {"Max open files", "files"},
582 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
583 [RLIMIT_AS] = {"Max address space", "bytes"},
584 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
585 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
586 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
587 [RLIMIT_NICE] = {"Max nice priority", NULL},
588 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
589 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
592 /* Display limits for a process */
593 static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
594 struct pid *pid, struct task_struct *task)
599 struct rlimit rlim[RLIM_NLIMITS];
601 if (!lock_task_sighand(task, &flags))
603 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
604 unlock_task_sighand(task, &flags);
607 * print the file header
614 for (i = 0; i < RLIM_NLIMITS; i++) {
615 if (rlim[i].rlim_cur == RLIM_INFINITY)
616 seq_printf(m, "%-25s %-20s ",
617 lnames[i].name, "unlimited");
619 seq_printf(m, "%-25s %-20lu ",
620 lnames[i].name, rlim[i].rlim_cur);
622 if (rlim[i].rlim_max == RLIM_INFINITY)
623 seq_printf(m, "%-20s ", "unlimited");
625 seq_printf(m, "%-20lu ", rlim[i].rlim_max);
628 seq_printf(m, "%-10s\n", lnames[i].unit);
636 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
637 static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
638 struct pid *pid, struct task_struct *task)
640 struct syscall_info info;
641 u64 *args = &info.data.args[0];
644 res = lock_trace(task);
648 if (task_current_syscall(task, &info))
649 seq_puts(m, "running\n");
650 else if (info.data.nr < 0)
651 seq_printf(m, "%d 0x%llx 0x%llx\n",
652 info.data.nr, info.sp, info.data.instruction_pointer);
655 "%d 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx\n",
657 args[0], args[1], args[2], args[3], args[4], args[5],
658 info.sp, info.data.instruction_pointer);
663 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
665 /************************************************************************/
666 /* Here the fs part begins */
667 /************************************************************************/
669 /* permission checks */
670 static int proc_fd_access_allowed(struct inode *inode)
672 struct task_struct *task;
674 /* Allow access to a task's file descriptors if it is us or we
675 * may use ptrace attach to the process and find out that
678 task = get_proc_task(inode);
680 allowed = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
681 put_task_struct(task);
686 int proc_setattr(struct dentry *dentry, struct iattr *attr)
689 struct inode *inode = d_inode(dentry);
691 if (attr->ia_valid & ATTR_MODE)
694 error = setattr_prepare(dentry, attr);
698 setattr_copy(inode, attr);
699 mark_inode_dirty(inode);
704 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
705 * or euid/egid (for hide_pid_min=2)?
707 static bool has_pid_permissions(struct pid_namespace *pid,
708 struct task_struct *task,
711 if (pid->hide_pid < hide_pid_min)
713 if (in_group_p(pid->pid_gid))
715 return ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
719 static int proc_pid_permission(struct inode *inode, int mask)
721 struct pid_namespace *pid = proc_pid_ns(inode);
722 struct task_struct *task;
725 task = get_proc_task(inode);
728 has_perms = has_pid_permissions(pid, task, HIDEPID_NO_ACCESS);
729 put_task_struct(task);
732 if (pid->hide_pid == HIDEPID_INVISIBLE) {
734 * Let's make getdents(), stat(), and open()
735 * consistent with each other. If a process
736 * may not stat() a file, it shouldn't be seen
744 return generic_permission(inode, mask);
749 static const struct inode_operations proc_def_inode_operations = {
750 .setattr = proc_setattr,
753 static int proc_single_show(struct seq_file *m, void *v)
755 struct inode *inode = m->private;
756 struct pid_namespace *ns = proc_pid_ns(inode);
757 struct pid *pid = proc_pid(inode);
758 struct task_struct *task;
761 task = get_pid_task(pid, PIDTYPE_PID);
765 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
767 put_task_struct(task);
771 static int proc_single_open(struct inode *inode, struct file *filp)
773 return single_open(filp, proc_single_show, inode);
776 static const struct file_operations proc_single_file_operations = {
777 .open = proc_single_open,
780 .release = single_release,
784 struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode)
786 struct task_struct *task = get_proc_task(inode);
787 struct mm_struct *mm = ERR_PTR(-ESRCH);
790 mm = mm_access(task, mode | PTRACE_MODE_FSCREDS);
791 put_task_struct(task);
793 if (!IS_ERR_OR_NULL(mm)) {
794 /* ensure this mm_struct can't be freed */
796 /* but do not pin its memory */
804 static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
806 struct mm_struct *mm = proc_mem_open(inode, mode);
811 file->private_data = mm;
815 static int mem_open(struct inode *inode, struct file *file)
817 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
819 /* OK to pass negative loff_t, we can catch out-of-range */
820 file->f_mode |= FMODE_UNSIGNED_OFFSET;
825 static ssize_t mem_rw(struct file *file, char __user *buf,
826 size_t count, loff_t *ppos, int write)
828 struct mm_struct *mm = file->private_data;
829 unsigned long addr = *ppos;
837 page = (char *)__get_free_page(GFP_KERNEL);
842 if (!mmget_not_zero(mm))
845 flags = FOLL_FORCE | (write ? FOLL_WRITE : 0);
848 size_t this_len = min_t(size_t, count, PAGE_SIZE);
850 if (write && copy_from_user(page, buf, this_len)) {
855 this_len = access_remote_vm(mm, addr, page, this_len, flags);
862 if (!write && copy_to_user(buf, page, this_len)) {
876 free_page((unsigned long) page);
880 static ssize_t mem_read(struct file *file, char __user *buf,
881 size_t count, loff_t *ppos)
883 return mem_rw(file, buf, count, ppos, 0);
886 static ssize_t mem_write(struct file *file, const char __user *buf,
887 size_t count, loff_t *ppos)
889 return mem_rw(file, (char __user*)buf, count, ppos, 1);
892 loff_t mem_lseek(struct file *file, loff_t offset, int orig)
896 file->f_pos = offset;
899 file->f_pos += offset;
904 force_successful_syscall_return();
908 static int mem_release(struct inode *inode, struct file *file)
910 struct mm_struct *mm = file->private_data;
916 static const struct file_operations proc_mem_operations = {
921 .release = mem_release,
924 static int environ_open(struct inode *inode, struct file *file)
926 return __mem_open(inode, file, PTRACE_MODE_READ);
929 static ssize_t environ_read(struct file *file, char __user *buf,
930 size_t count, loff_t *ppos)
933 unsigned long src = *ppos;
935 struct mm_struct *mm = file->private_data;
936 unsigned long env_start, env_end;
938 /* Ensure the process spawned far enough to have an environment. */
939 if (!mm || !mm->env_end)
942 page = (char *)__get_free_page(GFP_KERNEL);
947 if (!mmget_not_zero(mm))
950 spin_lock(&mm->arg_lock);
951 env_start = mm->env_start;
952 env_end = mm->env_end;
953 spin_unlock(&mm->arg_lock);
956 size_t this_len, max_len;
959 if (src >= (env_end - env_start))
962 this_len = env_end - (env_start + src);
964 max_len = min_t(size_t, PAGE_SIZE, count);
965 this_len = min(max_len, this_len);
967 retval = access_remote_vm(mm, (env_start + src), page, this_len, FOLL_ANON);
974 if (copy_to_user(buf, page, retval)) {
988 free_page((unsigned long) page);
992 static const struct file_operations proc_environ_operations = {
993 .open = environ_open,
994 .read = environ_read,
995 .llseek = generic_file_llseek,
996 .release = mem_release,
999 static int auxv_open(struct inode *inode, struct file *file)
1001 return __mem_open(inode, file, PTRACE_MODE_READ_FSCREDS);
1004 static ssize_t auxv_read(struct file *file, char __user *buf,
1005 size_t count, loff_t *ppos)
1007 struct mm_struct *mm = file->private_data;
1008 unsigned int nwords = 0;
1014 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
1015 return simple_read_from_buffer(buf, count, ppos, mm->saved_auxv,
1016 nwords * sizeof(mm->saved_auxv[0]));
1019 static const struct file_operations proc_auxv_operations = {
1022 .llseek = generic_file_llseek,
1023 .release = mem_release,
1026 static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
1029 struct task_struct *task = get_proc_task(file_inode(file));
1030 char buffer[PROC_NUMBUF];
1031 int oom_adj = OOM_ADJUST_MIN;
1036 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
1037 oom_adj = OOM_ADJUST_MAX;
1039 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
1041 put_task_struct(task);
1042 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
1043 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1046 static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
1048 struct mm_struct *mm = NULL;
1049 struct task_struct *task;
1052 task = get_proc_task(file_inode(file));
1056 mutex_lock(&oom_adj_mutex);
1058 if (oom_adj < task->signal->oom_score_adj &&
1059 !capable(CAP_SYS_RESOURCE)) {
1064 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
1065 * /proc/pid/oom_score_adj instead.
1067 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
1068 current->comm, task_pid_nr(current), task_pid_nr(task),
1071 if ((short)oom_adj < task->signal->oom_score_adj_min &&
1072 !capable(CAP_SYS_RESOURCE)) {
1079 * Make sure we will check other processes sharing the mm if this is
1080 * not vfrok which wants its own oom_score_adj.
1081 * pin the mm so it doesn't go away and get reused after task_unlock
1083 if (!task->vfork_done) {
1084 struct task_struct *p = find_lock_task_mm(task);
1087 if (test_bit(MMF_MULTIPROCESS, &p->mm->flags)) {
1095 task->signal->oom_score_adj = oom_adj;
1096 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1097 task->signal->oom_score_adj_min = (short)oom_adj;
1098 trace_oom_score_adj_update(task);
1101 struct task_struct *p;
1104 for_each_process(p) {
1105 if (same_thread_group(task, p))
1108 /* do not touch kernel threads or the global init */
1109 if (p->flags & PF_KTHREAD || is_global_init(p))
1113 if (!p->vfork_done && process_shares_mm(p, mm)) {
1114 p->signal->oom_score_adj = oom_adj;
1115 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1116 p->signal->oom_score_adj_min = (short)oom_adj;
1124 mutex_unlock(&oom_adj_mutex);
1125 put_task_struct(task);
1130 * /proc/pid/oom_adj exists solely for backwards compatibility with previous
1131 * kernels. The effective policy is defined by oom_score_adj, which has a
1132 * different scale: oom_adj grew exponentially and oom_score_adj grows linearly.
1133 * Values written to oom_adj are simply mapped linearly to oom_score_adj.
1134 * Processes that become oom disabled via oom_adj will still be oom disabled
1135 * with this implementation.
1137 * oom_adj cannot be removed since existing userspace binaries use it.
1139 static ssize_t oom_adj_write(struct file *file, const char __user *buf,
1140 size_t count, loff_t *ppos)
1142 char buffer[PROC_NUMBUF];
1146 memset(buffer, 0, sizeof(buffer));
1147 if (count > sizeof(buffer) - 1)
1148 count = sizeof(buffer) - 1;
1149 if (copy_from_user(buffer, buf, count)) {
1154 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
1157 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
1158 oom_adj != OOM_DISABLE) {
1164 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1165 * value is always attainable.
1167 if (oom_adj == OOM_ADJUST_MAX)
1168 oom_adj = OOM_SCORE_ADJ_MAX;
1170 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
1172 err = __set_oom_adj(file, oom_adj, true);
1174 return err < 0 ? err : count;
1177 static const struct file_operations proc_oom_adj_operations = {
1178 .read = oom_adj_read,
1179 .write = oom_adj_write,
1180 .llseek = generic_file_llseek,
1183 static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1184 size_t count, loff_t *ppos)
1186 struct task_struct *task = get_proc_task(file_inode(file));
1187 char buffer[PROC_NUMBUF];
1188 short oom_score_adj = OOM_SCORE_ADJ_MIN;
1193 oom_score_adj = task->signal->oom_score_adj;
1194 put_task_struct(task);
1195 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
1196 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1199 static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1200 size_t count, loff_t *ppos)
1202 char buffer[PROC_NUMBUF];
1206 memset(buffer, 0, sizeof(buffer));
1207 if (count > sizeof(buffer) - 1)
1208 count = sizeof(buffer) - 1;
1209 if (copy_from_user(buffer, buf, count)) {
1214 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
1217 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
1218 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1223 err = __set_oom_adj(file, oom_score_adj, false);
1225 return err < 0 ? err : count;
1228 static const struct file_operations proc_oom_score_adj_operations = {
1229 .read = oom_score_adj_read,
1230 .write = oom_score_adj_write,
1231 .llseek = default_llseek,
1235 #define TMPBUFLEN 11
1236 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1237 size_t count, loff_t *ppos)
1239 struct inode * inode = file_inode(file);
1240 struct task_struct *task = get_proc_task(inode);
1242 char tmpbuf[TMPBUFLEN];
1246 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1247 from_kuid(file->f_cred->user_ns,
1248 audit_get_loginuid(task)));
1249 put_task_struct(task);
1250 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1253 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1254 size_t count, loff_t *ppos)
1256 struct inode * inode = file_inode(file);
1262 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1269 /* No partial writes. */
1273 rv = kstrtou32_from_user(buf, count, 10, &loginuid);
1277 /* is userspace tring to explicitly UNSET the loginuid? */
1278 if (loginuid == AUDIT_UID_UNSET) {
1279 kloginuid = INVALID_UID;
1281 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
1282 if (!uid_valid(kloginuid))
1286 rv = audit_set_loginuid(kloginuid);
1292 static const struct file_operations proc_loginuid_operations = {
1293 .read = proc_loginuid_read,
1294 .write = proc_loginuid_write,
1295 .llseek = generic_file_llseek,
1298 static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1299 size_t count, loff_t *ppos)
1301 struct inode * inode = file_inode(file);
1302 struct task_struct *task = get_proc_task(inode);
1304 char tmpbuf[TMPBUFLEN];
1308 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1309 audit_get_sessionid(task));
1310 put_task_struct(task);
1311 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1314 static const struct file_operations proc_sessionid_operations = {
1315 .read = proc_sessionid_read,
1316 .llseek = generic_file_llseek,
1320 #ifdef CONFIG_FAULT_INJECTION
1321 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1322 size_t count, loff_t *ppos)
1324 struct task_struct *task = get_proc_task(file_inode(file));
1325 char buffer[PROC_NUMBUF];
1331 make_it_fail = task->make_it_fail;
1332 put_task_struct(task);
1334 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1336 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1339 static ssize_t proc_fault_inject_write(struct file * file,
1340 const char __user * buf, size_t count, loff_t *ppos)
1342 struct task_struct *task;
1343 char buffer[PROC_NUMBUF];
1347 if (!capable(CAP_SYS_RESOURCE))
1349 memset(buffer, 0, sizeof(buffer));
1350 if (count > sizeof(buffer) - 1)
1351 count = sizeof(buffer) - 1;
1352 if (copy_from_user(buffer, buf, count))
1354 rv = kstrtoint(strstrip(buffer), 0, &make_it_fail);
1357 if (make_it_fail < 0 || make_it_fail > 1)
1360 task = get_proc_task(file_inode(file));
1363 task->make_it_fail = make_it_fail;
1364 put_task_struct(task);
1369 static const struct file_operations proc_fault_inject_operations = {
1370 .read = proc_fault_inject_read,
1371 .write = proc_fault_inject_write,
1372 .llseek = generic_file_llseek,
1375 static ssize_t proc_fail_nth_write(struct file *file, const char __user *buf,
1376 size_t count, loff_t *ppos)
1378 struct task_struct *task;
1382 err = kstrtouint_from_user(buf, count, 0, &n);
1386 task = get_proc_task(file_inode(file));
1390 put_task_struct(task);
1395 static ssize_t proc_fail_nth_read(struct file *file, char __user *buf,
1396 size_t count, loff_t *ppos)
1398 struct task_struct *task;
1399 char numbuf[PROC_NUMBUF];
1402 task = get_proc_task(file_inode(file));
1405 len = snprintf(numbuf, sizeof(numbuf), "%u\n", task->fail_nth);
1406 put_task_struct(task);
1407 return simple_read_from_buffer(buf, count, ppos, numbuf, len);
1410 static const struct file_operations proc_fail_nth_operations = {
1411 .read = proc_fail_nth_read,
1412 .write = proc_fail_nth_write,
1417 #ifdef CONFIG_SCHED_DEBUG
1419 * Print out various scheduling related per-task fields:
1421 static int sched_show(struct seq_file *m, void *v)
1423 struct inode *inode = m->private;
1424 struct pid_namespace *ns = proc_pid_ns(inode);
1425 struct task_struct *p;
1427 p = get_proc_task(inode);
1430 proc_sched_show_task(p, ns, m);
1438 sched_write(struct file *file, const char __user *buf,
1439 size_t count, loff_t *offset)
1441 struct inode *inode = file_inode(file);
1442 struct task_struct *p;
1444 p = get_proc_task(inode);
1447 proc_sched_set_task(p);
1454 static int sched_open(struct inode *inode, struct file *filp)
1456 return single_open(filp, sched_show, inode);
1459 static const struct file_operations proc_pid_sched_operations = {
1462 .write = sched_write,
1463 .llseek = seq_lseek,
1464 .release = single_release,
1469 #ifdef CONFIG_SCHED_AUTOGROUP
1471 * Print out autogroup related information:
1473 static int sched_autogroup_show(struct seq_file *m, void *v)
1475 struct inode *inode = m->private;
1476 struct task_struct *p;
1478 p = get_proc_task(inode);
1481 proc_sched_autogroup_show_task(p, m);
1489 sched_autogroup_write(struct file *file, const char __user *buf,
1490 size_t count, loff_t *offset)
1492 struct inode *inode = file_inode(file);
1493 struct task_struct *p;
1494 char buffer[PROC_NUMBUF];
1498 memset(buffer, 0, sizeof(buffer));
1499 if (count > sizeof(buffer) - 1)
1500 count = sizeof(buffer) - 1;
1501 if (copy_from_user(buffer, buf, count))
1504 err = kstrtoint(strstrip(buffer), 0, &nice);
1508 p = get_proc_task(inode);
1512 err = proc_sched_autogroup_set_nice(p, nice);
1521 static int sched_autogroup_open(struct inode *inode, struct file *filp)
1525 ret = single_open(filp, sched_autogroup_show, NULL);
1527 struct seq_file *m = filp->private_data;
1534 static const struct file_operations proc_pid_sched_autogroup_operations = {
1535 .open = sched_autogroup_open,
1537 .write = sched_autogroup_write,
1538 .llseek = seq_lseek,
1539 .release = single_release,
1542 #endif /* CONFIG_SCHED_AUTOGROUP */
1544 static ssize_t comm_write(struct file *file, const char __user *buf,
1545 size_t count, loff_t *offset)
1547 struct inode *inode = file_inode(file);
1548 struct task_struct *p;
1549 char buffer[TASK_COMM_LEN];
1550 const size_t maxlen = sizeof(buffer) - 1;
1552 memset(buffer, 0, sizeof(buffer));
1553 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
1556 p = get_proc_task(inode);
1560 if (same_thread_group(current, p))
1561 set_task_comm(p, buffer);
1570 static int comm_show(struct seq_file *m, void *v)
1572 struct inode *inode = m->private;
1573 struct task_struct *p;
1575 p = get_proc_task(inode);
1579 proc_task_name(m, p, false);
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, umode_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 pid_namespace *pid = proc_pid_ns(inode);
1796 struct task_struct *task;
1798 generic_fillattr(inode, stat);
1800 stat->uid = GLOBAL_ROOT_UID;
1801 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 * Set <pid>/... inode ownership (can change due to setuid(), etc.)
1824 void pid_update_inode(struct task_struct *task, struct inode *inode)
1826 task_dump_owner(task, inode->i_mode, &inode->i_uid, &inode->i_gid);
1828 inode->i_mode &= ~(S_ISUID | S_ISGID);
1829 security_task_to_inode(task, inode);
1833 * Rewrite the inode's ownerships here because the owning task may have
1834 * performed a setuid(), etc.
1837 static int pid_revalidate(struct dentry *dentry, unsigned int flags)
1839 struct inode *inode;
1840 struct task_struct *task;
1842 if (flags & LOOKUP_RCU)
1845 inode = d_inode(dentry);
1846 task = get_proc_task(inode);
1849 pid_update_inode(task, inode);
1850 put_task_struct(task);
1856 static inline bool proc_inode_is_dead(struct inode *inode)
1858 return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1861 int pid_delete_dentry(const struct dentry *dentry)
1863 /* Is the task we represent dead?
1864 * If so, then don't put the dentry on the lru list,
1865 * kill it immediately.
1867 return proc_inode_is_dead(d_inode(dentry));
1870 const struct dentry_operations pid_dentry_operations =
1872 .d_revalidate = pid_revalidate,
1873 .d_delete = pid_delete_dentry,
1879 * Fill a directory entry.
1881 * If possible create the dcache entry and derive our inode number and
1882 * file type from dcache entry.
1884 * Since all of the proc inode numbers are dynamically generated, the inode
1885 * numbers do not exist until the inode is cache. This means creating the
1886 * the dcache entry in readdir is necessary to keep the inode numbers
1887 * reported by readdir in sync with the inode numbers reported
1890 bool proc_fill_cache(struct file *file, struct dir_context *ctx,
1891 const char *name, unsigned int len,
1892 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1894 struct dentry *child, *dir = file->f_path.dentry;
1895 struct qstr qname = QSTR_INIT(name, len);
1896 struct inode *inode;
1897 unsigned type = DT_UNKNOWN;
1900 child = d_hash_and_lookup(dir, &qname);
1902 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1903 child = d_alloc_parallel(dir, &qname, &wq);
1905 goto end_instantiate;
1906 if (d_in_lookup(child)) {
1908 res = instantiate(child, task, ptr);
1909 d_lookup_done(child);
1910 if (unlikely(res)) {
1914 goto end_instantiate;
1918 inode = d_inode(child);
1920 type = inode->i_mode >> 12;
1923 return dir_emit(ctx, name, len, ino, type);
1927 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1928 * which represent vma start and end addresses.
1930 static int dname_to_vma_addr(struct dentry *dentry,
1931 unsigned long *start, unsigned long *end)
1933 const char *str = dentry->d_name.name;
1934 unsigned long long sval, eval;
1937 if (str[0] == '0' && str[1] != '-')
1939 len = _parse_integer(str, 16, &sval);
1940 if (len & KSTRTOX_OVERFLOW)
1942 if (sval != (unsigned long)sval)
1950 if (str[0] == '0' && str[1])
1952 len = _parse_integer(str, 16, &eval);
1953 if (len & KSTRTOX_OVERFLOW)
1955 if (eval != (unsigned long)eval)
1968 static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
1970 unsigned long vm_start, vm_end;
1971 bool exact_vma_exists = false;
1972 struct mm_struct *mm = NULL;
1973 struct task_struct *task;
1974 struct inode *inode;
1977 if (flags & LOOKUP_RCU)
1980 inode = d_inode(dentry);
1981 task = get_proc_task(inode);
1985 mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
1986 if (IS_ERR_OR_NULL(mm))
1989 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1990 status = down_read_killable(&mm->mmap_sem);
1992 exact_vma_exists = !!find_exact_vma(mm, vm_start,
1994 up_read(&mm->mmap_sem);
2000 if (exact_vma_exists) {
2001 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
2003 security_task_to_inode(task, inode);
2008 put_task_struct(task);
2014 static const struct dentry_operations tid_map_files_dentry_operations = {
2015 .d_revalidate = map_files_d_revalidate,
2016 .d_delete = pid_delete_dentry,
2019 static int map_files_get_link(struct dentry *dentry, struct path *path)
2021 unsigned long vm_start, vm_end;
2022 struct vm_area_struct *vma;
2023 struct task_struct *task;
2024 struct mm_struct *mm;
2028 task = get_proc_task(d_inode(dentry));
2032 mm = get_task_mm(task);
2033 put_task_struct(task);
2037 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
2041 rc = down_read_killable(&mm->mmap_sem);
2046 vma = find_exact_vma(mm, vm_start, vm_end);
2047 if (vma && vma->vm_file) {
2048 *path = vma->vm_file->f_path;
2052 up_read(&mm->mmap_sem);
2060 struct map_files_info {
2061 unsigned long start;
2067 * Only allow CAP_SYS_ADMIN to follow the links, due to concerns about how the
2068 * symlinks may be used to bypass permissions on ancestor directories in the
2069 * path to the file in question.
2072 proc_map_files_get_link(struct dentry *dentry,
2073 struct inode *inode,
2074 struct delayed_call *done)
2076 if (!capable(CAP_SYS_ADMIN))
2077 return ERR_PTR(-EPERM);
2079 return proc_pid_get_link(dentry, inode, done);
2083 * Identical to proc_pid_link_inode_operations except for get_link()
2085 static const struct inode_operations proc_map_files_link_inode_operations = {
2086 .readlink = proc_pid_readlink,
2087 .get_link = proc_map_files_get_link,
2088 .setattr = proc_setattr,
2091 static struct dentry *
2092 proc_map_files_instantiate(struct dentry *dentry,
2093 struct task_struct *task, const void *ptr)
2095 fmode_t mode = (fmode_t)(unsigned long)ptr;
2096 struct proc_inode *ei;
2097 struct inode *inode;
2099 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFLNK |
2100 ((mode & FMODE_READ ) ? S_IRUSR : 0) |
2101 ((mode & FMODE_WRITE) ? S_IWUSR : 0));
2103 return ERR_PTR(-ENOENT);
2106 ei->op.proc_get_link = map_files_get_link;
2108 inode->i_op = &proc_map_files_link_inode_operations;
2111 d_set_d_op(dentry, &tid_map_files_dentry_operations);
2112 return d_splice_alias(inode, dentry);
2115 static struct dentry *proc_map_files_lookup(struct inode *dir,
2116 struct dentry *dentry, unsigned int flags)
2118 unsigned long vm_start, vm_end;
2119 struct vm_area_struct *vma;
2120 struct task_struct *task;
2121 struct dentry *result;
2122 struct mm_struct *mm;
2124 result = ERR_PTR(-ENOENT);
2125 task = get_proc_task(dir);
2129 result = ERR_PTR(-EACCES);
2130 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
2133 result = ERR_PTR(-ENOENT);
2134 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
2137 mm = get_task_mm(task);
2141 result = ERR_PTR(-EINTR);
2142 if (down_read_killable(&mm->mmap_sem))
2145 result = ERR_PTR(-ENOENT);
2146 vma = find_exact_vma(mm, vm_start, vm_end);
2151 result = proc_map_files_instantiate(dentry, task,
2152 (void *)(unsigned long)vma->vm_file->f_mode);
2155 up_read(&mm->mmap_sem);
2159 put_task_struct(task);
2164 static const struct inode_operations proc_map_files_inode_operations = {
2165 .lookup = proc_map_files_lookup,
2166 .permission = proc_fd_permission,
2167 .setattr = proc_setattr,
2171 proc_map_files_readdir(struct file *file, struct dir_context *ctx)
2173 struct vm_area_struct *vma;
2174 struct task_struct *task;
2175 struct mm_struct *mm;
2176 unsigned long nr_files, pos, i;
2177 GENRADIX(struct map_files_info) fa;
2178 struct map_files_info *p;
2184 task = get_proc_task(file_inode(file));
2189 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
2193 if (!dir_emit_dots(file, ctx))
2196 mm = get_task_mm(task);
2200 ret = down_read_killable(&mm->mmap_sem);
2209 * We need two passes here:
2211 * 1) Collect vmas of mapped files with mmap_sem taken
2212 * 2) Release mmap_sem and instantiate entries
2214 * otherwise we get lockdep complained, since filldir()
2215 * routine might require mmap_sem taken in might_fault().
2218 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
2221 if (++pos <= ctx->pos)
2224 p = genradix_ptr_alloc(&fa, nr_files++, GFP_KERNEL);
2227 up_read(&mm->mmap_sem);
2232 p->start = vma->vm_start;
2233 p->end = vma->vm_end;
2234 p->mode = vma->vm_file->f_mode;
2236 up_read(&mm->mmap_sem);
2239 for (i = 0; i < nr_files; i++) {
2240 char buf[4 * sizeof(long) + 2]; /* max: %lx-%lx\0 */
2243 p = genradix_ptr(&fa, i);
2244 len = snprintf(buf, sizeof(buf), "%lx-%lx", p->start, p->end);
2245 if (!proc_fill_cache(file, ctx,
2247 proc_map_files_instantiate,
2249 (void *)(unsigned long)p->mode))
2255 put_task_struct(task);
2261 static const struct file_operations proc_map_files_operations = {
2262 .read = generic_read_dir,
2263 .iterate_shared = proc_map_files_readdir,
2264 .llseek = generic_file_llseek,
2267 #if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
2268 struct timers_private {
2270 struct task_struct *task;
2271 struct sighand_struct *sighand;
2272 struct pid_namespace *ns;
2273 unsigned long flags;
2276 static void *timers_start(struct seq_file *m, loff_t *pos)
2278 struct timers_private *tp = m->private;
2280 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2282 return ERR_PTR(-ESRCH);
2284 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2286 return ERR_PTR(-ESRCH);
2288 return seq_list_start(&tp->task->signal->posix_timers, *pos);
2291 static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2293 struct timers_private *tp = m->private;
2294 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2297 static void timers_stop(struct seq_file *m, void *v)
2299 struct timers_private *tp = m->private;
2302 unlock_task_sighand(tp->task, &tp->flags);
2307 put_task_struct(tp->task);
2312 static int show_timer(struct seq_file *m, void *v)
2314 struct k_itimer *timer;
2315 struct timers_private *tp = m->private;
2317 static const char * const nstr[] = {
2318 [SIGEV_SIGNAL] = "signal",
2319 [SIGEV_NONE] = "none",
2320 [SIGEV_THREAD] = "thread",
2323 timer = list_entry((struct list_head *)v, struct k_itimer, list);
2324 notify = timer->it_sigev_notify;
2326 seq_printf(m, "ID: %d\n", timer->it_id);
2327 seq_printf(m, "signal: %d/%px\n",
2328 timer->sigq->info.si_signo,
2329 timer->sigq->info.si_value.sival_ptr);
2330 seq_printf(m, "notify: %s/%s.%d\n",
2331 nstr[notify & ~SIGEV_THREAD_ID],
2332 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2333 pid_nr_ns(timer->it_pid, tp->ns));
2334 seq_printf(m, "ClockID: %d\n", timer->it_clock);
2339 static const struct seq_operations proc_timers_seq_ops = {
2340 .start = timers_start,
2341 .next = timers_next,
2342 .stop = timers_stop,
2346 static int proc_timers_open(struct inode *inode, struct file *file)
2348 struct timers_private *tp;
2350 tp = __seq_open_private(file, &proc_timers_seq_ops,
2351 sizeof(struct timers_private));
2355 tp->pid = proc_pid(inode);
2356 tp->ns = proc_pid_ns(inode);
2360 static const struct file_operations proc_timers_operations = {
2361 .open = proc_timers_open,
2363 .llseek = seq_lseek,
2364 .release = seq_release_private,
2368 static ssize_t timerslack_ns_write(struct file *file, const char __user *buf,
2369 size_t count, loff_t *offset)
2371 struct inode *inode = file_inode(file);
2372 struct task_struct *p;
2376 err = kstrtoull_from_user(buf, count, 10, &slack_ns);
2380 p = get_proc_task(inode);
2386 if (!ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE)) {
2393 err = security_task_setscheduler(p);
2402 p->timer_slack_ns = p->default_timer_slack_ns;
2404 p->timer_slack_ns = slack_ns;
2413 static int timerslack_ns_show(struct seq_file *m, void *v)
2415 struct inode *inode = m->private;
2416 struct task_struct *p;
2419 p = get_proc_task(inode);
2425 if (!ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE)) {
2432 err = security_task_getscheduler(p);
2438 seq_printf(m, "%llu\n", p->timer_slack_ns);
2447 static int timerslack_ns_open(struct inode *inode, struct file *filp)
2449 return single_open(filp, timerslack_ns_show, inode);
2452 static const struct file_operations proc_pid_set_timerslack_ns_operations = {
2453 .open = timerslack_ns_open,
2455 .write = timerslack_ns_write,
2456 .llseek = seq_lseek,
2457 .release = single_release,
2460 static struct dentry *proc_pident_instantiate(struct dentry *dentry,
2461 struct task_struct *task, const void *ptr)
2463 const struct pid_entry *p = ptr;
2464 struct inode *inode;
2465 struct proc_inode *ei;
2467 inode = proc_pid_make_inode(dentry->d_sb, task, p->mode);
2469 return ERR_PTR(-ENOENT);
2472 if (S_ISDIR(inode->i_mode))
2473 set_nlink(inode, 2); /* Use getattr to fix if necessary */
2475 inode->i_op = p->iop;
2477 inode->i_fop = p->fop;
2479 pid_update_inode(task, inode);
2480 d_set_d_op(dentry, &pid_dentry_operations);
2481 return d_splice_alias(inode, dentry);
2484 static struct dentry *proc_pident_lookup(struct inode *dir,
2485 struct dentry *dentry,
2486 const struct pid_entry *p,
2487 const struct pid_entry *end)
2489 struct task_struct *task = get_proc_task(dir);
2490 struct dentry *res = ERR_PTR(-ENOENT);
2496 * Yes, it does not scale. And it should not. Don't add
2497 * new entries into /proc/<tgid>/ without very good reasons.
2499 for (; p < end; p++) {
2500 if (p->len != dentry->d_name.len)
2502 if (!memcmp(dentry->d_name.name, p->name, p->len)) {
2503 res = proc_pident_instantiate(dentry, task, p);
2507 put_task_struct(task);
2512 static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
2513 const struct pid_entry *ents, unsigned int nents)
2515 struct task_struct *task = get_proc_task(file_inode(file));
2516 const struct pid_entry *p;
2521 if (!dir_emit_dots(file, ctx))
2524 if (ctx->pos >= nents + 2)
2527 for (p = ents + (ctx->pos - 2); p < ents + nents; p++) {
2528 if (!proc_fill_cache(file, ctx, p->name, p->len,
2529 proc_pident_instantiate, task, p))
2534 put_task_struct(task);
2538 #ifdef CONFIG_SECURITY
2539 static int proc_pid_attr_open(struct inode *inode, struct file *file)
2541 file->private_data = NULL;
2542 __mem_open(inode, file, PTRACE_MODE_READ_FSCREDS);
2546 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2547 size_t count, loff_t *ppos)
2549 struct inode * inode = file_inode(file);
2552 struct task_struct *task = get_proc_task(inode);
2557 length = security_getprocattr(task, PROC_I(inode)->op.lsm,
2558 (char*)file->f_path.dentry->d_name.name,
2560 put_task_struct(task);
2562 length = simple_read_from_buffer(buf, count, ppos, p, length);
2567 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2568 size_t count, loff_t *ppos)
2570 struct inode * inode = file_inode(file);
2571 struct task_struct *task;
2575 /* A task may only write when it was the opener. */
2576 if (file->private_data != current->mm)
2580 task = pid_task(proc_pid(inode), PIDTYPE_PID);
2585 /* A task may only write its own attributes. */
2586 if (current != task) {
2590 /* Prevent changes to overridden credentials. */
2591 if (current_cred() != current_real_cred()) {
2597 if (count > PAGE_SIZE)
2600 /* No partial writes. */
2604 page = memdup_user(buf, count);
2610 /* Guard against adverse ptrace interaction */
2611 rv = mutex_lock_interruptible(¤t->signal->cred_guard_mutex);
2615 rv = security_setprocattr(PROC_I(inode)->op.lsm,
2616 file->f_path.dentry->d_name.name, page,
2618 mutex_unlock(¤t->signal->cred_guard_mutex);
2625 static const struct file_operations proc_pid_attr_operations = {
2626 .open = proc_pid_attr_open,
2627 .read = proc_pid_attr_read,
2628 .write = proc_pid_attr_write,
2629 .llseek = generic_file_llseek,
2630 .release = mem_release,
2633 #define LSM_DIR_OPS(LSM) \
2634 static int proc_##LSM##_attr_dir_iterate(struct file *filp, \
2635 struct dir_context *ctx) \
2637 return proc_pident_readdir(filp, ctx, \
2638 LSM##_attr_dir_stuff, \
2639 ARRAY_SIZE(LSM##_attr_dir_stuff)); \
2642 static const struct file_operations proc_##LSM##_attr_dir_ops = { \
2643 .read = generic_read_dir, \
2644 .iterate = proc_##LSM##_attr_dir_iterate, \
2645 .llseek = default_llseek, \
2648 static struct dentry *proc_##LSM##_attr_dir_lookup(struct inode *dir, \
2649 struct dentry *dentry, unsigned int flags) \
2651 return proc_pident_lookup(dir, dentry, \
2652 LSM##_attr_dir_stuff, \
2653 LSM##_attr_dir_stuff + ARRAY_SIZE(LSM##_attr_dir_stuff)); \
2656 static const struct inode_operations proc_##LSM##_attr_dir_inode_ops = { \
2657 .lookup = proc_##LSM##_attr_dir_lookup, \
2658 .getattr = pid_getattr, \
2659 .setattr = proc_setattr, \
2662 #ifdef CONFIG_SECURITY_SMACK
2663 static const struct pid_entry smack_attr_dir_stuff[] = {
2664 ATTR("smack", "current", 0666),
2669 static const struct pid_entry attr_dir_stuff[] = {
2670 ATTR(NULL, "current", 0666),
2671 ATTR(NULL, "prev", 0444),
2672 ATTR(NULL, "exec", 0666),
2673 ATTR(NULL, "fscreate", 0666),
2674 ATTR(NULL, "keycreate", 0666),
2675 ATTR(NULL, "sockcreate", 0666),
2676 #ifdef CONFIG_SECURITY_SMACK
2678 proc_smack_attr_dir_inode_ops, proc_smack_attr_dir_ops),
2682 static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
2684 return proc_pident_readdir(file, ctx,
2685 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2688 static const struct file_operations proc_attr_dir_operations = {
2689 .read = generic_read_dir,
2690 .iterate_shared = proc_attr_dir_readdir,
2691 .llseek = generic_file_llseek,
2694 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2695 struct dentry *dentry, unsigned int flags)
2697 return proc_pident_lookup(dir, dentry,
2699 attr_dir_stuff + ARRAY_SIZE(attr_dir_stuff));
2702 static const struct inode_operations proc_attr_dir_inode_operations = {
2703 .lookup = proc_attr_dir_lookup,
2704 .getattr = pid_getattr,
2705 .setattr = proc_setattr,
2710 #ifdef CONFIG_ELF_CORE
2711 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2712 size_t count, loff_t *ppos)
2714 struct task_struct *task = get_proc_task(file_inode(file));
2715 struct mm_struct *mm;
2716 char buffer[PROC_NUMBUF];
2724 mm = get_task_mm(task);
2726 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2727 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2728 MMF_DUMP_FILTER_SHIFT));
2730 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2733 put_task_struct(task);
2738 static ssize_t proc_coredump_filter_write(struct file *file,
2739 const char __user *buf,
2743 struct task_struct *task;
2744 struct mm_struct *mm;
2750 ret = kstrtouint_from_user(buf, count, 0, &val);
2755 task = get_proc_task(file_inode(file));
2759 mm = get_task_mm(task);
2764 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2766 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2768 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2773 put_task_struct(task);
2780 static const struct file_operations proc_coredump_filter_operations = {
2781 .read = proc_coredump_filter_read,
2782 .write = proc_coredump_filter_write,
2783 .llseek = generic_file_llseek,
2787 #ifdef CONFIG_TASK_IO_ACCOUNTING
2788 static int do_io_accounting(struct task_struct *task, struct seq_file *m, int whole)
2790 struct task_io_accounting acct = task->ioac;
2791 unsigned long flags;
2794 result = down_read_killable(&task->signal->exec_update_lock);
2798 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
2803 if (whole && lock_task_sighand(task, &flags)) {
2804 struct task_struct *t = task;
2806 task_io_accounting_add(&acct, &task->signal->ioac);
2807 while_each_thread(task, t)
2808 task_io_accounting_add(&acct, &t->ioac);
2810 unlock_task_sighand(task, &flags);
2817 "read_bytes: %llu\n"
2818 "write_bytes: %llu\n"
2819 "cancelled_write_bytes: %llu\n",
2820 (unsigned long long)acct.rchar,
2821 (unsigned long long)acct.wchar,
2822 (unsigned long long)acct.syscr,
2823 (unsigned long long)acct.syscw,
2824 (unsigned long long)acct.read_bytes,
2825 (unsigned long long)acct.write_bytes,
2826 (unsigned long long)acct.cancelled_write_bytes);
2830 up_read(&task->signal->exec_update_lock);
2834 static int proc_tid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2835 struct pid *pid, struct task_struct *task)
2837 return do_io_accounting(task, m, 0);
2840 static int proc_tgid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2841 struct pid *pid, struct task_struct *task)
2843 return do_io_accounting(task, m, 1);
2845 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2847 #ifdef CONFIG_USER_NS
2848 static int proc_id_map_open(struct inode *inode, struct file *file,
2849 const struct seq_operations *seq_ops)
2851 struct user_namespace *ns = NULL;
2852 struct task_struct *task;
2853 struct seq_file *seq;
2856 task = get_proc_task(inode);
2859 ns = get_user_ns(task_cred_xxx(task, user_ns));
2861 put_task_struct(task);
2866 ret = seq_open(file, seq_ops);
2870 seq = file->private_data;
2880 static int proc_id_map_release(struct inode *inode, struct file *file)
2882 struct seq_file *seq = file->private_data;
2883 struct user_namespace *ns = seq->private;
2885 return seq_release(inode, file);
2888 static int proc_uid_map_open(struct inode *inode, struct file *file)
2890 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2893 static int proc_gid_map_open(struct inode *inode, struct file *file)
2895 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2898 static int proc_projid_map_open(struct inode *inode, struct file *file)
2900 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2903 static const struct file_operations proc_uid_map_operations = {
2904 .open = proc_uid_map_open,
2905 .write = proc_uid_map_write,
2907 .llseek = seq_lseek,
2908 .release = proc_id_map_release,
2911 static const struct file_operations proc_gid_map_operations = {
2912 .open = proc_gid_map_open,
2913 .write = proc_gid_map_write,
2915 .llseek = seq_lseek,
2916 .release = proc_id_map_release,
2919 static const struct file_operations proc_projid_map_operations = {
2920 .open = proc_projid_map_open,
2921 .write = proc_projid_map_write,
2923 .llseek = seq_lseek,
2924 .release = proc_id_map_release,
2927 static int proc_setgroups_open(struct inode *inode, struct file *file)
2929 struct user_namespace *ns = NULL;
2930 struct task_struct *task;
2934 task = get_proc_task(inode);
2937 ns = get_user_ns(task_cred_xxx(task, user_ns));
2939 put_task_struct(task);
2944 if (file->f_mode & FMODE_WRITE) {
2946 if (!ns_capable(ns, CAP_SYS_ADMIN))
2950 ret = single_open(file, &proc_setgroups_show, ns);
2961 static int proc_setgroups_release(struct inode *inode, struct file *file)
2963 struct seq_file *seq = file->private_data;
2964 struct user_namespace *ns = seq->private;
2965 int ret = single_release(inode, file);
2970 static const struct file_operations proc_setgroups_operations = {
2971 .open = proc_setgroups_open,
2972 .write = proc_setgroups_write,
2974 .llseek = seq_lseek,
2975 .release = proc_setgroups_release,
2977 #endif /* CONFIG_USER_NS */
2979 static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2980 struct pid *pid, struct task_struct *task)
2982 int err = lock_trace(task);
2984 seq_printf(m, "%08x\n", task->personality);
2990 #ifdef CONFIG_LIVEPATCH
2991 static int proc_pid_patch_state(struct seq_file *m, struct pid_namespace *ns,
2992 struct pid *pid, struct task_struct *task)
2994 seq_printf(m, "%d\n", task->patch_state);
2997 #endif /* CONFIG_LIVEPATCH */
2999 #ifdef CONFIG_STACKLEAK_METRICS
3000 static int proc_stack_depth(struct seq_file *m, struct pid_namespace *ns,
3001 struct pid *pid, struct task_struct *task)
3003 unsigned long prev_depth = THREAD_SIZE -
3004 (task->prev_lowest_stack & (THREAD_SIZE - 1));
3005 unsigned long depth = THREAD_SIZE -
3006 (task->lowest_stack & (THREAD_SIZE - 1));
3008 seq_printf(m, "previous stack depth: %lu\nstack depth: %lu\n",
3012 #endif /* CONFIG_STACKLEAK_METRICS */
3017 static const struct file_operations proc_task_operations;
3018 static const struct inode_operations proc_task_inode_operations;
3020 static const struct pid_entry tgid_base_stuff[] = {
3021 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
3022 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
3023 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
3024 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
3025 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
3027 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
3029 REG("environ", S_IRUSR, proc_environ_operations),
3030 REG("auxv", S_IRUSR, proc_auxv_operations),
3031 ONE("status", S_IRUGO, proc_pid_status),
3032 ONE("personality", S_IRUSR, proc_pid_personality),
3033 ONE("limits", S_IRUGO, proc_pid_limits),
3034 #ifdef CONFIG_SCHED_DEBUG
3035 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
3037 #ifdef CONFIG_SCHED_AUTOGROUP
3038 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
3040 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
3041 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3042 ONE("syscall", S_IRUSR, proc_pid_syscall),
3044 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
3045 ONE("stat", S_IRUGO, proc_tgid_stat),
3046 ONE("statm", S_IRUGO, proc_pid_statm),
3047 REG("maps", S_IRUGO, proc_pid_maps_operations),
3049 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
3051 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3052 LNK("cwd", proc_cwd_link),
3053 LNK("root", proc_root_link),
3054 LNK("exe", proc_exe_link),
3055 REG("mounts", S_IRUGO, proc_mounts_operations),
3056 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
3057 REG("mountstats", S_IRUSR, proc_mountstats_operations),
3058 #ifdef CONFIG_PROC_PAGE_MONITOR
3059 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3060 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
3061 REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
3062 REG("pagemap", S_IRUSR, proc_pagemap_operations),
3064 #ifdef CONFIG_SECURITY
3065 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
3067 #ifdef CONFIG_KALLSYMS
3068 ONE("wchan", S_IRUGO, proc_pid_wchan),
3070 #ifdef CONFIG_STACKTRACE
3071 ONE("stack", S_IRUSR, proc_pid_stack),
3073 #ifdef CONFIG_SCHED_INFO
3074 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
3076 #ifdef CONFIG_LATENCYTOP
3077 REG("latency", S_IRUGO, proc_lstats_operations),
3079 #ifdef CONFIG_PROC_PID_CPUSET
3080 ONE("cpuset", S_IRUGO, proc_cpuset_show),
3082 #ifdef CONFIG_CGROUPS
3083 ONE("cgroup", S_IRUGO, proc_cgroup_show),
3085 ONE("oom_score", S_IRUGO, proc_oom_score),
3086 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
3087 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
3089 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3090 REG("sessionid", S_IRUGO, proc_sessionid_operations),
3092 #ifdef CONFIG_FAULT_INJECTION
3093 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3094 REG("fail-nth", 0644, proc_fail_nth_operations),
3096 #ifdef CONFIG_ELF_CORE
3097 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
3099 #ifdef CONFIG_TASK_IO_ACCOUNTING
3100 ONE("io", S_IRUSR, proc_tgid_io_accounting),
3102 #ifdef CONFIG_USER_NS
3103 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3104 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
3105 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
3106 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
3108 #if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
3109 REG("timers", S_IRUGO, proc_timers_operations),
3111 REG("timerslack_ns", S_IRUGO|S_IWUGO, proc_pid_set_timerslack_ns_operations),
3112 #ifdef CONFIG_LIVEPATCH
3113 ONE("patch_state", S_IRUSR, proc_pid_patch_state),
3115 #ifdef CONFIG_STACKLEAK_METRICS
3116 ONE("stack_depth", S_IRUGO, proc_stack_depth),
3118 #ifdef CONFIG_PROC_PID_ARCH_STATUS
3119 ONE("arch_status", S_IRUGO, proc_pid_arch_status),
3123 static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
3125 return proc_pident_readdir(file, ctx,
3126 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
3129 static const struct file_operations proc_tgid_base_operations = {
3130 .read = generic_read_dir,
3131 .iterate_shared = proc_tgid_base_readdir,
3132 .llseek = generic_file_llseek,
3135 struct pid *tgid_pidfd_to_pid(const struct file *file)
3137 if (file->f_op != &proc_tgid_base_operations)
3138 return ERR_PTR(-EBADF);
3140 return proc_pid(file_inode(file));
3143 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3145 return proc_pident_lookup(dir, dentry,
3147 tgid_base_stuff + ARRAY_SIZE(tgid_base_stuff));
3150 static const struct inode_operations proc_tgid_base_inode_operations = {
3151 .lookup = proc_tgid_base_lookup,
3152 .getattr = pid_getattr,
3153 .setattr = proc_setattr,
3154 .permission = proc_pid_permission,
3157 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
3159 struct dentry *dentry, *leader, *dir;
3164 name.len = snprintf(buf, sizeof(buf), "%u", pid);
3165 /* no ->d_hash() rejects on procfs */
3166 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
3168 d_invalidate(dentry);
3176 name.len = snprintf(buf, sizeof(buf), "%u", tgid);
3177 leader = d_hash_and_lookup(mnt->mnt_root, &name);
3182 name.len = strlen(name.name);
3183 dir = d_hash_and_lookup(leader, &name);
3185 goto out_put_leader;
3188 name.len = snprintf(buf, sizeof(buf), "%u", pid);
3189 dentry = d_hash_and_lookup(dir, &name);
3191 d_invalidate(dentry);
3203 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
3204 * @task: task that should be flushed.
3206 * When flushing dentries from proc, one needs to flush them from global
3207 * proc (proc_mnt) and from all the namespaces' procs this task was seen
3208 * in. This call is supposed to do all of this job.
3210 * Looks in the dcache for
3212 * /proc/@tgid/task/@pid
3213 * if either directory is present flushes it and all of it'ts children
3216 * It is safe and reasonable to cache /proc entries for a task until
3217 * that task exits. After that they just clog up the dcache with
3218 * useless entries, possibly causing useful dcache entries to be
3219 * flushed instead. This routine is proved to flush those useless
3220 * dcache entries at process exit time.
3222 * NOTE: This routine is just an optimization so it does not guarantee
3223 * that no dcache entries will exist at process exit time it
3224 * just makes it very unlikely that any will persist.
3227 void proc_flush_task(struct task_struct *task)
3230 struct pid *pid, *tgid;
3233 pid = task_pid(task);
3234 tgid = task_tgid(task);
3236 for (i = 0; i <= pid->level; i++) {
3237 upid = &pid->numbers[i];
3238 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
3239 tgid->numbers[i].nr);
3243 static struct dentry *proc_pid_instantiate(struct dentry * dentry,
3244 struct task_struct *task, const void *ptr)
3246 struct inode *inode;
3248 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
3250 return ERR_PTR(-ENOENT);
3252 inode->i_op = &proc_tgid_base_inode_operations;
3253 inode->i_fop = &proc_tgid_base_operations;
3254 inode->i_flags|=S_IMMUTABLE;
3256 set_nlink(inode, nlink_tgid);
3257 pid_update_inode(task, inode);
3259 d_set_d_op(dentry, &pid_dentry_operations);
3260 return d_splice_alias(inode, dentry);
3263 struct dentry *proc_pid_lookup(struct dentry *dentry, unsigned int flags)
3265 struct task_struct *task;
3267 struct pid_namespace *ns;
3268 struct dentry *result = ERR_PTR(-ENOENT);
3270 tgid = name_to_int(&dentry->d_name);
3274 ns = dentry->d_sb->s_fs_info;
3276 task = find_task_by_pid_ns(tgid, ns);
3278 get_task_struct(task);
3283 result = proc_pid_instantiate(dentry, task, NULL);
3284 put_task_struct(task);
3290 * Find the first task with tgid >= tgid
3295 struct task_struct *task;
3297 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3302 put_task_struct(iter.task);
3306 pid = find_ge_pid(iter.tgid, ns);
3308 iter.tgid = pid_nr_ns(pid, ns);
3309 iter.task = pid_task(pid, PIDTYPE_PID);
3310 /* What we to know is if the pid we have find is the
3311 * pid of a thread_group_leader. Testing for task
3312 * being a thread_group_leader is the obvious thing
3313 * todo but there is a window when it fails, due to
3314 * the pid transfer logic in de_thread.
3316 * So we perform the straight forward test of seeing
3317 * if the pid we have found is the pid of a thread
3318 * group leader, and don't worry if the task we have
3319 * found doesn't happen to be a thread group leader.
3320 * As we don't care in the case of readdir.
3322 if (!iter.task || !has_group_leader_pid(iter.task)) {
3326 get_task_struct(iter.task);
3332 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
3334 /* for the /proc/ directory itself, after non-process stuff has been done */
3335 int proc_pid_readdir(struct file *file, struct dir_context *ctx)
3337 struct tgid_iter iter;
3338 struct pid_namespace *ns = proc_pid_ns(file_inode(file));
3339 loff_t pos = ctx->pos;
3341 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
3344 if (pos == TGID_OFFSET - 2) {
3345 struct inode *inode = d_inode(ns->proc_self);
3346 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
3348 ctx->pos = pos = pos + 1;
3350 if (pos == TGID_OFFSET - 1) {
3351 struct inode *inode = d_inode(ns->proc_thread_self);
3352 if (!dir_emit(ctx, "thread-self", 11, inode->i_ino, DT_LNK))
3354 ctx->pos = pos = pos + 1;
3356 iter.tgid = pos - TGID_OFFSET;
3358 for (iter = next_tgid(ns, iter);
3360 iter.tgid += 1, iter = next_tgid(ns, iter)) {
3365 if (!has_pid_permissions(ns, iter.task, HIDEPID_INVISIBLE))
3368 len = snprintf(name, sizeof(name), "%u", iter.tgid);
3369 ctx->pos = iter.tgid + TGID_OFFSET;
3370 if (!proc_fill_cache(file, ctx, name, len,
3371 proc_pid_instantiate, iter.task, NULL)) {
3372 put_task_struct(iter.task);
3376 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
3381 * proc_tid_comm_permission is a special permission function exclusively
3382 * used for the node /proc/<pid>/task/<tid>/comm.
3383 * It bypasses generic permission checks in the case where a task of the same
3384 * task group attempts to access the node.
3385 * The rationale behind this is that glibc and bionic access this node for
3386 * cross thread naming (pthread_set/getname_np(!self)). However, if
3387 * PR_SET_DUMPABLE gets set to 0 this node among others becomes uid=0 gid=0,
3388 * which locks out the cross thread naming implementation.
3389 * This function makes sure that the node is always accessible for members of
3390 * same thread group.
3392 static int proc_tid_comm_permission(struct inode *inode, int mask)
3394 bool is_same_tgroup;
3395 struct task_struct *task;
3397 task = get_proc_task(inode);
3400 is_same_tgroup = same_thread_group(current, task);
3401 put_task_struct(task);
3403 if (likely(is_same_tgroup && !(mask & MAY_EXEC))) {
3404 /* This file (/proc/<pid>/task/<tid>/comm) can always be
3405 * read or written by the members of the corresponding
3411 return generic_permission(inode, mask);
3414 static const struct inode_operations proc_tid_comm_inode_operations = {
3415 .setattr = proc_setattr,
3416 .permission = proc_tid_comm_permission,
3422 static const struct pid_entry tid_base_stuff[] = {
3423 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
3424 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
3425 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
3427 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
3429 REG("environ", S_IRUSR, proc_environ_operations),
3430 REG("auxv", S_IRUSR, proc_auxv_operations),
3431 ONE("status", S_IRUGO, proc_pid_status),
3432 ONE("personality", S_IRUSR, proc_pid_personality),
3433 ONE("limits", S_IRUGO, proc_pid_limits),
3434 #ifdef CONFIG_SCHED_DEBUG
3435 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
3437 NOD("comm", S_IFREG|S_IRUGO|S_IWUSR,
3438 &proc_tid_comm_inode_operations,
3439 &proc_pid_set_comm_operations, {}),
3440 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3441 ONE("syscall", S_IRUSR, proc_pid_syscall),
3443 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
3444 ONE("stat", S_IRUGO, proc_tid_stat),
3445 ONE("statm", S_IRUGO, proc_pid_statm),
3446 REG("maps", S_IRUGO, proc_pid_maps_operations),
3447 #ifdef CONFIG_PROC_CHILDREN
3448 REG("children", S_IRUGO, proc_tid_children_operations),
3451 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
3453 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3454 LNK("cwd", proc_cwd_link),
3455 LNK("root", proc_root_link),
3456 LNK("exe", proc_exe_link),
3457 REG("mounts", S_IRUGO, proc_mounts_operations),
3458 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
3459 #ifdef CONFIG_PROC_PAGE_MONITOR
3460 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3461 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
3462 REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
3463 REG("pagemap", S_IRUSR, proc_pagemap_operations),
3465 #ifdef CONFIG_SECURITY
3466 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
3468 #ifdef CONFIG_KALLSYMS
3469 ONE("wchan", S_IRUGO, proc_pid_wchan),
3471 #ifdef CONFIG_STACKTRACE
3472 ONE("stack", S_IRUSR, proc_pid_stack),
3474 #ifdef CONFIG_SCHED_INFO
3475 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
3477 #ifdef CONFIG_LATENCYTOP
3478 REG("latency", S_IRUGO, proc_lstats_operations),
3480 #ifdef CONFIG_PROC_PID_CPUSET
3481 ONE("cpuset", S_IRUGO, proc_cpuset_show),
3483 #ifdef CONFIG_CGROUPS
3484 ONE("cgroup", S_IRUGO, proc_cgroup_show),
3486 ONE("oom_score", S_IRUGO, proc_oom_score),
3487 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
3488 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
3490 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3491 REG("sessionid", S_IRUGO, proc_sessionid_operations),
3493 #ifdef CONFIG_FAULT_INJECTION
3494 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3495 REG("fail-nth", 0644, proc_fail_nth_operations),
3497 #ifdef CONFIG_TASK_IO_ACCOUNTING
3498 ONE("io", S_IRUSR, proc_tid_io_accounting),
3500 #ifdef CONFIG_USER_NS
3501 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3502 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
3503 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
3504 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
3506 #ifdef CONFIG_LIVEPATCH
3507 ONE("patch_state", S_IRUSR, proc_pid_patch_state),
3509 #ifdef CONFIG_PROC_PID_ARCH_STATUS
3510 ONE("arch_status", S_IRUGO, proc_pid_arch_status),
3514 static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
3516 return proc_pident_readdir(file, ctx,
3517 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3520 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3522 return proc_pident_lookup(dir, dentry,
3524 tid_base_stuff + ARRAY_SIZE(tid_base_stuff));
3527 static const struct file_operations proc_tid_base_operations = {
3528 .read = generic_read_dir,
3529 .iterate_shared = proc_tid_base_readdir,
3530 .llseek = generic_file_llseek,
3533 static const struct inode_operations proc_tid_base_inode_operations = {
3534 .lookup = proc_tid_base_lookup,
3535 .getattr = pid_getattr,
3536 .setattr = proc_setattr,
3539 static struct dentry *proc_task_instantiate(struct dentry *dentry,
3540 struct task_struct *task, const void *ptr)
3542 struct inode *inode;
3543 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
3545 return ERR_PTR(-ENOENT);
3547 inode->i_op = &proc_tid_base_inode_operations;
3548 inode->i_fop = &proc_tid_base_operations;
3549 inode->i_flags |= S_IMMUTABLE;
3551 set_nlink(inode, nlink_tid);
3552 pid_update_inode(task, inode);
3554 d_set_d_op(dentry, &pid_dentry_operations);
3555 return d_splice_alias(inode, dentry);
3558 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
3560 struct task_struct *task;
3561 struct task_struct *leader = get_proc_task(dir);
3563 struct pid_namespace *ns;
3564 struct dentry *result = ERR_PTR(-ENOENT);
3569 tid = name_to_int(&dentry->d_name);
3573 ns = dentry->d_sb->s_fs_info;
3575 task = find_task_by_pid_ns(tid, ns);
3577 get_task_struct(task);
3581 if (!same_thread_group(leader, task))
3584 result = proc_task_instantiate(dentry, task, NULL);
3586 put_task_struct(task);
3588 put_task_struct(leader);
3594 * Find the first tid of a thread group to return to user space.
3596 * Usually this is just the thread group leader, but if the users
3597 * buffer was too small or there was a seek into the middle of the
3598 * directory we have more work todo.
3600 * In the case of a short read we start with find_task_by_pid.
3602 * In the case of a seek we start with the leader and walk nr
3605 static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
3606 struct pid_namespace *ns)
3608 struct task_struct *pos, *task;
3609 unsigned long nr = f_pos;
3611 if (nr != f_pos) /* 32bit overflow? */
3615 task = pid_task(pid, PIDTYPE_PID);
3619 /* Attempt to start with the tid of a thread */
3621 pos = find_task_by_pid_ns(tid, ns);
3622 if (pos && same_thread_group(pos, task))
3626 /* If nr exceeds the number of threads there is nothing todo */
3627 if (nr >= get_nr_threads(task))
3630 /* If we haven't found our starting place yet start
3631 * with the leader and walk nr threads forward.
3633 pos = task = task->group_leader;
3637 } while_each_thread(task, pos);
3642 get_task_struct(pos);
3649 * Find the next thread in the thread list.
3650 * Return NULL if there is an error or no next thread.
3652 * The reference to the input task_struct is released.
3654 static struct task_struct *next_tid(struct task_struct *start)
3656 struct task_struct *pos = NULL;
3658 if (pid_alive(start)) {
3659 pos = next_thread(start);
3660 if (thread_group_leader(pos))
3663 get_task_struct(pos);
3666 put_task_struct(start);
3670 /* for the /proc/TGID/task/ directories */
3671 static int proc_task_readdir(struct file *file, struct dir_context *ctx)
3673 struct inode *inode = file_inode(file);
3674 struct task_struct *task;
3675 struct pid_namespace *ns;
3678 if (proc_inode_is_dead(inode))
3681 if (!dir_emit_dots(file, ctx))
3684 /* f_version caches the tgid value that the last readdir call couldn't
3685 * return. lseek aka telldir automagically resets f_version to 0.
3687 ns = proc_pid_ns(inode);
3688 tid = (int)file->f_version;
3689 file->f_version = 0;
3690 for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
3692 task = next_tid(task), ctx->pos++) {
3695 tid = task_pid_nr_ns(task, ns);
3696 len = snprintf(name, sizeof(name), "%u", tid);
3697 if (!proc_fill_cache(file, ctx, name, len,
3698 proc_task_instantiate, task, NULL)) {
3699 /* returning this tgid failed, save it as the first
3700 * pid for the next readir call */
3701 file->f_version = (u64)tid;
3702 put_task_struct(task);
3710 static int proc_task_getattr(const struct path *path, struct kstat *stat,
3711 u32 request_mask, unsigned int query_flags)
3713 struct inode *inode = d_inode(path->dentry);
3714 struct task_struct *p = get_proc_task(inode);
3715 generic_fillattr(inode, stat);
3718 stat->nlink += get_nr_threads(p);
3725 static const struct inode_operations proc_task_inode_operations = {
3726 .lookup = proc_task_lookup,
3727 .getattr = proc_task_getattr,
3728 .setattr = proc_setattr,
3729 .permission = proc_pid_permission,
3732 static const struct file_operations proc_task_operations = {
3733 .read = generic_read_dir,
3734 .iterate_shared = proc_task_readdir,
3735 .llseek = generic_file_llseek,
3738 void __init set_proc_pid_nlink(void)
3740 nlink_tid = pid_entry_nlink(tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3741 nlink_tgid = pid_entry_nlink(tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));