[PATCH 2/2] MIPS: kdump: Provide arch_kexec_protect(unprotect)_crashkres()

From: Youling Tang
Date: Fri Apr 16 2021 - 04:48:32 EST


Commit 9b492cf58077 ("kexec: introduce a protection mechanism for the
crashkernel reserved memory") , provides a mechanism to protect the
memory reserved by the crashed kernel.

1) After each crash kexec loading, it simply marks the reserved memory
regions readonly since we no longer access it after that. When someone
stamps the region, the first kernel will panic and trigger the kdump.
This arch_kexec_protect_crashkres() will actually protect the reserved
memory.

2) To allow multiple loading, once 1) was done we also need to remark
the reserved memory to readwrite each time a system call related to
kdump is made. This arch_kexec_unprotect_crashkres() will undo the
protection of the reserved memory.

Signed-off-by: Youling Tang <tangyouling@xxxxxxxxxxx>
---
arch/mips/kernel/machine_kexec.c | 40 ++++++++++++++++++++++++++++++++++++++++
1 file changed, 40 insertions(+)

diff --git a/arch/mips/kernel/machine_kexec.c b/arch/mips/kernel/machine_kexec.c
index 432bfd3..379c2c3 100644
--- a/arch/mips/kernel/machine_kexec.c
+++ b/arch/mips/kernel/machine_kexec.c
@@ -11,6 +11,7 @@

#include <asm/cacheflush.h>
#include <asm/page.h>
+#include <asm/set_memory.h>

extern const unsigned char relocate_new_kernel[];
extern const size_t relocate_new_kernel_size;
@@ -205,6 +206,45 @@ void kexec_reboot(void)
do_kexec();
}

+static int
+kexec_mark_range(unsigned long start, unsigned long end, bool protect)
+{
+ struct page *page;
+ unsigned int nr_pages;
+
+ /*
+ * For physical range: [start, end]. We must skip the unassigned
+ * crashk resource with zero-valued "end" member.
+ */
+ if (!end || start > end)
+ return 0;
+
+ page = pfn_to_page(start >> PAGE_SHIFT);
+ nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
+ if (protect)
+ return set_pages_ro(page, nr_pages);
+ else
+ return set_pages_rw(page, nr_pages);
+}
+
+static void
+kexec_mark_crashkres(bool protect)
+{
+ kexec_mark_range(crashk_res.start, crashk_res.end, protect);
+}
+
+void
+arch_kexec_protect_crashkres(void)
+{
+ kexec_mark_crashkres(true);
+}
+
+void
+arch_kexec_unprotect_crashkres(void)
+{
+ kexec_mark_crashkres(false);
+}
+
void
machine_kexec(struct kimage *image)
{
--
2.1.0