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 <linux/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.
108 static u8 nlink_tgid
;
114 const struct inode_operations
*iop
;
115 const struct file_operations
*fop
;
119 #define NOD(NAME, MODE, IOP, FOP, OP) { \
121 .len = sizeof(NAME) - 1, \
128 #define DIR(NAME, MODE, iops, fops) \
129 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
130 #define LNK(NAME, get_link) \
131 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
132 &proc_pid_link_inode_operations, NULL, \
133 { .proc_get_link = get_link } )
134 #define REG(NAME, MODE, fops) \
135 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
136 #define ONE(NAME, MODE, show) \
137 NOD(NAME, (S_IFREG|(MODE)), \
138 NULL, &proc_single_file_operations, \
139 { .proc_show = show } )
142 * Count the number of hardlinks for the pid_entry table, excluding the .
145 static unsigned int __init
pid_entry_nlink(const struct pid_entry
*entries
,
152 for (i
= 0; i
< n
; ++i
) {
153 if (S_ISDIR(entries
[i
].mode
))
160 static int get_task_root(struct task_struct
*task
, struct path
*root
)
162 int result
= -ENOENT
;
166 get_fs_root(task
->fs
, root
);
173 static int proc_cwd_link(struct dentry
*dentry
, struct path
*path
)
175 struct task_struct
*task
= get_proc_task(d_inode(dentry
));
176 int result
= -ENOENT
;
181 get_fs_pwd(task
->fs
, path
);
185 put_task_struct(task
);
190 static int proc_root_link(struct dentry
*dentry
, struct path
*path
)
192 struct task_struct
*task
= get_proc_task(d_inode(dentry
));
193 int result
= -ENOENT
;
196 result
= get_task_root(task
, path
);
197 put_task_struct(task
);
202 static ssize_t
proc_pid_cmdline_read(struct file
*file
, char __user
*buf
,
203 size_t _count
, loff_t
*pos
)
205 struct task_struct
*tsk
;
206 struct mm_struct
*mm
;
208 unsigned long count
= _count
;
209 unsigned long arg_start
, arg_end
, env_start
, env_end
;
210 unsigned long len1
, len2
, len
;
217 tsk
= get_proc_task(file_inode(file
));
220 mm
= get_task_mm(tsk
);
221 put_task_struct(tsk
);
224 /* Check if process spawned far enough to have cmdline. */
230 page
= (char *)__get_free_page(GFP_TEMPORARY
);
236 down_read(&mm
->mmap_sem
);
237 arg_start
= mm
->arg_start
;
238 arg_end
= mm
->arg_end
;
239 env_start
= mm
->env_start
;
240 env_end
= mm
->env_end
;
241 up_read(&mm
->mmap_sem
);
243 BUG_ON(arg_start
> arg_end
);
244 BUG_ON(env_start
> env_end
);
246 len1
= arg_end
- arg_start
;
247 len2
= env_end
- env_start
;
255 * Inherently racy -- command line shares address space
256 * with code and data.
258 rv
= access_remote_vm(mm
, arg_end
- 1, &c
, 1, 0);
265 /* Command line (set of strings) occupies whole ARGV. */
269 p
= arg_start
+ *pos
;
271 while (count
> 0 && len
> 0) {
275 _count
= min3(count
, len
, PAGE_SIZE
);
276 nr_read
= access_remote_vm(mm
, p
, page
, _count
, 0);
282 if (copy_to_user(buf
, page
, nr_read
)) {
295 * Command line (1 string) occupies ARGV and maybe
298 if (len1
+ len2
<= *pos
)
303 p
= arg_start
+ *pos
;
305 while (count
> 0 && len
> 0) {
306 unsigned int _count
, l
;
310 _count
= min3(count
, len
, PAGE_SIZE
);
311 nr_read
= access_remote_vm(mm
, p
, page
, _count
, 0);
318 * Command line can be shorter than whole ARGV
319 * even if last "marker" byte says it is not.
322 l
= strnlen(page
, nr_read
);
328 if (copy_to_user(buf
, page
, nr_read
)) {
344 * Command line (1 string) occupies ARGV and
348 p
= env_start
+ *pos
- len1
;
349 len
= len1
+ len2
- *pos
;
354 while (count
> 0 && len
> 0) {
355 unsigned int _count
, l
;
359 _count
= min3(count
, len
, PAGE_SIZE
);
360 nr_read
= access_remote_vm(mm
, p
, page
, _count
, 0);
368 l
= strnlen(page
, nr_read
);
374 if (copy_to_user(buf
, page
, nr_read
)) {
393 free_page((unsigned long)page
);
401 static const struct file_operations proc_pid_cmdline_ops
= {
402 .read
= proc_pid_cmdline_read
,
403 .llseek
= generic_file_llseek
,
406 #ifdef CONFIG_KALLSYMS
408 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
409 * Returns the resolved symbol. If that fails, simply return the address.
411 static int proc_pid_wchan(struct seq_file
*m
, struct pid_namespace
*ns
,
412 struct pid
*pid
, struct task_struct
*task
)
415 char symname
[KSYM_NAME_LEN
];
417 wchan
= get_wchan(task
);
419 if (wchan
&& ptrace_may_access(task
, PTRACE_MODE_READ_FSCREDS
)
420 && !lookup_symbol_name(wchan
, symname
))
421 seq_printf(m
, "%s", symname
);
427 #endif /* CONFIG_KALLSYMS */
429 static int lock_trace(struct task_struct
*task
)
431 int err
= mutex_lock_killable(&task
->signal
->cred_guard_mutex
);
434 if (!ptrace_may_access(task
, PTRACE_MODE_ATTACH_FSCREDS
)) {
435 mutex_unlock(&task
->signal
->cred_guard_mutex
);
441 static void unlock_trace(struct task_struct
*task
)
443 mutex_unlock(&task
->signal
->cred_guard_mutex
);
446 #ifdef CONFIG_STACKTRACE
448 #define MAX_STACK_TRACE_DEPTH 64
450 static int proc_pid_stack(struct seq_file
*m
, struct pid_namespace
*ns
,
451 struct pid
*pid
, struct task_struct
*task
)
453 struct stack_trace trace
;
454 unsigned long *entries
;
458 entries
= kmalloc(MAX_STACK_TRACE_DEPTH
* sizeof(*entries
), GFP_KERNEL
);
462 trace
.nr_entries
= 0;
463 trace
.max_entries
= MAX_STACK_TRACE_DEPTH
;
464 trace
.entries
= entries
;
467 err
= lock_trace(task
);
469 save_stack_trace_tsk(task
, &trace
);
471 for (i
= 0; i
< trace
.nr_entries
; i
++) {
472 seq_printf(m
, "[<%pK>] %pB\n",
473 (void *)entries
[i
], (void *)entries
[i
]);
483 #ifdef CONFIG_SCHED_INFO
485 * Provides /proc/PID/schedstat
487 static int proc_pid_schedstat(struct seq_file
*m
, struct pid_namespace
*ns
,
488 struct pid
*pid
, struct task_struct
*task
)
490 if (unlikely(!sched_info_on()))
491 seq_printf(m
, "0 0 0\n");
493 seq_printf(m
, "%llu %llu %lu\n",
494 (unsigned long long)task
->se
.sum_exec_runtime
,
495 (unsigned long long)task
->sched_info
.run_delay
,
496 task
->sched_info
.pcount
);
502 #ifdef CONFIG_LATENCYTOP
503 static int lstats_show_proc(struct seq_file
*m
, void *v
)
506 struct inode
*inode
= m
->private;
507 struct task_struct
*task
= get_proc_task(inode
);
511 seq_puts(m
, "Latency Top version : v0.1\n");
512 for (i
= 0; i
< 32; i
++) {
513 struct latency_record
*lr
= &task
->latency_record
[i
];
514 if (lr
->backtrace
[0]) {
516 seq_printf(m
, "%i %li %li",
517 lr
->count
, lr
->time
, lr
->max
);
518 for (q
= 0; q
< LT_BACKTRACEDEPTH
; q
++) {
519 unsigned long bt
= lr
->backtrace
[q
];
524 seq_printf(m
, " %ps", (void *)bt
);
530 put_task_struct(task
);
534 static int lstats_open(struct inode
*inode
, struct file
*file
)
536 return single_open(file
, lstats_show_proc
, inode
);
539 static ssize_t
lstats_write(struct file
*file
, const char __user
*buf
,
540 size_t count
, loff_t
*offs
)
542 struct task_struct
*task
= get_proc_task(file_inode(file
));
546 clear_all_latency_tracing(task
);
547 put_task_struct(task
);
552 static const struct file_operations proc_lstats_operations
= {
555 .write
= lstats_write
,
557 .release
= single_release
,
562 static int proc_oom_score(struct seq_file
*m
, struct pid_namespace
*ns
,
563 struct pid
*pid
, struct task_struct
*task
)
565 unsigned long totalpages
= totalram_pages
+ total_swap_pages
;
566 unsigned long points
= 0;
568 points
= oom_badness(task
, NULL
, NULL
, totalpages
) *
570 seq_printf(m
, "%lu\n", points
);
580 static const struct limit_names lnames
[RLIM_NLIMITS
] = {
581 [RLIMIT_CPU
] = {"Max cpu time", "seconds"},
582 [RLIMIT_FSIZE
] = {"Max file size", "bytes"},
583 [RLIMIT_DATA
] = {"Max data size", "bytes"},
584 [RLIMIT_STACK
] = {"Max stack size", "bytes"},
585 [RLIMIT_CORE
] = {"Max core file size", "bytes"},
586 [RLIMIT_RSS
] = {"Max resident set", "bytes"},
587 [RLIMIT_NPROC
] = {"Max processes", "processes"},
588 [RLIMIT_NOFILE
] = {"Max open files", "files"},
589 [RLIMIT_MEMLOCK
] = {"Max locked memory", "bytes"},
590 [RLIMIT_AS
] = {"Max address space", "bytes"},
591 [RLIMIT_LOCKS
] = {"Max file locks", "locks"},
592 [RLIMIT_SIGPENDING
] = {"Max pending signals", "signals"},
593 [RLIMIT_MSGQUEUE
] = {"Max msgqueue size", "bytes"},
594 [RLIMIT_NICE
] = {"Max nice priority", NULL
},
595 [RLIMIT_RTPRIO
] = {"Max realtime priority", NULL
},
596 [RLIMIT_RTTIME
] = {"Max realtime timeout", "us"},
599 /* Display limits for a process */
600 static int proc_pid_limits(struct seq_file
*m
, struct pid_namespace
*ns
,
601 struct pid
*pid
, struct task_struct
*task
)
606 struct rlimit rlim
[RLIM_NLIMITS
];
608 if (!lock_task_sighand(task
, &flags
))
610 memcpy(rlim
, task
->signal
->rlim
, sizeof(struct rlimit
) * RLIM_NLIMITS
);
611 unlock_task_sighand(task
, &flags
);
614 * print the file header
616 seq_printf(m
, "%-25s %-20s %-20s %-10s\n",
617 "Limit", "Soft Limit", "Hard Limit", "Units");
619 for (i
= 0; i
< RLIM_NLIMITS
; i
++) {
620 if (rlim
[i
].rlim_cur
== RLIM_INFINITY
)
621 seq_printf(m
, "%-25s %-20s ",
622 lnames
[i
].name
, "unlimited");
624 seq_printf(m
, "%-25s %-20lu ",
625 lnames
[i
].name
, rlim
[i
].rlim_cur
);
627 if (rlim
[i
].rlim_max
== RLIM_INFINITY
)
628 seq_printf(m
, "%-20s ", "unlimited");
630 seq_printf(m
, "%-20lu ", rlim
[i
].rlim_max
);
633 seq_printf(m
, "%-10s\n", lnames
[i
].unit
);
641 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
642 static int proc_pid_syscall(struct seq_file
*m
, struct pid_namespace
*ns
,
643 struct pid
*pid
, struct task_struct
*task
)
646 unsigned long args
[6], sp
, pc
;
649 res
= lock_trace(task
);
653 if (task_current_syscall(task
, &nr
, args
, 6, &sp
, &pc
))
654 seq_puts(m
, "running\n");
656 seq_printf(m
, "%ld 0x%lx 0x%lx\n", nr
, sp
, pc
);
659 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
661 args
[0], args
[1], args
[2], args
[3], args
[4], args
[5],
667 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
669 /************************************************************************/
670 /* Here the fs part begins */
671 /************************************************************************/
673 /* permission checks */
674 static int proc_fd_access_allowed(struct inode
*inode
)
676 struct task_struct
*task
;
678 /* Allow access to a task's file descriptors if it is us or we
679 * may use ptrace attach to the process and find out that
682 task
= get_proc_task(inode
);
684 allowed
= ptrace_may_access(task
, PTRACE_MODE_READ_FSCREDS
);
685 put_task_struct(task
);
690 int proc_setattr(struct dentry
*dentry
, struct iattr
*attr
)
693 struct inode
*inode
= d_inode(dentry
);
694 struct user_namespace
*s_user_ns
;
696 if (attr
->ia_valid
& ATTR_MODE
)
699 /* Don't let anyone mess with weird proc files */
700 s_user_ns
= inode
->i_sb
->s_user_ns
;
701 if (!kuid_has_mapping(s_user_ns
, inode
->i_uid
) ||
702 !kgid_has_mapping(s_user_ns
, inode
->i_gid
))
705 error
= setattr_prepare(dentry
, attr
);
709 setattr_copy(inode
, attr
);
710 mark_inode_dirty(inode
);
715 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
716 * or euid/egid (for hide_pid_min=2)?
718 static bool has_pid_permissions(struct pid_namespace
*pid
,
719 struct task_struct
*task
,
722 if (pid
->hide_pid
< hide_pid_min
)
724 if (in_group_p(pid
->pid_gid
))
726 return ptrace_may_access(task
, PTRACE_MODE_READ_FSCREDS
);
730 static int proc_pid_permission(struct inode
*inode
, int mask
)
732 struct pid_namespace
*pid
= inode
->i_sb
->s_fs_info
;
733 struct task_struct
*task
;
736 task
= get_proc_task(inode
);
739 has_perms
= has_pid_permissions(pid
, task
, 1);
740 put_task_struct(task
);
743 if (pid
->hide_pid
== 2) {
745 * Let's make getdents(), stat(), and open()
746 * consistent with each other. If a process
747 * may not stat() a file, it shouldn't be seen
755 return generic_permission(inode
, mask
);
760 static const struct inode_operations proc_def_inode_operations
= {
761 .setattr
= proc_setattr
,
764 static int proc_single_show(struct seq_file
*m
, void *v
)
766 struct inode
*inode
= m
->private;
767 struct pid_namespace
*ns
;
769 struct task_struct
*task
;
772 ns
= inode
->i_sb
->s_fs_info
;
773 pid
= proc_pid(inode
);
774 task
= get_pid_task(pid
, PIDTYPE_PID
);
778 ret
= PROC_I(inode
)->op
.proc_show(m
, ns
, pid
, task
);
780 put_task_struct(task
);
784 static int proc_single_open(struct inode
*inode
, struct file
*filp
)
786 return single_open(filp
, proc_single_show
, inode
);
789 static const struct file_operations proc_single_file_operations
= {
790 .open
= proc_single_open
,
793 .release
= single_release
,
797 struct mm_struct
*proc_mem_open(struct inode
*inode
, unsigned int mode
)
799 struct task_struct
*task
= get_proc_task(inode
);
800 struct mm_struct
*mm
= ERR_PTR(-ESRCH
);
803 mm
= mm_access(task
, mode
| PTRACE_MODE_FSCREDS
);
804 put_task_struct(task
);
806 if (!IS_ERR_OR_NULL(mm
)) {
807 /* ensure this mm_struct can't be freed */
808 atomic_inc(&mm
->mm_count
);
809 /* but do not pin its memory */
817 static int __mem_open(struct inode
*inode
, struct file
*file
, unsigned int mode
)
819 struct mm_struct
*mm
= proc_mem_open(inode
, mode
);
824 file
->private_data
= mm
;
828 static int mem_open(struct inode
*inode
, struct file
*file
)
830 int ret
= __mem_open(inode
, file
, PTRACE_MODE_ATTACH
);
832 /* OK to pass negative loff_t, we can catch out-of-range */
833 file
->f_mode
|= FMODE_UNSIGNED_OFFSET
;
838 static ssize_t
mem_rw(struct file
*file
, char __user
*buf
,
839 size_t count
, loff_t
*ppos
, int write
)
841 struct mm_struct
*mm
= file
->private_data
;
842 unsigned long addr
= *ppos
;
850 page
= (char *)__get_free_page(GFP_TEMPORARY
);
855 if (!atomic_inc_not_zero(&mm
->mm_users
))
858 /* Maybe we should limit FOLL_FORCE to actual ptrace users? */
864 int this_len
= min_t(int, count
, PAGE_SIZE
);
866 if (write
&& copy_from_user(page
, buf
, this_len
)) {
871 this_len
= access_remote_vm(mm
, addr
, page
, this_len
, flags
);
878 if (!write
&& copy_to_user(buf
, page
, this_len
)) {
892 free_page((unsigned long) page
);
896 static ssize_t
mem_read(struct file
*file
, char __user
*buf
,
897 size_t count
, loff_t
*ppos
)
899 return mem_rw(file
, buf
, count
, ppos
, 0);
902 static ssize_t
mem_write(struct file
*file
, const char __user
*buf
,
903 size_t count
, loff_t
*ppos
)
905 return mem_rw(file
, (char __user
*)buf
, count
, ppos
, 1);
908 loff_t
mem_lseek(struct file
*file
, loff_t offset
, int orig
)
912 file
->f_pos
= offset
;
915 file
->f_pos
+= offset
;
920 force_successful_syscall_return();
924 static int mem_release(struct inode
*inode
, struct file
*file
)
926 struct mm_struct
*mm
= file
->private_data
;
932 static const struct file_operations proc_mem_operations
= {
937 .release
= mem_release
,
940 static int environ_open(struct inode
*inode
, struct file
*file
)
942 return __mem_open(inode
, file
, PTRACE_MODE_READ
);
945 static ssize_t
environ_read(struct file
*file
, char __user
*buf
,
946 size_t count
, loff_t
*ppos
)
949 unsigned long src
= *ppos
;
951 struct mm_struct
*mm
= file
->private_data
;
952 unsigned long env_start
, env_end
;
954 /* Ensure the process spawned far enough to have an environment. */
955 if (!mm
|| !mm
->env_end
)
958 page
= (char *)__get_free_page(GFP_TEMPORARY
);
963 if (!atomic_inc_not_zero(&mm
->mm_users
))
966 down_read(&mm
->mmap_sem
);
967 env_start
= mm
->env_start
;
968 env_end
= mm
->env_end
;
969 up_read(&mm
->mmap_sem
);
972 size_t this_len
, max_len
;
975 if (src
>= (env_end
- env_start
))
978 this_len
= env_end
- (env_start
+ src
);
980 max_len
= min_t(size_t, PAGE_SIZE
, count
);
981 this_len
= min(max_len
, this_len
);
983 retval
= access_remote_vm(mm
, (env_start
+ src
), page
, this_len
, 0);
990 if (copy_to_user(buf
, page
, retval
)) {
1004 free_page((unsigned long) page
);
1008 static const struct file_operations proc_environ_operations
= {
1009 .open
= environ_open
,
1010 .read
= environ_read
,
1011 .llseek
= generic_file_llseek
,
1012 .release
= mem_release
,
1015 static int auxv_open(struct inode
*inode
, struct file
*file
)
1017 return __mem_open(inode
, file
, PTRACE_MODE_READ_FSCREDS
);
1020 static ssize_t
auxv_read(struct file
*file
, char __user
*buf
,
1021 size_t count
, loff_t
*ppos
)
1023 struct mm_struct
*mm
= file
->private_data
;
1024 unsigned int nwords
= 0;
1030 } while (mm
->saved_auxv
[nwords
- 2] != 0); /* AT_NULL */
1031 return simple_read_from_buffer(buf
, count
, ppos
, mm
->saved_auxv
,
1032 nwords
* sizeof(mm
->saved_auxv
[0]));
1035 static const struct file_operations proc_auxv_operations
= {
1038 .llseek
= generic_file_llseek
,
1039 .release
= mem_release
,
1042 static ssize_t
oom_adj_read(struct file
*file
, char __user
*buf
, size_t count
,
1045 struct task_struct
*task
= get_proc_task(file_inode(file
));
1046 char buffer
[PROC_NUMBUF
];
1047 int oom_adj
= OOM_ADJUST_MIN
;
1052 if (task
->signal
->oom_score_adj
== OOM_SCORE_ADJ_MAX
)
1053 oom_adj
= OOM_ADJUST_MAX
;
1055 oom_adj
= (task
->signal
->oom_score_adj
* -OOM_DISABLE
) /
1057 put_task_struct(task
);
1058 len
= snprintf(buffer
, sizeof(buffer
), "%d\n", oom_adj
);
1059 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
1062 static int __set_oom_adj(struct file
*file
, int oom_adj
, bool legacy
)
1064 static DEFINE_MUTEX(oom_adj_mutex
);
1065 struct mm_struct
*mm
= NULL
;
1066 struct task_struct
*task
;
1069 task
= get_proc_task(file_inode(file
));
1073 mutex_lock(&oom_adj_mutex
);
1075 if (oom_adj
< task
->signal
->oom_score_adj
&&
1076 !capable(CAP_SYS_RESOURCE
)) {
1081 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
1082 * /proc/pid/oom_score_adj instead.
1084 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
1085 current
->comm
, task_pid_nr(current
), task_pid_nr(task
),
1088 if ((short)oom_adj
< task
->signal
->oom_score_adj_min
&&
1089 !capable(CAP_SYS_RESOURCE
)) {
1096 * Make sure we will check other processes sharing the mm if this is
1097 * not vfrok which wants its own oom_score_adj.
1098 * pin the mm so it doesn't go away and get reused after task_unlock
1100 if (!task
->vfork_done
) {
1101 struct task_struct
*p
= find_lock_task_mm(task
);
1104 if (atomic_read(&p
->mm
->mm_users
) > 1) {
1106 atomic_inc(&mm
->mm_count
);
1112 task
->signal
->oom_score_adj
= oom_adj
;
1113 if (!legacy
&& has_capability_noaudit(current
, CAP_SYS_RESOURCE
))
1114 task
->signal
->oom_score_adj_min
= (short)oom_adj
;
1115 trace_oom_score_adj_update(task
);
1118 struct task_struct
*p
;
1121 for_each_process(p
) {
1122 if (same_thread_group(task
, p
))
1125 /* do not touch kernel threads or the global init */
1126 if (p
->flags
& PF_KTHREAD
|| is_global_init(p
))
1130 if (!p
->vfork_done
&& process_shares_mm(p
, mm
)) {
1131 pr_info("updating oom_score_adj for %d (%s) from %d to %d because it shares mm with %d (%s). Report if this is unexpected.\n",
1132 task_pid_nr(p
), p
->comm
,
1133 p
->signal
->oom_score_adj
, oom_adj
,
1134 task_pid_nr(task
), task
->comm
);
1135 p
->signal
->oom_score_adj
= oom_adj
;
1136 if (!legacy
&& has_capability_noaudit(current
, CAP_SYS_RESOURCE
))
1137 p
->signal
->oom_score_adj_min
= (short)oom_adj
;
1145 mutex_unlock(&oom_adj_mutex
);
1146 put_task_struct(task
);
1151 * /proc/pid/oom_adj exists solely for backwards compatibility with previous
1152 * kernels. The effective policy is defined by oom_score_adj, which has a
1153 * different scale: oom_adj grew exponentially and oom_score_adj grows linearly.
1154 * Values written to oom_adj are simply mapped linearly to oom_score_adj.
1155 * Processes that become oom disabled via oom_adj will still be oom disabled
1156 * with this implementation.
1158 * oom_adj cannot be removed since existing userspace binaries use it.
1160 static ssize_t
oom_adj_write(struct file
*file
, const char __user
*buf
,
1161 size_t count
, loff_t
*ppos
)
1163 char buffer
[PROC_NUMBUF
];
1167 memset(buffer
, 0, sizeof(buffer
));
1168 if (count
> sizeof(buffer
) - 1)
1169 count
= sizeof(buffer
) - 1;
1170 if (copy_from_user(buffer
, buf
, count
)) {
1175 err
= kstrtoint(strstrip(buffer
), 0, &oom_adj
);
1178 if ((oom_adj
< OOM_ADJUST_MIN
|| oom_adj
> OOM_ADJUST_MAX
) &&
1179 oom_adj
!= OOM_DISABLE
) {
1185 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1186 * value is always attainable.
1188 if (oom_adj
== OOM_ADJUST_MAX
)
1189 oom_adj
= OOM_SCORE_ADJ_MAX
;
1191 oom_adj
= (oom_adj
* OOM_SCORE_ADJ_MAX
) / -OOM_DISABLE
;
1193 err
= __set_oom_adj(file
, oom_adj
, true);
1195 return err
< 0 ? err
: count
;
1198 static const struct file_operations proc_oom_adj_operations
= {
1199 .read
= oom_adj_read
,
1200 .write
= oom_adj_write
,
1201 .llseek
= generic_file_llseek
,
1204 static ssize_t
oom_score_adj_read(struct file
*file
, char __user
*buf
,
1205 size_t count
, loff_t
*ppos
)
1207 struct task_struct
*task
= get_proc_task(file_inode(file
));
1208 char buffer
[PROC_NUMBUF
];
1209 short oom_score_adj
= OOM_SCORE_ADJ_MIN
;
1214 oom_score_adj
= task
->signal
->oom_score_adj
;
1215 put_task_struct(task
);
1216 len
= snprintf(buffer
, sizeof(buffer
), "%hd\n", oom_score_adj
);
1217 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
1220 static ssize_t
oom_score_adj_write(struct file
*file
, const char __user
*buf
,
1221 size_t count
, loff_t
*ppos
)
1223 char buffer
[PROC_NUMBUF
];
1227 memset(buffer
, 0, sizeof(buffer
));
1228 if (count
> sizeof(buffer
) - 1)
1229 count
= sizeof(buffer
) - 1;
1230 if (copy_from_user(buffer
, buf
, count
)) {
1235 err
= kstrtoint(strstrip(buffer
), 0, &oom_score_adj
);
1238 if (oom_score_adj
< OOM_SCORE_ADJ_MIN
||
1239 oom_score_adj
> OOM_SCORE_ADJ_MAX
) {
1244 err
= __set_oom_adj(file
, oom_score_adj
, false);
1246 return err
< 0 ? err
: count
;
1249 static const struct file_operations proc_oom_score_adj_operations
= {
1250 .read
= oom_score_adj_read
,
1251 .write
= oom_score_adj_write
,
1252 .llseek
= default_llseek
,
1255 #ifdef CONFIG_AUDITSYSCALL
1256 #define TMPBUFLEN 11
1257 static ssize_t
proc_loginuid_read(struct file
* file
, char __user
* buf
,
1258 size_t count
, loff_t
*ppos
)
1260 struct inode
* inode
= file_inode(file
);
1261 struct task_struct
*task
= get_proc_task(inode
);
1263 char tmpbuf
[TMPBUFLEN
];
1267 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
1268 from_kuid(file
->f_cred
->user_ns
,
1269 audit_get_loginuid(task
)));
1270 put_task_struct(task
);
1271 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1274 static ssize_t
proc_loginuid_write(struct file
* file
, const char __user
* buf
,
1275 size_t count
, loff_t
*ppos
)
1277 struct inode
* inode
= file_inode(file
);
1283 if (current
!= pid_task(proc_pid(inode
), PIDTYPE_PID
)) {
1290 /* No partial writes. */
1294 rv
= kstrtou32_from_user(buf
, count
, 10, &loginuid
);
1298 /* is userspace tring to explicitly UNSET the loginuid? */
1299 if (loginuid
== AUDIT_UID_UNSET
) {
1300 kloginuid
= INVALID_UID
;
1302 kloginuid
= make_kuid(file
->f_cred
->user_ns
, loginuid
);
1303 if (!uid_valid(kloginuid
))
1307 rv
= audit_set_loginuid(kloginuid
);
1313 static const struct file_operations proc_loginuid_operations
= {
1314 .read
= proc_loginuid_read
,
1315 .write
= proc_loginuid_write
,
1316 .llseek
= generic_file_llseek
,
1319 static ssize_t
proc_sessionid_read(struct file
* file
, char __user
* buf
,
1320 size_t count
, loff_t
*ppos
)
1322 struct inode
* inode
= file_inode(file
);
1323 struct task_struct
*task
= get_proc_task(inode
);
1325 char tmpbuf
[TMPBUFLEN
];
1329 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
1330 audit_get_sessionid(task
));
1331 put_task_struct(task
);
1332 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1335 static const struct file_operations proc_sessionid_operations
= {
1336 .read
= proc_sessionid_read
,
1337 .llseek
= generic_file_llseek
,
1341 #ifdef CONFIG_FAULT_INJECTION
1342 static ssize_t
proc_fault_inject_read(struct file
* file
, char __user
* buf
,
1343 size_t count
, loff_t
*ppos
)
1345 struct task_struct
*task
= get_proc_task(file_inode(file
));
1346 char buffer
[PROC_NUMBUF
];
1352 make_it_fail
= task
->make_it_fail
;
1353 put_task_struct(task
);
1355 len
= snprintf(buffer
, sizeof(buffer
), "%i\n", make_it_fail
);
1357 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
1360 static ssize_t
proc_fault_inject_write(struct file
* file
,
1361 const char __user
* buf
, size_t count
, loff_t
*ppos
)
1363 struct task_struct
*task
;
1364 char buffer
[PROC_NUMBUF
];
1368 if (!capable(CAP_SYS_RESOURCE
))
1370 memset(buffer
, 0, sizeof(buffer
));
1371 if (count
> sizeof(buffer
) - 1)
1372 count
= sizeof(buffer
) - 1;
1373 if (copy_from_user(buffer
, buf
, count
))
1375 rv
= kstrtoint(strstrip(buffer
), 0, &make_it_fail
);
1378 if (make_it_fail
< 0 || make_it_fail
> 1)
1381 task
= get_proc_task(file_inode(file
));
1384 task
->make_it_fail
= make_it_fail
;
1385 put_task_struct(task
);
1390 static const struct file_operations proc_fault_inject_operations
= {
1391 .read
= proc_fault_inject_read
,
1392 .write
= proc_fault_inject_write
,
1393 .llseek
= generic_file_llseek
,
1398 #ifdef CONFIG_SCHED_DEBUG
1400 * Print out various scheduling related per-task fields:
1402 static int sched_show(struct seq_file
*m
, void *v
)
1404 struct inode
*inode
= m
->private;
1405 struct task_struct
*p
;
1407 p
= get_proc_task(inode
);
1410 proc_sched_show_task(p
, m
);
1418 sched_write(struct file
*file
, const char __user
*buf
,
1419 size_t count
, loff_t
*offset
)
1421 struct inode
*inode
= file_inode(file
);
1422 struct task_struct
*p
;
1424 p
= get_proc_task(inode
);
1427 proc_sched_set_task(p
);
1434 static int sched_open(struct inode
*inode
, struct file
*filp
)
1436 return single_open(filp
, sched_show
, inode
);
1439 static const struct file_operations proc_pid_sched_operations
= {
1442 .write
= sched_write
,
1443 .llseek
= seq_lseek
,
1444 .release
= single_release
,
1449 #ifdef CONFIG_SCHED_AUTOGROUP
1451 * Print out autogroup related information:
1453 static int sched_autogroup_show(struct seq_file
*m
, void *v
)
1455 struct inode
*inode
= m
->private;
1456 struct task_struct
*p
;
1458 p
= get_proc_task(inode
);
1461 proc_sched_autogroup_show_task(p
, m
);
1469 sched_autogroup_write(struct file
*file
, const char __user
*buf
,
1470 size_t count
, loff_t
*offset
)
1472 struct inode
*inode
= file_inode(file
);
1473 struct task_struct
*p
;
1474 char buffer
[PROC_NUMBUF
];
1478 memset(buffer
, 0, sizeof(buffer
));
1479 if (count
> sizeof(buffer
) - 1)
1480 count
= sizeof(buffer
) - 1;
1481 if (copy_from_user(buffer
, buf
, count
))
1484 err
= kstrtoint(strstrip(buffer
), 0, &nice
);
1488 p
= get_proc_task(inode
);
1492 err
= proc_sched_autogroup_set_nice(p
, nice
);
1501 static int sched_autogroup_open(struct inode
*inode
, struct file
*filp
)
1505 ret
= single_open(filp
, sched_autogroup_show
, NULL
);
1507 struct seq_file
*m
= filp
->private_data
;
1514 static const struct file_operations proc_pid_sched_autogroup_operations
= {
1515 .open
= sched_autogroup_open
,
1517 .write
= sched_autogroup_write
,
1518 .llseek
= seq_lseek
,
1519 .release
= single_release
,
1522 #endif /* CONFIG_SCHED_AUTOGROUP */
1524 static ssize_t
comm_write(struct file
*file
, const char __user
*buf
,
1525 size_t count
, loff_t
*offset
)
1527 struct inode
*inode
= file_inode(file
);
1528 struct task_struct
*p
;
1529 char buffer
[TASK_COMM_LEN
];
1530 const size_t maxlen
= sizeof(buffer
) - 1;
1532 memset(buffer
, 0, sizeof(buffer
));
1533 if (copy_from_user(buffer
, buf
, count
> maxlen
? maxlen
: count
))
1536 p
= get_proc_task(inode
);
1540 if (same_thread_group(current
, p
))
1541 set_task_comm(p
, buffer
);
1550 static int comm_show(struct seq_file
*m
, void *v
)
1552 struct inode
*inode
= m
->private;
1553 struct task_struct
*p
;
1555 p
= get_proc_task(inode
);
1560 seq_printf(m
, "%s\n", p
->comm
);
1568 static int comm_open(struct inode
*inode
, struct file
*filp
)
1570 return single_open(filp
, comm_show
, inode
);
1573 static const struct file_operations proc_pid_set_comm_operations
= {
1576 .write
= comm_write
,
1577 .llseek
= seq_lseek
,
1578 .release
= single_release
,
1581 static int proc_exe_link(struct dentry
*dentry
, struct path
*exe_path
)
1583 struct task_struct
*task
;
1584 struct file
*exe_file
;
1586 task
= get_proc_task(d_inode(dentry
));
1589 exe_file
= get_task_exe_file(task
);
1590 put_task_struct(task
);
1592 *exe_path
= exe_file
->f_path
;
1593 path_get(&exe_file
->f_path
);
1600 static const char *proc_pid_get_link(struct dentry
*dentry
,
1601 struct inode
*inode
,
1602 struct delayed_call
*done
)
1605 int error
= -EACCES
;
1608 return ERR_PTR(-ECHILD
);
1610 /* Are we allowed to snoop on the tasks file descriptors? */
1611 if (!proc_fd_access_allowed(inode
))
1614 error
= PROC_I(inode
)->op
.proc_get_link(dentry
, &path
);
1618 nd_jump_link(&path
);
1621 return ERR_PTR(error
);
1624 static int do_proc_readlink(struct path
*path
, char __user
*buffer
, int buflen
)
1626 char *tmp
= (char*)__get_free_page(GFP_TEMPORARY
);
1633 pathname
= d_path(path
, tmp
, PAGE_SIZE
);
1634 len
= PTR_ERR(pathname
);
1635 if (IS_ERR(pathname
))
1637 len
= tmp
+ PAGE_SIZE
- 1 - pathname
;
1641 if (copy_to_user(buffer
, pathname
, len
))
1644 free_page((unsigned long)tmp
);
1648 static int proc_pid_readlink(struct dentry
* dentry
, char __user
* buffer
, int buflen
)
1650 int error
= -EACCES
;
1651 struct inode
*inode
= d_inode(dentry
);
1654 /* Are we allowed to snoop on the tasks file descriptors? */
1655 if (!proc_fd_access_allowed(inode
))
1658 error
= PROC_I(inode
)->op
.proc_get_link(dentry
, &path
);
1662 error
= do_proc_readlink(&path
, buffer
, buflen
);
1668 const struct inode_operations proc_pid_link_inode_operations
= {
1669 .readlink
= proc_pid_readlink
,
1670 .get_link
= proc_pid_get_link
,
1671 .setattr
= proc_setattr
,
1675 /* building an inode */
1677 struct inode
*proc_pid_make_inode(struct super_block
* sb
,
1678 struct task_struct
*task
, umode_t mode
)
1680 struct inode
* inode
;
1681 struct proc_inode
*ei
;
1682 const struct cred
*cred
;
1684 /* We need a new inode */
1686 inode
= new_inode(sb
);
1692 inode
->i_mode
= mode
;
1693 inode
->i_ino
= get_next_ino();
1694 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= current_time(inode
);
1695 inode
->i_op
= &proc_def_inode_operations
;
1698 * grab the reference to task.
1700 ei
->pid
= get_task_pid(task
, PIDTYPE_PID
);
1704 if (task_dumpable(task
)) {
1706 cred
= __task_cred(task
);
1707 inode
->i_uid
= cred
->euid
;
1708 inode
->i_gid
= cred
->egid
;
1711 security_task_to_inode(task
, inode
);
1721 int pid_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
1723 struct inode
*inode
= d_inode(dentry
);
1724 struct task_struct
*task
;
1725 const struct cred
*cred
;
1726 struct pid_namespace
*pid
= dentry
->d_sb
->s_fs_info
;
1728 generic_fillattr(inode
, stat
);
1731 stat
->uid
= GLOBAL_ROOT_UID
;
1732 stat
->gid
= GLOBAL_ROOT_GID
;
1733 task
= pid_task(proc_pid(inode
), PIDTYPE_PID
);
1735 if (!has_pid_permissions(pid
, task
, 2)) {
1738 * This doesn't prevent learning whether PID exists,
1739 * it only makes getattr() consistent with readdir().
1743 if ((inode
->i_mode
== (S_IFDIR
|S_IRUGO
|S_IXUGO
)) ||
1744 task_dumpable(task
)) {
1745 cred
= __task_cred(task
);
1746 stat
->uid
= cred
->euid
;
1747 stat
->gid
= cred
->egid
;
1757 * Exceptional case: normally we are not allowed to unhash a busy
1758 * directory. In this case, however, we can do it - no aliasing problems
1759 * due to the way we treat inodes.
1761 * Rewrite the inode's ownerships here because the owning task may have
1762 * performed a setuid(), etc.
1764 * Before the /proc/pid/status file was created the only way to read
1765 * the effective uid of a /process was to stat /proc/pid. Reading
1766 * /proc/pid/status is slow enough that procps and other packages
1767 * kept stating /proc/pid. To keep the rules in /proc simple I have
1768 * made this apply to all per process world readable and executable
1771 int pid_revalidate(struct dentry
*dentry
, unsigned int flags
)
1773 struct inode
*inode
;
1774 struct task_struct
*task
;
1775 const struct cred
*cred
;
1777 if (flags
& LOOKUP_RCU
)
1780 inode
= d_inode(dentry
);
1781 task
= get_proc_task(inode
);
1784 if ((inode
->i_mode
== (S_IFDIR
|S_IRUGO
|S_IXUGO
)) ||
1785 task_dumpable(task
)) {
1787 cred
= __task_cred(task
);
1788 inode
->i_uid
= cred
->euid
;
1789 inode
->i_gid
= cred
->egid
;
1792 inode
->i_uid
= GLOBAL_ROOT_UID
;
1793 inode
->i_gid
= GLOBAL_ROOT_GID
;
1795 inode
->i_mode
&= ~(S_ISUID
| S_ISGID
);
1796 security_task_to_inode(task
, inode
);
1797 put_task_struct(task
);
1803 static inline bool proc_inode_is_dead(struct inode
*inode
)
1805 return !proc_pid(inode
)->tasks
[PIDTYPE_PID
].first
;
1808 int pid_delete_dentry(const struct dentry
*dentry
)
1810 /* Is the task we represent dead?
1811 * If so, then don't put the dentry on the lru list,
1812 * kill it immediately.
1814 return proc_inode_is_dead(d_inode(dentry
));
1817 const struct dentry_operations pid_dentry_operations
=
1819 .d_revalidate
= pid_revalidate
,
1820 .d_delete
= pid_delete_dentry
,
1826 * Fill a directory entry.
1828 * If possible create the dcache entry and derive our inode number and
1829 * file type from dcache entry.
1831 * Since all of the proc inode numbers are dynamically generated, the inode
1832 * numbers do not exist until the inode is cache. This means creating the
1833 * the dcache entry in readdir is necessary to keep the inode numbers
1834 * reported by readdir in sync with the inode numbers reported
1837 bool proc_fill_cache(struct file
*file
, struct dir_context
*ctx
,
1838 const char *name
, int len
,
1839 instantiate_t instantiate
, struct task_struct
*task
, const void *ptr
)
1841 struct dentry
*child
, *dir
= file
->f_path
.dentry
;
1842 struct qstr qname
= QSTR_INIT(name
, len
);
1843 struct inode
*inode
;
1847 child
= d_hash_and_lookup(dir
, &qname
);
1849 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq
);
1850 child
= d_alloc_parallel(dir
, &qname
, &wq
);
1852 goto end_instantiate
;
1853 if (d_in_lookup(child
)) {
1854 int err
= instantiate(d_inode(dir
), child
, task
, ptr
);
1855 d_lookup_done(child
);
1858 goto end_instantiate
;
1862 inode
= d_inode(child
);
1864 type
= inode
->i_mode
>> 12;
1866 return dir_emit(ctx
, name
, len
, ino
, type
);
1869 return dir_emit(ctx
, name
, len
, 1, DT_UNKNOWN
);
1873 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1874 * which represent vma start and end addresses.
1876 static int dname_to_vma_addr(struct dentry
*dentry
,
1877 unsigned long *start
, unsigned long *end
)
1879 if (sscanf(dentry
->d_name
.name
, "%lx-%lx", start
, end
) != 2)
1885 static int map_files_d_revalidate(struct dentry
*dentry
, unsigned int flags
)
1887 unsigned long vm_start
, vm_end
;
1888 bool exact_vma_exists
= false;
1889 struct mm_struct
*mm
= NULL
;
1890 struct task_struct
*task
;
1891 const struct cred
*cred
;
1892 struct inode
*inode
;
1895 if (flags
& LOOKUP_RCU
)
1898 inode
= d_inode(dentry
);
1899 task
= get_proc_task(inode
);
1903 mm
= mm_access(task
, PTRACE_MODE_READ_FSCREDS
);
1904 if (IS_ERR_OR_NULL(mm
))
1907 if (!dname_to_vma_addr(dentry
, &vm_start
, &vm_end
)) {
1908 down_read(&mm
->mmap_sem
);
1909 exact_vma_exists
= !!find_exact_vma(mm
, vm_start
, vm_end
);
1910 up_read(&mm
->mmap_sem
);
1915 if (exact_vma_exists
) {
1916 if (task_dumpable(task
)) {
1918 cred
= __task_cred(task
);
1919 inode
->i_uid
= cred
->euid
;
1920 inode
->i_gid
= cred
->egid
;
1923 inode
->i_uid
= GLOBAL_ROOT_UID
;
1924 inode
->i_gid
= GLOBAL_ROOT_GID
;
1926 security_task_to_inode(task
, inode
);
1931 put_task_struct(task
);
1937 static const struct dentry_operations tid_map_files_dentry_operations
= {
1938 .d_revalidate
= map_files_d_revalidate
,
1939 .d_delete
= pid_delete_dentry
,
1942 static int map_files_get_link(struct dentry
*dentry
, struct path
*path
)
1944 unsigned long vm_start
, vm_end
;
1945 struct vm_area_struct
*vma
;
1946 struct task_struct
*task
;
1947 struct mm_struct
*mm
;
1951 task
= get_proc_task(d_inode(dentry
));
1955 mm
= get_task_mm(task
);
1956 put_task_struct(task
);
1960 rc
= dname_to_vma_addr(dentry
, &vm_start
, &vm_end
);
1965 down_read(&mm
->mmap_sem
);
1966 vma
= find_exact_vma(mm
, vm_start
, vm_end
);
1967 if (vma
&& vma
->vm_file
) {
1968 *path
= vma_pr_or_file(vma
)->f_path
;
1972 up_read(&mm
->mmap_sem
);
1980 struct map_files_info
{
1983 unsigned char name
[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
1987 * Only allow CAP_SYS_ADMIN to follow the links, due to concerns about how the
1988 * symlinks may be used to bypass permissions on ancestor directories in the
1989 * path to the file in question.
1992 proc_map_files_get_link(struct dentry
*dentry
,
1993 struct inode
*inode
,
1994 struct delayed_call
*done
)
1996 if (!capable(CAP_SYS_ADMIN
))
1997 return ERR_PTR(-EPERM
);
1999 return proc_pid_get_link(dentry
, inode
, done
);
2003 * Identical to proc_pid_link_inode_operations except for get_link()
2005 static const struct inode_operations proc_map_files_link_inode_operations
= {
2006 .readlink
= proc_pid_readlink
,
2007 .get_link
= proc_map_files_get_link
,
2008 .setattr
= proc_setattr
,
2012 proc_map_files_instantiate(struct inode
*dir
, struct dentry
*dentry
,
2013 struct task_struct
*task
, const void *ptr
)
2015 fmode_t mode
= (fmode_t
)(unsigned long)ptr
;
2016 struct proc_inode
*ei
;
2017 struct inode
*inode
;
2019 inode
= proc_pid_make_inode(dir
->i_sb
, task
, S_IFLNK
|
2020 ((mode
& FMODE_READ
) ? S_IRUSR
: 0) |
2021 ((mode
& FMODE_WRITE
) ? S_IWUSR
: 0));
2026 ei
->op
.proc_get_link
= map_files_get_link
;
2028 inode
->i_op
= &proc_map_files_link_inode_operations
;
2031 d_set_d_op(dentry
, &tid_map_files_dentry_operations
);
2032 d_add(dentry
, inode
);
2037 static struct dentry
*proc_map_files_lookup(struct inode
*dir
,
2038 struct dentry
*dentry
, unsigned int flags
)
2040 unsigned long vm_start
, vm_end
;
2041 struct vm_area_struct
*vma
;
2042 struct task_struct
*task
;
2044 struct mm_struct
*mm
;
2047 task
= get_proc_task(dir
);
2052 if (!ptrace_may_access(task
, PTRACE_MODE_READ_FSCREDS
))
2056 if (dname_to_vma_addr(dentry
, &vm_start
, &vm_end
))
2059 mm
= get_task_mm(task
);
2063 down_read(&mm
->mmap_sem
);
2064 vma
= find_exact_vma(mm
, vm_start
, vm_end
);
2069 result
= proc_map_files_instantiate(dir
, dentry
, task
,
2070 (void *)(unsigned long)vma
->vm_file
->f_mode
);
2073 up_read(&mm
->mmap_sem
);
2076 put_task_struct(task
);
2078 return ERR_PTR(result
);
2081 static const struct inode_operations proc_map_files_inode_operations
= {
2082 .lookup
= proc_map_files_lookup
,
2083 .permission
= proc_fd_permission
,
2084 .setattr
= proc_setattr
,
2088 proc_map_files_readdir(struct file
*file
, struct dir_context
*ctx
)
2090 struct vm_area_struct
*vma
;
2091 struct task_struct
*task
;
2092 struct mm_struct
*mm
;
2093 unsigned long nr_files
, pos
, i
;
2094 struct flex_array
*fa
= NULL
;
2095 struct map_files_info info
;
2096 struct map_files_info
*p
;
2100 task
= get_proc_task(file_inode(file
));
2105 if (!ptrace_may_access(task
, PTRACE_MODE_READ_FSCREDS
))
2109 if (!dir_emit_dots(file
, ctx
))
2112 mm
= get_task_mm(task
);
2115 down_read(&mm
->mmap_sem
);
2120 * We need two passes here:
2122 * 1) Collect vmas of mapped files with mmap_sem taken
2123 * 2) Release mmap_sem and instantiate entries
2125 * otherwise we get lockdep complained, since filldir()
2126 * routine might require mmap_sem taken in might_fault().
2129 for (vma
= mm
->mmap
, pos
= 2; vma
; vma
= vma
->vm_next
) {
2130 if (vma
->vm_file
&& ++pos
> ctx
->pos
)
2135 fa
= flex_array_alloc(sizeof(info
), nr_files
,
2137 if (!fa
|| flex_array_prealloc(fa
, 0, nr_files
,
2141 flex_array_free(fa
);
2142 up_read(&mm
->mmap_sem
);
2146 for (i
= 0, vma
= mm
->mmap
, pos
= 2; vma
;
2147 vma
= vma
->vm_next
) {
2150 if (++pos
<= ctx
->pos
)
2153 info
.mode
= vma
->vm_file
->f_mode
;
2154 info
.len
= snprintf(info
.name
,
2155 sizeof(info
.name
), "%lx-%lx",
2156 vma
->vm_start
, vma
->vm_end
);
2157 if (flex_array_put(fa
, i
++, &info
, GFP_KERNEL
))
2161 up_read(&mm
->mmap_sem
);
2163 for (i
= 0; i
< nr_files
; i
++) {
2164 p
= flex_array_get(fa
, i
);
2165 if (!proc_fill_cache(file
, ctx
,
2167 proc_map_files_instantiate
,
2169 (void *)(unsigned long)p
->mode
))
2174 flex_array_free(fa
);
2178 put_task_struct(task
);
2183 static const struct file_operations proc_map_files_operations
= {
2184 .read
= generic_read_dir
,
2185 .iterate_shared
= proc_map_files_readdir
,
2186 .llseek
= generic_file_llseek
,
2189 #ifdef CONFIG_CHECKPOINT_RESTORE
2190 struct timers_private
{
2192 struct task_struct
*task
;
2193 struct sighand_struct
*sighand
;
2194 struct pid_namespace
*ns
;
2195 unsigned long flags
;
2198 static void *timers_start(struct seq_file
*m
, loff_t
*pos
)
2200 struct timers_private
*tp
= m
->private;
2202 tp
->task
= get_pid_task(tp
->pid
, PIDTYPE_PID
);
2204 return ERR_PTR(-ESRCH
);
2206 tp
->sighand
= lock_task_sighand(tp
->task
, &tp
->flags
);
2208 return ERR_PTR(-ESRCH
);
2210 return seq_list_start(&tp
->task
->signal
->posix_timers
, *pos
);
2213 static void *timers_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
2215 struct timers_private
*tp
= m
->private;
2216 return seq_list_next(v
, &tp
->task
->signal
->posix_timers
, pos
);
2219 static void timers_stop(struct seq_file
*m
, void *v
)
2221 struct timers_private
*tp
= m
->private;
2224 unlock_task_sighand(tp
->task
, &tp
->flags
);
2229 put_task_struct(tp
->task
);
2234 static int show_timer(struct seq_file
*m
, void *v
)
2236 struct k_itimer
*timer
;
2237 struct timers_private
*tp
= m
->private;
2239 static const char * const nstr
[] = {
2240 [SIGEV_SIGNAL
] = "signal",
2241 [SIGEV_NONE
] = "none",
2242 [SIGEV_THREAD
] = "thread",
2245 timer
= list_entry((struct list_head
*)v
, struct k_itimer
, list
);
2246 notify
= timer
->it_sigev_notify
;
2248 seq_printf(m
, "ID: %d\n", timer
->it_id
);
2249 seq_printf(m
, "signal: %d/%p\n",
2250 timer
->sigq
->info
.si_signo
,
2251 timer
->sigq
->info
.si_value
.sival_ptr
);
2252 seq_printf(m
, "notify: %s/%s.%d\n",
2253 nstr
[notify
& ~SIGEV_THREAD_ID
],
2254 (notify
& SIGEV_THREAD_ID
) ? "tid" : "pid",
2255 pid_nr_ns(timer
->it_pid
, tp
->ns
));
2256 seq_printf(m
, "ClockID: %d\n", timer
->it_clock
);
2261 static const struct seq_operations proc_timers_seq_ops
= {
2262 .start
= timers_start
,
2263 .next
= timers_next
,
2264 .stop
= timers_stop
,
2268 static int proc_timers_open(struct inode
*inode
, struct file
*file
)
2270 struct timers_private
*tp
;
2272 tp
= __seq_open_private(file
, &proc_timers_seq_ops
,
2273 sizeof(struct timers_private
));
2277 tp
->pid
= proc_pid(inode
);
2278 tp
->ns
= inode
->i_sb
->s_fs_info
;
2282 static const struct file_operations proc_timers_operations
= {
2283 .open
= proc_timers_open
,
2285 .llseek
= seq_lseek
,
2286 .release
= seq_release_private
,
2290 static ssize_t
timerslack_ns_write(struct file
*file
, const char __user
*buf
,
2291 size_t count
, loff_t
*offset
)
2293 struct inode
*inode
= file_inode(file
);
2294 struct task_struct
*p
;
2298 err
= kstrtoull_from_user(buf
, count
, 10, &slack_ns
);
2302 p
= get_proc_task(inode
);
2307 if (!capable(CAP_SYS_NICE
)) {
2312 err
= security_task_setscheduler(p
);
2321 p
->timer_slack_ns
= p
->default_timer_slack_ns
;
2323 p
->timer_slack_ns
= slack_ns
;
2332 static int timerslack_ns_show(struct seq_file
*m
, void *v
)
2334 struct inode
*inode
= m
->private;
2335 struct task_struct
*p
;
2338 p
= get_proc_task(inode
);
2344 if (!capable(CAP_SYS_NICE
)) {
2348 err
= security_task_getscheduler(p
);
2354 seq_printf(m
, "%llu\n", p
->timer_slack_ns
);
2363 static int timerslack_ns_open(struct inode
*inode
, struct file
*filp
)
2365 return single_open(filp
, timerslack_ns_show
, inode
);
2368 static const struct file_operations proc_pid_set_timerslack_ns_operations
= {
2369 .open
= timerslack_ns_open
,
2371 .write
= timerslack_ns_write
,
2372 .llseek
= seq_lseek
,
2373 .release
= single_release
,
2376 static int proc_pident_instantiate(struct inode
*dir
,
2377 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
2379 const struct pid_entry
*p
= ptr
;
2380 struct inode
*inode
;
2381 struct proc_inode
*ei
;
2383 inode
= proc_pid_make_inode(dir
->i_sb
, task
, p
->mode
);
2388 if (S_ISDIR(inode
->i_mode
))
2389 set_nlink(inode
, 2); /* Use getattr to fix if necessary */
2391 inode
->i_op
= p
->iop
;
2393 inode
->i_fop
= p
->fop
;
2395 d_set_d_op(dentry
, &pid_dentry_operations
);
2396 d_add(dentry
, inode
);
2397 /* Close the race of the process dying before we return the dentry */
2398 if (pid_revalidate(dentry
, 0))
2404 static struct dentry
*proc_pident_lookup(struct inode
*dir
,
2405 struct dentry
*dentry
,
2406 const struct pid_entry
*ents
,
2410 struct task_struct
*task
= get_proc_task(dir
);
2411 const struct pid_entry
*p
, *last
;
2419 * Yes, it does not scale. And it should not. Don't add
2420 * new entries into /proc/<tgid>/ without very good reasons.
2422 last
= &ents
[nents
];
2423 for (p
= ents
; p
< last
; p
++) {
2424 if (p
->len
!= dentry
->d_name
.len
)
2426 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
))
2432 error
= proc_pident_instantiate(dir
, dentry
, task
, p
);
2434 put_task_struct(task
);
2436 return ERR_PTR(error
);
2439 static int proc_pident_readdir(struct file
*file
, struct dir_context
*ctx
,
2440 const struct pid_entry
*ents
, unsigned int nents
)
2442 struct task_struct
*task
= get_proc_task(file_inode(file
));
2443 const struct pid_entry
*p
;
2448 if (!dir_emit_dots(file
, ctx
))
2451 if (ctx
->pos
>= nents
+ 2)
2454 for (p
= ents
+ (ctx
->pos
- 2); p
< ents
+ nents
; p
++) {
2455 if (!proc_fill_cache(file
, ctx
, p
->name
, p
->len
,
2456 proc_pident_instantiate
, task
, p
))
2461 put_task_struct(task
);
2465 #ifdef CONFIG_SECURITY
2466 static ssize_t
proc_pid_attr_read(struct file
* file
, char __user
* buf
,
2467 size_t count
, loff_t
*ppos
)
2469 struct inode
* inode
= file_inode(file
);
2472 struct task_struct
*task
= get_proc_task(inode
);
2477 length
= security_getprocattr(task
,
2478 (char*)file
->f_path
.dentry
->d_name
.name
,
2480 put_task_struct(task
);
2482 length
= simple_read_from_buffer(buf
, count
, ppos
, p
, length
);
2487 static ssize_t
proc_pid_attr_write(struct file
* file
, const char __user
* buf
,
2488 size_t count
, loff_t
*ppos
)
2490 struct inode
* inode
= file_inode(file
);
2493 struct task_struct
*task
= get_proc_task(inode
);
2498 if (count
> PAGE_SIZE
)
2501 /* No partial writes. */
2506 page
= memdup_user(buf
, count
);
2508 length
= PTR_ERR(page
);
2512 /* Guard against adverse ptrace interaction */
2513 length
= mutex_lock_interruptible(&task
->signal
->cred_guard_mutex
);
2517 length
= security_setprocattr(task
,
2518 (char*)file
->f_path
.dentry
->d_name
.name
,
2520 mutex_unlock(&task
->signal
->cred_guard_mutex
);
2524 put_task_struct(task
);
2529 static const struct file_operations proc_pid_attr_operations
= {
2530 .read
= proc_pid_attr_read
,
2531 .write
= proc_pid_attr_write
,
2532 .llseek
= generic_file_llseek
,
2535 static const struct pid_entry attr_dir_stuff
[] = {
2536 REG("current", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2537 REG("prev", S_IRUGO
, proc_pid_attr_operations
),
2538 REG("exec", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2539 REG("fscreate", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2540 REG("keycreate", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2541 REG("sockcreate", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2544 static int proc_attr_dir_readdir(struct file
*file
, struct dir_context
*ctx
)
2546 return proc_pident_readdir(file
, ctx
,
2547 attr_dir_stuff
, ARRAY_SIZE(attr_dir_stuff
));
2550 static const struct file_operations proc_attr_dir_operations
= {
2551 .read
= generic_read_dir
,
2552 .iterate_shared
= proc_attr_dir_readdir
,
2553 .llseek
= generic_file_llseek
,
2556 static struct dentry
*proc_attr_dir_lookup(struct inode
*dir
,
2557 struct dentry
*dentry
, unsigned int flags
)
2559 return proc_pident_lookup(dir
, dentry
,
2560 attr_dir_stuff
, ARRAY_SIZE(attr_dir_stuff
));
2563 static const struct inode_operations proc_attr_dir_inode_operations
= {
2564 .lookup
= proc_attr_dir_lookup
,
2565 .getattr
= pid_getattr
,
2566 .setattr
= proc_setattr
,
2571 #ifdef CONFIG_ELF_CORE
2572 static ssize_t
proc_coredump_filter_read(struct file
*file
, char __user
*buf
,
2573 size_t count
, loff_t
*ppos
)
2575 struct task_struct
*task
= get_proc_task(file_inode(file
));
2576 struct mm_struct
*mm
;
2577 char buffer
[PROC_NUMBUF
];
2585 mm
= get_task_mm(task
);
2587 len
= snprintf(buffer
, sizeof(buffer
), "%08lx\n",
2588 ((mm
->flags
& MMF_DUMP_FILTER_MASK
) >>
2589 MMF_DUMP_FILTER_SHIFT
));
2591 ret
= simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
2594 put_task_struct(task
);
2599 static ssize_t
proc_coredump_filter_write(struct file
*file
,
2600 const char __user
*buf
,
2604 struct task_struct
*task
;
2605 struct mm_struct
*mm
;
2611 ret
= kstrtouint_from_user(buf
, count
, 0, &val
);
2616 task
= get_proc_task(file_inode(file
));
2620 mm
= get_task_mm(task
);
2625 for (i
= 0, mask
= 1; i
< MMF_DUMP_FILTER_BITS
; i
++, mask
<<= 1) {
2627 set_bit(i
+ MMF_DUMP_FILTER_SHIFT
, &mm
->flags
);
2629 clear_bit(i
+ MMF_DUMP_FILTER_SHIFT
, &mm
->flags
);
2634 put_task_struct(task
);
2641 static const struct file_operations proc_coredump_filter_operations
= {
2642 .read
= proc_coredump_filter_read
,
2643 .write
= proc_coredump_filter_write
,
2644 .llseek
= generic_file_llseek
,
2648 #ifdef CONFIG_TASK_IO_ACCOUNTING
2649 static int do_io_accounting(struct task_struct
*task
, struct seq_file
*m
, int whole
)
2651 struct task_io_accounting acct
= task
->ioac
;
2652 unsigned long flags
;
2655 result
= mutex_lock_killable(&task
->signal
->cred_guard_mutex
);
2659 if (!ptrace_may_access(task
, PTRACE_MODE_READ_FSCREDS
)) {
2664 if (whole
&& lock_task_sighand(task
, &flags
)) {
2665 struct task_struct
*t
= task
;
2667 task_io_accounting_add(&acct
, &task
->signal
->ioac
);
2668 while_each_thread(task
, t
)
2669 task_io_accounting_add(&acct
, &t
->ioac
);
2671 unlock_task_sighand(task
, &flags
);
2678 "read_bytes: %llu\n"
2679 "write_bytes: %llu\n"
2680 "cancelled_write_bytes: %llu\n",
2681 (unsigned long long)acct
.rchar
,
2682 (unsigned long long)acct
.wchar
,
2683 (unsigned long long)acct
.syscr
,
2684 (unsigned long long)acct
.syscw
,
2685 (unsigned long long)acct
.read_bytes
,
2686 (unsigned long long)acct
.write_bytes
,
2687 (unsigned long long)acct
.cancelled_write_bytes
);
2691 mutex_unlock(&task
->signal
->cred_guard_mutex
);
2695 static int proc_tid_io_accounting(struct seq_file
*m
, struct pid_namespace
*ns
,
2696 struct pid
*pid
, struct task_struct
*task
)
2698 return do_io_accounting(task
, m
, 0);
2701 static int proc_tgid_io_accounting(struct seq_file
*m
, struct pid_namespace
*ns
,
2702 struct pid
*pid
, struct task_struct
*task
)
2704 return do_io_accounting(task
, m
, 1);
2706 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2708 #ifdef CONFIG_USER_NS
2709 static int proc_id_map_open(struct inode
*inode
, struct file
*file
,
2710 const struct seq_operations
*seq_ops
)
2712 struct user_namespace
*ns
= NULL
;
2713 struct task_struct
*task
;
2714 struct seq_file
*seq
;
2717 task
= get_proc_task(inode
);
2720 ns
= get_user_ns(task_cred_xxx(task
, user_ns
));
2722 put_task_struct(task
);
2727 ret
= seq_open(file
, seq_ops
);
2731 seq
= file
->private_data
;
2741 static int proc_id_map_release(struct inode
*inode
, struct file
*file
)
2743 struct seq_file
*seq
= file
->private_data
;
2744 struct user_namespace
*ns
= seq
->private;
2746 return seq_release(inode
, file
);
2749 static int proc_uid_map_open(struct inode
*inode
, struct file
*file
)
2751 return proc_id_map_open(inode
, file
, &proc_uid_seq_operations
);
2754 static int proc_gid_map_open(struct inode
*inode
, struct file
*file
)
2756 return proc_id_map_open(inode
, file
, &proc_gid_seq_operations
);
2759 static int proc_projid_map_open(struct inode
*inode
, struct file
*file
)
2761 return proc_id_map_open(inode
, file
, &proc_projid_seq_operations
);
2764 static const struct file_operations proc_uid_map_operations
= {
2765 .open
= proc_uid_map_open
,
2766 .write
= proc_uid_map_write
,
2768 .llseek
= seq_lseek
,
2769 .release
= proc_id_map_release
,
2772 static const struct file_operations proc_gid_map_operations
= {
2773 .open
= proc_gid_map_open
,
2774 .write
= proc_gid_map_write
,
2776 .llseek
= seq_lseek
,
2777 .release
= proc_id_map_release
,
2780 static const struct file_operations proc_projid_map_operations
= {
2781 .open
= proc_projid_map_open
,
2782 .write
= proc_projid_map_write
,
2784 .llseek
= seq_lseek
,
2785 .release
= proc_id_map_release
,
2788 static int proc_setgroups_open(struct inode
*inode
, struct file
*file
)
2790 struct user_namespace
*ns
= NULL
;
2791 struct task_struct
*task
;
2795 task
= get_proc_task(inode
);
2798 ns
= get_user_ns(task_cred_xxx(task
, user_ns
));
2800 put_task_struct(task
);
2805 if (file
->f_mode
& FMODE_WRITE
) {
2807 if (!ns_capable(ns
, CAP_SYS_ADMIN
))
2811 ret
= single_open(file
, &proc_setgroups_show
, ns
);
2822 static int proc_setgroups_release(struct inode
*inode
, struct file
*file
)
2824 struct seq_file
*seq
= file
->private_data
;
2825 struct user_namespace
*ns
= seq
->private;
2826 int ret
= single_release(inode
, file
);
2831 static const struct file_operations proc_setgroups_operations
= {
2832 .open
= proc_setgroups_open
,
2833 .write
= proc_setgroups_write
,
2835 .llseek
= seq_lseek
,
2836 .release
= proc_setgroups_release
,
2838 #endif /* CONFIG_USER_NS */
2840 static int proc_pid_personality(struct seq_file
*m
, struct pid_namespace
*ns
,
2841 struct pid
*pid
, struct task_struct
*task
)
2843 int err
= lock_trace(task
);
2845 seq_printf(m
, "%08x\n", task
->personality
);
2854 static const struct file_operations proc_task_operations
;
2855 static const struct inode_operations proc_task_inode_operations
;
2857 static const struct pid_entry tgid_base_stuff
[] = {
2858 DIR("task", S_IRUGO
|S_IXUGO
, proc_task_inode_operations
, proc_task_operations
),
2859 DIR("fd", S_IRUSR
|S_IXUSR
, proc_fd_inode_operations
, proc_fd_operations
),
2860 DIR("map_files", S_IRUSR
|S_IXUSR
, proc_map_files_inode_operations
, proc_map_files_operations
),
2861 DIR("fdinfo", S_IRUSR
|S_IXUSR
, proc_fdinfo_inode_operations
, proc_fdinfo_operations
),
2862 DIR("ns", S_IRUSR
|S_IXUGO
, proc_ns_dir_inode_operations
, proc_ns_dir_operations
),
2864 DIR("net", S_IRUGO
|S_IXUGO
, proc_net_inode_operations
, proc_net_operations
),
2866 REG("environ", S_IRUSR
, proc_environ_operations
),
2867 REG("auxv", S_IRUSR
, proc_auxv_operations
),
2868 ONE("status", S_IRUGO
, proc_pid_status
),
2869 ONE("personality", S_IRUSR
, proc_pid_personality
),
2870 ONE("limits", S_IRUGO
, proc_pid_limits
),
2871 #ifdef CONFIG_SCHED_DEBUG
2872 REG("sched", S_IRUGO
|S_IWUSR
, proc_pid_sched_operations
),
2874 #ifdef CONFIG_SCHED_AUTOGROUP
2875 REG("autogroup", S_IRUGO
|S_IWUSR
, proc_pid_sched_autogroup_operations
),
2877 REG("comm", S_IRUGO
|S_IWUSR
, proc_pid_set_comm_operations
),
2878 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2879 ONE("syscall", S_IRUSR
, proc_pid_syscall
),
2881 REG("cmdline", S_IRUGO
, proc_pid_cmdline_ops
),
2882 ONE("stat", S_IRUGO
, proc_tgid_stat
),
2883 ONE("statm", S_IRUGO
, proc_pid_statm
),
2884 REG("maps", S_IRUGO
, proc_pid_maps_operations
),
2886 REG("numa_maps", S_IRUGO
, proc_pid_numa_maps_operations
),
2888 REG("mem", S_IRUSR
|S_IWUSR
, proc_mem_operations
),
2889 LNK("cwd", proc_cwd_link
),
2890 LNK("root", proc_root_link
),
2891 LNK("exe", proc_exe_link
),
2892 REG("mounts", S_IRUGO
, proc_mounts_operations
),
2893 REG("mountinfo", S_IRUGO
, proc_mountinfo_operations
),
2894 REG("mountstats", S_IRUSR
, proc_mountstats_operations
),
2895 #ifdef CONFIG_PROC_PAGE_MONITOR
2896 REG("clear_refs", S_IWUSR
, proc_clear_refs_operations
),
2897 REG("smaps", S_IRUGO
, proc_pid_smaps_operations
),
2898 REG("pagemap", S_IRUSR
, proc_pagemap_operations
),
2900 #ifdef CONFIG_SECURITY
2901 DIR("attr", S_IRUGO
|S_IXUGO
, proc_attr_dir_inode_operations
, proc_attr_dir_operations
),
2903 #ifdef CONFIG_KALLSYMS
2904 ONE("wchan", S_IRUGO
, proc_pid_wchan
),
2906 #ifdef CONFIG_STACKTRACE
2907 ONE("stack", S_IRUSR
, proc_pid_stack
),
2909 #ifdef CONFIG_SCHED_INFO
2910 ONE("schedstat", S_IRUGO
, proc_pid_schedstat
),
2912 #ifdef CONFIG_LATENCYTOP
2913 REG("latency", S_IRUGO
, proc_lstats_operations
),
2915 #ifdef CONFIG_PROC_PID_CPUSET
2916 ONE("cpuset", S_IRUGO
, proc_cpuset_show
),
2918 #ifdef CONFIG_CGROUPS
2919 ONE("cgroup", S_IRUGO
, proc_cgroup_show
),
2921 ONE("oom_score", S_IRUGO
, proc_oom_score
),
2922 REG("oom_adj", S_IRUGO
|S_IWUSR
, proc_oom_adj_operations
),
2923 REG("oom_score_adj", S_IRUGO
|S_IWUSR
, proc_oom_score_adj_operations
),
2924 #ifdef CONFIG_AUDITSYSCALL
2925 REG("loginuid", S_IWUSR
|S_IRUGO
, proc_loginuid_operations
),
2926 REG("sessionid", S_IRUGO
, proc_sessionid_operations
),
2928 #ifdef CONFIG_FAULT_INJECTION
2929 REG("make-it-fail", S_IRUGO
|S_IWUSR
, proc_fault_inject_operations
),
2931 #ifdef CONFIG_ELF_CORE
2932 REG("coredump_filter", S_IRUGO
|S_IWUSR
, proc_coredump_filter_operations
),
2934 #ifdef CONFIG_TASK_IO_ACCOUNTING
2935 ONE("io", S_IRUSR
, proc_tgid_io_accounting
),
2937 #ifdef CONFIG_HARDWALL
2938 ONE("hardwall", S_IRUGO
, proc_pid_hardwall
),
2940 #ifdef CONFIG_USER_NS
2941 REG("uid_map", S_IRUGO
|S_IWUSR
, proc_uid_map_operations
),
2942 REG("gid_map", S_IRUGO
|S_IWUSR
, proc_gid_map_operations
),
2943 REG("projid_map", S_IRUGO
|S_IWUSR
, proc_projid_map_operations
),
2944 REG("setgroups", S_IRUGO
|S_IWUSR
, proc_setgroups_operations
),
2946 #ifdef CONFIG_CHECKPOINT_RESTORE
2947 REG("timers", S_IRUGO
, proc_timers_operations
),
2949 REG("timerslack_ns", S_IRUGO
|S_IWUGO
, proc_pid_set_timerslack_ns_operations
),
2952 static int proc_tgid_base_readdir(struct file
*file
, struct dir_context
*ctx
)
2954 return proc_pident_readdir(file
, ctx
,
2955 tgid_base_stuff
, ARRAY_SIZE(tgid_base_stuff
));
2958 static const struct file_operations proc_tgid_base_operations
= {
2959 .read
= generic_read_dir
,
2960 .iterate_shared
= proc_tgid_base_readdir
,
2961 .llseek
= generic_file_llseek
,
2964 static struct dentry
*proc_tgid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, unsigned int flags
)
2966 return proc_pident_lookup(dir
, dentry
,
2967 tgid_base_stuff
, ARRAY_SIZE(tgid_base_stuff
));
2970 static const struct inode_operations proc_tgid_base_inode_operations
= {
2971 .lookup
= proc_tgid_base_lookup
,
2972 .getattr
= pid_getattr
,
2973 .setattr
= proc_setattr
,
2974 .permission
= proc_pid_permission
,
2977 static void proc_flush_task_mnt(struct vfsmount
*mnt
, pid_t pid
, pid_t tgid
)
2979 struct dentry
*dentry
, *leader
, *dir
;
2980 char buf
[PROC_NUMBUF
];
2984 name
.len
= snprintf(buf
, sizeof(buf
), "%d", pid
);
2985 /* no ->d_hash() rejects on procfs */
2986 dentry
= d_hash_and_lookup(mnt
->mnt_root
, &name
);
2988 d_invalidate(dentry
);
2996 name
.len
= snprintf(buf
, sizeof(buf
), "%d", tgid
);
2997 leader
= d_hash_and_lookup(mnt
->mnt_root
, &name
);
3002 name
.len
= strlen(name
.name
);
3003 dir
= d_hash_and_lookup(leader
, &name
);
3005 goto out_put_leader
;
3008 name
.len
= snprintf(buf
, sizeof(buf
), "%d", pid
);
3009 dentry
= d_hash_and_lookup(dir
, &name
);
3011 d_invalidate(dentry
);
3023 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
3024 * @task: task that should be flushed.
3026 * When flushing dentries from proc, one needs to flush them from global
3027 * proc (proc_mnt) and from all the namespaces' procs this task was seen
3028 * in. This call is supposed to do all of this job.
3030 * Looks in the dcache for
3032 * /proc/@tgid/task/@pid
3033 * if either directory is present flushes it and all of it'ts children
3036 * It is safe and reasonable to cache /proc entries for a task until
3037 * that task exits. After that they just clog up the dcache with
3038 * useless entries, possibly causing useful dcache entries to be
3039 * flushed instead. This routine is proved to flush those useless
3040 * dcache entries at process exit time.
3042 * NOTE: This routine is just an optimization so it does not guarantee
3043 * that no dcache entries will exist at process exit time it
3044 * just makes it very unlikely that any will persist.
3047 void proc_flush_task(struct task_struct
*task
)
3050 struct pid
*pid
, *tgid
;
3053 pid
= task_pid(task
);
3054 tgid
= task_tgid(task
);
3056 for (i
= 0; i
<= pid
->level
; i
++) {
3057 upid
= &pid
->numbers
[i
];
3058 proc_flush_task_mnt(upid
->ns
->proc_mnt
, upid
->nr
,
3059 tgid
->numbers
[i
].nr
);
3063 static int proc_pid_instantiate(struct inode
*dir
,
3064 struct dentry
* dentry
,
3065 struct task_struct
*task
, const void *ptr
)
3067 struct inode
*inode
;
3069 inode
= proc_pid_make_inode(dir
->i_sb
, task
, S_IFDIR
| S_IRUGO
| S_IXUGO
);
3073 inode
->i_op
= &proc_tgid_base_inode_operations
;
3074 inode
->i_fop
= &proc_tgid_base_operations
;
3075 inode
->i_flags
|=S_IMMUTABLE
;
3077 set_nlink(inode
, nlink_tgid
);
3079 d_set_d_op(dentry
, &pid_dentry_operations
);
3081 d_add(dentry
, inode
);
3082 /* Close the race of the process dying before we return the dentry */
3083 if (pid_revalidate(dentry
, 0))
3089 struct dentry
*proc_pid_lookup(struct inode
*dir
, struct dentry
* dentry
, unsigned int flags
)
3091 int result
= -ENOENT
;
3092 struct task_struct
*task
;
3094 struct pid_namespace
*ns
;
3096 tgid
= name_to_int(&dentry
->d_name
);
3100 ns
= dentry
->d_sb
->s_fs_info
;
3102 task
= find_task_by_pid_ns(tgid
, ns
);
3104 get_task_struct(task
);
3109 result
= proc_pid_instantiate(dir
, dentry
, task
, NULL
);
3110 put_task_struct(task
);
3112 return ERR_PTR(result
);
3116 * Find the first task with tgid >= tgid
3121 struct task_struct
*task
;
3123 static struct tgid_iter
next_tgid(struct pid_namespace
*ns
, struct tgid_iter iter
)
3128 put_task_struct(iter
.task
);
3132 pid
= find_ge_pid(iter
.tgid
, ns
);
3134 iter
.tgid
= pid_nr_ns(pid
, ns
);
3135 iter
.task
= pid_task(pid
, PIDTYPE_PID
);
3136 /* What we to know is if the pid we have find is the
3137 * pid of a thread_group_leader. Testing for task
3138 * being a thread_group_leader is the obvious thing
3139 * todo but there is a window when it fails, due to
3140 * the pid transfer logic in de_thread.
3142 * So we perform the straight forward test of seeing
3143 * if the pid we have found is the pid of a thread
3144 * group leader, and don't worry if the task we have
3145 * found doesn't happen to be a thread group leader.
3146 * As we don't care in the case of readdir.
3148 if (!iter
.task
|| !has_group_leader_pid(iter
.task
)) {
3152 get_task_struct(iter
.task
);
3158 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
3160 /* for the /proc/ directory itself, after non-process stuff has been done */
3161 int proc_pid_readdir(struct file
*file
, struct dir_context
*ctx
)
3163 struct tgid_iter iter
;
3164 struct pid_namespace
*ns
= file_inode(file
)->i_sb
->s_fs_info
;
3165 loff_t pos
= ctx
->pos
;
3167 if (pos
>= PID_MAX_LIMIT
+ TGID_OFFSET
)
3170 if (pos
== TGID_OFFSET
- 2) {
3171 struct inode
*inode
= d_inode(ns
->proc_self
);
3172 if (!dir_emit(ctx
, "self", 4, inode
->i_ino
, DT_LNK
))
3174 ctx
->pos
= pos
= pos
+ 1;
3176 if (pos
== TGID_OFFSET
- 1) {
3177 struct inode
*inode
= d_inode(ns
->proc_thread_self
);
3178 if (!dir_emit(ctx
, "thread-self", 11, inode
->i_ino
, DT_LNK
))
3180 ctx
->pos
= pos
= pos
+ 1;
3182 iter
.tgid
= pos
- TGID_OFFSET
;
3184 for (iter
= next_tgid(ns
, iter
);
3186 iter
.tgid
+= 1, iter
= next_tgid(ns
, iter
)) {
3187 char name
[PROC_NUMBUF
];
3191 if (!has_pid_permissions(ns
, iter
.task
, 2))
3194 len
= snprintf(name
, sizeof(name
), "%d", iter
.tgid
);
3195 ctx
->pos
= iter
.tgid
+ TGID_OFFSET
;
3196 if (!proc_fill_cache(file
, ctx
, name
, len
,
3197 proc_pid_instantiate
, iter
.task
, NULL
)) {
3198 put_task_struct(iter
.task
);
3202 ctx
->pos
= PID_MAX_LIMIT
+ TGID_OFFSET
;
3207 * proc_tid_comm_permission is a special permission function exclusively
3208 * used for the node /proc/<pid>/task/<tid>/comm.
3209 * It bypasses generic permission checks in the case where a task of the same
3210 * task group attempts to access the node.
3211 * The rationale behind this is that glibc and bionic access this node for
3212 * cross thread naming (pthread_set/getname_np(!self)). However, if
3213 * PR_SET_DUMPABLE gets set to 0 this node among others becomes uid=0 gid=0,
3214 * which locks out the cross thread naming implementation.
3215 * This function makes sure that the node is always accessible for members of
3216 * same thread group.
3218 static int proc_tid_comm_permission(struct inode
*inode
, int mask
)
3220 bool is_same_tgroup
;
3221 struct task_struct
*task
;
3223 task
= get_proc_task(inode
);
3226 is_same_tgroup
= same_thread_group(current
, task
);
3227 put_task_struct(task
);
3229 if (likely(is_same_tgroup
&& !(mask
& MAY_EXEC
))) {
3230 /* This file (/proc/<pid>/task/<tid>/comm) can always be
3231 * read or written by the members of the corresponding
3237 return generic_permission(inode
, mask
);
3240 static const struct inode_operations proc_tid_comm_inode_operations
= {
3241 .permission
= proc_tid_comm_permission
,
3247 static const struct pid_entry tid_base_stuff
[] = {
3248 DIR("fd", S_IRUSR
|S_IXUSR
, proc_fd_inode_operations
, proc_fd_operations
),
3249 DIR("fdinfo", S_IRUSR
|S_IXUSR
, proc_fdinfo_inode_operations
, proc_fdinfo_operations
),
3250 DIR("ns", S_IRUSR
|S_IXUGO
, proc_ns_dir_inode_operations
, proc_ns_dir_operations
),
3252 DIR("net", S_IRUGO
|S_IXUGO
, proc_net_inode_operations
, proc_net_operations
),
3254 REG("environ", S_IRUSR
, proc_environ_operations
),
3255 REG("auxv", S_IRUSR
, proc_auxv_operations
),
3256 ONE("status", S_IRUGO
, proc_pid_status
),
3257 ONE("personality", S_IRUSR
, proc_pid_personality
),
3258 ONE("limits", S_IRUGO
, proc_pid_limits
),
3259 #ifdef CONFIG_SCHED_DEBUG
3260 REG("sched", S_IRUGO
|S_IWUSR
, proc_pid_sched_operations
),
3262 NOD("comm", S_IFREG
|S_IRUGO
|S_IWUSR
,
3263 &proc_tid_comm_inode_operations
,
3264 &proc_pid_set_comm_operations
, {}),
3265 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3266 ONE("syscall", S_IRUSR
, proc_pid_syscall
),
3268 REG("cmdline", S_IRUGO
, proc_pid_cmdline_ops
),
3269 ONE("stat", S_IRUGO
, proc_tid_stat
),
3270 ONE("statm", S_IRUGO
, proc_pid_statm
),
3271 REG("maps", S_IRUGO
, proc_tid_maps_operations
),
3272 #ifdef CONFIG_PROC_CHILDREN
3273 REG("children", S_IRUGO
, proc_tid_children_operations
),
3276 REG("numa_maps", S_IRUGO
, proc_tid_numa_maps_operations
),
3278 REG("mem", S_IRUSR
|S_IWUSR
, proc_mem_operations
),
3279 LNK("cwd", proc_cwd_link
),
3280 LNK("root", proc_root_link
),
3281 LNK("exe", proc_exe_link
),
3282 REG("mounts", S_IRUGO
, proc_mounts_operations
),
3283 REG("mountinfo", S_IRUGO
, proc_mountinfo_operations
),
3284 #ifdef CONFIG_PROC_PAGE_MONITOR
3285 REG("clear_refs", S_IWUSR
, proc_clear_refs_operations
),
3286 REG("smaps", S_IRUGO
, proc_tid_smaps_operations
),
3287 REG("pagemap", S_IRUSR
, proc_pagemap_operations
),
3289 #ifdef CONFIG_SECURITY
3290 DIR("attr", S_IRUGO
|S_IXUGO
, proc_attr_dir_inode_operations
, proc_attr_dir_operations
),
3292 #ifdef CONFIG_KALLSYMS
3293 ONE("wchan", S_IRUGO
, proc_pid_wchan
),
3295 #ifdef CONFIG_STACKTRACE
3296 ONE("stack", S_IRUSR
, proc_pid_stack
),
3298 #ifdef CONFIG_SCHED_INFO
3299 ONE("schedstat", S_IRUGO
, proc_pid_schedstat
),
3301 #ifdef CONFIG_LATENCYTOP
3302 REG("latency", S_IRUGO
, proc_lstats_operations
),
3304 #ifdef CONFIG_PROC_PID_CPUSET
3305 ONE("cpuset", S_IRUGO
, proc_cpuset_show
),
3307 #ifdef CONFIG_CGROUPS
3308 ONE("cgroup", S_IRUGO
, proc_cgroup_show
),
3310 ONE("oom_score", S_IRUGO
, proc_oom_score
),
3311 REG("oom_adj", S_IRUGO
|S_IWUSR
, proc_oom_adj_operations
),
3312 REG("oom_score_adj", S_IRUGO
|S_IWUSR
, proc_oom_score_adj_operations
),
3313 #ifdef CONFIG_AUDITSYSCALL
3314 REG("loginuid", S_IWUSR
|S_IRUGO
, proc_loginuid_operations
),
3315 REG("sessionid", S_IRUGO
, proc_sessionid_operations
),
3317 #ifdef CONFIG_FAULT_INJECTION
3318 REG("make-it-fail", S_IRUGO
|S_IWUSR
, proc_fault_inject_operations
),
3320 #ifdef CONFIG_TASK_IO_ACCOUNTING
3321 ONE("io", S_IRUSR
, proc_tid_io_accounting
),
3323 #ifdef CONFIG_HARDWALL
3324 ONE("hardwall", S_IRUGO
, proc_pid_hardwall
),
3326 #ifdef CONFIG_USER_NS
3327 REG("uid_map", S_IRUGO
|S_IWUSR
, proc_uid_map_operations
),
3328 REG("gid_map", S_IRUGO
|S_IWUSR
, proc_gid_map_operations
),
3329 REG("projid_map", S_IRUGO
|S_IWUSR
, proc_projid_map_operations
),
3330 REG("setgroups", S_IRUGO
|S_IWUSR
, proc_setgroups_operations
),
3334 static int proc_tid_base_readdir(struct file
*file
, struct dir_context
*ctx
)
3336 return proc_pident_readdir(file
, ctx
,
3337 tid_base_stuff
, ARRAY_SIZE(tid_base_stuff
));
3340 static struct dentry
*proc_tid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, unsigned int flags
)
3342 return proc_pident_lookup(dir
, dentry
,
3343 tid_base_stuff
, ARRAY_SIZE(tid_base_stuff
));
3346 static const struct file_operations proc_tid_base_operations
= {
3347 .read
= generic_read_dir
,
3348 .iterate_shared
= proc_tid_base_readdir
,
3349 .llseek
= generic_file_llseek
,
3352 static const struct inode_operations proc_tid_base_inode_operations
= {
3353 .lookup
= proc_tid_base_lookup
,
3354 .getattr
= pid_getattr
,
3355 .setattr
= proc_setattr
,
3358 static int proc_task_instantiate(struct inode
*dir
,
3359 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
3361 struct inode
*inode
;
3362 inode
= proc_pid_make_inode(dir
->i_sb
, task
, S_IFDIR
| S_IRUGO
| S_IXUGO
);
3366 inode
->i_op
= &proc_tid_base_inode_operations
;
3367 inode
->i_fop
= &proc_tid_base_operations
;
3368 inode
->i_flags
|=S_IMMUTABLE
;
3370 set_nlink(inode
, nlink_tid
);
3372 d_set_d_op(dentry
, &pid_dentry_operations
);
3374 d_add(dentry
, inode
);
3375 /* Close the race of the process dying before we return the dentry */
3376 if (pid_revalidate(dentry
, 0))
3382 static struct dentry
*proc_task_lookup(struct inode
*dir
, struct dentry
* dentry
, unsigned int flags
)
3384 int result
= -ENOENT
;
3385 struct task_struct
*task
;
3386 struct task_struct
*leader
= get_proc_task(dir
);
3388 struct pid_namespace
*ns
;
3393 tid
= name_to_int(&dentry
->d_name
);
3397 ns
= dentry
->d_sb
->s_fs_info
;
3399 task
= find_task_by_pid_ns(tid
, ns
);
3401 get_task_struct(task
);
3405 if (!same_thread_group(leader
, task
))
3408 result
= proc_task_instantiate(dir
, dentry
, task
, NULL
);
3410 put_task_struct(task
);
3412 put_task_struct(leader
);
3414 return ERR_PTR(result
);
3418 * Find the first tid of a thread group to return to user space.
3420 * Usually this is just the thread group leader, but if the users
3421 * buffer was too small or there was a seek into the middle of the
3422 * directory we have more work todo.
3424 * In the case of a short read we start with find_task_by_pid.
3426 * In the case of a seek we start with the leader and walk nr
3429 static struct task_struct
*first_tid(struct pid
*pid
, int tid
, loff_t f_pos
,
3430 struct pid_namespace
*ns
)
3432 struct task_struct
*pos
, *task
;
3433 unsigned long nr
= f_pos
;
3435 if (nr
!= f_pos
) /* 32bit overflow? */
3439 task
= pid_task(pid
, PIDTYPE_PID
);
3443 /* Attempt to start with the tid of a thread */
3445 pos
= find_task_by_pid_ns(tid
, ns
);
3446 if (pos
&& same_thread_group(pos
, task
))
3450 /* If nr exceeds the number of threads there is nothing todo */
3451 if (nr
>= get_nr_threads(task
))
3454 /* If we haven't found our starting place yet start
3455 * with the leader and walk nr threads forward.
3457 pos
= task
= task
->group_leader
;
3461 } while_each_thread(task
, pos
);
3466 get_task_struct(pos
);
3473 * Find the next thread in the thread list.
3474 * Return NULL if there is an error or no next thread.
3476 * The reference to the input task_struct is released.
3478 static struct task_struct
*next_tid(struct task_struct
*start
)
3480 struct task_struct
*pos
= NULL
;
3482 if (pid_alive(start
)) {
3483 pos
= next_thread(start
);
3484 if (thread_group_leader(pos
))
3487 get_task_struct(pos
);
3490 put_task_struct(start
);
3494 /* for the /proc/TGID/task/ directories */
3495 static int proc_task_readdir(struct file
*file
, struct dir_context
*ctx
)
3497 struct inode
*inode
= file_inode(file
);
3498 struct task_struct
*task
;
3499 struct pid_namespace
*ns
;
3502 if (proc_inode_is_dead(inode
))
3505 if (!dir_emit_dots(file
, ctx
))
3508 /* f_version caches the tgid value that the last readdir call couldn't
3509 * return. lseek aka telldir automagically resets f_version to 0.
3511 ns
= inode
->i_sb
->s_fs_info
;
3512 tid
= (int)file
->f_version
;
3513 file
->f_version
= 0;
3514 for (task
= first_tid(proc_pid(inode
), tid
, ctx
->pos
- 2, ns
);
3516 task
= next_tid(task
), ctx
->pos
++) {
3517 char name
[PROC_NUMBUF
];
3519 tid
= task_pid_nr_ns(task
, ns
);
3520 len
= snprintf(name
, sizeof(name
), "%d", tid
);
3521 if (!proc_fill_cache(file
, ctx
, name
, len
,
3522 proc_task_instantiate
, task
, NULL
)) {
3523 /* returning this tgid failed, save it as the first
3524 * pid for the next readir call */
3525 file
->f_version
= (u64
)tid
;
3526 put_task_struct(task
);
3534 static int proc_task_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
3536 struct inode
*inode
= d_inode(dentry
);
3537 struct task_struct
*p
= get_proc_task(inode
);
3538 generic_fillattr(inode
, stat
);
3541 stat
->nlink
+= get_nr_threads(p
);
3548 static const struct inode_operations proc_task_inode_operations
= {
3549 .lookup
= proc_task_lookup
,
3550 .getattr
= proc_task_getattr
,
3551 .setattr
= proc_setattr
,
3552 .permission
= proc_pid_permission
,
3555 static const struct file_operations proc_task_operations
= {
3556 .read
= generic_read_dir
,
3557 .iterate_shared
= proc_task_readdir
,
3558 .llseek
= generic_file_llseek
,
3561 void __init
set_proc_pid_nlink(void)
3563 nlink_tid
= pid_entry_nlink(tid_base_stuff
, ARRAY_SIZE(tid_base_stuff
));
3564 nlink_tgid
= pid_entry_nlink(tgid_base_stuff
, ARRAY_SIZE(tgid_base_stuff
));