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 seq_file *m, struct pid_namespace *ns,
204 struct pid *pid, struct task_struct *task)
207 * Rely on struct seq_operations::show() being called once
208 * per internal buffer allocation. See single_open(), traverse().
210 BUG_ON(m->size < PAGE_SIZE);
211 m->count += get_cmdline(task, m->buf, PAGE_SIZE);
215 static int proc_pid_auxv(struct seq_file *m, struct pid_namespace *ns,
216 struct pid *pid, struct task_struct *task)
218 struct mm_struct *mm = mm_access(task, PTRACE_MODE_READ);
219 if (mm && !IS_ERR(mm)) {
220 unsigned int nwords = 0;
223 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
224 seq_write(m, mm->saved_auxv, nwords * sizeof(mm->saved_auxv[0]));
232 #ifdef CONFIG_KALLSYMS
234 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
235 * Returns the resolved symbol. If that fails, simply return the address.
237 static int proc_pid_wchan(struct task_struct *task, char *buffer)
240 char symname[KSYM_NAME_LEN];
242 wchan = get_wchan(task);
244 if (lookup_symbol_name(wchan, symname) < 0)
245 if (!ptrace_may_access(task, PTRACE_MODE_READ))
248 return sprintf(buffer, "%lu", wchan);
250 return sprintf(buffer, "%s", symname);
252 #endif /* CONFIG_KALLSYMS */
254 static int lock_trace(struct task_struct *task)
256 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
259 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH)) {
260 mutex_unlock(&task->signal->cred_guard_mutex);
266 static void unlock_trace(struct task_struct *task)
268 mutex_unlock(&task->signal->cred_guard_mutex);
271 #ifdef CONFIG_STACKTRACE
273 #define MAX_STACK_TRACE_DEPTH 64
275 static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
276 struct pid *pid, struct task_struct *task)
278 struct stack_trace trace;
279 unsigned long *entries;
283 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
287 trace.nr_entries = 0;
288 trace.max_entries = MAX_STACK_TRACE_DEPTH;
289 trace.entries = entries;
292 err = lock_trace(task);
294 save_stack_trace_tsk(task, &trace);
296 for (i = 0; i < trace.nr_entries; i++) {
297 seq_printf(m, "[<%pK>] %pS\n",
298 (void *)entries[i], (void *)entries[i]);
308 #ifdef CONFIG_SCHEDSTATS
310 * Provides /proc/PID/schedstat
312 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
314 return sprintf(buffer, "%llu %llu %lu\n",
315 (unsigned long long)task->se.sum_exec_runtime,
316 (unsigned long long)task->sched_info.run_delay,
317 task->sched_info.pcount);
321 #ifdef CONFIG_LATENCYTOP
322 static int lstats_show_proc(struct seq_file *m, void *v)
325 struct inode *inode = m->private;
326 struct task_struct *task = get_proc_task(inode);
330 seq_puts(m, "Latency Top version : v0.1\n");
331 for (i = 0; i < 32; i++) {
332 struct latency_record *lr = &task->latency_record[i];
333 if (lr->backtrace[0]) {
335 seq_printf(m, "%i %li %li",
336 lr->count, lr->time, lr->max);
337 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
338 unsigned long bt = lr->backtrace[q];
343 seq_printf(m, " %ps", (void *)bt);
349 put_task_struct(task);
353 static int lstats_open(struct inode *inode, struct file *file)
355 return single_open(file, lstats_show_proc, inode);
358 static ssize_t lstats_write(struct file *file, const char __user *buf,
359 size_t count, loff_t *offs)
361 struct task_struct *task = get_proc_task(file_inode(file));
365 clear_all_latency_tracing(task);
366 put_task_struct(task);
371 static const struct file_operations proc_lstats_operations = {
374 .write = lstats_write,
376 .release = single_release,
381 #ifdef CONFIG_CGROUPS
382 static int cgroup_open(struct inode *inode, struct file *file)
384 struct pid *pid = PROC_I(inode)->pid;
385 return single_open(file, proc_cgroup_show, pid);
388 static const struct file_operations proc_cgroup_operations = {
392 .release = single_release,
396 #ifdef CONFIG_PROC_PID_CPUSET
398 static int cpuset_open(struct inode *inode, struct file *file)
400 struct pid *pid = PROC_I(inode)->pid;
401 return single_open(file, proc_cpuset_show, pid);
404 static const struct file_operations proc_cpuset_operations = {
408 .release = single_release,
412 static int proc_oom_score(struct task_struct *task, char *buffer)
414 unsigned long totalpages = totalram_pages + total_swap_pages;
415 unsigned long points = 0;
417 read_lock(&tasklist_lock);
419 points = oom_badness(task, NULL, NULL, totalpages) *
421 read_unlock(&tasklist_lock);
422 return sprintf(buffer, "%lu\n", points);
430 static const struct limit_names lnames[RLIM_NLIMITS] = {
431 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
432 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
433 [RLIMIT_DATA] = {"Max data size", "bytes"},
434 [RLIMIT_STACK] = {"Max stack size", "bytes"},
435 [RLIMIT_CORE] = {"Max core file size", "bytes"},
436 [RLIMIT_RSS] = {"Max resident set", "bytes"},
437 [RLIMIT_NPROC] = {"Max processes", "processes"},
438 [RLIMIT_NOFILE] = {"Max open files", "files"},
439 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
440 [RLIMIT_AS] = {"Max address space", "bytes"},
441 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
442 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
443 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
444 [RLIMIT_NICE] = {"Max nice priority", NULL},
445 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
446 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
449 /* Display limits for a process */
450 static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
451 struct pid *pid, struct task_struct *task)
456 struct rlimit rlim[RLIM_NLIMITS];
458 if (!lock_task_sighand(task, &flags))
460 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
461 unlock_task_sighand(task, &flags);
464 * print the file header
466 seq_printf(m, "%-25s %-20s %-20s %-10s\n",
467 "Limit", "Soft Limit", "Hard Limit", "Units");
469 for (i = 0; i < RLIM_NLIMITS; i++) {
470 if (rlim[i].rlim_cur == RLIM_INFINITY)
471 seq_printf(m, "%-25s %-20s ",
472 lnames[i].name, "unlimited");
474 seq_printf(m, "%-25s %-20lu ",
475 lnames[i].name, rlim[i].rlim_cur);
477 if (rlim[i].rlim_max == RLIM_INFINITY)
478 seq_printf(m, "%-20s ", "unlimited");
480 seq_printf(m, "%-20lu ", rlim[i].rlim_max);
483 seq_printf(m, "%-10s\n", lnames[i].unit);
491 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
492 static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
493 struct pid *pid, struct task_struct *task)
496 unsigned long args[6], sp, pc;
497 int res = lock_trace(task);
501 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
502 seq_puts(m, "running\n");
504 seq_printf(m, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
507 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
509 args[0], args[1], args[2], args[3], args[4], args[5],
514 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
516 /************************************************************************/
517 /* Here the fs part begins */
518 /************************************************************************/
520 /* permission checks */
521 static int proc_fd_access_allowed(struct inode *inode)
523 struct task_struct *task;
525 /* Allow access to a task's file descriptors if it is us or we
526 * may use ptrace attach to the process and find out that
529 task = get_proc_task(inode);
531 allowed = ptrace_may_access(task, PTRACE_MODE_READ);
532 put_task_struct(task);
537 int proc_setattr(struct dentry *dentry, struct iattr *attr)
540 struct inode *inode = dentry->d_inode;
542 if (attr->ia_valid & ATTR_MODE)
545 error = inode_change_ok(inode, attr);
549 setattr_copy(inode, attr);
550 mark_inode_dirty(inode);
555 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
556 * or euid/egid (for hide_pid_min=2)?
558 static bool has_pid_permissions(struct pid_namespace *pid,
559 struct task_struct *task,
562 if (pid->hide_pid < hide_pid_min)
564 if (in_group_p(pid->pid_gid))
566 return ptrace_may_access(task, PTRACE_MODE_READ);
570 static int proc_pid_permission(struct inode *inode, int mask)
572 struct pid_namespace *pid = inode->i_sb->s_fs_info;
573 struct task_struct *task;
576 task = get_proc_task(inode);
579 has_perms = has_pid_permissions(pid, task, 1);
580 put_task_struct(task);
583 if (pid->hide_pid == 2) {
585 * Let's make getdents(), stat(), and open()
586 * consistent with each other. If a process
587 * may not stat() a file, it shouldn't be seen
595 return generic_permission(inode, mask);
600 static const struct inode_operations proc_def_inode_operations = {
601 .setattr = proc_setattr,
604 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
606 static ssize_t proc_info_read(struct file * file, char __user * buf,
607 size_t count, loff_t *ppos)
609 struct inode * inode = file_inode(file);
612 struct task_struct *task = get_proc_task(inode);
618 if (count > PROC_BLOCK_SIZE)
619 count = PROC_BLOCK_SIZE;
622 if (!(page = __get_free_page(GFP_TEMPORARY)))
625 length = PROC_I(inode)->op.proc_read(task, (char*)page);
628 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
631 put_task_struct(task);
636 static const struct file_operations proc_info_file_operations = {
637 .read = proc_info_read,
638 .llseek = generic_file_llseek,
641 static int proc_single_show(struct seq_file *m, void *v)
643 struct inode *inode = m->private;
644 struct pid_namespace *ns;
646 struct task_struct *task;
649 ns = inode->i_sb->s_fs_info;
650 pid = proc_pid(inode);
651 task = get_pid_task(pid, PIDTYPE_PID);
655 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
657 put_task_struct(task);
661 static int proc_single_open(struct inode *inode, struct file *filp)
663 return single_open(filp, proc_single_show, inode);
666 static const struct file_operations proc_single_file_operations = {
667 .open = proc_single_open,
670 .release = single_release,
673 static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
675 struct task_struct *task = get_proc_task(file_inode(file));
676 struct mm_struct *mm;
681 mm = mm_access(task, mode);
682 put_task_struct(task);
688 /* ensure this mm_struct can't be freed */
689 atomic_inc(&mm->mm_count);
690 /* but do not pin its memory */
694 file->private_data = mm;
699 static int mem_open(struct inode *inode, struct file *file)
701 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
703 /* OK to pass negative loff_t, we can catch out-of-range */
704 file->f_mode |= FMODE_UNSIGNED_OFFSET;
709 static ssize_t mem_rw(struct file *file, char __user *buf,
710 size_t count, loff_t *ppos, int write)
712 struct mm_struct *mm = file->private_data;
713 unsigned long addr = *ppos;
720 page = (char *)__get_free_page(GFP_TEMPORARY);
725 if (!atomic_inc_not_zero(&mm->mm_users))
729 int this_len = min_t(int, count, PAGE_SIZE);
731 if (write && copy_from_user(page, buf, this_len)) {
736 this_len = access_remote_vm(mm, addr, page, this_len, write);
743 if (!write && copy_to_user(buf, page, this_len)) {
757 free_page((unsigned long) page);
761 static ssize_t mem_read(struct file *file, char __user *buf,
762 size_t count, loff_t *ppos)
764 return mem_rw(file, buf, count, ppos, 0);
767 static ssize_t mem_write(struct file *file, const char __user *buf,
768 size_t count, loff_t *ppos)
770 return mem_rw(file, (char __user*)buf, count, ppos, 1);
773 loff_t mem_lseek(struct file *file, loff_t offset, int orig)
777 file->f_pos = offset;
780 file->f_pos += offset;
785 force_successful_syscall_return();
789 static int mem_release(struct inode *inode, struct file *file)
791 struct mm_struct *mm = file->private_data;
797 static const struct file_operations proc_mem_operations = {
802 .release = mem_release,
805 static int environ_open(struct inode *inode, struct file *file)
807 return __mem_open(inode, file, PTRACE_MODE_READ);
810 static ssize_t environ_read(struct file *file, char __user *buf,
811 size_t count, loff_t *ppos)
814 unsigned long src = *ppos;
816 struct mm_struct *mm = file->private_data;
821 page = (char *)__get_free_page(GFP_TEMPORARY);
826 if (!atomic_inc_not_zero(&mm->mm_users))
829 size_t this_len, max_len;
832 if (src >= (mm->env_end - mm->env_start))
835 this_len = mm->env_end - (mm->env_start + src);
837 max_len = min_t(size_t, PAGE_SIZE, count);
838 this_len = min(max_len, this_len);
840 retval = access_remote_vm(mm, (mm->env_start + src),
848 if (copy_to_user(buf, page, retval)) {
862 free_page((unsigned long) page);
866 static const struct file_operations proc_environ_operations = {
867 .open = environ_open,
868 .read = environ_read,
869 .llseek = generic_file_llseek,
870 .release = mem_release,
873 static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
876 struct task_struct *task = get_proc_task(file_inode(file));
877 char buffer[PROC_NUMBUF];
878 int oom_adj = OOM_ADJUST_MIN;
884 if (lock_task_sighand(task, &flags)) {
885 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
886 oom_adj = OOM_ADJUST_MAX;
888 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
890 unlock_task_sighand(task, &flags);
892 put_task_struct(task);
893 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
894 return simple_read_from_buffer(buf, count, ppos, buffer, len);
897 static ssize_t oom_adj_write(struct file *file, const char __user *buf,
898 size_t count, loff_t *ppos)
900 struct task_struct *task;
901 char buffer[PROC_NUMBUF];
906 memset(buffer, 0, sizeof(buffer));
907 if (count > sizeof(buffer) - 1)
908 count = sizeof(buffer) - 1;
909 if (copy_from_user(buffer, buf, count)) {
914 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
917 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
918 oom_adj != OOM_DISABLE) {
923 task = get_proc_task(file_inode(file));
935 if (!lock_task_sighand(task, &flags)) {
941 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
942 * value is always attainable.
944 if (oom_adj == OOM_ADJUST_MAX)
945 oom_adj = OOM_SCORE_ADJ_MAX;
947 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
949 if (oom_adj < task->signal->oom_score_adj &&
950 !capable(CAP_SYS_RESOURCE)) {
956 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
957 * /proc/pid/oom_score_adj instead.
959 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
960 current->comm, task_pid_nr(current), task_pid_nr(task),
963 task->signal->oom_score_adj = oom_adj;
964 trace_oom_score_adj_update(task);
966 unlock_task_sighand(task, &flags);
969 put_task_struct(task);
971 return err < 0 ? err : count;
974 static const struct file_operations proc_oom_adj_operations = {
975 .read = oom_adj_read,
976 .write = oom_adj_write,
977 .llseek = generic_file_llseek,
980 static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
981 size_t count, loff_t *ppos)
983 struct task_struct *task = get_proc_task(file_inode(file));
984 char buffer[PROC_NUMBUF];
985 short oom_score_adj = OOM_SCORE_ADJ_MIN;
991 if (lock_task_sighand(task, &flags)) {
992 oom_score_adj = task->signal->oom_score_adj;
993 unlock_task_sighand(task, &flags);
995 put_task_struct(task);
996 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
997 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1000 static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1001 size_t count, loff_t *ppos)
1003 struct task_struct *task;
1004 char buffer[PROC_NUMBUF];
1005 unsigned long flags;
1009 memset(buffer, 0, sizeof(buffer));
1010 if (count > sizeof(buffer) - 1)
1011 count = sizeof(buffer) - 1;
1012 if (copy_from_user(buffer, buf, count)) {
1017 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
1020 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
1021 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1026 task = get_proc_task(file_inode(file));
1038 if (!lock_task_sighand(task, &flags)) {
1043 if ((short)oom_score_adj < task->signal->oom_score_adj_min &&
1044 !capable(CAP_SYS_RESOURCE)) {
1049 task->signal->oom_score_adj = (short)oom_score_adj;
1050 if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
1051 task->signal->oom_score_adj_min = (short)oom_score_adj;
1052 trace_oom_score_adj_update(task);
1055 unlock_task_sighand(task, &flags);
1058 put_task_struct(task);
1060 return err < 0 ? err : count;
1063 static const struct file_operations proc_oom_score_adj_operations = {
1064 .read = oom_score_adj_read,
1065 .write = oom_score_adj_write,
1066 .llseek = default_llseek,
1069 #ifdef CONFIG_AUDITSYSCALL
1070 #define TMPBUFLEN 21
1071 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1072 size_t count, loff_t *ppos)
1074 struct inode * inode = file_inode(file);
1075 struct task_struct *task = get_proc_task(inode);
1077 char tmpbuf[TMPBUFLEN];
1081 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1082 from_kuid(file->f_cred->user_ns,
1083 audit_get_loginuid(task)));
1084 put_task_struct(task);
1085 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1088 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1089 size_t count, loff_t *ppos)
1091 struct inode * inode = file_inode(file);
1098 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1104 if (count >= PAGE_SIZE)
1105 count = PAGE_SIZE - 1;
1108 /* No partial writes. */
1111 page = (char*)__get_free_page(GFP_TEMPORARY);
1115 if (copy_from_user(page, buf, count))
1119 loginuid = simple_strtoul(page, &tmp, 10);
1126 /* is userspace tring to explicitly UNSET the loginuid? */
1127 if (loginuid == AUDIT_UID_UNSET) {
1128 kloginuid = INVALID_UID;
1130 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
1131 if (!uid_valid(kloginuid)) {
1137 length = audit_set_loginuid(kloginuid);
1138 if (likely(length == 0))
1142 free_page((unsigned long) page);
1146 static const struct file_operations proc_loginuid_operations = {
1147 .read = proc_loginuid_read,
1148 .write = proc_loginuid_write,
1149 .llseek = generic_file_llseek,
1152 static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1153 size_t count, loff_t *ppos)
1155 struct inode * inode = file_inode(file);
1156 struct task_struct *task = get_proc_task(inode);
1158 char tmpbuf[TMPBUFLEN];
1162 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1163 audit_get_sessionid(task));
1164 put_task_struct(task);
1165 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1168 static const struct file_operations proc_sessionid_operations = {
1169 .read = proc_sessionid_read,
1170 .llseek = generic_file_llseek,
1174 #ifdef CONFIG_FAULT_INJECTION
1175 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1176 size_t count, loff_t *ppos)
1178 struct task_struct *task = get_proc_task(file_inode(file));
1179 char buffer[PROC_NUMBUF];
1185 make_it_fail = task->make_it_fail;
1186 put_task_struct(task);
1188 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1190 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1193 static ssize_t proc_fault_inject_write(struct file * file,
1194 const char __user * buf, size_t count, loff_t *ppos)
1196 struct task_struct *task;
1197 char buffer[PROC_NUMBUF], *end;
1200 if (!capable(CAP_SYS_RESOURCE))
1202 memset(buffer, 0, sizeof(buffer));
1203 if (count > sizeof(buffer) - 1)
1204 count = sizeof(buffer) - 1;
1205 if (copy_from_user(buffer, buf, count))
1207 make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1210 if (make_it_fail < 0 || make_it_fail > 1)
1213 task = get_proc_task(file_inode(file));
1216 task->make_it_fail = make_it_fail;
1217 put_task_struct(task);
1222 static const struct file_operations proc_fault_inject_operations = {
1223 .read = proc_fault_inject_read,
1224 .write = proc_fault_inject_write,
1225 .llseek = generic_file_llseek,
1230 #ifdef CONFIG_SCHED_DEBUG
1232 * Print out various scheduling related per-task fields:
1234 static int sched_show(struct seq_file *m, void *v)
1236 struct inode *inode = m->private;
1237 struct task_struct *p;
1239 p = get_proc_task(inode);
1242 proc_sched_show_task(p, m);
1250 sched_write(struct file *file, const char __user *buf,
1251 size_t count, loff_t *offset)
1253 struct inode *inode = file_inode(file);
1254 struct task_struct *p;
1256 p = get_proc_task(inode);
1259 proc_sched_set_task(p);
1266 static int sched_open(struct inode *inode, struct file *filp)
1268 return single_open(filp, sched_show, inode);
1271 static const struct file_operations proc_pid_sched_operations = {
1274 .write = sched_write,
1275 .llseek = seq_lseek,
1276 .release = single_release,
1281 #ifdef CONFIG_SCHED_AUTOGROUP
1283 * Print out autogroup related information:
1285 static int sched_autogroup_show(struct seq_file *m, void *v)
1287 struct inode *inode = m->private;
1288 struct task_struct *p;
1290 p = get_proc_task(inode);
1293 proc_sched_autogroup_show_task(p, m);
1301 sched_autogroup_write(struct file *file, const char __user *buf,
1302 size_t count, loff_t *offset)
1304 struct inode *inode = file_inode(file);
1305 struct task_struct *p;
1306 char buffer[PROC_NUMBUF];
1310 memset(buffer, 0, sizeof(buffer));
1311 if (count > sizeof(buffer) - 1)
1312 count = sizeof(buffer) - 1;
1313 if (copy_from_user(buffer, buf, count))
1316 err = kstrtoint(strstrip(buffer), 0, &nice);
1320 p = get_proc_task(inode);
1324 err = proc_sched_autogroup_set_nice(p, nice);
1333 static int sched_autogroup_open(struct inode *inode, struct file *filp)
1337 ret = single_open(filp, sched_autogroup_show, NULL);
1339 struct seq_file *m = filp->private_data;
1346 static const struct file_operations proc_pid_sched_autogroup_operations = {
1347 .open = sched_autogroup_open,
1349 .write = sched_autogroup_write,
1350 .llseek = seq_lseek,
1351 .release = single_release,
1354 #endif /* CONFIG_SCHED_AUTOGROUP */
1356 static ssize_t comm_write(struct file *file, const char __user *buf,
1357 size_t count, loff_t *offset)
1359 struct inode *inode = file_inode(file);
1360 struct task_struct *p;
1361 char buffer[TASK_COMM_LEN];
1362 const size_t maxlen = sizeof(buffer) - 1;
1364 memset(buffer, 0, sizeof(buffer));
1365 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
1368 p = get_proc_task(inode);
1372 if (same_thread_group(current, p))
1373 set_task_comm(p, buffer);
1382 static int comm_show(struct seq_file *m, void *v)
1384 struct inode *inode = m->private;
1385 struct task_struct *p;
1387 p = get_proc_task(inode);
1392 seq_printf(m, "%s\n", p->comm);
1400 static int comm_open(struct inode *inode, struct file *filp)
1402 return single_open(filp, comm_show, inode);
1405 static const struct file_operations proc_pid_set_comm_operations = {
1408 .write = comm_write,
1409 .llseek = seq_lseek,
1410 .release = single_release,
1413 static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
1415 struct task_struct *task;
1416 struct mm_struct *mm;
1417 struct file *exe_file;
1419 task = get_proc_task(dentry->d_inode);
1422 mm = get_task_mm(task);
1423 put_task_struct(task);
1426 exe_file = get_mm_exe_file(mm);
1429 *exe_path = exe_file->f_path;
1430 path_get(&exe_file->f_path);
1437 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1439 struct inode *inode = dentry->d_inode;
1441 int error = -EACCES;
1443 /* Are we allowed to snoop on the tasks file descriptors? */
1444 if (!proc_fd_access_allowed(inode))
1447 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1451 nd_jump_link(nd, &path);
1454 return ERR_PTR(error);
1457 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1459 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
1466 pathname = d_path(path, tmp, PAGE_SIZE);
1467 len = PTR_ERR(pathname);
1468 if (IS_ERR(pathname))
1470 len = tmp + PAGE_SIZE - 1 - pathname;
1474 if (copy_to_user(buffer, pathname, len))
1477 free_page((unsigned long)tmp);
1481 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1483 int error = -EACCES;
1484 struct inode *inode = dentry->d_inode;
1487 /* Are we allowed to snoop on the tasks file descriptors? */
1488 if (!proc_fd_access_allowed(inode))
1491 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1495 error = do_proc_readlink(&path, buffer, buflen);
1501 const struct inode_operations proc_pid_link_inode_operations = {
1502 .readlink = proc_pid_readlink,
1503 .follow_link = proc_pid_follow_link,
1504 .setattr = proc_setattr,
1508 /* building an inode */
1510 struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1512 struct inode * inode;
1513 struct proc_inode *ei;
1514 const struct cred *cred;
1516 /* We need a new inode */
1518 inode = new_inode(sb);
1524 inode->i_ino = get_next_ino();
1525 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1526 inode->i_op = &proc_def_inode_operations;
1529 * grab the reference to task.
1531 ei->pid = get_task_pid(task, PIDTYPE_PID);
1535 if (task_dumpable(task)) {
1537 cred = __task_cred(task);
1538 inode->i_uid = cred->euid;
1539 inode->i_gid = cred->egid;
1542 security_task_to_inode(task, inode);
1552 int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1554 struct inode *inode = dentry->d_inode;
1555 struct task_struct *task;
1556 const struct cred *cred;
1557 struct pid_namespace *pid = dentry->d_sb->s_fs_info;
1559 generic_fillattr(inode, stat);
1562 stat->uid = GLOBAL_ROOT_UID;
1563 stat->gid = GLOBAL_ROOT_GID;
1564 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1566 if (!has_pid_permissions(pid, task, 2)) {
1569 * This doesn't prevent learning whether PID exists,
1570 * it only makes getattr() consistent with readdir().
1574 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1575 task_dumpable(task)) {
1576 cred = __task_cred(task);
1577 stat->uid = cred->euid;
1578 stat->gid = cred->egid;
1588 * Exceptional case: normally we are not allowed to unhash a busy
1589 * directory. In this case, however, we can do it - no aliasing problems
1590 * due to the way we treat inodes.
1592 * Rewrite the inode's ownerships here because the owning task may have
1593 * performed a setuid(), etc.
1595 * Before the /proc/pid/status file was created the only way to read
1596 * the effective uid of a /process was to stat /proc/pid. Reading
1597 * /proc/pid/status is slow enough that procps and other packages
1598 * kept stating /proc/pid. To keep the rules in /proc simple I have
1599 * made this apply to all per process world readable and executable
1602 int pid_revalidate(struct dentry *dentry, unsigned int flags)
1604 struct inode *inode;
1605 struct task_struct *task;
1606 const struct cred *cred;
1608 if (flags & LOOKUP_RCU)
1611 inode = dentry->d_inode;
1612 task = get_proc_task(inode);
1615 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1616 task_dumpable(task)) {
1618 cred = __task_cred(task);
1619 inode->i_uid = cred->euid;
1620 inode->i_gid = cred->egid;
1623 inode->i_uid = GLOBAL_ROOT_UID;
1624 inode->i_gid = GLOBAL_ROOT_GID;
1626 inode->i_mode &= ~(S_ISUID | S_ISGID);
1627 security_task_to_inode(task, inode);
1628 put_task_struct(task);
1635 static inline bool proc_inode_is_dead(struct inode *inode)
1637 return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1640 int pid_delete_dentry(const struct dentry *dentry)
1642 /* Is the task we represent dead?
1643 * If so, then don't put the dentry on the lru list,
1644 * kill it immediately.
1646 return proc_inode_is_dead(dentry->d_inode);
1649 const struct dentry_operations pid_dentry_operations =
1651 .d_revalidate = pid_revalidate,
1652 .d_delete = pid_delete_dentry,
1658 * Fill a directory entry.
1660 * If possible create the dcache entry and derive our inode number and
1661 * file type from dcache entry.
1663 * Since all of the proc inode numbers are dynamically generated, the inode
1664 * numbers do not exist until the inode is cache. This means creating the
1665 * the dcache entry in readdir is necessary to keep the inode numbers
1666 * reported by readdir in sync with the inode numbers reported
1669 bool proc_fill_cache(struct file *file, struct dir_context *ctx,
1670 const char *name, int len,
1671 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1673 struct dentry *child, *dir = file->f_path.dentry;
1674 struct qstr qname = QSTR_INIT(name, len);
1675 struct inode *inode;
1679 child = d_hash_and_lookup(dir, &qname);
1681 child = d_alloc(dir, &qname);
1683 goto end_instantiate;
1684 if (instantiate(dir->d_inode, child, task, ptr) < 0) {
1686 goto end_instantiate;
1689 inode = child->d_inode;
1691 type = inode->i_mode >> 12;
1693 return dir_emit(ctx, name, len, ino, type);
1696 return dir_emit(ctx, name, len, 1, DT_UNKNOWN);
1699 #ifdef CONFIG_CHECKPOINT_RESTORE
1702 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1703 * which represent vma start and end addresses.
1705 static int dname_to_vma_addr(struct dentry *dentry,
1706 unsigned long *start, unsigned long *end)
1708 if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2)
1714 static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
1716 unsigned long vm_start, vm_end;
1717 bool exact_vma_exists = false;
1718 struct mm_struct *mm = NULL;
1719 struct task_struct *task;
1720 const struct cred *cred;
1721 struct inode *inode;
1724 if (flags & LOOKUP_RCU)
1727 if (!capable(CAP_SYS_ADMIN)) {
1732 inode = dentry->d_inode;
1733 task = get_proc_task(inode);
1737 mm = mm_access(task, PTRACE_MODE_READ);
1738 if (IS_ERR_OR_NULL(mm))
1741 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1742 down_read(&mm->mmap_sem);
1743 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1744 up_read(&mm->mmap_sem);
1749 if (exact_vma_exists) {
1750 if (task_dumpable(task)) {
1752 cred = __task_cred(task);
1753 inode->i_uid = cred->euid;
1754 inode->i_gid = cred->egid;
1757 inode->i_uid = GLOBAL_ROOT_UID;
1758 inode->i_gid = GLOBAL_ROOT_GID;
1760 security_task_to_inode(task, inode);
1765 put_task_struct(task);
1774 static const struct dentry_operations tid_map_files_dentry_operations = {
1775 .d_revalidate = map_files_d_revalidate,
1776 .d_delete = pid_delete_dentry,
1779 static int proc_map_files_get_link(struct dentry *dentry, struct path *path)
1781 unsigned long vm_start, vm_end;
1782 struct vm_area_struct *vma;
1783 struct task_struct *task;
1784 struct mm_struct *mm;
1788 task = get_proc_task(dentry->d_inode);
1792 mm = get_task_mm(task);
1793 put_task_struct(task);
1797 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
1802 down_read(&mm->mmap_sem);
1803 vma = find_exact_vma(mm, vm_start, vm_end);
1804 if (vma && vma->vm_file) {
1805 *path = vma->vm_file->f_path;
1809 up_read(&mm->mmap_sem);
1817 struct map_files_info {
1820 unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
1824 proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
1825 struct task_struct *task, const void *ptr)
1827 fmode_t mode = (fmode_t)(unsigned long)ptr;
1828 struct proc_inode *ei;
1829 struct inode *inode;
1831 inode = proc_pid_make_inode(dir->i_sb, task);
1836 ei->op.proc_get_link = proc_map_files_get_link;
1838 inode->i_op = &proc_pid_link_inode_operations;
1840 inode->i_mode = S_IFLNK;
1842 if (mode & FMODE_READ)
1843 inode->i_mode |= S_IRUSR;
1844 if (mode & FMODE_WRITE)
1845 inode->i_mode |= S_IWUSR;
1847 d_set_d_op(dentry, &tid_map_files_dentry_operations);
1848 d_add(dentry, inode);
1853 static struct dentry *proc_map_files_lookup(struct inode *dir,
1854 struct dentry *dentry, unsigned int flags)
1856 unsigned long vm_start, vm_end;
1857 struct vm_area_struct *vma;
1858 struct task_struct *task;
1860 struct mm_struct *mm;
1863 if (!capable(CAP_SYS_ADMIN))
1867 task = get_proc_task(dir);
1872 if (!ptrace_may_access(task, PTRACE_MODE_READ))
1876 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
1879 mm = get_task_mm(task);
1883 down_read(&mm->mmap_sem);
1884 vma = find_exact_vma(mm, vm_start, vm_end);
1889 result = proc_map_files_instantiate(dir, dentry, task,
1890 (void *)(unsigned long)vma->vm_file->f_mode);
1893 up_read(&mm->mmap_sem);
1896 put_task_struct(task);
1898 return ERR_PTR(result);
1901 static const struct inode_operations proc_map_files_inode_operations = {
1902 .lookup = proc_map_files_lookup,
1903 .permission = proc_fd_permission,
1904 .setattr = proc_setattr,
1908 proc_map_files_readdir(struct file *file, struct dir_context *ctx)
1910 struct vm_area_struct *vma;
1911 struct task_struct *task;
1912 struct mm_struct *mm;
1913 unsigned long nr_files, pos, i;
1914 struct flex_array *fa = NULL;
1915 struct map_files_info info;
1916 struct map_files_info *p;
1920 if (!capable(CAP_SYS_ADMIN))
1924 task = get_proc_task(file_inode(file));
1929 if (!ptrace_may_access(task, PTRACE_MODE_READ))
1933 if (!dir_emit_dots(file, ctx))
1936 mm = get_task_mm(task);
1939 down_read(&mm->mmap_sem);
1944 * We need two passes here:
1946 * 1) Collect vmas of mapped files with mmap_sem taken
1947 * 2) Release mmap_sem and instantiate entries
1949 * otherwise we get lockdep complained, since filldir()
1950 * routine might require mmap_sem taken in might_fault().
1953 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
1954 if (vma->vm_file && ++pos > ctx->pos)
1959 fa = flex_array_alloc(sizeof(info), nr_files,
1961 if (!fa || flex_array_prealloc(fa, 0, nr_files,
1965 flex_array_free(fa);
1966 up_read(&mm->mmap_sem);
1970 for (i = 0, vma = mm->mmap, pos = 2; vma;
1971 vma = vma->vm_next) {
1974 if (++pos <= ctx->pos)
1977 info.mode = vma->vm_file->f_mode;
1978 info.len = snprintf(info.name,
1979 sizeof(info.name), "%lx-%lx",
1980 vma->vm_start, vma->vm_end);
1981 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
1985 up_read(&mm->mmap_sem);
1987 for (i = 0; i < nr_files; i++) {
1988 p = flex_array_get(fa, i);
1989 if (!proc_fill_cache(file, ctx,
1991 proc_map_files_instantiate,
1993 (void *)(unsigned long)p->mode))
1998 flex_array_free(fa);
2002 put_task_struct(task);
2007 static const struct file_operations proc_map_files_operations = {
2008 .read = generic_read_dir,
2009 .iterate = proc_map_files_readdir,
2010 .llseek = default_llseek,
2013 struct timers_private {
2015 struct task_struct *task;
2016 struct sighand_struct *sighand;
2017 struct pid_namespace *ns;
2018 unsigned long flags;
2021 static void *timers_start(struct seq_file *m, loff_t *pos)
2023 struct timers_private *tp = m->private;
2025 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2027 return ERR_PTR(-ESRCH);
2029 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2031 return ERR_PTR(-ESRCH);
2033 return seq_list_start(&tp->task->signal->posix_timers, *pos);
2036 static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2038 struct timers_private *tp = m->private;
2039 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2042 static void timers_stop(struct seq_file *m, void *v)
2044 struct timers_private *tp = m->private;
2047 unlock_task_sighand(tp->task, &tp->flags);
2052 put_task_struct(tp->task);
2057 static int show_timer(struct seq_file *m, void *v)
2059 struct k_itimer *timer;
2060 struct timers_private *tp = m->private;
2062 static const char * const nstr[] = {
2063 [SIGEV_SIGNAL] = "signal",
2064 [SIGEV_NONE] = "none",
2065 [SIGEV_THREAD] = "thread",
2068 timer = list_entry((struct list_head *)v, struct k_itimer, list);
2069 notify = timer->it_sigev_notify;
2071 seq_printf(m, "ID: %d\n", timer->it_id);
2072 seq_printf(m, "signal: %d/%p\n", timer->sigq->info.si_signo,
2073 timer->sigq->info.si_value.sival_ptr);
2074 seq_printf(m, "notify: %s/%s.%d\n",
2075 nstr[notify & ~SIGEV_THREAD_ID],
2076 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2077 pid_nr_ns(timer->it_pid, tp->ns));
2078 seq_printf(m, "ClockID: %d\n", timer->it_clock);
2083 static const struct seq_operations proc_timers_seq_ops = {
2084 .start = timers_start,
2085 .next = timers_next,
2086 .stop = timers_stop,
2090 static int proc_timers_open(struct inode *inode, struct file *file)
2092 struct timers_private *tp;
2094 tp = __seq_open_private(file, &proc_timers_seq_ops,
2095 sizeof(struct timers_private));
2099 tp->pid = proc_pid(inode);
2100 tp->ns = inode->i_sb->s_fs_info;
2104 static const struct file_operations proc_timers_operations = {
2105 .open = proc_timers_open,
2107 .llseek = seq_lseek,
2108 .release = seq_release_private,
2110 #endif /* CONFIG_CHECKPOINT_RESTORE */
2112 static int proc_pident_instantiate(struct inode *dir,
2113 struct dentry *dentry, struct task_struct *task, const void *ptr)
2115 const struct pid_entry *p = ptr;
2116 struct inode *inode;
2117 struct proc_inode *ei;
2119 inode = proc_pid_make_inode(dir->i_sb, task);
2124 inode->i_mode = p->mode;
2125 if (S_ISDIR(inode->i_mode))
2126 set_nlink(inode, 2); /* Use getattr to fix if necessary */
2128 inode->i_op = p->iop;
2130 inode->i_fop = p->fop;
2132 d_set_d_op(dentry, &pid_dentry_operations);
2133 d_add(dentry, inode);
2134 /* Close the race of the process dying before we return the dentry */
2135 if (pid_revalidate(dentry, 0))
2141 static struct dentry *proc_pident_lookup(struct inode *dir,
2142 struct dentry *dentry,
2143 const struct pid_entry *ents,
2147 struct task_struct *task = get_proc_task(dir);
2148 const struct pid_entry *p, *last;
2156 * Yes, it does not scale. And it should not. Don't add
2157 * new entries into /proc/<tgid>/ without very good reasons.
2159 last = &ents[nents - 1];
2160 for (p = ents; p <= last; p++) {
2161 if (p->len != dentry->d_name.len)
2163 if (!memcmp(dentry->d_name.name, p->name, p->len))
2169 error = proc_pident_instantiate(dir, dentry, task, p);
2171 put_task_struct(task);
2173 return ERR_PTR(error);
2176 static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
2177 const struct pid_entry *ents, unsigned int nents)
2179 struct task_struct *task = get_proc_task(file_inode(file));
2180 const struct pid_entry *p;
2185 if (!dir_emit_dots(file, ctx))
2188 if (ctx->pos >= nents + 2)
2191 for (p = ents + (ctx->pos - 2); p <= ents + nents - 1; p++) {
2192 if (!proc_fill_cache(file, ctx, p->name, p->len,
2193 proc_pident_instantiate, task, p))
2198 put_task_struct(task);
2202 #ifdef CONFIG_SECURITY
2203 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2204 size_t count, loff_t *ppos)
2206 struct inode * inode = file_inode(file);
2209 struct task_struct *task = get_proc_task(inode);
2214 length = security_getprocattr(task,
2215 (char*)file->f_path.dentry->d_name.name,
2217 put_task_struct(task);
2219 length = simple_read_from_buffer(buf, count, ppos, p, length);
2224 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2225 size_t count, loff_t *ppos)
2227 struct inode * inode = file_inode(file);
2230 struct task_struct *task = get_proc_task(inode);
2235 if (count > PAGE_SIZE)
2238 /* No partial writes. */
2244 page = (char*)__get_free_page(GFP_TEMPORARY);
2249 if (copy_from_user(page, buf, count))
2252 /* Guard against adverse ptrace interaction */
2253 length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
2257 length = security_setprocattr(task,
2258 (char*)file->f_path.dentry->d_name.name,
2259 (void*)page, count);
2260 mutex_unlock(&task->signal->cred_guard_mutex);
2262 free_page((unsigned long) page);
2264 put_task_struct(task);
2269 static const struct file_operations proc_pid_attr_operations = {
2270 .read = proc_pid_attr_read,
2271 .write = proc_pid_attr_write,
2272 .llseek = generic_file_llseek,
2275 static const struct pid_entry attr_dir_stuff[] = {
2276 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2277 REG("prev", S_IRUGO, proc_pid_attr_operations),
2278 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2279 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2280 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2281 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2284 static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
2286 return proc_pident_readdir(file, ctx,
2287 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2290 static const struct file_operations proc_attr_dir_operations = {
2291 .read = generic_read_dir,
2292 .iterate = proc_attr_dir_readdir,
2293 .llseek = default_llseek,
2296 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2297 struct dentry *dentry, unsigned int flags)
2299 return proc_pident_lookup(dir, dentry,
2300 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2303 static const struct inode_operations proc_attr_dir_inode_operations = {
2304 .lookup = proc_attr_dir_lookup,
2305 .getattr = pid_getattr,
2306 .setattr = proc_setattr,
2311 #ifdef CONFIG_ELF_CORE
2312 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2313 size_t count, loff_t *ppos)
2315 struct task_struct *task = get_proc_task(file_inode(file));
2316 struct mm_struct *mm;
2317 char buffer[PROC_NUMBUF];
2325 mm = get_task_mm(task);
2327 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2328 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2329 MMF_DUMP_FILTER_SHIFT));
2331 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2334 put_task_struct(task);
2339 static ssize_t proc_coredump_filter_write(struct file *file,
2340 const char __user *buf,
2344 struct task_struct *task;
2345 struct mm_struct *mm;
2346 char buffer[PROC_NUMBUF], *end;
2353 memset(buffer, 0, sizeof(buffer));
2354 if (count > sizeof(buffer) - 1)
2355 count = sizeof(buffer) - 1;
2356 if (copy_from_user(buffer, buf, count))
2360 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2363 if (end - buffer == 0)
2367 task = get_proc_task(file_inode(file));
2372 mm = get_task_mm(task);
2376 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2378 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2380 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2385 put_task_struct(task);
2390 static const struct file_operations proc_coredump_filter_operations = {
2391 .read = proc_coredump_filter_read,
2392 .write = proc_coredump_filter_write,
2393 .llseek = generic_file_llseek,
2397 #ifdef CONFIG_TASK_IO_ACCOUNTING
2398 static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
2400 struct task_io_accounting acct = task->ioac;
2401 unsigned long flags;
2404 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2408 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
2413 if (whole && lock_task_sighand(task, &flags)) {
2414 struct task_struct *t = task;
2416 task_io_accounting_add(&acct, &task->signal->ioac);
2417 while_each_thread(task, t)
2418 task_io_accounting_add(&acct, &t->ioac);
2420 unlock_task_sighand(task, &flags);
2422 result = sprintf(buffer,
2427 "read_bytes: %llu\n"
2428 "write_bytes: %llu\n"
2429 "cancelled_write_bytes: %llu\n",
2430 (unsigned long long)acct.rchar,
2431 (unsigned long long)acct.wchar,
2432 (unsigned long long)acct.syscr,
2433 (unsigned long long)acct.syscw,
2434 (unsigned long long)acct.read_bytes,
2435 (unsigned long long)acct.write_bytes,
2436 (unsigned long long)acct.cancelled_write_bytes);
2438 mutex_unlock(&task->signal->cred_guard_mutex);
2442 static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2444 return do_io_accounting(task, buffer, 0);
2447 static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2449 return do_io_accounting(task, buffer, 1);
2451 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2453 #ifdef CONFIG_USER_NS
2454 static int proc_id_map_open(struct inode *inode, struct file *file,
2455 const struct seq_operations *seq_ops)
2457 struct user_namespace *ns = NULL;
2458 struct task_struct *task;
2459 struct seq_file *seq;
2462 task = get_proc_task(inode);
2465 ns = get_user_ns(task_cred_xxx(task, user_ns));
2467 put_task_struct(task);
2472 ret = seq_open(file, seq_ops);
2476 seq = file->private_data;
2486 static int proc_id_map_release(struct inode *inode, struct file *file)
2488 struct seq_file *seq = file->private_data;
2489 struct user_namespace *ns = seq->private;
2491 return seq_release(inode, file);
2494 static int proc_uid_map_open(struct inode *inode, struct file *file)
2496 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2499 static int proc_gid_map_open(struct inode *inode, struct file *file)
2501 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2504 static int proc_projid_map_open(struct inode *inode, struct file *file)
2506 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2509 static const struct file_operations proc_uid_map_operations = {
2510 .open = proc_uid_map_open,
2511 .write = proc_uid_map_write,
2513 .llseek = seq_lseek,
2514 .release = proc_id_map_release,
2517 static const struct file_operations proc_gid_map_operations = {
2518 .open = proc_gid_map_open,
2519 .write = proc_gid_map_write,
2521 .llseek = seq_lseek,
2522 .release = proc_id_map_release,
2525 static const struct file_operations proc_projid_map_operations = {
2526 .open = proc_projid_map_open,
2527 .write = proc_projid_map_write,
2529 .llseek = seq_lseek,
2530 .release = proc_id_map_release,
2532 #endif /* CONFIG_USER_NS */
2534 static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2535 struct pid *pid, struct task_struct *task)
2537 int err = lock_trace(task);
2539 seq_printf(m, "%08x\n", task->personality);
2548 static const struct file_operations proc_task_operations;
2549 static const struct inode_operations proc_task_inode_operations;
2551 static const struct pid_entry tgid_base_stuff[] = {
2552 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2553 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2554 #ifdef CONFIG_CHECKPOINT_RESTORE
2555 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
2557 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2558 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
2560 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2562 REG("environ", S_IRUSR, proc_environ_operations),
2563 ONE("auxv", S_IRUSR, proc_pid_auxv),
2564 ONE("status", S_IRUGO, proc_pid_status),
2565 ONE("personality", S_IRUSR, proc_pid_personality),
2566 ONE("limits", S_IRUGO, proc_pid_limits),
2567 #ifdef CONFIG_SCHED_DEBUG
2568 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2570 #ifdef CONFIG_SCHED_AUTOGROUP
2571 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2573 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2574 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2575 ONE("syscall", S_IRUSR, proc_pid_syscall),
2577 ONE("cmdline", S_IRUGO, proc_pid_cmdline),
2578 ONE("stat", S_IRUGO, proc_tgid_stat),
2579 ONE("statm", S_IRUGO, proc_pid_statm),
2580 REG("maps", S_IRUGO, proc_pid_maps_operations),
2582 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
2584 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2585 LNK("cwd", proc_cwd_link),
2586 LNK("root", proc_root_link),
2587 LNK("exe", proc_exe_link),
2588 REG("mounts", S_IRUGO, proc_mounts_operations),
2589 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2590 REG("mountstats", S_IRUSR, proc_mountstats_operations),
2591 #ifdef CONFIG_PROC_PAGE_MONITOR
2592 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2593 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
2594 REG("pagemap", S_IRUSR, proc_pagemap_operations),
2596 #ifdef CONFIG_SECURITY
2597 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
2599 #ifdef CONFIG_KALLSYMS
2600 INF("wchan", S_IRUGO, proc_pid_wchan),
2602 #ifdef CONFIG_STACKTRACE
2603 ONE("stack", S_IRUSR, proc_pid_stack),
2605 #ifdef CONFIG_SCHEDSTATS
2606 INF("schedstat", S_IRUGO, proc_pid_schedstat),
2608 #ifdef CONFIG_LATENCYTOP
2609 REG("latency", S_IRUGO, proc_lstats_operations),
2611 #ifdef CONFIG_PROC_PID_CPUSET
2612 REG("cpuset", S_IRUGO, proc_cpuset_operations),
2614 #ifdef CONFIG_CGROUPS
2615 REG("cgroup", S_IRUGO, proc_cgroup_operations),
2617 INF("oom_score", S_IRUGO, proc_oom_score),
2618 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
2619 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
2620 #ifdef CONFIG_AUDITSYSCALL
2621 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2622 REG("sessionid", S_IRUGO, proc_sessionid_operations),
2624 #ifdef CONFIG_FAULT_INJECTION
2625 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2627 #ifdef CONFIG_ELF_CORE
2628 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
2630 #ifdef CONFIG_TASK_IO_ACCOUNTING
2631 INF("io", S_IRUSR, proc_tgid_io_accounting),
2633 #ifdef CONFIG_HARDWALL
2634 INF("hardwall", S_IRUGO, proc_pid_hardwall),
2636 #ifdef CONFIG_USER_NS
2637 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2638 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
2639 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
2641 #ifdef CONFIG_CHECKPOINT_RESTORE
2642 REG("timers", S_IRUGO, proc_timers_operations),
2646 static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
2648 return proc_pident_readdir(file, ctx,
2649 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2652 static const struct file_operations proc_tgid_base_operations = {
2653 .read = generic_read_dir,
2654 .iterate = proc_tgid_base_readdir,
2655 .llseek = default_llseek,
2658 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2660 return proc_pident_lookup(dir, dentry,
2661 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2664 static const struct inode_operations proc_tgid_base_inode_operations = {
2665 .lookup = proc_tgid_base_lookup,
2666 .getattr = pid_getattr,
2667 .setattr = proc_setattr,
2668 .permission = proc_pid_permission,
2671 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
2673 struct dentry *dentry, *leader, *dir;
2674 char buf[PROC_NUMBUF];
2678 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2679 /* no ->d_hash() rejects on procfs */
2680 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
2682 shrink_dcache_parent(dentry);
2688 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2689 leader = d_hash_and_lookup(mnt->mnt_root, &name);
2694 name.len = strlen(name.name);
2695 dir = d_hash_and_lookup(leader, &name);
2697 goto out_put_leader;
2700 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2701 dentry = d_hash_and_lookup(dir, &name);
2703 shrink_dcache_parent(dentry);
2716 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2717 * @task: task that should be flushed.
2719 * When flushing dentries from proc, one needs to flush them from global
2720 * proc (proc_mnt) and from all the namespaces' procs this task was seen
2721 * in. This call is supposed to do all of this job.
2723 * Looks in the dcache for
2725 * /proc/@tgid/task/@pid
2726 * if either directory is present flushes it and all of it'ts children
2729 * It is safe and reasonable to cache /proc entries for a task until
2730 * that task exits. After that they just clog up the dcache with
2731 * useless entries, possibly causing useful dcache entries to be
2732 * flushed instead. This routine is proved to flush those useless
2733 * dcache entries at process exit time.
2735 * NOTE: This routine is just an optimization so it does not guarantee
2736 * that no dcache entries will exist at process exit time it
2737 * just makes it very unlikely that any will persist.
2740 void proc_flush_task(struct task_struct *task)
2743 struct pid *pid, *tgid;
2746 pid = task_pid(task);
2747 tgid = task_tgid(task);
2749 for (i = 0; i <= pid->level; i++) {
2750 upid = &pid->numbers[i];
2751 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
2752 tgid->numbers[i].nr);
2756 static int proc_pid_instantiate(struct inode *dir,
2757 struct dentry * dentry,
2758 struct task_struct *task, const void *ptr)
2760 struct inode *inode;
2762 inode = proc_pid_make_inode(dir->i_sb, task);
2766 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2767 inode->i_op = &proc_tgid_base_inode_operations;
2768 inode->i_fop = &proc_tgid_base_operations;
2769 inode->i_flags|=S_IMMUTABLE;
2771 set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
2772 ARRAY_SIZE(tgid_base_stuff)));
2774 d_set_d_op(dentry, &pid_dentry_operations);
2776 d_add(dentry, inode);
2777 /* Close the race of the process dying before we return the dentry */
2778 if (pid_revalidate(dentry, 0))
2784 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
2786 int result = -ENOENT;
2787 struct task_struct *task;
2789 struct pid_namespace *ns;
2791 tgid = name_to_int(&dentry->d_name);
2795 ns = dentry->d_sb->s_fs_info;
2797 task = find_task_by_pid_ns(tgid, ns);
2799 get_task_struct(task);
2804 result = proc_pid_instantiate(dir, dentry, task, NULL);
2805 put_task_struct(task);
2807 return ERR_PTR(result);
2811 * Find the first task with tgid >= tgid
2816 struct task_struct *task;
2818 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2823 put_task_struct(iter.task);
2827 pid = find_ge_pid(iter.tgid, ns);
2829 iter.tgid = pid_nr_ns(pid, ns);
2830 iter.task = pid_task(pid, PIDTYPE_PID);
2831 /* What we to know is if the pid we have find is the
2832 * pid of a thread_group_leader. Testing for task
2833 * being a thread_group_leader is the obvious thing
2834 * todo but there is a window when it fails, due to
2835 * the pid transfer logic in de_thread.
2837 * So we perform the straight forward test of seeing
2838 * if the pid we have found is the pid of a thread
2839 * group leader, and don't worry if the task we have
2840 * found doesn't happen to be a thread group leader.
2841 * As we don't care in the case of readdir.
2843 if (!iter.task || !has_group_leader_pid(iter.task)) {
2847 get_task_struct(iter.task);
2853 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + 1)
2855 /* for the /proc/ directory itself, after non-process stuff has been done */
2856 int proc_pid_readdir(struct file *file, struct dir_context *ctx)
2858 struct tgid_iter iter;
2859 struct pid_namespace *ns = file->f_dentry->d_sb->s_fs_info;
2860 loff_t pos = ctx->pos;
2862 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
2865 if (pos == TGID_OFFSET - 1) {
2866 struct inode *inode = ns->proc_self->d_inode;
2867 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
2871 iter.tgid = pos - TGID_OFFSET;
2874 for (iter = next_tgid(ns, iter);
2876 iter.tgid += 1, iter = next_tgid(ns, iter)) {
2877 char name[PROC_NUMBUF];
2879 if (!has_pid_permissions(ns, iter.task, 2))
2882 len = snprintf(name, sizeof(name), "%d", iter.tgid);
2883 ctx->pos = iter.tgid + TGID_OFFSET;
2884 if (!proc_fill_cache(file, ctx, name, len,
2885 proc_pid_instantiate, iter.task, NULL)) {
2886 put_task_struct(iter.task);
2890 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
2897 static const struct pid_entry tid_base_stuff[] = {
2898 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2899 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2900 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
2901 REG("environ", S_IRUSR, proc_environ_operations),
2902 ONE("auxv", S_IRUSR, proc_pid_auxv),
2903 ONE("status", S_IRUGO, proc_pid_status),
2904 ONE("personality", S_IRUSR, proc_pid_personality),
2905 ONE("limits", S_IRUGO, proc_pid_limits),
2906 #ifdef CONFIG_SCHED_DEBUG
2907 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2909 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2910 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2911 ONE("syscall", S_IRUSR, proc_pid_syscall),
2913 ONE("cmdline", S_IRUGO, proc_pid_cmdline),
2914 ONE("stat", S_IRUGO, proc_tid_stat),
2915 ONE("statm", S_IRUGO, proc_pid_statm),
2916 REG("maps", S_IRUGO, proc_tid_maps_operations),
2917 #ifdef CONFIG_CHECKPOINT_RESTORE
2918 REG("children", S_IRUGO, proc_tid_children_operations),
2921 REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
2923 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2924 LNK("cwd", proc_cwd_link),
2925 LNK("root", proc_root_link),
2926 LNK("exe", proc_exe_link),
2927 REG("mounts", S_IRUGO, proc_mounts_operations),
2928 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2929 #ifdef CONFIG_PROC_PAGE_MONITOR
2930 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2931 REG("smaps", S_IRUGO, proc_tid_smaps_operations),
2932 REG("pagemap", S_IRUSR, proc_pagemap_operations),
2934 #ifdef CONFIG_SECURITY
2935 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
2937 #ifdef CONFIG_KALLSYMS
2938 INF("wchan", S_IRUGO, proc_pid_wchan),
2940 #ifdef CONFIG_STACKTRACE
2941 ONE("stack", S_IRUSR, proc_pid_stack),
2943 #ifdef CONFIG_SCHEDSTATS
2944 INF("schedstat", S_IRUGO, proc_pid_schedstat),
2946 #ifdef CONFIG_LATENCYTOP
2947 REG("latency", S_IRUGO, proc_lstats_operations),
2949 #ifdef CONFIG_PROC_PID_CPUSET
2950 REG("cpuset", S_IRUGO, proc_cpuset_operations),
2952 #ifdef CONFIG_CGROUPS
2953 REG("cgroup", S_IRUGO, proc_cgroup_operations),
2955 INF("oom_score", S_IRUGO, proc_oom_score),
2956 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
2957 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
2958 #ifdef CONFIG_AUDITSYSCALL
2959 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2960 REG("sessionid", S_IRUGO, proc_sessionid_operations),
2962 #ifdef CONFIG_FAULT_INJECTION
2963 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2965 #ifdef CONFIG_TASK_IO_ACCOUNTING
2966 INF("io", S_IRUSR, proc_tid_io_accounting),
2968 #ifdef CONFIG_HARDWALL
2969 INF("hardwall", S_IRUGO, proc_pid_hardwall),
2971 #ifdef CONFIG_USER_NS
2972 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2973 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
2974 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
2978 static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
2980 return proc_pident_readdir(file, ctx,
2981 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
2984 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2986 return proc_pident_lookup(dir, dentry,
2987 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
2990 static const struct file_operations proc_tid_base_operations = {
2991 .read = generic_read_dir,
2992 .iterate = proc_tid_base_readdir,
2993 .llseek = default_llseek,
2996 static const struct inode_operations proc_tid_base_inode_operations = {
2997 .lookup = proc_tid_base_lookup,
2998 .getattr = pid_getattr,
2999 .setattr = proc_setattr,
3002 static int proc_task_instantiate(struct inode *dir,
3003 struct dentry *dentry, struct task_struct *task, const void *ptr)
3005 struct inode *inode;
3006 inode = proc_pid_make_inode(dir->i_sb, task);
3010 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3011 inode->i_op = &proc_tid_base_inode_operations;
3012 inode->i_fop = &proc_tid_base_operations;
3013 inode->i_flags|=S_IMMUTABLE;
3015 set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3016 ARRAY_SIZE(tid_base_stuff)));
3018 d_set_d_op(dentry, &pid_dentry_operations);
3020 d_add(dentry, inode);
3021 /* Close the race of the process dying before we return the dentry */
3022 if (pid_revalidate(dentry, 0))
3028 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
3030 int result = -ENOENT;
3031 struct task_struct *task;
3032 struct task_struct *leader = get_proc_task(dir);
3034 struct pid_namespace *ns;
3039 tid = name_to_int(&dentry->d_name);
3043 ns = dentry->d_sb->s_fs_info;
3045 task = find_task_by_pid_ns(tid, ns);
3047 get_task_struct(task);
3051 if (!same_thread_group(leader, task))
3054 result = proc_task_instantiate(dir, dentry, task, NULL);
3056 put_task_struct(task);
3058 put_task_struct(leader);
3060 return ERR_PTR(result);
3064 * Find the first tid of a thread group to return to user space.
3066 * Usually this is just the thread group leader, but if the users
3067 * buffer was too small or there was a seek into the middle of the
3068 * directory we have more work todo.
3070 * In the case of a short read we start with find_task_by_pid.
3072 * In the case of a seek we start with the leader and walk nr
3075 static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
3076 struct pid_namespace *ns)
3078 struct task_struct *pos, *task;
3079 unsigned long nr = f_pos;
3081 if (nr != f_pos) /* 32bit overflow? */
3085 task = pid_task(pid, PIDTYPE_PID);
3089 /* Attempt to start with the tid of a thread */
3091 pos = find_task_by_pid_ns(tid, ns);
3092 if (pos && same_thread_group(pos, task))
3096 /* If nr exceeds the number of threads there is nothing todo */
3097 if (nr >= get_nr_threads(task))
3100 /* If we haven't found our starting place yet start
3101 * with the leader and walk nr threads forward.
3103 pos = task = task->group_leader;
3107 } while_each_thread(task, pos);
3112 get_task_struct(pos);
3119 * Find the next thread in the thread list.
3120 * Return NULL if there is an error or no next thread.
3122 * The reference to the input task_struct is released.
3124 static struct task_struct *next_tid(struct task_struct *start)
3126 struct task_struct *pos = NULL;
3128 if (pid_alive(start)) {
3129 pos = next_thread(start);
3130 if (thread_group_leader(pos))
3133 get_task_struct(pos);
3136 put_task_struct(start);
3140 /* for the /proc/TGID/task/ directories */
3141 static int proc_task_readdir(struct file *file, struct dir_context *ctx)
3143 struct inode *inode = file_inode(file);
3144 struct task_struct *task;
3145 struct pid_namespace *ns;
3148 if (proc_inode_is_dead(inode))
3151 if (!dir_emit_dots(file, ctx))
3154 /* f_version caches the tgid value that the last readdir call couldn't
3155 * return. lseek aka telldir automagically resets f_version to 0.
3157 ns = file->f_dentry->d_sb->s_fs_info;
3158 tid = (int)file->f_version;
3159 file->f_version = 0;
3160 for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
3162 task = next_tid(task), ctx->pos++) {
3163 char name[PROC_NUMBUF];
3165 tid = task_pid_nr_ns(task, ns);
3166 len = snprintf(name, sizeof(name), "%d", tid);
3167 if (!proc_fill_cache(file, ctx, name, len,
3168 proc_task_instantiate, task, NULL)) {
3169 /* returning this tgid failed, save it as the first
3170 * pid for the next readir call */
3171 file->f_version = (u64)tid;
3172 put_task_struct(task);
3180 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3182 struct inode *inode = dentry->d_inode;
3183 struct task_struct *p = get_proc_task(inode);
3184 generic_fillattr(inode, stat);
3187 stat->nlink += get_nr_threads(p);
3194 static const struct inode_operations proc_task_inode_operations = {
3195 .lookup = proc_task_lookup,
3196 .getattr = proc_task_getattr,
3197 .setattr = proc_setattr,
3198 .permission = proc_pid_permission,
3201 static const struct file_operations proc_task_operations = {
3202 .read = generic_read_dir,
3203 .iterate = proc_task_readdir,
3204 .llseek = default_llseek,