Skip to content

Commit a498d59

Browse files
committed
Merge tag 'dma-mapping-6.18-2025-09-30' of git://git.kernel.org/pub/scm/linux/kernel/git/mszyprowski/linux
Pull dma-mapping updates from Marek Szyprowski: - Refactoring of DMA mapping API to physical addresses as the primary interface instead of page+offset parameters This gets much closer to Matthew Wilcox's long term wish for struct-pageless IO to cacheable DRAM and is supporting memdesc project which seeks to substantially transform how struct page works. An advantage of this approach is the possibility of introducing DMA_ATTR_MMIO, which covers existing 'dma_map_resource' flow in the common paths, what in turn lets to use recently introduced dma_iova_link() API to map PCI P2P MMIO without creating struct page Developped by Leon Romanovsky and Jason Gunthorpe - Minor clean-up by Petr Tesarik and Qianfeng Rong * tag 'dma-mapping-6.18-2025-09-30' of git://git.kernel.org/pub/scm/linux/kernel/git/mszyprowski/linux: kmsan: fix missed kmsan_handle_dma() signature conversion mm/hmm: properly take MMIO path mm/hmm: migrate to physical address-based DMA mapping API dma-mapping: export new dma_*map_phys() interface xen: swiotlb: Open code map_resource callback dma-mapping: implement DMA_ATTR_MMIO for dma_(un)map_page_attrs() kmsan: convert kmsan_handle_dma to use physical addresses dma-mapping: convert dma_direct_*map_page to be phys_addr_t based iommu/dma: implement DMA_ATTR_MMIO for iommu_dma_(un)map_phys() iommu/dma: rename iommu_dma_*map_page to iommu_dma_*map_phys dma-mapping: rename trace_dma_*map_page to trace_dma_*map_phys dma-debug: refactor to use physical addresses for page mapping iommu/dma: implement DMA_ATTR_MMIO for dma_iova_link(). dma-mapping: introduce new DMA attribute to indicate MMIO memory swiotlb: Remove redundant __GFP_NOWARN dma-direct: clean up the logic in __dma_direct_alloc_pages()
2 parents ee2fe81 + ef3d979 commit a498d59

File tree

24 files changed

+295
-276
lines changed

24 files changed

+295
-276
lines changed

Documentation/core-api/dma-api.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,7 @@ example warning message may look like this::
761761
[<ffffffff80235177>] find_busiest_group+0x207/0x8a0
762762
[<ffffffff8064784f>] _spin_lock_irqsave+0x1f/0x50
763763
[<ffffffff803c7ea3>] check_unmap+0x203/0x490
764-
[<ffffffff803c8259>] debug_dma_unmap_page+0x49/0x50
764+
[<ffffffff803c8259>] debug_dma_unmap_phys+0x49/0x50
765765
[<ffffffff80485f26>] nv_tx_done_optimized+0xc6/0x2c0
766766
[<ffffffff80486c13>] nv_nic_irq_optimized+0x73/0x2b0
767767
[<ffffffff8026df84>] handle_IRQ_event+0x34/0x70
@@ -855,7 +855,7 @@ that a driver may be leaking mappings.
855855
dma-debug interface debug_dma_mapping_error() to debug drivers that fail
856856
to check DMA mapping errors on addresses returned by dma_map_single() and
857857
dma_map_page() interfaces. This interface clears a flag set by
858-
debug_dma_map_page() to indicate that dma_mapping_error() has been called by
858+
debug_dma_map_phys() to indicate that dma_mapping_error() has been called by
859859
the driver. When driver does unmap, debug_dma_unmap() checks the flag and if
860860
this flag is still set, prints warning message that includes call trace that
861861
leads up to the unmap. This interface can be called from dma_mapping_error()

Documentation/core-api/dma-attributes.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,21 @@ accesses to DMA buffers in both privileged "supervisor" and unprivileged
130130
subsystem that the buffer is fully accessible at the elevated privilege
131131
level (and ideally inaccessible or at least read-only at the
132132
lesser-privileged levels).
133+
134+
DMA_ATTR_MMIO
135+
-------------
136+
137+
This attribute indicates the physical address is not normal system
138+
memory. It may not be used with kmap*()/phys_to_virt()/phys_to_page()
139+
functions, it may not be cacheable, and access using CPU load/store
140+
instructions may not be allowed.
141+
142+
Usually this will be used to describe MMIO addresses, or other non-cacheable
143+
register addresses. When DMA mapping this sort of address we call
144+
the operation Peer to Peer as a one device is DMA'ing to another device.
145+
For PCI devices the p2pdma APIs must be used to determine if
146+
DMA_ATTR_MMIO is appropriate.
147+
148+
For architectures that require cache flushing for DMA coherence
149+
DMA_ATTR_MMIO will not perform any cache flushing. The address
150+
provided must never be mapped cacheable into the CPU.

arch/powerpc/kernel/dma-iommu.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#define can_map_direct(dev, addr) \
1515
((dev)->bus_dma_limit >= phys_to_dma((dev), (addr)))
1616

17-
bool arch_dma_map_page_direct(struct device *dev, phys_addr_t addr)
17+
bool arch_dma_map_phys_direct(struct device *dev, phys_addr_t addr)
1818
{
1919
if (likely(!dev->bus_dma_limit))
2020
return false;
@@ -24,7 +24,7 @@ bool arch_dma_map_page_direct(struct device *dev, phys_addr_t addr)
2424

2525
#define is_direct_handle(dev, h) ((h) >= (dev)->archdata.dma_offset)
2626

27-
bool arch_dma_unmap_page_direct(struct device *dev, dma_addr_t dma_handle)
27+
bool arch_dma_unmap_phys_direct(struct device *dev, dma_addr_t dma_handle)
2828
{
2929
if (likely(!dev->bus_dma_limit))
3030
return false;

drivers/iommu/dma-iommu.c

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,12 @@ static int iommu_dma_init_domain(struct iommu_domain *domain, struct device *dev
724724
static int dma_info_to_prot(enum dma_data_direction dir, bool coherent,
725725
unsigned long attrs)
726726
{
727-
int prot = coherent ? IOMMU_CACHE : 0;
727+
int prot;
728+
729+
if (attrs & DMA_ATTR_MMIO)
730+
prot = IOMMU_MMIO;
731+
else
732+
prot = coherent ? IOMMU_CACHE : 0;
728733

729734
if (attrs & DMA_ATTR_PRIVILEGED)
730735
prot |= IOMMU_PRIV;
@@ -1190,11 +1195,9 @@ static inline size_t iova_unaligned(struct iova_domain *iovad, phys_addr_t phys,
11901195
return iova_offset(iovad, phys | size);
11911196
}
11921197

1193-
dma_addr_t iommu_dma_map_page(struct device *dev, struct page *page,
1194-
unsigned long offset, size_t size, enum dma_data_direction dir,
1195-
unsigned long attrs)
1198+
dma_addr_t iommu_dma_map_phys(struct device *dev, phys_addr_t phys, size_t size,
1199+
enum dma_data_direction dir, unsigned long attrs)
11961200
{
1197-
phys_addr_t phys = page_to_phys(page) + offset;
11981201
bool coherent = dev_is_dma_coherent(dev);
11991202
int prot = dma_info_to_prot(dir, coherent, attrs);
12001203
struct iommu_domain *domain = iommu_get_dma_domain(dev);
@@ -1208,27 +1211,34 @@ dma_addr_t iommu_dma_map_page(struct device *dev, struct page *page,
12081211
*/
12091212
if (dev_use_swiotlb(dev, size, dir) &&
12101213
iova_unaligned(iovad, phys, size)) {
1214+
if (attrs & DMA_ATTR_MMIO)
1215+
return DMA_MAPPING_ERROR;
1216+
12111217
phys = iommu_dma_map_swiotlb(dev, phys, size, dir, attrs);
12121218
if (phys == (phys_addr_t)DMA_MAPPING_ERROR)
12131219
return DMA_MAPPING_ERROR;
12141220
}
12151221

1216-
if (!coherent && !(attrs & DMA_ATTR_SKIP_CPU_SYNC))
1222+
if (!coherent && !(attrs & (DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_MMIO)))
12171223
arch_sync_dma_for_device(phys, size, dir);
12181224

12191225
iova = __iommu_dma_map(dev, phys, size, prot, dma_mask);
1220-
if (iova == DMA_MAPPING_ERROR)
1226+
if (iova == DMA_MAPPING_ERROR && !(attrs & DMA_ATTR_MMIO))
12211227
swiotlb_tbl_unmap_single(dev, phys, size, dir, attrs);
12221228
return iova;
12231229
}
12241230

1225-
void iommu_dma_unmap_page(struct device *dev, dma_addr_t dma_handle,
1231+
void iommu_dma_unmap_phys(struct device *dev, dma_addr_t dma_handle,
12261232
size_t size, enum dma_data_direction dir, unsigned long attrs)
12271233
{
1228-
struct iommu_domain *domain = iommu_get_dma_domain(dev);
12291234
phys_addr_t phys;
12301235

1231-
phys = iommu_iova_to_phys(domain, dma_handle);
1236+
if (attrs & DMA_ATTR_MMIO) {
1237+
__iommu_dma_unmap(dev, dma_handle, size);
1238+
return;
1239+
}
1240+
1241+
phys = iommu_iova_to_phys(iommu_get_dma_domain(dev), dma_handle);
12321242
if (WARN_ON(!phys))
12331243
return;
12341244

@@ -1341,7 +1351,7 @@ static void iommu_dma_unmap_sg_swiotlb(struct device *dev, struct scatterlist *s
13411351
int i;
13421352

13431353
for_each_sg(sg, s, nents, i)
1344-
iommu_dma_unmap_page(dev, sg_dma_address(s),
1354+
iommu_dma_unmap_phys(dev, sg_dma_address(s),
13451355
sg_dma_len(s), dir, attrs);
13461356
}
13471357

@@ -1354,8 +1364,8 @@ static int iommu_dma_map_sg_swiotlb(struct device *dev, struct scatterlist *sg,
13541364
sg_dma_mark_swiotlb(sg);
13551365

13561366
for_each_sg(sg, s, nents, i) {
1357-
sg_dma_address(s) = iommu_dma_map_page(dev, sg_page(s),
1358-
s->offset, s->length, dir, attrs);
1367+
sg_dma_address(s) = iommu_dma_map_phys(dev, sg_phys(s),
1368+
s->length, dir, attrs);
13591369
if (sg_dma_address(s) == DMA_MAPPING_ERROR)
13601370
goto out_unmap;
13611371
sg_dma_len(s) = s->length;
@@ -1546,20 +1556,6 @@ void iommu_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
15461556
__iommu_dma_unmap(dev, start, end - start);
15471557
}
15481558

1549-
dma_addr_t iommu_dma_map_resource(struct device *dev, phys_addr_t phys,
1550-
size_t size, enum dma_data_direction dir, unsigned long attrs)
1551-
{
1552-
return __iommu_dma_map(dev, phys, size,
1553-
dma_info_to_prot(dir, false, attrs) | IOMMU_MMIO,
1554-
dma_get_mask(dev));
1555-
}
1556-
1557-
void iommu_dma_unmap_resource(struct device *dev, dma_addr_t handle,
1558-
size_t size, enum dma_data_direction dir, unsigned long attrs)
1559-
{
1560-
__iommu_dma_unmap(dev, handle, size);
1561-
}
1562-
15631559
static void __iommu_dma_free(struct device *dev, size_t size, void *cpu_addr)
15641560
{
15651561
size_t alloc_size = PAGE_ALIGN(size);
@@ -1838,12 +1834,13 @@ static int __dma_iova_link(struct device *dev, dma_addr_t addr,
18381834
unsigned long attrs)
18391835
{
18401836
bool coherent = dev_is_dma_coherent(dev);
1837+
int prot = dma_info_to_prot(dir, coherent, attrs);
18411838

1842-
if (!coherent && !(attrs & DMA_ATTR_SKIP_CPU_SYNC))
1839+
if (!coherent && !(attrs & (DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_MMIO)))
18431840
arch_sync_dma_for_device(phys, size, dir);
18441841

18451842
return iommu_map_nosync(iommu_get_dma_domain(dev), addr, phys, size,
1846-
dma_info_to_prot(dir, coherent, attrs), GFP_ATOMIC);
1843+
prot, GFP_ATOMIC);
18471844
}
18481845

18491846
static int iommu_dma_iova_bounce_and_link(struct device *dev, dma_addr_t addr,
@@ -1949,9 +1946,13 @@ int dma_iova_link(struct device *dev, struct dma_iova_state *state,
19491946
return -EIO;
19501947

19511948
if (dev_use_swiotlb(dev, size, dir) &&
1952-
iova_unaligned(iovad, phys, size))
1949+
iova_unaligned(iovad, phys, size)) {
1950+
if (attrs & DMA_ATTR_MMIO)
1951+
return -EPERM;
1952+
19531953
return iommu_dma_iova_link_swiotlb(dev, state, phys, offset,
19541954
size, dir, attrs);
1955+
}
19551956

19561957
return __dma_iova_link(dev, state->addr + offset - iova_start_pad,
19571958
phys - iova_start_pad,

drivers/virtio/virtio_ring.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ static int vring_map_one_sg(const struct vring_virtqueue *vq, struct scatterlist
378378
* is initialized by the hardware. Explicitly check/unpoison it
379379
* depending on the direction.
380380
*/
381-
kmsan_handle_dma(sg_page(sg), sg->offset, sg->length, direction);
381+
kmsan_handle_dma(sg_phys(sg), sg->length, direction);
382382
*addr = (dma_addr_t)sg_phys(sg);
383383
return 0;
384384
}
@@ -3157,7 +3157,7 @@ dma_addr_t virtqueue_dma_map_single_attrs(struct virtqueue *_vq, void *ptr,
31573157
struct vring_virtqueue *vq = to_vvq(_vq);
31583158

31593159
if (!vq->use_dma_api) {
3160-
kmsan_handle_dma(virt_to_page(ptr), offset_in_page(ptr), size, dir);
3160+
kmsan_handle_dma(virt_to_phys(ptr), size, dir);
31613161
return (dma_addr_t)virt_to_phys(ptr);
31623162
}
31633163

drivers/xen/swiotlb-xen.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,25 @@ xen_swiotlb_sync_sg_for_device(struct device *dev, struct scatterlist *sgl,
392392
}
393393
}
394394

395+
static dma_addr_t xen_swiotlb_direct_map_resource(struct device *dev,
396+
phys_addr_t paddr,
397+
size_t size,
398+
enum dma_data_direction dir,
399+
unsigned long attrs)
400+
{
401+
dma_addr_t dma_addr = paddr;
402+
403+
if (unlikely(!dma_capable(dev, dma_addr, size, false))) {
404+
dev_err_once(dev,
405+
"DMA addr %pad+%zu overflow (mask %llx, bus limit %llx).\n",
406+
&dma_addr, size, *dev->dma_mask, dev->bus_dma_limit);
407+
WARN_ON_ONCE(1);
408+
return DMA_MAPPING_ERROR;
409+
}
410+
411+
return dma_addr;
412+
}
413+
395414
/*
396415
* Return whether the given device DMA address mask can be supported
397416
* properly. For example, if your device can only drive the low 24-bits
@@ -426,5 +445,5 @@ const struct dma_map_ops xen_swiotlb_dma_ops = {
426445
.alloc_pages_op = dma_common_alloc_pages,
427446
.free_pages = dma_common_free_pages,
428447
.max_mapping_size = swiotlb_max_mapping_size,
429-
.map_resource = dma_direct_map_resource,
448+
.map_resource = xen_swiotlb_direct_map_resource,
430449
};

include/linux/dma-direct.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,5 @@ void dma_direct_free_pages(struct device *dev, size_t size,
149149
struct page *page, dma_addr_t dma_addr,
150150
enum dma_data_direction dir);
151151
int dma_direct_supported(struct device *dev, u64 mask);
152-
dma_addr_t dma_direct_map_resource(struct device *dev, phys_addr_t paddr,
153-
size_t size, enum dma_data_direction dir, unsigned long attrs);
154152

155153
#endif /* _LINUX_DMA_DIRECT_H */

include/linux/dma-map-ops.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -395,15 +395,15 @@ void *arch_dma_set_uncached(void *addr, size_t size);
395395
void arch_dma_clear_uncached(void *addr, size_t size);
396396

397397
#ifdef CONFIG_ARCH_HAS_DMA_MAP_DIRECT
398-
bool arch_dma_map_page_direct(struct device *dev, phys_addr_t addr);
399-
bool arch_dma_unmap_page_direct(struct device *dev, dma_addr_t dma_handle);
398+
bool arch_dma_map_phys_direct(struct device *dev, phys_addr_t addr);
399+
bool arch_dma_unmap_phys_direct(struct device *dev, dma_addr_t dma_handle);
400400
bool arch_dma_map_sg_direct(struct device *dev, struct scatterlist *sg,
401401
int nents);
402402
bool arch_dma_unmap_sg_direct(struct device *dev, struct scatterlist *sg,
403403
int nents);
404404
#else
405-
#define arch_dma_map_page_direct(d, a) (false)
406-
#define arch_dma_unmap_page_direct(d, a) (false)
405+
#define arch_dma_map_phys_direct(d, a) (false)
406+
#define arch_dma_unmap_phys_direct(d, a) (false)
407407
#define arch_dma_map_sg_direct(d, s, n) (false)
408408
#define arch_dma_unmap_sg_direct(d, s, n) (false)
409409
#endif

include/linux/dma-mapping.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,26 @@
5858
*/
5959
#define DMA_ATTR_PRIVILEGED (1UL << 9)
6060

61+
/*
62+
* DMA_ATTR_MMIO - Indicates memory-mapped I/O (MMIO) region for DMA mapping
63+
*
64+
* This attribute indicates the physical address is not normal system
65+
* memory. It may not be used with kmap*()/phys_to_virt()/phys_to_page()
66+
* functions, it may not be cacheable, and access using CPU load/store
67+
* instructions may not be allowed.
68+
*
69+
* Usually this will be used to describe MMIO addresses, or other non-cacheable
70+
* register addresses. When DMA mapping this sort of address we call
71+
* the operation Peer to Peer as a one device is DMA'ing to another device.
72+
* For PCI devices the p2pdma APIs must be used to determine if DMA_ATTR_MMIO
73+
* is appropriate.
74+
*
75+
* For architectures that require cache flushing for DMA coherence
76+
* DMA_ATTR_MMIO will not perform any cache flushing. The address
77+
* provided must never be mapped cacheable into the CPU.
78+
*/
79+
#define DMA_ATTR_MMIO (1UL << 10)
80+
6181
/*
6282
* A dma_addr_t can hold any valid DMA or bus address for the platform. It can
6383
* be given to a device to use as a DMA source or target. It is specific to a
@@ -118,6 +138,10 @@ dma_addr_t dma_map_page_attrs(struct device *dev, struct page *page,
118138
unsigned long attrs);
119139
void dma_unmap_page_attrs(struct device *dev, dma_addr_t addr, size_t size,
120140
enum dma_data_direction dir, unsigned long attrs);
141+
dma_addr_t dma_map_phys(struct device *dev, phys_addr_t phys, size_t size,
142+
enum dma_data_direction dir, unsigned long attrs);
143+
void dma_unmap_phys(struct device *dev, dma_addr_t addr, size_t size,
144+
enum dma_data_direction dir, unsigned long attrs);
121145
unsigned int dma_map_sg_attrs(struct device *dev, struct scatterlist *sg,
122146
int nents, enum dma_data_direction dir, unsigned long attrs);
123147
void dma_unmap_sg_attrs(struct device *dev, struct scatterlist *sg,
@@ -172,6 +196,15 @@ static inline void dma_unmap_page_attrs(struct device *dev, dma_addr_t addr,
172196
size_t size, enum dma_data_direction dir, unsigned long attrs)
173197
{
174198
}
199+
static inline dma_addr_t dma_map_phys(struct device *dev, phys_addr_t phys,
200+
size_t size, enum dma_data_direction dir, unsigned long attrs)
201+
{
202+
return DMA_MAPPING_ERROR;
203+
}
204+
static inline void dma_unmap_phys(struct device *dev, dma_addr_t addr,
205+
size_t size, enum dma_data_direction dir, unsigned long attrs)
206+
{
207+
}
175208
static inline unsigned int dma_map_sg_attrs(struct device *dev,
176209
struct scatterlist *sg, int nents, enum dma_data_direction dir,
177210
unsigned long attrs)

include/linux/iommu-dma.h

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,9 @@ static inline bool use_dma_iommu(struct device *dev)
2121
}
2222
#endif /* CONFIG_IOMMU_DMA */
2323

24-
dma_addr_t iommu_dma_map_page(struct device *dev, struct page *page,
25-
unsigned long offset, size_t size, enum dma_data_direction dir,
26-
unsigned long attrs);
27-
void iommu_dma_unmap_page(struct device *dev, dma_addr_t dma_handle,
24+
dma_addr_t iommu_dma_map_phys(struct device *dev, phys_addr_t phys, size_t size,
25+
enum dma_data_direction dir, unsigned long attrs);
26+
void iommu_dma_unmap_phys(struct device *dev, dma_addr_t dma_handle,
2827
size_t size, enum dma_data_direction dir, unsigned long attrs);
2928
int iommu_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
3029
enum dma_data_direction dir, unsigned long attrs);
@@ -43,10 +42,6 @@ size_t iommu_dma_opt_mapping_size(void);
4342
size_t iommu_dma_max_mapping_size(struct device *dev);
4443
void iommu_dma_free(struct device *dev, size_t size, void *cpu_addr,
4544
dma_addr_t handle, unsigned long attrs);
46-
dma_addr_t iommu_dma_map_resource(struct device *dev, phys_addr_t phys,
47-
size_t size, enum dma_data_direction dir, unsigned long attrs);
48-
void iommu_dma_unmap_resource(struct device *dev, dma_addr_t handle,
49-
size_t size, enum dma_data_direction dir, unsigned long attrs);
5045
struct sg_table *iommu_dma_alloc_noncontiguous(struct device *dev, size_t size,
5146
enum dma_data_direction dir, gfp_t gfp, unsigned long attrs);
5247
void iommu_dma_free_noncontiguous(struct device *dev, size_t size,

0 commit comments

Comments
 (0)