Re: [PATCH v2 1/4] mm/mempolicy: Fix memory leaks in mempolicy_sysfs_init()
From: Gregory Price
Date: Fri Mar 14 2025 - 09:42:43 EST
On Fri, Mar 14, 2025 at 10:55:00AM +0000, Jonathan Cameron wrote:
> >
> > 1) allocate node_attrs and mempolicy_kobj up front and keep your
> > reordering, this lets us clean up allocations on failure before
> > kobject_init is called
> >
> > 2) after this remove all the other code and just let
> > mempolicy_kobj_release clean up node_attrs
> >
> > 3) Add a (%d) to the error message to differentiate failures
>
> Given how unlikely (and noisy) a memory allocation failure is,
> maybe just drop the printing at all in those paths - allowing
> early returns.
>
> The lifetime rules around node_attrs in here are making readability
> poor. It is implicitly owned by the mempolicy_kobj, but no direct association.
> Maybe just encapsulating the kobject in a structure that contains
> this as a [] array at the end. Then we end up with single allocation of
> stuff that is effectively one thing.
>
Even better recommendation, lets do as Jonathan suggests. <3
~Gregory