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