Skip to contentSkip to conversation

Add support for AMD IOMMU GAPPI

RFC PATCH Superseded Sairaj Kodilkar 3 participants 28 messages Jul 13 – Aug 17

Subsystems (6)
Documentation x86 KVM KVM x86 +2 more subsystems

v3 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v3 cover Add support for AMD IOMMU GAPPI

Introduction
============
On newer generations of AMD processors, IOMMU AVIC/x2AVIC guest-mode interrupt remapping can use Guest APIC Physical Processor Interrupt (GAPPI) as an alternative host-notification path when device interrupts target a vCPU that is not running (IRTE[IsRun] = 0).

With AVIC enabled, the IOMMU posts device interrupts into the guest virtual APIC backing page. When the vCPU is not running, KVM must additionally be notified so it can schedule the vCPU.

The legacy notification path is the GA log (GALOG): the IOMMU appends vCPU tags to a shared GA log buffer and raises a single GA log interrupt. KVM registers a notifier and scans the buffer to decide which vCPUs to wake. Under heavy interrupt load this adds latency and can overflow the buffer because all wakeups funnel through one interrupt and one shared log.

Guest APIC Physical Processor Interrupt (GAPPI), defined in section 2.2.5.4 of the AMD I/O Virtualization Technology (IOMMU) Specification [1], is an alternative. With GAPPI enabled, the IOMMU still updates the guest vAPIC backing page IRR, but may deliver a physical APIC interrupt directly to IRTE[Destination], using IRTE[GATag][7:0] as the vector. This distributes host wakeup notifications across CPUs instead of centralizing them in a log buffer.

This series programs guest-mode IRTEs accordingly: IRTE[Destination] carries the target host physical APIC ID, IRTE[GATag] is set to POSTED_INTR_WAKEUP_VECTOR, and IRTE[GAPPIDis] / IRTE[GALogIntr] are set based on whether KVM requests host wakeup. GAPPI is selected at boot via the amd_iommu=gappi kernel parameter on capable hardware, otherwise the existing GA log path is unchanged.

SVM/AMD IOMMU interface changes
===============================
The first four patches refactor the SVM/AMD IOMMU interface ahead of GAPPI.

The cpu field is renamed to apicid because it carries the host physical APIC ID for IRTE[Destination], not a Linux CPU number.

The ga_log_intr boolean is renamed to wakeup_intr (and the synthetic AVIC_PHYSICAL_ID_ENTRY_GA_LOG_INTR shadow bit to AVIC_PHYSICAL_ID_ENTRY_WAKEUP_INTR). wakeup_intr describes KVM's intent (request host wakeup while the vCPU is not running), not a specific hardware mechanism.

A separate is_running boolean is added to IOMMU interface because GAPPI requires a valid apicid in IRTE[Destination] even when the vCPU is not running. The prior encoding (apicid >= 0 means running, apicid == -1 means not running) no longer works once apicid carries the GAPPI destination while IRTE[IsRun] is clear. The IOMMU driver keys IRTE[IsRun] and destination programming off this explicit boolean instead of inferring running state from apicid.

KVM GAPPI wakeup scheme
=======================
SVM follows the Intel posted-interrupt wakeup model already used by VMX. Each pCPU maintains a list of blocked vCPUs that may be woken by a GAPPI delivery to that CPU. When a vCPU blocks while waiting for a device interrupt, SVM enqueues it on the wakeup list of the pCPU on which it was previously running (gappi_cpu) and passes that pCPU's physical APIC ID to the IOMMU to program IRTE[Destination]. The rationale is that the vCPU is likely to run again on the same pCPU, which is common when vCPUs are pinned; targeting GAPPI notifications there reduces unnecessary VMEXITs from GAPPI deliveries on other CPUs. When the vCPU is scheduled in again, it is removed from the list and IRTE[Destination] is updated to the current pCPU.

SVM registers avic_gappi_wakeup_handler() via kvm_set_posted_intr_wakeup_handler(). On POSTED_INTR_WAKEUP_VECTOR delivery, the handler walks the local per-CPU list and wakes vCPUs with a pending LAPIC IRR. The IOMMU has already posted the interrupt into the guest vAPIC; waking the vCPU lets it observe the pending interrupt and run.

List maintenance is hooked into the existing AVIC vCPU and IRQ affinity
paths: vCPU load/put through avic_update_iommu_vcpu_affinity(), the first IRQ affined to a non-running vCPU through avic_pi_update_irte() when ir_list was empty at put time, removal when the last IRTE is detached, and cleanup on vCPU destroy. All GAPPI-specific logic is gated on amd_iommu_gappi.

Changes since v2
================
https://lore.kernel.org/linux-iommu/[email protected]/

Patch[1-6]
- Expand commit messages to explain GAPPI, the interface changes, and the per-CPU wakeup list scheme [Sean].

Patch[1-3]
- Split the monolithic SVM/IOMMU API refactor into four preparatory patches [Sean]
- Rename posted_intr to wakeup_intr to reflect host wakeup intent, not guest interrupt posting [Sean]
- Pass vCPU running status with a extra parameter (is_running) instead of flags.

Patch[4,5]
- Move ga_tag=POSTED_INTR_WAKEUP_VECTOR setting from IOMMU to SVM layer.

Changes since V1:
================
https://lore.kernel.org/all/[email protected]/

Patch4
- Disable interrupts while holding wakeup list lock inside [sashiko] avic_add_vcpu_to_gappi_wakeup_list and avic_remove_vcpu_from_gappi_wakeup_list
- Unregister posted_intr_wakeup_handler during module unload [sashiko]

Patch5
- Disable GAPPI feature during kexec and suspend path [sashiko]

------
[1] https://docs.amd.com/v/u/en-US/48882_3.11_IOMMU_PUB

Sairaj Kodilkar (6):
iommu/amd: KVM: SVM: Rename cpu to apicid in IOMMU interface iommu/amd: KVM: SVM: Rename ga_log_intr to wakeup_intr in IOMMU
interface
iommu/amd: KVM: SVM: Add explicit vCPU running state to IOMMU
interface
iommu/amd: Program guest-mode IRTEs for GAPPI wakeup when IRTE[IsRun]

    = 0

KVM: SVM: Add support for AMD IOMMU Guest APIC Physical Processor
Interrupt (GAPPI)
iommu/amd: Provide kernel command line option to enable GAPPI

 .../admin-guide/kernel-parameters.txt         |   3 +-
 arch/x86/include/asm/irq_remapping.h          |   5 +-
 arch/x86/include/asm/svm.h                    |   9 +-
 arch/x86/kvm/svm/avic.c                       | 173 ++++++++++++++----
 arch/x86/kvm/svm/svm.c                        |   2 +
 arch/x86/kvm/svm/svm.h                        |   5 +
 drivers/iommu/amd/amd_iommu.h                 |   1 +
 drivers/iommu/amd/amd_iommu_types.h           |   6 +-
 drivers/iommu/amd/init.c                      |  31 +++-
 drivers/iommu/amd/iommu.c                     |  59 +++---
 include/linux/amd-iommu.h                     |  13 +-
 11 files changed, 237 insertions(+), 70 deletions(-)

base-commit: 8cd9520d35a6c38db6567e97dd93b1f11f185dc6

Signature · 1 line
2.34.1
#1 lore raw

RFC PATCH v3 1/6 iommu/amd: KVM: SVM: Rename cpu to apicid in IOMMU interface

The cpu field passed from SVM to the AMD IOMMU driver does not hold a linux CPU number. It carries the host physical APIC ID used to program the IRTE Destination field when the target vCPU is running (IRTE[IsRun] = 1).

Rename the field to apicid everywhere in the IOMMU interface so the name matches the hardware semantics.

No functional change is intended.

---
 arch/x86/include/asm/irq_remapping.h |  2 +-
 arch/x86/kvm/svm/avic.c              | 12 +++++-----
 drivers/iommu/amd/iommu.c            | 35 ++++++++++++++--------------
 include/linux/amd-iommu.h            |  8 +++----
 4 files changed, 29 insertions(+), 28 deletions(-)
arch/x86/include/asm/irq_remapping.h +1 -1
index 37b94f484ef3..263f4df429d8 100644
@@ -35,7 +35,7 @@ struct amd_iommu_pi_data {
 	u64 vapic_addr;		/* Physical address of the vCPU's vAPIC. */
 	u32 ga_tag;
 	u32 vector;		/* Guest vector of the interrupt */
-	int cpu;
+	int apicid;
 	bool ga_log_intr;
 	bool is_guest_mode;
 	void *ir_data;
arch/x86/kvm/svm/avic.c +6 -6
index cdd5a6dc646f..bccc5d7ed207 100644
@@ -949,9 +949,9 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm,
 		 */
 		entry = svm->avic_physical_id_entry;
 		if (entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK) {
-			pi_data.cpu = entry & AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK;
+			pi_data.apicid = entry & AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK;
 		} else {
-			pi_data.cpu = -1;
+			pi_data.apicid = -1;
 			pi_data.ga_log_intr = entry & AVIC_PHYSICAL_ID_ENTRY_GA_LOG_INTR;
 		}
 
@@ -1004,7 +1004,7 @@ enum avic_vcpu_action {
 	AVIC_START_BLOCKING	= BIT(1),
 };
 
-static void avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int cpu,
+static void avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int apicid,
 					    enum avic_vcpu_action action)
 {
 	bool ga_log_intr = (action & AVIC_START_BLOCKING);
@@ -1024,9 +1024,9 @@ static void avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int cpu,
 		void *data = irqfd->irq_bypass_data;
 
 		if (!(action & AVIC_TOGGLE_ON_OFF))
-			WARN_ON_ONCE(amd_iommu_update_ga(data, cpu, ga_log_intr));
-		else if (cpu >= 0)
-			WARN_ON_ONCE(amd_iommu_activate_guest_mode(data, cpu, ga_log_intr));
+			WARN_ON_ONCE(amd_iommu_update_ga(data, apicid, ga_log_intr));
+		else if (apicid >= 0)
+			WARN_ON_ONCE(amd_iommu_activate_guest_mode(data, apicid, ga_log_intr));
 		else
 			WARN_ON_ONCE(amd_iommu_deactivate_guest_mode(data));
 	}
drivers/iommu/amd/iommu.c +18 -17
index 57dc8fabc7d9..c0cf7799c56e 100644
@@ -3959,14 +3959,14 @@ static const struct irq_domain_ops amd_ir_domain_ops = {
 	.deactivate = irq_remapping_deactivate,
 };
 
-static void __amd_iommu_update_ga(struct irte_ga *entry, int cpu,
+static void __amd_iommu_update_ga(struct irte_ga *entry, int apicid,
 				  bool ga_log_intr)
 {
-	if (cpu >= 0) {
+	if (apicid >= 0) {
 		entry->lo.fields_vapic.destination =
-					APICID_TO_IRTE_DEST_LO(cpu);
+					APICID_TO_IRTE_DEST_LO(apicid);
 		entry->hi.fields.destination =
-					APICID_TO_IRTE_DEST_HI(cpu);
+					APICID_TO_IRTE_DEST_HI(apicid);
 		entry->lo.fields_vapic.is_run = true;
 		entry->lo.fields_vapic.ga_log_intr = false;
 	} else {
@@ -3979,20 +3979,21 @@ static void __amd_iommu_update_ga(struct irte_ga *entry, int cpu,
  * Update the pCPU information for an IRTE that is configured to post IRQs to
  * a vCPU, without issuing an IOMMU invalidation for the IRTE.
  *
- * If the vCPU is associated with a pCPU (@cpu >= 0), configure the Destination
- * with the pCPU's APIC ID, set IsRun, and clear GALogIntr.  If the vCPU isn't
- * associated with a pCPU (@cpu < 0), clear IsRun and set/clear GALogIntr based
- * on input from the caller (e.g. KVM only requests GALogIntr when the vCPU is
- * blocking and requires a notification wake event).  I.e. treat vCPUs that are
- * associated with a pCPU as running.  This API is intended to be used when a
- * vCPU is scheduled in/out (or stops running for any reason), to do a fast
- * update of IsRun, GALogIntr, and (conditionally) Destination.
+ * If the vCPU is associated with a pCPU (@apicid >= 0), configure the
+ * Destination with the pCPU's APIC ID, set IsRun, and clear GALogIntr.  If the
+ * vCPU isn't associated with a pCPU (@apicid < 0), clear IsRun and set/clear
+ * GALogIntr based on input from the caller (e.g. KVM only requests GALogIntr
+ * when the vCPU is blocking and requires a notification wake event).  I.e.
+ * treat vCPUs that are associated with a pCPU as running.  This API is
+ * intended to be used when a vCPU is scheduled in/out (or stops running for
+ * any reason), to do a fast update of IsRun, GALogIntr, and (conditionally)
+ * Destination.
  *
  * Per the IOMMU spec, the Destination, IsRun, and GATag fields are not cached
  * and thus don't require an invalidation to ensure the IOMMU consumes fresh
  * information.
  */
-int amd_iommu_update_ga(void *data, int cpu, bool ga_log_intr)
+int amd_iommu_update_ga(void *data, int apicid, bool ga_log_intr)
 {
 	struct amd_ir_data *ir_data = (struct amd_ir_data *)data;
 	struct irte_ga *entry = (struct irte_ga *) ir_data->entry;
@@ -4006,14 +4007,14 @@ int amd_iommu_update_ga(void *data, int cpu, bool ga_log_intr)
 	if (!ir_data->iommu)
 		return -ENODEV;
 
-	__amd_iommu_update_ga(entry, cpu, ga_log_intr);
+	__amd_iommu_update_ga(entry, apicid, ga_log_intr);
 
 	return __modify_irte_ga(ir_data->iommu, ir_data->irq_2_irte.devid,
 				ir_data->irq_2_irte.index, entry);
 }
 EXPORT_SYMBOL(amd_iommu_update_ga);
 
-int amd_iommu_activate_guest_mode(void *data, int cpu, bool ga_log_intr)
+int amd_iommu_activate_guest_mode(void *data, int apicid, bool ga_log_intr)
 {
 	struct amd_ir_data *ir_data = (struct amd_ir_data *)data;
 	struct irte_ga *entry = (struct irte_ga *) ir_data->entry;
@@ -4036,7 +4037,7 @@ int amd_iommu_activate_guest_mode(void *data, int cpu, bool ga_log_intr)
 	entry->hi.fields.vector            = ir_data->ga_vector;
 	entry->lo.fields_vapic.ga_tag      = ir_data->ga_tag;
 
-	__amd_iommu_update_ga(entry, cpu, ga_log_intr);
+	__amd_iommu_update_ga(entry, apicid, ga_log_intr);
 
 	return modify_irte_ga(ir_data->iommu, ir_data->irq_2_irte.devid,
 			      ir_data->irq_2_irte.index, entry);
@@ -4107,7 +4108,7 @@ static int amd_ir_set_vcpu_affinity(struct irq_data *data, void *info)
 		ir_data->ga_vector = pi_data->vector;
 		ir_data->ga_tag = pi_data->ga_tag;
 		if (pi_data->is_guest_mode)
-			ret = amd_iommu_activate_guest_mode(ir_data, pi_data->cpu,
+			ret = amd_iommu_activate_guest_mode(ir_data, pi_data->apicid,
 							    pi_data->ga_log_intr);
 		else
 			ret = amd_iommu_deactivate_guest_mode(ir_data);
include/linux/amd-iommu.h +4 -4
index edcee9f5335a..2c6232aefafa 100644
@@ -30,8 +30,8 @@ static inline void amd_iommu_detect(void) { }
 /* IOMMU AVIC Function */
 extern int amd_iommu_register_ga_log_notifier(int (*notifier)(u32));
 
-extern int amd_iommu_update_ga(void *data, int cpu, bool ga_log_intr);
-extern int amd_iommu_activate_guest_mode(void *data, int cpu, bool ga_log_intr);
+extern int amd_iommu_update_ga(void *data, int apicid, bool ga_log_intr);
+extern int amd_iommu_activate_guest_mode(void *data, int apicid, bool ga_log_intr);
 extern int amd_iommu_deactivate_guest_mode(void *data);
 
 #else /* defined(CONFIG_AMD_IOMMU) && defined(CONFIG_IRQ_REMAP) */
@@ -42,12 +42,12 @@ amd_iommu_register_ga_log_notifier(int (*notifier)(u32))
 	return 0;
 }
 
-static inline int amd_iommu_update_ga(void *data, int cpu, bool ga_log_intr)
+static inline int amd_iommu_update_ga(void *data, int apicid, bool ga_log_intr)
 {
 	return 0;
 }
 
-static inline int amd_iommu_activate_guest_mode(void *data, int cpu, bool ga_log_intr)
+static inline int amd_iommu_activate_guest_mode(void *data, int apicid, bool ga_log_intr)
 {
 	return 0;
 }
Signature · 1 line
2.34.1
#2 lore raw

RFC PATCH v3 2/6 iommu/amd: KVM: SVM: Rename ga_log_intr to wakeup_intr in IOMMU interface

In reply to Sairaj Kodilkar

When AVIC is enabled, device interrupts are posted into the guest virtual APIC backing page regardless of vCPU running state. When the vCPU is not running, KVM may additionally need the IOMMU to notify the host so the vCPU can be woken, historically via the GA log mechanism.

The existing ga_log_intr name is tied to one specific hardware mechanism. Guest APIC Physical Processor Interrupt (GAPPI) provides an alternative host-notification path when the vCPU is not running, i.e., IRTE[IsRun] = 0. The KVM-side intent is the same in both cases: request host wakeup notification while the vCPU is blocked.

Rename ga_log_intr to wakeup_intr in the IOMMU API to describe KVM's intent rather than a single hardware knob. The IOMMU driver still maps wakeup_intr to IRTE[GALogIntr] in this patch; GAPPI support is added in following patches.

Similarly rename the synthetic AVIC physical ID table shadow bit from AVIC_PHYSICAL_ID_ENTRY_GA_LOG_INTR to AVIC_PHYSICAL_ID_ENTRY_WAKEUP_INTR.

No functional change is intended.

---
 arch/x86/include/asm/irq_remapping.h |  2 +-
 arch/x86/include/asm/svm.h           |  9 +++++----
 arch/x86/kvm/svm/avic.c              | 26 +++++++++++++-------------
 drivers/iommu/amd/iommu.c            | 16 ++++++++--------
 include/linux/amd-iommu.h            |  8 ++++----
 5 files changed, 31 insertions(+), 30 deletions(-)
arch/x86/include/asm/irq_remapping.h +1 -1
index 263f4df429d8..789e3e154600 100644
@@ -36,7 +36,7 @@ struct amd_iommu_pi_data {
 	u32 ga_tag;
 	u32 vector;		/* Guest vector of the interrupt */
 	int apicid;
-	bool ga_log_intr;
+	bool wakeup_intr;
 	bool is_guest_mode;
 	void *ir_data;
 };
arch/x86/include/asm/svm.h +5 -4
index bcfeb5e7c0ed..add8b7274cc2 100644
@@ -259,11 +259,12 @@ struct __attribute__ ((__packed__)) vmcb_control_area {
 #define AVIC_LOGICAL_ID_ENTRY_VALID_MASK		(1 << 31)
 
 /*
- * GA_LOG_INTR is a synthetic flag that's never propagated to hardware-visible
- * tables.  GA_LOG_INTR is set if the vCPU needs device posted IRQs to generate
- * GA log interrupts to wake the vCPU (because it's blocking or about to block).
+ * WAKEUP_INTR is a synthetic flag that's never propagated to hardware-visible
+ * tables.  WAKEUP_INTR is set if the vCPU needs device posted IRQs to generate
+ * GA log/GAPPI interrupts to wake the vCPU (because it's blocking or about to
+ * block).
  */
-#define AVIC_PHYSICAL_ID_ENTRY_GA_LOG_INTR		BIT_ULL(61)
+#define AVIC_PHYSICAL_ID_ENTRY_WAKEUP_INTR		BIT_ULL(61)
 
 #define AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK	GENMASK_ULL(11, 0)
 #define AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK	GENMASK_ULL(51, 12)
arch/x86/kvm/svm/avic.c +13 -13
index bccc5d7ed207..8e87a7f2f64f 100644
@@ -952,7 +952,7 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm,
 			pi_data.apicid = entry & AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK;
 		} else {
 			pi_data.apicid = -1;
-			pi_data.ga_log_intr = entry & AVIC_PHYSICAL_ID_ENTRY_GA_LOG_INTR;
+			pi_data.wakeup_intr = entry & AVIC_PHYSICAL_ID_ENTRY_WAKEUP_INTR;
 		}
 
 		ret = irq_set_vcpu_affinity(host_irq, &pi_data);
@@ -991,15 +991,15 @@ enum avic_vcpu_action {
 	 * No unique action is required to deal with a vCPU that stops/starts
 	 * running.  A vCPU that starts running by definition stops blocking as
 	 * well, and a vCPU that stops running can't have been blocking, i.e.
-	 * doesn't need to toggle GALogIntr.
+	 * doesn't need to toggle host wakeup notification.
 	 */
 	AVIC_START_RUNNING	= 0,
 	AVIC_STOP_RUNNING	= 0,
 
 	/*
-	 * When a vCPU starts blocking, KVM needs to set the GALogIntr flag
-	 * int all associated IRTEs so that KVM can wake the vCPU if an IRQ is
-	 * sent to the vCPU.
+	 * When a vCPU starts blocking, KVM needs to request host wakeup
+	 * notification in all associated IRTEs so that KVM can wake the vCPU
+	 * if an IRQ is sent to the vCPU.
 	 */
 	AVIC_START_BLOCKING	= BIT(1),
 };
@@ -1007,7 +1007,7 @@ enum avic_vcpu_action {
 static void avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int apicid,
 					    enum avic_vcpu_action action)
 {
-	bool ga_log_intr = (action & AVIC_START_BLOCKING);
+	bool wakeup_intr = (action & AVIC_START_BLOCKING);
 	struct vcpu_svm *svm = to_svm(vcpu);
 	struct kvm_kernel_irqfd *irqfd;
 
@@ -1024,9 +1024,9 @@ static void avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int apicid,
 		void *data = irqfd->irq_bypass_data;
 
 		if (!(action & AVIC_TOGGLE_ON_OFF))
-			WARN_ON_ONCE(amd_iommu_update_ga(data, apicid, ga_log_intr));
+			WARN_ON_ONCE(amd_iommu_update_ga(data, apicid, wakeup_intr));
 		else if (apicid >= 0)
-			WARN_ON_ONCE(amd_iommu_activate_guest_mode(data, apicid, ga_log_intr));
+			WARN_ON_ONCE(amd_iommu_activate_guest_mode(data, apicid, wakeup_intr));
 		else
 			WARN_ON_ONCE(amd_iommu_deactivate_guest_mode(data));
 	}
@@ -1063,7 +1063,7 @@ static void __avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu,
 	WARN_ON_ONCE(entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK);
 
 	entry &= ~(AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK |
-		   AVIC_PHYSICAL_ID_ENTRY_GA_LOG_INTR);
+		   AVIC_PHYSICAL_ID_ENTRY_WAKEUP_INTR);
 	entry |= (h_physical_id & AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK);
 	entry |= AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK;
 
@@ -1125,7 +1125,7 @@ static void __avic_vcpu_put(struct kvm_vcpu *vcpu, enum avic_vcpu_action action)
 
 	avic_update_iommu_vcpu_affinity(vcpu, -1, action);
 
-	WARN_ON_ONCE(entry & AVIC_PHYSICAL_ID_ENTRY_GA_LOG_INTR);
+	WARN_ON_ONCE(entry & AVIC_PHYSICAL_ID_ENTRY_WAKEUP_INTR);
 
 	/*
 	 * Keep the previous APIC ID in the entry so that a rogue doorbell from
@@ -1137,11 +1137,11 @@ static void __avic_vcpu_put(struct kvm_vcpu *vcpu, enum avic_vcpu_action action)
 		WRITE_ONCE(kvm_svm->avic_physical_id_table[vcpu->vcpu_id], entry);
 
 	/*
-	 * Note!  Don't set AVIC_PHYSICAL_ID_ENTRY_GA_LOG_INTR in the table as
+	 * Note!  Don't set AVIC_PHYSICAL_ID_ENTRY_WAKEUP_INTR in the table as
 	 * it's a synthetic flag that usurps an unused should-be-zero bit.
 	 */
 	if (action & AVIC_START_BLOCKING)
-		entry |= AVIC_PHYSICAL_ID_ENTRY_GA_LOG_INTR;
+		entry |= AVIC_PHYSICAL_ID_ENTRY_WAKEUP_INTR;
 
 	svm->avic_physical_id_entry = entry;
 
@@ -1173,7 +1173,7 @@ void avic_vcpu_put(struct kvm_vcpu *vcpu)
 		 * The vCPU was preempted while blocking, ensure its IRTEs are
 		 * configured to generate GA Log Interrupts.
 		 */
-		if (!(WARN_ON_ONCE(!(entry & AVIC_PHYSICAL_ID_ENTRY_GA_LOG_INTR))))
+		if (!(WARN_ON_ONCE(!(entry & AVIC_PHYSICAL_ID_ENTRY_WAKEUP_INTR))))
 			return;
 	}
 
drivers/iommu/amd/iommu.c +8 -8
index c0cf7799c56e..1284f37e44c7 100644
@@ -3960,7 +3960,7 @@ static const struct irq_domain_ops amd_ir_domain_ops = {
 };
 
 static void __amd_iommu_update_ga(struct irte_ga *entry, int apicid,
-				  bool ga_log_intr)
+				  bool wakeup_intr)
 {
 	if (apicid >= 0) {
 		entry->lo.fields_vapic.destination =
@@ -3971,7 +3971,7 @@ static void __amd_iommu_update_ga(struct irte_ga *entry, int apicid,
 		entry->lo.fields_vapic.ga_log_intr = false;
 	} else {
 		entry->lo.fields_vapic.is_run = false;
-		entry->lo.fields_vapic.ga_log_intr = ga_log_intr;
+		entry->lo.fields_vapic.ga_log_intr = wakeup_intr;
 	}
 }
 
@@ -3982,7 +3982,7 @@ static void __amd_iommu_update_ga(struct irte_ga *entry, int apicid,
  * If the vCPU is associated with a pCPU (@apicid >= 0), configure the
  * Destination with the pCPU's APIC ID, set IsRun, and clear GALogIntr.  If the
  * vCPU isn't associated with a pCPU (@apicid < 0), clear IsRun and set/clear
- * GALogIntr based on input from the caller (e.g. KVM only requests GALogIntr
+ * GALogIntr based on input from the caller (e.g. KVM only requests wakeup_intr
  * when the vCPU is blocking and requires a notification wake event).  I.e.
  * treat vCPUs that are associated with a pCPU as running.  This API is
  * intended to be used when a vCPU is scheduled in/out (or stops running for
@@ -3993,7 +3993,7 @@ static void __amd_iommu_update_ga(struct irte_ga *entry, int apicid,
  * and thus don't require an invalidation to ensure the IOMMU consumes fresh
  * information.
  */
-int amd_iommu_update_ga(void *data, int apicid, bool ga_log_intr)
+int amd_iommu_update_ga(void *data, int apicid, bool wakeup_intr)
 {
 	struct amd_ir_data *ir_data = (struct amd_ir_data *)data;
 	struct irte_ga *entry = (struct irte_ga *) ir_data->entry;
@@ -4007,14 +4007,14 @@ int amd_iommu_update_ga(void *data, int apicid, bool ga_log_intr)
 	if (!ir_data->iommu)
 		return -ENODEV;
 
-	__amd_iommu_update_ga(entry, apicid, ga_log_intr);
+	__amd_iommu_update_ga(entry, apicid, wakeup_intr);
 
 	return __modify_irte_ga(ir_data->iommu, ir_data->irq_2_irte.devid,
 				ir_data->irq_2_irte.index, entry);
 }
 EXPORT_SYMBOL(amd_iommu_update_ga);
 
-int amd_iommu_activate_guest_mode(void *data, int apicid, bool ga_log_intr)
+int amd_iommu_activate_guest_mode(void *data, int apicid, bool wakeup_intr)
 {
 	struct amd_ir_data *ir_data = (struct amd_ir_data *)data;
 	struct irte_ga *entry = (struct irte_ga *) ir_data->entry;
@@ -4037,7 +4037,7 @@ int amd_iommu_activate_guest_mode(void *data, int apicid, bool ga_log_intr)
 	entry->hi.fields.vector            = ir_data->ga_vector;
 	entry->lo.fields_vapic.ga_tag      = ir_data->ga_tag;
 
-	__amd_iommu_update_ga(entry, apicid, ga_log_intr);
+	__amd_iommu_update_ga(entry, apicid, wakeup_intr);
 
 	return modify_irte_ga(ir_data->iommu, ir_data->irq_2_irte.devid,
 			      ir_data->irq_2_irte.index, entry);
@@ -4109,7 +4109,7 @@ static int amd_ir_set_vcpu_affinity(struct irq_data *data, void *info)
 		ir_data->ga_tag = pi_data->ga_tag;
 		if (pi_data->is_guest_mode)
 			ret = amd_iommu_activate_guest_mode(ir_data, pi_data->apicid,
-							    pi_data->ga_log_intr);
+							    pi_data->wakeup_intr);
 		else
 			ret = amd_iommu_deactivate_guest_mode(ir_data);
 	} else {
include/linux/amd-iommu.h +4 -4
index 2c6232aefafa..e20c909edc56 100644
@@ -30,8 +30,8 @@ static inline void amd_iommu_detect(void) { }
 /* IOMMU AVIC Function */
 extern int amd_iommu_register_ga_log_notifier(int (*notifier)(u32));
 
-extern int amd_iommu_update_ga(void *data, int apicid, bool ga_log_intr);
-extern int amd_iommu_activate_guest_mode(void *data, int apicid, bool ga_log_intr);
+extern int amd_iommu_update_ga(void *data, int apicid, bool wakeup_intr);
+extern int amd_iommu_activate_guest_mode(void *data, int apicid, bool wakeup_intr);
 extern int amd_iommu_deactivate_guest_mode(void *data);
 
 #else /* defined(CONFIG_AMD_IOMMU) && defined(CONFIG_IRQ_REMAP) */
@@ -42,12 +42,12 @@ amd_iommu_register_ga_log_notifier(int (*notifier)(u32))
 	return 0;
 }
 
-static inline int amd_iommu_update_ga(void *data, int apicid, bool ga_log_intr)
+static inline int amd_iommu_update_ga(void *data, int apicid, bool wakeup_intr)
 {
 	return 0;
 }
 
-static inline int amd_iommu_activate_guest_mode(void *data, int apicid, bool ga_log_intr)
+static inline int amd_iommu_activate_guest_mode(void *data, int apicid, bool wakeup_intr)
 {
 	return 0;
 }
Signature · 1 line
2.34.1
#3 lore raw

RFC PATCH v3 3/6 iommu/amd: KVM: SVM: Add explicit vCPU running state to IOMMU interface

In reply to Sairaj Kodilkar

Guest APIC Physical Processor Interrupt (GAPPI) is an AMD IOMMU mechanism for notifying the host when a device interrupt targets a non-running vCPU (IRTE[IsRun] = 0). Unlike the GA log path, GAPPI delivers a physical APIC interrupt directly to the host CPU described by IRTE[Destination], with the vector carried in IRTE[GATag], while still posting the interrupt into the guest virtual APIC backing page as usual.

In GAPPI mode, the IOMMU delivers the notification to the host CPU using the physical APIC ID in IRTE[Destination] and the vector in IRTE[GATag]. KVM must therefore supply a valid apicid even when the vCPU is not running. The prior interface inferred running state from apicid: apicid >= 0 meant running and apicid == -1 meant not running. That encoding breaks once apicid carries the GAPPI destination while the vCPU is not running; the IOMMU driver can no longer derive vCPU running state from apicid alone and needs an explicit indication from KVM.

Add an explicit is_running indication to the IOMMU interface so that SVM can pass vCPU running state to the IOMMU.

No functional change is intended.

---
 arch/x86/include/asm/irq_remapping.h |  1 +
 arch/x86/kvm/svm/avic.c              | 10 +++++++---
 drivers/iommu/amd/iommu.c            | 26 +++++++++++++-------------
 include/linux/amd-iommu.h            | 12 ++++++++----
 4 files changed, 29 insertions(+), 20 deletions(-)
arch/x86/include/asm/irq_remapping.h +1 -0
index 789e3e154600..af0cdfa32c44 100644
@@ -37,6 +37,7 @@ struct amd_iommu_pi_data {
 	u32 vector;		/* Guest vector of the interrupt */
 	int apicid;
 	bool wakeup_intr;
+	bool is_running;
 	bool is_guest_mode;
 	void *ir_data;
 };
arch/x86/kvm/svm/avic.c +7 -3
index 8e87a7f2f64f..3b2d92a58709 100644
@@ -950,9 +950,11 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm,
 		entry = svm->avic_physical_id_entry;
 		if (entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK) {
 			pi_data.apicid = entry & AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK;
+			pi_data.is_running = true;
 		} else {
 			pi_data.apicid = -1;
 			pi_data.wakeup_intr = entry & AVIC_PHYSICAL_ID_ENTRY_WAKEUP_INTR;
+			pi_data.is_running = false;
 		}
 
 		ret = irq_set_vcpu_affinity(host_irq, &pi_data);
@@ -1008,6 +1010,7 @@ static void avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int apicid,
 					    enum avic_vcpu_action action)
 {
 	bool wakeup_intr = (action & AVIC_START_BLOCKING);
+	bool is_running = apicid >= 0;
 	struct vcpu_svm *svm = to_svm(vcpu);
 	struct kvm_kernel_irqfd *irqfd;
 
@@ -1024,9 +1027,10 @@ static void avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int apicid,
 		void *data = irqfd->irq_bypass_data;
 
 		if (!(action & AVIC_TOGGLE_ON_OFF))
-			WARN_ON_ONCE(amd_iommu_update_ga(data, apicid, wakeup_intr));
-		else if (apicid >= 0)
-			WARN_ON_ONCE(amd_iommu_activate_guest_mode(data, apicid, wakeup_intr));
+			WARN_ON_ONCE(amd_iommu_update_ga(data, apicid, wakeup_intr, is_running));
+		else if (is_running)
+			WARN_ON_ONCE(amd_iommu_activate_guest_mode(data, apicid, wakeup_intr,
+								   is_running));
 		else
 			WARN_ON_ONCE(amd_iommu_deactivate_guest_mode(data));
 	}
drivers/iommu/amd/iommu.c +13 -13
index 1284f37e44c7..91405e71b3c3 100644
@@ -3960,9 +3960,9 @@ static const struct irq_domain_ops amd_ir_domain_ops = {
 };
 
 static void __amd_iommu_update_ga(struct irte_ga *entry, int apicid,
-				  bool wakeup_intr)
+				  bool wakeup_intr, bool is_running)
 {
-	if (apicid >= 0) {
+	if (is_running) {
 		entry->lo.fields_vapic.destination =
 					APICID_TO_IRTE_DEST_LO(apicid);
 		entry->hi.fields.destination =
@@ -3979,12 +3979,11 @@ static void __amd_iommu_update_ga(struct irte_ga *entry, int apicid,
  * Update the pCPU information for an IRTE that is configured to post IRQs to
  * a vCPU, without issuing an IOMMU invalidation for the IRTE.
  *
- * If the vCPU is associated with a pCPU (@apicid >= 0), configure the
- * Destination with the pCPU's APIC ID, set IsRun, and clear GALogIntr.  If the
- * vCPU isn't associated with a pCPU (@apicid < 0), clear IsRun and set/clear
- * GALogIntr based on input from the caller (e.g. KVM only requests wakeup_intr
- * when the vCPU is blocking and requires a notification wake event).  I.e.
- * treat vCPUs that are associated with a pCPU as running.  This API is
+ * If the vCPU is scheduled to run on pCPU (@is_running = 1), configure the
+ * Destination with the pCPU's APIC ID, set IsRun, and clear GALogIntr. If the
+ * vCPU is scheduled out (@is_running = 0), clear IsRun and set/clear GALogIntr
+ * based on input from the caller (e.g. KVM only requests wakeup_intr when the
+ * vCPU is blocking and requires a notification wake event). This API is
  * intended to be used when a vCPU is scheduled in/out (or stops running for
  * any reason), to do a fast update of IsRun, GALogIntr, and (conditionally)
  * Destination.
@@ -3993,7 +3992,7 @@ static void __amd_iommu_update_ga(struct irte_ga *entry, int apicid,
  * and thus don't require an invalidation to ensure the IOMMU consumes fresh
  * information.
  */
-int amd_iommu_update_ga(void *data, int apicid, bool wakeup_intr)
+int amd_iommu_update_ga(void *data, int apicid, bool wakeup_intr, bool is_running)
 {
 	struct amd_ir_data *ir_data = (struct amd_ir_data *)data;
 	struct irte_ga *entry = (struct irte_ga *) ir_data->entry;
@@ -4007,14 +4006,14 @@ int amd_iommu_update_ga(void *data, int apicid, bool wakeup_intr)
 	if (!ir_data->iommu)
 		return -ENODEV;
 
-	__amd_iommu_update_ga(entry, apicid, wakeup_intr);
+	__amd_iommu_update_ga(entry, apicid, wakeup_intr, is_running);
 
 	return __modify_irte_ga(ir_data->iommu, ir_data->irq_2_irte.devid,
 				ir_data->irq_2_irte.index, entry);
 }
 EXPORT_SYMBOL(amd_iommu_update_ga);
 
-int amd_iommu_activate_guest_mode(void *data, int apicid, bool wakeup_intr)
+int amd_iommu_activate_guest_mode(void *data, int apicid, bool wakeup_intr, bool is_running)
 {
 	struct amd_ir_data *ir_data = (struct amd_ir_data *)data;
 	struct irte_ga *entry = (struct irte_ga *) ir_data->entry;
@@ -4037,7 +4036,7 @@ int amd_iommu_activate_guest_mode(void *data, int apicid, bool wakeup_intr)
 	entry->hi.fields.vector            = ir_data->ga_vector;
 	entry->lo.fields_vapic.ga_tag      = ir_data->ga_tag;
 
-	__amd_iommu_update_ga(entry, apicid, wakeup_intr);
+	__amd_iommu_update_ga(entry, apicid, wakeup_intr, is_running);
 
 	return modify_irte_ga(ir_data->iommu, ir_data->irq_2_irte.devid,
 			      ir_data->irq_2_irte.index, entry);
@@ -4109,7 +4108,8 @@ static int amd_ir_set_vcpu_affinity(struct irq_data *data, void *info)
 		ir_data->ga_tag = pi_data->ga_tag;
 		if (pi_data->is_guest_mode)
 			ret = amd_iommu_activate_guest_mode(ir_data, pi_data->apicid,
-							    pi_data->wakeup_intr);
+							    pi_data->wakeup_intr,
+							    pi_data->is_running);
 		else
 			ret = amd_iommu_deactivate_guest_mode(ir_data);
 	} else {
include/linux/amd-iommu.h +8 -4
index e20c909edc56..e962ad511d04 100644
@@ -30,8 +30,10 @@ static inline void amd_iommu_detect(void) { }
 /* IOMMU AVIC Function */
 extern int amd_iommu_register_ga_log_notifier(int (*notifier)(u32));
 
-extern int amd_iommu_update_ga(void *data, int apicid, bool wakeup_intr);
-extern int amd_iommu_activate_guest_mode(void *data, int apicid, bool wakeup_intr);
+extern int amd_iommu_update_ga(void *data, int apicid, bool wakeup_intr,
+			       bool is_running);
+extern int amd_iommu_activate_guest_mode(void *data, int apicid, bool wakeup_intr,
+					 bool is_running);
 extern int amd_iommu_deactivate_guest_mode(void *data);
 
 #else /* defined(CONFIG_AMD_IOMMU) && defined(CONFIG_IRQ_REMAP) */
@@ -42,12 +44,14 @@ amd_iommu_register_ga_log_notifier(int (*notifier)(u32))
 	return 0;
 }
 
-static inline int amd_iommu_update_ga(void *data, int apicid, bool wakeup_intr)
+static inline int amd_iommu_update_ga(void *data, int apicid, bool wakeup_intr,
+				      bool is_running)
 {
 	return 0;
 }
 
-static inline int amd_iommu_activate_guest_mode(void *data, int apicid, bool wakeup_intr)
+static inline int amd_iommu_activate_guest_mode(void *data, int apicid, bool wakeup_intr,
+						bool is_running)
 {
 	return 0;
 }
Signature · 1 line
2.34.1
#4 lore raw

Re: [RFC PATCH v3 3/6] iommu/amd: KVM: SVM: Add explicit vCPU running state to IOMMU interface

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] KVM continues to infer the vCPU running state via `is_running = apicid >= 0`, completely…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] KVM continues to infer the vCPU running state via is_running = apicid >= 0, completely defeating the purpose of the newly decoupled IOMMU API. --

--- Patch [3]: [RFC PATCH v3 3/6] iommu/amd: KVM: SVM: Add explicit vCPU running state to IOMMU interface --- commit bbb2175fc26e0d5c105e610aeee4bcf1fe796a2d
Author: Sairaj Kodilkar <[email protected]>
iommu/amd: KVM: SVM: Add explicit vCPU running state to IOMMU interface

This commit adds an explicit is_running parameter to the AMD IOMMU guest mode interface. It states that this decoupling is necessary for GAPPI to pass a valid destination apicid when a vCPU is not running. The change intends to allow KVM to pass the vCPU running state explicitly rather than inferring it from the apicid value.

Quoted 4 lines
arch/x86/kvm/svm/avic.c +0 -0
index 8e87a7f2f64f7..3b2d92a587098 100644

[ ... ]

arch/x86/kvm/svm/avic.c static void avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int apicid, · line 1013

Quoted 5 lines
arch/x86/kvm/svm/avic.c +1 -0
@@ -1008,6 +1010,7 @@ static void avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int apicid,
 					    enum avic_vcpu_action action)
 {
 	bool wakeup_intr = (action & AVIC_START_BLOCKING);
+	bool is_running = apicid >= 0;

[Severity: Medium]
Does this assignment defeat the purpose of the API decoupling described in the commit message?

The commit message states that KVM must supply a valid apicid even when the vCPU is not running, and that we can no longer derive the vCPU running state from the apicid alone.

However, by defining is_running as apicid >= 0 here in avic_update_iommu_vcpu_affinity(), aren't we continuing to infer the running state directly from the apicid?

If a caller attempts to pass a valid GAPPI destination APIC ID for a scheduled-out vCPU, won't this helper erroneously force is_running to true? Because of this hardcoded logic, other KVM paths handling scheduled-out vCPUs would be forced to continue passing -1 for the APIC ID rather than a valid GAPPI destination.

Quoted 3 lines
arch/x86/kvm/svm/avic.c +0 -0
 	struct vcpu_svm *svm = to_svm(vcpu);
 	struct kvm_kernel_irqfd *irqfd;
 

[ ... ]

Quoted 14 lines
arch/x86/kvm/svm/avic.c +4 -3
@@ -1024,9 +1027,10 @@ static void avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int apicid,
 		void *data = irqfd->irq_bypass_data;
 
 		if (!(action & AVIC_TOGGLE_ON_OFF))
-			WARN_ON_ONCE(amd_iommu_update_ga(data, apicid, wakeup_intr));
-		else if (apicid >= 0)
-			WARN_ON_ONCE(amd_iommu_activate_guest_mode(data, apicid, wakeup_intr));
+			WARN_ON_ONCE(amd_iommu_update_ga(data, apicid, wakeup_intr, is_running));
+		else if (is_running)
+			WARN_ON_ONCE(amd_iommu_activate_guest_mode(data, apicid, wakeup_intr,
+								   is_running));
 		else
 			WARN_ON_ONCE(amd_iommu_deactivate_guest_mode(data));
 	}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#5 lore raw

Re: [RFC PATCH v3 3/6] iommu/amd: KVM: SVM: Add explicit vCPU running state to IOMMU interface

Quoted 38 lines · On 7/13/2026 4:38 PM, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] KVM continues to infer the vCPU running state via is_running = apicid >= 0, completely defeating the purpose of the newly decoupled IOMMU API. --

--- Patch [3]: [RFC PATCH v3 3/6] iommu/amd: KVM: SVM: Add explicit vCPU running state to IOMMU interface --- commit bbb2175fc26e0d5c105e610aeee4bcf1fe796a2d
Author: Sairaj Kodilkar <[email protected]>
iommu/amd: KVM: SVM: Add explicit vCPU running state to IOMMU interface

This commit adds an explicit is_running parameter to the AMD IOMMU guest mode interface. It states that this decoupling is necessary for GAPPI to pass a valid destination apicid when a vCPU is not running. The change intends to allow KVM to pass the vCPU running state explicitly rather than inferring it from the apicid value.

Quoted 4 lines
arch/x86/kvm/svm/avic.c +0 -0
index 8e87a7f2f64f7..3b2d92a587098 100644

[ ... ]

arch/x86/kvm/svm/avic.c static void avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int apicid, · line 1013

Quoted 5 lines
arch/x86/kvm/svm/avic.c +1 -0
@@ -1008,6 +1010,7 @@ static void avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int apicid,
 					    enum avic_vcpu_action action)
 {
 	bool wakeup_intr = (action & AVIC_START_BLOCKING);
+	bool is_running = apicid >= 0;

[Severity: Medium]
Does this assignment defeat the purpose of the API decoupling described in the commit message?

The commit message states that KVM must supply a valid apicid even when the vCPU is not running, and that we can no longer derive the vCPU running state from the apicid alone.

However, by defining is_running as apicid >= 0 here in avic_update_iommu_vcpu_affinity(), aren't we continuing to infer the running state directly from the apicid?

Quoted 6 lines

If a caller attempts to pass a valid GAPPI destination APIC ID for a scheduled-out vCPU, won't this helper erroneously force is_running to true? Because of this hardcoded logic, other KVM paths handling scheduled-out vCPUs would be forced to continue passing -1 for the APIC ID rather than a valid GAPPI destination.

No — this does not defeat the API decoupling. The change is that the IOMMU driver no longer infers vCPU running state from apicid. Previously, __amd_iommu_update_ga() used apicid >= 0 to decide IRTE[IsRun] and destination programming. That breaks with GAPPI, where a scheduled-out vCPU still needs a valid IRTE[Destination] while IRTE[IsRun] is clear. The new API takes an explicit is_running parameter so the IOMMU can program both fields independently.

The is_running = apicid >= 0 assignment in avic_update_iommu_vcpu_affinity() is internal SVM logic, not part of the IOMMU API. That helper is private to avic.c and only called from __avic_vcpu_load() (running pCPU APIC ID) and __avic_vcpu_put() (apicid = -1). Within SVM, apicid == -1 still means the vCPU is not running. When GAPPI is enabled on that path, SVM derives the GAPPI destination from gappi_cpu, substitutes it into apicid, and calls the IOMMU with is_running = false.

Scheduled-out callers pass -1 to this helper by convention; SVM translates that into (valid GAPPI apicid, is_running = false) before reaching the IOMMU. The inference moved from the IOMMU layer to SVM, where vCPU lifecycle is already known. avic_pi_update_irte() follows the same model: it sets is_running explicitly and fills GAPPI apicid separately when the vCPU is not running.

Thanks
Sairaj

Quoted 19 lines
Quoted 3 lines
arch/x86/kvm/svm/avic.c +0 -0
 	struct vcpu_svm *svm = to_svm(vcpu);
 	struct kvm_kernel_irqfd *irqfd;
 

[ ... ]

Quoted 14 lines
arch/x86/kvm/svm/avic.c +4 -3
@@ -1024,9 +1027,10 @@ static void avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int apicid,
 		void *data = irqfd->irq_bypass_data;
 
 		if (!(action & AVIC_TOGGLE_ON_OFF))
-			WARN_ON_ONCE(amd_iommu_update_ga(data, apicid, wakeup_intr));
-		else if (apicid >= 0)
-			WARN_ON_ONCE(amd_iommu_activate_guest_mode(data, apicid, wakeup_intr));
+			WARN_ON_ONCE(amd_iommu_update_ga(data, apicid, wakeup_intr, is_running));
+		else if (is_running)
+			WARN_ON_ONCE(amd_iommu_activate_guest_mode(data, apicid, wakeup_intr,
+								   is_running));
 		else
 			WARN_ON_ONCE(amd_iommu_deactivate_guest_mode(data));
 	}
#6 lore raw

Re: [RFC PATCH v3 3/6] iommu/amd: KVM: SVM: Add explicit vCPU running state to IOMMU interface

In reply to Sairaj Kodilkar
Quoted 9 lines · Sairaj Kodilkar
arch/x86/kvm/svm/avic.c +1 -0
@@ -1008,6 +1010,7 @@ static void avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int apicid,
 					    enum avic_vcpu_action action)
 {
 	bool wakeup_intr = (action & AVIC_START_BLOCKING);
+	bool is_running = apicid >= 0;
 	struct vcpu_svm *svm = to_svm(vcpu);
 	struct kvm_kernel_irqfd *irqfd;
 

arch/x86/kvm/svm/avic.c static void avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int apicid, · line 1033

Quoted 10 lines
arch/x86/kvm/svm/avic.c +4 -3
@@ -1024,9 +1027,10 @@ static void avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int apicid,
 		void *data = irqfd->irq_bypass_data;
 
 		if (!(action & AVIC_TOGGLE_ON_OFF))
-			WARN_ON_ONCE(amd_iommu_update_ga(data, apicid, wakeup_intr));
-		else if (apicid >= 0)
-			WARN_ON_ONCE(amd_iommu_activate_guest_mode(data, apicid, wakeup_intr));
+			WARN_ON_ONCE(amd_iommu_update_ga(data, apicid, wakeup_intr, is_running));
+		else if (is_running)
+			WARN_ON_ONCE(amd_iommu_activate_guest_mode(data, apicid, wakeup_intr,
+								   is_running));

Pass is_running here is rather weird. Given that this is the only call site that originates outside of the IOMMU driver, what if the exported amd_iommu_activate_guest_mode() is a wrapper to an internal API and hardcodes is_running=true? Then the internal path via irq_set_vcpu_affinity() can pass an actual variable.

#7 lore raw

Re: [RFC PATCH v3 3/6] iommu/amd: KVM: SVM: Add explicit vCPU running state to IOMMU interface

Quoted 12 lines · Sean Christopherson
Quoted 11 lines · Sairaj Kodilkar
arch/x86/kvm/svm/avic.c +1 -0
@@ -1008,6 +1010,7 @@ static void avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int apicid,
 					    enum avic_vcpu_action action)
 {
 	bool wakeup_intr = (action & AVIC_START_BLOCKING);
+	bool is_running = apicid >= 0;
 	struct vcpu_svm *svm = to_svm(vcpu);
 	struct kvm_kernel_irqfd *irqfd;
 
@@ -1024,9 +1027,10 @@ static void avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int apicid,
 		void *data = irqfd->irq_bypass_data;
 
Quoted 14 lines

arch/x86/kvm/svm/avic.c static void avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int apicid, · line 1033

Quoted 8 lines
arch/x86/kvm/svm/avic.c +4 -3
 		if (!(action & AVIC_TOGGLE_ON_OFF))
-			WARN_ON_ONCE(amd_iommu_update_ga(data, apicid, wakeup_intr));
-		else if (apicid >= 0)
-			WARN_ON_ONCE(amd_iommu_activate_guest_mode(data, apicid, wakeup_intr));
+			WARN_ON_ONCE(amd_iommu_update_ga(data, apicid, wakeup_intr, is_running));
+		else if (is_running)
+			WARN_ON_ONCE(amd_iommu_activate_guest_mode(data, apicid, wakeup_intr,
+								   is_running));

Pass is_running here is rather weird. Given that this is the only call site that originates outside of the IOMMU driver, what if the exported amd_iommu_activate_guest_mode() is a wrapper to an internal API and hardcodes is_running=true? Then the internal path via irq_set_vcpu_affinity() can pass an actual variable.

Yep, this makes sense, I'll create an internal function __amd_iommu_activate_guest_mode() and keep exported amd_iommu_activate_guest_mode() as a wrapper around it.

Thanks
Sairaj

#8 lore raw

RFC PATCH v3 4/6 iommu/amd: Program guest-mode IRTEs for GAPPI wakeup when IRTE[IsRun] = 0

In reply to Sairaj Kodilkar

Guest APIC Physical Processor Interrupt (GAPPI) is an alternative to the GA log for notifying the host when a device interrupt targets a non-running vCPU (IRTE[IsRun] = 0). Per the AMD IOMMU specification [1] (section 2.2.5.4), with GAPPI enabled the IOMMU delivers a physical APIC interrupt to the CPU described by IRTE[Destination], using IRTE[GATag][7:0] as the vector, while still updating the guest vAPIC backing page IRR as usual.

The AMD IOMMU also allows suppressing GAPPI interrupts using the IRTE[GAPPIDis] bit when the GAPPIDisSup feature is available in extended feature register 2. The AMD IOMMU driver sets this bit when the wakeup_intr flag is not set by KVM.

Note: amd_iommu_gappi is currently false; a later patch enables it via the kernel command line.

[1] https://docs.amd.com/v/u/en-US/48882_3.11_IOMMU_PUB

---
 drivers/iommu/amd/amd_iommu.h       |  1 +
 drivers/iommu/amd/amd_iommu_types.h |  4 +++-
 drivers/iommu/amd/init.c            |  3 +++
 drivers/iommu/amd/iommu.c           | 30 +++++++++++++++++++----------
 include/linux/amd-iommu.h           |  1 +
 5 files changed, 28 insertions(+), 11 deletions(-)
drivers/iommu/amd/amd_iommu.h +1 -0
index 834d8fabfba3..044179cab12e 100644
@@ -41,6 +41,7 @@ int amd_iommu_enable(void);
 void amd_iommu_disable(void);
 int amd_iommu_reenable(int mode);
 int amd_iommu_enable_faulting(unsigned int cpu);
+extern bool amd_iommu_gappi;
 extern int amd_iommu_guest_ir;
 extern enum protection_domain_mode amd_iommu_pgtable;
 extern int amd_iommu_gpt_level;
drivers/iommu/amd/amd_iommu_types.h +3 -1
index f9f718087893..26d7a9796e64 100644
@@ -113,6 +113,7 @@
 /* Extended Feature 2 Bits */
 #define FEATURE_SEVSNPIO_SUP	BIT_ULL(1)
 #define FEATURE_GCR3TRPMODE	BIT_ULL(3)
+#define FEATURE_GAPPIDISSUP	BIT_ULL(4)
 #define FEATURE_SNPAVICSUP	GENMASK_ULL(7, 5)
 #define FEATURE_SNPAVICSUP_GAM(x) \
 	(FIELD_GET(FEATURE_SNPAVICSUP, x) == 0x1)
@@ -1004,7 +1005,8 @@ union irte_ga_lo {
 		    no_fault	: 1,
 		    /* ------ */
 		    ga_log_intr	: 1,
-		    rsvd1	: 3,
+		    rsvd1	: 2,
+		    gappi_dis	: 1,
 		    is_run	: 1,
 		    /* ------ */
 		    guest_mode	: 1,
drivers/iommu/amd/init.c +3 -0
index 3bdb380d23e9..2e1889f8a9e4 100644
@@ -160,6 +160,9 @@ u8 amd_iommu_hpt_level;
 /* Guest page table level */
 int amd_iommu_gpt_level = PAGE_MODE_4_LEVEL;
 
+bool amd_iommu_gappi;
+EXPORT_SYMBOL(amd_iommu_gappi);
+
 int amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_VAPIC;
 static int amd_iommu_xt_mode = IRQ_REMAP_XAPIC_MODE;
 
drivers/iommu/amd/iommu.c +20 -10
index 91405e71b3c3..7eebf4745a67 100644
@@ -3970,8 +3970,18 @@ static void __amd_iommu_update_ga(struct irte_ga *entry, int apicid,
 		entry->lo.fields_vapic.is_run = true;
 		entry->lo.fields_vapic.ga_log_intr = false;
 	} else {
-		entry->lo.fields_vapic.is_run = false;
-		entry->lo.fields_vapic.ga_log_intr = wakeup_intr;
+		if (amd_iommu_gappi) {
+			entry->lo.fields_vapic.gappi_dis = !wakeup_intr &&
+						check_feature2(FEATURE_GAPPIDISSUP);
+			entry->lo.fields_vapic.is_run = false;
+			entry->lo.fields_vapic.destination =
+						APICID_TO_IRTE_DEST_LO(apicid);
+			entry->hi.fields.destination =
+						APICID_TO_IRTE_DEST_HI(apicid);
+		} else {
+			entry->lo.fields_vapic.is_run = false;
+			entry->lo.fields_vapic.ga_log_intr = wakeup_intr;
+		}
 	}
 }
 
@@ -3982,15 +3992,15 @@ static void __amd_iommu_update_ga(struct irte_ga *entry, int apicid,
  * If the vCPU is scheduled to run on pCPU (@is_running = 1), configure the
  * Destination with the pCPU's APIC ID, set IsRun, and clear GALogIntr. If the
  * vCPU is scheduled out (@is_running = 0), clear IsRun and set/clear GALogIntr
- * based on input from the caller (e.g. KVM only requests wakeup_intr when the
- * vCPU is blocking and requires a notification wake event). This API is
- * intended to be used when a vCPU is scheduled in/out (or stops running for
- * any reason), to do a fast update of IsRun, GALogIntr, and (conditionally)
- * Destination.
+ * and GAPPIDis based on input from the caller (e.g. KVM only requests
+ * wakeup_intr when the vCPU is blocking and requires a notification wake
+ * event). This API is intended to be used when a vCPU is scheduled in/out (or
+ * stops running for any reason), to do a fast update of IsRun, GALogIntr,
+ * GAPPIDis and (conditionally) Destination.
  *
- * Per the IOMMU spec, the Destination, IsRun, and GATag fields are not cached
- * and thus don't require an invalidation to ensure the IOMMU consumes fresh
- * information.
+ * Per the IOMMU spec, the Destination, IsRun, GATag and GAPPIDis fields are
+ * not cached and thus don't require an invalidation to ensure the IOMMU
+ * consumes fresh information.
  */
 int amd_iommu_update_ga(void *data, int apicid, bool wakeup_intr, bool is_running)
 {
include/linux/amd-iommu.h +1 -0
index e962ad511d04..729d82c4e09e 100644
@@ -80,4 +80,5 @@ static inline int amd_iommu_snp_disable(void) { return 0; }
 static inline bool amd_iommu_sev_tio_supported(void) { return false; }
 #endif
 
+extern bool amd_iommu_gappi;
 #endif /* _ASM_X86_AMD_IOMMU_H */
Signature · 1 line
2.34.1
#9 lore raw

Re: [RFC PATCH v3 4/6] iommu/amd: Program guest-mode IRTEs for GAPPI wakeup when IRTE[IsRun] = 0

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Critical] GAPPI uses the lower 8 bits of the IRTE `ga_tag` as the host physical APIC interrupt vector…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Critical] GAPPI uses the lower 8 bits of the IRTE ga_tag as the host physical APIC interrupt vector, but this tag holds the guest's unconstrained vcpu_idx, leading to host CPU exception injection.
- [High] When GAPPI is enabled, putting a vCPU into a blocked state configures the IOMMU IRTE with a broadcast physical destination APIC ID (0xffffffff), causing an interrupt storm. --

--- Patch [4]: [RFC PATCH v3 4/6] iommu/amd: Program guest-mode IRTEs for GAPPI wakeup when IRTE[IsRun] = 0 --- commit aad2d4e0f82cbce4cf0a8e8b65994633202311a3
Author: Sairaj Kodilkar <[email protected]>

iommu/amd: Program guest-mode IRTEs for GAPPI wakeup when IRTE[IsRun] = 0

This commit enables Guest APIC Physical Processor Interrupt (GAPPI) support for the AMD IOMMU. When a vCPU is scheduled out, it configures the IOMMU to deliver a physical APIC interrupt using the IRTE destination and GATag fields to wake up the blocked vCPU.

Quoted 7 lines

Guest APIC Physical Processor Interrupt (GAPPI) is an alternative to the GA log for notifying the host when a device interrupt targets a non-running vCPU (IRTE[IsRun] = 0). Per the AMD IOMMU specification [1] (section 2.2.5.4), with GAPPI enabled the IOMMU delivers a physical APIC interrupt to the CPU described by IRTE[Destination], using IRTE[GATag][7:0] as the vector, while still updating the guest vAPIC backing page IRR as usual.

[Severity: Critical]
KVM constructs the ga_tag using AVIC_GATAG(vm_id, vcpu_idx), which embeds the guest vcpu_idx into the lowest 8 bits. This unmodified tag is programmed into the IRTE's ga_tag field when a guest mode is activated:

drivers/iommu/amd/iommu.c:amd_iommu_activate_guest_mode() {
    ...
    entry->lo.fields_vapic.ga_tag = ir_data->ga_tag;
    ...
}

If GAPPI uses IRTE[GATag][7:0] as the physical APIC interrupt vector, and a device interrupts vCPU 0, will the IOMMU deliver a physical interrupt with vector 0?

Since vectors 0-31 on x86 are reserved for CPU exceptions, could this regression allow an untrusted guest VM to trigger a host kernel panic (e.g., #DE Divide-by-Zero) by firing an interrupt while a low-numbered vCPU is blocked?

Quoted 8 lines
drivers/iommu/amd/iommu.c +0 -0
index 91405e71b3c3a..7eebf4745a678 100644
@@ -3970,8 +3970,18 @@ static void __amd_iommu_update_ga(struct irte_ga *entry, int apicid,
 		entry->lo.fields_vapic.is_run = true;
 		entry->lo.fields_vapic.ga_log_intr = false;
 	} else {

drivers/iommu/amd/iommu.c static void __amd_iommu_update_ga(struct irte_ga *entry, int apicid, · line 3980

Quoted 10 lines
drivers/iommu/amd/iommu.c +8 -2
-		entry->lo.fields_vapic.is_run = false;
-		entry->lo.fields_vapic.ga_log_intr = wakeup_intr;
+		if (amd_iommu_gappi) {
+			entry->lo.fields_vapic.gappi_dis = !wakeup_intr &&
+						check_feature2(FEATURE_GAPPIDISSUP);
+			entry->lo.fields_vapic.is_run = false;
+			entry->lo.fields_vapic.destination =
+						APICID_TO_IRTE_DEST_LO(apicid);
+			entry->hi.fields.destination =
+						APICID_TO_IRTE_DEST_HI(apicid);

[Severity: High]
When KVM blocks a vCPU, it calls amd_iommu_update_ga() with apicid = -1 and wakeup_intr = true.

If apicid is -1, won't APICID_TO_IRTE_DEST_LO() and APICID_TO_IRTE_DEST_HI() convert this to 0xffffffff?

Does this regression result in writing the x2APIC broadcast address to the IRTE destination field, potentially causing a host-wide interrupt storm when a passed-through device fires an interrupt?

Quoted 6 lines
drivers/iommu/amd/iommu.c +4 -0
+		} else {
+			entry->lo.fields_vapic.is_run = false;
+			entry->lo.fields_vapic.ga_log_intr = wakeup_intr;
+		}
 	}
 }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
#10 lore raw

Re: [RFC PATCH v3 4/6] iommu/amd: Program guest-mode IRTEs for GAPPI wakeup when IRTE[IsRun] = 0

Quoted 35 lines · On 7/13/2026 4:37 PM, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Critical] GAPPI uses the lower 8 bits of the IRTE ga_tag as the host physical APIC interrupt vector, but this tag holds the guest's unconstrained vcpu_idx, leading to host CPU exception injection.
- [High] When GAPPI is enabled, putting a vCPU into a blocked state configures the IOMMU IRTE with a broadcast physical destination APIC ID (0xffffffff), causing an interrupt storm. --

--- Patch [4]: [RFC PATCH v3 4/6] iommu/amd: Program guest-mode IRTEs for GAPPI wakeup when IRTE[IsRun] = 0 --- commit aad2d4e0f82cbce4cf0a8e8b65994633202311a3
Author: Sairaj Kodilkar <[email protected]>

iommu/amd: Program guest-mode IRTEs for GAPPI wakeup when IRTE[IsRun] = 0

This commit enables Guest APIC Physical Processor Interrupt (GAPPI) support for the AMD IOMMU. When a vCPU is scheduled out, it configures the IOMMU to deliver a physical APIC interrupt using the IRTE destination and GATag fields to wake up the blocked vCPU.

Quoted 7 lines

Guest APIC Physical Processor Interrupt (GAPPI) is an alternative to the GA log for notifying the host when a device interrupt targets a non-running vCPU (IRTE[IsRun] = 0). Per the AMD IOMMU specification [1] (section 2.2.5.4), with GAPPI enabled the IOMMU delivers a physical APIC interrupt to the CPU described by IRTE[Destination], using IRTE[GATag][7:0] as the vector, while still updating the guest vAPIC backing page IRR as usual.

[Severity: Critical]
KVM constructs the ga_tag using AVIC_GATAG(vm_id, vcpu_idx), which embeds the guest vcpu_idx into the lowest 8 bits. This unmodified tag is programmed into the IRTE's ga_tag field when a guest mode is activated:

drivers/iommu/amd/iommu.c:amd_iommu_activate_guest_mode() {
    ...
    entry->lo.fields_vapic.ga_tag = ir_data->ga_tag;
    ...
}
Quoted 8 lines

If GAPPI uses IRTE[GATag][7:0] as the physical APIC interrupt vector, and a device interrupts vCPU 0, will the IOMMU deliver a physical interrupt with vector 0?

Since vectors 0-31 on x86 are reserved for CPU exceptions, could this regression allow an untrusted guest VM to trigger a host kernel panic (e.g., #DE Divide-by-Zero) by firing an interrupt while a low-numbered vCPU is blocked?

Currently GAPPI depends on the variable amd_iommu_gappi which is 0, and is turned on by kernel command line parameter in the last patch of this series. So IOMMU will not use vectors 0-31. In fact next patch sets ga_tag to POSTED_INTR_WAKEUP_VECTOR when amd_iommu_gappi is 1.

Thanks
Sairaj

Quoted 24 lines
Quoted 9 lines
drivers/iommu/amd/iommu.c +0 -0
index 91405e71b3c3a..7eebf4745a678 100644
@@ -3970,8 +3970,18 @@ static void __amd_iommu_update_ga(struct irte_ga *entry, int apicid,
 		entry->lo.fields_vapic.is_run = true;
 		entry->lo.fields_vapic.ga_log_intr = false;
 	} else {

drivers/iommu/amd/iommu.c static void __amd_iommu_update_ga(struct irte_ga *entry, int apicid, · line 3980

Quoted 10 lines
drivers/iommu/amd/iommu.c +8 -2
-		entry->lo.fields_vapic.is_run = false;
-		entry->lo.fields_vapic.ga_log_intr = wakeup_intr;
+		if (amd_iommu_gappi) {
+			entry->lo.fields_vapic.gappi_dis = !wakeup_intr &&
+						check_feature2(FEATURE_GAPPIDISSUP);
+			entry->lo.fields_vapic.is_run = false;
+			entry->lo.fields_vapic.destination =
+						APICID_TO_IRTE_DEST_LO(apicid);
+			entry->hi.fields.destination =
+						APICID_TO_IRTE_DEST_HI(apicid);

[Severity: High]
When KVM blocks a vCPU, it calls amd_iommu_update_ga() with apicid = -1 and wakeup_intr = true.

Quoted 6 lines

If apicid is -1, won't APICID_TO_IRTE_DEST_LO() and APICID_TO_IRTE_DEST_HI() convert this to 0xffffffff?

Does this regression result in writing the x2APIC broadcast address to the IRTE destination field, potentially causing a host-wide interrupt storm when a passed-through device fires an interrupt?

No, When amd_iommu_gappi is 1 and vCPU is unloaded, the SVM layer will pass a valid apicid. Please see next patch for complete SVM side implementation.

Thanks
Sairaj

Quoted 8 lines
Quoted 7 lines
drivers/iommu/amd/iommu.c +4 -0
+		} else {
+			entry->lo.fields_vapic.is_run = false;
+			entry->lo.fields_vapic.ga_log_intr = wakeup_intr;
+		}
 	}
 }
#11 lore raw

RFC PATCH v3 5/6 KVM: SVM: Add support for AMD IOMMU Guest APIC Physical Processor Interrupt (GAPPI)

In reply to Sairaj Kodilkar

With AVIC guest-mode interrupt remapping, device interrupts are posted into the guest vAPIC backing page by the IOMMU. When the vCPU is not running (IRTE[IsRun] = 0), KVM must still be notified to schedule it. The legacy path uses the GA log.

GAPPI (Guest APIC Physical Processor Interrupt) is an alternative to the GA log mechanism provided by the AMD IOMMU. With GAPPI enabled, the IOMMU still updates the vAPIC backing page IRR, but the host wakeup notification is delivered as a physical APIC interrupt to IRTE[Destination], using IRTE[GATag][7:0] as the vector (POSTED_INTR_WAKEUP_VECTOR).

SVM follows the Intel posted-interrupt wakeup model. Each pCPU maintains a list of blocked vCPUs that may be woken by a GAPPI delivery to that CPU. When a vCPU blocks while waiting for an interrupt, SVM enqueues it on the wakeup list of the pCPU on which it was previously running and passes that same pCPU's physical APIC ID to the IOMMU to program IRTE[Destination]. The rationale is that the vCPU is likely to run again on the same pCPU, which is common when vCPUs are pinned; targeting GAPPI notifications there reduces unnecessary VMEXITs from GAPPI deliveries on other CPUs. SVM registers the GAPPI handler via kvm_set_posted_intr_wakeup_handler(). On delivery, it walks the local vCPU list and wakes vCPUs with a pending IRR.

All GAPPI logic is gated on amd_iommu_gappi. Without it, KVM and the IOMMU falls back to the legacy GA log mechanism for vCPU wakeup.

---
 arch/x86/kvm/svm/avic.c | 135 +++++++++++++++++++++++++++++++++++-----
 arch/x86/kvm/svm/svm.c  |   2 +
 arch/x86/kvm/svm/svm.h  |   5 ++
 3 files changed, 128 insertions(+), 14 deletions(-)
arch/x86/kvm/svm/avic.c +121 -14
index 3b2d92a58709..982a5988532f 100644
@@ -63,6 +63,9 @@
 	ga_tag;								\
 })
 
+static DEFINE_PER_CPU(struct list_head, gappi_vcpu_wakeup_list);
+static DEFINE_PER_CPU(raw_spinlock_t, gappi_vcpu_wakeup_list_lock);
+
 static_assert(__AVIC_GATAG(AVIC_VM_ID_MASK, AVIC_VCPU_IDX_MASK) == -1u);
 
 #define AVIC_AUTO_MODE -1
@@ -874,6 +877,9 @@ int avic_init_vcpu(struct vcpu_svm *svm)
 	INIT_LIST_HEAD(&svm->ir_list);
 	raw_spin_lock_init(&svm->ir_list_lock);
 
+	INIT_LIST_HEAD(&svm->gappi_vcpu_wakeup_list);
+	svm->gappi_cpu = -1;
+
 	if (!enable_apicv || !irqchip_in_kernel(vcpu->kvm))
 		return 0;
 
@@ -886,6 +892,42 @@ int avic_init_vcpu(struct vcpu_svm *svm)
 	return ret;
 }
 
+static void avic_add_vcpu_to_gappi_wakeup_list(struct vcpu_svm *svm, int cpu)
+{
+	struct list_head *wakeup_list;
+	raw_spinlock_t *spinlock;
+
+	if (WARN_ON(unlikely(cpu < 0)))
+		return;
+
+	wakeup_list = &per_cpu(gappi_vcpu_wakeup_list, cpu);
+	spinlock = &per_cpu(gappi_vcpu_wakeup_list_lock, cpu);
+	guard(raw_spinlock_irqsave)(spinlock);
+	if (list_empty(&svm->gappi_vcpu_wakeup_list))
+		list_add_tail(&svm->gappi_vcpu_wakeup_list, wakeup_list);
+}
+
+static void avic_remove_vcpu_from_gappi_wakeup_list(struct vcpu_svm *svm, int cpu)
+{
+	raw_spinlock_t *spinlock;
+
+	if (WARN_ON(unlikely(cpu < 0)))
+		return;
+
+	spinlock = &per_cpu(gappi_vcpu_wakeup_list_lock, cpu);
+	guard(raw_spinlock_irqsave)(spinlock);
+	if (!list_empty(&svm->gappi_vcpu_wakeup_list))
+		list_del_init(&svm->gappi_vcpu_wakeup_list);
+}
+
+void avic_destroy_vcpu(struct vcpu_svm *svm)
+{
+	if (svm->gappi_cpu != -1 && amd_iommu_gappi) {
+		avic_remove_vcpu_from_gappi_wakeup_list(svm, svm->gappi_cpu);
+		svm->gappi_cpu = -1;
+	}
+}
+
 void avic_apicv_post_state_restore(struct kvm_vcpu *vcpu)
 {
 	avic_handle_dfr_update(vcpu);
@@ -896,13 +938,18 @@ static void svm_ir_list_del(struct kvm_kernel_irqfd *irqfd)
 {
 	struct kvm_vcpu *vcpu = irqfd->irq_bypass_vcpu;
 	unsigned long flags;
+	struct vcpu_svm *svm;
 
 	if (!vcpu)
 		return;
 
-	raw_spin_lock_irqsave(&to_svm(vcpu)->ir_list_lock, flags);
+	svm = to_svm(vcpu);
+
+	raw_spin_lock_irqsave(&svm->ir_list_lock, flags);
 	list_del(&irqfd->vcpu_list);
-	raw_spin_unlock_irqrestore(&to_svm(vcpu)->ir_list_lock, flags);
+	if (amd_iommu_gappi && list_empty(&svm->ir_list))
+		avic_remove_vcpu_from_gappi_wakeup_list(svm, svm->gappi_cpu);
+	raw_spin_unlock_irqrestore(&svm->ir_list_lock, flags);
 }
 
 int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm,
@@ -923,8 +970,6 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm,
 		 * if AVIC is enabled/uninhibited in the future.
 		 */
 		struct amd_iommu_pi_data pi_data = {
-			.ga_tag = AVIC_GATAG(to_kvm_svm(kvm)->avic_vm_id,
-					     vcpu->vcpu_idx),
 			.is_guest_mode = kvm_vcpu_apicv_active(vcpu),
 			.vapic_addr = avic_get_backing_page_address(to_svm(vcpu)),
 			.vector = vector,
@@ -932,6 +977,7 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm,
 		struct vcpu_svm *svm = to_svm(vcpu);
 		u64 entry;
 		int ret;
+		bool is_vcpu_waiting = false;
 
 		/*
 		 * Prevent the vCPU from being scheduled out or migrated until
@@ -947,6 +993,12 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm,
 		 * scheduled out, KVM will update the pCPU info when the vCPU
 		 * is awakened and/or scheduled in.  See also avic_vcpu_load().
 		 */
+		if (amd_iommu_gappi)
+			pi_data.ga_tag = POSTED_INTR_WAKEUP_VECTOR;
+		else
+			pi_data.ga_tag = AVIC_GATAG(to_kvm_svm(kvm)->avic_vm_id,
+						    vcpu->vcpu_idx);
+
 		entry = svm->avic_physical_id_entry;
 		if (entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK) {
 			pi_data.apicid = entry & AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK;
@@ -955,11 +1007,19 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm,
 			pi_data.apicid = -1;
 			pi_data.wakeup_intr = entry & AVIC_PHYSICAL_ID_ENTRY_WAKEUP_INTR;
 			pi_data.is_running = false;
+
+			if (amd_iommu_gappi) {
+				pi_data.apicid = kvm_cpu_get_apicid(svm->gappi_cpu);
+				if (list_empty(&svm->ir_list)) {
+					avic_add_vcpu_to_gappi_wakeup_list(svm, svm->gappi_cpu);
+					is_vcpu_waiting = true;
+				}
+			}
 		}
 
 		ret = irq_set_vcpu_affinity(host_irq, &pi_data);
 		if (ret)
-			return ret;
+			goto gappi_err_out;
 
 		/*
 		 * Revert to legacy mode if the IOMMU didn't provide metadata
@@ -968,12 +1028,17 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm,
 		 */
 		if (WARN_ON_ONCE(!pi_data.ir_data)) {
 			irq_set_vcpu_affinity(host_irq, NULL);
-			return -EIO;
+			ret = -EIO;
+			goto gappi_err_out;
 		}
 
 		irqfd->irq_bypass_data = pi_data.ir_data;
 		list_add(&irqfd->vcpu_list, &svm->ir_list);
 		return 0;
+gappi_err_out:
+		if (is_vcpu_waiting)
+			avic_remove_vcpu_from_gappi_wakeup_list(svm, svm->gappi_cpu);
+		return ret;
 	}
 	return irq_set_vcpu_affinity(host_irq, NULL);
 }
@@ -1007,7 +1072,7 @@ enum avic_vcpu_action {
 };
 
 static void avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int apicid,
-					    enum avic_vcpu_action action)
+					    int cpu, enum avic_vcpu_action action)
 {
 	bool wakeup_intr = (action & AVIC_START_BLOCKING);
 	bool is_running = apicid >= 0;
@@ -1018,10 +1083,27 @@ static void avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int apicid,
 
 	/*
 	 * Here, we go through the per-vcpu ir_list to update all existing
-	 * interrupt remapping table entry targeting this vcpu.
+	 * interrupt remapping table entries targeting this vcpu.
 	 */
-	if (list_empty(&svm->ir_list))
+	if (list_empty(&svm->ir_list)) {
+		if (amd_iommu_gappi && cpu >= 0)
+			svm->gappi_cpu = cpu;
 		return;
+	}
+
+	if (is_running && amd_iommu_gappi) {
+		if (svm->gappi_cpu != -1)
+			/*
+			 * Handle initial state when vCPU is loaded for the
+			 * first time without any IRQ affinity.
+			 */
+			avic_remove_vcpu_from_gappi_wakeup_list(svm, svm->gappi_cpu);
+
+		svm->gappi_cpu = cpu; /* Store cpu number as target for GAPPI */
+	} else if (amd_iommu_gappi) {
+		apicid = kvm_cpu_get_apicid(svm->gappi_cpu);
+		avic_add_vcpu_to_gappi_wakeup_list(svm, svm->gappi_cpu);
+	}
 
 	list_for_each_entry(irqfd, &svm->ir_list, vcpu_list) {
 		void *data = irqfd->irq_bypass_data;
@@ -1084,7 +1166,7 @@ static void __avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu,
 
 	WRITE_ONCE(kvm_svm->avic_physical_id_table[vcpu->vcpu_id], entry);
 
-	avic_update_iommu_vcpu_affinity(vcpu, h_physical_id, action);
+	avic_update_iommu_vcpu_affinity(vcpu, h_physical_id, cpu, action);
 
 	raw_spin_unlock_irqrestore(&svm->ir_list_lock, flags);
 }
@@ -1127,7 +1209,7 @@ static void __avic_vcpu_put(struct kvm_vcpu *vcpu, enum avic_vcpu_action action)
 	 */
 	raw_spin_lock_irqsave(&svm->ir_list_lock, flags);
 
-	avic_update_iommu_vcpu_affinity(vcpu, -1, action);
+	avic_update_iommu_vcpu_affinity(vcpu, -1, -1, action);
 
 	WARN_ON_ONCE(entry & AVIC_PHYSICAL_ID_ENTRY_WAKEUP_INTR);
 
@@ -1175,7 +1257,7 @@ void avic_vcpu_put(struct kvm_vcpu *vcpu)
 
 		/*
 		 * The vCPU was preempted while blocking, ensure its IRTEs are
-		 * configured to generate GA Log Interrupts.
+		 * configured to request host wakeup notification.
 		 */
 		if (!(WARN_ON_ONCE(!(entry & AVIC_PHYSICAL_ID_ENTRY_WAKEUP_INTR))))
 			return;
@@ -1300,6 +1382,21 @@ static bool __init avic_want_avic_enabled(void)
 	return true;
 }
 
+static void avic_gappi_wakeup_handler(void)
+{
+	int cpu = smp_processor_id();
+	struct list_head *vcpu_wakeup_list = &per_cpu(gappi_vcpu_wakeup_list, cpu);
+	raw_spinlock_t *spinlock = &per_cpu(gappi_vcpu_wakeup_list_lock, cpu);
+	struct vcpu_svm *svm;
+
+	raw_spin_lock(spinlock);
+	list_for_each_entry(svm, vcpu_wakeup_list, gappi_vcpu_wakeup_list) {
+		if (kvm_lapic_find_highest_irr(&svm->vcpu) >= 0)
+			kvm_vcpu_wake_up(&svm->vcpu);
+	}
+	raw_spin_unlock(spinlock);
+}
+
 /*
  * Note:
  * - The module param avic enable both xAPIC and x2APIC mode.
@@ -1308,12 +1405,18 @@ static bool __init avic_want_avic_enabled(void)
  */
 bool __init avic_hardware_setup(void)
 {
+	int cpu;
 	avic = avic_want_avic_enabled();
 	if (!avic)
 		return false;
 
 	pr_info("AVIC enabled\n");
 
+	for_each_possible_cpu(cpu) {
+		INIT_LIST_HEAD(&per_cpu(gappi_vcpu_wakeup_list, cpu));
+		raw_spin_lock_init(&per_cpu(gappi_vcpu_wakeup_list_lock, cpu));
+	}
+
 	/* AVIC is a prerequisite for x2AVIC. */
 	x2avic_enabled = boot_cpu_has(X86_FEATURE_X2AVIC);
 	if (x2avic_enabled) {
@@ -1337,12 +1440,16 @@ bool __init avic_hardware_setup(void)
 		enable_ipiv = false;
 
 	amd_iommu_register_ga_log_notifier(&avic_ga_log_notifier);
+	kvm_set_posted_intr_wakeup_handler(&avic_gappi_wakeup_handler);
 
 	return true;
 }
 
 void avic_hardware_unsetup(void)
 {
-	if (avic)
-		amd_iommu_register_ga_log_notifier(NULL);
+	if (!avic)
+		return;
+
+	amd_iommu_register_ga_log_notifier(NULL);
+	kvm_set_posted_intr_wakeup_handler(NULL);
 }
arch/x86/kvm/svm/svm.c +2 -0
index e02a38da5296..b687133f8528 100644
@@ -1356,6 +1356,8 @@ static void svm_vcpu_free(struct kvm_vcpu *vcpu)
 
 	WARN_ON_ONCE(!list_empty(&svm->ir_list));
 
+	avic_destroy_vcpu(svm);
+
 	svm_leave_nested(vcpu);
 	svm_free_nested(svm);
 
arch/x86/kvm/svm/svm.h +5 -0
index 5137416be593..47d5bb5d7103 100644
@@ -362,6 +362,10 @@ struct vcpu_svm {
 
 	/* Guest GIF value, used when vGIF is not enabled */
 	bool guest_gif;
+
+	/* GAPPI related fields */
+	struct list_head gappi_vcpu_wakeup_list;
+	int gappi_cpu;
 };
 
 struct svm_cpu_data {
@@ -909,6 +913,7 @@ void avic_init_vmcb(struct vcpu_svm *svm, struct vmcb *vmcb);
 int avic_incomplete_ipi_interception(struct kvm_vcpu *vcpu);
 int avic_unaccelerated_access_interception(struct kvm_vcpu *vcpu);
 int avic_init_vcpu(struct vcpu_svm *svm);
+void avic_destroy_vcpu(struct vcpu_svm *svm);
 void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu);
 void avic_vcpu_put(struct kvm_vcpu *vcpu);
 void avic_apicv_post_state_restore(struct kvm_vcpu *vcpu);
Signature · 1 line
2.34.1
#12 lore raw

Re: [RFC PATCH v3 5/6] KVM: SVM: Add support for AMD IOMMU Guest APIC Physical Processor Interrupt (GAPPI)

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [High] Host-userspace-reachable WARN_ON due to uninitialized `svm->gappi_cpu` (-1) when configuring IRQ…

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] Host-userspace-reachable WARN_ON due to uninitialized svm->gappi_cpu (-1) when configuring IRQ routing before a vCPU runs.
- [High] Missing CPU hotplug handling causes blocked vCPUs to hang if their last-run physical CPU goes offline.
- [High] ABBA deadlock between scheduler rq_lock and gappi_vcpu_wakeup_list_lock due to manipulating the wakeup list during vCPU preemption.
- [Low] Mixing scoped guard() cleanup with goto error handling violates architectural guidelines.
--

--- Patch [5]: [RFC PATCH v3 5/6] KVM: SVM: Add support for AMD IOMMU Guest APIC Physical Processor Interrupt (GAPPI) --- commit c3bb8845bd944e9a327f36d68b70032d436f727c
Author: Sairaj Kodilkar <[email protected]>

KVM: SVM: Add support for AMD IOMMU Guest APIC Physical Processor Interrupt (GAPPI)

With AVIC guest-mode interrupt remapping, device interrupts are posted into the guest vAPIC backing page by the IOMMU. When the vCPU is not running (IRTE[IsRun] = 0), KVM must still be notified to schedule it. The legacy path uses the GA log.

Quoted 5 lines
arch/x86/kvm/svm/avic.c +0 -0
index 3b2d92a587098..982a5988532ff 100644

arch/x86/kvm/svm/avic.c int avic_init_vcpu(struct vcpu_svm *svm) · line 901

Quoted 10 lines
arch/x86/kvm/svm/avic.c +7 -0
@@ -886,6 +892,42 @@ int avic_init_vcpu(struct vcpu_svm *svm)
 	return ret;
 }
 
+static void avic_add_vcpu_to_gappi_wakeup_list(struct vcpu_svm *svm, int cpu)
+{
+	struct list_head *wakeup_list;
+	raw_spinlock_t *spinlock;
+
+	if (WARN_ON(unlikely(cpu < 0)))
+		return;

[Severity: High]
Can an unprivileged user-space process trigger this WARN_ON?

When a vCPU is created, svm->gappi_cpu is initialized to -1 in avic_init_vcpu(). It is only set to a valid CPU ID later during __avic_vcpu_load().

If host user-space configures device interrupt routing via the KVM_IRQFD ioctl before the vCPU runs, avic_pi_update_irte() is invoked. This will pass the uninitialized svm->gappi_cpu (-1) down to this function, hitting the WARN_ON. If the host has panic_on_warn enabled, this allows host userspace to trigger a kernel panic.

[ ... ]

Quoted 21 lines
arch/x86/kvm/svm/avic.c +10 -0
@@ -947,6 +993,12 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm,
 		 * scheduled out, KVM will update the pCPU info when the vCPU
 		 * is awakened and/or scheduled in.  See also avic_vcpu_load().
 		 */
+		if (amd_iommu_gappi)
+			pi_data.ga_tag = POSTED_INTR_WAKEUP_VECTOR;
+		else
+			pi_data.ga_tag = AVIC_GATAG(to_kvm_svm(kvm)->avic_vm_id,
+						    vcpu->vcpu_idx);
+
 		entry = svm->avic_physical_id_entry;
 		if (entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK) {
 			pi_data.apicid = entry & AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK;
@@ -955,11 +1007,19 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm,
 			pi_data.apicid = -1;
 			pi_data.wakeup_intr = entry & AVIC_PHYSICAL_ID_ENTRY_WAKEUP_INTR;
 			pi_data.is_running = false;
+
+			if (amd_iommu_gappi) {
+				pi_data.apicid = kvm_cpu_get_apicid(svm->gappi_cpu);
+				if (list_empty(&svm->ir_list)) {

arch/x86/kvm/svm/avic.c int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm, · line 1022

Quoted 10 lines
arch/x86/kvm/svm/avic.c +5 -1
+					avic_add_vcpu_to_gappi_wakeup_list(svm, svm->gappi_cpu);
+					is_vcpu_waiting = true;
+				}
+			}
 		}
 
 		ret = irq_set_vcpu_affinity(host_irq, &pi_data);
 		if (ret)
-			return ret;
+			goto gappi_err_out;

[Severity: Low]
Is it safe to mix goto-based error handling in a function that uses guard()?

Earlier in avic_pi_update_irte(), guard(raw_spinlock_irqsave)(&svm->ir_list_lock) is used. According to include/linux/cleanup.h, mixing goto with guard() in the same function creates confusing ownership semantics and violates the subsystem's cleanup rules.

Quoted 22 lines
arch/x86/kvm/svm/avic.c +6 -1
 		/*
 		 * Revert to legacy mode if the IOMMU didn't provide metadata
@@ -968,12 +1028,17 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm,
 		 */
 		if (WARN_ON_ONCE(!pi_data.ir_data)) {
 			irq_set_vcpu_affinity(host_irq, NULL);
-			return -EIO;
+			ret = -EIO;
+			goto gappi_err_out;
 		}
 
 		irqfd->irq_bypass_data = pi_data.ir_data;
 		list_add(&irqfd->vcpu_list, &svm->ir_list);
 		return 0;
+gappi_err_out:
+		if (is_vcpu_waiting)
+			avic_remove_vcpu_from_gappi_wakeup_list(svm, svm->gappi_cpu);
+		return ret;
 	}
 	return irq_set_vcpu_affinity(host_irq, NULL);
 }

[ ... ]

Quoted 8 lines
arch/x86/kvm/svm/avic.c +4 -0
@@ -1300,6 +1382,21 @@ static bool __init avic_want_avic_enabled(void)
 	return true;
 }
 
+static void avic_gappi_wakeup_handler(void)
+{
+	int cpu = smp_processor_id();
+	struct list_head *vcpu_wakeup_list = &per_cpu(gappi_vcpu_wakeup_list, cpu);

arch/x86/kvm/svm/avic.c static bool __init avic_want_avic_enabled(void) · line 1398

Quoted 10 lines
arch/x86/kvm/svm/avic.c +10 -0
+	raw_spinlock_t *spinlock = &per_cpu(gappi_vcpu_wakeup_list_lock, cpu);
+	struct vcpu_svm *svm;
+
+	raw_spin_lock(spinlock);
+	list_for_each_entry(svm, vcpu_wakeup_list, gappi_vcpu_wakeup_list) {
+		if (kvm_lapic_find_highest_irr(&svm->vcpu) >= 0)
+			kvm_vcpu_wake_up(&svm->vcpu);
+	}
+	raw_spin_unlock(spinlock);
+}

[Severity: High]
Does this lock ordering create an ABBA deadlock?

When a vCPU is preempted, the scheduler executes kvm_sched_out() while holding the runqueue lock (rq_lock). KVM then calls __avic_vcpu_put(), which in turn calls avic_add_vcpu_to_gappi_wakeup_list(). This acquires the gappi_vcpu_wakeup_list_lock, establishing an ordering of rq_lock -> gappi_vcpu_wakeup_list_lock.

Conversely, when a GAPPI interrupt arrives, avic_gappi_wakeup_handler() acquires gappi_vcpu_wakeup_list_lock and calls kvm_vcpu_wake_up(). The wakeup path then attempts to acquire the target task's runqueue lock, establishing the reverse order: gappi_vcpu_wakeup_list_lock -> rq_lock.

This inversion will likely trigger a lockdep ABBA deadlock splat and violates the scheduler locking hierarchy.

[ ... ]

Quoted 5 lines
arch/x86/kvm/svm/avic.c +1 -0
@@ -1308,12 +1405,18 @@ static bool __init avic_want_avic_enabled(void)
  */
 bool __init avic_hardware_setup(void)
 {
+	int cpu;

arch/x86/kvm/svm/avic.c static bool __init avic_want_avic_enabled(void) · line 1418

Quoted 10 lines
arch/x86/kvm/svm/avic.c +4 -0
 	avic = avic_want_avic_enabled();
 	if (!avic)
 		return false;
 
 	pr_info("AVIC enabled\n");
 
+	for_each_possible_cpu(cpu) {
+		INIT_LIST_HEAD(&per_cpu(gappi_vcpu_wakeup_list, cpu));
+		raw_spin_lock_init(&per_cpu(gappi_vcpu_wakeup_list_lock, cpu));
+	}

[Severity: High]
Are blocked vCPUs left stranded if their physical CPU is taken offline?

The per-CPU wakeup lists queue blocked vCPUs on the physical CPU they last ran on. However, there are no CPU hotplug callbacks registered. If a physical CPU is taken offline, the IOMMU hardware will not deliver GAPPI interrupts for that offlined CPU's APIC ID.

Since there is no mechanism to migrate the stranded vCPUs to an online CPU's wakeup list during the offline event, these vCPUs could hang indefinitely.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
#13 lore raw

Re: [RFC PATCH v3 5/6] KVM: SVM: Add support for AMD IOMMU Guest APIC Physical Processor Interrupt (GAPPI)

Quoted 139 lines · On 7/13/2026 4:41 PM, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] Host-userspace-reachable WARN_ON due to uninitialized svm->gappi_cpu (-1) when configuring IRQ routing before a vCPU runs.
- [High] Missing CPU hotplug handling causes blocked vCPUs to hang if their last-run physical CPU goes offline.
- [High] ABBA deadlock between scheduler rq_lock and gappi_vcpu_wakeup_list_lock due to manipulating the wakeup list during vCPU preemption.
- [Low] Mixing scoped guard() cleanup with goto error handling violates architectural guidelines.
--

--- Patch [5]: [RFC PATCH v3 5/6] KVM: SVM: Add support for AMD IOMMU Guest APIC Physical Processor Interrupt (GAPPI) --- commit c3bb8845bd944e9a327f36d68b70032d436f727c
Author: Sairaj Kodilkar <[email protected]>

KVM: SVM: Add support for AMD IOMMU Guest APIC Physical Processor Interrupt (GAPPI)

With AVIC guest-mode interrupt remapping, device interrupts are posted into the guest vAPIC backing page by the IOMMU. When the vCPU is not running (IRTE[IsRun] = 0), KVM must still be notified to schedule it. The legacy path uses the GA log.

Quoted 5 lines
arch/x86/kvm/svm/avic.c +0 -0
index 3b2d92a587098..982a5988532ff 100644

arch/x86/kvm/svm/avic.c int avic_init_vcpu(struct vcpu_svm *svm) · line 901

Quoted 10 lines
arch/x86/kvm/svm/avic.c +7 -0
@@ -886,6 +892,42 @@ int avic_init_vcpu(struct vcpu_svm *svm)
 	return ret;
 }
 
+static void avic_add_vcpu_to_gappi_wakeup_list(struct vcpu_svm *svm, int cpu)
+{
+	struct list_head *wakeup_list;
+	raw_spinlock_t *spinlock;
+
+	if (WARN_ON(unlikely(cpu < 0)))
+		return;

[Severity: High]
Can an unprivileged user-space process trigger this WARN_ON?

When a vCPU is created, svm->gappi_cpu is initialized to -1 in avic_init_vcpu(). It is only set to a valid CPU ID later during __avic_vcpu_load().

If host user-space configures device interrupt routing via the KVM_IRQFD ioctl before the vCPU runs, avic_pi_update_irte() is invoked. This will pass the uninitialized svm->gappi_cpu (-1) down to this function, hitting the WARN_ON. If the host has panic_on_warn enabled, this allows host userspace to trigger a kernel panic.

[ ... ]

Quoted 21 lines
arch/x86/kvm/svm/avic.c +10 -0
@@ -947,6 +993,12 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm,
 		 * scheduled out, KVM will update the pCPU info when the vCPU
 		 * is awakened and/or scheduled in.  See also avic_vcpu_load().
 		 */
+		if (amd_iommu_gappi)
+			pi_data.ga_tag = POSTED_INTR_WAKEUP_VECTOR;
+		else
+			pi_data.ga_tag = AVIC_GATAG(to_kvm_svm(kvm)->avic_vm_id,
+						    vcpu->vcpu_idx);
+
 		entry = svm->avic_physical_id_entry;
 		if (entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK) {
 			pi_data.apicid = entry & AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK;
@@ -955,11 +1007,19 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm,
 			pi_data.apicid = -1;
 			pi_data.wakeup_intr = entry & AVIC_PHYSICAL_ID_ENTRY_WAKEUP_INTR;
 			pi_data.is_running = false;
+
+			if (amd_iommu_gappi) {
+				pi_data.apicid = kvm_cpu_get_apicid(svm->gappi_cpu);
+				if (list_empty(&svm->ir_list)) {

arch/x86/kvm/svm/avic.c int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm, · line 1022

Quoted 10 lines
arch/x86/kvm/svm/avic.c +5 -1
+					avic_add_vcpu_to_gappi_wakeup_list(svm, svm->gappi_cpu);
+					is_vcpu_waiting = true;
+				}
+			}
 		}
 
 		ret = irq_set_vcpu_affinity(host_irq, &pi_data);
 		if (ret)
-			return ret;
+			goto gappi_err_out;

[Severity: Low]
Is it safe to mix goto-based error handling in a function that uses guard()?

Earlier in avic_pi_update_irte(), guard(raw_spinlock_irqsave)(&svm->ir_list_lock) is used. According to include/linux/cleanup.h, mixing goto with guard() in the same function creates confusing ownership semantics and violates the subsystem's cleanup rules.

Quoted 22 lines
arch/x86/kvm/svm/avic.c +6 -1
 		/*
 		 * Revert to legacy mode if the IOMMU didn't provide metadata
@@ -968,12 +1028,17 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm,
 		 */
 		if (WARN_ON_ONCE(!pi_data.ir_data)) {
 			irq_set_vcpu_affinity(host_irq, NULL);
-			return -EIO;
+			ret = -EIO;
+			goto gappi_err_out;
 		}
 
 		irqfd->irq_bypass_data = pi_data.ir_data;
 		list_add(&irqfd->vcpu_list, &svm->ir_list);
 		return 0;
+gappi_err_out:
+		if (is_vcpu_waiting)
+			avic_remove_vcpu_from_gappi_wakeup_list(svm, svm->gappi_cpu);
+		return ret;
 	}
 	return irq_set_vcpu_affinity(host_irq, NULL);
 }

[ ... ]

Quoted 8 lines
arch/x86/kvm/svm/avic.c +4 -0
@@ -1300,6 +1382,21 @@ static bool __init avic_want_avic_enabled(void)
 	return true;
 }
 
+static void avic_gappi_wakeup_handler(void)
+{
+	int cpu = smp_processor_id();
+	struct list_head *vcpu_wakeup_list = &per_cpu(gappi_vcpu_wakeup_list, cpu);

arch/x86/kvm/svm/avic.c static bool __init avic_want_avic_enabled(void) · line 1398

Quoted 10 lines
arch/x86/kvm/svm/avic.c +10 -0
+	raw_spinlock_t *spinlock = &per_cpu(gappi_vcpu_wakeup_list_lock, cpu);
+	struct vcpu_svm *svm;
+
+	raw_spin_lock(spinlock);
+	list_for_each_entry(svm, vcpu_wakeup_list, gappi_vcpu_wakeup_list) {
+		if (kvm_lapic_find_highest_irr(&svm->vcpu) >= 0)
+			kvm_vcpu_wake_up(&svm->vcpu);
+	}
+	raw_spin_unlock(spinlock);
+}

[Severity: High]
Does this lock ordering create an ABBA deadlock?

When a vCPU is preempted, the scheduler executes kvm_sched_out() while holding the runqueue lock (rq_lock). KVM then calls __avic_vcpu_put(), which in turn calls avic_add_vcpu_to_gappi_wakeup_list(). This acquires the gappi_vcpu_wakeup_list_lock, establishing an ordering of rq_lock -> gappi_vcpu_wakeup_list_lock.

Quoted 7 lines

Conversely, when a GAPPI interrupt arrives, avic_gappi_wakeup_handler() acquires gappi_vcpu_wakeup_list_lock and calls kvm_vcpu_wake_up(). The wakeup path then attempts to acquire the target task's runqueue lock, establishing the reverse order: gappi_vcpu_wakeup_list_lock -> rq_lock.

This inversion will likely trigger a lockdep ABBA deadlock splat and violates the scheduler locking hierarchy.

For this condition to exist, Following two conditions must be true.
1. avic_vcpu_put() and gappi interrupt handler() must be on same physical CPU.
2. GAPPI must preempt the __avic_vcpu_put() once it has acquired gappi_vcpu_wakeup_list_lock().

The second condition is not possible since __avic_vcpu_put() disables the interrupt while aquiring the ir_list lock.

Hence ABBA deadlock is not possible

Thanks
Sairaj

Quoted 29 lines

[ ... ]

Quoted 5 lines
arch/x86/kvm/svm/avic.c +1 -0
@@ -1308,12 +1405,18 @@ static bool __init avic_want_avic_enabled(void)
  */
 bool __init avic_hardware_setup(void)
 {
+	int cpu;

arch/x86/kvm/svm/avic.c static bool __init avic_want_avic_enabled(void) · line 1418

Quoted 10 lines
arch/x86/kvm/svm/avic.c +4 -0
 	avic = avic_want_avic_enabled();
 	if (!avic)
 		return false;
 
 	pr_info("AVIC enabled\n");
 
+	for_each_possible_cpu(cpu) {
+		INIT_LIST_HEAD(&per_cpu(gappi_vcpu_wakeup_list, cpu));
+		raw_spin_lock_init(&per_cpu(gappi_vcpu_wakeup_list_lock, cpu));
+	}

[Severity: High]
Are blocked vCPUs left stranded if their physical CPU is taken offline?

The per-CPU wakeup lists queue blocked vCPUs on the physical CPU they last ran on. However, there are no CPU hotplug callbacks registered. If a physical CPU is taken offline, the IOMMU hardware will not deliver GAPPI interrupts for that offlined CPU's APIC ID.

Since there is no mechanism to migrate the stranded vCPUs to an online CPU's wakeup list during the offline event, these vCPUs could hang indefinitely.

#14 lore raw

Re: [RFC PATCH v3 5/6] KVM: SVM: Add support for AMD IOMMU Guest APIC Physical Processor Interrupt (GAPPI)

Quoted 24 lines · Sairaj Kodilkar
Quoted 9 lines · On 7/13/2026 4:41 PM, [email protected] wrote:

[Severity: High]
Does this lock ordering create an ABBA deadlock?

When a vCPU is preempted, the scheduler executes kvm_sched_out() while holding the runqueue lock (rq_lock). KVM then calls __avic_vcpu_put(), which in turn calls avic_add_vcpu_to_gappi_wakeup_list(). This acquires the gappi_vcpu_wakeup_list_lock, establishing an ordering of rq_lock -> gappi_vcpu_wakeup_list_lock.

Quoted 7 lines

Conversely, when a GAPPI interrupt arrives, avic_gappi_wakeup_handler() acquires gappi_vcpu_wakeup_list_lock and calls kvm_vcpu_wake_up(). The wakeup path then attempts to acquire the target task's runqueue lock, establishing the reverse order: gappi_vcpu_wakeup_list_lock -> rq_lock.

This inversion will likely trigger a lockdep ABBA deadlock splat and violates the scheduler locking hierarchy.

For this condition to exist, Following two conditions must be true.
1. avic_vcpu_put() and gappi interrupt handler() must be on same physical CPU.
2. GAPPI must preempt the __avic_vcpu_put() once it has acquired gappi_vcpu_wakeup_list_lock().

Quoted 4 lines

The second condition is not possible since __avic_vcpu_put() disables the interrupt while aquiring the ir_list lock.

Hence ABBA deadlock is not possible

I wouldn't be surprised if you need to tell lockdep that, e.g. see commit c0b8dcabb2cd ("KVM: VMX: Use separate subclasses for PI wakeup lock to squash false positive").

#15 lore raw

Re: [RFC PATCH v3 5/6] KVM: SVM: Add support for AMD IOMMU Guest APIC Physical Processor Interrupt (GAPPI)

Quoted 25 lines · Sean Christopherson
Quoted 24 lines · Sairaj Kodilkar
Quoted 9 lines · On 7/13/2026 4:41 PM, [email protected] wrote:

[Severity: High]
Does this lock ordering create an ABBA deadlock?

When a vCPU is preempted, the scheduler executes kvm_sched_out() while holding the runqueue lock (rq_lock). KVM then calls __avic_vcpu_put(), which in turn calls avic_add_vcpu_to_gappi_wakeup_list(). This acquires the gappi_vcpu_wakeup_list_lock, establishing an ordering of rq_lock -> gappi_vcpu_wakeup_list_lock.

Quoted 7 lines

Conversely, when a GAPPI interrupt arrives, avic_gappi_wakeup_handler() acquires gappi_vcpu_wakeup_list_lock and calls kvm_vcpu_wake_up(). The wakeup path then attempts to acquire the target task's runqueue lock, establishing the reverse order: gappi_vcpu_wakeup_list_lock -> rq_lock.

This inversion will likely trigger a lockdep ABBA deadlock splat and violates the scheduler locking hierarchy.

For this condition to exist, Following two conditions must be true.
1. avic_vcpu_put() and gappi interrupt handler() must be on same physical CPU.
2. GAPPI must preempt the __avic_vcpu_put() once it has acquired gappi_vcpu_wakeup_list_lock().

Quoted 8 lines
Quoted 4 lines

The second condition is not possible since __avic_vcpu_put() disables the interrupt while aquiring the ir_list lock.

Hence ABBA deadlock is not possible

I wouldn't be surprised if you need to tell lockdep that, e.g. see commit c0b8dcabb2cd ("KVM: VMX: Use separate subclasses for PI wakeup lock to squash false positive").

Will do this in next version.

Thanks
Sairaj

#16 lore raw

Re: [RFC PATCH v3 5/6] KVM: SVM: Add support for AMD IOMMU Guest APIC Physical Processor Interrupt (GAPPI)

In reply to [email protected]
Quoted 41 lines · On 7/13/2026 4:41 PM, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] Host-userspace-reachable WARN_ON due to uninitialized svm->gappi_cpu (-1) when configuring IRQ routing before a vCPU runs.
- [High] Missing CPU hotplug handling causes blocked vCPUs to hang if their last-run physical CPU goes offline.
- [High] ABBA deadlock between scheduler rq_lock and gappi_vcpu_wakeup_list_lock due to manipulating the wakeup list during vCPU preemption.
- [Low] Mixing scoped guard() cleanup with goto error handling violates architectural guidelines.
--

--- Patch [5]: [RFC PATCH v3 5/6] KVM: SVM: Add support for AMD IOMMU Guest APIC Physical Processor Interrupt (GAPPI) --- commit c3bb8845bd944e9a327f36d68b70032d436f727c
Author: Sairaj Kodilkar <[email protected]>

KVM: SVM: Add support for AMD IOMMU Guest APIC Physical Processor Interrupt (GAPPI)

With AVIC guest-mode interrupt remapping, device interrupts are posted into the guest vAPIC backing page by the IOMMU. When the vCPU is not running (IRTE[IsRun] = 0), KVM must still be notified to schedule it. The legacy path uses the GA log.

Quoted 5 lines
arch/x86/kvm/svm/avic.c +0 -0
index 3b2d92a587098..982a5988532ff 100644

arch/x86/kvm/svm/avic.c int avic_init_vcpu(struct vcpu_svm *svm) · line 901

Quoted 10 lines
arch/x86/kvm/svm/avic.c +7 -0
@@ -886,6 +892,42 @@ int avic_init_vcpu(struct vcpu_svm *svm)
 	return ret;
 }
 
+static void avic_add_vcpu_to_gappi_wakeup_list(struct vcpu_svm *svm, int cpu)
+{
+	struct list_head *wakeup_list;
+	raw_spinlock_t *spinlock;
+
+	if (WARN_ON(unlikely(cpu < 0)))
+		return;

[Severity: High]
Can an unprivileged user-space process trigger this WARN_ON?

When a vCPU is created, svm->gappi_cpu is initialized to -1 in avic_init_vcpu(). It is only set to a valid CPU ID later during __avic_vcpu_load().

Quoted 5 lines

If host user-space configures device interrupt routing via the KVM_IRQFD ioctl before the vCPU runs, avic_pi_update_irte() is invoked. This will pass the uninitialized svm->gappi_cpu (-1) down to this function, hitting the WARN_ON. If the host has panic_on_warn enabled, this allows host userspace to trigger a kernel panic.

This is a valid concern.

If host userspace attaches a bypass IRQ targeting a vCPU that has never been loaded. Functionally, there is nothing to do in that window. A vCPU that has never been loaded cannot be blocking, so no GAPPI wakeup is required. The IOMMU still posts the interrupt into the vAPIC backing page, and the pending IRR is evaluated at the first VMRUN after avic_vcpu_load(), which is also where the IRTE gets a valid Destination and IsRun = 1.

This can be resolved by assigning a arbitrary gappi destination, without actually updating the gappi wakeup list of that CPU.

Thanks
Sairaj

Quoted 37 lines

[ ... ]

Quoted 21 lines
arch/x86/kvm/svm/avic.c +10 -0
@@ -947,6 +993,12 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm,
 		 * scheduled out, KVM will update the pCPU info when the vCPU
 		 * is awakened and/or scheduled in.  See also avic_vcpu_load().
 		 */
+		if (amd_iommu_gappi)
+			pi_data.ga_tag = POSTED_INTR_WAKEUP_VECTOR;
+		else
+			pi_data.ga_tag = AVIC_GATAG(to_kvm_svm(kvm)->avic_vm_id,
+						    vcpu->vcpu_idx);
+
 		entry = svm->avic_physical_id_entry;
 		if (entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK) {
 			pi_data.apicid = entry & AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK;
@@ -955,11 +1007,19 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm,
 			pi_data.apicid = -1;
 			pi_data.wakeup_intr = entry & AVIC_PHYSICAL_ID_ENTRY_WAKEUP_INTR;
 			pi_data.is_running = false;
+
+			if (amd_iommu_gappi) {
+				pi_data.apicid = kvm_cpu_get_apicid(svm->gappi_cpu);
+				if (list_empty(&svm->ir_list)) {

arch/x86/kvm/svm/avic.c int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm, · line 1022

Quoted 10 lines
arch/x86/kvm/svm/avic.c +5 -1
+					avic_add_vcpu_to_gappi_wakeup_list(svm, svm->gappi_cpu);
+					is_vcpu_waiting = true;
+				}
+			}
 		}
 
 		ret = irq_set_vcpu_affinity(host_irq, &pi_data);
 		if (ret)
-			return ret;
+			goto gappi_err_out;

[Severity: Low]
Is it safe to mix goto-based error handling in a function that uses guard()?

Quoted 5 lines

Earlier in avic_pi_update_irte(), guard(raw_spinlock_irqsave)(&svm->ir_list_lock) is used. According to include/linux/cleanup.h, mixing goto with guard() in the same function creates confusing ownership semantics and violates the subsystem's cleanup rules.

Valid concern

Will remove all the gotos in this function.

Thanks
Sairaj

Quoted 71 lines
Quoted 22 lines
arch/x86/kvm/svm/avic.c +6 -1
 		/*
 		 * Revert to legacy mode if the IOMMU didn't provide metadata
@@ -968,12 +1028,17 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm,
 		 */
 		if (WARN_ON_ONCE(!pi_data.ir_data)) {
 			irq_set_vcpu_affinity(host_irq, NULL);
-			return -EIO;
+			ret = -EIO;
+			goto gappi_err_out;
 		}
 
 		irqfd->irq_bypass_data = pi_data.ir_data;
 		list_add(&irqfd->vcpu_list, &svm->ir_list);
 		return 0;
+gappi_err_out:
+		if (is_vcpu_waiting)
+			avic_remove_vcpu_from_gappi_wakeup_list(svm, svm->gappi_cpu);
+		return ret;
 	}
 	return irq_set_vcpu_affinity(host_irq, NULL);
 }

[ ... ]

Quoted 18 lines
arch/x86/kvm/svm/avic.c +14 -0
@@ -1300,6 +1382,21 @@ static bool __init avic_want_avic_enabled(void)
 	return true;
 }
 
+static void avic_gappi_wakeup_handler(void)
+{
+	int cpu = smp_processor_id();
+	struct list_head *vcpu_wakeup_list = &per_cpu(gappi_vcpu_wakeup_list, cpu);
+	raw_spinlock_t *spinlock = &per_cpu(gappi_vcpu_wakeup_list_lock, cpu);
+	struct vcpu_svm *svm;
+
+	raw_spin_lock(spinlock);
+	list_for_each_entry(svm, vcpu_wakeup_list, gappi_vcpu_wakeup_list) {
+		if (kvm_lapic_find_highest_irr(&svm->vcpu) >= 0)
+			kvm_vcpu_wake_up(&svm->vcpu);
+	}
+	raw_spin_unlock(spinlock);
+}

[ ... ]

Quoted 5 lines
arch/x86/kvm/svm/avic.c +1 -0
@@ -1308,12 +1405,18 @@ static bool __init avic_want_avic_enabled(void)
  */
 bool __init avic_hardware_setup(void)
 {
+	int cpu;

arch/x86/kvm/svm/avic.c static bool __init avic_want_avic_enabled(void) · line 1418

Quoted 10 lines
arch/x86/kvm/svm/avic.c +4 -0
 	avic = avic_want_avic_enabled();
 	if (!avic)
 		return false;
 
 	pr_info("AVIC enabled\n");
 
+	for_each_possible_cpu(cpu) {
+		INIT_LIST_HEAD(&per_cpu(gappi_vcpu_wakeup_list, cpu));
+		raw_spin_lock_init(&per_cpu(gappi_vcpu_wakeup_list_lock, cpu));
+	}

[Severity: High]
Are blocked vCPUs left stranded if their physical CPU is taken offline?

The per-CPU wakeup lists queue blocked vCPUs on the physical CPU they last ran on. However, there are no CPU hotplug callbacks registered. If a physical CPU is taken offline, the IOMMU hardware will not deliver GAPPI interrupts for that offlined CPU's APIC ID.

Since there is no mechanism to migrate the stranded vCPUs to an online CPU's wakeup list during the offline event, these vCPUs could hang indefinitely.

#17 lore raw

Re: [RFC PATCH v3 5/6] KVM: SVM: Add support for AMD IOMMU Guest APIC Physical Processor Interrupt (GAPPI)

Quoted 24 lines · Sairaj Kodilkar
Quoted 7 lines · On 7/13/2026 4:41 PM, [email protected] wrote:

[Severity: High]
Can an unprivileged user-space process trigger this WARN_ON?

When a vCPU is created, svm->gappi_cpu is initialized to -1 in avic_init_vcpu(). It is only set to a valid CPU ID later during __avic_vcpu_load().

Quoted 5 lines

If host user-space configures device interrupt routing via the KVM_IRQFD ioctl before the vCPU runs, avic_pi_update_irte() is invoked. This will pass the uninitialized svm->gappi_cpu (-1) down to this function, hitting the WARN_ON. If the host has panic_on_warn enabled, this allows host userspace to trigger a kernel panic.

This is a valid concern.

If host userspace attaches a bypass IRQ targeting a vCPU that has never been loaded. Functionally, there is nothing to do in that window. A vCPU that has never been loaded cannot be blocking, so no GAPPI wakeup is required. The IOMMU still posts the interrupt into the vAPIC backing page, and the pending IRR is evaluated at the first VMRUN after avic_vcpu_load(), which is also where the IRTE gets a valid Destination and IsRun = 1.

Quoted 2 lines

This can be resolved by assigning a arbitrary gappi destination, without actually updating the gappi wakeup list of that CPU.

With the disclaimer that I haven't look super closely at this series, and haven't thought too deeply about the feature itself either...

Why are we doing anything different than what VMX does? vCPUs on the wakeup list when they block, and come off the list when they wakeup. It's literally one flow that's guarantee to pair put()+load(), and the logic for manipulating the list is quite simple as a result.

Going a step further, why is GAPPI not sharing code with VMX Posted Interupts? At a glance, the only meaningful difference in the wakeup flow is the "should this particular vCPU be awakened".

#18 lore raw

Re: [RFC PATCH v3 5/6] KVM: SVM: Add support for AMD IOMMU Guest APIC Physical Processor Interrupt (GAPPI)

Quoted 28 lines · Sean Christopherson
Quoted 24 lines · Sairaj Kodilkar
Quoted 7 lines · On 7/13/2026 4:41 PM, [email protected] wrote:

[Severity: High]
Can an unprivileged user-space process trigger this WARN_ON?

When a vCPU is created, svm->gappi_cpu is initialized to -1 in avic_init_vcpu(). It is only set to a valid CPU ID later during __avic_vcpu_load().

Quoted 5 lines

If host user-space configures device interrupt routing via the KVM_IRQFD ioctl before the vCPU runs, avic_pi_update_irte() is invoked. This will pass the uninitialized svm->gappi_cpu (-1) down to this function, hitting the WARN_ON. If the host has panic_on_warn enabled, this allows host userspace to trigger a kernel panic.

This is a valid concern.

If host userspace attaches a bypass IRQ targeting a vCPU that has never been loaded. Functionally, there is nothing to do in that window. A vCPU that has never been loaded cannot be blocking, so no GAPPI wakeup is required. The IOMMU still posts the interrupt into the vAPIC backing page, and the pending IRR is evaluated at the first VMRUN after avic_vcpu_load(), which is also where the IRTE gets a valid Destination and IsRun = 1.

Quoted 2 lines

This can be resolved by assigning a arbitrary gappi destination, without actually updating the gappi wakeup list of that CPU.

Quoted 8 lines

With the disclaimer that I haven't look super closely at this series, and haven't thought too deeply about the feature itself either...

Why are we doing anything different than what VMX does? vCPUs on the wakeup list when they block, and come off the list when they wakeup. It's literally one flow that's guarantee to pair put()+load(), and the logic for manipulating the list is quite simple as a result.

I was trying to manipulate the vCPU list after is_empty(ir_list) check in put() and load() path. Which complicated the things, since pi_update_irte() will have to add the vCPU to the list if it was a first interrupt assigned to given vCPU.

I think its better to keep the list operations before is_empty(ir_list) check in order to simplify things a little bit.Note that it may increase the list size and potentially increasing time for gappi_wakeup_handler.

Quoted 3 lines

Going a step further, why is GAPPI not sharing code with VMX Posted Interupts? At a glance, the only meaningful difference in the wakeup flow is the "should this particular vCPU be awakened".

On Intel there is a level of indirection: KVM hands the IOMMU the physical address of the PI descriptor once, at IRQ affinity setup time (vmx_pi_update_irte()). After that the descriptor is the only thing that needs updating, so vcpu_load()/vcpu_put() just write NDST/NV/SN in memory and never call into the IOMMU driver again.

AMD has no such indirection. The destination APIC ID, IsRun and GATag live directly in the IRTE, and there is no per-vCPU structure that the IOMMU dereferences. So every vcpu_load()/vcpu_put() has to call into the AMD IOMMU driver to update each IRTE targeting the vCPU, via avic_update_iommu_vcpu_affinity() -> amd_iommu_update_ga().

That difference leaks into the wakeup list handling. On Intel, putting a vCPU on the per-pCPU list is self-contained. On AMD it has to happen in the same ir_list_lock critical section as the IRTE update, and it is conditional on the vCPU actually having posted IRQs (ir_list being non empty), because the pCPU that the vCPU is enqueued on is also what gets programmed into IRTE[Destination].

Because of above complications I did not try to factor out the common code.

Thanks
Sairaj

#19 lore raw

Re: [RFC PATCH v3 5/6] KVM: SVM: Add support for AMD IOMMU Guest APIC Physical Processor Interrupt (GAPPI)

Quoted 39 lines · Sairaj Kodilkar
Quoted 28 lines · Sean Christopherson
Quoted 24 lines · Sairaj Kodilkar
Quoted 7 lines · On 7/13/2026 4:41 PM, [email protected] wrote:

[Severity: High]
Can an unprivileged user-space process trigger this WARN_ON?

When a vCPU is created, svm->gappi_cpu is initialized to -1 in avic_init_vcpu(). It is only set to a valid CPU ID later during __avic_vcpu_load().

Quoted 5 lines

If host user-space configures device interrupt routing via the KVM_IRQFD ioctl before the vCPU runs, avic_pi_update_irte() is invoked. This will pass the uninitialized svm->gappi_cpu (-1) down to this function, hitting the WARN_ON. If the host has panic_on_warn enabled, this allows host userspace to trigger a kernel panic.

This is a valid concern.

If host userspace attaches a bypass IRQ targeting a vCPU that has never been loaded. Functionally, there is nothing to do in that window. A vCPU that has never been loaded cannot be blocking, so no GAPPI wakeup is required. The IOMMU still posts the interrupt into the vAPIC backing page, and the pending IRR is evaluated at the first VMRUN after avic_vcpu_load(), which is also where the IRTE gets a valid Destination and IsRun = 1.

Quoted 2 lines

This can be resolved by assigning a arbitrary gappi destination, without actually updating the gappi wakeup list of that CPU.

Quoted 7 lines

With the disclaimer that I haven't look super closely at this series, and haven't thought too deeply about the feature itself either...

Why are we doing anything different than what VMX does? vCPUs on the wakeup list when they block, and come off the list when they wakeup. It's literally one flow that's guarantee to pair put()+load(), and the logic for manipulating the list is quite simple as a result.

Quoted 4 lines

I was trying to manipulate the vCPU list after is_empty(ir_list) check in put() and load() path. Which complicated the things, since pi_update_irte() will have to add the vCPU to the list if it was a first interrupt assigned to given vCPU.

No, there are other options. Specifically, the solution VMX uses is to put the vCPU on the wakeup list if it *might* get a posted IRQ. Again, why reinvent the wheel?

Quoted 20 lines

I think its better to keep the list operations before is_empty(ir_list) check in order to simplify things a little bit.Note that it may increase the list size and potentially increasing time for gappi_wakeup_handler.

Quoted 3 lines

Going a step further, why is GAPPI not sharing code with VMX Posted Interupts? At a glance, the only meaningful difference in the wakeup flow is the "should this particular vCPU be awakened".

On Intel there is a level of indirection: KVM hands the IOMMU the physical address of the PI descriptor once, at IRQ affinity setup time (vmx_pi_update_irte()). After that the descriptor is the only thing that needs updating, so vcpu_load()/vcpu_put() just write NDST/NV/SN in memory and never call into the IOMMU driver again.

AMD has no such indirection. The destination APIC ID, IsRun and GATag live directly in the IRTE, and there is no per-vCPU structure that the IOMMU dereferences. So every vcpu_load()/vcpu_put() has to call into the AMD IOMMU driver to update each IRTE targeting the vCPU, via avic_update_iommu_vcpu_affinity() -> amd_iommu_update_ga().

Quoted 6 lines

That difference leaks into the wakeup list handling. On Intel, putting a vCPU on the per-pCPU list is self-contained. On AMD it has to happen in the same ir_list_lock critical section as the IRTE update, and it is conditional on the vCPU actually having posted IRQs (ir_list being non empty), because the pCPU that the vCPU is enqueued on is also what gets programmed into IRTE[Destination].

And? That's literally calling one function from a slightly difference location on Intel vs. AMD. E.g. unless I'm missing something, the handling should be something like this, sans actual code movement:

x86/kvm/svm/avic.c +3 -0
index c5b1d294b15a..c9a6da646c8e 100644
@@ -1020,6 +1020,9 @@ static void avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int cpu,
 
        lockdep_assert_held(&svm->ir_list_lock);
 
+       if (ga_log_intr && gappi && kvm_can_use_device_posted_irqs(vcpu->kvm))
+               kvm_pi_enable_wakeup_handler(vcpu);
+
        /*
         * Here, we go through the per-vcpu ir_list to update all existing
         * interrupt remapping table entry targeting this vcpu.
x86/kvm/vmx/posted_intr.c +3 -3
index 4a6d9a17da23..f6e3fea11baa 100644
@@ -144,7 +144,7 @@ void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
                pi_set_on(pi_desc);
 }
 
-static bool vmx_can_use_vtd_pi(struct kvm *kvm)
+static bool kvm_can_use_device_posted_irqs(struct kvm *kvm)
 {
        /*
         * Note, reading the number of possible bypass IRQs can race with a
@@ -217,7 +217,7 @@ static bool vmx_needs_pi_wakeup(struct kvm_vcpu *vcpu)
         * back to the pi_wakeup_handler() function.
         */
        return (vmx_can_use_ipiv(vcpu) && !is_td_vcpu(vcpu)) ||
-               vmx_can_use_vtd_pi(vcpu->kvm);
+               kvm_can_use_device_posted_irqs(vcpu->kvm);
 }
 
 void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
@@ -242,7 +242,7 @@ void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
        if (!vcpu->preempted && kvm_vcpu_is_blocking(vcpu) &&
            ((is_td_vcpu(vcpu) && tdx_interrupt_allowed(vcpu)) ||
             (!is_td_vcpu(vcpu) && !vmx_interrupt_blocked(vcpu))))
-               pi_enable_wakeup_handler(vcpu);
+               kvm_pi_enable_wakeup_handler(vcpu);
        else
                pi_set_sn(pi_desc);
 }
#20 lore raw

Re: [RFC PATCH v3 5/6] KVM: SVM: Add support for AMD IOMMU Guest APIC Physical Processor Interrupt (GAPPI)

Quoted 85 lines · Sean Christopherson
Quoted 39 lines · Sairaj Kodilkar
Quoted 28 lines · Sean Christopherson
Quoted 24 lines · Sairaj Kodilkar
Quoted 7 lines · On 7/13/2026 4:41 PM, [email protected] wrote:

[Severity: High]
Can an unprivileged user-space process trigger this WARN_ON?

When a vCPU is created, svm->gappi_cpu is initialized to -1 in avic_init_vcpu(). It is only set to a valid CPU ID later during __avic_vcpu_load().

Quoted 5 lines

If host user-space configures device interrupt routing via the KVM_IRQFD ioctl before the vCPU runs, avic_pi_update_irte() is invoked. This will pass the uninitialized svm->gappi_cpu (-1) down to this function, hitting the WARN_ON. If the host has panic_on_warn enabled, this allows host userspace to trigger a kernel panic.

This is a valid concern.

If host userspace attaches a bypass IRQ targeting a vCPU that has never been loaded. Functionally, there is nothing to do in that window. A vCPU that has never been loaded cannot be blocking, so no GAPPI wakeup is required. The IOMMU still posts the interrupt into the vAPIC backing page, and the pending IRR is evaluated at the first VMRUN after avic_vcpu_load(), which is also where the IRTE gets a valid Destination and IsRun = 1.

Quoted 2 lines

This can be resolved by assigning a arbitrary gappi destination, without actually updating the gappi wakeup list of that CPU.

Quoted 7 lines

With the disclaimer that I haven't look super closely at this series, and haven't thought too deeply about the feature itself either...

Why are we doing anything different than what VMX does? vCPUs on the wakeup list when they block, and come off the list when they wakeup. It's literally one flow that's guarantee to pair put()+load(), and the logic for manipulating the list is quite simple as a result.

Quoted 4 lines

I was trying to manipulate the vCPU list after is_empty(ir_list) check in put() and load() path. Which complicated the things, since pi_update_irte() will have to add the vCPU to the list if it was a first interrupt assigned to given vCPU.

No, there are other options. Specifically, the solution VMX uses is to put the vCPU on the wakeup list if it *might* get a posted IRQ. Again, why reinvent the wheel?

Quoted 20 lines

I think its better to keep the list operations before is_empty(ir_list) check in order to simplify things a little bit.Note that it may increase the list size and potentially increasing time for gappi_wakeup_handler.

Quoted 3 lines

Going a step further, why is GAPPI not sharing code with VMX Posted Interupts? At a glance, the only meaningful difference in the wakeup flow is the "should this particular vCPU be awakened".

On Intel there is a level of indirection: KVM hands the IOMMU the physical address of the PI descriptor once, at IRQ affinity setup time (vmx_pi_update_irte()). After that the descriptor is the only thing that needs updating, so vcpu_load()/vcpu_put() just write NDST/NV/SN in memory and never call into the IOMMU driver again.

AMD has no such indirection. The destination APIC ID, IsRun and GATag live directly in the IRTE, and there is no per-vCPU structure that the IOMMU dereferences. So every vcpu_load()/vcpu_put() has to call into the AMD IOMMU driver to update each IRTE targeting the vCPU, via avic_update_iommu_vcpu_affinity() -> amd_iommu_update_ga().

Quoted 6 lines

That difference leaks into the wakeup list handling. On Intel, putting a vCPU on the per-pCPU list is self-contained. On AMD it has to happen in the same ir_list_lock critical section as the IRTE update, and it is conditional on the vCPU actually having posted IRQs (ir_list being non empty), because the pCPU that the vCPU is enqueued on is also what gets programmed into IRTE[Destination].

And? That's literally calling one function from a slightly difference location on Intel vs. AMD. E.g. unless I'm missing something, the handling should be something like this, sans actual code movement:

x86/kvm/svm/avic.c +0 -0
index c5b1d294b15a..c9a6da646c8e 100644

x86/kvm/svm/avic.c static void avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int cpu, · line 1025

Quoted 6 lines
x86/kvm/svm/avic.c +3 -0
@@ -1020,6 +1020,9 @@ static void avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int cpu,
 
        lockdep_assert_held(&svm->ir_list_lock);
 
+       if (ga_log_intr && gappi && kvm_can_use_device_posted_irqs(vcpu->kvm))
+               kvm_pi_enable_wakeup_handler(vcpu);
+

I am not sure what you mean here, because pi_enable_wakeup_handler does more than just manipulating the wakeup list, it also updates pi descriptor which is Intel specific, We have following two options:

Option A:
Adding a new x86_ops

avic_update_iommu_vcpu_affinity
kvm_pi_enable_wakeup_handler
list handling
kvm_x86_callback(configure_pi)
IOMMU callbacks

The new x86_ops makes sure that irte and pi descriptor are configured correctly.

Option B:
Common list handling

The svm and vmx will share function for list handling but IRTE/PI setup code will remain same.

avic_update_iommu_vcpu_affinity/pi_enable_wakeup_handler
kvm_add_to_wakeup_list
list handling
IOMMU callbacks

Thanks
Sairaj

Quoted 34 lines
x86/kvm/svm/avic.c +0 -0
        /*
         * Here, we go through the per-vcpu ir_list to update all existing
         * interrupt remapping table entry targeting this vcpu.
x86/kvm/vmx/posted_intr.c +3 -3
index 4a6d9a17da23..f6e3fea11baa 100644
@@ -144,7 +144,7 @@ void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
                pi_set_on(pi_desc);
 }
 
-static bool vmx_can_use_vtd_pi(struct kvm *kvm)
+static bool kvm_can_use_device_posted_irqs(struct kvm *kvm)
 {
        /*
         * Note, reading the number of possible bypass IRQs can race with a
@@ -217,7 +217,7 @@ static bool vmx_needs_pi_wakeup(struct kvm_vcpu *vcpu)
         * back to the pi_wakeup_handler() function.
         */
        return (vmx_can_use_ipiv(vcpu) && !is_td_vcpu(vcpu)) ||
-               vmx_can_use_vtd_pi(vcpu->kvm);
+               kvm_can_use_device_posted_irqs(vcpu->kvm);
 }
 
 void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
@@ -242,7 +242,7 @@ void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
        if (!vcpu->preempted && kvm_vcpu_is_blocking(vcpu) &&
            ((is_td_vcpu(vcpu) && tdx_interrupt_allowed(vcpu)) ||
             (!is_td_vcpu(vcpu) && !vmx_interrupt_blocked(vcpu))))
-               pi_enable_wakeup_handler(vcpu);
+               kvm_pi_enable_wakeup_handler(vcpu);
        else
                pi_set_sn(pi_desc);
 }
#21 lore raw

Re: [RFC PATCH v3 5/6] KVM: SVM: Add support for AMD IOMMU Guest APIC Physical Processor Interrupt (GAPPI)

Quoted 9 lines · Sairaj Kodilkar
Quoted 8 lines · Sean Christopherson
x86/kvm/svm/avic.c +0 -0
index c5b1d294b15a..c9a6da646c8e 100644
@@ -1020,6 +1020,9 @@ static void avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int cpu,
 
        lockdep_assert_held(&svm->ir_list_lock);
 
Quoted 7 lines

x86/kvm/svm/avic.c static void avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int cpu, · line 1025

Quoted 3 lines
x86/kvm/svm/avic.c +3 -0
+       if (ga_log_intr && gappi && kvm_can_use_device_posted_irqs(vcpu->kvm))
+               kvm_pi_enable_wakeup_handler(vcpu);
+

I am not sure what you mean here, because pi_enable_wakeup_handler does more than just manipulating the wakeup list, it also updates pi descriptor which is Intel specific,

Sorry, got ahead of myself. I was envisioning a helper that for just the common part, e.g.

void kvm_pi_enable_wakeup_handler(struct kvm_vcpu *vcpu)

{
	lockdep_assert_irqs_disabled();

	/*
	 * Acquire the wakeup lock using the "sched out" context to workaround
	 * a lockdep false positive.  When this is called, schedule() holds
	 * various per-CPU scheduler locks.  When the wakeup handler runs, it
	 * holds this CPU's wakeup lock while calling try_to_wake_up(), which
	 * can eventually take the aforementioned scheduler locks, which causes
	 * lockdep to assume there is deadlock.
	 *
	 * Deadlock can't actually occur because IRQs are disabled for the
	 * entirety of the sched_out critical section, i.e. the wakeup handler
	 * can't run while the scheduler locks are held.
	 */
	raw_spin_lock_nested(&per_cpu(wakeup_vcpus_on_cpu_lock, vcpu->cpu),
			     PI_LOCK_SCHED_OUT);
	list_add_tail(&vcpu->pi_wakeup_list,
		      &per_cpu(wakeup_vcpus_on_cpu, vcpu->cpu));
	raw_spin_unlock(&per_cpu(wakeup_vcpus_on_cpu_lock, vcpu->cpu));
}
Quoted 18 lines

We have following two options:

Option A:
Adding a new x86_ops

avic_update_iommu_vcpu_affinity
kvm_pi_enable_wakeup_handler
list handling
kvm_x86_callback(configure_pi)
IOMMU callbacks

The new x86_ops makes sure that irte and pi descriptor are configured correctly.

Option B:
Common list handling

Quoted 7 lines

The svm and vmx will share function for list handling but IRTE/PI setup code will remain same.

avic_update_iommu_vcpu_affinity/pi_enable_wakeup_handler
kvm_add_to_wakeup_list
list handling
IOMMU callbacks

Definitely option B.

#22 lore raw

RFC PATCH v3 6/6 iommu/amd: Provide kernel command line option to enable GAPPI

In reply to Sairaj Kodilkar

Enable GAPPI when the IOMMU GAPPISup extended feature bit is set and the kernel is booted with the amd_iommu=gappi kernel parameter.

Document the option in Documentation/admin-guide/kernel-parameters.txt.

---
 .../admin-guide/kernel-parameters.txt         |  3 +-
 drivers/iommu/amd/amd_iommu_types.h           |  2 ++
 drivers/iommu/amd/init.c                      | 28 +++++++++++++++++--
 3 files changed, 29 insertions(+), 4 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +2 -1
index 97007f4f69d4..d27f10cbb004 100644
@@ -455,7 +455,8 @@ Kernel parameters
 				          to 4 KiB.
 			v2_pgsizes_only - Limit page-sizes used for v1 page-tables
 				          to 4KiB/2Mib/1GiB.
-
+			gappi		- Use GAPPI instead of GALOG for vCPU
+					  wakeup notification.
 
 	amd_iommu_dump=	[HW,X86-64]
 			Enable AMD IOMMU driver option to dump the ACPI table
drivers/iommu/amd/amd_iommu_types.h +2 -0
index 26d7a9796e64..de0b23306944 100644
@@ -94,6 +94,7 @@
 #define FEATURE_X2APIC		BIT_ULL(2)
 #define FEATURE_NX		BIT_ULL(3)
 #define FEATURE_GT		BIT_ULL(4)
+#define FEATURE_GAPPI		BIT_ULL(5)
 #define FEATURE_IA		BIT_ULL(6)
 #define FEATURE_GA		BIT_ULL(7)
 #define FEATURE_HE		BIT_ULL(8)
@@ -191,6 +192,7 @@
 #define CONTROL_EPH_EN		45
 #define CONTROL_XT_EN		50
 #define CONTROL_INTCAPXT_EN	51
+#define CONTROL_GAPPI_EN	55
 #define CONTROL_GCR3TRPMODE	58
 #define CONTROL_IRTCACHEDIS	59
 #define CONTROL_SNPAVIC_EN	61
drivers/iommu/amd/init.c +25 -3
index 2e1889f8a9e4..7d2b1c24f6ec 100644
@@ -465,6 +465,9 @@ static void iommu_disable(struct amd_iommu *iommu)
 	iommu_feature_disable(iommu, CONTROL_GALOG_EN);
 	iommu_feature_disable(iommu, CONTROL_GAINT_EN);
 
+	/* Disable IOMMU GAPPI */
+	iommu_feature_disable(iommu, CONTROL_GAPPI_EN);
+
 	/* Disable IOMMU PPR logging */
 	iommu_feature_disable(iommu, CONTROL_PPRLOG_EN);
 	iommu_feature_disable(iommu, CONTROL_PPRINT_EN);
@@ -2999,6 +3002,13 @@ static void enable_iommus_vapic(void)
 	struct amd_iommu *iommu;
 
 	for_each_iommu(iommu) {
+		/*
+		 * Disable GAPPI, do not check amd_iommu_gappi as it may be
+		 * false in new kexec kernel even though previous kernel has
+		 * enabled it.
+		 */
+		iommu_feature_disable(iommu, CONTROL_GAPPI_EN);
+
 		/*
 		 * Disable GALog if already running. It could have been enabled
 		 * in the previous boot before kdump.
@@ -3038,10 +3048,19 @@ static void enable_iommus_vapic(void)
 		return;
 	}
 
+	if (amd_iommu_gappi &&
+	    !(check_feature(FEATURE_GAPPI) &&
+	      AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))) {
+		pr_warn("GAPPI is not supported.\n");
+		amd_iommu_gappi = false;
+	}
+
 	/* Enabling GAM and SNPAVIC support */
 	for_each_iommu(iommu) {
-		if (iommu_init_ga_log(iommu) ||
-		    iommu_ga_log_enable(iommu))
+		if (amd_iommu_gappi)
+			iommu_feature_enable(iommu, CONTROL_GAPPI_EN);
+		else if (iommu_init_ga_log(iommu) ||
+			 iommu_ga_log_enable(iommu))
 			return;
 
 		iommu_feature_enable(iommu, CONTROL_GAM_EN);
@@ -3050,7 +3069,8 @@ static void enable_iommus_vapic(void)
 	}
 
 	amd_iommu_irq_ops.capability |= (1 << IRQ_POSTING_CAP);
-	pr_info("Virtual APIC enabled\n");
+	pr_info("Virtual APIC enabled with %s\n",
+		amd_iommu_gappi ? "GAPPI" : "GALOG");
 #endif
 }
 
@@ -3741,6 +3761,8 @@ static int __init parse_amd_iommu_options(char *str)
 		} else if (strncmp(str, "v2_pgsizes_only", 15) == 0) {
 			pr_info("Restricting V1 page-sizes to 4KiB/2MiB/1GiB");
 			amd_iommu_pgsize_bitmap = AMD_IOMMU_PGSIZES_V2;
+		} else if (strncmp(str, "gappi", 5) == 0) {
+			amd_iommu_gappi = true;
 		} else {
 			pr_notice("Unknown option - '%s'\n", str);
 		}
Signature · 1 line
2.34.1
#23 lore raw

Re: [RFC PATCH v3 6/6] iommu/amd: Provide kernel command line option to enable GAPPI

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

New issues:
- [Critical] Unconditional enablement of GA log on overflow in kexec/kdump environments with GAPPI leads to critical DMA memory corruption.

Pre-existing issues:
- [High] Lockless read-modify-write of the IOMMU MMIO control register causes a race condition that can permanently disable IOMMU interrupts or GAPPI. --

--- Patch [6]: [RFC PATCH v3 6/6] iommu/amd: Provide kernel command line option to enable GAPPI --- commit 38ebc4ddc54202efd2ae74234b7ee4169af3e39a
Author: Sairaj Kodilkar <[email protected]>

iommu/amd: Provide kernel command line option to enable GAPPI

This commit adds a kernel parameter to enable GAPPI for AMD IOMMUs when the GAPPISup extended feature bit is set. It also modifies initialization code to handle GAPPI alongside the existing GALOG feature.

Quoted 4 lines
drivers/iommu/amd/init.c +0 -0
index 2e1889f8a9e42..7d2b1c24f6ecc 100644

[ ... ]

drivers/iommu/amd/init.c static void enable_iommus_vapic(void) · line 3010

Quoted 10 lines
drivers/iommu/amd/init.c +6 -0
@@ -2999,6 +3002,13 @@ static void enable_iommus_vapic(void)
 	struct amd_iommu *iommu;
 
 	for_each_iommu(iommu) {
+		/*
+		 * Disable GAPPI, do not check amd_iommu_gappi as it may be
+		 * false in new kexec kernel even though previous kernel has
+		 * enabled it.
+		 */
+		iommu_feature_disable(iommu, CONTROL_GAPPI_EN);

[Severity: High]
This is a pre-existing issue, but does this unlocked modification of the IOMMU MMIO control register cause a race condition that could disable interrupts or other features?

The iommu_feature_disable() function performs a read-modify-write without holding iommu->lock:

drivers/iommu/amd/init.c:iommu_feature_set() {
    ctrl = readq(iommu->mmio_base +  MMIO_CONTROL_OFFSET);
    ctrl &= ~((u64)mask << shift);
    ctrl |= ((u64)val << shift) & ((u64)mask << shift);
    writeq(ctrl, iommu->mmio_base +  MMIO_CONTROL_OFFSET);
}

During initialization, the IRQ thread could be running concurrently. If a log overflow occurs, the IRQ thread calls amd_iommu_restart_log() which also invokes iommu_feature_disable().

Since this read-modify-write isn't synchronized, could concurrent modifications silently overwrite each other?

Quoted 3 lines
drivers/iommu/amd/init.c +1 -0
+
 		/*
 		 * Disable GALog if already running. It could have been enabled

[ ... ]

Quoted 11 lines
drivers/iommu/amd/init.c +7 -0
@@ -3038,10 +3048,19 @@ static void enable_iommus_vapic(void)
 		return;
 	}
 
+	if (amd_iommu_gappi &&
+	    !(check_feature(FEATURE_GAPPI) &&
+	      AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))) {
+		pr_warn("GAPPI is not supported.\n");
+		amd_iommu_gappi = false;
+	}
+

drivers/iommu/amd/init.c static void enable_iommus_vapic(void) · line 3065

Quoted 10 lines
drivers/iommu/amd/init.c +4 -2
 	/* Enabling GAM and SNPAVIC support */
 	for_each_iommu(iommu) {
-		if (iommu_init_ga_log(iommu) ||
-		    iommu_ga_log_enable(iommu))
+		if (amd_iommu_gappi)
+			iommu_feature_enable(iommu, CONTROL_GAPPI_EN);
+		else if (iommu_init_ga_log(iommu) ||
+			 iommu_ga_log_enable(iommu))
 			return;
 

[Severity: Critical]
If the system is booted into a kdump kernel with GAPPI enabled, could this lead to the IOMMU writing to stale physical memory from the previous kernel?

When GAPPI is used, iommu_init_ga_log() is skipped, meaning the ga_log buffer is not allocated. However, if the previous kernel crashed with a pending GA log overflow, the hardware's overflow status bit remains set.

Once shared IOMMU interrupts are enabled, the pending overflow status triggers the interrupt thread:

drivers/iommu/amd/iommu.c:amd_iommu_int_thread_galog() {
amd_iommu_handle_irq(data, "GA", MMIO_STATUS_GALOG_INT_MASK,
MMIO_STATUS_GALOG_OVERFLOW_MASK, iommu_poll_ga_log, amd_iommu_restart_ga_log);
}

This calls amd_iommu_restart_ga_log() which unconditionally enables CONTROL_GALOG_EN:

drivers/iommu/amd/init.c:amd_iommu_restart_ga_log() {
amd_iommu_restart_log(iommu, "GA", CONTROL_GAINT_EN,
CONTROL_GALOG_EN, MMIO_STATUS_GALOG_RUN_MASK, MMIO_STATUS_GALOG_OVERFLOW_MASK);
}

Since CONTROL_GALOG_EN is re-enabled without checking if GAPPI is active or if the log memory was allocated, will the IOMMU resume writing GA log entries to the uninitialized physical address left by the crashed kernel?

Quoted 2 lines
drivers/iommu/amd/init.c +0 -0
 		iommu_feature_enable(iommu, CONTROL_GAM_EN);

if (amd_iommu_snp_en)

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
#24 lore raw

Re: [RFC PATCH v3 6/6] iommu/amd: Provide kernel command line option to enable GAPPI

Quoted 113 lines · On 7/13/2026 4:41 PM, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

New issues:
- [Critical] Unconditional enablement of GA log on overflow in kexec/kdump environments with GAPPI leads to critical DMA memory corruption.

Pre-existing issues:
- [High] Lockless read-modify-write of the IOMMU MMIO control register causes a race condition that can permanently disable IOMMU interrupts or GAPPI. --

--- Patch [6]: [RFC PATCH v3 6/6] iommu/amd: Provide kernel command line option to enable GAPPI --- commit 38ebc4ddc54202efd2ae74234b7ee4169af3e39a
Author: Sairaj Kodilkar <[email protected]>

iommu/amd: Provide kernel command line option to enable GAPPI

This commit adds a kernel parameter to enable GAPPI for AMD IOMMUs when the GAPPISup extended feature bit is set. It also modifies initialization code to handle GAPPI alongside the existing GALOG feature.

Quoted 4 lines
drivers/iommu/amd/init.c +0 -0
index 2e1889f8a9e42..7d2b1c24f6ecc 100644

[ ... ]

drivers/iommu/amd/init.c static void enable_iommus_vapic(void) · line 3010

Quoted 10 lines
drivers/iommu/amd/init.c +6 -0
@@ -2999,6 +3002,13 @@ static void enable_iommus_vapic(void)
 	struct amd_iommu *iommu;
 
 	for_each_iommu(iommu) {
+		/*
+		 * Disable GAPPI, do not check amd_iommu_gappi as it may be
+		 * false in new kexec kernel even though previous kernel has
+		 * enabled it.
+		 */
+		iommu_feature_disable(iommu, CONTROL_GAPPI_EN);

[Severity: High]
This is a pre-existing issue, but does this unlocked modification of the IOMMU MMIO control register cause a race condition that could disable interrupts or other features?

The iommu_feature_disable() function performs a read-modify-write without holding iommu->lock:

drivers/iommu/amd/init.c:iommu_feature_set() {
    ctrl = readq(iommu->mmio_base +  MMIO_CONTROL_OFFSET);
    ctrl &= ~((u64)mask << shift);
    ctrl |= ((u64)val << shift) & ((u64)mask << shift);
    writeq(ctrl, iommu->mmio_base +  MMIO_CONTROL_OFFSET);
}

During initialization, the IRQ thread could be running concurrently. If a log overflow occurs, the IRQ thread calls amd_iommu_restart_log() which also invokes iommu_feature_disable().

Since this read-modify-write isn't synchronized, could concurrent modifications silently overwrite each other?

Quoted 3 lines
drivers/iommu/amd/init.c +1 -0
+
 		/*
 		 * Disable GALog if already running. It could have been enabled

[ ... ]

Quoted 11 lines
drivers/iommu/amd/init.c +7 -0
@@ -3038,10 +3048,19 @@ static void enable_iommus_vapic(void)
 		return;
 	}
 
+	if (amd_iommu_gappi &&
+	    !(check_feature(FEATURE_GAPPI) &&
+	      AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))) {
+		pr_warn("GAPPI is not supported.\n");
+		amd_iommu_gappi = false;
+	}
+

drivers/iommu/amd/init.c static void enable_iommus_vapic(void) · line 3065

Quoted 10 lines
drivers/iommu/amd/init.c +4 -2
 	/* Enabling GAM and SNPAVIC support */
 	for_each_iommu(iommu) {
-		if (iommu_init_ga_log(iommu) ||
-		    iommu_ga_log_enable(iommu))
+		if (amd_iommu_gappi)
+			iommu_feature_enable(iommu, CONTROL_GAPPI_EN);
+		else if (iommu_init_ga_log(iommu) ||
+			 iommu_ga_log_enable(iommu))
 			return;
 

[Severity: Critical]
If the system is booted into a kdump kernel with GAPPI enabled, could this lead to the IOMMU writing to stale physical memory from the previous kernel?

When GAPPI is used, iommu_init_ga_log() is skipped, meaning the ga_log buffer is not allocated. However, if the previous kernel crashed with a pending GA log overflow, the hardware's overflow status bit remains set.

Once shared IOMMU interrupts are enabled, the pending overflow status triggers the interrupt thread:

drivers/iommu/amd/iommu.c:amd_iommu_int_thread_galog() {
amd_iommu_handle_irq(data, "GA", MMIO_STATUS_GALOG_INT_MASK,
MMIO_STATUS_GALOG_OVERFLOW_MASK, iommu_poll_ga_log, amd_iommu_restart_ga_log);
}

This calls amd_iommu_restart_ga_log() which unconditionally enables CONTROL_GALOG_EN:

drivers/iommu/amd/init.c:amd_iommu_restart_ga_log() {
amd_iommu_restart_log(iommu, "GA", CONTROL_GAINT_EN,
CONTROL_GALOG_EN, MMIO_STATUS_GALOG_RUN_MASK, MMIO_STATUS_GALOG_OVERFLOW_MASK);
}

Quoted 4 lines

Since CONTROL_GALOG_EN is re-enabled without checking if GAPPI is active or if the log memory was allocated, will the IOMMU resume writing GA log entries to the uninitialized physical address left by the crashed kernel?

The IOMMU driver does not evaluate GALOG overflow status bit unless IOMMU delivers an GALOG interrupt, which is not possible when IOMMU switches GAPPI mode.

Thanks
Sairaj

Quoted 3 lines
Quoted 2 lines
drivers/iommu/amd/init.c +0 -0
 		iommu_feature_enable(iommu, CONTROL_GAM_EN);

if (amd_iommu_snp_en)

#25 lore raw
In reply to Sairaj Kodilkar

Hi everyone

Gentle ping...

Quoted 143 lines · Sairaj Kodilkar

Introduction
============
On newer generations of AMD processors, IOMMU AVIC/x2AVIC guest-mode interrupt remapping can use Guest APIC Physical Processor Interrupt (GAPPI) as an alternative host-notification path when device interrupts target a vCPU that is not running (IRTE[IsRun] = 0).

With AVIC enabled, the IOMMU posts device interrupts into the guest virtual APIC backing page. When the vCPU is not running, KVM must additionally be notified so it can schedule the vCPU.

The legacy notification path is the GA log (GALOG): the IOMMU appends vCPU tags to a shared GA log buffer and raises a single GA log interrupt. KVM registers a notifier and scans the buffer to decide which vCPUs to wake. Under heavy interrupt load this adds latency and can overflow the buffer because all wakeups funnel through one interrupt and one shared log.

Guest APIC Physical Processor Interrupt (GAPPI), defined in section 2.2.5.4 of the AMD I/O Virtualization Technology (IOMMU) Specification [1], is an alternative. With GAPPI enabled, the IOMMU still updates the guest vAPIC backing page IRR, but may deliver a physical APIC interrupt directly to IRTE[Destination], using IRTE[GATag][7:0] as the vector. This distributes host wakeup notifications across CPUs instead of centralizing them in a log buffer.

This series programs guest-mode IRTEs accordingly: IRTE[Destination] carries the target host physical APIC ID, IRTE[GATag] is set to POSTED_INTR_WAKEUP_VECTOR, and IRTE[GAPPIDis] / IRTE[GALogIntr] are set based on whether KVM requests host wakeup. GAPPI is selected at boot via the amd_iommu=gappi kernel parameter on capable hardware, otherwise the existing GA log path is unchanged.

SVM/AMD IOMMU interface changes
===============================
The first four patches refactor the SVM/AMD IOMMU interface ahead of GAPPI.

The cpu field is renamed to apicid because it carries the host physical APIC ID for IRTE[Destination], not a Linux CPU number.

The ga_log_intr boolean is renamed to wakeup_intr (and the synthetic AVIC_PHYSICAL_ID_ENTRY_GA_LOG_INTR shadow bit to AVIC_PHYSICAL_ID_ENTRY_WAKEUP_INTR). wakeup_intr describes KVM's intent (request host wakeup while the vCPU is not running), not a specific hardware mechanism.

A separate is_running boolean is added to IOMMU interface because GAPPI requires a valid apicid in IRTE[Destination] even when the vCPU is not running. The prior encoding (apicid >= 0 means running, apicid == -1 means not running) no longer works once apicid carries the GAPPI destination while IRTE[IsRun] is clear. The IOMMU driver keys IRTE[IsRun] and destination programming off this explicit boolean instead of inferring running state from apicid.

KVM GAPPI wakeup scheme
=======================
SVM follows the Intel posted-interrupt wakeup model already used by VMX. Each pCPU maintains a list of blocked vCPUs that may be woken by a GAPPI delivery to that CPU. When a vCPU blocks while waiting for a device interrupt, SVM enqueues it on the wakeup list of the pCPU on which it was previously running (gappi_cpu) and passes that pCPU's physical APIC ID to the IOMMU to program IRTE[Destination]. The rationale is that the vCPU is likely to run again on the same pCPU, which is common when vCPUs are pinned; targeting GAPPI notifications there reduces unnecessary VMEXITs from GAPPI deliveries on other CPUs. When the vCPU is scheduled in again, it is removed from the list and IRTE[Destination] is updated to the current pCPU.

SVM registers avic_gappi_wakeup_handler() via kvm_set_posted_intr_wakeup_handler(). On POSTED_INTR_WAKEUP_VECTOR delivery, the handler walks the local per-CPU list and wakes vCPUs with a pending LAPIC IRR. The IOMMU has already posted the interrupt into the guest vAPIC; waking the vCPU lets it observe the pending interrupt and run.

List maintenance is hooked into the existing AVIC vCPU and IRQ affinity
paths: vCPU load/put through avic_update_iommu_vcpu_affinity(), the first IRQ affined to a non-running vCPU through avic_pi_update_irte() when ir_list was empty at put time, removal when the last IRTE is detached, and cleanup on vCPU destroy. All GAPPI-specific logic is gated on amd_iommu_gappi.

Changes since v2
================
https://lore.kernel.org/linux-iommu/[email protected]/

Patch[1-6]
- Expand commit messages to explain GAPPI, the interface changes, and the per-CPU wakeup list scheme [Sean].

Patch[1-3]
- Split the monolithic SVM/IOMMU API refactor into four preparatory patches [Sean]
- Rename posted_intr to wakeup_intr to reflect host wakeup intent, not guest interrupt posting [Sean]
- Pass vCPU running status with a extra parameter (is_running) instead of flags.

Patch[4,5]
- Move ga_tag=POSTED_INTR_WAKEUP_VECTOR setting from IOMMU to SVM layer.

Changes since V1:
================
https://lore.kernel.org/all/[email protected]/

Patch4
- Disable interrupts while holding wakeup list lock inside [sashiko] avic_add_vcpu_to_gappi_wakeup_list and avic_remove_vcpu_from_gappi_wakeup_list
- Unregister posted_intr_wakeup_handler during module unload [sashiko]

Patch5
- Disable GAPPI feature during kexec and suspend path [sashiko]

------
[1] https://docs.amd.com/v/u/en-US/48882_3.11_IOMMU_PUB

Sairaj Kodilkar (6):
iommu/amd: KVM: SVM: Rename cpu to apicid in IOMMU interface iommu/amd: KVM: SVM: Rename ga_log_intr to wakeup_intr in IOMMU
interface
iommu/amd: KVM: SVM: Add explicit vCPU running state to IOMMU
interface
iommu/amd: Program guest-mode IRTEs for GAPPI wakeup when IRTE[IsRun]

    = 0

KVM: SVM: Add support for AMD IOMMU Guest APIC Physical Processor
Interrupt (GAPPI)
iommu/amd: Provide kernel command line option to enable GAPPI

 .../admin-guide/kernel-parameters.txt         |   3 +-
 arch/x86/include/asm/irq_remapping.h          |   5 +-
 arch/x86/include/asm/svm.h                    |   9 +-
 arch/x86/kvm/svm/avic.c                       | 173 ++++++++++++++----
 arch/x86/kvm/svm/svm.c                        |   2 +
 arch/x86/kvm/svm/svm.h                        |   5 +
 drivers/iommu/amd/amd_iommu.h                 |   1 +
 drivers/iommu/amd/amd_iommu_types.h           |   6 +-
 drivers/iommu/amd/init.c                      |  31 +++-
 drivers/iommu/amd/iommu.c                     |  59 +++---
 include/linux/amd-iommu.h                     |  13 +-
 11 files changed, 237 insertions(+), 70 deletions(-)

base-commit: 8cd9520d35a6c38db6567e97dd93b1f11f185dc6

#26 lore raw
In reply to Sairaj Kodilkar
Quoted 4 lines · Sairaj Kodilkar

On newer generations of AMD processors, IOMMU AVIC/x2AVIC guest-mode interrupt remapping can use Guest APIC Physical Processor Interrupt (GAPPI) as an alternative host-notification path when device interrupts target a vCPU that is not running (IRTE[IsRun] = 0).

Why is this still an RFC? I mean that in a good way. I haven't looked at this super duper closely, but the patches all look to be in generally good shape. I.e. this seems well beyond RFC, unless there's a hard blocker or something, e.g. known bugs?

Another bigger picture comment: I'm very tempted to say this has a hard dependency on KVM's irq_test[*] adding support for AMD. I know it's being worked on, but I can't remember who is doing the work, or when it's expected...

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

#27 lore raw
Quoted 10 lines · Sean Christopherson
Quoted 4 lines · Sairaj Kodilkar

On newer generations of AMD processors, IOMMU AVIC/x2AVIC guest-mode interrupt remapping can use Guest APIC Physical Processor Interrupt (GAPPI) as an alternative host-notification path when device interrupts target a vCPU that is not running (IRTE[IsRun] = 0).

Why is this still an RFC? I mean that in a good way. I haven't looked at this super duper closely, but the patches all look to be in generally good shape. I.e. this seems well beyond RFC, unless there's a hard blocker or something, e.g. known bugs?

Sorry I forgot to remove RFC while sending the v3, will remove it from next version onward.

Quoted 6 lines

Another bigger picture comment: I'm very tempted to say this has a hard dependency on KVM's irq_test[*] adding support for AMD. I know it's being worked on, but I can't remember who is doing the work, or when it's expected...

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

Sure. I'll look into it and will see if anyone from the AMD is working on it actively.

Thanks
Sairaj