Skip to contentSkip to conversation

APICv-related fixes for inhibits and tracepoint

PATCH Applied Alejandro Jimenez 2 participants 5 messages Apr 18, 2024 – May 3, 2024

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

v2 cover APICv-related fixes for inhibits and tracepoint

v2: Add Sean's changes to [PATCH 1/2].

--

Patch 1 fixes an issue when avic=0 (current default) where APICV_INHIBIT_REASON_ABSENT remains set even after an in-kernel local APIC has been created. e.g. tracing the inhibition tracepoint shows:

<...>-196432 [247] ..... 70380.628931: kvm_apicv_inhibit_changed: set reason=2, inhibits=0x4 <...>-196432 [247] ..... 70380.628941: kvm_apicv_inhibit_changed: set reason=0, inhibits=0x5

and the reason=2 inhibit is not removed after the local APIC is created.

Patch 2 modifies the wording in the pi_irte_update tracepoint to make it clear that it is used by the posted interrupt implementation of both vendors. I have reservations about modifying the tracepoint output and breaking user scripts, but according to recent discussions tracepoints are not strictly a stable ABI, so I'd consider this minor change to avoid confusion around this area.

Thank you,
Alejandro

Alejandro Jimenez (2):
KVM: x86: Only set APICV_INHIBIT_REASON_ABSENT if APICv is enabled
KVM: x86: Remove VT-d mention in posted interrupt tracepoint

 arch/x86/kvm/trace.h |  4 ++--
 arch/x86/kvm/x86.c   | 13 ++++++-------
 2 files changed, 8 insertions(+), 9 deletions(-)

base-commit: 2d181d84af38146748042a6974c577fc46c3f1c3

Signature · 1 line
2.39.3
#1 lore raw

PATCH v2 1/2 KVM: x86: Only set APICV_INHIBIT_REASON_ABSENT if APICv is enabled

Use the APICv enablement status to determine if APICV_INHIBIT_REASON_ABSENT needs to be set, instead of unconditionally setting the reason during initialization.

Specifically, in cases where AVIC is disabled via module parameter or lack of hardware support, unconditionally setting an inhibit reason due to the absence of an in-kernel local APIC can lead to a scenario where the reason incorrectly remains set after a local APIC has been created by either KVM_CREATE_IRQCHIP or the enabling of KVM_CAP_IRQCHIP_SPLIT. This is because the helpers in charge of removing the inhibit return early if enable_apicv is not true, and therefore the bit remains set.

This leads to confusion as to the cause why APICv is not active, since an incorrect reason will be reported by tracepoints and/or a debugging tool that examines the currently set inhibit reasons.

Fixes ef8b4b720368 ("KVM: ensure APICv is considered inactive if there is no APIC")
---
 arch/x86/kvm/x86.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)
arch/x86/kvm/x86.c +6 -7
index 26288ca05364..09052ff5a9a0 100644
@@ -9995,15 +9995,14 @@ static void set_or_clear_apicv_inhibit(unsigned long *inhibits,
 
 static void kvm_apicv_init(struct kvm *kvm)
 {
-	unsigned long *inhibits = &kvm->arch.apicv_inhibit_reasons;
+	enum kvm_apicv_inhibit reason = enable_apicv ?
+						APICV_INHIBIT_REASON_ABSENT :
+						APICV_INHIBIT_REASON_DISABLE;
 
-	init_rwsem(&kvm->arch.apicv_update_lock);
-
-	set_or_clear_apicv_inhibit(inhibits, APICV_INHIBIT_REASON_ABSENT, true);
+	set_or_clear_apicv_inhibit(&kvm->arch.apicv_inhibit_reasons, reason,
+				   true);
 
-	if (!enable_apicv)
-		set_or_clear_apicv_inhibit(inhibits,
-					   APICV_INHIBIT_REASON_DISABLE, true);
+	init_rwsem(&kvm->arch.apicv_update_lock);
 }
 
 static void kvm_sched_yield(struct kvm_vcpu *vcpu, unsigned long dest_id)
Signature · 1 line
2.39.3
#2 lore raw

Re: [PATCH v2 1/2] KVM: x86: Only set APICV_INHIBIT_REASON_ABSENT if APICv is enabled

Quoted 12 lines · Alejandro Jimenez

Use the APICv enablement status to determine if APICV_INHIBIT_REASON_ABSENT needs to be set, instead of unconditionally setting the reason during initialization.

Specifically, in cases where AVIC is disabled via module parameter or lack of hardware support, unconditionally setting an inhibit reason due to the absence of an in-kernel local APIC can lead to a scenario where the reason incorrectly remains set after a local APIC has been created by either KVM_CREATE_IRQCHIP or the enabling of KVM_CAP_IRQCHIP_SPLIT. This is because the helpers in charge of removing the inhibit return early if enable_apicv is not true, and therefore the bit remains set.

Quoted 6 lines

This leads to confusion as to the cause why APICv is not active, since an incorrect reason will be reported by tracepoints and/or a debugging tool that examines the currently set inhibit reasons.

Fixes ef8b4b720368 ("KVM: ensure APICv is considered inactive if there is no APIC")

Heh, no need, I just provided review feedback.

Quoted 10 lines
---
 arch/x86/kvm/x86.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)
arch/x86/kvm/x86.c +0 -0
index 26288ca05364..09052ff5a9a0 100644

arch/x86/kvm/x86.c static void set_or_clear_apicv_inhibit(unsigned long *inhibits, · line 10000

Quoted 7 lines
arch/x86/kvm/x86.c +3 -1
@@ -9995,15 +9995,14 @@ static void set_or_clear_apicv_inhibit(unsigned long *inhibits,
 
 static void kvm_apicv_init(struct kvm *kvm)
 {
-	unsigned long *inhibits = &kvm->arch.apicv_inhibit_reasons;
+	enum kvm_apicv_inhibit reason = enable_apicv ?
+						APICV_INHIBIT_REASON_ABSENT :
+						APICV_INHIBIT_REASON_DISABLE;

Just let this poke out, the 80 char limit is a soft limit, where "soft" is fairly "hard" in KVM", but there are still legitimate situations where running past 80 is yields more readable code, and IMO this is one of them.

arch/x86/kvm/x86.c static void set_or_clear_apicv_inhibit(unsigned long *inhibits, · line 10003

Quoted 5 lines
arch/x86/kvm/x86.c +2 -3
-	init_rwsem(&kvm->arch.apicv_update_lock);
-
-	set_or_clear_apicv_inhibit(inhibits, APICV_INHIBIT_REASON_ABSENT, true);
+	set_or_clear_apicv_inhibit(&kvm->arch.apicv_inhibit_reasons, reason,
+				   true);

Same here.

No need for a v3, I'll fixup when applying.

Quoted 11 lines
arch/x86/kvm/x86.c +1 -3
-	if (!enable_apicv)
-		set_or_clear_apicv_inhibit(inhibits,
-					   APICV_INHIBIT_REASON_DISABLE, true);
+	init_rwsem(&kvm->arch.apicv_update_lock);
 }
 
 static void kvm_sched_yield(struct kvm_vcpu *vcpu, unsigned long dest_id)
Signature · 1 line
2.39.3
#3 lore raw

PATCH v2 2/2 KVM: x86: Remove VT-d mention in posted interrupt tracepoint

In reply to Alejandro Jimenez

The kvm_pi_irte_update tracepoint is called from both SVM and VMX vendor code, and while the "posted interrupt" naming is also adopted by SVM in several places, VT-d specifically refers to Intel's "Virtualization Technology for Directed I/O".

---
 arch/x86/kvm/trace.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kvm/trace.h +2 -2
index c6b4b1728006..9d0b02ef307e 100644
@@ -1074,7 +1074,7 @@ TRACE_EVENT(kvm_smm_transition,
 );
 
 /*
- * Tracepoint for VT-d posted-interrupts.
+ * Tracepoint for VT-d posted-interrupts and AMD-Vi Guest Virtual APIC.
  */
 TRACE_EVENT(kvm_pi_irte_update,
 	TP_PROTO(unsigned int host_irq, unsigned int vcpu_id,
@@ -1100,7 +1100,7 @@ TRACE_EVENT(kvm_pi_irte_update,
 		__entry->set		= set;
 	),
 
-	TP_printk("VT-d PI is %s for irq %u, vcpu %u, gsi: 0x%x, "
+	TP_printk("PI is %s for irq %u, vcpu %u, gsi: 0x%x, "
 		  "gvec: 0x%x, pi_desc_addr: 0x%llx",
 		  __entry->set ? "enabled and being updated" : "disabled",
 		  __entry->host_irq,
Signature · 1 line
2.39.3
maintainer Applied
#4 lore raw
In reply to Alejandro Jimenez
Quoted 9 lines · Alejandro Jimenez

v2: Add Sean's changes to [PATCH 1/2].

--

Patch 1 fixes an issue when avic=0 (current default) where APICV_INHIBIT_REASON_ABSENT remains set even after an in-kernel local APIC has been created. e.g. tracing the inhibition tracepoint shows:

[...]

Applied to kvm-x86 misc, with the previously mentioned fixups, thanks!

[1/2] KVM: x86: Only set APICV_INHIBIT_REASON_ABSENT if APICv is enabled

      https://github.com/kvm-x86/linux/commit/6982b34c21cb
[2/2] KVM: x86: Remove VT-d mention in posted interrupt tracepoint
      https://github.com/kvm-x86/linux/commit/51937f2aae18

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