162306a36Sopenharmony_ci==========================
262306a36Sopenharmony_ciMemory Resource Controller
362306a36Sopenharmony_ci==========================
462306a36Sopenharmony_ci
562306a36Sopenharmony_ci.. caution::
662306a36Sopenharmony_ci      This document is hopelessly outdated and it asks for a complete
762306a36Sopenharmony_ci      rewrite. It still contains a useful information so we are keeping it
862306a36Sopenharmony_ci      here but make sure to check the current code if you need a deeper
962306a36Sopenharmony_ci      understanding.
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci.. note::
1262306a36Sopenharmony_ci      The Memory Resource Controller has generically been referred to as the
1362306a36Sopenharmony_ci      memory controller in this document. Do not confuse memory controller
1462306a36Sopenharmony_ci      used here with the memory controller that is used in hardware.
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ci.. hint::
1762306a36Sopenharmony_ci      When we mention a cgroup (cgroupfs's directory) with memory controller,
1862306a36Sopenharmony_ci      we call it "memory cgroup". When you see git-log and source code, you'll
1962306a36Sopenharmony_ci      see patch's title and function names tend to use "memcg".
2062306a36Sopenharmony_ci      In this document, we avoid using it.
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_ciBenefits and Purpose of the memory controller
2362306a36Sopenharmony_ci=============================================
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ciThe memory controller isolates the memory behaviour of a group of tasks
2662306a36Sopenharmony_cifrom the rest of the system. The article on LWN [12]_ mentions some probable
2762306a36Sopenharmony_ciuses of the memory controller. The memory controller can be used to
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_cia. Isolate an application or a group of applications
3062306a36Sopenharmony_ci   Memory-hungry applications can be isolated and limited to a smaller
3162306a36Sopenharmony_ci   amount of memory.
3262306a36Sopenharmony_cib. Create a cgroup with a limited amount of memory; this can be used
3362306a36Sopenharmony_ci   as a good alternative to booting with mem=XXXX.
3462306a36Sopenharmony_cic. Virtualization solutions can control the amount of memory they want
3562306a36Sopenharmony_ci   to assign to a virtual machine instance.
3662306a36Sopenharmony_cid. A CD/DVD burner could control the amount of memory used by the
3762306a36Sopenharmony_ci   rest of the system to ensure that burning does not fail due to lack
3862306a36Sopenharmony_ci   of available memory.
3962306a36Sopenharmony_cie. There are several other use cases; find one or use the controller just
4062306a36Sopenharmony_ci   for fun (to learn and hack on the VM subsystem).
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_ciCurrent Status: linux-2.6.34-mmotm(development version of 2010/April)
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ciFeatures:
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ci - accounting anonymous pages, file caches, swap caches usage and limiting them.
4762306a36Sopenharmony_ci - pages are linked to per-memcg LRU exclusively, and there is no global LRU.
4862306a36Sopenharmony_ci - optionally, memory+swap usage can be accounted and limited.
4962306a36Sopenharmony_ci - hierarchical accounting
5062306a36Sopenharmony_ci - soft limit
5162306a36Sopenharmony_ci - moving (recharging) account at moving a task is selectable.
5262306a36Sopenharmony_ci - usage threshold notifier
5362306a36Sopenharmony_ci - memory pressure notifier
5462306a36Sopenharmony_ci - oom-killer disable knob and oom-notifier
5562306a36Sopenharmony_ci - Root cgroup has no limit controls.
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_ci Kernel memory support is a work in progress, and the current version provides
5862306a36Sopenharmony_ci basically functionality. (See :ref:`section 2.7
5962306a36Sopenharmony_ci <cgroup-v1-memory-kernel-extension>`)
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_ciBrief summary of control files.
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ci==================================== ==========================================
6462306a36Sopenharmony_ci tasks				     attach a task(thread) and show list of
6562306a36Sopenharmony_ci				     threads
6662306a36Sopenharmony_ci cgroup.procs			     show list of processes
6762306a36Sopenharmony_ci cgroup.event_control		     an interface for event_fd()
6862306a36Sopenharmony_ci				     This knob is not available on CONFIG_PREEMPT_RT systems.
6962306a36Sopenharmony_ci memory.usage_in_bytes		     show current usage for memory
7062306a36Sopenharmony_ci				     (See 5.5 for details)
7162306a36Sopenharmony_ci memory.memsw.usage_in_bytes	     show current usage for memory+Swap
7262306a36Sopenharmony_ci				     (See 5.5 for details)
7362306a36Sopenharmony_ci memory.limit_in_bytes		     set/show limit of memory usage
7462306a36Sopenharmony_ci memory.memsw.limit_in_bytes	     set/show limit of memory+Swap usage
7562306a36Sopenharmony_ci memory.failcnt			     show the number of memory usage hits limits
7662306a36Sopenharmony_ci memory.memsw.failcnt		     show the number of memory+Swap hits limits
7762306a36Sopenharmony_ci memory.max_usage_in_bytes	     show max memory usage recorded
7862306a36Sopenharmony_ci memory.memsw.max_usage_in_bytes     show max memory+Swap usage recorded
7962306a36Sopenharmony_ci memory.soft_limit_in_bytes	     set/show soft limit of memory usage
8062306a36Sopenharmony_ci				     This knob is not available on CONFIG_PREEMPT_RT systems.
8162306a36Sopenharmony_ci memory.stat			     show various statistics
8262306a36Sopenharmony_ci memory.use_hierarchy		     set/show hierarchical account enabled
8362306a36Sopenharmony_ci                                     This knob is deprecated and shouldn't be
8462306a36Sopenharmony_ci                                     used.
8562306a36Sopenharmony_ci memory.force_empty		     trigger forced page reclaim
8662306a36Sopenharmony_ci memory.pressure_level		     set memory pressure notifications
8762306a36Sopenharmony_ci memory.swappiness		     set/show swappiness parameter of vmscan
8862306a36Sopenharmony_ci				     (See sysctl's vm.swappiness)
8962306a36Sopenharmony_ci memory.move_charge_at_immigrate     set/show controls of moving charges
9062306a36Sopenharmony_ci                                     This knob is deprecated and shouldn't be
9162306a36Sopenharmony_ci                                     used.
9262306a36Sopenharmony_ci memory.oom_control		     set/show oom controls.
9362306a36Sopenharmony_ci memory.numa_stat		     show the number of memory usage per numa
9462306a36Sopenharmony_ci				     node
9562306a36Sopenharmony_ci memory.kmem.limit_in_bytes          Deprecated knob to set and read the kernel
9662306a36Sopenharmony_ci                                     memory hard limit. Kernel hard limit is not
9762306a36Sopenharmony_ci                                     supported since 5.16. Writing any value to
9862306a36Sopenharmony_ci                                     do file will not have any effect same as if
9962306a36Sopenharmony_ci                                     nokmem kernel parameter was specified.
10062306a36Sopenharmony_ci                                     Kernel memory is still charged and reported
10162306a36Sopenharmony_ci                                     by memory.kmem.usage_in_bytes.
10262306a36Sopenharmony_ci memory.kmem.usage_in_bytes          show current kernel memory allocation
10362306a36Sopenharmony_ci memory.kmem.failcnt                 show the number of kernel memory usage
10462306a36Sopenharmony_ci				     hits limits
10562306a36Sopenharmony_ci memory.kmem.max_usage_in_bytes      show max kernel memory usage recorded
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_ci memory.kmem.tcp.limit_in_bytes      set/show hard limit for tcp buf memory
10862306a36Sopenharmony_ci memory.kmem.tcp.usage_in_bytes      show current tcp buf memory allocation
10962306a36Sopenharmony_ci memory.kmem.tcp.failcnt             show the number of tcp buf memory usage
11062306a36Sopenharmony_ci				     hits limits
11162306a36Sopenharmony_ci memory.kmem.tcp.max_usage_in_bytes  show max tcp buf memory usage recorded
11262306a36Sopenharmony_ci==================================== ==========================================
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_ci1. History
11562306a36Sopenharmony_ci==========
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_ciThe memory controller has a long history. A request for comments for the memory
11862306a36Sopenharmony_cicontroller was posted by Balbir Singh [1]_. At the time the RFC was posted
11962306a36Sopenharmony_cithere were several implementations for memory control. The goal of the
12062306a36Sopenharmony_ciRFC was to build consensus and agreement for the minimal features required
12162306a36Sopenharmony_cifor memory control. The first RSS controller was posted by Balbir Singh [2]_
12262306a36Sopenharmony_ciin Feb 2007. Pavel Emelianov [3]_ [4]_ [5]_ has since posted three versions
12362306a36Sopenharmony_ciof the RSS controller. At OLS, at the resource management BoF, everyone
12462306a36Sopenharmony_cisuggested that we handle both page cache and RSS together. Another request was
12562306a36Sopenharmony_ciraised to allow user space handling of OOM. The current memory controller is
12662306a36Sopenharmony_ciat version 6; it combines both mapped (RSS) and unmapped Page
12762306a36Sopenharmony_ciCache Control [11]_.
12862306a36Sopenharmony_ci
12962306a36Sopenharmony_ci2. Memory Control
13062306a36Sopenharmony_ci=================
13162306a36Sopenharmony_ci
13262306a36Sopenharmony_ciMemory is a unique resource in the sense that it is present in a limited
13362306a36Sopenharmony_ciamount. If a task requires a lot of CPU processing, the task can spread
13462306a36Sopenharmony_ciits processing over a period of hours, days, months or years, but with
13562306a36Sopenharmony_cimemory, the same physical memory needs to be reused to accomplish the task.
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_ciThe memory controller implementation has been divided into phases. These
13862306a36Sopenharmony_ciare:
13962306a36Sopenharmony_ci
14062306a36Sopenharmony_ci1. Memory controller
14162306a36Sopenharmony_ci2. mlock(2) controller
14262306a36Sopenharmony_ci3. Kernel user memory accounting and slab control
14362306a36Sopenharmony_ci4. user mappings length controller
14462306a36Sopenharmony_ci
14562306a36Sopenharmony_ciThe memory controller is the first controller developed.
14662306a36Sopenharmony_ci
14762306a36Sopenharmony_ci2.1. Design
14862306a36Sopenharmony_ci-----------
14962306a36Sopenharmony_ci
15062306a36Sopenharmony_ciThe core of the design is a counter called the page_counter. The
15162306a36Sopenharmony_cipage_counter tracks the current memory usage and limit of the group of
15262306a36Sopenharmony_ciprocesses associated with the controller. Each cgroup has a memory controller
15362306a36Sopenharmony_cispecific data structure (mem_cgroup) associated with it.
15462306a36Sopenharmony_ci
15562306a36Sopenharmony_ci2.2. Accounting
15662306a36Sopenharmony_ci---------------
15762306a36Sopenharmony_ci
15862306a36Sopenharmony_ci.. code-block::
15962306a36Sopenharmony_ci   :caption: Figure 1: Hierarchy of Accounting
16062306a36Sopenharmony_ci
16162306a36Sopenharmony_ci		+--------------------+
16262306a36Sopenharmony_ci		|  mem_cgroup        |
16362306a36Sopenharmony_ci		|  (page_counter)    |
16462306a36Sopenharmony_ci		+--------------------+
16562306a36Sopenharmony_ci		 /            ^      \
16662306a36Sopenharmony_ci		/             |       \
16762306a36Sopenharmony_ci           +---------------+  |        +---------------+
16862306a36Sopenharmony_ci           | mm_struct     |  |....    | mm_struct     |
16962306a36Sopenharmony_ci           |               |  |        |               |
17062306a36Sopenharmony_ci           +---------------+  |        +---------------+
17162306a36Sopenharmony_ci                              |
17262306a36Sopenharmony_ci                              + --------------+
17362306a36Sopenharmony_ci                                              |
17462306a36Sopenharmony_ci           +---------------+           +------+--------+
17562306a36Sopenharmony_ci           | page          +---------->  page_cgroup|
17662306a36Sopenharmony_ci           |               |           |               |
17762306a36Sopenharmony_ci           +---------------+           +---------------+
17862306a36Sopenharmony_ci
17962306a36Sopenharmony_ci
18062306a36Sopenharmony_ci
18162306a36Sopenharmony_ciFigure 1 shows the important aspects of the controller
18262306a36Sopenharmony_ci
18362306a36Sopenharmony_ci1. Accounting happens per cgroup
18462306a36Sopenharmony_ci2. Each mm_struct knows about which cgroup it belongs to
18562306a36Sopenharmony_ci3. Each page has a pointer to the page_cgroup, which in turn knows the
18662306a36Sopenharmony_ci   cgroup it belongs to
18762306a36Sopenharmony_ci
18862306a36Sopenharmony_ciThe accounting is done as follows: mem_cgroup_charge_common() is invoked to
18962306a36Sopenharmony_ciset up the necessary data structures and check if the cgroup that is being
19062306a36Sopenharmony_cicharged is over its limit. If it is, then reclaim is invoked on the cgroup.
19162306a36Sopenharmony_ciMore details can be found in the reclaim section of this document.
19262306a36Sopenharmony_ciIf everything goes well, a page meta-data-structure called page_cgroup is
19362306a36Sopenharmony_ciupdated. page_cgroup has its own LRU on cgroup.
19462306a36Sopenharmony_ci(*) page_cgroup structure is allocated at boot/memory-hotplug time.
19562306a36Sopenharmony_ci
19662306a36Sopenharmony_ci2.2.1 Accounting details
19762306a36Sopenharmony_ci------------------------
19862306a36Sopenharmony_ci
19962306a36Sopenharmony_ciAll mapped anon pages (RSS) and cache pages (Page Cache) are accounted.
20062306a36Sopenharmony_ciSome pages which are never reclaimable and will not be on the LRU
20162306a36Sopenharmony_ciare not accounted. We just account pages under usual VM management.
20262306a36Sopenharmony_ci
20362306a36Sopenharmony_ciRSS pages are accounted at page_fault unless they've already been accounted
20462306a36Sopenharmony_cifor earlier. A file page will be accounted for as Page Cache when it's
20562306a36Sopenharmony_ciinserted into inode (xarray). While it's mapped into the page tables of
20662306a36Sopenharmony_ciprocesses, duplicate accounting is carefully avoided.
20762306a36Sopenharmony_ci
20862306a36Sopenharmony_ciAn RSS page is unaccounted when it's fully unmapped. A PageCache page is
20962306a36Sopenharmony_ciunaccounted when it's removed from xarray. Even if RSS pages are fully
21062306a36Sopenharmony_ciunmapped (by kswapd), they may exist as SwapCache in the system until they
21162306a36Sopenharmony_ciare really freed. Such SwapCaches are also accounted.
21262306a36Sopenharmony_ciA swapped-in page is accounted after adding into swapcache.
21362306a36Sopenharmony_ci
21462306a36Sopenharmony_ciNote: The kernel does swapin-readahead and reads multiple swaps at once.
21562306a36Sopenharmony_ciSince page's memcg recorded into swap whatever memsw enabled, the page will
21662306a36Sopenharmony_cibe accounted after swapin.
21762306a36Sopenharmony_ci
21862306a36Sopenharmony_ciAt page migration, accounting information is kept.
21962306a36Sopenharmony_ci
22062306a36Sopenharmony_ciNote: we just account pages-on-LRU because our purpose is to control amount
22162306a36Sopenharmony_ciof used pages; not-on-LRU pages tend to be out-of-control from VM view.
22262306a36Sopenharmony_ci
22362306a36Sopenharmony_ci2.3 Shared Page Accounting
22462306a36Sopenharmony_ci--------------------------
22562306a36Sopenharmony_ci
22662306a36Sopenharmony_ciShared pages are accounted on the basis of the first touch approach. The
22762306a36Sopenharmony_cicgroup that first touches a page is accounted for the page. The principle
22862306a36Sopenharmony_cibehind this approach is that a cgroup that aggressively uses a shared
22962306a36Sopenharmony_cipage will eventually get charged for it (once it is uncharged from
23062306a36Sopenharmony_cithe cgroup that brought it in -- this will happen on memory pressure).
23162306a36Sopenharmony_ci
23262306a36Sopenharmony_ciBut see :ref:`section 8.2 <cgroup-v1-memory-movable-charges>` when moving a
23362306a36Sopenharmony_citask to another cgroup, its pages may be recharged to the new cgroup, if
23462306a36Sopenharmony_cimove_charge_at_immigrate has been chosen.
23562306a36Sopenharmony_ci
23662306a36Sopenharmony_ci2.4 Swap Extension
23762306a36Sopenharmony_ci--------------------------------------
23862306a36Sopenharmony_ci
23962306a36Sopenharmony_ciSwap usage is always recorded for each of cgroup. Swap Extension allows you to
24062306a36Sopenharmony_ciread and limit it.
24162306a36Sopenharmony_ci
24262306a36Sopenharmony_ciWhen CONFIG_SWAP is enabled, following files are added.
24362306a36Sopenharmony_ci
24462306a36Sopenharmony_ci - memory.memsw.usage_in_bytes.
24562306a36Sopenharmony_ci - memory.memsw.limit_in_bytes.
24662306a36Sopenharmony_ci
24762306a36Sopenharmony_cimemsw means memory+swap. Usage of memory+swap is limited by
24862306a36Sopenharmony_cimemsw.limit_in_bytes.
24962306a36Sopenharmony_ci
25062306a36Sopenharmony_ciExample: Assume a system with 4G of swap. A task which allocates 6G of memory
25162306a36Sopenharmony_ci(by mistake) under 2G memory limitation will use all swap.
25262306a36Sopenharmony_ciIn this case, setting memsw.limit_in_bytes=3G will prevent bad use of swap.
25362306a36Sopenharmony_ciBy using the memsw limit, you can avoid system OOM which can be caused by swap
25462306a36Sopenharmony_cishortage.
25562306a36Sopenharmony_ci
25662306a36Sopenharmony_ci2.4.1 why 'memory+swap' rather than swap
25762306a36Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25862306a36Sopenharmony_ci
25962306a36Sopenharmony_ciThe global LRU(kswapd) can swap out arbitrary pages. Swap-out means
26062306a36Sopenharmony_cito move account from memory to swap...there is no change in usage of
26162306a36Sopenharmony_cimemory+swap. In other words, when we want to limit the usage of swap without
26262306a36Sopenharmony_ciaffecting global LRU, memory+swap limit is better than just limiting swap from
26362306a36Sopenharmony_cian OS point of view.
26462306a36Sopenharmony_ci
26562306a36Sopenharmony_ci2.4.2. What happens when a cgroup hits memory.memsw.limit_in_bytes
26662306a36Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
26762306a36Sopenharmony_ci
26862306a36Sopenharmony_ciWhen a cgroup hits memory.memsw.limit_in_bytes, it's useless to do swap-out
26962306a36Sopenharmony_ciin this cgroup. Then, swap-out will not be done by cgroup routine and file
27062306a36Sopenharmony_cicaches are dropped. But as mentioned above, global LRU can do swapout memory
27162306a36Sopenharmony_cifrom it for sanity of the system's memory management state. You can't forbid
27262306a36Sopenharmony_ciit by cgroup.
27362306a36Sopenharmony_ci
27462306a36Sopenharmony_ci2.5 Reclaim
27562306a36Sopenharmony_ci-----------
27662306a36Sopenharmony_ci
27762306a36Sopenharmony_ciEach cgroup maintains a per cgroup LRU which has the same structure as
27862306a36Sopenharmony_ciglobal VM. When a cgroup goes over its limit, we first try
27962306a36Sopenharmony_cito reclaim memory from the cgroup so as to make space for the new
28062306a36Sopenharmony_cipages that the cgroup has touched. If the reclaim is unsuccessful,
28162306a36Sopenharmony_cian OOM routine is invoked to select and kill the bulkiest task in the
28262306a36Sopenharmony_cicgroup. (See :ref:`10. OOM Control <cgroup-v1-memory-oom-control>` below.)
28362306a36Sopenharmony_ci
28462306a36Sopenharmony_ciThe reclaim algorithm has not been modified for cgroups, except that
28562306a36Sopenharmony_cipages that are selected for reclaiming come from the per-cgroup LRU
28662306a36Sopenharmony_cilist.
28762306a36Sopenharmony_ci
28862306a36Sopenharmony_ci.. note::
28962306a36Sopenharmony_ci   Reclaim does not work for the root cgroup, since we cannot set any
29062306a36Sopenharmony_ci   limits on the root cgroup.
29162306a36Sopenharmony_ci
29262306a36Sopenharmony_ci.. note::
29362306a36Sopenharmony_ci   When panic_on_oom is set to "2", the whole system will panic.
29462306a36Sopenharmony_ci
29562306a36Sopenharmony_ciWhen oom event notifier is registered, event will be delivered.
29662306a36Sopenharmony_ci(See :ref:`oom_control <cgroup-v1-memory-oom-control>` section)
29762306a36Sopenharmony_ci
29862306a36Sopenharmony_ci2.6 Locking
29962306a36Sopenharmony_ci-----------
30062306a36Sopenharmony_ci
30162306a36Sopenharmony_ciLock order is as follows::
30262306a36Sopenharmony_ci
30362306a36Sopenharmony_ci  Page lock (PG_locked bit of page->flags)
30462306a36Sopenharmony_ci    mm->page_table_lock or split pte_lock
30562306a36Sopenharmony_ci      folio_memcg_lock (memcg->move_lock)
30662306a36Sopenharmony_ci        mapping->i_pages lock
30762306a36Sopenharmony_ci          lruvec->lru_lock.
30862306a36Sopenharmony_ci
30962306a36Sopenharmony_ciPer-node-per-memcgroup LRU (cgroup's private LRU) is guarded by
31062306a36Sopenharmony_cilruvec->lru_lock; PG_lru bit of page->flags is cleared before
31162306a36Sopenharmony_ciisolating a page from its LRU under lruvec->lru_lock.
31262306a36Sopenharmony_ci
31362306a36Sopenharmony_ci.. _cgroup-v1-memory-kernel-extension:
31462306a36Sopenharmony_ci
31562306a36Sopenharmony_ci2.7 Kernel Memory Extension
31662306a36Sopenharmony_ci-----------------------------------------------
31762306a36Sopenharmony_ci
31862306a36Sopenharmony_ciWith the Kernel memory extension, the Memory Controller is able to limit
31962306a36Sopenharmony_cithe amount of kernel memory used by the system. Kernel memory is fundamentally
32062306a36Sopenharmony_cidifferent than user memory, since it can't be swapped out, which makes it
32162306a36Sopenharmony_cipossible to DoS the system by consuming too much of this precious resource.
32262306a36Sopenharmony_ci
32362306a36Sopenharmony_ciKernel memory accounting is enabled for all memory cgroups by default. But
32462306a36Sopenharmony_ciit can be disabled system-wide by passing cgroup.memory=nokmem to the kernel
32562306a36Sopenharmony_ciat boot time. In this case, kernel memory will not be accounted at all.
32662306a36Sopenharmony_ci
32762306a36Sopenharmony_ciKernel memory limits are not imposed for the root cgroup. Usage for the root
32862306a36Sopenharmony_cicgroup may or may not be accounted. The memory used is accumulated into
32962306a36Sopenharmony_cimemory.kmem.usage_in_bytes, or in a separate counter when it makes sense.
33062306a36Sopenharmony_ci(currently only for tcp).
33162306a36Sopenharmony_ci
33262306a36Sopenharmony_ciThe main "kmem" counter is fed into the main counter, so kmem charges will
33362306a36Sopenharmony_cialso be visible from the user counter.
33462306a36Sopenharmony_ci
33562306a36Sopenharmony_ciCurrently no soft limit is implemented for kernel memory. It is future work
33662306a36Sopenharmony_cito trigger slab reclaim when those limits are reached.
33762306a36Sopenharmony_ci
33862306a36Sopenharmony_ci2.7.1 Current Kernel Memory resources accounted
33962306a36Sopenharmony_ci-----------------------------------------------
34062306a36Sopenharmony_ci
34162306a36Sopenharmony_cistack pages:
34262306a36Sopenharmony_ci  every process consumes some stack pages. By accounting into
34362306a36Sopenharmony_ci  kernel memory, we prevent new processes from being created when the kernel
34462306a36Sopenharmony_ci  memory usage is too high.
34562306a36Sopenharmony_ci
34662306a36Sopenharmony_cislab pages:
34762306a36Sopenharmony_ci  pages allocated by the SLAB or SLUB allocator are tracked. A copy
34862306a36Sopenharmony_ci  of each kmem_cache is created every time the cache is touched by the first time
34962306a36Sopenharmony_ci  from inside the memcg. The creation is done lazily, so some objects can still be
35062306a36Sopenharmony_ci  skipped while the cache is being created. All objects in a slab page should
35162306a36Sopenharmony_ci  belong to the same memcg. This only fails to hold when a task is migrated to a
35262306a36Sopenharmony_ci  different memcg during the page allocation by the cache.
35362306a36Sopenharmony_ci
35462306a36Sopenharmony_cisockets memory pressure:
35562306a36Sopenharmony_ci  some sockets protocols have memory pressure
35662306a36Sopenharmony_ci  thresholds. The Memory Controller allows them to be controlled individually
35762306a36Sopenharmony_ci  per cgroup, instead of globally.
35862306a36Sopenharmony_ci
35962306a36Sopenharmony_citcp memory pressure:
36062306a36Sopenharmony_ci  sockets memory pressure for the tcp protocol.
36162306a36Sopenharmony_ci
36262306a36Sopenharmony_ci2.7.2 Common use cases
36362306a36Sopenharmony_ci----------------------
36462306a36Sopenharmony_ci
36562306a36Sopenharmony_ciBecause the "kmem" counter is fed to the main user counter, kernel memory can
36662306a36Sopenharmony_cinever be limited completely independently of user memory. Say "U" is the user
36762306a36Sopenharmony_cilimit, and "K" the kernel limit. There are three possible ways limits can be
36862306a36Sopenharmony_ciset:
36962306a36Sopenharmony_ci
37062306a36Sopenharmony_ciU != 0, K = unlimited:
37162306a36Sopenharmony_ci    This is the standard memcg limitation mechanism already present before kmem
37262306a36Sopenharmony_ci    accounting. Kernel memory is completely ignored.
37362306a36Sopenharmony_ci
37462306a36Sopenharmony_ciU != 0, K < U:
37562306a36Sopenharmony_ci    Kernel memory is a subset of the user memory. This setup is useful in
37662306a36Sopenharmony_ci    deployments where the total amount of memory per-cgroup is overcommitted.
37762306a36Sopenharmony_ci    Overcommitting kernel memory limits is definitely not recommended, since the
37862306a36Sopenharmony_ci    box can still run out of non-reclaimable memory.
37962306a36Sopenharmony_ci    In this case, the admin could set up K so that the sum of all groups is
38062306a36Sopenharmony_ci    never greater than the total memory, and freely set U at the cost of his
38162306a36Sopenharmony_ci    QoS.
38262306a36Sopenharmony_ci
38362306a36Sopenharmony_ci    .. warning::
38462306a36Sopenharmony_ci       In the current implementation, memory reclaim will NOT be triggered for
38562306a36Sopenharmony_ci       a cgroup when it hits K while staying below U, which makes this setup
38662306a36Sopenharmony_ci       impractical.
38762306a36Sopenharmony_ci
38862306a36Sopenharmony_ciU != 0, K >= U:
38962306a36Sopenharmony_ci    Since kmem charges will also be fed to the user counter and reclaim will be
39062306a36Sopenharmony_ci    triggered for the cgroup for both kinds of memory. This setup gives the
39162306a36Sopenharmony_ci    admin a unified view of memory, and it is also useful for people who just
39262306a36Sopenharmony_ci    want to track kernel memory usage.
39362306a36Sopenharmony_ci
39462306a36Sopenharmony_ci3. User Interface
39562306a36Sopenharmony_ci=================
39662306a36Sopenharmony_ci
39762306a36Sopenharmony_ciTo use the user interface:
39862306a36Sopenharmony_ci
39962306a36Sopenharmony_ci1. Enable CONFIG_CGROUPS and CONFIG_MEMCG options
40062306a36Sopenharmony_ci2. Prepare the cgroups (see :ref:`Why are cgroups needed?
40162306a36Sopenharmony_ci   <cgroups-why-needed>` for the background information)::
40262306a36Sopenharmony_ci
40362306a36Sopenharmony_ci	# mount -t tmpfs none /sys/fs/cgroup
40462306a36Sopenharmony_ci	# mkdir /sys/fs/cgroup/memory
40562306a36Sopenharmony_ci	# mount -t cgroup none /sys/fs/cgroup/memory -o memory
40662306a36Sopenharmony_ci
40762306a36Sopenharmony_ci3. Make the new group and move bash into it::
40862306a36Sopenharmony_ci
40962306a36Sopenharmony_ci	# mkdir /sys/fs/cgroup/memory/0
41062306a36Sopenharmony_ci	# echo $$ > /sys/fs/cgroup/memory/0/tasks
41162306a36Sopenharmony_ci
41262306a36Sopenharmony_ci4. Since now we're in the 0 cgroup, we can alter the memory limit::
41362306a36Sopenharmony_ci
41462306a36Sopenharmony_ci	# echo 4M > /sys/fs/cgroup/memory/0/memory.limit_in_bytes
41562306a36Sopenharmony_ci
41662306a36Sopenharmony_ci   The limit can now be queried::
41762306a36Sopenharmony_ci
41862306a36Sopenharmony_ci	# cat /sys/fs/cgroup/memory/0/memory.limit_in_bytes
41962306a36Sopenharmony_ci	4194304
42062306a36Sopenharmony_ci
42162306a36Sopenharmony_ci.. note::
42262306a36Sopenharmony_ci   We can use a suffix (k, K, m, M, g or G) to indicate values in kilo,
42362306a36Sopenharmony_ci   mega or gigabytes. (Here, Kilo, Mega, Giga are Kibibytes, Mebibytes,
42462306a36Sopenharmony_ci   Gibibytes.)
42562306a36Sopenharmony_ci
42662306a36Sopenharmony_ci.. note::
42762306a36Sopenharmony_ci   We can write "-1" to reset the ``*.limit_in_bytes(unlimited)``.
42862306a36Sopenharmony_ci
42962306a36Sopenharmony_ci.. note::
43062306a36Sopenharmony_ci   We cannot set limits on the root cgroup any more.
43162306a36Sopenharmony_ci
43262306a36Sopenharmony_ci
43362306a36Sopenharmony_ciWe can check the usage::
43462306a36Sopenharmony_ci
43562306a36Sopenharmony_ci  # cat /sys/fs/cgroup/memory/0/memory.usage_in_bytes
43662306a36Sopenharmony_ci  1216512
43762306a36Sopenharmony_ci
43862306a36Sopenharmony_ciA successful write to this file does not guarantee a successful setting of
43962306a36Sopenharmony_cithis limit to the value written into the file. This can be due to a
44062306a36Sopenharmony_cinumber of factors, such as rounding up to page boundaries or the total
44162306a36Sopenharmony_ciavailability of memory on the system. The user is required to re-read
44262306a36Sopenharmony_cithis file after a write to guarantee the value committed by the kernel::
44362306a36Sopenharmony_ci
44462306a36Sopenharmony_ci  # echo 1 > memory.limit_in_bytes
44562306a36Sopenharmony_ci  # cat memory.limit_in_bytes
44662306a36Sopenharmony_ci  4096
44762306a36Sopenharmony_ci
44862306a36Sopenharmony_ciThe memory.failcnt field gives the number of times that the cgroup limit was
44962306a36Sopenharmony_ciexceeded.
45062306a36Sopenharmony_ci
45162306a36Sopenharmony_ciThe memory.stat file gives accounting information. Now, the number of
45262306a36Sopenharmony_cicaches, RSS and Active pages/Inactive pages are shown.
45362306a36Sopenharmony_ci
45462306a36Sopenharmony_ci4. Testing
45562306a36Sopenharmony_ci==========
45662306a36Sopenharmony_ci
45762306a36Sopenharmony_ciFor testing features and implementation, see memcg_test.txt.
45862306a36Sopenharmony_ci
45962306a36Sopenharmony_ciPerformance test is also important. To see pure memory controller's overhead,
46062306a36Sopenharmony_citesting on tmpfs will give you good numbers of small overheads.
46162306a36Sopenharmony_ciExample: do kernel make on tmpfs.
46262306a36Sopenharmony_ci
46362306a36Sopenharmony_ciPage-fault scalability is also important. At measuring parallel
46462306a36Sopenharmony_cipage fault test, multi-process test may be better than multi-thread
46562306a36Sopenharmony_citest because it has noise of shared objects/status.
46662306a36Sopenharmony_ci
46762306a36Sopenharmony_ciBut the above two are testing extreme situations.
46862306a36Sopenharmony_ciTrying usual test under memory controller is always helpful.
46962306a36Sopenharmony_ci
47062306a36Sopenharmony_ci.. _cgroup-v1-memory-test-troubleshoot:
47162306a36Sopenharmony_ci
47262306a36Sopenharmony_ci4.1 Troubleshooting
47362306a36Sopenharmony_ci-------------------
47462306a36Sopenharmony_ci
47562306a36Sopenharmony_ciSometimes a user might find that the application under a cgroup is
47662306a36Sopenharmony_citerminated by the OOM killer. There are several causes for this:
47762306a36Sopenharmony_ci
47862306a36Sopenharmony_ci1. The cgroup limit is too low (just too low to do anything useful)
47962306a36Sopenharmony_ci2. The user is using anonymous memory and swap is turned off or too low
48062306a36Sopenharmony_ci
48162306a36Sopenharmony_ciA sync followed by echo 1 > /proc/sys/vm/drop_caches will help get rid of
48262306a36Sopenharmony_cisome of the pages cached in the cgroup (page cache pages).
48362306a36Sopenharmony_ci
48462306a36Sopenharmony_ciTo know what happens, disabling OOM_Kill as per :ref:`"10. OOM Control"
48562306a36Sopenharmony_ci<cgroup-v1-memory-oom-control>` (below) and seeing what happens will be
48662306a36Sopenharmony_cihelpful.
48762306a36Sopenharmony_ci
48862306a36Sopenharmony_ci.. _cgroup-v1-memory-test-task-migration:
48962306a36Sopenharmony_ci
49062306a36Sopenharmony_ci4.2 Task migration
49162306a36Sopenharmony_ci------------------
49262306a36Sopenharmony_ci
49362306a36Sopenharmony_ciWhen a task migrates from one cgroup to another, its charge is not
49462306a36Sopenharmony_cicarried forward by default. The pages allocated from the original cgroup still
49562306a36Sopenharmony_ciremain charged to it, the charge is dropped when the page is freed or
49662306a36Sopenharmony_cireclaimed.
49762306a36Sopenharmony_ci
49862306a36Sopenharmony_ciYou can move charges of a task along with task migration.
49962306a36Sopenharmony_ciSee :ref:`8. "Move charges at task migration" <cgroup-v1-memory-move-charges>`
50062306a36Sopenharmony_ci
50162306a36Sopenharmony_ci4.3 Removing a cgroup
50262306a36Sopenharmony_ci---------------------
50362306a36Sopenharmony_ci
50462306a36Sopenharmony_ciA cgroup can be removed by rmdir, but as discussed in :ref:`sections 4.1
50562306a36Sopenharmony_ci<cgroup-v1-memory-test-troubleshoot>` and :ref:`4.2
50662306a36Sopenharmony_ci<cgroup-v1-memory-test-task-migration>`, a cgroup might have some charge
50762306a36Sopenharmony_ciassociated with it, even though all tasks have migrated away from it. (because
50862306a36Sopenharmony_ciwe charge against pages, not against tasks.)
50962306a36Sopenharmony_ci
51062306a36Sopenharmony_ciWe move the stats to parent, and no change on the charge except uncharging
51162306a36Sopenharmony_cifrom the child.
51262306a36Sopenharmony_ci
51362306a36Sopenharmony_ciCharges recorded in swap information is not updated at removal of cgroup.
51462306a36Sopenharmony_ciRecorded information is discarded and a cgroup which uses swap (swapcache)
51562306a36Sopenharmony_ciwill be charged as a new owner of it.
51662306a36Sopenharmony_ci
51762306a36Sopenharmony_ci5. Misc. interfaces
51862306a36Sopenharmony_ci===================
51962306a36Sopenharmony_ci
52062306a36Sopenharmony_ci5.1 force_empty
52162306a36Sopenharmony_ci---------------
52262306a36Sopenharmony_ci  memory.force_empty interface is provided to make cgroup's memory usage empty.
52362306a36Sopenharmony_ci  When writing anything to this::
52462306a36Sopenharmony_ci
52562306a36Sopenharmony_ci    # echo 0 > memory.force_empty
52662306a36Sopenharmony_ci
52762306a36Sopenharmony_ci  the cgroup will be reclaimed and as many pages reclaimed as possible.
52862306a36Sopenharmony_ci
52962306a36Sopenharmony_ci  The typical use case for this interface is before calling rmdir().
53062306a36Sopenharmony_ci  Though rmdir() offlines memcg, but the memcg may still stay there due to
53162306a36Sopenharmony_ci  charged file caches. Some out-of-use page caches may keep charged until
53262306a36Sopenharmony_ci  memory pressure happens. If you want to avoid that, force_empty will be useful.
53362306a36Sopenharmony_ci
53462306a36Sopenharmony_ci5.2 stat file
53562306a36Sopenharmony_ci-------------
53662306a36Sopenharmony_ci
53762306a36Sopenharmony_cimemory.stat file includes following statistics:
53862306a36Sopenharmony_ci
53962306a36Sopenharmony_ci  * per-memory cgroup local status
54062306a36Sopenharmony_ci
54162306a36Sopenharmony_ci    =============== ===============================================================
54262306a36Sopenharmony_ci    cache           # of bytes of page cache memory.
54362306a36Sopenharmony_ci    rss             # of bytes of anonymous and swap cache memory (includes
54462306a36Sopenharmony_ci                    transparent hugepages).
54562306a36Sopenharmony_ci    rss_huge        # of bytes of anonymous transparent hugepages.
54662306a36Sopenharmony_ci    mapped_file     # of bytes of mapped file (includes tmpfs/shmem)
54762306a36Sopenharmony_ci    pgpgin          # of charging events to the memory cgroup. The charging
54862306a36Sopenharmony_ci                    event happens each time a page is accounted as either mapped
54962306a36Sopenharmony_ci                    anon page(RSS) or cache page(Page Cache) to the cgroup.
55062306a36Sopenharmony_ci    pgpgout         # of uncharging events to the memory cgroup. The uncharging
55162306a36Sopenharmony_ci                    event happens each time a page is unaccounted from the
55262306a36Sopenharmony_ci                    cgroup.
55362306a36Sopenharmony_ci    swap            # of bytes of swap usage
55462306a36Sopenharmony_ci    dirty           # of bytes that are waiting to get written back to the disk.
55562306a36Sopenharmony_ci    writeback       # of bytes of file/anon cache that are queued for syncing to
55662306a36Sopenharmony_ci                    disk.
55762306a36Sopenharmony_ci    inactive_anon   # of bytes of anonymous and swap cache memory on inactive
55862306a36Sopenharmony_ci                    LRU list.
55962306a36Sopenharmony_ci    active_anon     # of bytes of anonymous and swap cache memory on active
56062306a36Sopenharmony_ci                    LRU list.
56162306a36Sopenharmony_ci    inactive_file   # of bytes of file-backed memory and MADV_FREE anonymous
56262306a36Sopenharmony_ci                    memory (LazyFree pages) on inactive LRU list.
56362306a36Sopenharmony_ci    active_file     # of bytes of file-backed memory on active LRU list.
56462306a36Sopenharmony_ci    unevictable     # of bytes of memory that cannot be reclaimed (mlocked etc).
56562306a36Sopenharmony_ci    =============== ===============================================================
56662306a36Sopenharmony_ci
56762306a36Sopenharmony_ci  * status considering hierarchy (see memory.use_hierarchy settings):
56862306a36Sopenharmony_ci
56962306a36Sopenharmony_ci    ========================= ===================================================
57062306a36Sopenharmony_ci    hierarchical_memory_limit # of bytes of memory limit with regard to
57162306a36Sopenharmony_ci                              hierarchy
57262306a36Sopenharmony_ci                              under which the memory cgroup is
57362306a36Sopenharmony_ci    hierarchical_memsw_limit  # of bytes of memory+swap limit with regard to
57462306a36Sopenharmony_ci                              hierarchy under which memory cgroup is.
57562306a36Sopenharmony_ci
57662306a36Sopenharmony_ci    total_<counter>           # hierarchical version of <counter>, which in
57762306a36Sopenharmony_ci                              addition to the cgroup's own value includes the
57862306a36Sopenharmony_ci                              sum of all hierarchical children's values of
57962306a36Sopenharmony_ci                              <counter>, i.e. total_cache
58062306a36Sopenharmony_ci    ========================= ===================================================
58162306a36Sopenharmony_ci
58262306a36Sopenharmony_ci  * additional vm parameters (depends on CONFIG_DEBUG_VM):
58362306a36Sopenharmony_ci
58462306a36Sopenharmony_ci    ========================= ========================================
58562306a36Sopenharmony_ci    recent_rotated_anon       VM internal parameter. (see mm/vmscan.c)
58662306a36Sopenharmony_ci    recent_rotated_file       VM internal parameter. (see mm/vmscan.c)
58762306a36Sopenharmony_ci    recent_scanned_anon       VM internal parameter. (see mm/vmscan.c)
58862306a36Sopenharmony_ci    recent_scanned_file       VM internal parameter. (see mm/vmscan.c)
58962306a36Sopenharmony_ci    ========================= ========================================
59062306a36Sopenharmony_ci
59162306a36Sopenharmony_ci.. hint::
59262306a36Sopenharmony_ci	recent_rotated means recent frequency of LRU rotation.
59362306a36Sopenharmony_ci	recent_scanned means recent # of scans to LRU.
59462306a36Sopenharmony_ci	showing for better debug please see the code for meanings.
59562306a36Sopenharmony_ci
59662306a36Sopenharmony_ci.. note::
59762306a36Sopenharmony_ci	Only anonymous and swap cache memory is listed as part of 'rss' stat.
59862306a36Sopenharmony_ci	This should not be confused with the true 'resident set size' or the
59962306a36Sopenharmony_ci	amount of physical memory used by the cgroup.
60062306a36Sopenharmony_ci
60162306a36Sopenharmony_ci	'rss + mapped_file" will give you resident set size of cgroup.
60262306a36Sopenharmony_ci
60362306a36Sopenharmony_ci	(Note: file and shmem may be shared among other cgroups. In that case,
60462306a36Sopenharmony_ci	mapped_file is accounted only when the memory cgroup is owner of page
60562306a36Sopenharmony_ci	cache.)
60662306a36Sopenharmony_ci
60762306a36Sopenharmony_ci5.3 swappiness
60862306a36Sopenharmony_ci--------------
60962306a36Sopenharmony_ci
61062306a36Sopenharmony_ciOverrides /proc/sys/vm/swappiness for the particular group. The tunable
61162306a36Sopenharmony_ciin the root cgroup corresponds to the global swappiness setting.
61262306a36Sopenharmony_ci
61362306a36Sopenharmony_ciPlease note that unlike during the global reclaim, limit reclaim
61462306a36Sopenharmony_cienforces that 0 swappiness really prevents from any swapping even if
61562306a36Sopenharmony_cithere is a swap storage available. This might lead to memcg OOM killer
61662306a36Sopenharmony_ciif there are no file pages to reclaim.
61762306a36Sopenharmony_ci
61862306a36Sopenharmony_ci5.4 failcnt
61962306a36Sopenharmony_ci-----------
62062306a36Sopenharmony_ci
62162306a36Sopenharmony_ciA memory cgroup provides memory.failcnt and memory.memsw.failcnt files.
62262306a36Sopenharmony_ciThis failcnt(== failure count) shows the number of times that a usage counter
62362306a36Sopenharmony_cihit its limit. When a memory cgroup hits a limit, failcnt increases and
62462306a36Sopenharmony_cimemory under it will be reclaimed.
62562306a36Sopenharmony_ci
62662306a36Sopenharmony_ciYou can reset failcnt by writing 0 to failcnt file::
62762306a36Sopenharmony_ci
62862306a36Sopenharmony_ci	# echo 0 > .../memory.failcnt
62962306a36Sopenharmony_ci
63062306a36Sopenharmony_ci5.5 usage_in_bytes
63162306a36Sopenharmony_ci------------------
63262306a36Sopenharmony_ci
63362306a36Sopenharmony_ciFor efficiency, as other kernel components, memory cgroup uses some optimization
63462306a36Sopenharmony_cito avoid unnecessary cacheline false sharing. usage_in_bytes is affected by the
63562306a36Sopenharmony_cimethod and doesn't show 'exact' value of memory (and swap) usage, it's a fuzz
63662306a36Sopenharmony_civalue for efficient access. (Of course, when necessary, it's synchronized.)
63762306a36Sopenharmony_ciIf you want to know more exact memory usage, you should use RSS+CACHE(+SWAP)
63862306a36Sopenharmony_civalue in memory.stat(see 5.2).
63962306a36Sopenharmony_ci
64062306a36Sopenharmony_ci5.6 numa_stat
64162306a36Sopenharmony_ci-------------
64262306a36Sopenharmony_ci
64362306a36Sopenharmony_ciThis is similar to numa_maps but operates on a per-memcg basis.  This is
64462306a36Sopenharmony_ciuseful for providing visibility into the numa locality information within
64562306a36Sopenharmony_cian memcg since the pages are allowed to be allocated from any physical
64662306a36Sopenharmony_cinode.  One of the use cases is evaluating application performance by
64762306a36Sopenharmony_cicombining this information with the application's CPU allocation.
64862306a36Sopenharmony_ci
64962306a36Sopenharmony_ciEach memcg's numa_stat file includes "total", "file", "anon" and "unevictable"
65062306a36Sopenharmony_ciper-node page counts including "hierarchical_<counter>" which sums up all
65162306a36Sopenharmony_cihierarchical children's values in addition to the memcg's own value.
65262306a36Sopenharmony_ci
65362306a36Sopenharmony_ciThe output format of memory.numa_stat is::
65462306a36Sopenharmony_ci
65562306a36Sopenharmony_ci  total=<total pages> N0=<node 0 pages> N1=<node 1 pages> ...
65662306a36Sopenharmony_ci  file=<total file pages> N0=<node 0 pages> N1=<node 1 pages> ...
65762306a36Sopenharmony_ci  anon=<total anon pages> N0=<node 0 pages> N1=<node 1 pages> ...
65862306a36Sopenharmony_ci  unevictable=<total anon pages> N0=<node 0 pages> N1=<node 1 pages> ...
65962306a36Sopenharmony_ci  hierarchical_<counter>=<counter pages> N0=<node 0 pages> N1=<node 1 pages> ...
66062306a36Sopenharmony_ci
66162306a36Sopenharmony_ciThe "total" count is sum of file + anon + unevictable.
66262306a36Sopenharmony_ci
66362306a36Sopenharmony_ci6. Hierarchy support
66462306a36Sopenharmony_ci====================
66562306a36Sopenharmony_ci
66662306a36Sopenharmony_ciThe memory controller supports a deep hierarchy and hierarchical accounting.
66762306a36Sopenharmony_ciThe hierarchy is created by creating the appropriate cgroups in the
66862306a36Sopenharmony_cicgroup filesystem. Consider for example, the following cgroup filesystem
66962306a36Sopenharmony_cihierarchy::
67062306a36Sopenharmony_ci
67162306a36Sopenharmony_ci	       root
67262306a36Sopenharmony_ci	     /  |   \
67362306a36Sopenharmony_ci            /	|    \
67462306a36Sopenharmony_ci	   a	b     c
67562306a36Sopenharmony_ci		      | \
67662306a36Sopenharmony_ci		      |  \
67762306a36Sopenharmony_ci		      d   e
67862306a36Sopenharmony_ci
67962306a36Sopenharmony_ciIn the diagram above, with hierarchical accounting enabled, all memory
68062306a36Sopenharmony_ciusage of e, is accounted to its ancestors up until the root (i.e, c and root).
68162306a36Sopenharmony_ciIf one of the ancestors goes over its limit, the reclaim algorithm reclaims
68262306a36Sopenharmony_cifrom the tasks in the ancestor and the children of the ancestor.
68362306a36Sopenharmony_ci
68462306a36Sopenharmony_ci6.1 Hierarchical accounting and reclaim
68562306a36Sopenharmony_ci---------------------------------------
68662306a36Sopenharmony_ci
68762306a36Sopenharmony_ciHierarchical accounting is enabled by default. Disabling the hierarchical
68862306a36Sopenharmony_ciaccounting is deprecated. An attempt to do it will result in a failure
68962306a36Sopenharmony_ciand a warning printed to dmesg.
69062306a36Sopenharmony_ci
69162306a36Sopenharmony_ciFor compatibility reasons writing 1 to memory.use_hierarchy will always pass::
69262306a36Sopenharmony_ci
69362306a36Sopenharmony_ci	# echo 1 > memory.use_hierarchy
69462306a36Sopenharmony_ci
69562306a36Sopenharmony_ci7. Soft limits
69662306a36Sopenharmony_ci==============
69762306a36Sopenharmony_ci
69862306a36Sopenharmony_ciSoft limits allow for greater sharing of memory. The idea behind soft limits
69962306a36Sopenharmony_ciis to allow control groups to use as much of the memory as needed, provided
70062306a36Sopenharmony_ci
70162306a36Sopenharmony_cia. There is no memory contention
70262306a36Sopenharmony_cib. They do not exceed their hard limit
70362306a36Sopenharmony_ci
70462306a36Sopenharmony_ciWhen the system detects memory contention or low memory, control groups
70562306a36Sopenharmony_ciare pushed back to their soft limits. If the soft limit of each control
70662306a36Sopenharmony_cigroup is very high, they are pushed back as much as possible to make
70762306a36Sopenharmony_cisure that one control group does not starve the others of memory.
70862306a36Sopenharmony_ci
70962306a36Sopenharmony_ciPlease note that soft limits is a best-effort feature; it comes with
71062306a36Sopenharmony_cino guarantees, but it does its best to make sure that when memory is
71162306a36Sopenharmony_ciheavily contended for, memory is allocated based on the soft limit
71262306a36Sopenharmony_cihints/setup. Currently soft limit based reclaim is set up such that
71362306a36Sopenharmony_ciit gets invoked from balance_pgdat (kswapd).
71462306a36Sopenharmony_ci
71562306a36Sopenharmony_ci7.1 Interface
71662306a36Sopenharmony_ci-------------
71762306a36Sopenharmony_ci
71862306a36Sopenharmony_ciSoft limits can be setup by using the following commands (in this example we
71962306a36Sopenharmony_ciassume a soft limit of 256 MiB)::
72062306a36Sopenharmony_ci
72162306a36Sopenharmony_ci	# echo 256M > memory.soft_limit_in_bytes
72262306a36Sopenharmony_ci
72362306a36Sopenharmony_ciIf we want to change this to 1G, we can at any time use::
72462306a36Sopenharmony_ci
72562306a36Sopenharmony_ci	# echo 1G > memory.soft_limit_in_bytes
72662306a36Sopenharmony_ci
72762306a36Sopenharmony_ci.. note::
72862306a36Sopenharmony_ci       Soft limits take effect over a long period of time, since they involve
72962306a36Sopenharmony_ci       reclaiming memory for balancing between memory cgroups
73062306a36Sopenharmony_ci
73162306a36Sopenharmony_ci.. note::
73262306a36Sopenharmony_ci       It is recommended to set the soft limit always below the hard limit,
73362306a36Sopenharmony_ci       otherwise the hard limit will take precedence.
73462306a36Sopenharmony_ci
73562306a36Sopenharmony_ci.. _cgroup-v1-memory-move-charges:
73662306a36Sopenharmony_ci
73762306a36Sopenharmony_ci8. Move charges at task migration (DEPRECATED!)
73862306a36Sopenharmony_ci===============================================
73962306a36Sopenharmony_ci
74062306a36Sopenharmony_ciTHIS IS DEPRECATED!
74162306a36Sopenharmony_ci
74262306a36Sopenharmony_ciIt's expensive and unreliable! It's better practice to launch workload
74362306a36Sopenharmony_citasks directly from inside their target cgroup. Use dedicated workload
74462306a36Sopenharmony_cicgroups to allow fine-grained policy adjustments without having to
74562306a36Sopenharmony_cimove physical pages between control domains.
74662306a36Sopenharmony_ci
74762306a36Sopenharmony_ciUsers can move charges associated with a task along with task migration, that
74862306a36Sopenharmony_ciis, uncharge task's pages from the old cgroup and charge them to the new cgroup.
74962306a36Sopenharmony_ciThis feature is not supported in !CONFIG_MMU environments because of lack of
75062306a36Sopenharmony_cipage tables.
75162306a36Sopenharmony_ci
75262306a36Sopenharmony_ci8.1 Interface
75362306a36Sopenharmony_ci-------------
75462306a36Sopenharmony_ci
75562306a36Sopenharmony_ciThis feature is disabled by default. It can be enabled (and disabled again) by
75662306a36Sopenharmony_ciwriting to memory.move_charge_at_immigrate of the destination cgroup.
75762306a36Sopenharmony_ci
75862306a36Sopenharmony_ciIf you want to enable it::
75962306a36Sopenharmony_ci
76062306a36Sopenharmony_ci	# echo (some positive value) > memory.move_charge_at_immigrate
76162306a36Sopenharmony_ci
76262306a36Sopenharmony_ci.. note::
76362306a36Sopenharmony_ci      Each bits of move_charge_at_immigrate has its own meaning about what type
76462306a36Sopenharmony_ci      of charges should be moved. See :ref:`section 8.2
76562306a36Sopenharmony_ci      <cgroup-v1-memory-movable-charges>` for details.
76662306a36Sopenharmony_ci
76762306a36Sopenharmony_ci.. note::
76862306a36Sopenharmony_ci      Charges are moved only when you move mm->owner, in other words,
76962306a36Sopenharmony_ci      a leader of a thread group.
77062306a36Sopenharmony_ci
77162306a36Sopenharmony_ci.. note::
77262306a36Sopenharmony_ci      If we cannot find enough space for the task in the destination cgroup, we
77362306a36Sopenharmony_ci      try to make space by reclaiming memory. Task migration may fail if we
77462306a36Sopenharmony_ci      cannot make enough space.
77562306a36Sopenharmony_ci
77662306a36Sopenharmony_ci.. note::
77762306a36Sopenharmony_ci      It can take several seconds if you move charges much.
77862306a36Sopenharmony_ci
77962306a36Sopenharmony_ciAnd if you want disable it again::
78062306a36Sopenharmony_ci
78162306a36Sopenharmony_ci	# echo 0 > memory.move_charge_at_immigrate
78262306a36Sopenharmony_ci
78362306a36Sopenharmony_ci.. _cgroup-v1-memory-movable-charges:
78462306a36Sopenharmony_ci
78562306a36Sopenharmony_ci8.2 Type of charges which can be moved
78662306a36Sopenharmony_ci--------------------------------------
78762306a36Sopenharmony_ci
78862306a36Sopenharmony_ciEach bit in move_charge_at_immigrate has its own meaning about what type of
78962306a36Sopenharmony_cicharges should be moved. But in any case, it must be noted that an account of
79062306a36Sopenharmony_cia page or a swap can be moved only when it is charged to the task's current
79162306a36Sopenharmony_ci(old) memory cgroup.
79262306a36Sopenharmony_ci
79362306a36Sopenharmony_ci+---+--------------------------------------------------------------------------+
79462306a36Sopenharmony_ci|bit| what type of charges would be moved ?                                    |
79562306a36Sopenharmony_ci+===+==========================================================================+
79662306a36Sopenharmony_ci| 0 | A charge of an anonymous page (or swap of it) used by the target task.   |
79762306a36Sopenharmony_ci|   | You must enable Swap Extension (see 2.4) to enable move of swap charges. |
79862306a36Sopenharmony_ci+---+--------------------------------------------------------------------------+
79962306a36Sopenharmony_ci| 1 | A charge of file pages (normal file, tmpfs file (e.g. ipc shared memory) |
80062306a36Sopenharmony_ci|   | and swaps of tmpfs file) mmapped by the target task. Unlike the case of  |
80162306a36Sopenharmony_ci|   | anonymous pages, file pages (and swaps) in the range mmapped by the task |
80262306a36Sopenharmony_ci|   | will be moved even if the task hasn't done page fault, i.e. they might   |
80362306a36Sopenharmony_ci|   | not be the task's "RSS", but other task's "RSS" that maps the same file. |
80462306a36Sopenharmony_ci|   | And mapcount of the page is ignored (the page can be moved even if       |
80562306a36Sopenharmony_ci|   | page_mapcount(page) > 1). You must enable Swap Extension (see 2.4) to    |
80662306a36Sopenharmony_ci|   | enable move of swap charges.                                             |
80762306a36Sopenharmony_ci+---+--------------------------------------------------------------------------+
80862306a36Sopenharmony_ci
80962306a36Sopenharmony_ci8.3 TODO
81062306a36Sopenharmony_ci--------
81162306a36Sopenharmony_ci
81262306a36Sopenharmony_ci- All of moving charge operations are done under cgroup_mutex. It's not good
81362306a36Sopenharmony_ci  behavior to hold the mutex too long, so we may need some trick.
81462306a36Sopenharmony_ci
81562306a36Sopenharmony_ci9. Memory thresholds
81662306a36Sopenharmony_ci====================
81762306a36Sopenharmony_ci
81862306a36Sopenharmony_ciMemory cgroup implements memory thresholds using the cgroups notification
81962306a36Sopenharmony_ciAPI (see cgroups.txt). It allows to register multiple memory and memsw
82062306a36Sopenharmony_cithresholds and gets notifications when it crosses.
82162306a36Sopenharmony_ci
82262306a36Sopenharmony_ciTo register a threshold, an application must:
82362306a36Sopenharmony_ci
82462306a36Sopenharmony_ci- create an eventfd using eventfd(2);
82562306a36Sopenharmony_ci- open memory.usage_in_bytes or memory.memsw.usage_in_bytes;
82662306a36Sopenharmony_ci- write string like "<event_fd> <fd of memory.usage_in_bytes> <threshold>" to
82762306a36Sopenharmony_ci  cgroup.event_control.
82862306a36Sopenharmony_ci
82962306a36Sopenharmony_ciApplication will be notified through eventfd when memory usage crosses
83062306a36Sopenharmony_cithreshold in any direction.
83162306a36Sopenharmony_ci
83262306a36Sopenharmony_ciIt's applicable for root and non-root cgroup.
83362306a36Sopenharmony_ci
83462306a36Sopenharmony_ci.. _cgroup-v1-memory-oom-control:
83562306a36Sopenharmony_ci
83662306a36Sopenharmony_ci10. OOM Control
83762306a36Sopenharmony_ci===============
83862306a36Sopenharmony_ci
83962306a36Sopenharmony_cimemory.oom_control file is for OOM notification and other controls.
84062306a36Sopenharmony_ci
84162306a36Sopenharmony_ciMemory cgroup implements OOM notifier using the cgroup notification
84262306a36Sopenharmony_ciAPI (See cgroups.txt). It allows to register multiple OOM notification
84362306a36Sopenharmony_cidelivery and gets notification when OOM happens.
84462306a36Sopenharmony_ci
84562306a36Sopenharmony_ciTo register a notifier, an application must:
84662306a36Sopenharmony_ci
84762306a36Sopenharmony_ci - create an eventfd using eventfd(2)
84862306a36Sopenharmony_ci - open memory.oom_control file
84962306a36Sopenharmony_ci - write string like "<event_fd> <fd of memory.oom_control>" to
85062306a36Sopenharmony_ci   cgroup.event_control
85162306a36Sopenharmony_ci
85262306a36Sopenharmony_ciThe application will be notified through eventfd when OOM happens.
85362306a36Sopenharmony_ciOOM notification doesn't work for the root cgroup.
85462306a36Sopenharmony_ci
85562306a36Sopenharmony_ciYou can disable the OOM-killer by writing "1" to memory.oom_control file, as:
85662306a36Sopenharmony_ci
85762306a36Sopenharmony_ci	#echo 1 > memory.oom_control
85862306a36Sopenharmony_ci
85962306a36Sopenharmony_ciIf OOM-killer is disabled, tasks under cgroup will hang/sleep
86062306a36Sopenharmony_ciin memory cgroup's OOM-waitqueue when they request accountable memory.
86162306a36Sopenharmony_ci
86262306a36Sopenharmony_ciFor running them, you have to relax the memory cgroup's OOM status by
86362306a36Sopenharmony_ci
86462306a36Sopenharmony_ci	* enlarge limit or reduce usage.
86562306a36Sopenharmony_ci
86662306a36Sopenharmony_ciTo reduce usage,
86762306a36Sopenharmony_ci
86862306a36Sopenharmony_ci	* kill some tasks.
86962306a36Sopenharmony_ci	* move some tasks to other group with account migration.
87062306a36Sopenharmony_ci	* remove some files (on tmpfs?)
87162306a36Sopenharmony_ci
87262306a36Sopenharmony_ciThen, stopped tasks will work again.
87362306a36Sopenharmony_ci
87462306a36Sopenharmony_ciAt reading, current status of OOM is shown.
87562306a36Sopenharmony_ci
87662306a36Sopenharmony_ci	- oom_kill_disable 0 or 1
87762306a36Sopenharmony_ci	  (if 1, oom-killer is disabled)
87862306a36Sopenharmony_ci	- under_oom	   0 or 1
87962306a36Sopenharmony_ci	  (if 1, the memory cgroup is under OOM, tasks may be stopped.)
88062306a36Sopenharmony_ci        - oom_kill         integer counter
88162306a36Sopenharmony_ci          The number of processes belonging to this cgroup killed by any
88262306a36Sopenharmony_ci          kind of OOM killer.
88362306a36Sopenharmony_ci
88462306a36Sopenharmony_ci11. Memory Pressure
88562306a36Sopenharmony_ci===================
88662306a36Sopenharmony_ci
88762306a36Sopenharmony_ciThe pressure level notifications can be used to monitor the memory
88862306a36Sopenharmony_ciallocation cost; based on the pressure, applications can implement
88962306a36Sopenharmony_cidifferent strategies of managing their memory resources. The pressure
89062306a36Sopenharmony_cilevels are defined as following:
89162306a36Sopenharmony_ci
89262306a36Sopenharmony_ciThe "low" level means that the system is reclaiming memory for new
89362306a36Sopenharmony_ciallocations. Monitoring this reclaiming activity might be useful for
89462306a36Sopenharmony_cimaintaining cache level. Upon notification, the program (typically
89562306a36Sopenharmony_ci"Activity Manager") might analyze vmstat and act in advance (i.e.
89662306a36Sopenharmony_ciprematurely shutdown unimportant services).
89762306a36Sopenharmony_ci
89862306a36Sopenharmony_ciThe "medium" level means that the system is experiencing medium memory
89962306a36Sopenharmony_cipressure, the system might be making swap, paging out active file caches,
90062306a36Sopenharmony_cietc. Upon this event applications may decide to further analyze
90162306a36Sopenharmony_civmstat/zoneinfo/memcg or internal memory usage statistics and free any
90262306a36Sopenharmony_ciresources that can be easily reconstructed or re-read from a disk.
90362306a36Sopenharmony_ci
90462306a36Sopenharmony_ciThe "critical" level means that the system is actively thrashing, it is
90562306a36Sopenharmony_ciabout to out of memory (OOM) or even the in-kernel OOM killer is on its
90662306a36Sopenharmony_ciway to trigger. Applications should do whatever they can to help the
90762306a36Sopenharmony_cisystem. It might be too late to consult with vmstat or any other
90862306a36Sopenharmony_cistatistics, so it's advisable to take an immediate action.
90962306a36Sopenharmony_ci
91062306a36Sopenharmony_ciBy default, events are propagated upward until the event is handled, i.e. the
91162306a36Sopenharmony_cievents are not pass-through. For example, you have three cgroups: A->B->C. Now
91262306a36Sopenharmony_ciyou set up an event listener on cgroups A, B and C, and suppose group C
91362306a36Sopenharmony_ciexperiences some pressure. In this situation, only group C will receive the
91462306a36Sopenharmony_cinotification, i.e. groups A and B will not receive it. This is done to avoid
91562306a36Sopenharmony_ciexcessive "broadcasting" of messages, which disturbs the system and which is
91662306a36Sopenharmony_ciespecially bad if we are low on memory or thrashing. Group B, will receive
91762306a36Sopenharmony_cinotification only if there are no event listeners for group C.
91862306a36Sopenharmony_ci
91962306a36Sopenharmony_ciThere are three optional modes that specify different propagation behavior:
92062306a36Sopenharmony_ci
92162306a36Sopenharmony_ci - "default": this is the default behavior specified above. This mode is the
92262306a36Sopenharmony_ci   same as omitting the optional mode parameter, preserved by backwards
92362306a36Sopenharmony_ci   compatibility.
92462306a36Sopenharmony_ci
92562306a36Sopenharmony_ci - "hierarchy": events always propagate up to the root, similar to the default
92662306a36Sopenharmony_ci   behavior, except that propagation continues regardless of whether there are
92762306a36Sopenharmony_ci   event listeners at each level, with the "hierarchy" mode. In the above
92862306a36Sopenharmony_ci   example, groups A, B, and C will receive notification of memory pressure.
92962306a36Sopenharmony_ci
93062306a36Sopenharmony_ci - "local": events are pass-through, i.e. they only receive notifications when
93162306a36Sopenharmony_ci   memory pressure is experienced in the memcg for which the notification is
93262306a36Sopenharmony_ci   registered. In the above example, group C will receive notification if
93362306a36Sopenharmony_ci   registered for "local" notification and the group experiences memory
93462306a36Sopenharmony_ci   pressure. However, group B will never receive notification, regardless if
93562306a36Sopenharmony_ci   there is an event listener for group C or not, if group B is registered for
93662306a36Sopenharmony_ci   local notification.
93762306a36Sopenharmony_ci
93862306a36Sopenharmony_ciThe level and event notification mode ("hierarchy" or "local", if necessary) are
93962306a36Sopenharmony_cispecified by a comma-delimited string, i.e. "low,hierarchy" specifies
94062306a36Sopenharmony_cihierarchical, pass-through, notification for all ancestor memcgs. Notification
94162306a36Sopenharmony_cithat is the default, non pass-through behavior, does not specify a mode.
94262306a36Sopenharmony_ci"medium,local" specifies pass-through notification for the medium level.
94362306a36Sopenharmony_ci
94462306a36Sopenharmony_ciThe file memory.pressure_level is only used to setup an eventfd. To
94562306a36Sopenharmony_ciregister a notification, an application must:
94662306a36Sopenharmony_ci
94762306a36Sopenharmony_ci- create an eventfd using eventfd(2);
94862306a36Sopenharmony_ci- open memory.pressure_level;
94962306a36Sopenharmony_ci- write string as "<event_fd> <fd of memory.pressure_level> <level[,mode]>"
95062306a36Sopenharmony_ci  to cgroup.event_control.
95162306a36Sopenharmony_ci
95262306a36Sopenharmony_ciApplication will be notified through eventfd when memory pressure is at
95362306a36Sopenharmony_cithe specific level (or higher). Read/write operations to
95462306a36Sopenharmony_cimemory.pressure_level are no implemented.
95562306a36Sopenharmony_ci
95662306a36Sopenharmony_ciTest:
95762306a36Sopenharmony_ci
95862306a36Sopenharmony_ci   Here is a small script example that makes a new cgroup, sets up a
95962306a36Sopenharmony_ci   memory limit, sets up a notification in the cgroup and then makes child
96062306a36Sopenharmony_ci   cgroup experience a critical pressure::
96162306a36Sopenharmony_ci
96262306a36Sopenharmony_ci	# cd /sys/fs/cgroup/memory/
96362306a36Sopenharmony_ci	# mkdir foo
96462306a36Sopenharmony_ci	# cd foo
96562306a36Sopenharmony_ci	# cgroup_event_listener memory.pressure_level low,hierarchy &
96662306a36Sopenharmony_ci	# echo 8000000 > memory.limit_in_bytes
96762306a36Sopenharmony_ci	# echo 8000000 > memory.memsw.limit_in_bytes
96862306a36Sopenharmony_ci	# echo $$ > tasks
96962306a36Sopenharmony_ci	# dd if=/dev/zero | read x
97062306a36Sopenharmony_ci
97162306a36Sopenharmony_ci   (Expect a bunch of notifications, and eventually, the oom-killer will
97262306a36Sopenharmony_ci   trigger.)
97362306a36Sopenharmony_ci
97462306a36Sopenharmony_ci12. TODO
97562306a36Sopenharmony_ci========
97662306a36Sopenharmony_ci
97762306a36Sopenharmony_ci1. Make per-cgroup scanner reclaim not-shared pages first
97862306a36Sopenharmony_ci2. Teach controller to account for shared-pages
97962306a36Sopenharmony_ci3. Start reclamation in the background when the limit is
98062306a36Sopenharmony_ci   not yet hit but the usage is getting closer
98162306a36Sopenharmony_ci
98262306a36Sopenharmony_ciSummary
98362306a36Sopenharmony_ci=======
98462306a36Sopenharmony_ci
98562306a36Sopenharmony_ciOverall, the memory controller has been a stable controller and has been
98662306a36Sopenharmony_cicommented and discussed quite extensively in the community.
98762306a36Sopenharmony_ci
98862306a36Sopenharmony_ciReferences
98962306a36Sopenharmony_ci==========
99062306a36Sopenharmony_ci
99162306a36Sopenharmony_ci.. [1] Singh, Balbir. RFC: Memory Controller, http://lwn.net/Articles/206697/
99262306a36Sopenharmony_ci.. [2] Singh, Balbir. Memory Controller (RSS Control),
99362306a36Sopenharmony_ci   http://lwn.net/Articles/222762/
99462306a36Sopenharmony_ci.. [3] Emelianov, Pavel. Resource controllers based on process cgroups
99562306a36Sopenharmony_ci   https://lore.kernel.org/r/45ED7DEC.7010403@sw.ru
99662306a36Sopenharmony_ci.. [4] Emelianov, Pavel. RSS controller based on process cgroups (v2)
99762306a36Sopenharmony_ci   https://lore.kernel.org/r/461A3010.90403@sw.ru
99862306a36Sopenharmony_ci.. [5] Emelianov, Pavel. RSS controller based on process cgroups (v3)
99962306a36Sopenharmony_ci   https://lore.kernel.org/r/465D9739.8070209@openvz.org
100062306a36Sopenharmony_ci
100162306a36Sopenharmony_ci6. Menage, Paul. Control Groups v10, http://lwn.net/Articles/236032/
100262306a36Sopenharmony_ci7. Vaidyanathan, Srinivasan, Control Groups: Pagecache accounting and control
100362306a36Sopenharmony_ci   subsystem (v3), http://lwn.net/Articles/235534/
100462306a36Sopenharmony_ci8. Singh, Balbir. RSS controller v2 test results (lmbench),
100562306a36Sopenharmony_ci   https://lore.kernel.org/r/464C95D4.7070806@linux.vnet.ibm.com
100662306a36Sopenharmony_ci9. Singh, Balbir. RSS controller v2 AIM9 results
100762306a36Sopenharmony_ci   https://lore.kernel.org/r/464D267A.50107@linux.vnet.ibm.com
100862306a36Sopenharmony_ci10. Singh, Balbir. Memory controller v6 test results,
100962306a36Sopenharmony_ci    https://lore.kernel.org/r/20070819094658.654.84837.sendpatchset@balbir-laptop
101062306a36Sopenharmony_ci
101162306a36Sopenharmony_ci.. [11] Singh, Balbir. Memory controller introduction (v6),
101262306a36Sopenharmony_ci   https://lore.kernel.org/r/20070817084228.26003.12568.sendpatchset@balbir-laptop
101362306a36Sopenharmony_ci.. [12] Corbet, Jonathan, Controlling memory use in cgroups,
101462306a36Sopenharmony_ci   http://lwn.net/Articles/243795/
1015