[PATCH] image.mk: fix dd memory exhaustion during padding of large images

Petr Štetiar ynezz at true.cz
Mon Apr 1 03:25:11 PDT 2024


This commit adjusts the Image/pad-to function within image.mk to improve
memory management when padding large images. Previously, the dd command
was used with a block size (bs) directly derived from the target padding
size. This could lead to inefficiencies or memory exhaustion issues when
dealing with very large images.

To address this, a conditional has been introduced: If the desired
padding size exceeds 1MiB (1048576 bytes), dd now operates with a block
size of 1MiB and calculates the necessary count to achieve the desired
padding. This approach aims to balance memory usage and performance by
avoiding excessively large block sizes in memory-intensive operations.

For padding sizes of 1MiB or less, the original behavior is preserved,
using the specified padding size as the block size.

This change ensures more reliable and efficient image padding,
particularly in environments with limited memory resources.

References: https://github.com/openwrt/openwrt/pull/2862
Reported-by: Nishant Sharma <codemarauder at gmail.com>
Suggested-by: shir474 <shir474 at users.noreply.github.com>
Signed-off-by: Petr Štetiar <ynezz at true.cz>
---
 include/image.mk | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/include/image.mk b/include/image.mk
index 4b6acbe1aad6..2586f29160f6 100644
--- a/include/image.mk
+++ b/include/image.mk
@@ -163,7 +163,13 @@ DTC_FLAGS += $(DTC_WARN_FLAGS)
 DTCO_FLAGS += $(DTC_WARN_FLAGS)
 
 define Image/pad-to
-	dd if=$(1) of=$(1).new bs=$(2) conv=sync
+	if [ $(2) -gt 1048576 ]; then \
+		dd if=$(1) of=$(1).new \
+			bs=1048576 count=$(shell expr $(2) / 1048576) \
+			conv=sync ; \
+	else \
+		dd if=$(1) of=$(1).new bs=$(2) conv=sync  ; \
+	fi
 	mv $(1).new $(1)
 endef
 



More information about the openwrt-devel mailing list