Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Sang-Heon Jeon <ekffu200098@gmail.com>
To: Andrew Morton <akpm@linux-foundation.org>,
	Mike Rapoport <rppt@kernel.org>
Cc: Jason Gunthorpe <jgg@ziepe.ca>,
	linux-mm@kvack.org, Pratyush Yadav <pratyush@kernel.org>
Subject: [PATCH] memblock: fix page count when preserving reserve_mem regions
Date: Wed,  2 Sep 2026 01:52:35 +0900	[thread overview]
Message-ID: <20260901165237.1025973-1-ekffu200098@gmail.com> (raw)

When map->start or map->size is not page aligned,
reserved_mem_preserve() does not preserve the last pages of the region
from map->start to map->start + map->size.

After kexec the new kernel reserves only the preserved pages, so the
last pages are released to the buddy allocator. But
reserve_mem_find_by_name() still returns the whole region including the
last pages, so they can be allocated while the subsystem is still using
them.

Preserve every page the region spans so the new kernel reserves all of
it.

Fixes: 8375b76517cb ("kho: replace kho_preserve_phys() with kho_preserve_pages()")
Signed-off-by: Sang-Heon Jeon <ekffu200098@gmail.com>
---

QEMU-based test results

- Booted with "memblock=debug kho=on reserve_mem=5000:64:oops"

  # kexec -s -l /bzImage --initrd=/initramfs.gz --reuse-cmdline
  # kexec -e
  # dmesg | grep kho_preserved_memory_reserve

1) AS-IS (before fix)
[    0.188536] __memblock_reserve: [0x000000013ffee000-0x000000013ffeefff] nid=-1 flags=0 kho_preserved_memory_reserve+0x3d/0x70

2) TO-BE (after fix)
[    0.076935] __memblock_reserve: [0x000000013ffee000-0x000000013ffeffff] nid=-1 flags=0 kho_preserved_memory_reserve+0x3d/0x70

 mm/memblock.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/mm/memblock.c b/mm/memblock.c
index 777c69f05400..a17b1cccaf3d 100644
--- a/mm/memblock.c
+++ b/mm/memblock.c
@@ -2534,7 +2534,8 @@ static int __init reserved_mem_preserve(void)
 	for (unsigned int i = 0; i < reserved_mem_count; i++, nr_preserved++) {
 		struct reserve_mem_table *map = &reserved_mem_table[i];
 		struct page *page = phys_to_page(map->start);
-		unsigned int nr_pages = map->size >> PAGE_SHIFT;
+		unsigned int nr_pages = PFN_UP(map->start + map->size) -
+					PFN_DOWN(map->start);
 
 		err = kho_preserve_pages(page, nr_pages);
 		if (err)
@@ -2547,7 +2548,8 @@ static int __init reserved_mem_preserve(void)
 	for (unsigned int i = 0; i < nr_preserved; i++) {
 		struct reserve_mem_table *map = &reserved_mem_table[i];
 		struct page *page = phys_to_page(map->start);
-		unsigned int nr_pages = map->size >> PAGE_SHIFT;
+		unsigned int nr_pages = PFN_UP(map->start + map->size) -
+					PFN_DOWN(map->start);
 
 		kho_unpreserve_pages(page, nr_pages);
 	}

base-commit: dde94c26af59f5020adafafa08ece21ccd32590b
-- 
2.43.0



             reply	other threads:[~2026-09-01 16:52 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-01 16:52 Sang-Heon Jeon [this message]
2026-09-01 18:49 ` [PATCH] memblock: fix page count when preserving reserve_mem regions Mike Rapoport
2026-09-02 15:19   ` Sang-Heon Jeon

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=20260901165237.1025973-1-ekffu200098@gmail.com \
    --to=ekffu200098@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=jgg@ziepe.ca \
    --cc=linux-mm@kvack.org \
    --cc=pratyush@kernel.org \
    --cc=rppt@kernel.org \
    /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