diff mbox series

[v1,1/3] drm/admgpu: replace kmalloc() and memcpy() with kmemdup()

Message ID 20241217225811.2437150-2-mtodorovac69@gmail.com (mailing list archive)
State New
Headers show
Series [v1,1/3] drm/admgpu: replace kmalloc() and memcpy() with kmemdup() | expand

Commit Message

Mirsad Todorovac Dec. 17, 2024, 10:58 p.m. UTC
The static analyser tool gave the following advice:

./drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c:1266:7-14: WARNING opportunity for kmemdup

 → 1266         tmp = kmalloc(used_size, GFP_KERNEL);
   1267         if (!tmp)
   1268                 return -ENOMEM;
   1269
 → 1270         memcpy(tmp, &host_telemetry->body.error_count, used_size);

Replacing kmalloc() + memcpy() with kmemdump() doesn't change semantics.
Original code works without fault, so this is not a bug fix but proposed improvement.

Link: https://lwn.net/Articles/198928/
Fixes: 84a2947ecc85c ("drm/amdgpu: Implement virt req_ras_err_count")
Cc: Alex Deucher <alexander.deucher@amd.com>
Cc: "Christian König" <christian.koenig@amd.com>
Cc: Xinhui Pan <Xinhui.Pan@amd.com>
Cc: David Airlie <airlied@gmail.com>
Cc: Simona Vetter <simona@ffwll.ch>
Cc: Zhigang Luo <Zhigang.Luo@amd.com>
Cc: Victor Skvortsov <victor.skvortsov@amd.com>
Cc: Hawking Zhang <Hawking.Zhang@amd.com>
Cc: Lijo Lazar <lijo.lazar@amd.com>
Cc: Yunxiang Li <Yunxiang.Li@amd.com>
Cc: Jack Xiao <Jack.Xiao@amd.com>
Cc: Vignesh Chander <Vignesh.Chander@amd.com>
Cc: Danijel Slivka <danijel.slivka@amd.com>
Cc: amd-gfx@lists.freedesktop.org
Cc: dri-devel@lists.freedesktop.org
Cc: linux-kernel@vger.kernel.org
Signed-off-by: Mirsad Todorovac <mtodorovac69@gmail.com>
---
 v1:
	initial version.

 drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

Comments

Lazar, Lijo Dec. 18, 2024, 8:03 a.m. UTC | #1
On 12/18/2024 4:28 AM, Mirsad Todorovac wrote:
> The static analyser tool gave the following advice:
> 
> ./drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c:1266:7-14: WARNING opportunity for kmemdup
> 
>  → 1266         tmp = kmalloc(used_size, GFP_KERNEL);
>    1267         if (!tmp)
>    1268                 return -ENOMEM;
>    1269
>  → 1270         memcpy(tmp, &host_telemetry->body.error_count, used_size);
> 
> Replacing kmalloc() + memcpy() with kmemdump() doesn't change semantics.
> Original code works without fault, so this is not a bug fix but proposed improvement.
> 
> Link: https://lwn.net/Articles/198928/
> Fixes: 84a2947ecc85c ("drm/amdgpu: Implement virt req_ras_err_count")
> Cc: Alex Deucher <alexander.deucher@amd.com>
> Cc: "Christian König" <christian.koenig@amd.com>
> Cc: Xinhui Pan <Xinhui.Pan@amd.com>
> Cc: David Airlie <airlied@gmail.com>
> Cc: Simona Vetter <simona@ffwll.ch>
> Cc: Zhigang Luo <Zhigang.Luo@amd.com>
> Cc: Victor Skvortsov <victor.skvortsov@amd.com>
> Cc: Hawking Zhang <Hawking.Zhang@amd.com>
> Cc: Lijo Lazar <lijo.lazar@amd.com>
> Cc: Yunxiang Li <Yunxiang.Li@amd.com>
> Cc: Jack Xiao <Jack.Xiao@amd.com>
> Cc: Vignesh Chander <Vignesh.Chander@amd.com>
> Cc: Danijel Slivka <danijel.slivka@amd.com>
> Cc: amd-gfx@lists.freedesktop.org
> Cc: dri-devel@lists.freedesktop.org
> Cc: linux-kernel@vger.kernel.org
> Signed-off-by: Mirsad Todorovac <mtodorovac69@gmail.com>

Reviewed-by: Lijo Lazar <lijo.lazar@amd.com>

Thanks,
Lijo

> ---
>  v1:
> 	initial version.
> 
>  drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c | 4 +---
>  1 file changed, 1 insertion(+), 3 deletions(-)
> 
> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c
> index c704e9803e11..0af469ec6fcc 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c
> @@ -1263,12 +1263,10 @@ static int amdgpu_virt_cache_host_error_counts(struct amdgpu_device *adev,
>  	if (used_size > (AMD_SRIOV_RAS_TELEMETRY_SIZE_KB << 10))
>  		return 0;
>  
> -	tmp = kmalloc(used_size, GFP_KERNEL);
> +	tmp = kmemdup(&host_telemetry->body.error_count, used_size, GFP_KERNEL);
>  	if (!tmp)
>  		return -ENOMEM;
>  
> -	memcpy(tmp, &host_telemetry->body.error_count, used_size);
> -
>  	if (checksum != amd_sriov_msg_checksum(tmp, used_size, 0, 0))
>  		goto out;
>
Alex Deucher Dec. 18, 2024, 2:47 p.m. UTC | #2
Applied.  Thanks!

Alex

On Wed, Dec 18, 2024 at 3:03 AM Lazar, Lijo <lijo.lazar@amd.com> wrote:
>
>
>
> On 12/18/2024 4:28 AM, Mirsad Todorovac wrote:
> > The static analyser tool gave the following advice:
> >
> > ./drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c:1266:7-14: WARNING opportunity for kmemdup
> >
> >  → 1266         tmp = kmalloc(used_size, GFP_KERNEL);
> >    1267         if (!tmp)
> >    1268                 return -ENOMEM;
> >    1269
> >  → 1270         memcpy(tmp, &host_telemetry->body.error_count, used_size);
> >
> > Replacing kmalloc() + memcpy() with kmemdump() doesn't change semantics.
> > Original code works without fault, so this is not a bug fix but proposed improvement.
> >
> > Link: https://lwn.net/Articles/198928/
> > Fixes: 84a2947ecc85c ("drm/amdgpu: Implement virt req_ras_err_count")
> > Cc: Alex Deucher <alexander.deucher@amd.com>
> > Cc: "Christian König" <christian.koenig@amd.com>
> > Cc: Xinhui Pan <Xinhui.Pan@amd.com>
> > Cc: David Airlie <airlied@gmail.com>
> > Cc: Simona Vetter <simona@ffwll.ch>
> > Cc: Zhigang Luo <Zhigang.Luo@amd.com>
> > Cc: Victor Skvortsov <victor.skvortsov@amd.com>
> > Cc: Hawking Zhang <Hawking.Zhang@amd.com>
> > Cc: Lijo Lazar <lijo.lazar@amd.com>
> > Cc: Yunxiang Li <Yunxiang.Li@amd.com>
> > Cc: Jack Xiao <Jack.Xiao@amd.com>
> > Cc: Vignesh Chander <Vignesh.Chander@amd.com>
> > Cc: Danijel Slivka <danijel.slivka@amd.com>
> > Cc: amd-gfx@lists.freedesktop.org
> > Cc: dri-devel@lists.freedesktop.org
> > Cc: linux-kernel@vger.kernel.org
> > Signed-off-by: Mirsad Todorovac <mtodorovac69@gmail.com>
>
> Reviewed-by: Lijo Lazar <lijo.lazar@amd.com>
>
> Thanks,
> Lijo
>
> > ---
> >  v1:
> >       initial version.
> >
> >  drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c | 4 +---
> >  1 file changed, 1 insertion(+), 3 deletions(-)
> >
> > diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c
> > index c704e9803e11..0af469ec6fcc 100644
> > --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c
> > +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c
> > @@ -1263,12 +1263,10 @@ static int amdgpu_virt_cache_host_error_counts(struct amdgpu_device *adev,
> >       if (used_size > (AMD_SRIOV_RAS_TELEMETRY_SIZE_KB << 10))
> >               return 0;
> >
> > -     tmp = kmalloc(used_size, GFP_KERNEL);
> > +     tmp = kmemdup(&host_telemetry->body.error_count, used_size, GFP_KERNEL);
> >       if (!tmp)
> >               return -ENOMEM;
> >
> > -     memcpy(tmp, &host_telemetry->body.error_count, used_size);
> > -
> >       if (checksum != amd_sriov_msg_checksum(tmp, used_size, 0, 0))
> >               goto out;
> >
>
diff mbox series

Patch

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c
index c704e9803e11..0af469ec6fcc 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c
@@ -1263,12 +1263,10 @@  static int amdgpu_virt_cache_host_error_counts(struct amdgpu_device *adev,
 	if (used_size > (AMD_SRIOV_RAS_TELEMETRY_SIZE_KB << 10))
 		return 0;
 
-	tmp = kmalloc(used_size, GFP_KERNEL);
+	tmp = kmemdup(&host_telemetry->body.error_count, used_size, GFP_KERNEL);
 	if (!tmp)
 		return -ENOMEM;
 
-	memcpy(tmp, &host_telemetry->body.error_count, used_size);
-
 	if (checksum != amd_sriov_msg_checksum(tmp, used_size, 0, 0))
 		goto out;