public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] drm/amdkfd: Replace kzalloc + copy_from_user with memdup_user
@ 2025-09-08 21:31 Thorsten Blum
  2025-09-09 13:06 ` Alex Deucher
  0 siblings, 1 reply; 2+ messages in thread
From: Thorsten Blum @ 2025-09-08 21:31 UTC (permalink / raw)
  To: Felix Kuehling, Alex Deucher, Christian König, David Airlie,
	Simona Vetter
  Cc: Thorsten Blum, amd-gfx, dri-devel, linux-kernel

Replace kzalloc() followed by copy_from_user() with memdup_user() to
improve and simplify kfd_ioctl_set_cu_mask().

Return early if an error occurs and remove the obsolete 'out' label.

No functional changes intended.

Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
---
 drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 12 +++---------
 1 file changed, 3 insertions(+), 9 deletions(-)

diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
index 828a9ceef1e7..5d58a7bf309a 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
@@ -521,15 +521,10 @@ static int kfd_ioctl_set_cu_mask(struct file *filp, struct kfd_process *p,
 		cu_mask_size = sizeof(uint32_t) * (max_num_cus/32);
 	}
 
-	minfo.cu_mask.ptr = kzalloc(cu_mask_size, GFP_KERNEL);
-	if (!minfo.cu_mask.ptr)
-		return -ENOMEM;
-
-	retval = copy_from_user(minfo.cu_mask.ptr, cu_mask_ptr, cu_mask_size);
-	if (retval) {
+	minfo.cu_mask.ptr = memdup_user(cu_mask_ptr, cu_mask_size);
+	if (IS_ERR(minfo.cu_mask.ptr)) {
 		pr_debug("Could not copy CU mask from userspace");
-		retval = -EFAULT;
-		goto out;
+		return PTR_ERR(minfo.cu_mask.ptr);
 	}
 
 	mutex_lock(&p->mutex);
@@ -538,7 +533,6 @@ static int kfd_ioctl_set_cu_mask(struct file *filp, struct kfd_process *p,
 
 	mutex_unlock(&p->mutex);
 
-out:
 	kfree(minfo.cu_mask.ptr);
 	return retval;
 }
-- 
2.51.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] drm/amdkfd: Replace kzalloc + copy_from_user with memdup_user
  2025-09-08 21:31 [PATCH] drm/amdkfd: Replace kzalloc + copy_from_user with memdup_user Thorsten Blum
@ 2025-09-09 13:06 ` Alex Deucher
  0 siblings, 0 replies; 2+ messages in thread
From: Alex Deucher @ 2025-09-09 13:06 UTC (permalink / raw)
  To: Thorsten Blum
  Cc: Felix Kuehling, Alex Deucher, Christian König, David Airlie,
	Simona Vetter, amd-gfx, dri-devel, linux-kernel

Applied.  thanks!

Alex

On Mon, Sep 8, 2025 at 5:49 PM Thorsten Blum <thorsten.blum@linux.dev> wrote:
>
> Replace kzalloc() followed by copy_from_user() with memdup_user() to
> improve and simplify kfd_ioctl_set_cu_mask().
>
> Return early if an error occurs and remove the obsolete 'out' label.
>
> No functional changes intended.
>
> Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
> ---
>  drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 12 +++---------
>  1 file changed, 3 insertions(+), 9 deletions(-)
>
> diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
> index 828a9ceef1e7..5d58a7bf309a 100644
> --- a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
> +++ b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
> @@ -521,15 +521,10 @@ static int kfd_ioctl_set_cu_mask(struct file *filp, struct kfd_process *p,
>                 cu_mask_size = sizeof(uint32_t) * (max_num_cus/32);
>         }
>
> -       minfo.cu_mask.ptr = kzalloc(cu_mask_size, GFP_KERNEL);
> -       if (!minfo.cu_mask.ptr)
> -               return -ENOMEM;
> -
> -       retval = copy_from_user(minfo.cu_mask.ptr, cu_mask_ptr, cu_mask_size);
> -       if (retval) {
> +       minfo.cu_mask.ptr = memdup_user(cu_mask_ptr, cu_mask_size);
> +       if (IS_ERR(minfo.cu_mask.ptr)) {
>                 pr_debug("Could not copy CU mask from userspace");
> -               retval = -EFAULT;
> -               goto out;
> +               return PTR_ERR(minfo.cu_mask.ptr);
>         }
>
>         mutex_lock(&p->mutex);
> @@ -538,7 +533,6 @@ static int kfd_ioctl_set_cu_mask(struct file *filp, struct kfd_process *p,
>
>         mutex_unlock(&p->mutex);
>
> -out:
>         kfree(minfo.cu_mask.ptr);
>         return retval;
>  }
> --
> 2.51.0
>

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2025-09-09 13:06 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-09-08 21:31 [PATCH] drm/amdkfd: Replace kzalloc + copy_from_user with memdup_user Thorsten Blum
2025-09-09 13:06 ` Alex Deucher

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox