Skip to contentSkip to conversation

swiotlb: Adjust SWIOTBL bounce buffer size for SEV guests.

PATCH Superseded Ashish Kalra 2 participants 6 messages Dec 3, 2020 – Dec 7, 2020

Subsystems (3)

v7 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

PATCH v7 1/1 swiotlb: Adjust SWIOTBL bounce buffer size for SEV guests.

From: Ashish Kalra <[email protected]>

For SEV, all DMA to and from guest has to use shared (un-encrypted) pages. SEV uses SWIOTLB to make this happen without requiring changes to device drivers. However, depending on workload being run, the default 64MB of SWIOTLB might not be enough and SWIOTLB may run out of buffers to use for DMA, resulting in I/O errors and/or performance degradation for high I/O workloads.

Adjust the default size of SWIOTLB for SEV guests using a percentage of the total memory available to guest for SWIOTLB buffers.

Using late_initcall() interface to invoke swiotlb_adjust() does not work as the size adjustment needs to be done before mem_encrypt_init() and reserve_crashkernel() which use the allocated SWIOTLB buffer size, hence call it explicitly from setup_arch().

The SWIOTLB default size adjustment needs to be added as an architecture specific interface/callback to allow architectures such as those supporting memory encryption to adjust/expand SWIOTLB size for their use.

v5 fixed build errors and warnings as

---
 arch/x86/kernel/setup.c   |  2 ++
 arch/x86/mm/mem_encrypt.c | 31 +++++++++++++++++++++++++++++++
 include/linux/swiotlb.h   |  6 ++++++
 kernel/dma/swiotlb.c      | 22 ++++++++++++++++++++++
 4 files changed, 61 insertions(+)
arch/x86/kernel/setup.c +2 -0
index 84f581c91db4..31e24e198061 100644
@@ -1149,6 +1149,8 @@ void __init setup_arch(char **cmdline_p)
 	if (boot_cpu_has(X86_FEATURE_GBPAGES))
 		hugetlb_cma_reserve(PUD_SHIFT - PAGE_SHIFT);
 
+	swiotlb_adjust();
+
 	/*
 	 * Reserve memory for crash kernel after SRAT is parsed so that it
 	 * won't consume hotpluggable memory.
arch/x86/mm/mem_encrypt.c +31 -0
index 1bcfbcd2bfd7..46549bd3d840 100644
@@ -485,7 +485,38 @@ static void print_mem_encrypt_feature_info(void)
 	pr_cont("\n");
 }
 
+#define SEV_ADJUST_SWIOTLB_SIZE_PERCENT	6
+
 /* Architecture __weak replacement functions */
+unsigned long __init arch_swiotlb_adjust(unsigned long iotlb_default_size)
+{
+	unsigned long size = iotlb_default_size;
+
+	/*
+	 * For SEV, all DMA has to occur via shared/unencrypted pages.
+	 * SEV uses SWOTLB to make this happen without changing device
+	 * drivers. However, depending on the workload being run, the
+	 * default 64MB of SWIOTLB may not be enough and`SWIOTLB may
+	 * run out of buffers for DMA, resulting in I/O errors and/or
+	 * performance degradation especially with high I/O workloads.
+	 * Adjust the default size of SWIOTLB for SEV guests using
+	 * a percentage of guest memory for SWIOTLB buffers.
+	 * Also as the SWIOTLB bounce buffer memory is allocated
+	 * from low memory, ensure that the adjusted size is within
+	 * the limits of low available memory.
+	 *
+	 */
+	if (sev_active()) {
+		phys_addr_t total_mem = memblock_phys_mem_size();
+		size = total_mem * SEV_ADJUST_SWIOTLB_SIZE_PERCENT / 100;
+		size = clamp_val(size, iotlb_default_size, SZ_1G);
+		pr_info("SWIOTLB bounce buffer size adjusted to %luMB for SEV",
+			size >> 20);
+	}
+
+	return size;
+}
+
 void __init mem_encrypt_init(void)
 {
 	if (!sme_me_mask)
include/linux/swiotlb.h +6 -0
index 3bb72266a75a..b5904fa4b67c 100644
@@ -33,6 +33,7 @@ extern void swiotlb_init(int verbose);
 int swiotlb_init_with_tbl(char *tlb, unsigned long nslabs, int verbose);
 extern unsigned long swiotlb_nr_tbl(void);
 unsigned long swiotlb_size_or_default(void);
+unsigned long __init arch_swiotlb_adjust(unsigned long size);
 extern int swiotlb_late_init_with_tbl(char *tlb, unsigned long nslabs);
 extern int swiotlb_late_init_with_default_size(size_t default_size);
 extern void __init swiotlb_update_mem_attributes(void);
@@ -77,6 +78,7 @@ void __init swiotlb_exit(void);
 unsigned int swiotlb_max_segment(void);
 size_t swiotlb_max_mapping_size(struct device *dev);
 bool is_swiotlb_active(void);
+void __init swiotlb_adjust(void);
 #else
 #define swiotlb_force SWIOTLB_NO_FORCE
 static inline bool is_swiotlb_buffer(phys_addr_t paddr)
@@ -99,6 +101,10 @@ static inline bool is_swiotlb_active(void)
 {
 	return false;
 }
+
+static inline void swiotlb_adjust(void)
+{
+}
 #endif /* CONFIG_SWIOTLB */
 
 extern void swiotlb_print_info(void);
kernel/dma/swiotlb.c +22 -0
index 781b9dca197c..0150ca2336bc 100644
@@ -163,6 +163,28 @@ unsigned long swiotlb_size_or_default(void)
 	return size ? size : (IO_TLB_DEFAULT_SIZE);
 }
 
+unsigned long __init __weak arch_swiotlb_adjust(unsigned long size)
+{
+	return size;
+}
+
+void __init swiotlb_adjust(void)
+{
+	unsigned long size;
+
+	/*
+	 * If swiotlb parameter has not been specified, give a chance to
+	 * architectures such as those supporting memory encryption to
+	 * adjust/expand SWIOTLB size for their use.
+	 */
+	if (!io_tlb_nslabs) {
+		size = arch_swiotlb_adjust(IO_TLB_DEFAULT_SIZE);
+		size = ALIGN(size, 1 << IO_TLB_SHIFT);
+		io_tlb_nslabs = size >> IO_TLB_SHIFT;
+		io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE);
+	}
+}
+
 void swiotlb_print_info(void)
 {
 	unsigned long bytes = io_tlb_nslabs << IO_TLB_SHIFT;
Signature · 6 lines
2.17.1

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
maintainer
#1 lore raw

arch/x86/mm/mem_encrypt.c static void print_mem_encrypt_feature_info(void) · line 486

Quoted 7 lines · Ashish Kalra
arch/x86/mm/mem_encrypt.c +0 -0
index 1bcfbcd2bfd7..46549bd3d840 100644
@@ -485,7 +485,38 @@ static void print_mem_encrypt_feature_info(void)
 	pr_cont("\n");
 }

Any text about why 6% was chosen? A rule of thumb or so? Measurements?

Quoted 13 lines
arch/x86/mm/mem_encrypt.c +12 -0
+#define SEV_ADJUST_SWIOTLB_SIZE_PERCENT	6
+
 /* Architecture __weak replacement functions */
+unsigned long __init arch_swiotlb_adjust(unsigned long iotlb_default_size)
+{
+	unsigned long size = iotlb_default_size;
+
+	/*
+	 * For SEV, all DMA has to occur via shared/unencrypted pages.
+	 * SEV uses SWOTLB to make this happen without changing device
+	 * drivers. However, depending on the workload being run, the
+	 * default 64MB of SWIOTLB may not be enough and`SWIOTLB may
+	 * run out of buffers for DMA, resulting in I/O errors and/or

arch/x86/mm/mem_encrypt.c static void print_mem_encrypt_feature_info(void) · line 510

Quoted 10 lines
arch/x86/mm/mem_encrypt.c +10 -0
+	 * performance degradation especially with high I/O workloads.
+	 * Adjust the default size of SWIOTLB for SEV guests using
+	 * a percentage of guest memory for SWIOTLB buffers.
+	 * Also as the SWIOTLB bounce buffer memory is allocated
+	 * from low memory, ensure that the adjusted size is within
+	 * the limits of low available memory.
+	 *
+	 */
+	if (sev_active()) {
+		phys_addr_t total_mem = memblock_phys_mem_size();

Please integrate scripts/checkpatch.pl into your patch creation workflow. Some of the warnings/errors *actually* make sense:

WARNING: Missing a blank line after declarations
#95: FILE: arch/x86/mm/mem_encrypt.c:511:
+ phys_addr_t total_mem = memblock_phys_mem_size();
+ size = total_mem * SEV_ADJUST_SWIOTLB_SIZE_PERCENT / 100;

But no need to resend now - just a hint for the future.

Konrad, ack?

On a 2G guest here, it says:

[    0.018373] SWIOTLB bounce buffer size adjusted to 122MB for SEV

so it makes sense to me.

Thx.

Signature · 8 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
#2 lore raw

Hello Boris,

Quoted 11 lines · Borislav Petkov

arch/x86/mm/mem_encrypt.c static void print_mem_encrypt_feature_info(void) · line 486

Quoted 7 lines · Ashish Kalra
arch/x86/mm/mem_encrypt.c +0 -0
index 1bcfbcd2bfd7..46549bd3d840 100644
@@ -485,7 +485,38 @@ static void print_mem_encrypt_feature_info(void)
 	pr_cont("\n");
 }

Any text about why 6% was chosen? A rule of thumb or so? Measurements?

This is related to the earlier static adjustment of the SWIOTLB buffers as per guest memory size and Konrad's feedback on the same, as copied below :

Quoted 7 lines
Quoted 7 lines

That is eating 128MB for 1GB, aka 12% of the guest memory allocated statically for this.

And for guests that are 2GB, that is 12% until it gets to 3GB when it is 8% and then 6% at 4GB.

I would prefer this to be based on your memory count, that is 6% of total memory.

Thanks,
Ashish

Quoted 49 lines
Quoted 13 lines
arch/x86/mm/mem_encrypt.c +12 -0
+#define SEV_ADJUST_SWIOTLB_SIZE_PERCENT	6
+
 /* Architecture __weak replacement functions */
+unsigned long __init arch_swiotlb_adjust(unsigned long iotlb_default_size)
+{
+	unsigned long size = iotlb_default_size;
+
+	/*
+	 * For SEV, all DMA has to occur via shared/unencrypted pages.
+	 * SEV uses SWOTLB to make this happen without changing device
+	 * drivers. However, depending on the workload being run, the
+	 * default 64MB of SWIOTLB may not be enough and`SWIOTLB may
+	 * run out of buffers for DMA, resulting in I/O errors and/or

arch/x86/mm/mem_encrypt.c static void print_mem_encrypt_feature_info(void) · line 510

Quoted 10 lines
arch/x86/mm/mem_encrypt.c +10 -0
+	 * performance degradation especially with high I/O workloads.
+	 * Adjust the default size of SWIOTLB for SEV guests using
+	 * a percentage of guest memory for SWIOTLB buffers.
+	 * Also as the SWIOTLB bounce buffer memory is allocated
+	 * from low memory, ensure that the adjusted size is within
+	 * the limits of low available memory.
+	 *
+	 */
+	if (sev_active()) {
+		phys_addr_t total_mem = memblock_phys_mem_size();

Please integrate scripts/checkpatch.pl into your patch creation workflow. Some of the warnings/errors *actually* make sense:

WARNING: Missing a blank line after declarations
#95: FILE: arch/x86/mm/mem_encrypt.c:511:

arch/x86/mm/mem_encrypt.c +2 -0
+               phys_addr_t total_mem = memblock_phys_mem_size();
+               size = total_mem * SEV_ADJUST_SWIOTLB_SIZE_PERCENT / 100;

But no need to resend now - just a hint for the future.

Konrad, ack?

On a 2G guest here, it says:

[    0.018373] SWIOTLB bounce buffer size adjusted to 122MB for SEV

so it makes sense to me.

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fpeople.kernel.org%2Ftglx%2Fnotes-about-netiquette&amp;data=04%7C01%7CAshish.Kalra%40amd.com%7C5c7096203e774538383308d89aa95b4a%7C3dd8961fe4884e608e11a82d994e183d%7C0%7C0%7C637429399488203154%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=G8JOTIQ5eVyOU50iBAI3%2FotUlGV1EBuOZectZNWlUyE%3D&amp;reserved=0
maintainer
#3 lore raw
Quoted 11 lines · Ashish Kalra

This is related to the earlier static adjustment of the SWIOTLB buffers as per guest memory size and Konrad's feedback on the same, as copied below :

Quoted 7 lines
Quoted 7 lines

That is eating 128MB for 1GB, aka 12% of the guest memory allocated statically for this.

And for guests that are 2GB, that is 12% until it gets to 3GB when it is 8% and then 6% at 4GB.

I would prefer this to be based on your memory count, that is 6% of total memory.

So no rule of thumb and no measurements? Just a magic number 6.

Btw, pls trim the rest of the mail when you reply. Like I just did. There's no need to leave text which you're not responding to, quoted.

Thx.

Signature · 8 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
#4 lore raw
Quoted 11 lines · Borislav Petkov
Quoted 6 lines · Ashish Kalra

This is related to the earlier static adjustment of the SWIOTLB buffers as per guest memory size and Konrad's feedback on the same, as copied below :

Quoted 2 lines
Quoted 2 lines

That is eating 128MB for 1GB, aka 12% of the guest memory allocated statically for this.

Quoted 7 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

And for guests that are 2GB, that is 12% until it gets to 3GB when it is 8% and then 6% at 4GB.

I would prefer this to be based on your memory count, that is 6% of total memory.

So no rule of thumb and no measurements? Just a magic number 6.

It is more of an approximation of the earlier static adjustment which was 128M for <1G guests, 256M for 1G-4G guests and 512M for >4G guests.

Thanks,
Ashish
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

maintainer
#5 lore raw
Quoted 3 lines · Ashish

It is more of an approximation of the earlier static adjustment which was 128M for <1G guests, 256M for 1G-4G guests and 512M for >4G guests.

Makes sense and it is better than nothing. Please put that explanation over the 6% define.

Signature · 8 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu