diff mbox series

[1/1] fs/xfs: Add check to kmem_cache_zalloc()

Message ID 20250228024842.3739554-1-ruc_gongyuanjun@163.com (mailing list archive)
State New
Headers show
Series [1/1] fs/xfs: Add check to kmem_cache_zalloc() | expand

Commit Message

Yuanjun Gong Feb. 28, 2025, 2:48 a.m. UTC
Add check to the return value of kmem_cache_zalloc() in case it
fails.

Signed-off-by: Yuanjun Gong <ruc_gongyuanjun@163.com>
---
 fs/xfs/xfs_buf_item.c | 2 ++
 1 file changed, 2 insertions(+)

Comments

Julian Sun Feb. 28, 2025, 3:20 a.m. UTC | #1
On Fri, 2025-02-28 at 10:48 +0800, Yuanjun Gong wrote:
> > Add check to the return value of kmem_cache_zalloc() in case it
> > fails.
> > 
> > Signed-off-by: Yuanjun Gong <ruc_gongyuanjun@163.com>
> > ---
> >  fs/xfs/xfs_buf_item.c | 2 ++
> >  1 file changed, 2 insertions(+)
> > 
> > diff --git a/fs/xfs/xfs_buf_item.c b/fs/xfs/xfs_buf_item.c
> > index 47549cfa61cd..3c23223b7d70 100644
> > --- a/fs/xfs/xfs_buf_item.c
> > +++ b/fs/xfs/xfs_buf_item.c
> > @@ -880,6 +880,8 @@ xfs_buf_item_init(
> >         }
> >  
> >         bip = kmem_cache_zalloc(xfs_buf_item_cache, GFP_KERNEL |
> > __GFP_NOFAIL);
> > +       if (!bip)
> > +               return -ENOMEM;
> >         xfs_log_item_init(mp, &bip->bli_item, XFS_LI_BUF,
> > &xfs_buf_item_ops);
> >         bip->bli_buf = bp;
> >  

The __GFP_NOFAIL flag ensures that this allocation will not fail, as stated
in the comments in include/linux/gfp_types.h:
 
 %__GFP_NOFAIL: The VM implementation _must_ retry infinitely: the caller
 * cannot handle allocation failures. The allocation could block
 * indefinitely but will never return with failure. Testing for
 * failure is pointless.

Thanks,
diff mbox series

Patch

diff --git a/fs/xfs/xfs_buf_item.c b/fs/xfs/xfs_buf_item.c
index 47549cfa61cd..3c23223b7d70 100644
--- a/fs/xfs/xfs_buf_item.c
+++ b/fs/xfs/xfs_buf_item.c
@@ -880,6 +880,8 @@  xfs_buf_item_init(
 	}
 
 	bip = kmem_cache_zalloc(xfs_buf_item_cache, GFP_KERNEL | __GFP_NOFAIL);
+	if (!bip)
+		return -ENOMEM;
 	xfs_log_item_init(mp, &bip->bli_item, XFS_LI_BUF, &xfs_buf_item_ops);
 	bip->bli_buf = bp;