initramfs tidyups
Subsystems (20)
Hi all,
I've spent some time chasing down behavior in initramfs and found plenty of opportunity to improve the code. A first stab on that is contained in this series.
PATCH 1/8 mm: unexport free_reserved_area
This function is only used by built-in code, which makes perfect sense given the purpose of it.
--- mm/page_alloc.c | 1 - 1 file changed, 1 deletion(-)
mm/page_alloc.c +0 -1
@@ -7255,7 +7255,6 @@ unsigned long free_reserved_area(void *start, void *end, int poison, const charreturn pages; } -EXPORT_SYMBOL (free_reserved_area);#ifdef CONFIG_HIGHMEM void free_highmem_page (struct page *page)
Signature · 1 line
2.20.1
PATCH 2/8 initramfs: free initrd memory if opening /initrd.image fails
In reply to Christoph HellwigWe free the initrd memory for all successful or error cases except for the case where opening /initrd.image fails, which looks like an oversight.
--- init/initramfs.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-)
init/initramfs.c +6 -8
@@ -610,13 +610,12 @@ static int __init populate_rootfs(void)printk (KERN_INFO "Trying to unpack rootfs image as initramfs...\n" ); err =unpack_to_rootfs ((char *)initrd_start, initrd_end - initrd_start); -if (!err) { -free_initrd (); +if (!err) goto done; - }else { -clean_rootfs (); -unpack_to_rootfs (__initramfs_start, __initramfs_size); - } + +clean_rootfs (); +unpack_to_rootfs (__initramfs_start, __initramfs_size); +printk (KERN_INFO "rootfs image is not initramfs (%s)" "; looks like an initrd\n" , err); fd = ksys_open("/initrd.image",
@@ -630,7 +629,6 @@ static int __init populate_rootfs(void) written, initrd_end - initrd_start);ksys_close (fd); -free_initrd (); } done:/* empty statement */ ;
@@ -642,9 +640,9 @@ static int __init populate_rootfs(void)printk (KERN_EMERG "Initramfs unpacking failed: %s\n" , err); clean_rootfs(); } -free_initrd ();#endif } +free_initrd ();flush_delayed_fput ();return 0 ; }
Signature · 1 line
2.20.1
Re: [PATCH 2/8] initramfs: free initrd memory if opening /initrd.image fails
Quoted 3 lines · Christoph Hellwig
We free the initrd memory for all successful or error cases except for the case where opening /initrd.image fails, which looks like an oversight.
This also changes the behaviour when CONFIG_INITRAMFS_FORCE is enabled - specifically it means that the initrd is freed (previously it was ignored and never freed). But that seems like reasonable behaviour and the previous behaviour looks like another oversight. FWIW:
Quoted 48 lines
Signed-off-by Christoph Hellwig <[email protected]>--- init/initramfs.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-)init/initramfs.c +6
-8@@ -610,13 +610,12 @@ static int __init populate_rootfs(void)printk (KERN_INFO "Trying to unpack rootfs image as initramfs...\n" ); err =unpack_to_rootfs ((char *)initrd_start, initrd_end - initrd_start); -if (!err) { -free_initrd (); +if (!err) goto done; - }else { -clean_rootfs (); -unpack_to_rootfs (__initramfs_start, __initramfs_size); - } + +clean_rootfs (); +unpack_to_rootfs (__initramfs_start, __initramfs_size); +printk (KERN_INFO "rootfs image is not initramfs (%s)" "; looks like an initrd\n" , err); fd = ksys_open("/initrd.image",@@ -630,7 +629,6 @@ static int __init populate_rootfs(void) written, initrd_end - initrd_start);ksys_close (fd); -free_initrd (); } done:/* empty statement */ ;@@ -642,9 +640,9 @@ static int __init populate_rootfs(void)printk (KERN_EMERG "Initramfs unpacking failed: %s\n" , err); clean_rootfs(); } -free_initrd ();#endif } +free_initrd ();flush_delayed_fput ();return 0 ; }
PATCH 3/8 initramfs: cleanup initrd freeing
In reply to Christoph HellwigFactor the kexec logic into a separate helper, and then inline the rest of free_initrd into the only caller.
--- init/initramfs.c | 53 +++++++++++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 23 deletions(-)
init/initramfs.c +30 -23
@@ -518,37 +518,35 @@ extern unsigned long __initramfs_size;#include <linux/initrd.h> #include <linux/kexec.h> -static void __init free_initrd (void ) -{#ifdef CONFIG_KEXEC_CORE +static bool kexec_free_initrd (void ) +{unsigned long crashk_start = (unsigned long )__va (crashk_res.start);unsigned long crashk_end = (unsigned long )__va (crashk_res.end); -#endif -if (do_retain_initrd) - goto skip; -#ifdef CONFIG_KEXEC_CORE /* * If the initrd region is overlapped with crashkernel reserved region, * free only memory that is not part of crashkernel region. */ -if (initrd_start < crashk_end && initrd_end > crashk_start) { -/* -* Initialize initrd memory region since the kexec boot does -* not do. -*/ -memset ((void *)initrd_start,0 , initrd_end - initrd_start); -if (initrd_start < crashk_start) -free_initrd_mem (initrd_start, crashk_start); -if (initrd_end > crashk_end) -free_initrd_mem (crashk_end, initrd_end); - } else -#endif -free_initrd_mem (initrd_start, initrd_end); -skip: - initrd_start =0 ; - initrd_end =0 ; +if (initrd_start >= crashk_end || initrd_end <= crashk_start) +return false; + +/* +* Initialize initrd memory region since the kexec boot does not do. +*/ +memset ((void *)initrd_start,0 , initrd_end - initrd_start); +if (initrd_start < crashk_start) +free_initrd_mem (initrd_start, crashk_start); +if (initrd_end > crashk_end) +free_initrd_mem (crashk_end, initrd_end); +return true; } +#else +static inline bool kexec_free_initrd (void ) +{ +return false; +} +#endif /* CONFIG_KEXEC_CORE */ #define BUF_SIZE 1024static void __init clean_rootfs (void )
@@ -642,7 +640,16 @@ static int __init populate_rootfs(void) }#endif } -free_initrd (); + +/* +* If the initrd region is overlapped with crashkernel reserved region, +* free only memory that is not part of crashkernel region. +*/ +if (!do_retain_initrd && !kexec_free_initrd ()) +free_initrd_mem (initrd_start, initrd_end); + initrd_start =0 ; + initrd_end =0 ; +flush_delayed_fput ();return 0 ; }
Signature · 1 line
2.20.1
PATCH 4/8 initramfs: factor out a helper to populate the initrd image
In reply to Christoph HellwigThis will allow for cleaner code sharing in the caller.
--- init/initramfs.c | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-)
init/initramfs.c +23 -17
@@ -595,6 +595,28 @@ static void __init clean_rootfs(void)kfree (buf); } +#ifdef CONFIG_BLK_DEV_RAM +static void populate_initrd_image (char *err) +{ +ssize_t written; +int fd; + +unpack_to_rootfs (__initramfs_start, __initramfs_size); + +printk (KERN_INFO "rootfs image is not initramfs (%s); looks like an initrd\n" , + err); + fd =ksys_open ("/initrd.image" ,O_WRONLY |O_CREAT ,0700 ); +if (fd <0 ) +return ; + + written =xwrite (fd, (char *)initrd_start, initrd_end - initrd_start); +if (written != initrd_end - initrd_start) +pr_err ("/initrd.image: incomplete write (%zd != %ld)\n" , + written, initrd_end - initrd_start); +ksys_close (fd); +} +#endif /* CONFIG_BLK_DEV_RAM */ +static int __init populate_rootfs (void ) {/* Load the built in initramfs */
@@ -604,7 +626,6 @@ static int __init populate_rootfs(void)/* If available load the bootloader supplied initrd */ if (initrd_start && !IS_ENABLED (CONFIG_INITRAMFS_FORCE )) {#ifdef CONFIG_BLK_DEV_RAM -int fd;printk (KERN_INFO "Trying to unpack rootfs image as initramfs...\n" ); err =unpack_to_rootfs ((char *)initrd_start, initrd_end - initrd_start);
@@ -612,22 +633,7 @@ static int __init populate_rootfs(void) goto done;clean_rootfs (); -unpack_to_rootfs (__initramfs_start, __initramfs_size); - -printk (KERN_INFO "rootfs image is not initramfs (%s)" -"; looks like an initrd\n" , err); - fd =ksys_open ("/initrd.image" , -O_WRONLY |O_CREAT ,0700 ); -if (fd >=0 ) { -ssize_t written =xwrite (fd, (char *)initrd_start, - initrd_end - initrd_start); - -if (written != initrd_end - initrd_start) -pr_err ("/initrd.image: incomplete write (%zd != %ld)\n" , - written, initrd_end - initrd_start); - -ksys_close (fd); - } +populate_initrd_image (err); done:/* empty statement */ ;#else
Signature · 1 line
2.20.1
PATCH 5/8 initramfs: cleanup populate_rootfs
In reply to Christoph HellwigThe code for kernels that support ramdisks or not is mostly the same. Unify it by using an IS_ENABLED for the info message, and moving the error message into a stub for populate_initrd_image.
--- init/initramfs.c | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-)
init/initramfs.c +15 -18
@@ -615,6 +615,11 @@ static void populate_initrd_image(char *err) written, initrd_end - initrd_start); ksys_close(fd); } +#else +static void populate_initrd_image (char *err) +{ +printk (KERN_EMERG "Initramfs unpacking failed: %s\n" , err); +}#endif /* CONFIG_BLK_DEV_RAM */ static int __init populate_rootfs (void )
@@ -623,30 +628,22 @@ static int __init populate_rootfs(void)char *err =unpack_to_rootfs (__initramfs_start, __initramfs_size);if (err)panic ("%s" , err);/* Failed to decompress INTERNAL initramfs */ -/* If available load the bootloader supplied initrd */ -if (initrd_start && !IS_ENABLED (CONFIG_INITRAMFS_FORCE )) { -#ifdef CONFIG_BLK_DEV_RAM + +if (!initrd_start ||IS_ENABLED (CONFIG_INITRAMFS_FORCE )) + goto done; + +if (IS_ENABLED (CONFIG_BLK_DEV_RAM ))printk (KERN_INFO "Trying to unpack rootfs image as initramfs...\n" ); - err =unpack_to_rootfs ((char *)initrd_start, - initrd_end - initrd_start); -if (!err) - goto done; +else +printk (KERN_INFO "Unpacking initramfs...\n" ); + err =unpack_to_rootfs ((char *)initrd_start, initrd_end - initrd_start); +if (err) {clean_rootfs ();populate_initrd_image (err); - done: -/* empty statement */ ; -#else -printk (KERN_INFO "Unpacking initramfs...\n" ); - err =unpack_to_rootfs ((char *)initrd_start, - initrd_end - initrd_start); -if (err) { -printk (KERN_EMERG "Initramfs unpacking failed: %s\n" , err); -clean_rootfs (); - } -#endif } +done:/* * If the initrd region is overlapped with crashkernel reserved region, * free only memory that is not part of crashkernel region.
Signature · 1 line
2.20.1
PATCH 6/8 initramfs: move the legacy keepinitrd parameter to core code
In reply to Christoph HellwigNo need to handle the freeing disable in arch code when we already have a core hook (and a different name for the option) for it.
--- arch/Kconfig | 7 +++++++ arch/arm/Kconfig | 1 + arch/arm/mm/init.c | 25 ++++++------------------- arch/arm64/Kconfig | 1 + arch/arm64/mm/init.c | 17 ++--------------- arch/unicore32/Kconfig | 1 + arch/unicore32/mm/init.c | 14 +------------- init/initramfs.c | 9 +++++++++ 8 files changed, 28 insertions(+), 47 deletions(-)
arch/Kconfig +7 -0
@@ -245,6 +245,13 @@ config ARCH_HAS_FORTIFY_SOURCE An architecture should select this when it can successfully build and run with CONFIG_FORTIFY_SOURCE. +# +# Select if the arch provides a historic keepinit alias for the retain_initrd +# command line option +# +config ARCH_HAS_KEEPINITRD + bool + # Select if arch has all set_memory_ro/rw/x/nx() functions in asm/cacheflush.h config ARCH_HAS_SET_MEMORY bool
arch/arm/Kconfig +1 -0
@@ -8,6 +8,7 @@ config ARM select ARCH_HAS_DEVMEM_IS_ALLOWED select ARCH_HAS_ELF_RANDOMIZE select ARCH_HAS_FORTIFY_SOURCE + select ARCH_HAS_KEEPINITRD select ARCH_HAS_KCOV select ARCH_HAS_MEMBARRIER_SYNC_CORE select ARCH_HAS_PTE_SPECIAL if ARM_LPAE
arch/arm/mm/init.c +6 -19
@@ -752,27 +752,14 @@ void free_initmem(void) }#ifdef CONFIG_BLK_DEV_INITRD - -static int keep_initrd; -void free_initrd_mem (unsigned long start,unsigned long end) { -if (!keep_initrd) { -if (start == initrd_start) - start =round_down (start,PAGE_SIZE ); -if (end == initrd_end) - end =round_up (end,PAGE_SIZE ); +if (start == initrd_start) + start =round_down (start,PAGE_SIZE ); +if (end == initrd_end) + end =round_up (end,PAGE_SIZE ); -poison_init_mem ((void *)start,PAGE_ALIGN (end) - start); -free_reserved_area ((void *)start, (void *)end,-1 ,"initrd" ); - } +poison_init_mem ((void *)start,PAGE_ALIGN (end) - start); +free_reserved_area ((void *)start, (void *)end,-1 ,"initrd" ); } - -static int __init keepinitrd_setup (char *__unused) -{ - keep_initrd =1 ; -return 1 ; -} - -__setup ("keepinitrd" , keepinitrd_setup);#endif
arch/arm64/Kconfig +1 -0
@@ -19,6 +19,7 @@ config ARM64 select ARCH_HAS_FORTIFY_SOURCE select ARCH_HAS_GCOV_PROFILE_ALL select ARCH_HAS_GIGANTIC_PAGE if (MEMORY_ISOLATION && COMPACTION) || CMA + select ARCH_HAS_KEEPINITRD select ARCH_HAS_KCOV select ARCH_HAS_MEMBARRIER_SYNC_CORE select ARCH_HAS_PTE_SPECIAL
arch/arm64/mm/init.c +2 -15
@@ -622,24 +622,11 @@ void free_initmem(void) }#ifdef CONFIG_BLK_DEV_INITRD - -static int keep_initrd__initdata ; -void __init free_initrd_mem (unsigned long start,unsigned long end) { -if (!keep_initrd) { -free_reserved_area ((void *)start, (void *)end,0 ,"initrd" ); -memblock_free (__virt_to_phys (start), end - start); - } -} - -static int __init keepinitrd_setup (char *__unused) -{ - keep_initrd =1 ; -return 1 ; +free_reserved_area ((void *)start, (void *)end,0 ,"initrd" ); +memblock_free (__virt_to_phys (start), end - start); } - -__setup ("keepinitrd" , keepinitrd_setup);#endif /*
arch/unicore32/Kconfig +1 -0
@@ -2,6 +2,7 @@ config UNICORE32 def_bool y select ARCH_HAS_DEVMEM_IS_ALLOWED + select ARCH_HAS_KEEPINITRD select ARCH_MIGHT_HAVE_PC_PARPORT select ARCH_MIGHT_HAVE_PC_SERIO select HAVE_GENERIC_DMA_COHERENT
arch/unicore32/mm/init.c +1 -13
@@ -318,20 +318,8 @@ void free_initmem(void) }#ifdef CONFIG_BLK_DEV_INITRD - -static int keep_initrd; -void free_initrd_mem (unsigned long start,unsigned long end) { -if (!keep_initrd) -free_reserved_area ((void *)start, (void *)end,-1 ,"initrd" ); -} - -static int __init keepinitrd_setup (char *__unused) -{ - keep_initrd =1 ; -return 1 ; +free_reserved_area ((void *)start, (void *)end,-1 ,"initrd" ); } - -__setup ("keepinitrd" , keepinitrd_setup);#endif
init/initramfs.c +9 -0
@@ -513,6 +513,15 @@ static int __init retain_initrd_param(char *str) }__setup ("retain_initrd" , retain_initrd_param); +#ifdef CONFIG_ARCH_HAS_KEEPINITRD +static int __init keepinitrd_setup (char *__unused) +{ + do_retain_initrd =1 ; +return 1 ; +} +__setup ("keepinitrd" , keepinitrd_setup); +#endif +extern char __initramfs_start[];extern unsigned long __initramfs_size;#include <linux/initrd.h>
Signature · 1 line
2.20.1
Re: [PATCH 6/8] initramfs: move the legacy keepinitrd parameter to core code
Quoted 4 lines · Christoph Hellwig
No need to handle the freeing disable in arch code when we already have a core hook (and a different name for the option) for it.
Signed-off-by Christoph Hellwig <[email protected]>
Quoted 10 lines
--- arch/Kconfig | 7 +++++++ arch/arm/Kconfig | 1 + arch/arm/mm/init.c | 25 ++++++------------------- arch/arm64/Kconfig | 1 + arch/arm64/mm/init.c | 17 ++--------------- arch/unicore32/Kconfig | 1 + arch/unicore32/mm/init.c | 14 +------------- init/initramfs.c | 9 +++++++++ 8 files changed, 28 insertions(+), 47 deletions(-)
For arm64:
PATCH 7/8 initramfs: proide a generic free_initrd_mem implementation
In reply to Christoph HellwigFor most architectures free_initrd_mem just expands to the same free_reserved_area call. Provide that as a generic implementation marked __weak.
--- arch/alpha/mm/init.c | 8 -------- arch/arc/mm/init.c | 7 ------- arch/c6x/mm/init.c | 7 ------- arch/h8300/mm/init.c | 8 -------- arch/m68k/mm/init.c | 7 ------- arch/microblaze/mm/init.c | 7 ------- arch/nds32/mm/init.c | 7 ------- arch/nios2/mm/init.c | 7 ------- arch/openrisc/mm/init.c | 7 ------- arch/parisc/mm/init.c | 7 ------- arch/powerpc/mm/mem.c | 7 ------- arch/sh/mm/init.c | 7 ------- arch/um/kernel/mem.c | 7 ------- arch/unicore32/mm/init.c | 7 ------- init/initramfs.c | 5 +++++ 15 files changed, 5 insertions(+), 100 deletions(-)
arch/alpha/mm/init.c +0 -8
@@ -291,11 +291,3 @@ free_initmem(void) {free_initmem_default (-1 ); } - -#ifdef CONFIG_BLK_DEV_INITRD -void -free_initrd_mem (unsigned long start,unsigned long end) -{ -free_reserved_area ((void *)start, (void *)end,-1 ,"initrd" ); -} -#endif
arch/arc/mm/init.c +0 -7
@@ -214,10 +214,3 @@ void __ref free_initmem(void) {free_initmem_default (-1 ); } - -#ifdef CONFIG_BLK_DEV_INITRD -void __init free_initrd_mem (unsigned long start,unsigned long end) -{ -free_reserved_area ((void *)start, (void *)end,-1 ,"initrd" ); -} -#endif
arch/c6x/mm/init.c +0 -7
@@ -67,13 +67,6 @@ void __init mem_init(void)mem_init_print_info (NULL ); } -#ifdef CONFIG_BLK_DEV_INITRD -void __init free_initrd_mem (unsigned long start,unsigned long end) -{ -free_reserved_area ((void *)start, (void *)end,-1 ,"initrd" ); -} -#endif -void __init free_initmem (void ) {free_initmem_default (-1 );
arch/h8300/mm/init.c +0 -8
@@ -101,14 +101,6 @@ void __init mem_init(void)mem_init_print_info (NULL ); } - -#ifdef CONFIG_BLK_DEV_INITRD -void free_initrd_mem (unsigned long start,unsigned long end) -{ -free_reserved_area ((void *)start, (void *)end,-1 ,"initrd" ); -} -#endif -void free_initmem (void ) {
arch/m68k/mm/init.c +0 -7
@@ -144,10 +144,3 @@ void __init mem_init(void)init_pointer_tables ();mem_init_print_info (NULL ); } - -#ifdef CONFIG_BLK_DEV_INITRD -void free_initrd_mem (unsigned long start,unsigned long end) -{ -free_reserved_area ((void *)start, (void *)end,-1 ,"initrd" ); -} -#endif
arch/microblaze/mm/init.c +0 -7
@@ -186,13 +186,6 @@ void __init setup_memory(void)paging_init (); } -#ifdef CONFIG_BLK_DEV_INITRD -void free_initrd_mem (unsigned long start,unsigned long end) -{ -free_reserved_area ((void *)start, (void *)end,-1 ,"initrd" ); -} -#endif -void free_initmem (void ) {free_initmem_default (-1 );
arch/nds32/mm/init.c +0 -7
@@ -249,13 +249,6 @@ void free_initmem(void)free_initmem_default (-1 ); } -#ifdef CONFIG_BLK_DEV_INITRD -void free_initrd_mem (unsigned long start,unsigned long end) -{ -free_reserved_area ((void *)start, (void *)end,-1 ,"initrd" ); -} -#endif -void __set_fixmap (enum fixed_addresses idx,phys_addr_t phys,pgprot_t flags) {
arch/nios2/mm/init.c +0 -7
@@ -82,13 +82,6 @@ void __init mmu_init(void)flush_tlb_all (); } -#ifdef CONFIG_BLK_DEV_INITRD -void __init free_initrd_mem (unsigned long start,unsigned long end) -{ -free_reserved_area ((void *)start, (void *)end,-1 ,"initrd" ); -} -#endif -void __ref free_initmem (void ) {free_initmem_default (-1 );
arch/openrisc/mm/init.c +0 -7
@@ -221,13 +221,6 @@ void __init mem_init(void)return ; } -#ifdef CONFIG_BLK_DEV_INITRD -void free_initrd_mem (unsigned long start,unsigned long end) -{ -free_reserved_area ((void *)start, (void *)end,-1 ,"initrd" ); -} -#endif -void free_initmem (void ) {free_initmem_default (-1 );
arch/parisc/mm/init.c +0 -7
@@ -935,10 +935,3 @@ void flush_tlb_all(void) spin_unlock(&sid_lock); }#endif - -#ifdef CONFIG_BLK_DEV_INITRD -void free_initrd_mem (unsigned long start,unsigned long end) -{ -free_reserved_area ((void *)start, (void *)end,-1 ,"initrd" ); -} -#endif
arch/powerpc/mm/mem.c +0 -7
@@ -388,13 +388,6 @@ void free_initmem(void)free_initmem_default (POISON_FREE_INITMEM ); } -#ifdef CONFIG_BLK_DEV_INITRD -void __init free_initrd_mem (unsigned long start,unsigned long end) -{ -free_reserved_area ((void *)start, (void *)end,-1 ,"initrd" ); -} -#endif -/* * This is called when a page has been modified by the kernel. * It just marks the page as not i-cache clean. We do the i-cache
arch/sh/mm/init.c +0 -7
@@ -410,13 +410,6 @@ void free_initmem(void)free_initmem_default (-1 ); } -#ifdef CONFIG_BLK_DEV_INITRD -void free_initrd_mem (unsigned long start,unsigned long end) -{ -free_reserved_area ((void *)start, (void *)end,-1 ,"initrd" ); -} -#endif -#ifdef CONFIG_MEMORY_HOTPLUG int arch_add_memory (int nid,u64 start,u64 size,struct vmem_altmap *altmap,bool want_memblock)
arch/um/kernel/mem.c +0 -7
@@ -172,13 +172,6 @@ void free_initmem(void) { } -#ifdef CONFIG_BLK_DEV_INITRD -void free_initrd_mem (unsigned long start,unsigned long end) -{ -free_reserved_area ((void *)start, (void *)end,-1 ,"initrd" ); -} -#endif -/* Allocate and free page tables. */ pgd_t *pgd_alloc (struct mm_struct *mm)
arch/unicore32/mm/init.c +0 -7
@@ -316,10 +316,3 @@ void free_initmem(void) {free_initmem_default (-1 ); } - -#ifdef CONFIG_BLK_DEV_INITRD -void free_initrd_mem (unsigned long start,unsigned long end) -{ -free_reserved_area ((void *)start, (void *)end,-1 ,"initrd" ); -} -#endif
init/initramfs.c +5 -0
@@ -527,6 +527,11 @@ extern unsigned long __initramfs_size;#include <linux/initrd.h> #include <linux/kexec.h> +void __weak free_initrd_mem (unsigned long start,unsigned long end) +{ +free_reserved_area ((void *)start, (void *)end,-1 ,"initrd" ); +} +#ifdef CONFIG_KEXEC_CORE static bool kexec_free_initrd (void ) {
Signature · 1 line
2.20.1
Re: [PATCH 7/8] initramfs: proide a generic free_initrd_mem implementation
Quoted 9 lines · Christoph Hellwig
For most architectures
free_initrd_memjust expands to the samefree_reserved_areacall. Provide that as a generic implementation marked__weak.Signed-off-by Christoph Hellwig <[email protected]>--- arch/alpha/mm/init.c | 8 -------- arch/arc/mm/init.c | 7 ------- arch/c6x/mm/init.c | 7 -------
csky seems to open-code free_reserved_page with the only difference that it's also increments totalram_pages for the freed pages, which doesn't seem correct anyway...
That said, I suppose arch/csky can be also added to the party.
Quoted 13 lines
arch/h8300/mm/init.c | 8 -------- arch/m68k/mm/init.c | 7 ------- arch/microblaze/mm/init.c | 7 ------- arch/nds32/mm/init.c | 7 ------- arch/nios2/mm/init.c | 7 ------- arch/openrisc/mm/init.c | 7 ------- arch/parisc/mm/init.c | 7 ------- arch/powerpc/mm/mem.c | 7 ------- arch/sh/mm/init.c | 7 ------- arch/um/kernel/mem.c | 7 ------- arch/unicore32/mm/init.c | 7 ------- init/initramfs.c | 5 +++++ 15 files changed, 5 insertions(+), 100 deletions(-)
...
init/initramfs.c extern unsigned long __initramfs_size; · line 532
Quoted 11 lines
init/initramfs.c +3
-0@@ -527,6 +527,11 @@ extern unsigned long __initramfs_size;#include <linux/initrd.h> #include <linux/kexec.h> +void __weak free_initrd_mem (unsigned long start,unsigned long end) +{ +free_reserved_area ((void *)start, (void *)end,-1 ,"initrd" );
Some architectures have pr_info("Freeing initrd memory..."), I'd add it for the generic version as well.
Another thing that I was thinking of is that x86 has all those memory protection calls in its free_initrd_mem, maybe it'd make sense to have them in the generic version as well?
Quoted 8 lines
init/initramfs.c +2
-0+} +#ifdef CONFIG_KEXEC_CORE static bool kexec_free_initrd (void ) {Signature · 1 line
2.20.1
Signature · 2 lines
Sincerely yours, Mike.
Re: [PATCH 7/8] initramfs: proide a generic free_initrd_mem implementation
Quoted 5 lines · Mike Rapoport
csky seems to open-code
free_reserved_pagewith the only difference that it's also incrementstotalram_pagesfor the freed pages, which doesn't seem correct anyway...That said, I suppose
arch/cskycan be also added to the party.
Yes, I noticed that. But I'd rather move it over manually in another patch post rc1 or for the next merge window.
Quoted 6 lines
init/initramfs.c extern unsigned long __initramfs_size; · line 532
Quoted 3 lines
init/initramfs.c +3
-0+void __weak free_initrd_mem (unsigned long start,unsigned long end) +{ +free_reserved_area ((void *)start, (void *)end,-1 ,"initrd" );Some architectures have
pr_info("Freeing initrd memory..."), I'd add it for the generic version as well.
Well, if we think such a printk is useful it should probably be moved to the caller in init/initramfs.c instead. I can include a patch for that in the next iteration of the series.
Quoted 3 lines
Another thing that I was thinking of is that x86 has all those memory protection calls in its
free_initrd_mem, maybe it'd make sense to have them in the generic version as well?
Maybe. But I'd rather keep it out of the initial series as it looks a little more complicated. Having a single implementation of free_initrd_mem would be great, though.
Re: [PATCH 7/8] initramfs: proide a generic free_initrd_mem implementation
Quoted 9 lines · Christoph Hellwig
Quoted 5 lines · Mike Rapoport
csky seems to open-code
free_reserved_pagewith the only difference that it's also incrementstotalram_pagesfor the freed pages, which doesn't seem correct anyway...That said, I suppose
arch/cskycan be also added to the party.Yes, I noticed that. But I'd rather move it over manually in another patch post rc1 or for the next merge window.
Fair enough.
Quoted 10 lines
Quoted 6 lines
init/initramfs.c extern unsigned long __initramfs_size; · line 532
Quoted 3 lines
init/initramfs.c +3
-0+void __weak free_initrd_mem (unsigned long start,unsigned long end) +{ +free_reserved_area ((void *)start, (void *)end,-1 ,"initrd" );Some architectures have
pr_info("Freeing initrd memory..."), I'd add it for the generic version as well.Well, if we think such a printk is useful it should probably be moved to the caller in
init/initramfs.cinstead. I can include a patch for that in the next iteration of the series.
I found it useful during board bring ups, this gave some starting point when everything hangs and you are out to catch the lion in the desert.
Quoted 7 lines
Quoted 3 lines
Another thing that I was thinking of is that x86 has all those memory protection calls in its
free_initrd_mem, maybe it'd make sense to have them in the generic version as well?Maybe. But I'd rather keep it out of the initial series as it looks a little more complicated. Having a single implementation of
free_initrd_memwould be great, though.
Ok.
BTW, the memblock_free() arm64 does, seems to be relevant for architectures with CONFIG_ARCH_DISCARD_MEMBLOCK=n.
On powerpc the freed initrd region shows up in /sys/kernel/debug/memblock/reserved.
Signature · 2 lines
Sincerely yours, Mike.
Re: [PATCH 7/8] initramfs: proide a generic free_initrd_mem implementation
In reply to Christoph HellwigQuoted 7 lines · Christoph Hellwig
For most architectures
free_initrd_memjust expands to the samefree_reserved_areacall. Provide that as a generic implementation marked__weak.Signed-off-by Christoph Hellwig <[email protected]>arch/m68k/mm/init.c | 7 -------
Gr{oetje,eeting}s,
Geert
Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected] In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds
Re: [PATCH 7/8] initramfs: proide a generic free_initrd_mem implementation
In reply to Christoph HellwigQuoted 1 line
Subject: initramfs: proide a generic
free_initrd_memimplementation
Nit: ^ provide
Quoted 281 lines · Christoph Hellwig
For most architectures
free_initrd_memjust expands to the samefree_reserved_areacall. Provide that as a generic implementation marked__weak.Signed-off-by Christoph Hellwig <[email protected]>--- arch/alpha/mm/init.c | 8 -------- arch/arc/mm/init.c | 7 ------- arch/c6x/mm/init.c | 7 ------- arch/h8300/mm/init.c | 8 -------- arch/m68k/mm/init.c | 7 ------- arch/microblaze/mm/init.c | 7 ------- arch/nds32/mm/init.c | 7 ------- arch/nios2/mm/init.c | 7 ------- arch/openrisc/mm/init.c | 7 ------- arch/parisc/mm/init.c | 7 ------- arch/powerpc/mm/mem.c | 7 ------- arch/sh/mm/init.c | 7 ------- arch/um/kernel/mem.c | 7 ------- arch/unicore32/mm/init.c | 7 ------- init/initramfs.c | 5 +++++ 15 files changed, 5 insertions(+), 100 deletions(-)arch/alpha/mm/init.c +0
-8@@ -291,11 +291,3 @@ free_initmem(void) {free_initmem_default (-1 ); } - -#ifdef CONFIG_BLK_DEV_INITRD -void -free_initrd_mem (unsigned long start,unsigned long end) -{ -free_reserved_area ((void *)start, (void *)end,-1 ,"initrd" ); -} -#endif arch/arc/mm/init.c +0
-7@@ -214,10 +214,3 @@ void __ref free_initmem(void) {free_initmem_default (-1 ); } - -#ifdef CONFIG_BLK_DEV_INITRD -void __init free_initrd_mem (unsigned long start,unsigned long end) -{ -free_reserved_area ((void *)start, (void *)end,-1 ,"initrd" ); -} -#endif arch/c6x/mm/init.c +0
-7@@ -67,13 +67,6 @@ void __init mem_init(void)mem_init_print_info (NULL ); } -#ifdef CONFIG_BLK_DEV_INITRD -void __init free_initrd_mem (unsigned long start,unsigned long end) -{ -free_reserved_area ((void *)start, (void *)end,-1 ,"initrd" ); -} -#endif -void __init free_initmem (void ) {free_initmem_default (-1 );arch/h8300/mm/init.c +0
-8@@ -101,14 +101,6 @@ void __init mem_init(void)mem_init_print_info (NULL ); } - -#ifdef CONFIG_BLK_DEV_INITRD -void free_initrd_mem (unsigned long start,unsigned long end) -{ -free_reserved_area ((void *)start, (void *)end,-1 ,"initrd" ); -} -#endif -void free_initmem (void ) {arch/m68k/mm/init.c +0
-7@@ -144,10 +144,3 @@ void __init mem_init(void)init_pointer_tables ();mem_init_print_info (NULL ); } - -#ifdef CONFIG_BLK_DEV_INITRD -void free_initrd_mem (unsigned long start,unsigned long end) -{ -free_reserved_area ((void *)start, (void *)end,-1 ,"initrd" ); -} -#endif arch/microblaze/mm/init.c +0
-7@@ -186,13 +186,6 @@ void __init setup_memory(void)paging_init (); } -#ifdef CONFIG_BLK_DEV_INITRD -void free_initrd_mem (unsigned long start,unsigned long end) -{ -free_reserved_area ((void *)start, (void *)end,-1 ,"initrd" ); -} -#endif -void free_initmem (void ) {free_initmem_default (-1 );arch/nds32/mm/init.c +0
-7@@ -249,13 +249,6 @@ void free_initmem(void)free_initmem_default (-1 ); } -#ifdef CONFIG_BLK_DEV_INITRD -void free_initrd_mem (unsigned long start,unsigned long end) -{ -free_reserved_area ((void *)start, (void *)end,-1 ,"initrd" ); -} -#endif -void __set_fixmap (enum fixed_addresses idx,phys_addr_t phys,pgprot_t flags) {arch/nios2/mm/init.c +0
-7@@ -82,13 +82,6 @@ void __init mmu_init(void)flush_tlb_all (); } -#ifdef CONFIG_BLK_DEV_INITRD -void __init free_initrd_mem (unsigned long start,unsigned long end) -{ -free_reserved_area ((void *)start, (void *)end,-1 ,"initrd" ); -} -#endif -void __ref free_initmem (void ) {free_initmem_default (-1 );arch/openrisc/mm/init.c +0
-7@@ -221,13 +221,6 @@ void __init mem_init(void)return ; } -#ifdef CONFIG_BLK_DEV_INITRD -void free_initrd_mem (unsigned long start,unsigned long end) -{ -free_reserved_area ((void *)start, (void *)end,-1 ,"initrd" ); -} -#endif -void free_initmem (void ) {free_initmem_default (-1 );arch/parisc/mm/init.c +0
-7@@ -935,10 +935,3 @@ void flush_tlb_all(void) spin_unlock(&sid_lock); }#endif - -#ifdef CONFIG_BLK_DEV_INITRD -void free_initrd_mem (unsigned long start,unsigned long end) -{ -free_reserved_area ((void *)start, (void *)end,-1 ,"initrd" ); -} -#endif arch/powerpc/mm/mem.c +0
-7@@ -388,13 +388,6 @@ void free_initmem(void)free_initmem_default (POISON_FREE_INITMEM ); } -#ifdef CONFIG_BLK_DEV_INITRD -void __init free_initrd_mem (unsigned long start,unsigned long end) -{ -free_reserved_area ((void *)start, (void *)end,-1 ,"initrd" ); -} -#endif -/* * This is called when a page has been modified by the kernel. * It just marks the page as not i-cache clean. We do the i-cache arch/sh/mm/init.c +0
-7@@ -410,13 +410,6 @@ void free_initmem(void)free_initmem_default (-1 ); } -#ifdef CONFIG_BLK_DEV_INITRD -void free_initrd_mem (unsigned long start,unsigned long end) -{ -free_reserved_area ((void *)start, (void *)end,-1 ,"initrd" ); -} -#endif -#ifdef CONFIG_MEMORY_HOTPLUG int arch_add_memory (int nid,u64 start,u64 size,struct vmem_altmap *altmap,bool want_memblock)arch/um/kernel/mem.c +0
-7@@ -172,13 +172,6 @@ void free_initmem(void) { } -#ifdef CONFIG_BLK_DEV_INITRD -void free_initrd_mem (unsigned long start,unsigned long end) -{ -free_reserved_area ((void *)start, (void *)end,-1 ,"initrd" ); -} -#endif -/* Allocate and free page tables. */ pgd_t *pgd_alloc (struct mm_struct *mm)arch/unicore32/mm/init.c +0
-7@@ -316,10 +316,3 @@ void free_initmem(void) {free_initmem_default (-1 ); } - -#ifdef CONFIG_BLK_DEV_INITRD -void free_initrd_mem (unsigned long start,unsigned long end) -{ -free_reserved_area ((void *)start, (void *)end,-1 ,"initrd" ); -} -#endif init/initramfs.c +5
-0@@ -527,6 +527,11 @@ extern unsigned long __initramfs_size;#include <linux/initrd.h> #include <linux/kexec.h> +void __weak free_initrd_mem (unsigned long start,unsigned long end) +{ +free_reserved_area ((void *)start, (void *)end,-1 ,"initrd" ); +} +#ifdef CONFIG_KEXEC_CORE static bool kexec_free_initrd (void ) {Signature · 1 line
2.20.1
Signature · 2 lines
Sincerely yours, Mike.
PATCH 8/8 initramfs: poison freed initrd memory
In reply to Christoph HellwigVarious architectures including x86 poison the freed initrd memory. Do the same in the generic free_initrd_mem implementation and switch a few more architectures that are identical to the generic code over to it now.
--- arch/mips/mm/init.c | 8 -------- arch/s390/mm/init.c | 8 -------- arch/sparc/mm/init_32.c | 8 -------- arch/sparc/mm/init_64.c | 8 -------- init/initramfs.c | 3 ++- 5 files changed, 2 insertions(+), 33 deletions(-)
arch/mips/mm/init.c +0 -8
@@ -492,14 +492,6 @@ void free_init_pages(const char *what, unsigned long begin, unsigned long end)printk (KERN_INFO "Freeing %s: %ldk freed\n" , what, (end - begin) >>10 ); } -#ifdef CONFIG_BLK_DEV_INITRD -void free_initrd_mem (unsigned long start,unsigned long end) -{ -free_reserved_area ((void *)start, (void *)end,POISON_FREE_INITMEM , -"initrd" ); -} -#endif -void (*free_init_pages_eva)(void *begin,void *end) =NULL ;void __ref free_initmem (void )
arch/s390/mm/init.c +0 -8
@@ -154,14 +154,6 @@ void free_initmem(void)free_initmem_default (POISON_FREE_INITMEM ); } -#ifdef CONFIG_BLK_DEV_INITRD -void __init free_initrd_mem (unsigned long start,unsigned long end) -{ -free_reserved_area ((void *)start, (void *)end,POISON_FREE_INITMEM , -"initrd" ); -} -#endif -unsigned long memory_block_size_bytes (void ) {/*
arch/sparc/mm/init_32.c +0 -8
@@ -299,14 +299,6 @@ void free_initmem (void)free_initmem_default (POISON_FREE_INITMEM ); } -#ifdef CONFIG_BLK_DEV_INITRD -void free_initrd_mem (unsigned long start,unsigned long end) -{ -free_reserved_area ((void *)start, (void *)end,POISON_FREE_INITMEM , -"initrd" ); -} -#endif -void sparc_flush_page_to_ram (struct page *page) {unsigned long vaddr = (unsigned long )page_address (page);
arch/sparc/mm/init_64.c +0 -8
@@ -2602,14 +2602,6 @@ void free_initmem(void) } } -#ifdef CONFIG_BLK_DEV_INITRD -void free_initrd_mem (unsigned long start,unsigned long end) -{ -free_reserved_area ((void *)start, (void *)end,POISON_FREE_INITMEM , -"initrd" ); -} -#endif -pgprot_t PAGE_KERNEL __read_mostly ;EXPORT_SYMBOL (PAGE_KERNEL );
init/initramfs.c +2 -1
@@ -529,7 +529,8 @@ extern unsigned long __initramfs_size;void __weak free_initrd_mem (unsigned long start,unsigned long end) { -free_reserved_area ((void *)start, (void *)end,-1 ,"initrd" ); +free_reserved_area ((void *)start, (void *)end,POISON_FREE_INITMEM , +"initrd" ); }#ifdef CONFIG_KEXEC_CORE
Signature · 1 line
2.20.1
Mike Rapoport
Acked-byQuoted 5 lines · Christoph Hellwig
Hi all,
I've spent some time chasing down behavior in initramfs and found plenty of opportunity to improve the code. A first stab on that is contained in this series.
For the series:
Signature · 2 lines
Sincerely yours, Mike.