From 562a04a6369a04217f25e168dc26a5211c3e439f Mon Sep 17 00:00:00 2001 From: Alyx-ML <256263590+Alyx-ML@users.noreply.github.com> Date: Sun, 22 Nov 2026 01:10:45 +0000 Subject: [PATCH] accel/ane: reject buffers larger than the engine's address space struct ane_bo keeps its page count in a u32, and ane_iommu_map_pages(), ane_iommu_map_each() and the unmap paths computed sizes and offsets as `npages << ane->shift` and `i << ane->shift`. Both sides are 32-bit, so with 16 KiB pages a page count of 2^18 or more wraps. A buffer of 2^18 + 1 pages reserved a 16 KiB drm_mm node, while the default batch path (ane_iommu_map_batch(), which already used size_t) mapped all 4 GiB + 16 KiB from that node's start, over IOVA the allocator could hand to other buffers; the per-page path mapped page i at the IOVA of a much lower page. DRM_IOCTL_ANE_BO_INIT accepted any non-zero u64 size from any client that can open the accel node, and round_up() of a size near U64_MAX wrapped to 0. Record the size of the range ane->mm hands out (below 4 GiB) at domain init and return -E2BIG from BO_INIT for a larger buffer, before anything is allocated. A buffer that cannot be mapped whole is never created, and its page count always fits the u32. Also do every page-to-IOVA shift in u64, as ane_iommu_map_batch() already did, so the arithmetic stays right if the limit ever changes. Verified with a host harness under ASan and UBSan: the old expressions give 0 for a 4 GiB buffer and wrap page offsets past 2^18 pages; with the new check sizes up to the aperture are accepted with exact page counts and no truncation, and aperture+1, 4 GiB, U64_MAX and sizes that wrap round_up() return -E2BIG, for four aperture shapes. The harness models the patched expressions; the kernel object was not built here, and no ANE hardware was used. Signed-off-by: Alyx-ML <256263590+Alyx-ML@users.noreply.github.com> --- drivers/accel/ane/ane.h | 2 ++ drivers/accel/ane/ane_drv.c | 24 ++++++++++++++++-------- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/drivers/accel/ane/ane.h b/drivers/accel/ane/ane.h index 24a44ea99ed5d..36e7657173db1 100644 --- a/drivers/accel/ane/ane.h +++ b/drivers/accel/ane/ane.h @@ -46,6 +46,8 @@ struct ane_device { void __iomem *ps; struct drm_mm mm; + /* Size of the range mm hands out, page aligned and below 4 GiB. */ + u64 iova_size; struct iommu_domain *domain; unsigned long shift; struct ane_dart { diff --git a/drivers/accel/ane/ane_drv.c b/drivers/accel/ane/ane_drv.c index 9db2959604570..0a870573a1386 100644 --- a/drivers/accel/ane/ane_drv.c +++ b/drivers/accel/ane/ane_drv.c @@ -145,7 +145,7 @@ static int ane_iommu_map_each(struct ane_device *ane, struct ane_bo *bo, int err = 0; for (u32 i = 0; i < bo->npages; i++) { - dma_addr_t iova = bo->iova + (i << ane->shift); + dma_addr_t iova = bo->iova + ((u64)i << ane->shift); bool healed = false; retry: @@ -168,12 +168,12 @@ static int ane_iommu_map_each(struct ane_device *ane, struct ane_bo *bo, unmapped = iommu_unmap(ane->domain, bo->iova + - (i << ane->shift), + ((u64)i << ane->shift), 1UL << ane->shift); if (unmapped != 1UL << ane->shift) dev_err(ane->dev, "rollback unmap short at %#llx: %zu\n", - bo->iova + (i << ane->shift), + bo->iova + ((u64)i << ane->shift), unmapped); } break; @@ -261,7 +261,7 @@ static int ane_iommu_map_pages(struct ane_device *ane, struct ane_bo *bo) /* reserve area from ANE address space */ err = drm_mm_insert_node_generic(&ane->mm, bo->mm, - bo->npages << ane->shift, + (u64)bo->npages << ane->shift, 1UL << ane->shift, 0, 0); if (err < 0) { dev_err(ane->dev, "out of ANE space: %d\n", err); @@ -300,7 +300,7 @@ static void ane_iommu_unmap_pages(struct ane_device *ane, struct ane_bo *bo) list_del_init(&bo->node); mutex_lock(&ane->iommu_lock); for (u32 i = 0; i < bo->npages; i++) { - dma_addr_t iova = bo->iova + (i << ane->shift); + dma_addr_t iova = bo->iova + ((u64)i << ane->shift); size_t unmapped = iommu_unmap(ane->domain, iova, 1UL << ane->shift); @@ -371,12 +371,12 @@ void ane_reclaim_preserved(struct ane_device *ane) size_t unmapped; unmapped = iommu_unmap(ane->domain, - iova + (i << ane->shift), + iova + ((u64)i << ane->shift), 1UL << ane->shift); if (unmapped != 1UL << ane->shift) dev_err(ane->dev, "preserved unmap short at %#llx: %zu\n", - iova + (i << ane->shift), unmapped); + iova + ((u64)i << ane->shift), unmapped); } drm_mm_remove_node(p->mm); mutex_unlock(&ane->iommu_lock); @@ -490,6 +490,13 @@ static int ane_bo_init(struct drm_device *drm, void *data, if (args->pad || !args->size) return -EINVAL; + /* + * A buffer is mapped whole into the engine's address space, which + * ends below 4 GiB. A larger one could never be mapped, and its page + * count would overflow the u32 in struct ane_bo. + */ + if (args->size > ane->iova_size) + return -E2BIG; bo = kzalloc_obj(*bo); if (!bo) @@ -1044,7 +1051,8 @@ static int ane_iommu_domain_init(struct ane_device *ane) return -EINVAL; ane->domain = domain; ane->shift = PAGE_SHIFT; - drm_mm_init(&ane->mm, min_iova, limit - min_iova); + ane->iova_size = limit - min_iova; + drm_mm_init(&ane->mm, min_iova, ane->iova_size); return 0; }