GNU Linux-libre 4.9.337-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,
1680                                   struct task_struct *task, umode_t mode)
1681 {
1682         struct inode * inode;
1683         struct proc_inode *ei;
1684         const struct cred *cred;
1685
1686         /* We need a new inode */
1687
1688         inode = new_inode(sb);
1689         if (!inode)
1690                 goto out;
1691
1692         /* Common stuff */
1693         ei = PROC_I(inode);
1694         inode->i_mode = mode;
1695         inode->i_ino = get_next_ino();
1696         inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
1697         inode->i_op = &proc_def_inode_operations;
1698
1699         /*
1700          * grab the reference to task.
1701          */
1702         ei->pid = get_task_pid(task, PIDTYPE_PID);
1703         if (!ei->pid)
1704                 goto out_unlock;
1705
1706         if (task_dumpable(task)) {
1707                 rcu_read_lock();
1708                 cred = __task_cred(task);
1709                 inode->i_uid = cred->euid;
1710                 inode->i_gid = cred->egid;
1711                 rcu_read_unlock();
1712         }
1713         security_task_to_inode(task, inode);
1714
1715 out:
1716         return inode;
1717
1718 out_unlock:
1719         iput(inode);
1720         return NULL;
1721 }
1722
1723 int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1724 {
1725         struct inode *inode = d_inode(dentry);
1726         struct task_struct *task;
1727         const struct cred *cred;
1728         struct pid_namespace *pid = dentry->d_sb->s_fs_info;
1729
1730         generic_fillattr(inode, stat);
1731
1732         rcu_read_lock();
1733         stat->uid = GLOBAL_ROOT_UID;
1734         stat->gid = GLOBAL_ROOT_GID;
1735         task = pid_task(proc_pid(inode), PIDTYPE_PID);
1736         if (task) {
1737                 if (!has_pid_permissions(pid, task, 2)) {
1738                         rcu_read_unlock();
1739                         /*
1740                          * This doesn't prevent learning whether PID exists,
1741                          * it only makes getattr() consistent with readdir().
1742                          */
1743                         return -ENOENT;
1744                 }
1745                 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1746                     task_dumpable(task)) {
1747                         cred = __task_cred(task);
1748                         stat->uid = cred->euid;
1749                         stat->gid = cred->egid;
1750                 }
1751         }
1752         rcu_read_unlock();
1753         return 0;
1754 }
1755
1756 /* dentry stuff */
1757
1758 /*
1759  *      Exceptional case: normally we are not allowed to unhash a busy
1760  * directory. In this case, however, we can do it - no aliasing problems
1761  * due to the way we treat inodes.
1762  *
1763  * Rewrite the inode's ownerships here because the owning task may have
1764  * performed a setuid(), etc.
1765  *
1766  * Before the /proc/pid/status file was created the only way to read
1767  * the effective uid of a /process was to stat /proc/pid.  Reading
1768  * /proc/pid/status is slow enough that procps and other packages
1769  * kept stating /proc/pid.  To keep the rules in /proc simple I have
1770  * made this apply to all per process world readable and executable
1771  * directories.
1772  */
1773 int pid_revalidate(struct dentry *dentry, unsigned int flags)
1774 {
1775         struct inode *inode;
1776         struct task_struct *task;
1777         const struct cred *cred;
1778
1779         if (flags & LOOKUP_RCU)
1780                 return -ECHILD;
1781
1782         inode = d_inode(dentry);
1783         task = get_proc_task(inode);
1784
1785         if (task) {
1786                 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1787                     task_dumpable(task)) {
1788                         rcu_read_lock();
1789                         cred = __task_cred(task);
1790                         inode->i_uid = cred->euid;
1791                         inode->i_gid = cred->egid;
1792                         rcu_read_unlock();
1793                 } else {
1794                         inode->i_uid = GLOBAL_ROOT_UID;
1795                         inode->i_gid = GLOBAL_ROOT_GID;
1796                 }
1797                 inode->i_mode &= ~(S_ISUID | S_ISGID);
1798                 security_task_to_inode(task, inode);
1799                 put_task_struct(task);
1800                 return 1;
1801         }
1802         return 0;
1803 }
1804
1805 static inline bool proc_inode_is_dead(struct inode *inode)
1806 {
1807         return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1808 }
1809
1810 int pid_delete_dentry(const struct dentry *dentry)
1811 {
1812         /* Is the task we represent dead?
1813          * If so, then don't put the dentry on the lru list,
1814          * kill it immediately.
1815          */
1816         return proc_inode_is_dead(d_inode(dentry));
1817 }
1818
1819 const struct dentry_operations pid_dentry_operations =
1820 {
1821         .d_revalidate   = pid_revalidate,
1822         .d_delete       = pid_delete_dentry,
1823 };
1824
1825 /* Lookups */
1826
1827 /*
1828  * Fill a directory entry.
1829  *
1830  * If possible create the dcache entry and derive our inode number and
1831  * file type from dcache entry.
1832  *
1833  * Since all of the proc inode numbers are dynamically generated, the inode
1834  * numbers do not exist until the inode is cache.  This means creating the
1835  * the dcache entry in readdir is necessary to keep the inode numbers
1836  * reported by readdir in sync with the inode numbers reported
1837  * by stat.
1838  */
1839 bool proc_fill_cache(struct file *file, struct dir_context *ctx,
1840         const char *name, int len,
1841         instantiate_t instantiate, struct task_struct *task, const void *ptr)
1842 {
1843         struct dentry *child, *dir = file->f_path.dentry;
1844         struct qstr qname = QSTR_INIT(name, len);
1845         struct inode *inode;
1846         unsigned type;
1847         ino_t ino;
1848
1849         child = d_hash_and_lookup(dir, &qname);
1850         if (!child) {
1851                 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1852                 child = d_alloc_parallel(dir, &qname, &wq);
1853                 if (IS_ERR(child))
1854                         goto end_instantiate;
1855                 if (d_in_lookup(child)) {
1856                         int err = instantiate(d_inode(dir), child, task, ptr);
1857                         d_lookup_done(child);
1858                         if (err < 0) {
1859                                 dput(child);
1860                                 goto end_instantiate;
1861                         }
1862                 }
1863         }
1864         inode = d_inode(child);
1865         ino = inode->i_ino;
1866         type = inode->i_mode >> 12;
1867         dput(child);
1868         return dir_emit(ctx, name, len, ino, type);
1869
1870 end_instantiate:
1871         return dir_emit(ctx, name, len, 1, DT_UNKNOWN);
1872 }
1873
1874 /*
1875  * dname_to_vma_addr - maps a dentry name into two unsigned longs
1876  * which represent vma start and end addresses.
1877  */
1878 static int dname_to_vma_addr(struct dentry *dentry,
1879                              unsigned long *start, unsigned long *end)
1880 {
1881         const char *str = dentry->d_name.name;
1882         unsigned long long sval, eval;
1883         unsigned int len;
1884
1885         len = _parse_integer(str, 16, &sval);
1886         if (len & KSTRTOX_OVERFLOW)
1887                 return -EINVAL;
1888         if (sval != (unsigned long)sval)
1889                 return -EINVAL;
1890         str += len;
1891
1892         if (*str != '-')
1893                 return -EINVAL;
1894         str++;
1895
1896         len = _parse_integer(str, 16, &eval);
1897         if (len & KSTRTOX_OVERFLOW)
1898                 return -EINVAL;
1899         if (eval != (unsigned long)eval)
1900                 return -EINVAL;
1901         str += len;
1902
1903         if (*str != '\0')
1904                 return -EINVAL;
1905
1906         *start = sval;
1907         *end = eval;
1908
1909         return 0;
1910 }
1911
1912 static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
1913 {
1914         unsigned long vm_start, vm_end;
1915         bool exact_vma_exists = false;
1916         struct mm_struct *mm = NULL;
1917         struct task_struct *task;
1918         const struct cred *cred;
1919         struct inode *inode;
1920         int status = 0;
1921
1922         if (flags & LOOKUP_RCU)
1923                 return -ECHILD;
1924
1925         inode = d_inode(dentry);
1926         task = get_proc_task(inode);
1927         if (!task)
1928                 goto out_notask;
1929
1930         mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
1931         if (IS_ERR_OR_NULL(mm))
1932                 goto out;
1933
1934         if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1935                 down_read(&mm->mmap_sem);
1936                 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1937                 up_read(&mm->mmap_sem);
1938         }
1939
1940         mmput(mm);
1941
1942         if (exact_vma_exists) {
1943                 if (task_dumpable(task)) {
1944                         rcu_read_lock();
1945                         cred = __task_cred(task);
1946                         inode->i_uid = cred->euid;
1947                         inode->i_gid = cred->egid;
1948                         rcu_read_unlock();
1949                 } else {
1950                         inode->i_uid = GLOBAL_ROOT_UID;
1951                         inode->i_gid = GLOBAL_ROOT_GID;
1952                 }
1953                 security_task_to_inode(task, inode);
1954                 status = 1;
1955         }
1956
1957 out:
1958         put_task_struct(task);
1959
1960 out_notask:
1961         return status;
1962 }
1963
1964 static const struct dentry_operations tid_map_files_dentry_operations = {
1965         .d_revalidate   = map_files_d_revalidate,
1966         .d_delete       = pid_delete_dentry,
1967 };
1968
1969 static int map_files_get_link(struct dentry *dentry, struct path *path)
1970 {
1971         unsigned long vm_start, vm_end;
1972         struct vm_area_struct *vma;
1973         struct task_struct *task;
1974         struct mm_struct *mm;
1975         int rc;
1976
1977         rc = -ENOENT;
1978         task = get_proc_task(d_inode(dentry));
1979         if (!task)
1980                 goto out;
1981
1982         mm = get_task_mm(task);
1983         put_task_struct(task);
1984         if (!mm)
1985                 goto out;
1986
1987         rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
1988         if (rc)
1989                 goto out_mmput;
1990
1991         rc = -ENOENT;
1992         down_read(&mm->mmap_sem);
1993         vma = find_exact_vma(mm, vm_start, vm_end);
1994         if (vma && vma->vm_file) {
1995                 *path = vma->vm_file->f_path;
1996                 path_get(path);
1997                 rc = 0;
1998         }
1999         up_read(&mm->mmap_sem);
2000
2001 out_mmput:
2002         mmput(mm);
2003 out:
2004         return rc;
2005 }
2006
2007 struct map_files_info {
2008         fmode_t         mode;
2009         unsigned long   len;
2010         unsigned char   name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
2011 };
2012
2013 /*
2014  * Only allow CAP_SYS_ADMIN to follow the links, due to concerns about how the
2015  * symlinks may be used to bypass permissions on ancestor directories in the
2016  * path to the file in question.
2017  */
2018 static const char *
2019 proc_map_files_get_link(struct dentry *dentry,
2020                         struct inode *inode,
2021                         struct delayed_call *done)
2022 {
2023         if (!capable(CAP_SYS_ADMIN))
2024                 return ERR_PTR(-EPERM);
2025
2026         return proc_pid_get_link(dentry, inode, done);
2027 }
2028
2029 /*
2030  * Identical to proc_pid_link_inode_operations except for get_link()
2031  */
2032 static const struct inode_operations proc_map_files_link_inode_operations = {
2033         .readlink       = proc_pid_readlink,
2034         .get_link       = proc_map_files_get_link,
2035         .setattr        = proc_setattr,
2036 };
2037
2038 static int
2039 proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
2040                            struct task_struct *task, const void *ptr)
2041 {
2042         fmode_t mode = (fmode_t)(unsigned long)ptr;
2043         struct proc_inode *ei;
2044         struct inode *inode;
2045
2046         inode = proc_pid_make_inode(dir->i_sb, task, S_IFLNK |
2047                                     ((mode & FMODE_READ ) ? S_IRUSR : 0) |
2048                                     ((mode & FMODE_WRITE) ? S_IWUSR : 0));
2049         if (!inode)
2050                 return -ENOENT;
2051
2052         ei = PROC_I(inode);
2053         ei->op.proc_get_link = map_files_get_link;
2054
2055         inode->i_op = &proc_map_files_link_inode_operations;
2056         inode->i_size = 64;
2057
2058         d_set_d_op(dentry, &tid_map_files_dentry_operations);
2059         d_add(dentry, inode);
2060
2061         return 0;
2062 }
2063
2064 static struct dentry *proc_map_files_lookup(struct inode *dir,
2065                 struct dentry *dentry, unsigned int flags)
2066 {
2067         unsigned long vm_start, vm_end;
2068         struct vm_area_struct *vma;
2069         struct task_struct *task;
2070         int result;
2071         struct mm_struct *mm;
2072
2073         result = -ENOENT;
2074         task = get_proc_task(dir);
2075         if (!task)
2076                 goto out;
2077
2078         result = -EACCES;
2079         if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
2080                 goto out_put_task;
2081
2082         result = -ENOENT;
2083         if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
2084                 goto out_put_task;
2085
2086         mm = get_task_mm(task);
2087         if (!mm)
2088                 goto out_put_task;
2089
2090         down_read(&mm->mmap_sem);
2091         vma = find_exact_vma(mm, vm_start, vm_end);
2092         if (!vma)
2093                 goto out_no_vma;
2094
2095         if (vma->vm_file)
2096                 result = proc_map_files_instantiate(dir, dentry, task,
2097                                 (void *)(unsigned long)vma->vm_file->f_mode);
2098
2099 out_no_vma:
2100         up_read(&mm->mmap_sem);
2101         mmput(mm);
2102 out_put_task:
2103         put_task_struct(task);
2104 out:
2105         return ERR_PTR(result);
2106 }
2107
2108 static const struct inode_operations proc_map_files_inode_operations = {
2109         .lookup         = proc_map_files_lookup,
2110         .permission     = proc_fd_permission,
2111         .setattr        = proc_setattr,
2112 };
2113
2114 static int
2115 proc_map_files_readdir(struct file *file, struct dir_context *ctx)
2116 {
2117         struct vm_area_struct *vma;
2118         struct task_struct *task;
2119         struct mm_struct *mm;
2120         unsigned long nr_files, pos, i;
2121         struct flex_array *fa = NULL;
2122         struct map_files_info info;
2123         struct map_files_info *p;
2124         int ret;
2125
2126         ret = -ENOENT;
2127         task = get_proc_task(file_inode(file));
2128         if (!task)
2129                 goto out;
2130
2131         ret = -EACCES;
2132         if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
2133                 goto out_put_task;
2134
2135         ret = 0;
2136         if (!dir_emit_dots(file, ctx))
2137                 goto out_put_task;
2138
2139         mm = get_task_mm(task);
2140         if (!mm)
2141                 goto out_put_task;
2142         down_read(&mm->mmap_sem);
2143
2144         nr_files = 0;
2145
2146         /*
2147          * We need two passes here:
2148          *
2149          *  1) Collect vmas of mapped files with mmap_sem taken
2150          *  2) Release mmap_sem and instantiate entries
2151          *
2152          * otherwise we get lockdep complained, since filldir()
2153          * routine might require mmap_sem taken in might_fault().
2154          */
2155
2156         for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
2157                 if (vma->vm_file && ++pos > ctx->pos)
2158                         nr_files++;
2159         }
2160
2161         if (nr_files) {
2162                 fa = flex_array_alloc(sizeof(info), nr_files,
2163                                         GFP_KERNEL);
2164                 if (!fa || flex_array_prealloc(fa, 0, nr_files,
2165                                                 GFP_KERNEL)) {
2166                         ret = -ENOMEM;
2167                         if (fa)
2168                                 flex_array_free(fa);
2169                         up_read(&mm->mmap_sem);
2170                         mmput(mm);
2171                         goto out_put_task;
2172                 }
2173                 for (i = 0, vma = mm->mmap, pos = 2; vma;
2174                                 vma = vma->vm_next) {
2175                         if (!vma->vm_file)
2176                                 continue;
2177                         if (++pos <= ctx->pos)
2178                                 continue;
2179
2180                         info.mode = vma->vm_file->f_mode;
2181                         info.len = snprintf(info.name,
2182                                         sizeof(info.name), "%lx-%lx",
2183                                         vma->vm_start, vma->vm_end);
2184                         if (flex_array_put(fa, i++, &info, GFP_KERNEL))
2185                                 BUG();
2186                 }
2187         }
2188         up_read(&mm->mmap_sem);
2189
2190         for (i = 0; i < nr_files; i++) {
2191                 p = flex_array_get(fa, i);
2192                 if (!proc_fill_cache(file, ctx,
2193                                       p->name, p->len,
2194                                       proc_map_files_instantiate,
2195                                       task,
2196                                       (void *)(unsigned long)p->mode))
2197                         break;
2198                 ctx->pos++;
2199         }
2200         if (fa)
2201                 flex_array_free(fa);
2202         mmput(mm);
2203
2204 out_put_task:
2205         put_task_struct(task);
2206 out:
2207         return ret;
2208 }
2209
2210 static const struct file_operations proc_map_files_operations = {
2211         .read           = generic_read_dir,
2212         .iterate_shared = proc_map_files_readdir,
2213         .llseek         = generic_file_llseek,
2214 };
2215
2216 #ifdef CONFIG_CHECKPOINT_RESTORE
2217 struct timers_private {
2218         struct pid *pid;
2219         struct task_struct *task;
2220         struct sighand_struct *sighand;
2221         struct pid_namespace *ns;
2222         unsigned long flags;
2223 };
2224
2225 static void *timers_start(struct seq_file *m, loff_t *pos)
2226 {
2227         struct timers_private *tp = m->private;
2228
2229         tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2230         if (!tp->task)
2231                 return ERR_PTR(-ESRCH);
2232
2233         tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2234         if (!tp->sighand)
2235                 return ERR_PTR(-ESRCH);
2236
2237         return seq_list_start(&tp->task->signal->posix_timers, *pos);
2238 }
2239
2240 static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2241 {
2242         struct timers_private *tp = m->private;
2243         return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2244 }
2245
2246 static void timers_stop(struct seq_file *m, void *v)
2247 {
2248         struct timers_private *tp = m->private;
2249
2250         if (tp->sighand) {
2251                 unlock_task_sighand(tp->task, &tp->flags);
2252                 tp->sighand = NULL;
2253         }
2254
2255         if (tp->task) {
2256                 put_task_struct(tp->task);
2257                 tp->task = NULL;
2258         }
2259 }
2260
2261 static int show_timer(struct seq_file *m, void *v)
2262 {
2263         struct k_itimer *timer;
2264         struct timers_private *tp = m->private;
2265         int notify;
2266         static const char * const nstr[] = {
2267                 [SIGEV_SIGNAL] = "signal",
2268                 [SIGEV_NONE] = "none",
2269                 [SIGEV_THREAD] = "thread",
2270         };
2271
2272         timer = list_entry((struct list_head *)v, struct k_itimer, list);
2273         notify = timer->it_sigev_notify;
2274
2275         seq_printf(m, "ID: %d\n", timer->it_id);
2276         seq_printf(m, "signal: %d/%p\n",
2277                    timer->sigq->info.si_signo,
2278                    timer->sigq->info.si_value.sival_ptr);
2279         seq_printf(m, "notify: %s/%s.%d\n",
2280                    nstr[notify & ~SIGEV_THREAD_ID],
2281                    (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2282                    pid_nr_ns(timer->it_pid, tp->ns));
2283         seq_printf(m, "ClockID: %d\n", timer->it_clock);
2284
2285         return 0;
2286 }
2287
2288 static const struct seq_operations proc_timers_seq_ops = {
2289         .start  = timers_start,
2290         .next   = timers_next,
2291         .stop   = timers_stop,
2292         .show   = show_timer,
2293 };
2294
2295 static int proc_timers_open(struct inode *inode, struct file *file)
2296 {
2297         struct timers_private *tp;
2298
2299         tp = __seq_open_private(file, &proc_timers_seq_ops,
2300                         sizeof(struct timers_private));
2301         if (!tp)
2302                 return -ENOMEM;
2303
2304         tp->pid = proc_pid(inode);
2305         tp->ns = inode->i_sb->s_fs_info;
2306         return 0;
2307 }
2308
2309 static const struct file_operations proc_timers_operations = {
2310         .open           = proc_timers_open,
2311         .read           = seq_read,
2312         .llseek         = seq_lseek,
2313         .release        = seq_release_private,
2314 };
2315 #endif
2316
2317 static ssize_t timerslack_ns_write(struct file *file, const char __user *buf,
2318                                         size_t count, loff_t *offset)
2319 {
2320         struct inode *inode = file_inode(file);
2321         struct task_struct *p;
2322         u64 slack_ns;
2323         int err;
2324
2325         err = kstrtoull_from_user(buf, count, 10, &slack_ns);
2326         if (err < 0)
2327                 return err;
2328
2329         p = get_proc_task(inode);
2330         if (!p)
2331                 return -ESRCH;
2332
2333         if (p != current) {
2334                 if (!capable(CAP_SYS_NICE)) {
2335                         count = -EPERM;
2336                         goto out;
2337                 }
2338
2339                 err = security_task_setscheduler(p);
2340                 if (err) {
2341                         count = err;
2342                         goto out;
2343                 }
2344         }
2345
2346         task_lock(p);
2347         if (slack_ns == 0)
2348                 p->timer_slack_ns = p->default_timer_slack_ns;
2349         else
2350                 p->timer_slack_ns = slack_ns;
2351         task_unlock(p);
2352
2353 out:
2354         put_task_struct(p);
2355
2356         return count;
2357 }
2358
2359 static int timerslack_ns_show(struct seq_file *m, void *v)
2360 {
2361         struct inode *inode = m->private;
2362         struct task_struct *p;
2363         int err = 0;
2364
2365         p = get_proc_task(inode);
2366         if (!p)
2367                 return -ESRCH;
2368
2369         if (p != current) {
2370
2371                 if (!capable(CAP_SYS_NICE)) {
2372                         err = -EPERM;
2373                         goto out;
2374                 }
2375                 err = security_task_getscheduler(p);
2376                 if (err)
2377                         goto out;
2378         }
2379
2380         task_lock(p);
2381         seq_printf(m, "%llu\n", p->timer_slack_ns);
2382         task_unlock(p);
2383
2384 out:
2385         put_task_struct(p);
2386
2387         return err;
2388 }
2389
2390 static int timerslack_ns_open(struct inode *inode, struct file *filp)
2391 {
2392         return single_open(filp, timerslack_ns_show, inode);
2393 }
2394
2395 static const struct file_operations proc_pid_set_timerslack_ns_operations = {
2396         .open           = timerslack_ns_open,
2397         .read           = seq_read,
2398         .write          = timerslack_ns_write,
2399         .llseek         = seq_lseek,
2400         .release        = single_release,
2401 };
2402
2403 static int proc_pident_instantiate(struct inode *dir,
2404         struct dentry *dentry, struct task_struct *task, const void *ptr)
2405 {
2406         const struct pid_entry *p = ptr;
2407         struct inode *inode;
2408         struct proc_inode *ei;
2409
2410         inode = proc_pid_make_inode(dir->i_sb, task, p->mode);
2411         if (!inode)
2412                 goto out;
2413
2414         ei = PROC_I(inode);
2415         if (S_ISDIR(inode->i_mode))
2416                 set_nlink(inode, 2);    /* Use getattr to fix if necessary */
2417         if (p->iop)
2418                 inode->i_op = p->iop;
2419         if (p->fop)
2420                 inode->i_fop = p->fop;
2421         ei->op = p->op;
2422         d_set_d_op(dentry, &pid_dentry_operations);
2423         d_add(dentry, inode);
2424         /* Close the race of the process dying before we return the dentry */
2425         if (pid_revalidate(dentry, 0))
2426                 return 0;
2427 out:
2428         return -ENOENT;
2429 }
2430
2431 static struct dentry *proc_pident_lookup(struct inode *dir, 
2432                                          struct dentry *dentry,
2433                                          const struct pid_entry *ents,
2434                                          unsigned int nents)
2435 {
2436         int error;
2437         struct task_struct *task = get_proc_task(dir);
2438         const struct pid_entry *p, *last;
2439
2440         error = -ENOENT;
2441
2442         if (!task)
2443                 goto out_no_task;
2444
2445         /*
2446          * Yes, it does not scale. And it should not. Don't add
2447          * new entries into /proc/<tgid>/ without very good reasons.
2448          */
2449         last = &ents[nents - 1];
2450         for (p = ents; p <= last; p++) {
2451                 if (p->len != dentry->d_name.len)
2452                         continue;
2453                 if (!memcmp(dentry->d_name.name, p->name, p->len))
2454                         break;
2455         }
2456         if (p > last)
2457                 goto out;
2458
2459         error = proc_pident_instantiate(dir, dentry, task, p);
2460 out:
2461         put_task_struct(task);
2462 out_no_task:
2463         return ERR_PTR(error);
2464 }
2465
2466 static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
2467                 const struct pid_entry *ents, unsigned int nents)
2468 {
2469         struct task_struct *task = get_proc_task(file_inode(file));
2470         const struct pid_entry *p;
2471
2472         if (!task)
2473                 return -ENOENT;
2474
2475         if (!dir_emit_dots(file, ctx))
2476                 goto out;
2477
2478         if (ctx->pos >= nents + 2)
2479                 goto out;
2480
2481         for (p = ents + (ctx->pos - 2); p <= ents + nents - 1; p++) {
2482                 if (!proc_fill_cache(file, ctx, p->name, p->len,
2483                                 proc_pident_instantiate, task, p))
2484                         break;
2485                 ctx->pos++;
2486         }
2487 out:
2488         put_task_struct(task);
2489         return 0;
2490 }
2491
2492 #ifdef CONFIG_SECURITY
2493 static int proc_pid_attr_open(struct inode *inode, struct file *file)
2494 {
2495         file->private_data = NULL;
2496         __mem_open(inode, file, PTRACE_MODE_READ_FSCREDS);
2497         return 0;
2498 }
2499
2500 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2501                                   size_t count, loff_t *ppos)
2502 {
2503         struct inode * inode = file_inode(file);
2504         char *p = NULL;
2505         ssize_t length;
2506         struct task_struct *task = get_proc_task(inode);
2507
2508         if (!task)
2509                 return -ESRCH;
2510
2511         length = security_getprocattr(task,
2512                                       (char*)file->f_path.dentry->d_name.name,
2513                                       &p);
2514         put_task_struct(task);
2515         if (length > 0)
2516                 length = simple_read_from_buffer(buf, count, ppos, p, length);
2517         kfree(p);
2518         return length;
2519 }
2520
2521 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2522                                    size_t count, loff_t *ppos)
2523 {
2524         struct inode * inode = file_inode(file);
2525         void *page;
2526         ssize_t length;
2527         struct task_struct *task = get_proc_task(inode);
2528
2529         /* A task may only write when it was the opener. */
2530         if (file->private_data != current->mm)
2531                 return -EPERM;
2532
2533         length = -ESRCH;
2534         if (!task)
2535                 goto out_no_task;
2536         if (count > PAGE_SIZE)
2537                 count = PAGE_SIZE;
2538
2539         /* No partial writes. */
2540         length = -EINVAL;
2541         if (*ppos != 0)
2542                 goto out;
2543
2544         page = memdup_user(buf, count);
2545         if (IS_ERR(page)) {
2546                 length = PTR_ERR(page);
2547                 goto out;
2548         }
2549
2550         /* Guard against adverse ptrace interaction */
2551         length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
2552         if (length < 0)
2553                 goto out_free;
2554
2555         length = security_setprocattr(task,
2556                                       (char*)file->f_path.dentry->d_name.name,
2557                                       page, count);
2558         mutex_unlock(&task->signal->cred_guard_mutex);
2559 out_free:
2560         kfree(page);
2561 out:
2562         put_task_struct(task);
2563 out_no_task:
2564         return length;
2565 }
2566
2567 static const struct file_operations proc_pid_attr_operations = {
2568         .open           = proc_pid_attr_open,
2569         .read           = proc_pid_attr_read,
2570         .write          = proc_pid_attr_write,
2571         .llseek         = generic_file_llseek,
2572         .release        = mem_release,
2573 };
2574
2575 static const struct pid_entry attr_dir_stuff[] = {
2576         REG("current",    S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2577         REG("prev",       S_IRUGO,         proc_pid_attr_operations),
2578         REG("exec",       S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2579         REG("fscreate",   S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2580         REG("keycreate",  S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2581         REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2582 };
2583
2584 static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
2585 {
2586         return proc_pident_readdir(file, ctx, 
2587                                    attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2588 }
2589
2590 static const struct file_operations proc_attr_dir_operations = {
2591         .read           = generic_read_dir,
2592         .iterate_shared = proc_attr_dir_readdir,
2593         .llseek         = generic_file_llseek,
2594 };
2595
2596 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2597                                 struct dentry *dentry, unsigned int flags)
2598 {
2599         return proc_pident_lookup(dir, dentry,
2600                                   attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2601 }
2602
2603 static const struct inode_operations proc_attr_dir_inode_operations = {
2604         .lookup         = proc_attr_dir_lookup,
2605         .getattr        = pid_getattr,
2606         .setattr        = proc_setattr,
2607 };
2608
2609 #endif
2610
2611 #ifdef CONFIG_ELF_CORE
2612 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2613                                          size_t count, loff_t *ppos)
2614 {
2615         struct task_struct *task = get_proc_task(file_inode(file));
2616         struct mm_struct *mm;
2617         char buffer[PROC_NUMBUF];
2618         size_t len;
2619         int ret;
2620
2621         if (!task)
2622                 return -ESRCH;
2623
2624         ret = 0;
2625         mm = get_task_mm(task);
2626         if (mm) {
2627                 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2628                                ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2629                                 MMF_DUMP_FILTER_SHIFT));
2630                 mmput(mm);
2631                 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2632         }
2633
2634         put_task_struct(task);
2635
2636         return ret;
2637 }
2638
2639 static ssize_t proc_coredump_filter_write(struct file *file,
2640                                           const char __user *buf,
2641                                           size_t count,
2642                                           loff_t *ppos)
2643 {
2644         struct task_struct *task;
2645         struct mm_struct *mm;
2646         unsigned int val;
2647         int ret;
2648         int i;
2649         unsigned long mask;
2650
2651         ret = kstrtouint_from_user(buf, count, 0, &val);
2652         if (ret < 0)
2653                 return ret;
2654
2655         ret = -ESRCH;
2656         task = get_proc_task(file_inode(file));
2657         if (!task)
2658                 goto out_no_task;
2659
2660         mm = get_task_mm(task);
2661         if (!mm)
2662                 goto out_no_mm;
2663         ret = 0;
2664
2665         for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2666                 if (val & mask)
2667                         set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2668                 else
2669                         clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2670         }
2671
2672         mmput(mm);
2673  out_no_mm:
2674         put_task_struct(task);
2675  out_no_task:
2676         if (ret < 0)
2677                 return ret;
2678         return count;
2679 }
2680
2681 static const struct file_operations proc_coredump_filter_operations = {
2682         .read           = proc_coredump_filter_read,
2683         .write          = proc_coredump_filter_write,
2684         .llseek         = generic_file_llseek,
2685 };
2686 #endif
2687
2688 #ifdef CONFIG_TASK_IO_ACCOUNTING
2689 static int do_io_accounting(struct task_struct *task, struct seq_file *m, int whole)
2690 {
2691         struct task_io_accounting acct = task->ioac;
2692         unsigned long flags;
2693         int result;
2694
2695         result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2696         if (result)
2697                 return result;
2698
2699         if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
2700                 result = -EACCES;
2701                 goto out_unlock;
2702         }
2703
2704         if (whole && lock_task_sighand(task, &flags)) {
2705                 struct task_struct *t = task;
2706
2707                 task_io_accounting_add(&acct, &task->signal->ioac);
2708                 while_each_thread(task, t)
2709                         task_io_accounting_add(&acct, &t->ioac);
2710
2711                 unlock_task_sighand(task, &flags);
2712         }
2713         seq_printf(m,
2714                    "rchar: %llu\n"
2715                    "wchar: %llu\n"
2716                    "syscr: %llu\n"
2717                    "syscw: %llu\n"
2718                    "read_bytes: %llu\n"
2719                    "write_bytes: %llu\n"
2720                    "cancelled_write_bytes: %llu\n",
2721                    (unsigned long long)acct.rchar,
2722                    (unsigned long long)acct.wchar,
2723                    (unsigned long long)acct.syscr,
2724                    (unsigned long long)acct.syscw,
2725                    (unsigned long long)acct.read_bytes,
2726                    (unsigned long long)acct.write_bytes,
2727                    (unsigned long long)acct.cancelled_write_bytes);
2728         result = 0;
2729
2730 out_unlock:
2731         mutex_unlock(&task->signal->cred_guard_mutex);
2732         return result;
2733 }
2734
2735 static int proc_tid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2736                                   struct pid *pid, struct task_struct *task)
2737 {
2738         return do_io_accounting(task, m, 0);
2739 }
2740
2741 static int proc_tgid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2742                                    struct pid *pid, struct task_struct *task)
2743 {
2744         return do_io_accounting(task, m, 1);
2745 }
2746 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2747
2748 #ifdef CONFIG_USER_NS
2749 static int proc_id_map_open(struct inode *inode, struct file *file,
2750         const struct seq_operations *seq_ops)
2751 {
2752         struct user_namespace *ns = NULL;
2753         struct task_struct *task;
2754         struct seq_file *seq;
2755         int ret = -EINVAL;
2756
2757         task = get_proc_task(inode);
2758         if (task) {
2759                 rcu_read_lock();
2760                 ns = get_user_ns(task_cred_xxx(task, user_ns));
2761                 rcu_read_unlock();
2762                 put_task_struct(task);
2763         }
2764         if (!ns)
2765                 goto err;
2766
2767         ret = seq_open(file, seq_ops);
2768         if (ret)
2769                 goto err_put_ns;
2770
2771         seq = file->private_data;
2772         seq->private = ns;
2773
2774         return 0;
2775 err_put_ns:
2776         put_user_ns(ns);
2777 err:
2778         return ret;
2779 }
2780
2781 static int proc_id_map_release(struct inode *inode, struct file *file)
2782 {
2783         struct seq_file *seq = file->private_data;
2784         struct user_namespace *ns = seq->private;
2785         put_user_ns(ns);
2786         return seq_release(inode, file);
2787 }
2788
2789 static int proc_uid_map_open(struct inode *inode, struct file *file)
2790 {
2791         return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2792 }
2793
2794 static int proc_gid_map_open(struct inode *inode, struct file *file)
2795 {
2796         return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2797 }
2798
2799 static int proc_projid_map_open(struct inode *inode, struct file *file)
2800 {
2801         return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2802 }
2803
2804 static const struct file_operations proc_uid_map_operations = {
2805         .open           = proc_uid_map_open,
2806         .write          = proc_uid_map_write,
2807         .read           = seq_read,
2808         .llseek         = seq_lseek,
2809         .release        = proc_id_map_release,
2810 };
2811
2812 static const struct file_operations proc_gid_map_operations = {
2813         .open           = proc_gid_map_open,
2814         .write          = proc_gid_map_write,
2815         .read           = seq_read,
2816         .llseek         = seq_lseek,
2817         .release        = proc_id_map_release,
2818 };
2819
2820 static const struct file_operations proc_projid_map_operations = {
2821         .open           = proc_projid_map_open,
2822         .write          = proc_projid_map_write,
2823         .read           = seq_read,
2824         .llseek         = seq_lseek,
2825         .release        = proc_id_map_release,
2826 };
2827
2828 static int proc_setgroups_open(struct inode *inode, struct file *file)
2829 {
2830         struct user_namespace *ns = NULL;
2831         struct task_struct *task;
2832         int ret;
2833
2834         ret = -ESRCH;
2835         task = get_proc_task(inode);
2836         if (task) {
2837                 rcu_read_lock();
2838                 ns = get_user_ns(task_cred_xxx(task, user_ns));
2839                 rcu_read_unlock();
2840                 put_task_struct(task);
2841         }
2842         if (!ns)
2843                 goto err;
2844
2845         if (file->f_mode & FMODE_WRITE) {
2846                 ret = -EACCES;
2847                 if (!ns_capable(ns, CAP_SYS_ADMIN))
2848                         goto err_put_ns;
2849         }
2850
2851         ret = single_open(file, &proc_setgroups_show, ns);
2852         if (ret)
2853                 goto err_put_ns;
2854
2855         return 0;
2856 err_put_ns:
2857         put_user_ns(ns);
2858 err:
2859         return ret;
2860 }
2861
2862 static int proc_setgroups_release(struct inode *inode, struct file *file)
2863 {
2864         struct seq_file *seq = file->private_data;
2865         struct user_namespace *ns = seq->private;
2866         int ret = single_release(inode, file);
2867         put_user_ns(ns);
2868         return ret;
2869 }
2870
2871 static const struct file_operations proc_setgroups_operations = {
2872         .open           = proc_setgroups_open,
2873         .write          = proc_setgroups_write,
2874         .read           = seq_read,
2875         .llseek         = seq_lseek,
2876         .release        = proc_setgroups_release,
2877 };
2878 #endif /* CONFIG_USER_NS */
2879
2880 static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2881                                 struct pid *pid, struct task_struct *task)
2882 {
2883         int err = lock_trace(task);
2884         if (!err) {
2885                 seq_printf(m, "%08x\n", task->personality);
2886                 unlock_trace(task);
2887         }
2888         return err;
2889 }
2890
2891 /*
2892  * Thread groups
2893  */
2894 static const struct file_operations proc_task_operations;
2895 static const struct inode_operations proc_task_inode_operations;
2896
2897 static const struct pid_entry tgid_base_stuff[] = {
2898         DIR("task",       S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2899         DIR("fd",         S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2900         DIR("map_files",  S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
2901         DIR("fdinfo",     S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2902         DIR("ns",         S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
2903 #ifdef CONFIG_NET
2904         DIR("net",        S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2905 #endif
2906         REG("environ",    S_IRUSR, proc_environ_operations),
2907         REG("auxv",       S_IRUSR, proc_auxv_operations),
2908         ONE("status",     S_IRUGO, proc_pid_status),
2909         ONE("personality", S_IRUSR, proc_pid_personality),
2910         ONE("limits",     S_IRUGO, proc_pid_limits),
2911 #ifdef CONFIG_SCHED_DEBUG
2912         REG("sched",      S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2913 #endif
2914 #ifdef CONFIG_SCHED_AUTOGROUP
2915         REG("autogroup",  S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2916 #endif
2917         REG("comm",      S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2918 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2919         ONE("syscall",    S_IRUSR, proc_pid_syscall),
2920 #endif
2921         REG("cmdline",    S_IRUGO, proc_pid_cmdline_ops),
2922         ONE("stat",       S_IRUGO, proc_tgid_stat),
2923         ONE("statm",      S_IRUGO, proc_pid_statm),
2924         REG("maps",       S_IRUGO, proc_pid_maps_operations),
2925 #ifdef CONFIG_NUMA
2926         REG("numa_maps",  S_IRUGO, proc_pid_numa_maps_operations),
2927 #endif
2928         REG("mem",        S_IRUSR|S_IWUSR, proc_mem_operations),
2929         LNK("cwd",        proc_cwd_link),
2930         LNK("root",       proc_root_link),
2931         LNK("exe",        proc_exe_link),
2932         REG("mounts",     S_IRUGO, proc_mounts_operations),
2933         REG("mountinfo",  S_IRUGO, proc_mountinfo_operations),
2934         REG("mountstats", S_IRUSR, proc_mountstats_operations),
2935 #ifdef CONFIG_PROC_PAGE_MONITOR
2936         REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2937         REG("smaps",      S_IRUGO, proc_pid_smaps_operations),
2938         REG("pagemap",    S_IRUSR, proc_pagemap_operations),
2939 #endif
2940 #ifdef CONFIG_SECURITY
2941         DIR("attr",       S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
2942 #endif
2943 #ifdef CONFIG_KALLSYMS
2944         ONE("wchan",      S_IRUGO, proc_pid_wchan),
2945 #endif
2946 #ifdef CONFIG_STACKTRACE
2947         ONE("stack",      S_IRUSR, proc_pid_stack),
2948 #endif
2949 #ifdef CONFIG_SCHED_INFO
2950         ONE("schedstat",  S_IRUGO, proc_pid_schedstat),
2951 #endif
2952 #ifdef CONFIG_LATENCYTOP
2953         REG("latency",  S_IRUGO, proc_lstats_operations),
2954 #endif
2955 #ifdef CONFIG_PROC_PID_CPUSET
2956         ONE("cpuset",     S_IRUGO, proc_cpuset_show),
2957 #endif
2958 #ifdef CONFIG_CGROUPS
2959         ONE("cgroup",  S_IRUGO, proc_cgroup_show),
2960 #endif
2961         ONE("oom_score",  S_IRUGO, proc_oom_score),
2962         REG("oom_adj",    S_IRUGO|S_IWUSR, proc_oom_adj_operations),
2963         REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
2964 #ifdef CONFIG_AUDITSYSCALL
2965         REG("loginuid",   S_IWUSR|S_IRUGO, proc_loginuid_operations),
2966         REG("sessionid",  S_IRUGO, proc_sessionid_operations),
2967 #endif
2968 #ifdef CONFIG_FAULT_INJECTION
2969         REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2970 #endif
2971 #ifdef CONFIG_ELF_CORE
2972         REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
2973 #endif
2974 #ifdef CONFIG_TASK_IO_ACCOUNTING
2975         ONE("io",       S_IRUSR, proc_tgid_io_accounting),
2976 #endif
2977 #ifdef CONFIG_HARDWALL
2978         ONE("hardwall",   S_IRUGO, proc_pid_hardwall),
2979 #endif
2980 #ifdef CONFIG_USER_NS
2981         REG("uid_map",    S_IRUGO|S_IWUSR, proc_uid_map_operations),
2982         REG("gid_map",    S_IRUGO|S_IWUSR, proc_gid_map_operations),
2983         REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
2984         REG("setgroups",  S_IRUGO|S_IWUSR, proc_setgroups_operations),
2985 #endif
2986 #ifdef CONFIG_CHECKPOINT_RESTORE
2987         REG("timers",     S_IRUGO, proc_timers_operations),
2988 #endif
2989         REG("timerslack_ns", S_IRUGO|S_IWUGO, proc_pid_set_timerslack_ns_operations),
2990 };
2991
2992 static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
2993 {
2994         return proc_pident_readdir(file, ctx,
2995                                    tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2996 }
2997
2998 static const struct file_operations proc_tgid_base_operations = {
2999         .read           = generic_read_dir,
3000         .iterate_shared = proc_tgid_base_readdir,
3001         .llseek         = generic_file_llseek,
3002 };
3003
3004 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3005 {
3006         return proc_pident_lookup(dir, dentry,
3007                                   tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
3008 }
3009
3010 static const struct inode_operations proc_tgid_base_inode_operations = {
3011         .lookup         = proc_tgid_base_lookup,
3012         .getattr        = pid_getattr,
3013         .setattr        = proc_setattr,
3014         .permission     = proc_pid_permission,
3015 };
3016
3017 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
3018 {
3019         struct dentry *dentry, *leader, *dir;
3020         char buf[PROC_NUMBUF];
3021         struct qstr name;
3022
3023         name.name = buf;
3024         name.len = snprintf(buf, sizeof(buf), "%d", pid);
3025         /* no ->d_hash() rejects on procfs */
3026         dentry = d_hash_and_lookup(mnt->mnt_root, &name);
3027         if (dentry) {
3028                 d_invalidate(dentry);
3029                 dput(dentry);
3030         }
3031
3032         if (pid == tgid)
3033                 return;
3034
3035         name.name = buf;
3036         name.len = snprintf(buf, sizeof(buf), "%d", tgid);
3037         leader = d_hash_and_lookup(mnt->mnt_root, &name);
3038         if (!leader)
3039                 goto out;
3040
3041         name.name = "task";
3042         name.len = strlen(name.name);
3043         dir = d_hash_and_lookup(leader, &name);
3044         if (!dir)
3045                 goto out_put_leader;
3046
3047         name.name = buf;
3048         name.len = snprintf(buf, sizeof(buf), "%d", pid);
3049         dentry = d_hash_and_lookup(dir, &name);
3050         if (dentry) {
3051                 d_invalidate(dentry);
3052                 dput(dentry);
3053         }
3054
3055         dput(dir);
3056 out_put_leader:
3057         dput(leader);
3058 out:
3059         return;
3060 }
3061
3062 /**
3063  * proc_flush_task -  Remove dcache entries for @task from the /proc dcache.
3064  * @task: task that should be flushed.
3065  *
3066  * When flushing dentries from proc, one needs to flush them from global
3067  * proc (proc_mnt) and from all the namespaces' procs this task was seen
3068  * in. This call is supposed to do all of this job.
3069  *
3070  * Looks in the dcache for
3071  * /proc/@pid
3072  * /proc/@tgid/task/@pid
3073  * if either directory is present flushes it and all of it'ts children
3074  * from the dcache.
3075  *
3076  * It is safe and reasonable to cache /proc entries for a task until
3077  * that task exits.  After that they just clog up the dcache with
3078  * useless entries, possibly causing useful dcache entries to be
3079  * flushed instead.  This routine is proved to flush those useless
3080  * dcache entries at process exit time.
3081  *
3082  * NOTE: This routine is just an optimization so it does not guarantee
3083  *       that no dcache entries will exist at process exit time it
3084  *       just makes it very unlikely that any will persist.
3085  */
3086
3087 void proc_flush_task(struct task_struct *task)
3088 {
3089         int i;
3090         struct pid *pid, *tgid;
3091         struct upid *upid;
3092
3093         pid = task_pid(task);
3094         tgid = task_tgid(task);
3095
3096         for (i = 0; i <= pid->level; i++) {
3097                 upid = &pid->numbers[i];
3098                 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
3099                                         tgid->numbers[i].nr);
3100         }
3101 }
3102
3103 static int proc_pid_instantiate(struct inode *dir,
3104                                    struct dentry * dentry,
3105                                    struct task_struct *task, const void *ptr)
3106 {
3107         struct inode *inode;
3108
3109         inode = proc_pid_make_inode(dir->i_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
3110         if (!inode)
3111                 goto out;
3112
3113         inode->i_op = &proc_tgid_base_inode_operations;
3114         inode->i_fop = &proc_tgid_base_operations;
3115         inode->i_flags|=S_IMMUTABLE;
3116
3117         set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
3118                                                   ARRAY_SIZE(tgid_base_stuff)));
3119
3120         d_set_d_op(dentry, &pid_dentry_operations);
3121
3122         d_add(dentry, inode);
3123         /* Close the race of the process dying before we return the dentry */
3124         if (pid_revalidate(dentry, 0))
3125                 return 0;
3126 out:
3127         return -ENOENT;
3128 }
3129
3130 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
3131 {
3132         int result = -ENOENT;
3133         struct task_struct *task;
3134         unsigned tgid;
3135         struct pid_namespace *ns;
3136
3137         tgid = name_to_int(&dentry->d_name);
3138         if (tgid == ~0U)
3139                 goto out;
3140
3141         ns = dentry->d_sb->s_fs_info;
3142         rcu_read_lock();
3143         task = find_task_by_pid_ns(tgid, ns);
3144         if (task)
3145                 get_task_struct(task);
3146         rcu_read_unlock();
3147         if (!task)
3148                 goto out;
3149
3150         result = proc_pid_instantiate(dir, dentry, task, NULL);
3151         put_task_struct(task);
3152 out:
3153         return ERR_PTR(result);
3154 }
3155
3156 /*
3157  * Find the first task with tgid >= tgid
3158  *
3159  */
3160 struct tgid_iter {
3161         unsigned int tgid;
3162         struct task_struct *task;
3163 };
3164 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3165 {
3166         struct pid *pid;
3167
3168         if (iter.task)
3169                 put_task_struct(iter.task);
3170         rcu_read_lock();
3171 retry:
3172         iter.task = NULL;
3173         pid = find_ge_pid(iter.tgid, ns);
3174         if (pid) {
3175                 iter.tgid = pid_nr_ns(pid, ns);
3176                 iter.task = pid_task(pid, PIDTYPE_PID);
3177                 /* What we to know is if the pid we have find is the
3178                  * pid of a thread_group_leader.  Testing for task
3179                  * being a thread_group_leader is the obvious thing
3180                  * todo but there is a window when it fails, due to
3181                  * the pid transfer logic in de_thread.
3182                  *
3183                  * So we perform the straight forward test of seeing
3184                  * if the pid we have found is the pid of a thread
3185                  * group leader, and don't worry if the task we have
3186                  * found doesn't happen to be a thread group leader.
3187                  * As we don't care in the case of readdir.
3188                  */
3189                 if (!iter.task || !has_group_leader_pid(iter.task)) {
3190                         iter.tgid += 1;
3191                         goto retry;
3192                 }
3193                 get_task_struct(iter.task);
3194         }
3195         rcu_read_unlock();
3196         return iter;
3197 }
3198
3199 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
3200
3201 /* for the /proc/ directory itself, after non-process stuff has been done */
3202 int proc_pid_readdir(struct file *file, struct dir_context *ctx)
3203 {
3204         struct tgid_iter iter;
3205         struct pid_namespace *ns = file_inode(file)->i_sb->s_fs_info;
3206         loff_t pos = ctx->pos;
3207
3208         if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
3209                 return 0;
3210
3211         if (pos == TGID_OFFSET - 2) {
3212                 struct inode *inode = d_inode(ns->proc_self);
3213                 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
3214                         return 0;
3215                 ctx->pos = pos = pos + 1;
3216         }
3217         if (pos == TGID_OFFSET - 1) {
3218                 struct inode *inode = d_inode(ns->proc_thread_self);
3219                 if (!dir_emit(ctx, "thread-self", 11, inode->i_ino, DT_LNK))
3220                         return 0;
3221                 ctx->pos = pos = pos + 1;
3222         }
3223         iter.tgid = pos - TGID_OFFSET;
3224         iter.task = NULL;
3225         for (iter = next_tgid(ns, iter);
3226              iter.task;
3227              iter.tgid += 1, iter = next_tgid(ns, iter)) {
3228                 char name[PROC_NUMBUF];
3229                 int len;
3230
3231                 cond_resched();
3232                 if (!has_pid_permissions(ns, iter.task, 2))
3233                         continue;
3234
3235                 len = snprintf(name, sizeof(name), "%d", iter.tgid);
3236                 ctx->pos = iter.tgid + TGID_OFFSET;
3237                 if (!proc_fill_cache(file, ctx, name, len,
3238                                      proc_pid_instantiate, iter.task, NULL)) {
3239                         put_task_struct(iter.task);
3240                         return 0;
3241                 }
3242         }
3243         ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
3244         return 0;
3245 }
3246
3247 /*
3248  * proc_tid_comm_permission is a special permission function exclusively
3249  * used for the node /proc/<pid>/task/<tid>/comm.
3250  * It bypasses generic permission checks in the case where a task of the same
3251  * task group attempts to access the node.
3252  * The rationale behind this is that glibc and bionic access this node for
3253  * cross thread naming (pthread_set/getname_np(!self)). However, if
3254  * PR_SET_DUMPABLE gets set to 0 this node among others becomes uid=0 gid=0,
3255  * which locks out the cross thread naming implementation.
3256  * This function makes sure that the node is always accessible for members of
3257  * same thread group.
3258  */
3259 static int proc_tid_comm_permission(struct inode *inode, int mask)
3260 {
3261         bool is_same_tgroup;
3262         struct task_struct *task;
3263
3264         task = get_proc_task(inode);
3265         if (!task)
3266                 return -ESRCH;
3267         is_same_tgroup = same_thread_group(current, task);
3268         put_task_struct(task);
3269
3270         if (likely(is_same_tgroup && !(mask & MAY_EXEC))) {
3271                 /* This file (/proc/<pid>/task/<tid>/comm) can always be
3272                  * read or written by the members of the corresponding
3273                  * thread group.
3274                  */
3275                 return 0;
3276         }
3277
3278         return generic_permission(inode, mask);
3279 }
3280
3281 static const struct inode_operations proc_tid_comm_inode_operations = {
3282                 .permission = proc_tid_comm_permission,
3283 };
3284
3285 /*
3286  * Tasks
3287  */
3288 static const struct pid_entry tid_base_stuff[] = {
3289         DIR("fd",        S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
3290         DIR("fdinfo",    S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
3291         DIR("ns",        S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
3292 #ifdef CONFIG_NET
3293         DIR("net",        S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
3294 #endif
3295         REG("environ",   S_IRUSR, proc_environ_operations),
3296         REG("auxv",      S_IRUSR, proc_auxv_operations),
3297         ONE("status",    S_IRUGO, proc_pid_status),
3298         ONE("personality", S_IRUSR, proc_pid_personality),
3299         ONE("limits",    S_IRUGO, proc_pid_limits),
3300 #ifdef CONFIG_SCHED_DEBUG
3301         REG("sched",     S_IRUGO|S_IWUSR, proc_pid_sched_operations),
3302 #endif
3303         NOD("comm",      S_IFREG|S_IRUGO|S_IWUSR,
3304                          &proc_tid_comm_inode_operations,
3305                          &proc_pid_set_comm_operations, {}),
3306 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3307         ONE("syscall",   S_IRUSR, proc_pid_syscall),
3308 #endif
3309         REG("cmdline",   S_IRUGO, proc_pid_cmdline_ops),
3310         ONE("stat",      S_IRUGO, proc_tid_stat),
3311         ONE("statm",     S_IRUGO, proc_pid_statm),
3312         REG("maps",      S_IRUGO, proc_tid_maps_operations),
3313 #ifdef CONFIG_PROC_CHILDREN
3314         REG("children",  S_IRUGO, proc_tid_children_operations),
3315 #endif
3316 #ifdef CONFIG_NUMA
3317         REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
3318 #endif
3319         REG("mem",       S_IRUSR|S_IWUSR, proc_mem_operations),
3320         LNK("cwd",       proc_cwd_link),
3321         LNK("root",      proc_root_link),
3322         LNK("exe",       proc_exe_link),
3323         REG("mounts",    S_IRUGO, proc_mounts_operations),
3324         REG("mountinfo",  S_IRUGO, proc_mountinfo_operations),
3325 #ifdef CONFIG_PROC_PAGE_MONITOR
3326         REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3327         REG("smaps",     S_IRUGO, proc_tid_smaps_operations),
3328         REG("pagemap",    S_IRUSR, proc_pagemap_operations),
3329 #endif
3330 #ifdef CONFIG_SECURITY
3331         DIR("attr",      S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
3332 #endif
3333 #ifdef CONFIG_KALLSYMS
3334         ONE("wchan",     S_IRUGO, proc_pid_wchan),
3335 #endif
3336 #ifdef CONFIG_STACKTRACE
3337         ONE("stack",      S_IRUSR, proc_pid_stack),
3338 #endif
3339 #ifdef CONFIG_SCHED_INFO
3340         ONE("schedstat", S_IRUGO, proc_pid_schedstat),
3341 #endif
3342 #ifdef CONFIG_LATENCYTOP
3343         REG("latency",  S_IRUGO, proc_lstats_operations),
3344 #endif
3345 #ifdef CONFIG_PROC_PID_CPUSET
3346         ONE("cpuset",    S_IRUGO, proc_cpuset_show),
3347 #endif
3348 #ifdef CONFIG_CGROUPS
3349         ONE("cgroup",  S_IRUGO, proc_cgroup_show),
3350 #endif
3351         ONE("oom_score", S_IRUGO, proc_oom_score),
3352         REG("oom_adj",   S_IRUGO|S_IWUSR, proc_oom_adj_operations),
3353         REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
3354 #ifdef CONFIG_AUDITSYSCALL
3355         REG("loginuid",  S_IWUSR|S_IRUGO, proc_loginuid_operations),
3356         REG("sessionid",  S_IRUGO, proc_sessionid_operations),
3357 #endif
3358 #ifdef CONFIG_FAULT_INJECTION
3359         REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3360 #endif
3361 #ifdef CONFIG_TASK_IO_ACCOUNTING
3362         ONE("io",       S_IRUSR, proc_tid_io_accounting),
3363 #endif
3364 #ifdef CONFIG_HARDWALL
3365         ONE("hardwall",   S_IRUGO, proc_pid_hardwall),
3366 #endif
3367 #ifdef CONFIG_USER_NS
3368         REG("uid_map",    S_IRUGO|S_IWUSR, proc_uid_map_operations),
3369         REG("gid_map",    S_IRUGO|S_IWUSR, proc_gid_map_operations),
3370         REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
3371         REG("setgroups",  S_IRUGO|S_IWUSR, proc_setgroups_operations),
3372 #endif
3373 };
3374
3375 static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
3376 {
3377         return proc_pident_readdir(file, ctx,
3378                                    tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3379 }
3380
3381 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3382 {
3383         return proc_pident_lookup(dir, dentry,
3384                                   tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3385 }
3386
3387 static const struct file_operations proc_tid_base_operations = {
3388         .read           = generic_read_dir,
3389         .iterate_shared = proc_tid_base_readdir,
3390         .llseek         = generic_file_llseek,
3391 };
3392
3393 static const struct inode_operations proc_tid_base_inode_operations = {
3394         .lookup         = proc_tid_base_lookup,
3395         .getattr        = pid_getattr,
3396         .setattr        = proc_setattr,
3397 };
3398
3399 static int proc_task_instantiate(struct inode *dir,
3400         struct dentry *dentry, struct task_struct *task, const void *ptr)
3401 {
3402         struct inode *inode;
3403         inode = proc_pid_make_inode(dir->i_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
3404
3405         if (!inode)
3406                 goto out;
3407         inode->i_op = &proc_tid_base_inode_operations;
3408         inode->i_fop = &proc_tid_base_operations;
3409         inode->i_flags|=S_IMMUTABLE;
3410
3411         set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3412                                                   ARRAY_SIZE(tid_base_stuff)));
3413
3414         d_set_d_op(dentry, &pid_dentry_operations);
3415
3416         d_add(dentry, inode);
3417         /* Close the race of the process dying before we return the dentry */
3418         if (pid_revalidate(dentry, 0))
3419                 return 0;
3420 out:
3421         return -ENOENT;
3422 }
3423
3424 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
3425 {
3426         int result = -ENOENT;
3427         struct task_struct *task;
3428         struct task_struct *leader = get_proc_task(dir);
3429         unsigned tid;
3430         struct pid_namespace *ns;
3431
3432         if (!leader)
3433                 goto out_no_task;
3434
3435         tid = name_to_int(&dentry->d_name);
3436         if (tid == ~0U)
3437                 goto out;
3438
3439         ns = dentry->d_sb->s_fs_info;
3440         rcu_read_lock();
3441         task = find_task_by_pid_ns(tid, ns);
3442         if (task)
3443                 get_task_struct(task);
3444         rcu_read_unlock();
3445         if (!task)
3446                 goto out;
3447         if (!same_thread_group(leader, task))
3448                 goto out_drop_task;
3449
3450         result = proc_task_instantiate(dir, dentry, task, NULL);
3451 out_drop_task:
3452         put_task_struct(task);
3453 out:
3454         put_task_struct(leader);
3455 out_no_task:
3456         return ERR_PTR(result);
3457 }
3458
3459 /*
3460  * Find the first tid of a thread group to return to user space.
3461  *
3462  * Usually this is just the thread group leader, but if the users
3463  * buffer was too small or there was a seek into the middle of the
3464  * directory we have more work todo.
3465  *
3466  * In the case of a short read we start with find_task_by_pid.
3467  *
3468  * In the case of a seek we start with the leader and walk nr
3469  * threads past it.
3470  */
3471 static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
3472                                         struct pid_namespace *ns)
3473 {
3474         struct task_struct *pos, *task;
3475         unsigned long nr = f_pos;
3476
3477         if (nr != f_pos)        /* 32bit overflow? */
3478                 return NULL;
3479
3480         rcu_read_lock();
3481         task = pid_task(pid, PIDTYPE_PID);
3482         if (!task)
3483                 goto fail;
3484
3485         /* Attempt to start with the tid of a thread */
3486         if (tid && nr) {
3487                 pos = find_task_by_pid_ns(tid, ns);
3488                 if (pos && same_thread_group(pos, task))
3489                         goto found;
3490         }
3491
3492         /* If nr exceeds the number of threads there is nothing todo */
3493         if (nr >= get_nr_threads(task))
3494                 goto fail;
3495
3496         /* If we haven't found our starting place yet start
3497          * with the leader and walk nr threads forward.
3498          */
3499         pos = task = task->group_leader;
3500         do {
3501                 if (!nr--)
3502                         goto found;
3503         } while_each_thread(task, pos);
3504 fail:
3505         pos = NULL;
3506         goto out;
3507 found:
3508         get_task_struct(pos);
3509 out:
3510         rcu_read_unlock();
3511         return pos;
3512 }
3513
3514 /*
3515  * Find the next thread in the thread list.
3516  * Return NULL if there is an error or no next thread.
3517  *
3518  * The reference to the input task_struct is released.
3519  */
3520 static struct task_struct *next_tid(struct task_struct *start)
3521 {
3522         struct task_struct *pos = NULL;
3523         rcu_read_lock();
3524         if (pid_alive(start)) {
3525                 pos = next_thread(start);
3526                 if (thread_group_leader(pos))
3527                         pos = NULL;
3528                 else
3529                         get_task_struct(pos);
3530         }
3531         rcu_read_unlock();
3532         put_task_struct(start);
3533         return pos;
3534 }
3535
3536 /* for the /proc/TGID/task/ directories */
3537 static int proc_task_readdir(struct file *file, struct dir_context *ctx)
3538 {
3539         struct inode *inode = file_inode(file);
3540         struct task_struct *task;
3541         struct pid_namespace *ns;
3542         int tid;
3543
3544         if (proc_inode_is_dead(inode))
3545                 return -ENOENT;
3546
3547         if (!dir_emit_dots(file, ctx))
3548                 return 0;
3549
3550         /* f_version caches the tgid value that the last readdir call couldn't
3551          * return. lseek aka telldir automagically resets f_version to 0.
3552          */
3553         ns = inode->i_sb->s_fs_info;
3554         tid = (int)file->f_version;
3555         file->f_version = 0;
3556         for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
3557              task;
3558              task = next_tid(task), ctx->pos++) {
3559                 char name[PROC_NUMBUF];
3560                 int len;
3561                 tid = task_pid_nr_ns(task, ns);
3562                 len = snprintf(name, sizeof(name), "%d", tid);
3563                 if (!proc_fill_cache(file, ctx, name, len,
3564                                 proc_task_instantiate, task, NULL)) {
3565                         /* returning this tgid failed, save it as the first
3566                          * pid for the next readir call */
3567                         file->f_version = (u64)tid;
3568                         put_task_struct(task);
3569                         break;
3570                 }
3571         }
3572
3573         return 0;
3574 }
3575
3576 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3577 {
3578         struct inode *inode = d_inode(dentry);
3579         struct task_struct *p = get_proc_task(inode);
3580         generic_fillattr(inode, stat);
3581
3582         if (p) {
3583                 stat->nlink += get_nr_threads(p);
3584                 put_task_struct(p);
3585         }
3586
3587         return 0;
3588 }
3589
3590 static const struct inode_operations proc_task_inode_operations = {
3591         .lookup         = proc_task_lookup,
3592         .getattr        = proc_task_getattr,
3593         .setattr        = proc_setattr,
3594         .permission     = proc_pid_permission,
3595 };
3596
3597 static const struct file_operations proc_task_operations = {
3598         .read           = generic_read_dir,
3599         .iterate_shared = proc_task_readdir,
3600         .llseek         = generic_file_llseek,
3601 };