iio: adc: ltc2309: Optimize chip_info structure layout

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>
Reviewed-by: Nuno Sá <nuno.sa@analog.com>
Reviewed-by: Andy Shevchenko <andy@kernel.org>
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
This commit is contained in:
Carlos Jones Jr 2026-03-31 09:24:57 +08:00 committed by Jonathan Cameron
parent 671b8541c7
commit 7a89047a36

View File

@ -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,