blob: ebe7ce49f4b73924b716d0b6326ebc1b295a6917 [file] [log] [blame]
Paul Menageddbcc7e2007-10-18 23:39:30 -07001#ifndef _LINUX_CGROUP_H
2#define _LINUX_CGROUP_H
3/*
4 * cgroup interface
5 *
6 * Copyright (C) 2003 BULL SA
7 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
8 *
9 */
10
11#include <linux/sched.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070012#include <linux/cpumask.h>
13#include <linux/nodemask.h>
14#include <linux/rcupdate.h>
Tejun Heoeb6fd502012-11-09 09:12:29 -080015#include <linux/rculist.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070016#include <linux/cgroupstats.h>
Paul Menagecc31edc2008-10-18 20:28:04 -070017#include <linux/rwsem.h>
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -070018#include <linux/idr.h>
Tejun Heo48ddbe12012-04-01 12:09:56 -070019#include <linux/workqueue.h>
Tejun Heo25a7e682013-04-14 20:15:25 -070020#include <linux/fs.h>
Tejun Heod3daf282013-06-13 19:39:16 -070021#include <linux/percpu-refcount.h>
Tejun Heo7da11272013-12-05 12:28:04 -050022#include <linux/seq_file.h>
Tejun Heo2bd59d42014-02-11 11:52:49 -050023#include <linux/kernfs.h>
Tejun Heof8f22e52014-04-23 11:13:16 -040024#include <linux/wait.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070025
26#ifdef CONFIG_CGROUPS
27
Tejun Heo3dd06ff2014-03-19 10:23:54 -040028struct cgroup_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -070029struct cgroup_subsys;
30struct inode;
Paul Menage84eea842008-07-25 01:47:00 -070031struct cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -070032
33extern int cgroup_init_early(void);
34extern int cgroup_init(void);
Paul Menageb4f48b62007-10-18 23:39:33 -070035extern void cgroup_fork(struct task_struct *p);
Paul Menage817929e2007-10-18 23:39:36 -070036extern void cgroup_post_fork(struct task_struct *p);
Li Zefan1ec41832014-03-28 15:22:19 +080037extern void cgroup_exit(struct task_struct *p);
Balbir Singh846c7bb2007-10-18 23:39:44 -070038extern int cgroupstats_build(struct cgroupstats *stats,
39 struct dentry *dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -070040
Al Viro8d8b97b2013-04-19 23:11:24 -040041extern int proc_cgroup_show(struct seq_file *, void *);
Paul Menagea4243162007-10-18 23:39:35 -070042
Tejun Heo3ed80a62014-02-08 10:36:58 -050043/* define the enumeration of all cgroup subsystems */
Tejun Heo073219e2014-02-08 10:36:58 -050044#define SUBSYS(_x) _x ## _cgrp_id,
Paul Menage817929e2007-10-18 23:39:36 -070045enum cgroup_subsys_id {
46#include <linux/cgroup_subsys.h>
Daniel Wagnera6f00292012-09-12 16:12:08 +020047 CGROUP_SUBSYS_COUNT,
Paul Menage817929e2007-10-18 23:39:36 -070048};
49#undef SUBSYS
50
Tejun Heo5c9d5352014-05-16 13:22:48 -040051/*
52 * Per-subsystem/per-cgroup state maintained by the system. This is the
53 * fundamental structural building block that controllers deal with.
54 *
55 * Fields marked with "PI:" are public and immutable and may be accessed
56 * directly without synchronization.
57 */
Paul Menageddbcc7e2007-10-18 23:39:30 -070058struct cgroup_subsys_state {
Tejun Heo5c9d5352014-05-16 13:22:48 -040059 /* PI: the cgroup that this css is attached to */
Paul Menageddbcc7e2007-10-18 23:39:30 -070060 struct cgroup *cgroup;
61
Tejun Heo5c9d5352014-05-16 13:22:48 -040062 /* PI: the cgroup subsystem that this css is attached to */
Tejun Heo72c97e52013-08-08 20:11:22 -040063 struct cgroup_subsys *ss;
64
Tejun Heod3daf282013-06-13 19:39:16 -070065 /* reference count - access via css_[try]get() and css_put() */
66 struct percpu_ref refcnt;
Paul Menageddbcc7e2007-10-18 23:39:30 -070067
Tejun Heo5c9d5352014-05-16 13:22:48 -040068 /* PI: the parent css */
Tejun Heo0ae78e02013-08-13 11:01:54 -040069 struct cgroup_subsys_state *parent;
70
Tejun Heod5c419b2014-05-16 13:22:48 -040071 /* siblings list anchored at the parent's ->children */
72 struct list_head sibling;
73 struct list_head children;
74
Tejun Heo15a4c832014-05-04 15:09:14 -040075 /*
Tejun Heo5c9d5352014-05-16 13:22:48 -040076 * PI: Subsys-unique ID. 0 is unused and root is always 1. The
Tejun Heo15a4c832014-05-04 15:09:14 -040077 * matching css can be looked up using css_from_id().
78 */
79 int id;
80
Tejun Heo69dfa002014-05-04 15:09:13 -040081 unsigned int flags;
Tejun Heo48ddbe12012-04-01 12:09:56 -070082
Tejun Heo0cb51d72014-05-16 13:22:49 -040083 /*
84 * Monotonically increasing unique serial number which defines a
85 * uniform order among all csses. It's guaranteed that all
86 * ->children lists are in the ascending order of ->serial_nr and
87 * used to allow interrupting and resuming iterations.
88 */
89 u64 serial_nr;
90
Tejun Heo0c21ead2013-08-13 20:22:51 -040091 /* percpu_ref killing and RCU release */
92 struct rcu_head rcu_head;
Tejun Heo35ef10d2013-08-13 11:01:54 -040093 struct work_struct destroy_work;
Paul Menageddbcc7e2007-10-18 23:39:30 -070094};
95
96/* bits in struct cgroup_subsys_state flags field */
97enum {
Tejun Heo3b514d22014-05-16 13:22:47 -040098 CSS_NO_REF = (1 << 0), /* no reference counting for this css */
Tejun Heo92fb9742012-11-19 08:13:38 -080099 CSS_ONLINE = (1 << 1), /* between ->css_online() and ->css_offline() */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700100};
101
Tejun Heo5de01072013-06-12 21:04:52 -0700102/**
103 * css_get - obtain a reference on the specified css
104 * @css: target css
105 *
106 * The caller must already have a reference.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700107 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700108static inline void css_get(struct cgroup_subsys_state *css)
109{
Tejun Heo3b514d22014-05-16 13:22:47 -0400110 if (!(css->flags & CSS_NO_REF))
111 percpu_ref_get(&css->refcnt);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700112}
Paul Menagee7c5ec92009-01-07 18:08:38 -0800113
Tejun Heo5de01072013-06-12 21:04:52 -0700114/**
Tejun Heoec903c02014-05-13 12:11:01 -0400115 * css_tryget_online - try to obtain a reference on the specified css if online
Tejun Heo5de01072013-06-12 21:04:52 -0700116 * @css: target css
117 *
Tejun Heoec903c02014-05-13 12:11:01 -0400118 * Obtain a reference on @css if it's online. The caller naturally needs
119 * to ensure that @css is accessible but doesn't have to be holding a
Tejun Heo5de01072013-06-12 21:04:52 -0700120 * reference on it - IOW, RCU protected access is good enough for this
121 * function. Returns %true if a reference count was successfully obtained;
122 * %false otherwise.
123 */
Tejun Heoec903c02014-05-13 12:11:01 -0400124static inline bool css_tryget_online(struct cgroup_subsys_state *css)
Paul Menagee7c5ec92009-01-07 18:08:38 -0800125{
Tejun Heo3b514d22014-05-16 13:22:47 -0400126 if (!(css->flags & CSS_NO_REF))
127 return percpu_ref_tryget_live(&css->refcnt);
128 return true;
Paul Menagee7c5ec92009-01-07 18:08:38 -0800129}
130
Tejun Heo5de01072013-06-12 21:04:52 -0700131/**
132 * css_put - put a css reference
133 * @css: target css
134 *
Tejun Heoec903c02014-05-13 12:11:01 -0400135 * Put a reference obtained via css_get() and css_tryget_online().
Tejun Heo5de01072013-06-12 21:04:52 -0700136 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700137static inline void css_put(struct cgroup_subsys_state *css)
138{
Tejun Heo3b514d22014-05-16 13:22:47 -0400139 if (!(css->flags & CSS_NO_REF))
140 percpu_ref_put(&css->refcnt);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700141}
142
Paul Menage3116f0e2008-04-29 01:00:04 -0700143/* bits in struct cgroup flags field */
144enum {
145 /* Control Group is dead */
Tejun Heo54766d42013-06-12 21:04:53 -0700146 CGRP_DEAD,
Paul Menaged20a3902009-04-02 16:57:22 -0700147 /*
148 * Control Group has previously had a child cgroup or a task,
149 * but no longer (only if CGRP_NOTIFY_ON_RELEASE is set)
150 */
Paul Menage3116f0e2008-04-29 01:00:04 -0700151 CGRP_RELEASABLE,
152 /* Control Group requires release notifications to userspace */
153 CGRP_NOTIFY_ON_RELEASE,
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700154 /*
Tejun Heo2260e7f2012-11-19 08:13:38 -0800155 * Clone the parent's configuration when creating a new child
156 * cpuset cgroup. For historical reasons, this option can be
157 * specified at mount time and thus is implemented here.
Daniel Lezcano97978e62010-10-27 15:33:35 -0700158 */
Tejun Heo2260e7f2012-11-19 08:13:38 -0800159 CGRP_CPUSET_CLONE_CHILDREN,
Paul Menage3116f0e2008-04-29 01:00:04 -0700160};
161
Paul Menageddbcc7e2007-10-18 23:39:30 -0700162struct cgroup {
Tejun Heo9d800df2014-05-14 09:15:00 -0400163 /* self css with NULL ->ss, points back to this cgroup */
164 struct cgroup_subsys_state self;
165
Paul Menageddbcc7e2007-10-18 23:39:30 -0700166 unsigned long flags; /* "unsigned long" so bitops work */
167
Li Zefanb414dc02013-07-31 09:51:06 +0800168 /*
169 * idr allocated in-hierarchy ID.
170 *
Tejun Heo7d699dd2014-05-04 15:09:13 -0400171 * ID 0 is not used, the ID of the root cgroup is always 1, and a
172 * new cgroup will be assigned with a smallest available ID.
Li Zefan0ab02ca2014-02-11 16:05:46 +0800173 *
174 * Allocating/Removing ID must be protected by cgroup_mutex.
Li Zefanb414dc02013-07-31 09:51:06 +0800175 */
176 int id;
Tejun Heo0a950f62012-11-19 09:02:12 -0800177
Tejun Heo842b5972014-04-25 18:28:02 -0400178 /*
179 * If this cgroup contains any tasks, it contributes one to
180 * populated_cnt. All children with non-zero popuplated_cnt of
181 * their own contribute one. The count is zero iff there's no task
182 * in this cgroup or its subtree.
183 */
184 int populated_cnt;
185
Tejun Heo2bd59d42014-02-11 11:52:49 -0500186 struct kernfs_node *kn; /* cgroup kernfs entry */
Tejun Heo842b5972014-04-25 18:28:02 -0400187 struct kernfs_node *populated_kn; /* kn for "cgroup.subtree_populated" */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700188
Tejun Heof392e512014-04-23 11:13:14 -0400189 /* the bitmask of subsystems enabled on the child cgroups */
Tejun Heo69dfa002014-05-04 15:09:13 -0400190 unsigned int child_subsys_mask;
Tejun Heo94419622014-03-19 10:23:54 -0400191
Paul Menageddbcc7e2007-10-18 23:39:30 -0700192 /* Private pointers for each registered subsystem */
Tejun Heo73e80ed2013-08-13 11:01:55 -0400193 struct cgroup_subsys_state __rcu *subsys[CGROUP_SUBSYS_COUNT];
Paul Menageddbcc7e2007-10-18 23:39:30 -0700194
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400195 struct cgroup_root *root;
Paul Menage817929e2007-10-18 23:39:36 -0700196
197 /*
Tejun Heo69d02062013-06-12 21:04:50 -0700198 * List of cgrp_cset_links pointing at css_sets with tasks in this
199 * cgroup. Protected by css_set_lock.
Paul Menage817929e2007-10-18 23:39:36 -0700200 */
Tejun Heo69d02062013-06-12 21:04:50 -0700201 struct list_head cset_links;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700202
203 /*
Tejun Heo2d8f2432014-04-23 11:13:15 -0400204 * On the default hierarchy, a css_set for a cgroup with some
205 * susbsys disabled will point to css's which are associated with
206 * the closest ancestor which has the subsys enabled. The
207 * following lists all css_sets which point to this cgroup's css
208 * for the given subsystem.
209 */
210 struct list_head e_csets[CGROUP_SUBSYS_COUNT];
211
212 /*
Paul Menage81a6a5c2007-10-18 23:39:38 -0700213 * Linked list running through all cgroups that can
214 * potentially be reaped by the release agent. Protected by
215 * release_list_lock
216 */
217 struct list_head release_list;
Paul Menagecc31edc2008-10-18 20:28:04 -0700218
Ben Blum72a8cb32009-09-23 15:56:27 -0700219 /*
220 * list of pidlists, up to two for each namespace (one for procs, one
221 * for tasks); created on demand.
222 */
223 struct list_head pidlists;
224 struct mutex pidlist_mutex;
Paul Menagea47295e2009-01-07 18:07:44 -0800225
Tejun Heof8f22e52014-04-23 11:13:16 -0400226 /* used to wait for offlining of csses */
227 wait_queue_head_t offline_waitq;
Paul Menage817929e2007-10-18 23:39:36 -0700228};
229
Tejun Heo25a7e682013-04-14 20:15:25 -0700230#define MAX_CGROUP_ROOT_NAMELEN 64
231
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400232/* cgroup_root->flags */
Tejun Heo25a7e682013-04-14 20:15:25 -0700233enum {
Tejun Heo873fe092013-04-14 20:15:26 -0700234 /*
235 * Unfortunately, cgroup core and various controllers are riddled
236 * with idiosyncrasies and pointless options. The following flag,
237 * when set, will force sane behavior - some options are forced on,
238 * others are disallowed, and some controllers will change their
239 * hierarchical or other behaviors.
240 *
241 * The set of behaviors affected by this flag are still being
242 * determined and developed and the mount option for this flag is
243 * prefixed with __DEVEL__. The prefix will be dropped once we
244 * reach the point where all behaviors are compatible with the
245 * planned unified hierarchy, which will automatically turn on this
246 * flag.
247 *
248 * The followings are the behaviors currently affected this flag.
249 *
Tejun Heod3ba07c2014-02-13 06:58:38 -0500250 * - Mount options "noprefix", "xattr", "clone_children",
251 * "release_agent" and "name" are disallowed.
Tejun Heo873fe092013-04-14 20:15:26 -0700252 *
253 * - When mounting an existing superblock, mount options should
254 * match.
255 *
256 * - Remount is disallowed.
257 *
Linus Torvalds0b0585c2013-07-02 20:04:25 -0700258 * - rename(2) is disallowed.
259 *
Tejun Heof63674f2013-06-13 19:58:38 -0700260 * - "tasks" is removed. Everything should be at process
261 * granularity. Use "cgroup.procs" instead.
262 *
Tejun Heoafb2bc12013-11-29 10:42:59 -0500263 * - "cgroup.procs" is not sorted. pids will be unique unless they
264 * got recycled inbetween reads.
265 *
Tejun Heof63674f2013-06-13 19:58:38 -0700266 * - "release_agent" and "notify_on_release" are removed.
267 * Replacement notification mechanism will be implemented.
268 *
Tejun Heod3ba07c2014-02-13 06:58:38 -0500269 * - "cgroup.clone_children" is removed.
Tejun Heo86bf4b62014-02-12 09:29:48 -0500270 *
Tejun Heo842b5972014-04-25 18:28:02 -0400271 * - "cgroup.subtree_populated" is available. Its value is 0 if
272 * the cgroup and its descendants contain no task; otherwise, 1.
273 * The file also generates kernfs notification which can be
274 * monitored through poll and [di]notify when the value of the
275 * file changes.
276 *
Tejun Heoa2dd4242014-03-19 10:23:55 -0400277 * - If mount is requested with sane_behavior but without any
278 * subsystem, the default unified hierarchy is mounted.
279 *
Li Zefan5c5cc622013-06-09 17:16:29 +0800280 * - cpuset: tasks will be kept in empty cpusets when hotplug happens
281 * and take masks of ancestors with non-empty cpus/mems, instead of
282 * being moved to an ancestor.
283 *
Li Zefan88fa5232013-06-09 17:16:46 +0800284 * - cpuset: a task can be moved into an empty cpuset, and again it
285 * takes masks of ancestors.
Tejun Heo6db8e852013-06-14 11:18:22 -0700286 *
Tejun Heof00baae2013-04-15 13:41:15 -0700287 * - memcg: use_hierarchy is on by default and the cgroup file for
288 * the flag is not created.
289 *
Tejun Heo91381252013-05-14 13:52:38 -0700290 * - blkcg: blk-throttle becomes properly hierarchical.
Tejun Heo873fe092013-04-14 20:15:26 -0700291 */
292 CGRP_ROOT_SANE_BEHAVIOR = (1 << 0),
293
Tejun Heo25a7e682013-04-14 20:15:25 -0700294 CGRP_ROOT_NOPREFIX = (1 << 1), /* mounted subsystems have no named prefix */
295 CGRP_ROOT_XATTR = (1 << 2), /* supports extended attributes */
Tejun Heo0ce6cba2013-06-27 19:37:26 -0700296
297 /* mount options live below bit 16 */
298 CGRP_ROOT_OPTION_MASK = (1 << 16) - 1,
Tejun Heo25a7e682013-04-14 20:15:25 -0700299};
300
301/*
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400302 * A cgroup_root represents the root of a cgroup hierarchy, and may be
Tejun Heo2bd59d42014-02-11 11:52:49 -0500303 * associated with a kernfs_root to form an active hierarchy. This is
Tejun Heo25a7e682013-04-14 20:15:25 -0700304 * internal to cgroup core. Don't access directly from controllers.
305 */
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400306struct cgroup_root {
Tejun Heo2bd59d42014-02-11 11:52:49 -0500307 struct kernfs_root *kf_root;
Tejun Heo25a7e682013-04-14 20:15:25 -0700308
Tejun Heof392e512014-04-23 11:13:14 -0400309 /* The bitmask of subsystems attached to this hierarchy */
Tejun Heo69dfa002014-05-04 15:09:13 -0400310 unsigned int subsys_mask;
Tejun Heof392e512014-04-23 11:13:14 -0400311
Tejun Heo25a7e682013-04-14 20:15:25 -0700312 /* Unique id for this hierarchy. */
313 int hierarchy_id;
314
Tejun Heo776f02f2014-02-12 09:29:50 -0500315 /* The root cgroup. Root is destroyed on its release. */
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400316 struct cgroup cgrp;
Tejun Heo25a7e682013-04-14 20:15:25 -0700317
Tejun Heo3c9c8252014-02-12 09:29:50 -0500318 /* Number of cgroups in the hierarchy, used only for /proc/cgroups */
319 atomic_t nr_cgrps;
Tejun Heo25a7e682013-04-14 20:15:25 -0700320
321 /* A list running through the active hierarchies */
322 struct list_head root_list;
323
Tejun Heo25a7e682013-04-14 20:15:25 -0700324 /* Hierarchy-specific flags */
Tejun Heo69dfa002014-05-04 15:09:13 -0400325 unsigned int flags;
Tejun Heo25a7e682013-04-14 20:15:25 -0700326
327 /* IDs for cgroups in this hierarchy */
Li Zefan4e96ee8e2013-07-31 09:50:50 +0800328 struct idr cgroup_idr;
Tejun Heo25a7e682013-04-14 20:15:25 -0700329
330 /* The path to use for release notifications. */
331 char release_agent_path[PATH_MAX];
332
333 /* The name for this hierarchy - may be empty */
334 char name[MAX_CGROUP_ROOT_NAMELEN];
335};
336
Paul Menaged20a3902009-04-02 16:57:22 -0700337/*
338 * A css_set is a structure holding pointers to a set of
Paul Menage817929e2007-10-18 23:39:36 -0700339 * cgroup_subsys_state objects. This saves space in the task struct
340 * object and speeds up fork()/exit(), since a single inc/dec and a
Paul Menaged20a3902009-04-02 16:57:22 -0700341 * list_add()/del() can bump the reference count on the entire cgroup
342 * set for a task.
Paul Menage817929e2007-10-18 23:39:36 -0700343 */
344
345struct css_set {
346
347 /* Reference count */
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700348 atomic_t refcount;
Paul Menage817929e2007-10-18 23:39:36 -0700349
350 /*
Li Zefan472b1052008-04-29 01:00:11 -0700351 * List running through all cgroup groups in the same hash
352 * slot. Protected by css_set_lock
353 */
354 struct hlist_node hlist;
355
356 /*
Tejun Heoc7561122014-02-25 10:04:01 -0500357 * Lists running through all tasks using this cgroup group.
358 * mg_tasks lists tasks which belong to this cset but are in the
359 * process of being migrated out or in. Protected by
360 * css_set_rwsem, but, during migration, once tasks are moved to
361 * mg_tasks, it can be read safely while holding cgroup_mutex.
Paul Menage817929e2007-10-18 23:39:36 -0700362 */
363 struct list_head tasks;
Tejun Heoc7561122014-02-25 10:04:01 -0500364 struct list_head mg_tasks;
Paul Menage817929e2007-10-18 23:39:36 -0700365
366 /*
Tejun Heo69d02062013-06-12 21:04:50 -0700367 * List of cgrp_cset_links pointing at cgroups referenced from this
368 * css_set. Protected by css_set_lock.
Paul Menage817929e2007-10-18 23:39:36 -0700369 */
Tejun Heo69d02062013-06-12 21:04:50 -0700370 struct list_head cgrp_links;
Paul Menage817929e2007-10-18 23:39:36 -0700371
Tejun Heo6803c002014-04-23 11:13:16 -0400372 /* the default cgroup associated with this css_set */
373 struct cgroup *dfl_cgrp;
374
Paul Menage817929e2007-10-18 23:39:36 -0700375 /*
Tejun Heo3ed80a62014-02-08 10:36:58 -0500376 * Set of subsystem states, one for each subsystem. This array is
377 * immutable after creation apart from the init_css_set during
378 * subsystem registration (at boot time).
Paul Menage817929e2007-10-18 23:39:36 -0700379 */
380 struct cgroup_subsys_state *subsys[CGROUP_SUBSYS_COUNT];
Ben Blumc3783692009-09-23 15:56:29 -0700381
Tejun Heob3dc0942014-02-25 10:04:01 -0500382 /*
383 * List of csets participating in the on-going migration either as
384 * source or destination. Protected by cgroup_mutex.
385 */
Tejun Heo1958d2d2014-02-25 10:04:03 -0500386 struct list_head mg_preload_node;
Tejun Heob3dc0942014-02-25 10:04:01 -0500387 struct list_head mg_node;
388
389 /*
390 * If this cset is acting as the source of migration the following
391 * two fields are set. mg_src_cgrp is the source cgroup of the
392 * on-going migration and mg_dst_cset is the destination cset the
393 * target tasks on this cset should be migrated to. Protected by
394 * cgroup_mutex.
395 */
396 struct cgroup *mg_src_cgrp;
397 struct css_set *mg_dst_cset;
398
Tejun Heo2d8f2432014-04-23 11:13:15 -0400399 /*
400 * On the default hierarhcy, ->subsys[ssid] may point to a css
401 * attached to an ancestor instead of the cgroup this css_set is
402 * associated with. The following node is anchored at
403 * ->subsys[ssid]->cgroup->e_csets[ssid] and provides a way to
404 * iterate through all css's attached to a given cgroup.
405 */
406 struct list_head e_cset_node[CGROUP_SUBSYS_COUNT];
407
Ben Blumc3783692009-09-23 15:56:29 -0700408 /* For RCU-protected deletion */
409 struct rcu_head rcu_head;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700410};
411
Paul Menage91796562008-04-29 01:00:01 -0700412/*
Paul Menaged20a3902009-04-02 16:57:22 -0700413 * struct cftype: handler definitions for cgroup control files
Paul Menageddbcc7e2007-10-18 23:39:30 -0700414 *
415 * When reading/writing to a file:
Li Zefana043e3b2008-02-23 15:24:09 -0800416 * - the cgroup to use is file->f_dentry->d_parent->d_fsdata
Paul Menageddbcc7e2007-10-18 23:39:30 -0700417 * - the 'cftype' of the file is file->f_dentry->d_fsdata
418 */
419
Tejun Heo8e3f6542012-04-01 12:09:55 -0700420/* cftype->flags */
Tejun Heo02c402d2013-06-24 15:21:47 -0700421enum {
Li Zefan6f4b7e62013-07-31 16:18:36 +0800422 CFTYPE_ONLY_ON_ROOT = (1 << 0), /* only create on root cgrp */
423 CFTYPE_NOT_ON_ROOT = (1 << 1), /* don't create on root cgrp */
Tejun Heo02c402d2013-06-24 15:21:47 -0700424 CFTYPE_INSANE = (1 << 2), /* don't create if sane_behavior */
Tejun Heo9fa4db32013-08-26 18:40:56 -0400425 CFTYPE_NO_PREFIX = (1 << 3), /* (DON'T USE FOR NEW FILES) no subsys prefix */
Tejun Heo8cbbf2c2014-03-19 10:23:55 -0400426 CFTYPE_ONLY_ON_DFL = (1 << 4), /* only on default hierarchy */
Tejun Heo02c402d2013-06-24 15:21:47 -0700427};
Tejun Heo8e3f6542012-04-01 12:09:55 -0700428
429#define MAX_CFTYPE_NAME 64
430
Paul Menageddbcc7e2007-10-18 23:39:30 -0700431struct cftype {
Paul Menaged20a3902009-04-02 16:57:22 -0700432 /*
433 * By convention, the name should begin with the name of the
Tejun Heo8e3f6542012-04-01 12:09:55 -0700434 * subsystem, followed by a period. Zero length string indicates
435 * end of cftype array.
Paul Menaged20a3902009-04-02 16:57:22 -0700436 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700437 char name[MAX_CFTYPE_NAME];
438 int private;
Li Zefan099fca32009-04-02 16:57:29 -0700439 /*
440 * If not 0, file mode is set to this value, otherwise it will
441 * be figured out automatically
442 */
Al Viroa5e7ed32011-07-26 01:55:55 -0400443 umode_t mode;
Paul Menagedb3b1492008-07-25 01:46:58 -0700444
445 /*
Tejun Heo5f469902014-02-11 11:52:48 -0500446 * The maximum length of string, excluding trailing nul, that can
Tejun Heo451af502014-05-13 12:16:21 -0400447 * be passed to write. If < PAGE_SIZE-1, PAGE_SIZE-1 is assumed.
Paul Menagedb3b1492008-07-25 01:46:58 -0700448 */
449 size_t max_write_len;
450
Tejun Heo8e3f6542012-04-01 12:09:55 -0700451 /* CFTYPE_* flags */
452 unsigned int flags;
453
Tejun Heo2bb566c2013-08-08 20:11:23 -0400454 /*
Tejun Heo0adb0702014-02-12 09:29:48 -0500455 * Fields used for internal bookkeeping. Initialized automatically
456 * during registration.
Tejun Heo2bb566c2013-08-08 20:11:23 -0400457 */
Tejun Heo0adb0702014-02-12 09:29:48 -0500458 struct cgroup_subsys *ss; /* NULL for cgroup core files */
459 struct list_head node; /* anchored at ss->cfts */
Tejun Heo2bd59d42014-02-11 11:52:49 -0500460 struct kernfs_ops *kf_ops;
461
Paul Menageddbcc7e2007-10-18 23:39:30 -0700462 /*
Paul Menagef4c753b2008-04-29 00:59:56 -0700463 * read_u64() is a shortcut for the common case of returning a
Paul Menageddbcc7e2007-10-18 23:39:30 -0700464 * single integer. Use it in place of read()
465 */
Tejun Heo182446d2013-08-08 20:11:24 -0400466 u64 (*read_u64)(struct cgroup_subsys_state *css, struct cftype *cft);
Paul Menage91796562008-04-29 01:00:01 -0700467 /*
Paul Menagee73d2c62008-04-29 01:00:06 -0700468 * read_s64() is a signed version of read_u64()
469 */
Tejun Heo182446d2013-08-08 20:11:24 -0400470 s64 (*read_s64)(struct cgroup_subsys_state *css, struct cftype *cft);
Tejun Heo2da8ca82013-12-05 12:28:04 -0500471
472 /* generic seq_file read interface */
473 int (*seq_show)(struct seq_file *sf, void *v);
Paul Menage91796562008-04-29 01:00:01 -0700474
Tejun Heo6612f052013-12-05 12:28:04 -0500475 /* optional ops, implement all or none */
476 void *(*seq_start)(struct seq_file *sf, loff_t *ppos);
477 void *(*seq_next)(struct seq_file *sf, void *v, loff_t *ppos);
478 void (*seq_stop)(struct seq_file *sf, void *v);
479
Paul Menage355e0c42007-10-18 23:39:33 -0700480 /*
Paul Menagef4c753b2008-04-29 00:59:56 -0700481 * write_u64() is a shortcut for the common case of accepting
Paul Menage355e0c42007-10-18 23:39:33 -0700482 * a single integer (as parsed by simple_strtoull) from
483 * userspace. Use in place of write(); return 0 or error.
484 */
Tejun Heo182446d2013-08-08 20:11:24 -0400485 int (*write_u64)(struct cgroup_subsys_state *css, struct cftype *cft,
486 u64 val);
Paul Menagee73d2c62008-04-29 01:00:06 -0700487 /*
488 * write_s64() is a signed version of write_u64()
489 */
Tejun Heo182446d2013-08-08 20:11:24 -0400490 int (*write_s64)(struct cgroup_subsys_state *css, struct cftype *cft,
491 s64 val);
Paul Menage355e0c42007-10-18 23:39:33 -0700492
Pavel Emelyanovd447ea22008-04-29 01:00:08 -0700493 /*
Tejun Heob4168642014-05-13 12:16:21 -0400494 * write() is the generic write callback which maps directly to
495 * kernfs write operation and overrides all other operations.
496 * Maximum write size is determined by ->max_write_len. Use
497 * of_css/cft() to access the associated css and cft.
498 */
499 ssize_t (*write)(struct kernfs_open_file *of,
500 char *buf, size_t nbytes, loff_t off);
501
Tejun Heo2bd59d42014-02-11 11:52:49 -0500502#ifdef CONFIG_DEBUG_LOCK_ALLOC
503 struct lock_class_key lockdep_key;
504#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -0700505};
506
Tejun Heoa2dd4242014-03-19 10:23:55 -0400507extern struct cgroup_root cgrp_dfl_root;
Tejun Heo5024ae22014-05-07 21:31:17 -0400508extern struct css_set init_css_set;
Tejun Heoa2dd4242014-03-19 10:23:55 -0400509
510static inline bool cgroup_on_dfl(const struct cgroup *cgrp)
511{
512 return cgrp->root == &cgrp_dfl_root;
513}
514
Tejun Heo8e3f6542012-04-01 12:09:55 -0700515/*
Tejun Heo873fe092013-04-14 20:15:26 -0700516 * See the comment above CGRP_ROOT_SANE_BEHAVIOR for details. This
517 * function can be called as long as @cgrp is accessible.
518 */
519static inline bool cgroup_sane_behavior(const struct cgroup *cgrp)
520{
521 return cgrp->root->flags & CGRP_ROOT_SANE_BEHAVIOR;
522}
523
Tejun Heo07bc3562014-02-13 06:58:39 -0500524/* no synchronization, the result can only be used as a hint */
525static inline bool cgroup_has_tasks(struct cgroup *cgrp)
526{
527 return !list_empty(&cgrp->cset_links);
528}
529
Tejun Heob1664922014-02-11 11:52:49 -0500530/* returns ino associated with a cgroup, 0 indicates unmounted root */
531static inline ino_t cgroup_ino(struct cgroup *cgrp)
532{
Tejun Heo2bd59d42014-02-11 11:52:49 -0500533 if (cgrp->kn)
534 return cgrp->kn->ino;
Tejun Heob1664922014-02-11 11:52:49 -0500535 else
536 return 0;
537}
538
Tejun Heob4168642014-05-13 12:16:21 -0400539/* cft/css accessors for cftype->write() operation */
540static inline struct cftype *of_cft(struct kernfs_open_file *of)
Tejun Heo7da11272013-12-05 12:28:04 -0500541{
Tejun Heo2bd59d42014-02-11 11:52:49 -0500542 return of->kn->priv;
Tejun Heo7da11272013-12-05 12:28:04 -0500543}
544
Tejun Heob4168642014-05-13 12:16:21 -0400545struct cgroup_subsys_state *of_css(struct kernfs_open_file *of);
546
547/* cft/css accessors for cftype->seq_*() operations */
548static inline struct cftype *seq_cft(struct seq_file *seq)
549{
550 return of_cft(seq->private);
551}
552
553static inline struct cgroup_subsys_state *seq_css(struct seq_file *seq)
554{
555 return of_css(seq->private);
556}
Tejun Heo59f52962014-02-11 11:52:49 -0500557
Tejun Heoe61734c2014-02-12 09:29:50 -0500558/*
559 * Name / path handling functions. All are thin wrappers around the kernfs
560 * counterparts and can be called under any context.
561 */
562
563static inline int cgroup_name(struct cgroup *cgrp, char *buf, size_t buflen)
564{
Tejun Heofdce6bf2014-03-19 10:23:54 -0400565 return kernfs_name(cgrp->kn, buf, buflen);
Tejun Heoe61734c2014-02-12 09:29:50 -0500566}
567
568static inline char * __must_check cgroup_path(struct cgroup *cgrp, char *buf,
569 size_t buflen)
570{
Tejun Heofdce6bf2014-03-19 10:23:54 -0400571 return kernfs_path(cgrp->kn, buf, buflen);
Tejun Heoe61734c2014-02-12 09:29:50 -0500572}
573
574static inline void pr_cont_cgroup_name(struct cgroup *cgrp)
575{
Tejun Heofdce6bf2014-03-19 10:23:54 -0400576 pr_cont_kernfs_name(cgrp->kn);
Tejun Heoe61734c2014-02-12 09:29:50 -0500577}
578
579static inline void pr_cont_cgroup_path(struct cgroup *cgrp)
580{
Tejun Heofdce6bf2014-03-19 10:23:54 -0400581 pr_cont_kernfs_path(cgrp->kn);
Tejun Heoe61734c2014-02-12 09:29:50 -0500582}
583
584char *task_cgroup_path(struct task_struct *task, char *buf, size_t buflen);
585
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -0400586int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts);
Tejun Heo2bb566c2013-08-08 20:11:23 -0400587int cgroup_rm_cftypes(struct cftype *cfts);
Tejun Heo8e3f6542012-04-01 12:09:55 -0700588
Li Zefan78574cf2013-04-08 19:00:38 -0700589bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700590
Thadeu Lima de Souza Cascardo21acb9c2009-02-04 10:12:08 +0100591/*
Tejun Heo2f7ee562011-12-12 18:12:21 -0800592 * Control Group taskset, used to pass around set of tasks to cgroup_subsys
593 * methods.
594 */
595struct cgroup_taskset;
596struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset);
597struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset);
Tejun Heo2f7ee562011-12-12 18:12:21 -0800598
599/**
600 * cgroup_taskset_for_each - iterate cgroup_taskset
601 * @task: the loop cursor
Tejun Heo2f7ee562011-12-12 18:12:21 -0800602 * @tset: taskset to iterate
603 */
Tejun Heo924f0d9a2014-02-13 06:58:41 -0500604#define cgroup_taskset_for_each(task, tset) \
Tejun Heo2f7ee562011-12-12 18:12:21 -0800605 for ((task) = cgroup_taskset_first((tset)); (task); \
Tejun Heo924f0d9a2014-02-13 06:58:41 -0500606 (task) = cgroup_taskset_next((tset)))
Tejun Heo2f7ee562011-12-12 18:12:21 -0800607
608/*
Thadeu Lima de Souza Cascardo21acb9c2009-02-04 10:12:08 +0100609 * Control Group subsystem type.
610 * See Documentation/cgroups/cgroups.txt for details
611 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700612
613struct cgroup_subsys {
Tejun Heoeb954192013-08-08 20:11:23 -0400614 struct cgroup_subsys_state *(*css_alloc)(struct cgroup_subsys_state *parent_css);
615 int (*css_online)(struct cgroup_subsys_state *css);
616 void (*css_offline)(struct cgroup_subsys_state *css);
617 void (*css_free)(struct cgroup_subsys_state *css);
Tejun Heo92fb9742012-11-19 08:13:38 -0800618
Tejun Heoeb954192013-08-08 20:11:23 -0400619 int (*can_attach)(struct cgroup_subsys_state *css,
620 struct cgroup_taskset *tset);
621 void (*cancel_attach)(struct cgroup_subsys_state *css,
622 struct cgroup_taskset *tset);
623 void (*attach)(struct cgroup_subsys_state *css,
624 struct cgroup_taskset *tset);
Li Zefan761b3ef52012-01-31 13:47:36 +0800625 void (*fork)(struct task_struct *task);
Tejun Heoeb954192013-08-08 20:11:23 -0400626 void (*exit)(struct cgroup_subsys_state *css,
627 struct cgroup_subsys_state *old_css,
Li Zefan761b3ef52012-01-31 13:47:36 +0800628 struct task_struct *task);
Tejun Heoeb954192013-08-08 20:11:23 -0400629 void (*bind)(struct cgroup_subsys_state *root_css);
Hugh Dickinse5991372009-01-06 14:39:22 -0800630
Paul Menage8bab8dd2008-04-04 14:29:57 -0700631 int disabled;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700632 int early_init;
Tejun Heo48ddbe12012-04-01 12:09:56 -0700633
634 /*
Tejun Heo8c7f6ed2012-09-13 12:20:58 -0700635 * If %false, this subsystem is properly hierarchical -
636 * configuration, resource accounting and restriction on a parent
637 * cgroup cover those of its children. If %true, hierarchy support
638 * is broken in some ways - some subsystems ignore hierarchy
639 * completely while others are only implemented half-way.
640 *
641 * It's now disallowed to create nested cgroups if the subsystem is
642 * broken and cgroup core will emit a warning message on such
643 * cases. Eventually, all subsystems will be made properly
644 * hierarchical and this will go away.
645 */
646 bool broken_hierarchy;
647 bool warned_broken_hierarchy;
648
Tejun Heo073219e2014-02-08 10:36:58 -0500649 /* the following two fields are initialized automtically during boot */
Tejun Heoaec25022014-02-08 10:36:58 -0500650 int id;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700651#define MAX_CGROUP_TYPE_NAMELEN 32
652 const char *name;
653
Tejun Heob85d2042013-12-06 15:11:57 -0500654 /* link to parent, protected by cgroup_lock() */
Tejun Heo3dd06ff2014-03-19 10:23:54 -0400655 struct cgroup_root *root;
Ben Blume6a11052010-03-10 15:22:09 -0800656
Tejun Heo15a4c832014-05-04 15:09:14 -0400657 /* idr for css->id */
658 struct idr css_idr;
659
Tejun Heo0adb0702014-02-12 09:29:48 -0500660 /*
661 * List of cftypes. Each entry is the first entry of an array
662 * terminated by zero length name.
663 */
664 struct list_head cfts;
Tejun Heo8e3f6542012-04-01 12:09:55 -0700665
Tejun Heode00ffa2014-02-11 11:52:48 -0500666 /* base cftypes, automatically registered with subsys itself */
Tejun Heo8e3f6542012-04-01 12:09:55 -0700667 struct cftype *base_cftypes;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700668};
669
Tejun Heo073219e2014-02-08 10:36:58 -0500670#define SUBSYS(_x) extern struct cgroup_subsys _x ## _cgrp_subsys;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700671#include <linux/cgroup_subsys.h>
672#undef SUBSYS
673
Tejun Heo8af01f52013-08-08 20:11:22 -0400674/**
Tejun Heo14611e52013-06-25 11:48:32 -0700675 * task_css_set_check - obtain a task's css_set with extra access conditions
676 * @task: the task to obtain css_set for
677 * @__c: extra condition expression to be passed to rcu_dereference_check()
678 *
679 * A task's css_set is RCU protected, initialized and exited while holding
680 * task_lock(), and can only be modified while holding both cgroup_mutex
681 * and task_lock() while the task is alive. This macro verifies that the
682 * caller is inside proper critical section and returns @task's css_set.
683 *
684 * The caller can also specify additional allowed conditions via @__c, such
685 * as locks used during the cgroup_subsys::attach() methods.
Peter Zijlstradc61b1d2010-06-08 11:40:42 +0200686 */
Tejun Heo22194492013-04-07 09:29:51 -0700687#ifdef CONFIG_PROVE_RCU
688extern struct mutex cgroup_mutex;
Tejun Heo0e1d7682014-02-25 10:04:03 -0500689extern struct rw_semaphore css_set_rwsem;
Tejun Heo14611e52013-06-25 11:48:32 -0700690#define task_css_set_check(task, __c) \
691 rcu_dereference_check((task)->cgroups, \
Tejun Heo0e1d7682014-02-25 10:04:03 -0500692 lockdep_is_held(&cgroup_mutex) || \
693 lockdep_is_held(&css_set_rwsem) || \
694 ((task)->flags & PF_EXITING) || (__c))
Tejun Heo22194492013-04-07 09:29:51 -0700695#else
Tejun Heo14611e52013-06-25 11:48:32 -0700696#define task_css_set_check(task, __c) \
697 rcu_dereference((task)->cgroups)
Tejun Heo22194492013-04-07 09:29:51 -0700698#endif
Peter Zijlstradc61b1d2010-06-08 11:40:42 +0200699
Tejun Heo14611e52013-06-25 11:48:32 -0700700/**
Tejun Heo8af01f52013-08-08 20:11:22 -0400701 * task_css_check - obtain css for (task, subsys) w/ extra access conds
Tejun Heo14611e52013-06-25 11:48:32 -0700702 * @task: the target task
703 * @subsys_id: the target subsystem ID
704 * @__c: extra condition expression to be passed to rcu_dereference_check()
705 *
706 * Return the cgroup_subsys_state for the (@task, @subsys_id) pair. The
707 * synchronization rules are the same as task_css_set_check().
708 */
Tejun Heo8af01f52013-08-08 20:11:22 -0400709#define task_css_check(task, subsys_id, __c) \
Tejun Heo14611e52013-06-25 11:48:32 -0700710 task_css_set_check((task), (__c))->subsys[(subsys_id)]
711
712/**
713 * task_css_set - obtain a task's css_set
714 * @task: the task to obtain css_set for
715 *
716 * See task_css_set_check().
717 */
718static inline struct css_set *task_css_set(struct task_struct *task)
719{
720 return task_css_set_check(task, false);
721}
722
723/**
Tejun Heo8af01f52013-08-08 20:11:22 -0400724 * task_css - obtain css for (task, subsys)
Tejun Heo14611e52013-06-25 11:48:32 -0700725 * @task: the target task
726 * @subsys_id: the target subsystem ID
727 *
Tejun Heo8af01f52013-08-08 20:11:22 -0400728 * See task_css_check().
Tejun Heo14611e52013-06-25 11:48:32 -0700729 */
Tejun Heo8af01f52013-08-08 20:11:22 -0400730static inline struct cgroup_subsys_state *task_css(struct task_struct *task,
731 int subsys_id)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700732{
Tejun Heo8af01f52013-08-08 20:11:22 -0400733 return task_css_check(task, subsys_id, false);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700734}
735
Tejun Heo5024ae22014-05-07 21:31:17 -0400736/**
737 * task_css_is_root - test whether a task belongs to the root css
738 * @task: the target task
739 * @subsys_id: the target subsystem ID
740 *
741 * Test whether @task belongs to the root css on the specified subsystem.
742 * May be invoked in any context.
743 */
744static inline bool task_css_is_root(struct task_struct *task, int subsys_id)
745{
746 return task_css_check(task, subsys_id, true) ==
747 init_css_set.subsys[subsys_id];
748}
749
Tejun Heo8af01f52013-08-08 20:11:22 -0400750static inline struct cgroup *task_cgroup(struct task_struct *task,
751 int subsys_id)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700752{
Tejun Heo8af01f52013-08-08 20:11:22 -0400753 return task_css(task, subsys_id)->cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700754}
755
Tejun Heo492eb212013-08-08 20:11:25 -0400756struct cgroup_subsys_state *css_next_child(struct cgroup_subsys_state *pos,
757 struct cgroup_subsys_state *parent);
Tejun Heo53fa5262013-05-24 10:55:38 +0900758
Li Zefan1cb650b2013-08-19 10:05:24 +0800759struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss);
760
Tejun Heo574bd9f2012-11-09 09:12:29 -0800761/**
Tejun Heo492eb212013-08-08 20:11:25 -0400762 * css_for_each_child - iterate through children of a css
763 * @pos: the css * to use as the loop cursor
764 * @parent: css whose children to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -0800765 *
Tejun Heo492eb212013-08-08 20:11:25 -0400766 * Walk @parent's children. Must be called under rcu_read_lock(). A child
767 * css which hasn't finished ->css_online() or already has finished
Tejun Heo92fb9742012-11-19 08:13:38 -0800768 * ->css_offline() may show up during traversal and it's each subsystem's
Tejun Heo574bd9f2012-11-09 09:12:29 -0800769 * responsibility to verify that each @pos is alive.
770 *
Tejun Heo92fb9742012-11-19 08:13:38 -0800771 * If a subsystem synchronizes against the parent in its ->css_online() and
Tejun Heo492eb212013-08-08 20:11:25 -0400772 * before starting iterating, a css which finished ->css_online() is
Tejun Heo92fb9742012-11-19 08:13:38 -0800773 * guaranteed to be visible in the future iterations.
Tejun Heo75501a62013-05-24 10:55:38 +0900774 *
775 * It is allowed to temporarily drop RCU read lock during iteration. The
776 * caller is responsible for ensuring that @pos remains accessible until
777 * the start of the next iteration by, for example, bumping the css refcnt.
Tejun Heo574bd9f2012-11-09 09:12:29 -0800778 */
Tejun Heo492eb212013-08-08 20:11:25 -0400779#define css_for_each_child(pos, parent) \
780 for ((pos) = css_next_child(NULL, (parent)); (pos); \
781 (pos) = css_next_child((pos), (parent)))
Tejun Heo574bd9f2012-11-09 09:12:29 -0800782
Tejun Heo492eb212013-08-08 20:11:25 -0400783struct cgroup_subsys_state *
784css_next_descendant_pre(struct cgroup_subsys_state *pos,
785 struct cgroup_subsys_state *css);
786
787struct cgroup_subsys_state *
788css_rightmost_descendant(struct cgroup_subsys_state *pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -0800789
790/**
Tejun Heo492eb212013-08-08 20:11:25 -0400791 * css_for_each_descendant_pre - pre-order walk of a css's descendants
792 * @pos: the css * to use as the loop cursor
793 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -0800794 *
Tejun Heobd8815a2013-08-08 20:11:27 -0400795 * Walk @root's descendants. @root is included in the iteration and the
796 * first node to be visited. Must be called under rcu_read_lock(). A
Tejun Heo492eb212013-08-08 20:11:25 -0400797 * descendant css which hasn't finished ->css_online() or already has
Tejun Heo92fb9742012-11-19 08:13:38 -0800798 * finished ->css_offline() may show up during traversal and it's each
Tejun Heo574bd9f2012-11-09 09:12:29 -0800799 * subsystem's responsibility to verify that each @pos is alive.
800 *
Tejun Heo92fb9742012-11-19 08:13:38 -0800801 * If a subsystem synchronizes against the parent in its ->css_online() and
802 * before starting iterating, and synchronizes against @pos on each
Tejun Heo492eb212013-08-08 20:11:25 -0400803 * iteration, any descendant css which finished ->css_online() is
Tejun Heo574bd9f2012-11-09 09:12:29 -0800804 * guaranteed to be visible in the future iterations.
805 *
806 * In other words, the following guarantees that a descendant can't escape
807 * state updates of its ancestors.
808 *
Tejun Heo492eb212013-08-08 20:11:25 -0400809 * my_online(@css)
Tejun Heo574bd9f2012-11-09 09:12:29 -0800810 * {
Tejun Heo492eb212013-08-08 20:11:25 -0400811 * Lock @css's parent and @css;
812 * Inherit state from the parent;
Tejun Heo574bd9f2012-11-09 09:12:29 -0800813 * Unlock both.
814 * }
815 *
Tejun Heo492eb212013-08-08 20:11:25 -0400816 * my_update_state(@css)
Tejun Heo574bd9f2012-11-09 09:12:29 -0800817 * {
Tejun Heo492eb212013-08-08 20:11:25 -0400818 * css_for_each_descendant_pre(@pos, @css) {
Tejun Heo574bd9f2012-11-09 09:12:29 -0800819 * Lock @pos;
Tejun Heobd8815a2013-08-08 20:11:27 -0400820 * if (@pos == @css)
821 * Update @css's state;
822 * else
823 * Verify @pos is alive and inherit state from its parent;
Tejun Heo574bd9f2012-11-09 09:12:29 -0800824 * Unlock @pos;
825 * }
826 * }
827 *
828 * As long as the inheriting step, including checking the parent state, is
829 * enclosed inside @pos locking, double-locking the parent isn't necessary
830 * while inheriting. The state update to the parent is guaranteed to be
831 * visible by walking order and, as long as inheriting operations to the
832 * same @pos are atomic to each other, multiple updates racing each other
833 * still result in the correct state. It's guaranateed that at least one
Tejun Heo492eb212013-08-08 20:11:25 -0400834 * inheritance happens for any css after the latest update to its parent.
Tejun Heo574bd9f2012-11-09 09:12:29 -0800835 *
836 * If checking parent's state requires locking the parent, each inheriting
837 * iteration should lock and unlock both @pos->parent and @pos.
838 *
839 * Alternatively, a subsystem may choose to use a single global lock to
Tejun Heo92fb9742012-11-19 08:13:38 -0800840 * synchronize ->css_online() and ->css_offline() against tree-walking
Tejun Heo574bd9f2012-11-09 09:12:29 -0800841 * operations.
Tejun Heo75501a62013-05-24 10:55:38 +0900842 *
843 * It is allowed to temporarily drop RCU read lock during iteration. The
844 * caller is responsible for ensuring that @pos remains accessible until
845 * the start of the next iteration by, for example, bumping the css refcnt.
Tejun Heo574bd9f2012-11-09 09:12:29 -0800846 */
Tejun Heo492eb212013-08-08 20:11:25 -0400847#define css_for_each_descendant_pre(pos, css) \
848 for ((pos) = css_next_descendant_pre(NULL, (css)); (pos); \
849 (pos) = css_next_descendant_pre((pos), (css)))
Tejun Heo574bd9f2012-11-09 09:12:29 -0800850
Tejun Heo492eb212013-08-08 20:11:25 -0400851struct cgroup_subsys_state *
852css_next_descendant_post(struct cgroup_subsys_state *pos,
853 struct cgroup_subsys_state *css);
Tejun Heo574bd9f2012-11-09 09:12:29 -0800854
855/**
Tejun Heo492eb212013-08-08 20:11:25 -0400856 * css_for_each_descendant_post - post-order walk of a css's descendants
857 * @pos: the css * to use as the loop cursor
858 * @css: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -0800859 *
Tejun Heo492eb212013-08-08 20:11:25 -0400860 * Similar to css_for_each_descendant_pre() but performs post-order
Tejun Heobd8815a2013-08-08 20:11:27 -0400861 * traversal instead. @root is included in the iteration and the last
862 * node to be visited. Note that the walk visibility guarantee described
863 * in pre-order walk doesn't apply the same to post-order walks.
Tejun Heo574bd9f2012-11-09 09:12:29 -0800864 */
Tejun Heo492eb212013-08-08 20:11:25 -0400865#define css_for_each_descendant_post(pos, css) \
866 for ((pos) = css_next_descendant_post(NULL, (css)); (pos); \
867 (pos) = css_next_descendant_post((pos), (css)))
Tejun Heo574bd9f2012-11-09 09:12:29 -0800868
Tejun Heo72ec7022013-08-08 20:11:26 -0400869/* A css_task_iter should be treated as an opaque object */
870struct css_task_iter {
Tejun Heo3ebb2b62014-04-23 11:13:15 -0400871 struct cgroup_subsys *ss;
872
Tejun Heo0f0a2b42014-04-23 11:13:15 -0400873 struct list_head *cset_pos;
874 struct list_head *cset_head;
875
876 struct list_head *task_pos;
877 struct list_head *tasks_head;
878 struct list_head *mg_tasks_head;
Paul Menage817929e2007-10-18 23:39:36 -0700879};
880
Tejun Heo72ec7022013-08-08 20:11:26 -0400881void css_task_iter_start(struct cgroup_subsys_state *css,
882 struct css_task_iter *it);
883struct task_struct *css_task_iter_next(struct css_task_iter *it);
884void css_task_iter_end(struct css_task_iter *it);
Tejun Heoe5358372013-08-08 20:11:26 -0400885
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -0700886int cgroup_attach_task_all(struct task_struct *from, struct task_struct *);
Tejun Heo8cc99342013-04-07 09:29:50 -0700887int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from);
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -0700888
Tejun Heoec903c02014-05-13 12:11:01 -0400889struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry,
890 struct cgroup_subsys *ss);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700891
Paul Menageddbcc7e2007-10-18 23:39:30 -0700892#else /* !CONFIG_CGROUPS */
893
894static inline int cgroup_init_early(void) { return 0; }
895static inline int cgroup_init(void) { return 0; }
Paul Menageb4f48b62007-10-18 23:39:33 -0700896static inline void cgroup_fork(struct task_struct *p) {}
Paul Menage817929e2007-10-18 23:39:36 -0700897static inline void cgroup_post_fork(struct task_struct *p) {}
Li Zefan1ec41832014-03-28 15:22:19 +0800898static inline void cgroup_exit(struct task_struct *p) {}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700899
Balbir Singh846c7bb2007-10-18 23:39:44 -0700900static inline int cgroupstats_build(struct cgroupstats *stats,
901 struct dentry *dentry)
902{
903 return -EINVAL;
904}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700905
Sridhar Samudralad7926ee2010-05-30 22:24:39 +0200906/* No cgroups - nothing to do */
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -0700907static inline int cgroup_attach_task_all(struct task_struct *from,
908 struct task_struct *t)
909{
910 return 0;
911}
Sridhar Samudralad7926ee2010-05-30 22:24:39 +0200912
Paul Menageddbcc7e2007-10-18 23:39:30 -0700913#endif /* !CONFIG_CGROUPS */
914
Paul Menageddbcc7e2007-10-18 23:39:30 -0700915#endif /* _LINUX_CGROUP_H */