cgroup.h 18 KB

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