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