Re: [PATCH v3 46/57] perf: Simplify pmu_dev_alloc()

From: Greg KH
Date: Mon Jun 12 2023 - 08:59:06 EST


On Mon, Jun 12, 2023 at 02:29:05PM +0200, Paolo Bonzini wrote:
> On 6/12/23 14:18, Greg KH wrote:
> > Yeah, it's a pain, but you are trying to hand-roll code that is not a
> > "normal" path for a struct device, sorry.
> >
> > I don't know if you really can encode all of that crazy logic in the
> > cleanup api, UNLESS you can "switch" the cleanup function at a point in
> > time (i.e. after device_add() is successful). Is that possible?
>
> What _could_ make sense is that device_add() completely takes ownership of
> the given pointer, and takes care of calling put_device() on failure.

I think we tried that decades ago :)

Problem is that the caller wants to clean stuff up associted with that
struct device-embedded structure _before_ the final put_device() is
called which would usually free the memory of the overall structure.

So device_add() can't call put_device() on the error path within it, you
need the caller to unwind it's local stuff before that happens.

> Then you can have
>
> struct device *dev_struct __free(put_device) =
> kzalloc(sizeof(struct device), GFP_KERNEL);
>
> struct device *dev __free(device_del) =
> device_add(no_free_ptr(dev_struct));
>
> /* dev_struct is NULL now */
>
> pmu->dev = no_free_ptr(dev);

I don't see how that works properly, what am I missing?

thanks,

greg k-h