2009-01-08 02:08:27 +00:00
|
|
|
Memory Resource Controller(Memcg) Implementation Memo.
|
2010-03-10 23:22:31 +00:00
|
|
|
Last Updated: 2010/2
|
|
|
|
Base Kernel Version: based on 2.6.33-rc7-mm(candidate for 34).
|
2009-01-08 02:08:27 +00:00
|
|
|
|
|
|
|
Because VM is getting complex (one of reasons is memcg...), memcg's behavior
|
|
|
|
is complex. This is a document for memcg's internal behavior.
|
|
|
|
Please note that implementation details can be changed.
|
|
|
|
|
2016-08-02 14:23:57 +00:00
|
|
|
(*) Topics on API should be in Documentation/cgroup-v1/memory.txt)
|
2009-01-08 02:08:27 +00:00
|
|
|
|
|
|
|
0. How to record usage ?
|
|
|
|
2 objects are used.
|
|
|
|
|
|
|
|
page_cgroup ....an object per page.
|
|
|
|
Allocated at boot or memory hotplug. Freed at memory hot removal.
|
|
|
|
|
|
|
|
swap_cgroup ... an entry per swp_entry.
|
|
|
|
Allocated at swapon(). Freed at swapoff().
|
|
|
|
|
|
|
|
The page_cgroup has USED bit and double count against a page_cgroup never
|
|
|
|
occurs. swap_cgroup is used only when a charged page is swapped-out.
|
|
|
|
|
|
|
|
1. Charge
|
|
|
|
|
|
|
|
a page/swp_entry may be charged (usage += PAGE_SIZE) at
|
|
|
|
|
mm: memcontrol: rewrite charge API
These patches rework memcg charge lifetime to integrate more naturally
with the lifetime of user pages. This drastically simplifies the code and
reduces charging and uncharging overhead. The most expensive part of
charging and uncharging is the page_cgroup bit spinlock, which is removed
entirely after this series.
Here are the top-10 profile entries of a stress test that reads a 128G
sparse file on a freshly booted box, without even a dedicated cgroup (i.e.
executing in the root memcg). Before:
15.36% cat [kernel.kallsyms] [k] copy_user_generic_string
13.31% cat [kernel.kallsyms] [k] memset
11.48% cat [kernel.kallsyms] [k] do_mpage_readpage
4.23% cat [kernel.kallsyms] [k] get_page_from_freelist
2.38% cat [kernel.kallsyms] [k] put_page
2.32% cat [kernel.kallsyms] [k] __mem_cgroup_commit_charge
2.18% kswapd0 [kernel.kallsyms] [k] __mem_cgroup_uncharge_common
1.92% kswapd0 [kernel.kallsyms] [k] shrink_page_list
1.86% cat [kernel.kallsyms] [k] __radix_tree_lookup
1.62% cat [kernel.kallsyms] [k] __pagevec_lru_add_fn
After:
15.67% cat [kernel.kallsyms] [k] copy_user_generic_string
13.48% cat [kernel.kallsyms] [k] memset
11.42% cat [kernel.kallsyms] [k] do_mpage_readpage
3.98% cat [kernel.kallsyms] [k] get_page_from_freelist
2.46% cat [kernel.kallsyms] [k] put_page
2.13% kswapd0 [kernel.kallsyms] [k] shrink_page_list
1.88% cat [kernel.kallsyms] [k] __radix_tree_lookup
1.67% cat [kernel.kallsyms] [k] __pagevec_lru_add_fn
1.39% kswapd0 [kernel.kallsyms] [k] free_pcppages_bulk
1.30% cat [kernel.kallsyms] [k] kfree
As you can see, the memcg footprint has shrunk quite a bit.
text data bss dec hex filename
37970 9892 400 48262 bc86 mm/memcontrol.o.old
35239 9892 400 45531 b1db mm/memcontrol.o
This patch (of 4):
The memcg charge API charges pages before they are rmapped - i.e. have an
actual "type" - and so every callsite needs its own set of charge and
uncharge functions to know what type is being operated on. Worse,
uncharge has to happen from a context that is still type-specific, rather
than at the end of the page's lifetime with exclusive access, and so
requires a lot of synchronization.
Rewrite the charge API to provide a generic set of try_charge(),
commit_charge() and cancel_charge() transaction operations, much like
what's currently done for swap-in:
mem_cgroup_try_charge() attempts to reserve a charge, reclaiming
pages from the memcg if necessary.
mem_cgroup_commit_charge() commits the page to the charge once it
has a valid page->mapping and PageAnon() reliably tells the type.
mem_cgroup_cancel_charge() aborts the transaction.
This reduces the charge API and enables subsequent patches to
drastically simplify uncharging.
As pages need to be committed after rmap is established but before they
are added to the LRU, page_add_new_anon_rmap() must stop doing LRU
additions again. Revive lru_cache_add_active_or_unevictable().
[hughd@google.com: fix shmem_unuse]
[hughd@google.com: Add comments on the private use of -EAGAIN]
Signed-off-by: Johannes Weiner <hannes@cmpxchg.org>
Acked-by: Michal Hocko <mhocko@suse.cz>
Cc: Tejun Heo <tj@kernel.org>
Cc: Vladimir Davydov <vdavydov@parallels.com>
Signed-off-by: Hugh Dickins <hughd@google.com>
Cc: Naoya Horiguchi <n-horiguchi@ah.jp.nec.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2014-08-08 21:19:20 +00:00
|
|
|
mem_cgroup_try_charge()
|
2009-01-08 02:08:27 +00:00
|
|
|
|
|
|
|
2. Uncharge
|
|
|
|
a page/swp_entry may be uncharged (usage -= PAGE_SIZE) by
|
|
|
|
|
mm: memcontrol: rewrite uncharge API
The memcg uncharging code that is involved towards the end of a page's
lifetime - truncation, reclaim, swapout, migration - is impressively
complicated and fragile.
Because anonymous and file pages were always charged before they had their
page->mapping established, uncharges had to happen when the page type
could still be known from the context; as in unmap for anonymous, page
cache removal for file and shmem pages, and swap cache truncation for swap
pages. However, these operations happen well before the page is actually
freed, and so a lot of synchronization is necessary:
- Charging, uncharging, page migration, and charge migration all need
to take a per-page bit spinlock as they could race with uncharging.
- Swap cache truncation happens during both swap-in and swap-out, and
possibly repeatedly before the page is actually freed. This means
that the memcg swapout code is called from many contexts that make
no sense and it has to figure out the direction from page state to
make sure memory and memory+swap are always correctly charged.
- On page migration, the old page might be unmapped but then reused,
so memcg code has to prevent untimely uncharging in that case.
Because this code - which should be a simple charge transfer - is so
special-cased, it is not reusable for replace_page_cache().
But now that charged pages always have a page->mapping, introduce
mem_cgroup_uncharge(), which is called after the final put_page(), when we
know for sure that nobody is looking at the page anymore.
For page migration, introduce mem_cgroup_migrate(), which is called after
the migration is successful and the new page is fully rmapped. Because
the old page is no longer uncharged after migration, prevent double
charges by decoupling the page's memcg association (PCG_USED and
pc->mem_cgroup) from the page holding an actual charge. The new bits
PCG_MEM and PCG_MEMSW represent the respective charges and are transferred
to the new page during migration.
mem_cgroup_migrate() is suitable for replace_page_cache() as well,
which gets rid of mem_cgroup_replace_page_cache(). However, care
needs to be taken because both the source and the target page can
already be charged and on the LRU when fuse is splicing: grab the page
lock on the charge moving side to prevent changing pc->mem_cgroup of a
page under migration. Also, the lruvecs of both pages change as we
uncharge the old and charge the new during migration, and putback may
race with us, so grab the lru lock and isolate the pages iff on LRU to
prevent races and ensure the pages are on the right lruvec afterward.
Swap accounting is massively simplified: because the page is no longer
uncharged as early as swap cache deletion, a new mem_cgroup_swapout() can
transfer the page's memory+swap charge (PCG_MEMSW) to the swap entry
before the final put_page() in page reclaim.
Finally, page_cgroup changes are now protected by whatever protection the
page itself offers: anonymous pages are charged under the page table lock,
whereas page cache insertions, swapin, and migration hold the page lock.
Uncharging happens under full exclusion with no outstanding references.
Charging and uncharging also ensure that the page is off-LRU, which
serializes against charge migration. Remove the very costly page_cgroup
lock and set pc->flags non-atomically.
[mhocko@suse.cz: mem_cgroup_charge_statistics needs preempt_disable]
[vdavydov@parallels.com: fix flags definition]
Signed-off-by: Johannes Weiner <hannes@cmpxchg.org>
Cc: Hugh Dickins <hughd@google.com>
Cc: Tejun Heo <tj@kernel.org>
Cc: Vladimir Davydov <vdavydov@parallels.com>
Tested-by: Jet Chen <jet.chen@intel.com>
Acked-by: Michal Hocko <mhocko@suse.cz>
Tested-by: Felipe Balbi <balbi@ti.com>
Signed-off-by: Vladimir Davydov <vdavydov@parallels.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2014-08-08 21:19:22 +00:00
|
|
|
mem_cgroup_uncharge()
|
|
|
|
Called when a page's refcount goes down to 0.
|
2009-01-08 02:08:27 +00:00
|
|
|
|
|
|
|
mem_cgroup_uncharge_swap()
|
|
|
|
Called when swp_entry's refcnt goes down to 0. A charge against swap
|
|
|
|
disappears.
|
|
|
|
|
|
|
|
3. charge-commit-cancel
|
mm: memcontrol: rewrite charge API
These patches rework memcg charge lifetime to integrate more naturally
with the lifetime of user pages. This drastically simplifies the code and
reduces charging and uncharging overhead. The most expensive part of
charging and uncharging is the page_cgroup bit spinlock, which is removed
entirely after this series.
Here are the top-10 profile entries of a stress test that reads a 128G
sparse file on a freshly booted box, without even a dedicated cgroup (i.e.
executing in the root memcg). Before:
15.36% cat [kernel.kallsyms] [k] copy_user_generic_string
13.31% cat [kernel.kallsyms] [k] memset
11.48% cat [kernel.kallsyms] [k] do_mpage_readpage
4.23% cat [kernel.kallsyms] [k] get_page_from_freelist
2.38% cat [kernel.kallsyms] [k] put_page
2.32% cat [kernel.kallsyms] [k] __mem_cgroup_commit_charge
2.18% kswapd0 [kernel.kallsyms] [k] __mem_cgroup_uncharge_common
1.92% kswapd0 [kernel.kallsyms] [k] shrink_page_list
1.86% cat [kernel.kallsyms] [k] __radix_tree_lookup
1.62% cat [kernel.kallsyms] [k] __pagevec_lru_add_fn
After:
15.67% cat [kernel.kallsyms] [k] copy_user_generic_string
13.48% cat [kernel.kallsyms] [k] memset
11.42% cat [kernel.kallsyms] [k] do_mpage_readpage
3.98% cat [kernel.kallsyms] [k] get_page_from_freelist
2.46% cat [kernel.kallsyms] [k] put_page
2.13% kswapd0 [kernel.kallsyms] [k] shrink_page_list
1.88% cat [kernel.kallsyms] [k] __radix_tree_lookup
1.67% cat [kernel.kallsyms] [k] __pagevec_lru_add_fn
1.39% kswapd0 [kernel.kallsyms] [k] free_pcppages_bulk
1.30% cat [kernel.kallsyms] [k] kfree
As you can see, the memcg footprint has shrunk quite a bit.
text data bss dec hex filename
37970 9892 400 48262 bc86 mm/memcontrol.o.old
35239 9892 400 45531 b1db mm/memcontrol.o
This patch (of 4):
The memcg charge API charges pages before they are rmapped - i.e. have an
actual "type" - and so every callsite needs its own set of charge and
uncharge functions to know what type is being operated on. Worse,
uncharge has to happen from a context that is still type-specific, rather
than at the end of the page's lifetime with exclusive access, and so
requires a lot of synchronization.
Rewrite the charge API to provide a generic set of try_charge(),
commit_charge() and cancel_charge() transaction operations, much like
what's currently done for swap-in:
mem_cgroup_try_charge() attempts to reserve a charge, reclaiming
pages from the memcg if necessary.
mem_cgroup_commit_charge() commits the page to the charge once it
has a valid page->mapping and PageAnon() reliably tells the type.
mem_cgroup_cancel_charge() aborts the transaction.
This reduces the charge API and enables subsequent patches to
drastically simplify uncharging.
As pages need to be committed after rmap is established but before they
are added to the LRU, page_add_new_anon_rmap() must stop doing LRU
additions again. Revive lru_cache_add_active_or_unevictable().
[hughd@google.com: fix shmem_unuse]
[hughd@google.com: Add comments on the private use of -EAGAIN]
Signed-off-by: Johannes Weiner <hannes@cmpxchg.org>
Acked-by: Michal Hocko <mhocko@suse.cz>
Cc: Tejun Heo <tj@kernel.org>
Cc: Vladimir Davydov <vdavydov@parallels.com>
Signed-off-by: Hugh Dickins <hughd@google.com>
Cc: Naoya Horiguchi <n-horiguchi@ah.jp.nec.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2014-08-08 21:19:20 +00:00
|
|
|
Memcg pages are charged in two steps:
|
|
|
|
mem_cgroup_try_charge()
|
|
|
|
mem_cgroup_commit_charge() or mem_cgroup_cancel_charge()
|
2009-01-08 02:08:27 +00:00
|
|
|
|
|
|
|
At try_charge(), there are no flags to say "this page is charged".
|
|
|
|
at this point, usage += PAGE_SIZE.
|
|
|
|
|
mm: memcontrol: rewrite charge API
These patches rework memcg charge lifetime to integrate more naturally
with the lifetime of user pages. This drastically simplifies the code and
reduces charging and uncharging overhead. The most expensive part of
charging and uncharging is the page_cgroup bit spinlock, which is removed
entirely after this series.
Here are the top-10 profile entries of a stress test that reads a 128G
sparse file on a freshly booted box, without even a dedicated cgroup (i.e.
executing in the root memcg). Before:
15.36% cat [kernel.kallsyms] [k] copy_user_generic_string
13.31% cat [kernel.kallsyms] [k] memset
11.48% cat [kernel.kallsyms] [k] do_mpage_readpage
4.23% cat [kernel.kallsyms] [k] get_page_from_freelist
2.38% cat [kernel.kallsyms] [k] put_page
2.32% cat [kernel.kallsyms] [k] __mem_cgroup_commit_charge
2.18% kswapd0 [kernel.kallsyms] [k] __mem_cgroup_uncharge_common
1.92% kswapd0 [kernel.kallsyms] [k] shrink_page_list
1.86% cat [kernel.kallsyms] [k] __radix_tree_lookup
1.62% cat [kernel.kallsyms] [k] __pagevec_lru_add_fn
After:
15.67% cat [kernel.kallsyms] [k] copy_user_generic_string
13.48% cat [kernel.kallsyms] [k] memset
11.42% cat [kernel.kallsyms] [k] do_mpage_readpage
3.98% cat [kernel.kallsyms] [k] get_page_from_freelist
2.46% cat [kernel.kallsyms] [k] put_page
2.13% kswapd0 [kernel.kallsyms] [k] shrink_page_list
1.88% cat [kernel.kallsyms] [k] __radix_tree_lookup
1.67% cat [kernel.kallsyms] [k] __pagevec_lru_add_fn
1.39% kswapd0 [kernel.kallsyms] [k] free_pcppages_bulk
1.30% cat [kernel.kallsyms] [k] kfree
As you can see, the memcg footprint has shrunk quite a bit.
text data bss dec hex filename
37970 9892 400 48262 bc86 mm/memcontrol.o.old
35239 9892 400 45531 b1db mm/memcontrol.o
This patch (of 4):
The memcg charge API charges pages before they are rmapped - i.e. have an
actual "type" - and so every callsite needs its own set of charge and
uncharge functions to know what type is being operated on. Worse,
uncharge has to happen from a context that is still type-specific, rather
than at the end of the page's lifetime with exclusive access, and so
requires a lot of synchronization.
Rewrite the charge API to provide a generic set of try_charge(),
commit_charge() and cancel_charge() transaction operations, much like
what's currently done for swap-in:
mem_cgroup_try_charge() attempts to reserve a charge, reclaiming
pages from the memcg if necessary.
mem_cgroup_commit_charge() commits the page to the charge once it
has a valid page->mapping and PageAnon() reliably tells the type.
mem_cgroup_cancel_charge() aborts the transaction.
This reduces the charge API and enables subsequent patches to
drastically simplify uncharging.
As pages need to be committed after rmap is established but before they
are added to the LRU, page_add_new_anon_rmap() must stop doing LRU
additions again. Revive lru_cache_add_active_or_unevictable().
[hughd@google.com: fix shmem_unuse]
[hughd@google.com: Add comments on the private use of -EAGAIN]
Signed-off-by: Johannes Weiner <hannes@cmpxchg.org>
Acked-by: Michal Hocko <mhocko@suse.cz>
Cc: Tejun Heo <tj@kernel.org>
Cc: Vladimir Davydov <vdavydov@parallels.com>
Signed-off-by: Hugh Dickins <hughd@google.com>
Cc: Naoya Horiguchi <n-horiguchi@ah.jp.nec.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2014-08-08 21:19:20 +00:00
|
|
|
At commit(), the page is associated with the memcg.
|
2009-01-08 02:08:27 +00:00
|
|
|
|
|
|
|
At cancel(), simply usage -= PAGE_SIZE.
|
|
|
|
|
|
|
|
Under below explanation, we assume CONFIG_MEM_RES_CTRL_SWAP=y.
|
|
|
|
|
|
|
|
4. Anonymous
|
|
|
|
Anonymous page is newly allocated at
|
|
|
|
- page fault into MAP_ANONYMOUS mapping.
|
|
|
|
- Copy-On-Write.
|
|
|
|
|
|
|
|
4.1 Swap-in.
|
|
|
|
At swap-in, the page is taken from swap-cache. There are 2 cases.
|
|
|
|
|
|
|
|
(a) If the SwapCache is newly allocated and read, it has no charges.
|
|
|
|
(b) If the SwapCache has been mapped by processes, it has been
|
|
|
|
charged already.
|
|
|
|
|
|
|
|
4.2 Swap-out.
|
|
|
|
At swap-out, typical state transition is below.
|
|
|
|
|
|
|
|
(a) add to swap cache. (marked as SwapCache)
|
|
|
|
swp_entry's refcnt += 1.
|
|
|
|
(b) fully unmapped.
|
|
|
|
swp_entry's refcnt += # of ptes.
|
|
|
|
(c) write back to swap.
|
|
|
|
(d) delete from swap cache. (remove from SwapCache)
|
|
|
|
swp_entry's refcnt -= 1.
|
|
|
|
|
|
|
|
|
|
|
|
Finally, at task exit,
|
|
|
|
(e) zap_pte() is called and swp_entry's refcnt -=1 -> 0.
|
|
|
|
|
|
|
|
5. Page Cache
|
|
|
|
Page Cache is charged at
|
|
|
|
- add_to_page_cache_locked().
|
|
|
|
|
|
|
|
The logic is very clear. (About migration, see below)
|
|
|
|
Note: __remove_from_page_cache() is called by remove_from_page_cache()
|
|
|
|
and __remove_mapping().
|
|
|
|
|
|
|
|
6. Shmem(tmpfs) Page Cache
|
mm: memcontrol: rewrite uncharge API
The memcg uncharging code that is involved towards the end of a page's
lifetime - truncation, reclaim, swapout, migration - is impressively
complicated and fragile.
Because anonymous and file pages were always charged before they had their
page->mapping established, uncharges had to happen when the page type
could still be known from the context; as in unmap for anonymous, page
cache removal for file and shmem pages, and swap cache truncation for swap
pages. However, these operations happen well before the page is actually
freed, and so a lot of synchronization is necessary:
- Charging, uncharging, page migration, and charge migration all need
to take a per-page bit spinlock as they could race with uncharging.
- Swap cache truncation happens during both swap-in and swap-out, and
possibly repeatedly before the page is actually freed. This means
that the memcg swapout code is called from many contexts that make
no sense and it has to figure out the direction from page state to
make sure memory and memory+swap are always correctly charged.
- On page migration, the old page might be unmapped but then reused,
so memcg code has to prevent untimely uncharging in that case.
Because this code - which should be a simple charge transfer - is so
special-cased, it is not reusable for replace_page_cache().
But now that charged pages always have a page->mapping, introduce
mem_cgroup_uncharge(), which is called after the final put_page(), when we
know for sure that nobody is looking at the page anymore.
For page migration, introduce mem_cgroup_migrate(), which is called after
the migration is successful and the new page is fully rmapped. Because
the old page is no longer uncharged after migration, prevent double
charges by decoupling the page's memcg association (PCG_USED and
pc->mem_cgroup) from the page holding an actual charge. The new bits
PCG_MEM and PCG_MEMSW represent the respective charges and are transferred
to the new page during migration.
mem_cgroup_migrate() is suitable for replace_page_cache() as well,
which gets rid of mem_cgroup_replace_page_cache(). However, care
needs to be taken because both the source and the target page can
already be charged and on the LRU when fuse is splicing: grab the page
lock on the charge moving side to prevent changing pc->mem_cgroup of a
page under migration. Also, the lruvecs of both pages change as we
uncharge the old and charge the new during migration, and putback may
race with us, so grab the lru lock and isolate the pages iff on LRU to
prevent races and ensure the pages are on the right lruvec afterward.
Swap accounting is massively simplified: because the page is no longer
uncharged as early as swap cache deletion, a new mem_cgroup_swapout() can
transfer the page's memory+swap charge (PCG_MEMSW) to the swap entry
before the final put_page() in page reclaim.
Finally, page_cgroup changes are now protected by whatever protection the
page itself offers: anonymous pages are charged under the page table lock,
whereas page cache insertions, swapin, and migration hold the page lock.
Uncharging happens under full exclusion with no outstanding references.
Charging and uncharging also ensure that the page is off-LRU, which
serializes against charge migration. Remove the very costly page_cgroup
lock and set pc->flags non-atomically.
[mhocko@suse.cz: mem_cgroup_charge_statistics needs preempt_disable]
[vdavydov@parallels.com: fix flags definition]
Signed-off-by: Johannes Weiner <hannes@cmpxchg.org>
Cc: Hugh Dickins <hughd@google.com>
Cc: Tejun Heo <tj@kernel.org>
Cc: Vladimir Davydov <vdavydov@parallels.com>
Tested-by: Jet Chen <jet.chen@intel.com>
Acked-by: Michal Hocko <mhocko@suse.cz>
Tested-by: Felipe Balbi <balbi@ti.com>
Signed-off-by: Vladimir Davydov <vdavydov@parallels.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2014-08-08 21:19:22 +00:00
|
|
|
The best way to understand shmem's page state transition is to read
|
|
|
|
mm/shmem.c.
|
2009-01-08 02:08:27 +00:00
|
|
|
But brief explanation of the behavior of memcg around shmem will be
|
|
|
|
helpful to understand the logic.
|
|
|
|
|
|
|
|
Shmem's page (just leaf page, not direct/indirect block) can be on
|
|
|
|
- radix-tree of shmem's inode.
|
|
|
|
- SwapCache.
|
|
|
|
- Both on radix-tree and SwapCache. This happens at swap-in
|
|
|
|
and swap-out,
|
|
|
|
|
|
|
|
It's charged when...
|
|
|
|
- A new page is added to shmem's radix-tree.
|
|
|
|
- A swp page is read. (move a charge from swap_cgroup to page_cgroup)
|
|
|
|
|
|
|
|
7. Page Migration
|
mm: memcontrol: rewrite uncharge API
The memcg uncharging code that is involved towards the end of a page's
lifetime - truncation, reclaim, swapout, migration - is impressively
complicated and fragile.
Because anonymous and file pages were always charged before they had their
page->mapping established, uncharges had to happen when the page type
could still be known from the context; as in unmap for anonymous, page
cache removal for file and shmem pages, and swap cache truncation for swap
pages. However, these operations happen well before the page is actually
freed, and so a lot of synchronization is necessary:
- Charging, uncharging, page migration, and charge migration all need
to take a per-page bit spinlock as they could race with uncharging.
- Swap cache truncation happens during both swap-in and swap-out, and
possibly repeatedly before the page is actually freed. This means
that the memcg swapout code is called from many contexts that make
no sense and it has to figure out the direction from page state to
make sure memory and memory+swap are always correctly charged.
- On page migration, the old page might be unmapped but then reused,
so memcg code has to prevent untimely uncharging in that case.
Because this code - which should be a simple charge transfer - is so
special-cased, it is not reusable for replace_page_cache().
But now that charged pages always have a page->mapping, introduce
mem_cgroup_uncharge(), which is called after the final put_page(), when we
know for sure that nobody is looking at the page anymore.
For page migration, introduce mem_cgroup_migrate(), which is called after
the migration is successful and the new page is fully rmapped. Because
the old page is no longer uncharged after migration, prevent double
charges by decoupling the page's memcg association (PCG_USED and
pc->mem_cgroup) from the page holding an actual charge. The new bits
PCG_MEM and PCG_MEMSW represent the respective charges and are transferred
to the new page during migration.
mem_cgroup_migrate() is suitable for replace_page_cache() as well,
which gets rid of mem_cgroup_replace_page_cache(). However, care
needs to be taken because both the source and the target page can
already be charged and on the LRU when fuse is splicing: grab the page
lock on the charge moving side to prevent changing pc->mem_cgroup of a
page under migration. Also, the lruvecs of both pages change as we
uncharge the old and charge the new during migration, and putback may
race with us, so grab the lru lock and isolate the pages iff on LRU to
prevent races and ensure the pages are on the right lruvec afterward.
Swap accounting is massively simplified: because the page is no longer
uncharged as early as swap cache deletion, a new mem_cgroup_swapout() can
transfer the page's memory+swap charge (PCG_MEMSW) to the swap entry
before the final put_page() in page reclaim.
Finally, page_cgroup changes are now protected by whatever protection the
page itself offers: anonymous pages are charged under the page table lock,
whereas page cache insertions, swapin, and migration hold the page lock.
Uncharging happens under full exclusion with no outstanding references.
Charging and uncharging also ensure that the page is off-LRU, which
serializes against charge migration. Remove the very costly page_cgroup
lock and set pc->flags non-atomically.
[mhocko@suse.cz: mem_cgroup_charge_statistics needs preempt_disable]
[vdavydov@parallels.com: fix flags definition]
Signed-off-by: Johannes Weiner <hannes@cmpxchg.org>
Cc: Hugh Dickins <hughd@google.com>
Cc: Tejun Heo <tj@kernel.org>
Cc: Vladimir Davydov <vdavydov@parallels.com>
Tested-by: Jet Chen <jet.chen@intel.com>
Acked-by: Michal Hocko <mhocko@suse.cz>
Tested-by: Felipe Balbi <balbi@ti.com>
Signed-off-by: Vladimir Davydov <vdavydov@parallels.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2014-08-08 21:19:22 +00:00
|
|
|
|
|
|
|
mem_cgroup_migrate()
|
2009-01-08 02:08:27 +00:00
|
|
|
|
|
|
|
8. LRU
|
2010-04-22 22:08:02 +00:00
|
|
|
Each memcg has its own private LRU. Now, its handling is under global
|
2016-07-28 22:45:28 +00:00
|
|
|
VM's control (means that it's handled under global zone_lru_lock).
|
2009-01-08 02:08:27 +00:00
|
|
|
Almost all routines around memcg's LRU is called by global LRU's
|
2016-07-28 22:45:28 +00:00
|
|
|
list management functions under zone_lru_lock().
|
2009-01-08 02:08:27 +00:00
|
|
|
|
|
|
|
A special function is mem_cgroup_isolate_pages(). This scans
|
|
|
|
memcg's private LRU and call __isolate_lru_page() to extract a page
|
|
|
|
from LRU.
|
|
|
|
(By __isolate_lru_page(), the page is removed from both of global and
|
|
|
|
private LRU.)
|
|
|
|
|
|
|
|
|
|
|
|
9. Typical Tests.
|
|
|
|
|
|
|
|
Tests for racy cases.
|
|
|
|
|
|
|
|
9.1 Small limit to memcg.
|
|
|
|
When you do test to do racy case, it's good test to set memcg's limit
|
|
|
|
to be very small rather than GB. Many races found in the test under
|
|
|
|
xKB or xxMB limits.
|
|
|
|
(Memory behavior under GB and Memory behavior under MB shows very
|
|
|
|
different situation.)
|
|
|
|
|
|
|
|
9.2 Shmem
|
|
|
|
Historically, memcg's shmem handling was poor and we saw some amount
|
|
|
|
of troubles here. This is because shmem is page-cache but can be
|
|
|
|
SwapCache. Test with shmem/tmpfs is always good test.
|
|
|
|
|
|
|
|
9.3 Migration
|
|
|
|
For NUMA, migration is an another special case. To do easy test, cpuset
|
|
|
|
is useful. Following is a sample script to do migration.
|
|
|
|
|
|
|
|
mount -t cgroup -o cpuset none /opt/cpuset
|
|
|
|
|
|
|
|
mkdir /opt/cpuset/01
|
|
|
|
echo 1 > /opt/cpuset/01/cpuset.cpus
|
|
|
|
echo 0 > /opt/cpuset/01/cpuset.mems
|
|
|
|
echo 1 > /opt/cpuset/01/cpuset.memory_migrate
|
|
|
|
mkdir /opt/cpuset/02
|
|
|
|
echo 1 > /opt/cpuset/02/cpuset.cpus
|
|
|
|
echo 1 > /opt/cpuset/02/cpuset.mems
|
|
|
|
echo 1 > /opt/cpuset/02/cpuset.memory_migrate
|
|
|
|
|
|
|
|
In above set, when you moves a task from 01 to 02, page migration to
|
|
|
|
node 0 to node 1 will occur. Following is a script to migrate all
|
|
|
|
under cpuset.
|
|
|
|
--
|
|
|
|
move_task()
|
|
|
|
{
|
|
|
|
for pid in $1
|
|
|
|
do
|
|
|
|
/bin/echo $pid >$2/tasks 2>/dev/null
|
|
|
|
echo -n $pid
|
|
|
|
echo -n " "
|
|
|
|
done
|
|
|
|
echo END
|
|
|
|
}
|
|
|
|
|
|
|
|
G1_TASK=`cat ${G1}/tasks`
|
|
|
|
G2_TASK=`cat ${G2}/tasks`
|
|
|
|
move_task "${G1_TASK}" ${G2} &
|
|
|
|
--
|
|
|
|
9.4 Memory hotplug.
|
|
|
|
memory hotplug test is one of good test.
|
|
|
|
to offline memory, do following.
|
|
|
|
# echo offline > /sys/devices/system/memory/memoryXXX/state
|
|
|
|
(XXX is the place of memory)
|
|
|
|
This is an easy way to test page migration, too.
|
|
|
|
|
|
|
|
9.5 mkdir/rmdir
|
|
|
|
When using hierarchy, mkdir/rmdir test should be done.
|
|
|
|
Use tests like the following.
|
|
|
|
|
|
|
|
echo 1 >/opt/cgroup/01/memory/use_hierarchy
|
|
|
|
mkdir /opt/cgroup/01/child_a
|
|
|
|
mkdir /opt/cgroup/01/child_b
|
|
|
|
|
|
|
|
set limit to 01.
|
|
|
|
add limit to 01/child_b
|
|
|
|
run jobs under child_a and child_b
|
|
|
|
|
|
|
|
create/delete following groups at random while jobs are running.
|
|
|
|
/opt/cgroup/01/child_a/child_aa
|
|
|
|
/opt/cgroup/01/child_b/child_bb
|
|
|
|
/opt/cgroup/01/child_c
|
|
|
|
|
|
|
|
running new jobs in new group is also good.
|
|
|
|
|
|
|
|
9.6 Mount with other subsystems.
|
|
|
|
Mounting with other subsystems is a good test because there is a
|
|
|
|
race and lock dependency with other cgroup subsystems.
|
|
|
|
|
|
|
|
example)
|
2010-03-10 23:22:37 +00:00
|
|
|
# mount -t cgroup none /cgroup -o cpuset,memory,cpu,devices
|
2009-01-08 02:08:27 +00:00
|
|
|
|
|
|
|
and do task move, mkdir, rmdir etc...under this.
|
2009-01-29 22:25:14 +00:00
|
|
|
|
|
|
|
9.7 swapoff.
|
|
|
|
Besides management of swap is one of complicated parts of memcg,
|
|
|
|
call path of swap-in at swapoff is not same as usual swap-in path..
|
|
|
|
It's worth to be tested explicitly.
|
|
|
|
|
|
|
|
For example, test like following is good.
|
|
|
|
(Shell-A)
|
2010-03-10 23:22:37 +00:00
|
|
|
# mount -t cgroup none /cgroup -o memory
|
2009-01-29 22:25:14 +00:00
|
|
|
# mkdir /cgroup/test
|
|
|
|
# echo 40M > /cgroup/test/memory.limit_in_bytes
|
|
|
|
# echo 0 > /cgroup/test/tasks
|
|
|
|
Run malloc(100M) program under this. You'll see 60M of swaps.
|
|
|
|
(Shell-B)
|
|
|
|
# move all tasks in /cgroup/test to /cgroup
|
|
|
|
# /sbin/swapoff -a
|
2009-02-03 10:57:13 +00:00
|
|
|
# rmdir /cgroup/test
|
2009-01-29 22:25:14 +00:00
|
|
|
# kill malloc task.
|
|
|
|
|
|
|
|
Of course, tmpfs v.s. swapoff test should be tested, too.
|
memcg: fix OOM killer under memcg
This patch tries to fix OOM Killer problems caused by hierarchy.
Now, memcg itself has OOM KILL function (in oom_kill.c) and tries to
kill a task in memcg.
But, when hierarchy is used, it's broken and correct task cannot
be killed. For example, in following cgroup
/groupA/ hierarchy=1, limit=1G,
01 nolimit
02 nolimit
All tasks' memory usage under /groupA, /groupA/01, groupA/02 is limited to
groupA's 1Gbytes but OOM Killer just kills tasks in groupA.
This patch provides makes the bad process be selected from all tasks
under hierarchy. BTW, currently, oom_jiffies is updated against groupA
in above case. oom_jiffies of tree should be updated.
To see how oom_jiffies is used, please check mem_cgroup_oom_called()
callers.
[akpm@linux-foundation.org: build fix]
[akpm@linux-foundation.org: const fix]
Signed-off-by: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com>
Cc: Paul Menage <menage@google.com>
Cc: Li Zefan <lizf@cn.fujitsu.com>
Cc: Balbir Singh <balbir@in.ibm.com>
Cc: Daisuke Nishimura <nishimura@mxp.nes.nec.co.jp>
Cc: David Rientjes <rientjes@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2009-04-02 23:57:38 +00:00
|
|
|
|
|
|
|
9.8 OOM-Killer
|
|
|
|
Out-of-memory caused by memcg's limit will kill tasks under
|
|
|
|
the memcg. When hierarchy is used, a task under hierarchy
|
|
|
|
will be killed by the kernel.
|
|
|
|
In this case, panic_on_oom shouldn't be invoked and tasks
|
|
|
|
in other groups shouldn't be killed.
|
|
|
|
|
|
|
|
It's not difficult to cause OOM under memcg as following.
|
|
|
|
Case A) when you can swapoff
|
|
|
|
#swapoff -a
|
|
|
|
#echo 50M > /memory.limit_in_bytes
|
|
|
|
run 51M of malloc
|
|
|
|
|
|
|
|
Case B) when you use mem+swap limitation.
|
|
|
|
#echo 50M > memory.limit_in_bytes
|
|
|
|
#echo 50M > memory.memsw.limit_in_bytes
|
|
|
|
run 51M of malloc
|
2010-03-10 23:22:31 +00:00
|
|
|
|
|
|
|
9.9 Move charges at task migration
|
|
|
|
Charges associated with a task can be moved along with task migration.
|
|
|
|
|
|
|
|
(Shell-A)
|
|
|
|
#mkdir /cgroup/A
|
|
|
|
#echo $$ >/cgroup/A/tasks
|
|
|
|
run some programs which uses some amount of memory in /cgroup/A.
|
|
|
|
|
|
|
|
(Shell-B)
|
|
|
|
#mkdir /cgroup/B
|
|
|
|
#echo 1 >/cgroup/B/memory.move_charge_at_immigrate
|
|
|
|
#echo "pid of the program running in group A" >/cgroup/B/tasks
|
|
|
|
|
|
|
|
You can see charges have been moved by reading *.usage_in_bytes or
|
|
|
|
memory.stat of both A and B.
|
2016-08-02 14:23:57 +00:00
|
|
|
See 8.2 of Documentation/cgroup-v1/memory.txt to see what value should be
|
2010-03-10 23:22:31 +00:00
|
|
|
written to move_charge_at_immigrate.
|
2010-03-10 23:22:36 +00:00
|
|
|
|
|
|
|
9.10 Memory thresholds
|
tree-wide: fix comment/printk typos
"gadget", "through", "command", "maintain", "maintain", "controller", "address",
"between", "initiali[zs]e", "instead", "function", "select", "already",
"equal", "access", "management", "hierarchy", "registration", "interest",
"relative", "memory", "offset", "already",
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
Signed-off-by: Jiri Kosina <jkosina@suse.cz>
2010-11-01 19:38:34 +00:00
|
|
|
Memory controller implements memory thresholds using cgroups notification
|
2013-01-04 21:05:17 +00:00
|
|
|
API. You can use tools/cgroup/cgroup_event_listener.c to test it.
|
2010-03-10 23:22:36 +00:00
|
|
|
|
|
|
|
(Shell-A) Create cgroup and run event listener
|
|
|
|
# mkdir /cgroup/A
|
|
|
|
# ./cgroup_event_listener /cgroup/A/memory.usage_in_bytes 5M
|
|
|
|
|
|
|
|
(Shell-B) Add task to cgroup and try to allocate and free memory
|
|
|
|
# echo $$ >/cgroup/A/tasks
|
|
|
|
# a="$(dd if=/dev/zero bs=1M count=10)"
|
|
|
|
# a=
|
|
|
|
|
|
|
|
You will see message from cgroup_event_listener every time you cross
|
|
|
|
the thresholds.
|
|
|
|
|
|
|
|
Use /cgroup/A/memory.memsw.usage_in_bytes to test memsw thresholds.
|
|
|
|
|
|
|
|
It's good idea to test root cgroup as well.
|