rmapi_node: bound external DMA-BUF sgt addresses by the GPU DMA width (#605)

nv_rs_rmapi_register_external_sgt() (GEM_EXPORT_DMABUF_MEMORY bridge,
#587) copied the importer's sg_dma_address() values verbatim into
external_dma_addrs[], which page_dma/page_dma_batch hand straight to
GPU sysmem PTEs. Nothing checked them against the GPU's addressing
width, so on GV11B (dma_bits = 40, platform_driver.c) a scatterlist
mapped for a different/wider device would be silently truncated by
the MMU. Refuse any segment ending past DMA_BIT_MASK(dma_bits) with
-EINVAL, before the RM object is created (caller keeps ownership).

Also derive page_dma_batch's renegotiation width from
nv_rs_rmapi_gpu_dma_bits() like page_dma does instead of a literal 47
(no behaviour change: negotiate already clamps to the node's width).

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
BlivionIaG
2026-12-05 16:21:03 +01:00
committed by GitHub
parent dfda0a29a4
commit 51d7d35c4a

View File

@ -2377,13 +2377,16 @@ int nv_rs_rmapi_register_external_sgt(u32 client, u32 device, u32 handle,
struct nv_rs_ext_release_work *rel_work = NULL;
struct scatterlist *sg;
u64 *dma_addrs;
u64 count, page = 0, pool, tok = 0;
u64 count, page = 0, pool, tok = 0, dma_limit;
unsigned int i;
u32 rm_st;
bool ok;
if (!sgt || !sgt->sgl || size == 0 || (size & (PAGE_SIZE - 1)))
return -EINVAL;
/* The addresses go into GPU PTEs verbatim: anything past the GPU's DMA
* width (40 bits on GV11B) would be truncated by the MMU. */
dma_limit = DMA_BIT_MASK(nv_rs_rmapi_gpu_dma_bits(gpu_id));
count = size >> PAGE_SHIFT;
if (count == 0 || count > (u64)UINT_MAX)
return -EINVAL;
@ -2406,7 +2409,9 @@ int nv_rs_rmapi_register_external_sgt(u32 client, u32 device, u32 handle,
unsigned int len = sg_dma_len(sg);
u64 off;
if ((addr & (PAGE_SIZE - 1)) || (len & (PAGE_SIZE - 1))) {
if ((addr & (PAGE_SIZE - 1)) || (len & (PAGE_SIZE - 1)) ||
(len && ((u64)addr > dma_limit ||
(u64)len - 1 > dma_limit - (u64)addr))) {
kvfree(dma_addrs);
return -EINVAL;
}
@ -2855,7 +2860,7 @@ u32 nv_rs_rmapi_os_page_dma_batch(u32 gpu_id, u64 token, u64 start_page, u64 cou
return NV_RS_STATUS_INVALID_ARGUMENT;
dev = nv_rs_rmapi_gpu_device_get(gpu_id);
if (dev)
nv_rs_rmapi_gpu_negotiate_dma(gpu_id, 47);
nv_rs_rmapi_gpu_negotiate_dma(gpu_id, nv_rs_rmapi_gpu_dma_bits(gpu_id));
mutex_lock(&nv_rs_rmapi_alloc_lock);
alloc = nv_rs_rmapi_alloc_find(token);