cgroup-v2.rst 119 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238
  1. .. _cgroup-v2:
  2. ================
  3. Control Group v2
  4. ================
  5. :Date: October, 2015
  6. :Author: Tejun Heo <tj@kernel.org>
  7. This is the authoritative documentation on the design, interface and
  8. conventions of cgroup v2. It describes all userland-visible aspects
  9. of cgroup including core and specific controller behaviors. All
  10. future changes must be reflected in this document. Documentation for
  11. v1 is available under :ref:`Documentation/admin-guide/cgroup-v1/index.rst <cgroup-v1>`.
  12. .. CONTENTS
  13. 1. Introduction
  14. 1-1. Terminology
  15. 1-2. What is cgroup?
  16. 2. Basic Operations
  17. 2-1. Mounting
  18. 2-2. Organizing Processes and Threads
  19. 2-2-1. Processes
  20. 2-2-2. Threads
  21. 2-3. [Un]populated Notification
  22. 2-4. Controlling Controllers
  23. 2-4-1. Enabling and Disabling
  24. 2-4-2. Top-down Constraint
  25. 2-4-3. No Internal Process Constraint
  26. 2-5. Delegation
  27. 2-5-1. Model of Delegation
  28. 2-5-2. Delegation Containment
  29. 2-6. Guidelines
  30. 2-6-1. Organize Once and Control
  31. 2-6-2. Avoid Name Collisions
  32. 3. Resource Distribution Models
  33. 3-1. Weights
  34. 3-2. Limits
  35. 3-3. Protections
  36. 3-4. Allocations
  37. 4. Interface Files
  38. 4-1. Format
  39. 4-2. Conventions
  40. 4-3. Core Interface Files
  41. 5. Controllers
  42. 5-1. CPU
  43. 5-1-1. CPU Interface Files
  44. 5-2. Memory
  45. 5-2-1. Memory Interface Files
  46. 5-2-2. Usage Guidelines
  47. 5-2-3. Memory Ownership
  48. 5-3. IO
  49. 5-3-1. IO Interface Files
  50. 5-3-2. Writeback
  51. 5-3-3. IO Latency
  52. 5-3-3-1. How IO Latency Throttling Works
  53. 5-3-3-2. IO Latency Interface Files
  54. 5-3-4. IO Priority
  55. 5-4. PID
  56. 5-4-1. PID Interface Files
  57. 5-5. Cpuset
  58. 5.5-1. Cpuset Interface Files
  59. 5-6. Device
  60. 5-7. RDMA
  61. 5-7-1. RDMA Interface Files
  62. 5-8. HugeTLB
  63. 5.8-1. HugeTLB Interface Files
  64. 5-9. Misc
  65. 5.9-1 Miscellaneous cgroup Interface Files
  66. 5.9-2 Migration and Ownership
  67. 5-10. Others
  68. 5-10-1. perf_event
  69. 5-N. Non-normative information
  70. 5-N-1. CPU controller root cgroup process behaviour
  71. 5-N-2. IO controller root cgroup process behaviour
  72. 6. Namespace
  73. 6-1. Basics
  74. 6-2. The Root and Views
  75. 6-3. Migration and setns(2)
  76. 6-4. Interaction with Other Namespaces
  77. P. Information on Kernel Programming
  78. P-1. Filesystem Support for Writeback
  79. D. Deprecated v1 Core Features
  80. R. Issues with v1 and Rationales for v2
  81. R-1. Multiple Hierarchies
  82. R-2. Thread Granularity
  83. R-3. Competition Between Inner Nodes and Threads
  84. R-4. Other Interface Issues
  85. R-5. Controller Issues and Remedies
  86. R-5-1. Memory
  87. Introduction
  88. ============
  89. Terminology
  90. -----------
  91. "cgroup" stands for "control group" and is never capitalized. The
  92. singular form is used to designate the whole feature and also as a
  93. qualifier as in "cgroup controllers". When explicitly referring to
  94. multiple individual control groups, the plural form "cgroups" is used.
  95. What is cgroup?
  96. ---------------
  97. cgroup is a mechanism to organize processes hierarchically and
  98. distribute system resources along the hierarchy in a controlled and
  99. configurable manner.
  100. cgroup is largely composed of two parts - the core and controllers.
  101. cgroup core is primarily responsible for hierarchically organizing
  102. processes. A cgroup controller is usually responsible for
  103. distributing a specific type of system resource along the hierarchy
  104. although there are utility controllers which serve purposes other than
  105. resource distribution.
  106. cgroups form a tree structure and every process in the system belongs
  107. to one and only one cgroup. All threads of a process belong to the
  108. same cgroup. On creation, all processes are put in the cgroup that
  109. the parent process belongs to at the time. A process can be migrated
  110. to another cgroup. Migration of a process doesn't affect already
  111. existing descendant processes.
  112. Following certain structural constraints, controllers may be enabled or
  113. disabled selectively on a cgroup. All controller behaviors are
  114. hierarchical - if a controller is enabled on a cgroup, it affects all
  115. processes which belong to the cgroups consisting the inclusive
  116. sub-hierarchy of the cgroup. When a controller is enabled on a nested
  117. cgroup, it always restricts the resource distribution further. The
  118. restrictions set closer to the root in the hierarchy can not be
  119. overridden from further away.
  120. Basic Operations
  121. ================
  122. Mounting
  123. --------
  124. Unlike v1, cgroup v2 has only single hierarchy. The cgroup v2
  125. hierarchy can be mounted with the following mount command::
  126. # mount -t cgroup2 none $MOUNT_POINT
  127. cgroup2 filesystem has the magic number 0x63677270 ("cgrp"). All
  128. controllers which support v2 and are not bound to a v1 hierarchy are
  129. automatically bound to the v2 hierarchy and show up at the root.
  130. Controllers which are not in active use in the v2 hierarchy can be
  131. bound to other hierarchies. This allows mixing v2 hierarchy with the
  132. legacy v1 multiple hierarchies in a fully backward compatible way.
  133. A controller can be moved across hierarchies only after the controller
  134. is no longer referenced in its current hierarchy. Because per-cgroup
  135. controller states are destroyed asynchronously and controllers may
  136. have lingering references, a controller may not show up immediately on
  137. the v2 hierarchy after the final umount of the previous hierarchy.
  138. Similarly, a controller should be fully disabled to be moved out of
  139. the unified hierarchy and it may take some time for the disabled
  140. controller to become available for other hierarchies; furthermore, due
  141. to inter-controller dependencies, other controllers may need to be
  142. disabled too.
  143. While useful for development and manual configurations, moving
  144. controllers dynamically between the v2 and other hierarchies is
  145. strongly discouraged for production use. It is recommended to decide
  146. the hierarchies and controller associations before starting using the
  147. controllers after system boot.
  148. During transition to v2, system management software might still
  149. automount the v1 cgroup filesystem and so hijack all controllers
  150. during boot, before manual intervention is possible. To make testing
  151. and experimenting easier, the kernel parameter cgroup_no_v1= allows
  152. disabling controllers in v1 and make them always available in v2.
  153. cgroup v2 currently supports the following mount options.
  154. nsdelegate
  155. Consider cgroup namespaces as delegation boundaries. This
  156. option is system wide and can only be set on mount or modified
  157. through remount from the init namespace. The mount option is
  158. ignored on non-init namespace mounts. Please refer to the
  159. Delegation section for details.
  160. favordynmods
  161. Reduce the latencies of dynamic cgroup modifications such as
  162. task migrations and controller on/offs at the cost of making
  163. hot path operations such as forks and exits more expensive.
  164. The static usage pattern of creating a cgroup, enabling
  165. controllers, and then seeding it with CLONE_INTO_CGROUP is
  166. not affected by this option.
  167. memory_localevents
  168. Only populate memory.events with data for the current cgroup,
  169. and not any subtrees. This is legacy behaviour, the default
  170. behaviour without this option is to include subtree counts.
  171. This option is system wide and can only be set on mount or
  172. modified through remount from the init namespace. The mount
  173. option is ignored on non-init namespace mounts.
  174. memory_recursiveprot
  175. Recursively apply memory.min and memory.low protection to
  176. entire subtrees, without requiring explicit downward
  177. propagation into leaf cgroups. This allows protecting entire
  178. subtrees from one another, while retaining free competition
  179. within those subtrees. This should have been the default
  180. behavior but is a mount-option to avoid regressing setups
  181. relying on the original semantics (e.g. specifying bogusly
  182. high 'bypass' protection values at higher tree levels).
  183. memory_hugetlb_accounting
  184. Count HugeTLB memory usage towards the cgroup's overall
  185. memory usage for the memory controller (for the purpose of
  186. statistics reporting and memory protetion). This is a new
  187. behavior that could regress existing setups, so it must be
  188. explicitly opted in with this mount option.
  189. A few caveats to keep in mind:
  190. * There is no HugeTLB pool management involved in the memory
  191. controller. The pre-allocated pool does not belong to anyone.
  192. Specifically, when a new HugeTLB folio is allocated to
  193. the pool, it is not accounted for from the perspective of the
  194. memory controller. It is only charged to a cgroup when it is
  195. actually used (for e.g at page fault time). Host memory
  196. overcommit management has to consider this when configuring
  197. hard limits. In general, HugeTLB pool management should be
  198. done via other mechanisms (such as the HugeTLB controller).
  199. * Failure to charge a HugeTLB folio to the memory controller
  200. results in SIGBUS. This could happen even if the HugeTLB pool
  201. still has pages available (but the cgroup limit is hit and
  202. reclaim attempt fails).
  203. * Charging HugeTLB memory towards the memory controller affects
  204. memory protection and reclaim dynamics. Any userspace tuning
  205. (of low, min limits for e.g) needs to take this into account.
  206. * HugeTLB pages utilized while this option is not selected
  207. will not be tracked by the memory controller (even if cgroup
  208. v2 is remounted later on).
  209. pids_localevents
  210. The option restores v1-like behavior of pids.events:max, that is only
  211. local (inside cgroup proper) fork failures are counted. Without this
  212. option pids.events.max represents any pids.max enforcemnt across
  213. cgroup's subtree.
  214. Organizing Processes and Threads
  215. --------------------------------
  216. Processes
  217. ~~~~~~~~~
  218. Initially, only the root cgroup exists to which all processes belong.
  219. A child cgroup can be created by creating a sub-directory::
  220. # mkdir $CGROUP_NAME
  221. A given cgroup may have multiple child cgroups forming a tree
  222. structure. Each cgroup has a read-writable interface file
  223. "cgroup.procs". When read, it lists the PIDs of all processes which
  224. belong to the cgroup one-per-line. The PIDs are not ordered and the
  225. same PID may show up more than once if the process got moved to
  226. another cgroup and then back or the PID got recycled while reading.
  227. A process can be migrated into a cgroup by writing its PID to the
  228. target cgroup's "cgroup.procs" file. Only one process can be migrated
  229. on a single write(2) call. If a process is composed of multiple
  230. threads, writing the PID of any thread migrates all threads of the
  231. process.
  232. When a process forks a child process, the new process is born into the
  233. cgroup that the forking process belongs to at the time of the
  234. operation. After exit, a process stays associated with the cgroup
  235. that it belonged to at the time of exit until it's reaped; however, a
  236. zombie process does not appear in "cgroup.procs" and thus can't be
  237. moved to another cgroup.
  238. A cgroup which doesn't have any children or live processes can be
  239. destroyed by removing the directory. Note that a cgroup which doesn't
  240. have any children and is associated only with zombie processes is
  241. considered empty and can be removed::
  242. # rmdir $CGROUP_NAME
  243. "/proc/$PID/cgroup" lists a process's cgroup membership. If legacy
  244. cgroup is in use in the system, this file may contain multiple lines,
  245. one for each hierarchy. The entry for cgroup v2 is always in the
  246. format "0::$PATH"::
  247. # cat /proc/842/cgroup
  248. ...
  249. 0::/test-cgroup/test-cgroup-nested
  250. If the process becomes a zombie and the cgroup it was associated with
  251. is removed subsequently, " (deleted)" is appended to the path::
  252. # cat /proc/842/cgroup
  253. ...
  254. 0::/test-cgroup/test-cgroup-nested (deleted)
  255. Threads
  256. ~~~~~~~
  257. cgroup v2 supports thread granularity for a subset of controllers to
  258. support use cases requiring hierarchical resource distribution across
  259. the threads of a group of processes. By default, all threads of a
  260. process belong to the same cgroup, which also serves as the resource
  261. domain to host resource consumptions which are not specific to a
  262. process or thread. The thread mode allows threads to be spread across
  263. a subtree while still maintaining the common resource domain for them.
  264. Controllers which support thread mode are called threaded controllers.
  265. The ones which don't are called domain controllers.
  266. Marking a cgroup threaded makes it join the resource domain of its
  267. parent as a threaded cgroup. The parent may be another threaded
  268. cgroup whose resource domain is further up in the hierarchy. The root
  269. of a threaded subtree, that is, the nearest ancestor which is not
  270. threaded, is called threaded domain or thread root interchangeably and
  271. serves as the resource domain for the entire subtree.
  272. Inside a threaded subtree, threads of a process can be put in
  273. different cgroups and are not subject to the no internal process
  274. constraint - threaded controllers can be enabled on non-leaf cgroups
  275. whether they have threads in them or not.
  276. As the threaded domain cgroup hosts all the domain resource
  277. consumptions of the subtree, it is considered to have internal
  278. resource consumptions whether there are processes in it or not and
  279. can't have populated child cgroups which aren't threaded. Because the
  280. root cgroup is not subject to no internal process constraint, it can
  281. serve both as a threaded domain and a parent to domain cgroups.
  282. The current operation mode or type of the cgroup is shown in the
  283. "cgroup.type" file which indicates whether the cgroup is a normal
  284. domain, a domain which is serving as the domain of a threaded subtree,
  285. or a threaded cgroup.
  286. On creation, a cgroup is always a domain cgroup and can be made
  287. threaded by writing "threaded" to the "cgroup.type" file. The
  288. operation is single direction::
  289. # echo threaded > cgroup.type
  290. Once threaded, the cgroup can't be made a domain again. To enable the
  291. thread mode, the following conditions must be met.
  292. - As the cgroup will join the parent's resource domain. The parent
  293. must either be a valid (threaded) domain or a threaded cgroup.
  294. - When the parent is an unthreaded domain, it must not have any domain
  295. controllers enabled or populated domain children. The root is
  296. exempt from this requirement.
  297. Topology-wise, a cgroup can be in an invalid state. Please consider
  298. the following topology::
  299. A (threaded domain) - B (threaded) - C (domain, just created)
  300. C is created as a domain but isn't connected to a parent which can
  301. host child domains. C can't be used until it is turned into a
  302. threaded cgroup. "cgroup.type" file will report "domain (invalid)" in
  303. these cases. Operations which fail due to invalid topology use
  304. EOPNOTSUPP as the errno.
  305. A domain cgroup is turned into a threaded domain when one of its child
  306. cgroup becomes threaded or threaded controllers are enabled in the
  307. "cgroup.subtree_control" file while there are processes in the cgroup.
  308. A threaded domain reverts to a normal domain when the conditions
  309. clear.
  310. When read, "cgroup.threads" contains the list of the thread IDs of all
  311. threads in the cgroup. Except that the operations are per-thread
  312. instead of per-process, "cgroup.threads" has the same format and
  313. behaves the same way as "cgroup.procs". While "cgroup.threads" can be
  314. written to in any cgroup, as it can only move threads inside the same
  315. threaded domain, its operations are confined inside each threaded
  316. subtree.
  317. The threaded domain cgroup serves as the resource domain for the whole
  318. subtree, and, while the threads can be scattered across the subtree,
  319. all the processes are considered to be in the threaded domain cgroup.
  320. "cgroup.procs" in a threaded domain cgroup contains the PIDs of all
  321. processes in the subtree and is not readable in the subtree proper.
  322. However, "cgroup.procs" can be written to from anywhere in the subtree
  323. to migrate all threads of the matching process to the cgroup.
  324. Only threaded controllers can be enabled in a threaded subtree. When
  325. a threaded controller is enabled inside a threaded subtree, it only
  326. accounts for and controls resource consumptions associated with the
  327. threads in the cgroup and its descendants. All consumptions which
  328. aren't tied to a specific thread belong to the threaded domain cgroup.
  329. Because a threaded subtree is exempt from no internal process
  330. constraint, a threaded controller must be able to handle competition
  331. between threads in a non-leaf cgroup and its child cgroups. Each
  332. threaded controller defines how such competitions are handled.
  333. Currently, the following controllers are threaded and can be enabled
  334. in a threaded cgroup::
  335. - cpu
  336. - cpuset
  337. - perf_event
  338. - pids
  339. [Un]populated Notification
  340. --------------------------
  341. Each non-root cgroup has a "cgroup.events" file which contains
  342. "populated" field indicating whether the cgroup's sub-hierarchy has
  343. live processes in it. Its value is 0 if there is no live process in
  344. the cgroup and its descendants; otherwise, 1. poll and [id]notify
  345. events are triggered when the value changes. This can be used, for
  346. example, to start a clean-up operation after all processes of a given
  347. sub-hierarchy have exited. The populated state updates and
  348. notifications are recursive. Consider the following sub-hierarchy
  349. where the numbers in the parentheses represent the numbers of processes
  350. in each cgroup::
  351. A(4) - B(0) - C(1)
  352. \ D(0)
  353. A, B and C's "populated" fields would be 1 while D's 0. After the one
  354. process in C exits, B and C's "populated" fields would flip to "0" and
  355. file modified events will be generated on the "cgroup.events" files of
  356. both cgroups.
  357. Controlling Controllers
  358. -----------------------
  359. Enabling and Disabling
  360. ~~~~~~~~~~~~~~~~~~~~~~
  361. Each cgroup has a "cgroup.controllers" file which lists all
  362. controllers available for the cgroup to enable::
  363. # cat cgroup.controllers
  364. cpu io memory
  365. No controller is enabled by default. Controllers can be enabled and
  366. disabled by writing to the "cgroup.subtree_control" file::
  367. # echo "+cpu +memory -io" > cgroup.subtree_control
  368. Only controllers which are listed in "cgroup.controllers" can be
  369. enabled. When multiple operations are specified as above, either they
  370. all succeed or fail. If multiple operations on the same controller
  371. are specified, the last one is effective.
  372. Enabling a controller in a cgroup indicates that the distribution of
  373. the target resource across its immediate children will be controlled.
  374. Consider the following sub-hierarchy. The enabled controllers are
  375. listed in parentheses::
  376. A(cpu,memory) - B(memory) - C()
  377. \ D()
  378. As A has "cpu" and "memory" enabled, A will control the distribution
  379. of CPU cycles and memory to its children, in this case, B. As B has
  380. "memory" enabled but not "CPU", C and D will compete freely on CPU
  381. cycles but their division of memory available to B will be controlled.
  382. As a controller regulates the distribution of the target resource to
  383. the cgroup's children, enabling it creates the controller's interface
  384. files in the child cgroups. In the above example, enabling "cpu" on B
  385. would create the "cpu." prefixed controller interface files in C and
  386. D. Likewise, disabling "memory" from B would remove the "memory."
  387. prefixed controller interface files from C and D. This means that the
  388. controller interface files - anything which doesn't start with
  389. "cgroup." are owned by the parent rather than the cgroup itself.
  390. Top-down Constraint
  391. ~~~~~~~~~~~~~~~~~~~
  392. Resources are distributed top-down and a cgroup can further distribute
  393. a resource only if the resource has been distributed to it from the
  394. parent. This means that all non-root "cgroup.subtree_control" files
  395. can only contain controllers which are enabled in the parent's
  396. "cgroup.subtree_control" file. A controller can be enabled only if
  397. the parent has the controller enabled and a controller can't be
  398. disabled if one or more children have it enabled.
  399. No Internal Process Constraint
  400. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  401. Non-root cgroups can distribute domain resources to their children
  402. only when they don't have any processes of their own. In other words,
  403. only domain cgroups which don't contain any processes can have domain
  404. controllers enabled in their "cgroup.subtree_control" files.
  405. This guarantees that, when a domain controller is looking at the part
  406. of the hierarchy which has it enabled, processes are always only on
  407. the leaves. This rules out situations where child cgroups compete
  408. against internal processes of the parent.
  409. The root cgroup is exempt from this restriction. Root contains
  410. processes and anonymous resource consumption which can't be associated
  411. with any other cgroups and requires special treatment from most
  412. controllers. How resource consumption in the root cgroup is governed
  413. is up to each controller (for more information on this topic please
  414. refer to the Non-normative information section in the Controllers
  415. chapter).
  416. Note that the restriction doesn't get in the way if there is no
  417. enabled controller in the cgroup's "cgroup.subtree_control". This is
  418. important as otherwise it wouldn't be possible to create children of a
  419. populated cgroup. To control resource distribution of a cgroup, the
  420. cgroup must create children and transfer all its processes to the
  421. children before enabling controllers in its "cgroup.subtree_control"
  422. file.
  423. Delegation
  424. ----------
  425. Model of Delegation
  426. ~~~~~~~~~~~~~~~~~~~
  427. A cgroup can be delegated in two ways. First, to a less privileged
  428. user by granting write access of the directory and its "cgroup.procs",
  429. "cgroup.threads" and "cgroup.subtree_control" files to the user.
  430. Second, if the "nsdelegate" mount option is set, automatically to a
  431. cgroup namespace on namespace creation.
  432. Because the resource control interface files in a given directory
  433. control the distribution of the parent's resources, the delegatee
  434. shouldn't be allowed to write to them. For the first method, this is
  435. achieved by not granting access to these files. For the second, files
  436. outside the namespace should be hidden from the delegatee by the means
  437. of at least mount namespacing, and the kernel rejects writes to all
  438. files on a namespace root from inside the cgroup namespace, except for
  439. those files listed in "/sys/kernel/cgroup/delegate" (including
  440. "cgroup.procs", "cgroup.threads", "cgroup.subtree_control", etc.).
  441. The end results are equivalent for both delegation types. Once
  442. delegated, the user can build sub-hierarchy under the directory,
  443. organize processes inside it as it sees fit and further distribute the
  444. resources it received from the parent. The limits and other settings
  445. of all resource controllers are hierarchical and regardless of what
  446. happens in the delegated sub-hierarchy, nothing can escape the
  447. resource restrictions imposed by the parent.
  448. Currently, cgroup doesn't impose any restrictions on the number of
  449. cgroups in or nesting depth of a delegated sub-hierarchy; however,
  450. this may be limited explicitly in the future.
  451. Delegation Containment
  452. ~~~~~~~~~~~~~~~~~~~~~~
  453. A delegated sub-hierarchy is contained in the sense that processes
  454. can't be moved into or out of the sub-hierarchy by the delegatee.
  455. For delegations to a less privileged user, this is achieved by
  456. requiring the following conditions for a process with a non-root euid
  457. to migrate a target process into a cgroup by writing its PID to the
  458. "cgroup.procs" file.
  459. - The writer must have write access to the "cgroup.procs" file.
  460. - The writer must have write access to the "cgroup.procs" file of the
  461. common ancestor of the source and destination cgroups.
  462. The above two constraints ensure that while a delegatee may migrate
  463. processes around freely in the delegated sub-hierarchy it can't pull
  464. in from or push out to outside the sub-hierarchy.
  465. For an example, let's assume cgroups C0 and C1 have been delegated to
  466. user U0 who created C00, C01 under C0 and C10 under C1 as follows and
  467. all processes under C0 and C1 belong to U0::
  468. ~~~~~~~~~~~~~ - C0 - C00
  469. ~ cgroup ~ \ C01
  470. ~ hierarchy ~
  471. ~~~~~~~~~~~~~ - C1 - C10
  472. Let's also say U0 wants to write the PID of a process which is
  473. currently in C10 into "C00/cgroup.procs". U0 has write access to the
  474. file; however, the common ancestor of the source cgroup C10 and the
  475. destination cgroup C00 is above the points of delegation and U0 would
  476. not have write access to its "cgroup.procs" files and thus the write
  477. will be denied with -EACCES.
  478. For delegations to namespaces, containment is achieved by requiring
  479. that both the source and destination cgroups are reachable from the
  480. namespace of the process which is attempting the migration. If either
  481. is not reachable, the migration is rejected with -ENOENT.
  482. Guidelines
  483. ----------
  484. Organize Once and Control
  485. ~~~~~~~~~~~~~~~~~~~~~~~~~
  486. Migrating a process across cgroups is a relatively expensive operation
  487. and stateful resources such as memory are not moved together with the
  488. process. This is an explicit design decision as there often exist
  489. inherent trade-offs between migration and various hot paths in terms
  490. of synchronization cost.
  491. As such, migrating processes across cgroups frequently as a means to
  492. apply different resource restrictions is discouraged. A workload
  493. should be assigned to a cgroup according to the system's logical and
  494. resource structure once on start-up. Dynamic adjustments to resource
  495. distribution can be made by changing controller configuration through
  496. the interface files.
  497. Avoid Name Collisions
  498. ~~~~~~~~~~~~~~~~~~~~~
  499. Interface files for a cgroup and its children cgroups occupy the same
  500. directory and it is possible to create children cgroups which collide
  501. with interface files.
  502. All cgroup core interface files are prefixed with "cgroup." and each
  503. controller's interface files are prefixed with the controller name and
  504. a dot. A controller's name is composed of lower case alphabets and
  505. '_'s but never begins with an '_' so it can be used as the prefix
  506. character for collision avoidance. Also, interface file names won't
  507. start or end with terms which are often used in categorizing workloads
  508. such as job, service, slice, unit or workload.
  509. cgroup doesn't do anything to prevent name collisions and it's the
  510. user's responsibility to avoid them.
  511. Resource Distribution Models
  512. ============================
  513. cgroup controllers implement several resource distribution schemes
  514. depending on the resource type and expected use cases. This section
  515. describes major schemes in use along with their expected behaviors.
  516. Weights
  517. -------
  518. A parent's resource is distributed by adding up the weights of all
  519. active children and giving each the fraction matching the ratio of its
  520. weight against the sum. As only children which can make use of the
  521. resource at the moment participate in the distribution, this is
  522. work-conserving. Due to the dynamic nature, this model is usually
  523. used for stateless resources.
  524. All weights are in the range [1, 10000] with the default at 100. This
  525. allows symmetric multiplicative biases in both directions at fine
  526. enough granularity while staying in the intuitive range.
  527. As long as the weight is in range, all configuration combinations are
  528. valid and there is no reason to reject configuration changes or
  529. process migrations.
  530. "cpu.weight" proportionally distributes CPU cycles to active children
  531. and is an example of this type.
  532. .. _cgroupv2-limits-distributor:
  533. Limits
  534. ------
  535. A child can only consume up to the configured amount of the resource.
  536. Limits can be over-committed - the sum of the limits of children can
  537. exceed the amount of resource available to the parent.
  538. Limits are in the range [0, max] and defaults to "max", which is noop.
  539. As limits can be over-committed, all configuration combinations are
  540. valid and there is no reason to reject configuration changes or
  541. process migrations.
  542. "io.max" limits the maximum BPS and/or IOPS that a cgroup can consume
  543. on an IO device and is an example of this type.
  544. .. _cgroupv2-protections-distributor:
  545. Protections
  546. -----------
  547. A cgroup is protected up to the configured amount of the resource
  548. as long as the usages of all its ancestors are under their
  549. protected levels. Protections can be hard guarantees or best effort
  550. soft boundaries. Protections can also be over-committed in which case
  551. only up to the amount available to the parent is protected among
  552. children.
  553. Protections are in the range [0, max] and defaults to 0, which is
  554. noop.
  555. As protections can be over-committed, all configuration combinations
  556. are valid and there is no reason to reject configuration changes or
  557. process migrations.
  558. "memory.low" implements best-effort memory protection and is an
  559. example of this type.
  560. Allocations
  561. -----------
  562. A cgroup is exclusively allocated a certain amount of a finite
  563. resource. Allocations can't be over-committed - the sum of the
  564. allocations of children can not exceed the amount of resource
  565. available to the parent.
  566. Allocations are in the range [0, max] and defaults to 0, which is no
  567. resource.
  568. As allocations can't be over-committed, some configuration
  569. combinations are invalid and should be rejected. Also, if the
  570. resource is mandatory for execution of processes, process migrations
  571. may be rejected.
  572. "cpu.rt.max" hard-allocates realtime slices and is an example of this
  573. type.
  574. Interface Files
  575. ===============
  576. Format
  577. ------
  578. All interface files should be in one of the following formats whenever
  579. possible::
  580. New-line separated values
  581. (when only one value can be written at once)
  582. VAL0\n
  583. VAL1\n
  584. ...
  585. Space separated values
  586. (when read-only or multiple values can be written at once)
  587. VAL0 VAL1 ...\n
  588. Flat keyed
  589. KEY0 VAL0\n
  590. KEY1 VAL1\n
  591. ...
  592. Nested keyed
  593. KEY0 SUB_KEY0=VAL00 SUB_KEY1=VAL01...
  594. KEY1 SUB_KEY0=VAL10 SUB_KEY1=VAL11...
  595. ...
  596. For a writable file, the format for writing should generally match
  597. reading; however, controllers may allow omitting later fields or
  598. implement restricted shortcuts for most common use cases.
  599. For both flat and nested keyed files, only the values for a single key
  600. can be written at a time. For nested keyed files, the sub key pairs
  601. may be specified in any order and not all pairs have to be specified.
  602. Conventions
  603. -----------
  604. - Settings for a single feature should be contained in a single file.
  605. - The root cgroup should be exempt from resource control and thus
  606. shouldn't have resource control interface files.
  607. - The default time unit is microseconds. If a different unit is ever
  608. used, an explicit unit suffix must be present.
  609. - A parts-per quantity should use a percentage decimal with at least
  610. two digit fractional part - e.g. 13.40.
  611. - If a controller implements weight based resource distribution, its
  612. interface file should be named "weight" and have the range [1,
  613. 10000] with 100 as the default. The values are chosen to allow
  614. enough and symmetric bias in both directions while keeping it
  615. intuitive (the default is 100%).
  616. - If a controller implements an absolute resource guarantee and/or
  617. limit, the interface files should be named "min" and "max"
  618. respectively. If a controller implements best effort resource
  619. guarantee and/or limit, the interface files should be named "low"
  620. and "high" respectively.
  621. In the above four control files, the special token "max" should be
  622. used to represent upward infinity for both reading and writing.
  623. - If a setting has a configurable default value and keyed specific
  624. overrides, the default entry should be keyed with "default" and
  625. appear as the first entry in the file.
  626. The default value can be updated by writing either "default $VAL" or
  627. "$VAL".
  628. When writing to update a specific override, "default" can be used as
  629. the value to indicate removal of the override. Override entries
  630. with "default" as the value must not appear when read.
  631. For example, a setting which is keyed by major:minor device numbers
  632. with integer values may look like the following::
  633. # cat cgroup-example-interface-file
  634. default 150
  635. 8:0 300
  636. The default value can be updated by::
  637. # echo 125 > cgroup-example-interface-file
  638. or::
  639. # echo "default 125" > cgroup-example-interface-file
  640. An override can be set by::
  641. # echo "8:16 170" > cgroup-example-interface-file
  642. and cleared by::
  643. # echo "8:0 default" > cgroup-example-interface-file
  644. # cat cgroup-example-interface-file
  645. default 125
  646. 8:16 170
  647. - For events which are not very high frequency, an interface file
  648. "events" should be created which lists event key value pairs.
  649. Whenever a notifiable event happens, file modified event should be
  650. generated on the file.
  651. Core Interface Files
  652. --------------------
  653. All cgroup core files are prefixed with "cgroup."
  654. cgroup.type
  655. A read-write single value file which exists on non-root
  656. cgroups.
  657. When read, it indicates the current type of the cgroup, which
  658. can be one of the following values.
  659. - "domain" : A normal valid domain cgroup.
  660. - "domain threaded" : A threaded domain cgroup which is
  661. serving as the root of a threaded subtree.
  662. - "domain invalid" : A cgroup which is in an invalid state.
  663. It can't be populated or have controllers enabled. It may
  664. be allowed to become a threaded cgroup.
  665. - "threaded" : A threaded cgroup which is a member of a
  666. threaded subtree.
  667. A cgroup can be turned into a threaded cgroup by writing
  668. "threaded" to this file.
  669. cgroup.procs
  670. A read-write new-line separated values file which exists on
  671. all cgroups.
  672. When read, it lists the PIDs of all processes which belong to
  673. the cgroup one-per-line. The PIDs are not ordered and the
  674. same PID may show up more than once if the process got moved
  675. to another cgroup and then back or the PID got recycled while
  676. reading.
  677. A PID can be written to migrate the process associated with
  678. the PID to the cgroup. The writer should match all of the
  679. following conditions.
  680. - It must have write access to the "cgroup.procs" file.
  681. - It must have write access to the "cgroup.procs" file of the
  682. common ancestor of the source and destination cgroups.
  683. When delegating a sub-hierarchy, write access to this file
  684. should be granted along with the containing directory.
  685. In a threaded cgroup, reading this file fails with EOPNOTSUPP
  686. as all the processes belong to the thread root. Writing is
  687. supported and moves every thread of the process to the cgroup.
  688. cgroup.threads
  689. A read-write new-line separated values file which exists on
  690. all cgroups.
  691. When read, it lists the TIDs of all threads which belong to
  692. the cgroup one-per-line. The TIDs are not ordered and the
  693. same TID may show up more than once if the thread got moved to
  694. another cgroup and then back or the TID got recycled while
  695. reading.
  696. A TID can be written to migrate the thread associated with the
  697. TID to the cgroup. The writer should match all of the
  698. following conditions.
  699. - It must have write access to the "cgroup.threads" file.
  700. - The cgroup that the thread is currently in must be in the
  701. same resource domain as the destination cgroup.
  702. - It must have write access to the "cgroup.procs" file of the
  703. common ancestor of the source and destination cgroups.
  704. When delegating a sub-hierarchy, write access to this file
  705. should be granted along with the containing directory.
  706. cgroup.controllers
  707. A read-only space separated values file which exists on all
  708. cgroups.
  709. It shows space separated list of all controllers available to
  710. the cgroup. The controllers are not ordered.
  711. cgroup.subtree_control
  712. A read-write space separated values file which exists on all
  713. cgroups. Starts out empty.
  714. When read, it shows space separated list of the controllers
  715. which are enabled to control resource distribution from the
  716. cgroup to its children.
  717. Space separated list of controllers prefixed with '+' or '-'
  718. can be written to enable or disable controllers. A controller
  719. name prefixed with '+' enables the controller and '-'
  720. disables. If a controller appears more than once on the list,
  721. the last one is effective. When multiple enable and disable
  722. operations are specified, either all succeed or all fail.
  723. cgroup.events
  724. A read-only flat-keyed file which exists on non-root cgroups.
  725. The following entries are defined. Unless specified
  726. otherwise, a value change in this file generates a file
  727. modified event.
  728. populated
  729. 1 if the cgroup or its descendants contains any live
  730. processes; otherwise, 0.
  731. frozen
  732. 1 if the cgroup is frozen; otherwise, 0.
  733. cgroup.max.descendants
  734. A read-write single value files. The default is "max".
  735. Maximum allowed number of descent cgroups.
  736. If the actual number of descendants is equal or larger,
  737. an attempt to create a new cgroup in the hierarchy will fail.
  738. cgroup.max.depth
  739. A read-write single value files. The default is "max".
  740. Maximum allowed descent depth below the current cgroup.
  741. If the actual descent depth is equal or larger,
  742. an attempt to create a new child cgroup will fail.
  743. cgroup.stat
  744. A read-only flat-keyed file with the following entries:
  745. nr_descendants
  746. Total number of visible descendant cgroups.
  747. nr_dying_descendants
  748. Total number of dying descendant cgroups. A cgroup becomes
  749. dying after being deleted by a user. The cgroup will remain
  750. in dying state for some time undefined time (which can depend
  751. on system load) before being completely destroyed.
  752. A process can't enter a dying cgroup under any circumstances,
  753. a dying cgroup can't revive.
  754. A dying cgroup can consume system resources not exceeding
  755. limits, which were active at the moment of cgroup deletion.
  756. nr_subsys_<cgroup_subsys>
  757. Total number of live cgroup subsystems (e.g memory
  758. cgroup) at and beneath the current cgroup.
  759. nr_dying_subsys_<cgroup_subsys>
  760. Total number of dying cgroup subsystems (e.g. memory
  761. cgroup) at and beneath the current cgroup.
  762. cgroup.freeze
  763. A read-write single value file which exists on non-root cgroups.
  764. Allowed values are "0" and "1". The default is "0".
  765. Writing "1" to the file causes freezing of the cgroup and all
  766. descendant cgroups. This means that all belonging processes will
  767. be stopped and will not run until the cgroup will be explicitly
  768. unfrozen. Freezing of the cgroup may take some time; when this action
  769. is completed, the "frozen" value in the cgroup.events control file
  770. will be updated to "1" and the corresponding notification will be
  771. issued.
  772. A cgroup can be frozen either by its own settings, or by settings
  773. of any ancestor cgroups. If any of ancestor cgroups is frozen, the
  774. cgroup will remain frozen.
  775. Processes in the frozen cgroup can be killed by a fatal signal.
  776. They also can enter and leave a frozen cgroup: either by an explicit
  777. move by a user, or if freezing of the cgroup races with fork().
  778. If a process is moved to a frozen cgroup, it stops. If a process is
  779. moved out of a frozen cgroup, it becomes running.
  780. Frozen status of a cgroup doesn't affect any cgroup tree operations:
  781. it's possible to delete a frozen (and empty) cgroup, as well as
  782. create new sub-cgroups.
  783. cgroup.kill
  784. A write-only single value file which exists in non-root cgroups.
  785. The only allowed value is "1".
  786. Writing "1" to the file causes the cgroup and all descendant cgroups to
  787. be killed. This means that all processes located in the affected cgroup
  788. tree will be killed via SIGKILL.
  789. Killing a cgroup tree will deal with concurrent forks appropriately and
  790. is protected against migrations.
  791. In a threaded cgroup, writing this file fails with EOPNOTSUPP as
  792. killing cgroups is a process directed operation, i.e. it affects
  793. the whole thread-group.
  794. cgroup.pressure
  795. A read-write single value file that allowed values are "0" and "1".
  796. The default is "1".
  797. Writing "0" to the file will disable the cgroup PSI accounting.
  798. Writing "1" to the file will re-enable the cgroup PSI accounting.
  799. This control attribute is not hierarchical, so disable or enable PSI
  800. accounting in a cgroup does not affect PSI accounting in descendants
  801. and doesn't need pass enablement via ancestors from root.
  802. The reason this control attribute exists is that PSI accounts stalls for
  803. each cgroup separately and aggregates it at each level of the hierarchy.
  804. This may cause non-negligible overhead for some workloads when under
  805. deep level of the hierarchy, in which case this control attribute can
  806. be used to disable PSI accounting in the non-leaf cgroups.
  807. irq.pressure
  808. A read-write nested-keyed file.
  809. Shows pressure stall information for IRQ/SOFTIRQ. See
  810. :ref:`Documentation/accounting/psi.rst <psi>` for details.
  811. Controllers
  812. ===========
  813. .. _cgroup-v2-cpu:
  814. CPU
  815. ---
  816. The "cpu" controllers regulates distribution of CPU cycles. This
  817. controller implements weight and absolute bandwidth limit models for
  818. normal scheduling policy and absolute bandwidth allocation model for
  819. realtime scheduling policy.
  820. In all the above models, cycles distribution is defined only on a temporal
  821. base and it does not account for the frequency at which tasks are executed.
  822. The (optional) utilization clamping support allows to hint the schedutil
  823. cpufreq governor about the minimum desired frequency which should always be
  824. provided by a CPU, as well as the maximum desired frequency, which should not
  825. be exceeded by a CPU.
  826. WARNING: cgroup2 doesn't yet support control of realtime processes. For
  827. a kernel built with the CONFIG_RT_GROUP_SCHED option enabled for group
  828. scheduling of realtime processes, the cpu controller can only be enabled
  829. when all RT processes are in the root cgroup. This limitation does
  830. not apply if CONFIG_RT_GROUP_SCHED is disabled. Be aware that system
  831. management software may already have placed RT processes into nonroot
  832. cgroups during the system boot process, and these processes may need
  833. to be moved to the root cgroup before the cpu controller can be enabled
  834. with a CONFIG_RT_GROUP_SCHED enabled kernel.
  835. CPU Interface Files
  836. ~~~~~~~~~~~~~~~~~~~
  837. All time durations are in microseconds.
  838. cpu.stat
  839. A read-only flat-keyed file.
  840. This file exists whether the controller is enabled or not.
  841. It always reports the following three stats:
  842. - usage_usec
  843. - user_usec
  844. - system_usec
  845. and the following five when the controller is enabled:
  846. - nr_periods
  847. - nr_throttled
  848. - throttled_usec
  849. - nr_bursts
  850. - burst_usec
  851. cpu.weight
  852. A read-write single value file which exists on non-root
  853. cgroups. The default is "100".
  854. For non idle groups (cpu.idle = 0), the weight is in the
  855. range [1, 10000].
  856. If the cgroup has been configured to be SCHED_IDLE (cpu.idle = 1),
  857. then the weight will show as a 0.
  858. cpu.weight.nice
  859. A read-write single value file which exists on non-root
  860. cgroups. The default is "0".
  861. The nice value is in the range [-20, 19].
  862. This interface file is an alternative interface for
  863. "cpu.weight" and allows reading and setting weight using the
  864. same values used by nice(2). Because the range is smaller and
  865. granularity is coarser for the nice values, the read value is
  866. the closest approximation of the current weight.
  867. cpu.max
  868. A read-write two value file which exists on non-root cgroups.
  869. The default is "max 100000".
  870. The maximum bandwidth limit. It's in the following format::
  871. $MAX $PERIOD
  872. which indicates that the group may consume up to $MAX in each
  873. $PERIOD duration. "max" for $MAX indicates no limit. If only
  874. one number is written, $MAX is updated.
  875. cpu.max.burst
  876. A read-write single value file which exists on non-root
  877. cgroups. The default is "0".
  878. The burst in the range [0, $MAX].
  879. cpu.pressure
  880. A read-write nested-keyed file.
  881. Shows pressure stall information for CPU. See
  882. :ref:`Documentation/accounting/psi.rst <psi>` for details.
  883. cpu.uclamp.min
  884. A read-write single value file which exists on non-root cgroups.
  885. The default is "0", i.e. no utilization boosting.
  886. The requested minimum utilization (protection) as a percentage
  887. rational number, e.g. 12.34 for 12.34%.
  888. This interface allows reading and setting minimum utilization clamp
  889. values similar to the sched_setattr(2). This minimum utilization
  890. value is used to clamp the task specific minimum utilization clamp.
  891. The requested minimum utilization (protection) is always capped by
  892. the current value for the maximum utilization (limit), i.e.
  893. `cpu.uclamp.max`.
  894. cpu.uclamp.max
  895. A read-write single value file which exists on non-root cgroups.
  896. The default is "max". i.e. no utilization capping
  897. The requested maximum utilization (limit) as a percentage rational
  898. number, e.g. 98.76 for 98.76%.
  899. This interface allows reading and setting maximum utilization clamp
  900. values similar to the sched_setattr(2). This maximum utilization
  901. value is used to clamp the task specific maximum utilization clamp.
  902. cpu.idle
  903. A read-write single value file which exists on non-root cgroups.
  904. The default is 0.
  905. This is the cgroup analog of the per-task SCHED_IDLE sched policy.
  906. Setting this value to a 1 will make the scheduling policy of the
  907. cgroup SCHED_IDLE. The threads inside the cgroup will retain their
  908. own relative priorities, but the cgroup itself will be treated as
  909. very low priority relative to its peers.
  910. Memory
  911. ------
  912. The "memory" controller regulates distribution of memory. Memory is
  913. stateful and implements both limit and protection models. Due to the
  914. intertwining between memory usage and reclaim pressure and the
  915. stateful nature of memory, the distribution model is relatively
  916. complex.
  917. While not completely water-tight, all major memory usages by a given
  918. cgroup are tracked so that the total memory consumption can be
  919. accounted and controlled to a reasonable extent. Currently, the
  920. following types of memory usages are tracked.
  921. - Userland memory - page cache and anonymous memory.
  922. - Kernel data structures such as dentries and inodes.
  923. - TCP socket buffers.
  924. The above list may expand in the future for better coverage.
  925. Memory Interface Files
  926. ~~~~~~~~~~~~~~~~~~~~~~
  927. All memory amounts are in bytes. If a value which is not aligned to
  928. PAGE_SIZE is written, the value may be rounded up to the closest
  929. PAGE_SIZE multiple when read back.
  930. memory.current
  931. A read-only single value file which exists on non-root
  932. cgroups.
  933. The total amount of memory currently being used by the cgroup
  934. and its descendants.
  935. memory.min
  936. A read-write single value file which exists on non-root
  937. cgroups. The default is "0".
  938. Hard memory protection. If the memory usage of a cgroup
  939. is within its effective min boundary, the cgroup's memory
  940. won't be reclaimed under any conditions. If there is no
  941. unprotected reclaimable memory available, OOM killer
  942. is invoked. Above the effective min boundary (or
  943. effective low boundary if it is higher), pages are reclaimed
  944. proportionally to the overage, reducing reclaim pressure for
  945. smaller overages.
  946. Effective min boundary is limited by memory.min values of
  947. all ancestor cgroups. If there is memory.min overcommitment
  948. (child cgroup or cgroups are requiring more protected memory
  949. than parent will allow), then each child cgroup will get
  950. the part of parent's protection proportional to its
  951. actual memory usage below memory.min.
  952. Putting more memory than generally available under this
  953. protection is discouraged and may lead to constant OOMs.
  954. If a memory cgroup is not populated with processes,
  955. its memory.min is ignored.
  956. memory.low
  957. A read-write single value file which exists on non-root
  958. cgroups. The default is "0".
  959. Best-effort memory protection. If the memory usage of a
  960. cgroup is within its effective low boundary, the cgroup's
  961. memory won't be reclaimed unless there is no reclaimable
  962. memory available in unprotected cgroups.
  963. Above the effective low boundary (or
  964. effective min boundary if it is higher), pages are reclaimed
  965. proportionally to the overage, reducing reclaim pressure for
  966. smaller overages.
  967. Effective low boundary is limited by memory.low values of
  968. all ancestor cgroups. If there is memory.low overcommitment
  969. (child cgroup or cgroups are requiring more protected memory
  970. than parent will allow), then each child cgroup will get
  971. the part of parent's protection proportional to its
  972. actual memory usage below memory.low.
  973. Putting more memory than generally available under this
  974. protection is discouraged.
  975. memory.high
  976. A read-write single value file which exists on non-root
  977. cgroups. The default is "max".
  978. Memory usage throttle limit. If a cgroup's usage goes
  979. over the high boundary, the processes of the cgroup are
  980. throttled and put under heavy reclaim pressure.
  981. Going over the high limit never invokes the OOM killer and
  982. under extreme conditions the limit may be breached. The high
  983. limit should be used in scenarios where an external process
  984. monitors the limited cgroup to alleviate heavy reclaim
  985. pressure.
  986. memory.max
  987. A read-write single value file which exists on non-root
  988. cgroups. The default is "max".
  989. Memory usage hard limit. This is the main mechanism to limit
  990. memory usage of a cgroup. If a cgroup's memory usage reaches
  991. this limit and can't be reduced, the OOM killer is invoked in
  992. the cgroup. Under certain circumstances, the usage may go
  993. over the limit temporarily.
  994. In default configuration regular 0-order allocations always
  995. succeed unless OOM killer chooses current task as a victim.
  996. Some kinds of allocations don't invoke the OOM killer.
  997. Caller could retry them differently, return into userspace
  998. as -ENOMEM or silently ignore in cases like disk readahead.
  999. memory.reclaim
  1000. A write-only nested-keyed file which exists for all cgroups.
  1001. This is a simple interface to trigger memory reclaim in the
  1002. target cgroup.
  1003. Example::
  1004. echo "1G" > memory.reclaim
  1005. Please note that the kernel can over or under reclaim from
  1006. the target cgroup. If less bytes are reclaimed than the
  1007. specified amount, -EAGAIN is returned.
  1008. Please note that the proactive reclaim (triggered by this
  1009. interface) is not meant to indicate memory pressure on the
  1010. memory cgroup. Therefore socket memory balancing triggered by
  1011. the memory reclaim normally is not exercised in this case.
  1012. This means that the networking layer will not adapt based on
  1013. reclaim induced by memory.reclaim.
  1014. The following nested keys are defined.
  1015. ========== ================================
  1016. swappiness Swappiness value to reclaim with
  1017. ========== ================================
  1018. Specifying a swappiness value instructs the kernel to perform
  1019. the reclaim with that swappiness value. Note that this has the
  1020. same semantics as vm.swappiness applied to memcg reclaim with
  1021. all the existing limitations and potential future extensions.
  1022. memory.peak
  1023. A read-write single value file which exists on non-root cgroups.
  1024. The max memory usage recorded for the cgroup and its descendants since
  1025. either the creation of the cgroup or the most recent reset for that FD.
  1026. A write of any non-empty string to this file resets it to the
  1027. current memory usage for subsequent reads through the same
  1028. file descriptor.
  1029. memory.oom.group
  1030. A read-write single value file which exists on non-root
  1031. cgroups. The default value is "0".
  1032. Determines whether the cgroup should be treated as
  1033. an indivisible workload by the OOM killer. If set,
  1034. all tasks belonging to the cgroup or to its descendants
  1035. (if the memory cgroup is not a leaf cgroup) are killed
  1036. together or not at all. This can be used to avoid
  1037. partial kills to guarantee workload integrity.
  1038. Tasks with the OOM protection (oom_score_adj set to -1000)
  1039. are treated as an exception and are never killed.
  1040. If the OOM killer is invoked in a cgroup, it's not going
  1041. to kill any tasks outside of this cgroup, regardless
  1042. memory.oom.group values of ancestor cgroups.
  1043. memory.events
  1044. A read-only flat-keyed file which exists on non-root cgroups.
  1045. The following entries are defined. Unless specified
  1046. otherwise, a value change in this file generates a file
  1047. modified event.
  1048. Note that all fields in this file are hierarchical and the
  1049. file modified event can be generated due to an event down the
  1050. hierarchy. For the local events at the cgroup level see
  1051. memory.events.local.
  1052. low
  1053. The number of times the cgroup is reclaimed due to
  1054. high memory pressure even though its usage is under
  1055. the low boundary. This usually indicates that the low
  1056. boundary is over-committed.
  1057. high
  1058. The number of times processes of the cgroup are
  1059. throttled and routed to perform direct memory reclaim
  1060. because the high memory boundary was exceeded. For a
  1061. cgroup whose memory usage is capped by the high limit
  1062. rather than global memory pressure, this event's
  1063. occurrences are expected.
  1064. max
  1065. The number of times the cgroup's memory usage was
  1066. about to go over the max boundary. If direct reclaim
  1067. fails to bring it down, the cgroup goes to OOM state.
  1068. oom
  1069. The number of time the cgroup's memory usage was
  1070. reached the limit and allocation was about to fail.
  1071. This event is not raised if the OOM killer is not
  1072. considered as an option, e.g. for failed high-order
  1073. allocations or if caller asked to not retry attempts.
  1074. oom_kill
  1075. The number of processes belonging to this cgroup
  1076. killed by any kind of OOM killer.
  1077. oom_group_kill
  1078. The number of times a group OOM has occurred.
  1079. memory.events.local
  1080. Similar to memory.events but the fields in the file are local
  1081. to the cgroup i.e. not hierarchical. The file modified event
  1082. generated on this file reflects only the local events.
  1083. memory.stat
  1084. A read-only flat-keyed file which exists on non-root cgroups.
  1085. This breaks down the cgroup's memory footprint into different
  1086. types of memory, type-specific details, and other information
  1087. on the state and past events of the memory management system.
  1088. All memory amounts are in bytes.
  1089. The entries are ordered to be human readable, and new entries
  1090. can show up in the middle. Don't rely on items remaining in a
  1091. fixed position; use the keys to look up specific values!
  1092. If the entry has no per-node counter (or not show in the
  1093. memory.numa_stat). We use 'npn' (non-per-node) as the tag
  1094. to indicate that it will not show in the memory.numa_stat.
  1095. anon
  1096. Amount of memory used in anonymous mappings such as
  1097. brk(), sbrk(), and mmap(MAP_ANONYMOUS)
  1098. file
  1099. Amount of memory used to cache filesystem data,
  1100. including tmpfs and shared memory.
  1101. kernel (npn)
  1102. Amount of total kernel memory, including
  1103. (kernel_stack, pagetables, percpu, vmalloc, slab) in
  1104. addition to other kernel memory use cases.
  1105. kernel_stack
  1106. Amount of memory allocated to kernel stacks.
  1107. pagetables
  1108. Amount of memory allocated for page tables.
  1109. sec_pagetables
  1110. Amount of memory allocated for secondary page tables,
  1111. this currently includes KVM mmu allocations on x86
  1112. and arm64 and IOMMU page tables.
  1113. percpu (npn)
  1114. Amount of memory used for storing per-cpu kernel
  1115. data structures.
  1116. sock (npn)
  1117. Amount of memory used in network transmission buffers
  1118. vmalloc (npn)
  1119. Amount of memory used for vmap backed memory.
  1120. shmem
  1121. Amount of cached filesystem data that is swap-backed,
  1122. such as tmpfs, shm segments, shared anonymous mmap()s
  1123. zswap
  1124. Amount of memory consumed by the zswap compression backend.
  1125. zswapped
  1126. Amount of application memory swapped out to zswap.
  1127. file_mapped
  1128. Amount of cached filesystem data mapped with mmap()
  1129. file_dirty
  1130. Amount of cached filesystem data that was modified but
  1131. not yet written back to disk
  1132. file_writeback
  1133. Amount of cached filesystem data that was modified and
  1134. is currently being written back to disk
  1135. swapcached
  1136. Amount of swap cached in memory. The swapcache is accounted
  1137. against both memory and swap usage.
  1138. anon_thp
  1139. Amount of memory used in anonymous mappings backed by
  1140. transparent hugepages
  1141. file_thp
  1142. Amount of cached filesystem data backed by transparent
  1143. hugepages
  1144. shmem_thp
  1145. Amount of shm, tmpfs, shared anonymous mmap()s backed by
  1146. transparent hugepages
  1147. inactive_anon, active_anon, inactive_file, active_file, unevictable
  1148. Amount of memory, swap-backed and filesystem-backed,
  1149. on the internal memory management lists used by the
  1150. page reclaim algorithm.
  1151. As these represent internal list state (eg. shmem pages are on anon
  1152. memory management lists), inactive_foo + active_foo may not be equal to
  1153. the value for the foo counter, since the foo counter is type-based, not
  1154. list-based.
  1155. slab_reclaimable
  1156. Part of "slab" that might be reclaimed, such as
  1157. dentries and inodes.
  1158. slab_unreclaimable
  1159. Part of "slab" that cannot be reclaimed on memory
  1160. pressure.
  1161. slab (npn)
  1162. Amount of memory used for storing in-kernel data
  1163. structures.
  1164. workingset_refault_anon
  1165. Number of refaults of previously evicted anonymous pages.
  1166. workingset_refault_file
  1167. Number of refaults of previously evicted file pages.
  1168. workingset_activate_anon
  1169. Number of refaulted anonymous pages that were immediately
  1170. activated.
  1171. workingset_activate_file
  1172. Number of refaulted file pages that were immediately activated.
  1173. workingset_restore_anon
  1174. Number of restored anonymous pages which have been detected as
  1175. an active workingset before they got reclaimed.
  1176. workingset_restore_file
  1177. Number of restored file pages which have been detected as an
  1178. active workingset before they got reclaimed.
  1179. workingset_nodereclaim
  1180. Number of times a shadow node has been reclaimed
  1181. pgscan (npn)
  1182. Amount of scanned pages (in an inactive LRU list)
  1183. pgsteal (npn)
  1184. Amount of reclaimed pages
  1185. pgscan_kswapd (npn)
  1186. Amount of scanned pages by kswapd (in an inactive LRU list)
  1187. pgscan_direct (npn)
  1188. Amount of scanned pages directly (in an inactive LRU list)
  1189. pgscan_khugepaged (npn)
  1190. Amount of scanned pages by khugepaged (in an inactive LRU list)
  1191. pgsteal_kswapd (npn)
  1192. Amount of reclaimed pages by kswapd
  1193. pgsteal_direct (npn)
  1194. Amount of reclaimed pages directly
  1195. pgsteal_khugepaged (npn)
  1196. Amount of reclaimed pages by khugepaged
  1197. pgfault (npn)
  1198. Total number of page faults incurred
  1199. pgmajfault (npn)
  1200. Number of major page faults incurred
  1201. pgrefill (npn)
  1202. Amount of scanned pages (in an active LRU list)
  1203. pgactivate (npn)
  1204. Amount of pages moved to the active LRU list
  1205. pgdeactivate (npn)
  1206. Amount of pages moved to the inactive LRU list
  1207. pglazyfree (npn)
  1208. Amount of pages postponed to be freed under memory pressure
  1209. pglazyfreed (npn)
  1210. Amount of reclaimed lazyfree pages
  1211. swpin_zero
  1212. Number of pages swapped into memory and filled with zero, where I/O
  1213. was optimized out because the page content was detected to be zero
  1214. during swapout.
  1215. swpout_zero
  1216. Number of zero-filled pages swapped out with I/O skipped due to the
  1217. content being detected as zero.
  1218. zswpin
  1219. Number of pages moved in to memory from zswap.
  1220. zswpout
  1221. Number of pages moved out of memory to zswap.
  1222. zswpwb
  1223. Number of pages written from zswap to swap.
  1224. thp_fault_alloc (npn)
  1225. Number of transparent hugepages which were allocated to satisfy
  1226. a page fault. This counter is not present when CONFIG_TRANSPARENT_HUGEPAGE
  1227. is not set.
  1228. thp_collapse_alloc (npn)
  1229. Number of transparent hugepages which were allocated to allow
  1230. collapsing an existing range of pages. This counter is not
  1231. present when CONFIG_TRANSPARENT_HUGEPAGE is not set.
  1232. thp_swpout (npn)
  1233. Number of transparent hugepages which are swapout in one piece
  1234. without splitting.
  1235. thp_swpout_fallback (npn)
  1236. Number of transparent hugepages which were split before swapout.
  1237. Usually because failed to allocate some continuous swap space
  1238. for the huge page.
  1239. numa_pages_migrated (npn)
  1240. Number of pages migrated by NUMA balancing.
  1241. numa_pte_updates (npn)
  1242. Number of pages whose page table entries are modified by
  1243. NUMA balancing to produce NUMA hinting faults on access.
  1244. numa_hint_faults (npn)
  1245. Number of NUMA hinting faults.
  1246. pgdemote_kswapd
  1247. Number of pages demoted by kswapd.
  1248. pgdemote_direct
  1249. Number of pages demoted directly.
  1250. pgdemote_khugepaged
  1251. Number of pages demoted by khugepaged.
  1252. memory.numa_stat
  1253. A read-only nested-keyed file which exists on non-root cgroups.
  1254. This breaks down the cgroup's memory footprint into different
  1255. types of memory, type-specific details, and other information
  1256. per node on the state of the memory management system.
  1257. This is useful for providing visibility into the NUMA locality
  1258. information within an memcg since the pages are allowed to be
  1259. allocated from any physical node. One of the use case is evaluating
  1260. application performance by combining this information with the
  1261. application's CPU allocation.
  1262. All memory amounts are in bytes.
  1263. The output format of memory.numa_stat is::
  1264. type N0=<bytes in node 0> N1=<bytes in node 1> ...
  1265. The entries are ordered to be human readable, and new entries
  1266. can show up in the middle. Don't rely on items remaining in a
  1267. fixed position; use the keys to look up specific values!
  1268. The entries can refer to the memory.stat.
  1269. memory.swap.current
  1270. A read-only single value file which exists on non-root
  1271. cgroups.
  1272. The total amount of swap currently being used by the cgroup
  1273. and its descendants.
  1274. memory.swap.high
  1275. A read-write single value file which exists on non-root
  1276. cgroups. The default is "max".
  1277. Swap usage throttle limit. If a cgroup's swap usage exceeds
  1278. this limit, all its further allocations will be throttled to
  1279. allow userspace to implement custom out-of-memory procedures.
  1280. This limit marks a point of no return for the cgroup. It is NOT
  1281. designed to manage the amount of swapping a workload does
  1282. during regular operation. Compare to memory.swap.max, which
  1283. prohibits swapping past a set amount, but lets the cgroup
  1284. continue unimpeded as long as other memory can be reclaimed.
  1285. Healthy workloads are not expected to reach this limit.
  1286. memory.swap.peak
  1287. A read-write single value file which exists on non-root cgroups.
  1288. The max swap usage recorded for the cgroup and its descendants since
  1289. the creation of the cgroup or the most recent reset for that FD.
  1290. A write of any non-empty string to this file resets it to the
  1291. current memory usage for subsequent reads through the same
  1292. file descriptor.
  1293. memory.swap.max
  1294. A read-write single value file which exists on non-root
  1295. cgroups. The default is "max".
  1296. Swap usage hard limit. If a cgroup's swap usage reaches this
  1297. limit, anonymous memory of the cgroup will not be swapped out.
  1298. memory.swap.events
  1299. A read-only flat-keyed file which exists on non-root cgroups.
  1300. The following entries are defined. Unless specified
  1301. otherwise, a value change in this file generates a file
  1302. modified event.
  1303. high
  1304. The number of times the cgroup's swap usage was over
  1305. the high threshold.
  1306. max
  1307. The number of times the cgroup's swap usage was about
  1308. to go over the max boundary and swap allocation
  1309. failed.
  1310. fail
  1311. The number of times swap allocation failed either
  1312. because of running out of swap system-wide or max
  1313. limit.
  1314. When reduced under the current usage, the existing swap
  1315. entries are reclaimed gradually and the swap usage may stay
  1316. higher than the limit for an extended period of time. This
  1317. reduces the impact on the workload and memory management.
  1318. memory.zswap.current
  1319. A read-only single value file which exists on non-root
  1320. cgroups.
  1321. The total amount of memory consumed by the zswap compression
  1322. backend.
  1323. memory.zswap.max
  1324. A read-write single value file which exists on non-root
  1325. cgroups. The default is "max".
  1326. Zswap usage hard limit. If a cgroup's zswap pool reaches this
  1327. limit, it will refuse to take any more stores before existing
  1328. entries fault back in or are written out to disk.
  1329. memory.zswap.writeback
  1330. A read-write single value file. The default value is "1".
  1331. Note that this setting is hierarchical, i.e. the writeback would be
  1332. implicitly disabled for child cgroups if the upper hierarchy
  1333. does so.
  1334. When this is set to 0, all swapping attempts to swapping devices
  1335. are disabled. This included both zswap writebacks, and swapping due
  1336. to zswap store failures. If the zswap store failures are recurring
  1337. (for e.g if the pages are incompressible), users can observe
  1338. reclaim inefficiency after disabling writeback (because the same
  1339. pages might be rejected again and again).
  1340. Note that this is subtly different from setting memory.swap.max to
  1341. 0, as it still allows for pages to be written to the zswap pool.
  1342. This setting has no effect if zswap is disabled, and swapping
  1343. is allowed unless memory.swap.max is set to 0.
  1344. memory.pressure
  1345. A read-only nested-keyed file.
  1346. Shows pressure stall information for memory. See
  1347. :ref:`Documentation/accounting/psi.rst <psi>` for details.
  1348. Usage Guidelines
  1349. ~~~~~~~~~~~~~~~~
  1350. "memory.high" is the main mechanism to control memory usage.
  1351. Over-committing on high limit (sum of high limits > available memory)
  1352. and letting global memory pressure to distribute memory according to
  1353. usage is a viable strategy.
  1354. Because breach of the high limit doesn't trigger the OOM killer but
  1355. throttles the offending cgroup, a management agent has ample
  1356. opportunities to monitor and take appropriate actions such as granting
  1357. more memory or terminating the workload.
  1358. Determining whether a cgroup has enough memory is not trivial as
  1359. memory usage doesn't indicate whether the workload can benefit from
  1360. more memory. For example, a workload which writes data received from
  1361. network to a file can use all available memory but can also operate as
  1362. performant with a small amount of memory. A measure of memory
  1363. pressure - how much the workload is being impacted due to lack of
  1364. memory - is necessary to determine whether a workload needs more
  1365. memory; unfortunately, memory pressure monitoring mechanism isn't
  1366. implemented yet.
  1367. Memory Ownership
  1368. ~~~~~~~~~~~~~~~~
  1369. A memory area is charged to the cgroup which instantiated it and stays
  1370. charged to the cgroup until the area is released. Migrating a process
  1371. to a different cgroup doesn't move the memory usages that it
  1372. instantiated while in the previous cgroup to the new cgroup.
  1373. A memory area may be used by processes belonging to different cgroups.
  1374. To which cgroup the area will be charged is in-deterministic; however,
  1375. over time, the memory area is likely to end up in a cgroup which has
  1376. enough memory allowance to avoid high reclaim pressure.
  1377. If a cgroup sweeps a considerable amount of memory which is expected
  1378. to be accessed repeatedly by other cgroups, it may make sense to use
  1379. POSIX_FADV_DONTNEED to relinquish the ownership of memory areas
  1380. belonging to the affected files to ensure correct memory ownership.
  1381. IO
  1382. --
  1383. The "io" controller regulates the distribution of IO resources. This
  1384. controller implements both weight based and absolute bandwidth or IOPS
  1385. limit distribution; however, weight based distribution is available
  1386. only if cfq-iosched is in use and neither scheme is available for
  1387. blk-mq devices.
  1388. IO Interface Files
  1389. ~~~~~~~~~~~~~~~~~~
  1390. io.stat
  1391. A read-only nested-keyed file.
  1392. Lines are keyed by $MAJ:$MIN device numbers and not ordered.
  1393. The following nested keys are defined.
  1394. ====== =====================
  1395. rbytes Bytes read
  1396. wbytes Bytes written
  1397. rios Number of read IOs
  1398. wios Number of write IOs
  1399. dbytes Bytes discarded
  1400. dios Number of discard IOs
  1401. ====== =====================
  1402. An example read output follows::
  1403. 8:16 rbytes=1459200 wbytes=314773504 rios=192 wios=353 dbytes=0 dios=0
  1404. 8:0 rbytes=90430464 wbytes=299008000 rios=8950 wios=1252 dbytes=50331648 dios=3021
  1405. io.cost.qos
  1406. A read-write nested-keyed file which exists only on the root
  1407. cgroup.
  1408. This file configures the Quality of Service of the IO cost
  1409. model based controller (CONFIG_BLK_CGROUP_IOCOST) which
  1410. currently implements "io.weight" proportional control. Lines
  1411. are keyed by $MAJ:$MIN device numbers and not ordered. The
  1412. line for a given device is populated on the first write for
  1413. the device on "io.cost.qos" or "io.cost.model". The following
  1414. nested keys are defined.
  1415. ====== =====================================
  1416. enable Weight-based control enable
  1417. ctrl "auto" or "user"
  1418. rpct Read latency percentile [0, 100]
  1419. rlat Read latency threshold
  1420. wpct Write latency percentile [0, 100]
  1421. wlat Write latency threshold
  1422. min Minimum scaling percentage [1, 10000]
  1423. max Maximum scaling percentage [1, 10000]
  1424. ====== =====================================
  1425. The controller is disabled by default and can be enabled by
  1426. setting "enable" to 1. "rpct" and "wpct" parameters default
  1427. to zero and the controller uses internal device saturation
  1428. state to adjust the overall IO rate between "min" and "max".
  1429. When a better control quality is needed, latency QoS
  1430. parameters can be configured. For example::
  1431. 8:16 enable=1 ctrl=auto rpct=95.00 rlat=75000 wpct=95.00 wlat=150000 min=50.00 max=150.0
  1432. shows that on sdb, the controller is enabled, will consider
  1433. the device saturated if the 95th percentile of read completion
  1434. latencies is above 75ms or write 150ms, and adjust the overall
  1435. IO issue rate between 50% and 150% accordingly.
  1436. The lower the saturation point, the better the latency QoS at
  1437. the cost of aggregate bandwidth. The narrower the allowed
  1438. adjustment range between "min" and "max", the more conformant
  1439. to the cost model the IO behavior. Note that the IO issue
  1440. base rate may be far off from 100% and setting "min" and "max"
  1441. blindly can lead to a significant loss of device capacity or
  1442. control quality. "min" and "max" are useful for regulating
  1443. devices which show wide temporary behavior changes - e.g. a
  1444. ssd which accepts writes at the line speed for a while and
  1445. then completely stalls for multiple seconds.
  1446. When "ctrl" is "auto", the parameters are controlled by the
  1447. kernel and may change automatically. Setting "ctrl" to "user"
  1448. or setting any of the percentile and latency parameters puts
  1449. it into "user" mode and disables the automatic changes. The
  1450. automatic mode can be restored by setting "ctrl" to "auto".
  1451. io.cost.model
  1452. A read-write nested-keyed file which exists only on the root
  1453. cgroup.
  1454. This file configures the cost model of the IO cost model based
  1455. controller (CONFIG_BLK_CGROUP_IOCOST) which currently
  1456. implements "io.weight" proportional control. Lines are keyed
  1457. by $MAJ:$MIN device numbers and not ordered. The line for a
  1458. given device is populated on the first write for the device on
  1459. "io.cost.qos" or "io.cost.model". The following nested keys
  1460. are defined.
  1461. ===== ================================
  1462. ctrl "auto" or "user"
  1463. model The cost model in use - "linear"
  1464. ===== ================================
  1465. When "ctrl" is "auto", the kernel may change all parameters
  1466. dynamically. When "ctrl" is set to "user" or any other
  1467. parameters are written to, "ctrl" become "user" and the
  1468. automatic changes are disabled.
  1469. When "model" is "linear", the following model parameters are
  1470. defined.
  1471. ============= ========================================
  1472. [r|w]bps The maximum sequential IO throughput
  1473. [r|w]seqiops The maximum 4k sequential IOs per second
  1474. [r|w]randiops The maximum 4k random IOs per second
  1475. ============= ========================================
  1476. From the above, the builtin linear model determines the base
  1477. costs of a sequential and random IO and the cost coefficient
  1478. for the IO size. While simple, this model can cover most
  1479. common device classes acceptably.
  1480. The IO cost model isn't expected to be accurate in absolute
  1481. sense and is scaled to the device behavior dynamically.
  1482. If needed, tools/cgroup/iocost_coef_gen.py can be used to
  1483. generate device-specific coefficients.
  1484. io.weight
  1485. A read-write flat-keyed file which exists on non-root cgroups.
  1486. The default is "default 100".
  1487. The first line is the default weight applied to devices
  1488. without specific override. The rest are overrides keyed by
  1489. $MAJ:$MIN device numbers and not ordered. The weights are in
  1490. the range [1, 10000] and specifies the relative amount IO time
  1491. the cgroup can use in relation to its siblings.
  1492. The default weight can be updated by writing either "default
  1493. $WEIGHT" or simply "$WEIGHT". Overrides can be set by writing
  1494. "$MAJ:$MIN $WEIGHT" and unset by writing "$MAJ:$MIN default".
  1495. An example read output follows::
  1496. default 100
  1497. 8:16 200
  1498. 8:0 50
  1499. io.max
  1500. A read-write nested-keyed file which exists on non-root
  1501. cgroups.
  1502. BPS and IOPS based IO limit. Lines are keyed by $MAJ:$MIN
  1503. device numbers and not ordered. The following nested keys are
  1504. defined.
  1505. ===== ==================================
  1506. rbps Max read bytes per second
  1507. wbps Max write bytes per second
  1508. riops Max read IO operations per second
  1509. wiops Max write IO operations per second
  1510. ===== ==================================
  1511. When writing, any number of nested key-value pairs can be
  1512. specified in any order. "max" can be specified as the value
  1513. to remove a specific limit. If the same key is specified
  1514. multiple times, the outcome is undefined.
  1515. BPS and IOPS are measured in each IO direction and IOs are
  1516. delayed if limit is reached. Temporary bursts are allowed.
  1517. Setting read limit at 2M BPS and write at 120 IOPS for 8:16::
  1518. echo "8:16 rbps=2097152 wiops=120" > io.max
  1519. Reading returns the following::
  1520. 8:16 rbps=2097152 wbps=max riops=max wiops=120
  1521. Write IOPS limit can be removed by writing the following::
  1522. echo "8:16 wiops=max" > io.max
  1523. Reading now returns the following::
  1524. 8:16 rbps=2097152 wbps=max riops=max wiops=max
  1525. io.pressure
  1526. A read-only nested-keyed file.
  1527. Shows pressure stall information for IO. See
  1528. :ref:`Documentation/accounting/psi.rst <psi>` for details.
  1529. Writeback
  1530. ~~~~~~~~~
  1531. Page cache is dirtied through buffered writes and shared mmaps and
  1532. written asynchronously to the backing filesystem by the writeback
  1533. mechanism. Writeback sits between the memory and IO domains and
  1534. regulates the proportion of dirty memory by balancing dirtying and
  1535. write IOs.
  1536. The io controller, in conjunction with the memory controller,
  1537. implements control of page cache writeback IOs. The memory controller
  1538. defines the memory domain that dirty memory ratio is calculated and
  1539. maintained for and the io controller defines the io domain which
  1540. writes out dirty pages for the memory domain. Both system-wide and
  1541. per-cgroup dirty memory states are examined and the more restrictive
  1542. of the two is enforced.
  1543. cgroup writeback requires explicit support from the underlying
  1544. filesystem. Currently, cgroup writeback is implemented on ext2, ext4,
  1545. btrfs, f2fs, and xfs. On other filesystems, all writeback IOs are
  1546. attributed to the root cgroup.
  1547. There are inherent differences in memory and writeback management
  1548. which affects how cgroup ownership is tracked. Memory is tracked per
  1549. page while writeback per inode. For the purpose of writeback, an
  1550. inode is assigned to a cgroup and all IO requests to write dirty pages
  1551. from the inode are attributed to that cgroup.
  1552. As cgroup ownership for memory is tracked per page, there can be pages
  1553. which are associated with different cgroups than the one the inode is
  1554. associated with. These are called foreign pages. The writeback
  1555. constantly keeps track of foreign pages and, if a particular foreign
  1556. cgroup becomes the majority over a certain period of time, switches
  1557. the ownership of the inode to that cgroup.
  1558. While this model is enough for most use cases where a given inode is
  1559. mostly dirtied by a single cgroup even when the main writing cgroup
  1560. changes over time, use cases where multiple cgroups write to a single
  1561. inode simultaneously are not supported well. In such circumstances, a
  1562. significant portion of IOs are likely to be attributed incorrectly.
  1563. As memory controller assigns page ownership on the first use and
  1564. doesn't update it until the page is released, even if writeback
  1565. strictly follows page ownership, multiple cgroups dirtying overlapping
  1566. areas wouldn't work as expected. It's recommended to avoid such usage
  1567. patterns.
  1568. The sysctl knobs which affect writeback behavior are applied to cgroup
  1569. writeback as follows.
  1570. vm.dirty_background_ratio, vm.dirty_ratio
  1571. These ratios apply the same to cgroup writeback with the
  1572. amount of available memory capped by limits imposed by the
  1573. memory controller and system-wide clean memory.
  1574. vm.dirty_background_bytes, vm.dirty_bytes
  1575. For cgroup writeback, this is calculated into ratio against
  1576. total available memory and applied the same way as
  1577. vm.dirty[_background]_ratio.
  1578. IO Latency
  1579. ~~~~~~~~~~
  1580. This is a cgroup v2 controller for IO workload protection. You provide a group
  1581. with a latency target, and if the average latency exceeds that target the
  1582. controller will throttle any peers that have a lower latency target than the
  1583. protected workload.
  1584. The limits are only applied at the peer level in the hierarchy. This means that
  1585. in the diagram below, only groups A, B, and C will influence each other, and
  1586. groups D and F will influence each other. Group G will influence nobody::
  1587. [root]
  1588. / | \
  1589. A B C
  1590. / \ |
  1591. D F G
  1592. So the ideal way to configure this is to set io.latency in groups A, B, and C.
  1593. Generally you do not want to set a value lower than the latency your device
  1594. supports. Experiment to find the value that works best for your workload.
  1595. Start at higher than the expected latency for your device and watch the
  1596. avg_lat value in io.stat for your workload group to get an idea of the
  1597. latency you see during normal operation. Use the avg_lat value as a basis for
  1598. your real setting, setting at 10-15% higher than the value in io.stat.
  1599. How IO Latency Throttling Works
  1600. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1601. io.latency is work conserving; so as long as everybody is meeting their latency
  1602. target the controller doesn't do anything. Once a group starts missing its
  1603. target it begins throttling any peer group that has a higher target than itself.
  1604. This throttling takes 2 forms:
  1605. - Queue depth throttling. This is the number of outstanding IO's a group is
  1606. allowed to have. We will clamp down relatively quickly, starting at no limit
  1607. and going all the way down to 1 IO at a time.
  1608. - Artificial delay induction. There are certain types of IO that cannot be
  1609. throttled without possibly adversely affecting higher priority groups. This
  1610. includes swapping and metadata IO. These types of IO are allowed to occur
  1611. normally, however they are "charged" to the originating group. If the
  1612. originating group is being throttled you will see the use_delay and delay
  1613. fields in io.stat increase. The delay value is how many microseconds that are
  1614. being added to any process that runs in this group. Because this number can
  1615. grow quite large if there is a lot of swapping or metadata IO occurring we
  1616. limit the individual delay events to 1 second at a time.
  1617. Once the victimized group starts meeting its latency target again it will start
  1618. unthrottling any peer groups that were throttled previously. If the victimized
  1619. group simply stops doing IO the global counter will unthrottle appropriately.
  1620. IO Latency Interface Files
  1621. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  1622. io.latency
  1623. This takes a similar format as the other controllers.
  1624. "MAJOR:MINOR target=<target time in microseconds>"
  1625. io.stat
  1626. If the controller is enabled you will see extra stats in io.stat in
  1627. addition to the normal ones.
  1628. depth
  1629. This is the current queue depth for the group.
  1630. avg_lat
  1631. This is an exponential moving average with a decay rate of 1/exp
  1632. bound by the sampling interval. The decay rate interval can be
  1633. calculated by multiplying the win value in io.stat by the
  1634. corresponding number of samples based on the win value.
  1635. win
  1636. The sampling window size in milliseconds. This is the minimum
  1637. duration of time between evaluation events. Windows only elapse
  1638. with IO activity. Idle periods extend the most recent window.
  1639. IO Priority
  1640. ~~~~~~~~~~~
  1641. A single attribute controls the behavior of the I/O priority cgroup policy,
  1642. namely the io.prio.class attribute. The following values are accepted for
  1643. that attribute:
  1644. no-change
  1645. Do not modify the I/O priority class.
  1646. promote-to-rt
  1647. For requests that have a non-RT I/O priority class, change it into RT.
  1648. Also change the priority level of these requests to 4. Do not modify
  1649. the I/O priority of requests that have priority class RT.
  1650. restrict-to-be
  1651. For requests that do not have an I/O priority class or that have I/O
  1652. priority class RT, change it into BE. Also change the priority level
  1653. of these requests to 0. Do not modify the I/O priority class of
  1654. requests that have priority class IDLE.
  1655. idle
  1656. Change the I/O priority class of all requests into IDLE, the lowest
  1657. I/O priority class.
  1658. none-to-rt
  1659. Deprecated. Just an alias for promote-to-rt.
  1660. The following numerical values are associated with the I/O priority policies:
  1661. +----------------+---+
  1662. | no-change | 0 |
  1663. +----------------+---+
  1664. | promote-to-rt | 1 |
  1665. +----------------+---+
  1666. | restrict-to-be | 2 |
  1667. +----------------+---+
  1668. | idle | 3 |
  1669. +----------------+---+
  1670. The numerical value that corresponds to each I/O priority class is as follows:
  1671. +-------------------------------+---+
  1672. | IOPRIO_CLASS_NONE | 0 |
  1673. +-------------------------------+---+
  1674. | IOPRIO_CLASS_RT (real-time) | 1 |
  1675. +-------------------------------+---+
  1676. | IOPRIO_CLASS_BE (best effort) | 2 |
  1677. +-------------------------------+---+
  1678. | IOPRIO_CLASS_IDLE | 3 |
  1679. +-------------------------------+---+
  1680. The algorithm to set the I/O priority class for a request is as follows:
  1681. - If I/O priority class policy is promote-to-rt, change the request I/O
  1682. priority class to IOPRIO_CLASS_RT and change the request I/O priority
  1683. level to 4.
  1684. - If I/O priority class policy is not promote-to-rt, translate the I/O priority
  1685. class policy into a number, then change the request I/O priority class
  1686. into the maximum of the I/O priority class policy number and the numerical
  1687. I/O priority class.
  1688. PID
  1689. ---
  1690. The process number controller is used to allow a cgroup to stop any
  1691. new tasks from being fork()'d or clone()'d after a specified limit is
  1692. reached.
  1693. The number of tasks in a cgroup can be exhausted in ways which other
  1694. controllers cannot prevent, thus warranting its own controller. For
  1695. example, a fork bomb is likely to exhaust the number of tasks before
  1696. hitting memory restrictions.
  1697. Note that PIDs used in this controller refer to TIDs, process IDs as
  1698. used by the kernel.
  1699. PID Interface Files
  1700. ~~~~~~~~~~~~~~~~~~~
  1701. pids.max
  1702. A read-write single value file which exists on non-root
  1703. cgroups. The default is "max".
  1704. Hard limit of number of processes.
  1705. pids.current
  1706. A read-only single value file which exists on non-root cgroups.
  1707. The number of processes currently in the cgroup and its
  1708. descendants.
  1709. pids.peak
  1710. A read-only single value file which exists on non-root cgroups.
  1711. The maximum value that the number of processes in the cgroup and its
  1712. descendants has ever reached.
  1713. pids.events
  1714. A read-only flat-keyed file which exists on non-root cgroups. Unless
  1715. specified otherwise, a value change in this file generates a file
  1716. modified event. The following entries are defined.
  1717. max
  1718. The number of times the cgroup's total number of processes hit the pids.max
  1719. limit (see also pids_localevents).
  1720. pids.events.local
  1721. Similar to pids.events but the fields in the file are local
  1722. to the cgroup i.e. not hierarchical. The file modified event
  1723. generated on this file reflects only the local events.
  1724. Organisational operations are not blocked by cgroup policies, so it is
  1725. possible to have pids.current > pids.max. This can be done by either
  1726. setting the limit to be smaller than pids.current, or attaching enough
  1727. processes to the cgroup such that pids.current is larger than
  1728. pids.max. However, it is not possible to violate a cgroup PID policy
  1729. through fork() or clone(). These will return -EAGAIN if the creation
  1730. of a new process would cause a cgroup policy to be violated.
  1731. Cpuset
  1732. ------
  1733. The "cpuset" controller provides a mechanism for constraining
  1734. the CPU and memory node placement of tasks to only the resources
  1735. specified in the cpuset interface files in a task's current cgroup.
  1736. This is especially valuable on large NUMA systems where placing jobs
  1737. on properly sized subsets of the systems with careful processor and
  1738. memory placement to reduce cross-node memory access and contention
  1739. can improve overall system performance.
  1740. The "cpuset" controller is hierarchical. That means the controller
  1741. cannot use CPUs or memory nodes not allowed in its parent.
  1742. Cpuset Interface Files
  1743. ~~~~~~~~~~~~~~~~~~~~~~
  1744. cpuset.cpus
  1745. A read-write multiple values file which exists on non-root
  1746. cpuset-enabled cgroups.
  1747. It lists the requested CPUs to be used by tasks within this
  1748. cgroup. The actual list of CPUs to be granted, however, is
  1749. subjected to constraints imposed by its parent and can differ
  1750. from the requested CPUs.
  1751. The CPU numbers are comma-separated numbers or ranges.
  1752. For example::
  1753. # cat cpuset.cpus
  1754. 0-4,6,8-10
  1755. An empty value indicates that the cgroup is using the same
  1756. setting as the nearest cgroup ancestor with a non-empty
  1757. "cpuset.cpus" or all the available CPUs if none is found.
  1758. The value of "cpuset.cpus" stays constant until the next update
  1759. and won't be affected by any CPU hotplug events.
  1760. cpuset.cpus.effective
  1761. A read-only multiple values file which exists on all
  1762. cpuset-enabled cgroups.
  1763. It lists the onlined CPUs that are actually granted to this
  1764. cgroup by its parent. These CPUs are allowed to be used by
  1765. tasks within the current cgroup.
  1766. If "cpuset.cpus" is empty, the "cpuset.cpus.effective" file shows
  1767. all the CPUs from the parent cgroup that can be available to
  1768. be used by this cgroup. Otherwise, it should be a subset of
  1769. "cpuset.cpus" unless none of the CPUs listed in "cpuset.cpus"
  1770. can be granted. In this case, it will be treated just like an
  1771. empty "cpuset.cpus".
  1772. Its value will be affected by CPU hotplug events.
  1773. cpuset.mems
  1774. A read-write multiple values file which exists on non-root
  1775. cpuset-enabled cgroups.
  1776. It lists the requested memory nodes to be used by tasks within
  1777. this cgroup. The actual list of memory nodes granted, however,
  1778. is subjected to constraints imposed by its parent and can differ
  1779. from the requested memory nodes.
  1780. The memory node numbers are comma-separated numbers or ranges.
  1781. For example::
  1782. # cat cpuset.mems
  1783. 0-1,3
  1784. An empty value indicates that the cgroup is using the same
  1785. setting as the nearest cgroup ancestor with a non-empty
  1786. "cpuset.mems" or all the available memory nodes if none
  1787. is found.
  1788. The value of "cpuset.mems" stays constant until the next update
  1789. and won't be affected by any memory nodes hotplug events.
  1790. Setting a non-empty value to "cpuset.mems" causes memory of
  1791. tasks within the cgroup to be migrated to the designated nodes if
  1792. they are currently using memory outside of the designated nodes.
  1793. There is a cost for this memory migration. The migration
  1794. may not be complete and some memory pages may be left behind.
  1795. So it is recommended that "cpuset.mems" should be set properly
  1796. before spawning new tasks into the cpuset. Even if there is
  1797. a need to change "cpuset.mems" with active tasks, it shouldn't
  1798. be done frequently.
  1799. cpuset.mems.effective
  1800. A read-only multiple values file which exists on all
  1801. cpuset-enabled cgroups.
  1802. It lists the onlined memory nodes that are actually granted to
  1803. this cgroup by its parent. These memory nodes are allowed to
  1804. be used by tasks within the current cgroup.
  1805. If "cpuset.mems" is empty, it shows all the memory nodes from the
  1806. parent cgroup that will be available to be used by this cgroup.
  1807. Otherwise, it should be a subset of "cpuset.mems" unless none of
  1808. the memory nodes listed in "cpuset.mems" can be granted. In this
  1809. case, it will be treated just like an empty "cpuset.mems".
  1810. Its value will be affected by memory nodes hotplug events.
  1811. cpuset.cpus.exclusive
  1812. A read-write multiple values file which exists on non-root
  1813. cpuset-enabled cgroups.
  1814. It lists all the exclusive CPUs that are allowed to be used
  1815. to create a new cpuset partition. Its value is not used
  1816. unless the cgroup becomes a valid partition root. See the
  1817. "cpuset.cpus.partition" section below for a description of what
  1818. a cpuset partition is.
  1819. When the cgroup becomes a partition root, the actual exclusive
  1820. CPUs that are allocated to that partition are listed in
  1821. "cpuset.cpus.exclusive.effective" which may be different
  1822. from "cpuset.cpus.exclusive". If "cpuset.cpus.exclusive"
  1823. has previously been set, "cpuset.cpus.exclusive.effective"
  1824. is always a subset of it.
  1825. Users can manually set it to a value that is different from
  1826. "cpuset.cpus". One constraint in setting it is that the list of
  1827. CPUs must be exclusive with respect to "cpuset.cpus.exclusive"
  1828. of its sibling. If "cpuset.cpus.exclusive" of a sibling cgroup
  1829. isn't set, its "cpuset.cpus" value, if set, cannot be a subset
  1830. of it to leave at least one CPU available when the exclusive
  1831. CPUs are taken away.
  1832. For a parent cgroup, any one of its exclusive CPUs can only
  1833. be distributed to at most one of its child cgroups. Having an
  1834. exclusive CPU appearing in two or more of its child cgroups is
  1835. not allowed (the exclusivity rule). A value that violates the
  1836. exclusivity rule will be rejected with a write error.
  1837. The root cgroup is a partition root and all its available CPUs
  1838. are in its exclusive CPU set.
  1839. cpuset.cpus.exclusive.effective
  1840. A read-only multiple values file which exists on all non-root
  1841. cpuset-enabled cgroups.
  1842. This file shows the effective set of exclusive CPUs that
  1843. can be used to create a partition root. The content
  1844. of this file will always be a subset of its parent's
  1845. "cpuset.cpus.exclusive.effective" if its parent is not the root
  1846. cgroup. It will also be a subset of "cpuset.cpus.exclusive"
  1847. if it is set. If "cpuset.cpus.exclusive" is not set, it is
  1848. treated to have an implicit value of "cpuset.cpus" in the
  1849. formation of local partition.
  1850. cpuset.cpus.isolated
  1851. A read-only and root cgroup only multiple values file.
  1852. This file shows the set of all isolated CPUs used in existing
  1853. isolated partitions. It will be empty if no isolated partition
  1854. is created.
  1855. cpuset.cpus.partition
  1856. A read-write single value file which exists on non-root
  1857. cpuset-enabled cgroups. This flag is owned by the parent cgroup
  1858. and is not delegatable.
  1859. It accepts only the following input values when written to.
  1860. ========== =====================================
  1861. "member" Non-root member of a partition
  1862. "root" Partition root
  1863. "isolated" Partition root without load balancing
  1864. ========== =====================================
  1865. A cpuset partition is a collection of cpuset-enabled cgroups with
  1866. a partition root at the top of the hierarchy and its descendants
  1867. except those that are separate partition roots themselves and
  1868. their descendants. A partition has exclusive access to the
  1869. set of exclusive CPUs allocated to it. Other cgroups outside
  1870. of that partition cannot use any CPUs in that set.
  1871. There are two types of partitions - local and remote. A local
  1872. partition is one whose parent cgroup is also a valid partition
  1873. root. A remote partition is one whose parent cgroup is not a
  1874. valid partition root itself. Writing to "cpuset.cpus.exclusive"
  1875. is optional for the creation of a local partition as its
  1876. "cpuset.cpus.exclusive" file will assume an implicit value that
  1877. is the same as "cpuset.cpus" if it is not set. Writing the
  1878. proper "cpuset.cpus.exclusive" values down the cgroup hierarchy
  1879. before the target partition root is mandatory for the creation
  1880. of a remote partition.
  1881. Currently, a remote partition cannot be created under a local
  1882. partition. All the ancestors of a remote partition root except
  1883. the root cgroup cannot be a partition root.
  1884. The root cgroup is always a partition root and its state cannot
  1885. be changed. All other non-root cgroups start out as "member".
  1886. When set to "root", the current cgroup is the root of a new
  1887. partition or scheduling domain. The set of exclusive CPUs is
  1888. determined by the value of its "cpuset.cpus.exclusive.effective".
  1889. When set to "isolated", the CPUs in that partition will be in
  1890. an isolated state without any load balancing from the scheduler
  1891. and excluded from the unbound workqueues. Tasks placed in such
  1892. a partition with multiple CPUs should be carefully distributed
  1893. and bound to each of the individual CPUs for optimal performance.
  1894. A partition root ("root" or "isolated") can be in one of the
  1895. two possible states - valid or invalid. An invalid partition
  1896. root is in a degraded state where some state information may
  1897. be retained, but behaves more like a "member".
  1898. All possible state transitions among "member", "root" and
  1899. "isolated" are allowed.
  1900. On read, the "cpuset.cpus.partition" file can show the following
  1901. values.
  1902. ============================= =====================================
  1903. "member" Non-root member of a partition
  1904. "root" Partition root
  1905. "isolated" Partition root without load balancing
  1906. "root invalid (<reason>)" Invalid partition root
  1907. "isolated invalid (<reason>)" Invalid isolated partition root
  1908. ============================= =====================================
  1909. In the case of an invalid partition root, a descriptive string on
  1910. why the partition is invalid is included within parentheses.
  1911. For a local partition root to be valid, the following conditions
  1912. must be met.
  1913. 1) The parent cgroup is a valid partition root.
  1914. 2) The "cpuset.cpus.exclusive.effective" file cannot be empty,
  1915. though it may contain offline CPUs.
  1916. 3) The "cpuset.cpus.effective" cannot be empty unless there is
  1917. no task associated with this partition.
  1918. For a remote partition root to be valid, all the above conditions
  1919. except the first one must be met.
  1920. External events like hotplug or changes to "cpuset.cpus" or
  1921. "cpuset.cpus.exclusive" can cause a valid partition root to
  1922. become invalid and vice versa. Note that a task cannot be
  1923. moved to a cgroup with empty "cpuset.cpus.effective".
  1924. A valid non-root parent partition may distribute out all its CPUs
  1925. to its child local partitions when there is no task associated
  1926. with it.
  1927. Care must be taken to change a valid partition root to "member"
  1928. as all its child local partitions, if present, will become
  1929. invalid causing disruption to tasks running in those child
  1930. partitions. These inactivated partitions could be recovered if
  1931. their parent is switched back to a partition root with a proper
  1932. value in "cpuset.cpus" or "cpuset.cpus.exclusive".
  1933. Poll and inotify events are triggered whenever the state of
  1934. "cpuset.cpus.partition" changes. That includes changes caused
  1935. by write to "cpuset.cpus.partition", cpu hotplug or other
  1936. changes that modify the validity status of the partition.
  1937. This will allow user space agents to monitor unexpected changes
  1938. to "cpuset.cpus.partition" without the need to do continuous
  1939. polling.
  1940. A user can pre-configure certain CPUs to an isolated state
  1941. with load balancing disabled at boot time with the "isolcpus"
  1942. kernel boot command line option. If those CPUs are to be put
  1943. into a partition, they have to be used in an isolated partition.
  1944. Device controller
  1945. -----------------
  1946. Device controller manages access to device files. It includes both
  1947. creation of new device files (using mknod), and access to the
  1948. existing device files.
  1949. Cgroup v2 device controller has no interface files and is implemented
  1950. on top of cgroup BPF. To control access to device files, a user may
  1951. create bpf programs of type BPF_PROG_TYPE_CGROUP_DEVICE and attach
  1952. them to cgroups with BPF_CGROUP_DEVICE flag. On an attempt to access a
  1953. device file, corresponding BPF programs will be executed, and depending
  1954. on the return value the attempt will succeed or fail with -EPERM.
  1955. A BPF_PROG_TYPE_CGROUP_DEVICE program takes a pointer to the
  1956. bpf_cgroup_dev_ctx structure, which describes the device access attempt:
  1957. access type (mknod/read/write) and device (type, major and minor numbers).
  1958. If the program returns 0, the attempt fails with -EPERM, otherwise it
  1959. succeeds.
  1960. An example of BPF_PROG_TYPE_CGROUP_DEVICE program may be found in
  1961. tools/testing/selftests/bpf/progs/dev_cgroup.c in the kernel source tree.
  1962. RDMA
  1963. ----
  1964. The "rdma" controller regulates the distribution and accounting of
  1965. RDMA resources.
  1966. RDMA Interface Files
  1967. ~~~~~~~~~~~~~~~~~~~~
  1968. rdma.max
  1969. A readwrite nested-keyed file that exists for all the cgroups
  1970. except root that describes current configured resource limit
  1971. for a RDMA/IB device.
  1972. Lines are keyed by device name and are not ordered.
  1973. Each line contains space separated resource name and its configured
  1974. limit that can be distributed.
  1975. The following nested keys are defined.
  1976. ========== =============================
  1977. hca_handle Maximum number of HCA Handles
  1978. hca_object Maximum number of HCA Objects
  1979. ========== =============================
  1980. An example for mlx4 and ocrdma device follows::
  1981. mlx4_0 hca_handle=2 hca_object=2000
  1982. ocrdma1 hca_handle=3 hca_object=max
  1983. rdma.current
  1984. A read-only file that describes current resource usage.
  1985. It exists for all the cgroup except root.
  1986. An example for mlx4 and ocrdma device follows::
  1987. mlx4_0 hca_handle=1 hca_object=20
  1988. ocrdma1 hca_handle=1 hca_object=23
  1989. HugeTLB
  1990. -------
  1991. The HugeTLB controller allows to limit the HugeTLB usage per control group and
  1992. enforces the controller limit during page fault.
  1993. HugeTLB Interface Files
  1994. ~~~~~~~~~~~~~~~~~~~~~~~
  1995. hugetlb.<hugepagesize>.current
  1996. Show current usage for "hugepagesize" hugetlb. It exists for all
  1997. the cgroup except root.
  1998. hugetlb.<hugepagesize>.max
  1999. Set/show the hard limit of "hugepagesize" hugetlb usage.
  2000. The default value is "max". It exists for all the cgroup except root.
  2001. hugetlb.<hugepagesize>.events
  2002. A read-only flat-keyed file which exists on non-root cgroups.
  2003. max
  2004. The number of allocation failure due to HugeTLB limit
  2005. hugetlb.<hugepagesize>.events.local
  2006. Similar to hugetlb.<hugepagesize>.events but the fields in the file
  2007. are local to the cgroup i.e. not hierarchical. The file modified event
  2008. generated on this file reflects only the local events.
  2009. hugetlb.<hugepagesize>.numa_stat
  2010. Similar to memory.numa_stat, it shows the numa information of the
  2011. hugetlb pages of <hugepagesize> in this cgroup. Only active in
  2012. use hugetlb pages are included. The per-node values are in bytes.
  2013. Misc
  2014. ----
  2015. The Miscellaneous cgroup provides the resource limiting and tracking
  2016. mechanism for the scalar resources which cannot be abstracted like the other
  2017. cgroup resources. Controller is enabled by the CONFIG_CGROUP_MISC config
  2018. option.
  2019. A resource can be added to the controller via enum misc_res_type{} in the
  2020. include/linux/misc_cgroup.h file and the corresponding name via misc_res_name[]
  2021. in the kernel/cgroup/misc.c file. Provider of the resource must set its
  2022. capacity prior to using the resource by calling misc_cg_set_capacity().
  2023. Once a capacity is set then the resource usage can be updated using charge and
  2024. uncharge APIs. All of the APIs to interact with misc controller are in
  2025. include/linux/misc_cgroup.h.
  2026. Misc Interface Files
  2027. ~~~~~~~~~~~~~~~~~~~~
  2028. Miscellaneous controller provides 3 interface files. If two misc resources (res_a and res_b) are registered then:
  2029. misc.capacity
  2030. A read-only flat-keyed file shown only in the root cgroup. It shows
  2031. miscellaneous scalar resources available on the platform along with
  2032. their quantities::
  2033. $ cat misc.capacity
  2034. res_a 50
  2035. res_b 10
  2036. misc.current
  2037. A read-only flat-keyed file shown in the all cgroups. It shows
  2038. the current usage of the resources in the cgroup and its children.::
  2039. $ cat misc.current
  2040. res_a 3
  2041. res_b 0
  2042. misc.peak
  2043. A read-only flat-keyed file shown in all cgroups. It shows the
  2044. historical maximum usage of the resources in the cgroup and its
  2045. children.::
  2046. $ cat misc.peak
  2047. res_a 10
  2048. res_b 8
  2049. misc.max
  2050. A read-write flat-keyed file shown in the non root cgroups. Allowed
  2051. maximum usage of the resources in the cgroup and its children.::
  2052. $ cat misc.max
  2053. res_a max
  2054. res_b 4
  2055. Limit can be set by::
  2056. # echo res_a 1 > misc.max
  2057. Limit can be set to max by::
  2058. # echo res_a max > misc.max
  2059. Limits can be set higher than the capacity value in the misc.capacity
  2060. file.
  2061. misc.events
  2062. A read-only flat-keyed file which exists on non-root cgroups. The
  2063. following entries are defined. Unless specified otherwise, a value
  2064. change in this file generates a file modified event. All fields in
  2065. this file are hierarchical.
  2066. max
  2067. The number of times the cgroup's resource usage was
  2068. about to go over the max boundary.
  2069. misc.events.local
  2070. Similar to misc.events but the fields in the file are local to the
  2071. cgroup i.e. not hierarchical. The file modified event generated on
  2072. this file reflects only the local events.
  2073. Migration and Ownership
  2074. ~~~~~~~~~~~~~~~~~~~~~~~
  2075. A miscellaneous scalar resource is charged to the cgroup in which it is used
  2076. first, and stays charged to that cgroup until that resource is freed. Migrating
  2077. a process to a different cgroup does not move the charge to the destination
  2078. cgroup where the process has moved.
  2079. Others
  2080. ------
  2081. perf_event
  2082. ~~~~~~~~~~
  2083. perf_event controller, if not mounted on a legacy hierarchy, is
  2084. automatically enabled on the v2 hierarchy so that perf events can
  2085. always be filtered by cgroup v2 path. The controller can still be
  2086. moved to a legacy hierarchy after v2 hierarchy is populated.
  2087. Non-normative information
  2088. -------------------------
  2089. This section contains information that isn't considered to be a part of
  2090. the stable kernel API and so is subject to change.
  2091. CPU controller root cgroup process behaviour
  2092. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2093. When distributing CPU cycles in the root cgroup each thread in this
  2094. cgroup is treated as if it was hosted in a separate child cgroup of the
  2095. root cgroup. This child cgroup weight is dependent on its thread nice
  2096. level.
  2097. For details of this mapping see sched_prio_to_weight array in
  2098. kernel/sched/core.c file (values from this array should be scaled
  2099. appropriately so the neutral - nice 0 - value is 100 instead of 1024).
  2100. IO controller root cgroup process behaviour
  2101. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2102. Root cgroup processes are hosted in an implicit leaf child node.
  2103. When distributing IO resources this implicit child node is taken into
  2104. account as if it was a normal child cgroup of the root cgroup with a
  2105. weight value of 200.
  2106. Namespace
  2107. =========
  2108. Basics
  2109. ------
  2110. cgroup namespace provides a mechanism to virtualize the view of the
  2111. "/proc/$PID/cgroup" file and cgroup mounts. The CLONE_NEWCGROUP clone
  2112. flag can be used with clone(2) and unshare(2) to create a new cgroup
  2113. namespace. The process running inside the cgroup namespace will have
  2114. its "/proc/$PID/cgroup" output restricted to cgroupns root. The
  2115. cgroupns root is the cgroup of the process at the time of creation of
  2116. the cgroup namespace.
  2117. Without cgroup namespace, the "/proc/$PID/cgroup" file shows the
  2118. complete path of the cgroup of a process. In a container setup where
  2119. a set of cgroups and namespaces are intended to isolate processes the
  2120. "/proc/$PID/cgroup" file may leak potential system level information
  2121. to the isolated processes. For example::
  2122. # cat /proc/self/cgroup
  2123. 0::/batchjobs/container_id1
  2124. The path '/batchjobs/container_id1' can be considered as system-data
  2125. and undesirable to expose to the isolated processes. cgroup namespace
  2126. can be used to restrict visibility of this path. For example, before
  2127. creating a cgroup namespace, one would see::
  2128. # ls -l /proc/self/ns/cgroup
  2129. lrwxrwxrwx 1 root root 0 2014-07-15 10:37 /proc/self/ns/cgroup -> cgroup:[4026531835]
  2130. # cat /proc/self/cgroup
  2131. 0::/batchjobs/container_id1
  2132. After unsharing a new namespace, the view changes::
  2133. # ls -l /proc/self/ns/cgroup
  2134. lrwxrwxrwx 1 root root 0 2014-07-15 10:35 /proc/self/ns/cgroup -> cgroup:[4026532183]
  2135. # cat /proc/self/cgroup
  2136. 0::/
  2137. When some thread from a multi-threaded process unshares its cgroup
  2138. namespace, the new cgroupns gets applied to the entire process (all
  2139. the threads). This is natural for the v2 hierarchy; however, for the
  2140. legacy hierarchies, this may be unexpected.
  2141. A cgroup namespace is alive as long as there are processes inside or
  2142. mounts pinning it. When the last usage goes away, the cgroup
  2143. namespace is destroyed. The cgroupns root and the actual cgroups
  2144. remain.
  2145. The Root and Views
  2146. ------------------
  2147. The 'cgroupns root' for a cgroup namespace is the cgroup in which the
  2148. process calling unshare(2) is running. For example, if a process in
  2149. /batchjobs/container_id1 cgroup calls unshare, cgroup
  2150. /batchjobs/container_id1 becomes the cgroupns root. For the
  2151. init_cgroup_ns, this is the real root ('/') cgroup.
  2152. The cgroupns root cgroup does not change even if the namespace creator
  2153. process later moves to a different cgroup::
  2154. # ~/unshare -c # unshare cgroupns in some cgroup
  2155. # cat /proc/self/cgroup
  2156. 0::/
  2157. # mkdir sub_cgrp_1
  2158. # echo 0 > sub_cgrp_1/cgroup.procs
  2159. # cat /proc/self/cgroup
  2160. 0::/sub_cgrp_1
  2161. Each process gets its namespace-specific view of "/proc/$PID/cgroup"
  2162. Processes running inside the cgroup namespace will be able to see
  2163. cgroup paths (in /proc/self/cgroup) only inside their root cgroup.
  2164. From within an unshared cgroupns::
  2165. # sleep 100000 &
  2166. [1] 7353
  2167. # echo 7353 > sub_cgrp_1/cgroup.procs
  2168. # cat /proc/7353/cgroup
  2169. 0::/sub_cgrp_1
  2170. From the initial cgroup namespace, the real cgroup path will be
  2171. visible::
  2172. $ cat /proc/7353/cgroup
  2173. 0::/batchjobs/container_id1/sub_cgrp_1
  2174. From a sibling cgroup namespace (that is, a namespace rooted at a
  2175. different cgroup), the cgroup path relative to its own cgroup
  2176. namespace root will be shown. For instance, if PID 7353's cgroup
  2177. namespace root is at '/batchjobs/container_id2', then it will see::
  2178. # cat /proc/7353/cgroup
  2179. 0::/../container_id2/sub_cgrp_1
  2180. Note that the relative path always starts with '/' to indicate that
  2181. its relative to the cgroup namespace root of the caller.
  2182. Migration and setns(2)
  2183. ----------------------
  2184. Processes inside a cgroup namespace can move into and out of the
  2185. namespace root if they have proper access to external cgroups. For
  2186. example, from inside a namespace with cgroupns root at
  2187. /batchjobs/container_id1, and assuming that the global hierarchy is
  2188. still accessible inside cgroupns::
  2189. # cat /proc/7353/cgroup
  2190. 0::/sub_cgrp_1
  2191. # echo 7353 > batchjobs/container_id2/cgroup.procs
  2192. # cat /proc/7353/cgroup
  2193. 0::/../container_id2
  2194. Note that this kind of setup is not encouraged. A task inside cgroup
  2195. namespace should only be exposed to its own cgroupns hierarchy.
  2196. setns(2) to another cgroup namespace is allowed when:
  2197. (a) the process has CAP_SYS_ADMIN against its current user namespace
  2198. (b) the process has CAP_SYS_ADMIN against the target cgroup
  2199. namespace's userns
  2200. No implicit cgroup changes happen with attaching to another cgroup
  2201. namespace. It is expected that the someone moves the attaching
  2202. process under the target cgroup namespace root.
  2203. Interaction with Other Namespaces
  2204. ---------------------------------
  2205. Namespace specific cgroup hierarchy can be mounted by a process
  2206. running inside a non-init cgroup namespace::
  2207. # mount -t cgroup2 none $MOUNT_POINT
  2208. This will mount the unified cgroup hierarchy with cgroupns root as the
  2209. filesystem root. The process needs CAP_SYS_ADMIN against its user and
  2210. mount namespaces.
  2211. The virtualization of /proc/self/cgroup file combined with restricting
  2212. the view of cgroup hierarchy by namespace-private cgroupfs mount
  2213. provides a properly isolated cgroup view inside the container.
  2214. Information on Kernel Programming
  2215. =================================
  2216. This section contains kernel programming information in the areas
  2217. where interacting with cgroup is necessary. cgroup core and
  2218. controllers are not covered.
  2219. Filesystem Support for Writeback
  2220. --------------------------------
  2221. A filesystem can support cgroup writeback by updating
  2222. address_space_operations->writepage[s]() to annotate bio's using the
  2223. following two functions.
  2224. wbc_init_bio(@wbc, @bio)
  2225. Should be called for each bio carrying writeback data and
  2226. associates the bio with the inode's owner cgroup and the
  2227. corresponding request queue. This must be called after
  2228. a queue (device) has been associated with the bio and
  2229. before submission.
  2230. wbc_account_cgroup_owner(@wbc, @folio, @bytes)
  2231. Should be called for each data segment being written out.
  2232. While this function doesn't care exactly when it's called
  2233. during the writeback session, it's the easiest and most
  2234. natural to call it as data segments are added to a bio.
  2235. With writeback bio's annotated, cgroup support can be enabled per
  2236. super_block by setting SB_I_CGROUPWB in ->s_iflags. This allows for
  2237. selective disabling of cgroup writeback support which is helpful when
  2238. certain filesystem features, e.g. journaled data mode, are
  2239. incompatible.
  2240. wbc_init_bio() binds the specified bio to its cgroup. Depending on
  2241. the configuration, the bio may be executed at a lower priority and if
  2242. the writeback session is holding shared resources, e.g. a journal
  2243. entry, may lead to priority inversion. There is no one easy solution
  2244. for the problem. Filesystems can try to work around specific problem
  2245. cases by skipping wbc_init_bio() and using bio_associate_blkg()
  2246. directly.
  2247. Deprecated v1 Core Features
  2248. ===========================
  2249. - Multiple hierarchies including named ones are not supported.
  2250. - All v1 mount options are not supported.
  2251. - The "tasks" file is removed and "cgroup.procs" is not sorted.
  2252. - "cgroup.clone_children" is removed.
  2253. - /proc/cgroups is meaningless for v2. Use "cgroup.controllers" or
  2254. "cgroup.stat" files at the root instead.
  2255. Issues with v1 and Rationales for v2
  2256. ====================================
  2257. Multiple Hierarchies
  2258. --------------------
  2259. cgroup v1 allowed an arbitrary number of hierarchies and each
  2260. hierarchy could host any number of controllers. While this seemed to
  2261. provide a high level of flexibility, it wasn't useful in practice.
  2262. For example, as there is only one instance of each controller, utility
  2263. type controllers such as freezer which can be useful in all
  2264. hierarchies could only be used in one. The issue is exacerbated by
  2265. the fact that controllers couldn't be moved to another hierarchy once
  2266. hierarchies were populated. Another issue was that all controllers
  2267. bound to a hierarchy were forced to have exactly the same view of the
  2268. hierarchy. It wasn't possible to vary the granularity depending on
  2269. the specific controller.
  2270. In practice, these issues heavily limited which controllers could be
  2271. put on the same hierarchy and most configurations resorted to putting
  2272. each controller on its own hierarchy. Only closely related ones, such
  2273. as the cpu and cpuacct controllers, made sense to be put on the same
  2274. hierarchy. This often meant that userland ended up managing multiple
  2275. similar hierarchies repeating the same steps on each hierarchy
  2276. whenever a hierarchy management operation was necessary.
  2277. Furthermore, support for multiple hierarchies came at a steep cost.
  2278. It greatly complicated cgroup core implementation but more importantly
  2279. the support for multiple hierarchies restricted how cgroup could be
  2280. used in general and what controllers was able to do.
  2281. There was no limit on how many hierarchies there might be, which meant
  2282. that a thread's cgroup membership couldn't be described in finite
  2283. length. The key might contain any number of entries and was unlimited
  2284. in length, which made it highly awkward to manipulate and led to
  2285. addition of controllers which existed only to identify membership,
  2286. which in turn exacerbated the original problem of proliferating number
  2287. of hierarchies.
  2288. Also, as a controller couldn't have any expectation regarding the
  2289. topologies of hierarchies other controllers might be on, each
  2290. controller had to assume that all other controllers were attached to
  2291. completely orthogonal hierarchies. This made it impossible, or at
  2292. least very cumbersome, for controllers to cooperate with each other.
  2293. In most use cases, putting controllers on hierarchies which are
  2294. completely orthogonal to each other isn't necessary. What usually is
  2295. called for is the ability to have differing levels of granularity
  2296. depending on the specific controller. In other words, hierarchy may
  2297. be collapsed from leaf towards root when viewed from specific
  2298. controllers. For example, a given configuration might not care about
  2299. how memory is distributed beyond a certain level while still wanting
  2300. to control how CPU cycles are distributed.
  2301. Thread Granularity
  2302. ------------------
  2303. cgroup v1 allowed threads of a process to belong to different cgroups.
  2304. This didn't make sense for some controllers and those controllers
  2305. ended up implementing different ways to ignore such situations but
  2306. much more importantly it blurred the line between API exposed to
  2307. individual applications and system management interface.
  2308. Generally, in-process knowledge is available only to the process
  2309. itself; thus, unlike service-level organization of processes,
  2310. categorizing threads of a process requires active participation from
  2311. the application which owns the target process.
  2312. cgroup v1 had an ambiguously defined delegation model which got abused
  2313. in combination with thread granularity. cgroups were delegated to
  2314. individual applications so that they can create and manage their own
  2315. sub-hierarchies and control resource distributions along them. This
  2316. effectively raised cgroup to the status of a syscall-like API exposed
  2317. to lay programs.
  2318. First of all, cgroup has a fundamentally inadequate interface to be
  2319. exposed this way. For a process to access its own knobs, it has to
  2320. extract the path on the target hierarchy from /proc/self/cgroup,
  2321. construct the path by appending the name of the knob to the path, open
  2322. and then read and/or write to it. This is not only extremely clunky
  2323. and unusual but also inherently racy. There is no conventional way to
  2324. define transaction across the required steps and nothing can guarantee
  2325. that the process would actually be operating on its own sub-hierarchy.
  2326. cgroup controllers implemented a number of knobs which would never be
  2327. accepted as public APIs because they were just adding control knobs to
  2328. system-management pseudo filesystem. cgroup ended up with interface
  2329. knobs which were not properly abstracted or refined and directly
  2330. revealed kernel internal details. These knobs got exposed to
  2331. individual applications through the ill-defined delegation mechanism
  2332. effectively abusing cgroup as a shortcut to implementing public APIs
  2333. without going through the required scrutiny.
  2334. This was painful for both userland and kernel. Userland ended up with
  2335. misbehaving and poorly abstracted interfaces and kernel exposing and
  2336. locked into constructs inadvertently.
  2337. Competition Between Inner Nodes and Threads
  2338. -------------------------------------------
  2339. cgroup v1 allowed threads to be in any cgroups which created an
  2340. interesting problem where threads belonging to a parent cgroup and its
  2341. children cgroups competed for resources. This was nasty as two
  2342. different types of entities competed and there was no obvious way to
  2343. settle it. Different controllers did different things.
  2344. The cpu controller considered threads and cgroups as equivalents and
  2345. mapped nice levels to cgroup weights. This worked for some cases but
  2346. fell flat when children wanted to be allocated specific ratios of CPU
  2347. cycles and the number of internal threads fluctuated - the ratios
  2348. constantly changed as the number of competing entities fluctuated.
  2349. There also were other issues. The mapping from nice level to weight
  2350. wasn't obvious or universal, and there were various other knobs which
  2351. simply weren't available for threads.
  2352. The io controller implicitly created a hidden leaf node for each
  2353. cgroup to host the threads. The hidden leaf had its own copies of all
  2354. the knobs with ``leaf_`` prefixed. While this allowed equivalent
  2355. control over internal threads, it was with serious drawbacks. It
  2356. always added an extra layer of nesting which wouldn't be necessary
  2357. otherwise, made the interface messy and significantly complicated the
  2358. implementation.
  2359. The memory controller didn't have a way to control what happened
  2360. between internal tasks and child cgroups and the behavior was not
  2361. clearly defined. There were attempts to add ad-hoc behaviors and
  2362. knobs to tailor the behavior to specific workloads which would have
  2363. led to problems extremely difficult to resolve in the long term.
  2364. Multiple controllers struggled with internal tasks and came up with
  2365. different ways to deal with it; unfortunately, all the approaches were
  2366. severely flawed and, furthermore, the widely different behaviors
  2367. made cgroup as a whole highly inconsistent.
  2368. This clearly is a problem which needs to be addressed from cgroup core
  2369. in a uniform way.
  2370. Other Interface Issues
  2371. ----------------------
  2372. cgroup v1 grew without oversight and developed a large number of
  2373. idiosyncrasies and inconsistencies. One issue on the cgroup core side
  2374. was how an empty cgroup was notified - a userland helper binary was
  2375. forked and executed for each event. The event delivery wasn't
  2376. recursive or delegatable. The limitations of the mechanism also led
  2377. to in-kernel event delivery filtering mechanism further complicating
  2378. the interface.
  2379. Controller interfaces were problematic too. An extreme example is
  2380. controllers completely ignoring hierarchical organization and treating
  2381. all cgroups as if they were all located directly under the root
  2382. cgroup. Some controllers exposed a large amount of inconsistent
  2383. implementation details to userland.
  2384. There also was no consistency across controllers. When a new cgroup
  2385. was created, some controllers defaulted to not imposing extra
  2386. restrictions while others disallowed any resource usage until
  2387. explicitly configured. Configuration knobs for the same type of
  2388. control used widely differing naming schemes and formats. Statistics
  2389. and information knobs were named arbitrarily and used different
  2390. formats and units even in the same controller.
  2391. cgroup v2 establishes common conventions where appropriate and updates
  2392. controllers so that they expose minimal and consistent interfaces.
  2393. Controller Issues and Remedies
  2394. ------------------------------
  2395. Memory
  2396. ~~~~~~
  2397. The original lower boundary, the soft limit, is defined as a limit
  2398. that is per default unset. As a result, the set of cgroups that
  2399. global reclaim prefers is opt-in, rather than opt-out. The costs for
  2400. optimizing these mostly negative lookups are so high that the
  2401. implementation, despite its enormous size, does not even provide the
  2402. basic desirable behavior. First off, the soft limit has no
  2403. hierarchical meaning. All configured groups are organized in a global
  2404. rbtree and treated like equal peers, regardless where they are located
  2405. in the hierarchy. This makes subtree delegation impossible. Second,
  2406. the soft limit reclaim pass is so aggressive that it not just
  2407. introduces high allocation latencies into the system, but also impacts
  2408. system performance due to overreclaim, to the point where the feature
  2409. becomes self-defeating.
  2410. The memory.low boundary on the other hand is a top-down allocated
  2411. reserve. A cgroup enjoys reclaim protection when it's within its
  2412. effective low, which makes delegation of subtrees possible. It also
  2413. enjoys having reclaim pressure proportional to its overage when
  2414. above its effective low.
  2415. The original high boundary, the hard limit, is defined as a strict
  2416. limit that can not budge, even if the OOM killer has to be called.
  2417. But this generally goes against the goal of making the most out of the
  2418. available memory. The memory consumption of workloads varies during
  2419. runtime, and that requires users to overcommit. But doing that with a
  2420. strict upper limit requires either a fairly accurate prediction of the
  2421. working set size or adding slack to the limit. Since working set size
  2422. estimation is hard and error prone, and getting it wrong results in
  2423. OOM kills, most users tend to err on the side of a looser limit and
  2424. end up wasting precious resources.
  2425. The memory.high boundary on the other hand can be set much more
  2426. conservatively. When hit, it throttles allocations by forcing them
  2427. into direct reclaim to work off the excess, but it never invokes the
  2428. OOM killer. As a result, a high boundary that is chosen too
  2429. aggressively will not terminate the processes, but instead it will
  2430. lead to gradual performance degradation. The user can monitor this
  2431. and make corrections until the minimal memory footprint that still
  2432. gives acceptable performance is found.
  2433. In extreme cases, with many concurrent allocations and a complete
  2434. breakdown of reclaim progress within the group, the high boundary can
  2435. be exceeded. But even then it's mostly better to satisfy the
  2436. allocation from the slack available in other groups or the rest of the
  2437. system than killing the group. Otherwise, memory.max is there to
  2438. limit this type of spillover and ultimately contain buggy or even
  2439. malicious applications.
  2440. Setting the original memory.limit_in_bytes below the current usage was
  2441. subject to a race condition, where concurrent charges could cause the
  2442. limit setting to fail. memory.max on the other hand will first set the
  2443. limit to prevent new charges, and then reclaim and OOM kill until the
  2444. new limit is met - or the task writing to memory.max is killed.
  2445. The combined memory+swap accounting and limiting is replaced by real
  2446. control over swap space.
  2447. The main argument for a combined memory+swap facility in the original
  2448. cgroup design was that global or parental pressure would always be
  2449. able to swap all anonymous memory of a child group, regardless of the
  2450. child's own (possibly untrusted) configuration. However, untrusted
  2451. groups can sabotage swapping by other means - such as referencing its
  2452. anonymous memory in a tight loop - and an admin can not assume full
  2453. swappability when overcommitting untrusted jobs.
  2454. For trusted jobs, on the other hand, a combined counter is not an
  2455. intuitive userspace interface, and it flies in the face of the idea
  2456. that cgroup controllers should account and limit specific physical
  2457. resources. Swap space is a resource like all others in the system,
  2458. and that's why unified hierarchy allows distributing it separately.