Skip to contentSkip to conversation

s390/iomem: register initrd as an iomem resource when retain_initrd is set

PATCH In review Joseph Cathcart 3 participants 5 messages Aug 18 – Sep 16 s390

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 s390/iomem: register initrd as an iomem resource when retain_initrd is set

Currently, /proc/iomem contains no reference to initrd memory, even when initrd is retained and still held in RAM. This makes the memory ranges invisible from userspace, and unusable to programs such as kexec. (A sysfs bin file is created for initrd, but this doesn't help programs which need the explicit memory ranges)

Add initrd to standard_resources[] unconditionally. Add initrd as a child of iomem_resources only if retain_initrd is specified.

strstr() could match substrings like "retain_initrd=0" or "no_retain_initrd", but this is how PPC chose to check for "retain_initrd" param too. (arch/powerpc/kexec/ranges.c)

---
 arch/s390/kernel/setup.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)
arch/s390/kernel/setup.c +22 -0
index b60284328fe3..373e54ed7519 100644
@@ -481,15 +481,22 @@ static struct resource bss_resource = {
 	.flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM,
 };
 
+static struct resource initrd_resource = {
+	.name = "initrd",
+	.flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM,
+};
+
 static struct resource __initdata *standard_resources[] = {
 	&code_resource,
 	&data_resource,
 	&bss_resource,
+	&initrd_resource,
 };
 
 static void __init setup_resources(void)
 {
 	struct resource *res, *std_res, *sub_res;
+	bool retain_initrd = false;
 	phys_addr_t start, end;
 	int j;
 	u64 i;
@@ -501,6 +508,19 @@ static void __init setup_resources(void)
 	bss_resource.start = __pa_symbol(__bss_start);
 	bss_resource.end = __pa_symbol(__bss_stop) - 1;
 
+	if (IS_ENABLED(CONFIG_BLK_DEV_INITRD)) {
+		unsigned long initrd_addr, initrd_size;
+	
+		if (strstr(boot_command_line, "retain_initrd"))
+			retain_initrd = true;
+		if (retain_initrd && get_physmem_reserved(RR_INITRD, &initrd_addr, &initrd_size)) {
+			initrd_resource.start = initrd_addr;
+			initrd_resource.end = initrd_resource.start + initrd_size - 1;
+		} else {
+			retain_initrd = false;
+		}
+	}
+
 	for_each_mem_range(i, &start, &end) {
 		res = memblock_alloc_or_panic(sizeof(*res), 8);
 		res->flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM;
@@ -517,6 +537,8 @@ static void __init setup_resources(void)
 
 		for (j = 0; j < ARRAY_SIZE(standard_resources); j++) {
 			std_res = standard_resources[j];
+			if (std_res == &initrd_resource && !retain_initrd)
+				continue;
 			if (std_res->start < res->start ||
 			    std_res->start > res->end)
 				continue;
Signature · 3 lines
2.55.0

base commit: 0f23d56f17fdfc7db69d51f64c8b91bbab947aa9
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Using `strstr()` on `boot_command_line` loosely matches negated parameters like `retain_initrd=0`…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Using strstr() on boot_command_line loosely matches negated parameters like retain_initrd=0, creating a severe state desynchronization between the /proc/iomem resource tree and actual memory. --

--- Patch [1]: [PATCH v4] s390/iomem: register initrd as an iomem resource when retain_initrd is set --- commit 7ecf2fd4332ea230894d369c93eebe76e303f37f
Author: Joseph Cathcart <[email protected]>

s390/iomem: register initrd as an iomem resource when retain_initrd is set

This commit adds initrd to standard_resources[] and registers it as a child of iomem_resources if the "retain_initrd" boot parameter is specified, making the retained initrd memory ranges visible in /proc/iomem.

Quoted 4 lines
arch/s390/kernel/setup.c +0 -0
index b60284328fe3d..373e54ed75198 100644

[ ... ]

arch/s390/kernel/setup.c static void __init setup_resources(void) · line 515

Quoted 9 lines
arch/s390/kernel/setup.c +5 -0
@@ -501,6 +508,19 @@ static void __init setup_resources(void)
 	bss_resource.start = __pa_symbol(__bss_start);
 	bss_resource.end = __pa_symbol(__bss_stop) - 1;
 
+	if (IS_ENABLED(CONFIG_BLK_DEV_INITRD)) {
+		unsigned long initrd_addr, initrd_size;
+	
+		if (strstr(boot_command_line, "retain_initrd"))
+			retain_initrd = true;

[Severity: High]
Does this loose strstr() match create a state desynchronization between /proc/iomem and actual memory if a user passes a negated parameter like "retain_initrd=0" or "no_retain_initrd"?

The core kernel's exact parameter parsing in init/initramfs.c will correctly drop the retain_initrd flag and later free the initrd memory. However, this strstr() check in setup_resources() will falsely evaluate to true, causing the physical memory of the initrd to be incorrectly registered as IORESOURCE_BUSY in /proc/iomem.

Since the actual memory is freed by the general allocator, it will be overwritten by other kernel structures while still being exported as an intact initrd to userspace. Could this cause tools that rely on the /proc/iomem tree, such as kexec-tools, to extract corrupted memory thinking it is a valid initrd?

Even though the commit message notes that PowerPC uses the same pattern, does copying this approach into s390 still propagate a concrete bug where kexec might fail or boot with corrupted ramdisk data?

Quoted 8 lines
arch/s390/kernel/setup.c +8 -0
+		if (retain_initrd && get_physmem_reserved(RR_INITRD, &initrd_addr, &initrd_size)) {
+			initrd_resource.start = initrd_addr;
+			initrd_resource.end = initrd_resource.start + initrd_size - 1;
+		} else {
+			retain_initrd = false;
+		}
+	}
+
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Joseph Cathcart
Quoted 9 lines · Joseph Cathcart

Currently, /proc/iomem contains no reference to initrd memory, even when initrd is retained and still held in RAM. This makes the memory ranges invisible from userspace, and unusable to programs such as kexec. (A sysfs bin file is created for initrd, but this doesn't help programs which need the explicit memory ranges)

Add initrd to standard_resources[] unconditionally. Add initrd as a child of iomem_resources only if retain_initrd is specified.

Quoted 8 lines

strstr() could match substrings like "retain_initrd=0" or "no_retain_initrd", but this is how PPC chose to check for "retain_initrd" param too. (arch/powerpc/kexec/ranges.c)

---
  arch/s390/kernel/setup.c | 22 ++++++++++++++++++++++
  1 file changed, 22 insertions(+)

Checkpatch complains:

$ b4 shazam 'https://lore.kernel.org/all/[email protected]/'
$ git format-patch -1 --stdout | ./scripts/checkpatch.pl -
ERROR: trailing whitespace
#58: FILE: arch/s390/kernel/setup.c:513:
+^I$
Quoted 48 lines
arch/s390/kernel/setup.c +20 -0
index b60284328fe3..373e54ed7519 100644
@@ -481,15 +481,22 @@ static struct resource bss_resource = {
  	.flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM,
  };
 
+static struct resource initrd_resource = {
+	.name = "initrd",
+	.flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM,
+};
+
  static struct resource __initdata *standard_resources[] = {
  	&code_resource,
  	&data_resource,
  	&bss_resource,
+	&initrd_resource,
  };
 
  static void __init setup_resources(void)
  {
  	struct resource *res, *std_res, *sub_res;
+	bool retain_initrd = false;
  	phys_addr_t start, end;
  	int j;
  	u64 i;
@@ -501,6 +508,19 @@ static void __init setup_resources(void)
  	bss_resource.start = __pa_symbol(__bss_start);
  	bss_resource.end = __pa_symbol(__bss_stop) - 1;
 
+	if (IS_ENABLED(CONFIG_BLK_DEV_INITRD)) {
+		unsigned long initrd_addr, initrd_size;
+	
+		if (strstr(boot_command_line, "retain_initrd"))
+			retain_initrd = true;
+		if (retain_initrd && get_physmem_reserved(RR_INITRD, &initrd_addr, &initrd_size)) {
+			initrd_resource.start = initrd_addr;
+			initrd_resource.end = initrd_resource.start + initrd_size - 1;
+		} else {
+			retain_initrd = false;
+		}
+	}
+
  	for_each_mem_range(i, &start, &end) {
  		res = memblock_alloc_or_panic(sizeof(*res), 8);
  		res->flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM;

arch/s390/kernel/setup.c static void __init setup_resources(void) · line 544

Quoted 8 lines
arch/s390/kernel/setup.c +2 -0
@@ -517,6 +537,8 @@ static void __init setup_resources(void)
 
  		for (j = 0; j < ARRAY_SIZE(standard_resources); j++) {
  			std_res = standard_resources[j];
+			if (std_res == &initrd_resource && !retain_initrd)
+				continue;
  			if (std_res->start < res->start ||
  			    std_res->start > res->end)
  				continue;
Cc Christian and Alexander.

I wonder if it would make sense to move initrd resource creation to do_populate_rootfs(), right after /sys/firmware/initrd creation, enabling this feature on all architectures?

#3 lore raw
Quoted 87 lines · Ilya Leoshkevich
Quoted 9 lines · Joseph Cathcart

Currently, /proc/iomem contains no reference to initrd memory, even when initrd is retained and still held in RAM. This makes the memory ranges invisible from userspace, and unusable to programs such as kexec. (A sysfs bin file is created for initrd, but this doesn't help programs which need the explicit memory ranges)

Add initrd to standard_resources[] unconditionally. Add initrd as a child of iomem_resources only if retain_initrd is specified.

Quoted 8 lines

strstr() could match substrings like "retain_initrd=0" or "no_retain_initrd", but this is how PPC chose to check for "retain_initrd" param too. (arch/powerpc/kexec/ranges.c)

---
  arch/s390/kernel/setup.c | 22 ++++++++++++++++++++++

  1 file changed, 22 insertions(+)

Checkpatch complains:

$ b4 shazam 'https://lore.kernel.org/all/[email protected]/'
$ git format-patch -1 --stdout | ./scripts/checkpatch.pl -
ERROR: trailing whitespace
#58: FILE: arch/s390/kernel/setup.c:513:
+^I$
Quoted 51 lines
arch/s390/kernel/setup.c +18 -0
index b60284328fe3..373e54ed7519 100644
@@ -481,15 +481,22 @@ static struct resource bss_resource = {
       .flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM,
   };
   +static struct resource initrd_resource = {
+    .name = "initrd",
+    .flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM,
+};
+
   static struct resource __initdata *standard_resources[] = {
       &code_resource,
       &data_resource,
       &bss_resource,
+    &initrd_resource,
   };
     static void __init setup_resources(void)
   {
       struct resource *res, *std_res, *sub_res;
+    bool retain_initrd = false;
       phys_addr_t start, end;
       int j;
       u64 i;
@@ -501,6 +508,19 @@ static void __init setup_resources(void)
       bss_resource.start = __pa_symbol(__bss_start);
       bss_resource.end = __pa_symbol(__bss_stop) - 1;
   +    if (IS_ENABLED(CONFIG_BLK_DEV_INITRD)) {
+        unsigned long initrd_addr, initrd_size;
+
+        if (strstr(boot_command_line, "retain_initrd"))
+            retain_initrd = true;
+        if (retain_initrd && get_physmem_reserved(RR_INITRD, 
 &initrd_addr, &initrd_size)) {
+            initrd_resource.start = initrd_addr;
+            initrd_resource.end = initrd_resource.start + 
 initrd_size - 1;
+        } else {
+            retain_initrd = false;
+        }
+    }
+
       for_each_mem_range(i, &start, &end) {

          res = memblock_alloc_or_panic(sizeof(*res), 8); res->flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM;

arch/s390/kernel/setup.c +2 -0
@@ -517,6 +537,8 @@ static void __init setup_resources(void)
             for (j = 0; j < ARRAY_SIZE(standard_resources); j++) {
               std_res = standard_resources[j];
+            if (std_res == &initrd_resource && !retain_initrd)
+                continue;
Quoted 3 lines

              if (std_res->start < res->start ||
                  std_res->start > res->end)
                  continue;

Cc Christian and Alexander.
Quoted 3 lines

I wonder if it would make sense to move initrd resource creation to do_populate_rootfs(), right after /sys/firmware/initrd creation, enabling this feature on all architectures?

As I will no longer have access to my IBM email address after today, I will be continuing

my contributions through my personal email address "[email protected]"

#4 lore raw
In reply to Ilya Leoshkevich
Quoted 20 lines · Ilya Leoshkevich
Quoted 17 lines · Joseph Cathcart

Currently, /proc/iomem contains no reference to initrd memory, even when initrd is retained and still held in RAM. This makes the memory ranges invisible from userspace, and unusable to programs such as kexec. (A sysfs bin file is created for initrd, but this doesn't help programs which need the explicit memory ranges)

Add initrd to standard_resources[] unconditionally. Add initrd as a child of iomem_resources only if retain_initrd is specified.

strstr() could match substrings like "retain_initrd=0" or "no_retain_initrd", but this is how PPC chose to check for "retain_initrd" param too. (arch/powerpc/kexec/ranges.c)

---
  arch/s390/kernel/setup.c | 22 ++++++++++++++++++++++

  1 file changed, 22 insertions(+)

[...]

Quoted 5 lines
Cc Christian and Alexander.

I wonder if it would make sense to move initrd resource creation to do_populate_rootfs(), right after /sys/firmware/initrd creation, enabling this feature on all architectures?

Ping.