Re: [PATCH] initramfs: Reduce hardlink hash allocation sizes
From: Thorsten Blum
Date: Tue Sep 22 2026 - 07:20:33 EST
On Tue, Sep 22, 2026 at 11:45:37AM +0200, Jan Kara wrote:
> On Sat 19-09-26 23:39:00, Thorsten Blum wrote:
> > 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@xxxxxxxxxx>
>
> ...
>
> > @@ -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;
>
> What about the space for terminating \0 ? This way the stored 'name' will
> be actually one character shorter because strscpy() will overwrite the last
> character by \0. Or do I miss something?
do_name() guarantees that collected[name_len - 1] == '\0' before calling
maybe_link(), so name_len already includes the NUL terminator and no
character is overwritten.