Skip to content

Commit 3a02576

Browse files
hnaztorvalds
authored andcommitted
mm: page_alloc: spill to remote nodes before waking kswapd
On NUMA systems, a node may start thrashing cache or even swap anonymous pages while there are still free pages on remote nodes. This is a result of commits 81c0a2b ("mm: page_alloc: fair zone allocator policy") and fff4068 ("mm: page_alloc: revert NUMA aspect of fair allocation policy"). Before those changes, the allocator would first try all allowed zones, including those on remote nodes, before waking any kswapds. But now, the allocator fastpath doubles as the fairness pass, which in turn can only consider the local node to prevent remote spilling based on exhausted fairness batches alone. Remote nodes are only considered in the slowpath, after the kswapds are woken up. But if remote nodes still have free memory, kswapd should not be woken to rebalance the local node or it may thrash cash or swap prematurely. Fix this by adding one more unfair pass over the zonelist that is allowed to spill to remote nodes after the local fairness pass fails but before entering the slowpath and waking the kswapds. This also gets rid of the GFP_THISNODE exemption from the fairness protocol because the unfair pass is no longer tied to kswapd, which GFP_THISNODE is not allowed to wake up. However, because remote spills can be more frequent now - we prefer them over local kswapd reclaim - the allocation batches on remote nodes could underflow more heavily. When resetting the batches, use atomic_long_read() directly instead of zone_page_state() to calculate the delta as the latter filters negative counter values. Signed-off-by: Johannes Weiner <hannes@cmpxchg.org> Acked-by: Rik van Riel <riel@redhat.com> Acked-by: Mel Gorman <mgorman@suse.de> Cc: <stable@kernel.org> [3.12+] Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent d715ae0 commit 3a02576

2 files changed

Lines changed: 46 additions & 44 deletions

File tree

‎mm/internal.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,5 +370,6 @@ unsigned long reclaim_clean_pages_from_list(struct zone *zone,
370370
#define ALLOC_HIGH 0x20 /* __GFP_HIGH set */
371371
#define ALLOC_CPUSET 0x40 /* check for correct cpuset */
372372
#define ALLOC_CMA 0x80 /* allow allocations from CMA areas */
373+
#define ALLOC_FAIR 0x100 /* fair zone allocation */
373374

374375
#endif /* __MM_INTERNAL_H */

‎mm/page_alloc.c‎

Lines changed: 45 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,15 +1239,6 @@ void drain_zone_pages(struct zone *zone, struct per_cpu_pages *pcp)
12391239
}
12401240
local_irq_restore(flags);
12411241
}
1242-
static bool gfp_thisnode_allocation(gfp_t gfp_mask)
1243-
{
1244-
return (gfp_mask & GFP_THISNODE) == GFP_THISNODE;
1245-
}
1246-
#else
1247-
static bool gfp_thisnode_allocation(gfp_t gfp_mask)
1248-
{
1249-
return false;
1250-
}
12511242
#endif
12521243

12531244
/*
@@ -1584,12 +1575,7 @@ struct page *buffered_rmqueue(struct zone *preferred_zone,
15841575
get_pageblock_migratetype(page));
15851576
}
15861577

1587-
/*
1588-
* NOTE: GFP_THISNODE allocations do not partake in the kswapd
1589-
* aging protocol, so they can't be fair.
1590-
*/
1591-
if (!gfp_thisnode_allocation(gfp_flags))
1592-
__mod_zone_page_state(zone, NR_ALLOC_BATCH, -(1 << order));
1578+
__mod_zone_page_state(zone, NR_ALLOC_BATCH, -(1 << order));
15931579

15941580
__count_zone_vm_events(PGALLOC, zone, 1 << order);
15951581
zone_statistics(preferred_zone, zone, gfp_flags);
@@ -1955,23 +1941,12 @@ get_page_from_freelist(gfp_t gfp_mask, nodemask_t *nodemask, unsigned int order,
19551941
* zone size to ensure fair page aging. The zone a
19561942
* page was allocated in should have no effect on the
19571943
* time the page has in memory before being reclaimed.
1958-
*
1959-
* Try to stay in local zones in the fastpath. If
1960-
* that fails, the slowpath is entered, which will do
1961-
* another pass starting with the local zones, but
1962-
* ultimately fall back to remote zones that do not
1963-
* partake in the fairness round-robin cycle of this
1964-
* zonelist.
1965-
*
1966-
* NOTE: GFP_THISNODE allocations do not partake in
1967-
* the kswapd aging protocol, so they can't be fair.
19681944
*/
1969-
if ((alloc_flags & ALLOC_WMARK_LOW) &&
1970-
!gfp_thisnode_allocation(gfp_mask)) {
1971-
if (zone_page_state(zone, NR_ALLOC_BATCH) <= 0)
1972-
continue;
1945+
if (alloc_flags & ALLOC_FAIR) {
19731946
if (!zone_local(preferred_zone, zone))
19741947
continue;
1948+
if (zone_page_state(zone, NR_ALLOC_BATCH) <= 0)
1949+
continue;
19751950
}
19761951
/*
19771952
* When allocating a page cache page for writing, we
@@ -2409,32 +2384,40 @@ __alloc_pages_high_priority(gfp_t gfp_mask, unsigned int order,
24092384
return page;
24102385
}
24112386

2412-
static void prepare_slowpath(gfp_t gfp_mask, unsigned int order,
2413-
struct zonelist *zonelist,
2414-
enum zone_type high_zoneidx,
2415-
struct zone *preferred_zone)
2387+
static void reset_alloc_batches(struct zonelist *zonelist,
2388+
enum zone_type high_zoneidx,
2389+
struct zone *preferred_zone)
24162390
{
24172391
struct zoneref *z;
24182392
struct zone *zone;
24192393

24202394
for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) {
2421-
if (!(gfp_mask & __GFP_NO_KSWAPD))
2422-
wakeup_kswapd(zone, order, zone_idx(preferred_zone));
24232395
/*
24242396
* Only reset the batches of zones that were actually
2425-
* considered in the fast path, we don't want to
2426-
* thrash fairness information for zones that are not
2397+
* considered in the fairness pass, we don't want to
2398+
* trash fairness information for zones that are not
24272399
* actually part of this zonelist's round-robin cycle.
24282400
*/
24292401
if (!zone_local(preferred_zone, zone))
24302402
continue;
24312403
mod_zone_page_state(zone, NR_ALLOC_BATCH,
2432-
high_wmark_pages(zone) -
2433-
low_wmark_pages(zone) -
2434-
zone_page_state(zone, NR_ALLOC_BATCH));
2404+
high_wmark_pages(zone) - low_wmark_pages(zone) -
2405+
atomic_long_read(&zone->vm_stat[NR_ALLOC_BATCH]));
24352406
}
24362407
}
24372408

2409+
static void wake_all_kswapds(unsigned int order,
2410+
struct zonelist *zonelist,
2411+
enum zone_type high_zoneidx,
2412+
struct zone *preferred_zone)
2413+
{
2414+
struct zoneref *z;
2415+
struct zone *zone;
2416+
2417+
for_each_zone_zonelist(zone, z, zonelist, high_zoneidx)
2418+
wakeup_kswapd(zone, order, zone_idx(preferred_zone));
2419+
}
2420+
24382421
static inline int
24392422
gfp_to_alloc_flags(gfp_t gfp_mask)
24402423
{
@@ -2523,12 +2506,13 @@ __alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order,
25232506
* allowed per node queues are empty and that nodes are
25242507
* over allocated.
25252508
*/
2526-
if (gfp_thisnode_allocation(gfp_mask))
2509+
if (IS_ENABLED(CONFIG_NUMA) &&
2510+
(gfp_mask & GFP_THISNODE) == GFP_THISNODE)
25272511
goto nopage;
25282512

25292513
restart:
2530-
prepare_slowpath(gfp_mask, order, zonelist,
2531-
high_zoneidx, preferred_zone);
2514+
if (!(gfp_mask & __GFP_NO_KSWAPD))
2515+
wake_all_kswapds(order, zonelist, high_zoneidx, preferred_zone);
25322516

25332517
/*
25342518
* OK, we're below the kswapd watermark and have kicked background
@@ -2712,7 +2696,7 @@ __alloc_pages_nodemask(gfp_t gfp_mask, unsigned int order,
27122696
struct page *page = NULL;
27132697
int migratetype = allocflags_to_migratetype(gfp_mask);
27142698
unsigned int cpuset_mems_cookie;
2715-
int alloc_flags = ALLOC_WMARK_LOW|ALLOC_CPUSET;
2699+
int alloc_flags = ALLOC_WMARK_LOW|ALLOC_CPUSET|ALLOC_FAIR;
27162700
struct mem_cgroup *memcg = NULL;
27172701

27182702
gfp_mask &= gfp_allowed_mask;
@@ -2753,11 +2737,28 @@ __alloc_pages_nodemask(gfp_t gfp_mask, unsigned int order,
27532737
if (allocflags_to_migratetype(gfp_mask) == MIGRATE_MOVABLE)
27542738
alloc_flags |= ALLOC_CMA;
27552739
#endif
2740+
retry:
27562741
/* First allocation attempt */
27572742
page = get_page_from_freelist(gfp_mask|__GFP_HARDWALL, nodemask, order,
27582743
zonelist, high_zoneidx, alloc_flags,
27592744
preferred_zone, migratetype);
27602745
if (unlikely(!page)) {
2746+
/*
2747+
* The first pass makes sure allocations are spread
2748+
* fairly within the local node. However, the local
2749+
* node might have free pages left after the fairness
2750+
* batches are exhausted, and remote zones haven't
2751+
* even been considered yet. Try once more without
2752+
* fairness, and include remote zones now, before
2753+
* entering the slowpath and waking kswapd: prefer
2754+
* spilling to a remote zone over swapping locally.
2755+
*/
2756+
if (alloc_flags & ALLOC_FAIR) {
2757+
reset_alloc_batches(zonelist, high_zoneidx,
2758+
preferred_zone);
2759+
alloc_flags &= ~ALLOC_FAIR;
2760+
goto retry;
2761+
}
27612762
/*
27622763
* Runtime PM, block IO and its error handling path
27632764
* can deadlock because I/O on the device might not

0 commit comments

Comments
 (0)