diff mbox series

[v2,6/8] x86: kdump: use generic interface to simplify crashkernel reservation code

Message ID 20230829121610.138107-7-bhe@redhat.com (mailing list archive)
State New, archived
Headers show
Series kdump: use generic functions to simplify crashkernel reservation in arch | expand

Commit Message

Baoquan He Aug. 29, 2023, 12:16 p.m. UTC
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(), and do the ARCH specific work if
   needed.

3) Add ARCH_HAS_GENERIC_CRASHKERNEL_RESERVATION Kconfig in
   arch/x86/Kconfig.

When adding DEFAULT_CRASH_KERNEL_LOW_SIZE, add crash_low_size_default()
to calculate crashkernel low memory because x86_64 has special
requirement.

The old reserve_crashkernel_low() and reserve_crashkernel() can be
removed.

Signed-off-by: Baoquan He <bhe@redhat.com>
---
 arch/x86/Kconfig                  |   3 +
 arch/x86/include/asm/crash_core.h |  34 +++++++
 arch/x86/kernel/setup.c           | 144 ++++--------------------------
 3 files changed, 53 insertions(+), 128 deletions(-)
 create mode 100644 arch/x86/include/asm/crash_core.h

Comments

kernel test robot Aug. 29, 2023, 9:37 p.m. UTC | #1
Hi Baoquan,

kernel test robot noticed the following build errors:

[auto build test ERROR on arm64/for-next/core]
[also build test ERROR on tip/x86/core powerpc/next powerpc/fixes linus/master v6.5]
[cannot apply to next-20230829]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Baoquan-He/crash_core-c-remove-unnecessary-parameter-of-function/20230829-201942
base:   https://git.kernel.org/pub/scm/linux/kernel/git/arm64/linux.git for-next/core
patch link:    https://lore.kernel.org/r/20230829121610.138107-7-bhe%40redhat.com
patch subject: [PATCH v2 6/8] x86: kdump: use generic interface to simplify crashkernel reservation code
config: i386-randconfig-r026-20230830 (https://download.01.org/0day-ci/archive/20230830/202308300522.oOG0V3U3-lkp@intel.com/config)
compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20230830/202308300522.oOG0V3U3-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202308300522.oOG0V3U3-lkp@intel.com/

All errors (new ones prefixed by >>):

   ld: kernel/crash_core.o: in function `reserve_crashkernel_generic':
>> kernel/crash_core.c:450: undefined reference to `crashk_low_res'
>> ld: kernel/crash_core.c:451: undefined reference to `crashk_low_res'
>> ld: kernel/crash_core.c:455: undefined reference to `crashk_res'
   ld: kernel/crash_core.c:453: undefined reference to `crashk_res'
   ld: kernel/crash_core.c:454: undefined reference to `crashk_res'


vim +450 kernel/crash_core.c

6bee83d29d2e09 Baoquan He 2023-08-29  375  
6bee83d29d2e09 Baoquan He 2023-08-29  376  void __init reserve_crashkernel_generic(char *cmdline,
6bee83d29d2e09 Baoquan He 2023-08-29  377  			     unsigned long long crash_size,
6bee83d29d2e09 Baoquan He 2023-08-29  378  			     unsigned long long crash_base,
6bee83d29d2e09 Baoquan He 2023-08-29  379  			     unsigned long long crash_low_size,
6bee83d29d2e09 Baoquan He 2023-08-29  380  			     bool high)
6bee83d29d2e09 Baoquan He 2023-08-29  381  {
6bee83d29d2e09 Baoquan He 2023-08-29  382  	unsigned long long search_end = CRASH_ADDR_LOW_MAX, search_base = 0;
6bee83d29d2e09 Baoquan He 2023-08-29  383  	bool fixed_base = false;
6bee83d29d2e09 Baoquan He 2023-08-29  384  
6bee83d29d2e09 Baoquan He 2023-08-29  385  	/* User specifies base address explicitly. */
6bee83d29d2e09 Baoquan He 2023-08-29  386  	if (crash_base) {
6bee83d29d2e09 Baoquan He 2023-08-29  387  		fixed_base = true;
6bee83d29d2e09 Baoquan He 2023-08-29  388  		search_base = crash_base;
6bee83d29d2e09 Baoquan He 2023-08-29  389  		search_end = crash_base + crash_size;
6bee83d29d2e09 Baoquan He 2023-08-29  390  	}
6bee83d29d2e09 Baoquan He 2023-08-29  391  
6bee83d29d2e09 Baoquan He 2023-08-29  392  	if (high) {
6bee83d29d2e09 Baoquan He 2023-08-29  393  		search_base = CRASH_ADDR_LOW_MAX;
6bee83d29d2e09 Baoquan He 2023-08-29  394  		search_end = CRASH_ADDR_HIGH_MAX;
6bee83d29d2e09 Baoquan He 2023-08-29  395  	}
6bee83d29d2e09 Baoquan He 2023-08-29  396  
6bee83d29d2e09 Baoquan He 2023-08-29  397  retry:
6bee83d29d2e09 Baoquan He 2023-08-29  398  	crash_base = memblock_phys_alloc_range(crash_size, CRASH_ALIGN,
6bee83d29d2e09 Baoquan He 2023-08-29  399  					       search_base, search_end);
6bee83d29d2e09 Baoquan He 2023-08-29  400  	if (!crash_base) {
6bee83d29d2e09 Baoquan He 2023-08-29  401  		/*
6bee83d29d2e09 Baoquan He 2023-08-29  402  		 * For crashkernel=size[KMG]@offset[KMG], print out failure
6bee83d29d2e09 Baoquan He 2023-08-29  403  		 * message if can't reserve the specified region.
6bee83d29d2e09 Baoquan He 2023-08-29  404  		 */
6bee83d29d2e09 Baoquan He 2023-08-29  405  		if (fixed_base) {
6bee83d29d2e09 Baoquan He 2023-08-29  406  			pr_warn("crashkernel reservation failed - memory is in use.\n");
6bee83d29d2e09 Baoquan He 2023-08-29  407  			return;
6bee83d29d2e09 Baoquan He 2023-08-29  408  		}
6bee83d29d2e09 Baoquan He 2023-08-29  409  
6bee83d29d2e09 Baoquan He 2023-08-29  410  		/*
6bee83d29d2e09 Baoquan He 2023-08-29  411  		 * For crashkernel=size[KMG], if the first attempt was for
6bee83d29d2e09 Baoquan He 2023-08-29  412  		 * low memory, fall back to high memory, the minimum required
6bee83d29d2e09 Baoquan He 2023-08-29  413  		 * low memory will be reserved later.
6bee83d29d2e09 Baoquan He 2023-08-29  414  		 */
6bee83d29d2e09 Baoquan He 2023-08-29  415  		if (!high && search_end == CRASH_ADDR_LOW_MAX) {
6bee83d29d2e09 Baoquan He 2023-08-29  416  			search_end = CRASH_ADDR_HIGH_MAX;
6bee83d29d2e09 Baoquan He 2023-08-29  417  			search_base = CRASH_ADDR_LOW_MAX;
6bee83d29d2e09 Baoquan He 2023-08-29  418  			crash_low_size = DEFAULT_CRASH_KERNEL_LOW_SIZE;
6bee83d29d2e09 Baoquan He 2023-08-29  419  			goto retry;
6bee83d29d2e09 Baoquan He 2023-08-29  420  		}
6bee83d29d2e09 Baoquan He 2023-08-29  421  
6bee83d29d2e09 Baoquan He 2023-08-29  422  		/*
6bee83d29d2e09 Baoquan He 2023-08-29  423  		 * For crashkernel=size[KMG],high, if the first attempt was
6bee83d29d2e09 Baoquan He 2023-08-29  424  		 * for high memory, fall back to low memory.
6bee83d29d2e09 Baoquan He 2023-08-29  425  		 */
6bee83d29d2e09 Baoquan He 2023-08-29  426  		if (high && search_end == CRASH_ADDR_HIGH_MAX) {
6bee83d29d2e09 Baoquan He 2023-08-29  427  			search_end = CRASH_ADDR_LOW_MAX;
6bee83d29d2e09 Baoquan He 2023-08-29  428  			search_base = 0;
6bee83d29d2e09 Baoquan He 2023-08-29  429  			goto retry;
6bee83d29d2e09 Baoquan He 2023-08-29  430  		}
6bee83d29d2e09 Baoquan He 2023-08-29  431  		pr_warn("cannot allocate crashkernel (size:0x%llx)\n",
6bee83d29d2e09 Baoquan He 2023-08-29  432  			crash_size);
6bee83d29d2e09 Baoquan He 2023-08-29  433  		return;
6bee83d29d2e09 Baoquan He 2023-08-29  434  	}
6bee83d29d2e09 Baoquan He 2023-08-29  435  
6bee83d29d2e09 Baoquan He 2023-08-29  436  	if ((crash_base > CRASH_ADDR_LOW_MAX) &&
6bee83d29d2e09 Baoquan He 2023-08-29  437  	     crash_low_size && reserve_crashkernel_low(crash_low_size)) {
6bee83d29d2e09 Baoquan He 2023-08-29  438  		memblock_phys_free(crash_base, crash_size);
6bee83d29d2e09 Baoquan He 2023-08-29  439  		return;
6bee83d29d2e09 Baoquan He 2023-08-29  440  	}
6bee83d29d2e09 Baoquan He 2023-08-29  441  
6bee83d29d2e09 Baoquan He 2023-08-29  442  	pr_info("crashkernel reserved: 0x%016llx - 0x%016llx (%lld MB)\n",
6bee83d29d2e09 Baoquan He 2023-08-29  443  		crash_base, crash_base + crash_size, crash_size >> 20);
6bee83d29d2e09 Baoquan He 2023-08-29  444  
6bee83d29d2e09 Baoquan He 2023-08-29  445  	/*
6bee83d29d2e09 Baoquan He 2023-08-29  446  	 * The crashkernel memory will be removed from the kernel linear
6bee83d29d2e09 Baoquan He 2023-08-29  447  	 * map. Inform kmemleak so that it won't try to access it.
6bee83d29d2e09 Baoquan He 2023-08-29  448  	 */
6bee83d29d2e09 Baoquan He 2023-08-29  449  	kmemleak_ignore_phys(crash_base);
6bee83d29d2e09 Baoquan He 2023-08-29 @450  	if (crashk_low_res.end)
6bee83d29d2e09 Baoquan He 2023-08-29 @451  		kmemleak_ignore_phys(crashk_low_res.start);
6bee83d29d2e09 Baoquan He 2023-08-29  452  
6bee83d29d2e09 Baoquan He 2023-08-29  453  	crashk_res.start = crash_base;
6bee83d29d2e09 Baoquan He 2023-08-29  454  	crashk_res.end = crash_base + crash_size - 1;
6bee83d29d2e09 Baoquan He 2023-08-29 @455  	insert_resource(&iomem_resource, &crashk_res);
6bee83d29d2e09 Baoquan He 2023-08-29  456  }
6bee83d29d2e09 Baoquan He 2023-08-29  457  #endif
6bee83d29d2e09 Baoquan He 2023-08-29  458
kernel test robot Aug. 30, 2023, 1:49 a.m. UTC | #2
Hi Baoquan,

kernel test robot noticed the following build errors:

[auto build test ERROR on arm64/for-next/core]
[also build test ERROR on tip/x86/core powerpc/next powerpc/fixes v6.5]
[cannot apply to linus/master next-20230829]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Baoquan-He/crash_core-c-remove-unnecessary-parameter-of-function/20230829-201942
base:   https://git.kernel.org/pub/scm/linux/kernel/git/arm64/linux.git for-next/core
patch link:    https://lore.kernel.org/r/20230829121610.138107-7-bhe%40redhat.com
patch subject: [PATCH v2 6/8] x86: kdump: use generic interface to simplify crashkernel reservation code
config: x86_64-randconfig-r022-20230830 (https://download.01.org/0day-ci/archive/20230830/202308300910.e0i4piJT-lkp@intel.com/config)
compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20230830/202308300910.e0i4piJT-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202308300910.e0i4piJT-lkp@intel.com/

All errors (new ones prefixed by >>):

   ld: vmlinux.o: in function `reserve_crashkernel_low':
   kernel/crash_core.c:369: undefined reference to `crashk_low_res'
   ld: kernel/crash_core.c:369: undefined reference to `crashk_low_res'
   ld: kernel/crash_core.c:370: undefined reference to `crashk_low_res'
   ld: kernel/crash_core.c:369: undefined reference to `crashk_low_res'
   ld: kernel/crash_core.c:370: undefined reference to `crashk_low_res'
   ld: vmlinux.o:kernel/crash_core.c:371: more undefined references to `crashk_low_res' follow
   ld: vmlinux.o: in function `reserve_crashkernel_generic':
>> kernel/crash_core.c:453: undefined reference to `crashk_res'
>> ld: kernel/crash_core.c:453: undefined reference to `crashk_res'
   ld: kernel/crash_core.c:454: undefined reference to `crashk_res'
>> ld: kernel/crash_core.c:453: undefined reference to `crashk_res'
   ld: kernel/crash_core.c:454: undefined reference to `crashk_res'
   ld: vmlinux.o:kernel/crash_core.c:454: more undefined references to `crashk_res' follow


vim +453 kernel/crash_core.c

71d2bcec2d4d69 Philipp Rudo 2021-12-24  353  
6bee83d29d2e09 Baoquan He   2023-08-29  354  #ifdef CONFIG_ARCH_HAS_GENERIC_CRASHKERNEL_RESERVATION
6bee83d29d2e09 Baoquan He   2023-08-29  355  static int __init reserve_crashkernel_low(unsigned long long low_size)
6bee83d29d2e09 Baoquan He   2023-08-29  356  {
6bee83d29d2e09 Baoquan He   2023-08-29  357  #ifdef CONFIG_64BIT
6bee83d29d2e09 Baoquan He   2023-08-29  358  	unsigned long long low_base;
6bee83d29d2e09 Baoquan He   2023-08-29  359  
6bee83d29d2e09 Baoquan He   2023-08-29  360  	low_base = memblock_phys_alloc_range(low_size, CRASH_ALIGN, 0, CRASH_ADDR_LOW_MAX);
6bee83d29d2e09 Baoquan He   2023-08-29  361  	if (!low_base) {
6bee83d29d2e09 Baoquan He   2023-08-29  362  		pr_err("cannot allocate crashkernel low memory (size:0x%llx).\n", low_size);
6bee83d29d2e09 Baoquan He   2023-08-29  363  		return -ENOMEM;
6bee83d29d2e09 Baoquan He   2023-08-29  364  	}
6bee83d29d2e09 Baoquan He   2023-08-29  365  
6bee83d29d2e09 Baoquan He   2023-08-29  366  	pr_info("crashkernel low memory reserved: 0x%08llx - 0x%08llx (%lld MB)\n",
6bee83d29d2e09 Baoquan He   2023-08-29  367  		low_base, low_base + low_size, low_size >> 20);
6bee83d29d2e09 Baoquan He   2023-08-29  368  
6bee83d29d2e09 Baoquan He   2023-08-29 @369  	crashk_low_res.start = low_base;
6bee83d29d2e09 Baoquan He   2023-08-29  370  	crashk_low_res.end   = low_base + low_size - 1;
6bee83d29d2e09 Baoquan He   2023-08-29  371  	insert_resource(&iomem_resource, &crashk_low_res);
6bee83d29d2e09 Baoquan He   2023-08-29  372  #endif
6bee83d29d2e09 Baoquan He   2023-08-29  373  	return 0;
6bee83d29d2e09 Baoquan He   2023-08-29  374  }
6bee83d29d2e09 Baoquan He   2023-08-29  375  
6bee83d29d2e09 Baoquan He   2023-08-29  376  void __init reserve_crashkernel_generic(char *cmdline,
6bee83d29d2e09 Baoquan He   2023-08-29  377  			     unsigned long long crash_size,
6bee83d29d2e09 Baoquan He   2023-08-29  378  			     unsigned long long crash_base,
6bee83d29d2e09 Baoquan He   2023-08-29  379  			     unsigned long long crash_low_size,
6bee83d29d2e09 Baoquan He   2023-08-29  380  			     bool high)
6bee83d29d2e09 Baoquan He   2023-08-29  381  {
6bee83d29d2e09 Baoquan He   2023-08-29  382  	unsigned long long search_end = CRASH_ADDR_LOW_MAX, search_base = 0;
6bee83d29d2e09 Baoquan He   2023-08-29  383  	bool fixed_base = false;
6bee83d29d2e09 Baoquan He   2023-08-29  384  
6bee83d29d2e09 Baoquan He   2023-08-29  385  	/* User specifies base address explicitly. */
6bee83d29d2e09 Baoquan He   2023-08-29  386  	if (crash_base) {
6bee83d29d2e09 Baoquan He   2023-08-29  387  		fixed_base = true;
6bee83d29d2e09 Baoquan He   2023-08-29  388  		search_base = crash_base;
6bee83d29d2e09 Baoquan He   2023-08-29  389  		search_end = crash_base + crash_size;
6bee83d29d2e09 Baoquan He   2023-08-29  390  	}
6bee83d29d2e09 Baoquan He   2023-08-29  391  
6bee83d29d2e09 Baoquan He   2023-08-29  392  	if (high) {
6bee83d29d2e09 Baoquan He   2023-08-29  393  		search_base = CRASH_ADDR_LOW_MAX;
6bee83d29d2e09 Baoquan He   2023-08-29  394  		search_end = CRASH_ADDR_HIGH_MAX;
6bee83d29d2e09 Baoquan He   2023-08-29  395  	}
6bee83d29d2e09 Baoquan He   2023-08-29  396  
6bee83d29d2e09 Baoquan He   2023-08-29  397  retry:
6bee83d29d2e09 Baoquan He   2023-08-29  398  	crash_base = memblock_phys_alloc_range(crash_size, CRASH_ALIGN,
6bee83d29d2e09 Baoquan He   2023-08-29  399  					       search_base, search_end);
6bee83d29d2e09 Baoquan He   2023-08-29  400  	if (!crash_base) {
6bee83d29d2e09 Baoquan He   2023-08-29  401  		/*
6bee83d29d2e09 Baoquan He   2023-08-29  402  		 * For crashkernel=size[KMG]@offset[KMG], print out failure
6bee83d29d2e09 Baoquan He   2023-08-29  403  		 * message if can't reserve the specified region.
6bee83d29d2e09 Baoquan He   2023-08-29  404  		 */
6bee83d29d2e09 Baoquan He   2023-08-29  405  		if (fixed_base) {
6bee83d29d2e09 Baoquan He   2023-08-29  406  			pr_warn("crashkernel reservation failed - memory is in use.\n");
6bee83d29d2e09 Baoquan He   2023-08-29  407  			return;
6bee83d29d2e09 Baoquan He   2023-08-29  408  		}
6bee83d29d2e09 Baoquan He   2023-08-29  409  
6bee83d29d2e09 Baoquan He   2023-08-29  410  		/*
6bee83d29d2e09 Baoquan He   2023-08-29  411  		 * For crashkernel=size[KMG], if the first attempt was for
6bee83d29d2e09 Baoquan He   2023-08-29  412  		 * low memory, fall back to high memory, the minimum required
6bee83d29d2e09 Baoquan He   2023-08-29  413  		 * low memory will be reserved later.
6bee83d29d2e09 Baoquan He   2023-08-29  414  		 */
6bee83d29d2e09 Baoquan He   2023-08-29  415  		if (!high && search_end == CRASH_ADDR_LOW_MAX) {
6bee83d29d2e09 Baoquan He   2023-08-29  416  			search_end = CRASH_ADDR_HIGH_MAX;
6bee83d29d2e09 Baoquan He   2023-08-29  417  			search_base = CRASH_ADDR_LOW_MAX;
6bee83d29d2e09 Baoquan He   2023-08-29  418  			crash_low_size = DEFAULT_CRASH_KERNEL_LOW_SIZE;
6bee83d29d2e09 Baoquan He   2023-08-29  419  			goto retry;
6bee83d29d2e09 Baoquan He   2023-08-29  420  		}
6bee83d29d2e09 Baoquan He   2023-08-29  421  
6bee83d29d2e09 Baoquan He   2023-08-29  422  		/*
6bee83d29d2e09 Baoquan He   2023-08-29  423  		 * For crashkernel=size[KMG],high, if the first attempt was
6bee83d29d2e09 Baoquan He   2023-08-29  424  		 * for high memory, fall back to low memory.
6bee83d29d2e09 Baoquan He   2023-08-29  425  		 */
6bee83d29d2e09 Baoquan He   2023-08-29  426  		if (high && search_end == CRASH_ADDR_HIGH_MAX) {
6bee83d29d2e09 Baoquan He   2023-08-29  427  			search_end = CRASH_ADDR_LOW_MAX;
6bee83d29d2e09 Baoquan He   2023-08-29  428  			search_base = 0;
6bee83d29d2e09 Baoquan He   2023-08-29  429  			goto retry;
6bee83d29d2e09 Baoquan He   2023-08-29  430  		}
6bee83d29d2e09 Baoquan He   2023-08-29  431  		pr_warn("cannot allocate crashkernel (size:0x%llx)\n",
6bee83d29d2e09 Baoquan He   2023-08-29  432  			crash_size);
6bee83d29d2e09 Baoquan He   2023-08-29  433  		return;
6bee83d29d2e09 Baoquan He   2023-08-29  434  	}
6bee83d29d2e09 Baoquan He   2023-08-29  435  
6bee83d29d2e09 Baoquan He   2023-08-29  436  	if ((crash_base > CRASH_ADDR_LOW_MAX) &&
6bee83d29d2e09 Baoquan He   2023-08-29  437  	     crash_low_size && reserve_crashkernel_low(crash_low_size)) {
6bee83d29d2e09 Baoquan He   2023-08-29  438  		memblock_phys_free(crash_base, crash_size);
6bee83d29d2e09 Baoquan He   2023-08-29  439  		return;
6bee83d29d2e09 Baoquan He   2023-08-29  440  	}
6bee83d29d2e09 Baoquan He   2023-08-29  441  
6bee83d29d2e09 Baoquan He   2023-08-29  442  	pr_info("crashkernel reserved: 0x%016llx - 0x%016llx (%lld MB)\n",
6bee83d29d2e09 Baoquan He   2023-08-29  443  		crash_base, crash_base + crash_size, crash_size >> 20);
6bee83d29d2e09 Baoquan He   2023-08-29  444  
6bee83d29d2e09 Baoquan He   2023-08-29  445  	/*
6bee83d29d2e09 Baoquan He   2023-08-29  446  	 * The crashkernel memory will be removed from the kernel linear
6bee83d29d2e09 Baoquan He   2023-08-29  447  	 * map. Inform kmemleak so that it won't try to access it.
6bee83d29d2e09 Baoquan He   2023-08-29  448  	 */
6bee83d29d2e09 Baoquan He   2023-08-29  449  	kmemleak_ignore_phys(crash_base);
6bee83d29d2e09 Baoquan He   2023-08-29  450  	if (crashk_low_res.end)
6bee83d29d2e09 Baoquan He   2023-08-29  451  		kmemleak_ignore_phys(crashk_low_res.start);
6bee83d29d2e09 Baoquan He   2023-08-29  452  
6bee83d29d2e09 Baoquan He   2023-08-29 @453  	crashk_res.start = crash_base;
6bee83d29d2e09 Baoquan He   2023-08-29  454  	crashk_res.end = crash_base + crash_size - 1;
6bee83d29d2e09 Baoquan He   2023-08-29  455  	insert_resource(&iomem_resource, &crashk_res);
6bee83d29d2e09 Baoquan He   2023-08-29  456  }
6bee83d29d2e09 Baoquan He   2023-08-29  457  #endif
6bee83d29d2e09 Baoquan He   2023-08-29  458
Baoquan He Aug. 30, 2023, 11:39 a.m. UTC | #3
On 08/30/23 at 09:49am, kernel test robot wrote:
> Hi Baoquan,
> 
> kernel test robot noticed the following build errors:
> 
> [auto build test ERROR on arm64/for-next/core]
> [also build test ERROR on tip/x86/core powerpc/next powerpc/fixes v6.5]
> [cannot apply to linus/master next-20230829]
> [If your patch is applied to the wrong git tree, kindly drop us a note.
> And when submitting patch, we suggest to use '--base' as documented in
> https://git-scm.com/docs/git-format-patch#_base_tree_information]
> 
> url:    https://github.com/intel-lab-lkp/linux/commits/Baoquan-He/crash_core-c-remove-unnecessary-parameter-of-function/20230829-201942
> base:   https://git.kernel.org/pub/scm/linux/kernel/git/arm64/linux.git for-next/core
> patch link:    https://lore.kernel.org/r/20230829121610.138107-7-bhe%40redhat.com
> patch subject: [PATCH v2 6/8] x86: kdump: use generic interface to simplify crashkernel reservation code
> config: x86_64-randconfig-r022-20230830 (https://download.01.org/0day-ci/archive/20230830/202308300910.e0i4piJT-lkp@intel.com/config)
> compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
> reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20230830/202308300910.e0i4piJT-lkp@intel.com/reproduce)
> 
> If you fix the issue in a separate patch/commit (i.e. not just a new version of
> the same patch/commit), kindly add following tags
> | Reported-by: kernel test robot <lkp@intel.com>
> | Closes: https://lore.kernel.org/oe-kbuild-all/202308300910.e0i4piJT-lkp@intel.com/
> 
> All errors (new ones prefixed by >>):
> 
>    ld: vmlinux.o: in function `reserve_crashkernel_low':
>    kernel/crash_core.c:369: undefined reference to `crashk_low_res'
>    ld: kernel/crash_core.c:369: undefined reference to `crashk_low_res'
>    ld: kernel/crash_core.c:370: undefined reference to `crashk_low_res'
>    ld: kernel/crash_core.c:369: undefined reference to `crashk_low_res'
>    ld: kernel/crash_core.c:370: undefined reference to `crashk_low_res'
>    ld: vmlinux.o:kernel/crash_core.c:371: more undefined references to `crashk_low_res' follow
>    ld: vmlinux.o: in function `reserve_crashkernel_generic':
> >> kernel/crash_core.c:453: undefined reference to `crashk_res'
> >> ld: kernel/crash_core.c:453: undefined reference to `crashk_res'
>    ld: kernel/crash_core.c:454: undefined reference to `crashk_res'
> >> ld: kernel/crash_core.c:453: undefined reference to `crashk_res'
>    ld: kernel/crash_core.c:454: undefined reference to `crashk_res'
>    ld: vmlinux.o:kernel/crash_core.c:454: more undefined references to `crashk_res' follow

Thanks for reporting. This one has the same root cause as the i386-randconfig
building. It's because crashk_res|crashk_low_res are defined in
kernel/kexec_core.c, but referenced in generic reservation code in
crash_core.c, while in this lkp's randconfig, CONFIG_KEXEC_CORE is
unset, CONFIG_CRASH_CORE=on. Then undefined reference to `crashk_res'
and `crashk_low_res' are reported.

Below patch can fix it. I will post v3 to contain this.

From 5a5bda3b5b1e370b789489d87516c8f1fb966c46 Mon Sep 17 00:00:00 2001
From: Baoquan He <bhe@redhat.com>
Date: Tue, 29 Aug 2023 22:38:15 -0400
Subject: [PATCH] crash_core: move crashk_res and crashk_low_res definition
 into crash_core.c
Content-type: text/plain

Both crashk_res and crashk_low_res are used to mark the reserved
crashkernel regions in iomem_resource tree. And later the generic
crashkernel resrvation which references them will be added into
crash_core.c. So move crashk_res and crashk_low_res definition into
crash_core.c to avoid compiling error when CONFIG_CRASH_CORE=on while
CONFIG_KEXEC_CORE is unset.

Signed-off-by: Baoquan He <bhe@redhat.com>
---
 include/linux/crash_core.h |  5 +++++
 include/linux/kexec.h      |  4 ----
 kernel/crash_core.c        | 16 ++++++++++++++++
 kernel/kexec_core.c        | 17 -----------------
 4 files changed, 21 insertions(+), 21 deletions(-)

diff --git a/include/linux/crash_core.h b/include/linux/crash_core.h
index d64006c4bd43..daa87b8730d3 100644
--- a/include/linux/crash_core.h
+++ b/include/linux/crash_core.h
@@ -9,6 +9,11 @@
 #include <asm/crash_core.h>
 #endif
 
+/* Location of a reserved region to hold the crash kernel.
+ */
+extern struct resource crashk_res;
+extern struct resource crashk_low_res;
+
 #define CRASH_CORE_NOTE_NAME	   "CORE"
 #define CRASH_CORE_NOTE_HEAD_BYTES ALIGN(sizeof(struct elf_note), 4)
 #define CRASH_CORE_NOTE_NAME_BYTES ALIGN(sizeof(CRASH_CORE_NOTE_NAME), 4)
diff --git a/include/linux/kexec.h b/include/linux/kexec.h
index 22b5cd24f581..e762b0435c39 100644
--- a/include/linux/kexec.h
+++ b/include/linux/kexec.h
@@ -22,10 +22,6 @@
 #include <uapi/linux/kexec.h>
 #include <linux/verification.h>
 
-/* Location of a reserved region to hold the crash kernel.
- */
-extern struct resource crashk_res;
-extern struct resource crashk_low_res;
 extern note_buf_t __percpu *crash_notes;
 
 #ifdef CONFIG_KEXEC_CORE
diff --git a/kernel/crash_core.c b/kernel/crash_core.c
index 61a8ea3b23a2..d76e7280c651 100644
--- a/kernel/crash_core.c
+++ b/kernel/crash_core.c
@@ -28,6 +28,22 @@ u32 *vmcoreinfo_note;
 /* trusted vmcoreinfo, e.g. we can make a copy in the crash memory */
 static unsigned char *vmcoreinfo_data_safecopy;
 
+/* Location of the reserved area for the crash kernel */
+struct resource crashk_res = {
+	.name  = "Crash kernel",
+	.start = 0,
+	.end   = 0,
+	.flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM,
+	.desc  = IORES_DESC_CRASH_KERNEL
+};
+struct resource crashk_low_res = {
+	.name  = "Crash kernel",
+	.start = 0,
+	.end   = 0,
+	.flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM,
+	.desc  = IORES_DESC_CRASH_KERNEL
+};
+
 /*
  * parsing the "crashkernel" commandline
  *
diff --git a/kernel/kexec_core.c b/kernel/kexec_core.c
index e2f2574d8b74..03ee65546df6 100644
--- a/kernel/kexec_core.c
+++ b/kernel/kexec_core.c
@@ -55,23 +55,6 @@ note_buf_t __percpu *crash_notes;
 /* Flag to indicate we are going to kexec a new kernel */
 bool kexec_in_progress = false;
 
-
-/* Location of the reserved area for the crash kernel */
-struct resource crashk_res = {
-	.name  = "Crash kernel",
-	.start = 0,
-	.end   = 0,
-	.flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM,
-	.desc  = IORES_DESC_CRASH_KERNEL
-};
-struct resource crashk_low_res = {
-	.name  = "Crash kernel",
-	.start = 0,
-	.end   = 0,
-	.flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM,
-	.desc  = IORES_DESC_CRASH_KERNEL
-};
-
 int kexec_should_crash(struct task_struct *p)
 {
 	/*
Leizhen (ThunderTown) Aug. 31, 2023, 3:43 a.m. UTC | #4
On 2023/8/29 20:16, Baoquan He wrote:
> 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(), and do the ARCH specific work if
>    needed.
> 
> 3) Add ARCH_HAS_GENERIC_CRASHKERNEL_RESERVATION Kconfig in
>    arch/x86/Kconfig.
> 
> When adding DEFAULT_CRASH_KERNEL_LOW_SIZE, add crash_low_size_default()
> to calculate crashkernel low memory because x86_64 has special
> requirement.
> 
> The old reserve_crashkernel_low() and reserve_crashkernel() can be
> removed.
> 
> Signed-off-by: Baoquan He <bhe@redhat.com>
> ---
>  arch/x86/Kconfig                  |   3 +
>  arch/x86/include/asm/crash_core.h |  34 +++++++
>  arch/x86/kernel/setup.c           | 144 ++++--------------------------
>  3 files changed, 53 insertions(+), 128 deletions(-)
>  create mode 100644 arch/x86/include/asm/crash_core.h
> 
> diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig
> index 8d9e4b362572..c4539dc35985 100644
> --- a/arch/x86/Kconfig
> +++ b/arch/x86/Kconfig
> @@ -2037,6 +2037,9 @@ config KEXEC_FILE
>  config ARCH_HAS_KEXEC_PURGATORY
>  	def_bool KEXEC_FILE
>  
> +config ARCH_HAS_GENERIC_CRASHKERNEL_RESERVATION
> +	def_bool CRASH_CORE
> +
>  config KEXEC_SIG
>  	bool "Verify kernel signature during kexec_file_load() syscall"
>  	depends on KEXEC_FILE
> diff --git a/arch/x86/include/asm/crash_core.h b/arch/x86/include/asm/crash_core.h
> new file mode 100644
> index 000000000000..5fc5e4f94521
> --- /dev/null
> +++ b/arch/x86/include/asm/crash_core.h
> @@ -0,0 +1,34 @@
> +/* SPDX-License-Identifier: GPL-2.0 */
> +#ifndef _X86_CRASH_CORE_H
> +#define _X86_CRASH_CORE_H
> +
> +/* 16M alignment for crash kernel regions */
> +#define CRASH_ALIGN             SZ_16M
> +
> +/*
> + * Keep the crash kernel below this limit.
> + *
> + * Earlier 32-bits kernels would limit the kernel to the low 512 MB range
> + * due to mapping restrictions.
> + *
> + * 64-bit kdump kernels need to be restricted to be under 64 TB, which is
> + * the upper limit of system RAM in 4-level paging mode. Since the kdump
> + * jump could be from 5-level paging to 4-level paging, the jump will fail if
> + * the kernel is put above 64 TB, and during the 1st kernel bootup there's
> + * no good way to detect the paging mode of the target kernel which will be
> + * loaded for dumping.
> + */
> +
> +#ifdef CONFIG_X86_32
> +# define CRASH_ADDR_LOW_MAX     SZ_512M
> +# define CRASH_ADDR_HIGH_MAX    SZ_512M
> +#else
> +# define CRASH_ADDR_LOW_MAX     SZ_4G
> +# define CRASH_ADDR_HIGH_MAX    SZ_64T
> +#endif
> +
> +# define DEFAULT_CRASH_KERNEL_LOW_SIZE crash_low_size_default()
> +
> +extern unsigned long crash_low_size_default(void);
> +
> +#endif /* _X86_CRASH_CORE_H */
> diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c
> index 382c66d2cf71..559a5c4141db 100644
> --- a/arch/x86/kernel/setup.c
> +++ b/arch/x86/kernel/setup.c
> @@ -474,152 +474,40 @@ static void __init memblock_x86_reserve_range_setup_data(void)
>  /*
>   * --------- Crashkernel reservation ------------------------------
>   */
> -
> -/* 16M alignment for crash kernel regions */
> -#define CRASH_ALIGN		SZ_16M
> -
> -/*
> - * Keep the crash kernel below this limit.
> - *
> - * Earlier 32-bits kernels would limit the kernel to the low 512 MB range
> - * due to mapping restrictions.
> - *
> - * 64-bit kdump kernels need to be restricted to be under 64 TB, which is
> - * the upper limit of system RAM in 4-level paging mode. Since the kdump
> - * jump could be from 5-level paging to 4-level paging, the jump will fail if
> - * the kernel is put above 64 TB, and during the 1st kernel bootup there's
> - * no good way to detect the paging mode of the target kernel which will be
> - * loaded for dumping.
> - */
> -#ifdef CONFIG_X86_32
> -# define CRASH_ADDR_LOW_MAX	SZ_512M
> -# define CRASH_ADDR_HIGH_MAX	SZ_512M
> -#else
> -# define CRASH_ADDR_LOW_MAX	SZ_4G
> -# define CRASH_ADDR_HIGH_MAX	SZ_64T
> -#endif
> -
> -static int __init reserve_crashkernel_low(void)
> +unsigned long crash_low_size_default(void)
>  {
>  #ifdef CONFIG_X86_64
> -	unsigned long long base, low_base = 0, low_size = 0;
> -	unsigned long low_mem_limit;
> -	int ret;
> -
> -	low_mem_limit = min(memblock_phys_mem_size(), CRASH_ADDR_LOW_MAX);
> -
> -	/* crashkernel=Y,low */
> -	ret = parse_crashkernel_low(boot_command_line, low_mem_limit, &low_size, &base);
> -	if (ret) {
> -		/*
> -		 * two parts from kernel/dma/swiotlb.c:
> -		 * -swiotlb size: user-specified with swiotlb= or default.
> -		 *
> -		 * -swiotlb overflow buffer: now hardcoded to 32k. We round it
> -		 * to 8M for other buffers that may need to stay low too. Also
> -		 * make sure we allocate enough extra low memory so that we
> -		 * don't run out of DMA buffers for 32-bit devices.
> -		 */
> -		low_size = max(swiotlb_size_or_default() + (8UL << 20), 256UL << 20);
> -	} else {
> -		/* passed with crashkernel=0,low ? */
> -		if (!low_size)
> -			return 0;
> -	}
> -
> -	low_base = memblock_phys_alloc_range(low_size, CRASH_ALIGN, 0, CRASH_ADDR_LOW_MAX);
> -	if (!low_base) {
> -		pr_err("Cannot reserve %ldMB crashkernel low memory, please try smaller size.\n",
> -		       (unsigned long)(low_size >> 20));
> -		return -ENOMEM;
> -	}
> -
> -	pr_info("Reserving %ldMB of low memory at %ldMB for crashkernel (low RAM limit: %ldMB)\n",
> -		(unsigned long)(low_size >> 20),
> -		(unsigned long)(low_base >> 20),
> -		(unsigned long)(low_mem_limit >> 20));
> -
> -	crashk_low_res.start = low_base;
> -	crashk_low_res.end   = low_base + low_size - 1;
> -	insert_resource(&iomem_resource, &crashk_low_res);
> -#endif
> +	return max(swiotlb_size_or_default() + (8UL << 20), 256UL << 20);
> +#else
>  	return 0;
> +#endif
>  }
>  
> -static void __init reserve_crashkernel(void)
> +static void __init arch_reserve_crashkernel(void)
>  {
> -	unsigned long long crash_size, crash_base, total_mem;
> +	unsigned long long crash_base, crash_size, low_size = 0;
> +	char *cmdline = boot_command_line;
>  	bool high = false;
>  	int ret;
>  
>  	if (!IS_ENABLED(CONFIG_KEXEC_CORE))
>  		return;
>  
> -	total_mem = memblock_phys_mem_size();
> -
> -	/* crashkernel=XM */
> -	ret = parse_crashkernel(boot_command_line, total_mem,
> -				&crash_size, &crash_base, NULL, NULL);
> -	if (ret != 0 || crash_size <= 0) {
> -		/* crashkernel=X,high */
> -		ret = parse_crashkernel_high(boot_command_line, total_mem,
> -					     &crash_size, &crash_base);
> -		if (ret != 0 || crash_size <= 0)
> -			return;
> -		high = true;
> -	}
> +	ret = parse_crashkernel(cmdline, memblock_phys_mem_size(),
> +				&crash_size, &crash_base,
> +				&low_size, &high);
> +	if (ret)
> +		return;
>  
>  	if (xen_pv_domain()) {
>  		pr_info("Ignoring crashkernel for a Xen PV domain\n");
>  		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_phys_alloc_range(crash_size,
> -						CRASH_ALIGN, CRASH_ALIGN,
> -						CRASH_ADDR_LOW_MAX);
> -		if (!crash_base)
> -			crash_base = memblock_phys_alloc_range(crash_size,
> -						CRASH_ALIGN, CRASH_ALIGN,
> -						CRASH_ADDR_HIGH_MAX);
> -		if (!crash_base) {
> -			pr_info("crashkernel reservation failed - No suitable area found.\n");
> -			return;
> -		}
> -	} else {
> -		unsigned long long start;
> -
> -		start = memblock_phys_alloc_range(crash_size, SZ_1M, crash_base,
> -						  crash_base + crash_size);
> -		if (start != crash_base) {
> -			pr_info("crashkernel reservation failed - memory is in use.\n");
> -			return;
> -		}
> -	}
> -
> -	if (crash_base >= (1ULL << 32) && reserve_crashkernel_low()) {
> -		memblock_phys_free(crash_base, crash_size);
> -		return;
> -	}
> -
> -	pr_info("Reserving %ldMB of memory at %ldMB for crashkernel (System RAM: %ldMB)\n",
> -		(unsigned long)(crash_size >> 20),
> -		(unsigned long)(crash_base >> 20),
> -		(unsigned long)(total_mem >> 20));
> +	reserve_crashkernel_generic(cmdline, crash_size, crash_base,
> +				    low_size, high);
>  
> -	crashk_res.start = crash_base;
> -	crashk_res.end   = crash_base + crash_size - 1;
> -	insert_resource(&iomem_resource, &crashk_res);
> +	return;

This can be omitted.

>  }
>  
>  static struct resource standard_io_resources[] = {
> @@ -1231,7 +1119,7 @@ void __init setup_arch(char **cmdline_p)
>  	 * Reserve memory for crash kernel after SRAT is parsed so that it
>  	 * won't consume hotpluggable memory.
>  	 */
> -	reserve_crashkernel();
> +	arch_reserve_crashkernel();
>  
>  	memblock_find_dma_reserve();
>  
>
Baoquan He Sept. 1, 2023, 10:10 a.m. UTC | #5
On 08/31/23 at 11:43am, Leizhen (ThunderTown) wrote:
......  
> > -static void __init reserve_crashkernel(void)
> > +static void __init arch_reserve_crashkernel(void)
> >  {
> > -	unsigned long long crash_size, crash_base, total_mem;
> > +	unsigned long long crash_base, crash_size, low_size = 0;
> > +	char *cmdline = boot_command_line;
> >  	bool high = false;
> >  	int ret;
> >  
> >  	if (!IS_ENABLED(CONFIG_KEXEC_CORE))
> >  		return;
> >  
> > -	total_mem = memblock_phys_mem_size();
> > -
> > -	/* crashkernel=XM */
> > -	ret = parse_crashkernel(boot_command_line, total_mem,
> > -				&crash_size, &crash_base, NULL, NULL);
> > -	if (ret != 0 || crash_size <= 0) {
> > -		/* crashkernel=X,high */
> > -		ret = parse_crashkernel_high(boot_command_line, total_mem,
> > -					     &crash_size, &crash_base);
> > -		if (ret != 0 || crash_size <= 0)
> > -			return;
> > -		high = true;
> > -	}
> > +	ret = parse_crashkernel(cmdline, memblock_phys_mem_size(),
> > +				&crash_size, &crash_base,
> > +				&low_size, &high);
> > +	if (ret)
> > +		return;
> >  
> >  	if (xen_pv_domain()) {
> >  		pr_info("Ignoring crashkernel for a Xen PV domain\n");
> >  		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_phys_alloc_range(crash_size,
> > -						CRASH_ALIGN, CRASH_ALIGN,
> > -						CRASH_ADDR_LOW_MAX);
> > -		if (!crash_base)
> > -			crash_base = memblock_phys_alloc_range(crash_size,
> > -						CRASH_ALIGN, CRASH_ALIGN,
> > -						CRASH_ADDR_HIGH_MAX);
> > -		if (!crash_base) {
> > -			pr_info("crashkernel reservation failed - No suitable area found.\n");
> > -			return;
> > -		}
> > -	} else {
> > -		unsigned long long start;
> > -
> > -		start = memblock_phys_alloc_range(crash_size, SZ_1M, crash_base,
> > -						  crash_base + crash_size);
> > -		if (start != crash_base) {
> > -			pr_info("crashkernel reservation failed - memory is in use.\n");
> > -			return;
> > -		}
> > -	}
> > -
> > -	if (crash_base >= (1ULL << 32) && reserve_crashkernel_low()) {
> > -		memblock_phys_free(crash_base, crash_size);
> > -		return;
> > -	}
> > -
> > -	pr_info("Reserving %ldMB of memory at %ldMB for crashkernel (System RAM: %ldMB)\n",
> > -		(unsigned long)(crash_size >> 20),
> > -		(unsigned long)(crash_base >> 20),
> > -		(unsigned long)(total_mem >> 20));
> > +	reserve_crashkernel_generic(cmdline, crash_size, crash_base,
> > +				    low_size, high);
> >  
> > -	crashk_res.start = crash_base;
> > -	crashk_res.end   = crash_base + crash_size - 1;
> > -	insert_resource(&iomem_resource, &crashk_res);
> > +	return;
> 
> This can be omitted.

Will update, thx.

> 
> >  }
......
diff mbox series

Patch

diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig
index 8d9e4b362572..c4539dc35985 100644
--- a/arch/x86/Kconfig
+++ b/arch/x86/Kconfig
@@ -2037,6 +2037,9 @@  config KEXEC_FILE
 config ARCH_HAS_KEXEC_PURGATORY
 	def_bool KEXEC_FILE
 
+config ARCH_HAS_GENERIC_CRASHKERNEL_RESERVATION
+	def_bool CRASH_CORE
+
 config KEXEC_SIG
 	bool "Verify kernel signature during kexec_file_load() syscall"
 	depends on KEXEC_FILE
diff --git a/arch/x86/include/asm/crash_core.h b/arch/x86/include/asm/crash_core.h
new file mode 100644
index 000000000000..5fc5e4f94521
--- /dev/null
+++ b/arch/x86/include/asm/crash_core.h
@@ -0,0 +1,34 @@ 
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _X86_CRASH_CORE_H
+#define _X86_CRASH_CORE_H
+
+/* 16M alignment for crash kernel regions */
+#define CRASH_ALIGN             SZ_16M
+
+/*
+ * Keep the crash kernel below this limit.
+ *
+ * Earlier 32-bits kernels would limit the kernel to the low 512 MB range
+ * due to mapping restrictions.
+ *
+ * 64-bit kdump kernels need to be restricted to be under 64 TB, which is
+ * the upper limit of system RAM in 4-level paging mode. Since the kdump
+ * jump could be from 5-level paging to 4-level paging, the jump will fail if
+ * the kernel is put above 64 TB, and during the 1st kernel bootup there's
+ * no good way to detect the paging mode of the target kernel which will be
+ * loaded for dumping.
+ */
+
+#ifdef CONFIG_X86_32
+# define CRASH_ADDR_LOW_MAX     SZ_512M
+# define CRASH_ADDR_HIGH_MAX    SZ_512M
+#else
+# define CRASH_ADDR_LOW_MAX     SZ_4G
+# define CRASH_ADDR_HIGH_MAX    SZ_64T
+#endif
+
+# define DEFAULT_CRASH_KERNEL_LOW_SIZE crash_low_size_default()
+
+extern unsigned long crash_low_size_default(void);
+
+#endif /* _X86_CRASH_CORE_H */
diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c
index 382c66d2cf71..559a5c4141db 100644
--- a/arch/x86/kernel/setup.c
+++ b/arch/x86/kernel/setup.c
@@ -474,152 +474,40 @@  static void __init memblock_x86_reserve_range_setup_data(void)
 /*
  * --------- Crashkernel reservation ------------------------------
  */
-
-/* 16M alignment for crash kernel regions */
-#define CRASH_ALIGN		SZ_16M
-
-/*
- * Keep the crash kernel below this limit.
- *
- * Earlier 32-bits kernels would limit the kernel to the low 512 MB range
- * due to mapping restrictions.
- *
- * 64-bit kdump kernels need to be restricted to be under 64 TB, which is
- * the upper limit of system RAM in 4-level paging mode. Since the kdump
- * jump could be from 5-level paging to 4-level paging, the jump will fail if
- * the kernel is put above 64 TB, and during the 1st kernel bootup there's
- * no good way to detect the paging mode of the target kernel which will be
- * loaded for dumping.
- */
-#ifdef CONFIG_X86_32
-# define CRASH_ADDR_LOW_MAX	SZ_512M
-# define CRASH_ADDR_HIGH_MAX	SZ_512M
-#else
-# define CRASH_ADDR_LOW_MAX	SZ_4G
-# define CRASH_ADDR_HIGH_MAX	SZ_64T
-#endif
-
-static int __init reserve_crashkernel_low(void)
+unsigned long crash_low_size_default(void)
 {
 #ifdef CONFIG_X86_64
-	unsigned long long base, low_base = 0, low_size = 0;
-	unsigned long low_mem_limit;
-	int ret;
-
-	low_mem_limit = min(memblock_phys_mem_size(), CRASH_ADDR_LOW_MAX);
-
-	/* crashkernel=Y,low */
-	ret = parse_crashkernel_low(boot_command_line, low_mem_limit, &low_size, &base);
-	if (ret) {
-		/*
-		 * two parts from kernel/dma/swiotlb.c:
-		 * -swiotlb size: user-specified with swiotlb= or default.
-		 *
-		 * -swiotlb overflow buffer: now hardcoded to 32k. We round it
-		 * to 8M for other buffers that may need to stay low too. Also
-		 * make sure we allocate enough extra low memory so that we
-		 * don't run out of DMA buffers for 32-bit devices.
-		 */
-		low_size = max(swiotlb_size_or_default() + (8UL << 20), 256UL << 20);
-	} else {
-		/* passed with crashkernel=0,low ? */
-		if (!low_size)
-			return 0;
-	}
-
-	low_base = memblock_phys_alloc_range(low_size, CRASH_ALIGN, 0, CRASH_ADDR_LOW_MAX);
-	if (!low_base) {
-		pr_err("Cannot reserve %ldMB crashkernel low memory, please try smaller size.\n",
-		       (unsigned long)(low_size >> 20));
-		return -ENOMEM;
-	}
-
-	pr_info("Reserving %ldMB of low memory at %ldMB for crashkernel (low RAM limit: %ldMB)\n",
-		(unsigned long)(low_size >> 20),
-		(unsigned long)(low_base >> 20),
-		(unsigned long)(low_mem_limit >> 20));
-
-	crashk_low_res.start = low_base;
-	crashk_low_res.end   = low_base + low_size - 1;
-	insert_resource(&iomem_resource, &crashk_low_res);
-#endif
+	return max(swiotlb_size_or_default() + (8UL << 20), 256UL << 20);
+#else
 	return 0;
+#endif
 }
 
-static void __init reserve_crashkernel(void)
+static void __init arch_reserve_crashkernel(void)
 {
-	unsigned long long crash_size, crash_base, total_mem;
+	unsigned long long crash_base, crash_size, low_size = 0;
+	char *cmdline = boot_command_line;
 	bool high = false;
 	int ret;
 
 	if (!IS_ENABLED(CONFIG_KEXEC_CORE))
 		return;
 
-	total_mem = memblock_phys_mem_size();
-
-	/* crashkernel=XM */
-	ret = parse_crashkernel(boot_command_line, total_mem,
-				&crash_size, &crash_base, NULL, NULL);
-	if (ret != 0 || crash_size <= 0) {
-		/* crashkernel=X,high */
-		ret = parse_crashkernel_high(boot_command_line, total_mem,
-					     &crash_size, &crash_base);
-		if (ret != 0 || crash_size <= 0)
-			return;
-		high = true;
-	}
+	ret = parse_crashkernel(cmdline, memblock_phys_mem_size(),
+				&crash_size, &crash_base,
+				&low_size, &high);
+	if (ret)
+		return;
 
 	if (xen_pv_domain()) {
 		pr_info("Ignoring crashkernel for a Xen PV domain\n");
 		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_phys_alloc_range(crash_size,
-						CRASH_ALIGN, CRASH_ALIGN,
-						CRASH_ADDR_LOW_MAX);
-		if (!crash_base)
-			crash_base = memblock_phys_alloc_range(crash_size,
-						CRASH_ALIGN, CRASH_ALIGN,
-						CRASH_ADDR_HIGH_MAX);
-		if (!crash_base) {
-			pr_info("crashkernel reservation failed - No suitable area found.\n");
-			return;
-		}
-	} else {
-		unsigned long long start;
-
-		start = memblock_phys_alloc_range(crash_size, SZ_1M, crash_base,
-						  crash_base + crash_size);
-		if (start != crash_base) {
-			pr_info("crashkernel reservation failed - memory is in use.\n");
-			return;
-		}
-	}
-
-	if (crash_base >= (1ULL << 32) && reserve_crashkernel_low()) {
-		memblock_phys_free(crash_base, crash_size);
-		return;
-	}
-
-	pr_info("Reserving %ldMB of memory at %ldMB for crashkernel (System RAM: %ldMB)\n",
-		(unsigned long)(crash_size >> 20),
-		(unsigned long)(crash_base >> 20),
-		(unsigned long)(total_mem >> 20));
+	reserve_crashkernel_generic(cmdline, crash_size, crash_base,
+				    low_size, high);
 
-	crashk_res.start = crash_base;
-	crashk_res.end   = crash_base + crash_size - 1;
-	insert_resource(&iomem_resource, &crashk_res);
+	return;
 }
 
 static struct resource standard_io_resources[] = {
@@ -1231,7 +1119,7 @@  void __init setup_arch(char **cmdline_p)
 	 * Reserve memory for crash kernel after SRAT is parsed so that it
 	 * won't consume hotpluggable memory.
 	 */
-	reserve_crashkernel();
+	arch_reserve_crashkernel();
 
 	memblock_find_dma_reserve();