From: "Uwe Kleine-König" <u.kleine-koenig@pengutronix.de>
To: Yann Droneaud <ydroneaud@opteya.com>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
linux-kernel@vger.kernel.org, kernel@pengutronix.de,
Dmitry Torokhov <dmitry.torokhov@gmail.com>
Subject: Re: [PATCHv2] driver core/platform: don't leak memory allocated for dma_mask
Date: Tue, 14 Jan 2014 11:36:48 +0100 [thread overview]
Message-ID: <20140114103648.GT29475@pengutronix.de> (raw)
In-Reply-To: <1389693453.1585.26.camel@localhost.localdomain>
Hello Yann,
On Tue, Jan 14, 2014 at 10:57:33AM +0100, Yann Droneaud wrote:
> Le mardi 14 janvier 2014 à 09:19 +0100, Uwe Kleine-König a écrit :
> > On Tue, Jan 14, 2014 at 08:18:29AM +0100, Yann Droneaud wrote:
> > > Since commit 01dcc60a7cb8, platform_device_register_full() is
> > > available to allocate and register a platform device.
> > >
> > > If a dma_mask is provided as part of platform_device_info,
> > > platform_device_register_full() allocate memory for a u64
> > > using kmalloc().
> > >
> > > A comment in the code state that "[t]his memory isn't freed
> > > when the device is put".
> > >
>
> [...]
>
> > diff --git a/drivers/base/platform.c b/drivers/base/platform.c
> > > index 3a94b799f166..6e3e639fb886 100644
> > > --- a/drivers/base/platform.c
> > > +++ b/drivers/base/platform.c
> > > @@ -157,7 +157,7 @@ EXPORT_SYMBOL_GPL(platform_add_devices);
> > >
> > > struct platform_object {
> > > struct platform_device pdev;
> > > - char name[1];
> > > + char payload[0];
> > I don't know the recent minimal versions needed to compile the kernel
> > and since when gcc supports c99 flexible array members, but I would
> > expect that they just work. Having said that I'd prefer using that one,
> > i.e. use
> > char payload[];
> > > };
>
> I'm not confident with flexible array when using sizeof(), offsetof(),
> etc. I will try to use the c99 feature.
sizeof etc. will work. I'm not sure about c99 in general, but gcc gets
it right.
> > > +static struct platform_device *platform_device_dmamask_alloc(const char *name,
> > > + int id)
> > > +{
> > > + struct platform_object *pa;
> > > + const size_t padding = (((offsetof(struct platform_object, payload) +
> > > + (__alignof__(u64) - 1)) &
> > > + ~(__alignof__(u64) - 1)) -
> > > + offsetof(struct platform_object, payload));
> > > +
> > > + pa = platform_object_alloc(padding + sizeof(u64) + strlen(name) + 1);
> > > + if (pa) {
> > > + char *payload = pa->payload + padding;
> > > + /*
> > > + * Conceptually dma_mask in struct device should not be a pointer.
> > > + * See http://thread.gmane.org/gmane.linux.kernel.pci/9081
> > > + */
> > > + pa->pdev.dev.dma_mask = (void *)payload;
> > > + payload += sizeof(u64);
> > > + strcpy(payload, name);
> > > + platform_object_init(pa, payload, id);
> > > + }
> > > +
> > > + return pa ? &pa->pdev : NULL;
> > > +}
> > This looks all complicated. Did you think about spending the extra
> > memory and add a dma_mask to platform_object? That should simplify the
> > code quite a bit which probably is worth the extra memory being used.
> >
>
> You could have did it in the first place. But you choose to allocate a
> chunk of memory for the u64. I believe there's a reason ;)
Yeah, I think the reason is that back then I was younger and didn't
value maintainability higher than saving a few bytes. :-)
> I will try to get some figure on the number of platform_device
> registered with a dmamask versus without a dmamask: adding the u64 to
> all platform_object might cost more memory than the extra code (1 branch
> and a function).
Also take into account
sizeof(struct platform_object) + strlen(average device)
Before and after the change. ISTR that the first summand is ~300 (on
ARM). With putting dma_mask unconditionally into platform_object this
goes up by 8, IMHO that is OK.
In return you save that alignment hassle and both your patch and the
resulting code become simpler.
YMMV, best regards
Uwe
--
Pengutronix e.K. | Uwe Kleine-König |
Industrial Linux Solutions | http://www.pengutronix.de/ |
next prev parent reply other threads:[~2014-01-14 10:36 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-12-12 22:10 [PATCH] " Yann Droneaud
2014-01-13 21:38 ` [PATCHv1] " Yann Droneaud
2014-01-13 22:56 ` Greg Kroah-Hartman
2014-01-14 7:18 ` [PATCHv2] " Yann Droneaud
2014-01-14 8:19 ` Uwe Kleine-König
2014-01-14 9:57 ` Yann Droneaud
2014-01-14 10:36 ` Uwe Kleine-König [this message]
2014-01-14 18:02 ` Greg Kroah-Hartman
2014-01-26 21:18 ` [PATCHv3] " Yann Droneaud
2014-01-27 10:05 ` [PATCHv4] " Yann Droneaud
2014-02-07 23:20 ` Greg Kroah-Hartman
2014-02-08 15:09 ` Uwe Kleine-König
2014-02-09 7:47 ` Yann Droneaud
2014-02-09 9:30 ` Uwe Kleine-König
2014-02-15 19:39 ` Greg Kroah-Hartman
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20140114103648.GT29475@pengutronix.de \
--to=u.kleine-koenig@pengutronix.de \
--cc=dmitry.torokhov@gmail.com \
--cc=gregkh@linuxfoundation.org \
--cc=kernel@pengutronix.de \
--cc=linux-kernel@vger.kernel.org \
--cc=ydroneaud@opteya.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Powered by JetHome