Skip to contentSkip to conversation

mm, sparsemem: break out of loops early

PATCH New [email protected] 1 participant 1 message Jul 6, 2017

Subsystems (5)
lore
Share & tools
lore

[email protected]

author maintainer
#0 lore raw

PATCH 27/108 mm, sparsemem: break out of loops early

From: Dave Hansen <[email protected]>
Subject: 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".

---

 drivers/base/memory.c  |    4 ++
 include/linux/mmzone.h |    2 +
 mm/sparse.c            |   60 +++++++++++++++++++++++++++++----------
 3 files changed, 52 insertions(+), 14 deletions(-)
drivers/base/memory.c renamed from drivers/base/memory.c~mm-sparsemem-break-out-of-loops-early +4 -0
diff -puN drivers/base/memory.c~mm-sparsemem-break-out-of-loops-early drivers/base/memory.c
@@ -820,6 +820,10 @@ int __init memory_dev_init(void)
 	 */
 	mutex_lock(&mem_sysfs_mutex);
 	for (i = 0; i < NR_MEM_SECTIONS; i += sections_per_block) {
+		/* Don't iterate over sections we know are !present: */
+		if (i > __highest_present_section_nr)
+			break;
+
 		err = add_memory_block(i);
 		if (!ret)
 			ret = err;
include/linux/mmzone.h renamed from include/linux/mmzone.h~mm-sparsemem-break-out-of-loops-early +2 -0
diff -puN include/linux/mmzone.h~mm-sparsemem-break-out-of-loops-early include/linux/mmzone.h
@@ -1180,6 +1180,8 @@ static inline struct mem_section *__pfn_
 	return __nr_to_section(pfn_to_section_nr(pfn));
 }
 
+extern int __highest_present_section_nr;
+
 #ifndef CONFIG_HAVE_ARCH_PFN_VALID
 static inline int pfn_valid(unsigned long pfn)
 {
mm/sparse.c renamed from mm/sparse.c~mm-sparsemem-break-out-of-loops-early +44 -12

88 lines not on this page · raw message