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/cache.h>
79 #include <linux/cgroup.h>
80 #include <linux/cpuset.h>
81 #include <linux/audit.h>
82 #include <linux/poll.h>
83 #include <linux/nsproxy.h>
84 #include <linux/oom.h>
85 #include <linux/elf.h>
86 #include <linux/pid_namespace.h>
87 #include <linux/user_namespace.h>
88 #include <linux/fs_struct.h>
89 #include <linux/slab.h>
90 #include <linux/sched/autogroup.h>
91 #include <linux/sched/mm.h>
92 #include <linux/sched/coredump.h>
93 #include <linux/sched/debug.h>
94 #include <linux/sched/stat.h>
95 #include <linux/flex_array.h>
96 #include <linux/posix-timers.h>
97 #include <trace/events/oom.h>
101 #include "../../lib/kstrtox.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.
113 static u8 nlink_tid __ro_after_init
;
114 static u8 nlink_tgid __ro_after_init
;
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 } )
148 * Count the number of hardlinks for the pid_entry table, excluding the .
151 static unsigned int __init
pid_entry_nlink(const struct pid_entry
*entries
,
158 for (i
= 0; i
< n
; ++i
) {
159 if (S_ISDIR(entries
[i
].mode
))
166 static int get_task_root(struct task_struct
*task
, struct path
*root
)
168 int result
= -ENOENT
;
172 get_fs_root(task
->fs
, root
);
179 static int proc_cwd_link(struct dentry
*dentry
, struct path
*path
)
181 struct task_struct
*task
= get_proc_task(d_inode(dentry
));
182 int result
= -ENOENT
;
187 get_fs_pwd(task
->fs
, path
);
191 put_task_struct(task
);
196 static int proc_root_link(struct dentry
*dentry
, struct path
*path
)
198 struct task_struct
*task
= get_proc_task(d_inode(dentry
));
199 int result
= -ENOENT
;
202 result
= get_task_root(task
, path
);
203 put_task_struct(task
);
208 static ssize_t
proc_pid_cmdline_read(struct file
*file
, char __user
*buf
,
209 size_t _count
, loff_t
*pos
)
211 struct task_struct
*tsk
;
212 struct mm_struct
*mm
;
214 unsigned long count
= _count
;
215 unsigned long arg_start
, arg_end
, env_start
, env_end
;
216 unsigned long len1
, len2
, len
;
218 char __user
*buf0
= buf
;
224 tsk
= get_proc_task(file_inode(file
));
227 mm
= get_task_mm(tsk
);
228 put_task_struct(tsk
);
231 /* Check if process spawned far enough to have cmdline. */
237 page
= (char *)__get_free_page(GFP_KERNEL
);
243 spin_lock(&mm
->arg_lock
);
244 arg_start
= mm
->arg_start
;
245 arg_end
= mm
->arg_end
;
246 env_start
= mm
->env_start
;
247 env_end
= mm
->env_end
;
248 spin_unlock(&mm
->arg_lock
);
250 BUG_ON(arg_start
> arg_end
);
251 BUG_ON(env_start
> env_end
);
253 len1
= arg_end
- arg_start
;
254 len2
= env_end
- env_start
;
261 * Inherently racy -- command line shares address space
262 * with code and data.
264 if (access_remote_vm(mm
, arg_end
- 1, &c
, 1, FOLL_ANON
) != 1)
268 /* Command line (set of strings) occupies whole ARGV. */
272 p
= arg_start
+ *pos
;
274 while (count
> 0 && len
> 0) {
275 unsigned int nr_read
;
277 nr_read
= min3(count
, len
, PAGE_SIZE
);
278 nr_read
= access_remote_vm(mm
, p
, page
, nr_read
, FOLL_ANON
);
282 if (copy_to_user(buf
, page
, nr_read
)) {
294 * Command line (1 string) occupies ARGV and
301 { .p
= arg_start
, .len
= len1
},
302 { .p
= env_start
, .len
= len2
},
308 while (i
< 2 && pos1
>= cmdline
[i
].len
) {
309 pos1
-= cmdline
[i
].len
;
313 p
= cmdline
[i
].p
+ pos1
;
314 len
= cmdline
[i
].len
- pos1
;
315 while (count
> 0 && len
> 0) {
316 unsigned int nr_read
, nr_write
;
318 nr_read
= min3(count
, len
, PAGE_SIZE
);
319 nr_read
= access_remote_vm(mm
, p
, page
, nr_read
, FOLL_ANON
);
324 * Command line can be shorter than whole ARGV
325 * even if last "marker" byte says it is not.
327 nr_write
= strnlen(page
, nr_read
);
329 if (copy_to_user(buf
, page
, nr_write
)) {
339 if (nr_write
< nr_read
)
343 /* Only first chunk can be read partially. */
353 free_page((unsigned long)page
);
359 static const struct file_operations proc_pid_cmdline_ops
= {
360 .read
= proc_pid_cmdline_read
,
361 .llseek
= generic_file_llseek
,
364 #ifdef CONFIG_KALLSYMS
366 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
367 * Returns the resolved symbol. If that fails, simply return the address.
369 static int proc_pid_wchan(struct seq_file
*m
, struct pid_namespace
*ns
,
370 struct pid
*pid
, struct task_struct
*task
)
373 char symname
[KSYM_NAME_LEN
];
375 if (!ptrace_may_access(task
, PTRACE_MODE_READ_FSCREDS
))
378 wchan
= get_wchan(task
);
379 if (wchan
&& !lookup_symbol_name(wchan
, symname
)) {
380 seq_puts(m
, symname
);
388 #endif /* CONFIG_KALLSYMS */
390 static int lock_trace(struct task_struct
*task
)
392 int err
= mutex_lock_killable(&task
->signal
->cred_guard_mutex
);
395 if (!ptrace_may_access(task
, PTRACE_MODE_ATTACH_FSCREDS
)) {
396 mutex_unlock(&task
->signal
->cred_guard_mutex
);
402 static void unlock_trace(struct task_struct
*task
)
404 mutex_unlock(&task
->signal
->cred_guard_mutex
);
407 #ifdef CONFIG_STACKTRACE
409 #define MAX_STACK_TRACE_DEPTH 64
411 static int proc_pid_stack(struct seq_file
*m
, struct pid_namespace
*ns
,
412 struct pid
*pid
, struct task_struct
*task
)
414 struct stack_trace trace
;
415 unsigned long *entries
;
419 entries
= kmalloc(MAX_STACK_TRACE_DEPTH
* sizeof(*entries
), GFP_KERNEL
);
423 trace
.nr_entries
= 0;
424 trace
.max_entries
= MAX_STACK_TRACE_DEPTH
;
425 trace
.entries
= entries
;
428 err
= lock_trace(task
);
430 save_stack_trace_tsk(task
, &trace
);
432 for (i
= 0; i
< trace
.nr_entries
; i
++) {
433 seq_printf(m
, "[<0>] %pB\n", (void *)entries
[i
]);
443 #ifdef CONFIG_SCHED_INFO
445 * Provides /proc/PID/schedstat
447 static int proc_pid_schedstat(struct seq_file
*m
, struct pid_namespace
*ns
,
448 struct pid
*pid
, struct task_struct
*task
)
450 if (unlikely(!sched_info_on()))
451 seq_printf(m
, "0 0 0\n");
453 seq_printf(m
, "%llu %llu %lu\n",
454 (unsigned long long)task
->se
.sum_exec_runtime
,
455 (unsigned long long)task
->sched_info
.run_delay
,
456 task
->sched_info
.pcount
);
462 #ifdef CONFIG_LATENCYTOP
463 static int lstats_show_proc(struct seq_file
*m
, void *v
)
466 struct inode
*inode
= m
->private;
467 struct task_struct
*task
= get_proc_task(inode
);
471 seq_puts(m
, "Latency Top version : v0.1\n");
472 for (i
= 0; i
< 32; i
++) {
473 struct latency_record
*lr
= &task
->latency_record
[i
];
474 if (lr
->backtrace
[0]) {
476 seq_printf(m
, "%i %li %li",
477 lr
->count
, lr
->time
, lr
->max
);
478 for (q
= 0; q
< LT_BACKTRACEDEPTH
; q
++) {
479 unsigned long bt
= lr
->backtrace
[q
];
484 seq_printf(m
, " %ps", (void *)bt
);
490 put_task_struct(task
);
494 static int lstats_open(struct inode
*inode
, struct file
*file
)
496 return single_open(file
, lstats_show_proc
, inode
);
499 static ssize_t
lstats_write(struct file
*file
, const char __user
*buf
,
500 size_t count
, loff_t
*offs
)
502 struct task_struct
*task
= get_proc_task(file_inode(file
));
506 clear_all_latency_tracing(task
);
507 put_task_struct(task
);
512 static const struct file_operations proc_lstats_operations
= {
515 .write
= lstats_write
,
517 .release
= single_release
,
522 static int proc_oom_score(struct seq_file
*m
, struct pid_namespace
*ns
,
523 struct pid
*pid
, struct task_struct
*task
)
525 unsigned long totalpages
= totalram_pages
+ total_swap_pages
;
526 unsigned long points
= 0;
528 points
= oom_badness(task
, NULL
, NULL
, totalpages
) *
530 seq_printf(m
, "%lu\n", points
);
540 static const struct limit_names lnames
[RLIM_NLIMITS
] = {
541 [RLIMIT_CPU
] = {"Max cpu time", "seconds"},
542 [RLIMIT_FSIZE
] = {"Max file size", "bytes"},
543 [RLIMIT_DATA
] = {"Max data size", "bytes"},
544 [RLIMIT_STACK
] = {"Max stack size", "bytes"},
545 [RLIMIT_CORE
] = {"Max core file size", "bytes"},
546 [RLIMIT_RSS
] = {"Max resident set", "bytes"},
547 [RLIMIT_NPROC
] = {"Max processes", "processes"},
548 [RLIMIT_NOFILE
] = {"Max open files", "files"},
549 [RLIMIT_MEMLOCK
] = {"Max locked memory", "bytes"},
550 [RLIMIT_AS
] = {"Max address space", "bytes"},
551 [RLIMIT_LOCKS
] = {"Max file locks", "locks"},
552 [RLIMIT_SIGPENDING
] = {"Max pending signals", "signals"},
553 [RLIMIT_MSGQUEUE
] = {"Max msgqueue size", "bytes"},
554 [RLIMIT_NICE
] = {"Max nice priority", NULL
},
555 [RLIMIT_RTPRIO
] = {"Max realtime priority", NULL
},
556 [RLIMIT_RTTIME
] = {"Max realtime timeout", "us"},
559 /* Display limits for a process */
560 static int proc_pid_limits(struct seq_file
*m
, struct pid_namespace
*ns
,
561 struct pid
*pid
, struct task_struct
*task
)
566 struct rlimit rlim
[RLIM_NLIMITS
];
568 if (!lock_task_sighand(task
, &flags
))
570 memcpy(rlim
, task
->signal
->rlim
, sizeof(struct rlimit
) * RLIM_NLIMITS
);
571 unlock_task_sighand(task
, &flags
);
574 * print the file header
576 seq_printf(m
, "%-25s %-20s %-20s %-10s\n",
577 "Limit", "Soft Limit", "Hard Limit", "Units");
579 for (i
= 0; i
< RLIM_NLIMITS
; i
++) {
580 if (rlim
[i
].rlim_cur
== RLIM_INFINITY
)
581 seq_printf(m
, "%-25s %-20s ",
582 lnames
[i
].name
, "unlimited");
584 seq_printf(m
, "%-25s %-20lu ",
585 lnames
[i
].name
, rlim
[i
].rlim_cur
);
587 if (rlim
[i
].rlim_max
== RLIM_INFINITY
)
588 seq_printf(m
, "%-20s ", "unlimited");
590 seq_printf(m
, "%-20lu ", rlim
[i
].rlim_max
);
593 seq_printf(m
, "%-10s\n", lnames
[i
].unit
);
601 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
602 static int proc_pid_syscall(struct seq_file
*m
, struct pid_namespace
*ns
,
603 struct pid
*pid
, struct task_struct
*task
)
606 unsigned long args
[6], sp
, pc
;
609 res
= lock_trace(task
);
613 if (task_current_syscall(task
, &nr
, args
, 6, &sp
, &pc
))
614 seq_puts(m
, "running\n");
616 seq_printf(m
, "%ld 0x%lx 0x%lx\n", nr
, sp
, pc
);
619 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
621 args
[0], args
[1], args
[2], args
[3], args
[4], args
[5],
627 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
629 /************************************************************************/
630 /* Here the fs part begins */
631 /************************************************************************/
633 /* permission checks */
634 static int proc_fd_access_allowed(struct inode
*inode
)
636 struct task_struct
*task
;
638 /* Allow access to a task's file descriptors if it is us or we
639 * may use ptrace attach to the process and find out that
642 task
= get_proc_task(inode
);
644 allowed
= ptrace_may_access(task
, PTRACE_MODE_READ_FSCREDS
);
645 put_task_struct(task
);
650 int proc_setattr(struct dentry
*dentry
, struct iattr
*attr
)
653 struct inode
*inode
= d_inode(dentry
);
655 if (attr
->ia_valid
& ATTR_MODE
)
658 error
= setattr_prepare(dentry
, attr
);
662 setattr_copy(inode
, attr
);
663 mark_inode_dirty(inode
);
668 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
669 * or euid/egid (for hide_pid_min=2)?
671 static bool has_pid_permissions(struct pid_namespace
*pid
,
672 struct task_struct
*task
,
675 if (pid
->hide_pid
< hide_pid_min
)
677 if (in_group_p(pid
->pid_gid
))
679 return ptrace_may_access(task
, PTRACE_MODE_READ_FSCREDS
);
683 static int proc_pid_permission(struct inode
*inode
, int mask
)
685 struct pid_namespace
*pid
= proc_pid_ns(inode
);
686 struct task_struct
*task
;
689 task
= get_proc_task(inode
);
692 has_perms
= has_pid_permissions(pid
, task
, HIDEPID_NO_ACCESS
);
693 put_task_struct(task
);
696 if (pid
->hide_pid
== HIDEPID_INVISIBLE
) {
698 * Let's make getdents(), stat(), and open()
699 * consistent with each other. If a process
700 * may not stat() a file, it shouldn't be seen
708 return generic_permission(inode
, mask
);
713 static const struct inode_operations proc_def_inode_operations
= {
714 .setattr
= proc_setattr
,
717 static int proc_single_show(struct seq_file
*m
, void *v
)
719 struct inode
*inode
= m
->private;
720 struct pid_namespace
*ns
= proc_pid_ns(inode
);
721 struct pid
*pid
= proc_pid(inode
);
722 struct task_struct
*task
;
725 task
= get_pid_task(pid
, PIDTYPE_PID
);
729 ret
= PROC_I(inode
)->op
.proc_show(m
, ns
, pid
, task
);
731 put_task_struct(task
);
735 static int proc_single_open(struct inode
*inode
, struct file
*filp
)
737 return single_open(filp
, proc_single_show
, inode
);
740 static const struct file_operations proc_single_file_operations
= {
741 .open
= proc_single_open
,
744 .release
= single_release
,
748 struct mm_struct
*proc_mem_open(struct inode
*inode
, unsigned int mode
)
750 struct task_struct
*task
= get_proc_task(inode
);
751 struct mm_struct
*mm
= ERR_PTR(-ESRCH
);
754 mm
= mm_access(task
, mode
| PTRACE_MODE_FSCREDS
);
755 put_task_struct(task
);
757 if (!IS_ERR_OR_NULL(mm
)) {
758 /* ensure this mm_struct can't be freed */
760 /* but do not pin its memory */
768 static int __mem_open(struct inode
*inode
, struct file
*file
, unsigned int mode
)
770 struct mm_struct
*mm
= proc_mem_open(inode
, mode
);
775 file
->private_data
= mm
;
779 static int mem_open(struct inode
*inode
, struct file
*file
)
781 int ret
= __mem_open(inode
, file
, PTRACE_MODE_ATTACH
);
783 /* OK to pass negative loff_t, we can catch out-of-range */
784 file
->f_mode
|= FMODE_UNSIGNED_OFFSET
;
789 static ssize_t
mem_rw(struct file
*file
, char __user
*buf
,
790 size_t count
, loff_t
*ppos
, int write
)
792 struct mm_struct
*mm
= file
->private_data
;
793 unsigned long addr
= *ppos
;
801 page
= (char *)__get_free_page(GFP_KERNEL
);
806 if (!mmget_not_zero(mm
))
809 flags
= FOLL_FORCE
| (write
? FOLL_WRITE
: 0);
812 int this_len
= min_t(int, count
, PAGE_SIZE
);
814 if (write
&& copy_from_user(page
, buf
, this_len
)) {
819 this_len
= access_remote_vm(mm
, addr
, page
, this_len
, flags
);
826 if (!write
&& copy_to_user(buf
, page
, this_len
)) {
840 free_page((unsigned long) page
);
844 static ssize_t
mem_read(struct file
*file
, char __user
*buf
,
845 size_t count
, loff_t
*ppos
)
847 return mem_rw(file
, buf
, count
, ppos
, 0);
850 static ssize_t
mem_write(struct file
*file
, const char __user
*buf
,
851 size_t count
, loff_t
*ppos
)
853 return mem_rw(file
, (char __user
*)buf
, count
, ppos
, 1);
856 loff_t
mem_lseek(struct file
*file
, loff_t offset
, int orig
)
860 file
->f_pos
= offset
;
863 file
->f_pos
+= offset
;
868 force_successful_syscall_return();
872 static int mem_release(struct inode
*inode
, struct file
*file
)
874 struct mm_struct
*mm
= file
->private_data
;
880 static const struct file_operations proc_mem_operations
= {
885 .release
= mem_release
,
888 static int environ_open(struct inode
*inode
, struct file
*file
)
890 return __mem_open(inode
, file
, PTRACE_MODE_READ
);
893 static ssize_t
environ_read(struct file
*file
, char __user
*buf
,
894 size_t count
, loff_t
*ppos
)
897 unsigned long src
= *ppos
;
899 struct mm_struct
*mm
= file
->private_data
;
900 unsigned long env_start
, env_end
;
902 /* Ensure the process spawned far enough to have an environment. */
903 if (!mm
|| !mm
->env_end
)
906 page
= (char *)__get_free_page(GFP_KERNEL
);
911 if (!mmget_not_zero(mm
))
914 spin_lock(&mm
->arg_lock
);
915 env_start
= mm
->env_start
;
916 env_end
= mm
->env_end
;
917 spin_unlock(&mm
->arg_lock
);
920 size_t this_len
, max_len
;
923 if (src
>= (env_end
- env_start
))
926 this_len
= env_end
- (env_start
+ src
);
928 max_len
= min_t(size_t, PAGE_SIZE
, count
);
929 this_len
= min(max_len
, this_len
);
931 retval
= access_remote_vm(mm
, (env_start
+ src
), page
, this_len
, FOLL_ANON
);
938 if (copy_to_user(buf
, page
, retval
)) {
952 free_page((unsigned long) page
);
956 static const struct file_operations proc_environ_operations
= {
957 .open
= environ_open
,
958 .read
= environ_read
,
959 .llseek
= generic_file_llseek
,
960 .release
= mem_release
,
963 static int auxv_open(struct inode
*inode
, struct file
*file
)
965 return __mem_open(inode
, file
, PTRACE_MODE_READ_FSCREDS
);
968 static ssize_t
auxv_read(struct file
*file
, char __user
*buf
,
969 size_t count
, loff_t
*ppos
)
971 struct mm_struct
*mm
= file
->private_data
;
972 unsigned int nwords
= 0;
978 } while (mm
->saved_auxv
[nwords
- 2] != 0); /* AT_NULL */
979 return simple_read_from_buffer(buf
, count
, ppos
, mm
->saved_auxv
,
980 nwords
* sizeof(mm
->saved_auxv
[0]));
983 static const struct file_operations proc_auxv_operations
= {
986 .llseek
= generic_file_llseek
,
987 .release
= mem_release
,
990 static ssize_t
oom_adj_read(struct file
*file
, char __user
*buf
, size_t count
,
993 struct task_struct
*task
= get_proc_task(file_inode(file
));
994 char buffer
[PROC_NUMBUF
];
995 int oom_adj
= OOM_ADJUST_MIN
;
1000 if (task
->signal
->oom_score_adj
== OOM_SCORE_ADJ_MAX
)
1001 oom_adj
= OOM_ADJUST_MAX
;
1003 oom_adj
= (task
->signal
->oom_score_adj
* -OOM_DISABLE
) /
1005 put_task_struct(task
);
1006 len
= snprintf(buffer
, sizeof(buffer
), "%d\n", oom_adj
);
1007 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
1010 static int __set_oom_adj(struct file
*file
, int oom_adj
, bool legacy
)
1012 static DEFINE_MUTEX(oom_adj_mutex
);
1013 struct mm_struct
*mm
= NULL
;
1014 struct task_struct
*task
;
1017 task
= get_proc_task(file_inode(file
));
1021 mutex_lock(&oom_adj_mutex
);
1023 if (oom_adj
< task
->signal
->oom_score_adj
&&
1024 !capable(CAP_SYS_RESOURCE
)) {
1029 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
1030 * /proc/pid/oom_score_adj instead.
1032 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
1033 current
->comm
, task_pid_nr(current
), task_pid_nr(task
),
1036 if ((short)oom_adj
< task
->signal
->oom_score_adj_min
&&
1037 !capable(CAP_SYS_RESOURCE
)) {
1044 * Make sure we will check other processes sharing the mm if this is
1045 * not vfrok which wants its own oom_score_adj.
1046 * pin the mm so it doesn't go away and get reused after task_unlock
1048 if (!task
->vfork_done
) {
1049 struct task_struct
*p
= find_lock_task_mm(task
);
1052 if (atomic_read(&p
->mm
->mm_users
) > 1) {
1060 task
->signal
->oom_score_adj
= oom_adj
;
1061 if (!legacy
&& has_capability_noaudit(current
, CAP_SYS_RESOURCE
))
1062 task
->signal
->oom_score_adj_min
= (short)oom_adj
;
1063 trace_oom_score_adj_update(task
);
1066 struct task_struct
*p
;
1069 for_each_process(p
) {
1070 if (same_thread_group(task
, p
))
1073 /* do not touch kernel threads or the global init */
1074 if (p
->flags
& PF_KTHREAD
|| is_global_init(p
))
1078 if (!p
->vfork_done
&& process_shares_mm(p
, mm
)) {
1079 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",
1080 task_pid_nr(p
), p
->comm
,
1081 p
->signal
->oom_score_adj
, oom_adj
,
1082 task_pid_nr(task
), task
->comm
);
1083 p
->signal
->oom_score_adj
= oom_adj
;
1084 if (!legacy
&& has_capability_noaudit(current
, CAP_SYS_RESOURCE
))
1085 p
->signal
->oom_score_adj_min
= (short)oom_adj
;
1093 mutex_unlock(&oom_adj_mutex
);
1094 put_task_struct(task
);
1099 * /proc/pid/oom_adj exists solely for backwards compatibility with previous
1100 * kernels. The effective policy is defined by oom_score_adj, which has a
1101 * different scale: oom_adj grew exponentially and oom_score_adj grows linearly.
1102 * Values written to oom_adj are simply mapped linearly to oom_score_adj.
1103 * Processes that become oom disabled via oom_adj will still be oom disabled
1104 * with this implementation.
1106 * oom_adj cannot be removed since existing userspace binaries use it.
1108 static ssize_t
oom_adj_write(struct file
*file
, const char __user
*buf
,
1109 size_t count
, loff_t
*ppos
)
1111 char buffer
[PROC_NUMBUF
];
1115 memset(buffer
, 0, sizeof(buffer
));
1116 if (count
> sizeof(buffer
) - 1)
1117 count
= sizeof(buffer
) - 1;
1118 if (copy_from_user(buffer
, buf
, count
)) {
1123 err
= kstrtoint(strstrip(buffer
), 0, &oom_adj
);
1126 if ((oom_adj
< OOM_ADJUST_MIN
|| oom_adj
> OOM_ADJUST_MAX
) &&
1127 oom_adj
!= OOM_DISABLE
) {
1133 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1134 * value is always attainable.
1136 if (oom_adj
== OOM_ADJUST_MAX
)
1137 oom_adj
= OOM_SCORE_ADJ_MAX
;
1139 oom_adj
= (oom_adj
* OOM_SCORE_ADJ_MAX
) / -OOM_DISABLE
;
1141 err
= __set_oom_adj(file
, oom_adj
, true);
1143 return err
< 0 ? err
: count
;
1146 static const struct file_operations proc_oom_adj_operations
= {
1147 .read
= oom_adj_read
,
1148 .write
= oom_adj_write
,
1149 .llseek
= generic_file_llseek
,
1152 static ssize_t
oom_score_adj_read(struct file
*file
, char __user
*buf
,
1153 size_t count
, loff_t
*ppos
)
1155 struct task_struct
*task
= get_proc_task(file_inode(file
));
1156 char buffer
[PROC_NUMBUF
];
1157 short oom_score_adj
= OOM_SCORE_ADJ_MIN
;
1162 oom_score_adj
= task
->signal
->oom_score_adj
;
1163 put_task_struct(task
);
1164 len
= snprintf(buffer
, sizeof(buffer
), "%hd\n", oom_score_adj
);
1165 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
1168 static ssize_t
oom_score_adj_write(struct file
*file
, const char __user
*buf
,
1169 size_t count
, loff_t
*ppos
)
1171 char buffer
[PROC_NUMBUF
];
1175 memset(buffer
, 0, sizeof(buffer
));
1176 if (count
> sizeof(buffer
) - 1)
1177 count
= sizeof(buffer
) - 1;
1178 if (copy_from_user(buffer
, buf
, count
)) {
1183 err
= kstrtoint(strstrip(buffer
), 0, &oom_score_adj
);
1186 if (oom_score_adj
< OOM_SCORE_ADJ_MIN
||
1187 oom_score_adj
> OOM_SCORE_ADJ_MAX
) {
1192 err
= __set_oom_adj(file
, oom_score_adj
, false);
1194 return err
< 0 ? err
: count
;
1197 static const struct file_operations proc_oom_score_adj_operations
= {
1198 .read
= oom_score_adj_read
,
1199 .write
= oom_score_adj_write
,
1200 .llseek
= default_llseek
,
1203 #ifdef CONFIG_AUDITSYSCALL
1204 #define TMPBUFLEN 11
1205 static ssize_t
proc_loginuid_read(struct file
* file
, char __user
* buf
,
1206 size_t count
, loff_t
*ppos
)
1208 struct inode
* inode
= file_inode(file
);
1209 struct task_struct
*task
= get_proc_task(inode
);
1211 char tmpbuf
[TMPBUFLEN
];
1215 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
1216 from_kuid(file
->f_cred
->user_ns
,
1217 audit_get_loginuid(task
)));
1218 put_task_struct(task
);
1219 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1222 static ssize_t
proc_loginuid_write(struct file
* file
, const char __user
* buf
,
1223 size_t count
, loff_t
*ppos
)
1225 struct inode
* inode
= file_inode(file
);
1231 if (current
!= pid_task(proc_pid(inode
), PIDTYPE_PID
)) {
1238 /* No partial writes. */
1242 rv
= kstrtou32_from_user(buf
, count
, 10, &loginuid
);
1246 /* is userspace tring to explicitly UNSET the loginuid? */
1247 if (loginuid
== AUDIT_UID_UNSET
) {
1248 kloginuid
= INVALID_UID
;
1250 kloginuid
= make_kuid(file
->f_cred
->user_ns
, loginuid
);
1251 if (!uid_valid(kloginuid
))
1255 rv
= audit_set_loginuid(kloginuid
);
1261 static const struct file_operations proc_loginuid_operations
= {
1262 .read
= proc_loginuid_read
,
1263 .write
= proc_loginuid_write
,
1264 .llseek
= generic_file_llseek
,
1267 static ssize_t
proc_sessionid_read(struct file
* file
, char __user
* buf
,
1268 size_t count
, loff_t
*ppos
)
1270 struct inode
* inode
= file_inode(file
);
1271 struct task_struct
*task
= get_proc_task(inode
);
1273 char tmpbuf
[TMPBUFLEN
];
1277 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
1278 audit_get_sessionid(task
));
1279 put_task_struct(task
);
1280 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1283 static const struct file_operations proc_sessionid_operations
= {
1284 .read
= proc_sessionid_read
,
1285 .llseek
= generic_file_llseek
,
1289 #ifdef CONFIG_FAULT_INJECTION
1290 static ssize_t
proc_fault_inject_read(struct file
* file
, char __user
* buf
,
1291 size_t count
, loff_t
*ppos
)
1293 struct task_struct
*task
= get_proc_task(file_inode(file
));
1294 char buffer
[PROC_NUMBUF
];
1300 make_it_fail
= task
->make_it_fail
;
1301 put_task_struct(task
);
1303 len
= snprintf(buffer
, sizeof(buffer
), "%i\n", make_it_fail
);
1305 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
1308 static ssize_t
proc_fault_inject_write(struct file
* file
,
1309 const char __user
* buf
, size_t count
, loff_t
*ppos
)
1311 struct task_struct
*task
;
1312 char buffer
[PROC_NUMBUF
];
1316 if (!capable(CAP_SYS_RESOURCE
))
1318 memset(buffer
, 0, sizeof(buffer
));
1319 if (count
> sizeof(buffer
) - 1)
1320 count
= sizeof(buffer
) - 1;
1321 if (copy_from_user(buffer
, buf
, count
))
1323 rv
= kstrtoint(strstrip(buffer
), 0, &make_it_fail
);
1326 if (make_it_fail
< 0 || make_it_fail
> 1)
1329 task
= get_proc_task(file_inode(file
));
1332 task
->make_it_fail
= make_it_fail
;
1333 put_task_struct(task
);
1338 static const struct file_operations proc_fault_inject_operations
= {
1339 .read
= proc_fault_inject_read
,
1340 .write
= proc_fault_inject_write
,
1341 .llseek
= generic_file_llseek
,
1344 static ssize_t
proc_fail_nth_write(struct file
*file
, const char __user
*buf
,
1345 size_t count
, loff_t
*ppos
)
1347 struct task_struct
*task
;
1351 err
= kstrtouint_from_user(buf
, count
, 0, &n
);
1355 task
= get_proc_task(file_inode(file
));
1359 put_task_struct(task
);
1364 static ssize_t
proc_fail_nth_read(struct file
*file
, char __user
*buf
,
1365 size_t count
, loff_t
*ppos
)
1367 struct task_struct
*task
;
1368 char numbuf
[PROC_NUMBUF
];
1371 task
= get_proc_task(file_inode(file
));
1374 len
= snprintf(numbuf
, sizeof(numbuf
), "%u\n", task
->fail_nth
);
1375 len
= simple_read_from_buffer(buf
, count
, ppos
, numbuf
, len
);
1376 put_task_struct(task
);
1381 static const struct file_operations proc_fail_nth_operations
= {
1382 .read
= proc_fail_nth_read
,
1383 .write
= proc_fail_nth_write
,
1388 #ifdef CONFIG_SCHED_DEBUG
1390 * Print out various scheduling related per-task fields:
1392 static int sched_show(struct seq_file
*m
, void *v
)
1394 struct inode
*inode
= m
->private;
1395 struct pid_namespace
*ns
= proc_pid_ns(inode
);
1396 struct task_struct
*p
;
1398 p
= get_proc_task(inode
);
1401 proc_sched_show_task(p
, ns
, m
);
1409 sched_write(struct file
*file
, const char __user
*buf
,
1410 size_t count
, loff_t
*offset
)
1412 struct inode
*inode
= file_inode(file
);
1413 struct task_struct
*p
;
1415 p
= get_proc_task(inode
);
1418 proc_sched_set_task(p
);
1425 static int sched_open(struct inode
*inode
, struct file
*filp
)
1427 return single_open(filp
, sched_show
, inode
);
1430 static const struct file_operations proc_pid_sched_operations
= {
1433 .write
= sched_write
,
1434 .llseek
= seq_lseek
,
1435 .release
= single_release
,
1440 #ifdef CONFIG_SCHED_AUTOGROUP
1442 * Print out autogroup related information:
1444 static int sched_autogroup_show(struct seq_file
*m
, void *v
)
1446 struct inode
*inode
= m
->private;
1447 struct task_struct
*p
;
1449 p
= get_proc_task(inode
);
1452 proc_sched_autogroup_show_task(p
, m
);
1460 sched_autogroup_write(struct file
*file
, const char __user
*buf
,
1461 size_t count
, loff_t
*offset
)
1463 struct inode
*inode
= file_inode(file
);
1464 struct task_struct
*p
;
1465 char buffer
[PROC_NUMBUF
];
1469 memset(buffer
, 0, sizeof(buffer
));
1470 if (count
> sizeof(buffer
) - 1)
1471 count
= sizeof(buffer
) - 1;
1472 if (copy_from_user(buffer
, buf
, count
))
1475 err
= kstrtoint(strstrip(buffer
), 0, &nice
);
1479 p
= get_proc_task(inode
);
1483 err
= proc_sched_autogroup_set_nice(p
, nice
);
1492 static int sched_autogroup_open(struct inode
*inode
, struct file
*filp
)
1496 ret
= single_open(filp
, sched_autogroup_show
, NULL
);
1498 struct seq_file
*m
= filp
->private_data
;
1505 static const struct file_operations proc_pid_sched_autogroup_operations
= {
1506 .open
= sched_autogroup_open
,
1508 .write
= sched_autogroup_write
,
1509 .llseek
= seq_lseek
,
1510 .release
= single_release
,
1513 #endif /* CONFIG_SCHED_AUTOGROUP */
1515 static ssize_t
comm_write(struct file
*file
, const char __user
*buf
,
1516 size_t count
, loff_t
*offset
)
1518 struct inode
*inode
= file_inode(file
);
1519 struct task_struct
*p
;
1520 char buffer
[TASK_COMM_LEN
];
1521 const size_t maxlen
= sizeof(buffer
) - 1;
1523 memset(buffer
, 0, sizeof(buffer
));
1524 if (copy_from_user(buffer
, buf
, count
> maxlen
? maxlen
: count
))
1527 p
= get_proc_task(inode
);
1531 if (same_thread_group(current
, p
))
1532 set_task_comm(p
, buffer
);
1541 static int comm_show(struct seq_file
*m
, void *v
)
1543 struct inode
*inode
= m
->private;
1544 struct task_struct
*p
;
1546 p
= get_proc_task(inode
);
1550 proc_task_name(m
, p
, false);
1558 static int comm_open(struct inode
*inode
, struct file
*filp
)
1560 return single_open(filp
, comm_show
, inode
);
1563 static const struct file_operations proc_pid_set_comm_operations
= {
1566 .write
= comm_write
,
1567 .llseek
= seq_lseek
,
1568 .release
= single_release
,
1571 static int proc_exe_link(struct dentry
*dentry
, struct path
*exe_path
)
1573 struct task_struct
*task
;
1574 struct file
*exe_file
;
1576 task
= get_proc_task(d_inode(dentry
));
1579 exe_file
= get_task_exe_file(task
);
1580 put_task_struct(task
);
1582 *exe_path
= exe_file
->f_path
;
1583 path_get(&exe_file
->f_path
);
1590 static const char *proc_pid_get_link(struct dentry
*dentry
,
1591 struct inode
*inode
,
1592 struct delayed_call
*done
)
1595 int error
= -EACCES
;
1598 return ERR_PTR(-ECHILD
);
1600 /* Are we allowed to snoop on the tasks file descriptors? */
1601 if (!proc_fd_access_allowed(inode
))
1604 error
= PROC_I(inode
)->op
.proc_get_link(dentry
, &path
);
1608 nd_jump_link(&path
);
1611 return ERR_PTR(error
);
1614 static int do_proc_readlink(struct path
*path
, char __user
*buffer
, int buflen
)
1616 char *tmp
= (char *)__get_free_page(GFP_KERNEL
);
1623 pathname
= d_path(path
, tmp
, PAGE_SIZE
);
1624 len
= PTR_ERR(pathname
);
1625 if (IS_ERR(pathname
))
1627 len
= tmp
+ PAGE_SIZE
- 1 - pathname
;
1631 if (copy_to_user(buffer
, pathname
, len
))
1634 free_page((unsigned long)tmp
);
1638 static int proc_pid_readlink(struct dentry
* dentry
, char __user
* buffer
, int buflen
)
1640 int error
= -EACCES
;
1641 struct inode
*inode
= d_inode(dentry
);
1644 /* Are we allowed to snoop on the tasks file descriptors? */
1645 if (!proc_fd_access_allowed(inode
))
1648 error
= PROC_I(inode
)->op
.proc_get_link(dentry
, &path
);
1652 error
= do_proc_readlink(&path
, buffer
, buflen
);
1658 const struct inode_operations proc_pid_link_inode_operations
= {
1659 .readlink
= proc_pid_readlink
,
1660 .get_link
= proc_pid_get_link
,
1661 .setattr
= proc_setattr
,
1665 /* building an inode */
1667 void task_dump_owner(struct task_struct
*task
, umode_t mode
,
1668 kuid_t
*ruid
, kgid_t
*rgid
)
1670 /* Depending on the state of dumpable compute who should own a
1671 * proc file for a task.
1673 const struct cred
*cred
;
1677 if (unlikely(task
->flags
& PF_KTHREAD
)) {
1678 *ruid
= GLOBAL_ROOT_UID
;
1679 *rgid
= GLOBAL_ROOT_GID
;
1683 /* Default to the tasks effective ownership */
1685 cred
= __task_cred(task
);
1691 * Before the /proc/pid/status file was created the only way to read
1692 * the effective uid of a /process was to stat /proc/pid. Reading
1693 * /proc/pid/status is slow enough that procps and other packages
1694 * kept stating /proc/pid. To keep the rules in /proc simple I have
1695 * made this apply to all per process world readable and executable
1698 if (mode
!= (S_IFDIR
|S_IRUGO
|S_IXUGO
)) {
1699 struct mm_struct
*mm
;
1702 /* Make non-dumpable tasks owned by some root */
1704 if (get_dumpable(mm
) != SUID_DUMP_USER
) {
1705 struct user_namespace
*user_ns
= mm
->user_ns
;
1707 uid
= make_kuid(user_ns
, 0);
1708 if (!uid_valid(uid
))
1709 uid
= GLOBAL_ROOT_UID
;
1711 gid
= make_kgid(user_ns
, 0);
1712 if (!gid_valid(gid
))
1713 gid
= GLOBAL_ROOT_GID
;
1716 uid
= GLOBAL_ROOT_UID
;
1717 gid
= GLOBAL_ROOT_GID
;
1725 struct inode
*proc_pid_make_inode(struct super_block
* sb
,
1726 struct task_struct
*task
, umode_t mode
)
1728 struct inode
* inode
;
1729 struct proc_inode
*ei
;
1731 /* We need a new inode */
1733 inode
= new_inode(sb
);
1739 inode
->i_mode
= mode
;
1740 inode
->i_ino
= get_next_ino();
1741 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= current_time(inode
);
1742 inode
->i_op
= &proc_def_inode_operations
;
1745 * grab the reference to task.
1747 ei
->pid
= get_task_pid(task
, PIDTYPE_PID
);
1751 task_dump_owner(task
, 0, &inode
->i_uid
, &inode
->i_gid
);
1752 security_task_to_inode(task
, inode
);
1762 int pid_getattr(const struct path
*path
, struct kstat
*stat
,
1763 u32 request_mask
, unsigned int query_flags
)
1765 struct inode
*inode
= d_inode(path
->dentry
);
1766 struct pid_namespace
*pid
= proc_pid_ns(inode
);
1767 struct task_struct
*task
;
1769 generic_fillattr(inode
, stat
);
1772 stat
->uid
= GLOBAL_ROOT_UID
;
1773 stat
->gid
= GLOBAL_ROOT_GID
;
1774 task
= pid_task(proc_pid(inode
), PIDTYPE_PID
);
1776 if (!has_pid_permissions(pid
, task
, HIDEPID_INVISIBLE
)) {
1779 * This doesn't prevent learning whether PID exists,
1780 * it only makes getattr() consistent with readdir().
1784 task_dump_owner(task
, inode
->i_mode
, &stat
->uid
, &stat
->gid
);
1793 * Set <pid>/... inode ownership (can change due to setuid(), etc.)
1795 void pid_update_inode(struct task_struct
*task
, struct inode
*inode
)
1797 task_dump_owner(task
, inode
->i_mode
, &inode
->i_uid
, &inode
->i_gid
);
1799 inode
->i_mode
&= ~(S_ISUID
| S_ISGID
);
1800 security_task_to_inode(task
, inode
);
1804 * Rewrite the inode's ownerships here because the owning task may have
1805 * performed a setuid(), etc.
1808 static int pid_revalidate(struct dentry
*dentry
, unsigned int flags
)
1810 struct inode
*inode
;
1811 struct task_struct
*task
;
1813 if (flags
& LOOKUP_RCU
)
1816 inode
= d_inode(dentry
);
1817 task
= get_proc_task(inode
);
1820 pid_update_inode(task
, inode
);
1821 put_task_struct(task
);
1827 static inline bool proc_inode_is_dead(struct inode
*inode
)
1829 return !proc_pid(inode
)->tasks
[PIDTYPE_PID
].first
;
1832 int pid_delete_dentry(const struct dentry
*dentry
)
1834 /* Is the task we represent dead?
1835 * If so, then don't put the dentry on the lru list,
1836 * kill it immediately.
1838 return proc_inode_is_dead(d_inode(dentry
));
1841 const struct dentry_operations pid_dentry_operations
=
1843 .d_revalidate
= pid_revalidate
,
1844 .d_delete
= pid_delete_dentry
,
1850 * Fill a directory entry.
1852 * If possible create the dcache entry and derive our inode number and
1853 * file type from dcache entry.
1855 * Since all of the proc inode numbers are dynamically generated, the inode
1856 * numbers do not exist until the inode is cache. This means creating the
1857 * the dcache entry in readdir is necessary to keep the inode numbers
1858 * reported by readdir in sync with the inode numbers reported
1861 bool proc_fill_cache(struct file
*file
, struct dir_context
*ctx
,
1862 const char *name
, int len
,
1863 instantiate_t instantiate
, struct task_struct
*task
, const void *ptr
)
1865 struct dentry
*child
, *dir
= file
->f_path
.dentry
;
1866 struct qstr qname
= QSTR_INIT(name
, len
);
1867 struct inode
*inode
;
1868 unsigned type
= DT_UNKNOWN
;
1871 child
= d_hash_and_lookup(dir
, &qname
);
1873 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq
);
1874 child
= d_alloc_parallel(dir
, &qname
, &wq
);
1876 goto end_instantiate
;
1877 if (d_in_lookup(child
)) {
1879 res
= instantiate(child
, task
, ptr
);
1880 d_lookup_done(child
);
1882 goto end_instantiate
;
1883 if (unlikely(res
)) {
1889 inode
= d_inode(child
);
1891 type
= inode
->i_mode
>> 12;
1894 return dir_emit(ctx
, name
, len
, ino
, type
);
1898 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1899 * which represent vma start and end addresses.
1901 static int dname_to_vma_addr(struct dentry
*dentry
,
1902 unsigned long *start
, unsigned long *end
)
1904 const char *str
= dentry
->d_name
.name
;
1905 unsigned long long sval
, eval
;
1908 if (str
[0] == '0' && str
[1] != '-')
1910 len
= _parse_integer(str
, 16, &sval
);
1911 if (len
& KSTRTOX_OVERFLOW
)
1913 if (sval
!= (unsigned long)sval
)
1921 if (str
[0] == '0' && str
[1])
1923 len
= _parse_integer(str
, 16, &eval
);
1924 if (len
& KSTRTOX_OVERFLOW
)
1926 if (eval
!= (unsigned long)eval
)
1939 static int map_files_d_revalidate(struct dentry
*dentry
, unsigned int flags
)
1941 unsigned long vm_start
, vm_end
;
1942 bool exact_vma_exists
= false;
1943 struct mm_struct
*mm
= NULL
;
1944 struct task_struct
*task
;
1945 struct inode
*inode
;
1948 if (flags
& LOOKUP_RCU
)
1951 inode
= d_inode(dentry
);
1952 task
= get_proc_task(inode
);
1956 mm
= mm_access(task
, PTRACE_MODE_READ_FSCREDS
);
1957 if (IS_ERR_OR_NULL(mm
))
1960 if (!dname_to_vma_addr(dentry
, &vm_start
, &vm_end
)) {
1961 down_read(&mm
->mmap_sem
);
1962 exact_vma_exists
= !!find_exact_vma(mm
, vm_start
, vm_end
);
1963 up_read(&mm
->mmap_sem
);
1968 if (exact_vma_exists
) {
1969 task_dump_owner(task
, 0, &inode
->i_uid
, &inode
->i_gid
);
1971 security_task_to_inode(task
, inode
);
1976 put_task_struct(task
);
1982 static const struct dentry_operations tid_map_files_dentry_operations
= {
1983 .d_revalidate
= map_files_d_revalidate
,
1984 .d_delete
= pid_delete_dentry
,
1987 static int map_files_get_link(struct dentry
*dentry
, struct path
*path
)
1989 unsigned long vm_start
, vm_end
;
1990 struct vm_area_struct
*vma
;
1991 struct task_struct
*task
;
1992 struct mm_struct
*mm
;
1996 task
= get_proc_task(d_inode(dentry
));
2000 mm
= get_task_mm(task
);
2001 put_task_struct(task
);
2005 rc
= dname_to_vma_addr(dentry
, &vm_start
, &vm_end
);
2010 down_read(&mm
->mmap_sem
);
2011 vma
= find_exact_vma(mm
, vm_start
, vm_end
);
2012 if (vma
&& vma
->vm_file
) {
2013 *path
= vma
->vm_file
->f_path
;
2017 up_read(&mm
->mmap_sem
);
2025 struct map_files_info
{
2026 unsigned long start
;
2032 * Only allow CAP_SYS_ADMIN to follow the links, due to concerns about how the
2033 * symlinks may be used to bypass permissions on ancestor directories in the
2034 * path to the file in question.
2037 proc_map_files_get_link(struct dentry
*dentry
,
2038 struct inode
*inode
,
2039 struct delayed_call
*done
)
2041 if (!capable(CAP_SYS_ADMIN
))
2042 return ERR_PTR(-EPERM
);
2044 return proc_pid_get_link(dentry
, inode
, done
);
2048 * Identical to proc_pid_link_inode_operations except for get_link()
2050 static const struct inode_operations proc_map_files_link_inode_operations
= {
2051 .readlink
= proc_pid_readlink
,
2052 .get_link
= proc_map_files_get_link
,
2053 .setattr
= proc_setattr
,
2056 static struct dentry
*
2057 proc_map_files_instantiate(struct dentry
*dentry
,
2058 struct task_struct
*task
, const void *ptr
)
2060 fmode_t mode
= (fmode_t
)(unsigned long)ptr
;
2061 struct proc_inode
*ei
;
2062 struct inode
*inode
;
2064 inode
= proc_pid_make_inode(dentry
->d_sb
, task
, S_IFLNK
|
2065 ((mode
& FMODE_READ
) ? S_IRUSR
: 0) |
2066 ((mode
& FMODE_WRITE
) ? S_IWUSR
: 0));
2068 return ERR_PTR(-ENOENT
);
2071 ei
->op
.proc_get_link
= map_files_get_link
;
2073 inode
->i_op
= &proc_map_files_link_inode_operations
;
2076 d_set_d_op(dentry
, &tid_map_files_dentry_operations
);
2077 return d_splice_alias(inode
, dentry
);
2080 static struct dentry
*proc_map_files_lookup(struct inode
*dir
,
2081 struct dentry
*dentry
, unsigned int flags
)
2083 unsigned long vm_start
, vm_end
;
2084 struct vm_area_struct
*vma
;
2085 struct task_struct
*task
;
2086 struct dentry
*result
;
2087 struct mm_struct
*mm
;
2089 result
= ERR_PTR(-ENOENT
);
2090 task
= get_proc_task(dir
);
2094 result
= ERR_PTR(-EACCES
);
2095 if (!ptrace_may_access(task
, PTRACE_MODE_READ_FSCREDS
))
2098 result
= ERR_PTR(-ENOENT
);
2099 if (dname_to_vma_addr(dentry
, &vm_start
, &vm_end
))
2102 mm
= get_task_mm(task
);
2106 down_read(&mm
->mmap_sem
);
2107 vma
= find_exact_vma(mm
, vm_start
, vm_end
);
2112 result
= proc_map_files_instantiate(dentry
, task
,
2113 (void *)(unsigned long)vma
->vm_file
->f_mode
);
2116 up_read(&mm
->mmap_sem
);
2119 put_task_struct(task
);
2124 static const struct inode_operations proc_map_files_inode_operations
= {
2125 .lookup
= proc_map_files_lookup
,
2126 .permission
= proc_fd_permission
,
2127 .setattr
= proc_setattr
,
2131 proc_map_files_readdir(struct file
*file
, struct dir_context
*ctx
)
2133 struct vm_area_struct
*vma
;
2134 struct task_struct
*task
;
2135 struct mm_struct
*mm
;
2136 unsigned long nr_files
, pos
, i
;
2137 struct flex_array
*fa
= NULL
;
2138 struct map_files_info info
;
2139 struct map_files_info
*p
;
2143 task
= get_proc_task(file_inode(file
));
2148 if (!ptrace_may_access(task
, PTRACE_MODE_READ_FSCREDS
))
2152 if (!dir_emit_dots(file
, ctx
))
2155 mm
= get_task_mm(task
);
2158 down_read(&mm
->mmap_sem
);
2163 * We need two passes here:
2165 * 1) Collect vmas of mapped files with mmap_sem taken
2166 * 2) Release mmap_sem and instantiate entries
2168 * otherwise we get lockdep complained, since filldir()
2169 * routine might require mmap_sem taken in might_fault().
2172 for (vma
= mm
->mmap
, pos
= 2; vma
; vma
= vma
->vm_next
) {
2173 if (vma
->vm_file
&& ++pos
> ctx
->pos
)
2178 fa
= flex_array_alloc(sizeof(info
), nr_files
,
2180 if (!fa
|| flex_array_prealloc(fa
, 0, nr_files
,
2184 flex_array_free(fa
);
2185 up_read(&mm
->mmap_sem
);
2189 for (i
= 0, vma
= mm
->mmap
, pos
= 2; vma
;
2190 vma
= vma
->vm_next
) {
2193 if (++pos
<= ctx
->pos
)
2196 info
.start
= vma
->vm_start
;
2197 info
.end
= vma
->vm_end
;
2198 info
.mode
= vma
->vm_file
->f_mode
;
2199 if (flex_array_put(fa
, i
++, &info
, GFP_KERNEL
))
2203 up_read(&mm
->mmap_sem
);
2206 for (i
= 0; i
< nr_files
; i
++) {
2207 char buf
[4 * sizeof(long) + 2]; /* max: %lx-%lx\0 */
2210 p
= flex_array_get(fa
, i
);
2211 len
= snprintf(buf
, sizeof(buf
), "%lx-%lx", p
->start
, p
->end
);
2212 if (!proc_fill_cache(file
, ctx
,
2214 proc_map_files_instantiate
,
2216 (void *)(unsigned long)p
->mode
))
2221 flex_array_free(fa
);
2224 put_task_struct(task
);
2229 static const struct file_operations proc_map_files_operations
= {
2230 .read
= generic_read_dir
,
2231 .iterate_shared
= proc_map_files_readdir
,
2232 .llseek
= generic_file_llseek
,
2235 #if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
2236 struct timers_private
{
2238 struct task_struct
*task
;
2239 struct sighand_struct
*sighand
;
2240 struct pid_namespace
*ns
;
2241 unsigned long flags
;
2244 static void *timers_start(struct seq_file
*m
, loff_t
*pos
)
2246 struct timers_private
*tp
= m
->private;
2248 tp
->task
= get_pid_task(tp
->pid
, PIDTYPE_PID
);
2250 return ERR_PTR(-ESRCH
);
2252 tp
->sighand
= lock_task_sighand(tp
->task
, &tp
->flags
);
2254 return ERR_PTR(-ESRCH
);
2256 return seq_list_start(&tp
->task
->signal
->posix_timers
, *pos
);
2259 static void *timers_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
2261 struct timers_private
*tp
= m
->private;
2262 return seq_list_next(v
, &tp
->task
->signal
->posix_timers
, pos
);
2265 static void timers_stop(struct seq_file
*m
, void *v
)
2267 struct timers_private
*tp
= m
->private;
2270 unlock_task_sighand(tp
->task
, &tp
->flags
);
2275 put_task_struct(tp
->task
);
2280 static int show_timer(struct seq_file
*m
, void *v
)
2282 struct k_itimer
*timer
;
2283 struct timers_private
*tp
= m
->private;
2285 static const char * const nstr
[] = {
2286 [SIGEV_SIGNAL
] = "signal",
2287 [SIGEV_NONE
] = "none",
2288 [SIGEV_THREAD
] = "thread",
2291 timer
= list_entry((struct list_head
*)v
, struct k_itimer
, list
);
2292 notify
= timer
->it_sigev_notify
;
2294 seq_printf(m
, "ID: %d\n", timer
->it_id
);
2295 seq_printf(m
, "signal: %d/%px\n",
2296 timer
->sigq
->info
.si_signo
,
2297 timer
->sigq
->info
.si_value
.sival_ptr
);
2298 seq_printf(m
, "notify: %s/%s.%d\n",
2299 nstr
[notify
& ~SIGEV_THREAD_ID
],
2300 (notify
& SIGEV_THREAD_ID
) ? "tid" : "pid",
2301 pid_nr_ns(timer
->it_pid
, tp
->ns
));
2302 seq_printf(m
, "ClockID: %d\n", timer
->it_clock
);
2307 static const struct seq_operations proc_timers_seq_ops
= {
2308 .start
= timers_start
,
2309 .next
= timers_next
,
2310 .stop
= timers_stop
,
2314 static int proc_timers_open(struct inode
*inode
, struct file
*file
)
2316 struct timers_private
*tp
;
2318 tp
= __seq_open_private(file
, &proc_timers_seq_ops
,
2319 sizeof(struct timers_private
));
2323 tp
->pid
= proc_pid(inode
);
2324 tp
->ns
= proc_pid_ns(inode
);
2328 static const struct file_operations proc_timers_operations
= {
2329 .open
= proc_timers_open
,
2331 .llseek
= seq_lseek
,
2332 .release
= seq_release_private
,
2336 static ssize_t
timerslack_ns_write(struct file
*file
, const char __user
*buf
,
2337 size_t count
, loff_t
*offset
)
2339 struct inode
*inode
= file_inode(file
);
2340 struct task_struct
*p
;
2344 err
= kstrtoull_from_user(buf
, count
, 10, &slack_ns
);
2348 p
= get_proc_task(inode
);
2353 if (!capable(CAP_SYS_NICE
)) {
2358 err
= security_task_setscheduler(p
);
2367 p
->timer_slack_ns
= p
->default_timer_slack_ns
;
2369 p
->timer_slack_ns
= slack_ns
;
2378 static int timerslack_ns_show(struct seq_file
*m
, void *v
)
2380 struct inode
*inode
= m
->private;
2381 struct task_struct
*p
;
2384 p
= get_proc_task(inode
);
2390 if (!capable(CAP_SYS_NICE
)) {
2394 err
= security_task_getscheduler(p
);
2400 seq_printf(m
, "%llu\n", p
->timer_slack_ns
);
2409 static int timerslack_ns_open(struct inode
*inode
, struct file
*filp
)
2411 return single_open(filp
, timerslack_ns_show
, inode
);
2414 static const struct file_operations proc_pid_set_timerslack_ns_operations
= {
2415 .open
= timerslack_ns_open
,
2417 .write
= timerslack_ns_write
,
2418 .llseek
= seq_lseek
,
2419 .release
= single_release
,
2422 static struct dentry
*proc_pident_instantiate(struct dentry
*dentry
,
2423 struct task_struct
*task
, const void *ptr
)
2425 const struct pid_entry
*p
= ptr
;
2426 struct inode
*inode
;
2427 struct proc_inode
*ei
;
2429 inode
= proc_pid_make_inode(dentry
->d_sb
, task
, p
->mode
);
2431 return ERR_PTR(-ENOENT
);
2434 if (S_ISDIR(inode
->i_mode
))
2435 set_nlink(inode
, 2); /* Use getattr to fix if necessary */
2437 inode
->i_op
= p
->iop
;
2439 inode
->i_fop
= p
->fop
;
2441 pid_update_inode(task
, inode
);
2442 d_set_d_op(dentry
, &pid_dentry_operations
);
2443 return d_splice_alias(inode
, dentry
);
2446 static struct dentry
*proc_pident_lookup(struct inode
*dir
,
2447 struct dentry
*dentry
,
2448 const struct pid_entry
*ents
,
2451 struct task_struct
*task
= get_proc_task(dir
);
2452 const struct pid_entry
*p
, *last
;
2453 struct dentry
*res
= ERR_PTR(-ENOENT
);
2459 * Yes, it does not scale. And it should not. Don't add
2460 * new entries into /proc/<tgid>/ without very good reasons.
2462 last
= &ents
[nents
];
2463 for (p
= ents
; p
< last
; p
++) {
2464 if (p
->len
!= dentry
->d_name
.len
)
2466 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
))
2472 res
= proc_pident_instantiate(dentry
, task
, p
);
2474 put_task_struct(task
);
2479 static int proc_pident_readdir(struct file
*file
, struct dir_context
*ctx
,
2480 const struct pid_entry
*ents
, unsigned int nents
)
2482 struct task_struct
*task
= get_proc_task(file_inode(file
));
2483 const struct pid_entry
*p
;
2488 if (!dir_emit_dots(file
, ctx
))
2491 if (ctx
->pos
>= nents
+ 2)
2494 for (p
= ents
+ (ctx
->pos
- 2); p
< ents
+ nents
; p
++) {
2495 if (!proc_fill_cache(file
, ctx
, p
->name
, p
->len
,
2496 proc_pident_instantiate
, task
, p
))
2501 put_task_struct(task
);
2505 #ifdef CONFIG_SECURITY
2506 static ssize_t
proc_pid_attr_read(struct file
* file
, char __user
* buf
,
2507 size_t count
, loff_t
*ppos
)
2509 struct inode
* inode
= file_inode(file
);
2512 struct task_struct
*task
= get_proc_task(inode
);
2517 length
= security_getprocattr(task
,
2518 (char*)file
->f_path
.dentry
->d_name
.name
,
2520 put_task_struct(task
);
2522 length
= simple_read_from_buffer(buf
, count
, ppos
, p
, length
);
2527 static ssize_t
proc_pid_attr_write(struct file
* file
, const char __user
* buf
,
2528 size_t count
, loff_t
*ppos
)
2530 struct inode
* inode
= file_inode(file
);
2533 struct task_struct
*task
= get_proc_task(inode
);
2539 /* A task may only write its own attributes. */
2541 if (current
!= task
)
2544 if (count
> PAGE_SIZE
)
2547 /* No partial writes. */
2552 page
= memdup_user(buf
, count
);
2554 length
= PTR_ERR(page
);
2558 /* Guard against adverse ptrace interaction */
2559 length
= mutex_lock_interruptible(¤t
->signal
->cred_guard_mutex
);
2563 length
= security_setprocattr(file
->f_path
.dentry
->d_name
.name
,
2565 mutex_unlock(¤t
->signal
->cred_guard_mutex
);
2569 put_task_struct(task
);
2574 static const struct file_operations proc_pid_attr_operations
= {
2575 .read
= proc_pid_attr_read
,
2576 .write
= proc_pid_attr_write
,
2577 .llseek
= generic_file_llseek
,
2580 static const struct pid_entry attr_dir_stuff
[] = {
2581 REG("current", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2582 REG("prev", S_IRUGO
, proc_pid_attr_operations
),
2583 REG("exec", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2584 REG("fscreate", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2585 REG("keycreate", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2586 REG("sockcreate", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2589 static int proc_attr_dir_readdir(struct file
*file
, struct dir_context
*ctx
)
2591 return proc_pident_readdir(file
, ctx
,
2592 attr_dir_stuff
, ARRAY_SIZE(attr_dir_stuff
));
2595 static const struct file_operations proc_attr_dir_operations
= {
2596 .read
= generic_read_dir
,
2597 .iterate_shared
= proc_attr_dir_readdir
,
2598 .llseek
= generic_file_llseek
,
2601 static struct dentry
*proc_attr_dir_lookup(struct inode
*dir
,
2602 struct dentry
*dentry
, unsigned int flags
)
2604 return proc_pident_lookup(dir
, dentry
,
2605 attr_dir_stuff
, ARRAY_SIZE(attr_dir_stuff
));
2608 static const struct inode_operations proc_attr_dir_inode_operations
= {
2609 .lookup
= proc_attr_dir_lookup
,
2610 .getattr
= pid_getattr
,
2611 .setattr
= proc_setattr
,
2616 #ifdef CONFIG_ELF_CORE
2617 static ssize_t
proc_coredump_filter_read(struct file
*file
, char __user
*buf
,
2618 size_t count
, loff_t
*ppos
)
2620 struct task_struct
*task
= get_proc_task(file_inode(file
));
2621 struct mm_struct
*mm
;
2622 char buffer
[PROC_NUMBUF
];
2630 mm
= get_task_mm(task
);
2632 len
= snprintf(buffer
, sizeof(buffer
), "%08lx\n",
2633 ((mm
->flags
& MMF_DUMP_FILTER_MASK
) >>
2634 MMF_DUMP_FILTER_SHIFT
));
2636 ret
= simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
2639 put_task_struct(task
);
2644 static ssize_t
proc_coredump_filter_write(struct file
*file
,
2645 const char __user
*buf
,
2649 struct task_struct
*task
;
2650 struct mm_struct
*mm
;
2656 ret
= kstrtouint_from_user(buf
, count
, 0, &val
);
2661 task
= get_proc_task(file_inode(file
));
2665 mm
= get_task_mm(task
);
2670 for (i
= 0, mask
= 1; i
< MMF_DUMP_FILTER_BITS
; i
++, mask
<<= 1) {
2672 set_bit(i
+ MMF_DUMP_FILTER_SHIFT
, &mm
->flags
);
2674 clear_bit(i
+ MMF_DUMP_FILTER_SHIFT
, &mm
->flags
);
2679 put_task_struct(task
);
2686 static const struct file_operations proc_coredump_filter_operations
= {
2687 .read
= proc_coredump_filter_read
,
2688 .write
= proc_coredump_filter_write
,
2689 .llseek
= generic_file_llseek
,
2693 #ifdef CONFIG_TASK_IO_ACCOUNTING
2694 static int do_io_accounting(struct task_struct
*task
, struct seq_file
*m
, int whole
)
2696 struct task_io_accounting acct
= task
->ioac
;
2697 unsigned long flags
;
2700 result
= mutex_lock_killable(&task
->signal
->cred_guard_mutex
);
2704 if (!ptrace_may_access(task
, PTRACE_MODE_READ_FSCREDS
)) {
2709 if (whole
&& lock_task_sighand(task
, &flags
)) {
2710 struct task_struct
*t
= task
;
2712 task_io_accounting_add(&acct
, &task
->signal
->ioac
);
2713 while_each_thread(task
, t
)
2714 task_io_accounting_add(&acct
, &t
->ioac
);
2716 unlock_task_sighand(task
, &flags
);
2723 "read_bytes: %llu\n"
2724 "write_bytes: %llu\n"
2725 "cancelled_write_bytes: %llu\n",
2726 (unsigned long long)acct
.rchar
,
2727 (unsigned long long)acct
.wchar
,
2728 (unsigned long long)acct
.syscr
,
2729 (unsigned long long)acct
.syscw
,
2730 (unsigned long long)acct
.read_bytes
,
2731 (unsigned long long)acct
.write_bytes
,
2732 (unsigned long long)acct
.cancelled_write_bytes
);
2736 mutex_unlock(&task
->signal
->cred_guard_mutex
);
2740 static int proc_tid_io_accounting(struct seq_file
*m
, struct pid_namespace
*ns
,
2741 struct pid
*pid
, struct task_struct
*task
)
2743 return do_io_accounting(task
, m
, 0);
2746 static int proc_tgid_io_accounting(struct seq_file
*m
, struct pid_namespace
*ns
,
2747 struct pid
*pid
, struct task_struct
*task
)
2749 return do_io_accounting(task
, m
, 1);
2751 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2753 #ifdef CONFIG_USER_NS
2754 static int proc_id_map_open(struct inode
*inode
, struct file
*file
,
2755 const struct seq_operations
*seq_ops
)
2757 struct user_namespace
*ns
= NULL
;
2758 struct task_struct
*task
;
2759 struct seq_file
*seq
;
2762 task
= get_proc_task(inode
);
2765 ns
= get_user_ns(task_cred_xxx(task
, user_ns
));
2767 put_task_struct(task
);
2772 ret
= seq_open(file
, seq_ops
);
2776 seq
= file
->private_data
;
2786 static int proc_id_map_release(struct inode
*inode
, struct file
*file
)
2788 struct seq_file
*seq
= file
->private_data
;
2789 struct user_namespace
*ns
= seq
->private;
2791 return seq_release(inode
, file
);
2794 static int proc_uid_map_open(struct inode
*inode
, struct file
*file
)
2796 return proc_id_map_open(inode
, file
, &proc_uid_seq_operations
);
2799 static int proc_gid_map_open(struct inode
*inode
, struct file
*file
)
2801 return proc_id_map_open(inode
, file
, &proc_gid_seq_operations
);
2804 static int proc_projid_map_open(struct inode
*inode
, struct file
*file
)
2806 return proc_id_map_open(inode
, file
, &proc_projid_seq_operations
);
2809 static const struct file_operations proc_uid_map_operations
= {
2810 .open
= proc_uid_map_open
,
2811 .write
= proc_uid_map_write
,
2813 .llseek
= seq_lseek
,
2814 .release
= proc_id_map_release
,
2817 static const struct file_operations proc_gid_map_operations
= {
2818 .open
= proc_gid_map_open
,
2819 .write
= proc_gid_map_write
,
2821 .llseek
= seq_lseek
,
2822 .release
= proc_id_map_release
,
2825 static const struct file_operations proc_projid_map_operations
= {
2826 .open
= proc_projid_map_open
,
2827 .write
= proc_projid_map_write
,
2829 .llseek
= seq_lseek
,
2830 .release
= proc_id_map_release
,
2833 static int proc_setgroups_open(struct inode
*inode
, struct file
*file
)
2835 struct user_namespace
*ns
= NULL
;
2836 struct task_struct
*task
;
2840 task
= get_proc_task(inode
);
2843 ns
= get_user_ns(task_cred_xxx(task
, user_ns
));
2845 put_task_struct(task
);
2850 if (file
->f_mode
& FMODE_WRITE
) {
2852 if (!ns_capable(ns
, CAP_SYS_ADMIN
))
2856 ret
= single_open(file
, &proc_setgroups_show
, ns
);
2867 static int proc_setgroups_release(struct inode
*inode
, struct file
*file
)
2869 struct seq_file
*seq
= file
->private_data
;
2870 struct user_namespace
*ns
= seq
->private;
2871 int ret
= single_release(inode
, file
);
2876 static const struct file_operations proc_setgroups_operations
= {
2877 .open
= proc_setgroups_open
,
2878 .write
= proc_setgroups_write
,
2880 .llseek
= seq_lseek
,
2881 .release
= proc_setgroups_release
,
2883 #endif /* CONFIG_USER_NS */
2885 static int proc_pid_personality(struct seq_file
*m
, struct pid_namespace
*ns
,
2886 struct pid
*pid
, struct task_struct
*task
)
2888 int err
= lock_trace(task
);
2890 seq_printf(m
, "%08x\n", task
->personality
);
2896 #ifdef CONFIG_LIVEPATCH
2897 static int proc_pid_patch_state(struct seq_file
*m
, struct pid_namespace
*ns
,
2898 struct pid
*pid
, struct task_struct
*task
)
2900 seq_printf(m
, "%d\n", task
->patch_state
);
2903 #endif /* CONFIG_LIVEPATCH */
2908 static const struct file_operations proc_task_operations
;
2909 static const struct inode_operations proc_task_inode_operations
;
2911 static const struct pid_entry tgid_base_stuff
[] = {
2912 DIR("task", S_IRUGO
|S_IXUGO
, proc_task_inode_operations
, proc_task_operations
),
2913 DIR("fd", S_IRUSR
|S_IXUSR
, proc_fd_inode_operations
, proc_fd_operations
),
2914 DIR("map_files", S_IRUSR
|S_IXUSR
, proc_map_files_inode_operations
, proc_map_files_operations
),
2915 DIR("fdinfo", S_IRUSR
|S_IXUSR
, proc_fdinfo_inode_operations
, proc_fdinfo_operations
),
2916 DIR("ns", S_IRUSR
|S_IXUGO
, proc_ns_dir_inode_operations
, proc_ns_dir_operations
),
2918 DIR("net", S_IRUGO
|S_IXUGO
, proc_net_inode_operations
, proc_net_operations
),
2920 REG("environ", S_IRUSR
, proc_environ_operations
),
2921 REG("auxv", S_IRUSR
, proc_auxv_operations
),
2922 ONE("status", S_IRUGO
, proc_pid_status
),
2923 ONE("personality", S_IRUSR
, proc_pid_personality
),
2924 ONE("limits", S_IRUGO
, proc_pid_limits
),
2925 #ifdef CONFIG_SCHED_DEBUG
2926 REG("sched", S_IRUGO
|S_IWUSR
, proc_pid_sched_operations
),
2928 #ifdef CONFIG_SCHED_AUTOGROUP
2929 REG("autogroup", S_IRUGO
|S_IWUSR
, proc_pid_sched_autogroup_operations
),
2931 REG("comm", S_IRUGO
|S_IWUSR
, proc_pid_set_comm_operations
),
2932 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2933 ONE("syscall", S_IRUSR
, proc_pid_syscall
),
2935 REG("cmdline", S_IRUGO
, proc_pid_cmdline_ops
),
2936 ONE("stat", S_IRUGO
, proc_tgid_stat
),
2937 ONE("statm", S_IRUGO
, proc_pid_statm
),
2938 REG("maps", S_IRUGO
, proc_pid_maps_operations
),
2940 REG("numa_maps", S_IRUGO
, proc_pid_numa_maps_operations
),
2942 REG("mem", S_IRUSR
|S_IWUSR
, proc_mem_operations
),
2943 LNK("cwd", proc_cwd_link
),
2944 LNK("root", proc_root_link
),
2945 LNK("exe", proc_exe_link
),
2946 REG("mounts", S_IRUGO
, proc_mounts_operations
),
2947 REG("mountinfo", S_IRUGO
, proc_mountinfo_operations
),
2948 REG("mountstats", S_IRUSR
, proc_mountstats_operations
),
2949 #ifdef CONFIG_PROC_PAGE_MONITOR
2950 REG("clear_refs", S_IWUSR
, proc_clear_refs_operations
),
2951 REG("smaps", S_IRUGO
, proc_pid_smaps_operations
),
2952 REG("smaps_rollup", S_IRUGO
, proc_pid_smaps_rollup_operations
),
2953 REG("pagemap", S_IRUSR
, proc_pagemap_operations
),
2955 #ifdef CONFIG_SECURITY
2956 DIR("attr", S_IRUGO
|S_IXUGO
, proc_attr_dir_inode_operations
, proc_attr_dir_operations
),
2958 #ifdef CONFIG_KALLSYMS
2959 ONE("wchan", S_IRUGO
, proc_pid_wchan
),
2961 #ifdef CONFIG_STACKTRACE
2962 ONE("stack", S_IRUSR
, proc_pid_stack
),
2964 #ifdef CONFIG_SCHED_INFO
2965 ONE("schedstat", S_IRUGO
, proc_pid_schedstat
),
2967 #ifdef CONFIG_LATENCYTOP
2968 REG("latency", S_IRUGO
, proc_lstats_operations
),
2970 #ifdef CONFIG_PROC_PID_CPUSET
2971 ONE("cpuset", S_IRUGO
, proc_cpuset_show
),
2973 #ifdef CONFIG_CGROUPS
2974 ONE("cgroup", S_IRUGO
, proc_cgroup_show
),
2976 ONE("oom_score", S_IRUGO
, proc_oom_score
),
2977 REG("oom_adj", S_IRUGO
|S_IWUSR
, proc_oom_adj_operations
),
2978 REG("oom_score_adj", S_IRUGO
|S_IWUSR
, proc_oom_score_adj_operations
),
2979 #ifdef CONFIG_AUDITSYSCALL
2980 REG("loginuid", S_IWUSR
|S_IRUGO
, proc_loginuid_operations
),
2981 REG("sessionid", S_IRUGO
, proc_sessionid_operations
),
2983 #ifdef CONFIG_FAULT_INJECTION
2984 REG("make-it-fail", S_IRUGO
|S_IWUSR
, proc_fault_inject_operations
),
2985 REG("fail-nth", 0644, proc_fail_nth_operations
),
2987 #ifdef CONFIG_ELF_CORE
2988 REG("coredump_filter", S_IRUGO
|S_IWUSR
, proc_coredump_filter_operations
),
2990 #ifdef CONFIG_TASK_IO_ACCOUNTING
2991 ONE("io", S_IRUSR
, proc_tgid_io_accounting
),
2993 #ifdef CONFIG_USER_NS
2994 REG("uid_map", S_IRUGO
|S_IWUSR
, proc_uid_map_operations
),
2995 REG("gid_map", S_IRUGO
|S_IWUSR
, proc_gid_map_operations
),
2996 REG("projid_map", S_IRUGO
|S_IWUSR
, proc_projid_map_operations
),
2997 REG("setgroups", S_IRUGO
|S_IWUSR
, proc_setgroups_operations
),
2999 #if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
3000 REG("timers", S_IRUGO
, proc_timers_operations
),
3002 REG("timerslack_ns", S_IRUGO
|S_IWUGO
, proc_pid_set_timerslack_ns_operations
),
3003 #ifdef CONFIG_LIVEPATCH
3004 ONE("patch_state", S_IRUSR
, proc_pid_patch_state
),
3008 static int proc_tgid_base_readdir(struct file
*file
, struct dir_context
*ctx
)
3010 return proc_pident_readdir(file
, ctx
,
3011 tgid_base_stuff
, ARRAY_SIZE(tgid_base_stuff
));
3014 static const struct file_operations proc_tgid_base_operations
= {
3015 .read
= generic_read_dir
,
3016 .iterate_shared
= proc_tgid_base_readdir
,
3017 .llseek
= generic_file_llseek
,
3020 static struct dentry
*proc_tgid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, unsigned int flags
)
3022 return proc_pident_lookup(dir
, dentry
,
3023 tgid_base_stuff
, ARRAY_SIZE(tgid_base_stuff
));
3026 static const struct inode_operations proc_tgid_base_inode_operations
= {
3027 .lookup
= proc_tgid_base_lookup
,
3028 .getattr
= pid_getattr
,
3029 .setattr
= proc_setattr
,
3030 .permission
= proc_pid_permission
,
3033 static void proc_flush_task_mnt(struct vfsmount
*mnt
, pid_t pid
, pid_t tgid
)
3035 struct dentry
*dentry
, *leader
, *dir
;
3040 name
.len
= snprintf(buf
, sizeof(buf
), "%u", pid
);
3041 /* no ->d_hash() rejects on procfs */
3042 dentry
= d_hash_and_lookup(mnt
->mnt_root
, &name
);
3044 d_invalidate(dentry
);
3052 name
.len
= snprintf(buf
, sizeof(buf
), "%u", tgid
);
3053 leader
= d_hash_and_lookup(mnt
->mnt_root
, &name
);
3058 name
.len
= strlen(name
.name
);
3059 dir
= d_hash_and_lookup(leader
, &name
);
3061 goto out_put_leader
;
3064 name
.len
= snprintf(buf
, sizeof(buf
), "%u", pid
);
3065 dentry
= d_hash_and_lookup(dir
, &name
);
3067 d_invalidate(dentry
);
3079 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
3080 * @task: task that should be flushed.
3082 * When flushing dentries from proc, one needs to flush them from global
3083 * proc (proc_mnt) and from all the namespaces' procs this task was seen
3084 * in. This call is supposed to do all of this job.
3086 * Looks in the dcache for
3088 * /proc/@tgid/task/@pid
3089 * if either directory is present flushes it and all of it'ts children
3092 * It is safe and reasonable to cache /proc entries for a task until
3093 * that task exits. After that they just clog up the dcache with
3094 * useless entries, possibly causing useful dcache entries to be
3095 * flushed instead. This routine is proved to flush those useless
3096 * dcache entries at process exit time.
3098 * NOTE: This routine is just an optimization so it does not guarantee
3099 * that no dcache entries will exist at process exit time it
3100 * just makes it very unlikely that any will persist.
3103 void proc_flush_task(struct task_struct
*task
)
3106 struct pid
*pid
, *tgid
;
3109 pid
= task_pid(task
);
3110 tgid
= task_tgid(task
);
3112 for (i
= 0; i
<= pid
->level
; i
++) {
3113 upid
= &pid
->numbers
[i
];
3114 proc_flush_task_mnt(upid
->ns
->proc_mnt
, upid
->nr
,
3115 tgid
->numbers
[i
].nr
);
3119 static struct dentry
*proc_pid_instantiate(struct dentry
* dentry
,
3120 struct task_struct
*task
, const void *ptr
)
3122 struct inode
*inode
;
3124 inode
= proc_pid_make_inode(dentry
->d_sb
, task
, S_IFDIR
| S_IRUGO
| S_IXUGO
);
3126 return ERR_PTR(-ENOENT
);
3128 inode
->i_op
= &proc_tgid_base_inode_operations
;
3129 inode
->i_fop
= &proc_tgid_base_operations
;
3130 inode
->i_flags
|=S_IMMUTABLE
;
3132 set_nlink(inode
, nlink_tgid
);
3133 pid_update_inode(task
, inode
);
3135 d_set_d_op(dentry
, &pid_dentry_operations
);
3136 return d_splice_alias(inode
, dentry
);
3139 struct dentry
*proc_pid_lookup(struct inode
*dir
, struct dentry
* dentry
, unsigned int flags
)
3141 struct task_struct
*task
;
3143 struct pid_namespace
*ns
;
3144 struct dentry
*result
= ERR_PTR(-ENOENT
);
3146 tgid
= name_to_int(&dentry
->d_name
);
3150 ns
= dentry
->d_sb
->s_fs_info
;
3152 task
= find_task_by_pid_ns(tgid
, ns
);
3154 get_task_struct(task
);
3159 result
= proc_pid_instantiate(dentry
, task
, NULL
);
3160 put_task_struct(task
);
3166 * Find the first task with tgid >= tgid
3171 struct task_struct
*task
;
3173 static struct tgid_iter
next_tgid(struct pid_namespace
*ns
, struct tgid_iter iter
)
3178 put_task_struct(iter
.task
);
3182 pid
= find_ge_pid(iter
.tgid
, ns
);
3184 iter
.tgid
= pid_nr_ns(pid
, ns
);
3185 iter
.task
= pid_task(pid
, PIDTYPE_PID
);
3186 /* What we to know is if the pid we have find is the
3187 * pid of a thread_group_leader. Testing for task
3188 * being a thread_group_leader is the obvious thing
3189 * todo but there is a window when it fails, due to
3190 * the pid transfer logic in de_thread.
3192 * So we perform the straight forward test of seeing
3193 * if the pid we have found is the pid of a thread
3194 * group leader, and don't worry if the task we have
3195 * found doesn't happen to be a thread group leader.
3196 * As we don't care in the case of readdir.
3198 if (!iter
.task
|| !has_group_leader_pid(iter
.task
)) {
3202 get_task_struct(iter
.task
);
3208 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
3210 /* for the /proc/ directory itself, after non-process stuff has been done */
3211 int proc_pid_readdir(struct file
*file
, struct dir_context
*ctx
)
3213 struct tgid_iter iter
;
3214 struct pid_namespace
*ns
= proc_pid_ns(file_inode(file
));
3215 loff_t pos
= ctx
->pos
;
3217 if (pos
>= PID_MAX_LIMIT
+ TGID_OFFSET
)
3220 if (pos
== TGID_OFFSET
- 2) {
3221 struct inode
*inode
= d_inode(ns
->proc_self
);
3222 if (!dir_emit(ctx
, "self", 4, inode
->i_ino
, DT_LNK
))
3224 ctx
->pos
= pos
= pos
+ 1;
3226 if (pos
== TGID_OFFSET
- 1) {
3227 struct inode
*inode
= d_inode(ns
->proc_thread_self
);
3228 if (!dir_emit(ctx
, "thread-self", 11, inode
->i_ino
, DT_LNK
))
3230 ctx
->pos
= pos
= pos
+ 1;
3232 iter
.tgid
= pos
- TGID_OFFSET
;
3234 for (iter
= next_tgid(ns
, iter
);
3236 iter
.tgid
+= 1, iter
= next_tgid(ns
, iter
)) {
3241 if (!has_pid_permissions(ns
, iter
.task
, HIDEPID_INVISIBLE
))
3244 len
= snprintf(name
, sizeof(name
), "%u", iter
.tgid
);
3245 ctx
->pos
= iter
.tgid
+ TGID_OFFSET
;
3246 if (!proc_fill_cache(file
, ctx
, name
, len
,
3247 proc_pid_instantiate
, iter
.task
, NULL
)) {
3248 put_task_struct(iter
.task
);
3252 ctx
->pos
= PID_MAX_LIMIT
+ TGID_OFFSET
;
3257 * proc_tid_comm_permission is a special permission function exclusively
3258 * used for the node /proc/<pid>/task/<tid>/comm.
3259 * It bypasses generic permission checks in the case where a task of the same
3260 * task group attempts to access the node.
3261 * The rationale behind this is that glibc and bionic access this node for
3262 * cross thread naming (pthread_set/getname_np(!self)). However, if
3263 * PR_SET_DUMPABLE gets set to 0 this node among others becomes uid=0 gid=0,
3264 * which locks out the cross thread naming implementation.
3265 * This function makes sure that the node is always accessible for members of
3266 * same thread group.
3268 static int proc_tid_comm_permission(struct inode
*inode
, int mask
)
3270 bool is_same_tgroup
;
3271 struct task_struct
*task
;
3273 task
= get_proc_task(inode
);
3276 is_same_tgroup
= same_thread_group(current
, task
);
3277 put_task_struct(task
);
3279 if (likely(is_same_tgroup
&& !(mask
& MAY_EXEC
))) {
3280 /* This file (/proc/<pid>/task/<tid>/comm) can always be
3281 * read or written by the members of the corresponding
3287 return generic_permission(inode
, mask
);
3290 static const struct inode_operations proc_tid_comm_inode_operations
= {
3291 .permission
= proc_tid_comm_permission
,
3297 static const struct pid_entry tid_base_stuff
[] = {
3298 DIR("fd", S_IRUSR
|S_IXUSR
, proc_fd_inode_operations
, proc_fd_operations
),
3299 DIR("fdinfo", S_IRUSR
|S_IXUSR
, proc_fdinfo_inode_operations
, proc_fdinfo_operations
),
3300 DIR("ns", S_IRUSR
|S_IXUGO
, proc_ns_dir_inode_operations
, proc_ns_dir_operations
),
3302 DIR("net", S_IRUGO
|S_IXUGO
, proc_net_inode_operations
, proc_net_operations
),
3304 REG("environ", S_IRUSR
, proc_environ_operations
),
3305 REG("auxv", S_IRUSR
, proc_auxv_operations
),
3306 ONE("status", S_IRUGO
, proc_pid_status
),
3307 ONE("personality", S_IRUSR
, proc_pid_personality
),
3308 ONE("limits", S_IRUGO
, proc_pid_limits
),
3309 #ifdef CONFIG_SCHED_DEBUG
3310 REG("sched", S_IRUGO
|S_IWUSR
, proc_pid_sched_operations
),
3312 NOD("comm", S_IFREG
|S_IRUGO
|S_IWUSR
,
3313 &proc_tid_comm_inode_operations
,
3314 &proc_pid_set_comm_operations
, {}),
3315 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3316 ONE("syscall", S_IRUSR
, proc_pid_syscall
),
3318 REG("cmdline", S_IRUGO
, proc_pid_cmdline_ops
),
3319 ONE("stat", S_IRUGO
, proc_tid_stat
),
3320 ONE("statm", S_IRUGO
, proc_pid_statm
),
3321 REG("maps", S_IRUGO
, proc_tid_maps_operations
),
3322 #ifdef CONFIG_PROC_CHILDREN
3323 REG("children", S_IRUGO
, proc_tid_children_operations
),
3326 REG("numa_maps", S_IRUGO
, proc_tid_numa_maps_operations
),
3328 REG("mem", S_IRUSR
|S_IWUSR
, proc_mem_operations
),
3329 LNK("cwd", proc_cwd_link
),
3330 LNK("root", proc_root_link
),
3331 LNK("exe", proc_exe_link
),
3332 REG("mounts", S_IRUGO
, proc_mounts_operations
),
3333 REG("mountinfo", S_IRUGO
, proc_mountinfo_operations
),
3334 #ifdef CONFIG_PROC_PAGE_MONITOR
3335 REG("clear_refs", S_IWUSR
, proc_clear_refs_operations
),
3336 REG("smaps", S_IRUGO
, proc_tid_smaps_operations
),
3337 REG("smaps_rollup", S_IRUGO
, proc_pid_smaps_rollup_operations
),
3338 REG("pagemap", S_IRUSR
, proc_pagemap_operations
),
3340 #ifdef CONFIG_SECURITY
3341 DIR("attr", S_IRUGO
|S_IXUGO
, proc_attr_dir_inode_operations
, proc_attr_dir_operations
),
3343 #ifdef CONFIG_KALLSYMS
3344 ONE("wchan", S_IRUGO
, proc_pid_wchan
),
3346 #ifdef CONFIG_STACKTRACE
3347 ONE("stack", S_IRUSR
, proc_pid_stack
),
3349 #ifdef CONFIG_SCHED_INFO
3350 ONE("schedstat", S_IRUGO
, proc_pid_schedstat
),
3352 #ifdef CONFIG_LATENCYTOP
3353 REG("latency", S_IRUGO
, proc_lstats_operations
),
3355 #ifdef CONFIG_PROC_PID_CPUSET
3356 ONE("cpuset", S_IRUGO
, proc_cpuset_show
),
3358 #ifdef CONFIG_CGROUPS
3359 ONE("cgroup", S_IRUGO
, proc_cgroup_show
),
3361 ONE("oom_score", S_IRUGO
, proc_oom_score
),
3362 REG("oom_adj", S_IRUGO
|S_IWUSR
, proc_oom_adj_operations
),
3363 REG("oom_score_adj", S_IRUGO
|S_IWUSR
, proc_oom_score_adj_operations
),
3364 #ifdef CONFIG_AUDITSYSCALL
3365 REG("loginuid", S_IWUSR
|S_IRUGO
, proc_loginuid_operations
),
3366 REG("sessionid", S_IRUGO
, proc_sessionid_operations
),
3368 #ifdef CONFIG_FAULT_INJECTION
3369 REG("make-it-fail", S_IRUGO
|S_IWUSR
, proc_fault_inject_operations
),
3370 REG("fail-nth", 0644, proc_fail_nth_operations
),
3372 #ifdef CONFIG_TASK_IO_ACCOUNTING
3373 ONE("io", S_IRUSR
, proc_tid_io_accounting
),
3375 #ifdef CONFIG_USER_NS
3376 REG("uid_map", S_IRUGO
|S_IWUSR
, proc_uid_map_operations
),
3377 REG("gid_map", S_IRUGO
|S_IWUSR
, proc_gid_map_operations
),
3378 REG("projid_map", S_IRUGO
|S_IWUSR
, proc_projid_map_operations
),
3379 REG("setgroups", S_IRUGO
|S_IWUSR
, proc_setgroups_operations
),
3381 #ifdef CONFIG_LIVEPATCH
3382 ONE("patch_state", S_IRUSR
, proc_pid_patch_state
),
3386 static int proc_tid_base_readdir(struct file
*file
, struct dir_context
*ctx
)
3388 return proc_pident_readdir(file
, ctx
,
3389 tid_base_stuff
, ARRAY_SIZE(tid_base_stuff
));
3392 static struct dentry
*proc_tid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, unsigned int flags
)
3394 return proc_pident_lookup(dir
, dentry
,
3395 tid_base_stuff
, ARRAY_SIZE(tid_base_stuff
));
3398 static const struct file_operations proc_tid_base_operations
= {
3399 .read
= generic_read_dir
,
3400 .iterate_shared
= proc_tid_base_readdir
,
3401 .llseek
= generic_file_llseek
,
3404 static const struct inode_operations proc_tid_base_inode_operations
= {
3405 .lookup
= proc_tid_base_lookup
,
3406 .getattr
= pid_getattr
,
3407 .setattr
= proc_setattr
,
3410 static struct dentry
*proc_task_instantiate(struct dentry
*dentry
,
3411 struct task_struct
*task
, const void *ptr
)
3413 struct inode
*inode
;
3414 inode
= proc_pid_make_inode(dentry
->d_sb
, task
, S_IFDIR
| S_IRUGO
| S_IXUGO
);
3416 return ERR_PTR(-ENOENT
);
3418 inode
->i_op
= &proc_tid_base_inode_operations
;
3419 inode
->i_fop
= &proc_tid_base_operations
;
3420 inode
->i_flags
|= S_IMMUTABLE
;
3422 set_nlink(inode
, nlink_tid
);
3423 pid_update_inode(task
, inode
);
3425 d_set_d_op(dentry
, &pid_dentry_operations
);
3426 return d_splice_alias(inode
, dentry
);
3429 static struct dentry
*proc_task_lookup(struct inode
*dir
, struct dentry
* dentry
, unsigned int flags
)
3431 struct task_struct
*task
;
3432 struct task_struct
*leader
= get_proc_task(dir
);
3434 struct pid_namespace
*ns
;
3435 struct dentry
*result
= ERR_PTR(-ENOENT
);
3440 tid
= name_to_int(&dentry
->d_name
);
3444 ns
= dentry
->d_sb
->s_fs_info
;
3446 task
= find_task_by_pid_ns(tid
, ns
);
3448 get_task_struct(task
);
3452 if (!same_thread_group(leader
, task
))
3455 result
= proc_task_instantiate(dentry
, task
, NULL
);
3457 put_task_struct(task
);
3459 put_task_struct(leader
);
3465 * Find the first tid of a thread group to return to user space.
3467 * Usually this is just the thread group leader, but if the users
3468 * buffer was too small or there was a seek into the middle of the
3469 * directory we have more work todo.
3471 * In the case of a short read we start with find_task_by_pid.
3473 * In the case of a seek we start with the leader and walk nr
3476 static struct task_struct
*first_tid(struct pid
*pid
, int tid
, loff_t f_pos
,
3477 struct pid_namespace
*ns
)
3479 struct task_struct
*pos
, *task
;
3480 unsigned long nr
= f_pos
;
3482 if (nr
!= f_pos
) /* 32bit overflow? */
3486 task
= pid_task(pid
, PIDTYPE_PID
);
3490 /* Attempt to start with the tid of a thread */
3492 pos
= find_task_by_pid_ns(tid
, ns
);
3493 if (pos
&& same_thread_group(pos
, task
))
3497 /* If nr exceeds the number of threads there is nothing todo */
3498 if (nr
>= get_nr_threads(task
))
3501 /* If we haven't found our starting place yet start
3502 * with the leader and walk nr threads forward.
3504 pos
= task
= task
->group_leader
;
3508 } while_each_thread(task
, pos
);
3513 get_task_struct(pos
);
3520 * Find the next thread in the thread list.
3521 * Return NULL if there is an error or no next thread.
3523 * The reference to the input task_struct is released.
3525 static struct task_struct
*next_tid(struct task_struct
*start
)
3527 struct task_struct
*pos
= NULL
;
3529 if (pid_alive(start
)) {
3530 pos
= next_thread(start
);
3531 if (thread_group_leader(pos
))
3534 get_task_struct(pos
);
3537 put_task_struct(start
);
3541 /* for the /proc/TGID/task/ directories */
3542 static int proc_task_readdir(struct file
*file
, struct dir_context
*ctx
)
3544 struct inode
*inode
= file_inode(file
);
3545 struct task_struct
*task
;
3546 struct pid_namespace
*ns
;
3549 if (proc_inode_is_dead(inode
))
3552 if (!dir_emit_dots(file
, ctx
))
3555 /* f_version caches the tgid value that the last readdir call couldn't
3556 * return. lseek aka telldir automagically resets f_version to 0.
3558 ns
= proc_pid_ns(inode
);
3559 tid
= (int)file
->f_version
;
3560 file
->f_version
= 0;
3561 for (task
= first_tid(proc_pid(inode
), tid
, ctx
->pos
- 2, ns
);
3563 task
= next_tid(task
), ctx
->pos
++) {
3566 tid
= task_pid_nr_ns(task
, ns
);
3567 len
= snprintf(name
, sizeof(name
), "%u", tid
);
3568 if (!proc_fill_cache(file
, ctx
, name
, len
,
3569 proc_task_instantiate
, task
, NULL
)) {
3570 /* returning this tgid failed, save it as the first
3571 * pid for the next readir call */
3572 file
->f_version
= (u64
)tid
;
3573 put_task_struct(task
);
3581 static int proc_task_getattr(const struct path
*path
, struct kstat
*stat
,
3582 u32 request_mask
, unsigned int query_flags
)
3584 struct inode
*inode
= d_inode(path
->dentry
);
3585 struct task_struct
*p
= get_proc_task(inode
);
3586 generic_fillattr(inode
, stat
);
3589 stat
->nlink
+= get_nr_threads(p
);
3596 static const struct inode_operations proc_task_inode_operations
= {
3597 .lookup
= proc_task_lookup
,
3598 .getattr
= proc_task_getattr
,
3599 .setattr
= proc_setattr
,
3600 .permission
= proc_pid_permission
,
3603 static const struct file_operations proc_task_operations
= {
3604 .read
= generic_read_dir
,
3605 .iterate_shared
= proc_task_readdir
,
3606 .llseek
= generic_file_llseek
,
3609 void __init
set_proc_pid_nlink(void)
3611 nlink_tid
= pid_entry_nlink(tid_base_stuff
, ARRAY_SIZE(tid_base_stuff
));
3612 nlink_tgid
= pid_entry_nlink(tgid_base_stuff
, ARRAY_SIZE(tgid_base_stuff
));