cgroup-internal.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __CGROUP_INTERNAL_H
  3. #define __CGROUP_INTERNAL_H
  4. #include <linux/cgroup.h>
  5. #include <linux/kernfs.h>
  6. #include <linux/workqueue.h>
  7. #include <linux/list.h>
  8. #include <linux/refcount.h>
  9. #include <linux/fs_parser.h>
  10. #define TRACE_CGROUP_PATH_LEN 1024
  11. extern spinlock_t trace_cgroup_path_lock;
  12. extern char trace_cgroup_path[TRACE_CGROUP_PATH_LEN];
  13. extern bool cgroup_debug;
  14. extern void __init enable_debug_cgroup(void);
  15. /*
  16. * cgroup_path() takes a spin lock. It is good practice not to take
  17. * spin locks within trace point handlers, as they are mostly hidden
  18. * from normal view. As cgroup_path() can take the kernfs_rename_lock
  19. * spin lock, it is best to not call that function from the trace event
  20. * handler.
  21. *
  22. * Note: trace_cgroup_##type##_enabled() is a static branch that will only
  23. * be set when the trace event is enabled.
  24. */
  25. #define TRACE_CGROUP_PATH(type, cgrp, ...) \
  26. do { \
  27. if (trace_cgroup_##type##_enabled()) { \
  28. unsigned long flags; \
  29. spin_lock_irqsave(&trace_cgroup_path_lock, \
  30. flags); \
  31. cgroup_path(cgrp, trace_cgroup_path, \
  32. TRACE_CGROUP_PATH_LEN); \
  33. trace_cgroup_##type(cgrp, trace_cgroup_path, \
  34. ##__VA_ARGS__); \
  35. spin_unlock_irqrestore(&trace_cgroup_path_lock, \
  36. flags); \
  37. } \
  38. } while (0)
  39. /*
  40. * The cgroup filesystem superblock creation/mount context.
  41. */
  42. struct cgroup_fs_context {
  43. struct kernfs_fs_context kfc;
  44. struct cgroup_root *root;
  45. struct cgroup_namespace *ns;
  46. unsigned int flags; /* CGRP_ROOT_* flags */
  47. /* cgroup1 bits */
  48. bool cpuset_clone_children;
  49. bool none; /* User explicitly requested empty subsystem */
  50. bool all_ss; /* Seen 'all' option */
  51. u16 subsys_mask; /* Selected subsystems */
  52. char *name; /* Hierarchy name */
  53. char *release_agent; /* Path for release notifications */
  54. };
  55. static inline struct cgroup_fs_context *cgroup_fc2context(struct fs_context *fc)
  56. {
  57. struct kernfs_fs_context *kfc = fc->fs_private;
  58. return container_of(kfc, struct cgroup_fs_context, kfc);
  59. }
  60. struct cgroup_pidlist;
  61. struct cgroup_file_ctx {
  62. struct cgroup_namespace *ns;
  63. struct {
  64. void *trigger;
  65. } psi;
  66. struct {
  67. bool started;
  68. struct css_task_iter iter;
  69. } procs;
  70. struct {
  71. struct cgroup_pidlist *pidlist;
  72. } procs1;
  73. };
  74. /*
  75. * A cgroup can be associated with multiple css_sets as different tasks may
  76. * belong to different cgroups on different hierarchies. In the other
  77. * direction, a css_set is naturally associated with multiple cgroups.
  78. * This M:N relationship is represented by the following link structure
  79. * which exists for each association and allows traversing the associations
  80. * from both sides.
  81. */
  82. struct cgrp_cset_link {
  83. /* the cgroup and css_set this link associates */
  84. struct cgroup *cgrp;
  85. struct css_set *cset;
  86. /* list of cgrp_cset_links anchored at cgrp->cset_links */
  87. struct list_head cset_link;
  88. /* list of cgrp_cset_links anchored at css_set->cgrp_links */
  89. struct list_head cgrp_link;
  90. };
  91. /* used to track tasks and csets during migration */
  92. struct cgroup_taskset {
  93. /* the src and dst cset list running through cset->mg_node */
  94. struct list_head src_csets;
  95. struct list_head dst_csets;
  96. /* the number of tasks in the set */
  97. int nr_tasks;
  98. /* the subsys currently being processed */
  99. int ssid;
  100. /*
  101. * Fields for cgroup_taskset_*() iteration.
  102. *
  103. * Before migration is committed, the target migration tasks are on
  104. * ->mg_tasks of the csets on ->src_csets. After, on ->mg_tasks of
  105. * the csets on ->dst_csets. ->csets point to either ->src_csets
  106. * or ->dst_csets depending on whether migration is committed.
  107. *
  108. * ->cur_csets and ->cur_task point to the current task position
  109. * during iteration.
  110. */
  111. struct list_head *csets;
  112. struct css_set *cur_cset;
  113. struct task_struct *cur_task;
  114. };
  115. /* migration context also tracks preloading */
  116. struct cgroup_mgctx {
  117. /*
  118. * Preloaded source and destination csets. Used to guarantee
  119. * atomic success or failure on actual migration.
  120. */
  121. struct list_head preloaded_src_csets;
  122. struct list_head preloaded_dst_csets;
  123. /* tasks and csets to migrate */
  124. struct cgroup_taskset tset;
  125. /* subsystems affected by migration */
  126. u16 ss_mask;
  127. };
  128. #define CGROUP_TASKSET_INIT(tset) \
  129. { \
  130. .src_csets = LIST_HEAD_INIT(tset.src_csets), \
  131. .dst_csets = LIST_HEAD_INIT(tset.dst_csets), \
  132. .csets = &tset.src_csets, \
  133. }
  134. #define CGROUP_MGCTX_INIT(name) \
  135. { \
  136. LIST_HEAD_INIT(name.preloaded_src_csets), \
  137. LIST_HEAD_INIT(name.preloaded_dst_csets), \
  138. CGROUP_TASKSET_INIT(name.tset), \
  139. }
  140. #define DEFINE_CGROUP_MGCTX(name) \
  141. struct cgroup_mgctx name = CGROUP_MGCTX_INIT(name)
  142. extern struct mutex cgroup_mutex;
  143. extern spinlock_t css_set_lock;
  144. extern struct cgroup_subsys *cgroup_subsys[];
  145. extern struct list_head cgroup_roots;
  146. extern struct file_system_type cgroup_fs_type;
  147. /* iterate across the hierarchies */
  148. #define for_each_root(root) \
  149. list_for_each_entry((root), &cgroup_roots, root_list)
  150. /**
  151. * for_each_subsys - iterate all enabled cgroup subsystems
  152. * @ss: the iteration cursor
  153. * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
  154. */
  155. #define for_each_subsys(ss, ssid) \
  156. for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT && \
  157. (((ss) = cgroup_subsys[ssid]) || true); (ssid)++)
  158. static inline bool cgroup_is_dead(const struct cgroup *cgrp)
  159. {
  160. return !(cgrp->self.flags & CSS_ONLINE);
  161. }
  162. static inline bool notify_on_release(const struct cgroup *cgrp)
  163. {
  164. return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
  165. }
  166. void put_css_set_locked(struct css_set *cset);
  167. static inline void put_css_set(struct css_set *cset)
  168. {
  169. unsigned long flags;
  170. /*
  171. * Ensure that the refcount doesn't hit zero while any readers
  172. * can see it. Similar to atomic_dec_and_lock(), but for an
  173. * rwlock
  174. */
  175. if (refcount_dec_not_one(&cset->refcount))
  176. return;
  177. spin_lock_irqsave(&css_set_lock, flags);
  178. put_css_set_locked(cset);
  179. spin_unlock_irqrestore(&css_set_lock, flags);
  180. }
  181. /*
  182. * refcounted get/put for css_set objects
  183. */
  184. static inline void get_css_set(struct css_set *cset)
  185. {
  186. refcount_inc(&cset->refcount);
  187. }
  188. bool cgroup_ssid_enabled(int ssid);
  189. bool cgroup_on_dfl(const struct cgroup *cgrp);
  190. bool cgroup_is_thread_root(struct cgroup *cgrp);
  191. bool cgroup_is_threaded(struct cgroup *cgrp);
  192. struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root);
  193. struct cgroup *task_cgroup_from_root(struct task_struct *task,
  194. struct cgroup_root *root);
  195. struct cgroup *cgroup_kn_lock_live(struct kernfs_node *kn, bool drain_offline);
  196. void cgroup_kn_unlock(struct kernfs_node *kn);
  197. int cgroup_path_ns_locked(struct cgroup *cgrp, char *buf, size_t buflen,
  198. struct cgroup_namespace *ns);
  199. void cgroup_free_root(struct cgroup_root *root);
  200. void init_cgroup_root(struct cgroup_fs_context *ctx);
  201. int cgroup_setup_root(struct cgroup_root *root, u16 ss_mask);
  202. int rebind_subsystems(struct cgroup_root *dst_root, u16 ss_mask);
  203. int cgroup_do_get_tree(struct fs_context *fc);
  204. int cgroup_migrate_vet_dst(struct cgroup *dst_cgrp);
  205. void cgroup_migrate_finish(struct cgroup_mgctx *mgctx);
  206. void cgroup_migrate_add_src(struct css_set *src_cset, struct cgroup *dst_cgrp,
  207. struct cgroup_mgctx *mgctx);
  208. int cgroup_migrate_prepare_dst(struct cgroup_mgctx *mgctx);
  209. int cgroup_migrate(struct task_struct *leader, bool threadgroup,
  210. struct cgroup_mgctx *mgctx);
  211. int cgroup_attach_task(struct cgroup *dst_cgrp, struct task_struct *leader,
  212. bool threadgroup);
  213. struct task_struct *cgroup_procs_write_start(char *buf, bool threadgroup,
  214. bool *locked,
  215. struct cgroup *dst_cgrp);
  216. __acquires(&cgroup_threadgroup_rwsem);
  217. void cgroup_procs_write_finish(struct task_struct *task, bool locked)
  218. __releases(&cgroup_threadgroup_rwsem);
  219. void cgroup_lock_and_drain_offline(struct cgroup *cgrp);
  220. int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name, umode_t mode);
  221. int cgroup_rmdir(struct kernfs_node *kn);
  222. int cgroup_show_path(struct seq_file *sf, struct kernfs_node *kf_node,
  223. struct kernfs_root *kf_root);
  224. int __cgroup_task_count(const struct cgroup *cgrp);
  225. int cgroup_task_count(const struct cgroup *cgrp);
  226. /*
  227. * rstat.c
  228. */
  229. int cgroup_rstat_init(struct cgroup *cgrp);
  230. void cgroup_rstat_exit(struct cgroup *cgrp);
  231. void cgroup_rstat_boot(void);
  232. void cgroup_base_stat_cputime_show(struct seq_file *seq);
  233. /*
  234. * namespace.c
  235. */
  236. extern const struct proc_ns_operations cgroupns_operations;
  237. /*
  238. * cgroup-v1.c
  239. */
  240. extern struct cftype cgroup1_base_files[];
  241. extern struct kernfs_syscall_ops cgroup1_kf_syscall_ops;
  242. extern const struct fs_parameter_spec cgroup1_fs_parameters[];
  243. int proc_cgroupstats_show(struct seq_file *m, void *v);
  244. bool cgroup1_ssid_disabled(int ssid);
  245. void cgroup1_pidlist_destroy_all(struct cgroup *cgrp);
  246. void cgroup1_release_agent(struct work_struct *work);
  247. void cgroup1_check_for_release(struct cgroup *cgrp);
  248. int cgroup1_parse_param(struct fs_context *fc, struct fs_parameter *param);
  249. int cgroup1_get_tree(struct fs_context *fc);
  250. int cgroup1_reconfigure(struct fs_context *ctx);
  251. #endif /* __CGROUP_INTERNAL_H */