[PATCH] xfs: Use kmemdup() instead of kmalloc() and memcpy()

Thorsten Blum posted 1 patch 1 week, 4 days ago
fs/xfs/libxfs/xfs_attr_leaf.c | 5 ++---
fs/xfs/libxfs/xfs_dir2.c      | 3 +--
2 files changed, 3 insertions(+), 5 deletions(-)
[PATCH] xfs: Use kmemdup() instead of kmalloc() and memcpy()
Posted by Thorsten Blum 1 week, 4 days ago
Fixes the following two Coccinelle/coccicheck warnings reported by
memdup.cocci:

	xfs_dir2.c:343:15-22: WARNING opportunity for kmemdup
	xfs_attr_leaf.c:1062:13-20: WARNING opportunity for kmemdup

Signed-off-by: Thorsten Blum <thorsten.blum@toblux.com>
---
 fs/xfs/libxfs/xfs_attr_leaf.c | 5 ++---
 fs/xfs/libxfs/xfs_dir2.c      | 3 +--
 2 files changed, 3 insertions(+), 5 deletions(-)

diff --git a/fs/xfs/libxfs/xfs_attr_leaf.c b/fs/xfs/libxfs/xfs_attr_leaf.c
index ac904cc1a97b..7346ee9aa4ca 100644
--- a/fs/xfs/libxfs/xfs_attr_leaf.c
+++ b/fs/xfs/libxfs/xfs_attr_leaf.c
@@ -1059,12 +1059,11 @@ xfs_attr3_leaf_to_shortform(
 
 	trace_xfs_attr_leaf_to_sf(args);
 
-	tmpbuffer = kmalloc(args->geo->blksize, GFP_KERNEL | __GFP_NOFAIL);
+	tmpbuffer = kmemdup(bp->b_addr, args->geo->blksize,
+			GFP_KERNEL | __GFP_NOFAIL);
 	if (!tmpbuffer)
 		return -ENOMEM;
 
-	memcpy(tmpbuffer, bp->b_addr, args->geo->blksize);
-
 	leaf = (xfs_attr_leafblock_t *)tmpbuffer;
 	xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
 	entry = xfs_attr3_leaf_entryp(leaf);
diff --git a/fs/xfs/libxfs/xfs_dir2.c b/fs/xfs/libxfs/xfs_dir2.c
index 4821519efad4..3ebb959cdaf0 100644
--- a/fs/xfs/libxfs/xfs_dir2.c
+++ b/fs/xfs/libxfs/xfs_dir2.c
@@ -340,12 +340,11 @@ xfs_dir_cilookup_result(
 					!(args->op_flags & XFS_DA_OP_CILOOKUP))
 		return -EEXIST;
 
-	args->value = kmalloc(len,
+	args->value = kmemdup(name, len,
 			GFP_KERNEL | __GFP_NOLOCKDEP | __GFP_RETRY_MAYFAIL);
 	if (!args->value)
 		return -ENOMEM;
 
-	memcpy(args->value, name, len);
 	args->valuelen = len;
 	return -EEXIST;
 }
-- 
2.44.0
Re: [PATCH] xfs: Use kmemdup() instead of kmalloc() and memcpy()
Posted by Darrick J. Wong 1 week, 3 days ago
On Sat, Apr 27, 2024 at 12:00:47AM +0200, Thorsten Blum wrote:
> Fixes the following two Coccinelle/coccicheck warnings reported by
> memdup.cocci:
> 
> 	xfs_dir2.c:343:15-22: WARNING opportunity for kmemdup
> 	xfs_attr_leaf.c:1062:13-20: WARNING opportunity for kmemdup
> 
> Signed-off-by: Thorsten Blum <thorsten.blum@toblux.com>
> ---
>  fs/xfs/libxfs/xfs_attr_leaf.c | 5 ++---
>  fs/xfs/libxfs/xfs_dir2.c      | 3 +--
>  2 files changed, 3 insertions(+), 5 deletions(-)
> 
> diff --git a/fs/xfs/libxfs/xfs_attr_leaf.c b/fs/xfs/libxfs/xfs_attr_leaf.c
> index ac904cc1a97b..7346ee9aa4ca 100644
> --- a/fs/xfs/libxfs/xfs_attr_leaf.c
> +++ b/fs/xfs/libxfs/xfs_attr_leaf.c
> @@ -1059,12 +1059,11 @@ xfs_attr3_leaf_to_shortform(
>  
>  	trace_xfs_attr_leaf_to_sf(args);
>  
> -	tmpbuffer = kmalloc(args->geo->blksize, GFP_KERNEL | __GFP_NOFAIL);
> +	tmpbuffer = kmemdup(bp->b_addr, args->geo->blksize,
> +			GFP_KERNEL | __GFP_NOFAIL);
>  	if (!tmpbuffer)
>  		return -ENOMEM;
>  
> -	memcpy(tmpbuffer, bp->b_addr, args->geo->blksize);
> -

Please read the list before submitting a patch to a function that
already has a different change pending.

--D

>  	leaf = (xfs_attr_leafblock_t *)tmpbuffer;
>  	xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
>  	entry = xfs_attr3_leaf_entryp(leaf);
> diff --git a/fs/xfs/libxfs/xfs_dir2.c b/fs/xfs/libxfs/xfs_dir2.c
> index 4821519efad4..3ebb959cdaf0 100644
> --- a/fs/xfs/libxfs/xfs_dir2.c
> +++ b/fs/xfs/libxfs/xfs_dir2.c
> @@ -340,12 +340,11 @@ xfs_dir_cilookup_result(
>  					!(args->op_flags & XFS_DA_OP_CILOOKUP))
>  		return -EEXIST;
>  
> -	args->value = kmalloc(len,
> +	args->value = kmemdup(name, len,
>  			GFP_KERNEL | __GFP_NOLOCKDEP | __GFP_RETRY_MAYFAIL);
>  	if (!args->value)
>  		return -ENOMEM;
>  
> -	memcpy(args->value, name, len);
>  	args->valuelen = len;
>  	return -EEXIST;
>  }
> -- 
> 2.44.0
> 
>