Skip to contentSkip to conversation

arm-zImage: prepare for more malloc requirements of zstd

PATCH Superseded Andreas Kemnade 2 participants 2 messages Sep 12 – Sep 14

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 arm-zImage: prepare for more malloc requirements of zstd

As there are plans for zstd in zImage for ARM, which requires more malloc size fo, decompression, look at the header for the required size.

References:
https://lore.kernel.org/lkml/[email protected]/#t
https://lore.kernel.org/lkml/[email protected]/

---
To: [email protected]
To: Andrew Morton <[email protected]>
To: Baoquan He <[email protected]>
To: Mike Rapoport <[email protected]>
To: Pasha Tatashin <[email protected]>
To: Pratyush Yadav <[email protected]>
To: Simon Horman <[email protected]>
Cc: Russell King <[email protected]>
Cc: [email protected]
---
Changes in v2:
- extract malloc size from zImage header
- Link to v1: https://patch.msgid.link/[email protected]

---
 kexec/arch/arm/kexec-zImage-arm.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
kexec/arch/arm/kexec-zImage-arm.c +13 -3
index 8b474dd..1e682db 100644
@@ -131,6 +131,8 @@ struct zimage_tag {
 		struct zimage_krnl_size {
 			uint32_t size_ptr;
 			uint32_t bss_size;
+			uint32_t text_offset;
+			uint32_t malloc_size;
 		} krnl_size;
 	} u;
 };
@@ -663,18 +665,21 @@ int zImage_arm_load(int argc, char **argv, const char *buf, off_t len,
 
 	/*
 	 * The zImage length does not include its stack (4k) or its
-	 * malloc space (64k).  Include this.
+	 * malloc space (128k).  Include this.
 	 */
-	len += 0x11000;
+	len += 0x1000;
 
-	dbgprintf("zImage requires 0x%08llx bytes\n", (unsigned long long)len);
 
 	if (tag) {
 		uint32_t *p = (void *)buf + le32_to_cpu(tag->u.krnl_size.size_ptr);
 		uint32_t edata_size = le32_to_cpu(get_unaligned(p));
 		uint32_t bss_size = le32_to_cpu(tag->u.krnl_size.bss_size);
+		uint32_t malloc_size = tag->hdr.size >= 6 ? tag->u.krnl_size.malloc_size : 65536;
 		uint32_t kernel_size = edata_size + bss_size;
 
+		len += malloc_size;
+		dbgprintf("malloc size: 0x%lx\n", (unsigned long)malloc_size);
+
 		dbgprintf("Decompressed kernel sizes:\n");
 		dbgprintf(" text+data 0x%08lx bss 0x%08lx total 0x%08lx\n",
 			  (unsigned long)edata_size,
@@ -699,8 +704,13 @@ int zImage_arm_load(int argc, char **argv, const char *buf, off_t len,
 				(unsigned long)kernel_size);
 			kexec_arm_image_size = kernel_size;
 		}
+	} else {
+		/* add default malloc size */
+		len += 0x10000;
 	}
 
+	dbgprintf("zImage requires 0x%08llx bytes\n", (unsigned long long)len);
+
 	/*
 	 * If the user didn't specify the size of the image, and we don't
 	 * have the extension tables, assume the maximum kernel compression

---
base-commit: 43c5cba85ad23cc69c423527d9587c59b89fa437
change-id: 20260912-malloc-fix-31a13574cb84

Best regards,

Signature · 1 line
Andreas Kemnade <[email protected]>
#1 lore raw

Hi Andreas,

I'm glad to see improvements to kexec-tools on arm.

For context, I recently posted "arm: use the zImage tag's real TEXT_OFFSET for kernel placement"[0] to add support for text_offset.

I hadn't seen your v1 at the time, and will happily rebase my patch after your patch is merged. (Or, feel free to add support for text_offset, instead.)

I'll leave some comments, since I have a tiny bit of state on this code.

kexec/arch/arm/kexec-zImage-arm.c int zImage_arm_load(int argc, char **argv, const char *buf, off_t len, · line 668

Quoted 6 lines · Andreas Kemnade
kexec/arch/arm/kexec-zImage-arm.c +1 -1
@@ -663,18 +665,21 @@ int zImage_arm_load(int argc, char **argv, const char *buf, off_t len,
 
  	/*
  	 * The zImage length does not include its stack (4k) or its
-	 * malloc space (64k).  Include this.
+	 * malloc space (128k).  Include this.

This line no longer adds the malloc space (and nor is it necessarily 128k), so maybe remove the mention of malloc space in this comment.

kexec/arch/arm/kexec-zImage-arm.c int zImage_arm_load(int argc, char **argv, const char *buf, off_t len, · line 677

Quoted 11 lines
kexec/arch/arm/kexec-zImage-arm.c +2 -2
  	 */
-	len += 0x11000;
+	len += 0x1000;
 
-	dbgprintf("zImage requires 0x%08llx bytes\n", (unsigned long long)len);
 
  	if (tag) {
  		uint32_t *p = (void *)buf + le32_to_cpu(tag->u.krnl_size.size_ptr);
  		uint32_t edata_size = le32_to_cpu(get_unaligned(p));
  		uint32_t bss_size = le32_to_cpu(tag->u.krnl_size.bss_size);
+		uint32_t malloc_size = tag->hdr.size >= 6 ? tag->u.krnl_size.malloc_size : 65536;

There is support for big endian arm[1], so use le32_to_cpu to convert, similar to the other fields.

Also, consider replacing "65536" with "0x10000" to be consistent with the else-clause in your patch below.

Quoted 9 lines
kexec/arch/arm/kexec-zImage-arm.c +3 -0
  		uint32_t kernel_size = edata_size + bss_size;
 
+		len += malloc_size;
+		dbgprintf("malloc size: 0x%lx\n", (unsigned long)malloc_size);
+
  		dbgprintf("Decompressed kernel sizes:\n");
  		dbgprintf(" text+data 0x%08lx bss 0x%08lx total 0x%08lx\n",
  			  (unsigned long)edata_size,

kexec/arch/arm/kexec-zImage-arm.c int zImage_arm_load(int argc, char **argv, const char *buf, off_t len, · line 709

Quoted 6 lines
kexec/arch/arm/kexec-zImage-arm.c +3 -0
@@ -699,8 +704,13 @@ int zImage_arm_load(int argc, char **argv, const char *buf, off_t len,
  				(unsigned long)kernel_size);
  			kexec_arm_image_size = kernel_size;
  		}
+	} else {
+		/* add default malloc size */
+		len += 0x10000;

It's a bit awkward that this fallback case is far from the happy-case where the header defines a malloc size.

Consider adding a "if (tag && le32_to_cpu(tag->hdr.size) >= 6)" block before the "if (tag)" block to handle this.

This allows us to remove the two fallback cases (!tag, and tag&&tag->hdr.size >=6), in favour of just one fallback case.

Something like:

```
if (tag && le32_to_cpu(tag->hdr.size) >= 6)
	len += le32_to_cpu(tag->u.krnl_size.malloc_size);
else
	len += 0x10000;

if (tag) {
[...]
```

Regards,
Tom Fitzhenry

0. https://lists.infradead.org/pipermail/kexec/2026-August/038363.html
1. https://github.com/torvalds/linux/blob/704340f1cd0dcef829eb62f5b48ae95a2ce17bdf/arch/arm/mm/Kconfig#L780