cgroup.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  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/nsproxy.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/cgroup-defs.h>
  25. #ifdef CONFIG_CGROUPS
  26. /*
  27. * All weight knobs on the default hierarhcy should use the following min,
  28. * default and max values. The default value is the logarithmic center of
  29. * MIN and MAX and allows 100x to be expressed in both directions.
  30. */
  31. #define CGROUP_WEIGHT_MIN 1
  32. #define CGROUP_WEIGHT_DFL 100
  33. #define CGROUP_WEIGHT_MAX 10000
  34. /* a css_task_iter should be treated as an opaque object */
  35. struct css_task_iter {
  36. struct cgroup_subsys *ss;
  37. struct list_head *cset_pos;
  38. struct list_head *cset_head;
  39. struct list_head *task_pos;
  40. struct list_head *tasks_head;
  41. struct list_head *mg_tasks_head;
  42. struct css_set *cur_cset;
  43. struct task_struct *cur_task;
  44. struct list_head iters_node; /* css_set->task_iters */
  45. };
  46. extern struct cgroup_root cgrp_dfl_root;
  47. extern struct css_set init_css_set;
  48. #define SUBSYS(_x) extern struct cgroup_subsys _x ## _cgrp_subsys;
  49. #include <linux/cgroup_subsys.h>
  50. #undef SUBSYS
  51. #define SUBSYS(_x) \
  52. extern struct static_key_true _x ## _cgrp_subsys_enabled_key; \
  53. extern struct static_key_true _x ## _cgrp_subsys_on_dfl_key;
  54. #include <linux/cgroup_subsys.h>
  55. #undef SUBSYS
  56. /**
  57. * cgroup_subsys_enabled - fast test on whether a subsys is enabled
  58. * @ss: subsystem in question
  59. */
  60. #define cgroup_subsys_enabled(ss) \
  61. static_branch_likely(&ss ## _enabled_key)
  62. /**
  63. * cgroup_subsys_on_dfl - fast test on whether a subsys is on default hierarchy
  64. * @ss: subsystem in question
  65. */
  66. #define cgroup_subsys_on_dfl(ss) \
  67. static_branch_likely(&ss ## _on_dfl_key)
  68. bool css_has_online_children(struct cgroup_subsys_state *css);
  69. struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss);
  70. struct cgroup_subsys_state *cgroup_get_e_css(struct cgroup *cgroup,
  71. struct cgroup_subsys *ss);
  72. struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry,
  73. struct cgroup_subsys *ss);
  74. struct cgroup *cgroup_get_from_path(const char *path);
  75. struct cgroup *cgroup_get_from_fd(int fd);
  76. int cgroup_attach_task_all(struct task_struct *from, struct task_struct *);
  77. int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from);
  78. int cgroup_add_dfl_cftypes(struct cgroup_subsys *ss, struct cftype *cfts);
  79. int cgroup_add_legacy_cftypes(struct cgroup_subsys *ss, struct cftype *cfts);
  80. int cgroup_rm_cftypes(struct cftype *cfts);
  81. void cgroup_file_notify(struct cgroup_file *cfile);
  82. int task_cgroup_path(struct task_struct *task, char *buf, size_t buflen);
  83. int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry);
  84. int proc_cgroup_show(struct seq_file *m, struct pid_namespace *ns,
  85. struct pid *pid, struct task_struct *tsk);
  86. void cgroup_fork(struct task_struct *p);
  87. extern int cgroup_can_fork(struct task_struct *p);
  88. extern void cgroup_cancel_fork(struct task_struct *p);
  89. extern void cgroup_post_fork(struct task_struct *p);
  90. void cgroup_exit(struct task_struct *p);
  91. void cgroup_free(struct task_struct *p);
  92. int cgroup_init_early(void);
  93. int cgroup_init(void);
  94. /*
  95. * Iteration helpers and macros.
  96. */
  97. struct cgroup_subsys_state *css_next_child(struct cgroup_subsys_state *pos,
  98. struct cgroup_subsys_state *parent);
  99. struct cgroup_subsys_state *css_next_descendant_pre(struct cgroup_subsys_state *pos,
  100. struct cgroup_subsys_state *css);
  101. struct cgroup_subsys_state *css_rightmost_descendant(struct cgroup_subsys_state *pos);
  102. struct cgroup_subsys_state *css_next_descendant_post(struct cgroup_subsys_state *pos,
  103. struct cgroup_subsys_state *css);
  104. struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset,
  105. struct cgroup_subsys_state **dst_cssp);
  106. struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset,
  107. struct cgroup_subsys_state **dst_cssp);
  108. void css_task_iter_start(struct cgroup_subsys_state *css,
  109. struct css_task_iter *it);
  110. struct task_struct *css_task_iter_next(struct css_task_iter *it);
  111. void css_task_iter_end(struct css_task_iter *it);
  112. /**
  113. * css_for_each_child - iterate through children of a css
  114. * @pos: the css * to use as the loop cursor
  115. * @parent: css whose children to walk
  116. *
  117. * Walk @parent's children. Must be called under rcu_read_lock().
  118. *
  119. * If a subsystem synchronizes ->css_online() and the start of iteration, a
  120. * css which finished ->css_online() is guaranteed to be visible in the
  121. * future iterations and will stay visible until the last reference is put.
  122. * A css which hasn't finished ->css_online() or already finished
  123. * ->css_offline() may show up during traversal. It's each subsystem's
  124. * responsibility to synchronize against on/offlining.
  125. *
  126. * It is allowed to temporarily drop RCU read lock during iteration. The
  127. * caller is responsible for ensuring that @pos remains accessible until
  128. * the start of the next iteration by, for example, bumping the css refcnt.
  129. */
  130. #define css_for_each_child(pos, parent) \
  131. for ((pos) = css_next_child(NULL, (parent)); (pos); \
  132. (pos) = css_next_child((pos), (parent)))
  133. /**
  134. * css_for_each_descendant_pre - pre-order walk of a css's descendants
  135. * @pos: the css * to use as the loop cursor
  136. * @root: css whose descendants to walk
  137. *
  138. * Walk @root's descendants. @root is included in the iteration and the
  139. * first node to be visited. Must be called under rcu_read_lock().
  140. *
  141. * If a subsystem synchronizes ->css_online() and the start of iteration, a
  142. * css which finished ->css_online() is guaranteed to be visible in the
  143. * future iterations and will stay visible until the last reference is put.
  144. * A css which hasn't finished ->css_online() or already finished
  145. * ->css_offline() may show up during traversal. It's each subsystem's
  146. * responsibility to synchronize against on/offlining.
  147. *
  148. * For example, the following guarantees that a descendant can't escape
  149. * state updates of its ancestors.
  150. *
  151. * my_online(@css)
  152. * {
  153. * Lock @css's parent and @css;
  154. * Inherit state from the parent;
  155. * Unlock both.
  156. * }
  157. *
  158. * my_update_state(@css)
  159. * {
  160. * css_for_each_descendant_pre(@pos, @css) {
  161. * Lock @pos;
  162. * if (@pos == @css)
  163. * Update @css's state;
  164. * else
  165. * Verify @pos is alive and inherit state from its parent;
  166. * Unlock @pos;
  167. * }
  168. * }
  169. *
  170. * As long as the inheriting step, including checking the parent state, is
  171. * enclosed inside @pos locking, double-locking the parent isn't necessary
  172. * while inheriting. The state update to the parent is guaranteed to be
  173. * visible by walking order and, as long as inheriting operations to the
  174. * same @pos are atomic to each other, multiple updates racing each other
  175. * still result in the correct state. It's guaranateed that at least one
  176. * inheritance happens for any css after the latest update to its parent.
  177. *
  178. * If checking parent's state requires locking the parent, each inheriting
  179. * iteration should lock and unlock both @pos->parent and @pos.
  180. *
  181. * Alternatively, a subsystem may choose to use a single global lock to
  182. * synchronize ->css_online() and ->css_offline() against tree-walking
  183. * operations.
  184. *
  185. * It is allowed to temporarily drop RCU read lock during iteration. The
  186. * caller is responsible for ensuring that @pos remains accessible until
  187. * the start of the next iteration by, for example, bumping the css refcnt.
  188. */
  189. #define css_for_each_descendant_pre(pos, css) \
  190. for ((pos) = css_next_descendant_pre(NULL, (css)); (pos); \
  191. (pos) = css_next_descendant_pre((pos), (css)))
  192. /**
  193. * css_for_each_descendant_post - post-order walk of a css's descendants
  194. * @pos: the css * to use as the loop cursor
  195. * @css: css whose descendants to walk
  196. *
  197. * Similar to css_for_each_descendant_pre() but performs post-order
  198. * traversal instead. @root is included in the iteration and the last
  199. * node to be visited.
  200. *
  201. * If a subsystem synchronizes ->css_online() and the start of iteration, a
  202. * css which finished ->css_online() is guaranteed to be visible in the
  203. * future iterations and will stay visible until the last reference is put.
  204. * A css which hasn't finished ->css_online() or already finished
  205. * ->css_offline() may show up during traversal. It's each subsystem's
  206. * responsibility to synchronize against on/offlining.
  207. *
  208. * Note that the walk visibility guarantee example described in pre-order
  209. * walk doesn't apply the same to post-order walks.
  210. */
  211. #define css_for_each_descendant_post(pos, css) \
  212. for ((pos) = css_next_descendant_post(NULL, (css)); (pos); \
  213. (pos) = css_next_descendant_post((pos), (css)))
  214. /**
  215. * cgroup_taskset_for_each - iterate cgroup_taskset
  216. * @task: the loop cursor
  217. * @dst_css: the destination css
  218. * @tset: taskset to iterate
  219. *
  220. * @tset may contain multiple tasks and they may belong to multiple
  221. * processes.
  222. *
  223. * On the v2 hierarchy, there may be tasks from multiple processes and they
  224. * may not share the source or destination csses.
  225. *
  226. * On traditional hierarchies, when there are multiple tasks in @tset, if a
  227. * task of a process is in @tset, all tasks of the process are in @tset.
  228. * Also, all are guaranteed to share the same source and destination csses.
  229. *
  230. * Iteration is not in any specific order.
  231. */
  232. #define cgroup_taskset_for_each(task, dst_css, tset) \
  233. for ((task) = cgroup_taskset_first((tset), &(dst_css)); \
  234. (task); \
  235. (task) = cgroup_taskset_next((tset), &(dst_css)))
  236. /**
  237. * cgroup_taskset_for_each_leader - iterate group leaders in a cgroup_taskset
  238. * @leader: the loop cursor
  239. * @dst_css: the destination css
  240. * @tset: takset to iterate
  241. *
  242. * Iterate threadgroup leaders of @tset. For single-task migrations, @tset
  243. * may not contain any.
  244. */
  245. #define cgroup_taskset_for_each_leader(leader, dst_css, tset) \
  246. for ((leader) = cgroup_taskset_first((tset), &(dst_css)); \
  247. (leader); \
  248. (leader) = cgroup_taskset_next((tset), &(dst_css))) \
  249. if ((leader) != (leader)->group_leader) \
  250. ; \
  251. else
  252. /*
  253. * Inline functions.
  254. */
  255. /**
  256. * css_get - obtain a reference on the specified css
  257. * @css: target css
  258. *
  259. * The caller must already have a reference.
  260. */
  261. static inline void css_get(struct cgroup_subsys_state *css)
  262. {
  263. if (!(css->flags & CSS_NO_REF))
  264. percpu_ref_get(&css->refcnt);
  265. }
  266. /**
  267. * css_get_many - obtain references on the specified css
  268. * @css: target css
  269. * @n: number of references to get
  270. *
  271. * The caller must already have a reference.
  272. */
  273. static inline void css_get_many(struct cgroup_subsys_state *css, unsigned int n)
  274. {
  275. if (!(css->flags & CSS_NO_REF))
  276. percpu_ref_get_many(&css->refcnt, n);
  277. }
  278. /**
  279. * css_tryget - try to obtain a reference on the specified css
  280. * @css: target css
  281. *
  282. * Obtain a reference on @css unless it already has reached zero and is
  283. * being released. This function doesn't care whether @css is on or
  284. * offline. The caller naturally needs to ensure that @css is accessible
  285. * but doesn't have to be holding a reference on it - IOW, RCU protected
  286. * access is good enough for this function. Returns %true if a reference
  287. * count was successfully obtained; %false otherwise.
  288. */
  289. static inline bool css_tryget(struct cgroup_subsys_state *css)
  290. {
  291. if (!(css->flags & CSS_NO_REF))
  292. return percpu_ref_tryget(&css->refcnt);
  293. return true;
  294. }
  295. /**
  296. * css_tryget_online - try to obtain a reference on the specified css if online
  297. * @css: target css
  298. *
  299. * Obtain a reference on @css if it's online. The caller naturally needs
  300. * to ensure that @css is accessible but doesn't have to be holding a
  301. * reference on it - IOW, RCU protected access is good enough for this
  302. * function. Returns %true if a reference count was successfully obtained;
  303. * %false otherwise.
  304. */
  305. static inline bool css_tryget_online(struct cgroup_subsys_state *css)
  306. {
  307. if (!(css->flags & CSS_NO_REF))
  308. return percpu_ref_tryget_live(&css->refcnt);
  309. return true;
  310. }
  311. /**
  312. * css_is_dying - test whether the specified css is dying
  313. * @css: target css
  314. *
  315. * Test whether @css is in the process of offlining or already offline. In
  316. * most cases, ->css_online() and ->css_offline() callbacks should be
  317. * enough; however, the actual offline operations are RCU delayed and this
  318. * test returns %true also when @css is scheduled to be offlined.
  319. *
  320. * This is useful, for example, when the use case requires synchronous
  321. * behavior with respect to cgroup removal. cgroup removal schedules css
  322. * offlining but the css can seem alive while the operation is being
  323. * delayed. If the delay affects user visible semantics, this test can be
  324. * used to resolve the situation.
  325. */
  326. static inline bool css_is_dying(struct cgroup_subsys_state *css)
  327. {
  328. return !(css->flags & CSS_NO_REF) && percpu_ref_is_dying(&css->refcnt);
  329. }
  330. /**
  331. * css_put - put a css reference
  332. * @css: target css
  333. *
  334. * Put a reference obtained via css_get() and css_tryget_online().
  335. */
  336. static inline void css_put(struct cgroup_subsys_state *css)
  337. {
  338. if (!(css->flags & CSS_NO_REF))
  339. percpu_ref_put(&css->refcnt);
  340. }
  341. /**
  342. * css_put_many - put css references
  343. * @css: target css
  344. * @n: number of references to put
  345. *
  346. * Put references obtained via css_get() and css_tryget_online().
  347. */
  348. static inline void css_put_many(struct cgroup_subsys_state *css, unsigned int n)
  349. {
  350. if (!(css->flags & CSS_NO_REF))
  351. percpu_ref_put_many(&css->refcnt, n);
  352. }
  353. static inline void cgroup_put(struct cgroup *cgrp)
  354. {
  355. css_put(&cgrp->self);
  356. }
  357. /**
  358. * task_css_set_check - obtain a task's css_set with extra access conditions
  359. * @task: the task to obtain css_set for
  360. * @__c: extra condition expression to be passed to rcu_dereference_check()
  361. *
  362. * A task's css_set is RCU protected, initialized and exited while holding
  363. * task_lock(), and can only be modified while holding both cgroup_mutex
  364. * and task_lock() while the task is alive. This macro verifies that the
  365. * caller is inside proper critical section and returns @task's css_set.
  366. *
  367. * The caller can also specify additional allowed conditions via @__c, such
  368. * as locks used during the cgroup_subsys::attach() methods.
  369. */
  370. #ifdef CONFIG_PROVE_RCU
  371. extern struct mutex cgroup_mutex;
  372. extern spinlock_t css_set_lock;
  373. #define task_css_set_check(task, __c) \
  374. rcu_dereference_check((task)->cgroups, \
  375. lockdep_is_held(&cgroup_mutex) || \
  376. lockdep_is_held(&css_set_lock) || \
  377. ((task)->flags & PF_EXITING) || (__c))
  378. #else
  379. #define task_css_set_check(task, __c) \
  380. rcu_dereference((task)->cgroups)
  381. #endif
  382. /**
  383. * task_css_check - obtain css for (task, subsys) w/ extra access conds
  384. * @task: the target task
  385. * @subsys_id: the target subsystem ID
  386. * @__c: extra condition expression to be passed to rcu_dereference_check()
  387. *
  388. * Return the cgroup_subsys_state for the (@task, @subsys_id) pair. The
  389. * synchronization rules are the same as task_css_set_check().
  390. */
  391. #define task_css_check(task, subsys_id, __c) \
  392. task_css_set_check((task), (__c))->subsys[(subsys_id)]
  393. /**
  394. * task_css_set - obtain a task's css_set
  395. * @task: the task to obtain css_set for
  396. *
  397. * See task_css_set_check().
  398. */
  399. static inline struct css_set *task_css_set(struct task_struct *task)
  400. {
  401. return task_css_set_check(task, false);
  402. }
  403. /**
  404. * task_css - obtain css for (task, subsys)
  405. * @task: the target task
  406. * @subsys_id: the target subsystem ID
  407. *
  408. * See task_css_check().
  409. */
  410. static inline struct cgroup_subsys_state *task_css(struct task_struct *task,
  411. int subsys_id)
  412. {
  413. return task_css_check(task, subsys_id, false);
  414. }
  415. /**
  416. * task_get_css - find and get the css for (task, subsys)
  417. * @task: the target task
  418. * @subsys_id: the target subsystem ID
  419. *
  420. * Find the css for the (@task, @subsys_id) combination, increment a
  421. * reference on and return it. This function is guaranteed to return a
  422. * valid css.
  423. */
  424. static inline struct cgroup_subsys_state *
  425. task_get_css(struct task_struct *task, int subsys_id)
  426. {
  427. struct cgroup_subsys_state *css;
  428. rcu_read_lock();
  429. while (true) {
  430. css = task_css(task, subsys_id);
  431. if (likely(css_tryget_online(css)))
  432. break;
  433. cpu_relax();
  434. }
  435. rcu_read_unlock();
  436. return css;
  437. }
  438. /**
  439. * task_css_is_root - test whether a task belongs to the root css
  440. * @task: the target task
  441. * @subsys_id: the target subsystem ID
  442. *
  443. * Test whether @task belongs to the root css on the specified subsystem.
  444. * May be invoked in any context.
  445. */
  446. static inline bool task_css_is_root(struct task_struct *task, int subsys_id)
  447. {
  448. return task_css_check(task, subsys_id, true) ==
  449. init_css_set.subsys[subsys_id];
  450. }
  451. static inline struct cgroup *task_cgroup(struct task_struct *task,
  452. int subsys_id)
  453. {
  454. return task_css(task, subsys_id)->cgroup;
  455. }
  456. /**
  457. * cgroup_is_descendant - test ancestry
  458. * @cgrp: the cgroup to be tested
  459. * @ancestor: possible ancestor of @cgrp
  460. *
  461. * Test whether @cgrp is a descendant of @ancestor. It also returns %true
  462. * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
  463. * and @ancestor are accessible.
  464. */
  465. static inline bool cgroup_is_descendant(struct cgroup *cgrp,
  466. struct cgroup *ancestor)
  467. {
  468. if (cgrp->root != ancestor->root || cgrp->level < ancestor->level)
  469. return false;
  470. return cgrp->ancestor_ids[ancestor->level] == ancestor->id;
  471. }
  472. /**
  473. * task_under_cgroup_hierarchy - test task's membership of cgroup ancestry
  474. * @task: the task to be tested
  475. * @ancestor: possible ancestor of @task's cgroup
  476. *
  477. * Tests whether @task's default cgroup hierarchy is a descendant of @ancestor.
  478. * It follows all the same rules as cgroup_is_descendant, and only applies
  479. * to the default hierarchy.
  480. */
  481. static inline bool task_under_cgroup_hierarchy(struct task_struct *task,
  482. struct cgroup *ancestor)
  483. {
  484. struct css_set *cset = task_css_set(task);
  485. return cgroup_is_descendant(cset->dfl_cgrp, ancestor);
  486. }
  487. /* no synchronization, the result can only be used as a hint */
  488. static inline bool cgroup_is_populated(struct cgroup *cgrp)
  489. {
  490. return cgrp->populated_cnt;
  491. }
  492. /* returns ino associated with a cgroup */
  493. static inline ino_t cgroup_ino(struct cgroup *cgrp)
  494. {
  495. return cgrp->kn->ino;
  496. }
  497. /* cft/css accessors for cftype->write() operation */
  498. static inline struct cftype *of_cft(struct kernfs_open_file *of)
  499. {
  500. return of->kn->priv;
  501. }
  502. struct cgroup_subsys_state *of_css(struct kernfs_open_file *of);
  503. /* cft/css accessors for cftype->seq_*() operations */
  504. static inline struct cftype *seq_cft(struct seq_file *seq)
  505. {
  506. return of_cft(seq->private);
  507. }
  508. static inline struct cgroup_subsys_state *seq_css(struct seq_file *seq)
  509. {
  510. return of_css(seq->private);
  511. }
  512. /*
  513. * Name / path handling functions. All are thin wrappers around the kernfs
  514. * counterparts and can be called under any context.
  515. */
  516. static inline int cgroup_name(struct cgroup *cgrp, char *buf, size_t buflen)
  517. {
  518. return kernfs_name(cgrp->kn, buf, buflen);
  519. }
  520. static inline int cgroup_path(struct cgroup *cgrp, char *buf, size_t buflen)
  521. {
  522. return kernfs_path(cgrp->kn, buf, buflen);
  523. }
  524. static inline void pr_cont_cgroup_name(struct cgroup *cgrp)
  525. {
  526. pr_cont_kernfs_name(cgrp->kn);
  527. }
  528. static inline void pr_cont_cgroup_path(struct cgroup *cgrp)
  529. {
  530. pr_cont_kernfs_path(cgrp->kn);
  531. }
  532. static inline void cgroup_init_kthreadd(void)
  533. {
  534. /*
  535. * kthreadd is inherited by all kthreads, keep it in the root so
  536. * that the new kthreads are guaranteed to stay in the root until
  537. * initialization is finished.
  538. */
  539. current->no_cgroup_migration = 1;
  540. }
  541. static inline void cgroup_kthread_ready(void)
  542. {
  543. /*
  544. * This kthread finished initialization. The creator should have
  545. * set PF_NO_SETAFFINITY if this kthread should stay in the root.
  546. */
  547. current->no_cgroup_migration = 0;
  548. }
  549. #else /* !CONFIG_CGROUPS */
  550. struct cgroup_subsys_state;
  551. struct cgroup;
  552. static inline void css_put(struct cgroup_subsys_state *css) {}
  553. static inline int cgroup_attach_task_all(struct task_struct *from,
  554. struct task_struct *t) { return 0; }
  555. static inline int cgroupstats_build(struct cgroupstats *stats,
  556. struct dentry *dentry) { return -EINVAL; }
  557. static inline void cgroup_fork(struct task_struct *p) {}
  558. static inline int cgroup_can_fork(struct task_struct *p) { return 0; }
  559. static inline void cgroup_cancel_fork(struct task_struct *p) {}
  560. static inline void cgroup_post_fork(struct task_struct *p) {}
  561. static inline void cgroup_exit(struct task_struct *p) {}
  562. static inline void cgroup_free(struct task_struct *p) {}
  563. static inline int cgroup_init_early(void) { return 0; }
  564. static inline int cgroup_init(void) { return 0; }
  565. static inline void cgroup_init_kthreadd(void) {}
  566. static inline void cgroup_kthread_ready(void) {}
  567. static inline bool task_under_cgroup_hierarchy(struct task_struct *task,
  568. struct cgroup *ancestor)
  569. {
  570. return true;
  571. }
  572. #endif /* !CONFIG_CGROUPS */
  573. /*
  574. * sock->sk_cgrp_data handling. For more info, see sock_cgroup_data
  575. * definition in cgroup-defs.h.
  576. */
  577. #ifdef CONFIG_SOCK_CGROUP_DATA
  578. #if defined(CONFIG_CGROUP_NET_PRIO) || defined(CONFIG_CGROUP_NET_CLASSID)
  579. extern spinlock_t cgroup_sk_update_lock;
  580. #endif
  581. void cgroup_sk_alloc_disable(void);
  582. void cgroup_sk_alloc(struct sock_cgroup_data *skcd);
  583. void cgroup_sk_free(struct sock_cgroup_data *skcd);
  584. static inline struct cgroup *sock_cgroup_ptr(struct sock_cgroup_data *skcd)
  585. {
  586. #if defined(CONFIG_CGROUP_NET_PRIO) || defined(CONFIG_CGROUP_NET_CLASSID)
  587. unsigned long v;
  588. /*
  589. * @skcd->val is 64bit but the following is safe on 32bit too as we
  590. * just need the lower ulong to be written and read atomically.
  591. */
  592. v = READ_ONCE(skcd->val);
  593. if (v & 1)
  594. return &cgrp_dfl_root.cgrp;
  595. return (struct cgroup *)(unsigned long)v ?: &cgrp_dfl_root.cgrp;
  596. #else
  597. return (struct cgroup *)(unsigned long)skcd->val;
  598. #endif
  599. }
  600. #else /* CONFIG_CGROUP_DATA */
  601. static inline void cgroup_sk_alloc(struct sock_cgroup_data *skcd) {}
  602. static inline void cgroup_sk_free(struct sock_cgroup_data *skcd) {}
  603. #endif /* CONFIG_CGROUP_DATA */
  604. struct cgroup_namespace {
  605. atomic_t count;
  606. struct ns_common ns;
  607. struct user_namespace *user_ns;
  608. struct ucounts *ucounts;
  609. struct css_set *root_cset;
  610. };
  611. extern struct cgroup_namespace init_cgroup_ns;
  612. #ifdef CONFIG_CGROUPS
  613. void free_cgroup_ns(struct cgroup_namespace *ns);
  614. struct cgroup_namespace *copy_cgroup_ns(unsigned long flags,
  615. struct user_namespace *user_ns,
  616. struct cgroup_namespace *old_ns);
  617. int cgroup_path_ns(struct cgroup *cgrp, char *buf, size_t buflen,
  618. struct cgroup_namespace *ns);
  619. #else /* !CONFIG_CGROUPS */
  620. static inline void free_cgroup_ns(struct cgroup_namespace *ns) { }
  621. static inline struct cgroup_namespace *
  622. copy_cgroup_ns(unsigned long flags, struct user_namespace *user_ns,
  623. struct cgroup_namespace *old_ns)
  624. {
  625. return old_ns;
  626. }
  627. #endif /* !CONFIG_CGROUPS */
  628. static inline void get_cgroup_ns(struct cgroup_namespace *ns)
  629. {
  630. if (ns)
  631. atomic_inc(&ns->count);
  632. }
  633. static inline void put_cgroup_ns(struct cgroup_namespace *ns)
  634. {
  635. if (ns && atomic_dec_and_test(&ns->count))
  636. free_cgroup_ns(ns);
  637. }
  638. #endif /* _LINUX_CGROUP_H */