Re: [PATCH v3] kernfs: fix memory leak of kernfs_iattrs in __kernfs_new_node

From: Will Rosenberg
Date: Tue Nov 25 2025 - 10:17:20 EST


On Mon, Nov 24, 2025 at 03:41:51PM -0500, Paul Moore wrote:
>
> Should this be kmem_cache_free(kernfs_iattrs_cache, kn->iattr)
> instead? I suspect it would also probably be a good idea to call
> simple_xattrs_free() first.
>
> I also noticed there is a reasonable amount of duplicated kernfs
> cleanup code spread around fs/kernfs/dir.c, we could probably benefit
> from some helper functions here, but that would be a follow-on patch;
> keeping a small bugfix patch to ease stable tree backporting is a good
> thing.

I agree. Helper functions could also help prevent this type of bug from
re-emerging.

> [HINT FOR THE FUTURE: for patches like this, look for a function that
> normally does resource cleanup for the object and do what it does, in
> this case kernfs_free_rcu() is a good example.]

Thank you for all the help. I updated kfree to kmem_cache_free() and
added simple_xattrs_free() just to be safe for future changes.

--
will rosenberg