5 a. Enable control of both RSS (mapped) and Page Cache (unmapped) pages
6 b. The infrastructure allows easy addition of other types of memory to control
7 c. Provides *zero overhead* for non memory controller users
8 d. Provides a double LRU: global memory pressure causes reclaim from the
9 global LRU; a cgroup on hitting a limit, reclaims from the per
12 NOTE: Page Cache (unmapped) also includes Swap Cache pages as a subset
13 and will not be referred to explicitly in the rest of the documentation.
15 Benefits and Purpose of the memory controller
17 The memory controller isolates the memory behaviour of a group of tasks
18 from the rest of the system. The article on LWN [12] mentions some probable
19 uses of the memory controller. The memory controller can be used to
21 a. Isolate an application or a group of applications
22 Memory hungry applications can be isolated and limited to a smaller
24 b. Create a cgroup with limited amount of memory, this can be used
25 as a good alternative to booting with mem=XXXX.
26 c. Virtualization solutions can control the amount of memory they want
27 to assign to a virtual machine instance.
28 d. A CD/DVD burner could control the amount of memory used by the
29 rest of the system to ensure that burning does not fail due to lack
31 e. There are several other use cases, find one or use the controller just
32 for fun (to learn and hack on the VM subsystem).
36 The memory controller has a long history. A request for comments for the memory
37 controller was posted by Balbir Singh [1]. At the time the RFC was posted
38 there were several implementations for memory control. The goal of the
39 RFC was to build consensus and agreement for the minimal features required
40 for memory control. The first RSS controller was posted by Balbir Singh[2]
41 in Feb 2007. Pavel Emelianov [3][4][5] has since posted three versions of the
42 RSS controller. At OLS, at the resource management BoF, everyone suggested
43 that we handle both page cache and RSS together. Another request was raised
44 to allow user space handling of OOM. The current memory controller is
45 at version 6; it combines both mapped (RSS) and unmapped Page
50 Memory is a unique resource in the sense that it is present in a limited
51 amount. If a task requires a lot of CPU processing, the task can spread
52 its processing over a period of hours, days, months or years, but with
53 memory, the same physical memory needs to be reused to accomplish the task.
55 The memory controller implementation has been divided into phases. These
59 2. mlock(2) controller
60 3. Kernel user memory accounting and slab control
61 4. user mappings length controller
63 The memory controller is the first controller developed.
67 The core of the design is a counter called the res_counter. The res_counter
68 tracks the current memory usage and limit of the group of processes associated
69 with the controller. Each cgroup has a memory controller specific data
70 structure (mem_cgroup) associated with it.
74 +--------------------+
77 +--------------------+
80 +---------------+ | +---------------+
81 | mm_struct | |.... | mm_struct |
83 +---------------+ | +---------------+
87 +---------------+ +------+--------+
88 | page +----------> page_cgroup|
90 +---------------+ +---------------+
92 (Figure 1: Hierarchy of Accounting)
95 Figure 1 shows the important aspects of the controller
97 1. Accounting happens per cgroup
98 2. Each mm_struct knows about which cgroup it belongs to
99 3. Each page has a pointer to the page_cgroup, which in turn knows the
102 The accounting is done as follows: mem_cgroup_charge() is invoked to setup
103 the necessary data structures and check if the cgroup that is being charged
104 is over its limit. If it is then reclaim is invoked on the cgroup.
105 More details can be found in the reclaim section of this document.
106 If everything goes well, a page meta-data-structure called page_cgroup is
107 allocated and associated with the page. This routine also adds the page to
110 2.2.1 Accounting details
112 All mapped pages (RSS) and unmapped user pages (Page Cache) are accounted.
113 RSS pages are accounted at the time of page_add_*_rmap() unless they've already
114 been accounted for earlier. A file page will be accounted for as Page Cache;
115 it's mapped into the page tables of a process, duplicate accounting is carefully
116 avoided. Page Cache pages are accounted at the time of add_to_page_cache().
117 The corresponding routines that remove a page from the page tables or removes
118 a page from Page Cache is used to decrement the accounting counters of the
121 2.3 Shared Page Accounting
123 Shared pages are accounted on the basis of the first touch approach. The
124 cgroup that first touches a page is accounted for the page. The principle
125 behind this approach is that a cgroup that aggressively uses a shared
126 page will eventually get charged for it (once it is uncharged from
127 the cgroup that brought it in -- this will happen on memory pressure).
131 Each cgroup maintains a per cgroup LRU that consists of an active
132 and inactive list. When a cgroup goes over its limit, we first try
133 to reclaim memory from the cgroup so as to make space for the new
134 pages that the cgroup has touched. If the reclaim is unsuccessful,
135 an OOM routine is invoked to select and kill the bulkiest task in the
138 The reclaim algorithm has not been modified for cgroups, except that
139 pages that are selected for reclaiming come from the per cgroup LRU
144 The memory controller uses the following hierarchy
146 1. zone->lru_lock is used for selecting pages to be isolated
147 2. mem->lru_lock protects the per cgroup LRU
148 3. lock_page_cgroup() is used to protect page->page_cgroup
154 a. Enable CONFIG_CGROUPS
155 b. Enable CONFIG_RESOURCE_COUNTERS
156 c. Enable CONFIG_CGROUP_MEM_CONT
158 1. Prepare the cgroups
160 # mount -t cgroup none /cgroups -o memory
162 2. Make the new group and move bash into it
164 # echo $$ > /cgroups/0/tasks
166 Since now we're in the 0 cgroup,
167 We can alter the memory limit:
168 # echo -n 4M > /cgroups/0/memory.limit_in_bytes
170 NOTE: We can use a suffix (k, K, m, M, g or G) to indicate values in kilo,
173 # cat /cgroups/0/memory.limit_in_bytes
176 NOTE: The interface has now changed to display the usage in bytes
179 We can check the usage:
180 # cat /cgroups/0/memory.usage_in_bytes
183 A successful write to this file does not guarantee a successful set of
184 this limit to the value written into the file. This can be due to a
185 number of factors, such as rounding up to page boundaries or the total
186 availability of memory on the system. The user is required to re-read
187 this file after a write to guarantee the value committed by the kernel.
189 # echo -n 1 > memory.limit_in_bytes
190 # cat memory.limit_in_bytes
193 The memory.failcnt field gives the number of times that the cgroup limit was
198 Balbir posted lmbench, AIM9, LTP and vmmstress results [10] and [11].
199 Apart from that v6 has been tested with several applications and regular
200 daily use. The controller has also been tested on the PPC64, x86_64 and
205 Sometimes a user might find that the application under a cgroup is
206 terminated. There are several causes for this:
208 1. The cgroup limit is too low (just too low to do anything useful)
209 2. The user is using anonymous memory and swap is turned off or too low
211 A sync followed by echo 1 > /proc/sys/vm/drop_caches will help get rid of
212 some of the pages cached in the cgroup (page cache pages).
216 When a task migrates from one cgroup to another, it's charge is not
217 carried forward. The pages allocated from the original cgroup still
218 remain charged to it, the charge is dropped when the page is freed or
221 4.3 Removing a cgroup
223 A cgroup can be removed by rmdir, but as discussed in sections 4.1 and 4.2, a
224 cgroup might have some charge associated with it, even though all
225 tasks have migrated away from it. If some pages are still left, after following
226 the steps listed in sections 4.1 and 4.2, check the Swap Cache usage in
227 /proc/meminfo to see if the Swap Cache usage is showing up in the
228 cgroups memory.usage_in_bytes counter. A simple test of swapoff -a and
229 swapon -a should free any pending Swap Cache usage.
231 4.4 Choosing what to account -- Page Cache (unmapped) vs RSS (mapped)?
233 The type of memory accounted by the cgroup can be limited to just
234 mapped pages by writing "1" to memory.control_type field
236 echo -n 1 > memory.control_type
240 1. Add support for accounting huge pages (as a separate controller)
241 2. Improve the user interface to accept/display memory limits in KB or MB
242 rather than pages (since page sizes can differ across platforms/machines).
243 3. Make cgroup lists per-zone
244 4. Make per-cgroup scanner reclaim not-shared pages first
245 5. Teach controller to account for shared-pages
246 6. Start reclamation when the limit is lowered
247 7. Start reclamation in the background when the limit is
248 not yet hit but the usage is getting closer
249 8. Create per zone LRU lists per cgroup
253 Overall, the memory controller has been a stable controller and has been
254 commented and discussed quite extensively in the community.
258 1. Singh, Balbir. RFC: Memory Controller, http://lwn.net/Articles/206697/
259 2. Singh, Balbir. Memory Controller (RSS Control),
260 http://lwn.net/Articles/222762/
261 3. Emelianov, Pavel. Resource controllers based on process cgroups
262 http://lkml.org/lkml/2007/3/6/198
263 4. Emelianov, Pavel. RSS controller based on process cgroups (v2)
264 http://lkml.org/lkml/2007/4/9/74
265 5. Emelianov, Pavel. RSS controller based on process cgroups (v3)
266 http://lkml.org/lkml/2007/5/30/244
267 6. Menage, Paul. Control Groups v10, http://lwn.net/Articles/236032/
268 7. Vaidyanathan, Srinivasan, Control Groups: Pagecache accounting and control
269 subsystem (v3), http://lwn.net/Articles/235534/
270 8. Singh, Balbir. RSS controller V2 test results (lmbench),
271 http://lkml.org/lkml/2007/5/17/232
272 9. Singh, Balbir. RSS controller V2 AIM9 results
273 http://lkml.org/lkml/2007/5/18/1
274 10. Singh, Balbir. Memory controller v6 results,
275 http://lkml.org/lkml/2007/8/19/36
276 11. Singh, Balbir. Memory controller v6, http://lkml.org/lkml/2007/8/17/69
277 12. Corbet, Jonathan, Controlling memory use in cgroups,
278 http://lwn.net/Articles/243795/