]> git.karo-electronics.de Git - karo-tx-linux.git/blob - kernel/cgroup/cgroup.c
firmware: qcom_scm: add two scm calls for iommu secure page table
[karo-tx-linux.git] / kernel / cgroup / cgroup.c
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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30
31 #include "cgroup-internal.h"
32
33 #include <linux/cred.h>
34 #include <linux/errno.h>
35 #include <linux/init_task.h>
36 #include <linux/kernel.h>
37 #include <linux/magic.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/sched/task.h>
45 #include <linux/slab.h>
46 #include <linux/spinlock.h>
47 #include <linux/percpu-rwsem.h>
48 #include <linux/string.h>
49 #include <linux/hashtable.h>
50 #include <linux/idr.h>
51 #include <linux/kthread.h>
52 #include <linux/atomic.h>
53 #include <linux/cpuset.h>
54 #include <linux/proc_ns.h>
55 #include <linux/nsproxy.h>
56 #include <linux/file.h>
57 #include <net/sock.h>
58
59 #define CREATE_TRACE_POINTS
60 #include <trace/events/cgroup.h>
61
62 #define CGROUP_FILE_NAME_MAX            (MAX_CGROUP_TYPE_NAMELEN +      \
63                                          MAX_CFTYPE_NAME + 2)
64
65 /*
66  * cgroup_mutex is the master lock.  Any modification to cgroup or its
67  * hierarchy must be performed while holding it.
68  *
69  * css_set_lock protects task->cgroups pointer, the list of css_set
70  * objects, and the chain of tasks off each css_set.
71  *
72  * These locks are exported if CONFIG_PROVE_RCU so that accessors in
73  * cgroup.h can use them for lockdep annotations.
74  */
75 DEFINE_MUTEX(cgroup_mutex);
76 DEFINE_SPINLOCK(css_set_lock);
77
78 #ifdef CONFIG_PROVE_RCU
79 EXPORT_SYMBOL_GPL(cgroup_mutex);
80 EXPORT_SYMBOL_GPL(css_set_lock);
81 #endif
82
83 /*
84  * Protects cgroup_idr and css_idr so that IDs can be released without
85  * grabbing cgroup_mutex.
86  */
87 static DEFINE_SPINLOCK(cgroup_idr_lock);
88
89 /*
90  * Protects cgroup_file->kn for !self csses.  It synchronizes notifications
91  * against file removal/re-creation across css hiding.
92  */
93 static DEFINE_SPINLOCK(cgroup_file_kn_lock);
94
95 struct percpu_rw_semaphore cgroup_threadgroup_rwsem;
96
97 #define cgroup_assert_mutex_or_rcu_locked()                             \
98         RCU_LOCKDEP_WARN(!rcu_read_lock_held() &&                       \
99                            !lockdep_is_held(&cgroup_mutex),             \
100                            "cgroup_mutex or RCU read lock required");
101
102 /*
103  * cgroup destruction makes heavy use of work items and there can be a lot
104  * of concurrent destructions.  Use a separate workqueue so that cgroup
105  * destruction work items don't end up filling up max_active of system_wq
106  * which may lead to deadlock.
107  */
108 static struct workqueue_struct *cgroup_destroy_wq;
109
110 /* generate an array of cgroup subsystem pointers */
111 #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys,
112 struct cgroup_subsys *cgroup_subsys[] = {
113 #include <linux/cgroup_subsys.h>
114 };
115 #undef SUBSYS
116
117 /* array of cgroup subsystem names */
118 #define SUBSYS(_x) [_x ## _cgrp_id] = #_x,
119 static const char *cgroup_subsys_name[] = {
120 #include <linux/cgroup_subsys.h>
121 };
122 #undef SUBSYS
123
124 /* array of static_keys for cgroup_subsys_enabled() and cgroup_subsys_on_dfl() */
125 #define SUBSYS(_x)                                                              \
126         DEFINE_STATIC_KEY_TRUE(_x ## _cgrp_subsys_enabled_key);                 \
127         DEFINE_STATIC_KEY_TRUE(_x ## _cgrp_subsys_on_dfl_key);                  \
128         EXPORT_SYMBOL_GPL(_x ## _cgrp_subsys_enabled_key);                      \
129         EXPORT_SYMBOL_GPL(_x ## _cgrp_subsys_on_dfl_key);
130 #include <linux/cgroup_subsys.h>
131 #undef SUBSYS
132
133 #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys_enabled_key,
134 static struct static_key_true *cgroup_subsys_enabled_key[] = {
135 #include <linux/cgroup_subsys.h>
136 };
137 #undef SUBSYS
138
139 #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys_on_dfl_key,
140 static struct static_key_true *cgroup_subsys_on_dfl_key[] = {
141 #include <linux/cgroup_subsys.h>
142 };
143 #undef SUBSYS
144
145 /*
146  * The default hierarchy, reserved for the subsystems that are otherwise
147  * unattached - it never has more than a single cgroup, and all tasks are
148  * part of that cgroup.
149  */
150 struct cgroup_root cgrp_dfl_root;
151 EXPORT_SYMBOL_GPL(cgrp_dfl_root);
152
153 /*
154  * The default hierarchy always exists but is hidden until mounted for the
155  * first time.  This is for backward compatibility.
156  */
157 static bool cgrp_dfl_visible;
158
159 /* some controllers are not supported in the default hierarchy */
160 static u16 cgrp_dfl_inhibit_ss_mask;
161
162 /* some controllers are implicitly enabled on the default hierarchy */
163 static u16 cgrp_dfl_implicit_ss_mask;
164
165 /* The list of hierarchy roots */
166 LIST_HEAD(cgroup_roots);
167 static int cgroup_root_count;
168
169 /* hierarchy ID allocation and mapping, protected by cgroup_mutex */
170 static DEFINE_IDR(cgroup_hierarchy_idr);
171
172 /*
173  * Assign a monotonically increasing serial number to csses.  It guarantees
174  * cgroups with bigger numbers are newer than those with smaller numbers.
175  * Also, as csses are always appended to the parent's ->children list, it
176  * guarantees that sibling csses are always sorted in the ascending serial
177  * number order on the list.  Protected by cgroup_mutex.
178  */
179 static u64 css_serial_nr_next = 1;
180
181 /*
182  * These bitmasks identify subsystems with specific features to avoid
183  * having to do iterative checks repeatedly.
184  */
185 static u16 have_fork_callback __read_mostly;
186 static u16 have_exit_callback __read_mostly;
187 static u16 have_free_callback __read_mostly;
188 static u16 have_canfork_callback __read_mostly;
189
190 /* cgroup namespace for init task */
191 struct cgroup_namespace init_cgroup_ns = {
192         .count          = { .counter = 2, },
193         .user_ns        = &init_user_ns,
194         .ns.ops         = &cgroupns_operations,
195         .ns.inum        = PROC_CGROUP_INIT_INO,
196         .root_cset      = &init_css_set,
197 };
198
199 static struct file_system_type cgroup2_fs_type;
200 static struct cftype cgroup_base_files[];
201
202 static int cgroup_apply_control(struct cgroup *cgrp);
203 static void cgroup_finalize_control(struct cgroup *cgrp, int ret);
204 static void css_task_iter_advance(struct css_task_iter *it);
205 static int cgroup_destroy_locked(struct cgroup *cgrp);
206 static struct cgroup_subsys_state *css_create(struct cgroup *cgrp,
207                                               struct cgroup_subsys *ss);
208 static void css_release(struct percpu_ref *ref);
209 static void kill_css(struct cgroup_subsys_state *css);
210 static int cgroup_addrm_files(struct cgroup_subsys_state *css,
211                               struct cgroup *cgrp, struct cftype cfts[],
212                               bool is_add);
213
214 /**
215  * cgroup_ssid_enabled - cgroup subsys enabled test by subsys ID
216  * @ssid: subsys ID of interest
217  *
218  * cgroup_subsys_enabled() can only be used with literal subsys names which
219  * is fine for individual subsystems but unsuitable for cgroup core.  This
220  * is slower static_key_enabled() based test indexed by @ssid.
221  */
222 bool cgroup_ssid_enabled(int ssid)
223 {
224         if (CGROUP_SUBSYS_COUNT == 0)
225                 return false;
226
227         return static_key_enabled(cgroup_subsys_enabled_key[ssid]);
228 }
229
230 /**
231  * cgroup_on_dfl - test whether a cgroup is on the default hierarchy
232  * @cgrp: the cgroup of interest
233  *
234  * The default hierarchy is the v2 interface of cgroup and this function
235  * can be used to test whether a cgroup is on the default hierarchy for
236  * cases where a subsystem should behave differnetly depending on the
237  * interface version.
238  *
239  * The set of behaviors which change on the default hierarchy are still
240  * being determined and the mount option is prefixed with __DEVEL__.
241  *
242  * List of changed behaviors:
243  *
244  * - Mount options "noprefix", "xattr", "clone_children", "release_agent"
245  *   and "name" are disallowed.
246  *
247  * - When mounting an existing superblock, mount options should match.
248  *
249  * - Remount is disallowed.
250  *
251  * - rename(2) is disallowed.
252  *
253  * - "tasks" is removed.  Everything should be at process granularity.  Use
254  *   "cgroup.procs" instead.
255  *
256  * - "cgroup.procs" is not sorted.  pids will be unique unless they got
257  *   recycled inbetween reads.
258  *
259  * - "release_agent" and "notify_on_release" are removed.  Replacement
260  *   notification mechanism will be implemented.
261  *
262  * - "cgroup.clone_children" is removed.
263  *
264  * - "cgroup.subtree_populated" is available.  Its value is 0 if the cgroup
265  *   and its descendants contain no task; otherwise, 1.  The file also
266  *   generates kernfs notification which can be monitored through poll and
267  *   [di]notify when the value of the file changes.
268  *
269  * - cpuset: tasks will be kept in empty cpusets when hotplug happens and
270  *   take masks of ancestors with non-empty cpus/mems, instead of being
271  *   moved to an ancestor.
272  *
273  * - cpuset: a task can be moved into an empty cpuset, and again it takes
274  *   masks of ancestors.
275  *
276  * - memcg: use_hierarchy is on by default and the cgroup file for the flag
277  *   is not created.
278  *
279  * - blkcg: blk-throttle becomes properly hierarchical.
280  *
281  * - debug: disallowed on the default hierarchy.
282  */
283 bool cgroup_on_dfl(const struct cgroup *cgrp)
284 {
285         return cgrp->root == &cgrp_dfl_root;
286 }
287
288 /* IDR wrappers which synchronize using cgroup_idr_lock */
289 static int cgroup_idr_alloc(struct idr *idr, void *ptr, int start, int end,
290                             gfp_t gfp_mask)
291 {
292         int ret;
293
294         idr_preload(gfp_mask);
295         spin_lock_bh(&cgroup_idr_lock);
296         ret = idr_alloc(idr, ptr, start, end, gfp_mask & ~__GFP_DIRECT_RECLAIM);
297         spin_unlock_bh(&cgroup_idr_lock);
298         idr_preload_end();
299         return ret;
300 }
301
302 static void *cgroup_idr_replace(struct idr *idr, void *ptr, int id)
303 {
304         void *ret;
305
306         spin_lock_bh(&cgroup_idr_lock);
307         ret = idr_replace(idr, ptr, id);
308         spin_unlock_bh(&cgroup_idr_lock);
309         return ret;
310 }
311
312 static void cgroup_idr_remove(struct idr *idr, int id)
313 {
314         spin_lock_bh(&cgroup_idr_lock);
315         idr_remove(idr, id);
316         spin_unlock_bh(&cgroup_idr_lock);
317 }
318
319 static struct cgroup *cgroup_parent(struct cgroup *cgrp)
320 {
321         struct cgroup_subsys_state *parent_css = cgrp->self.parent;
322
323         if (parent_css)
324                 return container_of(parent_css, struct cgroup, self);
325         return NULL;
326 }
327
328 /* subsystems visibly enabled on a cgroup */
329 static u16 cgroup_control(struct cgroup *cgrp)
330 {
331         struct cgroup *parent = cgroup_parent(cgrp);
332         u16 root_ss_mask = cgrp->root->subsys_mask;
333
334         if (parent)
335                 return parent->subtree_control;
336
337         if (cgroup_on_dfl(cgrp))
338                 root_ss_mask &= ~(cgrp_dfl_inhibit_ss_mask |
339                                   cgrp_dfl_implicit_ss_mask);
340         return root_ss_mask;
341 }
342
343 /* subsystems enabled on a cgroup */
344 static u16 cgroup_ss_mask(struct cgroup *cgrp)
345 {
346         struct cgroup *parent = cgroup_parent(cgrp);
347
348         if (parent)
349                 return parent->subtree_ss_mask;
350
351         return cgrp->root->subsys_mask;
352 }
353
354 /**
355  * cgroup_css - obtain a cgroup's css for the specified subsystem
356  * @cgrp: the cgroup of interest
357  * @ss: the subsystem of interest (%NULL returns @cgrp->self)
358  *
359  * Return @cgrp's css (cgroup_subsys_state) associated with @ss.  This
360  * function must be called either under cgroup_mutex or rcu_read_lock() and
361  * the caller is responsible for pinning the returned css if it wants to
362  * keep accessing it outside the said locks.  This function may return
363  * %NULL if @cgrp doesn't have @subsys_id enabled.
364  */
365 static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
366                                               struct cgroup_subsys *ss)
367 {
368         if (ss)
369                 return rcu_dereference_check(cgrp->subsys[ss->id],
370                                         lockdep_is_held(&cgroup_mutex));
371         else
372                 return &cgrp->self;
373 }
374
375 /**
376  * cgroup_e_css - obtain a cgroup's effective css for the specified subsystem
377  * @cgrp: the cgroup of interest
378  * @ss: the subsystem of interest (%NULL returns @cgrp->self)
379  *
380  * Similar to cgroup_css() but returns the effective css, which is defined
381  * as the matching css of the nearest ancestor including self which has @ss
382  * enabled.  If @ss is associated with the hierarchy @cgrp is on, this
383  * function is guaranteed to return non-NULL css.
384  */
385 static struct cgroup_subsys_state *cgroup_e_css(struct cgroup *cgrp,
386                                                 struct cgroup_subsys *ss)
387 {
388         lockdep_assert_held(&cgroup_mutex);
389
390         if (!ss)
391                 return &cgrp->self;
392
393         /*
394          * This function is used while updating css associations and thus
395          * can't test the csses directly.  Test ss_mask.
396          */
397         while (!(cgroup_ss_mask(cgrp) & (1 << ss->id))) {
398                 cgrp = cgroup_parent(cgrp);
399                 if (!cgrp)
400                         return NULL;
401         }
402
403         return cgroup_css(cgrp, ss);
404 }
405
406 /**
407  * cgroup_get_e_css - get a cgroup's effective css for the specified subsystem
408  * @cgrp: the cgroup of interest
409  * @ss: the subsystem of interest
410  *
411  * Find and get the effective css of @cgrp for @ss.  The effective css is
412  * defined as the matching css of the nearest ancestor including self which
413  * has @ss enabled.  If @ss is not mounted on the hierarchy @cgrp is on,
414  * the root css is returned, so this function always returns a valid css.
415  * The returned css must be put using css_put().
416  */
417 struct cgroup_subsys_state *cgroup_get_e_css(struct cgroup *cgrp,
418                                              struct cgroup_subsys *ss)
419 {
420         struct cgroup_subsys_state *css;
421
422         rcu_read_lock();
423
424         do {
425                 css = cgroup_css(cgrp, ss);
426
427                 if (css && css_tryget_online(css))
428                         goto out_unlock;
429                 cgrp = cgroup_parent(cgrp);
430         } while (cgrp);
431
432         css = init_css_set.subsys[ss->id];
433         css_get(css);
434 out_unlock:
435         rcu_read_unlock();
436         return css;
437 }
438
439 static void cgroup_get(struct cgroup *cgrp)
440 {
441         WARN_ON_ONCE(cgroup_is_dead(cgrp));
442         css_get(&cgrp->self);
443 }
444
445 static bool cgroup_tryget(struct cgroup *cgrp)
446 {
447         return css_tryget(&cgrp->self);
448 }
449
450 struct cgroup_subsys_state *of_css(struct kernfs_open_file *of)
451 {
452         struct cgroup *cgrp = of->kn->parent->priv;
453         struct cftype *cft = of_cft(of);
454
455         /*
456          * This is open and unprotected implementation of cgroup_css().
457          * seq_css() is only called from a kernfs file operation which has
458          * an active reference on the file.  Because all the subsystem
459          * files are drained before a css is disassociated with a cgroup,
460          * the matching css from the cgroup's subsys table is guaranteed to
461          * be and stay valid until the enclosing operation is complete.
462          */
463         if (cft->ss)
464                 return rcu_dereference_raw(cgrp->subsys[cft->ss->id]);
465         else
466                 return &cgrp->self;
467 }
468 EXPORT_SYMBOL_GPL(of_css);
469
470 /**
471  * for_each_css - iterate all css's of a cgroup
472  * @css: the iteration cursor
473  * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
474  * @cgrp: the target cgroup to iterate css's of
475  *
476  * Should be called under cgroup_[tree_]mutex.
477  */
478 #define for_each_css(css, ssid, cgrp)                                   \
479         for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++)        \
480                 if (!((css) = rcu_dereference_check(                    \
481                                 (cgrp)->subsys[(ssid)],                 \
482                                 lockdep_is_held(&cgroup_mutex)))) { }   \
483                 else
484
485 /**
486  * for_each_e_css - iterate all effective css's of a cgroup
487  * @css: the iteration cursor
488  * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
489  * @cgrp: the target cgroup to iterate css's of
490  *
491  * Should be called under cgroup_[tree_]mutex.
492  */
493 #define for_each_e_css(css, ssid, cgrp)                                 \
494         for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++)        \
495                 if (!((css) = cgroup_e_css(cgrp, cgroup_subsys[(ssid)]))) \
496                         ;                                               \
497                 else
498
499 /**
500  * do_each_subsys_mask - filter for_each_subsys with a bitmask
501  * @ss: the iteration cursor
502  * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
503  * @ss_mask: the bitmask
504  *
505  * The block will only run for cases where the ssid-th bit (1 << ssid) of
506  * @ss_mask is set.
507  */
508 #define do_each_subsys_mask(ss, ssid, ss_mask) do {                     \
509         unsigned long __ss_mask = (ss_mask);                            \
510         if (!CGROUP_SUBSYS_COUNT) { /* to avoid spurious gcc warning */ \
511                 (ssid) = 0;                                             \
512                 break;                                                  \
513         }                                                               \
514         for_each_set_bit(ssid, &__ss_mask, CGROUP_SUBSYS_COUNT) {       \
515                 (ss) = cgroup_subsys[ssid];                             \
516                 {
517
518 #define while_each_subsys_mask()                                        \
519                 }                                                       \
520         }                                                               \
521 } while (false)
522
523 /* iterate over child cgrps, lock should be held throughout iteration */
524 #define cgroup_for_each_live_child(child, cgrp)                         \
525         list_for_each_entry((child), &(cgrp)->self.children, self.sibling) \
526                 if (({ lockdep_assert_held(&cgroup_mutex);              \
527                        cgroup_is_dead(child); }))                       \
528                         ;                                               \
529                 else
530
531 /* walk live descendants in preorder */
532 #define cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp)          \
533         css_for_each_descendant_pre((d_css), cgroup_css((cgrp), NULL))  \
534                 if (({ lockdep_assert_held(&cgroup_mutex);              \
535                        (dsct) = (d_css)->cgroup;                        \
536                        cgroup_is_dead(dsct); }))                        \
537                         ;                                               \
538                 else
539
540 /* walk live descendants in postorder */
541 #define cgroup_for_each_live_descendant_post(dsct, d_css, cgrp)         \
542         css_for_each_descendant_post((d_css), cgroup_css((cgrp), NULL)) \
543                 if (({ lockdep_assert_held(&cgroup_mutex);              \
544                        (dsct) = (d_css)->cgroup;                        \
545                        cgroup_is_dead(dsct); }))                        \
546                         ;                                               \
547                 else
548
549 /*
550  * The default css_set - used by init and its children prior to any
551  * hierarchies being mounted. It contains a pointer to the root state
552  * for each subsystem. Also used to anchor the list of css_sets. Not
553  * reference-counted, to improve performance when child cgroups
554  * haven't been created.
555  */
556 struct css_set init_css_set = {
557         .refcount               = ATOMIC_INIT(1),
558         .tasks                  = LIST_HEAD_INIT(init_css_set.tasks),
559         .mg_tasks               = LIST_HEAD_INIT(init_css_set.mg_tasks),
560         .task_iters             = LIST_HEAD_INIT(init_css_set.task_iters),
561         .cgrp_links             = LIST_HEAD_INIT(init_css_set.cgrp_links),
562         .mg_preload_node        = LIST_HEAD_INIT(init_css_set.mg_preload_node),
563         .mg_node                = LIST_HEAD_INIT(init_css_set.mg_node),
564 };
565
566 static int css_set_count        = 1;    /* 1 for init_css_set */
567
568 /**
569  * css_set_populated - does a css_set contain any tasks?
570  * @cset: target css_set
571  */
572 static bool css_set_populated(struct css_set *cset)
573 {
574         lockdep_assert_held(&css_set_lock);
575
576         return !list_empty(&cset->tasks) || !list_empty(&cset->mg_tasks);
577 }
578
579 /**
580  * cgroup_update_populated - updated populated count of a cgroup
581  * @cgrp: the target cgroup
582  * @populated: inc or dec populated count
583  *
584  * One of the css_sets associated with @cgrp is either getting its first
585  * task or losing the last.  Update @cgrp->populated_cnt accordingly.  The
586  * count is propagated towards root so that a given cgroup's populated_cnt
587  * is zero iff the cgroup and all its descendants don't contain any tasks.
588  *
589  * @cgrp's interface file "cgroup.populated" is zero if
590  * @cgrp->populated_cnt is zero and 1 otherwise.  When @cgrp->populated_cnt
591  * changes from or to zero, userland is notified that the content of the
592  * interface file has changed.  This can be used to detect when @cgrp and
593  * its descendants become populated or empty.
594  */
595 static void cgroup_update_populated(struct cgroup *cgrp, bool populated)
596 {
597         lockdep_assert_held(&css_set_lock);
598
599         do {
600                 bool trigger;
601
602                 if (populated)
603                         trigger = !cgrp->populated_cnt++;
604                 else
605                         trigger = !--cgrp->populated_cnt;
606
607                 if (!trigger)
608                         break;
609
610                 cgroup1_check_for_release(cgrp);
611                 cgroup_file_notify(&cgrp->events_file);
612
613                 cgrp = cgroup_parent(cgrp);
614         } while (cgrp);
615 }
616
617 /**
618  * css_set_update_populated - update populated state of a css_set
619  * @cset: target css_set
620  * @populated: whether @cset is populated or depopulated
621  *
622  * @cset is either getting the first task or losing the last.  Update the
623  * ->populated_cnt of all associated cgroups accordingly.
624  */
625 static void css_set_update_populated(struct css_set *cset, bool populated)
626 {
627         struct cgrp_cset_link *link;
628
629         lockdep_assert_held(&css_set_lock);
630
631         list_for_each_entry(link, &cset->cgrp_links, cgrp_link)
632                 cgroup_update_populated(link->cgrp, populated);
633 }
634
635 /**
636  * css_set_move_task - move a task from one css_set to another
637  * @task: task being moved
638  * @from_cset: css_set @task currently belongs to (may be NULL)
639  * @to_cset: new css_set @task is being moved to (may be NULL)
640  * @use_mg_tasks: move to @to_cset->mg_tasks instead of ->tasks
641  *
642  * Move @task from @from_cset to @to_cset.  If @task didn't belong to any
643  * css_set, @from_cset can be NULL.  If @task is being disassociated
644  * instead of moved, @to_cset can be NULL.
645  *
646  * This function automatically handles populated_cnt updates and
647  * css_task_iter adjustments but the caller is responsible for managing
648  * @from_cset and @to_cset's reference counts.
649  */
650 static void css_set_move_task(struct task_struct *task,
651                               struct css_set *from_cset, struct css_set *to_cset,
652                               bool use_mg_tasks)
653 {
654         lockdep_assert_held(&css_set_lock);
655
656         if (to_cset && !css_set_populated(to_cset))
657                 css_set_update_populated(to_cset, true);
658
659         if (from_cset) {
660                 struct css_task_iter *it, *pos;
661
662                 WARN_ON_ONCE(list_empty(&task->cg_list));
663
664                 /*
665                  * @task is leaving, advance task iterators which are
666                  * pointing to it so that they can resume at the next
667                  * position.  Advancing an iterator might remove it from
668                  * the list, use safe walk.  See css_task_iter_advance*()
669                  * for details.
670                  */
671                 list_for_each_entry_safe(it, pos, &from_cset->task_iters,
672                                          iters_node)
673                         if (it->task_pos == &task->cg_list)
674                                 css_task_iter_advance(it);
675
676                 list_del_init(&task->cg_list);
677                 if (!css_set_populated(from_cset))
678                         css_set_update_populated(from_cset, false);
679         } else {
680                 WARN_ON_ONCE(!list_empty(&task->cg_list));
681         }
682
683         if (to_cset) {
684                 /*
685                  * We are synchronized through cgroup_threadgroup_rwsem
686                  * against PF_EXITING setting such that we can't race
687                  * against cgroup_exit() changing the css_set to
688                  * init_css_set and dropping the old one.
689                  */
690                 WARN_ON_ONCE(task->flags & PF_EXITING);
691
692                 rcu_assign_pointer(task->cgroups, to_cset);
693                 list_add_tail(&task->cg_list, use_mg_tasks ? &to_cset->mg_tasks :
694                                                              &to_cset->tasks);
695         }
696 }
697
698 /*
699  * hash table for cgroup groups. This improves the performance to find
700  * an existing css_set. This hash doesn't (currently) take into
701  * account cgroups in empty hierarchies.
702  */
703 #define CSS_SET_HASH_BITS       7
704 static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
705
706 static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
707 {
708         unsigned long key = 0UL;
709         struct cgroup_subsys *ss;
710         int i;
711
712         for_each_subsys(ss, i)
713                 key += (unsigned long)css[i];
714         key = (key >> 16) ^ key;
715
716         return key;
717 }
718
719 void put_css_set_locked(struct css_set *cset)
720 {
721         struct cgrp_cset_link *link, *tmp_link;
722         struct cgroup_subsys *ss;
723         int ssid;
724
725         lockdep_assert_held(&css_set_lock);
726
727         if (!atomic_dec_and_test(&cset->refcount))
728                 return;
729
730         /* This css_set is dead. unlink it and release cgroup and css refs */
731         for_each_subsys(ss, ssid) {
732                 list_del(&cset->e_cset_node[ssid]);
733                 css_put(cset->subsys[ssid]);
734         }
735         hash_del(&cset->hlist);
736         css_set_count--;
737
738         list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
739                 list_del(&link->cset_link);
740                 list_del(&link->cgrp_link);
741                 if (cgroup_parent(link->cgrp))
742                         cgroup_put(link->cgrp);
743                 kfree(link);
744         }
745
746         kfree_rcu(cset, rcu_head);
747 }
748
749 /**
750  * compare_css_sets - helper function for find_existing_css_set().
751  * @cset: candidate css_set being tested
752  * @old_cset: existing css_set for a task
753  * @new_cgrp: cgroup that's being entered by the task
754  * @template: desired set of css pointers in css_set (pre-calculated)
755  *
756  * Returns true if "cset" matches "old_cset" except for the hierarchy
757  * which "new_cgrp" belongs to, for which it should match "new_cgrp".
758  */
759 static bool compare_css_sets(struct css_set *cset,
760                              struct css_set *old_cset,
761                              struct cgroup *new_cgrp,
762                              struct cgroup_subsys_state *template[])
763 {
764         struct list_head *l1, *l2;
765
766         /*
767          * On the default hierarchy, there can be csets which are
768          * associated with the same set of cgroups but different csses.
769          * Let's first ensure that csses match.
770          */
771         if (memcmp(template, cset->subsys, sizeof(cset->subsys)))
772                 return false;
773
774         /*
775          * Compare cgroup pointers in order to distinguish between
776          * different cgroups in hierarchies.  As different cgroups may
777          * share the same effective css, this comparison is always
778          * necessary.
779          */
780         l1 = &cset->cgrp_links;
781         l2 = &old_cset->cgrp_links;
782         while (1) {
783                 struct cgrp_cset_link *link1, *link2;
784                 struct cgroup *cgrp1, *cgrp2;
785
786                 l1 = l1->next;
787                 l2 = l2->next;
788                 /* See if we reached the end - both lists are equal length. */
789                 if (l1 == &cset->cgrp_links) {
790                         BUG_ON(l2 != &old_cset->cgrp_links);
791                         break;
792                 } else {
793                         BUG_ON(l2 == &old_cset->cgrp_links);
794                 }
795                 /* Locate the cgroups associated with these links. */
796                 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
797                 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
798                 cgrp1 = link1->cgrp;
799                 cgrp2 = link2->cgrp;
800                 /* Hierarchies should be linked in the same order. */
801                 BUG_ON(cgrp1->root != cgrp2->root);
802
803                 /*
804                  * If this hierarchy is the hierarchy of the cgroup
805                  * that's changing, then we need to check that this
806                  * css_set points to the new cgroup; if it's any other
807                  * hierarchy, then this css_set should point to the
808                  * same cgroup as the old css_set.
809                  */
810                 if (cgrp1->root == new_cgrp->root) {
811                         if (cgrp1 != new_cgrp)
812                                 return false;
813                 } else {
814                         if (cgrp1 != cgrp2)
815                                 return false;
816                 }
817         }
818         return true;
819 }
820
821 /**
822  * find_existing_css_set - init css array and find the matching css_set
823  * @old_cset: the css_set that we're using before the cgroup transition
824  * @cgrp: the cgroup that we're moving into
825  * @template: out param for the new set of csses, should be clear on entry
826  */
827 static struct css_set *find_existing_css_set(struct css_set *old_cset,
828                                         struct cgroup *cgrp,
829                                         struct cgroup_subsys_state *template[])
830 {
831         struct cgroup_root *root = cgrp->root;
832         struct cgroup_subsys *ss;
833         struct css_set *cset;
834         unsigned long key;
835         int i;
836
837         /*
838          * Build the set of subsystem state objects that we want to see in the
839          * new css_set. while subsystems can change globally, the entries here
840          * won't change, so no need for locking.
841          */
842         for_each_subsys(ss, i) {
843                 if (root->subsys_mask & (1UL << i)) {
844                         /*
845                          * @ss is in this hierarchy, so we want the
846                          * effective css from @cgrp.
847                          */
848                         template[i] = cgroup_e_css(cgrp, ss);
849                 } else {
850                         /*
851                          * @ss is not in this hierarchy, so we don't want
852                          * to change the css.
853                          */
854                         template[i] = old_cset->subsys[i];
855                 }
856         }
857
858         key = css_set_hash(template);
859         hash_for_each_possible(css_set_table, cset, hlist, key) {
860                 if (!compare_css_sets(cset, old_cset, cgrp, template))
861                         continue;
862
863                 /* This css_set matches what we need */
864                 return cset;
865         }
866
867         /* No existing cgroup group matched */
868         return NULL;
869 }
870
871 static void free_cgrp_cset_links(struct list_head *links_to_free)
872 {
873         struct cgrp_cset_link *link, *tmp_link;
874
875         list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
876                 list_del(&link->cset_link);
877                 kfree(link);
878         }
879 }
880
881 /**
882  * allocate_cgrp_cset_links - allocate cgrp_cset_links
883  * @count: the number of links to allocate
884  * @tmp_links: list_head the allocated links are put on
885  *
886  * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
887  * through ->cset_link.  Returns 0 on success or -errno.
888  */
889 static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
890 {
891         struct cgrp_cset_link *link;
892         int i;
893
894         INIT_LIST_HEAD(tmp_links);
895
896         for (i = 0; i < count; i++) {
897                 link = kzalloc(sizeof(*link), GFP_KERNEL);
898                 if (!link) {
899                         free_cgrp_cset_links(tmp_links);
900                         return -ENOMEM;
901                 }
902                 list_add(&link->cset_link, tmp_links);
903         }
904         return 0;
905 }
906
907 /**
908  * link_css_set - a helper function to link a css_set to a cgroup
909  * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
910  * @cset: the css_set to be linked
911  * @cgrp: the destination cgroup
912  */
913 static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
914                          struct cgroup *cgrp)
915 {
916         struct cgrp_cset_link *link;
917
918         BUG_ON(list_empty(tmp_links));
919
920         if (cgroup_on_dfl(cgrp))
921                 cset->dfl_cgrp = cgrp;
922
923         link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
924         link->cset = cset;
925         link->cgrp = cgrp;
926
927         /*
928          * Always add links to the tail of the lists so that the lists are
929          * in choronological order.
930          */
931         list_move_tail(&link->cset_link, &cgrp->cset_links);
932         list_add_tail(&link->cgrp_link, &cset->cgrp_links);
933
934         if (cgroup_parent(cgrp))
935                 cgroup_get(cgrp);
936 }
937
938 /**
939  * find_css_set - return a new css_set with one cgroup updated
940  * @old_cset: the baseline css_set
941  * @cgrp: the cgroup to be updated
942  *
943  * Return a new css_set that's equivalent to @old_cset, but with @cgrp
944  * substituted into the appropriate hierarchy.
945  */
946 static struct css_set *find_css_set(struct css_set *old_cset,
947                                     struct cgroup *cgrp)
948 {
949         struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
950         struct css_set *cset;
951         struct list_head tmp_links;
952         struct cgrp_cset_link *link;
953         struct cgroup_subsys *ss;
954         unsigned long key;
955         int ssid;
956
957         lockdep_assert_held(&cgroup_mutex);
958
959         /* First see if we already have a cgroup group that matches
960          * the desired set */
961         spin_lock_irq(&css_set_lock);
962         cset = find_existing_css_set(old_cset, cgrp, template);
963         if (cset)
964                 get_css_set(cset);
965         spin_unlock_irq(&css_set_lock);
966
967         if (cset)
968                 return cset;
969
970         cset = kzalloc(sizeof(*cset), GFP_KERNEL);
971         if (!cset)
972                 return NULL;
973
974         /* Allocate all the cgrp_cset_link objects that we'll need */
975         if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
976                 kfree(cset);
977                 return NULL;
978         }
979
980         atomic_set(&cset->refcount, 1);
981         INIT_LIST_HEAD(&cset->tasks);
982         INIT_LIST_HEAD(&cset->mg_tasks);
983         INIT_LIST_HEAD(&cset->task_iters);
984         INIT_HLIST_NODE(&cset->hlist);
985         INIT_LIST_HEAD(&cset->cgrp_links);
986         INIT_LIST_HEAD(&cset->mg_preload_node);
987         INIT_LIST_HEAD(&cset->mg_node);
988
989         /* Copy the set of subsystem state objects generated in
990          * find_existing_css_set() */
991         memcpy(cset->subsys, template, sizeof(cset->subsys));
992
993         spin_lock_irq(&css_set_lock);
994         /* Add reference counts and links from the new css_set. */
995         list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
996                 struct cgroup *c = link->cgrp;
997
998                 if (c->root == cgrp->root)
999                         c = cgrp;
1000                 link_css_set(&tmp_links, cset, c);
1001         }
1002
1003         BUG_ON(!list_empty(&tmp_links));
1004
1005         css_set_count++;
1006
1007         /* Add @cset to the hash table */
1008         key = css_set_hash(cset->subsys);
1009         hash_add(css_set_table, &cset->hlist, key);
1010
1011         for_each_subsys(ss, ssid) {
1012                 struct cgroup_subsys_state *css = cset->subsys[ssid];
1013
1014                 list_add_tail(&cset->e_cset_node[ssid],
1015                               &css->cgroup->e_csets[ssid]);
1016                 css_get(css);
1017         }
1018
1019         spin_unlock_irq(&css_set_lock);
1020
1021         return cset;
1022 }
1023
1024 struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root)
1025 {
1026         struct cgroup *root_cgrp = kf_root->kn->priv;
1027
1028         return root_cgrp->root;
1029 }
1030
1031 static int cgroup_init_root_id(struct cgroup_root *root)
1032 {
1033         int id;
1034
1035         lockdep_assert_held(&cgroup_mutex);
1036
1037         id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 0, 0, GFP_KERNEL);
1038         if (id < 0)
1039                 return id;
1040
1041         root->hierarchy_id = id;
1042         return 0;
1043 }
1044
1045 static void cgroup_exit_root_id(struct cgroup_root *root)
1046 {
1047         lockdep_assert_held(&cgroup_mutex);
1048
1049         idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
1050 }
1051
1052 void cgroup_free_root(struct cgroup_root *root)
1053 {
1054         if (root) {
1055                 idr_destroy(&root->cgroup_idr);
1056                 kfree(root);
1057         }
1058 }
1059
1060 static void cgroup_destroy_root(struct cgroup_root *root)
1061 {
1062         struct cgroup *cgrp = &root->cgrp;
1063         struct cgrp_cset_link *link, *tmp_link;
1064
1065         trace_cgroup_destroy_root(root);
1066
1067         cgroup_lock_and_drain_offline(&cgrp_dfl_root.cgrp);
1068
1069         BUG_ON(atomic_read(&root->nr_cgrps));
1070         BUG_ON(!list_empty(&cgrp->self.children));
1071
1072         /* Rebind all subsystems back to the default hierarchy */
1073         WARN_ON(rebind_subsystems(&cgrp_dfl_root, root->subsys_mask));
1074
1075         /*
1076          * Release all the links from cset_links to this hierarchy's
1077          * root cgroup
1078          */
1079         spin_lock_irq(&css_set_lock);
1080
1081         list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
1082                 list_del(&link->cset_link);
1083                 list_del(&link->cgrp_link);
1084                 kfree(link);
1085         }
1086
1087         spin_unlock_irq(&css_set_lock);
1088
1089         if (!list_empty(&root->root_list)) {
1090                 list_del(&root->root_list);
1091                 cgroup_root_count--;
1092         }
1093
1094         cgroup_exit_root_id(root);
1095
1096         mutex_unlock(&cgroup_mutex);
1097
1098         kernfs_destroy_root(root->kf_root);
1099         cgroup_free_root(root);
1100 }
1101
1102 /*
1103  * look up cgroup associated with current task's cgroup namespace on the
1104  * specified hierarchy
1105  */
1106 static struct cgroup *
1107 current_cgns_cgroup_from_root(struct cgroup_root *root)
1108 {
1109         struct cgroup *res = NULL;
1110         struct css_set *cset;
1111
1112         lockdep_assert_held(&css_set_lock);
1113
1114         rcu_read_lock();
1115
1116         cset = current->nsproxy->cgroup_ns->root_cset;
1117         if (cset == &init_css_set) {
1118                 res = &root->cgrp;
1119         } else {
1120                 struct cgrp_cset_link *link;
1121
1122                 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
1123                         struct cgroup *c = link->cgrp;
1124
1125                         if (c->root == root) {
1126                                 res = c;
1127                                 break;
1128                         }
1129                 }
1130         }
1131         rcu_read_unlock();
1132
1133         BUG_ON(!res);
1134         return res;
1135 }
1136
1137 /* look up cgroup associated with given css_set on the specified hierarchy */
1138 static struct cgroup *cset_cgroup_from_root(struct css_set *cset,
1139                                             struct cgroup_root *root)
1140 {
1141         struct cgroup *res = NULL;
1142
1143         lockdep_assert_held(&cgroup_mutex);
1144         lockdep_assert_held(&css_set_lock);
1145
1146         if (cset == &init_css_set) {
1147                 res = &root->cgrp;
1148         } else {
1149                 struct cgrp_cset_link *link;
1150
1151                 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
1152                         struct cgroup *c = link->cgrp;
1153
1154                         if (c->root == root) {
1155                                 res = c;
1156                                 break;
1157                         }
1158                 }
1159         }
1160
1161         BUG_ON(!res);
1162         return res;
1163 }
1164
1165 /*
1166  * Return the cgroup for "task" from the given hierarchy. Must be
1167  * called with cgroup_mutex and css_set_lock held.
1168  */
1169 struct cgroup *task_cgroup_from_root(struct task_struct *task,
1170                                      struct cgroup_root *root)
1171 {
1172         /*
1173          * No need to lock the task - since we hold cgroup_mutex the
1174          * task can't change groups, so the only thing that can happen
1175          * is that it exits and its css is set back to init_css_set.
1176          */
1177         return cset_cgroup_from_root(task_css_set(task), root);
1178 }
1179
1180 /*
1181  * A task must hold cgroup_mutex to modify cgroups.
1182  *
1183  * Any task can increment and decrement the count field without lock.
1184  * So in general, code holding cgroup_mutex can't rely on the count
1185  * field not changing.  However, if the count goes to zero, then only
1186  * cgroup_attach_task() can increment it again.  Because a count of zero
1187  * means that no tasks are currently attached, therefore there is no
1188  * way a task attached to that cgroup can fork (the other way to
1189  * increment the count).  So code holding cgroup_mutex can safely
1190  * assume that if the count is zero, it will stay zero. Similarly, if
1191  * a task holds cgroup_mutex on a cgroup with zero count, it
1192  * knows that the cgroup won't be removed, as cgroup_rmdir()
1193  * needs that mutex.
1194  *
1195  * A cgroup can only be deleted if both its 'count' of using tasks
1196  * is zero, and its list of 'children' cgroups is empty.  Since all
1197  * tasks in the system use _some_ cgroup, and since there is always at
1198  * least one task in the system (init, pid == 1), therefore, root cgroup
1199  * always has either children cgroups and/or using tasks.  So we don't
1200  * need a special hack to ensure that root cgroup cannot be deleted.
1201  *
1202  * P.S.  One more locking exception.  RCU is used to guard the
1203  * update of a tasks cgroup pointer by cgroup_attach_task()
1204  */
1205
1206 static struct kernfs_syscall_ops cgroup_kf_syscall_ops;
1207
1208 static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft,
1209                               char *buf)
1210 {
1211         struct cgroup_subsys *ss = cft->ss;
1212
1213         if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
1214             !(cgrp->root->flags & CGRP_ROOT_NOPREFIX))
1215                 snprintf(buf, CGROUP_FILE_NAME_MAX, "%s.%s",
1216                          cgroup_on_dfl(cgrp) ? ss->name : ss->legacy_name,
1217                          cft->name);
1218         else
1219                 strncpy(buf, cft->name, CGROUP_FILE_NAME_MAX);
1220         return buf;
1221 }
1222
1223 /**
1224  * cgroup_file_mode - deduce file mode of a control file
1225  * @cft: the control file in question
1226  *
1227  * S_IRUGO for read, S_IWUSR for write.
1228  */
1229 static umode_t cgroup_file_mode(const struct cftype *cft)
1230 {
1231         umode_t mode = 0;
1232
1233         if (cft->read_u64 || cft->read_s64 || cft->seq_show)
1234                 mode |= S_IRUGO;
1235
1236         if (cft->write_u64 || cft->write_s64 || cft->write) {
1237                 if (cft->flags & CFTYPE_WORLD_WRITABLE)
1238                         mode |= S_IWUGO;
1239                 else
1240                         mode |= S_IWUSR;
1241         }
1242
1243         return mode;
1244 }
1245
1246 /**
1247  * cgroup_calc_subtree_ss_mask - calculate subtree_ss_mask
1248  * @subtree_control: the new subtree_control mask to consider
1249  * @this_ss_mask: available subsystems
1250  *
1251  * On the default hierarchy, a subsystem may request other subsystems to be
1252  * enabled together through its ->depends_on mask.  In such cases, more
1253  * subsystems than specified in "cgroup.subtree_control" may be enabled.
1254  *
1255  * This function calculates which subsystems need to be enabled if
1256  * @subtree_control is to be applied while restricted to @this_ss_mask.
1257  */
1258 static u16 cgroup_calc_subtree_ss_mask(u16 subtree_control, u16 this_ss_mask)
1259 {
1260         u16 cur_ss_mask = subtree_control;
1261         struct cgroup_subsys *ss;
1262         int ssid;
1263
1264         lockdep_assert_held(&cgroup_mutex);
1265
1266         cur_ss_mask |= cgrp_dfl_implicit_ss_mask;
1267
1268         while (true) {
1269                 u16 new_ss_mask = cur_ss_mask;
1270
1271                 do_each_subsys_mask(ss, ssid, cur_ss_mask) {
1272                         new_ss_mask |= ss->depends_on;
1273                 } while_each_subsys_mask();
1274
1275                 /*
1276                  * Mask out subsystems which aren't available.  This can
1277                  * happen only if some depended-upon subsystems were bound
1278                  * to non-default hierarchies.
1279                  */
1280                 new_ss_mask &= this_ss_mask;
1281
1282                 if (new_ss_mask == cur_ss_mask)
1283                         break;
1284                 cur_ss_mask = new_ss_mask;
1285         }
1286
1287         return cur_ss_mask;
1288 }
1289
1290 /**
1291  * cgroup_kn_unlock - unlocking helper for cgroup kernfs methods
1292  * @kn: the kernfs_node being serviced
1293  *
1294  * This helper undoes cgroup_kn_lock_live() and should be invoked before
1295  * the method finishes if locking succeeded.  Note that once this function
1296  * returns the cgroup returned by cgroup_kn_lock_live() may become
1297  * inaccessible any time.  If the caller intends to continue to access the
1298  * cgroup, it should pin it before invoking this function.
1299  */
1300 void cgroup_kn_unlock(struct kernfs_node *kn)
1301 {
1302         struct cgroup *cgrp;
1303
1304         if (kernfs_type(kn) == KERNFS_DIR)
1305                 cgrp = kn->priv;
1306         else
1307                 cgrp = kn->parent->priv;
1308
1309         mutex_unlock(&cgroup_mutex);
1310
1311         kernfs_unbreak_active_protection(kn);
1312         cgroup_put(cgrp);
1313 }
1314
1315 /**
1316  * cgroup_kn_lock_live - locking helper for cgroup kernfs methods
1317  * @kn: the kernfs_node being serviced
1318  * @drain_offline: perform offline draining on the cgroup
1319  *
1320  * This helper is to be used by a cgroup kernfs method currently servicing
1321  * @kn.  It breaks the active protection, performs cgroup locking and
1322  * verifies that the associated cgroup is alive.  Returns the cgroup if
1323  * alive; otherwise, %NULL.  A successful return should be undone by a
1324  * matching cgroup_kn_unlock() invocation.  If @drain_offline is %true, the
1325  * cgroup is drained of offlining csses before return.
1326  *
1327  * Any cgroup kernfs method implementation which requires locking the
1328  * associated cgroup should use this helper.  It avoids nesting cgroup
1329  * locking under kernfs active protection and allows all kernfs operations
1330  * including self-removal.
1331  */
1332 struct cgroup *cgroup_kn_lock_live(struct kernfs_node *kn, bool drain_offline)
1333 {
1334         struct cgroup *cgrp;
1335
1336         if (kernfs_type(kn) == KERNFS_DIR)
1337                 cgrp = kn->priv;
1338         else
1339                 cgrp = kn->parent->priv;
1340
1341         /*
1342          * We're gonna grab cgroup_mutex which nests outside kernfs
1343          * active_ref.  cgroup liveliness check alone provides enough
1344          * protection against removal.  Ensure @cgrp stays accessible and
1345          * break the active_ref protection.
1346          */
1347         if (!cgroup_tryget(cgrp))
1348                 return NULL;
1349         kernfs_break_active_protection(kn);
1350
1351         if (drain_offline)
1352                 cgroup_lock_and_drain_offline(cgrp);
1353         else
1354                 mutex_lock(&cgroup_mutex);
1355
1356         if (!cgroup_is_dead(cgrp))
1357                 return cgrp;
1358
1359         cgroup_kn_unlock(kn);
1360         return NULL;
1361 }
1362
1363 static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
1364 {
1365         char name[CGROUP_FILE_NAME_MAX];
1366
1367         lockdep_assert_held(&cgroup_mutex);
1368
1369         if (cft->file_offset) {
1370                 struct cgroup_subsys_state *css = cgroup_css(cgrp, cft->ss);
1371                 struct cgroup_file *cfile = (void *)css + cft->file_offset;
1372
1373                 spin_lock_irq(&cgroup_file_kn_lock);
1374                 cfile->kn = NULL;
1375                 spin_unlock_irq(&cgroup_file_kn_lock);
1376         }
1377
1378         kernfs_remove_by_name(cgrp->kn, cgroup_file_name(cgrp, cft, name));
1379 }
1380
1381 /**
1382  * css_clear_dir - remove subsys files in a cgroup directory
1383  * @css: taget css
1384  */
1385 static void css_clear_dir(struct cgroup_subsys_state *css)
1386 {
1387         struct cgroup *cgrp = css->cgroup;
1388         struct cftype *cfts;
1389
1390         if (!(css->flags & CSS_VISIBLE))
1391                 return;
1392
1393         css->flags &= ~CSS_VISIBLE;
1394
1395         list_for_each_entry(cfts, &css->ss->cfts, node)
1396                 cgroup_addrm_files(css, cgrp, cfts, false);
1397 }
1398
1399 /**
1400  * css_populate_dir - create subsys files in a cgroup directory
1401  * @css: target css
1402  *
1403  * On failure, no file is added.
1404  */
1405 static int css_populate_dir(struct cgroup_subsys_state *css)
1406 {
1407         struct cgroup *cgrp = css->cgroup;
1408         struct cftype *cfts, *failed_cfts;
1409         int ret;
1410
1411         if ((css->flags & CSS_VISIBLE) || !cgrp->kn)
1412                 return 0;
1413
1414         if (!css->ss) {
1415                 if (cgroup_on_dfl(cgrp))
1416                         cfts = cgroup_base_files;
1417                 else
1418                         cfts = cgroup1_base_files;
1419
1420                 return cgroup_addrm_files(&cgrp->self, cgrp, cfts, true);
1421         }
1422
1423         list_for_each_entry(cfts, &css->ss->cfts, node) {
1424                 ret = cgroup_addrm_files(css, cgrp, cfts, true);
1425                 if (ret < 0) {
1426                         failed_cfts = cfts;
1427                         goto err;
1428                 }
1429         }
1430
1431         css->flags |= CSS_VISIBLE;
1432
1433         return 0;
1434 err:
1435         list_for_each_entry(cfts, &css->ss->cfts, node) {
1436                 if (cfts == failed_cfts)
1437                         break;
1438                 cgroup_addrm_files(css, cgrp, cfts, false);
1439         }
1440         return ret;
1441 }
1442
1443 int rebind_subsystems(struct cgroup_root *dst_root, u16 ss_mask)
1444 {
1445         struct cgroup *dcgrp = &dst_root->cgrp;
1446         struct cgroup_subsys *ss;
1447         int ssid, i, ret;
1448
1449         lockdep_assert_held(&cgroup_mutex);
1450
1451         do_each_subsys_mask(ss, ssid, ss_mask) {
1452                 /*
1453                  * If @ss has non-root csses attached to it, can't move.
1454                  * If @ss is an implicit controller, it is exempt from this
1455                  * rule and can be stolen.
1456                  */
1457                 if (css_next_child(NULL, cgroup_css(&ss->root->cgrp, ss)) &&
1458                     !ss->implicit_on_dfl)
1459                         return -EBUSY;
1460
1461                 /* can't move between two non-dummy roots either */
1462                 if (ss->root != &cgrp_dfl_root && dst_root != &cgrp_dfl_root)
1463                         return -EBUSY;
1464         } while_each_subsys_mask();
1465
1466         do_each_subsys_mask(ss, ssid, ss_mask) {
1467                 struct cgroup_root *src_root = ss->root;
1468                 struct cgroup *scgrp = &src_root->cgrp;
1469                 struct cgroup_subsys_state *css = cgroup_css(scgrp, ss);
1470                 struct css_set *cset;
1471
1472                 WARN_ON(!css || cgroup_css(dcgrp, ss));
1473
1474                 /* disable from the source */
1475                 src_root->subsys_mask &= ~(1 << ssid);
1476                 WARN_ON(cgroup_apply_control(scgrp));
1477                 cgroup_finalize_control(scgrp, 0);
1478
1479                 /* rebind */
1480                 RCU_INIT_POINTER(scgrp->subsys[ssid], NULL);
1481                 rcu_assign_pointer(dcgrp->subsys[ssid], css);
1482                 ss->root = dst_root;
1483                 css->cgroup = dcgrp;
1484
1485                 spin_lock_irq(&css_set_lock);
1486                 hash_for_each(css_set_table, i, cset, hlist)
1487                         list_move_tail(&cset->e_cset_node[ss->id],
1488                                        &dcgrp->e_csets[ss->id]);
1489                 spin_unlock_irq(&css_set_lock);
1490
1491                 /* default hierarchy doesn't enable controllers by default */
1492                 dst_root->subsys_mask |= 1 << ssid;
1493                 if (dst_root == &cgrp_dfl_root) {
1494                         static_branch_enable(cgroup_subsys_on_dfl_key[ssid]);
1495                 } else {
1496                         dcgrp->subtree_control |= 1 << ssid;
1497                         static_branch_disable(cgroup_subsys_on_dfl_key[ssid]);
1498                 }
1499
1500                 ret = cgroup_apply_control(dcgrp);
1501                 if (ret)
1502                         pr_warn("partial failure to rebind %s controller (err=%d)\n",
1503                                 ss->name, ret);
1504
1505                 if (ss->bind)
1506                         ss->bind(css);
1507         } while_each_subsys_mask();
1508
1509         kernfs_activate(dcgrp->kn);
1510         return 0;
1511 }
1512
1513 int cgroup_show_path(struct seq_file *sf, struct kernfs_node *kf_node,
1514                      struct kernfs_root *kf_root)
1515 {
1516         int len = 0;
1517         char *buf = NULL;
1518         struct cgroup_root *kf_cgroot = cgroup_root_from_kf(kf_root);
1519         struct cgroup *ns_cgroup;
1520
1521         buf = kmalloc(PATH_MAX, GFP_KERNEL);
1522         if (!buf)
1523                 return -ENOMEM;
1524
1525         spin_lock_irq(&css_set_lock);
1526         ns_cgroup = current_cgns_cgroup_from_root(kf_cgroot);
1527         len = kernfs_path_from_node(kf_node, ns_cgroup->kn, buf, PATH_MAX);
1528         spin_unlock_irq(&css_set_lock);
1529
1530         if (len >= PATH_MAX)
1531                 len = -ERANGE;
1532         else if (len > 0) {
1533                 seq_escape(sf, buf, " \t\n\\");
1534                 len = 0;
1535         }
1536         kfree(buf);
1537         return len;
1538 }
1539
1540 static int cgroup_remount(struct kernfs_root *kf_root, int *flags, char *data)
1541 {
1542         pr_err("remount is not allowed\n");
1543         return -EINVAL;
1544 }
1545
1546 /*
1547  * To reduce the fork() overhead for systems that are not actually using
1548  * their cgroups capability, we don't maintain the lists running through
1549  * each css_set to its tasks until we see the list actually used - in other
1550  * words after the first mount.
1551  */
1552 static bool use_task_css_set_links __read_mostly;
1553
1554 static void cgroup_enable_task_cg_lists(void)
1555 {
1556         struct task_struct *p, *g;
1557
1558         spin_lock_irq(&css_set_lock);
1559
1560         if (use_task_css_set_links)
1561                 goto out_unlock;
1562
1563         use_task_css_set_links = true;
1564
1565         /*
1566          * We need tasklist_lock because RCU is not safe against
1567          * while_each_thread(). Besides, a forking task that has passed
1568          * cgroup_post_fork() without seeing use_task_css_set_links = 1
1569          * is not guaranteed to have its child immediately visible in the
1570          * tasklist if we walk through it with RCU.
1571          */
1572         read_lock(&tasklist_lock);
1573         do_each_thread(g, p) {
1574                 WARN_ON_ONCE(!list_empty(&p->cg_list) ||
1575                              task_css_set(p) != &init_css_set);
1576
1577                 /*
1578                  * We should check if the process is exiting, otherwise
1579                  * it will race with cgroup_exit() in that the list
1580                  * entry won't be deleted though the process has exited.
1581                  * Do it while holding siglock so that we don't end up
1582                  * racing against cgroup_exit().
1583                  *
1584                  * Interrupts were already disabled while acquiring
1585                  * the css_set_lock, so we do not need to disable it
1586                  * again when acquiring the sighand->siglock here.
1587                  */
1588                 spin_lock(&p->sighand->siglock);
1589                 if (!(p->flags & PF_EXITING)) {
1590                         struct css_set *cset = task_css_set(p);
1591
1592                         if (!css_set_populated(cset))
1593                                 css_set_update_populated(cset, true);
1594                         list_add_tail(&p->cg_list, &cset->tasks);
1595                         get_css_set(cset);
1596                 }
1597                 spin_unlock(&p->sighand->siglock);
1598         } while_each_thread(g, p);
1599         read_unlock(&tasklist_lock);
1600 out_unlock:
1601         spin_unlock_irq(&css_set_lock);
1602 }
1603
1604 static void init_cgroup_housekeeping(struct cgroup *cgrp)
1605 {
1606         struct cgroup_subsys *ss;
1607         int ssid;
1608
1609         INIT_LIST_HEAD(&cgrp->self.sibling);
1610         INIT_LIST_HEAD(&cgrp->self.children);
1611         INIT_LIST_HEAD(&cgrp->cset_links);
1612         INIT_LIST_HEAD(&cgrp->pidlists);
1613         mutex_init(&cgrp->pidlist_mutex);
1614         cgrp->self.cgroup = cgrp;
1615         cgrp->self.flags |= CSS_ONLINE;
1616
1617         for_each_subsys(ss, ssid)
1618                 INIT_LIST_HEAD(&cgrp->e_csets[ssid]);
1619
1620         init_waitqueue_head(&cgrp->offline_waitq);
1621         INIT_WORK(&cgrp->release_agent_work, cgroup1_release_agent);
1622 }
1623
1624 void init_cgroup_root(struct cgroup_root *root, struct cgroup_sb_opts *opts)
1625 {
1626         struct cgroup *cgrp = &root->cgrp;
1627
1628         INIT_LIST_HEAD(&root->root_list);
1629         atomic_set(&root->nr_cgrps, 1);
1630         cgrp->root = root;
1631         init_cgroup_housekeeping(cgrp);
1632         idr_init(&root->cgroup_idr);
1633
1634         root->flags = opts->flags;
1635         if (opts->release_agent)
1636                 strcpy(root->release_agent_path, opts->release_agent);
1637         if (opts->name)
1638                 strcpy(root->name, opts->name);
1639         if (opts->cpuset_clone_children)
1640                 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags);
1641 }
1642
1643 int cgroup_setup_root(struct cgroup_root *root, u16 ss_mask)
1644 {
1645         LIST_HEAD(tmp_links);
1646         struct cgroup *root_cgrp = &root->cgrp;
1647         struct kernfs_syscall_ops *kf_sops;
1648         struct css_set *cset;
1649         int i, ret;
1650
1651         lockdep_assert_held(&cgroup_mutex);
1652
1653         ret = cgroup_idr_alloc(&root->cgroup_idr, root_cgrp, 1, 2, GFP_KERNEL);
1654         if (ret < 0)
1655                 goto out;
1656         root_cgrp->id = ret;
1657         root_cgrp->ancestor_ids[0] = ret;
1658
1659         ret = percpu_ref_init(&root_cgrp->self.refcnt, css_release, 0,
1660                               GFP_KERNEL);
1661         if (ret)
1662                 goto out;
1663
1664         /*
1665          * We're accessing css_set_count without locking css_set_lock here,
1666          * but that's OK - it can only be increased by someone holding
1667          * cgroup_lock, and that's us.  Later rebinding may disable
1668          * controllers on the default hierarchy and thus create new csets,
1669          * which can't be more than the existing ones.  Allocate 2x.
1670          */
1671         ret = allocate_cgrp_cset_links(2 * css_set_count, &tmp_links);
1672         if (ret)
1673                 goto cancel_ref;
1674
1675         ret = cgroup_init_root_id(root);
1676         if (ret)
1677                 goto cancel_ref;
1678
1679         kf_sops = root == &cgrp_dfl_root ?
1680                 &cgroup_kf_syscall_ops : &cgroup1_kf_syscall_ops;
1681
1682         root->kf_root = kernfs_create_root(kf_sops,
1683                                            KERNFS_ROOT_CREATE_DEACTIVATED,
1684                                            root_cgrp);
1685         if (IS_ERR(root->kf_root)) {
1686                 ret = PTR_ERR(root->kf_root);
1687                 goto exit_root_id;
1688         }
1689         root_cgrp->kn = root->kf_root->kn;
1690
1691         ret = css_populate_dir(&root_cgrp->self);
1692         if (ret)
1693                 goto destroy_root;
1694
1695         ret = rebind_subsystems(root, ss_mask);
1696         if (ret)
1697                 goto destroy_root;
1698
1699         trace_cgroup_setup_root(root);
1700
1701         /*
1702          * There must be no failure case after here, since rebinding takes
1703          * care of subsystems' refcounts, which are explicitly dropped in
1704          * the failure exit path.
1705          */
1706         list_add(&root->root_list, &cgroup_roots);
1707         cgroup_root_count++;
1708
1709         /*
1710          * Link the root cgroup in this hierarchy into all the css_set
1711          * objects.
1712          */
1713         spin_lock_irq(&css_set_lock);
1714         hash_for_each(css_set_table, i, cset, hlist) {
1715                 link_css_set(&tmp_links, cset, root_cgrp);
1716                 if (css_set_populated(cset))
1717                         cgroup_update_populated(root_cgrp, true);
1718         }
1719         spin_unlock_irq(&css_set_lock);
1720
1721         BUG_ON(!list_empty(&root_cgrp->self.children));
1722         BUG_ON(atomic_read(&root->nr_cgrps) != 1);
1723
1724         kernfs_activate(root_cgrp->kn);
1725         ret = 0;
1726         goto out;
1727
1728 destroy_root:
1729         kernfs_destroy_root(root->kf_root);
1730         root->kf_root = NULL;
1731 exit_root_id:
1732         cgroup_exit_root_id(root);
1733 cancel_ref:
1734         percpu_ref_exit(&root_cgrp->self.refcnt);
1735 out:
1736         free_cgrp_cset_links(&tmp_links);
1737         return ret;
1738 }
1739
1740 struct dentry *cgroup_do_mount(struct file_system_type *fs_type, int flags,
1741                                struct cgroup_root *root, unsigned long magic,
1742                                struct cgroup_namespace *ns)
1743 {
1744         struct dentry *dentry;
1745         bool new_sb;
1746
1747         dentry = kernfs_mount(fs_type, flags, root->kf_root, magic, &new_sb);
1748
1749         /*
1750          * In non-init cgroup namespace, instead of root cgroup's dentry,
1751          * we return the dentry corresponding to the cgroupns->root_cgrp.
1752          */
1753         if (!IS_ERR(dentry) && ns != &init_cgroup_ns) {
1754                 struct dentry *nsdentry;
1755                 struct cgroup *cgrp;
1756
1757                 mutex_lock(&cgroup_mutex);
1758                 spin_lock_irq(&css_set_lock);
1759
1760                 cgrp = cset_cgroup_from_root(ns->root_cset, root);
1761
1762                 spin_unlock_irq(&css_set_lock);
1763                 mutex_unlock(&cgroup_mutex);
1764
1765                 nsdentry = kernfs_node_dentry(cgrp->kn, dentry->d_sb);
1766                 dput(dentry);
1767                 dentry = nsdentry;
1768         }
1769
1770         if (IS_ERR(dentry) || !new_sb)
1771                 cgroup_put(&root->cgrp);
1772
1773         return dentry;
1774 }
1775
1776 static struct dentry *cgroup_mount(struct file_system_type *fs_type,
1777                          int flags, const char *unused_dev_name,
1778                          void *data)
1779 {
1780         struct cgroup_namespace *ns = current->nsproxy->cgroup_ns;
1781         struct dentry *dentry;
1782
1783         get_cgroup_ns(ns);
1784
1785         /* Check if the caller has permission to mount. */
1786         if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN)) {
1787                 put_cgroup_ns(ns);
1788                 return ERR_PTR(-EPERM);
1789         }
1790
1791         /*
1792          * The first time anyone tries to mount a cgroup, enable the list
1793          * linking each css_set to its tasks and fix up all existing tasks.
1794          */
1795         if (!use_task_css_set_links)
1796                 cgroup_enable_task_cg_lists();
1797
1798         if (fs_type == &cgroup2_fs_type) {
1799                 if (data) {
1800                         pr_err("cgroup2: unknown option \"%s\"\n", (char *)data);
1801                         put_cgroup_ns(ns);
1802                         return ERR_PTR(-EINVAL);
1803                 }
1804                 cgrp_dfl_visible = true;
1805                 cgroup_get(&cgrp_dfl_root.cgrp);
1806
1807                 dentry = cgroup_do_mount(&cgroup2_fs_type, flags, &cgrp_dfl_root,
1808                                          CGROUP2_SUPER_MAGIC, ns);
1809         } else {
1810                 dentry = cgroup1_mount(&cgroup_fs_type, flags, data,
1811                                        CGROUP_SUPER_MAGIC, ns);
1812         }
1813
1814         put_cgroup_ns(ns);
1815         return dentry;
1816 }
1817
1818 static void cgroup_kill_sb(struct super_block *sb)
1819 {
1820         struct kernfs_root *kf_root = kernfs_root_from_sb(sb);
1821         struct cgroup_root *root = cgroup_root_from_kf(kf_root);
1822
1823         /*
1824          * If @root doesn't have any mounts or children, start killing it.
1825          * This prevents new mounts by disabling percpu_ref_tryget_live().
1826          * cgroup_mount() may wait for @root's release.
1827          *
1828          * And don't kill the default root.
1829          */
1830         if (!list_empty(&root->cgrp.self.children) ||
1831             root == &cgrp_dfl_root)
1832                 cgroup_put(&root->cgrp);
1833         else
1834                 percpu_ref_kill(&root->cgrp.self.refcnt);
1835
1836         kernfs_kill_sb(sb);
1837 }
1838
1839 struct file_system_type cgroup_fs_type = {
1840         .name = "cgroup",
1841         .mount = cgroup_mount,
1842         .kill_sb = cgroup_kill_sb,
1843         .fs_flags = FS_USERNS_MOUNT,
1844 };
1845
1846 static struct file_system_type cgroup2_fs_type = {
1847         .name = "cgroup2",
1848         .mount = cgroup_mount,
1849         .kill_sb = cgroup_kill_sb,
1850         .fs_flags = FS_USERNS_MOUNT,
1851 };
1852
1853 int cgroup_path_ns_locked(struct cgroup *cgrp, char *buf, size_t buflen,
1854                           struct cgroup_namespace *ns)
1855 {
1856         struct cgroup *root = cset_cgroup_from_root(ns->root_cset, cgrp->root);
1857
1858         return kernfs_path_from_node(cgrp->kn, root->kn, buf, buflen);
1859 }
1860
1861 int cgroup_path_ns(struct cgroup *cgrp, char *buf, size_t buflen,
1862                    struct cgroup_namespace *ns)
1863 {
1864         int ret;
1865
1866         mutex_lock(&cgroup_mutex);
1867         spin_lock_irq(&css_set_lock);
1868
1869         ret = cgroup_path_ns_locked(cgrp, buf, buflen, ns);
1870
1871         spin_unlock_irq(&css_set_lock);
1872         mutex_unlock(&cgroup_mutex);
1873
1874         return ret;
1875 }
1876 EXPORT_SYMBOL_GPL(cgroup_path_ns);
1877
1878 /**
1879  * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
1880  * @task: target task
1881  * @buf: the buffer to write the path into
1882  * @buflen: the length of the buffer
1883  *
1884  * Determine @task's cgroup on the first (the one with the lowest non-zero
1885  * hierarchy_id) cgroup hierarchy and copy its path into @buf.  This
1886  * function grabs cgroup_mutex and shouldn't be used inside locks used by
1887  * cgroup controller callbacks.
1888  *
1889  * Return value is the same as kernfs_path().
1890  */
1891 int task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
1892 {
1893         struct cgroup_root *root;
1894         struct cgroup *cgrp;
1895         int hierarchy_id = 1;
1896         int ret;
1897
1898         mutex_lock(&cgroup_mutex);
1899         spin_lock_irq(&css_set_lock);
1900
1901         root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
1902
1903         if (root) {
1904                 cgrp = task_cgroup_from_root(task, root);
1905                 ret = cgroup_path_ns_locked(cgrp, buf, buflen, &init_cgroup_ns);
1906         } else {
1907                 /* if no hierarchy exists, everyone is in "/" */
1908                 ret = strlcpy(buf, "/", buflen);
1909         }
1910
1911         spin_unlock_irq(&css_set_lock);
1912         mutex_unlock(&cgroup_mutex);
1913         return ret;
1914 }
1915 EXPORT_SYMBOL_GPL(task_cgroup_path);
1916
1917 /**
1918  * cgroup_migrate_add_task - add a migration target task to a migration context
1919  * @task: target task
1920  * @mgctx: target migration context
1921  *
1922  * Add @task, which is a migration target, to @mgctx->tset.  This function
1923  * becomes noop if @task doesn't need to be migrated.  @task's css_set
1924  * should have been added as a migration source and @task->cg_list will be
1925  * moved from the css_set's tasks list to mg_tasks one.
1926  */
1927 static void cgroup_migrate_add_task(struct task_struct *task,
1928                                     struct cgroup_mgctx *mgctx)
1929 {
1930         struct css_set *cset;
1931
1932         lockdep_assert_held(&css_set_lock);
1933
1934         /* @task either already exited or can't exit until the end */
1935         if (task->flags & PF_EXITING)
1936                 return;
1937
1938         /* leave @task alone if post_fork() hasn't linked it yet */
1939         if (list_empty(&task->cg_list))
1940                 return;
1941
1942         cset = task_css_set(task);
1943         if (!cset->mg_src_cgrp)
1944                 return;
1945
1946         list_move_tail(&task->cg_list, &cset->mg_tasks);
1947         if (list_empty(&cset->mg_node))
1948                 list_add_tail(&cset->mg_node,
1949                               &mgctx->tset.src_csets);
1950         if (list_empty(&cset->mg_dst_cset->mg_node))
1951                 list_add_tail(&cset->mg_dst_cset->mg_node,
1952                               &mgctx->tset.dst_csets);
1953 }
1954
1955 /**
1956  * cgroup_taskset_first - reset taskset and return the first task
1957  * @tset: taskset of interest
1958  * @dst_cssp: output variable for the destination css
1959  *
1960  * @tset iteration is initialized and the first task is returned.
1961  */
1962 struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset,
1963                                          struct cgroup_subsys_state **dst_cssp)
1964 {
1965         tset->cur_cset = list_first_entry(tset->csets, struct css_set, mg_node);
1966         tset->cur_task = NULL;
1967
1968         return cgroup_taskset_next(tset, dst_cssp);
1969 }
1970
1971 /**
1972  * cgroup_taskset_next - iterate to the next task in taskset
1973  * @tset: taskset of interest
1974  * @dst_cssp: output variable for the destination css
1975  *
1976  * Return the next task in @tset.  Iteration must have been initialized
1977  * with cgroup_taskset_first().
1978  */
1979 struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset,
1980                                         struct cgroup_subsys_state **dst_cssp)
1981 {
1982         struct css_set *cset = tset->cur_cset;
1983         struct task_struct *task = tset->cur_task;
1984
1985         while (&cset->mg_node != tset->csets) {
1986                 if (!task)
1987                         task = list_first_entry(&cset->mg_tasks,
1988                                                 struct task_struct, cg_list);
1989                 else
1990                         task = list_next_entry(task, cg_list);
1991
1992                 if (&task->cg_list != &cset->mg_tasks) {
1993                         tset->cur_cset = cset;
1994                         tset->cur_task = task;
1995
1996                         /*
1997                          * This function may be called both before and
1998                          * after cgroup_taskset_migrate().  The two cases
1999                          * can be distinguished by looking at whether @cset
2000                          * has its ->mg_dst_cset set.
2001                          */
2002                         if (cset->mg_dst_cset)
2003                                 *dst_cssp = cset->mg_dst_cset->subsys[tset->ssid];
2004                         else
2005                                 *dst_cssp = cset->subsys[tset->ssid];
2006
2007                         return task;
2008                 }
2009
2010                 cset = list_next_entry(cset, mg_node);
2011                 task = NULL;
2012         }
2013
2014         return NULL;
2015 }
2016
2017 /**
2018  * cgroup_taskset_migrate - migrate a taskset
2019  * @mgctx: migration context
2020  *
2021  * Migrate tasks in @mgctx as setup by migration preparation functions.
2022  * This function fails iff one of the ->can_attach callbacks fails and
2023  * guarantees that either all or none of the tasks in @mgctx are migrated.
2024  * @mgctx is consumed regardless of success.
2025  */
2026 static int cgroup_migrate_execute(struct cgroup_mgctx *mgctx)
2027 {
2028         struct cgroup_taskset *tset = &mgctx->tset;
2029         struct cgroup_subsys *ss;
2030         struct task_struct *task, *tmp_task;
2031         struct css_set *cset, *tmp_cset;
2032         int ssid, failed_ssid, ret;
2033
2034         /* methods shouldn't be called if no task is actually migrating */
2035         if (list_empty(&tset->src_csets))
2036                 return 0;
2037
2038         /* check that we can legitimately attach to the cgroup */
2039         do_each_subsys_mask(ss, ssid, mgctx->ss_mask) {
2040                 if (ss->can_attach) {
2041                         tset->ssid = ssid;
2042                         ret = ss->can_attach(tset);
2043                         if (ret) {
2044                                 failed_ssid = ssid;
2045                                 goto out_cancel_attach;
2046                         }
2047                 }
2048         } while_each_subsys_mask();
2049
2050         /*
2051          * Now that we're guaranteed success, proceed to move all tasks to
2052          * the new cgroup.  There are no failure cases after here, so this
2053          * is the commit point.
2054          */
2055         spin_lock_irq(&css_set_lock);
2056         list_for_each_entry(cset, &tset->src_csets, mg_node) {
2057                 list_for_each_entry_safe(task, tmp_task, &cset->mg_tasks, cg_list) {
2058                         struct css_set *from_cset = task_css_set(task);
2059                         struct css_set *to_cset = cset->mg_dst_cset;
2060
2061                         get_css_set(to_cset);
2062                         css_set_move_task(task, from_cset, to_cset, true);
2063                         put_css_set_locked(from_cset);
2064                 }
2065         }
2066         spin_unlock_irq(&css_set_lock);
2067
2068         /*
2069          * Migration is committed, all target tasks are now on dst_csets.
2070          * Nothing is sensitive to fork() after this point.  Notify
2071          * controllers that migration is complete.
2072          */
2073         tset->csets = &tset->dst_csets;
2074
2075         do_each_subsys_mask(ss, ssid, mgctx->ss_mask) {
2076                 if (ss->attach) {
2077                         tset->ssid = ssid;
2078                         ss->attach(tset);
2079                 }
2080         } while_each_subsys_mask();
2081
2082         ret = 0;
2083         goto out_release_tset;
2084
2085 out_cancel_attach:
2086         do_each_subsys_mask(ss, ssid, mgctx->ss_mask) {
2087                 if (ssid == failed_ssid)
2088                         break;
2089                 if (ss->cancel_attach) {
2090                         tset->ssid = ssid;
2091                         ss->cancel_attach(tset);
2092                 }
2093         } while_each_subsys_mask();
2094 out_release_tset:
2095         spin_lock_irq(&css_set_lock);
2096         list_splice_init(&tset->dst_csets, &tset->src_csets);
2097         list_for_each_entry_safe(cset, tmp_cset, &tset->src_csets, mg_node) {
2098                 list_splice_tail_init(&cset->mg_tasks, &cset->tasks);
2099                 list_del_init(&cset->mg_node);
2100         }
2101         spin_unlock_irq(&css_set_lock);
2102         return ret;
2103 }
2104
2105 /**
2106  * cgroup_may_migrate_to - verify whether a cgroup can be migration destination
2107  * @dst_cgrp: destination cgroup to test
2108  *
2109  * On the default hierarchy, except for the root, subtree_control must be
2110  * zero for migration destination cgroups with tasks so that child cgroups
2111  * don't compete against tasks.
2112  */
2113 bool cgroup_may_migrate_to(struct cgroup *dst_cgrp)
2114 {
2115         return !cgroup_on_dfl(dst_cgrp) || !cgroup_parent(dst_cgrp) ||
2116                 !dst_cgrp->subtree_control;
2117 }
2118
2119 /**
2120  * cgroup_migrate_finish - cleanup after attach
2121  * @mgctx: migration context
2122  *
2123  * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst().  See
2124  * those functions for details.
2125  */
2126 void cgroup_migrate_finish(struct cgroup_mgctx *mgctx)
2127 {
2128         LIST_HEAD(preloaded);
2129         struct css_set *cset, *tmp_cset;
2130
2131         lockdep_assert_held(&cgroup_mutex);
2132
2133         spin_lock_irq(&css_set_lock);
2134
2135         list_splice_tail_init(&mgctx->preloaded_src_csets, &preloaded);
2136         list_splice_tail_init(&mgctx->preloaded_dst_csets, &preloaded);
2137
2138         list_for_each_entry_safe(cset, tmp_cset, &preloaded, mg_preload_node) {
2139                 cset->mg_src_cgrp = NULL;
2140                 cset->mg_dst_cgrp = NULL;
2141                 cset->mg_dst_cset = NULL;
2142                 list_del_init(&cset->mg_preload_node);
2143                 put_css_set_locked(cset);
2144         }
2145
2146         spin_unlock_irq(&css_set_lock);
2147 }
2148
2149 /**
2150  * cgroup_migrate_add_src - add a migration source css_set
2151  * @src_cset: the source css_set to add
2152  * @dst_cgrp: the destination cgroup
2153  * @mgctx: migration context
2154  *
2155  * Tasks belonging to @src_cset are about to be migrated to @dst_cgrp.  Pin
2156  * @src_cset and add it to @mgctx->src_csets, which should later be cleaned
2157  * up by cgroup_migrate_finish().
2158  *
2159  * This function may be called without holding cgroup_threadgroup_rwsem
2160  * even if the target is a process.  Threads may be created and destroyed
2161  * but as long as cgroup_mutex is not dropped, no new css_set can be put
2162  * into play and the preloaded css_sets are guaranteed to cover all
2163  * migrations.
2164  */
2165 void cgroup_migrate_add_src(struct css_set *src_cset,
2166                             struct cgroup *dst_cgrp,
2167                             struct cgroup_mgctx *mgctx)
2168 {
2169         struct cgroup *src_cgrp;
2170
2171         lockdep_assert_held(&cgroup_mutex);
2172         lockdep_assert_held(&css_set_lock);
2173
2174         /*
2175          * If ->dead, @src_set is associated with one or more dead cgroups
2176          * and doesn't contain any migratable tasks.  Ignore it early so
2177          * that the rest of migration path doesn't get confused by it.
2178          */
2179         if (src_cset->dead)
2180                 return;
2181
2182         src_cgrp = cset_cgroup_from_root(src_cset, dst_cgrp->root);
2183
2184         if (!list_empty(&src_cset->mg_preload_node))
2185                 return;
2186
2187         WARN_ON(src_cset->mg_src_cgrp);
2188         WARN_ON(src_cset->mg_dst_cgrp);
2189         WARN_ON(!list_empty(&src_cset->mg_tasks));
2190         WARN_ON(!list_empty(&src_cset->mg_node));
2191
2192         src_cset->mg_src_cgrp = src_cgrp;
2193         src_cset->mg_dst_cgrp = dst_cgrp;
2194         get_css_set(src_cset);
2195         list_add_tail(&src_cset->mg_preload_node, &mgctx->preloaded_src_csets);
2196 }
2197
2198 /**
2199  * cgroup_migrate_prepare_dst - prepare destination css_sets for migration
2200  * @mgctx: migration context
2201  *
2202  * Tasks are about to be moved and all the source css_sets have been
2203  * preloaded to @mgctx->preloaded_src_csets.  This function looks up and
2204  * pins all destination css_sets, links each to its source, and append them
2205  * to @mgctx->preloaded_dst_csets.
2206  *
2207  * This function must be called after cgroup_migrate_add_src() has been
2208  * called on each migration source css_set.  After migration is performed
2209  * using cgroup_migrate(), cgroup_migrate_finish() must be called on
2210  * @mgctx.
2211  */
2212 int cgroup_migrate_prepare_dst(struct cgroup_mgctx *mgctx)
2213 {
2214         struct css_set *src_cset, *tmp_cset;
2215
2216         lockdep_assert_held(&cgroup_mutex);
2217
2218         /* look up the dst cset for each src cset and link it to src */
2219         list_for_each_entry_safe(src_cset, tmp_cset, &mgctx->preloaded_src_csets,
2220                                  mg_preload_node) {
2221                 struct css_set *dst_cset;
2222                 struct cgroup_subsys *ss;
2223                 int ssid;
2224
2225                 dst_cset = find_css_set(src_cset, src_cset->mg_dst_cgrp);
2226                 if (!dst_cset)
2227                         goto err;
2228
2229                 WARN_ON_ONCE(src_cset->mg_dst_cset || dst_cset->mg_dst_cset);
2230
2231                 /*
2232                  * If src cset equals dst, it's noop.  Drop the src.
2233                  * cgroup_migrate() will skip the cset too.  Note that we
2234                  * can't handle src == dst as some nodes are used by both.
2235                  */
2236                 if (src_cset == dst_cset) {
2237                         src_cset->mg_src_cgrp = NULL;
2238                         src_cset->mg_dst_cgrp = NULL;
2239                         list_del_init(&src_cset->mg_preload_node);
2240                         put_css_set(src_cset);
2241                         put_css_set(dst_cset);
2242                         continue;
2243                 }
2244
2245                 src_cset->mg_dst_cset = dst_cset;
2246
2247                 if (list_empty(&dst_cset->mg_preload_node))
2248                         list_add_tail(&dst_cset->mg_preload_node,
2249                                       &mgctx->preloaded_dst_csets);
2250                 else
2251                         put_css_set(dst_cset);
2252
2253                 for_each_subsys(ss, ssid)
2254                         if (src_cset->subsys[ssid] != dst_cset->subsys[ssid])
2255                                 mgctx->ss_mask |= 1 << ssid;
2256         }
2257
2258         return 0;
2259 err:
2260         cgroup_migrate_finish(mgctx);
2261         return -ENOMEM;
2262 }
2263
2264 /**
2265  * cgroup_migrate - migrate a process or task to a cgroup
2266  * @leader: the leader of the process or the task to migrate
2267  * @threadgroup: whether @leader points to the whole process or a single task
2268  * @mgctx: migration context
2269  *
2270  * Migrate a process or task denoted by @leader.  If migrating a process,
2271  * the caller must be holding cgroup_threadgroup_rwsem.  The caller is also
2272  * responsible for invoking cgroup_migrate_add_src() and
2273  * cgroup_migrate_prepare_dst() on the targets before invoking this
2274  * function and following up with cgroup_migrate_finish().
2275  *
2276  * As long as a controller's ->can_attach() doesn't fail, this function is
2277  * guaranteed to succeed.  This means that, excluding ->can_attach()
2278  * failure, when migrating multiple targets, the success or failure can be
2279  * decided for all targets by invoking group_migrate_prepare_dst() before
2280  * actually starting migrating.
2281  */
2282 int cgroup_migrate(struct task_struct *leader, bool threadgroup,
2283                    struct cgroup_mgctx *mgctx)
2284 {
2285         struct task_struct *task;
2286
2287         /*
2288          * Prevent freeing of tasks while we take a snapshot. Tasks that are
2289          * already PF_EXITING could be freed from underneath us unless we
2290          * take an rcu_read_lock.
2291          */
2292         spin_lock_irq(&css_set_lock);
2293         rcu_read_lock();
2294         task = leader;
2295         do {
2296                 cgroup_migrate_add_task(task, mgctx);
2297                 if (!threadgroup)
2298                         break;
2299         } while_each_thread(leader, task);
2300         rcu_read_unlock();
2301         spin_unlock_irq(&css_set_lock);
2302
2303         return cgroup_migrate_execute(mgctx);
2304 }
2305
2306 /**
2307  * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
2308  * @dst_cgrp: the cgroup to attach to
2309  * @leader: the task or the leader of the threadgroup to be attached
2310  * @threadgroup: attach the whole threadgroup?
2311  *
2312  * Call holding cgroup_mutex and cgroup_threadgroup_rwsem.
2313  */
2314 int cgroup_attach_task(struct cgroup *dst_cgrp, struct task_struct *leader,
2315                        bool threadgroup)
2316 {
2317         DEFINE_CGROUP_MGCTX(mgctx);
2318         struct task_struct *task;
2319         int ret;
2320
2321         if (!cgroup_may_migrate_to(dst_cgrp))
2322                 return -EBUSY;
2323
2324         /* look up all src csets */
2325         spin_lock_irq(&css_set_lock);
2326         rcu_read_lock();
2327         task = leader;
2328         do {
2329                 cgroup_migrate_add_src(task_css_set(task), dst_cgrp, &mgctx);
2330                 if (!threadgroup)
2331                         break;
2332         } while_each_thread(leader, task);
2333         rcu_read_unlock();
2334         spin_unlock_irq(&css_set_lock);
2335
2336         /* prepare dst csets and commit */
2337         ret = cgroup_migrate_prepare_dst(&mgctx);
2338         if (!ret)
2339                 ret = cgroup_migrate(leader, threadgroup, &mgctx);
2340
2341         cgroup_migrate_finish(&mgctx);
2342
2343         if (!ret)
2344                 trace_cgroup_attach_task(dst_cgrp, leader, threadgroup);
2345
2346         return ret;
2347 }
2348
2349 static int cgroup_procs_write_permission(struct task_struct *task,
2350                                          struct cgroup *dst_cgrp,
2351                                          struct kernfs_open_file *of)
2352 {
2353         int ret = 0;
2354
2355         if (cgroup_on_dfl(dst_cgrp)) {
2356                 struct super_block *sb = of->file->f_path.dentry->d_sb;
2357                 struct cgroup *cgrp;
2358                 struct inode *inode;
2359
2360                 spin_lock_irq(&css_set_lock);
2361                 cgrp = task_cgroup_from_root(task, &cgrp_dfl_root);
2362                 spin_unlock_irq(&css_set_lock);
2363
2364                 while (!cgroup_is_descendant(dst_cgrp, cgrp))
2365                         cgrp = cgroup_parent(cgrp);
2366
2367                 ret = -ENOMEM;
2368                 inode = kernfs_get_inode(sb, cgrp->procs_file.kn);
2369                 if (inode) {
2370                         ret = inode_permission(inode, MAY_WRITE);
2371                         iput(inode);
2372                 }
2373         } else {
2374                 const struct cred *cred = current_cred();
2375                 const struct cred *tcred = get_task_cred(task);
2376
2377                 /*
2378                  * even if we're attaching all tasks in the thread group,
2379                  * we only need to check permissions on one of them.
2380                  */
2381                 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2382                     !uid_eq(cred->euid, tcred->uid) &&
2383                     !uid_eq(cred->euid, tcred->suid))
2384                         ret = -EACCES;
2385                 put_cred(tcred);
2386         }
2387
2388         return ret;
2389 }
2390
2391 /*
2392  * Find the task_struct of the task to attach by vpid and pass it along to the
2393  * function to attach either it or all tasks in its threadgroup. Will lock
2394  * cgroup_mutex and threadgroup.
2395  */
2396 ssize_t __cgroup_procs_write(struct kernfs_open_file *of, char *buf,
2397                              size_t nbytes, loff_t off, bool threadgroup)
2398 {
2399         struct task_struct *tsk;
2400         struct cgroup_subsys *ss;
2401         struct cgroup *cgrp;
2402         pid_t pid;
2403         int ssid, ret;
2404
2405         if (kstrtoint(strstrip(buf), 0, &pid) || pid < 0)
2406                 return -EINVAL;
2407
2408         cgrp = cgroup_kn_lock_live(of->kn, false);
2409         if (!cgrp)
2410                 return -ENODEV;
2411
2412         percpu_down_write(&cgroup_threadgroup_rwsem);
2413         rcu_read_lock();
2414         if (pid) {
2415                 tsk = find_task_by_vpid(pid);
2416                 if (!tsk) {
2417                         ret = -ESRCH;
2418                         goto out_unlock_rcu;
2419                 }
2420         } else {
2421                 tsk = current;
2422         }
2423
2424         if (threadgroup)
2425                 tsk = tsk->group_leader;
2426
2427         /*
2428          * Workqueue threads may acquire PF_NO_SETAFFINITY and become
2429          * trapped in a cpuset, or RT worker may be born in a cgroup
2430          * with no rt_runtime allocated.  Just say no.
2431          */
2432         if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
2433                 ret = -EINVAL;
2434                 goto out_unlock_rcu;
2435         }
2436
2437         get_task_struct(tsk);
2438         rcu_read_unlock();
2439
2440         ret = cgroup_procs_write_permission(tsk, cgrp, of);
2441         if (!ret)
2442                 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2443
2444         put_task_struct(tsk);
2445         goto out_unlock_threadgroup;
2446
2447 out_unlock_rcu:
2448         rcu_read_unlock();
2449 out_unlock_threadgroup:
2450         percpu_up_write(&cgroup_threadgroup_rwsem);
2451         for_each_subsys(ss, ssid)
2452                 if (ss->post_attach)
2453                         ss->post_attach();
2454         cgroup_kn_unlock(of->kn);
2455         return ret ?: nbytes;
2456 }
2457
2458 ssize_t cgroup_procs_write(struct kernfs_open_file *of, char *buf, size_t nbytes,
2459                            loff_t off)
2460 {
2461         return __cgroup_procs_write(of, buf, nbytes, off, true);
2462 }
2463
2464 static void cgroup_print_ss_mask(struct seq_file *seq, u16 ss_mask)
2465 {
2466         struct cgroup_subsys *ss;
2467         bool printed = false;
2468         int ssid;
2469
2470         do_each_subsys_mask(ss, ssid, ss_mask) {
2471                 if (printed)
2472                         seq_putc(seq, ' ');
2473                 seq_printf(seq, "%s", ss->name);
2474                 printed = true;
2475         } while_each_subsys_mask();
2476         if (printed)
2477                 seq_putc(seq, '\n');
2478 }
2479
2480 /* show controllers which are enabled from the parent */
2481 static int cgroup_controllers_show(struct seq_file *seq, void *v)
2482 {
2483         struct cgroup *cgrp = seq_css(seq)->cgroup;
2484
2485         cgroup_print_ss_mask(seq, cgroup_control(cgrp));
2486         return 0;
2487 }
2488
2489 /* show controllers which are enabled for a given cgroup's children */
2490 static int cgroup_subtree_control_show(struct seq_file *seq, void *v)
2491 {
2492         struct cgroup *cgrp = seq_css(seq)->cgroup;
2493
2494         cgroup_print_ss_mask(seq, cgrp->subtree_control);
2495         return 0;
2496 }
2497
2498 /**
2499  * cgroup_update_dfl_csses - update css assoc of a subtree in default hierarchy
2500  * @cgrp: root of the subtree to update csses for
2501  *
2502  * @cgrp's control masks have changed and its subtree's css associations
2503  * need to be updated accordingly.  This function looks up all css_sets
2504  * which are attached to the subtree, creates the matching updated css_sets
2505  * and migrates the tasks to the new ones.
2506  */
2507 static int cgroup_update_dfl_csses(struct cgroup *cgrp)
2508 {
2509         DEFINE_CGROUP_MGCTX(mgctx);
2510         struct cgroup_subsys_state *d_css;
2511         struct cgroup *dsct;
2512         struct css_set *src_cset;
2513         int ret;
2514
2515         lockdep_assert_held(&cgroup_mutex);
2516
2517         percpu_down_write(&cgroup_threadgroup_rwsem);
2518
2519         /* look up all csses currently attached to @cgrp's subtree */
2520         spin_lock_irq(&css_set_lock);
2521         cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) {
2522                 struct cgrp_cset_link *link;
2523
2524                 list_for_each_entry(link, &dsct->cset_links, cset_link)
2525                         cgroup_migrate_add_src(link->cset, dsct, &mgctx);
2526         }
2527         spin_unlock_irq(&css_set_lock);
2528
2529         /* NULL dst indicates self on default hierarchy */
2530         ret = cgroup_migrate_prepare_dst(&mgctx);
2531         if (ret)
2532                 goto out_finish;
2533
2534         spin_lock_irq(&css_set_lock);
2535         list_for_each_entry(src_cset, &mgctx.preloaded_src_csets, mg_preload_node) {
2536                 struct task_struct *task, *ntask;
2537
2538                 /* all tasks in src_csets need to be migrated */
2539                 list_for_each_entry_safe(task, ntask, &src_cset->tasks, cg_list)
2540                         cgroup_migrate_add_task(task, &mgctx);
2541         }
2542         spin_unlock_irq(&css_set_lock);
2543
2544         ret = cgroup_migrate_execute(&mgctx);
2545 out_finish:
2546         cgroup_migrate_finish(&mgctx);
2547         percpu_up_write(&cgroup_threadgroup_rwsem);
2548         return ret;
2549 }
2550
2551 /**
2552  * cgroup_lock_and_drain_offline - lock cgroup_mutex and drain offlined csses
2553  * @cgrp: root of the target subtree
2554  *
2555  * Because css offlining is asynchronous, userland may try to re-enable a
2556  * controller while the previous css is still around.  This function grabs
2557  * cgroup_mutex and drains the previous css instances of @cgrp's subtree.
2558  */
2559 void cgroup_lock_and_drain_offline(struct cgroup *cgrp)
2560         __acquires(&cgroup_mutex)
2561 {
2562         struct cgroup *dsct;
2563         struct cgroup_subsys_state *d_css;
2564         struct cgroup_subsys *ss;
2565         int ssid;
2566
2567 restart:
2568         mutex_lock(&cgroup_mutex);
2569
2570         cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) {
2571                 for_each_subsys(ss, ssid) {
2572                         struct cgroup_subsys_state *css = cgroup_css(dsct, ss);
2573                         DEFINE_WAIT(wait);
2574
2575                         if (!css || !percpu_ref_is_dying(&css->refcnt))
2576                                 continue;
2577
2578                         cgroup_get(dsct);
2579                         prepare_to_wait(&dsct->offline_waitq, &wait,
2580                                         TASK_UNINTERRUPTIBLE);
2581
2582                         mutex_unlock(&cgroup_mutex);
2583                         schedule();
2584                         finish_wait(&dsct->offline_waitq, &wait);
2585
2586                         cgroup_put(dsct);
2587                         goto restart;
2588                 }
2589         }
2590 }
2591
2592 /**
2593  * cgroup_save_control - save control masks of a subtree
2594  * @cgrp: root of the target subtree
2595  *
2596  * Save ->subtree_control and ->subtree_ss_mask to the respective old_
2597  * prefixed fields for @cgrp's subtree including @cgrp itself.
2598  */
2599 static void cgroup_save_control(struct cgroup *cgrp)
2600 {
2601         struct cgroup *dsct;
2602         struct cgroup_subsys_state *d_css;
2603
2604         cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) {
2605                 dsct->old_subtree_control = dsct->subtree_control;
2606                 dsct->old_subtree_ss_mask = dsct->subtree_ss_mask;
2607         }
2608 }
2609
2610 /**
2611  * cgroup_propagate_control - refresh control masks of a subtree
2612  * @cgrp: root of the target subtree
2613  *
2614  * For @cgrp and its subtree, ensure ->subtree_ss_mask matches
2615  * ->subtree_control and propagate controller availability through the
2616  * subtree so that descendants don't have unavailable controllers enabled.
2617  */
2618 static void cgroup_propagate_control(struct cgroup *cgrp)
2619 {
2620         struct cgroup *dsct;
2621         struct cgroup_subsys_state *d_css;
2622
2623         cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) {
2624                 dsct->subtree_control &= cgroup_control(dsct);
2625                 dsct->subtree_ss_mask =
2626                         cgroup_calc_subtree_ss_mask(dsct->subtree_control,
2627                                                     cgroup_ss_mask(dsct));
2628         }
2629 }
2630
2631 /**
2632  * cgroup_restore_control - restore control masks of a subtree
2633  * @cgrp: root of the target subtree
2634  *
2635  * Restore ->subtree_control and ->subtree_ss_mask from the respective old_
2636  * prefixed fields for @cgrp's subtree including @cgrp itself.
2637  */
2638 static void cgroup_restore_control(struct cgroup *cgrp)
2639 {
2640         struct cgroup *dsct;
2641         struct cgroup_subsys_state *d_css;
2642
2643         cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) {
2644                 dsct->subtree_control = dsct->old_subtree_control;
2645                 dsct->subtree_ss_mask = dsct->old_subtree_ss_mask;
2646         }
2647 }
2648
2649 static bool css_visible(struct cgroup_subsys_state *css)
2650 {
2651         struct cgroup_subsys *ss = css->ss;
2652         struct cgroup *cgrp = css->cgroup;
2653
2654         if (cgroup_control(cgrp) & (1 << ss->id))
2655                 return true;
2656         if (!(cgroup_ss_mask(cgrp) & (1 << ss->id)))
2657                 return false;
2658         return cgroup_on_dfl(cgrp) && ss->implicit_on_dfl;
2659 }
2660
2661 /**
2662  * cgroup_apply_control_enable - enable or show csses according to control
2663  * @cgrp: root of the target subtree
2664  *
2665  * Walk @cgrp's subtree and create new csses or make the existing ones
2666  * visible.  A css is created invisible if it's being implicitly enabled
2667  * through dependency.  An invisible css is made visible when the userland
2668  * explicitly enables it.
2669  *
2670  * Returns 0 on success, -errno on failure.  On failure, csses which have
2671  * been processed already aren't cleaned up.  The caller is responsible for
2672  * cleaning up with cgroup_apply_control_disble().
2673  */
2674 static int cgroup_apply_control_enable(struct cgroup *cgrp)
2675 {
2676         struct cgroup *dsct;
2677         struct cgroup_subsys_state *d_css;
2678         struct cgroup_subsys *ss;
2679         int ssid, ret;
2680
2681         cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) {
2682                 for_each_subsys(ss, ssid) {
2683                         struct cgroup_subsys_state *css = cgroup_css(dsct, ss);
2684
2685                         WARN_ON_ONCE(css && percpu_ref_is_dying(&css->refcnt));
2686
2687                         if (!(cgroup_ss_mask(dsct) & (1 << ss->id)))
2688                                 continue;
2689
2690                         if (!css) {
2691                                 css = css_create(dsct, ss);
2692                                 if (IS_ERR(css))
2693                                         return PTR_ERR(css);
2694                         }
2695
2696                         if (css_visible(css)) {
2697                                 ret = css_populate_dir(css);
2698                                 if (ret)
2699                                         return ret;
2700                         }
2701                 }
2702         }
2703
2704         return 0;
2705 }
2706
2707 /**
2708  * cgroup_apply_control_disable - kill or hide csses according to control
2709  * @cgrp: root of the target subtree
2710  *
2711  * Walk @cgrp's subtree and kill and hide csses so that they match
2712  * cgroup_ss_mask() and cgroup_visible_mask().
2713  *
2714  * A css is hidden when the userland requests it to be disabled while other
2715  * subsystems are still depending on it.  The css must not actively control
2716  * resources and be in the vanilla state if it's made visible again later.
2717  * Controllers which may be depended upon should provide ->css_reset() for
2718  * this purpose.
2719  */
2720 static void cgroup_apply_control_disable(struct cgroup *cgrp)
2721 {
2722         struct cgroup *dsct;
2723         struct cgroup_subsys_state *d_css;
2724         struct cgroup_subsys *ss;
2725         int ssid;
2726
2727         cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) {
2728                 for_each_subsys(ss, ssid) {
2729                         struct cgroup_subsys_state *css = cgroup_css(dsct, ss);
2730
2731                         WARN_ON_ONCE(css && percpu_ref_is_dying(&css->refcnt));
2732
2733                         if (!css)
2734                                 continue;
2735
2736                         if (css->parent &&
2737                             !(cgroup_ss_mask(dsct) & (1 << ss->id))) {
2738                                 kill_css(css);
2739                         } else if (!css_visible(css)) {
2740                                 css_clear_dir(css);
2741                                 if (ss->css_reset)
2742                                         ss->css_reset(css);
2743                         }
2744                 }
2745         }
2746 }
2747
2748 /**
2749  * cgroup_apply_control - apply control mask updates to the subtree
2750  * @cgrp: root of the target subtree
2751  *
2752  * subsystems can be enabled and disabled in a subtree using the following
2753  * steps.
2754  *
2755  * 1. Call cgroup_save_control() to stash the current state.
2756  * 2. Update ->subtree_control masks in the subtree as desired.
2757  * 3. Call cgroup_apply_control() to apply the changes.
2758  * 4. Optionally perform other related operations.
2759  * 5. Call cgroup_finalize_control() to finish up.
2760  *
2761  * This function implements step 3 and propagates the mask changes
2762  * throughout @cgrp's subtree, updates csses accordingly and perform
2763  * process migrations.
2764  */
2765 static int cgroup_apply_control(struct cgroup *cgrp)
2766 {
2767         int ret;
2768
2769         cgroup_propagate_control(cgrp);
2770
2771         ret = cgroup_apply_control_enable(cgrp);
2772         if (ret)
2773                 return ret;
2774
2775         /*
2776          * At this point, cgroup_e_css() results reflect the new csses
2777          * making the following cgroup_update_dfl_csses() properly update
2778          * css associations of all tasks in the subtree.
2779          */
2780         ret = cgroup_update_dfl_csses(cgrp);
2781         if (ret)
2782                 return ret;
2783
2784         return 0;
2785 }
2786
2787 /**
2788  * cgroup_finalize_control - finalize control mask update
2789  * @cgrp: root of the target subtree
2790  * @ret: the result of the update
2791  *
2792  * Finalize control mask update.  See cgroup_apply_control() for more info.
2793  */
2794 static void cgroup_finalize_control(struct cgroup *cgrp, int ret)
2795 {
2796         if (ret) {
2797                 cgroup_restore_control(cgrp);
2798                 cgroup_propagate_control(cgrp);
2799         }
2800
2801         cgroup_apply_control_disable(cgrp);
2802 }
2803
2804 /* change the enabled child controllers for a cgroup in the default hierarchy */
2805 static ssize_t cgroup_subtree_control_write(struct kernfs_open_file *of,
2806                                             char *buf, size_t nbytes,
2807                                             loff_t off)
2808 {
2809         u16 enable = 0, disable = 0;
2810         struct cgroup *cgrp, *child;
2811         struct cgroup_subsys *ss;
2812         char *tok;
2813         int ssid, ret;
2814
2815         /*
2816          * Parse input - space separated list of subsystem names prefixed
2817          * with either + or -.
2818          */
2819         buf = strstrip(buf);
2820         while ((tok = strsep(&buf, " "))) {
2821                 if (tok[0] == '\0')
2822                         continue;
2823                 do_each_subsys_mask(ss, ssid, ~cgrp_dfl_inhibit_ss_mask) {
2824                         if (!cgroup_ssid_enabled(ssid) ||
2825                             strcmp(tok + 1, ss->name))
2826                                 continue;
2827
2828                         if (*tok == '+') {
2829                                 enable |= 1 << ssid;
2830                                 disable &= ~(1 << ssid);
2831                         } else if (*tok == '-') {
2832                                 disable |= 1 << ssid;
2833                                 enable &= ~(1 << ssid);
2834                         } else {
2835                                 return -EINVAL;
2836                         }
2837                         break;
2838                 } while_each_subsys_mask();
2839                 if (ssid == CGROUP_SUBSYS_COUNT)
2840                         return -EINVAL;
2841         }
2842
2843         cgrp = cgroup_kn_lock_live(of->kn, true);
2844         if (!cgrp)
2845                 return -ENODEV;
2846
2847         for_each_subsys(ss, ssid) {
2848                 if (enable & (1 << ssid)) {
2849                         if (cgrp->subtree_control & (1 << ssid)) {
2850                                 enable &= ~(1 << ssid);
2851                                 continue;
2852                         }
2853
2854                         if (!(cgroup_control(cgrp) & (1 << ssid))) {
2855                                 ret = -ENOENT;
2856                                 goto out_unlock;
2857                         }
2858                 } else if (disable & (1 << ssid)) {
2859                         if (!(cgrp->subtree_control & (1 << ssid))) {
2860                                 disable &= ~(1 << ssid);
2861                                 continue;
2862                         }
2863
2864                         /* a child has it enabled? */
2865                         cgroup_for_each_live_child(child, cgrp) {
2866                                 if (child->subtree_control & (1 << ssid)) {
2867                                         ret = -EBUSY;
2868                                         goto out_unlock;
2869                                 }
2870                         }
2871                 }
2872         }
2873
2874         if (!enable && !disable) {
2875                 ret = 0;
2876                 goto out_unlock;
2877         }
2878
2879         /*
2880          * Except for the root, subtree_control must be zero for a cgroup
2881          * with tasks so that child cgroups don't compete against tasks.
2882          */
2883         if (enable && cgroup_parent(cgrp)) {
2884                 struct cgrp_cset_link *link;
2885
2886                 /*
2887                  * Because namespaces pin csets too, @cgrp->cset_links
2888                  * might not be empty even when @cgrp is empty.  Walk and
2889                  * verify each cset.
2890                  */
2891                 spin_lock_irq(&css_set_lock);
2892
2893                 ret = 0;
2894                 list_for_each_entry(link, &cgrp->cset_links, cset_link) {
2895                         if (css_set_populated(link->cset)) {
2896                                 ret = -EBUSY;
2897                                 break;
2898                         }
2899                 }
2900
2901                 spin_unlock_irq(&css_set_lock);
2902
2903                 if (ret)
2904                         goto out_unlock;
2905         }
2906
2907         /* save and update control masks and prepare csses */
2908         cgroup_save_control(cgrp);
2909
2910         cgrp->subtree_control |= enable;
2911         cgrp->subtree_control &= ~disable;
2912
2913         ret = cgroup_apply_control(cgrp);
2914
2915         cgroup_finalize_control(cgrp, ret);
2916
2917         kernfs_activate(cgrp->kn);
2918         ret = 0;
2919 out_unlock:
2920         cgroup_kn_unlock(of->kn);
2921         return ret ?: nbytes;
2922 }
2923
2924 static int cgroup_events_show(struct seq_file *seq, void *v)
2925 {
2926         seq_printf(seq, "populated %d\n",
2927                    cgroup_is_populated(seq_css(seq)->cgroup));
2928         return 0;
2929 }
2930
2931 static int cgroup_file_open(struct kernfs_open_file *of)
2932 {
2933         struct cftype *cft = of->kn->priv;
2934
2935         if (cft->open)
2936                 return cft->open(of);
2937         return 0;
2938 }
2939
2940 static void cgroup_file_release(struct kernfs_open_file *of)
2941 {
2942         struct cftype *cft = of->kn->priv;
2943
2944         if (cft->release)
2945                 cft->release(of);
2946 }
2947
2948 static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf,
2949                                  size_t nbytes, loff_t off)
2950 {
2951         struct cgroup *cgrp = of->kn->parent->priv;
2952         struct cftype *cft = of->kn->priv;
2953         struct cgroup_subsys_state *css;
2954         int ret;
2955
2956         if (cft->write)
2957                 return cft->write(of, buf, nbytes, off);
2958
2959         /*
2960          * kernfs guarantees that a file isn't deleted with operations in
2961          * flight, which means that the matching css is and stays alive and
2962          * doesn't need to be pinned.  The RCU locking is not necessary
2963          * either.  It's just for the convenience of using cgroup_css().
2964          */
2965         rcu_read_lock();
2966         css = cgroup_css(cgrp, cft->ss);
2967         rcu_read_unlock();
2968
2969         if (cft->write_u64) {
2970                 unsigned long long v;
2971                 ret = kstrtoull(buf, 0, &v);
2972                 if (!ret)
2973                         ret = cft->write_u64(css, cft, v);
2974         } else if (cft->write_s64) {
2975                 long long v;
2976                 ret = kstrtoll(buf, 0, &v);
2977                 if (!ret)
2978                         ret = cft->write_s64(css, cft, v);
2979         } else {
2980                 ret = -EINVAL;
2981         }
2982
2983         return ret ?: nbytes;
2984 }
2985
2986 static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos)
2987 {
2988         return seq_cft(seq)->seq_start(seq, ppos);
2989 }
2990
2991 static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos)
2992 {
2993         return seq_cft(seq)->seq_next(seq, v, ppos);
2994 }
2995
2996 static void cgroup_seqfile_stop(struct seq_file *seq, void *v)
2997 {
2998         if (seq_cft(seq)->seq_stop)
2999                 seq_cft(seq)->seq_stop(seq, v);
3000 }
3001
3002 static int cgroup_seqfile_show(struct seq_file *m, void *arg)
3003 {
3004         struct cftype *cft = seq_cft(m);
3005         struct cgroup_subsys_state *css = seq_css(m);
3006
3007         if (cft->seq_show)
3008                 return cft->seq_show(m, arg);
3009
3010         if (cft->read_u64)
3011                 seq_printf(m, "%llu\n", cft->read_u64(css, cft));
3012         else if (cft->read_s64)
3013                 seq_printf(m, "%lld\n", cft->read_s64(css, cft));
3014         else
3015                 return -EINVAL;
3016         return 0;
3017 }
3018
3019 static struct kernfs_ops cgroup_kf_single_ops = {
3020         .atomic_write_len       = PAGE_SIZE,
3021         .open                   = cgroup_file_open,
3022         .release                = cgroup_file_release,
3023         .write                  = cgroup_file_write,
3024         .seq_show               = cgroup_seqfile_show,
3025 };
3026
3027 static struct kernfs_ops cgroup_kf_ops = {
3028         .atomic_write_len       = PAGE_SIZE,
3029         .open                   = cgroup_file_open,
3030         .release                = cgroup_file_release,
3031         .write                  = cgroup_file_write,
3032         .seq_start              = cgroup_seqfile_start,
3033         .seq_next               = cgroup_seqfile_next,
3034         .seq_stop               = cgroup_seqfile_stop,
3035         .seq_show               = cgroup_seqfile_show,
3036 };
3037
3038 /* set uid and gid of cgroup dirs and files to that of the creator */
3039 static int cgroup_kn_set_ugid(struct kernfs_node *kn)
3040 {
3041         struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID,
3042                                .ia_uid = current_fsuid(),
3043                                .ia_gid = current_fsgid(), };
3044
3045         if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) &&
3046             gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID))
3047                 return 0;
3048
3049         return kernfs_setattr(kn, &iattr);
3050 }
3051
3052 static int cgroup_add_file(struct cgroup_subsys_state *css, struct cgroup *cgrp,
3053                            struct cftype *cft)
3054 {
3055         char name[CGROUP_FILE_NAME_MAX];
3056         struct kernfs_node *kn;
3057         struct lock_class_key *key = NULL;
3058         int ret;
3059
3060 #ifdef CONFIG_DEBUG_LOCK_ALLOC
3061         key = &cft->lockdep_key;
3062 #endif
3063         kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name),
3064                                   cgroup_file_mode(cft), 0, cft->kf_ops, cft,
3065                                   NULL, key);
3066         if (IS_ERR(kn))
3067                 return PTR_ERR(kn);
3068
3069         ret = cgroup_kn_set_ugid(kn);
3070         if (ret) {
3071                 kernfs_remove(kn);
3072                 return ret;
3073         }
3074
3075         if (cft->file_offset) {
3076                 struct cgroup_file *cfile = (void *)css + cft->file_offset;
3077
3078                 spin_lock_irq(&cgroup_file_kn_lock);
3079                 cfile->kn = kn;
3080                 spin_unlock_irq(&cgroup_file_kn_lock);
3081         }
3082
3083         return 0;
3084 }
3085
3086 /**
3087  * cgroup_addrm_files - add or remove files to a cgroup directory
3088  * @css: the target css
3089  * @cgrp: the target cgroup (usually css->cgroup)
3090  * @cfts: array of cftypes to be added
3091  * @is_add: whether to add or remove
3092  *
3093  * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
3094  * For removals, this function never fails.
3095  */
3096 static int cgroup_addrm_files(struct cgroup_subsys_state *css,
3097                               struct cgroup *cgrp, struct cftype cfts[],
3098                               bool is_add)
3099 {
3100         struct cftype *cft, *cft_end = NULL;
3101         int ret = 0;
3102
3103         lockdep_assert_held(&cgroup_mutex);
3104
3105 restart:
3106         for (cft = cfts; cft != cft_end && cft->name[0] != '\0'; cft++) {
3107                 /* does cft->flags tell us to skip this file on @cgrp? */
3108                 if ((cft->flags & __CFTYPE_ONLY_ON_DFL) && !cgroup_on_dfl(cgrp))
3109                         continue;
3110                 if ((cft->flags & __CFTYPE_NOT_ON_DFL) && cgroup_on_dfl(cgrp))
3111                         continue;
3112                 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgroup_parent(cgrp))
3113                         continue;
3114                 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgroup_parent(cgrp))
3115                         continue;
3116
3117                 if (is_add) {
3118                         ret = cgroup_add_file(css, cgrp, cft);
3119                         if (ret) {
3120                                 pr_warn("%s: failed to add %s, err=%d\n",
3121                                         __func__, cft->name, ret);
3122                                 cft_end = cft;
3123                                 is_add = false;
3124                                 goto restart;
3125                         }
3126                 } else {
3127                         cgroup_rm_file(cgrp, cft);
3128                 }
3129         }
3130         return ret;
3131 }
3132
3133 static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add)
3134 {
3135         LIST_HEAD(pending);
3136         struct cgroup_subsys *ss = cfts[0].ss;
3137         struct cgroup *root = &ss->root->cgrp;
3138         struct cgroup_subsys_state *css;
3139         int ret = 0;
3140
3141         lockdep_assert_held(&cgroup_mutex);
3142
3143         /* add/rm files for all cgroups created before */
3144         css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
3145                 struct cgroup *cgrp = css->cgroup;
3146
3147                 if (!(css->flags & CSS_VISIBLE))
3148                         continue;
3149
3150                 ret = cgroup_addrm_files(css, cgrp, cfts, is_add);
3151                 if (ret)
3152                         break;
3153         }
3154
3155         if (is_add && !ret)
3156                 kernfs_activate(root->kn);
3157         return ret;
3158 }
3159
3160 static void cgroup_exit_cftypes(struct cftype *cfts)
3161 {
3162         struct cftype *cft;
3163
3164         for (cft = cfts; cft->name[0] != '\0'; cft++) {
3165                 /* free copy for custom atomic_write_len, see init_cftypes() */
3166                 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE)
3167                         kfree(cft->kf_ops);
3168                 cft->kf_ops = NULL;
3169                 cft->ss = NULL;
3170
3171                 /* revert flags set by cgroup core while adding @cfts */
3172                 cft->flags &= ~(__CFTYPE_ONLY_ON_DFL | __CFTYPE_NOT_ON_DFL);
3173         }
3174 }
3175
3176 static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
3177 {
3178         struct cftype *cft;
3179
3180         for (cft = cfts; cft->name[0] != '\0'; cft++) {
3181                 struct kernfs_ops *kf_ops;
3182
3183                 WARN_ON(cft->ss || cft->kf_ops);
3184
3185                 if (cft->seq_start)
3186                         kf_ops = &cgroup_kf_ops;
3187                 else
3188                         kf_ops = &cgroup_kf_single_ops;
3189
3190                 /*
3191                  * Ugh... if @cft wants a custom max_write_len, we need to
3192                  * make a copy of kf_ops to set its atomic_write_len.
3193                  */
3194                 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) {
3195                         kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL);
3196                         if (!kf_ops) {
3197                                 cgroup_exit_cftypes(cfts);
3198                                 return -ENOMEM;
3199                         }
3200                         kf_ops->atomic_write_len = cft->max_write_len;
3201                 }
3202
3203                 cft->kf_ops = kf_ops;
3204                 cft->ss = ss;
3205         }
3206
3207         return 0;
3208 }
3209
3210 static int cgroup_rm_cftypes_locked(struct cftype *cfts)
3211 {
3212         lockdep_assert_held(&cgroup_mutex);
3213
3214         if (!cfts || !cfts[0].ss)
3215                 return -ENOENT;
3216
3217         list_del(&cfts->node);
3218         cgroup_apply_cftypes(cfts, false);
3219         cgroup_exit_cftypes(cfts);
3220         return 0;
3221 }
3222
3223 /**
3224  * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
3225  * @cfts: zero-length name terminated array of cftypes
3226  *
3227  * Unregister @cfts.  Files described by @cfts are removed from all
3228  * existing cgroups and all future cgroups won't have them either.  This
3229  * function can be called anytime whether @cfts' subsys is attached or not.
3230  *
3231  * Returns 0 on successful unregistration, -ENOENT if @cfts is not
3232  * registered.
3233  */
3234 int cgroup_rm_cftypes(struct cftype *cfts)
3235 {
3236         int ret;
3237
3238         mutex_lock(&cgroup_mutex);
3239         ret = cgroup_rm_cftypes_locked(cfts);
3240         mutex_unlock(&cgroup_mutex);
3241         return ret;
3242 }
3243
3244 /**
3245  * cgroup_add_cftypes - add an array of cftypes to a subsystem
3246  * @ss: target cgroup subsystem
3247  * @cfts: zero-length name terminated array of cftypes
3248  *
3249  * Register @cfts to @ss.  Files described by @cfts are created for all
3250  * existing cgroups to which @ss is attached and all future cgroups will
3251  * have them too.  This function can be called anytime whether @ss is
3252  * attached or not.
3253  *
3254  * Returns 0 on successful registration, -errno on failure.  Note that this
3255  * function currently returns 0 as long as @cfts registration is successful
3256  * even if some file creation attempts on existing cgroups fail.
3257  */
3258 static int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
3259 {
3260         int ret;
3261
3262         if (!cgroup_ssid_enabled(ss->id))
3263                 return 0;
3264
3265         if (!cfts || cfts[0].name[0] == '\0')
3266                 return 0;
3267
3268         ret = cgroup_init_cftypes(ss, cfts);
3269         if (ret)
3270                 return ret;
3271
3272         mutex_lock(&cgroup_mutex);
3273
3274         list_add_tail(&cfts->node, &ss->cfts);
3275         ret = cgroup_apply_cftypes(cfts, true);
3276         if (ret)
3277                 cgroup_rm_cftypes_locked(cfts);
3278
3279         mutex_unlock(&cgroup_mutex);
3280         return ret;
3281 }
3282
3283 /**
3284  * cgroup_add_dfl_cftypes - add an array of cftypes for default hierarchy
3285  * @ss: target cgroup subsystem
3286  * @cfts: zero-length name terminated array of cftypes
3287  *
3288  * Similar to cgroup_add_cftypes() but the added files are only used for
3289  * the default hierarchy.
3290  */
3291 int cgroup_add_dfl_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
3292 {
3293         struct cftype *cft;
3294
3295         for (cft = cfts; cft && cft->name[0] != '\0'; cft++)
3296                 cft->flags |= __CFTYPE_ONLY_ON_DFL;
3297         return cgroup_add_cftypes(ss, cfts);
3298 }
3299
3300 /**
3301  * cgroup_add_legacy_cftypes - add an array of cftypes for legacy hierarchies
3302  * @ss: target cgroup subsystem
3303  * @cfts: zero-length name terminated array of cftypes
3304  *
3305  * Similar to cgroup_add_cftypes() but the added files are only used for
3306  * the legacy hierarchies.
3307  */
3308 int cgroup_add_legacy_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
3309 {
3310         struct cftype *cft;
3311
3312         for (cft = cfts; cft && cft->name[0] != '\0'; cft++)
3313                 cft->flags |= __CFTYPE_NOT_ON_DFL;
3314         return cgroup_add_cftypes(ss, cfts);
3315 }
3316
3317 /**
3318  * cgroup_file_notify - generate a file modified event for a cgroup_file
3319  * @cfile: target cgroup_file
3320  *
3321  * @cfile must have been obtained by setting cftype->file_offset.
3322  */
3323 void cgroup_file_notify(struct cgroup_file *cfile)
3324 {
3325         unsigned long flags;
3326
3327         spin_lock_irqsave(&cgroup_file_kn_lock, flags);
3328         if (cfile->kn)
3329                 kernfs_notify(cfile->kn);
3330         spin_unlock_irqrestore(&cgroup_file_kn_lock, flags);
3331 }
3332
3333 /**
3334  * css_next_child - find the next child of a given css
3335  * @pos: the current position (%NULL to initiate traversal)
3336  * @parent: css whose children to walk
3337  *
3338  * This function returns the next child of @parent and should be called
3339  * under either cgroup_mutex or RCU read lock.  The only requirement is
3340  * that @parent and @pos are accessible.  The next sibling is guaranteed to
3341  * be returned regardless of their states.
3342  *
3343  * If a subsystem synchronizes ->css_online() and the start of iteration, a
3344  * css which finished ->css_online() is guaranteed to be visible in the
3345  * future iterations and will stay visible until the last reference is put.
3346  * A css which hasn't finished ->css_online() or already finished
3347  * ->css_offline() may show up during traversal.  It's each subsystem's
3348  * responsibility to synchronize against on/offlining.
3349  */
3350 struct cgroup_subsys_state *css_next_child(struct cgroup_subsys_state *pos,
3351                                            struct cgroup_subsys_state *parent)
3352 {
3353         struct cgroup_subsys_state *next;
3354
3355         cgroup_assert_mutex_or_rcu_locked();
3356
3357         /*
3358          * @pos could already have been unlinked from the sibling list.
3359          * Once a cgroup is removed, its ->sibling.next is no longer
3360          * updated when its next sibling changes.  CSS_RELEASED is set when
3361          * @pos is taken off list, at which time its next pointer is valid,
3362          * and, as releases are serialized, the one pointed to by the next
3363          * pointer is guaranteed to not have started release yet.  This
3364          * implies that if we observe !CSS_RELEASED on @pos in this RCU
3365          * critical section, the one pointed to by its next pointer is
3366          * guaranteed to not have finished its RCU grace period even if we
3367          * have dropped rcu_read_lock() inbetween iterations.
3368          *
3369          * If @pos has CSS_RELEASED set, its next pointer can't be
3370          * dereferenced; however, as each css is given a monotonically
3371          * increasing unique serial number and always appended to the
3372          * sibling list, the next one can be found by walking the parent's
3373          * children until the first css with higher serial number than
3374          * @pos's.  While this path can be slower, it happens iff iteration
3375          * races against release and the race window is very small.
3376          */
3377         if (!pos) {
3378                 next = list_entry_rcu(parent->children.next, struct cgroup_subsys_state, sibling);
3379         } else if (likely(!(pos->flags & CSS_RELEASED))) {
3380                 next = list_entry_rcu(pos->sibling.next, struct cgroup_subsys_state, sibling);
3381         } else {
3382                 list_for_each_entry_rcu(next, &parent->children, sibling)
3383                         if (next->serial_nr > pos->serial_nr)
3384                                 break;
3385         }
3386
3387         /*
3388          * @next, if not pointing to the head, can be dereferenced and is
3389          * the next sibling.
3390          */
3391         if (&next->sibling != &parent->children)
3392                 return next;
3393         return NULL;
3394 }
3395
3396 /**
3397  * css_next_descendant_pre - find the next descendant for pre-order walk
3398  * @pos: the current position (%NULL to initiate traversal)
3399  * @root: css whose descendants to walk
3400  *
3401  * To be used by css_for_each_descendant_pre().  Find the next descendant
3402  * to visit for pre-order traversal of @root's descendants.  @root is
3403  * included in the iteration and the first node to be visited.
3404  *
3405  * While this function requires cgroup_mutex or RCU read locking, it
3406  * doesn't require the whole traversal to be contained in a single critical
3407  * section.  This function will return the correct next descendant as long
3408  * as both @pos and @root are accessible and @pos is a descendant of @root.
3409  *
3410  * If a subsystem synchronizes ->css_online() and the start of iteration, a
3411  * css which finished ->css_online() is guaranteed to be visible in the
3412  * future iterations and will stay visible until the last reference is put.
3413  * A css which hasn't finished ->css_online() or already finished
3414  * ->css_offline() may show up during traversal.  It's each subsystem's
3415  * responsibility to synchronize against on/offlining.
3416  */
3417 struct cgroup_subsys_state *
3418 css_next_descendant_pre(struct cgroup_subsys_state *pos,
3419                         struct cgroup_subsys_state *root)
3420 {
3421         struct cgroup_subsys_state *next;
3422
3423         cgroup_assert_mutex_or_rcu_locked();
3424
3425         /* if first iteration, visit @root */
3426         if (!pos)
3427                 return root;
3428
3429         /* visit the first child if exists */
3430         next = css_next_child(NULL, pos);
3431         if (next)
3432                 return next;
3433
3434         /* no child, visit my or the closest ancestor's next sibling */
3435         while (pos != root) {
3436                 next = css_next_child(pos, pos->parent);
3437                 if (next)
3438                         return next;
3439                 pos = pos->parent;
3440         }
3441
3442         return NULL;
3443 }
3444
3445 /**
3446  * css_rightmost_descendant - return the rightmost descendant of a css
3447  * @pos: css of interest
3448  *
3449  * Return the rightmost descendant of @pos.  If there's no descendant, @pos
3450  * is returned.  This can be used during pre-order traversal to skip
3451  * subtree of @pos.
3452  *
3453  * While this function requires cgroup_mutex or RCU read locking, it
3454  * doesn't require the whole traversal to be contained in a single critical
3455  * section.  This function will return the correct rightmost descendant as
3456  * long as @pos is accessible.
3457  */
3458 struct cgroup_subsys_state *
3459 css_rightmost_descendant(struct cgroup_subsys_state *pos)
3460 {
3461         struct cgroup_subsys_state *last, *tmp;
3462
3463         cgroup_assert_mutex_or_rcu_locked();
3464
3465         do {
3466                 last = pos;
3467                 /* ->prev isn't RCU safe, walk ->next till the end */
3468                 pos = NULL;
3469                 css_for_each_child(tmp, last)
3470                         pos = tmp;
3471         } while (pos);
3472
3473         return last;
3474 }
3475
3476 static struct cgroup_subsys_state *
3477 css_leftmost_descendant(struct cgroup_subsys_state *pos)
3478 {
3479         struct cgroup_subsys_state *last;
3480
3481         do {
3482                 last = pos;
3483                 pos = css_next_child(NULL, pos);
3484         } while (pos);
3485
3486         return last;
3487 }
3488
3489 /**
3490  * css_next_descendant_post - find the next descendant for post-order walk
3491  * @pos: the current position (%NULL to initiate traversal)
3492  * @root: css whose descendants to walk
3493  *
3494  * To be used by css_for_each_descendant_post().  Find the next descendant
3495  * to visit for post-order traversal of @root's descendants.  @root is
3496  * included in the iteration and the last node to be visited.
3497  *
3498  * While this function requires cgroup_mutex or RCU read locking, it
3499  * doesn't require the whole traversal to be contained in a single critical
3500  * section.  This function will return the correct next descendant as long
3501  * as both @pos and @cgroup are accessible and @pos is a descendant of
3502  * @cgroup.
3503  *
3504  * If a subsystem synchronizes ->css_online() and the start of iteration, a
3505  * css which finished ->css_online() is guaranteed to be visible in the
3506  * future iterations and will stay visible until the last reference is put.
3507  * A css which hasn't finished ->css_online() or already finished
3508  * ->css_offline() may show up during traversal.  It's each subsystem's
3509  * responsibility to synchronize against on/offlining.
3510  */
3511 struct cgroup_subsys_state *
3512 css_next_descendant_post(struct cgroup_subsys_state *pos,
3513                          struct cgroup_subsys_state *root)
3514 {
3515         struct cgroup_subsys_state *next;
3516
3517         cgroup_assert_mutex_or_rcu_locked();
3518
3519         /* if first iteration, visit leftmost descendant which may be @root */
3520         if (!pos)
3521                 return css_leftmost_descendant(root);
3522
3523         /* if we visited @root, we're done */
3524         if (pos == root)
3525                 return NULL;
3526
3527         /* if there's an unvisited sibling, visit its leftmost descendant */
3528         next = css_next_child(pos, pos->parent);
3529         if (next)
3530                 return css_leftmost_descendant(next);
3531
3532         /* no sibling left, visit parent */
3533         return pos->parent;
3534 }
3535
3536 /**
3537  * css_has_online_children - does a css have online children
3538  * @css: the target css
3539  *
3540  * Returns %true if @css has any online children; otherwise, %false.  This
3541  * function can be called from any context but the caller is responsible
3542  * for synchronizing against on/offlining as necessary.
3543  */
3544 bool css_has_online_children(struct cgroup_subsys_state *css)
3545 {
3546         struct cgroup_subsys_state *child;
3547         bool ret = false;
3548
3549         rcu_read_lock();
3550         css_for_each_child(child, css) {
3551                 if (child->flags & CSS_ONLINE) {
3552                         ret = true;
3553                         break;
3554                 }
3555         }
3556         rcu_read_unlock();
3557         return ret;
3558 }
3559
3560 /**
3561  * css_task_iter_advance_css_set - advance a task itererator to the next css_set
3562  * @it: the iterator to advance
3563  *
3564  * Advance @it to the next css_set to walk.
3565  */
3566 static void css_task_iter_advance_css_set(struct css_task_iter *it)
3567 {
3568         struct list_head *l = it->cset_pos;
3569         struct cgrp_cset_link *link;
3570         struct css_set *cset;
3571
3572         lockdep_assert_held(&css_set_lock);
3573
3574         /* Advance to the next non-empty css_set */
3575         do {
3576                 l = l->next;
3577                 if (l == it->cset_head) {
3578                         it->cset_pos = NULL;
3579                         it->task_pos = NULL;
3580                         return;
3581                 }
3582
3583                 if (it->ss) {
3584                         cset = container_of(l, struct css_set,
3585                                             e_cset_node[it->ss->id]);
3586                 } else {
3587                         link = list_entry(l, struct cgrp_cset_link, cset_link);
3588                         cset = link->cset;
3589                 }
3590         } while (!css_set_populated(cset));
3591
3592         it->cset_pos = l;
3593
3594         if (!list_empty(&cset->tasks))
3595                 it->task_pos = cset->tasks.next;
3596         else
3597                 it->task_pos = cset->mg_tasks.next;
3598
3599         it->tasks_head = &cset->tasks;
3600         it->mg_tasks_head = &cset->mg_tasks;
3601
3602         /*
3603          * We don't keep css_sets locked across iteration steps and thus
3604          * need to take steps to ensure that iteration can be resumed after
3605          * the lock is re-acquired.  Iteration is performed at two levels -
3606          * css_sets and tasks in them.
3607          *
3608          * Once created, a css_set never leaves its cgroup lists, so a
3609          * pinned css_set is guaranteed to stay put and we can resume
3610          * iteration afterwards.
3611          *
3612          * Tasks may leave @cset across iteration steps.  This is resolved
3613          * by registering each iterator with the css_set currently being
3614          * walked and making css_set_move_task() advance iterators whose
3615          * next task is leaving.
3616          */
3617         if (it->cur_cset) {
3618                 list_del(&it->iters_node);
3619                 put_css_set_locked(it->cur_cset);
3620         }
3621         get_css_set(cset);
3622         it->cur_cset = cset;
3623         list_add(&it->iters_node, &cset->task_iters);
3624 }
3625
3626 static void css_task_iter_advance(struct css_task_iter *it)
3627 {
3628         struct list_head *l = it->task_pos;
3629
3630         lockdep_assert_held(&css_set_lock);
3631         WARN_ON_ONCE(!l);
3632
3633         /*
3634          * Advance iterator to find next entry.  cset->tasks is consumed
3635          * first and then ->mg_tasks.  After ->mg_tasks, we move onto the
3636          * next cset.
3637          */
3638         l = l->next;
3639
3640         if (l == it->tasks_head)
3641                 l = it->mg_tasks_head->next;
3642
3643         if (l == it->mg_tasks_head)
3644                 css_task_iter_advance_css_set(it);
3645         else
3646                 it->task_pos = l;
3647 }
3648
3649 /**
3650  * css_task_iter_start - initiate task iteration
3651  * @css: the css to walk tasks of
3652  * @it: the task iterator to use
3653  *
3654  * Initiate iteration through the tasks of @css.  The caller can call
3655  * css_task_iter_next() to walk through the tasks until the function
3656  * returns NULL.  On completion of iteration, css_task_iter_end() must be
3657  * called.
3658  */
3659 void css_task_iter_start(struct cgroup_subsys_state *css,
3660                          struct css_task_iter *it)
3661 {
3662         /* no one should try to iterate before mounting cgroups */
3663         WARN_ON_ONCE(!use_task_css_set_links);
3664
3665         memset(it, 0, sizeof(*it));
3666
3667         spin_lock_irq(&css_set_lock);
3668
3669         it->ss = css->ss;
3670
3671         if (it->ss)
3672                 it->cset_pos = &css->cgroup->e_csets[css->ss->id];
3673         else
3674                 it->cset_pos = &css->cgroup->cset_links;
3675
3676         it->cset_head = it->cset_pos;
3677
3678         css_task_iter_advance_css_set(it);
3679
3680         spin_unlock_irq(&css_set_lock);
3681 }
3682
3683 /**
3684  * css_task_iter_next - return the next task for the iterator
3685  * @it: the task iterator being iterated
3686  *
3687  * The "next" function for task iteration.  @it should have been
3688  * initialized via css_task_iter_start().  Returns NULL when the iteration
3689  * reaches the end.
3690  */
3691 struct task_struct *css_task_iter_next(struct css_task_iter *it)
3692 {
3693         if (it->cur_task) {
3694                 put_task_struct(it->cur_task);
3695                 it->cur_task = NULL;
3696         }
3697
3698         spin_lock_irq(&css_set_lock);
3699
3700         if (it->task_pos) {
3701                 it->cur_task = list_entry(it->task_pos, struct task_struct,
3702                                           cg_list);
3703                 get_task_struct(it->cur_task);
3704                 css_task_iter_advance(it);
3705         }
3706
3707         spin_unlock_irq(&css_set_lock);
3708
3709         return it->cur_task;
3710 }
3711
3712 /**
3713  * css_task_iter_end - finish task iteration
3714  * @it: the task iterator to finish
3715  *
3716  * Finish task iteration started by css_task_iter_start().
3717  */
3718 void css_task_iter_end(struct css_task_iter *it)
3719 {
3720         if (it->cur_cset) {
3721                 spin_lock_irq(&css_set_lock);
3722                 list_del(&it->iters_node);
3723                 put_css_set_locked(it->cur_cset);
3724                 spin_unlock_irq(&css_set_lock);
3725         }
3726
3727         if (it->cur_task)
3728                 put_task_struct(it->cur_task);
3729 }
3730
3731 static void cgroup_procs_release(struct kernfs_open_file *of)
3732 {
3733         if (of->priv) {
3734                 css_task_iter_end(of->priv);
3735                 kfree(of->priv);
3736         }
3737 }
3738
3739 static void *cgroup_procs_next(struct seq_file *s, void *v, loff_t *pos)
3740 {
3741         struct kernfs_open_file *of = s->private;
3742         struct css_task_iter *it = of->priv;
3743         struct task_struct *task;
3744
3745         do {
3746                 task = css_task_iter_next(it);
3747         } while (task && !thread_group_leader(task));
3748
3749         return task;
3750 }
3751
3752 static void *cgroup_procs_start(struct seq_file *s, loff_t *pos)
3753 {
3754         struct kernfs_open_file *of = s->private;
3755         struct cgroup *cgrp = seq_css(s)->cgroup;
3756         struct css_task_iter *it = of->priv;
3757
3758         /*
3759          * When a seq_file is seeked, it's always traversed sequentially
3760          * from position 0, so we can simply keep iterating on !0 *pos.
3761          */
3762         if (!it) {
3763                 if (WARN_ON_ONCE((*pos)++))
3764                         return ERR_PTR(-EINVAL);
3765
3766                 it = kzalloc(sizeof(*it), GFP_KERNEL);
3767                 if (!it)
3768                         return ERR_PTR(-ENOMEM);
3769                 of->priv = it;
3770                 css_task_iter_start(&cgrp->self, it);
3771         } else if (!(*pos)++) {
3772                 css_task_iter_end(it);
3773                 css_task_iter_start(&cgrp->self, it);
3774         }
3775
3776         return cgroup_procs_next(s, NULL, NULL);
3777 }
3778
3779 static int cgroup_procs_show(struct seq_file *s, void *v)
3780 {
3781         seq_printf(s, "%d\n", task_tgid_vnr(v));
3782         return 0;
3783 }
3784
3785 /* cgroup core interface files for the default hierarchy */
3786 static struct cftype cgroup_base_files[] = {
3787         {
3788                 .name = "cgroup.procs",
3789                 .file_offset = offsetof(struct cgroup, procs_file),
3790                 .release = cgroup_procs_release,
3791                 .seq_start = cgroup_procs_start,
3792                 .seq_next = cgroup_procs_next,
3793                 .seq_show = cgroup_procs_show,
3794                 .write = cgroup_procs_write,
3795         },
3796         {
3797                 .name = "cgroup.controllers",
3798                 .seq_show = cgroup_controllers_show,
3799         },
3800         {
3801                 .name = "cgroup.subtree_control",
3802                 .seq_show = cgroup_subtree_control_show,
3803                 .write = cgroup_subtree_control_write,
3804         },
3805         {
3806                 .name = "cgroup.events",
3807                 .flags = CFTYPE_NOT_ON_ROOT,
3808                 .file_offset = offsetof(struct cgroup, events_file),
3809                 .seq_show = cgroup_events_show,
3810         },
3811         { }     /* terminate */
3812 };
3813
3814 /*
3815  * css destruction is four-stage process.
3816  *
3817  * 1. Destruction starts.  Killing of the percpu_ref is initiated.
3818  *    Implemented in kill_css().
3819  *
3820  * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
3821  *    and thus css_tryget_online() is guaranteed to fail, the css can be
3822  *    offlined by invoking offline_css().  After offlining, the base ref is
3823  *    put.  Implemented in css_killed_work_fn().
3824  *
3825  * 3. When the percpu_ref reaches zero, the only possible remaining
3826  *    accessors are inside RCU read sections.  css_release() schedules the
3827  *    RCU callback.
3828  *
3829  * 4. After the grace period, the css can be freed.  Implemented in
3830  *    css_free_work_fn().
3831  *
3832  * It is actually hairier because both step 2 and 4 require process context
3833  * and thus involve punting to css->destroy_work adding two additional
3834  * steps to the already complex sequence.
3835  */
3836 static void css_free_work_fn(struct work_struct *work)
3837 {
3838         struct cgroup_subsys_state *css =
3839                 container_of(work, struct cgroup_subsys_state, destroy_work);
3840         struct cgroup_subsys *ss = css->ss;
3841         struct cgroup *cgrp = css->cgroup;
3842
3843         percpu_ref_exit(&css->refcnt);
3844
3845         if (ss) {
3846                 /* css free path */
3847                 struct cgroup_subsys_state *parent = css->parent;
3848                 int id = css->id;
3849
3850                 ss->css_free(css);
3851                 cgroup_idr_remove(&ss->css_idr, id);
3852                 cgroup_put(cgrp);
3853
3854                 if (parent)
3855                         css_put(parent);
3856         } else {
3857                 /* cgroup free path */
3858                 atomic_dec(&cgrp->root->nr_cgrps);
3859                 cgroup1_pidlist_destroy_all(cgrp);
3860                 cancel_work_sync(&cgrp->release_agent_work);
3861
3862                 if (cgroup_parent(cgrp)) {
3863                         /*
3864                          * We get a ref to the parent, and put the ref when
3865                          * this cgroup is being freed, so it's guaranteed
3866                          * that the parent won't be destroyed before its
3867                          * children.
3868                          */
3869                         cgroup_put(cgroup_parent(cgrp));
3870                         kernfs_put(cgrp->kn);
3871                         kfree(cgrp);
3872                 } else {
3873                         /*
3874                          * This is root cgroup's refcnt reaching zero,
3875                          * which indicates that the root should be
3876                          * released.
3877                          */
3878                         cgroup_destroy_root(cgrp->root);
3879                 }
3880         }
3881 }
3882
3883 static void css_free_rcu_fn(struct rcu_head *rcu_head)
3884 {
3885         struct cgroup_subsys_state *css =
3886                 container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
3887
3888         INIT_WORK(&css->destroy_work, css_free_work_fn);
3889         queue_work(cgroup_destroy_wq, &css->destroy_work);
3890 }
3891
3892 static void css_release_work_fn(struct work_struct *work)
3893 {
3894         struct cgroup_subsys_state *css =
3895                 container_of(work, struct cgroup_subsys_state, destroy_work);
3896         struct cgroup_subsys *ss = css->ss;
3897         struct cgroup *cgrp = css->cgroup;
3898
3899         mutex_lock(&cgroup_mutex);
3900
3901         css->flags |= CSS_RELEASED;
3902         list_del_rcu(&css->sibling);
3903
3904         if (ss) {
3905                 /* css release path */
3906                 cgroup_idr_replace(&ss->css_idr, NULL, css->id);
3907                 if (ss->css_released)
3908                         ss->css_released(css);
3909         } else {
3910                 /* cgroup release path */
3911                 trace_cgroup_release(cgrp);
3912
3913                 cgroup_idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
3914                 cgrp->id = -1;
3915
3916                 /*
3917                  * There are two control paths which try to determine
3918                  * cgroup from dentry without going through kernfs -
3919                  * cgroupstats_build() and css_tryget_online_from_dir().
3920                  * Those are supported by RCU protecting clearing of
3921                  * cgrp->kn->priv backpointer.
3922                  */
3923                 if (cgrp->kn)
3924                         RCU_INIT_POINTER(*(void __rcu __force **)&cgrp->kn->priv,
3925                                          NULL);
3926
3927                 cgroup_bpf_put(cgrp);
3928         }
3929
3930         mutex_unlock(&cgroup_mutex);
3931
3932         call_rcu(&css->rcu_head, css_free_rcu_fn);
3933 }
3934
3935 static void css_release(struct percpu_ref *ref)
3936 {
3937         struct cgroup_subsys_state *css =
3938                 container_of(ref, struct cgroup_subsys_state, refcnt);
3939
3940         INIT_WORK(&css->destroy_work, css_release_work_fn);
3941         queue_work(cgroup_destroy_wq, &css->destroy_work);
3942 }
3943
3944 static void init_and_link_css(struct cgroup_subsys_state *css,
3945                               struct cgroup_subsys *ss, struct cgroup *cgrp)
3946 {
3947         lockdep_assert_held(&cgroup_mutex);
3948
3949         cgroup_get(cgrp);
3950
3951         memset(css, 0, sizeof(*css));
3952         css->cgroup = cgrp;
3953         css->ss = ss;
3954         css->id = -1;
3955         INIT_LIST_HEAD(&css->sibling);
3956         INIT_LIST_HEAD(&css->children);
3957         css->serial_nr = css_serial_nr_next++;
3958         atomic_set(&css->online_cnt, 0);
3959
3960         if (cgroup_parent(cgrp)) {
3961                 css->parent = cgroup_css(cgroup_parent(cgrp), ss);
3962                 css_get(css->parent);
3963         }
3964
3965         BUG_ON(cgroup_css(cgrp, ss));
3966 }
3967
3968 /* invoke ->css_online() on a new CSS and mark it online if successful */
3969 static int online_css(struct cgroup_subsys_state *css)
3970 {
3971         struct cgroup_subsys *ss = css->ss;
3972         int ret = 0;
3973
3974         lockdep_assert_held(&cgroup_mutex);
3975
3976         if (ss->css_online)
3977                 ret = ss->css_online(css);
3978         if (!ret) {
3979                 css->flags |= CSS_ONLINE;
3980                 rcu_assign_pointer(css->cgroup->subsys[ss->id], css);
3981
3982                 atomic_inc(&css->online_cnt);
3983                 if (css->parent)
3984                         atomic_inc(&css->parent->online_cnt);
3985         }
3986         return ret;
3987 }
3988
3989 /* if the CSS is online, invoke ->css_offline() on it and mark it offline */
3990 static void offline_css(struct cgroup_subsys_state *css)
3991 {
3992         struct cgroup_subsys *ss = css->ss;
3993
3994         lockdep_assert_held(&cgroup_mutex);
3995
3996         if (!(css->flags & CSS_ONLINE))
3997                 return;
3998
3999         if (ss->css_reset)
4000                 ss->css_reset(css);
4001
4002         if (ss->css_offline)
4003                 ss->css_offline(css);
4004
4005         css->flags &= ~CSS_ONLINE;
4006         RCU_INIT_POINTER(css->cgroup->subsys[ss->id], NULL);
4007
4008         wake_up_all(&css->cgroup->offline_waitq);
4009 }
4010
4011 /**
4012  * css_create - create a cgroup_subsys_state
4013  * @cgrp: the cgroup new css will be associated with
4014  * @ss: the subsys of new css
4015  *
4016  * Create a new css associated with @cgrp - @ss pair.  On success, the new
4017  * css is online and installed in @cgrp.  This function doesn't create the
4018  * interface files.  Returns 0 on success, -errno on failure.
4019  */
4020 static struct cgroup_subsys_state *css_create(struct cgroup *cgrp,
4021                                               struct cgroup_subsys *ss)
4022 {
4023         struct cgroup *parent = cgroup_parent(cgrp);
4024         struct cgroup_subsys_state *parent_css = cgroup_css(parent, ss);
4025         struct cgroup_subsys_state *css;
4026         int err;
4027
4028         lockdep_assert_held(&cgroup_mutex);
4029
4030         css = ss->css_alloc(parent_css);
4031         if (!css)
4032                 css = ERR_PTR(-ENOMEM);
4033         if (IS_ERR(css))
4034                 return css;
4035
4036         init_and_link_css(css, ss, cgrp);
4037
4038         err = percpu_ref_init(&css->refcnt, css_release, 0, GFP_KERNEL);
4039         if (err)
4040                 goto err_free_css;
4041
4042         err = cgroup_idr_alloc(&ss->css_idr, NULL, 2, 0, GFP_KERNEL);
4043         if (err < 0)
4044                 goto err_free_css;
4045         css->id = err;
4046
4047         /* @css is ready to be brought online now, make it visible */
4048         list_add_tail_rcu(&css->sibling, &parent_css->children);
4049         cgroup_idr_replace(&ss->css_idr, css, css->id);
4050
4051         err = online_css(css);
4052         if (err)
4053                 goto err_list_del;
4054
4055         if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4056             cgroup_parent(parent)) {
4057                 pr_warn("%s (%d) created nested cgroup for controller \"%s\" which has incomplete hierarchy support. Nested cgroups may change behavior in the future.\n",
4058                         current->comm, current->pid, ss->name);
4059                 if (!strcmp(ss->name, "memory"))
4060                         pr_warn("\"memory\" requires setting use_hierarchy to 1 on the root\n");
4061                 ss->warned_broken_hierarchy = true;
4062         }
4063
4064         return css;
4065
4066 err_list_del:
4067         list_del_rcu(&css->sibling);
4068 err_free_css:
4069         call_rcu(&css->rcu_head, css_free_rcu_fn);
4070         return ERR_PTR(err);
4071 }
4072
4073 /*
4074  * The returned cgroup is fully initialized including its control mask, but
4075  * it isn't associated with its kernfs_node and doesn't have the control
4076  * mask applied.
4077  */
4078 static struct cgroup *cgroup_create(struct cgroup *parent)
4079 {
4080         struct cgroup_root *root = parent->root;
4081         struct cgroup *cgrp, *tcgrp;
4082         int level = parent->level + 1;
4083         int ret;
4084
4085         /* allocate the cgroup and its ID, 0 is reserved for the root */
4086         cgrp = kzalloc(sizeof(*cgrp) +
4087                        sizeof(cgrp->ancestor_ids[0]) * (level + 1), GFP_KERNEL);
4088         if (!cgrp)
4089                 return ERR_PTR(-ENOMEM);
4090
4091         ret = percpu_ref_init(&cgrp->self.refcnt, css_release, 0, GFP_KERNEL);
4092         if (ret)
4093                 goto out_free_cgrp;
4094
4095         /*
4096          * Temporarily set the pointer to NULL, so idr_find() won't return
4097          * a half-baked cgroup.
4098          */
4099         cgrp->id = cgroup_idr_alloc(&root->cgroup_idr, NULL, 2, 0, GFP_KERNEL);
4100         if (cgrp->id < 0) {
4101                 ret = -ENOMEM;
4102                 goto out_cancel_ref;
4103         }
4104
4105         init_cgroup_housekeeping(cgrp);
4106
4107         cgrp->self.parent = &parent->self;
4108         cgrp->root = root;
4109         cgrp->level = level;
4110
4111         for (tcgrp = cgrp; tcgrp; tcgrp = cgroup_parent(tcgrp))
4112                 cgrp->ancestor_ids[tcgrp->level] = tcgrp->id;
4113
4114         if (notify_on_release(parent))
4115                 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4116
4117         if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4118                 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
4119
4120         cgrp->self.serial_nr = css_serial_nr_next++;
4121
4122         /* allocation complete, commit to creation */
4123         list_add_tail_rcu(&cgrp->self.sibling, &cgroup_parent(cgrp)->self.children);
4124         atomic_inc(&root->nr_cgrps);
4125         cgroup_get(parent);
4126
4127         /*
4128          * @cgrp is now fully operational.  If something fails after this
4129          * point, it'll be released via the normal destruction path.
4130          */
4131         cgroup_idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
4132
4133         /*
4134          * On the default hierarchy, a child doesn't automatically inherit
4135          * subtree_control from the parent.  Each is configured manually.
4136          */
4137         if (!cgroup_on_dfl(cgrp))
4138                 cgrp->subtree_control = cgroup_control(cgrp);
4139
4140         if (parent)
4141                 cgroup_bpf_inherit(cgrp, parent);
4142
4143         cgroup_propagate_control(cgrp);
4144
4145         return cgrp;
4146
4147 out_cancel_ref:
4148         percpu_ref_exit(&cgrp->self.refcnt);
4149 out_free_cgrp:
4150         kfree(cgrp);
4151         return ERR_PTR(ret);
4152 }
4153
4154 int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name, umode_t mode)
4155 {
4156         struct cgroup *parent, *cgrp;
4157         struct kernfs_node *kn;
4158         int ret;
4159
4160         /* do not accept '\n' to prevent making /proc/<pid>/cgroup unparsable */
4161         if (strchr(name, '\n'))
4162                 return -EINVAL;
4163
4164         parent = cgroup_kn_lock_live(parent_kn, false);
4165         if (!parent)
4166                 return -ENODEV;
4167
4168         cgrp = cgroup_create(parent);
4169         if (IS_ERR(cgrp)) {
4170                 ret = PTR_ERR(cgrp);
4171                 goto out_unlock;
4172         }
4173
4174         /* create the directory */
4175         kn = kernfs_create_dir(parent->kn, name, mode, cgrp);
4176         if (IS_ERR(kn)) {
4177                 ret = PTR_ERR(kn);
4178                 goto out_destroy;
4179         }
4180         cgrp->kn = kn;
4181
4182         /*
4183          * This extra ref will be put in cgroup_free_fn() and guarantees
4184          * that @cgrp->kn is always accessible.
4185          */
4186         kernfs_get(kn);
4187
4188         ret = cgroup_kn_set_ugid(kn);
4189         if (ret)
4190                 goto out_destroy;
4191
4192         ret = css_populate_dir(&cgrp->self);
4193         if (ret)
4194                 goto out_destroy;
4195
4196         ret = cgroup_apply_control_enable(cgrp);
4197         if (ret)
4198                 goto out_destroy;
4199
4200         trace_cgroup_mkdir(cgrp);
4201
4202         /* let's create and online css's */
4203         kernfs_activate(kn);
4204
4205         ret = 0;
4206         goto out_unlock;
4207
4208 out_destroy:
4209         cgroup_destroy_locked(cgrp);
4210 out_unlock:
4211         cgroup_kn_unlock(parent_kn);
4212         return ret;
4213 }
4214
4215 /*
4216  * This is called when the refcnt of a css is confirmed to be killed.
4217  * css_tryget_online() is now guaranteed to fail.  Tell the subsystem to
4218  * initate destruction and put the css ref from kill_css().
4219  */
4220 static void css_killed_work_fn(struct work_struct *work)
4221 {
4222         struct cgroup_subsys_state *css =
4223                 container_of(work, struct cgroup_subsys_state, destroy_work);
4224
4225         mutex_lock(&cgroup_mutex);
4226
4227         do {
4228                 offline_css(css);
4229                 css_put(css);
4230                 /* @css can't go away while we're holding cgroup_mutex */
4231                 css = css->parent;
4232         } while (css && atomic_dec_and_test(&css->online_cnt));
4233
4234         mutex_unlock(&cgroup_mutex);
4235 }
4236
4237 /* css kill confirmation processing requires process context, bounce */
4238 static void css_killed_ref_fn(struct percpu_ref *ref)
4239 {
4240         struct cgroup_subsys_state *css =
4241                 container_of(ref, struct cgroup_subsys_state, refcnt);
4242
4243         if (atomic_dec_and_test(&css->online_cnt)) {
4244                 INIT_WORK(&css->destroy_work, css_killed_work_fn);
4245                 queue_work(cgroup_destroy_wq, &css->destroy_work);
4246         }
4247 }
4248
4249 /**
4250  * kill_css - destroy a css
4251  * @css: css to destroy
4252  *
4253  * This function initiates destruction of @css by removing cgroup interface
4254  * files and putting its base reference.  ->css_offline() will be invoked
4255  * asynchronously once css_tryget_online() is guaranteed to fail and when
4256  * the reference count reaches zero, @css will be released.
4257  */
4258 static void kill_css(struct cgroup_subsys_state *css)
4259 {
4260         lockdep_assert_held(&cgroup_mutex);
4261
4262         /*
4263          * This must happen before css is disassociated with its cgroup.
4264          * See seq_css() for details.
4265          */
4266         css_clear_dir(css);
4267
4268         /*
4269          * Killing would put the base ref, but we need to keep it alive
4270          * until after ->css_offline().
4271          */
4272         css_get(css);
4273
4274         /*
4275          * cgroup core guarantees that, by the time ->css_offline() is
4276          * invoked, no new css reference will be given out via
4277          * css_tryget_online().  We can't simply call percpu_ref_kill() and
4278          * proceed to offlining css's because percpu_ref_kill() doesn't
4279          * guarantee that the ref is seen as killed on all CPUs on return.
4280          *
4281          * Use percpu_ref_kill_and_confirm() to get notifications as each
4282          * css is confirmed to be seen as killed on all CPUs.
4283          */
4284         percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
4285 }
4286
4287 /**
4288  * cgroup_destroy_locked - the first stage of cgroup destruction
4289  * @cgrp: cgroup to be destroyed
4290  *
4291  * css's make use of percpu refcnts whose killing latency shouldn't be
4292  * exposed to userland and are RCU protected.  Also, cgroup core needs to
4293  * guarantee that css_tryget_online() won't succeed by the time
4294  * ->css_offline() is invoked.  To satisfy all the requirements,
4295  * destruction is implemented in the following two steps.
4296  *
4297  * s1. Verify @cgrp can be destroyed and mark it dying.  Remove all
4298  *     userland visible parts and start killing the percpu refcnts of
4299  *     css's.  Set up so that the next stage will be kicked off once all
4300  *     the percpu refcnts are confirmed to be killed.
4301  *
4302  * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4303  *     rest of destruction.  Once all cgroup references are gone, the
4304  *     cgroup is RCU-freed.
4305  *
4306  * This function implements s1.  After this step, @cgrp is gone as far as
4307  * the userland is concerned and a new cgroup with the same name may be
4308  * created.  As cgroup doesn't care about the names internally, this
4309  * doesn't cause any problem.
4310  */
4311 static int cgroup_destroy_locked(struct cgroup *cgrp)
4312         __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
4313 {
4314         struct cgroup_subsys_state *css;
4315         struct cgrp_cset_link *link;
4316         int ssid;
4317
4318         lockdep_assert_held(&cgroup_mutex);
4319
4320         /*
4321          * Only migration can raise populated from zero and we're already
4322          * holding cgroup_mutex.
4323          */
4324         if (cgroup_is_populated(cgrp))
4325                 return -EBUSY;
4326
4327         /*
4328          * Make sure there's no live children.  We can't test emptiness of
4329          * ->self.children as dead children linger on it while being
4330          * drained; otherwise, "rmdir parent/child parent" may fail.
4331          */
4332         if (css_has_online_children(&cgrp->self))
4333                 return -EBUSY;
4334
4335         /*
4336          * Mark @cgrp and the associated csets dead.  The former prevents
4337          * further task migration and child creation by disabling
4338          * cgroup_lock_live_group().  The latter makes the csets ignored by
4339          * the migration path.
4340          */
4341         cgrp->self.flags &= ~CSS_ONLINE;
4342
4343         spin_lock_irq(&css_set_lock);
4344         list_for_each_entry(link, &cgrp->cset_links, cset_link)
4345                 link->cset->dead = true;
4346         spin_unlock_irq(&css_set_lock);
4347
4348         /* initiate massacre of all css's */
4349         for_each_css(css, ssid, cgrp)
4350                 kill_css(css);
4351
4352         /*
4353          * Remove @cgrp directory along with the base files.  @cgrp has an
4354          * extra ref on its kn.
4355          */
4356         kernfs_remove(cgrp->kn);
4357
4358         cgroup1_check_for_release(cgroup_parent(cgrp));
4359
4360         /* put the base reference */
4361         percpu_ref_kill(&cgrp->self.refcnt);
4362
4363         return 0;
4364 };
4365
4366 int cgroup_rmdir(struct kernfs_node *kn)
4367 {
4368         struct cgroup *cgrp;
4369         int ret = 0;
4370
4371         cgrp = cgroup_kn_lock_live(kn, false);
4372         if (!cgrp)
4373                 return 0;
4374
4375         ret = cgroup_destroy_locked(cgrp);
4376
4377         if (!ret)
4378                 trace_cgroup_rmdir(cgrp);
4379
4380         cgroup_kn_unlock(kn);
4381         return ret;
4382 }
4383
4384 static struct kernfs_syscall_ops cgroup_kf_syscall_ops = {
4385         .remount_fs             = cgroup_remount,
4386         .mkdir                  = cgroup_mkdir,
4387         .rmdir                  = cgroup_rmdir,
4388         .show_path              = cgroup_show_path,
4389 };
4390
4391 static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early)
4392 {
4393         struct cgroup_subsys_state *css;
4394
4395         pr_debug("Initializing cgroup subsys %s\n", ss->name);
4396
4397         mutex_lock(&cgroup_mutex);
4398
4399         idr_init(&ss->css_idr);
4400         INIT_LIST_HEAD(&ss->cfts);
4401
4402         /* Create the root cgroup state for this subsystem */
4403         ss->root = &cgrp_dfl_root;
4404         css = ss->css_alloc(cgroup_css(&cgrp_dfl_root.cgrp, ss));
4405         /* We don't handle early failures gracefully */
4406         BUG_ON(IS_ERR(css));
4407         init_and_link_css(css, ss, &cgrp_dfl_root.cgrp);
4408
4409         /*
4410          * Root csses are never destroyed and we can't initialize
4411          * percpu_ref during early init.  Disable refcnting.
4412          */
4413         css->flags |= CSS_NO_REF;
4414
4415         if (early) {
4416                 /* allocation can't be done safely during early init */
4417                 css->id = 1;
4418         } else {
4419                 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2, GFP_KERNEL);
4420                 BUG_ON(css->id < 0);
4421         }
4422
4423         /* Update the init_css_set to contain a subsys
4424          * pointer to this state - since the subsystem is
4425          * newly registered, all tasks and hence the
4426          * init_css_set is in the subsystem's root cgroup. */
4427         init_css_set.subsys[ss->id] = css;
4428
4429         have_fork_callback |= (bool)ss->fork << ss->id;
4430         have_exit_callback |= (bool)ss->exit << ss->id;
4431         have_free_callback |= (bool)ss->free << ss->id;
4432         have_canfork_callback |= (bool)ss->can_fork << ss->id;
4433
4434         /* At system boot, before all subsystems have been
4435          * registered, no tasks have been forked, so we don't
4436          * need to invoke fork callbacks here. */
4437         BUG_ON(!list_empty(&init_task.tasks));
4438
4439         BUG_ON(online_css(css));
4440
4441         mutex_unlock(&cgroup_mutex);
4442 }
4443
4444 /**
4445  * cgroup_init_early - cgroup initialization at system boot
4446  *
4447  * Initialize cgroups at system boot, and initialize any
4448  * subsystems that request early init.
4449  */
4450 int __init cgroup_init_early(void)
4451 {
4452         static struct cgroup_sb_opts __initdata opts;
4453         struct cgroup_subsys *ss;
4454         int i;
4455
4456         init_cgroup_root(&cgrp_dfl_root, &opts);
4457         cgrp_dfl_root.cgrp.self.flags |= CSS_NO_REF;
4458
4459         RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
4460
4461         for_each_subsys(ss, i) {
4462                 WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id,
4463                      "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p id:name=%d:%s\n",
4464                      i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free,
4465                      ss->id, ss->name);
4466                 WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN,
4467                      "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]);
4468
4469                 ss->id = i;
4470                 ss->name = cgroup_subsys_name[i];
4471                 if (!ss->legacy_name)
4472                         ss->legacy_name = cgroup_subsys_name[i];
4473
4474                 if (ss->early_init)
4475                         cgroup_init_subsys(ss, true);
4476         }
4477         return 0;
4478 }
4479
4480 static u16 cgroup_disable_mask __initdata;
4481
4482 /**
4483  * cgroup_init - cgroup initialization
4484  *
4485  * Register cgroup filesystem and /proc file, and initialize
4486  * any subsystems that didn't request early init.
4487  */
4488 int __init cgroup_init(void)
4489 {
4490         struct cgroup_subsys *ss;
4491         int ssid;
4492
4493         BUILD_BUG_ON(CGROUP_SUBSYS_COUNT > 16);
4494         BUG_ON(percpu_init_rwsem(&cgroup_threadgroup_rwsem));
4495         BUG_ON(cgroup_init_cftypes(NULL, cgroup_base_files));
4496         BUG_ON(cgroup_init_cftypes(NULL, cgroup1_base_files));
4497
4498         /*
4499          * The latency of the synchronize_sched() is too high for cgroups,
4500          * avoid it at the cost of forcing all readers into the slow path.
4501          */
4502         rcu_sync_enter_start(&cgroup_threadgroup_rwsem.rss);
4503
4504         get_user_ns(init_cgroup_ns.user_ns);
4505
4506         mutex_lock(&cgroup_mutex);
4507
4508         /*
4509          * Add init_css_set to the hash table so that dfl_root can link to
4510          * it during init.
4511          */
4512         hash_add(css_set_table, &init_css_set.hlist,
4513                  css_set_hash(init_css_set.subsys));
4514
4515         BUG_ON(cgroup_setup_root(&cgrp_dfl_root, 0));
4516
4517         mutex_unlock(&cgroup_mutex);
4518
4519         for_each_subsys(ss, ssid) {
4520                 if (ss->early_init) {
4521                         struct cgroup_subsys_state *css =
4522                                 init_css_set.subsys[ss->id];
4523
4524                         css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2,
4525                                                    GFP_KERNEL);
4526                         BUG_ON(css->id < 0);
4527                 } else {
4528                         cgroup_init_subsys(ss, false);
4529                 }
4530
4531                 list_add_tail(&init_css_set.e_cset_node[ssid],
4532                               &cgrp_dfl_root.cgrp.e_csets[ssid]);
4533
4534                 /*
4535                  * Setting dfl_root subsys_mask needs to consider the
4536                  * disabled flag and cftype registration needs kmalloc,
4537                  * both of which aren't available during early_init.
4538                  */
4539                 if (cgroup_disable_mask & (1 << ssid)) {
4540                         static_branch_disable(cgroup_subsys_enabled_key[ssid]);
4541                         printk(KERN_INFO "Disabling %s control group subsystem\n",
4542                                ss->name);
4543                         continue;
4544                 }
4545
4546                 if (cgroup1_ssid_disabled(ssid))
4547                         printk(KERN_INFO "Disabling %s control group subsystem in v1 mounts\n",
4548                                ss->name);
4549
4550                 cgrp_dfl_root.subsys_mask |= 1 << ss->id;
4551
4552                 if (ss->implicit_on_dfl)
4553                         cgrp_dfl_implicit_ss_mask |= 1 << ss->id;
4554                 else if (!ss->dfl_cftypes)
4555                         cgrp_dfl_inhibit_ss_mask |= 1 << ss->id;
4556
4557                 if (ss->dfl_cftypes == ss->legacy_cftypes) {
4558                         WARN_ON(cgroup_add_cftypes(ss, ss->dfl_cftypes));
4559                 } else {
4560                         WARN_ON(cgroup_add_dfl_cftypes(ss, ss->dfl_cftypes));
4561                         WARN_ON(cgroup_add_legacy_cftypes(ss, ss->legacy_cftypes));
4562                 }
4563
4564                 if (ss->bind)
4565                         ss->bind(init_css_set.subsys[ssid]);
4566         }
4567
4568         /* init_css_set.subsys[] has been updated, re-hash */
4569         hash_del(&init_css_set.hlist);
4570         hash_add(css_set_table, &init_css_set.hlist,
4571                  css_set_hash(init_css_set.subsys));
4572
4573         WARN_ON(sysfs_create_mount_point(fs_kobj, "cgroup"));
4574         WARN_ON(register_filesystem(&cgroup_fs_type));
4575         WARN_ON(register_filesystem(&cgroup2_fs_type));
4576         WARN_ON(!proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations));
4577
4578         return 0;
4579 }
4580
4581 static int __init cgroup_wq_init(void)
4582 {
4583         /*
4584          * There isn't much point in executing destruction path in
4585          * parallel.  Good chunk is serialized with cgroup_mutex anyway.
4586          * Use 1 for @max_active.
4587          *
4588          * We would prefer to do this in cgroup_init() above, but that
4589          * is called before init_workqueues(): so leave this until after.
4590          */
4591         cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
4592         BUG_ON(!cgroup_destroy_wq);
4593         return 0;
4594 }
4595 core_initcall(cgroup_wq_init);
4596
4597 /*
4598  * proc_cgroup_show()
4599  *  - Print task's cgroup paths into seq_file, one line for each hierarchy
4600  *  - Used for /proc/<pid>/cgroup.
4601  */
4602 int proc_cgroup_show(struct seq_file *m, struct pid_namespace *ns,
4603                      struct pid *pid, struct task_struct *tsk)
4604 {
4605         char *buf;
4606         int retval;
4607         struct cgroup_root *root;
4608
4609         retval = -ENOMEM;
4610         buf = kmalloc(PATH_MAX, GFP_KERNEL);
4611         if (!buf)
4612                 goto out;
4613
4614         mutex_lock(&cgroup_mutex);
4615         spin_lock_irq(&css_set_lock);
4616
4617         for_each_root(root) {
4618                 struct cgroup_subsys *ss;
4619                 struct cgroup *cgrp;
4620                 int ssid, count = 0;
4621
4622                 if (root == &cgrp_dfl_root && !cgrp_dfl_visible)
4623                         continue;
4624
4625                 seq_printf(m, "%d:", root->hierarchy_id);
4626                 if (root != &cgrp_dfl_root)
4627                         for_each_subsys(ss, ssid)
4628                                 if (root->subsys_mask & (1 << ssid))
4629                                         seq_printf(m, "%s%s", count++ ? "," : "",
4630                                                    ss->legacy_name);
4631                 if (strlen(root->name))
4632                         seq_printf(m, "%sname=%s", count ? "," : "",
4633                                    root->name);
4634                 seq_putc(m, ':');
4635
4636                 cgrp = task_cgroup_from_root(tsk, root);
4637
4638                 /*
4639                  * On traditional hierarchies, all zombie tasks show up as
4640                  * belonging to the root cgroup.  On the default hierarchy,
4641                  * while a zombie doesn't show up in "cgroup.procs" and
4642                  * thus can't be migrated, its /proc/PID/cgroup keeps
4643                  * reporting the cgroup it belonged to before exiting.  If
4644                  * the cgroup is removed before the zombie is reaped,
4645                  * " (deleted)" is appended to the cgroup path.
4646                  */
4647                 if (cgroup_on_dfl(cgrp) || !(tsk->flags & PF_EXITING)) {
4648                         retval = cgroup_path_ns_locked(cgrp, buf, PATH_MAX,
4649                                                 current->nsproxy->cgroup_ns);
4650                         if (retval >= PATH_MAX)
4651                                 retval = -ENAMETOOLONG;
4652                         if (retval < 0)
4653                                 goto out_unlock;
4654
4655                         seq_puts(m, buf);
4656                 } else {
4657                         seq_puts(m, "/");
4658                 }
4659
4660                 if (cgroup_on_dfl(cgrp) && cgroup_is_dead(cgrp))
4661                         seq_puts(m, " (deleted)\n");
4662                 else
4663                         seq_putc(m, '\n');
4664         }
4665
4666         retval = 0;
4667 out_unlock:
4668         spin_unlock_irq(&css_set_lock);
4669         mutex_unlock(&cgroup_mutex);
4670         kfree(buf);
4671 out:
4672         return retval;
4673 }
4674
4675 /**
4676  * cgroup_fork - initialize cgroup related fields during copy_process()
4677  * @child: pointer to task_struct of forking parent process.
4678  *
4679  * A task is associated with the init_css_set until cgroup_post_fork()
4680  * attaches it to the parent's css_set.  Empty cg_list indicates that
4681  * @child isn't holding reference to its css_set.
4682  */
4683 void cgroup_fork(struct task_struct *child)
4684 {
4685         RCU_INIT_POINTER(child->cgroups, &init_css_set);
4686         INIT_LIST_HEAD(&child->cg_list);
4687 }
4688
4689 /**
4690  * cgroup_can_fork - called on a new task before the process is exposed
4691  * @child: the task in question.
4692  *
4693  * This calls the subsystem can_fork() callbacks. If the can_fork() callback
4694  * returns an error, the fork aborts with that error code. This allows for
4695  * a cgroup subsystem to conditionally allow or deny new forks.
4696  */
4697 int cgroup_can_fork(struct task_struct *child)
4698 {
4699         struct cgroup_subsys *ss;
4700         int i, j, ret;
4701
4702         do_each_subsys_mask(ss, i, have_canfork_callback) {
4703                 ret = ss->can_fork(child);
4704                 if (ret)
4705                         goto out_revert;
4706         } while_each_subsys_mask();
4707
4708         return 0;
4709
4710 out_revert:
4711         for_each_subsys(ss, j) {
4712                 if (j >= i)
4713                         break;
4714                 if (ss->cancel_fork)
4715                         ss->cancel_fork(child);
4716         }
4717
4718         return ret;
4719 }
4720
4721 /**
4722  * cgroup_cancel_fork - called if a fork failed after cgroup_can_fork()
4723  * @child: the task in question
4724  *
4725  * This calls the cancel_fork() callbacks if a fork failed *after*
4726  * cgroup_can_fork() succeded.
4727  */
4728 void cgroup_cancel_fork(struct task_struct *child)
4729 {
4730         struct cgroup_subsys *ss;
4731         int i;
4732
4733         for_each_subsys(ss, i)
4734                 if (ss->cancel_fork)
4735                         ss->cancel_fork(child);
4736 }
4737
4738 /**
4739  * cgroup_post_fork - called on a new task after adding it to the task list
4740  * @child: the task in question
4741  *
4742  * Adds the task to the list running through its css_set if necessary and
4743  * call the subsystem fork() callbacks.  Has to be after the task is
4744  * visible on the task list in case we race with the first call to
4745  * cgroup_task_iter_start() - to guarantee that the new task ends up on its
4746  * list.
4747  */
4748 void cgroup_post_fork(struct task_struct *child)
4749 {
4750         struct cgroup_subsys *ss;
4751         int i;
4752
4753         /*
4754          * This may race against cgroup_enable_task_cg_lists().  As that
4755          * function sets use_task_css_set_links before grabbing
4756          * tasklist_lock and we just went through tasklist_lock to add
4757          * @child, it's guaranteed that either we see the set
4758          * use_task_css_set_links or cgroup_enable_task_cg_lists() sees
4759          * @child during its iteration.
4760          *
4761          * If we won the race, @child is associated with %current's
4762          * css_set.  Grabbing css_set_lock guarantees both that the
4763          * association is stable, and, on completion of the parent's
4764          * migration, @child is visible in the source of migration or
4765          * already in the destination cgroup.  This guarantee is necessary
4766          * when implementing operations which need to migrate all tasks of
4767          * a cgroup to another.
4768          *
4769          * Note that if we lose to cgroup_enable_task_cg_lists(), @child
4770          * will remain in init_css_set.  This is safe because all tasks are
4771          * in the init_css_set before cg_links is enabled and there's no
4772          * operation which transfers all tasks out of init_css_set.
4773          */
4774         if (use_task_css_set_links) {
4775                 struct css_set *cset;
4776
4777                 spin_lock_irq(&css_set_lock);
4778                 cset = task_css_set(current);
4779                 if (list_empty(&child->cg_list)) {
4780                         get_css_set(cset);
4781                         css_set_move_task(child, NULL, cset, false);
4782                 }
4783                 spin_unlock_irq(&css_set_lock);
4784         }
4785
4786         /*
4787          * Call ss->fork().  This must happen after @child is linked on
4788          * css_set; otherwise, @child might change state between ->fork()
4789          * and addition to css_set.
4790          */
4791         do_each_subsys_mask(ss, i, have_fork_callback) {
4792                 ss->fork(child);
4793         } while_each_subsys_mask();
4794 }
4795
4796 /**
4797  * cgroup_exit - detach cgroup from exiting task
4798  * @tsk: pointer to task_struct of exiting process
4799  *
4800  * Description: Detach cgroup from @tsk and release it.
4801  *
4802  * Note that cgroups marked notify_on_release force every task in
4803  * them to take the global cgroup_mutex mutex when exiting.
4804  * This could impact scaling on very large systems.  Be reluctant to
4805  * use notify_on_release cgroups where very high task exit scaling
4806  * is required on large systems.
4807  *
4808  * We set the exiting tasks cgroup to the root cgroup (top_cgroup).  We
4809  * call cgroup_exit() while the task is still competent to handle
4810  * notify_on_release(), then leave the task attached to the root cgroup in
4811  * each hierarchy for the remainder of its exit.  No need to bother with
4812  * init_css_set refcnting.  init_css_set never goes away and we can't race
4813  * with migration path - PF_EXITING is visible to migration path.
4814  */
4815 void cgroup_exit(struct task_struct *tsk)
4816 {
4817         struct cgroup_subsys *ss;
4818         struct css_set *cset;
4819         int i;
4820
4821         /*
4822          * Unlink from @tsk from its css_set.  As migration path can't race
4823          * with us, we can check css_set and cg_list without synchronization.
4824          */
4825         cset = task_css_set(tsk);
4826
4827         if (!list_empty(&tsk->cg_list)) {
4828                 spin_lock_irq(&css_set_lock);
4829                 css_set_move_task(tsk, cset, NULL, false);
4830                 spin_unlock_irq(&css_set_lock);
4831         } else {
4832                 get_css_set(cset);
4833         }
4834
4835         /* see cgroup_post_fork() for details */
4836         do_each_subsys_mask(ss, i, have_exit_callback) {
4837                 ss->exit(tsk);
4838         } while_each_subsys_mask();
4839 }
4840
4841 void cgroup_free(struct task_struct *task)
4842 {
4843         struct css_set *cset = task_css_set(task);
4844         struct cgroup_subsys *ss;
4845         int ssid;
4846
4847         do_each_subsys_mask(ss, ssid, have_free_callback) {
4848                 ss->free(task);
4849         } while_each_subsys_mask();
4850
4851         put_css_set(cset);
4852 }
4853
4854 static int __init cgroup_disable(char *str)
4855 {
4856         struct cgroup_subsys *ss;
4857         char *token;
4858         int i;
4859
4860         while ((token = strsep(&str, ",")) != NULL) {
4861                 if (!*token)
4862                         continue;
4863
4864                 for_each_subsys(ss, i) {
4865                         if (strcmp(token, ss->name) &&
4866                             strcmp(token, ss->legacy_name))
4867                                 continue;
4868                         cgroup_disable_mask |= 1 << i;
4869                 }
4870         }
4871         return 1;
4872 }
4873 __setup("cgroup_disable=", cgroup_disable);
4874
4875 /**
4876  * css_tryget_online_from_dir - get corresponding css from a cgroup dentry
4877  * @dentry: directory dentry of interest
4878  * @ss: subsystem of interest
4879  *
4880  * If @dentry is a directory for a cgroup which has @ss enabled on it, try
4881  * to get the corresponding css and return it.  If such css doesn't exist
4882  * or can't be pinned, an ERR_PTR value is returned.
4883  */
4884 struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry,
4885                                                        struct cgroup_subsys *ss)
4886 {
4887         struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
4888         struct file_system_type *s_type = dentry->d_sb->s_type;
4889         struct cgroup_subsys_state *css = NULL;
4890         struct cgroup *cgrp;
4891
4892         /* is @dentry a cgroup dir? */
4893         if ((s_type != &cgroup_fs_type && s_type != &cgroup2_fs_type) ||
4894             !kn || kernfs_type(kn) != KERNFS_DIR)
4895                 return ERR_PTR(-EBADF);
4896
4897         rcu_read_lock();
4898
4899         /*
4900          * This path doesn't originate from kernfs and @kn could already
4901          * have been or be removed at any point.  @kn->priv is RCU
4902          * protected for this access.  See css_release_work_fn() for details.
4903          */
4904         cgrp = rcu_dereference(*(void __rcu __force **)&kn->priv);
4905         if (cgrp)
4906                 css = cgroup_css(cgrp, ss);
4907
4908         if (!css || !css_tryget_online(css))
4909                 css = ERR_PTR(-ENOENT);
4910
4911         rcu_read_unlock();
4912         return css;
4913 }
4914
4915 /**
4916  * css_from_id - lookup css by id
4917  * @id: the cgroup id
4918  * @ss: cgroup subsys to be looked into
4919  *
4920  * Returns the css if there's valid one with @id, otherwise returns NULL.
4921  * Should be called under rcu_read_lock().
4922  */
4923 struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
4924 {
4925         WARN_ON_ONCE(!rcu_read_lock_held());
4926         return idr_find(&ss->css_idr, id);
4927 }
4928
4929 /**
4930  * cgroup_get_from_path - lookup and get a cgroup from its default hierarchy path
4931  * @path: path on the default hierarchy
4932  *
4933  * Find the cgroup at @path on the default hierarchy, increment its
4934  * reference count and return it.  Returns pointer to the found cgroup on
4935  * success, ERR_PTR(-ENOENT) if @path doens't exist and ERR_PTR(-ENOTDIR)
4936  * if @path points to a non-directory.
4937  */
4938 struct cgroup *cgroup_get_from_path(const char *path)
4939 {
4940         struct kernfs_node *kn;
4941         struct cgroup *cgrp;
4942
4943         mutex_lock(&cgroup_mutex);
4944
4945         kn = kernfs_walk_and_get(cgrp_dfl_root.cgrp.kn, path);
4946         if (kn) {
4947                 if (kernfs_type(kn) == KERNFS_DIR) {
4948                         cgrp = kn->priv;
4949                         cgroup_get(cgrp);
4950                 } else {
4951                         cgrp = ERR_PTR(-ENOTDIR);
4952                 }
4953                 kernfs_put(kn);
4954         } else {
4955                 cgrp = ERR_PTR(-ENOENT);
4956         }
4957
4958         mutex_unlock(&cgroup_mutex);
4959         return cgrp;
4960 }
4961 EXPORT_SYMBOL_GPL(cgroup_get_from_path);
4962
4963 /**
4964  * cgroup_get_from_fd - get a cgroup pointer from a fd
4965  * @fd: fd obtained by open(cgroup2_dir)
4966  *
4967  * Find the cgroup from a fd which should be obtained
4968  * by opening a cgroup directory.  Returns a pointer to the
4969  * cgroup on success. ERR_PTR is returned if the cgroup
4970  * cannot be found.
4971  */
4972 struct cgroup *cgroup_get_from_fd(int fd)
4973 {
4974         struct cgroup_subsys_state *css;
4975         struct cgroup *cgrp;
4976         struct file *f;
4977
4978         f = fget_raw(fd);
4979         if (!f)
4980                 return ERR_PTR(-EBADF);
4981
4982         css = css_tryget_online_from_dir(f->f_path.dentry, NULL);
4983         fput(f);
4984         if (IS_ERR(css))
4985                 return ERR_CAST(css);
4986
4987         cgrp = css->cgroup;
4988         if (!cgroup_on_dfl(cgrp)) {
4989                 cgroup_put(cgrp);
4990                 return ERR_PTR(-EBADF);
4991         }
4992
4993         return cgrp;
4994 }
4995 EXPORT_SYMBOL_GPL(cgroup_get_from_fd);
4996
4997 /*
4998  * sock->sk_cgrp_data handling.  For more info, see sock_cgroup_data
4999  * definition in cgroup-defs.h.
5000  */
5001 #ifdef CONFIG_SOCK_CGROUP_DATA
5002
5003 #if defined(CONFIG_CGROUP_NET_PRIO) || defined(CONFIG_CGROUP_NET_CLASSID)
5004
5005 DEFINE_SPINLOCK(cgroup_sk_update_lock);
5006 static bool cgroup_sk_alloc_disabled __read_mostly;
5007
5008 void cgroup_sk_alloc_disable(void)
5009 {
5010         if (cgroup_sk_alloc_disabled)
5011                 return;
5012         pr_info("cgroup: disabling cgroup2 socket matching due to net_prio or net_cls activation\n");
5013         cgroup_sk_alloc_disabled = true;
5014 }
5015
5016 #else
5017
5018 #define cgroup_sk_alloc_disabled        false
5019
5020 #endif
5021
5022 void cgroup_sk_alloc(struct sock_cgroup_data *skcd)
5023 {
5024         if (cgroup_sk_alloc_disabled)
5025                 return;
5026
5027         /* Socket clone path */
5028         if (skcd->val) {
5029                 cgroup_get(sock_cgroup_ptr(skcd));
5030                 return;
5031         }
5032
5033         rcu_read_lock();
5034
5035         while (true) {
5036                 struct css_set *cset;
5037
5038                 cset = task_css_set(current);
5039                 if (likely(cgroup_tryget(cset->dfl_cgrp))) {
5040                         skcd->val = (unsigned long)cset->dfl_cgrp;
5041                         break;
5042                 }
5043                 cpu_relax();
5044         }
5045
5046         rcu_read_unlock();
5047 }
5048
5049 void cgroup_sk_free(struct sock_cgroup_data *skcd)
5050 {
5051         cgroup_put(sock_cgroup_ptr(skcd));
5052 }
5053
5054 #endif  /* CONFIG_SOCK_CGROUP_DATA */
5055
5056 #ifdef CONFIG_CGROUP_BPF
5057 int cgroup_bpf_update(struct cgroup *cgrp, struct bpf_prog *prog,
5058                       enum bpf_attach_type type, bool overridable)
5059 {
5060         struct cgroup *parent = cgroup_parent(cgrp);
5061         int ret;
5062
5063         mutex_lock(&cgroup_mutex);
5064         ret = __cgroup_bpf_update(cgrp, parent, prog, type, overridable);
5065         mutex_unlock(&cgroup_mutex);
5066         return ret;
5067 }
5068 #endif /* CONFIG_CGROUP_BPF */