cgroup.h 18 KB

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