Skip to contentSkip to conversation

KVM: x86: Track kvm_vcpu_arch.pending_ioapic_eoi as a u8 to drop dead BUG_ON()

PATCH Applied Sean Christopherson 3 participants 4 messages Aug 6 – Sep 21

Subsystems (3)
lore
Share & tools
lore

Sean Christopherson

author maintainer
#0 lore raw

PATCH v2 1/1 KVM: x86: Track kvm_vcpu_arch.pending_ioapic_eoi as a u8 to drop dead BUG_ON()

Track the vector associated with KVM_EXIT_IOAPIC_EOI as a u8, as the vector is zero-initialized and is only ever set to a legal vector. I.e. unlike many of the other vector variables in KVM, it can't be -1 (and can't be greater than 255, though that should hold true for all vectors in KVM).

Drop the now fully dead BUG_ON(), which aside from being completely unnecessary, was also useless, as it wouldn't guard against a negative value, i.e. wouldn't detect KVM's magic -1 value.

For all intents and purposes, no functional change intended.

---

v2: Use a u8 to make it impossible for the test to be out-of-bounds. [Paolo]

v1: https://lore.kernel.org/all/[email protected]

 arch/x86/include/asm/kvm_host.h | 2 +-
 arch/x86/kvm/lapic.c            | 4 ++--
 arch/x86/kvm/lapic.h            | 2 +-
 arch/x86/kvm/vmx/vmx.c          | 3 +--
 arch/x86/kvm/x86.c              | 1 -
 5 files changed, 5 insertions(+), 7 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -1
index 283847619ff8..9614314fd237 100644
@@ -973,7 +973,7 @@ struct kvm_vcpu_arch {
 		bool pv_unhalted;
 	} pv;
 
-	int pending_ioapic_eoi;
+	u8 pending_ioapic_eoi;
 	int pending_external_vector;
 	int highest_stale_pending_ioapic_eoi;
 
arch/x86/kvm/lapic.c +2 -2
index 586a5cee29e1..87bb8e5f4ca1 100644
@@ -1552,7 +1552,7 @@ static bool kvm_ioapic_handles_vector(struct kvm_lapic *apic, int vector)
 	return test_bit(vector, apic->vcpu->arch.ioapic_handled_vectors);
 }
 
-static void kvm_ioapic_send_eoi(struct kvm_lapic *apic, int vector)
+static void kvm_ioapic_send_eoi(struct kvm_lapic *apic, u8 vector)
 {
 	int __maybe_unused trigger_mode;
 
@@ -1622,7 +1622,7 @@ static int apic_set_eoi(struct kvm_lapic *apic)
  * this interface assumes a trap-like exit, which has already finished
  * desired side effect including vISR and vPPR update.
  */
-void kvm_apic_set_eoi_accelerated(struct kvm_vcpu *vcpu, int vector)
+void kvm_apic_set_eoi_accelerated(struct kvm_vcpu *vcpu, u8 vector)
 {
 	struct kvm_lapic *apic = vcpu->arch.apic;
 
arch/x86/kvm/lapic.h +1 -1
index bd1098c89d99..db071d0b172d 100644
@@ -177,7 +177,7 @@ u64 kvm_get_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu);
 void kvm_set_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu, u64 data);
 
 void kvm_apic_write_nodecode(struct kvm_vcpu *vcpu, u32 offset);
-void kvm_apic_set_eoi_accelerated(struct kvm_vcpu *vcpu, int vector);
+void kvm_apic_set_eoi_accelerated(struct kvm_vcpu *vcpu, u8 vector);
 
 int kvm_lapic_set_vapic_addr(struct kvm_vcpu *vcpu, gpa_t vapic_addr);
 void kvm_lapic_sync_from_vapic(struct kvm_vcpu *vcpu);
arch/x86/kvm/vmx/vmx.c +1 -2
index e3bfe6aca1a0..c1fdfccd20fa 100644
@@ -5886,10 +5886,9 @@ static int handle_apic_access(struct kvm_vcpu *vcpu)
 static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
 {
 	unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
-	int vector = exit_qualification & 0xff;
 
 	/* EOI-induced VM exit is trap-like and thus no need to adjust IP */
-	kvm_apic_set_eoi_accelerated(vcpu, vector);
+	kvm_apic_set_eoi_accelerated(vcpu, exit_qualification & 0xff);
 	return 1;
 }
 
arch/x86/kvm/x86.c +0 -1
index d94b59140c45..8b23f342a1fa 100644
@@ -8151,7 +8151,6 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
 		if (kvm_check_request(KVM_REQ_NMI, vcpu))
 			process_nmi(vcpu);
 		if (kvm_check_request(KVM_REQ_IOAPIC_EOI_EXIT, vcpu)) {
-			BUG_ON(vcpu->arch.pending_ioapic_eoi > 255);
 			if (test_bit(vcpu->arch.pending_ioapic_eoi,
 				     vcpu->arch.ioapic_handled_vectors)) {
 				vcpu->run->exit_reason = KVM_EXIT_IOAPIC_EOI;

base-commit: a806d364ef288a6443a1337820ea8410a7ccc6b3

Signature · 1 line
2.55.0.679.g6767b8d81c-goog
maintainer Reviewed-by on v2 1/1
#1 lore raw
Quoted 6 lines · Sean Christopherson

Track the vector associated with KVM_EXIT_IOAPIC_EOI as a u8, as the vector is zero-initialized and is only ever set to a legal vector. I.e. unlike many of the other vector variables in KVM, it can't be -1 (and can't be greater than 255, though that should hold true for all vectors in KVM).

Quoted 7 lines

Drop the now fully dead BUG_ON(), which aside from being completely unnecessary, was also useless, as it wouldn't guard against a negative value, i.e. wouldn't detect KVM's magic -1 value.

For all intents and purposes, no functional change intended.

Paolo

Quoted 95 lines

---

v2: Use a u8 to make it impossible for the test to be out-of-bounds. [Paolo]

v1: https://lore.kernel.org/all/[email protected]

 arch/x86/include/asm/kvm_host.h | 2 +-
 arch/x86/kvm/lapic.c            | 4 ++--
 arch/x86/kvm/lapic.h            | 2 +-
 arch/x86/kvm/vmx/vmx.c          | 3 +--
 arch/x86/kvm/x86.c              | 1 -
 5 files changed, 5 insertions(+), 7 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -1
index 283847619ff8..9614314fd237 100644
@@ -973,7 +973,7 @@ struct kvm_vcpu_arch {
                bool pv_unhalted;
        } pv;
 
-       int pending_ioapic_eoi;
+       u8 pending_ioapic_eoi;
        int pending_external_vector;
        int highest_stale_pending_ioapic_eoi;
 
arch/x86/kvm/lapic.c +2 -2
index 586a5cee29e1..87bb8e5f4ca1 100644
@@ -1552,7 +1552,7 @@ static bool kvm_ioapic_handles_vector(struct kvm_lapic *apic, int vector)
        return test_bit(vector, apic->vcpu->arch.ioapic_handled_vectors);
 }
 
-static void kvm_ioapic_send_eoi(struct kvm_lapic *apic, int vector)
+static void kvm_ioapic_send_eoi(struct kvm_lapic *apic, u8 vector)
 {
        int __maybe_unused trigger_mode;
 
@@ -1622,7 +1622,7 @@ static int apic_set_eoi(struct kvm_lapic *apic)
  * this interface assumes a trap-like exit, which has already finished
  * desired side effect including vISR and vPPR update.
  */
-void kvm_apic_set_eoi_accelerated(struct kvm_vcpu *vcpu, int vector)
+void kvm_apic_set_eoi_accelerated(struct kvm_vcpu *vcpu, u8 vector)
 {
        struct kvm_lapic *apic = vcpu->arch.apic;
 
arch/x86/kvm/lapic.h +1 -1
index bd1098c89d99..db071d0b172d 100644
@@ -177,7 +177,7 @@ u64 kvm_get_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu);
 void kvm_set_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu, u64 data);
 
 void kvm_apic_write_nodecode(struct kvm_vcpu *vcpu, u32 offset);
-void kvm_apic_set_eoi_accelerated(struct kvm_vcpu *vcpu, int vector);
+void kvm_apic_set_eoi_accelerated(struct kvm_vcpu *vcpu, u8 vector);
 
 int kvm_lapic_set_vapic_addr(struct kvm_vcpu *vcpu, gpa_t vapic_addr);
 void kvm_lapic_sync_from_vapic(struct kvm_vcpu *vcpu);
arch/x86/kvm/vmx/vmx.c +1 -2
index e3bfe6aca1a0..c1fdfccd20fa 100644
@@ -5886,10 +5886,9 @@ static int handle_apic_access(struct kvm_vcpu *vcpu)
 static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
 {
        unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
-       int vector = exit_qualification & 0xff;
 
        /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
-       kvm_apic_set_eoi_accelerated(vcpu, vector);
+       kvm_apic_set_eoi_accelerated(vcpu, exit_qualification & 0xff);
        return 1;
 }
 
arch/x86/kvm/x86.c +0 -1
index d94b59140c45..8b23f342a1fa 100644
@@ -8151,7 +8151,6 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
                if (kvm_check_request(KVM_REQ_NMI, vcpu))
                        process_nmi(vcpu);
                if (kvm_check_request(KVM_REQ_IOAPIC_EOI_EXIT, vcpu)) {
-                       BUG_ON(vcpu->arch.pending_ioapic_eoi > 255);
                        if (test_bit(vcpu->arch.pending_ioapic_eoi,
                                     vcpu->arch.ioapic_handled_vectors)) {
                                vcpu->run->exit_reason = KVM_EXIT_IOAPIC_EOI;

base-commit: a806d364ef288a6443a1337820ea8410a7ccc6b3 --
2.55.0.679.g6767b8d81c-goog

Reviewed-by on v2 1/1
#2 lore raw
In reply to Sean Christopherson
Quoted 5 lines · Sean Christopherson

Track the vector associated with KVM_EXIT_IOAPIC_EOI as a u8, as the vector is zero-initialized and is only ever set to a legal vector. I.e. unlike many of the other vector variables in KVM, it can't be -1 (and can't be greater than 255, though that should hold true for all vectors in KVM).

Quoted 9 lines

Drop the now fully dead BUG_ON(), which aside from being completely unnecessary, was also useless, as it wouldn't guard against a negative value, i.e. wouldn't detect KVM's magic -1 value.

For all intents and purposes, no functional change intended.

author maintainer Applied
#3 lore raw
In reply to Sean Christopherson
Quoted 10 lines · Sean Christopherson

Track the vector associated with KVM_EXIT_IOAPIC_EOI as a u8, as the vector is zero-initialized and is only ever set to a legal vector. I.e. unlike many of the other vector variables in KVM, it can't be -1 (and can't be greater than 255, though that should hold true for all vectors in KVM).

Drop the now fully dead BUG_ON(), which aside from being completely unnecessary, was also useless, as it wouldn't guard against a negative value, i.e. wouldn't detect KVM's magic -1 value.

[...]

Applied to kvm-x86 misc, thanks!

[1/1] KVM: x86: Track kvm_vcpu_arch.pending_ioapic_eoi as a u8 to drop dead BUG_ON()

      https://github.com/kvm-x86/linux/commit/926a9ea9bdf0

--
https://github.com/kvm-x86/linux/tree/next