cgroup-internal.h 6.7 KB

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