cgroup-internal.h 7.9 KB

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