Skip to content

Commit c4e1be9

Browse files
hansendctorvalds
authored andcommitted
mm, sparsemem: break out of loops early
There are a number of times that we loop over NR_MEM_SECTIONS, looking for section_present() on each section. But, when we have very large physical address spaces (large MAX_PHYSMEM_BITS), NR_MEM_SECTIONS becomes very large, making the loops quite long. With MAX_PHYSMEM_BITS=46 and a section size of 128MB, the current loops are 512k iterations, which we barely notice on modern hardware. But, raising MAX_PHYSMEM_BITS higher (like we will see on systems that support 5-level paging) makes this 64x longer and we start to notice, especially on slower systems like simulators. A 10-second delay for 512k iterations is annoying. But, a 640- second delay is crippling. This does not help if we have extremely sparse physical address spaces, but those are quite rare. We expect that most of the "slow" systems where this matters will also be quite small and non-sparse. To fix this, we track the highest section we've ever encountered. This lets us know when we will *never* see another section_present(), and lets us break out of the loops earlier. Doing the whole for_each_present_section_nr() macro is probably overkill, but it will ensure that any future loop iterations that we grow are more likely to be correct. Kirrill said "It shaved almost 40 seconds from boot time in qemu with 5-level paging enabled for me". Link: http://lkml.kernel.org/r/20170504174434.C45A4735@viggo.jf.intel.com Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com> Tested-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 7660a6f commit c4e1be9

3 files changed

Lines changed: 52 additions & 14 deletions

File tree

‎drivers/base/memory.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,10 @@ int __init memory_dev_init(void)
820820
*/
821821
mutex_lock(&mem_sysfs_mutex);
822822
for (i = 0; i < NR_MEM_SECTIONS; i += sections_per_block) {
823+
/* Don't iterate over sections we know are !present: */
824+
if (i > __highest_present_section_nr)
825+
break;
826+
823827
err = add_memory_block(i);
824828
if (!ret)
825829
ret = err;

‎include/linux/mmzone.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,6 +1180,8 @@ static inline struct mem_section *__pfn_to_section(unsigned long pfn)
11801180
return __nr_to_section(pfn_to_section_nr(pfn));
11811181
}
11821182

1183+
extern int __highest_present_section_nr;
1184+
11831185
#ifndef CONFIG_HAVE_ARCH_PFN_VALID
11841186
static inline int pfn_valid(unsigned long pfn)
11851187
{

‎mm/sparse.c‎

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,44 @@ void __meminit mminit_validate_memmodel_limits(unsigned long *start_pfn,
168168
}
169169
}
170170

171+
/*
172+
* There are a number of times that we loop over NR_MEM_SECTIONS,
173+
* looking for section_present() on each. But, when we have very
174+
* large physical address spaces, NR_MEM_SECTIONS can also be
175+
* very large which makes the loops quite long.
176+
*
177+
* Keeping track of this gives us an easy way to break out of
178+
* those loops early.
179+
*/
180+
int __highest_present_section_nr;
181+
static void section_mark_present(struct mem_section *ms)
182+
{
183+
int section_nr = __section_nr(ms);
184+
185+
if (section_nr > __highest_present_section_nr)
186+
__highest_present_section_nr = section_nr;
187+
188+
ms->section_mem_map |= SECTION_MARKED_PRESENT;
189+
}
190+
191+
static inline int next_present_section_nr(int section_nr)
192+
{
193+
do {
194+
section_nr++;
195+
if (present_section_nr(section_nr))
196+
return section_nr;
197+
} while ((section_nr < NR_MEM_SECTIONS) &&
198+
(section_nr <= __highest_present_section_nr));
199+
200+
return -1;
201+
}
202+
#define for_each_present_section_nr(start, section_nr) \
203+
for (section_nr = next_present_section_nr(start-1); \
204+
((section_nr >= 0) && \
205+
(section_nr < NR_MEM_SECTIONS) && \
206+
(section_nr <= __highest_present_section_nr)); \
207+
section_nr = next_present_section_nr(section_nr))
208+
171209
/* Record a memory area against a node. */
172210
void __init memory_present(int nid, unsigned long start, unsigned long end)
173211
{
@@ -183,9 +221,10 @@ void __init memory_present(int nid, unsigned long start, unsigned long end)
183221
set_section_nid(section, nid);
184222

185223
ms = __nr_to_section(section);
186-
if (!ms->section_mem_map)
187-
ms->section_mem_map = sparse_encode_early_nid(nid) |
188-
SECTION_MARKED_PRESENT;
224+
if (!ms->section_mem_map) {
225+
ms->section_mem_map = sparse_encode_early_nid(nid);
226+
section_mark_present(ms);
227+
}
189228
}
190229
}
191230

@@ -476,23 +515,19 @@ static void __init alloc_usemap_and_memmap(void (*alloc_func)
476515
int nodeid_begin = 0;
477516
unsigned long pnum_begin = 0;
478517

479-
for (pnum = 0; pnum < NR_MEM_SECTIONS; pnum++) {
518+
for_each_present_section_nr(0, pnum) {
480519
struct mem_section *ms;
481520

482-
if (!present_section_nr(pnum))
483-
continue;
484521
ms = __nr_to_section(pnum);
485522
nodeid_begin = sparse_early_nid(ms);
486523
pnum_begin = pnum;
487524
break;
488525
}
489526
map_count = 1;
490-
for (pnum = pnum_begin + 1; pnum < NR_MEM_SECTIONS; pnum++) {
527+
for_each_present_section_nr(pnum_begin + 1, pnum) {
491528
struct mem_section *ms;
492529
int nodeid;
493530

494-
if (!present_section_nr(pnum))
495-
continue;
496531
ms = __nr_to_section(pnum);
497532
nodeid = sparse_early_nid(ms);
498533
if (nodeid == nodeid_begin) {
@@ -561,10 +596,7 @@ void __init sparse_init(void)
561596
(void *)map_map);
562597
#endif
563598

564-
for (pnum = 0; pnum < NR_MEM_SECTIONS; pnum++) {
565-
if (!present_section_nr(pnum))
566-
continue;
567-
599+
for_each_present_section_nr(0, pnum) {
568600
usemap = usemap_map[pnum];
569601
if (!usemap)
570602
continue;
@@ -722,7 +754,7 @@ int __meminit sparse_add_one_section(struct zone *zone, unsigned long start_pfn)
722754

723755
memset(memmap, 0, sizeof(struct page) * PAGES_PER_SECTION);
724756

725-
ms->section_mem_map |= SECTION_MARKED_PRESENT;
757+
section_mark_present(ms);
726758

727759
ret = sparse_init_one_section(ms, section_nr, memmap, usemap);
728760

0 commit comments

Comments
 (0)