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.
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.
44 * Smaps information related to shared, private, clean and dirty pages.
47 * Overall revision about smaps.
50 #include <asm/uaccess.h>
52 #include <linux/errno.h>
53 #include <linux/time.h>
54 #include <linux/proc_fs.h>
55 #include <linux/stat.h>
56 #include <linux/task_io_accounting_ops.h>
57 #include <linux/init.h>
58 #include <linux/capability.h>
59 #include <linux/file.h>
60 #include <linux/fdtable.h>
61 #include <linux/string.h>
62 #include <linux/seq_file.h>
63 #include <linux/namei.h>
64 #include <linux/mnt_namespace.h>
66 #include <linux/swap.h>
67 #include <linux/rcupdate.h>
68 #include <linux/kallsyms.h>
69 #include <linux/stacktrace.h>
70 #include <linux/resource.h>
71 #include <linux/module.h>
72 #include <linux/mount.h>
73 #include <linux/security.h>
74 #include <linux/ptrace.h>
75 #include <linux/tracehook.h>
76 #include <linux/printk.h>
77 #include <linux/cgroup.h>
78 #include <linux/cpuset.h>
79 #include <linux/audit.h>
80 #include <linux/poll.h>
81 #include <linux/nsproxy.h>
82 #include <linux/oom.h>
83 #include <linux/elf.h>
84 #include <linux/pid_namespace.h>
85 #include <linux/user_namespace.h>
86 #include <linux/fs_struct.h>
87 #include <linux/slab.h>
88 #include <linux/flex_array.h>
89 #include <linux/posix-timers.h>
90 #ifdef CONFIG_HARDWALL
91 #include <asm/hardwall.h>
93 #include <trace/events/oom.h>
98 * Implementing inode permission operations in /proc is almost
99 * certainly an error. Permission checks need to happen during
100 * each system call not at open time. The reason is that most of
101 * what we wish to check for permissions in /proc varies at runtime.
103 * The classic example of a problem is opening file descriptors
104 * in /proc for a task before it execs a suid executable.
111 const struct inode_operations *iop;
112 const struct file_operations *fop;
116 #define NOD(NAME, MODE, IOP, FOP, OP) { \
118 .len = sizeof(NAME) - 1, \
125 #define DIR(NAME, MODE, iops, fops) \
126 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
127 #define LNK(NAME, get_link) \
128 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
129 &proc_pid_link_inode_operations, NULL, \
130 { .proc_get_link = get_link } )
131 #define REG(NAME, MODE, fops) \
132 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
133 #define INF(NAME, MODE, read) \
134 NOD(NAME, (S_IFREG|(MODE)), \
135 NULL, &proc_info_file_operations, \
136 { .proc_read = read } )
137 #define ONE(NAME, MODE, show) \
138 NOD(NAME, (S_IFREG|(MODE)), \
139 NULL, &proc_single_file_operations, \
140 { .proc_show = show } )
143 * Count the number of hardlinks for the pid_entry table, excluding the .
146 static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
153 for (i = 0; i < n; ++i) {
154 if (S_ISDIR(entries[i].mode))
161 static int get_task_root(struct task_struct *task, struct path *root)
163 int result = -ENOENT;
167 get_fs_root(task->fs, root);
174 static int proc_cwd_link(struct dentry *dentry, struct path *path)
176 struct task_struct *task = get_proc_task(dentry->d_inode);
177 int result = -ENOENT;
182 get_fs_pwd(task->fs, path);
186 put_task_struct(task);
191 static int proc_root_link(struct dentry *dentry, struct path *path)
193 struct task_struct *task = get_proc_task(dentry->d_inode);
194 int result = -ENOENT;
197 result = get_task_root(task, path);
198 put_task_struct(task);
203 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
207 struct mm_struct *mm = get_task_mm(task);
211 goto out_mm; /* Shh! No looking before we're done */
213 len = mm->arg_end - mm->arg_start;
218 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
220 // If the nul at the end of args has been overwritten, then
221 // assume application is using setproctitle(3).
222 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
223 len = strnlen(buffer, res);
227 len = mm->env_end - mm->env_start;
228 if (len > PAGE_SIZE - res)
229 len = PAGE_SIZE - res;
230 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
231 res = strnlen(buffer, res);
240 static int proc_pid_auxv(struct task_struct *task, char *buffer)
242 struct mm_struct *mm = mm_access(task, PTRACE_MODE_READ);
243 int res = PTR_ERR(mm);
244 if (mm && !IS_ERR(mm)) {
245 unsigned int nwords = 0;
248 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
249 res = nwords * sizeof(mm->saved_auxv[0]);
252 memcpy(buffer, mm->saved_auxv, res);
259 #ifdef CONFIG_KALLSYMS
261 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
262 * Returns the resolved symbol. If that fails, simply return the address.
264 static int proc_pid_wchan(struct task_struct *task, char *buffer)
267 char symname[KSYM_NAME_LEN];
269 wchan = get_wchan(task);
271 if (lookup_symbol_name(wchan, symname) < 0)
272 if (!ptrace_may_access(task, PTRACE_MODE_READ))
275 return sprintf(buffer, "%lu", wchan);
277 return sprintf(buffer, "%s", symname);
279 #endif /* CONFIG_KALLSYMS */
281 static int lock_trace(struct task_struct *task)
283 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
286 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH)) {
287 mutex_unlock(&task->signal->cred_guard_mutex);
293 static void unlock_trace(struct task_struct *task)
295 mutex_unlock(&task->signal->cred_guard_mutex);
298 #ifdef CONFIG_STACKTRACE
300 #define MAX_STACK_TRACE_DEPTH 64
302 static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
303 struct pid *pid, struct task_struct *task)
305 struct stack_trace trace;
306 unsigned long *entries;
310 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
314 trace.nr_entries = 0;
315 trace.max_entries = MAX_STACK_TRACE_DEPTH;
316 trace.entries = entries;
319 err = lock_trace(task);
321 save_stack_trace_tsk(task, &trace);
323 for (i = 0; i < trace.nr_entries; i++) {
324 seq_printf(m, "[<%pK>] %pS\n",
325 (void *)entries[i], (void *)entries[i]);
335 #ifdef CONFIG_SCHEDSTATS
337 * Provides /proc/PID/schedstat
339 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
341 return sprintf(buffer, "%llu %llu %lu\n",
342 (unsigned long long)task->se.sum_exec_runtime,
343 (unsigned long long)task->sched_info.run_delay,
344 task->sched_info.pcount);
348 #ifdef CONFIG_LATENCYTOP
349 static int lstats_show_proc(struct seq_file *m, void *v)
352 struct inode *inode = m->private;
353 struct task_struct *task = get_proc_task(inode);
357 seq_puts(m, "Latency Top version : v0.1\n");
358 for (i = 0; i < 32; i++) {
359 struct latency_record *lr = &task->latency_record[i];
360 if (lr->backtrace[0]) {
362 seq_printf(m, "%i %li %li",
363 lr->count, lr->time, lr->max);
364 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
365 unsigned long bt = lr->backtrace[q];
370 seq_printf(m, " %ps", (void *)bt);
376 put_task_struct(task);
380 static int lstats_open(struct inode *inode, struct file *file)
382 return single_open(file, lstats_show_proc, inode);
385 static ssize_t lstats_write(struct file *file, const char __user *buf,
386 size_t count, loff_t *offs)
388 struct task_struct *task = get_proc_task(file_inode(file));
392 clear_all_latency_tracing(task);
393 put_task_struct(task);
398 static const struct file_operations proc_lstats_operations = {
401 .write = lstats_write,
403 .release = single_release,
408 #ifdef CONFIG_CGROUPS
409 static int cgroup_open(struct inode *inode, struct file *file)
411 struct pid *pid = PROC_I(inode)->pid;
412 return single_open(file, proc_cgroup_show, pid);
415 static const struct file_operations proc_cgroup_operations = {
419 .release = single_release,
423 #ifdef CONFIG_PROC_PID_CPUSET
425 static int cpuset_open(struct inode *inode, struct file *file)
427 struct pid *pid = PROC_I(inode)->pid;
428 return single_open(file, proc_cpuset_show, pid);
431 static const struct file_operations proc_cpuset_operations = {
435 .release = single_release,
439 static int proc_oom_score(struct task_struct *task, char *buffer)
441 unsigned long totalpages = totalram_pages + total_swap_pages;
442 unsigned long points = 0;
444 read_lock(&tasklist_lock);
446 points = oom_badness(task, NULL, NULL, totalpages) *
448 read_unlock(&tasklist_lock);
449 return sprintf(buffer, "%lu\n", points);
457 static const struct limit_names lnames[RLIM_NLIMITS] = {
458 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
459 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
460 [RLIMIT_DATA] = {"Max data size", "bytes"},
461 [RLIMIT_STACK] = {"Max stack size", "bytes"},
462 [RLIMIT_CORE] = {"Max core file size", "bytes"},
463 [RLIMIT_RSS] = {"Max resident set", "bytes"},
464 [RLIMIT_NPROC] = {"Max processes", "processes"},
465 [RLIMIT_NOFILE] = {"Max open files", "files"},
466 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
467 [RLIMIT_AS] = {"Max address space", "bytes"},
468 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
469 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
470 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
471 [RLIMIT_NICE] = {"Max nice priority", NULL},
472 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
473 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
476 /* Display limits for a process */
477 static int proc_pid_limits(struct task_struct *task, char *buffer)
482 char *bufptr = buffer;
484 struct rlimit rlim[RLIM_NLIMITS];
486 if (!lock_task_sighand(task, &flags))
488 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
489 unlock_task_sighand(task, &flags);
492 * print the file header
494 count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
495 "Limit", "Soft Limit", "Hard Limit", "Units");
497 for (i = 0; i < RLIM_NLIMITS; i++) {
498 if (rlim[i].rlim_cur == RLIM_INFINITY)
499 count += sprintf(&bufptr[count], "%-25s %-20s ",
500 lnames[i].name, "unlimited");
502 count += sprintf(&bufptr[count], "%-25s %-20lu ",
503 lnames[i].name, rlim[i].rlim_cur);
505 if (rlim[i].rlim_max == RLIM_INFINITY)
506 count += sprintf(&bufptr[count], "%-20s ", "unlimited");
508 count += sprintf(&bufptr[count], "%-20lu ",
512 count += sprintf(&bufptr[count], "%-10s\n",
515 count += sprintf(&bufptr[count], "\n");
521 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
522 static int proc_pid_syscall(struct task_struct *task, char *buffer)
525 unsigned long args[6], sp, pc;
526 int res = lock_trace(task);
530 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
531 res = sprintf(buffer, "running\n");
533 res = sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
535 res = sprintf(buffer,
536 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
538 args[0], args[1], args[2], args[3], args[4], args[5],
543 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
545 /************************************************************************/
546 /* Here the fs part begins */
547 /************************************************************************/
549 /* permission checks */
550 static int proc_fd_access_allowed(struct inode *inode)
552 struct task_struct *task;
554 /* Allow access to a task's file descriptors if it is us or we
555 * may use ptrace attach to the process and find out that
558 task = get_proc_task(inode);
560 allowed = ptrace_may_access(task, PTRACE_MODE_READ);
561 put_task_struct(task);
566 int proc_setattr(struct dentry *dentry, struct iattr *attr)
569 struct inode *inode = dentry->d_inode;
571 if (attr->ia_valid & ATTR_MODE)
574 error = inode_change_ok(inode, attr);
578 setattr_copy(inode, attr);
579 mark_inode_dirty(inode);
584 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
585 * or euid/egid (for hide_pid_min=2)?
587 static bool has_pid_permissions(struct pid_namespace *pid,
588 struct task_struct *task,
591 if (pid->hide_pid < hide_pid_min)
593 if (in_group_p(pid->pid_gid))
595 return ptrace_may_access(task, PTRACE_MODE_READ);
599 static int proc_pid_permission(struct inode *inode, int mask)
601 struct pid_namespace *pid = inode->i_sb->s_fs_info;
602 struct task_struct *task;
605 task = get_proc_task(inode);
608 has_perms = has_pid_permissions(pid, task, 1);
609 put_task_struct(task);
612 if (pid->hide_pid == 2) {
614 * Let's make getdents(), stat(), and open()
615 * consistent with each other. If a process
616 * may not stat() a file, it shouldn't be seen
624 return generic_permission(inode, mask);
629 static const struct inode_operations proc_def_inode_operations = {
630 .setattr = proc_setattr,
633 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
635 static ssize_t proc_info_read(struct file * file, char __user * buf,
636 size_t count, loff_t *ppos)
638 struct inode * inode = file_inode(file);
641 struct task_struct *task = get_proc_task(inode);
647 if (count > PROC_BLOCK_SIZE)
648 count = PROC_BLOCK_SIZE;
651 if (!(page = __get_free_page(GFP_TEMPORARY)))
654 length = PROC_I(inode)->op.proc_read(task, (char*)page);
657 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
660 put_task_struct(task);
665 static const struct file_operations proc_info_file_operations = {
666 .read = proc_info_read,
667 .llseek = generic_file_llseek,
670 static int proc_single_show(struct seq_file *m, void *v)
672 struct inode *inode = m->private;
673 struct pid_namespace *ns;
675 struct task_struct *task;
678 ns = inode->i_sb->s_fs_info;
679 pid = proc_pid(inode);
680 task = get_pid_task(pid, PIDTYPE_PID);
684 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
686 put_task_struct(task);
690 static int proc_single_open(struct inode *inode, struct file *filp)
692 return single_open(filp, proc_single_show, inode);
695 static const struct file_operations proc_single_file_operations = {
696 .open = proc_single_open,
699 .release = single_release,
702 static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
704 struct task_struct *task = get_proc_task(file_inode(file));
705 struct mm_struct *mm;
710 mm = mm_access(task, mode);
711 put_task_struct(task);
717 /* ensure this mm_struct can't be freed */
718 atomic_inc(&mm->mm_count);
719 /* but do not pin its memory */
723 file->private_data = mm;
728 static int mem_open(struct inode *inode, struct file *file)
730 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
732 /* OK to pass negative loff_t, we can catch out-of-range */
733 file->f_mode |= FMODE_UNSIGNED_OFFSET;
738 static ssize_t mem_rw(struct file *file, char __user *buf,
739 size_t count, loff_t *ppos, int write)
741 struct mm_struct *mm = file->private_data;
742 unsigned long addr = *ppos;
749 page = (char *)__get_free_page(GFP_TEMPORARY);
754 if (!atomic_inc_not_zero(&mm->mm_users))
758 int this_len = min_t(int, count, PAGE_SIZE);
760 if (write && copy_from_user(page, buf, this_len)) {
765 this_len = access_remote_vm(mm, addr, page, this_len, write);
772 if (!write && copy_to_user(buf, page, this_len)) {
786 free_page((unsigned long) page);
790 static ssize_t mem_read(struct file *file, char __user *buf,
791 size_t count, loff_t *ppos)
793 return mem_rw(file, buf, count, ppos, 0);
796 static ssize_t mem_write(struct file *file, const char __user *buf,
797 size_t count, loff_t *ppos)
799 return mem_rw(file, (char __user*)buf, count, ppos, 1);
802 loff_t mem_lseek(struct file *file, loff_t offset, int orig)
806 file->f_pos = offset;
809 file->f_pos += offset;
814 force_successful_syscall_return();
818 static int mem_release(struct inode *inode, struct file *file)
820 struct mm_struct *mm = file->private_data;
826 static const struct file_operations proc_mem_operations = {
831 .release = mem_release,
834 static int environ_open(struct inode *inode, struct file *file)
836 return __mem_open(inode, file, PTRACE_MODE_READ);
839 static ssize_t environ_read(struct file *file, char __user *buf,
840 size_t count, loff_t *ppos)
843 unsigned long src = *ppos;
845 struct mm_struct *mm = file->private_data;
850 page = (char *)__get_free_page(GFP_TEMPORARY);
855 if (!atomic_inc_not_zero(&mm->mm_users))
858 size_t this_len, max_len;
861 if (src >= (mm->env_end - mm->env_start))
864 this_len = mm->env_end - (mm->env_start + src);
866 max_len = min_t(size_t, PAGE_SIZE, count);
867 this_len = min(max_len, this_len);
869 retval = access_remote_vm(mm, (mm->env_start + src),
877 if (copy_to_user(buf, page, retval)) {
891 free_page((unsigned long) page);
895 static const struct file_operations proc_environ_operations = {
896 .open = environ_open,
897 .read = environ_read,
898 .llseek = generic_file_llseek,
899 .release = mem_release,
902 static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
905 struct task_struct *task = get_proc_task(file_inode(file));
906 char buffer[PROC_NUMBUF];
907 int oom_adj = OOM_ADJUST_MIN;
913 if (lock_task_sighand(task, &flags)) {
914 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
915 oom_adj = OOM_ADJUST_MAX;
917 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
919 unlock_task_sighand(task, &flags);
921 put_task_struct(task);
922 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
923 return simple_read_from_buffer(buf, count, ppos, buffer, len);
926 static ssize_t oom_adj_write(struct file *file, const char __user *buf,
927 size_t count, loff_t *ppos)
929 struct task_struct *task;
930 char buffer[PROC_NUMBUF];
935 memset(buffer, 0, sizeof(buffer));
936 if (count > sizeof(buffer) - 1)
937 count = sizeof(buffer) - 1;
938 if (copy_from_user(buffer, buf, count)) {
943 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
946 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
947 oom_adj != OOM_DISABLE) {
952 task = get_proc_task(file_inode(file));
964 if (!lock_task_sighand(task, &flags)) {
970 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
971 * value is always attainable.
973 if (oom_adj == OOM_ADJUST_MAX)
974 oom_adj = OOM_SCORE_ADJ_MAX;
976 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
978 if (oom_adj < task->signal->oom_score_adj &&
979 !capable(CAP_SYS_RESOURCE)) {
985 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
986 * /proc/pid/oom_score_adj instead.
988 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
989 current->comm, task_pid_nr(current), task_pid_nr(task),
992 task->signal->oom_score_adj = oom_adj;
993 trace_oom_score_adj_update(task);
995 unlock_task_sighand(task, &flags);
998 put_task_struct(task);
1000 return err < 0 ? err : count;
1003 static const struct file_operations proc_oom_adj_operations = {
1004 .read = oom_adj_read,
1005 .write = oom_adj_write,
1006 .llseek = generic_file_llseek,
1009 static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1010 size_t count, loff_t *ppos)
1012 struct task_struct *task = get_proc_task(file_inode(file));
1013 char buffer[PROC_NUMBUF];
1014 short oom_score_adj = OOM_SCORE_ADJ_MIN;
1015 unsigned long flags;
1020 if (lock_task_sighand(task, &flags)) {
1021 oom_score_adj = task->signal->oom_score_adj;
1022 unlock_task_sighand(task, &flags);
1024 put_task_struct(task);
1025 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
1026 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1029 static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1030 size_t count, loff_t *ppos)
1032 struct task_struct *task;
1033 char buffer[PROC_NUMBUF];
1034 unsigned long flags;
1038 memset(buffer, 0, sizeof(buffer));
1039 if (count > sizeof(buffer) - 1)
1040 count = sizeof(buffer) - 1;
1041 if (copy_from_user(buffer, buf, count)) {
1046 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
1049 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
1050 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1055 task = get_proc_task(file_inode(file));
1067 if (!lock_task_sighand(task, &flags)) {
1072 if ((short)oom_score_adj < task->signal->oom_score_adj_min &&
1073 !capable(CAP_SYS_RESOURCE)) {
1078 task->signal->oom_score_adj = (short)oom_score_adj;
1079 if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
1080 task->signal->oom_score_adj_min = (short)oom_score_adj;
1081 trace_oom_score_adj_update(task);
1084 unlock_task_sighand(task, &flags);
1087 put_task_struct(task);
1089 return err < 0 ? err : count;
1092 static const struct file_operations proc_oom_score_adj_operations = {
1093 .read = oom_score_adj_read,
1094 .write = oom_score_adj_write,
1095 .llseek = default_llseek,
1098 #ifdef CONFIG_AUDITSYSCALL
1099 #define TMPBUFLEN 21
1100 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1101 size_t count, loff_t *ppos)
1103 struct inode * inode = file_inode(file);
1104 struct task_struct *task = get_proc_task(inode);
1106 char tmpbuf[TMPBUFLEN];
1110 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1111 from_kuid(file->f_cred->user_ns,
1112 audit_get_loginuid(task)));
1113 put_task_struct(task);
1114 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1117 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1118 size_t count, loff_t *ppos)
1120 struct inode * inode = file_inode(file);
1127 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1133 if (count >= PAGE_SIZE)
1134 count = PAGE_SIZE - 1;
1137 /* No partial writes. */
1140 page = (char*)__get_free_page(GFP_TEMPORARY);
1144 if (copy_from_user(page, buf, count))
1148 loginuid = simple_strtoul(page, &tmp, 10);
1155 /* is userspace tring to explicitly UNSET the loginuid? */
1156 if (loginuid == AUDIT_UID_UNSET) {
1157 kloginuid = INVALID_UID;
1159 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
1160 if (!uid_valid(kloginuid)) {
1166 length = audit_set_loginuid(kloginuid);
1167 if (likely(length == 0))
1171 free_page((unsigned long) page);
1175 static const struct file_operations proc_loginuid_operations = {
1176 .read = proc_loginuid_read,
1177 .write = proc_loginuid_write,
1178 .llseek = generic_file_llseek,
1181 static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1182 size_t count, loff_t *ppos)
1184 struct inode * inode = file_inode(file);
1185 struct task_struct *task = get_proc_task(inode);
1187 char tmpbuf[TMPBUFLEN];
1191 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1192 audit_get_sessionid(task));
1193 put_task_struct(task);
1194 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1197 static const struct file_operations proc_sessionid_operations = {
1198 .read = proc_sessionid_read,
1199 .llseek = generic_file_llseek,
1203 #ifdef CONFIG_FAULT_INJECTION
1204 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1205 size_t count, loff_t *ppos)
1207 struct task_struct *task = get_proc_task(file_inode(file));
1208 char buffer[PROC_NUMBUF];
1214 make_it_fail = task->make_it_fail;
1215 put_task_struct(task);
1217 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1219 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1222 static ssize_t proc_fault_inject_write(struct file * file,
1223 const char __user * buf, size_t count, loff_t *ppos)
1225 struct task_struct *task;
1226 char buffer[PROC_NUMBUF], *end;
1229 if (!capable(CAP_SYS_RESOURCE))
1231 memset(buffer, 0, sizeof(buffer));
1232 if (count > sizeof(buffer) - 1)
1233 count = sizeof(buffer) - 1;
1234 if (copy_from_user(buffer, buf, count))
1236 make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1239 task = get_proc_task(file_inode(file));
1242 task->make_it_fail = make_it_fail;
1243 put_task_struct(task);
1248 static const struct file_operations proc_fault_inject_operations = {
1249 .read = proc_fault_inject_read,
1250 .write = proc_fault_inject_write,
1251 .llseek = generic_file_llseek,
1256 #ifdef CONFIG_SCHED_DEBUG
1258 * Print out various scheduling related per-task fields:
1260 static int sched_show(struct seq_file *m, void *v)
1262 struct inode *inode = m->private;
1263 struct task_struct *p;
1265 p = get_proc_task(inode);
1268 proc_sched_show_task(p, m);
1276 sched_write(struct file *file, const char __user *buf,
1277 size_t count, loff_t *offset)
1279 struct inode *inode = file_inode(file);
1280 struct task_struct *p;
1282 p = get_proc_task(inode);
1285 proc_sched_set_task(p);
1292 static int sched_open(struct inode *inode, struct file *filp)
1294 return single_open(filp, sched_show, inode);
1297 static const struct file_operations proc_pid_sched_operations = {
1300 .write = sched_write,
1301 .llseek = seq_lseek,
1302 .release = single_release,
1307 #ifdef CONFIG_SCHED_AUTOGROUP
1309 * Print out autogroup related information:
1311 static int sched_autogroup_show(struct seq_file *m, void *v)
1313 struct inode *inode = m->private;
1314 struct task_struct *p;
1316 p = get_proc_task(inode);
1319 proc_sched_autogroup_show_task(p, m);
1327 sched_autogroup_write(struct file *file, const char __user *buf,
1328 size_t count, loff_t *offset)
1330 struct inode *inode = file_inode(file);
1331 struct task_struct *p;
1332 char buffer[PROC_NUMBUF];
1336 memset(buffer, 0, sizeof(buffer));
1337 if (count > sizeof(buffer) - 1)
1338 count = sizeof(buffer) - 1;
1339 if (copy_from_user(buffer, buf, count))
1342 err = kstrtoint(strstrip(buffer), 0, &nice);
1346 p = get_proc_task(inode);
1350 err = proc_sched_autogroup_set_nice(p, nice);
1359 static int sched_autogroup_open(struct inode *inode, struct file *filp)
1363 ret = single_open(filp, sched_autogroup_show, NULL);
1365 struct seq_file *m = filp->private_data;
1372 static const struct file_operations proc_pid_sched_autogroup_operations = {
1373 .open = sched_autogroup_open,
1375 .write = sched_autogroup_write,
1376 .llseek = seq_lseek,
1377 .release = single_release,
1380 #endif /* CONFIG_SCHED_AUTOGROUP */
1382 static ssize_t comm_write(struct file *file, const char __user *buf,
1383 size_t count, loff_t *offset)
1385 struct inode *inode = file_inode(file);
1386 struct task_struct *p;
1387 char buffer[TASK_COMM_LEN];
1388 const size_t maxlen = sizeof(buffer) - 1;
1390 memset(buffer, 0, sizeof(buffer));
1391 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
1394 p = get_proc_task(inode);
1398 if (same_thread_group(current, p))
1399 set_task_comm(p, buffer);
1408 static int comm_show(struct seq_file *m, void *v)
1410 struct inode *inode = m->private;
1411 struct task_struct *p;
1413 p = get_proc_task(inode);
1418 seq_printf(m, "%s\n", p->comm);
1426 static int comm_open(struct inode *inode, struct file *filp)
1428 return single_open(filp, comm_show, inode);
1431 static const struct file_operations proc_pid_set_comm_operations = {
1434 .write = comm_write,
1435 .llseek = seq_lseek,
1436 .release = single_release,
1439 static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
1441 struct task_struct *task;
1442 struct mm_struct *mm;
1443 struct file *exe_file;
1445 task = get_proc_task(dentry->d_inode);
1448 mm = get_task_mm(task);
1449 put_task_struct(task);
1452 exe_file = get_mm_exe_file(mm);
1455 *exe_path = exe_file->f_path;
1456 path_get(&exe_file->f_path);
1463 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1465 struct inode *inode = dentry->d_inode;
1467 int error = -EACCES;
1469 /* Are we allowed to snoop on the tasks file descriptors? */
1470 if (!proc_fd_access_allowed(inode))
1473 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1477 nd_jump_link(nd, &path);
1480 return ERR_PTR(error);
1483 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1485 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
1492 pathname = d_path(path, tmp, PAGE_SIZE);
1493 len = PTR_ERR(pathname);
1494 if (IS_ERR(pathname))
1496 len = tmp + PAGE_SIZE - 1 - pathname;
1500 if (copy_to_user(buffer, pathname, len))
1503 free_page((unsigned long)tmp);
1507 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1509 int error = -EACCES;
1510 struct inode *inode = dentry->d_inode;
1513 /* Are we allowed to snoop on the tasks file descriptors? */
1514 if (!proc_fd_access_allowed(inode))
1517 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1521 error = do_proc_readlink(&path, buffer, buflen);
1527 const struct inode_operations proc_pid_link_inode_operations = {
1528 .readlink = proc_pid_readlink,
1529 .follow_link = proc_pid_follow_link,
1530 .setattr = proc_setattr,
1534 /* building an inode */
1536 struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1538 struct inode * inode;
1539 struct proc_inode *ei;
1540 const struct cred *cred;
1542 /* We need a new inode */
1544 inode = new_inode(sb);
1550 inode->i_ino = get_next_ino();
1551 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1552 inode->i_op = &proc_def_inode_operations;
1555 * grab the reference to task.
1557 ei->pid = get_task_pid(task, PIDTYPE_PID);
1561 if (task_dumpable(task)) {
1563 cred = __task_cred(task);
1564 inode->i_uid = cred->euid;
1565 inode->i_gid = cred->egid;
1568 security_task_to_inode(task, inode);
1578 int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1580 struct inode *inode = dentry->d_inode;
1581 struct task_struct *task;
1582 const struct cred *cred;
1583 struct pid_namespace *pid = dentry->d_sb->s_fs_info;
1585 generic_fillattr(inode, stat);
1588 stat->uid = GLOBAL_ROOT_UID;
1589 stat->gid = GLOBAL_ROOT_GID;
1590 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1592 if (!has_pid_permissions(pid, task, 2)) {
1595 * This doesn't prevent learning whether PID exists,
1596 * it only makes getattr() consistent with readdir().
1600 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1601 task_dumpable(task)) {
1602 cred = __task_cred(task);
1603 stat->uid = cred->euid;
1604 stat->gid = cred->egid;
1614 * Exceptional case: normally we are not allowed to unhash a busy
1615 * directory. In this case, however, we can do it - no aliasing problems
1616 * due to the way we treat inodes.
1618 * Rewrite the inode's ownerships here because the owning task may have
1619 * performed a setuid(), etc.
1621 * Before the /proc/pid/status file was created the only way to read
1622 * the effective uid of a /process was to stat /proc/pid. Reading
1623 * /proc/pid/status is slow enough that procps and other packages
1624 * kept stating /proc/pid. To keep the rules in /proc simple I have
1625 * made this apply to all per process world readable and executable
1628 int pid_revalidate(struct dentry *dentry, unsigned int flags)
1630 struct inode *inode;
1631 struct task_struct *task;
1632 const struct cred *cred;
1634 if (flags & LOOKUP_RCU)
1637 inode = dentry->d_inode;
1638 task = get_proc_task(inode);
1641 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1642 task_dumpable(task)) {
1644 cred = __task_cred(task);
1645 inode->i_uid = cred->euid;
1646 inode->i_gid = cred->egid;
1649 inode->i_uid = GLOBAL_ROOT_UID;
1650 inode->i_gid = GLOBAL_ROOT_GID;
1652 inode->i_mode &= ~(S_ISUID | S_ISGID);
1653 security_task_to_inode(task, inode);
1654 put_task_struct(task);
1661 int pid_delete_dentry(const struct dentry *dentry)
1663 /* Is the task we represent dead?
1664 * If so, then don't put the dentry on the lru list,
1665 * kill it immediately.
1667 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1670 const struct dentry_operations pid_dentry_operations =
1672 .d_revalidate = pid_revalidate,
1673 .d_delete = pid_delete_dentry,
1679 * Fill a directory entry.
1681 * If possible create the dcache entry and derive our inode number and
1682 * file type from dcache entry.
1684 * Since all of the proc inode numbers are dynamically generated, the inode
1685 * numbers do not exist until the inode is cache. This means creating the
1686 * the dcache entry in readdir is necessary to keep the inode numbers
1687 * reported by readdir in sync with the inode numbers reported
1690 bool proc_fill_cache(struct file *file, struct dir_context *ctx,
1691 const char *name, int len,
1692 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1694 struct dentry *child, *dir = file->f_path.dentry;
1695 struct qstr qname = QSTR_INIT(name, len);
1696 struct inode *inode;
1700 child = d_hash_and_lookup(dir, &qname);
1702 child = d_alloc(dir, &qname);
1704 goto end_instantiate;
1705 if (instantiate(dir->d_inode, child, task, ptr) < 0) {
1707 goto end_instantiate;
1710 inode = child->d_inode;
1712 type = inode->i_mode >> 12;
1714 return dir_emit(ctx, name, len, ino, type);
1717 return dir_emit(ctx, name, len, 1, DT_UNKNOWN);
1720 #ifdef CONFIG_CHECKPOINT_RESTORE
1723 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1724 * which represent vma start and end addresses.
1726 static int dname_to_vma_addr(struct dentry *dentry,
1727 unsigned long *start, unsigned long *end)
1729 if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2)
1735 static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
1737 unsigned long vm_start, vm_end;
1738 bool exact_vma_exists = false;
1739 struct mm_struct *mm = NULL;
1740 struct task_struct *task;
1741 const struct cred *cred;
1742 struct inode *inode;
1745 if (flags & LOOKUP_RCU)
1748 if (!capable(CAP_SYS_ADMIN)) {
1753 inode = dentry->d_inode;
1754 task = get_proc_task(inode);
1758 mm = mm_access(task, PTRACE_MODE_READ);
1759 if (IS_ERR_OR_NULL(mm))
1762 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1763 down_read(&mm->mmap_sem);
1764 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1765 up_read(&mm->mmap_sem);
1770 if (exact_vma_exists) {
1771 if (task_dumpable(task)) {
1773 cred = __task_cred(task);
1774 inode->i_uid = cred->euid;
1775 inode->i_gid = cred->egid;
1778 inode->i_uid = GLOBAL_ROOT_UID;
1779 inode->i_gid = GLOBAL_ROOT_GID;
1781 security_task_to_inode(task, inode);
1786 put_task_struct(task);
1795 static const struct dentry_operations tid_map_files_dentry_operations = {
1796 .d_revalidate = map_files_d_revalidate,
1797 .d_delete = pid_delete_dentry,
1800 static int proc_map_files_get_link(struct dentry *dentry, struct path *path)
1802 unsigned long vm_start, vm_end;
1803 struct vm_area_struct *vma;
1804 struct task_struct *task;
1805 struct mm_struct *mm;
1809 task = get_proc_task(dentry->d_inode);
1813 mm = get_task_mm(task);
1814 put_task_struct(task);
1818 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
1822 down_read(&mm->mmap_sem);
1823 vma = find_exact_vma(mm, vm_start, vm_end);
1824 if (vma && vma->vm_file) {
1825 *path = vma->vm_file->f_path;
1829 up_read(&mm->mmap_sem);
1837 struct map_files_info {
1840 unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
1844 proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
1845 struct task_struct *task, const void *ptr)
1847 fmode_t mode = (fmode_t)(unsigned long)ptr;
1848 struct proc_inode *ei;
1849 struct inode *inode;
1851 inode = proc_pid_make_inode(dir->i_sb, task);
1856 ei->op.proc_get_link = proc_map_files_get_link;
1858 inode->i_op = &proc_pid_link_inode_operations;
1860 inode->i_mode = S_IFLNK;
1862 if (mode & FMODE_READ)
1863 inode->i_mode |= S_IRUSR;
1864 if (mode & FMODE_WRITE)
1865 inode->i_mode |= S_IWUSR;
1867 d_set_d_op(dentry, &tid_map_files_dentry_operations);
1868 d_add(dentry, inode);
1873 static struct dentry *proc_map_files_lookup(struct inode *dir,
1874 struct dentry *dentry, unsigned int flags)
1876 unsigned long vm_start, vm_end;
1877 struct vm_area_struct *vma;
1878 struct task_struct *task;
1880 struct mm_struct *mm;
1883 if (!capable(CAP_SYS_ADMIN))
1887 task = get_proc_task(dir);
1892 if (!ptrace_may_access(task, PTRACE_MODE_READ))
1896 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
1899 mm = get_task_mm(task);
1903 down_read(&mm->mmap_sem);
1904 vma = find_exact_vma(mm, vm_start, vm_end);
1909 result = proc_map_files_instantiate(dir, dentry, task,
1910 (void *)(unsigned long)vma->vm_file->f_mode);
1913 up_read(&mm->mmap_sem);
1916 put_task_struct(task);
1918 return ERR_PTR(result);
1921 static const struct inode_operations proc_map_files_inode_operations = {
1922 .lookup = proc_map_files_lookup,
1923 .permission = proc_fd_permission,
1924 .setattr = proc_setattr,
1928 proc_map_files_readdir(struct file *file, struct dir_context *ctx)
1930 struct vm_area_struct *vma;
1931 struct task_struct *task;
1932 struct mm_struct *mm;
1933 unsigned long nr_files, pos, i;
1934 struct flex_array *fa = NULL;
1935 struct map_files_info info;
1936 struct map_files_info *p;
1940 if (!capable(CAP_SYS_ADMIN))
1944 task = get_proc_task(file_inode(file));
1949 if (!ptrace_may_access(task, PTRACE_MODE_READ))
1953 if (!dir_emit_dots(file, ctx))
1956 mm = get_task_mm(task);
1959 down_read(&mm->mmap_sem);
1964 * We need two passes here:
1966 * 1) Collect vmas of mapped files with mmap_sem taken
1967 * 2) Release mmap_sem and instantiate entries
1969 * otherwise we get lockdep complained, since filldir()
1970 * routine might require mmap_sem taken in might_fault().
1973 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
1974 if (vma->vm_file && ++pos > ctx->pos)
1979 fa = flex_array_alloc(sizeof(info), nr_files,
1981 if (!fa || flex_array_prealloc(fa, 0, nr_files,
1985 flex_array_free(fa);
1986 up_read(&mm->mmap_sem);
1990 for (i = 0, vma = mm->mmap, pos = 2; vma;
1991 vma = vma->vm_next) {
1994 if (++pos <= ctx->pos)
1997 info.mode = vma->vm_file->f_mode;
1998 info.len = snprintf(info.name,
1999 sizeof(info.name), "%lx-%lx",
2000 vma->vm_start, vma->vm_end);
2001 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
2005 up_read(&mm->mmap_sem);
2007 for (i = 0; i < nr_files; i++) {
2008 p = flex_array_get(fa, i);
2009 if (!proc_fill_cache(file, ctx,
2011 proc_map_files_instantiate,
2013 (void *)(unsigned long)p->mode))
2018 flex_array_free(fa);
2022 put_task_struct(task);
2027 static const struct file_operations proc_map_files_operations = {
2028 .read = generic_read_dir,
2029 .iterate = proc_map_files_readdir,
2030 .llseek = default_llseek,
2033 struct timers_private {
2035 struct task_struct *task;
2036 struct sighand_struct *sighand;
2037 struct pid_namespace *ns;
2038 unsigned long flags;
2041 static void *timers_start(struct seq_file *m, loff_t *pos)
2043 struct timers_private *tp = m->private;
2045 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2047 return ERR_PTR(-ESRCH);
2049 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2051 return ERR_PTR(-ESRCH);
2053 return seq_list_start(&tp->task->signal->posix_timers, *pos);
2056 static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2058 struct timers_private *tp = m->private;
2059 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2062 static void timers_stop(struct seq_file *m, void *v)
2064 struct timers_private *tp = m->private;
2067 unlock_task_sighand(tp->task, &tp->flags);
2072 put_task_struct(tp->task);
2077 static int show_timer(struct seq_file *m, void *v)
2079 struct k_itimer *timer;
2080 struct timers_private *tp = m->private;
2082 static char *nstr[] = {
2083 [SIGEV_SIGNAL] = "signal",
2084 [SIGEV_NONE] = "none",
2085 [SIGEV_THREAD] = "thread",
2088 timer = list_entry((struct list_head *)v, struct k_itimer, list);
2089 notify = timer->it_sigev_notify;
2091 seq_printf(m, "ID: %d\n", timer->it_id);
2092 seq_printf(m, "signal: %d/%p\n", timer->sigq->info.si_signo,
2093 timer->sigq->info.si_value.sival_ptr);
2094 seq_printf(m, "notify: %s/%s.%d\n",
2095 nstr[notify & ~SIGEV_THREAD_ID],
2096 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2097 pid_nr_ns(timer->it_pid, tp->ns));
2098 seq_printf(m, "ClockID: %d\n", timer->it_clock);
2103 static const struct seq_operations proc_timers_seq_ops = {
2104 .start = timers_start,
2105 .next = timers_next,
2106 .stop = timers_stop,
2110 static int proc_timers_open(struct inode *inode, struct file *file)
2112 struct timers_private *tp;
2114 tp = __seq_open_private(file, &proc_timers_seq_ops,
2115 sizeof(struct timers_private));
2119 tp->pid = proc_pid(inode);
2120 tp->ns = inode->i_sb->s_fs_info;
2124 static const struct file_operations proc_timers_operations = {
2125 .open = proc_timers_open,
2127 .llseek = seq_lseek,
2128 .release = seq_release_private,
2130 #endif /* CONFIG_CHECKPOINT_RESTORE */
2132 static int proc_pident_instantiate(struct inode *dir,
2133 struct dentry *dentry, struct task_struct *task, const void *ptr)
2135 const struct pid_entry *p = ptr;
2136 struct inode *inode;
2137 struct proc_inode *ei;
2139 inode = proc_pid_make_inode(dir->i_sb, task);
2144 inode->i_mode = p->mode;
2145 if (S_ISDIR(inode->i_mode))
2146 set_nlink(inode, 2); /* Use getattr to fix if necessary */
2148 inode->i_op = p->iop;
2150 inode->i_fop = p->fop;
2152 d_set_d_op(dentry, &pid_dentry_operations);
2153 d_add(dentry, inode);
2154 /* Close the race of the process dying before we return the dentry */
2155 if (pid_revalidate(dentry, 0))
2161 static struct dentry *proc_pident_lookup(struct inode *dir,
2162 struct dentry *dentry,
2163 const struct pid_entry *ents,
2167 struct task_struct *task = get_proc_task(dir);
2168 const struct pid_entry *p, *last;
2176 * Yes, it does not scale. And it should not. Don't add
2177 * new entries into /proc/<tgid>/ without very good reasons.
2179 last = &ents[nents - 1];
2180 for (p = ents; p <= last; p++) {
2181 if (p->len != dentry->d_name.len)
2183 if (!memcmp(dentry->d_name.name, p->name, p->len))
2189 error = proc_pident_instantiate(dir, dentry, task, p);
2191 put_task_struct(task);
2193 return ERR_PTR(error);
2196 static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
2197 const struct pid_entry *ents, unsigned int nents)
2199 struct task_struct *task = get_proc_task(file_inode(file));
2200 const struct pid_entry *p;
2205 if (!dir_emit_dots(file, ctx))
2208 if (ctx->pos >= nents + 2)
2211 for (p = ents + (ctx->pos - 2); p <= ents + nents - 1; p++) {
2212 if (!proc_fill_cache(file, ctx, p->name, p->len,
2213 proc_pident_instantiate, task, p))
2218 put_task_struct(task);
2222 #ifdef CONFIG_SECURITY
2223 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2224 size_t count, loff_t *ppos)
2226 struct inode * inode = file_inode(file);
2229 struct task_struct *task = get_proc_task(inode);
2234 length = security_getprocattr(task,
2235 (char*)file->f_path.dentry->d_name.name,
2237 put_task_struct(task);
2239 length = simple_read_from_buffer(buf, count, ppos, p, length);
2244 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2245 size_t count, loff_t *ppos)
2247 struct inode * inode = file_inode(file);
2250 struct task_struct *task = get_proc_task(inode);
2255 if (count > PAGE_SIZE)
2258 /* No partial writes. */
2264 page = (char*)__get_free_page(GFP_TEMPORARY);
2269 if (copy_from_user(page, buf, count))
2272 /* Guard against adverse ptrace interaction */
2273 length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
2277 length = security_setprocattr(task,
2278 (char*)file->f_path.dentry->d_name.name,
2279 (void*)page, count);
2280 mutex_unlock(&task->signal->cred_guard_mutex);
2282 free_page((unsigned long) page);
2284 put_task_struct(task);
2289 static const struct file_operations proc_pid_attr_operations = {
2290 .read = proc_pid_attr_read,
2291 .write = proc_pid_attr_write,
2292 .llseek = generic_file_llseek,
2295 static const struct pid_entry attr_dir_stuff[] = {
2296 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2297 REG("prev", S_IRUGO, proc_pid_attr_operations),
2298 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2299 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2300 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2301 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2304 static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
2306 return proc_pident_readdir(file, ctx,
2307 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2310 static const struct file_operations proc_attr_dir_operations = {
2311 .read = generic_read_dir,
2312 .iterate = proc_attr_dir_readdir,
2313 .llseek = default_llseek,
2316 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2317 struct dentry *dentry, unsigned int flags)
2319 return proc_pident_lookup(dir, dentry,
2320 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2323 static const struct inode_operations proc_attr_dir_inode_operations = {
2324 .lookup = proc_attr_dir_lookup,
2325 .getattr = pid_getattr,
2326 .setattr = proc_setattr,
2331 #ifdef CONFIG_ELF_CORE
2332 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2333 size_t count, loff_t *ppos)
2335 struct task_struct *task = get_proc_task(file_inode(file));
2336 struct mm_struct *mm;
2337 char buffer[PROC_NUMBUF];
2345 mm = get_task_mm(task);
2347 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2348 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2349 MMF_DUMP_FILTER_SHIFT));
2351 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2354 put_task_struct(task);
2359 static ssize_t proc_coredump_filter_write(struct file *file,
2360 const char __user *buf,
2364 struct task_struct *task;
2365 struct mm_struct *mm;
2366 char buffer[PROC_NUMBUF], *end;
2373 memset(buffer, 0, sizeof(buffer));
2374 if (count > sizeof(buffer) - 1)
2375 count = sizeof(buffer) - 1;
2376 if (copy_from_user(buffer, buf, count))
2380 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2383 if (end - buffer == 0)
2387 task = get_proc_task(file_inode(file));
2392 mm = get_task_mm(task);
2396 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2398 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2400 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2405 put_task_struct(task);
2410 static const struct file_operations proc_coredump_filter_operations = {
2411 .read = proc_coredump_filter_read,
2412 .write = proc_coredump_filter_write,
2413 .llseek = generic_file_llseek,
2417 #ifdef CONFIG_TASK_IO_ACCOUNTING
2418 static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
2420 struct task_io_accounting acct = task->ioac;
2421 unsigned long flags;
2424 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2428 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
2433 if (whole && lock_task_sighand(task, &flags)) {
2434 struct task_struct *t = task;
2436 task_io_accounting_add(&acct, &task->signal->ioac);
2437 while_each_thread(task, t)
2438 task_io_accounting_add(&acct, &t->ioac);
2440 unlock_task_sighand(task, &flags);
2442 result = sprintf(buffer,
2447 "read_bytes: %llu\n"
2448 "write_bytes: %llu\n"
2449 "cancelled_write_bytes: %llu\n",
2450 (unsigned long long)acct.rchar,
2451 (unsigned long long)acct.wchar,
2452 (unsigned long long)acct.syscr,
2453 (unsigned long long)acct.syscw,
2454 (unsigned long long)acct.read_bytes,
2455 (unsigned long long)acct.write_bytes,
2456 (unsigned long long)acct.cancelled_write_bytes);
2458 mutex_unlock(&task->signal->cred_guard_mutex);
2462 static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2464 return do_io_accounting(task, buffer, 0);
2467 static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2469 return do_io_accounting(task, buffer, 1);
2471 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2473 #ifdef CONFIG_USER_NS
2474 static int proc_id_map_open(struct inode *inode, struct file *file,
2475 struct seq_operations *seq_ops)
2477 struct user_namespace *ns = NULL;
2478 struct task_struct *task;
2479 struct seq_file *seq;
2482 task = get_proc_task(inode);
2485 ns = get_user_ns(task_cred_xxx(task, user_ns));
2487 put_task_struct(task);
2492 ret = seq_open(file, seq_ops);
2496 seq = file->private_data;
2506 static int proc_id_map_release(struct inode *inode, struct file *file)
2508 struct seq_file *seq = file->private_data;
2509 struct user_namespace *ns = seq->private;
2511 return seq_release(inode, file);
2514 static int proc_uid_map_open(struct inode *inode, struct file *file)
2516 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2519 static int proc_gid_map_open(struct inode *inode, struct file *file)
2521 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2524 static int proc_projid_map_open(struct inode *inode, struct file *file)
2526 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2529 static const struct file_operations proc_uid_map_operations = {
2530 .open = proc_uid_map_open,
2531 .write = proc_uid_map_write,
2533 .llseek = seq_lseek,
2534 .release = proc_id_map_release,
2537 static const struct file_operations proc_gid_map_operations = {
2538 .open = proc_gid_map_open,
2539 .write = proc_gid_map_write,
2541 .llseek = seq_lseek,
2542 .release = proc_id_map_release,
2545 static const struct file_operations proc_projid_map_operations = {
2546 .open = proc_projid_map_open,
2547 .write = proc_projid_map_write,
2549 .llseek = seq_lseek,
2550 .release = proc_id_map_release,
2552 #endif /* CONFIG_USER_NS */
2554 static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2555 struct pid *pid, struct task_struct *task)
2557 int err = lock_trace(task);
2559 seq_printf(m, "%08x\n", task->personality);
2568 static const struct file_operations proc_task_operations;
2569 static const struct inode_operations proc_task_inode_operations;
2571 static const struct pid_entry tgid_base_stuff[] = {
2572 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2573 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2574 #ifdef CONFIG_CHECKPOINT_RESTORE
2575 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
2577 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2578 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
2580 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2582 REG("environ", S_IRUSR, proc_environ_operations),
2583 INF("auxv", S_IRUSR, proc_pid_auxv),
2584 ONE("status", S_IRUGO, proc_pid_status),
2585 ONE("personality", S_IRUGO, proc_pid_personality),
2586 INF("limits", S_IRUGO, proc_pid_limits),
2587 #ifdef CONFIG_SCHED_DEBUG
2588 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2590 #ifdef CONFIG_SCHED_AUTOGROUP
2591 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2593 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2594 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2595 INF("syscall", S_IRUGO, proc_pid_syscall),
2597 INF("cmdline", S_IRUGO, proc_pid_cmdline),
2598 ONE("stat", S_IRUGO, proc_tgid_stat),
2599 ONE("statm", S_IRUGO, proc_pid_statm),
2600 REG("maps", S_IRUGO, proc_pid_maps_operations),
2602 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
2604 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2605 LNK("cwd", proc_cwd_link),
2606 LNK("root", proc_root_link),
2607 LNK("exe", proc_exe_link),
2608 REG("mounts", S_IRUGO, proc_mounts_operations),
2609 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2610 REG("mountstats", S_IRUSR, proc_mountstats_operations),
2611 #ifdef CONFIG_PROC_PAGE_MONITOR
2612 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2613 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
2614 REG("pagemap", S_IRUGO, proc_pagemap_operations),
2616 #ifdef CONFIG_SECURITY
2617 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
2619 #ifdef CONFIG_KALLSYMS
2620 INF("wchan", S_IRUGO, proc_pid_wchan),
2622 #ifdef CONFIG_STACKTRACE
2623 ONE("stack", S_IRUGO, proc_pid_stack),
2625 #ifdef CONFIG_SCHEDSTATS
2626 INF("schedstat", S_IRUGO, proc_pid_schedstat),
2628 #ifdef CONFIG_LATENCYTOP
2629 REG("latency", S_IRUGO, proc_lstats_operations),
2631 #ifdef CONFIG_PROC_PID_CPUSET
2632 REG("cpuset", S_IRUGO, proc_cpuset_operations),
2634 #ifdef CONFIG_CGROUPS
2635 REG("cgroup", S_IRUGO, proc_cgroup_operations),
2637 INF("oom_score", S_IRUGO, proc_oom_score),
2638 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
2639 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
2640 #ifdef CONFIG_AUDITSYSCALL
2641 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2642 REG("sessionid", S_IRUGO, proc_sessionid_operations),
2644 #ifdef CONFIG_FAULT_INJECTION
2645 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2647 #ifdef CONFIG_ELF_CORE
2648 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
2650 #ifdef CONFIG_TASK_IO_ACCOUNTING
2651 INF("io", S_IRUSR, proc_tgid_io_accounting),
2653 #ifdef CONFIG_HARDWALL
2654 INF("hardwall", S_IRUGO, proc_pid_hardwall),
2656 #ifdef CONFIG_USER_NS
2657 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2658 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
2659 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
2661 #ifdef CONFIG_CHECKPOINT_RESTORE
2662 REG("timers", S_IRUGO, proc_timers_operations),
2666 static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
2668 return proc_pident_readdir(file, ctx,
2669 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2672 static const struct file_operations proc_tgid_base_operations = {
2673 .read = generic_read_dir,
2674 .iterate = proc_tgid_base_readdir,
2675 .llseek = default_llseek,
2678 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2680 return proc_pident_lookup(dir, dentry,
2681 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2684 static const struct inode_operations proc_tgid_base_inode_operations = {
2685 .lookup = proc_tgid_base_lookup,
2686 .getattr = pid_getattr,
2687 .setattr = proc_setattr,
2688 .permission = proc_pid_permission,
2691 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
2693 struct dentry *dentry, *leader, *dir;
2694 char buf[PROC_NUMBUF];
2698 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2699 /* no ->d_hash() rejects on procfs */
2700 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
2702 shrink_dcache_parent(dentry);
2708 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2709 leader = d_hash_and_lookup(mnt->mnt_root, &name);
2714 name.len = strlen(name.name);
2715 dir = d_hash_and_lookup(leader, &name);
2717 goto out_put_leader;
2720 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2721 dentry = d_hash_and_lookup(dir, &name);
2723 shrink_dcache_parent(dentry);
2736 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2737 * @task: task that should be flushed.
2739 * When flushing dentries from proc, one needs to flush them from global
2740 * proc (proc_mnt) and from all the namespaces' procs this task was seen
2741 * in. This call is supposed to do all of this job.
2743 * Looks in the dcache for
2745 * /proc/@tgid/task/@pid
2746 * if either directory is present flushes it and all of it'ts children
2749 * It is safe and reasonable to cache /proc entries for a task until
2750 * that task exits. After that they just clog up the dcache with
2751 * useless entries, possibly causing useful dcache entries to be
2752 * flushed instead. This routine is proved to flush those useless
2753 * dcache entries at process exit time.
2755 * NOTE: This routine is just an optimization so it does not guarantee
2756 * that no dcache entries will exist at process exit time it
2757 * just makes it very unlikely that any will persist.
2760 void proc_flush_task(struct task_struct *task)
2763 struct pid *pid, *tgid;
2766 pid = task_pid(task);
2767 tgid = task_tgid(task);
2769 for (i = 0; i <= pid->level; i++) {
2770 upid = &pid->numbers[i];
2771 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
2772 tgid->numbers[i].nr);
2776 static int proc_pid_instantiate(struct inode *dir,
2777 struct dentry * dentry,
2778 struct task_struct *task, const void *ptr)
2780 struct inode *inode;
2782 inode = proc_pid_make_inode(dir->i_sb, task);
2786 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2787 inode->i_op = &proc_tgid_base_inode_operations;
2788 inode->i_fop = &proc_tgid_base_operations;
2789 inode->i_flags|=S_IMMUTABLE;
2791 set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
2792 ARRAY_SIZE(tgid_base_stuff)));
2794 d_set_d_op(dentry, &pid_dentry_operations);
2796 d_add(dentry, inode);
2797 /* Close the race of the process dying before we return the dentry */
2798 if (pid_revalidate(dentry, 0))
2804 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
2807 struct task_struct *task;
2809 struct pid_namespace *ns;
2811 tgid = name_to_int(dentry);
2815 ns = dentry->d_sb->s_fs_info;
2817 task = find_task_by_pid_ns(tgid, ns);
2819 get_task_struct(task);
2824 result = proc_pid_instantiate(dir, dentry, task, NULL);
2825 put_task_struct(task);
2827 return ERR_PTR(result);
2831 * Find the first task with tgid >= tgid
2836 struct task_struct *task;
2838 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2843 put_task_struct(iter.task);
2847 pid = find_ge_pid(iter.tgid, ns);
2849 iter.tgid = pid_nr_ns(pid, ns);
2850 iter.task = pid_task(pid, PIDTYPE_PID);
2851 /* What we to know is if the pid we have find is the
2852 * pid of a thread_group_leader. Testing for task
2853 * being a thread_group_leader is the obvious thing
2854 * todo but there is a window when it fails, due to
2855 * the pid transfer logic in de_thread.
2857 * So we perform the straight forward test of seeing
2858 * if the pid we have found is the pid of a thread
2859 * group leader, and don't worry if the task we have
2860 * found doesn't happen to be a thread group leader.
2861 * As we don't care in the case of readdir.
2863 if (!iter.task || !has_group_leader_pid(iter.task)) {
2867 get_task_struct(iter.task);
2873 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + 1)
2875 /* for the /proc/ directory itself, after non-process stuff has been done */
2876 int proc_pid_readdir(struct file *file, struct dir_context *ctx)
2878 struct tgid_iter iter;
2879 struct pid_namespace *ns = file->f_dentry->d_sb->s_fs_info;
2880 loff_t pos = ctx->pos;
2882 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
2885 if (pos == TGID_OFFSET - 1) {
2886 struct inode *inode = ns->proc_self->d_inode;
2887 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
2891 iter.tgid = pos - TGID_OFFSET;
2894 for (iter = next_tgid(ns, iter);
2896 iter.tgid += 1, iter = next_tgid(ns, iter)) {
2897 char name[PROC_NUMBUF];
2899 if (!has_pid_permissions(ns, iter.task, 2))
2902 len = snprintf(name, sizeof(name), "%d", iter.tgid);
2903 ctx->pos = iter.tgid + TGID_OFFSET;
2904 if (!proc_fill_cache(file, ctx, name, len,
2905 proc_pid_instantiate, iter.task, NULL)) {
2906 put_task_struct(iter.task);
2910 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
2917 static const struct pid_entry tid_base_stuff[] = {
2918 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2919 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2920 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
2921 REG("environ", S_IRUSR, proc_environ_operations),
2922 INF("auxv", S_IRUSR, proc_pid_auxv),
2923 ONE("status", S_IRUGO, proc_pid_status),
2924 ONE("personality", S_IRUGO, proc_pid_personality),
2925 INF("limits", S_IRUGO, proc_pid_limits),
2926 #ifdef CONFIG_SCHED_DEBUG
2927 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2929 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2930 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2931 INF("syscall", S_IRUGO, proc_pid_syscall),
2933 INF("cmdline", S_IRUGO, proc_pid_cmdline),
2934 ONE("stat", S_IRUGO, proc_tid_stat),
2935 ONE("statm", S_IRUGO, proc_pid_statm),
2936 REG("maps", S_IRUGO, proc_tid_maps_operations),
2937 #ifdef CONFIG_CHECKPOINT_RESTORE
2938 REG("children", S_IRUGO, proc_tid_children_operations),
2941 REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
2943 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2944 LNK("cwd", proc_cwd_link),
2945 LNK("root", proc_root_link),
2946 LNK("exe", proc_exe_link),
2947 REG("mounts", S_IRUGO, proc_mounts_operations),
2948 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2949 #ifdef CONFIG_PROC_PAGE_MONITOR
2950 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2951 REG("smaps", S_IRUGO, proc_tid_smaps_operations),
2952 REG("pagemap", S_IRUGO, proc_pagemap_operations),
2954 #ifdef CONFIG_SECURITY
2955 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
2957 #ifdef CONFIG_KALLSYMS
2958 INF("wchan", S_IRUGO, proc_pid_wchan),
2960 #ifdef CONFIG_STACKTRACE
2961 ONE("stack", S_IRUGO, proc_pid_stack),
2963 #ifdef CONFIG_SCHEDSTATS
2964 INF("schedstat", S_IRUGO, proc_pid_schedstat),
2966 #ifdef CONFIG_LATENCYTOP
2967 REG("latency", S_IRUGO, proc_lstats_operations),
2969 #ifdef CONFIG_PROC_PID_CPUSET
2970 REG("cpuset", S_IRUGO, proc_cpuset_operations),
2972 #ifdef CONFIG_CGROUPS
2973 REG("cgroup", S_IRUGO, proc_cgroup_operations),
2975 INF("oom_score", S_IRUGO, proc_oom_score),
2976 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
2977 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
2978 #ifdef CONFIG_AUDITSYSCALL
2979 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2980 REG("sessionid", S_IRUGO, proc_sessionid_operations),
2982 #ifdef CONFIG_FAULT_INJECTION
2983 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2985 #ifdef CONFIG_TASK_IO_ACCOUNTING
2986 INF("io", S_IRUSR, proc_tid_io_accounting),
2988 #ifdef CONFIG_HARDWALL
2989 INF("hardwall", S_IRUGO, proc_pid_hardwall),
2991 #ifdef CONFIG_USER_NS
2992 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2993 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
2994 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
2998 static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
3000 return proc_pident_readdir(file, ctx,
3001 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3004 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3006 return proc_pident_lookup(dir, dentry,
3007 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3010 static const struct file_operations proc_tid_base_operations = {
3011 .read = generic_read_dir,
3012 .iterate = proc_tid_base_readdir,
3013 .llseek = default_llseek,
3016 static const struct inode_operations proc_tid_base_inode_operations = {
3017 .lookup = proc_tid_base_lookup,
3018 .getattr = pid_getattr,
3019 .setattr = proc_setattr,
3022 static int proc_task_instantiate(struct inode *dir,
3023 struct dentry *dentry, struct task_struct *task, const void *ptr)
3025 struct inode *inode;
3026 inode = proc_pid_make_inode(dir->i_sb, task);
3030 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3031 inode->i_op = &proc_tid_base_inode_operations;
3032 inode->i_fop = &proc_tid_base_operations;
3033 inode->i_flags|=S_IMMUTABLE;
3035 set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3036 ARRAY_SIZE(tid_base_stuff)));
3038 d_set_d_op(dentry, &pid_dentry_operations);
3040 d_add(dentry, inode);
3041 /* Close the race of the process dying before we return the dentry */
3042 if (pid_revalidate(dentry, 0))
3048 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
3050 int result = -ENOENT;
3051 struct task_struct *task;
3052 struct task_struct *leader = get_proc_task(dir);
3054 struct pid_namespace *ns;
3059 tid = name_to_int(dentry);
3063 ns = dentry->d_sb->s_fs_info;
3065 task = find_task_by_pid_ns(tid, ns);
3067 get_task_struct(task);
3071 if (!same_thread_group(leader, task))
3074 result = proc_task_instantiate(dir, dentry, task, NULL);
3076 put_task_struct(task);
3078 put_task_struct(leader);
3080 return ERR_PTR(result);
3084 * Find the first tid of a thread group to return to user space.
3086 * Usually this is just the thread group leader, but if the users
3087 * buffer was too small or there was a seek into the middle of the
3088 * directory we have more work todo.
3090 * In the case of a short read we start with find_task_by_pid.
3092 * In the case of a seek we start with the leader and walk nr
3095 static struct task_struct *first_tid(struct task_struct *leader,
3096 int tid, int nr, struct pid_namespace *ns)
3098 struct task_struct *pos;
3101 /* Attempt to start with the pid of a thread */
3102 if (tid && (nr > 0)) {
3103 pos = find_task_by_pid_ns(tid, ns);
3104 if (pos && (pos->group_leader == leader))
3108 /* If nr exceeds the number of threads there is nothing todo */
3110 if (nr && nr >= get_nr_threads(leader))
3113 /* If we haven't found our starting place yet start
3114 * with the leader and walk nr threads forward.
3116 for (pos = leader; nr > 0; --nr) {
3117 pos = next_thread(pos);
3118 if (pos == leader) {
3124 get_task_struct(pos);
3131 * Find the next thread in the thread list.
3132 * Return NULL if there is an error or no next thread.
3134 * The reference to the input task_struct is released.
3136 static struct task_struct *next_tid(struct task_struct *start)
3138 struct task_struct *pos = NULL;
3140 if (pid_alive(start)) {
3141 pos = next_thread(start);
3142 if (thread_group_leader(pos))
3145 get_task_struct(pos);
3148 put_task_struct(start);
3152 /* for the /proc/TGID/task/ directories */
3153 static int proc_task_readdir(struct file *file, struct dir_context *ctx)
3155 struct task_struct *leader = NULL;
3156 struct task_struct *task = get_proc_task(file_inode(file));
3157 struct pid_namespace *ns;
3163 if (pid_alive(task)) {
3164 leader = task->group_leader;
3165 get_task_struct(leader);
3168 put_task_struct(task);
3172 if (!dir_emit_dots(file, ctx))
3175 /* f_version caches the tgid value that the last readdir call couldn't
3176 * return. lseek aka telldir automagically resets f_version to 0.
3178 ns = file->f_dentry->d_sb->s_fs_info;
3179 tid = (int)file->f_version;
3180 file->f_version = 0;
3181 for (task = first_tid(leader, tid, ctx->pos - 2, ns);
3183 task = next_tid(task), ctx->pos++) {
3184 char name[PROC_NUMBUF];
3186 tid = task_pid_nr_ns(task, ns);
3187 len = snprintf(name, sizeof(name), "%d", tid);
3188 if (!proc_fill_cache(file, ctx, name, len,
3189 proc_task_instantiate, task, NULL)) {
3190 /* returning this tgid failed, save it as the first
3191 * pid for the next readir call */
3192 file->f_version = (u64)tid;
3193 put_task_struct(task);
3198 put_task_struct(leader);
3202 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3204 struct inode *inode = dentry->d_inode;
3205 struct task_struct *p = get_proc_task(inode);
3206 generic_fillattr(inode, stat);
3209 stat->nlink += get_nr_threads(p);
3216 static const struct inode_operations proc_task_inode_operations = {
3217 .lookup = proc_task_lookup,
3218 .getattr = proc_task_getattr,
3219 .setattr = proc_setattr,
3220 .permission = proc_pid_permission,
3223 static const struct file_operations proc_task_operations = {
3224 .read = generic_read_dir,
3225 .iterate = proc_task_readdir,
3226 .llseek = default_llseek,