wiotlb: split buffer into 32-bit default and 64-bit extra zones
Subsystems (8)
RFC PATCH v2 1/1 wiotlb: split buffer into 32-bit default and 64-bit extra zones
Hello,
I used to send out RFC v1 to introduce an extra io_tlb_mem (created with SWIOTLB_ANY) in addition to the default io_tlb_mem (32-bit). The dev->dma_io_tlb_mem is set to either default or the extra io_tlb_mem, depending on dma mask. However, that is not good for setting dev->dma_io_tlb_mem at swiotlb layer transparently as suggested by Christoph Hellwig.
https://lore.kernel.org/all/[email protected]/
Therefore, this is another RFC v2 implementation following a different direction. The core ideas are:
1. The swiotlb is splited into two zones, io_tlb_mem->zone[0] (32-bit) and io_tlb_mem->zone[1] (64-bit).
struct io_tlb_mem {
struct io_tlb_zone zone[SWIOTLB_NR];
struct dentry *debugfs;
bool late_alloc;
bool force_bounce;
bool for_alloc;
bool has_extra;
};struct io_tlb_zone {
phys_addr_t start;
phys_addr_t end;
void *vaddr;
unsigned long nslabs;
unsigned long used;
unsigned int nareas;
unsigned int area_nslabs;
struct io_tlb_area *areas;
struct io_tlb_slot *slots;
};2. By default, only io_tlb_mem->zone[0] is available. The io_tlb_mem->zone[1] is allocated conditionally if:
- the "swiotlb=" is configured to allocate extra buffer, and
- the SWIOTLB_EXTRA is set in the flag (this is to make sure arch(s) other than x86/sev/xen will not enable it until it is fully tested by each arch, e.g., mips/powerpc). Currently it is enabled for x86 and xen.
3. During swiotlb map, whether zone[0] (32-bit) or zone[1] (64-bit SWIOTLB_ANY)
is used depends on min_not_zero(*dev->dma_mask, dev->bus_dma_limit).
To test the RFC v2, here is the QEMU command line.
qemu-system-x86_64 -smp 8 -m 32G -enable-kvm -vnc :5 -hda disk.img \
-kernel path-to-linux/arch/x86_64/boot/bzImage \
-append "root=/dev/sda1 init=/sbin/init text console=ttyS0 loglevel=7 swiotlb=32768,4194304,force" \
-net nic -net user,hostfwd=tcp::5025-:22 \
-device nvme,drive=nvme01,serial=helloworld -drive file=test.qcow2,if=none,id=nvme01 \
-serial stdio
There is below in syslog. The extra 8GB buffer is allocated.
[ 0.152251] software IO TLB: area num 8. ... ... [ 3.706088] PCI-DMA: Using software bounce buffering for IO (SWIOTLB) [ 3.707334] software IO TLB: mapped default [mem 0x00000000bbfd7000-0x00000000bffd7000] (64MB) [ 3.708585] software IO TLB: mapped extra [mem 0x000000061cc00000-0x000000081cc00000] (8192MB)
After the FIO is triggered over NVMe, the 64-bit buffer is used.
$ cat /sys/kernel/debug/swiotlb/io_tlb_nslabs_extra 4194304 $ cat /sys/kernel/debug/swiotlb/io_tlb_used_extra 327552
Would you mind helping if this is the right direction to go?
Thank you very much!
--- arch/arm/xen/mm.c | 2 +- arch/mips/pci/pci-octeon.c | 5 +- arch/x86/include/asm/xen/swiotlb-xen.h | 2 +- arch/x86/kernel/pci-dma.c | 6 +- drivers/xen/swiotlb-xen.c | 18 +- include/linux/swiotlb.h | 73 +++-- kernel/dma/swiotlb.c | 499 +++++++++++++++++++++------------ 7 files changed, 388 insertions(+), 217 deletions(-)
arch/arm/xen/mm.c +1 -1
@@ -125,7 +125,7 @@ static int __init xen_mm_init(void)return 0 ;/* we can work with the default swiotlb */ -if (!io_tlb_default_mem.nslabs) { +if (!io_tlb_default_mem.zone[SWIOTLB_DF ].nslabs) { rc =swiotlb_init_late (swiotlb_size_or_default (),xen_swiotlb_gfp (),NULL );if (rc <0 )
arch/mips/pci/pci-octeon.c +4 -1
@@ -654,6 +654,9 @@ static int __init octeon_pci_setup(void) octeon_pci_mem_resource.end = octeon_pci_mem_resource.start + (1ul <<30 ); }else { +struct io_tlb_mem *mem = &io_tlb_default_mem; +struct io_tlb_zone *zone = &mem->zone[SWIOTLB_DF ]; +/* Remap the Octeon BAR 0 to map 128MB-(128MB+4KB) */ octeon_npi_write32 (CVMX_NPI_PCI_CFG04 ,128ul <<20 );octeon_npi_write32 (CVMX_NPI_PCI_CFG05 ,0 );
@@ -664,7 +667,7 @@ static int __init octeon_pci_setup(void)/* BAR1 movable regions contiguous to cover the swiotlb */ octeon_bar1_pci_phys = - io_tlb_default_mem.start & ~((1ull <<22 ) -1 ); + zone->start & ~((1ull <<22 ) -1 );for (index =0 ; index <32 ; index++) {union cvmx_pci_bar1_indexx bar1_index;
arch/x86/include/asm/xen/swiotlb-xen.h +1 -1
@@ -8,7 +8,7 @@static inline int pci_xen_swiotlb_init_late (void ) {return -ENXIO ; }#endif -int xen_swiotlb_fixup (void *buf,unsigned long nslabs); +int xen_swiotlb_fixup (void *buf,unsigned long nslabs,unsigned int flags);int xen_create_contiguous_region (phys_addr_t pstart,unsigned int order,unsigned int address_bits,dma_addr_t *dma_handle);
arch/x86/kernel/pci-dma.c +4 -2
@@ -77,7 +77,7 @@ static void __init pci_xen_swiotlb_init(void)if (!xen_initial_domain () && !x86_swiotlb_enable)return ; x86_swiotlb_enable = true; - x86_swiotlb_flags |=SWIOTLB_ANY ; + x86_swiotlb_flags |=SWIOTLB_ANY |SWIOTLB_EXTRA ;swiotlb_init_remap (true, x86_swiotlb_flags, xen_swiotlb_fixup); dma_ops = &xen_swiotlb_dma_ops;if (IS_ENABLED (CONFIG_PCI ))
@@ -90,7 +90,7 @@ int pci_xen_swiotlb_init_late(void)return 0 ;/* we can work with the default swiotlb */ -if (!io_tlb_default_mem.nslabs) { +if (!io_tlb_default_mem.zone[SWIOTLB_DF ].nslabs) {int rc =swiotlb_init_late (swiotlb_size_or_default (),GFP_KERNEL , xen_swiotlb_fixup);if (rc <0 )
@@ -120,6 +120,8 @@ void __init pci_iommu_alloc(void)gart_iommu_hole_init ();amd_iommu_detect ();detect_intel_iommu (); + + x86_swiotlb_flags |=SWIOTLB_EXTRA ;swiotlb_init (x86_swiotlb_enable, x86_swiotlb_flags); }
drivers/xen/swiotlb-xen.c +14 -4
@@ -38,7 +38,6 @@#include <asm/dma-mapping.h> #include <trace/events/swiotlb.h> -#define MAX_DMA_BITS 32/* * Quick lookup value of the bus address of the IOTLB.
@@ -104,24 +103,32 @@ static int is_xen_swiotlb_buffer(struct device *dev, dma_addr_t dma_addr) }#ifdef CONFIG_X86 -int xen_swiotlb_fixup (void *buf,unsigned long nslabs) +int xen_swiotlb_fixup (void *buf,unsigned long nslabs,unsigned int flags) {int rc;unsigned int order =get_order (IO_TLB_SEGSIZE <<IO_TLB_SHIFT );unsigned int i, dma_bits = order +PAGE_SHIFT ;dma_addr_t dma_handle;phys_addr_t p =virt_to_phys (buf); +unsigned int max_dma_bits =32 ;BUILD_BUG_ON (IO_TLB_SEGSIZE & (IO_TLB_SEGSIZE -1 ));BUG_ON (nslabs %IO_TLB_SEGSIZE ); +#ifdef CONFIG_X86_64 +if (flags &SWIOTLB_EXTRA ) { + max_dma_bits =64 ; + dma_bits =64 ; + } +#endif + i =0 ; do { do { rc = xen_create_contiguous_region( p + (i << IO_TLB_SHIFT), order, dma_bits, &dma_handle); - } while (rc && dma_bits++ < MAX_DMA_BITS); + } while (rc && dma_bits++ < max_dma_bits); if (rc) return rc;
@@ -381,7 +388,10 @@ static void xen_swiotlb_unmap_page(struct device *hwdev, dma_addr_t dev_addr,static int xen_swiotlb_dma_supported (struct device *hwdev,u64 mask) { -return xen_phys_to_dma (hwdev, io_tlb_default_mem.end -1 ) <= mask; +struct io_tlb_mem *mem = &io_tlb_default_mem; +struct io_tlb_zone *zone = &mem->zone[SWIOTLB_DF ]; + +return xen_phys_to_dma (hwdev, zone->end -1 ) <= mask; }const struct dma_map_ops xen_swiotlb_dma_ops = {
include/linux/swiotlb.h +51 -22
120 lines not on this page · raw message
kernel/dma/swiotlb.c +313 -186
927 lines not on this page · raw message
Signature · 1 line
1.8.3.1
Quoted 94 lines · Dongli Zhang
Hello,
I used to send out RFC v1 to introduce an extra
io_tlb_mem(created withSWIOTLB_ANY) in addition to the defaultio_tlb_mem(32-bit). Thedev->dma_io_tlb_memis set to either default or the extraio_tlb_mem, depending on dma mask. However, that is not good for settingdev->dma_io_tlb_memat swiotlb layer transparently as suggested by Christoph Hellwig.https://lore.kernel.org/all/[email protected]/
Therefore, this is another RFC v2 implementation following a different direction. The core ideas are:
1. The swiotlb is splited into two zones, io_tlb_mem->zone[0] (32-bit) and io_tlb_mem->zone[1] (64-bit).
struct io_tlb_mem { struct io_tlb_zone zone[SWIOTLB_NR]; struct dentry *debugfs; bool late_alloc; bool force_bounce; bool for_alloc; bool has_extra; };struct io_tlb_zone { phys_addr_t start; phys_addr_t end; void *vaddr; unsigned long nslabs; unsigned long used; unsigned int nareas; unsigned int area_nslabs; struct io_tlb_area *areas; struct io_tlb_slot *slots; };2. By default, only io_tlb_mem->zone[0] is available. The io_tlb_mem->zone[1] is allocated conditionally if:
- the "swiotlb=" is configured to allocate extra buffer, and
- theSWIOTLB_EXTRAis set in the flag (this is to make sure arch(s) other than x86/sev/xen will not enable it until it is fully tested by each arch, e.g., mips/powerpc). Currently it is enabled for x86 and xen.3. During swiotlb map, whether zone[0] (32-bit) or zone[1] (64-bit
SWIOTLB_ANY)
is used depends onmin_not_zero(*dev->dma_mask, dev->bus_dma_limit).To test the RFC v2, here is the QEMU command line.
qemu-system-x86_64 -smp 8 -m 32G -enable-kvm -vnc :5 -hda disk.img \
(quoted diff) +0
-5-kernel path-to-linux/arch/x86_64/boot/bzImage \ -append "root=/dev/sda1 init=/sbin/init text console=ttyS0 loglevel=7 swiotlb=32768,4194304,force" \ -net nic -net user,hostfwd=tcp::5025-:22 \ -device nvme,drive=nvme01,serial=helloworld -drive file=test.qcow2,if=none,id=nvme01 \ -serial stdioThere is below in syslog. The extra 8GB buffer is allocated.
[ 0.152251] software IO TLB: area num 8. ... ... [ 3.706088] PCI-DMA: Using software bounce buffering for IO (SWIOTLB) [ 3.707334] software IO TLB: mapped default [mem 0x00000000bbfd7000-0x00000000bffd7000] (64MB) [ 3.708585] software IO TLB: mapped extra [mem 0x000000061cc00000-0x000000081cc00000] (8192MB)After the FIO is triggered over NVMe, the 64-bit buffer is used.
$ cat /sys/kernel/debug/swiotlb/io_tlb_nslabs_extra 4194304 $ cat /sys/kernel/debug/swiotlb/io_tlb_used_extra 327552Would you mind helping if this is the right direction to go?
Thank you very much!
Cc Konrad Wilk <[email protected]>Cc Joe Jin <[email protected]>Signed-off-by Dongli Zhang <[email protected]>--- arch/arm/xen/mm.c | 2 +- arch/mips/pci/pci-octeon.c | 5 +- arch/x86/include/asm/xen/swiotlb-xen.h | 2 +- arch/x86/kernel/pci-dma.c | 6 +- drivers/xen/swiotlb-xen.c | 18 +- include/linux/swiotlb.h | 73 +++--arch/arm/xen/mm.c +0
-0
arch/arm/xen/mm.c static int __init xen_mm_init(void) · line 131
Quoted 8 lines
arch/arm/xen/mm.c +1
-1@@ -125,7 +125,7 @@ static int __init xen_mm_init(void)return 0 ;/* we can work with the default swiotlb */ -if (!io_tlb_default_mem.nslabs) { +if (!io_tlb_default_mem.zone[SWIOTLB_DF ].nslabs) { rc =swiotlb_init_late (swiotlb_size_or_default (),xen_swiotlb_gfp (),NULL );if (rc <0 )
First thing we need before doing anything about multiple default pools is to get all the knowledge of details hidden inside swiotlb.c.
For swiotlb_init_late that seems easy as we can just move the check into it.
Quoted 15 lines
arch/mips/pci/pci-octeon.c +3
-0@@ -654,6 +654,9 @@ static int __init octeon_pci_setup(void) octeon_pci_mem_resource.end = octeon_pci_mem_resource.start + (1ul <<30 ); }else { +struct io_tlb_mem *mem = &io_tlb_default_mem; +struct io_tlb_zone *zone = &mem->zone[SWIOTLB_DF ]; +/* Remap the Octeon BAR 0 to map 128MB-(128MB+4KB) */ octeon_npi_write32 (CVMX_NPI_PCI_CFG04 ,128ul <<20 );octeon_npi_write32 (CVMX_NPI_PCI_CFG05 ,0 );
arch/mips/pci/pci-octeon.c static int __init octeon_pci_setup(void) · line 670
Quoted 5 lines
arch/mips/pci/pci-octeon.c +1
-1@@ -664,7 +667,7 @@ static int __init octeon_pci_setup(void)/* BAR1 movable regions contiguous to cover the swiotlb */ octeon_bar1_pci_phys = - io_tlb_default_mem.start & ~((1ull <<22 ) -1 ); + zone->start & ~((1ull <<22 ) -1 );
But we'll need to do something about this mess. I'll need help from the octeon maintainer on it.
arch/mips/pci/pci-octeon.c static int __init octeon_pci_setup(void) · line 671
Quoted 2 lines
arch/mips/pci/pci-octeon.c +1
-1- x86_swiotlb_flags |=SWIOTLB_ANY ; + x86_swiotlb_flags |=SWIOTLB_ANY |SWIOTLB_EXTRA ;
I don't think this is how it is suppoed to be. SWIOTLB_ANY already says give me a pool with no addressing constrains. We don't need two pools without that. EXTRA is also not exactly a very helpful name here.
arch/mips/pci/pci-octeon.c
Quoted 10 lines
arch/mips/pci/pci-octeon.c +1
-1#ifdef CONFIG_X86 -int xen_swiotlb_fixup (void *buf,unsigned long nslabs) +int xen_swiotlb_fixup (void *buf,unsigned long nslabs,unsigned int flags){
arch/mips/pci/pci-octeon.c +1
-0int rc;unsigned int order =get_order (IO_TLB_SEGSIZE <<IO_TLB_SHIFT );unsigned int i, dma_bits = order +PAGE_SHIFT ;dma_addr_t dma_handle;phys_addr_t p =virt_to_phys (buf); +unsigned int max_dma_bits =32 ;
I think live will be a lot simple if the addressing bits are passed to this function, and not some kind of flags.
arch/mips/pci/pci-octeon.c
Quoted 3 lines
arch/mips/pci/pci-octeon.c +3
-0+#define SWIOTLB_DF 0 +#define SWIOTLB_EX 1 +#define SWIOTLB_NR 2
These names are not very descriptive.