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 seq_file *m, struct pid_namespace *ns,
314 struct pid *pid, struct task_struct *task)
316 return seq_printf(m, "%llu %llu %lu\n",
317 (unsigned long long)task->se.sum_exec_runtime,
318 (unsigned long long)task->sched_info.run_delay,
319 task->sched_info.pcount);
323 #ifdef CONFIG_LATENCYTOP
324 static int lstats_show_proc(struct seq_file *m, void *v)
327 struct inode *inode = m->private;
328 struct task_struct *task = get_proc_task(inode);
332 seq_puts(m, "Latency Top version : v0.1\n");
333 for (i = 0; i < 32; i++) {
334 struct latency_record *lr = &task->latency_record[i];
335 if (lr->backtrace[0]) {
337 seq_printf(m, "%i %li %li",
338 lr->count, lr->time, lr->max);
339 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
340 unsigned long bt = lr->backtrace[q];
345 seq_printf(m, " %ps", (void *)bt);
351 put_task_struct(task);
355 static int lstats_open(struct inode *inode, struct file *file)
357 return single_open(file, lstats_show_proc, inode);
360 static ssize_t lstats_write(struct file *file, const char __user *buf,
361 size_t count, loff_t *offs)
363 struct task_struct *task = get_proc_task(file_inode(file));
367 clear_all_latency_tracing(task);
368 put_task_struct(task);
373 static const struct file_operations proc_lstats_operations = {
376 .write = lstats_write,
378 .release = single_release,
383 #ifdef CONFIG_CGROUPS
384 static int cgroup_open(struct inode *inode, struct file *file)
386 struct pid *pid = PROC_I(inode)->pid;
387 return single_open(file, proc_cgroup_show, pid);
390 static const struct file_operations proc_cgroup_operations = {
394 .release = single_release,
398 #ifdef CONFIG_PROC_PID_CPUSET
400 static int cpuset_open(struct inode *inode, struct file *file)
402 struct pid *pid = PROC_I(inode)->pid;
403 return single_open(file, proc_cpuset_show, pid);
406 static const struct file_operations proc_cpuset_operations = {
410 .release = single_release,
414 static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns,
415 struct pid *pid, struct task_struct *task)
417 unsigned long totalpages = totalram_pages + total_swap_pages;
418 unsigned long points = 0;
420 read_lock(&tasklist_lock);
422 points = oom_badness(task, NULL, NULL, totalpages) *
424 read_unlock(&tasklist_lock);
425 return seq_printf(m, "%lu\n", points);
433 static const struct limit_names lnames[RLIM_NLIMITS] = {
434 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
435 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
436 [RLIMIT_DATA] = {"Max data size", "bytes"},
437 [RLIMIT_STACK] = {"Max stack size", "bytes"},
438 [RLIMIT_CORE] = {"Max core file size", "bytes"},
439 [RLIMIT_RSS] = {"Max resident set", "bytes"},
440 [RLIMIT_NPROC] = {"Max processes", "processes"},
441 [RLIMIT_NOFILE] = {"Max open files", "files"},
442 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
443 [RLIMIT_AS] = {"Max address space", "bytes"},
444 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
445 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
446 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
447 [RLIMIT_NICE] = {"Max nice priority", NULL},
448 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
449 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
452 /* Display limits for a process */
453 static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
454 struct pid *pid, struct task_struct *task)
459 struct rlimit rlim[RLIM_NLIMITS];
461 if (!lock_task_sighand(task, &flags))
463 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
464 unlock_task_sighand(task, &flags);
467 * print the file header
469 seq_printf(m, "%-25s %-20s %-20s %-10s\n",
470 "Limit", "Soft Limit", "Hard Limit", "Units");
472 for (i = 0; i < RLIM_NLIMITS; i++) {
473 if (rlim[i].rlim_cur == RLIM_INFINITY)
474 seq_printf(m, "%-25s %-20s ",
475 lnames[i].name, "unlimited");
477 seq_printf(m, "%-25s %-20lu ",
478 lnames[i].name, rlim[i].rlim_cur);
480 if (rlim[i].rlim_max == RLIM_INFINITY)
481 seq_printf(m, "%-20s ", "unlimited");
483 seq_printf(m, "%-20lu ", rlim[i].rlim_max);
486 seq_printf(m, "%-10s\n", lnames[i].unit);
494 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
495 static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
496 struct pid *pid, struct task_struct *task)
499 unsigned long args[6], sp, pc;
500 int res = lock_trace(task);
504 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
505 seq_puts(m, "running\n");
507 seq_printf(m, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
510 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
512 args[0], args[1], args[2], args[3], args[4], args[5],
517 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
519 /************************************************************************/
520 /* Here the fs part begins */
521 /************************************************************************/
523 /* permission checks */
524 static int proc_fd_access_allowed(struct inode *inode)
526 struct task_struct *task;
528 /* Allow access to a task's file descriptors if it is us or we
529 * may use ptrace attach to the process and find out that
532 task = get_proc_task(inode);
534 allowed = ptrace_may_access(task, PTRACE_MODE_READ);
535 put_task_struct(task);
540 int proc_setattr(struct dentry *dentry, struct iattr *attr)
543 struct inode *inode = dentry->d_inode;
545 if (attr->ia_valid & ATTR_MODE)
548 error = inode_change_ok(inode, attr);
552 setattr_copy(inode, attr);
553 mark_inode_dirty(inode);
558 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
559 * or euid/egid (for hide_pid_min=2)?
561 static bool has_pid_permissions(struct pid_namespace *pid,
562 struct task_struct *task,
565 if (pid->hide_pid < hide_pid_min)
567 if (in_group_p(pid->pid_gid))
569 return ptrace_may_access(task, PTRACE_MODE_READ);
573 static int proc_pid_permission(struct inode *inode, int mask)
575 struct pid_namespace *pid = inode->i_sb->s_fs_info;
576 struct task_struct *task;
579 task = get_proc_task(inode);
582 has_perms = has_pid_permissions(pid, task, 1);
583 put_task_struct(task);
586 if (pid->hide_pid == 2) {
588 * Let's make getdents(), stat(), and open()
589 * consistent with each other. If a process
590 * may not stat() a file, it shouldn't be seen
598 return generic_permission(inode, mask);
603 static const struct inode_operations proc_def_inode_operations = {
604 .setattr = proc_setattr,
607 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
609 static ssize_t proc_info_read(struct file * file, char __user * buf,
610 size_t count, loff_t *ppos)
612 struct inode * inode = file_inode(file);
615 struct task_struct *task = get_proc_task(inode);
621 if (count > PROC_BLOCK_SIZE)
622 count = PROC_BLOCK_SIZE;
625 if (!(page = __get_free_page(GFP_TEMPORARY)))
628 length = PROC_I(inode)->op.proc_read(task, (char*)page);
631 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
634 put_task_struct(task);
639 static const struct file_operations proc_info_file_operations = {
640 .read = proc_info_read,
641 .llseek = generic_file_llseek,
644 static int proc_single_show(struct seq_file *m, void *v)
646 struct inode *inode = m->private;
647 struct pid_namespace *ns;
649 struct task_struct *task;
652 ns = inode->i_sb->s_fs_info;
653 pid = proc_pid(inode);
654 task = get_pid_task(pid, PIDTYPE_PID);
658 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
660 put_task_struct(task);
664 static int proc_single_open(struct inode *inode, struct file *filp)
666 return single_open(filp, proc_single_show, inode);
669 static const struct file_operations proc_single_file_operations = {
670 .open = proc_single_open,
673 .release = single_release,
676 static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
678 struct task_struct *task = get_proc_task(file_inode(file));
679 struct mm_struct *mm;
684 mm = mm_access(task, mode);
685 put_task_struct(task);
691 /* ensure this mm_struct can't be freed */
692 atomic_inc(&mm->mm_count);
693 /* but do not pin its memory */
697 file->private_data = mm;
702 static int mem_open(struct inode *inode, struct file *file)
704 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
706 /* OK to pass negative loff_t, we can catch out-of-range */
707 file->f_mode |= FMODE_UNSIGNED_OFFSET;
712 static ssize_t mem_rw(struct file *file, char __user *buf,
713 size_t count, loff_t *ppos, int write)
715 struct mm_struct *mm = file->private_data;
716 unsigned long addr = *ppos;
723 page = (char *)__get_free_page(GFP_TEMPORARY);
728 if (!atomic_inc_not_zero(&mm->mm_users))
732 int this_len = min_t(int, count, PAGE_SIZE);
734 if (write && copy_from_user(page, buf, this_len)) {
739 this_len = access_remote_vm(mm, addr, page, this_len, write);
746 if (!write && copy_to_user(buf, page, this_len)) {
760 free_page((unsigned long) page);
764 static ssize_t mem_read(struct file *file, char __user *buf,
765 size_t count, loff_t *ppos)
767 return mem_rw(file, buf, count, ppos, 0);
770 static ssize_t mem_write(struct file *file, const char __user *buf,
771 size_t count, loff_t *ppos)
773 return mem_rw(file, (char __user*)buf, count, ppos, 1);
776 loff_t mem_lseek(struct file *file, loff_t offset, int orig)
780 file->f_pos = offset;
783 file->f_pos += offset;
788 force_successful_syscall_return();
792 static int mem_release(struct inode *inode, struct file *file)
794 struct mm_struct *mm = file->private_data;
800 static const struct file_operations proc_mem_operations = {
805 .release = mem_release,
808 static int environ_open(struct inode *inode, struct file *file)
810 return __mem_open(inode, file, PTRACE_MODE_READ);
813 static ssize_t environ_read(struct file *file, char __user *buf,
814 size_t count, loff_t *ppos)
817 unsigned long src = *ppos;
819 struct mm_struct *mm = file->private_data;
824 page = (char *)__get_free_page(GFP_TEMPORARY);
829 if (!atomic_inc_not_zero(&mm->mm_users))
832 size_t this_len, max_len;
835 if (src >= (mm->env_end - mm->env_start))
838 this_len = mm->env_end - (mm->env_start + src);
840 max_len = min_t(size_t, PAGE_SIZE, count);
841 this_len = min(max_len, this_len);
843 retval = access_remote_vm(mm, (mm->env_start + src),
851 if (copy_to_user(buf, page, retval)) {
865 free_page((unsigned long) page);
869 static const struct file_operations proc_environ_operations = {
870 .open = environ_open,
871 .read = environ_read,
872 .llseek = generic_file_llseek,
873 .release = mem_release,
876 static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
879 struct task_struct *task = get_proc_task(file_inode(file));
880 char buffer[PROC_NUMBUF];
881 int oom_adj = OOM_ADJUST_MIN;
887 if (lock_task_sighand(task, &flags)) {
888 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
889 oom_adj = OOM_ADJUST_MAX;
891 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
893 unlock_task_sighand(task, &flags);
895 put_task_struct(task);
896 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
897 return simple_read_from_buffer(buf, count, ppos, buffer, len);
900 static ssize_t oom_adj_write(struct file *file, const char __user *buf,
901 size_t count, loff_t *ppos)
903 struct task_struct *task;
904 char buffer[PROC_NUMBUF];
909 memset(buffer, 0, sizeof(buffer));
910 if (count > sizeof(buffer) - 1)
911 count = sizeof(buffer) - 1;
912 if (copy_from_user(buffer, buf, count)) {
917 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
920 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
921 oom_adj != OOM_DISABLE) {
926 task = get_proc_task(file_inode(file));
938 if (!lock_task_sighand(task, &flags)) {
944 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
945 * value is always attainable.
947 if (oom_adj == OOM_ADJUST_MAX)
948 oom_adj = OOM_SCORE_ADJ_MAX;
950 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
952 if (oom_adj < task->signal->oom_score_adj &&
953 !capable(CAP_SYS_RESOURCE)) {
959 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
960 * /proc/pid/oom_score_adj instead.
962 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
963 current->comm, task_pid_nr(current), task_pid_nr(task),
966 task->signal->oom_score_adj = oom_adj;
967 trace_oom_score_adj_update(task);
969 unlock_task_sighand(task, &flags);
972 put_task_struct(task);
974 return err < 0 ? err : count;
977 static const struct file_operations proc_oom_adj_operations = {
978 .read = oom_adj_read,
979 .write = oom_adj_write,
980 .llseek = generic_file_llseek,
983 static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
984 size_t count, loff_t *ppos)
986 struct task_struct *task = get_proc_task(file_inode(file));
987 char buffer[PROC_NUMBUF];
988 short oom_score_adj = OOM_SCORE_ADJ_MIN;
994 if (lock_task_sighand(task, &flags)) {
995 oom_score_adj = task->signal->oom_score_adj;
996 unlock_task_sighand(task, &flags);
998 put_task_struct(task);
999 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
1000 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1003 static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1004 size_t count, loff_t *ppos)
1006 struct task_struct *task;
1007 char buffer[PROC_NUMBUF];
1008 unsigned long flags;
1012 memset(buffer, 0, sizeof(buffer));
1013 if (count > sizeof(buffer) - 1)
1014 count = sizeof(buffer) - 1;
1015 if (copy_from_user(buffer, buf, count)) {
1020 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
1023 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
1024 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1029 task = get_proc_task(file_inode(file));
1041 if (!lock_task_sighand(task, &flags)) {
1046 if ((short)oom_score_adj < task->signal->oom_score_adj_min &&
1047 !capable(CAP_SYS_RESOURCE)) {
1052 task->signal->oom_score_adj = (short)oom_score_adj;
1053 if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
1054 task->signal->oom_score_adj_min = (short)oom_score_adj;
1055 trace_oom_score_adj_update(task);
1058 unlock_task_sighand(task, &flags);
1061 put_task_struct(task);
1063 return err < 0 ? err : count;
1066 static const struct file_operations proc_oom_score_adj_operations = {
1067 .read = oom_score_adj_read,
1068 .write = oom_score_adj_write,
1069 .llseek = default_llseek,
1072 #ifdef CONFIG_AUDITSYSCALL
1073 #define TMPBUFLEN 21
1074 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1075 size_t count, loff_t *ppos)
1077 struct inode * inode = file_inode(file);
1078 struct task_struct *task = get_proc_task(inode);
1080 char tmpbuf[TMPBUFLEN];
1084 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1085 from_kuid(file->f_cred->user_ns,
1086 audit_get_loginuid(task)));
1087 put_task_struct(task);
1088 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1091 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1092 size_t count, loff_t *ppos)
1094 struct inode * inode = file_inode(file);
1101 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1107 if (count >= PAGE_SIZE)
1108 count = PAGE_SIZE - 1;
1111 /* No partial writes. */
1114 page = (char*)__get_free_page(GFP_TEMPORARY);
1118 if (copy_from_user(page, buf, count))
1122 loginuid = simple_strtoul(page, &tmp, 10);
1129 /* is userspace tring to explicitly UNSET the loginuid? */
1130 if (loginuid == AUDIT_UID_UNSET) {
1131 kloginuid = INVALID_UID;
1133 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
1134 if (!uid_valid(kloginuid)) {
1140 length = audit_set_loginuid(kloginuid);
1141 if (likely(length == 0))
1145 free_page((unsigned long) page);
1149 static const struct file_operations proc_loginuid_operations = {
1150 .read = proc_loginuid_read,
1151 .write = proc_loginuid_write,
1152 .llseek = generic_file_llseek,
1155 static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1156 size_t count, loff_t *ppos)
1158 struct inode * inode = file_inode(file);
1159 struct task_struct *task = get_proc_task(inode);
1161 char tmpbuf[TMPBUFLEN];
1165 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1166 audit_get_sessionid(task));
1167 put_task_struct(task);
1168 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1171 static const struct file_operations proc_sessionid_operations = {
1172 .read = proc_sessionid_read,
1173 .llseek = generic_file_llseek,
1177 #ifdef CONFIG_FAULT_INJECTION
1178 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1179 size_t count, loff_t *ppos)
1181 struct task_struct *task = get_proc_task(file_inode(file));
1182 char buffer[PROC_NUMBUF];
1188 make_it_fail = task->make_it_fail;
1189 put_task_struct(task);
1191 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1193 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1196 static ssize_t proc_fault_inject_write(struct file * file,
1197 const char __user * buf, size_t count, loff_t *ppos)
1199 struct task_struct *task;
1200 char buffer[PROC_NUMBUF], *end;
1203 if (!capable(CAP_SYS_RESOURCE))
1205 memset(buffer, 0, sizeof(buffer));
1206 if (count > sizeof(buffer) - 1)
1207 count = sizeof(buffer) - 1;
1208 if (copy_from_user(buffer, buf, count))
1210 make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1213 if (make_it_fail < 0 || make_it_fail > 1)
1216 task = get_proc_task(file_inode(file));
1219 task->make_it_fail = make_it_fail;
1220 put_task_struct(task);
1225 static const struct file_operations proc_fault_inject_operations = {
1226 .read = proc_fault_inject_read,
1227 .write = proc_fault_inject_write,
1228 .llseek = generic_file_llseek,
1233 #ifdef CONFIG_SCHED_DEBUG
1235 * Print out various scheduling related per-task fields:
1237 static int sched_show(struct seq_file *m, void *v)
1239 struct inode *inode = m->private;
1240 struct task_struct *p;
1242 p = get_proc_task(inode);
1245 proc_sched_show_task(p, m);
1253 sched_write(struct file *file, const char __user *buf,
1254 size_t count, loff_t *offset)
1256 struct inode *inode = file_inode(file);
1257 struct task_struct *p;
1259 p = get_proc_task(inode);
1262 proc_sched_set_task(p);
1269 static int sched_open(struct inode *inode, struct file *filp)
1271 return single_open(filp, sched_show, inode);
1274 static const struct file_operations proc_pid_sched_operations = {
1277 .write = sched_write,
1278 .llseek = seq_lseek,
1279 .release = single_release,
1284 #ifdef CONFIG_SCHED_AUTOGROUP
1286 * Print out autogroup related information:
1288 static int sched_autogroup_show(struct seq_file *m, void *v)
1290 struct inode *inode = m->private;
1291 struct task_struct *p;
1293 p = get_proc_task(inode);
1296 proc_sched_autogroup_show_task(p, m);
1304 sched_autogroup_write(struct file *file, const char __user *buf,
1305 size_t count, loff_t *offset)
1307 struct inode *inode = file_inode(file);
1308 struct task_struct *p;
1309 char buffer[PROC_NUMBUF];
1313 memset(buffer, 0, sizeof(buffer));
1314 if (count > sizeof(buffer) - 1)
1315 count = sizeof(buffer) - 1;
1316 if (copy_from_user(buffer, buf, count))
1319 err = kstrtoint(strstrip(buffer), 0, &nice);
1323 p = get_proc_task(inode);
1327 err = proc_sched_autogroup_set_nice(p, nice);
1336 static int sched_autogroup_open(struct inode *inode, struct file *filp)
1340 ret = single_open(filp, sched_autogroup_show, NULL);
1342 struct seq_file *m = filp->private_data;
1349 static const struct file_operations proc_pid_sched_autogroup_operations = {
1350 .open = sched_autogroup_open,
1352 .write = sched_autogroup_write,
1353 .llseek = seq_lseek,
1354 .release = single_release,
1357 #endif /* CONFIG_SCHED_AUTOGROUP */
1359 static ssize_t comm_write(struct file *file, const char __user *buf,
1360 size_t count, loff_t *offset)
1362 struct inode *inode = file_inode(file);
1363 struct task_struct *p;
1364 char buffer[TASK_COMM_LEN];
1365 const size_t maxlen = sizeof(buffer) - 1;
1367 memset(buffer, 0, sizeof(buffer));
1368 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
1371 p = get_proc_task(inode);
1375 if (same_thread_group(current, p))
1376 set_task_comm(p, buffer);
1385 static int comm_show(struct seq_file *m, void *v)
1387 struct inode *inode = m->private;
1388 struct task_struct *p;
1390 p = get_proc_task(inode);
1395 seq_printf(m, "%s\n", p->comm);
1403 static int comm_open(struct inode *inode, struct file *filp)
1405 return single_open(filp, comm_show, inode);
1408 static const struct file_operations proc_pid_set_comm_operations = {
1411 .write = comm_write,
1412 .llseek = seq_lseek,
1413 .release = single_release,
1416 static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
1418 struct task_struct *task;
1419 struct mm_struct *mm;
1420 struct file *exe_file;
1422 task = get_proc_task(dentry->d_inode);
1425 mm = get_task_mm(task);
1426 put_task_struct(task);
1429 exe_file = get_mm_exe_file(mm);
1432 *exe_path = exe_file->f_path;
1433 path_get(&exe_file->f_path);
1440 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1442 struct inode *inode = dentry->d_inode;
1444 int error = -EACCES;
1446 /* Are we allowed to snoop on the tasks file descriptors? */
1447 if (!proc_fd_access_allowed(inode))
1450 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1454 nd_jump_link(nd, &path);
1457 return ERR_PTR(error);
1460 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1462 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
1469 pathname = d_path(path, tmp, PAGE_SIZE);
1470 len = PTR_ERR(pathname);
1471 if (IS_ERR(pathname))
1473 len = tmp + PAGE_SIZE - 1 - pathname;
1477 if (copy_to_user(buffer, pathname, len))
1480 free_page((unsigned long)tmp);
1484 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1486 int error = -EACCES;
1487 struct inode *inode = dentry->d_inode;
1490 /* Are we allowed to snoop on the tasks file descriptors? */
1491 if (!proc_fd_access_allowed(inode))
1494 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1498 error = do_proc_readlink(&path, buffer, buflen);
1504 const struct inode_operations proc_pid_link_inode_operations = {
1505 .readlink = proc_pid_readlink,
1506 .follow_link = proc_pid_follow_link,
1507 .setattr = proc_setattr,
1511 /* building an inode */
1513 struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1515 struct inode * inode;
1516 struct proc_inode *ei;
1517 const struct cred *cred;
1519 /* We need a new inode */
1521 inode = new_inode(sb);
1527 inode->i_ino = get_next_ino();
1528 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1529 inode->i_op = &proc_def_inode_operations;
1532 * grab the reference to task.
1534 ei->pid = get_task_pid(task, PIDTYPE_PID);
1538 if (task_dumpable(task)) {
1540 cred = __task_cred(task);
1541 inode->i_uid = cred->euid;
1542 inode->i_gid = cred->egid;
1545 security_task_to_inode(task, inode);
1555 int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1557 struct inode *inode = dentry->d_inode;
1558 struct task_struct *task;
1559 const struct cred *cred;
1560 struct pid_namespace *pid = dentry->d_sb->s_fs_info;
1562 generic_fillattr(inode, stat);
1565 stat->uid = GLOBAL_ROOT_UID;
1566 stat->gid = GLOBAL_ROOT_GID;
1567 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1569 if (!has_pid_permissions(pid, task, 2)) {
1572 * This doesn't prevent learning whether PID exists,
1573 * it only makes getattr() consistent with readdir().
1577 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1578 task_dumpable(task)) {
1579 cred = __task_cred(task);
1580 stat->uid = cred->euid;
1581 stat->gid = cred->egid;
1591 * Exceptional case: normally we are not allowed to unhash a busy
1592 * directory. In this case, however, we can do it - no aliasing problems
1593 * due to the way we treat inodes.
1595 * Rewrite the inode's ownerships here because the owning task may have
1596 * performed a setuid(), etc.
1598 * Before the /proc/pid/status file was created the only way to read
1599 * the effective uid of a /process was to stat /proc/pid. Reading
1600 * /proc/pid/status is slow enough that procps and other packages
1601 * kept stating /proc/pid. To keep the rules in /proc simple I have
1602 * made this apply to all per process world readable and executable
1605 int pid_revalidate(struct dentry *dentry, unsigned int flags)
1607 struct inode *inode;
1608 struct task_struct *task;
1609 const struct cred *cred;
1611 if (flags & LOOKUP_RCU)
1614 inode = dentry->d_inode;
1615 task = get_proc_task(inode);
1618 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1619 task_dumpable(task)) {
1621 cred = __task_cred(task);
1622 inode->i_uid = cred->euid;
1623 inode->i_gid = cred->egid;
1626 inode->i_uid = GLOBAL_ROOT_UID;
1627 inode->i_gid = GLOBAL_ROOT_GID;
1629 inode->i_mode &= ~(S_ISUID | S_ISGID);
1630 security_task_to_inode(task, inode);
1631 put_task_struct(task);
1638 static inline bool proc_inode_is_dead(struct inode *inode)
1640 return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1643 int pid_delete_dentry(const struct dentry *dentry)
1645 /* Is the task we represent dead?
1646 * If so, then don't put the dentry on the lru list,
1647 * kill it immediately.
1649 return proc_inode_is_dead(dentry->d_inode);
1652 const struct dentry_operations pid_dentry_operations =
1654 .d_revalidate = pid_revalidate,
1655 .d_delete = pid_delete_dentry,
1661 * Fill a directory entry.
1663 * If possible create the dcache entry and derive our inode number and
1664 * file type from dcache entry.
1666 * Since all of the proc inode numbers are dynamically generated, the inode
1667 * numbers do not exist until the inode is cache. This means creating the
1668 * the dcache entry in readdir is necessary to keep the inode numbers
1669 * reported by readdir in sync with the inode numbers reported
1672 bool proc_fill_cache(struct file *file, struct dir_context *ctx,
1673 const char *name, int len,
1674 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1676 struct dentry *child, *dir = file->f_path.dentry;
1677 struct qstr qname = QSTR_INIT(name, len);
1678 struct inode *inode;
1682 child = d_hash_and_lookup(dir, &qname);
1684 child = d_alloc(dir, &qname);
1686 goto end_instantiate;
1687 if (instantiate(dir->d_inode, child, task, ptr) < 0) {
1689 goto end_instantiate;
1692 inode = child->d_inode;
1694 type = inode->i_mode >> 12;
1696 return dir_emit(ctx, name, len, ino, type);
1699 return dir_emit(ctx, name, len, 1, DT_UNKNOWN);
1702 #ifdef CONFIG_CHECKPOINT_RESTORE
1705 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1706 * which represent vma start and end addresses.
1708 static int dname_to_vma_addr(struct dentry *dentry,
1709 unsigned long *start, unsigned long *end)
1711 if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2)
1717 static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
1719 unsigned long vm_start, vm_end;
1720 bool exact_vma_exists = false;
1721 struct mm_struct *mm = NULL;
1722 struct task_struct *task;
1723 const struct cred *cred;
1724 struct inode *inode;
1727 if (flags & LOOKUP_RCU)
1730 if (!capable(CAP_SYS_ADMIN)) {
1735 inode = dentry->d_inode;
1736 task = get_proc_task(inode);
1740 mm = mm_access(task, PTRACE_MODE_READ);
1741 if (IS_ERR_OR_NULL(mm))
1744 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1745 down_read(&mm->mmap_sem);
1746 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1747 up_read(&mm->mmap_sem);
1752 if (exact_vma_exists) {
1753 if (task_dumpable(task)) {
1755 cred = __task_cred(task);
1756 inode->i_uid = cred->euid;
1757 inode->i_gid = cred->egid;
1760 inode->i_uid = GLOBAL_ROOT_UID;
1761 inode->i_gid = GLOBAL_ROOT_GID;
1763 security_task_to_inode(task, inode);
1768 put_task_struct(task);
1777 static const struct dentry_operations tid_map_files_dentry_operations = {
1778 .d_revalidate = map_files_d_revalidate,
1779 .d_delete = pid_delete_dentry,
1782 static int proc_map_files_get_link(struct dentry *dentry, struct path *path)
1784 unsigned long vm_start, vm_end;
1785 struct vm_area_struct *vma;
1786 struct task_struct *task;
1787 struct mm_struct *mm;
1791 task = get_proc_task(dentry->d_inode);
1795 mm = get_task_mm(task);
1796 put_task_struct(task);
1800 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
1805 down_read(&mm->mmap_sem);
1806 vma = find_exact_vma(mm, vm_start, vm_end);
1807 if (vma && vma->vm_file) {
1808 *path = vma->vm_file->f_path;
1812 up_read(&mm->mmap_sem);
1820 struct map_files_info {
1823 unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
1827 proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
1828 struct task_struct *task, const void *ptr)
1830 fmode_t mode = (fmode_t)(unsigned long)ptr;
1831 struct proc_inode *ei;
1832 struct inode *inode;
1834 inode = proc_pid_make_inode(dir->i_sb, task);
1839 ei->op.proc_get_link = proc_map_files_get_link;
1841 inode->i_op = &proc_pid_link_inode_operations;
1843 inode->i_mode = S_IFLNK;
1845 if (mode & FMODE_READ)
1846 inode->i_mode |= S_IRUSR;
1847 if (mode & FMODE_WRITE)
1848 inode->i_mode |= S_IWUSR;
1850 d_set_d_op(dentry, &tid_map_files_dentry_operations);
1851 d_add(dentry, inode);
1856 static struct dentry *proc_map_files_lookup(struct inode *dir,
1857 struct dentry *dentry, unsigned int flags)
1859 unsigned long vm_start, vm_end;
1860 struct vm_area_struct *vma;
1861 struct task_struct *task;
1863 struct mm_struct *mm;
1866 if (!capable(CAP_SYS_ADMIN))
1870 task = get_proc_task(dir);
1875 if (!ptrace_may_access(task, PTRACE_MODE_READ))
1879 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
1882 mm = get_task_mm(task);
1886 down_read(&mm->mmap_sem);
1887 vma = find_exact_vma(mm, vm_start, vm_end);
1892 result = proc_map_files_instantiate(dir, dentry, task,
1893 (void *)(unsigned long)vma->vm_file->f_mode);
1896 up_read(&mm->mmap_sem);
1899 put_task_struct(task);
1901 return ERR_PTR(result);
1904 static const struct inode_operations proc_map_files_inode_operations = {
1905 .lookup = proc_map_files_lookup,
1906 .permission = proc_fd_permission,
1907 .setattr = proc_setattr,
1911 proc_map_files_readdir(struct file *file, struct dir_context *ctx)
1913 struct vm_area_struct *vma;
1914 struct task_struct *task;
1915 struct mm_struct *mm;
1916 unsigned long nr_files, pos, i;
1917 struct flex_array *fa = NULL;
1918 struct map_files_info info;
1919 struct map_files_info *p;
1923 if (!capable(CAP_SYS_ADMIN))
1927 task = get_proc_task(file_inode(file));
1932 if (!ptrace_may_access(task, PTRACE_MODE_READ))
1936 if (!dir_emit_dots(file, ctx))
1939 mm = get_task_mm(task);
1942 down_read(&mm->mmap_sem);
1947 * We need two passes here:
1949 * 1) Collect vmas of mapped files with mmap_sem taken
1950 * 2) Release mmap_sem and instantiate entries
1952 * otherwise we get lockdep complained, since filldir()
1953 * routine might require mmap_sem taken in might_fault().
1956 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
1957 if (vma->vm_file && ++pos > ctx->pos)
1962 fa = flex_array_alloc(sizeof(info), nr_files,
1964 if (!fa || flex_array_prealloc(fa, 0, nr_files,
1968 flex_array_free(fa);
1969 up_read(&mm->mmap_sem);
1973 for (i = 0, vma = mm->mmap, pos = 2; vma;
1974 vma = vma->vm_next) {
1977 if (++pos <= ctx->pos)
1980 info.mode = vma->vm_file->f_mode;
1981 info.len = snprintf(info.name,
1982 sizeof(info.name), "%lx-%lx",
1983 vma->vm_start, vma->vm_end);
1984 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
1988 up_read(&mm->mmap_sem);
1990 for (i = 0; i < nr_files; i++) {
1991 p = flex_array_get(fa, i);
1992 if (!proc_fill_cache(file, ctx,
1994 proc_map_files_instantiate,
1996 (void *)(unsigned long)p->mode))
2001 flex_array_free(fa);
2005 put_task_struct(task);
2010 static const struct file_operations proc_map_files_operations = {
2011 .read = generic_read_dir,
2012 .iterate = proc_map_files_readdir,
2013 .llseek = default_llseek,
2016 struct timers_private {
2018 struct task_struct *task;
2019 struct sighand_struct *sighand;
2020 struct pid_namespace *ns;
2021 unsigned long flags;
2024 static void *timers_start(struct seq_file *m, loff_t *pos)
2026 struct timers_private *tp = m->private;
2028 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2030 return ERR_PTR(-ESRCH);
2032 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2034 return ERR_PTR(-ESRCH);
2036 return seq_list_start(&tp->task->signal->posix_timers, *pos);
2039 static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2041 struct timers_private *tp = m->private;
2042 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2045 static void timers_stop(struct seq_file *m, void *v)
2047 struct timers_private *tp = m->private;
2050 unlock_task_sighand(tp->task, &tp->flags);
2055 put_task_struct(tp->task);
2060 static int show_timer(struct seq_file *m, void *v)
2062 struct k_itimer *timer;
2063 struct timers_private *tp = m->private;
2065 static const char * const nstr[] = {
2066 [SIGEV_SIGNAL] = "signal",
2067 [SIGEV_NONE] = "none",
2068 [SIGEV_THREAD] = "thread",
2071 timer = list_entry((struct list_head *)v, struct k_itimer, list);
2072 notify = timer->it_sigev_notify;
2074 seq_printf(m, "ID: %d\n", timer->it_id);
2075 seq_printf(m, "signal: %d/%p\n", timer->sigq->info.si_signo,
2076 timer->sigq->info.si_value.sival_ptr);
2077 seq_printf(m, "notify: %s/%s.%d\n",
2078 nstr[notify & ~SIGEV_THREAD_ID],
2079 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2080 pid_nr_ns(timer->it_pid, tp->ns));
2081 seq_printf(m, "ClockID: %d\n", timer->it_clock);
2086 static const struct seq_operations proc_timers_seq_ops = {
2087 .start = timers_start,
2088 .next = timers_next,
2089 .stop = timers_stop,
2093 static int proc_timers_open(struct inode *inode, struct file *file)
2095 struct timers_private *tp;
2097 tp = __seq_open_private(file, &proc_timers_seq_ops,
2098 sizeof(struct timers_private));
2102 tp->pid = proc_pid(inode);
2103 tp->ns = inode->i_sb->s_fs_info;
2107 static const struct file_operations proc_timers_operations = {
2108 .open = proc_timers_open,
2110 .llseek = seq_lseek,
2111 .release = seq_release_private,
2113 #endif /* CONFIG_CHECKPOINT_RESTORE */
2115 static int proc_pident_instantiate(struct inode *dir,
2116 struct dentry *dentry, struct task_struct *task, const void *ptr)
2118 const struct pid_entry *p = ptr;
2119 struct inode *inode;
2120 struct proc_inode *ei;
2122 inode = proc_pid_make_inode(dir->i_sb, task);
2127 inode->i_mode = p->mode;
2128 if (S_ISDIR(inode->i_mode))
2129 set_nlink(inode, 2); /* Use getattr to fix if necessary */
2131 inode->i_op = p->iop;
2133 inode->i_fop = p->fop;
2135 d_set_d_op(dentry, &pid_dentry_operations);
2136 d_add(dentry, inode);
2137 /* Close the race of the process dying before we return the dentry */
2138 if (pid_revalidate(dentry, 0))
2144 static struct dentry *proc_pident_lookup(struct inode *dir,
2145 struct dentry *dentry,
2146 const struct pid_entry *ents,
2150 struct task_struct *task = get_proc_task(dir);
2151 const struct pid_entry *p, *last;
2159 * Yes, it does not scale. And it should not. Don't add
2160 * new entries into /proc/<tgid>/ without very good reasons.
2162 last = &ents[nents - 1];
2163 for (p = ents; p <= last; p++) {
2164 if (p->len != dentry->d_name.len)
2166 if (!memcmp(dentry->d_name.name, p->name, p->len))
2172 error = proc_pident_instantiate(dir, dentry, task, p);
2174 put_task_struct(task);
2176 return ERR_PTR(error);
2179 static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
2180 const struct pid_entry *ents, unsigned int nents)
2182 struct task_struct *task = get_proc_task(file_inode(file));
2183 const struct pid_entry *p;
2188 if (!dir_emit_dots(file, ctx))
2191 if (ctx->pos >= nents + 2)
2194 for (p = ents + (ctx->pos - 2); p <= ents + nents - 1; p++) {
2195 if (!proc_fill_cache(file, ctx, p->name, p->len,
2196 proc_pident_instantiate, task, p))
2201 put_task_struct(task);
2205 #ifdef CONFIG_SECURITY
2206 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2207 size_t count, loff_t *ppos)
2209 struct inode * inode = file_inode(file);
2212 struct task_struct *task = get_proc_task(inode);
2217 length = security_getprocattr(task,
2218 (char*)file->f_path.dentry->d_name.name,
2220 put_task_struct(task);
2222 length = simple_read_from_buffer(buf, count, ppos, p, length);
2227 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2228 size_t count, loff_t *ppos)
2230 struct inode * inode = file_inode(file);
2233 struct task_struct *task = get_proc_task(inode);
2238 if (count > PAGE_SIZE)
2241 /* No partial writes. */
2247 page = (char*)__get_free_page(GFP_TEMPORARY);
2252 if (copy_from_user(page, buf, count))
2255 /* Guard against adverse ptrace interaction */
2256 length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
2260 length = security_setprocattr(task,
2261 (char*)file->f_path.dentry->d_name.name,
2262 (void*)page, count);
2263 mutex_unlock(&task->signal->cred_guard_mutex);
2265 free_page((unsigned long) page);
2267 put_task_struct(task);
2272 static const struct file_operations proc_pid_attr_operations = {
2273 .read = proc_pid_attr_read,
2274 .write = proc_pid_attr_write,
2275 .llseek = generic_file_llseek,
2278 static const struct pid_entry attr_dir_stuff[] = {
2279 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2280 REG("prev", S_IRUGO, proc_pid_attr_operations),
2281 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2282 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2283 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2284 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2287 static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
2289 return proc_pident_readdir(file, ctx,
2290 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2293 static const struct file_operations proc_attr_dir_operations = {
2294 .read = generic_read_dir,
2295 .iterate = proc_attr_dir_readdir,
2296 .llseek = default_llseek,
2299 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2300 struct dentry *dentry, unsigned int flags)
2302 return proc_pident_lookup(dir, dentry,
2303 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2306 static const struct inode_operations proc_attr_dir_inode_operations = {
2307 .lookup = proc_attr_dir_lookup,
2308 .getattr = pid_getattr,
2309 .setattr = proc_setattr,
2314 #ifdef CONFIG_ELF_CORE
2315 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2316 size_t count, loff_t *ppos)
2318 struct task_struct *task = get_proc_task(file_inode(file));
2319 struct mm_struct *mm;
2320 char buffer[PROC_NUMBUF];
2328 mm = get_task_mm(task);
2330 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2331 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2332 MMF_DUMP_FILTER_SHIFT));
2334 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2337 put_task_struct(task);
2342 static ssize_t proc_coredump_filter_write(struct file *file,
2343 const char __user *buf,
2347 struct task_struct *task;
2348 struct mm_struct *mm;
2349 char buffer[PROC_NUMBUF], *end;
2356 memset(buffer, 0, sizeof(buffer));
2357 if (count > sizeof(buffer) - 1)
2358 count = sizeof(buffer) - 1;
2359 if (copy_from_user(buffer, buf, count))
2363 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2366 if (end - buffer == 0)
2370 task = get_proc_task(file_inode(file));
2375 mm = get_task_mm(task);
2379 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2381 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2383 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2388 put_task_struct(task);
2393 static const struct file_operations proc_coredump_filter_operations = {
2394 .read = proc_coredump_filter_read,
2395 .write = proc_coredump_filter_write,
2396 .llseek = generic_file_llseek,
2400 #ifdef CONFIG_TASK_IO_ACCOUNTING
2401 static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
2403 struct task_io_accounting acct = task->ioac;
2404 unsigned long flags;
2407 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2411 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
2416 if (whole && lock_task_sighand(task, &flags)) {
2417 struct task_struct *t = task;
2419 task_io_accounting_add(&acct, &task->signal->ioac);
2420 while_each_thread(task, t)
2421 task_io_accounting_add(&acct, &t->ioac);
2423 unlock_task_sighand(task, &flags);
2425 result = sprintf(buffer,
2430 "read_bytes: %llu\n"
2431 "write_bytes: %llu\n"
2432 "cancelled_write_bytes: %llu\n",
2433 (unsigned long long)acct.rchar,
2434 (unsigned long long)acct.wchar,
2435 (unsigned long long)acct.syscr,
2436 (unsigned long long)acct.syscw,
2437 (unsigned long long)acct.read_bytes,
2438 (unsigned long long)acct.write_bytes,
2439 (unsigned long long)acct.cancelled_write_bytes);
2441 mutex_unlock(&task->signal->cred_guard_mutex);
2445 static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2447 return do_io_accounting(task, buffer, 0);
2450 static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2452 return do_io_accounting(task, buffer, 1);
2454 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2456 #ifdef CONFIG_USER_NS
2457 static int proc_id_map_open(struct inode *inode, struct file *file,
2458 const struct seq_operations *seq_ops)
2460 struct user_namespace *ns = NULL;
2461 struct task_struct *task;
2462 struct seq_file *seq;
2465 task = get_proc_task(inode);
2468 ns = get_user_ns(task_cred_xxx(task, user_ns));
2470 put_task_struct(task);
2475 ret = seq_open(file, seq_ops);
2479 seq = file->private_data;
2489 static int proc_id_map_release(struct inode *inode, struct file *file)
2491 struct seq_file *seq = file->private_data;
2492 struct user_namespace *ns = seq->private;
2494 return seq_release(inode, file);
2497 static int proc_uid_map_open(struct inode *inode, struct file *file)
2499 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2502 static int proc_gid_map_open(struct inode *inode, struct file *file)
2504 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2507 static int proc_projid_map_open(struct inode *inode, struct file *file)
2509 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2512 static const struct file_operations proc_uid_map_operations = {
2513 .open = proc_uid_map_open,
2514 .write = proc_uid_map_write,
2516 .llseek = seq_lseek,
2517 .release = proc_id_map_release,
2520 static const struct file_operations proc_gid_map_operations = {
2521 .open = proc_gid_map_open,
2522 .write = proc_gid_map_write,
2524 .llseek = seq_lseek,
2525 .release = proc_id_map_release,
2528 static const struct file_operations proc_projid_map_operations = {
2529 .open = proc_projid_map_open,
2530 .write = proc_projid_map_write,
2532 .llseek = seq_lseek,
2533 .release = proc_id_map_release,
2535 #endif /* CONFIG_USER_NS */
2537 static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2538 struct pid *pid, struct task_struct *task)
2540 int err = lock_trace(task);
2542 seq_printf(m, "%08x\n", task->personality);
2551 static const struct file_operations proc_task_operations;
2552 static const struct inode_operations proc_task_inode_operations;
2554 static const struct pid_entry tgid_base_stuff[] = {
2555 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2556 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2557 #ifdef CONFIG_CHECKPOINT_RESTORE
2558 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
2560 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2561 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
2563 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2565 REG("environ", S_IRUSR, proc_environ_operations),
2566 ONE("auxv", S_IRUSR, proc_pid_auxv),
2567 ONE("status", S_IRUGO, proc_pid_status),
2568 ONE("personality", S_IRUSR, proc_pid_personality),
2569 ONE("limits", S_IRUGO, proc_pid_limits),
2570 #ifdef CONFIG_SCHED_DEBUG
2571 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2573 #ifdef CONFIG_SCHED_AUTOGROUP
2574 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2576 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2577 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2578 ONE("syscall", S_IRUSR, proc_pid_syscall),
2580 ONE("cmdline", S_IRUGO, proc_pid_cmdline),
2581 ONE("stat", S_IRUGO, proc_tgid_stat),
2582 ONE("statm", S_IRUGO, proc_pid_statm),
2583 REG("maps", S_IRUGO, proc_pid_maps_operations),
2585 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
2587 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2588 LNK("cwd", proc_cwd_link),
2589 LNK("root", proc_root_link),
2590 LNK("exe", proc_exe_link),
2591 REG("mounts", S_IRUGO, proc_mounts_operations),
2592 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2593 REG("mountstats", S_IRUSR, proc_mountstats_operations),
2594 #ifdef CONFIG_PROC_PAGE_MONITOR
2595 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2596 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
2597 REG("pagemap", S_IRUSR, proc_pagemap_operations),
2599 #ifdef CONFIG_SECURITY
2600 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
2602 #ifdef CONFIG_KALLSYMS
2603 ONE("wchan", S_IRUGO, proc_pid_wchan),
2605 #ifdef CONFIG_STACKTRACE
2606 ONE("stack", S_IRUSR, proc_pid_stack),
2608 #ifdef CONFIG_SCHEDSTATS
2609 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
2611 #ifdef CONFIG_LATENCYTOP
2612 REG("latency", S_IRUGO, proc_lstats_operations),
2614 #ifdef CONFIG_PROC_PID_CPUSET
2615 REG("cpuset", S_IRUGO, proc_cpuset_operations),
2617 #ifdef CONFIG_CGROUPS
2618 REG("cgroup", S_IRUGO, proc_cgroup_operations),
2620 ONE("oom_score", S_IRUGO, proc_oom_score),
2621 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
2622 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
2623 #ifdef CONFIG_AUDITSYSCALL
2624 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2625 REG("sessionid", S_IRUGO, proc_sessionid_operations),
2627 #ifdef CONFIG_FAULT_INJECTION
2628 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2630 #ifdef CONFIG_ELF_CORE
2631 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
2633 #ifdef CONFIG_TASK_IO_ACCOUNTING
2634 INF("io", S_IRUSR, proc_tgid_io_accounting),
2636 #ifdef CONFIG_HARDWALL
2637 INF("hardwall", S_IRUGO, proc_pid_hardwall),
2639 #ifdef CONFIG_USER_NS
2640 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2641 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
2642 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
2644 #ifdef CONFIG_CHECKPOINT_RESTORE
2645 REG("timers", S_IRUGO, proc_timers_operations),
2649 static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
2651 return proc_pident_readdir(file, ctx,
2652 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2655 static const struct file_operations proc_tgid_base_operations = {
2656 .read = generic_read_dir,
2657 .iterate = proc_tgid_base_readdir,
2658 .llseek = default_llseek,
2661 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2663 return proc_pident_lookup(dir, dentry,
2664 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2667 static const struct inode_operations proc_tgid_base_inode_operations = {
2668 .lookup = proc_tgid_base_lookup,
2669 .getattr = pid_getattr,
2670 .setattr = proc_setattr,
2671 .permission = proc_pid_permission,
2674 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
2676 struct dentry *dentry, *leader, *dir;
2677 char buf[PROC_NUMBUF];
2681 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2682 /* no ->d_hash() rejects on procfs */
2683 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
2685 shrink_dcache_parent(dentry);
2691 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2692 leader = d_hash_and_lookup(mnt->mnt_root, &name);
2697 name.len = strlen(name.name);
2698 dir = d_hash_and_lookup(leader, &name);
2700 goto out_put_leader;
2703 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2704 dentry = d_hash_and_lookup(dir, &name);
2706 shrink_dcache_parent(dentry);
2719 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2720 * @task: task that should be flushed.
2722 * When flushing dentries from proc, one needs to flush them from global
2723 * proc (proc_mnt) and from all the namespaces' procs this task was seen
2724 * in. This call is supposed to do all of this job.
2726 * Looks in the dcache for
2728 * /proc/@tgid/task/@pid
2729 * if either directory is present flushes it and all of it'ts children
2732 * It is safe and reasonable to cache /proc entries for a task until
2733 * that task exits. After that they just clog up the dcache with
2734 * useless entries, possibly causing useful dcache entries to be
2735 * flushed instead. This routine is proved to flush those useless
2736 * dcache entries at process exit time.
2738 * NOTE: This routine is just an optimization so it does not guarantee
2739 * that no dcache entries will exist at process exit time it
2740 * just makes it very unlikely that any will persist.
2743 void proc_flush_task(struct task_struct *task)
2746 struct pid *pid, *tgid;
2749 pid = task_pid(task);
2750 tgid = task_tgid(task);
2752 for (i = 0; i <= pid->level; i++) {
2753 upid = &pid->numbers[i];
2754 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
2755 tgid->numbers[i].nr);
2759 static int proc_pid_instantiate(struct inode *dir,
2760 struct dentry * dentry,
2761 struct task_struct *task, const void *ptr)
2763 struct inode *inode;
2765 inode = proc_pid_make_inode(dir->i_sb, task);
2769 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2770 inode->i_op = &proc_tgid_base_inode_operations;
2771 inode->i_fop = &proc_tgid_base_operations;
2772 inode->i_flags|=S_IMMUTABLE;
2774 set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
2775 ARRAY_SIZE(tgid_base_stuff)));
2777 d_set_d_op(dentry, &pid_dentry_operations);
2779 d_add(dentry, inode);
2780 /* Close the race of the process dying before we return the dentry */
2781 if (pid_revalidate(dentry, 0))
2787 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
2789 int result = -ENOENT;
2790 struct task_struct *task;
2792 struct pid_namespace *ns;
2794 tgid = name_to_int(&dentry->d_name);
2798 ns = dentry->d_sb->s_fs_info;
2800 task = find_task_by_pid_ns(tgid, ns);
2802 get_task_struct(task);
2807 result = proc_pid_instantiate(dir, dentry, task, NULL);
2808 put_task_struct(task);
2810 return ERR_PTR(result);
2814 * Find the first task with tgid >= tgid
2819 struct task_struct *task;
2821 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2826 put_task_struct(iter.task);
2830 pid = find_ge_pid(iter.tgid, ns);
2832 iter.tgid = pid_nr_ns(pid, ns);
2833 iter.task = pid_task(pid, PIDTYPE_PID);
2834 /* What we to know is if the pid we have find is the
2835 * pid of a thread_group_leader. Testing for task
2836 * being a thread_group_leader is the obvious thing
2837 * todo but there is a window when it fails, due to
2838 * the pid transfer logic in de_thread.
2840 * So we perform the straight forward test of seeing
2841 * if the pid we have found is the pid of a thread
2842 * group leader, and don't worry if the task we have
2843 * found doesn't happen to be a thread group leader.
2844 * As we don't care in the case of readdir.
2846 if (!iter.task || !has_group_leader_pid(iter.task)) {
2850 get_task_struct(iter.task);
2856 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + 1)
2858 /* for the /proc/ directory itself, after non-process stuff has been done */
2859 int proc_pid_readdir(struct file *file, struct dir_context *ctx)
2861 struct tgid_iter iter;
2862 struct pid_namespace *ns = file->f_dentry->d_sb->s_fs_info;
2863 loff_t pos = ctx->pos;
2865 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
2868 if (pos == TGID_OFFSET - 1) {
2869 struct inode *inode = ns->proc_self->d_inode;
2870 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
2874 iter.tgid = pos - TGID_OFFSET;
2877 for (iter = next_tgid(ns, iter);
2879 iter.tgid += 1, iter = next_tgid(ns, iter)) {
2880 char name[PROC_NUMBUF];
2882 if (!has_pid_permissions(ns, iter.task, 2))
2885 len = snprintf(name, sizeof(name), "%d", iter.tgid);
2886 ctx->pos = iter.tgid + TGID_OFFSET;
2887 if (!proc_fill_cache(file, ctx, name, len,
2888 proc_pid_instantiate, iter.task, NULL)) {
2889 put_task_struct(iter.task);
2893 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
2900 static const struct pid_entry tid_base_stuff[] = {
2901 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2902 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2903 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
2904 REG("environ", S_IRUSR, proc_environ_operations),
2905 ONE("auxv", S_IRUSR, proc_pid_auxv),
2906 ONE("status", S_IRUGO, proc_pid_status),
2907 ONE("personality", S_IRUSR, proc_pid_personality),
2908 ONE("limits", S_IRUGO, proc_pid_limits),
2909 #ifdef CONFIG_SCHED_DEBUG
2910 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2912 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2913 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2914 ONE("syscall", S_IRUSR, proc_pid_syscall),
2916 ONE("cmdline", S_IRUGO, proc_pid_cmdline),
2917 ONE("stat", S_IRUGO, proc_tid_stat),
2918 ONE("statm", S_IRUGO, proc_pid_statm),
2919 REG("maps", S_IRUGO, proc_tid_maps_operations),
2920 #ifdef CONFIG_CHECKPOINT_RESTORE
2921 REG("children", S_IRUGO, proc_tid_children_operations),
2924 REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
2926 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2927 LNK("cwd", proc_cwd_link),
2928 LNK("root", proc_root_link),
2929 LNK("exe", proc_exe_link),
2930 REG("mounts", S_IRUGO, proc_mounts_operations),
2931 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2932 #ifdef CONFIG_PROC_PAGE_MONITOR
2933 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2934 REG("smaps", S_IRUGO, proc_tid_smaps_operations),
2935 REG("pagemap", S_IRUSR, proc_pagemap_operations),
2937 #ifdef CONFIG_SECURITY
2938 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
2940 #ifdef CONFIG_KALLSYMS
2941 ONE("wchan", S_IRUGO, proc_pid_wchan),
2943 #ifdef CONFIG_STACKTRACE
2944 ONE("stack", S_IRUSR, proc_pid_stack),
2946 #ifdef CONFIG_SCHEDSTATS
2947 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
2949 #ifdef CONFIG_LATENCYTOP
2950 REG("latency", S_IRUGO, proc_lstats_operations),
2952 #ifdef CONFIG_PROC_PID_CPUSET
2953 REG("cpuset", S_IRUGO, proc_cpuset_operations),
2955 #ifdef CONFIG_CGROUPS
2956 REG("cgroup", S_IRUGO, proc_cgroup_operations),
2958 ONE("oom_score", S_IRUGO, proc_oom_score),
2959 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
2960 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
2961 #ifdef CONFIG_AUDITSYSCALL
2962 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2963 REG("sessionid", S_IRUGO, proc_sessionid_operations),
2965 #ifdef CONFIG_FAULT_INJECTION
2966 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2968 #ifdef CONFIG_TASK_IO_ACCOUNTING
2969 INF("io", S_IRUSR, proc_tid_io_accounting),
2971 #ifdef CONFIG_HARDWALL
2972 INF("hardwall", S_IRUGO, proc_pid_hardwall),
2974 #ifdef CONFIG_USER_NS
2975 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2976 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
2977 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
2981 static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
2983 return proc_pident_readdir(file, ctx,
2984 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
2987 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2989 return proc_pident_lookup(dir, dentry,
2990 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
2993 static const struct file_operations proc_tid_base_operations = {
2994 .read = generic_read_dir,
2995 .iterate = proc_tid_base_readdir,
2996 .llseek = default_llseek,
2999 static const struct inode_operations proc_tid_base_inode_operations = {
3000 .lookup = proc_tid_base_lookup,
3001 .getattr = pid_getattr,
3002 .setattr = proc_setattr,
3005 static int proc_task_instantiate(struct inode *dir,
3006 struct dentry *dentry, struct task_struct *task, const void *ptr)
3008 struct inode *inode;
3009 inode = proc_pid_make_inode(dir->i_sb, task);
3013 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3014 inode->i_op = &proc_tid_base_inode_operations;
3015 inode->i_fop = &proc_tid_base_operations;
3016 inode->i_flags|=S_IMMUTABLE;
3018 set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3019 ARRAY_SIZE(tid_base_stuff)));
3021 d_set_d_op(dentry, &pid_dentry_operations);
3023 d_add(dentry, inode);
3024 /* Close the race of the process dying before we return the dentry */
3025 if (pid_revalidate(dentry, 0))
3031 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
3033 int result = -ENOENT;
3034 struct task_struct *task;
3035 struct task_struct *leader = get_proc_task(dir);
3037 struct pid_namespace *ns;
3042 tid = name_to_int(&dentry->d_name);
3046 ns = dentry->d_sb->s_fs_info;
3048 task = find_task_by_pid_ns(tid, ns);
3050 get_task_struct(task);
3054 if (!same_thread_group(leader, task))
3057 result = proc_task_instantiate(dir, dentry, task, NULL);
3059 put_task_struct(task);
3061 put_task_struct(leader);
3063 return ERR_PTR(result);
3067 * Find the first tid of a thread group to return to user space.
3069 * Usually this is just the thread group leader, but if the users
3070 * buffer was too small or there was a seek into the middle of the
3071 * directory we have more work todo.
3073 * In the case of a short read we start with find_task_by_pid.
3075 * In the case of a seek we start with the leader and walk nr
3078 static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
3079 struct pid_namespace *ns)
3081 struct task_struct *pos, *task;
3082 unsigned long nr = f_pos;
3084 if (nr != f_pos) /* 32bit overflow? */
3088 task = pid_task(pid, PIDTYPE_PID);
3092 /* Attempt to start with the tid of a thread */
3094 pos = find_task_by_pid_ns(tid, ns);
3095 if (pos && same_thread_group(pos, task))
3099 /* If nr exceeds the number of threads there is nothing todo */
3100 if (nr >= get_nr_threads(task))
3103 /* If we haven't found our starting place yet start
3104 * with the leader and walk nr threads forward.
3106 pos = task = task->group_leader;
3110 } while_each_thread(task, pos);
3115 get_task_struct(pos);
3122 * Find the next thread in the thread list.
3123 * Return NULL if there is an error or no next thread.
3125 * The reference to the input task_struct is released.
3127 static struct task_struct *next_tid(struct task_struct *start)
3129 struct task_struct *pos = NULL;
3131 if (pid_alive(start)) {
3132 pos = next_thread(start);
3133 if (thread_group_leader(pos))
3136 get_task_struct(pos);
3139 put_task_struct(start);
3143 /* for the /proc/TGID/task/ directories */
3144 static int proc_task_readdir(struct file *file, struct dir_context *ctx)
3146 struct inode *inode = file_inode(file);
3147 struct task_struct *task;
3148 struct pid_namespace *ns;
3151 if (proc_inode_is_dead(inode))
3154 if (!dir_emit_dots(file, ctx))
3157 /* f_version caches the tgid value that the last readdir call couldn't
3158 * return. lseek aka telldir automagically resets f_version to 0.
3160 ns = file->f_dentry->d_sb->s_fs_info;
3161 tid = (int)file->f_version;
3162 file->f_version = 0;
3163 for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
3165 task = next_tid(task), ctx->pos++) {
3166 char name[PROC_NUMBUF];
3168 tid = task_pid_nr_ns(task, ns);
3169 len = snprintf(name, sizeof(name), "%d", tid);
3170 if (!proc_fill_cache(file, ctx, name, len,
3171 proc_task_instantiate, task, NULL)) {
3172 /* returning this tgid failed, save it as the first
3173 * pid for the next readir call */
3174 file->f_version = (u64)tid;
3175 put_task_struct(task);
3183 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3185 struct inode *inode = dentry->d_inode;
3186 struct task_struct *p = get_proc_task(inode);
3187 generic_fillattr(inode, stat);
3190 stat->nlink += get_nr_threads(p);
3197 static const struct inode_operations proc_task_inode_operations = {
3198 .lookup = proc_task_lookup,
3199 .getattr = proc_task_getattr,
3200 .setattr = proc_setattr,
3201 .permission = proc_pid_permission,
3204 static const struct file_operations proc_task_operations = {
3205 .read = generic_read_dir,
3206 .iterate = proc_task_readdir,
3207 .llseek = default_llseek,