Skip to contentSkip to conversation

iio: iio-mux: use flexible array member

PATCH Superseded Rosen Penev 2 participants 6 messages Mar 15 – Mar 16

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iio: iio-mux: use flexible array member

Allows using struct_size to simplify allocation slightly.

Removes a pointer from the struct.

---
 drivers/iio/multiplexer/iio-mux.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/iio/multiplexer/iio-mux.c +2 -4
index b742ca9a99d1..8db061413829 100644
@@ -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);
Signature · 1 line
2.53.0
#1 lore raw
Quoted 3 lines · Rosen Penev

Allows using struct_size to simplify allocation slightly.

Removes a pointer from the struct.

...

drivers/iio/multiplexer/iio-mux.c static int mux_probe(struct platform_device *pdev) · line 384

Quoted 4 lines
drivers/iio/multiplexer/iio-mux.c +1 -2
-	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.

Quoted 1 line

sizeof_priv += sizeof_ext_info;

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#2 lore raw
Quoted 8 lines · Andy Shevchenko
Quoted 3 lines · Rosen Penev

Allows using struct_size to simplify allocation slightly.

Removes a pointer from the struct.

...

Quoted 7 lines

drivers/iio/multiplexer/iio-mux.c static int mux_probe(struct platform_device *pdev) · line 384

Quoted 4 lines
drivers/iio/multiplexer/iio-mux.c +1 -2
-     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.

Quoted 3 lines

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

OK

Quoted 8 lines
Quoted 2 lines
      sizeof_priv += sizeof_ext_info;

--
With Best Regards,
Andy Shevchenko

#3 lore raw
Quoted 3 lines · Rosen Penev
Quoted 1 line · Andy Shevchenko

On Sun, Mar 15, 2026 at 03:45:09PM -0700, Rosen Penev wrote:

...

Quoted 8 lines
Quoted 7 lines

drivers/iio/multiplexer/iio-mux.c static int mux_probe(struct platform_device *pdev) · line 384

Quoted 4 lines
drivers/iio/multiplexer/iio-mux.c +1 -2
-     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?

Quoted 5 lines
Quoted 1 line

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

OK

Quoted 1 line
Quoted 1 line
      sizeof_priv += sizeof_ext_info;
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#4 lore raw
Quoted 13 lines · Andy Shevchenko
Quoted 3 lines · Rosen Penev
Quoted 1 line · Andy Shevchenko

On Sun, Mar 15, 2026 at 03:45:09PM -0700, Rosen Penev wrote:

...

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
drivers/iio/multiplexer/iio-mux.c +1 -2
-     sizeof_priv = sizeof(*mux);
-     sizeof_priv += sizeof(*mux->child) * children;
+     sizeof_priv = struct_size(mux, child, children);
      sizeof_priv += sizeof(*mux->chan) * children;
Quoted 5 lines
Quoted 3 lines
Quoted 2 lines

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.

Quoted 12 lines
Quoted 6 lines
Quoted 2 lines

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

OK

Quoted 1 line
Quoted 1 line
      sizeof_priv += sizeof_ext_info;

--
With Best Regards,
Andy Shevchenko

#5 lore raw
Quoted 6 lines · Rosen Penev
Quoted 4 lines · Andy Shevchenko
Quoted 3 lines · Rosen Penev
Quoted 1 line · Andy Shevchenko

On Sun, Mar 15, 2026 at 03:45:09PM -0700, Rosen Penev wrote:

...

Quoted 10 lines
Quoted 10 lines
Quoted 8 lines
Quoted 7 lines

drivers/iio/multiplexer/iio-mux.c static int mux_probe(struct platform_device *pdev) · line 384

Quoted 4 lines
drivers/iio/multiplexer/iio-mux.c +1 -2
-     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?

Quoted 8 lines

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);
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 1 line

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

OK

Quoted 1 line
Quoted 1 line
      sizeof_priv += sizeof_ext_info;
Signature · 2 lines
With Best Regards,
Andy Shevchenko