Commit 19c5fb83f2a4 ("mm: page_alloc: avoid kswapd thrashing due to NUMA restrictions") makes get_page_from_freelist() skip any node with kswapd running to avoid piling on already-pressured nodes. This assumes that kswapd was woken to create free space, although kswapd can be woken up for other reasons, like higher order allocation failures. Thus, fragmented nodes with failed high order allocations will always wake up kswapd and direct order-0 allocations to the fallback path, even when there is free space. This behavior was observed on 2-NUMA Meta production workloads where faults would pile on already pressured remote nodes while stranding memory on the relatively empty local node, causing unnecessary pressure. Only skip nodes with kswapd running if the node is under the relevant low-memory watermark. On a 2-NUMA VM with a 60G node0 and a 20G node1, with THP enabled=always and defrag=defer, I ran a process that prefers allocating on node0. With a fragmented node0: - Before the patch, 66% of faults were served from node0 - After the patch, 100% of faults were served from node0 Fixes: 19c5fb83f2a4 ("mm: page_alloc: avoid kswapd thrashing due to NUMA restrictions") Suggested-by: Daniel Hodges Reviewed-by: Jose A. Perez de Azpillaga Reviewed-by: Johannes Weiner Reviewed-by: Lorenzo Stoakes (ARM) Acked-by: Gregory Price (Meta) Acked-by: Zi Yan Signed-off-by: Joshua Hahn --- v2 --> v3: - Shortening cover letter, suggested by Gregory and Johannes - Collect signatures v1 --> v2: - Added a Fixes tag and separated this from the other cleanup patches - Check zone_watermark_fast with no flag to match the call in pgdat_balanced(). - Calculates mark outside the zone_watermark_fast check to match the style for the other zone checks. - Wordsmithing commit message to explain the unaddressed gap. - Yes, this is ugly. I have another series to clean this code up but I wanted this to be a feature-minimal patch so that it could be easily backported. Things will be much prettier once those are landed. mm/page_alloc.c | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 8532af052d74b..56121c3f9c4a7 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -45,6 +45,7 @@ #include #include #include +#include #include #include #include @@ -3799,6 +3800,9 @@ get_page_from_freelist(gfp_t gfp_mask, unsigned int order, int alloc_flags, struct zoneref *z; struct zone *zone; struct pglist_data *last_pgdat = NULL; + enum zone_watermarks kswapd_watermark = + sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING ? + WMARK_PROMO : WMARK_HIGH; bool last_pgdat_dirty_ok = false; bool no_fallback; bool skip_kswapd_nodes = nr_online_nodes > 1; @@ -3866,16 +3870,21 @@ get_page_from_freelist(gfp_t gfp_mask, unsigned int order, int alloc_flags, } /* - * If kswapd is already active on a node, keep looking - * for other nodes that might be idle. This can happen - * if another process has NUMA bindings and is causing - * kswapd wakeups on only some nodes. Avoid accidental - * "node_reclaim_mode"-like behavior in this case. + * If kswapd is already active on a node and the zone is below + * its kswapd watermark for this request, keep looking for + * other nodes that might be idle. This can happen if another + * process has NUMA bindings and is causing kswapd wakeups on + * only some nodes. Avoid accidental "node_reclaim_mode"-like + * behavior in this case. */ if (skip_kswapd_nodes && !waitqueue_active(&zone->zone_pgdat->kswapd_wait)) { - skipped_kswapd_nodes = true; - continue; + mark = wmark_pages(zone, kswapd_watermark); + if (!zone_watermark_fast(zone, order, mark, + ac->highest_zoneidx, 0, gfp_mask)) { + skipped_kswapd_nodes = true; + continue; + } } cond_accept_memory(zone, order, alloc_flags); -- 2.53.0-Meta