4 * Copyright (C) 1991, 1992 Linus Torvalds
6 * proc base directory handling functions
8 * 1999, Al Viro. Rewritten. Now it covers the whole per-process part.
9 * Instead of using magical inumbers to determine the kind of object
10 * we allocate and fill in-core inodes upon lookup. They don't even
11 * go into icache. We cache the reference to task_struct upon lookup too.
12 * Eventually it should become a filesystem in its own. We don't use the
13 * rest of procfs anymore.
19 * Bruna Moreira <bruna.moreira@indt.org.br>
20 * Edjard Mota <edjard.mota@indt.org.br>
21 * Ilias Biris <ilias.biris@indt.org.br>
22 * Mauricio Lin <mauricio.lin@indt.org.br>
24 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
26 * A new process specific entry (smaps) included in /proc. It shows the
27 * size of rss for each memory area. The maps entry lacks information
28 * about physical memory size (rss) for each mapped file, i.e.,
29 * rss information for executables and library files.
30 * This additional information is useful for any tools that need to know
31 * about physical memory consumption for a process specific library.
35 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36 * Pud inclusion in the page table walking.
40 * 10LE Instituto Nokia de Tecnologia - INdT:
41 * A better way to walks through the page table as suggested by Hugh Dickins.
43 * Simo Piiroinen <simo.piiroinen@nokia.com>:
44 * Smaps information related to shared, private, clean and dirty pages.
46 * Paul Mundt <paul.mundt@nokia.com>:
47 * Overall revision about smaps.
50 #include <asm/uaccess.h>
52 #include <linux/errno.h>
53 #include <linux/time.h>
54 #include <linux/proc_fs.h>
55 #include <linux/stat.h>
56 #include <linux/task_io_accounting_ops.h>
57 #include <linux/init.h>
58 #include <linux/capability.h>
59 #include <linux/file.h>
60 #include <linux/fdtable.h>
61 #include <linux/string.h>
62 #include <linux/seq_file.h>
63 #include <linux/namei.h>
64 #include <linux/mnt_namespace.h>
66 #include <linux/swap.h>
67 #include <linux/rcupdate.h>
68 #include <linux/kallsyms.h>
69 #include <linux/stacktrace.h>
70 #include <linux/resource.h>
71 #include <linux/module.h>
72 #include <linux/mount.h>
73 #include <linux/security.h>
74 #include <linux/ptrace.h>
75 #include <linux/tracehook.h>
76 #include <linux/printk.h>
77 #include <linux/cgroup.h>
78 #include <linux/cpuset.h>
79 #include <linux/audit.h>
80 #include <linux/poll.h>
81 #include <linux/nsproxy.h>
82 #include <linux/oom.h>
83 #include <linux/elf.h>
84 #include <linux/pid_namespace.h>
85 #include <linux/user_namespace.h>
86 #include <linux/fs_struct.h>
87 #include <linux/slab.h>
88 #include <linux/flex_array.h>
89 #include <linux/posix-timers.h>
90 #ifdef CONFIG_HARDWALL
91 #include <asm/hardwall.h>
93 #include <trace/events/oom.h>
97 #include "../../lib/kstrtox.h"
100 * Implementing inode permission operations in /proc is almost
101 * certainly an error. Permission checks need to happen during
102 * each system call not at open time. The reason is that most of
103 * what we wish to check for permissions in /proc varies at runtime.
105 * The classic example of a problem is opening file descriptors
106 * in /proc for a task before it execs a suid executable.
113 const struct inode_operations *iop;
114 const struct file_operations *fop;
118 #define NOD(NAME, MODE, IOP, FOP, OP) { \
120 .len = sizeof(NAME) - 1, \
127 #define DIR(NAME, MODE, iops, fops) \
128 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
129 #define LNK(NAME, get_link) \
130 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
131 &proc_pid_link_inode_operations, NULL, \
132 { .proc_get_link = get_link } )
133 #define REG(NAME, MODE, fops) \
134 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
135 #define ONE(NAME, MODE, show) \
136 NOD(NAME, (S_IFREG|(MODE)), \
137 NULL, &proc_single_file_operations, \
138 { .proc_show = show } )
141 * Count the number of hardlinks for the pid_entry table, excluding the .
144 static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
151 for (i = 0; i < n; ++i) {
152 if (S_ISDIR(entries[i].mode))
159 static int get_task_root(struct task_struct *task, struct path *root)
161 int result = -ENOENT;
165 get_fs_root(task->fs, root);
172 static int proc_cwd_link(struct dentry *dentry, struct path *path)
174 struct task_struct *task = get_proc_task(d_inode(dentry));
175 int result = -ENOENT;
180 get_fs_pwd(task->fs, path);
184 put_task_struct(task);
189 static int proc_root_link(struct dentry *dentry, struct path *path)
191 struct task_struct *task = get_proc_task(d_inode(dentry));
192 int result = -ENOENT;
195 result = get_task_root(task, path);
196 put_task_struct(task);
201 static ssize_t proc_pid_cmdline_read(struct file *file, char __user *buf,
202 size_t _count, loff_t *pos)
204 struct task_struct *tsk;
205 struct mm_struct *mm;
207 unsigned long count = _count;
208 unsigned long arg_start, arg_end, env_start, env_end;
209 unsigned long len1, len2, len;
216 tsk = get_proc_task(file_inode(file));
219 mm = get_task_mm(tsk);
220 put_task_struct(tsk);
223 /* Check if process spawned far enough to have cmdline. */
229 page = (char *)__get_free_page(GFP_TEMPORARY);
235 down_read(&mm->mmap_sem);
236 arg_start = mm->arg_start;
237 arg_end = mm->arg_end;
238 env_start = mm->env_start;
239 env_end = mm->env_end;
240 up_read(&mm->mmap_sem);
242 BUG_ON(arg_start > arg_end);
243 BUG_ON(env_start > env_end);
245 len1 = arg_end - arg_start;
246 len2 = env_end - env_start;
254 * Inherently racy -- command line shares address space
255 * with code and data.
257 rv = access_remote_vm(mm, arg_end - 1, &c, 1, FOLL_ANON);
264 /* Command line (set of strings) occupies whole ARGV. */
268 p = arg_start + *pos;
270 while (count > 0 && len > 0) {
274 _count = min3(count, len, PAGE_SIZE);
275 nr_read = access_remote_vm(mm, p, page, _count, FOLL_ANON);
281 if (copy_to_user(buf, page, nr_read)) {
294 * Command line (1 string) occupies ARGV and maybe
297 if (len1 + len2 <= *pos)
302 p = arg_start + *pos;
304 while (count > 0 && len > 0) {
305 unsigned int _count, l;
309 _count = min3(count, len, PAGE_SIZE);
310 nr_read = access_remote_vm(mm, p, page, _count, FOLL_ANON);
317 * Command line can be shorter than whole ARGV
318 * even if last "marker" byte says it is not.
321 l = strnlen(page, nr_read);
327 if (copy_to_user(buf, page, nr_read)) {
343 * Command line (1 string) occupies ARGV and
347 p = env_start + *pos - len1;
348 len = len1 + len2 - *pos;
353 while (count > 0 && len > 0) {
354 unsigned int _count, l;
358 _count = min3(count, len, PAGE_SIZE);
359 nr_read = access_remote_vm(mm, p, page, _count, FOLL_ANON);
367 l = strnlen(page, nr_read);
373 if (copy_to_user(buf, page, nr_read)) {
392 free_page((unsigned long)page);
400 static const struct file_operations proc_pid_cmdline_ops = {
401 .read = proc_pid_cmdline_read,
402 .llseek = generic_file_llseek,
405 #ifdef CONFIG_KALLSYMS
407 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
408 * Returns the resolved symbol. If that fails, simply return the address.
410 static int proc_pid_wchan(struct seq_file *m, struct pid_namespace *ns,
411 struct pid *pid, struct task_struct *task)
414 char symname[KSYM_NAME_LEN];
416 wchan = get_wchan(task);
418 if (wchan && ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)
419 && !lookup_symbol_name(wchan, symname))
420 seq_printf(m, "%s", symname);
426 #endif /* CONFIG_KALLSYMS */
428 static int lock_trace(struct task_struct *task)
430 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
433 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH_FSCREDS)) {
434 mutex_unlock(&task->signal->cred_guard_mutex);
440 static void unlock_trace(struct task_struct *task)
442 mutex_unlock(&task->signal->cred_guard_mutex);
445 #ifdef CONFIG_STACKTRACE
447 #define MAX_STACK_TRACE_DEPTH 64
449 static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
450 struct pid *pid, struct task_struct *task)
452 struct stack_trace trace;
453 unsigned long *entries;
458 * The ability to racily run the kernel stack unwinder on a running task
459 * and then observe the unwinder output is scary; while it is useful for
460 * debugging kernel issues, it can also allow an attacker to leak kernel
462 * Doing this in a manner that is at least safe from races would require
463 * some work to ensure that the remote task can not be scheduled; and
464 * even then, this would still expose the unwinder as local attack
466 * Therefore, this interface is restricted to root.
468 if (!file_ns_capable(m->file, &init_user_ns, CAP_SYS_ADMIN))
471 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
475 trace.nr_entries = 0;
476 trace.max_entries = MAX_STACK_TRACE_DEPTH;
477 trace.entries = entries;
480 err = lock_trace(task);
482 save_stack_trace_tsk(task, &trace);
484 for (i = 0; i < trace.nr_entries; i++) {
485 seq_printf(m, "[<%pK>] %pB\n",
486 (void *)entries[i], (void *)entries[i]);
496 #ifdef CONFIG_SCHED_INFO
498 * Provides /proc/PID/schedstat
500 static int proc_pid_schedstat(struct seq_file *m, struct pid_namespace *ns,
501 struct pid *pid, struct task_struct *task)
503 if (unlikely(!sched_info_on()))
504 seq_printf(m, "0 0 0\n");
506 seq_printf(m, "%llu %llu %lu\n",
507 (unsigned long long)task->se.sum_exec_runtime,
508 (unsigned long long)task->sched_info.run_delay,
509 task->sched_info.pcount);
515 #ifdef CONFIG_LATENCYTOP
516 static int lstats_show_proc(struct seq_file *m, void *v)
519 struct inode *inode = m->private;
520 struct task_struct *task = get_proc_task(inode);
524 seq_puts(m, "Latency Top version : v0.1\n");
525 for (i = 0; i < 32; i++) {
526 struct latency_record *lr = &task->latency_record[i];
527 if (lr->backtrace[0]) {
529 seq_printf(m, "%i %li %li",
530 lr->count, lr->time, lr->max);
531 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
532 unsigned long bt = lr->backtrace[q];
537 seq_printf(m, " %ps", (void *)bt);
543 put_task_struct(task);
547 static int lstats_open(struct inode *inode, struct file *file)
549 return single_open(file, lstats_show_proc, inode);
552 static ssize_t lstats_write(struct file *file, const char __user *buf,
553 size_t count, loff_t *offs)
555 struct task_struct *task = get_proc_task(file_inode(file));
559 clear_all_latency_tracing(task);
560 put_task_struct(task);
565 static const struct file_operations proc_lstats_operations = {
568 .write = lstats_write,
570 .release = single_release,
575 static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns,
576 struct pid *pid, struct task_struct *task)
578 unsigned long totalpages = totalram_pages + total_swap_pages;
579 unsigned long points = 0;
581 points = oom_badness(task, NULL, NULL, totalpages) *
583 seq_printf(m, "%lu\n", points);
593 static const struct limit_names lnames[RLIM_NLIMITS] = {
594 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
595 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
596 [RLIMIT_DATA] = {"Max data size", "bytes"},
597 [RLIMIT_STACK] = {"Max stack size", "bytes"},
598 [RLIMIT_CORE] = {"Max core file size", "bytes"},
599 [RLIMIT_RSS] = {"Max resident set", "bytes"},
600 [RLIMIT_NPROC] = {"Max processes", "processes"},
601 [RLIMIT_NOFILE] = {"Max open files", "files"},
602 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
603 [RLIMIT_AS] = {"Max address space", "bytes"},
604 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
605 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
606 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
607 [RLIMIT_NICE] = {"Max nice priority", NULL},
608 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
609 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
612 /* Display limits for a process */
613 static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
614 struct pid *pid, struct task_struct *task)
619 struct rlimit rlim[RLIM_NLIMITS];
621 if (!lock_task_sighand(task, &flags))
623 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
624 unlock_task_sighand(task, &flags);
627 * print the file header
629 seq_printf(m, "%-25s %-20s %-20s %-10s\n",
630 "Limit", "Soft Limit", "Hard Limit", "Units");
632 for (i = 0; i < RLIM_NLIMITS; i++) {
633 if (rlim[i].rlim_cur == RLIM_INFINITY)
634 seq_printf(m, "%-25s %-20s ",
635 lnames[i].name, "unlimited");
637 seq_printf(m, "%-25s %-20lu ",
638 lnames[i].name, rlim[i].rlim_cur);
640 if (rlim[i].rlim_max == RLIM_INFINITY)
641 seq_printf(m, "%-20s ", "unlimited");
643 seq_printf(m, "%-20lu ", rlim[i].rlim_max);
646 seq_printf(m, "%-10s\n", lnames[i].unit);
654 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
655 static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
656 struct pid *pid, struct task_struct *task)
659 unsigned long args[6], sp, pc;
662 res = lock_trace(task);
666 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
667 seq_puts(m, "running\n");
669 seq_printf(m, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
672 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
674 args[0], args[1], args[2], args[3], args[4], args[5],
680 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
682 /************************************************************************/
683 /* Here the fs part begins */
684 /************************************************************************/
686 /* permission checks */
687 static int proc_fd_access_allowed(struct inode *inode)
689 struct task_struct *task;
691 /* Allow access to a task's file descriptors if it is us or we
692 * may use ptrace attach to the process and find out that
695 task = get_proc_task(inode);
697 allowed = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
698 put_task_struct(task);
703 int proc_setattr(struct dentry *dentry, struct iattr *attr)
706 struct inode *inode = d_inode(dentry);
708 if (attr->ia_valid & ATTR_MODE)
711 error = setattr_prepare(dentry, attr);
715 setattr_copy(inode, attr);
716 mark_inode_dirty(inode);
721 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
722 * or euid/egid (for hide_pid_min=2)?
724 static bool has_pid_permissions(struct pid_namespace *pid,
725 struct task_struct *task,
728 if (pid->hide_pid < hide_pid_min)
730 if (in_group_p(pid->pid_gid))
732 return ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
736 static int proc_pid_permission(struct inode *inode, int mask)
738 struct pid_namespace *pid = inode->i_sb->s_fs_info;
739 struct task_struct *task;
742 task = get_proc_task(inode);
745 has_perms = has_pid_permissions(pid, task, 1);
746 put_task_struct(task);
749 if (pid->hide_pid == 2) {
751 * Let's make getdents(), stat(), and open()
752 * consistent with each other. If a process
753 * may not stat() a file, it shouldn't be seen
761 return generic_permission(inode, mask);
766 static const struct inode_operations proc_def_inode_operations = {
767 .setattr = proc_setattr,
770 static int proc_single_show(struct seq_file *m, void *v)
772 struct inode *inode = m->private;
773 struct pid_namespace *ns;
775 struct task_struct *task;
778 ns = inode->i_sb->s_fs_info;
779 pid = proc_pid(inode);
780 task = get_pid_task(pid, PIDTYPE_PID);
784 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
786 put_task_struct(task);
790 static int proc_single_open(struct inode *inode, struct file *filp)
792 return single_open(filp, proc_single_show, inode);
795 static const struct file_operations proc_single_file_operations = {
796 .open = proc_single_open,
799 .release = single_release,
803 struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode)
805 struct task_struct *task = get_proc_task(inode);
806 struct mm_struct *mm = ERR_PTR(-ESRCH);
809 mm = mm_access(task, mode | PTRACE_MODE_FSCREDS);
810 put_task_struct(task);
812 if (!IS_ERR_OR_NULL(mm)) {
813 /* ensure this mm_struct can't be freed */
814 atomic_inc(&mm->mm_count);
815 /* but do not pin its memory */
823 static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
825 struct mm_struct *mm = proc_mem_open(inode, mode);
830 file->private_data = mm;
834 static int mem_open(struct inode *inode, struct file *file)
836 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
838 /* OK to pass negative loff_t, we can catch out-of-range */
839 file->f_mode |= FMODE_UNSIGNED_OFFSET;
844 static ssize_t mem_rw(struct file *file, char __user *buf,
845 size_t count, loff_t *ppos, int write)
847 struct mm_struct *mm = file->private_data;
848 unsigned long addr = *ppos;
856 page = (char *)__get_free_page(GFP_TEMPORARY);
861 if (!atomic_inc_not_zero(&mm->mm_users))
864 /* Maybe we should limit FOLL_FORCE to actual ptrace users? */
870 size_t this_len = min_t(size_t, count, PAGE_SIZE);
872 if (write && copy_from_user(page, buf, this_len)) {
877 this_len = access_remote_vm(mm, addr, page, this_len, flags);
884 if (!write && copy_to_user(buf, page, this_len)) {
898 free_page((unsigned long) page);
902 static ssize_t mem_read(struct file *file, char __user *buf,
903 size_t count, loff_t *ppos)
905 return mem_rw(file, buf, count, ppos, 0);
908 static ssize_t mem_write(struct file *file, const char __user *buf,
909 size_t count, loff_t *ppos)
911 return mem_rw(file, (char __user*)buf, count, ppos, 1);
914 loff_t mem_lseek(struct file *file, loff_t offset, int orig)
918 file->f_pos = offset;
921 file->f_pos += offset;
926 force_successful_syscall_return();
930 static int mem_release(struct inode *inode, struct file *file)
932 struct mm_struct *mm = file->private_data;
938 static const struct file_operations proc_mem_operations = {
943 .release = mem_release,
946 static int environ_open(struct inode *inode, struct file *file)
948 return __mem_open(inode, file, PTRACE_MODE_READ);
951 static ssize_t environ_read(struct file *file, char __user *buf,
952 size_t count, loff_t *ppos)
955 unsigned long src = *ppos;
957 struct mm_struct *mm = file->private_data;
958 unsigned long env_start, env_end;
960 /* Ensure the process spawned far enough to have an environment. */
961 if (!mm || !mm->env_end)
964 page = (char *)__get_free_page(GFP_TEMPORARY);
969 if (!atomic_inc_not_zero(&mm->mm_users))
972 down_read(&mm->mmap_sem);
973 env_start = mm->env_start;
974 env_end = mm->env_end;
975 up_read(&mm->mmap_sem);
978 size_t this_len, max_len;
981 if (src >= (env_end - env_start))
984 this_len = env_end - (env_start + src);
986 max_len = min_t(size_t, PAGE_SIZE, count);
987 this_len = min(max_len, this_len);
989 retval = access_remote_vm(mm, (env_start + src), page, this_len, FOLL_ANON);
996 if (copy_to_user(buf, page, retval)) {
1010 free_page((unsigned long) page);
1014 static const struct file_operations proc_environ_operations = {
1015 .open = environ_open,
1016 .read = environ_read,
1017 .llseek = generic_file_llseek,
1018 .release = mem_release,
1021 static int auxv_open(struct inode *inode, struct file *file)
1023 return __mem_open(inode, file, PTRACE_MODE_READ_FSCREDS);
1026 static ssize_t auxv_read(struct file *file, char __user *buf,
1027 size_t count, loff_t *ppos)
1029 struct mm_struct *mm = file->private_data;
1030 unsigned int nwords = 0;
1036 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
1037 return simple_read_from_buffer(buf, count, ppos, mm->saved_auxv,
1038 nwords * sizeof(mm->saved_auxv[0]));
1041 static const struct file_operations proc_auxv_operations = {
1044 .llseek = generic_file_llseek,
1045 .release = mem_release,
1048 static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
1051 struct task_struct *task = get_proc_task(file_inode(file));
1052 char buffer[PROC_NUMBUF];
1053 int oom_adj = OOM_ADJUST_MIN;
1058 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
1059 oom_adj = OOM_ADJUST_MAX;
1061 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
1063 put_task_struct(task);
1064 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
1065 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1068 static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
1070 static DEFINE_MUTEX(oom_adj_mutex);
1071 struct mm_struct *mm = NULL;
1072 struct task_struct *task;
1075 task = get_proc_task(file_inode(file));
1079 mutex_lock(&oom_adj_mutex);
1081 if (oom_adj < task->signal->oom_score_adj &&
1082 !capable(CAP_SYS_RESOURCE)) {
1087 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
1088 * /proc/pid/oom_score_adj instead.
1090 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
1091 current->comm, task_pid_nr(current), task_pid_nr(task),
1094 if ((short)oom_adj < task->signal->oom_score_adj_min &&
1095 !capable(CAP_SYS_RESOURCE)) {
1102 * Make sure we will check other processes sharing the mm if this is
1103 * not vfrok which wants its own oom_score_adj.
1104 * pin the mm so it doesn't go away and get reused after task_unlock
1106 if (!task->vfork_done) {
1107 struct task_struct *p = find_lock_task_mm(task);
1110 if (atomic_read(&p->mm->mm_users) > 1) {
1112 atomic_inc(&mm->mm_count);
1118 task->signal->oom_score_adj = oom_adj;
1119 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1120 task->signal->oom_score_adj_min = (short)oom_adj;
1121 trace_oom_score_adj_update(task);
1124 struct task_struct *p;
1127 for_each_process(p) {
1128 if (same_thread_group(task, p))
1131 /* do not touch kernel threads or the global init */
1132 if (p->flags & PF_KTHREAD || is_global_init(p))
1136 if (!p->vfork_done && process_shares_mm(p, mm)) {
1137 p->signal->oom_score_adj = oom_adj;
1138 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1139 p->signal->oom_score_adj_min = (short)oom_adj;
1147 mutex_unlock(&oom_adj_mutex);
1148 put_task_struct(task);
1153 * /proc/pid/oom_adj exists solely for backwards compatibility with previous
1154 * kernels. The effective policy is defined by oom_score_adj, which has a
1155 * different scale: oom_adj grew exponentially and oom_score_adj grows linearly.
1156 * Values written to oom_adj are simply mapped linearly to oom_score_adj.
1157 * Processes that become oom disabled via oom_adj will still be oom disabled
1158 * with this implementation.
1160 * oom_adj cannot be removed since existing userspace binaries use it.
1162 static ssize_t oom_adj_write(struct file *file, const char __user *buf,
1163 size_t count, loff_t *ppos)
1165 char buffer[PROC_NUMBUF];
1169 memset(buffer, 0, sizeof(buffer));
1170 if (count > sizeof(buffer) - 1)
1171 count = sizeof(buffer) - 1;
1172 if (copy_from_user(buffer, buf, count)) {
1177 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
1180 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
1181 oom_adj != OOM_DISABLE) {
1187 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1188 * value is always attainable.
1190 if (oom_adj == OOM_ADJUST_MAX)
1191 oom_adj = OOM_SCORE_ADJ_MAX;
1193 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
1195 err = __set_oom_adj(file, oom_adj, true);
1197 return err < 0 ? err : count;
1200 static const struct file_operations proc_oom_adj_operations = {
1201 .read = oom_adj_read,
1202 .write = oom_adj_write,
1203 .llseek = generic_file_llseek,
1206 static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1207 size_t count, loff_t *ppos)
1209 struct task_struct *task = get_proc_task(file_inode(file));
1210 char buffer[PROC_NUMBUF];
1211 short oom_score_adj = OOM_SCORE_ADJ_MIN;
1216 oom_score_adj = task->signal->oom_score_adj;
1217 put_task_struct(task);
1218 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
1219 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1222 static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1223 size_t count, loff_t *ppos)
1225 char buffer[PROC_NUMBUF];
1229 memset(buffer, 0, sizeof(buffer));
1230 if (count > sizeof(buffer) - 1)
1231 count = sizeof(buffer) - 1;
1232 if (copy_from_user(buffer, buf, count)) {
1237 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
1240 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
1241 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1246 err = __set_oom_adj(file, oom_score_adj, false);
1248 return err < 0 ? err : count;
1251 static const struct file_operations proc_oom_score_adj_operations = {
1252 .read = oom_score_adj_read,
1253 .write = oom_score_adj_write,
1254 .llseek = default_llseek,
1257 #ifdef CONFIG_AUDITSYSCALL
1258 #define TMPBUFLEN 21
1259 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1260 size_t count, loff_t *ppos)
1262 struct inode * inode = file_inode(file);
1263 struct task_struct *task = get_proc_task(inode);
1265 char tmpbuf[TMPBUFLEN];
1269 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1270 from_kuid(file->f_cred->user_ns,
1271 audit_get_loginuid(task)));
1272 put_task_struct(task);
1273 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1276 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1277 size_t count, loff_t *ppos)
1279 struct inode * inode = file_inode(file);
1285 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1292 /* No partial writes. */
1296 rv = kstrtou32_from_user(buf, count, 10, &loginuid);
1300 /* is userspace tring to explicitly UNSET the loginuid? */
1301 if (loginuid == AUDIT_UID_UNSET) {
1302 kloginuid = INVALID_UID;
1304 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
1305 if (!uid_valid(kloginuid))
1309 rv = audit_set_loginuid(kloginuid);
1315 static const struct file_operations proc_loginuid_operations = {
1316 .read = proc_loginuid_read,
1317 .write = proc_loginuid_write,
1318 .llseek = generic_file_llseek,
1321 static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1322 size_t count, loff_t *ppos)
1324 struct inode * inode = file_inode(file);
1325 struct task_struct *task = get_proc_task(inode);
1327 char tmpbuf[TMPBUFLEN];
1331 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1332 audit_get_sessionid(task));
1333 put_task_struct(task);
1334 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1337 static const struct file_operations proc_sessionid_operations = {
1338 .read = proc_sessionid_read,
1339 .llseek = generic_file_llseek,
1343 #ifdef CONFIG_FAULT_INJECTION
1344 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1345 size_t count, loff_t *ppos)
1347 struct task_struct *task = get_proc_task(file_inode(file));
1348 char buffer[PROC_NUMBUF];
1354 make_it_fail = task->make_it_fail;
1355 put_task_struct(task);
1357 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1359 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1362 static ssize_t proc_fault_inject_write(struct file * file,
1363 const char __user * buf, size_t count, loff_t *ppos)
1365 struct task_struct *task;
1366 char buffer[PROC_NUMBUF];
1370 if (!capable(CAP_SYS_RESOURCE))
1372 memset(buffer, 0, sizeof(buffer));
1373 if (count > sizeof(buffer) - 1)
1374 count = sizeof(buffer) - 1;
1375 if (copy_from_user(buffer, buf, count))
1377 rv = kstrtoint(strstrip(buffer), 0, &make_it_fail);
1380 if (make_it_fail < 0 || make_it_fail > 1)
1383 task = get_proc_task(file_inode(file));
1386 task->make_it_fail = make_it_fail;
1387 put_task_struct(task);
1392 static const struct file_operations proc_fault_inject_operations = {
1393 .read = proc_fault_inject_read,
1394 .write = proc_fault_inject_write,
1395 .llseek = generic_file_llseek,
1400 #ifdef CONFIG_SCHED_DEBUG
1402 * Print out various scheduling related per-task fields:
1404 static int sched_show(struct seq_file *m, void *v)
1406 struct inode *inode = m->private;
1407 struct task_struct *p;
1409 p = get_proc_task(inode);
1412 proc_sched_show_task(p, m);
1420 sched_write(struct file *file, const char __user *buf,
1421 size_t count, loff_t *offset)
1423 struct inode *inode = file_inode(file);
1424 struct task_struct *p;
1426 p = get_proc_task(inode);
1429 proc_sched_set_task(p);
1436 static int sched_open(struct inode *inode, struct file *filp)
1438 return single_open(filp, sched_show, inode);
1441 static const struct file_operations proc_pid_sched_operations = {
1444 .write = sched_write,
1445 .llseek = seq_lseek,
1446 .release = single_release,
1451 #ifdef CONFIG_SCHED_AUTOGROUP
1453 * Print out autogroup related information:
1455 static int sched_autogroup_show(struct seq_file *m, void *v)
1457 struct inode *inode = m->private;
1458 struct task_struct *p;
1460 p = get_proc_task(inode);
1463 proc_sched_autogroup_show_task(p, m);
1471 sched_autogroup_write(struct file *file, const char __user *buf,
1472 size_t count, loff_t *offset)
1474 struct inode *inode = file_inode(file);
1475 struct task_struct *p;
1476 char buffer[PROC_NUMBUF];
1480 memset(buffer, 0, sizeof(buffer));
1481 if (count > sizeof(buffer) - 1)
1482 count = sizeof(buffer) - 1;
1483 if (copy_from_user(buffer, buf, count))
1486 err = kstrtoint(strstrip(buffer), 0, &nice);
1490 p = get_proc_task(inode);
1494 err = proc_sched_autogroup_set_nice(p, nice);
1503 static int sched_autogroup_open(struct inode *inode, struct file *filp)
1507 ret = single_open(filp, sched_autogroup_show, NULL);
1509 struct seq_file *m = filp->private_data;
1516 static const struct file_operations proc_pid_sched_autogroup_operations = {
1517 .open = sched_autogroup_open,
1519 .write = sched_autogroup_write,
1520 .llseek = seq_lseek,
1521 .release = single_release,
1524 #endif /* CONFIG_SCHED_AUTOGROUP */
1526 static ssize_t comm_write(struct file *file, const char __user *buf,
1527 size_t count, loff_t *offset)
1529 struct inode *inode = file_inode(file);
1530 struct task_struct *p;
1531 char buffer[TASK_COMM_LEN];
1532 const size_t maxlen = sizeof(buffer) - 1;
1534 memset(buffer, 0, sizeof(buffer));
1535 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
1538 p = get_proc_task(inode);
1542 if (same_thread_group(current, p))
1543 set_task_comm(p, buffer);
1552 static int comm_show(struct seq_file *m, void *v)
1554 struct inode *inode = m->private;
1555 struct task_struct *p;
1557 p = get_proc_task(inode);
1562 seq_printf(m, "%s\n", p->comm);
1570 static int comm_open(struct inode *inode, struct file *filp)
1572 return single_open(filp, comm_show, inode);
1575 static const struct file_operations proc_pid_set_comm_operations = {
1578 .write = comm_write,
1579 .llseek = seq_lseek,
1580 .release = single_release,
1583 static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
1585 struct task_struct *task;
1586 struct file *exe_file;
1588 task = get_proc_task(d_inode(dentry));
1591 exe_file = get_task_exe_file(task);
1592 put_task_struct(task);
1594 *exe_path = exe_file->f_path;
1595 path_get(&exe_file->f_path);
1602 static const char *proc_pid_get_link(struct dentry *dentry,
1603 struct inode *inode,
1604 struct delayed_call *done)
1607 int error = -EACCES;
1610 return ERR_PTR(-ECHILD);
1612 /* Are we allowed to snoop on the tasks file descriptors? */
1613 if (!proc_fd_access_allowed(inode))
1616 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1620 nd_jump_link(&path);
1623 return ERR_PTR(error);
1626 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1628 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
1635 pathname = d_path(path, tmp, PAGE_SIZE);
1636 len = PTR_ERR(pathname);
1637 if (IS_ERR(pathname))
1639 len = tmp + PAGE_SIZE - 1 - pathname;
1643 if (copy_to_user(buffer, pathname, len))
1646 free_page((unsigned long)tmp);
1650 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1652 int error = -EACCES;
1653 struct inode *inode = d_inode(dentry);
1656 /* Are we allowed to snoop on the tasks file descriptors? */
1657 if (!proc_fd_access_allowed(inode))
1660 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1664 error = do_proc_readlink(&path, buffer, buflen);
1670 const struct inode_operations proc_pid_link_inode_operations = {
1671 .readlink = proc_pid_readlink,
1672 .get_link = proc_pid_get_link,
1673 .setattr = proc_setattr,
1677 /* building an inode */
1679 struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1681 struct inode * inode;
1682 struct proc_inode *ei;
1683 const struct cred *cred;
1685 /* We need a new inode */
1687 inode = new_inode(sb);
1693 inode->i_ino = get_next_ino();
1694 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
1695 inode->i_op = &proc_def_inode_operations;
1698 * grab the reference to task.
1700 ei->pid = get_task_pid(task, PIDTYPE_PID);
1704 if (task_dumpable(task)) {
1706 cred = __task_cred(task);
1707 inode->i_uid = cred->euid;
1708 inode->i_gid = cred->egid;
1711 security_task_to_inode(task, inode);
1721 int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1723 struct inode *inode = d_inode(dentry);
1724 struct task_struct *task;
1725 const struct cred *cred;
1726 struct pid_namespace *pid = dentry->d_sb->s_fs_info;
1728 generic_fillattr(inode, stat);
1731 stat->uid = GLOBAL_ROOT_UID;
1732 stat->gid = GLOBAL_ROOT_GID;
1733 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1735 if (!has_pid_permissions(pid, task, 2)) {
1738 * This doesn't prevent learning whether PID exists,
1739 * it only makes getattr() consistent with readdir().
1743 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1744 task_dumpable(task)) {
1745 cred = __task_cred(task);
1746 stat->uid = cred->euid;
1747 stat->gid = cred->egid;
1757 * Exceptional case: normally we are not allowed to unhash a busy
1758 * directory. In this case, however, we can do it - no aliasing problems
1759 * due to the way we treat inodes.
1761 * Rewrite the inode's ownerships here because the owning task may have
1762 * performed a setuid(), etc.
1764 * Before the /proc/pid/status file was created the only way to read
1765 * the effective uid of a /process was to stat /proc/pid. Reading
1766 * /proc/pid/status is slow enough that procps and other packages
1767 * kept stating /proc/pid. To keep the rules in /proc simple I have
1768 * made this apply to all per process world readable and executable
1771 int pid_revalidate(struct dentry *dentry, unsigned int flags)
1773 struct inode *inode;
1774 struct task_struct *task;
1775 const struct cred *cred;
1777 if (flags & LOOKUP_RCU)
1780 inode = d_inode(dentry);
1781 task = get_proc_task(inode);
1784 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1785 task_dumpable(task)) {
1787 cred = __task_cred(task);
1788 inode->i_uid = cred->euid;
1789 inode->i_gid = cred->egid;
1792 inode->i_uid = GLOBAL_ROOT_UID;
1793 inode->i_gid = GLOBAL_ROOT_GID;
1795 inode->i_mode &= ~(S_ISUID | S_ISGID);
1796 security_task_to_inode(task, inode);
1797 put_task_struct(task);
1803 static inline bool proc_inode_is_dead(struct inode *inode)
1805 return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1808 int pid_delete_dentry(const struct dentry *dentry)
1810 /* Is the task we represent dead?
1811 * If so, then don't put the dentry on the lru list,
1812 * kill it immediately.
1814 return proc_inode_is_dead(d_inode(dentry));
1817 const struct dentry_operations pid_dentry_operations =
1819 .d_revalidate = pid_revalidate,
1820 .d_delete = pid_delete_dentry,
1826 * Fill a directory entry.
1828 * If possible create the dcache entry and derive our inode number and
1829 * file type from dcache entry.
1831 * Since all of the proc inode numbers are dynamically generated, the inode
1832 * numbers do not exist until the inode is cache. This means creating the
1833 * the dcache entry in readdir is necessary to keep the inode numbers
1834 * reported by readdir in sync with the inode numbers reported
1837 bool proc_fill_cache(struct file *file, struct dir_context *ctx,
1838 const char *name, int len,
1839 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1841 struct dentry *child, *dir = file->f_path.dentry;
1842 struct qstr qname = QSTR_INIT(name, len);
1843 struct inode *inode;
1847 child = d_hash_and_lookup(dir, &qname);
1849 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1850 child = d_alloc_parallel(dir, &qname, &wq);
1852 goto end_instantiate;
1853 if (d_in_lookup(child)) {
1854 int err = instantiate(d_inode(dir), child, task, ptr);
1855 d_lookup_done(child);
1858 goto end_instantiate;
1862 inode = d_inode(child);
1864 type = inode->i_mode >> 12;
1866 return dir_emit(ctx, name, len, ino, type);
1869 return dir_emit(ctx, name, len, 1, DT_UNKNOWN);
1873 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1874 * which represent vma start and end addresses.
1876 static int dname_to_vma_addr(struct dentry *dentry,
1877 unsigned long *start, unsigned long *end)
1879 const char *str = dentry->d_name.name;
1880 unsigned long long sval, eval;
1883 len = _parse_integer(str, 16, &sval);
1884 if (len & KSTRTOX_OVERFLOW)
1886 if (sval != (unsigned long)sval)
1894 len = _parse_integer(str, 16, &eval);
1895 if (len & KSTRTOX_OVERFLOW)
1897 if (eval != (unsigned long)eval)
1910 static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
1912 unsigned long vm_start, vm_end;
1913 bool exact_vma_exists = false;
1914 struct mm_struct *mm = NULL;
1915 struct task_struct *task;
1916 const struct cred *cred;
1917 struct inode *inode;
1920 if (flags & LOOKUP_RCU)
1923 inode = d_inode(dentry);
1924 task = get_proc_task(inode);
1928 mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
1929 if (IS_ERR_OR_NULL(mm))
1932 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1933 down_read(&mm->mmap_sem);
1934 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1935 up_read(&mm->mmap_sem);
1940 if (exact_vma_exists) {
1941 if (task_dumpable(task)) {
1943 cred = __task_cred(task);
1944 inode->i_uid = cred->euid;
1945 inode->i_gid = cred->egid;
1948 inode->i_uid = GLOBAL_ROOT_UID;
1949 inode->i_gid = GLOBAL_ROOT_GID;
1951 security_task_to_inode(task, inode);
1956 put_task_struct(task);
1962 static const struct dentry_operations tid_map_files_dentry_operations = {
1963 .d_revalidate = map_files_d_revalidate,
1964 .d_delete = pid_delete_dentry,
1967 static int map_files_get_link(struct dentry *dentry, struct path *path)
1969 unsigned long vm_start, vm_end;
1970 struct vm_area_struct *vma;
1971 struct task_struct *task;
1972 struct mm_struct *mm;
1976 task = get_proc_task(d_inode(dentry));
1980 mm = get_task_mm(task);
1981 put_task_struct(task);
1985 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
1990 down_read(&mm->mmap_sem);
1991 vma = find_exact_vma(mm, vm_start, vm_end);
1992 if (vma && vma->vm_file) {
1993 *path = vma->vm_file->f_path;
1997 up_read(&mm->mmap_sem);
2005 struct map_files_info {
2008 unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
2012 * Only allow CAP_SYS_ADMIN to follow the links, due to concerns about how the
2013 * symlinks may be used to bypass permissions on ancestor directories in the
2014 * path to the file in question.
2017 proc_map_files_get_link(struct dentry *dentry,
2018 struct inode *inode,
2019 struct delayed_call *done)
2021 if (!capable(CAP_SYS_ADMIN))
2022 return ERR_PTR(-EPERM);
2024 return proc_pid_get_link(dentry, inode, done);
2028 * Identical to proc_pid_link_inode_operations except for get_link()
2030 static const struct inode_operations proc_map_files_link_inode_operations = {
2031 .readlink = proc_pid_readlink,
2032 .get_link = proc_map_files_get_link,
2033 .setattr = proc_setattr,
2037 proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
2038 struct task_struct *task, const void *ptr)
2040 fmode_t mode = (fmode_t)(unsigned long)ptr;
2041 struct proc_inode *ei;
2042 struct inode *inode;
2044 inode = proc_pid_make_inode(dir->i_sb, task);
2049 ei->op.proc_get_link = map_files_get_link;
2051 inode->i_op = &proc_map_files_link_inode_operations;
2053 inode->i_mode = S_IFLNK;
2055 if (mode & FMODE_READ)
2056 inode->i_mode |= S_IRUSR;
2057 if (mode & FMODE_WRITE)
2058 inode->i_mode |= S_IWUSR;
2060 d_set_d_op(dentry, &tid_map_files_dentry_operations);
2061 d_add(dentry, inode);
2066 static struct dentry *proc_map_files_lookup(struct inode *dir,
2067 struct dentry *dentry, unsigned int flags)
2069 unsigned long vm_start, vm_end;
2070 struct vm_area_struct *vma;
2071 struct task_struct *task;
2073 struct mm_struct *mm;
2076 task = get_proc_task(dir);
2081 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
2085 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
2088 mm = get_task_mm(task);
2092 down_read(&mm->mmap_sem);
2093 vma = find_exact_vma(mm, vm_start, vm_end);
2098 result = proc_map_files_instantiate(dir, dentry, task,
2099 (void *)(unsigned long)vma->vm_file->f_mode);
2102 up_read(&mm->mmap_sem);
2105 put_task_struct(task);
2107 return ERR_PTR(result);
2110 static const struct inode_operations proc_map_files_inode_operations = {
2111 .lookup = proc_map_files_lookup,
2112 .permission = proc_fd_permission,
2113 .setattr = proc_setattr,
2117 proc_map_files_readdir(struct file *file, struct dir_context *ctx)
2119 struct vm_area_struct *vma;
2120 struct task_struct *task;
2121 struct mm_struct *mm;
2122 unsigned long nr_files, pos, i;
2123 struct flex_array *fa = NULL;
2124 struct map_files_info info;
2125 struct map_files_info *p;
2129 task = get_proc_task(file_inode(file));
2134 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
2138 if (!dir_emit_dots(file, ctx))
2141 mm = get_task_mm(task);
2144 down_read(&mm->mmap_sem);
2149 * We need two passes here:
2151 * 1) Collect vmas of mapped files with mmap_sem taken
2152 * 2) Release mmap_sem and instantiate entries
2154 * otherwise we get lockdep complained, since filldir()
2155 * routine might require mmap_sem taken in might_fault().
2158 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
2159 if (vma->vm_file && ++pos > ctx->pos)
2164 fa = flex_array_alloc(sizeof(info), nr_files,
2166 if (!fa || flex_array_prealloc(fa, 0, nr_files,
2170 flex_array_free(fa);
2171 up_read(&mm->mmap_sem);
2175 for (i = 0, vma = mm->mmap, pos = 2; vma;
2176 vma = vma->vm_next) {
2179 if (++pos <= ctx->pos)
2182 info.mode = vma->vm_file->f_mode;
2183 info.len = snprintf(info.name,
2184 sizeof(info.name), "%lx-%lx",
2185 vma->vm_start, vma->vm_end);
2186 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
2190 up_read(&mm->mmap_sem);
2192 for (i = 0; i < nr_files; i++) {
2193 p = flex_array_get(fa, i);
2194 if (!proc_fill_cache(file, ctx,
2196 proc_map_files_instantiate,
2198 (void *)(unsigned long)p->mode))
2203 flex_array_free(fa);
2207 put_task_struct(task);
2212 static const struct file_operations proc_map_files_operations = {
2213 .read = generic_read_dir,
2214 .iterate_shared = proc_map_files_readdir,
2215 .llseek = generic_file_llseek,
2218 #ifdef CONFIG_CHECKPOINT_RESTORE
2219 struct timers_private {
2221 struct task_struct *task;
2222 struct sighand_struct *sighand;
2223 struct pid_namespace *ns;
2224 unsigned long flags;
2227 static void *timers_start(struct seq_file *m, loff_t *pos)
2229 struct timers_private *tp = m->private;
2231 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2233 return ERR_PTR(-ESRCH);
2235 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2237 return ERR_PTR(-ESRCH);
2239 return seq_list_start(&tp->task->signal->posix_timers, *pos);
2242 static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2244 struct timers_private *tp = m->private;
2245 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2248 static void timers_stop(struct seq_file *m, void *v)
2250 struct timers_private *tp = m->private;
2253 unlock_task_sighand(tp->task, &tp->flags);
2258 put_task_struct(tp->task);
2263 static int show_timer(struct seq_file *m, void *v)
2265 struct k_itimer *timer;
2266 struct timers_private *tp = m->private;
2268 static const char * const nstr[] = {
2269 [SIGEV_SIGNAL] = "signal",
2270 [SIGEV_NONE] = "none",
2271 [SIGEV_THREAD] = "thread",
2274 timer = list_entry((struct list_head *)v, struct k_itimer, list);
2275 notify = timer->it_sigev_notify;
2277 seq_printf(m, "ID: %d\n", timer->it_id);
2278 seq_printf(m, "signal: %d/%p\n",
2279 timer->sigq->info.si_signo,
2280 timer->sigq->info.si_value.sival_ptr);
2281 seq_printf(m, "notify: %s/%s.%d\n",
2282 nstr[notify & ~SIGEV_THREAD_ID],
2283 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2284 pid_nr_ns(timer->it_pid, tp->ns));
2285 seq_printf(m, "ClockID: %d\n", timer->it_clock);
2290 static const struct seq_operations proc_timers_seq_ops = {
2291 .start = timers_start,
2292 .next = timers_next,
2293 .stop = timers_stop,
2297 static int proc_timers_open(struct inode *inode, struct file *file)
2299 struct timers_private *tp;
2301 tp = __seq_open_private(file, &proc_timers_seq_ops,
2302 sizeof(struct timers_private));
2306 tp->pid = proc_pid(inode);
2307 tp->ns = inode->i_sb->s_fs_info;
2311 static const struct file_operations proc_timers_operations = {
2312 .open = proc_timers_open,
2314 .llseek = seq_lseek,
2315 .release = seq_release_private,
2319 static ssize_t timerslack_ns_write(struct file *file, const char __user *buf,
2320 size_t count, loff_t *offset)
2322 struct inode *inode = file_inode(file);
2323 struct task_struct *p;
2327 err = kstrtoull_from_user(buf, count, 10, &slack_ns);
2331 p = get_proc_task(inode);
2336 if (!capable(CAP_SYS_NICE)) {
2341 err = security_task_setscheduler(p);
2350 p->timer_slack_ns = p->default_timer_slack_ns;
2352 p->timer_slack_ns = slack_ns;
2361 static int timerslack_ns_show(struct seq_file *m, void *v)
2363 struct inode *inode = m->private;
2364 struct task_struct *p;
2367 p = get_proc_task(inode);
2373 if (!capable(CAP_SYS_NICE)) {
2377 err = security_task_getscheduler(p);
2383 seq_printf(m, "%llu\n", p->timer_slack_ns);
2392 static int timerslack_ns_open(struct inode *inode, struct file *filp)
2394 return single_open(filp, timerslack_ns_show, inode);
2397 static const struct file_operations proc_pid_set_timerslack_ns_operations = {
2398 .open = timerslack_ns_open,
2400 .write = timerslack_ns_write,
2401 .llseek = seq_lseek,
2402 .release = single_release,
2405 static int proc_pident_instantiate(struct inode *dir,
2406 struct dentry *dentry, struct task_struct *task, const void *ptr)
2408 const struct pid_entry *p = ptr;
2409 struct inode *inode;
2410 struct proc_inode *ei;
2412 inode = proc_pid_make_inode(dir->i_sb, task);
2417 inode->i_mode = p->mode;
2418 if (S_ISDIR(inode->i_mode))
2419 set_nlink(inode, 2); /* Use getattr to fix if necessary */
2421 inode->i_op = p->iop;
2423 inode->i_fop = p->fop;
2425 d_set_d_op(dentry, &pid_dentry_operations);
2426 d_add(dentry, inode);
2427 /* Close the race of the process dying before we return the dentry */
2428 if (pid_revalidate(dentry, 0))
2434 static struct dentry *proc_pident_lookup(struct inode *dir,
2435 struct dentry *dentry,
2436 const struct pid_entry *ents,
2440 struct task_struct *task = get_proc_task(dir);
2441 const struct pid_entry *p, *last;
2449 * Yes, it does not scale. And it should not. Don't add
2450 * new entries into /proc/<tgid>/ without very good reasons.
2452 last = &ents[nents - 1];
2453 for (p = ents; p <= last; p++) {
2454 if (p->len != dentry->d_name.len)
2456 if (!memcmp(dentry->d_name.name, p->name, p->len))
2462 error = proc_pident_instantiate(dir, dentry, task, p);
2464 put_task_struct(task);
2466 return ERR_PTR(error);
2469 static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
2470 const struct pid_entry *ents, unsigned int nents)
2472 struct task_struct *task = get_proc_task(file_inode(file));
2473 const struct pid_entry *p;
2478 if (!dir_emit_dots(file, ctx))
2481 if (ctx->pos >= nents + 2)
2484 for (p = ents + (ctx->pos - 2); p <= ents + nents - 1; p++) {
2485 if (!proc_fill_cache(file, ctx, p->name, p->len,
2486 proc_pident_instantiate, task, p))
2491 put_task_struct(task);
2495 #ifdef CONFIG_SECURITY
2496 static int proc_pid_attr_open(struct inode *inode, struct file *file)
2498 file->private_data = NULL;
2499 __mem_open(inode, file, PTRACE_MODE_READ_FSCREDS);
2503 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2504 size_t count, loff_t *ppos)
2506 struct inode * inode = file_inode(file);
2509 struct task_struct *task = get_proc_task(inode);
2514 length = security_getprocattr(task,
2515 (char*)file->f_path.dentry->d_name.name,
2517 put_task_struct(task);
2519 length = simple_read_from_buffer(buf, count, ppos, p, length);
2524 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2525 size_t count, loff_t *ppos)
2527 struct inode * inode = file_inode(file);
2530 struct task_struct *task = get_proc_task(inode);
2532 /* A task may only write when it was the opener. */
2533 if (file->private_data != current->mm)
2539 if (count > PAGE_SIZE)
2542 /* No partial writes. */
2547 page = memdup_user(buf, count);
2549 length = PTR_ERR(page);
2553 /* Guard against adverse ptrace interaction */
2554 length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
2558 length = security_setprocattr(task,
2559 (char*)file->f_path.dentry->d_name.name,
2561 mutex_unlock(&task->signal->cred_guard_mutex);
2565 put_task_struct(task);
2570 static const struct file_operations proc_pid_attr_operations = {
2571 .open = proc_pid_attr_open,
2572 .read = proc_pid_attr_read,
2573 .write = proc_pid_attr_write,
2574 .llseek = generic_file_llseek,
2575 .release = mem_release,
2578 static const struct pid_entry attr_dir_stuff[] = {
2579 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2580 REG("prev", S_IRUGO, proc_pid_attr_operations),
2581 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2582 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2583 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2584 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2587 static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
2589 return proc_pident_readdir(file, ctx,
2590 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2593 static const struct file_operations proc_attr_dir_operations = {
2594 .read = generic_read_dir,
2595 .iterate_shared = proc_attr_dir_readdir,
2596 .llseek = generic_file_llseek,
2599 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2600 struct dentry *dentry, unsigned int flags)
2602 return proc_pident_lookup(dir, dentry,
2603 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2606 static const struct inode_operations proc_attr_dir_inode_operations = {
2607 .lookup = proc_attr_dir_lookup,
2608 .getattr = pid_getattr,
2609 .setattr = proc_setattr,
2614 #ifdef CONFIG_ELF_CORE
2615 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2616 size_t count, loff_t *ppos)
2618 struct task_struct *task = get_proc_task(file_inode(file));
2619 struct mm_struct *mm;
2620 char buffer[PROC_NUMBUF];
2628 mm = get_task_mm(task);
2630 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2631 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2632 MMF_DUMP_FILTER_SHIFT));
2634 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2637 put_task_struct(task);
2642 static ssize_t proc_coredump_filter_write(struct file *file,
2643 const char __user *buf,
2647 struct task_struct *task;
2648 struct mm_struct *mm;
2654 ret = kstrtouint_from_user(buf, count, 0, &val);
2659 task = get_proc_task(file_inode(file));
2663 mm = get_task_mm(task);
2668 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2670 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2672 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2677 put_task_struct(task);
2684 static const struct file_operations proc_coredump_filter_operations = {
2685 .read = proc_coredump_filter_read,
2686 .write = proc_coredump_filter_write,
2687 .llseek = generic_file_llseek,
2691 #ifdef CONFIG_TASK_IO_ACCOUNTING
2692 static int do_io_accounting(struct task_struct *task, struct seq_file *m, int whole)
2694 struct task_io_accounting acct = task->ioac;
2695 unsigned long flags;
2698 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2702 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
2707 if (whole && lock_task_sighand(task, &flags)) {
2708 struct task_struct *t = task;
2710 task_io_accounting_add(&acct, &task->signal->ioac);
2711 while_each_thread(task, t)
2712 task_io_accounting_add(&acct, &t->ioac);
2714 unlock_task_sighand(task, &flags);
2721 "read_bytes: %llu\n"
2722 "write_bytes: %llu\n"
2723 "cancelled_write_bytes: %llu\n",
2724 (unsigned long long)acct.rchar,
2725 (unsigned long long)acct.wchar,
2726 (unsigned long long)acct.syscr,
2727 (unsigned long long)acct.syscw,
2728 (unsigned long long)acct.read_bytes,
2729 (unsigned long long)acct.write_bytes,
2730 (unsigned long long)acct.cancelled_write_bytes);
2734 mutex_unlock(&task->signal->cred_guard_mutex);
2738 static int proc_tid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2739 struct pid *pid, struct task_struct *task)
2741 return do_io_accounting(task, m, 0);
2744 static int proc_tgid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2745 struct pid *pid, struct task_struct *task)
2747 return do_io_accounting(task, m, 1);
2749 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2751 #ifdef CONFIG_USER_NS
2752 static int proc_id_map_open(struct inode *inode, struct file *file,
2753 const struct seq_operations *seq_ops)
2755 struct user_namespace *ns = NULL;
2756 struct task_struct *task;
2757 struct seq_file *seq;
2760 task = get_proc_task(inode);
2763 ns = get_user_ns(task_cred_xxx(task, user_ns));
2765 put_task_struct(task);
2770 ret = seq_open(file, seq_ops);
2774 seq = file->private_data;
2784 static int proc_id_map_release(struct inode *inode, struct file *file)
2786 struct seq_file *seq = file->private_data;
2787 struct user_namespace *ns = seq->private;
2789 return seq_release(inode, file);
2792 static int proc_uid_map_open(struct inode *inode, struct file *file)
2794 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2797 static int proc_gid_map_open(struct inode *inode, struct file *file)
2799 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2802 static int proc_projid_map_open(struct inode *inode, struct file *file)
2804 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2807 static const struct file_operations proc_uid_map_operations = {
2808 .open = proc_uid_map_open,
2809 .write = proc_uid_map_write,
2811 .llseek = seq_lseek,
2812 .release = proc_id_map_release,
2815 static const struct file_operations proc_gid_map_operations = {
2816 .open = proc_gid_map_open,
2817 .write = proc_gid_map_write,
2819 .llseek = seq_lseek,
2820 .release = proc_id_map_release,
2823 static const struct file_operations proc_projid_map_operations = {
2824 .open = proc_projid_map_open,
2825 .write = proc_projid_map_write,
2827 .llseek = seq_lseek,
2828 .release = proc_id_map_release,
2831 static int proc_setgroups_open(struct inode *inode, struct file *file)
2833 struct user_namespace *ns = NULL;
2834 struct task_struct *task;
2838 task = get_proc_task(inode);
2841 ns = get_user_ns(task_cred_xxx(task, user_ns));
2843 put_task_struct(task);
2848 if (file->f_mode & FMODE_WRITE) {
2850 if (!ns_capable(ns, CAP_SYS_ADMIN))
2854 ret = single_open(file, &proc_setgroups_show, ns);
2865 static int proc_setgroups_release(struct inode *inode, struct file *file)
2867 struct seq_file *seq = file->private_data;
2868 struct user_namespace *ns = seq->private;
2869 int ret = single_release(inode, file);
2874 static const struct file_operations proc_setgroups_operations = {
2875 .open = proc_setgroups_open,
2876 .write = proc_setgroups_write,
2878 .llseek = seq_lseek,
2879 .release = proc_setgroups_release,
2881 #endif /* CONFIG_USER_NS */
2883 static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2884 struct pid *pid, struct task_struct *task)
2886 int err = lock_trace(task);
2888 seq_printf(m, "%08x\n", task->personality);
2897 static const struct file_operations proc_task_operations;
2898 static const struct inode_operations proc_task_inode_operations;
2900 static const struct pid_entry tgid_base_stuff[] = {
2901 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2902 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2903 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
2904 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2905 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
2907 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2909 REG("environ", S_IRUSR, proc_environ_operations),
2910 REG("auxv", S_IRUSR, proc_auxv_operations),
2911 ONE("status", S_IRUGO, proc_pid_status),
2912 ONE("personality", S_IRUSR, proc_pid_personality),
2913 ONE("limits", S_IRUGO, proc_pid_limits),
2914 #ifdef CONFIG_SCHED_DEBUG
2915 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2917 #ifdef CONFIG_SCHED_AUTOGROUP
2918 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2920 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2921 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2922 ONE("syscall", S_IRUSR, proc_pid_syscall),
2924 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
2925 ONE("stat", S_IRUGO, proc_tgid_stat),
2926 ONE("statm", S_IRUGO, proc_pid_statm),
2927 REG("maps", S_IRUGO, proc_pid_maps_operations),
2929 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
2931 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2932 LNK("cwd", proc_cwd_link),
2933 LNK("root", proc_root_link),
2934 LNK("exe", proc_exe_link),
2935 REG("mounts", S_IRUGO, proc_mounts_operations),
2936 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2937 REG("mountstats", S_IRUSR, proc_mountstats_operations),
2938 #ifdef CONFIG_PROC_PAGE_MONITOR
2939 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2940 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
2941 REG("pagemap", S_IRUSR, proc_pagemap_operations),
2943 #ifdef CONFIG_SECURITY
2944 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
2946 #ifdef CONFIG_KALLSYMS
2947 ONE("wchan", S_IRUGO, proc_pid_wchan),
2949 #ifdef CONFIG_STACKTRACE
2950 ONE("stack", S_IRUSR, proc_pid_stack),
2952 #ifdef CONFIG_SCHED_INFO
2953 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
2955 #ifdef CONFIG_LATENCYTOP
2956 REG("latency", S_IRUGO, proc_lstats_operations),
2958 #ifdef CONFIG_PROC_PID_CPUSET
2959 ONE("cpuset", S_IRUGO, proc_cpuset_show),
2961 #ifdef CONFIG_CGROUPS
2962 ONE("cgroup", S_IRUGO, proc_cgroup_show),
2964 ONE("oom_score", S_IRUGO, proc_oom_score),
2965 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
2966 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
2967 #ifdef CONFIG_AUDITSYSCALL
2968 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2969 REG("sessionid", S_IRUGO, proc_sessionid_operations),
2971 #ifdef CONFIG_FAULT_INJECTION
2972 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2974 #ifdef CONFIG_ELF_CORE
2975 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
2977 #ifdef CONFIG_TASK_IO_ACCOUNTING
2978 ONE("io", S_IRUSR, proc_tgid_io_accounting),
2980 #ifdef CONFIG_HARDWALL
2981 ONE("hardwall", S_IRUGO, proc_pid_hardwall),
2983 #ifdef CONFIG_USER_NS
2984 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2985 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
2986 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
2987 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
2989 #ifdef CONFIG_CHECKPOINT_RESTORE
2990 REG("timers", S_IRUGO, proc_timers_operations),
2992 REG("timerslack_ns", S_IRUGO|S_IWUGO, proc_pid_set_timerslack_ns_operations),
2995 static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
2997 return proc_pident_readdir(file, ctx,
2998 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
3001 static const struct file_operations proc_tgid_base_operations = {
3002 .read = generic_read_dir,
3003 .iterate_shared = proc_tgid_base_readdir,
3004 .llseek = generic_file_llseek,
3007 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3009 return proc_pident_lookup(dir, dentry,
3010 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
3013 static const struct inode_operations proc_tgid_base_inode_operations = {
3014 .lookup = proc_tgid_base_lookup,
3015 .getattr = pid_getattr,
3016 .setattr = proc_setattr,
3017 .permission = proc_pid_permission,
3020 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
3022 struct dentry *dentry, *leader, *dir;
3023 char buf[PROC_NUMBUF];
3027 name.len = snprintf(buf, sizeof(buf), "%d", pid);
3028 /* no ->d_hash() rejects on procfs */
3029 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
3031 d_invalidate(dentry);
3039 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
3040 leader = d_hash_and_lookup(mnt->mnt_root, &name);
3045 name.len = strlen(name.name);
3046 dir = d_hash_and_lookup(leader, &name);
3048 goto out_put_leader;
3051 name.len = snprintf(buf, sizeof(buf), "%d", pid);
3052 dentry = d_hash_and_lookup(dir, &name);
3054 d_invalidate(dentry);
3066 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
3067 * @task: task that should be flushed.
3069 * When flushing dentries from proc, one needs to flush them from global
3070 * proc (proc_mnt) and from all the namespaces' procs this task was seen
3071 * in. This call is supposed to do all of this job.
3073 * Looks in the dcache for
3075 * /proc/@tgid/task/@pid
3076 * if either directory is present flushes it and all of it'ts children
3079 * It is safe and reasonable to cache /proc entries for a task until
3080 * that task exits. After that they just clog up the dcache with
3081 * useless entries, possibly causing useful dcache entries to be
3082 * flushed instead. This routine is proved to flush those useless
3083 * dcache entries at process exit time.
3085 * NOTE: This routine is just an optimization so it does not guarantee
3086 * that no dcache entries will exist at process exit time it
3087 * just makes it very unlikely that any will persist.
3090 void proc_flush_task(struct task_struct *task)
3093 struct pid *pid, *tgid;
3096 pid = task_pid(task);
3097 tgid = task_tgid(task);
3099 for (i = 0; i <= pid->level; i++) {
3100 upid = &pid->numbers[i];
3101 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
3102 tgid->numbers[i].nr);
3106 static int proc_pid_instantiate(struct inode *dir,
3107 struct dentry * dentry,
3108 struct task_struct *task, const void *ptr)
3110 struct inode *inode;
3112 inode = proc_pid_make_inode(dir->i_sb, task);
3116 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3117 inode->i_op = &proc_tgid_base_inode_operations;
3118 inode->i_fop = &proc_tgid_base_operations;
3119 inode->i_flags|=S_IMMUTABLE;
3121 set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
3122 ARRAY_SIZE(tgid_base_stuff)));
3124 d_set_d_op(dentry, &pid_dentry_operations);
3126 d_add(dentry, inode);
3127 /* Close the race of the process dying before we return the dentry */
3128 if (pid_revalidate(dentry, 0))
3134 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
3136 int result = -ENOENT;
3137 struct task_struct *task;
3139 struct pid_namespace *ns;
3141 tgid = name_to_int(&dentry->d_name);
3145 ns = dentry->d_sb->s_fs_info;
3147 task = find_task_by_pid_ns(tgid, ns);
3149 get_task_struct(task);
3154 result = proc_pid_instantiate(dir, dentry, task, NULL);
3155 put_task_struct(task);
3157 return ERR_PTR(result);
3161 * Find the first task with tgid >= tgid
3166 struct task_struct *task;
3168 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3173 put_task_struct(iter.task);
3177 pid = find_ge_pid(iter.tgid, ns);
3179 iter.tgid = pid_nr_ns(pid, ns);
3180 iter.task = pid_task(pid, PIDTYPE_PID);
3181 /* What we to know is if the pid we have find is the
3182 * pid of a thread_group_leader. Testing for task
3183 * being a thread_group_leader is the obvious thing
3184 * todo but there is a window when it fails, due to
3185 * the pid transfer logic in de_thread.
3187 * So we perform the straight forward test of seeing
3188 * if the pid we have found is the pid of a thread
3189 * group leader, and don't worry if the task we have
3190 * found doesn't happen to be a thread group leader.
3191 * As we don't care in the case of readdir.
3193 if (!iter.task || !has_group_leader_pid(iter.task)) {
3197 get_task_struct(iter.task);
3203 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
3205 /* for the /proc/ directory itself, after non-process stuff has been done */
3206 int proc_pid_readdir(struct file *file, struct dir_context *ctx)
3208 struct tgid_iter iter;
3209 struct pid_namespace *ns = file_inode(file)->i_sb->s_fs_info;
3210 loff_t pos = ctx->pos;
3212 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
3215 if (pos == TGID_OFFSET - 2) {
3216 struct inode *inode = d_inode(ns->proc_self);
3217 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
3219 ctx->pos = pos = pos + 1;
3221 if (pos == TGID_OFFSET - 1) {
3222 struct inode *inode = d_inode(ns->proc_thread_self);
3223 if (!dir_emit(ctx, "thread-self", 11, inode->i_ino, DT_LNK))
3225 ctx->pos = pos = pos + 1;
3227 iter.tgid = pos - TGID_OFFSET;
3229 for (iter = next_tgid(ns, iter);
3231 iter.tgid += 1, iter = next_tgid(ns, iter)) {
3232 char name[PROC_NUMBUF];
3236 if (!has_pid_permissions(ns, iter.task, 2))
3239 len = snprintf(name, sizeof(name), "%d", iter.tgid);
3240 ctx->pos = iter.tgid + TGID_OFFSET;
3241 if (!proc_fill_cache(file, ctx, name, len,
3242 proc_pid_instantiate, iter.task, NULL)) {
3243 put_task_struct(iter.task);
3247 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
3252 * proc_tid_comm_permission is a special permission function exclusively
3253 * used for the node /proc/<pid>/task/<tid>/comm.
3254 * It bypasses generic permission checks in the case where a task of the same
3255 * task group attempts to access the node.
3256 * The rationale behind this is that glibc and bionic access this node for
3257 * cross thread naming (pthread_set/getname_np(!self)). However, if
3258 * PR_SET_DUMPABLE gets set to 0 this node among others becomes uid=0 gid=0,
3259 * which locks out the cross thread naming implementation.
3260 * This function makes sure that the node is always accessible for members of
3261 * same thread group.
3263 static int proc_tid_comm_permission(struct inode *inode, int mask)
3265 bool is_same_tgroup;
3266 struct task_struct *task;
3268 task = get_proc_task(inode);
3271 is_same_tgroup = same_thread_group(current, task);
3272 put_task_struct(task);
3274 if (likely(is_same_tgroup && !(mask & MAY_EXEC))) {
3275 /* This file (/proc/<pid>/task/<tid>/comm) can always be
3276 * read or written by the members of the corresponding
3282 return generic_permission(inode, mask);
3285 static const struct inode_operations proc_tid_comm_inode_operations = {
3286 .permission = proc_tid_comm_permission,
3292 static const struct pid_entry tid_base_stuff[] = {
3293 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
3294 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
3295 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
3297 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
3299 REG("environ", S_IRUSR, proc_environ_operations),
3300 REG("auxv", S_IRUSR, proc_auxv_operations),
3301 ONE("status", S_IRUGO, proc_pid_status),
3302 ONE("personality", S_IRUSR, proc_pid_personality),
3303 ONE("limits", S_IRUGO, proc_pid_limits),
3304 #ifdef CONFIG_SCHED_DEBUG
3305 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
3307 NOD("comm", S_IFREG|S_IRUGO|S_IWUSR,
3308 &proc_tid_comm_inode_operations,
3309 &proc_pid_set_comm_operations, {}),
3310 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3311 ONE("syscall", S_IRUSR, proc_pid_syscall),
3313 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
3314 ONE("stat", S_IRUGO, proc_tid_stat),
3315 ONE("statm", S_IRUGO, proc_pid_statm),
3316 REG("maps", S_IRUGO, proc_tid_maps_operations),
3317 #ifdef CONFIG_PROC_CHILDREN
3318 REG("children", S_IRUGO, proc_tid_children_operations),
3321 REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
3323 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3324 LNK("cwd", proc_cwd_link),
3325 LNK("root", proc_root_link),
3326 LNK("exe", proc_exe_link),
3327 REG("mounts", S_IRUGO, proc_mounts_operations),
3328 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
3329 #ifdef CONFIG_PROC_PAGE_MONITOR
3330 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3331 REG("smaps", S_IRUGO, proc_tid_smaps_operations),
3332 REG("pagemap", S_IRUSR, proc_pagemap_operations),
3334 #ifdef CONFIG_SECURITY
3335 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
3337 #ifdef CONFIG_KALLSYMS
3338 ONE("wchan", S_IRUGO, proc_pid_wchan),
3340 #ifdef CONFIG_STACKTRACE
3341 ONE("stack", S_IRUSR, proc_pid_stack),
3343 #ifdef CONFIG_SCHED_INFO
3344 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
3346 #ifdef CONFIG_LATENCYTOP
3347 REG("latency", S_IRUGO, proc_lstats_operations),
3349 #ifdef CONFIG_PROC_PID_CPUSET
3350 ONE("cpuset", S_IRUGO, proc_cpuset_show),
3352 #ifdef CONFIG_CGROUPS
3353 ONE("cgroup", S_IRUGO, proc_cgroup_show),
3355 ONE("oom_score", S_IRUGO, proc_oom_score),
3356 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
3357 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
3358 #ifdef CONFIG_AUDITSYSCALL
3359 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3360 REG("sessionid", S_IRUGO, proc_sessionid_operations),
3362 #ifdef CONFIG_FAULT_INJECTION
3363 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3365 #ifdef CONFIG_TASK_IO_ACCOUNTING
3366 ONE("io", S_IRUSR, proc_tid_io_accounting),
3368 #ifdef CONFIG_HARDWALL
3369 ONE("hardwall", S_IRUGO, proc_pid_hardwall),
3371 #ifdef CONFIG_USER_NS
3372 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3373 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
3374 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
3375 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
3379 static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
3381 return proc_pident_readdir(file, ctx,
3382 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3385 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3387 return proc_pident_lookup(dir, dentry,
3388 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3391 static const struct file_operations proc_tid_base_operations = {
3392 .read = generic_read_dir,
3393 .iterate_shared = proc_tid_base_readdir,
3394 .llseek = generic_file_llseek,
3397 static const struct inode_operations proc_tid_base_inode_operations = {
3398 .lookup = proc_tid_base_lookup,
3399 .getattr = pid_getattr,
3400 .setattr = proc_setattr,
3403 static int proc_task_instantiate(struct inode *dir,
3404 struct dentry *dentry, struct task_struct *task, const void *ptr)
3406 struct inode *inode;
3407 inode = proc_pid_make_inode(dir->i_sb, task);
3411 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3412 inode->i_op = &proc_tid_base_inode_operations;
3413 inode->i_fop = &proc_tid_base_operations;
3414 inode->i_flags|=S_IMMUTABLE;
3416 set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3417 ARRAY_SIZE(tid_base_stuff)));
3419 d_set_d_op(dentry, &pid_dentry_operations);
3421 d_add(dentry, inode);
3422 /* Close the race of the process dying before we return the dentry */
3423 if (pid_revalidate(dentry, 0))
3429 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
3431 int result = -ENOENT;
3432 struct task_struct *task;
3433 struct task_struct *leader = get_proc_task(dir);
3435 struct pid_namespace *ns;
3440 tid = name_to_int(&dentry->d_name);
3444 ns = dentry->d_sb->s_fs_info;
3446 task = find_task_by_pid_ns(tid, ns);
3448 get_task_struct(task);
3452 if (!same_thread_group(leader, task))
3455 result = proc_task_instantiate(dir, dentry, task, NULL);
3457 put_task_struct(task);
3459 put_task_struct(leader);
3461 return ERR_PTR(result);
3465 * Find the first tid of a thread group to return to user space.
3467 * Usually this is just the thread group leader, but if the users
3468 * buffer was too small or there was a seek into the middle of the
3469 * directory we have more work todo.
3471 * In the case of a short read we start with find_task_by_pid.
3473 * In the case of a seek we start with the leader and walk nr
3476 static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
3477 struct pid_namespace *ns)
3479 struct task_struct *pos, *task;
3480 unsigned long nr = f_pos;
3482 if (nr != f_pos) /* 32bit overflow? */
3486 task = pid_task(pid, PIDTYPE_PID);
3490 /* Attempt to start with the tid of a thread */
3492 pos = find_task_by_pid_ns(tid, ns);
3493 if (pos && same_thread_group(pos, task))
3497 /* If nr exceeds the number of threads there is nothing todo */
3498 if (nr >= get_nr_threads(task))
3501 /* If we haven't found our starting place yet start
3502 * with the leader and walk nr threads forward.
3504 pos = task = task->group_leader;
3508 } while_each_thread(task, pos);
3513 get_task_struct(pos);
3520 * Find the next thread in the thread list.
3521 * Return NULL if there is an error or no next thread.
3523 * The reference to the input task_struct is released.
3525 static struct task_struct *next_tid(struct task_struct *start)
3527 struct task_struct *pos = NULL;
3529 if (pid_alive(start)) {
3530 pos = next_thread(start);
3531 if (thread_group_leader(pos))
3534 get_task_struct(pos);
3537 put_task_struct(start);
3541 /* for the /proc/TGID/task/ directories */
3542 static int proc_task_readdir(struct file *file, struct dir_context *ctx)
3544 struct inode *inode = file_inode(file);
3545 struct task_struct *task;
3546 struct pid_namespace *ns;
3549 if (proc_inode_is_dead(inode))
3552 if (!dir_emit_dots(file, ctx))
3555 /* f_version caches the tgid value that the last readdir call couldn't
3556 * return. lseek aka telldir automagically resets f_version to 0.
3558 ns = inode->i_sb->s_fs_info;
3559 tid = (int)file->f_version;
3560 file->f_version = 0;
3561 for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
3563 task = next_tid(task), ctx->pos++) {
3564 char name[PROC_NUMBUF];
3566 tid = task_pid_nr_ns(task, ns);
3567 len = snprintf(name, sizeof(name), "%d", tid);
3568 if (!proc_fill_cache(file, ctx, name, len,
3569 proc_task_instantiate, task, NULL)) {
3570 /* returning this tgid failed, save it as the first
3571 * pid for the next readir call */
3572 file->f_version = (u64)tid;
3573 put_task_struct(task);
3581 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3583 struct inode *inode = d_inode(dentry);
3584 struct task_struct *p = get_proc_task(inode);
3585 generic_fillattr(inode, stat);
3588 stat->nlink += get_nr_threads(p);
3595 static const struct inode_operations proc_task_inode_operations = {
3596 .lookup = proc_task_lookup,
3597 .getattr = proc_task_getattr,
3598 .setattr = proc_setattr,
3599 .permission = proc_pid_permission,
3602 static const struct file_operations proc_task_operations = {
3603 .read = generic_read_dir,
3604 .iterate_shared = proc_task_readdir,
3605 .llseek = generic_file_llseek,