From: Thorsten Blum <thorsten.blum@linux.dev>
To: Don Brace <don.brace@microchip.com>,
"James E.J. Bottomley" <James.Bottomley@HansenPartnership.com>,
"Martin K. Petersen" <martin.petersen@oracle.com>
Cc: Thorsten Blum <thorsten.blum@linux.dev>,
storagedev@microchip.com, linux-scsi@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: [PATCH] scsi: hpsa: Replace kmalloc + copy_from_user with memdup_user
Date: Tue, 9 Sep 2025 17:27:33 +0200 [thread overview]
Message-ID: <20250909152732.760975-3-thorsten.blum@linux.dev> (raw)
Replace kmalloc() followed by copy_from_user() with memdup_user() to
improve and simplify hpsa_passthru_ioctl().
Since memdup_user() already allocates memory, use kzalloc() in the else
branch instead of manually zeroing 'buff' using memset(0).
Return early if an error occurs and remove the 'out_kfree' label.
No functional changes intended.
Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
---
drivers/scsi/hpsa.c | 17 ++++++-----------
1 file changed, 6 insertions(+), 11 deletions(-)
diff --git a/drivers/scsi/hpsa.c b/drivers/scsi/hpsa.c
index c73a71ac3c29..55448238d8ed 100644
--- a/drivers/scsi/hpsa.c
+++ b/drivers/scsi/hpsa.c
@@ -6407,18 +6407,14 @@ static int hpsa_passthru_ioctl(struct ctlr_info *h,
return -EINVAL;
}
if (iocommand->buf_size > 0) {
- buff = kmalloc(iocommand->buf_size, GFP_KERNEL);
- if (buff == NULL)
- return -ENOMEM;
if (iocommand->Request.Type.Direction & XFER_WRITE) {
- /* Copy the data into the buffer we created */
- if (copy_from_user(buff, iocommand->buf,
- iocommand->buf_size)) {
- rc = -EFAULT;
- goto out_kfree;
- }
+ buff = memdup_user(iocommand->buf, iocommand->buf_size);
+ if (IS_ERR(buff))
+ return PTR_ERR(buff);
} else {
- memset(buff, 0, iocommand->buf_size);
+ buff = kzalloc(iocommand->buf_size, GFP_KERNEL);
+ if (!buff)
+ return -ENOMEM;
}
}
c = cmd_alloc(h);
@@ -6478,7 +6474,6 @@ static int hpsa_passthru_ioctl(struct ctlr_info *h,
}
out:
cmd_free(h, c);
-out_kfree:
kfree(buff);
return rc;
}
--
2.51.0
reply other threads:[~2025-09-09 15:29 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250909152732.760975-3-thorsten.blum@linux.dev \
--to=thorsten.blum@linux.dev \
--cc=James.Bottomley@HansenPartnership.com \
--cc=don.brace@microchip.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-scsi@vger.kernel.org \
--cc=martin.petersen@oracle.com \
--cc=storagedev@microchip.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.