Skip to content

Commit 23091e2

Browse files
Christoph Hellwigtorvalds
authored andcommitted
initramfs: cleanup initrd freeing
Factor the kexec logic into a separate helper, and then inline the rest of free_initrd into the only caller. Link: http://lkml.kernel.org/r/20190213174621.29297-4-hch@lst.de Signed-off-by: Christoph Hellwig <hch@lst.de> Acked-by: Mike Rapoport <rppt@linux.ibm.com> Cc: Catalin Marinas <catalin.marinas@arm.com> [arm64] Cc: Geert Uytterhoeven <geert@linux-m68k.org> [m68k] Cc: Steven Price <steven.price@arm.com> Cc: Alexander Viro <viro@zeniv.linux.org.uk> Cc: Guan Xuetao <gxt@pku.edu.cn> Cc: Russell King <linux@armlinux.org.uk> Cc: Will Deacon <will.deacon@arm.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 54c7a89 commit 23091e2

1 file changed

Lines changed: 30 additions & 23 deletions

File tree

‎init/initramfs.c‎

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -518,37 +518,35 @@ extern unsigned long __initramfs_size;
518518
#include <linux/initrd.h>
519519
#include <linux/kexec.h>
520520

521-
static void __init free_initrd(void)
522-
{
523521
#ifdef CONFIG_KEXEC_CORE
522+
static bool kexec_free_initrd(void)
523+
{
524524
unsigned long crashk_start = (unsigned long)__va(crashk_res.start);
525525
unsigned long crashk_end = (unsigned long)__va(crashk_res.end);
526-
#endif
527-
if (do_retain_initrd)
528-
goto skip;
529526

530-
#ifdef CONFIG_KEXEC_CORE
531527
/*
532528
* If the initrd region is overlapped with crashkernel reserved region,
533529
* free only memory that is not part of crashkernel region.
534530
*/
535-
if (initrd_start < crashk_end && initrd_end > crashk_start) {
536-
/*
537-
* Initialize initrd memory region since the kexec boot does
538-
* not do.
539-
*/
540-
memset((void *)initrd_start, 0, initrd_end - initrd_start);
541-
if (initrd_start < crashk_start)
542-
free_initrd_mem(initrd_start, crashk_start);
543-
if (initrd_end > crashk_end)
544-
free_initrd_mem(crashk_end, initrd_end);
545-
} else
546-
#endif
547-
free_initrd_mem(initrd_start, initrd_end);
548-
skip:
549-
initrd_start = 0;
550-
initrd_end = 0;
531+
if (initrd_start >= crashk_end || initrd_end <= crashk_start)
532+
return false;
533+
534+
/*
535+
* Initialize initrd memory region since the kexec boot does not do.
536+
*/
537+
memset((void *)initrd_start, 0, initrd_end - initrd_start);
538+
if (initrd_start < crashk_start)
539+
free_initrd_mem(initrd_start, crashk_start);
540+
if (initrd_end > crashk_end)
541+
free_initrd_mem(crashk_end, initrd_end);
542+
return true;
551543
}
544+
#else
545+
static inline bool kexec_free_initrd(void)
546+
{
547+
return false;
548+
}
549+
#endif /* CONFIG_KEXEC_CORE */
552550

553551
#ifdef CONFIG_BLK_DEV_RAM
554552
#define BUF_SIZE 1024
@@ -642,7 +640,16 @@ static int __init populate_rootfs(void)
642640
printk(KERN_EMERG "Initramfs unpacking failed: %s\n", err);
643641
#endif
644642
}
645-
free_initrd();
643+
644+
/*
645+
* If the initrd region is overlapped with crashkernel reserved region,
646+
* free only memory that is not part of crashkernel region.
647+
*/
648+
if (!do_retain_initrd && !kexec_free_initrd())
649+
free_initrd_mem(initrd_start, initrd_end);
650+
initrd_start = 0;
651+
initrd_end = 0;
652+
646653
flush_delayed_fput();
647654
return 0;
648655
}

0 commit comments

Comments
 (0)