initramfs: Expose retained initrd as sysfs file
Subsystems (2)
PATCH 1/1 initramfs: Expose retained initrd as sysfs file
When the kernel command line option "retain_initrd" is set, we do not free the initrd memory. However, we also don't expose it to anyone for consumption. That leaves us in a weird situation where the only user of this feature is ppc64 and arm64 specific kexec tooling.
To make it more generally useful, this patch adds a kobject to the firmware object that contains the initrd context when "retain_initrd" is set. That way, we can access the initrd any time after boot from user space and for example hand it into kexec as --initrd parameter if we want to reboot the same initrd. Or inspect it directly locally.
--- .../admin-guide/kernel-parameters.txt | 5 +++-- init/initramfs.c | 18 +++++++++++++++++- 2 files changed, 20 insertions(+), 3 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +3 -2
@@ -2438,7 +2438,7 @@ between unregistering the boot console and initializing the real console. - keepinitrd [HW,ARM] + keepinitrd [HW,ARM] See retain_initrd. kernelcore= [KNL,X86,IA-64,PPC] Format: nn[KMGTPE] | nn% | "mirror"
@@ -5580,7 +5580,8 @@ Useful for devices that are detected asynchronously (e.g. USB and MMC devices). - retain_initrd [RAM] Keep initrd memory after extraction + retain_initrd [RAM] Keep initrd memory after extraction. After boot, it will + be accessible via /sys/firmware/initrd. retbleed= [X86] Control mitigation of RETBleed (Arbitrary Speculative Code Execution with Return Instructions)
init/initramfs.c +17 -1
@@ -574,6 +574,16 @@ extern unsigned long __initramfs_size;#include <linux/initrd.h> #include <linux/kexec.h> +static ssize_t raw_read (struct file *file,struct kobject *kobj, +struct bin_attribute *attr,char *buf, +loff_t pos,size_t count) +{ +memcpy (buf, attr->private + pos, count); +return count; +} + +static BIN_ATTR(initrd, 0440, raw_read, NULL, 0); +void __init reserve_initrd_mem (void ) {phys_addr_t start;
@@ -715,8 +725,14 @@ static void __init do_populate_rootfs(void *unused, async_cookie_t cookie)* If the initrd region is overlapped with crashkernel reserved region, * free only memory that is not part of crashkernel region. */ -if (!do_retain_initrd && initrd_start && !kexec_free_initrd ()) +if (!do_retain_initrd && initrd_start && !kexec_free_initrd ()) {free_initrd_mem (initrd_start, initrd_end); + }else if (do_retain_initrd) { + bin_attr_initrd.size = initrd_end - initrd_start; + bin_attr_initrd.private = (void *)initrd_start; +if (sysfs_create_bin_file (firmware_kobj, &bin_attr_initrd)) +pr_err ("Failed to create initrd sysfs file" ); + } initrd_start =0 ; initrd_end =0 ;
Signature · 12 lines
2.40.1 Amazon Development Center Germany GmbH Krausenstr. 38 10117 Berlin Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B Sitz: Berlin Ust-ID: DE 289 237 879
Quoted 10 lines · On Wed, 6 Dec 2023 14:16:27 +0000 Alexander Graf <[email protected]> wrote:
When the kernel command line option "
retain_initrd" is set, we do not free the initrd memory. However, we also don't expose it to anyone for consumption. That leaves us in a weird situation where the only user of this feature is ppc64 and arm64 specific kexec tooling.To make it more generally useful, this patch adds a kobject to the firmware object that contains the initrd context when "
retain_initrd" is set. That way, we can access the initrd any time after boot from user space and for example hand it into kexec as --initrd parameter if we want to reboot the same initrd. Or inspect it directly locally.
I think it would be helpful if the changelog were mention and describe the new /sys/firmware/initrd. And I assume we should add a Documentation/ABI/testing/sysfs-firmware-initrd.
Quoted 7 lines · Andrew Morton
Quoted 5 lines · On Wed, 6 Dec 2023 14:16:27 +0000 Alexander Graf <[email protected]> wrote:
When the kernel command line option "
retain_initrd" is set, we do not free the initrd memory. However, we also don't expose it to anyone for consumption. That leaves us in a weird situation where the only user of this feature is ppc64 and arm64 specific kexec tooling.
Quoted 8 lines
Quoted 5 lines
To make it more generally useful, this patch adds a kobject to the firmware object that contains the initrd context when "
retain_initrd" is set. That way, we can access the initrd any time after boot from user space and for example hand it into kexec as --initrd parameter if we want to reboot the same initrd. Or inspect it directly locally.I think it would be helpful if the changelog were mention and describe the new
/sys/firmware/initrd. And I assume we should add aDocumentation/ABI/testing/sysfs-firmware-initrd.
Thanks a bunch for the suggestions - let me send v2 with both addressed :)
Alex
Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879