Skip to content

Commit

Permalink
initramfs: free initrd memory if opening /initrd.image fails
Browse files Browse the repository at this point in the history
Patch series "initramfs tidyups".

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.

This patch (of 7):

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.

Steven said:

: 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.

Link: http://lkml.kernel.org/r/[email protected]
Signed-off-by: Christoph Hellwig <[email protected]>
Reviewed-by: Steven Price <[email protected]>
Acked-by: Mike Rapoport <[email protected]>
Cc: Catalin Marinas <[email protected]>	[arm64]
Cc: Geert Uytterhoeven <[email protected]>	[m68k]
Cc: Alexander Viro <[email protected]>
Cc: Russell King <[email protected]>
Cc: Will Deacon <[email protected]>
Cc: Guan Xuetao <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
Christoph Hellwig authored and torvalds committed May 14, 2019
1 parent 1df3a33 commit 54c7a89
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions init/initramfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -612,13 +612,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",
Expand All @@ -632,7 +631,6 @@ static int __init populate_rootfs(void)
written, initrd_end - initrd_start);

ksys_close(fd);
free_initrd();
}
done:
/* empty statement */;
Expand All @@ -642,9 +640,9 @@ static int __init populate_rootfs(void)
initrd_end - initrd_start);
if (err)
printk(KERN_EMERG "Initramfs unpacking failed: %s\n", err);
free_initrd();
#endif
}
free_initrd();
flush_delayed_fput();
return 0;
}
Expand Down

0 comments on commit 54c7a89

Please sign in to comment.