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 } )
142 /* ANDROID is for special files in /proc. */
143 #define ANDROID(NAME, MODE, OTYPE) \
144 NOD(NAME, (S_IFREG|(MODE)), \
145 &proc_##OTYPE##_inode_operations, \
146 &proc_##OTYPE##_operations, {})
149 * Count the number of hardlinks for the pid_entry table, excluding the .
152 static unsigned int pid_entry_count_dirs(const struct pid_entry
*entries
,
159 for (i
= 0; i
< n
; ++i
) {
160 if (S_ISDIR(entries
[i
].mode
))
167 static int get_task_root(struct task_struct
*task
, struct path
*root
)
169 int result
= -ENOENT
;
173 get_fs_root(task
->fs
, root
);
180 static int proc_cwd_link(struct dentry
*dentry
, struct path
*path
)
182 struct task_struct
*task
= get_proc_task(dentry
->d_inode
);
183 int result
= -ENOENT
;
188 get_fs_pwd(task
->fs
, path
);
192 put_task_struct(task
);
197 static int proc_root_link(struct dentry
*dentry
, struct path
*path
)
199 struct task_struct
*task
= get_proc_task(dentry
->d_inode
);
200 int result
= -ENOENT
;
203 result
= get_task_root(task
, path
);
204 put_task_struct(task
);
209 static int proc_pid_cmdline(struct task_struct
*task
, char * buffer
)
213 struct mm_struct
*mm
= get_task_mm(task
);
217 goto out_mm
; /* Shh! No looking before we're done */
219 len
= mm
->arg_end
- mm
->arg_start
;
224 res
= access_process_vm(task
, mm
->arg_start
, buffer
, len
, 0);
226 // If the nul at the end of args has been overwritten, then
227 // assume application is using setproctitle(3).
228 if (res
> 0 && buffer
[res
-1] != '\0' && len
< PAGE_SIZE
) {
229 len
= strnlen(buffer
, res
);
233 len
= mm
->env_end
- mm
->env_start
;
234 if (len
> PAGE_SIZE
- res
)
235 len
= PAGE_SIZE
- res
;
236 res
+= access_process_vm(task
, mm
->env_start
, buffer
+res
, len
, 0);
237 res
= strnlen(buffer
, res
);
246 static int proc_pid_auxv(struct task_struct
*task
, char *buffer
)
248 struct mm_struct
*mm
= mm_access(task
, PTRACE_MODE_READ
);
249 int res
= PTR_ERR(mm
);
250 if (mm
&& !IS_ERR(mm
)) {
251 unsigned int nwords
= 0;
254 } while (mm
->saved_auxv
[nwords
- 2] != 0); /* AT_NULL */
255 res
= nwords
* sizeof(mm
->saved_auxv
[0]);
258 memcpy(buffer
, mm
->saved_auxv
, res
);
265 #ifdef CONFIG_KALLSYMS
267 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
268 * Returns the resolved symbol. If that fails, simply return the address.
270 static int proc_pid_wchan(struct task_struct
*task
, char *buffer
)
273 char symname
[KSYM_NAME_LEN
];
275 wchan
= get_wchan(task
);
277 if (lookup_symbol_name(wchan
, symname
) < 0)
278 if (!ptrace_may_access(task
, PTRACE_MODE_READ
))
281 return sprintf(buffer
, "%lu", wchan
);
283 return sprintf(buffer
, "%s", symname
);
285 #endif /* CONFIG_KALLSYMS */
287 static int lock_trace(struct task_struct
*task
)
289 int err
= mutex_lock_killable(&task
->signal
->cred_guard_mutex
);
292 if (!ptrace_may_access(task
, PTRACE_MODE_ATTACH
)) {
293 mutex_unlock(&task
->signal
->cred_guard_mutex
);
299 static void unlock_trace(struct task_struct
*task
)
301 mutex_unlock(&task
->signal
->cred_guard_mutex
);
304 #ifdef CONFIG_STACKTRACE
306 #define MAX_STACK_TRACE_DEPTH 64
308 static int proc_pid_stack(struct seq_file
*m
, struct pid_namespace
*ns
,
309 struct pid
*pid
, struct task_struct
*task
)
311 struct stack_trace trace
;
312 unsigned long *entries
;
316 entries
= kmalloc(MAX_STACK_TRACE_DEPTH
* sizeof(*entries
), GFP_KERNEL
);
320 trace
.nr_entries
= 0;
321 trace
.max_entries
= MAX_STACK_TRACE_DEPTH
;
322 trace
.entries
= entries
;
325 err
= lock_trace(task
);
327 save_stack_trace_tsk(task
, &trace
);
329 for (i
= 0; i
< trace
.nr_entries
; i
++) {
330 seq_printf(m
, "[<%pK>] %pS\n",
331 (void *)entries
[i
], (void *)entries
[i
]);
341 #ifdef CONFIG_SCHEDSTATS
343 * Provides /proc/PID/schedstat
345 static int proc_pid_schedstat(struct task_struct
*task
, char *buffer
)
347 return sprintf(buffer
, "%llu %llu %lu\n",
348 (unsigned long long)task
->se
.sum_exec_runtime
,
349 (unsigned long long)task
->sched_info
.run_delay
,
350 task
->sched_info
.pcount
);
354 #ifdef CONFIG_LATENCYTOP
355 static int lstats_show_proc(struct seq_file
*m
, void *v
)
358 struct inode
*inode
= m
->private;
359 struct task_struct
*task
= get_proc_task(inode
);
363 seq_puts(m
, "Latency Top version : v0.1\n");
364 for (i
= 0; i
< 32; i
++) {
365 struct latency_record
*lr
= &task
->latency_record
[i
];
366 if (lr
->backtrace
[0]) {
368 seq_printf(m
, "%i %li %li",
369 lr
->count
, lr
->time
, lr
->max
);
370 for (q
= 0; q
< LT_BACKTRACEDEPTH
; q
++) {
371 unsigned long bt
= lr
->backtrace
[q
];
376 seq_printf(m
, " %ps", (void *)bt
);
382 put_task_struct(task
);
386 static int lstats_open(struct inode
*inode
, struct file
*file
)
388 return single_open(file
, lstats_show_proc
, inode
);
391 static ssize_t
lstats_write(struct file
*file
, const char __user
*buf
,
392 size_t count
, loff_t
*offs
)
394 struct task_struct
*task
= get_proc_task(file_inode(file
));
398 clear_all_latency_tracing(task
);
399 put_task_struct(task
);
404 static const struct file_operations proc_lstats_operations
= {
407 .write
= lstats_write
,
409 .release
= single_release
,
414 #ifdef CONFIG_CGROUPS
415 static int cgroup_open(struct inode
*inode
, struct file
*file
)
417 struct pid
*pid
= PROC_I(inode
)->pid
;
418 return single_open(file
, proc_cgroup_show
, pid
);
421 static const struct file_operations proc_cgroup_operations
= {
425 .release
= single_release
,
429 #ifdef CONFIG_PROC_PID_CPUSET
431 static int cpuset_open(struct inode
*inode
, struct file
*file
)
433 struct pid
*pid
= PROC_I(inode
)->pid
;
434 return single_open(file
, proc_cpuset_show
, pid
);
437 static const struct file_operations proc_cpuset_operations
= {
441 .release
= single_release
,
445 static int proc_oom_score(struct task_struct
*task
, char *buffer
)
447 unsigned long totalpages
= totalram_pages
+ total_swap_pages
;
448 unsigned long points
= 0;
450 read_lock(&tasklist_lock
);
452 points
= oom_badness(task
, NULL
, NULL
, totalpages
) *
454 read_unlock(&tasklist_lock
);
455 return sprintf(buffer
, "%lu\n", points
);
463 static const struct limit_names lnames
[RLIM_NLIMITS
] = {
464 [RLIMIT_CPU
] = {"Max cpu time", "seconds"},
465 [RLIMIT_FSIZE
] = {"Max file size", "bytes"},
466 [RLIMIT_DATA
] = {"Max data size", "bytes"},
467 [RLIMIT_STACK
] = {"Max stack size", "bytes"},
468 [RLIMIT_CORE
] = {"Max core file size", "bytes"},
469 [RLIMIT_RSS
] = {"Max resident set", "bytes"},
470 [RLIMIT_NPROC
] = {"Max processes", "processes"},
471 [RLIMIT_NOFILE
] = {"Max open files", "files"},
472 [RLIMIT_MEMLOCK
] = {"Max locked memory", "bytes"},
473 [RLIMIT_AS
] = {"Max address space", "bytes"},
474 [RLIMIT_LOCKS
] = {"Max file locks", "locks"},
475 [RLIMIT_SIGPENDING
] = {"Max pending signals", "signals"},
476 [RLIMIT_MSGQUEUE
] = {"Max msgqueue size", "bytes"},
477 [RLIMIT_NICE
] = {"Max nice priority", NULL
},
478 [RLIMIT_RTPRIO
] = {"Max realtime priority", NULL
},
479 [RLIMIT_RTTIME
] = {"Max realtime timeout", "us"},
482 /* Display limits for a process */
483 static int proc_pid_limits(struct task_struct
*task
, char *buffer
)
488 char *bufptr
= buffer
;
490 struct rlimit rlim
[RLIM_NLIMITS
];
492 if (!lock_task_sighand(task
, &flags
))
494 memcpy(rlim
, task
->signal
->rlim
, sizeof(struct rlimit
) * RLIM_NLIMITS
);
495 unlock_task_sighand(task
, &flags
);
498 * print the file header
500 count
+= sprintf(&bufptr
[count
], "%-25s %-20s %-20s %-10s\n",
501 "Limit", "Soft Limit", "Hard Limit", "Units");
503 for (i
= 0; i
< RLIM_NLIMITS
; i
++) {
504 if (rlim
[i
].rlim_cur
== RLIM_INFINITY
)
505 count
+= sprintf(&bufptr
[count
], "%-25s %-20s ",
506 lnames
[i
].name
, "unlimited");
508 count
+= sprintf(&bufptr
[count
], "%-25s %-20lu ",
509 lnames
[i
].name
, rlim
[i
].rlim_cur
);
511 if (rlim
[i
].rlim_max
== RLIM_INFINITY
)
512 count
+= sprintf(&bufptr
[count
], "%-20s ", "unlimited");
514 count
+= sprintf(&bufptr
[count
], "%-20lu ",
518 count
+= sprintf(&bufptr
[count
], "%-10s\n",
521 count
+= sprintf(&bufptr
[count
], "\n");
527 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
528 static int proc_pid_syscall(struct task_struct
*task
, char *buffer
)
531 unsigned long args
[6], sp
, pc
;
532 int res
= lock_trace(task
);
536 if (task_current_syscall(task
, &nr
, args
, 6, &sp
, &pc
))
537 res
= sprintf(buffer
, "running\n");
539 res
= sprintf(buffer
, "%ld 0x%lx 0x%lx\n", nr
, sp
, pc
);
541 res
= sprintf(buffer
,
542 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
544 args
[0], args
[1], args
[2], args
[3], args
[4], args
[5],
549 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
551 /************************************************************************/
552 /* Here the fs part begins */
553 /************************************************************************/
555 /* permission checks */
556 static int proc_fd_access_allowed(struct inode
*inode
)
558 struct task_struct
*task
;
560 /* Allow access to a task's file descriptors if it is us or we
561 * may use ptrace attach to the process and find out that
564 task
= get_proc_task(inode
);
566 allowed
= ptrace_may_access(task
, PTRACE_MODE_READ
);
567 put_task_struct(task
);
572 int proc_setattr(struct dentry
*dentry
, struct iattr
*attr
)
575 struct inode
*inode
= dentry
->d_inode
;
577 if (attr
->ia_valid
& ATTR_MODE
)
580 error
= inode_change_ok(inode
, attr
);
584 setattr_copy(inode
, attr
);
585 mark_inode_dirty(inode
);
590 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
591 * or euid/egid (for hide_pid_min=2)?
593 static bool has_pid_permissions(struct pid_namespace
*pid
,
594 struct task_struct
*task
,
597 if (pid
->hide_pid
< hide_pid_min
)
599 if (in_group_p(pid
->pid_gid
))
601 return ptrace_may_access(task
, PTRACE_MODE_READ
);
605 static int proc_pid_permission(struct inode
*inode
, int mask
)
607 struct pid_namespace
*pid
= inode
->i_sb
->s_fs_info
;
608 struct task_struct
*task
;
611 task
= get_proc_task(inode
);
614 has_perms
= has_pid_permissions(pid
, task
, 1);
615 put_task_struct(task
);
618 if (pid
->hide_pid
== 2) {
620 * Let's make getdents(), stat(), and open()
621 * consistent with each other. If a process
622 * may not stat() a file, it shouldn't be seen
630 return generic_permission(inode
, mask
);
635 static const struct inode_operations proc_def_inode_operations
= {
636 .setattr
= proc_setattr
,
639 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
641 static ssize_t
proc_info_read(struct file
* file
, char __user
* buf
,
642 size_t count
, loff_t
*ppos
)
644 struct inode
* inode
= file_inode(file
);
647 struct task_struct
*task
= get_proc_task(inode
);
653 if (count
> PROC_BLOCK_SIZE
)
654 count
= PROC_BLOCK_SIZE
;
657 if (!(page
= __get_free_page(GFP_TEMPORARY
)))
660 length
= PROC_I(inode
)->op
.proc_read(task
, (char*)page
);
663 length
= simple_read_from_buffer(buf
, count
, ppos
, (char *)page
, length
);
666 put_task_struct(task
);
671 static const struct file_operations proc_info_file_operations
= {
672 .read
= proc_info_read
,
673 .llseek
= generic_file_llseek
,
676 static int proc_single_show(struct seq_file
*m
, void *v
)
678 struct inode
*inode
= m
->private;
679 struct pid_namespace
*ns
;
681 struct task_struct
*task
;
684 ns
= inode
->i_sb
->s_fs_info
;
685 pid
= proc_pid(inode
);
686 task
= get_pid_task(pid
, PIDTYPE_PID
);
690 ret
= PROC_I(inode
)->op
.proc_show(m
, ns
, pid
, task
);
692 put_task_struct(task
);
696 static int proc_single_open(struct inode
*inode
, struct file
*filp
)
698 return single_open(filp
, proc_single_show
, inode
);
701 static const struct file_operations proc_single_file_operations
= {
702 .open
= proc_single_open
,
705 .release
= single_release
,
708 static int __mem_open(struct inode
*inode
, struct file
*file
, unsigned int mode
)
710 struct task_struct
*task
= get_proc_task(file_inode(file
));
711 struct mm_struct
*mm
;
716 mm
= mm_access(task
, mode
);
717 put_task_struct(task
);
723 /* ensure this mm_struct can't be freed */
724 atomic_inc(&mm
->mm_count
);
725 /* but do not pin its memory */
729 file
->private_data
= mm
;
734 static int mem_open(struct inode
*inode
, struct file
*file
)
736 int ret
= __mem_open(inode
, file
, PTRACE_MODE_ATTACH
);
738 /* OK to pass negative loff_t, we can catch out-of-range */
739 file
->f_mode
|= FMODE_UNSIGNED_OFFSET
;
744 static ssize_t
mem_rw(struct file
*file
, char __user
*buf
,
745 size_t count
, loff_t
*ppos
, int write
)
747 struct mm_struct
*mm
= file
->private_data
;
748 unsigned long addr
= *ppos
;
755 page
= (char *)__get_free_page(GFP_TEMPORARY
);
760 if (!atomic_inc_not_zero(&mm
->mm_users
))
764 int this_len
= min_t(int, count
, PAGE_SIZE
);
766 if (write
&& copy_from_user(page
, buf
, this_len
)) {
771 this_len
= access_remote_vm(mm
, addr
, page
, this_len
, write
);
778 if (!write
&& copy_to_user(buf
, page
, this_len
)) {
792 free_page((unsigned long) page
);
796 static ssize_t
mem_read(struct file
*file
, char __user
*buf
,
797 size_t count
, loff_t
*ppos
)
799 return mem_rw(file
, buf
, count
, ppos
, 0);
802 static ssize_t
mem_write(struct file
*file
, const char __user
*buf
,
803 size_t count
, loff_t
*ppos
)
805 return mem_rw(file
, (char __user
*)buf
, count
, ppos
, 1);
808 loff_t
mem_lseek(struct file
*file
, loff_t offset
, int orig
)
812 file
->f_pos
= offset
;
815 file
->f_pos
+= offset
;
820 force_successful_syscall_return();
824 static int mem_release(struct inode
*inode
, struct file
*file
)
826 struct mm_struct
*mm
= file
->private_data
;
832 static const struct file_operations proc_mem_operations
= {
837 .release
= mem_release
,
840 static int environ_open(struct inode
*inode
, struct file
*file
)
842 return __mem_open(inode
, file
, PTRACE_MODE_READ
);
845 static ssize_t
environ_read(struct file
*file
, char __user
*buf
,
846 size_t count
, loff_t
*ppos
)
849 unsigned long src
= *ppos
;
851 struct mm_struct
*mm
= file
->private_data
;
853 /* Ensure the process spawned far enough to have an environment. */
854 if (!mm
|| !mm
->env_end
)
857 page
= (char *)__get_free_page(GFP_TEMPORARY
);
862 if (!atomic_inc_not_zero(&mm
->mm_users
))
865 size_t this_len
, max_len
;
868 if (src
>= (mm
->env_end
- mm
->env_start
))
871 this_len
= mm
->env_end
- (mm
->env_start
+ src
);
873 max_len
= min_t(size_t, PAGE_SIZE
, count
);
874 this_len
= min(max_len
, this_len
);
876 retval
= access_remote_vm(mm
, (mm
->env_start
+ src
),
884 if (copy_to_user(buf
, page
, retval
)) {
898 free_page((unsigned long) page
);
902 static const struct file_operations proc_environ_operations
= {
903 .open
= environ_open
,
904 .read
= environ_read
,
905 .llseek
= generic_file_llseek
,
906 .release
= mem_release
,
909 static ssize_t
oom_adj_read(struct file
*file
, char __user
*buf
, size_t count
,
912 struct task_struct
*task
= get_proc_task(file_inode(file
));
913 char buffer
[PROC_NUMBUF
];
914 int oom_adj
= OOM_ADJUST_MIN
;
920 if (lock_task_sighand(task
, &flags
)) {
921 if (task
->signal
->oom_score_adj
== OOM_SCORE_ADJ_MAX
)
922 oom_adj
= OOM_ADJUST_MAX
;
924 oom_adj
= ((task
->signal
->oom_score_adj
* -OOM_DISABLE
* 10)/OOM_SCORE_ADJ_MAX
+5)
925 /10; //modify for oom_score_adj->oom_adj round
926 unlock_task_sighand(task
, &flags
);
928 put_task_struct(task
);
929 len
= snprintf(buffer
, sizeof(buffer
), "%d\n", oom_adj
);
930 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
933 static ssize_t
oom_adj_write(struct file
*file
, const char __user
*buf
,
934 size_t count
, loff_t
*ppos
)
936 struct task_struct
*task
;
937 char buffer
[PROC_NUMBUF
];
942 memset(buffer
, 0, sizeof(buffer
));
943 if (count
> sizeof(buffer
) - 1)
944 count
= sizeof(buffer
) - 1;
945 if (copy_from_user(buffer
, buf
, count
)) {
950 err
= kstrtoint(strstrip(buffer
), 0, &oom_adj
);
953 if ((oom_adj
< OOM_ADJUST_MIN
|| oom_adj
> OOM_ADJUST_MAX
) &&
954 oom_adj
!= OOM_DISABLE
) {
959 task
= get_proc_task(file_inode(file
));
971 if (!lock_task_sighand(task
, &flags
)) {
977 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
978 * value is always attainable.
980 if (oom_adj
== OOM_ADJUST_MAX
)
981 oom_adj
= OOM_SCORE_ADJ_MAX
;
983 oom_adj
= ((oom_adj
* OOM_SCORE_ADJ_MAX
* 10) / -OOM_DISABLE
+ 5)/10; //modify for oom_adj->oom_score_adj round
985 if (oom_adj
< task
->signal
->oom_score_adj
&&
986 !capable(CAP_SYS_RESOURCE
)) {
992 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
993 * /proc/pid/oom_score_adj instead.
995 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
996 current
->comm
, task_pid_nr(current
), task_pid_nr(task
),
999 task
->signal
->oom_score_adj
= oom_adj
;
1000 trace_oom_score_adj_update(task
);
1002 unlock_task_sighand(task
, &flags
);
1005 put_task_struct(task
);
1007 return err
< 0 ? err
: count
;
1010 static int oom_adjust_permission(struct inode
*inode
, int mask
)
1013 struct task_struct
*p
;
1015 p
= get_proc_task(inode
);
1022 * System Server (uid == 1000) is granted access to oom_adj of all
1023 * android applications (uid > 10000) as and services (uid >= 1000)
1025 if (p
&& (current_fsuid() == 1000) && (uid
>= 1000)) {
1026 if (inode
->i_mode
>> 6 & mask
) {
1031 /* Fall back to default. */
1032 return generic_permission(inode
, mask
);
1035 static const struct inode_operations proc_oom_adj_inode_operations
= {
1036 .permission
= oom_adjust_permission
,
1039 static const struct file_operations proc_oom_adj_operations
= {
1040 .read
= oom_adj_read
,
1041 .write
= oom_adj_write
,
1042 .llseek
= generic_file_llseek
,
1045 static ssize_t
oom_score_adj_read(struct file
*file
, char __user
*buf
,
1046 size_t count
, loff_t
*ppos
)
1048 struct task_struct
*task
= get_proc_task(file_inode(file
));
1049 char buffer
[PROC_NUMBUF
];
1050 short oom_score_adj
= OOM_SCORE_ADJ_MIN
;
1051 unsigned long flags
;
1056 if (lock_task_sighand(task
, &flags
)) {
1057 oom_score_adj
= task
->signal
->oom_score_adj
;
1058 unlock_task_sighand(task
, &flags
);
1060 put_task_struct(task
);
1061 len
= snprintf(buffer
, sizeof(buffer
), "%hd\n", oom_score_adj
);
1062 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
1065 static ssize_t
oom_score_adj_write(struct file
*file
, const char __user
*buf
,
1066 size_t count
, loff_t
*ppos
)
1068 struct task_struct
*task
;
1069 char buffer
[PROC_NUMBUF
];
1070 unsigned long flags
;
1074 memset(buffer
, 0, sizeof(buffer
));
1075 if (count
> sizeof(buffer
) - 1)
1076 count
= sizeof(buffer
) - 1;
1077 if (copy_from_user(buffer
, buf
, count
)) {
1082 err
= kstrtoint(strstrip(buffer
), 0, &oom_score_adj
);
1085 if (oom_score_adj
< OOM_SCORE_ADJ_MIN
||
1086 oom_score_adj
> OOM_SCORE_ADJ_MAX
) {
1091 task
= get_proc_task(file_inode(file
));
1103 if (!lock_task_sighand(task
, &flags
)) {
1108 if ((short)oom_score_adj
< task
->signal
->oom_score_adj_min
&&
1109 !capable(CAP_SYS_RESOURCE
)) {
1114 task
->signal
->oom_score_adj
= (short)oom_score_adj
;
1115 if (has_capability_noaudit(current
, CAP_SYS_RESOURCE
))
1116 task
->signal
->oom_score_adj_min
= (short)oom_score_adj
;
1117 trace_oom_score_adj_update(task
);
1120 unlock_task_sighand(task
, &flags
);
1123 put_task_struct(task
);
1125 return err
< 0 ? err
: count
;
1128 static const struct file_operations proc_oom_score_adj_operations
= {
1129 .read
= oom_score_adj_read
,
1130 .write
= oom_score_adj_write
,
1131 .llseek
= default_llseek
,
1134 #ifdef CONFIG_AUDITSYSCALL
1135 #define TMPBUFLEN 21
1136 static ssize_t
proc_loginuid_read(struct file
* file
, char __user
* buf
,
1137 size_t count
, loff_t
*ppos
)
1139 struct inode
* inode
= file_inode(file
);
1140 struct task_struct
*task
= get_proc_task(inode
);
1142 char tmpbuf
[TMPBUFLEN
];
1146 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
1147 from_kuid(file
->f_cred
->user_ns
,
1148 audit_get_loginuid(task
)));
1149 put_task_struct(task
);
1150 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1153 static ssize_t
proc_loginuid_write(struct file
* file
, const char __user
* buf
,
1154 size_t count
, loff_t
*ppos
)
1156 struct inode
* inode
= file_inode(file
);
1163 if (current
!= pid_task(proc_pid(inode
), PIDTYPE_PID
)) {
1169 if (count
>= PAGE_SIZE
)
1170 count
= PAGE_SIZE
- 1;
1173 /* No partial writes. */
1176 page
= (char*)__get_free_page(GFP_TEMPORARY
);
1180 if (copy_from_user(page
, buf
, count
))
1184 loginuid
= simple_strtoul(page
, &tmp
, 10);
1190 kloginuid
= make_kuid(file
->f_cred
->user_ns
, loginuid
);
1191 if (!uid_valid(kloginuid
)) {
1196 length
= audit_set_loginuid(kloginuid
);
1197 if (likely(length
== 0))
1201 free_page((unsigned long) page
);
1205 static const struct file_operations proc_loginuid_operations
= {
1206 .read
= proc_loginuid_read
,
1207 .write
= proc_loginuid_write
,
1208 .llseek
= generic_file_llseek
,
1211 static ssize_t
proc_sessionid_read(struct file
* file
, char __user
* buf
,
1212 size_t count
, loff_t
*ppos
)
1214 struct inode
* inode
= file_inode(file
);
1215 struct task_struct
*task
= get_proc_task(inode
);
1217 char tmpbuf
[TMPBUFLEN
];
1221 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
1222 audit_get_sessionid(task
));
1223 put_task_struct(task
);
1224 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1227 static const struct file_operations proc_sessionid_operations
= {
1228 .read
= proc_sessionid_read
,
1229 .llseek
= generic_file_llseek
,
1233 #ifdef CONFIG_FAULT_INJECTION
1234 static ssize_t
proc_fault_inject_read(struct file
* file
, char __user
* buf
,
1235 size_t count
, loff_t
*ppos
)
1237 struct task_struct
*task
= get_proc_task(file_inode(file
));
1238 char buffer
[PROC_NUMBUF
];
1244 make_it_fail
= task
->make_it_fail
;
1245 put_task_struct(task
);
1247 len
= snprintf(buffer
, sizeof(buffer
), "%i\n", make_it_fail
);
1249 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
1252 static ssize_t
proc_fault_inject_write(struct file
* file
,
1253 const char __user
* buf
, size_t count
, loff_t
*ppos
)
1255 struct task_struct
*task
;
1256 char buffer
[PROC_NUMBUF
], *end
;
1259 if (!capable(CAP_SYS_RESOURCE
))
1261 memset(buffer
, 0, sizeof(buffer
));
1262 if (count
> sizeof(buffer
) - 1)
1263 count
= sizeof(buffer
) - 1;
1264 if (copy_from_user(buffer
, buf
, count
))
1266 make_it_fail
= simple_strtol(strstrip(buffer
), &end
, 0);
1269 task
= get_proc_task(file_inode(file
));
1272 task
->make_it_fail
= make_it_fail
;
1273 put_task_struct(task
);
1278 static const struct file_operations proc_fault_inject_operations
= {
1279 .read
= proc_fault_inject_read
,
1280 .write
= proc_fault_inject_write
,
1281 .llseek
= generic_file_llseek
,
1286 #ifdef CONFIG_SCHED_DEBUG
1288 * Print out various scheduling related per-task fields:
1290 static int sched_show(struct seq_file
*m
, void *v
)
1292 struct inode
*inode
= m
->private;
1293 struct task_struct
*p
;
1295 p
= get_proc_task(inode
);
1298 proc_sched_show_task(p
, m
);
1306 sched_write(struct file
*file
, const char __user
*buf
,
1307 size_t count
, loff_t
*offset
)
1309 struct inode
*inode
= file_inode(file
);
1310 struct task_struct
*p
;
1312 p
= get_proc_task(inode
);
1315 proc_sched_set_task(p
);
1322 static int sched_open(struct inode
*inode
, struct file
*filp
)
1324 return single_open(filp
, sched_show
, inode
);
1327 static const struct file_operations proc_pid_sched_operations
= {
1330 .write
= sched_write
,
1331 .llseek
= seq_lseek
,
1332 .release
= single_release
,
1337 #ifdef CONFIG_SCHED_AUTOGROUP
1339 * Print out autogroup related information:
1341 static int sched_autogroup_show(struct seq_file
*m
, void *v
)
1343 struct inode
*inode
= m
->private;
1344 struct task_struct
*p
;
1346 p
= get_proc_task(inode
);
1349 proc_sched_autogroup_show_task(p
, m
);
1357 sched_autogroup_write(struct file
*file
, const char __user
*buf
,
1358 size_t count
, loff_t
*offset
)
1360 struct inode
*inode
= file_inode(file
);
1361 struct task_struct
*p
;
1362 char buffer
[PROC_NUMBUF
];
1366 memset(buffer
, 0, sizeof(buffer
));
1367 if (count
> sizeof(buffer
) - 1)
1368 count
= sizeof(buffer
) - 1;
1369 if (copy_from_user(buffer
, buf
, count
))
1372 err
= kstrtoint(strstrip(buffer
), 0, &nice
);
1376 p
= get_proc_task(inode
);
1380 err
= proc_sched_autogroup_set_nice(p
, nice
);
1389 static int sched_autogroup_open(struct inode
*inode
, struct file
*filp
)
1393 ret
= single_open(filp
, sched_autogroup_show
, NULL
);
1395 struct seq_file
*m
= filp
->private_data
;
1402 static const struct file_operations proc_pid_sched_autogroup_operations
= {
1403 .open
= sched_autogroup_open
,
1405 .write
= sched_autogroup_write
,
1406 .llseek
= seq_lseek
,
1407 .release
= single_release
,
1410 #endif /* CONFIG_SCHED_AUTOGROUP */
1412 static ssize_t
comm_write(struct file
*file
, const char __user
*buf
,
1413 size_t count
, loff_t
*offset
)
1415 struct inode
*inode
= file_inode(file
);
1416 struct task_struct
*p
;
1417 char buffer
[TASK_COMM_LEN
];
1418 const size_t maxlen
= sizeof(buffer
) - 1;
1420 memset(buffer
, 0, sizeof(buffer
));
1421 if (copy_from_user(buffer
, buf
, count
> maxlen
? maxlen
: count
))
1424 p
= get_proc_task(inode
);
1428 if (same_thread_group(current
, p
))
1429 set_task_comm(p
, buffer
);
1438 static int comm_show(struct seq_file
*m
, void *v
)
1440 struct inode
*inode
= m
->private;
1441 struct task_struct
*p
;
1443 p
= get_proc_task(inode
);
1448 seq_printf(m
, "%s\n", p
->comm
);
1456 static int comm_open(struct inode
*inode
, struct file
*filp
)
1458 return single_open(filp
, comm_show
, inode
);
1461 static const struct file_operations proc_pid_set_comm_operations
= {
1464 .write
= comm_write
,
1465 .llseek
= seq_lseek
,
1466 .release
= single_release
,
1469 static int proc_exe_link(struct dentry
*dentry
, struct path
*exe_path
)
1471 struct task_struct
*task
;
1472 struct mm_struct
*mm
;
1473 struct file
*exe_file
;
1475 task
= get_proc_task(dentry
->d_inode
);
1478 mm
= get_task_mm(task
);
1479 put_task_struct(task
);
1482 exe_file
= get_mm_exe_file(mm
);
1485 *exe_path
= exe_file
->f_path
;
1486 path_get(&exe_file
->f_path
);
1493 static void *proc_pid_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
1495 struct inode
*inode
= dentry
->d_inode
;
1497 int error
= -EACCES
;
1499 /* Are we allowed to snoop on the tasks file descriptors? */
1500 if (!proc_fd_access_allowed(inode
))
1503 error
= PROC_I(inode
)->op
.proc_get_link(dentry
, &path
);
1507 nd_jump_link(nd
, &path
);
1510 return ERR_PTR(error
);
1513 static int do_proc_readlink(struct path
*path
, char __user
*buffer
, int buflen
)
1515 char *tmp
= (char*)__get_free_page(GFP_TEMPORARY
);
1522 pathname
= d_path(path
, tmp
, PAGE_SIZE
);
1523 len
= PTR_ERR(pathname
);
1524 if (IS_ERR(pathname
))
1526 len
= tmp
+ PAGE_SIZE
- 1 - pathname
;
1530 if (copy_to_user(buffer
, pathname
, len
))
1533 free_page((unsigned long)tmp
);
1537 static int proc_pid_readlink(struct dentry
* dentry
, char __user
* buffer
, int buflen
)
1539 int error
= -EACCES
;
1540 struct inode
*inode
= dentry
->d_inode
;
1543 /* Are we allowed to snoop on the tasks file descriptors? */
1544 if (!proc_fd_access_allowed(inode
))
1547 error
= PROC_I(inode
)->op
.proc_get_link(dentry
, &path
);
1551 error
= do_proc_readlink(&path
, buffer
, buflen
);
1557 const struct inode_operations proc_pid_link_inode_operations
= {
1558 .readlink
= proc_pid_readlink
,
1559 .follow_link
= proc_pid_follow_link
,
1560 .setattr
= proc_setattr
,
1564 /* building an inode */
1566 struct inode
*proc_pid_make_inode(struct super_block
* sb
, struct task_struct
*task
)
1568 struct inode
* inode
;
1569 struct proc_inode
*ei
;
1570 const struct cred
*cred
;
1572 /* We need a new inode */
1574 inode
= new_inode(sb
);
1580 inode
->i_ino
= get_next_ino();
1581 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1582 inode
->i_op
= &proc_def_inode_operations
;
1585 * grab the reference to task.
1587 ei
->pid
= get_task_pid(task
, PIDTYPE_PID
);
1591 if (task_dumpable(task
)) {
1593 cred
= __task_cred(task
);
1594 inode
->i_uid
= cred
->euid
;
1595 inode
->i_gid
= cred
->egid
;
1598 security_task_to_inode(task
, inode
);
1608 int pid_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
1610 struct inode
*inode
= dentry
->d_inode
;
1611 struct task_struct
*task
;
1612 const struct cred
*cred
;
1613 struct pid_namespace
*pid
= dentry
->d_sb
->s_fs_info
;
1615 generic_fillattr(inode
, stat
);
1618 stat
->uid
= GLOBAL_ROOT_UID
;
1619 stat
->gid
= GLOBAL_ROOT_GID
;
1620 task
= pid_task(proc_pid(inode
), PIDTYPE_PID
);
1622 if (!has_pid_permissions(pid
, task
, 2)) {
1625 * This doesn't prevent learning whether PID exists,
1626 * it only makes getattr() consistent with readdir().
1630 if ((inode
->i_mode
== (S_IFDIR
|S_IRUGO
|S_IXUGO
)) ||
1631 task_dumpable(task
)) {
1632 cred
= __task_cred(task
);
1633 stat
->uid
= cred
->euid
;
1634 stat
->gid
= cred
->egid
;
1644 * Exceptional case: normally we are not allowed to unhash a busy
1645 * directory. In this case, however, we can do it - no aliasing problems
1646 * due to the way we treat inodes.
1648 * Rewrite the inode's ownerships here because the owning task may have
1649 * performed a setuid(), etc.
1651 * Before the /proc/pid/status file was created the only way to read
1652 * the effective uid of a /process was to stat /proc/pid. Reading
1653 * /proc/pid/status is slow enough that procps and other packages
1654 * kept stating /proc/pid. To keep the rules in /proc simple I have
1655 * made this apply to all per process world readable and executable
1658 int pid_revalidate(struct dentry
*dentry
, unsigned int flags
)
1660 struct inode
*inode
;
1661 struct task_struct
*task
;
1662 const struct cred
*cred
;
1664 if (flags
& LOOKUP_RCU
)
1667 inode
= dentry
->d_inode
;
1668 task
= get_proc_task(inode
);
1671 if ((inode
->i_mode
== (S_IFDIR
|S_IRUGO
|S_IXUGO
)) ||
1672 task_dumpable(task
)) {
1674 cred
= __task_cred(task
);
1675 inode
->i_uid
= cred
->euid
;
1676 inode
->i_gid
= cred
->egid
;
1679 inode
->i_uid
= GLOBAL_ROOT_UID
;
1680 inode
->i_gid
= GLOBAL_ROOT_GID
;
1682 inode
->i_mode
&= ~(S_ISUID
| S_ISGID
);
1683 security_task_to_inode(task
, inode
);
1684 put_task_struct(task
);
1691 int pid_delete_dentry(const struct dentry
*dentry
)
1693 /* Is the task we represent dead?
1694 * If so, then don't put the dentry on the lru list,
1695 * kill it immediately.
1697 return !proc_pid(dentry
->d_inode
)->tasks
[PIDTYPE_PID
].first
;
1700 const struct dentry_operations pid_dentry_operations
=
1702 .d_revalidate
= pid_revalidate
,
1703 .d_delete
= pid_delete_dentry
,
1709 * Fill a directory entry.
1711 * If possible create the dcache entry and derive our inode number and
1712 * file type from dcache entry.
1714 * Since all of the proc inode numbers are dynamically generated, the inode
1715 * numbers do not exist until the inode is cache. This means creating the
1716 * the dcache entry in readdir is necessary to keep the inode numbers
1717 * reported by readdir in sync with the inode numbers reported
1720 int proc_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
1721 const char *name
, int len
,
1722 instantiate_t instantiate
, struct task_struct
*task
, const void *ptr
)
1724 struct dentry
*child
, *dir
= filp
->f_path
.dentry
;
1725 struct inode
*inode
;
1728 unsigned type
= DT_UNKNOWN
;
1732 qname
.hash
= full_name_hash(name
, len
);
1734 child
= d_lookup(dir
, &qname
);
1737 new = d_alloc(dir
, &qname
);
1739 child
= instantiate(dir
->d_inode
, new, task
, ptr
);
1746 if (!child
|| IS_ERR(child
) || !child
->d_inode
)
1747 goto end_instantiate
;
1748 inode
= child
->d_inode
;
1751 type
= inode
->i_mode
>> 12;
1756 ino
= find_inode_number(dir
, &qname
);
1759 return filldir(dirent
, name
, len
, filp
->f_pos
, ino
, type
);
1762 #ifdef CONFIG_CHECKPOINT_RESTORE
1765 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1766 * which represent vma start and end addresses.
1768 static int dname_to_vma_addr(struct dentry
*dentry
,
1769 unsigned long *start
, unsigned long *end
)
1771 if (sscanf(dentry
->d_name
.name
, "%lx-%lx", start
, end
) != 2)
1777 static int map_files_d_revalidate(struct dentry
*dentry
, unsigned int flags
)
1779 unsigned long vm_start
, vm_end
;
1780 bool exact_vma_exists
= false;
1781 struct mm_struct
*mm
= NULL
;
1782 struct task_struct
*task
;
1783 const struct cred
*cred
;
1784 struct inode
*inode
;
1787 if (flags
& LOOKUP_RCU
)
1790 if (!capable(CAP_SYS_ADMIN
)) {
1795 inode
= dentry
->d_inode
;
1796 task
= get_proc_task(inode
);
1800 mm
= mm_access(task
, PTRACE_MODE_READ
);
1801 if (IS_ERR_OR_NULL(mm
))
1804 if (!dname_to_vma_addr(dentry
, &vm_start
, &vm_end
)) {
1805 down_read(&mm
->mmap_sem
);
1806 exact_vma_exists
= !!find_exact_vma(mm
, vm_start
, vm_end
);
1807 up_read(&mm
->mmap_sem
);
1812 if (exact_vma_exists
) {
1813 if (task_dumpable(task
)) {
1815 cred
= __task_cred(task
);
1816 inode
->i_uid
= cred
->euid
;
1817 inode
->i_gid
= cred
->egid
;
1820 inode
->i_uid
= GLOBAL_ROOT_UID
;
1821 inode
->i_gid
= GLOBAL_ROOT_GID
;
1823 security_task_to_inode(task
, inode
);
1828 put_task_struct(task
);
1837 static const struct dentry_operations tid_map_files_dentry_operations
= {
1838 .d_revalidate
= map_files_d_revalidate
,
1839 .d_delete
= pid_delete_dentry
,
1842 static int proc_map_files_get_link(struct dentry
*dentry
, struct path
*path
)
1844 unsigned long vm_start
, vm_end
;
1845 struct vm_area_struct
*vma
;
1846 struct task_struct
*task
;
1847 struct mm_struct
*mm
;
1851 task
= get_proc_task(dentry
->d_inode
);
1855 mm
= get_task_mm(task
);
1856 put_task_struct(task
);
1860 rc
= dname_to_vma_addr(dentry
, &vm_start
, &vm_end
);
1865 down_read(&mm
->mmap_sem
);
1866 vma
= find_exact_vma(mm
, vm_start
, vm_end
);
1867 if (vma
&& vma
->vm_file
) {
1868 *path
= vma
->vm_file
->f_path
;
1872 up_read(&mm
->mmap_sem
);
1880 struct map_files_info
{
1883 unsigned char name
[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
1886 static struct dentry
*
1887 proc_map_files_instantiate(struct inode
*dir
, struct dentry
*dentry
,
1888 struct task_struct
*task
, const void *ptr
)
1890 fmode_t mode
= (fmode_t
)(unsigned long)ptr
;
1891 struct proc_inode
*ei
;
1892 struct inode
*inode
;
1894 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
1896 return ERR_PTR(-ENOENT
);
1899 ei
->op
.proc_get_link
= proc_map_files_get_link
;
1901 inode
->i_op
= &proc_pid_link_inode_operations
;
1903 inode
->i_mode
= S_IFLNK
;
1905 if (mode
& FMODE_READ
)
1906 inode
->i_mode
|= S_IRUSR
;
1907 if (mode
& FMODE_WRITE
)
1908 inode
->i_mode
|= S_IWUSR
;
1910 d_set_d_op(dentry
, &tid_map_files_dentry_operations
);
1911 d_add(dentry
, inode
);
1916 static struct dentry
*proc_map_files_lookup(struct inode
*dir
,
1917 struct dentry
*dentry
, unsigned int flags
)
1919 unsigned long vm_start
, vm_end
;
1920 struct vm_area_struct
*vma
;
1921 struct task_struct
*task
;
1922 struct dentry
*result
;
1923 struct mm_struct
*mm
;
1925 result
= ERR_PTR(-EPERM
);
1926 if (!capable(CAP_SYS_ADMIN
))
1929 result
= ERR_PTR(-ENOENT
);
1930 task
= get_proc_task(dir
);
1934 result
= ERR_PTR(-EACCES
);
1935 if (!ptrace_may_access(task
, PTRACE_MODE_READ
))
1938 result
= ERR_PTR(-ENOENT
);
1939 if (dname_to_vma_addr(dentry
, &vm_start
, &vm_end
))
1942 mm
= get_task_mm(task
);
1946 down_read(&mm
->mmap_sem
);
1947 vma
= find_exact_vma(mm
, vm_start
, vm_end
);
1952 result
= proc_map_files_instantiate(dir
, dentry
, task
,
1953 (void *)(unsigned long)vma
->vm_file
->f_mode
);
1956 up_read(&mm
->mmap_sem
);
1959 put_task_struct(task
);
1964 static const struct inode_operations proc_map_files_inode_operations
= {
1965 .lookup
= proc_map_files_lookup
,
1966 .permission
= proc_fd_permission
,
1967 .setattr
= proc_setattr
,
1971 proc_map_files_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
1973 struct dentry
*dentry
= filp
->f_path
.dentry
;
1974 struct inode
*inode
= dentry
->d_inode
;
1975 struct vm_area_struct
*vma
;
1976 struct task_struct
*task
;
1977 struct mm_struct
*mm
;
1982 if (!capable(CAP_SYS_ADMIN
))
1986 task
= get_proc_task(inode
);
1991 if (!ptrace_may_access(task
, PTRACE_MODE_READ
))
1995 switch (filp
->f_pos
) {
1998 if (filldir(dirent
, ".", 1, 0, ino
, DT_DIR
) < 0)
2002 ino
= parent_ino(dentry
);
2003 if (filldir(dirent
, "..", 2, 1, ino
, DT_DIR
) < 0)
2008 unsigned long nr_files
, pos
, i
;
2009 struct flex_array
*fa
= NULL
;
2010 struct map_files_info info
;
2011 struct map_files_info
*p
;
2013 mm
= get_task_mm(task
);
2016 down_read(&mm
->mmap_sem
);
2021 * We need two passes here:
2023 * 1) Collect vmas of mapped files with mmap_sem taken
2024 * 2) Release mmap_sem and instantiate entries
2026 * otherwise we get lockdep complained, since filldir()
2027 * routine might require mmap_sem taken in might_fault().
2030 for (vma
= mm
->mmap
, pos
= 2; vma
; vma
= vma
->vm_next
) {
2031 if (vma
->vm_file
&& ++pos
> filp
->f_pos
)
2036 fa
= flex_array_alloc(sizeof(info
), nr_files
,
2038 if (!fa
|| flex_array_prealloc(fa
, 0, nr_files
,
2042 flex_array_free(fa
);
2043 up_read(&mm
->mmap_sem
);
2047 for (i
= 0, vma
= mm
->mmap
, pos
= 2; vma
;
2048 vma
= vma
->vm_next
) {
2051 if (++pos
<= filp
->f_pos
)
2054 info
.mode
= vma
->vm_file
->f_mode
;
2055 info
.len
= snprintf(info
.name
,
2056 sizeof(info
.name
), "%lx-%lx",
2057 vma
->vm_start
, vma
->vm_end
);
2058 if (flex_array_put(fa
, i
++, &info
, GFP_KERNEL
))
2062 up_read(&mm
->mmap_sem
);
2064 for (i
= 0; i
< nr_files
; i
++) {
2065 p
= flex_array_get(fa
, i
);
2066 ret
= proc_fill_cache(filp
, dirent
, filldir
,
2068 proc_map_files_instantiate
,
2070 (void *)(unsigned long)p
->mode
);
2076 flex_array_free(fa
);
2082 put_task_struct(task
);
2087 static const struct file_operations proc_map_files_operations
= {
2088 .read
= generic_read_dir
,
2089 .readdir
= proc_map_files_readdir
,
2090 .llseek
= default_llseek
,
2093 struct timers_private
{
2095 struct task_struct
*task
;
2096 struct sighand_struct
*sighand
;
2097 struct pid_namespace
*ns
;
2098 unsigned long flags
;
2101 static void *timers_start(struct seq_file
*m
, loff_t
*pos
)
2103 struct timers_private
*tp
= m
->private;
2105 tp
->task
= get_pid_task(tp
->pid
, PIDTYPE_PID
);
2107 return ERR_PTR(-ESRCH
);
2109 tp
->sighand
= lock_task_sighand(tp
->task
, &tp
->flags
);
2111 return ERR_PTR(-ESRCH
);
2113 return seq_list_start(&tp
->task
->signal
->posix_timers
, *pos
);
2116 static void *timers_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
2118 struct timers_private
*tp
= m
->private;
2119 return seq_list_next(v
, &tp
->task
->signal
->posix_timers
, pos
);
2122 static void timers_stop(struct seq_file
*m
, void *v
)
2124 struct timers_private
*tp
= m
->private;
2127 unlock_task_sighand(tp
->task
, &tp
->flags
);
2132 put_task_struct(tp
->task
);
2137 static int show_timer(struct seq_file
*m
, void *v
)
2139 struct k_itimer
*timer
;
2140 struct timers_private
*tp
= m
->private;
2142 static char *nstr
[] = {
2143 [SIGEV_SIGNAL
] = "signal",
2144 [SIGEV_NONE
] = "none",
2145 [SIGEV_THREAD
] = "thread",
2148 timer
= list_entry((struct list_head
*)v
, struct k_itimer
, list
);
2149 notify
= timer
->it_sigev_notify
;
2151 seq_printf(m
, "ID: %d\n", timer
->it_id
);
2152 seq_printf(m
, "signal: %d/%p\n", timer
->sigq
->info
.si_signo
,
2153 timer
->sigq
->info
.si_value
.sival_ptr
);
2154 seq_printf(m
, "notify: %s/%s.%d\n",
2155 nstr
[notify
& ~SIGEV_THREAD_ID
],
2156 (notify
& SIGEV_THREAD_ID
) ? "tid" : "pid",
2157 pid_nr_ns(timer
->it_pid
, tp
->ns
));
2158 seq_printf(m
, "ClockID: %d\n", timer
->it_clock
);
2163 static const struct seq_operations proc_timers_seq_ops
= {
2164 .start
= timers_start
,
2165 .next
= timers_next
,
2166 .stop
= timers_stop
,
2170 static int proc_timers_open(struct inode
*inode
, struct file
*file
)
2172 struct timers_private
*tp
;
2174 tp
= __seq_open_private(file
, &proc_timers_seq_ops
,
2175 sizeof(struct timers_private
));
2179 tp
->pid
= proc_pid(inode
);
2180 tp
->ns
= inode
->i_sb
->s_fs_info
;
2184 static const struct file_operations proc_timers_operations
= {
2185 .open
= proc_timers_open
,
2187 .llseek
= seq_lseek
,
2188 .release
= seq_release_private
,
2190 #endif /* CONFIG_CHECKPOINT_RESTORE */
2192 static struct dentry
*proc_pident_instantiate(struct inode
*dir
,
2193 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
2195 const struct pid_entry
*p
= ptr
;
2196 struct inode
*inode
;
2197 struct proc_inode
*ei
;
2198 struct dentry
*error
= ERR_PTR(-ENOENT
);
2200 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
2205 inode
->i_mode
= p
->mode
;
2206 if (S_ISDIR(inode
->i_mode
))
2207 set_nlink(inode
, 2); /* Use getattr to fix if necessary */
2209 inode
->i_op
= p
->iop
;
2211 inode
->i_fop
= p
->fop
;
2213 d_set_d_op(dentry
, &pid_dentry_operations
);
2214 d_add(dentry
, inode
);
2215 /* Close the race of the process dying before we return the dentry */
2216 if (pid_revalidate(dentry
, 0))
2222 static struct dentry
*proc_pident_lookup(struct inode
*dir
,
2223 struct dentry
*dentry
,
2224 const struct pid_entry
*ents
,
2227 struct dentry
*error
;
2228 struct task_struct
*task
= get_proc_task(dir
);
2229 const struct pid_entry
*p
, *last
;
2231 error
= ERR_PTR(-ENOENT
);
2237 * Yes, it does not scale. And it should not. Don't add
2238 * new entries into /proc/<tgid>/ without very good reasons.
2240 last
= &ents
[nents
- 1];
2241 for (p
= ents
; p
<= last
; p
++) {
2242 if (p
->len
!= dentry
->d_name
.len
)
2244 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
))
2250 error
= proc_pident_instantiate(dir
, dentry
, task
, p
);
2252 put_task_struct(task
);
2257 static int proc_pident_fill_cache(struct file
*filp
, void *dirent
,
2258 filldir_t filldir
, struct task_struct
*task
, const struct pid_entry
*p
)
2260 return proc_fill_cache(filp
, dirent
, filldir
, p
->name
, p
->len
,
2261 proc_pident_instantiate
, task
, p
);
2264 static int proc_pident_readdir(struct file
*filp
,
2265 void *dirent
, filldir_t filldir
,
2266 const struct pid_entry
*ents
, unsigned int nents
)
2269 struct dentry
*dentry
= filp
->f_path
.dentry
;
2270 struct inode
*inode
= dentry
->d_inode
;
2271 struct task_struct
*task
= get_proc_task(inode
);
2272 const struct pid_entry
*p
, *last
;
2285 if (filldir(dirent
, ".", 1, i
, ino
, DT_DIR
) < 0)
2291 ino
= parent_ino(dentry
);
2292 if (filldir(dirent
, "..", 2, i
, ino
, DT_DIR
) < 0)
2304 last
= &ents
[nents
- 1];
2306 if (proc_pident_fill_cache(filp
, dirent
, filldir
, task
, p
) < 0)
2315 put_task_struct(task
);
2320 #ifdef CONFIG_SECURITY
2321 static ssize_t
proc_pid_attr_read(struct file
* file
, char __user
* buf
,
2322 size_t count
, loff_t
*ppos
)
2324 struct inode
* inode
= file_inode(file
);
2327 struct task_struct
*task
= get_proc_task(inode
);
2332 length
= security_getprocattr(task
,
2333 (char*)file
->f_path
.dentry
->d_name
.name
,
2335 put_task_struct(task
);
2337 length
= simple_read_from_buffer(buf
, count
, ppos
, p
, length
);
2342 static ssize_t
proc_pid_attr_write(struct file
* file
, const char __user
* buf
,
2343 size_t count
, loff_t
*ppos
)
2345 struct inode
* inode
= file_inode(file
);
2348 struct task_struct
*task
= get_proc_task(inode
);
2353 if (count
> PAGE_SIZE
)
2356 /* No partial writes. */
2362 page
= (char*)__get_free_page(GFP_TEMPORARY
);
2367 if (copy_from_user(page
, buf
, count
))
2370 /* Guard against adverse ptrace interaction */
2371 length
= mutex_lock_interruptible(&task
->signal
->cred_guard_mutex
);
2375 length
= security_setprocattr(task
,
2376 (char*)file
->f_path
.dentry
->d_name
.name
,
2377 (void*)page
, count
);
2378 mutex_unlock(&task
->signal
->cred_guard_mutex
);
2380 free_page((unsigned long) page
);
2382 put_task_struct(task
);
2387 static const struct file_operations proc_pid_attr_operations
= {
2388 .read
= proc_pid_attr_read
,
2389 .write
= proc_pid_attr_write
,
2390 .llseek
= generic_file_llseek
,
2393 static const struct pid_entry attr_dir_stuff
[] = {
2394 REG("current", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2395 REG("prev", S_IRUGO
, proc_pid_attr_operations
),
2396 REG("exec", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2397 REG("fscreate", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2398 REG("keycreate", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2399 REG("sockcreate", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2402 static int proc_attr_dir_readdir(struct file
* filp
,
2403 void * dirent
, filldir_t filldir
)
2405 return proc_pident_readdir(filp
,dirent
,filldir
,
2406 attr_dir_stuff
,ARRAY_SIZE(attr_dir_stuff
));
2409 static const struct file_operations proc_attr_dir_operations
= {
2410 .read
= generic_read_dir
,
2411 .readdir
= proc_attr_dir_readdir
,
2412 .llseek
= default_llseek
,
2415 static struct dentry
*proc_attr_dir_lookup(struct inode
*dir
,
2416 struct dentry
*dentry
, unsigned int flags
)
2418 return proc_pident_lookup(dir
, dentry
,
2419 attr_dir_stuff
, ARRAY_SIZE(attr_dir_stuff
));
2422 static const struct inode_operations proc_attr_dir_inode_operations
= {
2423 .lookup
= proc_attr_dir_lookup
,
2424 .getattr
= pid_getattr
,
2425 .setattr
= proc_setattr
,
2430 #ifdef CONFIG_ELF_CORE
2431 static ssize_t
proc_coredump_filter_read(struct file
*file
, char __user
*buf
,
2432 size_t count
, loff_t
*ppos
)
2434 struct task_struct
*task
= get_proc_task(file_inode(file
));
2435 struct mm_struct
*mm
;
2436 char buffer
[PROC_NUMBUF
];
2444 mm
= get_task_mm(task
);
2446 len
= snprintf(buffer
, sizeof(buffer
), "%08lx\n",
2447 ((mm
->flags
& MMF_DUMP_FILTER_MASK
) >>
2448 MMF_DUMP_FILTER_SHIFT
));
2450 ret
= simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
2453 put_task_struct(task
);
2458 static ssize_t
proc_coredump_filter_write(struct file
*file
,
2459 const char __user
*buf
,
2463 struct task_struct
*task
;
2464 struct mm_struct
*mm
;
2465 char buffer
[PROC_NUMBUF
], *end
;
2472 memset(buffer
, 0, sizeof(buffer
));
2473 if (count
> sizeof(buffer
) - 1)
2474 count
= sizeof(buffer
) - 1;
2475 if (copy_from_user(buffer
, buf
, count
))
2479 val
= (unsigned int)simple_strtoul(buffer
, &end
, 0);
2482 if (end
- buffer
== 0)
2486 task
= get_proc_task(file_inode(file
));
2491 mm
= get_task_mm(task
);
2495 for (i
= 0, mask
= 1; i
< MMF_DUMP_FILTER_BITS
; i
++, mask
<<= 1) {
2497 set_bit(i
+ MMF_DUMP_FILTER_SHIFT
, &mm
->flags
);
2499 clear_bit(i
+ MMF_DUMP_FILTER_SHIFT
, &mm
->flags
);
2504 put_task_struct(task
);
2509 static const struct file_operations proc_coredump_filter_operations
= {
2510 .read
= proc_coredump_filter_read
,
2511 .write
= proc_coredump_filter_write
,
2512 .llseek
= generic_file_llseek
,
2516 #ifdef CONFIG_TASK_IO_ACCOUNTING
2517 static int do_io_accounting(struct task_struct
*task
, char *buffer
, int whole
)
2519 struct task_io_accounting acct
= task
->ioac
;
2520 unsigned long flags
;
2523 result
= mutex_lock_killable(&task
->signal
->cred_guard_mutex
);
2527 if (!ptrace_may_access(task
, PTRACE_MODE_READ
)) {
2532 if (whole
&& lock_task_sighand(task
, &flags
)) {
2533 struct task_struct
*t
= task
;
2535 task_io_accounting_add(&acct
, &task
->signal
->ioac
);
2536 while_each_thread(task
, t
)
2537 task_io_accounting_add(&acct
, &t
->ioac
);
2539 unlock_task_sighand(task
, &flags
);
2541 result
= sprintf(buffer
,
2546 "read_bytes: %llu\n"
2547 "write_bytes: %llu\n"
2548 "cancelled_write_bytes: %llu\n",
2549 (unsigned long long)acct
.rchar
,
2550 (unsigned long long)acct
.wchar
,
2551 (unsigned long long)acct
.syscr
,
2552 (unsigned long long)acct
.syscw
,
2553 (unsigned long long)acct
.read_bytes
,
2554 (unsigned long long)acct
.write_bytes
,
2555 (unsigned long long)acct
.cancelled_write_bytes
);
2557 mutex_unlock(&task
->signal
->cred_guard_mutex
);
2561 static int proc_tid_io_accounting(struct task_struct
*task
, char *buffer
)
2563 return do_io_accounting(task
, buffer
, 0);
2566 static int proc_tgid_io_accounting(struct task_struct
*task
, char *buffer
)
2568 return do_io_accounting(task
, buffer
, 1);
2570 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2572 #ifdef CONFIG_USER_NS
2573 static int proc_id_map_open(struct inode
*inode
, struct file
*file
,
2574 struct seq_operations
*seq_ops
)
2576 struct user_namespace
*ns
= NULL
;
2577 struct task_struct
*task
;
2578 struct seq_file
*seq
;
2581 task
= get_proc_task(inode
);
2584 ns
= get_user_ns(task_cred_xxx(task
, user_ns
));
2586 put_task_struct(task
);
2591 ret
= seq_open(file
, seq_ops
);
2595 seq
= file
->private_data
;
2605 static int proc_id_map_release(struct inode
*inode
, struct file
*file
)
2607 struct seq_file
*seq
= file
->private_data
;
2608 struct user_namespace
*ns
= seq
->private;
2610 return seq_release(inode
, file
);
2613 static int proc_uid_map_open(struct inode
*inode
, struct file
*file
)
2615 return proc_id_map_open(inode
, file
, &proc_uid_seq_operations
);
2618 static int proc_gid_map_open(struct inode
*inode
, struct file
*file
)
2620 return proc_id_map_open(inode
, file
, &proc_gid_seq_operations
);
2623 static int proc_projid_map_open(struct inode
*inode
, struct file
*file
)
2625 return proc_id_map_open(inode
, file
, &proc_projid_seq_operations
);
2628 static const struct file_operations proc_uid_map_operations
= {
2629 .open
= proc_uid_map_open
,
2630 .write
= proc_uid_map_write
,
2632 .llseek
= seq_lseek
,
2633 .release
= proc_id_map_release
,
2636 static const struct file_operations proc_gid_map_operations
= {
2637 .open
= proc_gid_map_open
,
2638 .write
= proc_gid_map_write
,
2640 .llseek
= seq_lseek
,
2641 .release
= proc_id_map_release
,
2644 static const struct file_operations proc_projid_map_operations
= {
2645 .open
= proc_projid_map_open
,
2646 .write
= proc_projid_map_write
,
2648 .llseek
= seq_lseek
,
2649 .release
= proc_id_map_release
,
2652 static int proc_setgroups_open(struct inode
*inode
, struct file
*file
)
2654 struct user_namespace
*ns
= NULL
;
2655 struct task_struct
*task
;
2659 task
= get_proc_task(inode
);
2662 ns
= get_user_ns(task_cred_xxx(task
, user_ns
));
2664 put_task_struct(task
);
2669 if (file
->f_mode
& FMODE_WRITE
) {
2671 if (!ns_capable(ns
, CAP_SYS_ADMIN
))
2675 ret
= single_open(file
, &proc_setgroups_show
, ns
);
2686 static int proc_setgroups_release(struct inode
*inode
, struct file
*file
)
2688 struct seq_file
*seq
= file
->private_data
;
2689 struct user_namespace
*ns
= seq
->private;
2690 int ret
= single_release(inode
, file
);
2695 static const struct file_operations proc_setgroups_operations
= {
2696 .open
= proc_setgroups_open
,
2697 .write
= proc_setgroups_write
,
2699 .llseek
= seq_lseek
,
2700 .release
= proc_setgroups_release
,
2702 #endif /* CONFIG_USER_NS */
2704 static int proc_pid_personality(struct seq_file
*m
, struct pid_namespace
*ns
,
2705 struct pid
*pid
, struct task_struct
*task
)
2707 int err
= lock_trace(task
);
2709 seq_printf(m
, "%08x\n", task
->personality
);
2718 static const struct file_operations proc_task_operations
;
2719 static const struct inode_operations proc_task_inode_operations
;
2721 static const struct pid_entry tgid_base_stuff
[] = {
2722 DIR("task", S_IRUGO
|S_IXUGO
, proc_task_inode_operations
, proc_task_operations
),
2723 DIR("fd", S_IRUSR
|S_IXUSR
, proc_fd_inode_operations
, proc_fd_operations
),
2724 #ifdef CONFIG_CHECKPOINT_RESTORE
2725 DIR("map_files", S_IRUSR
|S_IXUSR
, proc_map_files_inode_operations
, proc_map_files_operations
),
2727 DIR("fdinfo", S_IRUSR
|S_IXUSR
, proc_fdinfo_inode_operations
, proc_fdinfo_operations
),
2728 DIR("ns", S_IRUSR
|S_IXUGO
, proc_ns_dir_inode_operations
, proc_ns_dir_operations
),
2730 DIR("net", S_IRUGO
|S_IXUGO
, proc_net_inode_operations
, proc_net_operations
),
2732 REG("environ", S_IRUSR
, proc_environ_operations
),
2733 INF("auxv", S_IRUSR
, proc_pid_auxv
),
2734 ONE("status", S_IRUGO
, proc_pid_status
),
2735 ONE("personality", S_IRUGO
, proc_pid_personality
),
2736 INF("limits", S_IRUGO
, proc_pid_limits
),
2737 #ifdef CONFIG_SCHED_DEBUG
2738 REG("sched", S_IRUGO
|S_IWUSR
, proc_pid_sched_operations
),
2740 #ifdef CONFIG_SCHED_AUTOGROUP
2741 REG("autogroup", S_IRUGO
|S_IWUSR
, proc_pid_sched_autogroup_operations
),
2743 REG("comm", S_IRUGO
|S_IWUSR
, proc_pid_set_comm_operations
),
2744 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2745 INF("syscall", S_IRUGO
, proc_pid_syscall
),
2747 INF("cmdline", S_IRUGO
, proc_pid_cmdline
),
2748 ONE("stat", S_IRUGO
, proc_tgid_stat
),
2749 ONE("statm", S_IRUGO
, proc_pid_statm
),
2750 REG("maps", S_IRUGO
, proc_pid_maps_operations
),
2752 REG("numa_maps", S_IRUGO
, proc_pid_numa_maps_operations
),
2754 REG("mem", S_IRUSR
|S_IWUSR
, proc_mem_operations
),
2755 LNK("cwd", proc_cwd_link
),
2756 LNK("root", proc_root_link
),
2757 LNK("exe", proc_exe_link
),
2758 REG("mounts", S_IRUGO
, proc_mounts_operations
),
2759 REG("mountinfo", S_IRUGO
, proc_mountinfo_operations
),
2760 REG("mountstats", S_IRUSR
, proc_mountstats_operations
),
2761 #ifdef CONFIG_PROC_PAGE_MONITOR
2762 REG("clear_refs", S_IWUSR
, proc_clear_refs_operations
),
2763 REG("smaps", S_IRUGO
, proc_pid_smaps_operations
),
2764 REG("pagemap", S_IRUGO
, proc_pagemap_operations
),
2766 #ifdef CONFIG_SECURITY
2767 DIR("attr", S_IRUGO
|S_IXUGO
, proc_attr_dir_inode_operations
, proc_attr_dir_operations
),
2769 #ifdef CONFIG_KALLSYMS
2770 INF("wchan", S_IRUGO
, proc_pid_wchan
),
2772 #ifdef CONFIG_STACKTRACE
2773 ONE("stack", S_IRUGO
, proc_pid_stack
),
2775 #ifdef CONFIG_SCHEDSTATS
2776 INF("schedstat", S_IRUGO
, proc_pid_schedstat
),
2778 #ifdef CONFIG_LATENCYTOP
2779 REG("latency", S_IRUGO
, proc_lstats_operations
),
2781 #ifdef CONFIG_PROC_PID_CPUSET
2782 REG("cpuset", S_IRUGO
, proc_cpuset_operations
),
2784 #ifdef CONFIG_CGROUPS
2785 REG("cgroup", S_IRUGO
, proc_cgroup_operations
),
2787 INF("oom_score", S_IRUGO
, proc_oom_score
),
2788 ANDROID("oom_adj", S_IRUGO
|S_IWUSR
, oom_adj
),
2789 REG("oom_score_adj", S_IRUGO
|S_IWUSR
, proc_oom_score_adj_operations
),
2790 #ifdef CONFIG_AUDITSYSCALL
2791 REG("loginuid", S_IWUSR
|S_IRUGO
, proc_loginuid_operations
),
2792 REG("sessionid", S_IRUGO
, proc_sessionid_operations
),
2794 #ifdef CONFIG_FAULT_INJECTION
2795 REG("make-it-fail", S_IRUGO
|S_IWUSR
, proc_fault_inject_operations
),
2797 #ifdef CONFIG_ELF_CORE
2798 REG("coredump_filter", S_IRUGO
|S_IWUSR
, proc_coredump_filter_operations
),
2800 #ifdef CONFIG_TASK_IO_ACCOUNTING
2801 INF("io", S_IRUSR
, proc_tgid_io_accounting
),
2803 #ifdef CONFIG_HARDWALL
2804 INF("hardwall", S_IRUGO
, proc_pid_hardwall
),
2806 #ifdef CONFIG_USER_NS
2807 REG("uid_map", S_IRUGO
|S_IWUSR
, proc_uid_map_operations
),
2808 REG("gid_map", S_IRUGO
|S_IWUSR
, proc_gid_map_operations
),
2809 REG("projid_map", S_IRUGO
|S_IWUSR
, proc_projid_map_operations
),
2810 REG("setgroups", S_IRUGO
|S_IWUSR
, proc_setgroups_operations
),
2812 #ifdef CONFIG_CHECKPOINT_RESTORE
2813 REG("timers", S_IRUGO
, proc_timers_operations
),
2817 static int proc_tgid_base_readdir(struct file
* filp
,
2818 void * dirent
, filldir_t filldir
)
2820 return proc_pident_readdir(filp
,dirent
,filldir
,
2821 tgid_base_stuff
,ARRAY_SIZE(tgid_base_stuff
));
2824 static const struct file_operations proc_tgid_base_operations
= {
2825 .read
= generic_read_dir
,
2826 .readdir
= proc_tgid_base_readdir
,
2827 .llseek
= default_llseek
,
2830 static struct dentry
*proc_tgid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, unsigned int flags
)
2832 return proc_pident_lookup(dir
, dentry
,
2833 tgid_base_stuff
, ARRAY_SIZE(tgid_base_stuff
));
2836 static const struct inode_operations proc_tgid_base_inode_operations
= {
2837 .lookup
= proc_tgid_base_lookup
,
2838 .getattr
= pid_getattr
,
2839 .setattr
= proc_setattr
,
2840 .permission
= proc_pid_permission
,
2843 static void proc_flush_task_mnt(struct vfsmount
*mnt
, pid_t pid
, pid_t tgid
)
2845 struct dentry
*dentry
, *leader
, *dir
;
2846 char buf
[PROC_NUMBUF
];
2850 name
.len
= snprintf(buf
, sizeof(buf
), "%d", pid
);
2851 /* no ->d_hash() rejects on procfs */
2852 dentry
= d_hash_and_lookup(mnt
->mnt_root
, &name
);
2854 shrink_dcache_parent(dentry
);
2860 name
.len
= snprintf(buf
, sizeof(buf
), "%d", tgid
);
2861 leader
= d_hash_and_lookup(mnt
->mnt_root
, &name
);
2866 name
.len
= strlen(name
.name
);
2867 dir
= d_hash_and_lookup(leader
, &name
);
2869 goto out_put_leader
;
2872 name
.len
= snprintf(buf
, sizeof(buf
), "%d", pid
);
2873 dentry
= d_hash_and_lookup(dir
, &name
);
2875 shrink_dcache_parent(dentry
);
2888 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2889 * @task: task that should be flushed.
2891 * When flushing dentries from proc, one needs to flush them from global
2892 * proc (proc_mnt) and from all the namespaces' procs this task was seen
2893 * in. This call is supposed to do all of this job.
2895 * Looks in the dcache for
2897 * /proc/@tgid/task/@pid
2898 * if either directory is present flushes it and all of it'ts children
2901 * It is safe and reasonable to cache /proc entries for a task until
2902 * that task exits. After that they just clog up the dcache with
2903 * useless entries, possibly causing useful dcache entries to be
2904 * flushed instead. This routine is proved to flush those useless
2905 * dcache entries at process exit time.
2907 * NOTE: This routine is just an optimization so it does not guarantee
2908 * that no dcache entries will exist at process exit time it
2909 * just makes it very unlikely that any will persist.
2912 void proc_flush_task(struct task_struct
*task
)
2915 struct pid
*pid
, *tgid
;
2918 pid
= task_pid(task
);
2919 tgid
= task_tgid(task
);
2921 for (i
= 0; i
<= pid
->level
; i
++) {
2922 upid
= &pid
->numbers
[i
];
2923 proc_flush_task_mnt(upid
->ns
->proc_mnt
, upid
->nr
,
2924 tgid
->numbers
[i
].nr
);
2928 static struct dentry
*proc_pid_instantiate(struct inode
*dir
,
2929 struct dentry
* dentry
,
2930 struct task_struct
*task
, const void *ptr
)
2932 struct dentry
*error
= ERR_PTR(-ENOENT
);
2933 struct inode
*inode
;
2935 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
2939 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
2940 inode
->i_op
= &proc_tgid_base_inode_operations
;
2941 inode
->i_fop
= &proc_tgid_base_operations
;
2942 inode
->i_flags
|=S_IMMUTABLE
;
2944 set_nlink(inode
, 2 + pid_entry_count_dirs(tgid_base_stuff
,
2945 ARRAY_SIZE(tgid_base_stuff
)));
2947 d_set_d_op(dentry
, &pid_dentry_operations
);
2949 d_add(dentry
, inode
);
2950 /* Close the race of the process dying before we return the dentry */
2951 if (pid_revalidate(dentry
, 0))
2957 struct dentry
*proc_pid_lookup(struct inode
*dir
, struct dentry
* dentry
, unsigned int flags
)
2959 struct dentry
*result
= NULL
;
2960 struct task_struct
*task
;
2962 struct pid_namespace
*ns
;
2964 tgid
= name_to_int(dentry
);
2968 ns
= dentry
->d_sb
->s_fs_info
;
2970 task
= find_task_by_pid_ns(tgid
, ns
);
2972 get_task_struct(task
);
2977 result
= proc_pid_instantiate(dir
, dentry
, task
, NULL
);
2978 put_task_struct(task
);
2984 * Find the first task with tgid >= tgid
2989 struct task_struct
*task
;
2991 static struct tgid_iter
next_tgid(struct pid_namespace
*ns
, struct tgid_iter iter
)
2996 put_task_struct(iter
.task
);
3000 pid
= find_ge_pid(iter
.tgid
, ns
);
3002 iter
.tgid
= pid_nr_ns(pid
, ns
);
3003 iter
.task
= pid_task(pid
, PIDTYPE_PID
);
3004 /* What we to know is if the pid we have find is the
3005 * pid of a thread_group_leader. Testing for task
3006 * being a thread_group_leader is the obvious thing
3007 * todo but there is a window when it fails, due to
3008 * the pid transfer logic in de_thread.
3010 * So we perform the straight forward test of seeing
3011 * if the pid we have found is the pid of a thread
3012 * group leader, and don't worry if the task we have
3013 * found doesn't happen to be a thread group leader.
3014 * As we don't care in the case of readdir.
3016 if (!iter
.task
|| !has_group_leader_pid(iter
.task
)) {
3020 get_task_struct(iter
.task
);
3026 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + 1)
3028 static int proc_pid_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
3029 struct tgid_iter iter
)
3031 char name
[PROC_NUMBUF
];
3032 int len
= snprintf(name
, sizeof(name
), "%d", iter
.tgid
);
3033 return proc_fill_cache(filp
, dirent
, filldir
, name
, len
,
3034 proc_pid_instantiate
, iter
.task
, NULL
);
3037 static int fake_filldir(void *buf
, const char *name
, int namelen
,
3038 loff_t offset
, u64 ino
, unsigned d_type
)
3043 /* for the /proc/ directory itself, after non-process stuff has been done */
3044 int proc_pid_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
3046 struct tgid_iter iter
;
3047 struct pid_namespace
*ns
;
3048 filldir_t __filldir
;
3049 loff_t pos
= filp
->f_pos
;
3051 if (pos
>= PID_MAX_LIMIT
+ TGID_OFFSET
)
3054 if (pos
== TGID_OFFSET
- 1) {
3055 if (proc_fill_cache(filp
, dirent
, filldir
, "self", 4,
3056 NULL
, NULL
, NULL
) < 0)
3060 iter
.tgid
= pos
- TGID_OFFSET
;
3063 ns
= filp
->f_dentry
->d_sb
->s_fs_info
;
3064 for (iter
= next_tgid(ns
, iter
);
3066 iter
.tgid
+= 1, iter
= next_tgid(ns
, iter
)) {
3067 if (has_pid_permissions(ns
, iter
.task
, 2))
3068 __filldir
= filldir
;
3070 __filldir
= fake_filldir
;
3072 filp
->f_pos
= iter
.tgid
+ TGID_OFFSET
;
3073 if (proc_pid_fill_cache(filp
, dirent
, __filldir
, iter
) < 0) {
3074 put_task_struct(iter
.task
);
3078 filp
->f_pos
= PID_MAX_LIMIT
+ TGID_OFFSET
;
3086 static const struct pid_entry tid_base_stuff
[] = {
3087 DIR("fd", S_IRUSR
|S_IXUSR
, proc_fd_inode_operations
, proc_fd_operations
),
3088 DIR("fdinfo", S_IRUSR
|S_IXUSR
, proc_fdinfo_inode_operations
, proc_fdinfo_operations
),
3089 DIR("ns", S_IRUSR
|S_IXUGO
, proc_ns_dir_inode_operations
, proc_ns_dir_operations
),
3090 REG("environ", S_IRUSR
, proc_environ_operations
),
3091 INF("auxv", S_IRUSR
, proc_pid_auxv
),
3092 ONE("status", S_IRUGO
, proc_pid_status
),
3093 ONE("personality", S_IRUGO
, proc_pid_personality
),
3094 INF("limits", S_IRUGO
, proc_pid_limits
),
3095 #ifdef CONFIG_SCHED_DEBUG
3096 REG("sched", S_IRUGO
|S_IWUSR
, proc_pid_sched_operations
),
3098 REG("comm", S_IRUGO
|S_IWUSR
, proc_pid_set_comm_operations
),
3099 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3100 INF("syscall", S_IRUGO
, proc_pid_syscall
),
3102 INF("cmdline", S_IRUGO
, proc_pid_cmdline
),
3103 ONE("stat", S_IRUGO
, proc_tid_stat
),
3104 ONE("statm", S_IRUGO
, proc_pid_statm
),
3105 REG("maps", S_IRUGO
, proc_tid_maps_operations
),
3106 #ifdef CONFIG_CHECKPOINT_RESTORE
3107 REG("children", S_IRUGO
, proc_tid_children_operations
),
3110 REG("numa_maps", S_IRUGO
, proc_tid_numa_maps_operations
),
3112 REG("mem", S_IRUSR
|S_IWUSR
, proc_mem_operations
),
3113 LNK("cwd", proc_cwd_link
),
3114 LNK("root", proc_root_link
),
3115 LNK("exe", proc_exe_link
),
3116 REG("mounts", S_IRUGO
, proc_mounts_operations
),
3117 REG("mountinfo", S_IRUGO
, proc_mountinfo_operations
),
3118 #ifdef CONFIG_PROC_PAGE_MONITOR
3119 REG("clear_refs", S_IWUSR
, proc_clear_refs_operations
),
3120 REG("smaps", S_IRUGO
, proc_tid_smaps_operations
),
3121 REG("pagemap", S_IRUGO
, proc_pagemap_operations
),
3123 #ifdef CONFIG_SECURITY
3124 DIR("attr", S_IRUGO
|S_IXUGO
, proc_attr_dir_inode_operations
, proc_attr_dir_operations
),
3126 #ifdef CONFIG_KALLSYMS
3127 INF("wchan", S_IRUGO
, proc_pid_wchan
),
3129 #ifdef CONFIG_STACKTRACE
3130 ONE("stack", S_IRUGO
, proc_pid_stack
),
3132 #ifdef CONFIG_SCHEDSTATS
3133 INF("schedstat", S_IRUGO
, proc_pid_schedstat
),
3135 #ifdef CONFIG_LATENCYTOP
3136 REG("latency", S_IRUGO
, proc_lstats_operations
),
3138 #ifdef CONFIG_PROC_PID_CPUSET
3139 REG("cpuset", S_IRUGO
, proc_cpuset_operations
),
3141 #ifdef CONFIG_CGROUPS
3142 REG("cgroup", S_IRUGO
, proc_cgroup_operations
),
3144 INF("oom_score", S_IRUGO
, proc_oom_score
),
3145 REG("oom_adj", S_IRUGO
|S_IWUSR
, proc_oom_adj_operations
),
3146 REG("oom_score_adj", S_IRUGO
|S_IWUSR
, proc_oom_score_adj_operations
),
3147 #ifdef CONFIG_AUDITSYSCALL
3148 REG("loginuid", S_IWUSR
|S_IRUGO
, proc_loginuid_operations
),
3149 REG("sessionid", S_IRUGO
, proc_sessionid_operations
),
3151 #ifdef CONFIG_FAULT_INJECTION
3152 REG("make-it-fail", S_IRUGO
|S_IWUSR
, proc_fault_inject_operations
),
3154 #ifdef CONFIG_TASK_IO_ACCOUNTING
3155 INF("io", S_IRUSR
, proc_tid_io_accounting
),
3157 #ifdef CONFIG_HARDWALL
3158 INF("hardwall", S_IRUGO
, proc_pid_hardwall
),
3160 #ifdef CONFIG_USER_NS
3161 REG("uid_map", S_IRUGO
|S_IWUSR
, proc_uid_map_operations
),
3162 REG("gid_map", S_IRUGO
|S_IWUSR
, proc_gid_map_operations
),
3163 REG("projid_map", S_IRUGO
|S_IWUSR
, proc_projid_map_operations
),
3164 REG("setgroups", S_IRUGO
|S_IWUSR
, proc_setgroups_operations
),
3168 static int proc_tid_base_readdir(struct file
* filp
,
3169 void * dirent
, filldir_t filldir
)
3171 return proc_pident_readdir(filp
,dirent
,filldir
,
3172 tid_base_stuff
,ARRAY_SIZE(tid_base_stuff
));
3175 static struct dentry
*proc_tid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, unsigned int flags
)
3177 return proc_pident_lookup(dir
, dentry
,
3178 tid_base_stuff
, ARRAY_SIZE(tid_base_stuff
));
3181 static const struct file_operations proc_tid_base_operations
= {
3182 .read
= generic_read_dir
,
3183 .readdir
= proc_tid_base_readdir
,
3184 .llseek
= default_llseek
,
3187 static const struct inode_operations proc_tid_base_inode_operations
= {
3188 .lookup
= proc_tid_base_lookup
,
3189 .getattr
= pid_getattr
,
3190 .setattr
= proc_setattr
,
3193 static struct dentry
*proc_task_instantiate(struct inode
*dir
,
3194 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
3196 struct dentry
*error
= ERR_PTR(-ENOENT
);
3197 struct inode
*inode
;
3198 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
3202 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
3203 inode
->i_op
= &proc_tid_base_inode_operations
;
3204 inode
->i_fop
= &proc_tid_base_operations
;
3205 inode
->i_flags
|=S_IMMUTABLE
;
3207 set_nlink(inode
, 2 + pid_entry_count_dirs(tid_base_stuff
,
3208 ARRAY_SIZE(tid_base_stuff
)));
3210 d_set_d_op(dentry
, &pid_dentry_operations
);
3212 d_add(dentry
, inode
);
3213 /* Close the race of the process dying before we return the dentry */
3214 if (pid_revalidate(dentry
, 0))
3220 static struct dentry
*proc_task_lookup(struct inode
*dir
, struct dentry
* dentry
, unsigned int flags
)
3222 struct dentry
*result
= ERR_PTR(-ENOENT
);
3223 struct task_struct
*task
;
3224 struct task_struct
*leader
= get_proc_task(dir
);
3226 struct pid_namespace
*ns
;
3231 tid
= name_to_int(dentry
);
3235 ns
= dentry
->d_sb
->s_fs_info
;
3237 task
= find_task_by_pid_ns(tid
, ns
);
3239 get_task_struct(task
);
3243 if (!same_thread_group(leader
, task
))
3246 result
= proc_task_instantiate(dir
, dentry
, task
, NULL
);
3248 put_task_struct(task
);
3250 put_task_struct(leader
);
3256 * Find the first tid of a thread group to return to user space.
3258 * Usually this is just the thread group leader, but if the users
3259 * buffer was too small or there was a seek into the middle of the
3260 * directory we have more work todo.
3262 * In the case of a short read we start with find_task_by_pid.
3264 * In the case of a seek we start with the leader and walk nr
3267 static struct task_struct
*first_tid(struct task_struct
*leader
,
3268 int tid
, int nr
, struct pid_namespace
*ns
)
3270 struct task_struct
*pos
;
3273 /* Attempt to start with the pid of a thread */
3274 if (tid
&& (nr
> 0)) {
3275 pos
= find_task_by_pid_ns(tid
, ns
);
3276 if (pos
&& (pos
->group_leader
== leader
))
3280 /* If nr exceeds the number of threads there is nothing todo */
3282 if (nr
&& nr
>= get_nr_threads(leader
))
3285 /* If we haven't found our starting place yet start
3286 * with the leader and walk nr threads forward.
3288 for (pos
= leader
; nr
> 0; --nr
) {
3289 pos
= next_thread(pos
);
3290 if (pos
== leader
) {
3296 get_task_struct(pos
);
3303 * Find the next thread in the thread list.
3304 * Return NULL if there is an error or no next thread.
3306 * The reference to the input task_struct is released.
3308 static struct task_struct
*next_tid(struct task_struct
*start
)
3310 struct task_struct
*pos
= NULL
;
3312 if (pid_alive(start
)) {
3313 pos
= next_thread(start
);
3314 if (thread_group_leader(pos
))
3317 get_task_struct(pos
);
3320 put_task_struct(start
);
3324 static int proc_task_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
3325 struct task_struct
*task
, int tid
)
3327 char name
[PROC_NUMBUF
];
3328 int len
= snprintf(name
, sizeof(name
), "%d", tid
);
3329 return proc_fill_cache(filp
, dirent
, filldir
, name
, len
,
3330 proc_task_instantiate
, task
, NULL
);
3333 /* for the /proc/TGID/task/ directories */
3334 static int proc_task_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
3336 struct dentry
*dentry
= filp
->f_path
.dentry
;
3337 struct inode
*inode
= dentry
->d_inode
;
3338 struct task_struct
*leader
= NULL
;
3339 struct task_struct
*task
;
3340 int retval
= -ENOENT
;
3343 struct pid_namespace
*ns
;
3345 task
= get_proc_task(inode
);
3349 if (pid_alive(task
)) {
3350 leader
= task
->group_leader
;
3351 get_task_struct(leader
);
3354 put_task_struct(task
);
3359 switch ((unsigned long)filp
->f_pos
) {
3362 if (filldir(dirent
, ".", 1, filp
->f_pos
, ino
, DT_DIR
) < 0)
3367 ino
= parent_ino(dentry
);
3368 if (filldir(dirent
, "..", 2, filp
->f_pos
, ino
, DT_DIR
) < 0)
3374 /* f_version caches the tgid value that the last readdir call couldn't
3375 * return. lseek aka telldir automagically resets f_version to 0.
3377 ns
= filp
->f_dentry
->d_sb
->s_fs_info
;
3378 tid
= (int)filp
->f_version
;
3379 filp
->f_version
= 0;
3380 for (task
= first_tid(leader
, tid
, filp
->f_pos
- 2, ns
);
3382 task
= next_tid(task
), filp
->f_pos
++) {
3383 tid
= task_pid_nr_ns(task
, ns
);
3384 if (proc_task_fill_cache(filp
, dirent
, filldir
, task
, tid
) < 0) {
3385 /* returning this tgid failed, save it as the first
3386 * pid for the next readir call */
3387 filp
->f_version
= (u64
)tid
;
3388 put_task_struct(task
);
3393 put_task_struct(leader
);
3398 static int proc_task_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
3400 struct inode
*inode
= dentry
->d_inode
;
3401 struct task_struct
*p
= get_proc_task(inode
);
3402 generic_fillattr(inode
, stat
);
3405 stat
->nlink
+= get_nr_threads(p
);
3412 static const struct inode_operations proc_task_inode_operations
= {
3413 .lookup
= proc_task_lookup
,
3414 .getattr
= proc_task_getattr
,
3415 .setattr
= proc_setattr
,
3416 .permission
= proc_pid_permission
,
3419 static const struct file_operations proc_task_operations
= {
3420 .read
= generic_read_dir
,
3421 .readdir
= proc_task_readdir
,
3422 .llseek
= default_llseek
,