Skip to content

Commit 99c65fa

Browse files
bebarinoChristoph Hellwig
authored andcommitted
dma-debug: Check for drivers mapping invalid addresses in dma_map_single()
I recently debugged a DMA mapping oops where a driver was trying to map a buffer returned from request_firmware() with dma_map_single(). Memory returned from request_firmware() is mapped into the vmalloc region and this isn't a valid region to map with dma_map_single() per the DMA documentation's "What memory is DMA'able?" section. Unfortunately, we don't really check that in the DMA debugging code, so enabling DMA debugging doesn't help catch this problem. Let's add a new DMA debug function to check for a vmalloc address or an invalid virtual address and print a warning if this happens. This makes it a little easier to debug these sorts of problems, instead of seeing odd behavior or crashes when drivers attempt to map the vmalloc space for DMA. Cc: Marek Szyprowski <m.szyprowski@samsung.com> Reviewed-by: Robin Murphy <robin.murphy@arm.com> Signed-off-by: Stephen Boyd <swboyd@chromium.org> Signed-off-by: Christoph Hellwig <hch@lst.de>
1 parent 1fc8e64 commit 99c65fa

3 files changed

Lines changed: 25 additions & 0 deletions

File tree

‎include/linux/dma-debug.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ extern void dma_debug_add_bus(struct bus_type *bus);
3232

3333
extern int dma_debug_resize_entries(u32 num_entries);
3434

35+
extern void debug_dma_map_single(struct device *dev, const void *addr,
36+
unsigned long len);
37+
3538
extern void debug_dma_map_page(struct device *dev, struct page *page,
3639
size_t offset, size_t size,
3740
int direction, dma_addr_t dma_addr,
@@ -103,6 +106,11 @@ static inline int dma_debug_resize_entries(u32 num_entries)
103106
return 0;
104107
}
105108

109+
static inline void debug_dma_map_single(struct device *dev, const void *addr,
110+
unsigned long len)
111+
{
112+
}
113+
106114
static inline void debug_dma_map_page(struct device *dev, struct page *page,
107115
size_t offset, size_t size,
108116
int direction, dma_addr_t dma_addr,

‎include/linux/dma-mapping.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ static inline dma_addr_t dma_map_single_attrs(struct device *dev, void *ptr,
229229
dma_addr_t addr;
230230

231231
BUG_ON(!valid_dma_direction(dir));
232+
debug_dma_map_single(dev, ptr, size);
232233
addr = ops->map_page(dev, virt_to_page(ptr),
233234
offset_in_page(ptr), size,
234235
dir, attrs);

‎kernel/dma/debug.c‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,6 +1312,22 @@ static void check_sg_segment(struct device *dev, struct scatterlist *sg)
13121312
#endif
13131313
}
13141314

1315+
void debug_dma_map_single(struct device *dev, const void *addr,
1316+
unsigned long len)
1317+
{
1318+
if (unlikely(dma_debug_disabled()))
1319+
return;
1320+
1321+
if (!virt_addr_valid(addr))
1322+
err_printk(dev, NULL, "DMA-API: device driver maps memory from invalid area [addr=%p] [len=%lu]\n",
1323+
addr, len);
1324+
1325+
if (is_vmalloc_addr(addr))
1326+
err_printk(dev, NULL, "DMA-API: device driver maps memory from vmalloc area [addr=%p] [len=%lu]\n",
1327+
addr, len);
1328+
}
1329+
EXPORT_SYMBOL(debug_dma_map_single);
1330+
13151331
void debug_dma_map_page(struct device *dev, struct page *page, size_t offset,
13161332
size_t size, int direction, dma_addr_t dma_addr,
13171333
bool map_single)

0 commit comments

Comments
 (0)