mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2] x86/setup: Report the page-aligned size in relocate_initrd()
@ 2026-11-16 10:02 Thorsten Blum
  0 siblings, 0 replies; only message in thread
From: Thorsten Blum @ 2026-11-16 10:02 UTC (permalink / raw)
  To: Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
	H. Peter Anvin, Mike Rapoport (Microsoft),
	Ard Biesheuvel, Andrew Morton, Pratyush Yadav (Google),
	Grzegorz Jaszczyk, Thomas Zimmermann, Breno Leitao,
	Harshit Mogalapalli, Thorsten Blum, Yinghai Lu, Johannes Weiner
  Cc: Ingo Molnar, linux-kernel

Since commit c967da6a0ba8 ("x86: Make sure free_init_pages() frees
pages on page boundary"), relocate_initrd() allocates area_size bytes,
i.e., ramdisk_size rounded up to a page boundary.

However, the allocation message and the panic message still use the
unaligned ramdisk_size, so the reported range or size can be too small.
Use area_size there instead, matching the page-aligned ramdisk_end
reported by reserve_initrd().

The exact ramdisk_size is still required for initrd_end, the copy
length, and the copy message, as those describe the image itself and
must not include the padding.

While at it, pass ramdisk_image and ramdisk_size from reserve_initrd()
to relocate_initrd() instead of re-reading them, use pr_info() and %llu
for the u64 allocation size, clarify the reservation and copy messages,
and drop ret by checking copy_from_early_mem() directly.

Fixes: c967da6a0ba8 ("x86: Make sure free_init_pages() frees pages on page boundary")
Signed-off-by: Thorsten Blum <blum@kernel.org>
---
Tested with QEMU using a 4097-byte initrd:

  Reserved RAMDISK: [mem 0x1ffde000-0x1ffdffff]
  Allocated new RAMDISK: [mem 0x0fffe000-0x0fffffff]
  Copied RAMDISK from [mem 0x1ffde000-0x1ffdf000] to [mem 0x0fffe000-0x0ffff000]

Changes in v2:
- Make it a single patch with a new title
- Make range and size reporting consistent
- Use area_size in the allocation panic message with %llu
- Clarify the reservation and copy messages
- Pass ramdisk_image and ramdisk_size to relocate_initrd()
- Update the commit message to explain allocation and image sizes
- v1: https://lore.kernel.org/r/20261111165608.78688-4-blum@kernel.org/

 arch/x86/kernel/setup.c | 27 ++++++++++-----------------
 1 file changed, 10 insertions(+), 17 deletions(-)

diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c
index 19fc8e1714e6..6694ebc2dd11 100644
--- a/arch/x86/kernel/setup.c
+++ b/arch/x86/kernel/setup.c
@@ -316,32 +316,26 @@ static u64 __init get_ramdisk_size(void)
 	return ramdisk_size;
 }
 
-static void __init relocate_initrd(void)
+static void __init relocate_initrd(u64 ramdisk_image, u64 ramdisk_size)
 {
 	/* Assume only end is not page aligned */
-	u64 ramdisk_image = get_ramdisk_image();
-	u64 ramdisk_size  = get_ramdisk_size();
-	u64 area_size     = PAGE_ALIGN(ramdisk_size);
-	int ret = 0;
+	u64 area_size = PAGE_ALIGN(ramdisk_size);
 
 	/* We need to move the initrd down into directly mapped mem */
 	u64 relocated_ramdisk = memblock_phys_alloc_range(area_size, PAGE_SIZE, 0,
 						      PFN_PHYS(max_pfn_mapped));
 	if (!relocated_ramdisk)
-		panic("Cannot find place for new RAMDISK of size %lld\n",
-		      ramdisk_size);
+		panic("Cannot allocate %llu bytes for new RAMDISK\n", area_size);
 
 	initrd_start = relocated_ramdisk + PAGE_OFFSET;
 	initrd_end   = initrd_start + ramdisk_size;
-	printk(KERN_INFO "Allocated new RAMDISK: [mem %#010llx-%#010llx]\n",
-	       relocated_ramdisk, relocated_ramdisk + ramdisk_size - 1);
+	pr_info("Allocated new RAMDISK: [mem %#010llx-%#010llx]\n",
+		relocated_ramdisk, relocated_ramdisk + area_size - 1);
 
-	ret = copy_from_early_mem((void *)initrd_start, ramdisk_image, ramdisk_size);
-	if (ret)
-		panic("Copy RAMDISK failed\n");
+	if (copy_from_early_mem((void *)initrd_start, ramdisk_image, ramdisk_size))
+		panic("Copying RAMDISK failed\n");
 
-	printk(KERN_INFO "Move RAMDISK from [mem %#010llx-%#010llx] to"
-		" [mem %#010llx-%#010llx]\n",
+	pr_info("Copied RAMDISK from [mem %#010llx-%#010llx] to [mem %#010llx-%#010llx]\n",
 		ramdisk_image, ramdisk_image + ramdisk_size - 1,
 		relocated_ramdisk, relocated_ramdisk + ramdisk_size - 1);
 }
@@ -373,8 +367,7 @@ static void __init reserve_initrd(void)
 
 	initrd_start = 0;
 
-	printk(KERN_INFO "RAMDISK: [mem %#010llx-%#010llx]\n", ramdisk_image,
-			ramdisk_end - 1);
+	pr_info("Reserved RAMDISK: [mem %#010llx-%#010llx]\n", ramdisk_image, ramdisk_end - 1);
 
 	if (pfn_range_is_mapped(PFN_DOWN(ramdisk_image),
 				PFN_DOWN(ramdisk_end))) {
@@ -384,7 +377,7 @@ static void __init reserve_initrd(void)
 		return;
 	}
 
-	relocate_initrd();
+	relocate_initrd(ramdisk_image, ramdisk_size);
 
 	memblock_phys_free(ramdisk_image, ramdisk_end - ramdisk_image);
 }

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-16 10:03 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-16 10:02 [PATCH v2] x86/setup: Report the page-aligned size in relocate_initrd() Thorsten Blum

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®