Skip to contentSkip to conversation

pds_fwctl: Replace kzalloc + copy_from_user with memdup_user in pdsfc_fw_rpc

PATCH Applied Thorsten Blum 4 participants 5 messages Sep 17, 2025 – Sep 22, 2025

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

PATCH 1/1 pds_fwctl: Replace kzalloc + copy_from_user with memdup_user in pdsfc_fw_rpc

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

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

No functional changes intended.

---
 drivers/fwctl/pds/main.c | 16 ++++------------
 1 file changed, 4 insertions(+), 12 deletions(-)
drivers/fwctl/pds/main.c +4 -12
index 9b9d1f6b5556..04d84f892fb8 100644
@@ -6,6 +6,7 @@
 #include <linux/pci.h>
 #include <linux/vmalloc.h>
 #include <linux/bitfield.h>
+#include <linux/string.h>
 
 #include <uapi/fwctl/fwctl.h>
 #include <uapi/fwctl/pds.h>
@@ -366,18 +367,10 @@ static void *pdsfc_fw_rpc(struct fwctl_uctx *uctx, enum fwctl_rpc_scope scope,
 		return ERR_PTR(err);
 
 	if (rpc->in.len > 0) {
-		in_payload = kzalloc(rpc->in.len, GFP_KERNEL);
-		if (!in_payload) {
-			dev_err(dev, "Failed to allocate in_payload\n");
-			err = -ENOMEM;
-			goto err_out;
-		}
-
-		if (copy_from_user(in_payload, u64_to_user_ptr(rpc->in.payload),
-				   rpc->in.len)) {
+		in_payload = memdup_user(u64_to_user_ptr(rpc->in.payload), rpc->in.len);
+		if (IS_ERR(in_payload)) {
 			dev_dbg(dev, "Failed to copy in_payload from user\n");
-			err = -EFAULT;
-			goto err_in_payload;
+			return in_payload;
 		}
 
 		in_payload_dma_addr = dma_map_single(dev->parent, in_payload,
@@ -453,7 +446,6 @@ static void *pdsfc_fw_rpc(struct fwctl_uctx *uctx, enum fwctl_rpc_scope scope,
 				 rpc->in.len, DMA_TO_DEVICE);
 err_in_payload:
 	kfree(in_payload);
-err_out:
 	if (err)
 		return ERR_PTR(err);
 
Signature · 1 line
2.51.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines · Thorsten Blum

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

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

No functional changes intended.

---
 drivers/fwctl/pds/main.c | 16 ++++------------
 1 file changed, 4 insertions(+), 12 deletions(-)

Looks OK to me

Jason

maintainer Reviewed-by on v1 1/1
#2 lore raw
In reply to Thorsten Blum
Quoted 8 lines · Thorsten Blum

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

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

No functional changes intended.

Quoted 46 lines
---
 drivers/fwctl/pds/main.c | 16 ++++------------
 1 file changed, 4 insertions(+), 12 deletions(-)
drivers/fwctl/pds/main.c +4 -12
index 9b9d1f6b5556..04d84f892fb8 100644
@@ -6,6 +6,7 @@
 #include <linux/pci.h>
 #include <linux/vmalloc.h>
 #include <linux/bitfield.h>
+#include <linux/string.h>
 
 #include <uapi/fwctl/fwctl.h>
 #include <uapi/fwctl/pds.h>
@@ -366,18 +367,10 @@ static void *pdsfc_fw_rpc(struct fwctl_uctx *uctx, enum fwctl_rpc_scope scope,
 		return ERR_PTR(err);
 
 	if (rpc->in.len > 0) {
-		in_payload = kzalloc(rpc->in.len, GFP_KERNEL);
-		if (!in_payload) {
-			dev_err(dev, "Failed to allocate in_payload\n");
-			err = -ENOMEM;
-			goto err_out;
-		}
-
-		if (copy_from_user(in_payload, u64_to_user_ptr(rpc->in.payload),
-				   rpc->in.len)) {
+		in_payload = memdup_user(u64_to_user_ptr(rpc->in.payload), rpc->in.len);
+		if (IS_ERR(in_payload)) {
 			dev_dbg(dev, "Failed to copy in_payload from user\n");
-			err = -EFAULT;
-			goto err_in_payload;
+			return in_payload;
 		}
 
 		in_payload_dma_addr = dma_map_single(dev->parent, in_payload,
@@ -453,7 +446,6 @@ static void *pdsfc_fw_rpc(struct fwctl_uctx *uctx, enum fwctl_rpc_scope scope,
 				 rpc->in.len, DMA_TO_DEVICE);
 err_in_payload:
 	kfree(in_payload);
-err_out:
 	if (err)
 		return ERR_PTR(err);
 
Tested-by on v1 1/1
#3 lore raw
In reply to Thorsten Blum
Quoted 6 lines · Thorsten Blum

Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.

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

Quoted 8 lines

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

No functional changes intended.

---
  drivers/fwctl/pds/main.c | 16 ++++------------
  1 file changed, 4 insertions(+), 12 deletions(-)

Thanks for the patch.

Quoted 46 lines
drivers/fwctl/pds/main.c +4 -13
index 9b9d1f6b5556..04d84f892fb8 100644
@@ -6,6 +6,7 @@
  #include <linux/pci.h>
  #include <linux/vmalloc.h>
  #include <linux/bitfield.h>
+#include <linux/string.h>
 
  #include <uapi/fwctl/fwctl.h>
  #include <uapi/fwctl/pds.h>
@@ -366,18 +367,10 @@ static void *pdsfc_fw_rpc(struct fwctl_uctx *uctx, enum fwctl_rpc_scope scope,
                 return ERR_PTR(err);
 
         if (rpc->in.len > 0) {
-               in_payload = kzalloc(rpc->in.len, GFP_KERNEL);
-               if (!in_payload) {
-                       dev_err(dev, "Failed to allocate in_payload\n");
-                       err = -ENOMEM;
-                       goto err_out;
-               }
-
-               if (copy_from_user(in_payload, u64_to_user_ptr(rpc->in.payload),
-                                  rpc->in.len)) {
+               in_payload = memdup_user(u64_to_user_ptr(rpc->in.payload), rpc->in.len);
+               if (IS_ERR(in_payload)) {
                         dev_dbg(dev, "Failed to copy in_payload from user\n");
-                       err = -EFAULT;
-                       goto err_in_payload;
+                       return in_payload;
                 }
 
                 in_payload_dma_addr = dma_map_single(dev->parent, in_payload,
@@ -453,7 +446,6 @@ static void *pdsfc_fw_rpc(struct fwctl_uctx *uctx, enum fwctl_rpc_scope scope,
                                  rpc->in.len, DMA_TO_DEVICE);
  err_in_payload:
         kfree(in_payload);
-err_out:
         if (err)
                 return ERR_PTR(err);
 
--

2.51.0

#4 lore raw
In reply to Thorsten Blum
Quoted 11 lines · Thorsten Blum

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

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

No functional changes intended.

---
 drivers/fwctl/pds/main.c | 16 ++++------------
 1 file changed, 4 insertions(+), 12 deletions(-)

Applied, thanks

Jason