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