Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] drm/amdgpu: unaligned VRAM IB read in
  amdgpu_devcoredump_print_ibs()
--

commit 14b3e312c2b66e1a7d6ed65ad219c71a4c22a7ca
Author: Matt Turner <[email protected]>

drm/amdgpu: fix IB content read in the devcoredump

This commit reworks IB content dumping in amdgpu_devcoredump_print_ibs() to
validate mappings and BO resources, account for mapping->offset, read VRAM
via amdgpu_device_vram_access(), and map non-VRAM pages into a local
ttm_bo_kmap_obj.

> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_dev_coredump.c 
> b/drivers/gpu/drm/amd/amdgpu/amdgpu_dev_coredump.c
> index 30a8b4c5591dd..0c6533b950f60 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_dev_coredump.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_dev_coredump.c
[ ... ]
> @@ -271,6 +281,7 @@ amdgpu_devcoredump_print_ibs(struct drm_printer *p,
>       }
>  
>       for (int i = 0; i < coredump->num_ibs; i++) {
> +             u64 size = (u64)coredump->ibs[i].ib_size_dw * 4;
>               bool emit_content = false;
>  
>               ib_content = kvmalloc_array(coredump->ibs[i].ib_size_dw, 4,
> @@ -280,41 +291,55 @@ amdgpu_devcoredump_print_ibs(struct drm_printer *p,
>  
>               va_start = coredump->ibs[i].gpu_addr & AMDGPU_GMC_HOLE_MASK;
>               mapping = amdgpu_vm_bo_lookup_mapping(vm, va_start);
> -             if (!mapping)
> +             abo = amdgpu_devcoredump_mapping_bo(mapping);
> +             if (!abo || !abo->tbo.resource)
>                       goto output_ib_content;
>  
> -             abo = mapping->bo_va->base.bo;
> -             offset = va_start - mapping->start * AMDGPU_GPU_PAGE_SIZE;
> +             if (va_start + size >
> +                 (mapping->last + 1) * AMDGPU_GPU_PAGE_SIZE)
> +                     goto output_ib_content;
>  
> -             if (abo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS) {
> +             offset = va_start - mapping->start * AMDGPU_GPU_PAGE_SIZE +
> +                      mapping->offset;
> +
> +             if (abo->tbo.resource->mem_type == TTM_PL_VRAM) {
>                       struct amdgpu_res_cursor cursor;
>  
>                       off = 0;
> -
> -                     if (abo->tbo.resource->mem_type != TTM_PL_VRAM)
> -                             goto output_ib_content;
> -
> -                     amdgpu_res_first(abo->tbo.resource, offset,
> -                                      coredump->ibs[i].ib_size_dw * 4, 
> &cursor);
> +                     amdgpu_res_first(abo->tbo.resource, offset, size,
> +                                      &cursor);
>                       while (cursor.remaining) {
> -                             amdgpu_device_mm_access(adev, cursor.start,
> -                                                     &ib_content[off], 
> cursor.size,
> -                                                     false);
> +                             amdgpu_device_vram_access(adev, cursor.start,
> +                                                       &ib_content[off],
> +                                                       cursor.size, false);
>                               off += cursor.size / 4;
>                               amdgpu_res_next(&cursor, cursor.size);
>                       }
>                       emit_content = true;

[Severity: Medium]
Can an unaligned IB address cause amdgpu_devcoredump_print_ibs() to print
uninitialized kernel memory from ib_content?

amdgpu_cs_p2_ib() copies userspace's chunk_ib->va_start into ib->gpu_addr
without checking 4-byte alignment.  When va_start is not 4-byte aligned,
offset, cursor.start, and cursor.size are unaligned as well.  For
CPU-invisible VRAM, amdgpu_device_vram_access() falls back to
amdgpu_device_mm_access(), which aborts without writing to the buffer:

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c:amdgpu_device_mm_access() {
        if (!drm_dev_enter(adev_to_drm(adev), &idx))
                return;

        if (!IS_ALIGNED(pos, 4) || !IS_ALIGNED(size, 4)) {
                dev_err(adev->dev, "unaligned pos/size (pos=0x%llx, 
size=0x%zx)\n",
                        pos, size);
                drm_dev_exit(idx);
                return;
        }

Since ib_content is allocated without __GFP_ZERO and emit_content is set to
true unconditionally, the devcoredump formats uninitialized memory.  Even in
CPU-visible VRAM, if an unaligned IB crosses a buddy block boundary,
cursor.size on the first block is not a multiple of 4, so off +=
cursor.size / 4 misaligns subsequent block reads and leaves the trailing
bytes of ib_content unwritten.

-- 
Sashiko AI review ยท 
https://sashiko.dev/#/patchset/[email protected]?part=1

Reply via email to