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