Commit f39d1b97 authored by Ingo Molnar's avatar Ingo Molnar

dma-debug: Fix the overlap() function to be correct and readable

Linus noticed how unclean and buggy the overlap() function is:

 - It uses convoluted (and bug-causing) positive checks for
   range overlap - instead of using a more natural negative
   check.

 - Even the positive checks are buggy: a positive intersection
   check has four natural cases while we checked only for three,
   missing the (addr < start && addr2 == end) case for example.

 - The variables are mis-named, making it non-obvious how the
   check was done.

 - It needlessly uses u64 instead of unsigned long. Since these
   are kernel memory pointers and we explicitly exclude highmem
   ranges anyway we cannot ever overflow 32 bits, even if we
   could. (and on 64-bit it doesnt matter anyway)

All in one, this function needs a total revamp. I used Linus's
suggestions minus the paranoid checks (we cannot overflow really
because if we get totally bad DMA ranges passed far more things
break in the systems than just DMA debugging). I also fixed a
few other small details i noticed.
Reported-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
Cc: Joerg Roedel <joerg.roedel@amd.com>
Signed-off-by: default avatarIngo Molnar <mingo@elte.hu>
parent 2b8777ca
...@@ -856,22 +856,21 @@ static void check_for_stack(struct device *dev, void *addr) ...@@ -856,22 +856,21 @@ static void check_for_stack(struct device *dev, void *addr)
"stack [addr=%p]\n", addr); "stack [addr=%p]\n", addr);
} }
static inline bool overlap(void *addr, u64 size, void *start, void *end) static inline bool overlap(void *addr, unsigned long len, void *start, void *end)
{ {
void *addr2 = (char *)addr + size; unsigned long a1 = (unsigned long)addr;
unsigned long b1 = a1 + len;
unsigned long a2 = (unsigned long)start;
unsigned long b2 = (unsigned long)end;
return ((addr >= start && addr < end) || return !(b1 <= a2 || a1 >= b2);
(addr2 >= start && addr2 < end) ||
((addr < start) && (addr2 > end)));
} }
static void check_for_illegal_area(struct device *dev, void *addr, u64 size) static void check_for_illegal_area(struct device *dev, void *addr, unsigned long len)
{ {
if (overlap(addr, size, _text, _etext) || if (overlap(addr, len, _text, _etext) ||
overlap(addr, size, __start_rodata, __end_rodata)) overlap(addr, len, __start_rodata, __end_rodata))
err_printk(dev, NULL, "DMA-API: device driver maps " err_printk(dev, NULL, "DMA-API: device driver maps memory from kernel text or rodata [addr=%p] [len=%lu]\n", addr, len);
"memory from kernel text or rodata "
"[addr=%p] [size=%llu]\n", addr, size);
} }
static void check_sync(struct device *dev, static void check_sync(struct device *dev,
...@@ -969,7 +968,8 @@ void debug_dma_map_page(struct device *dev, struct page *page, size_t offset, ...@@ -969,7 +968,8 @@ void debug_dma_map_page(struct device *dev, struct page *page, size_t offset,
entry->type = dma_debug_single; entry->type = dma_debug_single;
if (!PageHighMem(page)) { if (!PageHighMem(page)) {
void *addr = ((char *)page_address(page)) + offset; void *addr = page_address(page) + offset;
check_for_stack(dev, addr); check_for_stack(dev, addr);
check_for_illegal_area(dev, addr, size); check_for_illegal_area(dev, addr, size);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment