4 * Copyright (C) 1991, 1992 Linus Torvalds
6 * proc base directory handling functions
8 * 1999, Al Viro. Rewritten. Now it covers the whole per-process part.
9 * Instead of using magical inumbers to determine the kind of object
10 * we allocate and fill in-core inodes upon lookup. They don't even
11 * go into icache. We cache the reference to task_struct upon lookup too.
12 * Eventually it should become a filesystem in its own. We don't use the
13 * rest of procfs anymore.
19 * Bruna Moreira <bruna.moreira@indt.org.br>
20 * Edjard Mota <edjard.mota@indt.org.br>
21 * Ilias Biris <ilias.biris@indt.org.br>
22 * Mauricio Lin <mauricio.lin@indt.org.br>
24 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
26 * A new process specific entry (smaps) included in /proc. It shows the
27 * size of rss for each memory area. The maps entry lacks information
28 * about physical memory size (rss) for each mapped file, i.e.,
29 * rss information for executables and library files.
30 * This additional information is useful for any tools that need to know
31 * about physical memory consumption for a process specific library.
35 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36 * Pud inclusion in the page table walking.
40 * 10LE Instituto Nokia de Tecnologia - INdT:
41 * A better way to walks through the page table as suggested by Hugh Dickins.
43 * Simo Piiroinen <simo.piiroinen@nokia.com>:
44 * Smaps information related to shared, private, clean and dirty pages.
46 * Paul Mundt <paul.mundt@nokia.com>:
47 * Overall revision about smaps.
50 #include <asm/uaccess.h>
52 #include <linux/errno.h>
53 #include <linux/time.h>
54 #include <linux/proc_fs.h>
55 #include <linux/stat.h>
56 #include <linux/task_io_accounting_ops.h>
57 #include <linux/init.h>
58 #include <linux/capability.h>
59 #include <linux/file.h>
60 #include <linux/fdtable.h>
61 #include <linux/string.h>
62 #include <linux/seq_file.h>
63 #include <linux/namei.h>
64 #include <linux/mnt_namespace.h>
66 #include <linux/swap.h>
67 #include <linux/rcupdate.h>
68 #include <linux/kallsyms.h>
69 #include <linux/stacktrace.h>
70 #include <linux/resource.h>
71 #include <linux/module.h>
72 #include <linux/mount.h>
73 #include <linux/security.h>
74 #include <linux/ptrace.h>
75 #include <linux/tracehook.h>
76 #include <linux/printk.h>
77 #include <linux/cgroup.h>
78 #include <linux/cpuset.h>
79 #include <linux/audit.h>
80 #include <linux/poll.h>
81 #include <linux/nsproxy.h>
82 #include <linux/oom.h>
83 #include <linux/elf.h>
84 #include <linux/pid_namespace.h>
85 #include <linux/user_namespace.h>
86 #include <linux/fs_struct.h>
87 #include <linux/slab.h>
88 #include <linux/flex_array.h>
89 #include <linux/posix-timers.h>
90 #ifdef CONFIG_HARDWALL
91 #include <asm/hardwall.h>
93 #include <trace/events/oom.h>
98 * Implementing inode permission operations in /proc is almost
99 * certainly an error. Permission checks need to happen during
100 * each system call not at open time. The reason is that most of
101 * what we wish to check for permissions in /proc varies at runtime.
103 * The classic example of a problem is opening file descriptors
104 * in /proc for a task before it execs a suid executable.
111 const struct inode_operations *iop;
112 const struct file_operations *fop;
116 #define NOD(NAME, MODE, IOP, FOP, OP) { \
118 .len = sizeof(NAME) - 1, \
125 #define DIR(NAME, MODE, iops, fops) \
126 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
127 #define LNK(NAME, get_link) \
128 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
129 &proc_pid_link_inode_operations, NULL, \
130 { .proc_get_link = get_link } )
131 #define REG(NAME, MODE, fops) \
132 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
133 #define INF(NAME, MODE, read) \
134 NOD(NAME, (S_IFREG|(MODE)), \
135 NULL, &proc_info_file_operations, \
136 { .proc_read = read } )
137 #define ONE(NAME, MODE, show) \
138 NOD(NAME, (S_IFREG|(MODE)), \
139 NULL, &proc_single_file_operations, \
140 { .proc_show = show } )
143 * Count the number of hardlinks for the pid_entry table, excluding the .
146 static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
153 for (i = 0; i < n; ++i) {
154 if (S_ISDIR(entries[i].mode))
161 static int get_task_root(struct task_struct *task, struct path *root)
163 int result = -ENOENT;
167 get_fs_root(task->fs, root);
174 static int proc_cwd_link(struct dentry *dentry, struct path *path)
176 struct task_struct *task = get_proc_task(dentry->d_inode);
177 int result = -ENOENT;
182 get_fs_pwd(task->fs, path);
186 put_task_struct(task);
191 static int proc_root_link(struct dentry *dentry, struct path *path)
193 struct task_struct *task = get_proc_task(dentry->d_inode);
194 int result = -ENOENT;
197 result = get_task_root(task, path);
198 put_task_struct(task);
203 static int proc_pid_cmdline(struct task_struct *task, char *buffer)
205 return get_cmdline(task, buffer, PAGE_SIZE);
208 static int proc_pid_auxv(struct seq_file *m, struct pid_namespace *ns,
209 struct pid *pid, struct task_struct *task)
211 struct mm_struct *mm = mm_access(task, PTRACE_MODE_READ);
212 if (mm && !IS_ERR(mm)) {
213 unsigned int nwords = 0;
216 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
217 seq_write(m, mm->saved_auxv, nwords * sizeof(mm->saved_auxv[0]));
225 #ifdef CONFIG_KALLSYMS
227 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
228 * Returns the resolved symbol. If that fails, simply return the address.
230 static int proc_pid_wchan(struct task_struct *task, char *buffer)
233 char symname[KSYM_NAME_LEN];
235 wchan = get_wchan(task);
237 if (lookup_symbol_name(wchan, symname) < 0)
238 if (!ptrace_may_access(task, PTRACE_MODE_READ))
241 return sprintf(buffer, "%lu", wchan);
243 return sprintf(buffer, "%s", symname);
245 #endif /* CONFIG_KALLSYMS */
247 static int lock_trace(struct task_struct *task)
249 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
252 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH)) {
253 mutex_unlock(&task->signal->cred_guard_mutex);
259 static void unlock_trace(struct task_struct *task)
261 mutex_unlock(&task->signal->cred_guard_mutex);
264 #ifdef CONFIG_STACKTRACE
266 #define MAX_STACK_TRACE_DEPTH 64
268 static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
269 struct pid *pid, struct task_struct *task)
271 struct stack_trace trace;
272 unsigned long *entries;
276 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
280 trace.nr_entries = 0;
281 trace.max_entries = MAX_STACK_TRACE_DEPTH;
282 trace.entries = entries;
285 err = lock_trace(task);
287 save_stack_trace_tsk(task, &trace);
289 for (i = 0; i < trace.nr_entries; i++) {
290 seq_printf(m, "[<%pK>] %pS\n",
291 (void *)entries[i], (void *)entries[i]);
301 #ifdef CONFIG_SCHEDSTATS
303 * Provides /proc/PID/schedstat
305 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
307 return sprintf(buffer, "%llu %llu %lu\n",
308 (unsigned long long)task->se.sum_exec_runtime,
309 (unsigned long long)task->sched_info.run_delay,
310 task->sched_info.pcount);
314 #ifdef CONFIG_LATENCYTOP
315 static int lstats_show_proc(struct seq_file *m, void *v)
318 struct inode *inode = m->private;
319 struct task_struct *task = get_proc_task(inode);
323 seq_puts(m, "Latency Top version : v0.1\n");
324 for (i = 0; i < 32; i++) {
325 struct latency_record *lr = &task->latency_record[i];
326 if (lr->backtrace[0]) {
328 seq_printf(m, "%i %li %li",
329 lr->count, lr->time, lr->max);
330 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
331 unsigned long bt = lr->backtrace[q];
336 seq_printf(m, " %ps", (void *)bt);
342 put_task_struct(task);
346 static int lstats_open(struct inode *inode, struct file *file)
348 return single_open(file, lstats_show_proc, inode);
351 static ssize_t lstats_write(struct file *file, const char __user *buf,
352 size_t count, loff_t *offs)
354 struct task_struct *task = get_proc_task(file_inode(file));
358 clear_all_latency_tracing(task);
359 put_task_struct(task);
364 static const struct file_operations proc_lstats_operations = {
367 .write = lstats_write,
369 .release = single_release,
374 #ifdef CONFIG_CGROUPS
375 static int cgroup_open(struct inode *inode, struct file *file)
377 struct pid *pid = PROC_I(inode)->pid;
378 return single_open(file, proc_cgroup_show, pid);
381 static const struct file_operations proc_cgroup_operations = {
385 .release = single_release,
389 #ifdef CONFIG_PROC_PID_CPUSET
391 static int cpuset_open(struct inode *inode, struct file *file)
393 struct pid *pid = PROC_I(inode)->pid;
394 return single_open(file, proc_cpuset_show, pid);
397 static const struct file_operations proc_cpuset_operations = {
401 .release = single_release,
405 static int proc_oom_score(struct task_struct *task, char *buffer)
407 unsigned long totalpages = totalram_pages + total_swap_pages;
408 unsigned long points = 0;
410 read_lock(&tasklist_lock);
412 points = oom_badness(task, NULL, NULL, totalpages) *
414 read_unlock(&tasklist_lock);
415 return sprintf(buffer, "%lu\n", points);
423 static const struct limit_names lnames[RLIM_NLIMITS] = {
424 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
425 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
426 [RLIMIT_DATA] = {"Max data size", "bytes"},
427 [RLIMIT_STACK] = {"Max stack size", "bytes"},
428 [RLIMIT_CORE] = {"Max core file size", "bytes"},
429 [RLIMIT_RSS] = {"Max resident set", "bytes"},
430 [RLIMIT_NPROC] = {"Max processes", "processes"},
431 [RLIMIT_NOFILE] = {"Max open files", "files"},
432 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
433 [RLIMIT_AS] = {"Max address space", "bytes"},
434 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
435 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
436 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
437 [RLIMIT_NICE] = {"Max nice priority", NULL},
438 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
439 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
442 /* Display limits for a process */
443 static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
444 struct pid *pid, struct task_struct *task)
449 struct rlimit rlim[RLIM_NLIMITS];
451 if (!lock_task_sighand(task, &flags))
453 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
454 unlock_task_sighand(task, &flags);
457 * print the file header
459 seq_printf(m, "%-25s %-20s %-20s %-10s\n",
460 "Limit", "Soft Limit", "Hard Limit", "Units");
462 for (i = 0; i < RLIM_NLIMITS; i++) {
463 if (rlim[i].rlim_cur == RLIM_INFINITY)
464 seq_printf(m, "%-25s %-20s ",
465 lnames[i].name, "unlimited");
467 seq_printf(m, "%-25s %-20lu ",
468 lnames[i].name, rlim[i].rlim_cur);
470 if (rlim[i].rlim_max == RLIM_INFINITY)
471 seq_printf(m, "%-20s ", "unlimited");
473 seq_printf(m, "%-20lu ", rlim[i].rlim_max);
476 seq_printf(m, "%-10s\n", lnames[i].unit);
484 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
485 static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
486 struct pid *pid, struct task_struct *task)
489 unsigned long args[6], sp, pc;
490 int res = lock_trace(task);
494 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
495 seq_puts(m, "running\n");
497 seq_printf(m, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
500 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
502 args[0], args[1], args[2], args[3], args[4], args[5],
507 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
509 /************************************************************************/
510 /* Here the fs part begins */
511 /************************************************************************/
513 /* permission checks */
514 static int proc_fd_access_allowed(struct inode *inode)
516 struct task_struct *task;
518 /* Allow access to a task's file descriptors if it is us or we
519 * may use ptrace attach to the process and find out that
522 task = get_proc_task(inode);
524 allowed = ptrace_may_access(task, PTRACE_MODE_READ);
525 put_task_struct(task);
530 int proc_setattr(struct dentry *dentry, struct iattr *attr)
533 struct inode *inode = dentry->d_inode;
535 if (attr->ia_valid & ATTR_MODE)
538 error = inode_change_ok(inode, attr);
542 setattr_copy(inode, attr);
543 mark_inode_dirty(inode);
548 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
549 * or euid/egid (for hide_pid_min=2)?
551 static bool has_pid_permissions(struct pid_namespace *pid,
552 struct task_struct *task,
555 if (pid->hide_pid < hide_pid_min)
557 if (in_group_p(pid->pid_gid))
559 return ptrace_may_access(task, PTRACE_MODE_READ);
563 static int proc_pid_permission(struct inode *inode, int mask)
565 struct pid_namespace *pid = inode->i_sb->s_fs_info;
566 struct task_struct *task;
569 task = get_proc_task(inode);
572 has_perms = has_pid_permissions(pid, task, 1);
573 put_task_struct(task);
576 if (pid->hide_pid == 2) {
578 * Let's make getdents(), stat(), and open()
579 * consistent with each other. If a process
580 * may not stat() a file, it shouldn't be seen
588 return generic_permission(inode, mask);
593 static const struct inode_operations proc_def_inode_operations = {
594 .setattr = proc_setattr,
597 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
599 static ssize_t proc_info_read(struct file * file, char __user * buf,
600 size_t count, loff_t *ppos)
602 struct inode * inode = file_inode(file);
605 struct task_struct *task = get_proc_task(inode);
611 if (count > PROC_BLOCK_SIZE)
612 count = PROC_BLOCK_SIZE;
615 if (!(page = __get_free_page(GFP_TEMPORARY)))
618 length = PROC_I(inode)->op.proc_read(task, (char*)page);
621 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
624 put_task_struct(task);
629 static const struct file_operations proc_info_file_operations = {
630 .read = proc_info_read,
631 .llseek = generic_file_llseek,
634 static int proc_single_show(struct seq_file *m, void *v)
636 struct inode *inode = m->private;
637 struct pid_namespace *ns;
639 struct task_struct *task;
642 ns = inode->i_sb->s_fs_info;
643 pid = proc_pid(inode);
644 task = get_pid_task(pid, PIDTYPE_PID);
648 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
650 put_task_struct(task);
654 static int proc_single_open(struct inode *inode, struct file *filp)
656 return single_open(filp, proc_single_show, inode);
659 static const struct file_operations proc_single_file_operations = {
660 .open = proc_single_open,
663 .release = single_release,
666 static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
668 struct task_struct *task = get_proc_task(file_inode(file));
669 struct mm_struct *mm;
674 mm = mm_access(task, mode);
675 put_task_struct(task);
681 /* ensure this mm_struct can't be freed */
682 atomic_inc(&mm->mm_count);
683 /* but do not pin its memory */
687 file->private_data = mm;
692 static int mem_open(struct inode *inode, struct file *file)
694 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
696 /* OK to pass negative loff_t, we can catch out-of-range */
697 file->f_mode |= FMODE_UNSIGNED_OFFSET;
702 static ssize_t mem_rw(struct file *file, char __user *buf,
703 size_t count, loff_t *ppos, int write)
705 struct mm_struct *mm = file->private_data;
706 unsigned long addr = *ppos;
713 page = (char *)__get_free_page(GFP_TEMPORARY);
718 if (!atomic_inc_not_zero(&mm->mm_users))
722 int this_len = min_t(int, count, PAGE_SIZE);
724 if (write && copy_from_user(page, buf, this_len)) {
729 this_len = access_remote_vm(mm, addr, page, this_len, write);
736 if (!write && copy_to_user(buf, page, this_len)) {
750 free_page((unsigned long) page);
754 static ssize_t mem_read(struct file *file, char __user *buf,
755 size_t count, loff_t *ppos)
757 return mem_rw(file, buf, count, ppos, 0);
760 static ssize_t mem_write(struct file *file, const char __user *buf,
761 size_t count, loff_t *ppos)
763 return mem_rw(file, (char __user*)buf, count, ppos, 1);
766 loff_t mem_lseek(struct file *file, loff_t offset, int orig)
770 file->f_pos = offset;
773 file->f_pos += offset;
778 force_successful_syscall_return();
782 static int mem_release(struct inode *inode, struct file *file)
784 struct mm_struct *mm = file->private_data;
790 static const struct file_operations proc_mem_operations = {
795 .release = mem_release,
798 static int environ_open(struct inode *inode, struct file *file)
800 return __mem_open(inode, file, PTRACE_MODE_READ);
803 static ssize_t environ_read(struct file *file, char __user *buf,
804 size_t count, loff_t *ppos)
807 unsigned long src = *ppos;
809 struct mm_struct *mm = file->private_data;
814 page = (char *)__get_free_page(GFP_TEMPORARY);
819 if (!atomic_inc_not_zero(&mm->mm_users))
822 size_t this_len, max_len;
825 if (src >= (mm->env_end - mm->env_start))
828 this_len = mm->env_end - (mm->env_start + src);
830 max_len = min_t(size_t, PAGE_SIZE, count);
831 this_len = min(max_len, this_len);
833 retval = access_remote_vm(mm, (mm->env_start + src),
841 if (copy_to_user(buf, page, retval)) {
855 free_page((unsigned long) page);
859 static const struct file_operations proc_environ_operations = {
860 .open = environ_open,
861 .read = environ_read,
862 .llseek = generic_file_llseek,
863 .release = mem_release,
866 static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
869 struct task_struct *task = get_proc_task(file_inode(file));
870 char buffer[PROC_NUMBUF];
871 int oom_adj = OOM_ADJUST_MIN;
877 if (lock_task_sighand(task, &flags)) {
878 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
879 oom_adj = OOM_ADJUST_MAX;
881 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
883 unlock_task_sighand(task, &flags);
885 put_task_struct(task);
886 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
887 return simple_read_from_buffer(buf, count, ppos, buffer, len);
890 static ssize_t oom_adj_write(struct file *file, const char __user *buf,
891 size_t count, loff_t *ppos)
893 struct task_struct *task;
894 char buffer[PROC_NUMBUF];
899 memset(buffer, 0, sizeof(buffer));
900 if (count > sizeof(buffer) - 1)
901 count = sizeof(buffer) - 1;
902 if (copy_from_user(buffer, buf, count)) {
907 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
910 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
911 oom_adj != OOM_DISABLE) {
916 task = get_proc_task(file_inode(file));
928 if (!lock_task_sighand(task, &flags)) {
934 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
935 * value is always attainable.
937 if (oom_adj == OOM_ADJUST_MAX)
938 oom_adj = OOM_SCORE_ADJ_MAX;
940 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
942 if (oom_adj < task->signal->oom_score_adj &&
943 !capable(CAP_SYS_RESOURCE)) {
949 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
950 * /proc/pid/oom_score_adj instead.
952 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
953 current->comm, task_pid_nr(current), task_pid_nr(task),
956 task->signal->oom_score_adj = oom_adj;
957 trace_oom_score_adj_update(task);
959 unlock_task_sighand(task, &flags);
962 put_task_struct(task);
964 return err < 0 ? err : count;
967 static const struct file_operations proc_oom_adj_operations = {
968 .read = oom_adj_read,
969 .write = oom_adj_write,
970 .llseek = generic_file_llseek,
973 static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
974 size_t count, loff_t *ppos)
976 struct task_struct *task = get_proc_task(file_inode(file));
977 char buffer[PROC_NUMBUF];
978 short oom_score_adj = OOM_SCORE_ADJ_MIN;
984 if (lock_task_sighand(task, &flags)) {
985 oom_score_adj = task->signal->oom_score_adj;
986 unlock_task_sighand(task, &flags);
988 put_task_struct(task);
989 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
990 return simple_read_from_buffer(buf, count, ppos, buffer, len);
993 static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
994 size_t count, loff_t *ppos)
996 struct task_struct *task;
997 char buffer[PROC_NUMBUF];
1002 memset(buffer, 0, sizeof(buffer));
1003 if (count > sizeof(buffer) - 1)
1004 count = sizeof(buffer) - 1;
1005 if (copy_from_user(buffer, buf, count)) {
1010 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
1013 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
1014 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1019 task = get_proc_task(file_inode(file));
1031 if (!lock_task_sighand(task, &flags)) {
1036 if ((short)oom_score_adj < task->signal->oom_score_adj_min &&
1037 !capable(CAP_SYS_RESOURCE)) {
1042 task->signal->oom_score_adj = (short)oom_score_adj;
1043 if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
1044 task->signal->oom_score_adj_min = (short)oom_score_adj;
1045 trace_oom_score_adj_update(task);
1048 unlock_task_sighand(task, &flags);
1051 put_task_struct(task);
1053 return err < 0 ? err : count;
1056 static const struct file_operations proc_oom_score_adj_operations = {
1057 .read = oom_score_adj_read,
1058 .write = oom_score_adj_write,
1059 .llseek = default_llseek,
1062 #ifdef CONFIG_AUDITSYSCALL
1063 #define TMPBUFLEN 21
1064 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1065 size_t count, loff_t *ppos)
1067 struct inode * inode = file_inode(file);
1068 struct task_struct *task = get_proc_task(inode);
1070 char tmpbuf[TMPBUFLEN];
1074 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1075 from_kuid(file->f_cred->user_ns,
1076 audit_get_loginuid(task)));
1077 put_task_struct(task);
1078 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1081 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1082 size_t count, loff_t *ppos)
1084 struct inode * inode = file_inode(file);
1091 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1097 if (count >= PAGE_SIZE)
1098 count = PAGE_SIZE - 1;
1101 /* No partial writes. */
1104 page = (char*)__get_free_page(GFP_TEMPORARY);
1108 if (copy_from_user(page, buf, count))
1112 loginuid = simple_strtoul(page, &tmp, 10);
1119 /* is userspace tring to explicitly UNSET the loginuid? */
1120 if (loginuid == AUDIT_UID_UNSET) {
1121 kloginuid = INVALID_UID;
1123 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
1124 if (!uid_valid(kloginuid)) {
1130 length = audit_set_loginuid(kloginuid);
1131 if (likely(length == 0))
1135 free_page((unsigned long) page);
1139 static const struct file_operations proc_loginuid_operations = {
1140 .read = proc_loginuid_read,
1141 .write = proc_loginuid_write,
1142 .llseek = generic_file_llseek,
1145 static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1146 size_t count, loff_t *ppos)
1148 struct inode * inode = file_inode(file);
1149 struct task_struct *task = get_proc_task(inode);
1151 char tmpbuf[TMPBUFLEN];
1155 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1156 audit_get_sessionid(task));
1157 put_task_struct(task);
1158 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1161 static const struct file_operations proc_sessionid_operations = {
1162 .read = proc_sessionid_read,
1163 .llseek = generic_file_llseek,
1167 #ifdef CONFIG_FAULT_INJECTION
1168 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1169 size_t count, loff_t *ppos)
1171 struct task_struct *task = get_proc_task(file_inode(file));
1172 char buffer[PROC_NUMBUF];
1178 make_it_fail = task->make_it_fail;
1179 put_task_struct(task);
1181 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1183 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1186 static ssize_t proc_fault_inject_write(struct file * file,
1187 const char __user * buf, size_t count, loff_t *ppos)
1189 struct task_struct *task;
1190 char buffer[PROC_NUMBUF], *end;
1193 if (!capable(CAP_SYS_RESOURCE))
1195 memset(buffer, 0, sizeof(buffer));
1196 if (count > sizeof(buffer) - 1)
1197 count = sizeof(buffer) - 1;
1198 if (copy_from_user(buffer, buf, count))
1200 make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1203 if (make_it_fail < 0 || make_it_fail > 1)
1206 task = get_proc_task(file_inode(file));
1209 task->make_it_fail = make_it_fail;
1210 put_task_struct(task);
1215 static const struct file_operations proc_fault_inject_operations = {
1216 .read = proc_fault_inject_read,
1217 .write = proc_fault_inject_write,
1218 .llseek = generic_file_llseek,
1223 #ifdef CONFIG_SCHED_DEBUG
1225 * Print out various scheduling related per-task fields:
1227 static int sched_show(struct seq_file *m, void *v)
1229 struct inode *inode = m->private;
1230 struct task_struct *p;
1232 p = get_proc_task(inode);
1235 proc_sched_show_task(p, m);
1243 sched_write(struct file *file, const char __user *buf,
1244 size_t count, loff_t *offset)
1246 struct inode *inode = file_inode(file);
1247 struct task_struct *p;
1249 p = get_proc_task(inode);
1252 proc_sched_set_task(p);
1259 static int sched_open(struct inode *inode, struct file *filp)
1261 return single_open(filp, sched_show, inode);
1264 static const struct file_operations proc_pid_sched_operations = {
1267 .write = sched_write,
1268 .llseek = seq_lseek,
1269 .release = single_release,
1274 #ifdef CONFIG_SCHED_AUTOGROUP
1276 * Print out autogroup related information:
1278 static int sched_autogroup_show(struct seq_file *m, void *v)
1280 struct inode *inode = m->private;
1281 struct task_struct *p;
1283 p = get_proc_task(inode);
1286 proc_sched_autogroup_show_task(p, m);
1294 sched_autogroup_write(struct file *file, const char __user *buf,
1295 size_t count, loff_t *offset)
1297 struct inode *inode = file_inode(file);
1298 struct task_struct *p;
1299 char buffer[PROC_NUMBUF];
1303 memset(buffer, 0, sizeof(buffer));
1304 if (count > sizeof(buffer) - 1)
1305 count = sizeof(buffer) - 1;
1306 if (copy_from_user(buffer, buf, count))
1309 err = kstrtoint(strstrip(buffer), 0, &nice);
1313 p = get_proc_task(inode);
1317 err = proc_sched_autogroup_set_nice(p, nice);
1326 static int sched_autogroup_open(struct inode *inode, struct file *filp)
1330 ret = single_open(filp, sched_autogroup_show, NULL);
1332 struct seq_file *m = filp->private_data;
1339 static const struct file_operations proc_pid_sched_autogroup_operations = {
1340 .open = sched_autogroup_open,
1342 .write = sched_autogroup_write,
1343 .llseek = seq_lseek,
1344 .release = single_release,
1347 #endif /* CONFIG_SCHED_AUTOGROUP */
1349 static ssize_t comm_write(struct file *file, const char __user *buf,
1350 size_t count, loff_t *offset)
1352 struct inode *inode = file_inode(file);
1353 struct task_struct *p;
1354 char buffer[TASK_COMM_LEN];
1355 const size_t maxlen = sizeof(buffer) - 1;
1357 memset(buffer, 0, sizeof(buffer));
1358 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
1361 p = get_proc_task(inode);
1365 if (same_thread_group(current, p))
1366 set_task_comm(p, buffer);
1375 static int comm_show(struct seq_file *m, void *v)
1377 struct inode *inode = m->private;
1378 struct task_struct *p;
1380 p = get_proc_task(inode);
1385 seq_printf(m, "%s\n", p->comm);
1393 static int comm_open(struct inode *inode, struct file *filp)
1395 return single_open(filp, comm_show, inode);
1398 static const struct file_operations proc_pid_set_comm_operations = {
1401 .write = comm_write,
1402 .llseek = seq_lseek,
1403 .release = single_release,
1406 static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
1408 struct task_struct *task;
1409 struct mm_struct *mm;
1410 struct file *exe_file;
1412 task = get_proc_task(dentry->d_inode);
1415 mm = get_task_mm(task);
1416 put_task_struct(task);
1419 exe_file = get_mm_exe_file(mm);
1422 *exe_path = exe_file->f_path;
1423 path_get(&exe_file->f_path);
1430 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1432 struct inode *inode = dentry->d_inode;
1434 int error = -EACCES;
1436 /* Are we allowed to snoop on the tasks file descriptors? */
1437 if (!proc_fd_access_allowed(inode))
1440 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1444 nd_jump_link(nd, &path);
1447 return ERR_PTR(error);
1450 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1452 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
1459 pathname = d_path(path, tmp, PAGE_SIZE);
1460 len = PTR_ERR(pathname);
1461 if (IS_ERR(pathname))
1463 len = tmp + PAGE_SIZE - 1 - pathname;
1467 if (copy_to_user(buffer, pathname, len))
1470 free_page((unsigned long)tmp);
1474 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1476 int error = -EACCES;
1477 struct inode *inode = dentry->d_inode;
1480 /* Are we allowed to snoop on the tasks file descriptors? */
1481 if (!proc_fd_access_allowed(inode))
1484 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1488 error = do_proc_readlink(&path, buffer, buflen);
1494 const struct inode_operations proc_pid_link_inode_operations = {
1495 .readlink = proc_pid_readlink,
1496 .follow_link = proc_pid_follow_link,
1497 .setattr = proc_setattr,
1501 /* building an inode */
1503 struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1505 struct inode * inode;
1506 struct proc_inode *ei;
1507 const struct cred *cred;
1509 /* We need a new inode */
1511 inode = new_inode(sb);
1517 inode->i_ino = get_next_ino();
1518 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1519 inode->i_op = &proc_def_inode_operations;
1522 * grab the reference to task.
1524 ei->pid = get_task_pid(task, PIDTYPE_PID);
1528 if (task_dumpable(task)) {
1530 cred = __task_cred(task);
1531 inode->i_uid = cred->euid;
1532 inode->i_gid = cred->egid;
1535 security_task_to_inode(task, inode);
1545 int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1547 struct inode *inode = dentry->d_inode;
1548 struct task_struct *task;
1549 const struct cred *cred;
1550 struct pid_namespace *pid = dentry->d_sb->s_fs_info;
1552 generic_fillattr(inode, stat);
1555 stat->uid = GLOBAL_ROOT_UID;
1556 stat->gid = GLOBAL_ROOT_GID;
1557 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1559 if (!has_pid_permissions(pid, task, 2)) {
1562 * This doesn't prevent learning whether PID exists,
1563 * it only makes getattr() consistent with readdir().
1567 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1568 task_dumpable(task)) {
1569 cred = __task_cred(task);
1570 stat->uid = cred->euid;
1571 stat->gid = cred->egid;
1581 * Exceptional case: normally we are not allowed to unhash a busy
1582 * directory. In this case, however, we can do it - no aliasing problems
1583 * due to the way we treat inodes.
1585 * Rewrite the inode's ownerships here because the owning task may have
1586 * performed a setuid(), etc.
1588 * Before the /proc/pid/status file was created the only way to read
1589 * the effective uid of a /process was to stat /proc/pid. Reading
1590 * /proc/pid/status is slow enough that procps and other packages
1591 * kept stating /proc/pid. To keep the rules in /proc simple I have
1592 * made this apply to all per process world readable and executable
1595 int pid_revalidate(struct dentry *dentry, unsigned int flags)
1597 struct inode *inode;
1598 struct task_struct *task;
1599 const struct cred *cred;
1601 if (flags & LOOKUP_RCU)
1604 inode = dentry->d_inode;
1605 task = get_proc_task(inode);
1608 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1609 task_dumpable(task)) {
1611 cred = __task_cred(task);
1612 inode->i_uid = cred->euid;
1613 inode->i_gid = cred->egid;
1616 inode->i_uid = GLOBAL_ROOT_UID;
1617 inode->i_gid = GLOBAL_ROOT_GID;
1619 inode->i_mode &= ~(S_ISUID | S_ISGID);
1620 security_task_to_inode(task, inode);
1621 put_task_struct(task);
1628 static inline bool proc_inode_is_dead(struct inode *inode)
1630 return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1633 int pid_delete_dentry(const struct dentry *dentry)
1635 /* Is the task we represent dead?
1636 * If so, then don't put the dentry on the lru list,
1637 * kill it immediately.
1639 return proc_inode_is_dead(dentry->d_inode);
1642 const struct dentry_operations pid_dentry_operations =
1644 .d_revalidate = pid_revalidate,
1645 .d_delete = pid_delete_dentry,
1651 * Fill a directory entry.
1653 * If possible create the dcache entry and derive our inode number and
1654 * file type from dcache entry.
1656 * Since all of the proc inode numbers are dynamically generated, the inode
1657 * numbers do not exist until the inode is cache. This means creating the
1658 * the dcache entry in readdir is necessary to keep the inode numbers
1659 * reported by readdir in sync with the inode numbers reported
1662 bool proc_fill_cache(struct file *file, struct dir_context *ctx,
1663 const char *name, int len,
1664 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1666 struct dentry *child, *dir = file->f_path.dentry;
1667 struct qstr qname = QSTR_INIT(name, len);
1668 struct inode *inode;
1672 child = d_hash_and_lookup(dir, &qname);
1674 child = d_alloc(dir, &qname);
1676 goto end_instantiate;
1677 if (instantiate(dir->d_inode, child, task, ptr) < 0) {
1679 goto end_instantiate;
1682 inode = child->d_inode;
1684 type = inode->i_mode >> 12;
1686 return dir_emit(ctx, name, len, ino, type);
1689 return dir_emit(ctx, name, len, 1, DT_UNKNOWN);
1692 #ifdef CONFIG_CHECKPOINT_RESTORE
1695 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1696 * which represent vma start and end addresses.
1698 static int dname_to_vma_addr(struct dentry *dentry,
1699 unsigned long *start, unsigned long *end)
1701 if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2)
1707 static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
1709 unsigned long vm_start, vm_end;
1710 bool exact_vma_exists = false;
1711 struct mm_struct *mm = NULL;
1712 struct task_struct *task;
1713 const struct cred *cred;
1714 struct inode *inode;
1717 if (flags & LOOKUP_RCU)
1720 if (!capable(CAP_SYS_ADMIN)) {
1725 inode = dentry->d_inode;
1726 task = get_proc_task(inode);
1730 mm = mm_access(task, PTRACE_MODE_READ);
1731 if (IS_ERR_OR_NULL(mm))
1734 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1735 down_read(&mm->mmap_sem);
1736 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1737 up_read(&mm->mmap_sem);
1742 if (exact_vma_exists) {
1743 if (task_dumpable(task)) {
1745 cred = __task_cred(task);
1746 inode->i_uid = cred->euid;
1747 inode->i_gid = cred->egid;
1750 inode->i_uid = GLOBAL_ROOT_UID;
1751 inode->i_gid = GLOBAL_ROOT_GID;
1753 security_task_to_inode(task, inode);
1758 put_task_struct(task);
1767 static const struct dentry_operations tid_map_files_dentry_operations = {
1768 .d_revalidate = map_files_d_revalidate,
1769 .d_delete = pid_delete_dentry,
1772 static int proc_map_files_get_link(struct dentry *dentry, struct path *path)
1774 unsigned long vm_start, vm_end;
1775 struct vm_area_struct *vma;
1776 struct task_struct *task;
1777 struct mm_struct *mm;
1781 task = get_proc_task(dentry->d_inode);
1785 mm = get_task_mm(task);
1786 put_task_struct(task);
1790 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
1795 down_read(&mm->mmap_sem);
1796 vma = find_exact_vma(mm, vm_start, vm_end);
1797 if (vma && vma->vm_file) {
1798 *path = vma->vm_file->f_path;
1802 up_read(&mm->mmap_sem);
1810 struct map_files_info {
1813 unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
1817 proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
1818 struct task_struct *task, const void *ptr)
1820 fmode_t mode = (fmode_t)(unsigned long)ptr;
1821 struct proc_inode *ei;
1822 struct inode *inode;
1824 inode = proc_pid_make_inode(dir->i_sb, task);
1829 ei->op.proc_get_link = proc_map_files_get_link;
1831 inode->i_op = &proc_pid_link_inode_operations;
1833 inode->i_mode = S_IFLNK;
1835 if (mode & FMODE_READ)
1836 inode->i_mode |= S_IRUSR;
1837 if (mode & FMODE_WRITE)
1838 inode->i_mode |= S_IWUSR;
1840 d_set_d_op(dentry, &tid_map_files_dentry_operations);
1841 d_add(dentry, inode);
1846 static struct dentry *proc_map_files_lookup(struct inode *dir,
1847 struct dentry *dentry, unsigned int flags)
1849 unsigned long vm_start, vm_end;
1850 struct vm_area_struct *vma;
1851 struct task_struct *task;
1853 struct mm_struct *mm;
1856 if (!capable(CAP_SYS_ADMIN))
1860 task = get_proc_task(dir);
1865 if (!ptrace_may_access(task, PTRACE_MODE_READ))
1869 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
1872 mm = get_task_mm(task);
1876 down_read(&mm->mmap_sem);
1877 vma = find_exact_vma(mm, vm_start, vm_end);
1882 result = proc_map_files_instantiate(dir, dentry, task,
1883 (void *)(unsigned long)vma->vm_file->f_mode);
1886 up_read(&mm->mmap_sem);
1889 put_task_struct(task);
1891 return ERR_PTR(result);
1894 static const struct inode_operations proc_map_files_inode_operations = {
1895 .lookup = proc_map_files_lookup,
1896 .permission = proc_fd_permission,
1897 .setattr = proc_setattr,
1901 proc_map_files_readdir(struct file *file, struct dir_context *ctx)
1903 struct vm_area_struct *vma;
1904 struct task_struct *task;
1905 struct mm_struct *mm;
1906 unsigned long nr_files, pos, i;
1907 struct flex_array *fa = NULL;
1908 struct map_files_info info;
1909 struct map_files_info *p;
1913 if (!capable(CAP_SYS_ADMIN))
1917 task = get_proc_task(file_inode(file));
1922 if (!ptrace_may_access(task, PTRACE_MODE_READ))
1926 if (!dir_emit_dots(file, ctx))
1929 mm = get_task_mm(task);
1932 down_read(&mm->mmap_sem);
1937 * We need two passes here:
1939 * 1) Collect vmas of mapped files with mmap_sem taken
1940 * 2) Release mmap_sem and instantiate entries
1942 * otherwise we get lockdep complained, since filldir()
1943 * routine might require mmap_sem taken in might_fault().
1946 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
1947 if (vma->vm_file && ++pos > ctx->pos)
1952 fa = flex_array_alloc(sizeof(info), nr_files,
1954 if (!fa || flex_array_prealloc(fa, 0, nr_files,
1958 flex_array_free(fa);
1959 up_read(&mm->mmap_sem);
1963 for (i = 0, vma = mm->mmap, pos = 2; vma;
1964 vma = vma->vm_next) {
1967 if (++pos <= ctx->pos)
1970 info.mode = vma->vm_file->f_mode;
1971 info.len = snprintf(info.name,
1972 sizeof(info.name), "%lx-%lx",
1973 vma->vm_start, vma->vm_end);
1974 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
1978 up_read(&mm->mmap_sem);
1980 for (i = 0; i < nr_files; i++) {
1981 p = flex_array_get(fa, i);
1982 if (!proc_fill_cache(file, ctx,
1984 proc_map_files_instantiate,
1986 (void *)(unsigned long)p->mode))
1991 flex_array_free(fa);
1995 put_task_struct(task);
2000 static const struct file_operations proc_map_files_operations = {
2001 .read = generic_read_dir,
2002 .iterate = proc_map_files_readdir,
2003 .llseek = default_llseek,
2006 struct timers_private {
2008 struct task_struct *task;
2009 struct sighand_struct *sighand;
2010 struct pid_namespace *ns;
2011 unsigned long flags;
2014 static void *timers_start(struct seq_file *m, loff_t *pos)
2016 struct timers_private *tp = m->private;
2018 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2020 return ERR_PTR(-ESRCH);
2022 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2024 return ERR_PTR(-ESRCH);
2026 return seq_list_start(&tp->task->signal->posix_timers, *pos);
2029 static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2031 struct timers_private *tp = m->private;
2032 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2035 static void timers_stop(struct seq_file *m, void *v)
2037 struct timers_private *tp = m->private;
2040 unlock_task_sighand(tp->task, &tp->flags);
2045 put_task_struct(tp->task);
2050 static int show_timer(struct seq_file *m, void *v)
2052 struct k_itimer *timer;
2053 struct timers_private *tp = m->private;
2055 static const char * const nstr[] = {
2056 [SIGEV_SIGNAL] = "signal",
2057 [SIGEV_NONE] = "none",
2058 [SIGEV_THREAD] = "thread",
2061 timer = list_entry((struct list_head *)v, struct k_itimer, list);
2062 notify = timer->it_sigev_notify;
2064 seq_printf(m, "ID: %d\n", timer->it_id);
2065 seq_printf(m, "signal: %d/%p\n", timer->sigq->info.si_signo,
2066 timer->sigq->info.si_value.sival_ptr);
2067 seq_printf(m, "notify: %s/%s.%d\n",
2068 nstr[notify & ~SIGEV_THREAD_ID],
2069 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2070 pid_nr_ns(timer->it_pid, tp->ns));
2071 seq_printf(m, "ClockID: %d\n", timer->it_clock);
2076 static const struct seq_operations proc_timers_seq_ops = {
2077 .start = timers_start,
2078 .next = timers_next,
2079 .stop = timers_stop,
2083 static int proc_timers_open(struct inode *inode, struct file *file)
2085 struct timers_private *tp;
2087 tp = __seq_open_private(file, &proc_timers_seq_ops,
2088 sizeof(struct timers_private));
2092 tp->pid = proc_pid(inode);
2093 tp->ns = inode->i_sb->s_fs_info;
2097 static const struct file_operations proc_timers_operations = {
2098 .open = proc_timers_open,
2100 .llseek = seq_lseek,
2101 .release = seq_release_private,
2103 #endif /* CONFIG_CHECKPOINT_RESTORE */
2105 static int proc_pident_instantiate(struct inode *dir,
2106 struct dentry *dentry, struct task_struct *task, const void *ptr)
2108 const struct pid_entry *p = ptr;
2109 struct inode *inode;
2110 struct proc_inode *ei;
2112 inode = proc_pid_make_inode(dir->i_sb, task);
2117 inode->i_mode = p->mode;
2118 if (S_ISDIR(inode->i_mode))
2119 set_nlink(inode, 2); /* Use getattr to fix if necessary */
2121 inode->i_op = p->iop;
2123 inode->i_fop = p->fop;
2125 d_set_d_op(dentry, &pid_dentry_operations);
2126 d_add(dentry, inode);
2127 /* Close the race of the process dying before we return the dentry */
2128 if (pid_revalidate(dentry, 0))
2134 static struct dentry *proc_pident_lookup(struct inode *dir,
2135 struct dentry *dentry,
2136 const struct pid_entry *ents,
2140 struct task_struct *task = get_proc_task(dir);
2141 const struct pid_entry *p, *last;
2149 * Yes, it does not scale. And it should not. Don't add
2150 * new entries into /proc/<tgid>/ without very good reasons.
2152 last = &ents[nents - 1];
2153 for (p = ents; p <= last; p++) {
2154 if (p->len != dentry->d_name.len)
2156 if (!memcmp(dentry->d_name.name, p->name, p->len))
2162 error = proc_pident_instantiate(dir, dentry, task, p);
2164 put_task_struct(task);
2166 return ERR_PTR(error);
2169 static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
2170 const struct pid_entry *ents, unsigned int nents)
2172 struct task_struct *task = get_proc_task(file_inode(file));
2173 const struct pid_entry *p;
2178 if (!dir_emit_dots(file, ctx))
2181 if (ctx->pos >= nents + 2)
2184 for (p = ents + (ctx->pos - 2); p <= ents + nents - 1; p++) {
2185 if (!proc_fill_cache(file, ctx, p->name, p->len,
2186 proc_pident_instantiate, task, p))
2191 put_task_struct(task);
2195 #ifdef CONFIG_SECURITY
2196 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2197 size_t count, loff_t *ppos)
2199 struct inode * inode = file_inode(file);
2202 struct task_struct *task = get_proc_task(inode);
2207 length = security_getprocattr(task,
2208 (char*)file->f_path.dentry->d_name.name,
2210 put_task_struct(task);
2212 length = simple_read_from_buffer(buf, count, ppos, p, length);
2217 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2218 size_t count, loff_t *ppos)
2220 struct inode * inode = file_inode(file);
2223 struct task_struct *task = get_proc_task(inode);
2228 if (count > PAGE_SIZE)
2231 /* No partial writes. */
2237 page = (char*)__get_free_page(GFP_TEMPORARY);
2242 if (copy_from_user(page, buf, count))
2245 /* Guard against adverse ptrace interaction */
2246 length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
2250 length = security_setprocattr(task,
2251 (char*)file->f_path.dentry->d_name.name,
2252 (void*)page, count);
2253 mutex_unlock(&task->signal->cred_guard_mutex);
2255 free_page((unsigned long) page);
2257 put_task_struct(task);
2262 static const struct file_operations proc_pid_attr_operations = {
2263 .read = proc_pid_attr_read,
2264 .write = proc_pid_attr_write,
2265 .llseek = generic_file_llseek,
2268 static const struct pid_entry attr_dir_stuff[] = {
2269 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2270 REG("prev", S_IRUGO, proc_pid_attr_operations),
2271 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2272 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2273 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2274 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2277 static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
2279 return proc_pident_readdir(file, ctx,
2280 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2283 static const struct file_operations proc_attr_dir_operations = {
2284 .read = generic_read_dir,
2285 .iterate = proc_attr_dir_readdir,
2286 .llseek = default_llseek,
2289 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2290 struct dentry *dentry, unsigned int flags)
2292 return proc_pident_lookup(dir, dentry,
2293 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2296 static const struct inode_operations proc_attr_dir_inode_operations = {
2297 .lookup = proc_attr_dir_lookup,
2298 .getattr = pid_getattr,
2299 .setattr = proc_setattr,
2304 #ifdef CONFIG_ELF_CORE
2305 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2306 size_t count, loff_t *ppos)
2308 struct task_struct *task = get_proc_task(file_inode(file));
2309 struct mm_struct *mm;
2310 char buffer[PROC_NUMBUF];
2318 mm = get_task_mm(task);
2320 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2321 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2322 MMF_DUMP_FILTER_SHIFT));
2324 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2327 put_task_struct(task);
2332 static ssize_t proc_coredump_filter_write(struct file *file,
2333 const char __user *buf,
2337 struct task_struct *task;
2338 struct mm_struct *mm;
2339 char buffer[PROC_NUMBUF], *end;
2346 memset(buffer, 0, sizeof(buffer));
2347 if (count > sizeof(buffer) - 1)
2348 count = sizeof(buffer) - 1;
2349 if (copy_from_user(buffer, buf, count))
2353 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2356 if (end - buffer == 0)
2360 task = get_proc_task(file_inode(file));
2365 mm = get_task_mm(task);
2369 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2371 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2373 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2378 put_task_struct(task);
2383 static const struct file_operations proc_coredump_filter_operations = {
2384 .read = proc_coredump_filter_read,
2385 .write = proc_coredump_filter_write,
2386 .llseek = generic_file_llseek,
2390 #ifdef CONFIG_TASK_IO_ACCOUNTING
2391 static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
2393 struct task_io_accounting acct = task->ioac;
2394 unsigned long flags;
2397 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2401 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
2406 if (whole && lock_task_sighand(task, &flags)) {
2407 struct task_struct *t = task;
2409 task_io_accounting_add(&acct, &task->signal->ioac);
2410 while_each_thread(task, t)
2411 task_io_accounting_add(&acct, &t->ioac);
2413 unlock_task_sighand(task, &flags);
2415 result = sprintf(buffer,
2420 "read_bytes: %llu\n"
2421 "write_bytes: %llu\n"
2422 "cancelled_write_bytes: %llu\n",
2423 (unsigned long long)acct.rchar,
2424 (unsigned long long)acct.wchar,
2425 (unsigned long long)acct.syscr,
2426 (unsigned long long)acct.syscw,
2427 (unsigned long long)acct.read_bytes,
2428 (unsigned long long)acct.write_bytes,
2429 (unsigned long long)acct.cancelled_write_bytes);
2431 mutex_unlock(&task->signal->cred_guard_mutex);
2435 static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2437 return do_io_accounting(task, buffer, 0);
2440 static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2442 return do_io_accounting(task, buffer, 1);
2444 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2446 #ifdef CONFIG_USER_NS
2447 static int proc_id_map_open(struct inode *inode, struct file *file,
2448 const struct seq_operations *seq_ops)
2450 struct user_namespace *ns = NULL;
2451 struct task_struct *task;
2452 struct seq_file *seq;
2455 task = get_proc_task(inode);
2458 ns = get_user_ns(task_cred_xxx(task, user_ns));
2460 put_task_struct(task);
2465 ret = seq_open(file, seq_ops);
2469 seq = file->private_data;
2479 static int proc_id_map_release(struct inode *inode, struct file *file)
2481 struct seq_file *seq = file->private_data;
2482 struct user_namespace *ns = seq->private;
2484 return seq_release(inode, file);
2487 static int proc_uid_map_open(struct inode *inode, struct file *file)
2489 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2492 static int proc_gid_map_open(struct inode *inode, struct file *file)
2494 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2497 static int proc_projid_map_open(struct inode *inode, struct file *file)
2499 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2502 static const struct file_operations proc_uid_map_operations = {
2503 .open = proc_uid_map_open,
2504 .write = proc_uid_map_write,
2506 .llseek = seq_lseek,
2507 .release = proc_id_map_release,
2510 static const struct file_operations proc_gid_map_operations = {
2511 .open = proc_gid_map_open,
2512 .write = proc_gid_map_write,
2514 .llseek = seq_lseek,
2515 .release = proc_id_map_release,
2518 static const struct file_operations proc_projid_map_operations = {
2519 .open = proc_projid_map_open,
2520 .write = proc_projid_map_write,
2522 .llseek = seq_lseek,
2523 .release = proc_id_map_release,
2525 #endif /* CONFIG_USER_NS */
2527 static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2528 struct pid *pid, struct task_struct *task)
2530 int err = lock_trace(task);
2532 seq_printf(m, "%08x\n", task->personality);
2541 static const struct file_operations proc_task_operations;
2542 static const struct inode_operations proc_task_inode_operations;
2544 static const struct pid_entry tgid_base_stuff[] = {
2545 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2546 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2547 #ifdef CONFIG_CHECKPOINT_RESTORE
2548 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
2550 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2551 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
2553 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2555 REG("environ", S_IRUSR, proc_environ_operations),
2556 ONE("auxv", S_IRUSR, proc_pid_auxv),
2557 ONE("status", S_IRUGO, proc_pid_status),
2558 ONE("personality", S_IRUSR, proc_pid_personality),
2559 ONE("limits", S_IRUGO, proc_pid_limits),
2560 #ifdef CONFIG_SCHED_DEBUG
2561 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2563 #ifdef CONFIG_SCHED_AUTOGROUP
2564 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2566 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2567 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2568 ONE("syscall", S_IRUSR, proc_pid_syscall),
2570 INF("cmdline", S_IRUGO, proc_pid_cmdline),
2571 ONE("stat", S_IRUGO, proc_tgid_stat),
2572 ONE("statm", S_IRUGO, proc_pid_statm),
2573 REG("maps", S_IRUGO, proc_pid_maps_operations),
2575 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
2577 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2578 LNK("cwd", proc_cwd_link),
2579 LNK("root", proc_root_link),
2580 LNK("exe", proc_exe_link),
2581 REG("mounts", S_IRUGO, proc_mounts_operations),
2582 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2583 REG("mountstats", S_IRUSR, proc_mountstats_operations),
2584 #ifdef CONFIG_PROC_PAGE_MONITOR
2585 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2586 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
2587 REG("pagemap", S_IRUSR, proc_pagemap_operations),
2589 #ifdef CONFIG_SECURITY
2590 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
2592 #ifdef CONFIG_KALLSYMS
2593 INF("wchan", S_IRUGO, proc_pid_wchan),
2595 #ifdef CONFIG_STACKTRACE
2596 ONE("stack", S_IRUSR, proc_pid_stack),
2598 #ifdef CONFIG_SCHEDSTATS
2599 INF("schedstat", S_IRUGO, proc_pid_schedstat),
2601 #ifdef CONFIG_LATENCYTOP
2602 REG("latency", S_IRUGO, proc_lstats_operations),
2604 #ifdef CONFIG_PROC_PID_CPUSET
2605 REG("cpuset", S_IRUGO, proc_cpuset_operations),
2607 #ifdef CONFIG_CGROUPS
2608 REG("cgroup", S_IRUGO, proc_cgroup_operations),
2610 INF("oom_score", S_IRUGO, proc_oom_score),
2611 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
2612 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
2613 #ifdef CONFIG_AUDITSYSCALL
2614 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2615 REG("sessionid", S_IRUGO, proc_sessionid_operations),
2617 #ifdef CONFIG_FAULT_INJECTION
2618 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2620 #ifdef CONFIG_ELF_CORE
2621 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
2623 #ifdef CONFIG_TASK_IO_ACCOUNTING
2624 INF("io", S_IRUSR, proc_tgid_io_accounting),
2626 #ifdef CONFIG_HARDWALL
2627 INF("hardwall", S_IRUGO, proc_pid_hardwall),
2629 #ifdef CONFIG_USER_NS
2630 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2631 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
2632 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
2634 #ifdef CONFIG_CHECKPOINT_RESTORE
2635 REG("timers", S_IRUGO, proc_timers_operations),
2639 static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
2641 return proc_pident_readdir(file, ctx,
2642 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2645 static const struct file_operations proc_tgid_base_operations = {
2646 .read = generic_read_dir,
2647 .iterate = proc_tgid_base_readdir,
2648 .llseek = default_llseek,
2651 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2653 return proc_pident_lookup(dir, dentry,
2654 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2657 static const struct inode_operations proc_tgid_base_inode_operations = {
2658 .lookup = proc_tgid_base_lookup,
2659 .getattr = pid_getattr,
2660 .setattr = proc_setattr,
2661 .permission = proc_pid_permission,
2664 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
2666 struct dentry *dentry, *leader, *dir;
2667 char buf[PROC_NUMBUF];
2671 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2672 /* no ->d_hash() rejects on procfs */
2673 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
2675 shrink_dcache_parent(dentry);
2681 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2682 leader = d_hash_and_lookup(mnt->mnt_root, &name);
2687 name.len = strlen(name.name);
2688 dir = d_hash_and_lookup(leader, &name);
2690 goto out_put_leader;
2693 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2694 dentry = d_hash_and_lookup(dir, &name);
2696 shrink_dcache_parent(dentry);
2709 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2710 * @task: task that should be flushed.
2712 * When flushing dentries from proc, one needs to flush them from global
2713 * proc (proc_mnt) and from all the namespaces' procs this task was seen
2714 * in. This call is supposed to do all of this job.
2716 * Looks in the dcache for
2718 * /proc/@tgid/task/@pid
2719 * if either directory is present flushes it and all of it'ts children
2722 * It is safe and reasonable to cache /proc entries for a task until
2723 * that task exits. After that they just clog up the dcache with
2724 * useless entries, possibly causing useful dcache entries to be
2725 * flushed instead. This routine is proved to flush those useless
2726 * dcache entries at process exit time.
2728 * NOTE: This routine is just an optimization so it does not guarantee
2729 * that no dcache entries will exist at process exit time it
2730 * just makes it very unlikely that any will persist.
2733 void proc_flush_task(struct task_struct *task)
2736 struct pid *pid, *tgid;
2739 pid = task_pid(task);
2740 tgid = task_tgid(task);
2742 for (i = 0; i <= pid->level; i++) {
2743 upid = &pid->numbers[i];
2744 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
2745 tgid->numbers[i].nr);
2749 static int proc_pid_instantiate(struct inode *dir,
2750 struct dentry * dentry,
2751 struct task_struct *task, const void *ptr)
2753 struct inode *inode;
2755 inode = proc_pid_make_inode(dir->i_sb, task);
2759 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2760 inode->i_op = &proc_tgid_base_inode_operations;
2761 inode->i_fop = &proc_tgid_base_operations;
2762 inode->i_flags|=S_IMMUTABLE;
2764 set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
2765 ARRAY_SIZE(tgid_base_stuff)));
2767 d_set_d_op(dentry, &pid_dentry_operations);
2769 d_add(dentry, inode);
2770 /* Close the race of the process dying before we return the dentry */
2771 if (pid_revalidate(dentry, 0))
2777 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
2779 int result = -ENOENT;
2780 struct task_struct *task;
2782 struct pid_namespace *ns;
2784 tgid = name_to_int(&dentry->d_name);
2788 ns = dentry->d_sb->s_fs_info;
2790 task = find_task_by_pid_ns(tgid, ns);
2792 get_task_struct(task);
2797 result = proc_pid_instantiate(dir, dentry, task, NULL);
2798 put_task_struct(task);
2800 return ERR_PTR(result);
2804 * Find the first task with tgid >= tgid
2809 struct task_struct *task;
2811 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2816 put_task_struct(iter.task);
2820 pid = find_ge_pid(iter.tgid, ns);
2822 iter.tgid = pid_nr_ns(pid, ns);
2823 iter.task = pid_task(pid, PIDTYPE_PID);
2824 /* What we to know is if the pid we have find is the
2825 * pid of a thread_group_leader. Testing for task
2826 * being a thread_group_leader is the obvious thing
2827 * todo but there is a window when it fails, due to
2828 * the pid transfer logic in de_thread.
2830 * So we perform the straight forward test of seeing
2831 * if the pid we have found is the pid of a thread
2832 * group leader, and don't worry if the task we have
2833 * found doesn't happen to be a thread group leader.
2834 * As we don't care in the case of readdir.
2836 if (!iter.task || !has_group_leader_pid(iter.task)) {
2840 get_task_struct(iter.task);
2846 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + 1)
2848 /* for the /proc/ directory itself, after non-process stuff has been done */
2849 int proc_pid_readdir(struct file *file, struct dir_context *ctx)
2851 struct tgid_iter iter;
2852 struct pid_namespace *ns = file->f_dentry->d_sb->s_fs_info;
2853 loff_t pos = ctx->pos;
2855 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
2858 if (pos == TGID_OFFSET - 1) {
2859 struct inode *inode = ns->proc_self->d_inode;
2860 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
2864 iter.tgid = pos - TGID_OFFSET;
2867 for (iter = next_tgid(ns, iter);
2869 iter.tgid += 1, iter = next_tgid(ns, iter)) {
2870 char name[PROC_NUMBUF];
2872 if (!has_pid_permissions(ns, iter.task, 2))
2875 len = snprintf(name, sizeof(name), "%d", iter.tgid);
2876 ctx->pos = iter.tgid + TGID_OFFSET;
2877 if (!proc_fill_cache(file, ctx, name, len,
2878 proc_pid_instantiate, iter.task, NULL)) {
2879 put_task_struct(iter.task);
2883 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
2890 static const struct pid_entry tid_base_stuff[] = {
2891 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2892 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2893 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
2894 REG("environ", S_IRUSR, proc_environ_operations),
2895 ONE("auxv", S_IRUSR, proc_pid_auxv),
2896 ONE("status", S_IRUGO, proc_pid_status),
2897 ONE("personality", S_IRUSR, proc_pid_personality),
2898 ONE("limits", S_IRUGO, proc_pid_limits),
2899 #ifdef CONFIG_SCHED_DEBUG
2900 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2902 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2903 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2904 ONE("syscall", S_IRUSR, proc_pid_syscall),
2906 INF("cmdline", S_IRUGO, proc_pid_cmdline),
2907 ONE("stat", S_IRUGO, proc_tid_stat),
2908 ONE("statm", S_IRUGO, proc_pid_statm),
2909 REG("maps", S_IRUGO, proc_tid_maps_operations),
2910 #ifdef CONFIG_CHECKPOINT_RESTORE
2911 REG("children", S_IRUGO, proc_tid_children_operations),
2914 REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
2916 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2917 LNK("cwd", proc_cwd_link),
2918 LNK("root", proc_root_link),
2919 LNK("exe", proc_exe_link),
2920 REG("mounts", S_IRUGO, proc_mounts_operations),
2921 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2922 #ifdef CONFIG_PROC_PAGE_MONITOR
2923 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2924 REG("smaps", S_IRUGO, proc_tid_smaps_operations),
2925 REG("pagemap", S_IRUSR, proc_pagemap_operations),
2927 #ifdef CONFIG_SECURITY
2928 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
2930 #ifdef CONFIG_KALLSYMS
2931 INF("wchan", S_IRUGO, proc_pid_wchan),
2933 #ifdef CONFIG_STACKTRACE
2934 ONE("stack", S_IRUSR, proc_pid_stack),
2936 #ifdef CONFIG_SCHEDSTATS
2937 INF("schedstat", S_IRUGO, proc_pid_schedstat),
2939 #ifdef CONFIG_LATENCYTOP
2940 REG("latency", S_IRUGO, proc_lstats_operations),
2942 #ifdef CONFIG_PROC_PID_CPUSET
2943 REG("cpuset", S_IRUGO, proc_cpuset_operations),
2945 #ifdef CONFIG_CGROUPS
2946 REG("cgroup", S_IRUGO, proc_cgroup_operations),
2948 INF("oom_score", S_IRUGO, proc_oom_score),
2949 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
2950 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
2951 #ifdef CONFIG_AUDITSYSCALL
2952 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2953 REG("sessionid", S_IRUGO, proc_sessionid_operations),
2955 #ifdef CONFIG_FAULT_INJECTION
2956 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2958 #ifdef CONFIG_TASK_IO_ACCOUNTING
2959 INF("io", S_IRUSR, proc_tid_io_accounting),
2961 #ifdef CONFIG_HARDWALL
2962 INF("hardwall", S_IRUGO, proc_pid_hardwall),
2964 #ifdef CONFIG_USER_NS
2965 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2966 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
2967 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
2971 static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
2973 return proc_pident_readdir(file, ctx,
2974 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
2977 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2979 return proc_pident_lookup(dir, dentry,
2980 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
2983 static const struct file_operations proc_tid_base_operations = {
2984 .read = generic_read_dir,
2985 .iterate = proc_tid_base_readdir,
2986 .llseek = default_llseek,
2989 static const struct inode_operations proc_tid_base_inode_operations = {
2990 .lookup = proc_tid_base_lookup,
2991 .getattr = pid_getattr,
2992 .setattr = proc_setattr,
2995 static int proc_task_instantiate(struct inode *dir,
2996 struct dentry *dentry, struct task_struct *task, const void *ptr)
2998 struct inode *inode;
2999 inode = proc_pid_make_inode(dir->i_sb, task);
3003 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3004 inode->i_op = &proc_tid_base_inode_operations;
3005 inode->i_fop = &proc_tid_base_operations;
3006 inode->i_flags|=S_IMMUTABLE;
3008 set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3009 ARRAY_SIZE(tid_base_stuff)));
3011 d_set_d_op(dentry, &pid_dentry_operations);
3013 d_add(dentry, inode);
3014 /* Close the race of the process dying before we return the dentry */
3015 if (pid_revalidate(dentry, 0))
3021 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
3023 int result = -ENOENT;
3024 struct task_struct *task;
3025 struct task_struct *leader = get_proc_task(dir);
3027 struct pid_namespace *ns;
3032 tid = name_to_int(&dentry->d_name);
3036 ns = dentry->d_sb->s_fs_info;
3038 task = find_task_by_pid_ns(tid, ns);
3040 get_task_struct(task);
3044 if (!same_thread_group(leader, task))
3047 result = proc_task_instantiate(dir, dentry, task, NULL);
3049 put_task_struct(task);
3051 put_task_struct(leader);
3053 return ERR_PTR(result);
3057 * Find the first tid of a thread group to return to user space.
3059 * Usually this is just the thread group leader, but if the users
3060 * buffer was too small or there was a seek into the middle of the
3061 * directory we have more work todo.
3063 * In the case of a short read we start with find_task_by_pid.
3065 * In the case of a seek we start with the leader and walk nr
3068 static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
3069 struct pid_namespace *ns)
3071 struct task_struct *pos, *task;
3072 unsigned long nr = f_pos;
3074 if (nr != f_pos) /* 32bit overflow? */
3078 task = pid_task(pid, PIDTYPE_PID);
3082 /* Attempt to start with the tid of a thread */
3084 pos = find_task_by_pid_ns(tid, ns);
3085 if (pos && same_thread_group(pos, task))
3089 /* If nr exceeds the number of threads there is nothing todo */
3090 if (nr >= get_nr_threads(task))
3093 /* If we haven't found our starting place yet start
3094 * with the leader and walk nr threads forward.
3096 pos = task = task->group_leader;
3100 } while_each_thread(task, pos);
3105 get_task_struct(pos);
3112 * Find the next thread in the thread list.
3113 * Return NULL if there is an error or no next thread.
3115 * The reference to the input task_struct is released.
3117 static struct task_struct *next_tid(struct task_struct *start)
3119 struct task_struct *pos = NULL;
3121 if (pid_alive(start)) {
3122 pos = next_thread(start);
3123 if (thread_group_leader(pos))
3126 get_task_struct(pos);
3129 put_task_struct(start);
3133 /* for the /proc/TGID/task/ directories */
3134 static int proc_task_readdir(struct file *file, struct dir_context *ctx)
3136 struct inode *inode = file_inode(file);
3137 struct task_struct *task;
3138 struct pid_namespace *ns;
3141 if (proc_inode_is_dead(inode))
3144 if (!dir_emit_dots(file, ctx))
3147 /* f_version caches the tgid value that the last readdir call couldn't
3148 * return. lseek aka telldir automagically resets f_version to 0.
3150 ns = file->f_dentry->d_sb->s_fs_info;
3151 tid = (int)file->f_version;
3152 file->f_version = 0;
3153 for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
3155 task = next_tid(task), ctx->pos++) {
3156 char name[PROC_NUMBUF];
3158 tid = task_pid_nr_ns(task, ns);
3159 len = snprintf(name, sizeof(name), "%d", tid);
3160 if (!proc_fill_cache(file, ctx, name, len,
3161 proc_task_instantiate, task, NULL)) {
3162 /* returning this tgid failed, save it as the first
3163 * pid for the next readir call */
3164 file->f_version = (u64)tid;
3165 put_task_struct(task);
3173 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3175 struct inode *inode = dentry->d_inode;
3176 struct task_struct *p = get_proc_task(inode);
3177 generic_fillattr(inode, stat);
3180 stat->nlink += get_nr_threads(p);
3187 static const struct inode_operations proc_task_inode_operations = {
3188 .lookup = proc_task_lookup,
3189 .getattr = proc_task_getattr,
3190 .setattr = proc_setattr,
3191 .permission = proc_pid_permission,
3194 static const struct file_operations proc_task_operations = {
3195 .read = generic_read_dir,
3196 .iterate = proc_task_readdir,
3197 .llseek = default_llseek,