[PATCH] drm/amd/amdgpu: use kmalloc_array instead of kmalloc

Moktar SELLAMI posted 1 patch 1 week, 2 days ago
drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[PATCH] drm/amd/amdgpu: use kmalloc_array instead of kmalloc
Posted by Moktar SELLAMI 1 week, 2 days ago
Replace kmalloc -> kmalloc_array in drm/amd/amdgpu/amdgpu_ras.c .

As per said in the Documentation/process/deprecated.rst, dynamic size
calculation should not be performed in memory allocator function
arguments due to the risk of overflow.

Signed-off-by: Moktar SELLAMI <smokthar925@gmail.com>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c
index 540817e296da..8dbb9e349a8e 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c
@@ -2566,7 +2566,7 @@ static int amdgpu_ras_badpages_read(struct amdgpu_device *adev,
 		goto out;
 	}
 
-	*bps = kmalloc(sizeof(struct ras_badpage) * data->count, GFP_KERNEL);
+	*bps = kmalloc_array(data->count, sizeof(struct ras_badpage), GFP_KERNEL);
 	if (!*bps) {
 		ret = -ENOMEM;
 		goto out;
-- 
2.34.1
Re: [PATCH] drm/amd/amdgpu: use kmalloc_array instead of kmalloc
Posted by Alex Deucher 1 week, 1 day ago
On Mon, Sep 22, 2025 at 4:14 PM Moktar SELLAMI <smokthar925@gmail.com> wrote:
>
> Replace kmalloc -> kmalloc_array in drm/amd/amdgpu/amdgpu_ras.c .
>
> As per said in the Documentation/process/deprecated.rst, dynamic size
> calculation should not be performed in memory allocator function
> arguments due to the risk of overflow.
>
> Signed-off-by: Moktar SELLAMI <smokthar925@gmail.com>

Thanks for the patch.  Some already supplied a similar patch which was applied.

Alex

> ---
>  drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c
> index 540817e296da..8dbb9e349a8e 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c
> @@ -2566,7 +2566,7 @@ static int amdgpu_ras_badpages_read(struct amdgpu_device *adev,
>                 goto out;
>         }
>
> -       *bps = kmalloc(sizeof(struct ras_badpage) * data->count, GFP_KERNEL);
> +       *bps = kmalloc_array(data->count, sizeof(struct ras_badpage), GFP_KERNEL);
>         if (!*bps) {
>                 ret = -ENOMEM;
>                 goto out;
> --
> 2.34.1
>