Linux Trace Kernel
 help / color / mirror / Atom feed
From: "Masami Hiramatsu (Google)" <mhiramat@kernel.org>
To: Masami Hiramatsu <mhiramat@kernel.org>,
	Andrew Morton <akpm@linux-foundation.org>,
	Christian Brauner <brauner@kernel.org>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>, Jan Kara <jack@suse.cz>,
	Breno Leitao <leitao@debian.org>,
	Ryan Roberts <ryan.roberts@arm.com>,
	Steven Rostedt <rostedt@goodmis.org>,
	linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org,
	linux-trace-kernel@vger.kernel.org
Subject: [PATCH 2/2] bootconfig: Free the initrd memory used by bootconfig
Date: Wed, 18 Nov 2026 08:47:40 +0900	[thread overview]
Message-ID: <179495926058.237695.12800083196053168375.stgit@devnote2> (raw)
In-Reply-To: <179495924003.237695.10973676765152260584.stgit@devnote2>

From: Masami Hiramatsu (Google) <mhiramat@kernel.org>

get_boot_config_from_initrd() removes the bootconfig from the initrd by
decrementing initrd_end in place. However, the whole initrd including
the bootconfig is reserved, e.g. reserve_initrd_mem() reserves
[initrd_start, PAGE_ALIGN(original initrd_end)), and do_populate_rootfs()
frees it with free_initrd_mem(initrd_start, initrd_end). Thus the full
pages in [PAGE_ALIGN(new initrd_end), PAGE_ALIGN(original initrd_end))
are never freed. This leaks up to 8 pages for a 32KB bootconfig.

Save the original initrd_end in initrd_mem_end when trimming the
bootconfig, and restore initrd_end before freeing the initrd memory.
The bootconfig data has already been copied by xbc_init() at that point.
The trimmed initrd_end is kept when the initrd is retained, so that
/sys/firmware/initrd does not expose the bootconfig.

Fixes: de462e5f1071 ("bootconfig: Fix to remove bootconfig data from initrd while boot")
Cc: stable@vger.kernel.org
Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
---
 include/linux/initrd.h |    2 ++
 init/initramfs.c       |   10 ++++++++++
 init/main.c            |    8 +++++++-
 3 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/include/linux/initrd.h b/include/linux/initrd.h
index 7e5d26c8136f..3086a462da76 100644
--- a/include/linux/initrd.h
+++ b/include/linux/initrd.h
@@ -17,6 +17,8 @@ extern unsigned long initrd_start, initrd_end;
 extern void free_initrd_mem(unsigned long, unsigned long);
 
 #ifdef CONFIG_BLK_DEV_INITRD
+/* initrd_end before trailing data (e.g. bootconfig) was trimmed off */
+extern unsigned long initrd_mem_end;
 extern void __init reserve_initrd_mem(void);
 extern void wait_for_initramfs(void);
 #else
diff --git a/init/initramfs.c b/init/initramfs.c
index ebddde9c8f0d..11030cc1f35d 100644
--- a/init/initramfs.c
+++ b/init/initramfs.c
@@ -582,6 +582,7 @@ char * __init unpack_to_rootfs(char *buf, unsigned long len)
 }
 
 static int __initdata do_retain_initrd;
+unsigned long initrd_mem_end __initdata;
 
 static int __init retain_initrd_param(char *str)
 {
@@ -750,6 +751,15 @@ static void __init do_populate_rootfs(void *unused, async_cookie_t cookie)
 		security_initramfs_populated();
 	}
 
+	/*
+	 * The bootconfig loader trims the bootconfig off initrd_end. Restore
+	 * it to free the memory used by the bootconfig too, unless the initrd
+	 * is retained (which must not expose the bootconfig).
+	 */
+	if (!do_retain_initrd && initrd_start && initrd_end < initrd_mem_end)
+		initrd_end = initrd_mem_end;
+	initrd_mem_end = 0;
+
 	/*
 	 * If the initrd region is overlapped with crashkernel reserved region,
 	 * free only memory that is not part of crashkernel region.
diff --git a/init/main.c b/init/main.c
index 9471ece3d7d5..f14780345051 100644
--- a/init/main.c
+++ b/init/main.c
@@ -323,7 +323,13 @@ static void * __init get_boot_config_from_initrd(size_t *_size)
 		return NULL;
 	}
 
-	/* Remove bootconfig from initramfs/initrd */
+	/*
+	 * Remove bootconfig from initramfs/initrd. Since the initrd memory
+	 * (including the bootconfig) is freed with initrd_end, save the
+	 * original end to free the whole region later.
+	 */
+	if (!initrd_mem_end)
+		initrd_mem_end = initrd_end;
 	initrd_end = (unsigned long)data;
 	if (_size)
 		*_size = size;


      parent reply	other threads:[~2026-11-17 23:47 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17 23:47 [PATCH 0/2] bootconfig: Fix max size padding and initrd memory leak Masami Hiramatsu (Google)
2026-11-17 23:47 ` [PATCH 1/2] bootconfig: Exclude alignment padding from bootconfig max size Masami Hiramatsu (Google)
2026-11-17 23:47 ` Masami Hiramatsu (Google) [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=179495926058.237695.12800083196053168375.stgit@devnote2 \
    --to=mhiramat@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=brauner@kernel.org \
    --cc=jack@suse.cz \
    --cc=leitao@debian.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=rostedt@goodmis.org \
    --cc=ryan.roberts@arm.com \
    --cc=viro@zeniv.linux.org.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox