fs/xfs/libxfs/xfs_attr_leaf.c:1061:13-20: WARNING opportunity for kmemdup

From: kernel test robot
Date: Wed Mar 20 2024 - 14:40:50 EST


tree: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git master
head: a4145ce1e7bc247fd6f2846e8699473448717b37
commit: f078d4ea827607867d42fb3b2ef907caf86ce49d xfs: convert kmem_alloc() to kmalloc()
date: 5 weeks ago
config: x86_64-randconfig-104-20240320 (https://download.01.org/0day-ci/archive/20240321/202403210204.LPPBJMhf-lkp@xxxxxxxxx/config)
compiler: clang version 17.0.6 (https://github.com/llvm/llvm-project 6009708b4367171ccdbf4b5905cb6a803753fe18)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@xxxxxxxxx>
| Closes: https://lore.kernel.org/oe-kbuild-all/202403210204.LPPBJMhf-lkp@xxxxxxxxx/

cocci warnings: (new ones prefixed by >>)
>> fs/xfs/libxfs/xfs_attr_leaf.c:1061:13-20: WARNING opportunity for kmemdup

vim +1061 fs/xfs/libxfs/xfs_attr_leaf.c

1039
1040 /*
1041 * Convert a leaf attribute list to shortform attribute list
1042 */
1043 int
1044 xfs_attr3_leaf_to_shortform(
1045 struct xfs_buf *bp,
1046 struct xfs_da_args *args,
1047 int forkoff)
1048 {
1049 struct xfs_attr_leafblock *leaf;
1050 struct xfs_attr3_icleaf_hdr ichdr;
1051 struct xfs_attr_leaf_entry *entry;
1052 struct xfs_attr_leaf_name_local *name_loc;
1053 struct xfs_da_args nargs;
1054 struct xfs_inode *dp = args->dp;
1055 char *tmpbuffer;
1056 int error;
1057 int i;
1058
1059 trace_xfs_attr_leaf_to_sf(args);
1060
> 1061 tmpbuffer = kmalloc(args->geo->blksize, GFP_KERNEL | __GFP_NOFAIL);
1062 if (!tmpbuffer)
1063 return -ENOMEM;
1064
1065 memcpy(tmpbuffer, bp->b_addr, args->geo->blksize);
1066
1067 leaf = (xfs_attr_leafblock_t *)tmpbuffer;
1068 xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
1069 entry = xfs_attr3_leaf_entryp(leaf);
1070
1071 /* XXX (dgc): buffer is about to be marked stale - why zero it? */
1072 memset(bp->b_addr, 0, args->geo->blksize);
1073
1074 /*
1075 * Clean out the prior contents of the attribute list.
1076 */
1077 error = xfs_da_shrink_inode(args, 0, bp);
1078 if (error)
1079 goto out;
1080
1081 if (forkoff == -1) {
1082 /*
1083 * Don't remove the attr fork if this operation is the first
1084 * part of a attr replace operations. We're going to add a new
1085 * attr immediately, so we need to keep the attr fork around in
1086 * this case.
1087 */
1088 if (!(args->op_flags & XFS_DA_OP_REPLACE)) {
1089 ASSERT(xfs_has_attr2(dp->i_mount));
1090 ASSERT(dp->i_df.if_format != XFS_DINODE_FMT_BTREE);
1091 xfs_attr_fork_remove(dp, args->trans);
1092 }
1093 goto out;
1094 }
1095
1096 xfs_attr_shortform_create(args);
1097
1098 /*
1099 * Copy the attributes
1100 */
1101 memset((char *)&nargs, 0, sizeof(nargs));
1102 nargs.geo = args->geo;
1103 nargs.dp = dp;
1104 nargs.total = args->total;
1105 nargs.whichfork = XFS_ATTR_FORK;
1106 nargs.trans = args->trans;
1107 nargs.op_flags = XFS_DA_OP_OKNOENT;
1108
1109 for (i = 0; i < ichdr.count; entry++, i++) {
1110 if (entry->flags & XFS_ATTR_INCOMPLETE)
1111 continue; /* don't copy partial entries */
1112 if (!entry->nameidx)
1113 continue;
1114 ASSERT(entry->flags & XFS_ATTR_LOCAL);
1115 name_loc = xfs_attr3_leaf_name_local(leaf, i);
1116 nargs.name = name_loc->nameval;
1117 nargs.namelen = name_loc->namelen;
1118 nargs.value = &name_loc->nameval[nargs.namelen];
1119 nargs.valuelen = be16_to_cpu(name_loc->valuelen);
1120 nargs.hashval = be32_to_cpu(entry->hashval);
1121 nargs.attr_filter = entry->flags & XFS_ATTR_NSP_ONDISK_MASK;
1122 xfs_attr_shortform_add(&nargs, forkoff);
1123 }
1124 error = 0;
1125
1126 out:
1127 kmem_free(tmpbuffer);
1128 return error;
1129 }
1130

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki