Skip to content

Commit 6c1b980

Browse files
committed
Merge tag 'dma-mapping-6.6-2023-08-29' of git://git.infradead.org/users/hch/dma-mapping
Pull dma-maping updates from Christoph Hellwig: - allow dynamic sizing of the swiotlb buffer, to cater for secure virtualization workloads that require all I/O to be bounce buffered (Petr Tesarik) - move a declaration to a header (Arnd Bergmann) - check for memory region overlap in dma-contiguous (Binglei Wang) - remove the somewhat dangerous runtime swiotlb-xen enablement and unexport is_swiotlb_active (Christoph Hellwig, Juergen Gross) - per-node CMA improvements (Yajun Deng) * tag 'dma-mapping-6.6-2023-08-29' of git://git.infradead.org/users/hch/dma-mapping: swiotlb: optimize get_max_slots() swiotlb: move slot allocation explanation comment where it belongs swiotlb: search the software IO TLB only if the device makes use of it swiotlb: allocate a new memory pool when existing pools are full swiotlb: determine potential physical address limit swiotlb: if swiotlb is full, fall back to a transient memory pool swiotlb: add a flag whether SWIOTLB is allowed to grow swiotlb: separate memory pool data from other allocator data swiotlb: add documentation and rename swiotlb_do_find_slots() swiotlb: make io_tlb_default_mem local to swiotlb.c swiotlb: bail out of swiotlb_init_late() if swiotlb is already allocated dma-contiguous: check for memory region overlap dma-contiguous: support numa CMA for specified node dma-contiguous: support per-numa CMA for all architectures dma-mapping: move arch_dma_set_mask() declaration to header swiotlb: unexport is_swiotlb_active x86: always initialize xen-swiotlb when xen-pcifront is enabling xen/pci: add flag for PCI passthrough being possible
2 parents 3d3dfeb + d069ed2 commit 6c1b980

File tree

23 files changed

+910
-187
lines changed

23 files changed

+910
-187
lines changed

Documentation/admin-guide/kernel-parameters.txt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,7 @@
696696
kernel/dma/contiguous.c
697697

698698
cma_pernuma=nn[MG]
699-
[ARM64,KNL,CMA]
699+
[KNL,CMA]
700700
Sets the size of kernel per-numa memory area for
701701
contiguous memory allocations. A value of 0 disables
702702
per-numa CMA altogether. And If this option is not
@@ -706,6 +706,17 @@
706706
which is located in node nid, if the allocation fails,
707707
they will fallback to the global default memory area.
708708

709+
numa_cma=<node>:nn[MG][,<node>:nn[MG]]
710+
[KNL,CMA]
711+
Sets the size of kernel numa memory area for
712+
contiguous memory allocations. It will reserve CMA
713+
area for the specified node.
714+
715+
With numa CMA enabled, DMA users on node nid will
716+
first try to allocate buffer from the numa area
717+
which is located in node nid, if the allocation fails,
718+
they will fallback to the global default memory area.
719+
709720
cmo_free_hint= [PPC] Format: { yes | no }
710721
Specify whether pages are marked as being inactive
711722
when they are freed. This is used in CMO environments

arch/arm/xen/mm.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,10 @@ static int __init xen_mm_init(void)
125125
return 0;
126126

127127
/* we can work with the default swiotlb */
128-
if (!io_tlb_default_mem.nslabs) {
129-
rc = swiotlb_init_late(swiotlb_size_or_default(),
130-
xen_swiotlb_gfp(), NULL);
131-
if (rc < 0)
132-
return rc;
133-
}
128+
rc = swiotlb_init_late(swiotlb_size_or_default(),
129+
xen_swiotlb_gfp(), NULL);
130+
if (rc < 0)
131+
return rc;
134132

135133
cflush.op = 0;
136134
cflush.a.dev_bus_addr = 0;

arch/arm64/mm/init.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -461,8 +461,6 @@ void __init bootmem_init(void)
461461
arm64_hugetlb_cma_reserve();
462462
#endif
463463

464-
dma_pernuma_cma_reserve();
465-
466464
kvm_hyp_reserve();
467465

468466
/*

arch/mips/pci/pci-octeon.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@ static int __init octeon_pci_setup(void)
664664

665665
/* BAR1 movable regions contiguous to cover the swiotlb */
666666
octeon_bar1_pci_phys =
667-
io_tlb_default_mem.start & ~((1ull << 22) - 1);
667+
default_swiotlb_base() & ~((1ull << 22) - 1);
668668

669669
for (index = 0; index < 32; index++) {
670670
union cvmx_pci_bar1_indexx bar1_index;

arch/powerpc/kernel/dma-mask.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// SPDX-License-Identifier: GPL-2.0
22

33
#include <linux/dma-mapping.h>
4+
#include <linux/dma-map-ops.h>
45
#include <linux/export.h>
56
#include <asm/machdep.h>
67

arch/x86/include/asm/xen/swiotlb-xen.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,6 @@
22
#ifndef _ASM_X86_SWIOTLB_XEN_H
33
#define _ASM_X86_SWIOTLB_XEN_H
44

5-
#ifdef CONFIG_SWIOTLB_XEN
6-
extern int pci_xen_swiotlb_init_late(void);
7-
#else
8-
static inline int pci_xen_swiotlb_init_late(void) { return -ENXIO; }
9-
#endif
10-
115
int xen_swiotlb_fixup(void *buf, unsigned long nslabs);
126
int xen_create_contiguous_region(phys_addr_t pstart, unsigned int order,
137
unsigned int address_bits,

arch/x86/kernel/pci-dma.c

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,15 @@ static inline void __init pci_swiotlb_detect(void)
7272
#endif /* CONFIG_SWIOTLB */
7373

7474
#ifdef CONFIG_SWIOTLB_XEN
75+
static bool xen_swiotlb_enabled(void)
76+
{
77+
return xen_initial_domain() || x86_swiotlb_enable ||
78+
(IS_ENABLED(CONFIG_XEN_PCIDEV_FRONTEND) && xen_pv_pci_possible);
79+
}
80+
7581
static void __init pci_xen_swiotlb_init(void)
7682
{
77-
if (!xen_initial_domain() && !x86_swiotlb_enable)
83+
if (!xen_swiotlb_enabled())
7884
return;
7985
x86_swiotlb_enable = true;
8086
x86_swiotlb_flags |= SWIOTLB_ANY;
@@ -83,27 +89,6 @@ static void __init pci_xen_swiotlb_init(void)
8389
if (IS_ENABLED(CONFIG_PCI))
8490
pci_request_acs();
8591
}
86-
87-
int pci_xen_swiotlb_init_late(void)
88-
{
89-
if (dma_ops == &xen_swiotlb_dma_ops)
90-
return 0;
91-
92-
/* we can work with the default swiotlb */
93-
if (!io_tlb_default_mem.nslabs) {
94-
int rc = swiotlb_init_late(swiotlb_size_or_default(),
95-
GFP_KERNEL, xen_swiotlb_fixup);
96-
if (rc < 0)
97-
return rc;
98-
}
99-
100-
/* XXX: this switches the dma ops under live devices! */
101-
dma_ops = &xen_swiotlb_dma_ops;
102-
if (IS_ENABLED(CONFIG_PCI))
103-
pci_request_acs();
104-
return 0;
105-
}
106-
EXPORT_SYMBOL_GPL(pci_xen_swiotlb_init_late);
10792
#else
10893
static inline void __init pci_xen_swiotlb_init(void)
10994
{

arch/x86/xen/setup.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ struct xen_memory_region xen_extra_mem[XEN_EXTRA_MEM_MAX_REGIONS] __initdata;
4444
/* Number of pages released from the initial allocation. */
4545
unsigned long xen_released_pages;
4646

47+
/* Memory map would allow PCI passthrough. */
48+
bool xen_pv_pci_possible;
49+
4750
/* E820 map used during setting up memory. */
4851
static struct e820_table xen_e820_table __initdata;
4952

@@ -814,6 +817,9 @@ char * __init xen_memory_setup(void)
814817
chunk_size = size;
815818
type = xen_e820_table.entries[i].type;
816819

820+
if (type == E820_TYPE_RESERVED)
821+
xen_pv_pci_possible = true;
822+
817823
if (type == E820_TYPE_RAM) {
818824
if (addr < mem_end) {
819825
chunk_size = min(size, mem_end - addr);

drivers/base/core.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3108,9 +3108,7 @@ void device_initialize(struct device *dev)
31083108
defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL)
31093109
dev->dma_coherent = dma_default_coherent;
31103110
#endif
3111-
#ifdef CONFIG_SWIOTLB
3112-
dev->dma_io_tlb_mem = &io_tlb_default_mem;
3113-
#endif
3111+
swiotlb_dev_init(dev);
31143112
}
31153113
EXPORT_SYMBOL_GPL(device_initialize);
31163114

drivers/pci/xen-pcifront.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#include <linux/bitops.h>
2323
#include <linux/time.h>
2424
#include <linux/ktime.h>
25-
#include <linux/swiotlb.h>
2625
#include <xen/platform_pci.h>
2726

2827
#include <asm/xen/swiotlb-xen.h>
@@ -669,11 +668,6 @@ static int pcifront_connect_and_init_dma(struct pcifront_device *pdev)
669668

670669
spin_unlock(&pcifront_dev_lock);
671670

672-
if (!err && !is_swiotlb_active(&pdev->xdev->dev)) {
673-
err = pci_xen_swiotlb_init_late();
674-
if (err)
675-
dev_err(&pdev->xdev->dev, "Could not setup SWIOTLB!\n");
676-
}
677671
return err;
678672
}
679673

drivers/xen/swiotlb-xen.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ xen_swiotlb_sync_sg_for_device(struct device *dev, struct scatterlist *sgl,
381381
static int
382382
xen_swiotlb_dma_supported(struct device *hwdev, u64 mask)
383383
{
384-
return xen_phys_to_dma(hwdev, io_tlb_default_mem.end - 1) <= mask;
384+
return xen_phys_to_dma(hwdev, default_swiotlb_limit()) <= mask;
385385
}
386386

387387
const struct dma_map_ops xen_swiotlb_dma_ops = {

include/linux/device.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,10 @@ struct device_physical_location {
625625
* @dma_pools: Dma pools (if dma'ble device).
626626
* @dma_mem: Internal for coherent mem override.
627627
* @cma_area: Contiguous memory area for dma allocations
628-
* @dma_io_tlb_mem: Pointer to the swiotlb pool used. Not for driver use.
628+
* @dma_io_tlb_mem: Software IO TLB allocator. Not for driver use.
629+
* @dma_io_tlb_pools: List of transient swiotlb memory pools.
630+
* @dma_io_tlb_lock: Protects changes to the list of active pools.
631+
* @dma_uses_io_tlb: %true if device has used the software IO TLB.
629632
* @archdata: For arch-specific additions.
630633
* @of_node: Associated device tree node.
631634
* @fwnode: Associated device node supplied by platform firmware.
@@ -731,6 +734,11 @@ struct device {
731734
#endif
732735
#ifdef CONFIG_SWIOTLB
733736
struct io_tlb_mem *dma_io_tlb_mem;
737+
#endif
738+
#ifdef CONFIG_SWIOTLB_DYNAMIC
739+
struct list_head dma_io_tlb_pools;
740+
spinlock_t dma_io_tlb_lock;
741+
bool dma_uses_io_tlb;
734742
#endif
735743
/* arch specific additions */
736744
struct dev_archdata archdata;

include/linux/dma-map-ops.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,6 @@ static inline void dma_free_contiguous(struct device *dev, struct page *page,
169169
}
170170
#endif /* CONFIG_DMA_CMA*/
171171

172-
#ifdef CONFIG_DMA_PERNUMA_CMA
173-
void dma_pernuma_cma_reserve(void);
174-
#else
175-
static inline void dma_pernuma_cma_reserve(void) { }
176-
#endif /* CONFIG_DMA_PERNUMA_CMA */
177-
178172
#ifdef CONFIG_DMA_DECLARE_COHERENT
179173
int dma_declare_coherent_memory(struct device *dev, phys_addr_t phys_addr,
180174
dma_addr_t device_addr, size_t size);
@@ -343,6 +337,12 @@ void *arch_dma_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle,
343337
void arch_dma_free(struct device *dev, size_t size, void *cpu_addr,
344338
dma_addr_t dma_addr, unsigned long attrs);
345339

340+
#ifdef CONFIG_ARCH_HAS_DMA_SET_MASK
341+
void arch_dma_set_mask(struct device *dev, u64 mask);
342+
#else
343+
#define arch_dma_set_mask(dev, mask) do { } while (0)
344+
#endif
345+
346346
#ifdef CONFIG_MMU
347347
/*
348348
* Page protection so that devices that can't snoop CPU caches can use the

include/linux/dma-mapping.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,8 @@ static inline void dma_sync_sgtable_for_device(struct device *dev,
418418
#define dma_get_sgtable(d, t, v, h, s) dma_get_sgtable_attrs(d, t, v, h, s, 0)
419419
#define dma_mmap_coherent(d, v, c, h, s) dma_mmap_attrs(d, v, c, h, s, 0)
420420

421+
bool dma_coherent_ok(struct device *dev, phys_addr_t phys, size_t size);
422+
421423
static inline void *dma_alloc_coherent(struct device *dev, size_t size,
422424
dma_addr_t *dma_handle, gfp_t gfp)
423425
{

0 commit comments

Comments
 (0)