From: Kees Cook Date: Sun, 22 Feb 2026 22:41:11 +0000 (-0800) Subject: treewide: Replace kmalloc with kmalloc_obj for non-scalar types X-Git-Tag: v7.0.0~69 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=7c7f99c57a13c49b13ff9fbdd922e4a6aa48bfc1;p=xfsprogs-dev.git treewide: Replace kmalloc with kmalloc_obj for non-scalar types Source kernel commit: 69050f8d6d075dc01af7a5f2f550a8067510366f This is the result of running the Coccinelle script from scripts/coccinelle/api/kmalloc_objs.cocci. The script is designed to avoid scalar types (which need careful case-by-case checking), and instead replace kmalloc-family calls that allocate struct or union object instances: Single allocations: kmalloc(sizeof(TYPE), ...) are replaced with: kmalloc_obj(TYPE, ...) Array allocations: kmalloc_array(COUNT, sizeof(TYPE), ...) are replaced with: kmalloc_objs(TYPE, COUNT, ...) Flex array allocations: kmalloc(struct_size(PTR, FAM, COUNT), ...) are replaced with: kmalloc_flex(*PTR, FAM, COUNT, ...) (where TYPE may also be *VAR) The resulting allocations no longer return "void *", instead returning "TYPE *". Signed-off-by: Kees Cook --- diff --git a/libxfs/xfs_ag.c b/libxfs/xfs_ag.c index 8ccd6767..0f1eaf5d 100644 --- a/libxfs/xfs_ag.c +++ b/libxfs/xfs_ag.c @@ -222,7 +222,7 @@ xfs_perag_alloc( struct xfs_perag *pag; int error; - pag = kzalloc(sizeof(*pag), GFP_KERNEL); + pag = kzalloc_obj(*pag, GFP_KERNEL); if (!pag) return -ENOMEM; diff --git a/libxfs/xfs_defer.c b/libxfs/xfs_defer.c index c0d3aa40..e4d17177 100644 --- a/libxfs/xfs_defer.c +++ b/libxfs/xfs_defer.c @@ -975,7 +975,7 @@ xfs_defer_ops_capture( return ERR_PTR(error); /* Create an object to capture the defer ops. */ - dfc = kzalloc(sizeof(*dfc), GFP_KERNEL | __GFP_NOFAIL); + dfc = kzalloc_obj(*dfc, GFP_KERNEL | __GFP_NOFAIL); INIT_LIST_HEAD(&dfc->dfc_list); INIT_LIST_HEAD(&dfc->dfc_dfops); diff --git a/libxfs/xfs_dir2.c b/libxfs/xfs_dir2.c index 2ca6bf19..1a13fec2 100644 --- a/libxfs/xfs_dir2.c +++ b/libxfs/xfs_dir2.c @@ -115,10 +115,10 @@ xfs_da_mount( ASSERT(mp->m_sb.sb_versionnum & XFS_SB_VERSION_DIRV2BIT); ASSERT(xfs_dir2_dirblock_bytes(&mp->m_sb) <= XFS_MAX_BLOCKSIZE); - mp->m_dir_geo = kzalloc(sizeof(struct xfs_da_geometry), - GFP_KERNEL | __GFP_RETRY_MAYFAIL); - mp->m_attr_geo = kzalloc(sizeof(struct xfs_da_geometry), - GFP_KERNEL | __GFP_RETRY_MAYFAIL); + mp->m_dir_geo = kzalloc_obj(struct xfs_da_geometry, + GFP_KERNEL | __GFP_RETRY_MAYFAIL); + mp->m_attr_geo = kzalloc_obj(struct xfs_da_geometry, + GFP_KERNEL | __GFP_RETRY_MAYFAIL); if (!mp->m_dir_geo || !mp->m_attr_geo) { kfree(mp->m_dir_geo); kfree(mp->m_attr_geo); @@ -247,7 +247,7 @@ xfs_dir_init( if (error) return error; - args = kzalloc(sizeof(*args), GFP_KERNEL | __GFP_NOFAIL); + args = kzalloc_obj(*args, GFP_KERNEL | __GFP_NOFAIL); if (!args) return -ENOMEM; @@ -340,7 +340,7 @@ xfs_dir_createname( XFS_STATS_INC(dp->i_mount, xs_dir_create); } - args = kzalloc(sizeof(*args), GFP_KERNEL | __GFP_NOFAIL); + args = kzalloc_obj(*args, GFP_KERNEL | __GFP_NOFAIL); if (!args) return -ENOMEM; @@ -436,8 +436,7 @@ xfs_dir_lookup( ASSERT(S_ISDIR(VFS_I(dp)->i_mode)); XFS_STATS_INC(dp->i_mount, xs_dir_lookup); - args = kzalloc(sizeof(*args), - GFP_KERNEL | __GFP_NOLOCKDEP | __GFP_NOFAIL); + args = kzalloc_obj(*args, GFP_KERNEL | __GFP_NOLOCKDEP | __GFP_NOFAIL); args->geo = dp->i_mount->m_dir_geo; args->name = name->name; args->namelen = name->len; @@ -502,7 +501,7 @@ xfs_dir_removename( ASSERT(S_ISDIR(VFS_I(dp)->i_mode)); XFS_STATS_INC(dp->i_mount, xs_dir_remove); - args = kzalloc(sizeof(*args), GFP_KERNEL | __GFP_NOFAIL); + args = kzalloc_obj(*args, GFP_KERNEL | __GFP_NOFAIL); if (!args) return -ENOMEM; @@ -562,7 +561,7 @@ xfs_dir_replace( if (rval) return rval; - args = kzalloc(sizeof(*args), GFP_KERNEL | __GFP_NOFAIL); + args = kzalloc_obj(*args, GFP_KERNEL | __GFP_NOFAIL); if (!args) return -ENOMEM; diff --git a/libxfs/xfs_refcount.c b/libxfs/xfs_refcount.c index 5c362e0e..036497db 100644 --- a/libxfs/xfs_refcount.c +++ b/libxfs/xfs_refcount.c @@ -2031,8 +2031,8 @@ xfs_refcount_recover_extent( return -EFSCORRUPTED; } - rr = kmalloc(sizeof(struct xfs_refcount_recovery), - GFP_KERNEL | __GFP_NOFAIL); + rr = kmalloc_obj(struct xfs_refcount_recovery, + GFP_KERNEL | __GFP_NOFAIL); INIT_LIST_HEAD(&rr->rr_list); xfs_refcount_btrec_to_irec(rec, &rr->rr_rrec); diff --git a/libxfs/xfs_rtgroup.c b/libxfs/xfs_rtgroup.c index af4eafb2..69e71494 100644 --- a/libxfs/xfs_rtgroup.c +++ b/libxfs/xfs_rtgroup.c @@ -95,7 +95,7 @@ xfs_rtgroup_alloc( struct xfs_rtgroup *rtg; int error; - rtg = kzalloc(sizeof(struct xfs_rtgroup), GFP_KERNEL); + rtg = kzalloc_obj(struct xfs_rtgroup, GFP_KERNEL); if (!rtg) return -ENOMEM;