* [PATCH] drm/gud: Use kmalloc_array() instead of kmalloc()
@ 2025-09-22 17:43 Mehdi Ben Hadj Khelifa
2025-09-23 6:34 ` Thomas Zimmermann
0 siblings, 1 reply; 4+ messages in thread
From: Mehdi Ben Hadj Khelifa @ 2025-09-22 17:43 UTC (permalink / raw)
To: maarten.lankhorst, mripard, tzimmermann, airlied, simona, mingo,
tglx, jfalempe, skhan, david.hunter.linux
Cc: dri-devel, linux-kernel, linux-kernel-mentees,
Mehdi Ben Hadj Khelifa
Replace kmalloc with kmalloc array in drm/gud/gud_pipe.c since the
calculation inside kmalloc is dynamic "width * height" and added
u_char as the size of each element.
Signed-off-by: Mehdi Ben Hadj Khelifa <mehdi.benhadjkhelifa@gmail.com>
---
drivers/gpu/drm/gud/gud_pipe.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/gpu/drm/gud/gud_pipe.c b/drivers/gpu/drm/gud/gud_pipe.c
index 8d548d08f127..eeea0cb4c7aa 100644
--- a/drivers/gpu/drm/gud/gud_pipe.c
+++ b/drivers/gpu/drm/gud/gud_pipe.c
@@ -70,7 +70,7 @@ static size_t gud_xrgb8888_to_r124(u8 *dst, const struct drm_format_info *format
height = drm_rect_height(rect);
len = drm_format_info_min_pitch(format, 0, width) * height;
- buf = kmalloc(width * height, GFP_KERNEL);
+ buf = kmalloc_array(width * height, sizeof(u_char), GFP_KERNEL);
if (!buf)
return 0;
--
2.51.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] drm/gud: Use kmalloc_array() instead of kmalloc()
2025-09-22 17:43 [PATCH] drm/gud: Use kmalloc_array() instead of kmalloc() Mehdi Ben Hadj Khelifa
@ 2025-09-23 6:34 ` Thomas Zimmermann
2025-09-23 7:06 ` Thomas Zimmermann
0 siblings, 1 reply; 4+ messages in thread
From: Thomas Zimmermann @ 2025-09-23 6:34 UTC (permalink / raw)
To: Mehdi Ben Hadj Khelifa, maarten.lankhorst, mripard, airlied,
simona, mingo, tglx, jfalempe, skhan, david.hunter.linux,
Ruben Wauters
Cc: dri-devel, linux-kernel, linux-kernel-mentees
cc Ruben
Am 22.09.25 um 19:43 schrieb Mehdi Ben Hadj Khelifa:
> Replace kmalloc with kmalloc array in drm/gud/gud_pipe.c since the
> calculation inside kmalloc is dynamic "width * height" and added
> u_char as the size of each element.
>
> Signed-off-by: Mehdi Ben Hadj Khelifa <mehdi.benhadjkhelifa@gmail.com>
> ---
> drivers/gpu/drm/gud/gud_pipe.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/gpu/drm/gud/gud_pipe.c b/drivers/gpu/drm/gud/gud_pipe.c
> index 8d548d08f127..eeea0cb4c7aa 100644
> --- a/drivers/gpu/drm/gud/gud_pipe.c
> +++ b/drivers/gpu/drm/gud/gud_pipe.c
> @@ -70,7 +70,7 @@ static size_t gud_xrgb8888_to_r124(u8 *dst, const struct drm_format_info *format
> height = drm_rect_height(rect);
> len = drm_format_info_min_pitch(format, 0, width) * height;
>
> - buf = kmalloc(width * height, GFP_KERNEL);
> + buf = kmalloc_array(width * height, sizeof(u_char), GFP_KERNEL);
> if (!buf)
> return 0;
>
--
--
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Frankenstrasse 146, 90461 Nuernberg, Germany
GF: Ivo Totev, Andrew Myers, Andrew McDonald, Boudien Moerman
HRB 36809 (AG Nuernberg)
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] drm/gud: Use kmalloc_array() instead of kmalloc()
2025-09-23 6:34 ` Thomas Zimmermann
@ 2025-09-23 7:06 ` Thomas Zimmermann
2025-09-23 8:20 ` Mehdi Ben Hadj Khelifa
0 siblings, 1 reply; 4+ messages in thread
From: Thomas Zimmermann @ 2025-09-23 7:06 UTC (permalink / raw)
To: Mehdi Ben Hadj Khelifa, maarten.lankhorst, mripard, airlied,
simona, mingo, tglx, jfalempe, skhan, david.hunter.linux,
Ruben Wauters
Cc: dri-devel, linux-kernel, linux-kernel-mentees
Hi
Am 23.09.25 um 08:34 schrieb Thomas Zimmermann:
> cc Ruben
>
> Am 22.09.25 um 19:43 schrieb Mehdi Ben Hadj Khelifa:
>> Replace kmalloc with kmalloc array in drm/gud/gud_pipe.c since the
>> calculation inside kmalloc is dynamic "width * height" and added
>> u_char as the size of each element.
>>
>> Signed-off-by: Mehdi Ben Hadj Khelifa <mehdi.benhadjkhelifa@gmail.com>
>> ---
>> drivers/gpu/drm/gud/gud_pipe.c | 2 +-
>> 1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/drivers/gpu/drm/gud/gud_pipe.c
>> b/drivers/gpu/drm/gud/gud_pipe.c
>> index 8d548d08f127..eeea0cb4c7aa 100644
>> --- a/drivers/gpu/drm/gud/gud_pipe.c
>> +++ b/drivers/gpu/drm/gud/gud_pipe.c
>> @@ -70,7 +70,7 @@ static size_t gud_xrgb8888_to_r124(u8 *dst, const
>> struct drm_format_info *format
>> height = drm_rect_height(rect);
>> len = drm_format_info_min_pitch(format, 0, width) * height;
>> - buf = kmalloc(width * height, GFP_KERNEL);
>> + buf = kmalloc_array(width * height, sizeof(u_char), GFP_KERNEL);
u_char is more or less guaranteed to be 8 bit. So what's the point?
There's a potential overflow in width * height. If anything, you want to
call kmalloc_array(height, width).
Best regards
Thomas
>> if (!buf)
>> return 0;
>
--
--
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Frankenstrasse 146, 90461 Nuernberg, Germany
GF: Ivo Totev, Andrew Myers, Andrew McDonald, Boudien Moerman
HRB 36809 (AG Nuernberg)
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] drm/gud: Use kmalloc_array() instead of kmalloc()
2025-09-23 7:06 ` Thomas Zimmermann
@ 2025-09-23 8:20 ` Mehdi Ben Hadj Khelifa
0 siblings, 0 replies; 4+ messages in thread
From: Mehdi Ben Hadj Khelifa @ 2025-09-23 8:20 UTC (permalink / raw)
To: Thomas Zimmermann, maarten.lankhorst, mripard, airlied, simona,
mingo, tglx, jfalempe, Shuah Khan, david.hunter.linux, rubenru09
Cc: dri-devel, linux-kernel, linux-kernel-mentees
On 9/23/25 8:06 AM, Thomas Zimmermann wrote:
> Hi
>
> Am 23.09.25 um 08:34 schrieb Thomas Zimmermann:
>> cc Ruben
>>
>> Am 22.09.25 um 19:43 schrieb Mehdi Ben Hadj Khelifa:
>>> Replace kmalloc with kmalloc array in drm/gud/gud_pipe.c since the
>>> calculation inside kmalloc is dynamic "width * height" and added
>>> u_char as the size of each element.
>>>
>>> Signed-off-by: Mehdi Ben Hadj Khelifa <mehdi.benhadjkhelifa@gmail.com>
>>> ---
>>> drivers/gpu/drm/gud/gud_pipe.c | 2 +-
>>> 1 file changed, 1 insertion(+), 1 deletion(-)
>>>
>>> diff --git a/drivers/gpu/drm/gud/gud_pipe.c b/drivers/gpu/drm/gud/
>>> gud_pipe.c
>>> index 8d548d08f127..eeea0cb4c7aa 100644
>>> --- a/drivers/gpu/drm/gud/gud_pipe.c
>>> +++ b/drivers/gpu/drm/gud/gud_pipe.c
>>> @@ -70,7 +70,7 @@ static size_t gud_xrgb8888_to_r124(u8 *dst, const
>>> struct drm_format_info *format
>>> height = drm_rect_height(rect);
>>> len = drm_format_info_min_pitch(format, 0, width) * height;
>>> - buf = kmalloc(width * height, GFP_KERNEL);
>>> + buf = kmalloc_array(width * height, sizeof(u_char), GFP_KERNEL);
>
> u_char is more or less guaranteed to be 8 bit. So what's the point?
i thought it would be more "clean" to have a data type that is of 1 byte
size to use as a reference for the size of each element here.
> There's a potential overflow in width * height. If anything, you want to
> call kmalloc_array(height, width).
This was more apparent to me after i have sent the patch and was waiting
for a reply first. Since my initial though was that width or height
alone couldn't be treated as a reference to "n of elements" or "size of
each element" but your suggestion is more logical since leaving the
calculation width * height in the kmalloc_array would defeat the purpose
of switching from kmalloc to kmalloc_array since dynamic calculation is
still present and overflow is still possible.
I will send a v2 patch shortly changing to your suggestion.
> Best regards
> Thomas
>
Best Regards,
Mehdi Ben Hadj Khelifa
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2025-09-23 8:20 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-09-22 17:43 [PATCH] drm/gud: Use kmalloc_array() instead of kmalloc() Mehdi Ben Hadj Khelifa
2025-09-23 6:34 ` Thomas Zimmermann
2025-09-23 7:06 ` Thomas Zimmermann
2025-09-23 8:20 ` Mehdi Ben Hadj Khelifa
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox