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/cgroup.h>
77 #include <linux/cpuset.h>
78 #include <linux/audit.h>
79 #include <linux/poll.h>
80 #include <linux/nsproxy.h>
81 #include <linux/oom.h>
82 #include <linux/elf.h>
83 #include <linux/pid_namespace.h>
84 #include <linux/fs_struct.h>
85 #include <linux/slab.h>
86 #include <linux/flex_array.h>
87 #ifdef CONFIG_HARDWALL
88 #include <asm/hardwall.h>
90 #include <trace/events/oom.h>
94 * Implementing inode permission operations in /proc is almost
95 * certainly an error. Permission checks need to happen during
96 * each system call not at open time. The reason is that most of
97 * what we wish to check for permissions in /proc varies at runtime.
99 * The classic example of a problem is opening file descriptors
100 * in /proc for a task before it execs a suid executable.
107 const struct inode_operations *iop;
108 const struct file_operations *fop;
112 #define NOD(NAME, MODE, IOP, FOP, OP) { \
114 .len = sizeof(NAME) - 1, \
121 #define DIR(NAME, MODE, iops, fops) \
122 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
123 #define LNK(NAME, get_link) \
124 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
125 &proc_pid_link_inode_operations, NULL, \
126 { .proc_get_link = get_link } )
127 #define REG(NAME, MODE, fops) \
128 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
129 #define INF(NAME, MODE, read) \
130 NOD(NAME, (S_IFREG|(MODE)), \
131 NULL, &proc_info_file_operations, \
132 { .proc_read = read } )
133 #define ONE(NAME, MODE, show) \
134 NOD(NAME, (S_IFREG|(MODE)), \
135 NULL, &proc_single_file_operations, \
136 { .proc_show = show } )
138 static int proc_fd_permission(struct inode *inode, int mask);
141 * Count the number of hardlinks for the pid_entry table, excluding the .
144 static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
151 for (i = 0; i < n; ++i) {
152 if (S_ISDIR(entries[i].mode))
159 static int get_task_root(struct task_struct *task, struct path *root)
161 int result = -ENOENT;
165 get_fs_root(task->fs, root);
172 static int proc_cwd_link(struct dentry *dentry, struct path *path)
174 struct task_struct *task = get_proc_task(dentry->d_inode);
175 int result = -ENOENT;
180 get_fs_pwd(task->fs, path);
184 put_task_struct(task);
189 static int proc_root_link(struct dentry *dentry, struct path *path)
191 struct task_struct *task = get_proc_task(dentry->d_inode);
192 int result = -ENOENT;
195 result = get_task_root(task, path);
196 put_task_struct(task);
201 static struct mm_struct *mm_access(struct task_struct *task, unsigned int mode)
203 struct mm_struct *mm;
206 err = mutex_lock_killable(&task->signal->cred_guard_mutex);
210 mm = get_task_mm(task);
211 if (mm && mm != current->mm &&
212 !ptrace_may_access(task, mode)) {
214 mm = ERR_PTR(-EACCES);
216 mutex_unlock(&task->signal->cred_guard_mutex);
221 struct mm_struct *mm_for_maps(struct task_struct *task)
223 return mm_access(task, PTRACE_MODE_READ);
226 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
230 struct mm_struct *mm = get_task_mm(task);
234 goto out_mm; /* Shh! No looking before we're done */
236 len = mm->arg_end - mm->arg_start;
241 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
243 // If the nul at the end of args has been overwritten, then
244 // assume application is using setproctitle(3).
245 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
246 len = strnlen(buffer, res);
250 len = mm->env_end - mm->env_start;
251 if (len > PAGE_SIZE - res)
252 len = PAGE_SIZE - res;
253 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
254 res = strnlen(buffer, res);
263 static int proc_pid_auxv(struct task_struct *task, char *buffer)
265 struct mm_struct *mm = mm_for_maps(task);
266 int res = PTR_ERR(mm);
267 if (mm && !IS_ERR(mm)) {
268 unsigned int nwords = 0;
271 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
272 res = nwords * sizeof(mm->saved_auxv[0]);
275 memcpy(buffer, mm->saved_auxv, res);
282 #ifdef CONFIG_KALLSYMS
284 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
285 * Returns the resolved symbol. If that fails, simply return the address.
287 static int proc_pid_wchan(struct task_struct *task, char *buffer)
290 char symname[KSYM_NAME_LEN];
292 wchan = get_wchan(task);
294 if (lookup_symbol_name(wchan, symname) < 0)
295 if (!ptrace_may_access(task, PTRACE_MODE_READ))
298 return sprintf(buffer, "%lu", wchan);
300 return sprintf(buffer, "%s", symname);
302 #endif /* CONFIG_KALLSYMS */
304 static int lock_trace(struct task_struct *task)
306 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
309 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH)) {
310 mutex_unlock(&task->signal->cred_guard_mutex);
316 static void unlock_trace(struct task_struct *task)
318 mutex_unlock(&task->signal->cred_guard_mutex);
321 #ifdef CONFIG_STACKTRACE
323 #define MAX_STACK_TRACE_DEPTH 64
325 static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
326 struct pid *pid, struct task_struct *task)
328 struct stack_trace trace;
329 unsigned long *entries;
333 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
337 trace.nr_entries = 0;
338 trace.max_entries = MAX_STACK_TRACE_DEPTH;
339 trace.entries = entries;
342 err = lock_trace(task);
344 save_stack_trace_tsk(task, &trace);
346 for (i = 0; i < trace.nr_entries; i++) {
347 seq_printf(m, "[<%pK>] %pS\n",
348 (void *)entries[i], (void *)entries[i]);
358 #ifdef CONFIG_SCHEDSTATS
360 * Provides /proc/PID/schedstat
362 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
364 return sprintf(buffer, "%llu %llu %lu\n",
365 (unsigned long long)task->se.sum_exec_runtime,
366 (unsigned long long)task->sched_info.run_delay,
367 task->sched_info.pcount);
371 #ifdef CONFIG_LATENCYTOP
372 static int lstats_show_proc(struct seq_file *m, void *v)
375 struct inode *inode = m->private;
376 struct task_struct *task = get_proc_task(inode);
380 seq_puts(m, "Latency Top version : v0.1\n");
381 for (i = 0; i < 32; i++) {
382 struct latency_record *lr = &task->latency_record[i];
383 if (lr->backtrace[0]) {
385 seq_printf(m, "%i %li %li",
386 lr->count, lr->time, lr->max);
387 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
388 unsigned long bt = lr->backtrace[q];
393 seq_printf(m, " %ps", (void *)bt);
399 put_task_struct(task);
403 static int lstats_open(struct inode *inode, struct file *file)
405 return single_open(file, lstats_show_proc, inode);
408 static ssize_t lstats_write(struct file *file, const char __user *buf,
409 size_t count, loff_t *offs)
411 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
415 clear_all_latency_tracing(task);
416 put_task_struct(task);
421 static const struct file_operations proc_lstats_operations = {
424 .write = lstats_write,
426 .release = single_release,
431 static int proc_oom_score(struct task_struct *task, char *buffer)
433 unsigned long points = 0;
435 read_lock(&tasklist_lock);
437 points = oom_badness(task, NULL, NULL,
438 totalram_pages + total_swap_pages);
439 read_unlock(&tasklist_lock);
440 return sprintf(buffer, "%lu\n", points);
448 static const struct limit_names lnames[RLIM_NLIMITS] = {
449 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
450 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
451 [RLIMIT_DATA] = {"Max data size", "bytes"},
452 [RLIMIT_STACK] = {"Max stack size", "bytes"},
453 [RLIMIT_CORE] = {"Max core file size", "bytes"},
454 [RLIMIT_RSS] = {"Max resident set", "bytes"},
455 [RLIMIT_NPROC] = {"Max processes", "processes"},
456 [RLIMIT_NOFILE] = {"Max open files", "files"},
457 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
458 [RLIMIT_AS] = {"Max address space", "bytes"},
459 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
460 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
461 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
462 [RLIMIT_NICE] = {"Max nice priority", NULL},
463 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
464 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
467 /* Display limits for a process */
468 static int proc_pid_limits(struct task_struct *task, char *buffer)
473 char *bufptr = buffer;
475 struct rlimit rlim[RLIM_NLIMITS];
477 if (!lock_task_sighand(task, &flags))
479 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
480 unlock_task_sighand(task, &flags);
483 * print the file header
485 count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
486 "Limit", "Soft Limit", "Hard Limit", "Units");
488 for (i = 0; i < RLIM_NLIMITS; i++) {
489 if (rlim[i].rlim_cur == RLIM_INFINITY)
490 count += sprintf(&bufptr[count], "%-25s %-20s ",
491 lnames[i].name, "unlimited");
493 count += sprintf(&bufptr[count], "%-25s %-20lu ",
494 lnames[i].name, rlim[i].rlim_cur);
496 if (rlim[i].rlim_max == RLIM_INFINITY)
497 count += sprintf(&bufptr[count], "%-20s ", "unlimited");
499 count += sprintf(&bufptr[count], "%-20lu ",
503 count += sprintf(&bufptr[count], "%-10s\n",
506 count += sprintf(&bufptr[count], "\n");
512 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
513 static int proc_pid_syscall(struct task_struct *task, char *buffer)
516 unsigned long args[6], sp, pc;
517 int res = lock_trace(task);
521 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
522 res = sprintf(buffer, "running\n");
524 res = sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
526 res = sprintf(buffer,
527 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
529 args[0], args[1], args[2], args[3], args[4], args[5],
534 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
536 /************************************************************************/
537 /* Here the fs part begins */
538 /************************************************************************/
540 /* permission checks */
541 static int proc_fd_access_allowed(struct inode *inode)
543 struct task_struct *task;
545 /* Allow access to a task's file descriptors if it is us or we
546 * may use ptrace attach to the process and find out that
549 task = get_proc_task(inode);
551 allowed = ptrace_may_access(task, PTRACE_MODE_READ);
552 put_task_struct(task);
557 int proc_setattr(struct dentry *dentry, struct iattr *attr)
560 struct inode *inode = dentry->d_inode;
562 if (attr->ia_valid & ATTR_MODE)
565 error = inode_change_ok(inode, attr);
569 if ((attr->ia_valid & ATTR_SIZE) &&
570 attr->ia_size != i_size_read(inode)) {
571 error = vmtruncate(inode, attr->ia_size);
576 setattr_copy(inode, attr);
577 mark_inode_dirty(inode);
582 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
583 * or euid/egid (for hide_pid_min=2)?
585 static bool has_pid_permissions(struct pid_namespace *pid,
586 struct task_struct *task,
589 if (pid->hide_pid < hide_pid_min)
591 if (in_group_p(pid->pid_gid))
593 return ptrace_may_access(task, PTRACE_MODE_READ);
597 static int proc_pid_permission(struct inode *inode, int mask)
599 struct pid_namespace *pid = inode->i_sb->s_fs_info;
600 struct task_struct *task;
603 task = get_proc_task(inode);
606 has_perms = has_pid_permissions(pid, task, 1);
607 put_task_struct(task);
610 if (pid->hide_pid == 2) {
612 * Let's make getdents(), stat(), and open()
613 * consistent with each other. If a process
614 * may not stat() a file, it shouldn't be seen
622 return generic_permission(inode, mask);
627 static const struct inode_operations proc_def_inode_operations = {
628 .setattr = proc_setattr,
631 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
633 static ssize_t proc_info_read(struct file * file, char __user * buf,
634 size_t count, loff_t *ppos)
636 struct inode * inode = file->f_path.dentry->d_inode;
639 struct task_struct *task = get_proc_task(inode);
645 if (count > PROC_BLOCK_SIZE)
646 count = PROC_BLOCK_SIZE;
649 if (!(page = __get_free_page(GFP_TEMPORARY)))
652 length = PROC_I(inode)->op.proc_read(task, (char*)page);
655 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
658 put_task_struct(task);
663 static const struct file_operations proc_info_file_operations = {
664 .read = proc_info_read,
665 .llseek = generic_file_llseek,
668 static int proc_single_show(struct seq_file *m, void *v)
670 struct inode *inode = m->private;
671 struct pid_namespace *ns;
673 struct task_struct *task;
676 ns = inode->i_sb->s_fs_info;
677 pid = proc_pid(inode);
678 task = get_pid_task(pid, PIDTYPE_PID);
682 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
684 put_task_struct(task);
688 static int proc_single_open(struct inode *inode, struct file *filp)
690 return single_open(filp, proc_single_show, inode);
693 static const struct file_operations proc_single_file_operations = {
694 .open = proc_single_open,
697 .release = single_release,
700 static int mem_open(struct inode* inode, struct file* file)
702 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
703 struct mm_struct *mm;
708 mm = mm_access(task, PTRACE_MODE_ATTACH);
709 put_task_struct(task);
715 /* ensure this mm_struct can't be freed */
716 atomic_inc(&mm->mm_count);
717 /* but do not pin its memory */
721 /* OK to pass negative loff_t, we can catch out-of-range */
722 file->f_mode |= FMODE_UNSIGNED_OFFSET;
723 file->private_data = mm;
728 static ssize_t mem_rw(struct file *file, char __user *buf,
729 size_t count, loff_t *ppos, int write)
731 struct mm_struct *mm = file->private_data;
732 unsigned long addr = *ppos;
739 page = (char *)__get_free_page(GFP_TEMPORARY);
744 if (!atomic_inc_not_zero(&mm->mm_users))
748 int this_len = min_t(int, count, PAGE_SIZE);
750 if (write && copy_from_user(page, buf, this_len)) {
755 this_len = access_remote_vm(mm, addr, page, this_len, write);
762 if (!write && copy_to_user(buf, page, this_len)) {
776 free_page((unsigned long) page);
780 static ssize_t mem_read(struct file *file, char __user *buf,
781 size_t count, loff_t *ppos)
783 return mem_rw(file, buf, count, ppos, 0);
786 static ssize_t mem_write(struct file *file, const char __user *buf,
787 size_t count, loff_t *ppos)
789 return mem_rw(file, (char __user*)buf, count, ppos, 1);
792 loff_t mem_lseek(struct file *file, loff_t offset, int orig)
796 file->f_pos = offset;
799 file->f_pos += offset;
804 force_successful_syscall_return();
808 static int mem_release(struct inode *inode, struct file *file)
810 struct mm_struct *mm = file->private_data;
816 static const struct file_operations proc_mem_operations = {
821 .release = mem_release,
824 static ssize_t environ_read(struct file *file, char __user *buf,
825 size_t count, loff_t *ppos)
827 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
829 unsigned long src = *ppos;
831 struct mm_struct *mm;
837 page = (char *)__get_free_page(GFP_TEMPORARY);
842 mm = mm_for_maps(task);
844 if (!mm || IS_ERR(mm))
849 int this_len, retval, max_len;
851 this_len = mm->env_end - (mm->env_start + src);
856 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
857 this_len = (this_len > max_len) ? max_len : this_len;
859 retval = access_process_vm(task, (mm->env_start + src),
867 if (copy_to_user(buf, page, retval)) {
881 free_page((unsigned long) page);
883 put_task_struct(task);
888 static const struct file_operations proc_environ_operations = {
889 .read = environ_read,
890 .llseek = generic_file_llseek,
893 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
894 size_t count, loff_t *ppos)
896 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
897 char buffer[PROC_NUMBUF];
899 int oom_adjust = OOM_DISABLE;
905 if (lock_task_sighand(task, &flags)) {
906 oom_adjust = task->signal->oom_adj;
907 unlock_task_sighand(task, &flags);
910 put_task_struct(task);
912 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
914 return simple_read_from_buffer(buf, count, ppos, buffer, len);
917 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
918 size_t count, loff_t *ppos)
920 struct task_struct *task;
921 char buffer[PROC_NUMBUF];
926 memset(buffer, 0, sizeof(buffer));
927 if (count > sizeof(buffer) - 1)
928 count = sizeof(buffer) - 1;
929 if (copy_from_user(buffer, buf, count)) {
934 err = kstrtoint(strstrip(buffer), 0, &oom_adjust);
937 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
938 oom_adjust != OOM_DISABLE) {
943 task = get_proc_task(file->f_path.dentry->d_inode);
955 if (!lock_task_sighand(task, &flags)) {
960 if (oom_adjust < task->signal->oom_adj && !capable(CAP_SYS_RESOURCE)) {
966 * Warn that /proc/pid/oom_adj is deprecated, see
967 * Documentation/feature-removal-schedule.txt.
969 printk_once(KERN_WARNING "%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
970 current->comm, task_pid_nr(current), task_pid_nr(task),
972 task->signal->oom_adj = oom_adjust;
974 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
975 * value is always attainable.
977 if (task->signal->oom_adj == OOM_ADJUST_MAX)
978 task->signal->oom_score_adj = OOM_SCORE_ADJ_MAX;
980 task->signal->oom_score_adj = (oom_adjust * OOM_SCORE_ADJ_MAX) /
982 trace_oom_score_adj_update(task);
984 unlock_task_sighand(task, &flags);
987 put_task_struct(task);
989 return err < 0 ? err : count;
992 static const struct file_operations proc_oom_adjust_operations = {
993 .read = oom_adjust_read,
994 .write = oom_adjust_write,
995 .llseek = generic_file_llseek,
998 static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
999 size_t count, loff_t *ppos)
1001 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
1002 char buffer[PROC_NUMBUF];
1003 int oom_score_adj = OOM_SCORE_ADJ_MIN;
1004 unsigned long flags;
1009 if (lock_task_sighand(task, &flags)) {
1010 oom_score_adj = task->signal->oom_score_adj;
1011 unlock_task_sighand(task, &flags);
1013 put_task_struct(task);
1014 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_score_adj);
1015 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1018 static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1019 size_t count, loff_t *ppos)
1021 struct task_struct *task;
1022 char buffer[PROC_NUMBUF];
1023 unsigned long flags;
1027 memset(buffer, 0, sizeof(buffer));
1028 if (count > sizeof(buffer) - 1)
1029 count = sizeof(buffer) - 1;
1030 if (copy_from_user(buffer, buf, count)) {
1035 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
1038 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
1039 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1044 task = get_proc_task(file->f_path.dentry->d_inode);
1056 if (!lock_task_sighand(task, &flags)) {
1061 if (oom_score_adj < task->signal->oom_score_adj_min &&
1062 !capable(CAP_SYS_RESOURCE)) {
1067 task->signal->oom_score_adj = oom_score_adj;
1068 if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
1069 task->signal->oom_score_adj_min = oom_score_adj;
1070 trace_oom_score_adj_update(task);
1072 * Scale /proc/pid/oom_adj appropriately ensuring that OOM_DISABLE is
1073 * always attainable.
1075 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MIN)
1076 task->signal->oom_adj = OOM_DISABLE;
1078 task->signal->oom_adj = (oom_score_adj * OOM_ADJUST_MAX) /
1081 unlock_task_sighand(task, &flags);
1084 put_task_struct(task);
1086 return err < 0 ? err : count;
1089 static const struct file_operations proc_oom_score_adj_operations = {
1090 .read = oom_score_adj_read,
1091 .write = oom_score_adj_write,
1092 .llseek = default_llseek,
1095 #ifdef CONFIG_AUDITSYSCALL
1096 #define TMPBUFLEN 21
1097 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1098 size_t count, loff_t *ppos)
1100 struct inode * inode = file->f_path.dentry->d_inode;
1101 struct task_struct *task = get_proc_task(inode);
1103 char tmpbuf[TMPBUFLEN];
1107 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1108 audit_get_loginuid(task));
1109 put_task_struct(task);
1110 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1113 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1114 size_t count, loff_t *ppos)
1116 struct inode * inode = file->f_path.dentry->d_inode;
1122 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1128 if (count >= PAGE_SIZE)
1129 count = PAGE_SIZE - 1;
1132 /* No partial writes. */
1135 page = (char*)__get_free_page(GFP_TEMPORARY);
1139 if (copy_from_user(page, buf, count))
1143 loginuid = simple_strtoul(page, &tmp, 10);
1149 length = audit_set_loginuid(loginuid);
1150 if (likely(length == 0))
1154 free_page((unsigned long) page);
1158 static const struct file_operations proc_loginuid_operations = {
1159 .read = proc_loginuid_read,
1160 .write = proc_loginuid_write,
1161 .llseek = generic_file_llseek,
1164 static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1165 size_t count, loff_t *ppos)
1167 struct inode * inode = file->f_path.dentry->d_inode;
1168 struct task_struct *task = get_proc_task(inode);
1170 char tmpbuf[TMPBUFLEN];
1174 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1175 audit_get_sessionid(task));
1176 put_task_struct(task);
1177 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1180 static const struct file_operations proc_sessionid_operations = {
1181 .read = proc_sessionid_read,
1182 .llseek = generic_file_llseek,
1186 #ifdef CONFIG_FAULT_INJECTION
1187 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1188 size_t count, loff_t *ppos)
1190 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1191 char buffer[PROC_NUMBUF];
1197 make_it_fail = task->make_it_fail;
1198 put_task_struct(task);
1200 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1202 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1205 static ssize_t proc_fault_inject_write(struct file * file,
1206 const char __user * buf, size_t count, loff_t *ppos)
1208 struct task_struct *task;
1209 char buffer[PROC_NUMBUF], *end;
1212 if (!capable(CAP_SYS_RESOURCE))
1214 memset(buffer, 0, sizeof(buffer));
1215 if (count > sizeof(buffer) - 1)
1216 count = sizeof(buffer) - 1;
1217 if (copy_from_user(buffer, buf, count))
1219 make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1222 task = get_proc_task(file->f_dentry->d_inode);
1225 task->make_it_fail = make_it_fail;
1226 put_task_struct(task);
1231 static const struct file_operations proc_fault_inject_operations = {
1232 .read = proc_fault_inject_read,
1233 .write = proc_fault_inject_write,
1234 .llseek = generic_file_llseek,
1239 #ifdef CONFIG_SCHED_DEBUG
1241 * Print out various scheduling related per-task fields:
1243 static int sched_show(struct seq_file *m, void *v)
1245 struct inode *inode = m->private;
1246 struct task_struct *p;
1248 p = get_proc_task(inode);
1251 proc_sched_show_task(p, m);
1259 sched_write(struct file *file, const char __user *buf,
1260 size_t count, loff_t *offset)
1262 struct inode *inode = file->f_path.dentry->d_inode;
1263 struct task_struct *p;
1265 p = get_proc_task(inode);
1268 proc_sched_set_task(p);
1275 static int sched_open(struct inode *inode, struct file *filp)
1277 return single_open(filp, sched_show, inode);
1280 static const struct file_operations proc_pid_sched_operations = {
1283 .write = sched_write,
1284 .llseek = seq_lseek,
1285 .release = single_release,
1290 #ifdef CONFIG_SCHED_AUTOGROUP
1292 * Print out autogroup related information:
1294 static int sched_autogroup_show(struct seq_file *m, void *v)
1296 struct inode *inode = m->private;
1297 struct task_struct *p;
1299 p = get_proc_task(inode);
1302 proc_sched_autogroup_show_task(p, m);
1310 sched_autogroup_write(struct file *file, const char __user *buf,
1311 size_t count, loff_t *offset)
1313 struct inode *inode = file->f_path.dentry->d_inode;
1314 struct task_struct *p;
1315 char buffer[PROC_NUMBUF];
1319 memset(buffer, 0, sizeof(buffer));
1320 if (count > sizeof(buffer) - 1)
1321 count = sizeof(buffer) - 1;
1322 if (copy_from_user(buffer, buf, count))
1325 err = kstrtoint(strstrip(buffer), 0, &nice);
1329 p = get_proc_task(inode);
1334 err = proc_sched_autogroup_set_nice(p, &err);
1343 static int sched_autogroup_open(struct inode *inode, struct file *filp)
1347 ret = single_open(filp, sched_autogroup_show, NULL);
1349 struct seq_file *m = filp->private_data;
1356 static const struct file_operations proc_pid_sched_autogroup_operations = {
1357 .open = sched_autogroup_open,
1359 .write = sched_autogroup_write,
1360 .llseek = seq_lseek,
1361 .release = single_release,
1364 #endif /* CONFIG_SCHED_AUTOGROUP */
1366 static ssize_t comm_write(struct file *file, const char __user *buf,
1367 size_t count, loff_t *offset)
1369 struct inode *inode = file->f_path.dentry->d_inode;
1370 struct task_struct *p;
1371 char buffer[TASK_COMM_LEN];
1373 memset(buffer, 0, sizeof(buffer));
1374 if (count > sizeof(buffer) - 1)
1375 count = sizeof(buffer) - 1;
1376 if (copy_from_user(buffer, buf, count))
1379 p = get_proc_task(inode);
1383 if (same_thread_group(current, p))
1384 set_task_comm(p, buffer);
1393 static int comm_show(struct seq_file *m, void *v)
1395 struct inode *inode = m->private;
1396 struct task_struct *p;
1398 p = get_proc_task(inode);
1403 seq_printf(m, "%s\n", p->comm);
1411 static int comm_open(struct inode *inode, struct file *filp)
1413 return single_open(filp, comm_show, inode);
1416 static const struct file_operations proc_pid_set_comm_operations = {
1419 .write = comm_write,
1420 .llseek = seq_lseek,
1421 .release = single_release,
1424 static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
1426 struct task_struct *task;
1427 struct mm_struct *mm;
1428 struct file *exe_file;
1430 task = get_proc_task(dentry->d_inode);
1433 mm = get_task_mm(task);
1434 put_task_struct(task);
1437 exe_file = get_mm_exe_file(mm);
1440 *exe_path = exe_file->f_path;
1441 path_get(&exe_file->f_path);
1448 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1450 struct inode *inode = dentry->d_inode;
1451 int error = -EACCES;
1453 /* We don't need a base pointer in the /proc filesystem */
1454 path_put(&nd->path);
1456 /* Are we allowed to snoop on the tasks file descriptors? */
1457 if (!proc_fd_access_allowed(inode))
1460 error = PROC_I(inode)->op.proc_get_link(dentry, &nd->path);
1462 return ERR_PTR(error);
1465 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1467 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
1474 pathname = d_path(path, tmp, PAGE_SIZE);
1475 len = PTR_ERR(pathname);
1476 if (IS_ERR(pathname))
1478 len = tmp + PAGE_SIZE - 1 - pathname;
1482 if (copy_to_user(buffer, pathname, len))
1485 free_page((unsigned long)tmp);
1489 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1491 int error = -EACCES;
1492 struct inode *inode = dentry->d_inode;
1495 /* Are we allowed to snoop on the tasks file descriptors? */
1496 if (!proc_fd_access_allowed(inode))
1499 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1503 error = do_proc_readlink(&path, buffer, buflen);
1509 static const struct inode_operations proc_pid_link_inode_operations = {
1510 .readlink = proc_pid_readlink,
1511 .follow_link = proc_pid_follow_link,
1512 .setattr = proc_setattr,
1516 /* building an inode */
1518 static int task_dumpable(struct task_struct *task)
1521 struct mm_struct *mm;
1526 dumpable = get_dumpable(mm);
1533 struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1535 struct inode * inode;
1536 struct proc_inode *ei;
1537 const struct cred *cred;
1539 /* We need a new inode */
1541 inode = new_inode(sb);
1547 inode->i_ino = get_next_ino();
1548 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1549 inode->i_op = &proc_def_inode_operations;
1552 * grab the reference to task.
1554 ei->pid = get_task_pid(task, PIDTYPE_PID);
1558 if (task_dumpable(task)) {
1560 cred = __task_cred(task);
1561 inode->i_uid = cred->euid;
1562 inode->i_gid = cred->egid;
1565 security_task_to_inode(task, inode);
1575 int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1577 struct inode *inode = dentry->d_inode;
1578 struct task_struct *task;
1579 const struct cred *cred;
1580 struct pid_namespace *pid = dentry->d_sb->s_fs_info;
1582 generic_fillattr(inode, stat);
1587 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1589 if (!has_pid_permissions(pid, task, 2)) {
1592 * This doesn't prevent learning whether PID exists,
1593 * it only makes getattr() consistent with readdir().
1597 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1598 task_dumpable(task)) {
1599 cred = __task_cred(task);
1600 stat->uid = cred->euid;
1601 stat->gid = cred->egid;
1611 * Exceptional case: normally we are not allowed to unhash a busy
1612 * directory. In this case, however, we can do it - no aliasing problems
1613 * due to the way we treat inodes.
1615 * Rewrite the inode's ownerships here because the owning task may have
1616 * performed a setuid(), etc.
1618 * Before the /proc/pid/status file was created the only way to read
1619 * the effective uid of a /process was to stat /proc/pid. Reading
1620 * /proc/pid/status is slow enough that procps and other packages
1621 * kept stating /proc/pid. To keep the rules in /proc simple I have
1622 * made this apply to all per process world readable and executable
1625 int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1627 struct inode *inode;
1628 struct task_struct *task;
1629 const struct cred *cred;
1631 if (nd && nd->flags & LOOKUP_RCU)
1634 inode = dentry->d_inode;
1635 task = get_proc_task(inode);
1638 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1639 task_dumpable(task)) {
1641 cred = __task_cred(task);
1642 inode->i_uid = cred->euid;
1643 inode->i_gid = cred->egid;
1649 inode->i_mode &= ~(S_ISUID | S_ISGID);
1650 security_task_to_inode(task, inode);
1651 put_task_struct(task);
1658 static int pid_delete_dentry(const struct dentry * dentry)
1660 /* Is the task we represent dead?
1661 * If so, then don't put the dentry on the lru list,
1662 * kill it immediately.
1664 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1667 const struct dentry_operations pid_dentry_operations =
1669 .d_revalidate = pid_revalidate,
1670 .d_delete = pid_delete_dentry,
1676 * Fill a directory entry.
1678 * If possible create the dcache entry and derive our inode number and
1679 * file type from dcache entry.
1681 * Since all of the proc inode numbers are dynamically generated, the inode
1682 * numbers do not exist until the inode is cache. This means creating the
1683 * the dcache entry in readdir is necessary to keep the inode numbers
1684 * reported by readdir in sync with the inode numbers reported
1687 int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1688 const char *name, int len,
1689 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1691 struct dentry *child, *dir = filp->f_path.dentry;
1692 struct inode *inode;
1695 unsigned type = DT_UNKNOWN;
1699 qname.hash = full_name_hash(name, len);
1701 child = d_lookup(dir, &qname);
1704 new = d_alloc(dir, &qname);
1706 child = instantiate(dir->d_inode, new, task, ptr);
1713 if (!child || IS_ERR(child) || !child->d_inode)
1714 goto end_instantiate;
1715 inode = child->d_inode;
1718 type = inode->i_mode >> 12;
1723 ino = find_inode_number(dir, &qname);
1726 return filldir(dirent, name, len, filp->f_pos, ino, type);
1729 static unsigned name_to_int(struct dentry *dentry)
1731 const char *name = dentry->d_name.name;
1732 int len = dentry->d_name.len;
1735 if (len > 1 && *name == '0')
1738 unsigned c = *name++ - '0';
1741 if (n >= (~0U-9)/10)
1751 #define PROC_FDINFO_MAX 64
1753 static int proc_fd_info(struct inode *inode, struct path *path, char *info)
1755 struct task_struct *task = get_proc_task(inode);
1756 struct files_struct *files = NULL;
1758 int fd = proc_fd(inode);
1761 files = get_files_struct(task);
1762 put_task_struct(task);
1766 * We are not taking a ref to the file structure, so we must
1769 spin_lock(&files->file_lock);
1770 file = fcheck_files(files, fd);
1772 unsigned int f_flags;
1773 struct fdtable *fdt;
1775 fdt = files_fdtable(files);
1776 f_flags = file->f_flags & ~O_CLOEXEC;
1777 if (FD_ISSET(fd, fdt->close_on_exec))
1778 f_flags |= O_CLOEXEC;
1781 *path = file->f_path;
1782 path_get(&file->f_path);
1785 snprintf(info, PROC_FDINFO_MAX,
1788 (long long) file->f_pos,
1790 spin_unlock(&files->file_lock);
1791 put_files_struct(files);
1794 spin_unlock(&files->file_lock);
1795 put_files_struct(files);
1800 static int proc_fd_link(struct dentry *dentry, struct path *path)
1802 return proc_fd_info(dentry->d_inode, path, NULL);
1805 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1807 struct inode *inode;
1808 struct task_struct *task;
1810 struct files_struct *files;
1811 const struct cred *cred;
1813 if (nd && nd->flags & LOOKUP_RCU)
1816 inode = dentry->d_inode;
1817 task = get_proc_task(inode);
1818 fd = proc_fd(inode);
1821 files = get_files_struct(task);
1824 if (fcheck_files(files, fd)) {
1826 put_files_struct(files);
1827 if (task_dumpable(task)) {
1829 cred = __task_cred(task);
1830 inode->i_uid = cred->euid;
1831 inode->i_gid = cred->egid;
1837 inode->i_mode &= ~(S_ISUID | S_ISGID);
1838 security_task_to_inode(task, inode);
1839 put_task_struct(task);
1843 put_files_struct(files);
1845 put_task_struct(task);
1851 static const struct dentry_operations tid_fd_dentry_operations =
1853 .d_revalidate = tid_fd_revalidate,
1854 .d_delete = pid_delete_dentry,
1857 static struct dentry *proc_fd_instantiate(struct inode *dir,
1858 struct dentry *dentry, struct task_struct *task, const void *ptr)
1860 unsigned fd = *(const unsigned *)ptr;
1862 struct files_struct *files;
1863 struct inode *inode;
1864 struct proc_inode *ei;
1865 struct dentry *error = ERR_PTR(-ENOENT);
1867 inode = proc_pid_make_inode(dir->i_sb, task);
1872 files = get_files_struct(task);
1875 inode->i_mode = S_IFLNK;
1878 * We are not taking a ref to the file structure, so we must
1881 spin_lock(&files->file_lock);
1882 file = fcheck_files(files, fd);
1885 if (file->f_mode & FMODE_READ)
1886 inode->i_mode |= S_IRUSR | S_IXUSR;
1887 if (file->f_mode & FMODE_WRITE)
1888 inode->i_mode |= S_IWUSR | S_IXUSR;
1889 spin_unlock(&files->file_lock);
1890 put_files_struct(files);
1892 inode->i_op = &proc_pid_link_inode_operations;
1894 ei->op.proc_get_link = proc_fd_link;
1895 d_set_d_op(dentry, &tid_fd_dentry_operations);
1896 d_add(dentry, inode);
1897 /* Close the race of the process dying before we return the dentry */
1898 if (tid_fd_revalidate(dentry, NULL))
1904 spin_unlock(&files->file_lock);
1905 put_files_struct(files);
1911 static struct dentry *proc_lookupfd_common(struct inode *dir,
1912 struct dentry *dentry,
1913 instantiate_t instantiate)
1915 struct task_struct *task = get_proc_task(dir);
1916 unsigned fd = name_to_int(dentry);
1917 struct dentry *result = ERR_PTR(-ENOENT);
1924 result = instantiate(dir, dentry, task, &fd);
1926 put_task_struct(task);
1931 static int proc_readfd_common(struct file * filp, void * dirent,
1932 filldir_t filldir, instantiate_t instantiate)
1934 struct dentry *dentry = filp->f_path.dentry;
1935 struct inode *inode = dentry->d_inode;
1936 struct task_struct *p = get_proc_task(inode);
1937 unsigned int fd, ino;
1939 struct files_struct * files;
1949 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1953 ino = parent_ino(dentry);
1954 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1958 files = get_files_struct(p);
1962 for (fd = filp->f_pos-2;
1963 fd < files_fdtable(files)->max_fds;
1964 fd++, filp->f_pos++) {
1965 char name[PROC_NUMBUF];
1968 if (!fcheck_files(files, fd))
1972 len = snprintf(name, sizeof(name), "%d", fd);
1973 if (proc_fill_cache(filp, dirent, filldir,
1974 name, len, instantiate,
1982 put_files_struct(files);
1990 static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
1991 struct nameidata *nd)
1993 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
1996 static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
1998 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
2001 static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
2002 size_t len, loff_t *ppos)
2004 char tmp[PROC_FDINFO_MAX];
2005 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
2007 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
2011 static const struct file_operations proc_fdinfo_file_operations = {
2012 .open = nonseekable_open,
2013 .read = proc_fdinfo_read,
2014 .llseek = no_llseek,
2017 static const struct file_operations proc_fd_operations = {
2018 .read = generic_read_dir,
2019 .readdir = proc_readfd,
2020 .llseek = default_llseek,
2023 #ifdef CONFIG_CHECKPOINT_RESTORE
2026 * dname_to_vma_addr - maps a dentry name into two unsigned longs
2027 * which represent vma start and end addresses.
2029 static int dname_to_vma_addr(struct dentry *dentry,
2030 unsigned long *start, unsigned long *end)
2032 if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2)
2038 static int map_files_d_revalidate(struct dentry *dentry, struct nameidata *nd)
2040 unsigned long vm_start, vm_end;
2041 bool exact_vma_exists = false;
2042 struct mm_struct *mm = NULL;
2043 struct task_struct *task;
2044 const struct cred *cred;
2045 struct inode *inode;
2048 if (nd && nd->flags & LOOKUP_RCU)
2051 if (!capable(CAP_SYS_ADMIN)) {
2056 inode = dentry->d_inode;
2057 task = get_proc_task(inode);
2061 if (!ptrace_may_access(task, PTRACE_MODE_READ))
2064 mm = get_task_mm(task);
2068 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
2069 down_read(&mm->mmap_sem);
2070 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
2071 up_read(&mm->mmap_sem);
2076 if (exact_vma_exists) {
2077 if (task_dumpable(task)) {
2079 cred = __task_cred(task);
2080 inode->i_uid = cred->euid;
2081 inode->i_gid = cred->egid;
2087 security_task_to_inode(task, inode);
2092 put_task_struct(task);
2101 static const struct dentry_operations tid_map_files_dentry_operations = {
2102 .d_revalidate = map_files_d_revalidate,
2103 .d_delete = pid_delete_dentry,
2106 static int proc_map_files_get_link(struct dentry *dentry, struct path *path)
2108 unsigned long vm_start, vm_end;
2109 struct vm_area_struct *vma;
2110 struct task_struct *task;
2111 struct mm_struct *mm;
2115 task = get_proc_task(dentry->d_inode);
2119 mm = get_task_mm(task);
2120 put_task_struct(task);
2124 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
2128 down_read(&mm->mmap_sem);
2129 vma = find_exact_vma(mm, vm_start, vm_end);
2130 if (vma && vma->vm_file) {
2131 *path = vma->vm_file->f_path;
2135 up_read(&mm->mmap_sem);
2143 struct map_files_info {
2146 unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
2149 static struct dentry *
2150 proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
2151 struct task_struct *task, const void *ptr)
2153 const struct file *file = ptr;
2154 struct proc_inode *ei;
2155 struct inode *inode;
2158 return ERR_PTR(-ENOENT);
2160 inode = proc_pid_make_inode(dir->i_sb, task);
2162 return ERR_PTR(-ENOENT);
2165 ei->op.proc_get_link = proc_map_files_get_link;
2167 inode->i_op = &proc_pid_link_inode_operations;
2169 inode->i_mode = S_IFLNK;
2171 if (file->f_mode & FMODE_READ)
2172 inode->i_mode |= S_IRUSR;
2173 if (file->f_mode & FMODE_WRITE)
2174 inode->i_mode |= S_IWUSR;
2176 d_set_d_op(dentry, &tid_map_files_dentry_operations);
2177 d_add(dentry, inode);
2182 static struct dentry *proc_map_files_lookup(struct inode *dir,
2183 struct dentry *dentry, struct nameidata *nd)
2185 unsigned long vm_start, vm_end;
2186 struct vm_area_struct *vma;
2187 struct task_struct *task;
2188 struct dentry *result;
2189 struct mm_struct *mm;
2191 result = ERR_PTR(-EACCES);
2192 if (!capable(CAP_SYS_ADMIN))
2195 result = ERR_PTR(-ENOENT);
2196 task = get_proc_task(dir);
2200 result = ERR_PTR(-EACCES);
2201 if (lock_trace(task))
2204 result = ERR_PTR(-ENOENT);
2205 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
2208 mm = get_task_mm(task);
2212 down_read(&mm->mmap_sem);
2213 vma = find_exact_vma(mm, vm_start, vm_end);
2217 result = proc_map_files_instantiate(dir, dentry, task, vma->vm_file);
2220 up_read(&mm->mmap_sem);
2225 put_task_struct(task);
2230 static const struct inode_operations proc_map_files_inode_operations = {
2231 .lookup = proc_map_files_lookup,
2232 .permission = proc_fd_permission,
2233 .setattr = proc_setattr,
2237 proc_map_files_readdir(struct file *filp, void *dirent, filldir_t filldir)
2239 struct dentry *dentry = filp->f_path.dentry;
2240 struct inode *inode = dentry->d_inode;
2241 struct vm_area_struct *vma;
2242 struct task_struct *task;
2243 struct mm_struct *mm;
2248 if (!capable(CAP_SYS_ADMIN))
2252 task = get_proc_task(inode);
2257 if (lock_trace(task))
2261 switch (filp->f_pos) {
2264 if (filldir(dirent, ".", 1, 0, ino, DT_DIR) < 0)
2268 ino = parent_ino(dentry);
2269 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
2274 unsigned long nr_files, pos, i;
2275 struct flex_array *fa = NULL;
2276 struct map_files_info info;
2277 struct map_files_info *p;
2279 mm = get_task_mm(task);
2282 down_read(&mm->mmap_sem);
2287 * We need two passes here:
2289 * 1) Collect vmas of mapped files with mmap_sem taken
2290 * 2) Release mmap_sem and instantiate entries
2292 * otherwise we get lockdep complained, since filldir()
2293 * routine might require mmap_sem taken in might_fault().
2296 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
2297 if (vma->vm_file && ++pos > filp->f_pos)
2302 fa = flex_array_alloc(sizeof(info), nr_files,
2304 if (!fa || flex_array_prealloc(fa, 0, nr_files,
2308 flex_array_free(fa);
2309 up_read(&mm->mmap_sem);
2313 for (i = 0, vma = mm->mmap, pos = 2; vma;
2314 vma = vma->vm_next) {
2317 if (++pos <= filp->f_pos)
2320 get_file(vma->vm_file);
2321 info.file = vma->vm_file;
2322 info.len = snprintf(info.name,
2323 sizeof(info.name), "%lx-%lx",
2324 vma->vm_start, vma->vm_end);
2325 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
2329 up_read(&mm->mmap_sem);
2331 for (i = 0; i < nr_files; i++) {
2332 p = flex_array_get(fa, i);
2333 ret = proc_fill_cache(filp, dirent, filldir,
2335 proc_map_files_instantiate,
2342 for (; i < nr_files; i++) {
2344 * In case of error don't forget
2345 * to put rest of file refs.
2347 p = flex_array_get(fa, i);
2351 flex_array_free(fa);
2359 put_task_struct(task);
2364 static const struct file_operations proc_map_files_operations = {
2365 .read = generic_read_dir,
2366 .readdir = proc_map_files_readdir,
2367 .llseek = default_llseek,
2370 #endif /* CONFIG_CHECKPOINT_RESTORE */
2373 * /proc/pid/fd needs a special permission handler so that a process can still
2374 * access /proc/self/fd after it has executed a setuid().
2376 static int proc_fd_permission(struct inode *inode, int mask)
2378 int rv = generic_permission(inode, mask);
2381 if (task_pid(current) == proc_pid(inode))
2387 * proc directories can do almost nothing..
2389 static const struct inode_operations proc_fd_inode_operations = {
2390 .lookup = proc_lookupfd,
2391 .permission = proc_fd_permission,
2392 .setattr = proc_setattr,
2395 static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
2396 struct dentry *dentry, struct task_struct *task, const void *ptr)
2398 unsigned fd = *(unsigned *)ptr;
2399 struct inode *inode;
2400 struct proc_inode *ei;
2401 struct dentry *error = ERR_PTR(-ENOENT);
2403 inode = proc_pid_make_inode(dir->i_sb, task);
2408 inode->i_mode = S_IFREG | S_IRUSR;
2409 inode->i_fop = &proc_fdinfo_file_operations;
2410 d_set_d_op(dentry, &tid_fd_dentry_operations);
2411 d_add(dentry, inode);
2412 /* Close the race of the process dying before we return the dentry */
2413 if (tid_fd_revalidate(dentry, NULL))
2420 static struct dentry *proc_lookupfdinfo(struct inode *dir,
2421 struct dentry *dentry,
2422 struct nameidata *nd)
2424 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
2427 static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
2429 return proc_readfd_common(filp, dirent, filldir,
2430 proc_fdinfo_instantiate);
2433 static const struct file_operations proc_fdinfo_operations = {
2434 .read = generic_read_dir,
2435 .readdir = proc_readfdinfo,
2436 .llseek = default_llseek,
2440 * proc directories can do almost nothing..
2442 static const struct inode_operations proc_fdinfo_inode_operations = {
2443 .lookup = proc_lookupfdinfo,
2444 .setattr = proc_setattr,
2448 static struct dentry *proc_pident_instantiate(struct inode *dir,
2449 struct dentry *dentry, struct task_struct *task, const void *ptr)
2451 const struct pid_entry *p = ptr;
2452 struct inode *inode;
2453 struct proc_inode *ei;
2454 struct dentry *error = ERR_PTR(-ENOENT);
2456 inode = proc_pid_make_inode(dir->i_sb, task);
2461 inode->i_mode = p->mode;
2462 if (S_ISDIR(inode->i_mode))
2463 set_nlink(inode, 2); /* Use getattr to fix if necessary */
2465 inode->i_op = p->iop;
2467 inode->i_fop = p->fop;
2469 d_set_d_op(dentry, &pid_dentry_operations);
2470 d_add(dentry, inode);
2471 /* Close the race of the process dying before we return the dentry */
2472 if (pid_revalidate(dentry, NULL))
2478 static struct dentry *proc_pident_lookup(struct inode *dir,
2479 struct dentry *dentry,
2480 const struct pid_entry *ents,
2483 struct dentry *error;
2484 struct task_struct *task = get_proc_task(dir);
2485 const struct pid_entry *p, *last;
2487 error = ERR_PTR(-ENOENT);
2493 * Yes, it does not scale. And it should not. Don't add
2494 * new entries into /proc/<tgid>/ without very good reasons.
2496 last = &ents[nents - 1];
2497 for (p = ents; p <= last; p++) {
2498 if (p->len != dentry->d_name.len)
2500 if (!memcmp(dentry->d_name.name, p->name, p->len))
2506 error = proc_pident_instantiate(dir, dentry, task, p);
2508 put_task_struct(task);
2513 static int proc_pident_fill_cache(struct file *filp, void *dirent,
2514 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2516 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2517 proc_pident_instantiate, task, p);
2520 static int proc_pident_readdir(struct file *filp,
2521 void *dirent, filldir_t filldir,
2522 const struct pid_entry *ents, unsigned int nents)
2525 struct dentry *dentry = filp->f_path.dentry;
2526 struct inode *inode = dentry->d_inode;
2527 struct task_struct *task = get_proc_task(inode);
2528 const struct pid_entry *p, *last;
2541 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
2547 ino = parent_ino(dentry);
2548 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
2560 last = &ents[nents - 1];
2562 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
2571 put_task_struct(task);
2576 #ifdef CONFIG_SECURITY
2577 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2578 size_t count, loff_t *ppos)
2580 struct inode * inode = file->f_path.dentry->d_inode;
2583 struct task_struct *task = get_proc_task(inode);
2588 length = security_getprocattr(task,
2589 (char*)file->f_path.dentry->d_name.name,
2591 put_task_struct(task);
2593 length = simple_read_from_buffer(buf, count, ppos, p, length);
2598 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2599 size_t count, loff_t *ppos)
2601 struct inode * inode = file->f_path.dentry->d_inode;
2604 struct task_struct *task = get_proc_task(inode);
2609 if (count > PAGE_SIZE)
2612 /* No partial writes. */
2618 page = (char*)__get_free_page(GFP_TEMPORARY);
2623 if (copy_from_user(page, buf, count))
2626 /* Guard against adverse ptrace interaction */
2627 length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
2631 length = security_setprocattr(task,
2632 (char*)file->f_path.dentry->d_name.name,
2633 (void*)page, count);
2634 mutex_unlock(&task->signal->cred_guard_mutex);
2636 free_page((unsigned long) page);
2638 put_task_struct(task);
2643 static const struct file_operations proc_pid_attr_operations = {
2644 .read = proc_pid_attr_read,
2645 .write = proc_pid_attr_write,
2646 .llseek = generic_file_llseek,
2649 static const struct pid_entry attr_dir_stuff[] = {
2650 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2651 REG("prev", S_IRUGO, proc_pid_attr_operations),
2652 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2653 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2654 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2655 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2658 static int proc_attr_dir_readdir(struct file * filp,
2659 void * dirent, filldir_t filldir)
2661 return proc_pident_readdir(filp,dirent,filldir,
2662 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
2665 static const struct file_operations proc_attr_dir_operations = {
2666 .read = generic_read_dir,
2667 .readdir = proc_attr_dir_readdir,
2668 .llseek = default_llseek,
2671 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2672 struct dentry *dentry, struct nameidata *nd)
2674 return proc_pident_lookup(dir, dentry,
2675 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2678 static const struct inode_operations proc_attr_dir_inode_operations = {
2679 .lookup = proc_attr_dir_lookup,
2680 .getattr = pid_getattr,
2681 .setattr = proc_setattr,
2686 #ifdef CONFIG_ELF_CORE
2687 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2688 size_t count, loff_t *ppos)
2690 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2691 struct mm_struct *mm;
2692 char buffer[PROC_NUMBUF];
2700 mm = get_task_mm(task);
2702 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2703 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2704 MMF_DUMP_FILTER_SHIFT));
2706 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2709 put_task_struct(task);
2714 static ssize_t proc_coredump_filter_write(struct file *file,
2715 const char __user *buf,
2719 struct task_struct *task;
2720 struct mm_struct *mm;
2721 char buffer[PROC_NUMBUF], *end;
2728 memset(buffer, 0, sizeof(buffer));
2729 if (count > sizeof(buffer) - 1)
2730 count = sizeof(buffer) - 1;
2731 if (copy_from_user(buffer, buf, count))
2735 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2738 if (end - buffer == 0)
2742 task = get_proc_task(file->f_dentry->d_inode);
2747 mm = get_task_mm(task);
2751 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2753 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2755 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2760 put_task_struct(task);
2765 static const struct file_operations proc_coredump_filter_operations = {
2766 .read = proc_coredump_filter_read,
2767 .write = proc_coredump_filter_write,
2768 .llseek = generic_file_llseek,
2775 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2778 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2779 pid_t tgid = task_tgid_nr_ns(current, ns);
2780 char tmp[PROC_NUMBUF];
2783 sprintf(tmp, "%d", tgid);
2784 return vfs_readlink(dentry,buffer,buflen,tmp);
2787 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
2789 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2790 pid_t tgid = task_tgid_nr_ns(current, ns);
2791 char *name = ERR_PTR(-ENOENT);
2795 name = ERR_PTR(-ENOMEM);
2797 sprintf(name, "%d", tgid);
2799 nd_set_link(nd, name);
2803 static void proc_self_put_link(struct dentry *dentry, struct nameidata *nd,
2806 char *s = nd_get_link(nd);
2811 static const struct inode_operations proc_self_inode_operations = {
2812 .readlink = proc_self_readlink,
2813 .follow_link = proc_self_follow_link,
2814 .put_link = proc_self_put_link,
2820 * These are the directory entries in the root directory of /proc
2821 * that properly belong to the /proc filesystem, as they describe
2822 * describe something that is process related.
2824 static const struct pid_entry proc_base_stuff[] = {
2825 NOD("self", S_IFLNK|S_IRWXUGO,
2826 &proc_self_inode_operations, NULL, {}),
2829 static struct dentry *proc_base_instantiate(struct inode *dir,
2830 struct dentry *dentry, struct task_struct *task, const void *ptr)
2832 const struct pid_entry *p = ptr;
2833 struct inode *inode;
2834 struct proc_inode *ei;
2835 struct dentry *error;
2837 /* Allocate the inode */
2838 error = ERR_PTR(-ENOMEM);
2839 inode = new_inode(dir->i_sb);
2843 /* Initialize the inode */
2845 inode->i_ino = get_next_ino();
2846 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2849 * grab the reference to the task.
2851 ei->pid = get_task_pid(task, PIDTYPE_PID);
2855 inode->i_mode = p->mode;
2856 if (S_ISDIR(inode->i_mode))
2857 set_nlink(inode, 2);
2858 if (S_ISLNK(inode->i_mode))
2861 inode->i_op = p->iop;
2863 inode->i_fop = p->fop;
2865 d_add(dentry, inode);
2874 static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2876 struct dentry *error;
2877 struct task_struct *task = get_proc_task(dir);
2878 const struct pid_entry *p, *last;
2880 error = ERR_PTR(-ENOENT);
2885 /* Lookup the directory entry */
2886 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2887 for (p = proc_base_stuff; p <= last; p++) {
2888 if (p->len != dentry->d_name.len)
2890 if (!memcmp(dentry->d_name.name, p->name, p->len))
2896 error = proc_base_instantiate(dir, dentry, task, p);
2899 put_task_struct(task);
2904 static int proc_base_fill_cache(struct file *filp, void *dirent,
2905 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2907 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2908 proc_base_instantiate, task, p);
2911 #ifdef CONFIG_TASK_IO_ACCOUNTING
2912 static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
2914 struct task_io_accounting acct = task->ioac;
2915 unsigned long flags;
2918 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2922 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
2927 if (whole && lock_task_sighand(task, &flags)) {
2928 struct task_struct *t = task;
2930 task_io_accounting_add(&acct, &task->signal->ioac);
2931 while_each_thread(task, t)
2932 task_io_accounting_add(&acct, &t->ioac);
2934 unlock_task_sighand(task, &flags);
2936 result = sprintf(buffer,
2941 "read_bytes: %llu\n"
2942 "write_bytes: %llu\n"
2943 "cancelled_write_bytes: %llu\n",
2944 (unsigned long long)acct.rchar,
2945 (unsigned long long)acct.wchar,
2946 (unsigned long long)acct.syscr,
2947 (unsigned long long)acct.syscw,
2948 (unsigned long long)acct.read_bytes,
2949 (unsigned long long)acct.write_bytes,
2950 (unsigned long long)acct.cancelled_write_bytes);
2952 mutex_unlock(&task->signal->cred_guard_mutex);
2956 static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2958 return do_io_accounting(task, buffer, 0);
2961 static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2963 return do_io_accounting(task, buffer, 1);
2965 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2967 static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2968 struct pid *pid, struct task_struct *task)
2970 int err = lock_trace(task);
2972 seq_printf(m, "%08x\n", task->personality);
2981 static const struct file_operations proc_task_operations;
2982 static const struct inode_operations proc_task_inode_operations;
2984 static const struct pid_entry tgid_base_stuff[] = {
2985 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2986 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2987 #ifdef CONFIG_CHECKPOINT_RESTORE
2988 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
2990 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2991 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
2993 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2995 REG("environ", S_IRUSR, proc_environ_operations),
2996 INF("auxv", S_IRUSR, proc_pid_auxv),
2997 ONE("status", S_IRUGO, proc_pid_status),
2998 ONE("personality", S_IRUGO, proc_pid_personality),
2999 INF("limits", S_IRUGO, proc_pid_limits),
3000 #ifdef CONFIG_SCHED_DEBUG
3001 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
3003 #ifdef CONFIG_SCHED_AUTOGROUP
3004 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
3006 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
3007 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3008 INF("syscall", S_IRUGO, proc_pid_syscall),
3010 INF("cmdline", S_IRUGO, proc_pid_cmdline),
3011 ONE("stat", S_IRUGO, proc_tgid_stat),
3012 ONE("statm", S_IRUGO, proc_pid_statm),
3013 REG("maps", S_IRUGO, proc_maps_operations),
3015 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
3017 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3018 LNK("cwd", proc_cwd_link),
3019 LNK("root", proc_root_link),
3020 LNK("exe", proc_exe_link),
3021 REG("mounts", S_IRUGO, proc_mounts_operations),
3022 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
3023 REG("mountstats", S_IRUSR, proc_mountstats_operations),
3024 #ifdef CONFIG_PROC_PAGE_MONITOR
3025 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3026 REG("smaps", S_IRUGO, proc_smaps_operations),
3027 REG("pagemap", S_IRUGO, proc_pagemap_operations),
3029 #ifdef CONFIG_SECURITY
3030 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
3032 #ifdef CONFIG_KALLSYMS
3033 INF("wchan", S_IRUGO, proc_pid_wchan),
3035 #ifdef CONFIG_STACKTRACE
3036 ONE("stack", S_IRUGO, proc_pid_stack),
3038 #ifdef CONFIG_SCHEDSTATS
3039 INF("schedstat", S_IRUGO, proc_pid_schedstat),
3041 #ifdef CONFIG_LATENCYTOP
3042 REG("latency", S_IRUGO, proc_lstats_operations),
3044 #ifdef CONFIG_PROC_PID_CPUSET
3045 REG("cpuset", S_IRUGO, proc_cpuset_operations),
3047 #ifdef CONFIG_CGROUPS
3048 REG("cgroup", S_IRUGO, proc_cgroup_operations),
3050 INF("oom_score", S_IRUGO, proc_oom_score),
3051 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
3052 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
3053 #ifdef CONFIG_AUDITSYSCALL
3054 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3055 REG("sessionid", S_IRUGO, proc_sessionid_operations),
3057 #ifdef CONFIG_FAULT_INJECTION
3058 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3060 #ifdef CONFIG_ELF_CORE
3061 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
3063 #ifdef CONFIG_TASK_IO_ACCOUNTING
3064 INF("io", S_IRUSR, proc_tgid_io_accounting),
3066 #ifdef CONFIG_HARDWALL
3067 INF("hardwall", S_IRUGO, proc_pid_hardwall),
3071 static int proc_tgid_base_readdir(struct file * filp,
3072 void * dirent, filldir_t filldir)
3074 return proc_pident_readdir(filp,dirent,filldir,
3075 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
3078 static const struct file_operations proc_tgid_base_operations = {
3079 .read = generic_read_dir,
3080 .readdir = proc_tgid_base_readdir,
3081 .llseek = default_llseek,
3084 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
3085 return proc_pident_lookup(dir, dentry,
3086 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
3089 static const struct inode_operations proc_tgid_base_inode_operations = {
3090 .lookup = proc_tgid_base_lookup,
3091 .getattr = pid_getattr,
3092 .setattr = proc_setattr,
3093 .permission = proc_pid_permission,
3096 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
3098 struct dentry *dentry, *leader, *dir;
3099 char buf[PROC_NUMBUF];
3103 name.len = snprintf(buf, sizeof(buf), "%d", pid);
3104 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
3106 shrink_dcache_parent(dentry);
3112 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
3113 leader = d_hash_and_lookup(mnt->mnt_root, &name);
3118 name.len = strlen(name.name);
3119 dir = d_hash_and_lookup(leader, &name);
3121 goto out_put_leader;
3124 name.len = snprintf(buf, sizeof(buf), "%d", pid);
3125 dentry = d_hash_and_lookup(dir, &name);
3127 shrink_dcache_parent(dentry);
3140 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
3141 * @task: task that should be flushed.
3143 * When flushing dentries from proc, one needs to flush them from global
3144 * proc (proc_mnt) and from all the namespaces' procs this task was seen
3145 * in. This call is supposed to do all of this job.
3147 * Looks in the dcache for
3149 * /proc/@tgid/task/@pid
3150 * if either directory is present flushes it and all of it'ts children
3153 * It is safe and reasonable to cache /proc entries for a task until
3154 * that task exits. After that they just clog up the dcache with
3155 * useless entries, possibly causing useful dcache entries to be
3156 * flushed instead. This routine is proved to flush those useless
3157 * dcache entries at process exit time.
3159 * NOTE: This routine is just an optimization so it does not guarantee
3160 * that no dcache entries will exist at process exit time it
3161 * just makes it very unlikely that any will persist.
3164 void proc_flush_task(struct task_struct *task)
3167 struct pid *pid, *tgid;
3170 pid = task_pid(task);
3171 tgid = task_tgid(task);
3173 for (i = 0; i <= pid->level; i++) {
3174 upid = &pid->numbers[i];
3175 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
3176 tgid->numbers[i].nr);
3179 upid = &pid->numbers[pid->level];
3181 pid_ns_release_proc(upid->ns);
3184 static struct dentry *proc_pid_instantiate(struct inode *dir,
3185 struct dentry * dentry,
3186 struct task_struct *task, const void *ptr)
3188 struct dentry *error = ERR_PTR(-ENOENT);
3189 struct inode *inode;
3191 inode = proc_pid_make_inode(dir->i_sb, task);
3195 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3196 inode->i_op = &proc_tgid_base_inode_operations;
3197 inode->i_fop = &proc_tgid_base_operations;
3198 inode->i_flags|=S_IMMUTABLE;
3200 set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
3201 ARRAY_SIZE(tgid_base_stuff)));
3203 d_set_d_op(dentry, &pid_dentry_operations);
3205 d_add(dentry, inode);
3206 /* Close the race of the process dying before we return the dentry */
3207 if (pid_revalidate(dentry, NULL))
3213 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
3215 struct dentry *result;
3216 struct task_struct *task;
3218 struct pid_namespace *ns;
3220 result = proc_base_lookup(dir, dentry);
3221 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
3224 tgid = name_to_int(dentry);
3228 ns = dentry->d_sb->s_fs_info;
3230 task = find_task_by_pid_ns(tgid, ns);
3232 get_task_struct(task);
3237 result = proc_pid_instantiate(dir, dentry, task, NULL);
3238 put_task_struct(task);
3244 * Find the first task with tgid >= tgid
3249 struct task_struct *task;
3251 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3256 put_task_struct(iter.task);
3260 pid = find_ge_pid(iter.tgid, ns);
3262 iter.tgid = pid_nr_ns(pid, ns);
3263 iter.task = pid_task(pid, PIDTYPE_PID);
3264 /* What we to know is if the pid we have find is the
3265 * pid of a thread_group_leader. Testing for task
3266 * being a thread_group_leader is the obvious thing
3267 * todo but there is a window when it fails, due to
3268 * the pid transfer logic in de_thread.
3270 * So we perform the straight forward test of seeing
3271 * if the pid we have found is the pid of a thread
3272 * group leader, and don't worry if the task we have
3273 * found doesn't happen to be a thread group leader.
3274 * As we don't care in the case of readdir.
3276 if (!iter.task || !has_group_leader_pid(iter.task)) {
3280 get_task_struct(iter.task);
3286 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
3288 static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3289 struct tgid_iter iter)
3291 char name[PROC_NUMBUF];
3292 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
3293 return proc_fill_cache(filp, dirent, filldir, name, len,
3294 proc_pid_instantiate, iter.task, NULL);
3297 static int fake_filldir(void *buf, const char *name, int namelen,
3298 loff_t offset, u64 ino, unsigned d_type)
3303 /* for the /proc/ directory itself, after non-process stuff has been done */
3304 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
3307 struct task_struct *reaper;
3308 struct tgid_iter iter;
3309 struct pid_namespace *ns;
3310 filldir_t __filldir;
3312 if (filp->f_pos >= PID_MAX_LIMIT + TGID_OFFSET)
3314 nr = filp->f_pos - FIRST_PROCESS_ENTRY;
3316 reaper = get_proc_task(filp->f_path.dentry->d_inode);
3320 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
3321 const struct pid_entry *p = &proc_base_stuff[nr];
3322 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
3326 ns = filp->f_dentry->d_sb->s_fs_info;
3328 iter.tgid = filp->f_pos - TGID_OFFSET;
3329 for (iter = next_tgid(ns, iter);
3331 iter.tgid += 1, iter = next_tgid(ns, iter)) {
3332 if (has_pid_permissions(ns, iter.task, 2))
3333 __filldir = filldir;
3335 __filldir = fake_filldir;
3337 filp->f_pos = iter.tgid + TGID_OFFSET;
3338 if (proc_pid_fill_cache(filp, dirent, __filldir, iter) < 0) {
3339 put_task_struct(iter.task);
3343 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
3345 put_task_struct(reaper);
3353 static const struct pid_entry tid_base_stuff[] = {
3354 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
3355 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
3356 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
3357 REG("environ", S_IRUSR, proc_environ_operations),
3358 INF("auxv", S_IRUSR, proc_pid_auxv),
3359 ONE("status", S_IRUGO, proc_pid_status),
3360 ONE("personality", S_IRUGO, proc_pid_personality),
3361 INF("limits", S_IRUGO, proc_pid_limits),
3362 #ifdef CONFIG_SCHED_DEBUG
3363 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
3365 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
3366 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3367 INF("syscall", S_IRUGO, proc_pid_syscall),
3369 INF("cmdline", S_IRUGO, proc_pid_cmdline),
3370 ONE("stat", S_IRUGO, proc_tid_stat),
3371 ONE("statm", S_IRUGO, proc_pid_statm),
3372 REG("maps", S_IRUGO, proc_maps_operations),
3374 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
3376 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3377 LNK("cwd", proc_cwd_link),
3378 LNK("root", proc_root_link),
3379 LNK("exe", proc_exe_link),
3380 REG("mounts", S_IRUGO, proc_mounts_operations),
3381 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
3382 #ifdef CONFIG_PROC_PAGE_MONITOR
3383 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3384 REG("smaps", S_IRUGO, proc_smaps_operations),
3385 REG("pagemap", S_IRUGO, proc_pagemap_operations),
3387 #ifdef CONFIG_SECURITY
3388 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
3390 #ifdef CONFIG_KALLSYMS
3391 INF("wchan", S_IRUGO, proc_pid_wchan),
3393 #ifdef CONFIG_STACKTRACE
3394 ONE("stack", S_IRUGO, proc_pid_stack),
3396 #ifdef CONFIG_SCHEDSTATS
3397 INF("schedstat", S_IRUGO, proc_pid_schedstat),
3399 #ifdef CONFIG_LATENCYTOP
3400 REG("latency", S_IRUGO, proc_lstats_operations),
3402 #ifdef CONFIG_PROC_PID_CPUSET
3403 REG("cpuset", S_IRUGO, proc_cpuset_operations),
3405 #ifdef CONFIG_CGROUPS
3406 REG("cgroup", S_IRUGO, proc_cgroup_operations),
3408 INF("oom_score", S_IRUGO, proc_oom_score),
3409 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
3410 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
3411 #ifdef CONFIG_AUDITSYSCALL
3412 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3413 REG("sessionid", S_IRUGO, proc_sessionid_operations),
3415 #ifdef CONFIG_FAULT_INJECTION
3416 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3418 #ifdef CONFIG_TASK_IO_ACCOUNTING
3419 INF("io", S_IRUSR, proc_tid_io_accounting),
3421 #ifdef CONFIG_HARDWALL
3422 INF("hardwall", S_IRUGO, proc_pid_hardwall),
3426 static int proc_tid_base_readdir(struct file * filp,
3427 void * dirent, filldir_t filldir)
3429 return proc_pident_readdir(filp,dirent,filldir,
3430 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
3433 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
3434 return proc_pident_lookup(dir, dentry,
3435 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3438 static const struct file_operations proc_tid_base_operations = {
3439 .read = generic_read_dir,
3440 .readdir = proc_tid_base_readdir,
3441 .llseek = default_llseek,
3444 static const struct inode_operations proc_tid_base_inode_operations = {
3445 .lookup = proc_tid_base_lookup,
3446 .getattr = pid_getattr,
3447 .setattr = proc_setattr,
3450 static struct dentry *proc_task_instantiate(struct inode *dir,
3451 struct dentry *dentry, struct task_struct *task, const void *ptr)
3453 struct dentry *error = ERR_PTR(-ENOENT);
3454 struct inode *inode;
3455 inode = proc_pid_make_inode(dir->i_sb, task);
3459 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3460 inode->i_op = &proc_tid_base_inode_operations;
3461 inode->i_fop = &proc_tid_base_operations;
3462 inode->i_flags|=S_IMMUTABLE;
3464 set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3465 ARRAY_SIZE(tid_base_stuff)));
3467 d_set_d_op(dentry, &pid_dentry_operations);
3469 d_add(dentry, inode);
3470 /* Close the race of the process dying before we return the dentry */
3471 if (pid_revalidate(dentry, NULL))
3477 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
3479 struct dentry *result = ERR_PTR(-ENOENT);
3480 struct task_struct *task;
3481 struct task_struct *leader = get_proc_task(dir);
3483 struct pid_namespace *ns;
3488 tid = name_to_int(dentry);
3492 ns = dentry->d_sb->s_fs_info;
3494 task = find_task_by_pid_ns(tid, ns);
3496 get_task_struct(task);
3500 if (!same_thread_group(leader, task))
3503 result = proc_task_instantiate(dir, dentry, task, NULL);
3505 put_task_struct(task);
3507 put_task_struct(leader);
3513 * Find the first tid of a thread group to return to user space.
3515 * Usually this is just the thread group leader, but if the users
3516 * buffer was too small or there was a seek into the middle of the
3517 * directory we have more work todo.
3519 * In the case of a short read we start with find_task_by_pid.
3521 * In the case of a seek we start with the leader and walk nr
3524 static struct task_struct *first_tid(struct task_struct *leader,
3525 int tid, int nr, struct pid_namespace *ns)
3527 struct task_struct *pos;
3530 /* Attempt to start with the pid of a thread */
3531 if (tid && (nr > 0)) {
3532 pos = find_task_by_pid_ns(tid, ns);
3533 if (pos && (pos->group_leader == leader))
3537 /* If nr exceeds the number of threads there is nothing todo */
3539 if (nr && nr >= get_nr_threads(leader))
3542 /* If we haven't found our starting place yet start
3543 * with the leader and walk nr threads forward.
3545 for (pos = leader; nr > 0; --nr) {
3546 pos = next_thread(pos);
3547 if (pos == leader) {
3553 get_task_struct(pos);
3560 * Find the next thread in the thread list.
3561 * Return NULL if there is an error or no next thread.
3563 * The reference to the input task_struct is released.
3565 static struct task_struct *next_tid(struct task_struct *start)
3567 struct task_struct *pos = NULL;
3569 if (pid_alive(start)) {
3570 pos = next_thread(start);
3571 if (thread_group_leader(pos))
3574 get_task_struct(pos);
3577 put_task_struct(start);
3581 static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3582 struct task_struct *task, int tid)
3584 char name[PROC_NUMBUF];
3585 int len = snprintf(name, sizeof(name), "%d", tid);
3586 return proc_fill_cache(filp, dirent, filldir, name, len,
3587 proc_task_instantiate, task, NULL);
3590 /* for the /proc/TGID/task/ directories */
3591 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
3593 struct dentry *dentry = filp->f_path.dentry;
3594 struct inode *inode = dentry->d_inode;
3595 struct task_struct *leader = NULL;
3596 struct task_struct *task;
3597 int retval = -ENOENT;
3600 struct pid_namespace *ns;
3602 task = get_proc_task(inode);
3606 if (pid_alive(task)) {
3607 leader = task->group_leader;
3608 get_task_struct(leader);
3611 put_task_struct(task);
3616 switch ((unsigned long)filp->f_pos) {
3619 if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) < 0)
3624 ino = parent_ino(dentry);
3625 if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) < 0)
3631 /* f_version caches the tgid value that the last readdir call couldn't
3632 * return. lseek aka telldir automagically resets f_version to 0.
3634 ns = filp->f_dentry->d_sb->s_fs_info;
3635 tid = (int)filp->f_version;
3636 filp->f_version = 0;
3637 for (task = first_tid(leader, tid, filp->f_pos - 2, ns);
3639 task = next_tid(task), filp->f_pos++) {
3640 tid = task_pid_nr_ns(task, ns);
3641 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
3642 /* returning this tgid failed, save it as the first
3643 * pid for the next readir call */
3644 filp->f_version = (u64)tid;
3645 put_task_struct(task);
3650 put_task_struct(leader);
3655 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3657 struct inode *inode = dentry->d_inode;
3658 struct task_struct *p = get_proc_task(inode);
3659 generic_fillattr(inode, stat);
3662 stat->nlink += get_nr_threads(p);
3669 static const struct inode_operations proc_task_inode_operations = {
3670 .lookup = proc_task_lookup,
3671 .getattr = proc_task_getattr,
3672 .setattr = proc_setattr,
3673 .permission = proc_pid_permission,
3676 static const struct file_operations proc_task_operations = {
3677 .read = generic_read_dir,
3678 .readdir = proc_task_readdir,
3679 .llseek = default_llseek,