4 * Copyright (C) 1991, 1992 Linus Torvalds
6 * proc base directory handling functions
8 * 1999, Al Viro. Rewritten. Now it covers the whole per-process part.
9 * Instead of using magical inumbers to determine the kind of object
10 * we allocate and fill in-core inodes upon lookup. They don't even
11 * go into icache. We cache the reference to task_struct upon lookup too.
12 * Eventually it should become a filesystem in its own. We don't use the
13 * rest of procfs anymore.
19 * Bruna Moreira <bruna.moreira@indt.org.br>
20 * Edjard Mota <edjard.mota@indt.org.br>
21 * Ilias Biris <ilias.biris@indt.org.br>
22 * Mauricio Lin <mauricio.lin@indt.org.br>
24 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
26 * A new process specific entry (smaps) included in /proc. It shows the
27 * size of rss for each memory area. The maps entry lacks information
28 * about physical memory size (rss) for each mapped file, i.e.,
29 * rss information for executables and library files.
30 * This additional information is useful for any tools that need to know
31 * about physical memory consumption for a process specific library.
35 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36 * Pud inclusion in the page table walking.
40 * 10LE Instituto Nokia de Tecnologia - INdT:
41 * A better way to walks through the page table as suggested by Hugh Dickins.
43 * Simo Piiroinen <simo.piiroinen@nokia.com>:
44 * Smaps information related to shared, private, clean and dirty pages.
46 * Paul Mundt <paul.mundt@nokia.com>:
47 * Overall revision about smaps.
50 #include <linux/uaccess.h>
52 #include <linux/errno.h>
53 #include <linux/time.h>
54 #include <linux/proc_fs.h>
55 #include <linux/stat.h>
56 #include <linux/task_io_accounting_ops.h>
57 #include <linux/init.h>
58 #include <linux/capability.h>
59 #include <linux/file.h>
60 #include <linux/fdtable.h>
61 #include <linux/string.h>
62 #include <linux/seq_file.h>
63 #include <linux/namei.h>
64 #include <linux/mnt_namespace.h>
66 #include <linux/swap.h>
67 #include <linux/rcupdate.h>
68 #include <linux/kallsyms.h>
69 #include <linux/stacktrace.h>
70 #include <linux/resource.h>
71 #include <linux/module.h>
72 #include <linux/mount.h>
73 #include <linux/security.h>
74 #include <linux/ptrace.h>
75 #include <linux/tracehook.h>
76 #include <linux/printk.h>
77 #include <linux/cgroup.h>
78 #include <linux/cpuset.h>
79 #include <linux/audit.h>
80 #include <linux/poll.h>
81 #include <linux/nsproxy.h>
82 #include <linux/oom.h>
83 #include <linux/elf.h>
84 #include <linux/pid_namespace.h>
85 #include <linux/user_namespace.h>
86 #include <linux/fs_struct.h>
87 #include <linux/slab.h>
88 #include <linux/sched/autogroup.h>
89 #include <linux/sched/mm.h>
90 #include <linux/sched/coredump.h>
91 #include <linux/sched/debug.h>
92 #include <linux/sched/stat.h>
93 #include <linux/flex_array.h>
94 #include <linux/posix-timers.h>
95 #ifdef CONFIG_HARDWALL
96 #include <asm/hardwall.h>
98 #include <trace/events/oom.h>
103 * Implementing inode permission operations in /proc is almost
104 * certainly an error. Permission checks need to happen during
105 * each system call not at open time. The reason is that most of
106 * what we wish to check for permissions in /proc varies at runtime.
108 * The classic example of a problem is opening file descriptors
109 * in /proc for a task before it execs a suid executable.
113 static u8 nlink_tgid
;
119 const struct inode_operations
*iop
;
120 const struct file_operations
*fop
;
124 #define NOD(NAME, MODE, IOP, FOP, OP) { \
126 .len = sizeof(NAME) - 1, \
133 #define DIR(NAME, MODE, iops, fops) \
134 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
135 #define LNK(NAME, get_link) \
136 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
137 &proc_pid_link_inode_operations, NULL, \
138 { .proc_get_link = get_link } )
139 #define REG(NAME, MODE, fops) \
140 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
141 #define ONE(NAME, MODE, show) \
142 NOD(NAME, (S_IFREG|(MODE)), \
143 NULL, &proc_single_file_operations, \
144 { .proc_show = show } )
147 * Count the number of hardlinks for the pid_entry table, excluding the .
150 static unsigned int __init
pid_entry_nlink(const struct pid_entry
*entries
,
157 for (i
= 0; i
< n
; ++i
) {
158 if (S_ISDIR(entries
[i
].mode
))
165 static int get_task_root(struct task_struct
*task
, struct path
*root
)
167 int result
= -ENOENT
;
171 get_fs_root(task
->fs
, root
);
178 static int proc_cwd_link(struct dentry
*dentry
, struct path
*path
)
180 struct task_struct
*task
= get_proc_task(d_inode(dentry
));
181 int result
= -ENOENT
;
186 get_fs_pwd(task
->fs
, path
);
190 put_task_struct(task
);
195 static int proc_root_link(struct dentry
*dentry
, struct path
*path
)
197 struct task_struct
*task
= get_proc_task(d_inode(dentry
));
198 int result
= -ENOENT
;
201 result
= get_task_root(task
, path
);
202 put_task_struct(task
);
207 static ssize_t
proc_pid_cmdline_read(struct file
*file
, char __user
*buf
,
208 size_t _count
, loff_t
*pos
)
210 struct task_struct
*tsk
;
211 struct mm_struct
*mm
;
213 unsigned long count
= _count
;
214 unsigned long arg_start
, arg_end
, env_start
, env_end
;
215 unsigned long len1
, len2
, len
;
222 tsk
= get_proc_task(file_inode(file
));
225 mm
= get_task_mm(tsk
);
226 put_task_struct(tsk
);
229 /* Check if process spawned far enough to have cmdline. */
235 page
= (char *)__get_free_page(GFP_TEMPORARY
);
241 down_read(&mm
->mmap_sem
);
242 arg_start
= mm
->arg_start
;
243 arg_end
= mm
->arg_end
;
244 env_start
= mm
->env_start
;
245 env_end
= mm
->env_end
;
246 up_read(&mm
->mmap_sem
);
248 BUG_ON(arg_start
> arg_end
);
249 BUG_ON(env_start
> env_end
);
251 len1
= arg_end
- arg_start
;
252 len2
= env_end
- env_start
;
260 * Inherently racy -- command line shares address space
261 * with code and data.
263 rv
= access_remote_vm(mm
, arg_end
- 1, &c
, 1, 0);
270 /* Command line (set of strings) occupies whole ARGV. */
274 p
= arg_start
+ *pos
;
276 while (count
> 0 && len
> 0) {
280 _count
= min3(count
, len
, PAGE_SIZE
);
281 nr_read
= access_remote_vm(mm
, p
, page
, _count
, 0);
287 if (copy_to_user(buf
, page
, nr_read
)) {
300 * Command line (1 string) occupies ARGV and
307 { .p
= arg_start
, .len
= len1
},
308 { .p
= env_start
, .len
= len2
},
314 while (i
< 2 && pos1
>= cmdline
[i
].len
) {
315 pos1
-= cmdline
[i
].len
;
319 p
= cmdline
[i
].p
+ pos1
;
320 len
= cmdline
[i
].len
- pos1
;
321 while (count
> 0 && len
> 0) {
322 unsigned int _count
, l
;
326 _count
= min3(count
, len
, PAGE_SIZE
);
327 nr_read
= access_remote_vm(mm
, p
, page
, _count
, 0);
334 * Command line can be shorter than whole ARGV
335 * even if last "marker" byte says it is not.
338 l
= strnlen(page
, nr_read
);
344 if (copy_to_user(buf
, page
, nr_read
)) {
359 /* Only first chunk can be read partially. */
366 free_page((unsigned long)page
);
374 static const struct file_operations proc_pid_cmdline_ops
= {
375 .read
= proc_pid_cmdline_read
,
376 .llseek
= generic_file_llseek
,
379 #ifdef CONFIG_KALLSYMS
381 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
382 * Returns the resolved symbol. If that fails, simply return the address.
384 static int proc_pid_wchan(struct seq_file
*m
, struct pid_namespace
*ns
,
385 struct pid
*pid
, struct task_struct
*task
)
388 char symname
[KSYM_NAME_LEN
];
390 wchan
= get_wchan(task
);
392 if (wchan
&& ptrace_may_access(task
, PTRACE_MODE_READ_FSCREDS
)
393 && !lookup_symbol_name(wchan
, symname
))
394 seq_printf(m
, "%s", symname
);
400 #endif /* CONFIG_KALLSYMS */
402 static int lock_trace(struct task_struct
*task
)
404 int err
= mutex_lock_killable(&task
->signal
->cred_guard_mutex
);
407 if (!ptrace_may_access(task
, PTRACE_MODE_ATTACH_FSCREDS
)) {
408 mutex_unlock(&task
->signal
->cred_guard_mutex
);
414 static void unlock_trace(struct task_struct
*task
)
416 mutex_unlock(&task
->signal
->cred_guard_mutex
);
419 #ifdef CONFIG_STACKTRACE
421 #define MAX_STACK_TRACE_DEPTH 64
423 static int proc_pid_stack(struct seq_file
*m
, struct pid_namespace
*ns
,
424 struct pid
*pid
, struct task_struct
*task
)
426 struct stack_trace trace
;
427 unsigned long *entries
;
431 entries
= kmalloc(MAX_STACK_TRACE_DEPTH
* sizeof(*entries
), GFP_KERNEL
);
435 trace
.nr_entries
= 0;
436 trace
.max_entries
= MAX_STACK_TRACE_DEPTH
;
437 trace
.entries
= entries
;
440 err
= lock_trace(task
);
442 save_stack_trace_tsk(task
, &trace
);
444 for (i
= 0; i
< trace
.nr_entries
; i
++) {
445 seq_printf(m
, "[<%pK>] %pB\n",
446 (void *)entries
[i
], (void *)entries
[i
]);
456 #ifdef CONFIG_SCHED_INFO
458 * Provides /proc/PID/schedstat
460 static int proc_pid_schedstat(struct seq_file
*m
, struct pid_namespace
*ns
,
461 struct pid
*pid
, struct task_struct
*task
)
463 if (unlikely(!sched_info_on()))
464 seq_printf(m
, "0 0 0\n");
466 seq_printf(m
, "%llu %llu %lu\n",
467 (unsigned long long)task
->se
.sum_exec_runtime
,
468 (unsigned long long)task
->sched_info
.run_delay
,
469 task
->sched_info
.pcount
);
475 #ifdef CONFIG_LATENCYTOP
476 static int lstats_show_proc(struct seq_file
*m
, void *v
)
479 struct inode
*inode
= m
->private;
480 struct task_struct
*task
= get_proc_task(inode
);
484 seq_puts(m
, "Latency Top version : v0.1\n");
485 for (i
= 0; i
< 32; i
++) {
486 struct latency_record
*lr
= &task
->latency_record
[i
];
487 if (lr
->backtrace
[0]) {
489 seq_printf(m
, "%i %li %li",
490 lr
->count
, lr
->time
, lr
->max
);
491 for (q
= 0; q
< LT_BACKTRACEDEPTH
; q
++) {
492 unsigned long bt
= lr
->backtrace
[q
];
497 seq_printf(m
, " %ps", (void *)bt
);
503 put_task_struct(task
);
507 static int lstats_open(struct inode
*inode
, struct file
*file
)
509 return single_open(file
, lstats_show_proc
, inode
);
512 static ssize_t
lstats_write(struct file
*file
, const char __user
*buf
,
513 size_t count
, loff_t
*offs
)
515 struct task_struct
*task
= get_proc_task(file_inode(file
));
519 clear_all_latency_tracing(task
);
520 put_task_struct(task
);
525 static const struct file_operations proc_lstats_operations
= {
528 .write
= lstats_write
,
530 .release
= single_release
,
535 static int proc_oom_score(struct seq_file
*m
, struct pid_namespace
*ns
,
536 struct pid
*pid
, struct task_struct
*task
)
538 unsigned long totalpages
= totalram_pages
+ total_swap_pages
;
539 unsigned long points
= 0;
541 points
= oom_badness(task
, NULL
, NULL
, totalpages
) *
543 seq_printf(m
, "%lu\n", points
);
553 static const struct limit_names lnames
[RLIM_NLIMITS
] = {
554 [RLIMIT_CPU
] = {"Max cpu time", "seconds"},
555 [RLIMIT_FSIZE
] = {"Max file size", "bytes"},
556 [RLIMIT_DATA
] = {"Max data size", "bytes"},
557 [RLIMIT_STACK
] = {"Max stack size", "bytes"},
558 [RLIMIT_CORE
] = {"Max core file size", "bytes"},
559 [RLIMIT_RSS
] = {"Max resident set", "bytes"},
560 [RLIMIT_NPROC
] = {"Max processes", "processes"},
561 [RLIMIT_NOFILE
] = {"Max open files", "files"},
562 [RLIMIT_MEMLOCK
] = {"Max locked memory", "bytes"},
563 [RLIMIT_AS
] = {"Max address space", "bytes"},
564 [RLIMIT_LOCKS
] = {"Max file locks", "locks"},
565 [RLIMIT_SIGPENDING
] = {"Max pending signals", "signals"},
566 [RLIMIT_MSGQUEUE
] = {"Max msgqueue size", "bytes"},
567 [RLIMIT_NICE
] = {"Max nice priority", NULL
},
568 [RLIMIT_RTPRIO
] = {"Max realtime priority", NULL
},
569 [RLIMIT_RTTIME
] = {"Max realtime timeout", "us"},
572 /* Display limits for a process */
573 static int proc_pid_limits(struct seq_file
*m
, struct pid_namespace
*ns
,
574 struct pid
*pid
, struct task_struct
*task
)
579 struct rlimit rlim
[RLIM_NLIMITS
];
581 if (!lock_task_sighand(task
, &flags
))
583 memcpy(rlim
, task
->signal
->rlim
, sizeof(struct rlimit
) * RLIM_NLIMITS
);
584 unlock_task_sighand(task
, &flags
);
587 * print the file header
589 seq_printf(m
, "%-25s %-20s %-20s %-10s\n",
590 "Limit", "Soft Limit", "Hard Limit", "Units");
592 for (i
= 0; i
< RLIM_NLIMITS
; i
++) {
593 if (rlim
[i
].rlim_cur
== RLIM_INFINITY
)
594 seq_printf(m
, "%-25s %-20s ",
595 lnames
[i
].name
, "unlimited");
597 seq_printf(m
, "%-25s %-20lu ",
598 lnames
[i
].name
, rlim
[i
].rlim_cur
);
600 if (rlim
[i
].rlim_max
== RLIM_INFINITY
)
601 seq_printf(m
, "%-20s ", "unlimited");
603 seq_printf(m
, "%-20lu ", rlim
[i
].rlim_max
);
606 seq_printf(m
, "%-10s\n", lnames
[i
].unit
);
614 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
615 static int proc_pid_syscall(struct seq_file
*m
, struct pid_namespace
*ns
,
616 struct pid
*pid
, struct task_struct
*task
)
619 unsigned long args
[6], sp
, pc
;
622 res
= lock_trace(task
);
626 if (task_current_syscall(task
, &nr
, args
, 6, &sp
, &pc
))
627 seq_puts(m
, "running\n");
629 seq_printf(m
, "%ld 0x%lx 0x%lx\n", nr
, sp
, pc
);
632 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
634 args
[0], args
[1], args
[2], args
[3], args
[4], args
[5],
640 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
642 /************************************************************************/
643 /* Here the fs part begins */
644 /************************************************************************/
646 /* permission checks */
647 static int proc_fd_access_allowed(struct inode
*inode
)
649 struct task_struct
*task
;
651 /* Allow access to a task's file descriptors if it is us or we
652 * may use ptrace attach to the process and find out that
655 task
= get_proc_task(inode
);
657 allowed
= ptrace_may_access(task
, PTRACE_MODE_READ_FSCREDS
);
658 put_task_struct(task
);
663 int proc_setattr(struct dentry
*dentry
, struct iattr
*attr
)
666 struct inode
*inode
= d_inode(dentry
);
667 struct user_namespace
*s_user_ns
;
669 if (attr
->ia_valid
& ATTR_MODE
)
672 /* Don't let anyone mess with weird proc files */
673 s_user_ns
= inode
->i_sb
->s_user_ns
;
674 if (!kuid_has_mapping(s_user_ns
, inode
->i_uid
) ||
675 !kgid_has_mapping(s_user_ns
, inode
->i_gid
))
678 error
= setattr_prepare(dentry
, attr
);
682 setattr_copy(inode
, attr
);
683 mark_inode_dirty(inode
);
688 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
689 * or euid/egid (for hide_pid_min=2)?
691 static bool has_pid_permissions(struct pid_namespace
*pid
,
692 struct task_struct
*task
,
695 if (pid
->hide_pid
< hide_pid_min
)
697 if (in_group_p(pid
->pid_gid
))
699 return ptrace_may_access(task
, PTRACE_MODE_READ_FSCREDS
);
703 static int proc_pid_permission(struct inode
*inode
, int mask
)
705 struct pid_namespace
*pid
= inode
->i_sb
->s_fs_info
;
706 struct task_struct
*task
;
709 task
= get_proc_task(inode
);
712 has_perms
= has_pid_permissions(pid
, task
, HIDEPID_NO_ACCESS
);
713 put_task_struct(task
);
716 if (pid
->hide_pid
== HIDEPID_INVISIBLE
) {
718 * Let's make getdents(), stat(), and open()
719 * consistent with each other. If a process
720 * may not stat() a file, it shouldn't be seen
728 return generic_permission(inode
, mask
);
733 static const struct inode_operations proc_def_inode_operations
= {
734 .setattr
= proc_setattr
,
737 static int proc_single_show(struct seq_file
*m
, void *v
)
739 struct inode
*inode
= m
->private;
740 struct pid_namespace
*ns
;
742 struct task_struct
*task
;
745 ns
= inode
->i_sb
->s_fs_info
;
746 pid
= proc_pid(inode
);
747 task
= get_pid_task(pid
, PIDTYPE_PID
);
751 ret
= PROC_I(inode
)->op
.proc_show(m
, ns
, pid
, task
);
753 put_task_struct(task
);
757 static int proc_single_open(struct inode
*inode
, struct file
*filp
)
759 return single_open(filp
, proc_single_show
, inode
);
762 static const struct file_operations proc_single_file_operations
= {
763 .open
= proc_single_open
,
766 .release
= single_release
,
770 struct mm_struct
*proc_mem_open(struct inode
*inode
, unsigned int mode
)
772 struct task_struct
*task
= get_proc_task(inode
);
773 struct mm_struct
*mm
= ERR_PTR(-ESRCH
);
776 mm
= mm_access(task
, mode
| PTRACE_MODE_FSCREDS
);
777 put_task_struct(task
);
779 if (!IS_ERR_OR_NULL(mm
)) {
780 /* ensure this mm_struct can't be freed */
782 /* but do not pin its memory */
790 static int __mem_open(struct inode
*inode
, struct file
*file
, unsigned int mode
)
792 struct mm_struct
*mm
= proc_mem_open(inode
, mode
);
797 file
->private_data
= mm
;
801 static int mem_open(struct inode
*inode
, struct file
*file
)
803 int ret
= __mem_open(inode
, file
, PTRACE_MODE_ATTACH
);
805 /* OK to pass negative loff_t, we can catch out-of-range */
806 file
->f_mode
|= FMODE_UNSIGNED_OFFSET
;
811 static ssize_t
mem_rw(struct file
*file
, char __user
*buf
,
812 size_t count
, loff_t
*ppos
, int write
)
814 struct mm_struct
*mm
= file
->private_data
;
815 unsigned long addr
= *ppos
;
823 page
= (char *)__get_free_page(GFP_TEMPORARY
);
828 if (!mmget_not_zero(mm
))
831 flags
= FOLL_FORCE
| (write
? FOLL_WRITE
: 0);
834 int this_len
= min_t(int, count
, PAGE_SIZE
);
836 if (write
&& copy_from_user(page
, buf
, this_len
)) {
841 this_len
= access_remote_vm(mm
, addr
, page
, this_len
, flags
);
848 if (!write
&& copy_to_user(buf
, page
, this_len
)) {
862 free_page((unsigned long) page
);
866 static ssize_t
mem_read(struct file
*file
, char __user
*buf
,
867 size_t count
, loff_t
*ppos
)
869 return mem_rw(file
, buf
, count
, ppos
, 0);
872 static ssize_t
mem_write(struct file
*file
, const char __user
*buf
,
873 size_t count
, loff_t
*ppos
)
875 return mem_rw(file
, (char __user
*)buf
, count
, ppos
, 1);
878 loff_t
mem_lseek(struct file
*file
, loff_t offset
, int orig
)
882 file
->f_pos
= offset
;
885 file
->f_pos
+= offset
;
890 force_successful_syscall_return();
894 static int mem_release(struct inode
*inode
, struct file
*file
)
896 struct mm_struct
*mm
= file
->private_data
;
902 static const struct file_operations proc_mem_operations
= {
907 .release
= mem_release
,
910 static int environ_open(struct inode
*inode
, struct file
*file
)
912 return __mem_open(inode
, file
, PTRACE_MODE_READ
);
915 static ssize_t
environ_read(struct file
*file
, char __user
*buf
,
916 size_t count
, loff_t
*ppos
)
919 unsigned long src
= *ppos
;
921 struct mm_struct
*mm
= file
->private_data
;
922 unsigned long env_start
, env_end
;
924 /* Ensure the process spawned far enough to have an environment. */
925 if (!mm
|| !mm
->env_end
)
928 page
= (char *)__get_free_page(GFP_TEMPORARY
);
933 if (!mmget_not_zero(mm
))
936 down_read(&mm
->mmap_sem
);
937 env_start
= mm
->env_start
;
938 env_end
= mm
->env_end
;
939 up_read(&mm
->mmap_sem
);
942 size_t this_len
, max_len
;
945 if (src
>= (env_end
- env_start
))
948 this_len
= env_end
- (env_start
+ src
);
950 max_len
= min_t(size_t, PAGE_SIZE
, count
);
951 this_len
= min(max_len
, this_len
);
953 retval
= access_remote_vm(mm
, (env_start
+ src
), page
, this_len
, 0);
960 if (copy_to_user(buf
, page
, retval
)) {
974 free_page((unsigned long) page
);
978 static const struct file_operations proc_environ_operations
= {
979 .open
= environ_open
,
980 .read
= environ_read
,
981 .llseek
= generic_file_llseek
,
982 .release
= mem_release
,
985 static int auxv_open(struct inode
*inode
, struct file
*file
)
987 return __mem_open(inode
, file
, PTRACE_MODE_READ_FSCREDS
);
990 static ssize_t
auxv_read(struct file
*file
, char __user
*buf
,
991 size_t count
, loff_t
*ppos
)
993 struct mm_struct
*mm
= file
->private_data
;
994 unsigned int nwords
= 0;
1000 } while (mm
->saved_auxv
[nwords
- 2] != 0); /* AT_NULL */
1001 return simple_read_from_buffer(buf
, count
, ppos
, mm
->saved_auxv
,
1002 nwords
* sizeof(mm
->saved_auxv
[0]));
1005 static const struct file_operations proc_auxv_operations
= {
1008 .llseek
= generic_file_llseek
,
1009 .release
= mem_release
,
1012 static ssize_t
oom_adj_read(struct file
*file
, char __user
*buf
, size_t count
,
1015 struct task_struct
*task
= get_proc_task(file_inode(file
));
1016 char buffer
[PROC_NUMBUF
];
1017 int oom_adj
= OOM_ADJUST_MIN
;
1022 if (task
->signal
->oom_score_adj
== OOM_SCORE_ADJ_MAX
)
1023 oom_adj
= OOM_ADJUST_MAX
;
1025 oom_adj
= (task
->signal
->oom_score_adj
* -OOM_DISABLE
) /
1027 put_task_struct(task
);
1028 len
= snprintf(buffer
, sizeof(buffer
), "%d\n", oom_adj
);
1029 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
1032 static int __set_oom_adj(struct file
*file
, int oom_adj
, bool legacy
)
1034 static DEFINE_MUTEX(oom_adj_mutex
);
1035 struct mm_struct
*mm
= NULL
;
1036 struct task_struct
*task
;
1039 task
= get_proc_task(file_inode(file
));
1043 mutex_lock(&oom_adj_mutex
);
1045 if (oom_adj
< task
->signal
->oom_score_adj
&&
1046 !capable(CAP_SYS_RESOURCE
)) {
1051 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
1052 * /proc/pid/oom_score_adj instead.
1054 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
1055 current
->comm
, task_pid_nr(current
), task_pid_nr(task
),
1058 if ((short)oom_adj
< task
->signal
->oom_score_adj_min
&&
1059 !capable(CAP_SYS_RESOURCE
)) {
1066 * Make sure we will check other processes sharing the mm if this is
1067 * not vfrok which wants its own oom_score_adj.
1068 * pin the mm so it doesn't go away and get reused after task_unlock
1070 if (!task
->vfork_done
) {
1071 struct task_struct
*p
= find_lock_task_mm(task
);
1074 if (atomic_read(&p
->mm
->mm_users
) > 1) {
1082 task
->signal
->oom_score_adj
= oom_adj
;
1083 if (!legacy
&& has_capability_noaudit(current
, CAP_SYS_RESOURCE
))
1084 task
->signal
->oom_score_adj_min
= (short)oom_adj
;
1085 trace_oom_score_adj_update(task
);
1088 struct task_struct
*p
;
1091 for_each_process(p
) {
1092 if (same_thread_group(task
, p
))
1095 /* do not touch kernel threads or the global init */
1096 if (p
->flags
& PF_KTHREAD
|| is_global_init(p
))
1100 if (!p
->vfork_done
&& process_shares_mm(p
, mm
)) {
1101 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",
1102 task_pid_nr(p
), p
->comm
,
1103 p
->signal
->oom_score_adj
, oom_adj
,
1104 task_pid_nr(task
), task
->comm
);
1105 p
->signal
->oom_score_adj
= oom_adj
;
1106 if (!legacy
&& has_capability_noaudit(current
, CAP_SYS_RESOURCE
))
1107 p
->signal
->oom_score_adj_min
= (short)oom_adj
;
1115 mutex_unlock(&oom_adj_mutex
);
1116 put_task_struct(task
);
1121 * /proc/pid/oom_adj exists solely for backwards compatibility with previous
1122 * kernels. The effective policy is defined by oom_score_adj, which has a
1123 * different scale: oom_adj grew exponentially and oom_score_adj grows linearly.
1124 * Values written to oom_adj are simply mapped linearly to oom_score_adj.
1125 * Processes that become oom disabled via oom_adj will still be oom disabled
1126 * with this implementation.
1128 * oom_adj cannot be removed since existing userspace binaries use it.
1130 static ssize_t
oom_adj_write(struct file
*file
, const char __user
*buf
,
1131 size_t count
, loff_t
*ppos
)
1133 char buffer
[PROC_NUMBUF
];
1137 memset(buffer
, 0, sizeof(buffer
));
1138 if (count
> sizeof(buffer
) - 1)
1139 count
= sizeof(buffer
) - 1;
1140 if (copy_from_user(buffer
, buf
, count
)) {
1145 err
= kstrtoint(strstrip(buffer
), 0, &oom_adj
);
1148 if ((oom_adj
< OOM_ADJUST_MIN
|| oom_adj
> OOM_ADJUST_MAX
) &&
1149 oom_adj
!= OOM_DISABLE
) {
1155 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1156 * value is always attainable.
1158 if (oom_adj
== OOM_ADJUST_MAX
)
1159 oom_adj
= OOM_SCORE_ADJ_MAX
;
1161 oom_adj
= (oom_adj
* OOM_SCORE_ADJ_MAX
) / -OOM_DISABLE
;
1163 err
= __set_oom_adj(file
, oom_adj
, true);
1165 return err
< 0 ? err
: count
;
1168 static const struct file_operations proc_oom_adj_operations
= {
1169 .read
= oom_adj_read
,
1170 .write
= oom_adj_write
,
1171 .llseek
= generic_file_llseek
,
1174 static ssize_t
oom_score_adj_read(struct file
*file
, char __user
*buf
,
1175 size_t count
, loff_t
*ppos
)
1177 struct task_struct
*task
= get_proc_task(file_inode(file
));
1178 char buffer
[PROC_NUMBUF
];
1179 short oom_score_adj
= OOM_SCORE_ADJ_MIN
;
1184 oom_score_adj
= task
->signal
->oom_score_adj
;
1185 put_task_struct(task
);
1186 len
= snprintf(buffer
, sizeof(buffer
), "%hd\n", oom_score_adj
);
1187 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
1190 static ssize_t
oom_score_adj_write(struct file
*file
, const char __user
*buf
,
1191 size_t count
, loff_t
*ppos
)
1193 char buffer
[PROC_NUMBUF
];
1197 memset(buffer
, 0, sizeof(buffer
));
1198 if (count
> sizeof(buffer
) - 1)
1199 count
= sizeof(buffer
) - 1;
1200 if (copy_from_user(buffer
, buf
, count
)) {
1205 err
= kstrtoint(strstrip(buffer
), 0, &oom_score_adj
);
1208 if (oom_score_adj
< OOM_SCORE_ADJ_MIN
||
1209 oom_score_adj
> OOM_SCORE_ADJ_MAX
) {
1214 err
= __set_oom_adj(file
, oom_score_adj
, false);
1216 return err
< 0 ? err
: count
;
1219 static const struct file_operations proc_oom_score_adj_operations
= {
1220 .read
= oom_score_adj_read
,
1221 .write
= oom_score_adj_write
,
1222 .llseek
= default_llseek
,
1225 #ifdef CONFIG_AUDITSYSCALL
1226 #define TMPBUFLEN 11
1227 static ssize_t
proc_loginuid_read(struct file
* file
, char __user
* buf
,
1228 size_t count
, loff_t
*ppos
)
1230 struct inode
* inode
= file_inode(file
);
1231 struct task_struct
*task
= get_proc_task(inode
);
1233 char tmpbuf
[TMPBUFLEN
];
1237 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
1238 from_kuid(file
->f_cred
->user_ns
,
1239 audit_get_loginuid(task
)));
1240 put_task_struct(task
);
1241 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1244 static ssize_t
proc_loginuid_write(struct file
* file
, const char __user
* buf
,
1245 size_t count
, loff_t
*ppos
)
1247 struct inode
* inode
= file_inode(file
);
1253 if (current
!= pid_task(proc_pid(inode
), PIDTYPE_PID
)) {
1260 /* No partial writes. */
1264 rv
= kstrtou32_from_user(buf
, count
, 10, &loginuid
);
1268 /* is userspace tring to explicitly UNSET the loginuid? */
1269 if (loginuid
== AUDIT_UID_UNSET
) {
1270 kloginuid
= INVALID_UID
;
1272 kloginuid
= make_kuid(file
->f_cred
->user_ns
, loginuid
);
1273 if (!uid_valid(kloginuid
))
1277 rv
= audit_set_loginuid(kloginuid
);
1283 static const struct file_operations proc_loginuid_operations
= {
1284 .read
= proc_loginuid_read
,
1285 .write
= proc_loginuid_write
,
1286 .llseek
= generic_file_llseek
,
1289 static ssize_t
proc_sessionid_read(struct file
* file
, char __user
* buf
,
1290 size_t count
, loff_t
*ppos
)
1292 struct inode
* inode
= file_inode(file
);
1293 struct task_struct
*task
= get_proc_task(inode
);
1295 char tmpbuf
[TMPBUFLEN
];
1299 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
1300 audit_get_sessionid(task
));
1301 put_task_struct(task
);
1302 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1305 static const struct file_operations proc_sessionid_operations
= {
1306 .read
= proc_sessionid_read
,
1307 .llseek
= generic_file_llseek
,
1311 #ifdef CONFIG_FAULT_INJECTION
1312 static ssize_t
proc_fault_inject_read(struct file
* file
, char __user
* buf
,
1313 size_t count
, loff_t
*ppos
)
1315 struct task_struct
*task
= get_proc_task(file_inode(file
));
1316 char buffer
[PROC_NUMBUF
];
1322 make_it_fail
= task
->make_it_fail
;
1323 put_task_struct(task
);
1325 len
= snprintf(buffer
, sizeof(buffer
), "%i\n", make_it_fail
);
1327 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
1330 static ssize_t
proc_fault_inject_write(struct file
* file
,
1331 const char __user
* buf
, size_t count
, loff_t
*ppos
)
1333 struct task_struct
*task
;
1334 char buffer
[PROC_NUMBUF
];
1338 if (!capable(CAP_SYS_RESOURCE
))
1340 memset(buffer
, 0, sizeof(buffer
));
1341 if (count
> sizeof(buffer
) - 1)
1342 count
= sizeof(buffer
) - 1;
1343 if (copy_from_user(buffer
, buf
, count
))
1345 rv
= kstrtoint(strstrip(buffer
), 0, &make_it_fail
);
1348 if (make_it_fail
< 0 || make_it_fail
> 1)
1351 task
= get_proc_task(file_inode(file
));
1354 task
->make_it_fail
= make_it_fail
;
1355 put_task_struct(task
);
1360 static const struct file_operations proc_fault_inject_operations
= {
1361 .read
= proc_fault_inject_read
,
1362 .write
= proc_fault_inject_write
,
1363 .llseek
= generic_file_llseek
,
1366 static ssize_t
proc_fail_nth_write(struct file
*file
, const char __user
*buf
,
1367 size_t count
, loff_t
*ppos
)
1369 struct task_struct
*task
;
1373 err
= kstrtouint_from_user(buf
, count
, 0, &n
);
1377 task
= get_proc_task(file_inode(file
));
1380 WRITE_ONCE(task
->fail_nth
, n
);
1381 put_task_struct(task
);
1386 static ssize_t
proc_fail_nth_read(struct file
*file
, char __user
*buf
,
1387 size_t count
, loff_t
*ppos
)
1389 struct task_struct
*task
;
1390 char numbuf
[PROC_NUMBUF
];
1393 task
= get_proc_task(file_inode(file
));
1396 len
= snprintf(numbuf
, sizeof(numbuf
), "%u\n",
1397 READ_ONCE(task
->fail_nth
));
1398 len
= simple_read_from_buffer(buf
, count
, ppos
, numbuf
, len
);
1399 put_task_struct(task
);
1404 static const struct file_operations proc_fail_nth_operations
= {
1405 .read
= proc_fail_nth_read
,
1406 .write
= proc_fail_nth_write
,
1411 #ifdef CONFIG_SCHED_DEBUG
1413 * Print out various scheduling related per-task fields:
1415 static int sched_show(struct seq_file
*m
, void *v
)
1417 struct inode
*inode
= m
->private;
1418 struct task_struct
*p
;
1420 p
= get_proc_task(inode
);
1423 proc_sched_show_task(p
, m
);
1431 sched_write(struct file
*file
, const char __user
*buf
,
1432 size_t count
, loff_t
*offset
)
1434 struct inode
*inode
= file_inode(file
);
1435 struct task_struct
*p
;
1437 p
= get_proc_task(inode
);
1440 proc_sched_set_task(p
);
1447 static int sched_open(struct inode
*inode
, struct file
*filp
)
1449 return single_open(filp
, sched_show
, inode
);
1452 static const struct file_operations proc_pid_sched_operations
= {
1455 .write
= sched_write
,
1456 .llseek
= seq_lseek
,
1457 .release
= single_release
,
1462 #ifdef CONFIG_SCHED_AUTOGROUP
1464 * Print out autogroup related information:
1466 static int sched_autogroup_show(struct seq_file
*m
, void *v
)
1468 struct inode
*inode
= m
->private;
1469 struct task_struct
*p
;
1471 p
= get_proc_task(inode
);
1474 proc_sched_autogroup_show_task(p
, m
);
1482 sched_autogroup_write(struct file
*file
, const char __user
*buf
,
1483 size_t count
, loff_t
*offset
)
1485 struct inode
*inode
= file_inode(file
);
1486 struct task_struct
*p
;
1487 char buffer
[PROC_NUMBUF
];
1491 memset(buffer
, 0, sizeof(buffer
));
1492 if (count
> sizeof(buffer
) - 1)
1493 count
= sizeof(buffer
) - 1;
1494 if (copy_from_user(buffer
, buf
, count
))
1497 err
= kstrtoint(strstrip(buffer
), 0, &nice
);
1501 p
= get_proc_task(inode
);
1505 err
= proc_sched_autogroup_set_nice(p
, nice
);
1514 static int sched_autogroup_open(struct inode
*inode
, struct file
*filp
)
1518 ret
= single_open(filp
, sched_autogroup_show
, NULL
);
1520 struct seq_file
*m
= filp
->private_data
;
1527 static const struct file_operations proc_pid_sched_autogroup_operations
= {
1528 .open
= sched_autogroup_open
,
1530 .write
= sched_autogroup_write
,
1531 .llseek
= seq_lseek
,
1532 .release
= single_release
,
1535 #endif /* CONFIG_SCHED_AUTOGROUP */
1537 static ssize_t
comm_write(struct file
*file
, const char __user
*buf
,
1538 size_t count
, loff_t
*offset
)
1540 struct inode
*inode
= file_inode(file
);
1541 struct task_struct
*p
;
1542 char buffer
[TASK_COMM_LEN
];
1543 const size_t maxlen
= sizeof(buffer
) - 1;
1545 memset(buffer
, 0, sizeof(buffer
));
1546 if (copy_from_user(buffer
, buf
, count
> maxlen
? maxlen
: count
))
1549 p
= get_proc_task(inode
);
1553 if (same_thread_group(current
, p
))
1554 set_task_comm(p
, buffer
);
1563 static int comm_show(struct seq_file
*m
, void *v
)
1565 struct inode
*inode
= m
->private;
1566 struct task_struct
*p
;
1568 p
= get_proc_task(inode
);
1573 seq_printf(m
, "%s\n", p
->comm
);
1581 static int comm_open(struct inode
*inode
, struct file
*filp
)
1583 return single_open(filp
, comm_show
, inode
);
1586 static const struct file_operations proc_pid_set_comm_operations
= {
1589 .write
= comm_write
,
1590 .llseek
= seq_lseek
,
1591 .release
= single_release
,
1594 static int proc_exe_link(struct dentry
*dentry
, struct path
*exe_path
)
1596 struct task_struct
*task
;
1597 struct file
*exe_file
;
1599 task
= get_proc_task(d_inode(dentry
));
1602 exe_file
= get_task_exe_file(task
);
1603 put_task_struct(task
);
1605 *exe_path
= exe_file
->f_path
;
1606 path_get(&exe_file
->f_path
);
1613 static const char *proc_pid_get_link(struct dentry
*dentry
,
1614 struct inode
*inode
,
1615 struct delayed_call
*done
)
1618 int error
= -EACCES
;
1621 return ERR_PTR(-ECHILD
);
1623 /* Are we allowed to snoop on the tasks file descriptors? */
1624 if (!proc_fd_access_allowed(inode
))
1627 error
= PROC_I(inode
)->op
.proc_get_link(dentry
, &path
);
1631 nd_jump_link(&path
);
1634 return ERR_PTR(error
);
1637 static int do_proc_readlink(struct path
*path
, char __user
*buffer
, int buflen
)
1639 char *tmp
= (char*)__get_free_page(GFP_TEMPORARY
);
1646 pathname
= d_path(path
, tmp
, PAGE_SIZE
);
1647 len
= PTR_ERR(pathname
);
1648 if (IS_ERR(pathname
))
1650 len
= tmp
+ PAGE_SIZE
- 1 - pathname
;
1654 if (copy_to_user(buffer
, pathname
, len
))
1657 free_page((unsigned long)tmp
);
1661 static int proc_pid_readlink(struct dentry
* dentry
, char __user
* buffer
, int buflen
)
1663 int error
= -EACCES
;
1664 struct inode
*inode
= d_inode(dentry
);
1667 /* Are we allowed to snoop on the tasks file descriptors? */
1668 if (!proc_fd_access_allowed(inode
))
1671 error
= PROC_I(inode
)->op
.proc_get_link(dentry
, &path
);
1675 error
= do_proc_readlink(&path
, buffer
, buflen
);
1681 const struct inode_operations proc_pid_link_inode_operations
= {
1682 .readlink
= proc_pid_readlink
,
1683 .get_link
= proc_pid_get_link
,
1684 .setattr
= proc_setattr
,
1688 /* building an inode */
1690 void task_dump_owner(struct task_struct
*task
, mode_t mode
,
1691 kuid_t
*ruid
, kgid_t
*rgid
)
1693 /* Depending on the state of dumpable compute who should own a
1694 * proc file for a task.
1696 const struct cred
*cred
;
1700 /* Default to the tasks effective ownership */
1702 cred
= __task_cred(task
);
1708 * Before the /proc/pid/status file was created the only way to read
1709 * the effective uid of a /process was to stat /proc/pid. Reading
1710 * /proc/pid/status is slow enough that procps and other packages
1711 * kept stating /proc/pid. To keep the rules in /proc simple I have
1712 * made this apply to all per process world readable and executable
1715 if (mode
!= (S_IFDIR
|S_IRUGO
|S_IXUGO
)) {
1716 struct mm_struct
*mm
;
1719 /* Make non-dumpable tasks owned by some root */
1721 if (get_dumpable(mm
) != SUID_DUMP_USER
) {
1722 struct user_namespace
*user_ns
= mm
->user_ns
;
1724 uid
= make_kuid(user_ns
, 0);
1725 if (!uid_valid(uid
))
1726 uid
= GLOBAL_ROOT_UID
;
1728 gid
= make_kgid(user_ns
, 0);
1729 if (!gid_valid(gid
))
1730 gid
= GLOBAL_ROOT_GID
;
1733 uid
= GLOBAL_ROOT_UID
;
1734 gid
= GLOBAL_ROOT_GID
;
1742 struct inode
*proc_pid_make_inode(struct super_block
* sb
,
1743 struct task_struct
*task
, umode_t mode
)
1745 struct inode
* inode
;
1746 struct proc_inode
*ei
;
1748 /* We need a new inode */
1750 inode
= new_inode(sb
);
1756 inode
->i_mode
= mode
;
1757 inode
->i_ino
= get_next_ino();
1758 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= current_time(inode
);
1759 inode
->i_op
= &proc_def_inode_operations
;
1762 * grab the reference to task.
1764 ei
->pid
= get_task_pid(task
, PIDTYPE_PID
);
1768 task_dump_owner(task
, 0, &inode
->i_uid
, &inode
->i_gid
);
1769 security_task_to_inode(task
, inode
);
1779 int pid_getattr(const struct path
*path
, struct kstat
*stat
,
1780 u32 request_mask
, unsigned int query_flags
)
1782 struct inode
*inode
= d_inode(path
->dentry
);
1783 struct task_struct
*task
;
1784 struct pid_namespace
*pid
= path
->dentry
->d_sb
->s_fs_info
;
1786 generic_fillattr(inode
, stat
);
1789 stat
->uid
= GLOBAL_ROOT_UID
;
1790 stat
->gid
= GLOBAL_ROOT_GID
;
1791 task
= pid_task(proc_pid(inode
), PIDTYPE_PID
);
1793 if (!has_pid_permissions(pid
, task
, HIDEPID_INVISIBLE
)) {
1796 * This doesn't prevent learning whether PID exists,
1797 * it only makes getattr() consistent with readdir().
1801 task_dump_owner(task
, inode
->i_mode
, &stat
->uid
, &stat
->gid
);
1810 * Exceptional case: normally we are not allowed to unhash a busy
1811 * directory. In this case, however, we can do it - no aliasing problems
1812 * due to the way we treat inodes.
1814 * Rewrite the inode's ownerships here because the owning task may have
1815 * performed a setuid(), etc.
1818 int pid_revalidate(struct dentry
*dentry
, unsigned int flags
)
1820 struct inode
*inode
;
1821 struct task_struct
*task
;
1823 if (flags
& LOOKUP_RCU
)
1826 inode
= d_inode(dentry
);
1827 task
= get_proc_task(inode
);
1830 task_dump_owner(task
, inode
->i_mode
, &inode
->i_uid
, &inode
->i_gid
);
1832 inode
->i_mode
&= ~(S_ISUID
| S_ISGID
);
1833 security_task_to_inode(task
, inode
);
1834 put_task_struct(task
);
1840 static inline bool proc_inode_is_dead(struct inode
*inode
)
1842 return !proc_pid(inode
)->tasks
[PIDTYPE_PID
].first
;
1845 int pid_delete_dentry(const struct dentry
*dentry
)
1847 /* Is the task we represent dead?
1848 * If so, then don't put the dentry on the lru list,
1849 * kill it immediately.
1851 return proc_inode_is_dead(d_inode(dentry
));
1854 const struct dentry_operations pid_dentry_operations
=
1856 .d_revalidate
= pid_revalidate
,
1857 .d_delete
= pid_delete_dentry
,
1863 * Fill a directory entry.
1865 * If possible create the dcache entry and derive our inode number and
1866 * file type from dcache entry.
1868 * Since all of the proc inode numbers are dynamically generated, the inode
1869 * numbers do not exist until the inode is cache. This means creating the
1870 * the dcache entry in readdir is necessary to keep the inode numbers
1871 * reported by readdir in sync with the inode numbers reported
1874 bool proc_fill_cache(struct file
*file
, struct dir_context
*ctx
,
1875 const char *name
, int len
,
1876 instantiate_t instantiate
, struct task_struct
*task
, const void *ptr
)
1878 struct dentry
*child
, *dir
= file
->f_path
.dentry
;
1879 struct qstr qname
= QSTR_INIT(name
, len
);
1880 struct inode
*inode
;
1884 child
= d_hash_and_lookup(dir
, &qname
);
1886 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq
);
1887 child
= d_alloc_parallel(dir
, &qname
, &wq
);
1889 goto end_instantiate
;
1890 if (d_in_lookup(child
)) {
1891 int err
= instantiate(d_inode(dir
), child
, task
, ptr
);
1892 d_lookup_done(child
);
1895 goto end_instantiate
;
1899 inode
= d_inode(child
);
1901 type
= inode
->i_mode
>> 12;
1903 return dir_emit(ctx
, name
, len
, ino
, type
);
1906 return dir_emit(ctx
, name
, len
, 1, DT_UNKNOWN
);
1910 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1911 * which represent vma start and end addresses.
1913 static int dname_to_vma_addr(struct dentry
*dentry
,
1914 unsigned long *start
, unsigned long *end
)
1916 if (sscanf(dentry
->d_name
.name
, "%lx-%lx", start
, end
) != 2)
1922 static int map_files_d_revalidate(struct dentry
*dentry
, unsigned int flags
)
1924 unsigned long vm_start
, vm_end
;
1925 bool exact_vma_exists
= false;
1926 struct mm_struct
*mm
= NULL
;
1927 struct task_struct
*task
;
1928 struct inode
*inode
;
1931 if (flags
& LOOKUP_RCU
)
1934 inode
= d_inode(dentry
);
1935 task
= get_proc_task(inode
);
1939 mm
= mm_access(task
, PTRACE_MODE_READ_FSCREDS
);
1940 if (IS_ERR_OR_NULL(mm
))
1943 if (!dname_to_vma_addr(dentry
, &vm_start
, &vm_end
)) {
1944 down_read(&mm
->mmap_sem
);
1945 exact_vma_exists
= !!find_exact_vma(mm
, vm_start
, vm_end
);
1946 up_read(&mm
->mmap_sem
);
1951 if (exact_vma_exists
) {
1952 task_dump_owner(task
, 0, &inode
->i_uid
, &inode
->i_gid
);
1954 security_task_to_inode(task
, inode
);
1959 put_task_struct(task
);
1965 static const struct dentry_operations tid_map_files_dentry_operations
= {
1966 .d_revalidate
= map_files_d_revalidate
,
1967 .d_delete
= pid_delete_dentry
,
1970 static int map_files_get_link(struct dentry
*dentry
, struct path
*path
)
1972 unsigned long vm_start
, vm_end
;
1973 struct vm_area_struct
*vma
;
1974 struct task_struct
*task
;
1975 struct mm_struct
*mm
;
1979 task
= get_proc_task(d_inode(dentry
));
1983 mm
= get_task_mm(task
);
1984 put_task_struct(task
);
1988 rc
= dname_to_vma_addr(dentry
, &vm_start
, &vm_end
);
1993 down_read(&mm
->mmap_sem
);
1994 vma
= find_exact_vma(mm
, vm_start
, vm_end
);
1995 if (vma
&& vma
->vm_file
) {
1996 *path
= vma
->vm_file
->f_path
;
2000 up_read(&mm
->mmap_sem
);
2008 struct map_files_info
{
2011 unsigned char name
[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
2015 * Only allow CAP_SYS_ADMIN to follow the links, due to concerns about how the
2016 * symlinks may be used to bypass permissions on ancestor directories in the
2017 * path to the file in question.
2020 proc_map_files_get_link(struct dentry
*dentry
,
2021 struct inode
*inode
,
2022 struct delayed_call
*done
)
2024 if (!capable(CAP_SYS_ADMIN
))
2025 return ERR_PTR(-EPERM
);
2027 return proc_pid_get_link(dentry
, inode
, done
);
2031 * Identical to proc_pid_link_inode_operations except for get_link()
2033 static const struct inode_operations proc_map_files_link_inode_operations
= {
2034 .readlink
= proc_pid_readlink
,
2035 .get_link
= proc_map_files_get_link
,
2036 .setattr
= proc_setattr
,
2040 proc_map_files_instantiate(struct inode
*dir
, struct dentry
*dentry
,
2041 struct task_struct
*task
, const void *ptr
)
2043 fmode_t mode
= (fmode_t
)(unsigned long)ptr
;
2044 struct proc_inode
*ei
;
2045 struct inode
*inode
;
2047 inode
= proc_pid_make_inode(dir
->i_sb
, task
, S_IFLNK
|
2048 ((mode
& FMODE_READ
) ? S_IRUSR
: 0) |
2049 ((mode
& FMODE_WRITE
) ? S_IWUSR
: 0));
2054 ei
->op
.proc_get_link
= map_files_get_link
;
2056 inode
->i_op
= &proc_map_files_link_inode_operations
;
2059 d_set_d_op(dentry
, &tid_map_files_dentry_operations
);
2060 d_add(dentry
, inode
);
2065 static struct dentry
*proc_map_files_lookup(struct inode
*dir
,
2066 struct dentry
*dentry
, unsigned int flags
)
2068 unsigned long vm_start
, vm_end
;
2069 struct vm_area_struct
*vma
;
2070 struct task_struct
*task
;
2072 struct mm_struct
*mm
;
2075 task
= get_proc_task(dir
);
2080 if (!ptrace_may_access(task
, PTRACE_MODE_READ_FSCREDS
))
2084 if (dname_to_vma_addr(dentry
, &vm_start
, &vm_end
))
2087 mm
= get_task_mm(task
);
2091 down_read(&mm
->mmap_sem
);
2092 vma
= find_exact_vma(mm
, vm_start
, vm_end
);
2097 result
= proc_map_files_instantiate(dir
, dentry
, task
,
2098 (void *)(unsigned long)vma
->vm_file
->f_mode
);
2101 up_read(&mm
->mmap_sem
);
2104 put_task_struct(task
);
2106 return ERR_PTR(result
);
2109 static const struct inode_operations proc_map_files_inode_operations
= {
2110 .lookup
= proc_map_files_lookup
,
2111 .permission
= proc_fd_permission
,
2112 .setattr
= proc_setattr
,
2116 proc_map_files_readdir(struct file
*file
, struct dir_context
*ctx
)
2118 struct vm_area_struct
*vma
;
2119 struct task_struct
*task
;
2120 struct mm_struct
*mm
;
2121 unsigned long nr_files
, pos
, i
;
2122 struct flex_array
*fa
= NULL
;
2123 struct map_files_info info
;
2124 struct map_files_info
*p
;
2128 task
= get_proc_task(file_inode(file
));
2133 if (!ptrace_may_access(task
, PTRACE_MODE_READ_FSCREDS
))
2137 if (!dir_emit_dots(file
, ctx
))
2140 mm
= get_task_mm(task
);
2143 down_read(&mm
->mmap_sem
);
2148 * We need two passes here:
2150 * 1) Collect vmas of mapped files with mmap_sem taken
2151 * 2) Release mmap_sem and instantiate entries
2153 * otherwise we get lockdep complained, since filldir()
2154 * routine might require mmap_sem taken in might_fault().
2157 for (vma
= mm
->mmap
, pos
= 2; vma
; vma
= vma
->vm_next
) {
2158 if (vma
->vm_file
&& ++pos
> ctx
->pos
)
2163 fa
= flex_array_alloc(sizeof(info
), nr_files
,
2165 if (!fa
|| flex_array_prealloc(fa
, 0, nr_files
,
2169 flex_array_free(fa
);
2170 up_read(&mm
->mmap_sem
);
2174 for (i
= 0, vma
= mm
->mmap
, pos
= 2; vma
;
2175 vma
= vma
->vm_next
) {
2178 if (++pos
<= ctx
->pos
)
2181 info
.mode
= vma
->vm_file
->f_mode
;
2182 info
.len
= snprintf(info
.name
,
2183 sizeof(info
.name
), "%lx-%lx",
2184 vma
->vm_start
, vma
->vm_end
);
2185 if (flex_array_put(fa
, i
++, &info
, GFP_KERNEL
))
2189 up_read(&mm
->mmap_sem
);
2191 for (i
= 0; i
< nr_files
; i
++) {
2192 p
= flex_array_get(fa
, i
);
2193 if (!proc_fill_cache(file
, ctx
,
2195 proc_map_files_instantiate
,
2197 (void *)(unsigned long)p
->mode
))
2202 flex_array_free(fa
);
2206 put_task_struct(task
);
2211 static const struct file_operations proc_map_files_operations
= {
2212 .read
= generic_read_dir
,
2213 .iterate_shared
= proc_map_files_readdir
,
2214 .llseek
= generic_file_llseek
,
2217 #if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
2218 struct timers_private
{
2220 struct task_struct
*task
;
2221 struct sighand_struct
*sighand
;
2222 struct pid_namespace
*ns
;
2223 unsigned long flags
;
2226 static void *timers_start(struct seq_file
*m
, loff_t
*pos
)
2228 struct timers_private
*tp
= m
->private;
2230 tp
->task
= get_pid_task(tp
->pid
, PIDTYPE_PID
);
2232 return ERR_PTR(-ESRCH
);
2234 tp
->sighand
= lock_task_sighand(tp
->task
, &tp
->flags
);
2236 return ERR_PTR(-ESRCH
);
2238 return seq_list_start(&tp
->task
->signal
->posix_timers
, *pos
);
2241 static void *timers_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
2243 struct timers_private
*tp
= m
->private;
2244 return seq_list_next(v
, &tp
->task
->signal
->posix_timers
, pos
);
2247 static void timers_stop(struct seq_file
*m
, void *v
)
2249 struct timers_private
*tp
= m
->private;
2252 unlock_task_sighand(tp
->task
, &tp
->flags
);
2257 put_task_struct(tp
->task
);
2262 static int show_timer(struct seq_file
*m
, void *v
)
2264 struct k_itimer
*timer
;
2265 struct timers_private
*tp
= m
->private;
2267 static const char * const nstr
[] = {
2268 [SIGEV_SIGNAL
] = "signal",
2269 [SIGEV_NONE
] = "none",
2270 [SIGEV_THREAD
] = "thread",
2273 timer
= list_entry((struct list_head
*)v
, struct k_itimer
, list
);
2274 notify
= timer
->it_sigev_notify
;
2276 seq_printf(m
, "ID: %d\n", timer
->it_id
);
2277 seq_printf(m
, "signal: %d/%p\n",
2278 timer
->sigq
->info
.si_signo
,
2279 timer
->sigq
->info
.si_value
.sival_ptr
);
2280 seq_printf(m
, "notify: %s/%s.%d\n",
2281 nstr
[notify
& ~SIGEV_THREAD_ID
],
2282 (notify
& SIGEV_THREAD_ID
) ? "tid" : "pid",
2283 pid_nr_ns(timer
->it_pid
, tp
->ns
));
2284 seq_printf(m
, "ClockID: %d\n", timer
->it_clock
);
2289 static const struct seq_operations proc_timers_seq_ops
= {
2290 .start
= timers_start
,
2291 .next
= timers_next
,
2292 .stop
= timers_stop
,
2296 static int proc_timers_open(struct inode
*inode
, struct file
*file
)
2298 struct timers_private
*tp
;
2300 tp
= __seq_open_private(file
, &proc_timers_seq_ops
,
2301 sizeof(struct timers_private
));
2305 tp
->pid
= proc_pid(inode
);
2306 tp
->ns
= inode
->i_sb
->s_fs_info
;
2310 static const struct file_operations proc_timers_operations
= {
2311 .open
= proc_timers_open
,
2313 .llseek
= seq_lseek
,
2314 .release
= seq_release_private
,
2318 static ssize_t
timerslack_ns_write(struct file
*file
, const char __user
*buf
,
2319 size_t count
, loff_t
*offset
)
2321 struct inode
*inode
= file_inode(file
);
2322 struct task_struct
*p
;
2326 err
= kstrtoull_from_user(buf
, count
, 10, &slack_ns
);
2330 p
= get_proc_task(inode
);
2335 if (!capable(CAP_SYS_NICE
)) {
2340 err
= security_task_setscheduler(p
);
2349 p
->timer_slack_ns
= p
->default_timer_slack_ns
;
2351 p
->timer_slack_ns
= slack_ns
;
2360 static int timerslack_ns_show(struct seq_file
*m
, void *v
)
2362 struct inode
*inode
= m
->private;
2363 struct task_struct
*p
;
2366 p
= get_proc_task(inode
);
2372 if (!capable(CAP_SYS_NICE
)) {
2376 err
= security_task_getscheduler(p
);
2382 seq_printf(m
, "%llu\n", p
->timer_slack_ns
);
2391 static int timerslack_ns_open(struct inode
*inode
, struct file
*filp
)
2393 return single_open(filp
, timerslack_ns_show
, inode
);
2396 static const struct file_operations proc_pid_set_timerslack_ns_operations
= {
2397 .open
= timerslack_ns_open
,
2399 .write
= timerslack_ns_write
,
2400 .llseek
= seq_lseek
,
2401 .release
= single_release
,
2404 static int proc_pident_instantiate(struct inode
*dir
,
2405 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
2407 const struct pid_entry
*p
= ptr
;
2408 struct inode
*inode
;
2409 struct proc_inode
*ei
;
2411 inode
= proc_pid_make_inode(dir
->i_sb
, task
, p
->mode
);
2416 if (S_ISDIR(inode
->i_mode
))
2417 set_nlink(inode
, 2); /* Use getattr to fix if necessary */
2419 inode
->i_op
= p
->iop
;
2421 inode
->i_fop
= p
->fop
;
2423 d_set_d_op(dentry
, &pid_dentry_operations
);
2424 d_add(dentry
, inode
);
2425 /* Close the race of the process dying before we return the dentry */
2426 if (pid_revalidate(dentry
, 0))
2432 static struct dentry
*proc_pident_lookup(struct inode
*dir
,
2433 struct dentry
*dentry
,
2434 const struct pid_entry
*ents
,
2438 struct task_struct
*task
= get_proc_task(dir
);
2439 const struct pid_entry
*p
, *last
;
2447 * Yes, it does not scale. And it should not. Don't add
2448 * new entries into /proc/<tgid>/ without very good reasons.
2450 last
= &ents
[nents
];
2451 for (p
= ents
; p
< last
; p
++) {
2452 if (p
->len
!= dentry
->d_name
.len
)
2454 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
))
2460 error
= proc_pident_instantiate(dir
, dentry
, task
, p
);
2462 put_task_struct(task
);
2464 return ERR_PTR(error
);
2467 static int proc_pident_readdir(struct file
*file
, struct dir_context
*ctx
,
2468 const struct pid_entry
*ents
, unsigned int nents
)
2470 struct task_struct
*task
= get_proc_task(file_inode(file
));
2471 const struct pid_entry
*p
;
2476 if (!dir_emit_dots(file
, ctx
))
2479 if (ctx
->pos
>= nents
+ 2)
2482 for (p
= ents
+ (ctx
->pos
- 2); p
< ents
+ nents
; p
++) {
2483 if (!proc_fill_cache(file
, ctx
, p
->name
, p
->len
,
2484 proc_pident_instantiate
, task
, p
))
2489 put_task_struct(task
);
2493 #ifdef CONFIG_SECURITY
2494 static ssize_t
proc_pid_attr_read(struct file
* file
, char __user
* buf
,
2495 size_t count
, loff_t
*ppos
)
2497 struct inode
* inode
= file_inode(file
);
2500 struct task_struct
*task
= get_proc_task(inode
);
2505 length
= security_getprocattr(task
,
2506 (char*)file
->f_path
.dentry
->d_name
.name
,
2508 put_task_struct(task
);
2510 length
= simple_read_from_buffer(buf
, count
, ppos
, p
, length
);
2515 static ssize_t
proc_pid_attr_write(struct file
* file
, const char __user
* buf
,
2516 size_t count
, loff_t
*ppos
)
2518 struct inode
* inode
= file_inode(file
);
2521 struct task_struct
*task
= get_proc_task(inode
);
2527 /* A task may only write its own attributes. */
2529 if (current
!= task
)
2532 if (count
> PAGE_SIZE
)
2535 /* No partial writes. */
2540 page
= memdup_user(buf
, count
);
2542 length
= PTR_ERR(page
);
2546 /* Guard against adverse ptrace interaction */
2547 length
= mutex_lock_interruptible(¤t
->signal
->cred_guard_mutex
);
2551 length
= security_setprocattr(file
->f_path
.dentry
->d_name
.name
,
2553 mutex_unlock(¤t
->signal
->cred_guard_mutex
);
2557 put_task_struct(task
);
2562 static const struct file_operations proc_pid_attr_operations
= {
2563 .read
= proc_pid_attr_read
,
2564 .write
= proc_pid_attr_write
,
2565 .llseek
= generic_file_llseek
,
2568 static const struct pid_entry attr_dir_stuff
[] = {
2569 REG("current", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2570 REG("prev", S_IRUGO
, proc_pid_attr_operations
),
2571 REG("exec", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2572 REG("fscreate", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2573 REG("keycreate", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2574 REG("sockcreate", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2577 static int proc_attr_dir_readdir(struct file
*file
, struct dir_context
*ctx
)
2579 return proc_pident_readdir(file
, ctx
,
2580 attr_dir_stuff
, ARRAY_SIZE(attr_dir_stuff
));
2583 static const struct file_operations proc_attr_dir_operations
= {
2584 .read
= generic_read_dir
,
2585 .iterate_shared
= proc_attr_dir_readdir
,
2586 .llseek
= generic_file_llseek
,
2589 static struct dentry
*proc_attr_dir_lookup(struct inode
*dir
,
2590 struct dentry
*dentry
, unsigned int flags
)
2592 return proc_pident_lookup(dir
, dentry
,
2593 attr_dir_stuff
, ARRAY_SIZE(attr_dir_stuff
));
2596 static const struct inode_operations proc_attr_dir_inode_operations
= {
2597 .lookup
= proc_attr_dir_lookup
,
2598 .getattr
= pid_getattr
,
2599 .setattr
= proc_setattr
,
2604 #ifdef CONFIG_ELF_CORE
2605 static ssize_t
proc_coredump_filter_read(struct file
*file
, char __user
*buf
,
2606 size_t count
, loff_t
*ppos
)
2608 struct task_struct
*task
= get_proc_task(file_inode(file
));
2609 struct mm_struct
*mm
;
2610 char buffer
[PROC_NUMBUF
];
2618 mm
= get_task_mm(task
);
2620 len
= snprintf(buffer
, sizeof(buffer
), "%08lx\n",
2621 ((mm
->flags
& MMF_DUMP_FILTER_MASK
) >>
2622 MMF_DUMP_FILTER_SHIFT
));
2624 ret
= simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
2627 put_task_struct(task
);
2632 static ssize_t
proc_coredump_filter_write(struct file
*file
,
2633 const char __user
*buf
,
2637 struct task_struct
*task
;
2638 struct mm_struct
*mm
;
2644 ret
= kstrtouint_from_user(buf
, count
, 0, &val
);
2649 task
= get_proc_task(file_inode(file
));
2653 mm
= get_task_mm(task
);
2658 for (i
= 0, mask
= 1; i
< MMF_DUMP_FILTER_BITS
; i
++, mask
<<= 1) {
2660 set_bit(i
+ MMF_DUMP_FILTER_SHIFT
, &mm
->flags
);
2662 clear_bit(i
+ MMF_DUMP_FILTER_SHIFT
, &mm
->flags
);
2667 put_task_struct(task
);
2674 static const struct file_operations proc_coredump_filter_operations
= {
2675 .read
= proc_coredump_filter_read
,
2676 .write
= proc_coredump_filter_write
,
2677 .llseek
= generic_file_llseek
,
2681 #ifdef CONFIG_TASK_IO_ACCOUNTING
2682 static int do_io_accounting(struct task_struct
*task
, struct seq_file
*m
, int whole
)
2684 struct task_io_accounting acct
= task
->ioac
;
2685 unsigned long flags
;
2688 result
= mutex_lock_killable(&task
->signal
->cred_guard_mutex
);
2692 if (!ptrace_may_access(task
, PTRACE_MODE_READ_FSCREDS
)) {
2697 if (whole
&& lock_task_sighand(task
, &flags
)) {
2698 struct task_struct
*t
= task
;
2700 task_io_accounting_add(&acct
, &task
->signal
->ioac
);
2701 while_each_thread(task
, t
)
2702 task_io_accounting_add(&acct
, &t
->ioac
);
2704 unlock_task_sighand(task
, &flags
);
2711 "read_bytes: %llu\n"
2712 "write_bytes: %llu\n"
2713 "cancelled_write_bytes: %llu\n",
2714 (unsigned long long)acct
.rchar
,
2715 (unsigned long long)acct
.wchar
,
2716 (unsigned long long)acct
.syscr
,
2717 (unsigned long long)acct
.syscw
,
2718 (unsigned long long)acct
.read_bytes
,
2719 (unsigned long long)acct
.write_bytes
,
2720 (unsigned long long)acct
.cancelled_write_bytes
);
2724 mutex_unlock(&task
->signal
->cred_guard_mutex
);
2728 static int proc_tid_io_accounting(struct seq_file
*m
, struct pid_namespace
*ns
,
2729 struct pid
*pid
, struct task_struct
*task
)
2731 return do_io_accounting(task
, m
, 0);
2734 static int proc_tgid_io_accounting(struct seq_file
*m
, struct pid_namespace
*ns
,
2735 struct pid
*pid
, struct task_struct
*task
)
2737 return do_io_accounting(task
, m
, 1);
2739 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2741 #ifdef CONFIG_USER_NS
2742 static int proc_id_map_open(struct inode
*inode
, struct file
*file
,
2743 const struct seq_operations
*seq_ops
)
2745 struct user_namespace
*ns
= NULL
;
2746 struct task_struct
*task
;
2747 struct seq_file
*seq
;
2750 task
= get_proc_task(inode
);
2753 ns
= get_user_ns(task_cred_xxx(task
, user_ns
));
2755 put_task_struct(task
);
2760 ret
= seq_open(file
, seq_ops
);
2764 seq
= file
->private_data
;
2774 static int proc_id_map_release(struct inode
*inode
, struct file
*file
)
2776 struct seq_file
*seq
= file
->private_data
;
2777 struct user_namespace
*ns
= seq
->private;
2779 return seq_release(inode
, file
);
2782 static int proc_uid_map_open(struct inode
*inode
, struct file
*file
)
2784 return proc_id_map_open(inode
, file
, &proc_uid_seq_operations
);
2787 static int proc_gid_map_open(struct inode
*inode
, struct file
*file
)
2789 return proc_id_map_open(inode
, file
, &proc_gid_seq_operations
);
2792 static int proc_projid_map_open(struct inode
*inode
, struct file
*file
)
2794 return proc_id_map_open(inode
, file
, &proc_projid_seq_operations
);
2797 static const struct file_operations proc_uid_map_operations
= {
2798 .open
= proc_uid_map_open
,
2799 .write
= proc_uid_map_write
,
2801 .llseek
= seq_lseek
,
2802 .release
= proc_id_map_release
,
2805 static const struct file_operations proc_gid_map_operations
= {
2806 .open
= proc_gid_map_open
,
2807 .write
= proc_gid_map_write
,
2809 .llseek
= seq_lseek
,
2810 .release
= proc_id_map_release
,
2813 static const struct file_operations proc_projid_map_operations
= {
2814 .open
= proc_projid_map_open
,
2815 .write
= proc_projid_map_write
,
2817 .llseek
= seq_lseek
,
2818 .release
= proc_id_map_release
,
2821 static int proc_setgroups_open(struct inode
*inode
, struct file
*file
)
2823 struct user_namespace
*ns
= NULL
;
2824 struct task_struct
*task
;
2828 task
= get_proc_task(inode
);
2831 ns
= get_user_ns(task_cred_xxx(task
, user_ns
));
2833 put_task_struct(task
);
2838 if (file
->f_mode
& FMODE_WRITE
) {
2840 if (!ns_capable(ns
, CAP_SYS_ADMIN
))
2844 ret
= single_open(file
, &proc_setgroups_show
, ns
);
2855 static int proc_setgroups_release(struct inode
*inode
, struct file
*file
)
2857 struct seq_file
*seq
= file
->private_data
;
2858 struct user_namespace
*ns
= seq
->private;
2859 int ret
= single_release(inode
, file
);
2864 static const struct file_operations proc_setgroups_operations
= {
2865 .open
= proc_setgroups_open
,
2866 .write
= proc_setgroups_write
,
2868 .llseek
= seq_lseek
,
2869 .release
= proc_setgroups_release
,
2871 #endif /* CONFIG_USER_NS */
2873 static int proc_pid_personality(struct seq_file
*m
, struct pid_namespace
*ns
,
2874 struct pid
*pid
, struct task_struct
*task
)
2876 int err
= lock_trace(task
);
2878 seq_printf(m
, "%08x\n", task
->personality
);
2884 #ifdef CONFIG_LIVEPATCH
2885 static int proc_pid_patch_state(struct seq_file
*m
, struct pid_namespace
*ns
,
2886 struct pid
*pid
, struct task_struct
*task
)
2888 seq_printf(m
, "%d\n", task
->patch_state
);
2891 #endif /* CONFIG_LIVEPATCH */
2896 static const struct file_operations proc_task_operations
;
2897 static const struct inode_operations proc_task_inode_operations
;
2899 static const struct pid_entry tgid_base_stuff
[] = {
2900 DIR("task", S_IRUGO
|S_IXUGO
, proc_task_inode_operations
, proc_task_operations
),
2901 DIR("fd", S_IRUSR
|S_IXUSR
, proc_fd_inode_operations
, proc_fd_operations
),
2902 DIR("map_files", S_IRUSR
|S_IXUSR
, proc_map_files_inode_operations
, proc_map_files_operations
),
2903 DIR("fdinfo", S_IRUSR
|S_IXUSR
, proc_fdinfo_inode_operations
, proc_fdinfo_operations
),
2904 DIR("ns", S_IRUSR
|S_IXUGO
, proc_ns_dir_inode_operations
, proc_ns_dir_operations
),
2906 DIR("net", S_IRUGO
|S_IXUGO
, proc_net_inode_operations
, proc_net_operations
),
2908 REG("environ", S_IRUSR
, proc_environ_operations
),
2909 REG("auxv", S_IRUSR
, proc_auxv_operations
),
2910 ONE("status", S_IRUGO
, proc_pid_status
),
2911 ONE("personality", S_IRUSR
, proc_pid_personality
),
2912 ONE("limits", S_IRUGO
, proc_pid_limits
),
2913 #ifdef CONFIG_SCHED_DEBUG
2914 REG("sched", S_IRUGO
|S_IWUSR
, proc_pid_sched_operations
),
2916 #ifdef CONFIG_SCHED_AUTOGROUP
2917 REG("autogroup", S_IRUGO
|S_IWUSR
, proc_pid_sched_autogroup_operations
),
2919 REG("comm", S_IRUGO
|S_IWUSR
, proc_pid_set_comm_operations
),
2920 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2921 ONE("syscall", S_IRUSR
, proc_pid_syscall
),
2923 REG("cmdline", S_IRUGO
, proc_pid_cmdline_ops
),
2924 ONE("stat", S_IRUGO
, proc_tgid_stat
),
2925 ONE("statm", S_IRUGO
, proc_pid_statm
),
2926 REG("maps", S_IRUGO
, proc_pid_maps_operations
),
2928 REG("numa_maps", S_IRUGO
, proc_pid_numa_maps_operations
),
2930 REG("mem", S_IRUSR
|S_IWUSR
, proc_mem_operations
),
2931 LNK("cwd", proc_cwd_link
),
2932 LNK("root", proc_root_link
),
2933 LNK("exe", proc_exe_link
),
2934 REG("mounts", S_IRUGO
, proc_mounts_operations
),
2935 REG("mountinfo", S_IRUGO
, proc_mountinfo_operations
),
2936 REG("mountstats", S_IRUSR
, proc_mountstats_operations
),
2937 #ifdef CONFIG_PROC_PAGE_MONITOR
2938 REG("clear_refs", S_IWUSR
, proc_clear_refs_operations
),
2939 REG("smaps", S_IRUGO
, proc_pid_smaps_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_HARDWALL
2981 ONE("hardwall", S_IRUGO
, proc_pid_hardwall
),
2983 #ifdef CONFIG_USER_NS
2984 REG("uid_map", S_IRUGO
|S_IWUSR
, proc_uid_map_operations
),
2985 REG("gid_map", S_IRUGO
|S_IWUSR
, proc_gid_map_operations
),
2986 REG("projid_map", S_IRUGO
|S_IWUSR
, proc_projid_map_operations
),
2987 REG("setgroups", S_IRUGO
|S_IWUSR
, proc_setgroups_operations
),
2989 #if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
2990 REG("timers", S_IRUGO
, proc_timers_operations
),
2992 REG("timerslack_ns", S_IRUGO
|S_IWUGO
, proc_pid_set_timerslack_ns_operations
),
2993 #ifdef CONFIG_LIVEPATCH
2994 ONE("patch_state", S_IRUSR
, proc_pid_patch_state
),
2998 static int proc_tgid_base_readdir(struct file
*file
, struct dir_context
*ctx
)
3000 return proc_pident_readdir(file
, ctx
,
3001 tgid_base_stuff
, ARRAY_SIZE(tgid_base_stuff
));
3004 static const struct file_operations proc_tgid_base_operations
= {
3005 .read
= generic_read_dir
,
3006 .iterate_shared
= proc_tgid_base_readdir
,
3007 .llseek
= generic_file_llseek
,
3010 static struct dentry
*proc_tgid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, unsigned int flags
)
3012 return proc_pident_lookup(dir
, dentry
,
3013 tgid_base_stuff
, ARRAY_SIZE(tgid_base_stuff
));
3016 static const struct inode_operations proc_tgid_base_inode_operations
= {
3017 .lookup
= proc_tgid_base_lookup
,
3018 .getattr
= pid_getattr
,
3019 .setattr
= proc_setattr
,
3020 .permission
= proc_pid_permission
,
3023 static void proc_flush_task_mnt(struct vfsmount
*mnt
, pid_t pid
, pid_t tgid
)
3025 struct dentry
*dentry
, *leader
, *dir
;
3026 char buf
[PROC_NUMBUF
];
3030 name
.len
= snprintf(buf
, sizeof(buf
), "%d", pid
);
3031 /* no ->d_hash() rejects on procfs */
3032 dentry
= d_hash_and_lookup(mnt
->mnt_root
, &name
);
3034 d_invalidate(dentry
);
3042 name
.len
= snprintf(buf
, sizeof(buf
), "%d", tgid
);
3043 leader
= d_hash_and_lookup(mnt
->mnt_root
, &name
);
3048 name
.len
= strlen(name
.name
);
3049 dir
= d_hash_and_lookup(leader
, &name
);
3051 goto out_put_leader
;
3054 name
.len
= snprintf(buf
, sizeof(buf
), "%d", pid
);
3055 dentry
= d_hash_and_lookup(dir
, &name
);
3057 d_invalidate(dentry
);
3069 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
3070 * @task: task that should be flushed.
3072 * When flushing dentries from proc, one needs to flush them from global
3073 * proc (proc_mnt) and from all the namespaces' procs this task was seen
3074 * in. This call is supposed to do all of this job.
3076 * Looks in the dcache for
3078 * /proc/@tgid/task/@pid
3079 * if either directory is present flushes it and all of it'ts children
3082 * It is safe and reasonable to cache /proc entries for a task until
3083 * that task exits. After that they just clog up the dcache with
3084 * useless entries, possibly causing useful dcache entries to be
3085 * flushed instead. This routine is proved to flush those useless
3086 * dcache entries at process exit time.
3088 * NOTE: This routine is just an optimization so it does not guarantee
3089 * that no dcache entries will exist at process exit time it
3090 * just makes it very unlikely that any will persist.
3093 void proc_flush_task(struct task_struct
*task
)
3096 struct pid
*pid
, *tgid
;
3099 pid
= task_pid(task
);
3100 tgid
= task_tgid(task
);
3102 for (i
= 0; i
<= pid
->level
; i
++) {
3103 upid
= &pid
->numbers
[i
];
3104 proc_flush_task_mnt(upid
->ns
->proc_mnt
, upid
->nr
,
3105 tgid
->numbers
[i
].nr
);
3109 static int proc_pid_instantiate(struct inode
*dir
,
3110 struct dentry
* dentry
,
3111 struct task_struct
*task
, const void *ptr
)
3113 struct inode
*inode
;
3115 inode
= proc_pid_make_inode(dir
->i_sb
, task
, S_IFDIR
| S_IRUGO
| S_IXUGO
);
3119 inode
->i_op
= &proc_tgid_base_inode_operations
;
3120 inode
->i_fop
= &proc_tgid_base_operations
;
3121 inode
->i_flags
|=S_IMMUTABLE
;
3123 set_nlink(inode
, nlink_tgid
);
3125 d_set_d_op(dentry
, &pid_dentry_operations
);
3127 d_add(dentry
, inode
);
3128 /* Close the race of the process dying before we return the dentry */
3129 if (pid_revalidate(dentry
, 0))
3135 struct dentry
*proc_pid_lookup(struct inode
*dir
, struct dentry
* dentry
, unsigned int flags
)
3137 int result
= -ENOENT
;
3138 struct task_struct
*task
;
3140 struct pid_namespace
*ns
;
3142 tgid
= name_to_int(&dentry
->d_name
);
3146 ns
= dentry
->d_sb
->s_fs_info
;
3148 task
= find_task_by_pid_ns(tgid
, ns
);
3150 get_task_struct(task
);
3155 result
= proc_pid_instantiate(dir
, dentry
, task
, NULL
);
3156 put_task_struct(task
);
3158 return ERR_PTR(result
);
3162 * Find the first task with tgid >= tgid
3167 struct task_struct
*task
;
3169 static struct tgid_iter
next_tgid(struct pid_namespace
*ns
, struct tgid_iter iter
)
3174 put_task_struct(iter
.task
);
3178 pid
= find_ge_pid(iter
.tgid
, ns
);
3180 iter
.tgid
= pid_nr_ns(pid
, ns
);
3181 iter
.task
= pid_task(pid
, PIDTYPE_PID
);
3182 /* What we to know is if the pid we have find is the
3183 * pid of a thread_group_leader. Testing for task
3184 * being a thread_group_leader is the obvious thing
3185 * todo but there is a window when it fails, due to
3186 * the pid transfer logic in de_thread.
3188 * So we perform the straight forward test of seeing
3189 * if the pid we have found is the pid of a thread
3190 * group leader, and don't worry if the task we have
3191 * found doesn't happen to be a thread group leader.
3192 * As we don't care in the case of readdir.
3194 if (!iter
.task
|| !has_group_leader_pid(iter
.task
)) {
3198 get_task_struct(iter
.task
);
3204 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
3206 /* for the /proc/ directory itself, after non-process stuff has been done */
3207 int proc_pid_readdir(struct file
*file
, struct dir_context
*ctx
)
3209 struct tgid_iter iter
;
3210 struct pid_namespace
*ns
= file_inode(file
)->i_sb
->s_fs_info
;
3211 loff_t pos
= ctx
->pos
;
3213 if (pos
>= PID_MAX_LIMIT
+ TGID_OFFSET
)
3216 if (pos
== TGID_OFFSET
- 2) {
3217 struct inode
*inode
= d_inode(ns
->proc_self
);
3218 if (!dir_emit(ctx
, "self", 4, inode
->i_ino
, DT_LNK
))
3220 ctx
->pos
= pos
= pos
+ 1;
3222 if (pos
== TGID_OFFSET
- 1) {
3223 struct inode
*inode
= d_inode(ns
->proc_thread_self
);
3224 if (!dir_emit(ctx
, "thread-self", 11, inode
->i_ino
, DT_LNK
))
3226 ctx
->pos
= pos
= pos
+ 1;
3228 iter
.tgid
= pos
- TGID_OFFSET
;
3230 for (iter
= next_tgid(ns
, iter
);
3232 iter
.tgid
+= 1, iter
= next_tgid(ns
, iter
)) {
3233 char name
[PROC_NUMBUF
];
3237 if (!has_pid_permissions(ns
, iter
.task
, HIDEPID_INVISIBLE
))
3240 len
= snprintf(name
, sizeof(name
), "%d", iter
.tgid
);
3241 ctx
->pos
= iter
.tgid
+ TGID_OFFSET
;
3242 if (!proc_fill_cache(file
, ctx
, name
, len
,
3243 proc_pid_instantiate
, iter
.task
, NULL
)) {
3244 put_task_struct(iter
.task
);
3248 ctx
->pos
= PID_MAX_LIMIT
+ TGID_OFFSET
;
3253 * proc_tid_comm_permission is a special permission function exclusively
3254 * used for the node /proc/<pid>/task/<tid>/comm.
3255 * It bypasses generic permission checks in the case where a task of the same
3256 * task group attempts to access the node.
3257 * The rationale behind this is that glibc and bionic access this node for
3258 * cross thread naming (pthread_set/getname_np(!self)). However, if
3259 * PR_SET_DUMPABLE gets set to 0 this node among others becomes uid=0 gid=0,
3260 * which locks out the cross thread naming implementation.
3261 * This function makes sure that the node is always accessible for members of
3262 * same thread group.
3264 static int proc_tid_comm_permission(struct inode
*inode
, int mask
)
3266 bool is_same_tgroup
;
3267 struct task_struct
*task
;
3269 task
= get_proc_task(inode
);
3272 is_same_tgroup
= same_thread_group(current
, task
);
3273 put_task_struct(task
);
3275 if (likely(is_same_tgroup
&& !(mask
& MAY_EXEC
))) {
3276 /* This file (/proc/<pid>/task/<tid>/comm) can always be
3277 * read or written by the members of the corresponding
3283 return generic_permission(inode
, mask
);
3286 static const struct inode_operations proc_tid_comm_inode_operations
= {
3287 .permission
= proc_tid_comm_permission
,
3293 static const struct pid_entry tid_base_stuff
[] = {
3294 DIR("fd", S_IRUSR
|S_IXUSR
, proc_fd_inode_operations
, proc_fd_operations
),
3295 DIR("fdinfo", S_IRUSR
|S_IXUSR
, proc_fdinfo_inode_operations
, proc_fdinfo_operations
),
3296 DIR("ns", S_IRUSR
|S_IXUGO
, proc_ns_dir_inode_operations
, proc_ns_dir_operations
),
3298 DIR("net", S_IRUGO
|S_IXUGO
, proc_net_inode_operations
, proc_net_operations
),
3300 REG("environ", S_IRUSR
, proc_environ_operations
),
3301 REG("auxv", S_IRUSR
, proc_auxv_operations
),
3302 ONE("status", S_IRUGO
, proc_pid_status
),
3303 ONE("personality", S_IRUSR
, proc_pid_personality
),
3304 ONE("limits", S_IRUGO
, proc_pid_limits
),
3305 #ifdef CONFIG_SCHED_DEBUG
3306 REG("sched", S_IRUGO
|S_IWUSR
, proc_pid_sched_operations
),
3308 NOD("comm", S_IFREG
|S_IRUGO
|S_IWUSR
,
3309 &proc_tid_comm_inode_operations
,
3310 &proc_pid_set_comm_operations
, {}),
3311 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3312 ONE("syscall", S_IRUSR
, proc_pid_syscall
),
3314 REG("cmdline", S_IRUGO
, proc_pid_cmdline_ops
),
3315 ONE("stat", S_IRUGO
, proc_tid_stat
),
3316 ONE("statm", S_IRUGO
, proc_pid_statm
),
3317 REG("maps", S_IRUGO
, proc_tid_maps_operations
),
3318 #ifdef CONFIG_PROC_CHILDREN
3319 REG("children", S_IRUGO
, proc_tid_children_operations
),
3322 REG("numa_maps", S_IRUGO
, proc_tid_numa_maps_operations
),
3324 REG("mem", S_IRUSR
|S_IWUSR
, proc_mem_operations
),
3325 LNK("cwd", proc_cwd_link
),
3326 LNK("root", proc_root_link
),
3327 LNK("exe", proc_exe_link
),
3328 REG("mounts", S_IRUGO
, proc_mounts_operations
),
3329 REG("mountinfo", S_IRUGO
, proc_mountinfo_operations
),
3330 #ifdef CONFIG_PROC_PAGE_MONITOR
3331 REG("clear_refs", S_IWUSR
, proc_clear_refs_operations
),
3332 REG("smaps", S_IRUGO
, proc_tid_smaps_operations
),
3333 REG("pagemap", S_IRUSR
, proc_pagemap_operations
),
3335 #ifdef CONFIG_SECURITY
3336 DIR("attr", S_IRUGO
|S_IXUGO
, proc_attr_dir_inode_operations
, proc_attr_dir_operations
),
3338 #ifdef CONFIG_KALLSYMS
3339 ONE("wchan", S_IRUGO
, proc_pid_wchan
),
3341 #ifdef CONFIG_STACKTRACE
3342 ONE("stack", S_IRUSR
, proc_pid_stack
),
3344 #ifdef CONFIG_SCHED_INFO
3345 ONE("schedstat", S_IRUGO
, proc_pid_schedstat
),
3347 #ifdef CONFIG_LATENCYTOP
3348 REG("latency", S_IRUGO
, proc_lstats_operations
),
3350 #ifdef CONFIG_PROC_PID_CPUSET
3351 ONE("cpuset", S_IRUGO
, proc_cpuset_show
),
3353 #ifdef CONFIG_CGROUPS
3354 ONE("cgroup", S_IRUGO
, proc_cgroup_show
),
3356 ONE("oom_score", S_IRUGO
, proc_oom_score
),
3357 REG("oom_adj", S_IRUGO
|S_IWUSR
, proc_oom_adj_operations
),
3358 REG("oom_score_adj", S_IRUGO
|S_IWUSR
, proc_oom_score_adj_operations
),
3359 #ifdef CONFIG_AUDITSYSCALL
3360 REG("loginuid", S_IWUSR
|S_IRUGO
, proc_loginuid_operations
),
3361 REG("sessionid", S_IRUGO
, proc_sessionid_operations
),
3363 #ifdef CONFIG_FAULT_INJECTION
3364 REG("make-it-fail", S_IRUGO
|S_IWUSR
, proc_fault_inject_operations
),
3365 REG("fail-nth", 0644, proc_fail_nth_operations
),
3367 #ifdef CONFIG_TASK_IO_ACCOUNTING
3368 ONE("io", S_IRUSR
, proc_tid_io_accounting
),
3370 #ifdef CONFIG_HARDWALL
3371 ONE("hardwall", S_IRUGO
, proc_pid_hardwall
),
3373 #ifdef CONFIG_USER_NS
3374 REG("uid_map", S_IRUGO
|S_IWUSR
, proc_uid_map_operations
),
3375 REG("gid_map", S_IRUGO
|S_IWUSR
, proc_gid_map_operations
),
3376 REG("projid_map", S_IRUGO
|S_IWUSR
, proc_projid_map_operations
),
3377 REG("setgroups", S_IRUGO
|S_IWUSR
, proc_setgroups_operations
),
3379 #ifdef CONFIG_LIVEPATCH
3380 ONE("patch_state", S_IRUSR
, proc_pid_patch_state
),
3384 static int proc_tid_base_readdir(struct file
*file
, struct dir_context
*ctx
)
3386 return proc_pident_readdir(file
, ctx
,
3387 tid_base_stuff
, ARRAY_SIZE(tid_base_stuff
));
3390 static struct dentry
*proc_tid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, unsigned int flags
)
3392 return proc_pident_lookup(dir
, dentry
,
3393 tid_base_stuff
, ARRAY_SIZE(tid_base_stuff
));
3396 static const struct file_operations proc_tid_base_operations
= {
3397 .read
= generic_read_dir
,
3398 .iterate_shared
= proc_tid_base_readdir
,
3399 .llseek
= generic_file_llseek
,
3402 static const struct inode_operations proc_tid_base_inode_operations
= {
3403 .lookup
= proc_tid_base_lookup
,
3404 .getattr
= pid_getattr
,
3405 .setattr
= proc_setattr
,
3408 static int proc_task_instantiate(struct inode
*dir
,
3409 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
3411 struct inode
*inode
;
3412 inode
= proc_pid_make_inode(dir
->i_sb
, task
, S_IFDIR
| S_IRUGO
| S_IXUGO
);
3416 inode
->i_op
= &proc_tid_base_inode_operations
;
3417 inode
->i_fop
= &proc_tid_base_operations
;
3418 inode
->i_flags
|=S_IMMUTABLE
;
3420 set_nlink(inode
, nlink_tid
);
3422 d_set_d_op(dentry
, &pid_dentry_operations
);
3424 d_add(dentry
, inode
);
3425 /* Close the race of the process dying before we return the dentry */
3426 if (pid_revalidate(dentry
, 0))
3432 static struct dentry
*proc_task_lookup(struct inode
*dir
, struct dentry
* dentry
, unsigned int flags
)
3434 int result
= -ENOENT
;
3435 struct task_struct
*task
;
3436 struct task_struct
*leader
= get_proc_task(dir
);
3438 struct pid_namespace
*ns
;
3443 tid
= name_to_int(&dentry
->d_name
);
3447 ns
= dentry
->d_sb
->s_fs_info
;
3449 task
= find_task_by_pid_ns(tid
, ns
);
3451 get_task_struct(task
);
3455 if (!same_thread_group(leader
, task
))
3458 result
= proc_task_instantiate(dir
, dentry
, task
, NULL
);
3460 put_task_struct(task
);
3462 put_task_struct(leader
);
3464 return ERR_PTR(result
);
3468 * Find the first tid of a thread group to return to user space.
3470 * Usually this is just the thread group leader, but if the users
3471 * buffer was too small or there was a seek into the middle of the
3472 * directory we have more work todo.
3474 * In the case of a short read we start with find_task_by_pid.
3476 * In the case of a seek we start with the leader and walk nr
3479 static struct task_struct
*first_tid(struct pid
*pid
, int tid
, loff_t f_pos
,
3480 struct pid_namespace
*ns
)
3482 struct task_struct
*pos
, *task
;
3483 unsigned long nr
= f_pos
;
3485 if (nr
!= f_pos
) /* 32bit overflow? */
3489 task
= pid_task(pid
, PIDTYPE_PID
);
3493 /* Attempt to start with the tid of a thread */
3495 pos
= find_task_by_pid_ns(tid
, ns
);
3496 if (pos
&& same_thread_group(pos
, task
))
3500 /* If nr exceeds the number of threads there is nothing todo */
3501 if (nr
>= get_nr_threads(task
))
3504 /* If we haven't found our starting place yet start
3505 * with the leader and walk nr threads forward.
3507 pos
= task
= task
->group_leader
;
3511 } while_each_thread(task
, pos
);
3516 get_task_struct(pos
);
3523 * Find the next thread in the thread list.
3524 * Return NULL if there is an error or no next thread.
3526 * The reference to the input task_struct is released.
3528 static struct task_struct
*next_tid(struct task_struct
*start
)
3530 struct task_struct
*pos
= NULL
;
3532 if (pid_alive(start
)) {
3533 pos
= next_thread(start
);
3534 if (thread_group_leader(pos
))
3537 get_task_struct(pos
);
3540 put_task_struct(start
);
3544 /* for the /proc/TGID/task/ directories */
3545 static int proc_task_readdir(struct file
*file
, struct dir_context
*ctx
)
3547 struct inode
*inode
= file_inode(file
);
3548 struct task_struct
*task
;
3549 struct pid_namespace
*ns
;
3552 if (proc_inode_is_dead(inode
))
3555 if (!dir_emit_dots(file
, ctx
))
3558 /* f_version caches the tgid value that the last readdir call couldn't
3559 * return. lseek aka telldir automagically resets f_version to 0.
3561 ns
= inode
->i_sb
->s_fs_info
;
3562 tid
= (int)file
->f_version
;
3563 file
->f_version
= 0;
3564 for (task
= first_tid(proc_pid(inode
), tid
, ctx
->pos
- 2, ns
);
3566 task
= next_tid(task
), ctx
->pos
++) {
3567 char name
[PROC_NUMBUF
];
3569 tid
= task_pid_nr_ns(task
, ns
);
3570 len
= snprintf(name
, sizeof(name
), "%d", tid
);
3571 if (!proc_fill_cache(file
, ctx
, name
, len
,
3572 proc_task_instantiate
, task
, NULL
)) {
3573 /* returning this tgid failed, save it as the first
3574 * pid for the next readir call */
3575 file
->f_version
= (u64
)tid
;
3576 put_task_struct(task
);
3584 static int proc_task_getattr(const struct path
*path
, struct kstat
*stat
,
3585 u32 request_mask
, unsigned int query_flags
)
3587 struct inode
*inode
= d_inode(path
->dentry
);
3588 struct task_struct
*p
= get_proc_task(inode
);
3589 generic_fillattr(inode
, stat
);
3592 stat
->nlink
+= get_nr_threads(p
);
3599 static const struct inode_operations proc_task_inode_operations
= {
3600 .lookup
= proc_task_lookup
,
3601 .getattr
= proc_task_getattr
,
3602 .setattr
= proc_setattr
,
3603 .permission
= proc_pid_permission
,
3606 static const struct file_operations proc_task_operations
= {
3607 .read
= generic_read_dir
,
3608 .iterate_shared
= proc_task_readdir
,
3609 .llseek
= generic_file_llseek
,
3612 void __init
set_proc_pid_nlink(void)
3614 nlink_tid
= pid_entry_nlink(tid_base_stuff
, ARRAY_SIZE(tid_base_stuff
));
3615 nlink_tgid
= pid_entry_nlink(tgid_base_stuff
, ARRAY_SIZE(tgid_base_stuff
));