[PATCH v2 2/2] iio: adc: ltc2309: Optimize chip_info structure layout

Carlos Jones Jr posted 2 patches 1 day, 23 hours ago
[PATCH v2 2/2] iio: adc: ltc2309: Optimize chip_info structure layout
Posted by Carlos Jones Jr 1 day, 23 hours ago
Improve the ltc2309_chip_info structure with better type safety and
memory efficiency:

- Add __counted_by_ptr() annotation to the channels pointer, linking
  it to num_channels for improved bounds checking and kernel hardening
- Reorder structure fields to minimize padding:
  * Place read_delay_us before num_channels
  * This reduces struct size and eliminates internal gaps
- Reorder field initialization to match the structure definition order

The __counted_by_ptr() attribute enables compile-time and runtime
verification that array accesses to channels[] stay within the bounds
specified by num_channels, improving memory safety.

Signed-off-by: Carlos Jones Jr <carlosjr.jones@analog.com>
---
 drivers/iio/adc/ltc2309.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/iio/adc/ltc2309.c b/drivers/iio/adc/ltc2309.c
index 094966289922..87b78d0353f1 100644
--- a/drivers/iio/adc/ltc2309.c
+++ b/drivers/iio/adc/ltc2309.c
@@ -121,22 +121,22 @@ static const struct iio_chan_spec ltc2309_channels[] = {
 
 struct ltc2309_chip_info {
 	const char *name;
-	const struct iio_chan_spec *channels;
 	unsigned int read_delay_us;
 	int num_channels;
+	const struct iio_chan_spec *channels __counted_by_ptr(num_channels);
 };
 
 static const struct ltc2309_chip_info ltc2305_chip_info = {
 	.name = "ltc2305",
-	.channels = ltc2305_channels,
 	.read_delay_us = 2,
 	.num_channels = ARRAY_SIZE(ltc2305_channels),
+	.channels = ltc2305_channels,
 };
 
 static const struct ltc2309_chip_info ltc2309_chip_info = {
 	.name = "ltc2309",
-	.channels = ltc2309_channels,
 	.num_channels = ARRAY_SIZE(ltc2309_channels),
+	.channels = ltc2309_channels,
 };
 
 static int ltc2309_read_raw_channel(struct ltc2309 *ltc2309,
-- 
2.43.0