at v4.2 18 kB view raw
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> 12#include <linux/cpumask.h> 13#include <linux/nodemask.h> 14#include <linux/rculist.h> 15#include <linux/cgroupstats.h> 16#include <linux/rwsem.h> 17#include <linux/fs.h> 18#include <linux/seq_file.h> 19#include <linux/kernfs.h> 20 21#include <linux/cgroup-defs.h> 22 23#ifdef CONFIG_CGROUPS 24 25/* a css_task_iter should be treated as an opaque object */ 26struct css_task_iter { 27 struct cgroup_subsys *ss; 28 29 struct list_head *cset_pos; 30 struct list_head *cset_head; 31 32 struct list_head *task_pos; 33 struct list_head *tasks_head; 34 struct list_head *mg_tasks_head; 35}; 36 37extern struct cgroup_root cgrp_dfl_root; 38extern struct css_set init_css_set; 39 40#define SUBSYS(_x) extern struct cgroup_subsys _x ## _cgrp_subsys; 41#include <linux/cgroup_subsys.h> 42#undef SUBSYS 43 44bool css_has_online_children(struct cgroup_subsys_state *css); 45struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss); 46struct cgroup_subsys_state *cgroup_get_e_css(struct cgroup *cgroup, 47 struct cgroup_subsys *ss); 48struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry, 49 struct cgroup_subsys *ss); 50 51bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor); 52int cgroup_attach_task_all(struct task_struct *from, struct task_struct *); 53int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from); 54 55int cgroup_add_dfl_cftypes(struct cgroup_subsys *ss, struct cftype *cfts); 56int cgroup_add_legacy_cftypes(struct cgroup_subsys *ss, struct cftype *cfts); 57int cgroup_rm_cftypes(struct cftype *cfts); 58 59char *task_cgroup_path(struct task_struct *task, char *buf, size_t buflen); 60int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry); 61int proc_cgroup_show(struct seq_file *m, struct pid_namespace *ns, 62 struct pid *pid, struct task_struct *tsk); 63 64void cgroup_fork(struct task_struct *p); 65void cgroup_post_fork(struct task_struct *p); 66void cgroup_exit(struct task_struct *p); 67 68int cgroup_init_early(void); 69int cgroup_init(void); 70 71/* 72 * Iteration helpers and macros. 73 */ 74 75struct cgroup_subsys_state *css_next_child(struct cgroup_subsys_state *pos, 76 struct cgroup_subsys_state *parent); 77struct cgroup_subsys_state *css_next_descendant_pre(struct cgroup_subsys_state *pos, 78 struct cgroup_subsys_state *css); 79struct cgroup_subsys_state *css_rightmost_descendant(struct cgroup_subsys_state *pos); 80struct cgroup_subsys_state *css_next_descendant_post(struct cgroup_subsys_state *pos, 81 struct cgroup_subsys_state *css); 82 83struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset); 84struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset); 85 86void css_task_iter_start(struct cgroup_subsys_state *css, 87 struct css_task_iter *it); 88struct task_struct *css_task_iter_next(struct css_task_iter *it); 89void css_task_iter_end(struct css_task_iter *it); 90 91/** 92 * css_for_each_child - iterate through children of a css 93 * @pos: the css * to use as the loop cursor 94 * @parent: css whose children to walk 95 * 96 * Walk @parent's children. Must be called under rcu_read_lock(). 97 * 98 * If a subsystem synchronizes ->css_online() and the start of iteration, a 99 * css which finished ->css_online() is guaranteed to be visible in the 100 * future iterations and will stay visible until the last reference is put. 101 * A css which hasn't finished ->css_online() or already finished 102 * ->css_offline() may show up during traversal. It's each subsystem's 103 * responsibility to synchronize against on/offlining. 104 * 105 * It is allowed to temporarily drop RCU read lock during iteration. The 106 * caller is responsible for ensuring that @pos remains accessible until 107 * the start of the next iteration by, for example, bumping the css refcnt. 108 */ 109#define css_for_each_child(pos, parent) \ 110 for ((pos) = css_next_child(NULL, (parent)); (pos); \ 111 (pos) = css_next_child((pos), (parent))) 112 113/** 114 * css_for_each_descendant_pre - pre-order walk of a css's descendants 115 * @pos: the css * to use as the loop cursor 116 * @root: css whose descendants to walk 117 * 118 * Walk @root's descendants. @root is included in the iteration and the 119 * first node to be visited. Must be called under rcu_read_lock(). 120 * 121 * If a subsystem synchronizes ->css_online() and the start of iteration, a 122 * css which finished ->css_online() is guaranteed to be visible in the 123 * future iterations and will stay visible until the last reference is put. 124 * A css which hasn't finished ->css_online() or already finished 125 * ->css_offline() may show up during traversal. It's each subsystem's 126 * responsibility to synchronize against on/offlining. 127 * 128 * For example, the following guarantees that a descendant can't escape 129 * state updates of its ancestors. 130 * 131 * my_online(@css) 132 * { 133 * Lock @css's parent and @css; 134 * Inherit state from the parent; 135 * Unlock both. 136 * } 137 * 138 * my_update_state(@css) 139 * { 140 * css_for_each_descendant_pre(@pos, @css) { 141 * Lock @pos; 142 * if (@pos == @css) 143 * Update @css's state; 144 * else 145 * Verify @pos is alive and inherit state from its parent; 146 * Unlock @pos; 147 * } 148 * } 149 * 150 * As long as the inheriting step, including checking the parent state, is 151 * enclosed inside @pos locking, double-locking the parent isn't necessary 152 * while inheriting. The state update to the parent is guaranteed to be 153 * visible by walking order and, as long as inheriting operations to the 154 * same @pos are atomic to each other, multiple updates racing each other 155 * still result in the correct state. It's guaranateed that at least one 156 * inheritance happens for any css after the latest update to its parent. 157 * 158 * If checking parent's state requires locking the parent, each inheriting 159 * iteration should lock and unlock both @pos->parent and @pos. 160 * 161 * Alternatively, a subsystem may choose to use a single global lock to 162 * synchronize ->css_online() and ->css_offline() against tree-walking 163 * operations. 164 * 165 * It is allowed to temporarily drop RCU read lock during iteration. The 166 * caller is responsible for ensuring that @pos remains accessible until 167 * the start of the next iteration by, for example, bumping the css refcnt. 168 */ 169#define css_for_each_descendant_pre(pos, css) \ 170 for ((pos) = css_next_descendant_pre(NULL, (css)); (pos); \ 171 (pos) = css_next_descendant_pre((pos), (css))) 172 173/** 174 * css_for_each_descendant_post - post-order walk of a css's descendants 175 * @pos: the css * to use as the loop cursor 176 * @css: css whose descendants to walk 177 * 178 * Similar to css_for_each_descendant_pre() but performs post-order 179 * traversal instead. @root is included in the iteration and the last 180 * node to be visited. 181 * 182 * If a subsystem synchronizes ->css_online() and the start of iteration, a 183 * css which finished ->css_online() is guaranteed to be visible in the 184 * future iterations and will stay visible until the last reference is put. 185 * A css which hasn't finished ->css_online() or already finished 186 * ->css_offline() may show up during traversal. It's each subsystem's 187 * responsibility to synchronize against on/offlining. 188 * 189 * Note that the walk visibility guarantee example described in pre-order 190 * walk doesn't apply the same to post-order walks. 191 */ 192#define css_for_each_descendant_post(pos, css) \ 193 for ((pos) = css_next_descendant_post(NULL, (css)); (pos); \ 194 (pos) = css_next_descendant_post((pos), (css))) 195 196/** 197 * cgroup_taskset_for_each - iterate cgroup_taskset 198 * @task: the loop cursor 199 * @tset: taskset to iterate 200 */ 201#define cgroup_taskset_for_each(task, tset) \ 202 for ((task) = cgroup_taskset_first((tset)); (task); \ 203 (task) = cgroup_taskset_next((tset))) 204 205/* 206 * Inline functions. 207 */ 208 209/** 210 * css_get - obtain a reference on the specified css 211 * @css: target css 212 * 213 * The caller must already have a reference. 214 */ 215static inline void css_get(struct cgroup_subsys_state *css) 216{ 217 if (!(css->flags & CSS_NO_REF)) 218 percpu_ref_get(&css->refcnt); 219} 220 221/** 222 * css_get_many - obtain references on the specified css 223 * @css: target css 224 * @n: number of references to get 225 * 226 * The caller must already have a reference. 227 */ 228static inline void css_get_many(struct cgroup_subsys_state *css, unsigned int n) 229{ 230 if (!(css->flags & CSS_NO_REF)) 231 percpu_ref_get_many(&css->refcnt, n); 232} 233 234/** 235 * css_tryget - try to obtain a reference on the specified css 236 * @css: target css 237 * 238 * Obtain a reference on @css unless it already has reached zero and is 239 * being released. This function doesn't care whether @css is on or 240 * offline. The caller naturally needs to ensure that @css is accessible 241 * but doesn't have to be holding a reference on it - IOW, RCU protected 242 * access is good enough for this function. Returns %true if a reference 243 * count was successfully obtained; %false otherwise. 244 */ 245static inline bool css_tryget(struct cgroup_subsys_state *css) 246{ 247 if (!(css->flags & CSS_NO_REF)) 248 return percpu_ref_tryget(&css->refcnt); 249 return true; 250} 251 252/** 253 * css_tryget_online - try to obtain a reference on the specified css if online 254 * @css: target css 255 * 256 * Obtain a reference on @css if it's online. The caller naturally needs 257 * to ensure that @css is accessible but doesn't have to be holding a 258 * reference on it - IOW, RCU protected access is good enough for this 259 * function. Returns %true if a reference count was successfully obtained; 260 * %false otherwise. 261 */ 262static inline bool css_tryget_online(struct cgroup_subsys_state *css) 263{ 264 if (!(css->flags & CSS_NO_REF)) 265 return percpu_ref_tryget_live(&css->refcnt); 266 return true; 267} 268 269/** 270 * css_put - put a css reference 271 * @css: target css 272 * 273 * Put a reference obtained via css_get() and css_tryget_online(). 274 */ 275static inline void css_put(struct cgroup_subsys_state *css) 276{ 277 if (!(css->flags & CSS_NO_REF)) 278 percpu_ref_put(&css->refcnt); 279} 280 281/** 282 * css_put_many - put css references 283 * @css: target css 284 * @n: number of references to put 285 * 286 * Put references obtained via css_get() and css_tryget_online(). 287 */ 288static inline void css_put_many(struct cgroup_subsys_state *css, unsigned int n) 289{ 290 if (!(css->flags & CSS_NO_REF)) 291 percpu_ref_put_many(&css->refcnt, n); 292} 293 294/** 295 * task_css_set_check - obtain a task's css_set with extra access conditions 296 * @task: the task to obtain css_set for 297 * @__c: extra condition expression to be passed to rcu_dereference_check() 298 * 299 * A task's css_set is RCU protected, initialized and exited while holding 300 * task_lock(), and can only be modified while holding both cgroup_mutex 301 * and task_lock() while the task is alive. This macro verifies that the 302 * caller is inside proper critical section and returns @task's css_set. 303 * 304 * The caller can also specify additional allowed conditions via @__c, such 305 * as locks used during the cgroup_subsys::attach() methods. 306 */ 307#ifdef CONFIG_PROVE_RCU 308extern struct mutex cgroup_mutex; 309extern struct rw_semaphore css_set_rwsem; 310#define task_css_set_check(task, __c) \ 311 rcu_dereference_check((task)->cgroups, \ 312 lockdep_is_held(&cgroup_mutex) || \ 313 lockdep_is_held(&css_set_rwsem) || \ 314 ((task)->flags & PF_EXITING) || (__c)) 315#else 316#define task_css_set_check(task, __c) \ 317 rcu_dereference((task)->cgroups) 318#endif 319 320/** 321 * task_css_check - obtain css for (task, subsys) w/ extra access conds 322 * @task: the target task 323 * @subsys_id: the target subsystem ID 324 * @__c: extra condition expression to be passed to rcu_dereference_check() 325 * 326 * Return the cgroup_subsys_state for the (@task, @subsys_id) pair. The 327 * synchronization rules are the same as task_css_set_check(). 328 */ 329#define task_css_check(task, subsys_id, __c) \ 330 task_css_set_check((task), (__c))->subsys[(subsys_id)] 331 332/** 333 * task_css_set - obtain a task's css_set 334 * @task: the task to obtain css_set for 335 * 336 * See task_css_set_check(). 337 */ 338static inline struct css_set *task_css_set(struct task_struct *task) 339{ 340 return task_css_set_check(task, false); 341} 342 343/** 344 * task_css - obtain css for (task, subsys) 345 * @task: the target task 346 * @subsys_id: the target subsystem ID 347 * 348 * See task_css_check(). 349 */ 350static inline struct cgroup_subsys_state *task_css(struct task_struct *task, 351 int subsys_id) 352{ 353 return task_css_check(task, subsys_id, false); 354} 355 356/** 357 * task_get_css - find and get the css for (task, subsys) 358 * @task: the target task 359 * @subsys_id: the target subsystem ID 360 * 361 * Find the css for the (@task, @subsys_id) combination, increment a 362 * reference on and return it. This function is guaranteed to return a 363 * valid css. 364 */ 365static inline struct cgroup_subsys_state * 366task_get_css(struct task_struct *task, int subsys_id) 367{ 368 struct cgroup_subsys_state *css; 369 370 rcu_read_lock(); 371 while (true) { 372 css = task_css(task, subsys_id); 373 if (likely(css_tryget_online(css))) 374 break; 375 cpu_relax(); 376 } 377 rcu_read_unlock(); 378 return css; 379} 380 381/** 382 * task_css_is_root - test whether a task belongs to the root css 383 * @task: the target task 384 * @subsys_id: the target subsystem ID 385 * 386 * Test whether @task belongs to the root css on the specified subsystem. 387 * May be invoked in any context. 388 */ 389static inline bool task_css_is_root(struct task_struct *task, int subsys_id) 390{ 391 return task_css_check(task, subsys_id, true) == 392 init_css_set.subsys[subsys_id]; 393} 394 395static inline struct cgroup *task_cgroup(struct task_struct *task, 396 int subsys_id) 397{ 398 return task_css(task, subsys_id)->cgroup; 399} 400 401/** 402 * cgroup_on_dfl - test whether a cgroup is on the default hierarchy 403 * @cgrp: the cgroup of interest 404 * 405 * The default hierarchy is the v2 interface of cgroup and this function 406 * can be used to test whether a cgroup is on the default hierarchy for 407 * cases where a subsystem should behave differnetly depending on the 408 * interface version. 409 * 410 * The set of behaviors which change on the default hierarchy are still 411 * being determined and the mount option is prefixed with __DEVEL__. 412 * 413 * List of changed behaviors: 414 * 415 * - Mount options "noprefix", "xattr", "clone_children", "release_agent" 416 * and "name" are disallowed. 417 * 418 * - When mounting an existing superblock, mount options should match. 419 * 420 * - Remount is disallowed. 421 * 422 * - rename(2) is disallowed. 423 * 424 * - "tasks" is removed. Everything should be at process granularity. Use 425 * "cgroup.procs" instead. 426 * 427 * - "cgroup.procs" is not sorted. pids will be unique unless they got 428 * recycled inbetween reads. 429 * 430 * - "release_agent" and "notify_on_release" are removed. Replacement 431 * notification mechanism will be implemented. 432 * 433 * - "cgroup.clone_children" is removed. 434 * 435 * - "cgroup.subtree_populated" is available. Its value is 0 if the cgroup 436 * and its descendants contain no task; otherwise, 1. The file also 437 * generates kernfs notification which can be monitored through poll and 438 * [di]notify when the value of the file changes. 439 * 440 * - cpuset: tasks will be kept in empty cpusets when hotplug happens and 441 * take masks of ancestors with non-empty cpus/mems, instead of being 442 * moved to an ancestor. 443 * 444 * - cpuset: a task can be moved into an empty cpuset, and again it takes 445 * masks of ancestors. 446 * 447 * - memcg: use_hierarchy is on by default and the cgroup file for the flag 448 * is not created. 449 * 450 * - blkcg: blk-throttle becomes properly hierarchical. 451 * 452 * - debug: disallowed on the default hierarchy. 453 */ 454static inline bool cgroup_on_dfl(const struct cgroup *cgrp) 455{ 456 return cgrp->root == &cgrp_dfl_root; 457} 458 459/* no synchronization, the result can only be used as a hint */ 460static inline bool cgroup_has_tasks(struct cgroup *cgrp) 461{ 462 return !list_empty(&cgrp->cset_links); 463} 464 465/* returns ino associated with a cgroup */ 466static inline ino_t cgroup_ino(struct cgroup *cgrp) 467{ 468 return cgrp->kn->ino; 469} 470 471/* cft/css accessors for cftype->write() operation */ 472static inline struct cftype *of_cft(struct kernfs_open_file *of) 473{ 474 return of->kn->priv; 475} 476 477struct cgroup_subsys_state *of_css(struct kernfs_open_file *of); 478 479/* cft/css accessors for cftype->seq_*() operations */ 480static inline struct cftype *seq_cft(struct seq_file *seq) 481{ 482 return of_cft(seq->private); 483} 484 485static inline struct cgroup_subsys_state *seq_css(struct seq_file *seq) 486{ 487 return of_css(seq->private); 488} 489 490/* 491 * Name / path handling functions. All are thin wrappers around the kernfs 492 * counterparts and can be called under any context. 493 */ 494 495static inline int cgroup_name(struct cgroup *cgrp, char *buf, size_t buflen) 496{ 497 return kernfs_name(cgrp->kn, buf, buflen); 498} 499 500static inline char * __must_check cgroup_path(struct cgroup *cgrp, char *buf, 501 size_t buflen) 502{ 503 return kernfs_path(cgrp->kn, buf, buflen); 504} 505 506static inline void pr_cont_cgroup_name(struct cgroup *cgrp) 507{ 508 pr_cont_kernfs_name(cgrp->kn); 509} 510 511static inline void pr_cont_cgroup_path(struct cgroup *cgrp) 512{ 513 pr_cont_kernfs_path(cgrp->kn); 514} 515 516#else /* !CONFIG_CGROUPS */ 517 518struct cgroup_subsys_state; 519 520static inline void css_put(struct cgroup_subsys_state *css) {} 521static inline int cgroup_attach_task_all(struct task_struct *from, 522 struct task_struct *t) { return 0; } 523static inline int cgroupstats_build(struct cgroupstats *stats, 524 struct dentry *dentry) { return -EINVAL; } 525 526static inline void cgroup_fork(struct task_struct *p) {} 527static inline void cgroup_post_fork(struct task_struct *p) {} 528static inline void cgroup_exit(struct task_struct *p) {} 529 530static inline int cgroup_init_early(void) { return 0; } 531static inline int cgroup_init(void) { return 0; } 532 533#endif /* !CONFIG_CGROUPS */ 534 535#endif /* _LINUX_CGROUP_H */