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
;
217 char __user
*buf0
= buf
;
227 tsk
= get_proc_task(file_inode(file
));
230 mm
= get_task_mm(tsk
);
231 put_task_struct(tsk
);
234 /* Check if process spawned far enough to have cmdline. */
240 page
= (char *)__get_free_page(GFP_KERNEL
);
246 spin_lock(&mm
->arg_lock
);
247 arg_start
= mm
->arg_start
;
248 arg_end
= mm
->arg_end
;
249 env_start
= mm
->env_start
;
250 env_end
= mm
->env_end
;
251 spin_unlock(&mm
->arg_lock
);
253 BUG_ON(arg_start
> arg_end
);
254 BUG_ON(env_start
> env_end
);
256 len1
= arg_end
- arg_start
;
257 len2
= env_end
- env_start
;
264 * Inherently racy -- command line shares address space
265 * with code and data.
267 if (access_remote_vm(mm
, arg_end
- 1, &c
, 1, FOLL_ANON
) != 1)
270 cmdline
[0].p
= arg_start
;
271 cmdline
[0].len
= len1
;
273 /* Command line (set of strings) occupies whole ARGV. */
277 * Command line (1 string) occupies ARGV and
280 cmdline
[1].p
= env_start
;
281 cmdline
[1].len
= len2
;
289 while (i
< 2 && pos1
>= cmdline
[i
].len
) {
290 pos1
-= cmdline
[i
].len
;
297 p
= cmdline
[i
].p
+ pos1
;
298 len
= cmdline
[i
].len
- pos1
;
299 while (count
> 0 && len
> 0) {
300 unsigned int nr_read
, nr_write
;
302 nr_read
= min3(count
, len
, PAGE_SIZE
);
303 nr_read
= access_remote_vm(mm
, p
, page
, nr_read
, FOLL_ANON
);
308 * Command line can be shorter than whole ARGV
309 * even if last "marker" byte says it is not.
314 nr_write
= strnlen(page
, nr_read
);
316 if (copy_to_user(buf
, page
, nr_write
)) {
326 if (nr_write
< nr_read
)
330 /* Only first chunk can be read partially. */
340 free_page((unsigned long)page
);
346 static const struct file_operations proc_pid_cmdline_ops
= {
347 .read
= proc_pid_cmdline_read
,
348 .llseek
= generic_file_llseek
,
351 #ifdef CONFIG_KALLSYMS
353 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
354 * Returns the resolved symbol. If that fails, simply return the address.
356 static int proc_pid_wchan(struct seq_file
*m
, struct pid_namespace
*ns
,
357 struct pid
*pid
, struct task_struct
*task
)
360 char symname
[KSYM_NAME_LEN
];
362 if (!ptrace_may_access(task
, PTRACE_MODE_READ_FSCREDS
))
365 wchan
= get_wchan(task
);
366 if (wchan
&& !lookup_symbol_name(wchan
, symname
)) {
367 seq_puts(m
, symname
);
375 #endif /* CONFIG_KALLSYMS */
377 static int lock_trace(struct task_struct
*task
)
379 int err
= mutex_lock_killable(&task
->signal
->cred_guard_mutex
);
382 if (!ptrace_may_access(task
, PTRACE_MODE_ATTACH_FSCREDS
)) {
383 mutex_unlock(&task
->signal
->cred_guard_mutex
);
389 static void unlock_trace(struct task_struct
*task
)
391 mutex_unlock(&task
->signal
->cred_guard_mutex
);
394 #ifdef CONFIG_STACKTRACE
396 #define MAX_STACK_TRACE_DEPTH 64
398 static int proc_pid_stack(struct seq_file
*m
, struct pid_namespace
*ns
,
399 struct pid
*pid
, struct task_struct
*task
)
401 struct stack_trace trace
;
402 unsigned long *entries
;
406 entries
= kmalloc(MAX_STACK_TRACE_DEPTH
* sizeof(*entries
), GFP_KERNEL
);
410 trace
.nr_entries
= 0;
411 trace
.max_entries
= MAX_STACK_TRACE_DEPTH
;
412 trace
.entries
= entries
;
415 err
= lock_trace(task
);
417 save_stack_trace_tsk(task
, &trace
);
419 for (i
= 0; i
< trace
.nr_entries
; i
++) {
420 seq_printf(m
, "[<0>] %pB\n", (void *)entries
[i
]);
430 #ifdef CONFIG_SCHED_INFO
432 * Provides /proc/PID/schedstat
434 static int proc_pid_schedstat(struct seq_file
*m
, struct pid_namespace
*ns
,
435 struct pid
*pid
, struct task_struct
*task
)
437 if (unlikely(!sched_info_on()))
438 seq_printf(m
, "0 0 0\n");
440 seq_printf(m
, "%llu %llu %lu\n",
441 (unsigned long long)task
->se
.sum_exec_runtime
,
442 (unsigned long long)task
->sched_info
.run_delay
,
443 task
->sched_info
.pcount
);
449 #ifdef CONFIG_LATENCYTOP
450 static int lstats_show_proc(struct seq_file
*m
, void *v
)
453 struct inode
*inode
= m
->private;
454 struct task_struct
*task
= get_proc_task(inode
);
458 seq_puts(m
, "Latency Top version : v0.1\n");
459 for (i
= 0; i
< 32; i
++) {
460 struct latency_record
*lr
= &task
->latency_record
[i
];
461 if (lr
->backtrace
[0]) {
463 seq_printf(m
, "%i %li %li",
464 lr
->count
, lr
->time
, lr
->max
);
465 for (q
= 0; q
< LT_BACKTRACEDEPTH
; q
++) {
466 unsigned long bt
= lr
->backtrace
[q
];
471 seq_printf(m
, " %ps", (void *)bt
);
477 put_task_struct(task
);
481 static int lstats_open(struct inode
*inode
, struct file
*file
)
483 return single_open(file
, lstats_show_proc
, inode
);
486 static ssize_t
lstats_write(struct file
*file
, const char __user
*buf
,
487 size_t count
, loff_t
*offs
)
489 struct task_struct
*task
= get_proc_task(file_inode(file
));
493 clear_all_latency_tracing(task
);
494 put_task_struct(task
);
499 static const struct file_operations proc_lstats_operations
= {
502 .write
= lstats_write
,
504 .release
= single_release
,
509 static int proc_oom_score(struct seq_file
*m
, struct pid_namespace
*ns
,
510 struct pid
*pid
, struct task_struct
*task
)
512 unsigned long totalpages
= totalram_pages
+ total_swap_pages
;
513 unsigned long points
= 0;
515 points
= oom_badness(task
, NULL
, NULL
, totalpages
) *
517 seq_printf(m
, "%lu\n", points
);
527 static const struct limit_names lnames
[RLIM_NLIMITS
] = {
528 [RLIMIT_CPU
] = {"Max cpu time", "seconds"},
529 [RLIMIT_FSIZE
] = {"Max file size", "bytes"},
530 [RLIMIT_DATA
] = {"Max data size", "bytes"},
531 [RLIMIT_STACK
] = {"Max stack size", "bytes"},
532 [RLIMIT_CORE
] = {"Max core file size", "bytes"},
533 [RLIMIT_RSS
] = {"Max resident set", "bytes"},
534 [RLIMIT_NPROC
] = {"Max processes", "processes"},
535 [RLIMIT_NOFILE
] = {"Max open files", "files"},
536 [RLIMIT_MEMLOCK
] = {"Max locked memory", "bytes"},
537 [RLIMIT_AS
] = {"Max address space", "bytes"},
538 [RLIMIT_LOCKS
] = {"Max file locks", "locks"},
539 [RLIMIT_SIGPENDING
] = {"Max pending signals", "signals"},
540 [RLIMIT_MSGQUEUE
] = {"Max msgqueue size", "bytes"},
541 [RLIMIT_NICE
] = {"Max nice priority", NULL
},
542 [RLIMIT_RTPRIO
] = {"Max realtime priority", NULL
},
543 [RLIMIT_RTTIME
] = {"Max realtime timeout", "us"},
546 /* Display limits for a process */
547 static int proc_pid_limits(struct seq_file
*m
, struct pid_namespace
*ns
,
548 struct pid
*pid
, struct task_struct
*task
)
553 struct rlimit rlim
[RLIM_NLIMITS
];
555 if (!lock_task_sighand(task
, &flags
))
557 memcpy(rlim
, task
->signal
->rlim
, sizeof(struct rlimit
) * RLIM_NLIMITS
);
558 unlock_task_sighand(task
, &flags
);
561 * print the file header
563 seq_printf(m
, "%-25s %-20s %-20s %-10s\n",
564 "Limit", "Soft Limit", "Hard Limit", "Units");
566 for (i
= 0; i
< RLIM_NLIMITS
; i
++) {
567 if (rlim
[i
].rlim_cur
== RLIM_INFINITY
)
568 seq_printf(m
, "%-25s %-20s ",
569 lnames
[i
].name
, "unlimited");
571 seq_printf(m
, "%-25s %-20lu ",
572 lnames
[i
].name
, rlim
[i
].rlim_cur
);
574 if (rlim
[i
].rlim_max
== RLIM_INFINITY
)
575 seq_printf(m
, "%-20s ", "unlimited");
577 seq_printf(m
, "%-20lu ", rlim
[i
].rlim_max
);
580 seq_printf(m
, "%-10s\n", lnames
[i
].unit
);
588 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
589 static int proc_pid_syscall(struct seq_file
*m
, struct pid_namespace
*ns
,
590 struct pid
*pid
, struct task_struct
*task
)
593 unsigned long args
[6], sp
, pc
;
596 res
= lock_trace(task
);
600 if (task_current_syscall(task
, &nr
, args
, 6, &sp
, &pc
))
601 seq_puts(m
, "running\n");
603 seq_printf(m
, "%ld 0x%lx 0x%lx\n", nr
, sp
, pc
);
606 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
608 args
[0], args
[1], args
[2], args
[3], args
[4], args
[5],
614 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
616 /************************************************************************/
617 /* Here the fs part begins */
618 /************************************************************************/
620 /* permission checks */
621 static int proc_fd_access_allowed(struct inode
*inode
)
623 struct task_struct
*task
;
625 /* Allow access to a task's file descriptors if it is us or we
626 * may use ptrace attach to the process and find out that
629 task
= get_proc_task(inode
);
631 allowed
= ptrace_may_access(task
, PTRACE_MODE_READ_FSCREDS
);
632 put_task_struct(task
);
637 int proc_setattr(struct dentry
*dentry
, struct iattr
*attr
)
640 struct inode
*inode
= d_inode(dentry
);
642 if (attr
->ia_valid
& ATTR_MODE
)
645 error
= setattr_prepare(dentry
, attr
);
649 setattr_copy(inode
, attr
);
650 mark_inode_dirty(inode
);
655 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
656 * or euid/egid (for hide_pid_min=2)?
658 static bool has_pid_permissions(struct pid_namespace
*pid
,
659 struct task_struct
*task
,
662 if (pid
->hide_pid
< hide_pid_min
)
664 if (in_group_p(pid
->pid_gid
))
666 return ptrace_may_access(task
, PTRACE_MODE_READ_FSCREDS
);
670 static int proc_pid_permission(struct inode
*inode
, int mask
)
672 struct pid_namespace
*pid
= proc_pid_ns(inode
);
673 struct task_struct
*task
;
676 task
= get_proc_task(inode
);
679 has_perms
= has_pid_permissions(pid
, task
, HIDEPID_NO_ACCESS
);
680 put_task_struct(task
);
683 if (pid
->hide_pid
== HIDEPID_INVISIBLE
) {
685 * Let's make getdents(), stat(), and open()
686 * consistent with each other. If a process
687 * may not stat() a file, it shouldn't be seen
695 return generic_permission(inode
, mask
);
700 static const struct inode_operations proc_def_inode_operations
= {
701 .setattr
= proc_setattr
,
704 static int proc_single_show(struct seq_file
*m
, void *v
)
706 struct inode
*inode
= m
->private;
707 struct pid_namespace
*ns
= proc_pid_ns(inode
);
708 struct pid
*pid
= proc_pid(inode
);
709 struct task_struct
*task
;
712 task
= get_pid_task(pid
, PIDTYPE_PID
);
716 ret
= PROC_I(inode
)->op
.proc_show(m
, ns
, pid
, task
);
718 put_task_struct(task
);
722 static int proc_single_open(struct inode
*inode
, struct file
*filp
)
724 return single_open(filp
, proc_single_show
, inode
);
727 static const struct file_operations proc_single_file_operations
= {
728 .open
= proc_single_open
,
731 .release
= single_release
,
735 struct mm_struct
*proc_mem_open(struct inode
*inode
, unsigned int mode
)
737 struct task_struct
*task
= get_proc_task(inode
);
738 struct mm_struct
*mm
= ERR_PTR(-ESRCH
);
741 mm
= mm_access(task
, mode
| PTRACE_MODE_FSCREDS
);
742 put_task_struct(task
);
744 if (!IS_ERR_OR_NULL(mm
)) {
745 /* ensure this mm_struct can't be freed */
747 /* but do not pin its memory */
755 static int __mem_open(struct inode
*inode
, struct file
*file
, unsigned int mode
)
757 struct mm_struct
*mm
= proc_mem_open(inode
, mode
);
762 file
->private_data
= mm
;
766 static int mem_open(struct inode
*inode
, struct file
*file
)
768 int ret
= __mem_open(inode
, file
, PTRACE_MODE_ATTACH
);
770 /* OK to pass negative loff_t, we can catch out-of-range */
771 file
->f_mode
|= FMODE_UNSIGNED_OFFSET
;
776 static ssize_t
mem_rw(struct file
*file
, char __user
*buf
,
777 size_t count
, loff_t
*ppos
, int write
)
779 struct mm_struct
*mm
= file
->private_data
;
780 unsigned long addr
= *ppos
;
788 page
= (char *)__get_free_page(GFP_KERNEL
);
793 if (!mmget_not_zero(mm
))
796 flags
= FOLL_FORCE
| (write
? FOLL_WRITE
: 0);
799 int this_len
= min_t(int, count
, PAGE_SIZE
);
801 if (write
&& copy_from_user(page
, buf
, this_len
)) {
806 this_len
= access_remote_vm(mm
, addr
, page
, this_len
, flags
);
813 if (!write
&& copy_to_user(buf
, page
, this_len
)) {
827 free_page((unsigned long) page
);
831 static ssize_t
mem_read(struct file
*file
, char __user
*buf
,
832 size_t count
, loff_t
*ppos
)
834 return mem_rw(file
, buf
, count
, ppos
, 0);
837 static ssize_t
mem_write(struct file
*file
, const char __user
*buf
,
838 size_t count
, loff_t
*ppos
)
840 return mem_rw(file
, (char __user
*)buf
, count
, ppos
, 1);
843 loff_t
mem_lseek(struct file
*file
, loff_t offset
, int orig
)
847 file
->f_pos
= offset
;
850 file
->f_pos
+= offset
;
855 force_successful_syscall_return();
859 static int mem_release(struct inode
*inode
, struct file
*file
)
861 struct mm_struct
*mm
= file
->private_data
;
867 static const struct file_operations proc_mem_operations
= {
872 .release
= mem_release
,
875 static int environ_open(struct inode
*inode
, struct file
*file
)
877 return __mem_open(inode
, file
, PTRACE_MODE_READ
);
880 static ssize_t
environ_read(struct file
*file
, char __user
*buf
,
881 size_t count
, loff_t
*ppos
)
884 unsigned long src
= *ppos
;
886 struct mm_struct
*mm
= file
->private_data
;
887 unsigned long env_start
, env_end
;
889 /* Ensure the process spawned far enough to have an environment. */
890 if (!mm
|| !mm
->env_end
)
893 page
= (char *)__get_free_page(GFP_KERNEL
);
898 if (!mmget_not_zero(mm
))
901 spin_lock(&mm
->arg_lock
);
902 env_start
= mm
->env_start
;
903 env_end
= mm
->env_end
;
904 spin_unlock(&mm
->arg_lock
);
907 size_t this_len
, max_len
;
910 if (src
>= (env_end
- env_start
))
913 this_len
= env_end
- (env_start
+ src
);
915 max_len
= min_t(size_t, PAGE_SIZE
, count
);
916 this_len
= min(max_len
, this_len
);
918 retval
= access_remote_vm(mm
, (env_start
+ src
), page
, this_len
, FOLL_ANON
);
925 if (copy_to_user(buf
, page
, retval
)) {
939 free_page((unsigned long) page
);
943 static const struct file_operations proc_environ_operations
= {
944 .open
= environ_open
,
945 .read
= environ_read
,
946 .llseek
= generic_file_llseek
,
947 .release
= mem_release
,
950 static int auxv_open(struct inode
*inode
, struct file
*file
)
952 return __mem_open(inode
, file
, PTRACE_MODE_READ_FSCREDS
);
955 static ssize_t
auxv_read(struct file
*file
, char __user
*buf
,
956 size_t count
, loff_t
*ppos
)
958 struct mm_struct
*mm
= file
->private_data
;
959 unsigned int nwords
= 0;
965 } while (mm
->saved_auxv
[nwords
- 2] != 0); /* AT_NULL */
966 return simple_read_from_buffer(buf
, count
, ppos
, mm
->saved_auxv
,
967 nwords
* sizeof(mm
->saved_auxv
[0]));
970 static const struct file_operations proc_auxv_operations
= {
973 .llseek
= generic_file_llseek
,
974 .release
= mem_release
,
977 static ssize_t
oom_adj_read(struct file
*file
, char __user
*buf
, size_t count
,
980 struct task_struct
*task
= get_proc_task(file_inode(file
));
981 char buffer
[PROC_NUMBUF
];
982 int oom_adj
= OOM_ADJUST_MIN
;
987 if (task
->signal
->oom_score_adj
== OOM_SCORE_ADJ_MAX
)
988 oom_adj
= OOM_ADJUST_MAX
;
990 oom_adj
= (task
->signal
->oom_score_adj
* -OOM_DISABLE
) /
992 put_task_struct(task
);
993 len
= snprintf(buffer
, sizeof(buffer
), "%d\n", oom_adj
);
994 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
997 static int __set_oom_adj(struct file
*file
, int oom_adj
, bool legacy
)
999 static DEFINE_MUTEX(oom_adj_mutex
);
1000 struct mm_struct
*mm
= NULL
;
1001 struct task_struct
*task
;
1004 task
= get_proc_task(file_inode(file
));
1008 mutex_lock(&oom_adj_mutex
);
1010 if (oom_adj
< task
->signal
->oom_score_adj
&&
1011 !capable(CAP_SYS_RESOURCE
)) {
1016 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
1017 * /proc/pid/oom_score_adj instead.
1019 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
1020 current
->comm
, task_pid_nr(current
), task_pid_nr(task
),
1023 if ((short)oom_adj
< task
->signal
->oom_score_adj_min
&&
1024 !capable(CAP_SYS_RESOURCE
)) {
1031 * Make sure we will check other processes sharing the mm if this is
1032 * not vfrok which wants its own oom_score_adj.
1033 * pin the mm so it doesn't go away and get reused after task_unlock
1035 if (!task
->vfork_done
) {
1036 struct task_struct
*p
= find_lock_task_mm(task
);
1039 if (atomic_read(&p
->mm
->mm_users
) > 1) {
1047 task
->signal
->oom_score_adj
= oom_adj
;
1048 if (!legacy
&& has_capability_noaudit(current
, CAP_SYS_RESOURCE
))
1049 task
->signal
->oom_score_adj_min
= (short)oom_adj
;
1050 trace_oom_score_adj_update(task
);
1053 struct task_struct
*p
;
1056 for_each_process(p
) {
1057 if (same_thread_group(task
, p
))
1060 /* do not touch kernel threads or the global init */
1061 if (p
->flags
& PF_KTHREAD
|| is_global_init(p
))
1065 if (!p
->vfork_done
&& process_shares_mm(p
, mm
)) {
1066 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",
1067 task_pid_nr(p
), p
->comm
,
1068 p
->signal
->oom_score_adj
, oom_adj
,
1069 task_pid_nr(task
), task
->comm
);
1070 p
->signal
->oom_score_adj
= oom_adj
;
1071 if (!legacy
&& has_capability_noaudit(current
, CAP_SYS_RESOURCE
))
1072 p
->signal
->oom_score_adj_min
= (short)oom_adj
;
1080 mutex_unlock(&oom_adj_mutex
);
1081 put_task_struct(task
);
1086 * /proc/pid/oom_adj exists solely for backwards compatibility with previous
1087 * kernels. The effective policy is defined by oom_score_adj, which has a
1088 * different scale: oom_adj grew exponentially and oom_score_adj grows linearly.
1089 * Values written to oom_adj are simply mapped linearly to oom_score_adj.
1090 * Processes that become oom disabled via oom_adj will still be oom disabled
1091 * with this implementation.
1093 * oom_adj cannot be removed since existing userspace binaries use it.
1095 static ssize_t
oom_adj_write(struct file
*file
, const char __user
*buf
,
1096 size_t count
, loff_t
*ppos
)
1098 char buffer
[PROC_NUMBUF
];
1102 memset(buffer
, 0, sizeof(buffer
));
1103 if (count
> sizeof(buffer
) - 1)
1104 count
= sizeof(buffer
) - 1;
1105 if (copy_from_user(buffer
, buf
, count
)) {
1110 err
= kstrtoint(strstrip(buffer
), 0, &oom_adj
);
1113 if ((oom_adj
< OOM_ADJUST_MIN
|| oom_adj
> OOM_ADJUST_MAX
) &&
1114 oom_adj
!= OOM_DISABLE
) {
1120 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1121 * value is always attainable.
1123 if (oom_adj
== OOM_ADJUST_MAX
)
1124 oom_adj
= OOM_SCORE_ADJ_MAX
;
1126 oom_adj
= (oom_adj
* OOM_SCORE_ADJ_MAX
) / -OOM_DISABLE
;
1128 err
= __set_oom_adj(file
, oom_adj
, true);
1130 return err
< 0 ? err
: count
;
1133 static const struct file_operations proc_oom_adj_operations
= {
1134 .read
= oom_adj_read
,
1135 .write
= oom_adj_write
,
1136 .llseek
= generic_file_llseek
,
1139 static ssize_t
oom_score_adj_read(struct file
*file
, char __user
*buf
,
1140 size_t count
, loff_t
*ppos
)
1142 struct task_struct
*task
= get_proc_task(file_inode(file
));
1143 char buffer
[PROC_NUMBUF
];
1144 short oom_score_adj
= OOM_SCORE_ADJ_MIN
;
1149 oom_score_adj
= task
->signal
->oom_score_adj
;
1150 put_task_struct(task
);
1151 len
= snprintf(buffer
, sizeof(buffer
), "%hd\n", oom_score_adj
);
1152 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
1155 static ssize_t
oom_score_adj_write(struct file
*file
, const char __user
*buf
,
1156 size_t count
, loff_t
*ppos
)
1158 char buffer
[PROC_NUMBUF
];
1162 memset(buffer
, 0, sizeof(buffer
));
1163 if (count
> sizeof(buffer
) - 1)
1164 count
= sizeof(buffer
) - 1;
1165 if (copy_from_user(buffer
, buf
, count
)) {
1170 err
= kstrtoint(strstrip(buffer
), 0, &oom_score_adj
);
1173 if (oom_score_adj
< OOM_SCORE_ADJ_MIN
||
1174 oom_score_adj
> OOM_SCORE_ADJ_MAX
) {
1179 err
= __set_oom_adj(file
, oom_score_adj
, false);
1181 return err
< 0 ? err
: count
;
1184 static const struct file_operations proc_oom_score_adj_operations
= {
1185 .read
= oom_score_adj_read
,
1186 .write
= oom_score_adj_write
,
1187 .llseek
= default_llseek
,
1190 #ifdef CONFIG_AUDITSYSCALL
1191 #define TMPBUFLEN 11
1192 static ssize_t
proc_loginuid_read(struct file
* file
, char __user
* buf
,
1193 size_t count
, loff_t
*ppos
)
1195 struct inode
* inode
= file_inode(file
);
1196 struct task_struct
*task
= get_proc_task(inode
);
1198 char tmpbuf
[TMPBUFLEN
];
1202 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
1203 from_kuid(file
->f_cred
->user_ns
,
1204 audit_get_loginuid(task
)));
1205 put_task_struct(task
);
1206 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1209 static ssize_t
proc_loginuid_write(struct file
* file
, const char __user
* buf
,
1210 size_t count
, loff_t
*ppos
)
1212 struct inode
* inode
= file_inode(file
);
1218 if (current
!= pid_task(proc_pid(inode
), PIDTYPE_PID
)) {
1225 /* No partial writes. */
1229 rv
= kstrtou32_from_user(buf
, count
, 10, &loginuid
);
1233 /* is userspace tring to explicitly UNSET the loginuid? */
1234 if (loginuid
== AUDIT_UID_UNSET
) {
1235 kloginuid
= INVALID_UID
;
1237 kloginuid
= make_kuid(file
->f_cred
->user_ns
, loginuid
);
1238 if (!uid_valid(kloginuid
))
1242 rv
= audit_set_loginuid(kloginuid
);
1248 static const struct file_operations proc_loginuid_operations
= {
1249 .read
= proc_loginuid_read
,
1250 .write
= proc_loginuid_write
,
1251 .llseek
= generic_file_llseek
,
1254 static ssize_t
proc_sessionid_read(struct file
* file
, char __user
* buf
,
1255 size_t count
, loff_t
*ppos
)
1257 struct inode
* inode
= file_inode(file
);
1258 struct task_struct
*task
= get_proc_task(inode
);
1260 char tmpbuf
[TMPBUFLEN
];
1264 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
1265 audit_get_sessionid(task
));
1266 put_task_struct(task
);
1267 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1270 static const struct file_operations proc_sessionid_operations
= {
1271 .read
= proc_sessionid_read
,
1272 .llseek
= generic_file_llseek
,
1276 #ifdef CONFIG_FAULT_INJECTION
1277 static ssize_t
proc_fault_inject_read(struct file
* file
, char __user
* buf
,
1278 size_t count
, loff_t
*ppos
)
1280 struct task_struct
*task
= get_proc_task(file_inode(file
));
1281 char buffer
[PROC_NUMBUF
];
1287 make_it_fail
= task
->make_it_fail
;
1288 put_task_struct(task
);
1290 len
= snprintf(buffer
, sizeof(buffer
), "%i\n", make_it_fail
);
1292 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
1295 static ssize_t
proc_fault_inject_write(struct file
* file
,
1296 const char __user
* buf
, size_t count
, loff_t
*ppos
)
1298 struct task_struct
*task
;
1299 char buffer
[PROC_NUMBUF
];
1303 if (!capable(CAP_SYS_RESOURCE
))
1305 memset(buffer
, 0, sizeof(buffer
));
1306 if (count
> sizeof(buffer
) - 1)
1307 count
= sizeof(buffer
) - 1;
1308 if (copy_from_user(buffer
, buf
, count
))
1310 rv
= kstrtoint(strstrip(buffer
), 0, &make_it_fail
);
1313 if (make_it_fail
< 0 || make_it_fail
> 1)
1316 task
= get_proc_task(file_inode(file
));
1319 task
->make_it_fail
= make_it_fail
;
1320 put_task_struct(task
);
1325 static const struct file_operations proc_fault_inject_operations
= {
1326 .read
= proc_fault_inject_read
,
1327 .write
= proc_fault_inject_write
,
1328 .llseek
= generic_file_llseek
,
1331 static ssize_t
proc_fail_nth_write(struct file
*file
, const char __user
*buf
,
1332 size_t count
, loff_t
*ppos
)
1334 struct task_struct
*task
;
1338 err
= kstrtouint_from_user(buf
, count
, 0, &n
);
1342 task
= get_proc_task(file_inode(file
));
1346 put_task_struct(task
);
1351 static ssize_t
proc_fail_nth_read(struct file
*file
, char __user
*buf
,
1352 size_t count
, loff_t
*ppos
)
1354 struct task_struct
*task
;
1355 char numbuf
[PROC_NUMBUF
];
1358 task
= get_proc_task(file_inode(file
));
1361 len
= snprintf(numbuf
, sizeof(numbuf
), "%u\n", task
->fail_nth
);
1362 len
= simple_read_from_buffer(buf
, count
, ppos
, numbuf
, len
);
1363 put_task_struct(task
);
1368 static const struct file_operations proc_fail_nth_operations
= {
1369 .read
= proc_fail_nth_read
,
1370 .write
= proc_fail_nth_write
,
1375 #ifdef CONFIG_SCHED_DEBUG
1377 * Print out various scheduling related per-task fields:
1379 static int sched_show(struct seq_file
*m
, void *v
)
1381 struct inode
*inode
= m
->private;
1382 struct pid_namespace
*ns
= proc_pid_ns(inode
);
1383 struct task_struct
*p
;
1385 p
= get_proc_task(inode
);
1388 proc_sched_show_task(p
, ns
, m
);
1396 sched_write(struct file
*file
, const char __user
*buf
,
1397 size_t count
, loff_t
*offset
)
1399 struct inode
*inode
= file_inode(file
);
1400 struct task_struct
*p
;
1402 p
= get_proc_task(inode
);
1405 proc_sched_set_task(p
);
1412 static int sched_open(struct inode
*inode
, struct file
*filp
)
1414 return single_open(filp
, sched_show
, inode
);
1417 static const struct file_operations proc_pid_sched_operations
= {
1420 .write
= sched_write
,
1421 .llseek
= seq_lseek
,
1422 .release
= single_release
,
1427 #ifdef CONFIG_SCHED_AUTOGROUP
1429 * Print out autogroup related information:
1431 static int sched_autogroup_show(struct seq_file
*m
, void *v
)
1433 struct inode
*inode
= m
->private;
1434 struct task_struct
*p
;
1436 p
= get_proc_task(inode
);
1439 proc_sched_autogroup_show_task(p
, m
);
1447 sched_autogroup_write(struct file
*file
, const char __user
*buf
,
1448 size_t count
, loff_t
*offset
)
1450 struct inode
*inode
= file_inode(file
);
1451 struct task_struct
*p
;
1452 char buffer
[PROC_NUMBUF
];
1456 memset(buffer
, 0, sizeof(buffer
));
1457 if (count
> sizeof(buffer
) - 1)
1458 count
= sizeof(buffer
) - 1;
1459 if (copy_from_user(buffer
, buf
, count
))
1462 err
= kstrtoint(strstrip(buffer
), 0, &nice
);
1466 p
= get_proc_task(inode
);
1470 err
= proc_sched_autogroup_set_nice(p
, nice
);
1479 static int sched_autogroup_open(struct inode
*inode
, struct file
*filp
)
1483 ret
= single_open(filp
, sched_autogroup_show
, NULL
);
1485 struct seq_file
*m
= filp
->private_data
;
1492 static const struct file_operations proc_pid_sched_autogroup_operations
= {
1493 .open
= sched_autogroup_open
,
1495 .write
= sched_autogroup_write
,
1496 .llseek
= seq_lseek
,
1497 .release
= single_release
,
1500 #endif /* CONFIG_SCHED_AUTOGROUP */
1502 static ssize_t
comm_write(struct file
*file
, const char __user
*buf
,
1503 size_t count
, loff_t
*offset
)
1505 struct inode
*inode
= file_inode(file
);
1506 struct task_struct
*p
;
1507 char buffer
[TASK_COMM_LEN
];
1508 const size_t maxlen
= sizeof(buffer
) - 1;
1510 memset(buffer
, 0, sizeof(buffer
));
1511 if (copy_from_user(buffer
, buf
, count
> maxlen
? maxlen
: count
))
1514 p
= get_proc_task(inode
);
1518 if (same_thread_group(current
, p
))
1519 set_task_comm(p
, buffer
);
1528 static int comm_show(struct seq_file
*m
, void *v
)
1530 struct inode
*inode
= m
->private;
1531 struct task_struct
*p
;
1533 p
= get_proc_task(inode
);
1537 proc_task_name(m
, p
, false);
1545 static int comm_open(struct inode
*inode
, struct file
*filp
)
1547 return single_open(filp
, comm_show
, inode
);
1550 static const struct file_operations proc_pid_set_comm_operations
= {
1553 .write
= comm_write
,
1554 .llseek
= seq_lseek
,
1555 .release
= single_release
,
1558 static int proc_exe_link(struct dentry
*dentry
, struct path
*exe_path
)
1560 struct task_struct
*task
;
1561 struct file
*exe_file
;
1563 task
= get_proc_task(d_inode(dentry
));
1566 exe_file
= get_task_exe_file(task
);
1567 put_task_struct(task
);
1569 *exe_path
= exe_file
->f_path
;
1570 path_get(&exe_file
->f_path
);
1577 static const char *proc_pid_get_link(struct dentry
*dentry
,
1578 struct inode
*inode
,
1579 struct delayed_call
*done
)
1582 int error
= -EACCES
;
1585 return ERR_PTR(-ECHILD
);
1587 /* Are we allowed to snoop on the tasks file descriptors? */
1588 if (!proc_fd_access_allowed(inode
))
1591 error
= PROC_I(inode
)->op
.proc_get_link(dentry
, &path
);
1595 nd_jump_link(&path
);
1598 return ERR_PTR(error
);
1601 static int do_proc_readlink(struct path
*path
, char __user
*buffer
, int buflen
)
1603 char *tmp
= (char *)__get_free_page(GFP_KERNEL
);
1610 pathname
= d_path(path
, tmp
, PAGE_SIZE
);
1611 len
= PTR_ERR(pathname
);
1612 if (IS_ERR(pathname
))
1614 len
= tmp
+ PAGE_SIZE
- 1 - pathname
;
1618 if (copy_to_user(buffer
, pathname
, len
))
1621 free_page((unsigned long)tmp
);
1625 static int proc_pid_readlink(struct dentry
* dentry
, char __user
* buffer
, int buflen
)
1627 int error
= -EACCES
;
1628 struct inode
*inode
= d_inode(dentry
);
1631 /* Are we allowed to snoop on the tasks file descriptors? */
1632 if (!proc_fd_access_allowed(inode
))
1635 error
= PROC_I(inode
)->op
.proc_get_link(dentry
, &path
);
1639 error
= do_proc_readlink(&path
, buffer
, buflen
);
1645 const struct inode_operations proc_pid_link_inode_operations
= {
1646 .readlink
= proc_pid_readlink
,
1647 .get_link
= proc_pid_get_link
,
1648 .setattr
= proc_setattr
,
1652 /* building an inode */
1654 void task_dump_owner(struct task_struct
*task
, umode_t mode
,
1655 kuid_t
*ruid
, kgid_t
*rgid
)
1657 /* Depending on the state of dumpable compute who should own a
1658 * proc file for a task.
1660 const struct cred
*cred
;
1664 if (unlikely(task
->flags
& PF_KTHREAD
)) {
1665 *ruid
= GLOBAL_ROOT_UID
;
1666 *rgid
= GLOBAL_ROOT_GID
;
1670 /* Default to the tasks effective ownership */
1672 cred
= __task_cred(task
);
1678 * Before the /proc/pid/status file was created the only way to read
1679 * the effective uid of a /process was to stat /proc/pid. Reading
1680 * /proc/pid/status is slow enough that procps and other packages
1681 * kept stating /proc/pid. To keep the rules in /proc simple I have
1682 * made this apply to all per process world readable and executable
1685 if (mode
!= (S_IFDIR
|S_IRUGO
|S_IXUGO
)) {
1686 struct mm_struct
*mm
;
1689 /* Make non-dumpable tasks owned by some root */
1691 if (get_dumpable(mm
) != SUID_DUMP_USER
) {
1692 struct user_namespace
*user_ns
= mm
->user_ns
;
1694 uid
= make_kuid(user_ns
, 0);
1695 if (!uid_valid(uid
))
1696 uid
= GLOBAL_ROOT_UID
;
1698 gid
= make_kgid(user_ns
, 0);
1699 if (!gid_valid(gid
))
1700 gid
= GLOBAL_ROOT_GID
;
1703 uid
= GLOBAL_ROOT_UID
;
1704 gid
= GLOBAL_ROOT_GID
;
1712 struct inode
*proc_pid_make_inode(struct super_block
* sb
,
1713 struct task_struct
*task
, umode_t mode
)
1715 struct inode
* inode
;
1716 struct proc_inode
*ei
;
1718 /* We need a new inode */
1720 inode
= new_inode(sb
);
1726 inode
->i_mode
= mode
;
1727 inode
->i_ino
= get_next_ino();
1728 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= current_time(inode
);
1729 inode
->i_op
= &proc_def_inode_operations
;
1732 * grab the reference to task.
1734 ei
->pid
= get_task_pid(task
, PIDTYPE_PID
);
1738 task_dump_owner(task
, 0, &inode
->i_uid
, &inode
->i_gid
);
1739 security_task_to_inode(task
, inode
);
1749 int pid_getattr(const struct path
*path
, struct kstat
*stat
,
1750 u32 request_mask
, unsigned int query_flags
)
1752 struct inode
*inode
= d_inode(path
->dentry
);
1753 struct pid_namespace
*pid
= proc_pid_ns(inode
);
1754 struct task_struct
*task
;
1756 generic_fillattr(inode
, stat
);
1758 stat
->uid
= GLOBAL_ROOT_UID
;
1759 stat
->gid
= GLOBAL_ROOT_GID
;
1761 task
= pid_task(proc_pid(inode
), PIDTYPE_PID
);
1763 if (!has_pid_permissions(pid
, task
, HIDEPID_INVISIBLE
)) {
1766 * This doesn't prevent learning whether PID exists,
1767 * it only makes getattr() consistent with readdir().
1771 task_dump_owner(task
, inode
->i_mode
, &stat
->uid
, &stat
->gid
);
1780 * Set <pid>/... inode ownership (can change due to setuid(), etc.)
1782 void pid_update_inode(struct task_struct
*task
, struct inode
*inode
)
1784 task_dump_owner(task
, inode
->i_mode
, &inode
->i_uid
, &inode
->i_gid
);
1786 inode
->i_mode
&= ~(S_ISUID
| S_ISGID
);
1787 security_task_to_inode(task
, inode
);
1791 * Rewrite the inode's ownerships here because the owning task may have
1792 * performed a setuid(), etc.
1795 static int pid_revalidate(struct dentry
*dentry
, unsigned int flags
)
1797 struct inode
*inode
;
1798 struct task_struct
*task
;
1800 if (flags
& LOOKUP_RCU
)
1803 inode
= d_inode(dentry
);
1804 task
= get_proc_task(inode
);
1807 pid_update_inode(task
, inode
);
1808 put_task_struct(task
);
1814 static inline bool proc_inode_is_dead(struct inode
*inode
)
1816 return !proc_pid(inode
)->tasks
[PIDTYPE_PID
].first
;
1819 int pid_delete_dentry(const struct dentry
*dentry
)
1821 /* Is the task we represent dead?
1822 * If so, then don't put the dentry on the lru list,
1823 * kill it immediately.
1825 return proc_inode_is_dead(d_inode(dentry
));
1828 const struct dentry_operations pid_dentry_operations
=
1830 .d_revalidate
= pid_revalidate
,
1831 .d_delete
= pid_delete_dentry
,
1837 * Fill a directory entry.
1839 * If possible create the dcache entry and derive our inode number and
1840 * file type from dcache entry.
1842 * Since all of the proc inode numbers are dynamically generated, the inode
1843 * numbers do not exist until the inode is cache. This means creating the
1844 * the dcache entry in readdir is necessary to keep the inode numbers
1845 * reported by readdir in sync with the inode numbers reported
1848 bool proc_fill_cache(struct file
*file
, struct dir_context
*ctx
,
1849 const char *name
, int len
,
1850 instantiate_t instantiate
, struct task_struct
*task
, const void *ptr
)
1852 struct dentry
*child
, *dir
= file
->f_path
.dentry
;
1853 struct qstr qname
= QSTR_INIT(name
, len
);
1854 struct inode
*inode
;
1855 unsigned type
= DT_UNKNOWN
;
1858 child
= d_hash_and_lookup(dir
, &qname
);
1860 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq
);
1861 child
= d_alloc_parallel(dir
, &qname
, &wq
);
1863 goto end_instantiate
;
1864 if (d_in_lookup(child
)) {
1866 res
= instantiate(child
, task
, ptr
);
1867 d_lookup_done(child
);
1869 goto end_instantiate
;
1870 if (unlikely(res
)) {
1876 inode
= d_inode(child
);
1878 type
= inode
->i_mode
>> 12;
1881 return dir_emit(ctx
, name
, len
, ino
, type
);
1885 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1886 * which represent vma start and end addresses.
1888 static int dname_to_vma_addr(struct dentry
*dentry
,
1889 unsigned long *start
, unsigned long *end
)
1891 const char *str
= dentry
->d_name
.name
;
1892 unsigned long long sval
, eval
;
1895 if (str
[0] == '0' && str
[1] != '-')
1897 len
= _parse_integer(str
, 16, &sval
);
1898 if (len
& KSTRTOX_OVERFLOW
)
1900 if (sval
!= (unsigned long)sval
)
1908 if (str
[0] == '0' && str
[1])
1910 len
= _parse_integer(str
, 16, &eval
);
1911 if (len
& KSTRTOX_OVERFLOW
)
1913 if (eval
!= (unsigned long)eval
)
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
->vm_file
->f_path
;
2004 up_read(&mm
->mmap_sem
);
2012 struct map_files_info
{
2013 unsigned long start
;
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
,
2043 static struct dentry
*
2044 proc_map_files_instantiate(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(dentry
->d_sb
, task
, S_IFLNK
|
2052 ((mode
& FMODE_READ
) ? S_IRUSR
: 0) |
2053 ((mode
& FMODE_WRITE
) ? S_IWUSR
: 0));
2055 return ERR_PTR(-ENOENT
);
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 return d_splice_alias(inode
, dentry
);
2067 static struct dentry
*proc_map_files_lookup(struct inode
*dir
,
2068 struct dentry
*dentry
, unsigned int flags
)
2070 unsigned long vm_start
, vm_end
;
2071 struct vm_area_struct
*vma
;
2072 struct task_struct
*task
;
2073 struct dentry
*result
;
2074 struct mm_struct
*mm
;
2076 result
= ERR_PTR(-ENOENT
);
2077 task
= get_proc_task(dir
);
2081 result
= ERR_PTR(-EACCES
);
2082 if (!ptrace_may_access(task
, PTRACE_MODE_READ_FSCREDS
))
2085 result
= ERR_PTR(-ENOENT
);
2086 if (dname_to_vma_addr(dentry
, &vm_start
, &vm_end
))
2089 mm
= get_task_mm(task
);
2093 down_read(&mm
->mmap_sem
);
2094 vma
= find_exact_vma(mm
, vm_start
, vm_end
);
2099 result
= proc_map_files_instantiate(dentry
, task
,
2100 (void *)(unsigned long)vma
->vm_file
->f_mode
);
2103 up_read(&mm
->mmap_sem
);
2106 put_task_struct(task
);
2111 static const struct inode_operations proc_map_files_inode_operations
= {
2112 .lookup
= proc_map_files_lookup
,
2113 .permission
= proc_fd_permission
,
2114 .setattr
= proc_setattr
,
2118 proc_map_files_readdir(struct file
*file
, struct dir_context
*ctx
)
2120 struct vm_area_struct
*vma
;
2121 struct task_struct
*task
;
2122 struct mm_struct
*mm
;
2123 unsigned long nr_files
, pos
, i
;
2124 struct flex_array
*fa
= NULL
;
2125 struct map_files_info info
;
2126 struct map_files_info
*p
;
2130 task
= get_proc_task(file_inode(file
));
2135 if (!ptrace_may_access(task
, PTRACE_MODE_READ_FSCREDS
))
2139 if (!dir_emit_dots(file
, ctx
))
2142 mm
= get_task_mm(task
);
2145 down_read(&mm
->mmap_sem
);
2150 * We need two passes here:
2152 * 1) Collect vmas of mapped files with mmap_sem taken
2153 * 2) Release mmap_sem and instantiate entries
2155 * otherwise we get lockdep complained, since filldir()
2156 * routine might require mmap_sem taken in might_fault().
2159 for (vma
= mm
->mmap
, pos
= 2; vma
; vma
= vma
->vm_next
) {
2160 if (vma
->vm_file
&& ++pos
> ctx
->pos
)
2165 fa
= flex_array_alloc(sizeof(info
), nr_files
,
2167 if (!fa
|| flex_array_prealloc(fa
, 0, nr_files
,
2171 flex_array_free(fa
);
2172 up_read(&mm
->mmap_sem
);
2176 for (i
= 0, vma
= mm
->mmap
, pos
= 2; vma
;
2177 vma
= vma
->vm_next
) {
2180 if (++pos
<= ctx
->pos
)
2183 info
.start
= vma
->vm_start
;
2184 info
.end
= vma
->vm_end
;
2185 info
.mode
= vma
->vm_file
->f_mode
;
2186 if (flex_array_put(fa
, i
++, &info
, GFP_KERNEL
))
2190 up_read(&mm
->mmap_sem
);
2193 for (i
= 0; i
< nr_files
; i
++) {
2194 char buf
[4 * sizeof(long) + 2]; /* max: %lx-%lx\0 */
2197 p
= flex_array_get(fa
, i
);
2198 len
= snprintf(buf
, sizeof(buf
), "%lx-%lx", p
->start
, p
->end
);
2199 if (!proc_fill_cache(file
, ctx
,
2201 proc_map_files_instantiate
,
2203 (void *)(unsigned long)p
->mode
))
2208 flex_array_free(fa
);
2211 put_task_struct(task
);
2216 static const struct file_operations proc_map_files_operations
= {
2217 .read
= generic_read_dir
,
2218 .iterate_shared
= proc_map_files_readdir
,
2219 .llseek
= generic_file_llseek
,
2222 #if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
2223 struct timers_private
{
2225 struct task_struct
*task
;
2226 struct sighand_struct
*sighand
;
2227 struct pid_namespace
*ns
;
2228 unsigned long flags
;
2231 static void *timers_start(struct seq_file
*m
, loff_t
*pos
)
2233 struct timers_private
*tp
= m
->private;
2235 tp
->task
= get_pid_task(tp
->pid
, PIDTYPE_PID
);
2237 return ERR_PTR(-ESRCH
);
2239 tp
->sighand
= lock_task_sighand(tp
->task
, &tp
->flags
);
2241 return ERR_PTR(-ESRCH
);
2243 return seq_list_start(&tp
->task
->signal
->posix_timers
, *pos
);
2246 static void *timers_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
2248 struct timers_private
*tp
= m
->private;
2249 return seq_list_next(v
, &tp
->task
->signal
->posix_timers
, pos
);
2252 static void timers_stop(struct seq_file
*m
, void *v
)
2254 struct timers_private
*tp
= m
->private;
2257 unlock_task_sighand(tp
->task
, &tp
->flags
);
2262 put_task_struct(tp
->task
);
2267 static int show_timer(struct seq_file
*m
, void *v
)
2269 struct k_itimer
*timer
;
2270 struct timers_private
*tp
= m
->private;
2272 static const char * const nstr
[] = {
2273 [SIGEV_SIGNAL
] = "signal",
2274 [SIGEV_NONE
] = "none",
2275 [SIGEV_THREAD
] = "thread",
2278 timer
= list_entry((struct list_head
*)v
, struct k_itimer
, list
);
2279 notify
= timer
->it_sigev_notify
;
2281 seq_printf(m
, "ID: %d\n", timer
->it_id
);
2282 seq_printf(m
, "signal: %d/%px\n",
2283 timer
->sigq
->info
.si_signo
,
2284 timer
->sigq
->info
.si_value
.sival_ptr
);
2285 seq_printf(m
, "notify: %s/%s.%d\n",
2286 nstr
[notify
& ~SIGEV_THREAD_ID
],
2287 (notify
& SIGEV_THREAD_ID
) ? "tid" : "pid",
2288 pid_nr_ns(timer
->it_pid
, tp
->ns
));
2289 seq_printf(m
, "ClockID: %d\n", timer
->it_clock
);
2294 static const struct seq_operations proc_timers_seq_ops
= {
2295 .start
= timers_start
,
2296 .next
= timers_next
,
2297 .stop
= timers_stop
,
2301 static int proc_timers_open(struct inode
*inode
, struct file
*file
)
2303 struct timers_private
*tp
;
2305 tp
= __seq_open_private(file
, &proc_timers_seq_ops
,
2306 sizeof(struct timers_private
));
2310 tp
->pid
= proc_pid(inode
);
2311 tp
->ns
= proc_pid_ns(inode
);
2315 static const struct file_operations proc_timers_operations
= {
2316 .open
= proc_timers_open
,
2318 .llseek
= seq_lseek
,
2319 .release
= seq_release_private
,
2323 static ssize_t
timerslack_ns_write(struct file
*file
, const char __user
*buf
,
2324 size_t count
, loff_t
*offset
)
2326 struct inode
*inode
= file_inode(file
);
2327 struct task_struct
*p
;
2331 err
= kstrtoull_from_user(buf
, count
, 10, &slack_ns
);
2335 p
= get_proc_task(inode
);
2340 if (!capable(CAP_SYS_NICE
)) {
2345 err
= security_task_setscheduler(p
);
2354 p
->timer_slack_ns
= p
->default_timer_slack_ns
;
2356 p
->timer_slack_ns
= slack_ns
;
2365 static int timerslack_ns_show(struct seq_file
*m
, void *v
)
2367 struct inode
*inode
= m
->private;
2368 struct task_struct
*p
;
2371 p
= get_proc_task(inode
);
2377 if (!capable(CAP_SYS_NICE
)) {
2381 err
= security_task_getscheduler(p
);
2387 seq_printf(m
, "%llu\n", p
->timer_slack_ns
);
2396 static int timerslack_ns_open(struct inode
*inode
, struct file
*filp
)
2398 return single_open(filp
, timerslack_ns_show
, inode
);
2401 static const struct file_operations proc_pid_set_timerslack_ns_operations
= {
2402 .open
= timerslack_ns_open
,
2404 .write
= timerslack_ns_write
,
2405 .llseek
= seq_lseek
,
2406 .release
= single_release
,
2409 static struct dentry
*proc_pident_instantiate(struct dentry
*dentry
,
2410 struct task_struct
*task
, const void *ptr
)
2412 const struct pid_entry
*p
= ptr
;
2413 struct inode
*inode
;
2414 struct proc_inode
*ei
;
2416 inode
= proc_pid_make_inode(dentry
->d_sb
, task
, p
->mode
);
2418 return ERR_PTR(-ENOENT
);
2421 if (S_ISDIR(inode
->i_mode
))
2422 set_nlink(inode
, 2); /* Use getattr to fix if necessary */
2424 inode
->i_op
= p
->iop
;
2426 inode
->i_fop
= p
->fop
;
2428 pid_update_inode(task
, inode
);
2429 d_set_d_op(dentry
, &pid_dentry_operations
);
2430 return d_splice_alias(inode
, dentry
);
2433 static struct dentry
*proc_pident_lookup(struct inode
*dir
,
2434 struct dentry
*dentry
,
2435 const struct pid_entry
*ents
,
2438 struct task_struct
*task
= get_proc_task(dir
);
2439 const struct pid_entry
*p
, *last
;
2440 struct dentry
*res
= ERR_PTR(-ENOENT
);
2446 * Yes, it does not scale. And it should not. Don't add
2447 * new entries into /proc/<tgid>/ without very good reasons.
2449 last
= &ents
[nents
];
2450 for (p
= ents
; p
< last
; p
++) {
2451 if (p
->len
!= dentry
->d_name
.len
)
2453 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
))
2459 res
= proc_pident_instantiate(dentry
, task
, p
);
2461 put_task_struct(task
);
2466 static int proc_pident_readdir(struct file
*file
, struct dir_context
*ctx
,
2467 const struct pid_entry
*ents
, unsigned int nents
)
2469 struct task_struct
*task
= get_proc_task(file_inode(file
));
2470 const struct pid_entry
*p
;
2475 if (!dir_emit_dots(file
, ctx
))
2478 if (ctx
->pos
>= nents
+ 2)
2481 for (p
= ents
+ (ctx
->pos
- 2); p
< ents
+ nents
; p
++) {
2482 if (!proc_fill_cache(file
, ctx
, p
->name
, p
->len
,
2483 proc_pident_instantiate
, task
, p
))
2488 put_task_struct(task
);
2492 #ifdef CONFIG_SECURITY
2493 static ssize_t
proc_pid_attr_read(struct file
* file
, char __user
* buf
,
2494 size_t count
, loff_t
*ppos
)
2496 struct inode
* inode
= file_inode(file
);
2499 struct task_struct
*task
= get_proc_task(inode
);
2504 length
= security_getprocattr(task
,
2505 (char*)file
->f_path
.dentry
->d_name
.name
,
2507 put_task_struct(task
);
2509 length
= simple_read_from_buffer(buf
, count
, ppos
, p
, length
);
2514 static ssize_t
proc_pid_attr_write(struct file
* file
, const char __user
* buf
,
2515 size_t count
, loff_t
*ppos
)
2517 struct inode
* inode
= file_inode(file
);
2520 struct task_struct
*task
= get_proc_task(inode
);
2526 /* A task may only write its own attributes. */
2528 if (current
!= task
)
2531 if (count
> PAGE_SIZE
)
2534 /* No partial writes. */
2539 page
= memdup_user(buf
, count
);
2541 length
= PTR_ERR(page
);
2545 /* Guard against adverse ptrace interaction */
2546 length
= mutex_lock_interruptible(¤t
->signal
->cred_guard_mutex
);
2550 length
= security_setprocattr(file
->f_path
.dentry
->d_name
.name
,
2552 mutex_unlock(¤t
->signal
->cred_guard_mutex
);
2556 put_task_struct(task
);
2561 static const struct file_operations proc_pid_attr_operations
= {
2562 .read
= proc_pid_attr_read
,
2563 .write
= proc_pid_attr_write
,
2564 .llseek
= generic_file_llseek
,
2567 static const struct pid_entry attr_dir_stuff
[] = {
2568 REG("current", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2569 REG("prev", S_IRUGO
, proc_pid_attr_operations
),
2570 REG("exec", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2571 REG("fscreate", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2572 REG("keycreate", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2573 REG("sockcreate", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2576 static int proc_attr_dir_readdir(struct file
*file
, struct dir_context
*ctx
)
2578 return proc_pident_readdir(file
, ctx
,
2579 attr_dir_stuff
, ARRAY_SIZE(attr_dir_stuff
));
2582 static const struct file_operations proc_attr_dir_operations
= {
2583 .read
= generic_read_dir
,
2584 .iterate_shared
= proc_attr_dir_readdir
,
2585 .llseek
= generic_file_llseek
,
2588 static struct dentry
*proc_attr_dir_lookup(struct inode
*dir
,
2589 struct dentry
*dentry
, unsigned int flags
)
2591 return proc_pident_lookup(dir
, dentry
,
2592 attr_dir_stuff
, ARRAY_SIZE(attr_dir_stuff
));
2595 static const struct inode_operations proc_attr_dir_inode_operations
= {
2596 .lookup
= proc_attr_dir_lookup
,
2597 .getattr
= pid_getattr
,
2598 .setattr
= proc_setattr
,
2603 #ifdef CONFIG_ELF_CORE
2604 static ssize_t
proc_coredump_filter_read(struct file
*file
, char __user
*buf
,
2605 size_t count
, loff_t
*ppos
)
2607 struct task_struct
*task
= get_proc_task(file_inode(file
));
2608 struct mm_struct
*mm
;
2609 char buffer
[PROC_NUMBUF
];
2617 mm
= get_task_mm(task
);
2619 len
= snprintf(buffer
, sizeof(buffer
), "%08lx\n",
2620 ((mm
->flags
& MMF_DUMP_FILTER_MASK
) >>
2621 MMF_DUMP_FILTER_SHIFT
));
2623 ret
= simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
2626 put_task_struct(task
);
2631 static ssize_t
proc_coredump_filter_write(struct file
*file
,
2632 const char __user
*buf
,
2636 struct task_struct
*task
;
2637 struct mm_struct
*mm
;
2643 ret
= kstrtouint_from_user(buf
, count
, 0, &val
);
2648 task
= get_proc_task(file_inode(file
));
2652 mm
= get_task_mm(task
);
2657 for (i
= 0, mask
= 1; i
< MMF_DUMP_FILTER_BITS
; i
++, mask
<<= 1) {
2659 set_bit(i
+ MMF_DUMP_FILTER_SHIFT
, &mm
->flags
);
2661 clear_bit(i
+ MMF_DUMP_FILTER_SHIFT
, &mm
->flags
);
2666 put_task_struct(task
);
2673 static const struct file_operations proc_coredump_filter_operations
= {
2674 .read
= proc_coredump_filter_read
,
2675 .write
= proc_coredump_filter_write
,
2676 .llseek
= generic_file_llseek
,
2680 #ifdef CONFIG_TASK_IO_ACCOUNTING
2681 static int do_io_accounting(struct task_struct
*task
, struct seq_file
*m
, int whole
)
2683 struct task_io_accounting acct
= task
->ioac
;
2684 unsigned long flags
;
2687 result
= mutex_lock_killable(&task
->signal
->cred_guard_mutex
);
2691 if (!ptrace_may_access(task
, PTRACE_MODE_READ_FSCREDS
)) {
2696 if (whole
&& lock_task_sighand(task
, &flags
)) {
2697 struct task_struct
*t
= task
;
2699 task_io_accounting_add(&acct
, &task
->signal
->ioac
);
2700 while_each_thread(task
, t
)
2701 task_io_accounting_add(&acct
, &t
->ioac
);
2703 unlock_task_sighand(task
, &flags
);
2710 "read_bytes: %llu\n"
2711 "write_bytes: %llu\n"
2712 "cancelled_write_bytes: %llu\n",
2713 (unsigned long long)acct
.rchar
,
2714 (unsigned long long)acct
.wchar
,
2715 (unsigned long long)acct
.syscr
,
2716 (unsigned long long)acct
.syscw
,
2717 (unsigned long long)acct
.read_bytes
,
2718 (unsigned long long)acct
.write_bytes
,
2719 (unsigned long long)acct
.cancelled_write_bytes
);
2723 mutex_unlock(&task
->signal
->cred_guard_mutex
);
2727 static int proc_tid_io_accounting(struct seq_file
*m
, struct pid_namespace
*ns
,
2728 struct pid
*pid
, struct task_struct
*task
)
2730 return do_io_accounting(task
, m
, 0);
2733 static int proc_tgid_io_accounting(struct seq_file
*m
, struct pid_namespace
*ns
,
2734 struct pid
*pid
, struct task_struct
*task
)
2736 return do_io_accounting(task
, m
, 1);
2738 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2740 #ifdef CONFIG_USER_NS
2741 static int proc_id_map_open(struct inode
*inode
, struct file
*file
,
2742 const struct seq_operations
*seq_ops
)
2744 struct user_namespace
*ns
= NULL
;
2745 struct task_struct
*task
;
2746 struct seq_file
*seq
;
2749 task
= get_proc_task(inode
);
2752 ns
= get_user_ns(task_cred_xxx(task
, user_ns
));
2754 put_task_struct(task
);
2759 ret
= seq_open(file
, seq_ops
);
2763 seq
= file
->private_data
;
2773 static int proc_id_map_release(struct inode
*inode
, struct file
*file
)
2775 struct seq_file
*seq
= file
->private_data
;
2776 struct user_namespace
*ns
= seq
->private;
2778 return seq_release(inode
, file
);
2781 static int proc_uid_map_open(struct inode
*inode
, struct file
*file
)
2783 return proc_id_map_open(inode
, file
, &proc_uid_seq_operations
);
2786 static int proc_gid_map_open(struct inode
*inode
, struct file
*file
)
2788 return proc_id_map_open(inode
, file
, &proc_gid_seq_operations
);
2791 static int proc_projid_map_open(struct inode
*inode
, struct file
*file
)
2793 return proc_id_map_open(inode
, file
, &proc_projid_seq_operations
);
2796 static const struct file_operations proc_uid_map_operations
= {
2797 .open
= proc_uid_map_open
,
2798 .write
= proc_uid_map_write
,
2800 .llseek
= seq_lseek
,
2801 .release
= proc_id_map_release
,
2804 static const struct file_operations proc_gid_map_operations
= {
2805 .open
= proc_gid_map_open
,
2806 .write
= proc_gid_map_write
,
2808 .llseek
= seq_lseek
,
2809 .release
= proc_id_map_release
,
2812 static const struct file_operations proc_projid_map_operations
= {
2813 .open
= proc_projid_map_open
,
2814 .write
= proc_projid_map_write
,
2816 .llseek
= seq_lseek
,
2817 .release
= proc_id_map_release
,
2820 static int proc_setgroups_open(struct inode
*inode
, struct file
*file
)
2822 struct user_namespace
*ns
= NULL
;
2823 struct task_struct
*task
;
2827 task
= get_proc_task(inode
);
2830 ns
= get_user_ns(task_cred_xxx(task
, user_ns
));
2832 put_task_struct(task
);
2837 if (file
->f_mode
& FMODE_WRITE
) {
2839 if (!ns_capable(ns
, CAP_SYS_ADMIN
))
2843 ret
= single_open(file
, &proc_setgroups_show
, ns
);
2854 static int proc_setgroups_release(struct inode
*inode
, struct file
*file
)
2856 struct seq_file
*seq
= file
->private_data
;
2857 struct user_namespace
*ns
= seq
->private;
2858 int ret
= single_release(inode
, file
);
2863 static const struct file_operations proc_setgroups_operations
= {
2864 .open
= proc_setgroups_open
,
2865 .write
= proc_setgroups_write
,
2867 .llseek
= seq_lseek
,
2868 .release
= proc_setgroups_release
,
2870 #endif /* CONFIG_USER_NS */
2872 static int proc_pid_personality(struct seq_file
*m
, struct pid_namespace
*ns
,
2873 struct pid
*pid
, struct task_struct
*task
)
2875 int err
= lock_trace(task
);
2877 seq_printf(m
, "%08x\n", task
->personality
);
2883 #ifdef CONFIG_LIVEPATCH
2884 static int proc_pid_patch_state(struct seq_file
*m
, struct pid_namespace
*ns
,
2885 struct pid
*pid
, struct task_struct
*task
)
2887 seq_printf(m
, "%d\n", task
->patch_state
);
2890 #endif /* CONFIG_LIVEPATCH */
2895 static const struct file_operations proc_task_operations
;
2896 static const struct inode_operations proc_task_inode_operations
;
2898 static const struct pid_entry tgid_base_stuff
[] = {
2899 DIR("task", S_IRUGO
|S_IXUGO
, proc_task_inode_operations
, proc_task_operations
),
2900 DIR("fd", S_IRUSR
|S_IXUSR
, proc_fd_inode_operations
, proc_fd_operations
),
2901 DIR("map_files", S_IRUSR
|S_IXUSR
, proc_map_files_inode_operations
, proc_map_files_operations
),
2902 DIR("fdinfo", S_IRUSR
|S_IXUSR
, proc_fdinfo_inode_operations
, proc_fdinfo_operations
),
2903 DIR("ns", S_IRUSR
|S_IXUGO
, proc_ns_dir_inode_operations
, proc_ns_dir_operations
),
2905 DIR("net", S_IRUGO
|S_IXUGO
, proc_net_inode_operations
, proc_net_operations
),
2907 REG("environ", S_IRUSR
, proc_environ_operations
),
2908 REG("auxv", S_IRUSR
, proc_auxv_operations
),
2909 ONE("status", S_IRUGO
, proc_pid_status
),
2910 ONE("personality", S_IRUSR
, proc_pid_personality
),
2911 ONE("limits", S_IRUGO
, proc_pid_limits
),
2912 #ifdef CONFIG_SCHED_DEBUG
2913 REG("sched", S_IRUGO
|S_IWUSR
, proc_pid_sched_operations
),
2915 #ifdef CONFIG_SCHED_AUTOGROUP
2916 REG("autogroup", S_IRUGO
|S_IWUSR
, proc_pid_sched_autogroup_operations
),
2918 REG("comm", S_IRUGO
|S_IWUSR
, proc_pid_set_comm_operations
),
2919 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2920 ONE("syscall", S_IRUSR
, proc_pid_syscall
),
2922 REG("cmdline", S_IRUGO
, proc_pid_cmdline_ops
),
2923 ONE("stat", S_IRUGO
, proc_tgid_stat
),
2924 ONE("statm", S_IRUGO
, proc_pid_statm
),
2925 REG("maps", S_IRUGO
, proc_pid_maps_operations
),
2927 REG("numa_maps", S_IRUGO
, proc_pid_numa_maps_operations
),
2929 REG("mem", S_IRUSR
|S_IWUSR
, proc_mem_operations
),
2930 LNK("cwd", proc_cwd_link
),
2931 LNK("root", proc_root_link
),
2932 LNK("exe", proc_exe_link
),
2933 REG("mounts", S_IRUGO
, proc_mounts_operations
),
2934 REG("mountinfo", S_IRUGO
, proc_mountinfo_operations
),
2935 REG("mountstats", S_IRUSR
, proc_mountstats_operations
),
2936 #ifdef CONFIG_PROC_PAGE_MONITOR
2937 REG("clear_refs", S_IWUSR
, proc_clear_refs_operations
),
2938 REG("smaps", S_IRUGO
, proc_pid_smaps_operations
),
2939 REG("smaps_rollup", S_IRUGO
, proc_pid_smaps_rollup_operations
),
2940 REG("pagemap", S_IRUSR
, proc_pagemap_operations
),
2942 #ifdef CONFIG_SECURITY
2943 DIR("attr", S_IRUGO
|S_IXUGO
, proc_attr_dir_inode_operations
, proc_attr_dir_operations
),
2945 #ifdef CONFIG_KALLSYMS
2946 ONE("wchan", S_IRUGO
, proc_pid_wchan
),
2948 #ifdef CONFIG_STACKTRACE
2949 ONE("stack", S_IRUSR
, proc_pid_stack
),
2951 #ifdef CONFIG_SCHED_INFO
2952 ONE("schedstat", S_IRUGO
, proc_pid_schedstat
),
2954 #ifdef CONFIG_LATENCYTOP
2955 REG("latency", S_IRUGO
, proc_lstats_operations
),
2957 #ifdef CONFIG_PROC_PID_CPUSET
2958 ONE("cpuset", S_IRUGO
, proc_cpuset_show
),
2960 #ifdef CONFIG_CGROUPS
2961 ONE("cgroup", S_IRUGO
, proc_cgroup_show
),
2963 ONE("oom_score", S_IRUGO
, proc_oom_score
),
2964 REG("oom_adj", S_IRUGO
|S_IWUSR
, proc_oom_adj_operations
),
2965 REG("oom_score_adj", S_IRUGO
|S_IWUSR
, proc_oom_score_adj_operations
),
2966 #ifdef CONFIG_AUDITSYSCALL
2967 REG("loginuid", S_IWUSR
|S_IRUGO
, proc_loginuid_operations
),
2968 REG("sessionid", S_IRUGO
, proc_sessionid_operations
),
2970 #ifdef CONFIG_FAULT_INJECTION
2971 REG("make-it-fail", S_IRUGO
|S_IWUSR
, proc_fault_inject_operations
),
2972 REG("fail-nth", 0644, proc_fail_nth_operations
),
2974 #ifdef CONFIG_ELF_CORE
2975 REG("coredump_filter", S_IRUGO
|S_IWUSR
, proc_coredump_filter_operations
),
2977 #ifdef CONFIG_TASK_IO_ACCOUNTING
2978 ONE("io", S_IRUSR
, proc_tgid_io_accounting
),
2980 #ifdef CONFIG_USER_NS
2981 REG("uid_map", S_IRUGO
|S_IWUSR
, proc_uid_map_operations
),
2982 REG("gid_map", S_IRUGO
|S_IWUSR
, proc_gid_map_operations
),
2983 REG("projid_map", S_IRUGO
|S_IWUSR
, proc_projid_map_operations
),
2984 REG("setgroups", S_IRUGO
|S_IWUSR
, proc_setgroups_operations
),
2986 #if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
2987 REG("timers", S_IRUGO
, proc_timers_operations
),
2989 REG("timerslack_ns", S_IRUGO
|S_IWUGO
, proc_pid_set_timerslack_ns_operations
),
2990 #ifdef CONFIG_LIVEPATCH
2991 ONE("patch_state", S_IRUSR
, proc_pid_patch_state
),
2995 static int proc_tgid_base_readdir(struct file
*file
, struct dir_context
*ctx
)
2997 return proc_pident_readdir(file
, ctx
,
2998 tgid_base_stuff
, ARRAY_SIZE(tgid_base_stuff
));
3001 static const struct file_operations proc_tgid_base_operations
= {
3002 .read
= generic_read_dir
,
3003 .iterate_shared
= proc_tgid_base_readdir
,
3004 .llseek
= generic_file_llseek
,
3007 static struct dentry
*proc_tgid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, unsigned int flags
)
3009 return proc_pident_lookup(dir
, dentry
,
3010 tgid_base_stuff
, ARRAY_SIZE(tgid_base_stuff
));
3013 static const struct inode_operations proc_tgid_base_inode_operations
= {
3014 .lookup
= proc_tgid_base_lookup
,
3015 .getattr
= pid_getattr
,
3016 .setattr
= proc_setattr
,
3017 .permission
= proc_pid_permission
,
3020 static void proc_flush_task_mnt(struct vfsmount
*mnt
, pid_t pid
, pid_t tgid
)
3022 struct dentry
*dentry
, *leader
, *dir
;
3027 name
.len
= snprintf(buf
, sizeof(buf
), "%u", pid
);
3028 /* no ->d_hash() rejects on procfs */
3029 dentry
= d_hash_and_lookup(mnt
->mnt_root
, &name
);
3031 d_invalidate(dentry
);
3039 name
.len
= snprintf(buf
, sizeof(buf
), "%u", tgid
);
3040 leader
= d_hash_and_lookup(mnt
->mnt_root
, &name
);
3045 name
.len
= strlen(name
.name
);
3046 dir
= d_hash_and_lookup(leader
, &name
);
3048 goto out_put_leader
;
3051 name
.len
= snprintf(buf
, sizeof(buf
), "%u", pid
);
3052 dentry
= d_hash_and_lookup(dir
, &name
);
3054 d_invalidate(dentry
);
3066 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
3067 * @task: task that should be flushed.
3069 * When flushing dentries from proc, one needs to flush them from global
3070 * proc (proc_mnt) and from all the namespaces' procs this task was seen
3071 * in. This call is supposed to do all of this job.
3073 * Looks in the dcache for
3075 * /proc/@tgid/task/@pid
3076 * if either directory is present flushes it and all of it'ts children
3079 * It is safe and reasonable to cache /proc entries for a task until
3080 * that task exits. After that they just clog up the dcache with
3081 * useless entries, possibly causing useful dcache entries to be
3082 * flushed instead. This routine is proved to flush those useless
3083 * dcache entries at process exit time.
3085 * NOTE: This routine is just an optimization so it does not guarantee
3086 * that no dcache entries will exist at process exit time it
3087 * just makes it very unlikely that any will persist.
3090 void proc_flush_task(struct task_struct
*task
)
3093 struct pid
*pid
, *tgid
;
3096 pid
= task_pid(task
);
3097 tgid
= task_tgid(task
);
3099 for (i
= 0; i
<= pid
->level
; i
++) {
3100 upid
= &pid
->numbers
[i
];
3101 proc_flush_task_mnt(upid
->ns
->proc_mnt
, upid
->nr
,
3102 tgid
->numbers
[i
].nr
);
3106 static struct dentry
*proc_pid_instantiate(struct dentry
* dentry
,
3107 struct task_struct
*task
, const void *ptr
)
3109 struct inode
*inode
;
3111 inode
= proc_pid_make_inode(dentry
->d_sb
, task
, S_IFDIR
| S_IRUGO
| S_IXUGO
);
3113 return ERR_PTR(-ENOENT
);
3115 inode
->i_op
= &proc_tgid_base_inode_operations
;
3116 inode
->i_fop
= &proc_tgid_base_operations
;
3117 inode
->i_flags
|=S_IMMUTABLE
;
3119 set_nlink(inode
, nlink_tgid
);
3120 pid_update_inode(task
, inode
);
3122 d_set_d_op(dentry
, &pid_dentry_operations
);
3123 return d_splice_alias(inode
, dentry
);
3126 struct dentry
*proc_pid_lookup(struct inode
*dir
, struct dentry
* dentry
, unsigned int flags
)
3128 struct task_struct
*task
;
3130 struct pid_namespace
*ns
;
3131 struct dentry
*result
= ERR_PTR(-ENOENT
);
3133 tgid
= name_to_int(&dentry
->d_name
);
3137 ns
= dentry
->d_sb
->s_fs_info
;
3139 task
= find_task_by_pid_ns(tgid
, ns
);
3141 get_task_struct(task
);
3146 result
= proc_pid_instantiate(dentry
, task
, NULL
);
3147 put_task_struct(task
);
3153 * Find the first task with tgid >= tgid
3158 struct task_struct
*task
;
3160 static struct tgid_iter
next_tgid(struct pid_namespace
*ns
, struct tgid_iter iter
)
3165 put_task_struct(iter
.task
);
3169 pid
= find_ge_pid(iter
.tgid
, ns
);
3171 iter
.tgid
= pid_nr_ns(pid
, ns
);
3172 iter
.task
= pid_task(pid
, PIDTYPE_PID
);
3173 /* What we to know is if the pid we have find is the
3174 * pid of a thread_group_leader. Testing for task
3175 * being a thread_group_leader is the obvious thing
3176 * todo but there is a window when it fails, due to
3177 * the pid transfer logic in de_thread.
3179 * So we perform the straight forward test of seeing
3180 * if the pid we have found is the pid of a thread
3181 * group leader, and don't worry if the task we have
3182 * found doesn't happen to be a thread group leader.
3183 * As we don't care in the case of readdir.
3185 if (!iter
.task
|| !has_group_leader_pid(iter
.task
)) {
3189 get_task_struct(iter
.task
);
3195 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
3197 /* for the /proc/ directory itself, after non-process stuff has been done */
3198 int proc_pid_readdir(struct file
*file
, struct dir_context
*ctx
)
3200 struct tgid_iter iter
;
3201 struct pid_namespace
*ns
= proc_pid_ns(file_inode(file
));
3202 loff_t pos
= ctx
->pos
;
3204 if (pos
>= PID_MAX_LIMIT
+ TGID_OFFSET
)
3207 if (pos
== TGID_OFFSET
- 2) {
3208 struct inode
*inode
= d_inode(ns
->proc_self
);
3209 if (!dir_emit(ctx
, "self", 4, inode
->i_ino
, DT_LNK
))
3211 ctx
->pos
= pos
= pos
+ 1;
3213 if (pos
== TGID_OFFSET
- 1) {
3214 struct inode
*inode
= d_inode(ns
->proc_thread_self
);
3215 if (!dir_emit(ctx
, "thread-self", 11, inode
->i_ino
, DT_LNK
))
3217 ctx
->pos
= pos
= pos
+ 1;
3219 iter
.tgid
= pos
- TGID_OFFSET
;
3221 for (iter
= next_tgid(ns
, iter
);
3223 iter
.tgid
+= 1, iter
= next_tgid(ns
, iter
)) {
3228 if (!has_pid_permissions(ns
, iter
.task
, HIDEPID_INVISIBLE
))
3231 len
= snprintf(name
, sizeof(name
), "%u", iter
.tgid
);
3232 ctx
->pos
= iter
.tgid
+ TGID_OFFSET
;
3233 if (!proc_fill_cache(file
, ctx
, name
, len
,
3234 proc_pid_instantiate
, iter
.task
, NULL
)) {
3235 put_task_struct(iter
.task
);
3239 ctx
->pos
= PID_MAX_LIMIT
+ TGID_OFFSET
;
3244 * proc_tid_comm_permission is a special permission function exclusively
3245 * used for the node /proc/<pid>/task/<tid>/comm.
3246 * It bypasses generic permission checks in the case where a task of the same
3247 * task group attempts to access the node.
3248 * The rationale behind this is that glibc and bionic access this node for
3249 * cross thread naming (pthread_set/getname_np(!self)). However, if
3250 * PR_SET_DUMPABLE gets set to 0 this node among others becomes uid=0 gid=0,
3251 * which locks out the cross thread naming implementation.
3252 * This function makes sure that the node is always accessible for members of
3253 * same thread group.
3255 static int proc_tid_comm_permission(struct inode
*inode
, int mask
)
3257 bool is_same_tgroup
;
3258 struct task_struct
*task
;
3260 task
= get_proc_task(inode
);
3263 is_same_tgroup
= same_thread_group(current
, task
);
3264 put_task_struct(task
);
3266 if (likely(is_same_tgroup
&& !(mask
& MAY_EXEC
))) {
3267 /* This file (/proc/<pid>/task/<tid>/comm) can always be
3268 * read or written by the members of the corresponding
3274 return generic_permission(inode
, mask
);
3277 static const struct inode_operations proc_tid_comm_inode_operations
= {
3278 .permission
= proc_tid_comm_permission
,
3284 static const struct pid_entry tid_base_stuff
[] = {
3285 DIR("fd", S_IRUSR
|S_IXUSR
, proc_fd_inode_operations
, proc_fd_operations
),
3286 DIR("fdinfo", S_IRUSR
|S_IXUSR
, proc_fdinfo_inode_operations
, proc_fdinfo_operations
),
3287 DIR("ns", S_IRUSR
|S_IXUGO
, proc_ns_dir_inode_operations
, proc_ns_dir_operations
),
3289 DIR("net", S_IRUGO
|S_IXUGO
, proc_net_inode_operations
, proc_net_operations
),
3291 REG("environ", S_IRUSR
, proc_environ_operations
),
3292 REG("auxv", S_IRUSR
, proc_auxv_operations
),
3293 ONE("status", S_IRUGO
, proc_pid_status
),
3294 ONE("personality", S_IRUSR
, proc_pid_personality
),
3295 ONE("limits", S_IRUGO
, proc_pid_limits
),
3296 #ifdef CONFIG_SCHED_DEBUG
3297 REG("sched", S_IRUGO
|S_IWUSR
, proc_pid_sched_operations
),
3299 NOD("comm", S_IFREG
|S_IRUGO
|S_IWUSR
,
3300 &proc_tid_comm_inode_operations
,
3301 &proc_pid_set_comm_operations
, {}),
3302 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3303 ONE("syscall", S_IRUSR
, proc_pid_syscall
),
3305 REG("cmdline", S_IRUGO
, proc_pid_cmdline_ops
),
3306 ONE("stat", S_IRUGO
, proc_tid_stat
),
3307 ONE("statm", S_IRUGO
, proc_pid_statm
),
3308 REG("maps", S_IRUGO
, proc_tid_maps_operations
),
3309 #ifdef CONFIG_PROC_CHILDREN
3310 REG("children", S_IRUGO
, proc_tid_children_operations
),
3313 REG("numa_maps", S_IRUGO
, proc_tid_numa_maps_operations
),
3315 REG("mem", S_IRUSR
|S_IWUSR
, proc_mem_operations
),
3316 LNK("cwd", proc_cwd_link
),
3317 LNK("root", proc_root_link
),
3318 LNK("exe", proc_exe_link
),
3319 REG("mounts", S_IRUGO
, proc_mounts_operations
),
3320 REG("mountinfo", S_IRUGO
, proc_mountinfo_operations
),
3321 #ifdef CONFIG_PROC_PAGE_MONITOR
3322 REG("clear_refs", S_IWUSR
, proc_clear_refs_operations
),
3323 REG("smaps", S_IRUGO
, proc_tid_smaps_operations
),
3324 REG("smaps_rollup", S_IRUGO
, proc_pid_smaps_rollup_operations
),
3325 REG("pagemap", S_IRUSR
, proc_pagemap_operations
),
3327 #ifdef CONFIG_SECURITY
3328 DIR("attr", S_IRUGO
|S_IXUGO
, proc_attr_dir_inode_operations
, proc_attr_dir_operations
),
3330 #ifdef CONFIG_KALLSYMS
3331 ONE("wchan", S_IRUGO
, proc_pid_wchan
),
3333 #ifdef CONFIG_STACKTRACE
3334 ONE("stack", S_IRUSR
, proc_pid_stack
),
3336 #ifdef CONFIG_SCHED_INFO
3337 ONE("schedstat", S_IRUGO
, proc_pid_schedstat
),
3339 #ifdef CONFIG_LATENCYTOP
3340 REG("latency", S_IRUGO
, proc_lstats_operations
),
3342 #ifdef CONFIG_PROC_PID_CPUSET
3343 ONE("cpuset", S_IRUGO
, proc_cpuset_show
),
3345 #ifdef CONFIG_CGROUPS
3346 ONE("cgroup", S_IRUGO
, proc_cgroup_show
),
3348 ONE("oom_score", S_IRUGO
, proc_oom_score
),
3349 REG("oom_adj", S_IRUGO
|S_IWUSR
, proc_oom_adj_operations
),
3350 REG("oom_score_adj", S_IRUGO
|S_IWUSR
, proc_oom_score_adj_operations
),
3351 #ifdef CONFIG_AUDITSYSCALL
3352 REG("loginuid", S_IWUSR
|S_IRUGO
, proc_loginuid_operations
),
3353 REG("sessionid", S_IRUGO
, proc_sessionid_operations
),
3355 #ifdef CONFIG_FAULT_INJECTION
3356 REG("make-it-fail", S_IRUGO
|S_IWUSR
, proc_fault_inject_operations
),
3357 REG("fail-nth", 0644, proc_fail_nth_operations
),
3359 #ifdef CONFIG_TASK_IO_ACCOUNTING
3360 ONE("io", S_IRUSR
, proc_tid_io_accounting
),
3362 #ifdef CONFIG_USER_NS
3363 REG("uid_map", S_IRUGO
|S_IWUSR
, proc_uid_map_operations
),
3364 REG("gid_map", S_IRUGO
|S_IWUSR
, proc_gid_map_operations
),
3365 REG("projid_map", S_IRUGO
|S_IWUSR
, proc_projid_map_operations
),
3366 REG("setgroups", S_IRUGO
|S_IWUSR
, proc_setgroups_operations
),
3368 #ifdef CONFIG_LIVEPATCH
3369 ONE("patch_state", S_IRUSR
, proc_pid_patch_state
),
3373 static int proc_tid_base_readdir(struct file
*file
, struct dir_context
*ctx
)
3375 return proc_pident_readdir(file
, ctx
,
3376 tid_base_stuff
, ARRAY_SIZE(tid_base_stuff
));
3379 static struct dentry
*proc_tid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, unsigned int flags
)
3381 return proc_pident_lookup(dir
, dentry
,
3382 tid_base_stuff
, ARRAY_SIZE(tid_base_stuff
));
3385 static const struct file_operations proc_tid_base_operations
= {
3386 .read
= generic_read_dir
,
3387 .iterate_shared
= proc_tid_base_readdir
,
3388 .llseek
= generic_file_llseek
,
3391 static const struct inode_operations proc_tid_base_inode_operations
= {
3392 .lookup
= proc_tid_base_lookup
,
3393 .getattr
= pid_getattr
,
3394 .setattr
= proc_setattr
,
3397 static struct dentry
*proc_task_instantiate(struct dentry
*dentry
,
3398 struct task_struct
*task
, const void *ptr
)
3400 struct inode
*inode
;
3401 inode
= proc_pid_make_inode(dentry
->d_sb
, task
, S_IFDIR
| S_IRUGO
| S_IXUGO
);
3403 return ERR_PTR(-ENOENT
);
3405 inode
->i_op
= &proc_tid_base_inode_operations
;
3406 inode
->i_fop
= &proc_tid_base_operations
;
3407 inode
->i_flags
|= S_IMMUTABLE
;
3409 set_nlink(inode
, nlink_tid
);
3410 pid_update_inode(task
, inode
);
3412 d_set_d_op(dentry
, &pid_dentry_operations
);
3413 return d_splice_alias(inode
, dentry
);
3416 static struct dentry
*proc_task_lookup(struct inode
*dir
, struct dentry
* dentry
, unsigned int flags
)
3418 struct task_struct
*task
;
3419 struct task_struct
*leader
= get_proc_task(dir
);
3421 struct pid_namespace
*ns
;
3422 struct dentry
*result
= ERR_PTR(-ENOENT
);
3427 tid
= name_to_int(&dentry
->d_name
);
3431 ns
= dentry
->d_sb
->s_fs_info
;
3433 task
= find_task_by_pid_ns(tid
, ns
);
3435 get_task_struct(task
);
3439 if (!same_thread_group(leader
, task
))
3442 result
= proc_task_instantiate(dentry
, task
, NULL
);
3444 put_task_struct(task
);
3446 put_task_struct(leader
);
3452 * Find the first tid of a thread group to return to user space.
3454 * Usually this is just the thread group leader, but if the users
3455 * buffer was too small or there was a seek into the middle of the
3456 * directory we have more work todo.
3458 * In the case of a short read we start with find_task_by_pid.
3460 * In the case of a seek we start with the leader and walk nr
3463 static struct task_struct
*first_tid(struct pid
*pid
, int tid
, loff_t f_pos
,
3464 struct pid_namespace
*ns
)
3466 struct task_struct
*pos
, *task
;
3467 unsigned long nr
= f_pos
;
3469 if (nr
!= f_pos
) /* 32bit overflow? */
3473 task
= pid_task(pid
, PIDTYPE_PID
);
3477 /* Attempt to start with the tid of a thread */
3479 pos
= find_task_by_pid_ns(tid
, ns
);
3480 if (pos
&& same_thread_group(pos
, task
))
3484 /* If nr exceeds the number of threads there is nothing todo */
3485 if (nr
>= get_nr_threads(task
))
3488 /* If we haven't found our starting place yet start
3489 * with the leader and walk nr threads forward.
3491 pos
= task
= task
->group_leader
;
3495 } while_each_thread(task
, pos
);
3500 get_task_struct(pos
);
3507 * Find the next thread in the thread list.
3508 * Return NULL if there is an error or no next thread.
3510 * The reference to the input task_struct is released.
3512 static struct task_struct
*next_tid(struct task_struct
*start
)
3514 struct task_struct
*pos
= NULL
;
3516 if (pid_alive(start
)) {
3517 pos
= next_thread(start
);
3518 if (thread_group_leader(pos
))
3521 get_task_struct(pos
);
3524 put_task_struct(start
);
3528 /* for the /proc/TGID/task/ directories */
3529 static int proc_task_readdir(struct file
*file
, struct dir_context
*ctx
)
3531 struct inode
*inode
= file_inode(file
);
3532 struct task_struct
*task
;
3533 struct pid_namespace
*ns
;
3536 if (proc_inode_is_dead(inode
))
3539 if (!dir_emit_dots(file
, ctx
))
3542 /* f_version caches the tgid value that the last readdir call couldn't
3543 * return. lseek aka telldir automagically resets f_version to 0.
3545 ns
= proc_pid_ns(inode
);
3546 tid
= (int)file
->f_version
;
3547 file
->f_version
= 0;
3548 for (task
= first_tid(proc_pid(inode
), tid
, ctx
->pos
- 2, ns
);
3550 task
= next_tid(task
), ctx
->pos
++) {
3553 tid
= task_pid_nr_ns(task
, ns
);
3554 len
= snprintf(name
, sizeof(name
), "%u", tid
);
3555 if (!proc_fill_cache(file
, ctx
, name
, len
,
3556 proc_task_instantiate
, task
, NULL
)) {
3557 /* returning this tgid failed, save it as the first
3558 * pid for the next readir call */
3559 file
->f_version
= (u64
)tid
;
3560 put_task_struct(task
);
3568 static int proc_task_getattr(const struct path
*path
, struct kstat
*stat
,
3569 u32 request_mask
, unsigned int query_flags
)
3571 struct inode
*inode
= d_inode(path
->dentry
);
3572 struct task_struct
*p
= get_proc_task(inode
);
3573 generic_fillattr(inode
, stat
);
3576 stat
->nlink
+= get_nr_threads(p
);
3583 static const struct inode_operations proc_task_inode_operations
= {
3584 .lookup
= proc_task_lookup
,
3585 .getattr
= proc_task_getattr
,
3586 .setattr
= proc_setattr
,
3587 .permission
= proc_pid_permission
,
3590 static const struct file_operations proc_task_operations
= {
3591 .read
= generic_read_dir
,
3592 .iterate_shared
= proc_task_readdir
,
3593 .llseek
= generic_file_llseek
,
3596 void __init
set_proc_pid_nlink(void)
3598 nlink_tid
= pid_entry_nlink(tid_base_stuff
, ARRAY_SIZE(tid_base_stuff
));
3599 nlink_tgid
= pid_entry_nlink(tgid_base_stuff
, ARRAY_SIZE(tgid_base_stuff
));