Skip to contentSkip to conversation

Minor vcpu->requests improvements

PATCH Superseded Avi Kivity 1 participant 4 messages May 17, 2012

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Avi Kivity

author
#0 lore raw

cover Minor vcpu->requests improvements

Nothing spectacular, just regularization of the code.

Avi Kivity (3):
KVM: Simplify KVM_REQ_EVENT/req_int_win handling
KVM: Optimize vcpu->requests slow path slightly
KVM: Move mmu reload out of line

 arch/x86/kvm/mmu.c |    2 ++
 arch/x86/kvm/x86.c |   73 ++++++++++++++++++++++++++++------------------------
 2 files changed, 41 insertions(+), 34 deletions(-)
Signature · 1 line
1.7.10.1
author
#1 lore raw

PATCH 1/3 KVM: Simplify KVM_REQ_EVENT/req_int_win handling

Put the KVM_REQ_EVENT block in the regular vcpu->requests if (), instead of its own little check.

---
 arch/x86/kvm/x86.c |   30 ++++++++++++++++--------------
 1 file changed, 16 insertions(+), 14 deletions(-)
arch/x86/kvm/x86.c +16 -14
index b78f89d..953e692 100644
@@ -5233,6 +5233,9 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
 		vcpu->run->request_interrupt_window;
 	bool req_immediate_exit = 0;
 
+	if (unlikely(req_int_win))
+		kvm_make_request(KVM_REQ_EVENT, vcpu);
+
 	if (vcpu->requests) {
 		if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu))
 			kvm_mmu_unload(vcpu);
@@ -5277,20 +5280,19 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
 			kvm_handle_pmu_event(vcpu);
 		if (kvm_check_request(KVM_REQ_PMI, vcpu))
 			kvm_deliver_pmi(vcpu);
-	}
-
-	if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win) {
-		inject_pending_event(vcpu);
-
-		/* enable NMI/IRQ window open exits if needed */
-		if (vcpu->arch.nmi_pending)
-			kvm_x86_ops->enable_nmi_window(vcpu);
-		else if (kvm_cpu_has_interrupt(vcpu) || req_int_win)
-			kvm_x86_ops->enable_irq_window(vcpu);
-
-		if (kvm_lapic_enabled(vcpu)) {
-			update_cr8_intercept(vcpu);
-			kvm_lapic_sync_to_vapic(vcpu);
+		if (kvm_check_request(KVM_REQ_EVENT, vcpu)) {
+			inject_pending_event(vcpu);
+
+			/* enable NMI/IRQ window open exits if needed */
+			if (vcpu->arch.nmi_pending)
+				kvm_x86_ops->enable_nmi_window(vcpu);
+			else if (kvm_cpu_has_interrupt(vcpu) || req_int_win)
+				kvm_x86_ops->enable_irq_window(vcpu);
+
+			if (kvm_lapic_enabled(vcpu)) {
+				update_cr8_intercept(vcpu);
+				kvm_lapic_sync_to_vapic(vcpu);
+			}
 		}
 	}
 
Signature · 1 line
1.7.10.1
author
#2 lore raw

PATCH 2/3 KVM: Optimize vcpu->requests slow path slightly

In reply to Avi Kivity

Instead of using a atomic operation per active request, use just one to get all requests at once, then check them with local ops. This probably isn't any faster, since simultaneous requests are rare, but it does reduce code size.

---
 arch/x86/kvm/x86.c |   33 ++++++++++++++++++---------------
 1 file changed, 18 insertions(+), 15 deletions(-)
arch/x86/kvm/x86.c +18 -15
index 953e692..c0209eb 100644
@@ -5232,55 +5232,58 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
 	bool req_int_win = !irqchip_in_kernel(vcpu->kvm) &&
 		vcpu->run->request_interrupt_window;
 	bool req_immediate_exit = 0;
+	ulong reqs;
 
 	if (unlikely(req_int_win))
 		kvm_make_request(KVM_REQ_EVENT, vcpu);
 
 	if (vcpu->requests) {
-		if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu))
+		reqs = xchg(&vcpu->requests, 0UL);
+
+		if (test_bit(KVM_REQ_MMU_RELOAD, &reqs))
 			kvm_mmu_unload(vcpu);
-		if (kvm_check_request(KVM_REQ_MIGRATE_TIMER, vcpu))
+		if (test_bit(KVM_REQ_MIGRATE_TIMER, &reqs))
 			__kvm_migrate_timers(vcpu);
-		if (kvm_check_request(KVM_REQ_CLOCK_UPDATE, vcpu)) {
+		if (test_bit(KVM_REQ_CLOCK_UPDATE, &reqs)) {
 			r = kvm_guest_time_update(vcpu);
 			if (unlikely(r))
 				goto out;
 		}
-		if (kvm_check_request(KVM_REQ_MMU_SYNC, vcpu))
+		if (test_bit(KVM_REQ_MMU_SYNC, &reqs))
 			kvm_mmu_sync_roots(vcpu);
-		if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu))
+		if (test_bit(KVM_REQ_TLB_FLUSH, &reqs))
 			kvm_x86_ops->tlb_flush(vcpu);
-		if (kvm_check_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu)) {
+		if (test_bit(KVM_REQ_REPORT_TPR_ACCESS, &reqs)) {
 			vcpu->run->exit_reason = KVM_EXIT_TPR_ACCESS;
 			r = 0;
 			goto out;
 		}
-		if (kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu)) {
+		if (test_bit(KVM_REQ_TRIPLE_FAULT, &reqs)) {
 			vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
 			r = 0;
 			goto out;
 		}
-		if (kvm_check_request(KVM_REQ_DEACTIVATE_FPU, vcpu)) {
+		if (test_bit(KVM_REQ_DEACTIVATE_FPU, &reqs)) {
 			vcpu->fpu_active = 0;
 			kvm_x86_ops->fpu_deactivate(vcpu);
 		}
-		if (kvm_check_request(KVM_REQ_APF_HALT, vcpu)) {
+		if (test_bit(KVM_REQ_APF_HALT, &reqs)) {
 			/* Page is swapped out. Do synthetic halt */
 			vcpu->arch.apf.halted = true;
 			r = 1;
 			goto out;
 		}
-		if (kvm_check_request(KVM_REQ_STEAL_UPDATE, vcpu))
+		if (test_bit(KVM_REQ_STEAL_UPDATE, &reqs))
 			record_steal_time(vcpu);
-		if (kvm_check_request(KVM_REQ_NMI, vcpu))
+		if (test_bit(KVM_REQ_NMI, &reqs))
 			process_nmi(vcpu);
 		req_immediate_exit =
-			kvm_check_request(KVM_REQ_IMMEDIATE_EXIT, vcpu);
-		if (kvm_check_request(KVM_REQ_PMU, vcpu))
+			test_bit(KVM_REQ_IMMEDIATE_EXIT, &reqs);
+		if (test_bit(KVM_REQ_PMU, &reqs))
 			kvm_handle_pmu_event(vcpu);
-		if (kvm_check_request(KVM_REQ_PMI, vcpu))
+		if (test_bit(KVM_REQ_PMI, &reqs))
 			kvm_deliver_pmi(vcpu);
-		if (kvm_check_request(KVM_REQ_EVENT, vcpu)) {
+		if (test_bit(KVM_REQ_EVENT, &reqs)) {
 			inject_pending_event(vcpu);
 
 			/* enable NMI/IRQ window open exits if needed */
Signature · 1 line
1.7.10.1
author
#3 lore raw

PATCH 3/3 KVM: Move mmu reload out of line

In reply to Avi Kivity

Currently we check that the mmu root exits before every entry. Use the existing KVM_REQ_MMU_RELOAD mechanism instead, by making it really reload the mmu, and by adding the request to mmu initialization code.

---
 arch/x86/kvm/mmu.c |    2 ++
 arch/x86/kvm/x86.c |   14 +++++++-------
 2 files changed, 9 insertions(+), 7 deletions(-)
arch/x86/kvm/mmu.c +2 -0
index 72102e0..7b73b4b 100644
@@ -2723,6 +2723,7 @@ static void mmu_free_roots(struct kvm_vcpu *vcpu)
 	kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
 	spin_unlock(&vcpu->kvm->mmu_lock);
 	vcpu->arch.mmu.root_hpa = INVALID_PAGE;
+	kvm_make_request(KVM_REQ_MMU_RELOAD, vcpu);
 }
 
 static int mmu_check_root(struct kvm_vcpu *vcpu, gfn_t root_gfn)
@@ -3470,6 +3471,7 @@ static int init_kvm_nested_mmu(struct kvm_vcpu *vcpu)
 
 static int init_kvm_mmu(struct kvm_vcpu *vcpu)
 {
+	kvm_make_request(KVM_REQ_MMU_RELOAD, vcpu);
 	if (mmu_is_nested(vcpu))
 		return init_kvm_nested_mmu(vcpu);
 	else if (tdp_enabled)
arch/x86/kvm/x86.c +7 -7
index c0209eb..946933a 100644
@@ -5240,8 +5240,14 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
 	if (vcpu->requests) {
 		reqs = xchg(&vcpu->requests, 0UL);
 
-		if (test_bit(KVM_REQ_MMU_RELOAD, &reqs))
+		if (test_bit(KVM_REQ_MMU_RELOAD, &reqs)) {
 			kvm_mmu_unload(vcpu);
+			r = kvm_mmu_reload(vcpu);
+			if (unlikely(r)) {
+				kvm_make_request(KVM_REQ_MMU_RELOAD, vcpu);
+				goto out;
+			}
+		}
 		if (test_bit(KVM_REQ_MIGRATE_TIMER, &reqs))
 			__kvm_migrate_timers(vcpu);
 		if (test_bit(KVM_REQ_CLOCK_UPDATE, &reqs)) {
@@ -5299,12 +5305,6 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
 		}
 	}
 
-	r = kvm_mmu_reload(vcpu);
-	if (unlikely(r)) {
-		kvm_x86_ops->cancel_injection(vcpu);
-		goto out;
-	}
-
 	preempt_disable();
 
 	kvm_x86_ops->prepare_guest_switch(vcpu);
Signature · 1 line
1.7.10.1