2024-02-10 14:21:07

by Erick Archer

[permalink] [raw]
Subject: [PATCH] drm/xe: Prefer struct_size over open coded arithmetic

This is an effort to get rid of all multiplications from allocation
functions in order to prevent integer overflows [1].

As the "q" variable is a pointer to "struct xe_exec_queue" and this
structure ends in a flexible array:

struct xe_exec_queue {
[...]
struct xe_lrc lrc[];
};

the preferred way in the kernel is to use the struct_size() helper to
do the arithmetic instead of the argument "size + size * count" in the
kzalloc() function.

This way, the code is more readable and more safer.

Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#open-coded-arithmetic-in-allocator-arguments [1]
Link: https://github.com/KSPP/linux/issues/160 [2]
Signed-off-by: Erick Archer <[email protected]>
---
drivers/gpu/drm/xe/xe_exec_queue.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/gpu/drm/xe/xe_exec_queue.c b/drivers/gpu/drm/xe/xe_exec_queue.c
index bcfc4127c7c5..f4e53cbccd04 100644
--- a/drivers/gpu/drm/xe/xe_exec_queue.c
+++ b/drivers/gpu/drm/xe/xe_exec_queue.c
@@ -44,7 +44,7 @@ static struct xe_exec_queue *__xe_exec_queue_create(struct xe_device *xe,
/* only kernel queues can be permanent */
XE_WARN_ON((flags & EXEC_QUEUE_FLAG_PERMANENT) && !(flags & EXEC_QUEUE_FLAG_KERNEL));

- q = kzalloc(sizeof(*q) + sizeof(struct xe_lrc) * width, GFP_KERNEL);
+ q = kzalloc(struct_size(q, lrc, width), GFP_KERNEL);
if (!q)
return ERR_PTR(-ENOMEM);

--
2.25.1



2024-02-10 16:50:51

by Gustavo A. R. Silva

[permalink] [raw]
Subject: Re: [PATCH] drm/xe: Prefer struct_size over open coded arithmetic



On 2/10/24 08:19, Erick Archer wrote:
> This is an effort to get rid of all multiplications from allocation
> functions in order to prevent integer overflows [1].
>
> As the "q" variable is a pointer to "struct xe_exec_queue" and this
> structure ends in a flexible array:
>
> struct xe_exec_queue {
> [...]
> struct xe_lrc lrc[];
> };
>
> the preferred way in the kernel is to use the struct_size() helper to
> do the arithmetic instead of the argument "size + size * count" in the
> kzalloc() function.
>
> This way, the code is more readable and more safer.
>
> Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#open-coded-arithmetic-in-allocator-arguments [1]
> Link: https://github.com/KSPP/linux/issues/160 [2]
> Signed-off-by: Erick Archer <[email protected]>

LGTM:

Reviewed-by: Gustavo A. R. Silva <[email protected]>

Thanks!
--
Gustavo

> ---
> drivers/gpu/drm/xe/xe_exec_queue.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/gpu/drm/xe/xe_exec_queue.c b/drivers/gpu/drm/xe/xe_exec_queue.c
> index bcfc4127c7c5..f4e53cbccd04 100644
> --- a/drivers/gpu/drm/xe/xe_exec_queue.c
> +++ b/drivers/gpu/drm/xe/xe_exec_queue.c
> @@ -44,7 +44,7 @@ static struct xe_exec_queue *__xe_exec_queue_create(struct xe_device *xe,
> /* only kernel queues can be permanent */
> XE_WARN_ON((flags & EXEC_QUEUE_FLAG_PERMANENT) && !(flags & EXEC_QUEUE_FLAG_KERNEL));
>
> - q = kzalloc(sizeof(*q) + sizeof(struct xe_lrc) * width, GFP_KERNEL);
> + q = kzalloc(struct_size(q, lrc, width), GFP_KERNEL);
> if (!q)
> return ERR_PTR(-ENOMEM);
>
> --
> 2.25.1
>
>

2024-02-23 05:00:26

by Lucas De Marchi

[permalink] [raw]
Subject: Re: Re: [PATCH] drm/xe: Prefer struct_size over open coded arithmetic

On Sat, Feb 10, 2024 at 10:49:57AM -0600, Gustavo A. R. Silva wrote:
>
>
>On 2/10/24 08:19, Erick Archer wrote:
>>This is an effort to get rid of all multiplications from allocation
>>functions in order to prevent integer overflows [1].
>>
>>As the "q" variable is a pointer to "struct xe_exec_queue" and this
>>structure ends in a flexible array:
>>
>>struct xe_exec_queue {
>> [...]
>> struct xe_lrc lrc[];
>>};
>>
>>the preferred way in the kernel is to use the struct_size() helper to
>>do the arithmetic instead of the argument "size + size * count" in the
>>kzalloc() function.
>>
>>This way, the code is more readable and more safer.
>>
>>Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#open-coded-arithmetic-in-allocator-arguments [1]
>>Link: https://github.com/KSPP/linux/issues/160 [2]
>>Signed-off-by: Erick Archer <[email protected]>
>
>LGTM:
>
>Reviewed-by: Gustavo A. R. Silva <[email protected]>

applied to drm-xe-next. Thanks for the patch and review.

Lucas De Marchi