diff mbox series

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

Message ID 20240401102511.495791-1-ynezz@true.cz
State Under Review
Delegated to: Petr Štetiar
Headers show
Series image.mk: fix dd memory exhaustion during padding of large images | expand

Commit Message

Petr Štetiar April 1, 2024, 10:25 a.m. UTC
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@gmail.com>
Suggested-by: shir474 <shir474@users.noreply.github.com>
Signed-off-by: Petr Štetiar <ynezz@true.cz>
---
 include/image.mk | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

Comments

Mirko Parthey April 1, 2024, 1:34 p.m. UTC | #1
The sender domain has a DMARC Reject/Quarantine policy which disallows
sending mailing list messages using the original "From" header.

To mitigate this problem, the original message has been wrapped
automatically by the mailing list software.
On Mon, Apr 01, 2024 at 10:25:11AM +0000, Petr Štetiar wrote:
> +			bs=1048576 count=$(shell expr $(2) / 1048576) \

How about using dd iflag=count_bytes instead?
OpenWrt's busybox supports it.
Mirko Parthey April 1, 2024, 1:40 p.m. UTC | #2
The sender domain has a DMARC Reject/Quarantine policy which disallows
sending mailing list messages using the original "From" header.

To mitigate this problem, the original message has been wrapped
automatically by the mailing list software.
On Mon, Apr 01, 2024 at 03:34:09PM +0200, Mirko Parthey wrote:
> OpenWrt's busybox supports it.
Please disregard this, support is required on the build host.
diff mbox series

Patch

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