memblock.c 56.5 KB
Newer Older
Yinghai Lu's avatar
Yinghai Lu committed
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * Procedures for maintaining information about logical memory blocks.
 *
 * Peter Bergner, IBM Corp.	June 2001.
 * Copyright (C) 2001 Peter Bergner.
 *
 *      This program is free software; you can redistribute it and/or
 *      modify it under the terms of the GNU General Public License
 *      as published by the Free Software Foundation; either version
 *      2 of the License, or (at your option) any later version.
 */

#include <linux/kernel.h>
14
#include <linux/slab.h>
Yinghai Lu's avatar
Yinghai Lu committed
15 16
#include <linux/init.h>
#include <linux/bitops.h>
17
#include <linux/poison.h>
18
#include <linux/pfn.h>
19
#include <linux/debugfs.h>
20
#include <linux/kmemleak.h>
21
#include <linux/seq_file.h>
Yinghai Lu's avatar
Yinghai Lu committed
22 23
#include <linux/memblock.h>

24
#include <asm/sections.h>
25 26 27
#include <linux/io.h>

#include "internal.h"
28

29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
/**
 * DOC: memblock overview
 *
 * Memblock is a method of managing memory regions during the early
 * boot period when the usual kernel memory allocators are not up and
 * running.
 *
 * Memblock views the system memory as collections of contiguous
 * regions. There are several types of these collections:
 *
 * * ``memory`` - describes the physical memory available to the
 *   kernel; this may differ from the actual physical memory installed
 *   in the system, for instance when the memory is restricted with
 *   ``mem=`` command line parameter
 * * ``reserved`` - describes the regions that were allocated
 * * ``physmap`` - describes the actual physical memory regardless of
 *   the possible restrictions; the ``physmap`` type is only available
 *   on some architectures.
 *
 * Each region is represented by :c:type:`struct memblock_region` that
 * defines the region extents, its attributes and NUMA node id on NUMA
 * systems. Every memory type is described by the :c:type:`struct
 * memblock_type` which contains an array of memory regions along with
 * the allocator metadata. The memory types are nicely wrapped with
 * :c:type:`struct memblock`. This structure is statically initialzed
 * at build time. The region arrays for the "memory" and "reserved"
 * types are initially sized to %INIT_MEMBLOCK_REGIONS and for the
 * "physmap" type to %INIT_PHYSMEM_REGIONS.
 * The :c:func:`memblock_allow_resize` enables automatic resizing of
 * the region arrays during addition of new regions. This feature
 * should be used with care so that memory allocated for the region
 * array will not overlap with areas that should be reserved, for
 * example initrd.
 *
 * The early architecture setup should tell memblock what the physical
 * memory layout is by using :c:func:`memblock_add` or
 * :c:func:`memblock_add_node` functions. The first function does not
 * assign the region to a NUMA node and it is appropriate for UMA
 * systems. Yet, it is possible to use it on NUMA systems as well and
 * assign the region to a NUMA node later in the setup process using
 * :c:func:`memblock_set_node`. The :c:func:`memblock_add_node`
 * performs such an assignment directly.
 *
 * Once memblock is setup the memory can be allocated using either
 * memblock or bootmem APIs.
 *
 * As the system boot progresses, the architecture specific
 * :c:func:`mem_init` function frees all the memory to the buddy page
 * allocator.
 *
 * If an architecure enables %CONFIG_ARCH_DISCARD_MEMBLOCK, the
 * memblock data structures will be discarded after the system
 * initialization compltes.
 */

Mike Rapoport's avatar
Mike Rapoport committed
84 85 86 87 88 89 90 91 92 93
#ifndef CONFIG_NEED_MULTIPLE_NODES
struct pglist_data __refdata contig_page_data;
EXPORT_SYMBOL(contig_page_data);
#endif

unsigned long max_low_pfn;
unsigned long min_low_pfn;
unsigned long max_pfn;
unsigned long long max_possible_pfn;

94 95
static struct memblock_region memblock_memory_init_regions[INIT_MEMBLOCK_REGIONS] __initdata_memblock;
static struct memblock_region memblock_reserved_init_regions[INIT_MEMBLOCK_REGIONS] __initdata_memblock;
96 97 98
#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
static struct memblock_region memblock_physmem_init_regions[INIT_PHYSMEM_REGIONS] __initdata_memblock;
#endif
99 100 101 102 103

struct memblock memblock __initdata_memblock = {
	.memory.regions		= memblock_memory_init_regions,
	.memory.cnt		= 1,	/* empty dummy entry */
	.memory.max		= INIT_MEMBLOCK_REGIONS,
104
	.memory.name		= "memory",
105 106 107 108

	.reserved.regions	= memblock_reserved_init_regions,
	.reserved.cnt		= 1,	/* empty dummy entry */
	.reserved.max		= INIT_MEMBLOCK_REGIONS,
109
	.reserved.name		= "reserved",
110

111 112 113 114
#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
	.physmem.regions	= memblock_physmem_init_regions,
	.physmem.cnt		= 1,	/* empty dummy entry */
	.physmem.max		= INIT_PHYSMEM_REGIONS,
115
	.physmem.name		= "physmem",
116 117
#endif

118
	.bottom_up		= false,
119 120
	.current_limit		= MEMBLOCK_ALLOC_ANYWHERE,
};
Yinghai Lu's avatar
Yinghai Lu committed
121

122
int memblock_debug __initdata_memblock;
123
static bool system_has_some_mirror __initdata_memblock = false;
124
static int memblock_can_resize __initdata_memblock;
125 126
static int memblock_memory_in_slab __initdata_memblock = 0;
static int memblock_reserved_in_slab __initdata_memblock = 0;
Yinghai Lu's avatar
Yinghai Lu committed
127

128
enum memblock_flags __init_memblock choose_memblock_flags(void)
129 130 131 132
{
	return system_has_some_mirror ? MEMBLOCK_MIRROR : MEMBLOCK_NONE;
}

133 134 135
/* adjust *@size so that (@base + *@size) doesn't overflow, return new size */
static inline phys_addr_t memblock_cap_size(phys_addr_t base, phys_addr_t *size)
{
136
	return *size = min(*size, PHYS_ADDR_MAX - base);
137 138
}

139 140 141
/*
 * Address comparison utilities
 */
142
static unsigned long __init_memblock memblock_addrs_overlap(phys_addr_t base1, phys_addr_t size1,
143
				       phys_addr_t base2, phys_addr_t size2)
Yinghai Lu's avatar
Yinghai Lu committed
144 145 146 147
{
	return ((base1 < (base2 + size2)) && (base2 < (base1 + size1)));
}

148
bool __init_memblock memblock_overlaps_region(struct memblock_type *type,
149
					phys_addr_t base, phys_addr_t size)
150 151 152
{
	unsigned long i;

153 154 155
	for (i = 0; i < type->cnt; i++)
		if (memblock_addrs_overlap(base, size, type->regions[i].base,
					   type->regions[i].size))
156
			break;
157
	return i < type->cnt;
158 159
}

160
/**
161 162
 * __memblock_find_range_bottom_up - find free area utility in bottom-up
 * @start: start of candidate range
163 164
 * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
 *       %MEMBLOCK_ALLOC_ACCESSIBLE
165 166
 * @size: size of free area to find
 * @align: alignment of free area to find
167
 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
168
 * @flags: pick from blocks based on memory attributes
169 170 171
 *
 * Utility called from memblock_find_in_range_node(), find free area bottom-up.
 *
172
 * Return:
173 174 175 176
 * Found address on success, 0 on failure.
 */
static phys_addr_t __init_memblock
__memblock_find_range_bottom_up(phys_addr_t start, phys_addr_t end,
177
				phys_addr_t size, phys_addr_t align, int nid,
178
				enum memblock_flags flags)
179 180 181 182
{
	phys_addr_t this_start, this_end, cand;
	u64 i;

183
	for_each_free_mem_range(i, nid, flags, &this_start, &this_end, NULL) {
184 185 186 187 188 189 190 191 192 193 194
		this_start = clamp(this_start, start, end);
		this_end = clamp(this_end, start, end);

		cand = round_up(this_start, align);
		if (cand < this_end && this_end - cand >= size)
			return cand;
	}

	return 0;
}

195
/**
196
 * __memblock_find_range_top_down - find free area utility, in top-down
197
 * @start: start of candidate range
198 199
 * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
 *       %MEMBLOCK_ALLOC_ACCESSIBLE
200 201
 * @size: size of free area to find
 * @align: alignment of free area to find
202
 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
203
 * @flags: pick from blocks based on memory attributes
204
 *
205
 * Utility called from memblock_find_in_range_node(), find free area top-down.
206
 *
207
 * Return:
208
 * Found address on success, 0 on failure.
209
 */
210 211
static phys_addr_t __init_memblock
__memblock_find_range_top_down(phys_addr_t start, phys_addr_t end,
212
			       phys_addr_t size, phys_addr_t align, int nid,
213
			       enum memblock_flags flags)
214 215 216 217
{
	phys_addr_t this_start, this_end, cand;
	u64 i;

218 219
	for_each_free_mem_range_reverse(i, nid, flags, &this_start, &this_end,
					NULL) {
220 221 222 223 224 225 226 227 228 229
		this_start = clamp(this_start, start, end);
		this_end = clamp(this_end, start, end);

		if (this_end < size)
			continue;

		cand = round_down(this_end - size, align);
		if (cand >= this_start)
			return cand;
	}
230

231 232
	return 0;
}
233

234 235 236 237
/**
 * memblock_find_in_range_node - find free area in given range and node
 * @size: size of free area to find
 * @align: alignment of free area to find
238
 * @start: start of candidate range
239 240
 * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
 *       %MEMBLOCK_ALLOC_ACCESSIBLE
241
 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
242
 * @flags: pick from blocks based on memory attributes
243 244 245
 *
 * Find @size free area aligned to @align in the specified range and node.
 *
246 247 248 249 250 251 252 253
 * When allocation direction is bottom-up, the @start should be greater
 * than the end of the kernel image. Otherwise, it will be trimmed. The
 * reason is that we want the bottom-up allocation just near the kernel
 * image so it is highly likely that the allocated memory and the kernel
 * will reside in the same node.
 *
 * If bottom-up allocation failed, will try to allocate memory top-down.
 *
254
 * Return:
255
 * Found address on success, 0 on failure.
256
 */
257 258
phys_addr_t __init_memblock memblock_find_in_range_node(phys_addr_t size,
					phys_addr_t align, phys_addr_t start,
259 260
					phys_addr_t end, int nid,
					enum memblock_flags flags)
261
{
262
	phys_addr_t kernel_end, ret;
263

264 265 266 267 268 269 270
	/* pump up @end */
	if (end == MEMBLOCK_ALLOC_ACCESSIBLE)
		end = memblock.current_limit;

	/* avoid allocating the first page */
	start = max_t(phys_addr_t, start, PAGE_SIZE);
	end = max(start, end);
271 272 273 274 275 276 277 278 279 280 281 282 283 284
	kernel_end = __pa_symbol(_end);

	/*
	 * try bottom-up allocation only when bottom-up mode
	 * is set and @end is above the kernel image.
	 */
	if (memblock_bottom_up() && end > kernel_end) {
		phys_addr_t bottom_up_start;

		/* make sure we will allocate above the kernel */
		bottom_up_start = max(start, kernel_end);

		/* ok, try bottom-up allocation first */
		ret = __memblock_find_range_bottom_up(bottom_up_start, end,
285
						      size, align, nid, flags);
286 287 288 289 290 291 292 293 294 295 296 297 298
		if (ret)
			return ret;

		/*
		 * we always limit bottom-up allocation above the kernel,
		 * but top-down allocation doesn't have the limit, so
		 * retrying top-down allocation may succeed when bottom-up
		 * allocation failed.
		 *
		 * bottom-up allocation is expected to be fail very rarely,
		 * so we use WARN_ONCE() here to see the stack trace if
		 * fail happens.
		 */
299 300
		WARN_ONCE(IS_ENABLED(CONFIG_MEMORY_HOTREMOVE),
			  "memblock: bottom-up allocation failed, memory hotremove may be affected\n");
301
	}
302

303 304
	return __memblock_find_range_top_down(start, end, size, align, nid,
					      flags);
305 306
}

307 308 309
/**
 * memblock_find_in_range - find free area in given range
 * @start: start of candidate range
310 311
 * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
 *       %MEMBLOCK_ALLOC_ACCESSIBLE
312 313 314 315 316
 * @size: size of free area to find
 * @align: alignment of free area to find
 *
 * Find @size free area aligned to @align in the specified range.
 *
317
 * Return:
318
 * Found address on success, 0 on failure.
319
 */
320 321 322
phys_addr_t __init_memblock memblock_find_in_range(phys_addr_t start,
					phys_addr_t end, phys_addr_t size,
					phys_addr_t align)
323
{
324
	phys_addr_t ret;
325
	enum memblock_flags flags = choose_memblock_flags();
326 327 328 329 330 331 332 333 334 335 336 337 338

again:
	ret = memblock_find_in_range_node(size, align, start, end,
					    NUMA_NO_NODE, flags);

	if (!ret && (flags & MEMBLOCK_MIRROR)) {
		pr_warn("Could not allocate %pap bytes of mirrored memory\n",
			&size);
		flags &= ~MEMBLOCK_MIRROR;
		goto again;
	}

	return ret;
339 340
}

341
static void __init_memblock memblock_remove_region(struct memblock_type *type, unsigned long r)
Yinghai Lu's avatar
Yinghai Lu committed
342
{
343
	type->total_size -= type->regions[r].size;
344 345
	memmove(&type->regions[r], &type->regions[r + 1],
		(type->cnt - (r + 1)) * sizeof(type->regions[r]));
346
	type->cnt--;
Yinghai Lu's avatar
Yinghai Lu committed
347

348 349
	/* Special case for empty arrays */
	if (type->cnt == 0) {
350
		WARN_ON(type->total_size != 0);
351 352 353
		type->cnt = 1;
		type->regions[0].base = 0;
		type->regions[0].size = 0;
354
		type->regions[0].flags = 0;
355
		memblock_set_region_node(&type->regions[0], MAX_NUMNODES);
356
	}
Yinghai Lu's avatar
Yinghai Lu committed
357 358
}

359
#ifdef CONFIG_ARCH_DISCARD_MEMBLOCK
360
/**
361
 * memblock_discard - discard memory and reserved arrays if they were allocated
362 363
 */
void __init memblock_discard(void)
364
{
365
	phys_addr_t addr, size;
366

367 368 369 370 371 372
	if (memblock.reserved.regions != memblock_reserved_init_regions) {
		addr = __pa(memblock.reserved.regions);
		size = PAGE_ALIGN(sizeof(struct memblock_region) *
				  memblock.reserved.max);
		__memblock_free_late(addr, size);
	}
373

374
	if (memblock.memory.regions != memblock_memory_init_regions) {
375 376 377 378 379
		addr = __pa(memblock.memory.regions);
		size = PAGE_ALIGN(sizeof(struct memblock_region) *
				  memblock.memory.max);
		__memblock_free_late(addr, size);
	}
380 381 382
}
#endif

383 384 385 386 387 388 389 390
/**
 * memblock_double_array - double the size of the memblock regions array
 * @type: memblock type of the regions array being doubled
 * @new_area_start: starting address of memory range to avoid overlap with
 * @new_area_size: size of memory range to avoid overlap with
 *
 * Double the size of the @type regions array. If memblock is being used to
 * allocate memory for a new reserved regions array and there is a previously
391
 * allocated memory range [@new_area_start, @new_area_start + @new_area_size]
392 393 394
 * waiting to be reserved, ensure the memory used by the new array does
 * not overlap.
 *
395
 * Return:
396 397 398 399 400
 * 0 on success, -1 on failure.
 */
static int __init_memblock memblock_double_array(struct memblock_type *type,
						phys_addr_t new_area_start,
						phys_addr_t new_area_size)
401 402
{
	struct memblock_region *new_array, *old_array;
403
	phys_addr_t old_alloc_size, new_alloc_size;
404
	phys_addr_t old_size, new_size, addr, new_end;
405
	int use_slab = slab_is_available();
406
	int *in_slab;
407 408 409 410 411 412 413 414 415 416

	/* We don't allow resizing until we know about the reserved regions
	 * of memory that aren't suitable for allocation
	 */
	if (!memblock_can_resize)
		return -1;

	/* Calculate new doubled size */
	old_size = type->max * sizeof(struct memblock_region);
	new_size = old_size << 1;
417 418 419 420 421 422
	/*
	 * We need to allocated new one align to PAGE_SIZE,
	 *   so we can free them completely later.
	 */
	old_alloc_size = PAGE_ALIGN(old_size);
	new_alloc_size = PAGE_ALIGN(new_size);
423

424 425 426 427 428 429
	/* Retrieve the slab flag */
	if (type == &memblock.memory)
		in_slab = &memblock_memory_in_slab;
	else
		in_slab = &memblock_reserved_in_slab;

430 431 432
	/* Try to find some space for it.
	 *
	 * WARNING: We assume that either slab_is_available() and we use it or
433 434 435
	 * we use MEMBLOCK for allocations. That means that this is unsafe to
	 * use when bootmem is currently active (unless bootmem itself is
	 * implemented on top of MEMBLOCK which isn't the case yet)
436 437
	 *
	 * This should however not be an issue for now, as we currently only
438 439
	 * call into MEMBLOCK while it's still active, or much later when slab
	 * is active for memory hotplug operations
440 441 442
	 */
	if (use_slab) {
		new_array = kmalloc(new_size, GFP_KERNEL);
443
		addr = new_array ? __pa(new_array) : 0;
444
	} else {
445 446 447 448 449 450
		/* only exclude range when trying to double reserved.regions */
		if (type != &memblock.reserved)
			new_area_start = new_area_size = 0;

		addr = memblock_find_in_range(new_area_start + new_area_size,
						memblock.current_limit,
451
						new_alloc_size, PAGE_SIZE);
452 453
		if (!addr && new_area_size)
			addr = memblock_find_in_range(0,
454 455
				min(new_area_start, memblock.current_limit),
				new_alloc_size, PAGE_SIZE);
456

457
		new_array = addr ? __va(addr) : NULL;
458
	}
459
	if (!addr) {
460
		pr_err("memblock: Failed to double %s array from %ld to %ld entries !\n",
461
		       type->name, type->max, type->max * 2);
462 463 464
		return -1;
	}

465 466 467
	new_end = addr + new_size - 1;
	memblock_dbg("memblock: %s is doubled to %ld at [%pa-%pa]",
			type->name, type->max * 2, &addr, &new_end);
468

469 470 471 472
	/*
	 * Found space, we now need to move the array over before we add the
	 * reserved region since it may be our reserved array itself that is
	 * full.
473 474 475 476 477 478 479
	 */
	memcpy(new_array, type->regions, old_size);
	memset(new_array + type->max, 0, old_size);
	old_array = type->regions;
	type->regions = new_array;
	type->max <<= 1;

480
	/* Free old array. We needn't free it if the array is the static one */
481 482 483 484
	if (*in_slab)
		kfree(old_array);
	else if (old_array != memblock_memory_init_regions &&
		 old_array != memblock_reserved_init_regions)
485
		memblock_free(__pa(old_array), old_alloc_size);
486

487 488 489
	/*
	 * Reserve the new array if that comes from the memblock.  Otherwise, we
	 * needn't do it
490 491
	 */
	if (!use_slab)
492
		BUG_ON(memblock_reserve(addr, new_alloc_size));
493 494 495 496

	/* Update slab flag */
	*in_slab = use_slab;

497 498 499
	return 0;
}

500 501 502 503 504 505 506
/**
 * memblock_merge_regions - merge neighboring compatible regions
 * @type: memblock type to scan
 *
 * Scan @type and merge neighboring compatible regions.
 */
static void __init_memblock memblock_merge_regions(struct memblock_type *type)
Yinghai Lu's avatar
Yinghai Lu committed
507
{
508
	int i = 0;
Yinghai Lu's avatar
Yinghai Lu committed
509

510 511 512 513
	/* cnt never goes below 1 */
	while (i < type->cnt - 1) {
		struct memblock_region *this = &type->regions[i];
		struct memblock_region *next = &type->regions[i + 1];
Yinghai Lu's avatar
Yinghai Lu committed
514

515 516
		if (this->base + this->size != next->base ||
		    memblock_get_region_node(this) !=
517 518
		    memblock_get_region_node(next) ||
		    this->flags != next->flags) {
519 520 521
			BUG_ON(this->base + this->size > next->base);
			i++;
			continue;
522 523
		}

524
		this->size += next->size;
525 526
		/* move forward from next + 1, index of which is i + 2 */
		memmove(next, next + 1, (type->cnt - (i + 2)) * sizeof(*next));
527
		type->cnt--;
Yinghai Lu's avatar
Yinghai Lu committed
528
	}
529
}
Yinghai Lu's avatar
Yinghai Lu committed
530

531 532
/**
 * memblock_insert_region - insert new memblock region
533 534 535 536 537
 * @type:	memblock type to insert into
 * @idx:	index for the insertion point
 * @base:	base address of the new region
 * @size:	size of the new region
 * @nid:	node id of the new region
538
 * @flags:	flags of the new region
539
 *
540
 * Insert new memblock region [@base, @base + @size) into @type at @idx.
541
 * @type must already have extra room to accommodate the new region.
542 543 544
 */
static void __init_memblock memblock_insert_region(struct memblock_type *type,
						   int idx, phys_addr_t base,
545
						   phys_addr_t size,
546 547
						   int nid,
						   enum memblock_flags flags)
548 549 550 551 552 553 554
{
	struct memblock_region *rgn = &type->regions[idx];

	BUG_ON(type->cnt >= type->max);
	memmove(rgn + 1, rgn, (type->cnt - idx) * sizeof(*rgn));
	rgn->base = base;
	rgn->size = size;
555
	rgn->flags = flags;
556
	memblock_set_region_node(rgn, nid);
557
	type->cnt++;
558
	type->total_size += size;
559 560 561
}

/**
562
 * memblock_add_range - add new memblock region
563 564 565
 * @type: memblock type to add new region into
 * @base: base address of the new region
 * @size: size of the new region
566
 * @nid: nid of the new region
567
 * @flags: flags of the new region
568
 *
569
 * Add new memblock region [@base, @base + @size) into @type.  The new region
570 571 572 573
 * is allowed to overlap with existing ones - overlaps don't affect already
 * existing regions.  @type is guaranteed to be minimal (all neighbouring
 * compatible regions are merged) after the addition.
 *
574
 * Return:
575 576
 * 0 on success, -errno on failure.
 */
577
int __init_memblock memblock_add_range(struct memblock_type *type,
578
				phys_addr_t base, phys_addr_t size,
579
				int nid, enum memblock_flags flags)
580 581
{
	bool insert = false;
582 583
	phys_addr_t obase = base;
	phys_addr_t end = base + memblock_cap_size(base, &size);
584 585
	int idx, nr_new;
	struct memblock_region *rgn;
586

587 588 589
	if (!size)
		return 0;

590 591
	/* special case for empty array */
	if (type->regions[0].size == 0) {
592
		WARN_ON(type->cnt != 1 || type->total_size);
593 594
		type->regions[0].base = base;
		type->regions[0].size = size;
595
		type->regions[0].flags = flags;
596
		memblock_set_region_node(&type->regions[0], nid);
597
		type->total_size = size;
598
		return 0;
Yinghai Lu's avatar
Yinghai Lu committed
599
	}
600 601 602 603
repeat:
	/*
	 * The following is executed twice.  Once with %false @insert and
	 * then with %true.  The first counts the number of regions needed
604
	 * to accommodate the new area.  The second actually inserts them.
605
	 */
606 607
	base = obase;
	nr_new = 0;
Yinghai Lu's avatar
Yinghai Lu committed
608

609
	for_each_memblock_type(idx, type, rgn) {
610 611 612 613
		phys_addr_t rbase = rgn->base;
		phys_addr_t rend = rbase + rgn->size;

		if (rbase >= end)
Yinghai Lu's avatar
Yinghai Lu committed
614
			break;
615 616 617 618 619 620 621
		if (rend <= base)
			continue;
		/*
		 * @rgn overlaps.  If it separates the lower part of new
		 * area, insert that portion.
		 */
		if (rbase > base) {
622 623 624
#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
			WARN_ON(nid != memblock_get_region_node(rgn));
#endif
625
			WARN_ON(flags != rgn->flags);
626 627
			nr_new++;
			if (insert)
628
				memblock_insert_region(type, idx++, base,
629 630
						       rbase - base, nid,
						       flags);
Yinghai Lu's avatar
Yinghai Lu committed
631
		}
632 633
		/* area below @rend is dealt with, forget about it */
		base = min(rend, end);
Yinghai Lu's avatar
Yinghai Lu committed
634
	}
635 636 637 638 639

	/* insert the remaining portion */
	if (base < end) {
		nr_new++;
		if (insert)
640
			memblock_insert_region(type, idx, base, end - base,
641
					       nid, flags);
Yinghai Lu's avatar
Yinghai Lu committed
642 643
	}

644 645 646
	if (!nr_new)
		return 0;

647 648 649
	/*
	 * If this was the first round, resize array and repeat for actual
	 * insertions; otherwise, merge and return.
650
	 */
651 652
	if (!insert) {
		while (type->cnt + nr_new > type->max)
653
			if (memblock_double_array(type, obase, size) < 0)
654 655 656 657 658 659
				return -ENOMEM;
		insert = true;
		goto repeat;
	} else {
		memblock_merge_regions(type);
		return 0;
660
	}
Yinghai Lu's avatar
Yinghai Lu committed
661 662
}

663 664 665 666 667 668 669 670 671 672 673 674
/**
 * memblock_add_node - add new memblock region within a NUMA node
 * @base: base address of the new region
 * @size: size of the new region
 * @nid: nid of the new region
 *
 * Add new memblock region [@base, @base + @size) to the "memory"
 * type. See memblock_add_range() description for mode details
 *
 * Return:
 * 0 on success, -errno on failure.
 */
675 676 677
int __init_memblock memblock_add_node(phys_addr_t base, phys_addr_t size,
				       int nid)
{
678
	return memblock_add_range(&memblock.memory, base, size, nid, 0);
679 680
}

681 682 683 684 685 686 687 688 689 690 691
/**
 * memblock_add - add new memblock region
 * @base: base address of the new region
 * @size: size of the new region
 *
 * Add new memblock region [@base, @base + @size) to the "memory"
 * type. See memblock_add_range() description for mode details
 *
 * Return:
 * 0 on success, -errno on failure.
 */
692
int __init_memblock memblock_add(phys_addr_t base, phys_addr_t size)
693
{
694 695 696 697
	phys_addr_t end = base + size - 1;

	memblock_dbg("memblock_add: [%pa-%pa] %pF\n",
		     &base, &end, (void *)_RET_IP_);
698

699
	return memblock_add_range(&memblock.memory, base, size, MAX_NUMNODES, 0);
Yinghai Lu's avatar
Yinghai Lu committed
700 701
}

702 703 704 705 706 707 708 709 710
/**
 * memblock_isolate_range - isolate given range into disjoint memblocks
 * @type: memblock type to isolate range for
 * @base: base of range to isolate
 * @size: size of range to isolate
 * @start_rgn: out parameter for the start of isolated region
 * @end_rgn: out parameter for the end of isolated region
 *
 * Walk @type and ensure that regions don't cross the boundaries defined by
711
 * [@base, @base + @size).  Crossing regions are split at the boundaries,
712 713 714
 * which may create at most two more regions.  The index of the first
 * region inside the range is returned in *@start_rgn and end in *@end_rgn.
 *
715
 * Return:
716 717 718 719 720 721
 * 0 on success, -errno on failure.
 */
static int __init_memblock memblock_isolate_range(struct memblock_type *type,
					phys_addr_t base, phys_addr_t size,
					int *start_rgn, int *end_rgn)
{
722
	phys_addr_t end = base + memblock_cap_size(base, &size);
723 724
	int idx;
	struct memblock_region *rgn;
725 726 727

	*start_rgn = *end_rgn = 0;

728 729 730
	if (!size)
		return 0;

731 732
	/* we'll create at most two more regions */
	while (type->cnt + 2 > type->max)
733
		if (memblock_double_array(type, base, size) < 0)
734 735
			return -ENOMEM;

736
	for_each_memblock_type(idx, type, rgn) {
737 738 739 740 741 742 743 744 745 746 747 748 749 750
		phys_addr_t rbase = rgn->base;
		phys_addr_t rend = rbase + rgn->size;

		if (rbase >= end)
			break;
		if (rend <= base)
			continue;

		if (rbase < base) {
			/*
			 * @rgn intersects from below.  Split and continue
			 * to process the next region - the new top half.
			 */
			rgn->base = base;
751 752
			rgn->size -= base - rbase;
			type->total_size -= base - rbase;
753
			memblock_insert_region(type, idx, rbase, base - rbase,
754 755
					       memblock_get_region_node(rgn),
					       rgn->flags);
756 757 758 759 760 761
		} else if (rend > end) {
			/*
			 * @rgn intersects from above.  Split and redo the
			 * current region - the new bottom half.
			 */
			rgn->base = end;
762 763
			rgn->size -= end - rbase;
			type->total_size -= end - rbase;
764
			memblock_insert_region(type, idx--, rbase, end - rbase,
765 766
					       memblock_get_region_node(rgn),
					       rgn->flags);
767 768 769
		} else {
			/* @rgn is fully contained, record it */
			if (!*end_rgn)
770 771
				*start_rgn = idx;
			*end_rgn = idx + 1;
772 773 774 775 776 777
		}
	}

	return 0;
}

778
static int __init_memblock memblock_remove_range(struct memblock_type *type,
779
					  phys_addr_t base, phys_addr_t size)
Yinghai Lu's avatar
Yinghai Lu committed
780
{
781 782
	int start_rgn, end_rgn;
	int i, ret;
Yinghai Lu's avatar
Yinghai Lu committed
783

784 785 786
	ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
	if (ret)
		return ret;
Yinghai Lu's avatar
Yinghai Lu committed
787

788 789
	for (i = end_rgn - 1; i >= start_rgn; i--)
		memblock_remove_region(type, i);
790
	return 0;
Yinghai Lu's avatar
Yinghai Lu committed
791 792
}

793
int __init_memblock memblock_remove(phys_addr_t base, phys_addr_t size)
Yinghai Lu's avatar
Yinghai Lu committed
794
{
795 796 797 798 799
	phys_addr_t end = base + size - 1;

	memblock_dbg("memblock_remove: [%pa-%pa] %pS\n",
		     &base, &end, (void *)_RET_IP_);

800
	return memblock_remove_range(&memblock.memory, base, size);
Yinghai Lu's avatar
Yinghai Lu committed
801 802
}

803

804
int __init_memblock memblock_free(phys_addr_t base, phys_addr_t size)
Yinghai Lu's avatar
Yinghai Lu committed
805
{
806 807 808 809
	phys_addr_t end = base + size - 1;

	memblock_dbg("   memblock_free: [%pa-%pa] %pF\n",
		     &base, &end, (void *)_RET_IP_);
810

811
	kmemleak_free_part_phys(base, size);
812
	return memblock_remove_range(&memblock.reserved, base, size);
Yinghai Lu's avatar
Yinghai Lu committed
813 814
}

815
int __init_memblock memblock_reserve(phys_addr_t base, phys_addr_t size)
Yinghai Lu's avatar
Yinghai Lu committed
816
{
817 818 819 820
	phys_addr_t end = base + size - 1;

	memblock_dbg("memblock_reserve: [%pa-%pa] %pF\n",
		     &base, &end, (void *)_RET_IP_);
Yinghai Lu's avatar
Yinghai Lu committed
821

822
	return memblock_add_range(&memblock.reserved, base, size, MAX_NUMNODES, 0);
Yinghai Lu's avatar
Yinghai Lu committed
823 824
}

825
/**
826 827 828 829 830
 * memblock_setclr_flag - set or clear flag for a memory region
 * @base: base address of the region
 * @size: size of the region
 * @set: set or clear the flag
 * @flag: the flag to udpate
831
 *
832
 * This function isolates region [@base, @base + @size), and sets/clears flag
833
 *
834
 * Return: 0 on success, -errno on failure.
835
 */
836 837
static int __init_memblock memblock_setclr_flag(phys_addr_t base,
				phys_addr_t size, int set, int flag)
838 839 840 841 842 843 844 845 846
{
	struct memblock_type *type = &memblock.memory;
	int i, ret, start_rgn, end_rgn;

	ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
	if (ret)
		return ret;

	for (i = start_rgn; i < end_rgn; i++)
847 848 849 850
		if (set)
			memblock_set_region_flags(&type->regions[i], flag);
		else
			memblock_clear_region_flags(&type->regions[i], flag);
851 852 853 854 855 856

	memblock_merge_regions(type);
	return 0;
}

/**
857
 * memblock_mark_hotplug - Mark hotpluggable memory with flag MEMBLOCK_HOTPLUG.
858 859 860
 * @base: the base phys addr of the region
 * @size: the size of the region
 *
861
 * Return: 0 on success, -errno on failure.
862 863 864 865 866 867 868 869 870 871
 */
int __init_memblock memblock_mark_hotplug(phys_addr_t base, phys_addr_t size)
{
	return memblock_setclr_flag(base, size, 1, MEMBLOCK_HOTPLUG);
}

/**
 * memblock_clear_hotplug - Clear flag MEMBLOCK_HOTPLUG for a specified region.
 * @base: the base phys addr of the region
 * @size: the size of the region
872
 *
873
 * Return: 0 on success, -errno on failure.
874 875 876
 */
int __init_memblock memblock_clear_hotplug(phys_addr_t base, phys_addr_t size)
{
877
	return memblock_setclr_flag(base, size, 0, MEMBLOCK_HOTPLUG);
878 879
}

880 881 882 883 884
/**
 * memblock_mark_mirror - Mark mirrored memory with flag MEMBLOCK_MIRROR.
 * @base: the base phys addr of the region
 * @size: the size of the region
 *
885
 * Return: 0 on success, -errno on failure.
886 887 888 889 890 891 892 893
 */
int __init_memblock memblock_mark_mirror(phys_addr_t base, phys_addr_t size)
{
	system_has_some_mirror = true;

	return memblock_setclr_flag(base, size, 1, MEMBLOCK_MIRROR);
}

894 895 896 897 898
/**
 * memblock_mark_nomap - Mark a memory region with flag MEMBLOCK_NOMAP.
 * @base: the base phys addr of the region
 * @size: the size of the region
 *
899
 * Return: 0 on success, -errno on failure.
900 901 902 903 904
 */
int __init_memblock memblock_mark_nomap(phys_addr_t base, phys_addr_t size)
{
	return memblock_setclr_flag(base, size, 1, MEMBLOCK_NOMAP);
}
905

906 907 908 909 910
/**
 * memblock_clear_nomap - Clear flag MEMBLOCK_NOMAP for a specified region.
 * @base: the base phys addr of the region
 * @size: the size of the region
 *
911
 * Return: 0 on success, -errno on failure.
912 913 914 915 916 917
 */
int __init_memblock memblock_clear_nomap(phys_addr_t base, phys_addr_t size)
{
	return memblock_setclr_flag(base, size, 0, MEMBLOCK_NOMAP);
}

918 919 920 921 922 923 924 925 926 927 928 929
/**
 * __next_reserved_mem_region - next function for for_each_reserved_region()
 * @idx: pointer to u64 loop variable
 * @out_start: ptr to phys_addr_t for start address of the region, can be %NULL
 * @out_end: ptr to phys_addr_t for end address of the region, can be %NULL
 *
 * Iterate over all reserved memory regions.
 */
void __init_memblock __next_reserved_mem_region(u64 *idx,
					   phys_addr_t *out_start,
					   phys_addr_t *out_end)
{
930
	struct memblock_type *type = &memblock.reserved;
931

932
	if (*idx < type->cnt) {
933
		struct memblock_region *r = &type->regions[*idx];
934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949
		phys_addr_t base = r->base;
		phys_addr_t size = r->size;

		if (out_start)
			*out_start = base;
		if (out_end)
			*out_end = base + size - 1;

		*idx += 1;
		return;
	}

	/* signal end of iteration */
	*idx = ULLONG_MAX;
}

950
/**
951
 * __next__mem_range - next function for for_each_free_mem_range() etc.
952
 * @idx: pointer to u64 loop variable
953
 * @nid: node selector, %NUMA_NO_NODE for all nodes
954
 * @flags: pick from blocks based on memory attributes
955 956
 * @type_a: pointer to memblock_type from where the range is taken
 * @type_b: pointer to memblock_type which excludes memory from being taken
Wanpeng Li's avatar
Wanpeng Li committed
957 958 959
 * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
 * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
 * @out_nid: ptr to int for nid of the range, can be %NULL
960
 *
961
 * Find the first area from *@idx which matches @nid, fill the out
962
 * parameters, and update *@idx for the next iteration.  The lower 32bit of
963 964
 * *@idx contains index into type_a and the upper 32bit indexes the
 * areas before each region in type_b.	For example, if type_b regions
965 966 967 968 969 970 971 972 973 974 975
 * look like the following,
 *
 *	0:[0-16), 1:[32-48), 2:[128-130)
 *
 * The upper 32bit indexes the following regions.
 *
 *	0:[0-0), 1:[16-32), 2:[48-128), 3:[130-MAX)
 *
 * As both region arrays are sorted, the function advances the two indices
 * in lockstep and returns each intersection.
 */
976 977
void __init_memblock __next_mem_range(u64 *idx, int nid,
				      enum memblock_flags flags,
978 979 980 981
				      struct memblock_type *type_a,
				      struct memblock_type *type_b,
				      phys_addr_t *out_start,
				      phys_addr_t *out_end, int *out_nid)
982
{
983 984
	int idx_a = *idx & 0xffffffff;
	int idx_b = *idx >> 32;
985

986 987
	if (WARN_ONCE(nid == MAX_NUMNODES,
	"Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
988
		nid = NUMA_NO_NODE;
989

990 991 992
	for (; idx_a < type_a->cnt; idx_a++) {
		struct memblock_region *m = &type_a->regions[idx_a];

993 994
		phys_addr_t m_start = m->base;
		phys_addr_t m_end = m->base + m->size;
995
		int	    m_nid = memblock_get_region_node(m);
996 997

		/* only memory regions are associated with nodes, check it */
998
		if (nid != NUMA_NO_NODE && nid != m_nid)
999 1000
			continue;

1001 1002 1003 1004
		/* skip hotpluggable memory regions if needed */
		if (movable_node_is_enabled() && memblock_is_hotpluggable(m))
			continue;

1005 1006 1007 1008
		/* if we want mirror memory skip non-mirror memory regions */
		if ((flags & MEMBLOCK_MIRROR) && !memblock_is_mirror(m))
			continue;

1009 1010 1011 1012
		/* skip nomap memory unless we were asked for it explicitly */
		if (!(flags & MEMBLOCK_NOMAP) && memblock_is_nomap(m))
			continue;

1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
		if (!type_b) {
			if (out_start)
				*out_start = m_start;
			if (out_end)
				*out_end = m_end;
			if (out_nid)
				*out_nid = m_nid;
			idx_a++;
			*idx = (u32)idx_a | (u64)idx_b << 32;
			return;
		}

		/* scan areas before each reservation */
		for (; idx_b < type_b->cnt + 1; idx_b++) {
			struct memblock_region *r;
			phys_addr_t r_start;
			phys_addr_t r_end;

			r = &type_b->regions[idx_b];
			r_start = idx_b ? r[-1].base + r[-1].size : 0;
			r_end = idx_b < type_b->cnt ?
1034
				r->base : PHYS_ADDR_MAX;
1035

1036 1037 1038 1039
			/*
			 * if idx_b advanced past idx_a,
			 * break out to advance idx_a
			 */
1040 1041 1042 1043 1044
			if (r_start >= m_end)
				break;
			/* if the two regions intersect, we're done */
			if (m_start < r_end) {
				if (out_start)
1045 1046
					*out_start =
						max(m_start, r_start);
1047 1048 1049
				if (out_end)
					*out_end = min(m_end, r_end);
				if (out_nid)
1050
					*out_nid = m_nid;
1051
				/*
1052 1053
				 * The region which ends first is
				 * advanced for the next iteration.
1054 1055
				 */
				if (m_end <= r_end)
1056
					idx_a++;
1057
				else
1058 1059
					idx_b++;
				*idx = (u32)idx_a | (u64)idx_b << 32;
1060 1061 1062 1063 1064 1065 1066 1067 1068
				return;
			}
		}
	}

	/* signal end of iteration */
	*idx = ULLONG_MAX;
}

1069
/**
1070 1071
 * __next_mem_range_rev - generic next function for for_each_*_range_rev()
 *
1072
 * @idx: pointer to u64 loop variable
1073
 * @nid: node selector, %NUMA_NO_NODE for all nodes
1074
 * @flags: pick from blocks based on memory attributes
1075 1076
 * @type_a: pointer to memblock_type from where the range is taken
 * @type_b: pointer to memblock_type which excludes memory from being taken
Wanpeng Li's avatar
Wanpeng Li committed
1077 1078 1079
 * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
 * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
 * @out_nid: ptr to int for nid of the range, can be %NULL
1080
 *
1081 1082 1083
 * Finds the next range from type_a which is not marked as unsuitable
 * in type_b.
 *
1084
 * Reverse of __next_mem_range().
1085
 */
1086 1087
void __init_memblock __next_mem_range_rev(u64 *idx, int nid,
					  enum memblock_flags flags,
1088 1089 1090 1091
					  struct memblock_type *type_a,
					  struct memblock_type *type_b,
					  phys_addr_t *out_start,
					  phys_addr_t *out_end, int *out_nid)
1092
{
1093 1094
	int idx_a = *idx & 0xffffffff;
	int idx_b = *idx >> 32;
1095

1096 1097
	if (WARN_ONCE(nid == MAX_NUMNODES, "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
		nid = NUMA_NO_NODE;
1098 1099

	if (*idx == (u64)ULLONG_MAX) {
1100
		idx_a = type_a->cnt - 1;
1101 1102 1103 1104
		if (type_b != NULL)
			idx_b = type_b->cnt;
		else
			idx_b = 0;
1105 1106
	}

1107 1108 1109
	for (; idx_a >= 0; idx_a--) {
		struct memblock_region *m = &type_a->regions[idx_a];

1110 1111
		phys_addr_t m_start = m->base;
		phys_addr_t m_end = m->base + m->size;
1112
		int m_nid = memblock_get_region_node(m);
1113 1114

		/* only memory regions are associated with nodes, check it */
1115
		if (nid != NUMA_NO_NODE && nid != m_nid)
1116 1117
			continue;

1118 1119 1120 1121
		/* skip hotpluggable memory regions if needed */
		if (movable_node_is_enabled() && memblock_is_hotpluggable(m))
			continue;

1122 1123 1124 1125
		/* if we want mirror memory skip non-mirror memory regions */
		if ((flags & MEMBLOCK_MIRROR) && !memblock_is_mirror(m))
			continue;

1126 1127 1128 1129
		/* skip nomap memory unless we were asked for it explicitly */
		if (!(flags & MEMBLOCK_NOMAP) && memblock_is_nomap(m))
			continue;

1130 1131 1132 1133 1134 1135 1136
		if (!type_b) {
			if (out_start)
				*out_start = m_start;
			if (out_end)
				*out_end = m_end;
			if (out_nid)
				*out_nid = m_nid;
1137
			idx_a--;
1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150
			*idx = (u32)idx_a | (u64)idx_b << 32;
			return;
		}

		/* scan areas before each reservation */
		for (; idx_b >= 0; idx_b--) {
			struct memblock_region *r;
			phys_addr_t r_start;
			phys_addr_t r_end;

			r = &type_b->regions[idx_b];
			r_start = idx_b ? r[-1].base + r[-1].size : 0;
			r_end = idx_b < type_b->cnt ?
1151
				r->base : PHYS_ADDR_MAX;
1152 1153 1154 1155
			/*
			 * if idx_b advanced past idx_a,
			 * break out to advance idx_a
			 */
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165

			if (r_end <= m_start)
				break;
			/* if the two regions intersect, we're done */
			if (m_end > r_start) {
				if (out_start)
					*out_start = max(m_start, r_start);
				if (out_end)
					*out_end = min(m_end, r_end);
				if (out_nid)
1166
					*out_nid = m_nid;
1167
				if (m_start >= r_start)
1168
					idx_a--;
1169
				else
1170 1171
					idx_b--;
				*idx = (u32)idx_a | (u64)idx_b << 32;
1172 1173 1174 1175
				return;
			}
		}
	}
1176
	/* signal end of iteration */
1177 1178 1179
	*idx = ULLONG_MAX;
}

1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215
#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
/*
 * Common iterator interface used to define for_each_mem_range().
 */
void __init_memblock __next_mem_pfn_range(int *idx, int nid,
				unsigned long *out_start_pfn,
				unsigned long *out_end_pfn, int *out_nid)
{
	struct memblock_type *type = &memblock.memory;
	struct memblock_region *r;

	while (++*idx < type->cnt) {
		r = &type->regions[*idx];

		if (PFN_UP(r->base) >= PFN_DOWN(r->base + r->size))
			continue;
		if (nid == MAX_NUMNODES || nid == r->nid)
			break;
	}
	if (*idx >= type->cnt) {
		*idx = -1;
		return;
	}

	if (out_start_pfn)
		*out_start_pfn = PFN_UP(r->base);
	if (out_end_pfn)
		*out_end_pfn = PFN_DOWN(r->base + r->size);
	if (out_nid)
		*out_nid = r->nid;
}

/**
 * memblock_set_node - set node ID on memblock regions
 * @base: base of area to set node ID for
 * @size: size of area to set node ID for
1216
 * @type: memblock type to set node ID for
1217 1218
 * @nid: node ID to set
 *
1219
 * Set the nid of memblock @type regions in [@base, @base + @size) to @nid.
1220 1221
 * Regions which cross the area boundaries are split as necessary.
 *
1222
 * Return:
1223 1224 1225
 * 0 on success, -errno on failure.
 */
int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size,
1226
				      struct memblock_type *type, int nid)
1227
{
1228 1229
	int start_rgn, end_rgn;
	int i, ret;
1230

1231 1232 1233
	ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
	if (ret)
		return ret;
1234

1235
	for (i = start_rgn; i < end_rgn; i++)
1236
		memblock_set_region_node(&type->regions[i], nid);
1237 1238 1239 1240 1241 1242

	memblock_merge_regions(type);
	return 0;
}
#endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */

1243 1244
static phys_addr_t __init memblock_alloc_range_nid(phys_addr_t size,
					phys_addr_t align, phys_addr_t start,
1245 1246
					phys_addr_t end, int nid,
					enum memblock_flags flags)
Yinghai Lu's avatar
Yinghai Lu committed
1247
{
1248
	phys_addr_t found;
Yinghai Lu's avatar
Yinghai Lu committed
1249

1250 1251 1252 1253 1254 1255
	if (!align) {
		/* Can't use WARNs this early in boot on powerpc */
		dump_stack();
		align = SMP_CACHE_BYTES;
	}

1256 1257
	found = memblock_find_in_range_node(size, align, start, end, nid,
					    flags);
1258 1259 1260 1261 1262
	if (found && !memblock_reserve(found, size)) {
		/*
		 * The min_count is set to 0 so that memblock allocations are
		 * never reported as leaks.
		 */
1263
		kmemleak_alloc_phys(found, size, 0, 0);
1264
		return found;
1265
	}
1266
	return 0;
Yinghai Lu's avatar
Yinghai Lu committed
1267 1268
}

1269
phys_addr_t __init memblock_alloc_range(phys_addr_t size, phys_addr_t align,
1270
					phys_addr_t start, phys_addr_t end,
1271
					enum memblock_flags flags)
1272
{
1273 1274
	return memblock_alloc_range_nid(size, align, start, end, NUMA_NO_NODE,
					flags);
1275 1276
}

1277
phys_addr_t __init memblock_alloc_base_nid(phys_addr_t size,
1278
					phys_addr_t align, phys_addr_t max_addr,
1279
					int nid, enum memblock_flags flags)
1280
{
1281
	return memblock_alloc_range_nid(size, align, 0, max_addr, nid, flags);
1282 1283
}

1284
phys_addr_t __init memblock_phys_alloc_nid(phys_addr_t size, phys_addr_t align, int nid)
1285
{
1286
	enum memblock_flags flags = choose_memblock_flags();
1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297
	phys_addr_t ret;

again:
	ret = memblock_alloc_base_nid(size, align, MEMBLOCK_ALLOC_ACCESSIBLE,
				      nid, flags);

	if (!ret && (flags & MEMBLOCK_MIRROR)) {
		flags &= ~MEMBLOCK_MIRROR;
		goto again;
	}
	return ret;
1298 1299 1300 1301
}

phys_addr_t __init __memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
{
1302 1303
	return memblock_alloc_base_nid(size, align, max_addr, NUMA_NO_NODE,
				       MEMBLOCK_NONE);
1304 1305
}

1306
phys_addr_t __init memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
Yinghai Lu's avatar
Yinghai Lu committed
1307
{
1308 1309 1310 1311 1312
	phys_addr_t alloc;

	alloc = __memblock_alloc_base(size, align, max_addr);

	if (alloc == 0)
1313 1314
		panic("ERROR: Failed to allocate %pa bytes below %pa.\n",
		      &size, &max_addr);
1315 1316

	return alloc;
Yinghai Lu's avatar
Yinghai Lu committed
1317 1318
}

1319
phys_addr_t __init memblock_phys_alloc(phys_addr_t size, phys_addr_t align)
Yinghai Lu's avatar
Yinghai Lu committed
1320
{
1321 1322
	return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
}
Yinghai Lu's avatar
Yinghai Lu committed
1323

1324
phys_addr_t __init memblock_phys_alloc_try_nid(phys_add