Kexec Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Chen Haixiang <chenhaixiang3@huawei.com>
To: <kexec@lists.infradead.org>
Cc: <louhongxiang@huawei.com>, <wangbin224@huawei.com>,
	<yangyanchao6@huawei.com>, <chenhaixiang3@huawei.com>
Subject: [PATCH] Before adding to usablemem_rgns, check if the memory range is already included.
Date: Thu, 17 Oct 2024 20:38:59 +0800	[thread overview]
Message-ID: <20241017123859.1377-1-chenhaixiang3@huawei.com> (raw)

When kexec_iomem_for_each_line() reads from /proc/iomem,
concurrent modifications to /proc/iomem may lead to
usablemem_rgns recording duplicate Crash kernel segments.
This can result in the number of retrieved Crash kernel
segments exceeding CRASH_MAX_RESERVED_RANGES, triggering
a realloc of the crash_reserved_mem in usablemem_rgns,
which could crash the process. We should ensure that each
range added to usablemem_rgns is unique to prevent these issues.
---
 kexec/arch/arm64/crashdump-arm64.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/kexec/arch/arm64/crashdump-arm64.c b/kexec/arch/arm64/crashdump-arm64.c
index 3098315..f23b2bf 100644
--- a/kexec/arch/arm64/crashdump-arm64.c
+++ b/kexec/arch/arm64/crashdump-arm64.c
@@ -63,10 +63,22 @@ static int iomem_range_callback(void *UNUSED(data), int UNUSED(nr),
 				char *str, unsigned long long base,
 				unsigned long long length)
 {
-	if (strncmp(str, CRASH_KERNEL, strlen(CRASH_KERNEL)) == 0)
+	int i;
+
+	if (strncmp(str, CRASH_KERNEL, strlen(CRASH_KERNEL)) == 0) {
+		/*
+		 * Checks whether the area exists in crash_reserved_mem.
+		 */
+		for (i = 0; i < usablemem_rgns.max_size; i++) {
+			if (usablemem_rgns.ranges[i].start == base) {
+				fprintf(stderr, "Warning, the range already exists in usablemem_rgns, base=%lx, length=%lx\n",
+						base, length);
+				return 0;
+			}
+		}
 		return mem_regions_alloc_and_add(&usablemem_rgns,
 						base, length, RANGE_RAM);
-	else if (strncmp(str, SYSTEM_RAM, strlen(SYSTEM_RAM)) == 0)
+	} else if (strncmp(str, SYSTEM_RAM, strlen(SYSTEM_RAM)) == 0)
 		return mem_regions_alloc_and_add(&system_memory_rgns,
 						base, length, RANGE_RAM);
 	else if (strncmp(str, KERNEL_CODE, strlen(KERNEL_CODE)) == 0) {
-- 
2.41.0


_______________________________________________
kexec mailing list
kexec@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kexec

             reply	other threads:[~2024-10-17 12:49 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-10-17 12:38 Chen Haixiang [this message]
2024-10-21 10:25 ` [PATCH] Before adding to usablemem_rgns, check if the memory range is already included Simon Horman
  -- strict thread matches above, loose matches on Subject: below --
2024-10-21 11:57 chenhaixiang (A)

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=20241017123859.1377-1-chenhaixiang3@huawei.com \
    --to=chenhaixiang3@huawei.com \
    --cc=kexec@lists.infradead.org \
    --cc=louhongxiang@huawei.com \
    --cc=wangbin224@huawei.com \
    --cc=yangyanchao6@huawei.com \
    /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