Skip to content

Commit 7be37c9

Browse files
ddissbrauner
authored andcommitted
initramfs: allocate heap buffers together
header_buf, symlink_buf and name_buf all share the same lifecycle so needn't be allocated / freed separately. This change leads to a minor reduction in .text size: before: text data bss dec hex filename 7914 1110 8 9032 2348 init/initramfs.o after: text data bss dec hex filename 7854 1110 8 8972 230c init/initramfs.o A previous iteration of this patch reused a single buffer instead of three, given that buffer use is state-sequential (GotHeader, GotName, GotSymlink). However, the slight decrease in heap use during early boot isn't really worth the extra review complexity. Link: https://lore.kernel.org/all/20241107002044.16477-7-ddiss@suse.de/ Signed-off-by: David Disseldorp <ddiss@suse.de> Link: https://lore.kernel.org/r/20250304061020.9815-6-ddiss@suse.de Signed-off-by: Christian Brauner <brauner@kernel.org>
1 parent a8a3bc2 commit 7be37c9

1 file changed

Lines changed: 11 additions & 8 deletions

File tree

init/initramfs.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -510,14 +510,19 @@ char * __init unpack_to_rootfs(char *buf, unsigned long len)
510510
decompress_fn decompress;
511511
const char *compress_name;
512512
static __initdata char msg_buf[64];
513+
struct {
514+
char header[CPIO_HDRLEN];
515+
char symlink[PATH_MAX + N_ALIGN(PATH_MAX) + 1];
516+
char name[N_ALIGN(PATH_MAX)];
517+
} *bufs = kmalloc(sizeof(*bufs), GFP_KERNEL);
513518

514-
header_buf = kmalloc(CPIO_HDRLEN, GFP_KERNEL);
515-
symlink_buf = kmalloc(PATH_MAX + N_ALIGN(PATH_MAX) + 1, GFP_KERNEL);
516-
name_buf = kmalloc(N_ALIGN(PATH_MAX), GFP_KERNEL);
517-
518-
if (!header_buf || !symlink_buf || !name_buf)
519+
if (!bufs)
519520
panic_show_mem("can't allocate buffers");
520521

522+
header_buf = bufs->header;
523+
symlink_buf = bufs->symlink;
524+
name_buf = bufs->name;
525+
521526
state = Start;
522527
this_header = 0;
523528
message = NULL;
@@ -560,9 +565,7 @@ char * __init unpack_to_rootfs(char *buf, unsigned long len)
560565
len -= my_inptr;
561566
}
562567
dir_utime();
563-
kfree(name_buf);
564-
kfree(symlink_buf);
565-
kfree(header_buf);
568+
kfree(bufs);
566569
return message;
567570
}
568571

0 commit comments

Comments
 (0)