Skip to contentSkip to conversation

KVM: MMU: Don't use RCU for lockless shadow walking

PATCH Superseded Avi Kivity 3 participants 8 messages Apr 24, 2012 – Apr 29, 2012

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Avi Kivity

author
#0 lore raw

PATCH v2 1/1 KVM: MMU: Don't use RCU for lockless shadow walking

Using RCU for lockless shadow walking can increase the amount of memory in use by the system, since RCU grace periods are unpredictable. We also have an unconditional write to a shared variable (reader_counter), which isn't good for scaling.

Replace that with a scheme similar to x86's get_user_pages_fast(): disable interrupts during lockless shadow walk to force the freer (kvm_mmu_commit_zap_page()) to wait for the TLB flush IPI to find the processor with interrupts enabled.

We also add a new vcpu->mode, READING_SHADOW_PAGE_TABLES, to prevent kvm_flush_remote_tlbs() from avoiding the IPI.

---
 arch/x86/include/asm/kvm_host.h |    4 ---
 arch/x86/kvm/mmu.c              |   72 +++++++++++++++------------------------
 include/linux/kvm_host.h        |    3 +-
 3 files changed, 30 insertions(+), 49 deletions(-)
arch/x86/include/asm/kvm_host.h +0 -4
index f624ca7..67e66e6 100644
@@ -237,8 +237,6 @@ struct kvm_mmu_page {
 #endif
 
 	int write_flooding_count;
-
-	struct rcu_head rcu;
 };
 
 struct kvm_pio_request {
@@ -536,8 +534,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 +28 -44

101 lines not on this page · raw message

include/linux/kvm_host.h +2 -1
index 186ffab..d1f1adf 100644
@@ -128,7 +128,8 @@ int kvm_async_pf_wakeup_all(struct kvm_vcpu *vcpu);
 enum {
 	OUTSIDE_GUEST_MODE,
 	IN_GUEST_MODE,
-	EXITING_GUEST_MODE
+	EXITING_GUEST_MODE,
+	READING_SHADOW_PAGE_TABLES,
 };
 
 /*
Signature · 1 line
1.7.10
#1 lore raw
Quoted 5 lines · Avi Kivity

static void kvm_mmu_commit_zap_page(struct kvm *kvm,

arch/x86/kvm/mmu.c +0 -0
 				    struct list_head *invalid_list)
 {
@@ -2021,17 +2006,17 @@ static void kvm_mmu_commit_zap_page(struct kvm *kvm,
 	if (list_empty(invalid_list))

arch/x86/kvm/mmu.c static void kvm_mmu_commit_zap_page(struct kvm *kvm, · line 2010

Quoted 10 lines
arch/x86/kvm/mmu.c +2 -6
 		return;
 
-	kvm_flush_remote_tlbs(kvm);
-
-	if (atomic_read(&kvm->arch.reader_counter)) {
-		kvm_mmu_isolate_pages(invalid_list);
-		sp = list_first_entry(invalid_list, struct kvm_mmu_page, link);
-		list_del_init(invalid_list);
+	/*
+	 * wmb: make sure everyone sees our modifications to the page tables

Other lockless vcpu can see a old-copy of a spte, but it is ok since all page tables is valid to be used. (kvm_flush_remote_tlbs can stop page table to be freed)

arch/x86/kvm/mmu.c static void kvm_mmu_commit_zap_page(struct kvm *kvm, · line 2011

Quoted 1 line
arch/x86/kvm/mmu.c +1 -0
+	 * rmb: make sure we see changes to vcpu->mode

It is the responsibility of kvm_flush_remote_tlbs to see changes to vcpu->mode. No?

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

static void kvm_mmu_commit_zap_page(struct kvm *kvm,

arch/x86/kvm/mmu.c +0 -0
 				    struct list_head *invalid_list)
 {
@@ -2021,17 +2006,17 @@ static void kvm_mmu_commit_zap_page(struct kvm *kvm,
 	if (list_empty(invalid_list))
 		return;
 
Quoted 13 lines

arch/x86/kvm/mmu.c static void kvm_mmu_commit_zap_page(struct kvm *kvm, · line 2010

Quoted 8 lines
arch/x86/kvm/mmu.c +2 -6
-	kvm_flush_remote_tlbs(kvm);
-
-	if (atomic_read(&kvm->arch.reader_counter)) {
-		kvm_mmu_isolate_pages(invalid_list);
-		sp = list_first_entry(invalid_list, struct kvm_mmu_page, link);
-		list_del_init(invalid_list);
+	/*
+	 * wmb: make sure everyone sees our modifications to the page tables

Other lockless vcpu can see a old-copy of a spte, but it is ok since all page tables is valid to be used. (kvm_flush_remote_tlbs can stop page table to be freed)

We want kvm_commit_zap_page() to see the changes before freeing memory. But maybe it's the responsibility of kmem_cache_free()/kmem_cache_alloc().

Quoted 5 lines

arch/x86/kvm/mmu.c static void kvm_mmu_commit_zap_page(struct kvm *kvm, · line 2011

Quoted 1 line
arch/x86/kvm/mmu.c +1 -0
+	 * rmb: make sure we see changes to vcpu->mode

It is the responsibility of kvm_flush_remote_tlbs to see changes to vcpu->mode. No?

Yes.

Signature · 1 line
error compiling committee.c: too many arguments to function
#3 lore raw
In reply to Avi Kivity
Quoted 68 lines · Avi Kivity

Using RCU for lockless shadow walking can increase the amount of memory in use by the system, since RCU grace periods are unpredictable. We also have an unconditional write to a shared variable (reader_counter), which isn't good for scaling.

Replace that with a scheme similar to x86's get_user_pages_fast(): disable interrupts during lockless shadow walk to force the freer (kvm_mmu_commit_zap_page()) to wait for the TLB flush IPI to find the processor with interrupts enabled.

We also add a new vcpu->mode, READING_SHADOW_PAGE_TABLES, to prevent kvm_flush_remote_tlbs() from avoiding the IPI.

---
 arch/x86/include/asm/kvm_host.h |    4 ---
 arch/x86/kvm/mmu.c              |   72 +++++++++++++++------------------------
 include/linux/kvm_host.h        |    3 +-
 3 files changed, 30 insertions(+), 49 deletions(-)
arch/x86/include/asm/kvm_host.h +0 -4
index f624ca7..67e66e6 100644
@@ -237,8 +237,6 @@ struct kvm_mmu_page {
 #endif
 
 	int write_flooding_count;
-
-	struct rcu_head rcu;
 };
 
 struct kvm_pio_request {
@@ -536,8 +534,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 +11 -5
index 07424cf..ef88034 100644
@@ -551,19 +551,28 @@ static u64 mmu_spte_get_lockless(u64 *sptep)
 
 static void walk_shadow_page_lockless_begin(struct kvm_vcpu *vcpu)
 {
-	rcu_read_lock();
-	atomic_inc(&vcpu->kvm->arch.reader_counter);
-
-	/* Increase the counter before walking shadow page table */
-	smp_mb__after_atomic_inc();
+	/*
+	 * Prevent page table teardown by making any free-er wait during
+	 * kvm_flush_remote_tlbs() IPI to all active vcpus.
+	 */
+	local_irq_disable();
+	vcpu->mode = READING_SHADOW_PAGE_TABLES;
+	/*
+	 * wmb: advertise vcpu->mode change
+	 * rmb: make sure we see updated sptes
+	 */
+	smp_mb();
 }
 

arch/x86/kvm/mmu.c static u64 mmu_spte_get_lockless(u64 *sptep) · line 572

Quoted 10 lines
arch/x86/kvm/mmu.c +4 -4
 static void walk_shadow_page_lockless_end(struct kvm_vcpu *vcpu)
 {
-	/* Decrease the counter after walking shadow page table finished */
-	smp_mb__before_atomic_dec();
-	atomic_dec(&vcpu->kvm->arch.reader_counter);
-	rcu_read_unlock();
+	/*
+	 * Make our reads and writes to shadow page tables globally visible
+	 * before leaving READING_SHADOW_PAGE_TABLES mode.
+	 */

This comment is misleading. Writes to shadow page tables must be performed with locked instructions outside the mmu_lock.

arch/x86/kvm/mmu.c static u64 mmu_spte_get_lockless(u64 *sptep) · line 574

Quoted 2 lines
arch/x86/kvm/mmu.c +2 -0
+	smp_mb();
+	vcpu->mode = OUTSIDE_GUEST_MODE;

Don't you want

vcpu->mode = OUTSIDE_GUEST_MODE;
smp_mb();

So that vcpu->mode update is globally visible before subsequent loads execute?

#4 lore raw
Quoted 5 lines · Marcelo Tosatti
Quoted 5 lines

static void walk_shadow_page_lockless_end(struct kvm_vcpu *vcpu) {

arch/x86/kvm/mmu.c +0 -3
-	/* Decrease the counter after walking shadow page table finished */
-	smp_mb__before_atomic_dec();
-	atomic_dec(&vcpu->kvm->arch.reader_counter);
Quoted 9 lines

arch/x86/kvm/mmu.c static u64 mmu_spte_get_lockless(u64 *sptep) · line 572

Quoted 5 lines
arch/x86/kvm/mmu.c +4 -1
-	rcu_read_unlock();
+	/*
+	 * Make our reads and writes to shadow page tables globally visible
+	 * before leaving READING_SHADOW_PAGE_TABLES mode.
+	 */

This comment is misleading. Writes to shadow page tables must be performed with locked instructions outside the mmu_lock.

You mean that the write should guarantee a correct memory order by itself?

Quoted 8 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+	smp_mb();
+	vcpu->mode = OUTSIDE_GUEST_MODE;

Don't you want

vcpu->mode = OUTSIDE_GUEST_MODE;
smp_mb();

It is unsafe i think, it is a problem if spte read / spte update is ordered to the behind of vcpu->mode = OUTSIDE_GUEST_MODE, like below:

VCPU 0 VCPU 1
commit_zapped_page:
/*
* setting vcpu->mode is reordered
* to the head of read spte.
*/
vcpu->mode = OUTSIDE_GUEST_MODE;

see VCPU 0 is out-of-guest-mode, IPI is not sent, and the sp is free immediately.

read spte;
OOPS!!!

(It is invalid since spte is freed.)

smp_mb

#5 lore raw
Quoted 19 lines · Xiao Guangrong
Quoted 5 lines · Marcelo Tosatti
Quoted 5 lines

static void walk_shadow_page_lockless_end(struct kvm_vcpu *vcpu) {

arch/x86/kvm/mmu.c +0 -3
-	/* Decrease the counter after walking shadow page table finished */
-	smp_mb__before_atomic_dec();
-	atomic_dec(&vcpu->kvm->arch.reader_counter);
Quoted 9 lines

arch/x86/kvm/mmu.c static u64 mmu_spte_get_lockless(u64 *sptep) · line 572

Quoted 5 lines
arch/x86/kvm/mmu.c +4 -1
-	rcu_read_unlock();
+	/*
+	 * Make our reads and writes to shadow page tables globally visible
+	 * before leaving READING_SHADOW_PAGE_TABLES mode.
+	 */

This comment is misleading. Writes to shadow page tables must be performed with locked instructions outside the mmu_lock.

Quoted 1 line

You mean that the write should guarantee a correct memory order by itself?

Yes.

Quoted 24 lines
Quoted 8 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+	smp_mb();
+	vcpu->mode = OUTSIDE_GUEST_MODE;

Don't you want

vcpu->mode = OUTSIDE_GUEST_MODE;
smp_mb();

It is unsafe i think, it is a problem if spte read / spte update is ordered to the behind of vcpu->mode = OUTSIDE_GUEST_MODE, like below:

VCPU 0 VCPU 1
commit_zapped_page:
/*
* setting vcpu->mode is reordered
* to the head of read spte.
*/
vcpu->mode = OUTSIDE_GUEST_MODE;

see VCPU 0 is out-of-guest-mode, IPI is not sent, and the sp is free immediately.

Quoted 6 lines

read spte;
OOPS!!!

(It is invalid since spte is freed.)

smp_mb

Right. In that case a compiler barrier is sufficient (stores are not reordered with earlier loads on x86).

author
#7 lore raw
In reply to Marcelo Tosatti
Quoted 14 lines · Marcelo Tosatti

arch/x86/kvm/mmu.c static u64 mmu_spte_get_lockless(u64 *sptep) · line 572

Quoted 11 lines

static void walk_shadow_page_lockless_end(struct kvm_vcpu *vcpu) {

arch/x86/kvm/mmu.c +4 -4
-	/* Decrease the counter after walking shadow page table finished */
-	smp_mb__before_atomic_dec();
-	atomic_dec(&vcpu->kvm->arch.reader_counter);
-	rcu_read_unlock();
+	/*
+	 * Make our reads and writes to shadow page tables globally visible
+	 * before leaving READING_SHADOW_PAGE_TABLES mode.
+	 */

This comment is misleading. Writes to shadow page tables must be performed with locked instructions outside the mmu_lock.

You are correct with respect to writes. I guess reads are immaterial because they will be either protected by a write, or discarded.

Signature · 1 line
error compiling committee.c: too many arguments to function
author
#6 lore raw
In reply to Marcelo Tosatti
Quoted 21 lines · Marcelo Tosatti
Quoted 21 lines
Quoted 8 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+	smp_mb();
+	vcpu->mode = OUTSIDE_GUEST_MODE;

Don't you want

vcpu->mode = OUTSIDE_GUEST_MODE;
smp_mb();

It is unsafe i think, it is a problem if spte read / spte update is ordered to the behind of vcpu->mode = OUTSIDE_GUEST_MODE, like below:

VCPU 0 VCPU 1
commit_zapped_page:
/*
* setting vcpu->mode is reordered
* to the head of read spte.
*/
vcpu->mode = OUTSIDE_GUEST_MODE;

Quoted 5 lines
Quoted 5 lines

see VCPU 0 is out-of-guest-mode, IPI is not sent, and the sp is free immediately.

read spte;
OOPS!!!

Ouch!

Quoted 7 lines
Quoted 4 lines

(It is invalid since spte is freed.)

smp_mb

Right. In that case a compiler barrier is sufficient (stores are not reordered with earlier loads on x86).

I think it's customary not to depend on arch specific memory barrier behaviour (but I don't have anything to base this on, just a feeling).

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