Skip to contentSkip to conversation

arm-zImage: prepare for more malloc requirements of zstd

PATCH In review Andreas Kemnade 3 participants 3 messages Sep 22 – Oct 5

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v3 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 v3:
- calculate zImage size requirement before the if (tag) to simplify code
- Link to v2: https://patch.msgid.link/[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 | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
kexec/arch/arm/kexec-zImage-arm.c +10 -2
index 8b474dd..6c3bf27 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,9 +665,15 @@ 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.
 	 */
-	len += 0x11000;
+	len += 0x1000;
+
+	/* include malloc space */
+	if (tag && le32_to_cpu(tag->hdr.size) >= 6)
+		len += le32_to_cpu(tag->u.krnl_size.malloc_size);
+	else
+		len += 0x10000;
 
 	dbgprintf("zImage requires 0x%08llx bytes\n", (unsigned long long)len);
 

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

Best regards,

Signature · 1 line
Andreas Kemnade <[email protected]>
Reviewed-by Tested-by on v3 1/1
#1 lore raw

I tested this in QEMU with kexec-tools at 43c5cba85ad2 plus this patch:

1. Against unmodified Linux 8d3ae59288f1 (gzip): kexec reads the 64 KiB malloc size from the header and boots the second kernel via kexec -e without regression:

  # /bin/kexec-patched --debug -l /boot/zImage 
--initrd=/boot/tiny-initrd.cpio.gz --append="console=ttyAMA0 kexec_booted=1"
  [...]
  zImage size 0x1a3500, file size 0x1a3500
    offset 0x00007770 tag 0x5a534c4b size 24
  zImage requires 0x001b4500 bytes
  [...]
  $ printf "0x%x\n" $((0x001b4500 - 0x001a3500))
  0x11000  # 0x1000 (4 KiB stack) + 0x10000 (64 KiB malloc)

2. Against Linux 8d3ae59288f1 with the v2 ARM zstd series[1] applied:
kexec reads the increased 128 KiB malloc size from the header and boots the second kernel via kexec -e:

  # /bin/kexec-patched --debug -l /boot/zImage 
--initrd=/boot/tiny-initrd.cpio.gz --append="console=ttyAMA0 kexec_booted=1"
  [...]
  zImage size 0x182548, file size 0x182548
    offset 0x00017370 tag 0x5a534c4b size 24
  zImage requires 0x001a3548 bytes
  [...]
  $ printf "0x%x\n" $((0x001a3548 - 0x00182548))
  0x21000  # 0x1000 (4 KiB stack) + 0x20000 (128 KiB malloc)

Thank you addressing my comments.

[1] https://lore.kernel.org/lkml/[email protected]/

Reviewed-by Tom Fitzhenry Tested-by Tom Fitzhenry on v3 1/1
#2 lore raw
In reply to Andreas Kemnade
Quoted 24 lines · Andreas Kemnade

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 v3:
- calculate zImage size requirement before the if (tag) to simplify code
- Link to v2: https://patch.msgid.link/[email protected]

Quoted 3 lines

Changes in v2:
- extract malloc size from zImage header
- Link to v1: https://patch.msgid.link/[email protected]

Thanks, and sorry for the delay.

I have applied this as:

[1] arm-zImage: prepare for more malloc requirements of zstd

    https://git.kernel.org/pub/scm/utils/kernel/kexec/kexec-tools.git/commit/?id=e128675241fa

This required a manual merge due to conflicts with the following, which I had applied just beforehand.

[2] arm: use the zImage tag's real TEXT_OFFSET for kernel placement

    https://git.kernel.org/pub/scm/utils/kernel/kexec/kexec-tools.git/commit/?id=c1a526c87eb1

In particular:

a. The text_offset member no longer needs to be added to struct zimage_tag
by [1] as it was added by [2].

b. There is some fuzz in the 2nd hunk of [2] as [1] adds some
logic earlier on in zImage_arm_load().

FTR, the patch I applied is as follows.
If I've messed something up, please let me know.

From e128675241fa7777dcce613a2df191458c320f5b Mon Sep 17 00:00:00 2001
From: Andreas Kemnade <[email protected]>
Date: Tue, 22 Sep 2026 12:25:59 +0200
Subject: [PATCH] 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]/

---
 kexec/arch/arm/kexec-zImage-arm.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
kexec/arch/arm/kexec-zImage-arm.c +9 -2
index 892fca0cf09c..b1ee6ca2158b 100644
@@ -132,6 +132,7 @@ struct zimage_tag {
 			uint32_t size_ptr;
 			uint32_t bss_size;
 			uint32_t text_offset;
+			uint32_t malloc_size;
 		} krnl_size;
 	} u;
 };
@@ -679,9 +680,15 @@ 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.
 	 */
-	len += 0x11000;
+	len += 0x1000;
+
+	/* include malloc space */
+	if (tag && le32_to_cpu(tag->hdr.size) >= 6)
+		len += le32_to_cpu(tag->u.krnl_size.malloc_size);
+	else
+		len += 0x10000;
 
 	dbgprintf("zImage requires 0x%08llx bytes\n", (unsigned long long)len);
 
Signature · 1 line
2.47.3