* [PATCH v2] drm/amdgpu: Use kmalloc_array() instead of kmalloc()
@ 2025-09-18 7:12 Rahul Kumar
2025-09-22 13:59 ` Kuehling, Felix
0 siblings, 1 reply; 7+ messages in thread
From: Rahul Kumar @ 2025-09-18 7:12 UTC (permalink / raw)
To: Felix.Kuehling, alexander.deucher, christian.koenig, airlied, simona
Cc: amd-gfx, dri-devel, linux-kernel, linux-kernel-mentees, skhan,
rk0006818, Felix Kuehling
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>
---
Changes since v1:
- Extend fix to amdgpu_amdkfd_gfx_v10_3.c, amdgpu_amdkfd_gfx_v11.c and
amdgpu_amdkfd_gfx_v12.c as suggested.
- Added Suggested-by tag.
Link to v1:
https://lore.kernel.org/all/f782159b-ad3f-4d0c-8476-50a701bf29e6@amd.com/
---
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v10.c | 4 ++--
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v10_3.c | 4 ++--
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v11.c | 4 ++--
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v12.c | 4 ++--
4 files changed, 8 insertions(+), 8 deletions(-)
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v10.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v10.c
index 04ef0ca10541..0239114fb6c4 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v10.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v10.c
@@ -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;
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v10_3.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v10_3.c
index 6d08bc2781a3..f2278a0937ff 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v10_3.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v10_3.c
@@ -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;
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v11.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v11.c
index e0e6a6a49d90..aaccf0b9947d 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v11.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v11.c
@@ -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;
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v12.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v12.c
index 6f0dc23c901b..e0ceab400b2d 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v12.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v12.c
@@ -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;
--
2.43.0
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v2] drm/amdgpu: Use kmalloc_array() instead of kmalloc()
2025-09-18 7:12 [PATCH v2] drm/amdgpu: Use kmalloc_array() instead of kmalloc() Rahul Kumar
@ 2025-09-22 13:59 ` Kuehling, Felix
0 siblings, 0 replies; 7+ messages in thread
From: Kuehling, Felix @ 2025-09-22 13:59 UTC (permalink / raw)
To: Rahul Kumar, alexander.deucher, christian.koenig, airlied, simona
Cc: amd-gfx, dri-devel, linux-kernel, linux-kernel-mentees, skhan
On 2025-09-18 03:12, Rahul Kumar wrote:
> 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>
Thank you. I applied the patch to amd-staging-drm-next.
Reviewed-by: Felix Kuehling <felix.kuehling@amd.com>
> ---
> Changes since v1:
> - Extend fix to amdgpu_amdkfd_gfx_v10_3.c, amdgpu_amdkfd_gfx_v11.c and
> amdgpu_amdkfd_gfx_v12.c as suggested.
> - Added Suggested-by tag.
> Link to v1:
> https://lore.kernel.org/all/f782159b-ad3f-4d0c-8476-50a701bf29e6@amd.com/
> ---
> drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v10.c | 4 ++--
> drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v10_3.c | 4 ++--
> drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v11.c | 4 ++--
> drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v12.c | 4 ++--
> 4 files changed, 8 insertions(+), 8 deletions(-)
>
> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v10.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v10.c
> index 04ef0ca10541..0239114fb6c4 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v10.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v10.c
> @@ -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;
>
> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v10_3.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v10_3.c
> index 6d08bc2781a3..f2278a0937ff 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v10_3.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v10_3.c
> @@ -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;
>
> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v11.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v11.c
> index e0e6a6a49d90..aaccf0b9947d 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v11.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v11.c
> @@ -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;
>
> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v12.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v12.c
> index 6f0dc23c901b..e0ceab400b2d 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v12.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gfx_v12.c
> @@ -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;
>
^ permalink raw reply [flat|nested] 7+ messages in thread
* RE: [PATCH v2] drm/amdgpu: use kmalloc_array() instead of kmalloc()
2025-07-24 6:29 [PATCH v2] drm/amdgpu: use " Yunshui Jiang
@ 2025-07-29 18:58 ` Deucher, Alexander
0 siblings, 0 replies; 7+ messages in thread
From: Deucher, Alexander @ 2025-07-29 18:58 UTC (permalink / raw)
To: Yunshui Jiang, linux-kernel; +Cc: chriistian.koenig, linux-doc, willy
[Public]
Applied. Thanks!
Alex
> -----Original Message-----
> From: Yunshui Jiang <jiangyunshui@kylinos.cn>
> Sent: Thursday, July 24, 2025 2:30 AM
> To: linux-kernel@vger.kernel.org
> Cc: Deucher, Alexander <Alexander.Deucher@amd.com>;
> chriistian.koenig@amd.com; linux-doc@vger.kernel.org; willy@infradead.org;
> Yunshui Jiang <jiangyunshui@kylinos.cn>
> Subject: [PATCH v2] drm/amdgpu: use kmalloc_array() instead of kmalloc()
>
> Use kmalloc_array() instead of kmalloc() with multiplication.
> kmalloc_array() is a safer way because of its multiply overflow check.
>
> ---
> Changes in v2:
> Fixed incorrect kmalloc usage in v1:
> - *bps = kmalloc(data->count, sizeof(struct ras_badpage), GFP_KERNEL);
> + *bps = kmalloc_array(data->count, sizeof(struct ras_badpage),
> GFP_KERNEL);
>
> Signed-off-by: Yunshui Jiang <jiangyunshui@kylinos.cn>
> ---
> drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c | 4 ++--
> 1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c
> b/drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c
> index de0944947eaf..12f5a1b9ff8b 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c
> @@ -2563,7 +2563,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;
> @@ -2719,7 +2719,7 @@ static int amdgpu_ras_realloc_eh_data_space(struct
> amdgpu_device *adev,
> unsigned int old_space = data->count + data->space_left;
> unsigned int new_space = old_space + pages;
> unsigned int align_space = ALIGN(new_space, 512);
> - void *bps = kmalloc(align_space * sizeof(*data->bps), GFP_KERNEL);
> + void *bps = kmalloc_array(align_space, sizeof(*data->bps),
> GFP_KERNEL);
>
> if (!bps) {
> return -ENOMEM;
> --
> 2.47.1
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v2] drm/amdgpu: use kmalloc_array() instead of kmalloc()
@ 2025-07-24 6:29 Yunshui Jiang
2025-07-29 18:58 ` Deucher, Alexander
0 siblings, 1 reply; 7+ messages in thread
From: Yunshui Jiang @ 2025-07-24 6:29 UTC (permalink / raw)
To: linux-kernel
Cc: alexander.deucher, chriistian.koenig, linux-doc, willy, Yunshui Jiang
Use kmalloc_array() instead of kmalloc() with multiplication.
kmalloc_array() is a safer way because of its multiply overflow check.
---
Changes in v2:
Fixed incorrect kmalloc usage in v1:
- *bps = kmalloc(data->count, sizeof(struct ras_badpage), GFP_KERNEL);
+ *bps = kmalloc_array(data->count, sizeof(struct ras_badpage), GFP_KERNEL);
Signed-off-by: Yunshui Jiang <jiangyunshui@kylinos.cn>
---
drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c
index de0944947eaf..12f5a1b9ff8b 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c
@@ -2563,7 +2563,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;
@@ -2719,7 +2719,7 @@ static int amdgpu_ras_realloc_eh_data_space(struct amdgpu_device *adev,
unsigned int old_space = data->count + data->space_left;
unsigned int new_space = old_space + pages;
unsigned int align_space = ALIGN(new_space, 512);
- void *bps = kmalloc(align_space * sizeof(*data->bps), GFP_KERNEL);
+ void *bps = kmalloc_array(align_space, sizeof(*data->bps), GFP_KERNEL);
if (!bps) {
return -ENOMEM;
--
2.47.1
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v2] drm/amdgpu: use kmalloc_array() instead of kmalloc()
2025-07-24 1:37 ` [PATCH v2] " Yunshui Jiang
@ 2025-07-24 1:48 ` Jeff Johnson
0 siblings, 0 replies; 7+ messages in thread
From: Jeff Johnson @ 2025-07-24 1:48 UTC (permalink / raw)
To: Yunshui Jiang, willy
Cc: alexander.deucher, chriistian.koenig, linux-doc, linux-kernel
On 7/23/2025 6:37 PM, Yunshui Jiang wrote:
> Use kmalloc_array() instead of kmalloc() with multiplication.
> kmalloc_array() is a safer way because of its multiply overflow check.
>
> Signed-off-by: Yunshui Jiang <jiangyunshui@kylinos.cn>
> ---
1) When you post a new version, you should add a revision history "after the
cut" so that folks know what changed
2) The new version should be sent as a separate thread; don't make the new
version In-Reply-To the previous version
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v2] drm/amdgpu: use kmalloc_array() instead of kmalloc()
2025-06-16 3:14 [PATCH] " Matthew Wilcox
@ 2025-07-24 1:37 ` Yunshui Jiang
2025-07-24 1:48 ` Jeff Johnson
0 siblings, 1 reply; 7+ messages in thread
From: Yunshui Jiang @ 2025-07-24 1:37 UTC (permalink / raw)
To: willy
Cc: alexander.deucher, chriistian.koenig, jiangyunshui, linux-doc,
linux-kernel
Use kmalloc_array() instead of kmalloc() with multiplication.
kmalloc_array() is a safer way because of its multiply overflow check.
Signed-off-by: Yunshui Jiang <jiangyunshui@kylinos.cn>
---
drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c
index de0944947eaf..12f5a1b9ff8b 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c
@@ -2563,7 +2563,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;
@@ -2719,7 +2719,7 @@ static int amdgpu_ras_realloc_eh_data_space(struct amdgpu_device *adev,
unsigned int old_space = data->count + data->space_left;
unsigned int new_space = old_space + pages;
unsigned int align_space = ALIGN(new_space, 512);
- void *bps = kmalloc(align_space * sizeof(*data->bps), GFP_KERNEL);
+ void *bps = kmalloc_array(align_space, sizeof(*data->bps), GFP_KERNEL);
if (!bps) {
return -ENOMEM;
--
2.47.1
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v2] drm/amdgpu: use kmalloc_array() instead of kmalloc()
@ 2025-07-04 3:30 Yunshui Jiang
0 siblings, 0 replies; 7+ messages in thread
From: Yunshui Jiang @ 2025-07-04 3:30 UTC (permalink / raw)
To: linux-kernel
Cc: amd-gfx, dri-devel, alexander.deucher, christian.koenig, airlied,
Yunshui Jiang
Use kmalloc_array() instead of kmalloc() with multiplication.
kmalloc_array() is a safer way because of its multiply overflow check.
Signed-off-by: Yunshui Jiang <jiangyunshui@kylinos.cn>
---
drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c
index de0944947eaf..12f5a1b9ff8b 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c
@@ -2563,7 +2563,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;
@@ -2719,7 +2719,7 @@ static int amdgpu_ras_realloc_eh_data_space(struct amdgpu_device *adev,
unsigned int old_space = data->count + data->space_left;
unsigned int new_space = old_space + pages;
unsigned int align_space = ALIGN(new_space, 512);
- void *bps = kmalloc(align_space * sizeof(*data->bps), GFP_KERNEL);
+ void *bps = kmalloc_array(align_space, sizeof(*data->bps), GFP_KERNEL);
if (!bps) {
return -ENOMEM;
--
2.47.1
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2025-09-22 13:59 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-09-18 7:12 [PATCH v2] drm/amdgpu: Use kmalloc_array() instead of kmalloc() Rahul Kumar
2025-09-22 13:59 ` Kuehling, Felix
-- strict thread matches above, loose matches on Subject: below --
2025-07-24 6:29 [PATCH v2] drm/amdgpu: use " Yunshui Jiang
2025-07-29 18:58 ` Deucher, Alexander
2025-07-04 3:30 Yunshui Jiang
2025-06-16 3:14 [PATCH] " Matthew Wilcox
2025-07-24 1:37 ` [PATCH v2] " Yunshui Jiang
2025-07-24 1:48 ` Jeff Johnson
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®