A __GFP_NOFAIL slab allocation in a low memory situation may end up refilling a whole sheaf from slabs (possibly newly allocated), passing the __GFP_NOFAIL to all underlying allocations. This can make the low memory condition worse and it's unnecessary, given there's a fallback. Given how we use __GFP_MEMALLOC for the refill, there are also some undesirable corner cases, as reported by Chris Mason. If the allocation happens in a PF_MEMALLOC task (which it should not in the first place, but there's no guarantee), the following can occur: refill_sheaf(gfp | __GFP_NOMEMALLOC) __alloc_pages_slowpath() WARN_ON_ONCE(nofail && PF_MEMALLOC) retry: reserve_flags = 0 /* __GFP_NOMEMALLOC */ goto nopage /* PF_MEMALLOC */ goto retry /* __GFP_NOFAIL */ The allocation will loop without reclaim and no access to memory reserves. Additionally the slab page may be marked pfmemalloc, which the sheaf refill does not expect and handle: refill_objects() __GFP_NOFAIL | __GFP_NOMEMALLOC new_slab() __alloc_pages_may_oom() if (gfp_mask & __GFP_NOFAIL) ALLOC_NO_WATERMARKS the slab is then pfmemalloc alloc_from_new_slab() its objects go into the sheaf Improve or prevent all of the above by dropping __GFP_NOFAIL for the sheaf refill. Also perform all adjustments before potentially calling alloc_empty_sheaf(). While that one already clears __GFP_NOFAIL, it should also get the __GFP_NOMEMALLOC | __GFP_NOWARN flags, since a fallback exists. One consequence is that the WARN_ON_ONCE(nofail && PF_MEMALLOC) in the page allocator will now only be reachable from the fallback and thus in case such a caller is introduced, might lurk undetected for a while. If people think it's an issue, we can duplicate the warning here as well. Reported-by: Chris Mason Fixes: 1ce20c28eafd ("slab: handle pfmemalloc slabs properly with sheaves") Cc: Signed-off-by: Vlastimil Babka (SUSE) --- mm/slub.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/mm/slub.c b/mm/slub.c index 145a32c334cb..5fd148f11efd 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -4807,13 +4807,20 @@ __pcs_replace_empty_main(struct kmem_cache *s, struct slub_percpu_sheaves *pcs, if (!allow_spin) return NULL; + /* + * We don't want refills to rely on __GFP_NOFAIL or memalloc reserves to + * succeed, nor to warn on failure. Leave that to the fallback. + */ + gfp &= ~__GFP_NOFAIL; + gfp |= __GFP_NOMEMALLOC | __GFP_NOWARN; + if (!empty) { empty = alloc_empty_sheaf(s, gfp, alloc_flags); if (!empty) return NULL; } - if (refill_sheaf(s, empty, gfp | __GFP_NOMEMALLOC | __GFP_NOWARN)) { + if (refill_sheaf(s, empty, gfp)) { /* * we must be very low on memory so don't bother * with the barn -- 2.56.1