Skip to content

Commit 903edea

Browse files
Barry Songakpm00
authored andcommitted
mm: warn about illegal __GFP_NOFAIL usage in a more appropriate location and manner
Three points for this change: 1. We should consolidate all warnings in one place. Currently, the order > 1 warning is in the hotpath, while others are in less likely scenarios. Moving all warnings to the slowpath will reduce the overhead for order > 1 and increase the visibility of other warnings. 2. We currently have two warnings for order: one for order > 1 in the hotpath and another for order > costly_order in the laziest path. I suggest standardizing on order > 1 since it's been in use for a long time. 3. We don't need to check for __GFP_NOWARN in this case. __GFP_NOWARN is meant to suppress allocation failure reports, but here we're dealing with bug detection, not allocation failures. So replace WARN_ON_ONCE_GFP by WARN_ON_ONCE. [v-songbaohua@oppo.com: also update the doc for __GFP_NOFAIL with order > 1] Link: https://lkml.kernel.org/r/20240903223935.1697-1-21cnbao@gmail.com Link: https://lkml.kernel.org/r/20240830202823.21478-4-21cnbao@gmail.com Signed-off-by: Barry Song <v-songbaohua@oppo.com> Suggested-by: Vlastimil Babka <vbabka@suse.cz> Reviewed-by: Vlastimil Babka <vbabka@suse.cz> Acked-by: David Hildenbrand <david@redhat.com> Acked-by: Michal Hocko <mhocko@suse.com> Cc: Christoph Hellwig <hch@lst.de> Cc: Christoph Lameter <cl@linux.com> Cc: Davidlohr Bueso <dave@stgolabs.net> Cc: David Rientjes <rientjes@google.com> Cc: "Eugenio Pérez" <eperezma@redhat.com> Cc: Hailong.Liu <hailong.liu@oppo.com> Cc: Hyeonggon Yoo <42.hyeyoo@gmail.com> Cc: Jason Wang <jasowang@redhat.com> Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com> Cc: Kees Cook <kees@kernel.org> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Lorenzo Stoakes <lorenzo.stoakes@oracle.com> Cc: Maxime Coquelin <maxime.coquelin@redhat.com> Cc: "Michael S. Tsirkin" <mst@redhat.com> Cc: Pekka Enberg <penberg@kernel.org> Cc: Roman Gushchin <roman.gushchin@linux.dev> Cc: Uladzislau Rezki (Sony) <urezki@gmail.com> Cc: Xie Yongji <xieyongji@bytedance.com> Cc: Xuan Zhuo <xuanzhuo@linux.alibaba.com> Cc: Yafang Shao <laoar.shao@gmail.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent 17d7542 commit 903edea

File tree

2 files changed

+27
-26
lines changed

2 files changed

+27
-26
lines changed

include/linux/gfp_types.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,8 @@ enum {
253253
* used only when there is no reasonable failure policy) but it is
254254
* definitely preferable to use the flag rather than opencode endless
255255
* loop around allocator.
256-
* Using this flag for costly allocations is _highly_ discouraged.
256+
* Allocating pages from the buddy with __GFP_NOFAIL and order > 1 is
257+
* not supported. Please consider using kvmalloc() instead.
257258
*/
258259
#define __GFP_IO ((__force gfp_t)___GFP_IO)
259260
#define __GFP_FS ((__force gfp_t)___GFP_FS)

mm/page_alloc.c

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3032,12 +3032,6 @@ struct page *rmqueue(struct zone *preferred_zone,
30323032
{
30333033
struct page *page;
30343034

3035-
/*
3036-
* We most definitely don't want callers attempting to
3037-
* allocate greater than order-1 page units with __GFP_NOFAIL.
3038-
*/
3039-
WARN_ON_ONCE((gfp_flags & __GFP_NOFAIL) && (order > 1));
3040-
30413035
if (likely(pcp_allowed_order(order))) {
30423036
page = rmqueue_pcplist(preferred_zone, zone, order,
30433037
migratetype, alloc_flags);
@@ -4175,6 +4169,7 @@ __alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order,
41754169
{
41764170
bool can_direct_reclaim = gfp_mask & __GFP_DIRECT_RECLAIM;
41774171
bool can_compact = gfp_compaction_allowed(gfp_mask);
4172+
bool nofail = gfp_mask & __GFP_NOFAIL;
41784173
const bool costly_order = order > PAGE_ALLOC_COSTLY_ORDER;
41794174
struct page *page = NULL;
41804175
unsigned int alloc_flags;
@@ -4187,6 +4182,25 @@ __alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order,
41874182
unsigned int zonelist_iter_cookie;
41884183
int reserve_flags;
41894184

4185+
if (unlikely(nofail)) {
4186+
/*
4187+
* We most definitely don't want callers attempting to
4188+
* allocate greater than order-1 page units with __GFP_NOFAIL.
4189+
*/
4190+
WARN_ON_ONCE(order > 1);
4191+
/*
4192+
* Also we don't support __GFP_NOFAIL without __GFP_DIRECT_RECLAIM,
4193+
* otherwise, we may result in lockup.
4194+
*/
4195+
WARN_ON_ONCE(!can_direct_reclaim);
4196+
/*
4197+
* PF_MEMALLOC request from this context is rather bizarre
4198+
* because we cannot reclaim anything and only can loop waiting
4199+
* for somebody to do a work for us.
4200+
*/
4201+
WARN_ON_ONCE(current->flags & PF_MEMALLOC);
4202+
}
4203+
41904204
restart:
41914205
compaction_retries = 0;
41924206
no_progress_loops = 0;
@@ -4404,29 +4418,15 @@ __alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order,
44044418
* Make sure that __GFP_NOFAIL request doesn't leak out and make sure
44054419
* we always retry
44064420
*/
4407-
if (gfp_mask & __GFP_NOFAIL) {
4421+
if (unlikely(nofail)) {
44084422
/*
4409-
* All existing users of the __GFP_NOFAIL are blockable, so warn
4410-
* of any new users that actually require GFP_NOWAIT
4423+
* Lacking direct_reclaim we can't do anything to reclaim memory,
4424+
* we disregard these unreasonable nofail requests and still
4425+
* return NULL
44114426
*/
4412-
if (WARN_ON_ONCE_GFP(!can_direct_reclaim, gfp_mask))
4427+
if (!can_direct_reclaim)
44134428
goto fail;
44144429

4415-
/*
4416-
* PF_MEMALLOC request from this context is rather bizarre
4417-
* because we cannot reclaim anything and only can loop waiting
4418-
* for somebody to do a work for us
4419-
*/
4420-
WARN_ON_ONCE_GFP(current->flags & PF_MEMALLOC, gfp_mask);
4421-
4422-
/*
4423-
* non failing costly orders are a hard requirement which we
4424-
* are not prepared for much so let's warn about these users
4425-
* so that we can identify them and convert them to something
4426-
* else.
4427-
*/
4428-
WARN_ON_ONCE_GFP(costly_order, gfp_mask);
4429-
44304430
/*
44314431
* Help non-failing allocations by giving some access to memory
44324432
* reserves normally used for high priority non-blocking

0 commit comments

Comments
 (0)