]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
pds_fwctl: Replace kzalloc + copy_from_user with memdup_user in pdsfc_fw_rpc
authorThorsten Blum <thorsten.blum@linux.dev>
Wed, 17 Sep 2025 15:09:41 +0000 (17:09 +0200)
committerJason Gunthorpe <jgg@nvidia.com>
Mon, 22 Sep 2025 13:33:10 +0000 (10:33 -0300)
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.

Link: https://patch.msgid.link/r/20250917150941.168887-1-thorsten.blum@linux.dev
Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
Reviewed-by: Dave Jiang <dave.jiang@intel.com>
Reviewed-by: Jason Gunthorpe <jgg@nvidia.com>
Tested-by: Brett Creeley <brett.creeley@amd.com>
Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
drivers/fwctl/pds/main.c

index 8dd659aee2563709da7f474b0189de20e8d7cd01..1809853f635389195bf4d10c490518d2a35fc489 100644 (file)
@@ -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 @@ err_out_payload:
                                 rpc->in.len, DMA_TO_DEVICE);
 err_in_payload:
        kfree(in_payload);
-err_out:
        if (err)
                return ERR_PTR(err);