cgroup-internal.h 6.6 KB

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