[PATCH] iio: iio-mux: use flexible array member

Rosen Penev posted 1 patch 3 weeks, 1 day ago
There is a newer version of this series
drivers/iio/multiplexer/iio-mux.c | 6 ++----
1 file changed, 2 insertions(+), 4 deletions(-)
[PATCH] iio: iio-mux: use flexible array member
Posted by Rosen Penev 3 weeks, 1 day ago
Allows using struct_size to simplify allocation slightly.

Removes a pointer from the struct.

Signed-off-by: Rosen Penev <rosenp@gmail.com>
---
 drivers/iio/multiplexer/iio-mux.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/drivers/iio/multiplexer/iio-mux.c b/drivers/iio/multiplexer/iio-mux.c
index b742ca9a99d1..8db061413829 100644
--- a/drivers/iio/multiplexer/iio-mux.c
+++ b/drivers/iio/multiplexer/iio-mux.c
@@ -33,8 +33,8 @@ struct mux {
 	struct iio_channel *parent;
 	struct iio_chan_spec *chan;
 	struct iio_chan_spec_ext_info *ext_info;
-	struct mux_child *child;
 	u32 delay_us;
+	struct mux_child child[];
 };
 
 static int iio_mux_select(struct mux *mux, int idx)
@@ -380,8 +380,7 @@ static int mux_probe(struct platform_device *pdev)
 		return -EINVAL;
 	}
 
-	sizeof_priv = sizeof(*mux);
-	sizeof_priv += sizeof(*mux->child) * children;
+	sizeof_priv = struct_size(mux, child, children);
 	sizeof_priv += sizeof(*mux->chan) * children;
 	sizeof_priv += sizeof_ext_info;
 
@@ -390,7 +389,6 @@ static int mux_probe(struct platform_device *pdev)
 		return -ENOMEM;
 
 	mux = iio_priv(indio_dev);
-	mux->child = (struct mux_child *)(mux + 1);
 	mux->chan = (struct iio_chan_spec *)(mux->child + children);
 
 	platform_set_drvdata(pdev, indio_dev);
-- 
2.53.0
Re: [PATCH] iio: iio-mux: use flexible array member
Posted by Andy Shevchenko 3 weeks ago
On Sun, Mar 15, 2026 at 03:45:09PM -0700, Rosen Penev wrote:
> Allows using struct_size to simplify allocation slightly.
> 
> Removes a pointer from the struct.

...

> -	sizeof_priv = sizeof(*mux);
> -	sizeof_priv += sizeof(*mux->child) * children;
> +	sizeof_priv = struct_size(mux, child, children);
>  	sizeof_priv += sizeof(*mux->chan) * children;

There are two arrays that are allocated at the end of the same object.
Why is mux better than the chan?


I rather would see two times array_size() than the above.

>  	sizeof_priv += sizeof_ext_info;

-- 
With Best Regards,
Andy Shevchenko
Re: [PATCH] iio: iio-mux: use flexible array member
Posted by Rosen Penev 3 weeks ago
On Mon, Mar 16, 2026 at 7:16 AM Andy Shevchenko
<andriy.shevchenko@intel.com> wrote:
>
> On Sun, Mar 15, 2026 at 03:45:09PM -0700, Rosen Penev wrote:
> > Allows using struct_size to simplify allocation slightly.
> >
> > Removes a pointer from the struct.
>
> ...
>
> > -     sizeof_priv = sizeof(*mux);
> > -     sizeof_priv += sizeof(*mux->child) * children;
> > +     sizeof_priv = struct_size(mux, child, children);
> >       sizeof_priv += sizeof(*mux->chan) * children;
>
> There are two arrays that are allocated at the end of the same object.
> Why is mux better than the chan?
It's how it's already laid out. mux first and then the other data.
>
>
> I rather would see two times array_size() than the above.
OK
>
> >       sizeof_priv += sizeof_ext_info;
>
> --
> With Best Regards,
> Andy Shevchenko
>
>
Re: [PATCH] iio: iio-mux: use flexible array member
Posted by Andy Shevchenko 3 weeks ago
On Mon, Mar 16, 2026 at 11:11:22AM -0700, Rosen Penev wrote:
> On Mon, Mar 16, 2026 at 7:16 AM Andy Shevchenko
> <andriy.shevchenko@intel.com> wrote:
> > On Sun, Mar 15, 2026 at 03:45:09PM -0700, Rosen Penev wrote:

...

> > > -     sizeof_priv = sizeof(*mux);
> > > -     sizeof_priv += sizeof(*mux->child) * children;
> > > +     sizeof_priv = struct_size(mux, child, children);
> > >       sizeof_priv += sizeof(*mux->chan) * children;
> >
> > There are two arrays that are allocated at the end of the same object.
> > Why is mux better than the chan?
> It's how it's already laid out. mux first and then the other data.

It doesn't matter, right?

> > I rather would see two times array_size() than the above.

> OK

> > >       sizeof_priv += sizeof_ext_info;

-- 
With Best Regards,
Andy Shevchenko


Re: [PATCH] iio: iio-mux: use flexible array member
Posted by Rosen Penev 3 weeks ago
On Mon, Mar 16, 2026 at 12:43 PM Andy Shevchenko
<andriy.shevchenko@intel.com> wrote:
>
> On Mon, Mar 16, 2026 at 11:11:22AM -0700, Rosen Penev wrote:
> > On Mon, Mar 16, 2026 at 7:16 AM Andy Shevchenko
> > <andriy.shevchenko@intel.com> wrote:
> > > On Sun, Mar 15, 2026 at 03:45:09PM -0700, Rosen Penev wrote:
>
> ...
>
> > > > -     sizeof_priv = sizeof(*mux);
> > > > -     sizeof_priv += sizeof(*mux->child) * children;
> > > > +     sizeof_priv = struct_size(mux, child, children);
> > > >       sizeof_priv += sizeof(*mux->chan) * children;
> > >
> > > There are two arrays that are allocated at the end of the same object.
> > > Why is mux better than the chan?
> > It's how it's already laid out. mux first and then the other data.
>
> It doesn't matter, right?
I can't really judge. This commit came from running

git grep \\\*\)\  | grep \ \+\ 1\)\;

which is the pre-C99 way of doing trailing allocations.

Shuffling this around would increase the patch size which I've been
advised against doing elsewhere.
>
> > > I rather would see two times array_size() than the above.
>
> > OK
>
> > > >       sizeof_priv += sizeof_ext_info;
>
> --
> With Best Regards,
> Andy Shevchenko
>
>
Re: [PATCH] iio: iio-mux: use flexible array member
Posted by Andy Shevchenko 3 weeks ago
On Mon, Mar 16, 2026 at 02:12:35PM -0700, Rosen Penev wrote:
> On Mon, Mar 16, 2026 at 12:43 PM Andy Shevchenko
> <andriy.shevchenko@intel.com> wrote:
> > On Mon, Mar 16, 2026 at 11:11:22AM -0700, Rosen Penev wrote:
> > > On Mon, Mar 16, 2026 at 7:16 AM Andy Shevchenko
> > > <andriy.shevchenko@intel.com> wrote:
> > > > On Sun, Mar 15, 2026 at 03:45:09PM -0700, Rosen Penev wrote:

...

> > > > > -     sizeof_priv = sizeof(*mux);
> > > > > -     sizeof_priv += sizeof(*mux->child) * children;
> > > > > +     sizeof_priv = struct_size(mux, child, children);
> > > > >       sizeof_priv += sizeof(*mux->chan) * children;
> > > >
> > > > There are two arrays that are allocated at the end of the same object.
> > > > Why is mux better than the chan?
> > > It's how it's already laid out. mux first and then the other data.
> >
> > It doesn't matter, right?
> I can't really judge. This commit came from running
> 
> git grep \\\*\)\  | grep \ \+\ 1\)\;
> 
> which is the pre-C99 way of doing trailing allocations.
> 
> Shuffling this around would increase the patch size which I've been
> advised against doing elsewhere.

My point was to show that from the memory layout the mux first chan next and
vice versa have no differences. Hence the proposed patch actually changes the
status quo. What I suggested is to keep a status quo and use array_size()
instead.

	sizeof_priv = sizeof(mux);
	sizeof_priv += array_size(child);
	sizeof_priv += array_size(chan);

> > > > I rather would see two times array_size() than the above.
> >
> > > OK
> >
> > > > >       sizeof_priv += sizeof_ext_info;

-- 
With Best Regards,
Andy Shevchenko