iommu.lists.linux-foundation.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v7] swiotlb: Adjust SWIOTBL bounce buffer size for SEV guests.
@ 2020-12-03  3:25 Ashish Kalra
  2020-12-07 12:10 ` Borislav Petkov
  0 siblings, 1 reply; 6+ messages in thread
From: Ashish Kalra @ 2020-12-03  3:25 UTC (permalink / raw)
  To: konrad.wilk
  Cc: Thomas.Lendacky, Jon.Grimm, brijesh.singh, dave.hansen, peterz,
	x86, linux-kernel, iommu, mingo, bp, luto, hpa, rientjes, tglx,
	hch

From: Ashish Kalra <ashish.kalra@amd.com>

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
Reported-by: kbuild test robot <lkp@intel.com>

Signed-off-by: Ashish Kalra <ashish.kalra@amd.com>
---
 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(+)

diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c
index 84f581c91db4..31e24e198061 100644
--- a/arch/x86/kernel/setup.c
+++ b/arch/x86/kernel/setup.c
@@ -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.
diff --git a/arch/x86/mm/mem_encrypt.c b/arch/x86/mm/mem_encrypt.c
index 1bcfbcd2bfd7..46549bd3d840 100644
--- a/arch/x86/mm/mem_encrypt.c
+++ b/arch/x86/mm/mem_encrypt.c
@@ -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)
diff --git a/include/linux/swiotlb.h b/include/linux/swiotlb.h
index 3bb72266a75a..b5904fa4b67c 100644
--- a/include/linux/swiotlb.h
+++ b/include/linux/swiotlb.h
@@ -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);
diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c
index 781b9dca197c..0150ca2336bc 100644
--- a/kernel/dma/swiotlb.c
+++ b/kernel/dma/swiotlb.c
@@ -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;
-- 
2.17.1

_______________________________________________
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu

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

* Re: [PATCH v7] swiotlb: Adjust SWIOTBL bounce buffer size for SEV guests.
  2020-12-03  3:25 [PATCH v7] swiotlb: Adjust SWIOTBL bounce buffer size for SEV guests Ashish Kalra
@ 2020-12-07 12:10 ` Borislav Petkov
  2020-12-07 22:06   ` Ashish Kalra
  0 siblings, 1 reply; 6+ messages in thread
From: Borislav Petkov @ 2020-12-07 12:10 UTC (permalink / raw)
  To: Ashish Kalra
  Cc: Thomas.Lendacky, Jon.Grimm, brijesh.singh, dave.hansen,
	konrad.wilk, peterz, x86, linux-kernel, iommu, mingo, luto, hpa,
	rientjes, tglx, hch

On Thu, Dec 03, 2020 at 03:25:59AM +0000, Ashish Kalra wrote:
> diff --git a/arch/x86/mm/mem_encrypt.c b/arch/x86/mm/mem_encrypt.c
> index 1bcfbcd2bfd7..46549bd3d840 100644
> --- a/arch/x86/mm/mem_encrypt.c
> +++ b/arch/x86/mm/mem_encrypt.c
> @@ -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?

> +#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();

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.

-- 
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
_______________________________________________
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu

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

* Re: [PATCH v7] swiotlb: Adjust SWIOTBL bounce buffer size for SEV guests.
  2020-12-07 12:10 ` Borislav Petkov
@ 2020-12-07 22:06   ` Ashish Kalra
  2020-12-07 22:14     ` Borislav Petkov
  0 siblings, 1 reply; 6+ messages in thread
From: Ashish Kalra @ 2020-12-07 22:06 UTC (permalink / raw)
  To: Borislav Petkov
  Cc: Thomas.Lendacky, Jon.Grimm, brijesh.singh, dave.hansen,
	konrad.wilk, peterz, x86, linux-kernel, iommu, mingo, luto, hpa,
	rientjes, tglx, hch

Hello Boris, 

On Mon, Dec 07, 2020 at 01:10:07PM +0100, Borislav Petkov wrote:
> On Thu, Dec 03, 2020 at 03:25:59AM +0000, Ashish Kalra wrote:
> > diff --git a/arch/x86/mm/mem_encrypt.c b/arch/x86/mm/mem_encrypt.c
> > index 1bcfbcd2bfd7..46549bd3d840 100644
> > --- a/arch/x86/mm/mem_encrypt.c
> > +++ b/arch/x86/mm/mem_encrypt.c
> > @@ -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 : 

>>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

> > +#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();
> 
> 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.
> 
> -- 
> 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
_______________________________________________
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu

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

* Re: [PATCH v7] swiotlb: Adjust SWIOTBL bounce buffer size for SEV guests.
  2020-12-07 22:06   ` Ashish Kalra
@ 2020-12-07 22:14     ` Borislav Petkov
  2020-12-07 22:20       ` Kalra, Ashish
  0 siblings, 1 reply; 6+ messages in thread
From: Borislav Petkov @ 2020-12-07 22:14 UTC (permalink / raw)
  To: Ashish Kalra
  Cc: Thomas.Lendacky, Jon.Grimm, brijesh.singh, dave.hansen,
	konrad.wilk, peterz, x86, linux-kernel, iommu, mingo, luto, hpa,
	rientjes, tglx, hch

On Mon, Dec 07, 2020 at 10:06:24PM +0000, Ashish Kalra wrote:
> 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 : 
> 
> >>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.

-- 
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
_______________________________________________
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu

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

* Re: [PATCH v7] swiotlb: Adjust SWIOTBL bounce buffer size for SEV guests.
  2020-12-07 22:14     ` Borislav Petkov
@ 2020-12-07 22:20       ` Kalra, Ashish
  2020-12-07 22:33         ` Borislav Petkov
  0 siblings, 1 reply; 6+ messages in thread
From: Kalra, Ashish @ 2020-12-07 22:20 UTC (permalink / raw)
  To: Borislav Petkov
  Cc: Lendacky, Thomas, Grimm, Jon, Singh, Brijesh, dave.hansen,
	konrad.wilk, peterz, x86, linux-kernel, iommu, mingo, luto, hpa,
	rientjes, tglx, hch



> On Dec 7, 2020, at 4:14 PM, Borislav Petkov <bp@alien8.de> wrote:
> 
> On Mon, Dec 07, 2020 at 10:06:24PM +0000, Ashish Kalra wrote:
>> 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 : 
>> 
>>>> 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.

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
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu

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

* Re: [PATCH v7] swiotlb: Adjust SWIOTBL bounce buffer size for SEV guests.
  2020-12-07 22:20       ` Kalra, Ashish
@ 2020-12-07 22:33         ` Borislav Petkov
  0 siblings, 0 replies; 6+ messages in thread
From: Borislav Petkov @ 2020-12-07 22:33 UTC (permalink / raw)
  To: Kalra, Ashish
  Cc: Lendacky, Thomas, Grimm, Jon, Singh, Brijesh, dave.hansen,
	konrad.wilk, peterz, x86, linux-kernel, iommu, mingo, luto, hpa,
	rientjes, tglx, hch

On Mon, Dec 07, 2020 at 10:20:20PM +0000, Kalra, Ashish wrote:
> 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.

-- 
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
_______________________________________________
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu

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

end of thread, other threads:[~2020-12-07 22:33 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-03  3:25 [PATCH v7] swiotlb: Adjust SWIOTBL bounce buffer size for SEV guests Ashish Kalra
2020-12-07 12:10 ` Borislav Petkov
2020-12-07 22:06   ` Ashish Kalra
2020-12-07 22:14     ` Borislav Petkov
2020-12-07 22:20       ` Kalra, Ashish
2020-12-07 22:33         ` Borislav Petkov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).