unified-hierarchy.txt 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. Cgroup unified hierarchy
  2. April, 2014 Tejun Heo <tj@kernel.org>
  3. This document describes the changes made by unified hierarchy and
  4. their rationales. It will eventually be merged into the main cgroup
  5. documentation.
  6. CONTENTS
  7. 1. Background
  8. 2. Basic Operation
  9. 2-1. Mounting
  10. 2-2. cgroup.subtree_control
  11. 2-3. cgroup.controllers
  12. 3. Structural Constraints
  13. 3-1. Top-down
  14. 3-2. No internal tasks
  15. 4. Other Changes
  16. 4-1. [Un]populated Notification
  17. 4-2. Other Core Changes
  18. 4-3. Per-Controller Changes
  19. 4-3-1. blkio
  20. 4-3-2. cpuset
  21. 4-3-3. memory
  22. 5. Planned Changes
  23. 5-1. CAP for resource control
  24. 1. Background
  25. cgroup allows an arbitrary number of hierarchies and each hierarchy
  26. can host any number of controllers. While this seems to provide a
  27. high level of flexibility, it isn't quite useful in practice.
  28. For example, as there is only one instance of each controller, utility
  29. type controllers such as freezer which can be useful in all
  30. hierarchies can only be used in one. The issue is exacerbated by the
  31. fact that controllers can't be moved around once hierarchies are
  32. populated. Another issue is that all controllers bound to a hierarchy
  33. are forced to have exactly the same view of the hierarchy. It isn't
  34. possible to vary the granularity depending on the specific controller.
  35. In practice, these issues heavily limit which controllers can be put
  36. on the same hierarchy and most configurations resort to putting each
  37. controller on its own hierarchy. Only closely related ones, such as
  38. the cpu and cpuacct controllers, make sense to put on the same
  39. hierarchy. This often means that userland ends up managing multiple
  40. similar hierarchies repeating the same steps on each hierarchy
  41. whenever a hierarchy management operation is necessary.
  42. Unfortunately, support for multiple hierarchies comes at a steep cost.
  43. Internal implementation in cgroup core proper is dazzlingly
  44. complicated but more importantly the support for multiple hierarchies
  45. restricts how cgroup is used in general and what controllers can do.
  46. There's no limit on how many hierarchies there may be, which means
  47. that a task's cgroup membership can't be described in finite length.
  48. The key may contain any varying number of entries and is unlimited in
  49. length, which makes it highly awkward to handle and leads to addition
  50. of controllers which exist only to identify membership, which in turn
  51. exacerbates the original problem.
  52. Also, as a controller can't have any expectation regarding what shape
  53. of hierarchies other controllers would be on, each controller has to
  54. assume that all other controllers are operating on completely
  55. orthogonal hierarchies. This makes it impossible, or at least very
  56. cumbersome, for controllers to cooperate with each other.
  57. In most use cases, putting controllers on hierarchies which are
  58. completely orthogonal to each other isn't necessary. What usually is
  59. called for is the ability to have differing levels of granularity
  60. depending on the specific controller. In other words, hierarchy may
  61. be collapsed from leaf towards root when viewed from specific
  62. controllers. For example, a given configuration might not care about
  63. how memory is distributed beyond a certain level while still wanting
  64. to control how CPU cycles are distributed.
  65. Unified hierarchy is the next version of cgroup interface. It aims to
  66. address the aforementioned issues by having more structure while
  67. retaining enough flexibility for most use cases. Various other
  68. general and controller-specific interface issues are also addressed in
  69. the process.
  70. 2. Basic Operation
  71. 2-1. Mounting
  72. Currently, unified hierarchy can be mounted with the following mount
  73. command. Note that this is still under development and scheduled to
  74. change soon.
  75. mount -t cgroup -o __DEVEL__sane_behavior cgroup $MOUNT_POINT
  76. All controllers which support the unified hierarchy and are not bound
  77. to other hierarchies are automatically bound to unified hierarchy and
  78. show up at the root of it. Controllers which are enabled only in the
  79. root of unified hierarchy can be bound to other hierarchies. This
  80. allows mixing unified hierarchy with the traditional multiple
  81. hierarchies in a fully backward compatible way.
  82. For development purposes, the following boot parameter makes all
  83. controllers to appear on the unified hierarchy whether supported or
  84. not.
  85. cgroup__DEVEL__legacy_files_on_dfl
  86. A controller can be moved across hierarchies only after the controller
  87. is no longer referenced in its current hierarchy. Because per-cgroup
  88. controller states are destroyed asynchronously and controllers may
  89. have lingering references, a controller may not show up immediately on
  90. the unified hierarchy after the final umount of the previous
  91. hierarchy. Similarly, a controller should be fully disabled to be
  92. moved out of the unified hierarchy and it may take some time for the
  93. disabled controller to become available for other hierarchies;
  94. furthermore, due to dependencies among controllers, other controllers
  95. may need to be disabled too.
  96. While useful for development and manual configurations, dynamically
  97. moving controllers between the unified and other hierarchies is
  98. strongly discouraged for production use. It is recommended to decide
  99. the hierarchies and controller associations before starting using the
  100. controllers.
  101. 2-2. cgroup.subtree_control
  102. All cgroups on unified hierarchy have a "cgroup.subtree_control" file
  103. which governs which controllers are enabled on the children of the
  104. cgroup. Let's assume a hierarchy like the following.
  105. root - A - B - C
  106. \ D
  107. root's "cgroup.subtree_control" file determines which controllers are
  108. enabled on A. A's on B. B's on C and D. This coincides with the
  109. fact that controllers on the immediate sub-level are used to
  110. distribute the resources of the parent. In fact, it's natural to
  111. assume that resource control knobs of a child belong to its parent.
  112. Enabling a controller in a "cgroup.subtree_control" file declares that
  113. distribution of the respective resources of the cgroup will be
  114. controlled. Note that this means that controller enable states are
  115. shared among siblings.
  116. When read, the file contains a space-separated list of currently
  117. enabled controllers. A write to the file should contain a
  118. space-separated list of controllers with '+' or '-' prefixed (without
  119. the quotes). Controllers prefixed with '+' are enabled and '-'
  120. disabled. If a controller is listed multiple times, the last entry
  121. wins. The specific operations are executed atomically - either all
  122. succeed or fail.
  123. 2-3. cgroup.controllers
  124. Read-only "cgroup.controllers" file contains a space-separated list of
  125. controllers which can be enabled in the cgroup's
  126. "cgroup.subtree_control" file.
  127. In the root cgroup, this lists controllers which are not bound to
  128. other hierarchies and the content changes as controllers are bound to
  129. and unbound from other hierarchies.
  130. In non-root cgroups, the content of this file equals that of the
  131. parent's "cgroup.subtree_control" file as only controllers enabled
  132. from the parent can be used in its children.
  133. 3. Structural Constraints
  134. 3-1. Top-down
  135. As it doesn't make sense to nest control of an uncontrolled resource,
  136. all non-root "cgroup.subtree_control" files can only contain
  137. controllers which are enabled in the parent's "cgroup.subtree_control"
  138. file. A controller can be enabled only if the parent has the
  139. controller enabled and a controller can't be disabled if one or more
  140. children have it enabled.
  141. 3-2. No internal tasks
  142. One long-standing issue that cgroup faces is the competition between
  143. tasks belonging to the parent cgroup and its children cgroups. This
  144. is inherently nasty as two different types of entities compete and
  145. there is no agreed-upon obvious way to handle it. Different
  146. controllers are doing different things.
  147. The cpu controller considers tasks and cgroups as equivalents and maps
  148. nice levels to cgroup weights. This works for some cases but falls
  149. flat when children should be allocated specific ratios of CPU cycles
  150. and the number of internal tasks fluctuates - the ratios constantly
  151. change as the number of competing entities fluctuates. There also are
  152. other issues. The mapping from nice level to weight isn't obvious or
  153. universal, and there are various other knobs which simply aren't
  154. available for tasks.
  155. The blkio controller implicitly creates a hidden leaf node for each
  156. cgroup to host the tasks. The hidden leaf has its own copies of all
  157. the knobs with "leaf_" prefixed. While this allows equivalent control
  158. over internal tasks, it's with serious drawbacks. It always adds an
  159. extra layer of nesting which may not be necessary, makes the interface
  160. messy and significantly complicates the implementation.
  161. The memory controller currently doesn't have a way to control what
  162. happens between internal tasks and child cgroups and the behavior is
  163. not clearly defined. There have been attempts to add ad-hoc behaviors
  164. and knobs to tailor the behavior to specific workloads. Continuing
  165. this direction will lead to problems which will be extremely difficult
  166. to resolve in the long term.
  167. Multiple controllers struggle with internal tasks and came up with
  168. different ways to deal with it; unfortunately, all the approaches in
  169. use now are severely flawed and, furthermore, the widely different
  170. behaviors make cgroup as whole highly inconsistent.
  171. It is clear that this is something which needs to be addressed from
  172. cgroup core proper in a uniform way so that controllers don't need to
  173. worry about it and cgroup as a whole shows a consistent and logical
  174. behavior. To achieve that, unified hierarchy enforces the following
  175. structural constraint:
  176. Except for the root, only cgroups which don't contain any task may
  177. have controllers enabled in their "cgroup.subtree_control" files.
  178. Combined with other properties, this guarantees that, when a
  179. controller is looking at the part of the hierarchy which has it
  180. enabled, tasks are always only on the leaves. This rules out
  181. situations where child cgroups compete against internal tasks of the
  182. parent.
  183. There are two things to note. Firstly, the root cgroup is exempt from
  184. the restriction. Root contains tasks and anonymous resource
  185. consumption which can't be associated with any other cgroup and
  186. requires special treatment from most controllers. How resource
  187. consumption in the root cgroup is governed is up to each controller.
  188. Secondly, the restriction doesn't take effect if there is no enabled
  189. controller in the cgroup's "cgroup.subtree_control" file. This is
  190. important as otherwise it wouldn't be possible to create children of a
  191. populated cgroup. To control resource distribution of a cgroup, the
  192. cgroup must create children and transfer all its tasks to the children
  193. before enabling controllers in its "cgroup.subtree_control" file.
  194. 4. Other Changes
  195. 4-1. [Un]populated Notification
  196. cgroup users often need a way to determine when a cgroup's
  197. subhierarchy becomes empty so that it can be cleaned up. cgroup
  198. currently provides release_agent for it; unfortunately, this mechanism
  199. is riddled with issues.
  200. - It delivers events by forking and execing a userland binary
  201. specified as the release_agent. This is a long deprecated method of
  202. notification delivery. It's extremely heavy, slow and cumbersome to
  203. integrate with larger infrastructure.
  204. - There is single monitoring point at the root. There's no way to
  205. delegate management of a subtree.
  206. - The event isn't recursive. It triggers when a cgroup doesn't have
  207. any tasks or child cgroups. Events for internal nodes trigger only
  208. after all children are removed. This again makes it impossible to
  209. delegate management of a subtree.
  210. - Events are filtered from the kernel side. A "notify_on_release"
  211. file is used to subscribe to or suppress release events. This is
  212. unnecessarily complicated and probably done this way because event
  213. delivery itself was expensive.
  214. Unified hierarchy implements an interface file "cgroup.populated"
  215. which can be used to monitor whether the cgroup's subhierarchy has
  216. tasks in it or not. Its value is 0 if there is no task in the cgroup
  217. and its descendants; otherwise, 1. poll and [id]notify events are
  218. triggered when the value changes.
  219. This is significantly lighter and simpler and trivially allows
  220. delegating management of subhierarchy - subhierarchy monitoring can
  221. block further propagation simply by putting itself or another process
  222. in the subhierarchy and monitor events that it's interested in from
  223. there without interfering with monitoring higher in the tree.
  224. In unified hierarchy, the release_agent mechanism is no longer
  225. supported and the interface files "release_agent" and
  226. "notify_on_release" do not exist.
  227. 4-2. Other Core Changes
  228. - None of the mount options is allowed.
  229. - remount is disallowed.
  230. - rename(2) is disallowed.
  231. - The "tasks" file is removed. Everything should at process
  232. granularity. Use the "cgroup.procs" file instead.
  233. - The "cgroup.procs" file is not sorted. pids will be unique unless
  234. they got recycled in-between reads.
  235. - The "cgroup.clone_children" file is removed.
  236. 4-3. Per-Controller Changes
  237. 4-3-1. blkio
  238. - blk-throttle becomes properly hierarchical.
  239. 4-3-2. cpuset
  240. - Tasks are kept in empty cpusets after hotplug and take on the masks
  241. of the nearest non-empty ancestor, instead of being moved to it.
  242. - A task can be moved into an empty cpuset, and again it takes on the
  243. masks of the nearest non-empty ancestor.
  244. 4-3-3. memory
  245. - use_hierarchy is on by default and the cgroup file for the flag is
  246. not created.
  247. 5. Planned Changes
  248. 5-1. CAP for resource control
  249. Unified hierarchy will require one of the capabilities(7), which is
  250. yet to be decided, for all resource control related knobs. Process
  251. organization operations - creation of sub-cgroups and migration of
  252. processes in sub-hierarchies may be delegated by changing the
  253. ownership and/or permissions on the cgroup directory and
  254. "cgroup.procs" interface file; however, all operations which affect
  255. resource control - writes to a "cgroup.subtree_control" file or any
  256. controller-specific knobs - will require an explicit CAP privilege.
  257. This, in part, is to prevent the cgroup interface from being
  258. inadvertently promoted to programmable API used by non-privileged
  259. binaries. cgroup exposes various aspects of the system in ways which
  260. aren't properly abstracted for direct consumption by regular programs.
  261. This is an administration interface much closer to sysctl knobs than
  262. system calls. Even the basic access model, being filesystem path
  263. based, isn't suitable for direct consumption. There's no way to
  264. access "my cgroup" in a race-free way or make multiple operations
  265. atomic against migration to another cgroup.
  266. Another aspect is that, for better or for worse, the cgroup interface
  267. goes through far less scrutiny than regular interfaces for
  268. unprivileged userland. The upside is that cgroup is able to expose
  269. useful features which may not be suitable for general consumption in a
  270. reasonable time frame. It provides a relatively short path between
  271. internal details and userland-visible interface. Of course, this
  272. shortcut comes with high risk. We go through what we go through for
  273. general kernel APIs for good reasons. It may end up leaking internal
  274. details in a way which can exert significant pain by locking the
  275. kernel into a contract that can't be maintained in a reasonable
  276. manner.
  277. Also, due to the specific nature, cgroup and its controllers don't
  278. tend to attract attention from a wide scope of developers. cgroup's
  279. short history is already fraught with severely mis-designed
  280. interfaces, unnecessary commitments to and exposing of internal
  281. details, broken and dangerous implementations of various features.
  282. Keeping cgroup as an administration interface is both advantageous for
  283. its role and imperative given its nature. Some of the cgroup features
  284. may make sense for unprivileged access. If deemed justified, those
  285. must be further abstracted and implemented as a different interface,
  286. be it a system call or process-private filesystem, and survive through
  287. the scrutiny that any interface for general consumption is required to
  288. go through.
  289. Requiring CAP is not a complete solution but should serve as a
  290. significant deterrent against spraying cgroup usages in non-privileged
  291. programs.