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