iio: Fix core buffer demux failure to account for unwanted channels at tail
Jonathan Cameron
maintainerPATCH 1/1 iio: Fix core buffer demux failure to account for unwanted channels at tail
From: Jonathan Cameron <[email protected]>
The IIO channel demultiplexer code is there to deal with a mismatch between the channels captured and those requested by user space either due to driver supporting only particular channel combinations (available_scan_masks) or due to multiple concurrent consumers (e.g. userspace IIO buffered interfaces and an inkernel consumer such as a touch screen).
Whilst this code is exercised by many drivers, a corner case has been hiding there all along.
Consider an input of (postfix is the channel size)
a_32, b_32, c_32, d_32, ts_64
and desired output of
a32, b_32, ts_64
the current code ends up with
a32, b_32, c_32, d_32
because of a failure to iterate over the tail of unwanted channels (here c_32 and d_32).
Fix this by adding the code to walk the channels in the gap.
---
This is an RFT because whilst the reporter has confirmed that it works for their case, it is touching fiddly code and I don't have the original set of tests to hand that I used when writing that code.
Hence I'd like a lot of eyes + some testing on this. A number of drivers should have hit this such as some of the larger IMUs, but only with very specific channel combinations that perhaps were never of interest to users.
Thanks to Jiangping Shen for all their hard work figuring out what was wrong!
Whilst this is being tested I'll try to figure out a Fixes tag. There is some code movement so needs more digging that I have time for today.
drivers/iio/industrialio-buffer.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
drivers/iio/industrialio-buffer.c +14 -0
@@ -1082,6 +1082,20 @@ static int iio_buffer_update_demux(struct iio_dev *indio_dev, out_loc += length; in_loc += length; } +/* Walk remaining bits of active_scan_mask */ + in_ind =find_next_bit (indio_dev->active_scan_mask, masklength, + in_ind +1 ); +while (in_ind != masklength) { + ret =iio_storage_bytes_for_si (indio_dev, in_ind); +if (ret <0 ) + goto error_clear_mux_table; + + length = ret; +/* Make sure we are aligned */ + in_loc =roundup (in_loc, length) + length; + in_ind =find_next_bit (indio_dev->active_scan_mask, + masklength, in_ind +1 ); + }/* Relies on scan_timestamp being last */ if (buffer->scan_timestamp) { ret =iio_storage_bytes_for_timestamp (indio_dev);
Signature · 1 line
2.50.1
Quoted 10 lines · Jonathan Cameron
From: Jonathan Cameron <[email protected]>
The IIO channel demultiplexer code is there to deal with a mismatch between the channels captured and those requested by user space either due to driver supporting only particular channel combinations (
available_scan_masks) or due to multiple concurrent consumers (e.g. userspace IIO buffered interfaces and an inkernel consumer such as a touch screen).
Quoted 4 lines
Whilst this code is exercised by many drivers, a corner case has been hiding there all along.
Consider an input of (postfix is the channel size)
size in bits
Quoted 5 lines
a_32,b_32,c_32,d_32,ts_64and desired output of
a32,
b_32,ts_64
You missed underscore here and below.
Quoted 4 lines
the current code ends up with
a32,
b_32,c_32,d_32
Quoted 8 lines
because of a failure to iterate over the tail of unwanted channels (here
c_32andd_32).Fix this by adding the code to walk the channels in the gap.
Fixes?
...
Quoted 3 lines
Whilst this is being tested I'll try to figure out a Fixes tag. There is some code movement so needs more digging that I have time for today.
Ah, okay.
drivers/iio/industrialio-buffer.c static int iio_buffer_update_demux(struct iio_dev *indio_dev, · line 1088
Quoted 4 lines
drivers/iio/industrialio-buffer.c +4
-0+/* Walk remaining bits of active_scan_mask */ + in_ind =find_next_bit (indio_dev->active_scan_mask, masklength, + in_ind +1 ); +while (in_ind != masklength) {
for_each_set_bit_from() ?
Quoted 10 lines
drivers/iio/industrialio-buffer.c +10
-0+ ret =iio_storage_bytes_for_si (indio_dev, in_ind); +if (ret <0 ) + goto error_clear_mux_table; + + length = ret; +/* Make sure we are aligned */ + in_loc =roundup (in_loc, length) + length; + in_ind =find_next_bit (indio_dev->active_scan_mask, + masklength, in_ind +1 ); + }
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 50 lines · On Sun, 3 Aug 2025 21:46:50 +0200 Andy Shevchenko <[email protected]>…
Quoted 10 lines · Jonathan Cameron
From: Jonathan Cameron <[email protected]>
The IIO channel demultiplexer code is there to deal with a mismatch between the channels captured and those requested by user space either due to driver supporting only particular channel combinations (
available_scan_masks) or due to multiple concurrent consumers (e.g. userspace IIO buffered interfaces and an inkernel consumer such as a touch screen).Quoted 4 lines
Whilst this code is exercised by many drivers, a corner case has been hiding there all along.
Consider an input of (postfix is the channel size)
size in bits
Quoted 5 lines
a_32,b_32,c_32,d_32,ts_64and desired output of
a32,
b_32,ts_64You missed underscore here and below.
Quoted 4 lines
the current code ends up with
a32,
b_32,c_32,d_32Quoted 8 lines
because of a failure to iterate over the tail of unwanted channels (here
c_32andd_32).Fix this by adding the code to walk the channels in the gap.
Fixes?
...
Quoted 3 lines
Whilst this is being tested I'll try to figure out a Fixes tag. There is some code movement so needs more digging that I have time for today.
Ah, okay.
Quoted 6 lines
drivers/iio/industrialio-buffer.c static int iio_buffer_update_demux(struct iio_dev *indio_dev, · line 1088
Quoted 4 lines
drivers/iio/industrialio-buffer.c +4
-0+/* Walk remaining bits of active_scan_mask */ + in_ind =find_next_bit (indio_dev->active_scan_mask, masklength, + in_ind +1 ); +while (in_ind != masklength) {
for_each_set_bit_from()?
Would work but then takes the style away from the code above that this effectively duplicates. I'd need to have a closer look to see if we can potentially convert that as well. I don't want two very different looking bits of code effectively doing the same thing under subtly different constraints.
Jonathan
Quoted 13 lines
Quoted 11 lines
drivers/iio/industrialio-buffer.c +10
-0+ ret =iio_storage_bytes_for_si (indio_dev, in_ind); +if (ret <0 ) + goto error_clear_mux_table; + + length = ret; +/* Make sure we are aligned */ + in_loc =roundup (in_loc, length) + length; + in_ind =find_next_bit (indio_dev->active_scan_mask, + masklength, in_ind +1 ); + }
Quoted 3 lines · Jonathan Cameron
Quoted 1 line · On Sun, 3 Aug 2025 21:46:50 +0200 Andy Shevchenko <[email protected]>…
On Sat, Aug 2, 2025 at 7:15 PM Jonathan Cameron <[email protected]> wrote:
...
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
drivers/iio/industrialio-buffer.c +4
-0+/* Walk remaining bits of active_scan_mask */ + in_ind =find_next_bit (indio_dev->active_scan_mask, masklength, + in_ind +1 ); +while (in_ind != masklength) {
Quoted 7 lines
Quoted 1 line
for_each_set_bit_from()?Would work but then takes the style away from the code above that this effectively duplicates. I'd need to have a closer look to see if we can potentially convert that as well. I don't want two very different looking bits of code effectively doing the same thing under subtly different constraints.
I see. But can we have this as a fix followed by the conversion patch in one series, so we won't forget about that?
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
drivers/iio/industrialio-buffer.c +10
-0+ ret =iio_storage_bytes_for_si (indio_dev, in_ind); +if (ret <0 ) + goto error_clear_mux_table; + + length = ret; +/* Make sure we are aligned */ + in_loc =roundup (in_loc, length) + length; + in_ind =find_next_bit (indio_dev->active_scan_mask, + masklength, in_ind +1 ); + }
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 15 lines · On Mon, 4 Aug 2025 14:52:23 +0200 Andy Shevchenko <[email protected]>…
Quoted 3 lines · Jonathan Cameron
Quoted 1 line · On Sun, 3 Aug 2025 21:46:50 +0200 Andy Shevchenko <[email protected]>…
On Sat, Aug 2, 2025 at 7:15 PM Jonathan Cameron <[email protected]> wrote:
...
Quoted 7 lines
Quoted 6 lines
drivers/iio/industrialio-buffer.c static int iio_buffer_update_demux(struct iio_dev *indio_dev, · line 1088
Quoted 4 lines
drivers/iio/industrialio-buffer.c +4
-0+/* Walk remaining bits of active_scan_mask */ + in_ind =find_next_bit (indio_dev->active_scan_mask, masklength, + in_ind +1 ); +while (in_ind != masklength) {
for_each_set_bit_from()?
Quoted 8 lines
Quoted 5 lines
Would work but then takes the style away from the code above that this effectively duplicates. I'd need to have a closer look to see if we can potentially convert that as well. I don't want two very different looking bits of code effectively doing the same thing under subtly different constraints.
I see. But can we have this as a fix followed by the conversion patch in one series, so we won't forget about that?
Maybe. This is a nasty bug so I'm not sure I want to delay the fix whilst I work out how to cleanup the section above.
I'll see if I can find time to work on this. The time is mostly on building test cases rather than the code.
Quoted 13 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
drivers/iio/industrialio-buffer.c +10
-0+ ret =iio_storage_bytes_for_si (indio_dev, in_ind); +if (ret <0 ) + goto error_clear_mux_table; + + length = ret; +/* Make sure we are aligned */ + in_loc =roundup (in_loc, length) + length; + in_ind =find_next_bit (indio_dev->active_scan_mask, + masklength, in_ind +1 ); + }
Quoted 58 lines · Jonathan Cameron
From: Jonathan Cameron <[email protected]>
The IIO channel demultiplexer code is there to deal with a mismatch between the channels captured and those requested by user space either due to driver supporting only particular channel combinations (
available_scan_masks) or due to multiple concurrent consumers (e.g. userspace IIO buffered interfaces and an inkernel consumer such as a touch screen).Whilst this code is exercised by many drivers, a corner case has been hiding there all along.
Consider an input of (postfix is the channel size)
a_32,b_32,c_32,d_32,ts_64and desired output of
a32,
b_32,ts_64the current code ends up with
a32,
b_32,c_32,d_32because of a failure to iterate over the tail of unwanted channels (here
c_32andd_32).Fix this by adding the code to walk the channels in the gap.
Reported-by Jianping Shen <[email protected]>Closes https://lore.kernel.org/all/AM8PR10MB4721FB1A78F25B204BE3A26ACD5FA@AM8PR10MB4721.EURPRD10.PROD.OUTLOOK.COM/ open hereSigned-off-by Jonathan Cameron <[email protected]>---
This is an RFT because whilst the reporter has confirmed that it works for their case, it is touching fiddly code and I don't have the original set of tests to hand that I used when writing that code.
Hence I'd like a lot of eyes + some testing on this. A number of drivers should have hit this such as some of the larger IMUs, but only with very specific channel combinations that perhaps were never of interest to users.
Thanks to Jiangping Shen for all their hard work figuring out what was wrong!
Whilst this is being tested I'll try to figure out a Fixes tag. There is some code movement so needs more digging that I have time for today.
drivers/iio/industrialio-buffer.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)drivers/iio/industrialio-buffer.c +0
-0
drivers/iio/industrialio-buffer.c static int iio_buffer_update_demux(struct iio_dev *indio_dev, · line 1087
Quoted 6 lines
drivers/iio/industrialio-buffer.c +3
-0@@ -1082,6 +1082,20 @@ static int iio_buffer_update_demux(struct iio_dev *indio_dev, out_loc += length; in_loc += length; } +/* Walk remaining bits of active_scan_mask */ + in_ind =find_next_bit (indio_dev->active_scan_mask, masklength, + in_ind +1 );
I wonder if it matters to check that in_ind + 1 is in fact lower than masklength? Not that it will be an issue for find_next_bit() but we will fail the expectation:
if (unlikely(__start >= sz)) [1]
And being this a sensible path, I thought it's worth (at least) questioning...
Other than that kind of nit comment, patch looks good.
[1]: https://elixir.bootlin.com/linux/v6.16/source/lib/find_bit.c#L50
Thanks!
- Nuno Sá
Quoted 17 lines
drivers/iio/industrialio-buffer.c +11
-0+while (in_ind != masklength) { + ret =iio_storage_bytes_for_si (indio_dev, in_ind); +if (ret <0 ) + goto error_clear_mux_table; + + length = ret; +/* Make sure we are aligned */ + in_loc =roundup (in_loc, length) + length; + in_ind =find_next_bit (indio_dev->active_scan_mask, + masklength, in_ind +1 ); + }/* Relies on scan_timestamp being last */ if (buffer->scan_timestamp) { ret =iio_storage_bytes_for_timestamp (indio_dev);Signature · 1 line
2.50.1
Quoted 1 line · Nuno Sá
On Sat, Aug 02, 2025 at 06:15:39PM +0100, Jonathan Cameron wrote:
...
Quoted 11 lines
drivers/iio/industrialio-buffer.c static int iio_buffer_update_demux(struct iio_dev *indio_dev, · line 1087
Quoted 3 lines
drivers/iio/industrialio-buffer.c +3
-0+/* Walk remaining bits of active_scan_mask */ + in_ind =find_next_bit (indio_dev->active_scan_mask, masklength, + in_ind +1 );I wonder if it matters to check that
in_ind+ 1 is in fact lower than masklength? Not that it will be an issue forfind_next_bit()but we will fail the expectation:if (
unlikely(__start >= sz)) [1]And being this a sensible path, I thought it's worth (at least) questioning...
It doesn't matter. The find_*_bit() are all aligned to return sz for anything "not found anymore" cases, so it will be okay.
Quoted 15 lines
Other than that kind of nit comment, patch looks good.
[1]: https://elixir.bootlin.com/linux/v6.16/source/lib/find_bit.c#L50
Quoted 11 lines
drivers/iio/industrialio-buffer.c +11
-0+while (in_ind != masklength) { + ret =iio_storage_bytes_for_si (indio_dev, in_ind); +if (ret <0 ) + goto error_clear_mux_table; + + length = ret; +/* Make sure we are aligned */ + in_loc =roundup (in_loc, length) + length; + in_ind =find_next_bit (indio_dev->active_scan_mask, + masklength, in_ind +1 ); + }
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 13 lines · Andy Shevchenko
Quoted 1 line · Nuno Sá
On Sat, Aug 02, 2025 at 06:15:39PM +0100, Jonathan Cameron wrote:
...
Quoted 8 lines
drivers/iio/industrialio-buffer.c static int iio_buffer_update_demux(struct iio_dev *indio_dev, · line 1087
Quoted 3 lines
drivers/iio/industrialio-buffer.c +3
-0+/* Walk remaining bits of active_scan_mask */ + in_ind =find_next_bit (indio_dev->active_scan_mask, masklength, + in_ind +1 );I wonder if it matters to check that
in_ind+ 1 is in fact lower than masklength? Not that it will be an issue forfind_next_bit()but we will fail the expectation:
Quoted 6 lines
Quoted 3 lines
if (
unlikely(__start >= sz)) [1]And being this a sensible path, I thought it's worth (at least) questioning...
It doesn't matter. The find_*_bit() are all aligned to return sz for anything "not found anymore" cases, so it will be okay.
I know :):
"...Not that it will be an issue for find_next_bit()..."
I was mostly worried by performance as we'll have a compiler hint that will pretty much fail (that ´if (unlikely(__start >= sz))' for every sample we push and I guess the CPU will have to unroll that prediction. Maybe it will be smart enough to adapt.
But as I said, it might be neglectable but still worth at least questioning...
- Nuno Sá
Quoted 20 lines
Quoted 16 lines
Other than that kind of nit comment, patch looks good.
[1]: https://elixir.bootlin.com/linux/v6.16/source/lib/find_bit.c#L50
Quoted 11 lines
drivers/iio/industrialio-buffer.c +11
-0+while (in_ind != masklength) { + ret =iio_storage_bytes_for_si (indio_dev, in_ind); +if (ret <0 ) + goto error_clear_mux_table; + + length = ret; +/* Make sure we are aligned */ + in_loc =roundup (in_loc, length) + length; + in_ind =find_next_bit (indio_dev->active_scan_mask, + masklength, in_ind +1 ); + }Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 3 lines · Nuno Sá
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Nuno Sá
On Sat, Aug 02, 2025 at 06:15:39PM +0100, Jonathan Cameron wrote:
...
Quoted 15 lines
Quoted 8 lines
Quoted 8 lines
drivers/iio/industrialio-buffer.c static int iio_buffer_update_demux(struct iio_dev *indio_dev, · line 1087
Quoted 3 lines
drivers/iio/industrialio-buffer.c +3
-0+/* Walk remaining bits of active_scan_mask */ + in_ind =find_next_bit (indio_dev->active_scan_mask, masklength, + in_ind +1 );I wonder if it matters to check that
in_ind+ 1 is in fact lower than masklength? Not that it will be an issue forfind_next_bit()but we will fail the expectation:Quoted 6 lines
Quoted 3 lines
if (
unlikely(__start >= sz)) [1]And being this a sensible path, I thought it's worth (at least) questioning...
It doesn't matter. The find_*_bit() are all aligned to return sz for anything "not found anymore" cases, so it will be okay.
Quoted 8 lines
I know :):
"...Not that it will be an issue for
find_next_bit()..."I was mostly worried by performance as we'll have a compiler hint that will pretty much fail (that ´if (
unlikely(__start >= sz))' for every sample we push and I guess the CPU will have to unroll that prediction. Maybe it will be smart enough to adapt.
Ah, I see now. Yeah, there might be a hint to skip the branch which is unlikely() for.
Quoted 18 lines
But as I said, it might be neglectable but still worth at least questioning...
Quoted 15 lines
Quoted 15 lines
Other than that kind of nit comment, patch looks good.
[1]: https://elixir.bootlin.com/linux/v6.16/source/lib/find_bit.c#L50
Quoted 11 lines
drivers/iio/industrialio-buffer.c +11
-0+while (in_ind != masklength) { + ret =iio_storage_bytes_for_si (indio_dev, in_ind); +if (ret <0 ) + goto error_clear_mux_table; + + length = ret; +/* Make sure we are aligned */ + in_loc =roundup (in_loc, length) + length; + in_ind =find_next_bit (indio_dev->active_scan_mask, + masklength, in_ind +1 ); + }
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 26 lines · On Tue, 5 Aug 2025 14:41:03 +0200 Andy Shevchenko <[email protected]>…
Quoted 3 lines · Nuno Sá
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Nuno Sá
On Sat, Aug 02, 2025 at 06:15:39PM +0100, Jonathan Cameron wrote:
...
Quoted 19 lines
Quoted 8 lines
Quoted 8 lines
drivers/iio/industrialio-buffer.c static int iio_buffer_update_demux(struct iio_dev *indio_dev, · line 1087
Quoted 3 lines
drivers/iio/industrialio-buffer.c +3
-0+/* Walk remaining bits of active_scan_mask */ + in_ind =find_next_bit (indio_dev->active_scan_mask, masklength, + in_ind +1 );I wonder if it matters to check that
in_ind+ 1 is in fact lower than masklength? Not that it will be an issue forfind_next_bit()but we will fail the expectation:Quoted 6 lines
Quoted 3 lines
if (
unlikely(__start >= sz)) [1]And being this a sensible path, I thought it's worth (at least) questioning...
It doesn't matter. The find_*_bit() are all aligned to return sz for anything "not found anymore" cases, so it will be okay.
I know :):
"...Not that it will be an issue for
find_next_bit()..."
Quoted 7 lines
Quoted 4 lines
I was mostly worried by performance as we'll have a compiler hint that will pretty much fail (that ´if (
unlikely(__start >= sz))' for every sample we push and I guess the CPU will have to unroll that prediction. Maybe it will be smart enough to adapt.Ah, I see now. Yeah, there might be a hint to skip the branch which is
unlikely()for.
Assuming I remember how this all works...
This doesn't happen on the fast path (pushing samples)
It's a setup activity on a buffer being enabled. The code is generating a table of offsets and sizes that are then used to on every sample. So I don't think it's worth bothering to optimize it.
Jonathan
Quoted 20 lines
Quoted 19 lines
But as I said, it might be neglectable but still worth at least questioning...
Quoted 15 lines
Quoted 15 lines
Other than that kind of nit comment, patch looks good.
[1]: https://elixir.bootlin.com/linux/v6.16/source/lib/find_bit.c#L50
Quoted 11 lines
drivers/iio/industrialio-buffer.c +11
-0+while (in_ind != masklength) { + ret =iio_storage_bytes_for_si (indio_dev, in_ind); +if (ret <0 ) + goto error_clear_mux_table; + + length = ret; +/* Make sure we are aligned */ + in_loc =roundup (in_loc, length) + length; + in_ind =find_next_bit (indio_dev->active_scan_mask, + masklength, in_ind +1 ); + }
Nuno Sá
Reviewed-byQuoted 37 lines · Jonathan Cameron
Quoted 26 lines · On Tue, 5 Aug 2025 14:41:03 +0200 Andy Shevchenko <[email protected]>…
Quoted 3 lines · Nuno Sá
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Nuno Sá
On Sat, Aug 02, 2025 at 06:15:39PM +0100, Jonathan Cameron wrote:
...
Quoted 19 lines
Quoted 8 lines
Quoted 8 lines
drivers/iio/industrialio-buffer.c static int iio_buffer_update_demux(struct iio_dev *indio_dev, · line 1087
Quoted 3 lines
drivers/iio/industrialio-buffer.c +3
-0+/* Walk remaining bits of active_scan_mask */ + in_ind =find_next_bit (indio_dev->active_scan_mask, masklength, + in_ind +1 );I wonder if it matters to check that
in_ind+ 1 is in fact lower than masklength? Not that it will be an issue forfind_next_bit()but we will fail the expectation:Quoted 6 lines
Quoted 3 lines
if (
unlikely(__start >= sz)) [1]And being this a sensible path, I thought it's worth (at least) questioning...
It doesn't matter. The find_*_bit() are all aligned to return sz for anything "not found anymore" cases, so it will be okay.
I know :):
"...Not that it will be an issue for
find_next_bit()..."Quoted 7 lines
Quoted 4 lines
I was mostly worried by performance as we'll have a compiler hint that will pretty much fail (that ´if (
unlikely(__start >= sz))' for every sample we push and I guess the CPU will have to unroll that prediction. Maybe it will be smart enough to adapt.Ah, I see now. Yeah, there might be a hint to skip the branch which is
unlikely()for.
Quoted 6 lines
Assuming I remember how this all works...
This doesn't happen on the fast path (pushing samples)
It's a setup activity on a buffer being enabled. The code is generating a table of offsets and sizes that are then used to on every sample. So I don't think it's worth bothering to optimize it.
Right! For some reason I assumes this was running on every push (which would not be a great idea anyway we did it)
That said, the change LGTM:
Quoted 24 lines
Jonathan
Quoted 20 lines
Quoted 19 lines
But as I said, it might be neglectable but still worth at least questioning...
Quoted 15 lines
Quoted 15 lines
Other than that kind of nit comment, patch looks good.
[1]: https://elixir.bootlin.com/linux/v6.16/source/lib/find_bit.c#L50
Quoted 11 lines
drivers/iio/industrialio-buffer.c +11
-0+while (in_ind != masklength) { + ret =iio_storage_bytes_for_si (indio_dev, in_ind); +if (ret <0 ) + goto error_clear_mux_table; + + length = ret; +/* Make sure we are aligned */ + in_loc =roundup (in_loc, length) + length; + in_ind =find_next_bit (indio_dev->active_scan_mask, + masklength, in_ind +1 ); + }