shrinker support for the mmu cache
PATCH 1/1 shrinker support for the mmu cache
this patch simply register the mmu cache with the shrinker.
Quoted 1 line
From
fa7e68f0d0be21dff6d4f3ac12bae10247f5574cMon Sep 17 00:00:00 2001
From: Izik Eidus <[email protected]>
Date: Wed, 12 Mar 2008 20:05:11 +0200
Subject: [PATCH] KVM: register the kvm mmu cache with the shrinker.
--- arch/x86/kvm/mmu.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 52 insertions(+), 0 deletions(-)
arch/x86/kvm/mmu.c +52 -0
@@ -1940,6 +1940,55 @@ void kvm_mmu_zap_all(struct kvm *kvm)kvm_flush_remote_tlbs (kvm); } +void kvm_mmu_remove_one_alloc_mmu_page (struct kvm *kvm) +{ +struct kvm_mmu_page *page; + + page =container_of (kvm->arch.active_mmu_pages.prev, + struct kvm_mmu_page, link); +kvm_mmu_zap_page (kvm, page); +} + +static int mmu_shrink (int nr_to_scan,gfp_t gfp_mask) +{ +struct kvm *kvm; +int cache_count =0 ; +int once =0 ; + +do { +spin_lock (&kvm_lock); +if (list_empty (&vm_list)) { +spin_unlock (&kvm_lock); +return 0 ; + } +list_for_each_entry (kvm, &vm_list, vm_list) { +int npages; + +spin_lock (&kvm->mmu_lock); + npages = kvm->arch.n_alloc_mmu_pages - + kvm->arch.n_free_mmu_pages; +if (!once) + cache_count += npages -KVM_MIN_ALLOC_MMU_PAGES ; +if (nr_to_scan >0 && npages >KVM_MIN_ALLOC_MMU_PAGES ) { +kvm_mmu_remove_one_alloc_mmu_page (kvm); + cache_count--; + } + nr_to_scan--; + +spin_unlock (&kvm->mmu_lock); + } +spin_unlock (&kvm_lock); + once =1 ; + }while (nr_to_scan >0 ); + +return cache_count; +} + +static struct shrinker mmu_shrinker = { + .shrink = mmu_shrink, + .seeks =DEFAULT_SEEKS , +}; +void kvm_mmu_module_exit (void ) {if (pte_chain_cache)
@@ -1948,6 +1997,7 @@ void kvm_mmu_module_exit(void)kmem_cache_destroy (rmap_desc_cache);if (mmu_page_header_cache)kmem_cache_destroy (mmu_page_header_cache); +unregister_shrinker (&mmu_shrinker); }int kvm_mmu_module_init (void )
@@ -1969,6 +2019,8 @@ int kvm_mmu_module_init(void)if (!mmu_page_header_cache) goto nomem; +register_shrinker (&mmu_shrinker); +return 0 ; nomem:
Signature · 1 line
1.5.3.6
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
kvm-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/kvm-devel
Quoted 1 line · Izik Eidus
this patch simply register the mmu cache with the shrinker.
Please inline patches in the future as it makes it easier to review. The implementation looks good and I think it's a good idea.
One is that there is one shrinker for all VMs but you run through the list of VMs in order. This means the first VM in the list is most frequently going to be shrunk down to KVM_MIN_ALLOC_MMU_PAGES. This seems unfair and potentially dangerous. The shrinker can be triggered potentially by the growth of the MMU cache on other VMs.
I think in the least, you should attempt to go through the VMs in a round-robin fashion to ensure that if you shrink one VM, the next time you'll shrink a different VM.
The other thing I wonder about is whether DEFAULT_SEEKS is the best value to use. On the one hand, a guest page fault is probably not as expensive as reclaiming something from disk. On the other hand, NPT guests are likely to be very sensitive to evicting things from the shadow page cache. I would think it's pretty clear that in the NPT case, the MMU cache should have a higher seek cost than the default.
Regards,
Anthony Liguori
Quoted 8 lines
------------------------------------------------------------------------
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
------------------------------------------------------------------------
Quoted 5 lines
_______________________________________________
kvm-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/kvm-devel
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
Quoted 4 lines · Anthony Liguori
Quoted 1 line · Izik Eidus
this patch simply register the mmu cache with the shrinker.
Please inline patches in the future as it makes it easier to review.
I knew this time will come when ppl will force me to send patchs inline (will happen next time )... :)
Quoted 11 lines
The implementation looks good and I think it's a good idea.
One is that there is one shrinker for all VMs but you run through the list of VMs in order. This means the first VM in the list is most frequently going to be shrunk down to
KVM_MIN_ALLOC_MMU_PAGES. This seems unfair and potentially dangerous. The shrinker can be triggered potentially by the growth of the MMU cache on other VMs.I think in the least, you should attempt to go through the VMs in a round-robin fashion to ensure that if you shrink one VM, the next time you'll shrink a different VM.
you are 100% right, i will do that.
Quoted 7 lines
The other thing I wonder about is whether
DEFAULT_SEEKSis the best value to use. On the one hand, a guest page fault is probably not as expensive as reclaiming something from disk. On the other hand, NPT guests are likely to be very sensitive to evicting things from the shadow page cache. I would think it's pretty clear that in the NPT case, the MMU cache should have a higher seek cost than the default.
let me look at this, i think you have a case
Quoted 20 lines
Regards,
Anthony Liguori
Quoted 14 lines
------------------------------------------------------------------------
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
------------------------------------------------------------------------_______________________________________________
kvm-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/kvm-devel
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
Quoted 1 line · Izik Eidus
this patch simply register the mmu cache with the shrinker.
Hi Izik,
Nice.
I think you want some sort of aging mechanism here. Walk through all translations of a shadow page clearing the referenced bit of all mappings it holds (and moving pages with any accessed translation to the head of the list).
Because the active_mmu list position only indicates the order in which those pages have been shadowed, not how frequently or recently they have been accessed.
And then have a maximum number of pages that you walk (nr_to_scan) on each shrinker callback run. Oh, I don't think you want to free more than one page on each run (right now you can free a large of chunk per run).
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
Quoted 7 lines · Marcelo Tosatti
Quoted 2 lines · Izik Eidus
this patch simply register the mmu cache with the shrinker.
Hi Izik,
Hello Marcelo,
Quoted 3 lines
Nice.
I think you want some sort of aging mechanism here.
well it is long time in the todo list to do some kind of lru for the shadow mmu pages
right now it "recycle" pages in a random way...
Quoted 5 lines
Walk through all
translations of a shadow page clearing the referenced bit of all mappings it holds (and moving pages with any accessed translation to the head of the list).
ok, i think i will just add a function named "sort_accessed_mmu_pages", that will just put to the top of the list the pages pointed by the ptes that werent accessed
and used it when i shrink, and when pages get recycled
this what you meant right?
Quoted 4 lines
Because the
active_mmulist position only indicates the order in which those pages have been shadowed, not how frequently or recently they have been accessed.
yep
Quoted 5 lines
And then have a maximum number of pages that you walk (
nr_to_scan) on each shrinker callback run. Oh, I don't think you want to free more than one page on each run (right now you can free a large of chunk per run).
thanks.
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
Quoted 23 lines · Izik Eidus
Quoted 7 lines · Marcelo Tosatti
Quoted 2 lines · Izik Eidus
this patch simply register the mmu cache with the shrinker.
Hi Izik,
Hello Marcelo,
Quoted 3 lines
Nice.
I think you want some sort of aging mechanism here.
well it is long time in the todo list to do some kind of lru for the shadow mmu pages
right now it "recycle" pages in a random way...Quoted 5 lines
Walk through all
translations of a shadow page clearing the referenced bit of all mappings it holds (and moving pages with any accessed translation to the head of the list).
Quoted 6 lines
ok, i think i will just add a function named "
sort_accessed_mmu_pages", that will just put to the top of the list the pages pointed by the ptes that werent accessed
and used it when i shrink, and when pages get recycledthis what you meant right?
By "top" I suppose you mean "end". So yes, right.
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
Quoted 5 lines · Marcelo Tosatti
Quoted 2 lines · Izik Eidus
this patch simply register the mmu cache with the shrinker.
Quoted 9 lines
Hi Izik,
Nice.
I think you want some sort of aging mechanism here. Walk through all translations of a shadow page clearing the referenced bit of all mappings it holds (and moving pages with any accessed translation to the head of the list).
While aging is not too hard to do, I don't think it would add much in practice; we rarely observe mmu shadow pages being recycled due to memory pressure. So this is mostly helpful for preventing a VM from pinning memory when under severe memory pressure, where we don't expect good performance anyway.
Signature · 7 lines
error compiling committee.c: too many arguments to function ------------------------------------------------------------------------- This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008. http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
Quoted 16 lines · Avi Kivity
Quoted 5 lines · Marcelo Tosatti
Quoted 2 lines · Izik Eidus
this patch simply register the mmu cache with the shrinker.
Quoted 9 lines
Hi Izik,
Nice.
I think you want some sort of aging mechanism here. Walk through all translations of a shadow page clearing the referenced bit of all mappings it holds (and moving pages with any accessed translation to the head of the list).
Quoted 5 lines
While aging is not too hard to do, I don't think it would add much in practice; we rarely observe mmu shadow pages being recycled due to memory pressure. So this is mostly helpful for preventing a VM from pinning memory when under severe memory pressure, where we don't expect good performance anyway.
Issue is that the shrinker callback will not be called only under severe memory pressure, but for normal system pressure too.
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
Quoted 8 lines · Marcelo Tosatti
Quoted 7 lines
While aging is not too hard to do, I don't think it would add much in practice; we rarely observe mmu shadow pages being recycled due to memory pressure. So this is mostly helpful for preventing a VM from pinning memory when under severe memory pressure, where we don't expect good performance anyway.
Quoted 4 lines
Issue is that the shrinker callback will not be called only under severe memory pressure, but for normal system pressure too.
How much shrinkage goes on under normal pressure?
Rebuilding a single shadow page costs a maximum of 512 faults (so about 1 msec). If the shrinker evicts one entry per second, this is a performance hiy of 0.1%.
Perhaps if we set the cost high enough, the normal eviction rate will be low enough.
Signature · 7 lines
error compiling committee.c: too many arguments to function ------------------------------------------------------------------------- This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008. http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
Quoted 9 lines · Avi Kivity
Quoted 8 lines · Marcelo Tosatti
Quoted 7 lines
While aging is not too hard to do, I don't think it would add much in practice; we rarely observe mmu shadow pages being recycled due to memory pressure. So this is mostly helpful for preventing a VM from pinning memory when under severe memory pressure, where we don't expect good performance anyway.
Quoted 6 lines
Quoted 4 lines
Issue is that the shrinker callback will not be called only under severe memory pressure, but for normal system pressure too.
How much shrinkage goes on under normal pressure?
It depends on the number of LRU pages scanned and the size of the cache.
Roughly the number of LRU pages scanned divided by shrinker->seeks, relative to cache size (mm/vmscan.c shrink_slab).
Quoted 6 lines
Rebuilding a single shadow page costs a maximum of 512 faults (so about 1 msec). If the shrinker evicts one entry per second, this is a performance hiy of 0.1%.
Perhaps if we set the cost high enough, the normal eviction rate will be low enough.
I think its pretty easy to check for the referenced bit on pages to avoid recently used ones from being zapped.
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
Quoted 16 lines · Marcelo Tosatti
Quoted 14 lines · Avi Kivity
Quoted 12 lines · Marcelo Tosatti
Quoted 7 lines
While aging is not too hard to do, I don't think it would add much in practice; we rarely observe mmu shadow pages being recycled due to memory pressure. So this is mostly helpful for preventing a VM from pinning memory when under severe memory pressure, where we don't expect good performance anyway.
Issue is that the shrinker callback will not be called only under severe memory pressure, but for normal system pressure too.
Quoted 9 lines
Quoted 2 lines
How much shrinkage goes on under normal pressure?
It depends on the number of LRU pages scanned and the size of the cache.
Roughly the number of LRU pages scanned divided by
shrinker->seeks, relative to cache size (mm/vmscan.cshrink_slab).
Since the maximum cache size is a small fraction of memory size, I think we should be okay here.
Quoted 11 lines
Quoted 7 lines
Rebuilding a single shadow page costs a maximum of 512 faults (so about 1 msec). If the shrinker evicts one entry per second, this is a performance hiy of 0.1%.
Perhaps if we set the cost high enough, the normal eviction rate will be low enough.
I think its pretty easy to check for the referenced bit on pages to avoid recently used ones from being zapped.
Not so easy:
- the pages don't have an accessed bit, the parent ptes do, so we need to scan the parent ptes list
- pages start out referenced, so we need to age them in two stages:
first clear the accessed bits (and move them back to the tail of the queue); if we find a page on the head with all accessed bits clear, we can throw it away.
- root pages don't have parent ptes, so we need to track access to them manually
- if the accessed bit clearing rate is too high, it loses its meaning
Nothing horribly hard, but not trivial either.
Signature · 7 lines
Do not meddle in the internals of kernels, for they are subtle and quick to panic. ------------------------------------------------------------------------- This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008. http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/