linux/kernel/cgroup.c
<<
>>
Prefs
   1/*
   2 *  Generic process-grouping system.
   3 *
   4 *  Based originally on the cpuset system, extracted by Paul Menage
   5 *  Copyright (C) 2006 Google, Inc
   6 *
   7 *  Notifications support
   8 *  Copyright (C) 2009 Nokia Corporation
   9 *  Author: Kirill A. Shutemov
  10 *
  11 *  Copyright notices from the original cpuset code:
  12 *  --------------------------------------------------
  13 *  Copyright (C) 2003 BULL SA.
  14 *  Copyright (C) 2004-2006 Silicon Graphics, Inc.
  15 *
  16 *  Portions derived from Patrick Mochel's sysfs code.
  17 *  sysfs is Copyright (c) 2001-3 Patrick Mochel
  18 *
  19 *  2003-10-10 Written by Simon Derr.
  20 *  2003-10-22 Updates by Stephen Hemminger.
  21 *  2004 May-July Rework by Paul Jackson.
  22 *  ---------------------------------------------------
  23 *
  24 *  This file is subject to the terms and conditions of the GNU General Public
  25 *  License.  See the file COPYING in the main directory of the Linux
  26 *  distribution for more details.
  27 */
  28
  29#include <linux/cgroup.h>
  30#include <linux/cred.h>
  31#include <linux/ctype.h>
  32#include <linux/errno.h>
  33#include <linux/fs.h>
  34#include <linux/init_task.h>
  35#include <linux/kernel.h>
  36#include <linux/list.h>
  37#include <linux/mm.h>
  38#include <linux/mutex.h>
  39#include <linux/mount.h>
  40#include <linux/pagemap.h>
  41#include <linux/proc_fs.h>
  42#include <linux/rcupdate.h>
  43#include <linux/sched.h>
  44#include <linux/backing-dev.h>
  45#include <linux/seq_file.h>
  46#include <linux/slab.h>
  47#include <linux/magic.h>
  48#include <linux/spinlock.h>
  49#include <linux/string.h>
  50#include <linux/sort.h>
  51#include <linux/kmod.h>
  52#include <linux/module.h>
  53#include <linux/delayacct.h>
  54#include <linux/cgroupstats.h>
  55#include <linux/hashtable.h>
  56#include <linux/namei.h>
  57#include <linux/pid_namespace.h>
  58#include <linux/idr.h>
  59#include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
  60#include <linux/eventfd.h>
  61#include <linux/poll.h>
  62#include <linux/flex_array.h> /* used in cgroup_attach_proc */
  63#include <linux/kthread.h>
  64
  65#include <linux/atomic.h>
  66
  67/* css deactivation bias, makes css->refcnt negative to deny new trygets */
  68#define CSS_DEACT_BIAS          INT_MIN
  69
  70/*
  71 * cgroup_mutex is the master lock.  Any modification to cgroup or its
  72 * hierarchy must be performed while holding it.
  73 *
  74 * cgroup_root_mutex nests inside cgroup_mutex and should be held to modify
  75 * cgroupfs_root of any cgroup hierarchy - subsys list, flags,
  76 * release_agent_path and so on.  Modifying requires both cgroup_mutex and
  77 * cgroup_root_mutex.  Readers can acquire either of the two.  This is to
  78 * break the following locking order cycle.
  79 *
  80 *  A. cgroup_mutex -> cred_guard_mutex -> s_type->i_mutex_key -> namespace_sem
  81 *  B. namespace_sem -> cgroup_mutex
  82 *
  83 * B happens only through cgroup_show_options() and using cgroup_root_mutex
  84 * breaks it.
  85 */
  86static DEFINE_MUTEX(cgroup_mutex);
  87static DEFINE_MUTEX(cgroup_root_mutex);
  88
  89/*
  90 * Generate an array of cgroup subsystem pointers. At boot time, this is
  91 * populated with the built in subsystems, and modular subsystems are
  92 * registered after that. The mutable section of this array is protected by
  93 * cgroup_mutex.
  94 */
  95#define SUBSYS(_x) [_x ## _subsys_id] = &_x ## _subsys,
  96#define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option)
  97static struct cgroup_subsys *subsys[CGROUP_SUBSYS_COUNT] = {
  98#include <linux/cgroup_subsys.h>
  99};
 100
 101#define MAX_CGROUP_ROOT_NAMELEN 64
 102
 103/*
 104 * A cgroupfs_root represents the root of a cgroup hierarchy,
 105 * and may be associated with a superblock to form an active
 106 * hierarchy
 107 */
 108struct cgroupfs_root {
 109        struct super_block *sb;
 110
 111        /*
 112         * The bitmask of subsystems intended to be attached to this
 113         * hierarchy
 114         */
 115        unsigned long subsys_mask;
 116
 117        /* Unique id for this hierarchy. */
 118        int hierarchy_id;
 119
 120        /* The bitmask of subsystems currently attached to this hierarchy */
 121        unsigned long actual_subsys_mask;
 122
 123        /* A list running through the attached subsystems */
 124        struct list_head subsys_list;
 125
 126        /* The root cgroup for this hierarchy */
 127        struct cgroup top_cgroup;
 128
 129        /* Tracks how many cgroups are currently defined in hierarchy.*/
 130        int number_of_cgroups;
 131
 132        /* A list running through the active hierarchies */
 133        struct list_head root_list;
 134
 135        /* All cgroups on this root, cgroup_mutex protected */
 136        struct list_head allcg_list;
 137
 138        /* Hierarchy-specific flags */
 139        unsigned long flags;
 140
 141        /* IDs for cgroups in this hierarchy */
 142        struct ida cgroup_ida;
 143
 144        /* The path to use for release notifications. */
 145        char release_agent_path[PATH_MAX];
 146
 147        /* The name for this hierarchy - may be empty */
 148        char name[MAX_CGROUP_ROOT_NAMELEN];
 149};
 150
 151/*
 152 * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the
 153 * subsystems that are otherwise unattached - it never has more than a
 154 * single cgroup, and all tasks are part of that cgroup.
 155 */
 156static struct cgroupfs_root rootnode;
 157
 158/*
 159 * cgroupfs file entry, pointed to from leaf dentry->d_fsdata.
 160 */
 161struct cfent {
 162        struct list_head                node;
 163        struct dentry                   *dentry;
 164        struct cftype                   *type;
 165
 166        /* file xattrs */
 167        struct simple_xattrs            xattrs;
 168};
 169
 170/*
 171 * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when
 172 * cgroup_subsys->use_id != 0.
 173 */
 174#define CSS_ID_MAX      (65535)
 175struct css_id {
 176        /*
 177         * The css to which this ID points. This pointer is set to valid value
 178         * after cgroup is populated. If cgroup is removed, this will be NULL.
 179         * This pointer is expected to be RCU-safe because destroy()
 180         * is called after synchronize_rcu(). But for safe use, css_tryget()
 181         * should be used for avoiding race.
 182         */
 183        struct cgroup_subsys_state __rcu *css;
 184        /*
 185         * ID of this css.
 186         */
 187        unsigned short id;
 188        /*
 189         * Depth in hierarchy which this ID belongs to.
 190         */
 191        unsigned short depth;
 192        /*
 193         * ID is freed by RCU. (and lookup routine is RCU safe.)
 194         */
 195        struct rcu_head rcu_head;
 196        /*
 197         * Hierarchy of CSS ID belongs to.
 198         */
 199        unsigned short stack[0]; /* Array of Length (depth+1) */
 200};
 201
 202/*
 203 * cgroup_event represents events which userspace want to receive.
 204 */
 205struct cgroup_event {
 206        /*
 207         * Cgroup which the event belongs to.
 208         */
 209        struct cgroup *cgrp;
 210        /*
 211         * Control file which the event associated.
 212         */
 213        struct cftype *cft;
 214        /*
 215         * eventfd to signal userspace about the event.
 216         */
 217        struct eventfd_ctx *eventfd;
 218        /*
 219         * Each of these stored in a list by the cgroup.
 220         */
 221        struct list_head list;
 222        /*
 223         * All fields below needed to unregister event when
 224         * userspace closes eventfd.
 225         */
 226        poll_table pt;
 227        wait_queue_head_t *wqh;
 228        wait_queue_t wait;
 229        struct work_struct remove;
 230};
 231
 232/* The list of hierarchy roots */
 233
 234static LIST_HEAD(roots);
 235static int root_count;
 236
 237static DEFINE_IDA(hierarchy_ida);
 238static int next_hierarchy_id;
 239static DEFINE_SPINLOCK(hierarchy_id_lock);
 240
 241/* dummytop is a shorthand for the dummy hierarchy's top cgroup */
 242#define dummytop (&rootnode.top_cgroup)
 243
 244/* This flag indicates whether tasks in the fork and exit paths should
 245 * check for fork/exit handlers to call. This avoids us having to do
 246 * extra work in the fork/exit path if none of the subsystems need to
 247 * be called.
 248 */
 249static int need_forkexit_callback __read_mostly;
 250
 251static int cgroup_destroy_locked(struct cgroup *cgrp);
 252static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys,
 253                              struct cftype cfts[], bool is_add);
 254
 255#ifdef CONFIG_PROVE_LOCKING
 256int cgroup_lock_is_held(void)
 257{
 258        return lockdep_is_held(&cgroup_mutex);
 259}
 260#else /* #ifdef CONFIG_PROVE_LOCKING */
 261int cgroup_lock_is_held(void)
 262{
 263        return mutex_is_locked(&cgroup_mutex);
 264}
 265#endif /* #else #ifdef CONFIG_PROVE_LOCKING */
 266
 267EXPORT_SYMBOL_GPL(cgroup_lock_is_held);
 268
 269static int css_unbias_refcnt(int refcnt)
 270{
 271        return refcnt >= 0 ? refcnt : refcnt - CSS_DEACT_BIAS;
 272}
 273
 274/* the current nr of refs, always >= 0 whether @css is deactivated or not */
 275static int css_refcnt(struct cgroup_subsys_state *css)
 276{
 277        int v = atomic_read(&css->refcnt);
 278
 279        return css_unbias_refcnt(v);
 280}
 281
 282/* convenient tests for these bits */
 283inline int cgroup_is_removed(const struct cgroup *cgrp)
 284{
 285        return test_bit(CGRP_REMOVED, &cgrp->flags);
 286}
 287
 288/* bits in struct cgroupfs_root flags field */
 289enum {
 290        ROOT_NOPREFIX,  /* mounted subsystems have no named prefix */
 291        ROOT_XATTR,     /* supports extended attributes */
 292};
 293
 294static int cgroup_is_releasable(const struct cgroup *cgrp)
 295{
 296        const int bits =
 297                (1 << CGRP_RELEASABLE) |
 298                (1 << CGRP_NOTIFY_ON_RELEASE);
 299        return (cgrp->flags & bits) == bits;
 300}
 301
 302static int notify_on_release(const struct cgroup *cgrp)
 303{
 304        return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
 305}
 306
 307/*
 308 * for_each_subsys() allows you to iterate on each subsystem attached to
 309 * an active hierarchy
 310 */
 311#define for_each_subsys(_root, _ss) \
 312list_for_each_entry(_ss, &_root->subsys_list, sibling)
 313
 314/* for_each_active_root() allows you to iterate across the active hierarchies */
 315#define for_each_active_root(_root) \
 316list_for_each_entry(_root, &roots, root_list)
 317
 318static inline struct cgroup *__d_cgrp(struct dentry *dentry)
 319{
 320        return dentry->d_fsdata;
 321}
 322
 323static inline struct cfent *__d_cfe(struct dentry *dentry)
 324{
 325        return dentry->d_fsdata;
 326}
 327
 328static inline struct cftype *__d_cft(struct dentry *dentry)
 329{
 330        return __d_cfe(dentry)->type;
 331}
 332
 333/* the list of cgroups eligible for automatic release. Protected by
 334 * release_list_lock */
 335static LIST_HEAD(release_list);
 336static DEFINE_RAW_SPINLOCK(release_list_lock);
 337static void cgroup_release_agent(struct work_struct *work);
 338static DECLARE_WORK(release_agent_work, cgroup_release_agent);
 339static void check_for_release(struct cgroup *cgrp);
 340
 341/* Link structure for associating css_set objects with cgroups */
 342struct cg_cgroup_link {
 343        /*
 344         * List running through cg_cgroup_links associated with a
 345         * cgroup, anchored on cgroup->css_sets
 346         */
 347        struct list_head cgrp_link_list;
 348        struct cgroup *cgrp;
 349        /*
 350         * List running through cg_cgroup_links pointing at a
 351         * single css_set object, anchored on css_set->cg_links
 352         */
 353        struct list_head cg_link_list;
 354        struct css_set *cg;
 355};
 356
 357/* The default css_set - used by init and its children prior to any
 358 * hierarchies being mounted. It contains a pointer to the root state
 359 * for each subsystem. Also used to anchor the list of css_sets. Not
 360 * reference-counted, to improve performance when child cgroups
 361 * haven't been created.
 362 */
 363
 364static struct css_set init_css_set;
 365static struct cg_cgroup_link init_css_set_link;
 366
 367static int cgroup_init_idr(struct cgroup_subsys *ss,
 368                           struct cgroup_subsys_state *css);
 369
 370/* css_set_lock protects the list of css_set objects, and the
 371 * chain of tasks off each css_set.  Nests outside task->alloc_lock
 372 * due to cgroup_iter_start() */
 373static DEFINE_RWLOCK(css_set_lock);
 374static int css_set_count;
 375
 376/*
 377 * hash table for cgroup groups. This improves the performance to find
 378 * an existing css_set. This hash doesn't (currently) take into
 379 * account cgroups in empty hierarchies.
 380 */
 381#define CSS_SET_HASH_BITS       7
 382static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
 383
 384static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
 385{
 386        int i;
 387        unsigned long key = 0UL;
 388
 389        for (i = 0; i < CGROUP_SUBSYS_COUNT; i++)
 390                key += (unsigned long)css[i];
 391        key = (key >> 16) ^ key;
 392
 393        return key;
 394}
 395
 396/* We don't maintain the lists running through each css_set to its
 397 * task until after the first call to cgroup_iter_start(). This
 398 * reduces the fork()/exit() overhead for people who have cgroups
 399 * compiled into their kernel but not actually in use */
 400static int use_task_css_set_links __read_mostly;
 401
 402static void __put_css_set(struct css_set *cg, int taskexit)
 403{
 404        struct cg_cgroup_link *link;
 405        struct cg_cgroup_link *saved_link;
 406        /*
 407         * Ensure that the refcount doesn't hit zero while any readers
 408         * can see it. Similar to atomic_dec_and_lock(), but for an
 409         * rwlock
 410         */
 411        if (atomic_add_unless(&cg->refcount, -1, 1))
 412                return;
 413        write_lock(&css_set_lock);
 414        if (!atomic_dec_and_test(&cg->refcount)) {
 415                write_unlock(&css_set_lock);
 416                return;
 417        }
 418
 419        /* This css_set is dead. unlink it and release cgroup refcounts */
 420        hash_del(&cg->hlist);
 421        css_set_count--;
 422
 423        list_for_each_entry_safe(link, saved_link, &cg->cg_links,
 424                                 cg_link_list) {
 425                struct cgroup *cgrp = link->cgrp;
 426                list_del(&link->cg_link_list);
 427                list_del(&link->cgrp_link_list);
 428
 429                /*
 430                 * We may not be holding cgroup_mutex, and if cgrp->count is
 431                 * dropped to 0 the cgroup can be destroyed at any time, hence
 432                 * rcu_read_lock is used to keep it alive.
 433                 */
 434                rcu_read_lock();
 435                if (atomic_dec_and_test(&cgrp->count) &&
 436                    notify_on_release(cgrp)) {
 437                        if (taskexit)
 438                                set_bit(CGRP_RELEASABLE, &cgrp->flags);
 439                        check_for_release(cgrp);
 440                }
 441                rcu_read_unlock();
 442
 443                kfree(link);
 444        }
 445
 446        write_unlock(&css_set_lock);
 447        kfree_rcu(cg, rcu_head);
 448}
 449
 450/*
 451 * refcounted get/put for css_set objects
 452 */
 453static inline void get_css_set(struct css_set *cg)
 454{
 455        atomic_inc(&cg->refcount);
 456}
 457
 458static inline void put_css_set(struct css_set *cg)
 459{
 460        __put_css_set(cg, 0);
 461}
 462
 463static inline void put_css_set_taskexit(struct css_set *cg)
 464{
 465        __put_css_set(cg, 1);
 466}
 467
 468/*
 469 * compare_css_sets - helper function for find_existing_css_set().
 470 * @cg: candidate css_set being tested
 471 * @old_cg: existing css_set for a task
 472 * @new_cgrp: cgroup that's being entered by the task
 473 * @template: desired set of css pointers in css_set (pre-calculated)
 474 *
 475 * Returns true if "cg" matches "old_cg" except for the hierarchy
 476 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
 477 */
 478static bool compare_css_sets(struct css_set *cg,
 479                             struct css_set *old_cg,
 480                             struct cgroup *new_cgrp,
 481                             struct cgroup_subsys_state *template[])
 482{
 483        struct list_head *l1, *l2;
 484
 485        if (memcmp(template, cg->subsys, sizeof(cg->subsys))) {
 486                /* Not all subsystems matched */
 487                return false;
 488        }
 489
 490        /*
 491         * Compare cgroup pointers in order to distinguish between
 492         * different cgroups in heirarchies with no subsystems. We
 493         * could get by with just this check alone (and skip the
 494         * memcmp above) but on most setups the memcmp check will
 495         * avoid the need for this more expensive check on almost all
 496         * candidates.
 497         */
 498
 499        l1 = &cg->cg_links;
 500        l2 = &old_cg->cg_links;
 501        while (1) {
 502                struct cg_cgroup_link *cgl1, *cgl2;
 503                struct cgroup *cg1, *cg2;
 504
 505                l1 = l1->next;
 506                l2 = l2->next;
 507                /* See if we reached the end - both lists are equal length. */
 508                if (l1 == &cg->cg_links) {
 509                        BUG_ON(l2 != &old_cg->cg_links);
 510                        break;
 511                } else {
 512                        BUG_ON(l2 == &old_cg->cg_links);
 513                }
 514                /* Locate the cgroups associated with these links. */
 515                cgl1 = list_entry(l1, struct cg_cgroup_link, cg_link_list);
 516                cgl2 = list_entry(l2, struct cg_cgroup_link, cg_link_list);
 517                cg1 = cgl1->cgrp;
 518                cg2 = cgl2->cgrp;
 519                /* Hierarchies should be linked in the same order. */
 520                BUG_ON(cg1->root != cg2->root);
 521
 522                /*
 523                 * If this hierarchy is the hierarchy of the cgroup
 524                 * that's changing, then we need to check that this
 525                 * css_set points to the new cgroup; if it's any other
 526                 * hierarchy, then this css_set should point to the
 527                 * same cgroup as the old css_set.
 528                 */
 529                if (cg1->root == new_cgrp->root) {
 530                        if (cg1 != new_cgrp)
 531                                return false;
 532                } else {
 533                        if (cg1 != cg2)
 534                                return false;
 535                }
 536        }
 537        return true;
 538}
 539
 540/*
 541 * find_existing_css_set() is a helper for
 542 * find_css_set(), and checks to see whether an existing
 543 * css_set is suitable.
 544 *
 545 * oldcg: the cgroup group that we're using before the cgroup
 546 * transition
 547 *
 548 * cgrp: the cgroup that we're moving into
 549 *
 550 * template: location in which to build the desired set of subsystem
 551 * state objects for the new cgroup group
 552 */
 553static struct css_set *find_existing_css_set(
 554        struct css_set *oldcg,
 555        struct cgroup *cgrp,
 556        struct cgroup_subsys_state *template[])
 557{
 558        int i;
 559        struct cgroupfs_root *root = cgrp->root;
 560        struct css_set *cg;
 561        unsigned long key;
 562
 563        /*
 564         * Build the set of subsystem state objects that we want to see in the
 565         * new css_set. while subsystems can change globally, the entries here
 566         * won't change, so no need for locking.
 567         */
 568        for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
 569                if (root->subsys_mask & (1UL << i)) {
 570                        /* Subsystem is in this hierarchy. So we want
 571                         * the subsystem state from the new
 572                         * cgroup */
 573                        template[i] = cgrp->subsys[i];
 574                } else {
 575                        /* Subsystem is not in this hierarchy, so we
 576                         * don't want to change the subsystem state */
 577                        template[i] = oldcg->subsys[i];
 578                }
 579        }
 580
 581        key = css_set_hash(template);
 582        hash_for_each_possible(css_set_table, cg, hlist, key) {
 583                if (!compare_css_sets(cg, oldcg, cgrp, template))
 584                        continue;
 585
 586                /* This css_set matches what we need */
 587                return cg;
 588        }
 589
 590        /* No existing cgroup group matched */
 591        return NULL;
 592}
 593
 594static void free_cg_links(struct list_head *tmp)
 595{
 596        struct cg_cgroup_link *link;
 597        struct cg_cgroup_link *saved_link;
 598
 599        list_for_each_entry_safe(link, saved_link, tmp, cgrp_link_list) {
 600                list_del(&link->cgrp_link_list);
 601                kfree(link);
 602        }
 603}
 604
 605/*
 606 * allocate_cg_links() allocates "count" cg_cgroup_link structures
 607 * and chains them on tmp through their cgrp_link_list fields. Returns 0 on
 608 * success or a negative error
 609 */
 610static int allocate_cg_links(int count, struct list_head *tmp)
 611{
 612        struct cg_cgroup_link *link;
 613        int i;
 614        INIT_LIST_HEAD(tmp);
 615        for (i = 0; i < count; i++) {
 616                link = kmalloc(sizeof(*link), GFP_KERNEL);
 617                if (!link) {
 618                        free_cg_links(tmp);
 619                        return -ENOMEM;
 620                }
 621                list_add(&link->cgrp_link_list, tmp);
 622        }
 623        return 0;
 624}
 625
 626/**
 627 * link_css_set - a helper function to link a css_set to a cgroup
 628 * @tmp_cg_links: cg_cgroup_link objects allocated by allocate_cg_links()
 629 * @cg: the css_set to be linked
 630 * @cgrp: the destination cgroup
 631 */
 632static void link_css_set(struct list_head *tmp_cg_links,
 633                         struct css_set *cg, struct cgroup *cgrp)
 634{
 635        struct cg_cgroup_link *link;
 636
 637        BUG_ON(list_empty(tmp_cg_links));
 638        link = list_first_entry(tmp_cg_links, struct cg_cgroup_link,
 639                                cgrp_link_list);
 640        link->cg = cg;
 641        link->cgrp = cgrp;
 642        atomic_inc(&cgrp->count);
 643        list_move(&link->cgrp_link_list, &cgrp->css_sets);
 644        /*
 645         * Always add links to the tail of the list so that the list
 646         * is sorted by order of hierarchy creation
 647         */
 648        list_add_tail(&link->cg_link_list, &cg->cg_links);
 649}
 650
 651/*
 652 * find_css_set() takes an existing cgroup group and a
 653 * cgroup object, and returns a css_set object that's
 654 * equivalent to the old group, but with the given cgroup
 655 * substituted into the appropriate hierarchy. Must be called with
 656 * cgroup_mutex held
 657 */
 658static struct css_set *find_css_set(
 659        struct css_set *oldcg, struct cgroup *cgrp)
 660{
 661        struct css_set *res;
 662        struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT];
 663
 664        struct list_head tmp_cg_links;
 665
 666        struct cg_cgroup_link *link;
 667        unsigned long key;
 668
 669        /* First see if we already have a cgroup group that matches
 670         * the desired set */
 671        read_lock(&css_set_lock);
 672        res = find_existing_css_set(oldcg, cgrp, template);
 673        if (res)
 674                get_css_set(res);
 675        read_unlock(&css_set_lock);
 676
 677        if (res)
 678                return res;
 679
 680        res = kmalloc(sizeof(*res), GFP_KERNEL);
 681        if (!res)
 682                return NULL;
 683
 684        /* Allocate all the cg_cgroup_link objects that we'll need */
 685        if (allocate_cg_links(root_count, &tmp_cg_links) < 0) {
 686                kfree(res);
 687                return NULL;
 688        }
 689
 690        atomic_set(&res->refcount, 1);
 691        INIT_LIST_HEAD(&res->cg_links);
 692        INIT_LIST_HEAD(&res->tasks);
 693        INIT_HLIST_NODE(&res->hlist);
 694
 695        /* Copy the set of subsystem state objects generated in
 696         * find_existing_css_set() */
 697        memcpy(res->subsys, template, sizeof(res->subsys));
 698
 699        write_lock(&css_set_lock);
 700        /* Add reference counts and links from the new css_set. */
 701        list_for_each_entry(link, &oldcg->cg_links, cg_link_list) {
 702                struct cgroup *c = link->cgrp;
 703                if (c->root == cgrp->root)
 704                        c = cgrp;
 705                link_css_set(&tmp_cg_links, res, c);
 706        }
 707
 708        BUG_ON(!list_empty(&tmp_cg_links));
 709
 710        css_set_count++;
 711
 712        /* Add this cgroup group to the hash table */
 713        key = css_set_hash(res->subsys);
 714        hash_add(css_set_table, &res->hlist, key);
 715
 716        write_unlock(&css_set_lock);
 717
 718        return res;
 719}
 720
 721/*
 722 * Return the cgroup for "task" from the given hierarchy. Must be
 723 * called with cgroup_mutex held.
 724 */
 725static struct cgroup *task_cgroup_from_root(struct task_struct *task,
 726                                            struct cgroupfs_root *root)
 727{
 728        struct css_set *css;
 729        struct cgroup *res = NULL;
 730
 731        BUG_ON(!mutex_is_locked(&cgroup_mutex));
 732        read_lock(&css_set_lock);
 733        /*
 734         * No need to lock the task - since we hold cgroup_mutex the
 735         * task can't change groups, so the only thing that can happen
 736         * is that it exits and its css is set back to init_css_set.
 737         */
 738        css = task->cgroups;
 739        if (css == &init_css_set) {
 740                res = &root->top_cgroup;
 741        } else {
 742                struct cg_cgroup_link *link;
 743                list_for_each_entry(link, &css->cg_links, cg_link_list) {
 744                        struct cgroup *c = link->cgrp;
 745                        if (c->root == root) {
 746                                res = c;
 747                                break;
 748                        }
 749                }
 750        }
 751        read_unlock(&css_set_lock);
 752        BUG_ON(!res);
 753        return res;
 754}
 755
 756/*
 757 * There is one global cgroup mutex. We also require taking
 758 * task_lock() when dereferencing a task's cgroup subsys pointers.
 759 * See "The task_lock() exception", at the end of this comment.
 760 *
 761 * A task must hold cgroup_mutex to modify cgroups.
 762 *
 763 * Any task can increment and decrement the count field without lock.
 764 * So in general, code holding cgroup_mutex can't rely on the count
 765 * field not changing.  However, if the count goes to zero, then only
 766 * cgroup_attach_task() can increment it again.  Because a count of zero
 767 * means that no tasks are currently attached, therefore there is no
 768 * way a task attached to that cgroup can fork (the other way to
 769 * increment the count).  So code holding cgroup_mutex can safely
 770 * assume that if the count is zero, it will stay zero. Similarly, if
 771 * a task holds cgroup_mutex on a cgroup with zero count, it
 772 * knows that the cgroup won't be removed, as cgroup_rmdir()
 773 * needs that mutex.
 774 *
 775 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
 776 * (usually) take cgroup_mutex.  These are the two most performance
 777 * critical pieces of code here.  The exception occurs on cgroup_exit(),
 778 * when a task in a notify_on_release cgroup exits.  Then cgroup_mutex
 779 * is taken, and if the cgroup count is zero, a usermode call made
 780 * to the release agent with the name of the cgroup (path relative to
 781 * the root of cgroup file system) as the argument.
 782 *
 783 * A cgroup can only be deleted if both its 'count' of using tasks
 784 * is zero, and its list of 'children' cgroups is empty.  Since all
 785 * tasks in the system use _some_ cgroup, and since there is always at
 786 * least one task in the system (init, pid == 1), therefore, top_cgroup
 787 * always has either children cgroups and/or using tasks.  So we don't
 788 * need a special hack to ensure that top_cgroup cannot be deleted.
 789 *
 790 *      The task_lock() exception
 791 *
 792 * The need for this exception arises from the action of
 793 * cgroup_attach_task(), which overwrites one task's cgroup pointer with
 794 * another.  It does so using cgroup_mutex, however there are
 795 * several performance critical places that need to reference
 796 * task->cgroup without the expense of grabbing a system global
 797 * mutex.  Therefore except as noted below, when dereferencing or, as
 798 * in cgroup_attach_task(), modifying a task's cgroup pointer we use
 799 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
 800 * the task_struct routinely used for such matters.
 801 *
 802 * P.S.  One more locking exception.  RCU is used to guard the
 803 * update of a tasks cgroup pointer by cgroup_attach_task()
 804 */
 805
 806/**
 807 * cgroup_lock - lock out any changes to cgroup structures
 808 *
 809 */
 810void cgroup_lock(void)
 811{
 812        mutex_lock(&cgroup_mutex);
 813}
 814EXPORT_SYMBOL_GPL(cgroup_lock);
 815
 816/**
 817 * cgroup_unlock - release lock on cgroup changes
 818 *
 819 * Undo the lock taken in a previous cgroup_lock() call.
 820 */
 821void cgroup_unlock(void)
 822{
 823        mutex_unlock(&cgroup_mutex);
 824}
 825EXPORT_SYMBOL_GPL(cgroup_unlock);
 826
 827/*
 828 * A couple of forward declarations required, due to cyclic reference loop:
 829 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
 830 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
 831 * -> cgroup_mkdir.
 832 */
 833
 834static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
 835static struct dentry *cgroup_lookup(struct inode *, struct dentry *, unsigned int);
 836static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
 837static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
 838                               unsigned long subsys_mask);
 839static const struct inode_operations cgroup_dir_inode_operations;
 840static const struct file_operations proc_cgroupstats_operations;
 841
 842static struct backing_dev_info cgroup_backing_dev_info = {
 843        .name           = "cgroup",
 844        .capabilities   = BDI_CAP_NO_ACCT_AND_WRITEBACK,
 845};
 846
 847static int alloc_css_id(struct cgroup_subsys *ss,
 848                        struct cgroup *parent, struct cgroup *child);
 849
 850static struct inode *cgroup_new_inode(umode_t mode, struct super_block *sb)
 851{
 852        struct inode *inode = new_inode(sb);
 853
 854        if (inode) {
 855                inode->i_ino = get_next_ino();
 856                inode->i_mode = mode;
 857                inode->i_uid = current_fsuid();
 858                inode->i_gid = current_fsgid();
 859                inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
 860                inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
 861        }
 862        return inode;
 863}
 864
 865static void cgroup_free_fn(struct work_struct *work)
 866{
 867        struct cgroup *cgrp = container_of(work, struct cgroup, free_work);
 868        struct cgroup_subsys *ss;
 869
 870        mutex_lock(&cgroup_mutex);
 871        /*
 872         * Release the subsystem state objects.
 873         */
 874        for_each_subsys(cgrp->root, ss)
 875                ss->css_free(cgrp);
 876
 877        cgrp->root->number_of_cgroups--;
 878        mutex_unlock(&cgroup_mutex);
 879
 880        /*
 881         * Drop the active superblock reference that we took when we
 882         * created the cgroup
 883         */
 884        deactivate_super(cgrp->root->sb);
 885
 886        /*
 887         * if we're getting rid of the cgroup, refcount should ensure
 888         * that there are no pidlists left.
 889         */
 890        BUG_ON(!list_empty(&cgrp->pidlists));
 891
 892        simple_xattrs_free(&cgrp->xattrs);
 893
 894        ida_simple_remove(&cgrp->root->cgroup_ida, cgrp->id);
 895        kfree(cgrp);
 896}
 897
 898static void cgroup_free_rcu(struct rcu_head *head)
 899{
 900        struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
 901
 902        schedule_work(&cgrp->free_work);
 903}
 904
 905static void cgroup_diput(struct dentry *dentry, struct inode *inode)
 906{
 907        /* is dentry a directory ? if so, kfree() associated cgroup */
 908        if (S_ISDIR(inode->i_mode)) {
 909                struct cgroup *cgrp = dentry->d_fsdata;
 910
 911                BUG_ON(!(cgroup_is_removed(cgrp)));
 912                call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
 913        } else {
 914                struct cfent *cfe = __d_cfe(dentry);
 915                struct cgroup *cgrp = dentry->d_parent->d_fsdata;
 916
 917                WARN_ONCE(!list_empty(&cfe->node) &&
 918                          cgrp != &cgrp->root->top_cgroup,
 919                          "cfe still linked for %s\n", cfe->type->name);
 920                simple_xattrs_free(&cfe->xattrs);
 921                kfree(cfe);
 922        }
 923        iput(inode);
 924}
 925
 926static int cgroup_delete(const struct dentry *d)
 927{
 928        return 1;
 929}
 930
 931static void remove_dir(struct dentry *d)
 932{
 933        struct dentry *parent = dget(d->d_parent);
 934
 935        d_delete(d);
 936        simple_rmdir(parent->d_inode, d);
 937        dput(parent);
 938}
 939
 940static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
 941{
 942        struct cfent *cfe;
 943
 944        lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
 945        lockdep_assert_held(&cgroup_mutex);
 946
 947        /*
 948         * If we're doing cleanup due to failure of cgroup_create(),
 949         * the corresponding @cfe may not exist.
 950         */
 951        list_for_each_entry(cfe, &cgrp->files, node) {
 952                struct dentry *d = cfe->dentry;
 953
 954                if (cft && cfe->type != cft)
 955                        continue;
 956
 957                dget(d);
 958                d_delete(d);
 959                simple_unlink(cgrp->dentry->d_inode, d);
 960                list_del_init(&cfe->node);
 961                dput(d);
 962
 963                break;
 964        }
 965}
 966
 967/**
 968 * cgroup_clear_directory - selective removal of base and subsystem files
 969 * @dir: directory containing the files
 970 * @base_files: true if the base files should be removed
 971 * @subsys_mask: mask of the subsystem ids whose files should be removed
 972 */
 973static void cgroup_clear_directory(struct dentry *dir, bool base_files,
 974                                   unsigned long subsys_mask)
 975{
 976        struct cgroup *cgrp = __d_cgrp(dir);
 977        struct cgroup_subsys *ss;
 978
 979        for_each_subsys(cgrp->root, ss) {
 980                struct cftype_set *set;
 981                if (!test_bit(ss->subsys_id, &subsys_mask))
 982                        continue;
 983                list_for_each_entry(set, &ss->cftsets, node)
 984                        cgroup_addrm_files(cgrp, NULL, set->cfts, false);
 985        }
 986        if (base_files) {
 987                while (!list_empty(&cgrp->files))
 988                        cgroup_rm_file(cgrp, NULL);
 989        }
 990}
 991
 992/*
 993 * NOTE : the dentry must have been dget()'ed
 994 */
 995static void cgroup_d_remove_dir(struct dentry *dentry)
 996{
 997        struct dentry *parent;
 998        struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
 999
1000        cgroup_clear_directory(dentry, true, root->subsys_mask);
1001
1002        parent = dentry->d_parent;
1003        spin_lock(&parent->d_lock);
1004        spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
1005        list_del_init(&dentry->d_u.d_child);
1006        spin_unlock(&dentry->d_lock);
1007        spin_unlock(&parent->d_lock);
1008        remove_dir(dentry);
1009}
1010
1011/*
1012 * Call with cgroup_mutex held. Drops reference counts on modules, including
1013 * any duplicate ones that parse_cgroupfs_options took. If this function
1014 * returns an error, no reference counts are touched.
1015 */
1016static int rebind_subsystems(struct cgroupfs_root *root,
1017                              unsigned long final_subsys_mask)
1018{
1019        unsigned long added_mask, removed_mask;
1020        struct cgroup *cgrp = &root->top_cgroup;
1021        int i;
1022
1023        BUG_ON(!mutex_is_locked(&cgroup_mutex));
1024        BUG_ON(!mutex_is_locked(&cgroup_root_mutex));
1025
1026        removed_mask = root->actual_subsys_mask & ~final_subsys_mask;
1027        added_mask = final_subsys_mask & ~root->actual_subsys_mask;
1028        /* Check that any added subsystems are currently free */
1029        for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1030                unsigned long bit = 1UL << i;
1031                struct cgroup_subsys *ss = subsys[i];
1032                if (!(bit & added_mask))
1033                        continue;
1034                /*
1035                 * Nobody should tell us to do a subsys that doesn't exist:
1036                 * parse_cgroupfs_options should catch that case and refcounts
1037                 * ensure that subsystems won't disappear once selected.
1038                 */
1039                BUG_ON(ss == NULL);
1040                if (ss->root != &rootnode) {
1041                        /* Subsystem isn't free */
1042                        return -EBUSY;
1043                }
1044        }
1045
1046        /* Currently we don't handle adding/removing subsystems when
1047         * any child cgroups exist. This is theoretically supportable
1048         * but involves complex error handling, so it's being left until
1049         * later */
1050        if (root->number_of_cgroups > 1)
1051                return -EBUSY;
1052
1053        /* Process each subsystem */
1054        for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1055                struct cgroup_subsys *ss = subsys[i];
1056                unsigned long bit = 1UL << i;
1057                if (bit & added_mask) {
1058                        /* We're binding this subsystem to this hierarchy */
1059                        BUG_ON(ss == NULL);
1060                        BUG_ON(cgrp->subsys[i]);
1061                        BUG_ON(!dummytop->subsys[i]);
1062                        BUG_ON(dummytop->subsys[i]->cgroup != dummytop);
1063                        cgrp->subsys[i] = dummytop->subsys[i];
1064                        cgrp->subsys[i]->cgroup = cgrp;
1065                        list_move(&ss->sibling, &root->subsys_list);
1066                        ss->root = root;
1067                        if (ss->bind)
1068                                ss->bind(cgrp);
1069                        /* refcount was already taken, and we're keeping it */
1070                } else if (bit & removed_mask) {
1071                        /* We're removing this subsystem */
1072                        BUG_ON(ss == NULL);
1073                        BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]);
1074                        BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
1075                        if (ss->bind)
1076                                ss->bind(dummytop);
1077                        dummytop->subsys[i]->cgroup = dummytop;
1078                        cgrp->subsys[i] = NULL;
1079                        subsys[i]->root = &rootnode;
1080                        list_move(&ss->sibling, &rootnode.subsys_list);
1081                        /* subsystem is now free - drop reference on module */
1082                        module_put(ss->module);
1083                } else if (bit & final_subsys_mask) {
1084                        /* Subsystem state should already exist */
1085                        BUG_ON(ss == NULL);
1086                        BUG_ON(!cgrp->subsys[i]);
1087                        /*
1088                         * a refcount was taken, but we already had one, so
1089                         * drop the extra reference.
1090                         */
1091                        module_put(ss->module);
1092#ifdef CONFIG_MODULE_UNLOAD
1093                        BUG_ON(ss->module && !module_refcount(ss->module));
1094#endif
1095                } else {
1096                        /* Subsystem state shouldn't exist */
1097                        BUG_ON(cgrp->subsys[i]);
1098                }
1099        }
1100        root->subsys_mask = root->actual_subsys_mask = final_subsys_mask;
1101
1102        return 0;
1103}
1104
1105static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry)
1106{
1107        struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
1108        struct cgroup_subsys *ss;
1109
1110        mutex_lock(&cgroup_root_mutex);
1111        for_each_subsys(root, ss)
1112                seq_printf(seq, ",%s", ss->name);
1113        if (test_bit(ROOT_NOPREFIX, &root->flags))
1114                seq_puts(seq, ",noprefix");
1115        if (test_bit(ROOT_XATTR, &root->flags))
1116                seq_puts(seq, ",xattr");
1117        if (strlen(root->release_agent_path))
1118                seq_printf(seq, ",release_agent=%s", root->release_agent_path);
1119        if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
1120                seq_puts(seq, ",clone_children");
1121        if (strlen(root->name))
1122                seq_printf(seq, ",name=%s", root->name);
1123        mutex_unlock(&cgroup_root_mutex);
1124        return 0;
1125}
1126
1127struct cgroup_sb_opts {
1128        unsigned long subsys_mask;
1129        unsigned long flags;
1130        char *release_agent;
1131        bool cpuset_clone_children;
1132        char *name;
1133        /* User explicitly requested empty subsystem */
1134        bool none;
1135
1136        struct cgroupfs_root *new_root;
1137
1138};
1139
1140/*
1141 * Convert a hierarchy specifier into a bitmask of subsystems and flags. Call
1142 * with cgroup_mutex held to protect the subsys[] array. This function takes
1143 * refcounts on subsystems to be used, unless it returns error, in which case
1144 * no refcounts are taken.
1145 */
1146static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
1147{
1148        char *token, *o = data;
1149        bool all_ss = false, one_ss = false;
1150        unsigned long mask = (unsigned long)-1;
1151        int i;
1152        bool module_pin_failed = false;
1153
1154        BUG_ON(!mutex_is_locked(&cgroup_mutex));
1155
1156#ifdef CONFIG_CPUSETS
1157        mask = ~(1UL << cpuset_subsys_id);
1158#endif
1159
1160        memset(opts, 0, sizeof(*opts));
1161
1162        while ((token = strsep(&o, ",")) != NULL) {
1163                if (!*token)
1164                        return -EINVAL;
1165                if (!strcmp(token, "none")) {
1166                        /* Explicitly have no subsystems */
1167                        opts->none = true;
1168                        continue;
1169                }
1170                if (!strcmp(token, "all")) {
1171                        /* Mutually exclusive option 'all' + subsystem name */
1172                        if (one_ss)
1173                                return -EINVAL;
1174                        all_ss = true;
1175                        continue;
1176                }
1177                if (!strcmp(token, "noprefix")) {
1178                        set_bit(ROOT_NOPREFIX, &opts->flags);
1179                        continue;
1180                }
1181                if (!strcmp(token, "clone_children")) {
1182                        opts->cpuset_clone_children = true;
1183                        continue;
1184                }
1185                if (!strcmp(token, "xattr")) {
1186                        set_bit(ROOT_XATTR, &opts->flags);
1187                        continue;
1188                }
1189                if (!strncmp(token, "release_agent=", 14)) {
1190                        /* Specifying two release agents is forbidden */
1191                        if (opts->release_agent)
1192                                return -EINVAL;
1193                        opts->release_agent =
1194                                kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
1195                        if (!opts->release_agent)
1196                                return -ENOMEM;
1197                        continue;
1198                }
1199                if (!strncmp(token, "name=", 5)) {
1200                        const char *name = token + 5;
1201                        /* Can't specify an empty name */
1202                        if (!strlen(name))
1203                                return -EINVAL;
1204                        /* Must match [\w.-]+ */
1205                        for (i = 0; i < strlen(name); i++) {
1206                                char c = name[i];
1207                                if (isalnum(c))
1208                                        continue;
1209                                if ((c == '.') || (c == '-') || (c == '_'))
1210                                        continue;
1211                                return -EINVAL;
1212                        }
1213                        /* Specifying two names is forbidden */
1214                        if (opts->name)
1215                                return -EINVAL;
1216                        opts->name = kstrndup(name,
1217                                              MAX_CGROUP_ROOT_NAMELEN - 1,
1218                                              GFP_KERNEL);
1219                        if (!opts->name)
1220                                return -ENOMEM;
1221
1222                        continue;
1223                }
1224
1225                for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1226                        struct cgroup_subsys *ss = subsys[i];
1227                        if (ss == NULL)
1228                                continue;
1229                        if (strcmp(token, ss->name))
1230                                continue;
1231                        if (ss->disabled)
1232                                continue;
1233
1234                        /* Mutually exclusive option 'all' + subsystem name */
1235                        if (all_ss)
1236                                return -EINVAL;
1237                        set_bit(i, &opts->subsys_mask);
1238                        one_ss = true;
1239
1240                        break;
1241                }
1242                if (i == CGROUP_SUBSYS_COUNT)
1243                        return -ENOENT;
1244        }
1245
1246        /*
1247         * If the 'all' option was specified select all the subsystems,
1248         * otherwise if 'none', 'name=' and a subsystem name options
1249         * were not specified, let's default to 'all'
1250         */
1251        if (all_ss || (!one_ss && !opts->none && !opts->name)) {
1252                for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1253                        struct cgroup_subsys *ss = subsys[i];
1254                        if (ss == NULL)
1255                                continue;
1256                        if (ss->disabled)
1257                                continue;
1258                        set_bit(i, &opts->subsys_mask);
1259                }
1260        }
1261
1262        /* Consistency checks */
1263
1264        /*
1265         * Option noprefix was introduced just for backward compatibility
1266         * with the old cpuset, so we allow noprefix only if mounting just
1267         * the cpuset subsystem.
1268         */
1269        if (test_bit(ROOT_NOPREFIX, &opts->flags) &&
1270            (opts->subsys_mask & mask))
1271                return -EINVAL;
1272
1273
1274        /* Can't specify "none" and some subsystems */
1275        if (opts->subsys_mask && opts->none)
1276                return -EINVAL;
1277
1278        /*
1279         * We either have to specify by name or by subsystems. (So all
1280         * empty hierarchies must have a name).
1281         */
1282        if (!opts->subsys_mask && !opts->name)
1283                return -EINVAL;
1284
1285        /*
1286         * Grab references on all the modules we'll need, so the subsystems
1287         * don't dance around before rebind_subsystems attaches them. This may
1288         * take duplicate reference counts on a subsystem that's already used,
1289         * but rebind_subsystems handles this case.
1290         */
1291        for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1292                unsigned long bit = 1UL << i;
1293
1294                if (!(bit & opts->subsys_mask))
1295                        continue;
1296                if (!try_module_get(subsys[i]->module)) {
1297                        module_pin_failed = true;
1298                        break;
1299                }
1300        }
1301        if (module_pin_failed) {
1302                /*
1303                 * oops, one of the modules was going away. this means that we
1304                 * raced with a module_delete call, and to the user this is
1305                 * essentially a "subsystem doesn't exist" case.
1306                 */
1307                for (i--; i >= 0; i--) {
1308                        /* drop refcounts only on the ones we took */
1309                        unsigned long bit = 1UL << i;
1310
1311                        if (!(bit & opts->subsys_mask))
1312                                continue;
1313                        module_put(subsys[i]->module);
1314                }
1315                return -ENOENT;
1316        }
1317
1318        return 0;
1319}
1320
1321static void drop_parsed_module_refcounts(unsigned long subsys_mask)
1322{
1323        int i;
1324        for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1325                unsigned long bit = 1UL << i;
1326
1327                if (!(bit & subsys_mask))
1328                        continue;
1329                module_put(subsys[i]->module);
1330        }
1331}
1332
1333static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1334{
1335        int ret = 0;
1336        struct cgroupfs_root *root = sb->s_fs_info;
1337        struct cgroup *cgrp = &root->top_cgroup;
1338        struct cgroup_sb_opts opts;
1339        unsigned long added_mask, removed_mask;
1340
1341        mutex_lock(&cgrp->dentry->d_inode->i_mutex);
1342        mutex_lock(&cgroup_mutex);
1343        mutex_lock(&cgroup_root_mutex);
1344
1345        /* See what subsystems are wanted */
1346        ret = parse_cgroupfs_options(data, &opts);
1347        if (ret)
1348                goto out_unlock;
1349
1350        if (opts.subsys_mask != root->actual_subsys_mask || opts.release_agent)
1351                pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1352                           task_tgid_nr(current), current->comm);
1353
1354        added_mask = opts.subsys_mask & ~root->subsys_mask;
1355        removed_mask = root->subsys_mask & ~opts.subsys_mask;
1356
1357        /* Don't allow flags or name to change at remount */
1358        if (opts.flags != root->flags ||
1359            (opts.name && strcmp(opts.name, root->name))) {
1360                ret = -EINVAL;
1361                drop_parsed_module_refcounts(opts.subsys_mask);
1362                goto out_unlock;
1363        }
1364
1365        /*
1366         * Clear out the files of subsystems that should be removed, do
1367         * this before rebind_subsystems, since rebind_subsystems may
1368         * change this hierarchy's subsys_list.
1369         */
1370        cgroup_clear_directory(cgrp->dentry, false, removed_mask);
1371
1372        ret = rebind_subsystems(root, opts.subsys_mask);
1373        if (ret) {
1374                /* rebind_subsystems failed, re-populate the removed files */
1375                cgroup_populate_dir(cgrp, false, removed_mask);
1376                drop_parsed_module_refcounts(opts.subsys_mask);
1377                goto out_unlock;
1378        }
1379
1380        /* re-populate subsystem files */
1381        cgroup_populate_dir(cgrp, false, added_mask);
1382
1383        if (opts.release_agent)
1384                strcpy(root->release_agent_path, opts.release_agent);
1385 out_unlock:
1386        kfree(opts.release_agent);
1387        kfree(opts.name);
1388        mutex_unlock(&cgroup_root_mutex);
1389        mutex_unlock(&cgroup_mutex);
1390        mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
1391        return ret;
1392}
1393
1394static const struct super_operations cgroup_ops = {
1395        .statfs = simple_statfs,
1396        .drop_inode = generic_delete_inode,
1397        .show_options = cgroup_show_options,
1398        .remount_fs = cgroup_remount,
1399};
1400
1401static void init_cgroup_housekeeping(struct cgroup *cgrp)
1402{
1403        INIT_LIST_HEAD(&cgrp->sibling);
1404        INIT_LIST_HEAD(&cgrp->children);
1405        INIT_LIST_HEAD(&cgrp->files);
1406        INIT_LIST_HEAD(&cgrp->css_sets);
1407        INIT_LIST_HEAD(&cgrp->allcg_node);
1408        INIT_LIST_HEAD(&cgrp->release_list);
1409        INIT_LIST_HEAD(&cgrp->pidlists);
1410        INIT_WORK(&cgrp->free_work, cgroup_free_fn);
1411        mutex_init(&cgrp->pidlist_mutex);
1412        INIT_LIST_HEAD(&cgrp->event_list);
1413        spin_lock_init(&cgrp->event_list_lock);
1414        simple_xattrs_init(&cgrp->xattrs);
1415}
1416
1417static void init_cgroup_root(struct cgroupfs_root *root)
1418{
1419        struct cgroup *cgrp = &root->top_cgroup;
1420
1421        INIT_LIST_HEAD(&root->subsys_list);
1422        INIT_LIST_HEAD(&root->root_list);
1423        INIT_LIST_HEAD(&root->allcg_list);
1424        root->number_of_cgroups = 1;
1425        cgrp->root = root;
1426        cgrp->top_cgroup = cgrp;
1427        init_cgroup_housekeeping(cgrp);
1428        list_add_tail(&cgrp->allcg_node, &root->allcg_list);
1429}
1430
1431static bool init_root_id(struct cgroupfs_root *root)
1432{
1433        int ret = 0;
1434
1435        do {
1436                if (!ida_pre_get(&hierarchy_ida, GFP_KERNEL))
1437                        return false;
1438                spin_lock(&hierarchy_id_lock);
1439                /* Try to allocate the next unused ID */
1440                ret = ida_get_new_above(&hierarchy_ida, next_hierarchy_id,
1441                                        &root->hierarchy_id);
1442                if (ret == -ENOSPC)
1443                        /* Try again starting from 0 */
1444                        ret = ida_get_new(&hierarchy_ida, &root->hierarchy_id);
1445                if (!ret) {
1446                        next_hierarchy_id = root->hierarchy_id + 1;
1447                } else if (ret != -EAGAIN) {
1448                        /* Can only get here if the 31-bit IDR is full ... */
1449                        BUG_ON(ret);
1450                }
1451                spin_unlock(&hierarchy_id_lock);
1452        } while (ret);
1453        return true;
1454}
1455
1456static int cgroup_test_super(struct super_block *sb, void *data)
1457{
1458        struct cgroup_sb_opts *opts = data;
1459        struct cgroupfs_root *root = sb->s_fs_info;
1460
1461        /* If we asked for a name then it must match */
1462        if (opts->name && strcmp(opts->name, root->name))
1463                return 0;
1464
1465        /*
1466         * If we asked for subsystems (or explicitly for no
1467         * subsystems) then they must match
1468         */
1469        if ((opts->subsys_mask || opts->none)
1470            && (opts->subsys_mask != root->subsys_mask))
1471                return 0;
1472
1473        return 1;
1474}
1475
1476static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1477{
1478        struct cgroupfs_root *root;
1479
1480        if (!opts->subsys_mask && !opts->none)
1481                return NULL;
1482
1483        root = kzalloc(sizeof(*root), GFP_KERNEL);
1484        if (!root)
1485                return ERR_PTR(-ENOMEM);
1486
1487        if (!init_root_id(root)) {
1488                kfree(root);
1489                return ERR_PTR(-ENOMEM);
1490        }
1491        init_cgroup_root(root);
1492
1493        root->subsys_mask = opts->subsys_mask;
1494        root->flags = opts->flags;
1495        ida_init(&root->cgroup_ida);
1496        if (opts->release_agent)
1497                strcpy(root->release_agent_path, opts->release_agent);
1498        if (opts->name)
1499                strcpy(root->name, opts->name);
1500        if (opts->cpuset_clone_children)
1501                set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
1502        return root;
1503}
1504
1505static void cgroup_drop_root(struct cgroupfs_root *root)
1506{
1507        if (!root)
1508                return;
1509
1510        BUG_ON(!root->hierarchy_id);
1511        spin_lock(&hierarchy_id_lock);
1512        ida_remove(&hierarchy_ida, root->hierarchy_id);
1513        spin_unlock(&hierarchy_id_lock);
1514        ida_destroy(&root->cgroup_ida);
1515        kfree(root);
1516}
1517
1518static int cgroup_set_super(struct super_block *sb, void *data)
1519{
1520        int ret;
1521        struct cgroup_sb_opts *opts = data;
1522
1523        /* If we don't have a new root, we can't set up a new sb */
1524        if (!opts->new_root)
1525                return -EINVAL;
1526
1527        BUG_ON(!opts->subsys_mask && !opts->none);
1528
1529        ret = set_anon_super(sb, NULL);
1530        if (ret)
1531                return ret;
1532
1533        sb->s_fs_info = opts->new_root;
1534        opts->new_root->sb = sb;
1535
1536        sb->s_blocksize = PAGE_CACHE_SIZE;
1537        sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1538        sb->s_magic = CGROUP_SUPER_MAGIC;
1539        sb->s_op = &cgroup_ops;
1540
1541        return 0;
1542}
1543
1544static int cgroup_get_rootdir(struct super_block *sb)
1545{
1546        static const struct dentry_operations cgroup_dops = {
1547                .d_iput = cgroup_diput,
1548                .d_delete = cgroup_delete,
1549        };
1550
1551        struct inode *inode =
1552                cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
1553
1554        if (!inode)
1555                return -ENOMEM;
1556
1557        inode->i_fop = &simple_dir_operations;
1558        inode->i_op = &cgroup_dir_inode_operations;
1559        /* directories start off with i_nlink == 2 (for "." entry) */
1560        inc_nlink(inode);
1561        sb->s_root = d_make_root(inode);
1562        if (!sb->s_root)
1563                return -ENOMEM;
1564        /* for everything else we want ->d_op set */
1565        sb->s_d_op = &cgroup_dops;
1566        return 0;
1567}
1568
1569static struct dentry *cgroup_mount(struct file_system_type *fs_type,
1570                         int flags, const char *unused_dev_name,
1571                         void *data)
1572{
1573        struct cgroup_sb_opts opts;
1574        struct cgroupfs_root *root;
1575        int ret = 0;
1576        struct super_block *sb;
1577        struct cgroupfs_root *new_root;
1578        struct inode *inode;
1579
1580        /* First find the desired set of subsystems */
1581        mutex_lock(&cgroup_mutex);
1582        ret = parse_cgroupfs_options(data, &opts);
1583        mutex_unlock(&cgroup_mutex);
1584        if (ret)
1585                goto out_err;
1586
1587        /*
1588         * Allocate a new cgroup root. We may not need it if we're
1589         * reusing an existing hierarchy.
1590         */
1591        new_root = cgroup_root_from_opts(&opts);
1592        if (IS_ERR(new_root)) {
1593                ret = PTR_ERR(new_root);
1594                goto drop_modules;
1595        }
1596        opts.new_root = new_root;
1597
1598        /* Locate an existing or new sb for this hierarchy */
1599        sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts);
1600        if (IS_ERR(sb)) {
1601                ret = PTR_ERR(sb);
1602                cgroup_drop_root(opts.new_root);
1603                goto drop_modules;
1604        }
1605
1606        root = sb->s_fs_info;
1607        BUG_ON(!root);
1608        if (root == opts.new_root) {
1609                /* We used the new root structure, so this is a new hierarchy */
1610                struct list_head tmp_cg_links;
1611                struct cgroup *root_cgrp = &root->top_cgroup;
1612                struct cgroupfs_root *existing_root;
1613                const struct cred *cred;
1614                int i;
1615                struct css_set *cg;
1616
1617                BUG_ON(sb->s_root != NULL);
1618
1619                ret = cgroup_get_rootdir(sb);
1620                if (ret)
1621                        goto drop_new_super;
1622                inode = sb->s_root->d_inode;
1623
1624                mutex_lock(&inode->i_mutex);
1625                mutex_lock(&cgroup_mutex);
1626                mutex_lock(&cgroup_root_mutex);
1627
1628                /* Check for name clashes with existing mounts */
1629                ret = -EBUSY;
1630                if (strlen(root->name))
1631                        for_each_active_root(existing_root)
1632                                if (!strcmp(existing_root->name, root->name))
1633                                        goto unlock_drop;
1634
1635                /*
1636                 * We're accessing css_set_count without locking
1637                 * css_set_lock here, but that's OK - it can only be
1638                 * increased by someone holding cgroup_lock, and
1639                 * that's us. The worst that can happen is that we
1640                 * have some link structures left over
1641                 */
1642                ret = allocate_cg_links(css_set_count, &tmp_cg_links);
1643                if (ret)
1644                        goto unlock_drop;
1645
1646                ret = rebind_subsystems(root, root->subsys_mask);
1647                if (ret == -EBUSY) {
1648                        free_cg_links(&tmp_cg_links);
1649                        goto unlock_drop;
1650                }
1651                /*
1652                 * There must be no failure case after here, since rebinding
1653                 * takes care of subsystems' refcounts, which are explicitly
1654                 * dropped in the failure exit path.
1655                 */
1656
1657                /* EBUSY should be the only error here */
1658                BUG_ON(ret);
1659
1660                list_add(&root->root_list, &roots);
1661                root_count++;
1662
1663                sb->s_root->d_fsdata = root_cgrp;
1664                root->top_cgroup.dentry = sb->s_root;
1665
1666                /* Link the top cgroup in this hierarchy into all
1667                 * the css_set objects */
1668                write_lock(&css_set_lock);
1669                hash_for_each(css_set_table, i, cg, hlist)
1670                        link_css_set(&tmp_cg_links, cg, root_cgrp);
1671                write_unlock(&css_set_lock);
1672
1673                free_cg_links(&tmp_cg_links);
1674
1675                BUG_ON(!list_empty(&root_cgrp->children));
1676                BUG_ON(root->number_of_cgroups != 1);
1677
1678                cred = override_creds(&init_cred);
1679                cgroup_populate_dir(root_cgrp, true, root->subsys_mask);
1680                revert_creds(cred);
1681                mutex_unlock(&cgroup_root_mutex);
1682                mutex_unlock(&cgroup_mutex);
1683                mutex_unlock(&inode->i_mutex);
1684        } else {
1685                /*
1686                 * We re-used an existing hierarchy - the new root (if
1687                 * any) is not needed
1688                 */
1689                cgroup_drop_root(opts.new_root);
1690                /* no subsys rebinding, so refcounts don't change */
1691                drop_parsed_module_refcounts(opts.subsys_mask);
1692        }
1693
1694        kfree(opts.release_agent);
1695        kfree(opts.name);
1696        return dget(sb->s_root);
1697
1698 unlock_drop:
1699        mutex_unlock(&cgroup_root_mutex);
1700        mutex_unlock(&cgroup_mutex);
1701        mutex_unlock(&inode->i_mutex);
1702 drop_new_super:
1703        deactivate_locked_super(sb);
1704 drop_modules:
1705        drop_parsed_module_refcounts(opts.subsys_mask);
1706 out_err:
1707        kfree(opts.release_agent);
1708        kfree(opts.name);
1709        return ERR_PTR(ret);
1710}
1711
1712static void cgroup_kill_sb(struct super_block *sb) {
1713        struct cgroupfs_root *root = sb->s_fs_info;
1714        struct cgroup *cgrp = &root->top_cgroup;
1715        int ret;
1716        struct cg_cgroup_link *link;
1717        struct cg_cgroup_link *saved_link;
1718
1719        BUG_ON(!root);
1720
1721        BUG_ON(root->number_of_cgroups != 1);
1722        BUG_ON(!list_empty(&cgrp->children));
1723
1724        mutex_lock(&cgroup_mutex);
1725        mutex_lock(&cgroup_root_mutex);
1726
1727        /* Rebind all subsystems back to the default hierarchy */
1728        ret = rebind_subsystems(root, 0);
1729        /* Shouldn't be able to fail ... */
1730        BUG_ON(ret);
1731
1732        /*
1733         * Release all the links from css_sets to this hierarchy's
1734         * root cgroup
1735         */
1736        write_lock(&css_set_lock);
1737
1738        list_for_each_entry_safe(link, saved_link, &cgrp->css_sets,
1739                                 cgrp_link_list) {
1740                list_del(&link->cg_link_list);
1741                list_del(&link->cgrp_link_list);
1742                kfree(link);
1743        }
1744        write_unlock(&css_set_lock);
1745
1746        if (!list_empty(&root->root_list)) {
1747                list_del(&root->root_list);
1748                root_count--;
1749        }
1750
1751        mutex_unlock(&cgroup_root_mutex);
1752        mutex_unlock(&cgroup_mutex);
1753
1754        simple_xattrs_free(&cgrp->xattrs);
1755
1756        kill_litter_super(sb);
1757        cgroup_drop_root(root);
1758}
1759
1760static struct file_system_type cgroup_fs_type = {
1761        .name = "cgroup",
1762        .mount = cgroup_mount,
1763        .kill_sb = cgroup_kill_sb,
1764};
1765
1766static struct kobject *cgroup_kobj;
1767
1768/**
1769 * cgroup_path - generate the path of a cgroup
1770 * @cgrp: the cgroup in question
1771 * @buf: the buffer to write the path into
1772 * @buflen: the length of the buffer
1773 *
1774 * Called with cgroup_mutex held or else with an RCU-protected cgroup
1775 * reference.  Writes path of cgroup into buf.  Returns 0 on success,
1776 * -errno on error.
1777 */
1778int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
1779{
1780        struct dentry *dentry = cgrp->dentry;
1781        char *start;
1782
1783        rcu_lockdep_assert(rcu_read_lock_held() || cgroup_lock_is_held(),
1784                           "cgroup_path() called without proper locking");
1785
1786        if (cgrp == dummytop) {
1787                /*
1788                 * Inactive subsystems have no dentry for their root
1789                 * cgroup
1790                 */
1791                strcpy(buf, "/");
1792                return 0;
1793        }
1794
1795        start = buf + buflen - 1;
1796
1797        *start = '\0';
1798        for (;;) {
1799                int len = dentry->d_name.len;
1800
1801                if ((start -= len) < buf)
1802                        return -ENAMETOOLONG;
1803                memcpy(start, dentry->d_name.name, len);
1804                cgrp = cgrp->parent;
1805                if (!cgrp)
1806                        break;
1807
1808                dentry = cgrp->dentry;
1809                if (!cgrp->parent)
1810                        continue;
1811                if (--start < buf)
1812                        return -ENAMETOOLONG;
1813                *start = '/';
1814        }
1815        memmove(buf, start, buf + buflen - start);
1816        return 0;
1817}
1818EXPORT_SYMBOL_GPL(cgroup_path);
1819
1820/*
1821 * Control Group taskset
1822 */
1823struct task_and_cgroup {
1824        struct task_struct      *task;
1825        struct cgroup           *cgrp;
1826        struct css_set          *cg;
1827};
1828
1829struct cgroup_taskset {
1830        struct task_and_cgroup  single;
1831        struct flex_array       *tc_array;
1832        int                     tc_array_len;
1833        int                     idx;
1834        struct cgroup           *cur_cgrp;
1835};
1836
1837/**
1838 * cgroup_taskset_first - reset taskset and return the first task
1839 * @tset: taskset of interest
1840 *
1841 * @tset iteration is initialized and the first task is returned.
1842 */
1843struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1844{
1845        if (tset->tc_array) {
1846                tset->idx = 0;
1847                return cgroup_taskset_next(tset);
1848        } else {
1849                tset->cur_cgrp = tset->single.cgrp;
1850                return tset->single.task;
1851        }
1852}
1853EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1854
1855/**
1856 * cgroup_taskset_next - iterate to the next task in taskset
1857 * @tset: taskset of interest
1858 *
1859 * Return the next task in @tset.  Iteration must have been initialized
1860 * with cgroup_taskset_first().
1861 */
1862struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1863{
1864        struct task_and_cgroup *tc;
1865
1866        if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1867                return NULL;
1868
1869        tc = flex_array_get(tset->tc_array, tset->idx++);
1870        tset->cur_cgrp = tc->cgrp;
1871        return tc->task;
1872}
1873EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1874
1875/**
1876 * cgroup_taskset_cur_cgroup - return the matching cgroup for the current task
1877 * @tset: taskset of interest
1878 *
1879 * Return the cgroup for the current (last returned) task of @tset.  This
1880 * function must be preceded by either cgroup_taskset_first() or
1881 * cgroup_taskset_next().
1882 */
1883struct cgroup *cgroup_taskset_cur_cgroup(struct cgroup_taskset *tset)
1884{
1885        return tset->cur_cgrp;
1886}
1887EXPORT_SYMBOL_GPL(cgroup_taskset_cur_cgroup);
1888
1889/**
1890 * cgroup_taskset_size - return the number of tasks in taskset
1891 * @tset: taskset of interest
1892 */
1893int cgroup_taskset_size(struct cgroup_taskset *tset)
1894{
1895        return tset->tc_array ? tset->tc_array_len : 1;
1896}
1897EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1898
1899
1900/*
1901 * cgroup_task_migrate - move a task from one cgroup to another.
1902 *
1903 * Must be called with cgroup_mutex and threadgroup locked.
1904 */
1905static void cgroup_task_migrate(struct cgroup *cgrp, struct cgroup *oldcgrp,
1906                                struct task_struct *tsk, struct css_set *newcg)
1907{
1908        struct css_set *oldcg;
1909
1910        /*
1911         * We are synchronized through threadgroup_lock() against PF_EXITING
1912         * setting such that we can't race against cgroup_exit() changing the
1913         * css_set to init_css_set and dropping the old one.
1914         */
1915        WARN_ON_ONCE(tsk->flags & PF_EXITING);
1916        oldcg = tsk->cgroups;
1917
1918        task_lock(tsk);
1919        rcu_assign_pointer(tsk->cgroups, newcg);
1920        task_unlock(tsk);
1921
1922        /* Update the css_set linked lists if we're using them */
1923        write_lock(&css_set_lock);
1924        if (!list_empty(&tsk->cg_list))
1925                list_move(&tsk->cg_list, &newcg->tasks);
1926        write_unlock(&css_set_lock);
1927
1928        /*
1929         * We just gained a reference on oldcg by taking it from the task. As
1930         * trading it for newcg is protected by cgroup_mutex, we're safe to drop
1931         * it here; it will be freed under RCU.
1932         */
1933        set_bit(CGRP_RELEASABLE, &oldcgrp->flags);
1934        put_css_set(oldcg);
1935}
1936
1937/**
1938 * cgroup_attach_task - attach task 'tsk' to cgroup 'cgrp'
1939 * @cgrp: the cgroup the task is attaching to
1940 * @tsk: the task to be attached
1941 *
1942 * Call with cgroup_mutex and threadgroup locked. May take task_lock of
1943 * @tsk during call.
1944 */
1945int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
1946{
1947        int retval = 0;
1948        struct cgroup_subsys *ss, *failed_ss = NULL;
1949        struct cgroup *oldcgrp;
1950        struct cgroupfs_root *root = cgrp->root;
1951        struct cgroup_taskset tset = { };
1952        struct css_set *newcg;
1953
1954        /* @tsk either already exited or can't exit until the end */
1955        if (tsk->flags & PF_EXITING)
1956                return -ESRCH;
1957
1958        /* Nothing to do if the task is already in that cgroup */
1959        oldcgrp = task_cgroup_from_root(tsk, root);
1960        if (cgrp == oldcgrp)
1961                return 0;
1962
1963        tset.single.task = tsk;
1964        tset.single.cgrp = oldcgrp;
1965
1966        for_each_subsys(root, ss) {
1967                if (ss->can_attach) {
1968                        retval = ss->can_attach(cgrp, &tset);
1969                        if (retval) {
1970                                /*
1971                                 * Remember on which subsystem the can_attach()
1972                                 * failed, so that we only call cancel_attach()
1973                                 * against the subsystems whose can_attach()
1974                                 * succeeded. (See below)
1975                                 */
1976                                failed_ss = ss;
1977                                goto out;
1978                        }
1979                }
1980        }
1981
1982        newcg = find_css_set(tsk->cgroups, cgrp);
1983        if (!newcg) {
1984                retval = -ENOMEM;
1985                goto out;
1986        }
1987
1988        cgroup_task_migrate(cgrp, oldcgrp, tsk, newcg);
1989
1990        for_each_subsys(root, ss) {
1991                if (ss->attach)
1992                        ss->attach(cgrp, &tset);
1993        }
1994
1995out:
1996        if (retval) {
1997                for_each_subsys(root, ss) {
1998                        if (ss == failed_ss)
1999                                /*
2000                                 * This subsystem was the one that failed the
2001                                 * can_attach() check earlier, so we don't need
2002                                 * to call cancel_attach() against it or any
2003                                 * remaining subsystems.
2004                                 */
2005                                break;
2006                        if (ss->cancel_attach)
2007                                ss->cancel_attach(cgrp, &tset);
2008                }
2009        }
2010        return retval;
2011}
2012
2013/**
2014 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2015 * @from: attach to all cgroups of a given task
2016 * @tsk: the task to be attached
2017 */
2018int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2019{
2020        struct cgroupfs_root *root;
2021        int retval = 0;
2022
2023        cgroup_lock();
2024        for_each_active_root(root) {
2025                struct cgroup *from_cg = task_cgroup_from_root(from, root);
2026
2027                retval = cgroup_attach_task(from_cg, tsk);
2028                if (retval)
2029                        break;
2030        }
2031        cgroup_unlock();
2032
2033        return retval;
2034}
2035EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2036
2037/**
2038 * cgroup_attach_proc - attach all threads in a threadgroup to a cgroup
2039 * @cgrp: the cgroup to attach to
2040 * @leader: the threadgroup leader task_struct of the group to be attached
2041 *
2042 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
2043 * task_lock of each thread in leader's threadgroup individually in turn.
2044 */
2045static int cgroup_attach_proc(struct cgroup *cgrp, struct task_struct *leader)
2046{
2047        int retval, i, group_size;
2048        struct cgroup_subsys *ss, *failed_ss = NULL;
2049        /* guaranteed to be initialized later, but the compiler needs this */
2050        struct cgroupfs_root *root = cgrp->root;
2051        /* threadgroup list cursor and array */
2052        struct task_struct *tsk;
2053        struct task_and_cgroup *tc;
2054        struct flex_array *group;
2055        struct cgroup_taskset tset = { };
2056
2057        /*
2058         * step 0: in order to do expensive, possibly blocking operations for
2059         * every thread, we cannot iterate the thread group list, since it needs
2060         * rcu or tasklist locked. instead, build an array of all threads in the
2061         * group - group_rwsem prevents new threads from appearing, and if
2062         * threads exit, this will just be an over-estimate.
2063         */
2064        group_size = get_nr_threads(leader);
2065        /* flex_array supports very large thread-groups better than kmalloc. */
2066        group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
2067        if (!group)
2068                return -ENOMEM;
2069        /* pre-allocate to guarantee space while iterating in rcu read-side. */
2070        retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL);
2071        if (retval)
2072                goto out_free_group_list;
2073
2074        tsk = leader;
2075        i = 0;
2076        /*
2077         * Prevent freeing of tasks while we take a snapshot. Tasks that are
2078         * already PF_EXITING could be freed from underneath us unless we
2079         * take an rcu_read_lock.
2080         */
2081        rcu_read_lock();
2082        do {
2083                struct task_and_cgroup ent;
2084
2085                /* @tsk either already exited or can't exit until the end */
2086                if (tsk->flags & PF_EXITING)
2087                        continue;
2088
2089                /* as per above, nr_threads may decrease, but not increase. */
2090                BUG_ON(i >= group_size);
2091                ent.task = tsk;
2092                ent.cgrp = task_cgroup_from_root(tsk, root);
2093                /* nothing to do if this task is already in the cgroup */
2094                if (ent.cgrp == cgrp)
2095                        continue;
2096                /*
2097                 * saying GFP_ATOMIC has no effect here because we did prealloc
2098                 * earlier, but it's good form to communicate our expectations.
2099                 */
2100                retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
2101                BUG_ON(retval != 0);
2102                i++;
2103        } while_each_thread(leader, tsk);
2104        rcu_read_unlock();
2105        /* remember the number of threads in the array for later. */
2106        group_size = i;
2107        tset.tc_array = group;
2108        tset.tc_array_len = group_size;
2109
2110        /* methods shouldn't be called if no task is actually migrating */
2111        retval = 0;
2112        if (!group_size)
2113                goto out_free_group_list;
2114
2115        /*
2116         * step 1: check that we can legitimately attach to the cgroup.
2117         */
2118        for_each_subsys(root, ss) {
2119                if (ss->can_attach) {
2120                        retval = ss->can_attach(cgrp, &tset);
2121                        if (retval) {
2122                                failed_ss = ss;
2123                                goto out_cancel_attach;
2124                        }
2125                }
2126        }
2127
2128        /*
2129         * step 2: make sure css_sets exist for all threads to be migrated.
2130         * we use find_css_set, which allocates a new one if necessary.
2131         */
2132        for (i = 0; i < group_size; i++) {
2133                tc = flex_array_get(group, i);
2134                tc->cg = find_css_set(tc->task->cgroups, cgrp);
2135                if (!tc->cg) {
2136                        retval = -ENOMEM;
2137                        goto out_put_css_set_refs;
2138                }
2139        }
2140
2141        /*
2142         * step 3: now that we're guaranteed success wrt the css_sets,
2143         * proceed to move all tasks to the new cgroup.  There are no
2144         * failure cases after here, so this is the commit point.
2145         */
2146        for (i = 0; i < group_size; i++) {
2147                tc = flex_array_get(group, i);
2148                cgroup_task_migrate(cgrp, tc->cgrp, tc->task, tc->cg);
2149        }
2150        /* nothing is sensitive to fork() after this point. */
2151
2152        /*
2153         * step 4: do subsystem attach callbacks.
2154         */
2155        for_each_subsys(root, ss) {
2156                if (ss->attach)
2157                        ss->attach(cgrp, &tset);
2158        }
2159
2160        /*
2161         * step 5: success! and cleanup
2162         */
2163        retval = 0;
2164out_put_css_set_refs:
2165        if (retval) {
2166                for (i = 0; i < group_size; i++) {
2167                        tc = flex_array_get(group, i);
2168                        if (!tc->cg)
2169                                break;
2170                        put_css_set(tc->cg);
2171                }
2172        }
2173out_cancel_attach:
2174        if (retval) {
2175                for_each_subsys(root, ss) {
2176                        if (ss == failed_ss)
2177                                break;
2178                        if (ss->cancel_attach)
2179                                ss->cancel_attach(cgrp, &tset);
2180                }
2181        }
2182out_free_group_list:
2183        flex_array_free(group);
2184        return retval;
2185}
2186
2187/*
2188 * Find the task_struct of the task to attach by vpid and pass it along to the
2189 * function to attach either it or all tasks in its threadgroup. Will lock
2190 * cgroup_mutex and threadgroup; may take task_lock of task.
2191 */
2192static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
2193{
2194        struct task_struct *tsk;
2195        const struct cred *cred = current_cred(), *tcred;
2196        int ret;
2197
2198        if (!cgroup_lock_live_group(cgrp))
2199                return -ENODEV;
2200
2201retry_find_task:
2202        rcu_read_lock();
2203        if (pid) {
2204                tsk = find_task_by_vpid(pid);
2205                if (!tsk) {
2206                        rcu_read_unlock();
2207                        ret= -ESRCH;
2208                        goto out_unlock_cgroup;
2209                }
2210                /*
2211                 * even if we're attaching all tasks in the thread group, we
2212                 * only need to check permissions on one of them.
2213                 */
2214                tcred = __task_cred(tsk);
2215                if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2216                    !uid_eq(cred->euid, tcred->uid) &&
2217                    !uid_eq(cred->euid, tcred->suid)) {
2218                        rcu_read_unlock();
2219                        ret = -EACCES;
2220                        goto out_unlock_cgroup;
2221                }
2222        } else
2223                tsk = current;
2224
2225        if (threadgroup)
2226                tsk = tsk->group_leader;
2227
2228        /*
2229         * Workqueue threads may acquire PF_THREAD_BOUND and become
2230         * trapped in a cpuset, or RT worker may be born in a cgroup
2231         * with no rt_runtime allocated.  Just say no.
2232         */
2233        if (tsk == kthreadd_task || (tsk->flags & PF_THREAD_BOUND)) {
2234                ret = -EINVAL;
2235                rcu_read_unlock();
2236                goto out_unlock_cgroup;
2237        }
2238
2239        get_task_struct(tsk);
2240        rcu_read_unlock();
2241
2242        threadgroup_lock(tsk);
2243        if (threadgroup) {
2244                if (!thread_group_leader(tsk)) {
2245                        /*
2246                         * a race with de_thread from another thread's exec()
2247                         * may strip us of our leadership, if this happens,
2248                         * there is no choice but to throw this task away and
2249                         * try again; this is
2250                         * "double-double-toil-and-trouble-check locking".
2251                         */
2252                        threadgroup_unlock(tsk);
2253                        put_task_struct(tsk);
2254                        goto retry_find_task;
2255                }
2256                ret = cgroup_attach_proc(cgrp, tsk);
2257        } else
2258                ret = cgroup_attach_task(cgrp, tsk);
2259        threadgroup_unlock(tsk);
2260
2261        put_task_struct(tsk);
2262out_unlock_cgroup:
2263        cgroup_unlock();
2264        return ret;
2265}
2266
2267static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
2268{
2269        return attach_task_by_pid(cgrp, pid, false);
2270}
2271
2272static int cgroup_procs_write(struct cgroup *cgrp, struct cftype *cft, u64 tgid)
2273{
2274        return attach_task_by_pid(cgrp, tgid, true);
2275}
2276
2277/**
2278 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
2279 * @cgrp: the cgroup to be checked for liveness
2280 *
2281 * On success, returns true; the lock should be later released with
2282 * cgroup_unlock(). On failure returns false with no lock held.
2283 */
2284bool cgroup_lock_live_group(struct cgroup *cgrp)
2285{
2286        mutex_lock(&cgroup_mutex);
2287        if (cgroup_is_removed(cgrp)) {
2288                mutex_unlock(&cgroup_mutex);
2289                return false;
2290        }
2291        return true;
2292}
2293EXPORT_SYMBOL_GPL(cgroup_lock_live_group);
2294
2295static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
2296                                      const char *buffer)
2297{
2298        BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
2299        if (strlen(buffer) >= PATH_MAX)
2300                return -EINVAL;
2301        if (!cgroup_lock_live_group(cgrp))
2302                return -ENODEV;
2303        mutex_lock(&cgroup_root_mutex);
2304        strcpy(cgrp->root->release_agent_path, buffer);
2305        mutex_unlock(&cgroup_root_mutex);
2306        cgroup_unlock();
2307        return 0;
2308}
2309
2310static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
2311                                     struct seq_file *seq)
2312{
2313        if (!cgroup_lock_live_group(cgrp))
2314                return -ENODEV;
2315        seq_puts(seq, cgrp->root->release_agent_path);
2316        seq_putc(seq, '\n');
2317        cgroup_unlock();
2318        return 0;
2319}
2320
2321/* A buffer size big enough for numbers or short strings */
2322#define CGROUP_LOCAL_BUFFER_SIZE 64
2323
2324static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
2325                                struct file *file,
2326                                const char __user *userbuf,
2327                                size_t nbytes, loff_t *unused_ppos)
2328{
2329        char buffer[CGROUP_LOCAL_BUFFER_SIZE];
2330        int retval = 0;
2331        char *end;
2332
2333        if (!nbytes)
2334                return -EINVAL;
2335        if (nbytes >= sizeof(buffer))
2336                return -E2BIG;
2337        if (copy_from_user(buffer, userbuf, nbytes))
2338                return -EFAULT;
2339
2340        buffer[nbytes] = 0;     /* nul-terminate */
2341        if (cft->write_u64) {
2342                u64 val = simple_strtoull(strstrip(buffer), &end, 0);
2343                if (*end)
2344                        return -EINVAL;
2345                retval = cft->write_u64(cgrp, cft, val);
2346        } else {
2347                s64 val = simple_strtoll(strstrip(buffer), &end, 0);
2348                if (*end)
2349                        return -EINVAL;
2350                retval = cft->write_s64(cgrp, cft, val);
2351        }
2352        if (!retval)
2353                retval = nbytes;
2354        return retval;
2355}
2356
2357static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
2358                                   struct file *file,
2359                                   const char __user *userbuf,
2360                                   size_t nbytes, loff_t *unused_ppos)
2361{
2362        char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
2363        int retval = 0;
2364        size_t max_bytes = cft->max_write_len;
2365        char *buffer = local_buffer;
2366
2367        if (!max_bytes)
2368                max_bytes = sizeof(local_buffer) - 1;
2369        if (nbytes >= max_bytes)
2370                return -E2BIG;
2371        /* Allocate a dynamic buffer if we need one */
2372        if (nbytes >= sizeof(local_buffer)) {
2373                buffer = kmalloc(nbytes + 1, GFP_KERNEL);
2374                if (buffer == NULL)
2375                        return -ENOMEM;
2376        }
2377        if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
2378                retval = -EFAULT;
2379                goto out;
2380        }
2381
2382        buffer[nbytes] = 0;     /* nul-terminate */
2383        retval = cft->write_string(cgrp, cft, strstrip(buffer));
2384        if (!retval)
2385                retval = nbytes;
2386out:
2387        if (buffer != local_buffer)
2388                kfree(buffer);
2389        return retval;
2390}
2391
2392static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
2393                                                size_t nbytes, loff_t *ppos)
2394{
2395        struct cftype *cft = __d_cft(file->f_dentry);
2396        struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
2397
2398        if (cgroup_is_removed(cgrp))
2399                return -ENODEV;
2400        if (cft->write)
2401                return cft->write(cgrp, cft, file, buf, nbytes, ppos);
2402        if (cft->write_u64 || cft->write_s64)
2403                return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
2404        if (cft->write_string)
2405                return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
2406        if (cft->trigger) {
2407                int ret = cft->trigger(cgrp, (unsigned int)cft->private);
2408                return ret ? ret : nbytes;
2409        }
2410        return -EINVAL;
2411}
2412
2413static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
2414                               struct file *file,
2415                               char __user *buf, size_t nbytes,
2416                               loff_t *ppos)
2417{
2418        char tmp[CGROUP_LOCAL_BUFFER_SIZE];
2419        u64 val = cft->read_u64(cgrp, cft);
2420        int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
2421
2422        return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2423}
2424
2425static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
2426                               struct file *file,
2427                               char __user *buf, size_t nbytes,
2428                               loff_t *ppos)
2429{
2430        char tmp[CGROUP_LOCAL_BUFFER_SIZE];
2431        s64 val = cft->read_s64(cgrp, cft);
2432        int len = sprintf(tmp, "%lld\n", (long long) val);
2433
2434        return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2435}
2436
2437static ssize_t cgroup_file_read(struct file *file, char __user *buf,
2438                                   size_t nbytes, loff_t *ppos)
2439{
2440        struct cftype *cft = __d_cft(file->f_dentry);
2441        struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
2442
2443        if (cgroup_is_removed(cgrp))
2444                return -ENODEV;
2445
2446        if (cft->read)
2447                return cft->read(cgrp, cft, file, buf, nbytes, ppos);
2448        if (cft->read_u64)
2449                return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
2450        if (cft->read_s64)
2451                return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
2452        return -EINVAL;
2453}
2454
2455/*
2456 * seqfile ops/methods for returning structured data. Currently just
2457 * supports string->u64 maps, but can be extended in future.
2458 */
2459
2460struct cgroup_seqfile_state {
2461        struct cftype *cft;
2462        struct cgroup *cgroup;
2463};
2464
2465static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
2466{
2467        struct seq_file *sf = cb->state;
2468        return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
2469}
2470
2471static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2472{
2473        struct cgroup_seqfile_state *state = m->private;
2474        struct cftype *cft = state->cft;
2475        if (cft->read_map) {
2476                struct cgroup_map_cb cb = {
2477                        .fill = cgroup_map_add,
2478                        .state = m,
2479                };
2480                return cft->read_map(state->cgroup, cft, &cb);
2481        }
2482        return cft->read_seq_string(state->cgroup, cft, m);
2483}
2484
2485static int cgroup_seqfile_release(struct inode *inode, struct file *file)
2486{
2487        struct seq_file *seq = file->private_data;
2488        kfree(seq->private);
2489        return single_release(inode, file);
2490}
2491
2492static const struct file_operations cgroup_seqfile_operations = {
2493        .read = seq_read,
2494        .write = cgroup_file_write,
2495        .llseek = seq_lseek,
2496        .release = cgroup_seqfile_release,
2497};
2498
2499static int cgroup_file_open(struct inode *inode, struct file *file)
2500{
2501        int err;
2502        struct cftype *cft;
2503
2504        err = generic_file_open(inode, file);
2505        if (err)
2506                return err;
2507        cft = __d_cft(file->f_dentry);
2508
2509        if (cft->read_map || cft->read_seq_string) {
2510                struct cgroup_seqfile_state *state =
2511                        kzalloc(sizeof(*state), GFP_USER);
2512                if (!state)
2513                        return -ENOMEM;
2514                state->cft = cft;
2515                state->cgroup = __d_cgrp(file->f_dentry->d_parent);
2516                file->f_op = &cgroup_seqfile_operations;
2517                err = single_open(file, cgroup_seqfile_show, state);
2518                if (err < 0)
2519                        kfree(state);
2520        } else if (cft->open)
2521                err = cft->open(inode, file);
2522        else
2523                err = 0;
2524
2525        return err;
2526}
2527
2528static int cgroup_file_release(struct inode *inode, struct file *file)
2529{
2530        struct cftype *cft = __d_cft(file->f_dentry);
2531        if (cft->release)
2532                return cft->release(inode, file);
2533        return 0;
2534}
2535
2536/*
2537 * cgroup_rename - Only allow simple rename of directories in place.
2538 */
2539static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2540                            struct inode *new_dir, struct dentry *new_dentry)
2541{
2542        if (!S_ISDIR(old_dentry->d_inode->i_mode))
2543                return -ENOTDIR;
2544        if (new_dentry->d_inode)
2545                return -EEXIST;
2546        if (old_dir != new_dir)
2547                return -EIO;
2548        return simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2549}
2550
2551static struct simple_xattrs *__d_xattrs(struct dentry *dentry)
2552{
2553        if (S_ISDIR(dentry->d_inode->i_mode))
2554                return &__d_cgrp(dentry)->xattrs;
2555        else
2556                return &__d_cfe(dentry)->xattrs;
2557}
2558
2559static inline int xattr_enabled(struct dentry *dentry)
2560{
2561        struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
2562        return test_bit(ROOT_XATTR, &root->flags);
2563}
2564
2565static bool is_valid_xattr(const char *name)
2566{
2567        if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
2568            !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
2569                return true;
2570        return false;
2571}
2572
2573static int cgroup_setxattr(struct dentry *dentry, const char *name,
2574                           const void *val, size_t size, int flags)
2575{
2576        if (!xattr_enabled(dentry))
2577                return -EOPNOTSUPP;
2578        if (!is_valid_xattr(name))
2579                return -EINVAL;
2580        return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags);
2581}
2582
2583static int cgroup_removexattr(struct dentry *dentry, const char *name)
2584{
2585        if (!xattr_enabled(dentry))
2586                return -EOPNOTSUPP;
2587        if (!is_valid_xattr(name))
2588                return -EINVAL;
2589        return simple_xattr_remove(__d_xattrs(dentry), name);
2590}
2591
2592static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name,
2593                               void *buf, size_t size)
2594{
2595        if (!xattr_enabled(dentry))
2596                return -EOPNOTSUPP;
2597        if (!is_valid_xattr(name))
2598                return -EINVAL;
2599        return simple_xattr_get(__d_xattrs(dentry), name, buf, size);
2600}
2601
2602static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size)
2603{
2604        if (!xattr_enabled(dentry))
2605                return -EOPNOTSUPP;
2606        return simple_xattr_list(__d_xattrs(dentry), buf, size);
2607}
2608
2609static const struct file_operations cgroup_file_operations = {
2610        .read = cgroup_file_read,
2611        .write = cgroup_file_write,
2612        .llseek = generic_file_llseek,
2613        .open = cgroup_file_open,
2614        .release = cgroup_file_release,
2615};
2616
2617static const struct inode_operations cgroup_file_inode_operations = {
2618        .setxattr = cgroup_setxattr,
2619        .getxattr = cgroup_getxattr,
2620        .listxattr = cgroup_listxattr,
2621        .removexattr = cgroup_removexattr,
2622};
2623
2624static const struct inode_operations cgroup_dir_inode_operations = {
2625        .lookup = cgroup_lookup,
2626        .mkdir = cgroup_mkdir,
2627        .rmdir = cgroup_rmdir,
2628        .rename = cgroup_rename,
2629        .setxattr = cgroup_setxattr,
2630        .getxattr = cgroup_getxattr,
2631        .listxattr = cgroup_listxattr,
2632        .removexattr = cgroup_removexattr,
2633};
2634
2635static struct dentry *cgroup_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2636{
2637        if (dentry->d_name.len > NAME_MAX)
2638                return ERR_PTR(-ENAMETOOLONG);
2639        d_add(dentry, NULL);
2640        return NULL;
2641}
2642
2643/*
2644 * Check if a file is a control file
2645 */
2646static inline struct cftype *__file_cft(struct file *file)
2647{
2648        if (file_inode(file)->i_fop != &cgroup_file_operations)
2649                return ERR_PTR(-EINVAL);
2650        return __d_cft(file->f_dentry);
2651}
2652
2653static int cgroup_create_file(struct dentry *dentry, umode_t mode,
2654                                struct super_block *sb)
2655{
2656        struct inode *inode;
2657
2658        if (!dentry)
2659                return -ENOENT;
2660        if (dentry->d_inode)
2661                return -EEXIST;
2662
2663        inode = cgroup_new_inode(mode, sb);
2664        if (!inode)
2665                return -ENOMEM;
2666
2667        if (S_ISDIR(mode)) {
2668                inode->i_op = &cgroup_dir_inode_operations;
2669                inode->i_fop = &simple_dir_operations;
2670
2671                /* start off with i_nlink == 2 (for "." entry) */
2672                inc_nlink(inode);
2673                inc_nlink(dentry->d_parent->d_inode);
2674
2675                /*
2676                 * Control reaches here with cgroup_mutex held.
2677                 * @inode->i_mutex should nest outside cgroup_mutex but we
2678                 * want to populate it immediately without releasing
2679                 * cgroup_mutex.  As @inode isn't visible to anyone else
2680                 * yet, trylock will always succeed without affecting
2681                 * lockdep checks.
2682                 */
2683                WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex));
2684        } else if (S_ISREG(mode)) {
2685                inode->i_size = 0;
2686                inode->i_fop = &cgroup_file_operations;
2687                inode->i_op = &cgroup_file_inode_operations;
2688        }
2689        d_instantiate(dentry, inode);
2690        dget(dentry);   /* Extra count - pin the dentry in core */
2691        return 0;
2692}
2693
2694/**
2695 * cgroup_file_mode - deduce file mode of a control file
2696 * @cft: the control file in question
2697 *
2698 * returns cft->mode if ->mode is not 0
2699 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2700 * returns S_IRUGO if it has only a read handler
2701 * returns S_IWUSR if it has only a write hander
2702 */
2703static umode_t cgroup_file_mode(const struct cftype *cft)
2704{
2705        umode_t mode = 0;
2706
2707        if (cft->mode)
2708                return cft->mode;
2709
2710        if (cft->read || cft->read_u64 || cft->read_s64 ||
2711            cft->read_map || cft->read_seq_string)
2712                mode |= S_IRUGO;
2713
2714        if (cft->write || cft->write_u64 || cft->write_s64 ||
2715            cft->write_string || cft->trigger)
2716                mode |= S_IWUSR;
2717
2718        return mode;
2719}
2720
2721static int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys,
2722                           struct cftype *cft)
2723{
2724        struct dentry *dir = cgrp->dentry;
2725        struct cgroup *parent = __d_cgrp(dir);
2726        struct dentry *dentry;
2727        struct cfent *cfe;
2728        int error;
2729        umode_t mode;
2730        char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
2731
2732        if (subsys && !test_bit(ROOT_NOPREFIX, &cgrp->root->flags)) {
2733                strcpy(name, subsys->name);
2734                strcat(name, ".");
2735        }
2736        strcat(name, cft->name);
2737
2738        BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
2739
2740        cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
2741        if (!cfe)
2742                return -ENOMEM;
2743
2744        dentry = lookup_one_len(name, dir, strlen(name));
2745        if (IS_ERR(dentry)) {
2746                error = PTR_ERR(dentry);
2747                goto out;
2748        }
2749
2750        cfe->type = (void *)cft;
2751        cfe->dentry = dentry;
2752        dentry->d_fsdata = cfe;
2753        simple_xattrs_init(&cfe->xattrs);
2754
2755        mode = cgroup_file_mode(cft);
2756        error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
2757        if (!error) {
2758                list_add_tail(&cfe->node, &parent->files);
2759                cfe = NULL;
2760        }
2761        dput(dentry);
2762out:
2763        kfree(cfe);
2764        return error;
2765}
2766
2767static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys,
2768                              struct cftype cfts[], bool is_add)
2769{
2770        struct cftype *cft;
2771        int err, ret = 0;
2772
2773        for (cft = cfts; cft->name[0] != '\0'; cft++) {
2774                /* does cft->flags tell us to skip this file on @cgrp? */
2775                if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2776                        continue;
2777                if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2778                        continue;
2779
2780                if (is_add) {
2781                        err = cgroup_add_file(cgrp, subsys, cft);
2782                        if (err)
2783                                pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
2784                                        cft->name, err);
2785                        ret = err;
2786                } else {
2787                        cgroup_rm_file(cgrp, cft);
2788                }
2789        }
2790        return ret;
2791}
2792
2793static DEFINE_MUTEX(cgroup_cft_mutex);
2794
2795static void cgroup_cfts_prepare(void)
2796        __acquires(&cgroup_cft_mutex) __acquires(&cgroup_mutex)
2797{
2798        /*
2799         * Thanks to the entanglement with vfs inode locking, we can't walk
2800         * the existing cgroups under cgroup_mutex and create files.
2801         * Instead, we increment reference on all cgroups and build list of
2802         * them using @cgrp->cft_q_node.  Grab cgroup_cft_mutex to ensure
2803         * exclusive access to the field.
2804         */
2805        mutex_lock(&cgroup_cft_mutex);
2806        mutex_lock(&cgroup_mutex);
2807}
2808
2809static void cgroup_cfts_commit(struct cgroup_subsys *ss,
2810                               struct cftype *cfts, bool is_add)
2811        __releases(&cgroup_mutex) __releases(&cgroup_cft_mutex)
2812{
2813        LIST_HEAD(pending);
2814        struct cgroup *cgrp, *n;
2815
2816        /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
2817        if (cfts && ss->root != &rootnode) {
2818                list_for_each_entry(cgrp, &ss->root->allcg_list, allcg_node) {
2819                        dget(cgrp->dentry);
2820                        list_add_tail(&cgrp->cft_q_node, &pending);
2821                }
2822        }
2823
2824        mutex_unlock(&cgroup_mutex);
2825
2826        /*
2827         * All new cgroups will see @cfts update on @ss->cftsets.  Add/rm
2828         * files for all cgroups which were created before.
2829         */
2830        list_for_each_entry_safe(cgrp, n, &pending, cft_q_node) {
2831                struct inode *inode = cgrp->dentry->d_inode;
2832
2833                mutex_lock(&inode->i_mutex);
2834                mutex_lock(&cgroup_mutex);
2835                if (!cgroup_is_removed(cgrp))
2836                        cgroup_addrm_files(cgrp, ss, cfts, is_add);
2837                mutex_unlock(&cgroup_mutex);
2838                mutex_unlock(&inode->i_mutex);
2839
2840                list_del_init(&cgrp->cft_q_node);
2841                dput(cgrp->dentry);
2842        }
2843
2844        mutex_unlock(&cgroup_cft_mutex);
2845}
2846
2847/**
2848 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2849 * @ss: target cgroup subsystem
2850 * @cfts: zero-length name terminated array of cftypes
2851 *
2852 * Register @cfts to @ss.  Files described by @cfts are created for all
2853 * existing cgroups to which @ss is attached and all future cgroups will
2854 * have them too.  This function can be called anytime whether @ss is
2855 * attached or not.
2856 *
2857 * Returns 0 on successful registration, -errno on failure.  Note that this
2858 * function currently returns 0 as long as @cfts registration is successful
2859 * even if some file creation attempts on existing cgroups fail.
2860 */
2861int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
2862{
2863        struct cftype_set *set;
2864
2865        set = kzalloc(sizeof(*set), GFP_KERNEL);
2866        if (!set)
2867                return -ENOMEM;
2868
2869        cgroup_cfts_prepare();
2870        set->cfts = cfts;
2871        list_add_tail(&set->node, &ss->cftsets);
2872        cgroup_cfts_commit(ss, cfts, true);
2873
2874        return 0;
2875}
2876EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2877
2878/**
2879 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
2880 * @ss: target cgroup subsystem
2881 * @cfts: zero-length name terminated array of cftypes
2882 *
2883 * Unregister @cfts from @ss.  Files described by @cfts are removed from
2884 * all existing cgroups to which @ss is attached and all future cgroups
2885 * won't have them either.  This function can be called anytime whether @ss
2886 * is attached or not.
2887 *
2888 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
2889 * registered with @ss.
2890 */
2891int cgroup_rm_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
2892{
2893        struct cftype_set *set;
2894
2895        cgroup_cfts_prepare();
2896
2897        list_for_each_entry(set, &ss->cftsets, node) {
2898                if (set->cfts == cfts) {
2899                        list_del_init(&set->node);
2900                        cgroup_cfts_commit(ss, cfts, false);
2901                        return 0;
2902                }
2903        }
2904
2905        cgroup_cfts_commit(ss, NULL, false);
2906        return -ENOENT;
2907}
2908
2909/**
2910 * cgroup_task_count - count the number of tasks in a cgroup.
2911 * @cgrp: the cgroup in question
2912 *
2913 * Return the number of tasks in the cgroup.
2914 */
2915int cgroup_task_count(const struct cgroup *cgrp)
2916{
2917        int count = 0;
2918        struct cg_cgroup_link *link;
2919
2920        read_lock(&css_set_lock);
2921        list_for_each_entry(link, &cgrp->css_sets, cgrp_link_list) {
2922                count += atomic_read(&link->cg->refcount);
2923        }
2924        read_unlock(&css_set_lock);
2925        return count;
2926}
2927
2928/*
2929 * Advance a list_head iterator.  The iterator should be positioned at
2930 * the start of a css_set
2931 */
2932static void cgroup_advance_iter(struct cgroup *cgrp,
2933                                struct cgroup_iter *it)
2934{
2935        struct list_head *l = it->cg_link;
2936        struct cg_cgroup_link *link;
2937        struct css_set *cg;
2938
2939        /* Advance to the next non-empty css_set */
2940        do {
2941                l = l->next;
2942                if (l == &cgrp->css_sets) {
2943                        it->cg_link = NULL;
2944                        return;
2945                }
2946                link = list_entry(l, struct cg_cgroup_link, cgrp_link_list);
2947                cg = link->cg;
2948        } while (list_empty(&cg->tasks));
2949        it->cg_link = l;
2950        it->task = cg->tasks.next;
2951}
2952
2953/*
2954 * To reduce the fork() overhead for systems that are not actually
2955 * using their cgroups capability, we don't maintain the lists running
2956 * through each css_set to its tasks until we see the list actually
2957 * used - in other words after the first call to cgroup_iter_start().
2958 */
2959static void cgroup_enable_task_cg_lists(void)
2960{
2961        struct task_struct *p, *g;
2962        write_lock(&css_set_lock);
2963        use_task_css_set_links = 1;
2964        /*
2965         * We need tasklist_lock because RCU is not safe against
2966         * while_each_thread(). Besides, a forking task that has passed
2967         * cgroup_post_fork() without seeing use_task_css_set_links = 1
2968         * is not guaranteed to have its child immediately visible in the
2969         * tasklist if we walk through it with RCU.
2970         */
2971        read_lock(&tasklist_lock);
2972        do_each_thread(g, p) {
2973                task_lock(p);
2974                /*
2975                 * We should check if the process is exiting, otherwise
2976                 * it will race with cgroup_exit() in that the list
2977                 * entry won't be deleted though the process has exited.
2978                 */
2979                if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
2980                        list_add(&p->cg_list, &p->cgroups->tasks);
2981                task_unlock(p);
2982        } while_each_thread(g, p);
2983        read_unlock(&tasklist_lock);
2984        write_unlock(&css_set_lock);
2985}
2986
2987/**
2988 * cgroup_next_descendant_pre - find the next descendant for pre-order walk
2989 * @pos: the current position (%NULL to initiate traversal)
2990 * @cgroup: cgroup whose descendants to walk
2991 *
2992 * To be used by cgroup_for_each_descendant_pre().  Find the next
2993 * descendant to visit for pre-order traversal of @cgroup's descendants.
2994 */
2995struct cgroup *cgroup_next_descendant_pre(struct cgroup *pos,
2996                                          struct cgroup *cgroup)
2997{
2998        struct cgroup *next;
2999
3000        WARN_ON_ONCE(!rcu_read_lock_held());
3001
3002        /* if first iteration, pretend we just visited @cgroup */
3003        if (!pos)
3004                pos = cgroup;
3005
3006        /* visit the first child if exists */
3007        next = list_first_or_null_rcu(&pos->children, struct cgroup, sibling);
3008        if (next)
3009                return next;
3010
3011        /* no child, visit my or the closest ancestor's next sibling */
3012        while (pos != cgroup) {
3013                next = list_entry_rcu(pos->sibling.next, struct cgroup,
3014                                      sibling);
3015                if (&next->sibling != &pos->parent->children)
3016                        return next;
3017
3018                pos = pos->parent;
3019        }
3020
3021        return NULL;
3022}
3023EXPORT_SYMBOL_GPL(cgroup_next_descendant_pre);
3024
3025/**
3026 * cgroup_rightmost_descendant - return the rightmost descendant of a cgroup
3027 * @pos: cgroup of interest
3028 *
3029 * Return the rightmost descendant of @pos.  If there's no descendant,
3030 * @pos is returned.  This can be used during pre-order traversal to skip
3031 * subtree of @pos.
3032 */
3033struct cgroup *cgroup_rightmost_descendant(struct cgroup *pos)
3034{
3035        struct cgroup *last, *tmp;
3036
3037        WARN_ON_ONCE(!rcu_read_lock_held());
3038
3039        do {
3040                last = pos;
3041                /* ->prev isn't RCU safe, walk ->next till the end */
3042                pos = NULL;
3043                list_for_each_entry_rcu(tmp, &last->children, sibling)
3044                        pos = tmp;
3045        } while (pos);
3046
3047        return last;
3048}
3049EXPORT_SYMBOL_GPL(cgroup_rightmost_descendant);
3050
3051static struct cgroup *cgroup_leftmost_descendant(struct cgroup *pos)
3052{
3053        struct cgroup *last;
3054
3055        do {
3056                last = pos;
3057                pos = list_first_or_null_rcu(&pos->children, struct cgroup,
3058                                             sibling);
3059        } while (pos);
3060
3061        return last;
3062}
3063
3064/**
3065 * cgroup_next_descendant_post - find the next descendant for post-order walk
3066 * @pos: the current position (%NULL to initiate traversal)
3067 * @cgroup: cgroup whose descendants to walk
3068 *
3069 * To be used by cgroup_for_each_descendant_post().  Find the next
3070 * descendant to visit for post-order traversal of @cgroup's descendants.
3071 */
3072struct cgroup *cgroup_next_descendant_post(struct cgroup *pos,
3073                                           struct cgroup *cgroup)
3074{
3075        struct cgroup *next;
3076
3077        WARN_ON_ONCE(!rcu_read_lock_held());
3078
3079        /* if first iteration, visit the leftmost descendant */
3080        if (!pos) {
3081                next = cgroup_leftmost_descendant(cgroup);
3082                return next != cgroup ? next : NULL;
3083        }
3084
3085        /* if there's an unvisited sibling, visit its leftmost descendant */
3086        next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
3087        if (&next->sibling != &pos->parent->children)
3088                return cgroup_leftmost_descendant(next);
3089
3090        /* no sibling left, visit parent */
3091        next = pos->parent;
3092        return next != cgroup ? next : NULL;
3093}
3094EXPORT_SYMBOL_GPL(cgroup_next_descendant_post);
3095
3096void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
3097        __acquires(css_set_lock)
3098{
3099        /*
3100         * The first time anyone tries to iterate across a cgroup,
3101         * we need to enable the list linking each css_set to its
3102         * tasks, and fix up all existing tasks.
3103         */
3104        if (!use_task_css_set_links)
3105                cgroup_enable_task_cg_lists();
3106
3107        read_lock(&css_set_lock);
3108        it->cg_link = &cgrp->css_sets;
3109        cgroup_advance_iter(cgrp, it);
3110}
3111
3112struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
3113                                        struct cgroup_iter *it)
3114{
3115        struct task_struct *res;
3116        struct list_head *l = it->task;
3117        struct cg_cgroup_link *link;
3118
3119        /* If the iterator cg is NULL, we have no tasks */
3120        if (!it->cg_link)
3121                return NULL;
3122        res = list_entry(l, struct task_struct, cg_list);
3123        /* Advance iterator to find next entry */
3124        l = l->next;
3125        link = list_entry(it->cg_link, struct cg_cgroup_link, cgrp_link_list);
3126        if (l == &link->cg->tasks) {
3127                /* We reached the end of this task list - move on to
3128                 * the next cg_cgroup_link */
3129                cgroup_advance_iter(cgrp, it);
3130        } else {
3131                it->task = l;
3132        }
3133        return res;
3134}
3135
3136void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
3137        __releases(css_set_lock)
3138{
3139        read_unlock(&css_set_lock);
3140}
3141
3142static inline int started_after_time(struct task_struct *t1,
3143                                     struct timespec *time,
3144                                     struct task_struct *t2)
3145{
3146        int start_diff = timespec_compare(&t1->start_time, time);
3147        if (start_diff > 0) {
3148                return 1;
3149        } else if (start_diff < 0) {
3150                return 0;
3151        } else {
3152                /*
3153                 * Arbitrarily, if two processes started at the same
3154                 * time, we'll say that the lower pointer value
3155                 * started first. Note that t2 may have exited by now
3156                 * so this may not be a valid pointer any longer, but
3157                 * that's fine - it still serves to distinguish
3158                 * between two tasks started (effectively) simultaneously.
3159                 */
3160                return t1 > t2;
3161        }
3162}
3163
3164/*
3165 * This function is a callback from heap_insert() and is used to order
3166 * the heap.
3167 * In this case we order the heap in descending task start time.
3168 */
3169static inline int started_after(void *p1, void *p2)
3170{
3171        struct task_struct *t1 = p1;
3172        struct task_struct *t2 = p2;
3173        return started_after_time(t1, &t2->start_time, t2);
3174}
3175
3176/**
3177 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
3178 * @scan: struct cgroup_scanner containing arguments for the scan
3179 *
3180 * Arguments include pointers to callback functions test_task() and
3181 * process_task().
3182 * Iterate through all the tasks in a cgroup, calling test_task() for each,
3183 * and if it returns true, call process_task() for it also.
3184 * The test_task pointer may be NULL, meaning always true (select all tasks).
3185 * Effectively duplicates cgroup_iter_{start,next,end}()
3186 * but does not lock css_set_lock for the call to process_task().
3187 * The struct cgroup_scanner may be embedded in any structure of the caller's
3188 * creation.
3189 * It is guaranteed that process_task() will act on every task that
3190 * is a member of the cgroup for the duration of this call. This
3191 * function may or may not call process_task() for tasks that exit
3192 * or move to a different cgroup during the call, or are forked or
3193 * move into the cgroup during the call.
3194 *
3195 * Note that test_task() may be called with locks held, and may in some
3196 * situations be called multiple times for the same task, so it should
3197 * be cheap.
3198 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
3199 * pre-allocated and will be used for heap operations (and its "gt" member will
3200 * be overwritten), else a temporary heap will be used (allocation of which
3201 * may cause this function to fail).
3202 */
3203int cgroup_scan_tasks(struct cgroup_scanner *scan)
3204{
3205        int retval, i;
3206        struct cgroup_iter it;
3207        struct task_struct *p, *dropped;
3208        /* Never dereference latest_task, since it's not refcounted */
3209        struct task_struct *latest_task = NULL;
3210        struct ptr_heap tmp_heap;
3211        struct ptr_heap *heap;
3212        struct timespec latest_time = { 0, 0 };
3213
3214        if (scan->heap) {
3215                /* The caller supplied our heap and pre-allocated its memory */
3216                heap = scan->heap;
3217                heap->gt = &started_after;
3218        } else {
3219                /* We need to allocate our own heap memory */
3220                heap = &tmp_heap;
3221                retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3222                if (retval)
3223                        /* cannot allocate the heap */
3224                        return retval;
3225        }
3226
3227 again:
3228        /*
3229         * Scan tasks in the cgroup, using the scanner's "test_task" callback
3230         * to determine which are of interest, and using the scanner's
3231         * "process_task" callback to process any of them that need an update.
3232         * Since we don't want to hold any locks during the task updates,
3233         * gather tasks to be processed in a heap structure.
3234         * The heap is sorted by descending task start time.
3235         * If the statically-sized heap fills up, we overflow tasks that
3236         * started later, and in future iterations only consider tasks that
3237         * started after the latest task in the previous pass. This
3238         * guarantees forward progress and that we don't miss any tasks.
3239         */
3240        heap->size = 0;
3241        cgroup_iter_start(scan->cg, &it);
3242        while ((p = cgroup_iter_next(scan->cg, &it))) {
3243                /*
3244                 * Only affect tasks that qualify per the caller's callback,
3245                 * if he provided one
3246                 */
3247                if (scan->test_task && !scan->test_task(p, scan))
3248                        continue;
3249                /*
3250                 * Only process tasks that started after the last task
3251                 * we processed
3252                 */
3253                if (!started_after_time(p, &latest_time, latest_task))
3254                        continue;
3255                dropped = heap_insert(heap, p);
3256                if (dropped == NULL) {
3257                        /*
3258                         * The new task was inserted; the heap wasn't
3259                         * previously full
3260                         */
3261                        get_task_struct(p);
3262                } else if (dropped != p) {
3263                        /*
3264                         * The new task was inserted, and pushed out a
3265                         * different task
3266                         */
3267                        get_task_struct(p);
3268                        put_task_struct(dropped);
3269                }
3270                /*
3271                 * Else the new task was newer than anything already in
3272                 * the heap and wasn't inserted
3273                 */
3274        }
3275        cgroup_iter_end(scan->cg, &it);
3276
3277        if (heap->size) {
3278                for (i = 0; i < heap->size; i++) {
3279                        struct task_struct *q = heap->ptrs[i];
3280                        if (i == 0) {
3281                                latest_time = q->start_time;
3282                                latest_task = q;
3283                        }
3284                        /* Process the task per the caller's callback */
3285                        scan->process_task(q, scan);
3286                        put_task_struct(q);
3287                }
3288                /*
3289                 * If we had to process any tasks at all, scan again
3290                 * in case some of them were in the middle of forking
3291                 * children that didn't get processed.
3292                 * Not the most efficient way to do it, but it avoids
3293                 * having to take callback_mutex in the fork path
3294                 */
3295                goto again;
3296        }
3297        if (heap == &tmp_heap)
3298                heap_free(&tmp_heap);
3299        return 0;
3300}
3301
3302/*
3303 * Stuff for reading the 'tasks'/'procs' files.
3304 *
3305 * Reading this file can return large amounts of data if a cgroup has
3306 * *lots* of attached tasks. So it may need several calls to read(),
3307 * but we cannot guarantee that the information we produce is correct
3308 * unless we produce it entirely atomically.
3309 *
3310 */
3311
3312/* which pidlist file are we talking about? */
3313enum cgroup_filetype {
3314        CGROUP_FILE_PROCS,
3315        CGROUP_FILE_TASKS,
3316};
3317
3318/*
3319 * A pidlist is a list of pids that virtually represents the contents of one
3320 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3321 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3322 * to the cgroup.
3323 */
3324struct cgroup_pidlist {
3325        /*
3326         * used to find which pidlist is wanted. doesn't change as long as
3327         * this particular list stays in the list.
3328        */
3329        struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3330        /* array of xids */
3331        pid_t *list;
3332        /* how many elements the above list has */
3333        int length;
3334        /* how many files are using the current array */
3335        int use_count;
3336        /* each of these stored in a list by its cgroup */
3337        struct list_head links;
3338        /* pointer to the cgroup we belong to, for list removal purposes */
3339        struct cgroup *owner;
3340        /* protects the other fields */
3341        struct rw_semaphore mutex;
3342};
3343
3344/*
3345 * The following two functions "fix" the issue where there are more pids
3346 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3347 * TODO: replace with a kernel-wide solution to this problem
3348 */
3349#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3350static void *pidlist_allocate(int count)
3351{
3352        if (PIDLIST_TOO_LARGE(count))
3353                return vmalloc(count * sizeof(pid_t));
3354        else
3355                return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3356}
3357static void pidlist_free(void *p)
3358{
3359        if (is_vmalloc_addr(p))
3360                vfree(p);
3361        else
3362                kfree(p);
3363}
3364static void *pidlist_resize(void *p, int newcount)
3365{
3366        void *newlist;
3367        /* note: if new alloc fails, old p will still be valid either way */
3368        if (is_vmalloc_addr(p)) {
3369                newlist = vmalloc(newcount * sizeof(pid_t));
3370                if (!newlist)
3371                        return NULL;
3372                memcpy(newlist, p, newcount * sizeof(pid_t));
3373                vfree(p);
3374        } else {
3375                newlist = krealloc(p, newcount * sizeof(pid_t), GFP_KERNEL);
3376        }
3377        return newlist;
3378}
3379
3380/*
3381 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
3382 * If the new stripped list is sufficiently smaller and there's enough memory
3383 * to allocate a new buffer, will let go of the unneeded memory. Returns the
3384 * number of unique elements.
3385 */
3386/* is the size difference enough that we should re-allocate the array? */
3387#define PIDLIST_REALLOC_DIFFERENCE(old, new) ((old) - PAGE_SIZE >= (new))
3388static int pidlist_uniq(pid_t **p, int length)
3389{
3390        int src, dest = 1;
3391        pid_t *list = *p;
3392        pid_t *newlist;
3393
3394        /*
3395         * we presume the 0th element is unique, so i starts at 1. trivial
3396         * edge cases first; no work needs to be done for either
3397         */
3398        if (length == 0 || length == 1)
3399                return length;
3400        /* src and dest walk down the list; dest counts unique elements */
3401        for (src = 1; src < length; src++) {
3402                /* find next unique element */
3403                while (list[src] == list[src-1]) {
3404                        src++;
3405                        if (src == length)
3406                                goto after;
3407                }
3408                /* dest always points to where the next unique element goes */
3409                list[dest] = list[src];
3410                dest++;
3411        }
3412after:
3413        /*
3414         * if the length difference is large enough, we want to allocate a
3415         * smaller buffer to save memory. if this fails due to out of memory,
3416         * we'll just stay with what we've got.
3417         */
3418        if (PIDLIST_REALLOC_DIFFERENCE(length, dest)) {
3419                newlist = pidlist_resize(list, dest);
3420                if (newlist)
3421                        *p = newlist;
3422        }
3423        return dest;
3424}
3425
3426static int cmppid(const void *a, const void *b)
3427{
3428        return *(pid_t *)a - *(pid_t *)b;
3429}
3430
3431/*
3432 * find the appropriate pidlist for our purpose (given procs vs tasks)
3433 * returns with the lock on that pidlist already held, and takes care
3434 * of the use count, or returns NULL with no locks held if we're out of
3435 * memory.
3436 */
3437static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3438                                                  enum cgroup_filetype type)
3439{
3440        struct cgroup_pidlist *l;
3441        /* don't need task_nsproxy() if we're looking at ourself */
3442        struct pid_namespace *ns = task_active_pid_ns(current);
3443
3444        /*
3445         * We can't drop the pidlist_mutex before taking the l->mutex in case
3446         * the last ref-holder is trying to remove l from the list at the same
3447         * time. Holding the pidlist_mutex precludes somebody taking whichever
3448         * list we find out from under us - compare release_pid_array().
3449         */
3450        mutex_lock(&cgrp->pidlist_mutex);
3451        list_for_each_entry(l, &cgrp->pidlists, links) {
3452                if (l->key.type == type && l->key.ns == ns) {
3453                        /* make sure l doesn't vanish out from under us */
3454                        down_write(&l->mutex);
3455                        mutex_unlock(&cgrp->pidlist_mutex);
3456                        return l;
3457                }
3458        }
3459        /* entry not found; create a new one */
3460        l = kmalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
3461        if (!l) {
3462                mutex_unlock(&cgrp->pidlist_mutex);
3463                return l;
3464        }
3465        init_rwsem(&l->mutex);
3466        down_write(&l->mutex);
3467        l->key.type = type;
3468        l->key.ns = get_pid_ns(ns);
3469        l->use_count = 0; /* don't increment here */
3470        l->list = NULL;
3471        l->owner = cgrp;
3472        list_add(&l->links, &cgrp->pidlists);
3473        mutex_unlock(&cgrp->pidlist_mutex);
3474        return l;
3475}
3476
3477/*
3478 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3479 */
3480static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3481                              struct cgroup_pidlist **lp)
3482{
3483        pid_t *array;
3484        int length;
3485        int pid, n = 0; /* used for populating the array */
3486        struct cgroup_iter it;
3487        struct task_struct *tsk;
3488        struct cgroup_pidlist *l;
3489
3490        /*
3491         * If cgroup gets more users after we read count, we won't have
3492         * enough space - tough.  This race is indistinguishable to the
3493         * caller from the case that the additional cgroup users didn't
3494         * show up until sometime later on.
3495         */
3496        length = cgroup_task_count(cgrp);
3497        array = pidlist_allocate(length);
3498        if (!array)
3499                return -ENOMEM;
3500        /* now, populate the array */
3501        cgroup_iter_start(cgrp, &it);
3502        while ((tsk = cgroup_iter_next(cgrp, &it))) {
3503                if (unlikely(n == length))
3504                        break;
3505                /* get tgid or pid for procs or tasks file respectively */
3506                if (type == CGROUP_FILE_PROCS)
3507                        pid = task_tgid_vnr(tsk);
3508                else
3509                        pid = task_pid_vnr(tsk);
3510                if (pid > 0) /* make sure to only use valid results */
3511                        array[n++] = pid;
3512        }
3513        cgroup_iter_end(cgrp, &it);
3514        length = n;
3515        /* now sort & (if procs) strip out duplicates */
3516        sort(array, length, sizeof(pid_t), cmppid, NULL);
3517        if (type == CGROUP_FILE_PROCS)
3518                length = pidlist_uniq(&array, length);
3519        l = cgroup_pidlist_find(cgrp, type);
3520        if (!l) {
3521                pidlist_free(array);
3522                return -ENOMEM;
3523        }
3524        /* store array, freeing old if necessary - lock already held */
3525        pidlist_free(l->list);
3526        l->list = array;
3527        l->length = length;
3528        l->use_count++;
3529        up_write(&l->mutex);
3530        *lp = l;
3531        return 0;
3532}
3533
3534/**
3535 * cgroupstats_build - build and fill cgroupstats
3536 * @stats: cgroupstats to fill information into
3537 * @dentry: A dentry entry belonging to the cgroup for which stats have
3538 * been requested.
3539 *
3540 * Build and fill cgroupstats so that taskstats can export it to user
3541 * space.
3542 */
3543int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3544{
3545        int ret = -EINVAL;
3546        struct cgroup *cgrp;
3547        struct cgroup_iter it;
3548        struct task_struct *tsk;
3549
3550        /*
3551         * Validate dentry by checking the superblock operations,
3552         * and make sure it's a directory.
3553         */
3554        if (dentry->d_sb->s_op != &cgroup_ops ||
3555            !S_ISDIR(dentry->d_inode->i_mode))
3556                 goto err;
3557
3558        ret = 0;
3559        cgrp = dentry->d_fsdata;
3560
3561        cgroup_iter_start(cgrp, &it);
3562        while ((tsk = cgroup_iter_next(cgrp, &it))) {
3563                switch (tsk->state) {
3564                case TASK_RUNNING:
3565                        stats->nr_running++;
3566                        break;
3567                case TASK_INTERRUPTIBLE:
3568                        stats->nr_sleeping++;
3569                        break;
3570                case TASK_UNINTERRUPTIBLE:
3571                        stats->nr_uninterruptible++;
3572                        break;
3573                case TASK_STOPPED:
3574                        stats->nr_stopped++;
3575                        break;
3576                default:
3577                        if (delayacct_is_task_waiting_on_io(tsk))
3578                                stats->nr_io_wait++;
3579                        break;
3580                }
3581        }
3582        cgroup_iter_end(cgrp, &it);
3583
3584err:
3585        return ret;
3586}
3587
3588
3589/*
3590 * seq_file methods for the tasks/procs files. The seq_file position is the
3591 * next pid to display; the seq_file iterator is a pointer to the pid
3592 * in the cgroup->l->list array.
3593 */
3594
3595static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
3596{
3597        /*
3598         * Initially we receive a position value that corresponds to
3599         * one more than the last pid shown (or 0 on the first call or
3600         * after a seek to the start). Use a binary-search to find the
3601         * next pid to display, if any
3602         */
3603        struct cgroup_pidlist *l = s->private;
3604        int index = 0, pid = *pos;
3605        int *iter;
3606
3607        down_read(&l->mutex);
3608        if (pid) {
3609                int end = l->length;
3610
3611                while (index < end) {
3612                        int mid = (index + end) / 2;
3613                        if (l->list[mid] == pid) {
3614                                index = mid;
3615                                break;
3616                        } else if (l->list[mid] <= pid)
3617                                index = mid + 1;
3618                        else
3619                                end = mid;
3620                }
3621        }
3622        /* If we're off the end of the array, we're done */
3623        if (index >= l->length)
3624                return NULL;
3625        /* Update the abstract position to be the actual pid that we found */
3626        iter = l->list + index;
3627        *pos = *iter;
3628        return iter;
3629}
3630
3631static void cgroup_pidlist_stop(struct seq_file *s, void *v)
3632{
3633        struct cgroup_pidlist *l = s->private;
3634        up_read(&l->mutex);
3635}
3636
3637static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
3638{
3639        struct cgroup_pidlist *l = s->private;
3640        pid_t *p = v;
3641        pid_t *end = l->list + l->length;
3642        /*
3643         * Advance to the next pid in the array. If this goes off the
3644         * end, we're done
3645         */
3646        p++;
3647        if (p >= end) {
3648                return NULL;
3649        } else {
3650                *pos = *p;
3651                return p;
3652        }
3653}
3654
3655static int cgroup_pidlist_show(struct seq_file *s, void *v)
3656{
3657        return seq_printf(s, "%d\n", *(int *)v);
3658}
3659
3660/*
3661 * seq_operations functions for iterating on pidlists through seq_file -
3662 * independent of whether it's tasks or procs
3663 */
3664static const struct seq_operations cgroup_pidlist_seq_operations = {
3665        .start = cgroup_pidlist_start,
3666        .stop = cgroup_pidlist_stop,
3667        .next = cgroup_pidlist_next,
3668        .show = cgroup_pidlist_show,
3669};
3670
3671static void cgroup_release_pid_array(struct cgroup_pidlist *l)
3672{
3673        /*
3674         * the case where we're the last user of this particular pidlist will
3675         * have us remove it from the cgroup's list, which entails taking the
3676         * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3677         * pidlist_mutex, we have to take pidlist_mutex first.
3678         */
3679        mutex_lock(&l->owner->pidlist_mutex);
3680        down_write(&l->mutex);
3681        BUG_ON(!l->use_count);
3682        if (!--l->use_count) {
3683                /* we're the last user if refcount is 0; remove and free */
3684                list_del(&l->links);
3685                mutex_unlock(&l->owner->pidlist_mutex);
3686                pidlist_free(l->list);
3687                put_pid_ns(l->key.ns);
3688                up_write(&l->mutex);
3689                kfree(l);
3690                return;
3691        }
3692        mutex_unlock(&l->owner->pidlist_mutex);
3693        up_write(&l->mutex);
3694}
3695
3696static int cgroup_pidlist_release(struct inode *inode, struct file *file)
3697{
3698        struct cgroup_pidlist *l;
3699        if (!(file->f_mode & FMODE_READ))
3700                return 0;
3701        /*
3702         * the seq_file will only be initialized if the file was opened for
3703         * reading; hence we check if it's not null only in that case.
3704         */
3705        l = ((struct seq_file *)file->private_data)->private;
3706        cgroup_release_pid_array(l);
3707        return seq_release(inode, file);
3708}
3709
3710static const struct file_operations cgroup_pidlist_operations = {
3711        .read = seq_read,
3712        .llseek = seq_lseek,
3713        .write = cgroup_file_write,
3714        .release = cgroup_pidlist_release,
3715};
3716
3717/*
3718 * The following functions handle opens on a file that displays a pidlist
3719 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3720 * in the cgroup.
3721 */
3722/* helper function for the two below it */
3723static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
3724{
3725        struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
3726        struct cgroup_pidlist *l;
3727        int retval;
3728
3729        /* Nothing to do for write-only files */
3730        if (!(file->f_mode & FMODE_READ))
3731                return 0;
3732
3733        /* have the array populated */
3734        retval = pidlist_array_load(cgrp, type, &l);
3735        if (retval)
3736                return retval;
3737        /* configure file information */
3738        file->f_op = &cgroup_pidlist_operations;
3739
3740        retval = seq_open(file, &cgroup_pidlist_seq_operations);
3741        if (retval) {
3742                cgroup_release_pid_array(l);
3743                return retval;
3744        }
3745        ((struct seq_file *)file->private_data)->private = l;
3746        return 0;
3747}
3748static int cgroup_tasks_open(struct inode *unused, struct file *file)
3749{
3750        return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
3751}
3752static int cgroup_procs_open(struct inode *unused, struct file *file)
3753{
3754        return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
3755}
3756
3757static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
3758                                            struct cftype *cft)
3759{
3760        return notify_on_release(cgrp);
3761}
3762
3763static int cgroup_write_notify_on_release(struct cgroup *cgrp,
3764                                          struct cftype *cft,
3765                                          u64 val)
3766{
3767        clear_bit(CGRP_RELEASABLE, &cgrp->flags);
3768        if (val)
3769                set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3770        else
3771                clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3772        return 0;
3773}
3774
3775/*
3776 * Unregister event and free resources.
3777 *
3778 * Gets called from workqueue.
3779 */
3780static void cgroup_event_remove(struct work_struct *work)
3781{
3782        struct cgroup_event *event = container_of(work, struct cgroup_event,
3783                        remove);
3784        struct cgroup *cgrp = event->cgrp;
3785
3786        remove_wait_queue(event->wqh, &event->wait);
3787
3788        event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3789
3790        /* Notify userspace the event is going away. */
3791        eventfd_signal(event->eventfd, 1);
3792
3793        eventfd_ctx_put(event->eventfd);
3794        kfree(event);
3795        dput(cgrp->dentry);
3796}
3797
3798/*
3799 * Gets called on POLLHUP on eventfd when user closes it.
3800 *
3801 * Called with wqh->lock held and interrupts disabled.
3802 */
3803static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
3804                int sync, void *key)
3805{
3806        struct cgroup_event *event = container_of(wait,
3807                        struct cgroup_event, wait);
3808        struct cgroup *cgrp = event->cgrp;
3809        unsigned long flags = (unsigned long)key;
3810
3811        if (flags & POLLHUP) {
3812                /*
3813                 * If the event has been detached at cgroup removal, we
3814                 * can simply return knowing the other side will cleanup
3815                 * for us.
3816                 *
3817                 * We can't race against event freeing since the other
3818                 * side will require wqh->lock via remove_wait_queue(),
3819                 * which we hold.
3820                 */
3821                spin_lock(&cgrp->event_list_lock);
3822                if (!list_empty(&event->list)) {
3823                        list_del_init(&event->list);
3824                        /*
3825                         * We are in atomic context, but cgroup_event_remove()
3826                         * may sleep, so we have to call it in workqueue.
3827                         */
3828                        schedule_work(&event->remove);
3829                }
3830                spin_unlock(&cgrp->event_list_lock);
3831        }
3832
3833        return 0;
3834}
3835
3836static void cgroup_event_ptable_queue_proc(struct file *file,
3837                wait_queue_head_t *wqh, poll_table *pt)
3838{
3839        struct cgroup_event *event = container_of(pt,
3840                        struct cgroup_event, pt);
3841
3842        event->wqh = wqh;
3843        add_wait_queue(wqh, &event->wait);
3844}
3845
3846/*
3847 * Parse input and register new cgroup event handler.
3848 *
3849 * Input must be in format '<event_fd> <control_fd> <args>'.
3850 * Interpretation of args is defined by control file implementation.
3851 */
3852static int cgroup_write_event_control(struct cgroup *cgrp, struct cftype *cft,
3853                                      const char *buffer)
3854{
3855        struct cgroup_event *event = NULL;
3856        struct cgroup *cgrp_cfile;
3857        unsigned int efd, cfd;
3858        struct file *efile = NULL;
3859        struct file *cfile = NULL;
3860        char *endp;
3861        int ret;
3862
3863        efd = simple_strtoul(buffer, &endp, 10);
3864        if (*endp != ' ')
3865                return -EINVAL;
3866        buffer = endp + 1;
3867
3868        cfd = simple_strtoul(buffer, &endp, 10);
3869        if ((*endp != ' ') && (*endp != '\0'))
3870                return -EINVAL;
3871        buffer = endp + 1;
3872
3873        event = kzalloc(sizeof(*event), GFP_KERNEL);
3874        if (!event)
3875                return -ENOMEM;
3876        event->cgrp = cgrp;
3877        INIT_LIST_HEAD(&event->list);
3878        init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
3879        init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
3880        INIT_WORK(&event->remove, cgroup_event_remove);
3881
3882        efile = eventfd_fget(efd);
3883        if (IS_ERR(efile)) {
3884                ret = PTR_ERR(efile);
3885                goto fail;
3886        }
3887
3888        event->eventfd = eventfd_ctx_fileget(efile);
3889        if (IS_ERR(event->eventfd)) {
3890                ret = PTR_ERR(event->eventfd);
3891                goto fail;
3892        }
3893
3894        cfile = fget(cfd);
3895        if (!cfile) {
3896                ret = -EBADF;
3897                goto fail;
3898        }
3899
3900        /* the process need read permission on control file */
3901        /* AV: shouldn't we check that it's been opened for read instead? */
3902        ret = inode_permission(file_inode(cfile), MAY_READ);
3903        if (ret < 0)
3904                goto fail;
3905
3906        event->cft = __file_cft(cfile);
3907        if (IS_ERR(event->cft)) {
3908                ret = PTR_ERR(event->cft);
3909                goto fail;
3910        }
3911
3912        /*
3913         * The file to be monitored must be in the same cgroup as
3914         * cgroup.event_control is.
3915         */
3916        cgrp_cfile = __d_cgrp(cfile->f_dentry->d_parent);
3917        if (cgrp_cfile != cgrp) {
3918                ret = -EINVAL;
3919                goto fail;
3920        }
3921
3922        if (!event->cft->register_event || !event->cft->unregister_event) {
3923                ret = -EINVAL;
3924                goto fail;
3925        }
3926
3927        ret = event->cft->register_event(cgrp, event->cft,
3928                        event->eventfd, buffer);
3929        if (ret)
3930                goto fail;
3931
3932        if (efile->f_op->poll(efile, &event->pt) & POLLHUP) {
3933                event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3934                ret = 0;
3935                goto fail;
3936        }
3937
3938        /*
3939         * Events should be removed after rmdir of cgroup directory, but before
3940         * destroying subsystem state objects. Let's take reference to cgroup
3941         * directory dentry to do that.
3942         */
3943        dget(cgrp->dentry);
3944
3945        spin_lock(&cgrp->event_list_lock);
3946        list_add(&event->list, &cgrp->event_list);
3947        spin_unlock(&cgrp->event_list_lock);
3948
3949        fput(cfile);
3950        fput(efile);
3951
3952        return 0;
3953
3954fail:
3955        if (cfile)
3956                fput(cfile);
3957
3958        if (event && event->eventfd && !IS_ERR(event->eventfd))
3959                eventfd_ctx_put(event->eventfd);
3960
3961        if (!IS_ERR_OR_NULL(efile))
3962                fput(efile);
3963
3964        kfree(event);
3965
3966        return ret;
3967}
3968
3969static u64 cgroup_clone_children_read(struct cgroup *cgrp,
3970                                    struct cftype *cft)
3971{
3972        return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
3973}
3974
3975static int cgroup_clone_children_write(struct cgroup *cgrp,
3976                                     struct cftype *cft,
3977                                     u64 val)
3978{
3979        if (val)
3980                set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
3981        else
3982                clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
3983        return 0;
3984}
3985
3986/*
3987 * for the common functions, 'private' gives the type of file
3988 */
3989/* for hysterical raisins, we can't put this on the older files */
3990#define CGROUP_FILE_GENERIC_PREFIX "cgroup."
3991static struct cftype files[] = {
3992        {
3993                .name = "tasks",
3994                .open = cgroup_tasks_open,
3995                .write_u64 = cgroup_tasks_write,
3996                .release = cgroup_pidlist_release,
3997                .mode = S_IRUGO | S_IWUSR,
3998        },
3999        {
4000                .name = CGROUP_FILE_GENERIC_PREFIX "procs",
4001                .open = cgroup_procs_open,
4002                .write_u64 = cgroup_procs_write,
4003                .release = cgroup_pidlist_release,
4004                .mode = S_IRUGO | S_IWUSR,
4005        },
4006        {
4007                .name = "notify_on_release",
4008                .read_u64 = cgroup_read_notify_on_release,
4009                .write_u64 = cgroup_write_notify_on_release,
4010        },
4011        {
4012                .name = CGROUP_FILE_GENERIC_PREFIX "event_control",
4013                .write_string = cgroup_write_event_control,
4014                .mode = S_IWUGO,
4015        },
4016        {
4017                .name = "cgroup.clone_children",
4018                .read_u64 = cgroup_clone_children_read,
4019                .write_u64 = cgroup_clone_children_write,
4020        },
4021        {
4022                .name = "release_agent",
4023                .flags = CFTYPE_ONLY_ON_ROOT,
4024                .read_seq_string = cgroup_release_agent_show,
4025                .write_string = cgroup_release_agent_write,
4026                .max_write_len = PATH_MAX,
4027        },
4028        { }     /* terminate */
4029};
4030
4031/**
4032 * cgroup_populate_dir - selectively creation of files in a directory
4033 * @cgrp: target cgroup
4034 * @base_files: true if the base files should be added
4035 * @subsys_mask: mask of the subsystem ids whose files should be added
4036 */
4037static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
4038                               unsigned long subsys_mask)
4039{
4040        int err;
4041        struct cgroup_subsys *ss;
4042
4043        if (base_files) {
4044                err = cgroup_addrm_files(cgrp, NULL, files, true);
4045                if (err < 0)
4046                        return err;
4047        }
4048
4049        /* process cftsets of each subsystem */
4050        for_each_subsys(cgrp->root, ss) {
4051                struct cftype_set *set;
4052                if (!test_bit(ss->subsys_id, &subsys_mask))
4053                        continue;
4054
4055                list_for_each_entry(set, &ss->cftsets, node)
4056                        cgroup_addrm_files(cgrp, ss, set->cfts, true);
4057        }
4058
4059        /* This cgroup is ready now */
4060        for_each_subsys(cgrp->root, ss) {
4061                struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4062                /*
4063                 * Update id->css pointer and make this css visible from
4064                 * CSS ID functions. This pointer will be dereferened
4065                 * from RCU-read-side without locks.
4066                 */
4067                if (css->id)
4068                        rcu_assign_pointer(css->id->css, css);
4069        }
4070
4071        return 0;
4072}
4073
4074static void css_dput_fn(struct work_struct *work)
4075{
4076        struct cgroup_subsys_state *css =
4077                container_of(work, struct cgroup_subsys_state, dput_work);
4078        struct dentry *dentry = css->cgroup->dentry;
4079        struct super_block *sb = dentry->d_sb;
4080
4081        atomic_inc(&sb->s_active);
4082        dput(dentry);
4083        deactivate_super(sb);
4084}
4085
4086static void init_cgroup_css(struct cgroup_subsys_state *css,
4087                               struct cgroup_subsys *ss,
4088                               struct cgroup *cgrp)
4089{
4090        css->cgroup = cgrp;
4091        atomic_set(&css->refcnt, 1);
4092        css->flags = 0;
4093        css->id = NULL;
4094        if (cgrp == dummytop)
4095                css->flags |= CSS_ROOT;
4096        BUG_ON(cgrp->subsys[ss->subsys_id]);
4097        cgrp->subsys[ss->subsys_id] = css;
4098
4099        /*
4100         * css holds an extra ref to @cgrp->dentry which is put on the last
4101         * css_put().  dput() requires process context, which css_put() may
4102         * be called without.  @css->dput_work will be used to invoke
4103         * dput() asynchronously from css_put().
4104         */
4105        INIT_WORK(&css->dput_work, css_dput_fn);
4106}
4107
4108/* invoke ->post_create() on a new CSS and mark it online if successful */
4109static int online_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
4110{
4111        int ret = 0;
4112
4113        lockdep_assert_held(&cgroup_mutex);
4114
4115        if (ss->css_online)
4116                ret = ss->css_online(cgrp);
4117        if (!ret)
4118                cgrp->subsys[ss->subsys_id]->flags |= CSS_ONLINE;
4119        return ret;
4120}
4121
4122/* if the CSS is online, invoke ->pre_destory() on it and mark it offline */
4123static void offline_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
4124        __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
4125{
4126        struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4127
4128        lockdep_assert_held(&cgroup_mutex);
4129
4130        if (!(css->flags & CSS_ONLINE))
4131                return;
4132
4133        /*
4134         * css_offline() should be called with cgroup_mutex unlocked.  See
4135         * 3fa59dfbc3 ("cgroup: fix potential deadlock in pre_destroy") for
4136         * details.  This temporary unlocking should go away once
4137         * cgroup_mutex is unexported from controllers.
4138         */
4139        if (ss->css_offline) {
4140                mutex_unlock(&cgroup_mutex);
4141                ss->css_offline(cgrp);
4142                mutex_lock(&cgroup_mutex);
4143        }
4144
4145        cgrp->subsys[ss->subsys_id]->flags &= ~CSS_ONLINE;
4146}
4147
4148/*
4149 * cgroup_create - create a cgroup
4150 * @parent: cgroup that will be parent of the new cgroup
4151 * @dentry: dentry of the new cgroup
4152 * @mode: mode to set on new inode
4153 *
4154 * Must be called with the mutex on the parent inode held
4155 */
4156static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
4157                             umode_t mode)
4158{
4159        struct cgroup *cgrp;
4160        struct cgroupfs_root *root = parent->root;
4161        int err = 0;
4162        struct cgroup_subsys *ss;
4163        struct super_block *sb = root->sb;
4164
4165        /* allocate the cgroup and its ID, 0 is reserved for the root */
4166        cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4167        if (!cgrp)
4168                return -ENOMEM;
4169
4170        cgrp->id = ida_simple_get(&root->cgroup_ida, 1, 0, GFP_KERNEL);
4171        if (cgrp->id < 0)
4172                goto err_free_cgrp;
4173
4174        /*
4175         * Only live parents can have children.  Note that the liveliness
4176         * check isn't strictly necessary because cgroup_mkdir() and
4177         * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4178         * anyway so that locking is contained inside cgroup proper and we
4179         * don't get nasty surprises if we ever grow another caller.
4180         */
4181        if (!cgroup_lock_live_group(parent)) {
4182                err = -ENODEV;
4183                goto err_free_id;
4184        }
4185
4186        /* Grab a reference on the superblock so the hierarchy doesn't
4187         * get deleted on unmount if there are child cgroups.  This
4188         * can be done outside cgroup_mutex, since the sb can't
4189         * disappear while someone has an open control file on the
4190         * fs */
4191        atomic_inc(&sb->s_active);
4192
4193        init_cgroup_housekeeping(cgrp);
4194
4195        dentry->d_fsdata = cgrp;
4196        cgrp->dentry = dentry;
4197
4198        cgrp->parent = parent;
4199        cgrp->root = parent->root;
4200        cgrp->top_cgroup = parent->top_cgroup;
4201
4202        if (notify_on_release(parent))
4203                set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4204
4205        if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4206                set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
4207
4208        for_each_subsys(root, ss) {
4209                struct cgroup_subsys_state *css;
4210
4211                css = ss->css_alloc(cgrp);
4212                if (IS_ERR(css)) {
4213                        err = PTR_ERR(css);
4214                        goto err_free_all;
4215                }
4216                init_cgroup_css(css, ss, cgrp);
4217                if (ss->use_id) {
4218                        err = alloc_css_id(ss, parent, cgrp);
4219                        if (err)
4220                                goto err_free_all;
4221                }
4222        }
4223
4224        /*
4225         * Create directory.  cgroup_create_file() returns with the new
4226         * directory locked on success so that it can be populated without
4227         * dropping cgroup_mutex.
4228         */
4229        err = cgroup_create_file(dentry, S_IFDIR | mode, sb);
4230        if (err < 0)
4231                goto err_free_all;
4232        lockdep_assert_held(&dentry->d_inode->i_mutex);
4233
4234        /* allocation complete, commit to creation */
4235        list_add_tail(&cgrp->allcg_node, &root->allcg_list);
4236        list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
4237        root->number_of_cgroups++;
4238
4239        /* each css holds a ref to the cgroup's dentry */
4240        for_each_subsys(root, ss)
4241                dget(dentry);
4242
4243        /* creation succeeded, notify subsystems */
4244        for_each_subsys(root, ss) {
4245                err = online_css(ss, cgrp);
4246                if (err)
4247                        goto err_destroy;
4248
4249                if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4250                    parent->parent) {
4251                        pr_warning("cgroup: %s (%d) created nested cgroup for controller \"%s\" which has incomplete hierarchy support. Nested cgroups may change behavior in the future.\n",
4252                                   current->comm, current->pid, ss->name);
4253                        if (!strcmp(ss->name, "memory"))
4254                                pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4255                        ss->warned_broken_hierarchy = true;
4256                }
4257        }
4258
4259        err = cgroup_populate_dir(cgrp, true, root->subsys_mask);
4260        if (err)
4261                goto err_destroy;
4262
4263        mutex_unlock(&cgroup_mutex);
4264        mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
4265
4266        return 0;
4267
4268err_free_all:
4269        for_each_subsys(root, ss) {
4270                if (cgrp->subsys[ss->subsys_id])
4271                        ss->css_free(cgrp);
4272        }
4273        mutex_unlock(&cgroup_mutex);
4274        /* Release the reference count that we took on the superblock */
4275        deactivate_super(sb);
4276err_free_id:
4277        ida_simple_remove(&root->cgroup_ida, cgrp->id);
4278err_free_cgrp:
4279        kfree(cgrp);
4280        return err;
4281
4282err_destroy:
4283        cgroup_destroy_locked(cgrp);
4284        mutex_unlock(&cgroup_mutex);
4285        mutex_unlock(&dentry->d_inode->i_mutex);
4286        return err;
4287}
4288
4289static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
4290{
4291        struct cgroup *c_parent = dentry->d_parent->d_fsdata;
4292
4293        /* the vfs holds inode->i_mutex already */
4294        return cgroup_create(c_parent, dentry, mode | S_IFDIR);
4295}
4296
4297/*
4298 * Check the reference count on each subsystem. Since we already
4299 * established that there are no tasks in the cgroup, if the css refcount
4300 * is also 1, then there should be no outstanding references, so the
4301 * subsystem is safe to destroy. We scan across all subsystems rather than
4302 * using the per-hierarchy linked list of mounted subsystems since we can
4303 * be called via check_for_release() with no synchronization other than
4304 * RCU, and the subsystem linked list isn't RCU-safe.
4305 */
4306static int cgroup_has_css_refs(struct cgroup *cgrp)
4307{
4308        int i;
4309
4310        /*
4311         * We won't need to lock the subsys array, because the subsystems
4312         * we're concerned about aren't going anywhere since our cgroup root
4313         * has a reference on them.
4314         */
4315        for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
4316                struct cgroup_subsys *ss = subsys[i];
4317                struct cgroup_subsys_state *css;
4318
4319                /* Skip subsystems not present or not in this hierarchy */
4320                if (ss == NULL || ss->root != cgrp->root)
4321                        continue;
4322
4323                css = cgrp->subsys[ss->subsys_id];
4324                /*
4325                 * When called from check_for_release() it's possible
4326                 * that by this point the cgroup has been removed
4327                 * and the css deleted. But a false-positive doesn't
4328                 * matter, since it can only happen if the cgroup
4329                 * has been deleted and hence no longer needs the
4330                 * release agent to be called anyway.
4331                 */
4332                if (css && css_refcnt(css) > 1)
4333                        return 1;
4334        }
4335        return 0;
4336}
4337
4338static int cgroup_destroy_locked(struct cgroup *cgrp)
4339        __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
4340{
4341        struct dentry *d = cgrp->dentry;
4342        struct cgroup *parent = cgrp->parent;
4343        DEFINE_WAIT(wait);
4344        struct cgroup_event *event, *tmp;
4345        struct cgroup_subsys *ss;
4346        LIST_HEAD(tmp_list);
4347
4348        lockdep_assert_held(&d->d_inode->i_mutex);
4349        lockdep_assert_held(&cgroup_mutex);
4350
4351        if (atomic_read(&cgrp->count) || !list_empty(&cgrp->children))
4352                return -EBUSY;
4353
4354        /*
4355         * Block new css_tryget() by deactivating refcnt and mark @cgrp
4356         * removed.  This makes future css_tryget() and child creation
4357         * attempts fail thus maintaining the removal conditions verified
4358         * above.
4359         */
4360        for_each_subsys(cgrp->root, ss) {
4361                struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4362
4363                WARN_ON(atomic_read(&css->refcnt) < 0);
4364                atomic_add(CSS_DEACT_BIAS, &css->refcnt);
4365        }
4366        set_bit(CGRP_REMOVED, &cgrp->flags);
4367
4368        /* tell subsystems to initate destruction */
4369        for_each_subsys(cgrp->root, ss)
4370                offline_css(ss, cgrp);
4371
4372        /*
4373         * Put all the base refs.  Each css holds an extra reference to the
4374         * cgroup's dentry and cgroup removal proceeds regardless of css
4375         * refs.  On the last put of each css, whenever that may be, the
4376         * extra dentry ref is put so that dentry destruction happens only
4377         * after all css's are released.
4378         */
4379        for_each_subsys(cgrp->root, ss)
4380                css_put(cgrp->subsys[ss->subsys_id]);
4381
4382        raw_spin_lock(&release_list_lock);
4383        if (!list_empty(&cgrp->release_list))
4384                list_del_init(&cgrp->release_list);
4385        raw_spin_unlock(&release_list_lock);
4386
4387        /* delete this cgroup from parent->children */
4388        list_del_rcu(&cgrp->sibling);
4389        list_del_init(&cgrp->allcg_node);
4390
4391        dget(d);
4392        cgroup_d_remove_dir(d);
4393        dput(d);
4394
4395        set_bit(CGRP_RELEASABLE, &parent->flags);
4396        check_for_release(parent);
4397
4398        /*
4399         * Unregister events and notify userspace.
4400         * Notify userspace about cgroup removing only after rmdir of cgroup
4401         * directory to avoid race between userspace and kernelspace.
4402         */
4403        spin_lock(&cgrp->event_list_lock);
4404        list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) {
4405                list_del_init(&event->list);
4406                schedule_work(&event->remove);
4407        }
4408        spin_unlock(&cgrp->event_list_lock);
4409
4410        return 0;
4411}
4412
4413static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
4414{
4415        int ret;
4416
4417        mutex_lock(&cgroup_mutex);
4418        ret = cgroup_destroy_locked(dentry->d_fsdata);
4419        mutex_unlock(&cgroup_mutex);
4420
4421        return ret;
4422}
4423
4424static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss)
4425{
4426        INIT_LIST_HEAD(&ss->cftsets);
4427
4428        /*
4429         * base_cftset is embedded in subsys itself, no need to worry about
4430         * deregistration.
4431         */
4432        if (ss->base_cftypes) {
4433                ss->base_cftset.cfts = ss->base_cftypes;
4434                list_add_tail(&ss->base_cftset.node, &ss->cftsets);
4435        }
4436}
4437
4438static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
4439{
4440        struct cgroup_subsys_state *css;
4441
4442        printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
4443
4444        mutex_lock(&cgroup_mutex);
4445
4446        /* init base cftset */
4447        cgroup_init_cftsets(ss);
4448
4449        /* Create the top cgroup state for this subsystem */
4450        list_add(&ss->sibling, &rootnode.subsys_list);
4451        ss->root = &rootnode;
4452        css = ss->css_alloc(dummytop);
4453        /* We don't handle early failures gracefully */
4454        BUG_ON(IS_ERR(css));
4455        init_cgroup_css(css, ss, dummytop);
4456
4457        /* Update the init_css_set to contain a subsys
4458         * pointer to this state - since the subsystem is
4459         * newly registered, all tasks and hence the
4460         * init_css_set is in the subsystem's top cgroup. */
4461        init_css_set.subsys[ss->subsys_id] = css;
4462
4463        need_forkexit_callback |= ss->fork || ss->exit;
4464
4465        /* At system boot, before all subsystems have been
4466         * registered, no tasks have been forked, so we don't
4467         * need to invoke fork callbacks here. */
4468        BUG_ON(!list_empty(&init_task.tasks));
4469
4470        ss->active = 1;
4471        BUG_ON(online_css(ss, dummytop));
4472
4473        mutex_unlock(&cgroup_mutex);
4474
4475        /* this function shouldn't be used with modular subsystems, since they
4476         * need to register a subsys_id, among other things */
4477        BUG_ON(ss->module);
4478}
4479
4480/**
4481 * cgroup_load_subsys: load and register a modular subsystem at runtime
4482 * @ss: the subsystem to load
4483 *
4484 * This function should be called in a modular subsystem's initcall. If the
4485 * subsystem is built as a module, it will be assigned a new subsys_id and set
4486 * up for use. If the subsystem is built-in anyway, work is delegated to the
4487 * simpler cgroup_init_subsys.
4488 */
4489int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4490{
4491        struct cgroup_subsys_state *css;
4492        int i, ret;
4493        struct hlist_node *tmp;
4494        struct css_set *cg;
4495        unsigned long key;
4496
4497        /* check name and function validity */
4498        if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
4499            ss->css_alloc == NULL || ss->css_free == NULL)
4500                return -EINVAL;
4501
4502        /*
4503         * we don't support callbacks in modular subsystems. this check is
4504         * before the ss->module check for consistency; a subsystem that could
4505         * be a module should still have no callbacks even if the user isn't
4506         * compiling it as one.
4507         */
4508        if (ss->fork || ss->exit)
4509                return -EINVAL;
4510
4511        /*
4512         * an optionally modular subsystem is built-in: we want to do nothing,
4513         * since cgroup_init_subsys will have already taken care of it.
4514         */
4515        if (ss->module == NULL) {
4516                /* a sanity check */
4517                BUG_ON(subsys[ss->subsys_id] != ss);
4518                return 0;
4519        }
4520
4521        /* init base cftset */
4522        cgroup_init_cftsets(ss);
4523
4524        mutex_lock(&cgroup_mutex);
4525        subsys[ss->subsys_id] = ss;
4526
4527        /*
4528         * no ss->css_alloc seems to need anything important in the ss
4529         * struct, so this can happen first (i.e. before the rootnode
4530         * attachment).
4531         */
4532        css = ss->css_alloc(dummytop);
4533        if (IS_ERR(css)) {
4534                /* failure case - need to deassign the subsys[] slot. */
4535                subsys[ss->subsys_id] = NULL;
4536                mutex_unlock(&cgroup_mutex);
4537                return PTR_ERR(css);
4538        }
4539
4540        list_add(&ss->sibling, &rootnode.subsys_list);
4541        ss->root = &rootnode;
4542
4543        /* our new subsystem will be attached to the dummy hierarchy. */
4544        init_cgroup_css(css, ss, dummytop);
4545        /* init_idr must be after init_cgroup_css because it sets css->id. */
4546        if (ss->use_id) {
4547                ret = cgroup_init_idr(ss, css);
4548                if (ret)
4549                        goto err_unload;
4550        }
4551
4552        /*
4553         * Now we need to entangle the css into the existing css_sets. unlike
4554         * in cgroup_init_subsys, there are now multiple css_sets, so each one
4555         * will need a new pointer to it; done by iterating the css_set_table.
4556         * furthermore, modifying the existing css_sets will corrupt the hash
4557         * table state, so each changed css_set will need its hash recomputed.
4558         * this is all done under the css_set_lock.
4559         */
4560        write_lock(&css_set_lock);
4561        hash_for_each_safe(css_set_table, i, tmp, cg, hlist) {
4562                /* skip entries that we already rehashed */
4563                if (cg->subsys[ss->subsys_id])
4564                        continue;
4565                /* remove existing entry */
4566                hash_del(&cg->hlist);
4567                /* set new value */
4568                cg->subsys[ss->subsys_id] = css;
4569                /* recompute hash and restore entry */
4570                key = css_set_hash(cg->subsys);
4571                hash_add(css_set_table, &cg->hlist, key);
4572        }
4573        write_unlock(&css_set_lock);
4574
4575        ss->active = 1;
4576        ret = online_css(ss, dummytop);
4577        if (ret)
4578                goto err_unload;
4579
4580        /* success! */
4581        mutex_unlock(&cgroup_mutex);
4582        return 0;
4583
4584err_unload:
4585        mutex_unlock(&cgroup_mutex);
4586        /* @ss can't be mounted here as try_module_get() would fail */
4587        cgroup_unload_subsys(ss);
4588        return ret;
4589}
4590EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4591
4592/**
4593 * cgroup_unload_subsys: unload a modular subsystem
4594 * @ss: the subsystem to unload
4595 *
4596 * This function should be called in a modular subsystem's exitcall. When this
4597 * function is invoked, the refcount on the subsystem's module will be 0, so
4598 * the subsystem will not be attached to any hierarchy.
4599 */
4600void cgroup_unload_subsys(struct cgroup_subsys *ss)
4601{
4602        struct cg_cgroup_link *link;
4603
4604        BUG_ON(ss->module == NULL);
4605
4606        /*
4607         * we shouldn't be called if the subsystem is in use, and the use of
4608         * try_module_get in parse_cgroupfs_options should ensure that it
4609         * doesn't start being used while we're killing it off.
4610         */
4611        BUG_ON(ss->root != &rootnode);
4612
4613        mutex_lock(&cgroup_mutex);
4614
4615        offline_css(ss, dummytop);
4616        ss->active = 0;
4617
4618        if (ss->use_id)
4619                idr_destroy(&ss->idr);
4620
4621        /* deassign the subsys_id */
4622        subsys[ss->subsys_id] = NULL;
4623
4624        /* remove subsystem from rootnode's list of subsystems */
4625        list_del_init(&ss->sibling);
4626
4627        /*
4628         * disentangle the css from all css_sets attached to the dummytop. as
4629         * in loading, we need to pay our respects to the hashtable gods.
4630         */
4631        write_lock(&css_set_lock);
4632        list_for_each_entry(link, &dummytop->css_sets, cgrp_link_list) {
4633                struct css_set *cg = link->cg;
4634                unsigned long key;
4635
4636                hash_del(&cg->hlist);
4637                cg->subsys[ss->subsys_id] = NULL;
4638                key = css_set_hash(cg->subsys);
4639                hash_add(css_set_table, &cg->hlist, key);
4640        }
4641        write_unlock(&css_set_lock);
4642
4643        /*
4644         * remove subsystem's css from the dummytop and free it - need to
4645         * free before marking as null because ss->css_free needs the
4646         * cgrp->subsys pointer to find their state. note that this also
4647         * takes care of freeing the css_id.
4648         */
4649        ss->css_free(dummytop);
4650        dummytop->subsys[ss->subsys_id] = NULL;
4651
4652        mutex_unlock(&cgroup_mutex);
4653}
4654EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
4655
4656/**
4657 * cgroup_init_early - cgroup initialization at system boot
4658 *
4659 * Initialize cgroups at system boot, and initialize any
4660 * subsystems that request early init.
4661 */
4662int __init cgroup_init_early(void)
4663{
4664        int i;
4665        atomic_set(&init_css_set.refcount, 1);
4666        INIT_LIST_HEAD(&init_css_set.cg_links);
4667        INIT_LIST_HEAD(&init_css_set.tasks);
4668        INIT_HLIST_NODE(&init_css_set.hlist);
4669        css_set_count = 1;
4670        init_cgroup_root(&rootnode);
4671        root_count = 1;
4672        init_task.cgroups = &init_css_set;
4673
4674        init_css_set_link.cg = &init_css_set;
4675        init_css_set_link.cgrp = dummytop;
4676        list_add(&init_css_set_link.cgrp_link_list,
4677                 &rootnode.top_cgroup.css_sets);
4678        list_add(&init_css_set_link.cg_link_list,
4679                 &init_css_set.cg_links);
4680
4681        for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
4682                struct cgroup_subsys *ss = subsys[i];
4683
4684                /* at bootup time, we don't worry about modular subsystems */
4685                if (!ss || ss->module)
4686                        continue;
4687
4688                BUG_ON(!ss->name);
4689                BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
4690                BUG_ON(!ss->css_alloc);
4691                BUG_ON(!ss->css_free);
4692                if (ss->subsys_id != i) {
4693                        printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
4694                               ss->name, ss->subsys_id);
4695                        BUG();
4696                }
4697
4698                if (ss->early_init)
4699                        cgroup_init_subsys(ss);
4700        }
4701        return 0;
4702}
4703
4704/**
4705 * cgroup_init - cgroup initialization
4706 *
4707 * Register cgroup filesystem and /proc file, and initialize
4708 * any subsystems that didn't request early init.
4709 */
4710int __init cgroup_init(void)
4711{
4712        int err;
4713        int i;
4714        unsigned long key;
4715
4716        err = bdi_init(&cgroup_backing_dev_info);
4717        if (err)
4718                return err;
4719
4720        for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
4721                struct cgroup_subsys *ss = subsys[i];
4722
4723                /* at bootup time, we don't worry about modular subsystems */
4724                if (!ss || ss->module)
4725                        continue;
4726                if (!ss->early_init)
4727                        cgroup_init_subsys(ss);
4728                if (ss->use_id)
4729                        cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
4730        }
4731
4732        /* Add init_css_set to the hash table */
4733        key = css_set_hash(init_css_set.subsys);
4734        hash_add(css_set_table, &init_css_set.hlist, key);
4735        BUG_ON(!init_root_id(&rootnode));
4736
4737        cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
4738        if (!cgroup_kobj) {
4739                err = -ENOMEM;
4740                goto out;
4741        }
4742
4743        err = register_filesystem(&cgroup_fs_type);
4744        if (err < 0) {
4745                kobject_put(cgroup_kobj);
4746                goto out;
4747        }
4748
4749        proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
4750
4751out:
4752        if (err)
4753                bdi_destroy(&cgroup_backing_dev_info);
4754
4755        return err;
4756}
4757
4758/*
4759 * proc_cgroup_show()
4760 *  - Print task's cgroup paths into seq_file, one line for each hierarchy
4761 *  - Used for /proc/<pid>/cgroup.
4762 *  - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4763 *    doesn't really matter if tsk->cgroup changes after we read it,
4764 *    and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
4765 *    anyway.  No need to check that tsk->cgroup != NULL, thanks to
4766 *    the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4767 *    cgroup to top_cgroup.
4768 */
4769
4770/* TODO: Use a proper seq_file iterator */
4771static int proc_cgroup_show(struct seq_file *m, void *v)
4772{
4773        struct pid *pid;
4774        struct task_struct *tsk;
4775        char *buf;
4776        int retval;
4777        struct cgroupfs_root *root;
4778
4779        retval = -ENOMEM;
4780        buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4781        if (!buf)
4782                goto out;
4783
4784        retval = -ESRCH;
4785        pid = m->private;
4786        tsk = get_pid_task(pid, PIDTYPE_PID);
4787        if (!tsk)
4788                goto out_free;
4789
4790        retval = 0;
4791
4792        mutex_lock(&cgroup_mutex);
4793
4794        for_each_active_root(root) {
4795                struct cgroup_subsys *ss;
4796                struct cgroup *cgrp;
4797                int count = 0;
4798
4799                seq_printf(m, "%d:", root->hierarchy_id);
4800                for_each_subsys(root, ss)
4801                        seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
4802                if (strlen(root->name))
4803                        seq_printf(m, "%sname=%s", count ? "," : "",
4804                                   root->name);
4805                seq_putc(m, ':');
4806                cgrp = task_cgroup_from_root(tsk, root);
4807                retval = cgroup_path(cgrp, buf, PAGE_SIZE);
4808                if (retval < 0)
4809                        goto out_unlock;
4810                seq_puts(m, buf);
4811                seq_putc(m, '\n');
4812        }
4813
4814out_unlock:
4815        mutex_unlock(&cgroup_mutex);
4816        put_task_struct(tsk);
4817out_free:
4818        kfree(buf);
4819out:
4820        return retval;
4821}
4822
4823static int cgroup_open(struct inode *inode, struct file *file)
4824{
4825        struct pid *pid = PROC_I(inode)->pid;
4826        return single_open(file, proc_cgroup_show, pid);
4827}
4828
4829const struct file_operations proc_cgroup_operations = {
4830        .open           = cgroup_open,
4831        .read           = seq_read,
4832        .llseek         = seq_lseek,
4833        .release        = single_release,
4834};
4835
4836/* Display information about each subsystem and each hierarchy */
4837static int proc_cgroupstats_show(struct seq_file *m, void *v)
4838{
4839        int i;
4840
4841        seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
4842        /*
4843         * ideally we don't want subsystems moving around while we do this.
4844         * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4845         * subsys/hierarchy state.
4846         */
4847        mutex_lock(&cgroup_mutex);
4848        for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
4849                struct cgroup_subsys *ss = subsys[i];
4850                if (ss == NULL)
4851                        continue;
4852                seq_printf(m, "%s\t%d\t%d\t%d\n",
4853                           ss->name, ss->root->hierarchy_id,
4854                           ss->root->number_of_cgroups, !ss->disabled);
4855        }
4856        mutex_unlock(&cgroup_mutex);
4857        return 0;
4858}
4859
4860static int cgroupstats_open(struct inode *inode, struct file *file)
4861{
4862        return single_open(file, proc_cgroupstats_show, NULL);
4863}
4864
4865static const struct file_operations proc_cgroupstats_operations = {
4866        .open = cgroupstats_open,
4867        .read = seq_read,
4868        .llseek = seq_lseek,
4869        .release = single_release,
4870};
4871
4872/**
4873 * cgroup_fork - attach newly forked task to its parents cgroup.
4874 * @child: pointer to task_struct of forking parent process.
4875 *
4876 * Description: A task inherits its parent's cgroup at fork().
4877 *
4878 * A pointer to the shared css_set was automatically copied in
4879 * fork.c by dup_task_struct().  However, we ignore that copy, since
4880 * it was not made under the protection of RCU or cgroup_mutex, so
4881 * might no longer be a valid cgroup pointer.  cgroup_attach_task() might
4882 * have already changed current->cgroups, allowing the previously
4883 * referenced cgroup group to be removed and freed.
4884 *
4885 * At the point that cgroup_fork() is called, 'current' is the parent
4886 * task, and the passed argument 'child' points to the child task.
4887 */
4888void cgroup_fork(struct task_struct *child)
4889{
4890        task_lock(current);
4891        child->cgroups = current->cgroups;
4892        get_css_set(child->cgroups);
4893        task_unlock(current);
4894        INIT_LIST_HEAD(&child->cg_list);
4895}
4896
4897/**
4898 * cgroup_post_fork - called on a new task after adding it to the task list
4899 * @child: the task in question
4900 *
4901 * Adds the task to the list running through its css_set if necessary and
4902 * call the subsystem fork() callbacks.  Has to be after the task is
4903 * visible on the task list in case we race with the first call to
4904 * cgroup_iter_start() - to guarantee that the new task ends up on its
4905 * list.
4906 */
4907void cgroup_post_fork(struct task_struct *child)
4908{
4909        int i;
4910
4911        /*
4912         * use_task_css_set_links is set to 1 before we walk the tasklist
4913         * under the tasklist_lock and we read it here after we added the child
4914         * to the tasklist under the tasklist_lock as well. If the child wasn't
4915         * yet in the tasklist when we walked through it from
4916         * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
4917         * should be visible now due to the paired locking and barriers implied
4918         * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
4919         * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
4920         * lock on fork.
4921         */
4922        if (use_task_css_set_links) {
4923                write_lock(&css_set_lock);
4924                task_lock(child);
4925                if (list_empty(&child->cg_list))
4926                        list_add(&child->cg_list, &child->cgroups->tasks);
4927                task_unlock(child);
4928                write_unlock(&css_set_lock);
4929        }
4930
4931        /*
4932         * Call ss->fork().  This must happen after @child is linked on
4933         * css_set; otherwise, @child might change state between ->fork()
4934         * and addition to css_set.
4935         */
4936        if (need_forkexit_callback) {
4937                for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
4938                        struct cgroup_subsys *ss = subsys[i];
4939
4940                        /*
4941                         * fork/exit callbacks are supported only for
4942                         * builtin subsystems and we don't need further
4943                         * synchronization as they never go away.
4944                         */
4945                        if (!ss || ss->module)
4946                                continue;
4947
4948                        if (ss->fork)
4949                                ss->fork(child);
4950                }
4951        }
4952}
4953
4954/**
4955 * cgroup_exit - detach cgroup from exiting task
4956 * @tsk: pointer to task_struct of exiting process
4957 * @run_callback: run exit callbacks?
4958 *
4959 * Description: Detach cgroup from @tsk and release it.
4960 *
4961 * Note that cgroups marked notify_on_release force every task in
4962 * them to take the global cgroup_mutex mutex when exiting.
4963 * This could impact scaling on very large systems.  Be reluctant to
4964 * use notify_on_release cgroups where very high task exit scaling
4965 * is required on large systems.
4966 *
4967 * the_top_cgroup_hack:
4968 *
4969 *    Set the exiting tasks cgroup to the root cgroup (top_cgroup).
4970 *
4971 *    We call cgroup_exit() while the task is still competent to
4972 *    handle notify_on_release(), then leave the task attached to the
4973 *    root cgroup in each hierarchy for the remainder of its exit.
4974 *
4975 *    To do this properly, we would increment the reference count on
4976 *    top_cgroup, and near the very end of the kernel/exit.c do_exit()
4977 *    code we would add a second cgroup function call, to drop that
4978 *    reference.  This would just create an unnecessary hot spot on
4979 *    the top_cgroup reference count, to no avail.
4980 *
4981 *    Normally, holding a reference to a cgroup without bumping its
4982 *    count is unsafe.   The cgroup could go away, or someone could
4983 *    attach us to a different cgroup, decrementing the count on
4984 *    the first cgroup that we never incremented.  But in this case,
4985 *    top_cgroup isn't going away, and either task has PF_EXITING set,
4986 *    which wards off any cgroup_attach_task() attempts, or task is a failed
4987 *    fork, never visible to cgroup_attach_task.
4988 */
4989void cgroup_exit(struct task_struct *tsk, int run_callbacks)
4990{
4991        struct css_set *cg;
4992        int i;
4993
4994        /*
4995         * Unlink from the css_set task list if necessary.
4996         * Optimistically check cg_list before taking
4997         * css_set_lock
4998         */
4999        if (!list_empty(&tsk->cg_list)) {
5000                write_lock(&css_set_lock);
5001                if (!list_empty(&tsk->cg_list))
5002                        list_del_init(&tsk->cg_list);
5003                write_unlock(&css_set_lock);
5004        }
5005
5006        /* Reassign the task to the init_css_set. */
5007        task_lock(tsk);
5008        cg = tsk->cgroups;
5009        tsk->cgroups = &init_css_set;
5010
5011        if (run_callbacks && need_forkexit_callback) {
5012                for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
5013                        struct cgroup_subsys *ss = subsys[i];
5014
5015                        /* modular subsystems can't use callbacks */
5016                        if (!ss || ss->module)
5017                                continue;
5018
5019                        if (ss->exit) {
5020                                struct cgroup *old_cgrp =
5021                                        rcu_dereference_raw(cg->subsys[i])->cgroup;
5022                                struct cgroup *cgrp = task_cgroup(tsk, i);
5023                                ss->exit(cgrp, old_cgrp, tsk);
5024                        }
5025                }
5026        }
5027        task_unlock(tsk);
5028
5029        put_css_set_taskexit(cg);
5030}
5031
5032/**
5033 * cgroup_is_descendant - see if @cgrp is a descendant of @task's cgrp
5034 * @cgrp: the cgroup in question
5035 * @task: the task in question
5036 *
5037 * See if @cgrp is a descendant of @task's cgroup in the appropriate
5038 * hierarchy.
5039 *
5040 * If we are sending in dummytop, then presumably we are creating
5041 * the top cgroup in the subsystem.
5042 *
5043 * Called only by the ns (nsproxy) cgroup.
5044 */
5045int cgroup_is_descendant(const struct cgroup *cgrp, struct task_struct *task)
5046{
5047        int ret;
5048        struct cgroup *target;
5049
5050        if (cgrp == dummytop)
5051                return 1;
5052
5053        target = task_cgroup_from_root(task, cgrp->root);
5054        while (cgrp != target && cgrp!= cgrp->top_cgroup)
5055                cgrp = cgrp->parent;
5056        ret = (cgrp == target);
5057        return ret;
5058}
5059
5060static void check_for_release(struct cgroup *cgrp)
5061{
5062        /* All of these checks rely on RCU to keep the cgroup
5063         * structure alive */
5064        if (cgroup_is_releasable(cgrp) && !atomic_read(&cgrp->count)
5065            && list_empty(&cgrp->children) && !cgroup_has_css_refs(cgrp)) {
5066                /* Control Group is currently removeable. If it's not
5067                 * already queued for a userspace notification, queue
5068                 * it now */
5069                int need_schedule_work = 0;
5070                raw_spin_lock(&release_list_lock);
5071                if (!cgroup_is_removed(cgrp) &&
5072                    list_empty(&cgrp->release_list)) {
5073                        list_add(&cgrp->release_list, &release_list);
5074                        need_schedule_work = 1;
5075                }
5076                raw_spin_unlock(&release_list_lock);
5077                if (need_schedule_work)
5078                        schedule_work(&release_agent_work);
5079        }
5080}
5081
5082/* Caller must verify that the css is not for root cgroup */
5083bool __css_tryget(struct cgroup_subsys_state *css)
5084{
5085        while (true) {
5086                int t, v;
5087
5088                v = css_refcnt(css);
5089                t = atomic_cmpxchg(&css->refcnt, v, v + 1);
5090                if (likely(t == v))
5091                        return true;
5092                else if (t < 0)
5093                        return false;
5094                cpu_relax();
5095        }
5096}
5097EXPORT_SYMBOL_GPL(__css_tryget);
5098
5099/* Caller must verify that the css is not for root cgroup */
5100void __css_put(struct cgroup_subsys_state *css)
5101{
5102        struct cgroup *cgrp = css->cgroup;
5103        int v;
5104
5105        rcu_read_lock();
5106        v = css_unbias_refcnt(atomic_dec_return(&css->refcnt));
5107
5108        switch (v) {
5109        case 1:
5110                if (notify_on_release(cgrp)) {
5111                        set_bit(CGRP_RELEASABLE, &cgrp->flags);
5112                        check_for_release(cgrp);
5113                }
5114                break;
5115        case 0:
5116                schedule_work(&css->dput_work);
5117                break;
5118        }
5119        rcu_read_unlock();
5120}
5121EXPORT_SYMBOL_GPL(__css_put);
5122
5123/*
5124 * Notify userspace when a cgroup is released, by running the
5125 * configured release agent with the name of the cgroup (path
5126 * relative to the root of cgroup file system) as the argument.
5127 *
5128 * Most likely, this user command will try to rmdir this cgroup.
5129 *
5130 * This races with the possibility that some other task will be
5131 * attached to this cgroup before it is removed, or that some other
5132 * user task will 'mkdir' a child cgroup of this cgroup.  That's ok.
5133 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5134 * unused, and this cgroup will be reprieved from its death sentence,
5135 * to continue to serve a useful existence.  Next time it's released,
5136 * we will get notified again, if it still has 'notify_on_release' set.
5137 *
5138 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5139 * means only wait until the task is successfully execve()'d.  The
5140 * separate release agent task is forked by call_usermodehelper(),
5141 * then control in this thread returns here, without waiting for the
5142 * release agent task.  We don't bother to wait because the caller of
5143 * this routine has no use for the exit status of the release agent
5144 * task, so no sense holding our caller up for that.
5145 */
5146static void cgroup_release_agent(struct work_struct *work)
5147{
5148        BUG_ON(work != &release_agent_work);
5149        mutex_lock(&cgroup_mutex);
5150        raw_spin_lock(&release_list_lock);
5151        while (!list_empty(&release_list)) {
5152                char *argv[3], *envp[3];
5153                int i;
5154                char *pathbuf = NULL, *agentbuf = NULL;
5155                struct cgroup *cgrp = list_entry(release_list.next,
5156                                                    struct cgroup,
5157                                                    release_list);
5158                list_del_init(&cgrp->release_list);
5159                raw_spin_unlock(&release_list_lock);
5160                pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
5161                if (!pathbuf)
5162                        goto continue_free;
5163                if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
5164                        goto continue_free;
5165                agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5166                if (!agentbuf)
5167                        goto continue_free;
5168
5169                i = 0;
5170                argv[i++] = agentbuf;
5171                argv[i++] = pathbuf;
5172                argv[i] = NULL;
5173
5174                i = 0;
5175                /* minimal command environment */
5176                envp[i++] = "HOME=/";
5177                envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5178                envp[i] = NULL;
5179
5180                /* Drop the lock while we invoke the usermode helper,
5181                 * since the exec could involve hitting disk and hence
5182                 * be a slow process */
5183                mutex_unlock(&cgroup_mutex);
5184                call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
5185                mutex_lock(&cgroup_mutex);
5186 continue_free:
5187                kfree(pathbuf);
5188                kfree(agentbuf);
5189                raw_spin_lock(&release_list_lock);
5190        }
5191        raw_spin_unlock(&release_list_lock);
5192        mutex_unlock(&cgroup_mutex);
5193}
5194
5195static int __init cgroup_disable(char *str)
5196{
5197        int i;
5198        char *token;
5199
5200        while ((token = strsep(&str, ",")) != NULL) {
5201                if (!*token)
5202                        continue;
5203                for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
5204                        struct cgroup_subsys *ss = subsys[i];
5205
5206                        /*
5207                         * cgroup_disable, being at boot time, can't
5208                         * know about module subsystems, so we don't
5209                         * worry about them.
5210                         */
5211                        if (!ss || ss->module)
5212                                continue;
5213
5214                        if (!strcmp(token, ss->name)) {
5215                                ss->disabled = 1;
5216                                printk(KERN_INFO "Disabling %s control group"
5217                                        " subsystem\n", ss->name);
5218                                break;
5219                        }
5220                }
5221        }
5222        return 1;
5223}
5224__setup("cgroup_disable=", cgroup_disable);
5225
5226/*
5227 * Functons for CSS ID.
5228 */
5229
5230/*
5231 *To get ID other than 0, this should be called when !cgroup_is_removed().
5232 */
5233unsigned short css_id(struct cgroup_subsys_state *css)
5234{
5235        struct css_id *cssid;
5236
5237        /*
5238         * This css_id() can return correct value when somone has refcnt
5239         * on this or this is under rcu_read_lock(). Once css->id is allocated,
5240         * it's unchanged until freed.
5241         */
5242        cssid = rcu_dereference_check(css->id, css_refcnt(css));
5243
5244        if (cssid)
5245                return cssid->id;
5246        return 0;
5247}
5248EXPORT_SYMBOL_GPL(css_id);
5249
5250unsigned short css_depth(struct cgroup_subsys_state *css)
5251{
5252        struct css_id *cssid;
5253
5254        cssid = rcu_dereference_check(css->id, css_refcnt(css));
5255
5256        if (cssid)
5257                return cssid->depth;
5258        return 0;
5259}
5260EXPORT_SYMBOL_GPL(css_depth);
5261
5262/**
5263 *  css_is_ancestor - test "root" css is an ancestor of "child"
5264 * @child: the css to be tested.
5265 * @root: the css supporsed to be an ancestor of the child.
5266 *
5267 * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
5268 * this function reads css->id, the caller must hold rcu_read_lock().
5269 * But, considering usual usage, the csses should be valid objects after test.
5270 * Assuming that the caller will do some action to the child if this returns
5271 * returns true, the caller must take "child";s reference count.
5272 * If "child" is valid object and this returns true, "root" is valid, too.
5273 */
5274
5275bool css_is_ancestor(struct cgroup_subsys_state *child,
5276                    const struct cgroup_subsys_state *root)
5277{
5278        struct css_id *child_id;
5279        struct css_id *root_id;
5280
5281        child_id  = rcu_dereference(child->id);
5282        if (!child_id)
5283                return false;
5284        root_id = rcu_dereference(root->id);
5285        if (!root_id)
5286                return false;
5287        if (child_id->depth < root_id->depth)
5288                return false;
5289        if (child_id->stack[root_id->depth] != root_id->id)
5290                return false;
5291        return true;
5292}
5293
5294void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
5295{
5296        struct css_id *id = css->id;
5297        /* When this is called before css_id initialization, id can be NULL */
5298        if (!id)
5299                return;
5300
5301        BUG_ON(!ss->use_id);
5302
5303        rcu_assign_pointer(id->css, NULL);
5304        rcu_assign_pointer(css->id, NULL);
5305        spin_lock(&ss->id_lock);
5306        idr_remove(&ss->idr, id->id);
5307        spin_unlock(&ss->id_lock);
5308        kfree_rcu(id, rcu_head);
5309}
5310EXPORT_SYMBOL_GPL(free_css_id);
5311
5312/*
5313 * This is called by init or create(). Then, calls to this function are
5314 * always serialized (By cgroup_mutex() at create()).
5315 */
5316
5317static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
5318{
5319        struct css_id *newid;
5320        int ret, size;
5321
5322        BUG_ON(!ss->use_id);
5323
5324        size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
5325        newid = kzalloc(size, GFP_KERNEL);
5326        if (!newid)
5327                return ERR_PTR(-ENOMEM);
5328
5329        idr_preload(GFP_KERNEL);
5330        spin_lock(&ss->id_lock);
5331        /* Don't use 0. allocates an ID of 1-65535 */
5332        ret = idr_alloc(&ss->idr, newid, 1, CSS_ID_MAX + 1, GFP_NOWAIT);
5333        spin_unlock(&ss->id_lock);
5334        idr_preload_end();
5335
5336        /* Returns error when there are no free spaces for new ID.*/
5337        if (ret < 0)
5338                goto err_out;
5339
5340        newid->id = ret;
5341        newid->depth = depth;
5342        return newid;
5343err_out:
5344        kfree(newid);
5345        return ERR_PTR(ret);
5346
5347}
5348
5349static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
5350                                            struct cgroup_subsys_state *rootcss)
5351{
5352        struct css_id *newid;
5353
5354        spin_lock_init(&ss->id_lock);
5355        idr_init(&ss->idr);
5356
5357        newid = get_new_cssid(ss, 0);
5358        if (IS_ERR(newid))
5359                return PTR_ERR(newid);
5360
5361        newid->stack[0] = newid->id;
5362        newid->css = rootcss;
5363        rootcss->id = newid;
5364        return 0;
5365}
5366
5367static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
5368                        struct cgroup *child)
5369{
5370        int subsys_id, i, depth = 0;
5371        struct cgroup_subsys_state *parent_css, *child_css;
5372        struct css_id *child_id, *parent_id;
5373
5374        subsys_id = ss->subsys_id;
5375        parent_css = parent->subsys[subsys_id];
5376        child_css = child->subsys[subsys_id];
5377        parent_id = parent_css->id;
5378        depth = parent_id->depth + 1;
5379
5380        child_id = get_new_cssid(ss, depth);
5381        if (IS_ERR(child_id))
5382                return PTR_ERR(child_id);
5383
5384        for (i = 0; i < depth; i++)
5385                child_id->stack[i] = parent_id->stack[i];
5386        child_id->stack[depth] = child_id->id;
5387        /*
5388         * child_id->css pointer will be set after this cgroup is available
5389         * see cgroup_populate_dir()
5390         */
5391        rcu_assign_pointer(child_css->id, child_id);
5392
5393        return 0;
5394}
5395
5396/**
5397 * css_lookup - lookup css by id
5398 * @ss: cgroup subsys to be looked into.
5399 * @id: the id
5400 *
5401 * Returns pointer to cgroup_subsys_state if there is valid one with id.
5402 * NULL if not. Should be called under rcu_read_lock()
5403 */
5404struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
5405{
5406        struct css_id *cssid = NULL;
5407
5408        BUG_ON(!ss->use_id);
5409        cssid = idr_find(&ss->idr, id);
5410
5411        if (unlikely(!cssid))
5412                return NULL;
5413
5414        return rcu_dereference(cssid->css);
5415}
5416EXPORT_SYMBOL_GPL(css_lookup);
5417
5418/**
5419 * css_get_next - lookup next cgroup under specified hierarchy.
5420 * @ss: pointer to subsystem
5421 * @id: current position of iteration.
5422 * @root: pointer to css. search tree under this.
5423 * @foundid: position of found object.
5424 *
5425 * Search next css under the specified hierarchy of rootid. Calling under
5426 * rcu_read_lock() is necessary. Returns NULL if it reaches the end.
5427 */
5428struct cgroup_subsys_state *
5429css_get_next(struct cgroup_subsys *ss, int id,
5430             struct cgroup_subsys_state *root, int *foundid)
5431{
5432        struct cgroup_subsys_state *ret = NULL;
5433        struct css_id *tmp;
5434        int tmpid;
5435        int rootid = css_id(root);
5436        int depth = css_depth(root);
5437
5438        if (!rootid)
5439                return NULL;
5440
5441        BUG_ON(!ss->use_id);
5442        WARN_ON_ONCE(!rcu_read_lock_held());
5443
5444        /* fill start point for scan */
5445        tmpid = id;
5446        while (1) {
5447                /*
5448                 * scan next entry from bitmap(tree), tmpid is updated after
5449                 * idr_get_next().
5450                 */
5451                tmp = idr_get_next(&ss->idr, &tmpid);
5452                if (!tmp)
5453                        break;
5454                if (tmp->depth >= depth && tmp->stack[depth] == rootid) {
5455                        ret = rcu_dereference(tmp->css);
5456                        if (ret) {
5457                                *foundid = tmpid;
5458                                break;
5459                        }
5460                }
5461                /* continue to scan from next id */
5462                tmpid = tmpid + 1;
5463        }
5464        return ret;
5465}
5466
5467/*
5468 * get corresponding css from file open on cgroupfs directory
5469 */
5470struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id)
5471{
5472        struct cgroup *cgrp;
5473        struct inode *inode;
5474        struct cgroup_subsys_state *css;
5475
5476        inode = file_inode(f);
5477        /* check in cgroup filesystem dir */
5478        if (inode->i_op != &cgroup_dir_inode_operations)
5479                return ERR_PTR(-EBADF);
5480
5481        if (id < 0 || id >= CGROUP_SUBSYS_COUNT)
5482                return ERR_PTR(-EINVAL);
5483
5484        /* get cgroup */
5485        cgrp = __d_cgrp(f->f_dentry);
5486        css = cgrp->subsys[id];
5487        return css ? css : ERR_PTR(-ENOENT);
5488}
5489
5490#ifdef CONFIG_CGROUP_DEBUG
5491static struct cgroup_subsys_state *debug_css_alloc(struct cgroup *cont)
5492{
5493        struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5494
5495        if (!css)
5496                return ERR_PTR(-ENOMEM);
5497
5498        return css;
5499}
5500
5501static void debug_css_free(struct cgroup *cont)
5502{
5503        kfree(cont->subsys[debug_subsys_id]);
5504}
5505
5506static u64 cgroup_refcount_read(struct cgroup *cont, struct cftype *cft)
5507{
5508        return atomic_read(&cont->count);
5509}
5510
5511static u64 debug_taskcount_read(struct cgroup *cont, struct cftype *cft)
5512{
5513        return cgroup_task_count(cont);
5514}
5515
5516static u64 current_css_set_read(struct cgroup *cont, struct cftype *cft)
5517{
5518        return (u64)(unsigned long)current->cgroups;
5519}
5520
5521static u64 current_css_set_refcount_read(struct cgroup *cont,
5522                                           struct cftype *cft)
5523{
5524        u64 count;
5525
5526        rcu_read_lock();
5527        count = atomic_read(&current->cgroups->refcount);
5528        rcu_read_unlock();
5529        return count;
5530}
5531
5532static int current_css_set_cg_links_read(struct cgroup *cont,
5533                                         struct cftype *cft,
5534                                         struct seq_file *seq)
5535{
5536        struct cg_cgroup_link *link;
5537        struct css_set *cg;
5538
5539        read_lock(&css_set_lock);
5540        rcu_read_lock();
5541        cg = rcu_dereference(current->cgroups);
5542        list_for_each_entry(link, &cg->cg_links, cg_link_list) {
5543                struct cgroup *c = link->cgrp;
5544                const char *name;
5545
5546                if (c->dentry)
5547                        name = c->dentry->d_name.name;
5548                else
5549                        name = "?";
5550                seq_printf(seq, "Root %d group %s\n",
5551                           c->root->hierarchy_id, name);
5552        }
5553        rcu_read_unlock();
5554        read_unlock(&css_set_lock);
5555        return 0;
5556}
5557
5558#define MAX_TASKS_SHOWN_PER_CSS 25
5559static int cgroup_css_links_read(struct cgroup *cont,
5560                                 struct cftype *cft,
5561                                 struct seq_file *seq)
5562{
5563        struct cg_cgroup_link *link;
5564
5565        read_lock(&css_set_lock);
5566        list_for_each_entry(link, &cont->css_sets, cgrp_link_list) {
5567                struct css_set *cg = link->cg;
5568                struct task_struct *task;
5569                int count = 0;
5570                seq_printf(seq, "css_set %p\n", cg);
5571                list_for_each_entry(task, &cg->tasks, cg_list) {
5572                        if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5573                                seq_puts(seq, "  ...\n");
5574                                break;
5575                        } else {
5576                                seq_printf(seq, "  task %d\n",
5577                                           task_pid_vnr(task));
5578                        }
5579                }
5580        }
5581        read_unlock(&css_set_lock);
5582        return 0;
5583}
5584
5585static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft)
5586{
5587        return test_bit(CGRP_RELEASABLE, &cgrp->flags);
5588}
5589
5590static struct cftype debug_files[] =  {
5591        {
5592                .name = "cgroup_refcount",
5593                .read_u64 = cgroup_refcount_read,
5594        },
5595        {
5596                .name = "taskcount",
5597                .read_u64 = debug_taskcount_read,
5598        },
5599
5600        {
5601                .name = "current_css_set",
5602                .read_u64 = current_css_set_read,
5603        },
5604
5605        {
5606                .name = "current_css_set_refcount",
5607                .read_u64 = current_css_set_refcount_read,
5608        },
5609
5610        {
5611                .name = "current_css_set_cg_links",
5612                .read_seq_string = current_css_set_cg_links_read,
5613        },
5614
5615        {
5616                .name = "cgroup_css_links",
5617                .read_seq_string = cgroup_css_links_read,
5618        },
5619
5620        {
5621                .name = "releasable",
5622                .read_u64 = releasable_read,
5623        },
5624
5625        { }     /* terminate */
5626};
5627
5628struct cgroup_subsys debug_subsys = {
5629        .name = "debug",
5630        .css_alloc = debug_css_alloc,
5631        .css_free = debug_css_free,
5632        .subsys_id = debug_subsys_id,
5633        .base_cftypes = debug_files,
5634};
5635#endif /* CONFIG_CGROUP_DEBUG */
5636
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.