åå: [PATCH] gpu: drm: use struct_size() in kmalloc()

From: Pan, Xinhui
Date: Mon May 20 2019 - 19:22:44 EST


Daniel, what you are talking about is totally wrong.
1) AFAIK, only one zero-size array can be in the end of a struct.
2) two struct_size will add up struct itself twice. the sum is wrong then.

No offense. I can't help feeling lucky that you are in intel.


åää: Daniel Vetter <daniel.vetter@xxxxxxxx> äè Daniel Vetter <daniel@xxxxxxxx>
åéæé: 2019å5æ21æ 0:28
æää: Pan, Xinhui
æé: Deucher, Alexander; Koenig, Christian; Zhou, David(ChunMing); airlied@xxxxxxxx; daniel@xxxxxxxx; Quan, Evan; xiaolinkui; amd-gfx@xxxxxxxxxxxxxxxxxxxxx; dri-devel@xxxxxxxxxxxxxxxxxxxxx; linux-kernel@xxxxxxxxxxxxxxx
äé: Re: [PATCH] gpu: drm: use struct_size() in kmalloc()
Â
[CAUTION: External Email]

On Fri, May 17, 2019 at 04:44:30PM +0000, Pan, Xinhui wrote:
> I am going to put more members which are also array after this struct,
> not only obj[]. Looks like this struct_size did not help on multiple
> array case. Thanks anyway. ________________________________

You can then add them up, e.g. kmalloc(struct_size()+struct_size(),
GFP_KERNEL), so this patch here still looks like a good idea.

Reviewed-by: Daniel Vetter <daniel.vetter@xxxxxxxx>

Cheers, Daniel

> From: xiaolinkui <xiaolinkui@xxxxxxxxxx>
> Sent: Friday, May 17, 2019 4:46:00 PM
> To: Deucher, Alexander; Koenig, Christian; Zhou, David(ChunMing); airlied@xxxxxxxx; daniel@xxxxxxxx; Pan, Xinhui; Quan, Evan
> Cc: amd-gfx@xxxxxxxxxxxxxxxxxxxxx; dri-devel@xxxxxxxxxxxxxxxxxxxxx; linux-kernel@xxxxxxxxxxxxxxx; xiaolinkui@xxxxxxxxxx
> Subject: [PATCH] gpu: drm: use struct_size() in kmalloc()
>
> [CAUTION: External Email]
>
> Use struct_size() helper to keep code simple.
>
> Signed-off-by: xiaolinkui <xiaolinkui@xxxxxxxxxx>
> ---
>Â drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c | 3 +--
>Â 1 file changed, 1 insertion(+), 2 deletions(-)
>
> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c
> index 22bd21e..4717a64 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c
> @@ -1375,8 +1375,7 @@ int amdgpu_ras_init(struct amdgpu_device *adev)
>ÂÂÂÂÂÂÂÂ if (con)
>ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ return 0;
>
> -ÂÂÂÂÂÂ con = kmalloc(sizeof(struct amdgpu_ras) +
> -ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ sizeof(struct ras_manager) * AMDGPU_RAS_BLOCK_COUNT,
> +ÂÂÂÂÂÂ con = kmalloc(struct_size(con, objs, AMDGPU_RAS_BLOCK_COUNT),
>ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ GFP_KERNEL|__GFP_ZERO);
>ÂÂÂÂÂÂÂÂ if (!con)
>ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ return -ENOMEM;
> --
> 2.7.4
>
>
>

--
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch