linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Youling Tang <tangyouling@loongson.cn>
To: Thomas Bogendoerfer <tsbogend@alpha.franken.de>
Cc: Jiaxun Yang <jiaxun.yang@flygoat.com>,
	Baoquan He <bhe@redhat.com>, Huacai Chen <chenhuacai@kernel.org>,
	Jinyang He <hejinyang@loongson.cn>,
	kexec@lists.infradead.org, linux-mips@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: [PATCH v2 4/4] mips: kdump: Reserve old memory to avoid the destruction of production kernel data
Date: Thu, 22 Apr 2021 10:24:34 +0800	[thread overview]
Message-ID: <1619058274-6996-5-git-send-email-tangyouling@loongson.cn> (raw)
In-Reply-To: <1619058274-6996-1-git-send-email-tangyouling@loongson.cn>

From: Huacai Chen <chenhc@lemote.com>

Memory layout:

+---------+ end_pfn(e0+128M)
|         |
+---------+ e0
|         |
|         |
|         |
+---------+ e1(crashk_res.start)
|         |
|         |
|         |
+---------+ s1(crashk_res.start)
|         |
+---------+ s0(start_pfn)

[1] When producing the kernel:
Reserve the crashkernel space through crashkernel="YM@XM", so that
[s1, e1] is reserved for the capture kernel.

If the available memory range is greater than 1G, an additional 128M
range is reserved from top to bottom for the capture kernel (ie
[e0, end_pfn] range). The advantage of this is that it can make more
memory available to the capture kernel and avoid triggering insufficient
memory, resulting in panic.

[2] When capturing the kernel:
Finally, the "mem=" parameter is automatically added through kexec-tools
(the "mem=" parameter actually comes from the "crashkernel=" parameter,
and the scope is the same).

It is necessary to reserve the available memory area of the previous
production kernel to avoid the captured data of the production kernel
from being destroyed. If this area in the memory is not reserved, the
captured data will be destroyed, the generated vmcore file is invalid
and cannot be parsed by the crash-utility.

[3] Only consider the memory situation of kdump operation as follows:
1. Production kernel:
memblock.reserve: [s1, e1] and [e0, end_pfn] (Memory is reserved)
memblock.memory:  [s0, s1] and [e1, e0]      (Memory available)

2. Capture kernel:
memblock.reserve: [s0, s1] and [e1, e0]      (Memory is reserved)
memblock.memory:  [s1, e1] and [e0, end_pfn] (Memory available)

In conclusion,[s0, s1] and [e1, e0] memory areas should be reserved.

Signed-off-by: Huacai Chen <chenhuacai@kernel.org>
Signed-off-by: Youling Tang <tangyouling@loongson.cn>
---
v2:
 - New patch.

 arch/mips/kernel/setup.c | 38 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 38 insertions(+)

diff --git a/arch/mips/kernel/setup.c b/arch/mips/kernel/setup.c
index af2c860..aa89f28 100644
--- a/arch/mips/kernel/setup.c
+++ b/arch/mips/kernel/setup.c
@@ -55,6 +55,8 @@ EXPORT_SYMBOL(cpu_data);
 struct screen_info screen_info;
 #endif
 
+static phys_addr_t crashmem_start, crashmem_size;
+
 /*
  * Setup information
  *
@@ -367,6 +369,11 @@ static int __init early_parse_mem(char *p)
 
 	memblock_add_node(start, size, pa_to_nid(start));
 
+	if (strstr(boot_command_line, "elfcorehdr") && start && size) {
+		crashmem_start = start;
+		crashmem_size = size;
+	}
+
 	return 0;
 }
 early_param("mem", early_parse_mem);
@@ -525,6 +532,36 @@ static void reserve_crashm_region(int node, unsigned long s0, unsigned long e0)
 }
 #endif /* !defined(CONFIG_KEXEC)  */
 
+/*
+ * After the kdump operation is performed to enter the capture kernel, the
+ * memory area used by the previous production kernel should be reserved to
+ * avoid destroy to the captured data.
+ */
+static void reserve_oldmem_region(int node, unsigned long s0, unsigned long e0)
+{
+	unsigned long s1, e1;
+
+	if (!is_kdump_kernel())
+		return;
+
+	if ((e0 - s0) > (SZ_1G >> PAGE_SHIFT))
+		e0 = e0 - (SZ_128M >> PAGE_SHIFT);
+
+	/* crashmem_start is crashk_res reserved by primary production kernel */
+	s1 = PFN_UP(crashmem_start);
+	e1 = PFN_DOWN(crashmem_start + crashmem_size);
+
+	if (s1 == 0)
+		return;
+
+	if (node == 0) {
+		memblock_reserve(PFN_PHYS(s0), (s1 - s0) << PAGE_SHIFT);
+		memblock_reserve(PFN_PHYS(e1), (e0 - e1) << PAGE_SHIFT);
+	} else {
+		memblock_reserve(PFN_PHYS(s0), (e0 - s0) << PAGE_SHIFT);
+	}
+}
+
 static void __init check_kernel_sections_mem(void)
 {
 	phys_addr_t start = __pa_symbol(&_text);
@@ -696,6 +733,7 @@ static void __init arch_mem_init(char **cmdline_p)
 	for_each_online_node(node) {
 		get_pfn_range_for_nid(node, &start_pfn, &end_pfn);
 		reserve_crashm_region(node, start_pfn, end_pfn);
+		reserve_oldmem_region(node, start_pfn, end_pfn);
 	}
 
 	device_tree_init();
-- 
2.1.0


      parent reply	other threads:[~2021-04-22  2:25 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-22  2:24 [PATCH v2 0/4] mips: Fix related problems in kdump operation Youling Tang
2021-04-22  2:24 ` [PATCH v2 1/4] MIPS: Fix cmdline "mem=" parameter parsing Youling Tang
2021-04-22  2:24 ` [PATCH v2 2/4] mips: kdump: Capture kernel should be able to see old memories Youling Tang
2021-04-22  2:24 ` [PATCH v2 3/4] mips: kdump: Reserve extra memory for crash dump Youling Tang
2021-04-22  2:24 ` Youling Tang [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1619058274-6996-5-git-send-email-tangyouling@loongson.cn \
    --to=tangyouling@loongson.cn \
    --cc=bhe@redhat.com \
    --cc=chenhuacai@kernel.org \
    --cc=hejinyang@loongson.cn \
    --cc=jiaxun.yang@flygoat.com \
    --cc=kexec@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mips@vger.kernel.org \
    --cc=tsbogend@alpha.franken.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).