]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/proc/base.c
proc: convert /proc/$PID/schedstat to seq_file interface
[mirror_ubuntu-bionic-kernel.git] / fs / proc / base.c
CommitLineData
1da177e4
LT
1/*
2 * linux/fs/proc/base.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
6 * proc base directory handling functions
7 *
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.
e070ad49
ML
14 *
15 *
16 * Changelog:
17 * 17-Jan-2005
18 * Allan Bezerra
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>
23 *
24 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
25 *
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.
32 *
33 * Changelog:
34 * 21-Feb-2005
35 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36 * Pud inclusion in the page table walking.
37 *
38 * ChangeLog:
39 * 10-Mar-2005
40 * 10LE Instituto Nokia de Tecnologia - INdT:
41 * A better way to walks through the page table as suggested by Hugh Dickins.
42 *
43 * Simo Piiroinen <simo.piiroinen@nokia.com>:
44 * Smaps information related to shared, private, clean and dirty pages.
45 *
46 * Paul Mundt <paul.mundt@nokia.com>:
47 * Overall revision about smaps.
1da177e4
LT
48 */
49
50#include <asm/uaccess.h>
51
1da177e4
LT
52#include <linux/errno.h>
53#include <linux/time.h>
54#include <linux/proc_fs.h>
55#include <linux/stat.h>
5995477a 56#include <linux/task_io_accounting_ops.h>
1da177e4 57#include <linux/init.h>
16f7e0fe 58#include <linux/capability.h>
1da177e4 59#include <linux/file.h>
9f3acc31 60#include <linux/fdtable.h>
1da177e4
LT
61#include <linux/string.h>
62#include <linux/seq_file.h>
63#include <linux/namei.h>
6b3286ed 64#include <linux/mnt_namespace.h>
1da177e4 65#include <linux/mm.h>
a63d83f4 66#include <linux/swap.h>
b835996f 67#include <linux/rcupdate.h>
1da177e4 68#include <linux/kallsyms.h>
2ec220e2 69#include <linux/stacktrace.h>
d85f50d5 70#include <linux/resource.h>
5096add8 71#include <linux/module.h>
1da177e4
LT
72#include <linux/mount.h>
73#include <linux/security.h>
74#include <linux/ptrace.h>
0d094efe 75#include <linux/tracehook.h>
87ebdc00 76#include <linux/printk.h>
a424316c 77#include <linux/cgroup.h>
1da177e4
LT
78#include <linux/cpuset.h>
79#include <linux/audit.h>
5addc5dd 80#include <linux/poll.h>
1651e14e 81#include <linux/nsproxy.h>
8ac773b4 82#include <linux/oom.h>
3cb4a0bb 83#include <linux/elf.h>
60347f67 84#include <linux/pid_namespace.h>
22d917d8 85#include <linux/user_namespace.h>
5ad4e53b 86#include <linux/fs_struct.h>
5a0e3ad6 87#include <linux/slab.h>
640708a2 88#include <linux/flex_array.h>
48f6a7a5 89#include <linux/posix-timers.h>
f133ecca
CM
90#ifdef CONFIG_HARDWALL
91#include <asm/hardwall.h>
92#endif
43d2b113 93#include <trace/events/oom.h>
1da177e4 94#include "internal.h"
faf60af1 95#include "fd.h"
1da177e4 96
0f2fe20f
EB
97/* NOTE:
98 * Implementing inode permission operations in /proc is almost
99 * certainly an error. Permission checks need to happen during
100 * each system call not at open time. The reason is that most of
101 * what we wish to check for permissions in /proc varies at runtime.
102 *
103 * The classic example of a problem is opening file descriptors
104 * in /proc for a task before it execs a suid executable.
105 */
106
1da177e4 107struct pid_entry {
cedbccab 108 const char *name;
c5141e6d 109 int len;
d161a13f 110 umode_t mode;
c5ef1c42 111 const struct inode_operations *iop;
00977a59 112 const struct file_operations *fop;
20cdc894 113 union proc_op op;
1da177e4
LT
114};
115
61a28784 116#define NOD(NAME, MODE, IOP, FOP, OP) { \
20cdc894 117 .name = (NAME), \
c5141e6d 118 .len = sizeof(NAME) - 1, \
20cdc894
EB
119 .mode = MODE, \
120 .iop = IOP, \
121 .fop = FOP, \
122 .op = OP, \
123}
124
631f9c18
AD
125#define DIR(NAME, MODE, iops, fops) \
126 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
127#define LNK(NAME, get_link) \
61a28784 128 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
20cdc894 129 &proc_pid_link_inode_operations, NULL, \
631f9c18
AD
130 { .proc_get_link = get_link } )
131#define REG(NAME, MODE, fops) \
132 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
133#define INF(NAME, MODE, read) \
61a28784 134 NOD(NAME, (S_IFREG|(MODE)), \
20cdc894 135 NULL, &proc_info_file_operations, \
631f9c18
AD
136 { .proc_read = read } )
137#define ONE(NAME, MODE, show) \
be614086
EB
138 NOD(NAME, (S_IFREG|(MODE)), \
139 NULL, &proc_single_file_operations, \
631f9c18 140 { .proc_show = show } )
1da177e4 141
aed54175
VN
142/*
143 * Count the number of hardlinks for the pid_entry table, excluding the .
144 * and .. links.
145 */
146static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
147 unsigned int n)
148{
149 unsigned int i;
150 unsigned int count;
151
152 count = 0;
153 for (i = 0; i < n; ++i) {
154 if (S_ISDIR(entries[i].mode))
155 ++count;
156 }
157
158 return count;
159}
160
f7ad3c6b 161static int get_task_root(struct task_struct *task, struct path *root)
1da177e4 162{
7c2c7d99
HD
163 int result = -ENOENT;
164
0494f6ec 165 task_lock(task);
f7ad3c6b
MS
166 if (task->fs) {
167 get_fs_root(task->fs, root);
7c2c7d99
HD
168 result = 0;
169 }
0494f6ec 170 task_unlock(task);
7c2c7d99 171 return result;
0494f6ec
MS
172}
173
7773fbc5 174static int proc_cwd_link(struct dentry *dentry, struct path *path)
0494f6ec 175{
7773fbc5 176 struct task_struct *task = get_proc_task(dentry->d_inode);
0494f6ec 177 int result = -ENOENT;
99f89551
EB
178
179 if (task) {
f7ad3c6b
MS
180 task_lock(task);
181 if (task->fs) {
182 get_fs_pwd(task->fs, path);
183 result = 0;
184 }
185 task_unlock(task);
99f89551
EB
186 put_task_struct(task);
187 }
1da177e4
LT
188 return result;
189}
190
7773fbc5 191static int proc_root_link(struct dentry *dentry, struct path *path)
1da177e4 192{
7773fbc5 193 struct task_struct *task = get_proc_task(dentry->d_inode);
1da177e4 194 int result = -ENOENT;
99f89551
EB
195
196 if (task) {
f7ad3c6b 197 result = get_task_root(task, path);
99f89551
EB
198 put_task_struct(task);
199 }
1da177e4
LT
200 return result;
201}
202
2ca66ff7
AD
203static int proc_pid_cmdline(struct seq_file *m, struct pid_namespace *ns,
204 struct pid *pid, struct task_struct *task)
1da177e4 205{
2ca66ff7
AD
206 /*
207 * Rely on struct seq_operations::show() being called once
208 * per internal buffer allocation. See single_open(), traverse().
209 */
210 BUG_ON(m->size < PAGE_SIZE);
211 m->count += get_cmdline(task, m->buf, PAGE_SIZE);
212 return 0;
1da177e4
LT
213}
214
f9ea536e
AD
215static int proc_pid_auxv(struct seq_file *m, struct pid_namespace *ns,
216 struct pid *pid, struct task_struct *task)
1da177e4 217{
e7dcd999 218 struct mm_struct *mm = mm_access(task, PTRACE_MODE_READ);
2fadaef4 219 if (mm && !IS_ERR(mm)) {
1da177e4 220 unsigned int nwords = 0;
dfe6b7d9 221 do {
1da177e4 222 nwords += 2;
dfe6b7d9 223 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
f9ea536e 224 seq_write(m, mm->saved_auxv, nwords * sizeof(mm->saved_auxv[0]));
1da177e4 225 mmput(mm);
f9ea536e
AD
226 return 0;
227 } else
228 return PTR_ERR(mm);
1da177e4
LT
229}
230
231
232#ifdef CONFIG_KALLSYMS
233/*
234 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
235 * Returns the resolved symbol. If that fails, simply return the address.
236 */
edfcd606
AD
237static int proc_pid_wchan(struct seq_file *m, struct pid_namespace *ns,
238 struct pid *pid, struct task_struct *task)
1da177e4 239{
ffb45122 240 unsigned long wchan;
9281acea 241 char symname[KSYM_NAME_LEN];
1da177e4
LT
242
243 wchan = get_wchan(task);
244
9d65cb4a 245 if (lookup_symbol_name(wchan, symname) < 0)
f83ce3e6
JE
246 if (!ptrace_may_access(task, PTRACE_MODE_READ))
247 return 0;
248 else
edfcd606 249 return seq_printf(m, "%lu", wchan);
9d65cb4a 250 else
edfcd606 251 return seq_printf(m, "%s", symname);
1da177e4
LT
252}
253#endif /* CONFIG_KALLSYMS */
254
a9712bc1
AV
255static int lock_trace(struct task_struct *task)
256{
257 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
258 if (err)
259 return err;
260 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH)) {
261 mutex_unlock(&task->signal->cred_guard_mutex);
262 return -EPERM;
263 }
264 return 0;
265}
266
267static void unlock_trace(struct task_struct *task)
268{
269 mutex_unlock(&task->signal->cred_guard_mutex);
270}
271
2ec220e2
KC
272#ifdef CONFIG_STACKTRACE
273
274#define MAX_STACK_TRACE_DEPTH 64
275
276static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
277 struct pid *pid, struct task_struct *task)
278{
279 struct stack_trace trace;
280 unsigned long *entries;
a9712bc1 281 int err;
2ec220e2
KC
282 int i;
283
284 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
285 if (!entries)
286 return -ENOMEM;
287
288 trace.nr_entries = 0;
289 trace.max_entries = MAX_STACK_TRACE_DEPTH;
290 trace.entries = entries;
291 trace.skip = 0;
2ec220e2 292
a9712bc1
AV
293 err = lock_trace(task);
294 if (!err) {
295 save_stack_trace_tsk(task, &trace);
296
297 for (i = 0; i < trace.nr_entries; i++) {
b81a618d 298 seq_printf(m, "[<%pK>] %pS\n",
a9712bc1
AV
299 (void *)entries[i], (void *)entries[i]);
300 }
301 unlock_trace(task);
2ec220e2
KC
302 }
303 kfree(entries);
304
a9712bc1 305 return err;
2ec220e2
KC
306}
307#endif
308
1da177e4
LT
309#ifdef CONFIG_SCHEDSTATS
310/*
311 * Provides /proc/PID/schedstat
312 */
f6e826ca
AD
313static int proc_pid_schedstat(struct seq_file *m, struct pid_namespace *ns,
314 struct pid *pid, struct task_struct *task)
1da177e4 315{
f6e826ca 316 return seq_printf(m, "%llu %llu %lu\n",
826e08b0
IM
317 (unsigned long long)task->se.sum_exec_runtime,
318 (unsigned long long)task->sched_info.run_delay,
2d72376b 319 task->sched_info.pcount);
1da177e4
LT
320}
321#endif
322
9745512c
AV
323#ifdef CONFIG_LATENCYTOP
324static int lstats_show_proc(struct seq_file *m, void *v)
325{
326 int i;
13d77c37
HS
327 struct inode *inode = m->private;
328 struct task_struct *task = get_proc_task(inode);
9745512c 329
13d77c37
HS
330 if (!task)
331 return -ESRCH;
332 seq_puts(m, "Latency Top version : v0.1\n");
9745512c 333 for (i = 0; i < 32; i++) {
34e49d4f
JP
334 struct latency_record *lr = &task->latency_record[i];
335 if (lr->backtrace[0]) {
9745512c 336 int q;
34e49d4f
JP
337 seq_printf(m, "%i %li %li",
338 lr->count, lr->time, lr->max);
9745512c 339 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
34e49d4f
JP
340 unsigned long bt = lr->backtrace[q];
341 if (!bt)
9745512c 342 break;
34e49d4f 343 if (bt == ULONG_MAX)
9745512c 344 break;
34e49d4f 345 seq_printf(m, " %ps", (void *)bt);
9745512c 346 }
9d6de12f 347 seq_putc(m, '\n');
9745512c
AV
348 }
349
350 }
13d77c37 351 put_task_struct(task);
9745512c
AV
352 return 0;
353}
354
355static int lstats_open(struct inode *inode, struct file *file)
356{
13d77c37 357 return single_open(file, lstats_show_proc, inode);
d6643d12
HS
358}
359
9745512c
AV
360static ssize_t lstats_write(struct file *file, const char __user *buf,
361 size_t count, loff_t *offs)
362{
496ad9aa 363 struct task_struct *task = get_proc_task(file_inode(file));
9745512c 364
13d77c37
HS
365 if (!task)
366 return -ESRCH;
9745512c 367 clear_all_latency_tracing(task);
13d77c37 368 put_task_struct(task);
9745512c
AV
369
370 return count;
371}
372
373static const struct file_operations proc_lstats_operations = {
374 .open = lstats_open,
375 .read = seq_read,
376 .write = lstats_write,
377 .llseek = seq_lseek,
13d77c37 378 .release = single_release,
9745512c
AV
379};
380
381#endif
382
8d8b97ba
AV
383#ifdef CONFIG_CGROUPS
384static int cgroup_open(struct inode *inode, struct file *file)
385{
386 struct pid *pid = PROC_I(inode)->pid;
387 return single_open(file, proc_cgroup_show, pid);
388}
389
390static const struct file_operations proc_cgroup_operations = {
391 .open = cgroup_open,
392 .read = seq_read,
393 .llseek = seq_lseek,
394 .release = single_release,
395};
396#endif
397
398#ifdef CONFIG_PROC_PID_CPUSET
399
400static int cpuset_open(struct inode *inode, struct file *file)
401{
402 struct pid *pid = PROC_I(inode)->pid;
403 return single_open(file, proc_cpuset_show, pid);
404}
405
406static const struct file_operations proc_cpuset_operations = {
407 .open = cpuset_open,
408 .read = seq_read,
409 .llseek = seq_lseek,
410 .release = single_release,
411};
412#endif
413
1da177e4
LT
414static int proc_oom_score(struct task_struct *task, char *buffer)
415{
a7f638f9 416 unsigned long totalpages = totalram_pages + total_swap_pages;
b95c35e7 417 unsigned long points = 0;
1da177e4 418
19c5d45a 419 read_lock(&tasklist_lock);
b95c35e7 420 if (pid_alive(task))
a7f638f9
DR
421 points = oom_badness(task, NULL, NULL, totalpages) *
422 1000 / totalpages;
19c5d45a 423 read_unlock(&tasklist_lock);
1da177e4
LT
424 return sprintf(buffer, "%lu\n", points);
425}
426
d85f50d5 427struct limit_names {
cedbccab
AD
428 const char *name;
429 const char *unit;
d85f50d5
NH
430};
431
432static const struct limit_names lnames[RLIM_NLIMITS] = {
cff4edb5 433 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
d85f50d5
NH
434 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
435 [RLIMIT_DATA] = {"Max data size", "bytes"},
436 [RLIMIT_STACK] = {"Max stack size", "bytes"},
437 [RLIMIT_CORE] = {"Max core file size", "bytes"},
438 [RLIMIT_RSS] = {"Max resident set", "bytes"},
439 [RLIMIT_NPROC] = {"Max processes", "processes"},
440 [RLIMIT_NOFILE] = {"Max open files", "files"},
441 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
442 [RLIMIT_AS] = {"Max address space", "bytes"},
443 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
444 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
445 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
446 [RLIMIT_NICE] = {"Max nice priority", NULL},
447 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
8808117c 448 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
d85f50d5
NH
449};
450
451/* Display limits for a process */
1c963eb1
AD
452static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
453 struct pid *pid, struct task_struct *task)
d85f50d5
NH
454{
455 unsigned int i;
d85f50d5 456 unsigned long flags;
d85f50d5
NH
457
458 struct rlimit rlim[RLIM_NLIMITS];
459
a6bebbc8 460 if (!lock_task_sighand(task, &flags))
d85f50d5 461 return 0;
d85f50d5
NH
462 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
463 unlock_task_sighand(task, &flags);
d85f50d5
NH
464
465 /*
466 * print the file header
467 */
1c963eb1 468 seq_printf(m, "%-25s %-20s %-20s %-10s\n",
d85f50d5
NH
469 "Limit", "Soft Limit", "Hard Limit", "Units");
470
471 for (i = 0; i < RLIM_NLIMITS; i++) {
472 if (rlim[i].rlim_cur == RLIM_INFINITY)
1c963eb1 473 seq_printf(m, "%-25s %-20s ",
d85f50d5
NH
474 lnames[i].name, "unlimited");
475 else
1c963eb1 476 seq_printf(m, "%-25s %-20lu ",
d85f50d5
NH
477 lnames[i].name, rlim[i].rlim_cur);
478
479 if (rlim[i].rlim_max == RLIM_INFINITY)
1c963eb1 480 seq_printf(m, "%-20s ", "unlimited");
d85f50d5 481 else
1c963eb1 482 seq_printf(m, "%-20lu ", rlim[i].rlim_max);
d85f50d5
NH
483
484 if (lnames[i].unit)
1c963eb1 485 seq_printf(m, "%-10s\n", lnames[i].unit);
d85f50d5 486 else
1c963eb1 487 seq_putc(m, '\n');
d85f50d5
NH
488 }
489
1c963eb1 490 return 0;
d85f50d5
NH
491}
492
ebcb6734 493#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
09d93bd6
AD
494static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
495 struct pid *pid, struct task_struct *task)
ebcb6734
RM
496{
497 long nr;
498 unsigned long args[6], sp, pc;
a9712bc1
AV
499 int res = lock_trace(task);
500 if (res)
501 return res;
ebcb6734
RM
502
503 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
09d93bd6 504 seq_puts(m, "running\n");
a9712bc1 505 else if (nr < 0)
09d93bd6 506 seq_printf(m, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
a9712bc1 507 else
09d93bd6 508 seq_printf(m,
ebcb6734
RM
509 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
510 nr,
511 args[0], args[1], args[2], args[3], args[4], args[5],
512 sp, pc);
a9712bc1
AV
513 unlock_trace(task);
514 return res;
ebcb6734
RM
515}
516#endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
517
1da177e4
LT
518/************************************************************************/
519/* Here the fs part begins */
520/************************************************************************/
521
522/* permission checks */
778c1144 523static int proc_fd_access_allowed(struct inode *inode)
1da177e4 524{
778c1144
EB
525 struct task_struct *task;
526 int allowed = 0;
df26c40e
EB
527 /* Allow access to a task's file descriptors if it is us or we
528 * may use ptrace attach to the process and find out that
529 * information.
778c1144
EB
530 */
531 task = get_proc_task(inode);
df26c40e 532 if (task) {
006ebb40 533 allowed = ptrace_may_access(task, PTRACE_MODE_READ);
778c1144 534 put_task_struct(task);
df26c40e 535 }
778c1144 536 return allowed;
1da177e4
LT
537}
538
6b4e306a 539int proc_setattr(struct dentry *dentry, struct iattr *attr)
6d76fa58
LT
540{
541 int error;
542 struct inode *inode = dentry->d_inode;
543
544 if (attr->ia_valid & ATTR_MODE)
545 return -EPERM;
546
547 error = inode_change_ok(inode, attr);
1025774c
CH
548 if (error)
549 return error;
550
1025774c
CH
551 setattr_copy(inode, attr);
552 mark_inode_dirty(inode);
553 return 0;
6d76fa58
LT
554}
555
0499680a
VK
556/*
557 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
558 * or euid/egid (for hide_pid_min=2)?
559 */
560static bool has_pid_permissions(struct pid_namespace *pid,
561 struct task_struct *task,
562 int hide_pid_min)
563{
564 if (pid->hide_pid < hide_pid_min)
565 return true;
566 if (in_group_p(pid->pid_gid))
567 return true;
568 return ptrace_may_access(task, PTRACE_MODE_READ);
569}
570
571
572static int proc_pid_permission(struct inode *inode, int mask)
573{
574 struct pid_namespace *pid = inode->i_sb->s_fs_info;
575 struct task_struct *task;
576 bool has_perms;
577
578 task = get_proc_task(inode);
a2ef990a
XF
579 if (!task)
580 return -ESRCH;
0499680a
VK
581 has_perms = has_pid_permissions(pid, task, 1);
582 put_task_struct(task);
583
584 if (!has_perms) {
585 if (pid->hide_pid == 2) {
586 /*
587 * Let's make getdents(), stat(), and open()
588 * consistent with each other. If a process
589 * may not stat() a file, it shouldn't be seen
590 * in procfs at all.
591 */
592 return -ENOENT;
593 }
594
595 return -EPERM;
596 }
597 return generic_permission(inode, mask);
598}
599
600
601
c5ef1c42 602static const struct inode_operations proc_def_inode_operations = {
6d76fa58
LT
603 .setattr = proc_setattr,
604};
605
1da177e4
LT
606#define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
607
608static ssize_t proc_info_read(struct file * file, char __user * buf,
609 size_t count, loff_t *ppos)
610{
496ad9aa 611 struct inode * inode = file_inode(file);
1da177e4
LT
612 unsigned long page;
613 ssize_t length;
99f89551
EB
614 struct task_struct *task = get_proc_task(inode);
615
616 length = -ESRCH;
617 if (!task)
618 goto out_no_task;
1da177e4
LT
619
620 if (count > PROC_BLOCK_SIZE)
621 count = PROC_BLOCK_SIZE;
99f89551
EB
622
623 length = -ENOMEM;
e12ba74d 624 if (!(page = __get_free_page(GFP_TEMPORARY)))
99f89551 625 goto out;
1da177e4
LT
626
627 length = PROC_I(inode)->op.proc_read(task, (char*)page);
628
629 if (length >= 0)
630 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
631 free_page(page);
99f89551
EB
632out:
633 put_task_struct(task);
634out_no_task:
1da177e4
LT
635 return length;
636}
637
00977a59 638static const struct file_operations proc_info_file_operations = {
1da177e4 639 .read = proc_info_read,
87df8424 640 .llseek = generic_file_llseek,
1da177e4
LT
641};
642
be614086
EB
643static int proc_single_show(struct seq_file *m, void *v)
644{
645 struct inode *inode = m->private;
646 struct pid_namespace *ns;
647 struct pid *pid;
648 struct task_struct *task;
649 int ret;
650
651 ns = inode->i_sb->s_fs_info;
652 pid = proc_pid(inode);
653 task = get_pid_task(pid, PIDTYPE_PID);
654 if (!task)
655 return -ESRCH;
656
657 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
658
659 put_task_struct(task);
660 return ret;
661}
662
663static int proc_single_open(struct inode *inode, struct file *filp)
664{
c6a34058 665 return single_open(filp, proc_single_show, inode);
be614086
EB
666}
667
668static const struct file_operations proc_single_file_operations = {
669 .open = proc_single_open,
670 .read = seq_read,
671 .llseek = seq_lseek,
672 .release = single_release,
673};
674
b409e578 675static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
1da177e4 676{
496ad9aa 677 struct task_struct *task = get_proc_task(file_inode(file));
e268337d
LT
678 struct mm_struct *mm;
679
680 if (!task)
681 return -ESRCH;
682
b409e578 683 mm = mm_access(task, mode);
e268337d
LT
684 put_task_struct(task);
685
686 if (IS_ERR(mm))
687 return PTR_ERR(mm);
688
6d08f2c7
ON
689 if (mm) {
690 /* ensure this mm_struct can't be freed */
691 atomic_inc(&mm->mm_count);
692 /* but do not pin its memory */
693 mmput(mm);
694 }
695
e268337d
LT
696 file->private_data = mm;
697
1da177e4
LT
698 return 0;
699}
700
b409e578
CW
701static int mem_open(struct inode *inode, struct file *file)
702{
bc452b4b
DH
703 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
704
705 /* OK to pass negative loff_t, we can catch out-of-range */
706 file->f_mode |= FMODE_UNSIGNED_OFFSET;
707
708 return ret;
b409e578
CW
709}
710
572d34b9
ON
711static ssize_t mem_rw(struct file *file, char __user *buf,
712 size_t count, loff_t *ppos, int write)
1da177e4 713{
e268337d 714 struct mm_struct *mm = file->private_data;
572d34b9
ON
715 unsigned long addr = *ppos;
716 ssize_t copied;
1da177e4 717 char *page;
1da177e4 718
e268337d
LT
719 if (!mm)
720 return 0;
99f89551 721
30cd8903
KM
722 page = (char *)__get_free_page(GFP_TEMPORARY);
723 if (!page)
e268337d 724 return -ENOMEM;
1da177e4 725
f7ca54f4 726 copied = 0;
6d08f2c7
ON
727 if (!atomic_inc_not_zero(&mm->mm_users))
728 goto free;
729
1da177e4 730 while (count > 0) {
572d34b9 731 int this_len = min_t(int, count, PAGE_SIZE);
1da177e4 732
572d34b9 733 if (write && copy_from_user(page, buf, this_len)) {
1da177e4
LT
734 copied = -EFAULT;
735 break;
736 }
572d34b9
ON
737
738 this_len = access_remote_vm(mm, addr, page, this_len, write);
739 if (!this_len) {
1da177e4
LT
740 if (!copied)
741 copied = -EIO;
742 break;
743 }
572d34b9
ON
744
745 if (!write && copy_to_user(buf, page, this_len)) {
746 copied = -EFAULT;
747 break;
748 }
749
750 buf += this_len;
751 addr += this_len;
752 copied += this_len;
753 count -= this_len;
1da177e4 754 }
572d34b9 755 *ppos = addr;
30cd8903 756
6d08f2c7
ON
757 mmput(mm);
758free:
30cd8903 759 free_page((unsigned long) page);
1da177e4
LT
760 return copied;
761}
1da177e4 762
572d34b9
ON
763static ssize_t mem_read(struct file *file, char __user *buf,
764 size_t count, loff_t *ppos)
765{
766 return mem_rw(file, buf, count, ppos, 0);
767}
768
769static ssize_t mem_write(struct file *file, const char __user *buf,
770 size_t count, loff_t *ppos)
771{
772 return mem_rw(file, (char __user*)buf, count, ppos, 1);
773}
774
85863e47 775loff_t mem_lseek(struct file *file, loff_t offset, int orig)
1da177e4
LT
776{
777 switch (orig) {
778 case 0:
779 file->f_pos = offset;
780 break;
781 case 1:
782 file->f_pos += offset;
783 break;
784 default:
785 return -EINVAL;
786 }
787 force_successful_syscall_return();
788 return file->f_pos;
789}
790
e268337d
LT
791static int mem_release(struct inode *inode, struct file *file)
792{
793 struct mm_struct *mm = file->private_data;
71879d3c 794 if (mm)
6d08f2c7 795 mmdrop(mm);
e268337d
LT
796 return 0;
797}
798
00977a59 799static const struct file_operations proc_mem_operations = {
1da177e4
LT
800 .llseek = mem_lseek,
801 .read = mem_read,
802 .write = mem_write,
803 .open = mem_open,
e268337d 804 .release = mem_release,
1da177e4
LT
805};
806
b409e578
CW
807static int environ_open(struct inode *inode, struct file *file)
808{
809 return __mem_open(inode, file, PTRACE_MODE_READ);
810}
811
315e28c8
JP
812static ssize_t environ_read(struct file *file, char __user *buf,
813 size_t count, loff_t *ppos)
814{
315e28c8
JP
815 char *page;
816 unsigned long src = *ppos;
b409e578
CW
817 int ret = 0;
818 struct mm_struct *mm = file->private_data;
315e28c8 819
b409e578
CW
820 if (!mm)
821 return 0;
315e28c8 822
315e28c8
JP
823 page = (char *)__get_free_page(GFP_TEMPORARY);
824 if (!page)
b409e578 825 return -ENOMEM;
315e28c8 826
d6f64b89 827 ret = 0;
b409e578
CW
828 if (!atomic_inc_not_zero(&mm->mm_users))
829 goto free;
315e28c8 830 while (count > 0) {
e8905ec2
DH
831 size_t this_len, max_len;
832 int retval;
315e28c8 833
e8905ec2 834 if (src >= (mm->env_end - mm->env_start))
315e28c8
JP
835 break;
836
e8905ec2
DH
837 this_len = mm->env_end - (mm->env_start + src);
838
839 max_len = min_t(size_t, PAGE_SIZE, count);
840 this_len = min(max_len, this_len);
315e28c8 841
b409e578 842 retval = access_remote_vm(mm, (mm->env_start + src),
315e28c8
JP
843 page, this_len, 0);
844
845 if (retval <= 0) {
846 ret = retval;
847 break;
848 }
849
850 if (copy_to_user(buf, page, retval)) {
851 ret = -EFAULT;
852 break;
853 }
854
855 ret += retval;
856 src += retval;
857 buf += retval;
858 count -= retval;
859 }
860 *ppos = src;
315e28c8 861 mmput(mm);
b409e578
CW
862
863free:
315e28c8 864 free_page((unsigned long) page);
315e28c8
JP
865 return ret;
866}
867
868static const struct file_operations proc_environ_operations = {
b409e578 869 .open = environ_open,
315e28c8 870 .read = environ_read,
87df8424 871 .llseek = generic_file_llseek,
b409e578 872 .release = mem_release,
315e28c8
JP
873};
874
fa0cbbf1
DR
875static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
876 loff_t *ppos)
877{
496ad9aa 878 struct task_struct *task = get_proc_task(file_inode(file));
fa0cbbf1
DR
879 char buffer[PROC_NUMBUF];
880 int oom_adj = OOM_ADJUST_MIN;
881 size_t len;
882 unsigned long flags;
883
884 if (!task)
885 return -ESRCH;
886 if (lock_task_sighand(task, &flags)) {
887 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
888 oom_adj = OOM_ADJUST_MAX;
889 else
890 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
891 OOM_SCORE_ADJ_MAX;
892 unlock_task_sighand(task, &flags);
893 }
894 put_task_struct(task);
895 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
896 return simple_read_from_buffer(buf, count, ppos, buffer, len);
897}
898
899static ssize_t oom_adj_write(struct file *file, const char __user *buf,
900 size_t count, loff_t *ppos)
901{
902 struct task_struct *task;
903 char buffer[PROC_NUMBUF];
904 int oom_adj;
905 unsigned long flags;
906 int err;
907
908 memset(buffer, 0, sizeof(buffer));
909 if (count > sizeof(buffer) - 1)
910 count = sizeof(buffer) - 1;
911 if (copy_from_user(buffer, buf, count)) {
912 err = -EFAULT;
913 goto out;
914 }
915
916 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
917 if (err)
918 goto out;
919 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
920 oom_adj != OOM_DISABLE) {
921 err = -EINVAL;
922 goto out;
923 }
924
496ad9aa 925 task = get_proc_task(file_inode(file));
fa0cbbf1
DR
926 if (!task) {
927 err = -ESRCH;
928 goto out;
929 }
930
931 task_lock(task);
932 if (!task->mm) {
933 err = -EINVAL;
934 goto err_task_lock;
935 }
936
937 if (!lock_task_sighand(task, &flags)) {
938 err = -ESRCH;
939 goto err_task_lock;
940 }
941
942 /*
943 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
944 * value is always attainable.
945 */
946 if (oom_adj == OOM_ADJUST_MAX)
947 oom_adj = OOM_SCORE_ADJ_MAX;
948 else
949 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
950
951 if (oom_adj < task->signal->oom_score_adj &&
952 !capable(CAP_SYS_RESOURCE)) {
953 err = -EACCES;
954 goto err_sighand;
955 }
956
957 /*
958 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
959 * /proc/pid/oom_score_adj instead.
960 */
87ebdc00 961 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
fa0cbbf1
DR
962 current->comm, task_pid_nr(current), task_pid_nr(task),
963 task_pid_nr(task));
964
965 task->signal->oom_score_adj = oom_adj;
966 trace_oom_score_adj_update(task);
967err_sighand:
968 unlock_task_sighand(task, &flags);
969err_task_lock:
970 task_unlock(task);
971 put_task_struct(task);
972out:
973 return err < 0 ? err : count;
974}
975
976static const struct file_operations proc_oom_adj_operations = {
977 .read = oom_adj_read,
978 .write = oom_adj_write,
979 .llseek = generic_file_llseek,
980};
981
a63d83f4
DR
982static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
983 size_t count, loff_t *ppos)
984{
496ad9aa 985 struct task_struct *task = get_proc_task(file_inode(file));
a63d83f4 986 char buffer[PROC_NUMBUF];
a9c58b90 987 short oom_score_adj = OOM_SCORE_ADJ_MIN;
a63d83f4
DR
988 unsigned long flags;
989 size_t len;
990
991 if (!task)
992 return -ESRCH;
993 if (lock_task_sighand(task, &flags)) {
994 oom_score_adj = task->signal->oom_score_adj;
995 unlock_task_sighand(task, &flags);
996 }
997 put_task_struct(task);
a9c58b90 998 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
a63d83f4
DR
999 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1000}
1001
1002static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1003 size_t count, loff_t *ppos)
1004{
1005 struct task_struct *task;
1006 char buffer[PROC_NUMBUF];
1007 unsigned long flags;
0a8cb8e3 1008 int oom_score_adj;
a63d83f4
DR
1009 int err;
1010
1011 memset(buffer, 0, sizeof(buffer));
1012 if (count > sizeof(buffer) - 1)
1013 count = sizeof(buffer) - 1;
723548bf
DR
1014 if (copy_from_user(buffer, buf, count)) {
1015 err = -EFAULT;
1016 goto out;
1017 }
a63d83f4 1018
0a8cb8e3 1019 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
a63d83f4 1020 if (err)
723548bf 1021 goto out;
a63d83f4 1022 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
723548bf
DR
1023 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1024 err = -EINVAL;
1025 goto out;
1026 }
a63d83f4 1027
496ad9aa 1028 task = get_proc_task(file_inode(file));
723548bf
DR
1029 if (!task) {
1030 err = -ESRCH;
1031 goto out;
1032 }
d19d5476
DR
1033
1034 task_lock(task);
1035 if (!task->mm) {
1036 err = -EINVAL;
1037 goto err_task_lock;
1038 }
1039
a63d83f4 1040 if (!lock_task_sighand(task, &flags)) {
723548bf 1041 err = -ESRCH;
d19d5476 1042 goto err_task_lock;
a63d83f4 1043 }
d19d5476 1044
a9c58b90 1045 if ((short)oom_score_adj < task->signal->oom_score_adj_min &&
a63d83f4 1046 !capable(CAP_SYS_RESOURCE)) {
723548bf
DR
1047 err = -EACCES;
1048 goto err_sighand;
a63d83f4
DR
1049 }
1050
a9c58b90 1051 task->signal->oom_score_adj = (short)oom_score_adj;
dabb16f6 1052 if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
a9c58b90 1053 task->signal->oom_score_adj_min = (short)oom_score_adj;
43d2b113 1054 trace_oom_score_adj_update(task);
01dc52eb 1055
723548bf 1056err_sighand:
a63d83f4 1057 unlock_task_sighand(task, &flags);
d19d5476
DR
1058err_task_lock:
1059 task_unlock(task);
a63d83f4 1060 put_task_struct(task);
723548bf
DR
1061out:
1062 return err < 0 ? err : count;
a63d83f4
DR
1063}
1064
1065static const struct file_operations proc_oom_score_adj_operations = {
1066 .read = oom_score_adj_read,
1067 .write = oom_score_adj_write,
6038f373 1068 .llseek = default_llseek,
a63d83f4
DR
1069};
1070
1da177e4
LT
1071#ifdef CONFIG_AUDITSYSCALL
1072#define TMPBUFLEN 21
1073static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1074 size_t count, loff_t *ppos)
1075{
496ad9aa 1076 struct inode * inode = file_inode(file);
99f89551 1077 struct task_struct *task = get_proc_task(inode);
1da177e4
LT
1078 ssize_t length;
1079 char tmpbuf[TMPBUFLEN];
1080
99f89551
EB
1081 if (!task)
1082 return -ESRCH;
1da177e4 1083 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
e1760bd5
EB
1084 from_kuid(file->f_cred->user_ns,
1085 audit_get_loginuid(task)));
99f89551 1086 put_task_struct(task);
1da177e4
LT
1087 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1088}
1089
1090static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1091 size_t count, loff_t *ppos)
1092{
496ad9aa 1093 struct inode * inode = file_inode(file);
1da177e4
LT
1094 char *page, *tmp;
1095 ssize_t length;
1da177e4 1096 uid_t loginuid;
e1760bd5 1097 kuid_t kloginuid;
1da177e4 1098
7dc52157
PM
1099 rcu_read_lock();
1100 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1101 rcu_read_unlock();
1da177e4 1102 return -EPERM;
7dc52157
PM
1103 }
1104 rcu_read_unlock();
1da177e4 1105
e0182909
AV
1106 if (count >= PAGE_SIZE)
1107 count = PAGE_SIZE - 1;
1da177e4
LT
1108
1109 if (*ppos != 0) {
1110 /* No partial writes. */
1111 return -EINVAL;
1112 }
e12ba74d 1113 page = (char*)__get_free_page(GFP_TEMPORARY);
1da177e4
LT
1114 if (!page)
1115 return -ENOMEM;
1116 length = -EFAULT;
1117 if (copy_from_user(page, buf, count))
1118 goto out_free_page;
1119
e0182909 1120 page[count] = '\0';
1da177e4
LT
1121 loginuid = simple_strtoul(page, &tmp, 10);
1122 if (tmp == page) {
1123 length = -EINVAL;
1124 goto out_free_page;
1125
1126 }
81407c84
EP
1127
1128 /* is userspace tring to explicitly UNSET the loginuid? */
1129 if (loginuid == AUDIT_UID_UNSET) {
1130 kloginuid = INVALID_UID;
1131 } else {
1132 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
1133 if (!uid_valid(kloginuid)) {
1134 length = -EINVAL;
1135 goto out_free_page;
1136 }
e1760bd5
EB
1137 }
1138
1139 length = audit_set_loginuid(kloginuid);
1da177e4
LT
1140 if (likely(length == 0))
1141 length = count;
1142
1143out_free_page:
1144 free_page((unsigned long) page);
1145 return length;
1146}
1147
00977a59 1148static const struct file_operations proc_loginuid_operations = {
1da177e4
LT
1149 .read = proc_loginuid_read,
1150 .write = proc_loginuid_write,
87df8424 1151 .llseek = generic_file_llseek,
1da177e4 1152};
1e0bd755
EP
1153
1154static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1155 size_t count, loff_t *ppos)
1156{
496ad9aa 1157 struct inode * inode = file_inode(file);
1e0bd755
EP
1158 struct task_struct *task = get_proc_task(inode);
1159 ssize_t length;
1160 char tmpbuf[TMPBUFLEN];
1161
1162 if (!task)
1163 return -ESRCH;
1164 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1165 audit_get_sessionid(task));
1166 put_task_struct(task);
1167 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1168}
1169
1170static const struct file_operations proc_sessionid_operations = {
1171 .read = proc_sessionid_read,
87df8424 1172 .llseek = generic_file_llseek,
1e0bd755 1173};
1da177e4
LT
1174#endif
1175
f4f154fd
AM
1176#ifdef CONFIG_FAULT_INJECTION
1177static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1178 size_t count, loff_t *ppos)
1179{
496ad9aa 1180 struct task_struct *task = get_proc_task(file_inode(file));
f4f154fd
AM
1181 char buffer[PROC_NUMBUF];
1182 size_t len;
1183 int make_it_fail;
f4f154fd
AM
1184
1185 if (!task)
1186 return -ESRCH;
1187 make_it_fail = task->make_it_fail;
1188 put_task_struct(task);
1189
1190 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
0c28f287
AM
1191
1192 return simple_read_from_buffer(buf, count, ppos, buffer, len);
f4f154fd
AM
1193}
1194
1195static ssize_t proc_fault_inject_write(struct file * file,
1196 const char __user * buf, size_t count, loff_t *ppos)
1197{
1198 struct task_struct *task;
1199 char buffer[PROC_NUMBUF], *end;
1200 int make_it_fail;
1201
1202 if (!capable(CAP_SYS_RESOURCE))
1203 return -EPERM;
1204 memset(buffer, 0, sizeof(buffer));
1205 if (count > sizeof(buffer) - 1)
1206 count = sizeof(buffer) - 1;
1207 if (copy_from_user(buffer, buf, count))
1208 return -EFAULT;
cba8aafe
VL
1209 make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1210 if (*end)
1211 return -EINVAL;
16caed31
DJ
1212 if (make_it_fail < 0 || make_it_fail > 1)
1213 return -EINVAL;
1214
496ad9aa 1215 task = get_proc_task(file_inode(file));
f4f154fd
AM
1216 if (!task)
1217 return -ESRCH;
1218 task->make_it_fail = make_it_fail;
1219 put_task_struct(task);
cba8aafe
VL
1220
1221 return count;
f4f154fd
AM
1222}
1223
00977a59 1224static const struct file_operations proc_fault_inject_operations = {
f4f154fd
AM
1225 .read = proc_fault_inject_read,
1226 .write = proc_fault_inject_write,
87df8424 1227 .llseek = generic_file_llseek,
f4f154fd
AM
1228};
1229#endif
1230
9745512c 1231
43ae34cb
IM
1232#ifdef CONFIG_SCHED_DEBUG
1233/*
1234 * Print out various scheduling related per-task fields:
1235 */
1236static int sched_show(struct seq_file *m, void *v)
1237{
1238 struct inode *inode = m->private;
1239 struct task_struct *p;
1240
43ae34cb
IM
1241 p = get_proc_task(inode);
1242 if (!p)
1243 return -ESRCH;
1244 proc_sched_show_task(p, m);
1245
1246 put_task_struct(p);
1247
1248 return 0;
1249}
1250
1251static ssize_t
1252sched_write(struct file *file, const char __user *buf,
1253 size_t count, loff_t *offset)
1254{
496ad9aa 1255 struct inode *inode = file_inode(file);
43ae34cb
IM
1256 struct task_struct *p;
1257
43ae34cb
IM
1258 p = get_proc_task(inode);
1259 if (!p)
1260 return -ESRCH;
1261 proc_sched_set_task(p);
1262
1263 put_task_struct(p);
1264
1265 return count;
1266}
1267
1268static int sched_open(struct inode *inode, struct file *filp)
1269{
c6a34058 1270 return single_open(filp, sched_show, inode);
43ae34cb
IM
1271}
1272
1273static const struct file_operations proc_pid_sched_operations = {
1274 .open = sched_open,
1275 .read = seq_read,
1276 .write = sched_write,
1277 .llseek = seq_lseek,
5ea473a1 1278 .release = single_release,
43ae34cb
IM
1279};
1280
1281#endif
1282
5091faa4
MG
1283#ifdef CONFIG_SCHED_AUTOGROUP
1284/*
1285 * Print out autogroup related information:
1286 */
1287static int sched_autogroup_show(struct seq_file *m, void *v)
1288{
1289 struct inode *inode = m->private;
1290 struct task_struct *p;
1291
1292 p = get_proc_task(inode);
1293 if (!p)
1294 return -ESRCH;
1295 proc_sched_autogroup_show_task(p, m);
1296
1297 put_task_struct(p);
1298
1299 return 0;
1300}
1301
1302static ssize_t
1303sched_autogroup_write(struct file *file, const char __user *buf,
1304 size_t count, loff_t *offset)
1305{
496ad9aa 1306 struct inode *inode = file_inode(file);
5091faa4
MG
1307 struct task_struct *p;
1308 char buffer[PROC_NUMBUF];
0a8cb8e3 1309 int nice;
5091faa4
MG
1310 int err;
1311
1312 memset(buffer, 0, sizeof(buffer));
1313 if (count > sizeof(buffer) - 1)
1314 count = sizeof(buffer) - 1;
1315 if (copy_from_user(buffer, buf, count))
1316 return -EFAULT;
1317
0a8cb8e3
AD
1318 err = kstrtoint(strstrip(buffer), 0, &nice);
1319 if (err < 0)
1320 return err;
5091faa4
MG
1321
1322 p = get_proc_task(inode);
1323 if (!p)
1324 return -ESRCH;
1325
2e5b5b3a 1326 err = proc_sched_autogroup_set_nice(p, nice);
5091faa4
MG
1327 if (err)
1328 count = err;
1329
1330 put_task_struct(p);
1331
1332 return count;
1333}
1334
1335static int sched_autogroup_open(struct inode *inode, struct file *filp)
1336{
1337 int ret;
1338
1339 ret = single_open(filp, sched_autogroup_show, NULL);
1340 if (!ret) {
1341 struct seq_file *m = filp->private_data;
1342
1343 m->private = inode;
1344 }
1345 return ret;
1346}
1347
1348static const struct file_operations proc_pid_sched_autogroup_operations = {
1349 .open = sched_autogroup_open,
1350 .read = seq_read,
1351 .write = sched_autogroup_write,
1352 .llseek = seq_lseek,
1353 .release = single_release,
1354};
1355
1356#endif /* CONFIG_SCHED_AUTOGROUP */
1357
4614a696
JS
1358static ssize_t comm_write(struct file *file, const char __user *buf,
1359 size_t count, loff_t *offset)
1360{
496ad9aa 1361 struct inode *inode = file_inode(file);
4614a696
JS
1362 struct task_struct *p;
1363 char buffer[TASK_COMM_LEN];
830e0fc9 1364 const size_t maxlen = sizeof(buffer) - 1;
4614a696
JS
1365
1366 memset(buffer, 0, sizeof(buffer));
830e0fc9 1367 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
4614a696
JS
1368 return -EFAULT;
1369
1370 p = get_proc_task(inode);
1371 if (!p)
1372 return -ESRCH;
1373
1374 if (same_thread_group(current, p))
1375 set_task_comm(p, buffer);
1376 else
1377 count = -EINVAL;
1378
1379 put_task_struct(p);
1380
1381 return count;
1382}
1383
1384static int comm_show(struct seq_file *m, void *v)
1385{
1386 struct inode *inode = m->private;
1387 struct task_struct *p;
1388
1389 p = get_proc_task(inode);
1390 if (!p)
1391 return -ESRCH;
1392
1393 task_lock(p);
1394 seq_printf(m, "%s\n", p->comm);
1395 task_unlock(p);
1396
1397 put_task_struct(p);
1398
1399 return 0;
1400}
1401
1402static int comm_open(struct inode *inode, struct file *filp)
1403{
c6a34058 1404 return single_open(filp, comm_show, inode);
4614a696
JS
1405}
1406
1407static const struct file_operations proc_pid_set_comm_operations = {
1408 .open = comm_open,
1409 .read = seq_read,
1410 .write = comm_write,
1411 .llseek = seq_lseek,
1412 .release = single_release,
1413};
1414
7773fbc5 1415static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
925d1c40
MH
1416{
1417 struct task_struct *task;
1418 struct mm_struct *mm;
1419 struct file *exe_file;
1420
7773fbc5 1421 task = get_proc_task(dentry->d_inode);
925d1c40
MH
1422 if (!task)
1423 return -ENOENT;
1424 mm = get_task_mm(task);
1425 put_task_struct(task);
1426 if (!mm)
1427 return -ENOENT;
1428 exe_file = get_mm_exe_file(mm);
1429 mmput(mm);
1430 if (exe_file) {
1431 *exe_path = exe_file->f_path;
1432 path_get(&exe_file->f_path);
1433 fput(exe_file);
1434 return 0;
1435 } else
1436 return -ENOENT;
1437}
1438
008b150a 1439static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1da177e4
LT
1440{
1441 struct inode *inode = dentry->d_inode;
408ef013 1442 struct path path;
1da177e4
LT
1443 int error = -EACCES;
1444
778c1144
EB
1445 /* Are we allowed to snoop on the tasks file descriptors? */
1446 if (!proc_fd_access_allowed(inode))
1da177e4 1447 goto out;
1da177e4 1448
408ef013
CH
1449 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1450 if (error)
1451 goto out;
1452
b5fb63c1 1453 nd_jump_link(nd, &path);
408ef013 1454 return NULL;
1da177e4 1455out:
008b150a 1456 return ERR_PTR(error);
1da177e4
LT
1457}
1458
3dcd25f3 1459static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1da177e4 1460{
e12ba74d 1461 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
3dcd25f3 1462 char *pathname;
1da177e4
LT
1463 int len;
1464
1465 if (!tmp)
1466 return -ENOMEM;
0c28f287 1467
7b2a69ba 1468 pathname = d_path(path, tmp, PAGE_SIZE);
3dcd25f3
JB
1469 len = PTR_ERR(pathname);
1470 if (IS_ERR(pathname))
1da177e4 1471 goto out;
3dcd25f3 1472 len = tmp + PAGE_SIZE - 1 - pathname;
1da177e4
LT
1473
1474 if (len > buflen)
1475 len = buflen;
3dcd25f3 1476 if (copy_to_user(buffer, pathname, len))
1da177e4
LT
1477 len = -EFAULT;
1478 out:
1479 free_page((unsigned long)tmp);
1480 return len;
1481}
1482
1483static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1484{
1485 int error = -EACCES;
1486 struct inode *inode = dentry->d_inode;
3dcd25f3 1487 struct path path;
1da177e4 1488
778c1144
EB
1489 /* Are we allowed to snoop on the tasks file descriptors? */
1490 if (!proc_fd_access_allowed(inode))
1da177e4 1491 goto out;
1da177e4 1492
7773fbc5 1493 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1da177e4
LT
1494 if (error)
1495 goto out;
1496
3dcd25f3
JB
1497 error = do_proc_readlink(&path, buffer, buflen);
1498 path_put(&path);
1da177e4 1499out:
1da177e4
LT
1500 return error;
1501}
1502
faf60af1 1503const struct inode_operations proc_pid_link_inode_operations = {
1da177e4 1504 .readlink = proc_pid_readlink,
6d76fa58
LT
1505 .follow_link = proc_pid_follow_link,
1506 .setattr = proc_setattr,
1da177e4
LT
1507};
1508
28a6d671
EB
1509
1510/* building an inode */
1511
6b4e306a 1512struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
28a6d671
EB
1513{
1514 struct inode * inode;
1515 struct proc_inode *ei;
c69e8d9c 1516 const struct cred *cred;
1da177e4 1517
28a6d671 1518 /* We need a new inode */
1da177e4 1519
28a6d671
EB
1520 inode = new_inode(sb);
1521 if (!inode)
1522 goto out;
1523
1524 /* Common stuff */
1525 ei = PROC_I(inode);
85fe4025 1526 inode->i_ino = get_next_ino();
28a6d671 1527 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
28a6d671
EB
1528 inode->i_op = &proc_def_inode_operations;
1529
1530 /*
1531 * grab the reference to task.
1532 */
1a657f78 1533 ei->pid = get_task_pid(task, PIDTYPE_PID);
28a6d671
EB
1534 if (!ei->pid)
1535 goto out_unlock;
1536
28a6d671 1537 if (task_dumpable(task)) {
c69e8d9c
DH
1538 rcu_read_lock();
1539 cred = __task_cred(task);
1540 inode->i_uid = cred->euid;
1541 inode->i_gid = cred->egid;
1542 rcu_read_unlock();
1da177e4 1543 }
28a6d671
EB
1544 security_task_to_inode(task, inode);
1545
1da177e4 1546out:
28a6d671
EB
1547 return inode;
1548
1549out_unlock:
1550 iput(inode);
1551 return NULL;
1da177e4
LT
1552}
1553
6b4e306a 1554int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1da177e4 1555{
1da177e4 1556 struct inode *inode = dentry->d_inode;
28a6d671 1557 struct task_struct *task;
c69e8d9c 1558 const struct cred *cred;
0499680a 1559 struct pid_namespace *pid = dentry->d_sb->s_fs_info;
c69e8d9c 1560
28a6d671 1561 generic_fillattr(inode, stat);
1da177e4 1562
28a6d671 1563 rcu_read_lock();
dcb0f222
EB
1564 stat->uid = GLOBAL_ROOT_UID;
1565 stat->gid = GLOBAL_ROOT_GID;
28a6d671
EB
1566 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1567 if (task) {
0499680a
VK
1568 if (!has_pid_permissions(pid, task, 2)) {
1569 rcu_read_unlock();
1570 /*
1571 * This doesn't prevent learning whether PID exists,
1572 * it only makes getattr() consistent with readdir().
1573 */
1574 return -ENOENT;
1575 }
28a6d671
EB
1576 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1577 task_dumpable(task)) {
c69e8d9c
DH
1578 cred = __task_cred(task);
1579 stat->uid = cred->euid;
1580 stat->gid = cred->egid;
1da177e4
LT
1581 }
1582 }
28a6d671 1583 rcu_read_unlock();
d6e71144 1584 return 0;
1da177e4
LT
1585}
1586
1da177e4
LT
1587/* dentry stuff */
1588
1589/*
1590 * Exceptional case: normally we are not allowed to unhash a busy
1591 * directory. In this case, however, we can do it - no aliasing problems
1592 * due to the way we treat inodes.
1593 *
1594 * Rewrite the inode's ownerships here because the owning task may have
1595 * performed a setuid(), etc.
99f89551
EB
1596 *
1597 * Before the /proc/pid/status file was created the only way to read
1598 * the effective uid of a /process was to stat /proc/pid. Reading
1599 * /proc/pid/status is slow enough that procps and other packages
1600 * kept stating /proc/pid. To keep the rules in /proc simple I have
1601 * made this apply to all per process world readable and executable
1602 * directories.
1da177e4 1603 */
0b728e19 1604int pid_revalidate(struct dentry *dentry, unsigned int flags)
1da177e4 1605{
34286d66
NP
1606 struct inode *inode;
1607 struct task_struct *task;
c69e8d9c
DH
1608 const struct cred *cred;
1609
0b728e19 1610 if (flags & LOOKUP_RCU)
34286d66
NP
1611 return -ECHILD;
1612
1613 inode = dentry->d_inode;
1614 task = get_proc_task(inode);
1615
99f89551
EB
1616 if (task) {
1617 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1618 task_dumpable(task)) {
c69e8d9c
DH
1619 rcu_read_lock();
1620 cred = __task_cred(task);
1621 inode->i_uid = cred->euid;
1622 inode->i_gid = cred->egid;
1623 rcu_read_unlock();
1da177e4 1624 } else {
dcb0f222
EB
1625 inode->i_uid = GLOBAL_ROOT_UID;
1626 inode->i_gid = GLOBAL_ROOT_GID;
1da177e4 1627 }
9ee8ab9f 1628 inode->i_mode &= ~(S_ISUID | S_ISGID);
1da177e4 1629 security_task_to_inode(task, inode);
99f89551 1630 put_task_struct(task);
1da177e4
LT
1631 return 1;
1632 }
1633 d_drop(dentry);
1634 return 0;
1635}
1636
d855a4b7
ON
1637static inline bool proc_inode_is_dead(struct inode *inode)
1638{
1639 return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1640}
1641
1dd704b6
DH
1642int pid_delete_dentry(const struct dentry *dentry)
1643{
1644 /* Is the task we represent dead?
1645 * If so, then don't put the dentry on the lru list,
1646 * kill it immediately.
1647 */
d855a4b7 1648 return proc_inode_is_dead(dentry->d_inode);
1dd704b6
DH
1649}
1650
6b4e306a 1651const struct dentry_operations pid_dentry_operations =
28a6d671
EB
1652{
1653 .d_revalidate = pid_revalidate,
1654 .d_delete = pid_delete_dentry,
1655};
1656
1657/* Lookups */
1658
1c0d04c9
EB
1659/*
1660 * Fill a directory entry.
1661 *
1662 * If possible create the dcache entry and derive our inode number and
1663 * file type from dcache entry.
1664 *
1665 * Since all of the proc inode numbers are dynamically generated, the inode
1666 * numbers do not exist until the inode is cache. This means creating the
1667 * the dcache entry in readdir is necessary to keep the inode numbers
1668 * reported by readdir in sync with the inode numbers reported
1669 * by stat.
1670 */
f0c3b509 1671bool proc_fill_cache(struct file *file, struct dir_context *ctx,
6b4e306a 1672 const char *name, int len,
c5141e6d 1673 instantiate_t instantiate, struct task_struct *task, const void *ptr)
61a28784 1674{
f0c3b509 1675 struct dentry *child, *dir = file->f_path.dentry;
1df98b8b 1676 struct qstr qname = QSTR_INIT(name, len);
61a28784 1677 struct inode *inode;
1df98b8b
AV
1678 unsigned type;
1679 ino_t ino;
61a28784 1680
1df98b8b 1681 child = d_hash_and_lookup(dir, &qname);
61a28784 1682 if (!child) {
1df98b8b
AV
1683 child = d_alloc(dir, &qname);
1684 if (!child)
1685 goto end_instantiate;
1686 if (instantiate(dir->d_inode, child, task, ptr) < 0) {
1687 dput(child);
1688 goto end_instantiate;
61a28784
EB
1689 }
1690 }
61a28784 1691 inode = child->d_inode;
147ce699
AV
1692 ino = inode->i_ino;
1693 type = inode->i_mode >> 12;
61a28784 1694 dput(child);
f0c3b509 1695 return dir_emit(ctx, name, len, ino, type);
1df98b8b
AV
1696
1697end_instantiate:
1698 return dir_emit(ctx, name, len, 1, DT_UNKNOWN);
61a28784
EB
1699}
1700
640708a2
PE
1701#ifdef CONFIG_CHECKPOINT_RESTORE
1702
1703/*
1704 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1705 * which represent vma start and end addresses.
1706 */
1707static int dname_to_vma_addr(struct dentry *dentry,
1708 unsigned long *start, unsigned long *end)
1709{
1710 if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2)
1711 return -EINVAL;
1712
1713 return 0;
1714}
1715
0b728e19 1716static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
640708a2
PE
1717{
1718 unsigned long vm_start, vm_end;
1719 bool exact_vma_exists = false;
1720 struct mm_struct *mm = NULL;
1721 struct task_struct *task;
1722 const struct cred *cred;
1723 struct inode *inode;
1724 int status = 0;
1725
0b728e19 1726 if (flags & LOOKUP_RCU)
640708a2
PE
1727 return -ECHILD;
1728
1729 if (!capable(CAP_SYS_ADMIN)) {
41735818 1730 status = -EPERM;
640708a2
PE
1731 goto out_notask;
1732 }
1733
1734 inode = dentry->d_inode;
1735 task = get_proc_task(inode);
1736 if (!task)
1737 goto out_notask;
1738
2344bec7
CW
1739 mm = mm_access(task, PTRACE_MODE_READ);
1740 if (IS_ERR_OR_NULL(mm))
640708a2
PE
1741 goto out;
1742
1743 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1744 down_read(&mm->mmap_sem);
1745 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1746 up_read(&mm->mmap_sem);
1747 }
1748
1749 mmput(mm);
1750
1751 if (exact_vma_exists) {
1752 if (task_dumpable(task)) {
1753 rcu_read_lock();
1754 cred = __task_cred(task);
1755 inode->i_uid = cred->euid;
1756 inode->i_gid = cred->egid;
1757 rcu_read_unlock();
1758 } else {
dcb0f222
EB
1759 inode->i_uid = GLOBAL_ROOT_UID;
1760 inode->i_gid = GLOBAL_ROOT_GID;
640708a2
PE
1761 }
1762 security_task_to_inode(task, inode);
1763 status = 1;
1764 }
1765
1766out:
1767 put_task_struct(task);
1768
1769out_notask:
1770 if (status <= 0)
1771 d_drop(dentry);
1772
1773 return status;
1774}
1775
1776static const struct dentry_operations tid_map_files_dentry_operations = {
1777 .d_revalidate = map_files_d_revalidate,
1778 .d_delete = pid_delete_dentry,
1779};
1780
1781static int proc_map_files_get_link(struct dentry *dentry, struct path *path)
1782{
1783 unsigned long vm_start, vm_end;
1784 struct vm_area_struct *vma;
1785 struct task_struct *task;
1786 struct mm_struct *mm;
1787 int rc;
1788
1789 rc = -ENOENT;
1790 task = get_proc_task(dentry->d_inode);
1791 if (!task)
1792 goto out;
1793
1794 mm = get_task_mm(task);
1795 put_task_struct(task);
1796 if (!mm)
1797 goto out;
1798
1799 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
1800 if (rc)
1801 goto out_mmput;
1802
70335abb 1803 rc = -ENOENT;
640708a2
PE
1804 down_read(&mm->mmap_sem);
1805 vma = find_exact_vma(mm, vm_start, vm_end);
1806 if (vma && vma->vm_file) {
1807 *path = vma->vm_file->f_path;
1808 path_get(path);
1809 rc = 0;
1810 }
1811 up_read(&mm->mmap_sem);
1812
1813out_mmput:
1814 mmput(mm);
1815out:
1816 return rc;
1817}
1818
1819struct map_files_info {
7b540d06 1820 fmode_t mode;
640708a2
PE
1821 unsigned long len;
1822 unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
1823};
1824
c52a47ac 1825static int
640708a2
PE
1826proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
1827 struct task_struct *task, const void *ptr)
1828{
7b540d06 1829 fmode_t mode = (fmode_t)(unsigned long)ptr;
640708a2
PE
1830 struct proc_inode *ei;
1831 struct inode *inode;
1832
640708a2
PE
1833 inode = proc_pid_make_inode(dir->i_sb, task);
1834 if (!inode)
c52a47ac 1835 return -ENOENT;
640708a2
PE
1836
1837 ei = PROC_I(inode);
1838 ei->op.proc_get_link = proc_map_files_get_link;
1839
1840 inode->i_op = &proc_pid_link_inode_operations;
1841 inode->i_size = 64;
1842 inode->i_mode = S_IFLNK;
1843
7b540d06 1844 if (mode & FMODE_READ)
640708a2 1845 inode->i_mode |= S_IRUSR;
7b540d06 1846 if (mode & FMODE_WRITE)
640708a2
PE
1847 inode->i_mode |= S_IWUSR;
1848
1849 d_set_d_op(dentry, &tid_map_files_dentry_operations);
1850 d_add(dentry, inode);
1851
c52a47ac 1852 return 0;
640708a2
PE
1853}
1854
1855static struct dentry *proc_map_files_lookup(struct inode *dir,
00cd8dd3 1856 struct dentry *dentry, unsigned int flags)
640708a2
PE
1857{
1858 unsigned long vm_start, vm_end;
1859 struct vm_area_struct *vma;
1860 struct task_struct *task;
c52a47ac 1861 int result;
640708a2
PE
1862 struct mm_struct *mm;
1863
c52a47ac 1864 result = -EPERM;
640708a2
PE
1865 if (!capable(CAP_SYS_ADMIN))
1866 goto out;
1867
c52a47ac 1868 result = -ENOENT;
640708a2
PE
1869 task = get_proc_task(dir);
1870 if (!task)
1871 goto out;
1872
c52a47ac 1873 result = -EACCES;
eb94cd96 1874 if (!ptrace_may_access(task, PTRACE_MODE_READ))
640708a2
PE
1875 goto out_put_task;
1876
c52a47ac 1877 result = -ENOENT;
640708a2 1878 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
eb94cd96 1879 goto out_put_task;
640708a2
PE
1880
1881 mm = get_task_mm(task);
1882 if (!mm)
eb94cd96 1883 goto out_put_task;
640708a2
PE
1884
1885 down_read(&mm->mmap_sem);
1886 vma = find_exact_vma(mm, vm_start, vm_end);
1887 if (!vma)
1888 goto out_no_vma;
1889
05f56484
SK
1890 if (vma->vm_file)
1891 result = proc_map_files_instantiate(dir, dentry, task,
1892 (void *)(unsigned long)vma->vm_file->f_mode);
640708a2
PE
1893
1894out_no_vma:
1895 up_read(&mm->mmap_sem);
1896 mmput(mm);
640708a2
PE
1897out_put_task:
1898 put_task_struct(task);
1899out:
c52a47ac 1900 return ERR_PTR(result);
640708a2
PE
1901}
1902
1903static const struct inode_operations proc_map_files_inode_operations = {
1904 .lookup = proc_map_files_lookup,
1905 .permission = proc_fd_permission,
1906 .setattr = proc_setattr,
1907};
1908
1909static int
f0c3b509 1910proc_map_files_readdir(struct file *file, struct dir_context *ctx)
640708a2 1911{
640708a2
PE
1912 struct vm_area_struct *vma;
1913 struct task_struct *task;
1914 struct mm_struct *mm;
f0c3b509
AV
1915 unsigned long nr_files, pos, i;
1916 struct flex_array *fa = NULL;
1917 struct map_files_info info;
1918 struct map_files_info *p;
640708a2
PE
1919 int ret;
1920
41735818 1921 ret = -EPERM;
640708a2
PE
1922 if (!capable(CAP_SYS_ADMIN))
1923 goto out;
1924
1925 ret = -ENOENT;
f0c3b509 1926 task = get_proc_task(file_inode(file));
640708a2
PE
1927 if (!task)
1928 goto out;
1929
1930 ret = -EACCES;
eb94cd96 1931 if (!ptrace_may_access(task, PTRACE_MODE_READ))
640708a2
PE
1932 goto out_put_task;
1933
1934 ret = 0;
f0c3b509
AV
1935 if (!dir_emit_dots(file, ctx))
1936 goto out_put_task;
640708a2 1937
f0c3b509
AV
1938 mm = get_task_mm(task);
1939 if (!mm)
1940 goto out_put_task;
1941 down_read(&mm->mmap_sem);
640708a2 1942
f0c3b509 1943 nr_files = 0;
640708a2 1944
f0c3b509
AV
1945 /*
1946 * We need two passes here:
1947 *
1948 * 1) Collect vmas of mapped files with mmap_sem taken
1949 * 2) Release mmap_sem and instantiate entries
1950 *
1951 * otherwise we get lockdep complained, since filldir()
1952 * routine might require mmap_sem taken in might_fault().
1953 */
640708a2 1954
f0c3b509
AV
1955 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
1956 if (vma->vm_file && ++pos > ctx->pos)
1957 nr_files++;
1958 }
1959
1960 if (nr_files) {
1961 fa = flex_array_alloc(sizeof(info), nr_files,
1962 GFP_KERNEL);
1963 if (!fa || flex_array_prealloc(fa, 0, nr_files,
1964 GFP_KERNEL)) {
1965 ret = -ENOMEM;
1966 if (fa)
1967 flex_array_free(fa);
1968 up_read(&mm->mmap_sem);
1969 mmput(mm);
1970 goto out_put_task;
640708a2 1971 }
f0c3b509
AV
1972 for (i = 0, vma = mm->mmap, pos = 2; vma;
1973 vma = vma->vm_next) {
1974 if (!vma->vm_file)
1975 continue;
1976 if (++pos <= ctx->pos)
1977 continue;
1978
1979 info.mode = vma->vm_file->f_mode;
1980 info.len = snprintf(info.name,
1981 sizeof(info.name), "%lx-%lx",
1982 vma->vm_start, vma->vm_end);
1983 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
1984 BUG();
640708a2 1985 }
640708a2 1986 }
f0c3b509
AV
1987 up_read(&mm->mmap_sem);
1988
1989 for (i = 0; i < nr_files; i++) {
1990 p = flex_array_get(fa, i);
1991 if (!proc_fill_cache(file, ctx,
1992 p->name, p->len,
1993 proc_map_files_instantiate,
1994 task,
1995 (void *)(unsigned long)p->mode))
1996 break;
1997 ctx->pos++;
640708a2 1998 }
f0c3b509
AV
1999 if (fa)
2000 flex_array_free(fa);
2001 mmput(mm);
640708a2 2002
640708a2
PE
2003out_put_task:
2004 put_task_struct(task);
2005out:
2006 return ret;
2007}
2008
2009static const struct file_operations proc_map_files_operations = {
2010 .read = generic_read_dir,
f0c3b509 2011 .iterate = proc_map_files_readdir,
640708a2
PE
2012 .llseek = default_llseek,
2013};
2014
48f6a7a5
PE
2015struct timers_private {
2016 struct pid *pid;
2017 struct task_struct *task;
2018 struct sighand_struct *sighand;
57b8015e 2019 struct pid_namespace *ns;
48f6a7a5
PE
2020 unsigned long flags;
2021};
2022
2023static void *timers_start(struct seq_file *m, loff_t *pos)
2024{
2025 struct timers_private *tp = m->private;
2026
2027 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2028 if (!tp->task)
2029 return ERR_PTR(-ESRCH);
2030
2031 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2032 if (!tp->sighand)
2033 return ERR_PTR(-ESRCH);
2034
2035 return seq_list_start(&tp->task->signal->posix_timers, *pos);
2036}
2037
2038static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2039{
2040 struct timers_private *tp = m->private;
2041 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2042}
2043
2044static void timers_stop(struct seq_file *m, void *v)
2045{
2046 struct timers_private *tp = m->private;
2047
2048 if (tp->sighand) {
2049 unlock_task_sighand(tp->task, &tp->flags);
2050 tp->sighand = NULL;
2051 }
2052
2053 if (tp->task) {
2054 put_task_struct(tp->task);
2055 tp->task = NULL;
2056 }
2057}
2058
2059static int show_timer(struct seq_file *m, void *v)
2060{
2061 struct k_itimer *timer;
57b8015e
PE
2062 struct timers_private *tp = m->private;
2063 int notify;
cedbccab 2064 static const char * const nstr[] = {
57b8015e
PE
2065 [SIGEV_SIGNAL] = "signal",
2066 [SIGEV_NONE] = "none",
2067 [SIGEV_THREAD] = "thread",
2068 };
48f6a7a5
PE
2069
2070 timer = list_entry((struct list_head *)v, struct k_itimer, list);
57b8015e
PE
2071 notify = timer->it_sigev_notify;
2072
48f6a7a5 2073 seq_printf(m, "ID: %d\n", timer->it_id);
57b8015e
PE
2074 seq_printf(m, "signal: %d/%p\n", timer->sigq->info.si_signo,
2075 timer->sigq->info.si_value.sival_ptr);
2076 seq_printf(m, "notify: %s/%s.%d\n",
2077 nstr[notify & ~SIGEV_THREAD_ID],
2078 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2079 pid_nr_ns(timer->it_pid, tp->ns));
15ef0298 2080 seq_printf(m, "ClockID: %d\n", timer->it_clock);
48f6a7a5
PE
2081
2082 return 0;
2083}
2084
2085static const struct seq_operations proc_timers_seq_ops = {
2086 .start = timers_start,
2087 .next = timers_next,
2088 .stop = timers_stop,
2089 .show = show_timer,
2090};
2091
2092static int proc_timers_open(struct inode *inode, struct file *file)
2093{
2094 struct timers_private *tp;
2095
2096 tp = __seq_open_private(file, &proc_timers_seq_ops,
2097 sizeof(struct timers_private));
2098 if (!tp)
2099 return -ENOMEM;
2100
2101 tp->pid = proc_pid(inode);
57b8015e 2102 tp->ns = inode->i_sb->s_fs_info;
48f6a7a5
PE
2103 return 0;
2104}
2105
2106static const struct file_operations proc_timers_operations = {
2107 .open = proc_timers_open,
2108 .read = seq_read,
2109 .llseek = seq_lseek,
2110 .release = seq_release_private,
2111};
640708a2
PE
2112#endif /* CONFIG_CHECKPOINT_RESTORE */
2113
c52a47ac 2114static int proc_pident_instantiate(struct inode *dir,
c5141e6d 2115 struct dentry *dentry, struct task_struct *task, const void *ptr)
444ceed8 2116{
c5141e6d 2117 const struct pid_entry *p = ptr;
444ceed8
EB
2118 struct inode *inode;
2119 struct proc_inode *ei;
444ceed8 2120
61a28784 2121 inode = proc_pid_make_inode(dir->i_sb, task);
444ceed8
EB
2122 if (!inode)
2123 goto out;
2124
2125 ei = PROC_I(inode);
2126 inode->i_mode = p->mode;
2127 if (S_ISDIR(inode->i_mode))
bfe86848 2128 set_nlink(inode, 2); /* Use getattr to fix if necessary */
444ceed8
EB
2129 if (p->iop)
2130 inode->i_op = p->iop;
2131 if (p->fop)
2132 inode->i_fop = p->fop;
2133 ei->op = p->op;
fb045adb 2134 d_set_d_op(dentry, &pid_dentry_operations);
444ceed8
EB
2135 d_add(dentry, inode);
2136 /* Close the race of the process dying before we return the dentry */
0b728e19 2137 if (pid_revalidate(dentry, 0))
c52a47ac 2138 return 0;
444ceed8 2139out:
c52a47ac 2140 return -ENOENT;
444ceed8
EB
2141}
2142
1da177e4
LT
2143static struct dentry *proc_pident_lookup(struct inode *dir,
2144 struct dentry *dentry,
c5141e6d 2145 const struct pid_entry *ents,
7bcd6b0e 2146 unsigned int nents)
1da177e4 2147{
c52a47ac 2148 int error;
99f89551 2149 struct task_struct *task = get_proc_task(dir);
c5141e6d 2150 const struct pid_entry *p, *last;
1da177e4 2151
c52a47ac 2152 error = -ENOENT;
1da177e4 2153
99f89551
EB
2154 if (!task)
2155 goto out_no_task;
1da177e4 2156
20cdc894
EB
2157 /*
2158 * Yes, it does not scale. And it should not. Don't add
2159 * new entries into /proc/<tgid>/ without very good reasons.
2160 */
7bcd6b0e
EB
2161 last = &ents[nents - 1];
2162 for (p = ents; p <= last; p++) {
1da177e4
LT
2163 if (p->len != dentry->d_name.len)
2164 continue;
2165 if (!memcmp(dentry->d_name.name, p->name, p->len))
2166 break;
2167 }
7bcd6b0e 2168 if (p > last)
1da177e4
LT
2169 goto out;
2170
444ceed8 2171 error = proc_pident_instantiate(dir, dentry, task, p);
1da177e4 2172out:
99f89551
EB
2173 put_task_struct(task);
2174out_no_task:
c52a47ac 2175 return ERR_PTR(error);
1da177e4
LT
2176}
2177
f0c3b509 2178static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
c5141e6d 2179 const struct pid_entry *ents, unsigned int nents)
28a6d671 2180{
f0c3b509
AV
2181 struct task_struct *task = get_proc_task(file_inode(file));
2182 const struct pid_entry *p;
28a6d671 2183
28a6d671 2184 if (!task)
f0c3b509 2185 return -ENOENT;
28a6d671 2186
f0c3b509
AV
2187 if (!dir_emit_dots(file, ctx))
2188 goto out;
2189
2190 if (ctx->pos >= nents + 2)
2191 goto out;
28a6d671 2192
f0c3b509
AV
2193 for (p = ents + (ctx->pos - 2); p <= ents + nents - 1; p++) {
2194 if (!proc_fill_cache(file, ctx, p->name, p->len,
2195 proc_pident_instantiate, task, p))
2196 break;
2197 ctx->pos++;
2198 }
28a6d671 2199out:
61a28784 2200 put_task_struct(task);
f0c3b509 2201 return 0;
1da177e4
LT
2202}
2203
28a6d671
EB
2204#ifdef CONFIG_SECURITY
2205static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2206 size_t count, loff_t *ppos)
2207{
496ad9aa 2208 struct inode * inode = file_inode(file);
04ff9708 2209 char *p = NULL;
28a6d671
EB
2210 ssize_t length;
2211 struct task_struct *task = get_proc_task(inode);
2212
28a6d671 2213 if (!task)
04ff9708 2214 return -ESRCH;
28a6d671
EB
2215
2216 length = security_getprocattr(task,
2fddfeef 2217 (char*)file->f_path.dentry->d_name.name,
04ff9708 2218 &p);
28a6d671 2219 put_task_struct(task);
04ff9708
AV
2220 if (length > 0)
2221 length = simple_read_from_buffer(buf, count, ppos, p, length);
2222 kfree(p);
28a6d671 2223 return length;
1da177e4
LT
2224}
2225
28a6d671
EB
2226static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2227 size_t count, loff_t *ppos)
2228{
496ad9aa 2229 struct inode * inode = file_inode(file);
28a6d671
EB
2230 char *page;
2231 ssize_t length;
2232 struct task_struct *task = get_proc_task(inode);
2233
2234 length = -ESRCH;
2235 if (!task)
2236 goto out_no_task;
2237 if (count > PAGE_SIZE)
2238 count = PAGE_SIZE;
2239
2240 /* No partial writes. */
2241 length = -EINVAL;
2242 if (*ppos != 0)
2243 goto out;
2244
2245 length = -ENOMEM;
e12ba74d 2246 page = (char*)__get_free_page(GFP_TEMPORARY);
28a6d671
EB
2247 if (!page)
2248 goto out;
2249
2250 length = -EFAULT;
2251 if (copy_from_user(page, buf, count))
2252 goto out_free;
2253
107db7c7 2254 /* Guard against adverse ptrace interaction */
9b1bf12d 2255 length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
107db7c7
DH
2256 if (length < 0)
2257 goto out_free;
2258
28a6d671 2259 length = security_setprocattr(task,
2fddfeef 2260 (char*)file->f_path.dentry->d_name.name,
28a6d671 2261 (void*)page, count);
9b1bf12d 2262 mutex_unlock(&task->signal->cred_guard_mutex);
28a6d671
EB
2263out_free:
2264 free_page((unsigned long) page);
2265out:
2266 put_task_struct(task);
2267out_no_task:
2268 return length;
2269}
2270
00977a59 2271static const struct file_operations proc_pid_attr_operations = {
28a6d671
EB
2272 .read = proc_pid_attr_read,
2273 .write = proc_pid_attr_write,
87df8424 2274 .llseek = generic_file_llseek,
28a6d671
EB
2275};
2276
c5141e6d 2277static const struct pid_entry attr_dir_stuff[] = {
631f9c18
AD
2278 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2279 REG("prev", S_IRUGO, proc_pid_attr_operations),
2280 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2281 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2282 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2283 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
28a6d671
EB
2284};
2285
f0c3b509 2286static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
28a6d671 2287{
f0c3b509
AV
2288 return proc_pident_readdir(file, ctx,
2289 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
28a6d671
EB
2290}
2291
00977a59 2292static const struct file_operations proc_attr_dir_operations = {
1da177e4 2293 .read = generic_read_dir,
f0c3b509 2294 .iterate = proc_attr_dir_readdir,
6038f373 2295 .llseek = default_llseek,
1da177e4
LT
2296};
2297
72d9dcfc 2298static struct dentry *proc_attr_dir_lookup(struct inode *dir,
00cd8dd3 2299 struct dentry *dentry, unsigned int flags)
28a6d671 2300{
7bcd6b0e
EB
2301 return proc_pident_lookup(dir, dentry,
2302 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
28a6d671
EB
2303}
2304
c5ef1c42 2305static const struct inode_operations proc_attr_dir_inode_operations = {
72d9dcfc 2306 .lookup = proc_attr_dir_lookup,
99f89551 2307 .getattr = pid_getattr,
6d76fa58 2308 .setattr = proc_setattr,
1da177e4
LT
2309};
2310
28a6d671
EB
2311#endif
2312
698ba7b5 2313#ifdef CONFIG_ELF_CORE
3cb4a0bb
KH
2314static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2315 size_t count, loff_t *ppos)
2316{
496ad9aa 2317 struct task_struct *task = get_proc_task(file_inode(file));
3cb4a0bb
KH
2318 struct mm_struct *mm;
2319 char buffer[PROC_NUMBUF];
2320 size_t len;
2321 int ret;
2322
2323 if (!task)
2324 return -ESRCH;
2325
2326 ret = 0;
2327 mm = get_task_mm(task);
2328 if (mm) {
2329 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2330 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2331 MMF_DUMP_FILTER_SHIFT));
2332 mmput(mm);
2333 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2334 }
2335
2336 put_task_struct(task);
2337
2338 return ret;
2339}
2340
2341static ssize_t proc_coredump_filter_write(struct file *file,
2342 const char __user *buf,
2343 size_t count,
2344 loff_t *ppos)
2345{
2346 struct task_struct *task;
2347 struct mm_struct *mm;
2348 char buffer[PROC_NUMBUF], *end;
2349 unsigned int val;
2350 int ret;
2351 int i;
2352 unsigned long mask;
2353
2354 ret = -EFAULT;
2355 memset(buffer, 0, sizeof(buffer));
2356 if (count > sizeof(buffer) - 1)
2357 count = sizeof(buffer) - 1;
2358 if (copy_from_user(buffer, buf, count))
2359 goto out_no_task;
2360
2361 ret = -EINVAL;
2362 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2363 if (*end == '\n')
2364 end++;
2365 if (end - buffer == 0)
2366 goto out_no_task;
2367
2368 ret = -ESRCH;
496ad9aa 2369 task = get_proc_task(file_inode(file));
3cb4a0bb
KH
2370 if (!task)
2371 goto out_no_task;
2372
2373 ret = end - buffer;
2374 mm = get_task_mm(task);
2375 if (!mm)
2376 goto out_no_mm;
2377
2378 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2379 if (val & mask)
2380 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2381 else
2382 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2383 }
2384
2385 mmput(mm);
2386 out_no_mm:
2387 put_task_struct(task);
2388 out_no_task:
2389 return ret;
2390}
2391
2392static const struct file_operations proc_coredump_filter_operations = {
2393 .read = proc_coredump_filter_read,
2394 .write = proc_coredump_filter_write,
87df8424 2395 .llseek = generic_file_llseek,
3cb4a0bb
KH
2396};
2397#endif
2398
aba76fdb 2399#ifdef CONFIG_TASK_IO_ACCOUNTING
297c5d92
AR
2400static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
2401{
940389b8 2402 struct task_io_accounting acct = task->ioac;
5995477a 2403 unsigned long flags;
293eb1e7 2404 int result;
5995477a 2405
293eb1e7
VK
2406 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2407 if (result)
2408 return result;
2409
2410 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
2411 result = -EACCES;
2412 goto out_unlock;
2413 }
1d1221f3 2414
5995477a
AR
2415 if (whole && lock_task_sighand(task, &flags)) {
2416 struct task_struct *t = task;
2417
2418 task_io_accounting_add(&acct, &task->signal->ioac);
2419 while_each_thread(task, t)
2420 task_io_accounting_add(&acct, &t->ioac);
2421
2422 unlock_task_sighand(task, &flags);
297c5d92 2423 }
293eb1e7 2424 result = sprintf(buffer,
aba76fdb
AM
2425 "rchar: %llu\n"
2426 "wchar: %llu\n"
2427 "syscr: %llu\n"
2428 "syscw: %llu\n"
2429 "read_bytes: %llu\n"
2430 "write_bytes: %llu\n"
2431 "cancelled_write_bytes: %llu\n",
7c44319d
AB
2432 (unsigned long long)acct.rchar,
2433 (unsigned long long)acct.wchar,
2434 (unsigned long long)acct.syscr,
2435 (unsigned long long)acct.syscw,
2436 (unsigned long long)acct.read_bytes,
2437 (unsigned long long)acct.write_bytes,
2438 (unsigned long long)acct.cancelled_write_bytes);
293eb1e7
VK
2439out_unlock:
2440 mutex_unlock(&task->signal->cred_guard_mutex);
2441 return result;
297c5d92
AR
2442}
2443
2444static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2445{
2446 return do_io_accounting(task, buffer, 0);
aba76fdb 2447}
297c5d92
AR
2448
2449static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2450{
2451 return do_io_accounting(task, buffer, 1);
2452}
2453#endif /* CONFIG_TASK_IO_ACCOUNTING */
aba76fdb 2454
22d917d8
EB
2455#ifdef CONFIG_USER_NS
2456static int proc_id_map_open(struct inode *inode, struct file *file,
ccf94f1b 2457 const struct seq_operations *seq_ops)
22d917d8
EB
2458{
2459 struct user_namespace *ns = NULL;
2460 struct task_struct *task;
2461 struct seq_file *seq;
2462 int ret = -EINVAL;
2463
2464 task = get_proc_task(inode);
2465 if (task) {
2466 rcu_read_lock();
2467 ns = get_user_ns(task_cred_xxx(task, user_ns));
2468 rcu_read_unlock();
2469 put_task_struct(task);
2470 }
2471 if (!ns)
2472 goto err;
2473
2474 ret = seq_open(file, seq_ops);
2475 if (ret)
2476 goto err_put_ns;
2477
2478 seq = file->private_data;
2479 seq->private = ns;
2480
2481 return 0;
2482err_put_ns:
2483 put_user_ns(ns);
2484err:
2485 return ret;
2486}
2487
2488static int proc_id_map_release(struct inode *inode, struct file *file)
2489{
2490 struct seq_file *seq = file->private_data;
2491 struct user_namespace *ns = seq->private;
2492 put_user_ns(ns);
2493 return seq_release(inode, file);
2494}
2495
2496static int proc_uid_map_open(struct inode *inode, struct file *file)
2497{
2498 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2499}
2500
2501static int proc_gid_map_open(struct inode *inode, struct file *file)
2502{
2503 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2504}
2505
f76d207a
EB
2506static int proc_projid_map_open(struct inode *inode, struct file *file)
2507{
2508 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2509}
2510
22d917d8
EB
2511static const struct file_operations proc_uid_map_operations = {
2512 .open = proc_uid_map_open,
2513 .write = proc_uid_map_write,
2514 .read = seq_read,
2515 .llseek = seq_lseek,
2516 .release = proc_id_map_release,
2517};
2518
2519static const struct file_operations proc_gid_map_operations = {
2520 .open = proc_gid_map_open,
2521 .write = proc_gid_map_write,
2522 .read = seq_read,
2523 .llseek = seq_lseek,
2524 .release = proc_id_map_release,
2525};
f76d207a
EB
2526
2527static const struct file_operations proc_projid_map_operations = {
2528 .open = proc_projid_map_open,
2529 .write = proc_projid_map_write,
2530 .read = seq_read,
2531 .llseek = seq_lseek,
2532 .release = proc_id_map_release,
2533};
22d917d8
EB
2534#endif /* CONFIG_USER_NS */
2535
47830723
KC
2536static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2537 struct pid *pid, struct task_struct *task)
2538{
a9712bc1
AV
2539 int err = lock_trace(task);
2540 if (!err) {
2541 seq_printf(m, "%08x\n", task->personality);
2542 unlock_trace(task);
2543 }
2544 return err;
47830723
KC
2545}
2546
28a6d671
EB
2547/*
2548 * Thread groups
2549 */
00977a59 2550static const struct file_operations proc_task_operations;
c5ef1c42 2551static const struct inode_operations proc_task_inode_operations;
20cdc894 2552
c5141e6d 2553static const struct pid_entry tgid_base_stuff[] = {
631f9c18
AD
2554 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2555 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
640708a2
PE
2556#ifdef CONFIG_CHECKPOINT_RESTORE
2557 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
2558#endif
631f9c18 2559 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
6b4e306a 2560 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
b2211a36 2561#ifdef CONFIG_NET
631f9c18 2562 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
b2211a36 2563#endif
631f9c18 2564 REG("environ", S_IRUSR, proc_environ_operations),
f9ea536e 2565 ONE("auxv", S_IRUSR, proc_pid_auxv),
631f9c18 2566 ONE("status", S_IRUGO, proc_pid_status),
35a35046 2567 ONE("personality", S_IRUSR, proc_pid_personality),
1c963eb1 2568 ONE("limits", S_IRUGO, proc_pid_limits),
43ae34cb 2569#ifdef CONFIG_SCHED_DEBUG
631f9c18 2570 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
5091faa4
MG
2571#endif
2572#ifdef CONFIG_SCHED_AUTOGROUP
2573 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
ebcb6734 2574#endif
4614a696 2575 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
ebcb6734 2576#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
09d93bd6 2577 ONE("syscall", S_IRUSR, proc_pid_syscall),
43ae34cb 2578#endif
2ca66ff7 2579 ONE("cmdline", S_IRUGO, proc_pid_cmdline),
631f9c18
AD
2580 ONE("stat", S_IRUGO, proc_tgid_stat),
2581 ONE("statm", S_IRUGO, proc_pid_statm),
b7643757 2582 REG("maps", S_IRUGO, proc_pid_maps_operations),
28a6d671 2583#ifdef CONFIG_NUMA
b7643757 2584 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
28a6d671 2585#endif
631f9c18
AD
2586 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2587 LNK("cwd", proc_cwd_link),
2588 LNK("root", proc_root_link),
2589 LNK("exe", proc_exe_link),
2590 REG("mounts", S_IRUGO, proc_mounts_operations),
2591 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2592 REG("mountstats", S_IRUSR, proc_mountstats_operations),
1e883281 2593#ifdef CONFIG_PROC_PAGE_MONITOR
631f9c18 2594 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
b7643757 2595 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
32ed74a4 2596 REG("pagemap", S_IRUSR, proc_pagemap_operations),
28a6d671
EB
2597#endif
2598#ifdef CONFIG_SECURITY
631f9c18 2599 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
28a6d671
EB
2600#endif
2601#ifdef CONFIG_KALLSYMS
edfcd606 2602 ONE("wchan", S_IRUGO, proc_pid_wchan),
28a6d671 2603#endif
2ec220e2 2604#ifdef CONFIG_STACKTRACE
35a35046 2605 ONE("stack", S_IRUSR, proc_pid_stack),
28a6d671
EB
2606#endif
2607#ifdef CONFIG_SCHEDSTATS
f6e826ca 2608 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
28a6d671 2609#endif
9745512c 2610#ifdef CONFIG_LATENCYTOP
631f9c18 2611 REG("latency", S_IRUGO, proc_lstats_operations),
9745512c 2612#endif
8793d854 2613#ifdef CONFIG_PROC_PID_CPUSET
631f9c18 2614 REG("cpuset", S_IRUGO, proc_cpuset_operations),
a424316c
PM
2615#endif
2616#ifdef CONFIG_CGROUPS
631f9c18 2617 REG("cgroup", S_IRUGO, proc_cgroup_operations),
28a6d671 2618#endif
631f9c18 2619 INF("oom_score", S_IRUGO, proc_oom_score),
fa0cbbf1 2620 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
a63d83f4 2621 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
28a6d671 2622#ifdef CONFIG_AUDITSYSCALL
631f9c18
AD
2623 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2624 REG("sessionid", S_IRUGO, proc_sessionid_operations),
28a6d671 2625#endif
f4f154fd 2626#ifdef CONFIG_FAULT_INJECTION
631f9c18 2627 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
f4f154fd 2628#endif
698ba7b5 2629#ifdef CONFIG_ELF_CORE
631f9c18 2630 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
3cb4a0bb 2631#endif
aba76fdb 2632#ifdef CONFIG_TASK_IO_ACCOUNTING
1d1221f3 2633 INF("io", S_IRUSR, proc_tgid_io_accounting),
aba76fdb 2634#endif
f133ecca
CM
2635#ifdef CONFIG_HARDWALL
2636 INF("hardwall", S_IRUGO, proc_pid_hardwall),
2637#endif
22d917d8
EB
2638#ifdef CONFIG_USER_NS
2639 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2640 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
f76d207a 2641 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
22d917d8 2642#endif
48f6a7a5
PE
2643#ifdef CONFIG_CHECKPOINT_RESTORE
2644 REG("timers", S_IRUGO, proc_timers_operations),
2645#endif
28a6d671 2646};
1da177e4 2647
f0c3b509 2648static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
1da177e4 2649{
f0c3b509
AV
2650 return proc_pident_readdir(file, ctx,
2651 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
1da177e4
LT
2652}
2653
00977a59 2654static const struct file_operations proc_tgid_base_operations = {
1da177e4 2655 .read = generic_read_dir,
f0c3b509 2656 .iterate = proc_tgid_base_readdir,
6038f373 2657 .llseek = default_llseek,
1da177e4
LT
2658};
2659
00cd8dd3
AV
2660static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2661{
7bcd6b0e
EB
2662 return proc_pident_lookup(dir, dentry,
2663 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
1da177e4
LT
2664}
2665
c5ef1c42 2666static const struct inode_operations proc_tgid_base_inode_operations = {
28a6d671 2667 .lookup = proc_tgid_base_lookup,
99f89551 2668 .getattr = pid_getattr,
6d76fa58 2669 .setattr = proc_setattr,
0499680a 2670 .permission = proc_pid_permission,
1da177e4 2671};
1da177e4 2672
60347f67 2673static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
1da177e4 2674{
48e6484d 2675 struct dentry *dentry, *leader, *dir;
8578cea7 2676 char buf[PROC_NUMBUF];
48e6484d
EB
2677 struct qstr name;
2678
2679 name.name = buf;
60347f67 2680 name.len = snprintf(buf, sizeof(buf), "%d", pid);
4f522a24 2681 /* no ->d_hash() rejects on procfs */
60347f67 2682 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
48e6484d 2683 if (dentry) {
29f12ca3 2684 shrink_dcache_parent(dentry);
48e6484d
EB
2685 d_drop(dentry);
2686 dput(dentry);
2687 }
1da177e4 2688
48e6484d 2689 name.name = buf;
60347f67
PE
2690 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2691 leader = d_hash_and_lookup(mnt->mnt_root, &name);
48e6484d
EB
2692 if (!leader)
2693 goto out;
1da177e4 2694
48e6484d
EB
2695 name.name = "task";
2696 name.len = strlen(name.name);
2697 dir = d_hash_and_lookup(leader, &name);
2698 if (!dir)
2699 goto out_put_leader;
2700
2701 name.name = buf;
60347f67 2702 name.len = snprintf(buf, sizeof(buf), "%d", pid);
48e6484d
EB
2703 dentry = d_hash_and_lookup(dir, &name);
2704 if (dentry) {
2705 shrink_dcache_parent(dentry);
2706 d_drop(dentry);
2707 dput(dentry);
1da177e4 2708 }
48e6484d
EB
2709
2710 dput(dir);
2711out_put_leader:
2712 dput(leader);
2713out:
2714 return;
1da177e4
LT
2715}
2716
0895e91d
RD
2717/**
2718 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2719 * @task: task that should be flushed.
2720 *
2721 * When flushing dentries from proc, one needs to flush them from global
60347f67 2722 * proc (proc_mnt) and from all the namespaces' procs this task was seen
0895e91d
RD
2723 * in. This call is supposed to do all of this job.
2724 *
2725 * Looks in the dcache for
2726 * /proc/@pid
2727 * /proc/@tgid/task/@pid
2728 * if either directory is present flushes it and all of it'ts children
2729 * from the dcache.
2730 *
2731 * It is safe and reasonable to cache /proc entries for a task until
2732 * that task exits. After that they just clog up the dcache with
2733 * useless entries, possibly causing useful dcache entries to be
2734 * flushed instead. This routine is proved to flush those useless
2735 * dcache entries at process exit time.
2736 *
2737 * NOTE: This routine is just an optimization so it does not guarantee
2738 * that no dcache entries will exist at process exit time it
2739 * just makes it very unlikely that any will persist.
60347f67
PE
2740 */
2741
2742void proc_flush_task(struct task_struct *task)
2743{
9fcc2d15 2744 int i;
9b4d1cbe 2745 struct pid *pid, *tgid;
130f77ec
PE
2746 struct upid *upid;
2747
130f77ec 2748 pid = task_pid(task);
9b4d1cbe 2749 tgid = task_tgid(task);
130f77ec 2750
9fcc2d15 2751 for (i = 0; i <= pid->level; i++) {
130f77ec
PE
2752 upid = &pid->numbers[i];
2753 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
9b4d1cbe 2754 tgid->numbers[i].nr);
130f77ec 2755 }
60347f67
PE
2756}
2757
c52a47ac
AV
2758static int proc_pid_instantiate(struct inode *dir,
2759 struct dentry * dentry,
2760 struct task_struct *task, const void *ptr)
444ceed8 2761{
444ceed8
EB
2762 struct inode *inode;
2763
61a28784 2764 inode = proc_pid_make_inode(dir->i_sb, task);
444ceed8
EB
2765 if (!inode)
2766 goto out;
2767
2768 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2769 inode->i_op = &proc_tgid_base_inode_operations;
2770 inode->i_fop = &proc_tgid_base_operations;
2771 inode->i_flags|=S_IMMUTABLE;
aed54175 2772
bfe86848
MS
2773 set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
2774 ARRAY_SIZE(tgid_base_stuff)));
444ceed8 2775
fb045adb 2776 d_set_d_op(dentry, &pid_dentry_operations);
444ceed8
EB
2777
2778 d_add(dentry, inode);
2779 /* Close the race of the process dying before we return the dentry */
0b728e19 2780 if (pid_revalidate(dentry, 0))
c52a47ac 2781 return 0;
444ceed8 2782out:
c52a47ac 2783 return -ENOENT;
444ceed8
EB
2784}
2785
00cd8dd3 2786struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
1da177e4 2787{
335eb531 2788 int result = -ENOENT;
1da177e4 2789 struct task_struct *task;
1da177e4 2790 unsigned tgid;
b488893a 2791 struct pid_namespace *ns;
1da177e4 2792
dbcdb504 2793 tgid = name_to_int(&dentry->d_name);
1da177e4
LT
2794 if (tgid == ~0U)
2795 goto out;
2796
b488893a 2797 ns = dentry->d_sb->s_fs_info;
de758734 2798 rcu_read_lock();
b488893a 2799 task = find_task_by_pid_ns(tgid, ns);
1da177e4
LT
2800 if (task)
2801 get_task_struct(task);
de758734 2802 rcu_read_unlock();
1da177e4
LT
2803 if (!task)
2804 goto out;
2805
444ceed8 2806 result = proc_pid_instantiate(dir, dentry, task, NULL);
1da177e4 2807 put_task_struct(task);
1da177e4 2808out:
c52a47ac 2809 return ERR_PTR(result);
1da177e4
LT
2810}
2811
1da177e4 2812/*
0804ef4b 2813 * Find the first task with tgid >= tgid
0bc58a91 2814 *
1da177e4 2815 */
19fd4bb2
EB
2816struct tgid_iter {
2817 unsigned int tgid;
0804ef4b 2818 struct task_struct *task;
19fd4bb2
EB
2819};
2820static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2821{
0804ef4b 2822 struct pid *pid;
1da177e4 2823
19fd4bb2
EB
2824 if (iter.task)
2825 put_task_struct(iter.task);
454cc105 2826 rcu_read_lock();
0804ef4b 2827retry:
19fd4bb2
EB
2828 iter.task = NULL;
2829 pid = find_ge_pid(iter.tgid, ns);
0804ef4b 2830 if (pid) {
19fd4bb2
EB
2831 iter.tgid = pid_nr_ns(pid, ns);
2832 iter.task = pid_task(pid, PIDTYPE_PID);
0804ef4b
EB
2833 /* What we to know is if the pid we have find is the
2834 * pid of a thread_group_leader. Testing for task
2835 * being a thread_group_leader is the obvious thing
2836 * todo but there is a window when it fails, due to
2837 * the pid transfer logic in de_thread.
2838 *
2839 * So we perform the straight forward test of seeing
2840 * if the pid we have found is the pid of a thread
2841 * group leader, and don't worry if the task we have
2842 * found doesn't happen to be a thread group leader.
2843 * As we don't care in the case of readdir.
2844 */
19fd4bb2
EB
2845 if (!iter.task || !has_group_leader_pid(iter.task)) {
2846 iter.tgid += 1;
0804ef4b 2847 goto retry;
19fd4bb2
EB
2848 }
2849 get_task_struct(iter.task);
0bc58a91 2850 }
454cc105 2851 rcu_read_unlock();
19fd4bb2 2852 return iter;
1da177e4
LT
2853}
2854
021ada7d 2855#define TGID_OFFSET (FIRST_PROCESS_ENTRY + 1)
0804ef4b 2856
1da177e4 2857/* for the /proc/ directory itself, after non-process stuff has been done */
f0c3b509 2858int proc_pid_readdir(struct file *file, struct dir_context *ctx)
1da177e4 2859{
19fd4bb2 2860 struct tgid_iter iter;
db963164 2861 struct pid_namespace *ns = file->f_dentry->d_sb->s_fs_info;
f0c3b509 2862 loff_t pos = ctx->pos;
1da177e4 2863
021ada7d 2864 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
f0c3b509 2865 return 0;
1da177e4 2866
021ada7d 2867 if (pos == TGID_OFFSET - 1) {
db963164
AV
2868 struct inode *inode = ns->proc_self->d_inode;
2869 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
f0c3b509 2870 return 0;
021ada7d
AV
2871 iter.tgid = 0;
2872 } else {
2873 iter.tgid = pos - TGID_OFFSET;
2874 }
19fd4bb2 2875 iter.task = NULL;
19fd4bb2
EB
2876 for (iter = next_tgid(ns, iter);
2877 iter.task;
2878 iter.tgid += 1, iter = next_tgid(ns, iter)) {
f0c3b509
AV
2879 char name[PROC_NUMBUF];
2880 int len;
2881 if (!has_pid_permissions(ns, iter.task, 2))
2882 continue;
0499680a 2883
f0c3b509
AV
2884 len = snprintf(name, sizeof(name), "%d", iter.tgid);
2885 ctx->pos = iter.tgid + TGID_OFFSET;
2886 if (!proc_fill_cache(file, ctx, name, len,
2887 proc_pid_instantiate, iter.task, NULL)) {
19fd4bb2 2888 put_task_struct(iter.task);
f0c3b509 2889 return 0;
1da177e4 2890 }
0bc58a91 2891 }
f0c3b509 2892 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
0bc58a91
EB
2893 return 0;
2894}
1da177e4 2895
28a6d671
EB
2896/*
2897 * Tasks
2898 */
c5141e6d 2899static const struct pid_entry tid_base_stuff[] = {
631f9c18 2900 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
3835541d 2901 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
6b4e306a 2902 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
631f9c18 2903 REG("environ", S_IRUSR, proc_environ_operations),
f9ea536e 2904 ONE("auxv", S_IRUSR, proc_pid_auxv),
631f9c18 2905 ONE("status", S_IRUGO, proc_pid_status),
35a35046 2906 ONE("personality", S_IRUSR, proc_pid_personality),
1c963eb1 2907 ONE("limits", S_IRUGO, proc_pid_limits),
43ae34cb 2908#ifdef CONFIG_SCHED_DEBUG
631f9c18 2909 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
ebcb6734 2910#endif
4614a696 2911 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
ebcb6734 2912#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
09d93bd6 2913 ONE("syscall", S_IRUSR, proc_pid_syscall),
43ae34cb 2914#endif
2ca66ff7 2915 ONE("cmdline", S_IRUGO, proc_pid_cmdline),
631f9c18
AD
2916 ONE("stat", S_IRUGO, proc_tid_stat),
2917 ONE("statm", S_IRUGO, proc_pid_statm),
b7643757 2918 REG("maps", S_IRUGO, proc_tid_maps_operations),
81841161
CG
2919#ifdef CONFIG_CHECKPOINT_RESTORE
2920 REG("children", S_IRUGO, proc_tid_children_operations),
2921#endif
28a6d671 2922#ifdef CONFIG_NUMA
b7643757 2923 REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
28a6d671 2924#endif
631f9c18
AD
2925 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2926 LNK("cwd", proc_cwd_link),
2927 LNK("root", proc_root_link),
2928 LNK("exe", proc_exe_link),
2929 REG("mounts", S_IRUGO, proc_mounts_operations),
2930 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
1e883281 2931#ifdef CONFIG_PROC_PAGE_MONITOR
631f9c18 2932 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
b7643757 2933 REG("smaps", S_IRUGO, proc_tid_smaps_operations),
32ed74a4 2934 REG("pagemap", S_IRUSR, proc_pagemap_operations),
28a6d671
EB
2935#endif
2936#ifdef CONFIG_SECURITY
631f9c18 2937 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
28a6d671
EB
2938#endif
2939#ifdef CONFIG_KALLSYMS
edfcd606 2940 ONE("wchan", S_IRUGO, proc_pid_wchan),
28a6d671 2941#endif
2ec220e2 2942#ifdef CONFIG_STACKTRACE
35a35046 2943 ONE("stack", S_IRUSR, proc_pid_stack),
28a6d671
EB
2944#endif
2945#ifdef CONFIG_SCHEDSTATS
f6e826ca 2946 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
28a6d671 2947#endif
9745512c 2948#ifdef CONFIG_LATENCYTOP
631f9c18 2949 REG("latency", S_IRUGO, proc_lstats_operations),
9745512c 2950#endif
8793d854 2951#ifdef CONFIG_PROC_PID_CPUSET
631f9c18 2952 REG("cpuset", S_IRUGO, proc_cpuset_operations),
a424316c
PM
2953#endif
2954#ifdef CONFIG_CGROUPS
631f9c18 2955 REG("cgroup", S_IRUGO, proc_cgroup_operations),
28a6d671 2956#endif
631f9c18 2957 INF("oom_score", S_IRUGO, proc_oom_score),
fa0cbbf1 2958 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
a63d83f4 2959 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
28a6d671 2960#ifdef CONFIG_AUDITSYSCALL
631f9c18 2961 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
26ec3c64 2962 REG("sessionid", S_IRUGO, proc_sessionid_operations),
28a6d671 2963#endif
f4f154fd 2964#ifdef CONFIG_FAULT_INJECTION
631f9c18 2965 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
f4f154fd 2966#endif
297c5d92 2967#ifdef CONFIG_TASK_IO_ACCOUNTING
1d1221f3 2968 INF("io", S_IRUSR, proc_tid_io_accounting),
297c5d92 2969#endif
f133ecca
CM
2970#ifdef CONFIG_HARDWALL
2971 INF("hardwall", S_IRUGO, proc_pid_hardwall),
2972#endif
22d917d8
EB
2973#ifdef CONFIG_USER_NS
2974 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2975 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
f76d207a 2976 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
22d917d8 2977#endif
28a6d671
EB
2978};
2979
f0c3b509 2980static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
28a6d671 2981{
f0c3b509
AV
2982 return proc_pident_readdir(file, ctx,
2983 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
28a6d671
EB
2984}
2985
00cd8dd3
AV
2986static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2987{
7bcd6b0e
EB
2988 return proc_pident_lookup(dir, dentry,
2989 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
28a6d671
EB
2990}
2991
00977a59 2992static const struct file_operations proc_tid_base_operations = {
28a6d671 2993 .read = generic_read_dir,
f0c3b509 2994 .iterate = proc_tid_base_readdir,
6038f373 2995 .llseek = default_llseek,
28a6d671
EB
2996};
2997
c5ef1c42 2998static const struct inode_operations proc_tid_base_inode_operations = {
28a6d671
EB
2999 .lookup = proc_tid_base_lookup,
3000 .getattr = pid_getattr,
3001 .setattr = proc_setattr,
3002};
3003
c52a47ac 3004static int proc_task_instantiate(struct inode *dir,
c5141e6d 3005 struct dentry *dentry, struct task_struct *task, const void *ptr)
444ceed8 3006{
444ceed8 3007 struct inode *inode;
61a28784 3008 inode = proc_pid_make_inode(dir->i_sb, task);
444ceed8
EB
3009
3010 if (!inode)
3011 goto out;
3012 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3013 inode->i_op = &proc_tid_base_inode_operations;
3014 inode->i_fop = &proc_tid_base_operations;
3015 inode->i_flags|=S_IMMUTABLE;
aed54175 3016
bfe86848
MS
3017 set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3018 ARRAY_SIZE(tid_base_stuff)));
444ceed8 3019
fb045adb 3020 d_set_d_op(dentry, &pid_dentry_operations);
444ceed8
EB
3021
3022 d_add(dentry, inode);
3023 /* Close the race of the process dying before we return the dentry */
0b728e19 3024 if (pid_revalidate(dentry, 0))
c52a47ac 3025 return 0;
444ceed8 3026out:
c52a47ac 3027 return -ENOENT;
444ceed8
EB
3028}
3029
00cd8dd3 3030static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
28a6d671 3031{
c52a47ac 3032 int result = -ENOENT;
28a6d671
EB
3033 struct task_struct *task;
3034 struct task_struct *leader = get_proc_task(dir);
28a6d671 3035 unsigned tid;
b488893a 3036 struct pid_namespace *ns;
28a6d671
EB
3037
3038 if (!leader)
3039 goto out_no_task;
3040
dbcdb504 3041 tid = name_to_int(&dentry->d_name);
28a6d671
EB
3042 if (tid == ~0U)
3043 goto out;
3044
b488893a 3045 ns = dentry->d_sb->s_fs_info;
28a6d671 3046 rcu_read_lock();
b488893a 3047 task = find_task_by_pid_ns(tid, ns);
28a6d671
EB
3048 if (task)
3049 get_task_struct(task);
3050 rcu_read_unlock();
3051 if (!task)
3052 goto out;
bac0abd6 3053 if (!same_thread_group(leader, task))
28a6d671
EB
3054 goto out_drop_task;
3055
444ceed8 3056 result = proc_task_instantiate(dir, dentry, task, NULL);
28a6d671
EB
3057out_drop_task:
3058 put_task_struct(task);
3059out:
3060 put_task_struct(leader);
3061out_no_task:
c52a47ac 3062 return ERR_PTR(result);
28a6d671
EB
3063}
3064
0bc58a91
EB
3065/*
3066 * Find the first tid of a thread group to return to user space.
3067 *
3068 * Usually this is just the thread group leader, but if the users
3069 * buffer was too small or there was a seek into the middle of the
3070 * directory we have more work todo.
3071 *
3072 * In the case of a short read we start with find_task_by_pid.
3073 *
3074 * In the case of a seek we start with the leader and walk nr
3075 * threads past it.
3076 */
9f6e963f
ON
3077static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
3078 struct pid_namespace *ns)
0bc58a91 3079{
d855a4b7 3080 struct task_struct *pos, *task;
9f6e963f
ON
3081 unsigned long nr = f_pos;
3082
3083 if (nr != f_pos) /* 32bit overflow? */
3084 return NULL;
1da177e4 3085
cc288738 3086 rcu_read_lock();
d855a4b7
ON
3087 task = pid_task(pid, PIDTYPE_PID);
3088 if (!task)
3089 goto fail;
3090
3091 /* Attempt to start with the tid of a thread */
9f6e963f 3092 if (tid && nr) {
b488893a 3093 pos = find_task_by_pid_ns(tid, ns);
d855a4b7 3094 if (pos && same_thread_group(pos, task))
a872ff0c 3095 goto found;
0bc58a91 3096 }
1da177e4 3097
0bc58a91 3098 /* If nr exceeds the number of threads there is nothing todo */
9f6e963f 3099 if (nr >= get_nr_threads(task))
c986c14a 3100 goto fail;
1da177e4 3101
a872ff0c
ON
3102 /* If we haven't found our starting place yet start
3103 * with the leader and walk nr threads forward.
0bc58a91 3104 */
d855a4b7 3105 pos = task = task->group_leader;
c986c14a 3106 do {
9f6e963f 3107 if (!nr--)
c986c14a 3108 goto found;
d855a4b7 3109 } while_each_thread(task, pos);
c986c14a
ON
3110fail:
3111 pos = NULL;
3112 goto out;
a872ff0c
ON
3113found:
3114 get_task_struct(pos);
3115out:
cc288738 3116 rcu_read_unlock();
0bc58a91
EB
3117 return pos;
3118}
3119
3120/*
3121 * Find the next thread in the thread list.
3122 * Return NULL if there is an error or no next thread.
3123 *
3124 * The reference to the input task_struct is released.
3125 */
3126static struct task_struct *next_tid(struct task_struct *start)
3127{
c1df7fb8 3128 struct task_struct *pos = NULL;
cc288738 3129 rcu_read_lock();
c1df7fb8 3130 if (pid_alive(start)) {
0bc58a91 3131 pos = next_thread(start);
c1df7fb8
ON
3132 if (thread_group_leader(pos))
3133 pos = NULL;
3134 else
3135 get_task_struct(pos);
3136 }
cc288738 3137 rcu_read_unlock();
0bc58a91
EB
3138 put_task_struct(start);
3139 return pos;
1da177e4
LT
3140}
3141
3142/* for the /proc/TGID/task/ directories */
f0c3b509 3143static int proc_task_readdir(struct file *file, struct dir_context *ctx)
1da177e4 3144{
d855a4b7
ON
3145 struct inode *inode = file_inode(file);
3146 struct task_struct *task;
b488893a 3147 struct pid_namespace *ns;
f0c3b509 3148 int tid;
1da177e4 3149
d855a4b7 3150 if (proc_inode_is_dead(inode))
f0c3b509 3151 return -ENOENT;
1da177e4 3152
f0c3b509 3153 if (!dir_emit_dots(file, ctx))
d855a4b7 3154 return 0;
1da177e4 3155
0bc58a91
EB
3156 /* f_version caches the tgid value that the last readdir call couldn't
3157 * return. lseek aka telldir automagically resets f_version to 0.
3158 */
f0c3b509
AV
3159 ns = file->f_dentry->d_sb->s_fs_info;
3160 tid = (int)file->f_version;
3161 file->f_version = 0;
d855a4b7 3162 for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
0bc58a91 3163 task;
f0c3b509
AV
3164 task = next_tid(task), ctx->pos++) {
3165 char name[PROC_NUMBUF];
3166 int len;
b488893a 3167 tid = task_pid_nr_ns(task, ns);
f0c3b509
AV
3168 len = snprintf(name, sizeof(name), "%d", tid);
3169 if (!proc_fill_cache(file, ctx, name, len,
3170 proc_task_instantiate, task, NULL)) {
0bc58a91
EB
3171 /* returning this tgid failed, save it as the first
3172 * pid for the next readir call */
f0c3b509 3173 file->f_version = (u64)tid;
0bc58a91 3174 put_task_struct(task);
1da177e4 3175 break;
0bc58a91 3176 }
1da177e4 3177 }
d855a4b7 3178
f0c3b509 3179 return 0;
1da177e4 3180}
6e66b52b
EB
3181
3182static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3183{
3184 struct inode *inode = dentry->d_inode;
99f89551 3185 struct task_struct *p = get_proc_task(inode);
6e66b52b
EB
3186 generic_fillattr(inode, stat);
3187
99f89551 3188 if (p) {
99f89551 3189 stat->nlink += get_nr_threads(p);
99f89551 3190 put_task_struct(p);
6e66b52b
EB
3191 }
3192
3193 return 0;
3194}
28a6d671 3195
c5ef1c42 3196static const struct inode_operations proc_task_inode_operations = {
28a6d671
EB
3197 .lookup = proc_task_lookup,
3198 .getattr = proc_task_getattr,
3199 .setattr = proc_setattr,
0499680a 3200 .permission = proc_pid_permission,
28a6d671
EB
3201};
3202
00977a59 3203static const struct file_operations proc_task_operations = {
28a6d671 3204 .read = generic_read_dir,
f0c3b509 3205 .iterate = proc_task_readdir,
6038f373 3206 .llseek = default_llseek,
28a6d671 3207};