Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

drm/amdgpu: Use kmalloc_array() instead of kmalloc()

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>

authored by

Rahul Kumar and committed by
Alex Deucher
86a54e45 2f9c6388

+8 -8
+2 -2
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v10.c
··· 352 352 (*dump)[i++][1] = RREG32_SOC15_IP(GC, addr); \ 353 353 } while (0) 354 354 355 - *dump = kmalloc(HQD_N_REGS*2*sizeof(uint32_t), GFP_KERNEL); 355 + *dump = kmalloc_array(HQD_N_REGS, sizeof(**dump), GFP_KERNEL); 356 356 if (*dump == NULL) 357 357 return -ENOMEM; 358 358 ··· 449 449 #undef HQD_N_REGS 450 450 #define HQD_N_REGS (19+6+7+10) 451 451 452 - *dump = kmalloc(HQD_N_REGS*2*sizeof(uint32_t), GFP_KERNEL); 452 + *dump = kmalloc_array(HQD_N_REGS, sizeof(**dump), GFP_KERNEL); 453 453 if (*dump == NULL) 454 454 return -ENOMEM; 455 455
+2 -2
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v10_3.c
··· 338 338 (*dump)[i++][1] = RREG32_SOC15_IP(GC, addr); \ 339 339 } while (0) 340 340 341 - *dump = kmalloc(HQD_N_REGS*2*sizeof(uint32_t), GFP_KERNEL); 341 + *dump = kmalloc_array(HQD_N_REGS, sizeof(**dump), GFP_KERNEL); 342 342 if (*dump == NULL) 343 343 return -ENOMEM; 344 344 ··· 435 435 #undef HQD_N_REGS 436 436 #define HQD_N_REGS (19+6+7+12) 437 437 438 - *dump = kmalloc(HQD_N_REGS*2*sizeof(uint32_t), GFP_KERNEL); 438 + *dump = kmalloc_array(HQD_N_REGS, sizeof(**dump), GFP_KERNEL); 439 439 if (*dump == NULL) 440 440 return -ENOMEM; 441 441
+2 -2
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v11.c
··· 323 323 (*dump)[i++][1] = RREG32(addr); \ 324 324 } while (0) 325 325 326 - *dump = kmalloc(HQD_N_REGS*2*sizeof(uint32_t), GFP_KERNEL); 326 + *dump = kmalloc_array(HQD_N_REGS, sizeof(**dump), GFP_KERNEL); 327 327 if (*dump == NULL) 328 328 return -ENOMEM; 329 329 ··· 420 420 #undef HQD_N_REGS 421 421 #define HQD_N_REGS (7+11+1+12+12) 422 422 423 - *dump = kmalloc(HQD_N_REGS*2*sizeof(uint32_t), GFP_KERNEL); 423 + *dump = kmalloc_array(HQD_N_REGS, sizeof(**dump), GFP_KERNEL); 424 424 if (*dump == NULL) 425 425 return -ENOMEM; 426 426
+2 -2
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v12.c
··· 115 115 (*dump)[i++][1] = RREG32(addr); \ 116 116 } while (0) 117 117 118 - *dump = kmalloc(HQD_N_REGS*2*sizeof(uint32_t), GFP_KERNEL); 118 + *dump = kmalloc_array(HQD_N_REGS, sizeof(**dump), GFP_KERNEL); 119 119 if (*dump == NULL) 120 120 return -ENOMEM; 121 121 ··· 146 146 #undef HQD_N_REGS 147 147 #define HQD_N_REGS (last_reg - first_reg + 1) 148 148 149 - *dump = kmalloc(HQD_N_REGS*2*sizeof(uint32_t), GFP_KERNEL); 149 + *dump = kmalloc_array(HQD_N_REGS, sizeof(**dump), GFP_KERNEL); 150 150 if (*dump == NULL) 151 151 return -ENOMEM; 152 152