Skip to content

Commit aef7ee7

Browse files
pchelkin91Christoph Hellwig
authored andcommitted
dma-debug: fix physical address calculation for struct dma_debug_entry
Offset into the page should also be considered while calculating a physical address for struct dma_debug_entry. page_to_phys() just shifts the value PAGE_SHIFT bits to the left so offset part is zero-filled. An example (wrong) debug assertion failure with CONFIG_DMA_API_DEBUG enabled which is observed during systemd boot process after recent dma-debug changes: DMA-API: e1000 0000:00:03.0: cacheline tracking EEXIST, overlapping mappings aren't supported WARNING: CPU: 4 PID: 941 at kernel/dma/debug.c:596 add_dma_entry CPU: 4 UID: 0 PID: 941 Comm: ip Not tainted 6.12.0+ #288 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.16.2-debian-1.16.2-1 04/01/2014 RIP: 0010:add_dma_entry kernel/dma/debug.c:596 Call Trace: <TASK> debug_dma_map_page kernel/dma/debug.c:1236 dma_map_page_attrs kernel/dma/mapping.c:179 e1000_alloc_rx_buffers drivers/net/ethernet/intel/e1000/e1000_main.c:4616 ... Found by Linux Verification Center (linuxtesting.org). Fixes: 9d4f645 ("dma-debug: store a phys_addr_t in struct dma_debug_entry") Signed-off-by: Fedor Pchelkin <[email protected]> [hch: added a little helper to clean up the code] Signed-off-by: Christoph Hellwig <[email protected]>
1 parent 22293c3 commit aef7ee7

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

kernel/dma/debug.c

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,7 +1219,7 @@ void debug_dma_map_page(struct device *dev, struct page *page, size_t offset,
12191219

12201220
entry->dev = dev;
12211221
entry->type = dma_debug_single;
1222-
entry->paddr = page_to_phys(page);
1222+
entry->paddr = page_to_phys(page) + offset;
12231223
entry->dev_addr = dma_addr;
12241224
entry->size = size;
12251225
entry->direction = direction;
@@ -1377,6 +1377,18 @@ void debug_dma_unmap_sg(struct device *dev, struct scatterlist *sglist,
13771377
}
13781378
}
13791379

1380+
static phys_addr_t virt_to_paddr(void *virt)
1381+
{
1382+
struct page *page;
1383+
1384+
if (is_vmalloc_addr(virt))
1385+
page = vmalloc_to_page(virt);
1386+
else
1387+
page = virt_to_page(virt);
1388+
1389+
return page_to_phys(page) + offset_in_page(virt);
1390+
}
1391+
13801392
void debug_dma_alloc_coherent(struct device *dev, size_t size,
13811393
dma_addr_t dma_addr, void *virt,
13821394
unsigned long attrs)
@@ -1399,8 +1411,7 @@ void debug_dma_alloc_coherent(struct device *dev, size_t size,
13991411

14001412
entry->type = dma_debug_coherent;
14011413
entry->dev = dev;
1402-
entry->paddr = page_to_phys((is_vmalloc_addr(virt) ?
1403-
vmalloc_to_page(virt) : virt_to_page(virt)));
1414+
entry->paddr = virt_to_paddr(virt);
14041415
entry->size = size;
14051416
entry->dev_addr = dma_addr;
14061417
entry->direction = DMA_BIDIRECTIONAL;
@@ -1423,8 +1434,7 @@ void debug_dma_free_coherent(struct device *dev, size_t size,
14231434
if (!is_vmalloc_addr(virt) && !virt_addr_valid(virt))
14241435
return;
14251436

1426-
ref.paddr = page_to_phys((is_vmalloc_addr(virt) ?
1427-
vmalloc_to_page(virt) : virt_to_page(virt)));
1437+
ref.paddr = virt_to_paddr(virt);
14281438

14291439
if (unlikely(dma_debug_disabled()))
14301440
return;

0 commit comments

Comments
 (0)