Add a new arch_early_alloc() interface for x86-64 v3 This allows to allocate memory really early before bootmem is setup. And a symbol that can be tested by the preprocessor. pgtable.h is probably not the best include for it, but also not the worst. This starts to allocate at 128MB and falls back to lower memory only if that fails. Rationale is that this will handle 64MB kdump kernels loaded at 16MB. TBD need to make it boot again with e820.nr_map == 0 ? TBD need to move e820 map parsing earlier before using this v1->v2: [includes typo fix from Eric Dumazet] v1->v2: [Move default start to 128MB instead of 32MB] v2->v3: Move ARCH_EARLY_ALLOC into Kconfig Cc: peterz@infradead.org Signed-off-by: Andi Kleen --- arch/x86/Kconfig | 4 ++++ arch/x86/kernel/e820_64.c | 14 ++++++++++++++ include/asm-x86/pgtable_64.h | 2 ++ 3 files changed, 20 insertions(+) Index: linux/arch/x86/kernel/e820_64.c =================================================================== --- linux.orig/arch/x86/kernel/e820_64.c +++ linux/arch/x86/kernel/e820_64.c @@ -819,3 +819,17 @@ int __init arch_get_ram_range(int slot, max_pfn << PAGE_SHIFT) - *addr; return i + 1; } + +#define EARLY_ALLOC_START (128<<20) +__init void *arch_early_alloc(unsigned long size) +{ + unsigned long p = find_e820_area(0 /*EARLY_ALLOC_START*/, -1UL, size); + if (p == -1ULL) { + /* Risk filling the DMA zone */ + p = find_e820_area(0, -1UL, size); + if (p == -1ULL) + panic("arch_early_alloc %lu failed", size); + } + reserve_early(p, p + size); + return __va(p); +} Index: linux/include/asm-x86/pgtable_64.h =================================================================== --- linux.orig/include/asm-x86/pgtable_64.h +++ linux/include/asm-x86/pgtable_64.h @@ -436,6 +436,8 @@ pte_t *lookup_address(unsigned long addr #define kc_offset_to_vaddr(o) \ (((o) & (1UL << (__VIRTUAL_MASK_SHIFT-1))) ? ((o) | (~__VIRTUAL_MASK)) : (o)) +extern void *arch_early_alloc(unsigned long size); + #define __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG #define __HAVE_ARCH_PTEP_GET_AND_CLEAR #define __HAVE_ARCH_PTEP_GET_AND_CLEAR_FULL Index: linux/arch/x86/Kconfig =================================================================== --- linux.orig/arch/x86/Kconfig +++ linux/arch/x86/Kconfig @@ -87,6 +87,10 @@ config RWSEM_GENERIC_SPINLOCK config RWSEM_XCHGADD_ALGORITHM def_bool X86_XADD +config EARLY_ALLOC + depends on X86_64 + def_bool y + config ARCH_HAS_ILOG2_U32 def_bool n