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 INF(NAME, MODE, read) \
134 NOD(NAME, (S_IFREG|(MODE)), \
135 NULL, &proc_info_file_operations, \
136 { .proc_read = read } )
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 pid_entry_count_dirs(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(dentry->d_inode);
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(dentry->d_inode);
194 int result = -ENOENT;
197 result = get_task_root(task, path);
198 put_task_struct(task);
203 static int proc_pid_cmdline(struct seq_file *m, struct pid_namespace *ns,
204 struct pid *pid, struct task_struct *task)
207 * Rely on struct seq_operations::show() being called once
208 * per internal buffer allocation. See single_open(), traverse().
210 BUG_ON(m->size < PAGE_SIZE);
211 m->count += get_cmdline(task, m->buf, PAGE_SIZE);
215 static int proc_pid_auxv(struct seq_file *m, struct pid_namespace *ns,
216 struct pid *pid, struct task_struct *task)
218 struct mm_struct *mm = mm_access(task, PTRACE_MODE_READ);
219 if (mm && !IS_ERR(mm)) {
220 unsigned int nwords = 0;
223 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
224 seq_write(m, mm->saved_auxv, nwords * sizeof(mm->saved_auxv[0]));
232 #ifdef CONFIG_KALLSYMS
234 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
235 * Returns the resolved symbol. If that fails, simply return the address.
237 static int proc_pid_wchan(struct seq_file *m, struct pid_namespace *ns,
238 struct pid *pid, struct task_struct *task)
241 char symname[KSYM_NAME_LEN];
243 wchan = get_wchan(task);
245 if (lookup_symbol_name(wchan, symname) < 0)
246 if (!ptrace_may_access(task, PTRACE_MODE_READ))
249 return seq_printf(m, "%lu", wchan);
251 return seq_printf(m, "%s", symname);
253 #endif /* CONFIG_KALLSYMS */
255 static int lock_trace(struct task_struct *task)
257 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
260 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH)) {
261 mutex_unlock(&task->signal->cred_guard_mutex);
267 static void unlock_trace(struct task_struct *task)
269 mutex_unlock(&task->signal->cred_guard_mutex);
272 #ifdef CONFIG_STACKTRACE
274 #define MAX_STACK_TRACE_DEPTH 64
276 static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
277 struct pid *pid, struct task_struct *task)
279 struct stack_trace trace;
280 unsigned long *entries;
284 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
288 trace.nr_entries = 0;
289 trace.max_entries = MAX_STACK_TRACE_DEPTH;
290 trace.entries = entries;
293 err = lock_trace(task);
295 save_stack_trace_tsk(task, &trace);
297 for (i = 0; i < trace.nr_entries; i++) {
298 seq_printf(m, "[<%pK>] %pS\n",
299 (void *)entries[i], (void *)entries[i]);
309 #ifdef CONFIG_SCHEDSTATS
311 * Provides /proc/PID/schedstat
313 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
315 return sprintf(buffer, "%llu %llu %lu\n",
316 (unsigned long long)task->se.sum_exec_runtime,
317 (unsigned long long)task->sched_info.run_delay,
318 task->sched_info.pcount);
322 #ifdef CONFIG_LATENCYTOP
323 static int lstats_show_proc(struct seq_file *m, void *v)
326 struct inode *inode = m->private;
327 struct task_struct *task = get_proc_task(inode);
331 seq_puts(m, "Latency Top version : v0.1\n");
332 for (i = 0; i < 32; i++) {
333 struct latency_record *lr = &task->latency_record[i];
334 if (lr->backtrace[0]) {
336 seq_printf(m, "%i %li %li",
337 lr->count, lr->time, lr->max);
338 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
339 unsigned long bt = lr->backtrace[q];
344 seq_printf(m, " %ps", (void *)bt);
350 put_task_struct(task);
354 static int lstats_open(struct inode *inode, struct file *file)
356 return single_open(file, lstats_show_proc, inode);
359 static ssize_t lstats_write(struct file *file, const char __user *buf,
360 size_t count, loff_t *offs)
362 struct task_struct *task = get_proc_task(file_inode(file));
366 clear_all_latency_tracing(task);
367 put_task_struct(task);
372 static const struct file_operations proc_lstats_operations = {
375 .write = lstats_write,
377 .release = single_release,
382 #ifdef CONFIG_CGROUPS
383 static int cgroup_open(struct inode *inode, struct file *file)
385 struct pid *pid = PROC_I(inode)->pid;
386 return single_open(file, proc_cgroup_show, pid);
389 static const struct file_operations proc_cgroup_operations = {
393 .release = single_release,
397 #ifdef CONFIG_PROC_PID_CPUSET
399 static int cpuset_open(struct inode *inode, struct file *file)
401 struct pid *pid = PROC_I(inode)->pid;
402 return single_open(file, proc_cpuset_show, pid);
405 static const struct file_operations proc_cpuset_operations = {
409 .release = single_release,
413 static int proc_oom_score(struct task_struct *task, char *buffer)
415 unsigned long totalpages = totalram_pages + total_swap_pages;
416 unsigned long points = 0;
418 read_lock(&tasklist_lock);
420 points = oom_badness(task, NULL, NULL, totalpages) *
422 read_unlock(&tasklist_lock);
423 return sprintf(buffer, "%lu\n", points);
431 static const struct limit_names lnames[RLIM_NLIMITS] = {
432 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
433 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
434 [RLIMIT_DATA] = {"Max data size", "bytes"},
435 [RLIMIT_STACK] = {"Max stack size", "bytes"},
436 [RLIMIT_CORE] = {"Max core file size", "bytes"},
437 [RLIMIT_RSS] = {"Max resident set", "bytes"},
438 [RLIMIT_NPROC] = {"Max processes", "processes"},
439 [RLIMIT_NOFILE] = {"Max open files", "files"},
440 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
441 [RLIMIT_AS] = {"Max address space", "bytes"},
442 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
443 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
444 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
445 [RLIMIT_NICE] = {"Max nice priority", NULL},
446 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
447 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
450 /* Display limits for a process */
451 static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
452 struct pid *pid, struct task_struct *task)
457 struct rlimit rlim[RLIM_NLIMITS];
459 if (!lock_task_sighand(task, &flags))
461 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
462 unlock_task_sighand(task, &flags);
465 * print the file header
467 seq_printf(m, "%-25s %-20s %-20s %-10s\n",
468 "Limit", "Soft Limit", "Hard Limit", "Units");
470 for (i = 0; i < RLIM_NLIMITS; i++) {
471 if (rlim[i].rlim_cur == RLIM_INFINITY)
472 seq_printf(m, "%-25s %-20s ",
473 lnames[i].name, "unlimited");
475 seq_printf(m, "%-25s %-20lu ",
476 lnames[i].name, rlim[i].rlim_cur);
478 if (rlim[i].rlim_max == RLIM_INFINITY)
479 seq_printf(m, "%-20s ", "unlimited");
481 seq_printf(m, "%-20lu ", rlim[i].rlim_max);
484 seq_printf(m, "%-10s\n", lnames[i].unit);
492 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
493 static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
494 struct pid *pid, struct task_struct *task)
497 unsigned long args[6], sp, pc;
498 int res = lock_trace(task);
502 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
503 seq_puts(m, "running\n");
505 seq_printf(m, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
508 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
510 args[0], args[1], args[2], args[3], args[4], args[5],
515 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
517 /************************************************************************/
518 /* Here the fs part begins */
519 /************************************************************************/
521 /* permission checks */
522 static int proc_fd_access_allowed(struct inode *inode)
524 struct task_struct *task;
526 /* Allow access to a task's file descriptors if it is us or we
527 * may use ptrace attach to the process and find out that
530 task = get_proc_task(inode);
532 allowed = ptrace_may_access(task, PTRACE_MODE_READ);
533 put_task_struct(task);
538 int proc_setattr(struct dentry *dentry, struct iattr *attr)
541 struct inode *inode = dentry->d_inode;
543 if (attr->ia_valid & ATTR_MODE)
546 error = inode_change_ok(inode, attr);
550 setattr_copy(inode, attr);
551 mark_inode_dirty(inode);
556 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
557 * or euid/egid (for hide_pid_min=2)?
559 static bool has_pid_permissions(struct pid_namespace *pid,
560 struct task_struct *task,
563 if (pid->hide_pid < hide_pid_min)
565 if (in_group_p(pid->pid_gid))
567 return ptrace_may_access(task, PTRACE_MODE_READ);
571 static int proc_pid_permission(struct inode *inode, int mask)
573 struct pid_namespace *pid = inode->i_sb->s_fs_info;
574 struct task_struct *task;
577 task = get_proc_task(inode);
580 has_perms = has_pid_permissions(pid, task, 1);
581 put_task_struct(task);
584 if (pid->hide_pid == 2) {
586 * Let's make getdents(), stat(), and open()
587 * consistent with each other. If a process
588 * may not stat() a file, it shouldn't be seen
596 return generic_permission(inode, mask);
601 static const struct inode_operations proc_def_inode_operations = {
602 .setattr = proc_setattr,
605 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
607 static ssize_t proc_info_read(struct file * file, char __user * buf,
608 size_t count, loff_t *ppos)
610 struct inode * inode = file_inode(file);
613 struct task_struct *task = get_proc_task(inode);
619 if (count > PROC_BLOCK_SIZE)
620 count = PROC_BLOCK_SIZE;
623 if (!(page = __get_free_page(GFP_TEMPORARY)))
626 length = PROC_I(inode)->op.proc_read(task, (char*)page);
629 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
632 put_task_struct(task);
637 static const struct file_operations proc_info_file_operations = {
638 .read = proc_info_read,
639 .llseek = generic_file_llseek,
642 static int proc_single_show(struct seq_file *m, void *v)
644 struct inode *inode = m->private;
645 struct pid_namespace *ns;
647 struct task_struct *task;
650 ns = inode->i_sb->s_fs_info;
651 pid = proc_pid(inode);
652 task = get_pid_task(pid, PIDTYPE_PID);
656 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
658 put_task_struct(task);
662 static int proc_single_open(struct inode *inode, struct file *filp)
664 return single_open(filp, proc_single_show, inode);
667 static const struct file_operations proc_single_file_operations = {
668 .open = proc_single_open,
671 .release = single_release,
674 static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
676 struct task_struct *task = get_proc_task(file_inode(file));
677 struct mm_struct *mm;
682 mm = mm_access(task, mode);
683 put_task_struct(task);
689 /* ensure this mm_struct can't be freed */
690 atomic_inc(&mm->mm_count);
691 /* but do not pin its memory */
695 file->private_data = mm;
700 static int mem_open(struct inode *inode, struct file *file)
702 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
704 /* OK to pass negative loff_t, we can catch out-of-range */
705 file->f_mode |= FMODE_UNSIGNED_OFFSET;
710 static ssize_t mem_rw(struct file *file, char __user *buf,
711 size_t count, loff_t *ppos, int write)
713 struct mm_struct *mm = file->private_data;
714 unsigned long addr = *ppos;
721 page = (char *)__get_free_page(GFP_TEMPORARY);
726 if (!atomic_inc_not_zero(&mm->mm_users))
730 int this_len = min_t(int, count, PAGE_SIZE);
732 if (write && copy_from_user(page, buf, this_len)) {
737 this_len = access_remote_vm(mm, addr, page, this_len, write);
744 if (!write && copy_to_user(buf, page, this_len)) {
758 free_page((unsigned long) page);
762 static ssize_t mem_read(struct file *file, char __user *buf,
763 size_t count, loff_t *ppos)
765 return mem_rw(file, buf, count, ppos, 0);
768 static ssize_t mem_write(struct file *file, const char __user *buf,
769 size_t count, loff_t *ppos)
771 return mem_rw(file, (char __user*)buf, count, ppos, 1);
774 loff_t mem_lseek(struct file *file, loff_t offset, int orig)
778 file->f_pos = offset;
781 file->f_pos += offset;
786 force_successful_syscall_return();
790 static int mem_release(struct inode *inode, struct file *file)
792 struct mm_struct *mm = file->private_data;
798 static const struct file_operations proc_mem_operations = {
803 .release = mem_release,
806 static int environ_open(struct inode *inode, struct file *file)
808 return __mem_open(inode, file, PTRACE_MODE_READ);
811 static ssize_t environ_read(struct file *file, char __user *buf,
812 size_t count, loff_t *ppos)
815 unsigned long src = *ppos;
817 struct mm_struct *mm = file->private_data;
822 page = (char *)__get_free_page(GFP_TEMPORARY);
827 if (!atomic_inc_not_zero(&mm->mm_users))
830 size_t this_len, max_len;
833 if (src >= (mm->env_end - mm->env_start))
836 this_len = mm->env_end - (mm->env_start + src);
838 max_len = min_t(size_t, PAGE_SIZE, count);
839 this_len = min(max_len, this_len);
841 retval = access_remote_vm(mm, (mm->env_start + src),
849 if (copy_to_user(buf, page, retval)) {
863 free_page((unsigned long) page);
867 static const struct file_operations proc_environ_operations = {
868 .open = environ_open,
869 .read = environ_read,
870 .llseek = generic_file_llseek,
871 .release = mem_release,
874 static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
877 struct task_struct *task = get_proc_task(file_inode(file));
878 char buffer[PROC_NUMBUF];
879 int oom_adj = OOM_ADJUST_MIN;
885 if (lock_task_sighand(task, &flags)) {
886 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
887 oom_adj = OOM_ADJUST_MAX;
889 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
891 unlock_task_sighand(task, &flags);
893 put_task_struct(task);
894 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
895 return simple_read_from_buffer(buf, count, ppos, buffer, len);
898 static ssize_t oom_adj_write(struct file *file, const char __user *buf,
899 size_t count, loff_t *ppos)
901 struct task_struct *task;
902 char buffer[PROC_NUMBUF];
907 memset(buffer, 0, sizeof(buffer));
908 if (count > sizeof(buffer) - 1)
909 count = sizeof(buffer) - 1;
910 if (copy_from_user(buffer, buf, count)) {
915 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
918 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
919 oom_adj != OOM_DISABLE) {
924 task = get_proc_task(file_inode(file));
936 if (!lock_task_sighand(task, &flags)) {
942 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
943 * value is always attainable.
945 if (oom_adj == OOM_ADJUST_MAX)
946 oom_adj = OOM_SCORE_ADJ_MAX;
948 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
950 if (oom_adj < task->signal->oom_score_adj &&
951 !capable(CAP_SYS_RESOURCE)) {
957 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
958 * /proc/pid/oom_score_adj instead.
960 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
961 current->comm, task_pid_nr(current), task_pid_nr(task),
964 task->signal->oom_score_adj = oom_adj;
965 trace_oom_score_adj_update(task);
967 unlock_task_sighand(task, &flags);
970 put_task_struct(task);
972 return err < 0 ? err : count;
975 static const struct file_operations proc_oom_adj_operations = {
976 .read = oom_adj_read,
977 .write = oom_adj_write,
978 .llseek = generic_file_llseek,
981 static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
982 size_t count, loff_t *ppos)
984 struct task_struct *task = get_proc_task(file_inode(file));
985 char buffer[PROC_NUMBUF];
986 short oom_score_adj = OOM_SCORE_ADJ_MIN;
992 if (lock_task_sighand(task, &flags)) {
993 oom_score_adj = task->signal->oom_score_adj;
994 unlock_task_sighand(task, &flags);
996 put_task_struct(task);
997 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
998 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1001 static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1002 size_t count, loff_t *ppos)
1004 struct task_struct *task;
1005 char buffer[PROC_NUMBUF];
1006 unsigned long flags;
1010 memset(buffer, 0, sizeof(buffer));
1011 if (count > sizeof(buffer) - 1)
1012 count = sizeof(buffer) - 1;
1013 if (copy_from_user(buffer, buf, count)) {
1018 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
1021 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
1022 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1027 task = get_proc_task(file_inode(file));
1039 if (!lock_task_sighand(task, &flags)) {
1044 if ((short)oom_score_adj < task->signal->oom_score_adj_min &&
1045 !capable(CAP_SYS_RESOURCE)) {
1050 task->signal->oom_score_adj = (short)oom_score_adj;
1051 if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
1052 task->signal->oom_score_adj_min = (short)oom_score_adj;
1053 trace_oom_score_adj_update(task);
1056 unlock_task_sighand(task, &flags);
1059 put_task_struct(task);
1061 return err < 0 ? err : count;
1064 static const struct file_operations proc_oom_score_adj_operations = {
1065 .read = oom_score_adj_read,
1066 .write = oom_score_adj_write,
1067 .llseek = default_llseek,
1070 #ifdef CONFIG_AUDITSYSCALL
1071 #define TMPBUFLEN 21
1072 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1073 size_t count, loff_t *ppos)
1075 struct inode * inode = file_inode(file);
1076 struct task_struct *task = get_proc_task(inode);
1078 char tmpbuf[TMPBUFLEN];
1082 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1083 from_kuid(file->f_cred->user_ns,
1084 audit_get_loginuid(task)));
1085 put_task_struct(task);
1086 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1089 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1090 size_t count, loff_t *ppos)
1092 struct inode * inode = file_inode(file);
1099 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1105 if (count >= PAGE_SIZE)
1106 count = PAGE_SIZE - 1;
1109 /* No partial writes. */
1112 page = (char*)__get_free_page(GFP_TEMPORARY);
1116 if (copy_from_user(page, buf, count))
1120 loginuid = simple_strtoul(page, &tmp, 10);
1127 /* is userspace tring to explicitly UNSET the loginuid? */
1128 if (loginuid == AUDIT_UID_UNSET) {
1129 kloginuid = INVALID_UID;
1131 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
1132 if (!uid_valid(kloginuid)) {
1138 length = audit_set_loginuid(kloginuid);
1139 if (likely(length == 0))
1143 free_page((unsigned long) page);
1147 static const struct file_operations proc_loginuid_operations = {
1148 .read = proc_loginuid_read,
1149 .write = proc_loginuid_write,
1150 .llseek = generic_file_llseek,
1153 static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1154 size_t count, loff_t *ppos)
1156 struct inode * inode = file_inode(file);
1157 struct task_struct *task = get_proc_task(inode);
1159 char tmpbuf[TMPBUFLEN];
1163 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1164 audit_get_sessionid(task));
1165 put_task_struct(task);
1166 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1169 static const struct file_operations proc_sessionid_operations = {
1170 .read = proc_sessionid_read,
1171 .llseek = generic_file_llseek,
1175 #ifdef CONFIG_FAULT_INJECTION
1176 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1177 size_t count, loff_t *ppos)
1179 struct task_struct *task = get_proc_task(file_inode(file));
1180 char buffer[PROC_NUMBUF];
1186 make_it_fail = task->make_it_fail;
1187 put_task_struct(task);
1189 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1191 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1194 static ssize_t proc_fault_inject_write(struct file * file,
1195 const char __user * buf, size_t count, loff_t *ppos)
1197 struct task_struct *task;
1198 char buffer[PROC_NUMBUF], *end;
1201 if (!capable(CAP_SYS_RESOURCE))
1203 memset(buffer, 0, sizeof(buffer));
1204 if (count > sizeof(buffer) - 1)
1205 count = sizeof(buffer) - 1;
1206 if (copy_from_user(buffer, buf, count))
1208 make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1211 if (make_it_fail < 0 || make_it_fail > 1)
1214 task = get_proc_task(file_inode(file));
1217 task->make_it_fail = make_it_fail;
1218 put_task_struct(task);
1223 static const struct file_operations proc_fault_inject_operations = {
1224 .read = proc_fault_inject_read,
1225 .write = proc_fault_inject_write,
1226 .llseek = generic_file_llseek,
1231 #ifdef CONFIG_SCHED_DEBUG
1233 * Print out various scheduling related per-task fields:
1235 static int sched_show(struct seq_file *m, void *v)
1237 struct inode *inode = m->private;
1238 struct task_struct *p;
1240 p = get_proc_task(inode);
1243 proc_sched_show_task(p, m);
1251 sched_write(struct file *file, const char __user *buf,
1252 size_t count, loff_t *offset)
1254 struct inode *inode = file_inode(file);
1255 struct task_struct *p;
1257 p = get_proc_task(inode);
1260 proc_sched_set_task(p);
1267 static int sched_open(struct inode *inode, struct file *filp)
1269 return single_open(filp, sched_show, inode);
1272 static const struct file_operations proc_pid_sched_operations = {
1275 .write = sched_write,
1276 .llseek = seq_lseek,
1277 .release = single_release,
1282 #ifdef CONFIG_SCHED_AUTOGROUP
1284 * Print out autogroup related information:
1286 static int sched_autogroup_show(struct seq_file *m, void *v)
1288 struct inode *inode = m->private;
1289 struct task_struct *p;
1291 p = get_proc_task(inode);
1294 proc_sched_autogroup_show_task(p, m);
1302 sched_autogroup_write(struct file *file, const char __user *buf,
1303 size_t count, loff_t *offset)
1305 struct inode *inode = file_inode(file);
1306 struct task_struct *p;
1307 char buffer[PROC_NUMBUF];
1311 memset(buffer, 0, sizeof(buffer));
1312 if (count > sizeof(buffer) - 1)
1313 count = sizeof(buffer) - 1;
1314 if (copy_from_user(buffer, buf, count))
1317 err = kstrtoint(strstrip(buffer), 0, &nice);
1321 p = get_proc_task(inode);
1325 err = proc_sched_autogroup_set_nice(p, nice);
1334 static int sched_autogroup_open(struct inode *inode, struct file *filp)
1338 ret = single_open(filp, sched_autogroup_show, NULL);
1340 struct seq_file *m = filp->private_data;
1347 static const struct file_operations proc_pid_sched_autogroup_operations = {
1348 .open = sched_autogroup_open,
1350 .write = sched_autogroup_write,
1351 .llseek = seq_lseek,
1352 .release = single_release,
1355 #endif /* CONFIG_SCHED_AUTOGROUP */
1357 static ssize_t comm_write(struct file *file, const char __user *buf,
1358 size_t count, loff_t *offset)
1360 struct inode *inode = file_inode(file);
1361 struct task_struct *p;
1362 char buffer[TASK_COMM_LEN];
1363 const size_t maxlen = sizeof(buffer) - 1;
1365 memset(buffer, 0, sizeof(buffer));
1366 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
1369 p = get_proc_task(inode);
1373 if (same_thread_group(current, p))
1374 set_task_comm(p, buffer);
1383 static int comm_show(struct seq_file *m, void *v)
1385 struct inode *inode = m->private;
1386 struct task_struct *p;
1388 p = get_proc_task(inode);
1393 seq_printf(m, "%s\n", p->comm);
1401 static int comm_open(struct inode *inode, struct file *filp)
1403 return single_open(filp, comm_show, inode);
1406 static const struct file_operations proc_pid_set_comm_operations = {
1409 .write = comm_write,
1410 .llseek = seq_lseek,
1411 .release = single_release,
1414 static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
1416 struct task_struct *task;
1417 struct mm_struct *mm;
1418 struct file *exe_file;
1420 task = get_proc_task(dentry->d_inode);
1423 mm = get_task_mm(task);
1424 put_task_struct(task);
1427 exe_file = get_mm_exe_file(mm);
1430 *exe_path = exe_file->f_path;
1431 path_get(&exe_file->f_path);
1438 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1440 struct inode *inode = dentry->d_inode;
1442 int error = -EACCES;
1444 /* Are we allowed to snoop on the tasks file descriptors? */
1445 if (!proc_fd_access_allowed(inode))
1448 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1452 nd_jump_link(nd, &path);
1455 return ERR_PTR(error);
1458 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1460 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
1467 pathname = d_path(path, tmp, PAGE_SIZE);
1468 len = PTR_ERR(pathname);
1469 if (IS_ERR(pathname))
1471 len = tmp + PAGE_SIZE - 1 - pathname;
1475 if (copy_to_user(buffer, pathname, len))
1478 free_page((unsigned long)tmp);
1482 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1484 int error = -EACCES;
1485 struct inode *inode = dentry->d_inode;
1488 /* Are we allowed to snoop on the tasks file descriptors? */
1489 if (!proc_fd_access_allowed(inode))
1492 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1496 error = do_proc_readlink(&path, buffer, buflen);
1502 const struct inode_operations proc_pid_link_inode_operations = {
1503 .readlink = proc_pid_readlink,
1504 .follow_link = proc_pid_follow_link,
1505 .setattr = proc_setattr,
1509 /* building an inode */
1511 struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1513 struct inode * inode;
1514 struct proc_inode *ei;
1515 const struct cred *cred;
1517 /* We need a new inode */
1519 inode = new_inode(sb);
1525 inode->i_ino = get_next_ino();
1526 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1527 inode->i_op = &proc_def_inode_operations;
1530 * grab the reference to task.
1532 ei->pid = get_task_pid(task, PIDTYPE_PID);
1536 if (task_dumpable(task)) {
1538 cred = __task_cred(task);
1539 inode->i_uid = cred->euid;
1540 inode->i_gid = cred->egid;
1543 security_task_to_inode(task, inode);
1553 int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1555 struct inode *inode = dentry->d_inode;
1556 struct task_struct *task;
1557 const struct cred *cred;
1558 struct pid_namespace *pid = dentry->d_sb->s_fs_info;
1560 generic_fillattr(inode, stat);
1563 stat->uid = GLOBAL_ROOT_UID;
1564 stat->gid = GLOBAL_ROOT_GID;
1565 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1567 if (!has_pid_permissions(pid, task, 2)) {
1570 * This doesn't prevent learning whether PID exists,
1571 * it only makes getattr() consistent with readdir().
1575 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1576 task_dumpable(task)) {
1577 cred = __task_cred(task);
1578 stat->uid = cred->euid;
1579 stat->gid = cred->egid;
1589 * Exceptional case: normally we are not allowed to unhash a busy
1590 * directory. In this case, however, we can do it - no aliasing problems
1591 * due to the way we treat inodes.
1593 * Rewrite the inode's ownerships here because the owning task may have
1594 * performed a setuid(), etc.
1596 * Before the /proc/pid/status file was created the only way to read
1597 * the effective uid of a /process was to stat /proc/pid. Reading
1598 * /proc/pid/status is slow enough that procps and other packages
1599 * kept stating /proc/pid. To keep the rules in /proc simple I have
1600 * made this apply to all per process world readable and executable
1603 int pid_revalidate(struct dentry *dentry, unsigned int flags)
1605 struct inode *inode;
1606 struct task_struct *task;
1607 const struct cred *cred;
1609 if (flags & LOOKUP_RCU)
1612 inode = dentry->d_inode;
1613 task = get_proc_task(inode);
1616 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1617 task_dumpable(task)) {
1619 cred = __task_cred(task);
1620 inode->i_uid = cred->euid;
1621 inode->i_gid = cred->egid;
1624 inode->i_uid = GLOBAL_ROOT_UID;
1625 inode->i_gid = GLOBAL_ROOT_GID;
1627 inode->i_mode &= ~(S_ISUID | S_ISGID);
1628 security_task_to_inode(task, inode);
1629 put_task_struct(task);
1636 static inline bool proc_inode_is_dead(struct inode *inode)
1638 return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1641 int pid_delete_dentry(const struct dentry *dentry)
1643 /* Is the task we represent dead?
1644 * If so, then don't put the dentry on the lru list,
1645 * kill it immediately.
1647 return proc_inode_is_dead(dentry->d_inode);
1650 const struct dentry_operations pid_dentry_operations =
1652 .d_revalidate = pid_revalidate,
1653 .d_delete = pid_delete_dentry,
1659 * Fill a directory entry.
1661 * If possible create the dcache entry and derive our inode number and
1662 * file type from dcache entry.
1664 * Since all of the proc inode numbers are dynamically generated, the inode
1665 * numbers do not exist until the inode is cache. This means creating the
1666 * the dcache entry in readdir is necessary to keep the inode numbers
1667 * reported by readdir in sync with the inode numbers reported
1670 bool proc_fill_cache(struct file *file, struct dir_context *ctx,
1671 const char *name, int len,
1672 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1674 struct dentry *child, *dir = file->f_path.dentry;
1675 struct qstr qname = QSTR_INIT(name, len);
1676 struct inode *inode;
1680 child = d_hash_and_lookup(dir, &qname);
1682 child = d_alloc(dir, &qname);
1684 goto end_instantiate;
1685 if (instantiate(dir->d_inode, child, task, ptr) < 0) {
1687 goto end_instantiate;
1690 inode = child->d_inode;
1692 type = inode->i_mode >> 12;
1694 return dir_emit(ctx, name, len, ino, type);
1697 return dir_emit(ctx, name, len, 1, DT_UNKNOWN);
1700 #ifdef CONFIG_CHECKPOINT_RESTORE
1703 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1704 * which represent vma start and end addresses.
1706 static int dname_to_vma_addr(struct dentry *dentry,
1707 unsigned long *start, unsigned long *end)
1709 if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2)
1715 static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
1717 unsigned long vm_start, vm_end;
1718 bool exact_vma_exists = false;
1719 struct mm_struct *mm = NULL;
1720 struct task_struct *task;
1721 const struct cred *cred;
1722 struct inode *inode;
1725 if (flags & LOOKUP_RCU)
1728 if (!capable(CAP_SYS_ADMIN)) {
1733 inode = dentry->d_inode;
1734 task = get_proc_task(inode);
1738 mm = mm_access(task, PTRACE_MODE_READ);
1739 if (IS_ERR_OR_NULL(mm))
1742 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1743 down_read(&mm->mmap_sem);
1744 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1745 up_read(&mm->mmap_sem);
1750 if (exact_vma_exists) {
1751 if (task_dumpable(task)) {
1753 cred = __task_cred(task);
1754 inode->i_uid = cred->euid;
1755 inode->i_gid = cred->egid;
1758 inode->i_uid = GLOBAL_ROOT_UID;
1759 inode->i_gid = GLOBAL_ROOT_GID;
1761 security_task_to_inode(task, inode);
1766 put_task_struct(task);
1775 static const struct dentry_operations tid_map_files_dentry_operations = {
1776 .d_revalidate = map_files_d_revalidate,
1777 .d_delete = pid_delete_dentry,
1780 static int proc_map_files_get_link(struct dentry *dentry, struct path *path)
1782 unsigned long vm_start, vm_end;
1783 struct vm_area_struct *vma;
1784 struct task_struct *task;
1785 struct mm_struct *mm;
1789 task = get_proc_task(dentry->d_inode);
1793 mm = get_task_mm(task);
1794 put_task_struct(task);
1798 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
1803 down_read(&mm->mmap_sem);
1804 vma = find_exact_vma(mm, vm_start, vm_end);
1805 if (vma && vma->vm_file) {
1806 *path = vma->vm_file->f_path;
1810 up_read(&mm->mmap_sem);
1818 struct map_files_info {
1821 unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
1825 proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
1826 struct task_struct *task, const void *ptr)
1828 fmode_t mode = (fmode_t)(unsigned long)ptr;
1829 struct proc_inode *ei;
1830 struct inode *inode;
1832 inode = proc_pid_make_inode(dir->i_sb, task);
1837 ei->op.proc_get_link = proc_map_files_get_link;
1839 inode->i_op = &proc_pid_link_inode_operations;
1841 inode->i_mode = S_IFLNK;
1843 if (mode & FMODE_READ)
1844 inode->i_mode |= S_IRUSR;
1845 if (mode & FMODE_WRITE)
1846 inode->i_mode |= S_IWUSR;
1848 d_set_d_op(dentry, &tid_map_files_dentry_operations);
1849 d_add(dentry, inode);
1854 static struct dentry *proc_map_files_lookup(struct inode *dir,
1855 struct dentry *dentry, unsigned int flags)
1857 unsigned long vm_start, vm_end;
1858 struct vm_area_struct *vma;
1859 struct task_struct *task;
1861 struct mm_struct *mm;
1864 if (!capable(CAP_SYS_ADMIN))
1868 task = get_proc_task(dir);
1873 if (!ptrace_may_access(task, PTRACE_MODE_READ))
1877 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
1880 mm = get_task_mm(task);
1884 down_read(&mm->mmap_sem);
1885 vma = find_exact_vma(mm, vm_start, vm_end);
1890 result = proc_map_files_instantiate(dir, dentry, task,
1891 (void *)(unsigned long)vma->vm_file->f_mode);
1894 up_read(&mm->mmap_sem);
1897 put_task_struct(task);
1899 return ERR_PTR(result);
1902 static const struct inode_operations proc_map_files_inode_operations = {
1903 .lookup = proc_map_files_lookup,
1904 .permission = proc_fd_permission,
1905 .setattr = proc_setattr,
1909 proc_map_files_readdir(struct file *file, struct dir_context *ctx)
1911 struct vm_area_struct *vma;
1912 struct task_struct *task;
1913 struct mm_struct *mm;
1914 unsigned long nr_files, pos, i;
1915 struct flex_array *fa = NULL;
1916 struct map_files_info info;
1917 struct map_files_info *p;
1921 if (!capable(CAP_SYS_ADMIN))
1925 task = get_proc_task(file_inode(file));
1930 if (!ptrace_may_access(task, PTRACE_MODE_READ))
1934 if (!dir_emit_dots(file, ctx))
1937 mm = get_task_mm(task);
1940 down_read(&mm->mmap_sem);
1945 * We need two passes here:
1947 * 1) Collect vmas of mapped files with mmap_sem taken
1948 * 2) Release mmap_sem and instantiate entries
1950 * otherwise we get lockdep complained, since filldir()
1951 * routine might require mmap_sem taken in might_fault().
1954 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
1955 if (vma->vm_file && ++pos > ctx->pos)
1960 fa = flex_array_alloc(sizeof(info), nr_files,
1962 if (!fa || flex_array_prealloc(fa, 0, nr_files,
1966 flex_array_free(fa);
1967 up_read(&mm->mmap_sem);
1971 for (i = 0, vma = mm->mmap, pos = 2; vma;
1972 vma = vma->vm_next) {
1975 if (++pos <= ctx->pos)
1978 info.mode = vma->vm_file->f_mode;
1979 info.len = snprintf(info.name,
1980 sizeof(info.name), "%lx-%lx",
1981 vma->vm_start, vma->vm_end);
1982 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
1986 up_read(&mm->mmap_sem);
1988 for (i = 0; i < nr_files; i++) {
1989 p = flex_array_get(fa, i);
1990 if (!proc_fill_cache(file, ctx,
1992 proc_map_files_instantiate,
1994 (void *)(unsigned long)p->mode))
1999 flex_array_free(fa);
2003 put_task_struct(task);
2008 static const struct file_operations proc_map_files_operations = {
2009 .read = generic_read_dir,
2010 .iterate = proc_map_files_readdir,
2011 .llseek = default_llseek,
2014 struct timers_private {
2016 struct task_struct *task;
2017 struct sighand_struct *sighand;
2018 struct pid_namespace *ns;
2019 unsigned long flags;
2022 static void *timers_start(struct seq_file *m, loff_t *pos)
2024 struct timers_private *tp = m->private;
2026 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2028 return ERR_PTR(-ESRCH);
2030 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2032 return ERR_PTR(-ESRCH);
2034 return seq_list_start(&tp->task->signal->posix_timers, *pos);
2037 static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2039 struct timers_private *tp = m->private;
2040 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2043 static void timers_stop(struct seq_file *m, void *v)
2045 struct timers_private *tp = m->private;
2048 unlock_task_sighand(tp->task, &tp->flags);
2053 put_task_struct(tp->task);
2058 static int show_timer(struct seq_file *m, void *v)
2060 struct k_itimer *timer;
2061 struct timers_private *tp = m->private;
2063 static const char * const nstr[] = {
2064 [SIGEV_SIGNAL] = "signal",
2065 [SIGEV_NONE] = "none",
2066 [SIGEV_THREAD] = "thread",
2069 timer = list_entry((struct list_head *)v, struct k_itimer, list);
2070 notify = timer->it_sigev_notify;
2072 seq_printf(m, "ID: %d\n", timer->it_id);
2073 seq_printf(m, "signal: %d/%p\n", timer->sigq->info.si_signo,
2074 timer->sigq->info.si_value.sival_ptr);
2075 seq_printf(m, "notify: %s/%s.%d\n",
2076 nstr[notify & ~SIGEV_THREAD_ID],
2077 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2078 pid_nr_ns(timer->it_pid, tp->ns));
2079 seq_printf(m, "ClockID: %d\n", timer->it_clock);
2084 static const struct seq_operations proc_timers_seq_ops = {
2085 .start = timers_start,
2086 .next = timers_next,
2087 .stop = timers_stop,
2091 static int proc_timers_open(struct inode *inode, struct file *file)
2093 struct timers_private *tp;
2095 tp = __seq_open_private(file, &proc_timers_seq_ops,
2096 sizeof(struct timers_private));
2100 tp->pid = proc_pid(inode);
2101 tp->ns = inode->i_sb->s_fs_info;
2105 static const struct file_operations proc_timers_operations = {
2106 .open = proc_timers_open,
2108 .llseek = seq_lseek,
2109 .release = seq_release_private,
2111 #endif /* CONFIG_CHECKPOINT_RESTORE */
2113 static int proc_pident_instantiate(struct inode *dir,
2114 struct dentry *dentry, struct task_struct *task, const void *ptr)
2116 const struct pid_entry *p = ptr;
2117 struct inode *inode;
2118 struct proc_inode *ei;
2120 inode = proc_pid_make_inode(dir->i_sb, task);
2125 inode->i_mode = p->mode;
2126 if (S_ISDIR(inode->i_mode))
2127 set_nlink(inode, 2); /* Use getattr to fix if necessary */
2129 inode->i_op = p->iop;
2131 inode->i_fop = p->fop;
2133 d_set_d_op(dentry, &pid_dentry_operations);
2134 d_add(dentry, inode);
2135 /* Close the race of the process dying before we return the dentry */
2136 if (pid_revalidate(dentry, 0))
2142 static struct dentry *proc_pident_lookup(struct inode *dir,
2143 struct dentry *dentry,
2144 const struct pid_entry *ents,
2148 struct task_struct *task = get_proc_task(dir);
2149 const struct pid_entry *p, *last;
2157 * Yes, it does not scale. And it should not. Don't add
2158 * new entries into /proc/<tgid>/ without very good reasons.
2160 last = &ents[nents - 1];
2161 for (p = ents; p <= last; p++) {
2162 if (p->len != dentry->d_name.len)
2164 if (!memcmp(dentry->d_name.name, p->name, p->len))
2170 error = proc_pident_instantiate(dir, dentry, task, p);
2172 put_task_struct(task);
2174 return ERR_PTR(error);
2177 static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
2178 const struct pid_entry *ents, unsigned int nents)
2180 struct task_struct *task = get_proc_task(file_inode(file));
2181 const struct pid_entry *p;
2186 if (!dir_emit_dots(file, ctx))
2189 if (ctx->pos >= nents + 2)
2192 for (p = ents + (ctx->pos - 2); p <= ents + nents - 1; p++) {
2193 if (!proc_fill_cache(file, ctx, p->name, p->len,
2194 proc_pident_instantiate, task, p))
2199 put_task_struct(task);
2203 #ifdef CONFIG_SECURITY
2204 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2205 size_t count, loff_t *ppos)
2207 struct inode * inode = file_inode(file);
2210 struct task_struct *task = get_proc_task(inode);
2215 length = security_getprocattr(task,
2216 (char*)file->f_path.dentry->d_name.name,
2218 put_task_struct(task);
2220 length = simple_read_from_buffer(buf, count, ppos, p, length);
2225 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2226 size_t count, loff_t *ppos)
2228 struct inode * inode = file_inode(file);
2231 struct task_struct *task = get_proc_task(inode);
2236 if (count > PAGE_SIZE)
2239 /* No partial writes. */
2245 page = (char*)__get_free_page(GFP_TEMPORARY);
2250 if (copy_from_user(page, buf, count))
2253 /* Guard against adverse ptrace interaction */
2254 length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
2258 length = security_setprocattr(task,
2259 (char*)file->f_path.dentry->d_name.name,
2260 (void*)page, count);
2261 mutex_unlock(&task->signal->cred_guard_mutex);
2263 free_page((unsigned long) page);
2265 put_task_struct(task);
2270 static const struct file_operations proc_pid_attr_operations = {
2271 .read = proc_pid_attr_read,
2272 .write = proc_pid_attr_write,
2273 .llseek = generic_file_llseek,
2276 static const struct pid_entry attr_dir_stuff[] = {
2277 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2278 REG("prev", S_IRUGO, proc_pid_attr_operations),
2279 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2280 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2281 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2282 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2285 static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
2287 return proc_pident_readdir(file, ctx,
2288 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2291 static const struct file_operations proc_attr_dir_operations = {
2292 .read = generic_read_dir,
2293 .iterate = proc_attr_dir_readdir,
2294 .llseek = default_llseek,
2297 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2298 struct dentry *dentry, unsigned int flags)
2300 return proc_pident_lookup(dir, dentry,
2301 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2304 static const struct inode_operations proc_attr_dir_inode_operations = {
2305 .lookup = proc_attr_dir_lookup,
2306 .getattr = pid_getattr,
2307 .setattr = proc_setattr,
2312 #ifdef CONFIG_ELF_CORE
2313 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2314 size_t count, loff_t *ppos)
2316 struct task_struct *task = get_proc_task(file_inode(file));
2317 struct mm_struct *mm;
2318 char buffer[PROC_NUMBUF];
2326 mm = get_task_mm(task);
2328 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2329 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2330 MMF_DUMP_FILTER_SHIFT));
2332 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2335 put_task_struct(task);
2340 static ssize_t proc_coredump_filter_write(struct file *file,
2341 const char __user *buf,
2345 struct task_struct *task;
2346 struct mm_struct *mm;
2347 char buffer[PROC_NUMBUF], *end;
2354 memset(buffer, 0, sizeof(buffer));
2355 if (count > sizeof(buffer) - 1)
2356 count = sizeof(buffer) - 1;
2357 if (copy_from_user(buffer, buf, count))
2361 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2364 if (end - buffer == 0)
2368 task = get_proc_task(file_inode(file));
2373 mm = get_task_mm(task);
2377 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2379 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2381 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2386 put_task_struct(task);
2391 static const struct file_operations proc_coredump_filter_operations = {
2392 .read = proc_coredump_filter_read,
2393 .write = proc_coredump_filter_write,
2394 .llseek = generic_file_llseek,
2398 #ifdef CONFIG_TASK_IO_ACCOUNTING
2399 static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
2401 struct task_io_accounting acct = task->ioac;
2402 unsigned long flags;
2405 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2409 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
2414 if (whole && lock_task_sighand(task, &flags)) {
2415 struct task_struct *t = task;
2417 task_io_accounting_add(&acct, &task->signal->ioac);
2418 while_each_thread(task, t)
2419 task_io_accounting_add(&acct, &t->ioac);
2421 unlock_task_sighand(task, &flags);
2423 result = sprintf(buffer,
2428 "read_bytes: %llu\n"
2429 "write_bytes: %llu\n"
2430 "cancelled_write_bytes: %llu\n",
2431 (unsigned long long)acct.rchar,
2432 (unsigned long long)acct.wchar,
2433 (unsigned long long)acct.syscr,
2434 (unsigned long long)acct.syscw,
2435 (unsigned long long)acct.read_bytes,
2436 (unsigned long long)acct.write_bytes,
2437 (unsigned long long)acct.cancelled_write_bytes);
2439 mutex_unlock(&task->signal->cred_guard_mutex);
2443 static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2445 return do_io_accounting(task, buffer, 0);
2448 static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2450 return do_io_accounting(task, buffer, 1);
2452 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2454 #ifdef CONFIG_USER_NS
2455 static int proc_id_map_open(struct inode *inode, struct file *file,
2456 const struct seq_operations *seq_ops)
2458 struct user_namespace *ns = NULL;
2459 struct task_struct *task;
2460 struct seq_file *seq;
2463 task = get_proc_task(inode);
2466 ns = get_user_ns(task_cred_xxx(task, user_ns));
2468 put_task_struct(task);
2473 ret = seq_open(file, seq_ops);
2477 seq = file->private_data;
2487 static int proc_id_map_release(struct inode *inode, struct file *file)
2489 struct seq_file *seq = file->private_data;
2490 struct user_namespace *ns = seq->private;
2492 return seq_release(inode, file);
2495 static int proc_uid_map_open(struct inode *inode, struct file *file)
2497 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2500 static int proc_gid_map_open(struct inode *inode, struct file *file)
2502 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2505 static int proc_projid_map_open(struct inode *inode, struct file *file)
2507 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2510 static const struct file_operations proc_uid_map_operations = {
2511 .open = proc_uid_map_open,
2512 .write = proc_uid_map_write,
2514 .llseek = seq_lseek,
2515 .release = proc_id_map_release,
2518 static const struct file_operations proc_gid_map_operations = {
2519 .open = proc_gid_map_open,
2520 .write = proc_gid_map_write,
2522 .llseek = seq_lseek,
2523 .release = proc_id_map_release,
2526 static const struct file_operations proc_projid_map_operations = {
2527 .open = proc_projid_map_open,
2528 .write = proc_projid_map_write,
2530 .llseek = seq_lseek,
2531 .release = proc_id_map_release,
2533 #endif /* CONFIG_USER_NS */
2535 static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2536 struct pid *pid, struct task_struct *task)
2538 int err = lock_trace(task);
2540 seq_printf(m, "%08x\n", task->personality);
2549 static const struct file_operations proc_task_operations;
2550 static const struct inode_operations proc_task_inode_operations;
2552 static const struct pid_entry tgid_base_stuff[] = {
2553 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2554 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2555 #ifdef CONFIG_CHECKPOINT_RESTORE
2556 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
2558 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2559 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
2561 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2563 REG("environ", S_IRUSR, proc_environ_operations),
2564 ONE("auxv", S_IRUSR, proc_pid_auxv),
2565 ONE("status", S_IRUGO, proc_pid_status),
2566 ONE("personality", S_IRUSR, proc_pid_personality),
2567 ONE("limits", S_IRUGO, proc_pid_limits),
2568 #ifdef CONFIG_SCHED_DEBUG
2569 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2571 #ifdef CONFIG_SCHED_AUTOGROUP
2572 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2574 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2575 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2576 ONE("syscall", S_IRUSR, proc_pid_syscall),
2578 ONE("cmdline", S_IRUGO, proc_pid_cmdline),
2579 ONE("stat", S_IRUGO, proc_tgid_stat),
2580 ONE("statm", S_IRUGO, proc_pid_statm),
2581 REG("maps", S_IRUGO, proc_pid_maps_operations),
2583 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
2585 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2586 LNK("cwd", proc_cwd_link),
2587 LNK("root", proc_root_link),
2588 LNK("exe", proc_exe_link),
2589 REG("mounts", S_IRUGO, proc_mounts_operations),
2590 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2591 REG("mountstats", S_IRUSR, proc_mountstats_operations),
2592 #ifdef CONFIG_PROC_PAGE_MONITOR
2593 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2594 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
2595 REG("pagemap", S_IRUSR, proc_pagemap_operations),
2597 #ifdef CONFIG_SECURITY
2598 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
2600 #ifdef CONFIG_KALLSYMS
2601 ONE("wchan", S_IRUGO, proc_pid_wchan),
2603 #ifdef CONFIG_STACKTRACE
2604 ONE("stack", S_IRUSR, proc_pid_stack),
2606 #ifdef CONFIG_SCHEDSTATS
2607 INF("schedstat", S_IRUGO, proc_pid_schedstat),
2609 #ifdef CONFIG_LATENCYTOP
2610 REG("latency", S_IRUGO, proc_lstats_operations),
2612 #ifdef CONFIG_PROC_PID_CPUSET
2613 REG("cpuset", S_IRUGO, proc_cpuset_operations),
2615 #ifdef CONFIG_CGROUPS
2616 REG("cgroup", S_IRUGO, proc_cgroup_operations),
2618 INF("oom_score", S_IRUGO, proc_oom_score),
2619 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
2620 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
2621 #ifdef CONFIG_AUDITSYSCALL
2622 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2623 REG("sessionid", S_IRUGO, proc_sessionid_operations),
2625 #ifdef CONFIG_FAULT_INJECTION
2626 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2628 #ifdef CONFIG_ELF_CORE
2629 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
2631 #ifdef CONFIG_TASK_IO_ACCOUNTING
2632 INF("io", S_IRUSR, proc_tgid_io_accounting),
2634 #ifdef CONFIG_HARDWALL
2635 INF("hardwall", S_IRUGO, proc_pid_hardwall),
2637 #ifdef CONFIG_USER_NS
2638 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2639 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
2640 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
2642 #ifdef CONFIG_CHECKPOINT_RESTORE
2643 REG("timers", S_IRUGO, proc_timers_operations),
2647 static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
2649 return proc_pident_readdir(file, ctx,
2650 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2653 static const struct file_operations proc_tgid_base_operations = {
2654 .read = generic_read_dir,
2655 .iterate = proc_tgid_base_readdir,
2656 .llseek = default_llseek,
2659 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2661 return proc_pident_lookup(dir, dentry,
2662 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2665 static const struct inode_operations proc_tgid_base_inode_operations = {
2666 .lookup = proc_tgid_base_lookup,
2667 .getattr = pid_getattr,
2668 .setattr = proc_setattr,
2669 .permission = proc_pid_permission,
2672 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
2674 struct dentry *dentry, *leader, *dir;
2675 char buf[PROC_NUMBUF];
2679 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2680 /* no ->d_hash() rejects on procfs */
2681 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
2683 shrink_dcache_parent(dentry);
2689 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2690 leader = d_hash_and_lookup(mnt->mnt_root, &name);
2695 name.len = strlen(name.name);
2696 dir = d_hash_and_lookup(leader, &name);
2698 goto out_put_leader;
2701 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2702 dentry = d_hash_and_lookup(dir, &name);
2704 shrink_dcache_parent(dentry);
2717 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2718 * @task: task that should be flushed.
2720 * When flushing dentries from proc, one needs to flush them from global
2721 * proc (proc_mnt) and from all the namespaces' procs this task was seen
2722 * in. This call is supposed to do all of this job.
2724 * Looks in the dcache for
2726 * /proc/@tgid/task/@pid
2727 * if either directory is present flushes it and all of it'ts children
2730 * It is safe and reasonable to cache /proc entries for a task until
2731 * that task exits. After that they just clog up the dcache with
2732 * useless entries, possibly causing useful dcache entries to be
2733 * flushed instead. This routine is proved to flush those useless
2734 * dcache entries at process exit time.
2736 * NOTE: This routine is just an optimization so it does not guarantee
2737 * that no dcache entries will exist at process exit time it
2738 * just makes it very unlikely that any will persist.
2741 void proc_flush_task(struct task_struct *task)
2744 struct pid *pid, *tgid;
2747 pid = task_pid(task);
2748 tgid = task_tgid(task);
2750 for (i = 0; i <= pid->level; i++) {
2751 upid = &pid->numbers[i];
2752 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
2753 tgid->numbers[i].nr);
2757 static int proc_pid_instantiate(struct inode *dir,
2758 struct dentry * dentry,
2759 struct task_struct *task, const void *ptr)
2761 struct inode *inode;
2763 inode = proc_pid_make_inode(dir->i_sb, task);
2767 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2768 inode->i_op = &proc_tgid_base_inode_operations;
2769 inode->i_fop = &proc_tgid_base_operations;
2770 inode->i_flags|=S_IMMUTABLE;
2772 set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
2773 ARRAY_SIZE(tgid_base_stuff)));
2775 d_set_d_op(dentry, &pid_dentry_operations);
2777 d_add(dentry, inode);
2778 /* Close the race of the process dying before we return the dentry */
2779 if (pid_revalidate(dentry, 0))
2785 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
2787 int result = -ENOENT;
2788 struct task_struct *task;
2790 struct pid_namespace *ns;
2792 tgid = name_to_int(&dentry->d_name);
2796 ns = dentry->d_sb->s_fs_info;
2798 task = find_task_by_pid_ns(tgid, ns);
2800 get_task_struct(task);
2805 result = proc_pid_instantiate(dir, dentry, task, NULL);
2806 put_task_struct(task);
2808 return ERR_PTR(result);
2812 * Find the first task with tgid >= tgid
2817 struct task_struct *task;
2819 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2824 put_task_struct(iter.task);
2828 pid = find_ge_pid(iter.tgid, ns);
2830 iter.tgid = pid_nr_ns(pid, ns);
2831 iter.task = pid_task(pid, PIDTYPE_PID);
2832 /* What we to know is if the pid we have find is the
2833 * pid of a thread_group_leader. Testing for task
2834 * being a thread_group_leader is the obvious thing
2835 * todo but there is a window when it fails, due to
2836 * the pid transfer logic in de_thread.
2838 * So we perform the straight forward test of seeing
2839 * if the pid we have found is the pid of a thread
2840 * group leader, and don't worry if the task we have
2841 * found doesn't happen to be a thread group leader.
2842 * As we don't care in the case of readdir.
2844 if (!iter.task || !has_group_leader_pid(iter.task)) {
2848 get_task_struct(iter.task);
2854 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + 1)
2856 /* for the /proc/ directory itself, after non-process stuff has been done */
2857 int proc_pid_readdir(struct file *file, struct dir_context *ctx)
2859 struct tgid_iter iter;
2860 struct pid_namespace *ns = file->f_dentry->d_sb->s_fs_info;
2861 loff_t pos = ctx->pos;
2863 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
2866 if (pos == TGID_OFFSET - 1) {
2867 struct inode *inode = ns->proc_self->d_inode;
2868 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
2872 iter.tgid = pos - TGID_OFFSET;
2875 for (iter = next_tgid(ns, iter);
2877 iter.tgid += 1, iter = next_tgid(ns, iter)) {
2878 char name[PROC_NUMBUF];
2880 if (!has_pid_permissions(ns, iter.task, 2))
2883 len = snprintf(name, sizeof(name), "%d", iter.tgid);
2884 ctx->pos = iter.tgid + TGID_OFFSET;
2885 if (!proc_fill_cache(file, ctx, name, len,
2886 proc_pid_instantiate, iter.task, NULL)) {
2887 put_task_struct(iter.task);
2891 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
2898 static const struct pid_entry tid_base_stuff[] = {
2899 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2900 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2901 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
2902 REG("environ", S_IRUSR, proc_environ_operations),
2903 ONE("auxv", S_IRUSR, proc_pid_auxv),
2904 ONE("status", S_IRUGO, proc_pid_status),
2905 ONE("personality", S_IRUSR, proc_pid_personality),
2906 ONE("limits", S_IRUGO, proc_pid_limits),
2907 #ifdef CONFIG_SCHED_DEBUG
2908 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2910 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2911 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2912 ONE("syscall", S_IRUSR, proc_pid_syscall),
2914 ONE("cmdline", S_IRUGO, proc_pid_cmdline),
2915 ONE("stat", S_IRUGO, proc_tid_stat),
2916 ONE("statm", S_IRUGO, proc_pid_statm),
2917 REG("maps", S_IRUGO, proc_tid_maps_operations),
2918 #ifdef CONFIG_CHECKPOINT_RESTORE
2919 REG("children", S_IRUGO, proc_tid_children_operations),
2922 REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
2924 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2925 LNK("cwd", proc_cwd_link),
2926 LNK("root", proc_root_link),
2927 LNK("exe", proc_exe_link),
2928 REG("mounts", S_IRUGO, proc_mounts_operations),
2929 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2930 #ifdef CONFIG_PROC_PAGE_MONITOR
2931 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2932 REG("smaps", S_IRUGO, proc_tid_smaps_operations),
2933 REG("pagemap", S_IRUSR, proc_pagemap_operations),
2935 #ifdef CONFIG_SECURITY
2936 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
2938 #ifdef CONFIG_KALLSYMS
2939 ONE("wchan", S_IRUGO, proc_pid_wchan),
2941 #ifdef CONFIG_STACKTRACE
2942 ONE("stack", S_IRUSR, proc_pid_stack),
2944 #ifdef CONFIG_SCHEDSTATS
2945 INF("schedstat", S_IRUGO, proc_pid_schedstat),
2947 #ifdef CONFIG_LATENCYTOP
2948 REG("latency", S_IRUGO, proc_lstats_operations),
2950 #ifdef CONFIG_PROC_PID_CPUSET
2951 REG("cpuset", S_IRUGO, proc_cpuset_operations),
2953 #ifdef CONFIG_CGROUPS
2954 REG("cgroup", S_IRUGO, proc_cgroup_operations),
2956 INF("oom_score", S_IRUGO, proc_oom_score),
2957 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
2958 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
2959 #ifdef CONFIG_AUDITSYSCALL
2960 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2961 REG("sessionid", S_IRUGO, proc_sessionid_operations),
2963 #ifdef CONFIG_FAULT_INJECTION
2964 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2966 #ifdef CONFIG_TASK_IO_ACCOUNTING
2967 INF("io", S_IRUSR, proc_tid_io_accounting),
2969 #ifdef CONFIG_HARDWALL
2970 INF("hardwall", S_IRUGO, proc_pid_hardwall),
2972 #ifdef CONFIG_USER_NS
2973 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2974 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
2975 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
2979 static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
2981 return proc_pident_readdir(file, ctx,
2982 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
2985 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2987 return proc_pident_lookup(dir, dentry,
2988 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
2991 static const struct file_operations proc_tid_base_operations = {
2992 .read = generic_read_dir,
2993 .iterate = proc_tid_base_readdir,
2994 .llseek = default_llseek,
2997 static const struct inode_operations proc_tid_base_inode_operations = {
2998 .lookup = proc_tid_base_lookup,
2999 .getattr = pid_getattr,
3000 .setattr = proc_setattr,
3003 static int proc_task_instantiate(struct inode *dir,
3004 struct dentry *dentry, struct task_struct *task, const void *ptr)
3006 struct inode *inode;
3007 inode = proc_pid_make_inode(dir->i_sb, task);
3011 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3012 inode->i_op = &proc_tid_base_inode_operations;
3013 inode->i_fop = &proc_tid_base_operations;
3014 inode->i_flags|=S_IMMUTABLE;
3016 set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3017 ARRAY_SIZE(tid_base_stuff)));
3019 d_set_d_op(dentry, &pid_dentry_operations);
3021 d_add(dentry, inode);
3022 /* Close the race of the process dying before we return the dentry */
3023 if (pid_revalidate(dentry, 0))
3029 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
3031 int result = -ENOENT;
3032 struct task_struct *task;
3033 struct task_struct *leader = get_proc_task(dir);
3035 struct pid_namespace *ns;
3040 tid = name_to_int(&dentry->d_name);
3044 ns = dentry->d_sb->s_fs_info;
3046 task = find_task_by_pid_ns(tid, ns);
3048 get_task_struct(task);
3052 if (!same_thread_group(leader, task))
3055 result = proc_task_instantiate(dir, dentry, task, NULL);
3057 put_task_struct(task);
3059 put_task_struct(leader);
3061 return ERR_PTR(result);
3065 * Find the first tid of a thread group to return to user space.
3067 * Usually this is just the thread group leader, but if the users
3068 * buffer was too small or there was a seek into the middle of the
3069 * directory we have more work todo.
3071 * In the case of a short read we start with find_task_by_pid.
3073 * In the case of a seek we start with the leader and walk nr
3076 static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
3077 struct pid_namespace *ns)
3079 struct task_struct *pos, *task;
3080 unsigned long nr = f_pos;
3082 if (nr != f_pos) /* 32bit overflow? */
3086 task = pid_task(pid, PIDTYPE_PID);
3090 /* Attempt to start with the tid of a thread */
3092 pos = find_task_by_pid_ns(tid, ns);
3093 if (pos && same_thread_group(pos, task))
3097 /* If nr exceeds the number of threads there is nothing todo */
3098 if (nr >= get_nr_threads(task))
3101 /* If we haven't found our starting place yet start
3102 * with the leader and walk nr threads forward.
3104 pos = task = task->group_leader;
3108 } while_each_thread(task, pos);
3113 get_task_struct(pos);
3120 * Find the next thread in the thread list.
3121 * Return NULL if there is an error or no next thread.
3123 * The reference to the input task_struct is released.
3125 static struct task_struct *next_tid(struct task_struct *start)
3127 struct task_struct *pos = NULL;
3129 if (pid_alive(start)) {
3130 pos = next_thread(start);
3131 if (thread_group_leader(pos))
3134 get_task_struct(pos);
3137 put_task_struct(start);
3141 /* for the /proc/TGID/task/ directories */
3142 static int proc_task_readdir(struct file *file, struct dir_context *ctx)
3144 struct inode *inode = file_inode(file);
3145 struct task_struct *task;
3146 struct pid_namespace *ns;
3149 if (proc_inode_is_dead(inode))
3152 if (!dir_emit_dots(file, ctx))
3155 /* f_version caches the tgid value that the last readdir call couldn't
3156 * return. lseek aka telldir automagically resets f_version to 0.
3158 ns = file->f_dentry->d_sb->s_fs_info;
3159 tid = (int)file->f_version;
3160 file->f_version = 0;
3161 for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
3163 task = next_tid(task), ctx->pos++) {
3164 char name[PROC_NUMBUF];
3166 tid = task_pid_nr_ns(task, ns);
3167 len = snprintf(name, sizeof(name), "%d", tid);
3168 if (!proc_fill_cache(file, ctx, name, len,
3169 proc_task_instantiate, task, NULL)) {
3170 /* returning this tgid failed, save it as the first
3171 * pid for the next readir call */
3172 file->f_version = (u64)tid;
3173 put_task_struct(task);
3181 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3183 struct inode *inode = dentry->d_inode;
3184 struct task_struct *p = get_proc_task(inode);
3185 generic_fillattr(inode, stat);
3188 stat->nlink += get_nr_threads(p);
3195 static const struct inode_operations proc_task_inode_operations = {
3196 .lookup = proc_task_lookup,
3197 .getattr = proc_task_getattr,
3198 .setattr = proc_setattr,
3199 .permission = proc_pid_permission,
3202 static const struct file_operations proc_task_operations = {
3203 .read = generic_read_dir,
3204 .iterate = proc_task_readdir,
3205 .llseek = default_llseek,