Skip to contentSkip to conversation

iommu/amd: Explicitly bail from enable_iommus_vapic() when in legacy mode

PATCH Applied Sean Christopherson 3 participants 7 messages Mar 15, 2025 – May 11

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

PATCH 1/1 iommu/amd: Explicitly bail from enable_iommus_vapic() when in legacy mode

Bail early from enable_iommus_vapic() if IOMMUs are configured for either of the legacy modes, as it's absurdly difficult to see that iommu_ga_log_enable() is guaranteed to fail because iommu_init_ga_log() skips allocating the ga_log.

Opportunistically have iommu_ga_log_enable() WARN if it's called without IOMMUs being configured to support AVIC/vAPIC.

Cc Suravee Suthikulpanit <[email protected]>
---
 drivers/iommu/amd/init.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
drivers/iommu/amd/init.c +6 -4
index cb536d372b12..05c568da589a 100644
@@ -931,8 +931,8 @@ static int iommu_ga_log_enable(struct amd_iommu *iommu)
 
 static int iommu_init_ga_log(struct amd_iommu *iommu)
 {
-	if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
-		return 0;
+	if (WARN_ON_ONCE(!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir)))
+		return -EINVAL;
 
 	iommu->ga_log = iommu_alloc_pages(GFP_KERNEL, get_order(GA_LOG_SIZE));
 	if (!iommu->ga_log)
@@ -2863,8 +2863,10 @@ static void enable_iommus_vapic(void)
 			return;
 	}
 
-	if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) &&
-	    !check_feature(FEATURE_GAM_VAPIC)) {
+	if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
+		return;
+
+	if (!check_feature(FEATURE_GAM_VAPIC)) {
 		amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY_GA;
 		return;
 	}

base-commit: ea9bd29a9c0d757b3384ae3e633e6bbaddf00725

Signature · 1 line
2.49.0.rc1.451.g8f38331e32-goog
#1 lore raw
Quoted 43 lines · Sean Christopherson

Bail early from enable_iommus_vapic() if IOMMUs are configured for either of the legacy modes, as it's absurdly difficult to see that iommu_ga_log_enable() is guaranteed to fail because iommu_init_ga_log() skips allocating the ga_log.

Opportunistically have iommu_ga_log_enable() WARN if it's called without IOMMUs being configured to support AVIC/vAPIC.

Cc Suravee Suthikulpanit <[email protected]>
---
 drivers/iommu/amd/init.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
drivers/iommu/amd/init.c +6 -4
index cb536d372b12..05c568da589a 100644
@@ -931,8 +931,8 @@ static int iommu_ga_log_enable(struct amd_iommu *iommu)
 
 static int iommu_init_ga_log(struct amd_iommu *iommu)
 {
-	if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
-		return 0;
+	if (WARN_ON_ONCE(!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir)))
+		return -EINVAL;
 
 	iommu->ga_log = iommu_alloc_pages(GFP_KERNEL, get_order(GA_LOG_SIZE));
 	if (!iommu->ga_log)
@@ -2863,8 +2863,10 @@ static void enable_iommus_vapic(void)
 			return;
 	}
 
-	if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) &&
-	    !check_feature(FEATURE_GAM_VAPIC)) {
+	if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
+		return;
+
+	if (!check_feature(FEATURE_GAM_VAPIC)) {
 		amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY_GA;
 		return;
 	}
Quoted 2 lines

base-commit: ea9bd29a9c0d757b3384ae3e633e6bbaddf00725

Another ping. This too still applies.

maintainer
#2 lore raw

Vasant, could you please have a look? I'd prefer a review from you or Suravee before applying.

Thanks,

Joerg

Quoted 48 lines · Sean Christopherson
Quoted 43 lines · Sean Christopherson

Bail early from enable_iommus_vapic() if IOMMUs are configured for either of the legacy modes, as it's absurdly difficult to see that iommu_ga_log_enable() is guaranteed to fail because iommu_init_ga_log() skips allocating the ga_log.

Opportunistically have iommu_ga_log_enable() WARN if it's called without IOMMUs being configured to support AVIC/vAPIC.

Cc Suravee Suthikulpanit <[email protected]>
---
 drivers/iommu/amd/init.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
drivers/iommu/amd/init.c +6 -4
index cb536d372b12..05c568da589a 100644
@@ -931,8 +931,8 @@ static int iommu_ga_log_enable(struct amd_iommu *iommu)
 
 static int iommu_init_ga_log(struct amd_iommu *iommu)
 {
-	if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
-		return 0;
+	if (WARN_ON_ONCE(!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir)))
+		return -EINVAL;
 
 	iommu->ga_log = iommu_alloc_pages(GFP_KERNEL, get_order(GA_LOG_SIZE));
 	if (!iommu->ga_log)
@@ -2863,8 +2863,10 @@ static void enable_iommus_vapic(void)
 			return;
 	}
 
-	if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) &&
-	    !check_feature(FEATURE_GAM_VAPIC)) {
+	if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
+		return;
+
+	if (!check_feature(FEATURE_GAM_VAPIC)) {
 		amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY_GA;
 		return;
 	}
Quoted 2 lines

base-commit: ea9bd29a9c0d757b3384ae3e633e6bbaddf00725

Another ping. This too still applies.

maintainer
#3 lore raw
In reply to Sean Christopherson

Sean,

Quoted 21 lines · Sean Christopherson

Bail early from enable_iommus_vapic() if IOMMUs are configured for either of the legacy modes, as it's absurdly difficult to see that iommu_ga_log_enable() is guaranteed to fail because iommu_init_ga_log() skips allocating the ga_log.

Opportunistically have iommu_ga_log_enable() WARN if it's called without IOMMUs being configured to support AVIC/vAPIC.

Cc Suravee Suthikulpanit <[email protected]>
---
 drivers/iommu/amd/init.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
drivers/iommu/amd/init.c +0 -0
index cb536d372b12..05c568da589a 100644

drivers/iommu/amd/init.c static int iommu_ga_log_enable(struct amd_iommu *iommu) · line 934

Quoted 6 lines
drivers/iommu/amd/init.c +1 -2
@@ -931,8 +931,8 @@ static int iommu_ga_log_enable(struct amd_iommu *iommu)
 
 static int iommu_init_ga_log(struct amd_iommu *iommu)
 {
-	if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
-		return 0;
+	if (WARN_ON_ONCE(!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir)))

+ return -EINVAL;

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

Quoted 12 lines
drivers/iommu/amd/init.c +3 -2
 	iommu->ga_log = iommu_alloc_pages(GFP_KERNEL, get_order(GA_LOG_SIZE));
 	if (!iommu->ga_log)
@@ -2863,8 +2863,10 @@ static void enable_iommus_vapic(void)
 			return;
 	}
 
-	if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) &&
-	    !check_feature(FEATURE_GAM_VAPIC)) {
+	if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
+		return;
+

We should move this down after SNP check. Otherwise we may hit WARN_ON_ONCE in iommu_init_ga_log().

-Vasant

Quoted 8 lines
drivers/iommu/amd/init.c +1 -0
+	if (!check_feature(FEATURE_GAM_VAPIC)) {
 		amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY_GA;
 		return;
 	}

base-commit: ea9bd29a9c0d757b3384ae3e633e6bbaddf00725

#4 lore raw
Quoted 38 lines · Vasant Hegde

Sean,

Quoted 21 lines · Sean Christopherson

Bail early from enable_iommus_vapic() if IOMMUs are configured for either of the legacy modes, as it's absurdly difficult to see that iommu_ga_log_enable() is guaranteed to fail because iommu_init_ga_log() skips allocating the ga_log.

Opportunistically have iommu_ga_log_enable() WARN if it's called without IOMMUs being configured to support AVIC/vAPIC.

Cc Suravee Suthikulpanit <[email protected]>
---
 drivers/iommu/amd/init.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
drivers/iommu/amd/init.c +0 -0
index cb536d372b12..05c568da589a 100644

drivers/iommu/amd/init.c static int iommu_ga_log_enable(struct amd_iommu *iommu) · line 934

Quoted 6 lines
drivers/iommu/amd/init.c +1 -2
@@ -931,8 +931,8 @@ static int iommu_ga_log_enable(struct amd_iommu *iommu)
 
 static int iommu_init_ga_log(struct amd_iommu *iommu)
 {
-	if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
-		return 0;
+	if (WARN_ON_ONCE(!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir)))

+ return -EINVAL;

Quoted 7 lines
drivers/iommu/amd/init.c +0 -0
 	iommu->ga_log = iommu_alloc_pages(GFP_KERNEL, get_order(GA_LOG_SIZE));
 	if (!iommu->ga_log)
@@ -2863,8 +2863,10 @@ static void enable_iommus_vapic(void)
 			return;
 	}
 
Quoted 8 lines

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

Quoted 5 lines
drivers/iommu/amd/init.c +3 -2
-	if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) &&
-	    !check_feature(FEATURE_GAM_VAPIC)) {
+	if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
+		return;
+

We should move this down after SNP check. Otherwise we may hit WARN_ON_ONCE in iommu_init_ga_log().

No? enable_iommus_vapic() ends up with:

1. if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))

		return;
2.	if (!check_feature(FEATURE_GAM_VAPIC)) {
		amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY_GA;
		return;
	}

3. if (amd_iommu_snp_en &&

	    !FEATURE_SNPAVICSUP_GAM(amd_iommu_efr2)) {
		pr_warn("Force to disable Virtual APIC due to SNP\n");
		amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY_GA;
		return;
	}

	/* Enabling GAM and SNPAVIC support */
	for_each_iommu(iommu) {
		if (iommu_init_ga_log(iommu) ||
		    iommu_ga_log_enable(iommu))
			return;

		iommu_feature_enable(iommu, CONTROL_GAM_EN);
		if (amd_iommu_snp_en)
			iommu_feature_enable(iommu, CONTROL_SNPAVIC_EN);
	}

And that's the only call site for iommu_init_ga_log(). Getting past #1 guarantees "amd_iommu_guest_ir == AMD_IOMMU_GUEST_IR_VAPIC", and if #2 and #3 return early if they modify amd_iommu_guest_ir, so it should be impossible for iommu_init_ga_log() to be called with amd_iommu_guest_ir anything other than AMD_IOMMU_GUEST_IR_VAPIC.

Shifting the initial check down isn't functionally correct, as it wouldn't do the right thing if amd_iommu_guest_ir==AMD_IOMMU_GUEST_IR_LEGACY. AFAICT, nothing prevents calling into enable_iommus_vapic() in that case.

maintainer Reviewed-by
#5 lore raw
Quoted 58 lines · Sean Christopherson
Quoted 38 lines · Vasant Hegde

Sean,

Quoted 21 lines · Sean Christopherson

Bail early from enable_iommus_vapic() if IOMMUs are configured for either of the legacy modes, as it's absurdly difficult to see that iommu_ga_log_enable() is guaranteed to fail because iommu_init_ga_log() skips allocating the ga_log.

Opportunistically have iommu_ga_log_enable() WARN if it's called without IOMMUs being configured to support AVIC/vAPIC.

Cc Suravee Suthikulpanit <[email protected]>
---
 drivers/iommu/amd/init.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
drivers/iommu/amd/init.c +0 -0
index cb536d372b12..05c568da589a 100644

drivers/iommu/amd/init.c static int iommu_ga_log_enable(struct amd_iommu *iommu) · line 934

Quoted 6 lines
drivers/iommu/amd/init.c +1 -2
@@ -931,8 +931,8 @@ static int iommu_ga_log_enable(struct amd_iommu *iommu)
 
 static int iommu_init_ga_log(struct amd_iommu *iommu)
 {
-	if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
-		return 0;
+	if (WARN_ON_ONCE(!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir)))

+ return -EINVAL;

Quoted 7 lines
drivers/iommu/amd/init.c +0 -0
 	iommu->ga_log = iommu_alloc_pages(GFP_KERNEL, get_order(GA_LOG_SIZE));
 	if (!iommu->ga_log)
@@ -2863,8 +2863,10 @@ static void enable_iommus_vapic(void)
 			return;
 	}
 
Quoted 8 lines

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

Quoted 5 lines
drivers/iommu/amd/init.c +3 -2
-	if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) &&
-	    !check_feature(FEATURE_GAM_VAPIC)) {
+	if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
+		return;
+

We should move this down after SNP check. Otherwise we may hit WARN_ON_ONCE in iommu_init_ga_log().

No? enable_iommus_vapic() ends up with:

1. if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))

		return;
2.	if (!check_feature(FEATURE_GAM_VAPIC)) {
		amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY_GA;
		return;
	}
Quoted 6 lines

3. if (amd_iommu_snp_en &&

	    !FEATURE_SNPAVICSUP_GAM(amd_iommu_efr2)) {
		pr_warn("Force to disable Virtual APIC due to SNP\n");
		amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY_GA;
		return;
	}

Right. I missed the return part. Thanks!

Quoted 12 lines
	/* Enabling GAM and SNPAVIC support */
	for_each_iommu(iommu) {
		if (iommu_init_ga_log(iommu) ||
		    iommu_ga_log_enable(iommu))
			return;

		iommu_feature_enable(iommu, CONTROL_GAM_EN);
		if (amd_iommu_snp_en)
			iommu_feature_enable(iommu, CONTROL_SNPAVIC_EN);
	}
Quoted 5 lines

And that's the only call site for iommu_init_ga_log(). Getting past #1 guarantees "amd_iommu_guest_ir == AMD_IOMMU_GUEST_IR_VAPIC", and if #2 and #3 return early if they modify amd_iommu_guest_ir, so it should be impossible for iommu_init_ga_log() to be called with amd_iommu_guest_ir anything other than AMD_IOMMU_GUEST_IR_VAPIC.

Quoted 1 line

Shifting the initial check down isn't functionally correct, as it wouldn't

do the

Quoted 2 lines

right thing if amd_iommu_guest_ir==AMD_IOMMU_GUEST_IR_LEGACY. AFAICT, nothing prevents calling into enable_iommus_vapic() in that case.

Right. I will add fix to move #2 to iommu_init_pci() and #3 to iommu_snp_enable() .

-Vasant

maintainer Applied
#6 lore raw
In reply to Sean Christopherson
Quoted 5 lines · Sean Christopherson

Bail early from enable_iommus_vapic() if IOMMUs are configured for either of the legacy modes, as it's absurdly difficult to see that iommu_ga_log_enable() is guaranteed to fail because iommu_init_ga_log() skips allocating the ga_log.

Quoted 8 lines

Opportunistically have iommu_ga_log_enable() WARN if it's called without IOMMUs being configured to support AVIC/vAPIC.

Cc Suravee Suthikulpanit <[email protected]>
---
 drivers/iommu/amd/init.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

Applied, thanks.