mirror of
https://github.com/torvalds/linux.git
synced 2026-11-07 06:17:02 +01:00
net: devmem: decode DMA addresses for TX
On 32-bit architectures where dma_addr_t is wider than unsigned long, page_pool_set_dma_addr_netmem() stores page-aligned DMA addresses shifted by PAGE_SHIFT. The net_iov branch of __skb_frag_dma_map() adds byte offsets to the encoded value, so the NIC is programmed with an invalid DMA address. This can trigger an IOMMU fault or DMA from unintended memory. Consolidate DMA address encoding, decoding, and representability checks in netmem helpers. Use the common decoder from the page pool and net_iov TX paths so both interpret stored addresses consistently. Reviewed-by: Mina Almasry <almasrymina@google.com> Signed-off-by: Stanislav Fomichev <sdf@fomichev.me> Link: https://patch.msgid.link/20260925201522.254717-4-sdf@fomichev.me Signed-off-by: Paolo Abeni <pabeni@redhat.com>
This commit is contained in:
parent
5bdeb89ec5
commit
5c729e6c71
|
|
@ -3816,9 +3816,12 @@ static inline dma_addr_t __skb_frag_dma_map(struct device *dev,
|
|||
size_t offset, size_t size,
|
||||
enum dma_data_direction dir)
|
||||
{
|
||||
dma_addr_t addr;
|
||||
|
||||
if (skb_frag_is_net_iov(frag)) {
|
||||
return netmem_to_net_iov(frag->netmem)->desc.dma_addr +
|
||||
offset + frag->offset;
|
||||
addr = netmem_dma_addr_decode(
|
||||
netmem_get_dma_addr(frag->netmem));
|
||||
return addr + offset + frag->offset;
|
||||
}
|
||||
return dma_map_page(dev, skb_frag_page(frag),
|
||||
skb_frag_off(frag) + offset, size, dir);
|
||||
|
|
|
|||
|
|
@ -358,6 +358,33 @@ static inline bool netmem_is_pfmemalloc(netmem_ref netmem)
|
|||
return page_is_pfmemalloc(netmem_to_page(netmem));
|
||||
}
|
||||
|
||||
#define NETMEM_32BIT_ARCH_WITH_64BIT_DMA \
|
||||
(sizeof(dma_addr_t) > sizeof(unsigned long))
|
||||
|
||||
static inline unsigned long netmem_dma_addr_encode(dma_addr_t addr)
|
||||
{
|
||||
if (NETMEM_32BIT_ARCH_WITH_64BIT_DMA)
|
||||
addr >>= PAGE_SHIFT;
|
||||
|
||||
return addr;
|
||||
}
|
||||
|
||||
static inline dma_addr_t netmem_dma_addr_decode(unsigned long addr)
|
||||
{
|
||||
if (NETMEM_32BIT_ARCH_WITH_64BIT_DMA)
|
||||
return (dma_addr_t)addr << PAGE_SHIFT;
|
||||
|
||||
return addr;
|
||||
}
|
||||
|
||||
static inline bool netmem_dma_addr_fits(dma_addr_t addr)
|
||||
{
|
||||
/* We assume page alignment to shave off bottom bits,
|
||||
* if this "compression" doesn't work we need to drop.
|
||||
*/
|
||||
return addr == netmem_dma_addr_decode(netmem_dma_addr_encode(addr));
|
||||
}
|
||||
|
||||
static inline unsigned long netmem_get_dma_addr(netmem_ref netmem)
|
||||
{
|
||||
return netmem_to_nmdesc(netmem)->dma_addr;
|
||||
|
|
|
|||
|
|
@ -408,9 +408,6 @@ static inline void page_pool_recycle_direct_netmem(struct page_pool *pool,
|
|||
page_pool_put_full_netmem(pool, netmem, true);
|
||||
}
|
||||
|
||||
#define PAGE_POOL_32BIT_ARCH_WITH_64BIT_DMA \
|
||||
(sizeof(dma_addr_t) > sizeof(unsigned long))
|
||||
|
||||
/**
|
||||
* page_pool_free_va() - free a va into the page_pool
|
||||
* @pool: pool from which va was allocated
|
||||
|
|
@ -427,12 +424,7 @@ static inline void page_pool_free_va(struct page_pool *pool, void *va,
|
|||
|
||||
static inline dma_addr_t page_pool_get_dma_addr_netmem(netmem_ref netmem)
|
||||
{
|
||||
dma_addr_t ret = netmem_get_dma_addr(netmem);
|
||||
|
||||
if (PAGE_POOL_32BIT_ARCH_WITH_64BIT_DMA)
|
||||
ret <<= PAGE_SHIFT;
|
||||
|
||||
return ret;
|
||||
return netmem_dma_addr_decode(netmem_get_dma_addr(netmem));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -18,18 +18,8 @@ void page_pool_unlist(struct page_pool *pool);
|
|||
static inline bool
|
||||
page_pool_set_dma_addr_netmem(netmem_ref netmem, dma_addr_t addr)
|
||||
{
|
||||
if (PAGE_POOL_32BIT_ARCH_WITH_64BIT_DMA) {
|
||||
netmem_set_dma_addr(netmem, addr >> PAGE_SHIFT);
|
||||
|
||||
/* We assume page alignment to shave off bottom bits,
|
||||
* if this "compression" doesn't work we need to drop.
|
||||
*/
|
||||
return addr != (dma_addr_t)netmem_get_dma_addr(netmem)
|
||||
<< PAGE_SHIFT;
|
||||
}
|
||||
|
||||
netmem_set_dma_addr(netmem, addr);
|
||||
return false;
|
||||
netmem_set_dma_addr(netmem, netmem_dma_addr_encode(addr));
|
||||
return !netmem_dma_addr_fits(addr);
|
||||
}
|
||||
|
||||
static inline bool page_pool_set_dma_addr(struct page *page, dma_addr_t addr)
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user