cgroup-v2.txt 71 KB

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