diff mbox series

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

Message ID 1618562842-20915-2-git-send-email-tangyouling@loongson.cn (mailing list archive)
State New
Headers show
Series [1/2] MIPS: mm: Add ARCH_HAS_SET_MEMORY support | expand

Commit Message

Youling Tang April 16, 2021, 8:47 a.m. UTC
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@loongson.cn>
---
 arch/mips/kernel/machine_kexec.c | 40 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)
diff mbox series

Patch

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)
 {