]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
iio: adc: ltc2309: Optimize chip_info structure layout
authorCarlos Jones Jr <carlosjr.jones@analog.com>
Tue, 31 Mar 2026 01:24:57 +0000 (09:24 +0800)
committerJonathan Cameron <jic23@kernel.org>
Mon, 27 Apr 2026 08:58:17 +0000 (09:58 +0100)
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>
drivers/iio/adc/ltc2309.c

index 0949662899225800e304d27e39e58ba9016eba4a..87b78d0353f198588c2a4155944317074dd20791 100644 (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,