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 <linux/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/sched/autogroup.h>
89 #include <linux/flex_array.h>
90 #include <linux/posix-timers.h>
91 #ifdef CONFIG_HARDWALL
92 #include <asm/hardwall.h>
94 #include <trace/events/oom.h>
99 * Implementing inode permission operations in /proc is almost
100 * certainly an error. Permission checks need to happen during
101 * each system call not at open time. The reason is that most of
102 * what we wish to check for permissions in /proc varies at runtime.
104 * The classic example of a problem is opening file descriptors
105 * in /proc for a task before it execs a suid executable.
109 static u8 nlink_tgid;
115 const struct inode_operations *iop;
116 const struct file_operations *fop;
120 #define NOD(NAME, MODE, IOP, FOP, OP) { \
122 .len = sizeof(NAME) - 1, \
129 #define DIR(NAME, MODE, iops, fops) \
130 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
131 #define LNK(NAME, get_link) \
132 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
133 &proc_pid_link_inode_operations, NULL, \
134 { .proc_get_link = get_link } )
135 #define REG(NAME, MODE, fops) \
136 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
137 #define ONE(NAME, MODE, show) \
138 NOD(NAME, (S_IFREG|(MODE)), \
139 NULL, &proc_single_file_operations, \
140 { .proc_show = show } )
143 * Count the number of hardlinks for the pid_entry table, excluding the .
146 static unsigned int __init pid_entry_nlink(const struct pid_entry *entries,
153 for (i = 0; i < n; ++i) {
154 if (S_ISDIR(entries[i].mode))
161 static int get_task_root(struct task_struct *task, struct path *root)
163 int result = -ENOENT;
167 get_fs_root(task->fs, root);
174 static int proc_cwd_link(struct dentry *dentry, struct path *path)
176 struct task_struct *task = get_proc_task(d_inode(dentry));
177 int result = -ENOENT;
182 get_fs_pwd(task->fs, path);
186 put_task_struct(task);
191 static int proc_root_link(struct dentry *dentry, struct path *path)
193 struct task_struct *task = get_proc_task(d_inode(dentry));
194 int result = -ENOENT;
197 result = get_task_root(task, path);
198 put_task_struct(task);
203 static ssize_t proc_pid_cmdline_read(struct file *file, char __user *buf,
204 size_t _count, loff_t *pos)
206 struct task_struct *tsk;
207 struct mm_struct *mm;
209 unsigned long count = _count;
210 unsigned long arg_start, arg_end, env_start, env_end;
211 unsigned long len1, len2, len;
218 tsk = get_proc_task(file_inode(file));
221 mm = get_task_mm(tsk);
222 put_task_struct(tsk);
225 /* Check if process spawned far enough to have cmdline. */
231 page = (char *)__get_free_page(GFP_TEMPORARY);
237 down_read(&mm->mmap_sem);
238 arg_start = mm->arg_start;
239 arg_end = mm->arg_end;
240 env_start = mm->env_start;
241 env_end = mm->env_end;
242 up_read(&mm->mmap_sem);
244 BUG_ON(arg_start > arg_end);
245 BUG_ON(env_start > env_end);
247 len1 = arg_end - arg_start;
248 len2 = env_end - env_start;
256 * Inherently racy -- command line shares address space
257 * with code and data.
259 rv = access_remote_vm(mm, arg_end - 1, &c, 1, 0);
266 /* Command line (set of strings) occupies whole ARGV. */
270 p = arg_start + *pos;
272 while (count > 0 && len > 0) {
276 _count = min3(count, len, PAGE_SIZE);
277 nr_read = access_remote_vm(mm, p, page, _count, 0);
283 if (copy_to_user(buf, page, nr_read)) {
296 * Command line (1 string) occupies ARGV and
303 { .p = arg_start, .len = len1 },
304 { .p = env_start, .len = len2 },
310 while (i < 2 && pos1 >= cmdline[i].len) {
311 pos1 -= cmdline[i].len;
315 p = cmdline[i].p + pos1;
316 len = cmdline[i].len - pos1;
317 while (count > 0 && len > 0) {
318 unsigned int _count, l;
322 _count = min3(count, len, PAGE_SIZE);
323 nr_read = access_remote_vm(mm, p, page, _count, 0);
330 * Command line can be shorter than whole ARGV
331 * even if last "marker" byte says it is not.
334 l = strnlen(page, nr_read);
340 if (copy_to_user(buf, page, nr_read)) {
355 /* Only first chunk can be read partially. */
362 free_page((unsigned long)page);
370 static const struct file_operations proc_pid_cmdline_ops = {
371 .read = proc_pid_cmdline_read,
372 .llseek = generic_file_llseek,
375 #ifdef CONFIG_KALLSYMS
377 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
378 * Returns the resolved symbol. If that fails, simply return the address.
380 static int proc_pid_wchan(struct seq_file *m, struct pid_namespace *ns,
381 struct pid *pid, struct task_struct *task)
384 char symname[KSYM_NAME_LEN];
386 wchan = get_wchan(task);
388 if (wchan && ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)
389 && !lookup_symbol_name(wchan, symname))
390 seq_printf(m, "%s", symname);
396 #endif /* CONFIG_KALLSYMS */
398 static int lock_trace(struct task_struct *task)
400 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
403 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH_FSCREDS)) {
404 mutex_unlock(&task->signal->cred_guard_mutex);
410 static void unlock_trace(struct task_struct *task)
412 mutex_unlock(&task->signal->cred_guard_mutex);
415 #ifdef CONFIG_STACKTRACE
417 #define MAX_STACK_TRACE_DEPTH 64
419 static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
420 struct pid *pid, struct task_struct *task)
422 struct stack_trace trace;
423 unsigned long *entries;
427 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
431 trace.nr_entries = 0;
432 trace.max_entries = MAX_STACK_TRACE_DEPTH;
433 trace.entries = entries;
436 err = lock_trace(task);
438 save_stack_trace_tsk(task, &trace);
440 for (i = 0; i < trace.nr_entries; i++) {
441 seq_printf(m, "[<%pK>] %pB\n",
442 (void *)entries[i], (void *)entries[i]);
452 #ifdef CONFIG_SCHED_INFO
454 * Provides /proc/PID/schedstat
456 static int proc_pid_schedstat(struct seq_file *m, struct pid_namespace *ns,
457 struct pid *pid, struct task_struct *task)
459 if (unlikely(!sched_info_on()))
460 seq_printf(m, "0 0 0\n");
462 seq_printf(m, "%llu %llu %lu\n",
463 (unsigned long long)task->se.sum_exec_runtime,
464 (unsigned long long)task->sched_info.run_delay,
465 task->sched_info.pcount);
471 #ifdef CONFIG_LATENCYTOP
472 static int lstats_show_proc(struct seq_file *m, void *v)
475 struct inode *inode = m->private;
476 struct task_struct *task = get_proc_task(inode);
480 seq_puts(m, "Latency Top version : v0.1\n");
481 for (i = 0; i < 32; i++) {
482 struct latency_record *lr = &task->latency_record[i];
483 if (lr->backtrace[0]) {
485 seq_printf(m, "%i %li %li",
486 lr->count, lr->time, lr->max);
487 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
488 unsigned long bt = lr->backtrace[q];
493 seq_printf(m, " %ps", (void *)bt);
499 put_task_struct(task);
503 static int lstats_open(struct inode *inode, struct file *file)
505 return single_open(file, lstats_show_proc, inode);
508 static ssize_t lstats_write(struct file *file, const char __user *buf,
509 size_t count, loff_t *offs)
511 struct task_struct *task = get_proc_task(file_inode(file));
515 clear_all_latency_tracing(task);
516 put_task_struct(task);
521 static const struct file_operations proc_lstats_operations = {
524 .write = lstats_write,
526 .release = single_release,
531 static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns,
532 struct pid *pid, struct task_struct *task)
534 unsigned long totalpages = totalram_pages + total_swap_pages;
535 unsigned long points = 0;
537 points = oom_badness(task, NULL, NULL, totalpages) *
539 seq_printf(m, "%lu\n", points);
549 static const struct limit_names lnames[RLIM_NLIMITS] = {
550 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
551 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
552 [RLIMIT_DATA] = {"Max data size", "bytes"},
553 [RLIMIT_STACK] = {"Max stack size", "bytes"},
554 [RLIMIT_CORE] = {"Max core file size", "bytes"},
555 [RLIMIT_RSS] = {"Max resident set", "bytes"},
556 [RLIMIT_NPROC] = {"Max processes", "processes"},
557 [RLIMIT_NOFILE] = {"Max open files", "files"},
558 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
559 [RLIMIT_AS] = {"Max address space", "bytes"},
560 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
561 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
562 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
563 [RLIMIT_NICE] = {"Max nice priority", NULL},
564 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
565 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
568 /* Display limits for a process */
569 static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
570 struct pid *pid, struct task_struct *task)
575 struct rlimit rlim[RLIM_NLIMITS];
577 if (!lock_task_sighand(task, &flags))
579 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
580 unlock_task_sighand(task, &flags);
583 * print the file header
585 seq_printf(m, "%-25s %-20s %-20s %-10s\n",
586 "Limit", "Soft Limit", "Hard Limit", "Units");
588 for (i = 0; i < RLIM_NLIMITS; i++) {
589 if (rlim[i].rlim_cur == RLIM_INFINITY)
590 seq_printf(m, "%-25s %-20s ",
591 lnames[i].name, "unlimited");
593 seq_printf(m, "%-25s %-20lu ",
594 lnames[i].name, rlim[i].rlim_cur);
596 if (rlim[i].rlim_max == RLIM_INFINITY)
597 seq_printf(m, "%-20s ", "unlimited");
599 seq_printf(m, "%-20lu ", rlim[i].rlim_max);
602 seq_printf(m, "%-10s\n", lnames[i].unit);
610 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
611 static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
612 struct pid *pid, struct task_struct *task)
615 unsigned long args[6], sp, pc;
618 res = lock_trace(task);
622 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
623 seq_puts(m, "running\n");
625 seq_printf(m, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
628 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
630 args[0], args[1], args[2], args[3], args[4], args[5],
636 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
638 /************************************************************************/
639 /* Here the fs part begins */
640 /************************************************************************/
642 /* permission checks */
643 static int proc_fd_access_allowed(struct inode *inode)
645 struct task_struct *task;
647 /* Allow access to a task's file descriptors if it is us or we
648 * may use ptrace attach to the process and find out that
651 task = get_proc_task(inode);
653 allowed = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
654 put_task_struct(task);
659 int proc_setattr(struct dentry *dentry, struct iattr *attr)
662 struct inode *inode = d_inode(dentry);
664 if (attr->ia_valid & ATTR_MODE)
667 error = setattr_prepare(dentry, attr);
671 setattr_copy(inode, attr);
672 mark_inode_dirty(inode);
677 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
678 * or euid/egid (for hide_pid_min=2)?
680 static bool has_pid_permissions(struct pid_namespace *pid,
681 struct task_struct *task,
684 if (pid->hide_pid < hide_pid_min)
686 if (in_group_p(pid->pid_gid))
688 return ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
692 static int proc_pid_permission(struct inode *inode, int mask)
694 struct pid_namespace *pid = inode->i_sb->s_fs_info;
695 struct task_struct *task;
698 task = get_proc_task(inode);
701 has_perms = has_pid_permissions(pid, task, HIDEPID_NO_ACCESS);
702 put_task_struct(task);
705 if (pid->hide_pid == HIDEPID_INVISIBLE) {
707 * Let's make getdents(), stat(), and open()
708 * consistent with each other. If a process
709 * may not stat() a file, it shouldn't be seen
717 return generic_permission(inode, mask);
722 static const struct inode_operations proc_def_inode_operations = {
723 .setattr = proc_setattr,
726 static int proc_single_show(struct seq_file *m, void *v)
728 struct inode *inode = m->private;
729 struct pid_namespace *ns;
731 struct task_struct *task;
734 ns = inode->i_sb->s_fs_info;
735 pid = proc_pid(inode);
736 task = get_pid_task(pid, PIDTYPE_PID);
740 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
742 put_task_struct(task);
746 static int proc_single_open(struct inode *inode, struct file *filp)
748 return single_open(filp, proc_single_show, inode);
751 static const struct file_operations proc_single_file_operations = {
752 .open = proc_single_open,
755 .release = single_release,
759 struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode)
761 struct task_struct *task = get_proc_task(inode);
762 struct mm_struct *mm = ERR_PTR(-ESRCH);
765 mm = mm_access(task, mode | PTRACE_MODE_FSCREDS);
766 put_task_struct(task);
768 if (!IS_ERR_OR_NULL(mm)) {
769 /* ensure this mm_struct can't be freed */
771 /* but do not pin its memory */
779 static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
781 struct mm_struct *mm = proc_mem_open(inode, mode);
786 file->private_data = mm;
790 static int mem_open(struct inode *inode, struct file *file)
792 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
794 /* OK to pass negative loff_t, we can catch out-of-range */
795 file->f_mode |= FMODE_UNSIGNED_OFFSET;
800 static ssize_t mem_rw(struct file *file, char __user *buf,
801 size_t count, loff_t *ppos, int write)
803 struct mm_struct *mm = file->private_data;
804 unsigned long addr = *ppos;
812 page = (char *)__get_free_page(GFP_TEMPORARY);
817 if (!mmget_not_zero(mm))
820 /* Maybe we should limit FOLL_FORCE to actual ptrace users? */
826 int this_len = min_t(int, count, PAGE_SIZE);
828 if (write && copy_from_user(page, buf, this_len)) {
833 this_len = access_remote_vm(mm, addr, page, this_len, flags);
840 if (!write && copy_to_user(buf, page, this_len)) {
854 free_page((unsigned long) page);
858 static ssize_t mem_read(struct file *file, char __user *buf,
859 size_t count, loff_t *ppos)
861 return mem_rw(file, buf, count, ppos, 0);
864 static ssize_t mem_write(struct file *file, const char __user *buf,
865 size_t count, loff_t *ppos)
867 return mem_rw(file, (char __user*)buf, count, ppos, 1);
870 loff_t mem_lseek(struct file *file, loff_t offset, int orig)
874 file->f_pos = offset;
877 file->f_pos += offset;
882 force_successful_syscall_return();
886 static int mem_release(struct inode *inode, struct file *file)
888 struct mm_struct *mm = file->private_data;
894 static const struct file_operations proc_mem_operations = {
899 .release = mem_release,
902 static int environ_open(struct inode *inode, struct file *file)
904 return __mem_open(inode, file, PTRACE_MODE_READ);
907 static ssize_t environ_read(struct file *file, char __user *buf,
908 size_t count, loff_t *ppos)
911 unsigned long src = *ppos;
913 struct mm_struct *mm = file->private_data;
914 unsigned long env_start, env_end;
916 /* Ensure the process spawned far enough to have an environment. */
917 if (!mm || !mm->env_end)
920 page = (char *)__get_free_page(GFP_TEMPORARY);
925 if (!mmget_not_zero(mm))
928 down_read(&mm->mmap_sem);
929 env_start = mm->env_start;
930 env_end = mm->env_end;
931 up_read(&mm->mmap_sem);
934 size_t this_len, max_len;
937 if (src >= (env_end - env_start))
940 this_len = env_end - (env_start + src);
942 max_len = min_t(size_t, PAGE_SIZE, count);
943 this_len = min(max_len, this_len);
945 retval = access_remote_vm(mm, (env_start + src), page, this_len, 0);
952 if (copy_to_user(buf, page, retval)) {
966 free_page((unsigned long) page);
970 static const struct file_operations proc_environ_operations = {
971 .open = environ_open,
972 .read = environ_read,
973 .llseek = generic_file_llseek,
974 .release = mem_release,
977 static int auxv_open(struct inode *inode, struct file *file)
979 return __mem_open(inode, file, PTRACE_MODE_READ_FSCREDS);
982 static ssize_t auxv_read(struct file *file, char __user *buf,
983 size_t count, loff_t *ppos)
985 struct mm_struct *mm = file->private_data;
986 unsigned int nwords = 0;
992 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
993 return simple_read_from_buffer(buf, count, ppos, mm->saved_auxv,
994 nwords * sizeof(mm->saved_auxv[0]));
997 static const struct file_operations proc_auxv_operations = {
1000 .llseek = generic_file_llseek,
1001 .release = mem_release,
1004 static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
1007 struct task_struct *task = get_proc_task(file_inode(file));
1008 char buffer[PROC_NUMBUF];
1009 int oom_adj = OOM_ADJUST_MIN;
1014 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
1015 oom_adj = OOM_ADJUST_MAX;
1017 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
1019 put_task_struct(task);
1020 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
1021 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1024 static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
1026 static DEFINE_MUTEX(oom_adj_mutex);
1027 struct mm_struct *mm = NULL;
1028 struct task_struct *task;
1031 task = get_proc_task(file_inode(file));
1035 mutex_lock(&oom_adj_mutex);
1037 if (oom_adj < task->signal->oom_score_adj &&
1038 !capable(CAP_SYS_RESOURCE)) {
1043 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
1044 * /proc/pid/oom_score_adj instead.
1046 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
1047 current->comm, task_pid_nr(current), task_pid_nr(task),
1050 if ((short)oom_adj < task->signal->oom_score_adj_min &&
1051 !capable(CAP_SYS_RESOURCE)) {
1058 * Make sure we will check other processes sharing the mm if this is
1059 * not vfrok which wants its own oom_score_adj.
1060 * pin the mm so it doesn't go away and get reused after task_unlock
1062 if (!task->vfork_done) {
1063 struct task_struct *p = find_lock_task_mm(task);
1066 if (atomic_read(&p->mm->mm_users) > 1) {
1074 task->signal->oom_score_adj = oom_adj;
1075 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1076 task->signal->oom_score_adj_min = (short)oom_adj;
1077 trace_oom_score_adj_update(task);
1080 struct task_struct *p;
1083 for_each_process(p) {
1084 if (same_thread_group(task, p))
1087 /* do not touch kernel threads or the global init */
1088 if (p->flags & PF_KTHREAD || is_global_init(p))
1092 if (!p->vfork_done && process_shares_mm(p, mm)) {
1093 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",
1094 task_pid_nr(p), p->comm,
1095 p->signal->oom_score_adj, oom_adj,
1096 task_pid_nr(task), task->comm);
1097 p->signal->oom_score_adj = oom_adj;
1098 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1099 p->signal->oom_score_adj_min = (short)oom_adj;
1107 mutex_unlock(&oom_adj_mutex);
1108 put_task_struct(task);
1113 * /proc/pid/oom_adj exists solely for backwards compatibility with previous
1114 * kernels. The effective policy is defined by oom_score_adj, which has a
1115 * different scale: oom_adj grew exponentially and oom_score_adj grows linearly.
1116 * Values written to oom_adj are simply mapped linearly to oom_score_adj.
1117 * Processes that become oom disabled via oom_adj will still be oom disabled
1118 * with this implementation.
1120 * oom_adj cannot be removed since existing userspace binaries use it.
1122 static ssize_t oom_adj_write(struct file *file, const char __user *buf,
1123 size_t count, loff_t *ppos)
1125 char buffer[PROC_NUMBUF];
1129 memset(buffer, 0, sizeof(buffer));
1130 if (count > sizeof(buffer) - 1)
1131 count = sizeof(buffer) - 1;
1132 if (copy_from_user(buffer, buf, count)) {
1137 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
1140 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
1141 oom_adj != OOM_DISABLE) {
1147 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1148 * value is always attainable.
1150 if (oom_adj == OOM_ADJUST_MAX)
1151 oom_adj = OOM_SCORE_ADJ_MAX;
1153 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
1155 err = __set_oom_adj(file, oom_adj, true);
1157 return err < 0 ? err : count;
1160 static const struct file_operations proc_oom_adj_operations = {
1161 .read = oom_adj_read,
1162 .write = oom_adj_write,
1163 .llseek = generic_file_llseek,
1166 static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1167 size_t count, loff_t *ppos)
1169 struct task_struct *task = get_proc_task(file_inode(file));
1170 char buffer[PROC_NUMBUF];
1171 short oom_score_adj = OOM_SCORE_ADJ_MIN;
1176 oom_score_adj = task->signal->oom_score_adj;
1177 put_task_struct(task);
1178 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
1179 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1182 static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1183 size_t count, loff_t *ppos)
1185 char buffer[PROC_NUMBUF];
1189 memset(buffer, 0, sizeof(buffer));
1190 if (count > sizeof(buffer) - 1)
1191 count = sizeof(buffer) - 1;
1192 if (copy_from_user(buffer, buf, count)) {
1197 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
1200 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
1201 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1206 err = __set_oom_adj(file, oom_score_adj, false);
1208 return err < 0 ? err : count;
1211 static const struct file_operations proc_oom_score_adj_operations = {
1212 .read = oom_score_adj_read,
1213 .write = oom_score_adj_write,
1214 .llseek = default_llseek,
1217 #ifdef CONFIG_AUDITSYSCALL
1218 #define TMPBUFLEN 11
1219 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1220 size_t count, loff_t *ppos)
1222 struct inode * inode = file_inode(file);
1223 struct task_struct *task = get_proc_task(inode);
1225 char tmpbuf[TMPBUFLEN];
1229 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1230 from_kuid(file->f_cred->user_ns,
1231 audit_get_loginuid(task)));
1232 put_task_struct(task);
1233 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1236 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1237 size_t count, loff_t *ppos)
1239 struct inode * inode = file_inode(file);
1245 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1252 /* No partial writes. */
1256 rv = kstrtou32_from_user(buf, count, 10, &loginuid);
1260 /* is userspace tring to explicitly UNSET the loginuid? */
1261 if (loginuid == AUDIT_UID_UNSET) {
1262 kloginuid = INVALID_UID;
1264 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
1265 if (!uid_valid(kloginuid))
1269 rv = audit_set_loginuid(kloginuid);
1275 static const struct file_operations proc_loginuid_operations = {
1276 .read = proc_loginuid_read,
1277 .write = proc_loginuid_write,
1278 .llseek = generic_file_llseek,
1281 static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1282 size_t count, loff_t *ppos)
1284 struct inode * inode = file_inode(file);
1285 struct task_struct *task = get_proc_task(inode);
1287 char tmpbuf[TMPBUFLEN];
1291 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1292 audit_get_sessionid(task));
1293 put_task_struct(task);
1294 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1297 static const struct file_operations proc_sessionid_operations = {
1298 .read = proc_sessionid_read,
1299 .llseek = generic_file_llseek,
1303 #ifdef CONFIG_FAULT_INJECTION
1304 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1305 size_t count, loff_t *ppos)
1307 struct task_struct *task = get_proc_task(file_inode(file));
1308 char buffer[PROC_NUMBUF];
1314 make_it_fail = task->make_it_fail;
1315 put_task_struct(task);
1317 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1319 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1322 static ssize_t proc_fault_inject_write(struct file * file,
1323 const char __user * buf, size_t count, loff_t *ppos)
1325 struct task_struct *task;
1326 char buffer[PROC_NUMBUF];
1330 if (!capable(CAP_SYS_RESOURCE))
1332 memset(buffer, 0, sizeof(buffer));
1333 if (count > sizeof(buffer) - 1)
1334 count = sizeof(buffer) - 1;
1335 if (copy_from_user(buffer, buf, count))
1337 rv = kstrtoint(strstrip(buffer), 0, &make_it_fail);
1340 if (make_it_fail < 0 || make_it_fail > 1)
1343 task = get_proc_task(file_inode(file));
1346 task->make_it_fail = make_it_fail;
1347 put_task_struct(task);
1352 static const struct file_operations proc_fault_inject_operations = {
1353 .read = proc_fault_inject_read,
1354 .write = proc_fault_inject_write,
1355 .llseek = generic_file_llseek,
1360 #ifdef CONFIG_SCHED_DEBUG
1362 * Print out various scheduling related per-task fields:
1364 static int sched_show(struct seq_file *m, void *v)
1366 struct inode *inode = m->private;
1367 struct task_struct *p;
1369 p = get_proc_task(inode);
1372 proc_sched_show_task(p, m);
1380 sched_write(struct file *file, const char __user *buf,
1381 size_t count, loff_t *offset)
1383 struct inode *inode = file_inode(file);
1384 struct task_struct *p;
1386 p = get_proc_task(inode);
1389 proc_sched_set_task(p);
1396 static int sched_open(struct inode *inode, struct file *filp)
1398 return single_open(filp, sched_show, inode);
1401 static const struct file_operations proc_pid_sched_operations = {
1404 .write = sched_write,
1405 .llseek = seq_lseek,
1406 .release = single_release,
1411 #ifdef CONFIG_SCHED_AUTOGROUP
1413 * Print out autogroup related information:
1415 static int sched_autogroup_show(struct seq_file *m, void *v)
1417 struct inode *inode = m->private;
1418 struct task_struct *p;
1420 p = get_proc_task(inode);
1423 proc_sched_autogroup_show_task(p, m);
1431 sched_autogroup_write(struct file *file, const char __user *buf,
1432 size_t count, loff_t *offset)
1434 struct inode *inode = file_inode(file);
1435 struct task_struct *p;
1436 char buffer[PROC_NUMBUF];
1440 memset(buffer, 0, sizeof(buffer));
1441 if (count > sizeof(buffer) - 1)
1442 count = sizeof(buffer) - 1;
1443 if (copy_from_user(buffer, buf, count))
1446 err = kstrtoint(strstrip(buffer), 0, &nice);
1450 p = get_proc_task(inode);
1454 err = proc_sched_autogroup_set_nice(p, nice);
1463 static int sched_autogroup_open(struct inode *inode, struct file *filp)
1467 ret = single_open(filp, sched_autogroup_show, NULL);
1469 struct seq_file *m = filp->private_data;
1476 static const struct file_operations proc_pid_sched_autogroup_operations = {
1477 .open = sched_autogroup_open,
1479 .write = sched_autogroup_write,
1480 .llseek = seq_lseek,
1481 .release = single_release,
1484 #endif /* CONFIG_SCHED_AUTOGROUP */
1486 static ssize_t comm_write(struct file *file, const char __user *buf,
1487 size_t count, loff_t *offset)
1489 struct inode *inode = file_inode(file);
1490 struct task_struct *p;
1491 char buffer[TASK_COMM_LEN];
1492 const size_t maxlen = sizeof(buffer) - 1;
1494 memset(buffer, 0, sizeof(buffer));
1495 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
1498 p = get_proc_task(inode);
1502 if (same_thread_group(current, p))
1503 set_task_comm(p, buffer);
1512 static int comm_show(struct seq_file *m, void *v)
1514 struct inode *inode = m->private;
1515 struct task_struct *p;
1517 p = get_proc_task(inode);
1522 seq_printf(m, "%s\n", p->comm);
1530 static int comm_open(struct inode *inode, struct file *filp)
1532 return single_open(filp, comm_show, inode);
1535 static const struct file_operations proc_pid_set_comm_operations = {
1538 .write = comm_write,
1539 .llseek = seq_lseek,
1540 .release = single_release,
1543 static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
1545 struct task_struct *task;
1546 struct file *exe_file;
1548 task = get_proc_task(d_inode(dentry));
1551 exe_file = get_task_exe_file(task);
1552 put_task_struct(task);
1554 *exe_path = exe_file->f_path;
1555 path_get(&exe_file->f_path);
1562 static const char *proc_pid_get_link(struct dentry *dentry,
1563 struct inode *inode,
1564 struct delayed_call *done)
1567 int error = -EACCES;
1570 return ERR_PTR(-ECHILD);
1572 /* Are we allowed to snoop on the tasks file descriptors? */
1573 if (!proc_fd_access_allowed(inode))
1576 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1580 nd_jump_link(&path);
1583 return ERR_PTR(error);
1586 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1588 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
1595 pathname = d_path(path, tmp, PAGE_SIZE);
1596 len = PTR_ERR(pathname);
1597 if (IS_ERR(pathname))
1599 len = tmp + PAGE_SIZE - 1 - pathname;
1603 if (copy_to_user(buffer, pathname, len))
1606 free_page((unsigned long)tmp);
1610 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1612 int error = -EACCES;
1613 struct inode *inode = d_inode(dentry);
1616 /* Are we allowed to snoop on the tasks file descriptors? */
1617 if (!proc_fd_access_allowed(inode))
1620 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1624 error = do_proc_readlink(&path, buffer, buflen);
1630 const struct inode_operations proc_pid_link_inode_operations = {
1631 .readlink = proc_pid_readlink,
1632 .get_link = proc_pid_get_link,
1633 .setattr = proc_setattr,
1637 /* building an inode */
1639 void task_dump_owner(struct task_struct *task, mode_t mode,
1640 kuid_t *ruid, kgid_t *rgid)
1642 /* Depending on the state of dumpable compute who should own a
1643 * proc file for a task.
1645 const struct cred *cred;
1649 /* Default to the tasks effective ownership */
1651 cred = __task_cred(task);
1657 * Before the /proc/pid/status file was created the only way to read
1658 * the effective uid of a /process was to stat /proc/pid. Reading
1659 * /proc/pid/status is slow enough that procps and other packages
1660 * kept stating /proc/pid. To keep the rules in /proc simple I have
1661 * made this apply to all per process world readable and executable
1664 if (mode != (S_IFDIR|S_IRUGO|S_IXUGO)) {
1665 struct mm_struct *mm;
1668 /* Make non-dumpable tasks owned by some root */
1670 if (get_dumpable(mm) != SUID_DUMP_USER) {
1671 struct user_namespace *user_ns = mm->user_ns;
1673 uid = make_kuid(user_ns, 0);
1674 if (!uid_valid(uid))
1675 uid = GLOBAL_ROOT_UID;
1677 gid = make_kgid(user_ns, 0);
1678 if (!gid_valid(gid))
1679 gid = GLOBAL_ROOT_GID;
1682 uid = GLOBAL_ROOT_UID;
1683 gid = GLOBAL_ROOT_GID;
1691 struct inode *proc_pid_make_inode(struct super_block * sb,
1692 struct task_struct *task, umode_t mode)
1694 struct inode * inode;
1695 struct proc_inode *ei;
1697 /* We need a new inode */
1699 inode = new_inode(sb);
1705 inode->i_mode = mode;
1706 inode->i_ino = get_next_ino();
1707 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
1708 inode->i_op = &proc_def_inode_operations;
1711 * grab the reference to task.
1713 ei->pid = get_task_pid(task, PIDTYPE_PID);
1717 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
1718 security_task_to_inode(task, inode);
1728 int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1730 struct inode *inode = d_inode(dentry);
1731 struct task_struct *task;
1732 struct pid_namespace *pid = dentry->d_sb->s_fs_info;
1734 generic_fillattr(inode, stat);
1737 stat->uid = GLOBAL_ROOT_UID;
1738 stat->gid = GLOBAL_ROOT_GID;
1739 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1741 if (!has_pid_permissions(pid, task, HIDEPID_INVISIBLE)) {
1744 * This doesn't prevent learning whether PID exists,
1745 * it only makes getattr() consistent with readdir().
1749 task_dump_owner(task, inode->i_mode, &stat->uid, &stat->gid);
1758 * Exceptional case: normally we are not allowed to unhash a busy
1759 * directory. In this case, however, we can do it - no aliasing problems
1760 * due to the way we treat inodes.
1762 * Rewrite the inode's ownerships here because the owning task may have
1763 * performed a setuid(), etc.
1766 int pid_revalidate(struct dentry *dentry, unsigned int flags)
1768 struct inode *inode;
1769 struct task_struct *task;
1771 if (flags & LOOKUP_RCU)
1774 inode = d_inode(dentry);
1775 task = get_proc_task(inode);
1778 task_dump_owner(task, inode->i_mode, &inode->i_uid, &inode->i_gid);
1780 inode->i_mode &= ~(S_ISUID | S_ISGID);
1781 security_task_to_inode(task, inode);
1782 put_task_struct(task);
1788 static inline bool proc_inode_is_dead(struct inode *inode)
1790 return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1793 int pid_delete_dentry(const struct dentry *dentry)
1795 /* Is the task we represent dead?
1796 * If so, then don't put the dentry on the lru list,
1797 * kill it immediately.
1799 return proc_inode_is_dead(d_inode(dentry));
1802 const struct dentry_operations pid_dentry_operations =
1804 .d_revalidate = pid_revalidate,
1805 .d_delete = pid_delete_dentry,
1811 * Fill a directory entry.
1813 * If possible create the dcache entry and derive our inode number and
1814 * file type from dcache entry.
1816 * Since all of the proc inode numbers are dynamically generated, the inode
1817 * numbers do not exist until the inode is cache. This means creating the
1818 * the dcache entry in readdir is necessary to keep the inode numbers
1819 * reported by readdir in sync with the inode numbers reported
1822 bool proc_fill_cache(struct file *file, struct dir_context *ctx,
1823 const char *name, int len,
1824 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1826 struct dentry *child, *dir = file->f_path.dentry;
1827 struct qstr qname = QSTR_INIT(name, len);
1828 struct inode *inode;
1832 child = d_hash_and_lookup(dir, &qname);
1834 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1835 child = d_alloc_parallel(dir, &qname, &wq);
1837 goto end_instantiate;
1838 if (d_in_lookup(child)) {
1839 int err = instantiate(d_inode(dir), child, task, ptr);
1840 d_lookup_done(child);
1843 goto end_instantiate;
1847 inode = d_inode(child);
1849 type = inode->i_mode >> 12;
1851 return dir_emit(ctx, name, len, ino, type);
1854 return dir_emit(ctx, name, len, 1, DT_UNKNOWN);
1858 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1859 * which represent vma start and end addresses.
1861 static int dname_to_vma_addr(struct dentry *dentry,
1862 unsigned long *start, unsigned long *end)
1864 if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2)
1870 static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
1872 unsigned long vm_start, vm_end;
1873 bool exact_vma_exists = false;
1874 struct mm_struct *mm = NULL;
1875 struct task_struct *task;
1876 struct inode *inode;
1879 if (flags & LOOKUP_RCU)
1882 inode = d_inode(dentry);
1883 task = get_proc_task(inode);
1887 mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
1888 if (IS_ERR_OR_NULL(mm))
1891 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1892 down_read(&mm->mmap_sem);
1893 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1894 up_read(&mm->mmap_sem);
1899 if (exact_vma_exists) {
1900 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
1902 security_task_to_inode(task, inode);
1907 put_task_struct(task);
1913 static const struct dentry_operations tid_map_files_dentry_operations = {
1914 .d_revalidate = map_files_d_revalidate,
1915 .d_delete = pid_delete_dentry,
1918 static int map_files_get_link(struct dentry *dentry, struct path *path)
1920 unsigned long vm_start, vm_end;
1921 struct vm_area_struct *vma;
1922 struct task_struct *task;
1923 struct mm_struct *mm;
1927 task = get_proc_task(d_inode(dentry));
1931 mm = get_task_mm(task);
1932 put_task_struct(task);
1936 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
1941 down_read(&mm->mmap_sem);
1942 vma = find_exact_vma(mm, vm_start, vm_end);
1943 if (vma && vma->vm_file) {
1944 *path = vma->vm_file->f_path;
1948 up_read(&mm->mmap_sem);
1956 struct map_files_info {
1959 unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
1963 * Only allow CAP_SYS_ADMIN to follow the links, due to concerns about how the
1964 * symlinks may be used to bypass permissions on ancestor directories in the
1965 * path to the file in question.
1968 proc_map_files_get_link(struct dentry *dentry,
1969 struct inode *inode,
1970 struct delayed_call *done)
1972 if (!capable(CAP_SYS_ADMIN))
1973 return ERR_PTR(-EPERM);
1975 return proc_pid_get_link(dentry, inode, done);
1979 * Identical to proc_pid_link_inode_operations except for get_link()
1981 static const struct inode_operations proc_map_files_link_inode_operations = {
1982 .readlink = proc_pid_readlink,
1983 .get_link = proc_map_files_get_link,
1984 .setattr = proc_setattr,
1988 proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
1989 struct task_struct *task, const void *ptr)
1991 fmode_t mode = (fmode_t)(unsigned long)ptr;
1992 struct proc_inode *ei;
1993 struct inode *inode;
1995 inode = proc_pid_make_inode(dir->i_sb, task, S_IFLNK |
1996 ((mode & FMODE_READ ) ? S_IRUSR : 0) |
1997 ((mode & FMODE_WRITE) ? S_IWUSR : 0));
2002 ei->op.proc_get_link = map_files_get_link;
2004 inode->i_op = &proc_map_files_link_inode_operations;
2007 d_set_d_op(dentry, &tid_map_files_dentry_operations);
2008 d_add(dentry, inode);
2013 static struct dentry *proc_map_files_lookup(struct inode *dir,
2014 struct dentry *dentry, unsigned int flags)
2016 unsigned long vm_start, vm_end;
2017 struct vm_area_struct *vma;
2018 struct task_struct *task;
2020 struct mm_struct *mm;
2023 task = get_proc_task(dir);
2028 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
2032 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
2035 mm = get_task_mm(task);
2039 down_read(&mm->mmap_sem);
2040 vma = find_exact_vma(mm, vm_start, vm_end);
2045 result = proc_map_files_instantiate(dir, dentry, task,
2046 (void *)(unsigned long)vma->vm_file->f_mode);
2049 up_read(&mm->mmap_sem);
2052 put_task_struct(task);
2054 return ERR_PTR(result);
2057 static const struct inode_operations proc_map_files_inode_operations = {
2058 .lookup = proc_map_files_lookup,
2059 .permission = proc_fd_permission,
2060 .setattr = proc_setattr,
2064 proc_map_files_readdir(struct file *file, struct dir_context *ctx)
2066 struct vm_area_struct *vma;
2067 struct task_struct *task;
2068 struct mm_struct *mm;
2069 unsigned long nr_files, pos, i;
2070 struct flex_array *fa = NULL;
2071 struct map_files_info info;
2072 struct map_files_info *p;
2076 task = get_proc_task(file_inode(file));
2081 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
2085 if (!dir_emit_dots(file, ctx))
2088 mm = get_task_mm(task);
2091 down_read(&mm->mmap_sem);
2096 * We need two passes here:
2098 * 1) Collect vmas of mapped files with mmap_sem taken
2099 * 2) Release mmap_sem and instantiate entries
2101 * otherwise we get lockdep complained, since filldir()
2102 * routine might require mmap_sem taken in might_fault().
2105 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
2106 if (vma->vm_file && ++pos > ctx->pos)
2111 fa = flex_array_alloc(sizeof(info), nr_files,
2113 if (!fa || flex_array_prealloc(fa, 0, nr_files,
2117 flex_array_free(fa);
2118 up_read(&mm->mmap_sem);
2122 for (i = 0, vma = mm->mmap, pos = 2; vma;
2123 vma = vma->vm_next) {
2126 if (++pos <= ctx->pos)
2129 info.mode = vma->vm_file->f_mode;
2130 info.len = snprintf(info.name,
2131 sizeof(info.name), "%lx-%lx",
2132 vma->vm_start, vma->vm_end);
2133 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
2137 up_read(&mm->mmap_sem);
2139 for (i = 0; i < nr_files; i++) {
2140 p = flex_array_get(fa, i);
2141 if (!proc_fill_cache(file, ctx,
2143 proc_map_files_instantiate,
2145 (void *)(unsigned long)p->mode))
2150 flex_array_free(fa);
2154 put_task_struct(task);
2159 static const struct file_operations proc_map_files_operations = {
2160 .read = generic_read_dir,
2161 .iterate_shared = proc_map_files_readdir,
2162 .llseek = generic_file_llseek,
2165 #if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
2166 struct timers_private {
2168 struct task_struct *task;
2169 struct sighand_struct *sighand;
2170 struct pid_namespace *ns;
2171 unsigned long flags;
2174 static void *timers_start(struct seq_file *m, loff_t *pos)
2176 struct timers_private *tp = m->private;
2178 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2180 return ERR_PTR(-ESRCH);
2182 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2184 return ERR_PTR(-ESRCH);
2186 return seq_list_start(&tp->task->signal->posix_timers, *pos);
2189 static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2191 struct timers_private *tp = m->private;
2192 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2195 static void timers_stop(struct seq_file *m, void *v)
2197 struct timers_private *tp = m->private;
2200 unlock_task_sighand(tp->task, &tp->flags);
2205 put_task_struct(tp->task);
2210 static int show_timer(struct seq_file *m, void *v)
2212 struct k_itimer *timer;
2213 struct timers_private *tp = m->private;
2215 static const char * const nstr[] = {
2216 [SIGEV_SIGNAL] = "signal",
2217 [SIGEV_NONE] = "none",
2218 [SIGEV_THREAD] = "thread",
2221 timer = list_entry((struct list_head *)v, struct k_itimer, list);
2222 notify = timer->it_sigev_notify;
2224 seq_printf(m, "ID: %d\n", timer->it_id);
2225 seq_printf(m, "signal: %d/%p\n",
2226 timer->sigq->info.si_signo,
2227 timer->sigq->info.si_value.sival_ptr);
2228 seq_printf(m, "notify: %s/%s.%d\n",
2229 nstr[notify & ~SIGEV_THREAD_ID],
2230 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2231 pid_nr_ns(timer->it_pid, tp->ns));
2232 seq_printf(m, "ClockID: %d\n", timer->it_clock);
2237 static const struct seq_operations proc_timers_seq_ops = {
2238 .start = timers_start,
2239 .next = timers_next,
2240 .stop = timers_stop,
2244 static int proc_timers_open(struct inode *inode, struct file *file)
2246 struct timers_private *tp;
2248 tp = __seq_open_private(file, &proc_timers_seq_ops,
2249 sizeof(struct timers_private));
2253 tp->pid = proc_pid(inode);
2254 tp->ns = inode->i_sb->s_fs_info;
2258 static const struct file_operations proc_timers_operations = {
2259 .open = proc_timers_open,
2261 .llseek = seq_lseek,
2262 .release = seq_release_private,
2266 static ssize_t timerslack_ns_write(struct file *file, const char __user *buf,
2267 size_t count, loff_t *offset)
2269 struct inode *inode = file_inode(file);
2270 struct task_struct *p;
2274 err = kstrtoull_from_user(buf, count, 10, &slack_ns);
2278 p = get_proc_task(inode);
2283 if (!capable(CAP_SYS_NICE)) {
2288 err = security_task_setscheduler(p);
2297 p->timer_slack_ns = p->default_timer_slack_ns;
2299 p->timer_slack_ns = slack_ns;
2308 static int timerslack_ns_show(struct seq_file *m, void *v)
2310 struct inode *inode = m->private;
2311 struct task_struct *p;
2314 p = get_proc_task(inode);
2320 if (!capable(CAP_SYS_NICE)) {
2324 err = security_task_getscheduler(p);
2330 seq_printf(m, "%llu\n", p->timer_slack_ns);
2339 static int timerslack_ns_open(struct inode *inode, struct file *filp)
2341 return single_open(filp, timerslack_ns_show, inode);
2344 static const struct file_operations proc_pid_set_timerslack_ns_operations = {
2345 .open = timerslack_ns_open,
2347 .write = timerslack_ns_write,
2348 .llseek = seq_lseek,
2349 .release = single_release,
2352 static int proc_pident_instantiate(struct inode *dir,
2353 struct dentry *dentry, struct task_struct *task, const void *ptr)
2355 const struct pid_entry *p = ptr;
2356 struct inode *inode;
2357 struct proc_inode *ei;
2359 inode = proc_pid_make_inode(dir->i_sb, task, p->mode);
2364 if (S_ISDIR(inode->i_mode))
2365 set_nlink(inode, 2); /* Use getattr to fix if necessary */
2367 inode->i_op = p->iop;
2369 inode->i_fop = p->fop;
2371 d_set_d_op(dentry, &pid_dentry_operations);
2372 d_add(dentry, inode);
2373 /* Close the race of the process dying before we return the dentry */
2374 if (pid_revalidate(dentry, 0))
2380 static struct dentry *proc_pident_lookup(struct inode *dir,
2381 struct dentry *dentry,
2382 const struct pid_entry *ents,
2386 struct task_struct *task = get_proc_task(dir);
2387 const struct pid_entry *p, *last;
2395 * Yes, it does not scale. And it should not. Don't add
2396 * new entries into /proc/<tgid>/ without very good reasons.
2398 last = &ents[nents];
2399 for (p = ents; p < last; p++) {
2400 if (p->len != dentry->d_name.len)
2402 if (!memcmp(dentry->d_name.name, p->name, p->len))
2408 error = proc_pident_instantiate(dir, dentry, task, p);
2410 put_task_struct(task);
2412 return ERR_PTR(error);
2415 static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
2416 const struct pid_entry *ents, unsigned int nents)
2418 struct task_struct *task = get_proc_task(file_inode(file));
2419 const struct pid_entry *p;
2424 if (!dir_emit_dots(file, ctx))
2427 if (ctx->pos >= nents + 2)
2430 for (p = ents + (ctx->pos - 2); p < ents + nents; p++) {
2431 if (!proc_fill_cache(file, ctx, p->name, p->len,
2432 proc_pident_instantiate, task, p))
2437 put_task_struct(task);
2441 #ifdef CONFIG_SECURITY
2442 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2443 size_t count, loff_t *ppos)
2445 struct inode * inode = file_inode(file);
2448 struct task_struct *task = get_proc_task(inode);
2453 length = security_getprocattr(task,
2454 (char*)file->f_path.dentry->d_name.name,
2456 put_task_struct(task);
2458 length = simple_read_from_buffer(buf, count, ppos, p, length);
2463 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2464 size_t count, loff_t *ppos)
2466 struct inode * inode = file_inode(file);
2469 struct task_struct *task = get_proc_task(inode);
2475 /* A task may only write its own attributes. */
2477 if (current != task)
2480 if (count > PAGE_SIZE)
2483 /* No partial writes. */
2488 page = memdup_user(buf, count);
2490 length = PTR_ERR(page);
2494 /* Guard against adverse ptrace interaction */
2495 length = mutex_lock_interruptible(¤t->signal->cred_guard_mutex);
2499 length = security_setprocattr(file->f_path.dentry->d_name.name,
2501 mutex_unlock(¤t->signal->cred_guard_mutex);
2505 put_task_struct(task);
2510 static const struct file_operations proc_pid_attr_operations = {
2511 .read = proc_pid_attr_read,
2512 .write = proc_pid_attr_write,
2513 .llseek = generic_file_llseek,
2516 static const struct pid_entry attr_dir_stuff[] = {
2517 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2518 REG("prev", S_IRUGO, proc_pid_attr_operations),
2519 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2520 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2521 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2522 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2525 static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
2527 return proc_pident_readdir(file, ctx,
2528 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2531 static const struct file_operations proc_attr_dir_operations = {
2532 .read = generic_read_dir,
2533 .iterate_shared = proc_attr_dir_readdir,
2534 .llseek = generic_file_llseek,
2537 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2538 struct dentry *dentry, unsigned int flags)
2540 return proc_pident_lookup(dir, dentry,
2541 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2544 static const struct inode_operations proc_attr_dir_inode_operations = {
2545 .lookup = proc_attr_dir_lookup,
2546 .getattr = pid_getattr,
2547 .setattr = proc_setattr,
2552 #ifdef CONFIG_ELF_CORE
2553 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2554 size_t count, loff_t *ppos)
2556 struct task_struct *task = get_proc_task(file_inode(file));
2557 struct mm_struct *mm;
2558 char buffer[PROC_NUMBUF];
2566 mm = get_task_mm(task);
2568 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2569 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2570 MMF_DUMP_FILTER_SHIFT));
2572 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2575 put_task_struct(task);
2580 static ssize_t proc_coredump_filter_write(struct file *file,
2581 const char __user *buf,
2585 struct task_struct *task;
2586 struct mm_struct *mm;
2592 ret = kstrtouint_from_user(buf, count, 0, &val);
2597 task = get_proc_task(file_inode(file));
2601 mm = get_task_mm(task);
2606 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2608 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2610 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2615 put_task_struct(task);
2622 static const struct file_operations proc_coredump_filter_operations = {
2623 .read = proc_coredump_filter_read,
2624 .write = proc_coredump_filter_write,
2625 .llseek = generic_file_llseek,
2629 #ifdef CONFIG_TASK_IO_ACCOUNTING
2630 static int do_io_accounting(struct task_struct *task, struct seq_file *m, int whole)
2632 struct task_io_accounting acct = task->ioac;
2633 unsigned long flags;
2636 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2640 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
2645 if (whole && lock_task_sighand(task, &flags)) {
2646 struct task_struct *t = task;
2648 task_io_accounting_add(&acct, &task->signal->ioac);
2649 while_each_thread(task, t)
2650 task_io_accounting_add(&acct, &t->ioac);
2652 unlock_task_sighand(task, &flags);
2659 "read_bytes: %llu\n"
2660 "write_bytes: %llu\n"
2661 "cancelled_write_bytes: %llu\n",
2662 (unsigned long long)acct.rchar,
2663 (unsigned long long)acct.wchar,
2664 (unsigned long long)acct.syscr,
2665 (unsigned long long)acct.syscw,
2666 (unsigned long long)acct.read_bytes,
2667 (unsigned long long)acct.write_bytes,
2668 (unsigned long long)acct.cancelled_write_bytes);
2672 mutex_unlock(&task->signal->cred_guard_mutex);
2676 static int proc_tid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2677 struct pid *pid, struct task_struct *task)
2679 return do_io_accounting(task, m, 0);
2682 static int proc_tgid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2683 struct pid *pid, struct task_struct *task)
2685 return do_io_accounting(task, m, 1);
2687 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2689 #ifdef CONFIG_USER_NS
2690 static int proc_id_map_open(struct inode *inode, struct file *file,
2691 const struct seq_operations *seq_ops)
2693 struct user_namespace *ns = NULL;
2694 struct task_struct *task;
2695 struct seq_file *seq;
2698 task = get_proc_task(inode);
2701 ns = get_user_ns(task_cred_xxx(task, user_ns));
2703 put_task_struct(task);
2708 ret = seq_open(file, seq_ops);
2712 seq = file->private_data;
2722 static int proc_id_map_release(struct inode *inode, struct file *file)
2724 struct seq_file *seq = file->private_data;
2725 struct user_namespace *ns = seq->private;
2727 return seq_release(inode, file);
2730 static int proc_uid_map_open(struct inode *inode, struct file *file)
2732 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2735 static int proc_gid_map_open(struct inode *inode, struct file *file)
2737 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2740 static int proc_projid_map_open(struct inode *inode, struct file *file)
2742 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2745 static const struct file_operations proc_uid_map_operations = {
2746 .open = proc_uid_map_open,
2747 .write = proc_uid_map_write,
2749 .llseek = seq_lseek,
2750 .release = proc_id_map_release,
2753 static const struct file_operations proc_gid_map_operations = {
2754 .open = proc_gid_map_open,
2755 .write = proc_gid_map_write,
2757 .llseek = seq_lseek,
2758 .release = proc_id_map_release,
2761 static const struct file_operations proc_projid_map_operations = {
2762 .open = proc_projid_map_open,
2763 .write = proc_projid_map_write,
2765 .llseek = seq_lseek,
2766 .release = proc_id_map_release,
2769 static int proc_setgroups_open(struct inode *inode, struct file *file)
2771 struct user_namespace *ns = NULL;
2772 struct task_struct *task;
2776 task = get_proc_task(inode);
2779 ns = get_user_ns(task_cred_xxx(task, user_ns));
2781 put_task_struct(task);
2786 if (file->f_mode & FMODE_WRITE) {
2788 if (!ns_capable(ns, CAP_SYS_ADMIN))
2792 ret = single_open(file, &proc_setgroups_show, ns);
2803 static int proc_setgroups_release(struct inode *inode, struct file *file)
2805 struct seq_file *seq = file->private_data;
2806 struct user_namespace *ns = seq->private;
2807 int ret = single_release(inode, file);
2812 static const struct file_operations proc_setgroups_operations = {
2813 .open = proc_setgroups_open,
2814 .write = proc_setgroups_write,
2816 .llseek = seq_lseek,
2817 .release = proc_setgroups_release,
2819 #endif /* CONFIG_USER_NS */
2821 static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2822 struct pid *pid, struct task_struct *task)
2824 int err = lock_trace(task);
2826 seq_printf(m, "%08x\n", task->personality);
2835 static const struct file_operations proc_task_operations;
2836 static const struct inode_operations proc_task_inode_operations;
2838 static const struct pid_entry tgid_base_stuff[] = {
2839 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2840 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2841 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
2842 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2843 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
2845 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2847 REG("environ", S_IRUSR, proc_environ_operations),
2848 REG("auxv", S_IRUSR, proc_auxv_operations),
2849 ONE("status", S_IRUGO, proc_pid_status),
2850 ONE("personality", S_IRUSR, proc_pid_personality),
2851 ONE("limits", S_IRUGO, proc_pid_limits),
2852 #ifdef CONFIG_SCHED_DEBUG
2853 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2855 #ifdef CONFIG_SCHED_AUTOGROUP
2856 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2858 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2859 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2860 ONE("syscall", S_IRUSR, proc_pid_syscall),
2862 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
2863 ONE("stat", S_IRUGO, proc_tgid_stat),
2864 ONE("statm", S_IRUGO, proc_pid_statm),
2865 REG("maps", S_IRUGO, proc_pid_maps_operations),
2867 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
2869 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2870 LNK("cwd", proc_cwd_link),
2871 LNK("root", proc_root_link),
2872 LNK("exe", proc_exe_link),
2873 REG("mounts", S_IRUGO, proc_mounts_operations),
2874 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2875 REG("mountstats", S_IRUSR, proc_mountstats_operations),
2876 #ifdef CONFIG_PROC_PAGE_MONITOR
2877 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2878 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
2879 REG("pagemap", S_IRUSR, proc_pagemap_operations),
2881 #ifdef CONFIG_SECURITY
2882 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
2884 #ifdef CONFIG_KALLSYMS
2885 ONE("wchan", S_IRUGO, proc_pid_wchan),
2887 #ifdef CONFIG_STACKTRACE
2888 ONE("stack", S_IRUSR, proc_pid_stack),
2890 #ifdef CONFIG_SCHED_INFO
2891 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
2893 #ifdef CONFIG_LATENCYTOP
2894 REG("latency", S_IRUGO, proc_lstats_operations),
2896 #ifdef CONFIG_PROC_PID_CPUSET
2897 ONE("cpuset", S_IRUGO, proc_cpuset_show),
2899 #ifdef CONFIG_CGROUPS
2900 ONE("cgroup", S_IRUGO, proc_cgroup_show),
2902 ONE("oom_score", S_IRUGO, proc_oom_score),
2903 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
2904 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
2905 #ifdef CONFIG_AUDITSYSCALL
2906 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2907 REG("sessionid", S_IRUGO, proc_sessionid_operations),
2909 #ifdef CONFIG_FAULT_INJECTION
2910 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2912 #ifdef CONFIG_ELF_CORE
2913 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
2915 #ifdef CONFIG_TASK_IO_ACCOUNTING
2916 ONE("io", S_IRUSR, proc_tgid_io_accounting),
2918 #ifdef CONFIG_HARDWALL
2919 ONE("hardwall", S_IRUGO, proc_pid_hardwall),
2921 #ifdef CONFIG_USER_NS
2922 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2923 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
2924 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
2925 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
2927 #if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
2928 REG("timers", S_IRUGO, proc_timers_operations),
2930 REG("timerslack_ns", S_IRUGO|S_IWUGO, proc_pid_set_timerslack_ns_operations),
2933 static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
2935 return proc_pident_readdir(file, ctx,
2936 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2939 static const struct file_operations proc_tgid_base_operations = {
2940 .read = generic_read_dir,
2941 .iterate_shared = proc_tgid_base_readdir,
2942 .llseek = generic_file_llseek,
2945 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2947 return proc_pident_lookup(dir, dentry,
2948 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2951 static const struct inode_operations proc_tgid_base_inode_operations = {
2952 .lookup = proc_tgid_base_lookup,
2953 .getattr = pid_getattr,
2954 .setattr = proc_setattr,
2955 .permission = proc_pid_permission,
2958 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
2960 struct dentry *dentry, *leader, *dir;
2961 char buf[PROC_NUMBUF];
2965 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2966 /* no ->d_hash() rejects on procfs */
2967 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
2969 d_invalidate(dentry);
2977 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2978 leader = d_hash_and_lookup(mnt->mnt_root, &name);
2983 name.len = strlen(name.name);
2984 dir = d_hash_and_lookup(leader, &name);
2986 goto out_put_leader;
2989 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2990 dentry = d_hash_and_lookup(dir, &name);
2992 d_invalidate(dentry);
3004 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
3005 * @task: task that should be flushed.
3007 * When flushing dentries from proc, one needs to flush them from global
3008 * proc (proc_mnt) and from all the namespaces' procs this task was seen
3009 * in. This call is supposed to do all of this job.
3011 * Looks in the dcache for
3013 * /proc/@tgid/task/@pid
3014 * if either directory is present flushes it and all of it'ts children
3017 * It is safe and reasonable to cache /proc entries for a task until
3018 * that task exits. After that they just clog up the dcache with
3019 * useless entries, possibly causing useful dcache entries to be
3020 * flushed instead. This routine is proved to flush those useless
3021 * dcache entries at process exit time.
3023 * NOTE: This routine is just an optimization so it does not guarantee
3024 * that no dcache entries will exist at process exit time it
3025 * just makes it very unlikely that any will persist.
3028 void proc_flush_task(struct task_struct *task)
3031 struct pid *pid, *tgid;
3034 pid = task_pid(task);
3035 tgid = task_tgid(task);
3037 for (i = 0; i <= pid->level; i++) {
3038 upid = &pid->numbers[i];
3039 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
3040 tgid->numbers[i].nr);
3044 static int proc_pid_instantiate(struct inode *dir,
3045 struct dentry * dentry,
3046 struct task_struct *task, const void *ptr)
3048 struct inode *inode;
3050 inode = proc_pid_make_inode(dir->i_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
3054 inode->i_op = &proc_tgid_base_inode_operations;
3055 inode->i_fop = &proc_tgid_base_operations;
3056 inode->i_flags|=S_IMMUTABLE;
3058 set_nlink(inode, nlink_tgid);
3060 d_set_d_op(dentry, &pid_dentry_operations);
3062 d_add(dentry, inode);
3063 /* Close the race of the process dying before we return the dentry */
3064 if (pid_revalidate(dentry, 0))
3070 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
3072 int result = -ENOENT;
3073 struct task_struct *task;
3075 struct pid_namespace *ns;
3077 tgid = name_to_int(&dentry->d_name);
3081 ns = dentry->d_sb->s_fs_info;
3083 task = find_task_by_pid_ns(tgid, ns);
3085 get_task_struct(task);
3090 result = proc_pid_instantiate(dir, dentry, task, NULL);
3091 put_task_struct(task);
3093 return ERR_PTR(result);
3097 * Find the first task with tgid >= tgid
3102 struct task_struct *task;
3104 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3109 put_task_struct(iter.task);
3113 pid = find_ge_pid(iter.tgid, ns);
3115 iter.tgid = pid_nr_ns(pid, ns);
3116 iter.task = pid_task(pid, PIDTYPE_PID);
3117 /* What we to know is if the pid we have find is the
3118 * pid of a thread_group_leader. Testing for task
3119 * being a thread_group_leader is the obvious thing
3120 * todo but there is a window when it fails, due to
3121 * the pid transfer logic in de_thread.
3123 * So we perform the straight forward test of seeing
3124 * if the pid we have found is the pid of a thread
3125 * group leader, and don't worry if the task we have
3126 * found doesn't happen to be a thread group leader.
3127 * As we don't care in the case of readdir.
3129 if (!iter.task || !has_group_leader_pid(iter.task)) {
3133 get_task_struct(iter.task);
3139 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
3141 /* for the /proc/ directory itself, after non-process stuff has been done */
3142 int proc_pid_readdir(struct file *file, struct dir_context *ctx)
3144 struct tgid_iter iter;
3145 struct pid_namespace *ns = file_inode(file)->i_sb->s_fs_info;
3146 loff_t pos = ctx->pos;
3148 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
3151 if (pos == TGID_OFFSET - 2) {
3152 struct inode *inode = d_inode(ns->proc_self);
3153 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
3155 ctx->pos = pos = pos + 1;
3157 if (pos == TGID_OFFSET - 1) {
3158 struct inode *inode = d_inode(ns->proc_thread_self);
3159 if (!dir_emit(ctx, "thread-self", 11, inode->i_ino, DT_LNK))
3161 ctx->pos = pos = pos + 1;
3163 iter.tgid = pos - TGID_OFFSET;
3165 for (iter = next_tgid(ns, iter);
3167 iter.tgid += 1, iter = next_tgid(ns, iter)) {
3168 char name[PROC_NUMBUF];
3172 if (!has_pid_permissions(ns, iter.task, HIDEPID_INVISIBLE))
3175 len = snprintf(name, sizeof(name), "%d", iter.tgid);
3176 ctx->pos = iter.tgid + TGID_OFFSET;
3177 if (!proc_fill_cache(file, ctx, name, len,
3178 proc_pid_instantiate, iter.task, NULL)) {
3179 put_task_struct(iter.task);
3183 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
3188 * proc_tid_comm_permission is a special permission function exclusively
3189 * used for the node /proc/<pid>/task/<tid>/comm.
3190 * It bypasses generic permission checks in the case where a task of the same
3191 * task group attempts to access the node.
3192 * The rationale behind this is that glibc and bionic access this node for
3193 * cross thread naming (pthread_set/getname_np(!self)). However, if
3194 * PR_SET_DUMPABLE gets set to 0 this node among others becomes uid=0 gid=0,
3195 * which locks out the cross thread naming implementation.
3196 * This function makes sure that the node is always accessible for members of
3197 * same thread group.
3199 static int proc_tid_comm_permission(struct inode *inode, int mask)
3201 bool is_same_tgroup;
3202 struct task_struct *task;
3204 task = get_proc_task(inode);
3207 is_same_tgroup = same_thread_group(current, task);
3208 put_task_struct(task);
3210 if (likely(is_same_tgroup && !(mask & MAY_EXEC))) {
3211 /* This file (/proc/<pid>/task/<tid>/comm) can always be
3212 * read or written by the members of the corresponding
3218 return generic_permission(inode, mask);
3221 static const struct inode_operations proc_tid_comm_inode_operations = {
3222 .permission = proc_tid_comm_permission,
3228 static const struct pid_entry tid_base_stuff[] = {
3229 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
3230 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
3231 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
3233 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
3235 REG("environ", S_IRUSR, proc_environ_operations),
3236 REG("auxv", S_IRUSR, proc_auxv_operations),
3237 ONE("status", S_IRUGO, proc_pid_status),
3238 ONE("personality", S_IRUSR, proc_pid_personality),
3239 ONE("limits", S_IRUGO, proc_pid_limits),
3240 #ifdef CONFIG_SCHED_DEBUG
3241 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
3243 NOD("comm", S_IFREG|S_IRUGO|S_IWUSR,
3244 &proc_tid_comm_inode_operations,
3245 &proc_pid_set_comm_operations, {}),
3246 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3247 ONE("syscall", S_IRUSR, proc_pid_syscall),
3249 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
3250 ONE("stat", S_IRUGO, proc_tid_stat),
3251 ONE("statm", S_IRUGO, proc_pid_statm),
3252 REG("maps", S_IRUGO, proc_tid_maps_operations),
3253 #ifdef CONFIG_PROC_CHILDREN
3254 REG("children", S_IRUGO, proc_tid_children_operations),
3257 REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
3259 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3260 LNK("cwd", proc_cwd_link),
3261 LNK("root", proc_root_link),
3262 LNK("exe", proc_exe_link),
3263 REG("mounts", S_IRUGO, proc_mounts_operations),
3264 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
3265 #ifdef CONFIG_PROC_PAGE_MONITOR
3266 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3267 REG("smaps", S_IRUGO, proc_tid_smaps_operations),
3268 REG("pagemap", S_IRUSR, proc_pagemap_operations),
3270 #ifdef CONFIG_SECURITY
3271 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
3273 #ifdef CONFIG_KALLSYMS
3274 ONE("wchan", S_IRUGO, proc_pid_wchan),
3276 #ifdef CONFIG_STACKTRACE
3277 ONE("stack", S_IRUSR, proc_pid_stack),
3279 #ifdef CONFIG_SCHED_INFO
3280 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
3282 #ifdef CONFIG_LATENCYTOP
3283 REG("latency", S_IRUGO, proc_lstats_operations),
3285 #ifdef CONFIG_PROC_PID_CPUSET
3286 ONE("cpuset", S_IRUGO, proc_cpuset_show),
3288 #ifdef CONFIG_CGROUPS
3289 ONE("cgroup", S_IRUGO, proc_cgroup_show),
3291 ONE("oom_score", S_IRUGO, proc_oom_score),
3292 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
3293 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
3294 #ifdef CONFIG_AUDITSYSCALL
3295 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3296 REG("sessionid", S_IRUGO, proc_sessionid_operations),
3298 #ifdef CONFIG_FAULT_INJECTION
3299 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3301 #ifdef CONFIG_TASK_IO_ACCOUNTING
3302 ONE("io", S_IRUSR, proc_tid_io_accounting),
3304 #ifdef CONFIG_HARDWALL
3305 ONE("hardwall", S_IRUGO, proc_pid_hardwall),
3307 #ifdef CONFIG_USER_NS
3308 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3309 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
3310 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
3311 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
3315 static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
3317 return proc_pident_readdir(file, ctx,
3318 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3321 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3323 return proc_pident_lookup(dir, dentry,
3324 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3327 static const struct file_operations proc_tid_base_operations = {
3328 .read = generic_read_dir,
3329 .iterate_shared = proc_tid_base_readdir,
3330 .llseek = generic_file_llseek,
3333 static const struct inode_operations proc_tid_base_inode_operations = {
3334 .lookup = proc_tid_base_lookup,
3335 .getattr = pid_getattr,
3336 .setattr = proc_setattr,
3339 static int proc_task_instantiate(struct inode *dir,
3340 struct dentry *dentry, struct task_struct *task, const void *ptr)
3342 struct inode *inode;
3343 inode = proc_pid_make_inode(dir->i_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
3347 inode->i_op = &proc_tid_base_inode_operations;
3348 inode->i_fop = &proc_tid_base_operations;
3349 inode->i_flags|=S_IMMUTABLE;
3351 set_nlink(inode, nlink_tid);
3353 d_set_d_op(dentry, &pid_dentry_operations);
3355 d_add(dentry, inode);
3356 /* Close the race of the process dying before we return the dentry */
3357 if (pid_revalidate(dentry, 0))
3363 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
3365 int result = -ENOENT;
3366 struct task_struct *task;
3367 struct task_struct *leader = get_proc_task(dir);
3369 struct pid_namespace *ns;
3374 tid = name_to_int(&dentry->d_name);
3378 ns = dentry->d_sb->s_fs_info;
3380 task = find_task_by_pid_ns(tid, ns);
3382 get_task_struct(task);
3386 if (!same_thread_group(leader, task))
3389 result = proc_task_instantiate(dir, dentry, task, NULL);
3391 put_task_struct(task);
3393 put_task_struct(leader);
3395 return ERR_PTR(result);
3399 * Find the first tid of a thread group to return to user space.
3401 * Usually this is just the thread group leader, but if the users
3402 * buffer was too small or there was a seek into the middle of the
3403 * directory we have more work todo.
3405 * In the case of a short read we start with find_task_by_pid.
3407 * In the case of a seek we start with the leader and walk nr
3410 static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
3411 struct pid_namespace *ns)
3413 struct task_struct *pos, *task;
3414 unsigned long nr = f_pos;
3416 if (nr != f_pos) /* 32bit overflow? */
3420 task = pid_task(pid, PIDTYPE_PID);
3424 /* Attempt to start with the tid of a thread */
3426 pos = find_task_by_pid_ns(tid, ns);
3427 if (pos && same_thread_group(pos, task))
3431 /* If nr exceeds the number of threads there is nothing todo */
3432 if (nr >= get_nr_threads(task))
3435 /* If we haven't found our starting place yet start
3436 * with the leader and walk nr threads forward.
3438 pos = task = task->group_leader;
3442 } while_each_thread(task, pos);
3447 get_task_struct(pos);
3454 * Find the next thread in the thread list.
3455 * Return NULL if there is an error or no next thread.
3457 * The reference to the input task_struct is released.
3459 static struct task_struct *next_tid(struct task_struct *start)
3461 struct task_struct *pos = NULL;
3463 if (pid_alive(start)) {
3464 pos = next_thread(start);
3465 if (thread_group_leader(pos))
3468 get_task_struct(pos);
3471 put_task_struct(start);
3475 /* for the /proc/TGID/task/ directories */
3476 static int proc_task_readdir(struct file *file, struct dir_context *ctx)
3478 struct inode *inode = file_inode(file);
3479 struct task_struct *task;
3480 struct pid_namespace *ns;
3483 if (proc_inode_is_dead(inode))
3486 if (!dir_emit_dots(file, ctx))
3489 /* f_version caches the tgid value that the last readdir call couldn't
3490 * return. lseek aka telldir automagically resets f_version to 0.
3492 ns = inode->i_sb->s_fs_info;
3493 tid = (int)file->f_version;
3494 file->f_version = 0;
3495 for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
3497 task = next_tid(task), ctx->pos++) {
3498 char name[PROC_NUMBUF];
3500 tid = task_pid_nr_ns(task, ns);
3501 len = snprintf(name, sizeof(name), "%d", tid);
3502 if (!proc_fill_cache(file, ctx, name, len,
3503 proc_task_instantiate, task, NULL)) {
3504 /* returning this tgid failed, save it as the first
3505 * pid for the next readir call */
3506 file->f_version = (u64)tid;
3507 put_task_struct(task);
3515 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3517 struct inode *inode = d_inode(dentry);
3518 struct task_struct *p = get_proc_task(inode);
3519 generic_fillattr(inode, stat);
3522 stat->nlink += get_nr_threads(p);
3529 static const struct inode_operations proc_task_inode_operations = {
3530 .lookup = proc_task_lookup,
3531 .getattr = proc_task_getattr,
3532 .setattr = proc_setattr,
3533 .permission = proc_pid_permission,
3536 static const struct file_operations proc_task_operations = {
3537 .read = generic_read_dir,
3538 .iterate_shared = proc_task_readdir,
3539 .llseek = generic_file_llseek,
3542 void __init set_proc_pid_nlink(void)
3544 nlink_tid = pid_entry_nlink(tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3545 nlink_tgid = pid_entry_nlink(tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));