cgroup.h 28 KB

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