Skip to contentSkip to conversation

kvm: x86: remove vmx_vm_has_apicv() outside of hwapic_isr_update()

PATCH In review Tiejun Chen 2 participants 6 messages Dec 1, 2014 – Dec 22, 2014

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kvm: x86: remove vmx_vm_has_apicv() outside of hwapic_isr_update()

In most cases calling hwapic_isr_update(), actually we always check if kvm_apic_vid_enabled() == 1, and also actually, kvm_apic_vid_enabled()

    -> kvm_x86_ops->vm_has_apicv()
        -> vmx_vm_has_apicv() or '0' in svm case

So its unnecessary to recall this inside hwapic_isr_update(), here just remove vmx_vm_has_apicv() out and follow others.

---
 arch/x86/kvm/lapic.c | 3 ++-
 arch/x86/kvm/vmx.c   | 3 ---
 2 files changed, 2 insertions(+), 4 deletions(-)
arch/x86/kvm/lapic.c +2 -1
index e0e5642..2ddc426 100644
@@ -1739,7 +1739,8 @@ void kvm_apic_post_state_restore(struct kvm_vcpu *vcpu,
 	if (kvm_x86_ops->hwapic_irr_update)
 		kvm_x86_ops->hwapic_irr_update(vcpu,
 				apic_find_highest_irr(apic));
-	kvm_x86_ops->hwapic_isr_update(vcpu->kvm, apic_find_highest_isr(apic));
+	if (kvm_apic_vid_enabled(vcpu->kvm))
+	    kvm_x86_ops->hwapic_isr_update(vcpu->kvm, apic_find_highest_isr(apic));
 	kvm_make_request(KVM_REQ_EVENT, vcpu);
 	kvm_rtc_eoi_tracking_restore_one(vcpu);
 }
arch/x86/kvm/vmx.c +0 -3
index 6a951d8..f0c16a9 100644
@@ -7406,9 +7406,6 @@ static void vmx_hwapic_isr_update(struct kvm *kvm, int isr)
 	u16 status;
 	u8 old;
 
-	if (!vmx_vm_has_apicv(kvm))
-		return;
-
 	if (isr == -1)
 		isr = 0;
 
Signature · 1 line
1.9.1
maintainer
#1 lore raw
Quoted 8 lines · Tiejun Chen

In most cases calling hwapic_isr_update(), actually we always check if kvm_apic_vid_enabled() == 1, and also actually, kvm_apic_vid_enabled()

    -> kvm_x86_ops->vm_has_apicv()
        -> vmx_vm_has_apicv() or '0' in svm case

So its unnecessary to recall this inside hwapic_isr_update(), here just remove vmx_vm_has_apicv() out and follow others.

If you want to do this, please NULL out the function pointer instead, as KVM already does for hwapic_irr_update.

Paolo

Quoted 35 lines
---
 arch/x86/kvm/lapic.c | 3 ++-
 arch/x86/kvm/vmx.c   | 3 ---
 2 files changed, 2 insertions(+), 4 deletions(-)
arch/x86/kvm/lapic.c +2 -1
index e0e5642..2ddc426 100644
@@ -1739,7 +1739,8 @@ void kvm_apic_post_state_restore(struct kvm_vcpu *vcpu,
 	if (kvm_x86_ops->hwapic_irr_update)
 		kvm_x86_ops->hwapic_irr_update(vcpu,
 				apic_find_highest_irr(apic));
-	kvm_x86_ops->hwapic_isr_update(vcpu->kvm, apic_find_highest_isr(apic));
+	if (kvm_apic_vid_enabled(vcpu->kvm))
+	    kvm_x86_ops->hwapic_isr_update(vcpu->kvm, apic_find_highest_isr(apic));
 	kvm_make_request(KVM_REQ_EVENT, vcpu);
 	kvm_rtc_eoi_tracking_restore_one(vcpu);
 }
arch/x86/kvm/vmx.c +0 -3
index 6a951d8..f0c16a9 100644
@@ -7406,9 +7406,6 @@ static void vmx_hwapic_isr_update(struct kvm *kvm, int isr)
 	u16 status;
 	u8 old;
 
-	if (!vmx_vm_has_apicv(kvm))
-		return;
-
 	if (isr == -1)
 		isr = 0;
 
#2 lore raw
Quoted 9 lines · Paolo Bonzini
Quoted 6 lines · Tiejun Chen

In most cases calling hwapic_isr_update(), actually we always check if kvm_apic_vid_enabled() == 1, and also actually, kvm_apic_vid_enabled()

     -> kvm_x86_ops->vm_has_apicv()
         -> vmx_vm_has_apicv() or '0' in svm case
Quoted 5 lines
Quoted 2 lines

So its unnecessary to recall this inside hwapic_isr_update(), here just remove vmx_vm_has_apicv() out and follow others.

If you want to do this, please NULL out the function pointer instead, as KVM already does for hwapic_irr_update.

Are you saying something below?

if (enable_apicv)

	...
else {
	kvm_x86_ops->hwapic_irr_update = NULL;

But there's a little bit difference to NULL out hwapic_isr_update(),

static int vmx_vm_has_apicv(struct kvm *kvm)

{
    return enable_apicv && irqchip_in_kernel(kvm);
}

Yes, I can do something like this,

static __init int hadware_setup(void)

{
	...
	if (enable_apicv) {
		...
		if (!irqchip_in_kernel(kvm))
			kvm_x86_ops->hwapic_isr_update = NULL;	
	} else {
		...
		kvm_x86_ops->hwapic_isr_update = NULL;

But this means we have to revise hadware_setup() to get 'kvm' inside, then rebase other callers to hwapic_isr_update(), is it really good?

Here what I will intend to do is trying to reduce some cost (reduplicate check) with a little code, so its may not be worth changing much more.

Tiejun

Quoted 43 lines

Paolo

Quoted 26 lines
---
  arch/x86/kvm/lapic.c | 3 ++-
  arch/x86/kvm/vmx.c   | 3 ---
  2 files changed, 2 insertions(+), 4 deletions(-)
arch/x86/kvm/lapic.c +2 -1
index e0e5642..2ddc426 100644
@@ -1739,7 +1739,8 @@ void kvm_apic_post_state_restore(struct kvm_vcpu *vcpu,
  	if (kvm_x86_ops->hwapic_irr_update)
  		kvm_x86_ops->hwapic_irr_update(vcpu,
  				apic_find_highest_irr(apic));
-	kvm_x86_ops->hwapic_isr_update(vcpu->kvm, apic_find_highest_isr(apic));
+	if (kvm_apic_vid_enabled(vcpu->kvm))
+	    kvm_x86_ops->hwapic_isr_update(vcpu->kvm, apic_find_highest_isr(apic));
  	kvm_make_request(KVM_REQ_EVENT, vcpu);
  	kvm_rtc_eoi_tracking_restore_one(vcpu);
  }
arch/x86/kvm/vmx.c +0 -0
index 6a951d8..f0c16a9 100644

arch/x86/kvm/vmx.c static void vmx_hwapic_isr_update(struct kvm *kvm, int isr) · line 7411

Quoted 9 lines
arch/x86/kvm/vmx.c +0 -3
@@ -7406,9 +7406,6 @@ static void vmx_hwapic_isr_update(struct kvm *kvm, int isr)
  	u16 status;
  	u8 old;
 
-	if (!vmx_vm_has_apicv(kvm))
-		return;
-
  	if (isr == -1)
  		isr = 0;
 

--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

maintainer
#3 lore raw
Quoted 7 lines · Tiejun

Are you saying something below?

if (enable_apicv)

    ...
else {
    kvm_x86_ops->hwapic_irr_update = NULL;

Yes.

Quoted 1 line

But this means we have to revise hadware_setup() to get 'kvm' inside,

This would not even be possible, since hardware_setup() is only called once.

However, for the only caller of hwapic_isr_update (but presumably all of them, as is the case for hwapic_irr_update), you already know that irqchip_in_kernel(kvm) is true. You are in kvm_apic_post_state_restore, which takes a kvm_lapic_state, and no lapic exists if !irqchip_in_kernel(kvm).

(Yes, irqchip_in_kernel) is a bit weird and tests pic_irqchip(kvm) instead, but it's the same. It tests pic_irqchip(kvm) only because the LAPIC is per-cpu and irqchip_in_kernel takes a struct kvm).

So it's possible to NULL out hwapic_isr_update in hardware_setup. It simply shouldn't happen that you call hwapic_isr_update without the in-kernel irqchip. The kernel knows nothing about ISR/IRR without the in-kernel irqchip.

Paolo

Quoted 1 line

then rebase other callers to hwapic_isr_update(), is it really good?

#4 lore raw
Quoted 11 lines · Paolo Bonzini
Quoted 7 lines · Tiejun

Are you saying something below?

if (enable_apicv)

     ...
else {
     kvm_x86_ops->hwapic_irr_update = NULL;
Quoted 5 lines

Yes.

Quoted 1 line

But this means we have to revise hadware_setup() to get 'kvm' inside,

This would not even be possible, since hardware_setup() is only called once.

Yeah.

Quoted 3 lines

However, for the only caller of hwapic_isr_update (but presumably all of them, as is the case for hwapic_irr_update), you already know that

There are two other callers to hwapic_isr_update(), apic_set_isr() and apic_clear_isr(), but I think they still work here.

Quoted 4 lines

irqchip_in_kernel(kvm) is true. You are in kvm_apic_post_state_restore, which takes a kvm_lapic_state, and no lapic exists if !irqchip_in_kernel(kvm).

Quoted 8 lines

(Yes, irqchip_in_kernel) is a bit weird and tests pic_irqchip(kvm) instead, but it's the same. It tests pic_irqchip(kvm) only because the LAPIC is per-cpu and irqchip_in_kernel takes a struct kvm).

So it's possible to NULL out hwapic_isr_update in hardware_setup. It simply shouldn't happen that you call hwapic_isr_update without the in-kernel irqchip. The kernel knows nothing about ISR/IRR without the in-kernel irqchip.

Thanks for your kind elaboration which always benefits me.

What about this revision as follows?

kvm: x86: vmx: NULL out hwapic_isr_update() in case of !enable_apicv

In most cases calling hwapic_isr_update(), we always check if kvm_apic_vid_enabled() == 1, but actually, kvm_apic_vid_enabled()

    -> kvm_x86_ops->vm_has_apicv()
        -> vmx_vm_has_apicv() or '0' in svm case
            -> return enable_apicv && irqchip_in_kernel(kvm)

So its a little cost to recall vmx_vm_has_apicv() inside hwapic_isr_update(), here just NULL out hwapic_isr_update() in case of !enable_apicv inside hardware_setup() then make all related stuffs follow this. Note we don't check this under that condition of irqchip_in_kernel() since we should make sure definitely any caller don't work without in-kernel irqchip.

---
 arch/x86/kvm/lapic.c | 7 ++++---
 arch/x86/kvm/vmx.c   | 4 +---
 2 files changed, 5 insertions(+), 6 deletions(-)
arch/x86/kvm/lapic.c +2 -2
index 4f0c0b9..eb4cd5e 100644
@@ -402,7 +402,7 @@ static inline void apic_set_isr(int vec, struct kvm_lapic *apic)
         * because the processor can modify ISR under the hood.  Instead
         * just set SVI.
         */
-       if (unlikely(kvm_apic_vid_enabled(vcpu->kvm)))
+       if (unlikely(kvm_x86_ops->hwapic_isr_update))
                kvm_x86_ops->hwapic_isr_update(vcpu->kvm, vec);
        else {
                ++apic->isr_count;
@@ -450,7 +450,7 @@ static inline void apic_clear_isr(int vec, struct kvm_lapic *apic)
         * on the other hand isr_count and highest_isr_cache are unused
         * and must be left alone.
         */
-       if (unlikely(kvm_apic_vid_enabled(vcpu->kvm)))
+       if (unlikely(kvm_x86_ops->hwapic_isr_update))
                kvm_x86_ops->hwapic_isr_update(vcpu->kvm,
 
apic_find_highest_isr(apic));
        else {
@@ -1742,7 +1742,8 @@ void kvm_apic_post_state_restore(struct kvm_vcpu *vcpu,
        if (kvm_x86_ops->hwapic_irr_update)
                kvm_x86_ops->hwapic_irr_update(vcpu,
                                apic_find_highest_irr(apic));
-       kvm_x86_ops->hwapic_isr_update(vcpu->kvm, apic_find_highest_isr(apic));
+       if (kvm_x86_ops->hwapic_isr_update)
+           kvm_x86_ops->hwapic_isr_update(vcpu->kvm, apic_find_highest_isr(apic));
        kvm_make_request(KVM_REQ_EVENT, vcpu);
        kvm_rtc_eoi_tracking_restore_one(vcpu);

}

arch/x86/kvm/vmx.c +1 -3
index 96c84a8..e378dff 100644
@@ -5895,6 +5895,7 @@ static __init int hardware_setup(void)
                kvm_x86_ops->update_cr8_intercept = NULL;
        else {
                kvm_x86_ops->hwapic_irr_update = NULL;
+               kvm_x86_ops->hwapic_isr_update = NULL;
                kvm_x86_ops->deliver_posted_interrupt = NULL;
                kvm_x86_ops->sync_pir_to_irr = vmx_sync_pir_to_irr_dummy;
        }
@@ -7471,9 +7472,6 @@ static void vmx_hwapic_isr_update(struct kvm *kvm, int isr)
        u16 status;
        u8 old;
 
-       if (!vmx_vm_has_apicv(kvm))
-               return;
-
        if (isr == -1)
                isr = 0;
 
Signature · 6 lines
1.9.1

I can send out as a patch if we have on any objections.

Thanks
Tiejun
maintainer
#5 lore raw
Quoted 1 line · Tiejun

I can send out as a patch if we have on any objections.

No problem, I will apply it to kvm/queue.

Paolo