From: Thorsten Blum <blum@kernel.org>
To: Alexander Viro <viro@zeniv.linux.org.uk>,
Christian Brauner <brauner@kernel.org>, Jan Kara <jack@suse.cz>
Cc: Thorsten Blum <blum@kernel.org>,
linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH] initramfs: Reduce hardlink hash allocation sizes
Date: Sat, 19 Sep 2026 23:39:00 +0200 [thread overview]
Message-ID: <20260919213859.24667-3-blum@kernel.org> (raw)
Each hardlink hash entry reserves N_ALIGN(PATH_MAX) bytes for its name.
This makes every allocation larger than 4 KiB, placing it in the
kmalloc-8k bucket even for short names.
Use a flexible array with the already validated cpio name_len to reduce
allocation sizes.
Also use const for the read-only name parameter.
Signed-off-by: Thorsten Blum <blum@kernel.org>
---
Tested with initramfs KUnit suite: all 9 tests pass.
---
init/initramfs.c | 11 ++++++-----
1 file changed, 6 insertions(+), 5 deletions(-)
diff --git a/init/initramfs.c b/init/initramfs.c
index 3cee8b50ad82..ebddde9c8f0d 100644
--- a/init/initramfs.c
+++ b/init/initramfs.c
@@ -80,7 +80,7 @@ static __initdata struct hash {
int ino, minor, major;
umode_t mode;
struct hash *next;
- char name[N_ALIGN(PATH_MAX)];
+ char name[];
} *head[32];
static __initdata bool hardlink_seen;
@@ -92,7 +92,7 @@ static inline int hash(int major, int minor, int ino)
}
static char __init *find_link(int major, int minor, int ino,
- umode_t mode, char *name)
+ umode_t mode, const char *name, size_t nlen)
{
struct hash **p, *q;
for (p = head + hash(major, minor, ino); *p; p = &(*p)->next) {
@@ -106,14 +106,15 @@ static char __init *find_link(int major, int minor, int ino,
continue;
return (*p)->name;
}
- q = kmalloc_obj(struct hash);
+
+ q = kmalloc_flex(struct hash, name, nlen);
if (!q)
panic_show_mem("can't allocate link hash entry");
q->major = major;
q->minor = minor;
q->ino = ino;
q->mode = mode;
- strscpy(q->name, name);
+ strscpy(q->name, name, nlen);
q->next = NULL;
*p = q;
hardlink_seen = true;
@@ -355,7 +356,7 @@ static void __init clean_path(char *path, umode_t fmode)
static int __init maybe_link(void)
{
if (nlink >= 2) {
- char *old = find_link(major, minor, ino, mode, collected);
+ char *old = find_link(major, minor, ino, mode, collected, name_len);
if (old) {
clean_path(collected, 0);
return (init_link(old, collected) < 0) ? -1 : 1;
reply other threads:[~2026-09-19 21:39 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260919213859.24667-3-blum@kernel.org \
--to=blum@kernel.org \
--cc=brauner@kernel.org \
--cc=jack@suse.cz \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=viro@zeniv.linux.org.uk \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®