All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH RFT] iio: Fix core buffer demux failure to account for unwanted channels at tail
@ 2025-08-02 17:15 Jonathan Cameron
  2025-08-03 19:46 ` Andy Shevchenko
  2025-08-04 15:38 ` Nuno Sá
  0 siblings, 2 replies; 11+ messages in thread
From: Jonathan Cameron @ 2025-08-02 17:15 UTC (permalink / raw)
  To: linux-iio, David Lechner, Nuno Sá, Andy Shevchenko,
	Shen Jianping
  Cc: Jonathan Cameron

From: Jonathan Cameron <Jonathan.Cameron@huawei.com>

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.

Reported-by: Jianping Shen <Jianping.Shen@de.bosch.com>
Closes: https://lore.kernel.org/all/AM8PR10MB4721FB1A78F25B204BE3A26ACD5FA@AM8PR10MB4721.EURPRD10.PROD.OUTLOOK.COM/
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
---

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(+)

diff --git a/drivers/iio/industrialio-buffer.c b/drivers/iio/industrialio-buffer.c
index a80f7cc25a27..d7dd9764091d 100644
--- a/drivers/iio/industrialio-buffer.c
+++ b/drivers/iio/industrialio-buffer.c
@@ -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);
-- 
2.50.1


^ permalink raw reply related	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2025-08-11  9:54 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-08-02 17:15 [PATCH RFT] iio: Fix core buffer demux failure to account for unwanted channels at tail Jonathan Cameron
2025-08-03 19:46 ` Andy Shevchenko
2025-08-04 10:48   ` Jonathan Cameron
2025-08-04 12:52     ` Andy Shevchenko
2025-08-06 15:40       ` Jonathan Cameron
2025-08-04 15:38 ` Nuno Sá
2025-08-04 16:02   ` Andy Shevchenko
2025-08-05  8:16     ` Nuno Sá
2025-08-05 12:41       ` Andy Shevchenko
2025-08-06 15:36         ` Jonathan Cameron
2025-08-11  9:54           ` Nuno Sá

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.