Skip to contentSkip to conversation

drm/amdkfd: Replace kmalloc + copy_from_user with memdup_user

PATCH Applied Thorsten Blum 2 participants 4 messages Sep 9, 2025 – Sep 12, 2025

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/amdkfd: Replace kmalloc + copy_from_user with memdup_user

Replace kmalloc() followed by copy_from_user() with memdup_user() to improve and simplify kfd_criu_restore_queue().

No functional changes intended.

---
 .../amd/amdkfd/kfd_process_queue_manager.c    | 22 +++++--------------
 1 file changed, 6 insertions(+), 16 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c +6 -16
index 7fbb5c274ccc..70c17a12cadf 100644
@@ -1004,13 +1004,9 @@ int kfd_criu_restore_queue(struct kfd_process *p,
 	if (*priv_data_offset + sizeof(*q_data) > max_priv_data_size)
 		return -EINVAL;
 
-	q_data = kmalloc(sizeof(*q_data), GFP_KERNEL);
-	if (!q_data)
-		return -ENOMEM;
-
-	ret = copy_from_user(q_data, user_priv_ptr + *priv_data_offset, sizeof(*q_data));
-	if (ret) {
-		ret = -EFAULT;
+	q_data = memdup_user(user_priv_ptr + *priv_data_offset, sizeof(*q_data));
+	if (IS_ERR(q_data)) {
+		ret = PTR_ERR(q_data);
 		goto exit;
 	}
 
@@ -1022,15 +1018,9 @@ int kfd_criu_restore_queue(struct kfd_process *p,
 		goto exit;
 	}
 
-	q_extra_data = kmalloc(q_extra_data_size, GFP_KERNEL);
-	if (!q_extra_data) {
-		ret = -ENOMEM;
-		goto exit;
-	}
-
-	ret = copy_from_user(q_extra_data, user_priv_ptr + *priv_data_offset, q_extra_data_size);
-	if (ret) {
-		ret = -EFAULT;
+	q_extra_data = memdup_user(user_priv_ptr + *priv_data_offset, q_extra_data_size);
+	if (IS_ERR(q_extra_data)) {
+		ret = PTR_ERR(q_extra_data);
 		goto exit;
 	}
 
Signature · 1 line
2.51.0
Applied
#1 lore raw

Applied. Thanks!

Alex

Quoted 54 lines · Thorsten Blum

Replace kmalloc() followed by copy_from_user() with memdup_user() to improve and simplify kfd_criu_restore_queue().

No functional changes intended.

---
 .../amd/amdkfd/kfd_process_queue_manager.c    | 22 +++++--------------
 1 file changed, 6 insertions(+), 16 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c +6 -17
index 7fbb5c274ccc..70c17a12cadf 100644
@@ -1004,13 +1004,9 @@ int kfd_criu_restore_queue(struct kfd_process *p,
        if (*priv_data_offset + sizeof(*q_data) > max_priv_data_size)
                return -EINVAL;
 
-       q_data = kmalloc(sizeof(*q_data), GFP_KERNEL);
-       if (!q_data)
-               return -ENOMEM;
-
-       ret = copy_from_user(q_data, user_priv_ptr + *priv_data_offset, sizeof(*q_data));
-       if (ret) {
-               ret = -EFAULT;
+       q_data = memdup_user(user_priv_ptr + *priv_data_offset, sizeof(*q_data));
+       if (IS_ERR(q_data)) {
+               ret = PTR_ERR(q_data);
                goto exit;
        }
 
@@ -1022,15 +1018,9 @@ int kfd_criu_restore_queue(struct kfd_process *p,
                goto exit;
        }
 
-       q_extra_data = kmalloc(q_extra_data_size, GFP_KERNEL);
-       if (!q_extra_data) {
-               ret = -ENOMEM;
-               goto exit;
-       }
-
-       ret = copy_from_user(q_extra_data, user_priv_ptr + *priv_data_offset, q_extra_data_size);
-       if (ret) {
-               ret = -EFAULT;
+       q_extra_data = memdup_user(user_priv_ptr + *priv_data_offset, q_extra_data_size);
+       if (IS_ERR(q_extra_data)) {
+               ret = PTR_ERR(q_extra_data);
                goto exit;
        }
 
--

2.51.0

#2 lore raw

Hi Alex,

Quoted 11 lines · Alex Deucher

Applied. Thanks!

Quoted 8 lines · Thorsten Blum

Replace kmalloc() followed by copy_from_user() with memdup_user() to improve and simplify kfd_criu_restore_queue().

No functional changes intended.

---

I just learned that calling kfree() on an error pointer doesn't work, so this patch should probably be reverted/not applied.

Thanks,
Thorsten

#3 lore raw
Quoted 11 lines · Thorsten Blum

Hi Alex,

Quoted 7 lines · Alex Deucher

Applied. Thanks!

Quoted 4 lines · Thorsten Blum

Replace kmalloc() followed by copy_from_user() with memdup_user() to improve and simplify kfd_criu_restore_queue().

Quoted 7 lines
Quoted 4 lines
Quoted 4 lines

No functional changes intended.

---

I just learned that calling kfree() on an error pointer doesn't work, so this patch should probably be reverted/not applied.

Thanks for the heads up.

Alex