/*1* bootmem - A boot-time physical memory allocator and configurator2*3* Copyright (C) 1999 Ingo Molnar4* 1999 Kanoj Sarcar, SGI5* 2008 Johannes Weiner6*7* Access to this subsystem has to be serialized externally (which is true8* for the boot process anyway).9*/10#include <linux/init.h>11#include <linux/pfn.h>12#include <linux/slab.h>13#include <linux/bootmem.h>14#include <linux/module.h>15#include <linux/kmemleak.h>16#include <linux/range.h>17#include <linux/memblock.h>1819#include <asm/bug.h>20#include <asm/io.h>21#include <asm/processor.h>2223#include "internal.h"2425#ifndef CONFIG_NEED_MULTIPLE_NODES26struct pglist_data __refdata contig_page_data;27EXPORT_SYMBOL(contig_page_data);28#endif2930unsigned long max_low_pfn;31unsigned long min_low_pfn;32unsigned long max_pfn;3334static void * __init __alloc_memory_core_early(int nid, u64 size, u64 align,35u64 goal, u64 limit)36{37void *ptr;38u64 addr;3940if (limit > memblock.current_limit)41limit = memblock.current_limit;4243addr = find_memory_core_early(nid, size, align, goal, limit);4445if (addr == MEMBLOCK_ERROR)46return NULL;4748ptr = phys_to_virt(addr);49memset(ptr, 0, size);50memblock_x86_reserve_range(addr, addr + size, "BOOTMEM");51/*52* The min_count is set to 0 so that bootmem allocated blocks53* are never reported as leaks.54*/55kmemleak_alloc(ptr, size, 0, 0);56return ptr;57}5859/*60* free_bootmem_late - free bootmem pages directly to page allocator61* @addr: starting address of the range62* @size: size of the range in bytes63*64* This is only useful when the bootmem allocator has already been torn65* down, but we are still initializing the system. Pages are given directly66* to the page allocator, no bootmem metadata is updated because it is gone.67*/68void __init free_bootmem_late(unsigned long addr, unsigned long size)69{70unsigned long cursor, end;7172kmemleak_free_part(__va(addr), size);7374cursor = PFN_UP(addr);75end = PFN_DOWN(addr + size);7677for (; cursor < end; cursor++) {78__free_pages_bootmem(pfn_to_page(cursor), 0);79totalram_pages++;80}81}8283static void __init __free_pages_memory(unsigned long start, unsigned long end)84{85int i;86unsigned long start_aligned, end_aligned;87int order = ilog2(BITS_PER_LONG);8889start_aligned = (start + (BITS_PER_LONG - 1)) & ~(BITS_PER_LONG - 1);90end_aligned = end & ~(BITS_PER_LONG - 1);9192if (end_aligned <= start_aligned) {93for (i = start; i < end; i++)94__free_pages_bootmem(pfn_to_page(i), 0);9596return;97}9899for (i = start; i < start_aligned; i++)100__free_pages_bootmem(pfn_to_page(i), 0);101102for (i = start_aligned; i < end_aligned; i += BITS_PER_LONG)103__free_pages_bootmem(pfn_to_page(i), order);104105for (i = end_aligned; i < end; i++)106__free_pages_bootmem(pfn_to_page(i), 0);107}108109unsigned long __init free_all_memory_core_early(int nodeid)110{111int i;112u64 start, end;113unsigned long count = 0;114struct range *range = NULL;115int nr_range;116117nr_range = get_free_all_memory_range(&range, nodeid);118119for (i = 0; i < nr_range; i++) {120start = range[i].start;121end = range[i].end;122count += end - start;123__free_pages_memory(start, end);124}125126return count;127}128129/**130* free_all_bootmem_node - release a node's free pages to the buddy allocator131* @pgdat: node to be released132*133* Returns the number of pages actually released.134*/135unsigned long __init free_all_bootmem_node(pg_data_t *pgdat)136{137register_page_bootmem_info_node(pgdat);138139/* free_all_memory_core_early(MAX_NUMNODES) will be called later */140return 0;141}142143/**144* free_all_bootmem - release free pages to the buddy allocator145*146* Returns the number of pages actually released.147*/148unsigned long __init free_all_bootmem(void)149{150/*151* We need to use MAX_NUMNODES instead of NODE_DATA(0)->node_id152* because in some case like Node0 doesn't have RAM installed153* low ram will be on Node1154* Use MAX_NUMNODES will make sure all ranges in early_node_map[]155* will be used instead of only Node0 related156*/157return free_all_memory_core_early(MAX_NUMNODES);158}159160/**161* free_bootmem_node - mark a page range as usable162* @pgdat: node the range resides on163* @physaddr: starting address of the range164* @size: size of the range in bytes165*166* Partial pages will be considered reserved and left as they are.167*168* The range must reside completely on the specified node.169*/170void __init free_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,171unsigned long size)172{173kmemleak_free_part(__va(physaddr), size);174memblock_x86_free_range(physaddr, physaddr + size);175}176177/**178* free_bootmem - mark a page range as usable179* @addr: starting address of the range180* @size: size of the range in bytes181*182* Partial pages will be considered reserved and left as they are.183*184* The range must be contiguous but may span node boundaries.185*/186void __init free_bootmem(unsigned long addr, unsigned long size)187{188kmemleak_free_part(__va(addr), size);189memblock_x86_free_range(addr, addr + size);190}191192static void * __init ___alloc_bootmem_nopanic(unsigned long size,193unsigned long align,194unsigned long goal,195unsigned long limit)196{197void *ptr;198199if (WARN_ON_ONCE(slab_is_available()))200return kzalloc(size, GFP_NOWAIT);201202restart:203204ptr = __alloc_memory_core_early(MAX_NUMNODES, size, align, goal, limit);205206if (ptr)207return ptr;208209if (goal != 0) {210goal = 0;211goto restart;212}213214return NULL;215}216217/**218* __alloc_bootmem_nopanic - allocate boot memory without panicking219* @size: size of the request in bytes220* @align: alignment of the region221* @goal: preferred starting address of the region222*223* The goal is dropped if it can not be satisfied and the allocation will224* fall back to memory below @goal.225*226* Allocation may happen on any node in the system.227*228* Returns NULL on failure.229*/230void * __init __alloc_bootmem_nopanic(unsigned long size, unsigned long align,231unsigned long goal)232{233unsigned long limit = -1UL;234235return ___alloc_bootmem_nopanic(size, align, goal, limit);236}237238static void * __init ___alloc_bootmem(unsigned long size, unsigned long align,239unsigned long goal, unsigned long limit)240{241void *mem = ___alloc_bootmem_nopanic(size, align, goal, limit);242243if (mem)244return mem;245/*246* Whoops, we cannot satisfy the allocation request.247*/248printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size);249panic("Out of memory");250return NULL;251}252253/**254* __alloc_bootmem - allocate boot memory255* @size: size of the request in bytes256* @align: alignment of the region257* @goal: preferred starting address of the region258*259* The goal is dropped if it can not be satisfied and the allocation will260* fall back to memory below @goal.261*262* Allocation may happen on any node in the system.263*264* The function panics if the request can not be satisfied.265*/266void * __init __alloc_bootmem(unsigned long size, unsigned long align,267unsigned long goal)268{269unsigned long limit = -1UL;270271return ___alloc_bootmem(size, align, goal, limit);272}273274/**275* __alloc_bootmem_node - allocate boot memory from a specific node276* @pgdat: node to allocate from277* @size: size of the request in bytes278* @align: alignment of the region279* @goal: preferred starting address of the region280*281* The goal is dropped if it can not be satisfied and the allocation will282* fall back to memory below @goal.283*284* Allocation may fall back to any node in the system if the specified node285* can not hold the requested memory.286*287* The function panics if the request can not be satisfied.288*/289void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,290unsigned long align, unsigned long goal)291{292void *ptr;293294if (WARN_ON_ONCE(slab_is_available()))295return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);296297ptr = __alloc_memory_core_early(pgdat->node_id, size, align,298goal, -1ULL);299if (ptr)300return ptr;301302return __alloc_memory_core_early(MAX_NUMNODES, size, align,303goal, -1ULL);304}305306void * __init __alloc_bootmem_node_high(pg_data_t *pgdat, unsigned long size,307unsigned long align, unsigned long goal)308{309return __alloc_bootmem_node(pgdat, size, align, goal);310}311312#ifdef CONFIG_SPARSEMEM313/**314* alloc_bootmem_section - allocate boot memory from a specific section315* @size: size of the request in bytes316* @section_nr: sparse map section to allocate from317*318* Return NULL on failure.319*/320void * __init alloc_bootmem_section(unsigned long size,321unsigned long section_nr)322{323unsigned long pfn, goal, limit;324325pfn = section_nr_to_pfn(section_nr);326goal = pfn << PAGE_SHIFT;327limit = section_nr_to_pfn(section_nr + 1) << PAGE_SHIFT;328329return __alloc_memory_core_early(early_pfn_to_nid(pfn), size,330SMP_CACHE_BYTES, goal, limit);331}332#endif333334void * __init __alloc_bootmem_node_nopanic(pg_data_t *pgdat, unsigned long size,335unsigned long align, unsigned long goal)336{337void *ptr;338339if (WARN_ON_ONCE(slab_is_available()))340return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);341342ptr = __alloc_memory_core_early(pgdat->node_id, size, align,343goal, -1ULL);344if (ptr)345return ptr;346347return __alloc_bootmem_nopanic(size, align, goal);348}349350#ifndef ARCH_LOW_ADDRESS_LIMIT351#define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL352#endif353354/**355* __alloc_bootmem_low - allocate low boot memory356* @size: size of the request in bytes357* @align: alignment of the region358* @goal: preferred starting address of the region359*360* The goal is dropped if it can not be satisfied and the allocation will361* fall back to memory below @goal.362*363* Allocation may happen on any node in the system.364*365* The function panics if the request can not be satisfied.366*/367void * __init __alloc_bootmem_low(unsigned long size, unsigned long align,368unsigned long goal)369{370return ___alloc_bootmem(size, align, goal, ARCH_LOW_ADDRESS_LIMIT);371}372373/**374* __alloc_bootmem_low_node - allocate low boot memory from a specific node375* @pgdat: node to allocate from376* @size: size of the request in bytes377* @align: alignment of the region378* @goal: preferred starting address of the region379*380* The goal is dropped if it can not be satisfied and the allocation will381* fall back to memory below @goal.382*383* Allocation may fall back to any node in the system if the specified node384* can not hold the requested memory.385*386* The function panics if the request can not be satisfied.387*/388void * __init __alloc_bootmem_low_node(pg_data_t *pgdat, unsigned long size,389unsigned long align, unsigned long goal)390{391void *ptr;392393if (WARN_ON_ONCE(slab_is_available()))394return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);395396ptr = __alloc_memory_core_early(pgdat->node_id, size, align,397goal, ARCH_LOW_ADDRESS_LIMIT);398if (ptr)399return ptr;400401return __alloc_memory_core_early(MAX_NUMNODES, size, align,402goal, ARCH_LOW_ADDRESS_LIMIT);403}404405406