Skip to contentSkip to conversation

iommu/amd: Default to passthrough mode for improved performance

PATCH In review lirongqing 2 participants 4 messages Mar 26 – Apr 1

Subsystems (2)
lore
Share & tools
lore

lirongqing

author
#0 lore raw

PATCH 1/1 iommu/amd: Default to passthrough mode for improved performance

From: Li RongQing <[email protected]>

On x86 platforms, AMD IOMMU is typically enabled by default. When the kernel is compiled with CONFIG_IOMMU_DEFAULT_DMA_LAZY, the IOMMU operates in translated mode with deferred TLB flushing. While this provides a security layer, it introduces measurable performance overhead compared to Intel systems where the IOMMU often defaults to a disabled state.

To optimize out-of-the-box performance for AMD users, shift the default to passthrough mode when the following conditions are met:
1. No explicit IOMMU mode was requested via the command line.
2. The kernel was configured to use 'lazy' DMA remapping by default.
3. Memory encryption (SME/SEV) is not active, as these features require translation for security.

This change allows standard DMA operations to bypass remapping overhead while maintaining the ability for users to explicitly enable translation if required.

To support this, export iommu_dma_is_user_configured() from the IOMMU core to allow vendor drivers to check if the DMA API configuration was overridden by the user.

---
 drivers/iommu/amd/init.c | 9 +++++++++
 drivers/iommu/iommu.c    | 6 ++++++
 include/linux/iommu.h    | 1 +
 3 files changed, 16 insertions(+)
drivers/iommu/amd/init.c +9 -0
index f3fd7f3..e89a5ce 100644
@@ -3619,6 +3619,15 @@ void __init amd_iommu_detect(void)
 	amd_iommu_detected = true;
 	iommu_detected = 1;
 	x86_init.iommu.iommu_init = amd_iommu_init;
+
+	if (!iommu_dma_is_user_configured()) {
+		if (!cc_platform_has(CC_ATTR_MEM_ENCRYPT) &&
+				IS_ENABLED(CONFIG_IOMMU_DEFAULT_DMA_LAZY)) {
+			pr_info("Defaulting to Passthrough mode for performance\n");
+			iommu_set_default_passthrough(false);
+		}
+	}
+
 	return;
 
 disable_snp:
drivers/iommu/iommu.c +6 -0
index 50718ab..a950dbb 100644
@@ -4091,3 +4091,9 @@ int iommu_dma_prepare_msi(struct msi_desc *desc, phys_addr_t msi_addr)
 	return ret;
 }
 #endif /* CONFIG_IRQ_MSI_IOMMU */
+
+bool iommu_dma_is_user_configured(void)
+{
+	return !!(iommu_cmd_line & IOMMU_CMD_LINE_DMA_API);
+}
+EXPORT_SYMBOL_GPL(iommu_dma_is_user_configured);
include/linux/iommu.h +1 -0
index 54b8b48..c3ff8a9 100644
@@ -967,6 +967,7 @@ int iommu_set_pgtable_quirks(struct iommu_domain *domain,
 		unsigned long quirks);
 
 void iommu_set_dma_strict(void);
+bool iommu_dma_is_user_configured(void);
 
 extern int report_iommu_fault(struct iommu_domain *domain, struct device *dev,
 			      unsigned long iova, int flags);
Signature · 1 line
2.9.4
reviewer
#1 lore raw
Quoted 16 lines · lirongqing

From: Li RongQing <[email protected]>

On x86 platforms, AMD IOMMU is typically enabled by default. When the kernel is compiled with CONFIG_IOMMU_DEFAULT_DMA_LAZY, the IOMMU operates in translated mode with deferred TLB flushing. While this provides a security layer, it introduces measurable performance overhead compared to Intel systems where the IOMMU often defaults to a disabled state.

To optimize out-of-the-box performance for AMD users, shift the default to passthrough mode when the following conditions are met:
1. No explicit IOMMU mode was requested via the command line.
2. The kernel was configured to use 'lazy' DMA remapping by default.
3. Memory encryption (SME/SEV) is not active, as these features require translation for security.

Quoted 7 lines

This change allows standard DMA operations to bypass remapping overhead while maintaining the ability for users to explicitly enable translation if required.

To support this, export iommu_dma_is_user_configured() from the IOMMU core to allow vendor drivers to check if the DMA API configuration was overridden by the user.

Frankly, no. CONFIG_IOMMU_DEFAULT_PASSTHROUGH already exists for users who want that behaviour. IF you want an equivalent of CONFIG_INTEL_IOMMU_DEFAULT_ON which prevents the IOMMU being used at all then implement that (however I imagine a lot of VFIO users would be unhappy about changing the default of that at this point). You can't just completely break CONFIG_IOMMU_DEFAULT_DMA_LAZY for all the users who do want its particular behaviour.

Note that "lazy" mode does still represent nearly all of the security/memory safety functionality offered by the IOMMU, so it does have significant value - strict mode only adds protection for use-after-free of memory which _was_ already a legitimate DMA buffer for the given device at one point. A better title for this patch would be "Silently make AMD systems less secure unless users go out of their way to add command-line arguments to work around this change"...

There may well also still be some performance difference between the IOMMU being enabled in passthrough, and being truly disabled - I seem to recall the Intel GPU folks saying that was significant enough to care about at least on some older Intel systems.

Thansk,
Robin.

Quoted 53 lines
---
  drivers/iommu/amd/init.c | 9 +++++++++
  drivers/iommu/iommu.c    | 6 ++++++
  include/linux/iommu.h    | 1 +
  3 files changed, 16 insertions(+)
drivers/iommu/amd/init.c +9 -0
index f3fd7f3..e89a5ce 100644
@@ -3619,6 +3619,15 @@ void __init amd_iommu_detect(void)
  	amd_iommu_detected = true;
  	iommu_detected = 1;
  	x86_init.iommu.iommu_init = amd_iommu_init;
+
+	if (!iommu_dma_is_user_configured()) {
+		if (!cc_platform_has(CC_ATTR_MEM_ENCRYPT) &&
+				IS_ENABLED(CONFIG_IOMMU_DEFAULT_DMA_LAZY)) {
+			pr_info("Defaulting to Passthrough mode for performance\n");
+			iommu_set_default_passthrough(false);
+		}
+	}
+
  	return;
 
  disable_snp:
drivers/iommu/iommu.c +6 -0
index 50718ab..a950dbb 100644
@@ -4091,3 +4091,9 @@ int iommu_dma_prepare_msi(struct msi_desc *desc, phys_addr_t msi_addr)
  	return ret;
  }
  #endif /* CONFIG_IRQ_MSI_IOMMU */
+
+bool iommu_dma_is_user_configured(void)
+{
+	return !!(iommu_cmd_line & IOMMU_CMD_LINE_DMA_API);
+}
+EXPORT_SYMBOL_GPL(iommu_dma_is_user_configured);
include/linux/iommu.h +1 -0
index 54b8b48..c3ff8a9 100644
@@ -967,6 +967,7 @@ int iommu_set_pgtable_quirks(struct iommu_domain *domain,
  		unsigned long quirks);
 
  void iommu_set_dma_strict(void);
+bool iommu_dma_is_user_configured(void);
 
  extern int report_iommu_fault(struct iommu_domain *domain, struct device *dev,
  			      unsigned long iova, int flags);
#2 lore raw
Quoted 24 lines
Quoted 16 lines

On x86 platforms, AMD IOMMU is typically enabled by default. When the kernel is compiled with CONFIG_IOMMU_DEFAULT_DMA_LAZY, the IOMMU operates in translated mode with deferred TLB flushing. While this provides a security layer, it introduces measurable performance overhead compared to Intel systems where the IOMMU often defaults to a disabled state.

To optimize out-of-the-box performance for AMD users, shift the default to passthrough mode when the following conditions are met:
1. No explicit IOMMU mode was requested via the command line.
2. The kernel was configured to use 'lazy' DMA remapping by default.
3. Memory encryption (SME/SEV) is not active, as these features require translation for security.

Quoted 7 lines

This change allows standard DMA operations to bypass remapping overhead while maintaining the ability for users to explicitly enable translation if required.

To support this, export iommu_dma_is_user_configured() from the IOMMU core to allow vendor drivers to check if the DMA API configuration was overridden by the user.

Quoted 8 lines

Frankly, no. CONFIG_IOMMU_DEFAULT_PASSTHROUGH already exists for users who want that behaviour. IF you want an equivalent of CONFIG_INTEL_IOMMU_DEFAULT_ON which prevents the IOMMU being used at all then implement that (however I imagine a lot of VFIO users would be unhappy about changing the default of that at this point). You can't just completely break CONFIG_IOMMU_DEFAULT_DMA_LAZY for all the users who do want its particular behaviour.

However, CONFIG_IOMMU_DEFAULT_DMA_LAZY is a kernel default. On AMD CPUs, this causes a drop in networking and I/O performance, and many manuals to recommend setting iommu=pt. Why can't passthrough be the default? and IOMMU is disabled by default on Intel CPUs, why is security considered a non-issue for Intel but a concern for AMD?"

And I understand that setting iommu to PT should not affect the usage of VFIO.

[Li,Rongqing]

Quoted 71 lines

Note that "lazy" mode does still represent nearly all of the security/memory safety functionality offered by the IOMMU, so it does have significant value - strict mode only adds protection for use-after-free of memory which _was_ already a legitimate DMA buffer for the given device at one point. A better title for this patch would be "Silently make AMD systems less secure unless users go out of their way to add command-line arguments to work around this change"...

There may well also still be some performance difference between the IOMMU being enabled in passthrough, and being truly disabled - I seem to recall the Intel GPU folks saying that was significant enough to care about at least on some older Intel systems.

Thansk,
Robin.

drivers/iommu/iommu.c int iommu_dma_prepare_msi(struct msi_desc

Quoted 32 lines
---
  drivers/iommu/amd/init.c | 9 +++++++++
  drivers/iommu/iommu.c    | 6 ++++++
  include/linux/iommu.h    | 1 +
  3 files changed, 16 insertions(+)
drivers/iommu/amd/init.c index renamed from drivers/iommu/amd/init.c +0 -0

f3fd7f3..e89a5ce 100644

drivers/iommu/amd/init.c +9 -0
@@ -3619,6 +3619,15 @@ void __init amd_iommu_detect(void)
  	amd_iommu_detected = true;
  	iommu_detected = 1;
  	x86_init.iommu.iommu_init = amd_iommu_init;
+
+	if (!iommu_dma_is_user_configured()) {
+		if (!cc_platform_has(CC_ATTR_MEM_ENCRYPT) &&
+				IS_ENABLED(CONFIG_IOMMU_DEFAULT_DMA_LAZY)) {
+			pr_info("Defaulting to Passthrough mode for performance\n");
+			iommu_set_default_passthrough(false);
+		}
+	}
+
  	return;
 
  disable_snp:
drivers/iommu/iommu.c index renamed from drivers/iommu/iommu.c +0 -0

50718ab..a950dbb 100644

drivers/iommu/iommu.c +0 -0
@@ -4091,3 +4091,9 @@ int iommu_dma_prepare_msi(struct msi_desc

*desc, phys_addr_t msi_addr)

include/linux/iommu.h int iommu_set_pgtable_quirks(struct

Quoted 13 lines
drivers/iommu/iommu.c +5 -0
  	return ret;
  }
  #endif /* CONFIG_IRQ_MSI_IOMMU */
+
+bool iommu_dma_is_user_configured(void)
+{
+	return !!(iommu_cmd_line & IOMMU_CMD_LINE_DMA_API); }
+EXPORT_SYMBOL_GPL(iommu_dma_is_user_configured);
include/linux/iommu.h index renamed from include/linux/iommu.h +0 -0

54b8b48..c3ff8a9 100644

include/linux/iommu.h +0 -0
@@ -967,6 +967,7 @@ int iommu_set_pgtable_quirks(struct

iommu_domain *domain,

include/linux/iommu.h int iommu_set_pgtable_quirks(struct iommu_domain *domain,

Quoted 6 lines
include/linux/iommu.h +1 -0
  		unsigned long quirks);
 
  void iommu_set_dma_strict(void);
+bool iommu_dma_is_user_configured(void);
 
  extern int report_iommu_fault(struct iommu_domain *domain, struct

device *dev,

Quoted 1 line
include/linux/iommu.h +0 -0
  			      unsigned long iova, int flags);
reviewer
#3 lore raw
Quoted 33 lines · Li,Rongqing(ACG CCN)
Quoted 24 lines
Quoted 16 lines

On x86 platforms, AMD IOMMU is typically enabled by default. When the kernel is compiled with CONFIG_IOMMU_DEFAULT_DMA_LAZY, the IOMMU operates in translated mode with deferred TLB flushing. While this provides a security layer, it introduces measurable performance overhead compared to Intel systems where the IOMMU often defaults to a disabled state.

To optimize out-of-the-box performance for AMD users, shift the default to passthrough mode when the following conditions are met:
1. No explicit IOMMU mode was requested via the command line.
2. The kernel was configured to use 'lazy' DMA remapping by default.
3. Memory encryption (SME/SEV) is not active, as these features require translation for security.

Quoted 7 lines

This change allows standard DMA operations to bypass remapping overhead while maintaining the ability for users to explicitly enable translation if required.

To support this, export iommu_dma_is_user_configured() from the IOMMU core to allow vendor drivers to check if the DMA API configuration was overridden by the user.

Quoted 8 lines

Frankly, no. CONFIG_IOMMU_DEFAULT_PASSTHROUGH already exists for users who want that behaviour. IF you want an equivalent of CONFIG_INTEL_IOMMU_DEFAULT_ON which prevents the IOMMU being used at all then implement that (however I imagine a lot of VFIO users would be unhappy about changing the default of that at this point). You can't just completely break CONFIG_IOMMU_DEFAULT_DMA_LAZY for all the users who do want its particular behaviour.

Quoted 1 line

However, CONFIG_IOMMU_DEFAULT_DMA_LAZY is a kernel default.

Sure, it's what x86 defconfig sets, so if you want to change the default x86 behaviour to passthrough, send a patch to x86 defconfig to make it select IOMMU_DEFAULT_DMA_PASSTHROUGH instead (and I guess also move IOMMU_DEFAULT_DMA_LAZY to hardening.config to preserve expectations there). Don't just vandalise one driver such that IOMMU_DEFAULT_DMA_LAZY cannot even work properly at all.

Quoted 3 lines

On AMD CPUs, this causes a drop in networking and I/O performance, and many manuals to recommend setting iommu=pt. Why can't passthrough be the default? and IOMMU is disabled by default on Intel CPUs, why is security considered a non-issue for Intel but a concern for AMD?"

And I understand that setting iommu to PT should not affect the usage of VFIO.

Indeed it wouldn't, but if you're trying to argue for parity with INTEL_IOMMU_DEFAULT_ON=n, then what that would actually be is applying the "amd_iommu=off" behaviour by default, not passthrough.

Thanks,
Robin.

Quoted 77 lines

[Li,Rongqing]

Quoted 71 lines

Note that "lazy" mode does still represent nearly all of the security/memory safety functionality offered by the IOMMU, so it does have significant value - strict mode only adds protection for use-after-free of memory which _was_ already a legitimate DMA buffer for the given device at one point. A better title for this patch would be "Silently make AMD systems less secure unless users go out of their way to add command-line arguments to work around this change"...

There may well also still be some performance difference between the IOMMU being enabled in passthrough, and being truly disabled - I seem to recall the Intel GPU folks saying that was significant enough to care about at least on some older Intel systems.

Thansk,
Robin.

drivers/iommu/iommu.c int iommu_dma_prepare_msi(struct msi_desc

Quoted 32 lines
---
   drivers/iommu/amd/init.c | 9 +++++++++
   drivers/iommu/iommu.c    | 6 ++++++
   include/linux/iommu.h    | 1 +
   3 files changed, 16 insertions(+)
drivers/iommu/amd/init.c index renamed from drivers/iommu/amd/init.c +0 -0

f3fd7f3..e89a5ce 100644

drivers/iommu/amd/init.c +9 -0
@@ -3619,6 +3619,15 @@ void __init amd_iommu_detect(void)
   	amd_iommu_detected = true;
   	iommu_detected = 1;
   	x86_init.iommu.iommu_init = amd_iommu_init;
+
+	if (!iommu_dma_is_user_configured()) {
+		if (!cc_platform_has(CC_ATTR_MEM_ENCRYPT) &&
+				IS_ENABLED(CONFIG_IOMMU_DEFAULT_DMA_LAZY)) {
+			pr_info("Defaulting to Passthrough mode for performance\n");
+			iommu_set_default_passthrough(false);
+		}
+	}
+
   	return;
 
   disable_snp:
drivers/iommu/iommu.c index renamed from drivers/iommu/iommu.c +0 -0

50718ab..a950dbb 100644

drivers/iommu/iommu.c +0 -0
@@ -4091,3 +4091,9 @@ int iommu_dma_prepare_msi(struct msi_desc

*desc, phys_addr_t msi_addr)

include/linux/iommu.h int iommu_set_pgtable_quirks(struct

Quoted 13 lines
drivers/iommu/iommu.c +5 -0
   	return ret;
   }
   #endif /* CONFIG_IRQ_MSI_IOMMU */
+
+bool iommu_dma_is_user_configured(void)
+{
+	return !!(iommu_cmd_line & IOMMU_CMD_LINE_DMA_API); }
+EXPORT_SYMBOL_GPL(iommu_dma_is_user_configured);
include/linux/iommu.h index renamed from include/linux/iommu.h +0 -0

54b8b48..c3ff8a9 100644

include/linux/iommu.h +0 -0
@@ -967,6 +967,7 @@ int iommu_set_pgtable_quirks(struct

iommu_domain *domain,

include/linux/iommu.h int iommu_set_pgtable_quirks(struct iommu_domain *domain,

Quoted 6 lines
include/linux/iommu.h +1 -0
   		unsigned long quirks);
 
   void iommu_set_dma_strict(void);
+bool iommu_dma_is_user_configured(void);
 
   extern int report_iommu_fault(struct iommu_domain *domain, struct

device *dev,

Quoted 1 line
include/linux/iommu.h +0 -0
   			      unsigned long iova, int flags);