memcg_test.txt 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. Memory Resource Controller(Memcg) Implementation Memo.
  2. Last Updated: 2010/2
  3. Base Kernel Version: based on 2.6.33-rc7-mm(candidate for 34).
  4. Because VM is getting complex (one of reasons is memcg...), memcg's behavior
  5. is complex. This is a document for memcg's internal behavior.
  6. Please note that implementation details can be changed.
  7. (*) Topics on API should be in Documentation/cgroups/memory.txt)
  8. 0. How to record usage ?
  9. 2 objects are used.
  10. page_cgroup ....an object per page.
  11. Allocated at boot or memory hotplug. Freed at memory hot removal.
  12. swap_cgroup ... an entry per swp_entry.
  13. Allocated at swapon(). Freed at swapoff().
  14. The page_cgroup has USED bit and double count against a page_cgroup never
  15. occurs. swap_cgroup is used only when a charged page is swapped-out.
  16. 1. Charge
  17. a page/swp_entry may be charged (usage += PAGE_SIZE) at
  18. mem_cgroup_newpage_charge()
  19. Called at new page fault and Copy-On-Write.
  20. mem_cgroup_try_charge_swapin()
  21. Called at do_swap_page() (page fault on swap entry) and swapoff.
  22. Followed by charge-commit-cancel protocol. (With swap accounting)
  23. At commit, a charge recorded in swap_cgroup is removed.
  24. mem_cgroup_cache_charge()
  25. Called at add_to_page_cache()
  26. mem_cgroup_cache_charge_swapin()
  27. Called at shmem's swapin.
  28. mem_cgroup_prepare_migration()
  29. Called before migration. "extra" charge is done and followed by
  30. charge-commit-cancel protocol.
  31. At commit, charge against oldpage or newpage will be committed.
  32. 2. Uncharge
  33. a page/swp_entry may be uncharged (usage -= PAGE_SIZE) by
  34. mem_cgroup_uncharge_page()
  35. Called when an anonymous page is fully unmapped. I.e., mapcount goes
  36. to 0. If the page is SwapCache, uncharge is delayed until
  37. mem_cgroup_uncharge_swapcache().
  38. mem_cgroup_uncharge_cache_page()
  39. Called when a page-cache is deleted from radix-tree. If the page is
  40. SwapCache, uncharge is delayed until mem_cgroup_uncharge_swapcache().
  41. mem_cgroup_uncharge_swapcache()
  42. Called when SwapCache is removed from radix-tree. The charge itself
  43. is moved to swap_cgroup. (If mem+swap controller is disabled, no
  44. charge to swap occurs.)
  45. mem_cgroup_uncharge_swap()
  46. Called when swp_entry's refcnt goes down to 0. A charge against swap
  47. disappears.
  48. mem_cgroup_end_migration(old, new)
  49. At success of migration old is uncharged (if necessary), a charge
  50. to new page is committed. At failure, charge to old page is committed.
  51. 3. charge-commit-cancel
  52. In some case, we can't know this "charge" is valid or not at charging
  53. (because of races).
  54. To handle such case, there are charge-commit-cancel functions.
  55. mem_cgroup_try_charge_XXX
  56. mem_cgroup_commit_charge_XXX
  57. mem_cgroup_cancel_charge_XXX
  58. these are used in swap-in and migration.
  59. At try_charge(), there are no flags to say "this page is charged".
  60. at this point, usage += PAGE_SIZE.
  61. At commit(), the function checks the page should be charged or not
  62. and set flags or avoid charging.(usage -= PAGE_SIZE)
  63. At cancel(), simply usage -= PAGE_SIZE.
  64. Under below explanation, we assume CONFIG_MEM_RES_CTRL_SWAP=y.
  65. 4. Anonymous
  66. Anonymous page is newly allocated at
  67. - page fault into MAP_ANONYMOUS mapping.
  68. - Copy-On-Write.
  69. It is charged right after it's allocated before doing any page table
  70. related operations. Of course, it's uncharged when another page is used
  71. for the fault address.
  72. At freeing anonymous page (by exit() or munmap()), zap_pte() is called
  73. and pages for ptes are freed one by one.(see mm/memory.c). Uncharges
  74. are done at page_remove_rmap() when page_mapcount() goes down to 0.
  75. Another page freeing is by page-reclaim (vmscan.c) and anonymous
  76. pages are swapped out. In this case, the page is marked as
  77. PageSwapCache(). uncharge() routine doesn't uncharge the page marked
  78. as SwapCache(). It's delayed until __delete_from_swap_cache().
  79. 4.1 Swap-in.
  80. At swap-in, the page is taken from swap-cache. There are 2 cases.
  81. (a) If the SwapCache is newly allocated and read, it has no charges.
  82. (b) If the SwapCache has been mapped by processes, it has been
  83. charged already.
  84. This swap-in is one of the most complicated work. In do_swap_page(),
  85. following events occur when pte is unchanged.
  86. (1) the page (SwapCache) is looked up.
  87. (2) lock_page()
  88. (3) try_charge_swapin()
  89. (4) reuse_swap_page() (may call delete_swap_cache())
  90. (5) commit_charge_swapin()
  91. (6) swap_free().
  92. Considering following situation for example.
  93. (A) The page has not been charged before (2) and reuse_swap_page()
  94. doesn't call delete_from_swap_cache().
  95. (B) The page has not been charged before (2) and reuse_swap_page()
  96. calls delete_from_swap_cache().
  97. (C) The page has been charged before (2) and reuse_swap_page() doesn't
  98. call delete_from_swap_cache().
  99. (D) The page has been charged before (2) and reuse_swap_page() calls
  100. delete_from_swap_cache().
  101. memory.usage/memsw.usage changes to this page/swp_entry will be
  102. Case (A) (B) (C) (D)
  103. Event
  104. Before (2) 0/ 1 0/ 1 1/ 1 1/ 1
  105. ===========================================
  106. (3) +1/+1 +1/+1 +1/+1 +1/+1
  107. (4) - 0/ 0 - -1/ 0
  108. (5) 0/-1 0/ 0 -1/-1 0/ 0
  109. (6) - 0/-1 - 0/-1
  110. ===========================================
  111. Result 1/ 1 1/ 1 1/ 1 1/ 1
  112. In any cases, charges to this page should be 1/ 1.
  113. 4.2 Swap-out.
  114. At swap-out, typical state transition is below.
  115. (a) add to swap cache. (marked as SwapCache)
  116. swp_entry's refcnt += 1.
  117. (b) fully unmapped.
  118. swp_entry's refcnt += # of ptes.
  119. (c) write back to swap.
  120. (d) delete from swap cache. (remove from SwapCache)
  121. swp_entry's refcnt -= 1.
  122. At (b), the page is marked as SwapCache and not uncharged.
  123. At (d), the page is removed from SwapCache and a charge in page_cgroup
  124. is moved to swap_cgroup.
  125. Finally, at task exit,
  126. (e) zap_pte() is called and swp_entry's refcnt -=1 -> 0.
  127. Here, a charge in swap_cgroup disappears.
  128. 5. Page Cache
  129. Page Cache is charged at
  130. - add_to_page_cache_locked().
  131. uncharged at
  132. - __remove_from_page_cache().
  133. The logic is very clear. (About migration, see below)
  134. Note: __remove_from_page_cache() is called by remove_from_page_cache()
  135. and __remove_mapping().
  136. 6. Shmem(tmpfs) Page Cache
  137. Memcg's charge/uncharge have special handlers of shmem. The best way
  138. to understand shmem's page state transition is to read mm/shmem.c.
  139. But brief explanation of the behavior of memcg around shmem will be
  140. helpful to understand the logic.
  141. Shmem's page (just leaf page, not direct/indirect block) can be on
  142. - radix-tree of shmem's inode.
  143. - SwapCache.
  144. - Both on radix-tree and SwapCache. This happens at swap-in
  145. and swap-out,
  146. It's charged when...
  147. - A new page is added to shmem's radix-tree.
  148. - A swp page is read. (move a charge from swap_cgroup to page_cgroup)
  149. It's uncharged when
  150. - A page is removed from radix-tree and not SwapCache.
  151. - When SwapCache is removed, a charge is moved to swap_cgroup.
  152. - When swp_entry's refcnt goes down to 0, a charge in swap_cgroup
  153. disappears.
  154. 7. Page Migration
  155. One of the most complicated functions is page-migration-handler.
  156. Memcg has 2 routines. Assume that we are migrating a page's contents
  157. from OLDPAGE to NEWPAGE.
  158. Usual migration logic is..
  159. (a) remove the page from LRU.
  160. (b) allocate NEWPAGE (migration target)
  161. (c) lock by lock_page().
  162. (d) unmap all mappings.
  163. (e-1) If necessary, replace entry in radix-tree.
  164. (e-2) move contents of a page.
  165. (f) map all mappings again.
  166. (g) pushback the page to LRU.
  167. (-) OLDPAGE will be freed.
  168. Before (g), memcg should complete all necessary charge/uncharge to
  169. NEWPAGE/OLDPAGE.
  170. The point is....
  171. - If OLDPAGE is anonymous, all charges will be dropped at (d) because
  172. try_to_unmap() drops all mapcount and the page will not be
  173. SwapCache.
  174. - If OLDPAGE is SwapCache, charges will be kept at (g) because
  175. __delete_from_swap_cache() isn't called at (e-1)
  176. - If OLDPAGE is page-cache, charges will be kept at (g) because
  177. remove_from_swap_cache() isn't called at (e-1)
  178. memcg provides following hooks.
  179. - mem_cgroup_prepare_migration(OLDPAGE)
  180. Called after (b) to account a charge (usage += PAGE_SIZE) against
  181. memcg which OLDPAGE belongs to.
  182. - mem_cgroup_end_migration(OLDPAGE, NEWPAGE)
  183. Called after (f) before (g).
  184. If OLDPAGE is used, commit OLDPAGE again. If OLDPAGE is already
  185. charged, a charge by prepare_migration() is automatically canceled.
  186. If NEWPAGE is used, commit NEWPAGE and uncharge OLDPAGE.
  187. But zap_pte() (by exit or munmap) can be called while migration,
  188. we have to check if OLDPAGE/NEWPAGE is a valid page after commit().
  189. 8. LRU
  190. Each memcg has its own private LRU. Now, its handling is under global
  191. VM's control (means that it's handled under global zone->lru_lock).
  192. Almost all routines around memcg's LRU is called by global LRU's
  193. list management functions under zone->lru_lock().
  194. A special function is mem_cgroup_isolate_pages(). This scans
  195. memcg's private LRU and call __isolate_lru_page() to extract a page
  196. from LRU.
  197. (By __isolate_lru_page(), the page is removed from both of global and
  198. private LRU.)
  199. 9. Typical Tests.
  200. Tests for racy cases.
  201. 9.1 Small limit to memcg.
  202. When you do test to do racy case, it's good test to set memcg's limit
  203. to be very small rather than GB. Many races found in the test under
  204. xKB or xxMB limits.
  205. (Memory behavior under GB and Memory behavior under MB shows very
  206. different situation.)
  207. 9.2 Shmem
  208. Historically, memcg's shmem handling was poor and we saw some amount
  209. of troubles here. This is because shmem is page-cache but can be
  210. SwapCache. Test with shmem/tmpfs is always good test.
  211. 9.3 Migration
  212. For NUMA, migration is an another special case. To do easy test, cpuset
  213. is useful. Following is a sample script to do migration.
  214. mount -t cgroup -o cpuset none /opt/cpuset
  215. mkdir /opt/cpuset/01
  216. echo 1 > /opt/cpuset/01/cpuset.cpus
  217. echo 0 > /opt/cpuset/01/cpuset.mems
  218. echo 1 > /opt/cpuset/01/cpuset.memory_migrate
  219. mkdir /opt/cpuset/02
  220. echo 1 > /opt/cpuset/02/cpuset.cpus
  221. echo 1 > /opt/cpuset/02/cpuset.mems
  222. echo 1 > /opt/cpuset/02/cpuset.memory_migrate
  223. In above set, when you moves a task from 01 to 02, page migration to
  224. node 0 to node 1 will occur. Following is a script to migrate all
  225. under cpuset.
  226. --
  227. move_task()
  228. {
  229. for pid in $1
  230. do
  231. /bin/echo $pid >$2/tasks 2>/dev/null
  232. echo -n $pid
  233. echo -n " "
  234. done
  235. echo END
  236. }
  237. G1_TASK=`cat ${G1}/tasks`
  238. G2_TASK=`cat ${G2}/tasks`
  239. move_task "${G1_TASK}" ${G2} &
  240. --
  241. 9.4 Memory hotplug.
  242. memory hotplug test is one of good test.
  243. to offline memory, do following.
  244. # echo offline > /sys/devices/system/memory/memoryXXX/state
  245. (XXX is the place of memory)
  246. This is an easy way to test page migration, too.
  247. 9.5 mkdir/rmdir
  248. When using hierarchy, mkdir/rmdir test should be done.
  249. Use tests like the following.
  250. echo 1 >/opt/cgroup/01/memory/use_hierarchy
  251. mkdir /opt/cgroup/01/child_a
  252. mkdir /opt/cgroup/01/child_b
  253. set limit to 01.
  254. add limit to 01/child_b
  255. run jobs under child_a and child_b
  256. create/delete following groups at random while jobs are running.
  257. /opt/cgroup/01/child_a/child_aa
  258. /opt/cgroup/01/child_b/child_bb
  259. /opt/cgroup/01/child_c
  260. running new jobs in new group is also good.
  261. 9.6 Mount with other subsystems.
  262. Mounting with other subsystems is a good test because there is a
  263. race and lock dependency with other cgroup subsystems.
  264. example)
  265. # mount -t cgroup none /cgroup -o cpuset,memory,cpu,devices
  266. and do task move, mkdir, rmdir etc...under this.
  267. 9.7 swapoff.
  268. Besides management of swap is one of complicated parts of memcg,
  269. call path of swap-in at swapoff is not same as usual swap-in path..
  270. It's worth to be tested explicitly.
  271. For example, test like following is good.
  272. (Shell-A)
  273. # mount -t cgroup none /cgroup -o memory
  274. # mkdir /cgroup/test
  275. # echo 40M > /cgroup/test/memory.limit_in_bytes
  276. # echo 0 > /cgroup/test/tasks
  277. Run malloc(100M) program under this. You'll see 60M of swaps.
  278. (Shell-B)
  279. # move all tasks in /cgroup/test to /cgroup
  280. # /sbin/swapoff -a
  281. # rmdir /cgroup/test
  282. # kill malloc task.
  283. Of course, tmpfs v.s. swapoff test should be tested, too.
  284. 9.8 OOM-Killer
  285. Out-of-memory caused by memcg's limit will kill tasks under
  286. the memcg. When hierarchy is used, a task under hierarchy
  287. will be killed by the kernel.
  288. In this case, panic_on_oom shouldn't be invoked and tasks
  289. in other groups shouldn't be killed.
  290. It's not difficult to cause OOM under memcg as following.
  291. Case A) when you can swapoff
  292. #swapoff -a
  293. #echo 50M > /memory.limit_in_bytes
  294. run 51M of malloc
  295. Case B) when you use mem+swap limitation.
  296. #echo 50M > memory.limit_in_bytes
  297. #echo 50M > memory.memsw.limit_in_bytes
  298. run 51M of malloc
  299. 9.9 Move charges at task migration
  300. Charges associated with a task can be moved along with task migration.
  301. (Shell-A)
  302. #mkdir /cgroup/A
  303. #echo $$ >/cgroup/A/tasks
  304. run some programs which uses some amount of memory in /cgroup/A.
  305. (Shell-B)
  306. #mkdir /cgroup/B
  307. #echo 1 >/cgroup/B/memory.move_charge_at_immigrate
  308. #echo "pid of the program running in group A" >/cgroup/B/tasks
  309. You can see charges have been moved by reading *.usage_in_bytes or
  310. memory.stat of both A and B.
  311. See 8.2 of Documentation/cgroups/memory.txt to see what value should be
  312. written to move_charge_at_immigrate.
  313. 9.10 Memory thresholds
  314. Memory controller implements memory thresholds using cgroups notification
  315. API. You can use Documentation/cgroups/cgroup_event_listener.c to test
  316. it.
  317. (Shell-A) Create cgroup and run event listener
  318. # mkdir /cgroup/A
  319. # ./cgroup_event_listener /cgroup/A/memory.usage_in_bytes 5M
  320. (Shell-B) Add task to cgroup and try to allocate and free memory
  321. # echo $$ >/cgroup/A/tasks
  322. # a="$(dd if=/dev/zero bs=1M count=10)"
  323. # a=
  324. You will see message from cgroup_event_listener every time you cross
  325. the thresholds.
  326. Use /cgroup/A/memory.memsw.usage_in_bytes to test memsw thresholds.
  327. It's good idea to test root cgroup as well.