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