lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date:   Fri, 20 Dec 2019 11:22:18 +0100
From:   Greg Kroah-Hartman <gregkh@...uxfoundation.org>
To:     Marc Gonzalez <marc.w.gonzalez@...e.fr>
Cc:     Rafael Wysocki <rjw@...ysocki.net>,
        LKML <linux-kernel@...r.kernel.org>,
        Linux ARM <linux-arm-kernel@...ts.infradead.org>,
        Robin Murphy <robin.murphy@....com>,
        Dmitry Torokhov <dmitry.torokhov@...il.com>,
        Alexey Brodkin <alexey.brodkin@...opsys.com>,
        Peter Zijlstra <peterz@...radead.org>,
        Will Deacon <will@...nel.org>,
        Russell King <rmk+kernel@...linux.org.uk>,
        Bjorn Andersson <bjorn.andersson@...aro.org>,
        Tejun Heo <tj@...nel.org>, Mark Brown <broonie@...nel.org>
Subject: Re: [RFC PATCH v1] devres: align devres.data strictly only for
 devm_kmalloc()

On Fri, Dec 20, 2019 at 11:19:27AM +0100, Marc Gonzalez wrote:
> On 17/12/2019 16:30, Marc Gonzalez wrote:
> 
> > Commit a66d972465d15 ("devres: Align data[] to ARCH_KMALLOC_MINALIGN")
> > increased the alignment of devres.data unconditionally.
> > 
> > Some platforms have very strict alignment requirements for DMA-safe
> > addresses, e.g. 128 bytes on arm64. There, struct devres amounts to:
> > 	3 pointers + pad_to_128 + data + pad_to_256
> > i.e. ~220 bytes of padding.
> > 
> > Let's enforce the alignment only for devm_kmalloc().
> > 
> > Suggested-by: Robin Murphy <robin.murphy@....com>
> > Signed-off-by: Marc Gonzalez <marc.w.gonzalez@...e.fr>
> > ---
> > I had not been aware that dynamic allocation granularity on arm64 was
> > 128 bytes. This means there's a lot of waste on small allocations.
> > I suppose there's no easy solution, though.
> > ---
> >  drivers/base/devres.c | 23 +++++++++++++----------
> >  1 file changed, 13 insertions(+), 10 deletions(-)
> > 
> > diff --git a/drivers/base/devres.c b/drivers/base/devres.c
> > index 0bbb328bd17f..bf39188613d9 100644
> > --- a/drivers/base/devres.c
> > +++ b/drivers/base/devres.c
> > @@ -26,14 +26,7 @@ struct devres_node {
> >  
> >  struct devres {
> >  	struct devres_node		node;
> > -	/*
> > -	 * Some archs want to perform DMA into kmalloc caches
> > -	 * and need a guaranteed alignment larger than
> > -	 * the alignment of a 64-bit integer.
> > -	 * Thus we use ARCH_KMALLOC_MINALIGN here and get exactly the same
> > -	 * buffer alignment as if it was allocated by plain kmalloc().
> > -	 */
> > -	u8 __aligned(ARCH_KMALLOC_MINALIGN) data[];
> > +	u8				data[];
> >  };
> >  
> >  struct devres_group {
> > @@ -789,9 +782,16 @@ static void devm_kmalloc_release(struct device *dev, void *res)
> >  	/* noop */
> >  }
> >  
> > +#define DEVM_KMALLOC_PADDING_SIZE \
> > +	(ARCH_KMALLOC_MINALIGN - sizeof(struct devres) % ARCH_KMALLOC_MINALIGN)
> > +
> >  static int devm_kmalloc_match(struct device *dev, void *res, void *data)
> >  {
> > -	return res == data;
> > +	/*
> > +	 * 'res' is dr->data (not DMA-safe)
> > +	 * 'data' is the hand-aligned address from devm_kmalloc
> > +	 */
> > +	return res + DEVM_KMALLOC_PADDING_SIZE == data;
> >  }
> >  
> >  /**
> > @@ -811,6 +811,9 @@ void * devm_kmalloc(struct device *dev, size_t size, gfp_t gfp)
> >  {
> >  	struct devres *dr;
> >  
> > +	/* Add enough padding to provide a DMA-safe address */
> > +	size += DEVM_KMALLOC_PADDING_SIZE;
> > +
> >  	/* use raw alloc_dr for kmalloc caller tracing */
> >  	dr = alloc_dr(devm_kmalloc_release, size, gfp, dev_to_node(dev));
> >  	if (unlikely(!dr))
> > @@ -822,7 +825,7 @@ void * devm_kmalloc(struct device *dev, size_t size, gfp_t gfp)
> >  	 */
> >  	set_node_dbginfo(&dr->node, "devm_kzalloc_release", size);
> >  	devres_add(dev, dr->data);
> > -	return dr->data;
> > +	return dr->data + DEVM_KMALLOC_PADDING_SIZE;
> >  }
> >  EXPORT_SYMBOL_GPL(devm_kmalloc);
> 
> Would anyone else have any suggestions, comments, insights, recommendations,
> improvements, guidance, or wisdom? :-)
> 
> I keep thinking about the memory waste caused by the strict alignment requirement
> on arm64. Is there a way to inspect how much memory has been requested vs how much
> has been allocated? (Turning on SLAB DEBUG perhaps?)
> 
> Couldn't there be a kmalloc flag saying "this alloc will not require strict
> alignment, so just give me something 8-byte aligned" ?

Or you can not use the devm interface for lots of tiny allocations :)

thanks,

greg k-h

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ