cgroup-v2.rst 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040
  1. ================
  2. Control Group v2
  3. ================
  4. :Date: October, 2015
  5. :Author: Tejun Heo <tj@kernel.org>
  6. This is the authoritative documentation on the design, interface and
  7. conventions of cgroup v2. It describes all userland-visible aspects
  8. of cgroup including core and specific controller behaviors. All
  9. future changes must be reflected in this document. Documentation for
  10. v1 is available under Documentation/cgroup-v1/.
  11. .. CONTENTS
  12. 1. Introduction
  13. 1-1. Terminology
  14. 1-2. What is cgroup?
  15. 2. Basic Operations
  16. 2-1. Mounting
  17. 2-2. Organizing Processes and Threads
  18. 2-2-1. Processes
  19. 2-2-2. Threads
  20. 2-3. [Un]populated Notification
  21. 2-4. Controlling Controllers
  22. 2-4-1. Enabling and Disabling
  23. 2-4-2. Top-down Constraint
  24. 2-4-3. No Internal Process Constraint
  25. 2-5. Delegation
  26. 2-5-1. Model of Delegation
  27. 2-5-2. Delegation Containment
  28. 2-6. Guidelines
  29. 2-6-1. Organize Once and Control
  30. 2-6-2. Avoid Name Collisions
  31. 3. Resource Distribution Models
  32. 3-1. Weights
  33. 3-2. Limits
  34. 3-3. Protections
  35. 3-4. Allocations
  36. 4. Interface Files
  37. 4-1. Format
  38. 4-2. Conventions
  39. 4-3. Core Interface Files
  40. 5. Controllers
  41. 5-1. CPU
  42. 5-1-1. CPU Interface Files
  43. 5-2. Memory
  44. 5-2-1. Memory Interface Files
  45. 5-2-2. Usage Guidelines
  46. 5-2-3. Memory Ownership
  47. 5-3. IO
  48. 5-3-1. IO Interface Files
  49. 5-3-2. Writeback
  50. 5-4. PID
  51. 5-4-1. PID Interface Files
  52. 5-5. Device
  53. 5-6. RDMA
  54. 5-6-1. RDMA Interface Files
  55. 5-7. Misc
  56. 5-7-1. perf_event
  57. 5-N. Non-normative information
  58. 5-N-1. CPU controller root cgroup process behaviour
  59. 5-N-2. IO controller root cgroup process behaviour
  60. 6. Namespace
  61. 6-1. Basics
  62. 6-2. The Root and Views
  63. 6-3. Migration and setns(2)
  64. 6-4. Interaction with Other Namespaces
  65. P. Information on Kernel Programming
  66. P-1. Filesystem Support for Writeback
  67. D. Deprecated v1 Core Features
  68. R. Issues with v1 and Rationales for v2
  69. R-1. Multiple Hierarchies
  70. R-2. Thread Granularity
  71. R-3. Competition Between Inner Nodes and Threads
  72. R-4. Other Interface Issues
  73. R-5. Controller Issues and Remedies
  74. R-5-1. Memory
  75. Introduction
  76. ============
  77. Terminology
  78. -----------
  79. "cgroup" stands for "control group" and is never capitalized. The
  80. singular form is used to designate the whole feature and also as a
  81. qualifier as in "cgroup controllers". When explicitly referring to
  82. multiple individual control groups, the plural form "cgroups" is used.
  83. What is cgroup?
  84. ---------------
  85. cgroup is a mechanism to organize processes hierarchically and
  86. distribute system resources along the hierarchy in a controlled and
  87. configurable manner.
  88. cgroup is largely composed of two parts - the core and controllers.
  89. cgroup core is primarily responsible for hierarchically organizing
  90. processes. A cgroup controller is usually responsible for
  91. distributing a specific type of system resource along the hierarchy
  92. although there are utility controllers which serve purposes other than
  93. resource distribution.
  94. cgroups form a tree structure and every process in the system belongs
  95. to one and only one cgroup. All threads of a process belong to the
  96. same cgroup. On creation, all processes are put in the cgroup that
  97. the parent process belongs to at the time. A process can be migrated
  98. to another cgroup. Migration of a process doesn't affect already
  99. existing descendant processes.
  100. Following certain structural constraints, controllers may be enabled or
  101. disabled selectively on a cgroup. All controller behaviors are
  102. hierarchical - if a controller is enabled on a cgroup, it affects all
  103. processes which belong to the cgroups consisting the inclusive
  104. sub-hierarchy of the cgroup. When a controller is enabled on a nested
  105. cgroup, it always restricts the resource distribution further. The
  106. restrictions set closer to the root in the hierarchy can not be
  107. overridden from further away.
  108. Basic Operations
  109. ================
  110. Mounting
  111. --------
  112. Unlike v1, cgroup v2 has only single hierarchy. The cgroup v2
  113. hierarchy can be mounted with the following mount command::
  114. # mount -t cgroup2 none $MOUNT_POINT
  115. cgroup2 filesystem has the magic number 0x63677270 ("cgrp"). All
  116. controllers which support v2 and are not bound to a v1 hierarchy are
  117. automatically bound to the v2 hierarchy and show up at the root.
  118. Controllers which are not in active use in the v2 hierarchy can be
  119. bound to other hierarchies. This allows mixing v2 hierarchy with the
  120. legacy v1 multiple hierarchies in a fully backward compatible way.
  121. A controller can be moved across hierarchies only after the controller
  122. is no longer referenced in its current hierarchy. Because per-cgroup
  123. controller states are destroyed asynchronously and controllers may
  124. have lingering references, a controller may not show up immediately on
  125. the v2 hierarchy after the final umount of the previous hierarchy.
  126. Similarly, a controller should be fully disabled to be moved out of
  127. the unified hierarchy and it may take some time for the disabled
  128. controller to become available for other hierarchies; furthermore, due
  129. to inter-controller dependencies, other controllers may need to be
  130. disabled too.
  131. While useful for development and manual configurations, moving
  132. controllers dynamically between the v2 and other hierarchies is
  133. strongly discouraged for production use. It is recommended to decide
  134. the hierarchies and controller associations before starting using the
  135. controllers after system boot.
  136. During transition to v2, system management software might still
  137. automount the v1 cgroup filesystem and so hijack all controllers
  138. during boot, before manual intervention is possible. To make testing
  139. and experimenting easier, the kernel parameter cgroup_no_v1= allows
  140. disabling controllers in v1 and make them always available in v2.
  141. cgroup v2 currently supports the following mount options.
  142. nsdelegate
  143. Consider cgroup namespaces as delegation boundaries. This
  144. option is system wide and can only be set on mount or modified
  145. through remount from the init namespace. The mount option is
  146. ignored on non-init namespace mounts. Please refer to the
  147. Delegation section for details.
  148. Organizing Processes and Threads
  149. --------------------------------
  150. Processes
  151. ~~~~~~~~~
  152. Initially, only the root cgroup exists to which all processes belong.
  153. A child cgroup can be created by creating a sub-directory::
  154. # mkdir $CGROUP_NAME
  155. A given cgroup may have multiple child cgroups forming a tree
  156. structure. Each cgroup has a read-writable interface file
  157. "cgroup.procs". When read, it lists the PIDs of all processes which
  158. belong to the cgroup one-per-line. The PIDs are not ordered and the
  159. same PID may show up more than once if the process got moved to
  160. another cgroup and then back or the PID got recycled while reading.
  161. A process can be migrated into a cgroup by writing its PID to the
  162. target cgroup's "cgroup.procs" file. Only one process can be migrated
  163. on a single write(2) call. If a process is composed of multiple
  164. threads, writing the PID of any thread migrates all threads of the
  165. process.
  166. When a process forks a child process, the new process is born into the
  167. cgroup that the forking process belongs to at the time of the
  168. operation. After exit, a process stays associated with the cgroup
  169. that it belonged to at the time of exit until it's reaped; however, a
  170. zombie process does not appear in "cgroup.procs" and thus can't be
  171. moved to another cgroup.
  172. A cgroup which doesn't have any children or live processes can be
  173. destroyed by removing the directory. Note that a cgroup which doesn't
  174. have any children and is associated only with zombie processes is
  175. considered empty and can be removed::
  176. # rmdir $CGROUP_NAME
  177. "/proc/$PID/cgroup" lists a process's cgroup membership. If legacy
  178. cgroup is in use in the system, this file may contain multiple lines,
  179. one for each hierarchy. The entry for cgroup v2 is always in the
  180. format "0::$PATH"::
  181. # cat /proc/842/cgroup
  182. ...
  183. 0::/test-cgroup/test-cgroup-nested
  184. If the process becomes a zombie and the cgroup it was associated with
  185. is removed subsequently, " (deleted)" is appended to the path::
  186. # cat /proc/842/cgroup
  187. ...
  188. 0::/test-cgroup/test-cgroup-nested (deleted)
  189. Threads
  190. ~~~~~~~
  191. cgroup v2 supports thread granularity for a subset of controllers to
  192. support use cases requiring hierarchical resource distribution across
  193. the threads of a group of processes. By default, all threads of a
  194. process belong to the same cgroup, which also serves as the resource
  195. domain to host resource consumptions which are not specific to a
  196. process or thread. The thread mode allows threads to be spread across
  197. a subtree while still maintaining the common resource domain for them.
  198. Controllers which support thread mode are called threaded controllers.
  199. The ones which don't are called domain controllers.
  200. Marking a cgroup threaded makes it join the resource domain of its
  201. parent as a threaded cgroup. The parent may be another threaded
  202. cgroup whose resource domain is further up in the hierarchy. The root
  203. of a threaded subtree, that is, the nearest ancestor which is not
  204. threaded, is called threaded domain or thread root interchangeably and
  205. serves as the resource domain for the entire subtree.
  206. Inside a threaded subtree, threads of a process can be put in
  207. different cgroups and are not subject to the no internal process
  208. constraint - threaded controllers can be enabled on non-leaf cgroups
  209. whether they have threads in them or not.
  210. As the threaded domain cgroup hosts all the domain resource
  211. consumptions of the subtree, it is considered to have internal
  212. resource consumptions whether there are processes in it or not and
  213. can't have populated child cgroups which aren't threaded. Because the
  214. root cgroup is not subject to no internal process constraint, it can
  215. serve both as a threaded domain and a parent to domain cgroups.
  216. The current operation mode or type of the cgroup is shown in the
  217. "cgroup.type" file which indicates whether the cgroup is a normal
  218. domain, a domain which is serving as the domain of a threaded subtree,
  219. or a threaded cgroup.
  220. On creation, a cgroup is always a domain cgroup and can be made
  221. threaded by writing "threaded" to the "cgroup.type" file. The
  222. operation is single direction::
  223. # echo threaded > cgroup.type
  224. Once threaded, the cgroup can't be made a domain again. To enable the
  225. thread mode, the following conditions must be met.
  226. - As the cgroup will join the parent's resource domain. The parent
  227. must either be a valid (threaded) domain or a threaded cgroup.
  228. - When the parent is an unthreaded domain, it must not have any domain
  229. controllers enabled or populated domain children. The root is
  230. exempt from this requirement.
  231. Topology-wise, a cgroup can be in an invalid state. Please consider
  232. the following topology::
  233. A (threaded domain) - B (threaded) - C (domain, just created)
  234. C is created as a domain but isn't connected to a parent which can
  235. host child domains. C can't be used until it is turned into a
  236. threaded cgroup. "cgroup.type" file will report "domain (invalid)" in
  237. these cases. Operations which fail due to invalid topology use
  238. EOPNOTSUPP as the errno.
  239. A domain cgroup is turned into a threaded domain when one of its child
  240. cgroup becomes threaded or threaded controllers are enabled in the
  241. "cgroup.subtree_control" file while there are processes in the cgroup.
  242. A threaded domain reverts to a normal domain when the conditions
  243. clear.
  244. When read, "cgroup.threads" contains the list of the thread IDs of all
  245. threads in the cgroup. Except that the operations are per-thread
  246. instead of per-process, "cgroup.threads" has the same format and
  247. behaves the same way as "cgroup.procs". While "cgroup.threads" can be
  248. written to in any cgroup, as it can only move threads inside the same
  249. threaded domain, its operations are confined inside each threaded
  250. subtree.
  251. The threaded domain cgroup serves as the resource domain for the whole
  252. subtree, and, while the threads can be scattered across the subtree,
  253. all the processes are considered to be in the threaded domain cgroup.
  254. "cgroup.procs" in a threaded domain cgroup contains the PIDs of all
  255. processes in the subtree and is not readable in the subtree proper.
  256. However, "cgroup.procs" can be written to from anywhere in the subtree
  257. to migrate all threads of the matching process to the cgroup.
  258. Only threaded controllers can be enabled in a threaded subtree. When
  259. a threaded controller is enabled inside a threaded subtree, it only
  260. accounts for and controls resource consumptions associated with the
  261. threads in the cgroup and its descendants. All consumptions which
  262. aren't tied to a specific thread belong to the threaded domain cgroup.
  263. Because a threaded subtree is exempt from no internal process
  264. constraint, a threaded controller must be able to handle competition
  265. between threads in a non-leaf cgroup and its child cgroups. Each
  266. threaded controller defines how such competitions are handled.
  267. [Un]populated Notification
  268. --------------------------
  269. Each non-root cgroup has a "cgroup.events" file which contains
  270. "populated" field indicating whether the cgroup's sub-hierarchy has
  271. live processes in it. Its value is 0 if there is no live process in
  272. the cgroup and its descendants; otherwise, 1. poll and [id]notify
  273. events are triggered when the value changes. This can be used, for
  274. example, to start a clean-up operation after all processes of a given
  275. sub-hierarchy have exited. The populated state updates and
  276. notifications are recursive. Consider the following sub-hierarchy
  277. where the numbers in the parentheses represent the numbers of processes
  278. in each cgroup::
  279. A(4) - B(0) - C(1)
  280. \ D(0)
  281. A, B and C's "populated" fields would be 1 while D's 0. After the one
  282. process in C exits, B and C's "populated" fields would flip to "0" and
  283. file modified events will be generated on the "cgroup.events" files of
  284. both cgroups.
  285. Controlling Controllers
  286. -----------------------
  287. Enabling and Disabling
  288. ~~~~~~~~~~~~~~~~~~~~~~
  289. Each cgroup has a "cgroup.controllers" file which lists all
  290. controllers available for the cgroup to enable::
  291. # cat cgroup.controllers
  292. cpu io memory
  293. No controller is enabled by default. Controllers can be enabled and
  294. disabled by writing to the "cgroup.subtree_control" file::
  295. # echo "+cpu +memory -io" > cgroup.subtree_control
  296. Only controllers which are listed in "cgroup.controllers" can be
  297. enabled. When multiple operations are specified as above, either they
  298. all succeed or fail. If multiple operations on the same controller
  299. are specified, the last one is effective.
  300. Enabling a controller in a cgroup indicates that the distribution of
  301. the target resource across its immediate children will be controlled.
  302. Consider the following sub-hierarchy. The enabled controllers are
  303. listed in parentheses::
  304. A(cpu,memory) - B(memory) - C()
  305. \ D()
  306. As A has "cpu" and "memory" enabled, A will control the distribution
  307. of CPU cycles and memory to its children, in this case, B. As B has
  308. "memory" enabled but not "CPU", C and D will compete freely on CPU
  309. cycles but their division of memory available to B will be controlled.
  310. As a controller regulates the distribution of the target resource to
  311. the cgroup's children, enabling it creates the controller's interface
  312. files in the child cgroups. In the above example, enabling "cpu" on B
  313. would create the "cpu." prefixed controller interface files in C and
  314. D. Likewise, disabling "memory" from B would remove the "memory."
  315. prefixed controller interface files from C and D. This means that the
  316. controller interface files - anything which doesn't start with
  317. "cgroup." are owned by the parent rather than the cgroup itself.
  318. Top-down Constraint
  319. ~~~~~~~~~~~~~~~~~~~
  320. Resources are distributed top-down and a cgroup can further distribute
  321. a resource only if the resource has been distributed to it from the
  322. parent. This means that all non-root "cgroup.subtree_control" files
  323. can only contain controllers which are enabled in the parent's
  324. "cgroup.subtree_control" file. A controller can be enabled only if
  325. the parent has the controller enabled and a controller can't be
  326. disabled if one or more children have it enabled.
  327. No Internal Process Constraint
  328. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  329. Non-root cgroups can distribute domain resources to their children
  330. only when they don't have any processes of their own. In other words,
  331. only domain cgroups which don't contain any processes can have domain
  332. controllers enabled in their "cgroup.subtree_control" files.
  333. This guarantees that, when a domain controller is looking at the part
  334. of the hierarchy which has it enabled, processes are always only on
  335. the leaves. This rules out situations where child cgroups compete
  336. against internal processes of the parent.
  337. The root cgroup is exempt from this restriction. Root contains
  338. processes and anonymous resource consumption which can't be associated
  339. with any other cgroups and requires special treatment from most
  340. controllers. How resource consumption in the root cgroup is governed
  341. is up to each controller (for more information on this topic please
  342. refer to the Non-normative information section in the Controllers
  343. chapter).
  344. Note that the restriction doesn't get in the way if there is no
  345. enabled controller in the cgroup's "cgroup.subtree_control". This is
  346. important as otherwise it wouldn't be possible to create children of a
  347. populated cgroup. To control resource distribution of a cgroup, the
  348. cgroup must create children and transfer all its processes to the
  349. children before enabling controllers in its "cgroup.subtree_control"
  350. file.
  351. Delegation
  352. ----------
  353. Model of Delegation
  354. ~~~~~~~~~~~~~~~~~~~
  355. A cgroup can be delegated in two ways. First, to a less privileged
  356. user by granting write access of the directory and its "cgroup.procs",
  357. "cgroup.threads" and "cgroup.subtree_control" files to the user.
  358. Second, if the "nsdelegate" mount option is set, automatically to a
  359. cgroup namespace on namespace creation.
  360. Because the resource control interface files in a given directory
  361. control the distribution of the parent's resources, the delegatee
  362. shouldn't be allowed to write to them. For the first method, this is
  363. achieved by not granting access to these files. For the second, the
  364. kernel rejects writes to all files other than "cgroup.procs" and
  365. "cgroup.subtree_control" on a namespace root from inside the
  366. namespace.
  367. The end results are equivalent for both delegation types. Once
  368. delegated, the user can build sub-hierarchy under the directory,
  369. organize processes inside it as it sees fit and further distribute the
  370. resources it received from the parent. The limits and other settings
  371. of all resource controllers are hierarchical and regardless of what
  372. happens in the delegated sub-hierarchy, nothing can escape the
  373. resource restrictions imposed by the parent.
  374. Currently, cgroup doesn't impose any restrictions on the number of
  375. cgroups in or nesting depth of a delegated sub-hierarchy; however,
  376. this may be limited explicitly in the future.
  377. Delegation Containment
  378. ~~~~~~~~~~~~~~~~~~~~~~
  379. A delegated sub-hierarchy is contained in the sense that processes
  380. can't be moved into or out of the sub-hierarchy by the delegatee.
  381. For delegations to a less privileged user, this is achieved by
  382. requiring the following conditions for a process with a non-root euid
  383. to migrate a target process into a cgroup by writing its PID to the
  384. "cgroup.procs" file.
  385. - The writer must have write access to the "cgroup.procs" file.
  386. - The writer must have write access to the "cgroup.procs" file of the
  387. common ancestor of the source and destination cgroups.
  388. The above two constraints ensure that while a delegatee may migrate
  389. processes around freely in the delegated sub-hierarchy it can't pull
  390. in from or push out to outside the sub-hierarchy.
  391. For an example, let's assume cgroups C0 and C1 have been delegated to
  392. user U0 who created C00, C01 under C0 and C10 under C1 as follows and
  393. all processes under C0 and C1 belong to U0::
  394. ~~~~~~~~~~~~~ - C0 - C00
  395. ~ cgroup ~ \ C01
  396. ~ hierarchy ~
  397. ~~~~~~~~~~~~~ - C1 - C10
  398. Let's also say U0 wants to write the PID of a process which is
  399. currently in C10 into "C00/cgroup.procs". U0 has write access to the
  400. file; however, the common ancestor of the source cgroup C10 and the
  401. destination cgroup C00 is above the points of delegation and U0 would
  402. not have write access to its "cgroup.procs" files and thus the write
  403. will be denied with -EACCES.
  404. For delegations to namespaces, containment is achieved by requiring
  405. that both the source and destination cgroups are reachable from the
  406. namespace of the process which is attempting the migration. If either
  407. is not reachable, the migration is rejected with -ENOENT.
  408. Guidelines
  409. ----------
  410. Organize Once and Control
  411. ~~~~~~~~~~~~~~~~~~~~~~~~~
  412. Migrating a process across cgroups is a relatively expensive operation
  413. and stateful resources such as memory are not moved together with the
  414. process. This is an explicit design decision as there often exist
  415. inherent trade-offs between migration and various hot paths in terms
  416. of synchronization cost.
  417. As such, migrating processes across cgroups frequently as a means to
  418. apply different resource restrictions is discouraged. A workload
  419. should be assigned to a cgroup according to the system's logical and
  420. resource structure once on start-up. Dynamic adjustments to resource
  421. distribution can be made by changing controller configuration through
  422. the interface files.
  423. Avoid Name Collisions
  424. ~~~~~~~~~~~~~~~~~~~~~
  425. Interface files for a cgroup and its children cgroups occupy the same
  426. directory and it is possible to create children cgroups which collide
  427. with interface files.
  428. All cgroup core interface files are prefixed with "cgroup." and each
  429. controller's interface files are prefixed with the controller name and
  430. a dot. A controller's name is composed of lower case alphabets and
  431. '_'s but never begins with an '_' so it can be used as the prefix
  432. character for collision avoidance. Also, interface file names won't
  433. start or end with terms which are often used in categorizing workloads
  434. such as job, service, slice, unit or workload.
  435. cgroup doesn't do anything to prevent name collisions and it's the
  436. user's responsibility to avoid them.
  437. Resource Distribution Models
  438. ============================
  439. cgroup controllers implement several resource distribution schemes
  440. depending on the resource type and expected use cases. This section
  441. describes major schemes in use along with their expected behaviors.
  442. Weights
  443. -------
  444. A parent's resource is distributed by adding up the weights of all
  445. active children and giving each the fraction matching the ratio of its
  446. weight against the sum. As only children which can make use of the
  447. resource at the moment participate in the distribution, this is
  448. work-conserving. Due to the dynamic nature, this model is usually
  449. used for stateless resources.
  450. All weights are in the range [1, 10000] with the default at 100. This
  451. allows symmetric multiplicative biases in both directions at fine
  452. enough granularity while staying in the intuitive range.
  453. As long as the weight is in range, all configuration combinations are
  454. valid and there is no reason to reject configuration changes or
  455. process migrations.
  456. "cpu.weight" proportionally distributes CPU cycles to active children
  457. and is an example of this type.
  458. Limits
  459. ------
  460. A child can only consume upto the configured amount of the resource.
  461. Limits can be over-committed - the sum of the limits of children can
  462. exceed the amount of resource available to the parent.
  463. Limits are in the range [0, max] and defaults to "max", which is noop.
  464. As limits can be over-committed, all configuration combinations are
  465. valid and there is no reason to reject configuration changes or
  466. process migrations.
  467. "io.max" limits the maximum BPS and/or IOPS that a cgroup can consume
  468. on an IO device and is an example of this type.
  469. Protections
  470. -----------
  471. A cgroup is protected to be allocated upto the configured amount of
  472. the resource if the usages of all its ancestors are under their
  473. protected levels. Protections can be hard guarantees or best effort
  474. soft boundaries. Protections can also be over-committed in which case
  475. only upto the amount available to the parent is protected among
  476. children.
  477. Protections are in the range [0, max] and defaults to 0, which is
  478. noop.
  479. As protections can be over-committed, all configuration combinations
  480. are valid and there is no reason to reject configuration changes or
  481. process migrations.
  482. "memory.low" implements best-effort memory protection and is an
  483. example of this type.
  484. Allocations
  485. -----------
  486. A cgroup is exclusively allocated a certain amount of a finite
  487. resource. Allocations can't be over-committed - the sum of the
  488. allocations of children can not exceed the amount of resource
  489. available to the parent.
  490. Allocations are in the range [0, max] and defaults to 0, which is no
  491. resource.
  492. As allocations can't be over-committed, some configuration
  493. combinations are invalid and should be rejected. Also, if the
  494. resource is mandatory for execution of processes, process migrations
  495. may be rejected.
  496. "cpu.rt.max" hard-allocates realtime slices and is an example of this
  497. type.
  498. Interface Files
  499. ===============
  500. Format
  501. ------
  502. All interface files should be in one of the following formats whenever
  503. possible::
  504. New-line separated values
  505. (when only one value can be written at once)
  506. VAL0\n
  507. VAL1\n
  508. ...
  509. Space separated values
  510. (when read-only or multiple values can be written at once)
  511. VAL0 VAL1 ...\n
  512. Flat keyed
  513. KEY0 VAL0\n
  514. KEY1 VAL1\n
  515. ...
  516. Nested keyed
  517. KEY0 SUB_KEY0=VAL00 SUB_KEY1=VAL01...
  518. KEY1 SUB_KEY0=VAL10 SUB_KEY1=VAL11...
  519. ...
  520. For a writable file, the format for writing should generally match
  521. reading; however, controllers may allow omitting later fields or
  522. implement restricted shortcuts for most common use cases.
  523. For both flat and nested keyed files, only the values for a single key
  524. can be written at a time. For nested keyed files, the sub key pairs
  525. may be specified in any order and not all pairs have to be specified.
  526. Conventions
  527. -----------
  528. - Settings for a single feature should be contained in a single file.
  529. - The root cgroup should be exempt from resource control and thus
  530. shouldn't have resource control interface files. Also,
  531. informational files on the root cgroup which end up showing global
  532. information available elsewhere shouldn't exist.
  533. - If a controller implements weight based resource distribution, its
  534. interface file should be named "weight" and have the range [1,
  535. 10000] with 100 as the default. The values are chosen to allow
  536. enough and symmetric bias in both directions while keeping it
  537. intuitive (the default is 100%).
  538. - If a controller implements an absolute resource guarantee and/or
  539. limit, the interface files should be named "min" and "max"
  540. respectively. If a controller implements best effort resource
  541. guarantee and/or limit, the interface files should be named "low"
  542. and "high" respectively.
  543. In the above four control files, the special token "max" should be
  544. used to represent upward infinity for both reading and writing.
  545. - If a setting has a configurable default value and keyed specific
  546. overrides, the default entry should be keyed with "default" and
  547. appear as the first entry in the file.
  548. The default value can be updated by writing either "default $VAL" or
  549. "$VAL".
  550. When writing to update a specific override, "default" can be used as
  551. the value to indicate removal of the override. Override entries
  552. with "default" as the value must not appear when read.
  553. For example, a setting which is keyed by major:minor device numbers
  554. with integer values may look like the following::
  555. # cat cgroup-example-interface-file
  556. default 150
  557. 8:0 300
  558. The default value can be updated by::
  559. # echo 125 > cgroup-example-interface-file
  560. or::
  561. # echo "default 125" > cgroup-example-interface-file
  562. An override can be set by::
  563. # echo "8:16 170" > cgroup-example-interface-file
  564. and cleared by::
  565. # echo "8:0 default" > cgroup-example-interface-file
  566. # cat cgroup-example-interface-file
  567. default 125
  568. 8:16 170
  569. - For events which are not very high frequency, an interface file
  570. "events" should be created which lists event key value pairs.
  571. Whenever a notifiable event happens, file modified event should be
  572. generated on the file.
  573. Core Interface Files
  574. --------------------
  575. All cgroup core files are prefixed with "cgroup."
  576. cgroup.type
  577. A read-write single value file which exists on non-root
  578. cgroups.
  579. When read, it indicates the current type of the cgroup, which
  580. can be one of the following values.
  581. - "domain" : A normal valid domain cgroup.
  582. - "domain threaded" : A threaded domain cgroup which is
  583. serving as the root of a threaded subtree.
  584. - "domain invalid" : A cgroup which is in an invalid state.
  585. It can't be populated or have controllers enabled. It may
  586. be allowed to become a threaded cgroup.
  587. - "threaded" : A threaded cgroup which is a member of a
  588. threaded subtree.
  589. A cgroup can be turned into a threaded cgroup by writing
  590. "threaded" to this file.
  591. cgroup.procs
  592. A read-write new-line separated values file which exists on
  593. all cgroups.
  594. When read, it lists the PIDs of all processes which belong to
  595. the cgroup one-per-line. The PIDs are not ordered and the
  596. same PID may show up more than once if the process got moved
  597. to another cgroup and then back or the PID got recycled while
  598. reading.
  599. A PID can be written to migrate the process associated with
  600. the PID to the cgroup. The writer should match all of the
  601. following conditions.
  602. - It must have write access to the "cgroup.procs" file.
  603. - It must have write access to the "cgroup.procs" file of the
  604. common ancestor of the source and destination cgroups.
  605. When delegating a sub-hierarchy, write access to this file
  606. should be granted along with the containing directory.
  607. In a threaded cgroup, reading this file fails with EOPNOTSUPP
  608. as all the processes belong to the thread root. Writing is
  609. supported and moves every thread of the process to the cgroup.
  610. cgroup.threads
  611. A read-write new-line separated values file which exists on
  612. all cgroups.
  613. When read, it lists the TIDs of all threads which belong to
  614. the cgroup one-per-line. The TIDs are not ordered and the
  615. same TID may show up more than once if the thread got moved to
  616. another cgroup and then back or the TID got recycled while
  617. reading.
  618. A TID can be written to migrate the thread associated with the
  619. TID to the cgroup. The writer should match all of the
  620. following conditions.
  621. - It must have write access to the "cgroup.threads" file.
  622. - The cgroup that the thread is currently in must be in the
  623. same resource domain as the destination cgroup.
  624. - It must have write access to the "cgroup.procs" file of the
  625. common ancestor of the source and destination cgroups.
  626. When delegating a sub-hierarchy, write access to this file
  627. should be granted along with the containing directory.
  628. cgroup.controllers
  629. A read-only space separated values file which exists on all
  630. cgroups.
  631. It shows space separated list of all controllers available to
  632. the cgroup. The controllers are not ordered.
  633. cgroup.subtree_control
  634. A read-write space separated values file which exists on all
  635. cgroups. Starts out empty.
  636. When read, it shows space separated list of the controllers
  637. which are enabled to control resource distribution from the
  638. cgroup to its children.
  639. Space separated list of controllers prefixed with '+' or '-'
  640. can be written to enable or disable controllers. A controller
  641. name prefixed with '+' enables the controller and '-'
  642. disables. If a controller appears more than once on the list,
  643. the last one is effective. When multiple enable and disable
  644. operations are specified, either all succeed or all fail.
  645. cgroup.events
  646. A read-only flat-keyed file which exists on non-root cgroups.
  647. The following entries are defined. Unless specified
  648. otherwise, a value change in this file generates a file
  649. modified event.
  650. populated
  651. 1 if the cgroup or its descendants contains any live
  652. processes; otherwise, 0.
  653. cgroup.max.descendants
  654. A read-write single value files. The default is "max".
  655. Maximum allowed number of descent cgroups.
  656. If the actual number of descendants is equal or larger,
  657. an attempt to create a new cgroup in the hierarchy will fail.
  658. cgroup.max.depth
  659. A read-write single value files. The default is "max".
  660. Maximum allowed descent depth below the current cgroup.
  661. If the actual descent depth is equal or larger,
  662. an attempt to create a new child cgroup will fail.
  663. cgroup.stat
  664. A read-only flat-keyed file with the following entries:
  665. nr_descendants
  666. Total number of visible descendant cgroups.
  667. nr_dying_descendants
  668. Total number of dying descendant cgroups. A cgroup becomes
  669. dying after being deleted by a user. The cgroup will remain
  670. in dying state for some time undefined time (which can depend
  671. on system load) before being completely destroyed.
  672. A process can't enter a dying cgroup under any circumstances,
  673. a dying cgroup can't revive.
  674. A dying cgroup can consume system resources not exceeding
  675. limits, which were active at the moment of cgroup deletion.
  676. Controllers
  677. ===========
  678. CPU
  679. ---
  680. The "cpu" controllers regulates distribution of CPU cycles. This
  681. controller implements weight and absolute bandwidth limit models for
  682. normal scheduling policy and absolute bandwidth allocation model for
  683. realtime scheduling policy.
  684. WARNING: cgroup2 doesn't yet support control of realtime processes and
  685. the cpu controller can only be enabled when all RT processes are in
  686. the root cgroup. Be aware that system management software may already
  687. have placed RT processes into nonroot cgroups during the system boot
  688. process, and these processes may need to be moved to the root cgroup
  689. before the cpu controller can be enabled.
  690. CPU Interface Files
  691. ~~~~~~~~~~~~~~~~~~~
  692. All time durations are in microseconds.
  693. cpu.stat
  694. A read-only flat-keyed file which exists on non-root cgroups.
  695. This file exists whether the controller is enabled or not.
  696. It always reports the following three stats:
  697. - usage_usec
  698. - user_usec
  699. - system_usec
  700. and the following three when the controller is enabled:
  701. - nr_periods
  702. - nr_throttled
  703. - throttled_usec
  704. cpu.weight
  705. A read-write single value file which exists on non-root
  706. cgroups. The default is "100".
  707. The weight in the range [1, 10000].
  708. cpu.weight.nice
  709. A read-write single value file which exists on non-root
  710. cgroups. The default is "0".
  711. The nice value is in the range [-20, 19].
  712. This interface file is an alternative interface for
  713. "cpu.weight" and allows reading and setting weight using the
  714. same values used by nice(2). Because the range is smaller and
  715. granularity is coarser for the nice values, the read value is
  716. the closest approximation of the current weight.
  717. cpu.max
  718. A read-write two value file which exists on non-root cgroups.
  719. The default is "max 100000".
  720. The maximum bandwidth limit. It's in the following format::
  721. $MAX $PERIOD
  722. which indicates that the group may consume upto $MAX in each
  723. $PERIOD duration. "max" for $MAX indicates no limit. If only
  724. one number is written, $MAX is updated.
  725. Memory
  726. ------
  727. The "memory" controller regulates distribution of memory. Memory is
  728. stateful and implements both limit and protection models. Due to the
  729. intertwining between memory usage and reclaim pressure and the
  730. stateful nature of memory, the distribution model is relatively
  731. complex.
  732. While not completely water-tight, all major memory usages by a given
  733. cgroup are tracked so that the total memory consumption can be
  734. accounted and controlled to a reasonable extent. Currently, the
  735. following types of memory usages are tracked.
  736. - Userland memory - page cache and anonymous memory.
  737. - Kernel data structures such as dentries and inodes.
  738. - TCP socket buffers.
  739. The above list may expand in the future for better coverage.
  740. Memory Interface Files
  741. ~~~~~~~~~~~~~~~~~~~~~~
  742. All memory amounts are in bytes. If a value which is not aligned to
  743. PAGE_SIZE is written, the value may be rounded up to the closest
  744. PAGE_SIZE multiple when read back.
  745. memory.current
  746. A read-only single value file which exists on non-root
  747. cgroups.
  748. The total amount of memory currently being used by the cgroup
  749. and its descendants.
  750. memory.min
  751. A read-write single value file which exists on non-root
  752. cgroups. The default is "0".
  753. Hard memory protection. If the memory usage of a cgroup
  754. is within its effective min boundary, the cgroup's memory
  755. won't be reclaimed under any conditions. If there is no
  756. unprotected reclaimable memory available, OOM killer
  757. is invoked.
  758. Effective min boundary is limited by memory.min values of
  759. all ancestor cgroups. If there is memory.min overcommitment
  760. (child cgroup or cgroups are requiring more protected memory
  761. than parent will allow), then each child cgroup will get
  762. the part of parent's protection proportional to its
  763. actual memory usage below memory.min.
  764. Putting more memory than generally available under this
  765. protection is discouraged and may lead to constant OOMs.
  766. If a memory cgroup is not populated with processes,
  767. its memory.min is ignored.
  768. memory.low
  769. A read-write single value file which exists on non-root
  770. cgroups. The default is "0".
  771. Best-effort memory protection. If the memory usage of a
  772. cgroup is within its effective low boundary, the cgroup's
  773. memory won't be reclaimed unless memory can be reclaimed
  774. from unprotected cgroups.
  775. Effective low boundary is limited by memory.low values of
  776. all ancestor cgroups. If there is memory.low overcommitment
  777. (child cgroup or cgroups are requiring more protected memory
  778. than parent will allow), then each child cgroup will get
  779. the part of parent's protection proportional to its
  780. actual memory usage below memory.low.
  781. Putting more memory than generally available under this
  782. protection is discouraged.
  783. memory.high
  784. A read-write single value file which exists on non-root
  785. cgroups. The default is "max".
  786. Memory usage throttle limit. This is the main mechanism to
  787. control memory usage of a cgroup. If a cgroup's usage goes
  788. over the high boundary, the processes of the cgroup are
  789. throttled and put under heavy reclaim pressure.
  790. Going over the high limit never invokes the OOM killer and
  791. under extreme conditions the limit may be breached.
  792. memory.max
  793. A read-write single value file which exists on non-root
  794. cgroups. The default is "max".
  795. Memory usage hard limit. This is the final protection
  796. mechanism. If a cgroup's memory usage reaches this limit and
  797. can't be reduced, the OOM killer is invoked in the cgroup.
  798. Under certain circumstances, the usage may go over the limit
  799. temporarily.
  800. This is the ultimate protection mechanism. As long as the
  801. high limit is used and monitored properly, this limit's
  802. utility is limited to providing the final safety net.
  803. memory.events
  804. A read-only flat-keyed file which exists on non-root cgroups.
  805. The following entries are defined. Unless specified
  806. otherwise, a value change in this file generates a file
  807. modified event.
  808. low
  809. The number of times the cgroup is reclaimed due to
  810. high memory pressure even though its usage is under
  811. the low boundary. This usually indicates that the low
  812. boundary is over-committed.
  813. high
  814. The number of times processes of the cgroup are
  815. throttled and routed to perform direct memory reclaim
  816. because the high memory boundary was exceeded. For a
  817. cgroup whose memory usage is capped by the high limit
  818. rather than global memory pressure, this event's
  819. occurrences are expected.
  820. max
  821. The number of times the cgroup's memory usage was
  822. about to go over the max boundary. If direct reclaim
  823. fails to bring it down, the cgroup goes to OOM state.
  824. oom
  825. The number of time the cgroup's memory usage was
  826. reached the limit and allocation was about to fail.
  827. Depending on context result could be invocation of OOM
  828. killer and retrying allocation or failing allocation.
  829. Failed allocation in its turn could be returned into
  830. userspace as -ENOMEM or silently ignored in cases like
  831. disk readahead. For now OOM in memory cgroup kills
  832. tasks iff shortage has happened inside page fault.
  833. oom_kill
  834. The number of processes belonging to this cgroup
  835. killed by any kind of OOM killer.
  836. memory.stat
  837. A read-only flat-keyed file which exists on non-root cgroups.
  838. This breaks down the cgroup's memory footprint into different
  839. types of memory, type-specific details, and other information
  840. on the state and past events of the memory management system.
  841. All memory amounts are in bytes.
  842. The entries are ordered to be human readable, and new entries
  843. can show up in the middle. Don't rely on items remaining in a
  844. fixed position; use the keys to look up specific values!
  845. anon
  846. Amount of memory used in anonymous mappings such as
  847. brk(), sbrk(), and mmap(MAP_ANONYMOUS)
  848. file
  849. Amount of memory used to cache filesystem data,
  850. including tmpfs and shared memory.
  851. kernel_stack
  852. Amount of memory allocated to kernel stacks.
  853. slab
  854. Amount of memory used for storing in-kernel data
  855. structures.
  856. sock
  857. Amount of memory used in network transmission buffers
  858. shmem
  859. Amount of cached filesystem data that is swap-backed,
  860. such as tmpfs, shm segments, shared anonymous mmap()s
  861. file_mapped
  862. Amount of cached filesystem data mapped with mmap()
  863. file_dirty
  864. Amount of cached filesystem data that was modified but
  865. not yet written back to disk
  866. file_writeback
  867. Amount of cached filesystem data that was modified and
  868. is currently being written back to disk
  869. inactive_anon, active_anon, inactive_file, active_file, unevictable
  870. Amount of memory, swap-backed and filesystem-backed,
  871. on the internal memory management lists used by the
  872. page reclaim algorithm
  873. slab_reclaimable
  874. Part of "slab" that might be reclaimed, such as
  875. dentries and inodes.
  876. slab_unreclaimable
  877. Part of "slab" that cannot be reclaimed on memory
  878. pressure.
  879. pgfault
  880. Total number of page faults incurred
  881. pgmajfault
  882. Number of major page faults incurred
  883. workingset_refault
  884. Number of refaults of previously evicted pages
  885. workingset_activate
  886. Number of refaulted pages that were immediately activated
  887. workingset_nodereclaim
  888. Number of times a shadow node has been reclaimed
  889. pgrefill
  890. Amount of scanned pages (in an active LRU list)
  891. pgscan
  892. Amount of scanned pages (in an inactive LRU list)
  893. pgsteal
  894. Amount of reclaimed pages
  895. pgactivate
  896. Amount of pages moved to the active LRU list
  897. pgdeactivate
  898. Amount of pages moved to the inactive LRU lis
  899. pglazyfree
  900. Amount of pages postponed to be freed under memory pressure
  901. pglazyfreed
  902. Amount of reclaimed lazyfree pages
  903. memory.swap.current
  904. A read-only single value file which exists on non-root
  905. cgroups.
  906. The total amount of swap currently being used by the cgroup
  907. and its descendants.
  908. memory.swap.max
  909. A read-write single value file which exists on non-root
  910. cgroups. The default is "max".
  911. Swap usage hard limit. If a cgroup's swap usage reaches this
  912. limit, anonymous memory of the cgroup will not be swapped out.
  913. memory.swap.events
  914. A read-only flat-keyed file which exists on non-root cgroups.
  915. The following entries are defined. Unless specified
  916. otherwise, a value change in this file generates a file
  917. modified event.
  918. max
  919. The number of times the cgroup's swap usage was about
  920. to go over the max boundary and swap allocation
  921. failed.
  922. fail
  923. The number of times swap allocation failed either
  924. because of running out of swap system-wide or max
  925. limit.
  926. When reduced under the current usage, the existing swap
  927. entries are reclaimed gradually and the swap usage may stay
  928. higher than the limit for an extended period of time. This
  929. reduces the impact on the workload and memory management.
  930. Usage Guidelines
  931. ~~~~~~~~~~~~~~~~
  932. "memory.high" is the main mechanism to control memory usage.
  933. Over-committing on high limit (sum of high limits > available memory)
  934. and letting global memory pressure to distribute memory according to
  935. usage is a viable strategy.
  936. Because breach of the high limit doesn't trigger the OOM killer but
  937. throttles the offending cgroup, a management agent has ample
  938. opportunities to monitor and take appropriate actions such as granting
  939. more memory or terminating the workload.
  940. Determining whether a cgroup has enough memory is not trivial as
  941. memory usage doesn't indicate whether the workload can benefit from
  942. more memory. For example, a workload which writes data received from
  943. network to a file can use all available memory but can also operate as
  944. performant with a small amount of memory. A measure of memory
  945. pressure - how much the workload is being impacted due to lack of
  946. memory - is necessary to determine whether a workload needs more
  947. memory; unfortunately, memory pressure monitoring mechanism isn't
  948. implemented yet.
  949. Memory Ownership
  950. ~~~~~~~~~~~~~~~~
  951. A memory area is charged to the cgroup which instantiated it and stays
  952. charged to the cgroup until the area is released. Migrating a process
  953. to a different cgroup doesn't move the memory usages that it
  954. instantiated while in the previous cgroup to the new cgroup.
  955. A memory area may be used by processes belonging to different cgroups.
  956. To which cgroup the area will be charged is in-deterministic; however,
  957. over time, the memory area is likely to end up in a cgroup which has
  958. enough memory allowance to avoid high reclaim pressure.
  959. If a cgroup sweeps a considerable amount of memory which is expected
  960. to be accessed repeatedly by other cgroups, it may make sense to use
  961. POSIX_FADV_DONTNEED to relinquish the ownership of memory areas
  962. belonging to the affected files to ensure correct memory ownership.
  963. IO
  964. --
  965. The "io" controller regulates the distribution of IO resources. This
  966. controller implements both weight based and absolute bandwidth or IOPS
  967. limit distribution; however, weight based distribution is available
  968. only if cfq-iosched is in use and neither scheme is available for
  969. blk-mq devices.
  970. IO Interface Files
  971. ~~~~~~~~~~~~~~~~~~
  972. io.stat
  973. A read-only nested-keyed file which exists on non-root
  974. cgroups.
  975. Lines are keyed by $MAJ:$MIN device numbers and not ordered.
  976. The following nested keys are defined.
  977. ====== ===================
  978. rbytes Bytes read
  979. wbytes Bytes written
  980. rios Number of read IOs
  981. wios Number of write IOs
  982. ====== ===================
  983. An example read output follows:
  984. 8:16 rbytes=1459200 wbytes=314773504 rios=192 wios=353
  985. 8:0 rbytes=90430464 wbytes=299008000 rios=8950 wios=1252
  986. io.weight
  987. A read-write flat-keyed file which exists on non-root cgroups.
  988. The default is "default 100".
  989. The first line is the default weight applied to devices
  990. without specific override. The rest are overrides keyed by
  991. $MAJ:$MIN device numbers and not ordered. The weights are in
  992. the range [1, 10000] and specifies the relative amount IO time
  993. the cgroup can use in relation to its siblings.
  994. The default weight can be updated by writing either "default
  995. $WEIGHT" or simply "$WEIGHT". Overrides can be set by writing
  996. "$MAJ:$MIN $WEIGHT" and unset by writing "$MAJ:$MIN default".
  997. An example read output follows::
  998. default 100
  999. 8:16 200
  1000. 8:0 50
  1001. io.max
  1002. A read-write nested-keyed file which exists on non-root
  1003. cgroups.
  1004. BPS and IOPS based IO limit. Lines are keyed by $MAJ:$MIN
  1005. device numbers and not ordered. The following nested keys are
  1006. defined.
  1007. ===== ==================================
  1008. rbps Max read bytes per second
  1009. wbps Max write bytes per second
  1010. riops Max read IO operations per second
  1011. wiops Max write IO operations per second
  1012. ===== ==================================
  1013. When writing, any number of nested key-value pairs can be
  1014. specified in any order. "max" can be specified as the value
  1015. to remove a specific limit. If the same key is specified
  1016. multiple times, the outcome is undefined.
  1017. BPS and IOPS are measured in each IO direction and IOs are
  1018. delayed if limit is reached. Temporary bursts are allowed.
  1019. Setting read limit at 2M BPS and write at 120 IOPS for 8:16::
  1020. echo "8:16 rbps=2097152 wiops=120" > io.max
  1021. Reading returns the following::
  1022. 8:16 rbps=2097152 wbps=max riops=max wiops=120
  1023. Write IOPS limit can be removed by writing the following::
  1024. echo "8:16 wiops=max" > io.max
  1025. Reading now returns the following::
  1026. 8:16 rbps=2097152 wbps=max riops=max wiops=max
  1027. Writeback
  1028. ~~~~~~~~~
  1029. Page cache is dirtied through buffered writes and shared mmaps and
  1030. written asynchronously to the backing filesystem by the writeback
  1031. mechanism. Writeback sits between the memory and IO domains and
  1032. regulates the proportion of dirty memory by balancing dirtying and
  1033. write IOs.
  1034. The io controller, in conjunction with the memory controller,
  1035. implements control of page cache writeback IOs. The memory controller
  1036. defines the memory domain that dirty memory ratio is calculated and
  1037. maintained for and the io controller defines the io domain which
  1038. writes out dirty pages for the memory domain. Both system-wide and
  1039. per-cgroup dirty memory states are examined and the more restrictive
  1040. of the two is enforced.
  1041. cgroup writeback requires explicit support from the underlying
  1042. filesystem. Currently, cgroup writeback is implemented on ext2, ext4
  1043. and btrfs. On other filesystems, all writeback IOs are attributed to
  1044. the root cgroup.
  1045. There are inherent differences in memory and writeback management
  1046. which affects how cgroup ownership is tracked. Memory is tracked per
  1047. page while writeback per inode. For the purpose of writeback, an
  1048. inode is assigned to a cgroup and all IO requests to write dirty pages
  1049. from the inode are attributed to that cgroup.
  1050. As cgroup ownership for memory is tracked per page, there can be pages
  1051. which are associated with different cgroups than the one the inode is
  1052. associated with. These are called foreign pages. The writeback
  1053. constantly keeps track of foreign pages and, if a particular foreign
  1054. cgroup becomes the majority over a certain period of time, switches
  1055. the ownership of the inode to that cgroup.
  1056. While this model is enough for most use cases where a given inode is
  1057. mostly dirtied by a single cgroup even when the main writing cgroup
  1058. changes over time, use cases where multiple cgroups write to a single
  1059. inode simultaneously are not supported well. In such circumstances, a
  1060. significant portion of IOs are likely to be attributed incorrectly.
  1061. As memory controller assigns page ownership on the first use and
  1062. doesn't update it until the page is released, even if writeback
  1063. strictly follows page ownership, multiple cgroups dirtying overlapping
  1064. areas wouldn't work as expected. It's recommended to avoid such usage
  1065. patterns.
  1066. The sysctl knobs which affect writeback behavior are applied to cgroup
  1067. writeback as follows.
  1068. vm.dirty_background_ratio, vm.dirty_ratio
  1069. These ratios apply the same to cgroup writeback with the
  1070. amount of available memory capped by limits imposed by the
  1071. memory controller and system-wide clean memory.
  1072. vm.dirty_background_bytes, vm.dirty_bytes
  1073. For cgroup writeback, this is calculated into ratio against
  1074. total available memory and applied the same way as
  1075. vm.dirty[_background]_ratio.
  1076. PID
  1077. ---
  1078. The process number controller is used to allow a cgroup to stop any
  1079. new tasks from being fork()'d or clone()'d after a specified limit is
  1080. reached.
  1081. The number of tasks in a cgroup can be exhausted in ways which other
  1082. controllers cannot prevent, thus warranting its own controller. For
  1083. example, a fork bomb is likely to exhaust the number of tasks before
  1084. hitting memory restrictions.
  1085. Note that PIDs used in this controller refer to TIDs, process IDs as
  1086. used by the kernel.
  1087. PID Interface Files
  1088. ~~~~~~~~~~~~~~~~~~~
  1089. pids.max
  1090. A read-write single value file which exists on non-root
  1091. cgroups. The default is "max".
  1092. Hard limit of number of processes.
  1093. pids.current
  1094. A read-only single value file which exists on all cgroups.
  1095. The number of processes currently in the cgroup and its
  1096. descendants.
  1097. Organisational operations are not blocked by cgroup policies, so it is
  1098. possible to have pids.current > pids.max. This can be done by either
  1099. setting the limit to be smaller than pids.current, or attaching enough
  1100. processes to the cgroup such that pids.current is larger than
  1101. pids.max. However, it is not possible to violate a cgroup PID policy
  1102. through fork() or clone(). These will return -EAGAIN if the creation
  1103. of a new process would cause a cgroup policy to be violated.
  1104. Device controller
  1105. -----------------
  1106. Device controller manages access to device files. It includes both
  1107. creation of new device files (using mknod), and access to the
  1108. existing device files.
  1109. Cgroup v2 device controller has no interface files and is implemented
  1110. on top of cgroup BPF. To control access to device files, a user may
  1111. create bpf programs of the BPF_CGROUP_DEVICE type and attach them
  1112. to cgroups. On an attempt to access a device file, corresponding
  1113. BPF programs will be executed, and depending on the return value
  1114. the attempt will succeed or fail with -EPERM.
  1115. A BPF_CGROUP_DEVICE program takes a pointer to the bpf_cgroup_dev_ctx
  1116. structure, which describes the device access attempt: access type
  1117. (mknod/read/write) and device (type, major and minor numbers).
  1118. If the program returns 0, the attempt fails with -EPERM, otherwise
  1119. it succeeds.
  1120. An example of BPF_CGROUP_DEVICE program may be found in the kernel
  1121. source tree in the tools/testing/selftests/bpf/dev_cgroup.c file.
  1122. RDMA
  1123. ----
  1124. The "rdma" controller regulates the distribution and accounting of
  1125. of RDMA resources.
  1126. RDMA Interface Files
  1127. ~~~~~~~~~~~~~~~~~~~~
  1128. rdma.max
  1129. A readwrite nested-keyed file that exists for all the cgroups
  1130. except root that describes current configured resource limit
  1131. for a RDMA/IB device.
  1132. Lines are keyed by device name and are not ordered.
  1133. Each line contains space separated resource name and its configured
  1134. limit that can be distributed.
  1135. The following nested keys are defined.
  1136. ========== =============================
  1137. hca_handle Maximum number of HCA Handles
  1138. hca_object Maximum number of HCA Objects
  1139. ========== =============================
  1140. An example for mlx4 and ocrdma device follows::
  1141. mlx4_0 hca_handle=2 hca_object=2000
  1142. ocrdma1 hca_handle=3 hca_object=max
  1143. rdma.current
  1144. A read-only file that describes current resource usage.
  1145. It exists for all the cgroup except root.
  1146. An example for mlx4 and ocrdma device follows::
  1147. mlx4_0 hca_handle=1 hca_object=20
  1148. ocrdma1 hca_handle=1 hca_object=23
  1149. Misc
  1150. ----
  1151. perf_event
  1152. ~~~~~~~~~~
  1153. perf_event controller, if not mounted on a legacy hierarchy, is
  1154. automatically enabled on the v2 hierarchy so that perf events can
  1155. always be filtered by cgroup v2 path. The controller can still be
  1156. moved to a legacy hierarchy after v2 hierarchy is populated.
  1157. Non-normative information
  1158. -------------------------
  1159. This section contains information that isn't considered to be a part of
  1160. the stable kernel API and so is subject to change.
  1161. CPU controller root cgroup process behaviour
  1162. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1163. When distributing CPU cycles in the root cgroup each thread in this
  1164. cgroup is treated as if it was hosted in a separate child cgroup of the
  1165. root cgroup. This child cgroup weight is dependent on its thread nice
  1166. level.
  1167. For details of this mapping see sched_prio_to_weight array in
  1168. kernel/sched/core.c file (values from this array should be scaled
  1169. appropriately so the neutral - nice 0 - value is 100 instead of 1024).
  1170. IO controller root cgroup process behaviour
  1171. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1172. Root cgroup processes are hosted in an implicit leaf child node.
  1173. When distributing IO resources this implicit child node is taken into
  1174. account as if it was a normal child cgroup of the root cgroup with a
  1175. weight value of 200.
  1176. Namespace
  1177. =========
  1178. Basics
  1179. ------
  1180. cgroup namespace provides a mechanism to virtualize the view of the
  1181. "/proc/$PID/cgroup" file and cgroup mounts. The CLONE_NEWCGROUP clone
  1182. flag can be used with clone(2) and unshare(2) to create a new cgroup
  1183. namespace. The process running inside the cgroup namespace will have
  1184. its "/proc/$PID/cgroup" output restricted to cgroupns root. The
  1185. cgroupns root is the cgroup of the process at the time of creation of
  1186. the cgroup namespace.
  1187. Without cgroup namespace, the "/proc/$PID/cgroup" file shows the
  1188. complete path of the cgroup of a process. In a container setup where
  1189. a set of cgroups and namespaces are intended to isolate processes the
  1190. "/proc/$PID/cgroup" file may leak potential system level information
  1191. to the isolated processes. For Example::
  1192. # cat /proc/self/cgroup
  1193. 0::/batchjobs/container_id1
  1194. The path '/batchjobs/container_id1' can be considered as system-data
  1195. and undesirable to expose to the isolated processes. cgroup namespace
  1196. can be used to restrict visibility of this path. For example, before
  1197. creating a cgroup namespace, one would see::
  1198. # ls -l /proc/self/ns/cgroup
  1199. lrwxrwxrwx 1 root root 0 2014-07-15 10:37 /proc/self/ns/cgroup -> cgroup:[4026531835]
  1200. # cat /proc/self/cgroup
  1201. 0::/batchjobs/container_id1
  1202. After unsharing a new namespace, the view changes::
  1203. # ls -l /proc/self/ns/cgroup
  1204. lrwxrwxrwx 1 root root 0 2014-07-15 10:35 /proc/self/ns/cgroup -> cgroup:[4026532183]
  1205. # cat /proc/self/cgroup
  1206. 0::/
  1207. When some thread from a multi-threaded process unshares its cgroup
  1208. namespace, the new cgroupns gets applied to the entire process (all
  1209. the threads). This is natural for the v2 hierarchy; however, for the
  1210. legacy hierarchies, this may be unexpected.
  1211. A cgroup namespace is alive as long as there are processes inside or
  1212. mounts pinning it. When the last usage goes away, the cgroup
  1213. namespace is destroyed. The cgroupns root and the actual cgroups
  1214. remain.
  1215. The Root and Views
  1216. ------------------
  1217. The 'cgroupns root' for a cgroup namespace is the cgroup in which the
  1218. process calling unshare(2) is running. For example, if a process in
  1219. /batchjobs/container_id1 cgroup calls unshare, cgroup
  1220. /batchjobs/container_id1 becomes the cgroupns root. For the
  1221. init_cgroup_ns, this is the real root ('/') cgroup.
  1222. The cgroupns root cgroup does not change even if the namespace creator
  1223. process later moves to a different cgroup::
  1224. # ~/unshare -c # unshare cgroupns in some cgroup
  1225. # cat /proc/self/cgroup
  1226. 0::/
  1227. # mkdir sub_cgrp_1
  1228. # echo 0 > sub_cgrp_1/cgroup.procs
  1229. # cat /proc/self/cgroup
  1230. 0::/sub_cgrp_1
  1231. Each process gets its namespace-specific view of "/proc/$PID/cgroup"
  1232. Processes running inside the cgroup namespace will be able to see
  1233. cgroup paths (in /proc/self/cgroup) only inside their root cgroup.
  1234. From within an unshared cgroupns::
  1235. # sleep 100000 &
  1236. [1] 7353
  1237. # echo 7353 > sub_cgrp_1/cgroup.procs
  1238. # cat /proc/7353/cgroup
  1239. 0::/sub_cgrp_1
  1240. From the initial cgroup namespace, the real cgroup path will be
  1241. visible::
  1242. $ cat /proc/7353/cgroup
  1243. 0::/batchjobs/container_id1/sub_cgrp_1
  1244. From a sibling cgroup namespace (that is, a namespace rooted at a
  1245. different cgroup), the cgroup path relative to its own cgroup
  1246. namespace root will be shown. For instance, if PID 7353's cgroup
  1247. namespace root is at '/batchjobs/container_id2', then it will see::
  1248. # cat /proc/7353/cgroup
  1249. 0::/../container_id2/sub_cgrp_1
  1250. Note that the relative path always starts with '/' to indicate that
  1251. its relative to the cgroup namespace root of the caller.
  1252. Migration and setns(2)
  1253. ----------------------
  1254. Processes inside a cgroup namespace can move into and out of the
  1255. namespace root if they have proper access to external cgroups. For
  1256. example, from inside a namespace with cgroupns root at
  1257. /batchjobs/container_id1, and assuming that the global hierarchy is
  1258. still accessible inside cgroupns::
  1259. # cat /proc/7353/cgroup
  1260. 0::/sub_cgrp_1
  1261. # echo 7353 > batchjobs/container_id2/cgroup.procs
  1262. # cat /proc/7353/cgroup
  1263. 0::/../container_id2
  1264. Note that this kind of setup is not encouraged. A task inside cgroup
  1265. namespace should only be exposed to its own cgroupns hierarchy.
  1266. setns(2) to another cgroup namespace is allowed when:
  1267. (a) the process has CAP_SYS_ADMIN against its current user namespace
  1268. (b) the process has CAP_SYS_ADMIN against the target cgroup
  1269. namespace's userns
  1270. No implicit cgroup changes happen with attaching to another cgroup
  1271. namespace. It is expected that the someone moves the attaching
  1272. process under the target cgroup namespace root.
  1273. Interaction with Other Namespaces
  1274. ---------------------------------
  1275. Namespace specific cgroup hierarchy can be mounted by a process
  1276. running inside a non-init cgroup namespace::
  1277. # mount -t cgroup2 none $MOUNT_POINT
  1278. This will mount the unified cgroup hierarchy with cgroupns root as the
  1279. filesystem root. The process needs CAP_SYS_ADMIN against its user and
  1280. mount namespaces.
  1281. The virtualization of /proc/self/cgroup file combined with restricting
  1282. the view of cgroup hierarchy by namespace-private cgroupfs mount
  1283. provides a properly isolated cgroup view inside the container.
  1284. Information on Kernel Programming
  1285. =================================
  1286. This section contains kernel programming information in the areas
  1287. where interacting with cgroup is necessary. cgroup core and
  1288. controllers are not covered.
  1289. Filesystem Support for Writeback
  1290. --------------------------------
  1291. A filesystem can support cgroup writeback by updating
  1292. address_space_operations->writepage[s]() to annotate bio's using the
  1293. following two functions.
  1294. wbc_init_bio(@wbc, @bio)
  1295. Should be called for each bio carrying writeback data and
  1296. associates the bio with the inode's owner cgroup. Can be
  1297. called anytime between bio allocation and submission.
  1298. wbc_account_io(@wbc, @page, @bytes)
  1299. Should be called for each data segment being written out.
  1300. While this function doesn't care exactly when it's called
  1301. during the writeback session, it's the easiest and most
  1302. natural to call it as data segments are added to a bio.
  1303. With writeback bio's annotated, cgroup support can be enabled per
  1304. super_block by setting SB_I_CGROUPWB in ->s_iflags. This allows for
  1305. selective disabling of cgroup writeback support which is helpful when
  1306. certain filesystem features, e.g. journaled data mode, are
  1307. incompatible.
  1308. wbc_init_bio() binds the specified bio to its cgroup. Depending on
  1309. the configuration, the bio may be executed at a lower priority and if
  1310. the writeback session is holding shared resources, e.g. a journal
  1311. entry, may lead to priority inversion. There is no one easy solution
  1312. for the problem. Filesystems can try to work around specific problem
  1313. cases by skipping wbc_init_bio() or using bio_associate_blkcg()
  1314. directly.
  1315. Deprecated v1 Core Features
  1316. ===========================
  1317. - Multiple hierarchies including named ones are not supported.
  1318. - All v1 mount options are not supported.
  1319. - The "tasks" file is removed and "cgroup.procs" is not sorted.
  1320. - "cgroup.clone_children" is removed.
  1321. - /proc/cgroups is meaningless for v2. Use "cgroup.controllers" file
  1322. at the root instead.
  1323. Issues with v1 and Rationales for v2
  1324. ====================================
  1325. Multiple Hierarchies
  1326. --------------------
  1327. cgroup v1 allowed an arbitrary number of hierarchies and each
  1328. hierarchy could host any number of controllers. While this seemed to
  1329. provide a high level of flexibility, it wasn't useful in practice.
  1330. For example, as there is only one instance of each controller, utility
  1331. type controllers such as freezer which can be useful in all
  1332. hierarchies could only be used in one. The issue is exacerbated by
  1333. the fact that controllers couldn't be moved to another hierarchy once
  1334. hierarchies were populated. Another issue was that all controllers
  1335. bound to a hierarchy were forced to have exactly the same view of the
  1336. hierarchy. It wasn't possible to vary the granularity depending on
  1337. the specific controller.
  1338. In practice, these issues heavily limited which controllers could be
  1339. put on the same hierarchy and most configurations resorted to putting
  1340. each controller on its own hierarchy. Only closely related ones, such
  1341. as the cpu and cpuacct controllers, made sense to be put on the same
  1342. hierarchy. This often meant that userland ended up managing multiple
  1343. similar hierarchies repeating the same steps on each hierarchy
  1344. whenever a hierarchy management operation was necessary.
  1345. Furthermore, support for multiple hierarchies came at a steep cost.
  1346. It greatly complicated cgroup core implementation but more importantly
  1347. the support for multiple hierarchies restricted how cgroup could be
  1348. used in general and what controllers was able to do.
  1349. There was no limit on how many hierarchies there might be, which meant
  1350. that a thread's cgroup membership couldn't be described in finite
  1351. length. The key might contain any number of entries and was unlimited
  1352. in length, which made it highly awkward to manipulate and led to
  1353. addition of controllers which existed only to identify membership,
  1354. which in turn exacerbated the original problem of proliferating number
  1355. of hierarchies.
  1356. Also, as a controller couldn't have any expectation regarding the
  1357. topologies of hierarchies other controllers might be on, each
  1358. controller had to assume that all other controllers were attached to
  1359. completely orthogonal hierarchies. This made it impossible, or at
  1360. least very cumbersome, for controllers to cooperate with each other.
  1361. In most use cases, putting controllers on hierarchies which are
  1362. completely orthogonal to each other isn't necessary. What usually is
  1363. called for is the ability to have differing levels of granularity
  1364. depending on the specific controller. In other words, hierarchy may
  1365. be collapsed from leaf towards root when viewed from specific
  1366. controllers. For example, a given configuration might not care about
  1367. how memory is distributed beyond a certain level while still wanting
  1368. to control how CPU cycles are distributed.
  1369. Thread Granularity
  1370. ------------------
  1371. cgroup v1 allowed threads of a process to belong to different cgroups.
  1372. This didn't make sense for some controllers and those controllers
  1373. ended up implementing different ways to ignore such situations but
  1374. much more importantly it blurred the line between API exposed to
  1375. individual applications and system management interface.
  1376. Generally, in-process knowledge is available only to the process
  1377. itself; thus, unlike service-level organization of processes,
  1378. categorizing threads of a process requires active participation from
  1379. the application which owns the target process.
  1380. cgroup v1 had an ambiguously defined delegation model which got abused
  1381. in combination with thread granularity. cgroups were delegated to
  1382. individual applications so that they can create and manage their own
  1383. sub-hierarchies and control resource distributions along them. This
  1384. effectively raised cgroup to the status of a syscall-like API exposed
  1385. to lay programs.
  1386. First of all, cgroup has a fundamentally inadequate interface to be
  1387. exposed this way. For a process to access its own knobs, it has to
  1388. extract the path on the target hierarchy from /proc/self/cgroup,
  1389. construct the path by appending the name of the knob to the path, open
  1390. and then read and/or write to it. This is not only extremely clunky
  1391. and unusual but also inherently racy. There is no conventional way to
  1392. define transaction across the required steps and nothing can guarantee
  1393. that the process would actually be operating on its own sub-hierarchy.
  1394. cgroup controllers implemented a number of knobs which would never be
  1395. accepted as public APIs because they were just adding control knobs to
  1396. system-management pseudo filesystem. cgroup ended up with interface
  1397. knobs which were not properly abstracted or refined and directly
  1398. revealed kernel internal details. These knobs got exposed to
  1399. individual applications through the ill-defined delegation mechanism
  1400. effectively abusing cgroup as a shortcut to implementing public APIs
  1401. without going through the required scrutiny.
  1402. This was painful for both userland and kernel. Userland ended up with
  1403. misbehaving and poorly abstracted interfaces and kernel exposing and
  1404. locked into constructs inadvertently.
  1405. Competition Between Inner Nodes and Threads
  1406. -------------------------------------------
  1407. cgroup v1 allowed threads to be in any cgroups which created an
  1408. interesting problem where threads belonging to a parent cgroup and its
  1409. children cgroups competed for resources. This was nasty as two
  1410. different types of entities competed and there was no obvious way to
  1411. settle it. Different controllers did different things.
  1412. The cpu controller considered threads and cgroups as equivalents and
  1413. mapped nice levels to cgroup weights. This worked for some cases but
  1414. fell flat when children wanted to be allocated specific ratios of CPU
  1415. cycles and the number of internal threads fluctuated - the ratios
  1416. constantly changed as the number of competing entities fluctuated.
  1417. There also were other issues. The mapping from nice level to weight
  1418. wasn't obvious or universal, and there were various other knobs which
  1419. simply weren't available for threads.
  1420. The io controller implicitly created a hidden leaf node for each
  1421. cgroup to host the threads. The hidden leaf had its own copies of all
  1422. the knobs with ``leaf_`` prefixed. While this allowed equivalent
  1423. control over internal threads, it was with serious drawbacks. It
  1424. always added an extra layer of nesting which wouldn't be necessary
  1425. otherwise, made the interface messy and significantly complicated the
  1426. implementation.
  1427. The memory controller didn't have a way to control what happened
  1428. between internal tasks and child cgroups and the behavior was not
  1429. clearly defined. There were attempts to add ad-hoc behaviors and
  1430. knobs to tailor the behavior to specific workloads which would have
  1431. led to problems extremely difficult to resolve in the long term.
  1432. Multiple controllers struggled with internal tasks and came up with
  1433. different ways to deal with it; unfortunately, all the approaches were
  1434. severely flawed and, furthermore, the widely different behaviors
  1435. made cgroup as a whole highly inconsistent.
  1436. This clearly is a problem which needs to be addressed from cgroup core
  1437. in a uniform way.
  1438. Other Interface Issues
  1439. ----------------------
  1440. cgroup v1 grew without oversight and developed a large number of
  1441. idiosyncrasies and inconsistencies. One issue on the cgroup core side
  1442. was how an empty cgroup was notified - a userland helper binary was
  1443. forked and executed for each event. The event delivery wasn't
  1444. recursive or delegatable. The limitations of the mechanism also led
  1445. to in-kernel event delivery filtering mechanism further complicating
  1446. the interface.
  1447. Controller interfaces were problematic too. An extreme example is
  1448. controllers completely ignoring hierarchical organization and treating
  1449. all cgroups as if they were all located directly under the root
  1450. cgroup. Some controllers exposed a large amount of inconsistent
  1451. implementation details to userland.
  1452. There also was no consistency across controllers. When a new cgroup
  1453. was created, some controllers defaulted to not imposing extra
  1454. restrictions while others disallowed any resource usage until
  1455. explicitly configured. Configuration knobs for the same type of
  1456. control used widely differing naming schemes and formats. Statistics
  1457. and information knobs were named arbitrarily and used different
  1458. formats and units even in the same controller.
  1459. cgroup v2 establishes common conventions where appropriate and updates
  1460. controllers so that they expose minimal and consistent interfaces.
  1461. Controller Issues and Remedies
  1462. ------------------------------
  1463. Memory
  1464. ~~~~~~
  1465. The original lower boundary, the soft limit, is defined as a limit
  1466. that is per default unset. As a result, the set of cgroups that
  1467. global reclaim prefers is opt-in, rather than opt-out. The costs for
  1468. optimizing these mostly negative lookups are so high that the
  1469. implementation, despite its enormous size, does not even provide the
  1470. basic desirable behavior. First off, the soft limit has no
  1471. hierarchical meaning. All configured groups are organized in a global
  1472. rbtree and treated like equal peers, regardless where they are located
  1473. in the hierarchy. This makes subtree delegation impossible. Second,
  1474. the soft limit reclaim pass is so aggressive that it not just
  1475. introduces high allocation latencies into the system, but also impacts
  1476. system performance due to overreclaim, to the point where the feature
  1477. becomes self-defeating.
  1478. The memory.low boundary on the other hand is a top-down allocated
  1479. reserve. A cgroup enjoys reclaim protection when it's within its low,
  1480. which makes delegation of subtrees possible.
  1481. The original high boundary, the hard limit, is defined as a strict
  1482. limit that can not budge, even if the OOM killer has to be called.
  1483. But this generally goes against the goal of making the most out of the
  1484. available memory. The memory consumption of workloads varies during
  1485. runtime, and that requires users to overcommit. But doing that with a
  1486. strict upper limit requires either a fairly accurate prediction of the
  1487. working set size or adding slack to the limit. Since working set size
  1488. estimation is hard and error prone, and getting it wrong results in
  1489. OOM kills, most users tend to err on the side of a looser limit and
  1490. end up wasting precious resources.
  1491. The memory.high boundary on the other hand can be set much more
  1492. conservatively. When hit, it throttles allocations by forcing them
  1493. into direct reclaim to work off the excess, but it never invokes the
  1494. OOM killer. As a result, a high boundary that is chosen too
  1495. aggressively will not terminate the processes, but instead it will
  1496. lead to gradual performance degradation. The user can monitor this
  1497. and make corrections until the minimal memory footprint that still
  1498. gives acceptable performance is found.
  1499. In extreme cases, with many concurrent allocations and a complete
  1500. breakdown of reclaim progress within the group, the high boundary can
  1501. be exceeded. But even then it's mostly better to satisfy the
  1502. allocation from the slack available in other groups or the rest of the
  1503. system than killing the group. Otherwise, memory.max is there to
  1504. limit this type of spillover and ultimately contain buggy or even
  1505. malicious applications.
  1506. Setting the original memory.limit_in_bytes below the current usage was
  1507. subject to a race condition, where concurrent charges could cause the
  1508. limit setting to fail. memory.max on the other hand will first set the
  1509. limit to prevent new charges, and then reclaim and OOM kill until the
  1510. new limit is met - or the task writing to memory.max is killed.
  1511. The combined memory+swap accounting and limiting is replaced by real
  1512. control over swap space.
  1513. The main argument for a combined memory+swap facility in the original
  1514. cgroup design was that global or parental pressure would always be
  1515. able to swap all anonymous memory of a child group, regardless of the
  1516. child's own (possibly untrusted) configuration. However, untrusted
  1517. groups can sabotage swapping by other means - such as referencing its
  1518. anonymous memory in a tight loop - and an admin can not assume full
  1519. swappability when overcommitting untrusted jobs.
  1520. For trusted jobs, on the other hand, a combined counter is not an
  1521. intuitive userspace interface, and it flies in the face of the idea
  1522. that cgroup controllers should account and limit specific physical
  1523. resources. Swap space is a resource like all others in the system,
  1524. and that's why unified hierarchy allows distributing it separately.