dma-debug: Check for drivers mapping invalid addresses in dma_map_single()
PATCH v2 1/1 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.
---
Changes from v1:
* Update code to check for invalid virtual address too
* Rename function to debug_dma_map_single()
include/linux/dma-debug.h | 8 ++++++++ include/linux/dma-mapping.h | 1 + kernel/dma/debug.c | 16 ++++++++++++++++ 3 files changed, 25 insertions(+)
include/linux/dma-debug.h +8 -0
@@ -32,6 +32,9 @@ extern void dma_debug_add_bus(struct bus_type *bus);extern int dma_debug_resize_entries (u32 num_entries); +extern void debug_dma_map_single (struct device *dev,const void *addr, +unsigned long len); + extern void debug_dma_map_page(struct device *dev, struct page *page, size_t offset, size_t size, int direction, dma_addr_t dma_addr,
@@ -103,6 +106,11 @@ static inline int dma_debug_resize_entries(u32 num_entries)return 0 ; } +static inline void debug_dma_map_single (struct device *dev,const void *addr, +unsigned long len) +{ +} + static inline void debug_dma_map_page(struct device *dev, struct page *page, size_t offset, size_t size, int direction, dma_addr_t dma_addr,
include/linux/dma-mapping.h +1 -0
@@ -231,6 +231,7 @@ static inline dma_addr_t dma_map_single_attrs(struct device *dev, void *ptr,dma_addr_t addr;BUG_ON (!valid_dma_direction (dir)); +debug_dma_check_single (dev, ptr, size); addr = ops->map_page (dev,virt_to_page (ptr),offset_in_page (ptr), size, dir, attrs);
kernel/dma/debug.c +16 -0
@@ -1312,6 +1312,22 @@ static void check_sg_segment(struct device *dev, struct scatterlist *sg)#endif } +void debug_dma_check_single (struct device *dev,const void *addr, +unsigned long len) +{ +if (unlikely (dma_debug_disabled ())) +return ; + +if (!virt_addr_valid (addr)) +err_printk (dev,NULL ,"DMA-API: device driver maps memory from invalid area [addr=%p] [len=%lu]\n" , + addr, len); + +if (is_vmalloc_addr (addr)) +err_printk (dev,NULL ,"DMA-API: device driver maps memory from vmalloc area [addr=%p] [len=%lu]\n" , + addr, len); +} +EXPORT_SYMBOL (debug_dma_check_single); +void debug_dma_map_page (struct device *dev,struct page *page,size_t offset,size_t size,int direction,dma_addr_t dma_addr,bool map_single)
Signature · 1 line
Sent by a computer through tubes
Quoted 58 lines · Stephen Boyd
I recently debugged a DMA mapping oops where a driver was trying to map a buffer returned from
request_firmware()withdma_map_single(). Memory returned fromrequest_firmware()is mapped into the vmalloc region and this isn't a valid region to map withdma_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 <[email protected]>Cc Robin Murphy <[email protected]>Signed-off-by Stephen Boyd <[email protected]>---
Changes from v1:
* Update code to check for invalid virtual address too
* Rename function todebug_dma_map_single()include/linux/dma-debug.h | 8 ++++++++ include/linux/dma-mapping.h | 1 + kernel/dma/debug.c | 16 ++++++++++++++++ 3 files changed, 25 insertions(+)include/linux/dma-debug.h +8
-0@@ -32,6 +32,9 @@ extern void dma_debug_add_bus(struct bus_type *bus);extern int dma_debug_resize_entries (u32 num_entries); +extern void debug_dma_map_single (struct device *dev,const void *addr, +unsigned long len); + extern void debug_dma_map_page(struct device *dev, struct page *page, size_t offset, size_t size, int direction, dma_addr_t dma_addr,@@ -103,6 +106,11 @@ static inline int dma_debug_resize_entries(u32 num_entries)return 0 ; } +static inline void debug_dma_map_single (struct device *dev,const void *addr, +unsigned long len) +{ +} + static inline void debug_dma_map_page(struct device *dev, struct page *page, size_t offset, size_t size, int direction, dma_addr_t dma_addr,include/linux/dma-mapping.h +0
-0
include/linux/dma-mapping.h static inline dma_addr_t dma_map_single_attrs(struct device *dev, void *ptr, · line 234
Quoted 4 lines
include/linux/dma-mapping.h +1
-0@@ -231,6 +231,7 @@ static inline dma_addr_t dma_map_single_attrs(struct device *dev, void *ptr,dma_addr_t addr;BUG_ON (!valid_dma_direction (dir)); +debug_dma_check_single (dev, ptr, size);
Ahem...
With that (and below) fixed so that it actually compiles,
Quoted 31 lines
include/linux/dma-mapping.h +0
-0addr = ops->map_page (dev,virt_to_page (ptr),offset_in_page (ptr), size, dir, attrs);kernel/dma/debug.c +16
-0@@ -1312,6 +1312,22 @@ static void check_sg_segment(struct device *dev, struct scatterlist *sg)#endif } +void debug_dma_check_single (struct device *dev,const void *addr, +unsigned long len) +{ +if (unlikely (dma_debug_disabled ())) +return ; + +if (!virt_addr_valid (addr)) +err_printk (dev,NULL ,"DMA-API: device driver maps memory from invalid area [addr=%p] [len=%lu]\n" , + addr, len); + +if (is_vmalloc_addr (addr)) +err_printk (dev,NULL ,"DMA-API: device driver maps memory from vmalloc area [addr=%p] [len=%lu]\n" , + addr, len); +} +EXPORT_SYMBOL (debug_dma_check_single); +void debug_dma_map_page (struct device *dev,struct page *page,size_t offset,size_t size,int direction,dma_addr_t dma_addr,bool map_single)
Quoting Robin Murphy (2018-10-02 07:01:42)
Quoted 8 lines · Stephen Boyd
Quoted 7 lines
include/linux/dma-mapping.h +0
-0@@ -231,6 +231,7 @@ static inline dma_addr_t dma_map_single_attrs(struct device *dev, void *ptr,dma_addr_t addr;
Quoted 8 lines
include/linux/dma-mapping.h static inline dma_addr_t dma_map_single_attrs(struct device *dev, void *ptr, · line 234
Quoted 2 lines
include/linux/dma-mapping.h +1
-0BUG_ON (!valid_dma_direction (dir)); +debug_dma_check_single (dev, ptr, size);Ahem...
With that (and below) fixed so that it actually compiles,
Reviewed-by Robin Murphy <[email protected]>
Thanks for checking! ;)
Quoted 3 lines · Stephen Boyd
Quoted 1 line
Reviewed-by Robin Murphy <[email protected]>Thanks for checking! ;)
Can you resend the fixed version to make my life easier?