Skip to content

Commit 4ffeaf3

Browse files
Mel Gormantorvalds
authored andcommitted
mm: page_alloc: reduce cost of the fair zone allocation policy
The fair zone allocation policy round-robins allocations between zones within a node to avoid age inversion problems during reclaim. If the first allocation fails, the batch counts are reset and a second attempt made before entering the slow path. One assumption made with this scheme is that batches expire at roughly the same time and the resets each time are justified. This assumption does not hold when zones reach their low watermark as the batches will be consumed at uneven rates. Allocation failure due to watermark depletion result in additional zonelist scans for the reset and another watermark check before hitting the slowpath. On UMA, the benefit is negligible -- around 0.25%. On 4-socket NUMA machine it's variable due to the variability of measuring overhead with the vmstat changes. The system CPU overhead comparison looks like 3.16.0-rc3 3.16.0-rc3 3.16.0-rc3 vanilla vmstat-v5 lowercost-v5 User 746.94 774.56 802.00 System 65336.22 32847.27 40852.33 Elapsed 27553.52 27415.04 27368.46 However it is worth noting that the overall benchmark still completed faster and intuitively it makes sense to take as few passes as possible through the zonelists. Signed-off-by: Mel Gorman <mgorman@suse.de> Acked-by: Johannes Weiner <hannes@cmpxchg.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent f7b5d64 commit 4ffeaf3

2 files changed

Lines changed: 59 additions & 48 deletions

File tree

‎include/linux/mmzone.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,7 @@ typedef enum {
534534
ZONE_WRITEBACK, /* reclaim scanning has recently found
535535
* many pages under writeback
536536
*/
537+
ZONE_FAIR_DEPLETED, /* fair zone policy batch depleted */
537538
} zone_flags_t;
538539

539540
static inline void zone_set_flag(struct zone *zone, zone_flags_t flag)
@@ -571,6 +572,11 @@ static inline int zone_is_reclaim_locked(const struct zone *zone)
571572
return test_bit(ZONE_RECLAIM_LOCKED, &zone->flags);
572573
}
573574

575+
static inline int zone_is_fair_depleted(const struct zone *zone)
576+
{
577+
return test_bit(ZONE_FAIR_DEPLETED, &zone->flags);
578+
}
579+
574580
static inline int zone_is_oom_locked(const struct zone *zone)
575581
{
576582
return test_bit(ZONE_OOM_LOCKED, &zone->flags);

‎mm/page_alloc.c‎

Lines changed: 53 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1612,6 +1612,9 @@ struct page *buffered_rmqueue(struct zone *preferred_zone,
16121612
}
16131613

16141614
__mod_zone_page_state(zone, NR_ALLOC_BATCH, -(1 << order));
1615+
if (zone_page_state(zone, NR_ALLOC_BATCH) == 0 &&
1616+
!zone_is_fair_depleted(zone))
1617+
zone_set_flag(zone, ZONE_FAIR_DEPLETED);
16151618

16161619
__count_zone_vm_events(PGALLOC, zone, 1 << order);
16171620
zone_statistics(preferred_zone, zone, gfp_flags);
@@ -1923,6 +1926,18 @@ static bool zone_allows_reclaim(struct zone *local_zone, struct zone *zone)
19231926

19241927
#endif /* CONFIG_NUMA */
19251928

1929+
static void reset_alloc_batches(struct zone *preferred_zone)
1930+
{
1931+
struct zone *zone = preferred_zone->zone_pgdat->node_zones;
1932+
1933+
do {
1934+
mod_zone_page_state(zone, NR_ALLOC_BATCH,
1935+
high_wmark_pages(zone) - low_wmark_pages(zone) -
1936+
atomic_long_read(&zone->vm_stat[NR_ALLOC_BATCH]));
1937+
zone_clear_flag(zone, ZONE_FAIR_DEPLETED);
1938+
} while (zone++ != preferred_zone);
1939+
}
1940+
19261941
/*
19271942
* get_page_from_freelist goes through the zonelist trying to allocate
19281943
* a page.
@@ -1940,8 +1955,12 @@ get_page_from_freelist(gfp_t gfp_mask, nodemask_t *nodemask, unsigned int order,
19401955
int did_zlc_setup = 0; /* just call zlc_setup() one time */
19411956
bool consider_zone_dirty = (alloc_flags & ALLOC_WMARK_LOW) &&
19421957
(gfp_mask & __GFP_WRITE);
1958+
int nr_fair_skipped = 0;
1959+
bool zonelist_rescan;
19431960

19441961
zonelist_scan:
1962+
zonelist_rescan = false;
1963+
19451964
/*
19461965
* Scan zonelist, looking for a zone with enough free.
19471966
* See also __cpuset_node_allowed_softwall() comment in kernel/cpuset.c.
@@ -1966,8 +1985,10 @@ get_page_from_freelist(gfp_t gfp_mask, nodemask_t *nodemask, unsigned int order,
19661985
if (alloc_flags & ALLOC_FAIR) {
19671986
if (!zone_local(preferred_zone, zone))
19681987
break;
1969-
if (zone_page_state(zone, NR_ALLOC_BATCH) <= 0)
1988+
if (zone_is_fair_depleted(zone)) {
1989+
nr_fair_skipped++;
19701990
continue;
1991+
}
19711992
}
19721993
/*
19731994
* When allocating a page cache page for writing, we
@@ -2073,13 +2094,7 @@ get_page_from_freelist(gfp_t gfp_mask, nodemask_t *nodemask, unsigned int order,
20732094
zlc_mark_zone_full(zonelist, z);
20742095
}
20752096

2076-
if (unlikely(IS_ENABLED(CONFIG_NUMA) && page == NULL && zlc_active)) {
2077-
/* Disable zlc cache for second zonelist scan */
2078-
zlc_active = 0;
2079-
goto zonelist_scan;
2080-
}
2081-
2082-
if (page)
2097+
if (page) {
20832098
/*
20842099
* page->pfmemalloc is set when ALLOC_NO_WATERMARKS was
20852100
* necessary to allocate the page. The expectation is
@@ -2088,8 +2103,37 @@ get_page_from_freelist(gfp_t gfp_mask, nodemask_t *nodemask, unsigned int order,
20882103
* for !PFMEMALLOC purposes.
20892104
*/
20902105
page->pfmemalloc = !!(alloc_flags & ALLOC_NO_WATERMARKS);
2106+
return page;
2107+
}
20912108

2092-
return page;
2109+
/*
2110+
* The first pass makes sure allocations are spread fairly within the
2111+
* local node. However, the local node might have free pages left
2112+
* after the fairness batches are exhausted, and remote zones haven't
2113+
* even been considered yet. Try once more without fairness, and
2114+
* include remote zones now, before entering the slowpath and waking
2115+
* kswapd: prefer spilling to a remote zone over swapping locally.
2116+
*/
2117+
if (alloc_flags & ALLOC_FAIR) {
2118+
alloc_flags &= ~ALLOC_FAIR;
2119+
if (nr_fair_skipped) {
2120+
zonelist_rescan = true;
2121+
reset_alloc_batches(preferred_zone);
2122+
}
2123+
if (nr_online_nodes > 1)
2124+
zonelist_rescan = true;
2125+
}
2126+
2127+
if (unlikely(IS_ENABLED(CONFIG_NUMA) && zlc_active)) {
2128+
/* Disable zlc cache for second zonelist scan */
2129+
zlc_active = 0;
2130+
zonelist_rescan = true;
2131+
}
2132+
2133+
if (zonelist_rescan)
2134+
goto zonelist_scan;
2135+
2136+
return NULL;
20932137
}
20942138

20952139
/*
@@ -2410,28 +2454,6 @@ __alloc_pages_high_priority(gfp_t gfp_mask, unsigned int order,
24102454
return page;
24112455
}
24122456

2413-
static void reset_alloc_batches(struct zonelist *zonelist,
2414-
enum zone_type high_zoneidx,
2415-
struct zone *preferred_zone)
2416-
{
2417-
struct zoneref *z;
2418-
struct zone *zone;
2419-
2420-
for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) {
2421-
/*
2422-
* Only reset the batches of zones that were actually
2423-
* considered in the fairness pass, we don't want to
2424-
* trash fairness information for zones that are not
2425-
* actually part of this zonelist's round-robin cycle.
2426-
*/
2427-
if (!zone_local(preferred_zone, zone))
2428-
continue;
2429-
mod_zone_page_state(zone, NR_ALLOC_BATCH,
2430-
high_wmark_pages(zone) - low_wmark_pages(zone) -
2431-
atomic_long_read(&zone->vm_stat[NR_ALLOC_BATCH]));
2432-
}
2433-
}
2434-
24352457
static void wake_all_kswapds(unsigned int order,
24362458
struct zonelist *zonelist,
24372459
enum zone_type high_zoneidx,
@@ -2767,28 +2789,11 @@ __alloc_pages_nodemask(gfp_t gfp_mask, unsigned int order,
27672789
if (allocflags_to_migratetype(gfp_mask) == MIGRATE_MOVABLE)
27682790
alloc_flags |= ALLOC_CMA;
27692791
#endif
2770-
retry:
27712792
/* First allocation attempt */
27722793
page = get_page_from_freelist(gfp_mask|__GFP_HARDWALL, nodemask, order,
27732794
zonelist, high_zoneidx, alloc_flags,
27742795
preferred_zone, classzone_idx, migratetype);
27752796
if (unlikely(!page)) {
2776-
/*
2777-
* The first pass makes sure allocations are spread
2778-
* fairly within the local node. However, the local
2779-
* node might have free pages left after the fairness
2780-
* batches are exhausted, and remote zones haven't
2781-
* even been considered yet. Try once more without
2782-
* fairness, and include remote zones now, before
2783-
* entering the slowpath and waking kswapd: prefer
2784-
* spilling to a remote zone over swapping locally.
2785-
*/
2786-
if (alloc_flags & ALLOC_FAIR) {
2787-
reset_alloc_batches(zonelist, high_zoneidx,
2788-
preferred_zone);
2789-
alloc_flags &= ~ALLOC_FAIR;
2790-
goto retry;
2791-
}
27922797
/*
27932798
* Runtime PM, block IO and its error handling path
27942799
* can deadlock because I/O on the device might not

0 commit comments

Comments
 (0)