From: Alexey Brodkin <Alexey.Brodkin@synopsys.com>
To: Marc Gonzalez <marc.w.gonzalez@free.fr>
Cc: Eugeniy Paltsev <Eugeniy.Paltsev@synopsys.com>,
Peter Zijlstra <peterz@infradead.org>,
Robin Murphy <robin.murphy@arm.com>,
Vineet Gupta <Vineet.Gupta1@synopsys.com>,
Dmitry Torokhov <dmitry.torokhov@gmail.com>,
Rafael Wysocki <rjw@rjwysocki.net>,
LKML <linux-kernel@vger.kernel.org>,
Bjorn Andersson <bjorn.andersson@linaro.org>,
Russell King <rmk+kernel@armlinux.org.uk>,
Mark Brown <broonie@kernel.org>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
Tejun Heo <tj@kernel.org>,
arcml <linux-snps-arc@lists.infradead.org>,
Will Deacon <will@kernel.org>,
Linux ARM <linux-arm-kernel@lists.infradead.org>
Subject: RE: [RFC PATCH v1] devres: align devres.data strictly only for devm_kmalloc()
Date: Wed, 18 Dec 2019 14:20:57 +0000 [thread overview]
Message-ID: <CY4PR1201MB012059FF6735C3EDB7E1F8E9A1530@CY4PR1201MB0120.namprd12.prod.outlook.com> (raw)
In-Reply-To: <74ae22cd-08c1-d846-3e1d-cbc38db87442@free.fr>
Hi Marc,
We sort of expected something like that to happen at some point.
Funny enough it's been a year since my change was accepted in v4.20
and only now somebody noticed :)
Though quite a few questions below.
> 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.
Could you please elaborate a bit on mentioned paddings?
I may understand the first one for 128 bytes but where does the
second one for 256 bytes come from?
> Let's enforce the alignment only for devm_kmalloc().
Ok so for devm_kmalloc() we don't change anything, right?
We still add the same padding before real data array.
> ---
> 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.
Now probably I'm missing something but when do you expect to save something?
If those smaller allocations are done with devm_kmalloc() you aren't
saving anything.
> I suppose there's no easy solution, though.
Right! It took a while till I was able to propose something
people [almost silently] agreed with.
> ---
> 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)
Even given your update with:
------------------------------->8--------------------------------
#define DEVM_KMALLOC_PADDING_SIZE \
((ARCH_KMALLOC_MINALIGN - sizeof(struct devres)) % ARCH_KMALLOC_MINALIGN)
------------------------------->8--------------------------------
I don't think I understand why do you need that "% ARCH_KMALLOC_MINALIGN" part?
> 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;
This implementation gets ugly and potentially will lead to problems later
when people will start changing code here. Compared to that initially aligned by
the compiler dr->data looks much more foolproof.
> /* 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;
Ditto. But first I'd like to understand what are you trying to really do
with your change and then we'll see if there could be any better implementation.
-Alexey
_______________________________________________
linux-snps-arc mailing list
linux-snps-arc@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-snps-arc
next parent reply other threads:[~2019-12-18 14:21 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <74ae22cd-08c1-d846-3e1d-cbc38db87442@free.fr>
2019-12-18 14:20 ` Alexey Brodkin [this message]
2019-12-18 15:40 ` [RFC PATCH v1] devres: align devres.data strictly only for devm_kmalloc() Marc Gonzalez
[not found] ` <bf020a68-00fd-2bb7-c3b6-00f5befa293a@free.fr>
[not found] ` <20191220140655.GN2827@hirez.programming.kicks-ass.net>
[not found] ` <9be1d523-e92c-836b-b79d-37e880d092a0@arm.com>
2019-12-20 19:32 ` Alexey Brodkin
2019-12-20 20:23 ` Peter Zijlstra
2019-12-20 21:02 ` Alexey Brodkin
2019-12-20 21:47 ` Dmitry Torokhov
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=CY4PR1201MB012059FF6735C3EDB7E1F8E9A1530@CY4PR1201MB0120.namprd12.prod.outlook.com \
--to=alexey.brodkin@synopsys.com \
--cc=Eugeniy.Paltsev@synopsys.com \
--cc=Vineet.Gupta1@synopsys.com \
--cc=bjorn.andersson@linaro.org \
--cc=broonie@kernel.org \
--cc=dmitry.torokhov@gmail.com \
--cc=gregkh@linuxfoundation.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-snps-arc@lists.infradead.org \
--cc=marc.w.gonzalez@free.fr \
--cc=peterz@infradead.org \
--cc=rjw@rjwysocki.net \
--cc=rmk+kernel@armlinux.org.uk \
--cc=robin.murphy@arm.com \
--cc=tj@kernel.org \
--cc=will@kernel.org \
/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;
as well as URLs for NNTP newsgroup(s).