public inbox for linux-gpio@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCHv2] gpio: ljca: reduce struct allocation
@ 2026-03-10 22:20 Rosen Penev
  2026-03-11  7:49 ` Sakari Ailus
  2026-03-11 12:56 ` Linus Walleij
  0 siblings, 2 replies; 5+ messages in thread
From: Rosen Penev @ 2026-03-10 22:20 UTC (permalink / raw)
  To: linux-gpio
  Cc: Lixu Zhang, Sakari Ailus, Linus Walleij, Bartosz Golaszewski,
	Kees Cook, Gustavo A. R. Silva, open list,
	open list:KERNEL HARDENING (not covered by other areas):Keyword:b__counted_by(_le|_be)?b

Convert connect_mode to a flexible array member to avoid calling
kcalloc and to combine the allocations.

Add __counted_by for extra runtime analysis.

Signed-off-by: Rosen Penev <rosenp@gmail.com>
---
 v2: add counting variable
 drivers/gpio/gpio-ljca.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)

diff --git a/drivers/gpio/gpio-ljca.c b/drivers/gpio/gpio-ljca.c
index f32d1d237795..61252e0eb444 100644
--- a/drivers/gpio/gpio-ljca.c
+++ b/drivers/gpio/gpio-ljca.c
@@ -63,7 +63,6 @@ struct ljca_gpio_dev {
 	DECLARE_BITMAP(enabled_irqs, LJCA_MAX_GPIO_NUM);
 	DECLARE_BITMAP(reenable_irqs, LJCA_MAX_GPIO_NUM);
 	DECLARE_BITMAP(output_enabled, LJCA_MAX_GPIO_NUM);
-	u8 *connect_mode;
 	/* protect irq bus */
 	struct mutex irq_lock;
 	struct work_struct work;
@@ -72,6 +71,9 @@ struct ljca_gpio_dev {

 	u8 obuf[LJCA_GPIO_BUF_SIZE];
 	u8 ibuf[LJCA_GPIO_BUF_SIZE];
+
+	size_t num;
+	u8 connect_mode[] __counted_by(num);
 };

 static int ljca_gpio_config(struct ljca_gpio_dev *ljca_gpio, u8 gpio_id,
@@ -400,22 +402,20 @@ static int ljca_gpio_probe(struct auxiliary_device *auxdev,
 			   const struct auxiliary_device_id *aux_dev_id)
 {
 	struct ljca_client *ljca = auxiliary_dev_to_ljca_client(auxdev);
+	struct ljca_gpio_info *gpio_info = dev_get_platdata(&auxdev->dev);
 	struct ljca_gpio_dev *ljca_gpio;
 	struct gpio_irq_chip *girq;
 	int ret;

-	ljca_gpio = devm_kzalloc(&auxdev->dev, sizeof(*ljca_gpio), GFP_KERNEL);
+	ljca_gpio = devm_kzalloc(&auxdev->dev,
+				 struct_size(ljca_gpio, connect_mode, gpio_info->num),
+				 GFP_KERNEL);
 	if (!ljca_gpio)
 		return -ENOMEM;

+	ljca_gpio->num = gpio_info->num;
 	ljca_gpio->ljca = ljca;
-	ljca_gpio->gpio_info = dev_get_platdata(&auxdev->dev);
-	ljca_gpio->connect_mode = devm_kcalloc(&auxdev->dev,
-					       ljca_gpio->gpio_info->num,
-					       sizeof(*ljca_gpio->connect_mode),
-					       GFP_KERNEL);
-	if (!ljca_gpio->connect_mode)
-		return -ENOMEM;
+	ljca_gpio->gpio_info = gpio_info;

 	ret = devm_mutex_init(&auxdev->dev, &ljca_gpio->irq_lock);
 	if (ret)
--
2.53.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCHv2] gpio: ljca: reduce struct allocation
  2026-03-10 22:20 [PATCHv2] gpio: ljca: reduce struct allocation Rosen Penev
@ 2026-03-11  7:49 ` Sakari Ailus
  2026-03-11 12:56 ` Linus Walleij
  1 sibling, 0 replies; 5+ messages in thread
From: Sakari Ailus @ 2026-03-11  7:49 UTC (permalink / raw)
  To: Rosen Penev
  Cc: linux-gpio, Lixu Zhang, Linus Walleij, Bartosz Golaszewski,
	Kees Cook, Gustavo A. R. Silva, open list,
	open list:KERNEL HARDENING (not covered by other areas):Keyword:b__counted_by(_le|_be)?b

Hi Rosen,

On Tue, Mar 10, 2026 at 03:20:40PM -0700, Rosen Penev wrote:
> Convert connect_mode to a flexible array member to avoid calling
> kcalloc and to combine the allocations.
> 
> Add __counted_by for extra runtime analysis.
> 
> Signed-off-by: Rosen Penev <rosenp@gmail.com>
> ---
>  v2: add counting variable
>  drivers/gpio/gpio-ljca.c | 18 +++++++++---------
>  1 file changed, 9 insertions(+), 9 deletions(-)
> 
> diff --git a/drivers/gpio/gpio-ljca.c b/drivers/gpio/gpio-ljca.c
> index f32d1d237795..61252e0eb444 100644
> --- a/drivers/gpio/gpio-ljca.c
> +++ b/drivers/gpio/gpio-ljca.c
> @@ -63,7 +63,6 @@ struct ljca_gpio_dev {
>  	DECLARE_BITMAP(enabled_irqs, LJCA_MAX_GPIO_NUM);
>  	DECLARE_BITMAP(reenable_irqs, LJCA_MAX_GPIO_NUM);
>  	DECLARE_BITMAP(output_enabled, LJCA_MAX_GPIO_NUM);
> -	u8 *connect_mode;
>  	/* protect irq bus */
>  	struct mutex irq_lock;
>  	struct work_struct work;
> @@ -72,6 +71,9 @@ struct ljca_gpio_dev {
> 
>  	u8 obuf[LJCA_GPIO_BUF_SIZE];
>  	u8 ibuf[LJCA_GPIO_BUF_SIZE];
> +
> +	size_t num;

num is unsigned int in struct ljca_gpio_info so I'd use that here, too.

> +	u8 connect_mode[] __counted_by(num);
>  };
> 
>  static int ljca_gpio_config(struct ljca_gpio_dev *ljca_gpio, u8 gpio_id,
> @@ -400,22 +402,20 @@ static int ljca_gpio_probe(struct auxiliary_device *auxdev,
>  			   const struct auxiliary_device_id *aux_dev_id)
>  {
>  	struct ljca_client *ljca = auxiliary_dev_to_ljca_client(auxdev);
> +	struct ljca_gpio_info *gpio_info = dev_get_platdata(&auxdev->dev);
>  	struct ljca_gpio_dev *ljca_gpio;
>  	struct gpio_irq_chip *girq;
>  	int ret;
> 
> -	ljca_gpio = devm_kzalloc(&auxdev->dev, sizeof(*ljca_gpio), GFP_KERNEL);
> +	ljca_gpio = devm_kzalloc(&auxdev->dev,
> +				 struct_size(ljca_gpio, connect_mode, gpio_info->num),
> +				 GFP_KERNEL);
>  	if (!ljca_gpio)
>  		return -ENOMEM;
> 
> +	ljca_gpio->num = gpio_info->num;
>  	ljca_gpio->ljca = ljca;
> -	ljca_gpio->gpio_info = dev_get_platdata(&auxdev->dev);
> -	ljca_gpio->connect_mode = devm_kcalloc(&auxdev->dev,
> -					       ljca_gpio->gpio_info->num,
> -					       sizeof(*ljca_gpio->connect_mode),
> -					       GFP_KERNEL);
> -	if (!ljca_gpio->connect_mode)
> -		return -ENOMEM;
> +	ljca_gpio->gpio_info = gpio_info;
> 
>  	ret = devm_mutex_init(&auxdev->dev, &ljca_gpio->irq_lock);
>  	if (ret)

-- 
Regards,

Sakari Ailus

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCHv2] gpio: ljca: reduce struct allocation
  2026-03-10 22:20 [PATCHv2] gpio: ljca: reduce struct allocation Rosen Penev
  2026-03-11  7:49 ` Sakari Ailus
@ 2026-03-11 12:56 ` Linus Walleij
  2026-03-11 23:19   ` Rosen Penev
  1 sibling, 1 reply; 5+ messages in thread
From: Linus Walleij @ 2026-03-11 12:56 UTC (permalink / raw)
  To: Rosen Penev
  Cc: linux-gpio, Lixu Zhang, Sakari Ailus, Bartosz Golaszewski,
	Kees Cook, Gustavo A. R. Silva, open list,
	open list:KERNEL HARDENING (not covered by other areas):Keyword:b__counted_by(_le|_be)?b

On Tue, Mar 10, 2026 at 11:21 PM Rosen Penev <rosenp@gmail.com> wrote:

> +       size_t num;

What about naming it num_connect_modes?
(and unsigned in as Sakari says.)

Yours,
Linus Walleij

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCHv2] gpio: ljca: reduce struct allocation
  2026-03-11 12:56 ` Linus Walleij
@ 2026-03-11 23:19   ` Rosen Penev
  2026-03-12  8:16     ` Linus Walleij
  0 siblings, 1 reply; 5+ messages in thread
From: Rosen Penev @ 2026-03-11 23:19 UTC (permalink / raw)
  To: Linus Walleij
  Cc: linux-gpio, Lixu Zhang, Sakari Ailus, Bartosz Golaszewski,
	Kees Cook, Gustavo A. R. Silva, open list,
	open list:KERNEL HARDENING (not covered by other areas):Keyword:b__counted_by(_le|_be)?b

On Wed, Mar 11, 2026 at 5:57 AM Linus Walleij <linusw@kernel.org> wrote:
>
> On Tue, Mar 10, 2026 at 11:21 PM Rosen Penev <rosenp@gmail.com> wrote:
>
> > +       size_t num;
>
> What about naming it num_connect_modes?
> (and unsigned in as Sakari says.)
Too long IMO. Makes the most sense to match variable names.
>
> Yours,
> Linus Walleij

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCHv2] gpio: ljca: reduce struct allocation
  2026-03-11 23:19   ` Rosen Penev
@ 2026-03-12  8:16     ` Linus Walleij
  0 siblings, 0 replies; 5+ messages in thread
From: Linus Walleij @ 2026-03-12  8:16 UTC (permalink / raw)
  To: Rosen Penev
  Cc: linux-gpio, Lixu Zhang, Sakari Ailus, Bartosz Golaszewski,
	Kees Cook, Gustavo A. R. Silva, open list,
	open list:KERNEL HARDENING (not covered by other areas):Keyword:b__counted_by(_le|_be)?b

On Thu, Mar 12, 2026 at 12:19 AM Rosen Penev <rosenp@gmail.com> wrote:
> On Wed, Mar 11, 2026 at 5:57 AM Linus Walleij <linusw@kernel.org> wrote:
> > On Tue, Mar 10, 2026 at 11:21 PM Rosen Penev <rosenp@gmail.com> wrote:
> >
> > > +       size_t num;
> >
> > What about naming it num_connect_modes?
> > (and unsigned in as Sakari says.)
>
> Too long IMO. Makes the most sense to match variable names.

This is one of those times I would bring up Rusty Russell's
API item #6 "The name tells you how to use it." well,
does it? Number of what?

Yours,
Linus Walleij

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-03-12  8:16 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-10 22:20 [PATCHv2] gpio: ljca: reduce struct allocation Rosen Penev
2026-03-11  7:49 ` Sakari Ailus
2026-03-11 12:56 ` Linus Walleij
2026-03-11 23:19   ` Rosen Penev
2026-03-12  8:16     ` Linus Walleij

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox