Skip to contentSkip to conversation

KVM: PPC: Book3S HV: Avoid spurious interrupts caused by LPCR_MER bit

PATCH Superseded Gautam Menghani 5 participants 9 messages Sep 21 – Oct 5

Subsystems (4)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 KVM: PPC: Book3S HV: Avoid spurious interrupts caused by LPCR_MER bit

A huge number of spurious interrupts can be seen immediately after a KVM on PowerNV guest boots up in XIVE mode.

$ cat /proc/interrupts  | grep SPU
SPU:     223705     192439     273526     147623   Spurious interrupts

This bug was introduced by commit ecd10702baae5 ("KVM: PPC: Book3S HV:
Handle pending exceptions on guest entry with MSR_EE"). The root cause is that once LPCR_MER bit is set, it is supposed to be reset by software. But when a vCPU starts running with LPCR_MER set, the vCPU does not exit back to the host until the decrementer expires or there is an hcall, etc. This is because KVM on PowerNV guests have support for native XIVE, so they are not dependent on host for interrupt emulation. Due to this behaviour, a huge number of spurious interrupts are seen since LPCR_MER continues to be set and LPCR_MER cannot be reset until the vCPU exits to the host.

Fix this behaviour by not using the LPCR_MER bit whenever native XIVE is available (currently in case of KVM on PowerNV only), as the XIVE hardware can present interrupts to the KVM guest vCPU directly. So the LPCR_MER functionality is not required. This reduces the number of spurious interrupts drastically.

Fixes ecd10702baae5 ("KVM: PPC: Book3S HV: Handle pending exceptions on guest entry with MSR_EE")

---
v3:
1. Continue the use of LPCR_MER when kernel-irqchip=off (Sashiko)

v2:
1. Handle the case where xive_interrupt_pending() is true and also the external exception bit is set. (Narayana)

 arch/powerpc/include/asm/kvm_ppc.h | 7 +++++++
 arch/powerpc/kvm/book3s_hv.c       | 2 +-
 2 files changed, 8 insertions(+), 1 deletion(-)
arch/powerpc/include/asm/kvm_ppc.h +7 -0
index 169ea6a7fbad..580ad2548c2b 100644
@@ -747,6 +747,11 @@ static inline int kvmppc_xive_enabled(struct kvm_vcpu *vcpu)
 	return vcpu->arch.irq_type == KVMPPC_IRQ_XIVE;
 }
 
+static inline bool kvmppc_xive_native_enabled(struct kvm *kvm)
+{
+	return kvm->arch.xive_devices.native;
+}
+
 extern int kvmppc_xive_native_connect_vcpu(struct kvm_device *dev,
 					   struct kvm_vcpu *vcpu, u32 cpu);
 extern void kvmppc_xive_native_cleanup_vcpu(struct kvm_vcpu *vcpu);
@@ -782,6 +787,8 @@ static inline bool kvmppc_xive_rearm_escalation(struct kvm_vcpu *vcpu) { return
 
 static inline int kvmppc_xive_enabled(struct kvm_vcpu *vcpu)
 	{ return 0; }
+static inline bool kvmppc_xive_native_enabled(struct kvm *kvm) { return false; }
+
 static inline int kvmppc_xive_native_connect_vcpu(struct kvm_device *dev,
 			  struct kvm_vcpu *vcpu, u32 cpu) { return -EBUSY; }
 static inline void kvmppc_xive_native_cleanup_vcpu(struct kvm_vcpu *vcpu) { }
arch/powerpc/kvm/book3s_hv.c +1 -1
index dbac3573b2c8..cb2bb29451a7 100644
@@ -4980,7 +4980,7 @@ int kvmhv_run_single_vcpu(struct kvm_vcpu *vcpu, u64 time_limit,
 			if (!kvmhv_on_pseries() && (__kvmppc_get_msr_hv(vcpu) & MSR_EE))
 				kvmppc_inject_interrupt_hv(vcpu,
 							   BOOK3S_INTERRUPT_EXTERNAL, 0);
-			else
+			else if (!kvmppc_xive_native_enabled(vcpu->kvm))
 				lpcr |= LPCR_MER;
 		} else {
 			/*
Signature · 1 line
2.55.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] kvm: ppc: lost LPCR_MER interrupts for XICS due to XIVE state leak --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] kvm: ppc: lost LPCR_MER interrupts for XICS due to XIVE state leak --

commit 215dda3f95780a965ce67c9c452b6f7d1948e486
Author: Gautam Menghani <[email protected]>

KVM: PPC: Book3S HV: Avoid spurious interrupts caused by LPCR_MER bit

This commit avoids spurious interrupts for KVM on PowerNV guests by not setting the LPCR_MER bit when native XIVE is available. It introduces a new helper, kvmppc_xive_native_enabled(), to check for native XIVE and skips setting LPCR_MER if it evaluates to true.

Quoted 13 lines
arch/powerpc/include/asm/kvm_ppc.h +5 -0
index 169ea6a7fbad4..580ad2548c2bf 100644
@@ -747,6 +747,11 @@ static inline int kvmppc_xive_enabled(struct kvm_vcpu *vcpu)
 	return vcpu->arch.irq_type == KVMPPC_IRQ_XIVE;
 }
 
+static inline bool kvmppc_xive_native_enabled(struct kvm *kvm)
+{
+	return kvm->arch.xive_devices.native;
+}
+

[ ... ]

arch/powerpc/kvm/book3s_hv.c int kvmhv_run_single_vcpu(struct kvm_vcpu *vcpu, u64 time_limit, · line 4986

Quoted 13 lines
arch/powerpc/kvm/book3s_hv.c +1 -1
index dbac3573b2c89..cb2bb29451a7f 100644
@@ -4980,7 +4980,7 @@ int kvmhv_run_single_vcpu(struct kvm_vcpu *vcpu, u64 time_limit,
 			if (!kvmhv_on_pseries() && (__kvmppc_get_msr_hv(vcpu) & MSR_EE))
 				kvmppc_inject_interrupt_hv(vcpu,
 							   BOOK3S_INTERRUPT_EXTERNAL, 0);
-			else
+			else if (!kvmppc_xive_native_enabled(vcpu->kvm))
 				lpcr |= LPCR_MER;
 		} else {
 			/*

[Severity: High]
Does kvmppc_xive_native_enabled() accurately reflect whether XIVE is currently in use?

If a VM switches from XIVE to XICS (such as during a kexec or CAS renegotiation), the XIVE native device is destroyed. However, the native pointer is intentionally not cleared:

arch/powerpc/kvm/book3s_xive_native.c:kvmppc_xive_native_release() {
	...
	/*
	 * A reference of the kvmppc_xive pointer is now kept under
	 * the xive_devices struct of the machine for reuse. It is
	 * freed when the VM is destroyed for now until we fix all the
	 * execution paths.
	 */

	kfree(dev);
}

When running in XICS mode after this switch, kvmppc_xive_native_enabled() will continue to evaluate to true.

Will this cause KVM to bypass setting LPCR_MER, and subsequently fail to inject external interrupts when MSR_EE=0, resulting in a guest hang?

Could this check instead evaluate the active vCPU IRQ routing, for example by using !kvmppc_xive_enabled(vcpu)?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
Quoted 50 lines · On Mon, Sep 21, 2026 at 11:25:38AM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] kvm: ppc: lost LPCR_MER interrupts for XICS due to XIVE state leak --

commit 215dda3f95780a965ce67c9c452b6f7d1948e486
Author: Gautam Menghani <[email protected]>

KVM: PPC: Book3S HV: Avoid spurious interrupts caused by LPCR_MER bit

This commit avoids spurious interrupts for KVM on PowerNV guests by not setting the LPCR_MER bit when native XIVE is available. It introduces a new helper, kvmppc_xive_native_enabled(), to check for native XIVE and skips setting LPCR_MER if it evaluates to true.

Quoted 13 lines
arch/powerpc/include/asm/kvm_ppc.h +5 -0
index 169ea6a7fbad4..580ad2548c2bf 100644
@@ -747,6 +747,11 @@ static inline int kvmppc_xive_enabled(struct kvm_vcpu *vcpu)
 	return vcpu->arch.irq_type == KVMPPC_IRQ_XIVE;
 }
 
+static inline bool kvmppc_xive_native_enabled(struct kvm *kvm)
+{
+	return kvm->arch.xive_devices.native;
+}
+

[ ... ]

arch/powerpc/kvm/book3s_hv.c int kvmhv_run_single_vcpu(struct kvm_vcpu *vcpu, u64 time_limit, · line 4986

Quoted 13 lines
arch/powerpc/kvm/book3s_hv.c +1 -1
index dbac3573b2c89..cb2bb29451a7f 100644
@@ -4980,7 +4980,7 @@ int kvmhv_run_single_vcpu(struct kvm_vcpu *vcpu, u64 time_limit,
 			if (!kvmhv_on_pseries() && (__kvmppc_get_msr_hv(vcpu) & MSR_EE))
 				kvmppc_inject_interrupt_hv(vcpu,
 							   BOOK3S_INTERRUPT_EXTERNAL, 0);
-			else
+			else if (!kvmppc_xive_native_enabled(vcpu->kvm))
 				lpcr |= LPCR_MER;
 		} else {
 			/*

[Severity: High]
Does kvmppc_xive_native_enabled() accurately reflect whether XIVE is currently in use?

Quoted 3 lines

If a VM switches from XIVE to XICS (such as during a kexec or CAS renegotiation), the XIVE native device is destroyed. However, the native pointer is intentionally not cleared:

The kexec case is not supported for guests - booting a KVM guest in XIVE mode and kexec to XICS mode.

Quoted 13 lines
arch/powerpc/kvm/book3s_xive_native.c:kvmppc_xive_native_release() {
	...
	/*
	 * A reference of the kvmppc_xive pointer is now kept under
	 * the xive_devices struct of the machine for reuse. It is
	 * freed when the VM is destroyed for now until we fix all the
	 * execution paths.
	 */

	kfree(dev);
}
Quoted 5 lines

When running in XICS mode after this switch, kvmppc_xive_native_enabled() will continue to evaluate to true.

Will this cause KVM to bypass setting LPCR_MER, and subsequently fail to inject external interrupts when MSR_EE=0, resulting in a guest hang?

No. If LPCR_MER is not set, the only effect of that is the vCPU will have to exit to the host for the host to inject interrupts into the vCPU. One extra exit isn't a big deal.

Quoted 3 lines

Could this check instead evaluate the active vCPU IRQ routing, for example by using !kvmppc_xive_enabled(vcpu)?

kvmppc_xive_enabled() does not check for native/irqchip=on case. It returns true even if emulation is used.

Quoted 3 lines
Signature · 2 lines
-- 
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw
In reply to Gautam Menghani

Hi Gautam,

Quoted 45 lines · Gautam Menghani

A huge number of spurious interrupts can be seen immediately after a KVM on PowerNV guest boots up in XIVE mode.

$ cat /proc/interrupts  | grep SPU
SPU:     223705     192439     273526     147623   Spurious interrupts

This bug was introduced by commit ecd10702baae5 ("KVM: PPC: Book3S HV:
Handle pending exceptions on guest entry with MSR_EE"). The root cause is that once LPCR_MER bit is set, it is supposed to be reset by software. But when a vCPU starts running with LPCR_MER set, the vCPU does not exit back to the host until the decrementer expires or there is an hcall, etc. This is because KVM on PowerNV guests have support for native XIVE, so they are not dependent on host for interrupt emulation. Due to this behaviour, a huge number of spurious interrupts are seen since LPCR_MER continues to be set and LPCR_MER cannot be reset until the vCPU exits to the host.

Fix this behaviour by not using the LPCR_MER bit whenever native XIVE is available (currently in case of KVM on PowerNV only), as the XIVE hardware can present interrupts to the KVM guest vCPU directly. So the LPCR_MER functionality is not required. This reduces the number of spurious interrupts drastically.

Fixes ecd10702baae5 ("KVM: PPC: Book3S HV: Handle pending exceptions on guest entry with MSR_EE")

---
v3:
1. Continue the use of LPCR_MER when kernel-irqchip=off (Sashiko)

v2:
1. Handle the case where xive_interrupt_pending() is true and also the external exception bit is set. (Narayana)

  arch/powerpc/include/asm/kvm_ppc.h | 7 +++++++
  arch/powerpc/kvm/book3s_hv.c       | 2 +-
  2 files changed, 8 insertions(+), 1 deletion(-)
arch/powerpc/include/asm/kvm_ppc.h +0 -0
index 169ea6a7fbad..580ad2548c2b 100644

arch/powerpc/include/asm/kvm_ppc.h static inline int kvmppc_xive_enabled(struct kvm_vcpu *vcpu) · line 753

Quoted 7 lines
arch/powerpc/include/asm/kvm_ppc.h +4 -0
@@ -747,6 +747,11 @@ static inline int kvmppc_xive_enabled(struct kvm_vcpu *vcpu)
  	return vcpu->arch.irq_type == KVMPPC_IRQ_XIVE;
  }
 
+static inline bool kvmppc_xive_native_enabled(struct kvm *kvm)
+{
+	return kvm->arch.xive_devices.native;
+}

Also, should 'kvmppc_xive_native_enabled()' check the active XIVE device rather than just whether a native device has been created?

'kvmppc_xive_native_release()' clears 'kvm->arch.xive', but explicitly keeps the 'kvmppc_xive' pointer under 'xive_devices' for reuse. The comment in 'book3s_xive.c' also says that when switching between XICS and native XIVE, the previous KVM device is released before the new one is created.

So 'xive_devices.native != NULL' seems to mean that a native XIVE device has been allocated/cached, rather than that it is currently active.

Would this be more appropriate?

return kvm->arch.xive_devices.native &&

       kvm->arch.xive == kvm->arch.xive_devices.native;
Quoted 18 lines
arch/powerpc/include/asm/kvm_ppc.h +3 -0
+
  extern int kvmppc_xive_native_connect_vcpu(struct kvm_device *dev,
  					   struct kvm_vcpu *vcpu, u32 cpu);
  extern void kvmppc_xive_native_cleanup_vcpu(struct kvm_vcpu *vcpu);
@@ -782,6 +787,8 @@ static inline bool kvmppc_xive_rearm_escalation(struct kvm_vcpu *vcpu) { return
 
  static inline int kvmppc_xive_enabled(struct kvm_vcpu *vcpu)
  	{ return 0; }
+static inline bool kvmppc_xive_native_enabled(struct kvm *kvm) { return false; }
+
  static inline int kvmppc_xive_native_connect_vcpu(struct kvm_device *dev,
  			  struct kvm_vcpu *vcpu, u32 cpu) { return -EBUSY; }
  static inline void kvmppc_xive_native_cleanup_vcpu(struct kvm_vcpu *vcpu) { }
arch/powerpc/kvm/book3s_hv.c +0 -0
index dbac3573b2c8..cb2bb29451a7 100644

arch/powerpc/kvm/book3s_hv.c int kvmhv_run_single_vcpu(struct kvm_vcpu *vcpu, u64 time_limit, · line 4984

Quoted 6 lines
arch/powerpc/kvm/book3s_hv.c +1 -1
@@ -4980,7 +4980,7 @@ int kvmhv_run_single_vcpu(struct kvm_vcpu *vcpu, u64 time_limit,
  			if (!kvmhv_on_pseries() && (__kvmppc_get_msr_hv(vcpu) & MSR_EE))
  				kvmppc_inject_interrupt_hv(vcpu,
  							   BOOK3S_INTERRUPT_EXTERNAL, 0);
-			else
+			else if (!kvmppc_xive_native_enabled(vcpu->kvm))
  				lpcr |= LPCR_MER;

Is the intent here to suppress LPCR_MER only for interrupts that native XIVE can deliver directly?

BOOK3S_IRQPRIO_EXTERNAL is a separate software-pending exception. If MSR_EE is clear it remains pending, so it seems this path should still set MER even when native XIVE is active.

IOW, should the behavior be:

else if (!kvmppc_xive_native_enabled(vcpu->kvm) ||

	 test_bit(BOOK3S_IRQPRIO_EXTERNAL,
		  &vcpu->arch.pending_exceptions))
	lpcr |= LPCR_MER;

so that only the native-XIVE-pending case suppresses MER?

Thanks,
narayana Murty N

Quoted 2 lines
arch/powerpc/kvm/book3s_hv.c +0 -0
  		} else {
  			/*
#5 lore raw
In reply to Gautam Menghani
Quoted 17 lines · Gautam Menghani

A huge number of spurious interrupts can be seen immediately after a KVM on PowerNV guest boots up in XIVE mode.

$ cat /proc/interrupts  | grep SPU
SPU:     223705     192439     273526     147623   Spurious interrupts

This bug was introduced by commit ecd10702baae5 ("KVM: PPC: Book3S HV:
Handle pending exceptions on guest entry with MSR_EE"). The root cause is that once LPCR_MER bit is set, it is supposed to be reset by software. But when a vCPU starts running with LPCR_MER set, the vCPU does not exit back to the host until the decrementer expires or there is an hcall, etc. This is because KVM on PowerNV guests have support for native XIVE, so they are not dependent on host for interrupt emulation. Due to this behaviour, a huge number of spurious interrupts are seen since LPCR_MER continues to be set and LPCR_MER cannot be reset until the vCPU exits to the host.

Quoted 5 lines

Fix this behaviour by not using the LPCR_MER bit whenever native XIVE is available (currently in case of KVM on PowerNV only), as the XIVE hardware can present interrupts to the KVM guest vCPU directly. So the LPCR_MER functionality is not required. This reduces the number of spurious interrupts drastically.

Hey Gautam,

Thanks for this. Can you also share the numbers after the change.

Regards,
Mukesh
[...]

#6 lore raw
Quoted 18 lines · Mukesh Kumar Chaurasiya
Quoted 17 lines · Gautam Menghani

A huge number of spurious interrupts can be seen immediately after a KVM on PowerNV guest boots up in XIVE mode.

$ cat /proc/interrupts  | grep SPU
SPU:     223705     192439     273526     147623   Spurious interrupts

This bug was introduced by commit ecd10702baae5 ("KVM: PPC: Book3S HV:
Handle pending exceptions on guest entry with MSR_EE"). The root cause is that once LPCR_MER bit is set, it is supposed to be reset by software. But when a vCPU starts running with LPCR_MER set, the vCPU does not exit back to the host until the decrementer expires or there is an hcall, etc. This is because KVM on PowerNV guests have support for native XIVE, so they are not dependent on host for interrupt emulation. Due to this behaviour, a huge number of spurious interrupts are seen since LPCR_MER continues to be set and LPCR_MER cannot be reset until the vCPU exits to the host.

Quoted 9 lines
Quoted 5 lines

Fix this behaviour by not using the LPCR_MER bit whenever native XIVE is available (currently in case of KVM on PowerNV only), as the XIVE hardware can present interrupts to the KVM guest vCPU directly. So the LPCR_MER functionality is not required. This reduces the number of spurious interrupts drastically.

Hey Gautam,

Thanks for this. Can you also share the numbers after the change.

Sure, here are the numbers seen immediately on boot with this patch applied:

$ cat /proc/interrupts | grep SPU
SPU:        123        139         99        144   Spurious interrupts
Quoted 3 lines

Regards,
Mukesh
[...]

#4 lore raw
In reply to Narayana Murty N
Quoted 63 lines · Narayana Murty N

Hi Gautam,

Quoted 45 lines · Gautam Menghani

A huge number of spurious interrupts can be seen immediately after a KVM on PowerNV guest boots up in XIVE mode.

$ cat /proc/interrupts  | grep SPU
SPU:     223705     192439     273526     147623   Spurious interrupts

This bug was introduced by commit ecd10702baae5 ("KVM: PPC: Book3S HV:
Handle pending exceptions on guest entry with MSR_EE"). The root cause is that once LPCR_MER bit is set, it is supposed to be reset by software. But when a vCPU starts running with LPCR_MER set, the vCPU does not exit back to the host until the decrementer expires or there is an hcall, etc. This is because KVM on PowerNV guests have support for native XIVE, so they are not dependent on host for interrupt emulation. Due to this behaviour, a huge number of spurious interrupts are seen since LPCR_MER continues to be set and LPCR_MER cannot be reset until the vCPU exits to the host.

Fix this behaviour by not using the LPCR_MER bit whenever native XIVE is available (currently in case of KVM on PowerNV only), as the XIVE hardware can present interrupts to the KVM guest vCPU directly. So the LPCR_MER functionality is not required. This reduces the number of spurious interrupts drastically.

Fixes ecd10702baae5 ("KVM: PPC: Book3S HV: Handle pending exceptions on guest entry with MSR_EE")

---
v3:
1. Continue the use of LPCR_MER when kernel-irqchip=off (Sashiko)

v2:
1. Handle the case where xive_interrupt_pending() is true and also the external exception bit is set. (Narayana)

  arch/powerpc/include/asm/kvm_ppc.h | 7 +++++++
  arch/powerpc/kvm/book3s_hv.c       | 2 +-
  2 files changed, 8 insertions(+), 1 deletion(-)
arch/powerpc/include/asm/kvm_ppc.h +0 -0
index 169ea6a7fbad..580ad2548c2b 100644

arch/powerpc/include/asm/kvm_ppc.h static inline int kvmppc_xive_enabled(struct kvm_vcpu *vcpu) · line 752

Quoted 6 lines
arch/powerpc/include/asm/kvm_ppc.h +4 -0
@@ -747,6 +747,11 @@ static inline int kvmppc_xive_enabled(struct kvm_vcpu *vcpu)
  	return vcpu->arch.irq_type == KVMPPC_IRQ_XIVE;
  }
+static inline bool kvmppc_xive_native_enabled(struct kvm *kvm)
+{
+	return kvm->arch.xive_devices.native;
+}

Also, should 'kvmppc_xive_native_enabled()' check the active XIVE device rather than just whether a native device has been created?

'kvmppc_xive_native_release()' clears 'kvm->arch.xive', but explicitly keeps the 'kvmppc_xive' pointer under 'xive_devices' for reuse. The comment in 'book3s_xive.c' also says that when switching between XICS and native XIVE, the previous KVM device is released before the new one is created.

Quoted 3 lines

So 'xive_devices.native != NULL' seems to mean that a native XIVE device has been allocated/cached, rather than that it is currently active.

In a KVM guest on PowerNV, whatever interrupt mode the guest is booted in (XIVE/ XICS), you cannot switch it with kexec. So if 'xive_devices.native' exists, we can be confident that native XIVE is being used. So the code in the patch should be fine.

Quoted 36 lines

Would this be more appropriate?

return kvm->arch.xive_devices.native &&

       kvm->arch.xive == kvm->arch.xive_devices.native;
Quoted 17 lines
arch/powerpc/include/asm/kvm_ppc.h +3 -0
+
  extern int kvmppc_xive_native_connect_vcpu(struct kvm_device *dev,
  					   struct kvm_vcpu *vcpu, u32 cpu);
  extern void kvmppc_xive_native_cleanup_vcpu(struct kvm_vcpu *vcpu);
@@ -782,6 +787,8 @@ static inline bool kvmppc_xive_rearm_escalation(struct kvm_vcpu *vcpu) { return
  static inline int kvmppc_xive_enabled(struct kvm_vcpu *vcpu)
  	{ return 0; }
+static inline bool kvmppc_xive_native_enabled(struct kvm *kvm) { return false; }
+
  static inline int kvmppc_xive_native_connect_vcpu(struct kvm_device *dev,
  			  struct kvm_vcpu *vcpu, u32 cpu) { return -EBUSY; }
  static inline void kvmppc_xive_native_cleanup_vcpu(struct kvm_vcpu *vcpu) { }
arch/powerpc/kvm/book3s_hv.c +0 -0
index dbac3573b2c8..cb2bb29451a7 100644

arch/powerpc/kvm/book3s_hv.c int kvmhv_run_single_vcpu(struct kvm_vcpu *vcpu, u64 time_limit, · line 4984

Quoted 6 lines
arch/powerpc/kvm/book3s_hv.c +1 -1
@@ -4980,7 +4980,7 @@ int kvmhv_run_single_vcpu(struct kvm_vcpu *vcpu, u64 time_limit,
  			if (!kvmhv_on_pseries() && (__kvmppc_get_msr_hv(vcpu) & MSR_EE))
  				kvmppc_inject_interrupt_hv(vcpu,
  							   BOOK3S_INTERRUPT_EXTERNAL, 0);
-			else
+			else if (!kvmppc_xive_native_enabled(vcpu->kvm))
  				lpcr |= LPCR_MER;

Is the intent here to suppress LPCR_MER only for interrupts that native XIVE can deliver directly?

BOOK3S_IRQPRIO_EXTERNAL is a separate software-pending exception. If MSR_EE is clear it remains pending, so it seems this path should still set MER even when native XIVE is active.

Quoted 8 lines

IOW, should the behavior be:

else if (!kvmppc_xive_native_enabled(vcpu->kvm) ||

	 test_bit(BOOK3S_IRQPRIO_EXTERNAL,
		  &vcpu->arch.pending_exceptions))
	lpcr |= LPCR_MER;

so that only the native-XIVE-pending case suppresses MER?

No this would still result in spurious flood. If native XIVE is being used, LPCR_MER cannot be set in any case. Software interrupts will continue to work in the same way they work in an LPAR.

#7 lore raw
In reply to Gautam Menghani

Hi Gautam,

Thanks for the patch and working on this. I have a couple of questions though.

Quoted 70 lines · Gautam Menghani

A huge number of spurious interrupts can be seen immediately after a KVM on PowerNV guest boots up in XIVE mode.

$ cat /proc/interrupts  | grep SPU
SPU:     223705     192439     273526     147623   Spurious interrupts

This bug was introduced by commit ecd10702baae5 ("KVM: PPC: Book3S HV:
Handle pending exceptions on guest entry with MSR_EE"). The root cause is that once LPCR_MER bit is set, it is supposed to be reset by software. But when a vCPU starts running with LPCR_MER set, the vCPU does not exit back to the host until the decrementer expires or there is an hcall, etc. This is because KVM on PowerNV guests have support for native XIVE, so they are not dependent on host for interrupt emulation. Due to this behaviour, a huge number of spurious interrupts are seen since LPCR_MER continues to be set and LPCR_MER cannot be reset until the vCPU exits to the host.

Fix this behaviour by not using the LPCR_MER bit whenever native XIVE is available (currently in case of KVM on PowerNV only), as the XIVE hardware can present interrupts to the KVM guest vCPU directly. So the LPCR_MER functionality is not required. This reduces the number of spurious interrupts drastically.

Fixes ecd10702baae5 ("KVM: PPC: Book3S HV: Handle pending exceptions on guest entry with MSR_EE")

---
v3:
1. Continue the use of LPCR_MER when kernel-irqchip=off (Sashiko)

v2:
1. Handle the case where xive_interrupt_pending() is true and also the external exception bit is set. (Narayana)

 arch/powerpc/include/asm/kvm_ppc.h | 7 +++++++
 arch/powerpc/kvm/book3s_hv.c       | 2 +-
 2 files changed, 8 insertions(+), 1 deletion(-)
arch/powerpc/include/asm/kvm_ppc.h +7 -0
index 169ea6a7fbad..580ad2548c2b 100644
@@ -747,6 +747,11 @@ static inline int kvmppc_xive_enabled(struct kvm_vcpu *vcpu)
 	return vcpu->arch.irq_type == KVMPPC_IRQ_XIVE;
 }
 
+static inline bool kvmppc_xive_native_enabled(struct kvm *kvm)
+{
+	return kvm->arch.xive_devices.native;
+}
+
 extern int kvmppc_xive_native_connect_vcpu(struct kvm_device *dev,
 					   struct kvm_vcpu *vcpu, u32 cpu);
 extern void kvmppc_xive_native_cleanup_vcpu(struct kvm_vcpu *vcpu);
@@ -782,6 +787,8 @@ static inline bool kvmppc_xive_rearm_escalation(struct kvm_vcpu *vcpu) { return
 
 static inline int kvmppc_xive_enabled(struct kvm_vcpu *vcpu)
 	{ return 0; }
+static inline bool kvmppc_xive_native_enabled(struct kvm *kvm) { return false; }
+
 static inline int kvmppc_xive_native_connect_vcpu(struct kvm_device *dev,
 			  struct kvm_vcpu *vcpu, u32 cpu) { return -EBUSY; }
 static inline void kvmppc_xive_native_cleanup_vcpu(struct kvm_vcpu *vcpu) { }
arch/powerpc/kvm/book3s_hv.c +0 -0
index dbac3573b2c8..cb2bb29451a7 100644

arch/powerpc/kvm/book3s_hv.c int kvmhv_run_single_vcpu(struct kvm_vcpu *vcpu, u64 time_limit, · line 4984

Quoted 6 lines
arch/powerpc/kvm/book3s_hv.c +1 -1
@@ -4980,7 +4980,7 @@ int kvmhv_run_single_vcpu(struct kvm_vcpu *vcpu, u64 time_limit,
 			if (!kvmhv_on_pseries() && (__kvmppc_get_msr_hv(vcpu) & MSR_EE))
 				kvmppc_inject_interrupt_hv(vcpu,
 							   BOOK3S_INTERRUPT_EXTERNAL, 0);
-			else
+			else if (!kvmppc_xive_native_enabled(vcpu->kvm))
 				lpcr |= LPCR_MER;

1. What happens when an L1 KVM guest is booted with (native) XIVE and then the guest is rebooted with xive=off i.e., with XICS? Will we stop setting LPCR_MER as kvm->arch.xive_devices.native would still be set?
2. What happends when an L1 KVM guest is booted with XIVE and then we kexec into a new kernel with xive=off? You did mention in your other reply that with kexec, xive=off is ignored currently but IMO, we would want to understand where this limiation lies and fix that if need be. I do understand we are trying to fix spurious interrupts problem with XIVE in this patch and this particular problem can be taken separately but it worth investigating.

Thanks,
Amit

#8 lore raw
Quoted 77 lines · Amit Machhiwal

Hi Gautam,

Thanks for the patch and working on this. I have a couple of questions though.

Quoted 72 lines · Gautam Menghani

A huge number of spurious interrupts can be seen immediately after a KVM on PowerNV guest boots up in XIVE mode.

$ cat /proc/interrupts  | grep SPU
SPU:     223705     192439     273526     147623   Spurious interrupts

This bug was introduced by commit ecd10702baae5 ("KVM: PPC: Book3S HV:
Handle pending exceptions on guest entry with MSR_EE"). The root cause is that once LPCR_MER bit is set, it is supposed to be reset by software. But when a vCPU starts running with LPCR_MER set, the vCPU does not exit back to the host until the decrementer expires or there is an hcall, etc. This is because KVM on PowerNV guests have support for native XIVE, so they are not dependent on host for interrupt emulation. Due to this behaviour, a huge number of spurious interrupts are seen since LPCR_MER continues to be set and LPCR_MER cannot be reset until the vCPU exits to the host.

Fix this behaviour by not using the LPCR_MER bit whenever native XIVE is available (currently in case of KVM on PowerNV only), as the XIVE hardware can present interrupts to the KVM guest vCPU directly. So the LPCR_MER functionality is not required. This reduces the number of spurious interrupts drastically.

Fixes ecd10702baae5 ("KVM: PPC: Book3S HV: Handle pending exceptions on guest entry with MSR_EE")

---
v3:
1. Continue the use of LPCR_MER when kernel-irqchip=off (Sashiko)

v2:
1. Handle the case where xive_interrupt_pending() is true and also the external exception bit is set. (Narayana)

 arch/powerpc/include/asm/kvm_ppc.h | 7 +++++++
 arch/powerpc/kvm/book3s_hv.c       | 2 +-
 2 files changed, 8 insertions(+), 1 deletion(-)
arch/powerpc/include/asm/kvm_ppc.h +7 -0
index 169ea6a7fbad..580ad2548c2b 100644
@@ -747,6 +747,11 @@ static inline int kvmppc_xive_enabled(struct kvm_vcpu *vcpu)
 	return vcpu->arch.irq_type == KVMPPC_IRQ_XIVE;
 }
 
+static inline bool kvmppc_xive_native_enabled(struct kvm *kvm)
+{
+	return kvm->arch.xive_devices.native;
+}
+
 extern int kvmppc_xive_native_connect_vcpu(struct kvm_device *dev,
 					   struct kvm_vcpu *vcpu, u32 cpu);
 extern void kvmppc_xive_native_cleanup_vcpu(struct kvm_vcpu *vcpu);
@@ -782,6 +787,8 @@ static inline bool kvmppc_xive_rearm_escalation(struct kvm_vcpu *vcpu) { return
 
 static inline int kvmppc_xive_enabled(struct kvm_vcpu *vcpu)
 	{ return 0; }
+static inline bool kvmppc_xive_native_enabled(struct kvm *kvm) { return false; }
+
 static inline int kvmppc_xive_native_connect_vcpu(struct kvm_device *dev,
 			  struct kvm_vcpu *vcpu, u32 cpu) { return -EBUSY; }
 static inline void kvmppc_xive_native_cleanup_vcpu(struct kvm_vcpu *vcpu) { }
arch/powerpc/kvm/book3s_hv.c +0 -0
index dbac3573b2c8..cb2bb29451a7 100644
@@ -4980,7 +4980,7 @@ int kvmhv_run_single_vcpu(struct kvm_vcpu *vcpu, u64 time_limit,
 			if (!kvmhv_on_pseries() && (__kvmppc_get_msr_hv(vcpu) & MSR_EE))
 				kvmppc_inject_interrupt_hv(vcpu,
Quoted 8 lines

arch/powerpc/kvm/book3s_hv.c int kvmhv_run_single_vcpu(struct kvm_vcpu *vcpu, u64 time_limit, · line 4984

Quoted 4 lines
arch/powerpc/kvm/book3s_hv.c +1 -1
 							   BOOK3S_INTERRUPT_EXTERNAL, 0);
-			else
+			else if (!kvmppc_xive_native_enabled(vcpu->kvm))
 				lpcr |= LPCR_MER;

1. What happens when an L1 KVM guest is booted with (native) XIVE and then the guest is rebooted with xive=off i.e., with XICS? Will we stop setting LPCR_MER as kvm->arch.xive_devices.native would still be set?

Yes, that's a good catch. kvm->arch.xive_devices.native is cleared only when vm is destroyed. So this case has to be handled.

Quoted 6 lines

2. What happends when an L1 KVM guest is booted with XIVE and then we kexec into a new kernel with xive=off? You did mention in your other reply that with kexec, xive=off is ignored currently but IMO, we would want to understand where this limiation lies and fix that if need be. I do understand we are trying to fix spurious interrupts problem with XIVE in this patch and this particular problem can be taken separately but it worth investigating.

Yes right, the kexec case is to be understood, but I'll take it up separately. Meanwhile, I'll send a v4 to fix the reboot case.

Quoted 3 lines

Thanks,
Amit