cgroup.h 18 KB

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