Re: [PATCH v3] mm/page_alloc: avoid direct compaction for costly __GFP_NORETRY allocations

From: Salvatore Dipietro

Date: Thu Aug 27 2026 - 05:24:05 EST



On Wed, Jul 22, 2026 at 02:35:00PM +0200, Vlastimil Babka (SUSE) wrote:
> AFAICS both your v3 and the implemented Johannes' suggestion effectively
> make all the compact_first logic dead code - at least for the intended THP
> use case based on costly_order. It will only continue to do something for
> the non-movable order>0 case. So that's a significant change which will not
> show up in your results, but might affect other workloads (I'd expect mainly
> by having fewer THPs).

Right - the change above modifies THP allocation requests as well. So instead
of applying it to every costly __GFP_NORETRY allocation, I restrict it to
callers that do not ask for __GFP_THISNODE. That leaves the local-node THP
attempt untouched: the same bit pair is already tested further down the function
to bail out of the compact_first block (page_alloc.c:4899), and alloc_pages_mpol()
sets both flags on the local-node-first attempt (mempolicy.c:2476-2478).

I tested this with my PostgreSQL workload on kernel v7.2-rc7 and results look good:

Baseline: 70,735 tps
This version: 144,670 tps (145,280 / 144,037 / 144,694) +104.5%


diff --git a/mm/page_alloc.c b/mm/page_alloc.c
index ee902a468c2f..1e4b0c1e7296 100644
--- a/mm/page_alloc.c
+++ b/mm/page_alloc.c
@@ -4725,10 +4725,10 @@ static inline struct page *
__alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order,
struct alloc_context *ac)
{
- bool can_direct_reclaim = gfp_mask & __GFP_DIRECT_RECLAIM;
- bool can_compact = can_direct_reclaim && gfp_compaction_allowed(gfp_mask);
- bool nofail = gfp_mask & __GFP_NOFAIL;
const bool costly_order = order > PAGE_ALLOC_COSTLY_ORDER;
+ bool can_direct_reclaim;
+ bool can_compact;
+ bool nofail;
struct page *page = NULL;
unsigned int alloc_flags;
unsigned long did_some_progress;
@@ -4743,6 +4743,18 @@ __alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order,
bool can_retry_reserves = true;
unsigned long alloc_start_time = jiffies;

+ /*
+ * Costly __GFP_NORETRY callers have a cheap fallback, so don't stall
+ * them in reclaim or compaction. __GFP_THISNODE callers are exempt.
+ */
+ if (costly_order && (gfp_mask & __GFP_NORETRY) &&
+ !(gfp_mask & __GFP_THISNODE))
+ gfp_mask &= ~__GFP_DIRECT_RECLAIM;
+
+ can_direct_reclaim = gfp_mask & __GFP_DIRECT_RECLAIM;
+ can_compact = can_direct_reclaim && gfp_compaction_allowed(gfp_mask);
+ nofail = gfp_mask & __GFP_NOFAIL;
+
if (unlikely(nofail)) {
/*
* Also we don't support __GFP_NOFAIL without __GFP_DIRECT_RECLAIM,



AMAZON DEVELOPMENT CENTER ITALY SRL, viale Monte Grappa 3/5, 20124 Milano, Italia, Registro delle Imprese di Milano Monza Brianza Lodi REA n. 2504859, Capitale Sociale: 10.000 EUR i.v., Cod. Fisc. e P.IVA 10100050961, Societa con Socio Unico