[PATCH v3 1/2] x86/kdump: Split some code out of reserve_crashkernel

From: Kairui Song
Date: Tue Sep 10 2019 - 11:18:54 EST


Split out the code related to finding suitable region for kdump out of
reserve_crashkernel, clean up and refactor for further change, no feature
change.

Signed-off-by: Kairui Song <kasong@xxxxxxxxxx>
---
arch/x86/kernel/setup.c | 92 +++++++++++++++++++++++++++--------------
1 file changed, 60 insertions(+), 32 deletions(-)

diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c
index bbe35bf879f5..71f20bb18cb0 100644
--- a/arch/x86/kernel/setup.c
+++ b/arch/x86/kernel/setup.c
@@ -526,6 +526,63 @@ static int __init reserve_crashkernel_low(void)
return 0;
}

+static int __init crashkernel_find_region(unsigned long long *crash_base,
+ unsigned long long *crash_size,
+ bool high)
+{
+ unsigned long long base, size;
+
+ base = *crash_base;
+ size = *crash_size;
+
+ /*
+ * base == 0 means: find the address automatically, else just
+ * verify the region is useable
+ */
+ if (base) {
+ unsigned long long start;
+
+ start = memblock_find_in_range(base, base + size,
+ size, 1 << 20);
+ if (start != base) {
+ pr_info("crashkernel reservation failed - memory is in use.\n");
+ return -1;
+ }
+ return 0;
+ }
+
+ /*
+ * crashkernel=x,high reserves memory over 4G, also allocates
+ * 256M extra low memory for DMA buffers and swiotlb.
+ * But the extra memory is not required for all machines.
+ * So try low memory first and fall back to high memory
+ * unless "crashkernel=size[KMG],high" is specified.
+ */
+ if (high)
+ goto high_reserve;
+
+ base = memblock_find_in_range(CRASH_ALIGN,
+ CRASH_ADDR_LOW_MAX, size,
+ CRASH_ALIGN);
+ if (base)
+ goto found;
+
+high_reserve:
+ /* Try high reserve */
+ base = memblock_find_in_range(CRASH_ALIGN,
+ CRASH_ADDR_HIGH_MAX, size,
+ CRASH_ALIGN);
+ if (base)
+ goto found;
+
+ pr_info("crashkernel reservation failed - No suitable area found.\n");
+ return -1;
+found:
+ *crash_base = base;
+ *crash_size = size;
+ return 0;
+}
+
static void __init reserve_crashkernel(void)
{
unsigned long long crash_size, crash_base, total_mem;
@@ -550,39 +607,10 @@ static void __init reserve_crashkernel(void)
return;
}

- /* 0 means: find the address automatically */
- if (!crash_base) {
- /*
- * Set CRASH_ADDR_LOW_MAX upper bound for crash memory,
- * crashkernel=x,high reserves memory over 4G, also allocates
- * 256M extra low memory for DMA buffers and swiotlb.
- * But the extra memory is not required for all machines.
- * So try low memory first and fall back to high memory
- * unless "crashkernel=size[KMG],high" is specified.
- */
- if (!high)
- crash_base = memblock_find_in_range(CRASH_ALIGN,
- CRASH_ADDR_LOW_MAX,
- crash_size, CRASH_ALIGN);
- if (!crash_base)
- crash_base = memblock_find_in_range(CRASH_ALIGN,
- CRASH_ADDR_HIGH_MAX,
- crash_size, CRASH_ALIGN);
- if (!crash_base) {
- pr_info("crashkernel reservation failed - No suitable area found.\n");
- return;
- }
- } else {
- unsigned long long start;
+ ret = crashkernel_find_region(&crash_base, &crash_size, high);
+ if (ret)
+ return;

- start = memblock_find_in_range(crash_base,
- crash_base + crash_size,
- crash_size, 1 << 20);
- if (start != crash_base) {
- pr_info("crashkernel reservation failed - memory is in use.\n");
- return;
- }
- }
ret = memblock_reserve(crash_base, crash_size);
if (ret) {
pr_err("%s: Error reserving crashkernel memblock.\n", __func__);
--
2.21.0