diff mbox series

[v1,3/7] gpio: 74x164: Annotate buffer with __counted_by()

Message ID 20250203121843.3183991-4-andriy.shevchenko@linux.intel.com (mailing list archive)
State New
Headers show
Series gpio: 74x164: Refactor and clean up the driver | expand

Commit Message

Andy Shevchenko Feb. 3, 2025, 12:17 p.m. UTC
Add the __counted_by() compiler attribute to the flexible array member
volumes to improve access bounds-checking via CONFIG_UBSAN_BOUNDS and
CONFIG_FORTIFY_SOURCE.

Use struct_size() instead of manually calculating the number of bytes to
allocate the private structure with a buffer.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
 drivers/gpio/gpio-74x164.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

Comments

Gustavo A. R. Silva Feb. 4, 2025, 1:28 a.m. UTC | #1
On 03/02/25 22:47, Andy Shevchenko wrote:
> Add the __counted_by() compiler attribute to the flexible array member
> volumes to improve access bounds-checking via CONFIG_UBSAN_BOUNDS and
> CONFIG_FORTIFY_SOURCE.
> 
> Use struct_size() instead of manually calculating the number of bytes to
> allocate the private structure with a buffer.
> 
> Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>

Reviewed-by: Gustavo A. R. Silva <gustavoars@kernel.org>

Thanks!
--
Gustavo

> ---
>   drivers/gpio/gpio-74x164.c | 10 +++++-----
>   1 file changed, 5 insertions(+), 5 deletions(-)
> 
> diff --git a/drivers/gpio/gpio-74x164.c b/drivers/gpio/gpio-74x164.c
> index 70c662bbca7b..7844f8a58834 100644
> --- a/drivers/gpio/gpio-74x164.c
> +++ b/drivers/gpio/gpio-74x164.c
> @@ -30,7 +30,7 @@ struct gen_74x164_chip {
>   	 * register at the end of the transfer. So, to have a logical
>   	 * numbering, store the bytes in reverse order.
>   	 */
> -	u8			buffer[];
> +	u8			buffer[] __counted_by(registers);
>   };
>   
>   static int __gen_74x164_write_config(struct gen_74x164_chip *chip)
> @@ -97,6 +97,7 @@ static int gen_74x164_direction_output(struct gpio_chip *gc,
>   
>   static int gen_74x164_probe(struct spi_device *spi)
>   {
> +	struct device *dev = &spi->dev;
>   	struct gen_74x164_chip *chip;
>   	u32 nregs;
>   	int ret;
> @@ -116,10 +117,12 @@ static int gen_74x164_probe(struct spi_device *spi)
>   		return -EINVAL;
>   	}
>   
> -	chip = devm_kzalloc(&spi->dev, sizeof(*chip) + nregs, GFP_KERNEL);
> +	chip = devm_kzalloc(dev, struct_size(chip, buffer, nregs), GFP_KERNEL);
>   	if (!chip)
>   		return -ENOMEM;
>   
> +	chip->registers = nregs;
> +
>   	chip->gpiod_oe = devm_gpiod_get_optional(&spi->dev, "enable",
>   						 GPIOD_OUT_LOW);
>   	if (IS_ERR(chip->gpiod_oe))
> @@ -133,10 +136,7 @@ static int gen_74x164_probe(struct spi_device *spi)
>   	chip->gpio_chip.set = gen_74x164_set_value;
>   	chip->gpio_chip.set_multiple = gen_74x164_set_multiple;
>   	chip->gpio_chip.base = -1;
> -
> -	chip->registers = nregs;
>   	chip->gpio_chip.ngpio = GEN_74X164_NUMBER_GPIOS * chip->registers;
> -
>   	chip->gpio_chip.can_sleep = true;
>   	chip->gpio_chip.parent = &spi->dev;
>   	chip->gpio_chip.owner = THIS_MODULE;
diff mbox series

Patch

diff --git a/drivers/gpio/gpio-74x164.c b/drivers/gpio/gpio-74x164.c
index 70c662bbca7b..7844f8a58834 100644
--- a/drivers/gpio/gpio-74x164.c
+++ b/drivers/gpio/gpio-74x164.c
@@ -30,7 +30,7 @@  struct gen_74x164_chip {
 	 * register at the end of the transfer. So, to have a logical
 	 * numbering, store the bytes in reverse order.
 	 */
-	u8			buffer[];
+	u8			buffer[] __counted_by(registers);
 };
 
 static int __gen_74x164_write_config(struct gen_74x164_chip *chip)
@@ -97,6 +97,7 @@  static int gen_74x164_direction_output(struct gpio_chip *gc,
 
 static int gen_74x164_probe(struct spi_device *spi)
 {
+	struct device *dev = &spi->dev;
 	struct gen_74x164_chip *chip;
 	u32 nregs;
 	int ret;
@@ -116,10 +117,12 @@  static int gen_74x164_probe(struct spi_device *spi)
 		return -EINVAL;
 	}
 
-	chip = devm_kzalloc(&spi->dev, sizeof(*chip) + nregs, GFP_KERNEL);
+	chip = devm_kzalloc(dev, struct_size(chip, buffer, nregs), GFP_KERNEL);
 	if (!chip)
 		return -ENOMEM;
 
+	chip->registers = nregs;
+
 	chip->gpiod_oe = devm_gpiod_get_optional(&spi->dev, "enable",
 						 GPIOD_OUT_LOW);
 	if (IS_ERR(chip->gpiod_oe))
@@ -133,10 +136,7 @@  static int gen_74x164_probe(struct spi_device *spi)
 	chip->gpio_chip.set = gen_74x164_set_value;
 	chip->gpio_chip.set_multiple = gen_74x164_set_multiple;
 	chip->gpio_chip.base = -1;
-
-	chip->registers = nregs;
 	chip->gpio_chip.ngpio = GEN_74X164_NUMBER_GPIOS * chip->registers;
-
 	chip->gpio_chip.can_sleep = true;
 	chip->gpio_chip.parent = &spi->dev;
 	chip->gpio_chip.owner = THIS_MODULE;