Skip to contentSkip to conversation

x86/setup: Report the page-aligned size in relocate_initrd()

PATCH New Thorsten Blum 1 participant 1 message Nov 16 x86

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 x86/setup: Report the page-aligned size in relocate_initrd()

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

---
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/[email protected]/

 arch/x86/kernel/setup.c | 27 ++++++++++-----------------
 1 file changed, 10 insertions(+), 17 deletions(-)
arch/x86/kernel/setup.c +10 -17
index 19fc8e1714e6..6694ebc2dd11 100644
@@ -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);
 }