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, 0);
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, 0);
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, 0);
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;
434 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
438 trace.nr_entries = 0;
439 trace.max_entries = MAX_STACK_TRACE_DEPTH;
440 trace.entries = entries;
443 err = lock_trace(task);
445 save_stack_trace_tsk(task, &trace);
447 for (i = 0; i < trace.nr_entries; i++) {
448 seq_printf(m, "[<0>] %pB\n", (void *)entries[i]);
458 #ifdef CONFIG_SCHED_INFO
460 * Provides /proc/PID/schedstat
462 static int proc_pid_schedstat(struct seq_file *m, struct pid_namespace *ns,
463 struct pid *pid, struct task_struct *task)
465 if (unlikely(!sched_info_on()))
466 seq_printf(m, "0 0 0\n");
468 seq_printf(m, "%llu %llu %lu\n",
469 (unsigned long long)task->se.sum_exec_runtime,
470 (unsigned long long)task->sched_info.run_delay,
471 task->sched_info.pcount);
477 #ifdef CONFIG_LATENCYTOP
478 static int lstats_show_proc(struct seq_file *m, void *v)
481 struct inode *inode = m->private;
482 struct task_struct *task = get_proc_task(inode);
486 seq_puts(m, "Latency Top version : v0.1\n");
487 for (i = 0; i < 32; i++) {
488 struct latency_record *lr = &task->latency_record[i];
489 if (lr->backtrace[0]) {
491 seq_printf(m, "%i %li %li",
492 lr->count, lr->time, lr->max);
493 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
494 unsigned long bt = lr->backtrace[q];
499 seq_printf(m, " %ps", (void *)bt);
505 put_task_struct(task);
509 static int lstats_open(struct inode *inode, struct file *file)
511 return single_open(file, lstats_show_proc, inode);
514 static ssize_t lstats_write(struct file *file, const char __user *buf,
515 size_t count, loff_t *offs)
517 struct task_struct *task = get_proc_task(file_inode(file));
521 clear_all_latency_tracing(task);
522 put_task_struct(task);
527 static const struct file_operations proc_lstats_operations = {
530 .write = lstats_write,
532 .release = single_release,
537 static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns,
538 struct pid *pid, struct task_struct *task)
540 unsigned long totalpages = totalram_pages + total_swap_pages;
541 unsigned long points = 0;
543 points = oom_badness(task, NULL, NULL, totalpages) *
545 seq_printf(m, "%lu\n", points);
555 static const struct limit_names lnames[RLIM_NLIMITS] = {
556 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
557 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
558 [RLIMIT_DATA] = {"Max data size", "bytes"},
559 [RLIMIT_STACK] = {"Max stack size", "bytes"},
560 [RLIMIT_CORE] = {"Max core file size", "bytes"},
561 [RLIMIT_RSS] = {"Max resident set", "bytes"},
562 [RLIMIT_NPROC] = {"Max processes", "processes"},
563 [RLIMIT_NOFILE] = {"Max open files", "files"},
564 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
565 [RLIMIT_AS] = {"Max address space", "bytes"},
566 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
567 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
568 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
569 [RLIMIT_NICE] = {"Max nice priority", NULL},
570 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
571 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
574 /* Display limits for a process */
575 static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
576 struct pid *pid, struct task_struct *task)
581 struct rlimit rlim[RLIM_NLIMITS];
583 if (!lock_task_sighand(task, &flags))
585 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
586 unlock_task_sighand(task, &flags);
589 * print the file header
591 seq_printf(m, "%-25s %-20s %-20s %-10s\n",
592 "Limit", "Soft Limit", "Hard Limit", "Units");
594 for (i = 0; i < RLIM_NLIMITS; i++) {
595 if (rlim[i].rlim_cur == RLIM_INFINITY)
596 seq_printf(m, "%-25s %-20s ",
597 lnames[i].name, "unlimited");
599 seq_printf(m, "%-25s %-20lu ",
600 lnames[i].name, rlim[i].rlim_cur);
602 if (rlim[i].rlim_max == RLIM_INFINITY)
603 seq_printf(m, "%-20s ", "unlimited");
605 seq_printf(m, "%-20lu ", rlim[i].rlim_max);
608 seq_printf(m, "%-10s\n", lnames[i].unit);
616 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
617 static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
618 struct pid *pid, struct task_struct *task)
621 unsigned long args[6], sp, pc;
624 res = lock_trace(task);
628 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
629 seq_puts(m, "running\n");
631 seq_printf(m, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
634 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
636 args[0], args[1], args[2], args[3], args[4], args[5],
642 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
644 /************************************************************************/
645 /* Here the fs part begins */
646 /************************************************************************/
648 /* permission checks */
649 static int proc_fd_access_allowed(struct inode *inode)
651 struct task_struct *task;
653 /* Allow access to a task's file descriptors if it is us or we
654 * may use ptrace attach to the process and find out that
657 task = get_proc_task(inode);
659 allowed = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
660 put_task_struct(task);
665 int proc_setattr(struct dentry *dentry, struct iattr *attr)
668 struct inode *inode = d_inode(dentry);
670 if (attr->ia_valid & ATTR_MODE)
673 error = setattr_prepare(dentry, attr);
677 setattr_copy(inode, attr);
678 mark_inode_dirty(inode);
683 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
684 * or euid/egid (for hide_pid_min=2)?
686 static bool has_pid_permissions(struct pid_namespace *pid,
687 struct task_struct *task,
690 if (pid->hide_pid < hide_pid_min)
692 if (in_group_p(pid->pid_gid))
694 return ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
698 static int proc_pid_permission(struct inode *inode, int mask)
700 struct pid_namespace *pid = inode->i_sb->s_fs_info;
701 struct task_struct *task;
704 task = get_proc_task(inode);
707 has_perms = has_pid_permissions(pid, task, HIDEPID_NO_ACCESS);
708 put_task_struct(task);
711 if (pid->hide_pid == HIDEPID_INVISIBLE) {
713 * Let's make getdents(), stat(), and open()
714 * consistent with each other. If a process
715 * may not stat() a file, it shouldn't be seen
723 return generic_permission(inode, mask);
728 static const struct inode_operations proc_def_inode_operations = {
729 .setattr = proc_setattr,
732 static int proc_single_show(struct seq_file *m, void *v)
734 struct inode *inode = m->private;
735 struct pid_namespace *ns;
737 struct task_struct *task;
740 ns = inode->i_sb->s_fs_info;
741 pid = proc_pid(inode);
742 task = get_pid_task(pid, PIDTYPE_PID);
746 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
748 put_task_struct(task);
752 static int proc_single_open(struct inode *inode, struct file *filp)
754 return single_open(filp, proc_single_show, inode);
757 static const struct file_operations proc_single_file_operations = {
758 .open = proc_single_open,
761 .release = single_release,
765 struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode)
767 struct task_struct *task = get_proc_task(inode);
768 struct mm_struct *mm = ERR_PTR(-ESRCH);
771 mm = mm_access(task, mode | PTRACE_MODE_FSCREDS);
772 put_task_struct(task);
774 if (!IS_ERR_OR_NULL(mm)) {
775 /* ensure this mm_struct can't be freed */
777 /* but do not pin its memory */
785 static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
787 struct mm_struct *mm = proc_mem_open(inode, mode);
792 file->private_data = mm;
796 static int mem_open(struct inode *inode, struct file *file)
798 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
800 /* OK to pass negative loff_t, we can catch out-of-range */
801 file->f_mode |= FMODE_UNSIGNED_OFFSET;
806 static ssize_t mem_rw(struct file *file, char __user *buf,
807 size_t count, loff_t *ppos, int write)
809 struct mm_struct *mm = file->private_data;
810 unsigned long addr = *ppos;
818 page = (char *)__get_free_page(GFP_KERNEL);
823 if (!mmget_not_zero(mm))
826 flags = FOLL_FORCE | (write ? FOLL_WRITE : 0);
829 int this_len = min_t(int, count, PAGE_SIZE);
831 if (write && copy_from_user(page, buf, this_len)) {
836 this_len = access_remote_vm(mm, addr, page, this_len, flags);
843 if (!write && copy_to_user(buf, page, this_len)) {
857 free_page((unsigned long) page);
861 static ssize_t mem_read(struct file *file, char __user *buf,
862 size_t count, loff_t *ppos)
864 return mem_rw(file, buf, count, ppos, 0);
867 static ssize_t mem_write(struct file *file, const char __user *buf,
868 size_t count, loff_t *ppos)
870 return mem_rw(file, (char __user*)buf, count, ppos, 1);
873 loff_t mem_lseek(struct file *file, loff_t offset, int orig)
877 file->f_pos = offset;
880 file->f_pos += offset;
885 force_successful_syscall_return();
889 static int mem_release(struct inode *inode, struct file *file)
891 struct mm_struct *mm = file->private_data;
897 static const struct file_operations proc_mem_operations = {
902 .release = mem_release,
905 static int environ_open(struct inode *inode, struct file *file)
907 return __mem_open(inode, file, PTRACE_MODE_READ);
910 static ssize_t environ_read(struct file *file, char __user *buf,
911 size_t count, loff_t *ppos)
914 unsigned long src = *ppos;
916 struct mm_struct *mm = file->private_data;
917 unsigned long env_start, env_end;
919 /* Ensure the process spawned far enough to have an environment. */
920 if (!mm || !mm->env_end)
923 page = (char *)__get_free_page(GFP_KERNEL);
928 if (!mmget_not_zero(mm))
931 down_read(&mm->mmap_sem);
932 env_start = mm->env_start;
933 env_end = mm->env_end;
934 up_read(&mm->mmap_sem);
937 size_t this_len, max_len;
940 if (src >= (env_end - env_start))
943 this_len = env_end - (env_start + src);
945 max_len = min_t(size_t, PAGE_SIZE, count);
946 this_len = min(max_len, this_len);
948 retval = access_remote_vm(mm, (env_start + src), page, this_len, 0);
955 if (copy_to_user(buf, page, retval)) {
969 free_page((unsigned long) page);
973 static const struct file_operations proc_environ_operations = {
974 .open = environ_open,
975 .read = environ_read,
976 .llseek = generic_file_llseek,
977 .release = mem_release,
980 static int auxv_open(struct inode *inode, struct file *file)
982 return __mem_open(inode, file, PTRACE_MODE_READ_FSCREDS);
985 static ssize_t auxv_read(struct file *file, char __user *buf,
986 size_t count, loff_t *ppos)
988 struct mm_struct *mm = file->private_data;
989 unsigned int nwords = 0;
995 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
996 return simple_read_from_buffer(buf, count, ppos, mm->saved_auxv,
997 nwords * sizeof(mm->saved_auxv[0]));
1000 static const struct file_operations proc_auxv_operations = {
1003 .llseek = generic_file_llseek,
1004 .release = mem_release,
1007 static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
1010 struct task_struct *task = get_proc_task(file_inode(file));
1011 char buffer[PROC_NUMBUF];
1012 int oom_adj = OOM_ADJUST_MIN;
1017 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
1018 oom_adj = OOM_ADJUST_MAX;
1020 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
1022 put_task_struct(task);
1023 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
1024 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1027 static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
1029 static DEFINE_MUTEX(oom_adj_mutex);
1030 struct mm_struct *mm = NULL;
1031 struct task_struct *task;
1034 task = get_proc_task(file_inode(file));
1038 mutex_lock(&oom_adj_mutex);
1040 if (oom_adj < task->signal->oom_score_adj &&
1041 !capable(CAP_SYS_RESOURCE)) {
1046 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
1047 * /proc/pid/oom_score_adj instead.
1049 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
1050 current->comm, task_pid_nr(current), task_pid_nr(task),
1053 if ((short)oom_adj < task->signal->oom_score_adj_min &&
1054 !capable(CAP_SYS_RESOURCE)) {
1061 * Make sure we will check other processes sharing the mm if this is
1062 * not vfrok which wants its own oom_score_adj.
1063 * pin the mm so it doesn't go away and get reused after task_unlock
1065 if (!task->vfork_done) {
1066 struct task_struct *p = find_lock_task_mm(task);
1069 if (atomic_read(&p->mm->mm_users) > 1) {
1077 task->signal->oom_score_adj = oom_adj;
1078 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1079 task->signal->oom_score_adj_min = (short)oom_adj;
1080 trace_oom_score_adj_update(task);
1083 struct task_struct *p;
1086 for_each_process(p) {
1087 if (same_thread_group(task, p))
1090 /* do not touch kernel threads or the global init */
1091 if (p->flags & PF_KTHREAD || is_global_init(p))
1095 if (!p->vfork_done && process_shares_mm(p, mm)) {
1096 pr_info("updating oom_score_adj for %d (%s) from %d to %d because it shares mm with %d (%s). Report if this is unexpected.\n",
1097 task_pid_nr(p), p->comm,
1098 p->signal->oom_score_adj, oom_adj,
1099 task_pid_nr(task), task->comm);
1100 p->signal->oom_score_adj = oom_adj;
1101 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1102 p->signal->oom_score_adj_min = (short)oom_adj;
1110 mutex_unlock(&oom_adj_mutex);
1111 put_task_struct(task);
1116 * /proc/pid/oom_adj exists solely for backwards compatibility with previous
1117 * kernels. The effective policy is defined by oom_score_adj, which has a
1118 * different scale: oom_adj grew exponentially and oom_score_adj grows linearly.
1119 * Values written to oom_adj are simply mapped linearly to oom_score_adj.
1120 * Processes that become oom disabled via oom_adj will still be oom disabled
1121 * with this implementation.
1123 * oom_adj cannot be removed since existing userspace binaries use it.
1125 static ssize_t oom_adj_write(struct file *file, const char __user *buf,
1126 size_t count, loff_t *ppos)
1128 char buffer[PROC_NUMBUF];
1132 memset(buffer, 0, sizeof(buffer));
1133 if (count > sizeof(buffer) - 1)
1134 count = sizeof(buffer) - 1;
1135 if (copy_from_user(buffer, buf, count)) {
1140 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
1143 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
1144 oom_adj != OOM_DISABLE) {
1150 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1151 * value is always attainable.
1153 if (oom_adj == OOM_ADJUST_MAX)
1154 oom_adj = OOM_SCORE_ADJ_MAX;
1156 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
1158 err = __set_oom_adj(file, oom_adj, true);
1160 return err < 0 ? err : count;
1163 static const struct file_operations proc_oom_adj_operations = {
1164 .read = oom_adj_read,
1165 .write = oom_adj_write,
1166 .llseek = generic_file_llseek,
1169 static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1170 size_t count, loff_t *ppos)
1172 struct task_struct *task = get_proc_task(file_inode(file));
1173 char buffer[PROC_NUMBUF];
1174 short oom_score_adj = OOM_SCORE_ADJ_MIN;
1179 oom_score_adj = task->signal->oom_score_adj;
1180 put_task_struct(task);
1181 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
1182 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1185 static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1186 size_t count, loff_t *ppos)
1188 char buffer[PROC_NUMBUF];
1192 memset(buffer, 0, sizeof(buffer));
1193 if (count > sizeof(buffer) - 1)
1194 count = sizeof(buffer) - 1;
1195 if (copy_from_user(buffer, buf, count)) {
1200 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
1203 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
1204 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1209 err = __set_oom_adj(file, oom_score_adj, false);
1211 return err < 0 ? err : count;
1214 static const struct file_operations proc_oom_score_adj_operations = {
1215 .read = oom_score_adj_read,
1216 .write = oom_score_adj_write,
1217 .llseek = default_llseek,
1220 #ifdef CONFIG_AUDITSYSCALL
1221 #define TMPBUFLEN 11
1222 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1223 size_t count, loff_t *ppos)
1225 struct inode * inode = file_inode(file);
1226 struct task_struct *task = get_proc_task(inode);
1228 char tmpbuf[TMPBUFLEN];
1232 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1233 from_kuid(file->f_cred->user_ns,
1234 audit_get_loginuid(task)));
1235 put_task_struct(task);
1236 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1239 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1240 size_t count, loff_t *ppos)
1242 struct inode * inode = file_inode(file);
1248 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1255 /* No partial writes. */
1259 rv = kstrtou32_from_user(buf, count, 10, &loginuid);
1263 /* is userspace tring to explicitly UNSET the loginuid? */
1264 if (loginuid == AUDIT_UID_UNSET) {
1265 kloginuid = INVALID_UID;
1267 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
1268 if (!uid_valid(kloginuid))
1272 rv = audit_set_loginuid(kloginuid);
1278 static const struct file_operations proc_loginuid_operations = {
1279 .read = proc_loginuid_read,
1280 .write = proc_loginuid_write,
1281 .llseek = generic_file_llseek,
1284 static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1285 size_t count, loff_t *ppos)
1287 struct inode * inode = file_inode(file);
1288 struct task_struct *task = get_proc_task(inode);
1290 char tmpbuf[TMPBUFLEN];
1294 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1295 audit_get_sessionid(task));
1296 put_task_struct(task);
1297 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1300 static const struct file_operations proc_sessionid_operations = {
1301 .read = proc_sessionid_read,
1302 .llseek = generic_file_llseek,
1306 #ifdef CONFIG_FAULT_INJECTION
1307 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1308 size_t count, loff_t *ppos)
1310 struct task_struct *task = get_proc_task(file_inode(file));
1311 char buffer[PROC_NUMBUF];
1317 make_it_fail = task->make_it_fail;
1318 put_task_struct(task);
1320 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1322 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1325 static ssize_t proc_fault_inject_write(struct file * file,
1326 const char __user * buf, size_t count, loff_t *ppos)
1328 struct task_struct *task;
1329 char buffer[PROC_NUMBUF];
1333 if (!capable(CAP_SYS_RESOURCE))
1335 memset(buffer, 0, sizeof(buffer));
1336 if (count > sizeof(buffer) - 1)
1337 count = sizeof(buffer) - 1;
1338 if (copy_from_user(buffer, buf, count))
1340 rv = kstrtoint(strstrip(buffer), 0, &make_it_fail);
1343 if (make_it_fail < 0 || make_it_fail > 1)
1346 task = get_proc_task(file_inode(file));
1349 task->make_it_fail = make_it_fail;
1350 put_task_struct(task);
1355 static const struct file_operations proc_fault_inject_operations = {
1356 .read = proc_fault_inject_read,
1357 .write = proc_fault_inject_write,
1358 .llseek = generic_file_llseek,
1361 static ssize_t proc_fail_nth_write(struct file *file, const char __user *buf,
1362 size_t count, loff_t *ppos)
1364 struct task_struct *task;
1368 err = kstrtouint_from_user(buf, count, 0, &n);
1372 task = get_proc_task(file_inode(file));
1376 put_task_struct(task);
1381 static ssize_t proc_fail_nth_read(struct file *file, char __user *buf,
1382 size_t count, loff_t *ppos)
1384 struct task_struct *task;
1385 char numbuf[PROC_NUMBUF];
1388 task = get_proc_task(file_inode(file));
1391 len = snprintf(numbuf, sizeof(numbuf), "%u\n", task->fail_nth);
1392 len = simple_read_from_buffer(buf, count, ppos, numbuf, len);
1393 put_task_struct(task);
1398 static const struct file_operations proc_fail_nth_operations = {
1399 .read = proc_fail_nth_read,
1400 .write = proc_fail_nth_write,
1405 #ifdef CONFIG_SCHED_DEBUG
1407 * Print out various scheduling related per-task fields:
1409 static int sched_show(struct seq_file *m, void *v)
1411 struct inode *inode = m->private;
1412 struct pid_namespace *ns = inode->i_sb->s_fs_info;
1413 struct task_struct *p;
1415 p = get_proc_task(inode);
1418 proc_sched_show_task(p, ns, m);
1426 sched_write(struct file *file, const char __user *buf,
1427 size_t count, loff_t *offset)
1429 struct inode *inode = file_inode(file);
1430 struct task_struct *p;
1432 p = get_proc_task(inode);
1435 proc_sched_set_task(p);
1442 static int sched_open(struct inode *inode, struct file *filp)
1444 return single_open(filp, sched_show, inode);
1447 static const struct file_operations proc_pid_sched_operations = {
1450 .write = sched_write,
1451 .llseek = seq_lseek,
1452 .release = single_release,
1457 #ifdef CONFIG_SCHED_AUTOGROUP
1459 * Print out autogroup related information:
1461 static int sched_autogroup_show(struct seq_file *m, void *v)
1463 struct inode *inode = m->private;
1464 struct task_struct *p;
1466 p = get_proc_task(inode);
1469 proc_sched_autogroup_show_task(p, m);
1477 sched_autogroup_write(struct file *file, const char __user *buf,
1478 size_t count, loff_t *offset)
1480 struct inode *inode = file_inode(file);
1481 struct task_struct *p;
1482 char buffer[PROC_NUMBUF];
1486 memset(buffer, 0, sizeof(buffer));
1487 if (count > sizeof(buffer) - 1)
1488 count = sizeof(buffer) - 1;
1489 if (copy_from_user(buffer, buf, count))
1492 err = kstrtoint(strstrip(buffer), 0, &nice);
1496 p = get_proc_task(inode);
1500 err = proc_sched_autogroup_set_nice(p, nice);
1509 static int sched_autogroup_open(struct inode *inode, struct file *filp)
1513 ret = single_open(filp, sched_autogroup_show, NULL);
1515 struct seq_file *m = filp->private_data;
1522 static const struct file_operations proc_pid_sched_autogroup_operations = {
1523 .open = sched_autogroup_open,
1525 .write = sched_autogroup_write,
1526 .llseek = seq_lseek,
1527 .release = single_release,
1530 #endif /* CONFIG_SCHED_AUTOGROUP */
1532 static ssize_t comm_write(struct file *file, const char __user *buf,
1533 size_t count, loff_t *offset)
1535 struct inode *inode = file_inode(file);
1536 struct task_struct *p;
1537 char buffer[TASK_COMM_LEN];
1538 const size_t maxlen = sizeof(buffer) - 1;
1540 memset(buffer, 0, sizeof(buffer));
1541 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
1544 p = get_proc_task(inode);
1548 if (same_thread_group(current, p))
1549 set_task_comm(p, buffer);
1558 static int comm_show(struct seq_file *m, void *v)
1560 struct inode *inode = m->private;
1561 struct task_struct *p;
1563 p = get_proc_task(inode);
1568 seq_printf(m, "%s\n", p->comm);
1576 static int comm_open(struct inode *inode, struct file *filp)
1578 return single_open(filp, comm_show, inode);
1581 static const struct file_operations proc_pid_set_comm_operations = {
1584 .write = comm_write,
1585 .llseek = seq_lseek,
1586 .release = single_release,
1589 static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
1591 struct task_struct *task;
1592 struct file *exe_file;
1594 task = get_proc_task(d_inode(dentry));
1597 exe_file = get_task_exe_file(task);
1598 put_task_struct(task);
1600 *exe_path = exe_file->f_path;
1601 path_get(&exe_file->f_path);
1608 static const char *proc_pid_get_link(struct dentry *dentry,
1609 struct inode *inode,
1610 struct delayed_call *done)
1613 int error = -EACCES;
1616 return ERR_PTR(-ECHILD);
1618 /* Are we allowed to snoop on the tasks file descriptors? */
1619 if (!proc_fd_access_allowed(inode))
1622 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1626 nd_jump_link(&path);
1629 return ERR_PTR(error);
1632 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1634 char *tmp = (char *)__get_free_page(GFP_KERNEL);
1641 pathname = d_path(path, tmp, PAGE_SIZE);
1642 len = PTR_ERR(pathname);
1643 if (IS_ERR(pathname))
1645 len = tmp + PAGE_SIZE - 1 - pathname;
1649 if (copy_to_user(buffer, pathname, len))
1652 free_page((unsigned long)tmp);
1656 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1658 int error = -EACCES;
1659 struct inode *inode = d_inode(dentry);
1662 /* Are we allowed to snoop on the tasks file descriptors? */
1663 if (!proc_fd_access_allowed(inode))
1666 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1670 error = do_proc_readlink(&path, buffer, buflen);
1676 const struct inode_operations proc_pid_link_inode_operations = {
1677 .readlink = proc_pid_readlink,
1678 .get_link = proc_pid_get_link,
1679 .setattr = proc_setattr,
1683 /* building an inode */
1685 void task_dump_owner(struct task_struct *task, umode_t mode,
1686 kuid_t *ruid, kgid_t *rgid)
1688 /* Depending on the state of dumpable compute who should own a
1689 * proc file for a task.
1691 const struct cred *cred;
1695 /* Default to the tasks effective ownership */
1697 cred = __task_cred(task);
1703 * Before the /proc/pid/status file was created the only way to read
1704 * the effective uid of a /process was to stat /proc/pid. Reading
1705 * /proc/pid/status is slow enough that procps and other packages
1706 * kept stating /proc/pid. To keep the rules in /proc simple I have
1707 * made this apply to all per process world readable and executable
1710 if (mode != (S_IFDIR|S_IRUGO|S_IXUGO)) {
1711 struct mm_struct *mm;
1714 /* Make non-dumpable tasks owned by some root */
1716 if (get_dumpable(mm) != SUID_DUMP_USER) {
1717 struct user_namespace *user_ns = mm->user_ns;
1719 uid = make_kuid(user_ns, 0);
1720 if (!uid_valid(uid))
1721 uid = GLOBAL_ROOT_UID;
1723 gid = make_kgid(user_ns, 0);
1724 if (!gid_valid(gid))
1725 gid = GLOBAL_ROOT_GID;
1728 uid = GLOBAL_ROOT_UID;
1729 gid = GLOBAL_ROOT_GID;
1737 struct inode *proc_pid_make_inode(struct super_block * sb,
1738 struct task_struct *task, umode_t mode)
1740 struct inode * inode;
1741 struct proc_inode *ei;
1743 /* We need a new inode */
1745 inode = new_inode(sb);
1751 inode->i_mode = mode;
1752 inode->i_ino = get_next_ino();
1753 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
1754 inode->i_op = &proc_def_inode_operations;
1757 * grab the reference to task.
1759 ei->pid = get_task_pid(task, PIDTYPE_PID);
1763 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
1764 security_task_to_inode(task, inode);
1774 int pid_getattr(const struct path *path, struct kstat *stat,
1775 u32 request_mask, unsigned int query_flags)
1777 struct inode *inode = d_inode(path->dentry);
1778 struct task_struct *task;
1779 struct pid_namespace *pid = path->dentry->d_sb->s_fs_info;
1781 generic_fillattr(inode, stat);
1784 stat->uid = GLOBAL_ROOT_UID;
1785 stat->gid = GLOBAL_ROOT_GID;
1786 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1788 if (!has_pid_permissions(pid, task, HIDEPID_INVISIBLE)) {
1791 * This doesn't prevent learning whether PID exists,
1792 * it only makes getattr() consistent with readdir().
1796 task_dump_owner(task, inode->i_mode, &stat->uid, &stat->gid);
1805 * Exceptional case: normally we are not allowed to unhash a busy
1806 * directory. In this case, however, we can do it - no aliasing problems
1807 * due to the way we treat inodes.
1809 * Rewrite the inode's ownerships here because the owning task may have
1810 * performed a setuid(), etc.
1813 int pid_revalidate(struct dentry *dentry, unsigned int flags)
1815 struct inode *inode;
1816 struct task_struct *task;
1818 if (flags & LOOKUP_RCU)
1821 inode = d_inode(dentry);
1822 task = get_proc_task(inode);
1825 task_dump_owner(task, inode->i_mode, &inode->i_uid, &inode->i_gid);
1827 inode->i_mode &= ~(S_ISUID | S_ISGID);
1828 security_task_to_inode(task, inode);
1829 put_task_struct(task);
1835 static inline bool proc_inode_is_dead(struct inode *inode)
1837 return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1840 int pid_delete_dentry(const struct dentry *dentry)
1842 /* Is the task we represent dead?
1843 * If so, then don't put the dentry on the lru list,
1844 * kill it immediately.
1846 return proc_inode_is_dead(d_inode(dentry));
1849 const struct dentry_operations pid_dentry_operations =
1851 .d_revalidate = pid_revalidate,
1852 .d_delete = pid_delete_dentry,
1858 * Fill a directory entry.
1860 * If possible create the dcache entry and derive our inode number and
1861 * file type from dcache entry.
1863 * Since all of the proc inode numbers are dynamically generated, the inode
1864 * numbers do not exist until the inode is cache. This means creating the
1865 * the dcache entry in readdir is necessary to keep the inode numbers
1866 * reported by readdir in sync with the inode numbers reported
1869 bool proc_fill_cache(struct file *file, struct dir_context *ctx,
1870 const char *name, int len,
1871 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1873 struct dentry *child, *dir = file->f_path.dentry;
1874 struct qstr qname = QSTR_INIT(name, len);
1875 struct inode *inode;
1879 child = d_hash_and_lookup(dir, &qname);
1881 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1882 child = d_alloc_parallel(dir, &qname, &wq);
1884 goto end_instantiate;
1885 if (d_in_lookup(child)) {
1886 int err = instantiate(d_inode(dir), child, task, ptr);
1887 d_lookup_done(child);
1890 goto end_instantiate;
1894 inode = d_inode(child);
1896 type = inode->i_mode >> 12;
1898 return dir_emit(ctx, name, len, ino, type);
1901 return dir_emit(ctx, name, len, 1, DT_UNKNOWN);
1905 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1906 * which represent vma start and end addresses.
1908 static int dname_to_vma_addr(struct dentry *dentry,
1909 unsigned long *start, unsigned long *end)
1911 const char *str = dentry->d_name.name;
1912 unsigned long long sval, eval;
1915 len = _parse_integer(str, 16, &sval);
1916 if (len & KSTRTOX_OVERFLOW)
1918 if (sval != (unsigned long)sval)
1926 len = _parse_integer(str, 16, &eval);
1927 if (len & KSTRTOX_OVERFLOW)
1929 if (eval != (unsigned long)eval)
1942 static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
1944 unsigned long vm_start, vm_end;
1945 bool exact_vma_exists = false;
1946 struct mm_struct *mm = NULL;
1947 struct task_struct *task;
1948 struct inode *inode;
1951 if (flags & LOOKUP_RCU)
1954 inode = d_inode(dentry);
1955 task = get_proc_task(inode);
1959 mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
1960 if (IS_ERR_OR_NULL(mm))
1963 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1964 down_read(&mm->mmap_sem);
1965 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1966 up_read(&mm->mmap_sem);
1971 if (exact_vma_exists) {
1972 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
1974 security_task_to_inode(task, inode);
1979 put_task_struct(task);
1985 static const struct dentry_operations tid_map_files_dentry_operations = {
1986 .d_revalidate = map_files_d_revalidate,
1987 .d_delete = pid_delete_dentry,
1990 static int map_files_get_link(struct dentry *dentry, struct path *path)
1992 unsigned long vm_start, vm_end;
1993 struct vm_area_struct *vma;
1994 struct task_struct *task;
1995 struct mm_struct *mm;
1999 task = get_proc_task(d_inode(dentry));
2003 mm = get_task_mm(task);
2004 put_task_struct(task);
2008 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
2013 down_read(&mm->mmap_sem);
2014 vma = find_exact_vma(mm, vm_start, vm_end);
2015 if (vma && vma->vm_file) {
2016 *path = vma->vm_file->f_path;
2020 up_read(&mm->mmap_sem);
2028 struct map_files_info {
2031 unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
2035 * Only allow CAP_SYS_ADMIN to follow the links, due to concerns about how the
2036 * symlinks may be used to bypass permissions on ancestor directories in the
2037 * path to the file in question.
2040 proc_map_files_get_link(struct dentry *dentry,
2041 struct inode *inode,
2042 struct delayed_call *done)
2044 if (!capable(CAP_SYS_ADMIN))
2045 return ERR_PTR(-EPERM);
2047 return proc_pid_get_link(dentry, inode, done);
2051 * Identical to proc_pid_link_inode_operations except for get_link()
2053 static const struct inode_operations proc_map_files_link_inode_operations = {
2054 .readlink = proc_pid_readlink,
2055 .get_link = proc_map_files_get_link,
2056 .setattr = proc_setattr,
2060 proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
2061 struct task_struct *task, const void *ptr)
2063 fmode_t mode = (fmode_t)(unsigned long)ptr;
2064 struct proc_inode *ei;
2065 struct inode *inode;
2067 inode = proc_pid_make_inode(dir->i_sb, task, S_IFLNK |
2068 ((mode & FMODE_READ ) ? S_IRUSR : 0) |
2069 ((mode & FMODE_WRITE) ? S_IWUSR : 0));
2074 ei->op.proc_get_link = map_files_get_link;
2076 inode->i_op = &proc_map_files_link_inode_operations;
2079 d_set_d_op(dentry, &tid_map_files_dentry_operations);
2080 d_add(dentry, inode);
2085 static struct dentry *proc_map_files_lookup(struct inode *dir,
2086 struct dentry *dentry, unsigned int flags)
2088 unsigned long vm_start, vm_end;
2089 struct vm_area_struct *vma;
2090 struct task_struct *task;
2092 struct mm_struct *mm;
2095 task = get_proc_task(dir);
2100 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
2104 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
2107 mm = get_task_mm(task);
2111 down_read(&mm->mmap_sem);
2112 vma = find_exact_vma(mm, vm_start, vm_end);
2117 result = proc_map_files_instantiate(dir, dentry, task,
2118 (void *)(unsigned long)vma->vm_file->f_mode);
2121 up_read(&mm->mmap_sem);
2124 put_task_struct(task);
2126 return ERR_PTR(result);
2129 static const struct inode_operations proc_map_files_inode_operations = {
2130 .lookup = proc_map_files_lookup,
2131 .permission = proc_fd_permission,
2132 .setattr = proc_setattr,
2136 proc_map_files_readdir(struct file *file, struct dir_context *ctx)
2138 struct vm_area_struct *vma;
2139 struct task_struct *task;
2140 struct mm_struct *mm;
2141 unsigned long nr_files, pos, i;
2142 struct flex_array *fa = NULL;
2143 struct map_files_info info;
2144 struct map_files_info *p;
2148 task = get_proc_task(file_inode(file));
2153 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
2157 if (!dir_emit_dots(file, ctx))
2160 mm = get_task_mm(task);
2163 down_read(&mm->mmap_sem);
2168 * We need two passes here:
2170 * 1) Collect vmas of mapped files with mmap_sem taken
2171 * 2) Release mmap_sem and instantiate entries
2173 * otherwise we get lockdep complained, since filldir()
2174 * routine might require mmap_sem taken in might_fault().
2177 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
2178 if (vma->vm_file && ++pos > ctx->pos)
2183 fa = flex_array_alloc(sizeof(info), nr_files,
2185 if (!fa || flex_array_prealloc(fa, 0, nr_files,
2189 flex_array_free(fa);
2190 up_read(&mm->mmap_sem);
2194 for (i = 0, vma = mm->mmap, pos = 2; vma;
2195 vma = vma->vm_next) {
2198 if (++pos <= ctx->pos)
2201 info.mode = vma->vm_file->f_mode;
2202 info.len = snprintf(info.name,
2203 sizeof(info.name), "%lx-%lx",
2204 vma->vm_start, vma->vm_end);
2205 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
2209 up_read(&mm->mmap_sem);
2211 for (i = 0; i < nr_files; i++) {
2212 p = flex_array_get(fa, i);
2213 if (!proc_fill_cache(file, ctx,
2215 proc_map_files_instantiate,
2217 (void *)(unsigned long)p->mode))
2222 flex_array_free(fa);
2226 put_task_struct(task);
2231 static const struct file_operations proc_map_files_operations = {
2232 .read = generic_read_dir,
2233 .iterate_shared = proc_map_files_readdir,
2234 .llseek = generic_file_llseek,
2237 #if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
2238 struct timers_private {
2240 struct task_struct *task;
2241 struct sighand_struct *sighand;
2242 struct pid_namespace *ns;
2243 unsigned long flags;
2246 static void *timers_start(struct seq_file *m, loff_t *pos)
2248 struct timers_private *tp = m->private;
2250 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2252 return ERR_PTR(-ESRCH);
2254 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2256 return ERR_PTR(-ESRCH);
2258 return seq_list_start(&tp->task->signal->posix_timers, *pos);
2261 static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2263 struct timers_private *tp = m->private;
2264 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2267 static void timers_stop(struct seq_file *m, void *v)
2269 struct timers_private *tp = m->private;
2272 unlock_task_sighand(tp->task, &tp->flags);
2277 put_task_struct(tp->task);
2282 static int show_timer(struct seq_file *m, void *v)
2284 struct k_itimer *timer;
2285 struct timers_private *tp = m->private;
2287 static const char * const nstr[] = {
2288 [SIGEV_SIGNAL] = "signal",
2289 [SIGEV_NONE] = "none",
2290 [SIGEV_THREAD] = "thread",
2293 timer = list_entry((struct list_head *)v, struct k_itimer, list);
2294 notify = timer->it_sigev_notify;
2296 seq_printf(m, "ID: %d\n", timer->it_id);
2297 seq_printf(m, "signal: %d/%px\n",
2298 timer->sigq->info.si_signo,
2299 timer->sigq->info.si_value.sival_ptr);
2300 seq_printf(m, "notify: %s/%s.%d\n",
2301 nstr[notify & ~SIGEV_THREAD_ID],
2302 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2303 pid_nr_ns(timer->it_pid, tp->ns));
2304 seq_printf(m, "ClockID: %d\n", timer->it_clock);
2309 static const struct seq_operations proc_timers_seq_ops = {
2310 .start = timers_start,
2311 .next = timers_next,
2312 .stop = timers_stop,
2316 static int proc_timers_open(struct inode *inode, struct file *file)
2318 struct timers_private *tp;
2320 tp = __seq_open_private(file, &proc_timers_seq_ops,
2321 sizeof(struct timers_private));
2325 tp->pid = proc_pid(inode);
2326 tp->ns = inode->i_sb->s_fs_info;
2330 static const struct file_operations proc_timers_operations = {
2331 .open = proc_timers_open,
2333 .llseek = seq_lseek,
2334 .release = seq_release_private,
2338 static ssize_t timerslack_ns_write(struct file *file, const char __user *buf,
2339 size_t count, loff_t *offset)
2341 struct inode *inode = file_inode(file);
2342 struct task_struct *p;
2346 err = kstrtoull_from_user(buf, count, 10, &slack_ns);
2350 p = get_proc_task(inode);
2355 if (!capable(CAP_SYS_NICE)) {
2360 err = security_task_setscheduler(p);
2369 p->timer_slack_ns = p->default_timer_slack_ns;
2371 p->timer_slack_ns = slack_ns;
2380 static int timerslack_ns_show(struct seq_file *m, void *v)
2382 struct inode *inode = m->private;
2383 struct task_struct *p;
2386 p = get_proc_task(inode);
2392 if (!capable(CAP_SYS_NICE)) {
2396 err = security_task_getscheduler(p);
2402 seq_printf(m, "%llu\n", p->timer_slack_ns);
2411 static int timerslack_ns_open(struct inode *inode, struct file *filp)
2413 return single_open(filp, timerslack_ns_show, inode);
2416 static const struct file_operations proc_pid_set_timerslack_ns_operations = {
2417 .open = timerslack_ns_open,
2419 .write = timerslack_ns_write,
2420 .llseek = seq_lseek,
2421 .release = single_release,
2424 static int proc_pident_instantiate(struct inode *dir,
2425 struct dentry *dentry, struct task_struct *task, const void *ptr)
2427 const struct pid_entry *p = ptr;
2428 struct inode *inode;
2429 struct proc_inode *ei;
2431 inode = proc_pid_make_inode(dir->i_sb, task, p->mode);
2436 if (S_ISDIR(inode->i_mode))
2437 set_nlink(inode, 2); /* Use getattr to fix if necessary */
2439 inode->i_op = p->iop;
2441 inode->i_fop = p->fop;
2443 d_set_d_op(dentry, &pid_dentry_operations);
2444 d_add(dentry, inode);
2445 /* Close the race of the process dying before we return the dentry */
2446 if (pid_revalidate(dentry, 0))
2452 static struct dentry *proc_pident_lookup(struct inode *dir,
2453 struct dentry *dentry,
2454 const struct pid_entry *ents,
2458 struct task_struct *task = get_proc_task(dir);
2459 const struct pid_entry *p, *last;
2467 * Yes, it does not scale. And it should not. Don't add
2468 * new entries into /proc/<tgid>/ without very good reasons.
2470 last = &ents[nents];
2471 for (p = ents; p < last; p++) {
2472 if (p->len != dentry->d_name.len)
2474 if (!memcmp(dentry->d_name.name, p->name, p->len))
2480 error = proc_pident_instantiate(dir, dentry, task, p);
2482 put_task_struct(task);
2484 return ERR_PTR(error);
2487 static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
2488 const struct pid_entry *ents, unsigned int nents)
2490 struct task_struct *task = get_proc_task(file_inode(file));
2491 const struct pid_entry *p;
2496 if (!dir_emit_dots(file, ctx))
2499 if (ctx->pos >= nents + 2)
2502 for (p = ents + (ctx->pos - 2); p < ents + nents; p++) {
2503 if (!proc_fill_cache(file, ctx, p->name, p->len,
2504 proc_pident_instantiate, task, p))
2509 put_task_struct(task);
2513 #ifdef CONFIG_SECURITY
2514 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2515 size_t count, loff_t *ppos)
2517 struct inode * inode = file_inode(file);
2520 struct task_struct *task = get_proc_task(inode);
2525 length = security_getprocattr(task,
2526 (char*)file->f_path.dentry->d_name.name,
2528 put_task_struct(task);
2530 length = simple_read_from_buffer(buf, count, ppos, p, length);
2535 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2536 size_t count, loff_t *ppos)
2538 struct inode * inode = file_inode(file);
2541 struct task_struct *task = get_proc_task(inode);
2547 /* A task may only write its own attributes. */
2549 if (current != task)
2552 if (count > PAGE_SIZE)
2555 /* No partial writes. */
2560 page = memdup_user(buf, count);
2562 length = PTR_ERR(page);
2566 /* Guard against adverse ptrace interaction */
2567 length = mutex_lock_interruptible(¤t->signal->cred_guard_mutex);
2571 length = security_setprocattr(file->f_path.dentry->d_name.name,
2573 mutex_unlock(¤t->signal->cred_guard_mutex);
2577 put_task_struct(task);
2582 static const struct file_operations proc_pid_attr_operations = {
2583 .read = proc_pid_attr_read,
2584 .write = proc_pid_attr_write,
2585 .llseek = generic_file_llseek,
2588 static const struct pid_entry attr_dir_stuff[] = {
2589 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2590 REG("prev", S_IRUGO, proc_pid_attr_operations),
2591 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2592 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2593 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2594 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2597 static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
2599 return proc_pident_readdir(file, ctx,
2600 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2603 static const struct file_operations proc_attr_dir_operations = {
2604 .read = generic_read_dir,
2605 .iterate_shared = proc_attr_dir_readdir,
2606 .llseek = generic_file_llseek,
2609 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2610 struct dentry *dentry, unsigned int flags)
2612 return proc_pident_lookup(dir, dentry,
2613 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2616 static const struct inode_operations proc_attr_dir_inode_operations = {
2617 .lookup = proc_attr_dir_lookup,
2618 .getattr = pid_getattr,
2619 .setattr = proc_setattr,
2624 #ifdef CONFIG_ELF_CORE
2625 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2626 size_t count, loff_t *ppos)
2628 struct task_struct *task = get_proc_task(file_inode(file));
2629 struct mm_struct *mm;
2630 char buffer[PROC_NUMBUF];
2638 mm = get_task_mm(task);
2640 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2641 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2642 MMF_DUMP_FILTER_SHIFT));
2644 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2647 put_task_struct(task);
2652 static ssize_t proc_coredump_filter_write(struct file *file,
2653 const char __user *buf,
2657 struct task_struct *task;
2658 struct mm_struct *mm;
2664 ret = kstrtouint_from_user(buf, count, 0, &val);
2669 task = get_proc_task(file_inode(file));
2673 mm = get_task_mm(task);
2678 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2680 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2682 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2687 put_task_struct(task);
2694 static const struct file_operations proc_coredump_filter_operations = {
2695 .read = proc_coredump_filter_read,
2696 .write = proc_coredump_filter_write,
2697 .llseek = generic_file_llseek,
2701 #ifdef CONFIG_TASK_IO_ACCOUNTING
2702 static int do_io_accounting(struct task_struct *task, struct seq_file *m, int whole)
2704 struct task_io_accounting acct = task->ioac;
2705 unsigned long flags;
2708 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2712 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
2717 if (whole && lock_task_sighand(task, &flags)) {
2718 struct task_struct *t = task;
2720 task_io_accounting_add(&acct, &task->signal->ioac);
2721 while_each_thread(task, t)
2722 task_io_accounting_add(&acct, &t->ioac);
2724 unlock_task_sighand(task, &flags);
2731 "read_bytes: %llu\n"
2732 "write_bytes: %llu\n"
2733 "cancelled_write_bytes: %llu\n",
2734 (unsigned long long)acct.rchar,
2735 (unsigned long long)acct.wchar,
2736 (unsigned long long)acct.syscr,
2737 (unsigned long long)acct.syscw,
2738 (unsigned long long)acct.read_bytes,
2739 (unsigned long long)acct.write_bytes,
2740 (unsigned long long)acct.cancelled_write_bytes);
2744 mutex_unlock(&task->signal->cred_guard_mutex);
2748 static int proc_tid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2749 struct pid *pid, struct task_struct *task)
2751 return do_io_accounting(task, m, 0);
2754 static int proc_tgid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2755 struct pid *pid, struct task_struct *task)
2757 return do_io_accounting(task, m, 1);
2759 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2761 #ifdef CONFIG_USER_NS
2762 static int proc_id_map_open(struct inode *inode, struct file *file,
2763 const struct seq_operations *seq_ops)
2765 struct user_namespace *ns = NULL;
2766 struct task_struct *task;
2767 struct seq_file *seq;
2770 task = get_proc_task(inode);
2773 ns = get_user_ns(task_cred_xxx(task, user_ns));
2775 put_task_struct(task);
2780 ret = seq_open(file, seq_ops);
2784 seq = file->private_data;
2794 static int proc_id_map_release(struct inode *inode, struct file *file)
2796 struct seq_file *seq = file->private_data;
2797 struct user_namespace *ns = seq->private;
2799 return seq_release(inode, file);
2802 static int proc_uid_map_open(struct inode *inode, struct file *file)
2804 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2807 static int proc_gid_map_open(struct inode *inode, struct file *file)
2809 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2812 static int proc_projid_map_open(struct inode *inode, struct file *file)
2814 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2817 static const struct file_operations proc_uid_map_operations = {
2818 .open = proc_uid_map_open,
2819 .write = proc_uid_map_write,
2821 .llseek = seq_lseek,
2822 .release = proc_id_map_release,
2825 static const struct file_operations proc_gid_map_operations = {
2826 .open = proc_gid_map_open,
2827 .write = proc_gid_map_write,
2829 .llseek = seq_lseek,
2830 .release = proc_id_map_release,
2833 static const struct file_operations proc_projid_map_operations = {
2834 .open = proc_projid_map_open,
2835 .write = proc_projid_map_write,
2837 .llseek = seq_lseek,
2838 .release = proc_id_map_release,
2841 static int proc_setgroups_open(struct inode *inode, struct file *file)
2843 struct user_namespace *ns = NULL;
2844 struct task_struct *task;
2848 task = get_proc_task(inode);
2851 ns = get_user_ns(task_cred_xxx(task, user_ns));
2853 put_task_struct(task);
2858 if (file->f_mode & FMODE_WRITE) {
2860 if (!ns_capable(ns, CAP_SYS_ADMIN))
2864 ret = single_open(file, &proc_setgroups_show, ns);
2875 static int proc_setgroups_release(struct inode *inode, struct file *file)
2877 struct seq_file *seq = file->private_data;
2878 struct user_namespace *ns = seq->private;
2879 int ret = single_release(inode, file);
2884 static const struct file_operations proc_setgroups_operations = {
2885 .open = proc_setgroups_open,
2886 .write = proc_setgroups_write,
2888 .llseek = seq_lseek,
2889 .release = proc_setgroups_release,
2891 #endif /* CONFIG_USER_NS */
2893 static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2894 struct pid *pid, struct task_struct *task)
2896 int err = lock_trace(task);
2898 seq_printf(m, "%08x\n", task->personality);
2904 #ifdef CONFIG_LIVEPATCH
2905 static int proc_pid_patch_state(struct seq_file *m, struct pid_namespace *ns,
2906 struct pid *pid, struct task_struct *task)
2908 seq_printf(m, "%d\n", task->patch_state);
2911 #endif /* CONFIG_LIVEPATCH */
2916 static const struct file_operations proc_task_operations;
2917 static const struct inode_operations proc_task_inode_operations;
2919 static const struct pid_entry tgid_base_stuff[] = {
2920 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2921 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2922 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
2923 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2924 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
2926 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2928 REG("environ", S_IRUSR, proc_environ_operations),
2929 REG("auxv", S_IRUSR, proc_auxv_operations),
2930 ONE("status", S_IRUGO, proc_pid_status),
2931 ONE("personality", S_IRUSR, proc_pid_personality),
2932 ONE("limits", S_IRUGO, proc_pid_limits),
2933 #ifdef CONFIG_SCHED_DEBUG
2934 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2936 #ifdef CONFIG_SCHED_AUTOGROUP
2937 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2939 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2940 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2941 ONE("syscall", S_IRUSR, proc_pid_syscall),
2943 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
2944 ONE("stat", S_IRUGO, proc_tgid_stat),
2945 ONE("statm", S_IRUGO, proc_pid_statm),
2946 REG("maps", S_IRUGO, proc_pid_maps_operations),
2948 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
2950 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2951 LNK("cwd", proc_cwd_link),
2952 LNK("root", proc_root_link),
2953 LNK("exe", proc_exe_link),
2954 REG("mounts", S_IRUGO, proc_mounts_operations),
2955 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2956 REG("mountstats", S_IRUSR, proc_mountstats_operations),
2957 #ifdef CONFIG_PROC_PAGE_MONITOR
2958 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2959 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
2960 REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
2961 REG("pagemap", S_IRUSR, proc_pagemap_operations),
2963 #ifdef CONFIG_SECURITY
2964 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
2966 #ifdef CONFIG_KALLSYMS
2967 ONE("wchan", S_IRUGO, proc_pid_wchan),
2969 #ifdef CONFIG_STACKTRACE
2970 ONE("stack", S_IRUSR, proc_pid_stack),
2972 #ifdef CONFIG_SCHED_INFO
2973 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
2975 #ifdef CONFIG_LATENCYTOP
2976 REG("latency", S_IRUGO, proc_lstats_operations),
2978 #ifdef CONFIG_PROC_PID_CPUSET
2979 ONE("cpuset", S_IRUGO, proc_cpuset_show),
2981 #ifdef CONFIG_CGROUPS
2982 ONE("cgroup", S_IRUGO, proc_cgroup_show),
2984 ONE("oom_score", S_IRUGO, proc_oom_score),
2985 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
2986 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
2987 #ifdef CONFIG_AUDITSYSCALL
2988 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2989 REG("sessionid", S_IRUGO, proc_sessionid_operations),
2991 #ifdef CONFIG_FAULT_INJECTION
2992 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2993 REG("fail-nth", 0644, proc_fail_nth_operations),
2995 #ifdef CONFIG_ELF_CORE
2996 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
2998 #ifdef CONFIG_TASK_IO_ACCOUNTING
2999 ONE("io", S_IRUSR, proc_tgid_io_accounting),
3001 #ifdef CONFIG_HARDWALL
3002 ONE("hardwall", S_IRUGO, proc_pid_hardwall),
3004 #ifdef CONFIG_USER_NS
3005 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3006 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
3007 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
3008 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
3010 #if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
3011 REG("timers", S_IRUGO, proc_timers_operations),
3013 REG("timerslack_ns", S_IRUGO|S_IWUGO, proc_pid_set_timerslack_ns_operations),
3014 #ifdef CONFIG_LIVEPATCH
3015 ONE("patch_state", S_IRUSR, proc_pid_patch_state),
3019 static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
3021 return proc_pident_readdir(file, ctx,
3022 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
3025 static const struct file_operations proc_tgid_base_operations = {
3026 .read = generic_read_dir,
3027 .iterate_shared = proc_tgid_base_readdir,
3028 .llseek = generic_file_llseek,
3031 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3033 return proc_pident_lookup(dir, dentry,
3034 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
3037 static const struct inode_operations proc_tgid_base_inode_operations = {
3038 .lookup = proc_tgid_base_lookup,
3039 .getattr = pid_getattr,
3040 .setattr = proc_setattr,
3041 .permission = proc_pid_permission,
3044 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
3046 struct dentry *dentry, *leader, *dir;
3051 name.len = snprintf(buf, sizeof(buf), "%u", pid);
3052 /* no ->d_hash() rejects on procfs */
3053 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
3055 d_invalidate(dentry);
3063 name.len = snprintf(buf, sizeof(buf), "%u", tgid);
3064 leader = d_hash_and_lookup(mnt->mnt_root, &name);
3069 name.len = strlen(name.name);
3070 dir = d_hash_and_lookup(leader, &name);
3072 goto out_put_leader;
3075 name.len = snprintf(buf, sizeof(buf), "%u", pid);
3076 dentry = d_hash_and_lookup(dir, &name);
3078 d_invalidate(dentry);
3090 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
3091 * @task: task that should be flushed.
3093 * When flushing dentries from proc, one needs to flush them from global
3094 * proc (proc_mnt) and from all the namespaces' procs this task was seen
3095 * in. This call is supposed to do all of this job.
3097 * Looks in the dcache for
3099 * /proc/@tgid/task/@pid
3100 * if either directory is present flushes it and all of it'ts children
3103 * It is safe and reasonable to cache /proc entries for a task until
3104 * that task exits. After that they just clog up the dcache with
3105 * useless entries, possibly causing useful dcache entries to be
3106 * flushed instead. This routine is proved to flush those useless
3107 * dcache entries at process exit time.
3109 * NOTE: This routine is just an optimization so it does not guarantee
3110 * that no dcache entries will exist at process exit time it
3111 * just makes it very unlikely that any will persist.
3114 void proc_flush_task(struct task_struct *task)
3117 struct pid *pid, *tgid;
3120 pid = task_pid(task);
3121 tgid = task_tgid(task);
3123 for (i = 0; i <= pid->level; i++) {
3124 upid = &pid->numbers[i];
3125 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
3126 tgid->numbers[i].nr);
3130 static int proc_pid_instantiate(struct inode *dir,
3131 struct dentry * dentry,
3132 struct task_struct *task, const void *ptr)
3134 struct inode *inode;
3136 inode = proc_pid_make_inode(dir->i_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
3140 inode->i_op = &proc_tgid_base_inode_operations;
3141 inode->i_fop = &proc_tgid_base_operations;
3142 inode->i_flags|=S_IMMUTABLE;
3144 set_nlink(inode, nlink_tgid);
3146 d_set_d_op(dentry, &pid_dentry_operations);
3148 d_add(dentry, inode);
3149 /* Close the race of the process dying before we return the dentry */
3150 if (pid_revalidate(dentry, 0))
3156 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
3158 int result = -ENOENT;
3159 struct task_struct *task;
3161 struct pid_namespace *ns;
3163 tgid = name_to_int(&dentry->d_name);
3167 ns = dentry->d_sb->s_fs_info;
3169 task = find_task_by_pid_ns(tgid, ns);
3171 get_task_struct(task);
3176 result = proc_pid_instantiate(dir, dentry, task, NULL);
3177 put_task_struct(task);
3179 return ERR_PTR(result);
3183 * Find the first task with tgid >= tgid
3188 struct task_struct *task;
3190 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3195 put_task_struct(iter.task);
3199 pid = find_ge_pid(iter.tgid, ns);
3201 iter.tgid = pid_nr_ns(pid, ns);
3202 iter.task = pid_task(pid, PIDTYPE_PID);
3203 /* What we to know is if the pid we have find is the
3204 * pid of a thread_group_leader. Testing for task
3205 * being a thread_group_leader is the obvious thing
3206 * todo but there is a window when it fails, due to
3207 * the pid transfer logic in de_thread.
3209 * So we perform the straight forward test of seeing
3210 * if the pid we have found is the pid of a thread
3211 * group leader, and don't worry if the task we have
3212 * found doesn't happen to be a thread group leader.
3213 * As we don't care in the case of readdir.
3215 if (!iter.task || !has_group_leader_pid(iter.task)) {
3219 get_task_struct(iter.task);
3225 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
3227 /* for the /proc/ directory itself, after non-process stuff has been done */
3228 int proc_pid_readdir(struct file *file, struct dir_context *ctx)
3230 struct tgid_iter iter;
3231 struct pid_namespace *ns = file_inode(file)->i_sb->s_fs_info;
3232 loff_t pos = ctx->pos;
3234 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
3237 if (pos == TGID_OFFSET - 2) {
3238 struct inode *inode = d_inode(ns->proc_self);
3239 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
3241 ctx->pos = pos = pos + 1;
3243 if (pos == TGID_OFFSET - 1) {
3244 struct inode *inode = d_inode(ns->proc_thread_self);
3245 if (!dir_emit(ctx, "thread-self", 11, inode->i_ino, DT_LNK))
3247 ctx->pos = pos = pos + 1;
3249 iter.tgid = pos - TGID_OFFSET;
3251 for (iter = next_tgid(ns, iter);
3253 iter.tgid += 1, iter = next_tgid(ns, iter)) {
3258 if (!has_pid_permissions(ns, iter.task, HIDEPID_INVISIBLE))
3261 len = snprintf(name, sizeof(name), "%u", iter.tgid);
3262 ctx->pos = iter.tgid + TGID_OFFSET;
3263 if (!proc_fill_cache(file, ctx, name, len,
3264 proc_pid_instantiate, iter.task, NULL)) {
3265 put_task_struct(iter.task);
3269 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
3274 * proc_tid_comm_permission is a special permission function exclusively
3275 * used for the node /proc/<pid>/task/<tid>/comm.
3276 * It bypasses generic permission checks in the case where a task of the same
3277 * task group attempts to access the node.
3278 * The rationale behind this is that glibc and bionic access this node for
3279 * cross thread naming (pthread_set/getname_np(!self)). However, if
3280 * PR_SET_DUMPABLE gets set to 0 this node among others becomes uid=0 gid=0,
3281 * which locks out the cross thread naming implementation.
3282 * This function makes sure that the node is always accessible for members of
3283 * same thread group.
3285 static int proc_tid_comm_permission(struct inode *inode, int mask)
3287 bool is_same_tgroup;
3288 struct task_struct *task;
3290 task = get_proc_task(inode);
3293 is_same_tgroup = same_thread_group(current, task);
3294 put_task_struct(task);
3296 if (likely(is_same_tgroup && !(mask & MAY_EXEC))) {
3297 /* This file (/proc/<pid>/task/<tid>/comm) can always be
3298 * read or written by the members of the corresponding
3304 return generic_permission(inode, mask);
3307 static const struct inode_operations proc_tid_comm_inode_operations = {
3308 .permission = proc_tid_comm_permission,
3314 static const struct pid_entry tid_base_stuff[] = {
3315 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
3316 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
3317 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
3319 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
3321 REG("environ", S_IRUSR, proc_environ_operations),
3322 REG("auxv", S_IRUSR, proc_auxv_operations),
3323 ONE("status", S_IRUGO, proc_pid_status),
3324 ONE("personality", S_IRUSR, proc_pid_personality),
3325 ONE("limits", S_IRUGO, proc_pid_limits),
3326 #ifdef CONFIG_SCHED_DEBUG
3327 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
3329 NOD("comm", S_IFREG|S_IRUGO|S_IWUSR,
3330 &proc_tid_comm_inode_operations,
3331 &proc_pid_set_comm_operations, {}),
3332 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3333 ONE("syscall", S_IRUSR, proc_pid_syscall),
3335 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
3336 ONE("stat", S_IRUGO, proc_tid_stat),
3337 ONE("statm", S_IRUGO, proc_pid_statm),
3338 REG("maps", S_IRUGO, proc_tid_maps_operations),
3339 #ifdef CONFIG_PROC_CHILDREN
3340 REG("children", S_IRUGO, proc_tid_children_operations),
3343 REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
3345 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3346 LNK("cwd", proc_cwd_link),
3347 LNK("root", proc_root_link),
3348 LNK("exe", proc_exe_link),
3349 REG("mounts", S_IRUGO, proc_mounts_operations),
3350 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
3351 #ifdef CONFIG_PROC_PAGE_MONITOR
3352 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3353 REG("smaps", S_IRUGO, proc_tid_smaps_operations),
3354 REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
3355 REG("pagemap", S_IRUSR, proc_pagemap_operations),
3357 #ifdef CONFIG_SECURITY
3358 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
3360 #ifdef CONFIG_KALLSYMS
3361 ONE("wchan", S_IRUGO, proc_pid_wchan),
3363 #ifdef CONFIG_STACKTRACE
3364 ONE("stack", S_IRUSR, proc_pid_stack),
3366 #ifdef CONFIG_SCHED_INFO
3367 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
3369 #ifdef CONFIG_LATENCYTOP
3370 REG("latency", S_IRUGO, proc_lstats_operations),
3372 #ifdef CONFIG_PROC_PID_CPUSET
3373 ONE("cpuset", S_IRUGO, proc_cpuset_show),
3375 #ifdef CONFIG_CGROUPS
3376 ONE("cgroup", S_IRUGO, proc_cgroup_show),
3378 ONE("oom_score", S_IRUGO, proc_oom_score),
3379 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
3380 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
3381 #ifdef CONFIG_AUDITSYSCALL
3382 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3383 REG("sessionid", S_IRUGO, proc_sessionid_operations),
3385 #ifdef CONFIG_FAULT_INJECTION
3386 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3387 REG("fail-nth", 0644, proc_fail_nth_operations),
3389 #ifdef CONFIG_TASK_IO_ACCOUNTING
3390 ONE("io", S_IRUSR, proc_tid_io_accounting),
3392 #ifdef CONFIG_HARDWALL
3393 ONE("hardwall", S_IRUGO, proc_pid_hardwall),
3395 #ifdef CONFIG_USER_NS
3396 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3397 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
3398 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
3399 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
3401 #ifdef CONFIG_LIVEPATCH
3402 ONE("patch_state", S_IRUSR, proc_pid_patch_state),
3406 static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
3408 return proc_pident_readdir(file, ctx,
3409 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3412 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3414 return proc_pident_lookup(dir, dentry,
3415 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3418 static const struct file_operations proc_tid_base_operations = {
3419 .read = generic_read_dir,
3420 .iterate_shared = proc_tid_base_readdir,
3421 .llseek = generic_file_llseek,
3424 static const struct inode_operations proc_tid_base_inode_operations = {
3425 .lookup = proc_tid_base_lookup,
3426 .getattr = pid_getattr,
3427 .setattr = proc_setattr,
3430 static int proc_task_instantiate(struct inode *dir,
3431 struct dentry *dentry, struct task_struct *task, const void *ptr)
3433 struct inode *inode;
3434 inode = proc_pid_make_inode(dir->i_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
3438 inode->i_op = &proc_tid_base_inode_operations;
3439 inode->i_fop = &proc_tid_base_operations;
3440 inode->i_flags|=S_IMMUTABLE;
3442 set_nlink(inode, nlink_tid);
3444 d_set_d_op(dentry, &pid_dentry_operations);
3446 d_add(dentry, inode);
3447 /* Close the race of the process dying before we return the dentry */
3448 if (pid_revalidate(dentry, 0))
3454 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
3456 int result = -ENOENT;
3457 struct task_struct *task;
3458 struct task_struct *leader = get_proc_task(dir);
3460 struct pid_namespace *ns;
3465 tid = name_to_int(&dentry->d_name);
3469 ns = dentry->d_sb->s_fs_info;
3471 task = find_task_by_pid_ns(tid, ns);
3473 get_task_struct(task);
3477 if (!same_thread_group(leader, task))
3480 result = proc_task_instantiate(dir, dentry, task, NULL);
3482 put_task_struct(task);
3484 put_task_struct(leader);
3486 return ERR_PTR(result);
3490 * Find the first tid of a thread group to return to user space.
3492 * Usually this is just the thread group leader, but if the users
3493 * buffer was too small or there was a seek into the middle of the
3494 * directory we have more work todo.
3496 * In the case of a short read we start with find_task_by_pid.
3498 * In the case of a seek we start with the leader and walk nr
3501 static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
3502 struct pid_namespace *ns)
3504 struct task_struct *pos, *task;
3505 unsigned long nr = f_pos;
3507 if (nr != f_pos) /* 32bit overflow? */
3511 task = pid_task(pid, PIDTYPE_PID);
3515 /* Attempt to start with the tid of a thread */
3517 pos = find_task_by_pid_ns(tid, ns);
3518 if (pos && same_thread_group(pos, task))
3522 /* If nr exceeds the number of threads there is nothing todo */
3523 if (nr >= get_nr_threads(task))
3526 /* If we haven't found our starting place yet start
3527 * with the leader and walk nr threads forward.
3529 pos = task = task->group_leader;
3533 } while_each_thread(task, pos);
3538 get_task_struct(pos);
3545 * Find the next thread in the thread list.
3546 * Return NULL if there is an error or no next thread.
3548 * The reference to the input task_struct is released.
3550 static struct task_struct *next_tid(struct task_struct *start)
3552 struct task_struct *pos = NULL;
3554 if (pid_alive(start)) {
3555 pos = next_thread(start);
3556 if (thread_group_leader(pos))
3559 get_task_struct(pos);
3562 put_task_struct(start);
3566 /* for the /proc/TGID/task/ directories */
3567 static int proc_task_readdir(struct file *file, struct dir_context *ctx)
3569 struct inode *inode = file_inode(file);
3570 struct task_struct *task;
3571 struct pid_namespace *ns;
3574 if (proc_inode_is_dead(inode))
3577 if (!dir_emit_dots(file, ctx))
3580 /* f_version caches the tgid value that the last readdir call couldn't
3581 * return. lseek aka telldir automagically resets f_version to 0.
3583 ns = inode->i_sb->s_fs_info;
3584 tid = (int)file->f_version;
3585 file->f_version = 0;
3586 for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
3588 task = next_tid(task), ctx->pos++) {
3591 tid = task_pid_nr_ns(task, ns);
3592 len = snprintf(name, sizeof(name), "%u", tid);
3593 if (!proc_fill_cache(file, ctx, name, len,
3594 proc_task_instantiate, task, NULL)) {
3595 /* returning this tgid failed, save it as the first
3596 * pid for the next readir call */
3597 file->f_version = (u64)tid;
3598 put_task_struct(task);
3606 static int proc_task_getattr(const struct path *path, struct kstat *stat,
3607 u32 request_mask, unsigned int query_flags)
3609 struct inode *inode = d_inode(path->dentry);
3610 struct task_struct *p = get_proc_task(inode);
3611 generic_fillattr(inode, stat);
3614 stat->nlink += get_nr_threads(p);
3621 static const struct inode_operations proc_task_inode_operations = {
3622 .lookup = proc_task_lookup,
3623 .getattr = proc_task_getattr,
3624 .setattr = proc_setattr,
3625 .permission = proc_pid_permission,
3628 static const struct file_operations proc_task_operations = {
3629 .read = generic_read_dir,
3630 .iterate_shared = proc_task_readdir,
3631 .llseek = generic_file_llseek,
3634 void __init set_proc_pid_nlink(void)
3636 nlink_tid = pid_entry_nlink(tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3637 nlink_tgid = pid_entry_nlink(tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));