All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3 0/3] KVM: x86: Add a module param for device posted IRQs
@ 2025-04-01 16:18 Sean Christopherson
  2025-04-01 16:18 ` [PATCH v3 1/3] KVM: VMX: Don't send UNBLOCK when starting device assignment without APICv Sean Christopherson
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Sean Christopherson @ 2025-04-01 16:18 UTC (permalink / raw)
  To: Sean Christopherson, Paolo Bonzini
  Cc: kvm, linux-kernel, Yosry Ahmed, Jim Mattson

Add a module param, enable_device_posted_irqs, to control and enumerate
KVM support for device posted IRQs.

v3:
 - Put the module param in vendor code to avoid issue with reloading vendor
   modules, and to match enable_apicv (and enable_ipiv in the future).
 - Fix a shortlog typo. [Jim]

v2:
 - https://lore.kernel.org/all/20250320142022.766201-1-seanjc@google.com
 - Add prep patches to use kvm_arch_has_irq_bypass() in vendor code when
   querying support IRQ bypass, a.k.a. device posted IRQs, so as not to
   unexpectedly introduce a (desired) dependency on enable_apicv. [Yosry]
 - Use "&=" when constraining enable_device_posted_irqs based on APICv
   and IOMMU posting support. [Yosry]

v1: https://lore.kernel.org/all/20250315025615.2367411-1-seanjc@google.com

Sean Christopherson (3):
  KVM: VMX: Don't send UNBLOCK when starting device assignment without
    APICv
  KVM: SVM: Don't update IRTEs if APICv/AVIC is disabled
  KVM: x86: Add module param to control and enumerate device posted IRQs

 arch/x86/include/asm/kvm_host.h | 1 +
 arch/x86/kvm/svm/avic.c         | 3 +--
 arch/x86/kvm/svm/svm.c          | 2 ++
 arch/x86/kvm/vmx/posted_intr.c  | 7 +++----
 arch/x86/kvm/vmx/vmx.c          | 2 ++
 arch/x86/kvm/x86.c              | 9 ++++++++-
 6 files changed, 17 insertions(+), 7 deletions(-)


base-commit: 782f9feaa9517caf33186dcdd6b50a8f770ed29b
-- 
2.49.0.472.ge94155a9ec-goog


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v3 1/3] KVM: VMX: Don't send UNBLOCK when starting device assignment without APICv
  2025-04-01 16:18 [PATCH v3 0/3] KVM: x86: Add a module param for device posted IRQs Sean Christopherson
@ 2025-04-01 16:18 ` Sean Christopherson
  2025-04-01 16:18 ` [PATCH v3 2/3] KVM: SVM: Don't update IRTEs if APICv/AVIC is disabled Sean Christopherson
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Sean Christopherson @ 2025-04-01 16:18 UTC (permalink / raw)
  To: Sean Christopherson, Paolo Bonzini
  Cc: kvm, linux-kernel, Yosry Ahmed, Jim Mattson

When starting device assignment, i.e. potential IRQ bypass, don't blast
KVM_REQ_UNBLOCK if APICv is disabled/unsupported.  There is no need to
wake vCPUs if they can never use VT-d posted IRQs (sending UNBLOCK guards
against races being vCPUs blocking and devices starting IRQ bypass).

Opportunistically use kvm_arch_has_irq_bypass() for all relevant checks in
the VMX Posted Interrupt code so that all checks in KVM x86 incorporate
the same information (once AMD/AVIC is given similar treatment).

Cc: Yosry Ahmed <yosry.ahmed@linux.dev>
Signed-off-by: Sean Christopherson <seanjc@google.com>
---
 arch/x86/kvm/vmx/posted_intr.c | 7 +++----
 arch/x86/kvm/x86.c             | 1 +
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/arch/x86/kvm/vmx/posted_intr.c b/arch/x86/kvm/vmx/posted_intr.c
index ec08fa3caf43..16121d29dfd9 100644
--- a/arch/x86/kvm/vmx/posted_intr.c
+++ b/arch/x86/kvm/vmx/posted_intr.c
@@ -134,9 +134,8 @@ void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
 
 static bool vmx_can_use_vtd_pi(struct kvm *kvm)
 {
-	return irqchip_in_kernel(kvm) && enable_apicv &&
-		kvm_arch_has_assigned_device(kvm) &&
-		irq_remapping_cap(IRQ_POSTING_CAP);
+	return irqchip_in_kernel(kvm) && kvm_arch_has_irq_bypass() &&
+	       kvm_arch_has_assigned_device(kvm);
 }
 
 /*
@@ -254,7 +253,7 @@ bool pi_has_pending_interrupt(struct kvm_vcpu *vcpu)
  */
 void vmx_pi_start_assignment(struct kvm *kvm)
 {
-	if (!irq_remapping_cap(IRQ_POSTING_CAP))
+	if (!kvm_arch_has_irq_bypass())
 		return;
 
 	kvm_make_all_cpus_request(kvm, KVM_REQ_UNBLOCK);
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index c841817a914a..a5ea4b4c7036 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -13556,6 +13556,7 @@ bool kvm_arch_has_irq_bypass(void)
 {
 	return enable_apicv && irq_remapping_cap(IRQ_POSTING_CAP);
 }
+EXPORT_SYMBOL_GPL(kvm_arch_has_irq_bypass);
 
 int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons,
 				      struct irq_bypass_producer *prod)
-- 
2.49.0.472.ge94155a9ec-goog


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH v3 2/3] KVM: SVM: Don't update IRTEs if APICv/AVIC is disabled
  2025-04-01 16:18 [PATCH v3 0/3] KVM: x86: Add a module param for device posted IRQs Sean Christopherson
  2025-04-01 16:18 ` [PATCH v3 1/3] KVM: VMX: Don't send UNBLOCK when starting device assignment without APICv Sean Christopherson
@ 2025-04-01 16:18 ` Sean Christopherson
  2025-04-01 16:18 ` [PATCH v3 3/3] KVM: x86: Add module param to control and enumerate device posted IRQs Sean Christopherson
  2025-04-25 22:09 ` [PATCH v3 0/3] KVM: x86: Add a module param for " Sean Christopherson
  3 siblings, 0 replies; 5+ messages in thread
From: Sean Christopherson @ 2025-04-01 16:18 UTC (permalink / raw)
  To: Sean Christopherson, Paolo Bonzini
  Cc: kvm, linux-kernel, Yosry Ahmed, Jim Mattson

Skip IRTE updates if AVIC is disabled/unsupported, as forcing the IRTE
into remapped mode (kvm_vcpu_apicv_active() will never be true) is
unnecessary and wasteful.  The IOMMU driver is responsible for putting
IRTEs into remapped mode when an IRQ is allocated by a device, long before
that device is assigned to a VM.  I.e. the kernel as a whole has major
issues if the IRTE isn't already in remapped mode.

Opportunsitically kvm_arch_has_irq_bypass() to query for APICv/AVIC, so
so that all checks in KVM x86 incorporate the same information.

Cc: Yosry Ahmed <yosry.ahmed@linux.dev>
Cc: Jim Mattson <jmattson@google.com>
Signed-off-by: Sean Christopherson <seanjc@google.com>
---
 arch/x86/kvm/svm/avic.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c
index 65fd245a9953..901d8d2dc169 100644
--- a/arch/x86/kvm/svm/avic.c
+++ b/arch/x86/kvm/svm/avic.c
@@ -898,8 +898,7 @@ int avic_pi_update_irte(struct kvm *kvm, unsigned int host_irq,
 	struct kvm_irq_routing_table *irq_rt;
 	int idx, ret = 0;
 
-	if (!kvm_arch_has_assigned_device(kvm) ||
-	    !irq_remapping_cap(IRQ_POSTING_CAP))
+	if (!kvm_arch_has_assigned_device(kvm) || !kvm_arch_has_irq_bypass())
 		return 0;
 
 	pr_debug("SVM: %s: host_irq=%#x, guest_irq=%#x, set=%#x\n",
-- 
2.49.0.472.ge94155a9ec-goog


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH v3 3/3] KVM: x86: Add module param to control and enumerate device posted IRQs
  2025-04-01 16:18 [PATCH v3 0/3] KVM: x86: Add a module param for device posted IRQs Sean Christopherson
  2025-04-01 16:18 ` [PATCH v3 1/3] KVM: VMX: Don't send UNBLOCK when starting device assignment without APICv Sean Christopherson
  2025-04-01 16:18 ` [PATCH v3 2/3] KVM: SVM: Don't update IRTEs if APICv/AVIC is disabled Sean Christopherson
@ 2025-04-01 16:18 ` Sean Christopherson
  2025-04-25 22:09 ` [PATCH v3 0/3] KVM: x86: Add a module param for " Sean Christopherson
  3 siblings, 0 replies; 5+ messages in thread
From: Sean Christopherson @ 2025-04-01 16:18 UTC (permalink / raw)
  To: Sean Christopherson, Paolo Bonzini
  Cc: kvm, linux-kernel, Yosry Ahmed, Jim Mattson

Add a module param to each KVM vendor module to allow disabling device
posted interrupts without having to sacrifice all of APICv/AVIC, and to
also effectively enumerate to userspace whether or not KVM may be
utilizing device posted IRQs.  Disabling device posted interrupts is
very desirable for testing, and can even be desirable for production
environments, e.g. if the host kernel wants to interpose on device
interrupts.

Put the module param in kvm-{amd,intel}.ko instead of kvm.ko to match
the overall APICv/AVIC controls, and to avoid complications with said
controls.  E.g. if the param is in kvm.ko, KVM needs to be snapshot the
original user-defined value to play nice with a vendor module being
reloaded with different enable_apicv settings.

Signed-off-by: Sean Christopherson <seanjc@google.com>
---
 arch/x86/include/asm/kvm_host.h | 1 +
 arch/x86/kvm/svm/svm.c          | 2 ++
 arch/x86/kvm/vmx/vmx.c          | 2 ++
 arch/x86/kvm/x86.c              | 8 +++++++-
 4 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
index a884ab544335..6e8be274c089 100644
--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -1923,6 +1923,7 @@ struct kvm_arch_async_pf {
 extern u32 __read_mostly kvm_nr_uret_msrs;
 extern bool __read_mostly allow_smaller_maxphyaddr;
 extern bool __read_mostly enable_apicv;
+extern bool __read_mostly enable_device_posted_irqs;
 extern struct kvm_x86_ops kvm_x86_ops;
 
 #define kvm_x86_call(func) static_call(kvm_x86_##func)
diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
index 8abeab91d329..def76e63562d 100644
--- a/arch/x86/kvm/svm/svm.c
+++ b/arch/x86/kvm/svm/svm.c
@@ -231,6 +231,8 @@ module_param(tsc_scaling, int, 0444);
 static bool avic;
 module_param(avic, bool, 0444);
 
+module_param(enable_device_posted_irqs, bool, 0444);
+
 bool __read_mostly dump_invalid_vmcb;
 module_param(dump_invalid_vmcb, bool, 0644);
 
diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
index b70ed72c1783..ac7f1df612e8 100644
--- a/arch/x86/kvm/vmx/vmx.c
+++ b/arch/x86/kvm/vmx/vmx.c
@@ -115,6 +115,8 @@ module_param(enable_apicv, bool, 0444);
 bool __read_mostly enable_ipiv = true;
 module_param(enable_ipiv, bool, 0444);
 
+module_param(enable_device_posted_irqs, bool, 0444);
+
 /*
  * If nested=1, nested virtualization is supported, i.e., guests may use
  * VMX and be a hypervisor for its own guests. If nested=0, guests may not
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index a5ea4b4c7036..9211344b20ae 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -227,6 +227,9 @@ EXPORT_SYMBOL_GPL(allow_smaller_maxphyaddr);
 bool __read_mostly enable_apicv = true;
 EXPORT_SYMBOL_GPL(enable_apicv);
 
+bool __read_mostly enable_device_posted_irqs = true;
+EXPORT_SYMBOL_GPL(enable_device_posted_irqs);
+
 const struct _kvm_stats_desc kvm_vm_stats_desc[] = {
 	KVM_GENERIC_VM_STATS(),
 	STATS_DESC_COUNTER(VM, mmu_shadow_zapped),
@@ -9784,6 +9787,9 @@ int kvm_x86_vendor_init(struct kvm_x86_init_ops *ops)
 	if (r != 0)
 		goto out_mmu_exit;
 
+	enable_device_posted_irqs &= enable_apicv &&
+				     irq_remapping_cap(IRQ_POSTING_CAP);
+
 	kvm_ops_update(ops);
 
 	for_each_online_cpu(cpu) {
@@ -13554,7 +13560,7 @@ EXPORT_SYMBOL_GPL(kvm_arch_has_noncoherent_dma);
 
 bool kvm_arch_has_irq_bypass(void)
 {
-	return enable_apicv && irq_remapping_cap(IRQ_POSTING_CAP);
+	return enable_device_posted_irqs;
 }
 EXPORT_SYMBOL_GPL(kvm_arch_has_irq_bypass);
 
-- 
2.49.0.472.ge94155a9ec-goog


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v3 0/3] KVM: x86: Add a module param for device posted IRQs
  2025-04-01 16:18 [PATCH v3 0/3] KVM: x86: Add a module param for device posted IRQs Sean Christopherson
                   ` (2 preceding siblings ...)
  2025-04-01 16:18 ` [PATCH v3 3/3] KVM: x86: Add module param to control and enumerate device posted IRQs Sean Christopherson
@ 2025-04-25 22:09 ` Sean Christopherson
  3 siblings, 0 replies; 5+ messages in thread
From: Sean Christopherson @ 2025-04-25 22:09 UTC (permalink / raw)
  To: Sean Christopherson, Paolo Bonzini
  Cc: kvm, linux-kernel, Yosry Ahmed, Jim Mattson

On Tue, 01 Apr 2025 09:18:01 -0700, Sean Christopherson wrote:
> Add a module param, enable_device_posted_irqs, to control and enumerate
> KVM support for device posted IRQs.
> 
> v3:
>  - Put the module param in vendor code to avoid issue with reloading vendor
>    modules, and to match enable_apicv (and enable_ipiv in the future).
>  - Fix a shortlog typo. [Jim]
> 
> [...]

Applied 1 and 3 to kvm-x86 misc (2 was grabbed by Paolo).

[1/3] KVM: VMX: Don't send UNBLOCK when starting device assignment without APICv
      commit: c364baad3e4f114284581c35d4b9006d59d2629a
[2/3] KVM: SVM: Don't update IRTEs if APICv/AVIC is disabled
      (no commit info)
[3/3] KVM: x86: Add module param to control and enumerate device posted IRQs
      commit: 459074cff66f77af3f327e2c1f9256cdb146d798

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

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2025-04-25 22:29 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-04-01 16:18 [PATCH v3 0/3] KVM: x86: Add a module param for device posted IRQs Sean Christopherson
2025-04-01 16:18 ` [PATCH v3 1/3] KVM: VMX: Don't send UNBLOCK when starting device assignment without APICv Sean Christopherson
2025-04-01 16:18 ` [PATCH v3 2/3] KVM: SVM: Don't update IRTEs if APICv/AVIC is disabled Sean Christopherson
2025-04-01 16:18 ` [PATCH v3 3/3] KVM: x86: Add module param to control and enumerate device posted IRQs Sean Christopherson
2025-04-25 22:09 ` [PATCH v3 0/3] KVM: x86: Add a module param for " Sean Christopherson

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.