* [PATCH v2 04/35] block: Use kmemdup rather than duplicating its implementation
@ 2019-07-03 16:26 Fuqian Huang
2019-07-03 16:33 ` Jeff Layton
0 siblings, 1 reply; 2+ messages in thread
From: Fuqian Huang @ 2019-07-03 16:26 UTC (permalink / raw)
Cc: Ilya Dryomov, Sage Weil, Alex Elder, Jens Axboe, ceph-devel,
linux-block, linux-kernel, Fuqian Huang
kmemdup is introduced to duplicate a region of memory in a neat way.
Rather than kmalloc/kzalloc + memcpy, which the programmer needs to
write the size twice (sometimes lead to mistakes), kmemdup improves
readability, leads to smaller code and also reduce the chances of mistakes.
Suggestion to use kmemdup rather than using kmalloc/kzalloc + memcpy.
Signed-off-by: Fuqian Huang <huangfq.daxian@gmail.com>
---
Changes in v2:
- Fix a typo in commit message (memset -> memcpy)
drivers/block/rbd.c | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/drivers/block/rbd.c b/drivers/block/rbd.c
index e5009a34f9c2..47ad3772dc58 100644
--- a/drivers/block/rbd.c
+++ b/drivers/block/rbd.c
@@ -1068,7 +1068,7 @@ static int rbd_header_from_disk(struct rbd_device *rbd_dev,
if (snap_names_len > (u64)SIZE_MAX)
goto out_2big;
- snap_names = kmalloc(snap_names_len, GFP_KERNEL);
+ snap_names = kmemdup(&ondisk->snaps[snap_count], snap_names_len, GFP_KERNEL);
if (!snap_names)
goto out_err;
@@ -1088,7 +1088,6 @@ static int rbd_header_from_disk(struct rbd_device *rbd_dev,
* snap_names_len bytes beyond the end of the
* snapshot id array, this memcpy() is safe.
*/
- memcpy(snap_names, &ondisk->snaps[snap_count], snap_names_len);
snaps = ondisk->snaps;
for (i = 0; i < snap_count; i++) {
snapc->snaps[i] = le64_to_cpu(snaps[i].id);
--
2.11.0
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH v2 04/35] block: Use kmemdup rather than duplicating its implementation
2019-07-03 16:26 [PATCH v2 04/35] block: Use kmemdup rather than duplicating its implementation Fuqian Huang
@ 2019-07-03 16:33 ` Jeff Layton
0 siblings, 0 replies; 2+ messages in thread
From: Jeff Layton @ 2019-07-03 16:33 UTC (permalink / raw)
To: Fuqian Huang
Cc: Ilya Dryomov, Sage Weil, Alex Elder, Jens Axboe, ceph-devel,
linux-block, linux-kernel
On Thu, 2019-07-04 at 00:26 +0800, Fuqian Huang wrote:
> kmemdup is introduced to duplicate a region of memory in a neat way.
> Rather than kmalloc/kzalloc + memcpy, which the programmer needs to
> write the size twice (sometimes lead to mistakes), kmemdup improves
> readability, leads to smaller code and also reduce the chances of mistakes.
> Suggestion to use kmemdup rather than using kmalloc/kzalloc + memcpy.
>
> Signed-off-by: Fuqian Huang <huangfq.daxian@gmail.com>
> ---
> Changes in v2:
> - Fix a typo in commit message (memset -> memcpy)
>
> drivers/block/rbd.c | 3 +--
> 1 file changed, 1 insertion(+), 2 deletions(-)
>
> diff --git a/drivers/block/rbd.c b/drivers/block/rbd.c
> index e5009a34f9c2..47ad3772dc58 100644
> --- a/drivers/block/rbd.c
> +++ b/drivers/block/rbd.c
> @@ -1068,7 +1068,7 @@ static int rbd_header_from_disk(struct rbd_device *rbd_dev,
>
> if (snap_names_len > (u64)SIZE_MAX)
> goto out_2big;
> - snap_names = kmalloc(snap_names_len, GFP_KERNEL);
> + snap_names = kmemdup(&ondisk->snaps[snap_count], snap_names_len, GFP_KERNEL);
> if (!snap_names)
> goto out_err;
>
> @@ -1088,7 +1088,6 @@ static int rbd_header_from_disk(struct rbd_device *rbd_dev,
> * snap_names_len bytes beyond the end of the
> * snapshot id array, this memcpy() is safe.
> */
> - memcpy(snap_names, &ondisk->snaps[snap_count], snap_names_len);
> snaps = ondisk->snaps;
> for (i = 0; i < snap_count; i++) {
> snapc->snaps[i] = le64_to_cpu(snaps[i].id);
Reviewed-by: Jeff Layton <jlayton@kernel.org>
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2019-07-03 16:33 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-07-03 16:26 [PATCH v2 04/35] block: Use kmemdup rather than duplicating its implementation Fuqian Huang
2019-07-03 16:33 ` Jeff Layton
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®