Skip to contentSkip to conversation

x86,mm: fix init_mem_mapping() when the first memory chunk is small

PATCH In review David Vrabel 2 participants 3 messages Mar 8, 2013

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86,mm: fix init_mem_mapping() when the first memory chunk is small

In init_mem_mapping(), if the first chunk of memory that is mapped is small, there will not be enough mapped pages to allocate page table pages for the next (larger) chunk.

Estimate how many pages are used for the mappings so far and how many are needed for a larger chunk, and only increase step_size if there are enough free pages.

This fixes a boot failure on a system where the first chunk of memory mapped only had 3 pages in it.

init_memory_mapping: [mem 0x00000000-0x000fffff]
init_memory_mapping: [mem 0x20d000000-0x20d002fff]
init_memory_mapping: [mem 0x20c000000-0x20cffffff]

Kernel panic - not syncing: alloc_low_page: can not alloc memory
---
 arch/x86/mm/init.c |   21 +++++++++++++++------
 1 files changed, 15 insertions(+), 6 deletions(-)
arch/x86/mm/init.c +15 -6
index 4903a03..0cc7afb 100644
@@ -389,6 +389,12 @@ static unsigned long __init init_range_memory_mapping(
 	return mapped_ram_size;
 }
 
+/* Estimate of the number of pages needed to page 'size' bytes. */
+static unsigned long __init nr_pages_to_map(unsigned long size)
+{
+	return DIV_ROUND_UP(size, PMD_SIZE) + DIV_ROUND_UP(size, PUD_SIZE);
+}
+
 /* (PUD_SHIFT-PMD_SHIFT)/2 */
 #define STEP_SIZE_SHIFT 5
 void __init init_mem_mapping(void)
@@ -397,7 +403,7 @@ void __init init_mem_mapping(void)
 	unsigned long step_size;
 	unsigned long addr;
 	unsigned long mapped_ram_size = 0;
-	unsigned long new_mapped_ram_size;
+	unsigned long mapped_pages;
 
 	probe_page_size_mask();
 
@@ -427,14 +433,17 @@ void __init init_mem_mapping(void)
 				start = ISA_END_ADDRESS;
 		} else
 			start = ISA_END_ADDRESS;
-		new_mapped_ram_size = init_range_memory_mapping(start,
-							last_start);
+		mapped_ram_size += init_range_memory_mapping(start,
+							     last_start);
+		mapped_pages = mapped_ram_size >> PAGE_SHIFT;
 		last_start = start;
 		min_pfn_mapped = last_start >> PAGE_SHIFT;
-		/* only increase step_size after big range get mapped */
-		if (new_mapped_ram_size > mapped_ram_size)
+
+		/* Only increase step_size if there is enough mapped
+		   ram to map the larger block. */
+		if (nr_pages_to_map(step_size << STEP_SIZE_SHIFT)
+		    < mapped_pages - nr_pages_to_map(mapped_ram_size))
 			step_size <<= STEP_SIZE_SHIFT;
-		mapped_ram_size += new_mapped_ram_size;
 	}
 
 	if (real_end < end)
Signature · 1 line
1.7.2.5
#1 lore raw
Quoted 8 lines · David Vrabel

In init_mem_mapping(), if the first chunk of memory that is mapped is small, there will not be enough mapped pages to allocate page table pages for the next (larger) chunk.

Estimate how many pages are used for the mappings so far and how many are needed for a larger chunk, and only increase step_size if there are enough free pages.

Quoted 7 lines

This fixes a boot failure on a system where the first chunk of memory mapped only had 3 pages in it.

init_memory_mapping: [mem 0x00000000-0x000fffff]
init_memory_mapping: [mem 0x20d000000-0x20d002fff]
init_memory_mapping: [mem 0x20c000000-0x20cffffff]

Kernel panic - not syncing: alloc_low_page: can not alloc memory

Can you check current linus tree?

https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=98e7a989979b185f49e86ddaed2ad6890299d9f0

should fix the problem with your system.

Thanks

Yinghai

#2 lore raw
Quoted 17 lines · Yinghai Lu
Quoted 8 lines · David Vrabel

In init_mem_mapping(), if the first chunk of memory that is mapped is small, there will not be enough mapped pages to allocate page table pages for the next (larger) chunk.

Estimate how many pages are used for the mappings so far and how many are needed for a larger chunk, and only increase step_size if there are enough free pages.

Quoted 7 lines

This fixes a boot failure on a system where the first chunk of memory mapped only had 3 pages in it.

init_memory_mapping: [mem 0x00000000-0x000fffff]
init_memory_mapping: [mem 0x20d000000-0x20d002fff]
init_memory_mapping: [mem 0x20c000000-0x20cffffff]

Kernel panic - not syncing: alloc_low_page: can not alloc memory
Quoted 5 lines

Can you check current linus tree?

https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=98e7a989979b185f49e86ddaed2ad6890299d9f0

should fix the problem with your system.

Yes, that fixes it thanks.

David