]> www.infradead.org Git - users/hch/misc.git/commitdiff
drm/amdgpu: Use kmalloc_array() instead of kmalloc()
authorRahul Kumar <rk0006818@gmail.com>
Thu, 18 Sep 2025 07:12:00 +0000 (12:42 +0530)
committerAlex Deucher <alexander.deucher@amd.com>
Tue, 23 Sep 2025 14:35:54 +0000 (10:35 -0400)
Documentation/process/deprecated.rst recommends against the use of
kmalloc with dynamic size calculations due to the risk of overflow and
smaller allocation being made than the caller was expecting.

Replace kmalloc() with kmalloc_array() in amdgpu_amdkfd_gfx_v10.c,
amdgpu_amdkfd_gfx_v10_3.c, amdgpu_amdkfd_gfx_v11.c and
amdgpu_amdkfd_gfx_v12.c to make the intended allocation size clearer
and avoid potential overflow issues.

Suggested-by: Felix Kuehling <felix.kuehling@amd.com>
Signed-off-by: Rahul Kumar <rk0006818@gmail.com>
Signed-off-by: Felix Kuehling <felix.kuehling@amd.com>
Reviewed-by: Felix Kuehling <felix.kuehling@amd.com>
Signed-off-by: Alex Deucher <alexander.deucher@amd.com>
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v10.c
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v10_3.c
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v11.c
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v12.c

index 04ef0ca105414a1c797bc894501451624f03d95c..0239114fb6c4a527ab40eade22b5c747472dfdfb 100644 (file)
@@ -352,7 +352,7 @@ static int kgd_hqd_dump(struct amdgpu_device *adev,
                (*dump)[i++][1] = RREG32_SOC15_IP(GC, addr);            \
        } while (0)
 
-       *dump = kmalloc(HQD_N_REGS*2*sizeof(uint32_t), GFP_KERNEL);
+       *dump = kmalloc_array(HQD_N_REGS, sizeof(**dump), GFP_KERNEL);
        if (*dump == NULL)
                return -ENOMEM;
 
@@ -449,7 +449,7 @@ static int kgd_hqd_sdma_dump(struct amdgpu_device *adev,
 #undef HQD_N_REGS
 #define HQD_N_REGS (19+6+7+10)
 
-       *dump = kmalloc(HQD_N_REGS*2*sizeof(uint32_t), GFP_KERNEL);
+       *dump = kmalloc_array(HQD_N_REGS, sizeof(**dump), GFP_KERNEL);
        if (*dump == NULL)
                return -ENOMEM;
 
index 6d08bc2781a397d07ed89cc7632f429aca05709b..f2278a0937ff0ce6760ac58abb14932c514411e4 100644 (file)
@@ -338,7 +338,7 @@ static int hqd_dump_v10_3(struct amdgpu_device *adev,
                (*dump)[i++][1] = RREG32_SOC15_IP(GC, addr);            \
        } while (0)
 
-       *dump = kmalloc(HQD_N_REGS*2*sizeof(uint32_t), GFP_KERNEL);
+       *dump = kmalloc_array(HQD_N_REGS, sizeof(**dump), GFP_KERNEL);
        if (*dump == NULL)
                return -ENOMEM;
 
@@ -435,7 +435,7 @@ static int hqd_sdma_dump_v10_3(struct amdgpu_device *adev,
 #undef HQD_N_REGS
 #define HQD_N_REGS (19+6+7+12)
 
-       *dump = kmalloc(HQD_N_REGS*2*sizeof(uint32_t), GFP_KERNEL);
+       *dump = kmalloc_array(HQD_N_REGS, sizeof(**dump), GFP_KERNEL);
        if (*dump == NULL)
                return -ENOMEM;
 
index e0e6a6a49d900e7bac1e921831d17720846f70d3..aaccf0b9947d4da6cb9f476b3a34c6892899b8ca 100644 (file)
@@ -323,7 +323,7 @@ static int hqd_dump_v11(struct amdgpu_device *adev,
                (*dump)[i++][1] = RREG32(addr);         \
        } while (0)
 
-       *dump = kmalloc(HQD_N_REGS*2*sizeof(uint32_t), GFP_KERNEL);
+       *dump = kmalloc_array(HQD_N_REGS, sizeof(**dump), GFP_KERNEL);
        if (*dump == NULL)
                return -ENOMEM;
 
@@ -420,7 +420,7 @@ static int hqd_sdma_dump_v11(struct amdgpu_device *adev,
 #undef HQD_N_REGS
 #define HQD_N_REGS (7+11+1+12+12)
 
-       *dump = kmalloc(HQD_N_REGS*2*sizeof(uint32_t), GFP_KERNEL);
+       *dump = kmalloc_array(HQD_N_REGS, sizeof(**dump), GFP_KERNEL);
        if (*dump == NULL)
                return -ENOMEM;
 
index 6f0dc23c901b8431228556128e790eec357374eb..e0ceab400b2da38321fc6e0e3f2d3bfd30029566 100644 (file)
@@ -115,7 +115,7 @@ static int hqd_dump_v12(struct amdgpu_device *adev,
                (*dump)[i++][1] = RREG32(addr);         \
        } while (0)
 
-       *dump = kmalloc(HQD_N_REGS*2*sizeof(uint32_t), GFP_KERNEL);
+       *dump = kmalloc_array(HQD_N_REGS, sizeof(**dump), GFP_KERNEL);
        if (*dump == NULL)
                return -ENOMEM;
 
@@ -146,7 +146,7 @@ static int hqd_sdma_dump_v12(struct amdgpu_device *adev,
 #undef HQD_N_REGS
 #define HQD_N_REGS (last_reg - first_reg + 1)
 
-       *dump = kmalloc(HQD_N_REGS*2*sizeof(uint32_t), GFP_KERNEL);
+       *dump = kmalloc_array(HQD_N_REGS, sizeof(**dump), GFP_KERNEL);
        if (*dump == NULL)
                return -ENOMEM;