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