Skip to content

Commit 3936539

Browse files
Baoquan Heakpm00
authored andcommitted
riscv: kdump: use generic interface to simplify crashkernel reservation
With the help of newly changed function parse_crashkernel() and generic reserve_crashkernel_generic(), crashkernel reservation can be simplified by steps: 1) Add a new header file <asm/crash_core.h>, and define CRASH_ALIGN, CRASH_ADDR_LOW_MAX, CRASH_ADDR_HIGH_MAX and DEFAULT_CRASH_KERNEL_LOW_SIZE in <asm/crash_core.h>; 2) Add arch_reserve_crashkernel() to call parse_crashkernel() and reserve_crashkernel_generic(); 3) Add ARCH_HAS_GENERIC_CRASHKERNEL_RESERVATION Kconfig in arch/riscv/Kconfig. The old reserve_crashkernel_low() and reserve_crashkernel() can be removed. [[email protected]: fix crashkernel reserving problem on RISC-V] Link: https://lkml.kernel.org/r/[email protected] Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Baoquan He <[email protected]> Signed-off-by: Chen Jiahao <[email protected]> Cc: Catalin Marinas <[email protected]> Cc: Chen Jiahao <[email protected]> Cc: Zhen Lei <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent fdc2682 commit 3936539

File tree

5 files changed

+27
-143
lines changed

5 files changed

+27
-143
lines changed

arch/riscv/Kconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,9 @@ config ARCH_SUPPORTS_KEXEC_PURGATORY
694694
config ARCH_SUPPORTS_CRASH_DUMP
695695
def_bool y
696696

697+
config ARCH_HAS_GENERIC_CRASHKERNEL_RESERVATION
698+
def_bool CRASH_CORE
699+
697700
config COMPAT
698701
bool "Kernel support for 32-bit U-mode"
699702
default 64BIT

arch/riscv/include/asm/crash_core.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/* SPDX-License-Identifier: GPL-2.0-only */
2+
#ifndef _RISCV_CRASH_CORE_H
3+
#define _RISCV_CRASH_CORE_H
4+
5+
#define CRASH_ALIGN PMD_SIZE
6+
7+
#define CRASH_ADDR_LOW_MAX dma32_phys_limit
8+
#define CRASH_ADDR_HIGH_MAX memblock_end_of_DRAM()
9+
10+
extern phys_addr_t memblock_end_of_DRAM(void);
11+
#endif

arch/riscv/include/asm/processor.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ static inline void wait_for_interrupt(void)
116116
__asm__ __volatile__ ("wfi");
117117
}
118118

119+
extern phys_addr_t dma32_phys_limit;
120+
119121
struct device_node;
120122
int riscv_of_processor_hartid(struct device_node *node, unsigned long *hartid);
121123
int riscv_early_of_processor_hartid(struct device_node *node, unsigned long *hartid);

arch/riscv/kernel/setup.c

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -173,19 +173,6 @@ static void __init init_resources(void)
173173
if (ret < 0)
174174
goto error;
175175

176-
#ifdef CONFIG_KEXEC_CORE
177-
if (crashk_res.start != crashk_res.end) {
178-
ret = add_resource(&iomem_resource, &crashk_res);
179-
if (ret < 0)
180-
goto error;
181-
}
182-
if (crashk_low_res.start != crashk_low_res.end) {
183-
ret = add_resource(&iomem_resource, &crashk_low_res);
184-
if (ret < 0)
185-
goto error;
186-
}
187-
#endif
188-
189176
#ifdef CONFIG_CRASH_DUMP
190177
if (elfcorehdr_size > 0) {
191178
elfcorehdr_res.start = elfcorehdr_addr;

arch/riscv/mm/init.c

Lines changed: 11 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ extern char _start[];
6565
void *_dtb_early_va __initdata;
6666
uintptr_t _dtb_early_pa __initdata;
6767

68-
static phys_addr_t dma32_phys_limit __initdata;
68+
phys_addr_t dma32_phys_limit __initdata;
6969

7070
static void __init zone_sizes_init(void)
7171
{
@@ -1333,151 +1333,32 @@ static inline void setup_vm_final(void)
13331333
}
13341334
#endif /* CONFIG_MMU */
13351335

1336-
/* Reserve 128M low memory by default for swiotlb buffer */
1337-
#define DEFAULT_CRASH_KERNEL_LOW_SIZE (128UL << 20)
1338-
1339-
static int __init reserve_crashkernel_low(unsigned long long low_size)
1340-
{
1341-
unsigned long long low_base;
1342-
1343-
low_base = memblock_phys_alloc_range(low_size, PMD_SIZE, 0, dma32_phys_limit);
1344-
if (!low_base) {
1345-
pr_err("cannot allocate crashkernel low memory (size:0x%llx).\n", low_size);
1346-
return -ENOMEM;
1347-
}
1348-
1349-
pr_info("crashkernel low memory reserved: 0x%016llx - 0x%016llx (%lld MB)\n",
1350-
low_base, low_base + low_size, low_size >> 20);
1351-
1352-
crashk_low_res.start = low_base;
1353-
crashk_low_res.end = low_base + low_size - 1;
1354-
1355-
return 0;
1356-
}
1357-
13581336
/*
13591337
* reserve_crashkernel() - reserves memory for crash kernel
13601338
*
13611339
* This function reserves memory area given in "crashkernel=" kernel command
13621340
* line parameter. The memory reserved is used by dump capture kernel when
13631341
* primary kernel is crashing.
13641342
*/
1365-
static void __init reserve_crashkernel(void)
1343+
static void __init arch_reserve_crashkernel(void)
13661344
{
1367-
unsigned long long crash_base = 0;
1368-
unsigned long long crash_size = 0;
1369-
unsigned long long crash_low_size = 0;
1370-
unsigned long search_start = memblock_start_of_DRAM();
1371-
unsigned long search_end = (unsigned long)dma32_phys_limit;
1345+
unsigned long long low_size = 0;
1346+
unsigned long long crash_base, crash_size;
13721347
char *cmdline = boot_command_line;
1373-
bool fixed_base = false;
13741348
bool high = false;
1375-
1376-
int ret = 0;
1349+
int ret;
13771350

13781351
if (!IS_ENABLED(CONFIG_KEXEC_CORE))
13791352
return;
1380-
/*
1381-
* Don't reserve a region for a crash kernel on a crash kernel
1382-
* since it doesn't make much sense and we have limited memory
1383-
* resources.
1384-
*/
1385-
if (is_kdump_kernel()) {
1386-
pr_info("crashkernel: ignoring reservation request\n");
1387-
return;
1388-
}
13891353

13901354
ret = parse_crashkernel(cmdline, memblock_phys_mem_size(),
1391-
&crash_size, &crash_base, NULL, NULL);
1392-
if (ret == -ENOENT) {
1393-
/* Fallback to crashkernel=X,[high,low] */
1394-
ret = parse_crashkernel_high(cmdline, 0, &crash_size, &crash_base);
1395-
if (ret || !crash_size)
1396-
return;
1397-
1398-
/*
1399-
* crashkernel=Y,low is valid only when crashkernel=X,high
1400-
* is passed.
1401-
*/
1402-
ret = parse_crashkernel_low(cmdline, 0, &crash_low_size, &crash_base);
1403-
if (ret == -ENOENT)
1404-
crash_low_size = DEFAULT_CRASH_KERNEL_LOW_SIZE;
1405-
else if (ret)
1406-
return;
1407-
1408-
search_start = (unsigned long)dma32_phys_limit;
1409-
search_end = memblock_end_of_DRAM();
1410-
high = true;
1411-
} else if (ret || !crash_size) {
1412-
/* Invalid argument value specified */
1355+
&crash_size, &crash_base,
1356+
&low_size, &high);
1357+
if (ret)
14131358
return;
1414-
}
1415-
1416-
crash_size = PAGE_ALIGN(crash_size);
1417-
1418-
if (crash_base) {
1419-
fixed_base = true;
1420-
search_start = crash_base;
1421-
search_end = crash_base + crash_size;
1422-
}
1423-
1424-
/*
1425-
* Current riscv boot protocol requires 2MB alignment for
1426-
* RV64 and 4MB alignment for RV32 (hugepage size)
1427-
*
1428-
* Try to alloc from 32bit addressible physical memory so that
1429-
* swiotlb can work on the crash kernel.
1430-
*/
1431-
crash_base = memblock_phys_alloc_range(crash_size, PMD_SIZE,
1432-
search_start, search_end);
1433-
if (crash_base == 0) {
1434-
/*
1435-
* For crashkernel=size[KMG]@offset[KMG], print out failure
1436-
* message if can't reserve the specified region.
1437-
*/
1438-
if (fixed_base) {
1439-
pr_warn("crashkernel: allocating failed with given size@offset\n");
1440-
return;
1441-
}
1442-
1443-
if (high) {
1444-
/*
1445-
* For crashkernel=size[KMG],high, if the first attempt was
1446-
* for high memory, fall back to low memory.
1447-
*/
1448-
search_start = memblock_start_of_DRAM();
1449-
search_end = (unsigned long)dma32_phys_limit;
1450-
} else {
1451-
/*
1452-
* For crashkernel=size[KMG], if the first attempt was for
1453-
* low memory, fall back to high memory, the minimum required
1454-
* low memory will be reserved later.
1455-
*/
1456-
search_start = (unsigned long)dma32_phys_limit;
1457-
search_end = memblock_end_of_DRAM();
1458-
crash_low_size = DEFAULT_CRASH_KERNEL_LOW_SIZE;
1459-
}
1460-
1461-
crash_base = memblock_phys_alloc_range(crash_size, PMD_SIZE,
1462-
search_start, search_end);
1463-
if (crash_base == 0) {
1464-
pr_warn("crashkernel: couldn't allocate %lldKB\n",
1465-
crash_size >> 10);
1466-
return;
1467-
}
1468-
}
1469-
1470-
if ((crash_base >= dma32_phys_limit) && crash_low_size &&
1471-
reserve_crashkernel_low(crash_low_size)) {
1472-
memblock_phys_free(crash_base, crash_size);
1473-
return;
1474-
}
1475-
1476-
pr_info("crashkernel: reserved 0x%016llx - 0x%016llx (%lld MB)\n",
1477-
crash_base, crash_base + crash_size, crash_size >> 20);
14781359

1479-
crashk_res.start = crash_base;
1480-
crashk_res.end = crash_base + crash_size - 1;
1360+
reserve_crashkernel_generic(cmdline, crash_size, crash_base,
1361+
low_size, high);
14811362
}
14821363

14831364
void __init paging_init(void)
@@ -1495,7 +1376,7 @@ void __init misc_mem_init(void)
14951376
arch_numa_init();
14961377
sparse_init();
14971378
zone_sizes_init();
1498-
reserve_crashkernel();
1379+
arch_reserve_crashkernel();
14991380
memblock_dump_all();
15001381
}
15011382

0 commit comments

Comments
 (0)