aboutsummaryrefslogtreecommitdiffstats
path: root/Documentation/controllers
diff options
context:
space:
mode:
Diffstat (limited to 'Documentation/controllers')
-rw-r--r--Documentation/controllers/memory.txt259
1 files changed, 259 insertions, 0 deletions
diff --git a/Documentation/controllers/memory.txt b/Documentation/controllers/memory.txt
new file mode 100644
index 000000000000..7e27baacca7b
--- /dev/null
+++ b/Documentation/controllers/memory.txt
@@ -0,0 +1,259 @@
1Memory Controller
2
3Salient features
4
5a. Enable control of both RSS (mapped) and Page Cache (unmapped) pages
6b. The infrastructure allows easy addition of other types of memory to control
7c. Provides *zero overhead* for non memory controller users
8d. Provides a double LRU: global memory pressure causes reclaim from the
9 global LRU; a cgroup on hitting a limit, reclaims from the per
10 cgroup LRU
11
12NOTE: Page Cache (unmapped) also includes Swap Cache pages as a subset
13and will not be referred to explicitly in the rest of the documentation.
14
15Benefits and Purpose of the memory controller
16
17The memory controller isolates the memory behaviour of a group of tasks
18from the rest of the system. The article on LWN [12] mentions some probable
19uses of the memory controller. The memory controller can be used to
20
21a. Isolate an application or a group of applications
22 Memory hungry applications can be isolated and limited to a smaller
23 amount of memory.
24b. Create a cgroup with limited amount of memory, this can be used
25 as a good alternative to booting with mem=XXXX.
26c. Virtualization solutions can control the amount of memory they want
27 to assign to a virtual machine instance.
28d. 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
30 of available memory.
31e. There are several other use cases, find one or use the controller just
32 for fun (to learn and hack on the VM subsystem).
33
341. History
35
36The memory controller has a long history. A request for comments for the memory
37controller was posted by Balbir Singh [1]. At the time the RFC was posted
38there were several implementations for memory control. The goal of the
39RFC was to build consensus and agreement for the minimal features required
40for memory control. The first RSS controller was posted by Balbir Singh[2]
41in Feb 2007. Pavel Emelianov [3][4][5] has since posted three versions of the
42RSS controller. At OLS, at the resource management BoF, everyone suggested
43that we handle both page cache and RSS together. Another request was raised
44to allow user space handling of OOM. The current memory controller is
45at version 6; it combines both mapped (RSS) and unmapped Page
46Cache Control [11].
47
482. Memory Control
49
50Memory is a unique resource in the sense that it is present in a limited
51amount. If a task requires a lot of CPU processing, the task can spread
52its processing over a period of hours, days, months or years, but with
53memory, the same physical memory needs to be reused to accomplish the task.
54
55The memory controller implementation has been divided into phases. These
56are:
57
581. Memory controller
592. mlock(2) controller
603. Kernel user memory accounting and slab control
614. user mappings length controller
62
63The memory controller is the first controller developed.
64
652.1. Design
66
67The core of the design is a counter called the res_counter. The res_counter
68tracks the current memory usage and limit of the group of processes associated
69with the controller. Each cgroup has a memory controller specific data
70structure (mem_cgroup) associated with it.
71
722.2. Accounting
73
74 +--------------------+
75 | mem_cgroup |
76 | (res_counter) |
77 +--------------------+
78 / ^ \
79 / | \
80 +---------------+ | +---------------+
81 | mm_struct | |.... | mm_struct |
82 | | | | |
83 +---------------+ | +---------------+
84 |
85 + --------------+
86 |
87 +---------------+ +------+--------+
88 | page +----------> page_cgroup|
89 | | | |
90 +---------------+ +---------------+
91
92 (Figure 1: Hierarchy of Accounting)
93
94
95Figure 1 shows the important aspects of the controller
96
971. Accounting happens per cgroup
982. Each mm_struct knows about which cgroup it belongs to
993. Each page has a pointer to the page_cgroup, which in turn knows the
100 cgroup it belongs to
101
102The accounting is done as follows: mem_cgroup_charge() is invoked to setup
103the necessary data structures and check if the cgroup that is being charged
104is over its limit. If it is then reclaim is invoked on the cgroup.
105More details can be found in the reclaim section of this document.
106If everything goes well, a page meta-data-structure called page_cgroup is
107allocated and associated with the page. This routine also adds the page to
108the per cgroup LRU.
109
1102.2.1 Accounting details
111
112All mapped pages (RSS) and unmapped user pages (Page Cache) are accounted.
113RSS pages are accounted at the time of page_add_*_rmap() unless they've already
114been accounted for earlier. A file page will be accounted for as Page Cache;
115it's mapped into the page tables of a process, duplicate accounting is carefully
116avoided. Page Cache pages are accounted at the time of add_to_page_cache().
117The corresponding routines that remove a page from the page tables or removes
118a page from Page Cache is used to decrement the accounting counters of the
119cgroup.
120
1212.3 Shared Page Accounting
122
123Shared pages are accounted on the basis of the first touch approach. The
124cgroup that first touches a page is accounted for the page. The principle
125behind this approach is that a cgroup that aggressively uses a shared
126page will eventually get charged for it (once it is uncharged from
127the cgroup that brought it in -- this will happen on memory pressure).
128
1292.4 Reclaim
130
131Each cgroup maintains a per cgroup LRU that consists of an active
132and inactive list. When a cgroup goes over its limit, we first try
133to reclaim memory from the cgroup so as to make space for the new
134pages that the cgroup has touched. If the reclaim is unsuccessful,
135an OOM routine is invoked to select and kill the bulkiest task in the
136cgroup.
137
138The reclaim algorithm has not been modified for cgroups, except that
139pages that are selected for reclaiming come from the per cgroup LRU
140list.
141
1422. Locking
143
144The memory controller uses the following hierarchy
145
1461. zone->lru_lock is used for selecting pages to be isolated
1472. mem->lru_lock protects the per cgroup LRU
1483. lock_page_cgroup() is used to protect page->page_cgroup
149
1503. User Interface
151
1520. Configuration
153
154a. Enable CONFIG_CGROUPS
155b. Enable CONFIG_RESOURCE_COUNTERS
156c. Enable CONFIG_CGROUP_MEM_CONT
157
1581. Prepare the cgroups
159# mkdir -p /cgroups
160# mount -t cgroup none /cgroups -o memory
161
1622. Make the new group and move bash into it
163# mkdir /cgroups/0
164# echo $$ > /cgroups/0/tasks
165
166Since now we're in the 0 cgroup,
167We can alter the memory limit:
168# echo -n 6000 > /cgroups/0/memory.limit
169
170We can check the usage:
171# cat /cgroups/0/memory.usage
17225
173
174The memory.failcnt field gives the number of times that the cgroup limit was
175exceeded.
176
1774. Testing
178
179Balbir posted lmbench, AIM9, LTP and vmmstress results [10] and [11].
180Apart from that v6 has been tested with several applications and regular
181daily use. The controller has also been tested on the PPC64, x86_64 and
182UML platforms.
183
1844.1 Troubleshooting
185
186Sometimes a user might find that the application under a cgroup is
187terminated. There are several causes for this:
188
1891. The cgroup limit is too low (just too low to do anything useful)
1902. The user is using anonymous memory and swap is turned off or too low
191
192A sync followed by echo 1 > /proc/sys/vm/drop_caches will help get rid of
193some of the pages cached in the cgroup (page cache pages).
194
1954.2 Task migration
196
197When a task migrates from one cgroup to another, it's charge is not
198carried forward. The pages allocated from the original cgroup still
199remain charged to it, the charge is dropped when the page is freed or
200reclaimed.
201
2024.3 Removing a cgroup
203
204A cgroup can be removed by rmdir, but as discussed in sections 4.1 and 4.2, a
205cgroup might have some charge associated with it, even though all
206tasks have migrated away from it. If some pages are still left, after following
207the steps listed in sections 4.1 and 4.2, check the Swap Cache usage in
208/proc/meminfo to see if the Swap Cache usage is showing up in the
209cgroups memory.usage counter. A simple test of swapoff -a and swapon -a
210should free any pending Swap Cache usage.
211
2124.4 Choosing what to account -- Page Cache (unmapped) vs RSS (mapped)?
213
214The type of memory accounted by the cgroup can be limited to just
215mapped pages by writing "1" to memory.control_type field
216
217echo -n 1 > memory.control_type
218
2195. TODO
220
2211. Add support for accounting huge pages (as a separate controller)
2222. Improve the user interface to accept/display memory limits in KB or MB
223 rather than pages (since page sizes can differ across platforms/machines).
2243. Make cgroup lists per-zone
2254. Make per-cgroup scanner reclaim not-shared pages first
2265. Teach controller to account for shared-pages
2276. Start reclamation when the limit is lowered
2287. Start reclamation in the background when the limit is
229 not yet hit but the usage is getting closer
2308. Create per zone LRU lists per cgroup
231
232Summary
233
234Overall, the memory controller has been a stable controller and has been
235commented and discussed quite extensively in the community.
236
237References
238
2391. Singh, Balbir. RFC: Memory Controller, http://lwn.net/Articles/206697/
2402. Singh, Balbir. Memory Controller (RSS Control),
241 http://lwn.net/Articles/222762/
2423. Emelianov, Pavel. Resource controllers based on process cgroups
243 http://lkml.org/lkml/2007/3/6/198
2444. Emelianov, Pavel. RSS controller based on process cgroups (v2)
245 http://lkml.org/lkml/2007/4/9/74
2465. Emelianov, Pavel. RSS controller based on process cgroups (v3)
247 http://lkml.org/lkml/2007/5/30/244
2486. Menage, Paul. Control Groups v10, http://lwn.net/Articles/236032/
2497. Vaidyanathan, Srinivasan, Control Groups: Pagecache accounting and control
250 subsystem (v3), http://lwn.net/Articles/235534/
2518. Singh, Balbir. RSS controller V2 test results (lmbench),
252 http://lkml.org/lkml/2007/5/17/232
2539. Singh, Balbir. RSS controller V2 AIM9 results
254 http://lkml.org/lkml/2007/5/18/1
25510. Singh, Balbir. Memory controller v6 results,
256 http://lkml.org/lkml/2007/8/19/36
25711. Singh, Balbir. Memory controller v6, http://lkml.org/lkml/2007/8/17/69
25812. Corbet, Jonathan, Controlling memory use in cgroups,
259 http://lwn.net/Articles/243795/