2020-05-14 13:22:35

by Alexandru Ardelean

[permalink] [raw]
Subject: [PATCH v2 7/8] iio: core: simplify alloc alignment code

There was a recent discussion about this code:
https://lore.kernel.org/linux-iio/20200322165317.0b1f0674@archlinux/

This looks like a good time to rework this, since any issues about it
should pop-up under testing, because the iio_dev is having a bit of an
overhaul and stuff being moved to iio_dev_priv.

Signed-off-by: Alexandru Ardelean <[email protected]>
---
drivers/iio/industrialio-core.c | 10 +++-------
1 file changed, 3 insertions(+), 7 deletions(-)

diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
index a1b29e0f8fd6..7671d36efae7 100644
--- a/drivers/iio/industrialio-core.c
+++ b/drivers/iio/industrialio-core.c
@@ -1514,13 +1514,9 @@ struct iio_dev *iio_device_alloc(int sizeof_priv)
struct iio_dev *dev;
size_t alloc_size;

- alloc_size = sizeof(struct iio_dev_opaque);
- if (sizeof_priv) {
- alloc_size = ALIGN(alloc_size, IIO_ALIGN);
- alloc_size += sizeof_priv;
- }
- /* ensure 32-byte alignment of whole construct ? */
- alloc_size += IIO_ALIGN - 1;
+ alloc_size = ALIGN(sizeof(struct iio_dev_opaque), IIO_ALIGN);
+ if (sizeof_priv)
+ alloc_size += ALIGN(sizeof_priv, IIO_ALIGN);

iio_dev_opaque = kzalloc(alloc_size, GFP_KERNEL);
if (!iio_dev_opaque)
--
2.17.1


2020-05-15 07:14:56

by Nuno Sa

[permalink] [raw]
Subject: RE: [PATCH v2 7/8] iio: core: simplify alloc alignment code

Hey Alex,

Just a small question...

> From: [email protected] <[email protected]>
> On Behalf Of Alexandru Ardelean
> Sent: Donnerstag, 14. Mai 2020 15:17
> To: [email protected]; [email protected]; linux-
> [email protected]; [email protected]
> Cc: [email protected]; [email protected];
> [email protected]; [email protected];
> [email protected]; [email protected];
> [email protected]; [email protected]; Ardelean, Alexandru
> <[email protected]>
> Subject: [PATCH v2 7/8] iio: core: simplify alloc alignment code
>
> There was a recent discussion about this code:
> https://urldefense.com/v3/__https://lore.kernel.org/linux-
> iio/20200322165317.0b1f0674@archlinux/__;!!A3Ni8CS0y2Y!pgdUSayJCfxMiE
> w8Fpv0LkEZurCSkX0sEcLnXeDSCLmhpu1xont6-vBQj3ZbCw$
>
> This looks like a good time to rework this, since any issues about it
> should pop-up under testing, because the iio_dev is having a bit of an
> overhaul and stuff being moved to iio_dev_priv.
>
> Signed-off-by: Alexandru Ardelean <[email protected]>
> ---
> drivers/iio/industrialio-core.c | 10 +++-------
> 1 file changed, 3 insertions(+), 7 deletions(-)
>
> diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
> index a1b29e0f8fd6..7671d36efae7 100644
> --- a/drivers/iio/industrialio-core.c
> +++ b/drivers/iio/industrialio-core.c
> @@ -1514,13 +1514,9 @@ struct iio_dev *iio_device_alloc(int sizeof_priv)
> struct iio_dev *dev;
> size_t alloc_size;
>
> - alloc_size = sizeof(struct iio_dev_opaque);
> - if (sizeof_priv) {
> - alloc_size = ALIGN(alloc_size, IIO_ALIGN);
> - alloc_size += sizeof_priv;
> - }
> - /* ensure 32-byte alignment of whole construct ? */
> - alloc_size += IIO_ALIGN - 1;
> + alloc_size = ALIGN(sizeof(struct iio_dev_opaque), IIO_ALIGN);
> + if (sizeof_priv)
> + alloc_size += ALIGN(sizeof_priv, IIO_ALIGN);

Do we actually need to do the `ALIGN` again? It seems to me that `alloc_size += sizeof_priv`
would be enough or am I missing something obvious?

- Nuno S?

2020-05-15 11:50:21

by Alexandru Ardelean

[permalink] [raw]
Subject: Re: [PATCH v2 7/8] iio: core: simplify alloc alignment code

On Fri, 2020-05-15 at 07:12 +0000, Sa, Nuno wrote:
> Hey Alex,
>
> Just a small question...
>
> > From: [email protected] <[email protected]>
> > On Behalf Of Alexandru Ardelean
> > Sent: Donnerstag, 14. Mai 2020 15:17
> > To: [email protected]; [email protected]; linux-
> > [email protected]; [email protected]
> > Cc: [email protected]; [email protected];
> > [email protected]; [email protected];
> > [email protected]; [email protected];
> > [email protected]; [email protected]; Ardelean, Alexandru
> > <[email protected]>
> > Subject: [PATCH v2 7/8] iio: core: simplify alloc alignment code
> >
> > There was a recent discussion about this code:
> > https://urldefense.com/v3/__https://lore.kernel.org/linux-
> > iio/20200322165317.0b1f0674@archlinux/__;!!A3Ni8CS0y2Y!pgdUSayJCfxMiE
> > w8Fpv0LkEZurCSkX0sEcLnXeDSCLmhpu1xont6-vBQj3ZbCw$
> >
> > This looks like a good time to rework this, since any issues about it
> > should pop-up under testing, because the iio_dev is having a bit of an
> > overhaul and stuff being moved to iio_dev_priv.
> >
> > Signed-off-by: Alexandru Ardelean <[email protected]>
> > ---
> > drivers/iio/industrialio-core.c | 10 +++-------
> > 1 file changed, 3 insertions(+), 7 deletions(-)
> >
> > diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-
> > core.c
> > index a1b29e0f8fd6..7671d36efae7 100644
> > --- a/drivers/iio/industrialio-core.c
> > +++ b/drivers/iio/industrialio-core.c
> > @@ -1514,13 +1514,9 @@ struct iio_dev *iio_device_alloc(int sizeof_priv)
> > struct iio_dev *dev;
> > size_t alloc_size;
> >
> > - alloc_size = sizeof(struct iio_dev_opaque);
> > - if (sizeof_priv) {
> > - alloc_size = ALIGN(alloc_size, IIO_ALIGN);
> > - alloc_size += sizeof_priv;
> > - }
> > - /* ensure 32-byte alignment of whole construct ? */
> > - alloc_size += IIO_ALIGN - 1;
> > + alloc_size = ALIGN(sizeof(struct iio_dev_opaque), IIO_ALIGN);
> > + if (sizeof_priv)
> > + alloc_size += ALIGN(sizeof_priv, IIO_ALIGN);
>
> Do we actually need to do the `ALIGN` again? It seems to me that `alloc_size
> += sizeof_priv`
> would be enough or am I missing something obvious?

Well, it's not always clear what value 'sizeof_priv' has, and whether it is
provided already aligned.
The requirement is usually that this data be cacheline aligned.

So, sizeof(struct iio_dev_opaque) is aligned already a few lines above, but the
private information should also be aligned [given that it's an unknown value
provided by the driver].
I think this is mostly important, if we need to do DMA access to buffers
allocated on the driver's state-struct, which is allocated here, and which is
usually provided as sizeof_priv.

Tbh, the discussions around this alignment/cacheline-alignment are a bit fuzzy
to me. I haven't run into any of these complicated issues.

>
> - Nuno Sá
>

2020-05-15 12:39:32

by Nuno Sa

[permalink] [raw]
Subject: RE: [PATCH v2 7/8] iio: core: simplify alloc alignment code

> From: Ardelean, Alexandru <[email protected]>
> Sent: Freitag, 15. Mai 2020 13:48
> To: [email protected]; linux-stm32@st-md-
> mailman.stormreply.com; Sa, Nuno <[email protected]>; linux-
> [email protected]; [email protected]
> Cc: [email protected]; [email protected];
> [email protected]; [email protected]; [email protected];
> [email protected]; [email protected];
> [email protected]
> Subject: Re: [PATCH v2 7/8] iio: core: simplify alloc alignment code
>
> On Fri, 2020-05-15 at 07:12 +0000, Sa, Nuno wrote:
> > Hey Alex,
> >
> > Just a small question...
> >
> > > From: [email protected] <linux-iio-
> [email protected]>
> > > On Behalf Of Alexandru Ardelean
> > > Sent: Donnerstag, 14. Mai 2020 15:17
> > > To: [email protected]; [email protected];
> linux-
> > > [email protected]; [email protected]
> > > Cc: [email protected]; [email protected];
> > > [email protected]; [email protected];
> > > [email protected]; [email protected];
> > > [email protected]; [email protected]; Ardelean, Alexandru
> > > <[email protected]>
> > > Subject: [PATCH v2 7/8] iio: core: simplify alloc alignment code
> > >
> > > There was a recent discussion about this code:
> > > https://urldefense.com/v3/__https://lore.kernel.org/linux-
> > >
> iio/20200322165317.0b1f0674@archlinux/__;!!A3Ni8CS0y2Y!pgdUSayJCfxMiE
> > > w8Fpv0LkEZurCSkX0sEcLnXeDSCLmhpu1xont6-vBQj3ZbCw$
> > >
> > > This looks like a good time to rework this, since any issues about it
> > > should pop-up under testing, because the iio_dev is having a bit of an
> > > overhaul and stuff being moved to iio_dev_priv.
> > >
> > > Signed-off-by: Alexandru Ardelean <[email protected]>
> > > ---
> > > drivers/iio/industrialio-core.c | 10 +++-------
> > > 1 file changed, 3 insertions(+), 7 deletions(-)
> > >
> > > diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-
> > > core.c
> > > index a1b29e0f8fd6..7671d36efae7 100644
> > > --- a/drivers/iio/industrialio-core.c
> > > +++ b/drivers/iio/industrialio-core.c
> > > @@ -1514,13 +1514,9 @@ struct iio_dev *iio_device_alloc(int
> sizeof_priv)
> > > struct iio_dev *dev;
> > > size_t alloc_size;
> > >
> > > - alloc_size = sizeof(struct iio_dev_opaque);
> > > - if (sizeof_priv) {
> > > - alloc_size = ALIGN(alloc_size, IIO_ALIGN);
> > > - alloc_size += sizeof_priv;
> > > - }
> > > - /* ensure 32-byte alignment of whole construct ? */
> > > - alloc_size += IIO_ALIGN - 1;
> > > + alloc_size = ALIGN(sizeof(struct iio_dev_opaque), IIO_ALIGN);
> > > + if (sizeof_priv)
> > > + alloc_size += ALIGN(sizeof_priv, IIO_ALIGN);
> >
> > Do we actually need to do the `ALIGN` again? It seems to me that
> `alloc_size
> > += sizeof_priv`
> > would be enough or am I missing something obvious?
>
> Well, it's not always clear what value 'sizeof_priv' has, and whether it is
> provided already aligned.
> The requirement is usually that this data be cacheline aligned.
>
> So, sizeof(struct iio_dev_opaque) is aligned already a few lines above, but
> the
> private information should also be aligned [given that it's an unknown value
> provided by the driver].
> I think this is mostly important, if we need to do DMA access to buffers
> allocated on the driver's state-struct, which is allocated here, and which is
> usually provided as sizeof_priv.

Yes, AFAIU this is to guarantee that the priv struct will start at an address that is
DMA safe (cacheline-aligned). Hence, if there is any data in 'priv' that needs to be DMA
safe, we are fine...

Well, I was also misreading the code. Still, I think it should look something like:

````
alloc_size = sizeof(struct iio_dev_opaque)
if (sizeof_priv)
alloc_size += ALIGN(alloc_size, IIO_ALIGN);
````

If there is no priv, I think we don't need the padding bytes...

- Nuno Sá

2020-05-16 17:47:36

by Jonathan Cameron

[permalink] [raw]
Subject: Re: [PATCH v2 7/8] iio: core: simplify alloc alignment code

On Fri, 15 May 2020 12:37:28 +0000
"Sa, Nuno" <[email protected]> wrote:

> > From: Ardelean, Alexandru <[email protected]>
> > Sent: Freitag, 15. Mai 2020 13:48
> > To: [email protected]; linux-stm32@st-md-
> > mailman.stormreply.com; Sa, Nuno <[email protected]>; linux-
> > [email protected]; [email protected]
> > Cc: [email protected]; [email protected];
> > [email protected]; [email protected]; [email protected];
> > [email protected]; [email protected];
> > [email protected]
> > Subject: Re: [PATCH v2 7/8] iio: core: simplify alloc alignment code
> >
> > On Fri, 2020-05-15 at 07:12 +0000, Sa, Nuno wrote:
> > > Hey Alex,
> > >
> > > Just a small question...
> > >
> > > > From: [email protected] <linux-iio-
> > [email protected]>
> > > > On Behalf Of Alexandru Ardelean
> > > > Sent: Donnerstag, 14. Mai 2020 15:17
> > > > To: [email protected]; [email protected];
> > linux-
> > > > [email protected]; [email protected]
> > > > Cc: [email protected]; [email protected];
> > > > [email protected]; [email protected];
> > > > [email protected]; [email protected];
> > > > [email protected]; [email protected]; Ardelean, Alexandru
> > > > <[email protected]>
> > > > Subject: [PATCH v2 7/8] iio: core: simplify alloc alignment code
> > > >
> > > > There was a recent discussion about this code:
> > > > https://urldefense.com/v3/__https://lore.kernel.org/linux-
> > > >
> > iio/20200322165317.0b1f0674@archlinux/__;!!A3Ni8CS0y2Y!pgdUSayJCfxMiE
> > > > w8Fpv0LkEZurCSkX0sEcLnXeDSCLmhpu1xont6-vBQj3ZbCw$
> > > >
> > > > This looks like a good time to rework this, since any issues about it
> > > > should pop-up under testing, because the iio_dev is having a bit of an
> > > > overhaul and stuff being moved to iio_dev_priv.
> > > >
> > > > Signed-off-by: Alexandru Ardelean <[email protected]>
> > > > ---
> > > > drivers/iio/industrialio-core.c | 10 +++-------
> > > > 1 file changed, 3 insertions(+), 7 deletions(-)
> > > >
> > > > diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-
> > > > core.c
> > > > index a1b29e0f8fd6..7671d36efae7 100644
> > > > --- a/drivers/iio/industrialio-core.c
> > > > +++ b/drivers/iio/industrialio-core.c
> > > > @@ -1514,13 +1514,9 @@ struct iio_dev *iio_device_alloc(int
> > sizeof_priv)
> > > > struct iio_dev *dev;
> > > > size_t alloc_size;
> > > >
> > > > - alloc_size = sizeof(struct iio_dev_opaque);
> > > > - if (sizeof_priv) {
> > > > - alloc_size = ALIGN(alloc_size, IIO_ALIGN);
> > > > - alloc_size += sizeof_priv;
> > > > - }
> > > > - /* ensure 32-byte alignment of whole construct ? */
> > > > - alloc_size += IIO_ALIGN - 1;
> > > > + alloc_size = ALIGN(sizeof(struct iio_dev_opaque), IIO_ALIGN);
> > > > + if (sizeof_priv)
> > > > + alloc_size += ALIGN(sizeof_priv, IIO_ALIGN);
> > >
> > > Do we actually need to do the `ALIGN` again? It seems to me that
> > `alloc_size
> > > += sizeof_priv`
> > > would be enough or am I missing something obvious?
> >
> > Well, it's not always clear what value 'sizeof_priv' has, and whether it is
> > provided already aligned.
> > The requirement is usually that this data be cacheline aligned.
> >
> > So, sizeof(struct iio_dev_opaque) is aligned already a few lines above, but
> > the
> > private information should also be aligned [given that it's an unknown value
> > provided by the driver].
> > I think this is mostly important, if we need to do DMA access to buffers
> > allocated on the driver's state-struct, which is allocated here, and which is
> > usually provided as sizeof_priv.
>
> Yes, AFAIU this is to guarantee that the priv struct will start at an address that is
> DMA safe (cacheline-aligned). Hence, if there is any data in 'priv' that needs to be DMA
> safe, we are fine...
>
> Well, I was also misreading the code. Still, I think it should look something like:
>
> ````
> alloc_size = sizeof(struct iio_dev_opaque)
> if (sizeof_priv)
> alloc_size += ALIGN(alloc_size, IIO_ALIGN);
> ````
>
> If there is no priv, I think we don't need the padding bytes...
Agreed - no need to guarantee alignment of something that doesn't exist :)

>
> - Nuno Sá
>