Re: [PATCH] mm/slub: prevent pfmemalloc objects from entering the barn
From: hu.shengming
Date: Mon Jul 20 2026 - 04:43:53 EST
Hao wrote:
> On Sun, Jul 19, 2026 at 11:37:01AM +0800, hu.shengming@xxxxxxxxxx wrote:
> > From: Shengming Hu <hu.shengming@xxxxxxxxxx>
> >
> > kmem_cache_return_sheaf() may refill a partially consumed sheaf before
> > placing it in the barn. Without an explicit restriction, this refill may
> > draw objects from pfmemalloc slabs and consume emergency reserves.
> >
> > Add __GFP_NOMEMALLOC so returned sheaves are refilled only from normal
> > memory. If that fails, flush and free the sheaf instead.
> >
> > Signed-off-by: Shengming Hu <hu.shengming@xxxxxxxxxx>
> > ---
> > mm/slub.c | 2 +-
> > 1 file changed, 1 insertion(+), 1 deletion(-)
> >
> > diff --git a/mm/slub.c b/mm/slub.c
> > index 53b4976d3831..34f17ecbde87 100644
> > --- a/mm/slub.c
> > +++ b/mm/slub.c
> > @@ -5123,7 +5123,7 @@ void kmem_cache_return_sheaf(struct kmem_cache *s, gfp_t gfp,
> > * simply flush and free it.
> > */
> > if (!barn || data_race(barn->nr_full) >= MAX_FULL_SHEAVES ||
> > - refill_sheaf(s, sheaf, gfp)) {
> > + refill_sheaf(s, sheaf, gfp | __GFP_NOMEMALLOC)) {
>
> maybe we can add __GFP_NOWARN as this is just an refilling attempt?
> refilling failure could be acceptable.
>
Thanks for the review!
Good suggestion. Since refilling the sheaf is only a best-effort attempt
and failure is acceptable here, adding __GFP_NOWARN makes sense.
After taking another look, maybe we should also clear __GFP_NOFAIL if
it is present in the caller-provided GFP flags? Otherwise, the allocation
could retry indefinitely, which may not be appropriate for this optional
refill path.
--
With Best Regards,
Shengming
> > sheaf_flush_unused(s, sheaf);
> > free_empty_sheaf(s, sheaf);
> > return;
> > --
> > 2.25.1