swiotlb cleanups v3
Subsystems (9)
Hi Konrad,
this series contains a bunch of swiotlb cleanups, mostly to reduce the amount of internals exposed to code outside of swiotlb.c, which should helper to prepare for supporting multiple different bounce buffer pools.
Changes since v2:
- fix a bisetion hazard that did not allocate the alloc_size array
- dropped all patches already merged
Changes since v1:
- rebased to v5.12-rc1
- a few more cleanups
- merge and forward port the patch from Claire to move all the global variables into a struct to prepare for multiple instances
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
PATCH 1/3 swiotlb: move global variables into a new io_tlb_mem structure
From: Claire Chang <[email protected]>
Added a new struct, io_tlb_mem, as the IO TLB memory pool descriptor and moved relevant global variables into that struct.
This will be useful later to allow for restricted DMA pool.
--- drivers/xen/swiotlb-xen.c | 2 +- include/linux/swiotlb.h | 43 ++++- kernel/dma/swiotlb.c | 354 ++++++++++++++++++-------------------- 3 files changed, 206 insertions(+), 193 deletions(-)
drivers/xen/swiotlb-xen.c +1 -1
@@ -548,7 +548,7 @@ xen_swiotlb_sync_sg_for_device(struct device *dev, struct scatterlist *sgl,static int xen_swiotlb_dma_supported (struct device *hwdev,u64 mask) { -return xen_phys_to_dma (hwdev, io_tlb_end -1 ) <= mask; +return xen_phys_to_dma (hwdev, io_tlb_default_mem.end -1 ) <= mask; }const struct dma_map_ops xen_swiotlb_dma_ops = {
include/linux/swiotlb.h +41 -2
58 lines not on this page · raw message
kernel/dma/swiotlb.c +164 -190
674 lines not on this page · raw message
Signature · 1 line
2.30.1
PATCH 2/3 swiotlb: dynamically allocate io_tlb_default_mem
In reply to Christoph HellwigInstead of allocating ->list and ->orig_addr separately just do one dynamic allocation for the actual io_tlb_mem structure. This simplifies a lot of the initialization code, and also allows to just check io_tlb_default_mem to see if swiotlb is in use.
--- drivers/xen/swiotlb-xen.c | 22 +-- include/linux/swiotlb.h | 18 ++- kernel/dma/swiotlb.c | 306 ++++++++++++-------------------------- 3 files changed, 117 insertions(+), 229 deletions(-)
drivers/xen/swiotlb-xen.c +8 -14
@@ -158,17 +158,14 @@ static const char *xen_swiotlb_error(enum xen_swiotlb_err err)int __ref xen_swiotlb_init (void ) {enum xen_swiotlb_err m_ret =XEN_SWIOTLB_UNKNOWN ; -unsigned long nslabs, bytes, order; -unsigned int repeat =3 ; +unsigned long bytes =swiotlb_size_or_default (); +unsigned long nslabs = bytes >>IO_TLB_SHIFT ; +unsigned int order, repeat =3 ;int rc = -ENOMEM ;char *start; - nslabs =swiotlb_nr_tbl (); -if (!nslabs) - nslabs =DEFAULT_NSLABS ; retry: m_ret =XEN_SWIOTLB_ENOMEM ; - bytes = nslabs <<IO_TLB_SHIFT ; order =get_order (bytes);/*
@@ -221,19 +218,16 @@ int __ref xen_swiotlb_init(void)#ifdef CONFIG_X86 void __init xen_swiotlb_init_early (void ) { -unsigned long nslabs, bytes; +unsigned long bytes =swiotlb_size_or_default (); +unsigned long nslabs = bytes >>IO_TLB_SHIFT ;unsigned int repeat =3 ;char *start;int rc; - nslabs =swiotlb_nr_tbl (); -if (!nslabs) - nslabs =DEFAULT_NSLABS ; retry:/* * Get IO TLB memory from any location. */ - bytes = nslabs <<IO_TLB_SHIFT ; start =memblock_alloc (PAGE_ALIGN (bytes),PAGE_SIZE );if (!start) panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
@@ -248,8 +242,8 @@ void __init xen_swiotlb_init_early(void)if (repeat--) {/* Min is 2MB */ nslabs =max (1024UL , (nslabs >>1 )); -pr_info ("Lowering to %luMB\n" , - (nslabs <<IO_TLB_SHIFT ) >>20 ); + bytes = nslabs <<IO_TLB_SHIFT ; +pr_info ("Lowering to %luMB\n" , bytes >>20 ); goto retry; }panic ("%s (rc:%d)" ,xen_swiotlb_error (XEN_SWIOTLB_EFIXUP ), rc);
@@ -548,7 +542,7 @@ xen_swiotlb_sync_sg_for_device(struct device *dev, struct scatterlist *sgl,static int xen_swiotlb_dma_supported (struct device *hwdev,u64 mask) { -return xen_phys_to_dma (hwdev, io_tlb_default_mem.end -1 ) <= mask; +return xen_phys_to_dma (hwdev, io_tlb_default_mem->end -1 ) <= mask; }const struct dma_map_ops xen_swiotlb_dma_ops = {
include/linux/swiotlb.h +10 -8
@@ -90,28 +90,30 @@ struct io_tlb_mem {phys_addr_t end;unsigned long nslabs;unsigned long used; -unsigned int *list;unsigned int index; -phys_addr_t *orig_addr; -size_t *alloc_size;spinlock_t lock;struct dentry *debugfs;bool late_alloc; +struct io_tlb_slot { +phys_addr_t orig_addr; +size_t alloc_size; +unsigned int list; + } slots[]; }; -extern struct io_tlb_mem io_tlb_default_mem; +extern struct io_tlb_mem *io_tlb_default_mem;static inline bool is_swiotlb_buffer (phys_addr_t paddr) { -struct io_tlb_mem *mem = &io_tlb_default_mem; +struct io_tlb_mem *mem = io_tlb_default_mem; -return paddr >= mem->start && paddr < mem->end; +return mem && paddr >= mem->start && paddr < mem->end; }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_size (unsigned long new_size); +void __init swiotlb_adjust_size (unsigned long size);#else #define swiotlb_force SWIOTLB_NO_FORCEstatic inline bool is_swiotlb_buffer (phys_addr_t paddr)
@@ -135,7 +137,7 @@ static inline bool is_swiotlb_active(void) return false; } -static inline void swiotlb_adjust_size (unsigned long new_size) +static inline void swiotlb_adjust_size (unsigned long size) { }#endif /* CONFIG_SWIOTLB */
kernel/dma/swiotlb.c +99 -207
547 lines not on this page · raw message
Signature · 1 line
2.30.1
PATCH 3/3 swiotlb: remove swiotlb_nr_tbl
In reply to Christoph HellwigAll callers just use it to check if swiotlb is active at all, for which they can just use is_swiotlb_active. In the longer run drivers need to stop using is_swiotlb_active as well, but let's do the simple step first.
--- drivers/gpu/drm/i915/gem/i915_gem_internal.c | 2 +- drivers/gpu/drm/nouveau/nouveau_ttm.c | 2 +- drivers/pci/xen-pcifront.c | 2 +- include/linux/swiotlb.h | 1 - kernel/dma/swiotlb.c | 7 +------ 5 files changed, 4 insertions(+), 10 deletions(-)
drivers/gpu/drm/i915/gem/i915_gem_internal.c +1 -1
@@ -42,7 +42,7 @@ static int i915_gem_object_get_pages_internal(struct drm_i915_gem_object *obj) max_order =MAX_ORDER ;#ifdef CONFIG_SWIOTLB -if (swiotlb_nr_tbl ()) { +if (is_swiotlb_active ()) {unsigned int max_segment; max_segment =swiotlb_max_segment ();
drivers/gpu/drm/nouveau/nouveau_ttm.c +1 -1
@@ -321,7 +321,7 @@ nouveau_ttm_init(struct nouveau_drm *drm) }#if IS_ENABLED (CONFIG_SWIOTLB ) &&IS_ENABLED (CONFIG_X86 ) - need_swiotlb = !!swiotlb_nr_tbl (); + need_swiotlb =is_swiotlb_active ();#endif ret = ttm_bo_device_init(&drm->ttm.bdev, &nouveau_bo_driver,
drivers/pci/xen-pcifront.c +1 -1
@@ -693,7 +693,7 @@ static int pcifront_connect_and_init_dma(struct pcifront_device *pdev)spin_unlock (&pcifront_dev_lock); -if (!err && !swiotlb_nr_tbl ()) { +if (!err && !is_swiotlb_active ()) { err =pci_xen_swiotlb_init_late ();if (err)dev_err (&pdev->xdev->dev,"Could not setup SWIOTLB!\n" );
include/linux/swiotlb.h +0 -1
@@ -37,7 +37,6 @@ enum swiotlb_force {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 );extern int swiotlb_late_init_with_tbl (char *tlb,unsigned long nslabs);extern int swiotlb_late_init_with_default_size (size_t default_size);
kernel/dma/swiotlb.c +1 -6
@@ -94,12 +94,6 @@ setup_io_tlb_npages(char *str) }early_param ("swiotlb" , setup_io_tlb_npages); -unsigned long swiotlb_nr_tbl (void ) -{ -return io_tlb_default_mem ? io_tlb_default_mem->nslabs :0 ; -} -EXPORT_SYMBOL_GPL (swiotlb_nr_tbl); -unsigned int swiotlb_max_segment (void ) {return io_tlb_default_mem ? max_segment :0 ;
@@ -652,6 +646,7 @@ bool is_swiotlb_active(void) {return io_tlb_default_mem !=NULL ; } +EXPORT_SYMBOL_GPL (is_swiotlb_active);#ifdef CONFIG_DEBUG_FS
Signature · 1 line
2.30.1
Quoted 5 lines
Hi Konrad,
this series contains a bunch of swiotlb cleanups, mostly to reduce the amount of internals exposed to code outside of
swiotlb.c, which should helper to prepare for supporting multiple different bounce buffer pools.
Somewhere between the 1st and 2nd patch, specifying a specific swiotlb for an SEV guest is no longer honored. For example, if I start an SEV guest with 16GB of memory and specify swiotlb=131072 I used to get a 256MB SWIOTLB. However, after the 2nd patch, the swiotlb=131072 is no longer honored and I get a 982MB SWIOTLB (as set via sev_setup_arch() in arch/x86/mm/mem_encrypt.c).
I can't be sure which patch caused the issue since an SEV guest fails to boot with the 1st patch but can boot with the 2nd patch, at which point the SWIOTLB comes in at 982MB (I haven't had a chance to debug it and so I'm hoping you might be able to quickly spot what's going on).
Thanks,
Tom
Quoted 10 lines
Changes since v2:
- fix a bisetion hazard that did not allocate thealloc_sizearray
- dropped all patches already mergedChanges since v1:
- rebased to v5.12-rc1
- a few more cleanups
- merge and forward port the patch from Claire to move all the global variables into a struct to prepare for multiple instances
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
Quoted 13 lines · Tom Lendacky
Quoted 5 lines
Hi Konrad,
this series contains a bunch of swiotlb cleanups, mostly to reduce the amount of internals exposed to code outside of
swiotlb.c, which should helper to prepare for supporting multiple different bounce buffer pools.Somewhere between the 1st and 2nd patch, specifying a specific swiotlb for an SEV guest is no longer honored. For example, if I start an SEV guest with 16GB of memory and specify swiotlb=131072 I used to get a 256MB SWIOTLB. However, after the 2nd patch, the swiotlb=131072 is no longer honored and I get a 982MB SWIOTLB (as set via
sev_setup_arch()inarch/x86/mm/mem_encrypt.c).
Quoted 4 lines
I can't be sure which patch caused the issue since an SEV guest fails to boot with the 1st patch but can boot with the 2nd patch, at which point the SWIOTLB comes in at 982MB (I haven't had a chance to debug it and so I'm hoping you might be able to quickly spot what's going on).
Ok, I figured out the 1st patch boot issue (which is gone when the second patch is applied). Here's the issue if anyone is interested:
kernel/dma/swiotlb.c +1 -1
@@ -226,7 +226,7 @@ int __init swiotlb_init_with_tbl(char *tlb, unsigned long nslabs, int verbose) alloc_size =PAGE_ALIGN (mem->nslabs *sizeof (size_t )); mem->alloc_size =memblock_alloc (alloc_size,PAGE_SIZE ); -if (mem->alloc_size) +if (!mem->alloc_size)panic ("%s: Failed to allocate %zu bytes align=0x%lx\n" , __func__, alloc_size,PAGE_SIZE );
The 1st patch still allowed the command line specified size of 256MB SWIOTLB. So that means the 2nd patch causes the command line specified 256MB SWIOTLB size to be ignored and results in a 982MB SWIOTLB size for the 16GB guest.
Thanks,
Tom
Quoted 15 lines
Thanks,
TomQuoted 10 lines
Changes since v2:
- fix a bisetion hazard that did not allocate thealloc_sizearray
- dropped all patches already mergedChanges since v1:
- rebased to v5.12-rc1
- a few more cleanups
- merge and forward port the patch from Claire to move all the global variables into a struct to prepare for multiple instances
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
Quoted 11 lines · Tom Lendacky
Somewhere between the 1st and 2nd patch, specifying a specific swiotlb for an SEV guest is no longer honored. For example, if I start an SEV guest with 16GB of memory and specify swiotlb=131072 I used to get a 256MB SWIOTLB. However, after the 2nd patch, the swiotlb=131072 is no longer honored and I get a 982MB SWIOTLB (as set via
sev_setup_arch()inarch/x86/mm/mem_encrypt.c).I can't be sure which patch caused the issue since an SEV guest fails to boot with the 1st patch but can boot with the 2nd patch, at which point the SWIOTLB comes in at 982MB (I haven't had a chance to debug it and so I'm hoping you might be able to quickly spot what's going on).
Can you try this patch?
kernel/dma/swiotlb.c +14 -9
@@ -71,15 +71,17 @@ struct io_tlb_mem *io_tlb_default_mem;*/ static unsigned int max_segment; -static unsigned long default_nslabs =IO_TLB_DEFAULT_SIZE >>IO_TLB_SHIFT ; +static unsigned long swiotlb_cmdline_size;static int __init setup_io_tlb_npages (char *str) {if (isdigit (*str)) {/* avoid tail segment of size < IO_TLB_SEGSIZE */ - default_nslabs = -ALIGN (simple_strtoul (str, &str,0 ),IO_TLB_SEGSIZE ); +unsigned long nslabs =simple_strtoul (str, &str,0 ); + + swiotlb_cmdline_size = +ALIGN (nslabs,IO_TLB_SEGSIZE ) <<IO_TLB_SHIFT ; }if (*str ==',' ) ++str;
@@ -108,7 +110,9 @@ void swiotlb_set_max_segment(unsigned int val)unsigned long swiotlb_size_or_default (void ) { -return default_nslabs <<IO_TLB_SHIFT ; +if (swiotlb_cmdline_size) +return swiotlb_cmdline_size; +return IO_TLB_DEFAULT_SIZE ; }void __init swiotlb_adjust_size (unsigned long size)
@@ -118,9 +122,10 @@ void __init swiotlb_adjust_size(unsigned long size)* architectures such as those supporting memory encryption to * adjust/expand SWIOTLB size for their use. */ - size =ALIGN (size,IO_TLB_SIZE ); - default_nslabs =ALIGN (size >>IO_TLB_SHIFT ,IO_TLB_SEGSIZE ); -pr_info ("SWIOTLB bounce buffer size adjusted to %luMB" , size >>20 ); +if (!swiotlb_cmdline_size) + swiotlb_cmdline_size =ALIGN (size,IO_TLB_SIZE ); +pr_info ("SWIOTLB bounce buffer size adjusted to %luMB" , + swiotlb_cmdline_size >>20 ); }void swiotlb_print_info (void )
@@ -209,7 +214,7 @@ int __init swiotlb_init_with_tbl(char *tlb, unsigned long nslabs, int verbose)void __init swiotlb_init (int verbose) { -size_t bytes =PAGE_ALIGN (default_nslabs <<IO_TLB_SHIFT ); +size_t bytes =PAGE_ALIGN (swiotlb_size_or_default ());void *tlb;if (swiotlb_force ==SWIOTLB_NO_FORCE )
@@ -219,7 +224,7 @@ swiotlb_init(int verbose) tlb =memblock_alloc_low (bytes,PAGE_SIZE );if (!tlb) goto fail; -if (swiotlb_init_with_tbl (tlb, default_nslabs, verbose)) +if (swiotlb_init_with_tbl (tlb, bytes >>IO_TLB_SHIFT , verbose)) goto fail_free_mem;return ;
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
Quoted 8 lines · Christoph Hellwig
Quoted 7 lines · Tom Lendacky
Somewhere between the 1st and 2nd patch, specifying a specific swiotlb for an SEV guest is no longer honored. For example, if I start an SEV guest with 16GB of memory and specify swiotlb=131072 I used to get a 256MB SWIOTLB. However, after the 2nd patch, the swiotlb=131072 is no longer honored and I get a 982MB SWIOTLB (as set via
sev_setup_arch()inarch/x86/mm/mem_encrypt.c).
Quoted 6 lines
Quoted 4 lines
I can't be sure which patch caused the issue since an SEV guest fails to boot with the 1st patch but can boot with the 2nd patch, at which point the SWIOTLB comes in at 982MB (I haven't had a chance to debug it and so I'm hoping you might be able to quickly spot what's going on).
Can you try this patch?
Thanks, Christoph. This works for honoring the command line value with SEV guests.
There was still a reference to default_nslabs in setup_io_tlb_npages() that I'm not sure how you want to handle. I just commented it out for now to let the code compile to test the intent of the patch.
Thanks,
Tom
Quoted 62 lines
kernel/dma/swiotlb.c +13
-8@@ -71,15 +71,17 @@ struct io_tlb_mem *io_tlb_default_mem;*/ static unsigned int max_segment; -static unsigned long default_nslabs =IO_TLB_DEFAULT_SIZE >>IO_TLB_SHIFT ; +static unsigned long swiotlb_cmdline_size;static int __init setup_io_tlb_npages (char *str) {if (isdigit (*str)) {/* avoid tail segment of size < IO_TLB_SEGSIZE */ - default_nslabs = -ALIGN (simple_strtoul (str, &str,0 ),IO_TLB_SEGSIZE ); +unsigned long nslabs =simple_strtoul (str, &str,0 ); + + swiotlb_cmdline_size = +ALIGN (nslabs,IO_TLB_SEGSIZE ) <<IO_TLB_SHIFT ; }if (*str ==',' ) ++str;@@ -108,7 +110,9 @@ void swiotlb_set_max_segment(unsigned int val)unsigned long swiotlb_size_or_default (void ) { -return default_nslabs <<IO_TLB_SHIFT ; +if (swiotlb_cmdline_size) +return swiotlb_cmdline_size; +return IO_TLB_DEFAULT_SIZE ; }void __init swiotlb_adjust_size (unsigned long size)@@ -118,9 +122,10 @@ void __init swiotlb_adjust_size(unsigned long size)* architectures such as those supporting memory encryption to * adjust/expand SWIOTLB size for their use. */ - size =ALIGN (size,IO_TLB_SIZE ); - default_nslabs =ALIGN (size >>IO_TLB_SHIFT ,IO_TLB_SEGSIZE ); -pr_info ("SWIOTLB bounce buffer size adjusted to %luMB" , size >>20 ); +if (!swiotlb_cmdline_size) + swiotlb_cmdline_size =ALIGN (size,IO_TLB_SIZE ); +pr_info ("SWIOTLB bounce buffer size adjusted to %luMB" , + swiotlb_cmdline_size >>20 ); }void swiotlb_print_info (void )@@ -209,7 +214,7 @@ int __init swiotlb_init_with_tbl(char *tlb, unsigned long nslabs, int verbose)void __init swiotlb_init (int verbose) { -size_t bytes =PAGE_ALIGN (default_nslabs <<IO_TLB_SHIFT ); +size_t bytes =PAGE_ALIGN (swiotlb_size_or_default ());void *tlb;if (swiotlb_force ==SWIOTLB_NO_FORCE )
kernel/dma/swiotlb.c swiotlb_init(int verbose) · line 230
Quoted 8 lines
kernel/dma/swiotlb.c +1
-1@@ -219,7 +224,7 @@ swiotlb_init(int verbose) tlb =memblock_alloc_low (bytes,PAGE_SIZE );if (!tlb) goto fail; -if (swiotlb_init_with_tbl (tlb, default_nslabs, verbose)) +if (swiotlb_init_with_tbl (tlb, bytes >>IO_TLB_SHIFT , verbose)) goto fail_free_mem;return ;
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu