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