1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 1991, 1992 Linus Torvalds
7 * proc base directory handling functions
9 * 1999, Al Viro. Rewritten. Now it covers the whole per-process part.
10 * Instead of using magical inumbers to determine the kind of object
11 * we allocate and fill in-core inodes upon lookup. They don't even
12 * go into icache. We cache the reference to task_struct upon lookup too.
13 * Eventually it should become a filesystem in its own. We don't use the
14 * rest of procfs anymore.
20 * Bruna Moreira <bruna.moreira@indt.org.br>
21 * Edjard Mota <edjard.mota@indt.org.br>
22 * Ilias Biris <ilias.biris@indt.org.br>
23 * Mauricio Lin <mauricio.lin@indt.org.br>
25 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
27 * A new process specific entry (smaps) included in /proc. It shows the
28 * size of rss for each memory area. The maps entry lacks information
29 * about physical memory size (rss) for each mapped file, i.e.,
30 * rss information for executables and library files.
31 * This additional information is useful for any tools that need to know
32 * about physical memory consumption for a process specific library.
36 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
37 * Pud inclusion in the page table walking.
41 * 10LE Instituto Nokia de Tecnologia - INdT:
42 * A better way to walks through the page table as suggested by Hugh Dickins.
44 * Simo Piiroinen <simo.piiroinen@nokia.com>:
45 * Smaps information related to shared, private, clean and dirty pages.
47 * Paul Mundt <paul.mundt@nokia.com>:
48 * Overall revision about smaps.
51 #include <linux/uaccess.h>
53 #include <linux/errno.h>
54 #include <linux/time.h>
55 #include <linux/proc_fs.h>
56 #include <linux/stat.h>
57 #include <linux/task_io_accounting_ops.h>
58 #include <linux/init.h>
59 #include <linux/capability.h>
60 #include <linux/file.h>
61 #include <linux/fdtable.h>
62 #include <linux/string.h>
63 #include <linux/seq_file.h>
64 #include <linux/namei.h>
65 #include <linux/mnt_namespace.h>
67 #include <linux/swap.h>
68 #include <linux/rcupdate.h>
69 #include <linux/kallsyms.h>
70 #include <linux/stacktrace.h>
71 #include <linux/resource.h>
72 #include <linux/module.h>
73 #include <linux/mount.h>
74 #include <linux/security.h>
75 #include <linux/ptrace.h>
76 #include <linux/tracehook.h>
77 #include <linux/printk.h>
78 #include <linux/cgroup.h>
79 #include <linux/cpuset.h>
80 #include <linux/audit.h>
81 #include <linux/poll.h>
82 #include <linux/nsproxy.h>
83 #include <linux/oom.h>
84 #include <linux/elf.h>
85 #include <linux/pid_namespace.h>
86 #include <linux/user_namespace.h>
87 #include <linux/fs_struct.h>
88 #include <linux/slab.h>
89 #include <linux/sched/autogroup.h>
90 #include <linux/sched/mm.h>
91 #include <linux/sched/coredump.h>
92 #include <linux/sched/debug.h>
93 #include <linux/sched/stat.h>
94 #include <linux/flex_array.h>
95 #include <linux/posix-timers.h>
96 #ifdef CONFIG_HARDWALL
97 #include <asm/hardwall.h>
99 #include <trace/events/oom.h>
100 #include "internal.h"
104 * Implementing inode permission operations in /proc is almost
105 * certainly an error. Permission checks need to happen during
106 * each system call not at open time. The reason is that most of
107 * what we wish to check for permissions in /proc varies at runtime.
109 * The classic example of a problem is opening file descriptors
110 * in /proc for a task before it execs a suid executable.
114 static u8 nlink_tgid
;
120 const struct inode_operations
*iop
;
121 const struct file_operations
*fop
;
125 #define NOD(NAME, MODE, IOP, FOP, OP) { \
127 .len = sizeof(NAME) - 1, \
134 #define DIR(NAME, MODE, iops, fops) \
135 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
136 #define LNK(NAME, get_link) \
137 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
138 &proc_pid_link_inode_operations, NULL, \
139 { .proc_get_link = get_link } )
140 #define REG(NAME, MODE, fops) \
141 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
142 #define ONE(NAME, MODE, show) \
143 NOD(NAME, (S_IFREG|(MODE)), \
144 NULL, &proc_single_file_operations, \
145 { .proc_show = show } )
146 #define ATTR(LSM, NAME, MODE) \
147 NOD(NAME, (S_IFREG|(MODE)), \
148 NULL, &proc_pid_attr_operations, \
152 * Count the number of hardlinks for the pid_entry table, excluding the .
155 static unsigned int __init
pid_entry_nlink(const struct pid_entry
*entries
,
162 for (i
= 0; i
< n
; ++i
) {
163 if (S_ISDIR(entries
[i
].mode
))
170 static int get_task_root(struct task_struct
*task
, struct path
*root
)
172 int result
= -ENOENT
;
176 get_fs_root(task
->fs
, root
);
183 static int proc_cwd_link(struct dentry
*dentry
, struct path
*path
)
185 struct task_struct
*task
= get_proc_task(d_inode(dentry
));
186 int result
= -ENOENT
;
191 get_fs_pwd(task
->fs
, path
);
195 put_task_struct(task
);
200 static int proc_root_link(struct dentry
*dentry
, struct path
*path
)
202 struct task_struct
*task
= get_proc_task(d_inode(dentry
));
203 int result
= -ENOENT
;
206 result
= get_task_root(task
, path
);
207 put_task_struct(task
);
212 static ssize_t
proc_pid_cmdline_read(struct file
*file
, char __user
*buf
,
213 size_t _count
, loff_t
*pos
)
215 struct task_struct
*tsk
;
216 struct mm_struct
*mm
;
218 unsigned long count
= _count
;
219 unsigned long arg_start
, arg_end
, env_start
, env_end
;
220 unsigned long len1
, len2
, len
;
227 tsk
= get_proc_task(file_inode(file
));
230 mm
= get_task_mm(tsk
);
231 put_task_struct(tsk
);
234 /* Check if process spawned far enough to have cmdline. */
240 page
= (char *)__get_free_page(GFP_KERNEL
);
246 down_read(&mm
->mmap_sem
);
247 arg_start
= mm
->arg_start
;
248 arg_end
= mm
->arg_end
;
249 env_start
= mm
->env_start
;
250 env_end
= mm
->env_end
;
251 up_read(&mm
->mmap_sem
);
253 BUG_ON(arg_start
> arg_end
);
254 BUG_ON(env_start
> env_end
);
256 len1
= arg_end
- arg_start
;
257 len2
= env_end
- env_start
;
265 * Inherently racy -- command line shares address space
266 * with code and data.
268 rv
= access_remote_vm(mm
, arg_end
- 1, &c
, 1, 0);
275 /* Command line (set of strings) occupies whole ARGV. */
279 p
= arg_start
+ *pos
;
281 while (count
> 0 && len
> 0) {
285 _count
= min3(count
, len
, PAGE_SIZE
);
286 nr_read
= access_remote_vm(mm
, p
, page
, _count
, 0);
292 if (copy_to_user(buf
, page
, nr_read
)) {
305 * Command line (1 string) occupies ARGV and
312 { .p
= arg_start
, .len
= len1
},
313 { .p
= env_start
, .len
= len2
},
319 while (i
< 2 && pos1
>= cmdline
[i
].len
) {
320 pos1
-= cmdline
[i
].len
;
324 p
= cmdline
[i
].p
+ pos1
;
325 len
= cmdline
[i
].len
- pos1
;
326 while (count
> 0 && len
> 0) {
327 unsigned int _count
, l
;
331 _count
= min3(count
, len
, PAGE_SIZE
);
332 nr_read
= access_remote_vm(mm
, p
, page
, _count
, 0);
339 * Command line can be shorter than whole ARGV
340 * even if last "marker" byte says it is not.
343 l
= strnlen(page
, nr_read
);
349 if (copy_to_user(buf
, page
, nr_read
)) {
364 /* Only first chunk can be read partially. */
371 free_page((unsigned long)page
);
379 static const struct file_operations proc_pid_cmdline_ops
= {
380 .read
= proc_pid_cmdline_read
,
381 .llseek
= generic_file_llseek
,
384 #ifdef CONFIG_KALLSYMS
386 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
387 * Returns the resolved symbol. If that fails, simply return the address.
389 static int proc_pid_wchan(struct seq_file
*m
, struct pid_namespace
*ns
,
390 struct pid
*pid
, struct task_struct
*task
)
393 char symname
[KSYM_NAME_LEN
];
395 wchan
= get_wchan(task
);
397 if (wchan
&& ptrace_may_access(task
, PTRACE_MODE_READ_FSCREDS
)
398 && !lookup_symbol_name(wchan
, symname
))
399 seq_printf(m
, "%s", symname
);
405 #endif /* CONFIG_KALLSYMS */
407 static int lock_trace(struct task_struct
*task
)
409 int err
= mutex_lock_killable(&task
->signal
->cred_guard_mutex
);
412 if (!ptrace_may_access(task
, PTRACE_MODE_ATTACH_FSCREDS
)) {
413 mutex_unlock(&task
->signal
->cred_guard_mutex
);
419 static void unlock_trace(struct task_struct
*task
)
421 mutex_unlock(&task
->signal
->cred_guard_mutex
);
424 #ifdef CONFIG_STACKTRACE
426 #define MAX_STACK_TRACE_DEPTH 64
428 static int proc_pid_stack(struct seq_file
*m
, struct pid_namespace
*ns
,
429 struct pid
*pid
, struct task_struct
*task
)
431 struct stack_trace trace
;
432 unsigned long *entries
;
436 entries
= kmalloc(MAX_STACK_TRACE_DEPTH
* sizeof(*entries
), GFP_KERNEL
);
440 trace
.nr_entries
= 0;
441 trace
.max_entries
= MAX_STACK_TRACE_DEPTH
;
442 trace
.entries
= entries
;
445 err
= lock_trace(task
);
447 save_stack_trace_tsk(task
, &trace
);
449 for (i
= 0; i
< trace
.nr_entries
; i
++) {
450 seq_printf(m
, "[<0>] %pB\n", (void *)entries
[i
]);
460 #ifdef CONFIG_SCHED_INFO
462 * Provides /proc/PID/schedstat
464 static int proc_pid_schedstat(struct seq_file
*m
, struct pid_namespace
*ns
,
465 struct pid
*pid
, struct task_struct
*task
)
467 if (unlikely(!sched_info_on()))
468 seq_printf(m
, "0 0 0\n");
470 seq_printf(m
, "%llu %llu %lu\n",
471 (unsigned long long)task
->se
.sum_exec_runtime
,
472 (unsigned long long)task
->sched_info
.run_delay
,
473 task
->sched_info
.pcount
);
479 #ifdef CONFIG_LATENCYTOP
480 static int lstats_show_proc(struct seq_file
*m
, void *v
)
483 struct inode
*inode
= m
->private;
484 struct task_struct
*task
= get_proc_task(inode
);
488 seq_puts(m
, "Latency Top version : v0.1\n");
489 for (i
= 0; i
< 32; i
++) {
490 struct latency_record
*lr
= &task
->latency_record
[i
];
491 if (lr
->backtrace
[0]) {
493 seq_printf(m
, "%i %li %li",
494 lr
->count
, lr
->time
, lr
->max
);
495 for (q
= 0; q
< LT_BACKTRACEDEPTH
; q
++) {
496 unsigned long bt
= lr
->backtrace
[q
];
501 seq_printf(m
, " %ps", (void *)bt
);
507 put_task_struct(task
);
511 static int lstats_open(struct inode
*inode
, struct file
*file
)
513 return single_open(file
, lstats_show_proc
, inode
);
516 static ssize_t
lstats_write(struct file
*file
, const char __user
*buf
,
517 size_t count
, loff_t
*offs
)
519 struct task_struct
*task
= get_proc_task(file_inode(file
));
523 clear_all_latency_tracing(task
);
524 put_task_struct(task
);
529 static const struct file_operations proc_lstats_operations
= {
532 .write
= lstats_write
,
534 .release
= single_release
,
539 static int proc_oom_score(struct seq_file
*m
, struct pid_namespace
*ns
,
540 struct pid
*pid
, struct task_struct
*task
)
542 unsigned long totalpages
= totalram_pages
+ total_swap_pages
;
543 unsigned long points
= 0;
545 points
= oom_badness(task
, NULL
, NULL
, totalpages
) *
547 seq_printf(m
, "%lu\n", points
);
557 static const struct limit_names lnames
[RLIM_NLIMITS
] = {
558 [RLIMIT_CPU
] = {"Max cpu time", "seconds"},
559 [RLIMIT_FSIZE
] = {"Max file size", "bytes"},
560 [RLIMIT_DATA
] = {"Max data size", "bytes"},
561 [RLIMIT_STACK
] = {"Max stack size", "bytes"},
562 [RLIMIT_CORE
] = {"Max core file size", "bytes"},
563 [RLIMIT_RSS
] = {"Max resident set", "bytes"},
564 [RLIMIT_NPROC
] = {"Max processes", "processes"},
565 [RLIMIT_NOFILE
] = {"Max open files", "files"},
566 [RLIMIT_MEMLOCK
] = {"Max locked memory", "bytes"},
567 [RLIMIT_AS
] = {"Max address space", "bytes"},
568 [RLIMIT_LOCKS
] = {"Max file locks", "locks"},
569 [RLIMIT_SIGPENDING
] = {"Max pending signals", "signals"},
570 [RLIMIT_MSGQUEUE
] = {"Max msgqueue size", "bytes"},
571 [RLIMIT_NICE
] = {"Max nice priority", NULL
},
572 [RLIMIT_RTPRIO
] = {"Max realtime priority", NULL
},
573 [RLIMIT_RTTIME
] = {"Max realtime timeout", "us"},
576 /* Display limits for a process */
577 static int proc_pid_limits(struct seq_file
*m
, struct pid_namespace
*ns
,
578 struct pid
*pid
, struct task_struct
*task
)
583 struct rlimit rlim
[RLIM_NLIMITS
];
585 if (!lock_task_sighand(task
, &flags
))
587 memcpy(rlim
, task
->signal
->rlim
, sizeof(struct rlimit
) * RLIM_NLIMITS
);
588 unlock_task_sighand(task
, &flags
);
591 * print the file header
593 seq_printf(m
, "%-25s %-20s %-20s %-10s\n",
594 "Limit", "Soft Limit", "Hard Limit", "Units");
596 for (i
= 0; i
< RLIM_NLIMITS
; i
++) {
597 if (rlim
[i
].rlim_cur
== RLIM_INFINITY
)
598 seq_printf(m
, "%-25s %-20s ",
599 lnames
[i
].name
, "unlimited");
601 seq_printf(m
, "%-25s %-20lu ",
602 lnames
[i
].name
, rlim
[i
].rlim_cur
);
604 if (rlim
[i
].rlim_max
== RLIM_INFINITY
)
605 seq_printf(m
, "%-20s ", "unlimited");
607 seq_printf(m
, "%-20lu ", rlim
[i
].rlim_max
);
610 seq_printf(m
, "%-10s\n", lnames
[i
].unit
);
618 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
619 static int proc_pid_syscall(struct seq_file
*m
, struct pid_namespace
*ns
,
620 struct pid
*pid
, struct task_struct
*task
)
623 unsigned long args
[6], sp
, pc
;
626 res
= lock_trace(task
);
630 if (task_current_syscall(task
, &nr
, args
, 6, &sp
, &pc
))
631 seq_puts(m
, "running\n");
633 seq_printf(m
, "%ld 0x%lx 0x%lx\n", nr
, sp
, pc
);
636 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
638 args
[0], args
[1], args
[2], args
[3], args
[4], args
[5],
644 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
646 /************************************************************************/
647 /* Here the fs part begins */
648 /************************************************************************/
650 /* permission checks */
651 static int proc_fd_access_allowed(struct inode
*inode
)
653 struct task_struct
*task
;
655 /* Allow access to a task's file descriptors if it is us or we
656 * may use ptrace attach to the process and find out that
659 task
= get_proc_task(inode
);
661 allowed
= ptrace_may_access(task
, PTRACE_MODE_READ_FSCREDS
);
662 put_task_struct(task
);
667 int proc_setattr(struct dentry
*dentry
, struct iattr
*attr
)
670 struct inode
*inode
= d_inode(dentry
);
671 struct user_namespace
*s_user_ns
;
673 if (attr
->ia_valid
& ATTR_MODE
)
676 /* Don't let anyone mess with weird proc files */
677 s_user_ns
= inode
->i_sb
->s_user_ns
;
678 if (!kuid_has_mapping(s_user_ns
, inode
->i_uid
) ||
679 !kgid_has_mapping(s_user_ns
, inode
->i_gid
))
682 error
= setattr_prepare(dentry
, attr
);
686 setattr_copy(inode
, attr
);
687 mark_inode_dirty(inode
);
692 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
693 * or euid/egid (for hide_pid_min=2)?
695 static bool has_pid_permissions(struct pid_namespace
*pid
,
696 struct task_struct
*task
,
699 if (pid
->hide_pid
< hide_pid_min
)
701 if (in_group_p(pid
->pid_gid
))
703 return ptrace_may_access(task
, PTRACE_MODE_READ_FSCREDS
);
707 static int proc_pid_permission(struct inode
*inode
, int mask
)
709 struct pid_namespace
*pid
= inode
->i_sb
->s_fs_info
;
710 struct task_struct
*task
;
713 task
= get_proc_task(inode
);
716 has_perms
= has_pid_permissions(pid
, task
, HIDEPID_NO_ACCESS
);
717 put_task_struct(task
);
720 if (pid
->hide_pid
== HIDEPID_INVISIBLE
) {
722 * Let's make getdents(), stat(), and open()
723 * consistent with each other. If a process
724 * may not stat() a file, it shouldn't be seen
732 return generic_permission(inode
, mask
);
737 static const struct inode_operations proc_def_inode_operations
= {
738 .setattr
= proc_setattr
,
741 static int proc_single_show(struct seq_file
*m
, void *v
)
743 struct inode
*inode
= m
->private;
744 struct pid_namespace
*ns
;
746 struct task_struct
*task
;
749 ns
= inode
->i_sb
->s_fs_info
;
750 pid
= proc_pid(inode
);
751 task
= get_pid_task(pid
, PIDTYPE_PID
);
755 ret
= PROC_I(inode
)->op
.proc_show(m
, ns
, pid
, task
);
757 put_task_struct(task
);
761 static int proc_single_open(struct inode
*inode
, struct file
*filp
)
763 return single_open(filp
, proc_single_show
, inode
);
766 static const struct file_operations proc_single_file_operations
= {
767 .open
= proc_single_open
,
770 .release
= single_release
,
774 struct mm_struct
*proc_mem_open(struct inode
*inode
, unsigned int mode
)
776 struct task_struct
*task
= get_proc_task(inode
);
777 struct mm_struct
*mm
= ERR_PTR(-ESRCH
);
780 mm
= mm_access(task
, mode
| PTRACE_MODE_FSCREDS
);
781 put_task_struct(task
);
783 if (!IS_ERR_OR_NULL(mm
)) {
784 /* ensure this mm_struct can't be freed */
786 /* but do not pin its memory */
794 static int __mem_open(struct inode
*inode
, struct file
*file
, unsigned int mode
)
796 struct mm_struct
*mm
= proc_mem_open(inode
, mode
);
801 file
->private_data
= mm
;
805 static int mem_open(struct inode
*inode
, struct file
*file
)
807 int ret
= __mem_open(inode
, file
, PTRACE_MODE_ATTACH
);
809 /* OK to pass negative loff_t, we can catch out-of-range */
810 file
->f_mode
|= FMODE_UNSIGNED_OFFSET
;
815 static ssize_t
mem_rw(struct file
*file
, char __user
*buf
,
816 size_t count
, loff_t
*ppos
, int write
)
818 struct mm_struct
*mm
= file
->private_data
;
819 unsigned long addr
= *ppos
;
827 page
= (char *)__get_free_page(GFP_KERNEL
);
832 if (!mmget_not_zero(mm
))
835 flags
= FOLL_FORCE
| (write
? FOLL_WRITE
: 0);
838 int this_len
= min_t(int, count
, PAGE_SIZE
);
840 if (write
&& copy_from_user(page
, buf
, this_len
)) {
845 this_len
= access_remote_vm(mm
, addr
, page
, this_len
, flags
);
852 if (!write
&& copy_to_user(buf
, page
, this_len
)) {
866 free_page((unsigned long) page
);
870 static ssize_t
mem_read(struct file
*file
, char __user
*buf
,
871 size_t count
, loff_t
*ppos
)
873 return mem_rw(file
, buf
, count
, ppos
, 0);
876 static ssize_t
mem_write(struct file
*file
, const char __user
*buf
,
877 size_t count
, loff_t
*ppos
)
879 return mem_rw(file
, (char __user
*)buf
, count
, ppos
, 1);
882 loff_t
mem_lseek(struct file
*file
, loff_t offset
, int orig
)
886 file
->f_pos
= offset
;
889 file
->f_pos
+= offset
;
894 force_successful_syscall_return();
898 static int mem_release(struct inode
*inode
, struct file
*file
)
900 struct mm_struct
*mm
= file
->private_data
;
906 static const struct file_operations proc_mem_operations
= {
911 .release
= mem_release
,
914 static int environ_open(struct inode
*inode
, struct file
*file
)
916 return __mem_open(inode
, file
, PTRACE_MODE_READ
);
919 static ssize_t
environ_read(struct file
*file
, char __user
*buf
,
920 size_t count
, loff_t
*ppos
)
923 unsigned long src
= *ppos
;
925 struct mm_struct
*mm
= file
->private_data
;
926 unsigned long env_start
, env_end
;
928 /* Ensure the process spawned far enough to have an environment. */
929 if (!mm
|| !mm
->env_end
)
932 page
= (char *)__get_free_page(GFP_KERNEL
);
937 if (!mmget_not_zero(mm
))
940 down_read(&mm
->mmap_sem
);
941 env_start
= mm
->env_start
;
942 env_end
= mm
->env_end
;
943 up_read(&mm
->mmap_sem
);
946 size_t this_len
, max_len
;
949 if (src
>= (env_end
- env_start
))
952 this_len
= env_end
- (env_start
+ src
);
954 max_len
= min_t(size_t, PAGE_SIZE
, count
);
955 this_len
= min(max_len
, this_len
);
957 retval
= access_remote_vm(mm
, (env_start
+ src
), page
, this_len
, 0);
964 if (copy_to_user(buf
, page
, retval
)) {
978 free_page((unsigned long) page
);
982 static const struct file_operations proc_environ_operations
= {
983 .open
= environ_open
,
984 .read
= environ_read
,
985 .llseek
= generic_file_llseek
,
986 .release
= mem_release
,
989 static int auxv_open(struct inode
*inode
, struct file
*file
)
991 return __mem_open(inode
, file
, PTRACE_MODE_READ_FSCREDS
);
994 static ssize_t
auxv_read(struct file
*file
, char __user
*buf
,
995 size_t count
, loff_t
*ppos
)
997 struct mm_struct
*mm
= file
->private_data
;
998 unsigned int nwords
= 0;
1004 } while (mm
->saved_auxv
[nwords
- 2] != 0); /* AT_NULL */
1005 return simple_read_from_buffer(buf
, count
, ppos
, mm
->saved_auxv
,
1006 nwords
* sizeof(mm
->saved_auxv
[0]));
1009 static const struct file_operations proc_auxv_operations
= {
1012 .llseek
= generic_file_llseek
,
1013 .release
= mem_release
,
1016 static ssize_t
oom_adj_read(struct file
*file
, char __user
*buf
, size_t count
,
1019 struct task_struct
*task
= get_proc_task(file_inode(file
));
1020 char buffer
[PROC_NUMBUF
];
1021 int oom_adj
= OOM_ADJUST_MIN
;
1026 if (task
->signal
->oom_score_adj
== OOM_SCORE_ADJ_MAX
)
1027 oom_adj
= OOM_ADJUST_MAX
;
1029 oom_adj
= (task
->signal
->oom_score_adj
* -OOM_DISABLE
) /
1031 put_task_struct(task
);
1032 len
= snprintf(buffer
, sizeof(buffer
), "%d\n", oom_adj
);
1033 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
1036 static int __set_oom_adj(struct file
*file
, int oom_adj
, bool legacy
)
1038 static DEFINE_MUTEX(oom_adj_mutex
);
1039 struct mm_struct
*mm
= NULL
;
1040 struct task_struct
*task
;
1043 task
= get_proc_task(file_inode(file
));
1047 mutex_lock(&oom_adj_mutex
);
1049 if (oom_adj
< task
->signal
->oom_score_adj
&&
1050 !capable(CAP_SYS_RESOURCE
)) {
1055 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
1056 * /proc/pid/oom_score_adj instead.
1058 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
1059 current
->comm
, task_pid_nr(current
), task_pid_nr(task
),
1062 if ((short)oom_adj
< task
->signal
->oom_score_adj_min
&&
1063 !capable(CAP_SYS_RESOURCE
)) {
1070 * Make sure we will check other processes sharing the mm if this is
1071 * not vfrok which wants its own oom_score_adj.
1072 * pin the mm so it doesn't go away and get reused after task_unlock
1074 if (!task
->vfork_done
) {
1075 struct task_struct
*p
= find_lock_task_mm(task
);
1078 if (atomic_read(&p
->mm
->mm_users
) > 1) {
1086 task
->signal
->oom_score_adj
= oom_adj
;
1087 if (!legacy
&& has_capability_noaudit(current
, CAP_SYS_RESOURCE
))
1088 task
->signal
->oom_score_adj_min
= (short)oom_adj
;
1089 trace_oom_score_adj_update(task
);
1092 struct task_struct
*p
;
1095 for_each_process(p
) {
1096 if (same_thread_group(task
, p
))
1099 /* do not touch kernel threads or the global init */
1100 if (p
->flags
& PF_KTHREAD
|| is_global_init(p
))
1104 if (!p
->vfork_done
&& process_shares_mm(p
, mm
)) {
1105 pr_info("updating oom_score_adj for %d (%s) from %d to %d because it shares mm with %d (%s). Report if this is unexpected.\n",
1106 task_pid_nr(p
), p
->comm
,
1107 p
->signal
->oom_score_adj
, oom_adj
,
1108 task_pid_nr(task
), task
->comm
);
1109 p
->signal
->oom_score_adj
= oom_adj
;
1110 if (!legacy
&& has_capability_noaudit(current
, CAP_SYS_RESOURCE
))
1111 p
->signal
->oom_score_adj_min
= (short)oom_adj
;
1119 mutex_unlock(&oom_adj_mutex
);
1120 put_task_struct(task
);
1125 * /proc/pid/oom_adj exists solely for backwards compatibility with previous
1126 * kernels. The effective policy is defined by oom_score_adj, which has a
1127 * different scale: oom_adj grew exponentially and oom_score_adj grows linearly.
1128 * Values written to oom_adj are simply mapped linearly to oom_score_adj.
1129 * Processes that become oom disabled via oom_adj will still be oom disabled
1130 * with this implementation.
1132 * oom_adj cannot be removed since existing userspace binaries use it.
1134 static ssize_t
oom_adj_write(struct file
*file
, const char __user
*buf
,
1135 size_t count
, loff_t
*ppos
)
1137 char buffer
[PROC_NUMBUF
];
1141 memset(buffer
, 0, sizeof(buffer
));
1142 if (count
> sizeof(buffer
) - 1)
1143 count
= sizeof(buffer
) - 1;
1144 if (copy_from_user(buffer
, buf
, count
)) {
1149 err
= kstrtoint(strstrip(buffer
), 0, &oom_adj
);
1152 if ((oom_adj
< OOM_ADJUST_MIN
|| oom_adj
> OOM_ADJUST_MAX
) &&
1153 oom_adj
!= OOM_DISABLE
) {
1159 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1160 * value is always attainable.
1162 if (oom_adj
== OOM_ADJUST_MAX
)
1163 oom_adj
= OOM_SCORE_ADJ_MAX
;
1165 oom_adj
= (oom_adj
* OOM_SCORE_ADJ_MAX
) / -OOM_DISABLE
;
1167 err
= __set_oom_adj(file
, oom_adj
, true);
1169 return err
< 0 ? err
: count
;
1172 static const struct file_operations proc_oom_adj_operations
= {
1173 .read
= oom_adj_read
,
1174 .write
= oom_adj_write
,
1175 .llseek
= generic_file_llseek
,
1178 static ssize_t
oom_score_adj_read(struct file
*file
, char __user
*buf
,
1179 size_t count
, loff_t
*ppos
)
1181 struct task_struct
*task
= get_proc_task(file_inode(file
));
1182 char buffer
[PROC_NUMBUF
];
1183 short oom_score_adj
= OOM_SCORE_ADJ_MIN
;
1188 oom_score_adj
= task
->signal
->oom_score_adj
;
1189 put_task_struct(task
);
1190 len
= snprintf(buffer
, sizeof(buffer
), "%hd\n", oom_score_adj
);
1191 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
1194 static ssize_t
oom_score_adj_write(struct file
*file
, const char __user
*buf
,
1195 size_t count
, loff_t
*ppos
)
1197 char buffer
[PROC_NUMBUF
];
1201 memset(buffer
, 0, sizeof(buffer
));
1202 if (count
> sizeof(buffer
) - 1)
1203 count
= sizeof(buffer
) - 1;
1204 if (copy_from_user(buffer
, buf
, count
)) {
1209 err
= kstrtoint(strstrip(buffer
), 0, &oom_score_adj
);
1212 if (oom_score_adj
< OOM_SCORE_ADJ_MIN
||
1213 oom_score_adj
> OOM_SCORE_ADJ_MAX
) {
1218 err
= __set_oom_adj(file
, oom_score_adj
, false);
1220 return err
< 0 ? err
: count
;
1223 static const struct file_operations proc_oom_score_adj_operations
= {
1224 .read
= oom_score_adj_read
,
1225 .write
= oom_score_adj_write
,
1226 .llseek
= default_llseek
,
1229 #ifdef CONFIG_AUDITSYSCALL
1230 #define TMPBUFLEN 11
1231 static ssize_t
proc_loginuid_read(struct file
* file
, char __user
* buf
,
1232 size_t count
, loff_t
*ppos
)
1234 struct inode
* inode
= file_inode(file
);
1235 struct task_struct
*task
= get_proc_task(inode
);
1237 char tmpbuf
[TMPBUFLEN
];
1241 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
1242 from_kuid(file
->f_cred
->user_ns
,
1243 audit_get_loginuid(task
)));
1244 put_task_struct(task
);
1245 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1248 static ssize_t
proc_loginuid_write(struct file
* file
, const char __user
* buf
,
1249 size_t count
, loff_t
*ppos
)
1251 struct inode
* inode
= file_inode(file
);
1257 if (current
!= pid_task(proc_pid(inode
), PIDTYPE_PID
)) {
1264 /* No partial writes. */
1268 rv
= kstrtou32_from_user(buf
, count
, 10, &loginuid
);
1272 /* is userspace tring to explicitly UNSET the loginuid? */
1273 if (loginuid
== AUDIT_UID_UNSET
) {
1274 kloginuid
= INVALID_UID
;
1276 kloginuid
= make_kuid(file
->f_cred
->user_ns
, loginuid
);
1277 if (!uid_valid(kloginuid
))
1281 rv
= audit_set_loginuid(kloginuid
);
1287 static const struct file_operations proc_loginuid_operations
= {
1288 .read
= proc_loginuid_read
,
1289 .write
= proc_loginuid_write
,
1290 .llseek
= generic_file_llseek
,
1293 static ssize_t
proc_sessionid_read(struct file
* file
, char __user
* buf
,
1294 size_t count
, loff_t
*ppos
)
1296 struct inode
* inode
= file_inode(file
);
1297 struct task_struct
*task
= get_proc_task(inode
);
1299 char tmpbuf
[TMPBUFLEN
];
1303 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
1304 audit_get_sessionid(task
));
1305 put_task_struct(task
);
1306 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1309 static const struct file_operations proc_sessionid_operations
= {
1310 .read
= proc_sessionid_read
,
1311 .llseek
= generic_file_llseek
,
1315 #ifdef CONFIG_FAULT_INJECTION
1316 static ssize_t
proc_fault_inject_read(struct file
* file
, char __user
* buf
,
1317 size_t count
, loff_t
*ppos
)
1319 struct task_struct
*task
= get_proc_task(file_inode(file
));
1320 char buffer
[PROC_NUMBUF
];
1326 make_it_fail
= task
->make_it_fail
;
1327 put_task_struct(task
);
1329 len
= snprintf(buffer
, sizeof(buffer
), "%i\n", make_it_fail
);
1331 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
1334 static ssize_t
proc_fault_inject_write(struct file
* file
,
1335 const char __user
* buf
, size_t count
, loff_t
*ppos
)
1337 struct task_struct
*task
;
1338 char buffer
[PROC_NUMBUF
];
1342 if (!capable(CAP_SYS_RESOURCE
))
1344 memset(buffer
, 0, sizeof(buffer
));
1345 if (count
> sizeof(buffer
) - 1)
1346 count
= sizeof(buffer
) - 1;
1347 if (copy_from_user(buffer
, buf
, count
))
1349 rv
= kstrtoint(strstrip(buffer
), 0, &make_it_fail
);
1352 if (make_it_fail
< 0 || make_it_fail
> 1)
1355 task
= get_proc_task(file_inode(file
));
1358 task
->make_it_fail
= make_it_fail
;
1359 put_task_struct(task
);
1364 static const struct file_operations proc_fault_inject_operations
= {
1365 .read
= proc_fault_inject_read
,
1366 .write
= proc_fault_inject_write
,
1367 .llseek
= generic_file_llseek
,
1370 static ssize_t
proc_fail_nth_write(struct file
*file
, const char __user
*buf
,
1371 size_t count
, loff_t
*ppos
)
1373 struct task_struct
*task
;
1377 err
= kstrtouint_from_user(buf
, count
, 0, &n
);
1381 task
= get_proc_task(file_inode(file
));
1384 WRITE_ONCE(task
->fail_nth
, n
);
1385 put_task_struct(task
);
1390 static ssize_t
proc_fail_nth_read(struct file
*file
, char __user
*buf
,
1391 size_t count
, loff_t
*ppos
)
1393 struct task_struct
*task
;
1394 char numbuf
[PROC_NUMBUF
];
1397 task
= get_proc_task(file_inode(file
));
1400 len
= snprintf(numbuf
, sizeof(numbuf
), "%u\n",
1401 READ_ONCE(task
->fail_nth
));
1402 len
= simple_read_from_buffer(buf
, count
, ppos
, numbuf
, len
);
1403 put_task_struct(task
);
1408 static const struct file_operations proc_fail_nth_operations
= {
1409 .read
= proc_fail_nth_read
,
1410 .write
= proc_fail_nth_write
,
1415 #ifdef CONFIG_SCHED_DEBUG
1417 * Print out various scheduling related per-task fields:
1419 static int sched_show(struct seq_file
*m
, void *v
)
1421 struct inode
*inode
= m
->private;
1422 struct pid_namespace
*ns
= inode
->i_sb
->s_fs_info
;
1423 struct task_struct
*p
;
1425 p
= get_proc_task(inode
);
1428 proc_sched_show_task(p
, ns
, m
);
1436 sched_write(struct file
*file
, const char __user
*buf
,
1437 size_t count
, loff_t
*offset
)
1439 struct inode
*inode
= file_inode(file
);
1440 struct task_struct
*p
;
1442 p
= get_proc_task(inode
);
1445 proc_sched_set_task(p
);
1452 static int sched_open(struct inode
*inode
, struct file
*filp
)
1454 return single_open(filp
, sched_show
, inode
);
1457 static const struct file_operations proc_pid_sched_operations
= {
1460 .write
= sched_write
,
1461 .llseek
= seq_lseek
,
1462 .release
= single_release
,
1467 #ifdef CONFIG_SCHED_AUTOGROUP
1469 * Print out autogroup related information:
1471 static int sched_autogroup_show(struct seq_file
*m
, void *v
)
1473 struct inode
*inode
= m
->private;
1474 struct task_struct
*p
;
1476 p
= get_proc_task(inode
);
1479 proc_sched_autogroup_show_task(p
, m
);
1487 sched_autogroup_write(struct file
*file
, const char __user
*buf
,
1488 size_t count
, loff_t
*offset
)
1490 struct inode
*inode
= file_inode(file
);
1491 struct task_struct
*p
;
1492 char buffer
[PROC_NUMBUF
];
1496 memset(buffer
, 0, sizeof(buffer
));
1497 if (count
> sizeof(buffer
) - 1)
1498 count
= sizeof(buffer
) - 1;
1499 if (copy_from_user(buffer
, buf
, count
))
1502 err
= kstrtoint(strstrip(buffer
), 0, &nice
);
1506 p
= get_proc_task(inode
);
1510 err
= proc_sched_autogroup_set_nice(p
, nice
);
1519 static int sched_autogroup_open(struct inode
*inode
, struct file
*filp
)
1523 ret
= single_open(filp
, sched_autogroup_show
, NULL
);
1525 struct seq_file
*m
= filp
->private_data
;
1532 static const struct file_operations proc_pid_sched_autogroup_operations
= {
1533 .open
= sched_autogroup_open
,
1535 .write
= sched_autogroup_write
,
1536 .llseek
= seq_lseek
,
1537 .release
= single_release
,
1540 #endif /* CONFIG_SCHED_AUTOGROUP */
1542 static ssize_t
comm_write(struct file
*file
, const char __user
*buf
,
1543 size_t count
, loff_t
*offset
)
1545 struct inode
*inode
= file_inode(file
);
1546 struct task_struct
*p
;
1547 char buffer
[TASK_COMM_LEN
];
1548 const size_t maxlen
= sizeof(buffer
) - 1;
1550 memset(buffer
, 0, sizeof(buffer
));
1551 if (copy_from_user(buffer
, buf
, count
> maxlen
? maxlen
: count
))
1554 p
= get_proc_task(inode
);
1558 if (same_thread_group(current
, p
))
1559 set_task_comm(p
, buffer
);
1568 static int comm_show(struct seq_file
*m
, void *v
)
1570 struct inode
*inode
= m
->private;
1571 struct task_struct
*p
;
1573 p
= get_proc_task(inode
);
1578 seq_printf(m
, "%s\n", p
->comm
);
1586 static int comm_open(struct inode
*inode
, struct file
*filp
)
1588 return single_open(filp
, comm_show
, inode
);
1591 static const struct file_operations proc_pid_set_comm_operations
= {
1594 .write
= comm_write
,
1595 .llseek
= seq_lseek
,
1596 .release
= single_release
,
1599 static int proc_exe_link(struct dentry
*dentry
, struct path
*exe_path
)
1601 struct task_struct
*task
;
1602 struct file
*exe_file
;
1604 task
= get_proc_task(d_inode(dentry
));
1607 exe_file
= get_task_exe_file(task
);
1608 put_task_struct(task
);
1610 *exe_path
= exe_file
->f_path
;
1611 path_get(&exe_file
->f_path
);
1618 static const char *proc_pid_get_link(struct dentry
*dentry
,
1619 struct inode
*inode
,
1620 struct delayed_call
*done
)
1623 int error
= -EACCES
;
1626 return ERR_PTR(-ECHILD
);
1628 /* Are we allowed to snoop on the tasks file descriptors? */
1629 if (!proc_fd_access_allowed(inode
))
1632 error
= PROC_I(inode
)->op
.proc_get_link(dentry
, &path
);
1636 nd_jump_link(&path
);
1639 return ERR_PTR(error
);
1642 static int do_proc_readlink(struct path
*path
, char __user
*buffer
, int buflen
)
1644 char *tmp
= (char *)__get_free_page(GFP_KERNEL
);
1651 pathname
= d_path(path
, tmp
, PAGE_SIZE
);
1652 len
= PTR_ERR(pathname
);
1653 if (IS_ERR(pathname
))
1655 len
= tmp
+ PAGE_SIZE
- 1 - pathname
;
1659 if (copy_to_user(buffer
, pathname
, len
))
1662 free_page((unsigned long)tmp
);
1666 static int proc_pid_readlink(struct dentry
* dentry
, char __user
* buffer
, int buflen
)
1668 int error
= -EACCES
;
1669 struct inode
*inode
= d_inode(dentry
);
1672 /* Are we allowed to snoop on the tasks file descriptors? */
1673 if (!proc_fd_access_allowed(inode
))
1676 error
= PROC_I(inode
)->op
.proc_get_link(dentry
, &path
);
1680 error
= do_proc_readlink(&path
, buffer
, buflen
);
1686 const struct inode_operations proc_pid_link_inode_operations
= {
1687 .readlink
= proc_pid_readlink
,
1688 .get_link
= proc_pid_get_link
,
1689 .setattr
= proc_setattr
,
1693 /* building an inode */
1695 void task_dump_owner(struct task_struct
*task
, umode_t mode
,
1696 kuid_t
*ruid
, kgid_t
*rgid
)
1698 /* Depending on the state of dumpable compute who should own a
1699 * proc file for a task.
1701 const struct cred
*cred
;
1705 /* Default to the tasks effective ownership */
1707 cred
= __task_cred(task
);
1713 * Before the /proc/pid/status file was created the only way to read
1714 * the effective uid of a /process was to stat /proc/pid. Reading
1715 * /proc/pid/status is slow enough that procps and other packages
1716 * kept stating /proc/pid. To keep the rules in /proc simple I have
1717 * made this apply to all per process world readable and executable
1720 if (mode
!= (S_IFDIR
|S_IRUGO
|S_IXUGO
)) {
1721 struct mm_struct
*mm
;
1724 /* Make non-dumpable tasks owned by some root */
1726 if (get_dumpable(mm
) != SUID_DUMP_USER
) {
1727 struct user_namespace
*user_ns
= mm
->user_ns
;
1729 uid
= make_kuid(user_ns
, 0);
1730 if (!uid_valid(uid
))
1731 uid
= GLOBAL_ROOT_UID
;
1733 gid
= make_kgid(user_ns
, 0);
1734 if (!gid_valid(gid
))
1735 gid
= GLOBAL_ROOT_GID
;
1738 uid
= GLOBAL_ROOT_UID
;
1739 gid
= GLOBAL_ROOT_GID
;
1747 struct inode
*proc_pid_make_inode(struct super_block
* sb
,
1748 struct task_struct
*task
, umode_t mode
)
1750 struct inode
* inode
;
1751 struct proc_inode
*ei
;
1753 /* We need a new inode */
1755 inode
= new_inode(sb
);
1761 inode
->i_mode
= mode
;
1762 inode
->i_ino
= get_next_ino();
1763 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= current_time(inode
);
1764 inode
->i_op
= &proc_def_inode_operations
;
1767 * grab the reference to task.
1769 ei
->pid
= get_task_pid(task
, PIDTYPE_PID
);
1773 task_dump_owner(task
, 0, &inode
->i_uid
, &inode
->i_gid
);
1774 security_task_to_inode(task
, inode
);
1784 int pid_getattr(const struct path
*path
, struct kstat
*stat
,
1785 u32 request_mask
, unsigned int query_flags
)
1787 struct inode
*inode
= d_inode(path
->dentry
);
1788 struct task_struct
*task
;
1789 struct pid_namespace
*pid
= path
->dentry
->d_sb
->s_fs_info
;
1791 generic_fillattr(inode
, stat
);
1794 stat
->uid
= GLOBAL_ROOT_UID
;
1795 stat
->gid
= GLOBAL_ROOT_GID
;
1796 task
= pid_task(proc_pid(inode
), PIDTYPE_PID
);
1798 if (!has_pid_permissions(pid
, task
, HIDEPID_INVISIBLE
)) {
1801 * This doesn't prevent learning whether PID exists,
1802 * it only makes getattr() consistent with readdir().
1806 task_dump_owner(task
, inode
->i_mode
, &stat
->uid
, &stat
->gid
);
1815 * Exceptional case: normally we are not allowed to unhash a busy
1816 * directory. In this case, however, we can do it - no aliasing problems
1817 * due to the way we treat inodes.
1819 * Rewrite the inode's ownerships here because the owning task may have
1820 * performed a setuid(), etc.
1823 int pid_revalidate(struct dentry
*dentry
, unsigned int flags
)
1825 struct inode
*inode
;
1826 struct task_struct
*task
;
1828 if (flags
& LOOKUP_RCU
)
1831 inode
= d_inode(dentry
);
1832 task
= get_proc_task(inode
);
1835 task_dump_owner(task
, inode
->i_mode
, &inode
->i_uid
, &inode
->i_gid
);
1837 inode
->i_mode
&= ~(S_ISUID
| S_ISGID
);
1838 security_task_to_inode(task
, inode
);
1839 put_task_struct(task
);
1845 static inline bool proc_inode_is_dead(struct inode
*inode
)
1847 return !proc_pid(inode
)->tasks
[PIDTYPE_PID
].first
;
1850 int pid_delete_dentry(const struct dentry
*dentry
)
1852 /* Is the task we represent dead?
1853 * If so, then don't put the dentry on the lru list,
1854 * kill it immediately.
1856 return proc_inode_is_dead(d_inode(dentry
));
1859 const struct dentry_operations pid_dentry_operations
=
1861 .d_revalidate
= pid_revalidate
,
1862 .d_delete
= pid_delete_dentry
,
1868 * Fill a directory entry.
1870 * If possible create the dcache entry and derive our inode number and
1871 * file type from dcache entry.
1873 * Since all of the proc inode numbers are dynamically generated, the inode
1874 * numbers do not exist until the inode is cache. This means creating the
1875 * the dcache entry in readdir is necessary to keep the inode numbers
1876 * reported by readdir in sync with the inode numbers reported
1879 bool proc_fill_cache(struct file
*file
, struct dir_context
*ctx
,
1880 const char *name
, int len
,
1881 instantiate_t instantiate
, struct task_struct
*task
, const void *ptr
)
1883 struct dentry
*child
, *dir
= file
->f_path
.dentry
;
1884 struct qstr qname
= QSTR_INIT(name
, len
);
1885 struct inode
*inode
;
1889 child
= d_hash_and_lookup(dir
, &qname
);
1891 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq
);
1892 child
= d_alloc_parallel(dir
, &qname
, &wq
);
1894 goto end_instantiate
;
1895 if (d_in_lookup(child
)) {
1896 int err
= instantiate(d_inode(dir
), child
, task
, ptr
);
1897 d_lookup_done(child
);
1900 goto end_instantiate
;
1904 inode
= d_inode(child
);
1906 type
= inode
->i_mode
>> 12;
1908 return dir_emit(ctx
, name
, len
, ino
, type
);
1911 return dir_emit(ctx
, name
, len
, 1, DT_UNKNOWN
);
1915 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1916 * which represent vma start and end addresses.
1918 static int dname_to_vma_addr(struct dentry
*dentry
,
1919 unsigned long *start
, unsigned long *end
)
1921 if (sscanf(dentry
->d_name
.name
, "%lx-%lx", start
, end
) != 2)
1927 static int map_files_d_revalidate(struct dentry
*dentry
, unsigned int flags
)
1929 unsigned long vm_start
, vm_end
;
1930 bool exact_vma_exists
= false;
1931 struct mm_struct
*mm
= NULL
;
1932 struct task_struct
*task
;
1933 struct inode
*inode
;
1936 if (flags
& LOOKUP_RCU
)
1939 inode
= d_inode(dentry
);
1940 task
= get_proc_task(inode
);
1944 mm
= mm_access(task
, PTRACE_MODE_READ_FSCREDS
);
1945 if (IS_ERR_OR_NULL(mm
))
1948 if (!dname_to_vma_addr(dentry
, &vm_start
, &vm_end
)) {
1949 down_read(&mm
->mmap_sem
);
1950 exact_vma_exists
= !!find_exact_vma(mm
, vm_start
, vm_end
);
1951 up_read(&mm
->mmap_sem
);
1956 if (exact_vma_exists
) {
1957 task_dump_owner(task
, 0, &inode
->i_uid
, &inode
->i_gid
);
1959 security_task_to_inode(task
, inode
);
1964 put_task_struct(task
);
1970 static const struct dentry_operations tid_map_files_dentry_operations
= {
1971 .d_revalidate
= map_files_d_revalidate
,
1972 .d_delete
= pid_delete_dentry
,
1975 static int map_files_get_link(struct dentry
*dentry
, struct path
*path
)
1977 unsigned long vm_start
, vm_end
;
1978 struct vm_area_struct
*vma
;
1979 struct task_struct
*task
;
1980 struct mm_struct
*mm
;
1984 task
= get_proc_task(d_inode(dentry
));
1988 mm
= get_task_mm(task
);
1989 put_task_struct(task
);
1993 rc
= dname_to_vma_addr(dentry
, &vm_start
, &vm_end
);
1998 down_read(&mm
->mmap_sem
);
1999 vma
= find_exact_vma(mm
, vm_start
, vm_end
);
2000 if (vma
&& vma
->vm_file
) {
2001 *path
= vma_pr_or_file(vma
)->f_path
;
2005 up_read(&mm
->mmap_sem
);
2013 struct map_files_info
{
2016 unsigned char name
[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
2020 * Only allow CAP_SYS_ADMIN to follow the links, due to concerns about how the
2021 * symlinks may be used to bypass permissions on ancestor directories in the
2022 * path to the file in question.
2025 proc_map_files_get_link(struct dentry
*dentry
,
2026 struct inode
*inode
,
2027 struct delayed_call
*done
)
2029 if (!capable(CAP_SYS_ADMIN
))
2030 return ERR_PTR(-EPERM
);
2032 return proc_pid_get_link(dentry
, inode
, done
);
2036 * Identical to proc_pid_link_inode_operations except for get_link()
2038 static const struct inode_operations proc_map_files_link_inode_operations
= {
2039 .readlink
= proc_pid_readlink
,
2040 .get_link
= proc_map_files_get_link
,
2041 .setattr
= proc_setattr
,
2045 proc_map_files_instantiate(struct inode
*dir
, struct dentry
*dentry
,
2046 struct task_struct
*task
, const void *ptr
)
2048 fmode_t mode
= (fmode_t
)(unsigned long)ptr
;
2049 struct proc_inode
*ei
;
2050 struct inode
*inode
;
2052 inode
= proc_pid_make_inode(dir
->i_sb
, task
, S_IFLNK
|
2053 ((mode
& FMODE_READ
) ? S_IRUSR
: 0) |
2054 ((mode
& FMODE_WRITE
) ? S_IWUSR
: 0));
2059 ei
->op
.proc_get_link
= map_files_get_link
;
2061 inode
->i_op
= &proc_map_files_link_inode_operations
;
2064 d_set_d_op(dentry
, &tid_map_files_dentry_operations
);
2065 d_add(dentry
, inode
);
2070 static struct dentry
*proc_map_files_lookup(struct inode
*dir
,
2071 struct dentry
*dentry
, unsigned int flags
)
2073 unsigned long vm_start
, vm_end
;
2074 struct vm_area_struct
*vma
;
2075 struct task_struct
*task
;
2077 struct mm_struct
*mm
;
2080 task
= get_proc_task(dir
);
2085 if (!ptrace_may_access(task
, PTRACE_MODE_READ_FSCREDS
))
2089 if (dname_to_vma_addr(dentry
, &vm_start
, &vm_end
))
2092 mm
= get_task_mm(task
);
2096 down_read(&mm
->mmap_sem
);
2097 vma
= find_exact_vma(mm
, vm_start
, vm_end
);
2102 result
= proc_map_files_instantiate(dir
, dentry
, task
,
2103 (void *)(unsigned long)vma
->vm_file
->f_mode
);
2106 up_read(&mm
->mmap_sem
);
2109 put_task_struct(task
);
2111 return ERR_PTR(result
);
2114 static const struct inode_operations proc_map_files_inode_operations
= {
2115 .lookup
= proc_map_files_lookup
,
2116 .permission
= proc_fd_permission
,
2117 .setattr
= proc_setattr
,
2121 proc_map_files_readdir(struct file
*file
, struct dir_context
*ctx
)
2123 struct vm_area_struct
*vma
;
2124 struct task_struct
*task
;
2125 struct mm_struct
*mm
;
2126 unsigned long nr_files
, pos
, i
;
2127 struct flex_array
*fa
= NULL
;
2128 struct map_files_info info
;
2129 struct map_files_info
*p
;
2133 task
= get_proc_task(file_inode(file
));
2138 if (!ptrace_may_access(task
, PTRACE_MODE_READ_FSCREDS
))
2142 if (!dir_emit_dots(file
, ctx
))
2145 mm
= get_task_mm(task
);
2148 down_read(&mm
->mmap_sem
);
2153 * We need two passes here:
2155 * 1) Collect vmas of mapped files with mmap_sem taken
2156 * 2) Release mmap_sem and instantiate entries
2158 * otherwise we get lockdep complained, since filldir()
2159 * routine might require mmap_sem taken in might_fault().
2162 for (vma
= mm
->mmap
, pos
= 2; vma
; vma
= vma
->vm_next
) {
2163 if (vma
->vm_file
&& ++pos
> ctx
->pos
)
2168 fa
= flex_array_alloc(sizeof(info
), nr_files
,
2170 if (!fa
|| flex_array_prealloc(fa
, 0, nr_files
,
2174 flex_array_free(fa
);
2175 up_read(&mm
->mmap_sem
);
2179 for (i
= 0, vma
= mm
->mmap
, pos
= 2; vma
;
2180 vma
= vma
->vm_next
) {
2183 if (++pos
<= ctx
->pos
)
2186 info
.mode
= vma
->vm_file
->f_mode
;
2187 info
.len
= snprintf(info
.name
,
2188 sizeof(info
.name
), "%lx-%lx",
2189 vma
->vm_start
, vma
->vm_end
);
2190 if (flex_array_put(fa
, i
++, &info
, GFP_KERNEL
))
2194 up_read(&mm
->mmap_sem
);
2196 for (i
= 0; i
< nr_files
; i
++) {
2197 p
= flex_array_get(fa
, i
);
2198 if (!proc_fill_cache(file
, ctx
,
2200 proc_map_files_instantiate
,
2202 (void *)(unsigned long)p
->mode
))
2207 flex_array_free(fa
);
2211 put_task_struct(task
);
2216 static const struct file_operations proc_map_files_operations
= {
2217 .read
= generic_read_dir
,
2218 .iterate_shared
= proc_map_files_readdir
,
2219 .llseek
= generic_file_llseek
,
2222 #if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
2223 struct timers_private
{
2225 struct task_struct
*task
;
2226 struct sighand_struct
*sighand
;
2227 struct pid_namespace
*ns
;
2228 unsigned long flags
;
2231 static void *timers_start(struct seq_file
*m
, loff_t
*pos
)
2233 struct timers_private
*tp
= m
->private;
2235 tp
->task
= get_pid_task(tp
->pid
, PIDTYPE_PID
);
2237 return ERR_PTR(-ESRCH
);
2239 tp
->sighand
= lock_task_sighand(tp
->task
, &tp
->flags
);
2241 return ERR_PTR(-ESRCH
);
2243 return seq_list_start(&tp
->task
->signal
->posix_timers
, *pos
);
2246 static void *timers_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
2248 struct timers_private
*tp
= m
->private;
2249 return seq_list_next(v
, &tp
->task
->signal
->posix_timers
, pos
);
2252 static void timers_stop(struct seq_file
*m
, void *v
)
2254 struct timers_private
*tp
= m
->private;
2257 unlock_task_sighand(tp
->task
, &tp
->flags
);
2262 put_task_struct(tp
->task
);
2267 static int show_timer(struct seq_file
*m
, void *v
)
2269 struct k_itimer
*timer
;
2270 struct timers_private
*tp
= m
->private;
2272 static const char * const nstr
[] = {
2273 [SIGEV_SIGNAL
] = "signal",
2274 [SIGEV_NONE
] = "none",
2275 [SIGEV_THREAD
] = "thread",
2278 timer
= list_entry((struct list_head
*)v
, struct k_itimer
, list
);
2279 notify
= timer
->it_sigev_notify
;
2281 seq_printf(m
, "ID: %d\n", timer
->it_id
);
2282 seq_printf(m
, "signal: %d/%px\n",
2283 timer
->sigq
->info
.si_signo
,
2284 timer
->sigq
->info
.si_value
.sival_ptr
);
2285 seq_printf(m
, "notify: %s/%s.%d\n",
2286 nstr
[notify
& ~SIGEV_THREAD_ID
],
2287 (notify
& SIGEV_THREAD_ID
) ? "tid" : "pid",
2288 pid_nr_ns(timer
->it_pid
, tp
->ns
));
2289 seq_printf(m
, "ClockID: %d\n", timer
->it_clock
);
2294 static const struct seq_operations proc_timers_seq_ops
= {
2295 .start
= timers_start
,
2296 .next
= timers_next
,
2297 .stop
= timers_stop
,
2301 static int proc_timers_open(struct inode
*inode
, struct file
*file
)
2303 struct timers_private
*tp
;
2305 tp
= __seq_open_private(file
, &proc_timers_seq_ops
,
2306 sizeof(struct timers_private
));
2310 tp
->pid
= proc_pid(inode
);
2311 tp
->ns
= inode
->i_sb
->s_fs_info
;
2315 static const struct file_operations proc_timers_operations
= {
2316 .open
= proc_timers_open
,
2318 .llseek
= seq_lseek
,
2319 .release
= seq_release_private
,
2323 static ssize_t
timerslack_ns_write(struct file
*file
, const char __user
*buf
,
2324 size_t count
, loff_t
*offset
)
2326 struct inode
*inode
= file_inode(file
);
2327 struct task_struct
*p
;
2331 err
= kstrtoull_from_user(buf
, count
, 10, &slack_ns
);
2335 p
= get_proc_task(inode
);
2340 if (!capable(CAP_SYS_NICE
)) {
2345 err
= security_task_setscheduler(p
);
2354 p
->timer_slack_ns
= p
->default_timer_slack_ns
;
2356 p
->timer_slack_ns
= slack_ns
;
2365 static int timerslack_ns_show(struct seq_file
*m
, void *v
)
2367 struct inode
*inode
= m
->private;
2368 struct task_struct
*p
;
2371 p
= get_proc_task(inode
);
2377 if (!capable(CAP_SYS_NICE
)) {
2381 err
= security_task_getscheduler(p
);
2387 seq_printf(m
, "%llu\n", p
->timer_slack_ns
);
2396 static int timerslack_ns_open(struct inode
*inode
, struct file
*filp
)
2398 return single_open(filp
, timerslack_ns_show
, inode
);
2401 static const struct file_operations proc_pid_set_timerslack_ns_operations
= {
2402 .open
= timerslack_ns_open
,
2404 .write
= timerslack_ns_write
,
2405 .llseek
= seq_lseek
,
2406 .release
= single_release
,
2409 static int proc_pident_instantiate(struct inode
*dir
,
2410 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
2412 const struct pid_entry
*p
= ptr
;
2413 struct inode
*inode
;
2414 struct proc_inode
*ei
;
2416 inode
= proc_pid_make_inode(dir
->i_sb
, task
, p
->mode
);
2421 if (S_ISDIR(inode
->i_mode
))
2422 set_nlink(inode
, 2); /* Use getattr to fix if necessary */
2424 inode
->i_op
= p
->iop
;
2426 inode
->i_fop
= p
->fop
;
2428 d_set_d_op(dentry
, &pid_dentry_operations
);
2429 d_add(dentry
, inode
);
2430 /* Close the race of the process dying before we return the dentry */
2431 if (pid_revalidate(dentry
, 0))
2437 static struct dentry
*proc_pident_lookup(struct inode
*dir
,
2438 struct dentry
*dentry
,
2439 const struct pid_entry
*ents
,
2443 struct task_struct
*task
= get_proc_task(dir
);
2444 const struct pid_entry
*p
, *last
;
2452 * Yes, it does not scale. And it should not. Don't add
2453 * new entries into /proc/<tgid>/ without very good reasons.
2455 last
= &ents
[nents
];
2456 for (p
= ents
; p
< last
; p
++) {
2457 if (p
->len
!= dentry
->d_name
.len
)
2459 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
))
2465 error
= proc_pident_instantiate(dir
, dentry
, task
, p
);
2467 put_task_struct(task
);
2469 return ERR_PTR(error
);
2472 static int proc_pident_readdir(struct file
*file
, struct dir_context
*ctx
,
2473 const struct pid_entry
*ents
, unsigned int nents
)
2475 struct task_struct
*task
= get_proc_task(file_inode(file
));
2476 const struct pid_entry
*p
;
2481 if (!dir_emit_dots(file
, ctx
))
2484 if (ctx
->pos
>= nents
+ 2)
2487 for (p
= ents
+ (ctx
->pos
- 2); p
< ents
+ nents
; p
++) {
2488 if (!proc_fill_cache(file
, ctx
, p
->name
, p
->len
,
2489 proc_pident_instantiate
, task
, p
))
2494 put_task_struct(task
);
2498 #ifdef CONFIG_SECURITY
2499 static ssize_t
proc_pid_attr_read(struct file
* file
, char __user
* buf
,
2500 size_t count
, loff_t
*ppos
)
2502 struct inode
* inode
= file_inode(file
);
2505 struct task_struct
*task
= get_proc_task(inode
);
2510 length
= security_getprocattr(task
, PROC_I(inode
)->op
.lsm
,
2511 (char*)file
->f_path
.dentry
->d_name
.name
,
2513 put_task_struct(task
);
2515 length
= simple_read_from_buffer(buf
, count
, ppos
, p
, length
);
2520 static ssize_t
proc_pid_attr_write(struct file
* file
, const char __user
* buf
,
2521 size_t count
, loff_t
*ppos
)
2523 struct inode
* inode
= file_inode(file
);
2526 struct task_struct
*task
= get_proc_task(inode
);
2532 /* A task may only write its own attributes. */
2534 if (current
!= task
)
2537 if (count
> PAGE_SIZE
)
2540 /* No partial writes. */
2545 page
= memdup_user(buf
, count
);
2547 length
= PTR_ERR(page
);
2551 /* Guard against adverse ptrace interaction */
2552 length
= mutex_lock_interruptible(¤t
->signal
->cred_guard_mutex
);
2556 length
= security_setprocattr(PROC_I(inode
)->op
.lsm
,
2557 file
->f_path
.dentry
->d_name
.name
,
2559 mutex_unlock(¤t
->signal
->cred_guard_mutex
);
2563 put_task_struct(task
);
2568 static const struct file_operations proc_pid_attr_operations
= {
2569 .read
= proc_pid_attr_read
,
2570 .write
= proc_pid_attr_write
,
2571 .llseek
= generic_file_llseek
,
2574 #define LSM_DIR_OPS(LSM) \
2575 static int proc_##LSM##_attr_dir_iterate(struct file *filp, \
2576 struct dir_context *ctx) \
2578 return proc_pident_readdir(filp, ctx, \
2579 LSM##_attr_dir_stuff, \
2580 ARRAY_SIZE(LSM##_attr_dir_stuff)); \
2583 static const struct file_operations proc_##LSM##_attr_dir_ops = { \
2584 .read = generic_read_dir, \
2585 .iterate = proc_##LSM##_attr_dir_iterate, \
2586 .llseek = default_llseek, \
2589 static struct dentry *proc_##LSM##_attr_dir_lookup(struct inode *dir, \
2590 struct dentry *dentry, unsigned int flags) \
2592 return proc_pident_lookup(dir, dentry, \
2593 LSM##_attr_dir_stuff, \
2594 ARRAY_SIZE(LSM##_attr_dir_stuff)); \
2597 static const struct inode_operations proc_##LSM##_attr_dir_inode_ops = { \
2598 .lookup = proc_##LSM##_attr_dir_lookup, \
2599 .getattr = pid_getattr, \
2600 .setattr = proc_setattr, \
2603 #ifdef CONFIG_SECURITY_SMACK
2604 static const struct pid_entry smack_attr_dir_stuff
[] = {
2605 ATTR("smack", "current", 0666),
2610 static const struct pid_entry attr_dir_stuff
[] = {
2611 ATTR(NULL
, "current", 0666),
2612 ATTR(NULL
, "prev", 0444),
2613 ATTR(NULL
, "exec", 0666),
2614 ATTR(NULL
, "fscreate", 0666),
2615 ATTR(NULL
, "keycreate", 0666),
2616 ATTR(NULL
, "sockcreate", 0666),
2617 ATTR(NULL
, "display_lsm", 0666),
2619 #ifdef CONFIG_SECURITY_SELINUX
2620 DIR("selinux", 0555,
2621 proc_selinux_attr_dir_inode_ops
, proc_selinux_attr_dir_ops
),
2624 #ifdef CONFIG_SECURITY_SMACK
2626 proc_smack_attr_dir_inode_ops
, proc_smack_attr_dir_ops
),
2630 static int proc_attr_dir_readdir(struct file
*file
, struct dir_context
*ctx
)
2632 return proc_pident_readdir(file
, ctx
,
2633 attr_dir_stuff
, ARRAY_SIZE(attr_dir_stuff
));
2636 static const struct file_operations proc_attr_dir_operations
= {
2637 .read
= generic_read_dir
,
2638 .iterate_shared
= proc_attr_dir_readdir
,
2639 .llseek
= generic_file_llseek
,
2642 static struct dentry
*proc_attr_dir_lookup(struct inode
*dir
,
2643 struct dentry
*dentry
, unsigned int flags
)
2645 return proc_pident_lookup(dir
, dentry
,
2646 attr_dir_stuff
, ARRAY_SIZE(attr_dir_stuff
));
2649 static const struct inode_operations proc_attr_dir_inode_operations
= {
2650 .lookup
= proc_attr_dir_lookup
,
2651 .getattr
= pid_getattr
,
2652 .setattr
= proc_setattr
,
2657 #ifdef CONFIG_ELF_CORE
2658 static ssize_t
proc_coredump_filter_read(struct file
*file
, char __user
*buf
,
2659 size_t count
, loff_t
*ppos
)
2661 struct task_struct
*task
= get_proc_task(file_inode(file
));
2662 struct mm_struct
*mm
;
2663 char buffer
[PROC_NUMBUF
];
2671 mm
= get_task_mm(task
);
2673 len
= snprintf(buffer
, sizeof(buffer
), "%08lx\n",
2674 ((mm
->flags
& MMF_DUMP_FILTER_MASK
) >>
2675 MMF_DUMP_FILTER_SHIFT
));
2677 ret
= simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
2680 put_task_struct(task
);
2685 static ssize_t
proc_coredump_filter_write(struct file
*file
,
2686 const char __user
*buf
,
2690 struct task_struct
*task
;
2691 struct mm_struct
*mm
;
2697 ret
= kstrtouint_from_user(buf
, count
, 0, &val
);
2702 task
= get_proc_task(file_inode(file
));
2706 mm
= get_task_mm(task
);
2711 for (i
= 0, mask
= 1; i
< MMF_DUMP_FILTER_BITS
; i
++, mask
<<= 1) {
2713 set_bit(i
+ MMF_DUMP_FILTER_SHIFT
, &mm
->flags
);
2715 clear_bit(i
+ MMF_DUMP_FILTER_SHIFT
, &mm
->flags
);
2720 put_task_struct(task
);
2727 static const struct file_operations proc_coredump_filter_operations
= {
2728 .read
= proc_coredump_filter_read
,
2729 .write
= proc_coredump_filter_write
,
2730 .llseek
= generic_file_llseek
,
2734 #ifdef CONFIG_TASK_IO_ACCOUNTING
2735 static int do_io_accounting(struct task_struct
*task
, struct seq_file
*m
, int whole
)
2737 struct task_io_accounting acct
= task
->ioac
;
2738 unsigned long flags
;
2741 result
= mutex_lock_killable(&task
->signal
->cred_guard_mutex
);
2745 if (!ptrace_may_access(task
, PTRACE_MODE_READ_FSCREDS
)) {
2750 if (whole
&& lock_task_sighand(task
, &flags
)) {
2751 struct task_struct
*t
= task
;
2753 task_io_accounting_add(&acct
, &task
->signal
->ioac
);
2754 while_each_thread(task
, t
)
2755 task_io_accounting_add(&acct
, &t
->ioac
);
2757 unlock_task_sighand(task
, &flags
);
2764 "read_bytes: %llu\n"
2765 "write_bytes: %llu\n"
2766 "cancelled_write_bytes: %llu\n",
2767 (unsigned long long)acct
.rchar
,
2768 (unsigned long long)acct
.wchar
,
2769 (unsigned long long)acct
.syscr
,
2770 (unsigned long long)acct
.syscw
,
2771 (unsigned long long)acct
.read_bytes
,
2772 (unsigned long long)acct
.write_bytes
,
2773 (unsigned long long)acct
.cancelled_write_bytes
);
2777 mutex_unlock(&task
->signal
->cred_guard_mutex
);
2781 static int proc_tid_io_accounting(struct seq_file
*m
, struct pid_namespace
*ns
,
2782 struct pid
*pid
, struct task_struct
*task
)
2784 return do_io_accounting(task
, m
, 0);
2787 static int proc_tgid_io_accounting(struct seq_file
*m
, struct pid_namespace
*ns
,
2788 struct pid
*pid
, struct task_struct
*task
)
2790 return do_io_accounting(task
, m
, 1);
2792 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2794 #ifdef CONFIG_USER_NS
2795 static int proc_id_map_open(struct inode
*inode
, struct file
*file
,
2796 const struct seq_operations
*seq_ops
)
2798 struct user_namespace
*ns
= NULL
;
2799 struct task_struct
*task
;
2800 struct seq_file
*seq
;
2803 task
= get_proc_task(inode
);
2806 ns
= get_user_ns(task_cred_xxx(task
, user_ns
));
2808 put_task_struct(task
);
2813 ret
= seq_open(file
, seq_ops
);
2817 seq
= file
->private_data
;
2827 static int proc_id_map_release(struct inode
*inode
, struct file
*file
)
2829 struct seq_file
*seq
= file
->private_data
;
2830 struct user_namespace
*ns
= seq
->private;
2832 return seq_release(inode
, file
);
2835 static int proc_uid_map_open(struct inode
*inode
, struct file
*file
)
2837 return proc_id_map_open(inode
, file
, &proc_uid_seq_operations
);
2840 static int proc_gid_map_open(struct inode
*inode
, struct file
*file
)
2842 return proc_id_map_open(inode
, file
, &proc_gid_seq_operations
);
2845 static int proc_projid_map_open(struct inode
*inode
, struct file
*file
)
2847 return proc_id_map_open(inode
, file
, &proc_projid_seq_operations
);
2850 static const struct file_operations proc_uid_map_operations
= {
2851 .open
= proc_uid_map_open
,
2852 .write
= proc_uid_map_write
,
2854 .llseek
= seq_lseek
,
2855 .release
= proc_id_map_release
,
2858 static const struct file_operations proc_gid_map_operations
= {
2859 .open
= proc_gid_map_open
,
2860 .write
= proc_gid_map_write
,
2862 .llseek
= seq_lseek
,
2863 .release
= proc_id_map_release
,
2866 static const struct file_operations proc_projid_map_operations
= {
2867 .open
= proc_projid_map_open
,
2868 .write
= proc_projid_map_write
,
2870 .llseek
= seq_lseek
,
2871 .release
= proc_id_map_release
,
2874 static int proc_setgroups_open(struct inode
*inode
, struct file
*file
)
2876 struct user_namespace
*ns
= NULL
;
2877 struct task_struct
*task
;
2881 task
= get_proc_task(inode
);
2884 ns
= get_user_ns(task_cred_xxx(task
, user_ns
));
2886 put_task_struct(task
);
2891 if (file
->f_mode
& FMODE_WRITE
) {
2893 if (!ns_capable(ns
, CAP_SYS_ADMIN
))
2897 ret
= single_open(file
, &proc_setgroups_show
, ns
);
2908 static int proc_setgroups_release(struct inode
*inode
, struct file
*file
)
2910 struct seq_file
*seq
= file
->private_data
;
2911 struct user_namespace
*ns
= seq
->private;
2912 int ret
= single_release(inode
, file
);
2917 static const struct file_operations proc_setgroups_operations
= {
2918 .open
= proc_setgroups_open
,
2919 .write
= proc_setgroups_write
,
2921 .llseek
= seq_lseek
,
2922 .release
= proc_setgroups_release
,
2924 #endif /* CONFIG_USER_NS */
2926 static int proc_pid_personality(struct seq_file
*m
, struct pid_namespace
*ns
,
2927 struct pid
*pid
, struct task_struct
*task
)
2929 int err
= lock_trace(task
);
2931 seq_printf(m
, "%08x\n", task
->personality
);
2937 #ifdef CONFIG_LIVEPATCH
2938 static int proc_pid_patch_state(struct seq_file
*m
, struct pid_namespace
*ns
,
2939 struct pid
*pid
, struct task_struct
*task
)
2941 seq_printf(m
, "%d\n", task
->patch_state
);
2944 #endif /* CONFIG_LIVEPATCH */
2949 static const struct file_operations proc_task_operations
;
2950 static const struct inode_operations proc_task_inode_operations
;
2952 static const struct pid_entry tgid_base_stuff
[] = {
2953 DIR("task", S_IRUGO
|S_IXUGO
, proc_task_inode_operations
, proc_task_operations
),
2954 DIR("fd", S_IRUSR
|S_IXUSR
, proc_fd_inode_operations
, proc_fd_operations
),
2955 DIR("map_files", S_IRUSR
|S_IXUSR
, proc_map_files_inode_operations
, proc_map_files_operations
),
2956 DIR("fdinfo", S_IRUSR
|S_IXUSR
, proc_fdinfo_inode_operations
, proc_fdinfo_operations
),
2957 DIR("ns", S_IRUSR
|S_IXUGO
, proc_ns_dir_inode_operations
, proc_ns_dir_operations
),
2959 DIR("net", S_IRUGO
|S_IXUGO
, proc_net_inode_operations
, proc_net_operations
),
2961 REG("environ", S_IRUSR
, proc_environ_operations
),
2962 REG("auxv", S_IRUSR
, proc_auxv_operations
),
2963 ONE("status", S_IRUGO
, proc_pid_status
),
2964 ONE("personality", S_IRUSR
, proc_pid_personality
),
2965 ONE("limits", S_IRUGO
, proc_pid_limits
),
2966 #ifdef CONFIG_SCHED_DEBUG
2967 REG("sched", S_IRUGO
|S_IWUSR
, proc_pid_sched_operations
),
2969 #ifdef CONFIG_SCHED_AUTOGROUP
2970 REG("autogroup", S_IRUGO
|S_IWUSR
, proc_pid_sched_autogroup_operations
),
2972 REG("comm", S_IRUGO
|S_IWUSR
, proc_pid_set_comm_operations
),
2973 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2974 ONE("syscall", S_IRUSR
, proc_pid_syscall
),
2976 REG("cmdline", S_IRUGO
, proc_pid_cmdline_ops
),
2977 ONE("stat", S_IRUGO
, proc_tgid_stat
),
2978 ONE("statm", S_IRUGO
, proc_pid_statm
),
2979 REG("maps", S_IRUGO
, proc_pid_maps_operations
),
2981 REG("numa_maps", S_IRUGO
, proc_pid_numa_maps_operations
),
2983 REG("mem", S_IRUSR
|S_IWUSR
, proc_mem_operations
),
2984 LNK("cwd", proc_cwd_link
),
2985 LNK("root", proc_root_link
),
2986 LNK("exe", proc_exe_link
),
2987 REG("mounts", S_IRUGO
, proc_mounts_operations
),
2988 REG("mountinfo", S_IRUGO
, proc_mountinfo_operations
),
2989 REG("mountstats", S_IRUSR
, proc_mountstats_operations
),
2990 #ifdef CONFIG_PROC_PAGE_MONITOR
2991 REG("clear_refs", S_IWUSR
, proc_clear_refs_operations
),
2992 REG("smaps", S_IRUGO
, proc_pid_smaps_operations
),
2993 REG("smaps_rollup", S_IRUGO
, proc_pid_smaps_rollup_operations
),
2994 REG("pagemap", S_IRUSR
, proc_pagemap_operations
),
2996 #ifdef CONFIG_SECURITY
2997 DIR("attr", S_IRUGO
|S_IXUGO
, proc_attr_dir_inode_operations
, proc_attr_dir_operations
),
2999 #ifdef CONFIG_KALLSYMS
3000 ONE("wchan", S_IRUGO
, proc_pid_wchan
),
3002 #ifdef CONFIG_STACKTRACE
3003 ONE("stack", S_IRUSR
, proc_pid_stack
),
3005 #ifdef CONFIG_SCHED_INFO
3006 ONE("schedstat", S_IRUGO
, proc_pid_schedstat
),
3008 #ifdef CONFIG_LATENCYTOP
3009 REG("latency", S_IRUGO
, proc_lstats_operations
),
3011 #ifdef CONFIG_PROC_PID_CPUSET
3012 ONE("cpuset", S_IRUGO
, proc_cpuset_show
),
3014 #ifdef CONFIG_CGROUPS
3015 ONE("cgroup", S_IRUGO
, proc_cgroup_show
),
3017 ONE("oom_score", S_IRUGO
, proc_oom_score
),
3018 REG("oom_adj", S_IRUGO
|S_IWUSR
, proc_oom_adj_operations
),
3019 REG("oom_score_adj", S_IRUGO
|S_IWUSR
, proc_oom_score_adj_operations
),
3020 #ifdef CONFIG_AUDITSYSCALL
3021 REG("loginuid", S_IWUSR
|S_IRUGO
, proc_loginuid_operations
),
3022 REG("sessionid", S_IRUGO
, proc_sessionid_operations
),
3024 #ifdef CONFIG_FAULT_INJECTION
3025 REG("make-it-fail", S_IRUGO
|S_IWUSR
, proc_fault_inject_operations
),
3026 REG("fail-nth", 0644, proc_fail_nth_operations
),
3028 #ifdef CONFIG_ELF_CORE
3029 REG("coredump_filter", S_IRUGO
|S_IWUSR
, proc_coredump_filter_operations
),
3031 #ifdef CONFIG_TASK_IO_ACCOUNTING
3032 ONE("io", S_IRUSR
, proc_tgid_io_accounting
),
3034 #ifdef CONFIG_HARDWALL
3035 ONE("hardwall", S_IRUGO
, proc_pid_hardwall
),
3037 #ifdef CONFIG_USER_NS
3038 REG("uid_map", S_IRUGO
|S_IWUSR
, proc_uid_map_operations
),
3039 REG("gid_map", S_IRUGO
|S_IWUSR
, proc_gid_map_operations
),
3040 REG("projid_map", S_IRUGO
|S_IWUSR
, proc_projid_map_operations
),
3041 REG("setgroups", S_IRUGO
|S_IWUSR
, proc_setgroups_operations
),
3043 #if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
3044 REG("timers", S_IRUGO
, proc_timers_operations
),
3046 REG("timerslack_ns", S_IRUGO
|S_IWUGO
, proc_pid_set_timerslack_ns_operations
),
3047 #ifdef CONFIG_LIVEPATCH
3048 ONE("patch_state", S_IRUSR
, proc_pid_patch_state
),
3052 static int proc_tgid_base_readdir(struct file
*file
, struct dir_context
*ctx
)
3054 return proc_pident_readdir(file
, ctx
,
3055 tgid_base_stuff
, ARRAY_SIZE(tgid_base_stuff
));
3058 static const struct file_operations proc_tgid_base_operations
= {
3059 .read
= generic_read_dir
,
3060 .iterate_shared
= proc_tgid_base_readdir
,
3061 .llseek
= generic_file_llseek
,
3064 static struct dentry
*proc_tgid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, unsigned int flags
)
3066 return proc_pident_lookup(dir
, dentry
,
3067 tgid_base_stuff
, ARRAY_SIZE(tgid_base_stuff
));
3070 static const struct inode_operations proc_tgid_base_inode_operations
= {
3071 .lookup
= proc_tgid_base_lookup
,
3072 .getattr
= pid_getattr
,
3073 .setattr
= proc_setattr
,
3074 .permission
= proc_pid_permission
,
3077 static void proc_flush_task_mnt(struct vfsmount
*mnt
, pid_t pid
, pid_t tgid
)
3079 struct dentry
*dentry
, *leader
, *dir
;
3080 char buf
[PROC_NUMBUF
];
3084 name
.len
= snprintf(buf
, sizeof(buf
), "%d", pid
);
3085 /* no ->d_hash() rejects on procfs */
3086 dentry
= d_hash_and_lookup(mnt
->mnt_root
, &name
);
3088 d_invalidate(dentry
);
3096 name
.len
= snprintf(buf
, sizeof(buf
), "%d", tgid
);
3097 leader
= d_hash_and_lookup(mnt
->mnt_root
, &name
);
3102 name
.len
= strlen(name
.name
);
3103 dir
= d_hash_and_lookup(leader
, &name
);
3105 goto out_put_leader
;
3108 name
.len
= snprintf(buf
, sizeof(buf
), "%d", pid
);
3109 dentry
= d_hash_and_lookup(dir
, &name
);
3111 d_invalidate(dentry
);
3123 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
3124 * @task: task that should be flushed.
3126 * When flushing dentries from proc, one needs to flush them from global
3127 * proc (proc_mnt) and from all the namespaces' procs this task was seen
3128 * in. This call is supposed to do all of this job.
3130 * Looks in the dcache for
3132 * /proc/@tgid/task/@pid
3133 * if either directory is present flushes it and all of it'ts children
3136 * It is safe and reasonable to cache /proc entries for a task until
3137 * that task exits. After that they just clog up the dcache with
3138 * useless entries, possibly causing useful dcache entries to be
3139 * flushed instead. This routine is proved to flush those useless
3140 * dcache entries at process exit time.
3142 * NOTE: This routine is just an optimization so it does not guarantee
3143 * that no dcache entries will exist at process exit time it
3144 * just makes it very unlikely that any will persist.
3147 void proc_flush_task(struct task_struct
*task
)
3150 struct pid
*pid
, *tgid
;
3153 pid
= task_pid(task
);
3154 tgid
= task_tgid(task
);
3156 for (i
= 0; i
<= pid
->level
; i
++) {
3157 upid
= &pid
->numbers
[i
];
3158 proc_flush_task_mnt(upid
->ns
->proc_mnt
, upid
->nr
,
3159 tgid
->numbers
[i
].nr
);
3163 static int proc_pid_instantiate(struct inode
*dir
,
3164 struct dentry
* dentry
,
3165 struct task_struct
*task
, const void *ptr
)
3167 struct inode
*inode
;
3169 inode
= proc_pid_make_inode(dir
->i_sb
, task
, S_IFDIR
| S_IRUGO
| S_IXUGO
);
3173 inode
->i_op
= &proc_tgid_base_inode_operations
;
3174 inode
->i_fop
= &proc_tgid_base_operations
;
3175 inode
->i_flags
|=S_IMMUTABLE
;
3177 set_nlink(inode
, nlink_tgid
);
3179 d_set_d_op(dentry
, &pid_dentry_operations
);
3181 d_add(dentry
, inode
);
3182 /* Close the race of the process dying before we return the dentry */
3183 if (pid_revalidate(dentry
, 0))
3189 struct dentry
*proc_pid_lookup(struct inode
*dir
, struct dentry
* dentry
, unsigned int flags
)
3191 int result
= -ENOENT
;
3192 struct task_struct
*task
;
3194 struct pid_namespace
*ns
;
3196 tgid
= name_to_int(&dentry
->d_name
);
3200 ns
= dentry
->d_sb
->s_fs_info
;
3202 task
= find_task_by_pid_ns(tgid
, ns
);
3204 get_task_struct(task
);
3209 result
= proc_pid_instantiate(dir
, dentry
, task
, NULL
);
3210 put_task_struct(task
);
3212 return ERR_PTR(result
);
3216 * Find the first task with tgid >= tgid
3221 struct task_struct
*task
;
3223 static struct tgid_iter
next_tgid(struct pid_namespace
*ns
, struct tgid_iter iter
)
3228 put_task_struct(iter
.task
);
3232 pid
= find_ge_pid(iter
.tgid
, ns
);
3234 iter
.tgid
= pid_nr_ns(pid
, ns
);
3235 iter
.task
= pid_task(pid
, PIDTYPE_PID
);
3236 /* What we to know is if the pid we have find is the
3237 * pid of a thread_group_leader. Testing for task
3238 * being a thread_group_leader is the obvious thing
3239 * todo but there is a window when it fails, due to
3240 * the pid transfer logic in de_thread.
3242 * So we perform the straight forward test of seeing
3243 * if the pid we have found is the pid of a thread
3244 * group leader, and don't worry if the task we have
3245 * found doesn't happen to be a thread group leader.
3246 * As we don't care in the case of readdir.
3248 if (!iter
.task
|| !has_group_leader_pid(iter
.task
)) {
3252 get_task_struct(iter
.task
);
3258 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
3260 /* for the /proc/ directory itself, after non-process stuff has been done */
3261 int proc_pid_readdir(struct file
*file
, struct dir_context
*ctx
)
3263 struct tgid_iter iter
;
3264 struct pid_namespace
*ns
= file_inode(file
)->i_sb
->s_fs_info
;
3265 loff_t pos
= ctx
->pos
;
3267 if (pos
>= PID_MAX_LIMIT
+ TGID_OFFSET
)
3270 if (pos
== TGID_OFFSET
- 2) {
3271 struct inode
*inode
= d_inode(ns
->proc_self
);
3272 if (!dir_emit(ctx
, "self", 4, inode
->i_ino
, DT_LNK
))
3274 ctx
->pos
= pos
= pos
+ 1;
3276 if (pos
== TGID_OFFSET
- 1) {
3277 struct inode
*inode
= d_inode(ns
->proc_thread_self
);
3278 if (!dir_emit(ctx
, "thread-self", 11, inode
->i_ino
, DT_LNK
))
3280 ctx
->pos
= pos
= pos
+ 1;
3282 iter
.tgid
= pos
- TGID_OFFSET
;
3284 for (iter
= next_tgid(ns
, iter
);
3286 iter
.tgid
+= 1, iter
= next_tgid(ns
, iter
)) {
3287 char name
[PROC_NUMBUF
];
3291 if (!has_pid_permissions(ns
, iter
.task
, HIDEPID_INVISIBLE
))
3294 len
= snprintf(name
, sizeof(name
), "%d", iter
.tgid
);
3295 ctx
->pos
= iter
.tgid
+ TGID_OFFSET
;
3296 if (!proc_fill_cache(file
, ctx
, name
, len
,
3297 proc_pid_instantiate
, iter
.task
, NULL
)) {
3298 put_task_struct(iter
.task
);
3302 ctx
->pos
= PID_MAX_LIMIT
+ TGID_OFFSET
;
3307 * proc_tid_comm_permission is a special permission function exclusively
3308 * used for the node /proc/<pid>/task/<tid>/comm.
3309 * It bypasses generic permission checks in the case where a task of the same
3310 * task group attempts to access the node.
3311 * The rationale behind this is that glibc and bionic access this node for
3312 * cross thread naming (pthread_set/getname_np(!self)). However, if
3313 * PR_SET_DUMPABLE gets set to 0 this node among others becomes uid=0 gid=0,
3314 * which locks out the cross thread naming implementation.
3315 * This function makes sure that the node is always accessible for members of
3316 * same thread group.
3318 static int proc_tid_comm_permission(struct inode
*inode
, int mask
)
3320 bool is_same_tgroup
;
3321 struct task_struct
*task
;
3323 task
= get_proc_task(inode
);
3326 is_same_tgroup
= same_thread_group(current
, task
);
3327 put_task_struct(task
);
3329 if (likely(is_same_tgroup
&& !(mask
& MAY_EXEC
))) {
3330 /* This file (/proc/<pid>/task/<tid>/comm) can always be
3331 * read or written by the members of the corresponding
3337 return generic_permission(inode
, mask
);
3340 static const struct inode_operations proc_tid_comm_inode_operations
= {
3341 .permission
= proc_tid_comm_permission
,
3347 static const struct pid_entry tid_base_stuff
[] = {
3348 DIR("fd", S_IRUSR
|S_IXUSR
, proc_fd_inode_operations
, proc_fd_operations
),
3349 DIR("fdinfo", S_IRUSR
|S_IXUSR
, proc_fdinfo_inode_operations
, proc_fdinfo_operations
),
3350 DIR("ns", S_IRUSR
|S_IXUGO
, proc_ns_dir_inode_operations
, proc_ns_dir_operations
),
3352 DIR("net", S_IRUGO
|S_IXUGO
, proc_net_inode_operations
, proc_net_operations
),
3354 REG("environ", S_IRUSR
, proc_environ_operations
),
3355 REG("auxv", S_IRUSR
, proc_auxv_operations
),
3356 ONE("status", S_IRUGO
, proc_pid_status
),
3357 ONE("personality", S_IRUSR
, proc_pid_personality
),
3358 ONE("limits", S_IRUGO
, proc_pid_limits
),
3359 #ifdef CONFIG_SCHED_DEBUG
3360 REG("sched", S_IRUGO
|S_IWUSR
, proc_pid_sched_operations
),
3362 NOD("comm", S_IFREG
|S_IRUGO
|S_IWUSR
,
3363 &proc_tid_comm_inode_operations
,
3364 &proc_pid_set_comm_operations
, {}),
3365 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3366 ONE("syscall", S_IRUSR
, proc_pid_syscall
),
3368 REG("cmdline", S_IRUGO
, proc_pid_cmdline_ops
),
3369 ONE("stat", S_IRUGO
, proc_tid_stat
),
3370 ONE("statm", S_IRUGO
, proc_pid_statm
),
3371 REG("maps", S_IRUGO
, proc_tid_maps_operations
),
3372 #ifdef CONFIG_PROC_CHILDREN
3373 REG("children", S_IRUGO
, proc_tid_children_operations
),
3376 REG("numa_maps", S_IRUGO
, proc_tid_numa_maps_operations
),
3378 REG("mem", S_IRUSR
|S_IWUSR
, proc_mem_operations
),
3379 LNK("cwd", proc_cwd_link
),
3380 LNK("root", proc_root_link
),
3381 LNK("exe", proc_exe_link
),
3382 REG("mounts", S_IRUGO
, proc_mounts_operations
),
3383 REG("mountinfo", S_IRUGO
, proc_mountinfo_operations
),
3384 #ifdef CONFIG_PROC_PAGE_MONITOR
3385 REG("clear_refs", S_IWUSR
, proc_clear_refs_operations
),
3386 REG("smaps", S_IRUGO
, proc_tid_smaps_operations
),
3387 REG("smaps_rollup", S_IRUGO
, proc_pid_smaps_rollup_operations
),
3388 REG("pagemap", S_IRUSR
, proc_pagemap_operations
),
3390 #ifdef CONFIG_SECURITY
3391 DIR("attr", S_IRUGO
|S_IXUGO
, proc_attr_dir_inode_operations
, proc_attr_dir_operations
),
3393 #ifdef CONFIG_KALLSYMS
3394 ONE("wchan", S_IRUGO
, proc_pid_wchan
),
3396 #ifdef CONFIG_STACKTRACE
3397 ONE("stack", S_IRUSR
, proc_pid_stack
),
3399 #ifdef CONFIG_SCHED_INFO
3400 ONE("schedstat", S_IRUGO
, proc_pid_schedstat
),
3402 #ifdef CONFIG_LATENCYTOP
3403 REG("latency", S_IRUGO
, proc_lstats_operations
),
3405 #ifdef CONFIG_PROC_PID_CPUSET
3406 ONE("cpuset", S_IRUGO
, proc_cpuset_show
),
3408 #ifdef CONFIG_CGROUPS
3409 ONE("cgroup", S_IRUGO
, proc_cgroup_show
),
3411 ONE("oom_score", S_IRUGO
, proc_oom_score
),
3412 REG("oom_adj", S_IRUGO
|S_IWUSR
, proc_oom_adj_operations
),
3413 REG("oom_score_adj", S_IRUGO
|S_IWUSR
, proc_oom_score_adj_operations
),
3414 #ifdef CONFIG_AUDITSYSCALL
3415 REG("loginuid", S_IWUSR
|S_IRUGO
, proc_loginuid_operations
),
3416 REG("sessionid", S_IRUGO
, proc_sessionid_operations
),
3418 #ifdef CONFIG_FAULT_INJECTION
3419 REG("make-it-fail", S_IRUGO
|S_IWUSR
, proc_fault_inject_operations
),
3420 REG("fail-nth", 0644, proc_fail_nth_operations
),
3422 #ifdef CONFIG_TASK_IO_ACCOUNTING
3423 ONE("io", S_IRUSR
, proc_tid_io_accounting
),
3425 #ifdef CONFIG_HARDWALL
3426 ONE("hardwall", S_IRUGO
, proc_pid_hardwall
),
3428 #ifdef CONFIG_USER_NS
3429 REG("uid_map", S_IRUGO
|S_IWUSR
, proc_uid_map_operations
),
3430 REG("gid_map", S_IRUGO
|S_IWUSR
, proc_gid_map_operations
),
3431 REG("projid_map", S_IRUGO
|S_IWUSR
, proc_projid_map_operations
),
3432 REG("setgroups", S_IRUGO
|S_IWUSR
, proc_setgroups_operations
),
3434 #ifdef CONFIG_LIVEPATCH
3435 ONE("patch_state", S_IRUSR
, proc_pid_patch_state
),
3439 static int proc_tid_base_readdir(struct file
*file
, struct dir_context
*ctx
)
3441 return proc_pident_readdir(file
, ctx
,
3442 tid_base_stuff
, ARRAY_SIZE(tid_base_stuff
));
3445 static struct dentry
*proc_tid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, unsigned int flags
)
3447 return proc_pident_lookup(dir
, dentry
,
3448 tid_base_stuff
, ARRAY_SIZE(tid_base_stuff
));
3451 static const struct file_operations proc_tid_base_operations
= {
3452 .read
= generic_read_dir
,
3453 .iterate_shared
= proc_tid_base_readdir
,
3454 .llseek
= generic_file_llseek
,
3457 static const struct inode_operations proc_tid_base_inode_operations
= {
3458 .lookup
= proc_tid_base_lookup
,
3459 .getattr
= pid_getattr
,
3460 .setattr
= proc_setattr
,
3463 static int proc_task_instantiate(struct inode
*dir
,
3464 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
3466 struct inode
*inode
;
3467 inode
= proc_pid_make_inode(dir
->i_sb
, task
, S_IFDIR
| S_IRUGO
| S_IXUGO
);
3471 inode
->i_op
= &proc_tid_base_inode_operations
;
3472 inode
->i_fop
= &proc_tid_base_operations
;
3473 inode
->i_flags
|=S_IMMUTABLE
;
3475 set_nlink(inode
, nlink_tid
);
3477 d_set_d_op(dentry
, &pid_dentry_operations
);
3479 d_add(dentry
, inode
);
3480 /* Close the race of the process dying before we return the dentry */
3481 if (pid_revalidate(dentry
, 0))
3487 static struct dentry
*proc_task_lookup(struct inode
*dir
, struct dentry
* dentry
, unsigned int flags
)
3489 int result
= -ENOENT
;
3490 struct task_struct
*task
;
3491 struct task_struct
*leader
= get_proc_task(dir
);
3493 struct pid_namespace
*ns
;
3498 tid
= name_to_int(&dentry
->d_name
);
3502 ns
= dentry
->d_sb
->s_fs_info
;
3504 task
= find_task_by_pid_ns(tid
, ns
);
3506 get_task_struct(task
);
3510 if (!same_thread_group(leader
, task
))
3513 result
= proc_task_instantiate(dir
, dentry
, task
, NULL
);
3515 put_task_struct(task
);
3517 put_task_struct(leader
);
3519 return ERR_PTR(result
);
3523 * Find the first tid of a thread group to return to user space.
3525 * Usually this is just the thread group leader, but if the users
3526 * buffer was too small or there was a seek into the middle of the
3527 * directory we have more work todo.
3529 * In the case of a short read we start with find_task_by_pid.
3531 * In the case of a seek we start with the leader and walk nr
3534 static struct task_struct
*first_tid(struct pid
*pid
, int tid
, loff_t f_pos
,
3535 struct pid_namespace
*ns
)
3537 struct task_struct
*pos
, *task
;
3538 unsigned long nr
= f_pos
;
3540 if (nr
!= f_pos
) /* 32bit overflow? */
3544 task
= pid_task(pid
, PIDTYPE_PID
);
3548 /* Attempt to start with the tid of a thread */
3550 pos
= find_task_by_pid_ns(tid
, ns
);
3551 if (pos
&& same_thread_group(pos
, task
))
3555 /* If nr exceeds the number of threads there is nothing todo */
3556 if (nr
>= get_nr_threads(task
))
3559 /* If we haven't found our starting place yet start
3560 * with the leader and walk nr threads forward.
3562 pos
= task
= task
->group_leader
;
3566 } while_each_thread(task
, pos
);
3571 get_task_struct(pos
);
3578 * Find the next thread in the thread list.
3579 * Return NULL if there is an error or no next thread.
3581 * The reference to the input task_struct is released.
3583 static struct task_struct
*next_tid(struct task_struct
*start
)
3585 struct task_struct
*pos
= NULL
;
3587 if (pid_alive(start
)) {
3588 pos
= next_thread(start
);
3589 if (thread_group_leader(pos
))
3592 get_task_struct(pos
);
3595 put_task_struct(start
);
3599 /* for the /proc/TGID/task/ directories */
3600 static int proc_task_readdir(struct file
*file
, struct dir_context
*ctx
)
3602 struct inode
*inode
= file_inode(file
);
3603 struct task_struct
*task
;
3604 struct pid_namespace
*ns
;
3607 if (proc_inode_is_dead(inode
))
3610 if (!dir_emit_dots(file
, ctx
))
3613 /* f_version caches the tgid value that the last readdir call couldn't
3614 * return. lseek aka telldir automagically resets f_version to 0.
3616 ns
= inode
->i_sb
->s_fs_info
;
3617 tid
= (int)file
->f_version
;
3618 file
->f_version
= 0;
3619 for (task
= first_tid(proc_pid(inode
), tid
, ctx
->pos
- 2, ns
);
3621 task
= next_tid(task
), ctx
->pos
++) {
3622 char name
[PROC_NUMBUF
];
3624 tid
= task_pid_nr_ns(task
, ns
);
3625 len
= snprintf(name
, sizeof(name
), "%d", tid
);
3626 if (!proc_fill_cache(file
, ctx
, name
, len
,
3627 proc_task_instantiate
, task
, NULL
)) {
3628 /* returning this tgid failed, save it as the first
3629 * pid for the next readir call */
3630 file
->f_version
= (u64
)tid
;
3631 put_task_struct(task
);
3639 static int proc_task_getattr(const struct path
*path
, struct kstat
*stat
,
3640 u32 request_mask
, unsigned int query_flags
)
3642 struct inode
*inode
= d_inode(path
->dentry
);
3643 struct task_struct
*p
= get_proc_task(inode
);
3644 generic_fillattr(inode
, stat
);
3647 stat
->nlink
+= get_nr_threads(p
);
3654 static const struct inode_operations proc_task_inode_operations
= {
3655 .lookup
= proc_task_lookup
,
3656 .getattr
= proc_task_getattr
,
3657 .setattr
= proc_setattr
,
3658 .permission
= proc_pid_permission
,
3661 static const struct file_operations proc_task_operations
= {
3662 .read
= generic_read_dir
,
3663 .iterate_shared
= proc_task_readdir
,
3664 .llseek
= generic_file_llseek
,
3667 void __init
set_proc_pid_nlink(void)
3669 nlink_tid
= pid_entry_nlink(tid_base_stuff
, ARRAY_SIZE(tid_base_stuff
));
3670 nlink_tgid
= pid_entry_nlink(tgid_base_stuff
, ARRAY_SIZE(tgid_base_stuff
));