Skip to contentSkip to conversation

iio: add __counted_by_ptr attribute to channels in struct iio_dev

PATCH Applied Bill Wendling 3 participants 3 messages Sep 23 – Sep 25 IIO

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iio: add __counted_by_ptr attribute to channels in struct iio_dev

In 'struct iio_dev', the 'channels' pointer refers to an array of IIO channel specifications ('struct iio_chan_spec const'), and the size of this array is tracked by the 'num_channels' field within the same struct.

Applying the '__counted_by_ptr' attribute to 'channels' allows KASAN and compiler-based bounds checkers to verify that accesses to 'channels' remain within bounds at runtime.

---
 include/linux/iio/iio.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/iio/iio.h +1 -1
index 711c00f67371..20505139a61b 100644
@@ -648,7 +648,7 @@ struct iio_dev {
 	struct iio_poll_func		*pollfunc;
 	struct iio_poll_func		*pollfunc_event;
 
-	struct iio_chan_spec const	*channels;
+	struct iio_chan_spec const	*channels __counted_by_ptr(num_channels);
 	int				num_channels;
 
 	const char			*name;
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
Reviewed-by on v1 1/1
#1 lore raw
Quoted 5 lines · Bill Wendling

In 'struct iio_dev', the 'channels' pointer refers to an array of IIO channel specifications ('struct iio_chan_spec const'), and the size of this array is tracked by the 'num_channels' field within the same struct.

Quoted 7 lines

Applying the '__counted_by_ptr' attribute to 'channels' allows KASAN and compiler-based bounds checkers to verify that accesses to 'channels' remain within bounds at runtime.

Thanks
-Gustavo

Quoted 17 lines
---
  include/linux/iio/iio.h | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/iio/iio.h +1 -1
index 711c00f67371..20505139a61b 100644
@@ -648,7 +648,7 @@ struct iio_dev {
  	struct iio_poll_func		*pollfunc;
  	struct iio_poll_func		*pollfunc_event;
 
-	struct iio_chan_spec const	*channels;
+	struct iio_chan_spec const	*channels __counted_by_ptr(num_channels);
  	int				num_channels;
 
  	const char			*name;
maintainer Applied
#2 lore raw
Quoted 31 lines · On Wed, 23 Sep 2026 14:48:24 +0900 "Gustavo A. R. Silva"…
Quoted 5 lines · Bill Wendling

In 'struct iio_dev', the 'channels' pointer refers to an array of IIO channel specifications ('struct iio_chan_spec const'), and the size of this array is tracked by the 'num_channels' field within the same struct.

Quoted 7 lines

Applying the '__counted_by_ptr' attribute to 'channels' allows KASAN and compiler-based bounds checkers to verify that accesses to 'channels' remain within bounds at runtime.

Thanks
-Gustavo

Quoted 12 lines
---
  include/linux/iio/iio.h | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/iio/iio.h +0 -0
index 711c00f67371..20505139a61b 100644
@@ -648,7 +648,7 @@ struct iio_dev {
  	struct iio_poll_func		*pollfunc;
  	struct iio_poll_func		*pollfunc_event;
 
Quoted 6 lines

include/linux/iio/iio.h struct iio_dev { · line 654

Quoted 5 lines
include/linux/iio/iio.h +1 -1
-	struct iio_chan_spec const	*channels;
+	struct iio_chan_spec const	*channels __counted_by_ptr(num_channels);
  	int				num_channels;
 
  	const char			*name;  

Will be interesting to see if this shakes anything loose. It's reasonably common for channels to actually point to a larger array as some variant of a device only has a subset of channels. Hopefully no driver is using that to be sneaky!

Anyhow, looks good to me.

Applied.

thanks,

Jonathan