Skip to content

Commit 63371be

Browse files
pchelkin91gregkh
authored andcommitted
dma-debug: fix physical address calculation for struct dma_debug_entry
[ Upstream commit aef7ee7 ] 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 <pchelkin@ispras.ru> [hch: added a little helper to clean up the code] Signed-off-by: Christoph Hellwig <hch@lst.de> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent a0d2200 commit 63371be

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
@@ -1224,7 +1224,7 @@ void debug_dma_map_page(struct device *dev, struct page *page, size_t offset,
12241224

12251225
entry->dev = dev;
12261226
entry->type = dma_debug_single;
1227-
entry->paddr = page_to_phys(page);
1227+
entry->paddr = page_to_phys(page) + offset;
12281228
entry->dev_addr = dma_addr;
12291229
entry->size = size;
12301230
entry->direction = direction;
@@ -1382,6 +1382,18 @@ void debug_dma_unmap_sg(struct device *dev, struct scatterlist *sglist,
13821382
}
13831383
}
13841384

1385+
static phys_addr_t virt_to_paddr(void *virt)
1386+
{
1387+
struct page *page;
1388+
1389+
if (is_vmalloc_addr(virt))
1390+
page = vmalloc_to_page(virt);
1391+
else
1392+
page = virt_to_page(virt);
1393+
1394+
return page_to_phys(page) + offset_in_page(virt);
1395+
}
1396+
13851397
void debug_dma_alloc_coherent(struct device *dev, size_t size,
13861398
dma_addr_t dma_addr, void *virt,
13871399
unsigned long attrs)
@@ -1404,8 +1416,7 @@ void debug_dma_alloc_coherent(struct device *dev, size_t size,
14041416

14051417
entry->type = dma_debug_coherent;
14061418
entry->dev = dev;
1407-
entry->paddr = page_to_phys((is_vmalloc_addr(virt) ?
1408-
vmalloc_to_page(virt) : virt_to_page(virt)));
1419+
entry->paddr = virt_to_paddr(virt);
14091420
entry->size = size;
14101421
entry->dev_addr = dma_addr;
14111422
entry->direction = DMA_BIDIRECTIONAL;
@@ -1428,8 +1439,7 @@ void debug_dma_free_coherent(struct device *dev, size_t size,
14281439
if (!is_vmalloc_addr(virt) && !virt_addr_valid(virt))
14291440
return;
14301441

1431-
ref.paddr = page_to_phys((is_vmalloc_addr(virt) ?
1432-
vmalloc_to_page(virt) : virt_to_page(virt)));
1442+
ref.paddr = virt_to_paddr(virt);
14331443

14341444
if (unlikely(dma_debug_disabled()))
14351445
return;

0 commit comments

Comments
 (0)