From: Mirsad Todorovac Date: Mon, 24 Feb 2025 18:21:56 +0000 (-0800) Subject: xfs/libxfs: replace kmalloc() and memcpy() with kmemdup() X-Git-Tag: v6.14.0~89 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=dce20b26c5b0d9a3f26932c514bb28a037debca0;p=thirdparty%2Fxfsprogs-dev.git xfs/libxfs: replace kmalloc() and memcpy() with kmemdup() Source kernel commit: 9d9b72472631262b35157f1a650f066c0e11c2bb The source static analysis tool gave the following advice: ./fs/xfs/libxfs/xfs_dir2.c:382:15-22: WARNING opportunity for kmemdup → 382 args->value = kmalloc(len, 383 GFP_KERNEL | __GFP_NOLOCKDEP | __GFP_RETRY_MAYFAIL); 384 if (!args->value) 385 return -ENOMEM; 386 → 387 memcpy(args->value, name, len); 388 args->valuelen = len; 389 return -EEXIST; Replacing kmalloc() + memcpy() with kmemdump() doesn't change semantics. Original code works without fault, so this is not a bug fix but proposed improvement. Link: https://lwn.net/Articles/198928/ Fixes: 94a69db2367ef ("xfs: use __GFP_NOLOCKDEP instead of GFP_NOFS") Fixes: 384f3ced07efd ("[XFS] Return case-insensitive match for dentry cache") Fixes: 2451337dd0439 ("xfs: global error sign conversion") Cc: Carlos Maiolino Cc: Darrick J. Wong Cc: Chandan Babu R Cc: Dave Chinner Cc: linux-xfs@vger.kernel.org Cc: linux-kernel@vger.kernel.org Reviewed-by: "Darrick J. Wong" Signed-off-by: Mirsad Todorovac Signed-off-by: Carlos Maiolino --- diff --git a/include/kmem.h b/include/kmem.h index 16a7957f..66f8b1fb 100644 --- a/include/kmem.h +++ b/include/kmem.h @@ -79,4 +79,13 @@ static inline void kfree_rcu_mightsleep(const void *ptr) __attribute__((format(printf,2,3))) char *kasprintf(gfp_t gfp, const char *fmt, ...); +static inline void *kmemdup(const void *src, size_t len, gfp_t gfp) +{ + void *p = kmalloc(len, gfp); + + if (p) + memcpy(p, src, len); + return p; +} + #endif diff --git a/libxfs/xfs_dir2.c b/libxfs/xfs_dir2.c index 29e64603..1285019b 100644 --- a/libxfs/xfs_dir2.c +++ b/libxfs/xfs_dir2.c @@ -378,12 +378,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; }