linuxppc-dev.lists.ozlabs.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v17 0/6] powerpc/crash: Kernel handling of CPU and memory hotplug
@ 2024-02-26  8:41 Sourabh Jain
  2024-02-26  8:41 ` [PATCH v17 1/6] crash: forward memory_notify arg to arch crash hotplug handler Sourabh Jain
                   ` (6 more replies)
  0 siblings, 7 replies; 18+ messages in thread
From: Sourabh Jain @ 2024-02-26  8:41 UTC (permalink / raw)
  To: linuxppc-dev
  Cc: David Hildenbrand, Dave Hansen, Mimi Zohar, Boris Ostrovsky,
	Valentin Schneider, Baoquan He, x86, Aneesh Kumar K . V,
	Laurent Dufour, Dave Young, Vivek Goyal, Naveen N Rao,
	Borislav Petkov, Thomas Gleixner, Hari Bathini, Oscar Salvador,
	Greg Kroah-Hartman, kexec, Mahesh Salgaonkar, Sourabh Jain,
	Akhil Raj, Andrew Morton

Commit 247262756121 ("crash: add generic infrastructure for crash
hotplug support") added a generic infrastructure that allows
architectures to selectively update the kdump image component during CPU
or memory add/remove events within the kernel itself.

This patch series adds crash hotplug handler for PowerPC and enable
support to update the kdump image on CPU/Memory add/remove events.

Among the 5 patches in this series, the first two patches make changes
to the generic crash hotplug handler to assist PowerPC in adding support
for this feature. The last three patches add support for this feature.

The following section outlines the problem addressed by this patch
series, along with the current solution, its shortcomings, and the
proposed resolution.

Problem:
========
Due to CPU/Memory hotplug or online/offline events the elfcorehdr
(which describes the CPUs and memory of the crashed kernel) and FDT
(Flattened Device Tree) of kdump image becomes outdated. Consequently,
attempting dump collection with an outdated elfcorehdr or FDT can lead
to failed or inaccurate dump collection.

Going forward CPU hotplug or online/offline events are referred as
CPU/Memory add/remove events.

Existing solution and its shortcoming:
======================================
The current solution to address the above issue involves monitoring the
CPU/memory add/remove events in userspace using udev rules and whenever
there are changes in CPU and memory resources, the entire kdump image
is loaded again. The kdump image includes kernel, initrd, elfcorehdr,
FDT, purgatory. Given that only elfcorehdr and FDT get outdated due to
CPU/Memory add/remove events, reloading the entire kdump image is
inefficient. More importantly, kdump remains inactive for a substantial
amount of time until the kdump reload completes.

Proposed solution:
==================
Instead of initiating a full kdump image reload from userspace on
CPU/Memory hotplug and online/offline events, the proposed solution aims
to update only the necessary kdump image component within the kernel
itself.

Git tree for testing:
=====================
https://github.com/sourabhjains/linux/tree/kdump-in-kernel-crash-update-v17

Above tree is rebased on top of linux-next and the below patch series:
https://lore.kernel.org/all/20240213113150.1148276-1-hbathini@linux.ibm.com/

To realize this feature, the kdump udev rule must be updated. On RHEL,
add the following two lines at the top of the
"/usr/lib/udev/rules.d/98-kexec.rules" file.

SUBSYSTEM=="cpu", ATTRS{crash_hotplug}=="1", GOTO="kdump_reload_end"
SUBSYSTEM=="memory", ATTRS{crash_hotplug}=="1", GOTO="kdump_reload_end"

With the above change to the kdump udev rule, kdump reload is avoided
during CPU/Memory add/remove events if this feature is enabled in the
kernel.

Note: only kexec_file_load syscall will work. For kexec_load minor changes
are required in kexec tool.

Changelog:
----------
v17:
  - Rebase the patch series on top linux-next tree and below patch series
    https://lore.kernel.org/all/20240213113150.1148276-1-hbathini@linux.ibm.com/
  - Split 0003 patch from v16 into two patches
       1. Move get_crash_memory_ranges() along with other *_memory_ranges()
          functions to ranges.c and make them public.
       2. Make update_cpus_node function public and take this function
          out of file_load_64.c
  - Keep arch_crash_hotplug_support in crash.c instead of core_64.c [05/06]
  - Use CONFIG_CRASH_MAX_MEMORY_RANGES to find extra elfcorehdr size [06/06]

v16: [https://lore.kernel.org/all/20240217081452.164571-1-sourabhjain@linux.ibm.com/]
  - Remove the unused #define `crash_hotplug_cpu_support`
    and `crash_hotplug_memory_support` in `arch/x86/include/asm/kexec.h`.
  - Document why two kexec flag bits are used in
    `arch_crash_hotplug_memory_support` (x86).
  - Use a switch case to handle different hotplug operations
    in `arch_crash_handle_hotplug_event` for PowerPC.
  - Fix a typo in 4/5.

v15:
  - Remove the patch that adds a new kexec flag for FDT update.
  - Introduce a generic kexec flag bit to share hotplug support
    intent between the kexec tool and the kernel for the kexec_load
    syscall. (2/5)
  - Introduce an architecture-specific handler to process the kexec
    flag for crash hotplug support. (2/5)
  - Rename the @update_elfcorehdr member of the struct kimage to
    @hotplug_support. (2/5)
  - Use a common function to advertise hotplug support for both CPU
    and Memory. (2/5)

v14:
  - Fix build warnings by including necessary header files
  - Rebase to v6.7-rc5

v13:
  - Fix a build warning, take ranges.c out of CONFIG_KEXEC_FILE
  - Rebase to v6.7-rc4

v12:
  - A patch to add new kexec flags to support this feature on kexec_load
    system call
  - Change in the way this feature is advertise to userspace for both
    kexec_load syscall
  - Rebase to v6.6-rc7

v11:
  - Rebase to v6.4-rc6
  - The patch that introduced CONFIG_CRASH_HOTPLUG for PowerPC has been
    removed. The config is now part of common configuration:
    https://lore.kernel.org/all/87ilbpflsk.fsf@mail.lhotse/

v10:
  - Drop the patch that adds fdt_index attribute to struct kimage_arch
    Find the fdt segment index when needed.
  - Added more details into commits messages.
  - Rebased onto 6.3.0-rc5

v9:
  - Removed patch to prepare elfcorehdr crash notes for possible CPUs.
    The patch is moved to generic patch series that introduces generic
    infrastructure for in kernel crash update.
  - Removed patch to pass the hotplug action type to the arch crash
    hotplug handler function. The generic patch series has introduced
    the hotplug action type in kimage struct.
  - Add detail commit message for better understanding.

v8:
  - Restrict fdt_index initialization to machine_kexec_post_load
    it work for both kexec_load and kexec_file_load.[3/8] Laurent Dufour

  - Updated the logic to find the number of offline core. [6/8]

  - Changed the logic to find the elfcore program header to accommodate
    future memory ranges due memory hotplug events. [8/8]

v7
  - added a new config to configure this feature
  - pass hotplug action type to arch specific handler

v6
  - Added crash memory hotplug support

v5:
  - Replace COFNIG_CRASH_HOTPLUG with CONFIG_HOTPLUG_CPU.
  - Move fdt segment identification for kexec_load case to load path
    instead of crash hotplug handler
  - Keep new attribute defined under kimage_arch to track FDT segment
    under CONFIG_HOTPLUG_CPU config.

v4:
  - Update the logic to find the additional space needed for hotadd CPUs
    post kexec load. Refer "[RFC v4 PATCH 4/5] powerpc/crash hp: add crash
    hotplug support for kexec_file_load" patch to know more about the
    change.
  - Fix a couple of typo.
  - Replace pr_err to pr_info_once to warn user about memory hotplug
    support.
  - In crash hotplug handle exit the for loop if FDT segment is found.

v3
  - Move fdt_index and fdt_index_vaild variables to kimage_arch struct.
  - Rebase patche on top of
    https://lore.kernel.org/lkml/20220303162725.49640-1-eric.devolder@oracle.com/
  - Fixed warning reported by checpatch script

v2:
  - Use generic hotplug handler introduced by
    https://lore.kernel.org/lkml/20220209195706.51522-1-eric.devolder@oracle.com/
    a significant change from v1.

Cc: Akhil Raj <lf32.dev@gmail.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Aneesh Kumar K.V <aneesh.kumar@kernel.org>
Cc: Baoquan He <bhe@redhat.com>
Cc: Borislav Petkov (AMD) <bp@alien8.de>
Cc: Boris Ostrovsky <boris.ostrovsky@oracle.com>
Cc: Christophe Leroy <christophe.leroy@csgroup.eu>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Cc: Dave Young <dyoung@redhat.com>
Cc: David Hildenbrand <david@redhat.com>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Hari Bathini <hbathini@linux.ibm.com>
Cc: Laurent Dufour <laurent.dufour@fr.ibm.com>
Cc: Mahesh Salgaonkar <mahesh@linux.ibm.com>
Cc: Michael Ellerman <mpe@ellerman.id.au>
Cc: Mimi Zohar <zohar@linux.ibm.com>
Cc: Naveen N Rao <naveen@kernel.org>
Cc: Oscar Salvador <osalvador@suse.de>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Valentin Schneider <vschneid@redhat.com>
Cc: Vivek Goyal <vgoyal@redhat.com>
Cc: kexec@lists.infradead.org
Cc: x86@kernel.org

Sourabh Jain (6):
  crash: forward memory_notify arg to arch crash hotplug handler
  crash: add a new kexec flag for hotplug support
  powerpc/kexec: move *_memory_ranges functions to ranges.c
  PowerPC/kexec: make the update_cpus_node() function public
  powerpc/crash: add crash CPU hotplug support
  powerpc/crash: add crash memory hotplug support

 arch/powerpc/Kconfig                    |   4 +
 arch/powerpc/include/asm/kexec.h        |  15 ++
 arch/powerpc/include/asm/kexec_ranges.h |  20 +-
 arch/powerpc/kexec/Makefile             |   4 +-
 arch/powerpc/kexec/core_64.c            |  91 +++++++
 arch/powerpc/kexec/crash.c              | 196 +++++++++++++++
 arch/powerpc/kexec/elf_64.c             |   3 +-
 arch/powerpc/kexec/file_load_64.c       | 314 +++---------------------
 arch/powerpc/kexec/ranges.c             | 312 ++++++++++++++++++++++-
 arch/x86/include/asm/kexec.h            |  13 +-
 arch/x86/kernel/crash.c                 |  32 ++-
 drivers/base/cpu.c                      |   2 +-
 drivers/base/memory.c                   |   2 +-
 include/linux/crash_core.h              |  15 +-
 include/linux/kexec.h                   |  11 +-
 include/uapi/linux/kexec.h              |   1 +
 kernel/crash_core.c                     |  25 +-
 kernel/kexec.c                          |   4 +-
 kernel/kexec_file.c                     |   5 +
 19 files changed, 712 insertions(+), 357 deletions(-)

-- 
2.43.0


^ permalink raw reply	[flat|nested] 18+ messages in thread
* Re: [PATCH v17 3/6] powerpc/kexec: move *_memory_ranges functions to ranges.c
@ 2024-03-02 13:18 Hari Bathini
  0 siblings, 0 replies; 18+ messages in thread
From: Hari Bathini @ 2024-03-02 13:18 UTC (permalink / raw)
  To: Sourabh Jain, linuxppc-dev
  Cc: Valentin Schneider, x86, Andrew Morton, Baoquan He,
	David Hildenbrand, Greg Kroah-Hartman, Dave Hansen, kexec,
	Naveen N Rao, Mimi Zohar, Mahesh Salgaonkar, Aneesh Kumar K . V,
	Laurent Dufour, Borislav Petkov, Akhil Raj, Boris Ostrovsky,
	Dave Young, Thomas Gleixner, Vivek Goyal, Oscar Salvador



On 26/02/24 2:11 pm, Sourabh Jain wrote:
> Move the following functions form kexec/{file_load_64.c => ranges.c} and
> make them public so that components other KEXEC_FILE can also use these
> functions.
> 1. get_exclude_memory_ranges
> 2. get_reserved_memory_ranges
> 3. get_crash_memory_ranges
> 4. get_usable_memory_ranges
> 
> Later in the series get_crash_memory_ranges function is utilized for
> in-kernel updates to kdump image during CPU/Memory hotplug or
> online/offline events for both kexec_load and kexec_file_load syscalls.
> 
> Since the above functions are moved to ranges.c, some of the helper
> functions in ranges.c are no longer required to be public. Mark them as
> static and removed them from kexec_ranges.h header file.
> 
> Finally, remove the CONFIG_KEXEC_FILE build dependency for range.c
> because it is required for other config, such as CONFIG_CRASH_DUMP.
> 
> No functional changes are intended.
> 

Acked-by: Hari Bathini <hbathini@linux.ibm.com>

> Signed-off-by: Sourabh Jain <sourabhjain@linux.ibm.com>
> Cc: Akhil Raj <lf32.dev@gmail.com>
> Cc: Andrew Morton <akpm@linux-foundation.org>
> Cc: Aneesh Kumar K.V <aneesh.kumar@kernel.org>
> Cc: Baoquan He <bhe@redhat.com>
> Cc: Borislav Petkov (AMD) <bp@alien8.de>
> Cc: Boris Ostrovsky <boris.ostrovsky@oracle.com>
> Cc: Christophe Leroy <christophe.leroy@csgroup.eu>
> Cc: Dave Hansen <dave.hansen@linux.intel.com>
> Cc: Dave Young <dyoung@redhat.com>
> Cc: David Hildenbrand <david@redhat.com>
> Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> Cc: Hari Bathini <hbathini@linux.ibm.com>
> Cc: Laurent Dufour <laurent.dufour@fr.ibm.com>
> Cc: Mahesh Salgaonkar <mahesh@linux.ibm.com>
> Cc: Michael Ellerman <mpe@ellerman.id.au>
> Cc: Mimi Zohar <zohar@linux.ibm.com>
> Cc: Naveen N Rao <naveen@kernel.org>
> Cc: Oscar Salvador <osalvador@suse.de>
> Cc: Thomas Gleixner <tglx@linutronix.de>
> Cc: Valentin Schneider <vschneid@redhat.com>
> Cc: Vivek Goyal <vgoyal@redhat.com>
> Cc: kexec@lists.infradead.org
> Cc: x86@kernel.org
> ---
>   arch/powerpc/include/asm/kexec_ranges.h |  19 +-
>   arch/powerpc/kexec/Makefile             |   4 +-
>   arch/powerpc/kexec/file_load_64.c       | 190 --------------------
>   arch/powerpc/kexec/ranges.c             | 227 +++++++++++++++++++++++-
>   4 files changed, 224 insertions(+), 216 deletions(-)
> 
> diff --git a/arch/powerpc/include/asm/kexec_ranges.h b/arch/powerpc/include/asm/kexec_ranges.h
> index f83866a19e87..8489e844b447 100644
> --- a/arch/powerpc/include/asm/kexec_ranges.h
> +++ b/arch/powerpc/include/asm/kexec_ranges.h
> @@ -7,19 +7,8 @@
>   void sort_memory_ranges(struct crash_mem *mrngs, bool merge);
>   struct crash_mem *realloc_mem_ranges(struct crash_mem **mem_ranges);
>   int add_mem_range(struct crash_mem **mem_ranges, u64 base, u64 size);
> -int add_tce_mem_ranges(struct crash_mem **mem_ranges);
> -int add_initrd_mem_range(struct crash_mem **mem_ranges);
> -#ifdef CONFIG_PPC_64S_HASH_MMU
> -int add_htab_mem_range(struct crash_mem **mem_ranges);
> -#else
> -static inline int add_htab_mem_range(struct crash_mem **mem_ranges)
> -{
> -	return 0;
> -}
> -#endif
> -int add_kernel_mem_range(struct crash_mem **mem_ranges);
> -int add_rtas_mem_range(struct crash_mem **mem_ranges);
> -int add_opal_mem_range(struct crash_mem **mem_ranges);
> -int add_reserved_mem_ranges(struct crash_mem **mem_ranges);
> -
> +int get_exclude_memory_ranges(struct crash_mem **mem_ranges);
> +int get_reserved_memory_ranges(struct crash_mem **mem_ranges);
> +int get_crash_memory_ranges(struct crash_mem **mem_ranges);
> +int get_usable_memory_ranges(struct crash_mem **mem_ranges);
>   #endif /* _ASM_POWERPC_KEXEC_RANGES_H */
> diff --git a/arch/powerpc/kexec/Makefile b/arch/powerpc/kexec/Makefile
> index 8e469c4da3f8..470eb0453e17 100644
> --- a/arch/powerpc/kexec/Makefile
> +++ b/arch/powerpc/kexec/Makefile
> @@ -3,11 +3,11 @@
>   # Makefile for the linux kernel.
>   #
>   
> -obj-y				+= core.o core_$(BITS).o
> +obj-y				+= core.o core_$(BITS).o ranges.o
>   
>   obj-$(CONFIG_PPC32)		+= relocate_32.o
>   
> -obj-$(CONFIG_KEXEC_FILE)	+= file_load.o ranges.o file_load_$(BITS).o elf_$(BITS).o
> +obj-$(CONFIG_KEXEC_FILE)	+= file_load.o file_load_$(BITS).o elf_$(BITS).o
>   obj-$(CONFIG_VMCORE_INFO)	+= vmcore_info.o
>   obj-$(CONFIG_CRASH_DUMP)	+= crash.o
>   
> diff --git a/arch/powerpc/kexec/file_load_64.c b/arch/powerpc/kexec/file_load_64.c
> index 1bc65de6174f..6a01f62b8fcf 100644
> --- a/arch/powerpc/kexec/file_load_64.c
> +++ b/arch/powerpc/kexec/file_load_64.c
> @@ -47,83 +47,6 @@ const struct kexec_file_ops * const kexec_file_loaders[] = {
>   	NULL
>   };
>   
> -/**
> - * get_exclude_memory_ranges - Get exclude memory ranges. This list includes
> - *                             regions like opal/rtas, tce-table, initrd,
> - *                             kernel, htab which should be avoided while
> - *                             setting up kexec load segments.
> - * @mem_ranges:                Range list to add the memory ranges to.
> - *
> - * Returns 0 on success, negative errno on error.
> - */
> -static int get_exclude_memory_ranges(struct crash_mem **mem_ranges)
> -{
> -	int ret;
> -
> -	ret = add_tce_mem_ranges(mem_ranges);
> -	if (ret)
> -		goto out;
> -
> -	ret = add_initrd_mem_range(mem_ranges);
> -	if (ret)
> -		goto out;
> -
> -	ret = add_htab_mem_range(mem_ranges);
> -	if (ret)
> -		goto out;
> -
> -	ret = add_kernel_mem_range(mem_ranges);
> -	if (ret)
> -		goto out;
> -
> -	ret = add_rtas_mem_range(mem_ranges);
> -	if (ret)
> -		goto out;
> -
> -	ret = add_opal_mem_range(mem_ranges);
> -	if (ret)
> -		goto out;
> -
> -	ret = add_reserved_mem_ranges(mem_ranges);
> -	if (ret)
> -		goto out;
> -
> -	/* exclude memory ranges should be sorted for easy lookup */
> -	sort_memory_ranges(*mem_ranges, true);
> -out:
> -	if (ret)
> -		pr_err("Failed to setup exclude memory ranges\n");
> -	return ret;
> -}
> -
> -/**
> - * get_reserved_memory_ranges - Get reserve memory ranges. This list includes
> - *                              memory regions that should be added to the
> - *                              memory reserve map to ensure the region is
> - *                              protected from any mischief.
> - * @mem_ranges:                 Range list to add the memory ranges to.
> - *
> - * Returns 0 on success, negative errno on error.
> - */
> -static int get_reserved_memory_ranges(struct crash_mem **mem_ranges)
> -{
> -	int ret;
> -
> -	ret = add_rtas_mem_range(mem_ranges);
> -	if (ret)
> -		goto out;
> -
> -	ret = add_tce_mem_ranges(mem_ranges);
> -	if (ret)
> -		goto out;
> -
> -	ret = add_reserved_mem_ranges(mem_ranges);
> -out:
> -	if (ret)
> -		pr_err("Failed to setup reserved memory ranges\n");
> -	return ret;
> -}
> -
>   /**
>    * __locate_mem_hole_top_down - Looks top down for a large enough memory hole
>    *                              in the memory regions between buf_min & buf_max
> @@ -322,119 +245,6 @@ static int locate_mem_hole_bottom_up_ppc64(struct kexec_buf *kbuf,
>   }
>   
>   #ifdef CONFIG_CRASH_DUMP
> -/**
> - * get_usable_memory_ranges - Get usable memory ranges. This list includes
> - *                            regions like crashkernel, opal/rtas & tce-table,
> - *                            that kdump kernel could use.
> - * @mem_ranges:               Range list to add the memory ranges to.
> - *
> - * Returns 0 on success, negative errno on error.
> - */
> -static int get_usable_memory_ranges(struct crash_mem **mem_ranges)
> -{
> -	int ret;
> -
> -	/*
> -	 * Early boot failure observed on guests when low memory (first memory
> -	 * block?) is not added to usable memory. So, add [0, crashk_res.end]
> -	 * instead of [crashk_res.start, crashk_res.end] to workaround it.
> -	 * Also, crashed kernel's memory must be added to reserve map to
> -	 * avoid kdump kernel from using it.
> -	 */
> -	ret = add_mem_range(mem_ranges, 0, crashk_res.end + 1);
> -	if (ret)
> -		goto out;
> -
> -	ret = add_rtas_mem_range(mem_ranges);
> -	if (ret)
> -		goto out;
> -
> -	ret = add_opal_mem_range(mem_ranges);
> -	if (ret)
> -		goto out;
> -
> -	ret = add_tce_mem_ranges(mem_ranges);
> -out:
> -	if (ret)
> -		pr_err("Failed to setup usable memory ranges\n");
> -	return ret;
> -}
> -
> -/**
> - * get_crash_memory_ranges - Get crash memory ranges. This list includes
> - *                           first/crashing kernel's memory regions that
> - *                           would be exported via an elfcore.
> - * @mem_ranges:              Range list to add the memory ranges to.
> - *
> - * Returns 0 on success, negative errno on error.
> - */
> -static int get_crash_memory_ranges(struct crash_mem **mem_ranges)
> -{
> -	phys_addr_t base, end;
> -	struct crash_mem *tmem;
> -	u64 i;
> -	int ret;
> -
> -	for_each_mem_range(i, &base, &end) {
> -		u64 size = end - base;
> -
> -		/* Skip backup memory region, which needs a separate entry */
> -		if (base == BACKUP_SRC_START) {
> -			if (size > BACKUP_SRC_SIZE) {
> -				base = BACKUP_SRC_END + 1;
> -				size -= BACKUP_SRC_SIZE;
> -			} else
> -				continue;
> -		}
> -
> -		ret = add_mem_range(mem_ranges, base, size);
> -		if (ret)
> -			goto out;
> -
> -		/* Try merging adjacent ranges before reallocation attempt */
> -		if ((*mem_ranges)->nr_ranges == (*mem_ranges)->max_nr_ranges)
> -			sort_memory_ranges(*mem_ranges, true);
> -	}
> -
> -	/* Reallocate memory ranges if there is no space to split ranges */
> -	tmem = *mem_ranges;
> -	if (tmem && (tmem->nr_ranges == tmem->max_nr_ranges)) {
> -		tmem = realloc_mem_ranges(mem_ranges);
> -		if (!tmem)
> -			goto out;
> -	}
> -
> -	/* Exclude crashkernel region */
> -	ret = crash_exclude_mem_range(tmem, crashk_res.start, crashk_res.end);
> -	if (ret)
> -		goto out;
> -
> -	/*
> -	 * FIXME: For now, stay in parity with kexec-tools but if RTAS/OPAL
> -	 *        regions are exported to save their context at the time of
> -	 *        crash, they should actually be backed up just like the
> -	 *        first 64K bytes of memory.
> -	 */
> -	ret = add_rtas_mem_range(mem_ranges);
> -	if (ret)
> -		goto out;
> -
> -	ret = add_opal_mem_range(mem_ranges);
> -	if (ret)
> -		goto out;
> -
> -	/* create a separate program header for the backup region */
> -	ret = add_mem_range(mem_ranges, BACKUP_SRC_START, BACKUP_SRC_SIZE);
> -	if (ret)
> -		goto out;
> -
> -	sort_memory_ranges(*mem_ranges, false);
> -out:
> -	if (ret)
> -		pr_err("Failed to setup crash memory ranges\n");
> -	return ret;
> -}
> -
>   /**
>    * check_realloc_usable_mem - Reallocate buffer if it can't accommodate entries
>    * @um_info:                  Usable memory buffer and ranges info.
> diff --git a/arch/powerpc/kexec/ranges.c b/arch/powerpc/kexec/ranges.c
> index fb3e12f15214..297b8bc97b9f 100644
> --- a/arch/powerpc/kexec/ranges.c
> +++ b/arch/powerpc/kexec/ranges.c
> @@ -20,9 +20,13 @@
>   #include <linux/kexec.h>
>   #include <linux/of.h>
>   #include <linux/slab.h>
> +#include <linux/memblock.h>
> +#include <linux/crash_core.h>
>   #include <asm/sections.h>
>   #include <asm/kexec_ranges.h>
> +#include <asm/crashdump-ppc64.h>
>   
> +#if defined(CONFIG_KEXEC_FILE) || defined(CONFIG_CRASH_DUMP)
>   /**
>    * get_max_nr_ranges - Get the max no. of ranges crash_mem structure
>    *                     could hold, given the size allocated for it.
> @@ -234,13 +238,16 @@ int add_mem_range(struct crash_mem **mem_ranges, u64 base, u64 size)
>   	return __add_mem_range(mem_ranges, base, size);
>   }
>   
> +#endif /* CONFIG_KEXEC_FILE || CONFIG_CRASH_DUMP */
> +
> +#ifdef CONFIG_KEXEC_FILE
>   /**
>    * add_tce_mem_ranges - Adds tce-table range to the given memory ranges list.
>    * @mem_ranges:         Range list to add the memory range(s) to.
>    *
>    * Returns 0 on success, negative errno on error.
>    */
> -int add_tce_mem_ranges(struct crash_mem **mem_ranges)
> +static int add_tce_mem_ranges(struct crash_mem **mem_ranges)
>   {
>   	struct device_node *dn = NULL;
>   	int ret = 0;
> @@ -279,7 +286,7 @@ int add_tce_mem_ranges(struct crash_mem **mem_ranges)
>    *
>    * Returns 0 on success, negative errno on error.
>    */
> -int add_initrd_mem_range(struct crash_mem **mem_ranges)
> +static int add_initrd_mem_range(struct crash_mem **mem_ranges)
>   {
>   	u64 base, end;
>   	int ret;
> @@ -296,7 +303,6 @@ int add_initrd_mem_range(struct crash_mem **mem_ranges)
>   	return ret;
>   }
>   
> -#ifdef CONFIG_PPC_64S_HASH_MMU
>   /**
>    * add_htab_mem_range - Adds htab range to the given memory ranges list,
>    *                      if it exists
> @@ -304,14 +310,18 @@ int add_initrd_mem_range(struct crash_mem **mem_ranges)
>    *
>    * Returns 0 on success, negative errno on error.
>    */
> -int add_htab_mem_range(struct crash_mem **mem_ranges)
> +static int add_htab_mem_range(struct crash_mem **mem_ranges)
>   {
> +
> +#ifdef CONFIG_PPC_64S_HASH_MMU
>   	if (!htab_address)
>   		return 0;
>   
>   	return add_mem_range(mem_ranges, __pa(htab_address), htab_size_bytes);
> -}
> +#else
> +	return 0;
>   #endif
> +}
>   
>   /**
>    * add_kernel_mem_range - Adds kernel text region to the given
> @@ -320,18 +330,20 @@ int add_htab_mem_range(struct crash_mem **mem_ranges)
>    *
>    * Returns 0 on success, negative errno on error.
>    */
> -int add_kernel_mem_range(struct crash_mem **mem_ranges)
> +static int add_kernel_mem_range(struct crash_mem **mem_ranges)
>   {
>   	return add_mem_range(mem_ranges, 0, __pa(_end));
>   }
> +#endif /* CONFIG_KEXEC_FILE */
>   
> +#if defined(CONFIG_KEXEC_FILE) || defined(CONFIG_CRASH_DUMP)
>   /**
>    * add_rtas_mem_range - Adds RTAS region to the given memory ranges list.
>    * @mem_ranges:         Range list to add the memory range to.
>    *
>    * Returns 0 on success, negative errno on error.
>    */
> -int add_rtas_mem_range(struct crash_mem **mem_ranges)
> +static int add_rtas_mem_range(struct crash_mem **mem_ranges)
>   {
>   	struct device_node *dn;
>   	u32 base, size;
> @@ -356,7 +368,7 @@ int add_rtas_mem_range(struct crash_mem **mem_ranges)
>    *
>    * Returns 0 on success, negative errno on error.
>    */
> -int add_opal_mem_range(struct crash_mem **mem_ranges)
> +static int add_opal_mem_range(struct crash_mem **mem_ranges)
>   {
>   	struct device_node *dn;
>   	u64 base, size;
> @@ -374,7 +386,9 @@ int add_opal_mem_range(struct crash_mem **mem_ranges)
>   	of_node_put(dn);
>   	return ret;
>   }
> +#endif /* CONFIG_KEXEC_FILE || CONFIG_CRASH_DUMP */
>   
> +#ifdef CONFIG_KEXEC_FILE
>   /**
>    * add_reserved_mem_ranges - Adds "/reserved-ranges" regions exported by f/w
>    *                           to the given memory ranges list.
> @@ -382,7 +396,7 @@ int add_opal_mem_range(struct crash_mem **mem_ranges)
>    *
>    * Returns 0 on success, negative errno on error.
>    */
> -int add_reserved_mem_ranges(struct crash_mem **mem_ranges)
> +static int add_reserved_mem_ranges(struct crash_mem **mem_ranges)
>   {
>   	int n_mem_addr_cells, n_mem_size_cells, i, len, cells, ret = 0;
>   	const __be32 *prop;
> @@ -410,3 +424,198 @@ int add_reserved_mem_ranges(struct crash_mem **mem_ranges)
>   
>   	return ret;
>   }
> +
> +/**
> + * get_reserved_memory_ranges - Get reserve memory ranges. This list includes
> + *                              memory regions that should be added to the
> + *                              memory reserve map to ensure the region is
> + *                              protected from any mischief.
> + * @mem_ranges:                 Range list to add the memory ranges to.
> + *
> + * Returns 0 on success, negative errno on error.
> + */
> +int get_reserved_memory_ranges(struct crash_mem **mem_ranges)
> +{
> +	int ret;
> +
> +	ret = add_rtas_mem_range(mem_ranges);
> +	if (ret)
> +		goto out;
> +
> +	ret = add_tce_mem_ranges(mem_ranges);
> +	if (ret)
> +		goto out;
> +
> +	ret = add_reserved_mem_ranges(mem_ranges);
> +out:
> +	if (ret)
> +		pr_err("Failed to setup reserved memory ranges\n");
> +	return ret;
> +}
> +
> +/**
> + * get_exclude_memory_ranges - Get exclude memory ranges. This list includes
> + *                             regions like opal/rtas, tce-table, initrd,
> + *                             kernel, htab which should be avoided while
> + *                             setting up kexec load segments.
> + * @mem_ranges:                Range list to add the memory ranges to.
> + *
> + * Returns 0 on success, negative errno on error.
> + */
> +int get_exclude_memory_ranges(struct crash_mem **mem_ranges)
> +{
> +	int ret;
> +
> +	ret = add_tce_mem_ranges(mem_ranges);
> +	if (ret)
> +		goto out;
> +
> +	ret = add_initrd_mem_range(mem_ranges);
> +	if (ret)
> +		goto out;
> +
> +	ret = add_htab_mem_range(mem_ranges);
> +	if (ret)
> +		goto out;
> +
> +	ret = add_kernel_mem_range(mem_ranges);
> +	if (ret)
> +		goto out;
> +
> +	ret = add_rtas_mem_range(mem_ranges);
> +	if (ret)
> +		goto out;
> +
> +	ret = add_opal_mem_range(mem_ranges);
> +	if (ret)
> +		goto out;
> +
> +	ret = add_reserved_mem_ranges(mem_ranges);
> +	if (ret)
> +		goto out;
> +
> +	/* exclude memory ranges should be sorted for easy lookup */
> +	sort_memory_ranges(*mem_ranges, true);
> +out:
> +	if (ret)
> +		pr_err("Failed to setup exclude memory ranges\n");
> +	return ret;
> +}
> +
> +#ifdef CONFIG_CRASH_DUMP
> +/**
> + * get_usable_memory_ranges - Get usable memory ranges. This list includes
> + *                            regions like crashkernel, opal/rtas & tce-table,
> + *                            that kdump kernel could use.
> + * @mem_ranges:               Range list to add the memory ranges to.
> + *
> + * Returns 0 on success, negative errno on error.
> + */
> +int get_usable_memory_ranges(struct crash_mem **mem_ranges)
> +{
> +	int ret;
> +
> +	/*
> +	 * Early boot failure observed on guests when low memory (first memory
> +	 * block?) is not added to usable memory. So, add [0, crashk_res.end]
> +	 * instead of [crashk_res.start, crashk_res.end] to workaround it.
> +	 * Also, crashed kernel's memory must be added to reserve map to
> +	 * avoid kdump kernel from using it.
> +	 */
> +	ret = add_mem_range(mem_ranges, 0, crashk_res.end + 1);
> +	if (ret)
> +		goto out;
> +
> +	ret = add_rtas_mem_range(mem_ranges);
> +	if (ret)
> +		goto out;
> +
> +	ret = add_opal_mem_range(mem_ranges);
> +	if (ret)
> +		goto out;
> +
> +	ret = add_tce_mem_ranges(mem_ranges);
> +out:
> +	if (ret)
> +		pr_err("Failed to setup usable memory ranges\n");
> +	return ret;
> +}
> +#endif /* CONFIG_CRASH_DUMP */
> +#endif /* CONFIG_KEXEC_FILE */
> +
> +#ifdef CONFIG_CRASH_DUMP
> +/**
> + * get_crash_memory_ranges - Get crash memory ranges. This list includes
> + *                           first/crashing kernel's memory regions that
> + *                           would be exported via an elfcore.
> + * @mem_ranges:              Range list to add the memory ranges to.
> + *
> + * Returns 0 on success, negative errno on error.
> + */
> +int get_crash_memory_ranges(struct crash_mem **mem_ranges)
> +{
> +	phys_addr_t base, end;
> +	struct crash_mem *tmem;
> +	u64 i;
> +	int ret;
> +
> +	for_each_mem_range(i, &base, &end) {
> +		u64 size = end - base;
> +
> +		/* Skip backup memory region, which needs a separate entry */
> +		if (base == BACKUP_SRC_START) {
> +			if (size > BACKUP_SRC_SIZE) {
> +				base = BACKUP_SRC_END + 1;
> +				size -= BACKUP_SRC_SIZE;
> +			} else
> +				continue;
> +		}
> +
> +		ret = add_mem_range(mem_ranges, base, size);
> +		if (ret)
> +			goto out;
> +
> +		/* Try merging adjacent ranges before reallocation attempt */
> +		if ((*mem_ranges)->nr_ranges == (*mem_ranges)->max_nr_ranges)
> +			sort_memory_ranges(*mem_ranges, true);
> +	}
> +
> +	/* Reallocate memory ranges if there is no space to split ranges */
> +	tmem = *mem_ranges;
> +	if (tmem && (tmem->nr_ranges == tmem->max_nr_ranges)) {
> +		tmem = realloc_mem_ranges(mem_ranges);
> +		if (!tmem)
> +			goto out;
> +	}
> +
> +	/* Exclude crashkernel region */
> +	ret = crash_exclude_mem_range(tmem, crashk_res.start, crashk_res.end);
> +	if (ret)
> +		goto out;
> +
> +	/*
> +	 * FIXME: For now, stay in parity with kexec-tools but if RTAS/OPAL
> +	 *        regions are exported to save their context at the time of
> +	 *        crash, they should actually be backed up just like the
> +	 *        first 64K bytes of memory.
> +	 */
> +	ret = add_rtas_mem_range(mem_ranges);
> +	if (ret)
> +		goto out;
> +
> +	ret = add_opal_mem_range(mem_ranges);
> +	if (ret)
> +		goto out;
> +
> +	/* create a separate program header for the backup region */
> +	ret = add_mem_range(mem_ranges, BACKUP_SRC_START, BACKUP_SRC_SIZE);
> +	if (ret)
> +		goto out;
> +
> +	sort_memory_ranges(*mem_ranges, false);
> +out:
> +	if (ret)
> +		pr_err("Failed to setup crash memory ranges\n");
> +	return ret;
> +}
> +#endif /* CONFIG_CRASH_DUMP */

^ permalink raw reply	[flat|nested] 18+ messages in thread

end of thread, other threads:[~2024-03-02 13:20 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-02-26  8:41 [PATCH v17 0/6] powerpc/crash: Kernel handling of CPU and memory hotplug Sourabh Jain
2024-02-26  8:41 ` [PATCH v17 1/6] crash: forward memory_notify arg to arch crash hotplug handler Sourabh Jain
2024-02-26  8:41 ` [PATCH v17 2/6] crash: add a new kexec flag for hotplug support Sourabh Jain
2024-02-29  5:05   ` Sourabh Jain
2024-02-29  5:56     ` Baoquan He
2024-02-29  7:58       ` Sourabh Jain
2024-02-29  7:28   ` Baoquan He
2024-02-29  8:14     ` Sourabh Jain
2024-02-29  8:34     ` Sourabh Jain
2024-02-26  8:41 ` [PATCH v17 3/6] powerpc/kexec: move *_memory_ranges functions to ranges.c Sourabh Jain
2024-02-29  8:11   ` Baoquan He
2024-02-29  8:16     ` Sourabh Jain
2024-02-26  8:41 ` [PATCH v17 4/6] PowerPC/kexec: make the update_cpus_node() function public Sourabh Jain
2024-02-26  8:41 ` [PATCH v17 5/6] powerpc/crash: add crash CPU hotplug support Sourabh Jain
2024-02-26  8:41 ` [PATCH v17 6/6] powerpc/crash: add crash memory " Sourabh Jain
2024-02-29 13:51 ` [PATCH v17 0/6] powerpc/crash: Kernel handling of CPU and memory hotplug Baoquan He
2024-03-01  5:13   ` Sourabh Jain
  -- strict thread matches above, loose matches on Subject: below --
2024-03-02 13:18 [PATCH v17 3/6] powerpc/kexec: move *_memory_ranges functions to ranges.c Hari Bathini

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).