[PATCH 4/4] arm64: kexec_image: Implement arch_kexec_locate_mem_hole()

From: Marc Zyngier
Date: Wed May 26 2021 - 15:06:03 EST


Provide an arm64-specific implementation for arch_kexec_locate_mem_hole(),
using the resource tree instead of memblock, and respecting
the reservations added by EFI.

This ensures that kexec_file is finally reliable.

Reported-by: Moritz Fischer <mdf@xxxxxxxxxx>
Signed-off-by: Marc Zyngier <maz@xxxxxxxxxx>
---
arch/arm64/kernel/kexec_image.c | 45 +++++++++++++++++++++++++++++++++
1 file changed, 45 insertions(+)

diff --git a/arch/arm64/kernel/kexec_image.c b/arch/arm64/kernel/kexec_image.c
index 9ec34690e255..91e852c735df 100644
--- a/arch/arm64/kernel/kexec_image.c
+++ b/arch/arm64/kernel/kexec_image.c
@@ -145,3 +145,48 @@ const struct kexec_file_ops kexec_image_ops = {
.verify_sig = image_verify_sig,
#endif
};
+
+static int filter_out_reserved_res(struct resource *res, void *arg)
+{
+ return walk_excluding_child_res(res, arg,
+ kexec_locate_mem_hole_callback);
+}
+
+/**
+ * arch_kexec_locate_mem_hole - Find free memory to place the segments.
+ * @kbuf: Parameters for the memory search.
+ *
+ * On success, kbuf->mem will have the start address of the memory region found.
+ *
+ * Return: 0 on success, negative errno on error.
+ */
+int arch_kexec_locate_mem_hole(struct kexec_buf *kbuf)
+{
+ int ret;
+
+ /* Arch knows where to place */
+ if (kbuf->mem != KEXEC_BUF_MEM_UNKNOWN)
+ return 0;
+
+ /*
+ * Crash kernels land in a well known place that has been
+ * reserved upfront.
+ *
+ * Normal kexec kernels can however land anywhere in memory.
+ * We have to be extra careful not to step over critical
+ * memory ranges that have been marked as reserved in the
+ * iomem resource tree (LPI and ACPI tables, among others),
+ * hence the use of the child-excluding iterator. This
+ * matches what the userspace version of kexec does.
+ */
+ if (kbuf->image->type == KEXEC_TYPE_CRASH)
+ ret = walk_iomem_res_desc(crashk_res.desc,
+ IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY,
+ crashk_res.start, crashk_res.end,
+ kbuf, kexec_locate_mem_hole_callback);
+ else
+ ret = walk_system_ram_res(0, ULONG_MAX, kbuf,
+ filter_out_reserved_res);
+
+ return ret == 1 ? 0 : -EADDRNOTAVAIL;
+}
--
2.30.2