[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CAAq0SUmMdTb5C-SCSD5WPPyj5B1iB6dD5QWuDTsXPnJktFr36g@mail.gmail.com>
Date: Fri, 19 Apr 2024 09:30:06 -0300
From: Wander Lairson Costa <wander@...hat.com>
To: David Gow <davidgow@...gle.com>
Cc: Brendan Higgins <brendan.higgins@...ux.dev>, Rae Moar <rmoar@...gle.com>,
Greg Kroah-Hartman <gregkh@...uxfoundation.org>, Shuah Khan <skhan@...uxfoundation.org>,
Maxime Ripard <mripard@...nel.org>, Matti Vaittinen <mazziesaccount@...il.com>,
"open list:KERNEL UNIT TESTING FRAMEWORK (KUnit)" <linux-kselftest@...r.kernel.org>,
"open list:KERNEL UNIT TESTING FRAMEWORK (KUnit)" <kunit-dev@...glegroups.com>, open list <linux-kernel@...r.kernel.org>
Subject: Re: [PATCH v3 2/2] kunit: avoid memory leak on device register error
On Fri, Apr 19, 2024 at 1:59 AM David Gow <davidgow@...gle.com> wrote:
>
> On Fri, 19 Apr 2024 at 05:02, Wander Lairson Costa <wander@...hat.com> wrote:
> >
> > If the device register fails, free the allocated memory before
> > returning.
> >
> > Signed-off-by: Wander Lairson Costa <wander@...hat.com>
> > Fixes: d03c720e03bd ("kunit: Add APIs for managing devices")
> > ---
>
> Thanks.
>
> I'm not sure this is correct, though... Shouldn't put_device() free this for us?
>
> The documentation for device_register() says to never free a device
> after device_register() has been called, even if it fails:
> https://docs.kernel.org/driver-api/infrastructure.html#c.device_register
>
> Or am I missing something?
>
I am not freeing the device object passed to device_register, but its
parent structure.
As a side note, the behavior of device_register() seems
counterintuitive and error-prone, IMO. If the function returns an
error, it should ensure it leaks no resource and shouldn't require the
caller to do any cleanup.
> Cheers,
> -- David
>
>
> > lib/kunit/device.c | 11 ++++++-----
> > 1 file changed, 6 insertions(+), 5 deletions(-)
> >
> > diff --git a/lib/kunit/device.c b/lib/kunit/device.c
> > index 25c81ed465fb..bc2e2032e505 100644
> > --- a/lib/kunit/device.c
> > +++ b/lib/kunit/device.c
> > @@ -119,10 +119,8 @@ static struct kunit_device *kunit_device_register_internal(struct kunit *test,
> > kunit_dev->owner = test;
> >
> > err = dev_set_name(&kunit_dev->dev, "%s.%s", test->name, name);
> > - if (err) {
> > - kfree(kunit_dev);
> > - return ERR_PTR(err);
> > - }
> > + if (err)
> > + goto error;
> >
> > kunit_dev->dev.release = kunit_device_release;
> > kunit_dev->dev.bus = &kunit_bus_type;
> > @@ -131,7 +129,7 @@ static struct kunit_device *kunit_device_register_internal(struct kunit *test,
> > err = device_register(&kunit_dev->dev);
> > if (err) {
> > put_device(&kunit_dev->dev);
> > - return ERR_PTR(err);
> > + goto error;
> > }
> >
> > kunit_dev->dev.dma_mask = &kunit_dev->dev.coherent_dma_mask;
> > @@ -140,6 +138,9 @@ static struct kunit_device *kunit_device_register_internal(struct kunit *test,
> > kunit_add_action(test, device_unregister_wrapper, &kunit_dev->dev);
> >
> > return kunit_dev;
> > +error:
> > + kfree(kunit_dev);
> > + return ERR_PTR(err);
> > }
> >
> > /*
> > --
> > 2.44.0
> >
Powered by blists - more mailing lists