dm thin metadata: fix __udivdi3 undefined on 32-bit
[linux/fpc-iii.git] / fs / proc / base.c
blob4beed301e2245d4e6dce35e067cbda4150631b88
1 /*
2 * linux/fs/proc/base.c
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.
16 * Changelog:
17 * 17-Jan-2005
18 * Allan Bezerra
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.
33 * Changelog:
34 * 21-Feb-2005
35 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36 * Pud inclusion in the page table walking.
38 * ChangeLog:
39 * 10-Mar-2005
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>
65 #include <linux/mm.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>
92 #endif
93 #include <trace/events/oom.h>
94 #include "internal.h"
95 #include "fd.h"
97 #include "../../lib/kstrtox.h"
99 /* NOTE:
100 * Implementing inode permission operations in /proc is almost
101 * certainly an error. Permission checks need to happen during
102 * each system call not at open time. The reason is that most of
103 * what we wish to check for permissions in /proc varies at runtime.
105 * The classic example of a problem is opening file descriptors
106 * in /proc for a task before it execs a suid executable.
109 struct pid_entry {
110 const char *name;
111 int len;
112 umode_t mode;
113 const struct inode_operations *iop;
114 const struct file_operations *fop;
115 union proc_op op;
118 #define NOD(NAME, MODE, IOP, FOP, OP) { \
119 .name = (NAME), \
120 .len = sizeof(NAME) - 1, \
121 .mode = MODE, \
122 .iop = IOP, \
123 .fop = FOP, \
124 .op = OP, \
127 #define DIR(NAME, MODE, iops, fops) \
128 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
129 #define LNK(NAME, get_link) \
130 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
131 &proc_pid_link_inode_operations, NULL, \
132 { .proc_get_link = get_link } )
133 #define REG(NAME, MODE, fops) \
134 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
135 #define ONE(NAME, MODE, show) \
136 NOD(NAME, (S_IFREG|(MODE)), \
137 NULL, &proc_single_file_operations, \
138 { .proc_show = show } )
141 * Count the number of hardlinks for the pid_entry table, excluding the .
142 * and .. links.
144 static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
145 unsigned int n)
147 unsigned int i;
148 unsigned int count;
150 count = 0;
151 for (i = 0; i < n; ++i) {
152 if (S_ISDIR(entries[i].mode))
153 ++count;
156 return count;
159 static int get_task_root(struct task_struct *task, struct path *root)
161 int result = -ENOENT;
163 task_lock(task);
164 if (task->fs) {
165 get_fs_root(task->fs, root);
166 result = 0;
168 task_unlock(task);
169 return result;
172 static int proc_cwd_link(struct dentry *dentry, struct path *path)
174 struct task_struct *task = get_proc_task(d_inode(dentry));
175 int result = -ENOENT;
177 if (task) {
178 task_lock(task);
179 if (task->fs) {
180 get_fs_pwd(task->fs, path);
181 result = 0;
183 task_unlock(task);
184 put_task_struct(task);
186 return result;
189 static int proc_root_link(struct dentry *dentry, struct path *path)
191 struct task_struct *task = get_proc_task(d_inode(dentry));
192 int result = -ENOENT;
194 if (task) {
195 result = get_task_root(task, path);
196 put_task_struct(task);
198 return result;
201 static ssize_t proc_pid_cmdline_read(struct file *file, char __user *buf,
202 size_t _count, loff_t *pos)
204 struct task_struct *tsk;
205 struct mm_struct *mm;
206 char *page;
207 unsigned long count = _count;
208 unsigned long arg_start, arg_end, env_start, env_end;
209 unsigned long len1, len2, len;
210 unsigned long p;
211 char c;
212 ssize_t rv;
214 BUG_ON(*pos < 0);
216 tsk = get_proc_task(file_inode(file));
217 if (!tsk)
218 return -ESRCH;
219 mm = get_task_mm(tsk);
220 put_task_struct(tsk);
221 if (!mm)
222 return 0;
223 /* Check if process spawned far enough to have cmdline. */
224 if (!mm->env_end) {
225 rv = 0;
226 goto out_mmput;
229 page = (char *)__get_free_page(GFP_TEMPORARY);
230 if (!page) {
231 rv = -ENOMEM;
232 goto out_mmput;
235 down_read(&mm->mmap_sem);
236 arg_start = mm->arg_start;
237 arg_end = mm->arg_end;
238 env_start = mm->env_start;
239 env_end = mm->env_end;
240 up_read(&mm->mmap_sem);
242 BUG_ON(arg_start > arg_end);
243 BUG_ON(env_start > env_end);
245 len1 = arg_end - arg_start;
246 len2 = env_end - env_start;
248 /* Empty ARGV. */
249 if (len1 == 0) {
250 rv = 0;
251 goto out_free_page;
254 * Inherently racy -- command line shares address space
255 * with code and data.
257 rv = access_remote_vm(mm, arg_end - 1, &c, 1, 0);
258 if (rv <= 0)
259 goto out_free_page;
261 rv = 0;
263 if (c == '\0') {
264 /* Command line (set of strings) occupies whole ARGV. */
265 if (len1 <= *pos)
266 goto out_free_page;
268 p = arg_start + *pos;
269 len = len1 - *pos;
270 while (count > 0 && len > 0) {
271 unsigned int _count;
272 int nr_read;
274 _count = min3(count, len, PAGE_SIZE);
275 nr_read = access_remote_vm(mm, p, page, _count, 0);
276 if (nr_read < 0)
277 rv = nr_read;
278 if (nr_read <= 0)
279 goto out_free_page;
281 if (copy_to_user(buf, page, nr_read)) {
282 rv = -EFAULT;
283 goto out_free_page;
286 p += nr_read;
287 len -= nr_read;
288 buf += nr_read;
289 count -= nr_read;
290 rv += nr_read;
292 } else {
294 * Command line (1 string) occupies ARGV and maybe
295 * extends into ENVP.
297 if (len1 + len2 <= *pos)
298 goto skip_argv_envp;
299 if (len1 <= *pos)
300 goto skip_argv;
302 p = arg_start + *pos;
303 len = len1 - *pos;
304 while (count > 0 && len > 0) {
305 unsigned int _count, l;
306 int nr_read;
307 bool final;
309 _count = min3(count, len, PAGE_SIZE);
310 nr_read = access_remote_vm(mm, p, page, _count, 0);
311 if (nr_read < 0)
312 rv = nr_read;
313 if (nr_read <= 0)
314 goto out_free_page;
317 * Command line can be shorter than whole ARGV
318 * even if last "marker" byte says it is not.
320 final = false;
321 l = strnlen(page, nr_read);
322 if (l < nr_read) {
323 nr_read = l;
324 final = true;
327 if (copy_to_user(buf, page, nr_read)) {
328 rv = -EFAULT;
329 goto out_free_page;
332 p += nr_read;
333 len -= nr_read;
334 buf += nr_read;
335 count -= nr_read;
336 rv += nr_read;
338 if (final)
339 goto out_free_page;
341 skip_argv:
343 * Command line (1 string) occupies ARGV and
344 * extends into ENVP.
346 if (len1 <= *pos) {
347 p = env_start + *pos - len1;
348 len = len1 + len2 - *pos;
349 } else {
350 p = env_start;
351 len = len2;
353 while (count > 0 && len > 0) {
354 unsigned int _count, l;
355 int nr_read;
356 bool final;
358 _count = min3(count, len, PAGE_SIZE);
359 nr_read = access_remote_vm(mm, p, page, _count, 0);
360 if (nr_read < 0)
361 rv = nr_read;
362 if (nr_read <= 0)
363 goto out_free_page;
365 /* Find EOS. */
366 final = false;
367 l = strnlen(page, nr_read);
368 if (l < nr_read) {
369 nr_read = l;
370 final = true;
373 if (copy_to_user(buf, page, nr_read)) {
374 rv = -EFAULT;
375 goto out_free_page;
378 p += nr_read;
379 len -= nr_read;
380 buf += nr_read;
381 count -= nr_read;
382 rv += nr_read;
384 if (final)
385 goto out_free_page;
387 skip_argv_envp:
391 out_free_page:
392 free_page((unsigned long)page);
393 out_mmput:
394 mmput(mm);
395 if (rv > 0)
396 *pos += rv;
397 return rv;
400 static const struct file_operations proc_pid_cmdline_ops = {
401 .read = proc_pid_cmdline_read,
402 .llseek = generic_file_llseek,
405 static int proc_pid_auxv(struct seq_file *m, struct pid_namespace *ns,
406 struct pid *pid, struct task_struct *task)
408 struct mm_struct *mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
409 if (mm && !IS_ERR(mm)) {
410 unsigned int nwords = 0;
411 do {
412 nwords += 2;
413 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
414 seq_write(m, mm->saved_auxv, nwords * sizeof(mm->saved_auxv[0]));
415 mmput(mm);
416 return 0;
417 } else
418 return PTR_ERR(mm);
422 #ifdef CONFIG_KALLSYMS
424 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
425 * Returns the resolved symbol. If that fails, simply return the address.
427 static int proc_pid_wchan(struct seq_file *m, struct pid_namespace *ns,
428 struct pid *pid, struct task_struct *task)
430 unsigned long wchan;
431 char symname[KSYM_NAME_LEN];
433 wchan = get_wchan(task);
435 if (wchan && ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)
436 && !lookup_symbol_name(wchan, symname))
437 seq_printf(m, "%s", symname);
438 else
439 seq_putc(m, '0');
441 return 0;
443 #endif /* CONFIG_KALLSYMS */
445 static int lock_trace(struct task_struct *task)
447 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
448 if (err)
449 return err;
450 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH_FSCREDS)) {
451 mutex_unlock(&task->signal->cred_guard_mutex);
452 return -EPERM;
454 return 0;
457 static void unlock_trace(struct task_struct *task)
459 mutex_unlock(&task->signal->cred_guard_mutex);
462 #ifdef CONFIG_STACKTRACE
464 #define MAX_STACK_TRACE_DEPTH 64
466 static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
467 struct pid *pid, struct task_struct *task)
469 struct stack_trace trace;
470 unsigned long *entries;
471 int err;
472 int i;
475 * The ability to racily run the kernel stack unwinder on a running task
476 * and then observe the unwinder output is scary; while it is useful for
477 * debugging kernel issues, it can also allow an attacker to leak kernel
478 * stack contents.
479 * Doing this in a manner that is at least safe from races would require
480 * some work to ensure that the remote task can not be scheduled; and
481 * even then, this would still expose the unwinder as local attack
482 * surface.
483 * Therefore, this interface is restricted to root.
485 if (!file_ns_capable(m->file, &init_user_ns, CAP_SYS_ADMIN))
486 return -EACCES;
488 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
489 if (!entries)
490 return -ENOMEM;
492 trace.nr_entries = 0;
493 trace.max_entries = MAX_STACK_TRACE_DEPTH;
494 trace.entries = entries;
495 trace.skip = 0;
497 err = lock_trace(task);
498 if (!err) {
499 save_stack_trace_tsk(task, &trace);
501 for (i = 0; i < trace.nr_entries; i++) {
502 seq_printf(m, "[<%pK>] %pS\n",
503 (void *)entries[i], (void *)entries[i]);
505 unlock_trace(task);
507 kfree(entries);
509 return err;
511 #endif
513 #ifdef CONFIG_SCHED_INFO
515 * Provides /proc/PID/schedstat
517 static int proc_pid_schedstat(struct seq_file *m, struct pid_namespace *ns,
518 struct pid *pid, struct task_struct *task)
520 if (unlikely(!sched_info_on()))
521 seq_printf(m, "0 0 0\n");
522 else
523 seq_printf(m, "%llu %llu %lu\n",
524 (unsigned long long)task->se.sum_exec_runtime,
525 (unsigned long long)task->sched_info.run_delay,
526 task->sched_info.pcount);
528 return 0;
530 #endif
532 #ifdef CONFIG_LATENCYTOP
533 static int lstats_show_proc(struct seq_file *m, void *v)
535 int i;
536 struct inode *inode = m->private;
537 struct task_struct *task = get_proc_task(inode);
539 if (!task)
540 return -ESRCH;
541 seq_puts(m, "Latency Top version : v0.1\n");
542 for (i = 0; i < 32; i++) {
543 struct latency_record *lr = &task->latency_record[i];
544 if (lr->backtrace[0]) {
545 int q;
546 seq_printf(m, "%i %li %li",
547 lr->count, lr->time, lr->max);
548 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
549 unsigned long bt = lr->backtrace[q];
550 if (!bt)
551 break;
552 if (bt == ULONG_MAX)
553 break;
554 seq_printf(m, " %ps", (void *)bt);
556 seq_putc(m, '\n');
560 put_task_struct(task);
561 return 0;
564 static int lstats_open(struct inode *inode, struct file *file)
566 return single_open(file, lstats_show_proc, inode);
569 static ssize_t lstats_write(struct file *file, const char __user *buf,
570 size_t count, loff_t *offs)
572 struct task_struct *task = get_proc_task(file_inode(file));
574 if (!task)
575 return -ESRCH;
576 clear_all_latency_tracing(task);
577 put_task_struct(task);
579 return count;
582 static const struct file_operations proc_lstats_operations = {
583 .open = lstats_open,
584 .read = seq_read,
585 .write = lstats_write,
586 .llseek = seq_lseek,
587 .release = single_release,
590 #endif
592 static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns,
593 struct pid *pid, struct task_struct *task)
595 unsigned long totalpages = totalram_pages + total_swap_pages;
596 unsigned long points = 0;
598 read_lock(&tasklist_lock);
599 if (pid_alive(task))
600 points = oom_badness(task, NULL, NULL, totalpages) *
601 1000 / totalpages;
602 read_unlock(&tasklist_lock);
603 seq_printf(m, "%lu\n", points);
605 return 0;
608 struct limit_names {
609 const char *name;
610 const char *unit;
613 static const struct limit_names lnames[RLIM_NLIMITS] = {
614 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
615 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
616 [RLIMIT_DATA] = {"Max data size", "bytes"},
617 [RLIMIT_STACK] = {"Max stack size", "bytes"},
618 [RLIMIT_CORE] = {"Max core file size", "bytes"},
619 [RLIMIT_RSS] = {"Max resident set", "bytes"},
620 [RLIMIT_NPROC] = {"Max processes", "processes"},
621 [RLIMIT_NOFILE] = {"Max open files", "files"},
622 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
623 [RLIMIT_AS] = {"Max address space", "bytes"},
624 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
625 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
626 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
627 [RLIMIT_NICE] = {"Max nice priority", NULL},
628 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
629 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
632 /* Display limits for a process */
633 static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
634 struct pid *pid, struct task_struct *task)
636 unsigned int i;
637 unsigned long flags;
639 struct rlimit rlim[RLIM_NLIMITS];
641 if (!lock_task_sighand(task, &flags))
642 return 0;
643 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
644 unlock_task_sighand(task, &flags);
647 * print the file header
649 seq_printf(m, "%-25s %-20s %-20s %-10s\n",
650 "Limit", "Soft Limit", "Hard Limit", "Units");
652 for (i = 0; i < RLIM_NLIMITS; i++) {
653 if (rlim[i].rlim_cur == RLIM_INFINITY)
654 seq_printf(m, "%-25s %-20s ",
655 lnames[i].name, "unlimited");
656 else
657 seq_printf(m, "%-25s %-20lu ",
658 lnames[i].name, rlim[i].rlim_cur);
660 if (rlim[i].rlim_max == RLIM_INFINITY)
661 seq_printf(m, "%-20s ", "unlimited");
662 else
663 seq_printf(m, "%-20lu ", rlim[i].rlim_max);
665 if (lnames[i].unit)
666 seq_printf(m, "%-10s\n", lnames[i].unit);
667 else
668 seq_putc(m, '\n');
671 return 0;
674 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
675 static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
676 struct pid *pid, struct task_struct *task)
678 long nr;
679 unsigned long args[6], sp, pc;
680 int res;
682 res = lock_trace(task);
683 if (res)
684 return res;
686 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
687 seq_puts(m, "running\n");
688 else if (nr < 0)
689 seq_printf(m, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
690 else
691 seq_printf(m,
692 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
694 args[0], args[1], args[2], args[3], args[4], args[5],
695 sp, pc);
696 unlock_trace(task);
698 return 0;
700 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
702 /************************************************************************/
703 /* Here the fs part begins */
704 /************************************************************************/
706 /* permission checks */
707 static int proc_fd_access_allowed(struct inode *inode)
709 struct task_struct *task;
710 int allowed = 0;
711 /* Allow access to a task's file descriptors if it is us or we
712 * may use ptrace attach to the process and find out that
713 * information.
715 task = get_proc_task(inode);
716 if (task) {
717 allowed = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
718 put_task_struct(task);
720 return allowed;
723 int proc_setattr(struct dentry *dentry, struct iattr *attr)
725 int error;
726 struct inode *inode = d_inode(dentry);
728 if (attr->ia_valid & ATTR_MODE)
729 return -EPERM;
731 error = inode_change_ok(inode, attr);
732 if (error)
733 return error;
735 setattr_copy(inode, attr);
736 mark_inode_dirty(inode);
737 return 0;
741 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
742 * or euid/egid (for hide_pid_min=2)?
744 static bool has_pid_permissions(struct pid_namespace *pid,
745 struct task_struct *task,
746 int hide_pid_min)
748 if (pid->hide_pid < hide_pid_min)
749 return true;
750 if (in_group_p(pid->pid_gid))
751 return true;
752 return ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
756 static int proc_pid_permission(struct inode *inode, int mask)
758 struct pid_namespace *pid = inode->i_sb->s_fs_info;
759 struct task_struct *task;
760 bool has_perms;
762 task = get_proc_task(inode);
763 if (!task)
764 return -ESRCH;
765 has_perms = has_pid_permissions(pid, task, 1);
766 put_task_struct(task);
768 if (!has_perms) {
769 if (pid->hide_pid == 2) {
771 * Let's make getdents(), stat(), and open()
772 * consistent with each other. If a process
773 * may not stat() a file, it shouldn't be seen
774 * in procfs at all.
776 return -ENOENT;
779 return -EPERM;
781 return generic_permission(inode, mask);
786 static const struct inode_operations proc_def_inode_operations = {
787 .setattr = proc_setattr,
790 static int proc_single_show(struct seq_file *m, void *v)
792 struct inode *inode = m->private;
793 struct pid_namespace *ns;
794 struct pid *pid;
795 struct task_struct *task;
796 int ret;
798 ns = inode->i_sb->s_fs_info;
799 pid = proc_pid(inode);
800 task = get_pid_task(pid, PIDTYPE_PID);
801 if (!task)
802 return -ESRCH;
804 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
806 put_task_struct(task);
807 return ret;
810 static int proc_single_open(struct inode *inode, struct file *filp)
812 return single_open(filp, proc_single_show, inode);
815 static const struct file_operations proc_single_file_operations = {
816 .open = proc_single_open,
817 .read = seq_read,
818 .llseek = seq_lseek,
819 .release = single_release,
823 struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode)
825 struct task_struct *task = get_proc_task(inode);
826 struct mm_struct *mm = ERR_PTR(-ESRCH);
828 if (task) {
829 mm = mm_access(task, mode | PTRACE_MODE_FSCREDS);
830 put_task_struct(task);
832 if (!IS_ERR_OR_NULL(mm)) {
833 /* ensure this mm_struct can't be freed */
834 atomic_inc(&mm->mm_count);
835 /* but do not pin its memory */
836 mmput(mm);
840 return mm;
843 static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
845 struct mm_struct *mm = proc_mem_open(inode, mode);
847 if (IS_ERR(mm))
848 return PTR_ERR(mm);
850 file->private_data = mm;
851 return 0;
854 static int mem_open(struct inode *inode, struct file *file)
856 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
858 /* OK to pass negative loff_t, we can catch out-of-range */
859 file->f_mode |= FMODE_UNSIGNED_OFFSET;
861 return ret;
864 static ssize_t mem_rw(struct file *file, char __user *buf,
865 size_t count, loff_t *ppos, int write)
867 struct mm_struct *mm = file->private_data;
868 unsigned long addr = *ppos;
869 ssize_t copied;
870 char *page;
872 if (!mm)
873 return 0;
875 page = (char *)__get_free_page(GFP_TEMPORARY);
876 if (!page)
877 return -ENOMEM;
879 copied = 0;
880 if (!atomic_inc_not_zero(&mm->mm_users))
881 goto free;
883 while (count > 0) {
884 int this_len = min_t(int, count, PAGE_SIZE);
886 if (write && copy_from_user(page, buf, this_len)) {
887 copied = -EFAULT;
888 break;
891 this_len = access_remote_vm(mm, addr, page, this_len, write);
892 if (!this_len) {
893 if (!copied)
894 copied = -EIO;
895 break;
898 if (!write && copy_to_user(buf, page, this_len)) {
899 copied = -EFAULT;
900 break;
903 buf += this_len;
904 addr += this_len;
905 copied += this_len;
906 count -= this_len;
908 *ppos = addr;
910 mmput(mm);
911 free:
912 free_page((unsigned long) page);
913 return copied;
916 static ssize_t mem_read(struct file *file, char __user *buf,
917 size_t count, loff_t *ppos)
919 return mem_rw(file, buf, count, ppos, 0);
922 static ssize_t mem_write(struct file *file, const char __user *buf,
923 size_t count, loff_t *ppos)
925 return mem_rw(file, (char __user*)buf, count, ppos, 1);
928 loff_t mem_lseek(struct file *file, loff_t offset, int orig)
930 switch (orig) {
931 case 0:
932 file->f_pos = offset;
933 break;
934 case 1:
935 file->f_pos += offset;
936 break;
937 default:
938 return -EINVAL;
940 force_successful_syscall_return();
941 return file->f_pos;
944 static int mem_release(struct inode *inode, struct file *file)
946 struct mm_struct *mm = file->private_data;
947 if (mm)
948 mmdrop(mm);
949 return 0;
952 static const struct file_operations proc_mem_operations = {
953 .llseek = mem_lseek,
954 .read = mem_read,
955 .write = mem_write,
956 .open = mem_open,
957 .release = mem_release,
960 static int environ_open(struct inode *inode, struct file *file)
962 return __mem_open(inode, file, PTRACE_MODE_READ);
965 static ssize_t environ_read(struct file *file, char __user *buf,
966 size_t count, loff_t *ppos)
968 char *page;
969 unsigned long src = *ppos;
970 int ret = 0;
971 struct mm_struct *mm = file->private_data;
972 unsigned long env_start, env_end;
974 /* Ensure the process spawned far enough to have an environment. */
975 if (!mm || !mm->env_end)
976 return 0;
978 page = (char *)__get_free_page(GFP_TEMPORARY);
979 if (!page)
980 return -ENOMEM;
982 ret = 0;
983 if (!atomic_inc_not_zero(&mm->mm_users))
984 goto free;
986 down_read(&mm->mmap_sem);
987 env_start = mm->env_start;
988 env_end = mm->env_end;
989 up_read(&mm->mmap_sem);
991 while (count > 0) {
992 size_t this_len, max_len;
993 int retval;
995 if (src >= (env_end - env_start))
996 break;
998 this_len = env_end - (env_start + src);
1000 max_len = min_t(size_t, PAGE_SIZE, count);
1001 this_len = min(max_len, this_len);
1003 retval = access_remote_vm(mm, (env_start + src),
1004 page, this_len, 0);
1006 if (retval <= 0) {
1007 ret = retval;
1008 break;
1011 if (copy_to_user(buf, page, retval)) {
1012 ret = -EFAULT;
1013 break;
1016 ret += retval;
1017 src += retval;
1018 buf += retval;
1019 count -= retval;
1021 *ppos = src;
1022 mmput(mm);
1024 free:
1025 free_page((unsigned long) page);
1026 return ret;
1029 static const struct file_operations proc_environ_operations = {
1030 .open = environ_open,
1031 .read = environ_read,
1032 .llseek = generic_file_llseek,
1033 .release = mem_release,
1036 static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
1037 loff_t *ppos)
1039 struct task_struct *task = get_proc_task(file_inode(file));
1040 char buffer[PROC_NUMBUF];
1041 int oom_adj = OOM_ADJUST_MIN;
1042 size_t len;
1043 unsigned long flags;
1045 if (!task)
1046 return -ESRCH;
1047 if (lock_task_sighand(task, &flags)) {
1048 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
1049 oom_adj = OOM_ADJUST_MAX;
1050 else
1051 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
1052 OOM_SCORE_ADJ_MAX;
1053 unlock_task_sighand(task, &flags);
1055 put_task_struct(task);
1056 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
1057 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1061 * /proc/pid/oom_adj exists solely for backwards compatibility with previous
1062 * kernels. The effective policy is defined by oom_score_adj, which has a
1063 * different scale: oom_adj grew exponentially and oom_score_adj grows linearly.
1064 * Values written to oom_adj are simply mapped linearly to oom_score_adj.
1065 * Processes that become oom disabled via oom_adj will still be oom disabled
1066 * with this implementation.
1068 * oom_adj cannot be removed since existing userspace binaries use it.
1070 static ssize_t oom_adj_write(struct file *file, const char __user *buf,
1071 size_t count, loff_t *ppos)
1073 struct task_struct *task;
1074 char buffer[PROC_NUMBUF];
1075 int oom_adj;
1076 unsigned long flags;
1077 int err;
1079 memset(buffer, 0, sizeof(buffer));
1080 if (count > sizeof(buffer) - 1)
1081 count = sizeof(buffer) - 1;
1082 if (copy_from_user(buffer, buf, count)) {
1083 err = -EFAULT;
1084 goto out;
1087 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
1088 if (err)
1089 goto out;
1090 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
1091 oom_adj != OOM_DISABLE) {
1092 err = -EINVAL;
1093 goto out;
1096 task = get_proc_task(file_inode(file));
1097 if (!task) {
1098 err = -ESRCH;
1099 goto out;
1102 task_lock(task);
1103 if (!task->mm) {
1104 err = -EINVAL;
1105 goto err_task_lock;
1108 if (!lock_task_sighand(task, &flags)) {
1109 err = -ESRCH;
1110 goto err_task_lock;
1114 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1115 * value is always attainable.
1117 if (oom_adj == OOM_ADJUST_MAX)
1118 oom_adj = OOM_SCORE_ADJ_MAX;
1119 else
1120 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
1122 if (oom_adj < task->signal->oom_score_adj &&
1123 !capable(CAP_SYS_RESOURCE)) {
1124 err = -EACCES;
1125 goto err_sighand;
1129 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
1130 * /proc/pid/oom_score_adj instead.
1132 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
1133 current->comm, task_pid_nr(current), task_pid_nr(task),
1134 task_pid_nr(task));
1136 task->signal->oom_score_adj = oom_adj;
1137 trace_oom_score_adj_update(task);
1138 err_sighand:
1139 unlock_task_sighand(task, &flags);
1140 err_task_lock:
1141 task_unlock(task);
1142 put_task_struct(task);
1143 out:
1144 return err < 0 ? err : count;
1147 static const struct file_operations proc_oom_adj_operations = {
1148 .read = oom_adj_read,
1149 .write = oom_adj_write,
1150 .llseek = generic_file_llseek,
1153 static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1154 size_t count, loff_t *ppos)
1156 struct task_struct *task = get_proc_task(file_inode(file));
1157 char buffer[PROC_NUMBUF];
1158 short oom_score_adj = OOM_SCORE_ADJ_MIN;
1159 unsigned long flags;
1160 size_t len;
1162 if (!task)
1163 return -ESRCH;
1164 if (lock_task_sighand(task, &flags)) {
1165 oom_score_adj = task->signal->oom_score_adj;
1166 unlock_task_sighand(task, &flags);
1168 put_task_struct(task);
1169 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
1170 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1173 static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1174 size_t count, loff_t *ppos)
1176 struct task_struct *task;
1177 char buffer[PROC_NUMBUF];
1178 unsigned long flags;
1179 int oom_score_adj;
1180 int err;
1182 memset(buffer, 0, sizeof(buffer));
1183 if (count > sizeof(buffer) - 1)
1184 count = sizeof(buffer) - 1;
1185 if (copy_from_user(buffer, buf, count)) {
1186 err = -EFAULT;
1187 goto out;
1190 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
1191 if (err)
1192 goto out;
1193 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
1194 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1195 err = -EINVAL;
1196 goto out;
1199 task = get_proc_task(file_inode(file));
1200 if (!task) {
1201 err = -ESRCH;
1202 goto out;
1205 task_lock(task);
1206 if (!task->mm) {
1207 err = -EINVAL;
1208 goto err_task_lock;
1211 if (!lock_task_sighand(task, &flags)) {
1212 err = -ESRCH;
1213 goto err_task_lock;
1216 if ((short)oom_score_adj < task->signal->oom_score_adj_min &&
1217 !capable(CAP_SYS_RESOURCE)) {
1218 err = -EACCES;
1219 goto err_sighand;
1222 task->signal->oom_score_adj = (short)oom_score_adj;
1223 if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
1224 task->signal->oom_score_adj_min = (short)oom_score_adj;
1225 trace_oom_score_adj_update(task);
1227 err_sighand:
1228 unlock_task_sighand(task, &flags);
1229 err_task_lock:
1230 task_unlock(task);
1231 put_task_struct(task);
1232 out:
1233 return err < 0 ? err : count;
1236 static const struct file_operations proc_oom_score_adj_operations = {
1237 .read = oom_score_adj_read,
1238 .write = oom_score_adj_write,
1239 .llseek = default_llseek,
1242 #ifdef CONFIG_AUDITSYSCALL
1243 #define TMPBUFLEN 21
1244 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1245 size_t count, loff_t *ppos)
1247 struct inode * inode = file_inode(file);
1248 struct task_struct *task = get_proc_task(inode);
1249 ssize_t length;
1250 char tmpbuf[TMPBUFLEN];
1252 if (!task)
1253 return -ESRCH;
1254 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1255 from_kuid(file->f_cred->user_ns,
1256 audit_get_loginuid(task)));
1257 put_task_struct(task);
1258 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1261 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1262 size_t count, loff_t *ppos)
1264 struct inode * inode = file_inode(file);
1265 uid_t loginuid;
1266 kuid_t kloginuid;
1267 int rv;
1269 rcu_read_lock();
1270 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1271 rcu_read_unlock();
1272 return -EPERM;
1274 rcu_read_unlock();
1276 if (*ppos != 0) {
1277 /* No partial writes. */
1278 return -EINVAL;
1281 rv = kstrtou32_from_user(buf, count, 10, &loginuid);
1282 if (rv < 0)
1283 return rv;
1285 /* is userspace tring to explicitly UNSET the loginuid? */
1286 if (loginuid == AUDIT_UID_UNSET) {
1287 kloginuid = INVALID_UID;
1288 } else {
1289 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
1290 if (!uid_valid(kloginuid))
1291 return -EINVAL;
1294 rv = audit_set_loginuid(kloginuid);
1295 if (rv < 0)
1296 return rv;
1297 return count;
1300 static const struct file_operations proc_loginuid_operations = {
1301 .read = proc_loginuid_read,
1302 .write = proc_loginuid_write,
1303 .llseek = generic_file_llseek,
1306 static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1307 size_t count, loff_t *ppos)
1309 struct inode * inode = file_inode(file);
1310 struct task_struct *task = get_proc_task(inode);
1311 ssize_t length;
1312 char tmpbuf[TMPBUFLEN];
1314 if (!task)
1315 return -ESRCH;
1316 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1317 audit_get_sessionid(task));
1318 put_task_struct(task);
1319 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1322 static const struct file_operations proc_sessionid_operations = {
1323 .read = proc_sessionid_read,
1324 .llseek = generic_file_llseek,
1326 #endif
1328 #ifdef CONFIG_FAULT_INJECTION
1329 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1330 size_t count, loff_t *ppos)
1332 struct task_struct *task = get_proc_task(file_inode(file));
1333 char buffer[PROC_NUMBUF];
1334 size_t len;
1335 int make_it_fail;
1337 if (!task)
1338 return -ESRCH;
1339 make_it_fail = task->make_it_fail;
1340 put_task_struct(task);
1342 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1344 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1347 static ssize_t proc_fault_inject_write(struct file * file,
1348 const char __user * buf, size_t count, loff_t *ppos)
1350 struct task_struct *task;
1351 char buffer[PROC_NUMBUF];
1352 int make_it_fail;
1353 int rv;
1355 if (!capable(CAP_SYS_RESOURCE))
1356 return -EPERM;
1357 memset(buffer, 0, sizeof(buffer));
1358 if (count > sizeof(buffer) - 1)
1359 count = sizeof(buffer) - 1;
1360 if (copy_from_user(buffer, buf, count))
1361 return -EFAULT;
1362 rv = kstrtoint(strstrip(buffer), 0, &make_it_fail);
1363 if (rv < 0)
1364 return rv;
1365 if (make_it_fail < 0 || make_it_fail > 1)
1366 return -EINVAL;
1368 task = get_proc_task(file_inode(file));
1369 if (!task)
1370 return -ESRCH;
1371 task->make_it_fail = make_it_fail;
1372 put_task_struct(task);
1374 return count;
1377 static const struct file_operations proc_fault_inject_operations = {
1378 .read = proc_fault_inject_read,
1379 .write = proc_fault_inject_write,
1380 .llseek = generic_file_llseek,
1382 #endif
1385 #ifdef CONFIG_SCHED_DEBUG
1387 * Print out various scheduling related per-task fields:
1389 static int sched_show(struct seq_file *m, void *v)
1391 struct inode *inode = m->private;
1392 struct task_struct *p;
1394 p = get_proc_task(inode);
1395 if (!p)
1396 return -ESRCH;
1397 proc_sched_show_task(p, m);
1399 put_task_struct(p);
1401 return 0;
1404 static ssize_t
1405 sched_write(struct file *file, const char __user *buf,
1406 size_t count, loff_t *offset)
1408 struct inode *inode = file_inode(file);
1409 struct task_struct *p;
1411 p = get_proc_task(inode);
1412 if (!p)
1413 return -ESRCH;
1414 proc_sched_set_task(p);
1416 put_task_struct(p);
1418 return count;
1421 static int sched_open(struct inode *inode, struct file *filp)
1423 return single_open(filp, sched_show, inode);
1426 static const struct file_operations proc_pid_sched_operations = {
1427 .open = sched_open,
1428 .read = seq_read,
1429 .write = sched_write,
1430 .llseek = seq_lseek,
1431 .release = single_release,
1434 #endif
1436 #ifdef CONFIG_SCHED_AUTOGROUP
1438 * Print out autogroup related information:
1440 static int sched_autogroup_show(struct seq_file *m, void *v)
1442 struct inode *inode = m->private;
1443 struct task_struct *p;
1445 p = get_proc_task(inode);
1446 if (!p)
1447 return -ESRCH;
1448 proc_sched_autogroup_show_task(p, m);
1450 put_task_struct(p);
1452 return 0;
1455 static ssize_t
1456 sched_autogroup_write(struct file *file, const char __user *buf,
1457 size_t count, loff_t *offset)
1459 struct inode *inode = file_inode(file);
1460 struct task_struct *p;
1461 char buffer[PROC_NUMBUF];
1462 int nice;
1463 int err;
1465 memset(buffer, 0, sizeof(buffer));
1466 if (count > sizeof(buffer) - 1)
1467 count = sizeof(buffer) - 1;
1468 if (copy_from_user(buffer, buf, count))
1469 return -EFAULT;
1471 err = kstrtoint(strstrip(buffer), 0, &nice);
1472 if (err < 0)
1473 return err;
1475 p = get_proc_task(inode);
1476 if (!p)
1477 return -ESRCH;
1479 err = proc_sched_autogroup_set_nice(p, nice);
1480 if (err)
1481 count = err;
1483 put_task_struct(p);
1485 return count;
1488 static int sched_autogroup_open(struct inode *inode, struct file *filp)
1490 int ret;
1492 ret = single_open(filp, sched_autogroup_show, NULL);
1493 if (!ret) {
1494 struct seq_file *m = filp->private_data;
1496 m->private = inode;
1498 return ret;
1501 static const struct file_operations proc_pid_sched_autogroup_operations = {
1502 .open = sched_autogroup_open,
1503 .read = seq_read,
1504 .write = sched_autogroup_write,
1505 .llseek = seq_lseek,
1506 .release = single_release,
1509 #endif /* CONFIG_SCHED_AUTOGROUP */
1511 static ssize_t comm_write(struct file *file, const char __user *buf,
1512 size_t count, loff_t *offset)
1514 struct inode *inode = file_inode(file);
1515 struct task_struct *p;
1516 char buffer[TASK_COMM_LEN];
1517 const size_t maxlen = sizeof(buffer) - 1;
1519 memset(buffer, 0, sizeof(buffer));
1520 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
1521 return -EFAULT;
1523 p = get_proc_task(inode);
1524 if (!p)
1525 return -ESRCH;
1527 if (same_thread_group(current, p))
1528 set_task_comm(p, buffer);
1529 else
1530 count = -EINVAL;
1532 put_task_struct(p);
1534 return count;
1537 static int comm_show(struct seq_file *m, void *v)
1539 struct inode *inode = m->private;
1540 struct task_struct *p;
1542 p = get_proc_task(inode);
1543 if (!p)
1544 return -ESRCH;
1546 task_lock(p);
1547 seq_printf(m, "%s\n", p->comm);
1548 task_unlock(p);
1550 put_task_struct(p);
1552 return 0;
1555 static int comm_open(struct inode *inode, struct file *filp)
1557 return single_open(filp, comm_show, inode);
1560 static const struct file_operations proc_pid_set_comm_operations = {
1561 .open = comm_open,
1562 .read = seq_read,
1563 .write = comm_write,
1564 .llseek = seq_lseek,
1565 .release = single_release,
1568 static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
1570 struct task_struct *task;
1571 struct file *exe_file;
1573 task = get_proc_task(d_inode(dentry));
1574 if (!task)
1575 return -ENOENT;
1576 exe_file = get_task_exe_file(task);
1577 put_task_struct(task);
1578 if (exe_file) {
1579 *exe_path = exe_file->f_path;
1580 path_get(&exe_file->f_path);
1581 fput(exe_file);
1582 return 0;
1583 } else
1584 return -ENOENT;
1587 static const char *proc_pid_follow_link(struct dentry *dentry, void **cookie)
1589 struct inode *inode = d_inode(dentry);
1590 struct path path;
1591 int error = -EACCES;
1593 /* Are we allowed to snoop on the tasks file descriptors? */
1594 if (!proc_fd_access_allowed(inode))
1595 goto out;
1597 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1598 if (error)
1599 goto out;
1601 nd_jump_link(&path);
1602 return NULL;
1603 out:
1604 return ERR_PTR(error);
1607 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1609 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
1610 char *pathname;
1611 int len;
1613 if (!tmp)
1614 return -ENOMEM;
1616 pathname = d_path(path, tmp, PAGE_SIZE);
1617 len = PTR_ERR(pathname);
1618 if (IS_ERR(pathname))
1619 goto out;
1620 len = tmp + PAGE_SIZE - 1 - pathname;
1622 if (len > buflen)
1623 len = buflen;
1624 if (copy_to_user(buffer, pathname, len))
1625 len = -EFAULT;
1626 out:
1627 free_page((unsigned long)tmp);
1628 return len;
1631 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1633 int error = -EACCES;
1634 struct inode *inode = d_inode(dentry);
1635 struct path path;
1637 /* Are we allowed to snoop on the tasks file descriptors? */
1638 if (!proc_fd_access_allowed(inode))
1639 goto out;
1641 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1642 if (error)
1643 goto out;
1645 error = do_proc_readlink(&path, buffer, buflen);
1646 path_put(&path);
1647 out:
1648 return error;
1651 const struct inode_operations proc_pid_link_inode_operations = {
1652 .readlink = proc_pid_readlink,
1653 .follow_link = proc_pid_follow_link,
1654 .setattr = proc_setattr,
1658 /* building an inode */
1660 struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1662 struct inode * inode;
1663 struct proc_inode *ei;
1664 const struct cred *cred;
1666 /* We need a new inode */
1668 inode = new_inode(sb);
1669 if (!inode)
1670 goto out;
1672 /* Common stuff */
1673 ei = PROC_I(inode);
1674 inode->i_ino = get_next_ino();
1675 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1676 inode->i_op = &proc_def_inode_operations;
1679 * grab the reference to task.
1681 ei->pid = get_task_pid(task, PIDTYPE_PID);
1682 if (!ei->pid)
1683 goto out_unlock;
1685 if (task_dumpable(task)) {
1686 rcu_read_lock();
1687 cred = __task_cred(task);
1688 inode->i_uid = cred->euid;
1689 inode->i_gid = cred->egid;
1690 rcu_read_unlock();
1692 security_task_to_inode(task, inode);
1694 out:
1695 return inode;
1697 out_unlock:
1698 iput(inode);
1699 return NULL;
1702 int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1704 struct inode *inode = d_inode(dentry);
1705 struct task_struct *task;
1706 const struct cred *cred;
1707 struct pid_namespace *pid = dentry->d_sb->s_fs_info;
1709 generic_fillattr(inode, stat);
1711 rcu_read_lock();
1712 stat->uid = GLOBAL_ROOT_UID;
1713 stat->gid = GLOBAL_ROOT_GID;
1714 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1715 if (task) {
1716 if (!has_pid_permissions(pid, task, 2)) {
1717 rcu_read_unlock();
1719 * This doesn't prevent learning whether PID exists,
1720 * it only makes getattr() consistent with readdir().
1722 return -ENOENT;
1724 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1725 task_dumpable(task)) {
1726 cred = __task_cred(task);
1727 stat->uid = cred->euid;
1728 stat->gid = cred->egid;
1731 rcu_read_unlock();
1732 return 0;
1735 /* dentry stuff */
1738 * Exceptional case: normally we are not allowed to unhash a busy
1739 * directory. In this case, however, we can do it - no aliasing problems
1740 * due to the way we treat inodes.
1742 * Rewrite the inode's ownerships here because the owning task may have
1743 * performed a setuid(), etc.
1745 * Before the /proc/pid/status file was created the only way to read
1746 * the effective uid of a /process was to stat /proc/pid. Reading
1747 * /proc/pid/status is slow enough that procps and other packages
1748 * kept stating /proc/pid. To keep the rules in /proc simple I have
1749 * made this apply to all per process world readable and executable
1750 * directories.
1752 int pid_revalidate(struct dentry *dentry, unsigned int flags)
1754 struct inode *inode;
1755 struct task_struct *task;
1756 const struct cred *cred;
1758 if (flags & LOOKUP_RCU)
1759 return -ECHILD;
1761 inode = d_inode(dentry);
1762 task = get_proc_task(inode);
1764 if (task) {
1765 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1766 task_dumpable(task)) {
1767 rcu_read_lock();
1768 cred = __task_cred(task);
1769 inode->i_uid = cred->euid;
1770 inode->i_gid = cred->egid;
1771 rcu_read_unlock();
1772 } else {
1773 inode->i_uid = GLOBAL_ROOT_UID;
1774 inode->i_gid = GLOBAL_ROOT_GID;
1776 inode->i_mode &= ~(S_ISUID | S_ISGID);
1777 security_task_to_inode(task, inode);
1778 put_task_struct(task);
1779 return 1;
1781 return 0;
1784 static inline bool proc_inode_is_dead(struct inode *inode)
1786 return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1789 int pid_delete_dentry(const struct dentry *dentry)
1791 /* Is the task we represent dead?
1792 * If so, then don't put the dentry on the lru list,
1793 * kill it immediately.
1795 return proc_inode_is_dead(d_inode(dentry));
1798 const struct dentry_operations pid_dentry_operations =
1800 .d_revalidate = pid_revalidate,
1801 .d_delete = pid_delete_dentry,
1804 /* Lookups */
1807 * Fill a directory entry.
1809 * If possible create the dcache entry and derive our inode number and
1810 * file type from dcache entry.
1812 * Since all of the proc inode numbers are dynamically generated, the inode
1813 * numbers do not exist until the inode is cache. This means creating the
1814 * the dcache entry in readdir is necessary to keep the inode numbers
1815 * reported by readdir in sync with the inode numbers reported
1816 * by stat.
1818 bool proc_fill_cache(struct file *file, struct dir_context *ctx,
1819 const char *name, int len,
1820 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1822 struct dentry *child, *dir = file->f_path.dentry;
1823 struct qstr qname = QSTR_INIT(name, len);
1824 struct inode *inode;
1825 unsigned type;
1826 ino_t ino;
1828 child = d_hash_and_lookup(dir, &qname);
1829 if (!child) {
1830 child = d_alloc(dir, &qname);
1831 if (!child)
1832 goto end_instantiate;
1833 if (instantiate(d_inode(dir), child, task, ptr) < 0) {
1834 dput(child);
1835 goto end_instantiate;
1838 inode = d_inode(child);
1839 ino = inode->i_ino;
1840 type = inode->i_mode >> 12;
1841 dput(child);
1842 return dir_emit(ctx, name, len, ino, type);
1844 end_instantiate:
1845 return dir_emit(ctx, name, len, 1, DT_UNKNOWN);
1849 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1850 * which represent vma start and end addresses.
1852 static int dname_to_vma_addr(struct dentry *dentry,
1853 unsigned long *start, unsigned long *end)
1855 const char *str = dentry->d_name.name;
1856 unsigned long long sval, eval;
1857 unsigned int len;
1859 len = _parse_integer(str, 16, &sval);
1860 if (len & KSTRTOX_OVERFLOW)
1861 return -EINVAL;
1862 if (sval != (unsigned long)sval)
1863 return -EINVAL;
1864 str += len;
1866 if (*str != '-')
1867 return -EINVAL;
1868 str++;
1870 len = _parse_integer(str, 16, &eval);
1871 if (len & KSTRTOX_OVERFLOW)
1872 return -EINVAL;
1873 if (eval != (unsigned long)eval)
1874 return -EINVAL;
1875 str += len;
1877 if (*str != '\0')
1878 return -EINVAL;
1880 *start = sval;
1881 *end = eval;
1883 return 0;
1886 static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
1888 unsigned long vm_start, vm_end;
1889 bool exact_vma_exists = false;
1890 struct mm_struct *mm = NULL;
1891 struct task_struct *task;
1892 const struct cred *cred;
1893 struct inode *inode;
1894 int status = 0;
1896 if (flags & LOOKUP_RCU)
1897 return -ECHILD;
1899 inode = d_inode(dentry);
1900 task = get_proc_task(inode);
1901 if (!task)
1902 goto out_notask;
1904 mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
1905 if (IS_ERR_OR_NULL(mm))
1906 goto out;
1908 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1909 down_read(&mm->mmap_sem);
1910 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1911 up_read(&mm->mmap_sem);
1914 mmput(mm);
1916 if (exact_vma_exists) {
1917 if (task_dumpable(task)) {
1918 rcu_read_lock();
1919 cred = __task_cred(task);
1920 inode->i_uid = cred->euid;
1921 inode->i_gid = cred->egid;
1922 rcu_read_unlock();
1923 } else {
1924 inode->i_uid = GLOBAL_ROOT_UID;
1925 inode->i_gid = GLOBAL_ROOT_GID;
1927 security_task_to_inode(task, inode);
1928 status = 1;
1931 out:
1932 put_task_struct(task);
1934 out_notask:
1935 return status;
1938 static const struct dentry_operations tid_map_files_dentry_operations = {
1939 .d_revalidate = map_files_d_revalidate,
1940 .d_delete = pid_delete_dentry,
1943 static int proc_map_files_get_link(struct dentry *dentry, struct path *path)
1945 unsigned long vm_start, vm_end;
1946 struct vm_area_struct *vma;
1947 struct task_struct *task;
1948 struct mm_struct *mm;
1949 int rc;
1951 rc = -ENOENT;
1952 task = get_proc_task(d_inode(dentry));
1953 if (!task)
1954 goto out;
1956 mm = get_task_mm(task);
1957 put_task_struct(task);
1958 if (!mm)
1959 goto out;
1961 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
1962 if (rc)
1963 goto out_mmput;
1965 rc = -ENOENT;
1966 down_read(&mm->mmap_sem);
1967 vma = find_exact_vma(mm, vm_start, vm_end);
1968 if (vma && vma->vm_file) {
1969 *path = vma->vm_file->f_path;
1970 path_get(path);
1971 rc = 0;
1973 up_read(&mm->mmap_sem);
1975 out_mmput:
1976 mmput(mm);
1977 out:
1978 return rc;
1981 struct map_files_info {
1982 fmode_t mode;
1983 unsigned long len;
1984 unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
1988 * Only allow CAP_SYS_ADMIN to follow the links, due to concerns about how the
1989 * symlinks may be used to bypass permissions on ancestor directories in the
1990 * path to the file in question.
1992 static const char *
1993 proc_map_files_follow_link(struct dentry *dentry, void **cookie)
1995 if (!capable(CAP_SYS_ADMIN))
1996 return ERR_PTR(-EPERM);
1998 return proc_pid_follow_link(dentry, NULL);
2002 * Identical to proc_pid_link_inode_operations except for follow_link()
2004 static const struct inode_operations proc_map_files_link_inode_operations = {
2005 .readlink = proc_pid_readlink,
2006 .follow_link = proc_map_files_follow_link,
2007 .setattr = proc_setattr,
2010 static int
2011 proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
2012 struct task_struct *task, const void *ptr)
2014 fmode_t mode = (fmode_t)(unsigned long)ptr;
2015 struct proc_inode *ei;
2016 struct inode *inode;
2018 inode = proc_pid_make_inode(dir->i_sb, task);
2019 if (!inode)
2020 return -ENOENT;
2022 ei = PROC_I(inode);
2023 ei->op.proc_get_link = proc_map_files_get_link;
2025 inode->i_op = &proc_map_files_link_inode_operations;
2026 inode->i_size = 64;
2027 inode->i_mode = S_IFLNK;
2029 if (mode & FMODE_READ)
2030 inode->i_mode |= S_IRUSR;
2031 if (mode & FMODE_WRITE)
2032 inode->i_mode |= S_IWUSR;
2034 d_set_d_op(dentry, &tid_map_files_dentry_operations);
2035 d_add(dentry, inode);
2037 return 0;
2040 static struct dentry *proc_map_files_lookup(struct inode *dir,
2041 struct dentry *dentry, unsigned int flags)
2043 unsigned long vm_start, vm_end;
2044 struct vm_area_struct *vma;
2045 struct task_struct *task;
2046 int result;
2047 struct mm_struct *mm;
2049 result = -ENOENT;
2050 task = get_proc_task(dir);
2051 if (!task)
2052 goto out;
2054 result = -EACCES;
2055 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
2056 goto out_put_task;
2058 result = -ENOENT;
2059 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
2060 goto out_put_task;
2062 mm = get_task_mm(task);
2063 if (!mm)
2064 goto out_put_task;
2066 down_read(&mm->mmap_sem);
2067 vma = find_exact_vma(mm, vm_start, vm_end);
2068 if (!vma)
2069 goto out_no_vma;
2071 if (vma->vm_file)
2072 result = proc_map_files_instantiate(dir, dentry, task,
2073 (void *)(unsigned long)vma->vm_file->f_mode);
2075 out_no_vma:
2076 up_read(&mm->mmap_sem);
2077 mmput(mm);
2078 out_put_task:
2079 put_task_struct(task);
2080 out:
2081 return ERR_PTR(result);
2084 static const struct inode_operations proc_map_files_inode_operations = {
2085 .lookup = proc_map_files_lookup,
2086 .permission = proc_fd_permission,
2087 .setattr = proc_setattr,
2090 static int
2091 proc_map_files_readdir(struct file *file, struct dir_context *ctx)
2093 struct vm_area_struct *vma;
2094 struct task_struct *task;
2095 struct mm_struct *mm;
2096 unsigned long nr_files, pos, i;
2097 struct flex_array *fa = NULL;
2098 struct map_files_info info;
2099 struct map_files_info *p;
2100 int ret;
2102 ret = -ENOENT;
2103 task = get_proc_task(file_inode(file));
2104 if (!task)
2105 goto out;
2107 ret = -EACCES;
2108 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
2109 goto out_put_task;
2111 ret = 0;
2112 if (!dir_emit_dots(file, ctx))
2113 goto out_put_task;
2115 mm = get_task_mm(task);
2116 if (!mm)
2117 goto out_put_task;
2118 down_read(&mm->mmap_sem);
2120 nr_files = 0;
2123 * We need two passes here:
2125 * 1) Collect vmas of mapped files with mmap_sem taken
2126 * 2) Release mmap_sem and instantiate entries
2128 * otherwise we get lockdep complained, since filldir()
2129 * routine might require mmap_sem taken in might_fault().
2132 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
2133 if (vma->vm_file && ++pos > ctx->pos)
2134 nr_files++;
2137 if (nr_files) {
2138 fa = flex_array_alloc(sizeof(info), nr_files,
2139 GFP_KERNEL);
2140 if (!fa || flex_array_prealloc(fa, 0, nr_files,
2141 GFP_KERNEL)) {
2142 ret = -ENOMEM;
2143 if (fa)
2144 flex_array_free(fa);
2145 up_read(&mm->mmap_sem);
2146 mmput(mm);
2147 goto out_put_task;
2149 for (i = 0, vma = mm->mmap, pos = 2; vma;
2150 vma = vma->vm_next) {
2151 if (!vma->vm_file)
2152 continue;
2153 if (++pos <= ctx->pos)
2154 continue;
2156 info.mode = vma->vm_file->f_mode;
2157 info.len = snprintf(info.name,
2158 sizeof(info.name), "%lx-%lx",
2159 vma->vm_start, vma->vm_end);
2160 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
2161 BUG();
2164 up_read(&mm->mmap_sem);
2166 for (i = 0; i < nr_files; i++) {
2167 p = flex_array_get(fa, i);
2168 if (!proc_fill_cache(file, ctx,
2169 p->name, p->len,
2170 proc_map_files_instantiate,
2171 task,
2172 (void *)(unsigned long)p->mode))
2173 break;
2174 ctx->pos++;
2176 if (fa)
2177 flex_array_free(fa);
2178 mmput(mm);
2180 out_put_task:
2181 put_task_struct(task);
2182 out:
2183 return ret;
2186 static const struct file_operations proc_map_files_operations = {
2187 .read = generic_read_dir,
2188 .iterate = proc_map_files_readdir,
2189 .llseek = default_llseek,
2192 struct timers_private {
2193 struct pid *pid;
2194 struct task_struct *task;
2195 struct sighand_struct *sighand;
2196 struct pid_namespace *ns;
2197 unsigned long flags;
2200 static void *timers_start(struct seq_file *m, loff_t *pos)
2202 struct timers_private *tp = m->private;
2204 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2205 if (!tp->task)
2206 return ERR_PTR(-ESRCH);
2208 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2209 if (!tp->sighand)
2210 return ERR_PTR(-ESRCH);
2212 return seq_list_start(&tp->task->signal->posix_timers, *pos);
2215 static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2217 struct timers_private *tp = m->private;
2218 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2221 static void timers_stop(struct seq_file *m, void *v)
2223 struct timers_private *tp = m->private;
2225 if (tp->sighand) {
2226 unlock_task_sighand(tp->task, &tp->flags);
2227 tp->sighand = NULL;
2230 if (tp->task) {
2231 put_task_struct(tp->task);
2232 tp->task = NULL;
2236 static int show_timer(struct seq_file *m, void *v)
2238 struct k_itimer *timer;
2239 struct timers_private *tp = m->private;
2240 int notify;
2241 static const char * const nstr[] = {
2242 [SIGEV_SIGNAL] = "signal",
2243 [SIGEV_NONE] = "none",
2244 [SIGEV_THREAD] = "thread",
2247 timer = list_entry((struct list_head *)v, struct k_itimer, list);
2248 notify = timer->it_sigev_notify;
2250 seq_printf(m, "ID: %d\n", timer->it_id);
2251 seq_printf(m, "signal: %d/%p\n",
2252 timer->sigq->info.si_signo,
2253 timer->sigq->info.si_value.sival_ptr);
2254 seq_printf(m, "notify: %s/%s.%d\n",
2255 nstr[notify & ~SIGEV_THREAD_ID],
2256 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2257 pid_nr_ns(timer->it_pid, tp->ns));
2258 seq_printf(m, "ClockID: %d\n", timer->it_clock);
2260 return 0;
2263 static const struct seq_operations proc_timers_seq_ops = {
2264 .start = timers_start,
2265 .next = timers_next,
2266 .stop = timers_stop,
2267 .show = show_timer,
2270 static int proc_timers_open(struct inode *inode, struct file *file)
2272 struct timers_private *tp;
2274 tp = __seq_open_private(file, &proc_timers_seq_ops,
2275 sizeof(struct timers_private));
2276 if (!tp)
2277 return -ENOMEM;
2279 tp->pid = proc_pid(inode);
2280 tp->ns = inode->i_sb->s_fs_info;
2281 return 0;
2284 static const struct file_operations proc_timers_operations = {
2285 .open = proc_timers_open,
2286 .read = seq_read,
2287 .llseek = seq_lseek,
2288 .release = seq_release_private,
2291 static int proc_pident_instantiate(struct inode *dir,
2292 struct dentry *dentry, struct task_struct *task, const void *ptr)
2294 const struct pid_entry *p = ptr;
2295 struct inode *inode;
2296 struct proc_inode *ei;
2298 inode = proc_pid_make_inode(dir->i_sb, task);
2299 if (!inode)
2300 goto out;
2302 ei = PROC_I(inode);
2303 inode->i_mode = p->mode;
2304 if (S_ISDIR(inode->i_mode))
2305 set_nlink(inode, 2); /* Use getattr to fix if necessary */
2306 if (p->iop)
2307 inode->i_op = p->iop;
2308 if (p->fop)
2309 inode->i_fop = p->fop;
2310 ei->op = p->op;
2311 d_set_d_op(dentry, &pid_dentry_operations);
2312 d_add(dentry, inode);
2313 /* Close the race of the process dying before we return the dentry */
2314 if (pid_revalidate(dentry, 0))
2315 return 0;
2316 out:
2317 return -ENOENT;
2320 static struct dentry *proc_pident_lookup(struct inode *dir,
2321 struct dentry *dentry,
2322 const struct pid_entry *ents,
2323 unsigned int nents)
2325 int error;
2326 struct task_struct *task = get_proc_task(dir);
2327 const struct pid_entry *p, *last;
2329 error = -ENOENT;
2331 if (!task)
2332 goto out_no_task;
2335 * Yes, it does not scale. And it should not. Don't add
2336 * new entries into /proc/<tgid>/ without very good reasons.
2338 last = &ents[nents - 1];
2339 for (p = ents; p <= last; p++) {
2340 if (p->len != dentry->d_name.len)
2341 continue;
2342 if (!memcmp(dentry->d_name.name, p->name, p->len))
2343 break;
2345 if (p > last)
2346 goto out;
2348 error = proc_pident_instantiate(dir, dentry, task, p);
2349 out:
2350 put_task_struct(task);
2351 out_no_task:
2352 return ERR_PTR(error);
2355 static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
2356 const struct pid_entry *ents, unsigned int nents)
2358 struct task_struct *task = get_proc_task(file_inode(file));
2359 const struct pid_entry *p;
2361 if (!task)
2362 return -ENOENT;
2364 if (!dir_emit_dots(file, ctx))
2365 goto out;
2367 if (ctx->pos >= nents + 2)
2368 goto out;
2370 for (p = ents + (ctx->pos - 2); p <= ents + nents - 1; p++) {
2371 if (!proc_fill_cache(file, ctx, p->name, p->len,
2372 proc_pident_instantiate, task, p))
2373 break;
2374 ctx->pos++;
2376 out:
2377 put_task_struct(task);
2378 return 0;
2381 #ifdef CONFIG_SECURITY
2382 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2383 size_t count, loff_t *ppos)
2385 struct inode * inode = file_inode(file);
2386 char *p = NULL;
2387 ssize_t length;
2388 struct task_struct *task = get_proc_task(inode);
2390 if (!task)
2391 return -ESRCH;
2393 length = security_getprocattr(task,
2394 (char*)file->f_path.dentry->d_name.name,
2395 &p);
2396 put_task_struct(task);
2397 if (length > 0)
2398 length = simple_read_from_buffer(buf, count, ppos, p, length);
2399 kfree(p);
2400 return length;
2403 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2404 size_t count, loff_t *ppos)
2406 struct inode * inode = file_inode(file);
2407 char *page;
2408 ssize_t length;
2409 struct task_struct *task = get_proc_task(inode);
2411 length = -ESRCH;
2412 if (!task)
2413 goto out_no_task;
2414 if (count > PAGE_SIZE)
2415 count = PAGE_SIZE;
2417 /* No partial writes. */
2418 length = -EINVAL;
2419 if (*ppos != 0)
2420 goto out;
2422 length = -ENOMEM;
2423 page = (char*)__get_free_page(GFP_TEMPORARY);
2424 if (!page)
2425 goto out;
2427 length = -EFAULT;
2428 if (copy_from_user(page, buf, count))
2429 goto out_free;
2431 /* Guard against adverse ptrace interaction */
2432 length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
2433 if (length < 0)
2434 goto out_free;
2436 length = security_setprocattr(task,
2437 (char*)file->f_path.dentry->d_name.name,
2438 (void*)page, count);
2439 mutex_unlock(&task->signal->cred_guard_mutex);
2440 out_free:
2441 free_page((unsigned long) page);
2442 out:
2443 put_task_struct(task);
2444 out_no_task:
2445 return length;
2448 static const struct file_operations proc_pid_attr_operations = {
2449 .read = proc_pid_attr_read,
2450 .write = proc_pid_attr_write,
2451 .llseek = generic_file_llseek,
2454 static const struct pid_entry attr_dir_stuff[] = {
2455 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2456 REG("prev", S_IRUGO, proc_pid_attr_operations),
2457 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2458 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2459 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2460 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2463 static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
2465 return proc_pident_readdir(file, ctx,
2466 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2469 static const struct file_operations proc_attr_dir_operations = {
2470 .read = generic_read_dir,
2471 .iterate = proc_attr_dir_readdir,
2472 .llseek = default_llseek,
2475 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2476 struct dentry *dentry, unsigned int flags)
2478 return proc_pident_lookup(dir, dentry,
2479 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2482 static const struct inode_operations proc_attr_dir_inode_operations = {
2483 .lookup = proc_attr_dir_lookup,
2484 .getattr = pid_getattr,
2485 .setattr = proc_setattr,
2488 #endif
2490 #ifdef CONFIG_ELF_CORE
2491 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2492 size_t count, loff_t *ppos)
2494 struct task_struct *task = get_proc_task(file_inode(file));
2495 struct mm_struct *mm;
2496 char buffer[PROC_NUMBUF];
2497 size_t len;
2498 int ret;
2500 if (!task)
2501 return -ESRCH;
2503 ret = 0;
2504 mm = get_task_mm(task);
2505 if (mm) {
2506 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2507 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2508 MMF_DUMP_FILTER_SHIFT));
2509 mmput(mm);
2510 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2513 put_task_struct(task);
2515 return ret;
2518 static ssize_t proc_coredump_filter_write(struct file *file,
2519 const char __user *buf,
2520 size_t count,
2521 loff_t *ppos)
2523 struct task_struct *task;
2524 struct mm_struct *mm;
2525 unsigned int val;
2526 int ret;
2527 int i;
2528 unsigned long mask;
2530 ret = kstrtouint_from_user(buf, count, 0, &val);
2531 if (ret < 0)
2532 return ret;
2534 ret = -ESRCH;
2535 task = get_proc_task(file_inode(file));
2536 if (!task)
2537 goto out_no_task;
2539 mm = get_task_mm(task);
2540 if (!mm)
2541 goto out_no_mm;
2542 ret = 0;
2544 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2545 if (val & mask)
2546 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2547 else
2548 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2551 mmput(mm);
2552 out_no_mm:
2553 put_task_struct(task);
2554 out_no_task:
2555 if (ret < 0)
2556 return ret;
2557 return count;
2560 static const struct file_operations proc_coredump_filter_operations = {
2561 .read = proc_coredump_filter_read,
2562 .write = proc_coredump_filter_write,
2563 .llseek = generic_file_llseek,
2565 #endif
2567 #ifdef CONFIG_TASK_IO_ACCOUNTING
2568 static int do_io_accounting(struct task_struct *task, struct seq_file *m, int whole)
2570 struct task_io_accounting acct = task->ioac;
2571 unsigned long flags;
2572 int result;
2574 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2575 if (result)
2576 return result;
2578 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
2579 result = -EACCES;
2580 goto out_unlock;
2583 if (whole && lock_task_sighand(task, &flags)) {
2584 struct task_struct *t = task;
2586 task_io_accounting_add(&acct, &task->signal->ioac);
2587 while_each_thread(task, t)
2588 task_io_accounting_add(&acct, &t->ioac);
2590 unlock_task_sighand(task, &flags);
2592 seq_printf(m,
2593 "rchar: %llu\n"
2594 "wchar: %llu\n"
2595 "syscr: %llu\n"
2596 "syscw: %llu\n"
2597 "read_bytes: %llu\n"
2598 "write_bytes: %llu\n"
2599 "cancelled_write_bytes: %llu\n",
2600 (unsigned long long)acct.rchar,
2601 (unsigned long long)acct.wchar,
2602 (unsigned long long)acct.syscr,
2603 (unsigned long long)acct.syscw,
2604 (unsigned long long)acct.read_bytes,
2605 (unsigned long long)acct.write_bytes,
2606 (unsigned long long)acct.cancelled_write_bytes);
2607 result = 0;
2609 out_unlock:
2610 mutex_unlock(&task->signal->cred_guard_mutex);
2611 return result;
2614 static int proc_tid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2615 struct pid *pid, struct task_struct *task)
2617 return do_io_accounting(task, m, 0);
2620 static int proc_tgid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2621 struct pid *pid, struct task_struct *task)
2623 return do_io_accounting(task, m, 1);
2625 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2627 #ifdef CONFIG_USER_NS
2628 static int proc_id_map_open(struct inode *inode, struct file *file,
2629 const struct seq_operations *seq_ops)
2631 struct user_namespace *ns = NULL;
2632 struct task_struct *task;
2633 struct seq_file *seq;
2634 int ret = -EINVAL;
2636 task = get_proc_task(inode);
2637 if (task) {
2638 rcu_read_lock();
2639 ns = get_user_ns(task_cred_xxx(task, user_ns));
2640 rcu_read_unlock();
2641 put_task_struct(task);
2643 if (!ns)
2644 goto err;
2646 ret = seq_open(file, seq_ops);
2647 if (ret)
2648 goto err_put_ns;
2650 seq = file->private_data;
2651 seq->private = ns;
2653 return 0;
2654 err_put_ns:
2655 put_user_ns(ns);
2656 err:
2657 return ret;
2660 static int proc_id_map_release(struct inode *inode, struct file *file)
2662 struct seq_file *seq = file->private_data;
2663 struct user_namespace *ns = seq->private;
2664 put_user_ns(ns);
2665 return seq_release(inode, file);
2668 static int proc_uid_map_open(struct inode *inode, struct file *file)
2670 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2673 static int proc_gid_map_open(struct inode *inode, struct file *file)
2675 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2678 static int proc_projid_map_open(struct inode *inode, struct file *file)
2680 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2683 static const struct file_operations proc_uid_map_operations = {
2684 .open = proc_uid_map_open,
2685 .write = proc_uid_map_write,
2686 .read = seq_read,
2687 .llseek = seq_lseek,
2688 .release = proc_id_map_release,
2691 static const struct file_operations proc_gid_map_operations = {
2692 .open = proc_gid_map_open,
2693 .write = proc_gid_map_write,
2694 .read = seq_read,
2695 .llseek = seq_lseek,
2696 .release = proc_id_map_release,
2699 static const struct file_operations proc_projid_map_operations = {
2700 .open = proc_projid_map_open,
2701 .write = proc_projid_map_write,
2702 .read = seq_read,
2703 .llseek = seq_lseek,
2704 .release = proc_id_map_release,
2707 static int proc_setgroups_open(struct inode *inode, struct file *file)
2709 struct user_namespace *ns = NULL;
2710 struct task_struct *task;
2711 int ret;
2713 ret = -ESRCH;
2714 task = get_proc_task(inode);
2715 if (task) {
2716 rcu_read_lock();
2717 ns = get_user_ns(task_cred_xxx(task, user_ns));
2718 rcu_read_unlock();
2719 put_task_struct(task);
2721 if (!ns)
2722 goto err;
2724 if (file->f_mode & FMODE_WRITE) {
2725 ret = -EACCES;
2726 if (!ns_capable(ns, CAP_SYS_ADMIN))
2727 goto err_put_ns;
2730 ret = single_open(file, &proc_setgroups_show, ns);
2731 if (ret)
2732 goto err_put_ns;
2734 return 0;
2735 err_put_ns:
2736 put_user_ns(ns);
2737 err:
2738 return ret;
2741 static int proc_setgroups_release(struct inode *inode, struct file *file)
2743 struct seq_file *seq = file->private_data;
2744 struct user_namespace *ns = seq->private;
2745 int ret = single_release(inode, file);
2746 put_user_ns(ns);
2747 return ret;
2750 static const struct file_operations proc_setgroups_operations = {
2751 .open = proc_setgroups_open,
2752 .write = proc_setgroups_write,
2753 .read = seq_read,
2754 .llseek = seq_lseek,
2755 .release = proc_setgroups_release,
2757 #endif /* CONFIG_USER_NS */
2759 static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2760 struct pid *pid, struct task_struct *task)
2762 int err = lock_trace(task);
2763 if (!err) {
2764 seq_printf(m, "%08x\n", task->personality);
2765 unlock_trace(task);
2767 return err;
2771 * Thread groups
2773 static const struct file_operations proc_task_operations;
2774 static const struct inode_operations proc_task_inode_operations;
2776 static const struct pid_entry tgid_base_stuff[] = {
2777 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2778 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2779 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
2780 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2781 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
2782 #ifdef CONFIG_NET
2783 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2784 #endif
2785 REG("environ", S_IRUSR, proc_environ_operations),
2786 ONE("auxv", S_IRUSR, proc_pid_auxv),
2787 ONE("status", S_IRUGO, proc_pid_status),
2788 ONE("personality", S_IRUSR, proc_pid_personality),
2789 ONE("limits", S_IRUGO, proc_pid_limits),
2790 #ifdef CONFIG_SCHED_DEBUG
2791 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2792 #endif
2793 #ifdef CONFIG_SCHED_AUTOGROUP
2794 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2795 #endif
2796 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2797 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2798 ONE("syscall", S_IRUSR, proc_pid_syscall),
2799 #endif
2800 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
2801 ONE("stat", S_IRUGO, proc_tgid_stat),
2802 ONE("statm", S_IRUGO, proc_pid_statm),
2803 REG("maps", S_IRUGO, proc_pid_maps_operations),
2804 #ifdef CONFIG_NUMA
2805 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
2806 #endif
2807 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2808 LNK("cwd", proc_cwd_link),
2809 LNK("root", proc_root_link),
2810 LNK("exe", proc_exe_link),
2811 REG("mounts", S_IRUGO, proc_mounts_operations),
2812 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2813 REG("mountstats", S_IRUSR, proc_mountstats_operations),
2814 #ifdef CONFIG_PROC_PAGE_MONITOR
2815 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2816 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
2817 REG("pagemap", S_IRUSR, proc_pagemap_operations),
2818 #endif
2819 #ifdef CONFIG_SECURITY
2820 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
2821 #endif
2822 #ifdef CONFIG_KALLSYMS
2823 ONE("wchan", S_IRUGO, proc_pid_wchan),
2824 #endif
2825 #ifdef CONFIG_STACKTRACE
2826 ONE("stack", S_IRUSR, proc_pid_stack),
2827 #endif
2828 #ifdef CONFIG_SCHED_INFO
2829 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
2830 #endif
2831 #ifdef CONFIG_LATENCYTOP
2832 REG("latency", S_IRUGO, proc_lstats_operations),
2833 #endif
2834 #ifdef CONFIG_PROC_PID_CPUSET
2835 ONE("cpuset", S_IRUGO, proc_cpuset_show),
2836 #endif
2837 #ifdef CONFIG_CGROUPS
2838 ONE("cgroup", S_IRUGO, proc_cgroup_show),
2839 #endif
2840 ONE("oom_score", S_IRUGO, proc_oom_score),
2841 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
2842 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
2843 #ifdef CONFIG_AUDITSYSCALL
2844 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2845 REG("sessionid", S_IRUGO, proc_sessionid_operations),
2846 #endif
2847 #ifdef CONFIG_FAULT_INJECTION
2848 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2849 #endif
2850 #ifdef CONFIG_ELF_CORE
2851 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
2852 #endif
2853 #ifdef CONFIG_TASK_IO_ACCOUNTING
2854 ONE("io", S_IRUSR, proc_tgid_io_accounting),
2855 #endif
2856 #ifdef CONFIG_HARDWALL
2857 ONE("hardwall", S_IRUGO, proc_pid_hardwall),
2858 #endif
2859 #ifdef CONFIG_USER_NS
2860 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2861 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
2862 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
2863 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
2864 #endif
2865 #ifdef CONFIG_CHECKPOINT_RESTORE
2866 REG("timers", S_IRUGO, proc_timers_operations),
2867 #endif
2870 static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
2872 return proc_pident_readdir(file, ctx,
2873 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2876 static const struct file_operations proc_tgid_base_operations = {
2877 .read = generic_read_dir,
2878 .iterate = proc_tgid_base_readdir,
2879 .llseek = default_llseek,
2882 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2884 return proc_pident_lookup(dir, dentry,
2885 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2888 static const struct inode_operations proc_tgid_base_inode_operations = {
2889 .lookup = proc_tgid_base_lookup,
2890 .getattr = pid_getattr,
2891 .setattr = proc_setattr,
2892 .permission = proc_pid_permission,
2895 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
2897 struct dentry *dentry, *leader, *dir;
2898 char buf[PROC_NUMBUF];
2899 struct qstr name;
2901 name.name = buf;
2902 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2903 /* no ->d_hash() rejects on procfs */
2904 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
2905 if (dentry) {
2906 d_invalidate(dentry);
2907 dput(dentry);
2910 if (pid == tgid)
2911 return;
2913 name.name = buf;
2914 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2915 leader = d_hash_and_lookup(mnt->mnt_root, &name);
2916 if (!leader)
2917 goto out;
2919 name.name = "task";
2920 name.len = strlen(name.name);
2921 dir = d_hash_and_lookup(leader, &name);
2922 if (!dir)
2923 goto out_put_leader;
2925 name.name = buf;
2926 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2927 dentry = d_hash_and_lookup(dir, &name);
2928 if (dentry) {
2929 d_invalidate(dentry);
2930 dput(dentry);
2933 dput(dir);
2934 out_put_leader:
2935 dput(leader);
2936 out:
2937 return;
2941 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2942 * @task: task that should be flushed.
2944 * When flushing dentries from proc, one needs to flush them from global
2945 * proc (proc_mnt) and from all the namespaces' procs this task was seen
2946 * in. This call is supposed to do all of this job.
2948 * Looks in the dcache for
2949 * /proc/@pid
2950 * /proc/@tgid/task/@pid
2951 * if either directory is present flushes it and all of it'ts children
2952 * from the dcache.
2954 * It is safe and reasonable to cache /proc entries for a task until
2955 * that task exits. After that they just clog up the dcache with
2956 * useless entries, possibly causing useful dcache entries to be
2957 * flushed instead. This routine is proved to flush those useless
2958 * dcache entries at process exit time.
2960 * NOTE: This routine is just an optimization so it does not guarantee
2961 * that no dcache entries will exist at process exit time it
2962 * just makes it very unlikely that any will persist.
2965 void proc_flush_task(struct task_struct *task)
2967 int i;
2968 struct pid *pid, *tgid;
2969 struct upid *upid;
2971 pid = task_pid(task);
2972 tgid = task_tgid(task);
2974 for (i = 0; i <= pid->level; i++) {
2975 upid = &pid->numbers[i];
2976 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
2977 tgid->numbers[i].nr);
2981 static int proc_pid_instantiate(struct inode *dir,
2982 struct dentry * dentry,
2983 struct task_struct *task, const void *ptr)
2985 struct inode *inode;
2987 inode = proc_pid_make_inode(dir->i_sb, task);
2988 if (!inode)
2989 goto out;
2991 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2992 inode->i_op = &proc_tgid_base_inode_operations;
2993 inode->i_fop = &proc_tgid_base_operations;
2994 inode->i_flags|=S_IMMUTABLE;
2996 set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
2997 ARRAY_SIZE(tgid_base_stuff)));
2999 d_set_d_op(dentry, &pid_dentry_operations);
3001 d_add(dentry, inode);
3002 /* Close the race of the process dying before we return the dentry */
3003 if (pid_revalidate(dentry, 0))
3004 return 0;
3005 out:
3006 return -ENOENT;
3009 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
3011 int result = -ENOENT;
3012 struct task_struct *task;
3013 unsigned tgid;
3014 struct pid_namespace *ns;
3016 tgid = name_to_int(&dentry->d_name);
3017 if (tgid == ~0U)
3018 goto out;
3020 ns = dentry->d_sb->s_fs_info;
3021 rcu_read_lock();
3022 task = find_task_by_pid_ns(tgid, ns);
3023 if (task)
3024 get_task_struct(task);
3025 rcu_read_unlock();
3026 if (!task)
3027 goto out;
3029 result = proc_pid_instantiate(dir, dentry, task, NULL);
3030 put_task_struct(task);
3031 out:
3032 return ERR_PTR(result);
3036 * Find the first task with tgid >= tgid
3039 struct tgid_iter {
3040 unsigned int tgid;
3041 struct task_struct *task;
3043 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3045 struct pid *pid;
3047 if (iter.task)
3048 put_task_struct(iter.task);
3049 rcu_read_lock();
3050 retry:
3051 iter.task = NULL;
3052 pid = find_ge_pid(iter.tgid, ns);
3053 if (pid) {
3054 iter.tgid = pid_nr_ns(pid, ns);
3055 iter.task = pid_task(pid, PIDTYPE_PID);
3056 /* What we to know is if the pid we have find is the
3057 * pid of a thread_group_leader. Testing for task
3058 * being a thread_group_leader is the obvious thing
3059 * todo but there is a window when it fails, due to
3060 * the pid transfer logic in de_thread.
3062 * So we perform the straight forward test of seeing
3063 * if the pid we have found is the pid of a thread
3064 * group leader, and don't worry if the task we have
3065 * found doesn't happen to be a thread group leader.
3066 * As we don't care in the case of readdir.
3068 if (!iter.task || !has_group_leader_pid(iter.task)) {
3069 iter.tgid += 1;
3070 goto retry;
3072 get_task_struct(iter.task);
3074 rcu_read_unlock();
3075 return iter;
3078 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
3080 /* for the /proc/ directory itself, after non-process stuff has been done */
3081 int proc_pid_readdir(struct file *file, struct dir_context *ctx)
3083 struct tgid_iter iter;
3084 struct pid_namespace *ns = file_inode(file)->i_sb->s_fs_info;
3085 loff_t pos = ctx->pos;
3087 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
3088 return 0;
3090 if (pos == TGID_OFFSET - 2) {
3091 struct inode *inode = d_inode(ns->proc_self);
3092 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
3093 return 0;
3094 ctx->pos = pos = pos + 1;
3096 if (pos == TGID_OFFSET - 1) {
3097 struct inode *inode = d_inode(ns->proc_thread_self);
3098 if (!dir_emit(ctx, "thread-self", 11, inode->i_ino, DT_LNK))
3099 return 0;
3100 ctx->pos = pos = pos + 1;
3102 iter.tgid = pos - TGID_OFFSET;
3103 iter.task = NULL;
3104 for (iter = next_tgid(ns, iter);
3105 iter.task;
3106 iter.tgid += 1, iter = next_tgid(ns, iter)) {
3107 char name[PROC_NUMBUF];
3108 int len;
3110 cond_resched();
3111 if (!has_pid_permissions(ns, iter.task, 2))
3112 continue;
3114 len = snprintf(name, sizeof(name), "%d", iter.tgid);
3115 ctx->pos = iter.tgid + TGID_OFFSET;
3116 if (!proc_fill_cache(file, ctx, name, len,
3117 proc_pid_instantiate, iter.task, NULL)) {
3118 put_task_struct(iter.task);
3119 return 0;
3122 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
3123 return 0;
3127 * proc_tid_comm_permission is a special permission function exclusively
3128 * used for the node /proc/<pid>/task/<tid>/comm.
3129 * It bypasses generic permission checks in the case where a task of the same
3130 * task group attempts to access the node.
3131 * The rationale behind this is that glibc and bionic access this node for
3132 * cross thread naming (pthread_set/getname_np(!self)). However, if
3133 * PR_SET_DUMPABLE gets set to 0 this node among others becomes uid=0 gid=0,
3134 * which locks out the cross thread naming implementation.
3135 * This function makes sure that the node is always accessible for members of
3136 * same thread group.
3138 static int proc_tid_comm_permission(struct inode *inode, int mask)
3140 bool is_same_tgroup;
3141 struct task_struct *task;
3143 task = get_proc_task(inode);
3144 if (!task)
3145 return -ESRCH;
3146 is_same_tgroup = same_thread_group(current, task);
3147 put_task_struct(task);
3149 if (likely(is_same_tgroup && !(mask & MAY_EXEC))) {
3150 /* This file (/proc/<pid>/task/<tid>/comm) can always be
3151 * read or written by the members of the corresponding
3152 * thread group.
3154 return 0;
3157 return generic_permission(inode, mask);
3160 static const struct inode_operations proc_tid_comm_inode_operations = {
3161 .permission = proc_tid_comm_permission,
3165 * Tasks
3167 static const struct pid_entry tid_base_stuff[] = {
3168 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
3169 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
3170 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
3171 #ifdef CONFIG_NET
3172 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
3173 #endif
3174 REG("environ", S_IRUSR, proc_environ_operations),
3175 ONE("auxv", S_IRUSR, proc_pid_auxv),
3176 ONE("status", S_IRUGO, proc_pid_status),
3177 ONE("personality", S_IRUSR, proc_pid_personality),
3178 ONE("limits", S_IRUGO, proc_pid_limits),
3179 #ifdef CONFIG_SCHED_DEBUG
3180 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
3181 #endif
3182 NOD("comm", S_IFREG|S_IRUGO|S_IWUSR,
3183 &proc_tid_comm_inode_operations,
3184 &proc_pid_set_comm_operations, {}),
3185 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3186 ONE("syscall", S_IRUSR, proc_pid_syscall),
3187 #endif
3188 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
3189 ONE("stat", S_IRUGO, proc_tid_stat),
3190 ONE("statm", S_IRUGO, proc_pid_statm),
3191 REG("maps", S_IRUGO, proc_tid_maps_operations),
3192 #ifdef CONFIG_PROC_CHILDREN
3193 REG("children", S_IRUGO, proc_tid_children_operations),
3194 #endif
3195 #ifdef CONFIG_NUMA
3196 REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
3197 #endif
3198 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3199 LNK("cwd", proc_cwd_link),
3200 LNK("root", proc_root_link),
3201 LNK("exe", proc_exe_link),
3202 REG("mounts", S_IRUGO, proc_mounts_operations),
3203 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
3204 #ifdef CONFIG_PROC_PAGE_MONITOR
3205 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3206 REG("smaps", S_IRUGO, proc_tid_smaps_operations),
3207 REG("pagemap", S_IRUSR, proc_pagemap_operations),
3208 #endif
3209 #ifdef CONFIG_SECURITY
3210 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
3211 #endif
3212 #ifdef CONFIG_KALLSYMS
3213 ONE("wchan", S_IRUGO, proc_pid_wchan),
3214 #endif
3215 #ifdef CONFIG_STACKTRACE
3216 ONE("stack", S_IRUSR, proc_pid_stack),
3217 #endif
3218 #ifdef CONFIG_SCHED_INFO
3219 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
3220 #endif
3221 #ifdef CONFIG_LATENCYTOP
3222 REG("latency", S_IRUGO, proc_lstats_operations),
3223 #endif
3224 #ifdef CONFIG_PROC_PID_CPUSET
3225 ONE("cpuset", S_IRUGO, proc_cpuset_show),
3226 #endif
3227 #ifdef CONFIG_CGROUPS
3228 ONE("cgroup", S_IRUGO, proc_cgroup_show),
3229 #endif
3230 ONE("oom_score", S_IRUGO, proc_oom_score),
3231 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
3232 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
3233 #ifdef CONFIG_AUDITSYSCALL
3234 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3235 REG("sessionid", S_IRUGO, proc_sessionid_operations),
3236 #endif
3237 #ifdef CONFIG_FAULT_INJECTION
3238 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3239 #endif
3240 #ifdef CONFIG_TASK_IO_ACCOUNTING
3241 ONE("io", S_IRUSR, proc_tid_io_accounting),
3242 #endif
3243 #ifdef CONFIG_HARDWALL
3244 ONE("hardwall", S_IRUGO, proc_pid_hardwall),
3245 #endif
3246 #ifdef CONFIG_USER_NS
3247 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3248 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
3249 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
3250 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
3251 #endif
3254 static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
3256 return proc_pident_readdir(file, ctx,
3257 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3260 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3262 return proc_pident_lookup(dir, dentry,
3263 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3266 static const struct file_operations proc_tid_base_operations = {
3267 .read = generic_read_dir,
3268 .iterate = proc_tid_base_readdir,
3269 .llseek = default_llseek,
3272 static const struct inode_operations proc_tid_base_inode_operations = {
3273 .lookup = proc_tid_base_lookup,
3274 .getattr = pid_getattr,
3275 .setattr = proc_setattr,
3278 static int proc_task_instantiate(struct inode *dir,
3279 struct dentry *dentry, struct task_struct *task, const void *ptr)
3281 struct inode *inode;
3282 inode = proc_pid_make_inode(dir->i_sb, task);
3284 if (!inode)
3285 goto out;
3286 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3287 inode->i_op = &proc_tid_base_inode_operations;
3288 inode->i_fop = &proc_tid_base_operations;
3289 inode->i_flags|=S_IMMUTABLE;
3291 set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3292 ARRAY_SIZE(tid_base_stuff)));
3294 d_set_d_op(dentry, &pid_dentry_operations);
3296 d_add(dentry, inode);
3297 /* Close the race of the process dying before we return the dentry */
3298 if (pid_revalidate(dentry, 0))
3299 return 0;
3300 out:
3301 return -ENOENT;
3304 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
3306 int result = -ENOENT;
3307 struct task_struct *task;
3308 struct task_struct *leader = get_proc_task(dir);
3309 unsigned tid;
3310 struct pid_namespace *ns;
3312 if (!leader)
3313 goto out_no_task;
3315 tid = name_to_int(&dentry->d_name);
3316 if (tid == ~0U)
3317 goto out;
3319 ns = dentry->d_sb->s_fs_info;
3320 rcu_read_lock();
3321 task = find_task_by_pid_ns(tid, ns);
3322 if (task)
3323 get_task_struct(task);
3324 rcu_read_unlock();
3325 if (!task)
3326 goto out;
3327 if (!same_thread_group(leader, task))
3328 goto out_drop_task;
3330 result = proc_task_instantiate(dir, dentry, task, NULL);
3331 out_drop_task:
3332 put_task_struct(task);
3333 out:
3334 put_task_struct(leader);
3335 out_no_task:
3336 return ERR_PTR(result);
3340 * Find the first tid of a thread group to return to user space.
3342 * Usually this is just the thread group leader, but if the users
3343 * buffer was too small or there was a seek into the middle of the
3344 * directory we have more work todo.
3346 * In the case of a short read we start with find_task_by_pid.
3348 * In the case of a seek we start with the leader and walk nr
3349 * threads past it.
3351 static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
3352 struct pid_namespace *ns)
3354 struct task_struct *pos, *task;
3355 unsigned long nr = f_pos;
3357 if (nr != f_pos) /* 32bit overflow? */
3358 return NULL;
3360 rcu_read_lock();
3361 task = pid_task(pid, PIDTYPE_PID);
3362 if (!task)
3363 goto fail;
3365 /* Attempt to start with the tid of a thread */
3366 if (tid && nr) {
3367 pos = find_task_by_pid_ns(tid, ns);
3368 if (pos && same_thread_group(pos, task))
3369 goto found;
3372 /* If nr exceeds the number of threads there is nothing todo */
3373 if (nr >= get_nr_threads(task))
3374 goto fail;
3376 /* If we haven't found our starting place yet start
3377 * with the leader and walk nr threads forward.
3379 pos = task = task->group_leader;
3380 do {
3381 if (!nr--)
3382 goto found;
3383 } while_each_thread(task, pos);
3384 fail:
3385 pos = NULL;
3386 goto out;
3387 found:
3388 get_task_struct(pos);
3389 out:
3390 rcu_read_unlock();
3391 return pos;
3395 * Find the next thread in the thread list.
3396 * Return NULL if there is an error or no next thread.
3398 * The reference to the input task_struct is released.
3400 static struct task_struct *next_tid(struct task_struct *start)
3402 struct task_struct *pos = NULL;
3403 rcu_read_lock();
3404 if (pid_alive(start)) {
3405 pos = next_thread(start);
3406 if (thread_group_leader(pos))
3407 pos = NULL;
3408 else
3409 get_task_struct(pos);
3411 rcu_read_unlock();
3412 put_task_struct(start);
3413 return pos;
3416 /* for the /proc/TGID/task/ directories */
3417 static int proc_task_readdir(struct file *file, struct dir_context *ctx)
3419 struct inode *inode = file_inode(file);
3420 struct task_struct *task;
3421 struct pid_namespace *ns;
3422 int tid;
3424 if (proc_inode_is_dead(inode))
3425 return -ENOENT;
3427 if (!dir_emit_dots(file, ctx))
3428 return 0;
3430 /* f_version caches the tgid value that the last readdir call couldn't
3431 * return. lseek aka telldir automagically resets f_version to 0.
3433 ns = inode->i_sb->s_fs_info;
3434 tid = (int)file->f_version;
3435 file->f_version = 0;
3436 for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
3437 task;
3438 task = next_tid(task), ctx->pos++) {
3439 char name[PROC_NUMBUF];
3440 int len;
3441 tid = task_pid_nr_ns(task, ns);
3442 len = snprintf(name, sizeof(name), "%d", tid);
3443 if (!proc_fill_cache(file, ctx, name, len,
3444 proc_task_instantiate, task, NULL)) {
3445 /* returning this tgid failed, save it as the first
3446 * pid for the next readir call */
3447 file->f_version = (u64)tid;
3448 put_task_struct(task);
3449 break;
3453 return 0;
3456 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3458 struct inode *inode = d_inode(dentry);
3459 struct task_struct *p = get_proc_task(inode);
3460 generic_fillattr(inode, stat);
3462 if (p) {
3463 stat->nlink += get_nr_threads(p);
3464 put_task_struct(p);
3467 return 0;
3470 static const struct inode_operations proc_task_inode_operations = {
3471 .lookup = proc_task_lookup,
3472 .getattr = proc_task_getattr,
3473 .setattr = proc_setattr,
3474 .permission = proc_pid_permission,
3477 static const struct file_operations proc_task_operations = {
3478 .read = generic_read_dir,
3479 .iterate = proc_task_readdir,
3480 .llseek = default_llseek,