Commit 0e56acae authored by Alexander Duyck's avatar Alexander Duyck Committed by Linus Torvalds

mm: initialize MAX_ORDER_NR_PAGES at a time instead of doing larger sections

Add yet another iterator, for_each_free_mem_range_in_zone_from, and then
use it to support initializing and freeing pages in groups no larger than
MAX_ORDER_NR_PAGES.  By doing this we can greatly improve the cache
locality of the pages while we do several loops over them in the init and
freeing process.

We are able to tighten the loops further as a result of the "from"
iterator as we can perform the initial checks for first_init_pfn in our
first call to the iterator, and continue without the need for those checks
via the "from" iterator.  I have added this functionality in the function
called deferred_init_mem_pfn_range_in_zone that primes the iterator and
causes us to exit if we encounter any failure.

On my x86_64 test system with 384GB of memory per node I saw a reduction
in initialization time from 1.85s to 1.38s as a result of this patch.

Link: http://lkml.kernel.org/r/20190405221231.12227.85836.stgit@localhost.localdomainSigned-off-by: default avatarAlexander Duyck <alexander.h.duyck@linux.intel.com>
Reviewed-by: default avatarPavel Tatashin <pasha.tatashin@soleen.com>
Cc: Mike Rapoport <rppt@linux.ibm.com>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Dave Jiang <dave.jiang@intel.com>
Cc: Matthew Wilcox <willy@infradead.org>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: <yi.z.zhang@linux.intel.com>
Cc: Khalid Aziz <khalid.aziz@oracle.com>
Cc: Mike Rapoport <rppt@linux.vnet.ibm.com>
Cc: Vlastimil Babka <vbabka@suse.cz>
Cc: Dan Williams <dan.j.williams@intel.com>
Cc: Laurent Dufour <ldufour@linux.vnet.ibm.com>
Cc: Mel Gorman <mgorman@techsingularity.net>
Cc: David S. Miller <davem@davemloft.net>
Cc: "Kirill A. Shutemov" <kirill.shutemov@linux.intel.com>
Cc: Pavel Tatashin <pavel.tatashin@microsoft.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 837566e7
...@@ -263,6 +263,22 @@ void __next_mem_pfn_range_in_zone(u64 *idx, struct zone *zone, ...@@ -263,6 +263,22 @@ void __next_mem_pfn_range_in_zone(u64 *idx, struct zone *zone,
__next_mem_pfn_range_in_zone(&i, zone, p_start, p_end); \ __next_mem_pfn_range_in_zone(&i, zone, p_start, p_end); \
i != U64_MAX; \ i != U64_MAX; \
__next_mem_pfn_range_in_zone(&i, zone, p_start, p_end)) __next_mem_pfn_range_in_zone(&i, zone, p_start, p_end))
/**
* for_each_free_mem_range_in_zone_from - iterate through zone specific
* free memblock areas from a given point
* @i: u64 used as loop variable
* @zone: zone in which all of the memory blocks reside
* @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
* @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
*
* Walks over free (memory && !reserved) areas of memblock in a specific
* zone, continuing from current position. Available as soon as memblock is
* initialized.
*/
#define for_each_free_mem_pfn_range_in_zone_from(i, zone, p_start, p_end) \
for (; i != U64_MAX; \
__next_mem_pfn_range_in_zone(&i, zone, p_start, p_end))
#endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */ #endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */
/** /**
......
...@@ -1627,16 +1627,100 @@ static unsigned long __init deferred_init_pages(struct zone *zone, ...@@ -1627,16 +1627,100 @@ static unsigned long __init deferred_init_pages(struct zone *zone,
return (nr_pages); return (nr_pages);
} }
/*
* This function is meant to pre-load the iterator for the zone init.
* Specifically it walks through the ranges until we are caught up to the
* first_init_pfn value and exits there. If we never encounter the value we
* return false indicating there are no valid ranges left.
*/
static bool __init
deferred_init_mem_pfn_range_in_zone(u64 *i, struct zone *zone,
unsigned long *spfn, unsigned long *epfn,
unsigned long first_init_pfn)
{
u64 j;
/*
* Start out by walking through the ranges in this zone that have
* already been initialized. We don't need to do anything with them
* so we just need to flush them out of the system.
*/
for_each_free_mem_pfn_range_in_zone(j, zone, spfn, epfn) {
if (*epfn <= first_init_pfn)
continue;
if (*spfn < first_init_pfn)
*spfn = first_init_pfn;
*i = j;
return true;
}
return false;
}
/*
* Initialize and free pages. We do it in two loops: first we initialize
* struct page, then free to buddy allocator, because while we are
* freeing pages we can access pages that are ahead (computing buddy
* page in __free_one_page()).
*
* In order to try and keep some memory in the cache we have the loop
* broken along max page order boundaries. This way we will not cause
* any issues with the buddy page computation.
*/
static unsigned long __init
deferred_init_maxorder(u64 *i, struct zone *zone, unsigned long *start_pfn,
unsigned long *end_pfn)
{
unsigned long mo_pfn = ALIGN(*start_pfn + 1, MAX_ORDER_NR_PAGES);
unsigned long spfn = *start_pfn, epfn = *end_pfn;
unsigned long nr_pages = 0;
u64 j = *i;
/* First we loop through and initialize the page values */
for_each_free_mem_pfn_range_in_zone_from(j, zone, start_pfn, end_pfn) {
unsigned long t;
if (mo_pfn <= *start_pfn)
break;
t = min(mo_pfn, *end_pfn);
nr_pages += deferred_init_pages(zone, *start_pfn, t);
if (mo_pfn < *end_pfn) {
*start_pfn = mo_pfn;
break;
}
}
/* Reset values and now loop through freeing pages as needed */
swap(j, *i);
for_each_free_mem_pfn_range_in_zone_from(j, zone, &spfn, &epfn) {
unsigned long t;
if (mo_pfn <= spfn)
break;
t = min(mo_pfn, epfn);
deferred_free_pages(spfn, t);
if (mo_pfn <= epfn)
break;
}
return nr_pages;
}
/* Initialise remaining memory on a node */ /* Initialise remaining memory on a node */
static int __init deferred_init_memmap(void *data) static int __init deferred_init_memmap(void *data)
{ {
pg_data_t *pgdat = data; pg_data_t *pgdat = data;
const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id);
unsigned long spfn = 0, epfn = 0, nr_pages = 0;
unsigned long first_init_pfn, flags;
unsigned long start = jiffies; unsigned long start = jiffies;
unsigned long nr_pages = 0;
unsigned long spfn, epfn, first_init_pfn, flags;
int zid;
struct zone *zone; struct zone *zone;
const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id); int zid;
u64 i; u64 i;
/* Bind memory initialisation thread to a local node if possible */ /* Bind memory initialisation thread to a local node if possible */
...@@ -1662,22 +1746,20 @@ static int __init deferred_init_memmap(void *data) ...@@ -1662,22 +1746,20 @@ static int __init deferred_init_memmap(void *data)
if (first_init_pfn < zone_end_pfn(zone)) if (first_init_pfn < zone_end_pfn(zone))
break; break;
} }
first_init_pfn = max(zone->zone_start_pfn, first_init_pfn);
/* If the zone is empty somebody else may have cleared out the zone */
if (!deferred_init_mem_pfn_range_in_zone(&i, zone, &spfn, &epfn,
first_init_pfn))
goto zone_empty;
/* /*
* Initialize and free pages. We do it in two loops: first we initialize * Initialize and free pages in MAX_ORDER sized increments so
* struct page, than free to buddy allocator, because while we are * that we can avoid introducing any issues with the buddy
* freeing pages we can access pages that are ahead (computing buddy * allocator.
* page in __free_one_page()).
*/ */
for_each_free_mem_pfn_range_in_zone(i, zone, &spfn, &epfn) { while (spfn < epfn)
spfn = max_t(unsigned long, first_init_pfn, spfn); nr_pages += deferred_init_maxorder(&i, zone, &spfn, &epfn);
nr_pages += deferred_init_pages(zone, spfn, epfn); zone_empty:
}
for_each_free_mem_pfn_range_in_zone(i, zone, &spfn, &epfn) {
spfn = max_t(unsigned long, first_init_pfn, spfn);
deferred_free_pages(spfn, epfn);
}
pgdat_resize_unlock(pgdat, &flags); pgdat_resize_unlock(pgdat, &flags);
/* Sanity check that the next zone really is unpopulated */ /* Sanity check that the next zone really is unpopulated */
...@@ -1710,9 +1792,9 @@ deferred_grow_zone(struct zone *zone, unsigned int order) ...@@ -1710,9 +1792,9 @@ deferred_grow_zone(struct zone *zone, unsigned int order)
{ {
unsigned long nr_pages_needed = ALIGN(1 << order, PAGES_PER_SECTION); unsigned long nr_pages_needed = ALIGN(1 << order, PAGES_PER_SECTION);
pg_data_t *pgdat = zone->zone_pgdat; pg_data_t *pgdat = zone->zone_pgdat;
unsigned long nr_pages = 0;
unsigned long first_init_pfn, spfn, epfn, t, flags;
unsigned long first_deferred_pfn = pgdat->first_deferred_pfn; unsigned long first_deferred_pfn = pgdat->first_deferred_pfn;
unsigned long spfn, epfn, flags;
unsigned long nr_pages = 0;
u64 i; u64 i;
/* Only the last zone may have deferred pages */ /* Only the last zone may have deferred pages */
...@@ -1741,37 +1823,35 @@ deferred_grow_zone(struct zone *zone, unsigned int order) ...@@ -1741,37 +1823,35 @@ deferred_grow_zone(struct zone *zone, unsigned int order)
return true; return true;
} }
first_init_pfn = max(zone->zone_start_pfn, first_deferred_pfn); /* If the zone is empty somebody else may have cleared out the zone */
if (!deferred_init_mem_pfn_range_in_zone(&i, zone, &spfn, &epfn,
if (first_init_pfn >= pgdat_end_pfn(pgdat)) { first_deferred_pfn)) {
pgdat->first_deferred_pfn = ULONG_MAX;
pgdat_resize_unlock(pgdat, &flags); pgdat_resize_unlock(pgdat, &flags);
return false; return true;
} }
for_each_free_mem_pfn_range_in_zone(i, zone, &spfn, &epfn) { /*
spfn = max_t(unsigned long, first_init_pfn, spfn); * Initialize and free pages in MAX_ORDER sized increments so
* that we can avoid introducing any issues with the buddy
* allocator.
*/
while (spfn < epfn) {
/* update our first deferred PFN for this section */
first_deferred_pfn = spfn;
nr_pages += deferred_init_maxorder(&i, zone, &spfn, &epfn);
while (spfn < epfn && nr_pages < nr_pages_needed) { /* We should only stop along section boundaries */
t = ALIGN(spfn + PAGES_PER_SECTION, PAGES_PER_SECTION); if ((first_deferred_pfn ^ spfn) < PAGES_PER_SECTION)
first_deferred_pfn = min(t, epfn); continue;
nr_pages += deferred_init_pages(zone, spfn,
first_deferred_pfn);
spfn = first_deferred_pfn;
}
/* If our quota has been met we can stop here */
if (nr_pages >= nr_pages_needed) if (nr_pages >= nr_pages_needed)
break; break;
} }
for_each_free_mem_pfn_range_in_zone(i, zone, &spfn, &epfn) { pgdat->first_deferred_pfn = spfn;
spfn = max_t(unsigned long, first_init_pfn, spfn);
epfn = min_t(unsigned long, first_deferred_pfn, epfn);
deferred_free_pages(spfn, epfn);
if (first_deferred_pfn == epfn)
break;
}
pgdat->first_deferred_pfn = first_deferred_pfn;
pgdat_resize_unlock(pgdat, &flags); pgdat_resize_unlock(pgdat, &flags);
return nr_pages > 0; return nr_pages > 0;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment