Skip to content

Commit 81c0a2b

Browse files
hnaztorvalds
authored andcommitted
mm: page_alloc: fair zone allocator policy
Each zone that holds userspace pages of one workload must be aged at a speed proportional to the zone size. Otherwise, the time an individual page gets to stay in memory depends on the zone it happened to be allocated in. Asymmetry in the zone aging creates rather unpredictable aging behavior and results in the wrong pages being reclaimed, activated etc. But exactly this happens right now because of the way the page allocator and kswapd interact. The page allocator uses per-node lists of all zones in the system, ordered by preference, when allocating a new page. When the first iteration does not yield any results, kswapd is woken up and the allocator retries. Due to the way kswapd reclaims zones below the high watermark while a zone can be allocated from when it is above the low watermark, the allocator may keep kswapd running while kswapd reclaim ensures that the page allocator can keep allocating from the first zone in the zonelist for extended periods of time. Meanwhile the other zones rarely see new allocations and thus get aged much slower in comparison. The result is that the occasional page placed in lower zones gets relatively more time in memory, even gets promoted to the active list after its peers have long been evicted. Meanwhile, the bulk of the working set may be thrashing on the preferred zone even though there may be significant amounts of memory available in the lower zones. Even the most basic test -- repeatedly reading a file slightly bigger than memory -- shows how broken the zone aging is. In this scenario, no single page should be able stay in memory long enough to get referenced twice and activated, but activation happens in spades: $ grep active_file /proc/zoneinfo nr_inactive_file 0 nr_active_file 0 nr_inactive_file 0 nr_active_file 8 nr_inactive_file 1582 nr_active_file 11994 $ cat data data data data >/dev/null $ grep active_file /proc/zoneinfo nr_inactive_file 0 nr_active_file 70 nr_inactive_file 258753 nr_active_file 443214 nr_inactive_file 149793 nr_active_file 12021 Fix this with a very simple round robin allocator. Each zone is allowed a batch of allocations that is proportional to the zone's size, after which it is treated as full. The batch counters are reset when all zones have been tried and the allocator enters the slowpath and kicks off kswapd reclaim. Allocation and reclaim is now fairly spread out to all available/allowable zones: $ grep active_file /proc/zoneinfo nr_inactive_file 0 nr_active_file 0 nr_inactive_file 174 nr_active_file 4865 nr_inactive_file 53 nr_active_file 860 $ cat data data data data >/dev/null $ grep active_file /proc/zoneinfo nr_inactive_file 0 nr_active_file 0 nr_inactive_file 666622 nr_active_file 4988 nr_inactive_file 190969 nr_active_file 937 When zone_reclaim_mode is enabled, allocations will now spread out to all zones on the local node, not just the first preferred zone (which on a 4G node might be a tiny Normal zone). Signed-off-by: Johannes Weiner <hannes@cmpxchg.org> Acked-by: Mel Gorman <mgorman@suse.de> Reviewed-by: Rik van Riel <riel@redhat.com> Cc: Andrea Arcangeli <aarcange@redhat.com> Cc: Paul Bolle <paul.bollee@gmail.com> Cc: Zlatko Calusic <zcalusic@bitsync.net> Tested-by: Kevin Hilman <khilman@linaro.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent e085dbc commit 81c0a2b

3 files changed

Lines changed: 64 additions & 10 deletions

File tree

‎include/linux/mmzone.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ struct zone_padding {
105105
enum zone_stat_item {
106106
/* First 128 byte cacheline (assuming 64 bit words) */
107107
NR_FREE_PAGES,
108+
NR_ALLOC_BATCH,
108109
NR_LRU_BASE,
109110
NR_INACTIVE_ANON = NR_LRU_BASE, /* must match order of LRU_[IN]ACTIVE */
110111
NR_ACTIVE_ANON, /* " " " " " */

‎mm/page_alloc.c‎

Lines changed: 62 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1551,6 +1551,7 @@ struct page *buffered_rmqueue(struct zone *preferred_zone,
15511551
get_pageblock_migratetype(page));
15521552
}
15531553

1554+
__mod_zone_page_state(zone, NR_ALLOC_BATCH, -(1 << order));
15541555
__count_zone_vm_events(PGALLOC, zone, 1 << order);
15551556
zone_statistics(preferred_zone, zone, gfp_flags);
15561557
local_irq_restore(flags);
@@ -1817,6 +1818,11 @@ static void zlc_clear_zones_full(struct zonelist *zonelist)
18171818
bitmap_zero(zlc->fullzones, MAX_ZONES_PER_ZONELIST);
18181819
}
18191820

1821+
static bool zone_local(struct zone *local_zone, struct zone *zone)
1822+
{
1823+
return node_distance(local_zone->node, zone->node) == LOCAL_DISTANCE;
1824+
}
1825+
18201826
static bool zone_allows_reclaim(struct zone *local_zone, struct zone *zone)
18211827
{
18221828
return node_isset(local_zone->node, zone->zone_pgdat->reclaim_nodes);
@@ -1854,6 +1860,11 @@ static void zlc_clear_zones_full(struct zonelist *zonelist)
18541860
{
18551861
}
18561862

1863+
static bool zone_local(struct zone *local_zone, struct zone *zone)
1864+
{
1865+
return true;
1866+
}
1867+
18571868
static bool zone_allows_reclaim(struct zone *local_zone, struct zone *zone)
18581869
{
18591870
return true;
@@ -1900,6 +1911,26 @@ get_page_from_freelist(gfp_t gfp_mask, nodemask_t *nodemask, unsigned int order,
19001911
BUILD_BUG_ON(ALLOC_NO_WATERMARKS < NR_WMARK);
19011912
if (alloc_flags & ALLOC_NO_WATERMARKS)
19021913
goto try_this_zone;
1914+
/*
1915+
* Distribute pages in proportion to the individual
1916+
* zone size to ensure fair page aging. The zone a
1917+
* page was allocated in should have no effect on the
1918+
* time the page has in memory before being reclaimed.
1919+
*
1920+
* When zone_reclaim_mode is enabled, try to stay in
1921+
* local zones in the fastpath. If that fails, the
1922+
* slowpath is entered, which will do another pass
1923+
* starting with the local zones, but ultimately fall
1924+
* back to remote zones that do not partake in the
1925+
* fairness round-robin cycle of this zonelist.
1926+
*/
1927+
if (alloc_flags & ALLOC_WMARK_LOW) {
1928+
if (zone_page_state(zone, NR_ALLOC_BATCH) <= 0)
1929+
continue;
1930+
if (zone_reclaim_mode &&
1931+
!zone_local(preferred_zone, zone))
1932+
continue;
1933+
}
19031934
/*
19041935
* When allocating a page cache page for writing, we
19051936
* want to get it from a zone that is within its dirty
@@ -2346,16 +2377,30 @@ __alloc_pages_high_priority(gfp_t gfp_mask, unsigned int order,
23462377
return page;
23472378
}
23482379

2349-
static inline
2350-
void wake_all_kswapd(unsigned int order, struct zonelist *zonelist,
2351-
enum zone_type high_zoneidx,
2352-
enum zone_type classzone_idx)
2380+
static void prepare_slowpath(gfp_t gfp_mask, unsigned int order,
2381+
struct zonelist *zonelist,
2382+
enum zone_type high_zoneidx,
2383+
struct zone *preferred_zone)
23532384
{
23542385
struct zoneref *z;
23552386
struct zone *zone;
23562387

2357-
for_each_zone_zonelist(zone, z, zonelist, high_zoneidx)
2358-
wakeup_kswapd(zone, order, classzone_idx);
2388+
for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) {
2389+
if (!(gfp_mask & __GFP_NO_KSWAPD))
2390+
wakeup_kswapd(zone, order, zone_idx(preferred_zone));
2391+
/*
2392+
* Only reset the batches of zones that were actually
2393+
* considered in the fast path, we don't want to
2394+
* thrash fairness information for zones that are not
2395+
* actually part of this zonelist's round-robin cycle.
2396+
*/
2397+
if (zone_reclaim_mode && !zone_local(preferred_zone, zone))
2398+
continue;
2399+
mod_zone_page_state(zone, NR_ALLOC_BATCH,
2400+
high_wmark_pages(zone) -
2401+
low_wmark_pages(zone) -
2402+
zone_page_state(zone, NR_ALLOC_BATCH));
2403+
}
23592404
}
23602405

23612406
static inline int
@@ -2451,9 +2496,8 @@ __alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order,
24512496
goto nopage;
24522497

24532498
restart:
2454-
if (!(gfp_mask & __GFP_NO_KSWAPD))
2455-
wake_all_kswapd(order, zonelist, high_zoneidx,
2456-
zone_idx(preferred_zone));
2499+
prepare_slowpath(gfp_mask, order, zonelist,
2500+
high_zoneidx, preferred_zone);
24572501

24582502
/*
24592503
* OK, we're below the kswapd watermark and have kicked background
@@ -4753,8 +4797,11 @@ static void __paginginit free_area_init_core(struct pglist_data *pgdat,
47534797
spin_lock_init(&zone->lru_lock);
47544798
zone_seqlock_init(zone);
47554799
zone->zone_pgdat = pgdat;
4756-
47574800
zone_pcp_init(zone);
4801+
4802+
/* For bootup, initialized properly in watermark setup */
4803+
mod_zone_page_state(zone, NR_ALLOC_BATCH, zone->managed_pages);
4804+
47584805
lruvec_init(&zone->lruvec);
47594806
if (!size)
47604807
continue;
@@ -5525,6 +5572,11 @@ static void __setup_per_zone_wmarks(void)
55255572
zone->watermark[WMARK_LOW] = min_wmark_pages(zone) + (tmp >> 2);
55265573
zone->watermark[WMARK_HIGH] = min_wmark_pages(zone) + (tmp >> 1);
55275574

5575+
__mod_zone_page_state(zone, NR_ALLOC_BATCH,
5576+
high_wmark_pages(zone) -
5577+
low_wmark_pages(zone) -
5578+
zone_page_state(zone, NR_ALLOC_BATCH));
5579+
55285580
setup_zone_migrate_reserve(zone);
55295581
spin_unlock_irqrestore(&zone->lock, flags);
55305582
}

‎mm/vmstat.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,7 @@ static void walk_zones_in_node(struct seq_file *m, pg_data_t *pgdat,
703703
const char * const vmstat_text[] = {
704704
/* Zoned VM counters */
705705
"nr_free_pages",
706+
"nr_alloc_batch",
706707
"nr_inactive_anon",
707708
"nr_active_anon",
708709
"nr_inactive_file",

0 commit comments

Comments
 (0)