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>
98 * Implementing inode permission operations in /proc is almost
99 * certainly an error. Permission checks need to happen during
100 * each system call not at open time. The reason is that most of
101 * what we wish to check for permissions in /proc varies at runtime.
103 * The classic example of a problem is opening file descriptors
104 * in /proc for a task before it execs a suid executable.
111 const struct inode_operations *iop;
112 const struct file_operations *fop;
116 #define NOD(NAME, MODE, IOP, FOP, OP) { \
118 .len = sizeof(NAME) - 1, \
125 #define DIR(NAME, MODE, iops, fops) \
126 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
127 #define LNK(NAME, get_link) \
128 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
129 &proc_pid_link_inode_operations, NULL, \
130 { .proc_get_link = get_link } )
131 #define REG(NAME, MODE, fops) \
132 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
133 #define ONE(NAME, MODE, show) \
134 NOD(NAME, (S_IFREG|(MODE)), \
135 NULL, &proc_single_file_operations, \
136 { .proc_show = show } )
139 * Count the number of hardlinks for the pid_entry table, excluding the .
142 static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
149 for (i = 0; i < n; ++i) {
150 if (S_ISDIR(entries[i].mode))
157 static int get_task_root(struct task_struct *task, struct path *root)
159 int result = -ENOENT;
163 get_fs_root(task->fs, root);
170 static int proc_cwd_link(struct dentry *dentry, struct path *path)
172 struct task_struct *task = get_proc_task(d_inode(dentry));
173 int result = -ENOENT;
178 get_fs_pwd(task->fs, path);
182 put_task_struct(task);
187 static int proc_root_link(struct dentry *dentry, struct path *path)
189 struct task_struct *task = get_proc_task(d_inode(dentry));
190 int result = -ENOENT;
193 result = get_task_root(task, path);
194 put_task_struct(task);
199 static ssize_t proc_pid_cmdline_read(struct file *file, char __user *buf,
200 size_t _count, loff_t *pos)
202 struct task_struct *tsk;
203 struct mm_struct *mm;
205 unsigned long count = _count;
206 unsigned long arg_start, arg_end, env_start, env_end;
207 unsigned long len1, len2, len;
214 tsk = get_proc_task(file_inode(file));
217 mm = get_task_mm(tsk);
218 put_task_struct(tsk);
221 /* Check if process spawned far enough to have cmdline. */
227 page = (char *)__get_free_page(GFP_TEMPORARY);
233 down_read(&mm->mmap_sem);
234 arg_start = mm->arg_start;
235 arg_end = mm->arg_end;
236 env_start = mm->env_start;
237 env_end = mm->env_end;
238 up_read(&mm->mmap_sem);
240 BUG_ON(arg_start > arg_end);
241 BUG_ON(env_start > env_end);
243 len1 = arg_end - arg_start;
244 len2 = env_end - env_start;
247 * Inherently racy -- command line shares address space
248 * with code and data.
250 rv = access_remote_vm(mm, arg_end - 1, &c, 1, 0);
257 /* Command line (set of strings) occupies whole ARGV. */
261 p = arg_start + *pos;
263 while (count > 0 && len > 0) {
267 _count = min3(count, len, PAGE_SIZE);
268 nr_read = access_remote_vm(mm, p, page, _count, 0);
274 if (copy_to_user(buf, page, nr_read)) {
287 * Command line (1 string) occupies ARGV and maybe
290 if (len1 + len2 <= *pos)
295 p = arg_start + *pos;
297 while (count > 0 && len > 0) {
298 unsigned int _count, l;
302 _count = min3(count, len, PAGE_SIZE);
303 nr_read = access_remote_vm(mm, p, page, _count, 0);
310 * Command line can be shorter than whole ARGV
311 * even if last "marker" byte says it is not.
314 l = strnlen(page, nr_read);
320 if (copy_to_user(buf, page, nr_read)) {
336 * Command line (1 string) occupies ARGV and
340 p = env_start + *pos - len1;
341 len = len1 + len2 - *pos;
346 while (count > 0 && len > 0) {
347 unsigned int _count, l;
351 _count = min3(count, len, PAGE_SIZE);
352 nr_read = access_remote_vm(mm, p, page, _count, 0);
360 l = strnlen(page, nr_read);
366 if (copy_to_user(buf, page, nr_read)) {
385 free_page((unsigned long)page);
393 static const struct file_operations proc_pid_cmdline_ops = {
394 .read = proc_pid_cmdline_read,
395 .llseek = generic_file_llseek,
398 static int proc_pid_auxv(struct seq_file *m, struct pid_namespace *ns,
399 struct pid *pid, struct task_struct *task)
401 struct mm_struct *mm = mm_access(task, PTRACE_MODE_READ);
402 if (mm && !IS_ERR(mm)) {
403 unsigned int nwords = 0;
406 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
407 seq_write(m, mm->saved_auxv, nwords * sizeof(mm->saved_auxv[0]));
415 #ifdef CONFIG_KALLSYMS
417 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
418 * Returns the resolved symbol. If that fails, simply return the address.
420 static int proc_pid_wchan(struct seq_file *m, struct pid_namespace *ns,
421 struct pid *pid, struct task_struct *task)
424 char symname[KSYM_NAME_LEN];
426 wchan = get_wchan(task);
428 if (lookup_symbol_name(wchan, symname) < 0) {
429 if (!ptrace_may_access(task, PTRACE_MODE_READ))
431 seq_printf(m, "%lu", wchan);
433 seq_printf(m, "%s", symname);
438 #endif /* CONFIG_KALLSYMS */
440 static int lock_trace(struct task_struct *task)
442 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
445 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH)) {
446 mutex_unlock(&task->signal->cred_guard_mutex);
452 static void unlock_trace(struct task_struct *task)
454 mutex_unlock(&task->signal->cred_guard_mutex);
457 #ifdef CONFIG_STACKTRACE
459 #define MAX_STACK_TRACE_DEPTH 64
461 static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
462 struct pid *pid, struct task_struct *task)
464 struct stack_trace trace;
465 unsigned long *entries;
469 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
473 trace.nr_entries = 0;
474 trace.max_entries = MAX_STACK_TRACE_DEPTH;
475 trace.entries = entries;
478 err = lock_trace(task);
480 save_stack_trace_tsk(task, &trace);
482 for (i = 0; i < trace.nr_entries; i++) {
483 seq_printf(m, "[<%pK>] %pS\n",
484 (void *)entries[i], (void *)entries[i]);
494 #ifdef CONFIG_SCHEDSTATS
496 * Provides /proc/PID/schedstat
498 static int proc_pid_schedstat(struct seq_file *m, struct pid_namespace *ns,
499 struct pid *pid, struct task_struct *task)
501 seq_printf(m, "%llu %llu %lu\n",
502 (unsigned long long)task->se.sum_exec_runtime,
503 (unsigned long long)task->sched_info.run_delay,
504 task->sched_info.pcount);
510 #ifdef CONFIG_LATENCYTOP
511 static int lstats_show_proc(struct seq_file *m, void *v)
514 struct inode *inode = m->private;
515 struct task_struct *task = get_proc_task(inode);
519 seq_puts(m, "Latency Top version : v0.1\n");
520 for (i = 0; i < 32; i++) {
521 struct latency_record *lr = &task->latency_record[i];
522 if (lr->backtrace[0]) {
524 seq_printf(m, "%i %li %li",
525 lr->count, lr->time, lr->max);
526 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
527 unsigned long bt = lr->backtrace[q];
532 seq_printf(m, " %ps", (void *)bt);
538 put_task_struct(task);
542 static int lstats_open(struct inode *inode, struct file *file)
544 return single_open(file, lstats_show_proc, inode);
547 static ssize_t lstats_write(struct file *file, const char __user *buf,
548 size_t count, loff_t *offs)
550 struct task_struct *task = get_proc_task(file_inode(file));
554 clear_all_latency_tracing(task);
555 put_task_struct(task);
560 static const struct file_operations proc_lstats_operations = {
563 .write = lstats_write,
565 .release = single_release,
570 static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns,
571 struct pid *pid, struct task_struct *task)
573 unsigned long totalpages = totalram_pages + total_swap_pages;
574 unsigned long points = 0;
576 read_lock(&tasklist_lock);
578 points = oom_badness(task, NULL, NULL, totalpages) *
580 read_unlock(&tasklist_lock);
581 seq_printf(m, "%lu\n", points);
591 static const struct limit_names lnames[RLIM_NLIMITS] = {
592 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
593 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
594 [RLIMIT_DATA] = {"Max data size", "bytes"},
595 [RLIMIT_STACK] = {"Max stack size", "bytes"},
596 [RLIMIT_CORE] = {"Max core file size", "bytes"},
597 [RLIMIT_RSS] = {"Max resident set", "bytes"},
598 [RLIMIT_NPROC] = {"Max processes", "processes"},
599 [RLIMIT_NOFILE] = {"Max open files", "files"},
600 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
601 [RLIMIT_AS] = {"Max address space", "bytes"},
602 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
603 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
604 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
605 [RLIMIT_NICE] = {"Max nice priority", NULL},
606 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
607 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
610 /* Display limits for a process */
611 static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
612 struct pid *pid, struct task_struct *task)
617 struct rlimit rlim[RLIM_NLIMITS];
619 if (!lock_task_sighand(task, &flags))
621 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
622 unlock_task_sighand(task, &flags);
625 * print the file header
627 seq_printf(m, "%-25s %-20s %-20s %-10s\n",
628 "Limit", "Soft Limit", "Hard Limit", "Units");
630 for (i = 0; i < RLIM_NLIMITS; i++) {
631 if (rlim[i].rlim_cur == RLIM_INFINITY)
632 seq_printf(m, "%-25s %-20s ",
633 lnames[i].name, "unlimited");
635 seq_printf(m, "%-25s %-20lu ",
636 lnames[i].name, rlim[i].rlim_cur);
638 if (rlim[i].rlim_max == RLIM_INFINITY)
639 seq_printf(m, "%-20s ", "unlimited");
641 seq_printf(m, "%-20lu ", rlim[i].rlim_max);
644 seq_printf(m, "%-10s\n", lnames[i].unit);
652 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
653 static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
654 struct pid *pid, struct task_struct *task)
657 unsigned long args[6], sp, pc;
660 res = lock_trace(task);
664 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
665 seq_puts(m, "running\n");
667 seq_printf(m, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
670 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
672 args[0], args[1], args[2], args[3], args[4], args[5],
678 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
680 /************************************************************************/
681 /* Here the fs part begins */
682 /************************************************************************/
684 /* permission checks */
685 static int proc_fd_access_allowed(struct inode *inode)
687 struct task_struct *task;
689 /* Allow access to a task's file descriptors if it is us or we
690 * may use ptrace attach to the process and find out that
693 task = get_proc_task(inode);
695 allowed = ptrace_may_access(task, PTRACE_MODE_READ);
696 put_task_struct(task);
701 int proc_setattr(struct dentry *dentry, struct iattr *attr)
704 struct inode *inode = d_inode(dentry);
706 if (attr->ia_valid & ATTR_MODE)
709 error = inode_change_ok(inode, attr);
713 setattr_copy(inode, attr);
714 mark_inode_dirty(inode);
719 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
720 * or euid/egid (for hide_pid_min=2)?
722 static bool has_pid_permissions(struct pid_namespace *pid,
723 struct task_struct *task,
726 if (pid->hide_pid < hide_pid_min)
728 if (in_group_p(pid->pid_gid))
730 return ptrace_may_access(task, PTRACE_MODE_READ);
734 static int proc_pid_permission(struct inode *inode, int mask)
736 struct pid_namespace *pid = inode->i_sb->s_fs_info;
737 struct task_struct *task;
740 task = get_proc_task(inode);
743 has_perms = has_pid_permissions(pid, task, 1);
744 put_task_struct(task);
747 if (pid->hide_pid == 2) {
749 * Let's make getdents(), stat(), and open()
750 * consistent with each other. If a process
751 * may not stat() a file, it shouldn't be seen
759 return generic_permission(inode, mask);
764 static const struct inode_operations proc_def_inode_operations = {
765 .setattr = proc_setattr,
768 static int proc_single_show(struct seq_file *m, void *v)
770 struct inode *inode = m->private;
771 struct pid_namespace *ns;
773 struct task_struct *task;
776 ns = inode->i_sb->s_fs_info;
777 pid = proc_pid(inode);
778 task = get_pid_task(pid, PIDTYPE_PID);
782 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
784 put_task_struct(task);
788 static int proc_single_open(struct inode *inode, struct file *filp)
790 return single_open(filp, proc_single_show, inode);
793 static const struct file_operations proc_single_file_operations = {
794 .open = proc_single_open,
797 .release = single_release,
801 struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode)
803 struct task_struct *task = get_proc_task(inode);
804 struct mm_struct *mm = ERR_PTR(-ESRCH);
807 mm = mm_access(task, mode);
808 put_task_struct(task);
810 if (!IS_ERR_OR_NULL(mm)) {
811 /* ensure this mm_struct can't be freed */
812 atomic_inc(&mm->mm_count);
813 /* but do not pin its memory */
821 static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
823 struct mm_struct *mm = proc_mem_open(inode, mode);
828 file->private_data = mm;
832 static int mem_open(struct inode *inode, struct file *file)
834 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
836 /* OK to pass negative loff_t, we can catch out-of-range */
837 file->f_mode |= FMODE_UNSIGNED_OFFSET;
842 static ssize_t mem_rw(struct file *file, char __user *buf,
843 size_t count, loff_t *ppos, int write)
845 struct mm_struct *mm = file->private_data;
846 unsigned long addr = *ppos;
853 page = (char *)__get_free_page(GFP_TEMPORARY);
858 if (!atomic_inc_not_zero(&mm->mm_users))
862 int this_len = min_t(int, count, PAGE_SIZE);
864 if (write && copy_from_user(page, buf, this_len)) {
869 this_len = access_remote_vm(mm, addr, page, this_len, write);
876 if (!write && copy_to_user(buf, page, this_len)) {
890 free_page((unsigned long) page);
894 static ssize_t mem_read(struct file *file, char __user *buf,
895 size_t count, loff_t *ppos)
897 return mem_rw(file, buf, count, ppos, 0);
900 static ssize_t mem_write(struct file *file, const char __user *buf,
901 size_t count, loff_t *ppos)
903 return mem_rw(file, (char __user*)buf, count, ppos, 1);
906 loff_t mem_lseek(struct file *file, loff_t offset, int orig)
910 file->f_pos = offset;
913 file->f_pos += offset;
918 force_successful_syscall_return();
922 static int mem_release(struct inode *inode, struct file *file)
924 struct mm_struct *mm = file->private_data;
930 static const struct file_operations proc_mem_operations = {
935 .release = mem_release,
938 static int environ_open(struct inode *inode, struct file *file)
940 return __mem_open(inode, file, PTRACE_MODE_READ);
943 static ssize_t environ_read(struct file *file, char __user *buf,
944 size_t count, loff_t *ppos)
947 unsigned long src = *ppos;
949 struct mm_struct *mm = file->private_data;
954 page = (char *)__get_free_page(GFP_TEMPORARY);
959 if (!atomic_inc_not_zero(&mm->mm_users))
962 size_t this_len, max_len;
965 if (src >= (mm->env_end - mm->env_start))
968 this_len = mm->env_end - (mm->env_start + src);
970 max_len = min_t(size_t, PAGE_SIZE, count);
971 this_len = min(max_len, this_len);
973 retval = access_remote_vm(mm, (mm->env_start + src),
981 if (copy_to_user(buf, page, retval)) {
995 free_page((unsigned long) page);
999 static const struct file_operations proc_environ_operations = {
1000 .open = environ_open,
1001 .read = environ_read,
1002 .llseek = generic_file_llseek,
1003 .release = mem_release,
1006 static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
1009 struct task_struct *task = get_proc_task(file_inode(file));
1010 char buffer[PROC_NUMBUF];
1011 int oom_adj = OOM_ADJUST_MIN;
1013 unsigned long flags;
1017 if (lock_task_sighand(task, &flags)) {
1018 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
1019 oom_adj = OOM_ADJUST_MAX;
1021 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
1023 unlock_task_sighand(task, &flags);
1025 put_task_struct(task);
1026 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
1027 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1030 static ssize_t oom_adj_write(struct file *file, const char __user *buf,
1031 size_t count, loff_t *ppos)
1033 struct task_struct *task;
1034 char buffer[PROC_NUMBUF];
1036 unsigned long flags;
1039 memset(buffer, 0, sizeof(buffer));
1040 if (count > sizeof(buffer) - 1)
1041 count = sizeof(buffer) - 1;
1042 if (copy_from_user(buffer, buf, count)) {
1047 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
1050 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
1051 oom_adj != OOM_DISABLE) {
1056 task = get_proc_task(file_inode(file));
1068 if (!lock_task_sighand(task, &flags)) {
1074 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1075 * value is always attainable.
1077 if (oom_adj == OOM_ADJUST_MAX)
1078 oom_adj = OOM_SCORE_ADJ_MAX;
1080 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
1082 if (oom_adj < task->signal->oom_score_adj &&
1083 !capable(CAP_SYS_RESOURCE)) {
1089 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
1090 * /proc/pid/oom_score_adj instead.
1092 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
1093 current->comm, task_pid_nr(current), task_pid_nr(task),
1096 task->signal->oom_score_adj = oom_adj;
1097 trace_oom_score_adj_update(task);
1099 unlock_task_sighand(task, &flags);
1102 put_task_struct(task);
1104 return err < 0 ? err : count;
1107 static const struct file_operations proc_oom_adj_operations = {
1108 .read = oom_adj_read,
1109 .write = oom_adj_write,
1110 .llseek = generic_file_llseek,
1113 static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1114 size_t count, loff_t *ppos)
1116 struct task_struct *task = get_proc_task(file_inode(file));
1117 char buffer[PROC_NUMBUF];
1118 short oom_score_adj = OOM_SCORE_ADJ_MIN;
1119 unsigned long flags;
1124 if (lock_task_sighand(task, &flags)) {
1125 oom_score_adj = task->signal->oom_score_adj;
1126 unlock_task_sighand(task, &flags);
1128 put_task_struct(task);
1129 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
1130 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1133 static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1134 size_t count, loff_t *ppos)
1136 struct task_struct *task;
1137 char buffer[PROC_NUMBUF];
1138 unsigned long flags;
1142 memset(buffer, 0, sizeof(buffer));
1143 if (count > sizeof(buffer) - 1)
1144 count = sizeof(buffer) - 1;
1145 if (copy_from_user(buffer, buf, count)) {
1150 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
1153 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
1154 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1159 task = get_proc_task(file_inode(file));
1171 if (!lock_task_sighand(task, &flags)) {
1176 if ((short)oom_score_adj < task->signal->oom_score_adj_min &&
1177 !capable(CAP_SYS_RESOURCE)) {
1182 task->signal->oom_score_adj = (short)oom_score_adj;
1183 if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
1184 task->signal->oom_score_adj_min = (short)oom_score_adj;
1185 trace_oom_score_adj_update(task);
1188 unlock_task_sighand(task, &flags);
1191 put_task_struct(task);
1193 return err < 0 ? err : count;
1196 static const struct file_operations proc_oom_score_adj_operations = {
1197 .read = oom_score_adj_read,
1198 .write = oom_score_adj_write,
1199 .llseek = default_llseek,
1202 #ifdef CONFIG_AUDITSYSCALL
1203 #define TMPBUFLEN 21
1204 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1205 size_t count, loff_t *ppos)
1207 struct inode * inode = file_inode(file);
1208 struct task_struct *task = get_proc_task(inode);
1210 char tmpbuf[TMPBUFLEN];
1214 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1215 from_kuid(file->f_cred->user_ns,
1216 audit_get_loginuid(task)));
1217 put_task_struct(task);
1218 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1221 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1222 size_t count, loff_t *ppos)
1224 struct inode * inode = file_inode(file);
1231 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1237 if (count >= PAGE_SIZE)
1238 count = PAGE_SIZE - 1;
1241 /* No partial writes. */
1244 page = (char*)__get_free_page(GFP_TEMPORARY);
1248 if (copy_from_user(page, buf, count))
1252 loginuid = simple_strtoul(page, &tmp, 10);
1259 /* is userspace tring to explicitly UNSET the loginuid? */
1260 if (loginuid == AUDIT_UID_UNSET) {
1261 kloginuid = INVALID_UID;
1263 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
1264 if (!uid_valid(kloginuid)) {
1270 length = audit_set_loginuid(kloginuid);
1271 if (likely(length == 0))
1275 free_page((unsigned long) page);
1279 static const struct file_operations proc_loginuid_operations = {
1280 .read = proc_loginuid_read,
1281 .write = proc_loginuid_write,
1282 .llseek = generic_file_llseek,
1285 static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1286 size_t count, loff_t *ppos)
1288 struct inode * inode = file_inode(file);
1289 struct task_struct *task = get_proc_task(inode);
1291 char tmpbuf[TMPBUFLEN];
1295 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1296 audit_get_sessionid(task));
1297 put_task_struct(task);
1298 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1301 static const struct file_operations proc_sessionid_operations = {
1302 .read = proc_sessionid_read,
1303 .llseek = generic_file_llseek,
1307 #ifdef CONFIG_FAULT_INJECTION
1308 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1309 size_t count, loff_t *ppos)
1311 struct task_struct *task = get_proc_task(file_inode(file));
1312 char buffer[PROC_NUMBUF];
1318 make_it_fail = task->make_it_fail;
1319 put_task_struct(task);
1321 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1323 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1326 static ssize_t proc_fault_inject_write(struct file * file,
1327 const char __user * buf, size_t count, loff_t *ppos)
1329 struct task_struct *task;
1330 char buffer[PROC_NUMBUF], *end;
1333 if (!capable(CAP_SYS_RESOURCE))
1335 memset(buffer, 0, sizeof(buffer));
1336 if (count > sizeof(buffer) - 1)
1337 count = sizeof(buffer) - 1;
1338 if (copy_from_user(buffer, buf, count))
1340 make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1343 if (make_it_fail < 0 || make_it_fail > 1)
1346 task = get_proc_task(file_inode(file));
1349 task->make_it_fail = make_it_fail;
1350 put_task_struct(task);
1355 static const struct file_operations proc_fault_inject_operations = {
1356 .read = proc_fault_inject_read,
1357 .write = proc_fault_inject_write,
1358 .llseek = generic_file_llseek,
1363 #ifdef CONFIG_SCHED_DEBUG
1365 * Print out various scheduling related per-task fields:
1367 static int sched_show(struct seq_file *m, void *v)
1369 struct inode *inode = m->private;
1370 struct task_struct *p;
1372 p = get_proc_task(inode);
1375 proc_sched_show_task(p, m);
1383 sched_write(struct file *file, const char __user *buf,
1384 size_t count, loff_t *offset)
1386 struct inode *inode = file_inode(file);
1387 struct task_struct *p;
1389 p = get_proc_task(inode);
1392 proc_sched_set_task(p);
1399 static int sched_open(struct inode *inode, struct file *filp)
1401 return single_open(filp, sched_show, inode);
1404 static const struct file_operations proc_pid_sched_operations = {
1407 .write = sched_write,
1408 .llseek = seq_lseek,
1409 .release = single_release,
1414 #ifdef CONFIG_SCHED_AUTOGROUP
1416 * Print out autogroup related information:
1418 static int sched_autogroup_show(struct seq_file *m, void *v)
1420 struct inode *inode = m->private;
1421 struct task_struct *p;
1423 p = get_proc_task(inode);
1426 proc_sched_autogroup_show_task(p, m);
1434 sched_autogroup_write(struct file *file, const char __user *buf,
1435 size_t count, loff_t *offset)
1437 struct inode *inode = file_inode(file);
1438 struct task_struct *p;
1439 char buffer[PROC_NUMBUF];
1443 memset(buffer, 0, sizeof(buffer));
1444 if (count > sizeof(buffer) - 1)
1445 count = sizeof(buffer) - 1;
1446 if (copy_from_user(buffer, buf, count))
1449 err = kstrtoint(strstrip(buffer), 0, &nice);
1453 p = get_proc_task(inode);
1457 err = proc_sched_autogroup_set_nice(p, nice);
1466 static int sched_autogroup_open(struct inode *inode, struct file *filp)
1470 ret = single_open(filp, sched_autogroup_show, NULL);
1472 struct seq_file *m = filp->private_data;
1479 static const struct file_operations proc_pid_sched_autogroup_operations = {
1480 .open = sched_autogroup_open,
1482 .write = sched_autogroup_write,
1483 .llseek = seq_lseek,
1484 .release = single_release,
1487 #endif /* CONFIG_SCHED_AUTOGROUP */
1489 static ssize_t comm_write(struct file *file, const char __user *buf,
1490 size_t count, loff_t *offset)
1492 struct inode *inode = file_inode(file);
1493 struct task_struct *p;
1494 char buffer[TASK_COMM_LEN];
1495 const size_t maxlen = sizeof(buffer) - 1;
1497 memset(buffer, 0, sizeof(buffer));
1498 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
1501 p = get_proc_task(inode);
1505 if (same_thread_group(current, p))
1506 set_task_comm(p, buffer);
1515 static int comm_show(struct seq_file *m, void *v)
1517 struct inode *inode = m->private;
1518 struct task_struct *p;
1520 p = get_proc_task(inode);
1525 seq_printf(m, "%s\n", p->comm);
1533 static int comm_open(struct inode *inode, struct file *filp)
1535 return single_open(filp, comm_show, inode);
1538 static const struct file_operations proc_pid_set_comm_operations = {
1541 .write = comm_write,
1542 .llseek = seq_lseek,
1543 .release = single_release,
1546 static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
1548 struct task_struct *task;
1549 struct mm_struct *mm;
1550 struct file *exe_file;
1552 task = get_proc_task(d_inode(dentry));
1555 mm = get_task_mm(task);
1556 put_task_struct(task);
1559 exe_file = get_mm_exe_file(mm);
1562 *exe_path = exe_file->f_path;
1563 path_get(&exe_file->f_path);
1570 static const char *proc_pid_follow_link(struct dentry *dentry, void **cookie)
1572 struct inode *inode = d_inode(dentry);
1574 int error = -EACCES;
1576 /* Are we allowed to snoop on the tasks file descriptors? */
1577 if (!proc_fd_access_allowed(inode))
1580 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1584 nd_jump_link(&path);
1587 return ERR_PTR(error);
1590 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1592 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
1599 pathname = d_path(path, tmp, PAGE_SIZE);
1600 len = PTR_ERR(pathname);
1601 if (IS_ERR(pathname))
1603 len = tmp + PAGE_SIZE - 1 - pathname;
1607 if (copy_to_user(buffer, pathname, len))
1610 free_page((unsigned long)tmp);
1614 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1616 int error = -EACCES;
1617 struct inode *inode = d_inode(dentry);
1620 /* Are we allowed to snoop on the tasks file descriptors? */
1621 if (!proc_fd_access_allowed(inode))
1624 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1628 error = do_proc_readlink(&path, buffer, buflen);
1634 const struct inode_operations proc_pid_link_inode_operations = {
1635 .readlink = proc_pid_readlink,
1636 .follow_link = proc_pid_follow_link,
1637 .setattr = proc_setattr,
1641 /* building an inode */
1643 struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1645 struct inode * inode;
1646 struct proc_inode *ei;
1647 const struct cred *cred;
1649 /* We need a new inode */
1651 inode = new_inode(sb);
1657 inode->i_ino = get_next_ino();
1658 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1659 inode->i_op = &proc_def_inode_operations;
1662 * grab the reference to task.
1664 ei->pid = get_task_pid(task, PIDTYPE_PID);
1668 if (task_dumpable(task)) {
1670 cred = __task_cred(task);
1671 inode->i_uid = cred->euid;
1672 inode->i_gid = cred->egid;
1675 security_task_to_inode(task, inode);
1685 int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1687 struct inode *inode = d_inode(dentry);
1688 struct task_struct *task;
1689 const struct cred *cred;
1690 struct pid_namespace *pid = dentry->d_sb->s_fs_info;
1692 generic_fillattr(inode, stat);
1695 stat->uid = GLOBAL_ROOT_UID;
1696 stat->gid = GLOBAL_ROOT_GID;
1697 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1699 if (!has_pid_permissions(pid, task, 2)) {
1702 * This doesn't prevent learning whether PID exists,
1703 * it only makes getattr() consistent with readdir().
1707 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1708 task_dumpable(task)) {
1709 cred = __task_cred(task);
1710 stat->uid = cred->euid;
1711 stat->gid = cred->egid;
1721 * Exceptional case: normally we are not allowed to unhash a busy
1722 * directory. In this case, however, we can do it - no aliasing problems
1723 * due to the way we treat inodes.
1725 * Rewrite the inode's ownerships here because the owning task may have
1726 * performed a setuid(), etc.
1728 * Before the /proc/pid/status file was created the only way to read
1729 * the effective uid of a /process was to stat /proc/pid. Reading
1730 * /proc/pid/status is slow enough that procps and other packages
1731 * kept stating /proc/pid. To keep the rules in /proc simple I have
1732 * made this apply to all per process world readable and executable
1735 int pid_revalidate(struct dentry *dentry, unsigned int flags)
1737 struct inode *inode;
1738 struct task_struct *task;
1739 const struct cred *cred;
1741 if (flags & LOOKUP_RCU)
1744 inode = d_inode(dentry);
1745 task = get_proc_task(inode);
1748 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1749 task_dumpable(task)) {
1751 cred = __task_cred(task);
1752 inode->i_uid = cred->euid;
1753 inode->i_gid = cred->egid;
1756 inode->i_uid = GLOBAL_ROOT_UID;
1757 inode->i_gid = GLOBAL_ROOT_GID;
1759 inode->i_mode &= ~(S_ISUID | S_ISGID);
1760 security_task_to_inode(task, inode);
1761 put_task_struct(task);
1767 static inline bool proc_inode_is_dead(struct inode *inode)
1769 return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1772 int pid_delete_dentry(const struct dentry *dentry)
1774 /* Is the task we represent dead?
1775 * If so, then don't put the dentry on the lru list,
1776 * kill it immediately.
1778 return proc_inode_is_dead(d_inode(dentry));
1781 const struct dentry_operations pid_dentry_operations =
1783 .d_revalidate = pid_revalidate,
1784 .d_delete = pid_delete_dentry,
1790 * Fill a directory entry.
1792 * If possible create the dcache entry and derive our inode number and
1793 * file type from dcache entry.
1795 * Since all of the proc inode numbers are dynamically generated, the inode
1796 * numbers do not exist until the inode is cache. This means creating the
1797 * the dcache entry in readdir is necessary to keep the inode numbers
1798 * reported by readdir in sync with the inode numbers reported
1801 bool proc_fill_cache(struct file *file, struct dir_context *ctx,
1802 const char *name, int len,
1803 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1805 struct dentry *child, *dir = file->f_path.dentry;
1806 struct qstr qname = QSTR_INIT(name, len);
1807 struct inode *inode;
1811 child = d_hash_and_lookup(dir, &qname);
1813 child = d_alloc(dir, &qname);
1815 goto end_instantiate;
1816 if (instantiate(d_inode(dir), child, task, ptr) < 0) {
1818 goto end_instantiate;
1821 inode = d_inode(child);
1823 type = inode->i_mode >> 12;
1825 return dir_emit(ctx, name, len, ino, type);
1828 return dir_emit(ctx, name, len, 1, DT_UNKNOWN);
1831 #ifdef CONFIG_CHECKPOINT_RESTORE
1834 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1835 * which represent vma start and end addresses.
1837 static int dname_to_vma_addr(struct dentry *dentry,
1838 unsigned long *start, unsigned long *end)
1840 if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2)
1846 static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
1848 unsigned long vm_start, vm_end;
1849 bool exact_vma_exists = false;
1850 struct mm_struct *mm = NULL;
1851 struct task_struct *task;
1852 const struct cred *cred;
1853 struct inode *inode;
1856 if (flags & LOOKUP_RCU)
1859 if (!capable(CAP_SYS_ADMIN)) {
1864 inode = d_inode(dentry);
1865 task = get_proc_task(inode);
1869 mm = mm_access(task, PTRACE_MODE_READ);
1870 if (IS_ERR_OR_NULL(mm))
1873 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1874 down_read(&mm->mmap_sem);
1875 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1876 up_read(&mm->mmap_sem);
1881 if (exact_vma_exists) {
1882 if (task_dumpable(task)) {
1884 cred = __task_cred(task);
1885 inode->i_uid = cred->euid;
1886 inode->i_gid = cred->egid;
1889 inode->i_uid = GLOBAL_ROOT_UID;
1890 inode->i_gid = GLOBAL_ROOT_GID;
1892 security_task_to_inode(task, inode);
1897 put_task_struct(task);
1903 static const struct dentry_operations tid_map_files_dentry_operations = {
1904 .d_revalidate = map_files_d_revalidate,
1905 .d_delete = pid_delete_dentry,
1908 static int proc_map_files_get_link(struct dentry *dentry, struct path *path)
1910 unsigned long vm_start, vm_end;
1911 struct vm_area_struct *vma;
1912 struct task_struct *task;
1913 struct mm_struct *mm;
1917 task = get_proc_task(d_inode(dentry));
1921 mm = get_task_mm(task);
1922 put_task_struct(task);
1926 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
1931 down_read(&mm->mmap_sem);
1932 vma = find_exact_vma(mm, vm_start, vm_end);
1933 if (vma && vma->vm_file) {
1934 *path = vma->vm_file->f_path;
1938 up_read(&mm->mmap_sem);
1946 struct map_files_info {
1949 unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
1953 proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
1954 struct task_struct *task, const void *ptr)
1956 fmode_t mode = (fmode_t)(unsigned long)ptr;
1957 struct proc_inode *ei;
1958 struct inode *inode;
1960 inode = proc_pid_make_inode(dir->i_sb, task);
1965 ei->op.proc_get_link = proc_map_files_get_link;
1967 inode->i_op = &proc_pid_link_inode_operations;
1969 inode->i_mode = S_IFLNK;
1971 if (mode & FMODE_READ)
1972 inode->i_mode |= S_IRUSR;
1973 if (mode & FMODE_WRITE)
1974 inode->i_mode |= S_IWUSR;
1976 d_set_d_op(dentry, &tid_map_files_dentry_operations);
1977 d_add(dentry, inode);
1982 static struct dentry *proc_map_files_lookup(struct inode *dir,
1983 struct dentry *dentry, unsigned int flags)
1985 unsigned long vm_start, vm_end;
1986 struct vm_area_struct *vma;
1987 struct task_struct *task;
1989 struct mm_struct *mm;
1992 if (!capable(CAP_SYS_ADMIN))
1996 task = get_proc_task(dir);
2001 if (!ptrace_may_access(task, PTRACE_MODE_READ))
2005 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
2008 mm = get_task_mm(task);
2012 down_read(&mm->mmap_sem);
2013 vma = find_exact_vma(mm, vm_start, vm_end);
2018 result = proc_map_files_instantiate(dir, dentry, task,
2019 (void *)(unsigned long)vma->vm_file->f_mode);
2022 up_read(&mm->mmap_sem);
2025 put_task_struct(task);
2027 return ERR_PTR(result);
2030 static const struct inode_operations proc_map_files_inode_operations = {
2031 .lookup = proc_map_files_lookup,
2032 .permission = proc_fd_permission,
2033 .setattr = proc_setattr,
2037 proc_map_files_readdir(struct file *file, struct dir_context *ctx)
2039 struct vm_area_struct *vma;
2040 struct task_struct *task;
2041 struct mm_struct *mm;
2042 unsigned long nr_files, pos, i;
2043 struct flex_array *fa = NULL;
2044 struct map_files_info info;
2045 struct map_files_info *p;
2049 if (!capable(CAP_SYS_ADMIN))
2053 task = get_proc_task(file_inode(file));
2058 if (!ptrace_may_access(task, PTRACE_MODE_READ))
2062 if (!dir_emit_dots(file, ctx))
2065 mm = get_task_mm(task);
2068 down_read(&mm->mmap_sem);
2073 * We need two passes here:
2075 * 1) Collect vmas of mapped files with mmap_sem taken
2076 * 2) Release mmap_sem and instantiate entries
2078 * otherwise we get lockdep complained, since filldir()
2079 * routine might require mmap_sem taken in might_fault().
2082 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
2083 if (vma->vm_file && ++pos > ctx->pos)
2088 fa = flex_array_alloc(sizeof(info), nr_files,
2090 if (!fa || flex_array_prealloc(fa, 0, nr_files,
2094 flex_array_free(fa);
2095 up_read(&mm->mmap_sem);
2099 for (i = 0, vma = mm->mmap, pos = 2; vma;
2100 vma = vma->vm_next) {
2103 if (++pos <= ctx->pos)
2106 info.mode = vma->vm_file->f_mode;
2107 info.len = snprintf(info.name,
2108 sizeof(info.name), "%lx-%lx",
2109 vma->vm_start, vma->vm_end);
2110 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
2114 up_read(&mm->mmap_sem);
2116 for (i = 0; i < nr_files; i++) {
2117 p = flex_array_get(fa, i);
2118 if (!proc_fill_cache(file, ctx,
2120 proc_map_files_instantiate,
2122 (void *)(unsigned long)p->mode))
2127 flex_array_free(fa);
2131 put_task_struct(task);
2136 static const struct file_operations proc_map_files_operations = {
2137 .read = generic_read_dir,
2138 .iterate = proc_map_files_readdir,
2139 .llseek = default_llseek,
2142 struct timers_private {
2144 struct task_struct *task;
2145 struct sighand_struct *sighand;
2146 struct pid_namespace *ns;
2147 unsigned long flags;
2150 static void *timers_start(struct seq_file *m, loff_t *pos)
2152 struct timers_private *tp = m->private;
2154 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2156 return ERR_PTR(-ESRCH);
2158 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2160 return ERR_PTR(-ESRCH);
2162 return seq_list_start(&tp->task->signal->posix_timers, *pos);
2165 static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2167 struct timers_private *tp = m->private;
2168 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2171 static void timers_stop(struct seq_file *m, void *v)
2173 struct timers_private *tp = m->private;
2176 unlock_task_sighand(tp->task, &tp->flags);
2181 put_task_struct(tp->task);
2186 static int show_timer(struct seq_file *m, void *v)
2188 struct k_itimer *timer;
2189 struct timers_private *tp = m->private;
2191 static const char * const nstr[] = {
2192 [SIGEV_SIGNAL] = "signal",
2193 [SIGEV_NONE] = "none",
2194 [SIGEV_THREAD] = "thread",
2197 timer = list_entry((struct list_head *)v, struct k_itimer, list);
2198 notify = timer->it_sigev_notify;
2200 seq_printf(m, "ID: %d\n", timer->it_id);
2201 seq_printf(m, "signal: %d/%p\n",
2202 timer->sigq->info.si_signo,
2203 timer->sigq->info.si_value.sival_ptr);
2204 seq_printf(m, "notify: %s/%s.%d\n",
2205 nstr[notify & ~SIGEV_THREAD_ID],
2206 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2207 pid_nr_ns(timer->it_pid, tp->ns));
2208 seq_printf(m, "ClockID: %d\n", timer->it_clock);
2213 static const struct seq_operations proc_timers_seq_ops = {
2214 .start = timers_start,
2215 .next = timers_next,
2216 .stop = timers_stop,
2220 static int proc_timers_open(struct inode *inode, struct file *file)
2222 struct timers_private *tp;
2224 tp = __seq_open_private(file, &proc_timers_seq_ops,
2225 sizeof(struct timers_private));
2229 tp->pid = proc_pid(inode);
2230 tp->ns = inode->i_sb->s_fs_info;
2234 static const struct file_operations proc_timers_operations = {
2235 .open = proc_timers_open,
2237 .llseek = seq_lseek,
2238 .release = seq_release_private,
2240 #endif /* CONFIG_CHECKPOINT_RESTORE */
2242 static int proc_pident_instantiate(struct inode *dir,
2243 struct dentry *dentry, struct task_struct *task, const void *ptr)
2245 const struct pid_entry *p = ptr;
2246 struct inode *inode;
2247 struct proc_inode *ei;
2249 inode = proc_pid_make_inode(dir->i_sb, task);
2254 inode->i_mode = p->mode;
2255 if (S_ISDIR(inode->i_mode))
2256 set_nlink(inode, 2); /* Use getattr to fix if necessary */
2258 inode->i_op = p->iop;
2260 inode->i_fop = p->fop;
2262 d_set_d_op(dentry, &pid_dentry_operations);
2263 d_add(dentry, inode);
2264 /* Close the race of the process dying before we return the dentry */
2265 if (pid_revalidate(dentry, 0))
2271 static struct dentry *proc_pident_lookup(struct inode *dir,
2272 struct dentry *dentry,
2273 const struct pid_entry *ents,
2277 struct task_struct *task = get_proc_task(dir);
2278 const struct pid_entry *p, *last;
2286 * Yes, it does not scale. And it should not. Don't add
2287 * new entries into /proc/<tgid>/ without very good reasons.
2289 last = &ents[nents - 1];
2290 for (p = ents; p <= last; p++) {
2291 if (p->len != dentry->d_name.len)
2293 if (!memcmp(dentry->d_name.name, p->name, p->len))
2299 error = proc_pident_instantiate(dir, dentry, task, p);
2301 put_task_struct(task);
2303 return ERR_PTR(error);
2306 static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
2307 const struct pid_entry *ents, unsigned int nents)
2309 struct task_struct *task = get_proc_task(file_inode(file));
2310 const struct pid_entry *p;
2315 if (!dir_emit_dots(file, ctx))
2318 if (ctx->pos >= nents + 2)
2321 for (p = ents + (ctx->pos - 2); p <= ents + nents - 1; p++) {
2322 if (!proc_fill_cache(file, ctx, p->name, p->len,
2323 proc_pident_instantiate, task, p))
2328 put_task_struct(task);
2332 #ifdef CONFIG_SECURITY
2333 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2334 size_t count, loff_t *ppos)
2336 struct inode * inode = file_inode(file);
2339 struct task_struct *task = get_proc_task(inode);
2344 length = security_getprocattr(task,
2345 (char*)file->f_path.dentry->d_name.name,
2347 put_task_struct(task);
2349 length = simple_read_from_buffer(buf, count, ppos, p, length);
2354 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2355 size_t count, loff_t *ppos)
2357 struct inode * inode = file_inode(file);
2360 struct task_struct *task = get_proc_task(inode);
2365 if (count > PAGE_SIZE)
2368 /* No partial writes. */
2374 page = (char*)__get_free_page(GFP_TEMPORARY);
2379 if (copy_from_user(page, buf, count))
2382 /* Guard against adverse ptrace interaction */
2383 length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
2387 length = security_setprocattr(task,
2388 (char*)file->f_path.dentry->d_name.name,
2389 (void*)page, count);
2390 mutex_unlock(&task->signal->cred_guard_mutex);
2392 free_page((unsigned long) page);
2394 put_task_struct(task);
2399 static const struct file_operations proc_pid_attr_operations = {
2400 .read = proc_pid_attr_read,
2401 .write = proc_pid_attr_write,
2402 .llseek = generic_file_llseek,
2405 static const struct pid_entry attr_dir_stuff[] = {
2406 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2407 REG("prev", S_IRUGO, proc_pid_attr_operations),
2408 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2409 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2410 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2411 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2414 static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
2416 return proc_pident_readdir(file, ctx,
2417 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2420 static const struct file_operations proc_attr_dir_operations = {
2421 .read = generic_read_dir,
2422 .iterate = proc_attr_dir_readdir,
2423 .llseek = default_llseek,
2426 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2427 struct dentry *dentry, unsigned int flags)
2429 return proc_pident_lookup(dir, dentry,
2430 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2433 static const struct inode_operations proc_attr_dir_inode_operations = {
2434 .lookup = proc_attr_dir_lookup,
2435 .getattr = pid_getattr,
2436 .setattr = proc_setattr,
2441 #ifdef CONFIG_ELF_CORE
2442 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2443 size_t count, loff_t *ppos)
2445 struct task_struct *task = get_proc_task(file_inode(file));
2446 struct mm_struct *mm;
2447 char buffer[PROC_NUMBUF];
2455 mm = get_task_mm(task);
2457 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2458 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2459 MMF_DUMP_FILTER_SHIFT));
2461 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2464 put_task_struct(task);
2469 static ssize_t proc_coredump_filter_write(struct file *file,
2470 const char __user *buf,
2474 struct task_struct *task;
2475 struct mm_struct *mm;
2476 char buffer[PROC_NUMBUF], *end;
2483 memset(buffer, 0, sizeof(buffer));
2484 if (count > sizeof(buffer) - 1)
2485 count = sizeof(buffer) - 1;
2486 if (copy_from_user(buffer, buf, count))
2490 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2493 if (end - buffer == 0)
2497 task = get_proc_task(file_inode(file));
2502 mm = get_task_mm(task);
2506 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2508 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2510 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2515 put_task_struct(task);
2520 static const struct file_operations proc_coredump_filter_operations = {
2521 .read = proc_coredump_filter_read,
2522 .write = proc_coredump_filter_write,
2523 .llseek = generic_file_llseek,
2527 #ifdef CONFIG_TASK_IO_ACCOUNTING
2528 static int do_io_accounting(struct task_struct *task, struct seq_file *m, int whole)
2530 struct task_io_accounting acct = task->ioac;
2531 unsigned long flags;
2534 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2538 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
2543 if (whole && lock_task_sighand(task, &flags)) {
2544 struct task_struct *t = task;
2546 task_io_accounting_add(&acct, &task->signal->ioac);
2547 while_each_thread(task, t)
2548 task_io_accounting_add(&acct, &t->ioac);
2550 unlock_task_sighand(task, &flags);
2557 "read_bytes: %llu\n"
2558 "write_bytes: %llu\n"
2559 "cancelled_write_bytes: %llu\n",
2560 (unsigned long long)acct.rchar,
2561 (unsigned long long)acct.wchar,
2562 (unsigned long long)acct.syscr,
2563 (unsigned long long)acct.syscw,
2564 (unsigned long long)acct.read_bytes,
2565 (unsigned long long)acct.write_bytes,
2566 (unsigned long long)acct.cancelled_write_bytes);
2570 mutex_unlock(&task->signal->cred_guard_mutex);
2574 static int proc_tid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2575 struct pid *pid, struct task_struct *task)
2577 return do_io_accounting(task, m, 0);
2580 static int proc_tgid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2581 struct pid *pid, struct task_struct *task)
2583 return do_io_accounting(task, m, 1);
2585 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2587 #ifdef CONFIG_USER_NS
2588 static int proc_id_map_open(struct inode *inode, struct file *file,
2589 const struct seq_operations *seq_ops)
2591 struct user_namespace *ns = NULL;
2592 struct task_struct *task;
2593 struct seq_file *seq;
2596 task = get_proc_task(inode);
2599 ns = get_user_ns(task_cred_xxx(task, user_ns));
2601 put_task_struct(task);
2606 ret = seq_open(file, seq_ops);
2610 seq = file->private_data;
2620 static int proc_id_map_release(struct inode *inode, struct file *file)
2622 struct seq_file *seq = file->private_data;
2623 struct user_namespace *ns = seq->private;
2625 return seq_release(inode, file);
2628 static int proc_uid_map_open(struct inode *inode, struct file *file)
2630 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2633 static int proc_gid_map_open(struct inode *inode, struct file *file)
2635 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2638 static int proc_projid_map_open(struct inode *inode, struct file *file)
2640 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2643 static const struct file_operations proc_uid_map_operations = {
2644 .open = proc_uid_map_open,
2645 .write = proc_uid_map_write,
2647 .llseek = seq_lseek,
2648 .release = proc_id_map_release,
2651 static const struct file_operations proc_gid_map_operations = {
2652 .open = proc_gid_map_open,
2653 .write = proc_gid_map_write,
2655 .llseek = seq_lseek,
2656 .release = proc_id_map_release,
2659 static const struct file_operations proc_projid_map_operations = {
2660 .open = proc_projid_map_open,
2661 .write = proc_projid_map_write,
2663 .llseek = seq_lseek,
2664 .release = proc_id_map_release,
2667 static int proc_setgroups_open(struct inode *inode, struct file *file)
2669 struct user_namespace *ns = NULL;
2670 struct task_struct *task;
2674 task = get_proc_task(inode);
2677 ns = get_user_ns(task_cred_xxx(task, user_ns));
2679 put_task_struct(task);
2684 if (file->f_mode & FMODE_WRITE) {
2686 if (!ns_capable(ns, CAP_SYS_ADMIN))
2690 ret = single_open(file, &proc_setgroups_show, ns);
2701 static int proc_setgroups_release(struct inode *inode, struct file *file)
2703 struct seq_file *seq = file->private_data;
2704 struct user_namespace *ns = seq->private;
2705 int ret = single_release(inode, file);
2710 static const struct file_operations proc_setgroups_operations = {
2711 .open = proc_setgroups_open,
2712 .write = proc_setgroups_write,
2714 .llseek = seq_lseek,
2715 .release = proc_setgroups_release,
2717 #endif /* CONFIG_USER_NS */
2719 static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2720 struct pid *pid, struct task_struct *task)
2722 int err = lock_trace(task);
2724 seq_printf(m, "%08x\n", task->personality);
2733 static const struct file_operations proc_task_operations;
2734 static const struct inode_operations proc_task_inode_operations;
2736 static const struct pid_entry tgid_base_stuff[] = {
2737 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2738 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2739 #ifdef CONFIG_CHECKPOINT_RESTORE
2740 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
2742 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2743 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
2745 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2747 REG("environ", S_IRUSR, proc_environ_operations),
2748 ONE("auxv", S_IRUSR, proc_pid_auxv),
2749 ONE("status", S_IRUGO, proc_pid_status),
2750 ONE("personality", S_IRUSR, proc_pid_personality),
2751 ONE("limits", S_IRUGO, proc_pid_limits),
2752 #ifdef CONFIG_SCHED_DEBUG
2753 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2755 #ifdef CONFIG_SCHED_AUTOGROUP
2756 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2758 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2759 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2760 ONE("syscall", S_IRUSR, proc_pid_syscall),
2762 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
2763 ONE("stat", S_IRUGO, proc_tgid_stat),
2764 ONE("statm", S_IRUGO, proc_pid_statm),
2765 REG("maps", S_IRUGO, proc_pid_maps_operations),
2767 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
2769 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2770 LNK("cwd", proc_cwd_link),
2771 LNK("root", proc_root_link),
2772 LNK("exe", proc_exe_link),
2773 REG("mounts", S_IRUGO, proc_mounts_operations),
2774 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2775 REG("mountstats", S_IRUSR, proc_mountstats_operations),
2776 #ifdef CONFIG_PROC_PAGE_MONITOR
2777 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2778 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
2779 REG("pagemap", S_IRUSR, proc_pagemap_operations),
2781 #ifdef CONFIG_SECURITY
2782 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
2784 #ifdef CONFIG_KALLSYMS
2785 ONE("wchan", S_IRUGO, proc_pid_wchan),
2787 #ifdef CONFIG_STACKTRACE
2788 ONE("stack", S_IRUSR, proc_pid_stack),
2790 #ifdef CONFIG_SCHEDSTATS
2791 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
2793 #ifdef CONFIG_LATENCYTOP
2794 REG("latency", S_IRUGO, proc_lstats_operations),
2796 #ifdef CONFIG_PROC_PID_CPUSET
2797 ONE("cpuset", S_IRUGO, proc_cpuset_show),
2799 #ifdef CONFIG_CGROUPS
2800 ONE("cgroup", S_IRUGO, proc_cgroup_show),
2802 ONE("oom_score", S_IRUGO, proc_oom_score),
2803 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
2804 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
2805 #ifdef CONFIG_AUDITSYSCALL
2806 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2807 REG("sessionid", S_IRUGO, proc_sessionid_operations),
2809 #ifdef CONFIG_FAULT_INJECTION
2810 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2812 #ifdef CONFIG_ELF_CORE
2813 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
2815 #ifdef CONFIG_TASK_IO_ACCOUNTING
2816 ONE("io", S_IRUSR, proc_tgid_io_accounting),
2818 #ifdef CONFIG_HARDWALL
2819 ONE("hardwall", S_IRUGO, proc_pid_hardwall),
2821 #ifdef CONFIG_USER_NS
2822 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2823 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
2824 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
2825 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
2827 #ifdef CONFIG_CHECKPOINT_RESTORE
2828 REG("timers", S_IRUGO, proc_timers_operations),
2832 static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
2834 return proc_pident_readdir(file, ctx,
2835 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2838 static const struct file_operations proc_tgid_base_operations = {
2839 .read = generic_read_dir,
2840 .iterate = proc_tgid_base_readdir,
2841 .llseek = default_llseek,
2844 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2846 return proc_pident_lookup(dir, dentry,
2847 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2850 static const struct inode_operations proc_tgid_base_inode_operations = {
2851 .lookup = proc_tgid_base_lookup,
2852 .getattr = pid_getattr,
2853 .setattr = proc_setattr,
2854 .permission = proc_pid_permission,
2857 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
2859 struct dentry *dentry, *leader, *dir;
2860 char buf[PROC_NUMBUF];
2864 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2865 /* no ->d_hash() rejects on procfs */
2866 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
2868 d_invalidate(dentry);
2876 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2877 leader = d_hash_and_lookup(mnt->mnt_root, &name);
2882 name.len = strlen(name.name);
2883 dir = d_hash_and_lookup(leader, &name);
2885 goto out_put_leader;
2888 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2889 dentry = d_hash_and_lookup(dir, &name);
2891 d_invalidate(dentry);
2903 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2904 * @task: task that should be flushed.
2906 * When flushing dentries from proc, one needs to flush them from global
2907 * proc (proc_mnt) and from all the namespaces' procs this task was seen
2908 * in. This call is supposed to do all of this job.
2910 * Looks in the dcache for
2912 * /proc/@tgid/task/@pid
2913 * if either directory is present flushes it and all of it'ts children
2916 * It is safe and reasonable to cache /proc entries for a task until
2917 * that task exits. After that they just clog up the dcache with
2918 * useless entries, possibly causing useful dcache entries to be
2919 * flushed instead. This routine is proved to flush those useless
2920 * dcache entries at process exit time.
2922 * NOTE: This routine is just an optimization so it does not guarantee
2923 * that no dcache entries will exist at process exit time it
2924 * just makes it very unlikely that any will persist.
2927 void proc_flush_task(struct task_struct *task)
2930 struct pid *pid, *tgid;
2933 pid = task_pid(task);
2934 tgid = task_tgid(task);
2936 for (i = 0; i <= pid->level; i++) {
2937 upid = &pid->numbers[i];
2938 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
2939 tgid->numbers[i].nr);
2943 static int proc_pid_instantiate(struct inode *dir,
2944 struct dentry * dentry,
2945 struct task_struct *task, const void *ptr)
2947 struct inode *inode;
2949 inode = proc_pid_make_inode(dir->i_sb, task);
2953 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2954 inode->i_op = &proc_tgid_base_inode_operations;
2955 inode->i_fop = &proc_tgid_base_operations;
2956 inode->i_flags|=S_IMMUTABLE;
2958 set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
2959 ARRAY_SIZE(tgid_base_stuff)));
2961 d_set_d_op(dentry, &pid_dentry_operations);
2963 d_add(dentry, inode);
2964 /* Close the race of the process dying before we return the dentry */
2965 if (pid_revalidate(dentry, 0))
2971 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
2973 int result = -ENOENT;
2974 struct task_struct *task;
2976 struct pid_namespace *ns;
2978 tgid = name_to_int(&dentry->d_name);
2982 ns = dentry->d_sb->s_fs_info;
2984 task = find_task_by_pid_ns(tgid, ns);
2986 get_task_struct(task);
2991 result = proc_pid_instantiate(dir, dentry, task, NULL);
2992 put_task_struct(task);
2994 return ERR_PTR(result);
2998 * Find the first task with tgid >= tgid
3003 struct task_struct *task;
3005 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3010 put_task_struct(iter.task);
3014 pid = find_ge_pid(iter.tgid, ns);
3016 iter.tgid = pid_nr_ns(pid, ns);
3017 iter.task = pid_task(pid, PIDTYPE_PID);
3018 /* What we to know is if the pid we have find is the
3019 * pid of a thread_group_leader. Testing for task
3020 * being a thread_group_leader is the obvious thing
3021 * todo but there is a window when it fails, due to
3022 * the pid transfer logic in de_thread.
3024 * So we perform the straight forward test of seeing
3025 * if the pid we have found is the pid of a thread
3026 * group leader, and don't worry if the task we have
3027 * found doesn't happen to be a thread group leader.
3028 * As we don't care in the case of readdir.
3030 if (!iter.task || !has_group_leader_pid(iter.task)) {
3034 get_task_struct(iter.task);
3040 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
3042 /* for the /proc/ directory itself, after non-process stuff has been done */
3043 int proc_pid_readdir(struct file *file, struct dir_context *ctx)
3045 struct tgid_iter iter;
3046 struct pid_namespace *ns = file_inode(file)->i_sb->s_fs_info;
3047 loff_t pos = ctx->pos;
3049 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
3052 if (pos == TGID_OFFSET - 2) {
3053 struct inode *inode = d_inode(ns->proc_self);
3054 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
3056 ctx->pos = pos = pos + 1;
3058 if (pos == TGID_OFFSET - 1) {
3059 struct inode *inode = d_inode(ns->proc_thread_self);
3060 if (!dir_emit(ctx, "thread-self", 11, inode->i_ino, DT_LNK))
3062 ctx->pos = pos = pos + 1;
3064 iter.tgid = pos - TGID_OFFSET;
3066 for (iter = next_tgid(ns, iter);
3068 iter.tgid += 1, iter = next_tgid(ns, iter)) {
3069 char name[PROC_NUMBUF];
3071 if (!has_pid_permissions(ns, iter.task, 2))
3074 len = snprintf(name, sizeof(name), "%d", iter.tgid);
3075 ctx->pos = iter.tgid + TGID_OFFSET;
3076 if (!proc_fill_cache(file, ctx, name, len,
3077 proc_pid_instantiate, iter.task, NULL)) {
3078 put_task_struct(iter.task);
3082 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
3089 static const struct pid_entry tid_base_stuff[] = {
3090 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
3091 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
3092 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
3094 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
3096 REG("environ", S_IRUSR, proc_environ_operations),
3097 ONE("auxv", S_IRUSR, proc_pid_auxv),
3098 ONE("status", S_IRUGO, proc_pid_status),
3099 ONE("personality", S_IRUSR, proc_pid_personality),
3100 ONE("limits", S_IRUGO, proc_pid_limits),
3101 #ifdef CONFIG_SCHED_DEBUG
3102 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
3104 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
3105 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3106 ONE("syscall", S_IRUSR, proc_pid_syscall),
3108 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
3109 ONE("stat", S_IRUGO, proc_tid_stat),
3110 ONE("statm", S_IRUGO, proc_pid_statm),
3111 REG("maps", S_IRUGO, proc_tid_maps_operations),
3112 #ifdef CONFIG_PROC_CHILDREN
3113 REG("children", S_IRUGO, proc_tid_children_operations),
3116 REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
3118 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3119 LNK("cwd", proc_cwd_link),
3120 LNK("root", proc_root_link),
3121 LNK("exe", proc_exe_link),
3122 REG("mounts", S_IRUGO, proc_mounts_operations),
3123 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
3124 #ifdef CONFIG_PROC_PAGE_MONITOR
3125 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3126 REG("smaps", S_IRUGO, proc_tid_smaps_operations),
3127 REG("pagemap", S_IRUSR, proc_pagemap_operations),
3129 #ifdef CONFIG_SECURITY
3130 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
3132 #ifdef CONFIG_KALLSYMS
3133 ONE("wchan", S_IRUGO, proc_pid_wchan),
3135 #ifdef CONFIG_STACKTRACE
3136 ONE("stack", S_IRUSR, proc_pid_stack),
3138 #ifdef CONFIG_SCHEDSTATS
3139 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
3141 #ifdef CONFIG_LATENCYTOP
3142 REG("latency", S_IRUGO, proc_lstats_operations),
3144 #ifdef CONFIG_PROC_PID_CPUSET
3145 ONE("cpuset", S_IRUGO, proc_cpuset_show),
3147 #ifdef CONFIG_CGROUPS
3148 ONE("cgroup", S_IRUGO, proc_cgroup_show),
3150 ONE("oom_score", S_IRUGO, proc_oom_score),
3151 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
3152 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
3153 #ifdef CONFIG_AUDITSYSCALL
3154 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3155 REG("sessionid", S_IRUGO, proc_sessionid_operations),
3157 #ifdef CONFIG_FAULT_INJECTION
3158 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3160 #ifdef CONFIG_TASK_IO_ACCOUNTING
3161 ONE("io", S_IRUSR, proc_tid_io_accounting),
3163 #ifdef CONFIG_HARDWALL
3164 ONE("hardwall", S_IRUGO, proc_pid_hardwall),
3166 #ifdef CONFIG_USER_NS
3167 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3168 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
3169 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
3170 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
3174 static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
3176 return proc_pident_readdir(file, ctx,
3177 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3180 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3182 return proc_pident_lookup(dir, dentry,
3183 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3186 static const struct file_operations proc_tid_base_operations = {
3187 .read = generic_read_dir,
3188 .iterate = proc_tid_base_readdir,
3189 .llseek = default_llseek,
3192 static const struct inode_operations proc_tid_base_inode_operations = {
3193 .lookup = proc_tid_base_lookup,
3194 .getattr = pid_getattr,
3195 .setattr = proc_setattr,
3198 static int proc_task_instantiate(struct inode *dir,
3199 struct dentry *dentry, struct task_struct *task, const void *ptr)
3201 struct inode *inode;
3202 inode = proc_pid_make_inode(dir->i_sb, task);
3206 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3207 inode->i_op = &proc_tid_base_inode_operations;
3208 inode->i_fop = &proc_tid_base_operations;
3209 inode->i_flags|=S_IMMUTABLE;
3211 set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3212 ARRAY_SIZE(tid_base_stuff)));
3214 d_set_d_op(dentry, &pid_dentry_operations);
3216 d_add(dentry, inode);
3217 /* Close the race of the process dying before we return the dentry */
3218 if (pid_revalidate(dentry, 0))
3224 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
3226 int result = -ENOENT;
3227 struct task_struct *task;
3228 struct task_struct *leader = get_proc_task(dir);
3230 struct pid_namespace *ns;
3235 tid = name_to_int(&dentry->d_name);
3239 ns = dentry->d_sb->s_fs_info;
3241 task = find_task_by_pid_ns(tid, ns);
3243 get_task_struct(task);
3247 if (!same_thread_group(leader, task))
3250 result = proc_task_instantiate(dir, dentry, task, NULL);
3252 put_task_struct(task);
3254 put_task_struct(leader);
3256 return ERR_PTR(result);
3260 * Find the first tid of a thread group to return to user space.
3262 * Usually this is just the thread group leader, but if the users
3263 * buffer was too small or there was a seek into the middle of the
3264 * directory we have more work todo.
3266 * In the case of a short read we start with find_task_by_pid.
3268 * In the case of a seek we start with the leader and walk nr
3271 static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
3272 struct pid_namespace *ns)
3274 struct task_struct *pos, *task;
3275 unsigned long nr = f_pos;
3277 if (nr != f_pos) /* 32bit overflow? */
3281 task = pid_task(pid, PIDTYPE_PID);
3285 /* Attempt to start with the tid of a thread */
3287 pos = find_task_by_pid_ns(tid, ns);
3288 if (pos && same_thread_group(pos, task))
3292 /* If nr exceeds the number of threads there is nothing todo */
3293 if (nr >= get_nr_threads(task))
3296 /* If we haven't found our starting place yet start
3297 * with the leader and walk nr threads forward.
3299 pos = task = task->group_leader;
3303 } while_each_thread(task, pos);
3308 get_task_struct(pos);
3315 * Find the next thread in the thread list.
3316 * Return NULL if there is an error or no next thread.
3318 * The reference to the input task_struct is released.
3320 static struct task_struct *next_tid(struct task_struct *start)
3322 struct task_struct *pos = NULL;
3324 if (pid_alive(start)) {
3325 pos = next_thread(start);
3326 if (thread_group_leader(pos))
3329 get_task_struct(pos);
3332 put_task_struct(start);
3336 /* for the /proc/TGID/task/ directories */
3337 static int proc_task_readdir(struct file *file, struct dir_context *ctx)
3339 struct inode *inode = file_inode(file);
3340 struct task_struct *task;
3341 struct pid_namespace *ns;
3344 if (proc_inode_is_dead(inode))
3347 if (!dir_emit_dots(file, ctx))
3350 /* f_version caches the tgid value that the last readdir call couldn't
3351 * return. lseek aka telldir automagically resets f_version to 0.
3353 ns = inode->i_sb->s_fs_info;
3354 tid = (int)file->f_version;
3355 file->f_version = 0;
3356 for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
3358 task = next_tid(task), ctx->pos++) {
3359 char name[PROC_NUMBUF];
3361 tid = task_pid_nr_ns(task, ns);
3362 len = snprintf(name, sizeof(name), "%d", tid);
3363 if (!proc_fill_cache(file, ctx, name, len,
3364 proc_task_instantiate, task, NULL)) {
3365 /* returning this tgid failed, save it as the first
3366 * pid for the next readir call */
3367 file->f_version = (u64)tid;
3368 put_task_struct(task);
3376 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3378 struct inode *inode = d_inode(dentry);
3379 struct task_struct *p = get_proc_task(inode);
3380 generic_fillattr(inode, stat);
3383 stat->nlink += get_nr_threads(p);
3390 static const struct inode_operations proc_task_inode_operations = {
3391 .lookup = proc_task_lookup,
3392 .getattr = proc_task_getattr,
3393 .setattr = proc_setattr,
3394 .permission = proc_pid_permission,
3397 static const struct file_operations proc_task_operations = {
3398 .read = generic_read_dir,
3399 .iterate = proc_task_readdir,
3400 .llseek = default_llseek,