delete_fdt_mem_rsv() retrieves the memory reserve map entry, for the given starting address and size, from the device tree blob, and removes the entry from the device tree blob. This function is called to free the resources reserved for the buffer used for carrying forward the IMA measurement logs on kexec. This function does not have architecture specific code, but is currently limited to powerpc. Move delete_fdt_mem_rsv() to "drivers/of/kexec_fdt.c" so that it is accessible for other architectures as well. Co-developed-by: Prakhar Srivastava <prsriva@xxxxxxxxxxxxxxxxxxx> Signed-off-by: Prakhar Srivastava <prsriva@xxxxxxxxxxxxxxxxxxx> Signed-off-by: Lakshmi Ramasubramanian <nramas@xxxxxxxxxxxxxxxxxxx> --- arch/powerpc/include/asm/kexec.h | 1 - arch/powerpc/kexec/file_load.c | 32 ------------------- drivers/of/Makefile | 6 ++++ drivers/of/kexec_fdt.c | 55 ++++++++++++++++++++++++++++++++ include/linux/kexec.h | 11 +++++++ 5 files changed, 72 insertions(+), 33 deletions(-) create mode 100644 drivers/of/kexec_fdt.c diff --git a/arch/powerpc/include/asm/kexec.h b/arch/powerpc/include/asm/kexec.h index 55d6ede30c19..7c223031ecdd 100644 --- a/arch/powerpc/include/asm/kexec.h +++ b/arch/powerpc/include/asm/kexec.h @@ -126,7 +126,6 @@ int setup_purgatory(struct kimage *image, const void *slave_code, int setup_new_fdt(const struct kimage *image, void *fdt, unsigned long initrd_load_addr, unsigned long initrd_len, const char *cmdline); -int delete_fdt_mem_rsv(void *fdt, unsigned long start, unsigned long size); #ifdef CONFIG_PPC64 struct kexec_buf; diff --git a/arch/powerpc/kexec/file_load.c b/arch/powerpc/kexec/file_load.c index 9a232bc36c8f..9efc98b1e2ae 100644 --- a/arch/powerpc/kexec/file_load.c +++ b/arch/powerpc/kexec/file_load.c @@ -109,38 +109,6 @@ int setup_purgatory(struct kimage *image, const void *slave_code, return 0; } -/** - * delete_fdt_mem_rsv - delete memory reservation with given address and size - * - * Return: 0 on success, or negative errno on error. - */ -int delete_fdt_mem_rsv(void *fdt, unsigned long start, unsigned long size) -{ - int i, ret, num_rsvs = fdt_num_mem_rsv(fdt); - - for (i = 0; i < num_rsvs; i++) { - uint64_t rsv_start, rsv_size; - - ret = fdt_get_mem_rsv(fdt, i, &rsv_start, &rsv_size); - if (ret) { - pr_err("Malformed device tree.\n"); - return -EINVAL; - } - - if (rsv_start == start && rsv_size == size) { - ret = fdt_del_mem_rsv(fdt, i); - if (ret) { - pr_err("Error deleting device tree reservation.\n"); - return -EINVAL; - } - - return 0; - } - } - - return -ENOENT; -} - /* * setup_new_fdt - modify /chosen and memory reservation for the next kernel * @image: kexec image being loaded. diff --git a/drivers/of/Makefile b/drivers/of/Makefile index 6e1e5212f058..5f42aa3bbac6 100644 --- a/drivers/of/Makefile +++ b/drivers/of/Makefile @@ -14,4 +14,10 @@ obj-$(CONFIG_OF_RESOLVE) += resolver.o obj-$(CONFIG_OF_OVERLAY) += overlay.o obj-$(CONFIG_OF_NUMA) += of_numa.o +ifdef CONFIG_OF_FLATTREE +ifdef CONFIG_KEXEC_FILE +obj-y += kexec_fdt.o +endif +endif + obj-$(CONFIG_OF_UNITTEST) += unittest-data/ diff --git a/drivers/of/kexec_fdt.c b/drivers/of/kexec_fdt.c new file mode 100644 index 000000000000..995d0ad22d0f --- /dev/null +++ b/drivers/of/kexec_fdt.c @@ -0,0 +1,55 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright (C) 2020 Microsoft Corporation + * + * Author: Lakshmi Ramasubramanian (nramas@xxxxxxxxxxxxxxxxxxx) + * + * File: kexec_fdt.c + * Defines functions to handle memory reservation for buffers + * used to carry data across kexec system call. + */ + +#define pr_fmt(fmt) "OF: kexec-fdt: " fmt + +#include <linux/slab.h> +#include <linux/kexec.h> +#include <linux/of_fdt.h> +#include <linux/libfdt.h> + +/** + * delete_fdt_mem_rsv - delete memory reservation with given address and size + * + * @fdt: Flattened Device Tree to update + * @start: Starting address of the reservation to delete + * @size: Size of the reservation to delete + * + * Return: 0 on success, or negative errno on error. + */ +int delete_fdt_mem_rsv(void *fdt, unsigned long start, unsigned long size) +{ + int i, ret, num_rsvs = fdt_num_mem_rsv(fdt); + + for (i = 0; i < num_rsvs; i++) { + uint64_t rsv_start, rsv_size; + + ret = fdt_get_mem_rsv(fdt, i, &rsv_start, &rsv_size); + if (ret) { + pr_err("Malformed device tree.\n"); + return -EINVAL; + } + + if (rsv_start == start && rsv_size == size) { + ret = fdt_del_mem_rsv(fdt, i); + if (ret) { + pr_err("Error deleting device tree reservation.\n"); + return -EINVAL; + } + + pr_debug("Freed reserved memory at %lu of size %lu\n", + start, size); + return 0; + } + } + + return -ENOENT; +} diff --git a/include/linux/kexec.h b/include/linux/kexec.h index 9e93bef52968..4ce09ccf2628 100644 --- a/include/linux/kexec.h +++ b/include/linux/kexec.h @@ -407,6 +407,17 @@ static inline int kexec_crash_loaded(void) { return 0; } #define kexec_in_progress false #endif /* CONFIG_KEXEC_CORE */ +#if defined(CONFIG_OF_FLATTREE) && defined(CONFIG_KEXEC_FILE) +extern int delete_fdt_mem_rsv(void *fdt, unsigned long start, + unsigned long size); +#else +static inline int delete_fdt_mem_rsv(void *fdt, unsigned long start, + unsigned long size) +{ + return 0; +} +#endif /* CONFIG_OF_FLATTREE && CONFIG_KEXEC_FILE */ + #endif /* !defined(__ASSEBMLY__) */ #endif /* LINUX_KEXEC_H */ -- 2.29.2