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