Yinghai Lu | 0932587 | 2011-02-24 14:43:05 +0100 | [diff] [blame] | 1 | /* |
| 2 | * bootmem - A boot-time physical memory allocator and configurator |
| 3 | * |
| 4 | * Copyright (C) 1999 Ingo Molnar |
| 5 | * 1999 Kanoj Sarcar, SGI |
| 6 | * 2008 Johannes Weiner |
| 7 | * |
| 8 | * Access to this subsystem has to be serialized externally (which is true |
| 9 | * for the boot process anyway). |
| 10 | */ |
| 11 | #include <linux/init.h> |
| 12 | #include <linux/pfn.h> |
| 13 | #include <linux/slab.h> |
| 14 | #include <linux/bootmem.h> |
Paul Gortmaker | b95f1b31 | 2011-10-16 02:01:52 -0400 | [diff] [blame] | 15 | #include <linux/export.h> |
Yinghai Lu | 0932587 | 2011-02-24 14:43:05 +0100 | [diff] [blame] | 16 | #include <linux/kmemleak.h> |
| 17 | #include <linux/range.h> |
| 18 | #include <linux/memblock.h> |
| 19 | |
| 20 | #include <asm/bug.h> |
| 21 | #include <asm/io.h> |
| 22 | #include <asm/processor.h> |
| 23 | |
| 24 | #include "internal.h" |
| 25 | |
Yinghai Lu | e782ab4 | 2011-02-24 14:43:06 +0100 | [diff] [blame] | 26 | #ifndef CONFIG_NEED_MULTIPLE_NODES |
| 27 | struct pglist_data __refdata contig_page_data; |
| 28 | EXPORT_SYMBOL(contig_page_data); |
| 29 | #endif |
| 30 | |
Yinghai Lu | 0932587 | 2011-02-24 14:43:05 +0100 | [diff] [blame] | 31 | unsigned long max_low_pfn; |
| 32 | unsigned long min_low_pfn; |
| 33 | unsigned long max_pfn; |
| 34 | |
Yinghai Lu | 8bc1f91 | 2011-02-24 14:43:06 +0100 | [diff] [blame] | 35 | static void * __init __alloc_memory_core_early(int nid, u64 size, u64 align, |
| 36 | u64 goal, u64 limit) |
| 37 | { |
| 38 | void *ptr; |
| 39 | u64 addr; |
| 40 | |
| 41 | if (limit > memblock.current_limit) |
| 42 | limit = memblock.current_limit; |
| 43 | |
Tejun Heo | eb40c4c | 2011-07-12 10:46:35 +0200 | [diff] [blame] | 44 | addr = memblock_find_in_range_node(goal, limit, size, align, nid); |
Tejun Heo | 1f5026a | 2011-07-12 09:58:09 +0200 | [diff] [blame] | 45 | if (!addr) |
Yinghai Lu | 8bc1f91 | 2011-02-24 14:43:06 +0100 | [diff] [blame] | 46 | return NULL; |
| 47 | |
Joonsoo Kim | b476e29 | 2013-04-29 15:08:53 -0700 | [diff] [blame] | 48 | memblock_reserve(addr, size); |
Yinghai Lu | 8bc1f91 | 2011-02-24 14:43:06 +0100 | [diff] [blame] | 49 | ptr = phys_to_virt(addr); |
| 50 | memset(ptr, 0, size); |
Yinghai Lu | 8bc1f91 | 2011-02-24 14:43:06 +0100 | [diff] [blame] | 51 | /* |
| 52 | * The min_count is set to 0 so that bootmem allocated blocks |
| 53 | * are never reported as leaks. |
| 54 | */ |
| 55 | kmemleak_alloc(ptr, size, 0, 0); |
| 56 | return ptr; |
| 57 | } |
| 58 | |
Yinghai Lu | 0932587 | 2011-02-24 14:43:05 +0100 | [diff] [blame] | 59 | /* |
| 60 | * free_bootmem_late - free bootmem pages directly to page allocator |
| 61 | * @addr: starting address of the range |
| 62 | * @size: size of the range in bytes |
| 63 | * |
| 64 | * This is only useful when the bootmem allocator has already been torn |
| 65 | * down, but we are still initializing the system. Pages are given directly |
| 66 | * to the page allocator, no bootmem metadata is updated because it is gone. |
| 67 | */ |
| 68 | void __init free_bootmem_late(unsigned long addr, unsigned long size) |
| 69 | { |
| 70 | unsigned long cursor, end; |
| 71 | |
| 72 | kmemleak_free_part(__va(addr), size); |
| 73 | |
| 74 | cursor = PFN_UP(addr); |
| 75 | end = PFN_DOWN(addr + size); |
| 76 | |
| 77 | for (; cursor < end; cursor++) { |
| 78 | __free_pages_bootmem(pfn_to_page(cursor), 0); |
| 79 | totalram_pages++; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | static void __init __free_pages_memory(unsigned long start, unsigned long end) |
| 84 | { |
Robin Holt | 309d0b3 | 2013-11-12 15:07:23 -0800 | [diff] [blame] | 85 | int order; |
Yinghai Lu | 0932587 | 2011-02-24 14:43:05 +0100 | [diff] [blame] | 86 | |
Robin Holt | 309d0b3 | 2013-11-12 15:07:23 -0800 | [diff] [blame] | 87 | while (start < end) { |
| 88 | order = min(MAX_ORDER - 1UL, __ffs(start)); |
Yinghai Lu | 0932587 | 2011-02-24 14:43:05 +0100 | [diff] [blame] | 89 | |
Robin Holt | 309d0b3 | 2013-11-12 15:07:23 -0800 | [diff] [blame] | 90 | while (start + (1UL << order) > end) |
| 91 | order--; |
Yinghai Lu | 0932587 | 2011-02-24 14:43:05 +0100 | [diff] [blame] | 92 | |
Robin Holt | 309d0b3 | 2013-11-12 15:07:23 -0800 | [diff] [blame] | 93 | __free_pages_bootmem(pfn_to_page(start), order); |
| 94 | |
| 95 | start += (1UL << order); |
Yinghai Lu | 0932587 | 2011-02-24 14:43:05 +0100 | [diff] [blame] | 96 | } |
Yinghai Lu | 0932587 | 2011-02-24 14:43:05 +0100 | [diff] [blame] | 97 | } |
| 98 | |
Yinghai Lu | 29f6738 | 2012-07-11 14:02:56 -0700 | [diff] [blame] | 99 | static unsigned long __init __free_memory_core(phys_addr_t start, |
| 100 | phys_addr_t end) |
| 101 | { |
| 102 | unsigned long start_pfn = PFN_UP(start); |
| 103 | unsigned long end_pfn = min_t(unsigned long, |
| 104 | PFN_DOWN(end), max_low_pfn); |
| 105 | |
| 106 | if (start_pfn > end_pfn) |
| 107 | return 0; |
| 108 | |
| 109 | __free_pages_memory(start_pfn, end_pfn); |
| 110 | |
| 111 | return end_pfn - start_pfn; |
| 112 | } |
| 113 | |
Joonsoo Kim | b4def35 | 2013-04-29 15:08:52 -0700 | [diff] [blame] | 114 | static unsigned long __init free_low_memory_core_early(void) |
Yinghai Lu | 0932587 | 2011-02-24 14:43:05 +0100 | [diff] [blame] | 115 | { |
Yinghai Lu | 0932587 | 2011-02-24 14:43:05 +0100 | [diff] [blame] | 116 | unsigned long count = 0; |
Yinghai Lu | 29f6738 | 2012-07-11 14:02:56 -0700 | [diff] [blame] | 117 | phys_addr_t start, end, size; |
Tejun Heo | 8a9ca34 | 2011-07-12 11:16:02 +0200 | [diff] [blame] | 118 | u64 i; |
Yinghai Lu | 0932587 | 2011-02-24 14:43:05 +0100 | [diff] [blame] | 119 | |
Yinghai Lu | 29f6738 | 2012-07-11 14:02:56 -0700 | [diff] [blame] | 120 | for_each_free_mem_range(i, MAX_NUMNODES, &start, &end, NULL) |
| 121 | count += __free_memory_core(start, end); |
Yinghai Lu | 0932587 | 2011-02-24 14:43:05 +0100 | [diff] [blame] | 122 | |
Yinghai Lu | 29f6738 | 2012-07-11 14:02:56 -0700 | [diff] [blame] | 123 | /* free range that is used for reserved array if we allocate it */ |
| 124 | size = get_allocated_memblock_reserved_regions_info(&start); |
| 125 | if (size) |
| 126 | count += __free_memory_core(start, start + size); |
Yinghai Lu | 0932587 | 2011-02-24 14:43:05 +0100 | [diff] [blame] | 127 | |
| 128 | return count; |
| 129 | } |
| 130 | |
Jiang Liu | 7b4b2a0 | 2013-07-03 15:03:11 -0700 | [diff] [blame] | 131 | static int reset_managed_pages_done __initdata; |
| 132 | |
| 133 | static inline void __init reset_node_managed_pages(pg_data_t *pgdat) |
Jiang Liu | 9feedc9 | 2012-12-12 13:52:12 -0800 | [diff] [blame] | 134 | { |
| 135 | struct zone *z; |
| 136 | |
Jiang Liu | 7b4b2a0 | 2013-07-03 15:03:11 -0700 | [diff] [blame] | 137 | if (reset_managed_pages_done) |
| 138 | return; |
Jiang Liu | 9feedc9 | 2012-12-12 13:52:12 -0800 | [diff] [blame] | 139 | for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++) |
Jiang Liu | 7b4b2a0 | 2013-07-03 15:03:11 -0700 | [diff] [blame] | 140 | z->managed_pages = 0; |
| 141 | } |
| 142 | |
| 143 | void __init reset_all_zones_managed_pages(void) |
| 144 | { |
| 145 | struct pglist_data *pgdat; |
| 146 | |
| 147 | for_each_online_pgdat(pgdat) |
| 148 | reset_node_managed_pages(pgdat); |
| 149 | reset_managed_pages_done = 1; |
Jiang Liu | 9feedc9 | 2012-12-12 13:52:12 -0800 | [diff] [blame] | 150 | } |
| 151 | |
Yinghai Lu | 0932587 | 2011-02-24 14:43:05 +0100 | [diff] [blame] | 152 | /** |
Yinghai Lu | 0932587 | 2011-02-24 14:43:05 +0100 | [diff] [blame] | 153 | * free_all_bootmem - release free pages to the buddy allocator |
| 154 | * |
| 155 | * Returns the number of pages actually released. |
| 156 | */ |
| 157 | unsigned long __init free_all_bootmem(void) |
| 158 | { |
Jiang Liu | 0c98853 | 2013-07-03 15:03:24 -0700 | [diff] [blame] | 159 | unsigned long pages; |
| 160 | |
Jiang Liu | 7b4b2a0 | 2013-07-03 15:03:11 -0700 | [diff] [blame] | 161 | reset_all_zones_managed_pages(); |
Jiang Liu | 9feedc9 | 2012-12-12 13:52:12 -0800 | [diff] [blame] | 162 | |
Yinghai Lu | 0932587 | 2011-02-24 14:43:05 +0100 | [diff] [blame] | 163 | /* |
| 164 | * We need to use MAX_NUMNODES instead of NODE_DATA(0)->node_id |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 165 | * because in some case like Node0 doesn't have RAM installed |
Yinghai Lu | 0932587 | 2011-02-24 14:43:05 +0100 | [diff] [blame] | 166 | * low ram will be on Node1 |
Yinghai Lu | 0932587 | 2011-02-24 14:43:05 +0100 | [diff] [blame] | 167 | */ |
Jiang Liu | 0c98853 | 2013-07-03 15:03:24 -0700 | [diff] [blame] | 168 | pages = free_low_memory_core_early(); |
| 169 | totalram_pages += pages; |
| 170 | |
| 171 | return pages; |
Yinghai Lu | 0932587 | 2011-02-24 14:43:05 +0100 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | /** |
| 175 | * free_bootmem_node - mark a page range as usable |
| 176 | * @pgdat: node the range resides on |
| 177 | * @physaddr: starting address of the range |
| 178 | * @size: size of the range in bytes |
| 179 | * |
| 180 | * Partial pages will be considered reserved and left as they are. |
| 181 | * |
| 182 | * The range must reside completely on the specified node. |
| 183 | */ |
| 184 | void __init free_bootmem_node(pg_data_t *pgdat, unsigned long physaddr, |
| 185 | unsigned long size) |
| 186 | { |
| 187 | kmemleak_free_part(__va(physaddr), size); |
Tejun Heo | 24aa078 | 2011-07-12 11:16:06 +0200 | [diff] [blame] | 188 | memblock_free(physaddr, size); |
Yinghai Lu | 0932587 | 2011-02-24 14:43:05 +0100 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | /** |
| 192 | * free_bootmem - mark a page range as usable |
| 193 | * @addr: starting address of the range |
| 194 | * @size: size of the range in bytes |
| 195 | * |
| 196 | * Partial pages will be considered reserved and left as they are. |
| 197 | * |
| 198 | * The range must be contiguous but may span node boundaries. |
| 199 | */ |
| 200 | void __init free_bootmem(unsigned long addr, unsigned long size) |
| 201 | { |
| 202 | kmemleak_free_part(__va(addr), size); |
Tejun Heo | 24aa078 | 2011-07-12 11:16:06 +0200 | [diff] [blame] | 203 | memblock_free(addr, size); |
Yinghai Lu | 0932587 | 2011-02-24 14:43:05 +0100 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | static void * __init ___alloc_bootmem_nopanic(unsigned long size, |
| 207 | unsigned long align, |
| 208 | unsigned long goal, |
| 209 | unsigned long limit) |
| 210 | { |
| 211 | void *ptr; |
| 212 | |
| 213 | if (WARN_ON_ONCE(slab_is_available())) |
| 214 | return kzalloc(size, GFP_NOWAIT); |
| 215 | |
| 216 | restart: |
| 217 | |
| 218 | ptr = __alloc_memory_core_early(MAX_NUMNODES, size, align, goal, limit); |
| 219 | |
| 220 | if (ptr) |
| 221 | return ptr; |
| 222 | |
| 223 | if (goal != 0) { |
| 224 | goal = 0; |
| 225 | goto restart; |
| 226 | } |
| 227 | |
| 228 | return NULL; |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * __alloc_bootmem_nopanic - allocate boot memory without panicking |
| 233 | * @size: size of the request in bytes |
| 234 | * @align: alignment of the region |
| 235 | * @goal: preferred starting address of the region |
| 236 | * |
| 237 | * The goal is dropped if it can not be satisfied and the allocation will |
| 238 | * fall back to memory below @goal. |
| 239 | * |
| 240 | * Allocation may happen on any node in the system. |
| 241 | * |
| 242 | * Returns NULL on failure. |
| 243 | */ |
| 244 | void * __init __alloc_bootmem_nopanic(unsigned long size, unsigned long align, |
| 245 | unsigned long goal) |
| 246 | { |
| 247 | unsigned long limit = -1UL; |
| 248 | |
| 249 | return ___alloc_bootmem_nopanic(size, align, goal, limit); |
| 250 | } |
| 251 | |
| 252 | static void * __init ___alloc_bootmem(unsigned long size, unsigned long align, |
| 253 | unsigned long goal, unsigned long limit) |
| 254 | { |
| 255 | void *mem = ___alloc_bootmem_nopanic(size, align, goal, limit); |
| 256 | |
| 257 | if (mem) |
| 258 | return mem; |
| 259 | /* |
| 260 | * Whoops, we cannot satisfy the allocation request. |
| 261 | */ |
| 262 | printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size); |
| 263 | panic("Out of memory"); |
| 264 | return NULL; |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * __alloc_bootmem - allocate boot memory |
| 269 | * @size: size of the request in bytes |
| 270 | * @align: alignment of the region |
| 271 | * @goal: preferred starting address of the region |
| 272 | * |
| 273 | * The goal is dropped if it can not be satisfied and the allocation will |
| 274 | * fall back to memory below @goal. |
| 275 | * |
| 276 | * Allocation may happen on any node in the system. |
| 277 | * |
| 278 | * The function panics if the request can not be satisfied. |
| 279 | */ |
| 280 | void * __init __alloc_bootmem(unsigned long size, unsigned long align, |
| 281 | unsigned long goal) |
| 282 | { |
| 283 | unsigned long limit = -1UL; |
| 284 | |
| 285 | return ___alloc_bootmem(size, align, goal, limit); |
| 286 | } |
| 287 | |
Yinghai Lu | 99ab7b1 | 2012-07-11 14:02:53 -0700 | [diff] [blame] | 288 | void * __init ___alloc_bootmem_node_nopanic(pg_data_t *pgdat, |
Johannes Weiner | ba53986 | 2012-05-29 15:06:35 -0700 | [diff] [blame] | 289 | unsigned long size, |
| 290 | unsigned long align, |
| 291 | unsigned long goal, |
| 292 | unsigned long limit) |
| 293 | { |
| 294 | void *ptr; |
| 295 | |
| 296 | again: |
| 297 | ptr = __alloc_memory_core_early(pgdat->node_id, size, align, |
| 298 | goal, limit); |
| 299 | if (ptr) |
| 300 | return ptr; |
| 301 | |
| 302 | ptr = __alloc_memory_core_early(MAX_NUMNODES, size, align, |
| 303 | goal, limit); |
| 304 | if (ptr) |
| 305 | return ptr; |
| 306 | |
| 307 | if (goal) { |
| 308 | goal = 0; |
| 309 | goto again; |
| 310 | } |
| 311 | |
| 312 | return NULL; |
| 313 | } |
| 314 | |
| 315 | void * __init __alloc_bootmem_node_nopanic(pg_data_t *pgdat, unsigned long size, |
| 316 | unsigned long align, unsigned long goal) |
| 317 | { |
| 318 | if (WARN_ON_ONCE(slab_is_available())) |
| 319 | return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id); |
| 320 | |
| 321 | return ___alloc_bootmem_node_nopanic(pgdat, size, align, goal, 0); |
| 322 | } |
| 323 | |
| 324 | void * __init ___alloc_bootmem_node(pg_data_t *pgdat, unsigned long size, |
| 325 | unsigned long align, unsigned long goal, |
| 326 | unsigned long limit) |
| 327 | { |
| 328 | void *ptr; |
| 329 | |
| 330 | ptr = ___alloc_bootmem_node_nopanic(pgdat, size, align, goal, limit); |
| 331 | if (ptr) |
| 332 | return ptr; |
| 333 | |
| 334 | printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size); |
| 335 | panic("Out of memory"); |
| 336 | return NULL; |
| 337 | } |
| 338 | |
Yinghai Lu | 0932587 | 2011-02-24 14:43:05 +0100 | [diff] [blame] | 339 | /** |
| 340 | * __alloc_bootmem_node - allocate boot memory from a specific node |
| 341 | * @pgdat: node to allocate from |
| 342 | * @size: size of the request in bytes |
| 343 | * @align: alignment of the region |
| 344 | * @goal: preferred starting address of the region |
| 345 | * |
| 346 | * The goal is dropped if it can not be satisfied and the allocation will |
| 347 | * fall back to memory below @goal. |
| 348 | * |
| 349 | * Allocation may fall back to any node in the system if the specified node |
| 350 | * can not hold the requested memory. |
| 351 | * |
| 352 | * The function panics if the request can not be satisfied. |
| 353 | */ |
| 354 | void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size, |
| 355 | unsigned long align, unsigned long goal) |
| 356 | { |
Yinghai Lu | 0932587 | 2011-02-24 14:43:05 +0100 | [diff] [blame] | 357 | if (WARN_ON_ONCE(slab_is_available())) |
| 358 | return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id); |
| 359 | |
Johannes Weiner | ba53986 | 2012-05-29 15:06:35 -0700 | [diff] [blame] | 360 | return ___alloc_bootmem_node(pgdat, size, align, goal, 0); |
Yinghai Lu | 0932587 | 2011-02-24 14:43:05 +0100 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | void * __init __alloc_bootmem_node_high(pg_data_t *pgdat, unsigned long size, |
| 364 | unsigned long align, unsigned long goal) |
| 365 | { |
Yinghai Lu | 0932587 | 2011-02-24 14:43:05 +0100 | [diff] [blame] | 366 | return __alloc_bootmem_node(pgdat, size, align, goal); |
Yinghai Lu | 0932587 | 2011-02-24 14:43:05 +0100 | [diff] [blame] | 367 | } |
| 368 | |
Yinghai Lu | 0932587 | 2011-02-24 14:43:05 +0100 | [diff] [blame] | 369 | #ifndef ARCH_LOW_ADDRESS_LIMIT |
| 370 | #define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL |
| 371 | #endif |
| 372 | |
| 373 | /** |
| 374 | * __alloc_bootmem_low - allocate low boot memory |
| 375 | * @size: size of the request in bytes |
| 376 | * @align: alignment of the region |
| 377 | * @goal: preferred starting address of the region |
| 378 | * |
| 379 | * The goal is dropped if it can not be satisfied and the allocation will |
| 380 | * fall back to memory below @goal. |
| 381 | * |
| 382 | * Allocation may happen on any node in the system. |
| 383 | * |
| 384 | * The function panics if the request can not be satisfied. |
| 385 | */ |
| 386 | void * __init __alloc_bootmem_low(unsigned long size, unsigned long align, |
| 387 | unsigned long goal) |
| 388 | { |
| 389 | return ___alloc_bootmem(size, align, goal, ARCH_LOW_ADDRESS_LIMIT); |
| 390 | } |
| 391 | |
Yinghai Lu | 38fa417 | 2013-01-24 12:20:15 -0800 | [diff] [blame] | 392 | void * __init __alloc_bootmem_low_nopanic(unsigned long size, |
| 393 | unsigned long align, |
| 394 | unsigned long goal) |
| 395 | { |
| 396 | return ___alloc_bootmem_nopanic(size, align, goal, |
| 397 | ARCH_LOW_ADDRESS_LIMIT); |
| 398 | } |
| 399 | |
Yinghai Lu | 0932587 | 2011-02-24 14:43:05 +0100 | [diff] [blame] | 400 | /** |
| 401 | * __alloc_bootmem_low_node - allocate low boot memory from a specific node |
| 402 | * @pgdat: node to allocate from |
| 403 | * @size: size of the request in bytes |
| 404 | * @align: alignment of the region |
| 405 | * @goal: preferred starting address of the region |
| 406 | * |
| 407 | * The goal is dropped if it can not be satisfied and the allocation will |
| 408 | * fall back to memory below @goal. |
| 409 | * |
| 410 | * Allocation may fall back to any node in the system if the specified node |
| 411 | * can not hold the requested memory. |
| 412 | * |
| 413 | * The function panics if the request can not be satisfied. |
| 414 | */ |
| 415 | void * __init __alloc_bootmem_low_node(pg_data_t *pgdat, unsigned long size, |
| 416 | unsigned long align, unsigned long goal) |
| 417 | { |
Yinghai Lu | 0932587 | 2011-02-24 14:43:05 +0100 | [diff] [blame] | 418 | if (WARN_ON_ONCE(slab_is_available())) |
| 419 | return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id); |
| 420 | |
Johannes Weiner | ba53986 | 2012-05-29 15:06:35 -0700 | [diff] [blame] | 421 | return ___alloc_bootmem_node(pgdat, size, align, goal, |
| 422 | ARCH_LOW_ADDRESS_LIMIT); |
Yinghai Lu | 0932587 | 2011-02-24 14:43:05 +0100 | [diff] [blame] | 423 | } |