2020-07-29 17:09:41 +05:30
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
|
|
|
/*
|
|
|
|
* ppc64 code to implement the kexec_file_load syscall
|
|
|
|
*
|
|
|
|
* Copyright (C) 2004 Adam Litke (agl@us.ibm.com)
|
|
|
|
* Copyright (C) 2004 IBM Corp.
|
|
|
|
* Copyright (C) 2004,2005 Milton D Miller II, IBM Corporation
|
|
|
|
* Copyright (C) 2005 R Sharada (sharada@in.ibm.com)
|
|
|
|
* Copyright (C) 2006 Mohan Kumar M (mohan@in.ibm.com)
|
|
|
|
* Copyright (C) 2020 IBM Corporation
|
|
|
|
*
|
|
|
|
* Based on kexec-tools' kexec-ppc64.c, kexec-elf-rel-ppc64.c, fs2dt.c.
|
|
|
|
* Heavily modified for the kernel by
|
|
|
|
* Hari Bathini, IBM Corporation.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/kexec.h>
|
|
|
|
#include <linux/of_fdt.h>
|
|
|
|
#include <linux/libfdt.h>
|
2023-07-24 15:02:42 -06:00
|
|
|
#include <linux/of.h>
|
2020-07-29 17:10:16 +05:30
|
|
|
#include <linux/memblock.h>
|
2020-07-29 17:12:16 +05:30
|
|
|
#include <linux/slab.h>
|
2020-07-29 17:12:58 +05:30
|
|
|
#include <linux/vmalloc.h>
|
2021-02-04 17:01:10 +05:30
|
|
|
#include <asm/setup.h>
|
2020-07-29 17:12:16 +05:30
|
|
|
#include <asm/drmem.h>
|
2022-08-03 16:29:41 +10:00
|
|
|
#include <asm/firmware.h>
|
2020-07-29 17:10:16 +05:30
|
|
|
#include <asm/kexec_ranges.h>
|
2020-07-29 17:12:58 +05:30
|
|
|
#include <asm/crashdump-ppc64.h>
|
2023-02-04 09:22:06 -08:00
|
|
|
#include <asm/mmzone.h>
|
2023-08-17 18:24:08 +02:00
|
|
|
#include <asm/iommu.h>
|
powerpc: Take in account addition CPU node when building kexec FDT
On a system with a large number of CPUs, the creation of the FDT for a
kexec kernel may fail because the allocated FDT is not large enough.
When this happens, such a message is displayed on the console:
Unable to add ibm,processor-vadd-size property: FDT_ERR_NOSPACE
The property's name may change depending when the buffer overwrite is
detected.
Obviously the created FDT is missing information, and it is expected
that system dump or kexec kernel failed to run properly.
When the FDT is allocated, the size of the FDT the kernel received at
boot time is used and an extra size can be applied. Currently, only
memory added after boot time is taken in account, not the CPU nodes.
The extra size should take in account these additional CPU nodes and
compute the required extra space. To achieve that, the size of a CPU
node, including its subnode is computed once and multiplied by the
number of additional CPU nodes.
The assumption is that the size of the CPU node is _same_ for all the
node, the only variable part should be the name "PowerPC,POWERxx@##"
where "##" may vary a little.
Signed-off-by: Laurent Dufour <ldufour@linux.ibm.com>
[mpe: Don't shadow function name w/variable, minor coding style changes]
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
Link: https://lore.kernel.org/r/20221110180619.15796-3-ldufour@linux.ibm.com
2022-11-10 19:06:19 +01:00
|
|
|
#include <asm/prom.h>
|
2023-02-10 19:03:58 +11:00
|
|
|
#include <asm/plpks.h>
|
powerpc/crash: add crash CPU hotplug support
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.
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.
To address the aforementioned issue, 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.
In the event of a CPU or memory add/remove events, the generic crash
hotplug event handler, `crash_handle_hotplug_event()`, is triggered. It
then acquires the necessary locks to update the kdump image and invokes
the architecture-specific crash hotplug handler,
`arch_crash_handle_hotplug_event()`, to update the required kdump image
components.
This patch adds crash hotplug handler for PowerPC and enable support to
update the kdump image on CPU add/remove events. Support for memory
add/remove events is added in a subsequent patch with the title
"powerpc: add crash memory hotplug support"
As mentioned earlier, only the elfcorehdr and FDT kdump image components
need to be updated in the event of CPU or memory add/remove events.
However, on PowerPC architecture crash hotplug handler only updates the
FDT to enable crash hotplug support for CPU add/remove events. Here's
why.
The elfcorehdr on PowerPC is built with possible CPUs, and thus, it does
not need an update on CPU add/remove events. On the other hand, the FDT
needs to be updated on CPU add events to include the newly added CPU. If
the FDT is not updated and the kernel crashes on a newly added CPU, the
kdump kernel will fail to boot due to the unavailability of the crashing
CPU in the FDT. During the early boot, it is expected that the boot CPU
must be a part of the FDT; otherwise, the kernel will raise a BUG and
fail to boot. For more information, refer to commit 36ae37e3436b0
("powerpc: Make boot_cpuid common between 32 and 64-bit"). Since it is
okay to have an offline CPU in the kdump FDT, no action is taken in case
of CPU removal.
There are two system calls, `kexec_file_load` and `kexec_load`, used to
load the kdump image. Few changes have been made to ensure kernel can
safely update the FDT of kdump image loaded using both system calls.
For kexec_file_load syscall the kdump image is prepared in kernel. So to
support an increasing number of CPUs, the FDT is constructed with extra
buffer space to ensure it can accommodate a possible number of CPU
nodes. Additionally, a call to fdt_pack (which trims the unused space
once the FDT is prepared) is avoided if this feature is enabled.
For the kexec_load syscall, the FDT is updated only if the
KEXEC_CRASH_HOTPLUG_SUPPORT kexec flag is passed to the kernel by
userspace (kexec tools). When userspace passes this flag to the kernel,
it indicates that the FDT is built to accommodate possible CPUs, and the
FDT segment is excluded from SHA calculation, making it safe to update.
The changes related to this feature are kept under the CRASH_HOTPLUG
config, and it is enabled by default.
Signed-off-by: Sourabh Jain <sourabhjain@linux.ibm.com>
Acked-by: Hari Bathini <hbathini@linux.ibm.com>
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
Link: https://msgid.link/20240326055413.186534-6-sourabhjain@linux.ibm.com
2024-03-26 11:24:12 +05:30
|
|
|
#include <asm/cputhreads.h>
|
2020-07-29 17:09:41 +05:30
|
|
|
|
2020-07-29 17:12:16 +05:30
|
|
|
struct umem_info {
|
2023-10-11 16:37:05 +11:00
|
|
|
__be64 *buf; /* data buffer for usable-memory property */
|
2020-07-29 17:12:16 +05:30
|
|
|
u32 size; /* size allocated for the data buffer */
|
|
|
|
u32 max_entries; /* maximum no. of entries */
|
|
|
|
u32 idx; /* index of current entry */
|
|
|
|
|
|
|
|
/* usable memory ranges to look up */
|
|
|
|
unsigned int nr_ranges;
|
2022-09-29 12:29:35 +08:00
|
|
|
const struct range *ranges;
|
2020-07-29 17:12:16 +05:30
|
|
|
};
|
|
|
|
|
2020-07-29 17:09:41 +05:30
|
|
|
const struct kexec_file_ops * const kexec_file_loaders[] = {
|
|
|
|
&kexec_elf64_ops,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2020-07-29 17:10:16 +05:30
|
|
|
/**
|
|
|
|
* __locate_mem_hole_top_down - Looks top down for a large enough memory hole
|
|
|
|
* in the memory regions between buf_min & buf_max
|
|
|
|
* for the buffer. If found, sets kbuf->mem.
|
|
|
|
* @kbuf: Buffer contents and memory parameters.
|
|
|
|
* @buf_min: Minimum address for the buffer.
|
|
|
|
* @buf_max: Maximum address for the buffer.
|
|
|
|
*
|
|
|
|
* Returns 0 on success, negative errno on error.
|
|
|
|
*/
|
|
|
|
static int __locate_mem_hole_top_down(struct kexec_buf *kbuf,
|
|
|
|
u64 buf_min, u64 buf_max)
|
|
|
|
{
|
|
|
|
int ret = -EADDRNOTAVAIL;
|
|
|
|
phys_addr_t start, end;
|
|
|
|
u64 i;
|
|
|
|
|
2020-10-13 16:57:59 -07:00
|
|
|
for_each_mem_range_rev(i, &start, &end) {
|
2020-07-29 17:10:16 +05:30
|
|
|
/*
|
|
|
|
* memblock uses [start, end) convention while it is
|
|
|
|
* [start, end] here. Fix the off-by-one to have the
|
|
|
|
* same convention.
|
|
|
|
*/
|
|
|
|
end -= 1;
|
|
|
|
|
|
|
|
if (start > buf_max)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Memory hole not found */
|
|
|
|
if (end < buf_min)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Adjust memory region based on the given range */
|
|
|
|
if (start < buf_min)
|
|
|
|
start = buf_min;
|
|
|
|
if (end > buf_max)
|
|
|
|
end = buf_max;
|
|
|
|
|
|
|
|
start = ALIGN(start, kbuf->buf_align);
|
|
|
|
if (start < end && (end - start + 1) >= kbuf->memsz) {
|
|
|
|
/* Suitable memory range found. Set kbuf->mem */
|
|
|
|
kbuf->mem = ALIGN_DOWN(end - kbuf->memsz + 1,
|
|
|
|
kbuf->buf_align);
|
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* locate_mem_hole_top_down_ppc64 - Skip special memory regions to find a
|
|
|
|
* suitable buffer with top down approach.
|
|
|
|
* @kbuf: Buffer contents and memory parameters.
|
|
|
|
* @buf_min: Minimum address for the buffer.
|
|
|
|
* @buf_max: Maximum address for the buffer.
|
|
|
|
* @emem: Exclude memory ranges.
|
|
|
|
*
|
|
|
|
* Returns 0 on success, negative errno on error.
|
|
|
|
*/
|
|
|
|
static int locate_mem_hole_top_down_ppc64(struct kexec_buf *kbuf,
|
|
|
|
u64 buf_min, u64 buf_max,
|
|
|
|
const struct crash_mem *emem)
|
|
|
|
{
|
|
|
|
int i, ret = 0, err = -EADDRNOTAVAIL;
|
|
|
|
u64 start, end, tmin, tmax;
|
|
|
|
|
|
|
|
tmax = buf_max;
|
|
|
|
for (i = (emem->nr_ranges - 1); i >= 0; i--) {
|
|
|
|
start = emem->ranges[i].start;
|
|
|
|
end = emem->ranges[i].end;
|
|
|
|
|
|
|
|
if (start > tmax)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (end < tmax) {
|
|
|
|
tmin = (end < buf_min ? buf_min : end + 1);
|
|
|
|
ret = __locate_mem_hole_top_down(kbuf, tmin, tmax);
|
|
|
|
if (!ret)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
tmax = start - 1;
|
|
|
|
|
|
|
|
if (tmax < buf_min) {
|
|
|
|
ret = err;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ret) {
|
|
|
|
tmin = buf_min;
|
|
|
|
ret = __locate_mem_hole_top_down(kbuf, tmin, tmax);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* __locate_mem_hole_bottom_up - Looks bottom up for a large enough memory hole
|
|
|
|
* in the memory regions between buf_min & buf_max
|
|
|
|
* for the buffer. If found, sets kbuf->mem.
|
|
|
|
* @kbuf: Buffer contents and memory parameters.
|
|
|
|
* @buf_min: Minimum address for the buffer.
|
|
|
|
* @buf_max: Maximum address for the buffer.
|
|
|
|
*
|
|
|
|
* Returns 0 on success, negative errno on error.
|
|
|
|
*/
|
|
|
|
static int __locate_mem_hole_bottom_up(struct kexec_buf *kbuf,
|
|
|
|
u64 buf_min, u64 buf_max)
|
|
|
|
{
|
|
|
|
int ret = -EADDRNOTAVAIL;
|
|
|
|
phys_addr_t start, end;
|
|
|
|
u64 i;
|
|
|
|
|
2020-10-13 16:57:59 -07:00
|
|
|
for_each_mem_range(i, &start, &end) {
|
2020-07-29 17:10:16 +05:30
|
|
|
/*
|
|
|
|
* memblock uses [start, end) convention while it is
|
|
|
|
* [start, end] here. Fix the off-by-one to have the
|
|
|
|
* same convention.
|
|
|
|
*/
|
|
|
|
end -= 1;
|
|
|
|
|
|
|
|
if (end < buf_min)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Memory hole not found */
|
|
|
|
if (start > buf_max)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Adjust memory region based on the given range */
|
|
|
|
if (start < buf_min)
|
|
|
|
start = buf_min;
|
|
|
|
if (end > buf_max)
|
|
|
|
end = buf_max;
|
|
|
|
|
|
|
|
start = ALIGN(start, kbuf->buf_align);
|
|
|
|
if (start < end && (end - start + 1) >= kbuf->memsz) {
|
|
|
|
/* Suitable memory range found. Set kbuf->mem */
|
|
|
|
kbuf->mem = start;
|
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* locate_mem_hole_bottom_up_ppc64 - Skip special memory regions to find a
|
|
|
|
* suitable buffer with bottom up approach.
|
|
|
|
* @kbuf: Buffer contents and memory parameters.
|
|
|
|
* @buf_min: Minimum address for the buffer.
|
|
|
|
* @buf_max: Maximum address for the buffer.
|
|
|
|
* @emem: Exclude memory ranges.
|
|
|
|
*
|
|
|
|
* Returns 0 on success, negative errno on error.
|
|
|
|
*/
|
|
|
|
static int locate_mem_hole_bottom_up_ppc64(struct kexec_buf *kbuf,
|
|
|
|
u64 buf_min, u64 buf_max,
|
|
|
|
const struct crash_mem *emem)
|
|
|
|
{
|
|
|
|
int i, ret = 0, err = -EADDRNOTAVAIL;
|
|
|
|
u64 start, end, tmin, tmax;
|
|
|
|
|
|
|
|
tmin = buf_min;
|
|
|
|
for (i = 0; i < emem->nr_ranges; i++) {
|
|
|
|
start = emem->ranges[i].start;
|
|
|
|
end = emem->ranges[i].end;
|
|
|
|
|
|
|
|
if (end < tmin)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (start > tmin) {
|
|
|
|
tmax = (start > buf_max ? buf_max : start - 1);
|
|
|
|
ret = __locate_mem_hole_bottom_up(kbuf, tmin, tmax);
|
|
|
|
if (!ret)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
tmin = end + 1;
|
|
|
|
|
|
|
|
if (tmin > buf_max) {
|
|
|
|
ret = err;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ret) {
|
|
|
|
tmax = buf_max;
|
|
|
|
ret = __locate_mem_hole_bottom_up(kbuf, tmin, tmax);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2024-02-26 16:00:09 +05:30
|
|
|
#ifdef CONFIG_CRASH_DUMP
|
2020-07-29 17:12:16 +05:30
|
|
|
/**
|
|
|
|
* check_realloc_usable_mem - Reallocate buffer if it can't accommodate entries
|
|
|
|
* @um_info: Usable memory buffer and ranges info.
|
|
|
|
* @cnt: No. of entries to accommodate.
|
|
|
|
*
|
|
|
|
* Frees up the old buffer if memory reallocation fails.
|
|
|
|
*
|
|
|
|
* Returns buffer on success, NULL on error.
|
|
|
|
*/
|
2023-10-11 16:37:05 +11:00
|
|
|
static __be64 *check_realloc_usable_mem(struct umem_info *um_info, int cnt)
|
2020-07-29 17:12:16 +05:30
|
|
|
{
|
|
|
|
u32 new_size;
|
2023-10-11 16:37:05 +11:00
|
|
|
__be64 *tbuf;
|
2020-07-29 17:12:16 +05:30
|
|
|
|
|
|
|
if ((um_info->idx + cnt) <= um_info->max_entries)
|
|
|
|
return um_info->buf;
|
|
|
|
|
|
|
|
new_size = um_info->size + MEM_RANGE_CHUNK_SZ;
|
|
|
|
tbuf = krealloc(um_info->buf, new_size, GFP_KERNEL);
|
|
|
|
if (tbuf) {
|
|
|
|
um_info->buf = tbuf;
|
|
|
|
um_info->size = new_size;
|
|
|
|
um_info->max_entries = (um_info->size / sizeof(u64));
|
|
|
|
}
|
|
|
|
|
|
|
|
return tbuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* add_usable_mem - Add the usable memory ranges within the given memory range
|
|
|
|
* to the buffer
|
|
|
|
* @um_info: Usable memory buffer and ranges info.
|
|
|
|
* @base: Base address of memory range to look for.
|
|
|
|
* @end: End address of memory range to look for.
|
|
|
|
*
|
|
|
|
* Returns 0 on success, negative errno on error.
|
|
|
|
*/
|
|
|
|
static int add_usable_mem(struct umem_info *um_info, u64 base, u64 end)
|
|
|
|
{
|
|
|
|
u64 loc_base, loc_end;
|
|
|
|
bool add;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < um_info->nr_ranges; i++) {
|
|
|
|
add = false;
|
|
|
|
loc_base = um_info->ranges[i].start;
|
|
|
|
loc_end = um_info->ranges[i].end;
|
|
|
|
if (loc_base >= base && loc_end <= end)
|
|
|
|
add = true;
|
|
|
|
else if (base < loc_end && end > loc_base) {
|
|
|
|
if (loc_base < base)
|
|
|
|
loc_base = base;
|
|
|
|
if (loc_end > end)
|
|
|
|
loc_end = end;
|
|
|
|
add = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (add) {
|
|
|
|
if (!check_realloc_usable_mem(um_info, 2))
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
um_info->buf[um_info->idx++] = cpu_to_be64(loc_base);
|
|
|
|
um_info->buf[um_info->idx++] =
|
|
|
|
cpu_to_be64(loc_end - loc_base + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* kdump_setup_usable_lmb - This is a callback function that gets called by
|
|
|
|
* walk_drmem_lmbs for every LMB to set its
|
|
|
|
* usable memory ranges.
|
|
|
|
* @lmb: LMB info.
|
|
|
|
* @usm: linux,drconf-usable-memory property value.
|
|
|
|
* @data: Pointer to usable memory buffer and ranges info.
|
|
|
|
*
|
|
|
|
* Returns 0 on success, negative errno on error.
|
|
|
|
*/
|
|
|
|
static int kdump_setup_usable_lmb(struct drmem_lmb *lmb, const __be32 **usm,
|
|
|
|
void *data)
|
|
|
|
{
|
|
|
|
struct umem_info *um_info;
|
|
|
|
int tmp_idx, ret;
|
|
|
|
u64 base, end;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* kdump load isn't supported on kernels already booted with
|
|
|
|
* linux,drconf-usable-memory property.
|
|
|
|
*/
|
|
|
|
if (*usm) {
|
|
|
|
pr_err("linux,drconf-usable-memory property already exists!");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
um_info = data;
|
|
|
|
tmp_idx = um_info->idx;
|
|
|
|
if (!check_realloc_usable_mem(um_info, 1))
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
um_info->idx++;
|
|
|
|
base = lmb->base_addr;
|
|
|
|
end = base + drmem_lmb_size() - 1;
|
|
|
|
ret = add_usable_mem(um_info, base, end);
|
|
|
|
if (!ret) {
|
|
|
|
/*
|
|
|
|
* Update the no. of ranges added. Two entries (base & size)
|
|
|
|
* for every range added.
|
|
|
|
*/
|
|
|
|
um_info->buf[tmp_idx] =
|
|
|
|
cpu_to_be64((um_info->idx - tmp_idx - 1) / 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define NODE_PATH_LEN 256
|
|
|
|
/**
|
|
|
|
* add_usable_mem_property - Add usable memory property for the given
|
|
|
|
* memory node.
|
|
|
|
* @fdt: Flattened device tree for the kdump kernel.
|
|
|
|
* @dn: Memory node.
|
|
|
|
* @um_info: Usable memory buffer and ranges info.
|
|
|
|
*
|
|
|
|
* Returns 0 on success, negative errno on error.
|
|
|
|
*/
|
|
|
|
static int add_usable_mem_property(void *fdt, struct device_node *dn,
|
|
|
|
struct umem_info *um_info)
|
|
|
|
{
|
|
|
|
int n_mem_addr_cells, n_mem_size_cells, node;
|
|
|
|
char path[NODE_PATH_LEN];
|
|
|
|
int i, len, ranges, ret;
|
|
|
|
const __be32 *prop;
|
|
|
|
u64 base, end;
|
|
|
|
|
|
|
|
of_node_get(dn);
|
|
|
|
|
|
|
|
if (snprintf(path, NODE_PATH_LEN, "%pOF", dn) > (NODE_PATH_LEN - 1)) {
|
|
|
|
pr_err("Buffer (%d) too small for memory node: %pOF\n",
|
|
|
|
NODE_PATH_LEN, dn);
|
|
|
|
return -EOVERFLOW;
|
|
|
|
}
|
2023-12-13 13:57:46 +08:00
|
|
|
kexec_dprintk("Memory node path: %s\n", path);
|
2020-07-29 17:12:16 +05:30
|
|
|
|
|
|
|
/* Now that we know the path, find its offset in kdump kernel's fdt */
|
|
|
|
node = fdt_path_offset(fdt, path);
|
|
|
|
if (node < 0) {
|
|
|
|
pr_err("Malformed device tree: error reading %s\n", path);
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the address & size cells */
|
|
|
|
n_mem_addr_cells = of_n_addr_cells(dn);
|
|
|
|
n_mem_size_cells = of_n_size_cells(dn);
|
2023-12-13 13:57:46 +08:00
|
|
|
kexec_dprintk("address cells: %d, size cells: %d\n", n_mem_addr_cells,
|
|
|
|
n_mem_size_cells);
|
2020-07-29 17:12:16 +05:30
|
|
|
|
|
|
|
um_info->idx = 0;
|
|
|
|
if (!check_realloc_usable_mem(um_info, 2)) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
prop = of_get_property(dn, "reg", &len);
|
|
|
|
if (!prop || len <= 0) {
|
|
|
|
ret = 0;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* "reg" property represents sequence of (addr,size) tuples
|
|
|
|
* each representing a memory range.
|
|
|
|
*/
|
|
|
|
ranges = (len >> 2) / (n_mem_addr_cells + n_mem_size_cells);
|
|
|
|
|
|
|
|
for (i = 0; i < ranges; i++) {
|
|
|
|
base = of_read_number(prop, n_mem_addr_cells);
|
|
|
|
prop += n_mem_addr_cells;
|
|
|
|
end = base + of_read_number(prop, n_mem_size_cells) - 1;
|
|
|
|
prop += n_mem_size_cells;
|
|
|
|
|
|
|
|
ret = add_usable_mem(um_info, base, end);
|
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* No kdump kernel usable memory found in this memory node.
|
|
|
|
* Write (0,0) tuple in linux,usable-memory property for
|
|
|
|
* this region to be ignored.
|
|
|
|
*/
|
|
|
|
if (um_info->idx == 0) {
|
|
|
|
um_info->buf[0] = 0;
|
|
|
|
um_info->buf[1] = 0;
|
|
|
|
um_info->idx = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = fdt_setprop(fdt, node, "linux,usable-memory", um_info->buf,
|
|
|
|
(um_info->idx * sizeof(u64)));
|
|
|
|
|
|
|
|
out:
|
|
|
|
of_node_put(dn);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* update_usable_mem_fdt - Updates kdump kernel's fdt with linux,usable-memory
|
|
|
|
* and linux,drconf-usable-memory DT properties as
|
|
|
|
* appropriate to restrict its memory usage.
|
|
|
|
* @fdt: Flattened device tree for the kdump kernel.
|
|
|
|
* @usable_mem: Usable memory ranges for kdump kernel.
|
|
|
|
*
|
|
|
|
* Returns 0 on success, negative errno on error.
|
|
|
|
*/
|
|
|
|
static int update_usable_mem_fdt(void *fdt, struct crash_mem *usable_mem)
|
|
|
|
{
|
|
|
|
struct umem_info um_info;
|
|
|
|
struct device_node *dn;
|
|
|
|
int node, ret = 0;
|
|
|
|
|
|
|
|
if (!usable_mem) {
|
|
|
|
pr_err("Usable memory ranges for kdump kernel not found\n");
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
node = fdt_path_offset(fdt, "/ibm,dynamic-reconfiguration-memory");
|
|
|
|
if (node == -FDT_ERR_NOTFOUND)
|
2023-12-13 13:57:46 +08:00
|
|
|
kexec_dprintk("No dynamic reconfiguration memory found\n");
|
2020-07-29 17:12:16 +05:30
|
|
|
else if (node < 0) {
|
|
|
|
pr_err("Malformed device tree: error reading /ibm,dynamic-reconfiguration-memory.\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
um_info.buf = NULL;
|
|
|
|
um_info.size = 0;
|
|
|
|
um_info.max_entries = 0;
|
|
|
|
um_info.idx = 0;
|
|
|
|
/* Memory ranges to look up */
|
|
|
|
um_info.ranges = &(usable_mem->ranges[0]);
|
|
|
|
um_info.nr_ranges = usable_mem->nr_ranges;
|
|
|
|
|
|
|
|
dn = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
|
|
|
|
if (dn) {
|
|
|
|
ret = walk_drmem_lmbs(dn, &um_info, kdump_setup_usable_lmb);
|
|
|
|
of_node_put(dn);
|
|
|
|
|
|
|
|
if (ret) {
|
|
|
|
pr_err("Could not setup linux,drconf-usable-memory property for kdump\n");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = fdt_setprop(fdt, node, "linux,drconf-usable-memory",
|
|
|
|
um_info.buf, (um_info.idx * sizeof(u64)));
|
|
|
|
if (ret) {
|
2022-12-16 17:57:08 +05:30
|
|
|
pr_err("Failed to update fdt with linux,drconf-usable-memory property: %s",
|
|
|
|
fdt_strerror(ret));
|
2020-07-29 17:12:16 +05:30
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Walk through each memory node and set linux,usable-memory property
|
|
|
|
* for the corresponding node in kdump kernel's fdt.
|
|
|
|
*/
|
|
|
|
for_each_node_by_type(dn, "memory") {
|
|
|
|
ret = add_usable_mem_property(fdt, dn, &um_info);
|
|
|
|
if (ret) {
|
|
|
|
pr_err("Failed to set linux,usable-memory property for %s node",
|
|
|
|
dn->full_name);
|
2021-10-17 21:54:16 -04:00
|
|
|
of_node_put(dn);
|
2020-07-29 17:12:16 +05:30
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
kfree(um_info.buf);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-07-29 17:12:58 +05:30
|
|
|
/**
|
|
|
|
* load_backup_segment - Locate a memory hole to place the backup region.
|
|
|
|
* @image: Kexec image.
|
|
|
|
* @kbuf: Buffer contents and memory parameters.
|
|
|
|
*
|
|
|
|
* Returns 0 on success, negative errno on error.
|
|
|
|
*/
|
|
|
|
static int load_backup_segment(struct kimage *image, struct kexec_buf *kbuf)
|
|
|
|
{
|
|
|
|
void *buf;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Setup a source buffer for backup segment.
|
|
|
|
*
|
|
|
|
* A source buffer has no meaning for backup region as data will
|
|
|
|
* be copied from backup source, after crash, in the purgatory.
|
|
|
|
* But as load segment code doesn't recognize such segments,
|
|
|
|
* setup a dummy source buffer to keep it happy for now.
|
|
|
|
*/
|
|
|
|
buf = vzalloc(BACKUP_SRC_SIZE);
|
|
|
|
if (!buf)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
kbuf->buffer = buf;
|
|
|
|
kbuf->mem = KEXEC_BUF_MEM_UNKNOWN;
|
|
|
|
kbuf->bufsz = kbuf->memsz = BACKUP_SRC_SIZE;
|
|
|
|
kbuf->top_down = false;
|
|
|
|
|
|
|
|
ret = kexec_add_buffer(kbuf);
|
|
|
|
if (ret) {
|
|
|
|
vfree(buf);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
image->arch.backup_buf = buf;
|
|
|
|
image->arch.backup_start = kbuf->mem;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-07-29 17:13:14 +05:30
|
|
|
/**
|
|
|
|
* update_backup_region_phdr - Update backup region's offset for the core to
|
|
|
|
* export the region appropriately.
|
|
|
|
* @image: Kexec image.
|
|
|
|
* @ehdr: ELF core header.
|
|
|
|
*
|
|
|
|
* Assumes an exclusive program header is setup for the backup region
|
|
|
|
* in the ELF headers
|
|
|
|
*
|
|
|
|
* Returns nothing.
|
|
|
|
*/
|
|
|
|
static void update_backup_region_phdr(struct kimage *image, Elf64_Ehdr *ehdr)
|
|
|
|
{
|
|
|
|
Elf64_Phdr *phdr;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
phdr = (Elf64_Phdr *)(ehdr + 1);
|
|
|
|
for (i = 0; i < ehdr->e_phnum; i++) {
|
|
|
|
if (phdr->p_paddr == BACKUP_SRC_START) {
|
|
|
|
phdr->p_offset = image->arch.backup_start;
|
2023-12-13 13:57:46 +08:00
|
|
|
kexec_dprintk("Backup region offset updated to 0x%lx\n",
|
|
|
|
image->arch.backup_start);
|
2020-07-29 17:13:14 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
powerpc/crash: add crash memory hotplug support
Extend the arch crash hotplug handler, as introduced by the patch title
("powerpc: add crash CPU hotplug support"), to also support memory
add/remove events.
Elfcorehdr describes the memory of the crash kernel to capture the
kernel; hence, it needs to be updated if memory resources change due to
memory add/remove events. Therefore, arch_crash_handle_hotplug_event()
is updated to recreate the elfcorehdr and replace it with the previous
one on memory add/remove events.
The memblock list is used to prepare the elfcorehdr. In the case of
memory hot remove, the memblock list is updated after the arch crash
hotplug handler is triggered, as depicted in Figure 1. Thus, the
hot-removed memory is explicitly removed from the crash memory ranges
to ensure that the memory ranges added to elfcorehdr do not include the
hot-removed memory.
Memory remove
|
v
Offline pages
|
v
Initiate memory notify call <----> crash hotplug handler
chain for MEM_OFFLINE event
|
v
Update memblock list
Figure 1
There are two system calls, `kexec_file_load` and `kexec_load`, used to
load the kdump image. A few changes have been made to ensure that the
kernel can safely update the elfcorehdr component of the kdump image for
both system calls.
For the kexec_file_load syscall, kdump image is prepared in the kernel.
To support an increasing number of memory regions, the elfcorehdr is
built with extra buffer space to ensure that it can accommodate
additional memory ranges in future.
For the kexec_load syscall, the elfcorehdr is updated only if the
KEXEC_CRASH_HOTPLUG_SUPPORT kexec flag is passed to the kernel by the
kexec tool. Passing this flag to the kernel indicates that the
elfcorehdr is built to accommodate additional memory ranges and the
elfcorehdr segment is not considered for SHA calculation, making it safe
to update.
The changes related to this feature are kept under the CRASH_HOTPLUG
config, and it is enabled by default.
Signed-off-by: Sourabh Jain <sourabhjain@linux.ibm.com>
Acked-by: Hari Bathini <hbathini@linux.ibm.com>
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
Link: https://msgid.link/20240326055413.186534-7-sourabhjain@linux.ibm.com
2024-03-26 11:24:13 +05:30
|
|
|
static unsigned int kdump_extra_elfcorehdr_size(struct crash_mem *cmem)
|
|
|
|
{
|
|
|
|
#if defined(CONFIG_CRASH_HOTPLUG) && defined(CONFIG_MEMORY_HOTPLUG)
|
|
|
|
unsigned int extra_sz = 0;
|
|
|
|
|
|
|
|
if (CONFIG_CRASH_MAX_MEMORY_RANGES > (unsigned int)PN_XNUM)
|
|
|
|
pr_warn("Number of Phdrs %u exceeds max\n", CONFIG_CRASH_MAX_MEMORY_RANGES);
|
|
|
|
else if (cmem->nr_ranges >= CONFIG_CRASH_MAX_MEMORY_RANGES)
|
|
|
|
pr_warn("Configured crash mem ranges may not be enough\n");
|
|
|
|
else
|
|
|
|
extra_sz = (CONFIG_CRASH_MAX_MEMORY_RANGES - cmem->nr_ranges) * sizeof(Elf64_Phdr);
|
|
|
|
|
|
|
|
return extra_sz;
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-07-29 17:13:14 +05:30
|
|
|
/**
|
|
|
|
* load_elfcorehdr_segment - Setup crash memory ranges and initialize elfcorehdr
|
|
|
|
* segment needed to load kdump kernel.
|
|
|
|
* @image: Kexec image.
|
|
|
|
* @kbuf: Buffer contents and memory parameters.
|
|
|
|
*
|
|
|
|
* Returns 0 on success, negative errno on error.
|
|
|
|
*/
|
|
|
|
static int load_elfcorehdr_segment(struct kimage *image, struct kexec_buf *kbuf)
|
|
|
|
{
|
|
|
|
struct crash_mem *cmem = NULL;
|
|
|
|
unsigned long headers_sz;
|
|
|
|
void *headers = NULL;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = get_crash_memory_ranges(&cmem);
|
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* Setup elfcorehdr segment */
|
|
|
|
ret = crash_prepare_elf64_headers(cmem, false, &headers, &headers_sz);
|
|
|
|
if (ret) {
|
|
|
|
pr_err("Failed to prepare elf headers for the core\n");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fix the offset for backup region in the ELF header */
|
|
|
|
update_backup_region_phdr(image, headers);
|
|
|
|
|
|
|
|
kbuf->buffer = headers;
|
|
|
|
kbuf->mem = KEXEC_BUF_MEM_UNKNOWN;
|
powerpc/crash: add crash memory hotplug support
Extend the arch crash hotplug handler, as introduced by the patch title
("powerpc: add crash CPU hotplug support"), to also support memory
add/remove events.
Elfcorehdr describes the memory of the crash kernel to capture the
kernel; hence, it needs to be updated if memory resources change due to
memory add/remove events. Therefore, arch_crash_handle_hotplug_event()
is updated to recreate the elfcorehdr and replace it with the previous
one on memory add/remove events.
The memblock list is used to prepare the elfcorehdr. In the case of
memory hot remove, the memblock list is updated after the arch crash
hotplug handler is triggered, as depicted in Figure 1. Thus, the
hot-removed memory is explicitly removed from the crash memory ranges
to ensure that the memory ranges added to elfcorehdr do not include the
hot-removed memory.
Memory remove
|
v
Offline pages
|
v
Initiate memory notify call <----> crash hotplug handler
chain for MEM_OFFLINE event
|
v
Update memblock list
Figure 1
There are two system calls, `kexec_file_load` and `kexec_load`, used to
load the kdump image. A few changes have been made to ensure that the
kernel can safely update the elfcorehdr component of the kdump image for
both system calls.
For the kexec_file_load syscall, kdump image is prepared in the kernel.
To support an increasing number of memory regions, the elfcorehdr is
built with extra buffer space to ensure that it can accommodate
additional memory ranges in future.
For the kexec_load syscall, the elfcorehdr is updated only if the
KEXEC_CRASH_HOTPLUG_SUPPORT kexec flag is passed to the kernel by the
kexec tool. Passing this flag to the kernel indicates that the
elfcorehdr is built to accommodate additional memory ranges and the
elfcorehdr segment is not considered for SHA calculation, making it safe
to update.
The changes related to this feature are kept under the CRASH_HOTPLUG
config, and it is enabled by default.
Signed-off-by: Sourabh Jain <sourabhjain@linux.ibm.com>
Acked-by: Hari Bathini <hbathini@linux.ibm.com>
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
Link: https://msgid.link/20240326055413.186534-7-sourabhjain@linux.ibm.com
2024-03-26 11:24:13 +05:30
|
|
|
kbuf->bufsz = headers_sz;
|
|
|
|
kbuf->memsz = headers_sz + kdump_extra_elfcorehdr_size(cmem);
|
2020-07-29 17:13:14 +05:30
|
|
|
kbuf->top_down = false;
|
|
|
|
|
|
|
|
ret = kexec_add_buffer(kbuf);
|
|
|
|
if (ret) {
|
|
|
|
vfree(headers);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2021-02-21 09:49:20 -08:00
|
|
|
image->elf_load_addr = kbuf->mem;
|
|
|
|
image->elf_headers_sz = headers_sz;
|
|
|
|
image->elf_headers = headers;
|
2020-07-29 17:13:14 +05:30
|
|
|
out:
|
|
|
|
kfree(cmem);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-07-29 17:12:58 +05:30
|
|
|
/**
|
|
|
|
* load_crashdump_segments_ppc64 - Initialize the additional segements needed
|
|
|
|
* to load kdump kernel.
|
|
|
|
* @image: Kexec image.
|
|
|
|
* @kbuf: Buffer contents and memory parameters.
|
|
|
|
*
|
|
|
|
* Returns 0 on success, negative errno on error.
|
|
|
|
*/
|
|
|
|
int load_crashdump_segments_ppc64(struct kimage *image,
|
|
|
|
struct kexec_buf *kbuf)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Load backup segment - first 64K bytes of the crashing kernel */
|
|
|
|
ret = load_backup_segment(image, kbuf);
|
|
|
|
if (ret) {
|
|
|
|
pr_err("Failed to load backup segment\n");
|
|
|
|
return ret;
|
|
|
|
}
|
2023-12-13 13:57:46 +08:00
|
|
|
kexec_dprintk("Loaded the backup region at 0x%lx\n", kbuf->mem);
|
2020-07-29 17:12:58 +05:30
|
|
|
|
2020-07-29 17:13:14 +05:30
|
|
|
/* Load elfcorehdr segment - to export crashing kernel's vmcore */
|
|
|
|
ret = load_elfcorehdr_segment(image, kbuf);
|
|
|
|
if (ret) {
|
|
|
|
pr_err("Failed to load elfcorehdr segment\n");
|
|
|
|
return ret;
|
|
|
|
}
|
2023-12-13 13:57:46 +08:00
|
|
|
kexec_dprintk("Loaded elf core header at 0x%lx, bufsz=0x%lx memsz=0x%lx\n",
|
|
|
|
image->elf_load_addr, kbuf->bufsz, kbuf->memsz);
|
2020-07-29 17:13:14 +05:30
|
|
|
|
2020-07-29 17:12:58 +05:30
|
|
|
return 0;
|
|
|
|
}
|
2024-02-26 16:00:09 +05:30
|
|
|
#endif
|
2020-07-29 17:12:58 +05:30
|
|
|
|
2020-07-29 17:09:41 +05:30
|
|
|
/**
|
|
|
|
* setup_purgatory_ppc64 - initialize PPC64 specific purgatory's global
|
|
|
|
* variables and call setup_purgatory() to initialize
|
|
|
|
* common global variable.
|
|
|
|
* @image: kexec image.
|
|
|
|
* @slave_code: Slave code for the purgatory.
|
|
|
|
* @fdt: Flattened device tree for the next kernel.
|
|
|
|
* @kernel_load_addr: Address where the kernel is loaded.
|
|
|
|
* @fdt_load_addr: Address where the flattened device tree is loaded.
|
|
|
|
*
|
|
|
|
* Returns 0 on success, negative errno on error.
|
|
|
|
*/
|
|
|
|
int setup_purgatory_ppc64(struct kimage *image, const void *slave_code,
|
|
|
|
const void *fdt, unsigned long kernel_load_addr,
|
|
|
|
unsigned long fdt_load_addr)
|
|
|
|
{
|
2020-07-29 17:14:06 +05:30
|
|
|
struct device_node *dn = NULL;
|
2020-07-29 17:09:41 +05:30
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = setup_purgatory(image, slave_code, fdt, kernel_load_addr,
|
|
|
|
fdt_load_addr);
|
2020-07-29 17:12:16 +05:30
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (image->type == KEXEC_TYPE_CRASH) {
|
|
|
|
u32 my_run_at_load = 1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Tell relocatable kernel to run at load address
|
|
|
|
* via the word meant for that at 0x5c.
|
|
|
|
*/
|
|
|
|
ret = kexec_purgatory_get_set_symbol(image, "run_at_load",
|
|
|
|
&my_run_at_load,
|
|
|
|
sizeof(my_run_at_load),
|
|
|
|
false);
|
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2020-07-29 17:12:58 +05:30
|
|
|
/* Tell purgatory where to look for backup region */
|
|
|
|
ret = kexec_purgatory_get_set_symbol(image, "backup_start",
|
|
|
|
&image->arch.backup_start,
|
|
|
|
sizeof(image->arch.backup_start),
|
|
|
|
false);
|
2020-07-29 17:14:06 +05:30
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* Setup OPAL base & entry values */
|
|
|
|
dn = of_find_node_by_path("/ibm,opal");
|
|
|
|
if (dn) {
|
|
|
|
u64 val;
|
|
|
|
|
|
|
|
of_property_read_u64(dn, "opal-base-address", &val);
|
|
|
|
ret = kexec_purgatory_get_set_symbol(image, "opal_base", &val,
|
|
|
|
sizeof(val), false);
|
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
of_property_read_u64(dn, "opal-entry-address", &val);
|
|
|
|
ret = kexec_purgatory_get_set_symbol(image, "opal_entry", &val,
|
|
|
|
sizeof(val), false);
|
|
|
|
}
|
2020-07-29 17:12:16 +05:30
|
|
|
out:
|
2020-07-29 17:09:41 +05:30
|
|
|
if (ret)
|
|
|
|
pr_err("Failed to setup purgatory symbols");
|
2020-07-29 17:14:06 +05:30
|
|
|
of_node_put(dn);
|
2020-07-29 17:09:41 +05:30
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
powerpc: Take in account addition CPU node when building kexec FDT
On a system with a large number of CPUs, the creation of the FDT for a
kexec kernel may fail because the allocated FDT is not large enough.
When this happens, such a message is displayed on the console:
Unable to add ibm,processor-vadd-size property: FDT_ERR_NOSPACE
The property's name may change depending when the buffer overwrite is
detected.
Obviously the created FDT is missing information, and it is expected
that system dump or kexec kernel failed to run properly.
When the FDT is allocated, the size of the FDT the kernel received at
boot time is used and an extra size can be applied. Currently, only
memory added after boot time is taken in account, not the CPU nodes.
The extra size should take in account these additional CPU nodes and
compute the required extra space. To achieve that, the size of a CPU
node, including its subnode is computed once and multiplied by the
number of additional CPU nodes.
The assumption is that the size of the CPU node is _same_ for all the
node, the only variable part should be the name "PowerPC,POWERxx@##"
where "##" may vary a little.
Signed-off-by: Laurent Dufour <ldufour@linux.ibm.com>
[mpe: Don't shadow function name w/variable, minor coding style changes]
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
Link: https://lore.kernel.org/r/20221110180619.15796-3-ldufour@linux.ibm.com
2022-11-10 19:06:19 +01:00
|
|
|
/**
|
2023-07-25 15:27:59 +02:00
|
|
|
* cpu_node_size - Compute the size of a CPU node in the FDT.
|
|
|
|
* This should be done only once and the value is stored in
|
|
|
|
* a static variable.
|
powerpc: Take in account addition CPU node when building kexec FDT
On a system with a large number of CPUs, the creation of the FDT for a
kexec kernel may fail because the allocated FDT is not large enough.
When this happens, such a message is displayed on the console:
Unable to add ibm,processor-vadd-size property: FDT_ERR_NOSPACE
The property's name may change depending when the buffer overwrite is
detected.
Obviously the created FDT is missing information, and it is expected
that system dump or kexec kernel failed to run properly.
When the FDT is allocated, the size of the FDT the kernel received at
boot time is used and an extra size can be applied. Currently, only
memory added after boot time is taken in account, not the CPU nodes.
The extra size should take in account these additional CPU nodes and
compute the required extra space. To achieve that, the size of a CPU
node, including its subnode is computed once and multiplied by the
number of additional CPU nodes.
The assumption is that the size of the CPU node is _same_ for all the
node, the only variable part should be the name "PowerPC,POWERxx@##"
where "##" may vary a little.
Signed-off-by: Laurent Dufour <ldufour@linux.ibm.com>
[mpe: Don't shadow function name w/variable, minor coding style changes]
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
Link: https://lore.kernel.org/r/20221110180619.15796-3-ldufour@linux.ibm.com
2022-11-10 19:06:19 +01:00
|
|
|
* Returns the max size of a CPU node in the FDT.
|
|
|
|
*/
|
|
|
|
static unsigned int cpu_node_size(void)
|
|
|
|
{
|
|
|
|
static unsigned int size;
|
|
|
|
struct device_node *dn;
|
|
|
|
struct property *pp;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Don't compute it twice, we are assuming that the per CPU node size
|
|
|
|
* doesn't change during the system's life.
|
|
|
|
*/
|
|
|
|
if (size)
|
|
|
|
return size;
|
|
|
|
|
|
|
|
dn = of_find_node_by_type(NULL, "cpu");
|
|
|
|
if (WARN_ON_ONCE(!dn)) {
|
|
|
|
// Unlikely to happen
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We compute the sub node size for a CPU node, assuming it
|
|
|
|
* will be the same for all.
|
|
|
|
*/
|
|
|
|
size += strlen(dn->name) + 5;
|
|
|
|
for_each_property_of_node(dn, pp) {
|
|
|
|
size += strlen(pp->name);
|
|
|
|
size += pp->length;
|
|
|
|
}
|
|
|
|
|
|
|
|
of_node_put(dn);
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2024-02-26 16:00:09 +05:30
|
|
|
static unsigned int kdump_extra_fdt_size_ppc64(struct kimage *image)
|
2021-02-04 17:01:10 +05:30
|
|
|
{
|
2023-02-10 19:03:58 +11:00
|
|
|
unsigned int cpu_nodes, extra_size = 0;
|
powerpc: Take in account addition CPU node when building kexec FDT
On a system with a large number of CPUs, the creation of the FDT for a
kexec kernel may fail because the allocated FDT is not large enough.
When this happens, such a message is displayed on the console:
Unable to add ibm,processor-vadd-size property: FDT_ERR_NOSPACE
The property's name may change depending when the buffer overwrite is
detected.
Obviously the created FDT is missing information, and it is expected
that system dump or kexec kernel failed to run properly.
When the FDT is allocated, the size of the FDT the kernel received at
boot time is used and an extra size can be applied. Currently, only
memory added after boot time is taken in account, not the CPU nodes.
The extra size should take in account these additional CPU nodes and
compute the required extra space. To achieve that, the size of a CPU
node, including its subnode is computed once and multiplied by the
number of additional CPU nodes.
The assumption is that the size of the CPU node is _same_ for all the
node, the only variable part should be the name "PowerPC,POWERxx@##"
where "##" may vary a little.
Signed-off-by: Laurent Dufour <ldufour@linux.ibm.com>
[mpe: Don't shadow function name w/variable, minor coding style changes]
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
Link: https://lore.kernel.org/r/20221110180619.15796-3-ldufour@linux.ibm.com
2022-11-10 19:06:19 +01:00
|
|
|
struct device_node *dn;
|
2021-02-04 17:01:10 +05:30
|
|
|
u64 usm_entries;
|
powerpc/crash: add crash CPU hotplug support
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.
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.
To address the aforementioned issue, 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.
In the event of a CPU or memory add/remove events, the generic crash
hotplug event handler, `crash_handle_hotplug_event()`, is triggered. It
then acquires the necessary locks to update the kdump image and invokes
the architecture-specific crash hotplug handler,
`arch_crash_handle_hotplug_event()`, to update the required kdump image
components.
This patch adds crash hotplug handler for PowerPC and enable support to
update the kdump image on CPU add/remove events. Support for memory
add/remove events is added in a subsequent patch with the title
"powerpc: add crash memory hotplug support"
As mentioned earlier, only the elfcorehdr and FDT kdump image components
need to be updated in the event of CPU or memory add/remove events.
However, on PowerPC architecture crash hotplug handler only updates the
FDT to enable crash hotplug support for CPU add/remove events. Here's
why.
The elfcorehdr on PowerPC is built with possible CPUs, and thus, it does
not need an update on CPU add/remove events. On the other hand, the FDT
needs to be updated on CPU add events to include the newly added CPU. If
the FDT is not updated and the kernel crashes on a newly added CPU, the
kdump kernel will fail to boot due to the unavailability of the crashing
CPU in the FDT. During the early boot, it is expected that the boot CPU
must be a part of the FDT; otherwise, the kernel will raise a BUG and
fail to boot. For more information, refer to commit 36ae37e3436b0
("powerpc: Make boot_cpuid common between 32 and 64-bit"). Since it is
okay to have an offline CPU in the kdump FDT, no action is taken in case
of CPU removal.
There are two system calls, `kexec_file_load` and `kexec_load`, used to
load the kdump image. Few changes have been made to ensure kernel can
safely update the FDT of kdump image loaded using both system calls.
For kexec_file_load syscall the kdump image is prepared in kernel. So to
support an increasing number of CPUs, the FDT is constructed with extra
buffer space to ensure it can accommodate a possible number of CPU
nodes. Additionally, a call to fdt_pack (which trims the unused space
once the FDT is prepared) is avoided if this feature is enabled.
For the kexec_load syscall, the FDT is updated only if the
KEXEC_CRASH_HOTPLUG_SUPPORT kexec flag is passed to the kernel by
userspace (kexec tools). When userspace passes this flag to the kernel,
it indicates that the FDT is built to accommodate possible CPUs, and the
FDT segment is excluded from SHA calculation, making it safe to update.
The changes related to this feature are kept under the CRASH_HOTPLUG
config, and it is enabled by default.
Signed-off-by: Sourabh Jain <sourabhjain@linux.ibm.com>
Acked-by: Hari Bathini <hbathini@linux.ibm.com>
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
Link: https://msgid.link/20240326055413.186534-6-sourabhjain@linux.ibm.com
2024-03-26 11:24:12 +05:30
|
|
|
#ifdef CONFIG_CRASH_HOTPLUG
|
|
|
|
unsigned int possible_cpu_nodes;
|
|
|
|
#endif
|
2021-02-04 17:01:10 +05:30
|
|
|
|
2024-02-26 16:00:09 +05:30
|
|
|
if (!IS_ENABLED(CONFIG_CRASH_DUMP) || image->type != KEXEC_TYPE_CRASH)
|
|
|
|
return 0;
|
2021-02-04 17:01:10 +05:30
|
|
|
|
|
|
|
/*
|
2021-02-19 21:52:04 -03:00
|
|
|
* For kdump kernel, account for linux,usable-memory and
|
2021-02-04 17:01:10 +05:30
|
|
|
* linux,drconf-usable-memory properties. Get an approximate on the
|
|
|
|
* number of usable memory entries and use for FDT size estimation.
|
|
|
|
*/
|
powerpc/kexec_file: Fix division by zero in extra size estimation
In kexec_extra_fdt_size_ppc64() there's logic to estimate how much
extra space will be needed in the device tree for some memory related
properties.
That logic uses the size of RAM divided by drmem_lmb_size() to do the
estimation. However drmem_lmb_size() can be zero if the machine has no
hotpluggable memory configured, which is the case when booting with qemu
and no maxmem=x parameter is passed (the default).
The division by zero is reported by UBSAN, and can also lead to an
overflow and a warning from kvmalloc, and kdump kernel loading fails:
WARNING: CPU: 0 PID: 133 at mm/util.c:596 kvmalloc_node+0x15c/0x160
Modules linked in:
CPU: 0 PID: 133 Comm: kexec Not tainted 6.2.0-rc5-03455-g07358bd97810 #223
Hardware name: IBM pSeries (emulated by qemu) POWER9 (raw) 0x4e1200 0xf000005 of:SLOF,git-dd0dca pSeries
NIP: c00000000041ff4c LR: c00000000041fe58 CTR: 0000000000000000
REGS: c0000000096ef750 TRAP: 0700 Not tainted (6.2.0-rc5-03455-g07358bd97810)
MSR: 800000000282b033 <SF,VEC,VSX,EE,FP,ME,IR,DR,RI,LE> CR: 24248242 XER: 2004011e
CFAR: c00000000041fed0 IRQMASK: 0
...
NIP kvmalloc_node+0x15c/0x160
LR kvmalloc_node+0x68/0x160
Call Trace:
kvmalloc_node+0x68/0x160 (unreliable)
of_kexec_alloc_and_setup_fdt+0xb8/0x7d0
elf64_load+0x25c/0x4a0
kexec_image_load_default+0x58/0x80
sys_kexec_file_load+0x5c0/0x920
system_call_exception+0x128/0x330
system_call_vectored_common+0x15c/0x2ec
To fix it, skip the calculation if drmem_lmb_size() is zero.
Fixes: 2377c92e37fe ("powerpc/kexec_file: fix FDT size estimation for kdump kernel")
Cc: stable@vger.kernel.org # v5.12+
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
Link: https://lore.kernel.org/r/20230130014707.541110-1-mpe@ellerman.id.au
2023-01-30 12:47:07 +11:00
|
|
|
if (drmem_lmb_size()) {
|
2023-01-31 08:36:15 +05:30
|
|
|
usm_entries = ((memory_hotplug_max() / drmem_lmb_size()) +
|
powerpc/kexec_file: Fix division by zero in extra size estimation
In kexec_extra_fdt_size_ppc64() there's logic to estimate how much
extra space will be needed in the device tree for some memory related
properties.
That logic uses the size of RAM divided by drmem_lmb_size() to do the
estimation. However drmem_lmb_size() can be zero if the machine has no
hotpluggable memory configured, which is the case when booting with qemu
and no maxmem=x parameter is passed (the default).
The division by zero is reported by UBSAN, and can also lead to an
overflow and a warning from kvmalloc, and kdump kernel loading fails:
WARNING: CPU: 0 PID: 133 at mm/util.c:596 kvmalloc_node+0x15c/0x160
Modules linked in:
CPU: 0 PID: 133 Comm: kexec Not tainted 6.2.0-rc5-03455-g07358bd97810 #223
Hardware name: IBM pSeries (emulated by qemu) POWER9 (raw) 0x4e1200 0xf000005 of:SLOF,git-dd0dca pSeries
NIP: c00000000041ff4c LR: c00000000041fe58 CTR: 0000000000000000
REGS: c0000000096ef750 TRAP: 0700 Not tainted (6.2.0-rc5-03455-g07358bd97810)
MSR: 800000000282b033 <SF,VEC,VSX,EE,FP,ME,IR,DR,RI,LE> CR: 24248242 XER: 2004011e
CFAR: c00000000041fed0 IRQMASK: 0
...
NIP kvmalloc_node+0x15c/0x160
LR kvmalloc_node+0x68/0x160
Call Trace:
kvmalloc_node+0x68/0x160 (unreliable)
of_kexec_alloc_and_setup_fdt+0xb8/0x7d0
elf64_load+0x25c/0x4a0
kexec_image_load_default+0x58/0x80
sys_kexec_file_load+0x5c0/0x920
system_call_exception+0x128/0x330
system_call_vectored_common+0x15c/0x2ec
To fix it, skip the calculation if drmem_lmb_size() is zero.
Fixes: 2377c92e37fe ("powerpc/kexec_file: fix FDT size estimation for kdump kernel")
Cc: stable@vger.kernel.org # v5.12+
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
Link: https://lore.kernel.org/r/20230130014707.541110-1-mpe@ellerman.id.au
2023-01-30 12:47:07 +11:00
|
|
|
(2 * (resource_size(&crashk_res) / drmem_lmb_size())));
|
2023-02-10 19:03:58 +11:00
|
|
|
extra_size += (unsigned int)(usm_entries * sizeof(u64));
|
powerpc/kexec_file: Fix division by zero in extra size estimation
In kexec_extra_fdt_size_ppc64() there's logic to estimate how much
extra space will be needed in the device tree for some memory related
properties.
That logic uses the size of RAM divided by drmem_lmb_size() to do the
estimation. However drmem_lmb_size() can be zero if the machine has no
hotpluggable memory configured, which is the case when booting with qemu
and no maxmem=x parameter is passed (the default).
The division by zero is reported by UBSAN, and can also lead to an
overflow and a warning from kvmalloc, and kdump kernel loading fails:
WARNING: CPU: 0 PID: 133 at mm/util.c:596 kvmalloc_node+0x15c/0x160
Modules linked in:
CPU: 0 PID: 133 Comm: kexec Not tainted 6.2.0-rc5-03455-g07358bd97810 #223
Hardware name: IBM pSeries (emulated by qemu) POWER9 (raw) 0x4e1200 0xf000005 of:SLOF,git-dd0dca pSeries
NIP: c00000000041ff4c LR: c00000000041fe58 CTR: 0000000000000000
REGS: c0000000096ef750 TRAP: 0700 Not tainted (6.2.0-rc5-03455-g07358bd97810)
MSR: 800000000282b033 <SF,VEC,VSX,EE,FP,ME,IR,DR,RI,LE> CR: 24248242 XER: 2004011e
CFAR: c00000000041fed0 IRQMASK: 0
...
NIP kvmalloc_node+0x15c/0x160
LR kvmalloc_node+0x68/0x160
Call Trace:
kvmalloc_node+0x68/0x160 (unreliable)
of_kexec_alloc_and_setup_fdt+0xb8/0x7d0
elf64_load+0x25c/0x4a0
kexec_image_load_default+0x58/0x80
sys_kexec_file_load+0x5c0/0x920
system_call_exception+0x128/0x330
system_call_vectored_common+0x15c/0x2ec
To fix it, skip the calculation if drmem_lmb_size() is zero.
Fixes: 2377c92e37fe ("powerpc/kexec_file: fix FDT size estimation for kdump kernel")
Cc: stable@vger.kernel.org # v5.12+
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
Link: https://lore.kernel.org/r/20230130014707.541110-1-mpe@ellerman.id.au
2023-01-30 12:47:07 +11:00
|
|
|
}
|
powerpc: Take in account addition CPU node when building kexec FDT
On a system with a large number of CPUs, the creation of the FDT for a
kexec kernel may fail because the allocated FDT is not large enough.
When this happens, such a message is displayed on the console:
Unable to add ibm,processor-vadd-size property: FDT_ERR_NOSPACE
The property's name may change depending when the buffer overwrite is
detected.
Obviously the created FDT is missing information, and it is expected
that system dump or kexec kernel failed to run properly.
When the FDT is allocated, the size of the FDT the kernel received at
boot time is used and an extra size can be applied. Currently, only
memory added after boot time is taken in account, not the CPU nodes.
The extra size should take in account these additional CPU nodes and
compute the required extra space. To achieve that, the size of a CPU
node, including its subnode is computed once and multiplied by the
number of additional CPU nodes.
The assumption is that the size of the CPU node is _same_ for all the
node, the only variable part should be the name "PowerPC,POWERxx@##"
where "##" may vary a little.
Signed-off-by: Laurent Dufour <ldufour@linux.ibm.com>
[mpe: Don't shadow function name w/variable, minor coding style changes]
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
Link: https://lore.kernel.org/r/20221110180619.15796-3-ldufour@linux.ibm.com
2022-11-10 19:06:19 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the number of CPU nodes in the current DT. This allows to
|
|
|
|
* reserve places for CPU nodes added since the boot time.
|
|
|
|
*/
|
|
|
|
cpu_nodes = 0;
|
|
|
|
for_each_node_by_type(dn, "cpu") {
|
|
|
|
cpu_nodes++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cpu_nodes > boot_cpu_node_count)
|
|
|
|
extra_size += (cpu_nodes - boot_cpu_node_count) * cpu_node_size();
|
|
|
|
|
powerpc/crash: add crash CPU hotplug support
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.
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.
To address the aforementioned issue, 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.
In the event of a CPU or memory add/remove events, the generic crash
hotplug event handler, `crash_handle_hotplug_event()`, is triggered. It
then acquires the necessary locks to update the kdump image and invokes
the architecture-specific crash hotplug handler,
`arch_crash_handle_hotplug_event()`, to update the required kdump image
components.
This patch adds crash hotplug handler for PowerPC and enable support to
update the kdump image on CPU add/remove events. Support for memory
add/remove events is added in a subsequent patch with the title
"powerpc: add crash memory hotplug support"
As mentioned earlier, only the elfcorehdr and FDT kdump image components
need to be updated in the event of CPU or memory add/remove events.
However, on PowerPC architecture crash hotplug handler only updates the
FDT to enable crash hotplug support for CPU add/remove events. Here's
why.
The elfcorehdr on PowerPC is built with possible CPUs, and thus, it does
not need an update on CPU add/remove events. On the other hand, the FDT
needs to be updated on CPU add events to include the newly added CPU. If
the FDT is not updated and the kernel crashes on a newly added CPU, the
kdump kernel will fail to boot due to the unavailability of the crashing
CPU in the FDT. During the early boot, it is expected that the boot CPU
must be a part of the FDT; otherwise, the kernel will raise a BUG and
fail to boot. For more information, refer to commit 36ae37e3436b0
("powerpc: Make boot_cpuid common between 32 and 64-bit"). Since it is
okay to have an offline CPU in the kdump FDT, no action is taken in case
of CPU removal.
There are two system calls, `kexec_file_load` and `kexec_load`, used to
load the kdump image. Few changes have been made to ensure kernel can
safely update the FDT of kdump image loaded using both system calls.
For kexec_file_load syscall the kdump image is prepared in kernel. So to
support an increasing number of CPUs, the FDT is constructed with extra
buffer space to ensure it can accommodate a possible number of CPU
nodes. Additionally, a call to fdt_pack (which trims the unused space
once the FDT is prepared) is avoided if this feature is enabled.
For the kexec_load syscall, the FDT is updated only if the
KEXEC_CRASH_HOTPLUG_SUPPORT kexec flag is passed to the kernel by
userspace (kexec tools). When userspace passes this flag to the kernel,
it indicates that the FDT is built to accommodate possible CPUs, and the
FDT segment is excluded from SHA calculation, making it safe to update.
The changes related to this feature are kept under the CRASH_HOTPLUG
config, and it is enabled by default.
Signed-off-by: Sourabh Jain <sourabhjain@linux.ibm.com>
Acked-by: Hari Bathini <hbathini@linux.ibm.com>
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
Link: https://msgid.link/20240326055413.186534-6-sourabhjain@linux.ibm.com
2024-03-26 11:24:12 +05:30
|
|
|
#ifdef CONFIG_CRASH_HOTPLUG
|
|
|
|
/*
|
|
|
|
* Make sure enough space is reserved to accommodate possible CPU nodes
|
|
|
|
* in the crash FDT. This allows packing possible CPU nodes which are
|
|
|
|
* not yet present in the system without regenerating the entire FDT.
|
|
|
|
*/
|
|
|
|
if (image->type == KEXEC_TYPE_CRASH) {
|
|
|
|
possible_cpu_nodes = num_possible_cpus() / threads_per_core;
|
|
|
|
if (possible_cpu_nodes > cpu_nodes)
|
|
|
|
extra_size += (possible_cpu_nodes - cpu_nodes) * cpu_node_size();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
powerpc: Take in account addition CPU node when building kexec FDT
On a system with a large number of CPUs, the creation of the FDT for a
kexec kernel may fail because the allocated FDT is not large enough.
When this happens, such a message is displayed on the console:
Unable to add ibm,processor-vadd-size property: FDT_ERR_NOSPACE
The property's name may change depending when the buffer overwrite is
detected.
Obviously the created FDT is missing information, and it is expected
that system dump or kexec kernel failed to run properly.
When the FDT is allocated, the size of the FDT the kernel received at
boot time is used and an extra size can be applied. Currently, only
memory added after boot time is taken in account, not the CPU nodes.
The extra size should take in account these additional CPU nodes and
compute the required extra space. To achieve that, the size of a CPU
node, including its subnode is computed once and multiplied by the
number of additional CPU nodes.
The assumption is that the size of the CPU node is _same_ for all the
node, the only variable part should be the name "PowerPC,POWERxx@##"
where "##" may vary a little.
Signed-off-by: Laurent Dufour <ldufour@linux.ibm.com>
[mpe: Don't shadow function name w/variable, minor coding style changes]
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
Link: https://lore.kernel.org/r/20221110180619.15796-3-ldufour@linux.ibm.com
2022-11-10 19:06:19 +01:00
|
|
|
return extra_size;
|
2021-02-04 17:01:10 +05:30
|
|
|
}
|
|
|
|
|
2024-02-26 16:00:09 +05:30
|
|
|
/**
|
|
|
|
* kexec_extra_fdt_size_ppc64 - Return the estimated additional size needed to
|
|
|
|
* setup FDT for kexec/kdump kernel.
|
|
|
|
* @image: kexec image being loaded.
|
|
|
|
*
|
|
|
|
* Returns the estimated extra size needed for kexec/kdump kernel FDT.
|
|
|
|
*/
|
|
|
|
unsigned int kexec_extra_fdt_size_ppc64(struct kimage *image)
|
|
|
|
{
|
|
|
|
unsigned int extra_size = 0;
|
|
|
|
|
|
|
|
// Budget some space for the password blob. There's already extra space
|
|
|
|
// for the key name
|
|
|
|
if (plpks_is_available())
|
|
|
|
extra_size += (unsigned int)plpks_get_passwordlen();
|
|
|
|
|
|
|
|
return extra_size + kdump_extra_fdt_size_ppc64(image);
|
|
|
|
}
|
|
|
|
|
2022-06-29 16:06:14 +10:00
|
|
|
static int copy_property(void *fdt, int node_offset, const struct device_node *dn,
|
|
|
|
const char *propname)
|
|
|
|
{
|
|
|
|
const void *prop, *fdtprop;
|
2022-08-10 15:43:31 +10:00
|
|
|
int len = 0, fdtlen = 0;
|
2022-06-29 16:06:14 +10:00
|
|
|
|
|
|
|
prop = of_get_property(dn, propname, &len);
|
|
|
|
fdtprop = fdt_getprop(fdt, node_offset, propname, &fdtlen);
|
|
|
|
|
|
|
|
if (fdtprop && !prop)
|
2022-08-10 15:43:31 +10:00
|
|
|
return fdt_delprop(fdt, node_offset, propname);
|
2022-06-29 16:06:14 +10:00
|
|
|
else if (prop)
|
2022-08-10 15:43:31 +10:00
|
|
|
return fdt_setprop(fdt, node_offset, propname, prop, len);
|
|
|
|
else
|
|
|
|
return -FDT_ERR_NOTFOUND;
|
2022-06-29 16:06:14 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
static int update_pci_dma_nodes(void *fdt, const char *dmapropname)
|
|
|
|
{
|
|
|
|
struct device_node *dn;
|
|
|
|
int pci_offset, root_offset, ret = 0;
|
|
|
|
|
|
|
|
if (!firmware_has_feature(FW_FEATURE_LPAR))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
root_offset = fdt_path_offset(fdt, "/");
|
|
|
|
for_each_node_with_property(dn, dmapropname) {
|
|
|
|
pci_offset = fdt_subnode_offset(fdt, root_offset, of_node_full_name(dn));
|
|
|
|
if (pci_offset < 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
ret = copy_property(fdt, pci_offset, dn, "ibm,dma-window");
|
2023-09-07 11:55:16 +02:00
|
|
|
if (ret < 0) {
|
|
|
|
of_node_put(dn);
|
2022-06-29 16:06:14 +10:00
|
|
|
break;
|
2023-09-07 11:55:16 +02:00
|
|
|
}
|
2022-06-29 16:06:14 +10:00
|
|
|
ret = copy_property(fdt, pci_offset, dn, dmapropname);
|
2023-09-07 11:55:16 +02:00
|
|
|
if (ret < 0) {
|
|
|
|
of_node_put(dn);
|
2022-06-29 16:06:14 +10:00
|
|
|
break;
|
2023-09-07 11:55:16 +02:00
|
|
|
}
|
2022-06-29 16:06:14 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-07-29 17:09:41 +05:30
|
|
|
/**
|
|
|
|
* setup_new_fdt_ppc64 - Update the flattend device-tree of the kernel
|
|
|
|
* being loaded.
|
|
|
|
* @image: kexec image being loaded.
|
|
|
|
* @fdt: Flattened device tree for the next kernel.
|
|
|
|
* @initrd_load_addr: Address where the next initrd will be loaded.
|
|
|
|
* @initrd_len: Size of the next initrd, or 0 if there will be none.
|
|
|
|
* @cmdline: Command line for the next kernel, or NULL if there will
|
|
|
|
* be none.
|
|
|
|
*
|
|
|
|
* Returns 0 on success, negative errno on error.
|
|
|
|
*/
|
|
|
|
int setup_new_fdt_ppc64(const struct kimage *image, void *fdt,
|
|
|
|
unsigned long initrd_load_addr,
|
|
|
|
unsigned long initrd_len, const char *cmdline)
|
|
|
|
{
|
2020-07-29 17:13:33 +05:30
|
|
|
struct crash_mem *umem = NULL, *rmem = NULL;
|
|
|
|
int i, nr_ranges, ret;
|
2020-07-29 17:12:16 +05:30
|
|
|
|
2024-02-26 16:00:09 +05:30
|
|
|
#ifdef CONFIG_CRASH_DUMP
|
2020-07-29 17:12:16 +05:30
|
|
|
/*
|
|
|
|
* Restrict memory usage for kdump kernel by setting up
|
2020-07-29 17:12:58 +05:30
|
|
|
* usable memory ranges and memory reserve map.
|
2020-07-29 17:12:16 +05:30
|
|
|
*/
|
|
|
|
if (image->type == KEXEC_TYPE_CRASH) {
|
|
|
|
ret = get_usable_memory_ranges(&umem);
|
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
ret = update_usable_mem_fdt(fdt, umem);
|
|
|
|
if (ret) {
|
|
|
|
pr_err("Error setting up usable-memory property for kdump kernel\n");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2020-07-29 17:12:58 +05:30
|
|
|
/*
|
|
|
|
* Ensure we don't touch crashed kernel's memory except the
|
|
|
|
* first 64K of RAM, which will be backed up.
|
|
|
|
*/
|
|
|
|
ret = fdt_add_mem_rsv(fdt, BACKUP_SRC_END + 1,
|
|
|
|
crashk_res.start - BACKUP_SRC_SIZE);
|
2020-07-29 17:12:16 +05:30
|
|
|
if (ret) {
|
|
|
|
pr_err("Error reserving crash memory: %s\n",
|
|
|
|
fdt_strerror(ret));
|
|
|
|
goto out;
|
|
|
|
}
|
2020-07-29 17:12:58 +05:30
|
|
|
|
|
|
|
/* Ensure backup region is not used by kdump/capture kernel */
|
|
|
|
ret = fdt_add_mem_rsv(fdt, image->arch.backup_start,
|
|
|
|
BACKUP_SRC_SIZE);
|
|
|
|
if (ret) {
|
|
|
|
pr_err("Error reserving memory for backup: %s\n",
|
|
|
|
fdt_strerror(ret));
|
|
|
|
goto out;
|
|
|
|
}
|
2020-07-29 17:12:16 +05:30
|
|
|
}
|
2024-02-26 16:00:09 +05:30
|
|
|
#endif
|
2020-07-29 17:12:16 +05:30
|
|
|
|
2021-04-29 11:32:56 +05:30
|
|
|
/* Update cpus nodes information to account hotplug CPUs. */
|
|
|
|
ret = update_cpus_node(fdt);
|
|
|
|
if (ret < 0)
|
|
|
|
goto out;
|
|
|
|
|
2022-06-29 16:06:14 +10:00
|
|
|
ret = update_pci_dma_nodes(fdt, DIRECT64_PROPNAME);
|
|
|
|
if (ret < 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
ret = update_pci_dma_nodes(fdt, DMA64_PROPNAME);
|
|
|
|
if (ret < 0)
|
|
|
|
goto out;
|
|
|
|
|
2020-07-29 17:13:33 +05:30
|
|
|
/* Update memory reserve map */
|
|
|
|
ret = get_reserved_memory_ranges(&rmem);
|
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
nr_ranges = rmem ? rmem->nr_ranges : 0;
|
|
|
|
for (i = 0; i < nr_ranges; i++) {
|
|
|
|
u64 base, size;
|
|
|
|
|
|
|
|
base = rmem->ranges[i].start;
|
|
|
|
size = rmem->ranges[i].end - base + 1;
|
|
|
|
ret = fdt_add_mem_rsv(fdt, base, size);
|
|
|
|
if (ret) {
|
|
|
|
pr_err("Error updating memory reserve map: %s\n",
|
|
|
|
fdt_strerror(ret));
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-10 19:03:58 +11:00
|
|
|
// If we have PLPKS active, we need to provide the password to the new kernel
|
|
|
|
if (plpks_is_available())
|
|
|
|
ret = plpks_populate_fdt(fdt);
|
|
|
|
|
2020-07-29 17:12:16 +05:30
|
|
|
out:
|
2020-07-29 17:13:33 +05:30
|
|
|
kfree(rmem);
|
2020-07-29 17:12:16 +05:30
|
|
|
kfree(umem);
|
|
|
|
return ret;
|
2020-07-29 17:09:41 +05:30
|
|
|
}
|
|
|
|
|
2020-07-29 17:10:16 +05:30
|
|
|
/**
|
|
|
|
* arch_kexec_locate_mem_hole - Skip special memory regions like rtas, opal,
|
|
|
|
* tce-table, reserved-ranges & such (exclude
|
|
|
|
* memory ranges) as they can't be used for kexec
|
|
|
|
* segment buffer. Sets kbuf->mem when a suitable
|
|
|
|
* memory hole is found.
|
|
|
|
* @kbuf: Buffer contents and memory parameters.
|
|
|
|
*
|
|
|
|
* Assumes minimum of PAGE_SIZE alignment for kbuf->memsz & kbuf->buf_align.
|
|
|
|
*
|
|
|
|
* Returns 0 on success, negative errno on error.
|
|
|
|
*/
|
|
|
|
int arch_kexec_locate_mem_hole(struct kexec_buf *kbuf)
|
|
|
|
{
|
|
|
|
struct crash_mem **emem;
|
|
|
|
u64 buf_min, buf_max;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Look up the exclude ranges list while locating the memory hole */
|
|
|
|
emem = &(kbuf->image->arch.exclude_ranges);
|
|
|
|
if (!(*emem) || ((*emem)->nr_ranges == 0)) {
|
|
|
|
pr_warn("No exclude range list. Using the default locate mem hole method\n");
|
|
|
|
return kexec_locate_mem_hole(kbuf);
|
|
|
|
}
|
|
|
|
|
2020-07-29 17:13:49 +05:30
|
|
|
buf_min = kbuf->buf_min;
|
|
|
|
buf_max = kbuf->buf_max;
|
2020-07-29 17:10:16 +05:30
|
|
|
/* Segments for kdump kernel should be within crashkernel region */
|
2024-02-26 16:00:09 +05:30
|
|
|
if (IS_ENABLED(CONFIG_CRASH_DUMP) && kbuf->image->type == KEXEC_TYPE_CRASH) {
|
2020-07-29 17:13:49 +05:30
|
|
|
buf_min = (buf_min < crashk_res.start ?
|
|
|
|
crashk_res.start : buf_min);
|
|
|
|
buf_max = (buf_max > crashk_res.end ?
|
|
|
|
crashk_res.end : buf_max);
|
|
|
|
}
|
2020-07-29 17:10:16 +05:30
|
|
|
|
|
|
|
if (buf_min > buf_max) {
|
|
|
|
pr_err("Invalid buffer min and/or max values\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (kbuf->top_down)
|
|
|
|
ret = locate_mem_hole_top_down_ppc64(kbuf, buf_min, buf_max,
|
|
|
|
*emem);
|
|
|
|
else
|
|
|
|
ret = locate_mem_hole_bottom_up_ppc64(kbuf, buf_min, buf_max,
|
|
|
|
*emem);
|
|
|
|
|
|
|
|
/* Add the buffer allocated to the exclude list for the next lookup */
|
|
|
|
if (!ret) {
|
|
|
|
add_mem_range(emem, kbuf->mem, kbuf->memsz);
|
|
|
|
sort_memory_ranges(*emem, true);
|
|
|
|
} else {
|
|
|
|
pr_err("Failed to locate memory buffer of size %lu\n",
|
|
|
|
kbuf->memsz);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-07-29 17:09:41 +05:30
|
|
|
/**
|
|
|
|
* arch_kexec_kernel_image_probe - Does additional handling needed to setup
|
|
|
|
* kexec segments.
|
|
|
|
* @image: kexec image being loaded.
|
|
|
|
* @buf: Buffer pointing to elf data.
|
|
|
|
* @buf_len: Length of the buffer.
|
|
|
|
*
|
|
|
|
* Returns 0 on success, negative errno on error.
|
|
|
|
*/
|
|
|
|
int arch_kexec_kernel_image_probe(struct kimage *image, void *buf,
|
|
|
|
unsigned long buf_len)
|
|
|
|
{
|
2020-07-29 17:13:49 +05:30
|
|
|
int ret;
|
2020-07-29 17:10:16 +05:30
|
|
|
|
2020-07-29 17:13:49 +05:30
|
|
|
/* Get exclude memory ranges needed for setting up kexec segments */
|
|
|
|
ret = get_exclude_memory_ranges(&(image->arch.exclude_ranges));
|
|
|
|
if (ret) {
|
|
|
|
pr_err("Failed to setup exclude memory ranges for buffer lookup\n");
|
|
|
|
return ret;
|
2020-07-29 17:10:16 +05:30
|
|
|
}
|
2020-07-29 17:09:41 +05:30
|
|
|
|
|
|
|
return kexec_image_probe_default(image, buf, buf_len);
|
|
|
|
}
|
2020-07-29 17:10:16 +05:30
|
|
|
|
|
|
|
/**
|
|
|
|
* arch_kimage_file_post_load_cleanup - Frees up all the allocations done
|
|
|
|
* while loading the image.
|
|
|
|
* @image: kexec image being loaded.
|
|
|
|
*
|
|
|
|
* Returns 0 on success, negative errno on error.
|
|
|
|
*/
|
|
|
|
int arch_kimage_file_post_load_cleanup(struct kimage *image)
|
|
|
|
{
|
|
|
|
kfree(image->arch.exclude_ranges);
|
|
|
|
image->arch.exclude_ranges = NULL;
|
|
|
|
|
2020-07-29 17:12:58 +05:30
|
|
|
vfree(image->arch.backup_buf);
|
|
|
|
image->arch.backup_buf = NULL;
|
|
|
|
|
2021-02-21 09:49:20 -08:00
|
|
|
vfree(image->elf_headers);
|
|
|
|
image->elf_headers = NULL;
|
|
|
|
image->elf_headers_sz = 0;
|
2020-07-29 17:13:14 +05:30
|
|
|
|
2021-02-21 09:49:24 -08:00
|
|
|
kvfree(image->arch.fdt);
|
|
|
|
image->arch.fdt = NULL;
|
|
|
|
|
2020-07-29 17:10:16 +05:30
|
|
|
return kexec_image_post_load_cleanup_default(image);
|
|
|
|
}
|