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);
714 /* OK to pass negative loff_t, we can catch out-of-range */
715 file->f_mode |= FMODE_UNSIGNED_OFFSET;
716 file->private_data = mm;
721 static ssize_t mem_read(struct file * file, char __user * buf,
722 size_t count, loff_t *ppos)
726 unsigned long src = *ppos;
727 struct mm_struct *mm = file->private_data;
732 page = (char *)__get_free_page(GFP_TEMPORARY);
739 int this_len, retval;
741 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
742 retval = access_remote_vm(mm, src, page, this_len, 0);
749 if (copy_to_user(buf, page, retval)) {
761 free_page((unsigned long) page);
765 static ssize_t mem_write(struct file * file, const char __user *buf,
766 size_t count, loff_t *ppos)
770 unsigned long dst = *ppos;
771 struct mm_struct *mm = file->private_data;
776 page = (char *)__get_free_page(GFP_TEMPORARY);
782 int this_len, retval;
784 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
785 if (copy_from_user(page, buf, this_len)) {
789 retval = access_remote_vm(mm, dst, page, this_len, 1);
802 free_page((unsigned long) page);
806 loff_t mem_lseek(struct file *file, loff_t offset, int orig)
810 file->f_pos = offset;
813 file->f_pos += offset;
818 force_successful_syscall_return();
822 static int mem_release(struct inode *inode, struct file *file)
824 struct mm_struct *mm = file->private_data;
830 static const struct file_operations proc_mem_operations = {
835 .release = mem_release,
838 static ssize_t environ_read(struct file *file, char __user *buf,
839 size_t count, loff_t *ppos)
841 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
843 unsigned long src = *ppos;
845 struct mm_struct *mm;
851 page = (char *)__get_free_page(GFP_TEMPORARY);
856 mm = mm_for_maps(task);
858 if (!mm || IS_ERR(mm))
863 int this_len, retval, max_len;
865 this_len = mm->env_end - (mm->env_start + src);
870 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
871 this_len = (this_len > max_len) ? max_len : this_len;
873 retval = access_process_vm(task, (mm->env_start + src),
881 if (copy_to_user(buf, page, retval)) {
895 free_page((unsigned long) page);
897 put_task_struct(task);
902 static const struct file_operations proc_environ_operations = {
903 .read = environ_read,
904 .llseek = generic_file_llseek,
907 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
908 size_t count, loff_t *ppos)
910 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
911 char buffer[PROC_NUMBUF];
913 int oom_adjust = OOM_DISABLE;
919 if (lock_task_sighand(task, &flags)) {
920 oom_adjust = task->signal->oom_adj;
921 unlock_task_sighand(task, &flags);
924 put_task_struct(task);
926 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
928 return simple_read_from_buffer(buf, count, ppos, buffer, len);
931 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
932 size_t count, loff_t *ppos)
934 struct task_struct *task;
935 char buffer[PROC_NUMBUF];
940 memset(buffer, 0, sizeof(buffer));
941 if (count > sizeof(buffer) - 1)
942 count = sizeof(buffer) - 1;
943 if (copy_from_user(buffer, buf, count)) {
948 err = kstrtoint(strstrip(buffer), 0, &oom_adjust);
951 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
952 oom_adjust != OOM_DISABLE) {
957 task = get_proc_task(file->f_path.dentry->d_inode);
969 if (!lock_task_sighand(task, &flags)) {
974 if (oom_adjust < task->signal->oom_adj && !capable(CAP_SYS_RESOURCE)) {
980 * Warn that /proc/pid/oom_adj is deprecated, see
981 * Documentation/feature-removal-schedule.txt.
983 printk_once(KERN_WARNING "%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
984 current->comm, task_pid_nr(current), task_pid_nr(task),
986 task->signal->oom_adj = oom_adjust;
988 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
989 * value is always attainable.
991 if (task->signal->oom_adj == OOM_ADJUST_MAX)
992 task->signal->oom_score_adj = OOM_SCORE_ADJ_MAX;
994 task->signal->oom_score_adj = (oom_adjust * OOM_SCORE_ADJ_MAX) /
996 trace_oom_score_adj_update(task);
998 unlock_task_sighand(task, &flags);
1001 put_task_struct(task);
1003 return err < 0 ? err : count;
1006 static const struct file_operations proc_oom_adjust_operations = {
1007 .read = oom_adjust_read,
1008 .write = oom_adjust_write,
1009 .llseek = generic_file_llseek,
1012 static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1013 size_t count, loff_t *ppos)
1015 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
1016 char buffer[PROC_NUMBUF];
1017 int oom_score_adj = OOM_SCORE_ADJ_MIN;
1018 unsigned long flags;
1023 if (lock_task_sighand(task, &flags)) {
1024 oom_score_adj = task->signal->oom_score_adj;
1025 unlock_task_sighand(task, &flags);
1027 put_task_struct(task);
1028 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_score_adj);
1029 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1032 static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1033 size_t count, loff_t *ppos)
1035 struct task_struct *task;
1036 char buffer[PROC_NUMBUF];
1037 unsigned long flags;
1041 memset(buffer, 0, sizeof(buffer));
1042 if (count > sizeof(buffer) - 1)
1043 count = sizeof(buffer) - 1;
1044 if (copy_from_user(buffer, buf, count)) {
1049 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
1052 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
1053 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1058 task = get_proc_task(file->f_path.dentry->d_inode);
1070 if (!lock_task_sighand(task, &flags)) {
1075 if (oom_score_adj < task->signal->oom_score_adj_min &&
1076 !capable(CAP_SYS_RESOURCE)) {
1081 task->signal->oom_score_adj = oom_score_adj;
1082 if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
1083 task->signal->oom_score_adj_min = oom_score_adj;
1084 trace_oom_score_adj_update(task);
1086 * Scale /proc/pid/oom_adj appropriately ensuring that OOM_DISABLE is
1087 * always attainable.
1089 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MIN)
1090 task->signal->oom_adj = OOM_DISABLE;
1092 task->signal->oom_adj = (oom_score_adj * OOM_ADJUST_MAX) /
1095 unlock_task_sighand(task, &flags);
1098 put_task_struct(task);
1100 return err < 0 ? err : count;
1103 static const struct file_operations proc_oom_score_adj_operations = {
1104 .read = oom_score_adj_read,
1105 .write = oom_score_adj_write,
1106 .llseek = default_llseek,
1109 #ifdef CONFIG_AUDITSYSCALL
1110 #define TMPBUFLEN 21
1111 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1112 size_t count, loff_t *ppos)
1114 struct inode * inode = file->f_path.dentry->d_inode;
1115 struct task_struct *task = get_proc_task(inode);
1117 char tmpbuf[TMPBUFLEN];
1121 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1122 audit_get_loginuid(task));
1123 put_task_struct(task);
1124 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1127 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1128 size_t count, loff_t *ppos)
1130 struct inode * inode = file->f_path.dentry->d_inode;
1136 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1142 if (count >= PAGE_SIZE)
1143 count = PAGE_SIZE - 1;
1146 /* No partial writes. */
1149 page = (char*)__get_free_page(GFP_TEMPORARY);
1153 if (copy_from_user(page, buf, count))
1157 loginuid = simple_strtoul(page, &tmp, 10);
1163 length = audit_set_loginuid(loginuid);
1164 if (likely(length == 0))
1168 free_page((unsigned long) page);
1172 static const struct file_operations proc_loginuid_operations = {
1173 .read = proc_loginuid_read,
1174 .write = proc_loginuid_write,
1175 .llseek = generic_file_llseek,
1178 static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1179 size_t count, loff_t *ppos)
1181 struct inode * inode = file->f_path.dentry->d_inode;
1182 struct task_struct *task = get_proc_task(inode);
1184 char tmpbuf[TMPBUFLEN];
1188 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1189 audit_get_sessionid(task));
1190 put_task_struct(task);
1191 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1194 static const struct file_operations proc_sessionid_operations = {
1195 .read = proc_sessionid_read,
1196 .llseek = generic_file_llseek,
1200 #ifdef CONFIG_FAULT_INJECTION
1201 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1202 size_t count, loff_t *ppos)
1204 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1205 char buffer[PROC_NUMBUF];
1211 make_it_fail = task->make_it_fail;
1212 put_task_struct(task);
1214 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1216 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1219 static ssize_t proc_fault_inject_write(struct file * file,
1220 const char __user * buf, size_t count, loff_t *ppos)
1222 struct task_struct *task;
1223 char buffer[PROC_NUMBUF], *end;
1226 if (!capable(CAP_SYS_RESOURCE))
1228 memset(buffer, 0, sizeof(buffer));
1229 if (count > sizeof(buffer) - 1)
1230 count = sizeof(buffer) - 1;
1231 if (copy_from_user(buffer, buf, count))
1233 make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1236 task = get_proc_task(file->f_dentry->d_inode);
1239 task->make_it_fail = make_it_fail;
1240 put_task_struct(task);
1245 static const struct file_operations proc_fault_inject_operations = {
1246 .read = proc_fault_inject_read,
1247 .write = proc_fault_inject_write,
1248 .llseek = generic_file_llseek,
1253 #ifdef CONFIG_SCHED_DEBUG
1255 * Print out various scheduling related per-task fields:
1257 static int sched_show(struct seq_file *m, void *v)
1259 struct inode *inode = m->private;
1260 struct task_struct *p;
1262 p = get_proc_task(inode);
1265 proc_sched_show_task(p, m);
1273 sched_write(struct file *file, const char __user *buf,
1274 size_t count, loff_t *offset)
1276 struct inode *inode = file->f_path.dentry->d_inode;
1277 struct task_struct *p;
1279 p = get_proc_task(inode);
1282 proc_sched_set_task(p);
1289 static int sched_open(struct inode *inode, struct file *filp)
1291 return single_open(filp, sched_show, inode);
1294 static const struct file_operations proc_pid_sched_operations = {
1297 .write = sched_write,
1298 .llseek = seq_lseek,
1299 .release = single_release,
1304 #ifdef CONFIG_SCHED_AUTOGROUP
1306 * Print out autogroup related information:
1308 static int sched_autogroup_show(struct seq_file *m, void *v)
1310 struct inode *inode = m->private;
1311 struct task_struct *p;
1313 p = get_proc_task(inode);
1316 proc_sched_autogroup_show_task(p, m);
1324 sched_autogroup_write(struct file *file, const char __user *buf,
1325 size_t count, loff_t *offset)
1327 struct inode *inode = file->f_path.dentry->d_inode;
1328 struct task_struct *p;
1329 char buffer[PROC_NUMBUF];
1333 memset(buffer, 0, sizeof(buffer));
1334 if (count > sizeof(buffer) - 1)
1335 count = sizeof(buffer) - 1;
1336 if (copy_from_user(buffer, buf, count))
1339 err = kstrtoint(strstrip(buffer), 0, &nice);
1343 p = get_proc_task(inode);
1348 err = proc_sched_autogroup_set_nice(p, &err);
1357 static int sched_autogroup_open(struct inode *inode, struct file *filp)
1361 ret = single_open(filp, sched_autogroup_show, NULL);
1363 struct seq_file *m = filp->private_data;
1370 static const struct file_operations proc_pid_sched_autogroup_operations = {
1371 .open = sched_autogroup_open,
1373 .write = sched_autogroup_write,
1374 .llseek = seq_lseek,
1375 .release = single_release,
1378 #endif /* CONFIG_SCHED_AUTOGROUP */
1380 static ssize_t comm_write(struct file *file, const char __user *buf,
1381 size_t count, loff_t *offset)
1383 struct inode *inode = file->f_path.dentry->d_inode;
1384 struct task_struct *p;
1385 char buffer[TASK_COMM_LEN];
1387 memset(buffer, 0, sizeof(buffer));
1388 if (count > sizeof(buffer) - 1)
1389 count = sizeof(buffer) - 1;
1390 if (copy_from_user(buffer, buf, count))
1393 p = get_proc_task(inode);
1397 if (same_thread_group(current, p))
1398 set_task_comm(p, buffer);
1407 static int comm_show(struct seq_file *m, void *v)
1409 struct inode *inode = m->private;
1410 struct task_struct *p;
1412 p = get_proc_task(inode);
1417 seq_printf(m, "%s\n", p->comm);
1425 static int comm_open(struct inode *inode, struct file *filp)
1427 return single_open(filp, comm_show, inode);
1430 static const struct file_operations proc_pid_set_comm_operations = {
1433 .write = comm_write,
1434 .llseek = seq_lseek,
1435 .release = single_release,
1438 static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
1440 struct task_struct *task;
1441 struct mm_struct *mm;
1442 struct file *exe_file;
1444 task = get_proc_task(dentry->d_inode);
1447 mm = get_task_mm(task);
1448 put_task_struct(task);
1451 exe_file = get_mm_exe_file(mm);
1454 *exe_path = exe_file->f_path;
1455 path_get(&exe_file->f_path);
1462 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1464 struct inode *inode = dentry->d_inode;
1465 int error = -EACCES;
1467 /* We don't need a base pointer in the /proc filesystem */
1468 path_put(&nd->path);
1470 /* Are we allowed to snoop on the tasks file descriptors? */
1471 if (!proc_fd_access_allowed(inode))
1474 error = PROC_I(inode)->op.proc_get_link(dentry, &nd->path);
1476 return ERR_PTR(error);
1479 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1481 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
1488 pathname = d_path(path, tmp, PAGE_SIZE);
1489 len = PTR_ERR(pathname);
1490 if (IS_ERR(pathname))
1492 len = tmp + PAGE_SIZE - 1 - pathname;
1496 if (copy_to_user(buffer, pathname, len))
1499 free_page((unsigned long)tmp);
1503 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1505 int error = -EACCES;
1506 struct inode *inode = dentry->d_inode;
1509 /* Are we allowed to snoop on the tasks file descriptors? */
1510 if (!proc_fd_access_allowed(inode))
1513 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1517 error = do_proc_readlink(&path, buffer, buflen);
1523 static const struct inode_operations proc_pid_link_inode_operations = {
1524 .readlink = proc_pid_readlink,
1525 .follow_link = proc_pid_follow_link,
1526 .setattr = proc_setattr,
1530 /* building an inode */
1532 static int task_dumpable(struct task_struct *task)
1535 struct mm_struct *mm;
1540 dumpable = get_dumpable(mm);
1547 struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1549 struct inode * inode;
1550 struct proc_inode *ei;
1551 const struct cred *cred;
1553 /* We need a new inode */
1555 inode = new_inode(sb);
1561 inode->i_ino = get_next_ino();
1562 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1563 inode->i_op = &proc_def_inode_operations;
1566 * grab the reference to task.
1568 ei->pid = get_task_pid(task, PIDTYPE_PID);
1572 if (task_dumpable(task)) {
1574 cred = __task_cred(task);
1575 inode->i_uid = cred->euid;
1576 inode->i_gid = cred->egid;
1579 security_task_to_inode(task, inode);
1589 int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1591 struct inode *inode = dentry->d_inode;
1592 struct task_struct *task;
1593 const struct cred *cred;
1594 struct pid_namespace *pid = dentry->d_sb->s_fs_info;
1596 generic_fillattr(inode, stat);
1601 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1603 if (!has_pid_permissions(pid, task, 2)) {
1606 * This doesn't prevent learning whether PID exists,
1607 * it only makes getattr() consistent with readdir().
1611 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1612 task_dumpable(task)) {
1613 cred = __task_cred(task);
1614 stat->uid = cred->euid;
1615 stat->gid = cred->egid;
1625 * Exceptional case: normally we are not allowed to unhash a busy
1626 * directory. In this case, however, we can do it - no aliasing problems
1627 * due to the way we treat inodes.
1629 * Rewrite the inode's ownerships here because the owning task may have
1630 * performed a setuid(), etc.
1632 * Before the /proc/pid/status file was created the only way to read
1633 * the effective uid of a /process was to stat /proc/pid. Reading
1634 * /proc/pid/status is slow enough that procps and other packages
1635 * kept stating /proc/pid. To keep the rules in /proc simple I have
1636 * made this apply to all per process world readable and executable
1639 int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1641 struct inode *inode;
1642 struct task_struct *task;
1643 const struct cred *cred;
1645 if (nd && nd->flags & LOOKUP_RCU)
1648 inode = dentry->d_inode;
1649 task = get_proc_task(inode);
1652 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1653 task_dumpable(task)) {
1655 cred = __task_cred(task);
1656 inode->i_uid = cred->euid;
1657 inode->i_gid = cred->egid;
1663 inode->i_mode &= ~(S_ISUID | S_ISGID);
1664 security_task_to_inode(task, inode);
1665 put_task_struct(task);
1672 static int pid_delete_dentry(const struct dentry * dentry)
1674 /* Is the task we represent dead?
1675 * If so, then don't put the dentry on the lru list,
1676 * kill it immediately.
1678 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1681 const struct dentry_operations pid_dentry_operations =
1683 .d_revalidate = pid_revalidate,
1684 .d_delete = pid_delete_dentry,
1690 * Fill a directory entry.
1692 * If possible create the dcache entry and derive our inode number and
1693 * file type from dcache entry.
1695 * Since all of the proc inode numbers are dynamically generated, the inode
1696 * numbers do not exist until the inode is cache. This means creating the
1697 * the dcache entry in readdir is necessary to keep the inode numbers
1698 * reported by readdir in sync with the inode numbers reported
1701 int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1702 const char *name, int len,
1703 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1705 struct dentry *child, *dir = filp->f_path.dentry;
1706 struct inode *inode;
1709 unsigned type = DT_UNKNOWN;
1713 qname.hash = full_name_hash(name, len);
1715 child = d_lookup(dir, &qname);
1718 new = d_alloc(dir, &qname);
1720 child = instantiate(dir->d_inode, new, task, ptr);
1727 if (!child || IS_ERR(child) || !child->d_inode)
1728 goto end_instantiate;
1729 inode = child->d_inode;
1732 type = inode->i_mode >> 12;
1737 ino = find_inode_number(dir, &qname);
1740 return filldir(dirent, name, len, filp->f_pos, ino, type);
1743 static unsigned name_to_int(struct dentry *dentry)
1745 const char *name = dentry->d_name.name;
1746 int len = dentry->d_name.len;
1749 if (len > 1 && *name == '0')
1752 unsigned c = *name++ - '0';
1755 if (n >= (~0U-9)/10)
1765 #define PROC_FDINFO_MAX 64
1767 static int proc_fd_info(struct inode *inode, struct path *path, char *info)
1769 struct task_struct *task = get_proc_task(inode);
1770 struct files_struct *files = NULL;
1772 int fd = proc_fd(inode);
1775 files = get_files_struct(task);
1776 put_task_struct(task);
1780 * We are not taking a ref to the file structure, so we must
1783 spin_lock(&files->file_lock);
1784 file = fcheck_files(files, fd);
1786 unsigned int f_flags;
1787 struct fdtable *fdt;
1789 fdt = files_fdtable(files);
1790 f_flags = file->f_flags & ~O_CLOEXEC;
1791 if (FD_ISSET(fd, fdt->close_on_exec))
1792 f_flags |= O_CLOEXEC;
1795 *path = file->f_path;
1796 path_get(&file->f_path);
1799 snprintf(info, PROC_FDINFO_MAX,
1802 (long long) file->f_pos,
1804 spin_unlock(&files->file_lock);
1805 put_files_struct(files);
1808 spin_unlock(&files->file_lock);
1809 put_files_struct(files);
1814 static int proc_fd_link(struct dentry *dentry, struct path *path)
1816 return proc_fd_info(dentry->d_inode, path, NULL);
1819 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1821 struct inode *inode;
1822 struct task_struct *task;
1824 struct files_struct *files;
1825 const struct cred *cred;
1827 if (nd && nd->flags & LOOKUP_RCU)
1830 inode = dentry->d_inode;
1831 task = get_proc_task(inode);
1832 fd = proc_fd(inode);
1835 files = get_files_struct(task);
1838 if (fcheck_files(files, fd)) {
1840 put_files_struct(files);
1841 if (task_dumpable(task)) {
1843 cred = __task_cred(task);
1844 inode->i_uid = cred->euid;
1845 inode->i_gid = cred->egid;
1851 inode->i_mode &= ~(S_ISUID | S_ISGID);
1852 security_task_to_inode(task, inode);
1853 put_task_struct(task);
1857 put_files_struct(files);
1859 put_task_struct(task);
1865 static const struct dentry_operations tid_fd_dentry_operations =
1867 .d_revalidate = tid_fd_revalidate,
1868 .d_delete = pid_delete_dentry,
1871 static struct dentry *proc_fd_instantiate(struct inode *dir,
1872 struct dentry *dentry, struct task_struct *task, const void *ptr)
1874 unsigned fd = *(const unsigned *)ptr;
1876 struct files_struct *files;
1877 struct inode *inode;
1878 struct proc_inode *ei;
1879 struct dentry *error = ERR_PTR(-ENOENT);
1881 inode = proc_pid_make_inode(dir->i_sb, task);
1886 files = get_files_struct(task);
1889 inode->i_mode = S_IFLNK;
1892 * We are not taking a ref to the file structure, so we must
1895 spin_lock(&files->file_lock);
1896 file = fcheck_files(files, fd);
1899 if (file->f_mode & FMODE_READ)
1900 inode->i_mode |= S_IRUSR | S_IXUSR;
1901 if (file->f_mode & FMODE_WRITE)
1902 inode->i_mode |= S_IWUSR | S_IXUSR;
1903 spin_unlock(&files->file_lock);
1904 put_files_struct(files);
1906 inode->i_op = &proc_pid_link_inode_operations;
1908 ei->op.proc_get_link = proc_fd_link;
1909 d_set_d_op(dentry, &tid_fd_dentry_operations);
1910 d_add(dentry, inode);
1911 /* Close the race of the process dying before we return the dentry */
1912 if (tid_fd_revalidate(dentry, NULL))
1918 spin_unlock(&files->file_lock);
1919 put_files_struct(files);
1925 static struct dentry *proc_lookupfd_common(struct inode *dir,
1926 struct dentry *dentry,
1927 instantiate_t instantiate)
1929 struct task_struct *task = get_proc_task(dir);
1930 unsigned fd = name_to_int(dentry);
1931 struct dentry *result = ERR_PTR(-ENOENT);
1938 result = instantiate(dir, dentry, task, &fd);
1940 put_task_struct(task);
1945 static int proc_readfd_common(struct file * filp, void * dirent,
1946 filldir_t filldir, instantiate_t instantiate)
1948 struct dentry *dentry = filp->f_path.dentry;
1949 struct inode *inode = dentry->d_inode;
1950 struct task_struct *p = get_proc_task(inode);
1951 unsigned int fd, ino;
1953 struct files_struct * files;
1963 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1967 ino = parent_ino(dentry);
1968 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1972 files = get_files_struct(p);
1976 for (fd = filp->f_pos-2;
1977 fd < files_fdtable(files)->max_fds;
1978 fd++, filp->f_pos++) {
1979 char name[PROC_NUMBUF];
1982 if (!fcheck_files(files, fd))
1986 len = snprintf(name, sizeof(name), "%d", fd);
1987 if (proc_fill_cache(filp, dirent, filldir,
1988 name, len, instantiate,
1996 put_files_struct(files);
2004 static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
2005 struct nameidata *nd)
2007 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
2010 static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
2012 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
2015 static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
2016 size_t len, loff_t *ppos)
2018 char tmp[PROC_FDINFO_MAX];
2019 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
2021 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
2025 static const struct file_operations proc_fdinfo_file_operations = {
2026 .open = nonseekable_open,
2027 .read = proc_fdinfo_read,
2028 .llseek = no_llseek,
2031 static const struct file_operations proc_fd_operations = {
2032 .read = generic_read_dir,
2033 .readdir = proc_readfd,
2034 .llseek = default_llseek,
2037 #ifdef CONFIG_CHECKPOINT_RESTORE
2040 * dname_to_vma_addr - maps a dentry name into two unsigned longs
2041 * which represent vma start and end addresses.
2043 static int dname_to_vma_addr(struct dentry *dentry,
2044 unsigned long *start, unsigned long *end)
2046 if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2)
2052 static int map_files_d_revalidate(struct dentry *dentry, struct nameidata *nd)
2054 unsigned long vm_start, vm_end;
2055 bool exact_vma_exists = false;
2056 struct mm_struct *mm = NULL;
2057 struct task_struct *task;
2058 const struct cred *cred;
2059 struct inode *inode;
2062 if (nd && nd->flags & LOOKUP_RCU)
2065 if (!capable(CAP_SYS_ADMIN)) {
2070 inode = dentry->d_inode;
2071 task = get_proc_task(inode);
2075 if (!ptrace_may_access(task, PTRACE_MODE_READ))
2078 mm = get_task_mm(task);
2082 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
2083 down_read(&mm->mmap_sem);
2084 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
2085 up_read(&mm->mmap_sem);
2090 if (exact_vma_exists) {
2091 if (task_dumpable(task)) {
2093 cred = __task_cred(task);
2094 inode->i_uid = cred->euid;
2095 inode->i_gid = cred->egid;
2101 security_task_to_inode(task, inode);
2106 put_task_struct(task);
2115 static const struct dentry_operations tid_map_files_dentry_operations = {
2116 .d_revalidate = map_files_d_revalidate,
2117 .d_delete = pid_delete_dentry,
2120 static int proc_map_files_get_link(struct dentry *dentry, struct path *path)
2122 unsigned long vm_start, vm_end;
2123 struct vm_area_struct *vma;
2124 struct task_struct *task;
2125 struct mm_struct *mm;
2129 task = get_proc_task(dentry->d_inode);
2133 mm = get_task_mm(task);
2134 put_task_struct(task);
2138 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
2142 down_read(&mm->mmap_sem);
2143 vma = find_exact_vma(mm, vm_start, vm_end);
2144 if (vma && vma->vm_file) {
2145 *path = vma->vm_file->f_path;
2149 up_read(&mm->mmap_sem);
2157 struct map_files_info {
2160 unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
2163 static struct dentry *
2164 proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
2165 struct task_struct *task, const void *ptr)
2167 const struct file *file = ptr;
2168 struct proc_inode *ei;
2169 struct inode *inode;
2172 return ERR_PTR(-ENOENT);
2174 inode = proc_pid_make_inode(dir->i_sb, task);
2176 return ERR_PTR(-ENOENT);
2179 ei->op.proc_get_link = proc_map_files_get_link;
2181 inode->i_op = &proc_pid_link_inode_operations;
2183 inode->i_mode = S_IFLNK;
2185 if (file->f_mode & FMODE_READ)
2186 inode->i_mode |= S_IRUSR;
2187 if (file->f_mode & FMODE_WRITE)
2188 inode->i_mode |= S_IWUSR;
2190 d_set_d_op(dentry, &tid_map_files_dentry_operations);
2191 d_add(dentry, inode);
2196 static struct dentry *proc_map_files_lookup(struct inode *dir,
2197 struct dentry *dentry, struct nameidata *nd)
2199 unsigned long vm_start, vm_end;
2200 struct vm_area_struct *vma;
2201 struct task_struct *task;
2202 struct dentry *result;
2203 struct mm_struct *mm;
2205 result = ERR_PTR(-EACCES);
2206 if (!capable(CAP_SYS_ADMIN))
2209 result = ERR_PTR(-ENOENT);
2210 task = get_proc_task(dir);
2214 result = ERR_PTR(-EACCES);
2215 if (lock_trace(task))
2218 result = ERR_PTR(-ENOENT);
2219 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
2222 mm = get_task_mm(task);
2226 down_read(&mm->mmap_sem);
2227 vma = find_exact_vma(mm, vm_start, vm_end);
2231 result = proc_map_files_instantiate(dir, dentry, task, vma->vm_file);
2234 up_read(&mm->mmap_sem);
2239 put_task_struct(task);
2244 static const struct inode_operations proc_map_files_inode_operations = {
2245 .lookup = proc_map_files_lookup,
2246 .permission = proc_fd_permission,
2247 .setattr = proc_setattr,
2251 proc_map_files_readdir(struct file *filp, void *dirent, filldir_t filldir)
2253 struct dentry *dentry = filp->f_path.dentry;
2254 struct inode *inode = dentry->d_inode;
2255 struct vm_area_struct *vma;
2256 struct task_struct *task;
2257 struct mm_struct *mm;
2262 if (!capable(CAP_SYS_ADMIN))
2266 task = get_proc_task(inode);
2271 if (lock_trace(task))
2275 switch (filp->f_pos) {
2278 if (filldir(dirent, ".", 1, 0, ino, DT_DIR) < 0)
2282 ino = parent_ino(dentry);
2283 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
2288 unsigned long nr_files, pos, i;
2289 struct flex_array *fa = NULL;
2290 struct map_files_info info;
2291 struct map_files_info *p;
2293 mm = get_task_mm(task);
2296 down_read(&mm->mmap_sem);
2301 * We need two passes here:
2303 * 1) Collect vmas of mapped files with mmap_sem taken
2304 * 2) Release mmap_sem and instantiate entries
2306 * otherwise we get lockdep complained, since filldir()
2307 * routine might require mmap_sem taken in might_fault().
2310 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
2311 if (vma->vm_file && ++pos > filp->f_pos)
2316 fa = flex_array_alloc(sizeof(info), nr_files,
2318 if (!fa || flex_array_prealloc(fa, 0, nr_files,
2322 flex_array_free(fa);
2323 up_read(&mm->mmap_sem);
2327 for (i = 0, vma = mm->mmap, pos = 2; vma;
2328 vma = vma->vm_next) {
2331 if (++pos <= filp->f_pos)
2334 get_file(vma->vm_file);
2335 info.file = vma->vm_file;
2336 info.len = snprintf(info.name,
2337 sizeof(info.name), "%lx-%lx",
2338 vma->vm_start, vma->vm_end);
2339 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
2343 up_read(&mm->mmap_sem);
2345 for (i = 0; i < nr_files; i++) {
2346 p = flex_array_get(fa, i);
2347 ret = proc_fill_cache(filp, dirent, filldir,
2349 proc_map_files_instantiate,
2356 for (; i < nr_files; i++) {
2358 * In case of error don't forget
2359 * to put rest of file refs.
2361 p = flex_array_get(fa, i);
2365 flex_array_free(fa);
2373 put_task_struct(task);
2378 static const struct file_operations proc_map_files_operations = {
2379 .read = generic_read_dir,
2380 .readdir = proc_map_files_readdir,
2381 .llseek = default_llseek,
2384 #endif /* CONFIG_CHECKPOINT_RESTORE */
2387 * /proc/pid/fd needs a special permission handler so that a process can still
2388 * access /proc/self/fd after it has executed a setuid().
2390 static int proc_fd_permission(struct inode *inode, int mask)
2392 int rv = generic_permission(inode, mask);
2395 if (task_pid(current) == proc_pid(inode))
2401 * proc directories can do almost nothing..
2403 static const struct inode_operations proc_fd_inode_operations = {
2404 .lookup = proc_lookupfd,
2405 .permission = proc_fd_permission,
2406 .setattr = proc_setattr,
2409 static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
2410 struct dentry *dentry, struct task_struct *task, const void *ptr)
2412 unsigned fd = *(unsigned *)ptr;
2413 struct inode *inode;
2414 struct proc_inode *ei;
2415 struct dentry *error = ERR_PTR(-ENOENT);
2417 inode = proc_pid_make_inode(dir->i_sb, task);
2422 inode->i_mode = S_IFREG | S_IRUSR;
2423 inode->i_fop = &proc_fdinfo_file_operations;
2424 d_set_d_op(dentry, &tid_fd_dentry_operations);
2425 d_add(dentry, inode);
2426 /* Close the race of the process dying before we return the dentry */
2427 if (tid_fd_revalidate(dentry, NULL))
2434 static struct dentry *proc_lookupfdinfo(struct inode *dir,
2435 struct dentry *dentry,
2436 struct nameidata *nd)
2438 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
2441 static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
2443 return proc_readfd_common(filp, dirent, filldir,
2444 proc_fdinfo_instantiate);
2447 static const struct file_operations proc_fdinfo_operations = {
2448 .read = generic_read_dir,
2449 .readdir = proc_readfdinfo,
2450 .llseek = default_llseek,
2454 * proc directories can do almost nothing..
2456 static const struct inode_operations proc_fdinfo_inode_operations = {
2457 .lookup = proc_lookupfdinfo,
2458 .setattr = proc_setattr,
2462 static struct dentry *proc_pident_instantiate(struct inode *dir,
2463 struct dentry *dentry, struct task_struct *task, const void *ptr)
2465 const struct pid_entry *p = ptr;
2466 struct inode *inode;
2467 struct proc_inode *ei;
2468 struct dentry *error = ERR_PTR(-ENOENT);
2470 inode = proc_pid_make_inode(dir->i_sb, task);
2475 inode->i_mode = p->mode;
2476 if (S_ISDIR(inode->i_mode))
2477 set_nlink(inode, 2); /* Use getattr to fix if necessary */
2479 inode->i_op = p->iop;
2481 inode->i_fop = p->fop;
2483 d_set_d_op(dentry, &pid_dentry_operations);
2484 d_add(dentry, inode);
2485 /* Close the race of the process dying before we return the dentry */
2486 if (pid_revalidate(dentry, NULL))
2492 static struct dentry *proc_pident_lookup(struct inode *dir,
2493 struct dentry *dentry,
2494 const struct pid_entry *ents,
2497 struct dentry *error;
2498 struct task_struct *task = get_proc_task(dir);
2499 const struct pid_entry *p, *last;
2501 error = ERR_PTR(-ENOENT);
2507 * Yes, it does not scale. And it should not. Don't add
2508 * new entries into /proc/<tgid>/ without very good reasons.
2510 last = &ents[nents - 1];
2511 for (p = ents; p <= last; p++) {
2512 if (p->len != dentry->d_name.len)
2514 if (!memcmp(dentry->d_name.name, p->name, p->len))
2520 error = proc_pident_instantiate(dir, dentry, task, p);
2522 put_task_struct(task);
2527 static int proc_pident_fill_cache(struct file *filp, void *dirent,
2528 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2530 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2531 proc_pident_instantiate, task, p);
2534 static int proc_pident_readdir(struct file *filp,
2535 void *dirent, filldir_t filldir,
2536 const struct pid_entry *ents, unsigned int nents)
2539 struct dentry *dentry = filp->f_path.dentry;
2540 struct inode *inode = dentry->d_inode;
2541 struct task_struct *task = get_proc_task(inode);
2542 const struct pid_entry *p, *last;
2555 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
2561 ino = parent_ino(dentry);
2562 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
2574 last = &ents[nents - 1];
2576 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
2585 put_task_struct(task);
2590 #ifdef CONFIG_SECURITY
2591 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2592 size_t count, loff_t *ppos)
2594 struct inode * inode = file->f_path.dentry->d_inode;
2597 struct task_struct *task = get_proc_task(inode);
2602 length = security_getprocattr(task,
2603 (char*)file->f_path.dentry->d_name.name,
2605 put_task_struct(task);
2607 length = simple_read_from_buffer(buf, count, ppos, p, length);
2612 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2613 size_t count, loff_t *ppos)
2615 struct inode * inode = file->f_path.dentry->d_inode;
2618 struct task_struct *task = get_proc_task(inode);
2623 if (count > PAGE_SIZE)
2626 /* No partial writes. */
2632 page = (char*)__get_free_page(GFP_TEMPORARY);
2637 if (copy_from_user(page, buf, count))
2640 /* Guard against adverse ptrace interaction */
2641 length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
2645 length = security_setprocattr(task,
2646 (char*)file->f_path.dentry->d_name.name,
2647 (void*)page, count);
2648 mutex_unlock(&task->signal->cred_guard_mutex);
2650 free_page((unsigned long) page);
2652 put_task_struct(task);
2657 static const struct file_operations proc_pid_attr_operations = {
2658 .read = proc_pid_attr_read,
2659 .write = proc_pid_attr_write,
2660 .llseek = generic_file_llseek,
2663 static const struct pid_entry attr_dir_stuff[] = {
2664 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2665 REG("prev", S_IRUGO, proc_pid_attr_operations),
2666 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2667 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2668 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2669 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2672 static int proc_attr_dir_readdir(struct file * filp,
2673 void * dirent, filldir_t filldir)
2675 return proc_pident_readdir(filp,dirent,filldir,
2676 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
2679 static const struct file_operations proc_attr_dir_operations = {
2680 .read = generic_read_dir,
2681 .readdir = proc_attr_dir_readdir,
2682 .llseek = default_llseek,
2685 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2686 struct dentry *dentry, struct nameidata *nd)
2688 return proc_pident_lookup(dir, dentry,
2689 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2692 static const struct inode_operations proc_attr_dir_inode_operations = {
2693 .lookup = proc_attr_dir_lookup,
2694 .getattr = pid_getattr,
2695 .setattr = proc_setattr,
2700 #ifdef CONFIG_ELF_CORE
2701 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2702 size_t count, loff_t *ppos)
2704 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2705 struct mm_struct *mm;
2706 char buffer[PROC_NUMBUF];
2714 mm = get_task_mm(task);
2716 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2717 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2718 MMF_DUMP_FILTER_SHIFT));
2720 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2723 put_task_struct(task);
2728 static ssize_t proc_coredump_filter_write(struct file *file,
2729 const char __user *buf,
2733 struct task_struct *task;
2734 struct mm_struct *mm;
2735 char buffer[PROC_NUMBUF], *end;
2742 memset(buffer, 0, sizeof(buffer));
2743 if (count > sizeof(buffer) - 1)
2744 count = sizeof(buffer) - 1;
2745 if (copy_from_user(buffer, buf, count))
2749 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2752 if (end - buffer == 0)
2756 task = get_proc_task(file->f_dentry->d_inode);
2761 mm = get_task_mm(task);
2765 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2767 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2769 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2774 put_task_struct(task);
2779 static const struct file_operations proc_coredump_filter_operations = {
2780 .read = proc_coredump_filter_read,
2781 .write = proc_coredump_filter_write,
2782 .llseek = generic_file_llseek,
2789 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2792 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2793 pid_t tgid = task_tgid_nr_ns(current, ns);
2794 char tmp[PROC_NUMBUF];
2797 sprintf(tmp, "%d", tgid);
2798 return vfs_readlink(dentry,buffer,buflen,tmp);
2801 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
2803 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2804 pid_t tgid = task_tgid_nr_ns(current, ns);
2805 char *name = ERR_PTR(-ENOENT);
2809 name = ERR_PTR(-ENOMEM);
2811 sprintf(name, "%d", tgid);
2813 nd_set_link(nd, name);
2817 static void proc_self_put_link(struct dentry *dentry, struct nameidata *nd,
2820 char *s = nd_get_link(nd);
2825 static const struct inode_operations proc_self_inode_operations = {
2826 .readlink = proc_self_readlink,
2827 .follow_link = proc_self_follow_link,
2828 .put_link = proc_self_put_link,
2834 * These are the directory entries in the root directory of /proc
2835 * that properly belong to the /proc filesystem, as they describe
2836 * describe something that is process related.
2838 static const struct pid_entry proc_base_stuff[] = {
2839 NOD("self", S_IFLNK|S_IRWXUGO,
2840 &proc_self_inode_operations, NULL, {}),
2843 static struct dentry *proc_base_instantiate(struct inode *dir,
2844 struct dentry *dentry, struct task_struct *task, const void *ptr)
2846 const struct pid_entry *p = ptr;
2847 struct inode *inode;
2848 struct proc_inode *ei;
2849 struct dentry *error;
2851 /* Allocate the inode */
2852 error = ERR_PTR(-ENOMEM);
2853 inode = new_inode(dir->i_sb);
2857 /* Initialize the inode */
2859 inode->i_ino = get_next_ino();
2860 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2863 * grab the reference to the task.
2865 ei->pid = get_task_pid(task, PIDTYPE_PID);
2869 inode->i_mode = p->mode;
2870 if (S_ISDIR(inode->i_mode))
2871 set_nlink(inode, 2);
2872 if (S_ISLNK(inode->i_mode))
2875 inode->i_op = p->iop;
2877 inode->i_fop = p->fop;
2879 d_add(dentry, inode);
2888 static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2890 struct dentry *error;
2891 struct task_struct *task = get_proc_task(dir);
2892 const struct pid_entry *p, *last;
2894 error = ERR_PTR(-ENOENT);
2899 /* Lookup the directory entry */
2900 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2901 for (p = proc_base_stuff; p <= last; p++) {
2902 if (p->len != dentry->d_name.len)
2904 if (!memcmp(dentry->d_name.name, p->name, p->len))
2910 error = proc_base_instantiate(dir, dentry, task, p);
2913 put_task_struct(task);
2918 static int proc_base_fill_cache(struct file *filp, void *dirent,
2919 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2921 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2922 proc_base_instantiate, task, p);
2925 #ifdef CONFIG_TASK_IO_ACCOUNTING
2926 static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
2928 struct task_io_accounting acct = task->ioac;
2929 unsigned long flags;
2932 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2936 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
2941 if (whole && lock_task_sighand(task, &flags)) {
2942 struct task_struct *t = task;
2944 task_io_accounting_add(&acct, &task->signal->ioac);
2945 while_each_thread(task, t)
2946 task_io_accounting_add(&acct, &t->ioac);
2948 unlock_task_sighand(task, &flags);
2950 result = sprintf(buffer,
2955 "read_bytes: %llu\n"
2956 "write_bytes: %llu\n"
2957 "cancelled_write_bytes: %llu\n",
2958 (unsigned long long)acct.rchar,
2959 (unsigned long long)acct.wchar,
2960 (unsigned long long)acct.syscr,
2961 (unsigned long long)acct.syscw,
2962 (unsigned long long)acct.read_bytes,
2963 (unsigned long long)acct.write_bytes,
2964 (unsigned long long)acct.cancelled_write_bytes);
2966 mutex_unlock(&task->signal->cred_guard_mutex);
2970 static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2972 return do_io_accounting(task, buffer, 0);
2975 static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2977 return do_io_accounting(task, buffer, 1);
2979 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2981 static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2982 struct pid *pid, struct task_struct *task)
2984 int err = lock_trace(task);
2986 seq_printf(m, "%08x\n", task->personality);
2995 static const struct file_operations proc_task_operations;
2996 static const struct inode_operations proc_task_inode_operations;
2998 static const struct pid_entry tgid_base_stuff[] = {
2999 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
3000 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
3001 #ifdef CONFIG_CHECKPOINT_RESTORE
3002 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
3004 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
3005 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
3007 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
3009 REG("environ", S_IRUSR, proc_environ_operations),
3010 INF("auxv", S_IRUSR, proc_pid_auxv),
3011 ONE("status", S_IRUGO, proc_pid_status),
3012 ONE("personality", S_IRUGO, proc_pid_personality),
3013 INF("limits", S_IRUGO, proc_pid_limits),
3014 #ifdef CONFIG_SCHED_DEBUG
3015 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
3017 #ifdef CONFIG_SCHED_AUTOGROUP
3018 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
3020 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
3021 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3022 INF("syscall", S_IRUGO, proc_pid_syscall),
3024 INF("cmdline", S_IRUGO, proc_pid_cmdline),
3025 ONE("stat", S_IRUGO, proc_tgid_stat),
3026 ONE("statm", S_IRUGO, proc_pid_statm),
3027 REG("maps", S_IRUGO, proc_maps_operations),
3029 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
3031 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3032 LNK("cwd", proc_cwd_link),
3033 LNK("root", proc_root_link),
3034 LNK("exe", proc_exe_link),
3035 REG("mounts", S_IRUGO, proc_mounts_operations),
3036 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
3037 REG("mountstats", S_IRUSR, proc_mountstats_operations),
3038 #ifdef CONFIG_PROC_PAGE_MONITOR
3039 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3040 REG("smaps", S_IRUGO, proc_smaps_operations),
3041 REG("pagemap", S_IRUGO, proc_pagemap_operations),
3043 #ifdef CONFIG_SECURITY
3044 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
3046 #ifdef CONFIG_KALLSYMS
3047 INF("wchan", S_IRUGO, proc_pid_wchan),
3049 #ifdef CONFIG_STACKTRACE
3050 ONE("stack", S_IRUGO, proc_pid_stack),
3052 #ifdef CONFIG_SCHEDSTATS
3053 INF("schedstat", S_IRUGO, proc_pid_schedstat),
3055 #ifdef CONFIG_LATENCYTOP
3056 REG("latency", S_IRUGO, proc_lstats_operations),
3058 #ifdef CONFIG_PROC_PID_CPUSET
3059 REG("cpuset", S_IRUGO, proc_cpuset_operations),
3061 #ifdef CONFIG_CGROUPS
3062 REG("cgroup", S_IRUGO, proc_cgroup_operations),
3064 INF("oom_score", S_IRUGO, proc_oom_score),
3065 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
3066 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
3067 #ifdef CONFIG_AUDITSYSCALL
3068 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3069 REG("sessionid", S_IRUGO, proc_sessionid_operations),
3071 #ifdef CONFIG_FAULT_INJECTION
3072 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3074 #ifdef CONFIG_ELF_CORE
3075 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
3077 #ifdef CONFIG_TASK_IO_ACCOUNTING
3078 INF("io", S_IRUSR, proc_tgid_io_accounting),
3080 #ifdef CONFIG_HARDWALL
3081 INF("hardwall", S_IRUGO, proc_pid_hardwall),
3085 static int proc_tgid_base_readdir(struct file * filp,
3086 void * dirent, filldir_t filldir)
3088 return proc_pident_readdir(filp,dirent,filldir,
3089 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
3092 static const struct file_operations proc_tgid_base_operations = {
3093 .read = generic_read_dir,
3094 .readdir = proc_tgid_base_readdir,
3095 .llseek = default_llseek,
3098 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
3099 return proc_pident_lookup(dir, dentry,
3100 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
3103 static const struct inode_operations proc_tgid_base_inode_operations = {
3104 .lookup = proc_tgid_base_lookup,
3105 .getattr = pid_getattr,
3106 .setattr = proc_setattr,
3107 .permission = proc_pid_permission,
3110 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
3112 struct dentry *dentry, *leader, *dir;
3113 char buf[PROC_NUMBUF];
3117 name.len = snprintf(buf, sizeof(buf), "%d", pid);
3118 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
3120 shrink_dcache_parent(dentry);
3126 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
3127 leader = d_hash_and_lookup(mnt->mnt_root, &name);
3132 name.len = strlen(name.name);
3133 dir = d_hash_and_lookup(leader, &name);
3135 goto out_put_leader;
3138 name.len = snprintf(buf, sizeof(buf), "%d", pid);
3139 dentry = d_hash_and_lookup(dir, &name);
3141 shrink_dcache_parent(dentry);
3154 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
3155 * @task: task that should be flushed.
3157 * When flushing dentries from proc, one needs to flush them from global
3158 * proc (proc_mnt) and from all the namespaces' procs this task was seen
3159 * in. This call is supposed to do all of this job.
3161 * Looks in the dcache for
3163 * /proc/@tgid/task/@pid
3164 * if either directory is present flushes it and all of it'ts children
3167 * It is safe and reasonable to cache /proc entries for a task until
3168 * that task exits. After that they just clog up the dcache with
3169 * useless entries, possibly causing useful dcache entries to be
3170 * flushed instead. This routine is proved to flush those useless
3171 * dcache entries at process exit time.
3173 * NOTE: This routine is just an optimization so it does not guarantee
3174 * that no dcache entries will exist at process exit time it
3175 * just makes it very unlikely that any will persist.
3178 void proc_flush_task(struct task_struct *task)
3181 struct pid *pid, *tgid;
3184 pid = task_pid(task);
3185 tgid = task_tgid(task);
3187 for (i = 0; i <= pid->level; i++) {
3188 upid = &pid->numbers[i];
3189 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
3190 tgid->numbers[i].nr);
3193 upid = &pid->numbers[pid->level];
3195 pid_ns_release_proc(upid->ns);
3198 static struct dentry *proc_pid_instantiate(struct inode *dir,
3199 struct dentry * dentry,
3200 struct task_struct *task, const void *ptr)
3202 struct dentry *error = ERR_PTR(-ENOENT);
3203 struct inode *inode;
3205 inode = proc_pid_make_inode(dir->i_sb, task);
3209 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3210 inode->i_op = &proc_tgid_base_inode_operations;
3211 inode->i_fop = &proc_tgid_base_operations;
3212 inode->i_flags|=S_IMMUTABLE;
3214 set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
3215 ARRAY_SIZE(tgid_base_stuff)));
3217 d_set_d_op(dentry, &pid_dentry_operations);
3219 d_add(dentry, inode);
3220 /* Close the race of the process dying before we return the dentry */
3221 if (pid_revalidate(dentry, NULL))
3227 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
3229 struct dentry *result;
3230 struct task_struct *task;
3232 struct pid_namespace *ns;
3234 result = proc_base_lookup(dir, dentry);
3235 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
3238 tgid = name_to_int(dentry);
3242 ns = dentry->d_sb->s_fs_info;
3244 task = find_task_by_pid_ns(tgid, ns);
3246 get_task_struct(task);
3251 result = proc_pid_instantiate(dir, dentry, task, NULL);
3252 put_task_struct(task);
3258 * Find the first task with tgid >= tgid
3263 struct task_struct *task;
3265 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3270 put_task_struct(iter.task);
3274 pid = find_ge_pid(iter.tgid, ns);
3276 iter.tgid = pid_nr_ns(pid, ns);
3277 iter.task = pid_task(pid, PIDTYPE_PID);
3278 /* What we to know is if the pid we have find is the
3279 * pid of a thread_group_leader. Testing for task
3280 * being a thread_group_leader is the obvious thing
3281 * todo but there is a window when it fails, due to
3282 * the pid transfer logic in de_thread.
3284 * So we perform the straight forward test of seeing
3285 * if the pid we have found is the pid of a thread
3286 * group leader, and don't worry if the task we have
3287 * found doesn't happen to be a thread group leader.
3288 * As we don't care in the case of readdir.
3290 if (!iter.task || !has_group_leader_pid(iter.task)) {
3294 get_task_struct(iter.task);
3300 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
3302 static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3303 struct tgid_iter iter)
3305 char name[PROC_NUMBUF];
3306 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
3307 return proc_fill_cache(filp, dirent, filldir, name, len,
3308 proc_pid_instantiate, iter.task, NULL);
3311 static int fake_filldir(void *buf, const char *name, int namelen,
3312 loff_t offset, u64 ino, unsigned d_type)
3317 /* for the /proc/ directory itself, after non-process stuff has been done */
3318 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
3321 struct task_struct *reaper;
3322 struct tgid_iter iter;
3323 struct pid_namespace *ns;
3324 filldir_t __filldir;
3326 if (filp->f_pos >= PID_MAX_LIMIT + TGID_OFFSET)
3328 nr = filp->f_pos - FIRST_PROCESS_ENTRY;
3330 reaper = get_proc_task(filp->f_path.dentry->d_inode);
3334 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
3335 const struct pid_entry *p = &proc_base_stuff[nr];
3336 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
3340 ns = filp->f_dentry->d_sb->s_fs_info;
3342 iter.tgid = filp->f_pos - TGID_OFFSET;
3343 for (iter = next_tgid(ns, iter);
3345 iter.tgid += 1, iter = next_tgid(ns, iter)) {
3346 if (has_pid_permissions(ns, iter.task, 2))
3347 __filldir = filldir;
3349 __filldir = fake_filldir;
3351 filp->f_pos = iter.tgid + TGID_OFFSET;
3352 if (proc_pid_fill_cache(filp, dirent, __filldir, iter) < 0) {
3353 put_task_struct(iter.task);
3357 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
3359 put_task_struct(reaper);
3367 static const struct pid_entry tid_base_stuff[] = {
3368 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
3369 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
3370 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
3371 REG("environ", S_IRUSR, proc_environ_operations),
3372 INF("auxv", S_IRUSR, proc_pid_auxv),
3373 ONE("status", S_IRUGO, proc_pid_status),
3374 ONE("personality", S_IRUGO, proc_pid_personality),
3375 INF("limits", S_IRUGO, proc_pid_limits),
3376 #ifdef CONFIG_SCHED_DEBUG
3377 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
3379 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
3380 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3381 INF("syscall", S_IRUGO, proc_pid_syscall),
3383 INF("cmdline", S_IRUGO, proc_pid_cmdline),
3384 ONE("stat", S_IRUGO, proc_tid_stat),
3385 ONE("statm", S_IRUGO, proc_pid_statm),
3386 REG("maps", S_IRUGO, proc_maps_operations),
3388 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
3390 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3391 LNK("cwd", proc_cwd_link),
3392 LNK("root", proc_root_link),
3393 LNK("exe", proc_exe_link),
3394 REG("mounts", S_IRUGO, proc_mounts_operations),
3395 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
3396 #ifdef CONFIG_PROC_PAGE_MONITOR
3397 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3398 REG("smaps", S_IRUGO, proc_smaps_operations),
3399 REG("pagemap", S_IRUGO, proc_pagemap_operations),
3401 #ifdef CONFIG_SECURITY
3402 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
3404 #ifdef CONFIG_KALLSYMS
3405 INF("wchan", S_IRUGO, proc_pid_wchan),
3407 #ifdef CONFIG_STACKTRACE
3408 ONE("stack", S_IRUGO, proc_pid_stack),
3410 #ifdef CONFIG_SCHEDSTATS
3411 INF("schedstat", S_IRUGO, proc_pid_schedstat),
3413 #ifdef CONFIG_LATENCYTOP
3414 REG("latency", S_IRUGO, proc_lstats_operations),
3416 #ifdef CONFIG_PROC_PID_CPUSET
3417 REG("cpuset", S_IRUGO, proc_cpuset_operations),
3419 #ifdef CONFIG_CGROUPS
3420 REG("cgroup", S_IRUGO, proc_cgroup_operations),
3422 INF("oom_score", S_IRUGO, proc_oom_score),
3423 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
3424 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
3425 #ifdef CONFIG_AUDITSYSCALL
3426 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3427 REG("sessionid", S_IRUGO, proc_sessionid_operations),
3429 #ifdef CONFIG_FAULT_INJECTION
3430 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3432 #ifdef CONFIG_TASK_IO_ACCOUNTING
3433 INF("io", S_IRUSR, proc_tid_io_accounting),
3435 #ifdef CONFIG_HARDWALL
3436 INF("hardwall", S_IRUGO, proc_pid_hardwall),
3440 static int proc_tid_base_readdir(struct file * filp,
3441 void * dirent, filldir_t filldir)
3443 return proc_pident_readdir(filp,dirent,filldir,
3444 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
3447 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
3448 return proc_pident_lookup(dir, dentry,
3449 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3452 static const struct file_operations proc_tid_base_operations = {
3453 .read = generic_read_dir,
3454 .readdir = proc_tid_base_readdir,
3455 .llseek = default_llseek,
3458 static const struct inode_operations proc_tid_base_inode_operations = {
3459 .lookup = proc_tid_base_lookup,
3460 .getattr = pid_getattr,
3461 .setattr = proc_setattr,
3464 static struct dentry *proc_task_instantiate(struct inode *dir,
3465 struct dentry *dentry, struct task_struct *task, const void *ptr)
3467 struct dentry *error = ERR_PTR(-ENOENT);
3468 struct inode *inode;
3469 inode = proc_pid_make_inode(dir->i_sb, task);
3473 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3474 inode->i_op = &proc_tid_base_inode_operations;
3475 inode->i_fop = &proc_tid_base_operations;
3476 inode->i_flags|=S_IMMUTABLE;
3478 set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3479 ARRAY_SIZE(tid_base_stuff)));
3481 d_set_d_op(dentry, &pid_dentry_operations);
3483 d_add(dentry, inode);
3484 /* Close the race of the process dying before we return the dentry */
3485 if (pid_revalidate(dentry, NULL))
3491 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
3493 struct dentry *result = ERR_PTR(-ENOENT);
3494 struct task_struct *task;
3495 struct task_struct *leader = get_proc_task(dir);
3497 struct pid_namespace *ns;
3502 tid = name_to_int(dentry);
3506 ns = dentry->d_sb->s_fs_info;
3508 task = find_task_by_pid_ns(tid, ns);
3510 get_task_struct(task);
3514 if (!same_thread_group(leader, task))
3517 result = proc_task_instantiate(dir, dentry, task, NULL);
3519 put_task_struct(task);
3521 put_task_struct(leader);
3527 * Find the first tid of a thread group to return to user space.
3529 * Usually this is just the thread group leader, but if the users
3530 * buffer was too small or there was a seek into the middle of the
3531 * directory we have more work todo.
3533 * In the case of a short read we start with find_task_by_pid.
3535 * In the case of a seek we start with the leader and walk nr
3538 static struct task_struct *first_tid(struct task_struct *leader,
3539 int tid, int nr, struct pid_namespace *ns)
3541 struct task_struct *pos;
3544 /* Attempt to start with the pid of a thread */
3545 if (tid && (nr > 0)) {
3546 pos = find_task_by_pid_ns(tid, ns);
3547 if (pos && (pos->group_leader == leader))
3551 /* If nr exceeds the number of threads there is nothing todo */
3553 if (nr && nr >= get_nr_threads(leader))
3556 /* If we haven't found our starting place yet start
3557 * with the leader and walk nr threads forward.
3559 for (pos = leader; nr > 0; --nr) {
3560 pos = next_thread(pos);
3561 if (pos == leader) {
3567 get_task_struct(pos);
3574 * Find the next thread in the thread list.
3575 * Return NULL if there is an error or no next thread.
3577 * The reference to the input task_struct is released.
3579 static struct task_struct *next_tid(struct task_struct *start)
3581 struct task_struct *pos = NULL;
3583 if (pid_alive(start)) {
3584 pos = next_thread(start);
3585 if (thread_group_leader(pos))
3588 get_task_struct(pos);
3591 put_task_struct(start);
3595 static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3596 struct task_struct *task, int tid)
3598 char name[PROC_NUMBUF];
3599 int len = snprintf(name, sizeof(name), "%d", tid);
3600 return proc_fill_cache(filp, dirent, filldir, name, len,
3601 proc_task_instantiate, task, NULL);
3604 /* for the /proc/TGID/task/ directories */
3605 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
3607 struct dentry *dentry = filp->f_path.dentry;
3608 struct inode *inode = dentry->d_inode;
3609 struct task_struct *leader = NULL;
3610 struct task_struct *task;
3611 int retval = -ENOENT;
3614 struct pid_namespace *ns;
3616 task = get_proc_task(inode);
3620 if (pid_alive(task)) {
3621 leader = task->group_leader;
3622 get_task_struct(leader);
3625 put_task_struct(task);
3630 switch ((unsigned long)filp->f_pos) {
3633 if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) < 0)
3638 ino = parent_ino(dentry);
3639 if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) < 0)
3645 /* f_version caches the tgid value that the last readdir call couldn't
3646 * return. lseek aka telldir automagically resets f_version to 0.
3648 ns = filp->f_dentry->d_sb->s_fs_info;
3649 tid = (int)filp->f_version;
3650 filp->f_version = 0;
3651 for (task = first_tid(leader, tid, filp->f_pos - 2, ns);
3653 task = next_tid(task), filp->f_pos++) {
3654 tid = task_pid_nr_ns(task, ns);
3655 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
3656 /* returning this tgid failed, save it as the first
3657 * pid for the next readir call */
3658 filp->f_version = (u64)tid;
3659 put_task_struct(task);
3664 put_task_struct(leader);
3669 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3671 struct inode *inode = dentry->d_inode;
3672 struct task_struct *p = get_proc_task(inode);
3673 generic_fillattr(inode, stat);
3676 stat->nlink += get_nr_threads(p);
3683 static const struct inode_operations proc_task_inode_operations = {
3684 .lookup = proc_task_lookup,
3685 .getattr = proc_task_getattr,
3686 .setattr = proc_setattr,
3687 .permission = proc_pid_permission,
3690 static const struct file_operations proc_task_operations = {
3691 .read = generic_read_dir,
3692 .readdir = proc_task_readdir,
3693 .llseek = default_llseek,