cgroup.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_CGROUP_H
  3. #define _LINUX_CGROUP_H
  4. /*
  5. * cgroup interface
  6. *
  7. * Copyright (C) 2003 BULL SA
  8. * Copyright (C) 2004-2006 Silicon Graphics, Inc.
  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/fs.h>
  17. #include <linux/seq_file.h>
  18. #include <linux/kernfs.h>
  19. #include <linux/jump_label.h>
  20. #include <linux/types.h>
  21. #include <linux/ns_common.h>
  22. #include <linux/nsproxy.h>
  23. #include <linux/user_namespace.h>
  24. #include <linux/refcount.h>
  25. #include <linux/kernel_stat.h>
  26. #include <linux/android_kabi.h>
  27. #include <linux/cgroup-defs.h>
  28. struct kernel_clone_args;
  29. #ifdef CONFIG_CGROUPS
  30. /*
  31. * All weight knobs on the default hierarhcy should use the following min,
  32. * default and max values. The default value is the logarithmic center of
  33. * MIN and MAX and allows 100x to be expressed in both directions.
  34. */
  35. #define CGROUP_WEIGHT_MIN 1
  36. #define CGROUP_WEIGHT_DFL 100
  37. #define CGROUP_WEIGHT_MAX 10000
  38. /* walk only threadgroup leaders */
  39. #define CSS_TASK_ITER_PROCS (1U << 0)
  40. /* walk all threaded css_sets in the domain */
  41. #define CSS_TASK_ITER_THREADED (1U << 1)
  42. /* internal flags */
  43. #define CSS_TASK_ITER_SKIPPED (1U << 16)
  44. /* a css_task_iter should be treated as an opaque object */
  45. struct css_task_iter {
  46. struct cgroup_subsys *ss;
  47. unsigned int flags;
  48. struct list_head *cset_pos;
  49. struct list_head *cset_head;
  50. struct list_head *tcset_pos;
  51. struct list_head *tcset_head;
  52. struct list_head *task_pos;
  53. struct list_head *cur_tasks_head;
  54. struct css_set *cur_cset;
  55. struct css_set *cur_dcset;
  56. struct task_struct *cur_task;
  57. struct list_head iters_node; /* css_set->task_iters */
  58. ANDROID_KABI_RESERVE(1);
  59. };
  60. extern struct cgroup_root cgrp_dfl_root;
  61. extern struct css_set init_css_set;
  62. #define SUBSYS(_x) extern struct cgroup_subsys _x ## _cgrp_subsys;
  63. #include <linux/cgroup_subsys.h>
  64. #undef SUBSYS
  65. #define SUBSYS(_x) \
  66. extern struct static_key_true _x ## _cgrp_subsys_enabled_key; \
  67. extern struct static_key_true _x ## _cgrp_subsys_on_dfl_key;
  68. #include <linux/cgroup_subsys.h>
  69. #undef SUBSYS
  70. /**
  71. * cgroup_subsys_enabled - fast test on whether a subsys is enabled
  72. * @ss: subsystem in question
  73. */
  74. #define cgroup_subsys_enabled(ss) \
  75. static_branch_likely(&ss ## _enabled_key)
  76. /**
  77. * cgroup_subsys_on_dfl - fast test on whether a subsys is on default hierarchy
  78. * @ss: subsystem in question
  79. */
  80. #define cgroup_subsys_on_dfl(ss) \
  81. static_branch_likely(&ss ## _on_dfl_key)
  82. bool css_has_online_children(struct cgroup_subsys_state *css);
  83. struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss);
  84. struct cgroup_subsys_state *cgroup_e_css(struct cgroup *cgroup,
  85. struct cgroup_subsys *ss);
  86. struct cgroup_subsys_state *cgroup_get_e_css(struct cgroup *cgroup,
  87. struct cgroup_subsys *ss);
  88. struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry,
  89. struct cgroup_subsys *ss);
  90. struct cgroup *cgroup_get_from_path(const char *path);
  91. struct cgroup *cgroup_get_from_fd(int fd);
  92. int cgroup_attach_task_all(struct task_struct *from, struct task_struct *);
  93. int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from);
  94. int cgroup_add_dfl_cftypes(struct cgroup_subsys *ss, struct cftype *cfts);
  95. int cgroup_add_legacy_cftypes(struct cgroup_subsys *ss, struct cftype *cfts);
  96. int cgroup_rm_cftypes(struct cftype *cfts);
  97. void cgroup_file_notify(struct cgroup_file *cfile);
  98. int task_cgroup_path(struct task_struct *task, char *buf, size_t buflen);
  99. int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry);
  100. int proc_cgroup_show(struct seq_file *m, struct pid_namespace *ns,
  101. struct pid *pid, struct task_struct *tsk);
  102. void cgroup_fork(struct task_struct *p);
  103. extern int cgroup_can_fork(struct task_struct *p,
  104. struct kernel_clone_args *kargs);
  105. extern void cgroup_cancel_fork(struct task_struct *p,
  106. struct kernel_clone_args *kargs);
  107. extern void cgroup_post_fork(struct task_struct *p,
  108. struct kernel_clone_args *kargs);
  109. void cgroup_exit(struct task_struct *p);
  110. void cgroup_release(struct task_struct *p);
  111. void cgroup_free(struct task_struct *p);
  112. int cgroup_init_early(void);
  113. int cgroup_init(void);
  114. int cgroup_parse_float(const char *input, unsigned dec_shift, s64 *v);
  115. /*
  116. * Iteration helpers and macros.
  117. */
  118. struct cgroup_subsys_state *css_next_child(struct cgroup_subsys_state *pos,
  119. struct cgroup_subsys_state *parent);
  120. struct cgroup_subsys_state *css_next_descendant_pre(struct cgroup_subsys_state *pos,
  121. struct cgroup_subsys_state *css);
  122. struct cgroup_subsys_state *css_rightmost_descendant(struct cgroup_subsys_state *pos);
  123. struct cgroup_subsys_state *css_next_descendant_post(struct cgroup_subsys_state *pos,
  124. struct cgroup_subsys_state *css);
  125. struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset,
  126. struct cgroup_subsys_state **dst_cssp);
  127. struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset,
  128. struct cgroup_subsys_state **dst_cssp);
  129. void css_task_iter_start(struct cgroup_subsys_state *css, unsigned int flags,
  130. struct css_task_iter *it);
  131. struct task_struct *css_task_iter_next(struct css_task_iter *it);
  132. void css_task_iter_end(struct css_task_iter *it);
  133. /**
  134. * css_for_each_child - iterate through children of a css
  135. * @pos: the css * to use as the loop cursor
  136. * @parent: css whose children to walk
  137. *
  138. * Walk @parent's children. Must be called under rcu_read_lock().
  139. *
  140. * If a subsystem synchronizes ->css_online() and the start of iteration, a
  141. * css which finished ->css_online() is guaranteed to be visible in the
  142. * future iterations and will stay visible until the last reference is put.
  143. * A css which hasn't finished ->css_online() or already finished
  144. * ->css_offline() may show up during traversal. It's each subsystem's
  145. * responsibility to synchronize against on/offlining.
  146. *
  147. * It is allowed to temporarily drop RCU read lock during iteration. The
  148. * caller is responsible for ensuring that @pos remains accessible until
  149. * the start of the next iteration by, for example, bumping the css refcnt.
  150. */
  151. #define css_for_each_child(pos, parent) \
  152. for ((pos) = css_next_child(NULL, (parent)); (pos); \
  153. (pos) = css_next_child((pos), (parent)))
  154. /**
  155. * css_for_each_descendant_pre - pre-order walk of a css's descendants
  156. * @pos: the css * to use as the loop cursor
  157. * @root: css whose descendants to walk
  158. *
  159. * Walk @root's descendants. @root is included in the iteration and the
  160. * first node to be visited. Must be called under rcu_read_lock().
  161. *
  162. * If a subsystem synchronizes ->css_online() and the start of iteration, a
  163. * css which finished ->css_online() is guaranteed to be visible in the
  164. * future iterations and will stay visible until the last reference is put.
  165. * A css which hasn't finished ->css_online() or already finished
  166. * ->css_offline() may show up during traversal. It's each subsystem's
  167. * responsibility to synchronize against on/offlining.
  168. *
  169. * For example, the following guarantees that a descendant can't escape
  170. * state updates of its ancestors.
  171. *
  172. * my_online(@css)
  173. * {
  174. * Lock @css's parent and @css;
  175. * Inherit state from the parent;
  176. * Unlock both.
  177. * }
  178. *
  179. * my_update_state(@css)
  180. * {
  181. * css_for_each_descendant_pre(@pos, @css) {
  182. * Lock @pos;
  183. * if (@pos == @css)
  184. * Update @css's state;
  185. * else
  186. * Verify @pos is alive and inherit state from its parent;
  187. * Unlock @pos;
  188. * }
  189. * }
  190. *
  191. * As long as the inheriting step, including checking the parent state, is
  192. * enclosed inside @pos locking, double-locking the parent isn't necessary
  193. * while inheriting. The state update to the parent is guaranteed to be
  194. * visible by walking order and, as long as inheriting operations to the
  195. * same @pos are atomic to each other, multiple updates racing each other
  196. * still result in the correct state. It's guaranateed that at least one
  197. * inheritance happens for any css after the latest update to its parent.
  198. *
  199. * If checking parent's state requires locking the parent, each inheriting
  200. * iteration should lock and unlock both @pos->parent and @pos.
  201. *
  202. * Alternatively, a subsystem may choose to use a single global lock to
  203. * synchronize ->css_online() and ->css_offline() against tree-walking
  204. * operations.
  205. *
  206. * It is allowed to temporarily drop RCU read lock during iteration. The
  207. * caller is responsible for ensuring that @pos remains accessible until
  208. * the start of the next iteration by, for example, bumping the css refcnt.
  209. */
  210. #define css_for_each_descendant_pre(pos, css) \
  211. for ((pos) = css_next_descendant_pre(NULL, (css)); (pos); \
  212. (pos) = css_next_descendant_pre((pos), (css)))
  213. /**
  214. * css_for_each_descendant_post - post-order walk of a css's descendants
  215. * @pos: the css * to use as the loop cursor
  216. * @css: css whose descendants to walk
  217. *
  218. * Similar to css_for_each_descendant_pre() but performs post-order
  219. * traversal instead. @root is included in the iteration and the last
  220. * node to be visited.
  221. *
  222. * If a subsystem synchronizes ->css_online() and the start of iteration, a
  223. * css which finished ->css_online() is guaranteed to be visible in the
  224. * future iterations and will stay visible until the last reference is put.
  225. * A css which hasn't finished ->css_online() or already finished
  226. * ->css_offline() may show up during traversal. It's each subsystem's
  227. * responsibility to synchronize against on/offlining.
  228. *
  229. * Note that the walk visibility guarantee example described in pre-order
  230. * walk doesn't apply the same to post-order walks.
  231. */
  232. #define css_for_each_descendant_post(pos, css) \
  233. for ((pos) = css_next_descendant_post(NULL, (css)); (pos); \
  234. (pos) = css_next_descendant_post((pos), (css)))
  235. /**
  236. * cgroup_taskset_for_each - iterate cgroup_taskset
  237. * @task: the loop cursor
  238. * @dst_css: the destination css
  239. * @tset: taskset to iterate
  240. *
  241. * @tset may contain multiple tasks and they may belong to multiple
  242. * processes.
  243. *
  244. * On the v2 hierarchy, there may be tasks from multiple processes and they
  245. * may not share the source or destination csses.
  246. *
  247. * On traditional hierarchies, when there are multiple tasks in @tset, if a
  248. * task of a process is in @tset, all tasks of the process are in @tset.
  249. * Also, all are guaranteed to share the same source and destination csses.
  250. *
  251. * Iteration is not in any specific order.
  252. */
  253. #define cgroup_taskset_for_each(task, dst_css, tset) \
  254. for ((task) = cgroup_taskset_first((tset), &(dst_css)); \
  255. (task); \
  256. (task) = cgroup_taskset_next((tset), &(dst_css)))
  257. /**
  258. * cgroup_taskset_for_each_leader - iterate group leaders in a cgroup_taskset
  259. * @leader: the loop cursor
  260. * @dst_css: the destination css
  261. * @tset: taskset to iterate
  262. *
  263. * Iterate threadgroup leaders of @tset. For single-task migrations, @tset
  264. * may not contain any.
  265. */
  266. #define cgroup_taskset_for_each_leader(leader, dst_css, tset) \
  267. for ((leader) = cgroup_taskset_first((tset), &(dst_css)); \
  268. (leader); \
  269. (leader) = cgroup_taskset_next((tset), &(dst_css))) \
  270. if ((leader) != (leader)->group_leader) \
  271. ; \
  272. else
  273. /*
  274. * Inline functions.
  275. */
  276. static inline u64 cgroup_id(struct cgroup *cgrp)
  277. {
  278. return cgrp->kn->id;
  279. }
  280. /**
  281. * css_get - obtain a reference on the specified css
  282. * @css: target css
  283. *
  284. * The caller must already have a reference.
  285. */
  286. static inline void css_get(struct cgroup_subsys_state *css)
  287. {
  288. if (!(css->flags & CSS_NO_REF))
  289. percpu_ref_get(&css->refcnt);
  290. }
  291. /**
  292. * css_get_many - obtain references on the specified css
  293. * @css: target css
  294. * @n: number of references to get
  295. *
  296. * The caller must already have a reference.
  297. */
  298. static inline void css_get_many(struct cgroup_subsys_state *css, unsigned int n)
  299. {
  300. if (!(css->flags & CSS_NO_REF))
  301. percpu_ref_get_many(&css->refcnt, n);
  302. }
  303. /**
  304. * css_tryget - try to obtain a reference on the specified css
  305. * @css: target css
  306. *
  307. * Obtain a reference on @css unless it already has reached zero and is
  308. * being released. This function doesn't care whether @css is on or
  309. * offline. The caller naturally needs to ensure that @css is accessible
  310. * but doesn't have to be holding a reference on it - IOW, RCU protected
  311. * access is good enough for this function. Returns %true if a reference
  312. * count was successfully obtained; %false otherwise.
  313. */
  314. static inline bool css_tryget(struct cgroup_subsys_state *css)
  315. {
  316. if (!(css->flags & CSS_NO_REF))
  317. return percpu_ref_tryget(&css->refcnt);
  318. return true;
  319. }
  320. /**
  321. * css_tryget_online - try to obtain a reference on the specified css if online
  322. * @css: target css
  323. *
  324. * Obtain a reference on @css if it's online. The caller naturally needs
  325. * to ensure that @css is accessible but doesn't have to be holding a
  326. * reference on it - IOW, RCU protected access is good enough for this
  327. * function. Returns %true if a reference count was successfully obtained;
  328. * %false otherwise.
  329. */
  330. static inline bool css_tryget_online(struct cgroup_subsys_state *css)
  331. {
  332. if (!(css->flags & CSS_NO_REF))
  333. return percpu_ref_tryget_live(&css->refcnt);
  334. return true;
  335. }
  336. /**
  337. * css_is_dying - test whether the specified css is dying
  338. * @css: target css
  339. *
  340. * Test whether @css is in the process of offlining or already offline. In
  341. * most cases, ->css_online() and ->css_offline() callbacks should be
  342. * enough; however, the actual offline operations are RCU delayed and this
  343. * test returns %true also when @css is scheduled to be offlined.
  344. *
  345. * This is useful, for example, when the use case requires synchronous
  346. * behavior with respect to cgroup removal. cgroup removal schedules css
  347. * offlining but the css can seem alive while the operation is being
  348. * delayed. If the delay affects user visible semantics, this test can be
  349. * used to resolve the situation.
  350. */
  351. static inline bool css_is_dying(struct cgroup_subsys_state *css)
  352. {
  353. return !(css->flags & CSS_NO_REF) && percpu_ref_is_dying(&css->refcnt);
  354. }
  355. /**
  356. * css_put - put a css reference
  357. * @css: target css
  358. *
  359. * Put a reference obtained via css_get() and css_tryget_online().
  360. */
  361. static inline void css_put(struct cgroup_subsys_state *css)
  362. {
  363. if (!(css->flags & CSS_NO_REF))
  364. percpu_ref_put(&css->refcnt);
  365. }
  366. /**
  367. * css_put_many - put css references
  368. * @css: target css
  369. * @n: number of references to put
  370. *
  371. * Put references obtained via css_get() and css_tryget_online().
  372. */
  373. static inline void css_put_many(struct cgroup_subsys_state *css, unsigned int n)
  374. {
  375. if (!(css->flags & CSS_NO_REF))
  376. percpu_ref_put_many(&css->refcnt, n);
  377. }
  378. static inline void cgroup_get(struct cgroup *cgrp)
  379. {
  380. css_get(&cgrp->self);
  381. }
  382. static inline bool cgroup_tryget(struct cgroup *cgrp)
  383. {
  384. return css_tryget(&cgrp->self);
  385. }
  386. static inline void cgroup_put(struct cgroup *cgrp)
  387. {
  388. css_put(&cgrp->self);
  389. }
  390. /**
  391. * task_css_set_check - obtain a task's css_set with extra access conditions
  392. * @task: the task to obtain css_set for
  393. * @__c: extra condition expression to be passed to rcu_dereference_check()
  394. *
  395. * A task's css_set is RCU protected, initialized and exited while holding
  396. * task_lock(), and can only be modified while holding both cgroup_mutex
  397. * and task_lock() while the task is alive. This macro verifies that the
  398. * caller is inside proper critical section and returns @task's css_set.
  399. *
  400. * The caller can also specify additional allowed conditions via @__c, such
  401. * as locks used during the cgroup_subsys::attach() methods.
  402. */
  403. #ifdef CONFIG_PROVE_RCU
  404. extern struct mutex cgroup_mutex;
  405. extern spinlock_t css_set_lock;
  406. #define task_css_set_check(task, __c) \
  407. rcu_dereference_check((task)->cgroups, \
  408. lockdep_is_held(&cgroup_mutex) || \
  409. lockdep_is_held(&css_set_lock) || \
  410. ((task)->flags & PF_EXITING) || (__c))
  411. #else
  412. #define task_css_set_check(task, __c) \
  413. rcu_dereference((task)->cgroups)
  414. #endif
  415. /**
  416. * task_css_check - obtain css for (task, subsys) w/ extra access conds
  417. * @task: the target task
  418. * @subsys_id: the target subsystem ID
  419. * @__c: extra condition expression to be passed to rcu_dereference_check()
  420. *
  421. * Return the cgroup_subsys_state for the (@task, @subsys_id) pair. The
  422. * synchronization rules are the same as task_css_set_check().
  423. */
  424. #define task_css_check(task, subsys_id, __c) \
  425. task_css_set_check((task), (__c))->subsys[(subsys_id)]
  426. /**
  427. * task_css_set - obtain a task's css_set
  428. * @task: the task to obtain css_set for
  429. *
  430. * See task_css_set_check().
  431. */
  432. static inline struct css_set *task_css_set(struct task_struct *task)
  433. {
  434. return task_css_set_check(task, false);
  435. }
  436. /**
  437. * task_css - obtain css for (task, subsys)
  438. * @task: the target task
  439. * @subsys_id: the target subsystem ID
  440. *
  441. * See task_css_check().
  442. */
  443. static inline struct cgroup_subsys_state *task_css(struct task_struct *task,
  444. int subsys_id)
  445. {
  446. return task_css_check(task, subsys_id, false);
  447. }
  448. /**
  449. * task_get_css - find and get the css for (task, subsys)
  450. * @task: the target task
  451. * @subsys_id: the target subsystem ID
  452. *
  453. * Find the css for the (@task, @subsys_id) combination, increment a
  454. * reference on and return it. This function is guaranteed to return a
  455. * valid css. The returned css may already have been offlined.
  456. */
  457. static inline struct cgroup_subsys_state *
  458. task_get_css(struct task_struct *task, int subsys_id)
  459. {
  460. struct cgroup_subsys_state *css;
  461. rcu_read_lock();
  462. while (true) {
  463. css = task_css(task, subsys_id);
  464. /*
  465. * Can't use css_tryget_online() here. A task which has
  466. * PF_EXITING set may stay associated with an offline css.
  467. * If such task calls this function, css_tryget_online()
  468. * will keep failing.
  469. */
  470. if (likely(css_tryget(css)))
  471. break;
  472. cpu_relax();
  473. }
  474. rcu_read_unlock();
  475. return css;
  476. }
  477. /**
  478. * task_css_is_root - test whether a task belongs to the root css
  479. * @task: the target task
  480. * @subsys_id: the target subsystem ID
  481. *
  482. * Test whether @task belongs to the root css on the specified subsystem.
  483. * May be invoked in any context.
  484. */
  485. static inline bool task_css_is_root(struct task_struct *task, int subsys_id)
  486. {
  487. return task_css_check(task, subsys_id, true) ==
  488. init_css_set.subsys[subsys_id];
  489. }
  490. static inline struct cgroup *task_cgroup(struct task_struct *task,
  491. int subsys_id)
  492. {
  493. return task_css(task, subsys_id)->cgroup;
  494. }
  495. static inline struct cgroup *task_dfl_cgroup(struct task_struct *task)
  496. {
  497. return task_css_set(task)->dfl_cgrp;
  498. }
  499. static inline struct cgroup *cgroup_parent(struct cgroup *cgrp)
  500. {
  501. struct cgroup_subsys_state *parent_css = cgrp->self.parent;
  502. if (parent_css)
  503. return container_of(parent_css, struct cgroup, self);
  504. return NULL;
  505. }
  506. /**
  507. * cgroup_is_descendant - test ancestry
  508. * @cgrp: the cgroup to be tested
  509. * @ancestor: possible ancestor of @cgrp
  510. *
  511. * Test whether @cgrp is a descendant of @ancestor. It also returns %true
  512. * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
  513. * and @ancestor are accessible.
  514. */
  515. static inline bool cgroup_is_descendant(struct cgroup *cgrp,
  516. struct cgroup *ancestor)
  517. {
  518. if (cgrp->root != ancestor->root || cgrp->level < ancestor->level)
  519. return false;
  520. return cgrp->ancestor_ids[ancestor->level] == cgroup_id(ancestor);
  521. }
  522. /**
  523. * cgroup_ancestor - find ancestor of cgroup
  524. * @cgrp: cgroup to find ancestor of
  525. * @ancestor_level: level of ancestor to find starting from root
  526. *
  527. * Find ancestor of cgroup at specified level starting from root if it exists
  528. * and return pointer to it. Return NULL if @cgrp doesn't have ancestor at
  529. * @ancestor_level.
  530. *
  531. * This function is safe to call as long as @cgrp is accessible.
  532. */
  533. static inline struct cgroup *cgroup_ancestor(struct cgroup *cgrp,
  534. int ancestor_level)
  535. {
  536. if (cgrp->level < ancestor_level)
  537. return NULL;
  538. while (cgrp && cgrp->level > ancestor_level)
  539. cgrp = cgroup_parent(cgrp);
  540. return cgrp;
  541. }
  542. /**
  543. * task_under_cgroup_hierarchy - test task's membership of cgroup ancestry
  544. * @task: the task to be tested
  545. * @ancestor: possible ancestor of @task's cgroup
  546. *
  547. * Tests whether @task's default cgroup hierarchy is a descendant of @ancestor.
  548. * It follows all the same rules as cgroup_is_descendant, and only applies
  549. * to the default hierarchy.
  550. */
  551. static inline bool task_under_cgroup_hierarchy(struct task_struct *task,
  552. struct cgroup *ancestor)
  553. {
  554. struct css_set *cset = task_css_set(task);
  555. return cgroup_is_descendant(cset->dfl_cgrp, ancestor);
  556. }
  557. /* no synchronization, the result can only be used as a hint */
  558. static inline bool cgroup_is_populated(struct cgroup *cgrp)
  559. {
  560. return cgrp->nr_populated_csets + cgrp->nr_populated_domain_children +
  561. cgrp->nr_populated_threaded_children;
  562. }
  563. /* returns ino associated with a cgroup */
  564. static inline ino_t cgroup_ino(struct cgroup *cgrp)
  565. {
  566. return kernfs_ino(cgrp->kn);
  567. }
  568. /* cft/css accessors for cftype->write() operation */
  569. static inline struct cftype *of_cft(struct kernfs_open_file *of)
  570. {
  571. return of->kn->priv;
  572. }
  573. struct cgroup_subsys_state *of_css(struct kernfs_open_file *of);
  574. /* cft/css accessors for cftype->seq_*() operations */
  575. static inline struct cftype *seq_cft(struct seq_file *seq)
  576. {
  577. return of_cft(seq->private);
  578. }
  579. static inline struct cgroup_subsys_state *seq_css(struct seq_file *seq)
  580. {
  581. return of_css(seq->private);
  582. }
  583. /*
  584. * Name / path handling functions. All are thin wrappers around the kernfs
  585. * counterparts and can be called under any context.
  586. */
  587. static inline int cgroup_name(struct cgroup *cgrp, char *buf, size_t buflen)
  588. {
  589. return kernfs_name(cgrp->kn, buf, buflen);
  590. }
  591. static inline int cgroup_path(struct cgroup *cgrp, char *buf, size_t buflen)
  592. {
  593. return kernfs_path(cgrp->kn, buf, buflen);
  594. }
  595. static inline void pr_cont_cgroup_name(struct cgroup *cgrp)
  596. {
  597. pr_cont_kernfs_name(cgrp->kn);
  598. }
  599. static inline void pr_cont_cgroup_path(struct cgroup *cgrp)
  600. {
  601. pr_cont_kernfs_path(cgrp->kn);
  602. }
  603. static inline struct psi_group *cgroup_psi(struct cgroup *cgrp)
  604. {
  605. return &cgrp->psi;
  606. }
  607. bool cgroup_psi_enabled(void);
  608. static inline void cgroup_init_kthreadd(void)
  609. {
  610. /*
  611. * kthreadd is inherited by all kthreads, keep it in the root so
  612. * that the new kthreads are guaranteed to stay in the root until
  613. * initialization is finished.
  614. */
  615. current->no_cgroup_migration = 1;
  616. }
  617. static inline void cgroup_kthread_ready(void)
  618. {
  619. /*
  620. * This kthread finished initialization. The creator should have
  621. * set PF_NO_SETAFFINITY if this kthread should stay in the root.
  622. */
  623. current->no_cgroup_migration = 0;
  624. }
  625. void cgroup_path_from_kernfs_id(u64 id, char *buf, size_t buflen);
  626. #else /* !CONFIG_CGROUPS */
  627. struct cgroup_subsys_state;
  628. struct cgroup;
  629. static inline u64 cgroup_id(struct cgroup *cgrp) { return 1; }
  630. static inline void css_get(struct cgroup_subsys_state *css) {}
  631. static inline void css_put(struct cgroup_subsys_state *css) {}
  632. static inline int cgroup_attach_task_all(struct task_struct *from,
  633. struct task_struct *t) { return 0; }
  634. static inline int cgroupstats_build(struct cgroupstats *stats,
  635. struct dentry *dentry) { return -EINVAL; }
  636. static inline void cgroup_fork(struct task_struct *p) {}
  637. static inline int cgroup_can_fork(struct task_struct *p,
  638. struct kernel_clone_args *kargs) { return 0; }
  639. static inline void cgroup_cancel_fork(struct task_struct *p,
  640. struct kernel_clone_args *kargs) {}
  641. static inline void cgroup_post_fork(struct task_struct *p,
  642. struct kernel_clone_args *kargs) {}
  643. static inline void cgroup_exit(struct task_struct *p) {}
  644. static inline void cgroup_release(struct task_struct *p) {}
  645. static inline void cgroup_free(struct task_struct *p) {}
  646. static inline int cgroup_init_early(void) { return 0; }
  647. static inline int cgroup_init(void) { return 0; }
  648. static inline void cgroup_init_kthreadd(void) {}
  649. static inline void cgroup_kthread_ready(void) {}
  650. static inline struct cgroup *cgroup_parent(struct cgroup *cgrp)
  651. {
  652. return NULL;
  653. }
  654. static inline struct psi_group *cgroup_psi(struct cgroup *cgrp)
  655. {
  656. return NULL;
  657. }
  658. static inline bool cgroup_psi_enabled(void)
  659. {
  660. return false;
  661. }
  662. static inline bool task_under_cgroup_hierarchy(struct task_struct *task,
  663. struct cgroup *ancestor)
  664. {
  665. return true;
  666. }
  667. static inline void cgroup_path_from_kernfs_id(u64 id, char *buf, size_t buflen)
  668. {}
  669. #endif /* !CONFIG_CGROUPS */
  670. #ifdef CONFIG_CGROUPS
  671. /*
  672. * cgroup scalable recursive statistics.
  673. */
  674. void cgroup_rstat_updated(struct cgroup *cgrp, int cpu);
  675. void cgroup_rstat_flush(struct cgroup *cgrp);
  676. void cgroup_rstat_flush_irqsafe(struct cgroup *cgrp);
  677. void cgroup_rstat_flush_hold(struct cgroup *cgrp);
  678. void cgroup_rstat_flush_release(void);
  679. /*
  680. * Basic resource stats.
  681. */
  682. #ifdef CONFIG_CGROUP_CPUACCT
  683. void cpuacct_charge(struct task_struct *tsk, u64 cputime);
  684. void cpuacct_account_field(struct task_struct *tsk, int index, u64 val);
  685. #else
  686. static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {}
  687. static inline void cpuacct_account_field(struct task_struct *tsk, int index,
  688. u64 val) {}
  689. #endif
  690. void __cgroup_account_cputime(struct cgroup *cgrp, u64 delta_exec);
  691. void __cgroup_account_cputime_field(struct cgroup *cgrp,
  692. enum cpu_usage_stat index, u64 delta_exec);
  693. static inline void cgroup_account_cputime(struct task_struct *task,
  694. u64 delta_exec)
  695. {
  696. struct cgroup *cgrp;
  697. cpuacct_charge(task, delta_exec);
  698. rcu_read_lock();
  699. cgrp = task_dfl_cgroup(task);
  700. if (cgroup_parent(cgrp))
  701. __cgroup_account_cputime(cgrp, delta_exec);
  702. rcu_read_unlock();
  703. }
  704. static inline void cgroup_account_cputime_field(struct task_struct *task,
  705. enum cpu_usage_stat index,
  706. u64 delta_exec)
  707. {
  708. struct cgroup *cgrp;
  709. cpuacct_account_field(task, index, delta_exec);
  710. rcu_read_lock();
  711. cgrp = task_dfl_cgroup(task);
  712. if (cgroup_parent(cgrp))
  713. __cgroup_account_cputime_field(cgrp, index, delta_exec);
  714. rcu_read_unlock();
  715. }
  716. #else /* CONFIG_CGROUPS */
  717. static inline void cgroup_account_cputime(struct task_struct *task,
  718. u64 delta_exec) {}
  719. static inline void cgroup_account_cputime_field(struct task_struct *task,
  720. enum cpu_usage_stat index,
  721. u64 delta_exec) {}
  722. #endif /* CONFIG_CGROUPS */
  723. /*
  724. * sock->sk_cgrp_data handling. For more info, see sock_cgroup_data
  725. * definition in cgroup-defs.h.
  726. */
  727. #ifdef CONFIG_SOCK_CGROUP_DATA
  728. #if defined(CONFIG_CGROUP_NET_PRIO) || defined(CONFIG_CGROUP_NET_CLASSID)
  729. extern spinlock_t cgroup_sk_update_lock;
  730. #endif
  731. void cgroup_sk_alloc_disable(void);
  732. void cgroup_sk_alloc(struct sock_cgroup_data *skcd);
  733. void cgroup_sk_clone(struct sock_cgroup_data *skcd);
  734. void cgroup_sk_free(struct sock_cgroup_data *skcd);
  735. static inline struct cgroup *sock_cgroup_ptr(struct sock_cgroup_data *skcd)
  736. {
  737. #if defined(CONFIG_CGROUP_NET_PRIO) || defined(CONFIG_CGROUP_NET_CLASSID)
  738. unsigned long v;
  739. /*
  740. * @skcd->val is 64bit but the following is safe on 32bit too as we
  741. * just need the lower ulong to be written and read atomically.
  742. */
  743. v = READ_ONCE(skcd->val);
  744. if (v & 3)
  745. return &cgrp_dfl_root.cgrp;
  746. return (struct cgroup *)(unsigned long)v ?: &cgrp_dfl_root.cgrp;
  747. #else
  748. return (struct cgroup *)(unsigned long)skcd->val;
  749. #endif
  750. }
  751. #else /* CONFIG_CGROUP_DATA */
  752. static inline void cgroup_sk_alloc(struct sock_cgroup_data *skcd) {}
  753. static inline void cgroup_sk_clone(struct sock_cgroup_data *skcd) {}
  754. static inline void cgroup_sk_free(struct sock_cgroup_data *skcd) {}
  755. #endif /* CONFIG_CGROUP_DATA */
  756. struct cgroup_namespace {
  757. refcount_t count;
  758. struct ns_common ns;
  759. struct user_namespace *user_ns;
  760. struct ucounts *ucounts;
  761. struct css_set *root_cset;
  762. };
  763. extern struct cgroup_namespace init_cgroup_ns;
  764. #ifdef CONFIG_CGROUPS
  765. void free_cgroup_ns(struct cgroup_namespace *ns);
  766. struct cgroup_namespace *copy_cgroup_ns(unsigned long flags,
  767. struct user_namespace *user_ns,
  768. struct cgroup_namespace *old_ns);
  769. int cgroup_path_ns(struct cgroup *cgrp, char *buf, size_t buflen,
  770. struct cgroup_namespace *ns);
  771. #else /* !CONFIG_CGROUPS */
  772. static inline void free_cgroup_ns(struct cgroup_namespace *ns) { }
  773. static inline struct cgroup_namespace *
  774. copy_cgroup_ns(unsigned long flags, struct user_namespace *user_ns,
  775. struct cgroup_namespace *old_ns)
  776. {
  777. return old_ns;
  778. }
  779. #endif /* !CONFIG_CGROUPS */
  780. static inline void get_cgroup_ns(struct cgroup_namespace *ns)
  781. {
  782. if (ns)
  783. refcount_inc(&ns->count);
  784. }
  785. static inline void put_cgroup_ns(struct cgroup_namespace *ns)
  786. {
  787. if (ns && refcount_dec_and_test(&ns->count))
  788. free_cgroup_ns(ns);
  789. }
  790. #ifdef CONFIG_CGROUPS
  791. void cgroup_enter_frozen(void);
  792. void cgroup_leave_frozen(bool always_leave);
  793. void cgroup_update_frozen(struct cgroup *cgrp);
  794. void cgroup_freeze(struct cgroup *cgrp, bool freeze);
  795. void cgroup_freezer_migrate_task(struct task_struct *task, struct cgroup *src,
  796. struct cgroup *dst);
  797. static inline bool cgroup_task_freeze(struct task_struct *task)
  798. {
  799. bool ret;
  800. if (task->flags & PF_KTHREAD)
  801. return false;
  802. rcu_read_lock();
  803. ret = test_bit(CGRP_FREEZE, &task_dfl_cgroup(task)->flags);
  804. rcu_read_unlock();
  805. return ret;
  806. }
  807. static inline bool cgroup_task_frozen(struct task_struct *task)
  808. {
  809. return task->frozen;
  810. }
  811. #else /* !CONFIG_CGROUPS */
  812. static inline void cgroup_enter_frozen(void) { }
  813. static inline void cgroup_leave_frozen(bool always_leave) { }
  814. static inline bool cgroup_task_freeze(struct task_struct *task)
  815. {
  816. return false;
  817. }
  818. static inline bool cgroup_task_frozen(struct task_struct *task)
  819. {
  820. return false;
  821. }
  822. #endif /* !CONFIG_CGROUPS */
  823. #ifdef CONFIG_CGROUP_BPF
  824. static inline void cgroup_bpf_get(struct cgroup *cgrp)
  825. {
  826. percpu_ref_get(&cgrp->bpf.refcnt);
  827. }
  828. static inline void cgroup_bpf_put(struct cgroup *cgrp)
  829. {
  830. percpu_ref_put(&cgrp->bpf.refcnt);
  831. }
  832. #else /* CONFIG_CGROUP_BPF */
  833. static inline void cgroup_bpf_get(struct cgroup *cgrp) {}
  834. static inline void cgroup_bpf_put(struct cgroup *cgrp) {}
  835. #endif /* CONFIG_CGROUP_BPF */
  836. #endif /* _LINUX_CGROUP_H */