Re: [PATCH] xfs: remove dead NULL check after __GFP_NOFAIL allocation

From: Carlos Maiolino

Date: Thu Jun 25 2026 - 04:43:10 EST


On Wed, Jun 24, 2026 at 09:51:55PM +0800, Gou Hao wrote:
> kmalloc with the __GFP_NOFAIL flag will never return NULL, so the
> subsequent ENOMEM check is unreachable dead code. Remove it.
>
> Signed-off-by: Gou Hao <gouhao@xxxxxxxxxxxxx>

Looks fine.

Reviewed-by: Carlos Maiolino <cmaiolino@xxxxxxxxxx>


> ---
> fs/xfs/libxfs/xfs_dir2.c | 12 ------------
> fs/xfs/libxfs/xfs_exchmaps.c | 5 -----
> fs/xfs/xfs_buf.c | 3 ---
> fs/xfs/xfs_mru_cache.c | 8 --------
> 4 files changed, 28 deletions(-)
>
> diff --git a/fs/xfs/libxfs/xfs_dir2.c b/fs/xfs/libxfs/xfs_dir2.c
> index 0c0402a29b6b..6009ec26843d 100644
> --- a/fs/xfs/libxfs/xfs_dir2.c
> +++ b/fs/xfs/libxfs/xfs_dir2.c
> @@ -249,9 +249,6 @@ xfs_dir_init(
> return error;
>
> args = kzalloc_obj(*args, GFP_KERNEL | __GFP_NOFAIL);
> - if (!args)
> - return -ENOMEM;
> -
> args->geo = dp->i_mount->m_dir_geo;
> args->dp = dp;
> args->trans = tp;
> @@ -342,9 +339,6 @@ xfs_dir_createname(
> }
>
> args = kzalloc_obj(*args, GFP_KERNEL | __GFP_NOFAIL);
> - if (!args)
> - return -ENOMEM;
> -
> args->geo = dp->i_mount->m_dir_geo;
> args->name = name->name;
> args->namelen = name->len;
> @@ -503,9 +497,6 @@ xfs_dir_removename(
> XFS_STATS_INC(dp->i_mount, xs_dir_remove);
>
> args = kzalloc_obj(*args, GFP_KERNEL | __GFP_NOFAIL);
> - if (!args)
> - return -ENOMEM;
> -
> args->geo = dp->i_mount->m_dir_geo;
> args->name = name->name;
> args->namelen = name->len;
> @@ -563,9 +554,6 @@ xfs_dir_replace(
> return rval;
>
> args = kzalloc_obj(*args, GFP_KERNEL | __GFP_NOFAIL);
> - if (!args)
> - return -ENOMEM;
> -
> args->geo = dp->i_mount->m_dir_geo;
> args->name = name->name;
> args->namelen = name->len;
> diff --git a/fs/xfs/libxfs/xfs_exchmaps.c b/fs/xfs/libxfs/xfs_exchmaps.c
> index dcd0bd0b13b4..bd897676e38a 100644
> --- a/fs/xfs/libxfs/xfs_exchmaps.c
> +++ b/fs/xfs/libxfs/xfs_exchmaps.c
> @@ -500,11 +500,6 @@ xfs_exchmaps_link_to_sf(
> /* Read the current symlink target into a buffer. */
> buf = kmalloc(ip->i_disk_size + 1,
> GFP_KERNEL | __GFP_NOLOCKDEP | __GFP_NOFAIL);
> - if (!buf) {
> - ASSERT(0);
> - return -ENOMEM;
> - }
> -
> error = xfs_symlink_remote_read(ip, buf);
> if (error)
> goto free;
> diff --git a/fs/xfs/xfs_buf.c b/fs/xfs/xfs_buf.c
> index 3ce12fe1c307..5583139d7478 100644
> --- a/fs/xfs/xfs_buf.c
> +++ b/fs/xfs/xfs_buf.c
> @@ -130,9 +130,6 @@ xfs_buf_alloc_kmem(
> ASSERT(size < PAGE_SIZE);
>
> bp->b_addr = kmalloc(size, gfp_mask | __GFP_NOFAIL);
> - if (!bp->b_addr)
> - return -ENOMEM;
> -
> /*
> * Slab guarantees that we get back naturally aligned allocations for
> * power of two sizes. Keep this check as the canary in the coal mine
> diff --git a/fs/xfs/xfs_mru_cache.c b/fs/xfs/xfs_mru_cache.c
> index d61ec8cb126d..258c07da73e1 100644
> --- a/fs/xfs/xfs_mru_cache.c
> +++ b/fs/xfs/xfs_mru_cache.c
> @@ -334,18 +334,10 @@ xfs_mru_cache_create(
> return -EINVAL;
>
> mru = kzalloc_obj(*mru, GFP_KERNEL | __GFP_NOFAIL);
> - if (!mru)
> - return -ENOMEM;
> -
> /* An extra list is needed to avoid reaping up to a grp_time early. */
> mru->grp_count = grp_count + 1;
> mru->lists = kzalloc(mru->grp_count * sizeof(*mru->lists),
> GFP_KERNEL | __GFP_NOFAIL);
> - if (!mru->lists) {
> - kfree(mru);
> - return -ENOMEM;
> - }
> -
> for (grp = 0; grp < mru->grp_count; grp++)
> INIT_LIST_HEAD(mru->lists + grp);
>
> --
> 2.20.1
>
>