Re: [PATCH] dcache: use kmalloc_flex() in __d_alloc
From: Jori Koolstra
Date: Fri Apr 17 2026 - 08:05:44 EST
On Fri, Apr 17, 2026 at 11:42:40AM +0200, Thorsten Blum wrote:
> Use kmalloc_flex() when allocating a new 'struct external_name' in
> __d_alloc() to replace offsetof() and the open-coded size arithmetic,
> and to keep the size type-safe.
>
> Signed-off-by: Thorsten Blum <thorsten.blum@xxxxxxxxx>
> ---
> fs/dcache.c | 8 ++++----
> 1 file changed, 4 insertions(+), 4 deletions(-)
>
> diff --git a/fs/dcache.c b/fs/dcache.c
> index dbcbd0affb26..4568f9530166 100644
> --- a/fs/dcache.c
> +++ b/fs/dcache.c
> @@ -1753,10 +1753,10 @@ static struct dentry *__d_alloc(struct super_block *sb, const struct qstr *name)
> name = &slash_name;
> dname = dentry->d_shortname.string;
> } else if (name->len > DNAME_INLINE_LEN-1) {
> - size_t size = offsetof(struct external_name, name[1]);
> - struct external_name *p = kmalloc(size + name->len,
> - GFP_KERNEL_ACCOUNT |
> - __GFP_RECLAIMABLE);
> + struct external_name *p;
> +
> + p = kmalloc_flex(*p, name, name->len + 1,
> + GFP_KERNEL_ACCOUNT | __GFP_RECLAIMABLE);
> if (!p) {
> kmem_cache_free(dentry_cache, dentry);
> return NULL;
Looks reasonable to me. Is the idea to completely outphase this old way
of requesting memory for structs with flexible array members?
Feel free to add:
Reviewed-by: Jori Koolstra <jkoolstra@xxxxxxxxx>