accel/ivpu: Validate scatter-gather size against buffer size
Karol Wachowski
maintainerPATCH 1/1 accel/ivpu: Validate scatter-gather size against buffer size
Validate scatter-gather table size matches buffer object size before mapping. Break mapping early if the table exceeds buffer size to prevent overwriting existing mappings. Also validate the table is not smaller than buffer size to avoid unmapped regions that trigger MMU translation faults.
Log error and fail mapping operation on size mismatch to prevent data corruption from mismatched host memory locations and NPU addresses. Unmap any partially mapped buffer on failure.
--- drivers/accel/ivpu/ivpu_gem.c | 2 +- drivers/accel/ivpu/ivpu_mmu_context.c | 20 +++++++++++++++++--- drivers/accel/ivpu/ivpu_mmu_context.h | 5 +++-- 3 files changed, 21 insertions(+), 6 deletions(-)
drivers/accel/ivpu/ivpu_gem.c +1 -1
@@ -95,7 +95,7 @@ int __must_check ivpu_bo_bind(struct ivpu_bo *bo)if (!bo->mmu_mapped) {drm_WARN_ON (&vdev->drm, !bo->ctx); - ret =ivpu_mmu_context_map_sgt (vdev, bo->ctx, bo->vpu_addr, sgt, + ret =ivpu_mmu_context_map_sgt (vdev, bo->ctx, bo->vpu_addr, sgt,ivpu_bo_size (bo),ivpu_bo_is_snooped (bo),ivpu_bo_is_read_only (bo));if (ret) {ivpu_err (vdev,"Failed to map BO in MMU: %d\n" , ret);
drivers/accel/ivpu/ivpu_mmu_context.c +17 -3
@@ -429,11 +429,12 @@ static void ivpu_mmu_context_unmap_pages(struct ivpu_mmu_context *ctx, u64 vpu_a }int -ivpu_mmu_context_map_sgt (struct ivpu_device *vdev,struct ivpu_mmu_context *ctx, -u64 vpu_addr,struct sg_table *sgt,bool llc_coherent,bool read_only) +ivpu_mmu_context_map_sgt (struct ivpu_device *vdev,struct ivpu_mmu_context *ctx,u64 vpu_addr, +struct sg_table *sgt,size_t bo_size,bool llc_coherent,bool read_only) {size_t start_vpu_addr = vpu_addr;struct scatterlist *sg; +size_t sgt_size =0 ;int ret;u64 prot;u64 i;
@@ -462,12 +463,25 @@ ivpu_mmu_context_map_sgt(struct ivpu_device *vdev, struct ivpu_mmu_context *ctx,ivpu_dbg (vdev,MMU_MAP ,"Map ctx: %u dma_addr: 0x%llx vpu_addr: 0x%llx size: %lu\n" , ctx->id, dma_addr, vpu_addr, size); +if (sgt_size + size > bo_size) { +ivpu_err (vdev,"Scatter-gather table size exceeds buffer object size\n" ); + ret = -EINVAL ; + goto err_unmap_pages; + } + ret =ivpu_mmu_context_map_pages (vdev, ctx, vpu_addr, dma_addr, size, prot);if (ret) {ivpu_err (vdev,"Failed to map context pages\n" ); goto err_unmap_pages; } vpu_addr += size; + sgt_size += size; + } + +if (sgt_size < bo_size) { +ivpu_err (vdev,"Scatter-gather table size too small to cover buffer object size\n" ); + ret = -EINVAL ; + goto err_unmap_pages; }if (!ctx->is_cd_valid) {
@@ -493,7 +507,7 @@ ivpu_mmu_context_map_sgt(struct ivpu_device *vdev, struct ivpu_mmu_context *ctx,return 0 ; err_unmap_pages: -ivpu_mmu_context_unmap_pages (ctx, start_vpu_addr, vpu_addr - start_vpu_addr); +ivpu_mmu_context_unmap_pages (ctx, start_vpu_addr, sgt_size);mutex_unlock (&ctx->lock);return ret; }
drivers/accel/ivpu/ivpu_mmu_context.h +3 -2
@@ -41,8 +41,9 @@ int ivpu_mmu_context_insert_node(struct ivpu_mmu_context *ctx, const struct ivpu u64 size, struct drm_mm_node *node);void ivpu_mmu_context_remove_node (struct ivpu_mmu_context *ctx,struct drm_mm_node *node); -int ivpu_mmu_context_map_sgt (struct ivpu_device *vdev,struct ivpu_mmu_context *ctx, -u64 vpu_addr,struct sg_table *sgt,bool llc_coherent,bool read_only); +int +ivpu_mmu_context_map_sgt (struct ivpu_device *vdev,struct ivpu_mmu_context *ctx,u64 vpu_addr, +struct sg_table *sgt,size_t bo_size,bool llc_coherent,bool read_only);void ivpu_mmu_context_unmap_sgt (struct ivpu_device *vdev,struct ivpu_mmu_context *ctx,u64 vpu_addr,struct sg_table *sgt); int ivpu_mmu_context_set_pages_ro(struct ivpu_device *vdev, struct ivpu_mmu_context *ctx,
Signature · 1 line
2.43.0
Quoted 100 lines · Karol Wachowski
Validate scatter-gather table size matches buffer object size before mapping. Break mapping early if the table exceeds buffer size to prevent overwriting existing mappings. Also validate the table is not smaller than buffer size to avoid unmapped regions that trigger MMU translation faults.
Log error and fail mapping operation on size mismatch to prevent data corruption from mismatched host memory locations and NPU addresses. Unmap any partially mapped buffer on failure.
Signed-off-by Karol Wachowski <[email protected]>--- drivers/accel/ivpu/ivpu_gem.c | 2 +- drivers/accel/ivpu/ivpu_mmu_context.c | 20 +++++++++++++++++--- drivers/accel/ivpu/ivpu_mmu_context.h | 5 +++-- 3 files changed, 21 insertions(+), 6 deletions(-)drivers/accel/ivpu/ivpu_gem.c +1
-1@@ -95,7 +95,7 @@ int __must_check ivpu_bo_bind(struct ivpu_bo *bo)if (!bo->mmu_mapped) {drm_WARN_ON (&vdev->drm, !bo->ctx); - ret =ivpu_mmu_context_map_sgt (vdev, bo->ctx, bo->vpu_addr, sgt, + ret =ivpu_mmu_context_map_sgt (vdev, bo->ctx, bo->vpu_addr, sgt,ivpu_bo_size (bo),ivpu_bo_is_snooped (bo),ivpu_bo_is_read_only (bo));if (ret) {ivpu_err (vdev,"Failed to map BO in MMU: %d\n" , ret);drivers/accel/ivpu/ivpu_mmu_context.c +17
-3@@ -429,11 +429,12 @@ static void ivpu_mmu_context_unmap_pages(struct ivpu_mmu_context *ctx, u64 vpu_a }int -ivpu_mmu_context_map_sgt (struct ivpu_device *vdev,struct ivpu_mmu_context *ctx, -u64 vpu_addr,struct sg_table *sgt,bool llc_coherent,bool read_only) +ivpu_mmu_context_map_sgt (struct ivpu_device *vdev,struct ivpu_mmu_context *ctx,u64 vpu_addr, +struct sg_table *sgt,size_t bo_size,bool llc_coherent,bool read_only) {size_t start_vpu_addr = vpu_addr;struct scatterlist *sg; +size_t sgt_size =0 ;int ret;u64 prot;u64 i;@@ -462,12 +463,25 @@ ivpu_mmu_context_map_sgt(struct ivpu_device *vdev, struct ivpu_mmu_context *ctx,ivpu_dbg (vdev,MMU_MAP ,"Map ctx: %u dma_addr: 0x%llx vpu_addr: 0x%llx size: %lu\n" , ctx->id, dma_addr, vpu_addr, size); +if (sgt_size + size > bo_size) { +ivpu_err (vdev,"Scatter-gather table size exceeds buffer object size\n" ); + ret = -EINVAL ; + goto err_unmap_pages; + } + ret =ivpu_mmu_context_map_pages (vdev, ctx, vpu_addr, dma_addr, size, prot);if (ret) {ivpu_err (vdev,"Failed to map context pages\n" ); goto err_unmap_pages; } vpu_addr += size; + sgt_size += size; + } + +if (sgt_size < bo_size) { +ivpu_err (vdev,"Scatter-gather table size too small to cover buffer object size\n" ); + ret = -EINVAL ; + goto err_unmap_pages; }if (!ctx->is_cd_valid) {@@ -493,7 +507,7 @@ ivpu_mmu_context_map_sgt(struct ivpu_device *vdev, struct ivpu_mmu_context *ctx,return 0 ; err_unmap_pages: -ivpu_mmu_context_unmap_pages (ctx, start_vpu_addr, vpu_addr - start_vpu_addr); +ivpu_mmu_context_unmap_pages (ctx, start_vpu_addr, sgt_size);mutex_unlock (&ctx->lock);return ret; }drivers/accel/ivpu/ivpu_mmu_context.h +3
-2@@ -41,8 +41,9 @@ int ivpu_mmu_context_insert_node(struct ivpu_mmu_context *ctx, const struct ivpu u64 size, struct drm_mm_node *node);void ivpu_mmu_context_remove_node (struct ivpu_mmu_context *ctx,struct drm_mm_node *node); -int ivpu_mmu_context_map_sgt (struct ivpu_device *vdev,struct ivpu_mmu_context *ctx, -u64 vpu_addr,struct sg_table *sgt,bool llc_coherent,bool read_only); +int +ivpu_mmu_context_map_sgt (struct ivpu_device *vdev,struct ivpu_mmu_context *ctx,u64 vpu_addr, +struct sg_table *sgt,size_t bo_size,bool llc_coherent,bool read_only);void ivpu_mmu_context_unmap_sgt (struct ivpu_device *vdev,struct ivpu_mmu_context *ctx,u64 vpu_addr,struct sg_table *sgt); int ivpu_mmu_context_set_pages_ro(struct ivpu_device *vdev, struct ivpu_mmu_context *ctx,
Karol Wachowski
maintainerQuoted 1 line · Lizhi Hou
Reviewed-by Lizhi Hou <[email protected]>
Thank you. Applied to drm-misc-next.
Karol>
Quoted 120 lines · Karol Wachowski
Quoted 119 lines
Validate scatter-gather table size matches buffer object size before mapping. Break mapping early if the table exceeds buffer size to prevent overwriting existing mappings. Also validate the table is not smaller than buffer size to avoid unmapped regions that trigger MMU translation faults.
Log error and fail mapping operation on size mismatch to prevent data corruption from mismatched host memory locations and NPU addresses. Unmap any partially mapped buffer on failure.
Signed-off-by Karol Wachowski <[email protected]>---
drivers/accel/ivpu/ivpu_gem.c | 2 +-drivers/accel/ivpu/ivpu_mmu_context.c| 20 +++++++++++++++++---drivers/accel/ivpu/ivpu_mmu_context.h| 5 +++--3 files changed, 21 insertions(+), 6 deletions(-)drivers/accel/ivpu/ renamed from drivers/accel/ivpu/ivpu_gem.c +0
-0
ivpu_gem.c
indexece68f570b7e..98b9ce26962b100644drivers/accel/ivpu/ivpu_gem.c +1
-1@@ -95,7 +95,7 @@ int __must_check ivpu_bo_bind(struct ivpu_bo *bo) if (!bo->mmu_mapped) { drm_WARN_ON(&vdev->drm, !bo->ctx); - ret = ivpu_mmu_context_map_sgt(vdev, bo->ctx, bo->vpu_addr, sgt, + ret = ivpu_mmu_context_map_sgt(vdev, bo->ctx, bo->vpu_addr, sgt,ivpu_bo_size (bo), ivpu_bo_is_snooped(bo),ivpu_bo_is_read_only (bo)); if (ret) {
ivpu_err(vdev, "Failed to map BO in MMU: %d\n", ret);drivers/accel/ renamed from drivers/accel/ivpu/ivpu_mmu_context.c +0
-0
ivpu/ivpu_mmu_context.c
index87ad593ef47d..c4014c83e727100644drivers/accel/ivpu/ivpu_mmu_context.c +0
-0@@ -429,11 +429,12 @@ static void ivpu_mmu_context_unmap_pages(struct
ivpu_mmu_context*ctx, u64vpu_a
}
int
-ivpu_mmu_context_map_sgt(structivpu_device*vdev, structivpu_mmu_context*ctx,
- u64vpu_addr, structsg_table*sgt, boolllc_coherent, boolread_only)
+ivpu_mmu_context_map_sgt(structivpu_device*vdev, structivpu_mmu_context*ctx, u64vpu_addr,
+ structsg_table*sgt,size_tbo_size, boolllc_coherent, boolread_only)
{
size_tstart_vpu_addr=vpu_addr;
struct scatterlist *sg;
+size_tsgt_size= 0;
int ret;
u64 prot;
u64 i;drivers/accel/ivpu/ivpu_mmu_context.c +0
-0@@ -462,12 +463,25 @@ ivpu_mmu_context_map_sgt(struct ivpu_device *vdev, struct ivpu_mmu_context *ctx,ivpu_dbg(vdev,
MMU_MAP, "Map ctx: %udma_addr: 0x%llxvpu_addr: 0x%llx size: %lu\n",
ctx->id,dma_addr,vpu_addr, size);
+ if (sgt_size+ size >bo_size) {
+ivpu_err(vdev, "Scatter-gather table size exceeds buffer object size\n");
+ ret =-EINVAL;
+ gotoerr_unmap_pages;
+ }
+
ret =ivpu_mmu_context_map_pages(vdev, ctx, vpu_addr, dma_addr, size, prot);
if (ret) {
ivpu_err(vdev, "Failed to map context pages\n"); gotoerr_unmap_pages;
}
vpu_addr+= size;
+sgt_size+= size;
+ }
+
+ if (sgt_size<bo_size) {
+ivpu_err(vdev, "Scatter-gather table size too small to cover buffer object size\n");
+ ret =-EINVAL;
+ gotoerr_unmap_pages;
}
if (!ctx->is_cd_valid) {drivers/accel/ivpu/ivpu_mmu_context.c +0
-0@@ -493,7 +507,7 @@ ivpu_mmu_context_map_sgt(struct ivpu_device *vdev, struct ivpu_mmu_context *ctx, return 0;
err_unmap_pages:
-ivpu_mmu_context_unmap_pages(ctx, start_vpu_addr, vpu_addr - start_vpu_addr);
+ivpu_mmu_context_unmap_pages(ctx, start_vpu_addr, sgt_size);mutex_unlock(&ctx->lock);
return ret;
}drivers/accel/ renamed from drivers/accel/ivpu/ivpu_mmu_context.h +0
-0
ivpu/ivpu_mmu_context.h
index663a11a9db11..cc02e7bab04e100644drivers/accel/ivpu/ivpu_mmu_context.h +0
-0@@ -41,8 +41,9 @@ int ivpu_mmu_context_insert_node(struct
ivpu_mmu_context*ctx, const struct ivpu
u64 size, structdrm_mm_node*node); voidivpu_mmu_context_remove_node(struct ivpu_mmu_context *ctx, struct drm_mm_node *node);
-int ivpu_mmu_context_map_sgt(structivpu_device*vdev, structivpu_mmu_context*ctx,
- u64vpu_addr, structsg_table*sgt, boolllc_coherent, boolread_only);drivers/accel/ivpu/ivpu_mmu_context.h +2
-0+int +ivpu_mmu_context_map_sgt(struct ivpu_device *vdev, struct
ivpu_mmu_context*ctx, u64vpu_addr,
+ structsg_table*sgt,size_tbo_size, boolllc_coherent, boolread_only);
void ivpu_mmu_context_unmap_sgt(structivpu_device*vdev, structivpu_mmu_context*ctx,
u64vpu_addr, structsg_table*sgt);
int ivpu_mmu_context_set_pages_ro(structivpu_device*vdev, structivpu_mmu_context*ctx,