4 * Copyright (C) 1991, 1992 Linus Torvalds
6 * proc base directory handling functions
8 * 1999, Al Viro. Rewritten. Now it covers the whole per-process part.
9 * Instead of using magical inumbers to determine the kind of object
10 * we allocate and fill in-core inodes upon lookup. They don't even
11 * go into icache. We cache the reference to task_struct upon lookup too.
12 * Eventually it should become a filesystem in its own. We don't use the
13 * rest of procfs anymore.
19 * Bruna Moreira <bruna.moreira@indt.org.br>
20 * Edjard Mota <edjard.mota@indt.org.br>
21 * Ilias Biris <ilias.biris@indt.org.br>
22 * Mauricio Lin <mauricio.lin@indt.org.br>
24 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
26 * A new process specific entry (smaps) included in /proc. It shows the
27 * size of rss for each memory area. The maps entry lacks information
28 * about physical memory size (rss) for each mapped file, i.e.,
29 * rss information for executables and library files.
30 * This additional information is useful for any tools that need to know
31 * about physical memory consumption for a process specific library.
35 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36 * Pud inclusion in the page table walking.
40 * 10LE Instituto Nokia de Tecnologia - INdT:
41 * A better way to walks through the page table as suggested by Hugh Dickins.
43 * Simo Piiroinen <simo.piiroinen@nokia.com>:
44 * Smaps information related to shared, private, clean and dirty pages.
46 * Paul Mundt <paul.mundt@nokia.com>:
47 * Overall revision about smaps.
50 #include <linux/uaccess.h>
52 #include <linux/errno.h>
53 #include <linux/time.h>
54 #include <linux/proc_fs.h>
55 #include <linux/stat.h>
56 #include <linux/task_io_accounting_ops.h>
57 #include <linux/init.h>
58 #include <linux/capability.h>
59 #include <linux/file.h>
60 #include <linux/fdtable.h>
61 #include <linux/string.h>
62 #include <linux/seq_file.h>
63 #include <linux/namei.h>
64 #include <linux/mnt_namespace.h>
66 #include <linux/swap.h>
67 #include <linux/rcupdate.h>
68 #include <linux/kallsyms.h>
69 #include <linux/stacktrace.h>
70 #include <linux/resource.h>
71 #include <linux/module.h>
72 #include <linux/mount.h>
73 #include <linux/security.h>
74 #include <linux/ptrace.h>
75 #include <linux/tracehook.h>
76 #include <linux/printk.h>
77 #include <linux/cgroup.h>
78 #include <linux/cpuset.h>
79 #include <linux/audit.h>
80 #include <linux/poll.h>
81 #include <linux/nsproxy.h>
82 #include <linux/oom.h>
83 #include <linux/elf.h>
84 #include <linux/pid_namespace.h>
85 #include <linux/user_namespace.h>
86 #include <linux/fs_struct.h>
87 #include <linux/slab.h>
88 #include <linux/sched/autogroup.h>
89 #include <linux/sched/mm.h>
90 #include <linux/sched/coredump.h>
91 #include <linux/sched/debug.h>
92 #include <linux/sched/stat.h>
93 #include <linux/flex_array.h>
94 #include <linux/posix-timers.h>
95 #ifdef CONFIG_HARDWALL
96 #include <asm/hardwall.h>
98 #include <trace/events/oom.h>
103 * Implementing inode permission operations in /proc is almost
104 * certainly an error. Permission checks need to happen during
105 * each system call not at open time. The reason is that most of
106 * what we wish to check for permissions in /proc varies at runtime.
108 * The classic example of a problem is opening file descriptors
109 * in /proc for a task before it execs a suid executable.
113 static u8 nlink_tgid
;
119 const struct inode_operations
*iop
;
120 const struct file_operations
*fop
;
124 #define NOD(NAME, MODE, IOP, FOP, OP) { \
126 .len = sizeof(NAME) - 1, \
133 #define DIR(NAME, MODE, iops, fops) \
134 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
135 #define LNK(NAME, get_link) \
136 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
137 &proc_pid_link_inode_operations, NULL, \
138 { .proc_get_link = get_link } )
139 #define REG(NAME, MODE, fops) \
140 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
141 #define ONE(NAME, MODE, show) \
142 NOD(NAME, (S_IFREG|(MODE)), \
143 NULL, &proc_single_file_operations, \
144 { .proc_show = show } )
145 #define ATTR(LSM, NAME, MODE) \
146 NOD(NAME, (S_IFREG|(MODE)), \
147 NULL, &proc_pid_attr_operations, \
151 * Count the number of hardlinks for the pid_entry table, excluding the .
154 static unsigned int __init
pid_entry_nlink(const struct pid_entry
*entries
,
161 for (i
= 0; i
< n
; ++i
) {
162 if (S_ISDIR(entries
[i
].mode
))
169 static int get_task_root(struct task_struct
*task
, struct path
*root
)
171 int result
= -ENOENT
;
175 get_fs_root(task
->fs
, root
);
182 static int proc_cwd_link(struct dentry
*dentry
, struct path
*path
)
184 struct task_struct
*task
= get_proc_task(d_inode(dentry
));
185 int result
= -ENOENT
;
190 get_fs_pwd(task
->fs
, path
);
194 put_task_struct(task
);
199 static int proc_root_link(struct dentry
*dentry
, struct path
*path
)
201 struct task_struct
*task
= get_proc_task(d_inode(dentry
));
202 int result
= -ENOENT
;
205 result
= get_task_root(task
, path
);
206 put_task_struct(task
);
211 static ssize_t
proc_pid_cmdline_read(struct file
*file
, char __user
*buf
,
212 size_t _count
, loff_t
*pos
)
214 struct task_struct
*tsk
;
215 struct mm_struct
*mm
;
217 unsigned long count
= _count
;
218 unsigned long arg_start
, arg_end
, env_start
, env_end
;
219 unsigned long len1
, len2
, len
;
226 tsk
= get_proc_task(file_inode(file
));
229 mm
= get_task_mm(tsk
);
230 put_task_struct(tsk
);
233 /* Check if process spawned far enough to have cmdline. */
239 page
= (char *)__get_free_page(GFP_TEMPORARY
);
245 down_read(&mm
->mmap_sem
);
246 arg_start
= mm
->arg_start
;
247 arg_end
= mm
->arg_end
;
248 env_start
= mm
->env_start
;
249 env_end
= mm
->env_end
;
250 up_read(&mm
->mmap_sem
);
252 BUG_ON(arg_start
> arg_end
);
253 BUG_ON(env_start
> env_end
);
255 len1
= arg_end
- arg_start
;
256 len2
= env_end
- env_start
;
264 * Inherently racy -- command line shares address space
265 * with code and data.
267 rv
= access_remote_vm(mm
, arg_end
- 1, &c
, 1, 0);
274 /* Command line (set of strings) occupies whole ARGV. */
278 p
= arg_start
+ *pos
;
280 while (count
> 0 && len
> 0) {
284 _count
= min3(count
, len
, PAGE_SIZE
);
285 nr_read
= access_remote_vm(mm
, p
, page
, _count
, 0);
291 if (copy_to_user(buf
, page
, nr_read
)) {
304 * Command line (1 string) occupies ARGV and
311 { .p
= arg_start
, .len
= len1
},
312 { .p
= env_start
, .len
= len2
},
318 while (i
< 2 && pos1
>= cmdline
[i
].len
) {
319 pos1
-= cmdline
[i
].len
;
323 p
= cmdline
[i
].p
+ pos1
;
324 len
= cmdline
[i
].len
- pos1
;
325 while (count
> 0 && len
> 0) {
326 unsigned int _count
, l
;
330 _count
= min3(count
, len
, PAGE_SIZE
);
331 nr_read
= access_remote_vm(mm
, p
, page
, _count
, 0);
338 * Command line can be shorter than whole ARGV
339 * even if last "marker" byte says it is not.
342 l
= strnlen(page
, nr_read
);
348 if (copy_to_user(buf
, page
, nr_read
)) {
363 /* Only first chunk can be read partially. */
370 free_page((unsigned long)page
);
378 static const struct file_operations proc_pid_cmdline_ops
= {
379 .read
= proc_pid_cmdline_read
,
380 .llseek
= generic_file_llseek
,
383 #ifdef CONFIG_KALLSYMS
385 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
386 * Returns the resolved symbol. If that fails, simply return the address.
388 static int proc_pid_wchan(struct seq_file
*m
, struct pid_namespace
*ns
,
389 struct pid
*pid
, struct task_struct
*task
)
392 char symname
[KSYM_NAME_LEN
];
394 wchan
= get_wchan(task
);
396 if (wchan
&& ptrace_may_access(task
, PTRACE_MODE_READ_FSCREDS
)
397 && !lookup_symbol_name(wchan
, symname
))
398 seq_printf(m
, "%s", symname
);
404 #endif /* CONFIG_KALLSYMS */
406 static int lock_trace(struct task_struct
*task
)
408 int err
= mutex_lock_killable(&task
->signal
->cred_guard_mutex
);
411 if (!ptrace_may_access(task
, PTRACE_MODE_ATTACH_FSCREDS
)) {
412 mutex_unlock(&task
->signal
->cred_guard_mutex
);
418 static void unlock_trace(struct task_struct
*task
)
420 mutex_unlock(&task
->signal
->cred_guard_mutex
);
423 #ifdef CONFIG_STACKTRACE
425 #define MAX_STACK_TRACE_DEPTH 64
427 static int proc_pid_stack(struct seq_file
*m
, struct pid_namespace
*ns
,
428 struct pid
*pid
, struct task_struct
*task
)
430 struct stack_trace trace
;
431 unsigned long *entries
;
435 entries
= kmalloc(MAX_STACK_TRACE_DEPTH
* sizeof(*entries
), GFP_KERNEL
);
439 trace
.nr_entries
= 0;
440 trace
.max_entries
= MAX_STACK_TRACE_DEPTH
;
441 trace
.entries
= entries
;
444 err
= lock_trace(task
);
446 save_stack_trace_tsk(task
, &trace
);
448 for (i
= 0; i
< trace
.nr_entries
; i
++) {
449 seq_printf(m
, "[<%pK>] %pB\n",
450 (void *)entries
[i
], (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_TEMPORARY
);
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_TEMPORARY
);
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 task_struct
*p
;
1424 p
= get_proc_task(inode
);
1427 proc_sched_show_task(p
, m
);
1435 sched_write(struct file
*file
, const char __user
*buf
,
1436 size_t count
, loff_t
*offset
)
1438 struct inode
*inode
= file_inode(file
);
1439 struct task_struct
*p
;
1441 p
= get_proc_task(inode
);
1444 proc_sched_set_task(p
);
1451 static int sched_open(struct inode
*inode
, struct file
*filp
)
1453 return single_open(filp
, sched_show
, inode
);
1456 static const struct file_operations proc_pid_sched_operations
= {
1459 .write
= sched_write
,
1460 .llseek
= seq_lseek
,
1461 .release
= single_release
,
1466 #ifdef CONFIG_SCHED_AUTOGROUP
1468 * Print out autogroup related information:
1470 static int sched_autogroup_show(struct seq_file
*m
, void *v
)
1472 struct inode
*inode
= m
->private;
1473 struct task_struct
*p
;
1475 p
= get_proc_task(inode
);
1478 proc_sched_autogroup_show_task(p
, m
);
1486 sched_autogroup_write(struct file
*file
, const char __user
*buf
,
1487 size_t count
, loff_t
*offset
)
1489 struct inode
*inode
= file_inode(file
);
1490 struct task_struct
*p
;
1491 char buffer
[PROC_NUMBUF
];
1495 memset(buffer
, 0, sizeof(buffer
));
1496 if (count
> sizeof(buffer
) - 1)
1497 count
= sizeof(buffer
) - 1;
1498 if (copy_from_user(buffer
, buf
, count
))
1501 err
= kstrtoint(strstrip(buffer
), 0, &nice
);
1505 p
= get_proc_task(inode
);
1509 err
= proc_sched_autogroup_set_nice(p
, nice
);
1518 static int sched_autogroup_open(struct inode
*inode
, struct file
*filp
)
1522 ret
= single_open(filp
, sched_autogroup_show
, NULL
);
1524 struct seq_file
*m
= filp
->private_data
;
1531 static const struct file_operations proc_pid_sched_autogroup_operations
= {
1532 .open
= sched_autogroup_open
,
1534 .write
= sched_autogroup_write
,
1535 .llseek
= seq_lseek
,
1536 .release
= single_release
,
1539 #endif /* CONFIG_SCHED_AUTOGROUP */
1541 static ssize_t
comm_write(struct file
*file
, const char __user
*buf
,
1542 size_t count
, loff_t
*offset
)
1544 struct inode
*inode
= file_inode(file
);
1545 struct task_struct
*p
;
1546 char buffer
[TASK_COMM_LEN
];
1547 const size_t maxlen
= sizeof(buffer
) - 1;
1549 memset(buffer
, 0, sizeof(buffer
));
1550 if (copy_from_user(buffer
, buf
, count
> maxlen
? maxlen
: count
))
1553 p
= get_proc_task(inode
);
1557 if (same_thread_group(current
, p
))
1558 set_task_comm(p
, buffer
);
1567 static int comm_show(struct seq_file
*m
, void *v
)
1569 struct inode
*inode
= m
->private;
1570 struct task_struct
*p
;
1572 p
= get_proc_task(inode
);
1577 seq_printf(m
, "%s\n", p
->comm
);
1585 static int comm_open(struct inode
*inode
, struct file
*filp
)
1587 return single_open(filp
, comm_show
, inode
);
1590 static const struct file_operations proc_pid_set_comm_operations
= {
1593 .write
= comm_write
,
1594 .llseek
= seq_lseek
,
1595 .release
= single_release
,
1598 static int proc_exe_link(struct dentry
*dentry
, struct path
*exe_path
)
1600 struct task_struct
*task
;
1601 struct file
*exe_file
;
1603 task
= get_proc_task(d_inode(dentry
));
1606 exe_file
= get_task_exe_file(task
);
1607 put_task_struct(task
);
1609 *exe_path
= exe_file
->f_path
;
1610 path_get(&exe_file
->f_path
);
1617 static const char *proc_pid_get_link(struct dentry
*dentry
,
1618 struct inode
*inode
,
1619 struct delayed_call
*done
)
1622 int error
= -EACCES
;
1625 return ERR_PTR(-ECHILD
);
1627 /* Are we allowed to snoop on the tasks file descriptors? */
1628 if (!proc_fd_access_allowed(inode
))
1631 error
= PROC_I(inode
)->op
.proc_get_link(dentry
, &path
);
1635 nd_jump_link(&path
);
1638 return ERR_PTR(error
);
1641 static int do_proc_readlink(struct path
*path
, char __user
*buffer
, int buflen
)
1643 char *tmp
= (char*)__get_free_page(GFP_TEMPORARY
);
1650 pathname
= d_path(path
, tmp
, PAGE_SIZE
);
1651 len
= PTR_ERR(pathname
);
1652 if (IS_ERR(pathname
))
1654 len
= tmp
+ PAGE_SIZE
- 1 - pathname
;
1658 if (copy_to_user(buffer
, pathname
, len
))
1661 free_page((unsigned long)tmp
);
1665 static int proc_pid_readlink(struct dentry
* dentry
, char __user
* buffer
, int buflen
)
1667 int error
= -EACCES
;
1668 struct inode
*inode
= d_inode(dentry
);
1671 /* Are we allowed to snoop on the tasks file descriptors? */
1672 if (!proc_fd_access_allowed(inode
))
1675 error
= PROC_I(inode
)->op
.proc_get_link(dentry
, &path
);
1679 error
= do_proc_readlink(&path
, buffer
, buflen
);
1685 const struct inode_operations proc_pid_link_inode_operations
= {
1686 .readlink
= proc_pid_readlink
,
1687 .get_link
= proc_pid_get_link
,
1688 .setattr
= proc_setattr
,
1692 /* building an inode */
1694 void task_dump_owner(struct task_struct
*task
, mode_t mode
,
1695 kuid_t
*ruid
, kgid_t
*rgid
)
1697 /* Depending on the state of dumpable compute who should own a
1698 * proc file for a task.
1700 const struct cred
*cred
;
1704 /* Default to the tasks effective ownership */
1706 cred
= __task_cred(task
);
1712 * Before the /proc/pid/status file was created the only way to read
1713 * the effective uid of a /process was to stat /proc/pid. Reading
1714 * /proc/pid/status is slow enough that procps and other packages
1715 * kept stating /proc/pid. To keep the rules in /proc simple I have
1716 * made this apply to all per process world readable and executable
1719 if (mode
!= (S_IFDIR
|S_IRUGO
|S_IXUGO
)) {
1720 struct mm_struct
*mm
;
1723 /* Make non-dumpable tasks owned by some root */
1725 if (get_dumpable(mm
) != SUID_DUMP_USER
) {
1726 struct user_namespace
*user_ns
= mm
->user_ns
;
1728 uid
= make_kuid(user_ns
, 0);
1729 if (!uid_valid(uid
))
1730 uid
= GLOBAL_ROOT_UID
;
1732 gid
= make_kgid(user_ns
, 0);
1733 if (!gid_valid(gid
))
1734 gid
= GLOBAL_ROOT_GID
;
1737 uid
= GLOBAL_ROOT_UID
;
1738 gid
= GLOBAL_ROOT_GID
;
1746 struct inode
*proc_pid_make_inode(struct super_block
* sb
,
1747 struct task_struct
*task
, umode_t mode
)
1749 struct inode
* inode
;
1750 struct proc_inode
*ei
;
1752 /* We need a new inode */
1754 inode
= new_inode(sb
);
1760 inode
->i_mode
= mode
;
1761 inode
->i_ino
= get_next_ino();
1762 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= current_time(inode
);
1763 inode
->i_op
= &proc_def_inode_operations
;
1766 * grab the reference to task.
1768 ei
->pid
= get_task_pid(task
, PIDTYPE_PID
);
1772 task_dump_owner(task
, 0, &inode
->i_uid
, &inode
->i_gid
);
1773 security_task_to_inode(task
, inode
);
1783 int pid_getattr(const struct path
*path
, struct kstat
*stat
,
1784 u32 request_mask
, unsigned int query_flags
)
1786 struct inode
*inode
= d_inode(path
->dentry
);
1787 struct task_struct
*task
;
1788 struct pid_namespace
*pid
= path
->dentry
->d_sb
->s_fs_info
;
1790 generic_fillattr(inode
, stat
);
1793 stat
->uid
= GLOBAL_ROOT_UID
;
1794 stat
->gid
= GLOBAL_ROOT_GID
;
1795 task
= pid_task(proc_pid(inode
), PIDTYPE_PID
);
1797 if (!has_pid_permissions(pid
, task
, HIDEPID_INVISIBLE
)) {
1800 * This doesn't prevent learning whether PID exists,
1801 * it only makes getattr() consistent with readdir().
1805 task_dump_owner(task
, inode
->i_mode
, &stat
->uid
, &stat
->gid
);
1814 * Exceptional case: normally we are not allowed to unhash a busy
1815 * directory. In this case, however, we can do it - no aliasing problems
1816 * due to the way we treat inodes.
1818 * Rewrite the inode's ownerships here because the owning task may have
1819 * performed a setuid(), etc.
1822 int pid_revalidate(struct dentry
*dentry
, unsigned int flags
)
1824 struct inode
*inode
;
1825 struct task_struct
*task
;
1827 if (flags
& LOOKUP_RCU
)
1830 inode
= d_inode(dentry
);
1831 task
= get_proc_task(inode
);
1834 task_dump_owner(task
, inode
->i_mode
, &inode
->i_uid
, &inode
->i_gid
);
1836 inode
->i_mode
&= ~(S_ISUID
| S_ISGID
);
1837 security_task_to_inode(task
, inode
);
1838 put_task_struct(task
);
1844 static inline bool proc_inode_is_dead(struct inode
*inode
)
1846 return !proc_pid(inode
)->tasks
[PIDTYPE_PID
].first
;
1849 int pid_delete_dentry(const struct dentry
*dentry
)
1851 /* Is the task we represent dead?
1852 * If so, then don't put the dentry on the lru list,
1853 * kill it immediately.
1855 return proc_inode_is_dead(d_inode(dentry
));
1858 const struct dentry_operations pid_dentry_operations
=
1860 .d_revalidate
= pid_revalidate
,
1861 .d_delete
= pid_delete_dentry
,
1867 * Fill a directory entry.
1869 * If possible create the dcache entry and derive our inode number and
1870 * file type from dcache entry.
1872 * Since all of the proc inode numbers are dynamically generated, the inode
1873 * numbers do not exist until the inode is cache. This means creating the
1874 * the dcache entry in readdir is necessary to keep the inode numbers
1875 * reported by readdir in sync with the inode numbers reported
1878 bool proc_fill_cache(struct file
*file
, struct dir_context
*ctx
,
1879 const char *name
, int len
,
1880 instantiate_t instantiate
, struct task_struct
*task
, const void *ptr
)
1882 struct dentry
*child
, *dir
= file
->f_path
.dentry
;
1883 struct qstr qname
= QSTR_INIT(name
, len
);
1884 struct inode
*inode
;
1888 child
= d_hash_and_lookup(dir
, &qname
);
1890 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq
);
1891 child
= d_alloc_parallel(dir
, &qname
, &wq
);
1893 goto end_instantiate
;
1894 if (d_in_lookup(child
)) {
1895 int err
= instantiate(d_inode(dir
), child
, task
, ptr
);
1896 d_lookup_done(child
);
1899 goto end_instantiate
;
1903 inode
= d_inode(child
);
1905 type
= inode
->i_mode
>> 12;
1907 return dir_emit(ctx
, name
, len
, ino
, type
);
1910 return dir_emit(ctx
, name
, len
, 1, DT_UNKNOWN
);
1914 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1915 * which represent vma start and end addresses.
1917 static int dname_to_vma_addr(struct dentry
*dentry
,
1918 unsigned long *start
, unsigned long *end
)
1920 if (sscanf(dentry
->d_name
.name
, "%lx-%lx", start
, end
) != 2)
1926 static int map_files_d_revalidate(struct dentry
*dentry
, unsigned int flags
)
1928 unsigned long vm_start
, vm_end
;
1929 bool exact_vma_exists
= false;
1930 struct mm_struct
*mm
= NULL
;
1931 struct task_struct
*task
;
1932 struct inode
*inode
;
1935 if (flags
& LOOKUP_RCU
)
1938 inode
= d_inode(dentry
);
1939 task
= get_proc_task(inode
);
1943 mm
= mm_access(task
, PTRACE_MODE_READ_FSCREDS
);
1944 if (IS_ERR_OR_NULL(mm
))
1947 if (!dname_to_vma_addr(dentry
, &vm_start
, &vm_end
)) {
1948 down_read(&mm
->mmap_sem
);
1949 exact_vma_exists
= !!find_exact_vma(mm
, vm_start
, vm_end
);
1950 up_read(&mm
->mmap_sem
);
1955 if (exact_vma_exists
) {
1956 task_dump_owner(task
, 0, &inode
->i_uid
, &inode
->i_gid
);
1958 security_task_to_inode(task
, inode
);
1963 put_task_struct(task
);
1969 static const struct dentry_operations tid_map_files_dentry_operations
= {
1970 .d_revalidate
= map_files_d_revalidate
,
1971 .d_delete
= pid_delete_dentry
,
1974 static int map_files_get_link(struct dentry
*dentry
, struct path
*path
)
1976 unsigned long vm_start
, vm_end
;
1977 struct vm_area_struct
*vma
;
1978 struct task_struct
*task
;
1979 struct mm_struct
*mm
;
1983 task
= get_proc_task(d_inode(dentry
));
1987 mm
= get_task_mm(task
);
1988 put_task_struct(task
);
1992 rc
= dname_to_vma_addr(dentry
, &vm_start
, &vm_end
);
1997 down_read(&mm
->mmap_sem
);
1998 vma
= find_exact_vma(mm
, vm_start
, vm_end
);
1999 if (vma
&& vma
->vm_file
) {
2000 *path
= vma_pr_or_file(vma
)->f_path
;
2004 up_read(&mm
->mmap_sem
);
2012 struct map_files_info
{
2015 unsigned char name
[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
2019 * Only allow CAP_SYS_ADMIN to follow the links, due to concerns about how the
2020 * symlinks may be used to bypass permissions on ancestor directories in the
2021 * path to the file in question.
2024 proc_map_files_get_link(struct dentry
*dentry
,
2025 struct inode
*inode
,
2026 struct delayed_call
*done
)
2028 if (!capable(CAP_SYS_ADMIN
))
2029 return ERR_PTR(-EPERM
);
2031 return proc_pid_get_link(dentry
, inode
, done
);
2035 * Identical to proc_pid_link_inode_operations except for get_link()
2037 static const struct inode_operations proc_map_files_link_inode_operations
= {
2038 .readlink
= proc_pid_readlink
,
2039 .get_link
= proc_map_files_get_link
,
2040 .setattr
= proc_setattr
,
2044 proc_map_files_instantiate(struct inode
*dir
, struct dentry
*dentry
,
2045 struct task_struct
*task
, const void *ptr
)
2047 fmode_t mode
= (fmode_t
)(unsigned long)ptr
;
2048 struct proc_inode
*ei
;
2049 struct inode
*inode
;
2051 inode
= proc_pid_make_inode(dir
->i_sb
, task
, S_IFLNK
|
2052 ((mode
& FMODE_READ
) ? S_IRUSR
: 0) |
2053 ((mode
& FMODE_WRITE
) ? S_IWUSR
: 0));
2058 ei
->op
.proc_get_link
= map_files_get_link
;
2060 inode
->i_op
= &proc_map_files_link_inode_operations
;
2063 d_set_d_op(dentry
, &tid_map_files_dentry_operations
);
2064 d_add(dentry
, inode
);
2069 static struct dentry
*proc_map_files_lookup(struct inode
*dir
,
2070 struct dentry
*dentry
, unsigned int flags
)
2072 unsigned long vm_start
, vm_end
;
2073 struct vm_area_struct
*vma
;
2074 struct task_struct
*task
;
2076 struct mm_struct
*mm
;
2079 task
= get_proc_task(dir
);
2084 if (!ptrace_may_access(task
, PTRACE_MODE_READ_FSCREDS
))
2088 if (dname_to_vma_addr(dentry
, &vm_start
, &vm_end
))
2091 mm
= get_task_mm(task
);
2095 down_read(&mm
->mmap_sem
);
2096 vma
= find_exact_vma(mm
, vm_start
, vm_end
);
2101 result
= proc_map_files_instantiate(dir
, dentry
, task
,
2102 (void *)(unsigned long)vma
->vm_file
->f_mode
);
2105 up_read(&mm
->mmap_sem
);
2108 put_task_struct(task
);
2110 return ERR_PTR(result
);
2113 static const struct inode_operations proc_map_files_inode_operations
= {
2114 .lookup
= proc_map_files_lookup
,
2115 .permission
= proc_fd_permission
,
2116 .setattr
= proc_setattr
,
2120 proc_map_files_readdir(struct file
*file
, struct dir_context
*ctx
)
2122 struct vm_area_struct
*vma
;
2123 struct task_struct
*task
;
2124 struct mm_struct
*mm
;
2125 unsigned long nr_files
, pos
, i
;
2126 struct flex_array
*fa
= NULL
;
2127 struct map_files_info info
;
2128 struct map_files_info
*p
;
2132 task
= get_proc_task(file_inode(file
));
2137 if (!ptrace_may_access(task
, PTRACE_MODE_READ_FSCREDS
))
2141 if (!dir_emit_dots(file
, ctx
))
2144 mm
= get_task_mm(task
);
2147 down_read(&mm
->mmap_sem
);
2152 * We need two passes here:
2154 * 1) Collect vmas of mapped files with mmap_sem taken
2155 * 2) Release mmap_sem and instantiate entries
2157 * otherwise we get lockdep complained, since filldir()
2158 * routine might require mmap_sem taken in might_fault().
2161 for (vma
= mm
->mmap
, pos
= 2; vma
; vma
= vma
->vm_next
) {
2162 if (vma
->vm_file
&& ++pos
> ctx
->pos
)
2167 fa
= flex_array_alloc(sizeof(info
), nr_files
,
2169 if (!fa
|| flex_array_prealloc(fa
, 0, nr_files
,
2173 flex_array_free(fa
);
2174 up_read(&mm
->mmap_sem
);
2178 for (i
= 0, vma
= mm
->mmap
, pos
= 2; vma
;
2179 vma
= vma
->vm_next
) {
2182 if (++pos
<= ctx
->pos
)
2185 info
.mode
= vma
->vm_file
->f_mode
;
2186 info
.len
= snprintf(info
.name
,
2187 sizeof(info
.name
), "%lx-%lx",
2188 vma
->vm_start
, vma
->vm_end
);
2189 if (flex_array_put(fa
, i
++, &info
, GFP_KERNEL
))
2193 up_read(&mm
->mmap_sem
);
2195 for (i
= 0; i
< nr_files
; i
++) {
2196 p
= flex_array_get(fa
, i
);
2197 if (!proc_fill_cache(file
, ctx
,
2199 proc_map_files_instantiate
,
2201 (void *)(unsigned long)p
->mode
))
2206 flex_array_free(fa
);
2210 put_task_struct(task
);
2215 static const struct file_operations proc_map_files_operations
= {
2216 .read
= generic_read_dir
,
2217 .iterate_shared
= proc_map_files_readdir
,
2218 .llseek
= generic_file_llseek
,
2221 #if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
2222 struct timers_private
{
2224 struct task_struct
*task
;
2225 struct sighand_struct
*sighand
;
2226 struct pid_namespace
*ns
;
2227 unsigned long flags
;
2230 static void *timers_start(struct seq_file
*m
, loff_t
*pos
)
2232 struct timers_private
*tp
= m
->private;
2234 tp
->task
= get_pid_task(tp
->pid
, PIDTYPE_PID
);
2236 return ERR_PTR(-ESRCH
);
2238 tp
->sighand
= lock_task_sighand(tp
->task
, &tp
->flags
);
2240 return ERR_PTR(-ESRCH
);
2242 return seq_list_start(&tp
->task
->signal
->posix_timers
, *pos
);
2245 static void *timers_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
2247 struct timers_private
*tp
= m
->private;
2248 return seq_list_next(v
, &tp
->task
->signal
->posix_timers
, pos
);
2251 static void timers_stop(struct seq_file
*m
, void *v
)
2253 struct timers_private
*tp
= m
->private;
2256 unlock_task_sighand(tp
->task
, &tp
->flags
);
2261 put_task_struct(tp
->task
);
2266 static int show_timer(struct seq_file
*m
, void *v
)
2268 struct k_itimer
*timer
;
2269 struct timers_private
*tp
= m
->private;
2271 static const char * const nstr
[] = {
2272 [SIGEV_SIGNAL
] = "signal",
2273 [SIGEV_NONE
] = "none",
2274 [SIGEV_THREAD
] = "thread",
2277 timer
= list_entry((struct list_head
*)v
, struct k_itimer
, list
);
2278 notify
= timer
->it_sigev_notify
;
2280 seq_printf(m
, "ID: %d\n", timer
->it_id
);
2281 seq_printf(m
, "signal: %d/%p\n",
2282 timer
->sigq
->info
.si_signo
,
2283 timer
->sigq
->info
.si_value
.sival_ptr
);
2284 seq_printf(m
, "notify: %s/%s.%d\n",
2285 nstr
[notify
& ~SIGEV_THREAD_ID
],
2286 (notify
& SIGEV_THREAD_ID
) ? "tid" : "pid",
2287 pid_nr_ns(timer
->it_pid
, tp
->ns
));
2288 seq_printf(m
, "ClockID: %d\n", timer
->it_clock
);
2293 static const struct seq_operations proc_timers_seq_ops
= {
2294 .start
= timers_start
,
2295 .next
= timers_next
,
2296 .stop
= timers_stop
,
2300 static int proc_timers_open(struct inode
*inode
, struct file
*file
)
2302 struct timers_private
*tp
;
2304 tp
= __seq_open_private(file
, &proc_timers_seq_ops
,
2305 sizeof(struct timers_private
));
2309 tp
->pid
= proc_pid(inode
);
2310 tp
->ns
= inode
->i_sb
->s_fs_info
;
2314 static const struct file_operations proc_timers_operations
= {
2315 .open
= proc_timers_open
,
2317 .llseek
= seq_lseek
,
2318 .release
= seq_release_private
,
2322 static ssize_t
timerslack_ns_write(struct file
*file
, const char __user
*buf
,
2323 size_t count
, loff_t
*offset
)
2325 struct inode
*inode
= file_inode(file
);
2326 struct task_struct
*p
;
2330 err
= kstrtoull_from_user(buf
, count
, 10, &slack_ns
);
2334 p
= get_proc_task(inode
);
2339 if (!capable(CAP_SYS_NICE
)) {
2344 err
= security_task_setscheduler(p
);
2353 p
->timer_slack_ns
= p
->default_timer_slack_ns
;
2355 p
->timer_slack_ns
= slack_ns
;
2364 static int timerslack_ns_show(struct seq_file
*m
, void *v
)
2366 struct inode
*inode
= m
->private;
2367 struct task_struct
*p
;
2370 p
= get_proc_task(inode
);
2376 if (!capable(CAP_SYS_NICE
)) {
2380 err
= security_task_getscheduler(p
);
2386 seq_printf(m
, "%llu\n", p
->timer_slack_ns
);
2395 static int timerslack_ns_open(struct inode
*inode
, struct file
*filp
)
2397 return single_open(filp
, timerslack_ns_show
, inode
);
2400 static const struct file_operations proc_pid_set_timerslack_ns_operations
= {
2401 .open
= timerslack_ns_open
,
2403 .write
= timerslack_ns_write
,
2404 .llseek
= seq_lseek
,
2405 .release
= single_release
,
2408 static int proc_pident_instantiate(struct inode
*dir
,
2409 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
2411 const struct pid_entry
*p
= ptr
;
2412 struct inode
*inode
;
2413 struct proc_inode
*ei
;
2415 inode
= proc_pid_make_inode(dir
->i_sb
, task
, p
->mode
);
2420 if (S_ISDIR(inode
->i_mode
))
2421 set_nlink(inode
, 2); /* Use getattr to fix if necessary */
2423 inode
->i_op
= p
->iop
;
2425 inode
->i_fop
= p
->fop
;
2427 d_set_d_op(dentry
, &pid_dentry_operations
);
2428 d_add(dentry
, inode
);
2429 /* Close the race of the process dying before we return the dentry */
2430 if (pid_revalidate(dentry
, 0))
2436 static struct dentry
*proc_pident_lookup(struct inode
*dir
,
2437 struct dentry
*dentry
,
2438 const struct pid_entry
*ents
,
2442 struct task_struct
*task
= get_proc_task(dir
);
2443 const struct pid_entry
*p
, *last
;
2451 * Yes, it does not scale. And it should not. Don't add
2452 * new entries into /proc/<tgid>/ without very good reasons.
2454 last
= &ents
[nents
];
2455 for (p
= ents
; p
< last
; p
++) {
2456 if (p
->len
!= dentry
->d_name
.len
)
2458 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
))
2464 error
= proc_pident_instantiate(dir
, dentry
, task
, p
);
2466 put_task_struct(task
);
2468 return ERR_PTR(error
);
2471 static int proc_pident_readdir(struct file
*file
, struct dir_context
*ctx
,
2472 const struct pid_entry
*ents
, unsigned int nents
)
2474 struct task_struct
*task
= get_proc_task(file_inode(file
));
2475 const struct pid_entry
*p
;
2480 if (!dir_emit_dots(file
, ctx
))
2483 if (ctx
->pos
>= nents
+ 2)
2486 for (p
= ents
+ (ctx
->pos
- 2); p
< ents
+ nents
; p
++) {
2487 if (!proc_fill_cache(file
, ctx
, p
->name
, p
->len
,
2488 proc_pident_instantiate
, task
, p
))
2493 put_task_struct(task
);
2497 #ifdef CONFIG_SECURITY
2498 static ssize_t
proc_pid_attr_read(struct file
* file
, char __user
* buf
,
2499 size_t count
, loff_t
*ppos
)
2501 struct inode
* inode
= file_inode(file
);
2504 struct task_struct
*task
= get_proc_task(inode
);
2509 length
= security_getprocattr(task
, PROC_I(inode
)->op
.lsm
,
2510 (char*)file
->f_path
.dentry
->d_name
.name
,
2512 put_task_struct(task
);
2514 length
= simple_read_from_buffer(buf
, count
, ppos
, p
, length
);
2519 static ssize_t
proc_pid_attr_write(struct file
* file
, const char __user
* buf
,
2520 size_t count
, loff_t
*ppos
)
2522 struct inode
* inode
= file_inode(file
);
2525 struct task_struct
*task
= get_proc_task(inode
);
2531 /* A task may only write its own attributes. */
2533 if (current
!= task
)
2536 if (count
> PAGE_SIZE
)
2539 /* No partial writes. */
2544 page
= memdup_user(buf
, count
);
2546 length
= PTR_ERR(page
);
2550 /* Guard against adverse ptrace interaction */
2551 length
= mutex_lock_interruptible(¤t
->signal
->cred_guard_mutex
);
2555 length
= security_setprocattr(PROC_I(inode
)->op
.lsm
,
2556 file
->f_path
.dentry
->d_name
.name
,
2558 mutex_unlock(¤t
->signal
->cred_guard_mutex
);
2562 put_task_struct(task
);
2567 static const struct file_operations proc_pid_attr_operations
= {
2568 .read
= proc_pid_attr_read
,
2569 .write
= proc_pid_attr_write
,
2570 .llseek
= generic_file_llseek
,
2573 #define LSM_DIR_OPS(LSM) \
2574 static int proc_##LSM##_attr_dir_iterate(struct file *filp, \
2575 struct dir_context *ctx) \
2577 return proc_pident_readdir(filp, ctx, \
2578 LSM##_attr_dir_stuff, \
2579 ARRAY_SIZE(LSM##_attr_dir_stuff)); \
2582 static const struct file_operations proc_##LSM##_attr_dir_ops = { \
2583 .read = generic_read_dir, \
2584 .iterate = proc_##LSM##_attr_dir_iterate, \
2585 .llseek = default_llseek, \
2588 static struct dentry *proc_##LSM##_attr_dir_lookup(struct inode *dir, \
2589 struct dentry *dentry, unsigned int flags) \
2591 return proc_pident_lookup(dir, dentry, \
2592 LSM##_attr_dir_stuff, \
2593 ARRAY_SIZE(LSM##_attr_dir_stuff)); \
2596 static const struct inode_operations proc_##LSM##_attr_dir_inode_ops = { \
2597 .lookup = proc_##LSM##_attr_dir_lookup, \
2598 .getattr = pid_getattr, \
2599 .setattr = proc_setattr, \
2602 #ifdef CONFIG_SECURITY_SELINUX
2603 static const struct pid_entry selinux_attr_dir_stuff
[] = {
2604 ATTR("selinux", "current", 0666),
2605 ATTR("selinux", "prev", 0444),
2606 ATTR("selinux", "exec", 0666),
2607 ATTR("selinux", "fscreate", 0666),
2608 ATTR("selinux", "keycreate", 0666),
2609 ATTR("selinux", "sockcreate", 0666),
2610 ATTR("selinux", "context", 0666),
2612 LSM_DIR_OPS(selinux
);
2615 #ifdef CONFIG_SECURITY_SMACK
2616 static const struct pid_entry smack_attr_dir_stuff
[] = {
2617 ATTR("smack", "current", 0666),
2618 ATTR("smack", "context", 0666),
2623 #ifdef CONFIG_SECURITY_APPARMOR
2624 static const struct pid_entry apparmor_attr_dir_stuff
[] = {
2625 ATTR("apparmor", "current", 0666),
2626 ATTR("apparmor", "prev", 0444),
2627 ATTR("apparmor", "exec", 0666),
2628 ATTR("apparmor", "context", 0666),
2630 LSM_DIR_OPS(apparmor
);
2633 static const struct pid_entry attr_dir_stuff
[] = {
2634 ATTR(NULL
, "current", 0666),
2635 ATTR(NULL
, "prev", 0444),
2636 ATTR(NULL
, "exec", 0666),
2637 ATTR(NULL
, "fscreate", 0666),
2638 ATTR(NULL
, "keycreate", 0666),
2639 ATTR(NULL
, "sockcreate", 0666),
2640 ATTR(NULL
, "context", 0666),
2641 ATTR(NULL
, "display_lsm", 0666),
2643 #ifdef CONFIG_SECURITY_SELINUX
2644 DIR("selinux", 0555,
2645 proc_selinux_attr_dir_inode_ops
, proc_selinux_attr_dir_ops
),
2647 #ifdef CONFIG_SECURITY_SMACK
2649 proc_smack_attr_dir_inode_ops
, proc_smack_attr_dir_ops
),
2651 #ifdef CONFIG_SECURITY_APPARMOR
2652 DIR("apparmor", 0555,
2653 proc_apparmor_attr_dir_inode_ops
, proc_apparmor_attr_dir_ops
),
2657 static int proc_attr_dir_readdir(struct file
*file
, struct dir_context
*ctx
)
2659 return proc_pident_readdir(file
, ctx
,
2660 attr_dir_stuff
, ARRAY_SIZE(attr_dir_stuff
));
2663 static const struct file_operations proc_attr_dir_operations
= {
2664 .read
= generic_read_dir
,
2665 .iterate_shared
= proc_attr_dir_readdir
,
2666 .llseek
= generic_file_llseek
,
2669 static struct dentry
*proc_attr_dir_lookup(struct inode
*dir
,
2670 struct dentry
*dentry
, unsigned int flags
)
2672 return proc_pident_lookup(dir
, dentry
,
2673 attr_dir_stuff
, ARRAY_SIZE(attr_dir_stuff
));
2676 static const struct inode_operations proc_attr_dir_inode_operations
= {
2677 .lookup
= proc_attr_dir_lookup
,
2678 .getattr
= pid_getattr
,
2679 .setattr
= proc_setattr
,
2684 #ifdef CONFIG_ELF_CORE
2685 static ssize_t
proc_coredump_filter_read(struct file
*file
, char __user
*buf
,
2686 size_t count
, loff_t
*ppos
)
2688 struct task_struct
*task
= get_proc_task(file_inode(file
));
2689 struct mm_struct
*mm
;
2690 char buffer
[PROC_NUMBUF
];
2698 mm
= get_task_mm(task
);
2700 len
= snprintf(buffer
, sizeof(buffer
), "%08lx\n",
2701 ((mm
->flags
& MMF_DUMP_FILTER_MASK
) >>
2702 MMF_DUMP_FILTER_SHIFT
));
2704 ret
= simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
2707 put_task_struct(task
);
2712 static ssize_t
proc_coredump_filter_write(struct file
*file
,
2713 const char __user
*buf
,
2717 struct task_struct
*task
;
2718 struct mm_struct
*mm
;
2724 ret
= kstrtouint_from_user(buf
, count
, 0, &val
);
2729 task
= get_proc_task(file_inode(file
));
2733 mm
= get_task_mm(task
);
2738 for (i
= 0, mask
= 1; i
< MMF_DUMP_FILTER_BITS
; i
++, mask
<<= 1) {
2740 set_bit(i
+ MMF_DUMP_FILTER_SHIFT
, &mm
->flags
);
2742 clear_bit(i
+ MMF_DUMP_FILTER_SHIFT
, &mm
->flags
);
2747 put_task_struct(task
);
2754 static const struct file_operations proc_coredump_filter_operations
= {
2755 .read
= proc_coredump_filter_read
,
2756 .write
= proc_coredump_filter_write
,
2757 .llseek
= generic_file_llseek
,
2761 #ifdef CONFIG_TASK_IO_ACCOUNTING
2762 static int do_io_accounting(struct task_struct
*task
, struct seq_file
*m
, int whole
)
2764 struct task_io_accounting acct
= task
->ioac
;
2765 unsigned long flags
;
2768 result
= mutex_lock_killable(&task
->signal
->cred_guard_mutex
);
2772 if (!ptrace_may_access(task
, PTRACE_MODE_READ_FSCREDS
)) {
2777 if (whole
&& lock_task_sighand(task
, &flags
)) {
2778 struct task_struct
*t
= task
;
2780 task_io_accounting_add(&acct
, &task
->signal
->ioac
);
2781 while_each_thread(task
, t
)
2782 task_io_accounting_add(&acct
, &t
->ioac
);
2784 unlock_task_sighand(task
, &flags
);
2791 "read_bytes: %llu\n"
2792 "write_bytes: %llu\n"
2793 "cancelled_write_bytes: %llu\n",
2794 (unsigned long long)acct
.rchar
,
2795 (unsigned long long)acct
.wchar
,
2796 (unsigned long long)acct
.syscr
,
2797 (unsigned long long)acct
.syscw
,
2798 (unsigned long long)acct
.read_bytes
,
2799 (unsigned long long)acct
.write_bytes
,
2800 (unsigned long long)acct
.cancelled_write_bytes
);
2804 mutex_unlock(&task
->signal
->cred_guard_mutex
);
2808 static int proc_tid_io_accounting(struct seq_file
*m
, struct pid_namespace
*ns
,
2809 struct pid
*pid
, struct task_struct
*task
)
2811 return do_io_accounting(task
, m
, 0);
2814 static int proc_tgid_io_accounting(struct seq_file
*m
, struct pid_namespace
*ns
,
2815 struct pid
*pid
, struct task_struct
*task
)
2817 return do_io_accounting(task
, m
, 1);
2819 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2821 #ifdef CONFIG_USER_NS
2822 static int proc_id_map_open(struct inode
*inode
, struct file
*file
,
2823 const struct seq_operations
*seq_ops
)
2825 struct user_namespace
*ns
= NULL
;
2826 struct task_struct
*task
;
2827 struct seq_file
*seq
;
2830 task
= get_proc_task(inode
);
2833 ns
= get_user_ns(task_cred_xxx(task
, user_ns
));
2835 put_task_struct(task
);
2840 ret
= seq_open(file
, seq_ops
);
2844 seq
= file
->private_data
;
2854 static int proc_id_map_release(struct inode
*inode
, struct file
*file
)
2856 struct seq_file
*seq
= file
->private_data
;
2857 struct user_namespace
*ns
= seq
->private;
2859 return seq_release(inode
, file
);
2862 static int proc_uid_map_open(struct inode
*inode
, struct file
*file
)
2864 return proc_id_map_open(inode
, file
, &proc_uid_seq_operations
);
2867 static int proc_gid_map_open(struct inode
*inode
, struct file
*file
)
2869 return proc_id_map_open(inode
, file
, &proc_gid_seq_operations
);
2872 static int proc_projid_map_open(struct inode
*inode
, struct file
*file
)
2874 return proc_id_map_open(inode
, file
, &proc_projid_seq_operations
);
2877 static const struct file_operations proc_uid_map_operations
= {
2878 .open
= proc_uid_map_open
,
2879 .write
= proc_uid_map_write
,
2881 .llseek
= seq_lseek
,
2882 .release
= proc_id_map_release
,
2885 static const struct file_operations proc_gid_map_operations
= {
2886 .open
= proc_gid_map_open
,
2887 .write
= proc_gid_map_write
,
2889 .llseek
= seq_lseek
,
2890 .release
= proc_id_map_release
,
2893 static const struct file_operations proc_projid_map_operations
= {
2894 .open
= proc_projid_map_open
,
2895 .write
= proc_projid_map_write
,
2897 .llseek
= seq_lseek
,
2898 .release
= proc_id_map_release
,
2901 static int proc_setgroups_open(struct inode
*inode
, struct file
*file
)
2903 struct user_namespace
*ns
= NULL
;
2904 struct task_struct
*task
;
2908 task
= get_proc_task(inode
);
2911 ns
= get_user_ns(task_cred_xxx(task
, user_ns
));
2913 put_task_struct(task
);
2918 if (file
->f_mode
& FMODE_WRITE
) {
2920 if (!ns_capable(ns
, CAP_SYS_ADMIN
))
2924 ret
= single_open(file
, &proc_setgroups_show
, ns
);
2935 static int proc_setgroups_release(struct inode
*inode
, struct file
*file
)
2937 struct seq_file
*seq
= file
->private_data
;
2938 struct user_namespace
*ns
= seq
->private;
2939 int ret
= single_release(inode
, file
);
2944 static const struct file_operations proc_setgroups_operations
= {
2945 .open
= proc_setgroups_open
,
2946 .write
= proc_setgroups_write
,
2948 .llseek
= seq_lseek
,
2949 .release
= proc_setgroups_release
,
2951 #endif /* CONFIG_USER_NS */
2953 static int proc_pid_personality(struct seq_file
*m
, struct pid_namespace
*ns
,
2954 struct pid
*pid
, struct task_struct
*task
)
2956 int err
= lock_trace(task
);
2958 seq_printf(m
, "%08x\n", task
->personality
);
2964 #ifdef CONFIG_LIVEPATCH
2965 static int proc_pid_patch_state(struct seq_file
*m
, struct pid_namespace
*ns
,
2966 struct pid
*pid
, struct task_struct
*task
)
2968 seq_printf(m
, "%d\n", task
->patch_state
);
2971 #endif /* CONFIG_LIVEPATCH */
2976 static const struct file_operations proc_task_operations
;
2977 static const struct inode_operations proc_task_inode_operations
;
2979 static const struct pid_entry tgid_base_stuff
[] = {
2980 DIR("task", S_IRUGO
|S_IXUGO
, proc_task_inode_operations
, proc_task_operations
),
2981 DIR("fd", S_IRUSR
|S_IXUSR
, proc_fd_inode_operations
, proc_fd_operations
),
2982 DIR("map_files", S_IRUSR
|S_IXUSR
, proc_map_files_inode_operations
, proc_map_files_operations
),
2983 DIR("fdinfo", S_IRUSR
|S_IXUSR
, proc_fdinfo_inode_operations
, proc_fdinfo_operations
),
2984 DIR("ns", S_IRUSR
|S_IXUGO
, proc_ns_dir_inode_operations
, proc_ns_dir_operations
),
2986 DIR("net", S_IRUGO
|S_IXUGO
, proc_net_inode_operations
, proc_net_operations
),
2988 REG("environ", S_IRUSR
, proc_environ_operations
),
2989 REG("auxv", S_IRUSR
, proc_auxv_operations
),
2990 ONE("status", S_IRUGO
, proc_pid_status
),
2991 ONE("personality", S_IRUSR
, proc_pid_personality
),
2992 ONE("limits", S_IRUGO
, proc_pid_limits
),
2993 #ifdef CONFIG_SCHED_DEBUG
2994 REG("sched", S_IRUGO
|S_IWUSR
, proc_pid_sched_operations
),
2996 #ifdef CONFIG_SCHED_AUTOGROUP
2997 REG("autogroup", S_IRUGO
|S_IWUSR
, proc_pid_sched_autogroup_operations
),
2999 REG("comm", S_IRUGO
|S_IWUSR
, proc_pid_set_comm_operations
),
3000 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3001 ONE("syscall", S_IRUSR
, proc_pid_syscall
),
3003 REG("cmdline", S_IRUGO
, proc_pid_cmdline_ops
),
3004 ONE("stat", S_IRUGO
, proc_tgid_stat
),
3005 ONE("statm", S_IRUGO
, proc_pid_statm
),
3006 REG("maps", S_IRUGO
, proc_pid_maps_operations
),
3008 REG("numa_maps", S_IRUGO
, proc_pid_numa_maps_operations
),
3010 REG("mem", S_IRUSR
|S_IWUSR
, proc_mem_operations
),
3011 LNK("cwd", proc_cwd_link
),
3012 LNK("root", proc_root_link
),
3013 LNK("exe", proc_exe_link
),
3014 REG("mounts", S_IRUGO
, proc_mounts_operations
),
3015 REG("mountinfo", S_IRUGO
, proc_mountinfo_operations
),
3016 REG("mountstats", S_IRUSR
, proc_mountstats_operations
),
3017 #ifdef CONFIG_PROC_PAGE_MONITOR
3018 REG("clear_refs", S_IWUSR
, proc_clear_refs_operations
),
3019 REG("smaps", S_IRUGO
, proc_pid_smaps_operations
),
3020 REG("pagemap", S_IRUSR
, proc_pagemap_operations
),
3022 #ifdef CONFIG_SECURITY
3023 DIR("attr", S_IRUGO
|S_IXUGO
, proc_attr_dir_inode_operations
, proc_attr_dir_operations
),
3025 #ifdef CONFIG_KALLSYMS
3026 ONE("wchan", S_IRUGO
, proc_pid_wchan
),
3028 #ifdef CONFIG_STACKTRACE
3029 ONE("stack", S_IRUSR
, proc_pid_stack
),
3031 #ifdef CONFIG_SCHED_INFO
3032 ONE("schedstat", S_IRUGO
, proc_pid_schedstat
),
3034 #ifdef CONFIG_LATENCYTOP
3035 REG("latency", S_IRUGO
, proc_lstats_operations
),
3037 #ifdef CONFIG_PROC_PID_CPUSET
3038 ONE("cpuset", S_IRUGO
, proc_cpuset_show
),
3040 #ifdef CONFIG_CGROUPS
3041 ONE("cgroup", S_IRUGO
, proc_cgroup_show
),
3043 ONE("oom_score", S_IRUGO
, proc_oom_score
),
3044 REG("oom_adj", S_IRUGO
|S_IWUSR
, proc_oom_adj_operations
),
3045 REG("oom_score_adj", S_IRUGO
|S_IWUSR
, proc_oom_score_adj_operations
),
3046 #ifdef CONFIG_AUDITSYSCALL
3047 REG("loginuid", S_IWUSR
|S_IRUGO
, proc_loginuid_operations
),
3048 REG("sessionid", S_IRUGO
, proc_sessionid_operations
),
3050 #ifdef CONFIG_FAULT_INJECTION
3051 REG("make-it-fail", S_IRUGO
|S_IWUSR
, proc_fault_inject_operations
),
3052 REG("fail-nth", 0644, proc_fail_nth_operations
),
3054 #ifdef CONFIG_ELF_CORE
3055 REG("coredump_filter", S_IRUGO
|S_IWUSR
, proc_coredump_filter_operations
),
3057 #ifdef CONFIG_TASK_IO_ACCOUNTING
3058 ONE("io", S_IRUSR
, proc_tgid_io_accounting
),
3060 #ifdef CONFIG_HARDWALL
3061 ONE("hardwall", S_IRUGO
, proc_pid_hardwall
),
3063 #ifdef CONFIG_USER_NS
3064 REG("uid_map", S_IRUGO
|S_IWUSR
, proc_uid_map_operations
),
3065 REG("gid_map", S_IRUGO
|S_IWUSR
, proc_gid_map_operations
),
3066 REG("projid_map", S_IRUGO
|S_IWUSR
, proc_projid_map_operations
),
3067 REG("setgroups", S_IRUGO
|S_IWUSR
, proc_setgroups_operations
),
3069 #if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
3070 REG("timers", S_IRUGO
, proc_timers_operations
),
3072 REG("timerslack_ns", S_IRUGO
|S_IWUGO
, proc_pid_set_timerslack_ns_operations
),
3073 #ifdef CONFIG_LIVEPATCH
3074 ONE("patch_state", S_IRUSR
, proc_pid_patch_state
),
3078 static int proc_tgid_base_readdir(struct file
*file
, struct dir_context
*ctx
)
3080 return proc_pident_readdir(file
, ctx
,
3081 tgid_base_stuff
, ARRAY_SIZE(tgid_base_stuff
));
3084 static const struct file_operations proc_tgid_base_operations
= {
3085 .read
= generic_read_dir
,
3086 .iterate_shared
= proc_tgid_base_readdir
,
3087 .llseek
= generic_file_llseek
,
3090 static struct dentry
*proc_tgid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, unsigned int flags
)
3092 return proc_pident_lookup(dir
, dentry
,
3093 tgid_base_stuff
, ARRAY_SIZE(tgid_base_stuff
));
3096 static const struct inode_operations proc_tgid_base_inode_operations
= {
3097 .lookup
= proc_tgid_base_lookup
,
3098 .getattr
= pid_getattr
,
3099 .setattr
= proc_setattr
,
3100 .permission
= proc_pid_permission
,
3103 static void proc_flush_task_mnt(struct vfsmount
*mnt
, pid_t pid
, pid_t tgid
)
3105 struct dentry
*dentry
, *leader
, *dir
;
3106 char buf
[PROC_NUMBUF
];
3110 name
.len
= snprintf(buf
, sizeof(buf
), "%d", pid
);
3111 /* no ->d_hash() rejects on procfs */
3112 dentry
= d_hash_and_lookup(mnt
->mnt_root
, &name
);
3114 d_invalidate(dentry
);
3122 name
.len
= snprintf(buf
, sizeof(buf
), "%d", tgid
);
3123 leader
= d_hash_and_lookup(mnt
->mnt_root
, &name
);
3128 name
.len
= strlen(name
.name
);
3129 dir
= d_hash_and_lookup(leader
, &name
);
3131 goto out_put_leader
;
3134 name
.len
= snprintf(buf
, sizeof(buf
), "%d", pid
);
3135 dentry
= d_hash_and_lookup(dir
, &name
);
3137 d_invalidate(dentry
);
3149 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
3150 * @task: task that should be flushed.
3152 * When flushing dentries from proc, one needs to flush them from global
3153 * proc (proc_mnt) and from all the namespaces' procs this task was seen
3154 * in. This call is supposed to do all of this job.
3156 * Looks in the dcache for
3158 * /proc/@tgid/task/@pid
3159 * if either directory is present flushes it and all of it'ts children
3162 * It is safe and reasonable to cache /proc entries for a task until
3163 * that task exits. After that they just clog up the dcache with
3164 * useless entries, possibly causing useful dcache entries to be
3165 * flushed instead. This routine is proved to flush those useless
3166 * dcache entries at process exit time.
3168 * NOTE: This routine is just an optimization so it does not guarantee
3169 * that no dcache entries will exist at process exit time it
3170 * just makes it very unlikely that any will persist.
3173 void proc_flush_task(struct task_struct
*task
)
3176 struct pid
*pid
, *tgid
;
3179 pid
= task_pid(task
);
3180 tgid
= task_tgid(task
);
3182 for (i
= 0; i
<= pid
->level
; i
++) {
3183 upid
= &pid
->numbers
[i
];
3184 proc_flush_task_mnt(upid
->ns
->proc_mnt
, upid
->nr
,
3185 tgid
->numbers
[i
].nr
);
3189 static int proc_pid_instantiate(struct inode
*dir
,
3190 struct dentry
* dentry
,
3191 struct task_struct
*task
, const void *ptr
)
3193 struct inode
*inode
;
3195 inode
= proc_pid_make_inode(dir
->i_sb
, task
, S_IFDIR
| S_IRUGO
| S_IXUGO
);
3199 inode
->i_op
= &proc_tgid_base_inode_operations
;
3200 inode
->i_fop
= &proc_tgid_base_operations
;
3201 inode
->i_flags
|=S_IMMUTABLE
;
3203 set_nlink(inode
, nlink_tgid
);
3205 d_set_d_op(dentry
, &pid_dentry_operations
);
3207 d_add(dentry
, inode
);
3208 /* Close the race of the process dying before we return the dentry */
3209 if (pid_revalidate(dentry
, 0))
3215 struct dentry
*proc_pid_lookup(struct inode
*dir
, struct dentry
* dentry
, unsigned int flags
)
3217 int result
= -ENOENT
;
3218 struct task_struct
*task
;
3220 struct pid_namespace
*ns
;
3222 tgid
= name_to_int(&dentry
->d_name
);
3226 ns
= dentry
->d_sb
->s_fs_info
;
3228 task
= find_task_by_pid_ns(tgid
, ns
);
3230 get_task_struct(task
);
3235 result
= proc_pid_instantiate(dir
, dentry
, task
, NULL
);
3236 put_task_struct(task
);
3238 return ERR_PTR(result
);
3242 * Find the first task with tgid >= tgid
3247 struct task_struct
*task
;
3249 static struct tgid_iter
next_tgid(struct pid_namespace
*ns
, struct tgid_iter iter
)
3254 put_task_struct(iter
.task
);
3258 pid
= find_ge_pid(iter
.tgid
, ns
);
3260 iter
.tgid
= pid_nr_ns(pid
, ns
);
3261 iter
.task
= pid_task(pid
, PIDTYPE_PID
);
3262 /* What we to know is if the pid we have find is the
3263 * pid of a thread_group_leader. Testing for task
3264 * being a thread_group_leader is the obvious thing
3265 * todo but there is a window when it fails, due to
3266 * the pid transfer logic in de_thread.
3268 * So we perform the straight forward test of seeing
3269 * if the pid we have found is the pid of a thread
3270 * group leader, and don't worry if the task we have
3271 * found doesn't happen to be a thread group leader.
3272 * As we don't care in the case of readdir.
3274 if (!iter
.task
|| !has_group_leader_pid(iter
.task
)) {
3278 get_task_struct(iter
.task
);
3284 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
3286 /* for the /proc/ directory itself, after non-process stuff has been done */
3287 int proc_pid_readdir(struct file
*file
, struct dir_context
*ctx
)
3289 struct tgid_iter iter
;
3290 struct pid_namespace
*ns
= file_inode(file
)->i_sb
->s_fs_info
;
3291 loff_t pos
= ctx
->pos
;
3293 if (pos
>= PID_MAX_LIMIT
+ TGID_OFFSET
)
3296 if (pos
== TGID_OFFSET
- 2) {
3297 struct inode
*inode
= d_inode(ns
->proc_self
);
3298 if (!dir_emit(ctx
, "self", 4, inode
->i_ino
, DT_LNK
))
3300 ctx
->pos
= pos
= pos
+ 1;
3302 if (pos
== TGID_OFFSET
- 1) {
3303 struct inode
*inode
= d_inode(ns
->proc_thread_self
);
3304 if (!dir_emit(ctx
, "thread-self", 11, inode
->i_ino
, DT_LNK
))
3306 ctx
->pos
= pos
= pos
+ 1;
3308 iter
.tgid
= pos
- TGID_OFFSET
;
3310 for (iter
= next_tgid(ns
, iter
);
3312 iter
.tgid
+= 1, iter
= next_tgid(ns
, iter
)) {
3313 char name
[PROC_NUMBUF
];
3317 if (!has_pid_permissions(ns
, iter
.task
, HIDEPID_INVISIBLE
))
3320 len
= snprintf(name
, sizeof(name
), "%d", iter
.tgid
);
3321 ctx
->pos
= iter
.tgid
+ TGID_OFFSET
;
3322 if (!proc_fill_cache(file
, ctx
, name
, len
,
3323 proc_pid_instantiate
, iter
.task
, NULL
)) {
3324 put_task_struct(iter
.task
);
3328 ctx
->pos
= PID_MAX_LIMIT
+ TGID_OFFSET
;
3333 * proc_tid_comm_permission is a special permission function exclusively
3334 * used for the node /proc/<pid>/task/<tid>/comm.
3335 * It bypasses generic permission checks in the case where a task of the same
3336 * task group attempts to access the node.
3337 * The rationale behind this is that glibc and bionic access this node for
3338 * cross thread naming (pthread_set/getname_np(!self)). However, if
3339 * PR_SET_DUMPABLE gets set to 0 this node among others becomes uid=0 gid=0,
3340 * which locks out the cross thread naming implementation.
3341 * This function makes sure that the node is always accessible for members of
3342 * same thread group.
3344 static int proc_tid_comm_permission(struct inode
*inode
, int mask
)
3346 bool is_same_tgroup
;
3347 struct task_struct
*task
;
3349 task
= get_proc_task(inode
);
3352 is_same_tgroup
= same_thread_group(current
, task
);
3353 put_task_struct(task
);
3355 if (likely(is_same_tgroup
&& !(mask
& MAY_EXEC
))) {
3356 /* This file (/proc/<pid>/task/<tid>/comm) can always be
3357 * read or written by the members of the corresponding
3363 return generic_permission(inode
, mask
);
3366 static const struct inode_operations proc_tid_comm_inode_operations
= {
3367 .permission
= proc_tid_comm_permission
,
3373 static const struct pid_entry tid_base_stuff
[] = {
3374 DIR("fd", S_IRUSR
|S_IXUSR
, proc_fd_inode_operations
, proc_fd_operations
),
3375 DIR("fdinfo", S_IRUSR
|S_IXUSR
, proc_fdinfo_inode_operations
, proc_fdinfo_operations
),
3376 DIR("ns", S_IRUSR
|S_IXUGO
, proc_ns_dir_inode_operations
, proc_ns_dir_operations
),
3378 DIR("net", S_IRUGO
|S_IXUGO
, proc_net_inode_operations
, proc_net_operations
),
3380 REG("environ", S_IRUSR
, proc_environ_operations
),
3381 REG("auxv", S_IRUSR
, proc_auxv_operations
),
3382 ONE("status", S_IRUGO
, proc_pid_status
),
3383 ONE("personality", S_IRUSR
, proc_pid_personality
),
3384 ONE("limits", S_IRUGO
, proc_pid_limits
),
3385 #ifdef CONFIG_SCHED_DEBUG
3386 REG("sched", S_IRUGO
|S_IWUSR
, proc_pid_sched_operations
),
3388 NOD("comm", S_IFREG
|S_IRUGO
|S_IWUSR
,
3389 &proc_tid_comm_inode_operations
,
3390 &proc_pid_set_comm_operations
, {}),
3391 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3392 ONE("syscall", S_IRUSR
, proc_pid_syscall
),
3394 REG("cmdline", S_IRUGO
, proc_pid_cmdline_ops
),
3395 ONE("stat", S_IRUGO
, proc_tid_stat
),
3396 ONE("statm", S_IRUGO
, proc_pid_statm
),
3397 REG("maps", S_IRUGO
, proc_tid_maps_operations
),
3398 #ifdef CONFIG_PROC_CHILDREN
3399 REG("children", S_IRUGO
, proc_tid_children_operations
),
3402 REG("numa_maps", S_IRUGO
, proc_tid_numa_maps_operations
),
3404 REG("mem", S_IRUSR
|S_IWUSR
, proc_mem_operations
),
3405 LNK("cwd", proc_cwd_link
),
3406 LNK("root", proc_root_link
),
3407 LNK("exe", proc_exe_link
),
3408 REG("mounts", S_IRUGO
, proc_mounts_operations
),
3409 REG("mountinfo", S_IRUGO
, proc_mountinfo_operations
),
3410 #ifdef CONFIG_PROC_PAGE_MONITOR
3411 REG("clear_refs", S_IWUSR
, proc_clear_refs_operations
),
3412 REG("smaps", S_IRUGO
, proc_tid_smaps_operations
),
3413 REG("pagemap", S_IRUSR
, proc_pagemap_operations
),
3415 #ifdef CONFIG_SECURITY
3416 DIR("attr", S_IRUGO
|S_IXUGO
, proc_attr_dir_inode_operations
, proc_attr_dir_operations
),
3418 #ifdef CONFIG_KALLSYMS
3419 ONE("wchan", S_IRUGO
, proc_pid_wchan
),
3421 #ifdef CONFIG_STACKTRACE
3422 ONE("stack", S_IRUSR
, proc_pid_stack
),
3424 #ifdef CONFIG_SCHED_INFO
3425 ONE("schedstat", S_IRUGO
, proc_pid_schedstat
),
3427 #ifdef CONFIG_LATENCYTOP
3428 REG("latency", S_IRUGO
, proc_lstats_operations
),
3430 #ifdef CONFIG_PROC_PID_CPUSET
3431 ONE("cpuset", S_IRUGO
, proc_cpuset_show
),
3433 #ifdef CONFIG_CGROUPS
3434 ONE("cgroup", S_IRUGO
, proc_cgroup_show
),
3436 ONE("oom_score", S_IRUGO
, proc_oom_score
),
3437 REG("oom_adj", S_IRUGO
|S_IWUSR
, proc_oom_adj_operations
),
3438 REG("oom_score_adj", S_IRUGO
|S_IWUSR
, proc_oom_score_adj_operations
),
3439 #ifdef CONFIG_AUDITSYSCALL
3440 REG("loginuid", S_IWUSR
|S_IRUGO
, proc_loginuid_operations
),
3441 REG("sessionid", S_IRUGO
, proc_sessionid_operations
),
3443 #ifdef CONFIG_FAULT_INJECTION
3444 REG("make-it-fail", S_IRUGO
|S_IWUSR
, proc_fault_inject_operations
),
3445 REG("fail-nth", 0644, proc_fail_nth_operations
),
3447 #ifdef CONFIG_TASK_IO_ACCOUNTING
3448 ONE("io", S_IRUSR
, proc_tid_io_accounting
),
3450 #ifdef CONFIG_HARDWALL
3451 ONE("hardwall", S_IRUGO
, proc_pid_hardwall
),
3453 #ifdef CONFIG_USER_NS
3454 REG("uid_map", S_IRUGO
|S_IWUSR
, proc_uid_map_operations
),
3455 REG("gid_map", S_IRUGO
|S_IWUSR
, proc_gid_map_operations
),
3456 REG("projid_map", S_IRUGO
|S_IWUSR
, proc_projid_map_operations
),
3457 REG("setgroups", S_IRUGO
|S_IWUSR
, proc_setgroups_operations
),
3459 #ifdef CONFIG_LIVEPATCH
3460 ONE("patch_state", S_IRUSR
, proc_pid_patch_state
),
3464 static int proc_tid_base_readdir(struct file
*file
, struct dir_context
*ctx
)
3466 return proc_pident_readdir(file
, ctx
,
3467 tid_base_stuff
, ARRAY_SIZE(tid_base_stuff
));
3470 static struct dentry
*proc_tid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, unsigned int flags
)
3472 return proc_pident_lookup(dir
, dentry
,
3473 tid_base_stuff
, ARRAY_SIZE(tid_base_stuff
));
3476 static const struct file_operations proc_tid_base_operations
= {
3477 .read
= generic_read_dir
,
3478 .iterate_shared
= proc_tid_base_readdir
,
3479 .llseek
= generic_file_llseek
,
3482 static const struct inode_operations proc_tid_base_inode_operations
= {
3483 .lookup
= proc_tid_base_lookup
,
3484 .getattr
= pid_getattr
,
3485 .setattr
= proc_setattr
,
3488 static int proc_task_instantiate(struct inode
*dir
,
3489 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
3491 struct inode
*inode
;
3492 inode
= proc_pid_make_inode(dir
->i_sb
, task
, S_IFDIR
| S_IRUGO
| S_IXUGO
);
3496 inode
->i_op
= &proc_tid_base_inode_operations
;
3497 inode
->i_fop
= &proc_tid_base_operations
;
3498 inode
->i_flags
|=S_IMMUTABLE
;
3500 set_nlink(inode
, nlink_tid
);
3502 d_set_d_op(dentry
, &pid_dentry_operations
);
3504 d_add(dentry
, inode
);
3505 /* Close the race of the process dying before we return the dentry */
3506 if (pid_revalidate(dentry
, 0))
3512 static struct dentry
*proc_task_lookup(struct inode
*dir
, struct dentry
* dentry
, unsigned int flags
)
3514 int result
= -ENOENT
;
3515 struct task_struct
*task
;
3516 struct task_struct
*leader
= get_proc_task(dir
);
3518 struct pid_namespace
*ns
;
3523 tid
= name_to_int(&dentry
->d_name
);
3527 ns
= dentry
->d_sb
->s_fs_info
;
3529 task
= find_task_by_pid_ns(tid
, ns
);
3531 get_task_struct(task
);
3535 if (!same_thread_group(leader
, task
))
3538 result
= proc_task_instantiate(dir
, dentry
, task
, NULL
);
3540 put_task_struct(task
);
3542 put_task_struct(leader
);
3544 return ERR_PTR(result
);
3548 * Find the first tid of a thread group to return to user space.
3550 * Usually this is just the thread group leader, but if the users
3551 * buffer was too small or there was a seek into the middle of the
3552 * directory we have more work todo.
3554 * In the case of a short read we start with find_task_by_pid.
3556 * In the case of a seek we start with the leader and walk nr
3559 static struct task_struct
*first_tid(struct pid
*pid
, int tid
, loff_t f_pos
,
3560 struct pid_namespace
*ns
)
3562 struct task_struct
*pos
, *task
;
3563 unsigned long nr
= f_pos
;
3565 if (nr
!= f_pos
) /* 32bit overflow? */
3569 task
= pid_task(pid
, PIDTYPE_PID
);
3573 /* Attempt to start with the tid of a thread */
3575 pos
= find_task_by_pid_ns(tid
, ns
);
3576 if (pos
&& same_thread_group(pos
, task
))
3580 /* If nr exceeds the number of threads there is nothing todo */
3581 if (nr
>= get_nr_threads(task
))
3584 /* If we haven't found our starting place yet start
3585 * with the leader and walk nr threads forward.
3587 pos
= task
= task
->group_leader
;
3591 } while_each_thread(task
, pos
);
3596 get_task_struct(pos
);
3603 * Find the next thread in the thread list.
3604 * Return NULL if there is an error or no next thread.
3606 * The reference to the input task_struct is released.
3608 static struct task_struct
*next_tid(struct task_struct
*start
)
3610 struct task_struct
*pos
= NULL
;
3612 if (pid_alive(start
)) {
3613 pos
= next_thread(start
);
3614 if (thread_group_leader(pos
))
3617 get_task_struct(pos
);
3620 put_task_struct(start
);
3624 /* for the /proc/TGID/task/ directories */
3625 static int proc_task_readdir(struct file
*file
, struct dir_context
*ctx
)
3627 struct inode
*inode
= file_inode(file
);
3628 struct task_struct
*task
;
3629 struct pid_namespace
*ns
;
3632 if (proc_inode_is_dead(inode
))
3635 if (!dir_emit_dots(file
, ctx
))
3638 /* f_version caches the tgid value that the last readdir call couldn't
3639 * return. lseek aka telldir automagically resets f_version to 0.
3641 ns
= inode
->i_sb
->s_fs_info
;
3642 tid
= (int)file
->f_version
;
3643 file
->f_version
= 0;
3644 for (task
= first_tid(proc_pid(inode
), tid
, ctx
->pos
- 2, ns
);
3646 task
= next_tid(task
), ctx
->pos
++) {
3647 char name
[PROC_NUMBUF
];
3649 tid
= task_pid_nr_ns(task
, ns
);
3650 len
= snprintf(name
, sizeof(name
), "%d", tid
);
3651 if (!proc_fill_cache(file
, ctx
, name
, len
,
3652 proc_task_instantiate
, task
, NULL
)) {
3653 /* returning this tgid failed, save it as the first
3654 * pid for the next readir call */
3655 file
->f_version
= (u64
)tid
;
3656 put_task_struct(task
);
3664 static int proc_task_getattr(const struct path
*path
, struct kstat
*stat
,
3665 u32 request_mask
, unsigned int query_flags
)
3667 struct inode
*inode
= d_inode(path
->dentry
);
3668 struct task_struct
*p
= get_proc_task(inode
);
3669 generic_fillattr(inode
, stat
);
3672 stat
->nlink
+= get_nr_threads(p
);
3679 static const struct inode_operations proc_task_inode_operations
= {
3680 .lookup
= proc_task_lookup
,
3681 .getattr
= proc_task_getattr
,
3682 .setattr
= proc_setattr
,
3683 .permission
= proc_pid_permission
,
3686 static const struct file_operations proc_task_operations
= {
3687 .read
= generic_read_dir
,
3688 .iterate_shared
= proc_task_readdir
,
3689 .llseek
= generic_file_llseek
,
3692 void __init
set_proc_pid_nlink(void)
3694 nlink_tid
= pid_entry_nlink(tid_base_stuff
, ARRAY_SIZE(tid_base_stuff
));
3695 nlink_tgid
= pid_entry_nlink(tgid_base_stuff
, ARRAY_SIZE(tgid_base_stuff
));