Skip to contentSkip to conversation

Simplify RCU freeing of shadow pages

PATCH In review Avi Kivity 3 participants 10 messages Apr 19, 2012 – Apr 23, 2012

Subsystems (3)
lore
Share & tools
lore

Avi Kivity

author
#0 lore raw

cover Simplify RCU freeing of shadow pages

This patchset simplifies the freeing by RCU of mmu pages.

Xiao, I'm sure you thought of always freeing by RCU. Why didn't you choose this way? I saves a couple of atomics in the fast path.

Avi Kivity (2):
KVM: MMU: Always free shadow pages using RCU
KVM: MMU: Recover space used by rcu_head in struct kvm_mmu_page

 arch/x86/include/asm/kvm_host.h |    9 +++---
 arch/x86/kvm/mmu.c              |   58 ++++++++-------------------------------
 2 files changed, 15 insertions(+), 52 deletions(-)
Signature · 1 line
1.7.10
author
#1 lore raw

PATCH 1/2 KVM: MMU: Always free shadow pages using RCU

The non-RCU path costs us two atomic operations, as well as extra code complexity.

---
 arch/x86/include/asm/kvm_host.h |    2 --
 arch/x86/kvm/mmu.c              |   46 ++++++++-------------------------------
 2 files changed, 9 insertions(+), 39 deletions(-)
arch/x86/include/asm/kvm_host.h +0 -2
index f624ca7..b885445 100644
@@ -536,8 +536,6 @@ struct kvm_arch {
 	u64 hv_guest_os_id;
 	u64 hv_hypercall;
 
-	atomic_t reader_counter;
-
 	#ifdef CONFIG_KVM_MMU_AUDIT
 	int audit_point;
 	#endif
arch/x86/kvm/mmu.c +9 -37

80 lines not on this page · raw message

Signature · 1 line
1.7.10
author
#2 lore raw

PATCH 2/2 KVM: MMU: Recover space used by rcu_head in struct kvm_mmu_page

In reply to Avi Kivity

By overlaying the field with 'link', we reduce the structure size by 16 bytes.

Changing call_rcu() to be per-page is not strictly necessary, but it can help RCU estimate the amount of work pending.

---
 arch/x86/include/asm/kvm_host.h |    7 ++++---
 arch/x86/kvm/mmu.c              |   26 +++++++++-----------------
 2 files changed, 13 insertions(+), 20 deletions(-)
arch/x86/include/asm/kvm_host.h +4 -3
index b885445..ae02ff8 100644
@@ -208,7 +208,10 @@ union kvm_mmu_page_role {
 };
 
 struct kvm_mmu_page {
-	struct list_head link;
+	union {
+		struct list_head link;
+		struct rcu_head rcu;
+	};
 	struct hlist_node hash_link;
 
 	/*
@@ -237,8 +240,6 @@ struct kvm_mmu_page {
 #endif
 
 	int write_flooding_count;
-
-	struct rcu_head rcu;
 };
 
 struct kvm_pio_request {
arch/x86/kvm/mmu.c +9 -17
index c10f60b..26257d7 100644
@@ -1338,7 +1338,6 @@ static void kvm_mmu_isolate_page(struct kvm_mmu_page *sp)
  */
 static void kvm_mmu_free_page(struct kvm_mmu_page *sp)
 {
-	list_del(&sp->link);
 	free_page((unsigned long)sp->spt);
 	kmem_cache_free(mmu_page_header_cache, sp);
 }
@@ -1980,20 +1979,12 @@ static void kvm_mmu_isolate_pages(struct list_head *invalid_list)
 		kvm_mmu_isolate_page(sp);
 }
 
-static void free_pages_rcu(struct rcu_head *head)
+static void free_page_rcu(struct rcu_head *head)
 {
-	struct kvm_mmu_page *next, *sp;
+	struct kvm_mmu_page *sp;
 
 	sp = container_of(head, struct kvm_mmu_page, rcu);
-	while (sp) {
-		if (!list_empty(&sp->link))
-			next = list_first_entry(&sp->link,
-				      struct kvm_mmu_page, link);
-		else
-			next = NULL;
-		kvm_mmu_free_page(sp);
-		sp = next;
-	}
+	kvm_mmu_free_page(sp);
 }
 
 static void kvm_mmu_commit_zap_page(struct kvm *kvm,
@@ -2007,11 +1998,12 @@ static void kvm_mmu_commit_zap_page(struct kvm *kvm,
 	kvm_flush_remote_tlbs(kvm);
 
 	kvm_mmu_isolate_pages(invalid_list);
-	sp = list_first_entry(invalid_list, struct kvm_mmu_page, link);
-	list_del_init(invalid_list);
-
-	trace_kvm_mmu_delay_free_pages(sp);
-	call_rcu(&sp->rcu, free_pages_rcu);
+	while (!list_empty(invalid_list)) {
+		sp = list_first_entry(invalid_list, struct kvm_mmu_page, link);
+		list_del(&sp->link);
+		trace_kvm_mmu_delay_free_pages(sp);
+		call_rcu(&sp->rcu, free_page_rcu);
+	}
 }
 
 /*
Signature · 1 line
1.7.10
#3 lore raw
In reply to Avi Kivity
Quoted 5 lines · Avi Kivity

This patchset simplifies the freeing by RCU of mmu pages.

Xiao, I'm sure you thought of always freeing by RCU. Why didn't you choose this way? I saves a couple of atomics in the fast path.

Avi, we have discussed it last year:

https://lkml.org/lkml/2011/6/29/177

I have optimized/simplified for "write flood" a lot, but, unfortunately, the zapping sp is still frequently.

Maybe we can cache the zapped sp in a invalid_sp_list to reduce the frequency.

Or, we may use SLAB_DESTROY_BY_RCU to free the shadow page.

author
#4 lore raw
Quoted 4 lines · Xiao Guangrong
Quoted 2 lines · Avi Kivity

This patchset simplifies the freeing by RCU of mmu pages.

Quoted 8 lines
Quoted 3 lines

Xiao, I'm sure you thought of always freeing by RCU. Why didn't you choose this way? I saves a couple of atomics in the fast path.

Avi, we have discussed it last year:

https://lkml.org/lkml/2011/6/29/177

Here are my own measurements (4 way guest on 24-way host):

time make -j8

ept=0, with patch

real 3m54.526s
user 11m7.074s
sys 3m29.678s

real 3m53.806s
user 11m13.654s
sys 3m26.577s

real 3m53.578s
user 11m13.491s
sys 3m26.095s

real 3m53.956s
user 11m14.866s
sys 3m25.075s

ept=1, with patch

real 2m36.510s
user 8m18.905s
sys 1m31.712s

real 2m36.642s
user 8m18.706s
sys 1m31.898s

real 2m36.952s
user 8m20.041s
sys 1m31.929s

real 2m37.036s
user 8m20.204s
sys 1m31.947s

ept=0, unpatched

real 3m51.299s
user 11m23.770s
sys 3m28.049s

real 3m52.982s
user 11m28.419s
sys 3m29.645s

real 3m52.634s
user 11m27.325s
sys 3m29.752s

real 3m53.696s
user 11m30.215s
sys 3m30.146s

ept=1, unpatched

real 2m37.134s
user 8m26.707s
sys 1m33.152s

real 2m36.838s
user 8m26.255s
sys 1m33.078s

real 2m36.826s
user 8m26.917s
sys 1m32.449s

real 2m36.877s
user 8m26.226s
sys 1m32.074s

So there is a small degradation with ept=0 and an even smaller improvement with ept=1. But certainly we should give more weight to ept=1, and also factor in the code simplification.

Quoted 5 lines

I have optimized/simplified for "write flood" a lot, but, unfortunately, the zapping sp is still frequently.

Maybe we can cache the zapped sp in a invalid_sp_list to reduce the frequency.

To recover the time for re-initializing sp->spt?

Quoted 1 line

Or, we may use SLAB_DESTROY_BY_RCU to free the shadow page.

IIUC a kmem_cache_alloc() can reuse an allocation freed using SLAB_DESTROY_BY_RCU before a grace period has elapsed.

Signature · 1 line
error compiling committee.c: too many arguments to function
#5 lore raw
Quoted 87 lines · Avi Kivity
Quoted 4 lines · Xiao Guangrong
Quoted 2 lines · Avi Kivity

This patchset simplifies the freeing by RCU of mmu pages.

Quoted 8 lines
Quoted 3 lines

Xiao, I'm sure you thought of always freeing by RCU. Why didn't you choose this way? I saves a couple of atomics in the fast path.

Avi, we have discussed it last year:

https://lkml.org/lkml/2011/6/29/177

Here are my own measurements (4 way guest on 24-way host):

time make -j8

ept=0, with patch

real 3m54.526s
user 11m7.074s
sys 3m29.678s

real 3m53.806s
user 11m13.654s
sys 3m26.577s

real 3m53.578s
user 11m13.491s
sys 3m26.095s

real 3m53.956s
user 11m14.866s
sys 3m25.075s

ept=1, with patch

real 2m36.510s
user 8m18.905s
sys 1m31.712s

real 2m36.642s
user 8m18.706s
sys 1m31.898s

real 2m36.952s
user 8m20.041s
sys 1m31.929s

real 2m37.036s
user 8m20.204s
sys 1m31.947s

ept=0, unpatched

real 3m51.299s
user 11m23.770s
sys 3m28.049s

real 3m52.982s
user 11m28.419s
sys 3m29.645s

real 3m52.634s
user 11m27.325s
sys 3m29.752s

real 3m53.696s
user 11m30.215s
sys 3m30.146s

ept=1, unpatched

real 2m37.134s
user 8m26.707s
sys 1m33.152s

real 2m36.838s
user 8m26.255s
sys 1m33.078s

real 2m36.826s
user 8m26.917s
sys 1m32.449s

Quoted 9 lines

real 2m36.877s
user 8m26.226s
sys 1m32.074s

So there is a small degradation with ept=0 and an even smaller improvement with ept=1. But certainly we should give more weight to ept=1, and also factor in the code simplification.

I agree.

Quoted 7 lines
Quoted 5 lines

I have optimized/simplified for "write flood" a lot, but, unfortunately, the zapping sp is still frequently.

Maybe we can cache the zapped sp in a invalid_sp_list to reduce the frequency.

To recover the time for re-initializing sp->spt?

Batch free shadow page, for example, list 10 sp in the invalid_list, then just need one rcu_free to free them.

Quoted 6 lines
Quoted 2 lines

Or, we may use SLAB_DESTROY_BY_RCU to free the shadow page.

IIUC a kmem_cache_alloc() can reuse an allocation freed using SLAB_DESTROY_BY_RCU before a grace period has elapsed.

Yes. So we need a refcount out of mmu-lock, and see the shadow page is stil pointed the upper level (prevent not re-alloced). This way may make code more complex.

#6 lore raw
In reply to Avi Kivity
Quoted 5 lines · Avi Kivity

This patchset simplifies the freeing by RCU of mmu pages.

Xiao, I'm sure you thought of always freeing by RCU. Why didn't you choose this way? I saves a couple of atomics in the fast path.

Quoted 7 lines

Avi Kivity (2):
KVM: MMU: Always free shadow pages using RCU
KVM: MMU: Recover space used by rcu_head in struct kvm_mmu_page

 arch/x86/include/asm/kvm_host.h |    9 +++---
 arch/x86/kvm/mmu.c              |   58 ++++++++-------------------------------
 2 files changed, 15 insertions(+), 52 deletions(-)

Check Documentation/RCU/checklist.txt item 8.

a. Keeping a count of the number of data-structure elements
used by the RCU-protected data structure, including those waiting for a grace period to elapse. Enforce a limit on this number, stalling updates as needed to allow previously deferred frees to complete. Alternatively, limit only the number awaiting deferred free rather than the total number of elements.

author
#7 lore raw
Quoted 16 lines · Marcelo Tosatti
Quoted 5 lines · Avi Kivity

This patchset simplifies the freeing by RCU of mmu pages.

Xiao, I'm sure you thought of always freeing by RCU. Why didn't you choose this way? I saves a couple of atomics in the fast path.

Quoted 7 lines

Avi Kivity (2):
KVM: MMU: Always free shadow pages using RCU
KVM: MMU: Recover space used by rcu_head in struct kvm_mmu_page

 arch/x86/include/asm/kvm_host.h |    9 +++---
 arch/x86/kvm/mmu.c              |   58 ++++++++-------------------------------
 2 files changed, 15 insertions(+), 52 deletions(-)

Check Documentation/RCU/checklist.txt item 8.

Quoted 8 lines

a. Keeping a count of the number of data-structure elements
used by the RCU-protected data structure, including those waiting for a grace period to elapse. Enforce a limit on this number, stalling updates as needed to allow previously deferred frees to complete. Alternatively, limit only the number awaiting deferred free rather than the total number of elements.

That's true before and after the patch. Currently the amount of memory that depends on rcu for freeing is unbounded.

Maybe we should protect the fast path using local_irq_disable() instead of rcu_read_lock(), like x86 page tables. That means that kvm_flush_remote_tlbs() needs to ignore OUTSIDE_GUEST_MODE when IPIing vcpu threads.

Signature · 1 line
error compiling committee.c: too many arguments to function
#8 lore raw
Quoted 26 lines · Avi Kivity
Quoted 16 lines · Marcelo Tosatti
Quoted 5 lines · Avi Kivity

This patchset simplifies the freeing by RCU of mmu pages.

Xiao, I'm sure you thought of always freeing by RCU. Why didn't you choose this way? I saves a couple of atomics in the fast path.

Quoted 7 lines

Avi Kivity (2):
KVM: MMU: Always free shadow pages using RCU
KVM: MMU: Recover space used by rcu_head in struct kvm_mmu_page

 arch/x86/include/asm/kvm_host.h |    9 +++---
 arch/x86/kvm/mmu.c              |   58 ++++++++-------------------------------
 2 files changed, 15 insertions(+), 52 deletions(-)

Check Documentation/RCU/checklist.txt item 8.

Quoted 8 lines

a. Keeping a count of the number of data-structure elements
used by the RCU-protected data structure, including those waiting for a grace period to elapse. Enforce a limit on this number, stalling updates as needed to allow previously deferred frees to complete. Alternatively, limit only the number awaiting deferred free rather than the total number of elements.

Quoted 7 lines

That's true before and after the patch. Currently the amount of memory that depends on rcu for freeing is unbounded.

Maybe we should protect the fast path using local_irq_disable() instead of rcu_read_lock(), like x86 page tables. That means that kvm_flush_remote_tlbs() needs to ignore OUTSIDE_GUEST_MODE when IPIing vcpu threads.

So, IPI is unconditionally sent, do we need introduce a new MODE (say STOP_SP_FREE) to reduce IPI?

author
#9 lore raw
Quoted 31 lines · Xiao Guangrong
Quoted 29 lines · Avi Kivity
Quoted 16 lines · Marcelo Tosatti
Quoted 5 lines · Avi Kivity

This patchset simplifies the freeing by RCU of mmu pages.

Xiao, I'm sure you thought of always freeing by RCU. Why didn't you choose this way? I saves a couple of atomics in the fast path.

Quoted 7 lines

Avi Kivity (2):
KVM: MMU: Always free shadow pages using RCU
KVM: MMU: Recover space used by rcu_head in struct kvm_mmu_page

 arch/x86/include/asm/kvm_host.h |    9 +++---
 arch/x86/kvm/mmu.c              |   58 ++++++++-------------------------------
 2 files changed, 15 insertions(+), 52 deletions(-)

Check Documentation/RCU/checklist.txt item 8.

Quoted 8 lines

a. Keeping a count of the number of data-structure elements
used by the RCU-protected data structure, including those waiting for a grace period to elapse. Enforce a limit on this number, stalling updates as needed to allow previously deferred frees to complete. Alternatively, limit only the number awaiting deferred free rather than the total number of elements.

That's true before and after the patch. Currently the amount of memory that depends on rcu for freeing is unbounded.

Quoted 7 lines
Quoted 4 lines

Maybe we should protect the fast path using local_irq_disable() instead of rcu_read_lock(), like x86 page tables. That means that kvm_flush_remote_tlbs() needs to ignore OUTSIDE_GUEST_MODE when IPIing vcpu threads.

So, IPI is unconditionally sent, do we need introduce a new MODE (say STOP_SP_FREE) to reduce IPI?

We can just reuse IN_GUEST_MODE for this (with a comment explaining why).

Signature · 1 line
error compiling committee.c: too many arguments to function