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 *__check_mem_permission(struct task_struct *task)
203 struct mm_struct *mm;
205 mm = get_task_mm(task);
207 return ERR_PTR(-EINVAL);
210 * A task can always look at itself, in case it chooses
211 * to use system calls instead of load instructions.
217 * If current is actively ptrace'ing, and would also be
218 * permitted to freshly attach with ptrace now, permit it.
220 if (task_is_stopped_or_traced(task)) {
223 match = (ptrace_parent(task) == current);
225 if (match && ptrace_may_access(task, PTRACE_MODE_ATTACH))
230 * No one else is allowed.
233 return ERR_PTR(-EPERM);
237 * If current may access user memory in @task return a reference to the
238 * corresponding mm, otherwise ERR_PTR.
240 static struct mm_struct *check_mem_permission(struct task_struct *task)
242 struct mm_struct *mm;
246 * Avoid racing if task exec's as we might get a new mm but validate
247 * against old credentials.
249 err = mutex_lock_killable(&task->signal->cred_guard_mutex);
253 mm = __check_mem_permission(task);
254 mutex_unlock(&task->signal->cred_guard_mutex);
259 struct mm_struct *mm_for_maps(struct task_struct *task)
261 struct mm_struct *mm;
264 err = mutex_lock_killable(&task->signal->cred_guard_mutex);
268 mm = get_task_mm(task);
269 if (mm && mm != current->mm &&
270 !ptrace_may_access(task, PTRACE_MODE_READ)) {
272 mm = ERR_PTR(-EACCES);
274 mutex_unlock(&task->signal->cred_guard_mutex);
279 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
283 struct mm_struct *mm = get_task_mm(task);
287 goto out_mm; /* Shh! No looking before we're done */
289 len = mm->arg_end - mm->arg_start;
294 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
296 // If the nul at the end of args has been overwritten, then
297 // assume application is using setproctitle(3).
298 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
299 len = strnlen(buffer, res);
303 len = mm->env_end - mm->env_start;
304 if (len > PAGE_SIZE - res)
305 len = PAGE_SIZE - res;
306 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
307 res = strnlen(buffer, res);
316 static int proc_pid_auxv(struct task_struct *task, char *buffer)
318 struct mm_struct *mm = mm_for_maps(task);
319 int res = PTR_ERR(mm);
320 if (mm && !IS_ERR(mm)) {
321 unsigned int nwords = 0;
324 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
325 res = nwords * sizeof(mm->saved_auxv[0]);
328 memcpy(buffer, mm->saved_auxv, res);
335 #ifdef CONFIG_KALLSYMS
337 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
338 * Returns the resolved symbol. If that fails, simply return the address.
340 static int proc_pid_wchan(struct task_struct *task, char *buffer)
343 char symname[KSYM_NAME_LEN];
345 wchan = get_wchan(task);
347 if (lookup_symbol_name(wchan, symname) < 0)
348 if (!ptrace_may_access(task, PTRACE_MODE_READ))
351 return sprintf(buffer, "%lu", wchan);
353 return sprintf(buffer, "%s", symname);
355 #endif /* CONFIG_KALLSYMS */
357 static int lock_trace(struct task_struct *task)
359 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
362 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH)) {
363 mutex_unlock(&task->signal->cred_guard_mutex);
369 static void unlock_trace(struct task_struct *task)
371 mutex_unlock(&task->signal->cred_guard_mutex);
374 #ifdef CONFIG_STACKTRACE
376 #define MAX_STACK_TRACE_DEPTH 64
378 static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
379 struct pid *pid, struct task_struct *task)
381 struct stack_trace trace;
382 unsigned long *entries;
386 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
390 trace.nr_entries = 0;
391 trace.max_entries = MAX_STACK_TRACE_DEPTH;
392 trace.entries = entries;
395 err = lock_trace(task);
397 save_stack_trace_tsk(task, &trace);
399 for (i = 0; i < trace.nr_entries; i++) {
400 seq_printf(m, "[<%pK>] %pS\n",
401 (void *)entries[i], (void *)entries[i]);
411 #ifdef CONFIG_SCHEDSTATS
413 * Provides /proc/PID/schedstat
415 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
417 return sprintf(buffer, "%llu %llu %lu\n",
418 (unsigned long long)task->se.sum_exec_runtime,
419 (unsigned long long)task->sched_info.run_delay,
420 task->sched_info.pcount);
424 #ifdef CONFIG_LATENCYTOP
425 static int lstats_show_proc(struct seq_file *m, void *v)
428 struct inode *inode = m->private;
429 struct task_struct *task = get_proc_task(inode);
433 seq_puts(m, "Latency Top version : v0.1\n");
434 for (i = 0; i < 32; i++) {
435 struct latency_record *lr = &task->latency_record[i];
436 if (lr->backtrace[0]) {
438 seq_printf(m, "%i %li %li",
439 lr->count, lr->time, lr->max);
440 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
441 unsigned long bt = lr->backtrace[q];
446 seq_printf(m, " %ps", (void *)bt);
452 put_task_struct(task);
456 static int lstats_open(struct inode *inode, struct file *file)
458 return single_open(file, lstats_show_proc, inode);
461 static ssize_t lstats_write(struct file *file, const char __user *buf,
462 size_t count, loff_t *offs)
464 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
468 clear_all_latency_tracing(task);
469 put_task_struct(task);
474 static const struct file_operations proc_lstats_operations = {
477 .write = lstats_write,
479 .release = single_release,
484 static int proc_oom_score(struct task_struct *task, char *buffer)
486 unsigned long points = 0;
488 read_lock(&tasklist_lock);
490 points = oom_badness(task, NULL, NULL,
491 totalram_pages + total_swap_pages);
492 read_unlock(&tasklist_lock);
493 return sprintf(buffer, "%lu\n", points);
501 static const struct limit_names lnames[RLIM_NLIMITS] = {
502 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
503 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
504 [RLIMIT_DATA] = {"Max data size", "bytes"},
505 [RLIMIT_STACK] = {"Max stack size", "bytes"},
506 [RLIMIT_CORE] = {"Max core file size", "bytes"},
507 [RLIMIT_RSS] = {"Max resident set", "bytes"},
508 [RLIMIT_NPROC] = {"Max processes", "processes"},
509 [RLIMIT_NOFILE] = {"Max open files", "files"},
510 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
511 [RLIMIT_AS] = {"Max address space", "bytes"},
512 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
513 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
514 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
515 [RLIMIT_NICE] = {"Max nice priority", NULL},
516 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
517 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
520 /* Display limits for a process */
521 static int proc_pid_limits(struct task_struct *task, char *buffer)
526 char *bufptr = buffer;
528 struct rlimit rlim[RLIM_NLIMITS];
530 if (!lock_task_sighand(task, &flags))
532 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
533 unlock_task_sighand(task, &flags);
536 * print the file header
538 count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
539 "Limit", "Soft Limit", "Hard Limit", "Units");
541 for (i = 0; i < RLIM_NLIMITS; i++) {
542 if (rlim[i].rlim_cur == RLIM_INFINITY)
543 count += sprintf(&bufptr[count], "%-25s %-20s ",
544 lnames[i].name, "unlimited");
546 count += sprintf(&bufptr[count], "%-25s %-20lu ",
547 lnames[i].name, rlim[i].rlim_cur);
549 if (rlim[i].rlim_max == RLIM_INFINITY)
550 count += sprintf(&bufptr[count], "%-20s ", "unlimited");
552 count += sprintf(&bufptr[count], "%-20lu ",
556 count += sprintf(&bufptr[count], "%-10s\n",
559 count += sprintf(&bufptr[count], "\n");
565 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
566 static int proc_pid_syscall(struct task_struct *task, char *buffer)
569 unsigned long args[6], sp, pc;
570 int res = lock_trace(task);
574 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
575 res = sprintf(buffer, "running\n");
577 res = sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
579 res = sprintf(buffer,
580 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
582 args[0], args[1], args[2], args[3], args[4], args[5],
587 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
589 /************************************************************************/
590 /* Here the fs part begins */
591 /************************************************************************/
593 /* permission checks */
594 static int proc_fd_access_allowed(struct inode *inode)
596 struct task_struct *task;
598 /* Allow access to a task's file descriptors if it is us or we
599 * may use ptrace attach to the process and find out that
602 task = get_proc_task(inode);
604 allowed = ptrace_may_access(task, PTRACE_MODE_READ);
605 put_task_struct(task);
610 int proc_setattr(struct dentry *dentry, struct iattr *attr)
613 struct inode *inode = dentry->d_inode;
615 if (attr->ia_valid & ATTR_MODE)
618 error = inode_change_ok(inode, attr);
622 if ((attr->ia_valid & ATTR_SIZE) &&
623 attr->ia_size != i_size_read(inode)) {
624 error = vmtruncate(inode, attr->ia_size);
629 setattr_copy(inode, attr);
630 mark_inode_dirty(inode);
635 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
636 * or euid/egid (for hide_pid_min=2)?
638 static bool has_pid_permissions(struct pid_namespace *pid,
639 struct task_struct *task,
642 if (pid->hide_pid < hide_pid_min)
644 if (in_group_p(pid->pid_gid))
646 return ptrace_may_access(task, PTRACE_MODE_READ);
650 static int proc_pid_permission(struct inode *inode, int mask)
652 struct pid_namespace *pid = inode->i_sb->s_fs_info;
653 struct task_struct *task;
656 task = get_proc_task(inode);
657 has_perms = has_pid_permissions(pid, task, 1);
658 put_task_struct(task);
661 if (pid->hide_pid == 2) {
663 * Let's make getdents(), stat(), and open()
664 * consistent with each other. If a process
665 * may not stat() a file, it shouldn't be seen
673 return generic_permission(inode, mask);
678 static const struct inode_operations proc_def_inode_operations = {
679 .setattr = proc_setattr,
682 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
684 static ssize_t proc_info_read(struct file * file, char __user * buf,
685 size_t count, loff_t *ppos)
687 struct inode * inode = file->f_path.dentry->d_inode;
690 struct task_struct *task = get_proc_task(inode);
696 if (count > PROC_BLOCK_SIZE)
697 count = PROC_BLOCK_SIZE;
700 if (!(page = __get_free_page(GFP_TEMPORARY)))
703 length = PROC_I(inode)->op.proc_read(task, (char*)page);
706 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
709 put_task_struct(task);
714 static const struct file_operations proc_info_file_operations = {
715 .read = proc_info_read,
716 .llseek = generic_file_llseek,
719 static int proc_single_show(struct seq_file *m, void *v)
721 struct inode *inode = m->private;
722 struct pid_namespace *ns;
724 struct task_struct *task;
727 ns = inode->i_sb->s_fs_info;
728 pid = proc_pid(inode);
729 task = get_pid_task(pid, PIDTYPE_PID);
733 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
735 put_task_struct(task);
739 static int proc_single_open(struct inode *inode, struct file *filp)
741 return single_open(filp, proc_single_show, inode);
744 static const struct file_operations proc_single_file_operations = {
745 .open = proc_single_open,
748 .release = single_release,
751 static int mem_open(struct inode* inode, struct file* file)
753 file->private_data = (void*)((long)current->self_exec_id);
754 /* OK to pass negative loff_t, we can catch out-of-range */
755 file->f_mode |= FMODE_UNSIGNED_OFFSET;
759 static ssize_t mem_read(struct file * file, char __user * buf,
760 size_t count, loff_t *ppos)
762 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
764 unsigned long src = *ppos;
766 struct mm_struct *mm;
772 page = (char *)__get_free_page(GFP_TEMPORARY);
776 mm = check_mem_permission(task);
783 if (file->private_data != (void*)((long)current->self_exec_id))
789 int this_len, retval;
791 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
792 retval = access_remote_vm(mm, src, page, this_len, 0);
799 if (copy_to_user(buf, page, retval)) {
814 free_page((unsigned long) page);
816 put_task_struct(task);
821 static ssize_t mem_write(struct file * file, const char __user *buf,
822 size_t count, loff_t *ppos)
826 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
827 unsigned long dst = *ppos;
828 struct mm_struct *mm;
835 page = (char *)__get_free_page(GFP_TEMPORARY);
839 mm = check_mem_permission(task);
840 copied = PTR_ERR(mm);
845 if (file->private_data != (void *)((long)current->self_exec_id))
850 int this_len, retval;
852 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
853 if (copy_from_user(page, buf, this_len)) {
857 retval = access_remote_vm(mm, dst, page, this_len, 1);
873 free_page((unsigned long) page);
875 put_task_struct(task);
880 loff_t mem_lseek(struct file *file, loff_t offset, int orig)
884 file->f_pos = offset;
887 file->f_pos += offset;
892 force_successful_syscall_return();
896 static const struct file_operations proc_mem_operations = {
903 static ssize_t environ_read(struct file *file, char __user *buf,
904 size_t count, loff_t *ppos)
906 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
908 unsigned long src = *ppos;
910 struct mm_struct *mm;
916 page = (char *)__get_free_page(GFP_TEMPORARY);
921 mm = mm_for_maps(task);
923 if (!mm || IS_ERR(mm))
928 int this_len, retval, max_len;
930 this_len = mm->env_end - (mm->env_start + src);
935 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
936 this_len = (this_len > max_len) ? max_len : this_len;
938 retval = access_process_vm(task, (mm->env_start + src),
946 if (copy_to_user(buf, page, retval)) {
960 free_page((unsigned long) page);
962 put_task_struct(task);
967 static const struct file_operations proc_environ_operations = {
968 .read = environ_read,
969 .llseek = generic_file_llseek,
972 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
973 size_t count, loff_t *ppos)
975 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
976 char buffer[PROC_NUMBUF];
978 int oom_adjust = OOM_DISABLE;
984 if (lock_task_sighand(task, &flags)) {
985 oom_adjust = task->signal->oom_adj;
986 unlock_task_sighand(task, &flags);
989 put_task_struct(task);
991 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
993 return simple_read_from_buffer(buf, count, ppos, buffer, len);
996 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
997 size_t count, loff_t *ppos)
999 struct task_struct *task;
1000 char buffer[PROC_NUMBUF];
1002 unsigned long flags;
1005 memset(buffer, 0, sizeof(buffer));
1006 if (count > sizeof(buffer) - 1)
1007 count = sizeof(buffer) - 1;
1008 if (copy_from_user(buffer, buf, count)) {
1013 err = kstrtoint(strstrip(buffer), 0, &oom_adjust);
1016 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
1017 oom_adjust != OOM_DISABLE) {
1022 task = get_proc_task(file->f_path.dentry->d_inode);
1034 if (!lock_task_sighand(task, &flags)) {
1039 if (oom_adjust < task->signal->oom_adj && !capable(CAP_SYS_RESOURCE)) {
1045 * Warn that /proc/pid/oom_adj is deprecated, see
1046 * Documentation/feature-removal-schedule.txt.
1048 printk_once(KERN_WARNING "%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
1049 current->comm, task_pid_nr(current), task_pid_nr(task),
1051 task->signal->oom_adj = oom_adjust;
1053 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1054 * value is always attainable.
1056 if (task->signal->oom_adj == OOM_ADJUST_MAX)
1057 task->signal->oom_score_adj = OOM_SCORE_ADJ_MAX;
1059 task->signal->oom_score_adj = (oom_adjust * OOM_SCORE_ADJ_MAX) /
1061 trace_oom_score_adj_update(task);
1063 unlock_task_sighand(task, &flags);
1066 put_task_struct(task);
1068 return err < 0 ? err : count;
1071 static const struct file_operations proc_oom_adjust_operations = {
1072 .read = oom_adjust_read,
1073 .write = oom_adjust_write,
1074 .llseek = generic_file_llseek,
1077 static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1078 size_t count, loff_t *ppos)
1080 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
1081 char buffer[PROC_NUMBUF];
1082 int oom_score_adj = OOM_SCORE_ADJ_MIN;
1083 unsigned long flags;
1088 if (lock_task_sighand(task, &flags)) {
1089 oom_score_adj = task->signal->oom_score_adj;
1090 unlock_task_sighand(task, &flags);
1092 put_task_struct(task);
1093 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_score_adj);
1094 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1097 static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1098 size_t count, loff_t *ppos)
1100 struct task_struct *task;
1101 char buffer[PROC_NUMBUF];
1102 unsigned long flags;
1106 memset(buffer, 0, sizeof(buffer));
1107 if (count > sizeof(buffer) - 1)
1108 count = sizeof(buffer) - 1;
1109 if (copy_from_user(buffer, buf, count)) {
1114 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
1117 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
1118 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1123 task = get_proc_task(file->f_path.dentry->d_inode);
1135 if (!lock_task_sighand(task, &flags)) {
1140 if (oom_score_adj < task->signal->oom_score_adj_min &&
1141 !capable(CAP_SYS_RESOURCE)) {
1146 task->signal->oom_score_adj = oom_score_adj;
1147 if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
1148 task->signal->oom_score_adj_min = oom_score_adj;
1149 trace_oom_score_adj_update(task);
1151 * Scale /proc/pid/oom_adj appropriately ensuring that OOM_DISABLE is
1152 * always attainable.
1154 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MIN)
1155 task->signal->oom_adj = OOM_DISABLE;
1157 task->signal->oom_adj = (oom_score_adj * OOM_ADJUST_MAX) /
1160 unlock_task_sighand(task, &flags);
1163 put_task_struct(task);
1165 return err < 0 ? err : count;
1168 static const struct file_operations proc_oom_score_adj_operations = {
1169 .read = oom_score_adj_read,
1170 .write = oom_score_adj_write,
1171 .llseek = default_llseek,
1174 #ifdef CONFIG_AUDITSYSCALL
1175 #define TMPBUFLEN 21
1176 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1177 size_t count, loff_t *ppos)
1179 struct inode * inode = file->f_path.dentry->d_inode;
1180 struct task_struct *task = get_proc_task(inode);
1182 char tmpbuf[TMPBUFLEN];
1186 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1187 audit_get_loginuid(task));
1188 put_task_struct(task);
1189 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1192 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1193 size_t count, loff_t *ppos)
1195 struct inode * inode = file->f_path.dentry->d_inode;
1200 if (!capable(CAP_AUDIT_CONTROL))
1204 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1210 if (count >= PAGE_SIZE)
1211 count = PAGE_SIZE - 1;
1214 /* No partial writes. */
1217 page = (char*)__get_free_page(GFP_TEMPORARY);
1221 if (copy_from_user(page, buf, count))
1225 loginuid = simple_strtoul(page, &tmp, 10);
1231 length = audit_set_loginuid(current, loginuid);
1232 if (likely(length == 0))
1236 free_page((unsigned long) page);
1240 static const struct file_operations proc_loginuid_operations = {
1241 .read = proc_loginuid_read,
1242 .write = proc_loginuid_write,
1243 .llseek = generic_file_llseek,
1246 static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1247 size_t count, loff_t *ppos)
1249 struct inode * inode = file->f_path.dentry->d_inode;
1250 struct task_struct *task = get_proc_task(inode);
1252 char tmpbuf[TMPBUFLEN];
1256 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1257 audit_get_sessionid(task));
1258 put_task_struct(task);
1259 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1262 static const struct file_operations proc_sessionid_operations = {
1263 .read = proc_sessionid_read,
1264 .llseek = generic_file_llseek,
1268 #ifdef CONFIG_FAULT_INJECTION
1269 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1270 size_t count, loff_t *ppos)
1272 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1273 char buffer[PROC_NUMBUF];
1279 make_it_fail = task->make_it_fail;
1280 put_task_struct(task);
1282 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1284 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1287 static ssize_t proc_fault_inject_write(struct file * file,
1288 const char __user * buf, size_t count, loff_t *ppos)
1290 struct task_struct *task;
1291 char buffer[PROC_NUMBUF], *end;
1294 if (!capable(CAP_SYS_RESOURCE))
1296 memset(buffer, 0, sizeof(buffer));
1297 if (count > sizeof(buffer) - 1)
1298 count = sizeof(buffer) - 1;
1299 if (copy_from_user(buffer, buf, count))
1301 make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1304 task = get_proc_task(file->f_dentry->d_inode);
1307 task->make_it_fail = make_it_fail;
1308 put_task_struct(task);
1313 static const struct file_operations proc_fault_inject_operations = {
1314 .read = proc_fault_inject_read,
1315 .write = proc_fault_inject_write,
1316 .llseek = generic_file_llseek,
1321 #ifdef CONFIG_SCHED_DEBUG
1323 * Print out various scheduling related per-task fields:
1325 static int sched_show(struct seq_file *m, void *v)
1327 struct inode *inode = m->private;
1328 struct task_struct *p;
1330 p = get_proc_task(inode);
1333 proc_sched_show_task(p, m);
1341 sched_write(struct file *file, const char __user *buf,
1342 size_t count, loff_t *offset)
1344 struct inode *inode = file->f_path.dentry->d_inode;
1345 struct task_struct *p;
1347 p = get_proc_task(inode);
1350 proc_sched_set_task(p);
1357 static int sched_open(struct inode *inode, struct file *filp)
1359 return single_open(filp, sched_show, inode);
1362 static const struct file_operations proc_pid_sched_operations = {
1365 .write = sched_write,
1366 .llseek = seq_lseek,
1367 .release = single_release,
1372 #ifdef CONFIG_SCHED_AUTOGROUP
1374 * Print out autogroup related information:
1376 static int sched_autogroup_show(struct seq_file *m, void *v)
1378 struct inode *inode = m->private;
1379 struct task_struct *p;
1381 p = get_proc_task(inode);
1384 proc_sched_autogroup_show_task(p, m);
1392 sched_autogroup_write(struct file *file, const char __user *buf,
1393 size_t count, loff_t *offset)
1395 struct inode *inode = file->f_path.dentry->d_inode;
1396 struct task_struct *p;
1397 char buffer[PROC_NUMBUF];
1401 memset(buffer, 0, sizeof(buffer));
1402 if (count > sizeof(buffer) - 1)
1403 count = sizeof(buffer) - 1;
1404 if (copy_from_user(buffer, buf, count))
1407 err = kstrtoint(strstrip(buffer), 0, &nice);
1411 p = get_proc_task(inode);
1416 err = proc_sched_autogroup_set_nice(p, &err);
1425 static int sched_autogroup_open(struct inode *inode, struct file *filp)
1429 ret = single_open(filp, sched_autogroup_show, NULL);
1431 struct seq_file *m = filp->private_data;
1438 static const struct file_operations proc_pid_sched_autogroup_operations = {
1439 .open = sched_autogroup_open,
1441 .write = sched_autogroup_write,
1442 .llseek = seq_lseek,
1443 .release = single_release,
1446 #endif /* CONFIG_SCHED_AUTOGROUP */
1448 static ssize_t comm_write(struct file *file, const char __user *buf,
1449 size_t count, loff_t *offset)
1451 struct inode *inode = file->f_path.dentry->d_inode;
1452 struct task_struct *p;
1453 char buffer[TASK_COMM_LEN];
1455 memset(buffer, 0, sizeof(buffer));
1456 if (count > sizeof(buffer) - 1)
1457 count = sizeof(buffer) - 1;
1458 if (copy_from_user(buffer, buf, count))
1461 p = get_proc_task(inode);
1465 if (same_thread_group(current, p))
1466 set_task_comm(p, buffer);
1475 static int comm_show(struct seq_file *m, void *v)
1477 struct inode *inode = m->private;
1478 struct task_struct *p;
1480 p = get_proc_task(inode);
1485 seq_printf(m, "%s\n", p->comm);
1493 static int comm_open(struct inode *inode, struct file *filp)
1495 return single_open(filp, comm_show, inode);
1498 static const struct file_operations proc_pid_set_comm_operations = {
1501 .write = comm_write,
1502 .llseek = seq_lseek,
1503 .release = single_release,
1506 static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
1508 struct task_struct *task;
1509 struct mm_struct *mm;
1510 struct file *exe_file;
1512 task = get_proc_task(dentry->d_inode);
1515 mm = get_task_mm(task);
1516 put_task_struct(task);
1519 exe_file = get_mm_exe_file(mm);
1522 *exe_path = exe_file->f_path;
1523 path_get(&exe_file->f_path);
1530 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1532 struct inode *inode = dentry->d_inode;
1533 int error = -EACCES;
1535 /* We don't need a base pointer in the /proc filesystem */
1536 path_put(&nd->path);
1538 /* Are we allowed to snoop on the tasks file descriptors? */
1539 if (!proc_fd_access_allowed(inode))
1542 error = PROC_I(inode)->op.proc_get_link(dentry, &nd->path);
1544 return ERR_PTR(error);
1547 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1549 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
1556 pathname = d_path(path, tmp, PAGE_SIZE);
1557 len = PTR_ERR(pathname);
1558 if (IS_ERR(pathname))
1560 len = tmp + PAGE_SIZE - 1 - pathname;
1564 if (copy_to_user(buffer, pathname, len))
1567 free_page((unsigned long)tmp);
1571 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1573 int error = -EACCES;
1574 struct inode *inode = dentry->d_inode;
1577 /* Are we allowed to snoop on the tasks file descriptors? */
1578 if (!proc_fd_access_allowed(inode))
1581 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1585 error = do_proc_readlink(&path, buffer, buflen);
1591 static const struct inode_operations proc_pid_link_inode_operations = {
1592 .readlink = proc_pid_readlink,
1593 .follow_link = proc_pid_follow_link,
1594 .setattr = proc_setattr,
1598 /* building an inode */
1600 static int task_dumpable(struct task_struct *task)
1603 struct mm_struct *mm;
1608 dumpable = get_dumpable(mm);
1615 struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1617 struct inode * inode;
1618 struct proc_inode *ei;
1619 const struct cred *cred;
1621 /* We need a new inode */
1623 inode = new_inode(sb);
1629 inode->i_ino = get_next_ino();
1630 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1631 inode->i_op = &proc_def_inode_operations;
1634 * grab the reference to task.
1636 ei->pid = get_task_pid(task, PIDTYPE_PID);
1640 if (task_dumpable(task)) {
1642 cred = __task_cred(task);
1643 inode->i_uid = cred->euid;
1644 inode->i_gid = cred->egid;
1647 security_task_to_inode(task, inode);
1657 int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1659 struct inode *inode = dentry->d_inode;
1660 struct task_struct *task;
1661 const struct cred *cred;
1662 struct pid_namespace *pid = dentry->d_sb->s_fs_info;
1664 generic_fillattr(inode, stat);
1669 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1671 if (!has_pid_permissions(pid, task, 2)) {
1674 * This doesn't prevent learning whether PID exists,
1675 * it only makes getattr() consistent with readdir().
1679 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1680 task_dumpable(task)) {
1681 cred = __task_cred(task);
1682 stat->uid = cred->euid;
1683 stat->gid = cred->egid;
1693 * Exceptional case: normally we are not allowed to unhash a busy
1694 * directory. In this case, however, we can do it - no aliasing problems
1695 * due to the way we treat inodes.
1697 * Rewrite the inode's ownerships here because the owning task may have
1698 * performed a setuid(), etc.
1700 * Before the /proc/pid/status file was created the only way to read
1701 * the effective uid of a /process was to stat /proc/pid. Reading
1702 * /proc/pid/status is slow enough that procps and other packages
1703 * kept stating /proc/pid. To keep the rules in /proc simple I have
1704 * made this apply to all per process world readable and executable
1707 int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1709 struct inode *inode;
1710 struct task_struct *task;
1711 const struct cred *cred;
1713 if (nd && nd->flags & LOOKUP_RCU)
1716 inode = dentry->d_inode;
1717 task = get_proc_task(inode);
1720 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1721 task_dumpable(task)) {
1723 cred = __task_cred(task);
1724 inode->i_uid = cred->euid;
1725 inode->i_gid = cred->egid;
1731 inode->i_mode &= ~(S_ISUID | S_ISGID);
1732 security_task_to_inode(task, inode);
1733 put_task_struct(task);
1740 static int pid_delete_dentry(const struct dentry * dentry)
1742 /* Is the task we represent dead?
1743 * If so, then don't put the dentry on the lru list,
1744 * kill it immediately.
1746 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1749 const struct dentry_operations pid_dentry_operations =
1751 .d_revalidate = pid_revalidate,
1752 .d_delete = pid_delete_dentry,
1758 * Fill a directory entry.
1760 * If possible create the dcache entry and derive our inode number and
1761 * file type from dcache entry.
1763 * Since all of the proc inode numbers are dynamically generated, the inode
1764 * numbers do not exist until the inode is cache. This means creating the
1765 * the dcache entry in readdir is necessary to keep the inode numbers
1766 * reported by readdir in sync with the inode numbers reported
1769 int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1770 const char *name, int len,
1771 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1773 struct dentry *child, *dir = filp->f_path.dentry;
1774 struct inode *inode;
1777 unsigned type = DT_UNKNOWN;
1781 qname.hash = full_name_hash(name, len);
1783 child = d_lookup(dir, &qname);
1786 new = d_alloc(dir, &qname);
1788 child = instantiate(dir->d_inode, new, task, ptr);
1795 if (!child || IS_ERR(child) || !child->d_inode)
1796 goto end_instantiate;
1797 inode = child->d_inode;
1800 type = inode->i_mode >> 12;
1805 ino = find_inode_number(dir, &qname);
1808 return filldir(dirent, name, len, filp->f_pos, ino, type);
1811 static unsigned name_to_int(struct dentry *dentry)
1813 const char *name = dentry->d_name.name;
1814 int len = dentry->d_name.len;
1817 if (len > 1 && *name == '0')
1820 unsigned c = *name++ - '0';
1823 if (n >= (~0U-9)/10)
1833 #define PROC_FDINFO_MAX 64
1835 static int proc_fd_info(struct inode *inode, struct path *path, char *info)
1837 struct task_struct *task = get_proc_task(inode);
1838 struct files_struct *files = NULL;
1840 int fd = proc_fd(inode);
1843 files = get_files_struct(task);
1844 put_task_struct(task);
1848 * We are not taking a ref to the file structure, so we must
1851 spin_lock(&files->file_lock);
1852 file = fcheck_files(files, fd);
1854 unsigned int f_flags;
1855 struct fdtable *fdt;
1857 fdt = files_fdtable(files);
1858 f_flags = file->f_flags & ~O_CLOEXEC;
1859 if (FD_ISSET(fd, fdt->close_on_exec))
1860 f_flags |= O_CLOEXEC;
1863 *path = file->f_path;
1864 path_get(&file->f_path);
1867 snprintf(info, PROC_FDINFO_MAX,
1870 (long long) file->f_pos,
1872 spin_unlock(&files->file_lock);
1873 put_files_struct(files);
1876 spin_unlock(&files->file_lock);
1877 put_files_struct(files);
1882 static int proc_fd_link(struct dentry *dentry, struct path *path)
1884 return proc_fd_info(dentry->d_inode, path, NULL);
1887 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1889 struct inode *inode;
1890 struct task_struct *task;
1892 struct files_struct *files;
1893 const struct cred *cred;
1895 if (nd && nd->flags & LOOKUP_RCU)
1898 inode = dentry->d_inode;
1899 task = get_proc_task(inode);
1900 fd = proc_fd(inode);
1903 files = get_files_struct(task);
1906 if (fcheck_files(files, fd)) {
1908 put_files_struct(files);
1909 if (task_dumpable(task)) {
1911 cred = __task_cred(task);
1912 inode->i_uid = cred->euid;
1913 inode->i_gid = cred->egid;
1919 inode->i_mode &= ~(S_ISUID | S_ISGID);
1920 security_task_to_inode(task, inode);
1921 put_task_struct(task);
1925 put_files_struct(files);
1927 put_task_struct(task);
1933 static const struct dentry_operations tid_fd_dentry_operations =
1935 .d_revalidate = tid_fd_revalidate,
1936 .d_delete = pid_delete_dentry,
1939 static struct dentry *proc_fd_instantiate(struct inode *dir,
1940 struct dentry *dentry, struct task_struct *task, const void *ptr)
1942 unsigned fd = *(const unsigned *)ptr;
1944 struct files_struct *files;
1945 struct inode *inode;
1946 struct proc_inode *ei;
1947 struct dentry *error = ERR_PTR(-ENOENT);
1949 inode = proc_pid_make_inode(dir->i_sb, task);
1954 files = get_files_struct(task);
1957 inode->i_mode = S_IFLNK;
1960 * We are not taking a ref to the file structure, so we must
1963 spin_lock(&files->file_lock);
1964 file = fcheck_files(files, fd);
1967 if (file->f_mode & FMODE_READ)
1968 inode->i_mode |= S_IRUSR | S_IXUSR;
1969 if (file->f_mode & FMODE_WRITE)
1970 inode->i_mode |= S_IWUSR | S_IXUSR;
1971 spin_unlock(&files->file_lock);
1972 put_files_struct(files);
1974 inode->i_op = &proc_pid_link_inode_operations;
1976 ei->op.proc_get_link = proc_fd_link;
1977 d_set_d_op(dentry, &tid_fd_dentry_operations);
1978 d_add(dentry, inode);
1979 /* Close the race of the process dying before we return the dentry */
1980 if (tid_fd_revalidate(dentry, NULL))
1986 spin_unlock(&files->file_lock);
1987 put_files_struct(files);
1993 static struct dentry *proc_lookupfd_common(struct inode *dir,
1994 struct dentry *dentry,
1995 instantiate_t instantiate)
1997 struct task_struct *task = get_proc_task(dir);
1998 unsigned fd = name_to_int(dentry);
1999 struct dentry *result = ERR_PTR(-ENOENT);
2006 result = instantiate(dir, dentry, task, &fd);
2008 put_task_struct(task);
2013 static int proc_readfd_common(struct file * filp, void * dirent,
2014 filldir_t filldir, instantiate_t instantiate)
2016 struct dentry *dentry = filp->f_path.dentry;
2017 struct inode *inode = dentry->d_inode;
2018 struct task_struct *p = get_proc_task(inode);
2019 unsigned int fd, ino;
2021 struct files_struct * files;
2031 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
2035 ino = parent_ino(dentry);
2036 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
2040 files = get_files_struct(p);
2044 for (fd = filp->f_pos-2;
2045 fd < files_fdtable(files)->max_fds;
2046 fd++, filp->f_pos++) {
2047 char name[PROC_NUMBUF];
2050 if (!fcheck_files(files, fd))
2054 len = snprintf(name, sizeof(name), "%d", fd);
2055 if (proc_fill_cache(filp, dirent, filldir,
2056 name, len, instantiate,
2064 put_files_struct(files);
2072 static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
2073 struct nameidata *nd)
2075 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
2078 static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
2080 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
2083 static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
2084 size_t len, loff_t *ppos)
2086 char tmp[PROC_FDINFO_MAX];
2087 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
2089 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
2093 static const struct file_operations proc_fdinfo_file_operations = {
2094 .open = nonseekable_open,
2095 .read = proc_fdinfo_read,
2096 .llseek = no_llseek,
2099 static const struct file_operations proc_fd_operations = {
2100 .read = generic_read_dir,
2101 .readdir = proc_readfd,
2102 .llseek = default_llseek,
2105 #ifdef CONFIG_CHECKPOINT_RESTORE
2108 * dname_to_vma_addr - maps a dentry name into two unsigned longs
2109 * which represent vma start and end addresses.
2111 static int dname_to_vma_addr(struct dentry *dentry,
2112 unsigned long *start, unsigned long *end)
2114 if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2)
2120 static int map_files_d_revalidate(struct dentry *dentry, struct nameidata *nd)
2122 unsigned long vm_start, vm_end;
2123 bool exact_vma_exists = false;
2124 struct mm_struct *mm = NULL;
2125 struct task_struct *task;
2126 const struct cred *cred;
2127 struct inode *inode;
2130 if (nd && nd->flags & LOOKUP_RCU)
2133 if (!capable(CAP_SYS_ADMIN)) {
2138 inode = dentry->d_inode;
2139 task = get_proc_task(inode);
2143 if (!ptrace_may_access(task, PTRACE_MODE_READ))
2146 mm = get_task_mm(task);
2150 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
2151 down_read(&mm->mmap_sem);
2152 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
2153 up_read(&mm->mmap_sem);
2158 if (exact_vma_exists) {
2159 if (task_dumpable(task)) {
2161 cred = __task_cred(task);
2162 inode->i_uid = cred->euid;
2163 inode->i_gid = cred->egid;
2169 security_task_to_inode(task, inode);
2174 put_task_struct(task);
2183 static const struct dentry_operations tid_map_files_dentry_operations = {
2184 .d_revalidate = map_files_d_revalidate,
2185 .d_delete = pid_delete_dentry,
2188 static int proc_map_files_get_link(struct dentry *dentry, struct path *path)
2190 unsigned long vm_start, vm_end;
2191 struct vm_area_struct *vma;
2192 struct task_struct *task;
2193 struct mm_struct *mm;
2197 task = get_proc_task(dentry->d_inode);
2201 mm = get_task_mm(task);
2202 put_task_struct(task);
2206 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
2210 down_read(&mm->mmap_sem);
2211 vma = find_exact_vma(mm, vm_start, vm_end);
2212 if (vma && vma->vm_file) {
2213 *path = vma->vm_file->f_path;
2217 up_read(&mm->mmap_sem);
2225 struct map_files_info {
2228 unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
2231 static struct dentry *
2232 proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
2233 struct task_struct *task, const void *ptr)
2235 const struct file *file = ptr;
2236 struct proc_inode *ei;
2237 struct inode *inode;
2240 return ERR_PTR(-ENOENT);
2242 inode = proc_pid_make_inode(dir->i_sb, task);
2244 return ERR_PTR(-ENOENT);
2247 ei->op.proc_get_link = proc_map_files_get_link;
2249 inode->i_op = &proc_pid_link_inode_operations;
2251 inode->i_mode = S_IFLNK;
2253 if (file->f_mode & FMODE_READ)
2254 inode->i_mode |= S_IRUSR;
2255 if (file->f_mode & FMODE_WRITE)
2256 inode->i_mode |= S_IWUSR;
2258 d_set_d_op(dentry, &tid_map_files_dentry_operations);
2259 d_add(dentry, inode);
2264 static struct dentry *proc_map_files_lookup(struct inode *dir,
2265 struct dentry *dentry, struct nameidata *nd)
2267 unsigned long vm_start, vm_end;
2268 struct vm_area_struct *vma;
2269 struct task_struct *task;
2270 struct dentry *result;
2271 struct mm_struct *mm;
2273 result = ERR_PTR(-EACCES);
2274 if (!capable(CAP_SYS_ADMIN))
2277 result = ERR_PTR(-ENOENT);
2278 task = get_proc_task(dir);
2282 result = ERR_PTR(-EACCES);
2283 if (lock_trace(task))
2286 result = ERR_PTR(-ENOENT);
2287 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
2290 mm = get_task_mm(task);
2294 down_read(&mm->mmap_sem);
2295 vma = find_exact_vma(mm, vm_start, vm_end);
2299 result = proc_map_files_instantiate(dir, dentry, task, vma->vm_file);
2302 up_read(&mm->mmap_sem);
2307 put_task_struct(task);
2312 static const struct inode_operations proc_map_files_inode_operations = {
2313 .lookup = proc_map_files_lookup,
2314 .permission = proc_fd_permission,
2315 .setattr = proc_setattr,
2319 proc_map_files_readdir(struct file *filp, void *dirent, filldir_t filldir)
2321 struct dentry *dentry = filp->f_path.dentry;
2322 struct inode *inode = dentry->d_inode;
2323 struct vm_area_struct *vma;
2324 struct task_struct *task;
2325 struct mm_struct *mm;
2330 if (!capable(CAP_SYS_ADMIN))
2334 task = get_proc_task(inode);
2339 if (lock_trace(task))
2343 switch (filp->f_pos) {
2346 if (filldir(dirent, ".", 1, 0, ino, DT_DIR) < 0)
2350 ino = parent_ino(dentry);
2351 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
2356 unsigned long nr_files, pos, i;
2357 struct flex_array *fa = NULL;
2358 struct map_files_info info;
2359 struct map_files_info *p;
2361 mm = get_task_mm(task);
2364 down_read(&mm->mmap_sem);
2369 * We need two passes here:
2371 * 1) Collect vmas of mapped files with mmap_sem taken
2372 * 2) Release mmap_sem and instantiate entries
2374 * otherwise we get lockdep complained, since filldir()
2375 * routine might require mmap_sem taken in might_fault().
2378 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
2379 if (vma->vm_file && ++pos > filp->f_pos)
2384 fa = flex_array_alloc(sizeof(info), nr_files,
2386 if (!fa || flex_array_prealloc(fa, 0, nr_files,
2390 flex_array_free(fa);
2391 up_read(&mm->mmap_sem);
2395 for (i = 0, vma = mm->mmap, pos = 2; vma;
2396 vma = vma->vm_next) {
2399 if (++pos <= filp->f_pos)
2402 get_file(vma->vm_file);
2403 info.file = vma->vm_file;
2404 info.len = snprintf(info.name,
2405 sizeof(info.name), "%lx-%lx",
2406 vma->vm_start, vma->vm_end);
2407 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
2411 up_read(&mm->mmap_sem);
2413 for (i = 0; i < nr_files; i++) {
2414 p = flex_array_get(fa, i);
2415 ret = proc_fill_cache(filp, dirent, filldir,
2417 proc_map_files_instantiate,
2424 for (; i < nr_files; i++) {
2426 * In case of error don't forget
2427 * to put rest of file refs.
2429 p = flex_array_get(fa, i);
2433 flex_array_free(fa);
2441 put_task_struct(task);
2446 static const struct file_operations proc_map_files_operations = {
2447 .read = generic_read_dir,
2448 .readdir = proc_map_files_readdir,
2449 .llseek = default_llseek,
2452 #endif /* CONFIG_CHECKPOINT_RESTORE */
2455 * /proc/pid/fd needs a special permission handler so that a process can still
2456 * access /proc/self/fd after it has executed a setuid().
2458 static int proc_fd_permission(struct inode *inode, int mask)
2460 int rv = generic_permission(inode, mask);
2463 if (task_pid(current) == proc_pid(inode))
2469 * proc directories can do almost nothing..
2471 static const struct inode_operations proc_fd_inode_operations = {
2472 .lookup = proc_lookupfd,
2473 .permission = proc_fd_permission,
2474 .setattr = proc_setattr,
2477 static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
2478 struct dentry *dentry, struct task_struct *task, const void *ptr)
2480 unsigned fd = *(unsigned *)ptr;
2481 struct inode *inode;
2482 struct proc_inode *ei;
2483 struct dentry *error = ERR_PTR(-ENOENT);
2485 inode = proc_pid_make_inode(dir->i_sb, task);
2490 inode->i_mode = S_IFREG | S_IRUSR;
2491 inode->i_fop = &proc_fdinfo_file_operations;
2492 d_set_d_op(dentry, &tid_fd_dentry_operations);
2493 d_add(dentry, inode);
2494 /* Close the race of the process dying before we return the dentry */
2495 if (tid_fd_revalidate(dentry, NULL))
2502 static struct dentry *proc_lookupfdinfo(struct inode *dir,
2503 struct dentry *dentry,
2504 struct nameidata *nd)
2506 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
2509 static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
2511 return proc_readfd_common(filp, dirent, filldir,
2512 proc_fdinfo_instantiate);
2515 static const struct file_operations proc_fdinfo_operations = {
2516 .read = generic_read_dir,
2517 .readdir = proc_readfdinfo,
2518 .llseek = default_llseek,
2522 * proc directories can do almost nothing..
2524 static const struct inode_operations proc_fdinfo_inode_operations = {
2525 .lookup = proc_lookupfdinfo,
2526 .setattr = proc_setattr,
2530 static struct dentry *proc_pident_instantiate(struct inode *dir,
2531 struct dentry *dentry, struct task_struct *task, const void *ptr)
2533 const struct pid_entry *p = ptr;
2534 struct inode *inode;
2535 struct proc_inode *ei;
2536 struct dentry *error = ERR_PTR(-ENOENT);
2538 inode = proc_pid_make_inode(dir->i_sb, task);
2543 inode->i_mode = p->mode;
2544 if (S_ISDIR(inode->i_mode))
2545 set_nlink(inode, 2); /* Use getattr to fix if necessary */
2547 inode->i_op = p->iop;
2549 inode->i_fop = p->fop;
2551 d_set_d_op(dentry, &pid_dentry_operations);
2552 d_add(dentry, inode);
2553 /* Close the race of the process dying before we return the dentry */
2554 if (pid_revalidate(dentry, NULL))
2560 static struct dentry *proc_pident_lookup(struct inode *dir,
2561 struct dentry *dentry,
2562 const struct pid_entry *ents,
2565 struct dentry *error;
2566 struct task_struct *task = get_proc_task(dir);
2567 const struct pid_entry *p, *last;
2569 error = ERR_PTR(-ENOENT);
2575 * Yes, it does not scale. And it should not. Don't add
2576 * new entries into /proc/<tgid>/ without very good reasons.
2578 last = &ents[nents - 1];
2579 for (p = ents; p <= last; p++) {
2580 if (p->len != dentry->d_name.len)
2582 if (!memcmp(dentry->d_name.name, p->name, p->len))
2588 error = proc_pident_instantiate(dir, dentry, task, p);
2590 put_task_struct(task);
2595 static int proc_pident_fill_cache(struct file *filp, void *dirent,
2596 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2598 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2599 proc_pident_instantiate, task, p);
2602 static int proc_pident_readdir(struct file *filp,
2603 void *dirent, filldir_t filldir,
2604 const struct pid_entry *ents, unsigned int nents)
2607 struct dentry *dentry = filp->f_path.dentry;
2608 struct inode *inode = dentry->d_inode;
2609 struct task_struct *task = get_proc_task(inode);
2610 const struct pid_entry *p, *last;
2623 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
2629 ino = parent_ino(dentry);
2630 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
2642 last = &ents[nents - 1];
2644 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
2653 put_task_struct(task);
2658 #ifdef CONFIG_SECURITY
2659 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2660 size_t count, loff_t *ppos)
2662 struct inode * inode = file->f_path.dentry->d_inode;
2665 struct task_struct *task = get_proc_task(inode);
2670 length = security_getprocattr(task,
2671 (char*)file->f_path.dentry->d_name.name,
2673 put_task_struct(task);
2675 length = simple_read_from_buffer(buf, count, ppos, p, length);
2680 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2681 size_t count, loff_t *ppos)
2683 struct inode * inode = file->f_path.dentry->d_inode;
2686 struct task_struct *task = get_proc_task(inode);
2691 if (count > PAGE_SIZE)
2694 /* No partial writes. */
2700 page = (char*)__get_free_page(GFP_TEMPORARY);
2705 if (copy_from_user(page, buf, count))
2708 /* Guard against adverse ptrace interaction */
2709 length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
2713 length = security_setprocattr(task,
2714 (char*)file->f_path.dentry->d_name.name,
2715 (void*)page, count);
2716 mutex_unlock(&task->signal->cred_guard_mutex);
2718 free_page((unsigned long) page);
2720 put_task_struct(task);
2725 static const struct file_operations proc_pid_attr_operations = {
2726 .read = proc_pid_attr_read,
2727 .write = proc_pid_attr_write,
2728 .llseek = generic_file_llseek,
2731 static const struct pid_entry attr_dir_stuff[] = {
2732 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2733 REG("prev", S_IRUGO, proc_pid_attr_operations),
2734 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2735 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2736 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2737 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2740 static int proc_attr_dir_readdir(struct file * filp,
2741 void * dirent, filldir_t filldir)
2743 return proc_pident_readdir(filp,dirent,filldir,
2744 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
2747 static const struct file_operations proc_attr_dir_operations = {
2748 .read = generic_read_dir,
2749 .readdir = proc_attr_dir_readdir,
2750 .llseek = default_llseek,
2753 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2754 struct dentry *dentry, struct nameidata *nd)
2756 return proc_pident_lookup(dir, dentry,
2757 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2760 static const struct inode_operations proc_attr_dir_inode_operations = {
2761 .lookup = proc_attr_dir_lookup,
2762 .getattr = pid_getattr,
2763 .setattr = proc_setattr,
2768 #ifdef CONFIG_ELF_CORE
2769 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2770 size_t count, loff_t *ppos)
2772 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2773 struct mm_struct *mm;
2774 char buffer[PROC_NUMBUF];
2782 mm = get_task_mm(task);
2784 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2785 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2786 MMF_DUMP_FILTER_SHIFT));
2788 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2791 put_task_struct(task);
2796 static ssize_t proc_coredump_filter_write(struct file *file,
2797 const char __user *buf,
2801 struct task_struct *task;
2802 struct mm_struct *mm;
2803 char buffer[PROC_NUMBUF], *end;
2810 memset(buffer, 0, sizeof(buffer));
2811 if (count > sizeof(buffer) - 1)
2812 count = sizeof(buffer) - 1;
2813 if (copy_from_user(buffer, buf, count))
2817 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2820 if (end - buffer == 0)
2824 task = get_proc_task(file->f_dentry->d_inode);
2829 mm = get_task_mm(task);
2833 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2835 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2837 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2842 put_task_struct(task);
2847 static const struct file_operations proc_coredump_filter_operations = {
2848 .read = proc_coredump_filter_read,
2849 .write = proc_coredump_filter_write,
2850 .llseek = generic_file_llseek,
2857 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2860 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2861 pid_t tgid = task_tgid_nr_ns(current, ns);
2862 char tmp[PROC_NUMBUF];
2865 sprintf(tmp, "%d", tgid);
2866 return vfs_readlink(dentry,buffer,buflen,tmp);
2869 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
2871 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2872 pid_t tgid = task_tgid_nr_ns(current, ns);
2873 char *name = ERR_PTR(-ENOENT);
2877 name = ERR_PTR(-ENOMEM);
2879 sprintf(name, "%d", tgid);
2881 nd_set_link(nd, name);
2885 static void proc_self_put_link(struct dentry *dentry, struct nameidata *nd,
2888 char *s = nd_get_link(nd);
2893 static const struct inode_operations proc_self_inode_operations = {
2894 .readlink = proc_self_readlink,
2895 .follow_link = proc_self_follow_link,
2896 .put_link = proc_self_put_link,
2902 * These are the directory entries in the root directory of /proc
2903 * that properly belong to the /proc filesystem, as they describe
2904 * describe something that is process related.
2906 static const struct pid_entry proc_base_stuff[] = {
2907 NOD("self", S_IFLNK|S_IRWXUGO,
2908 &proc_self_inode_operations, NULL, {}),
2911 static struct dentry *proc_base_instantiate(struct inode *dir,
2912 struct dentry *dentry, struct task_struct *task, const void *ptr)
2914 const struct pid_entry *p = ptr;
2915 struct inode *inode;
2916 struct proc_inode *ei;
2917 struct dentry *error;
2919 /* Allocate the inode */
2920 error = ERR_PTR(-ENOMEM);
2921 inode = new_inode(dir->i_sb);
2925 /* Initialize the inode */
2927 inode->i_ino = get_next_ino();
2928 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2931 * grab the reference to the task.
2933 ei->pid = get_task_pid(task, PIDTYPE_PID);
2937 inode->i_mode = p->mode;
2938 if (S_ISDIR(inode->i_mode))
2939 set_nlink(inode, 2);
2940 if (S_ISLNK(inode->i_mode))
2943 inode->i_op = p->iop;
2945 inode->i_fop = p->fop;
2947 d_add(dentry, inode);
2956 static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2958 struct dentry *error;
2959 struct task_struct *task = get_proc_task(dir);
2960 const struct pid_entry *p, *last;
2962 error = ERR_PTR(-ENOENT);
2967 /* Lookup the directory entry */
2968 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2969 for (p = proc_base_stuff; p <= last; p++) {
2970 if (p->len != dentry->d_name.len)
2972 if (!memcmp(dentry->d_name.name, p->name, p->len))
2978 error = proc_base_instantiate(dir, dentry, task, p);
2981 put_task_struct(task);
2986 static int proc_base_fill_cache(struct file *filp, void *dirent,
2987 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2989 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2990 proc_base_instantiate, task, p);
2993 #ifdef CONFIG_TASK_IO_ACCOUNTING
2994 static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
2996 struct task_io_accounting acct = task->ioac;
2997 unsigned long flags;
3000 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
3004 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
3009 if (whole && lock_task_sighand(task, &flags)) {
3010 struct task_struct *t = task;
3012 task_io_accounting_add(&acct, &task->signal->ioac);
3013 while_each_thread(task, t)
3014 task_io_accounting_add(&acct, &t->ioac);
3016 unlock_task_sighand(task, &flags);
3018 result = sprintf(buffer,
3023 "read_bytes: %llu\n"
3024 "write_bytes: %llu\n"
3025 "cancelled_write_bytes: %llu\n",
3026 (unsigned long long)acct.rchar,
3027 (unsigned long long)acct.wchar,
3028 (unsigned long long)acct.syscr,
3029 (unsigned long long)acct.syscw,
3030 (unsigned long long)acct.read_bytes,
3031 (unsigned long long)acct.write_bytes,
3032 (unsigned long long)acct.cancelled_write_bytes);
3034 mutex_unlock(&task->signal->cred_guard_mutex);
3038 static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
3040 return do_io_accounting(task, buffer, 0);
3043 static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
3045 return do_io_accounting(task, buffer, 1);
3047 #endif /* CONFIG_TASK_IO_ACCOUNTING */
3049 static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
3050 struct pid *pid, struct task_struct *task)
3052 int err = lock_trace(task);
3054 seq_printf(m, "%08x\n", task->personality);
3063 static const struct file_operations proc_task_operations;
3064 static const struct inode_operations proc_task_inode_operations;
3066 static const struct pid_entry tgid_base_stuff[] = {
3067 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
3068 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
3069 #ifdef CONFIG_CHECKPOINT_RESTORE
3070 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
3072 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
3073 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
3075 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
3077 REG("environ", S_IRUSR, proc_environ_operations),
3078 INF("auxv", S_IRUSR, proc_pid_auxv),
3079 ONE("status", S_IRUGO, proc_pid_status),
3080 ONE("personality", S_IRUGO, proc_pid_personality),
3081 INF("limits", S_IRUGO, proc_pid_limits),
3082 #ifdef CONFIG_SCHED_DEBUG
3083 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
3085 #ifdef CONFIG_SCHED_AUTOGROUP
3086 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
3088 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
3089 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3090 INF("syscall", S_IRUGO, proc_pid_syscall),
3092 INF("cmdline", S_IRUGO, proc_pid_cmdline),
3093 ONE("stat", S_IRUGO, proc_tgid_stat),
3094 ONE("statm", S_IRUGO, proc_pid_statm),
3095 REG("maps", S_IRUGO, proc_maps_operations),
3097 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
3099 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3100 LNK("cwd", proc_cwd_link),
3101 LNK("root", proc_root_link),
3102 LNK("exe", proc_exe_link),
3103 REG("mounts", S_IRUGO, proc_mounts_operations),
3104 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
3105 REG("mountstats", S_IRUSR, proc_mountstats_operations),
3106 #ifdef CONFIG_PROC_PAGE_MONITOR
3107 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3108 REG("smaps", S_IRUGO, proc_smaps_operations),
3109 REG("pagemap", S_IRUGO, proc_pagemap_operations),
3111 #ifdef CONFIG_SECURITY
3112 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
3114 #ifdef CONFIG_KALLSYMS
3115 INF("wchan", S_IRUGO, proc_pid_wchan),
3117 #ifdef CONFIG_STACKTRACE
3118 ONE("stack", S_IRUGO, proc_pid_stack),
3120 #ifdef CONFIG_SCHEDSTATS
3121 INF("schedstat", S_IRUGO, proc_pid_schedstat),
3123 #ifdef CONFIG_LATENCYTOP
3124 REG("latency", S_IRUGO, proc_lstats_operations),
3126 #ifdef CONFIG_PROC_PID_CPUSET
3127 REG("cpuset", S_IRUGO, proc_cpuset_operations),
3129 #ifdef CONFIG_CGROUPS
3130 REG("cgroup", S_IRUGO, proc_cgroup_operations),
3132 INF("oom_score", S_IRUGO, proc_oom_score),
3133 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
3134 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
3135 #ifdef CONFIG_AUDITSYSCALL
3136 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3137 REG("sessionid", S_IRUGO, proc_sessionid_operations),
3139 #ifdef CONFIG_FAULT_INJECTION
3140 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3142 #ifdef CONFIG_ELF_CORE
3143 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
3145 #ifdef CONFIG_TASK_IO_ACCOUNTING
3146 INF("io", S_IRUSR, proc_tgid_io_accounting),
3148 #ifdef CONFIG_HARDWALL
3149 INF("hardwall", S_IRUGO, proc_pid_hardwall),
3153 static int proc_tgid_base_readdir(struct file * filp,
3154 void * dirent, filldir_t filldir)
3156 return proc_pident_readdir(filp,dirent,filldir,
3157 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
3160 static const struct file_operations proc_tgid_base_operations = {
3161 .read = generic_read_dir,
3162 .readdir = proc_tgid_base_readdir,
3163 .llseek = default_llseek,
3166 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
3167 return proc_pident_lookup(dir, dentry,
3168 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
3171 static const struct inode_operations proc_tgid_base_inode_operations = {
3172 .lookup = proc_tgid_base_lookup,
3173 .getattr = pid_getattr,
3174 .setattr = proc_setattr,
3175 .permission = proc_pid_permission,
3178 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
3180 struct dentry *dentry, *leader, *dir;
3181 char buf[PROC_NUMBUF];
3185 name.len = snprintf(buf, sizeof(buf), "%d", pid);
3186 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
3188 shrink_dcache_parent(dentry);
3194 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
3195 leader = d_hash_and_lookup(mnt->mnt_root, &name);
3200 name.len = strlen(name.name);
3201 dir = d_hash_and_lookup(leader, &name);
3203 goto out_put_leader;
3206 name.len = snprintf(buf, sizeof(buf), "%d", pid);
3207 dentry = d_hash_and_lookup(dir, &name);
3209 shrink_dcache_parent(dentry);
3222 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
3223 * @task: task that should be flushed.
3225 * When flushing dentries from proc, one needs to flush them from global
3226 * proc (proc_mnt) and from all the namespaces' procs this task was seen
3227 * in. This call is supposed to do all of this job.
3229 * Looks in the dcache for
3231 * /proc/@tgid/task/@pid
3232 * if either directory is present flushes it and all of it'ts children
3235 * It is safe and reasonable to cache /proc entries for a task until
3236 * that task exits. After that they just clog up the dcache with
3237 * useless entries, possibly causing useful dcache entries to be
3238 * flushed instead. This routine is proved to flush those useless
3239 * dcache entries at process exit time.
3241 * NOTE: This routine is just an optimization so it does not guarantee
3242 * that no dcache entries will exist at process exit time it
3243 * just makes it very unlikely that any will persist.
3246 void proc_flush_task(struct task_struct *task)
3249 struct pid *pid, *tgid;
3252 pid = task_pid(task);
3253 tgid = task_tgid(task);
3255 for (i = 0; i <= pid->level; i++) {
3256 upid = &pid->numbers[i];
3257 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
3258 tgid->numbers[i].nr);
3261 upid = &pid->numbers[pid->level];
3263 pid_ns_release_proc(upid->ns);
3266 static struct dentry *proc_pid_instantiate(struct inode *dir,
3267 struct dentry * dentry,
3268 struct task_struct *task, const void *ptr)
3270 struct dentry *error = ERR_PTR(-ENOENT);
3271 struct inode *inode;
3273 inode = proc_pid_make_inode(dir->i_sb, task);
3277 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3278 inode->i_op = &proc_tgid_base_inode_operations;
3279 inode->i_fop = &proc_tgid_base_operations;
3280 inode->i_flags|=S_IMMUTABLE;
3282 set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
3283 ARRAY_SIZE(tgid_base_stuff)));
3285 d_set_d_op(dentry, &pid_dentry_operations);
3287 d_add(dentry, inode);
3288 /* Close the race of the process dying before we return the dentry */
3289 if (pid_revalidate(dentry, NULL))
3295 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
3297 struct dentry *result;
3298 struct task_struct *task;
3300 struct pid_namespace *ns;
3302 result = proc_base_lookup(dir, dentry);
3303 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
3306 tgid = name_to_int(dentry);
3310 ns = dentry->d_sb->s_fs_info;
3312 task = find_task_by_pid_ns(tgid, ns);
3314 get_task_struct(task);
3319 result = proc_pid_instantiate(dir, dentry, task, NULL);
3320 put_task_struct(task);
3326 * Find the first task with tgid >= tgid
3331 struct task_struct *task;
3333 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3338 put_task_struct(iter.task);
3342 pid = find_ge_pid(iter.tgid, ns);
3344 iter.tgid = pid_nr_ns(pid, ns);
3345 iter.task = pid_task(pid, PIDTYPE_PID);
3346 /* What we to know is if the pid we have find is the
3347 * pid of a thread_group_leader. Testing for task
3348 * being a thread_group_leader is the obvious thing
3349 * todo but there is a window when it fails, due to
3350 * the pid transfer logic in de_thread.
3352 * So we perform the straight forward test of seeing
3353 * if the pid we have found is the pid of a thread
3354 * group leader, and don't worry if the task we have
3355 * found doesn't happen to be a thread group leader.
3356 * As we don't care in the case of readdir.
3358 if (!iter.task || !has_group_leader_pid(iter.task)) {
3362 get_task_struct(iter.task);
3368 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
3370 static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3371 struct tgid_iter iter)
3373 char name[PROC_NUMBUF];
3374 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
3375 return proc_fill_cache(filp, dirent, filldir, name, len,
3376 proc_pid_instantiate, iter.task, NULL);
3379 static int fake_filldir(void *buf, const char *name, int namelen,
3380 loff_t offset, u64 ino, unsigned d_type)
3385 /* for the /proc/ directory itself, after non-process stuff has been done */
3386 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
3389 struct task_struct *reaper;
3390 struct tgid_iter iter;
3391 struct pid_namespace *ns;
3392 filldir_t __filldir;
3394 if (filp->f_pos >= PID_MAX_LIMIT + TGID_OFFSET)
3396 nr = filp->f_pos - FIRST_PROCESS_ENTRY;
3398 reaper = get_proc_task(filp->f_path.dentry->d_inode);
3402 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
3403 const struct pid_entry *p = &proc_base_stuff[nr];
3404 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
3408 ns = filp->f_dentry->d_sb->s_fs_info;
3410 iter.tgid = filp->f_pos - TGID_OFFSET;
3411 for (iter = next_tgid(ns, iter);
3413 iter.tgid += 1, iter = next_tgid(ns, iter)) {
3414 if (has_pid_permissions(ns, iter.task, 2))
3415 __filldir = filldir;
3417 __filldir = fake_filldir;
3419 filp->f_pos = iter.tgid + TGID_OFFSET;
3420 if (proc_pid_fill_cache(filp, dirent, __filldir, iter) < 0) {
3421 put_task_struct(iter.task);
3425 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
3427 put_task_struct(reaper);
3435 static const struct pid_entry tid_base_stuff[] = {
3436 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
3437 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
3438 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
3439 REG("environ", S_IRUSR, proc_environ_operations),
3440 INF("auxv", S_IRUSR, proc_pid_auxv),
3441 ONE("status", S_IRUGO, proc_pid_status),
3442 ONE("personality", S_IRUGO, proc_pid_personality),
3443 INF("limits", S_IRUGO, proc_pid_limits),
3444 #ifdef CONFIG_SCHED_DEBUG
3445 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
3447 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
3448 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3449 INF("syscall", S_IRUGO, proc_pid_syscall),
3451 INF("cmdline", S_IRUGO, proc_pid_cmdline),
3452 ONE("stat", S_IRUGO, proc_tid_stat),
3453 ONE("statm", S_IRUGO, proc_pid_statm),
3454 REG("maps", S_IRUGO, proc_maps_operations),
3456 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
3458 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3459 LNK("cwd", proc_cwd_link),
3460 LNK("root", proc_root_link),
3461 LNK("exe", proc_exe_link),
3462 REG("mounts", S_IRUGO, proc_mounts_operations),
3463 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
3464 #ifdef CONFIG_PROC_PAGE_MONITOR
3465 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3466 REG("smaps", S_IRUGO, proc_smaps_operations),
3467 REG("pagemap", S_IRUGO, proc_pagemap_operations),
3469 #ifdef CONFIG_SECURITY
3470 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
3472 #ifdef CONFIG_KALLSYMS
3473 INF("wchan", S_IRUGO, proc_pid_wchan),
3475 #ifdef CONFIG_STACKTRACE
3476 ONE("stack", S_IRUGO, proc_pid_stack),
3478 #ifdef CONFIG_SCHEDSTATS
3479 INF("schedstat", S_IRUGO, proc_pid_schedstat),
3481 #ifdef CONFIG_LATENCYTOP
3482 REG("latency", S_IRUGO, proc_lstats_operations),
3484 #ifdef CONFIG_PROC_PID_CPUSET
3485 REG("cpuset", S_IRUGO, proc_cpuset_operations),
3487 #ifdef CONFIG_CGROUPS
3488 REG("cgroup", S_IRUGO, proc_cgroup_operations),
3490 INF("oom_score", S_IRUGO, proc_oom_score),
3491 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
3492 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
3493 #ifdef CONFIG_AUDITSYSCALL
3494 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3495 REG("sessionid", S_IRUGO, proc_sessionid_operations),
3497 #ifdef CONFIG_FAULT_INJECTION
3498 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3500 #ifdef CONFIG_TASK_IO_ACCOUNTING
3501 INF("io", S_IRUSR, proc_tid_io_accounting),
3503 #ifdef CONFIG_HARDWALL
3504 INF("hardwall", S_IRUGO, proc_pid_hardwall),
3508 static int proc_tid_base_readdir(struct file * filp,
3509 void * dirent, filldir_t filldir)
3511 return proc_pident_readdir(filp,dirent,filldir,
3512 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
3515 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
3516 return proc_pident_lookup(dir, dentry,
3517 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3520 static const struct file_operations proc_tid_base_operations = {
3521 .read = generic_read_dir,
3522 .readdir = proc_tid_base_readdir,
3523 .llseek = default_llseek,
3526 static const struct inode_operations proc_tid_base_inode_operations = {
3527 .lookup = proc_tid_base_lookup,
3528 .getattr = pid_getattr,
3529 .setattr = proc_setattr,
3532 static struct dentry *proc_task_instantiate(struct inode *dir,
3533 struct dentry *dentry, struct task_struct *task, const void *ptr)
3535 struct dentry *error = ERR_PTR(-ENOENT);
3536 struct inode *inode;
3537 inode = proc_pid_make_inode(dir->i_sb, task);
3541 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3542 inode->i_op = &proc_tid_base_inode_operations;
3543 inode->i_fop = &proc_tid_base_operations;
3544 inode->i_flags|=S_IMMUTABLE;
3546 set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3547 ARRAY_SIZE(tid_base_stuff)));
3549 d_set_d_op(dentry, &pid_dentry_operations);
3551 d_add(dentry, inode);
3552 /* Close the race of the process dying before we return the dentry */
3553 if (pid_revalidate(dentry, NULL))
3559 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
3561 struct dentry *result = ERR_PTR(-ENOENT);
3562 struct task_struct *task;
3563 struct task_struct *leader = get_proc_task(dir);
3565 struct pid_namespace *ns;
3570 tid = name_to_int(dentry);
3574 ns = dentry->d_sb->s_fs_info;
3576 task = find_task_by_pid_ns(tid, ns);
3578 get_task_struct(task);
3582 if (!same_thread_group(leader, task))
3585 result = proc_task_instantiate(dir, dentry, task, NULL);
3587 put_task_struct(task);
3589 put_task_struct(leader);
3595 * Find the first tid of a thread group to return to user space.
3597 * Usually this is just the thread group leader, but if the users
3598 * buffer was too small or there was a seek into the middle of the
3599 * directory we have more work todo.
3601 * In the case of a short read we start with find_task_by_pid.
3603 * In the case of a seek we start with the leader and walk nr
3606 static struct task_struct *first_tid(struct task_struct *leader,
3607 int tid, int nr, struct pid_namespace *ns)
3609 struct task_struct *pos;
3612 /* Attempt to start with the pid of a thread */
3613 if (tid && (nr > 0)) {
3614 pos = find_task_by_pid_ns(tid, ns);
3615 if (pos && (pos->group_leader == leader))
3619 /* If nr exceeds the number of threads there is nothing todo */
3621 if (nr && nr >= get_nr_threads(leader))
3624 /* If we haven't found our starting place yet start
3625 * with the leader and walk nr threads forward.
3627 for (pos = leader; nr > 0; --nr) {
3628 pos = next_thread(pos);
3629 if (pos == leader) {
3635 get_task_struct(pos);
3642 * Find the next thread in the thread list.
3643 * Return NULL if there is an error or no next thread.
3645 * The reference to the input task_struct is released.
3647 static struct task_struct *next_tid(struct task_struct *start)
3649 struct task_struct *pos = NULL;
3651 if (pid_alive(start)) {
3652 pos = next_thread(start);
3653 if (thread_group_leader(pos))
3656 get_task_struct(pos);
3659 put_task_struct(start);
3663 static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3664 struct task_struct *task, int tid)
3666 char name[PROC_NUMBUF];
3667 int len = snprintf(name, sizeof(name), "%d", tid);
3668 return proc_fill_cache(filp, dirent, filldir, name, len,
3669 proc_task_instantiate, task, NULL);
3672 /* for the /proc/TGID/task/ directories */
3673 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
3675 struct dentry *dentry = filp->f_path.dentry;
3676 struct inode *inode = dentry->d_inode;
3677 struct task_struct *leader = NULL;
3678 struct task_struct *task;
3679 int retval = -ENOENT;
3682 struct pid_namespace *ns;
3684 task = get_proc_task(inode);
3688 if (pid_alive(task)) {
3689 leader = task->group_leader;
3690 get_task_struct(leader);
3693 put_task_struct(task);
3698 switch ((unsigned long)filp->f_pos) {
3701 if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) < 0)
3706 ino = parent_ino(dentry);
3707 if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) < 0)
3713 /* f_version caches the tgid value that the last readdir call couldn't
3714 * return. lseek aka telldir automagically resets f_version to 0.
3716 ns = filp->f_dentry->d_sb->s_fs_info;
3717 tid = (int)filp->f_version;
3718 filp->f_version = 0;
3719 for (task = first_tid(leader, tid, filp->f_pos - 2, ns);
3721 task = next_tid(task), filp->f_pos++) {
3722 tid = task_pid_nr_ns(task, ns);
3723 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
3724 /* returning this tgid failed, save it as the first
3725 * pid for the next readir call */
3726 filp->f_version = (u64)tid;
3727 put_task_struct(task);
3732 put_task_struct(leader);
3737 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3739 struct inode *inode = dentry->d_inode;
3740 struct task_struct *p = get_proc_task(inode);
3741 generic_fillattr(inode, stat);
3744 stat->nlink += get_nr_threads(p);
3751 static const struct inode_operations proc_task_inode_operations = {
3752 .lookup = proc_task_lookup,
3753 .getattr = proc_task_getattr,
3754 .setattr = proc_setattr,
3755 .permission = proc_pid_permission,
3758 static const struct file_operations proc_task_operations = {
3759 .read = generic_read_dir,
3760 .readdir = proc_task_readdir,
3761 .llseek = default_llseek,