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