4 * Copyright (C) 1991, 1992 Linus Torvalds
6 * proc base directory handling functions
8 * 1999, Al Viro. Rewritten. Now it covers the whole per-process part.
9 * Instead of using magical inumbers to determine the kind of object
10 * we allocate and fill in-core inodes upon lookup. They don't even
11 * go into icache. We cache the reference to task_struct upon lookup too.
12 * Eventually it should become a filesystem in its own. We don't use the
13 * rest of procfs anymore.
19 * Bruna Moreira <bruna.moreira@indt.org.br>
20 * Edjard Mota <edjard.mota@indt.org.br>
21 * Ilias Biris <ilias.biris@indt.org.br>
22 * Mauricio Lin <mauricio.lin@indt.org.br>
24 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
26 * A new process specific entry (smaps) included in /proc. It shows the
27 * size of rss for each memory area. The maps entry lacks information
28 * about physical memory size (rss) for each mapped file, i.e.,
29 * rss information for executables and library files.
30 * This additional information is useful for any tools that need to know
31 * about physical memory consumption for a process specific library.
35 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36 * Pud inclusion in the page table walking.
40 * 10LE Instituto Nokia de Tecnologia - INdT:
41 * A better way to walks through the page table as suggested by Hugh Dickins.
43 * Simo Piiroinen <simo.piiroinen@nokia.com>:
44 * Smaps information related to shared, private, clean and dirty pages.
46 * Paul Mundt <paul.mundt@nokia.com>:
47 * Overall revision about smaps.
50 #include <asm/uaccess.h>
52 #include <linux/errno.h>
53 #include <linux/time.h>
54 #include <linux/proc_fs.h>
55 #include <linux/stat.h>
56 #include <linux/task_io_accounting_ops.h>
57 #include <linux/init.h>
58 #include <linux/capability.h>
59 #include <linux/file.h>
60 #include <linux/fdtable.h>
61 #include <linux/string.h>
62 #include <linux/seq_file.h>
63 #include <linux/namei.h>
64 #include <linux/mnt_namespace.h>
66 #include <linux/swap.h>
67 #include <linux/rcupdate.h>
68 #include <linux/kallsyms.h>
69 #include <linux/stacktrace.h>
70 #include <linux/resource.h>
71 #include <linux/module.h>
72 #include <linux/mount.h>
73 #include <linux/security.h>
74 #include <linux/ptrace.h>
75 #include <linux/tracehook.h>
76 #include <linux/printk.h>
77 #include <linux/cgroup.h>
78 #include <linux/cpuset.h>
79 #include <linux/audit.h>
80 #include <linux/poll.h>
81 #include <linux/nsproxy.h>
82 #include <linux/oom.h>
83 #include <linux/elf.h>
84 #include <linux/pid_namespace.h>
85 #include <linux/user_namespace.h>
86 #include <linux/fs_struct.h>
87 #include <linux/slab.h>
88 #include <linux/flex_array.h>
89 #include <linux/posix-timers.h>
90 #ifdef CONFIG_HARDWALL
91 #include <asm/hardwall.h>
93 #include <trace/events/oom.h>
98 * Implementing inode permission operations in /proc is almost
99 * certainly an error. Permission checks need to happen during
100 * each system call not at open time. The reason is that most of
101 * what we wish to check for permissions in /proc varies at runtime.
103 * The classic example of a problem is opening file descriptors
104 * in /proc for a task before it execs a suid executable.
111 const struct inode_operations
*iop
;
112 const struct file_operations
*fop
;
116 #define NOD(NAME, MODE, IOP, FOP, OP) { \
118 .len = sizeof(NAME) - 1, \
125 #define DIR(NAME, MODE, iops, fops) \
126 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
127 #define LNK(NAME, get_link) \
128 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
129 &proc_pid_link_inode_operations, NULL, \
130 { .proc_get_link = get_link } )
131 #define REG(NAME, MODE, fops) \
132 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
133 #define INF(NAME, MODE, read) \
134 NOD(NAME, (S_IFREG|(MODE)), \
135 NULL, &proc_info_file_operations, \
136 { .proc_read = read } )
137 #define ONE(NAME, MODE, show) \
138 NOD(NAME, (S_IFREG|(MODE)), \
139 NULL, &proc_single_file_operations, \
140 { .proc_show = show } )
143 * Count the number of hardlinks for the pid_entry table, excluding the .
146 static unsigned int pid_entry_count_dirs(const struct pid_entry
*entries
,
153 for (i
= 0; i
< n
; ++i
) {
154 if (S_ISDIR(entries
[i
].mode
))
161 static int get_task_root(struct task_struct
*task
, struct path
*root
)
163 int result
= -ENOENT
;
167 get_fs_root(task
->fs
, root
);
174 static int proc_cwd_link(struct dentry
*dentry
, struct path
*path
)
176 struct task_struct
*task
= get_proc_task(dentry
->d_inode
);
177 int result
= -ENOENT
;
182 get_fs_pwd(task
->fs
, path
);
186 put_task_struct(task
);
191 static int proc_root_link(struct dentry
*dentry
, struct path
*path
)
193 struct task_struct
*task
= get_proc_task(dentry
->d_inode
);
194 int result
= -ENOENT
;
197 result
= get_task_root(task
, path
);
198 put_task_struct(task
);
203 static int proc_pid_cmdline(struct task_struct
*task
, char * buffer
)
207 struct mm_struct
*mm
= get_task_mm(task
);
211 goto out_mm
; /* Shh! No looking before we're done */
213 len
= mm
->arg_end
- mm
->arg_start
;
218 res
= access_process_vm(task
, mm
->arg_start
, buffer
, len
, 0);
220 // If the nul at the end of args has been overwritten, then
221 // assume application is using setproctitle(3).
222 if (res
> 0 && buffer
[res
-1] != '\0' && len
< PAGE_SIZE
) {
223 len
= strnlen(buffer
, res
);
227 len
= mm
->env_end
- mm
->env_start
;
228 if (len
> PAGE_SIZE
- res
)
229 len
= PAGE_SIZE
- res
;
230 res
+= access_process_vm(task
, mm
->env_start
, buffer
+res
, len
, 0);
231 res
= strnlen(buffer
, res
);
240 static int proc_pid_auxv(struct task_struct
*task
, char *buffer
)
242 struct mm_struct
*mm
= mm_access(task
, PTRACE_MODE_READ
);
243 int res
= PTR_ERR(mm
);
244 if (mm
&& !IS_ERR(mm
)) {
245 unsigned int nwords
= 0;
248 } while (mm
->saved_auxv
[nwords
- 2] != 0); /* AT_NULL */
249 res
= nwords
* sizeof(mm
->saved_auxv
[0]);
252 memcpy(buffer
, mm
->saved_auxv
, res
);
259 #ifdef CONFIG_KALLSYMS
261 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
262 * Returns the resolved symbol. If that fails, simply return the address.
264 static int proc_pid_wchan(struct task_struct
*task
, char *buffer
)
267 char symname
[KSYM_NAME_LEN
];
269 wchan
= get_wchan(task
);
271 if (lookup_symbol_name(wchan
, symname
) < 0)
272 if (!ptrace_may_access(task
, PTRACE_MODE_READ
))
275 return sprintf(buffer
, "%lu", wchan
);
277 return sprintf(buffer
, "%s", symname
);
279 #endif /* CONFIG_KALLSYMS */
281 static int lock_trace(struct task_struct
*task
)
283 int err
= mutex_lock_killable(&task
->signal
->cred_guard_mutex
);
286 if (!ptrace_may_access(task
, PTRACE_MODE_ATTACH
)) {
287 mutex_unlock(&task
->signal
->cred_guard_mutex
);
293 static void unlock_trace(struct task_struct
*task
)
295 mutex_unlock(&task
->signal
->cred_guard_mutex
);
298 #ifdef CONFIG_STACKTRACE
300 #define MAX_STACK_TRACE_DEPTH 64
302 static int proc_pid_stack(struct seq_file
*m
, struct pid_namespace
*ns
,
303 struct pid
*pid
, struct task_struct
*task
)
305 struct stack_trace trace
;
306 unsigned long *entries
;
310 entries
= kmalloc(MAX_STACK_TRACE_DEPTH
* sizeof(*entries
), GFP_KERNEL
);
314 trace
.nr_entries
= 0;
315 trace
.max_entries
= MAX_STACK_TRACE_DEPTH
;
316 trace
.entries
= entries
;
319 err
= lock_trace(task
);
321 save_stack_trace_tsk(task
, &trace
);
323 for (i
= 0; i
< trace
.nr_entries
; i
++) {
324 seq_printf(m
, "[<%pK>] %pS\n",
325 (void *)entries
[i
], (void *)entries
[i
]);
335 #ifdef CONFIG_SCHEDSTATS
337 * Provides /proc/PID/schedstat
339 static int proc_pid_schedstat(struct task_struct
*task
, char *buffer
)
341 return sprintf(buffer
, "%llu %llu %lu\n",
342 (unsigned long long)task
->se
.sum_exec_runtime
,
343 (unsigned long long)task
->sched_info
.run_delay
,
344 task
->sched_info
.pcount
);
348 #ifdef CONFIG_LATENCYTOP
349 static int lstats_show_proc(struct seq_file
*m
, void *v
)
352 struct inode
*inode
= m
->private;
353 struct task_struct
*task
= get_proc_task(inode
);
357 seq_puts(m
, "Latency Top version : v0.1\n");
358 for (i
= 0; i
< 32; i
++) {
359 struct latency_record
*lr
= &task
->latency_record
[i
];
360 if (lr
->backtrace
[0]) {
362 seq_printf(m
, "%i %li %li",
363 lr
->count
, lr
->time
, lr
->max
);
364 for (q
= 0; q
< LT_BACKTRACEDEPTH
; q
++) {
365 unsigned long bt
= lr
->backtrace
[q
];
370 seq_printf(m
, " %ps", (void *)bt
);
376 put_task_struct(task
);
380 static int lstats_open(struct inode
*inode
, struct file
*file
)
382 return single_open(file
, lstats_show_proc
, inode
);
385 static ssize_t
lstats_write(struct file
*file
, const char __user
*buf
,
386 size_t count
, loff_t
*offs
)
388 struct task_struct
*task
= get_proc_task(file_inode(file
));
392 clear_all_latency_tracing(task
);
393 put_task_struct(task
);
398 static const struct file_operations proc_lstats_operations
= {
401 .write
= lstats_write
,
403 .release
= single_release
,
408 #ifdef CONFIG_CGROUPS
409 static int cgroup_open(struct inode
*inode
, struct file
*file
)
411 struct pid
*pid
= PROC_I(inode
)->pid
;
412 return single_open(file
, proc_cgroup_show
, pid
);
415 static const struct file_operations proc_cgroup_operations
= {
419 .release
= single_release
,
423 #ifdef CONFIG_PROC_PID_CPUSET
425 static int cpuset_open(struct inode
*inode
, struct file
*file
)
427 struct pid
*pid
= PROC_I(inode
)->pid
;
428 return single_open(file
, proc_cpuset_show
, pid
);
431 static const struct file_operations proc_cpuset_operations
= {
435 .release
= single_release
,
439 static int proc_oom_score(struct task_struct
*task
, char *buffer
)
441 unsigned long totalpages
= totalram_pages
+ total_swap_pages
;
442 unsigned long points
= 0;
444 read_lock(&tasklist_lock
);
446 points
= oom_badness(task
, NULL
, NULL
, totalpages
) *
448 read_unlock(&tasklist_lock
);
449 return sprintf(buffer
, "%lu\n", points
);
457 static const struct limit_names lnames
[RLIM_NLIMITS
] = {
458 [RLIMIT_CPU
] = {"Max cpu time", "seconds"},
459 [RLIMIT_FSIZE
] = {"Max file size", "bytes"},
460 [RLIMIT_DATA
] = {"Max data size", "bytes"},
461 [RLIMIT_STACK
] = {"Max stack size", "bytes"},
462 [RLIMIT_CORE
] = {"Max core file size", "bytes"},
463 [RLIMIT_RSS
] = {"Max resident set", "bytes"},
464 [RLIMIT_NPROC
] = {"Max processes", "processes"},
465 [RLIMIT_NOFILE
] = {"Max open files", "files"},
466 [RLIMIT_MEMLOCK
] = {"Max locked memory", "bytes"},
467 [RLIMIT_AS
] = {"Max address space", "bytes"},
468 [RLIMIT_LOCKS
] = {"Max file locks", "locks"},
469 [RLIMIT_SIGPENDING
] = {"Max pending signals", "signals"},
470 [RLIMIT_MSGQUEUE
] = {"Max msgqueue size", "bytes"},
471 [RLIMIT_NICE
] = {"Max nice priority", NULL
},
472 [RLIMIT_RTPRIO
] = {"Max realtime priority", NULL
},
473 [RLIMIT_RTTIME
] = {"Max realtime timeout", "us"},
476 /* Display limits for a process */
477 static int proc_pid_limits(struct task_struct
*task
, char *buffer
)
482 char *bufptr
= buffer
;
484 struct rlimit rlim
[RLIM_NLIMITS
];
486 if (!lock_task_sighand(task
, &flags
))
488 memcpy(rlim
, task
->signal
->rlim
, sizeof(struct rlimit
) * RLIM_NLIMITS
);
489 unlock_task_sighand(task
, &flags
);
492 * print the file header
494 count
+= sprintf(&bufptr
[count
], "%-25s %-20s %-20s %-10s\n",
495 "Limit", "Soft Limit", "Hard Limit", "Units");
497 for (i
= 0; i
< RLIM_NLIMITS
; i
++) {
498 if (rlim
[i
].rlim_cur
== RLIM_INFINITY
)
499 count
+= sprintf(&bufptr
[count
], "%-25s %-20s ",
500 lnames
[i
].name
, "unlimited");
502 count
+= sprintf(&bufptr
[count
], "%-25s %-20lu ",
503 lnames
[i
].name
, rlim
[i
].rlim_cur
);
505 if (rlim
[i
].rlim_max
== RLIM_INFINITY
)
506 count
+= sprintf(&bufptr
[count
], "%-20s ", "unlimited");
508 count
+= sprintf(&bufptr
[count
], "%-20lu ",
512 count
+= sprintf(&bufptr
[count
], "%-10s\n",
515 count
+= sprintf(&bufptr
[count
], "\n");
521 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
522 static int proc_pid_syscall(struct task_struct
*task
, char *buffer
)
525 unsigned long args
[6], sp
, pc
;
526 int res
= lock_trace(task
);
530 if (task_current_syscall(task
, &nr
, args
, 6, &sp
, &pc
))
531 res
= sprintf(buffer
, "running\n");
533 res
= sprintf(buffer
, "%ld 0x%lx 0x%lx\n", nr
, sp
, pc
);
535 res
= sprintf(buffer
,
536 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
538 args
[0], args
[1], args
[2], args
[3], args
[4], args
[5],
543 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
545 /************************************************************************/
546 /* Here the fs part begins */
547 /************************************************************************/
549 /* permission checks */
550 static int proc_fd_access_allowed(struct inode
*inode
)
552 struct task_struct
*task
;
554 /* Allow access to a task's file descriptors if it is us or we
555 * may use ptrace attach to the process and find out that
558 task
= get_proc_task(inode
);
560 allowed
= ptrace_may_access(task
, PTRACE_MODE_READ
);
561 put_task_struct(task
);
566 int proc_setattr(struct dentry
*dentry
, struct iattr
*attr
)
569 struct inode
*inode
= dentry
->d_inode
;
571 if (attr
->ia_valid
& ATTR_MODE
)
574 error
= inode_change_ok(inode
, attr
);
578 setattr_copy(inode
, attr
);
579 mark_inode_dirty(inode
);
584 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
585 * or euid/egid (for hide_pid_min=2)?
587 static bool has_pid_permissions(struct pid_namespace
*pid
,
588 struct task_struct
*task
,
591 if (pid
->hide_pid
< hide_pid_min
)
593 if (in_group_p(pid
->pid_gid
))
595 return ptrace_may_access(task
, PTRACE_MODE_READ
);
599 static int proc_pid_permission(struct inode
*inode
, int mask
)
601 struct pid_namespace
*pid
= inode
->i_sb
->s_fs_info
;
602 struct task_struct
*task
;
605 task
= get_proc_task(inode
);
608 has_perms
= has_pid_permissions(pid
, task
, 1);
609 put_task_struct(task
);
612 if (pid
->hide_pid
== 2) {
614 * Let's make getdents(), stat(), and open()
615 * consistent with each other. If a process
616 * may not stat() a file, it shouldn't be seen
624 return generic_permission(inode
, mask
);
629 static const struct inode_operations proc_def_inode_operations
= {
630 .setattr
= proc_setattr
,
633 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
635 static ssize_t
proc_info_read(struct file
* file
, char __user
* buf
,
636 size_t count
, loff_t
*ppos
)
638 struct inode
* inode
= file_inode(file
);
641 struct task_struct
*task
= get_proc_task(inode
);
647 if (count
> PROC_BLOCK_SIZE
)
648 count
= PROC_BLOCK_SIZE
;
651 if (!(page
= __get_free_page(GFP_TEMPORARY
)))
654 length
= PROC_I(inode
)->op
.proc_read(task
, (char*)page
);
657 length
= simple_read_from_buffer(buf
, count
, ppos
, (char *)page
, length
);
660 put_task_struct(task
);
665 static const struct file_operations proc_info_file_operations
= {
666 .read
= proc_info_read
,
667 .llseek
= generic_file_llseek
,
670 static int proc_single_show(struct seq_file
*m
, void *v
)
672 struct inode
*inode
= m
->private;
673 struct pid_namespace
*ns
;
675 struct task_struct
*task
;
678 ns
= inode
->i_sb
->s_fs_info
;
679 pid
= proc_pid(inode
);
680 task
= get_pid_task(pid
, PIDTYPE_PID
);
684 ret
= PROC_I(inode
)->op
.proc_show(m
, ns
, pid
, task
);
686 put_task_struct(task
);
690 static int proc_single_open(struct inode
*inode
, struct file
*filp
)
692 return single_open(filp
, proc_single_show
, inode
);
695 static const struct file_operations proc_single_file_operations
= {
696 .open
= proc_single_open
,
699 .release
= single_release
,
702 static int __mem_open(struct inode
*inode
, struct file
*file
, unsigned int mode
)
704 struct task_struct
*task
= get_proc_task(file_inode(file
));
705 struct mm_struct
*mm
;
710 mm
= mm_access(task
, mode
);
711 put_task_struct(task
);
717 /* ensure this mm_struct can't be freed */
718 atomic_inc(&mm
->mm_count
);
719 /* but do not pin its memory */
723 file
->private_data
= mm
;
728 static int mem_open(struct inode
*inode
, struct file
*file
)
730 int ret
= __mem_open(inode
, file
, PTRACE_MODE_ATTACH
);
732 /* OK to pass negative loff_t, we can catch out-of-range */
733 file
->f_mode
|= FMODE_UNSIGNED_OFFSET
;
738 static ssize_t
mem_rw(struct file
*file
, char __user
*buf
,
739 size_t count
, loff_t
*ppos
, int write
)
741 struct mm_struct
*mm
= file
->private_data
;
742 unsigned long addr
= *ppos
;
749 page
= (char *)__get_free_page(GFP_TEMPORARY
);
754 if (!atomic_inc_not_zero(&mm
->mm_users
))
758 int this_len
= min_t(int, count
, PAGE_SIZE
);
760 if (write
&& copy_from_user(page
, buf
, this_len
)) {
765 this_len
= access_remote_vm(mm
, addr
, page
, this_len
, write
);
772 if (!write
&& copy_to_user(buf
, page
, this_len
)) {
786 free_page((unsigned long) page
);
790 static ssize_t
mem_read(struct file
*file
, char __user
*buf
,
791 size_t count
, loff_t
*ppos
)
793 return mem_rw(file
, buf
, count
, ppos
, 0);
796 static ssize_t
mem_write(struct file
*file
, const char __user
*buf
,
797 size_t count
, loff_t
*ppos
)
799 return mem_rw(file
, (char __user
*)buf
, count
, ppos
, 1);
802 loff_t
mem_lseek(struct file
*file
, loff_t offset
, int orig
)
806 file
->f_pos
= offset
;
809 file
->f_pos
+= offset
;
814 force_successful_syscall_return();
818 static int mem_release(struct inode
*inode
, struct file
*file
)
820 struct mm_struct
*mm
= file
->private_data
;
826 static const struct file_operations proc_mem_operations
= {
831 .release
= mem_release
,
834 static int environ_open(struct inode
*inode
, struct file
*file
)
836 return __mem_open(inode
, file
, PTRACE_MODE_READ
);
839 static ssize_t
environ_read(struct file
*file
, char __user
*buf
,
840 size_t count
, loff_t
*ppos
)
843 unsigned long src
= *ppos
;
845 struct mm_struct
*mm
= file
->private_data
;
850 page
= (char *)__get_free_page(GFP_TEMPORARY
);
855 if (!atomic_inc_not_zero(&mm
->mm_users
))
858 size_t this_len
, max_len
;
861 if (src
>= (mm
->env_end
- mm
->env_start
))
864 this_len
= mm
->env_end
- (mm
->env_start
+ src
);
866 max_len
= min_t(size_t, PAGE_SIZE
, count
);
867 this_len
= min(max_len
, this_len
);
869 retval
= access_remote_vm(mm
, (mm
->env_start
+ src
),
877 if (copy_to_user(buf
, page
, retval
)) {
891 free_page((unsigned long) page
);
895 static const struct file_operations proc_environ_operations
= {
896 .open
= environ_open
,
897 .read
= environ_read
,
898 .llseek
= generic_file_llseek
,
899 .release
= mem_release
,
902 static ssize_t
oom_adj_read(struct file
*file
, char __user
*buf
, size_t count
,
905 struct task_struct
*task
= get_proc_task(file_inode(file
));
906 char buffer
[PROC_NUMBUF
];
907 int oom_adj
= OOM_ADJUST_MIN
;
913 if (lock_task_sighand(task
, &flags
)) {
914 if (task
->signal
->oom_score_adj
== OOM_SCORE_ADJ_MAX
)
915 oom_adj
= OOM_ADJUST_MAX
;
917 oom_adj
= (task
->signal
->oom_score_adj
* -OOM_DISABLE
) /
919 unlock_task_sighand(task
, &flags
);
921 put_task_struct(task
);
922 len
= snprintf(buffer
, sizeof(buffer
), "%d\n", oom_adj
);
923 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
926 static ssize_t
oom_adj_write(struct file
*file
, const char __user
*buf
,
927 size_t count
, loff_t
*ppos
)
929 struct task_struct
*task
;
930 char buffer
[PROC_NUMBUF
];
935 memset(buffer
, 0, sizeof(buffer
));
936 if (count
> sizeof(buffer
) - 1)
937 count
= sizeof(buffer
) - 1;
938 if (copy_from_user(buffer
, buf
, count
)) {
943 err
= kstrtoint(strstrip(buffer
), 0, &oom_adj
);
946 if ((oom_adj
< OOM_ADJUST_MIN
|| oom_adj
> OOM_ADJUST_MAX
) &&
947 oom_adj
!= OOM_DISABLE
) {
952 task
= get_proc_task(file_inode(file
));
964 if (!lock_task_sighand(task
, &flags
)) {
970 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
971 * value is always attainable.
973 if (oom_adj
== OOM_ADJUST_MAX
)
974 oom_adj
= OOM_SCORE_ADJ_MAX
;
976 oom_adj
= (oom_adj
* OOM_SCORE_ADJ_MAX
) / -OOM_DISABLE
;
978 if (oom_adj
< task
->signal
->oom_score_adj
&&
979 !capable(CAP_SYS_RESOURCE
)) {
985 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
986 * /proc/pid/oom_score_adj instead.
988 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
989 current
->comm
, task_pid_nr(current
), task_pid_nr(task
),
992 task
->signal
->oom_score_adj
= oom_adj
;
993 trace_oom_score_adj_update(task
);
995 unlock_task_sighand(task
, &flags
);
998 put_task_struct(task
);
1000 return err
< 0 ? err
: count
;
1003 static const struct file_operations proc_oom_adj_operations
= {
1004 .read
= oom_adj_read
,
1005 .write
= oom_adj_write
,
1006 .llseek
= generic_file_llseek
,
1009 static ssize_t
oom_score_adj_read(struct file
*file
, char __user
*buf
,
1010 size_t count
, loff_t
*ppos
)
1012 struct task_struct
*task
= get_proc_task(file_inode(file
));
1013 char buffer
[PROC_NUMBUF
];
1014 short oom_score_adj
= OOM_SCORE_ADJ_MIN
;
1015 unsigned long flags
;
1020 if (lock_task_sighand(task
, &flags
)) {
1021 oom_score_adj
= task
->signal
->oom_score_adj
;
1022 unlock_task_sighand(task
, &flags
);
1024 put_task_struct(task
);
1025 len
= snprintf(buffer
, sizeof(buffer
), "%hd\n", oom_score_adj
);
1026 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
1029 static ssize_t
oom_score_adj_write(struct file
*file
, const char __user
*buf
,
1030 size_t count
, loff_t
*ppos
)
1032 struct task_struct
*task
;
1033 char buffer
[PROC_NUMBUF
];
1034 unsigned long flags
;
1038 memset(buffer
, 0, sizeof(buffer
));
1039 if (count
> sizeof(buffer
) - 1)
1040 count
= sizeof(buffer
) - 1;
1041 if (copy_from_user(buffer
, buf
, count
)) {
1046 err
= kstrtoint(strstrip(buffer
), 0, &oom_score_adj
);
1049 if (oom_score_adj
< OOM_SCORE_ADJ_MIN
||
1050 oom_score_adj
> OOM_SCORE_ADJ_MAX
) {
1055 task
= get_proc_task(file_inode(file
));
1067 if (!lock_task_sighand(task
, &flags
)) {
1072 if ((short)oom_score_adj
< task
->signal
->oom_score_adj_min
&&
1073 !capable(CAP_SYS_RESOURCE
)) {
1078 task
->signal
->oom_score_adj
= (short)oom_score_adj
;
1079 if (has_capability_noaudit(current
, CAP_SYS_RESOURCE
))
1080 task
->signal
->oom_score_adj_min
= (short)oom_score_adj
;
1081 trace_oom_score_adj_update(task
);
1084 unlock_task_sighand(task
, &flags
);
1087 put_task_struct(task
);
1089 return err
< 0 ? err
: count
;
1092 static const struct file_operations proc_oom_score_adj_operations
= {
1093 .read
= oom_score_adj_read
,
1094 .write
= oom_score_adj_write
,
1095 .llseek
= default_llseek
,
1098 #ifdef CONFIG_AUDITSYSCALL
1099 #define TMPBUFLEN 21
1100 static ssize_t
proc_loginuid_read(struct file
* file
, char __user
* buf
,
1101 size_t count
, loff_t
*ppos
)
1103 struct inode
* inode
= file_inode(file
);
1104 struct task_struct
*task
= get_proc_task(inode
);
1106 char tmpbuf
[TMPBUFLEN
];
1110 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
1111 from_kuid(file
->f_cred
->user_ns
,
1112 audit_get_loginuid(task
)));
1113 put_task_struct(task
);
1114 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1117 static ssize_t
proc_loginuid_write(struct file
* file
, const char __user
* buf
,
1118 size_t count
, loff_t
*ppos
)
1120 struct inode
* inode
= file_inode(file
);
1127 if (current
!= pid_task(proc_pid(inode
), PIDTYPE_PID
)) {
1133 if (count
>= PAGE_SIZE
)
1134 count
= PAGE_SIZE
- 1;
1137 /* No partial writes. */
1140 page
= (char*)__get_free_page(GFP_TEMPORARY
);
1144 if (copy_from_user(page
, buf
, count
))
1148 loginuid
= simple_strtoul(page
, &tmp
, 10);
1154 kloginuid
= make_kuid(file
->f_cred
->user_ns
, loginuid
);
1155 if (!uid_valid(kloginuid
)) {
1160 length
= audit_set_loginuid(kloginuid
);
1161 if (likely(length
== 0))
1165 free_page((unsigned long) page
);
1169 static const struct file_operations proc_loginuid_operations
= {
1170 .read
= proc_loginuid_read
,
1171 .write
= proc_loginuid_write
,
1172 .llseek
= generic_file_llseek
,
1175 static ssize_t
proc_sessionid_read(struct file
* file
, char __user
* buf
,
1176 size_t count
, loff_t
*ppos
)
1178 struct inode
* inode
= file_inode(file
);
1179 struct task_struct
*task
= get_proc_task(inode
);
1181 char tmpbuf
[TMPBUFLEN
];
1185 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
1186 audit_get_sessionid(task
));
1187 put_task_struct(task
);
1188 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1191 static const struct file_operations proc_sessionid_operations
= {
1192 .read
= proc_sessionid_read
,
1193 .llseek
= generic_file_llseek
,
1197 #ifdef CONFIG_FAULT_INJECTION
1198 static ssize_t
proc_fault_inject_read(struct file
* file
, char __user
* buf
,
1199 size_t count
, loff_t
*ppos
)
1201 struct task_struct
*task
= get_proc_task(file_inode(file
));
1202 char buffer
[PROC_NUMBUF
];
1208 make_it_fail
= task
->make_it_fail
;
1209 put_task_struct(task
);
1211 len
= snprintf(buffer
, sizeof(buffer
), "%i\n", make_it_fail
);
1213 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
1216 static ssize_t
proc_fault_inject_write(struct file
* file
,
1217 const char __user
* buf
, size_t count
, loff_t
*ppos
)
1219 struct task_struct
*task
;
1220 char buffer
[PROC_NUMBUF
], *end
;
1223 if (!capable(CAP_SYS_RESOURCE
))
1225 memset(buffer
, 0, sizeof(buffer
));
1226 if (count
> sizeof(buffer
) - 1)
1227 count
= sizeof(buffer
) - 1;
1228 if (copy_from_user(buffer
, buf
, count
))
1230 make_it_fail
= simple_strtol(strstrip(buffer
), &end
, 0);
1233 task
= get_proc_task(file_inode(file
));
1236 task
->make_it_fail
= make_it_fail
;
1237 put_task_struct(task
);
1242 static const struct file_operations proc_fault_inject_operations
= {
1243 .read
= proc_fault_inject_read
,
1244 .write
= proc_fault_inject_write
,
1245 .llseek
= generic_file_llseek
,
1250 #ifdef CONFIG_SCHED_DEBUG
1252 * Print out various scheduling related per-task fields:
1254 static int sched_show(struct seq_file
*m
, void *v
)
1256 struct inode
*inode
= m
->private;
1257 struct task_struct
*p
;
1259 p
= get_proc_task(inode
);
1262 proc_sched_show_task(p
, m
);
1270 sched_write(struct file
*file
, const char __user
*buf
,
1271 size_t count
, loff_t
*offset
)
1273 struct inode
*inode
= file_inode(file
);
1274 struct task_struct
*p
;
1276 p
= get_proc_task(inode
);
1279 proc_sched_set_task(p
);
1286 static int sched_open(struct inode
*inode
, struct file
*filp
)
1288 return single_open(filp
, sched_show
, inode
);
1291 static const struct file_operations proc_pid_sched_operations
= {
1294 .write
= sched_write
,
1295 .llseek
= seq_lseek
,
1296 .release
= single_release
,
1301 #ifdef CONFIG_SCHED_AUTOGROUP
1303 * Print out autogroup related information:
1305 static int sched_autogroup_show(struct seq_file
*m
, void *v
)
1307 struct inode
*inode
= m
->private;
1308 struct task_struct
*p
;
1310 p
= get_proc_task(inode
);
1313 proc_sched_autogroup_show_task(p
, m
);
1321 sched_autogroup_write(struct file
*file
, const char __user
*buf
,
1322 size_t count
, loff_t
*offset
)
1324 struct inode
*inode
= file_inode(file
);
1325 struct task_struct
*p
;
1326 char buffer
[PROC_NUMBUF
];
1330 memset(buffer
, 0, sizeof(buffer
));
1331 if (count
> sizeof(buffer
) - 1)
1332 count
= sizeof(buffer
) - 1;
1333 if (copy_from_user(buffer
, buf
, count
))
1336 err
= kstrtoint(strstrip(buffer
), 0, &nice
);
1340 p
= get_proc_task(inode
);
1344 err
= proc_sched_autogroup_set_nice(p
, nice
);
1353 static int sched_autogroup_open(struct inode
*inode
, struct file
*filp
)
1357 ret
= single_open(filp
, sched_autogroup_show
, NULL
);
1359 struct seq_file
*m
= filp
->private_data
;
1366 static const struct file_operations proc_pid_sched_autogroup_operations
= {
1367 .open
= sched_autogroup_open
,
1369 .write
= sched_autogroup_write
,
1370 .llseek
= seq_lseek
,
1371 .release
= single_release
,
1374 #endif /* CONFIG_SCHED_AUTOGROUP */
1376 static ssize_t
comm_write(struct file
*file
, const char __user
*buf
,
1377 size_t count
, loff_t
*offset
)
1379 struct inode
*inode
= file_inode(file
);
1380 struct task_struct
*p
;
1381 char buffer
[TASK_COMM_LEN
];
1382 const size_t maxlen
= sizeof(buffer
) - 1;
1384 memset(buffer
, 0, sizeof(buffer
));
1385 if (copy_from_user(buffer
, buf
, count
> maxlen
? maxlen
: count
))
1388 p
= get_proc_task(inode
);
1392 if (same_thread_group(current
, p
))
1393 set_task_comm(p
, buffer
);
1402 static int comm_show(struct seq_file
*m
, void *v
)
1404 struct inode
*inode
= m
->private;
1405 struct task_struct
*p
;
1407 p
= get_proc_task(inode
);
1412 seq_printf(m
, "%s\n", p
->comm
);
1420 static int comm_open(struct inode
*inode
, struct file
*filp
)
1422 return single_open(filp
, comm_show
, inode
);
1425 static const struct file_operations proc_pid_set_comm_operations
= {
1428 .write
= comm_write
,
1429 .llseek
= seq_lseek
,
1430 .release
= single_release
,
1433 static int proc_exe_link(struct dentry
*dentry
, struct path
*exe_path
)
1435 struct task_struct
*task
;
1436 struct mm_struct
*mm
;
1437 struct file
*exe_file
;
1439 task
= get_proc_task(dentry
->d_inode
);
1442 mm
= get_task_mm(task
);
1443 put_task_struct(task
);
1446 exe_file
= get_mm_exe_file(mm
);
1449 *exe_path
= exe_file
->f_path
;
1450 path_get(&exe_file
->f_path
);
1457 static void *proc_pid_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
1459 struct inode
*inode
= dentry
->d_inode
;
1461 int error
= -EACCES
;
1463 /* Are we allowed to snoop on the tasks file descriptors? */
1464 if (!proc_fd_access_allowed(inode
))
1467 error
= PROC_I(inode
)->op
.proc_get_link(dentry
, &path
);
1471 nd_jump_link(nd
, &path
);
1474 return ERR_PTR(error
);
1477 static int do_proc_readlink(struct path
*path
, char __user
*buffer
, int buflen
)
1479 char *tmp
= (char*)__get_free_page(GFP_TEMPORARY
);
1486 pathname
= d_path(path
, tmp
, PAGE_SIZE
);
1487 len
= PTR_ERR(pathname
);
1488 if (IS_ERR(pathname
))
1490 len
= tmp
+ PAGE_SIZE
- 1 - pathname
;
1494 if (copy_to_user(buffer
, pathname
, len
))
1497 free_page((unsigned long)tmp
);
1501 static int proc_pid_readlink(struct dentry
* dentry
, char __user
* buffer
, int buflen
)
1503 int error
= -EACCES
;
1504 struct inode
*inode
= dentry
->d_inode
;
1507 /* Are we allowed to snoop on the tasks file descriptors? */
1508 if (!proc_fd_access_allowed(inode
))
1511 error
= PROC_I(inode
)->op
.proc_get_link(dentry
, &path
);
1515 error
= do_proc_readlink(&path
, buffer
, buflen
);
1521 const struct inode_operations proc_pid_link_inode_operations
= {
1522 .readlink
= proc_pid_readlink
,
1523 .follow_link
= proc_pid_follow_link
,
1524 .setattr
= proc_setattr
,
1528 /* building an inode */
1530 struct inode
*proc_pid_make_inode(struct super_block
* sb
, struct task_struct
*task
)
1532 struct inode
* inode
;
1533 struct proc_inode
*ei
;
1534 const struct cred
*cred
;
1536 /* We need a new inode */
1538 inode
= new_inode(sb
);
1544 inode
->i_ino
= get_next_ino();
1545 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1546 inode
->i_op
= &proc_def_inode_operations
;
1549 * grab the reference to task.
1551 ei
->pid
= get_task_pid(task
, PIDTYPE_PID
);
1555 if (task_dumpable(task
)) {
1557 cred
= __task_cred(task
);
1558 inode
->i_uid
= cred
->euid
;
1559 inode
->i_gid
= cred
->egid
;
1562 security_task_to_inode(task
, inode
);
1572 int pid_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
1574 struct inode
*inode
= dentry
->d_inode
;
1575 struct task_struct
*task
;
1576 const struct cred
*cred
;
1577 struct pid_namespace
*pid
= dentry
->d_sb
->s_fs_info
;
1579 generic_fillattr(inode
, stat
);
1582 stat
->uid
= GLOBAL_ROOT_UID
;
1583 stat
->gid
= GLOBAL_ROOT_GID
;
1584 task
= pid_task(proc_pid(inode
), PIDTYPE_PID
);
1586 if (!has_pid_permissions(pid
, task
, 2)) {
1589 * This doesn't prevent learning whether PID exists,
1590 * it only makes getattr() consistent with readdir().
1594 if ((inode
->i_mode
== (S_IFDIR
|S_IRUGO
|S_IXUGO
)) ||
1595 task_dumpable(task
)) {
1596 cred
= __task_cred(task
);
1597 stat
->uid
= cred
->euid
;
1598 stat
->gid
= cred
->egid
;
1608 * Exceptional case: normally we are not allowed to unhash a busy
1609 * directory. In this case, however, we can do it - no aliasing problems
1610 * due to the way we treat inodes.
1612 * Rewrite the inode's ownerships here because the owning task may have
1613 * performed a setuid(), etc.
1615 * Before the /proc/pid/status file was created the only way to read
1616 * the effective uid of a /process was to stat /proc/pid. Reading
1617 * /proc/pid/status is slow enough that procps and other packages
1618 * kept stating /proc/pid. To keep the rules in /proc simple I have
1619 * made this apply to all per process world readable and executable
1622 int pid_revalidate(struct dentry
*dentry
, unsigned int flags
)
1624 struct inode
*inode
;
1625 struct task_struct
*task
;
1626 const struct cred
*cred
;
1628 if (flags
& LOOKUP_RCU
)
1631 inode
= dentry
->d_inode
;
1632 task
= get_proc_task(inode
);
1635 if ((inode
->i_mode
== (S_IFDIR
|S_IRUGO
|S_IXUGO
)) ||
1636 task_dumpable(task
)) {
1638 cred
= __task_cred(task
);
1639 inode
->i_uid
= cred
->euid
;
1640 inode
->i_gid
= cred
->egid
;
1643 inode
->i_uid
= GLOBAL_ROOT_UID
;
1644 inode
->i_gid
= GLOBAL_ROOT_GID
;
1646 inode
->i_mode
&= ~(S_ISUID
| S_ISGID
);
1647 security_task_to_inode(task
, inode
);
1648 put_task_struct(task
);
1655 int pid_delete_dentry(const struct dentry
*dentry
)
1657 /* Is the task we represent dead?
1658 * If so, then don't put the dentry on the lru list,
1659 * kill it immediately.
1661 return !proc_pid(dentry
->d_inode
)->tasks
[PIDTYPE_PID
].first
;
1664 const struct dentry_operations pid_dentry_operations
=
1666 .d_revalidate
= pid_revalidate
,
1667 .d_delete
= pid_delete_dentry
,
1673 * Fill a directory entry.
1675 * If possible create the dcache entry and derive our inode number and
1676 * file type from dcache entry.
1678 * Since all of the proc inode numbers are dynamically generated, the inode
1679 * numbers do not exist until the inode is cache. This means creating the
1680 * the dcache entry in readdir is necessary to keep the inode numbers
1681 * reported by readdir in sync with the inode numbers reported
1684 int proc_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
1685 const char *name
, int len
,
1686 instantiate_t instantiate
, struct task_struct
*task
, const void *ptr
)
1688 struct dentry
*child
, *dir
= filp
->f_path
.dentry
;
1689 struct inode
*inode
;
1692 unsigned type
= DT_UNKNOWN
;
1696 qname
.hash
= full_name_hash(name
, len
);
1698 child
= d_lookup(dir
, &qname
);
1701 new = d_alloc(dir
, &qname
);
1703 child
= instantiate(dir
->d_inode
, new, task
, ptr
);
1710 if (!child
|| IS_ERR(child
) || !child
->d_inode
)
1711 goto end_instantiate
;
1712 inode
= child
->d_inode
;
1715 type
= inode
->i_mode
>> 12;
1720 ino
= find_inode_number(dir
, &qname
);
1723 return filldir(dirent
, name
, len
, filp
->f_pos
, ino
, type
);
1726 #ifdef CONFIG_CHECKPOINT_RESTORE
1729 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1730 * which represent vma start and end addresses.
1732 static int dname_to_vma_addr(struct dentry
*dentry
,
1733 unsigned long *start
, unsigned long *end
)
1735 if (sscanf(dentry
->d_name
.name
, "%lx-%lx", start
, end
) != 2)
1741 static int map_files_d_revalidate(struct dentry
*dentry
, unsigned int flags
)
1743 unsigned long vm_start
, vm_end
;
1744 bool exact_vma_exists
= false;
1745 struct mm_struct
*mm
= NULL
;
1746 struct task_struct
*task
;
1747 const struct cred
*cred
;
1748 struct inode
*inode
;
1751 if (flags
& LOOKUP_RCU
)
1754 if (!capable(CAP_SYS_ADMIN
)) {
1759 inode
= dentry
->d_inode
;
1760 task
= get_proc_task(inode
);
1764 mm
= mm_access(task
, PTRACE_MODE_READ
);
1765 if (IS_ERR_OR_NULL(mm
))
1768 if (!dname_to_vma_addr(dentry
, &vm_start
, &vm_end
)) {
1769 down_read(&mm
->mmap_sem
);
1770 exact_vma_exists
= !!find_exact_vma(mm
, vm_start
, vm_end
);
1771 up_read(&mm
->mmap_sem
);
1776 if (exact_vma_exists
) {
1777 if (task_dumpable(task
)) {
1779 cred
= __task_cred(task
);
1780 inode
->i_uid
= cred
->euid
;
1781 inode
->i_gid
= cred
->egid
;
1784 inode
->i_uid
= GLOBAL_ROOT_UID
;
1785 inode
->i_gid
= GLOBAL_ROOT_GID
;
1787 security_task_to_inode(task
, inode
);
1792 put_task_struct(task
);
1801 static const struct dentry_operations tid_map_files_dentry_operations
= {
1802 .d_revalidate
= map_files_d_revalidate
,
1803 .d_delete
= pid_delete_dentry
,
1806 static int proc_map_files_get_link(struct dentry
*dentry
, struct path
*path
)
1808 unsigned long vm_start
, vm_end
;
1809 struct vm_area_struct
*vma
;
1810 struct task_struct
*task
;
1811 struct mm_struct
*mm
;
1815 task
= get_proc_task(dentry
->d_inode
);
1819 mm
= get_task_mm(task
);
1820 put_task_struct(task
);
1824 rc
= dname_to_vma_addr(dentry
, &vm_start
, &vm_end
);
1828 down_read(&mm
->mmap_sem
);
1829 vma
= find_exact_vma(mm
, vm_start
, vm_end
);
1830 if (vma
&& vma
->vm_file
) {
1831 *path
= vma
->vm_file
->f_path
;
1835 up_read(&mm
->mmap_sem
);
1843 struct map_files_info
{
1846 unsigned char name
[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
1849 static struct dentry
*
1850 proc_map_files_instantiate(struct inode
*dir
, struct dentry
*dentry
,
1851 struct task_struct
*task
, const void *ptr
)
1853 fmode_t mode
= (fmode_t
)(unsigned long)ptr
;
1854 struct proc_inode
*ei
;
1855 struct inode
*inode
;
1857 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
1859 return ERR_PTR(-ENOENT
);
1862 ei
->op
.proc_get_link
= proc_map_files_get_link
;
1864 inode
->i_op
= &proc_pid_link_inode_operations
;
1866 inode
->i_mode
= S_IFLNK
;
1868 if (mode
& FMODE_READ
)
1869 inode
->i_mode
|= S_IRUSR
;
1870 if (mode
& FMODE_WRITE
)
1871 inode
->i_mode
|= S_IWUSR
;
1873 d_set_d_op(dentry
, &tid_map_files_dentry_operations
);
1874 d_add(dentry
, inode
);
1879 static struct dentry
*proc_map_files_lookup(struct inode
*dir
,
1880 struct dentry
*dentry
, unsigned int flags
)
1882 unsigned long vm_start
, vm_end
;
1883 struct vm_area_struct
*vma
;
1884 struct task_struct
*task
;
1885 struct dentry
*result
;
1886 struct mm_struct
*mm
;
1888 result
= ERR_PTR(-EPERM
);
1889 if (!capable(CAP_SYS_ADMIN
))
1892 result
= ERR_PTR(-ENOENT
);
1893 task
= get_proc_task(dir
);
1897 result
= ERR_PTR(-EACCES
);
1898 if (!ptrace_may_access(task
, PTRACE_MODE_READ
))
1901 result
= ERR_PTR(-ENOENT
);
1902 if (dname_to_vma_addr(dentry
, &vm_start
, &vm_end
))
1905 mm
= get_task_mm(task
);
1909 down_read(&mm
->mmap_sem
);
1910 vma
= find_exact_vma(mm
, vm_start
, vm_end
);
1915 result
= proc_map_files_instantiate(dir
, dentry
, task
,
1916 (void *)(unsigned long)vma
->vm_file
->f_mode
);
1919 up_read(&mm
->mmap_sem
);
1922 put_task_struct(task
);
1927 static const struct inode_operations proc_map_files_inode_operations
= {
1928 .lookup
= proc_map_files_lookup
,
1929 .permission
= proc_fd_permission
,
1930 .setattr
= proc_setattr
,
1934 proc_map_files_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
1936 struct dentry
*dentry
= filp
->f_path
.dentry
;
1937 struct inode
*inode
= dentry
->d_inode
;
1938 struct vm_area_struct
*vma
;
1939 struct task_struct
*task
;
1940 struct mm_struct
*mm
;
1945 if (!capable(CAP_SYS_ADMIN
))
1949 task
= get_proc_task(inode
);
1954 if (!ptrace_may_access(task
, PTRACE_MODE_READ
))
1958 switch (filp
->f_pos
) {
1961 if (filldir(dirent
, ".", 1, 0, ino
, DT_DIR
) < 0)
1965 ino
= parent_ino(dentry
);
1966 if (filldir(dirent
, "..", 2, 1, ino
, DT_DIR
) < 0)
1971 unsigned long nr_files
, pos
, i
;
1972 struct flex_array
*fa
= NULL
;
1973 struct map_files_info info
;
1974 struct map_files_info
*p
;
1976 mm
= get_task_mm(task
);
1979 down_read(&mm
->mmap_sem
);
1984 * We need two passes here:
1986 * 1) Collect vmas of mapped files with mmap_sem taken
1987 * 2) Release mmap_sem and instantiate entries
1989 * otherwise we get lockdep complained, since filldir()
1990 * routine might require mmap_sem taken in might_fault().
1993 for (vma
= mm
->mmap
, pos
= 2; vma
; vma
= vma
->vm_next
) {
1994 if (vma
->vm_file
&& ++pos
> filp
->f_pos
)
1999 fa
= flex_array_alloc(sizeof(info
), nr_files
,
2001 if (!fa
|| flex_array_prealloc(fa
, 0, nr_files
,
2005 flex_array_free(fa
);
2006 up_read(&mm
->mmap_sem
);
2010 for (i
= 0, vma
= mm
->mmap
, pos
= 2; vma
;
2011 vma
= vma
->vm_next
) {
2014 if (++pos
<= filp
->f_pos
)
2017 info
.mode
= vma
->vm_file
->f_mode
;
2018 info
.len
= snprintf(info
.name
,
2019 sizeof(info
.name
), "%lx-%lx",
2020 vma
->vm_start
, vma
->vm_end
);
2021 if (flex_array_put(fa
, i
++, &info
, GFP_KERNEL
))
2025 up_read(&mm
->mmap_sem
);
2027 for (i
= 0; i
< nr_files
; i
++) {
2028 p
= flex_array_get(fa
, i
);
2029 ret
= proc_fill_cache(filp
, dirent
, filldir
,
2031 proc_map_files_instantiate
,
2033 (void *)(unsigned long)p
->mode
);
2039 flex_array_free(fa
);
2045 put_task_struct(task
);
2050 static const struct file_operations proc_map_files_operations
= {
2051 .read
= generic_read_dir
,
2052 .readdir
= proc_map_files_readdir
,
2053 .llseek
= default_llseek
,
2056 struct timers_private
{
2058 struct task_struct
*task
;
2059 struct sighand_struct
*sighand
;
2060 struct pid_namespace
*ns
;
2061 unsigned long flags
;
2064 static void *timers_start(struct seq_file
*m
, loff_t
*pos
)
2066 struct timers_private
*tp
= m
->private;
2068 tp
->task
= get_pid_task(tp
->pid
, PIDTYPE_PID
);
2070 return ERR_PTR(-ESRCH
);
2072 tp
->sighand
= lock_task_sighand(tp
->task
, &tp
->flags
);
2074 return ERR_PTR(-ESRCH
);
2076 return seq_list_start(&tp
->task
->signal
->posix_timers
, *pos
);
2079 static void *timers_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
2081 struct timers_private
*tp
= m
->private;
2082 return seq_list_next(v
, &tp
->task
->signal
->posix_timers
, pos
);
2085 static void timers_stop(struct seq_file
*m
, void *v
)
2087 struct timers_private
*tp
= m
->private;
2090 unlock_task_sighand(tp
->task
, &tp
->flags
);
2095 put_task_struct(tp
->task
);
2100 static int show_timer(struct seq_file
*m
, void *v
)
2102 struct k_itimer
*timer
;
2103 struct timers_private
*tp
= m
->private;
2105 static char *nstr
[] = {
2106 [SIGEV_SIGNAL
] = "signal",
2107 [SIGEV_NONE
] = "none",
2108 [SIGEV_THREAD
] = "thread",
2111 timer
= list_entry((struct list_head
*)v
, struct k_itimer
, list
);
2112 notify
= timer
->it_sigev_notify
;
2114 seq_printf(m
, "ID: %d\n", timer
->it_id
);
2115 seq_printf(m
, "signal: %d/%p\n", timer
->sigq
->info
.si_signo
,
2116 timer
->sigq
->info
.si_value
.sival_ptr
);
2117 seq_printf(m
, "notify: %s/%s.%d\n",
2118 nstr
[notify
& ~SIGEV_THREAD_ID
],
2119 (notify
& SIGEV_THREAD_ID
) ? "tid" : "pid",
2120 pid_nr_ns(timer
->it_pid
, tp
->ns
));
2125 static const struct seq_operations proc_timers_seq_ops
= {
2126 .start
= timers_start
,
2127 .next
= timers_next
,
2128 .stop
= timers_stop
,
2132 static int proc_timers_open(struct inode
*inode
, struct file
*file
)
2134 struct timers_private
*tp
;
2136 tp
= __seq_open_private(file
, &proc_timers_seq_ops
,
2137 sizeof(struct timers_private
));
2141 tp
->pid
= proc_pid(inode
);
2142 tp
->ns
= inode
->i_sb
->s_fs_info
;
2146 static const struct file_operations proc_timers_operations
= {
2147 .open
= proc_timers_open
,
2149 .llseek
= seq_lseek
,
2150 .release
= seq_release_private
,
2152 #endif /* CONFIG_CHECKPOINT_RESTORE */
2154 static struct dentry
*proc_pident_instantiate(struct inode
*dir
,
2155 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
2157 const struct pid_entry
*p
= ptr
;
2158 struct inode
*inode
;
2159 struct proc_inode
*ei
;
2160 struct dentry
*error
= ERR_PTR(-ENOENT
);
2162 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
2167 inode
->i_mode
= p
->mode
;
2168 if (S_ISDIR(inode
->i_mode
))
2169 set_nlink(inode
, 2); /* Use getattr to fix if necessary */
2171 inode
->i_op
= p
->iop
;
2173 inode
->i_fop
= p
->fop
;
2175 d_set_d_op(dentry
, &pid_dentry_operations
);
2176 d_add(dentry
, inode
);
2177 /* Close the race of the process dying before we return the dentry */
2178 if (pid_revalidate(dentry
, 0))
2184 static struct dentry
*proc_pident_lookup(struct inode
*dir
,
2185 struct dentry
*dentry
,
2186 const struct pid_entry
*ents
,
2189 struct dentry
*error
;
2190 struct task_struct
*task
= get_proc_task(dir
);
2191 const struct pid_entry
*p
, *last
;
2193 error
= ERR_PTR(-ENOENT
);
2199 * Yes, it does not scale. And it should not. Don't add
2200 * new entries into /proc/<tgid>/ without very good reasons.
2202 last
= &ents
[nents
- 1];
2203 for (p
= ents
; p
<= last
; p
++) {
2204 if (p
->len
!= dentry
->d_name
.len
)
2206 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
))
2212 error
= proc_pident_instantiate(dir
, dentry
, task
, p
);
2214 put_task_struct(task
);
2219 static int proc_pident_fill_cache(struct file
*filp
, void *dirent
,
2220 filldir_t filldir
, struct task_struct
*task
, const struct pid_entry
*p
)
2222 return proc_fill_cache(filp
, dirent
, filldir
, p
->name
, p
->len
,
2223 proc_pident_instantiate
, task
, p
);
2226 static int proc_pident_readdir(struct file
*filp
,
2227 void *dirent
, filldir_t filldir
,
2228 const struct pid_entry
*ents
, unsigned int nents
)
2231 struct dentry
*dentry
= filp
->f_path
.dentry
;
2232 struct inode
*inode
= dentry
->d_inode
;
2233 struct task_struct
*task
= get_proc_task(inode
);
2234 const struct pid_entry
*p
, *last
;
2247 if (filldir(dirent
, ".", 1, i
, ino
, DT_DIR
) < 0)
2253 ino
= parent_ino(dentry
);
2254 if (filldir(dirent
, "..", 2, i
, ino
, DT_DIR
) < 0)
2266 last
= &ents
[nents
- 1];
2268 if (proc_pident_fill_cache(filp
, dirent
, filldir
, task
, p
) < 0)
2277 put_task_struct(task
);
2282 #ifdef CONFIG_SECURITY
2283 static ssize_t
proc_pid_attr_read(struct file
* file
, char __user
* buf
,
2284 size_t count
, loff_t
*ppos
)
2286 struct inode
* inode
= file_inode(file
);
2289 struct task_struct
*task
= get_proc_task(inode
);
2294 length
= security_getprocattr(task
,
2295 (char*)file
->f_path
.dentry
->d_name
.name
,
2297 put_task_struct(task
);
2299 length
= simple_read_from_buffer(buf
, count
, ppos
, p
, length
);
2304 static ssize_t
proc_pid_attr_write(struct file
* file
, const char __user
* buf
,
2305 size_t count
, loff_t
*ppos
)
2307 struct inode
* inode
= file_inode(file
);
2310 struct task_struct
*task
= get_proc_task(inode
);
2315 if (count
> PAGE_SIZE
)
2318 /* No partial writes. */
2324 page
= (char*)__get_free_page(GFP_TEMPORARY
);
2329 if (copy_from_user(page
, buf
, count
))
2332 /* Guard against adverse ptrace interaction */
2333 length
= mutex_lock_interruptible(&task
->signal
->cred_guard_mutex
);
2337 length
= security_setprocattr(task
,
2338 (char*)file
->f_path
.dentry
->d_name
.name
,
2339 (void*)page
, count
);
2340 mutex_unlock(&task
->signal
->cred_guard_mutex
);
2342 free_page((unsigned long) page
);
2344 put_task_struct(task
);
2349 static const struct file_operations proc_pid_attr_operations
= {
2350 .read
= proc_pid_attr_read
,
2351 .write
= proc_pid_attr_write
,
2352 .llseek
= generic_file_llseek
,
2355 static const struct pid_entry attr_dir_stuff
[] = {
2356 REG("current", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2357 REG("prev", S_IRUGO
, proc_pid_attr_operations
),
2358 REG("exec", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2359 REG("fscreate", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2360 REG("keycreate", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2361 REG("sockcreate", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2364 static int proc_attr_dir_readdir(struct file
* filp
,
2365 void * dirent
, filldir_t filldir
)
2367 return proc_pident_readdir(filp
,dirent
,filldir
,
2368 attr_dir_stuff
,ARRAY_SIZE(attr_dir_stuff
));
2371 static const struct file_operations proc_attr_dir_operations
= {
2372 .read
= generic_read_dir
,
2373 .readdir
= proc_attr_dir_readdir
,
2374 .llseek
= default_llseek
,
2377 static struct dentry
*proc_attr_dir_lookup(struct inode
*dir
,
2378 struct dentry
*dentry
, unsigned int flags
)
2380 return proc_pident_lookup(dir
, dentry
,
2381 attr_dir_stuff
, ARRAY_SIZE(attr_dir_stuff
));
2384 static const struct inode_operations proc_attr_dir_inode_operations
= {
2385 .lookup
= proc_attr_dir_lookup
,
2386 .getattr
= pid_getattr
,
2387 .setattr
= proc_setattr
,
2392 #ifdef CONFIG_ELF_CORE
2393 static ssize_t
proc_coredump_filter_read(struct file
*file
, char __user
*buf
,
2394 size_t count
, loff_t
*ppos
)
2396 struct task_struct
*task
= get_proc_task(file_inode(file
));
2397 struct mm_struct
*mm
;
2398 char buffer
[PROC_NUMBUF
];
2406 mm
= get_task_mm(task
);
2408 len
= snprintf(buffer
, sizeof(buffer
), "%08lx\n",
2409 ((mm
->flags
& MMF_DUMP_FILTER_MASK
) >>
2410 MMF_DUMP_FILTER_SHIFT
));
2412 ret
= simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
2415 put_task_struct(task
);
2420 static ssize_t
proc_coredump_filter_write(struct file
*file
,
2421 const char __user
*buf
,
2425 struct task_struct
*task
;
2426 struct mm_struct
*mm
;
2427 char buffer
[PROC_NUMBUF
], *end
;
2434 memset(buffer
, 0, sizeof(buffer
));
2435 if (count
> sizeof(buffer
) - 1)
2436 count
= sizeof(buffer
) - 1;
2437 if (copy_from_user(buffer
, buf
, count
))
2441 val
= (unsigned int)simple_strtoul(buffer
, &end
, 0);
2444 if (end
- buffer
== 0)
2448 task
= get_proc_task(file_inode(file
));
2453 mm
= get_task_mm(task
);
2457 for (i
= 0, mask
= 1; i
< MMF_DUMP_FILTER_BITS
; i
++, mask
<<= 1) {
2459 set_bit(i
+ MMF_DUMP_FILTER_SHIFT
, &mm
->flags
);
2461 clear_bit(i
+ MMF_DUMP_FILTER_SHIFT
, &mm
->flags
);
2466 put_task_struct(task
);
2471 static const struct file_operations proc_coredump_filter_operations
= {
2472 .read
= proc_coredump_filter_read
,
2473 .write
= proc_coredump_filter_write
,
2474 .llseek
= generic_file_llseek
,
2478 #ifdef CONFIG_TASK_IO_ACCOUNTING
2479 static int do_io_accounting(struct task_struct
*task
, char *buffer
, int whole
)
2481 struct task_io_accounting acct
= task
->ioac
;
2482 unsigned long flags
;
2485 result
= mutex_lock_killable(&task
->signal
->cred_guard_mutex
);
2489 if (!ptrace_may_access(task
, PTRACE_MODE_READ
)) {
2494 if (whole
&& lock_task_sighand(task
, &flags
)) {
2495 struct task_struct
*t
= task
;
2497 task_io_accounting_add(&acct
, &task
->signal
->ioac
);
2498 while_each_thread(task
, t
)
2499 task_io_accounting_add(&acct
, &t
->ioac
);
2501 unlock_task_sighand(task
, &flags
);
2503 result
= sprintf(buffer
,
2508 "read_bytes: %llu\n"
2509 "write_bytes: %llu\n"
2510 "cancelled_write_bytes: %llu\n",
2511 (unsigned long long)acct
.rchar
,
2512 (unsigned long long)acct
.wchar
,
2513 (unsigned long long)acct
.syscr
,
2514 (unsigned long long)acct
.syscw
,
2515 (unsigned long long)acct
.read_bytes
,
2516 (unsigned long long)acct
.write_bytes
,
2517 (unsigned long long)acct
.cancelled_write_bytes
);
2519 mutex_unlock(&task
->signal
->cred_guard_mutex
);
2523 static int proc_tid_io_accounting(struct task_struct
*task
, char *buffer
)
2525 return do_io_accounting(task
, buffer
, 0);
2528 static int proc_tgid_io_accounting(struct task_struct
*task
, char *buffer
)
2530 return do_io_accounting(task
, buffer
, 1);
2532 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2534 #ifdef CONFIG_USER_NS
2535 static int proc_id_map_open(struct inode
*inode
, struct file
*file
,
2536 struct seq_operations
*seq_ops
)
2538 struct user_namespace
*ns
= NULL
;
2539 struct task_struct
*task
;
2540 struct seq_file
*seq
;
2543 task
= get_proc_task(inode
);
2546 ns
= get_user_ns(task_cred_xxx(task
, user_ns
));
2548 put_task_struct(task
);
2553 ret
= seq_open(file
, seq_ops
);
2557 seq
= file
->private_data
;
2567 static int proc_id_map_release(struct inode
*inode
, struct file
*file
)
2569 struct seq_file
*seq
= file
->private_data
;
2570 struct user_namespace
*ns
= seq
->private;
2572 return seq_release(inode
, file
);
2575 static int proc_uid_map_open(struct inode
*inode
, struct file
*file
)
2577 return proc_id_map_open(inode
, file
, &proc_uid_seq_operations
);
2580 static int proc_gid_map_open(struct inode
*inode
, struct file
*file
)
2582 return proc_id_map_open(inode
, file
, &proc_gid_seq_operations
);
2585 static int proc_projid_map_open(struct inode
*inode
, struct file
*file
)
2587 return proc_id_map_open(inode
, file
, &proc_projid_seq_operations
);
2590 static const struct file_operations proc_uid_map_operations
= {
2591 .open
= proc_uid_map_open
,
2592 .write
= proc_uid_map_write
,
2594 .llseek
= seq_lseek
,
2595 .release
= proc_id_map_release
,
2598 static const struct file_operations proc_gid_map_operations
= {
2599 .open
= proc_gid_map_open
,
2600 .write
= proc_gid_map_write
,
2602 .llseek
= seq_lseek
,
2603 .release
= proc_id_map_release
,
2606 static const struct file_operations proc_projid_map_operations
= {
2607 .open
= proc_projid_map_open
,
2608 .write
= proc_projid_map_write
,
2610 .llseek
= seq_lseek
,
2611 .release
= proc_id_map_release
,
2613 #endif /* CONFIG_USER_NS */
2615 static int proc_pid_personality(struct seq_file
*m
, struct pid_namespace
*ns
,
2616 struct pid
*pid
, struct task_struct
*task
)
2618 int err
= lock_trace(task
);
2620 seq_printf(m
, "%08x\n", task
->personality
);
2629 static const struct file_operations proc_task_operations
;
2630 static const struct inode_operations proc_task_inode_operations
;
2632 static const struct pid_entry tgid_base_stuff
[] = {
2633 DIR("task", S_IRUGO
|S_IXUGO
, proc_task_inode_operations
, proc_task_operations
),
2634 DIR("fd", S_IRUSR
|S_IXUSR
, proc_fd_inode_operations
, proc_fd_operations
),
2635 #ifdef CONFIG_CHECKPOINT_RESTORE
2636 DIR("map_files", S_IRUSR
|S_IXUSR
, proc_map_files_inode_operations
, proc_map_files_operations
),
2638 DIR("fdinfo", S_IRUSR
|S_IXUSR
, proc_fdinfo_inode_operations
, proc_fdinfo_operations
),
2639 DIR("ns", S_IRUSR
|S_IXUGO
, proc_ns_dir_inode_operations
, proc_ns_dir_operations
),
2641 DIR("net", S_IRUGO
|S_IXUGO
, proc_net_inode_operations
, proc_net_operations
),
2643 REG("environ", S_IRUSR
, proc_environ_operations
),
2644 INF("auxv", S_IRUSR
, proc_pid_auxv
),
2645 ONE("status", S_IRUGO
, proc_pid_status
),
2646 ONE("personality", S_IRUGO
, proc_pid_personality
),
2647 INF("limits", S_IRUGO
, proc_pid_limits
),
2648 #ifdef CONFIG_SCHED_DEBUG
2649 REG("sched", S_IRUGO
|S_IWUSR
, proc_pid_sched_operations
),
2651 #ifdef CONFIG_SCHED_AUTOGROUP
2652 REG("autogroup", S_IRUGO
|S_IWUSR
, proc_pid_sched_autogroup_operations
),
2654 REG("comm", S_IRUGO
|S_IWUSR
, proc_pid_set_comm_operations
),
2655 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2656 INF("syscall", S_IRUGO
, proc_pid_syscall
),
2658 INF("cmdline", S_IRUGO
, proc_pid_cmdline
),
2659 ONE("stat", S_IRUGO
, proc_tgid_stat
),
2660 ONE("statm", S_IRUGO
, proc_pid_statm
),
2661 REG("maps", S_IRUGO
, proc_pid_maps_operations
),
2663 REG("numa_maps", S_IRUGO
, proc_pid_numa_maps_operations
),
2665 REG("mem", S_IRUSR
|S_IWUSR
, proc_mem_operations
),
2666 LNK("cwd", proc_cwd_link
),
2667 LNK("root", proc_root_link
),
2668 LNK("exe", proc_exe_link
),
2669 REG("mounts", S_IRUGO
, proc_mounts_operations
),
2670 REG("mountinfo", S_IRUGO
, proc_mountinfo_operations
),
2671 REG("mountstats", S_IRUSR
, proc_mountstats_operations
),
2672 #ifdef CONFIG_PROC_PAGE_MONITOR
2673 REG("clear_refs", S_IWUSR
, proc_clear_refs_operations
),
2674 REG("smaps", S_IRUGO
, proc_pid_smaps_operations
),
2675 REG("pagemap", S_IRUGO
, proc_pagemap_operations
),
2677 #ifdef CONFIG_SECURITY
2678 DIR("attr", S_IRUGO
|S_IXUGO
, proc_attr_dir_inode_operations
, proc_attr_dir_operations
),
2680 #ifdef CONFIG_KALLSYMS
2681 INF("wchan", S_IRUGO
, proc_pid_wchan
),
2683 #ifdef CONFIG_STACKTRACE
2684 ONE("stack", S_IRUGO
, proc_pid_stack
),
2686 #ifdef CONFIG_SCHEDSTATS
2687 INF("schedstat", S_IRUGO
, proc_pid_schedstat
),
2689 #ifdef CONFIG_LATENCYTOP
2690 REG("latency", S_IRUGO
, proc_lstats_operations
),
2692 #ifdef CONFIG_PROC_PID_CPUSET
2693 REG("cpuset", S_IRUGO
, proc_cpuset_operations
),
2695 #ifdef CONFIG_CGROUPS
2696 REG("cgroup", S_IRUGO
, proc_cgroup_operations
),
2698 INF("oom_score", S_IRUGO
, proc_oom_score
),
2699 REG("oom_adj", S_IRUGO
|S_IWUSR
, proc_oom_adj_operations
),
2700 REG("oom_score_adj", S_IRUGO
|S_IWUSR
, proc_oom_score_adj_operations
),
2701 #ifdef CONFIG_AUDITSYSCALL
2702 REG("loginuid", S_IWUSR
|S_IRUGO
, proc_loginuid_operations
),
2703 REG("sessionid", S_IRUGO
, proc_sessionid_operations
),
2705 #ifdef CONFIG_FAULT_INJECTION
2706 REG("make-it-fail", S_IRUGO
|S_IWUSR
, proc_fault_inject_operations
),
2708 #ifdef CONFIG_ELF_CORE
2709 REG("coredump_filter", S_IRUGO
|S_IWUSR
, proc_coredump_filter_operations
),
2711 #ifdef CONFIG_TASK_IO_ACCOUNTING
2712 INF("io", S_IRUSR
, proc_tgid_io_accounting
),
2714 #ifdef CONFIG_HARDWALL
2715 INF("hardwall", S_IRUGO
, proc_pid_hardwall
),
2717 #ifdef CONFIG_USER_NS
2718 REG("uid_map", S_IRUGO
|S_IWUSR
, proc_uid_map_operations
),
2719 REG("gid_map", S_IRUGO
|S_IWUSR
, proc_gid_map_operations
),
2720 REG("projid_map", S_IRUGO
|S_IWUSR
, proc_projid_map_operations
),
2722 #ifdef CONFIG_CHECKPOINT_RESTORE
2723 REG("timers", S_IRUGO
, proc_timers_operations
),
2727 static int proc_tgid_base_readdir(struct file
* filp
,
2728 void * dirent
, filldir_t filldir
)
2730 return proc_pident_readdir(filp
,dirent
,filldir
,
2731 tgid_base_stuff
,ARRAY_SIZE(tgid_base_stuff
));
2734 static const struct file_operations proc_tgid_base_operations
= {
2735 .read
= generic_read_dir
,
2736 .readdir
= proc_tgid_base_readdir
,
2737 .llseek
= default_llseek
,
2740 static struct dentry
*proc_tgid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, unsigned int flags
)
2742 return proc_pident_lookup(dir
, dentry
,
2743 tgid_base_stuff
, ARRAY_SIZE(tgid_base_stuff
));
2746 static const struct inode_operations proc_tgid_base_inode_operations
= {
2747 .lookup
= proc_tgid_base_lookup
,
2748 .getattr
= pid_getattr
,
2749 .setattr
= proc_setattr
,
2750 .permission
= proc_pid_permission
,
2753 static void proc_flush_task_mnt(struct vfsmount
*mnt
, pid_t pid
, pid_t tgid
)
2755 struct dentry
*dentry
, *leader
, *dir
;
2756 char buf
[PROC_NUMBUF
];
2760 name
.len
= snprintf(buf
, sizeof(buf
), "%d", pid
);
2761 /* no ->d_hash() rejects on procfs */
2762 dentry
= d_hash_and_lookup(mnt
->mnt_root
, &name
);
2764 shrink_dcache_parent(dentry
);
2770 name
.len
= snprintf(buf
, sizeof(buf
), "%d", tgid
);
2771 leader
= d_hash_and_lookup(mnt
->mnt_root
, &name
);
2776 name
.len
= strlen(name
.name
);
2777 dir
= d_hash_and_lookup(leader
, &name
);
2779 goto out_put_leader
;
2782 name
.len
= snprintf(buf
, sizeof(buf
), "%d", pid
);
2783 dentry
= d_hash_and_lookup(dir
, &name
);
2785 shrink_dcache_parent(dentry
);
2798 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2799 * @task: task that should be flushed.
2801 * When flushing dentries from proc, one needs to flush them from global
2802 * proc (proc_mnt) and from all the namespaces' procs this task was seen
2803 * in. This call is supposed to do all of this job.
2805 * Looks in the dcache for
2807 * /proc/@tgid/task/@pid
2808 * if either directory is present flushes it and all of it'ts children
2811 * It is safe and reasonable to cache /proc entries for a task until
2812 * that task exits. After that they just clog up the dcache with
2813 * useless entries, possibly causing useful dcache entries to be
2814 * flushed instead. This routine is proved to flush those useless
2815 * dcache entries at process exit time.
2817 * NOTE: This routine is just an optimization so it does not guarantee
2818 * that no dcache entries will exist at process exit time it
2819 * just makes it very unlikely that any will persist.
2822 void proc_flush_task(struct task_struct
*task
)
2825 struct pid
*pid
, *tgid
;
2828 pid
= task_pid(task
);
2829 tgid
= task_tgid(task
);
2831 for (i
= 0; i
<= pid
->level
; i
++) {
2832 upid
= &pid
->numbers
[i
];
2833 proc_flush_task_mnt(upid
->ns
->proc_mnt
, upid
->nr
,
2834 tgid
->numbers
[i
].nr
);
2838 static struct dentry
*proc_pid_instantiate(struct inode
*dir
,
2839 struct dentry
* dentry
,
2840 struct task_struct
*task
, const void *ptr
)
2842 struct dentry
*error
= ERR_PTR(-ENOENT
);
2843 struct inode
*inode
;
2845 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
2849 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
2850 inode
->i_op
= &proc_tgid_base_inode_operations
;
2851 inode
->i_fop
= &proc_tgid_base_operations
;
2852 inode
->i_flags
|=S_IMMUTABLE
;
2854 set_nlink(inode
, 2 + pid_entry_count_dirs(tgid_base_stuff
,
2855 ARRAY_SIZE(tgid_base_stuff
)));
2857 d_set_d_op(dentry
, &pid_dentry_operations
);
2859 d_add(dentry
, inode
);
2860 /* Close the race of the process dying before we return the dentry */
2861 if (pid_revalidate(dentry
, 0))
2867 struct dentry
*proc_pid_lookup(struct inode
*dir
, struct dentry
* dentry
, unsigned int flags
)
2869 struct dentry
*result
= NULL
;
2870 struct task_struct
*task
;
2872 struct pid_namespace
*ns
;
2874 tgid
= name_to_int(dentry
);
2878 ns
= dentry
->d_sb
->s_fs_info
;
2880 task
= find_task_by_pid_ns(tgid
, ns
);
2882 get_task_struct(task
);
2887 result
= proc_pid_instantiate(dir
, dentry
, task
, NULL
);
2888 put_task_struct(task
);
2894 * Find the first task with tgid >= tgid
2899 struct task_struct
*task
;
2901 static struct tgid_iter
next_tgid(struct pid_namespace
*ns
, struct tgid_iter iter
)
2906 put_task_struct(iter
.task
);
2910 pid
= find_ge_pid(iter
.tgid
, ns
);
2912 iter
.tgid
= pid_nr_ns(pid
, ns
);
2913 iter
.task
= pid_task(pid
, PIDTYPE_PID
);
2914 /* What we to know is if the pid we have find is the
2915 * pid of a thread_group_leader. Testing for task
2916 * being a thread_group_leader is the obvious thing
2917 * todo but there is a window when it fails, due to
2918 * the pid transfer logic in de_thread.
2920 * So we perform the straight forward test of seeing
2921 * if the pid we have found is the pid of a thread
2922 * group leader, and don't worry if the task we have
2923 * found doesn't happen to be a thread group leader.
2924 * As we don't care in the case of readdir.
2926 if (!iter
.task
|| !has_group_leader_pid(iter
.task
)) {
2930 get_task_struct(iter
.task
);
2936 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + 1)
2938 static int proc_pid_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
2939 struct tgid_iter iter
)
2941 char name
[PROC_NUMBUF
];
2942 int len
= snprintf(name
, sizeof(name
), "%d", iter
.tgid
);
2943 return proc_fill_cache(filp
, dirent
, filldir
, name
, len
,
2944 proc_pid_instantiate
, iter
.task
, NULL
);
2947 static int fake_filldir(void *buf
, const char *name
, int namelen
,
2948 loff_t offset
, u64 ino
, unsigned d_type
)
2953 /* for the /proc/ directory itself, after non-process stuff has been done */
2954 int proc_pid_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
2956 struct tgid_iter iter
;
2957 struct pid_namespace
*ns
;
2958 filldir_t __filldir
;
2959 loff_t pos
= filp
->f_pos
;
2961 if (pos
>= PID_MAX_LIMIT
+ TGID_OFFSET
)
2964 if (pos
== TGID_OFFSET
- 1) {
2965 if (proc_fill_cache(filp
, dirent
, filldir
, "self", 4,
2966 NULL
, NULL
, NULL
) < 0)
2970 iter
.tgid
= pos
- TGID_OFFSET
;
2973 ns
= filp
->f_dentry
->d_sb
->s_fs_info
;
2974 for (iter
= next_tgid(ns
, iter
);
2976 iter
.tgid
+= 1, iter
= next_tgid(ns
, iter
)) {
2977 if (has_pid_permissions(ns
, iter
.task
, 2))
2978 __filldir
= filldir
;
2980 __filldir
= fake_filldir
;
2982 filp
->f_pos
= iter
.tgid
+ TGID_OFFSET
;
2983 if (proc_pid_fill_cache(filp
, dirent
, __filldir
, iter
) < 0) {
2984 put_task_struct(iter
.task
);
2988 filp
->f_pos
= PID_MAX_LIMIT
+ TGID_OFFSET
;
2996 static const struct pid_entry tid_base_stuff
[] = {
2997 DIR("fd", S_IRUSR
|S_IXUSR
, proc_fd_inode_operations
, proc_fd_operations
),
2998 DIR("fdinfo", S_IRUSR
|S_IXUSR
, proc_fdinfo_inode_operations
, proc_fdinfo_operations
),
2999 DIR("ns", S_IRUSR
|S_IXUGO
, proc_ns_dir_inode_operations
, proc_ns_dir_operations
),
3000 REG("environ", S_IRUSR
, proc_environ_operations
),
3001 INF("auxv", S_IRUSR
, proc_pid_auxv
),
3002 ONE("status", S_IRUGO
, proc_pid_status
),
3003 ONE("personality", S_IRUGO
, proc_pid_personality
),
3004 INF("limits", S_IRUGO
, proc_pid_limits
),
3005 #ifdef CONFIG_SCHED_DEBUG
3006 REG("sched", S_IRUGO
|S_IWUSR
, proc_pid_sched_operations
),
3008 REG("comm", S_IRUGO
|S_IWUSR
, proc_pid_set_comm_operations
),
3009 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3010 INF("syscall", S_IRUGO
, proc_pid_syscall
),
3012 INF("cmdline", S_IRUGO
, proc_pid_cmdline
),
3013 ONE("stat", S_IRUGO
, proc_tid_stat
),
3014 ONE("statm", S_IRUGO
, proc_pid_statm
),
3015 REG("maps", S_IRUGO
, proc_tid_maps_operations
),
3016 #ifdef CONFIG_CHECKPOINT_RESTORE
3017 REG("children", S_IRUGO
, proc_tid_children_operations
),
3020 REG("numa_maps", S_IRUGO
, proc_tid_numa_maps_operations
),
3022 REG("mem", S_IRUSR
|S_IWUSR
, proc_mem_operations
),
3023 LNK("cwd", proc_cwd_link
),
3024 LNK("root", proc_root_link
),
3025 LNK("exe", proc_exe_link
),
3026 REG("mounts", S_IRUGO
, proc_mounts_operations
),
3027 REG("mountinfo", S_IRUGO
, proc_mountinfo_operations
),
3028 #ifdef CONFIG_PROC_PAGE_MONITOR
3029 REG("clear_refs", S_IWUSR
, proc_clear_refs_operations
),
3030 REG("smaps", S_IRUGO
, proc_tid_smaps_operations
),
3031 REG("pagemap", S_IRUGO
, proc_pagemap_operations
),
3033 #ifdef CONFIG_SECURITY
3034 DIR("attr", S_IRUGO
|S_IXUGO
, proc_attr_dir_inode_operations
, proc_attr_dir_operations
),
3036 #ifdef CONFIG_KALLSYMS
3037 INF("wchan", S_IRUGO
, proc_pid_wchan
),
3039 #ifdef CONFIG_STACKTRACE
3040 ONE("stack", S_IRUGO
, proc_pid_stack
),
3042 #ifdef CONFIG_SCHEDSTATS
3043 INF("schedstat", S_IRUGO
, proc_pid_schedstat
),
3045 #ifdef CONFIG_LATENCYTOP
3046 REG("latency", S_IRUGO
, proc_lstats_operations
),
3048 #ifdef CONFIG_PROC_PID_CPUSET
3049 REG("cpuset", S_IRUGO
, proc_cpuset_operations
),
3051 #ifdef CONFIG_CGROUPS
3052 REG("cgroup", S_IRUGO
, proc_cgroup_operations
),
3054 INF("oom_score", S_IRUGO
, proc_oom_score
),
3055 REG("oom_adj", S_IRUGO
|S_IWUSR
, proc_oom_adj_operations
),
3056 REG("oom_score_adj", S_IRUGO
|S_IWUSR
, proc_oom_score_adj_operations
),
3057 #ifdef CONFIG_AUDITSYSCALL
3058 REG("loginuid", S_IWUSR
|S_IRUGO
, proc_loginuid_operations
),
3059 REG("sessionid", S_IRUGO
, proc_sessionid_operations
),
3061 #ifdef CONFIG_FAULT_INJECTION
3062 REG("make-it-fail", S_IRUGO
|S_IWUSR
, proc_fault_inject_operations
),
3064 #ifdef CONFIG_TASK_IO_ACCOUNTING
3065 INF("io", S_IRUSR
, proc_tid_io_accounting
),
3067 #ifdef CONFIG_HARDWALL
3068 INF("hardwall", S_IRUGO
, proc_pid_hardwall
),
3070 #ifdef CONFIG_USER_NS
3071 REG("uid_map", S_IRUGO
|S_IWUSR
, proc_uid_map_operations
),
3072 REG("gid_map", S_IRUGO
|S_IWUSR
, proc_gid_map_operations
),
3073 REG("projid_map", S_IRUGO
|S_IWUSR
, proc_projid_map_operations
),
3077 static int proc_tid_base_readdir(struct file
* filp
,
3078 void * dirent
, filldir_t filldir
)
3080 return proc_pident_readdir(filp
,dirent
,filldir
,
3081 tid_base_stuff
,ARRAY_SIZE(tid_base_stuff
));
3084 static struct dentry
*proc_tid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, unsigned int flags
)
3086 return proc_pident_lookup(dir
, dentry
,
3087 tid_base_stuff
, ARRAY_SIZE(tid_base_stuff
));
3090 static const struct file_operations proc_tid_base_operations
= {
3091 .read
= generic_read_dir
,
3092 .readdir
= proc_tid_base_readdir
,
3093 .llseek
= default_llseek
,
3096 static const struct inode_operations proc_tid_base_inode_operations
= {
3097 .lookup
= proc_tid_base_lookup
,
3098 .getattr
= pid_getattr
,
3099 .setattr
= proc_setattr
,
3102 static struct dentry
*proc_task_instantiate(struct inode
*dir
,
3103 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
3105 struct dentry
*error
= ERR_PTR(-ENOENT
);
3106 struct inode
*inode
;
3107 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
3111 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
3112 inode
->i_op
= &proc_tid_base_inode_operations
;
3113 inode
->i_fop
= &proc_tid_base_operations
;
3114 inode
->i_flags
|=S_IMMUTABLE
;
3116 set_nlink(inode
, 2 + pid_entry_count_dirs(tid_base_stuff
,
3117 ARRAY_SIZE(tid_base_stuff
)));
3119 d_set_d_op(dentry
, &pid_dentry_operations
);
3121 d_add(dentry
, inode
);
3122 /* Close the race of the process dying before we return the dentry */
3123 if (pid_revalidate(dentry
, 0))
3129 static struct dentry
*proc_task_lookup(struct inode
*dir
, struct dentry
* dentry
, unsigned int flags
)
3131 struct dentry
*result
= ERR_PTR(-ENOENT
);
3132 struct task_struct
*task
;
3133 struct task_struct
*leader
= get_proc_task(dir
);
3135 struct pid_namespace
*ns
;
3140 tid
= name_to_int(dentry
);
3144 ns
= dentry
->d_sb
->s_fs_info
;
3146 task
= find_task_by_pid_ns(tid
, ns
);
3148 get_task_struct(task
);
3152 if (!same_thread_group(leader
, task
))
3155 result
= proc_task_instantiate(dir
, dentry
, task
, NULL
);
3157 put_task_struct(task
);
3159 put_task_struct(leader
);
3165 * Find the first tid of a thread group to return to user space.
3167 * Usually this is just the thread group leader, but if the users
3168 * buffer was too small or there was a seek into the middle of the
3169 * directory we have more work todo.
3171 * In the case of a short read we start with find_task_by_pid.
3173 * In the case of a seek we start with the leader and walk nr
3176 static struct task_struct
*first_tid(struct task_struct
*leader
,
3177 int tid
, int nr
, struct pid_namespace
*ns
)
3179 struct task_struct
*pos
;
3182 /* Attempt to start with the pid of a thread */
3183 if (tid
&& (nr
> 0)) {
3184 pos
= find_task_by_pid_ns(tid
, ns
);
3185 if (pos
&& (pos
->group_leader
== leader
))
3189 /* If nr exceeds the number of threads there is nothing todo */
3191 if (nr
&& nr
>= get_nr_threads(leader
))
3194 /* If we haven't found our starting place yet start
3195 * with the leader and walk nr threads forward.
3197 for (pos
= leader
; nr
> 0; --nr
) {
3198 pos
= next_thread(pos
);
3199 if (pos
== leader
) {
3205 get_task_struct(pos
);
3212 * Find the next thread in the thread list.
3213 * Return NULL if there is an error or no next thread.
3215 * The reference to the input task_struct is released.
3217 static struct task_struct
*next_tid(struct task_struct
*start
)
3219 struct task_struct
*pos
= NULL
;
3221 if (pid_alive(start
)) {
3222 pos
= next_thread(start
);
3223 if (thread_group_leader(pos
))
3226 get_task_struct(pos
);
3229 put_task_struct(start
);
3233 static int proc_task_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
3234 struct task_struct
*task
, int tid
)
3236 char name
[PROC_NUMBUF
];
3237 int len
= snprintf(name
, sizeof(name
), "%d", tid
);
3238 return proc_fill_cache(filp
, dirent
, filldir
, name
, len
,
3239 proc_task_instantiate
, task
, NULL
);
3242 /* for the /proc/TGID/task/ directories */
3243 static int proc_task_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
3245 struct dentry
*dentry
= filp
->f_path
.dentry
;
3246 struct inode
*inode
= dentry
->d_inode
;
3247 struct task_struct
*leader
= NULL
;
3248 struct task_struct
*task
;
3249 int retval
= -ENOENT
;
3252 struct pid_namespace
*ns
;
3254 task
= get_proc_task(inode
);
3258 if (pid_alive(task
)) {
3259 leader
= task
->group_leader
;
3260 get_task_struct(leader
);
3263 put_task_struct(task
);
3268 switch ((unsigned long)filp
->f_pos
) {
3271 if (filldir(dirent
, ".", 1, filp
->f_pos
, ino
, DT_DIR
) < 0)
3276 ino
= parent_ino(dentry
);
3277 if (filldir(dirent
, "..", 2, filp
->f_pos
, ino
, DT_DIR
) < 0)
3283 /* f_version caches the tgid value that the last readdir call couldn't
3284 * return. lseek aka telldir automagically resets f_version to 0.
3286 ns
= filp
->f_dentry
->d_sb
->s_fs_info
;
3287 tid
= (int)filp
->f_version
;
3288 filp
->f_version
= 0;
3289 for (task
= first_tid(leader
, tid
, filp
->f_pos
- 2, ns
);
3291 task
= next_tid(task
), filp
->f_pos
++) {
3292 tid
= task_pid_nr_ns(task
, ns
);
3293 if (proc_task_fill_cache(filp
, dirent
, filldir
, task
, tid
) < 0) {
3294 /* returning this tgid failed, save it as the first
3295 * pid for the next readir call */
3296 filp
->f_version
= (u64
)tid
;
3297 put_task_struct(task
);
3302 put_task_struct(leader
);
3307 static int proc_task_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
3309 struct inode
*inode
= dentry
->d_inode
;
3310 struct task_struct
*p
= get_proc_task(inode
);
3311 generic_fillattr(inode
, stat
);
3314 stat
->nlink
+= get_nr_threads(p
);
3321 static const struct inode_operations proc_task_inode_operations
= {
3322 .lookup
= proc_task_lookup
,
3323 .getattr
= proc_task_getattr
,
3324 .setattr
= proc_setattr
,
3325 .permission
= proc_pid_permission
,
3328 static const struct file_operations proc_task_operations
= {
3329 .read
= generic_read_dir
,
3330 .readdir
= proc_task_readdir
,
3331 .llseek
= default_llseek
,