drm/amdgpu: Initialize r in VM and TTM buffer helpers
Subsystems (2)
cover drm/amdgpu: Initialize r in VM and TTM buffer helpers
Two fixes in drm/amdgpu for uninitialized return-value regressions found via Clang -Wconditional-uninitialized on an x86_64 allyesconfig build:
1. Initialize r = 0 in amdgpu_vm_bo_update().
2. Initialize r = 0 in amdgpu_ttm_clear_buffer().
Build-tested with LLVM=1 W=1 (-Werror); not runtime-tested on AMDGPU hardware.
Ian Rogers (2):
drm/amdgpu: initialize r in amdgpu_vm_bo_update() drm/amdgpu: initialize r in amdgpu_ttm_clear_buffer()
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 2 +- drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
Signature · 1 line
2.56.1.708.g77570a6a1d-goog
PATCH 1/2 drm/amdgpu: initialize r in amdgpu_vm_bo_update()
Commit bc566781845b ("drm/amdgpu: stop syncing PRT map operations") added an 'else if (!bo)' branch in amdgpu_vm_bo_update() that does not assign 'r'. Because fpriv->prt_va has bo == NULL and is updated on every command submission, userqueue validation, and KFD VM restore, calling amdgpu_vm_bo_update(adev, fpriv->prt_va, false) when bo_va->invalids is empty skips the mapping loop and returns uninitialized stack variable 'r'.
Initialize 'r' to 0.
Found via an x86_64 LLVM=1 (Clang 21.1.8) allyesconfig audit with -Wconditional-uninitialized enabled; fix and changelog drafted with an AI coding assistant. Build-tested under LLVM=1 W=1 (-Werror) and -Wconditional-uninitialized; not runtime-tested on AMD GPU hardware.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1 -1
@@ -1303,7 +1303,7 @@ int amdgpu_vm_bo_update(struct amdgpu_device *adev, struct amdgpu_bo_va *bo_va,uint64_t vram_base;uint64_t flags;bool uncached; -int r; +int r =0 ;amdgpu_sync_create (&sync);if (clear) {
Signature · 1 line
2.56.1.708.g77570a6a1d-goog
Christian König
maintainerRe: [PATCH 1/2] drm/amdgpu: initialize r in amdgpu_vm_bo_update()
Quoted 25 lines · Ian Rogers
Commit
bc566781845b("drm/amdgpu: stop syncing PRT map operations") added an 'else if (!bo)' branch inamdgpu_vm_bo_update()that does not assign 'r'. Becausefpriv->prt_vahas bo == NULL and is updated on every command submission, userqueue validation, and KFD VM restore, callingamdgpu_vm_bo_update(adev, fpriv->prt_va, false)whenbo_va->invalidsis empty skips the mapping loop and returns uninitialized stack variable 'r'.Initialize 'r' to 0.
Found via an
x86_64LLVM=1 (Clang 21.1.8) allyesconfig audit with -Wconditional-uninitialized enabled; fix and changelog drafted with an AI coding assistant. Build-tested under LLVM=1 W=1 (-Werror) and -Wconditional-uninitialized; not runtime-tested on AMD GPU hardware.Fixes bc566781845b ("drm/amdgpu: stop syncing PRT map operations")Signed-off-by Ian Rogers <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0
-0
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_bo_update(struct amdgpu_device *adev, struct amdgpu_bo_va *bo_va, · line 1306
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1
-1@@ -1303,7 +1303,7 @@ int amdgpu_vm_bo_update(struct amdgpu_device *adev, struct amdgpu_bo_va *bo_va,uint64_t vram_base;uint64_t flags;bool uncached; -int r; +int r =0 ;
Good catch, but please don't initialize return variables a default value.
That is often seen as bad coding style and often hides problems instead of fixing them.
Instead just initialize r in the if branch where it is missing.
Thanks,
Christian.
Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0
-0amdgpu_sync_create (&sync);if (clear) {
Re: [PATCH 1/2] drm/amdgpu: initialize r in amdgpu_vm_bo_update()
Quoted 37 lines · Christian König
Quoted 25 lines · Ian Rogers
Commit
bc566781845b("drm/amdgpu: stop syncing PRT map operations") added an 'else if (!bo)' branch inamdgpu_vm_bo_update()that does not assign 'r'. Becausefpriv->prt_vahas bo == NULL and is updated on every command submission, userqueue validation, and KFD VM restore, callingamdgpu_vm_bo_update(adev, fpriv->prt_va, false)whenbo_va->invalidsis empty skips the mapping loop and returns uninitialized stack variable 'r'.Initialize 'r' to 0.
Found via an
x86_64LLVM=1 (Clang 21.1.8) allyesconfig audit with -Wconditional-uninitialized enabled; fix and changelog drafted with an AI coding assistant. Build-tested under LLVM=1 W=1 (-Werror) and -Wconditional-uninitialized; not runtime-tested on AMD GPU hardware.Fixes bc566781845b ("drm/amdgpu: stop syncing PRT map operations")Signed-off-by Ian Rogers <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0
-0drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_bo_update(struct amdgpu_device *adev, struct amdgpu_bo_va *bo_va, · line 1306
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1
-1@@ -1303,7 +1303,7 @@ int amdgpu_vm_bo_update(struct amdgpu_device *adev, struct amdgpu_bo_va *bo_va,uint64_t vram_base;uint64_t flags;bool uncached; -int r; +int r =0 ;Good catch, but please don't initialize return variables a default value.
Quoted 3 lines
That is often seen as bad coding style and often hides problems instead of fixing them.
Instead just initialize r in the if branch where it is missing.
Makes sense - keeping 'int r;' uninitialized at declaration avoids masking future missing-assignment paths. I'll set 'r = 0;' inside the 'else if (!bo)' branch in v2.
Thanks,
Ian
Quoted 7 lines
Thanks,
Christian.Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0
-0amdgpu_sync_create (&sync);if (clear) {
PATCH 2/2 drm/amdgpu: initialize r in amdgpu_ttm_clear_buffer()
In reply to Ian RogersIn amdgpu_ttm_clear_buffer(), 'r' is declared uninitialized. When consider_clear_status is true (such as AMDGPU_GEM_CREATE_VRAM_CLEARED allocations) and all backing buddy blocks already have DRM_BUDDY_CLEARED set, every iteration of the cursor loop hits 'continue' without assigning 'r', returning uninitialized stack memory to amdgpu_bo_create().
Initialize 'r' to 0.
Found via an x86_64 LLVM=1 (Clang 21.1.8) allyesconfig audit with -Wconditional-uninitialized enabled; fix and changelog drafted with an AI coding assistant. Build-tested under LLVM=1 W=1 (-Werror) and -Wconditional-uninitialized; not runtime-tested on AMD GPU hardware.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +1 -1
@@ -2675,7 +2675,7 @@ int amdgpu_ttm_clear_buffer(struct amdgpu_ttm_buffer_entity *entity,struct dma_fence *fence =NULL ;struct amdgpu_res_cursor dst;bool vm_needs_flush = false; -int r; +int r =0 ;if (!entity)return -EINVAL ;
Signature · 1 line
2.56.1.708.g77570a6a1d-goog
Christian König
maintainerRe: [PATCH 2/2] drm/amdgpu: initialize r in amdgpu_ttm_clear_buffer()
Quoted 24 lines · Ian Rogers
In
amdgpu_ttm_clear_buffer(), 'r' is declared uninitialized. Whenconsider_clear_statusis true (such asAMDGPU_GEM_CREATE_VRAM_CLEAREDallocations) and all backing buddy blocks already haveDRM_BUDDY_CLEAREDset, every iteration of the cursor loop hits 'continue' without assigning 'r', returning uninitialized stack memory toamdgpu_bo_create().Initialize 'r' to 0.
Found via an
x86_64LLVM=1 (Clang 21.1.8) allyesconfig audit with -Wconditional-uninitialized enabled; fix and changelog drafted with an AI coding assistant. Build-tested under LLVM=1 W=1 (-Werror) and -Wconditional-uninitialized; not runtime-tested on AMD GPU hardware.Fixes 23bd7e67961e ("drm/amdgpu: get rid of amdgpu_ttm_clear_buffer")Signed-off-by Ian Rogers <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +0
-0
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c int amdgpu_ttm_clear_buffer(struct amdgpu_ttm_buffer_entity *entity, · line 2678
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +1
-1@@ -2675,7 +2675,7 @@ int amdgpu_ttm_clear_buffer(struct amdgpu_ttm_buffer_entity *entity,struct dma_fence *fence =NULL ;struct amdgpu_res_cursor dst;bool vm_needs_flush = false; -int r; +int r =0 ;
Please initialize r directly before the error label, apart from that looks good to me.
Regards,
Christian.
Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +0
-0if (!entity)return -EINVAL ;
Re: [PATCH 2/2] drm/amdgpu: initialize r in amdgpu_ttm_clear_buffer()
Quoted 23 lines · Christian König
Quoted 19 lines · Ian Rogers
In
amdgpu_ttm_clear_buffer(), 'r' is declared uninitialized. Whenconsider_clear_statusis true (such asAMDGPU_GEM_CREATE_VRAM_CLEAREDallocations) and all backing buddy blocks already haveDRM_BUDDY_CLEAREDset, every iteration of the cursor loop hits 'continue' without assigning 'r', returning uninitialized stack memory toamdgpu_bo_create().Initialize 'r' to 0.
Found via an
x86_64LLVM=1 (Clang 21.1.8) allyesconfig audit with -Wconditional-uninitialized enabled; fix and changelog drafted with an AI coding assistant. Build-tested under LLVM=1 W=1 (-Werror) and -Wconditional-uninitialized; not runtime-tested on AMD GPU hardware.Fixes 23bd7e67961e ("drm/amdgpu: get rid of amdgpu_ttm_clear_buffer")Signed-off-by Ian Rogers <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 12 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c int amdgpu_ttm_clear_buffer(struct amdgpu_ttm_buffer_entity *entity, · line 2678
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +1
-1@@ -2675,7 +2675,7 @@ int amdgpu_ttm_clear_buffer(struct amdgpu_ttm_buffer_entity *entity,struct dma_fence *fence =NULL ;struct amdgpu_res_cursor dst;bool vm_needs_flush = false; -int r; +int r =0 ;Please initialize r directly before the error label, apart from that looks good to me.
Will do - I'll move 'r = 0;' to right after the while loop directly before the 'error:' label in v2.
Thanks,
Ian
Quoted 7 lines
Regards,
Christian.Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +0
-0if (!entity)return -EINVAL ;