Commit | Line | Data |
---|---|---|
ddbcc7e8 PM |
1 | #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> | |
ddbcc7e8 PM |
12 | #include <linux/cpumask.h> |
13 | #include <linux/nodemask.h> | |
14 | #include <linux/rcupdate.h> | |
eb6fd504 | 15 | #include <linux/rculist.h> |
846c7bb0 | 16 | #include <linux/cgroupstats.h> |
cc31edce | 17 | #include <linux/rwsem.h> |
38460b48 | 18 | #include <linux/idr.h> |
48ddbe19 | 19 | #include <linux/workqueue.h> |
25a7e684 | 20 | #include <linux/fs.h> |
d3daf28d | 21 | #include <linux/percpu-refcount.h> |
7da11279 | 22 | #include <linux/seq_file.h> |
2bd59d48 | 23 | #include <linux/kernfs.h> |
f8f22e53 | 24 | #include <linux/wait.h> |
ddbcc7e8 PM |
25 | |
26 | #ifdef CONFIG_CGROUPS | |
27 | ||
3dd06ffa | 28 | struct cgroup_root; |
ddbcc7e8 PM |
29 | struct cgroup_subsys; |
30 | struct inode; | |
84eea842 | 31 | struct cgroup; |
ddbcc7e8 PM |
32 | |
33 | extern int cgroup_init_early(void); | |
34 | extern int cgroup_init(void); | |
b4f48b63 | 35 | extern void cgroup_fork(struct task_struct *p); |
817929ec | 36 | extern void cgroup_post_fork(struct task_struct *p); |
1ec41830 | 37 | extern void cgroup_exit(struct task_struct *p); |
846c7bb0 BS |
38 | extern int cgroupstats_build(struct cgroupstats *stats, |
39 | struct dentry *dentry); | |
ddbcc7e8 | 40 | |
8d8b97ba | 41 | extern int proc_cgroup_show(struct seq_file *, void *); |
a424316c | 42 | |
3ed80a62 | 43 | /* define the enumeration of all cgroup subsystems */ |
073219e9 | 44 | #define SUBSYS(_x) _x ## _cgrp_id, |
817929ec PM |
45 | enum cgroup_subsys_id { |
46 | #include <linux/cgroup_subsys.h> | |
a6f00298 | 47 | CGROUP_SUBSYS_COUNT, |
817929ec PM |
48 | }; |
49 | #undef SUBSYS | |
50 | ||
5c9d535b TH |
51 | /* |
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 | */ | |
ddbcc7e8 | 58 | struct cgroup_subsys_state { |
5c9d535b | 59 | /* PI: the cgroup that this css is attached to */ |
ddbcc7e8 PM |
60 | struct cgroup *cgroup; |
61 | ||
5c9d535b | 62 | /* PI: the cgroup subsystem that this css is attached to */ |
72c97e54 TH |
63 | struct cgroup_subsys *ss; |
64 | ||
d3daf28d TH |
65 | /* reference count - access via css_[try]get() and css_put() */ |
66 | struct percpu_ref refcnt; | |
ddbcc7e8 | 67 | |
5c9d535b | 68 | /* PI: the parent css */ |
0ae78e0b TH |
69 | struct cgroup_subsys_state *parent; |
70 | ||
d5c419b6 TH |
71 | /* siblings list anchored at the parent's ->children */ |
72 | struct list_head sibling; | |
73 | struct list_head children; | |
74 | ||
15a4c835 | 75 | /* |
5c9d535b | 76 | * PI: Subsys-unique ID. 0 is unused and root is always 1. The |
15a4c835 TH |
77 | * matching css can be looked up using css_from_id(). |
78 | */ | |
79 | int id; | |
80 | ||
69dfa00c | 81 | unsigned int flags; |
48ddbe19 | 82 | |
0c21ead1 TH |
83 | /* percpu_ref killing and RCU release */ |
84 | struct rcu_head rcu_head; | |
35ef10da | 85 | struct work_struct destroy_work; |
ddbcc7e8 PM |
86 | }; |
87 | ||
88 | /* bits in struct cgroup_subsys_state flags field */ | |
89 | enum { | |
3b514d24 | 90 | CSS_NO_REF = (1 << 0), /* no reference counting for this css */ |
92fb9748 | 91 | CSS_ONLINE = (1 << 1), /* between ->css_online() and ->css_offline() */ |
ddbcc7e8 PM |
92 | }; |
93 | ||
5de0107e TH |
94 | /** |
95 | * css_get - obtain a reference on the specified css | |
96 | * @css: target css | |
97 | * | |
98 | * The caller must already have a reference. | |
ddbcc7e8 | 99 | */ |
ddbcc7e8 PM |
100 | static inline void css_get(struct cgroup_subsys_state *css) |
101 | { | |
3b514d24 TH |
102 | if (!(css->flags & CSS_NO_REF)) |
103 | percpu_ref_get(&css->refcnt); | |
ddbcc7e8 | 104 | } |
e7c5ec91 | 105 | |
5de0107e | 106 | /** |
ec903c0c | 107 | * css_tryget_online - try to obtain a reference on the specified css if online |
5de0107e TH |
108 | * @css: target css |
109 | * | |
ec903c0c TH |
110 | * Obtain a reference on @css if it's online. The caller naturally needs |
111 | * to ensure that @css is accessible but doesn't have to be holding a | |
5de0107e TH |
112 | * reference on it - IOW, RCU protected access is good enough for this |
113 | * function. Returns %true if a reference count was successfully obtained; | |
114 | * %false otherwise. | |
115 | */ | |
ec903c0c | 116 | static inline bool css_tryget_online(struct cgroup_subsys_state *css) |
e7c5ec91 | 117 | { |
3b514d24 TH |
118 | if (!(css->flags & CSS_NO_REF)) |
119 | return percpu_ref_tryget_live(&css->refcnt); | |
120 | return true; | |
e7c5ec91 PM |
121 | } |
122 | ||
5de0107e TH |
123 | /** |
124 | * css_put - put a css reference | |
125 | * @css: target css | |
126 | * | |
ec903c0c | 127 | * Put a reference obtained via css_get() and css_tryget_online(). |
5de0107e | 128 | */ |
ddbcc7e8 PM |
129 | static inline void css_put(struct cgroup_subsys_state *css) |
130 | { | |
3b514d24 TH |
131 | if (!(css->flags & CSS_NO_REF)) |
132 | percpu_ref_put(&css->refcnt); | |
ddbcc7e8 PM |
133 | } |
134 | ||
3116f0e3 PM |
135 | /* bits in struct cgroup flags field */ |
136 | enum { | |
137 | /* Control Group is dead */ | |
54766d4a | 138 | CGRP_DEAD, |
d20a390a PM |
139 | /* |
140 | * Control Group has previously had a child cgroup or a task, | |
141 | * but no longer (only if CGRP_NOTIFY_ON_RELEASE is set) | |
142 | */ | |
3116f0e3 PM |
143 | CGRP_RELEASABLE, |
144 | /* Control Group requires release notifications to userspace */ | |
145 | CGRP_NOTIFY_ON_RELEASE, | |
97978e6d | 146 | /* |
2260e7fc TH |
147 | * Clone the parent's configuration when creating a new child |
148 | * cpuset cgroup. For historical reasons, this option can be | |
149 | * specified at mount time and thus is implemented here. | |
97978e6d | 150 | */ |
2260e7fc | 151 | CGRP_CPUSET_CLONE_CHILDREN, |
3116f0e3 PM |
152 | }; |
153 | ||
ddbcc7e8 | 154 | struct cgroup { |
9d800df1 TH |
155 | /* self css with NULL ->ss, points back to this cgroup */ |
156 | struct cgroup_subsys_state self; | |
157 | ||
ddbcc7e8 PM |
158 | unsigned long flags; /* "unsigned long" so bitops work */ |
159 | ||
b414dc09 LZ |
160 | /* |
161 | * idr allocated in-hierarchy ID. | |
162 | * | |
7d699ddb TH |
163 | * ID 0 is not used, the ID of the root cgroup is always 1, and a |
164 | * new cgroup will be assigned with a smallest available ID. | |
0ab02ca8 LZ |
165 | * |
166 | * Allocating/Removing ID must be protected by cgroup_mutex. | |
b414dc09 LZ |
167 | */ |
168 | int id; | |
0a950f65 | 169 | |
842b597e TH |
170 | /* |
171 | * If this cgroup contains any tasks, it contributes one to | |
172 | * populated_cnt. All children with non-zero popuplated_cnt of | |
173 | * their own contribute one. The count is zero iff there's no task | |
174 | * in this cgroup or its subtree. | |
175 | */ | |
176 | int populated_cnt; | |
177 | ||
2bd59d48 | 178 | struct kernfs_node *kn; /* cgroup kernfs entry */ |
842b597e | 179 | struct kernfs_node *populated_kn; /* kn for "cgroup.subtree_populated" */ |
ddbcc7e8 | 180 | |
53fa5261 TH |
181 | /* |
182 | * Monotonically increasing unique serial number which defines a | |
183 | * uniform order among all cgroups. It's guaranteed that all | |
184 | * ->children lists are in the ascending order of ->serial_nr. | |
185 | * It's used to allow interrupting and resuming iterations. | |
186 | */ | |
187 | u64 serial_nr; | |
188 | ||
f392e51c | 189 | /* the bitmask of subsystems enabled on the child cgroups */ |
69dfa00c | 190 | unsigned int child_subsys_mask; |
94419627 | 191 | |
ddbcc7e8 | 192 | /* Private pointers for each registered subsystem */ |
73e80ed8 | 193 | struct cgroup_subsys_state __rcu *subsys[CGROUP_SUBSYS_COUNT]; |
ddbcc7e8 | 194 | |
3dd06ffa | 195 | struct cgroup_root *root; |
817929ec PM |
196 | |
197 | /* | |
69d0206c TH |
198 | * List of cgrp_cset_links pointing at css_sets with tasks in this |
199 | * cgroup. Protected by css_set_lock. | |
817929ec | 200 | */ |
69d0206c | 201 | struct list_head cset_links; |
81a6a5cd | 202 | |
2d8f243a TH |
203 | /* |
204 | * 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 | ||
81a6a5cd PM |
212 | /* |
213 | * 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; | |
cc31edce | 218 | |
72a8cb30 BB |
219 | /* |
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; | |
a47295e6 | 225 | |
f8f22e53 TH |
226 | /* used to wait for offlining of csses */ |
227 | wait_queue_head_t offline_waitq; | |
817929ec PM |
228 | }; |
229 | ||
25a7e684 TH |
230 | #define MAX_CGROUP_ROOT_NAMELEN 64 |
231 | ||
3dd06ffa | 232 | /* cgroup_root->flags */ |
25a7e684 | 233 | enum { |
873fe09e TH |
234 | /* |
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 | * | |
d3ba07c3 TH |
250 | * - Mount options "noprefix", "xattr", "clone_children", |
251 | * "release_agent" and "name" are disallowed. | |
873fe09e TH |
252 | * |
253 | * - When mounting an existing superblock, mount options should | |
254 | * match. | |
255 | * | |
256 | * - Remount is disallowed. | |
257 | * | |
0b0585c3 LT |
258 | * - rename(2) is disallowed. |
259 | * | |
f63674fd TH |
260 | * - "tasks" is removed. Everything should be at process |
261 | * granularity. Use "cgroup.procs" instead. | |
f00baae7 | 262 | * |
afb2bc14 TH |
263 | * - "cgroup.procs" is not sorted. pids will be unique unless they |
264 | * got recycled inbetween reads. | |
265 | * | |
f63674fd TH |
266 | * - "release_agent" and "notify_on_release" are removed. |
267 | * Replacement notification mechanism will be implemented. | |
873fe09e | 268 | * |
d3ba07c3 | 269 | * - "cgroup.clone_children" is removed. |
86bf4b68 | 270 | * |
842b597e TH |
271 | * - "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 | * | |
a2dd4247 TH |
277 | * - If mount is requested with sane_behavior but without any |
278 | * subsystem, the default unified hierarchy is mounted. | |
279 | * | |
5c5cc623 LZ |
280 | * - 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 | * | |
88fa523b LZ |
284 | * - cpuset: a task can be moved into an empty cpuset, and again it |
285 | * takes masks of ancestors. | |
6db8e85c | 286 | * |
f63674fd TH |
287 | * - memcg: use_hierarchy is on by default and the cgroup file for |
288 | * the flag is not created. | |
f00baae7 | 289 | * |
9138125b | 290 | * - blkcg: blk-throttle becomes properly hierarchical. |
873fe09e TH |
291 | */ |
292 | CGRP_ROOT_SANE_BEHAVIOR = (1 << 0), | |
293 | ||
25a7e684 TH |
294 | CGRP_ROOT_NOPREFIX = (1 << 1), /* mounted subsystems have no named prefix */ |
295 | CGRP_ROOT_XATTR = (1 << 2), /* supports extended attributes */ | |
0ce6cba3 TH |
296 | |
297 | /* mount options live below bit 16 */ | |
298 | CGRP_ROOT_OPTION_MASK = (1 << 16) - 1, | |
25a7e684 TH |
299 | }; |
300 | ||
301 | /* | |
3dd06ffa | 302 | * A cgroup_root represents the root of a cgroup hierarchy, and may be |
2bd59d48 | 303 | * associated with a kernfs_root to form an active hierarchy. This is |
25a7e684 TH |
304 | * internal to cgroup core. Don't access directly from controllers. |
305 | */ | |
3dd06ffa | 306 | struct cgroup_root { |
2bd59d48 | 307 | struct kernfs_root *kf_root; |
25a7e684 | 308 | |
f392e51c | 309 | /* The bitmask of subsystems attached to this hierarchy */ |
69dfa00c | 310 | unsigned int subsys_mask; |
f392e51c | 311 | |
25a7e684 TH |
312 | /* Unique id for this hierarchy. */ |
313 | int hierarchy_id; | |
314 | ||
776f02fa | 315 | /* The root cgroup. Root is destroyed on its release. */ |
3dd06ffa | 316 | struct cgroup cgrp; |
25a7e684 | 317 | |
3c9c825b TH |
318 | /* Number of cgroups in the hierarchy, used only for /proc/cgroups */ |
319 | atomic_t nr_cgrps; | |
25a7e684 TH |
320 | |
321 | /* A list running through the active hierarchies */ | |
322 | struct list_head root_list; | |
323 | ||
25a7e684 | 324 | /* Hierarchy-specific flags */ |
69dfa00c | 325 | unsigned int flags; |
25a7e684 TH |
326 | |
327 | /* IDs for cgroups in this hierarchy */ | |
4e96ee8e | 328 | struct idr cgroup_idr; |
25a7e684 TH |
329 | |
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 | ||
d20a390a PM |
337 | /* |
338 | * A css_set is a structure holding pointers to a set of | |
817929ec PM |
339 | * 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 | |
d20a390a PM |
341 | * list_add()/del() can bump the reference count on the entire cgroup |
342 | * set for a task. | |
817929ec PM |
343 | */ |
344 | ||
345 | struct css_set { | |
346 | ||
347 | /* Reference count */ | |
146aa1bd | 348 | atomic_t refcount; |
817929ec | 349 | |
472b1053 LZ |
350 | /* |
351 | * List running through all cgroup groups in the same hash | |
352 | * slot. Protected by css_set_lock | |
353 | */ | |
354 | struct hlist_node hlist; | |
355 | ||
817929ec | 356 | /* |
c7561128 TH |
357 | * 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. | |
817929ec PM |
362 | */ |
363 | struct list_head tasks; | |
c7561128 | 364 | struct list_head mg_tasks; |
817929ec PM |
365 | |
366 | /* | |
69d0206c TH |
367 | * List of cgrp_cset_links pointing at cgroups referenced from this |
368 | * css_set. Protected by css_set_lock. | |
817929ec | 369 | */ |
69d0206c | 370 | struct list_head cgrp_links; |
817929ec | 371 | |
6803c006 TH |
372 | /* the default cgroup associated with this css_set */ |
373 | struct cgroup *dfl_cgrp; | |
374 | ||
817929ec | 375 | /* |
3ed80a62 TH |
376 | * 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). | |
817929ec PM |
379 | */ |
380 | struct cgroup_subsys_state *subsys[CGROUP_SUBSYS_COUNT]; | |
c378369d | 381 | |
b3dc094e TH |
382 | /* |
383 | * List of csets participating in the on-going migration either as | |
384 | * source or destination. Protected by cgroup_mutex. | |
385 | */ | |
1958d2d5 | 386 | struct list_head mg_preload_node; |
b3dc094e TH |
387 | 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 | ||
2d8f243a TH |
399 | /* |
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 | ||
c378369d BB |
408 | /* For RCU-protected deletion */ |
409 | struct rcu_head rcu_head; | |
ddbcc7e8 PM |
410 | }; |
411 | ||
d20a390a PM |
412 | /* |
413 | * struct cftype: handler definitions for cgroup control files | |
ddbcc7e8 PM |
414 | * |
415 | * When reading/writing to a file: | |
a043e3b2 | 416 | * - the cgroup to use is file->f_dentry->d_parent->d_fsdata |
ddbcc7e8 PM |
417 | * - the 'cftype' of the file is file->f_dentry->d_fsdata |
418 | */ | |
419 | ||
8e3f6541 | 420 | /* cftype->flags */ |
02c402d9 | 421 | enum { |
6f4b7e63 LZ |
422 | CFTYPE_ONLY_ON_ROOT = (1 << 0), /* only create on root cgrp */ |
423 | CFTYPE_NOT_ON_ROOT = (1 << 1), /* don't create on root cgrp */ | |
02c402d9 | 424 | CFTYPE_INSANE = (1 << 2), /* don't create if sane_behavior */ |
9fa4db33 | 425 | CFTYPE_NO_PREFIX = (1 << 3), /* (DON'T USE FOR NEW FILES) no subsys prefix */ |
8cbbf2c9 | 426 | CFTYPE_ONLY_ON_DFL = (1 << 4), /* only on default hierarchy */ |
02c402d9 | 427 | }; |
8e3f6541 TH |
428 | |
429 | #define MAX_CFTYPE_NAME 64 | |
430 | ||
ddbcc7e8 | 431 | struct cftype { |
d20a390a PM |
432 | /* |
433 | * By convention, the name should begin with the name of the | |
8e3f6541 TH |
434 | * subsystem, followed by a period. Zero length string indicates |
435 | * end of cftype array. | |
d20a390a | 436 | */ |
ddbcc7e8 PM |
437 | char name[MAX_CFTYPE_NAME]; |
438 | int private; | |
099fca32 LZ |
439 | /* |
440 | * If not 0, file mode is set to this value, otherwise it will | |
441 | * be figured out automatically | |
442 | */ | |
a5e7ed32 | 443 | umode_t mode; |
db3b1497 PM |
444 | |
445 | /* | |
5f469907 | 446 | * The maximum length of string, excluding trailing nul, that can |
451af504 | 447 | * be passed to write. If < PAGE_SIZE-1, PAGE_SIZE-1 is assumed. |
db3b1497 PM |
448 | */ |
449 | size_t max_write_len; | |
450 | ||
8e3f6541 TH |
451 | /* CFTYPE_* flags */ |
452 | unsigned int flags; | |
453 | ||
2bb566cb | 454 | /* |
0adb0704 TH |
455 | * Fields used for internal bookkeeping. Initialized automatically |
456 | * during registration. | |
2bb566cb | 457 | */ |
0adb0704 TH |
458 | struct cgroup_subsys *ss; /* NULL for cgroup core files */ |
459 | struct list_head node; /* anchored at ss->cfts */ | |
2bd59d48 TH |
460 | struct kernfs_ops *kf_ops; |
461 | ||
ddbcc7e8 | 462 | /* |
f4c753b7 | 463 | * read_u64() is a shortcut for the common case of returning a |
ddbcc7e8 PM |
464 | * single integer. Use it in place of read() |
465 | */ | |
182446d0 | 466 | u64 (*read_u64)(struct cgroup_subsys_state *css, struct cftype *cft); |
e73d2c61 PM |
467 | /* |
468 | * read_s64() is a signed version of read_u64() | |
469 | */ | |
182446d0 | 470 | s64 (*read_s64)(struct cgroup_subsys_state *css, struct cftype *cft); |
2da8ca82 TH |
471 | |
472 | /* generic seq_file read interface */ | |
473 | int (*seq_show)(struct seq_file *sf, void *v); | |
91796569 | 474 | |
6612f05b TH |
475 | /* 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 | ||
355e0c48 | 480 | /* |
f4c753b7 | 481 | * write_u64() is a shortcut for the common case of accepting |
355e0c48 PM |
482 | * a single integer (as parsed by simple_strtoull) from |
483 | * userspace. Use in place of write(); return 0 or error. | |
484 | */ | |
182446d0 TH |
485 | int (*write_u64)(struct cgroup_subsys_state *css, struct cftype *cft, |
486 | u64 val); | |
e73d2c61 PM |
487 | /* |
488 | * write_s64() is a signed version of write_u64() | |
489 | */ | |
182446d0 TH |
490 | int (*write_s64)(struct cgroup_subsys_state *css, struct cftype *cft, |
491 | s64 val); | |
355e0c48 | 492 | |
b4168640 TH |
493 | /* |
494 | * 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 | ||
2bd59d48 TH |
502 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
503 | struct lock_class_key lockdep_key; | |
504 | #endif | |
ddbcc7e8 PM |
505 | }; |
506 | ||
a2dd4247 | 507 | extern struct cgroup_root cgrp_dfl_root; |
5024ae29 | 508 | extern struct css_set init_css_set; |
a2dd4247 TH |
509 | |
510 | static inline bool cgroup_on_dfl(const struct cgroup *cgrp) | |
511 | { | |
512 | return cgrp->root == &cgrp_dfl_root; | |
513 | } | |
514 | ||
873fe09e TH |
515 | /* |
516 | * See the comment above CGRP_ROOT_SANE_BEHAVIOR for details. This | |
517 | * function can be called as long as @cgrp is accessible. | |
518 | */ | |
519 | static inline bool cgroup_sane_behavior(const struct cgroup *cgrp) | |
520 | { | |
521 | return cgrp->root->flags & CGRP_ROOT_SANE_BEHAVIOR; | |
522 | } | |
523 | ||
07bc356e TH |
524 | /* no synchronization, the result can only be used as a hint */ |
525 | static inline bool cgroup_has_tasks(struct cgroup *cgrp) | |
526 | { | |
527 | return !list_empty(&cgrp->cset_links); | |
528 | } | |
529 | ||
b1664924 TH |
530 | /* returns ino associated with a cgroup, 0 indicates unmounted root */ |
531 | static inline ino_t cgroup_ino(struct cgroup *cgrp) | |
532 | { | |
2bd59d48 TH |
533 | if (cgrp->kn) |
534 | return cgrp->kn->ino; | |
b1664924 TH |
535 | else |
536 | return 0; | |
537 | } | |
538 | ||
b4168640 TH |
539 | /* cft/css accessors for cftype->write() operation */ |
540 | static inline struct cftype *of_cft(struct kernfs_open_file *of) | |
7da11279 | 541 | { |
2bd59d48 | 542 | return of->kn->priv; |
7da11279 TH |
543 | } |
544 | ||
b4168640 TH |
545 | struct cgroup_subsys_state *of_css(struct kernfs_open_file *of); |
546 | ||
547 | /* cft/css accessors for cftype->seq_*() operations */ | |
548 | static inline struct cftype *seq_cft(struct seq_file *seq) | |
549 | { | |
550 | return of_cft(seq->private); | |
551 | } | |
552 | ||
553 | static inline struct cgroup_subsys_state *seq_css(struct seq_file *seq) | |
554 | { | |
555 | return of_css(seq->private); | |
556 | } | |
59f5296b | 557 | |
e61734c5 TH |
558 | /* |
559 | * Name / path handling functions. All are thin wrappers around the kernfs | |
560 | * counterparts and can be called under any context. | |
561 | */ | |
562 | ||
563 | static inline int cgroup_name(struct cgroup *cgrp, char *buf, size_t buflen) | |
564 | { | |
fdce6bf8 | 565 | return kernfs_name(cgrp->kn, buf, buflen); |
e61734c5 TH |
566 | } |
567 | ||
568 | static inline char * __must_check cgroup_path(struct cgroup *cgrp, char *buf, | |
569 | size_t buflen) | |
570 | { | |
fdce6bf8 | 571 | return kernfs_path(cgrp->kn, buf, buflen); |
e61734c5 TH |
572 | } |
573 | ||
574 | static inline void pr_cont_cgroup_name(struct cgroup *cgrp) | |
575 | { | |
fdce6bf8 | 576 | pr_cont_kernfs_name(cgrp->kn); |
e61734c5 TH |
577 | } |
578 | ||
579 | static inline void pr_cont_cgroup_path(struct cgroup *cgrp) | |
580 | { | |
fdce6bf8 | 581 | pr_cont_kernfs_path(cgrp->kn); |
e61734c5 TH |
582 | } |
583 | ||
584 | char *task_cgroup_path(struct task_struct *task, char *buf, size_t buflen); | |
585 | ||
03b1cde6 | 586 | int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts); |
2bb566cb | 587 | int cgroup_rm_cftypes(struct cftype *cfts); |
8e3f6541 | 588 | |
78574cf9 | 589 | bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor); |
ddbcc7e8 | 590 | |
2f7ee569 TH |
591 | /* |
592 | * Control Group taskset, used to pass around set of tasks to cgroup_subsys | |
593 | * methods. | |
594 | */ | |
595 | struct cgroup_taskset; | |
596 | struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset); | |
597 | struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset); | |
2f7ee569 TH |
598 | |
599 | /** | |
600 | * cgroup_taskset_for_each - iterate cgroup_taskset | |
601 | * @task: the loop cursor | |
2f7ee569 TH |
602 | * @tset: taskset to iterate |
603 | */ | |
924f0d9a | 604 | #define cgroup_taskset_for_each(task, tset) \ |
2f7ee569 | 605 | for ((task) = cgroup_taskset_first((tset)); (task); \ |
924f0d9a | 606 | (task) = cgroup_taskset_next((tset))) |
2f7ee569 | 607 | |
21acb9ca TLSC |
608 | /* |
609 | * Control Group subsystem type. | |
610 | * See Documentation/cgroups/cgroups.txt for details | |
611 | */ | |
ddbcc7e8 PM |
612 | |
613 | struct cgroup_subsys { | |
eb95419b TH |
614 | 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); | |
618 | ||
619 | 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); | |
761b3ef5 | 625 | void (*fork)(struct task_struct *task); |
eb95419b TH |
626 | void (*exit)(struct cgroup_subsys_state *css, |
627 | struct cgroup_subsys_state *old_css, | |
761b3ef5 | 628 | struct task_struct *task); |
eb95419b | 629 | void (*bind)(struct cgroup_subsys_state *root_css); |
e5991371 | 630 | |
8bab8dde | 631 | int disabled; |
ddbcc7e8 | 632 | int early_init; |
48ddbe19 | 633 | |
8c7f6edb TH |
634 | /* |
635 | * 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 | ||
073219e9 | 649 | /* the following two fields are initialized automtically during boot */ |
aec25020 | 650 | int id; |
ddbcc7e8 PM |
651 | #define MAX_CGROUP_TYPE_NAMELEN 32 |
652 | const char *name; | |
653 | ||
b85d2040 | 654 | /* link to parent, protected by cgroup_lock() */ |
3dd06ffa | 655 | struct cgroup_root *root; |
e6a1105b | 656 | |
15a4c835 TH |
657 | /* idr for css->id */ |
658 | struct idr css_idr; | |
659 | ||
0adb0704 TH |
660 | /* |
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; | |
8e3f6541 | 665 | |
de00ffa5 | 666 | /* base cftypes, automatically registered with subsys itself */ |
8e3f6541 | 667 | struct cftype *base_cftypes; |
ddbcc7e8 PM |
668 | }; |
669 | ||
073219e9 | 670 | #define SUBSYS(_x) extern struct cgroup_subsys _x ## _cgrp_subsys; |
ddbcc7e8 PM |
671 | #include <linux/cgroup_subsys.h> |
672 | #undef SUBSYS | |
673 | ||
14611e51 TH |
674 | /** |
675 | * 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. | |
dc61b1d6 | 686 | */ |
2219449a TH |
687 | #ifdef CONFIG_PROVE_RCU |
688 | extern struct mutex cgroup_mutex; | |
0e1d768f | 689 | extern struct rw_semaphore css_set_rwsem; |
14611e51 TH |
690 | #define task_css_set_check(task, __c) \ |
691 | rcu_dereference_check((task)->cgroups, \ | |
0e1d768f TH |
692 | lockdep_is_held(&cgroup_mutex) || \ |
693 | lockdep_is_held(&css_set_rwsem) || \ | |
694 | ((task)->flags & PF_EXITING) || (__c)) | |
2219449a | 695 | #else |
14611e51 TH |
696 | #define task_css_set_check(task, __c) \ |
697 | rcu_dereference((task)->cgroups) | |
2219449a | 698 | #endif |
dc61b1d6 | 699 | |
14611e51 | 700 | /** |
8af01f56 | 701 | * task_css_check - obtain css for (task, subsys) w/ extra access conds |
14611e51 TH |
702 | * @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 | */ | |
8af01f56 | 709 | #define task_css_check(task, subsys_id, __c) \ |
14611e51 TH |
710 | 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 | */ | |
718 | static inline struct css_set *task_css_set(struct task_struct *task) | |
719 | { | |
720 | return task_css_set_check(task, false); | |
721 | } | |
722 | ||
723 | /** | |
8af01f56 | 724 | * task_css - obtain css for (task, subsys) |
14611e51 TH |
725 | * @task: the target task |
726 | * @subsys_id: the target subsystem ID | |
727 | * | |
8af01f56 | 728 | * See task_css_check(). |
14611e51 | 729 | */ |
8af01f56 TH |
730 | static inline struct cgroup_subsys_state *task_css(struct task_struct *task, |
731 | int subsys_id) | |
ddbcc7e8 | 732 | { |
8af01f56 | 733 | return task_css_check(task, subsys_id, false); |
ddbcc7e8 PM |
734 | } |
735 | ||
5024ae29 TH |
736 | /** |
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 | */ | |
744 | static 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 | ||
8af01f56 TH |
750 | static inline struct cgroup *task_cgroup(struct task_struct *task, |
751 | int subsys_id) | |
ddbcc7e8 | 752 | { |
8af01f56 | 753 | return task_css(task, subsys_id)->cgroup; |
ddbcc7e8 PM |
754 | } |
755 | ||
492eb21b TH |
756 | struct cgroup_subsys_state *css_next_child(struct cgroup_subsys_state *pos, |
757 | struct cgroup_subsys_state *parent); | |
53fa5261 | 758 | |
1cb650b9 LZ |
759 | struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss); |
760 | ||
574bd9f7 | 761 | /** |
492eb21b TH |
762 | * 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 | |
574bd9f7 | 765 | * |
492eb21b TH |
766 | * 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 | |
92fb9748 | 768 | * ->css_offline() may show up during traversal and it's each subsystem's |
574bd9f7 TH |
769 | * responsibility to verify that each @pos is alive. |
770 | * | |
92fb9748 | 771 | * If a subsystem synchronizes against the parent in its ->css_online() and |
492eb21b | 772 | * before starting iterating, a css which finished ->css_online() is |
92fb9748 | 773 | * guaranteed to be visible in the future iterations. |
75501a6d TH |
774 | * |
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. | |
574bd9f7 | 778 | */ |
492eb21b TH |
779 | #define css_for_each_child(pos, parent) \ |
780 | for ((pos) = css_next_child(NULL, (parent)); (pos); \ | |
781 | (pos) = css_next_child((pos), (parent))) | |
574bd9f7 | 782 | |
492eb21b TH |
783 | struct cgroup_subsys_state * |
784 | css_next_descendant_pre(struct cgroup_subsys_state *pos, | |
785 | struct cgroup_subsys_state *css); | |
786 | ||
787 | struct cgroup_subsys_state * | |
788 | css_rightmost_descendant(struct cgroup_subsys_state *pos); | |
574bd9f7 TH |
789 | |
790 | /** | |
492eb21b TH |
791 | * 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 | |
574bd9f7 | 794 | * |
bd8815a6 TH |
795 | * 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 | |
492eb21b | 797 | * descendant css which hasn't finished ->css_online() or already has |
92fb9748 | 798 | * finished ->css_offline() may show up during traversal and it's each |
574bd9f7 TH |
799 | * subsystem's responsibility to verify that each @pos is alive. |
800 | * | |
92fb9748 TH |
801 | * If a subsystem synchronizes against the parent in its ->css_online() and |
802 | * before starting iterating, and synchronizes against @pos on each | |
492eb21b | 803 | * iteration, any descendant css which finished ->css_online() is |
574bd9f7 TH |
804 | * 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 | * | |
492eb21b | 809 | * my_online(@css) |
574bd9f7 | 810 | * { |
492eb21b TH |
811 | * Lock @css's parent and @css; |
812 | * Inherit state from the parent; | |
574bd9f7 TH |
813 | * Unlock both. |
814 | * } | |
815 | * | |
492eb21b | 816 | * my_update_state(@css) |
574bd9f7 | 817 | * { |
492eb21b | 818 | * css_for_each_descendant_pre(@pos, @css) { |
574bd9f7 | 819 | * Lock @pos; |
bd8815a6 TH |
820 | * if (@pos == @css) |
821 | * Update @css's state; | |
822 | * else | |
823 | * Verify @pos is alive and inherit state from its parent; | |
574bd9f7 TH |
824 | * 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 | |
492eb21b | 834 | * inheritance happens for any css after the latest update to its parent. |
574bd9f7 TH |
835 | * |
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 | |
92fb9748 | 840 | * synchronize ->css_online() and ->css_offline() against tree-walking |
574bd9f7 | 841 | * operations. |
75501a6d TH |
842 | * |
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. | |
574bd9f7 | 846 | */ |
492eb21b TH |
847 | #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))) | |
574bd9f7 | 850 | |
492eb21b TH |
851 | struct cgroup_subsys_state * |
852 | css_next_descendant_post(struct cgroup_subsys_state *pos, | |
853 | struct cgroup_subsys_state *css); | |
574bd9f7 TH |
854 | |
855 | /** | |
492eb21b TH |
856 | * 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 | |
574bd9f7 | 859 | * |
492eb21b | 860 | * Similar to css_for_each_descendant_pre() but performs post-order |
bd8815a6 TH |
861 | * 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. | |
574bd9f7 | 864 | */ |
492eb21b TH |
865 | #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))) | |
574bd9f7 | 868 | |
72ec7029 TH |
869 | /* A css_task_iter should be treated as an opaque object */ |
870 | struct css_task_iter { | |
3ebb2b6e TH |
871 | struct cgroup_subsys *ss; |
872 | ||
0f0a2b4f TH |
873 | 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; | |
817929ec PM |
879 | }; |
880 | ||
72ec7029 TH |
881 | void css_task_iter_start(struct cgroup_subsys_state *css, |
882 | struct css_task_iter *it); | |
883 | struct task_struct *css_task_iter_next(struct css_task_iter *it); | |
884 | void css_task_iter_end(struct css_task_iter *it); | |
e535837b | 885 | |
31583bb0 | 886 | int cgroup_attach_task_all(struct task_struct *from, struct task_struct *); |
8cc99345 | 887 | int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from); |
31583bb0 | 888 | |
ec903c0c TH |
889 | struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry, |
890 | struct cgroup_subsys *ss); | |
38460b48 | 891 | |
ddbcc7e8 PM |
892 | #else /* !CONFIG_CGROUPS */ |
893 | ||
894 | static inline int cgroup_init_early(void) { return 0; } | |
895 | static inline int cgroup_init(void) { return 0; } | |
b4f48b63 | 896 | static inline void cgroup_fork(struct task_struct *p) {} |
817929ec | 897 | static inline void cgroup_post_fork(struct task_struct *p) {} |
1ec41830 | 898 | static inline void cgroup_exit(struct task_struct *p) {} |
ddbcc7e8 | 899 | |
846c7bb0 BS |
900 | static inline int cgroupstats_build(struct cgroupstats *stats, |
901 | struct dentry *dentry) | |
902 | { | |
903 | return -EINVAL; | |
904 | } | |
ddbcc7e8 | 905 | |
d7926ee3 | 906 | /* No cgroups - nothing to do */ |
31583bb0 MT |
907 | static inline int cgroup_attach_task_all(struct task_struct *from, |
908 | struct task_struct *t) | |
909 | { | |
910 | return 0; | |
911 | } | |
d7926ee3 | 912 | |
ddbcc7e8 PM |
913 | #endif /* !CONFIG_CGROUPS */ |
914 | ||
915 | #endif /* _LINUX_CGROUP_H */ |