Skip to content

Commit a7259df

Browse files
rppttorvalds
authored andcommitted
memblock: make memblock_find_in_range method private
There are a lot of uses of memblock_find_in_range() along with memblock_reserve() from the times memblock allocation APIs did not exist. memblock_find_in_range() is the very core of memblock allocations, so any future changes to its internal behaviour would mandate updates of all the users outside memblock. Replace the calls to memblock_find_in_range() with an equivalent calls to memblock_phys_alloc() and memblock_phys_alloc_range() and make memblock_find_in_range() private method of memblock. This simplifies the callers, ensures that (unlikely) errors in memblock_reserve() are handled and improves maintainability of memblock_find_in_range(). Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Mike Rapoport <[email protected]> Reviewed-by: Catalin Marinas <[email protected]> [arm64] Acked-by: Kirill A. Shutemov <[email protected]> Acked-by: Rafael J. Wysocki <[email protected]> [ACPI] Acked-by: Russell King (Oracle) <[email protected]> Acked-by: Nick Kossifidis <[email protected]> [riscv] Tested-by: Guenter Roeck <[email protected]> Acked-by: Rob Herring <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 38b031d commit a7259df

File tree

16 files changed

+81
-117
lines changed

16 files changed

+81
-117
lines changed

arch/arm/kernel/setup.c

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,31 +1012,25 @@ static void __init reserve_crashkernel(void)
10121012
unsigned long long lowmem_max = __pa(high_memory - 1) + 1;
10131013
if (crash_max > lowmem_max)
10141014
crash_max = lowmem_max;
1015-
crash_base = memblock_find_in_range(CRASH_ALIGN, crash_max,
1016-
crash_size, CRASH_ALIGN);
1015+
1016+
crash_base = memblock_phys_alloc_range(crash_size, CRASH_ALIGN,
1017+
CRASH_ALIGN, crash_max);
10171018
if (!crash_base) {
10181019
pr_err("crashkernel reservation failed - No suitable area found.\n");
10191020
return;
10201021
}
10211022
} else {
1023+
unsigned long long crash_max = crash_base + crash_size;
10221024
unsigned long long start;
10231025

1024-
start = memblock_find_in_range(crash_base,
1025-
crash_base + crash_size,
1026-
crash_size, SECTION_SIZE);
1027-
if (start != crash_base) {
1026+
start = memblock_phys_alloc_range(crash_size, SECTION_SIZE,
1027+
crash_base, crash_max);
1028+
if (!start) {
10281029
pr_err("crashkernel reservation failed - memory is in use.\n");
10291030
return;
10301031
}
10311032
}
10321033

1033-
ret = memblock_reserve(crash_base, crash_size);
1034-
if (ret < 0) {
1035-
pr_warn("crashkernel reservation failed - memory is in use (0x%lx)\n",
1036-
(unsigned long)crash_base);
1037-
return;
1038-
}
1039-
10401034
pr_info("Reserving %ldMB of memory at %ldMB for crashkernel (System RAM: %ldMB)\n",
10411035
(unsigned long)(crash_size >> 20),
10421036
(unsigned long)(crash_base >> 20),

arch/arm64/kvm/hyp/reserved_mem.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,20 +92,17 @@ void __init kvm_hyp_reserve(void)
9292
* this is unmapped from the host stage-2, and fallback to PAGE_SIZE.
9393
*/
9494
hyp_mem_size = hyp_mem_pages << PAGE_SHIFT;
95-
hyp_mem_base = memblock_find_in_range(0, memblock_end_of_DRAM(),
96-
ALIGN(hyp_mem_size, PMD_SIZE),
97-
PMD_SIZE);
95+
hyp_mem_base = memblock_phys_alloc(ALIGN(hyp_mem_size, PMD_SIZE),
96+
PMD_SIZE);
9897
if (!hyp_mem_base)
99-
hyp_mem_base = memblock_find_in_range(0, memblock_end_of_DRAM(),
100-
hyp_mem_size, PAGE_SIZE);
98+
hyp_mem_base = memblock_phys_alloc(hyp_mem_size, PAGE_SIZE);
10199
else
102100
hyp_mem_size = ALIGN(hyp_mem_size, PMD_SIZE);
103101

104102
if (!hyp_mem_base) {
105103
kvm_err("Failed to reserve hyp memory\n");
106104
return;
107105
}
108-
memblock_reserve(hyp_mem_base, hyp_mem_size);
109106

110107
kvm_info("Reserved %lld MiB at 0x%llx\n", hyp_mem_size >> 20,
111108
hyp_mem_base);

arch/arm64/mm/init.c

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ phys_addr_t arm64_dma_phys_limit __ro_after_init;
7474
static void __init reserve_crashkernel(void)
7575
{
7676
unsigned long long crash_base, crash_size;
77+
unsigned long long crash_max = arm64_dma_phys_limit;
7778
int ret;
7879

7980
ret = parse_crashkernel(boot_command_line, memblock_phys_mem_size(),
@@ -84,33 +85,18 @@ static void __init reserve_crashkernel(void)
8485

8586
crash_size = PAGE_ALIGN(crash_size);
8687

87-
if (crash_base == 0) {
88-
/* Current arm64 boot protocol requires 2MB alignment */
89-
crash_base = memblock_find_in_range(0, arm64_dma_phys_limit,
90-
crash_size, SZ_2M);
91-
if (crash_base == 0) {
92-
pr_warn("cannot allocate crashkernel (size:0x%llx)\n",
93-
crash_size);
94-
return;
95-
}
96-
} else {
97-
/* User specifies base address explicitly. */
98-
if (!memblock_is_region_memory(crash_base, crash_size)) {
99-
pr_warn("cannot reserve crashkernel: region is not memory\n");
100-
return;
101-
}
88+
/* User specifies base address explicitly. */
89+
if (crash_base)
90+
crash_max = crash_base + crash_size;
10291

103-
if (memblock_is_region_reserved(crash_base, crash_size)) {
104-
pr_warn("cannot reserve crashkernel: region overlaps reserved memory\n");
105-
return;
106-
}
107-
108-
if (!IS_ALIGNED(crash_base, SZ_2M)) {
109-
pr_warn("cannot reserve crashkernel: base address is not 2MB aligned\n");
110-
return;
111-
}
92+
/* Current arm64 boot protocol requires 2MB alignment */
93+
crash_base = memblock_phys_alloc_range(crash_size, SZ_2M,
94+
crash_base, crash_max);
95+
if (!crash_base) {
96+
pr_warn("cannot allocate crashkernel (size:0x%llx)\n",
97+
crash_size);
98+
return;
11299
}
113-
memblock_reserve(crash_base, crash_size);
114100

115101
pr_info("crashkernel reserved: 0x%016llx - 0x%016llx (%lld MB)\n",
116102
crash_base, crash_base + crash_size, crash_size >> 20);

arch/mips/kernel/setup.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -452,17 +452,19 @@ static void __init mips_parse_crashkernel(void)
452452
return;
453453

454454
if (crash_base <= 0) {
455-
crash_base = memblock_find_in_range(CRASH_ALIGN, CRASH_ADDR_MAX,
456-
crash_size, CRASH_ALIGN);
455+
crash_base = memblock_phys_alloc_range(crash_size, CRASH_ALIGN,
456+
CRASH_ALIGN,
457+
CRASH_ADDR_MAX);
457458
if (!crash_base) {
458459
pr_warn("crashkernel reservation failed - No suitable area found.\n");
459460
return;
460461
}
461462
} else {
462463
unsigned long long start;
463464

464-
start = memblock_find_in_range(crash_base, crash_base + crash_size,
465-
crash_size, 1);
465+
start = memblock_phys_alloc_range(crash_size, 1,
466+
crash_base,
467+
crash_base + crash_size);
466468
if (start != crash_base) {
467469
pr_warn("Invalid memory region reserved for crash kernel\n");
468470
return;
@@ -656,10 +658,6 @@ static void __init arch_mem_init(char **cmdline_p)
656658
mips_reserve_vmcore();
657659

658660
mips_parse_crashkernel();
659-
#ifdef CONFIG_KEXEC
660-
if (crashk_res.start != crashk_res.end)
661-
memblock_reserve(crashk_res.start, resource_size(&crashk_res));
662-
#endif
663661
device_tree_init();
664662

665663
/*

arch/riscv/mm/init.c

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -819,38 +819,22 @@ static void __init reserve_crashkernel(void)
819819

820820
crash_size = PAGE_ALIGN(crash_size);
821821

822-
if (crash_base == 0) {
823-
/*
824-
* Current riscv boot protocol requires 2MB alignment for
825-
* RV64 and 4MB alignment for RV32 (hugepage size)
826-
*/
827-
crash_base = memblock_find_in_range(search_start, search_end,
828-
crash_size, PMD_SIZE);
829-
830-
if (crash_base == 0) {
831-
pr_warn("crashkernel: couldn't allocate %lldKB\n",
832-
crash_size >> 10);
833-
return;
834-
}
835-
} else {
836-
/* User specifies base address explicitly. */
837-
if (!memblock_is_region_memory(crash_base, crash_size)) {
838-
pr_warn("crashkernel: requested region is not memory\n");
839-
return;
840-
}
841-
842-
if (memblock_is_region_reserved(crash_base, crash_size)) {
843-
pr_warn("crashkernel: requested region is reserved\n");
844-
return;
845-
}
846-
822+
if (crash_base) {
823+
search_start = crash_base;
824+
search_end = crash_base + crash_size;
825+
}
847826

848-
if (!IS_ALIGNED(crash_base, PMD_SIZE)) {
849-
pr_warn("crashkernel: requested region is misaligned\n");
850-
return;
851-
}
827+
/*
828+
* Current riscv boot protocol requires 2MB alignment for
829+
* RV64 and 4MB alignment for RV32 (hugepage size)
830+
*/
831+
crash_base = memblock_phys_alloc_range(crash_size, PMD_SIZE,
832+
search_start, search_end);
833+
if (crash_base == 0) {
834+
pr_warn("crashkernel: couldn't allocate %lldKB\n",
835+
crash_size >> 10);
836+
return;
852837
}
853-
memblock_reserve(crash_base, crash_size);
854838

855839
pr_info("crashkernel: reserved 0x%016llx - 0x%016llx (%lld MB)\n",
856840
crash_base, crash_base + crash_size, crash_size >> 20);

arch/s390/kernel/setup.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -626,8 +626,9 @@ static void __init reserve_crashkernel(void)
626626
return;
627627
}
628628
low = crash_base ?: low;
629-
crash_base = memblock_find_in_range(low, high, crash_size,
630-
KEXEC_CRASH_MEM_ALIGN);
629+
crash_base = memblock_phys_alloc_range(crash_size,
630+
KEXEC_CRASH_MEM_ALIGN,
631+
low, high);
631632
}
632633

633634
if (!crash_base) {
@@ -636,8 +637,10 @@ static void __init reserve_crashkernel(void)
636637
return;
637638
}
638639

639-
if (register_memory_notifier(&kdump_mem_nb))
640+
if (register_memory_notifier(&kdump_mem_nb)) {
641+
memblock_free(crash_base, crash_size);
640642
return;
643+
}
641644

642645
if (!OLDMEM_BASE && MACHINE_IS_VM)
643646
diag10_range(PFN_DOWN(crash_base), PFN_DOWN(crash_size));

arch/x86/kernel/aperture_64.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,13 @@ static u32 __init allocate_aperture(void)
109109
* memory. Unfortunately we cannot move it up because that would
110110
* make the IOMMU useless.
111111
*/
112-
addr = memblock_find_in_range(GART_MIN_ADDR, GART_MAX_ADDR,
113-
aper_size, aper_size);
112+
addr = memblock_phys_alloc_range(aper_size, aper_size,
113+
GART_MIN_ADDR, GART_MAX_ADDR);
114114
if (!addr) {
115115
pr_err("Cannot allocate aperture memory hole [mem %#010lx-%#010lx] (%uKB)\n",
116116
addr, addr + aper_size - 1, aper_size >> 10);
117117
return 0;
118118
}
119-
memblock_reserve(addr, aper_size);
120119
pr_info("Mapping aperture over RAM [mem %#010lx-%#010lx] (%uKB)\n",
121120
addr, addr + aper_size - 1, aper_size >> 10);
122121
register_nosave_region(addr >> PAGE_SHIFT,

arch/x86/mm/init.c

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,12 @@ __ref void *alloc_low_pages(unsigned int num)
127127
unsigned long ret = 0;
128128

129129
if (min_pfn_mapped < max_pfn_mapped) {
130-
ret = memblock_find_in_range(
130+
ret = memblock_phys_alloc_range(
131+
PAGE_SIZE * num, PAGE_SIZE,
131132
min_pfn_mapped << PAGE_SHIFT,
132-
max_pfn_mapped << PAGE_SHIFT,
133-
PAGE_SIZE * num , PAGE_SIZE);
133+
max_pfn_mapped << PAGE_SHIFT);
134134
}
135-
if (ret)
136-
memblock_reserve(ret, PAGE_SIZE * num);
137-
else if (can_use_brk_pgt)
135+
if (!ret && can_use_brk_pgt)
138136
ret = __pa(extend_brk(PAGE_SIZE * num, PAGE_SIZE));
139137

140138
if (!ret)
@@ -610,8 +608,17 @@ static void __init memory_map_top_down(unsigned long map_start,
610608
unsigned long addr;
611609
unsigned long mapped_ram_size = 0;
612610

613-
/* xen has big range in reserved near end of ram, skip it at first.*/
614-
addr = memblock_find_in_range(map_start, map_end, PMD_SIZE, PMD_SIZE);
611+
/*
612+
* Systems that have many reserved areas near top of the memory,
613+
* e.g. QEMU with less than 1G RAM and EFI enabled, or Xen, will
614+
* require lots of 4K mappings which may exhaust pgt_buf.
615+
* Start with top-most PMD_SIZE range aligned at PMD_SIZE to ensure
616+
* there is enough mapped memory that can be allocated from
617+
* memblock.
618+
*/
619+
addr = memblock_phys_alloc_range(PMD_SIZE, PMD_SIZE, map_start,
620+
map_end);
621+
memblock_free(addr, PMD_SIZE);
615622
real_end = addr + PMD_SIZE;
616623

617624
/* step_size need to be small so pgt_buf from BRK could cover it */

arch/x86/mm/numa.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -376,15 +376,14 @@ static int __init numa_alloc_distance(void)
376376
cnt++;
377377
size = cnt * cnt * sizeof(numa_distance[0]);
378378

379-
phys = memblock_find_in_range(0, PFN_PHYS(max_pfn_mapped),
380-
size, PAGE_SIZE);
379+
phys = memblock_phys_alloc_range(size, PAGE_SIZE, 0,
380+
PFN_PHYS(max_pfn_mapped));
381381
if (!phys) {
382382
pr_warn("Warning: can't allocate distance table!\n");
383383
/* don't retry until explicitly reset */
384384
numa_distance = (void *)1LU;
385385
return -ENOMEM;
386386
}
387-
memblock_reserve(phys, size);
388387

389388
numa_distance = __va(phys);
390389
numa_distance_cnt = cnt;

arch/x86/mm/numa_emulation.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -447,13 +447,12 @@ void __init numa_emulation(struct numa_meminfo *numa_meminfo, int numa_dist_cnt)
447447
if (numa_dist_cnt) {
448448
u64 phys;
449449

450-
phys = memblock_find_in_range(0, PFN_PHYS(max_pfn_mapped),
451-
phys_size, PAGE_SIZE);
450+
phys = memblock_phys_alloc_range(phys_size, PAGE_SIZE, 0,
451+
PFN_PHYS(max_pfn_mapped));
452452
if (!phys) {
453453
pr_warn("NUMA: Warning: can't allocate copy of distance table, disabling emulation\n");
454454
goto no_emu;
455455
}
456-
memblock_reserve(phys, phys_size);
457456
phys_dist = __va(phys);
458457

459458
for (i = 0; i < numa_dist_cnt; i++)

arch/x86/realmode/init.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ void __init reserve_real_mode(void)
2828
WARN_ON(slab_is_available());
2929

3030
/* Has to be under 1M so we can execute real-mode AP code. */
31-
mem = memblock_find_in_range(0, 1<<20, size, PAGE_SIZE);
31+
mem = memblock_phys_alloc_range(size, PAGE_SIZE, 0, 1<<20);
3232
if (!mem)
3333
pr_info("No sub-1M memory is available for the trampoline\n");
3434
else

drivers/acpi/tables.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -583,8 +583,8 @@ void __init acpi_table_upgrade(void)
583583
}
584584

585585
acpi_tables_addr =
586-
memblock_find_in_range(0, ACPI_TABLE_UPGRADE_MAX_PHYS,
587-
all_tables_size, PAGE_SIZE);
586+
memblock_phys_alloc_range(all_tables_size, PAGE_SIZE,
587+
0, ACPI_TABLE_UPGRADE_MAX_PHYS);
588588
if (!acpi_tables_addr) {
589589
WARN_ON(1);
590590
return;
@@ -599,7 +599,6 @@ void __init acpi_table_upgrade(void)
599599
* Both memblock_reserve and e820__range_add (via arch_reserve_mem_area)
600600
* works fine.
601601
*/
602-
memblock_reserve(acpi_tables_addr, all_tables_size);
603602
arch_reserve_mem_area(acpi_tables_addr, all_tables_size);
604603

605604
/*

drivers/base/arch_numa.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -279,13 +279,10 @@ static int __init numa_alloc_distance(void)
279279
int i, j;
280280

281281
size = nr_node_ids * nr_node_ids * sizeof(numa_distance[0]);
282-
phys = memblock_find_in_range(0, PFN_PHYS(max_pfn),
283-
size, PAGE_SIZE);
282+
phys = memblock_phys_alloc_range(size, PAGE_SIZE, 0, PFN_PHYS(max_pfn));
284283
if (WARN_ON(!phys))
285284
return -ENOMEM;
286285

287-
memblock_reserve(phys, size);
288-
289286
numa_distance = __va(phys);
290287
numa_distance_cnt = nr_node_ids;
291288

drivers/of/of_reserved_mem.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,22 @@ static int __init early_init_dt_alloc_reserved_memory_arch(phys_addr_t size,
3333
phys_addr_t *res_base)
3434
{
3535
phys_addr_t base;
36+
int err = 0;
3637

3738
end = !end ? MEMBLOCK_ALLOC_ANYWHERE : end;
3839
align = !align ? SMP_CACHE_BYTES : align;
39-
base = memblock_find_in_range(start, end, size, align);
40+
base = memblock_phys_alloc_range(size, align, start, end);
4041
if (!base)
4142
return -ENOMEM;
4243

4344
*res_base = base;
44-
if (nomap)
45-
return memblock_mark_nomap(base, size);
45+
if (nomap) {
46+
err = memblock_mark_nomap(base, size);
47+
if (err)
48+
memblock_free(base, size);
49+
}
4650

47-
return memblock_reserve(base, size);
51+
return err;
4852
}
4953

5054
/*

include/linux/memblock.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,6 @@ void memblock_discard(void);
9999
static inline void memblock_discard(void) {}
100100
#endif
101101

102-
phys_addr_t memblock_find_in_range(phys_addr_t start, phys_addr_t end,
103-
phys_addr_t size, phys_addr_t align);
104102
void memblock_allow_resize(void);
105103
int memblock_add_node(phys_addr_t base, phys_addr_t size, int nid);
106104
int memblock_add(phys_addr_t base, phys_addr_t size);

0 commit comments

Comments
 (0)