linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] gpio: aggregator: Wrap access to gpiochip_fwd.tmp[]
@ 2021-10-04 12:45 Geert Uytterhoeven
  2021-10-04 13:22 ` Andy Shevchenko
  2021-10-13 14:20 ` Bartosz Golaszewski
  0 siblings, 2 replies; 4+ messages in thread
From: Geert Uytterhoeven @ 2021-10-04 12:45 UTC (permalink / raw)
  To: Linus Walleij, Bartosz Golaszewski, Andy Shevchenko
  Cc: linux-gpio, linux-kernel, Geert Uytterhoeven

The tmp[] member of the gpiochip_fwd structure is used to store both the
temporary values bitmap and the desc pointers for operations on multiple
GPIOs.  As both are arrays with sizes unknown at compile-time, accessing
them requires offset calculations, which are currently duplicated in
gpio_fwd_get_multiple() and gpio_fwd_set_multiple().

Introduce (a) accessors for both arrays and (b) a macro to calculate the
needed storage size.  This confines the layout of the tmp[] member into
a single spot, to ease maintenance.

Signed-off-by: Geert Uytterhoeven <geert+renesas@glider.be>
---
 drivers/gpio/gpio-aggregator.c | 25 +++++++++++--------------
 1 file changed, 11 insertions(+), 14 deletions(-)

diff --git a/drivers/gpio/gpio-aggregator.c b/drivers/gpio/gpio-aggregator.c
index 2ff867d2a3630d3b..869dc952cf45218b 100644
--- a/drivers/gpio/gpio-aggregator.c
+++ b/drivers/gpio/gpio-aggregator.c
@@ -247,6 +247,11 @@ struct gpiochip_fwd {
 	unsigned long tmp[];		/* values and descs for multiple ops */
 };
 
+#define fwd_tmp_values(fwd)	&(fwd)->tmp[0]
+#define fwd_tmp_descs(fwd)	(void *)&(fwd)->tmp[BITS_TO_LONGS((fwd)->chip.ngpio)]
+
+#define fwd_tmp_size(ngpios)	(BITS_TO_LONGS((ngpios)) + (ngpios))
+
 static int gpio_fwd_get_direction(struct gpio_chip *chip, unsigned int offset)
 {
 	struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
@@ -279,15 +284,11 @@ static int gpio_fwd_get(struct gpio_chip *chip, unsigned int offset)
 static int gpio_fwd_get_multiple(struct gpiochip_fwd *fwd, unsigned long *mask,
 				 unsigned long *bits)
 {
-	struct gpio_desc **descs;
-	unsigned long *values;
+	struct gpio_desc **descs = fwd_tmp_descs(fwd);
+	unsigned long *values = fwd_tmp_values(fwd);
 	unsigned int i, j = 0;
 	int error;
 
-	/* Both values bitmap and desc pointers are stored in tmp[] */
-	values = &fwd->tmp[0];
-	descs = (void *)&fwd->tmp[BITS_TO_LONGS(fwd->chip.ngpio)];
-
 	bitmap_clear(values, 0, fwd->chip.ngpio);
 	for_each_set_bit(i, mask, fwd->chip.ngpio)
 		descs[j++] = fwd->descs[i];
@@ -333,14 +334,10 @@ static void gpio_fwd_set(struct gpio_chip *chip, unsigned int offset, int value)
 static void gpio_fwd_set_multiple(struct gpiochip_fwd *fwd, unsigned long *mask,
 				  unsigned long *bits)
 {
-	struct gpio_desc **descs;
-	unsigned long *values;
+	struct gpio_desc **descs = fwd_tmp_descs(fwd);
+	unsigned long *values = fwd_tmp_values(fwd);
 	unsigned int i, j = 0;
 
-	/* Both values bitmap and desc pointers are stored in tmp[] */
-	values = &fwd->tmp[0];
-	descs = (void *)&fwd->tmp[BITS_TO_LONGS(fwd->chip.ngpio)];
-
 	for_each_set_bit(i, mask, fwd->chip.ngpio) {
 		__assign_bit(j, values, test_bit(i, bits));
 		descs[j++] = fwd->descs[i];
@@ -405,8 +402,8 @@ static struct gpiochip_fwd *gpiochip_fwd_create(struct device *dev,
 	unsigned int i;
 	int error;
 
-	fwd = devm_kzalloc(dev, struct_size(fwd, tmp,
-			   BITS_TO_LONGS(ngpios) + ngpios), GFP_KERNEL);
+	fwd = devm_kzalloc(dev, struct_size(fwd, tmp, fwd_tmp_size(ngpios)),
+			   GFP_KERNEL);
 	if (!fwd)
 		return ERR_PTR(-ENOMEM);
 
-- 
2.25.1


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

* Re: [PATCH] gpio: aggregator: Wrap access to gpiochip_fwd.tmp[]
  2021-10-04 12:45 [PATCH] gpio: aggregator: Wrap access to gpiochip_fwd.tmp[] Geert Uytterhoeven
@ 2021-10-04 13:22 ` Andy Shevchenko
  2021-10-04 13:38   ` Geert Uytterhoeven
  2021-10-13 14:20 ` Bartosz Golaszewski
  1 sibling, 1 reply; 4+ messages in thread
From: Andy Shevchenko @ 2021-10-04 13:22 UTC (permalink / raw)
  To: Geert Uytterhoeven
  Cc: Linus Walleij, Bartosz Golaszewski, Andy Shevchenko,
	open list:GPIO SUBSYSTEM, Linux Kernel Mailing List

On Mon, Oct 4, 2021 at 3:47 PM Geert Uytterhoeven
<geert+renesas@glider.be> wrote:
>
> The tmp[] member of the gpiochip_fwd structure is used to store both the
> temporary values bitmap and the desc pointers for operations on multiple
> GPIOs.  As both are arrays with sizes unknown at compile-time, accessing
> them requires offset calculations, which are currently duplicated in
> gpio_fwd_get_multiple() and gpio_fwd_set_multiple().
>
> Introduce (a) accessors for both arrays and (b) a macro to calculate the
> needed storage size.  This confines the layout of the tmp[] member into
> a single spot, to ease maintenance.

...

> +#define fwd_tmp_descs(fwd)     (void *)&(fwd)->tmp[BITS_TO_LONGS((fwd)->chip.ngpio)]
> +
> +#define fwd_tmp_size(ngpios)   (BITS_TO_LONGS((ngpios)) + (ngpios))

...

> -       fwd = devm_kzalloc(dev, struct_size(fwd, tmp,
> -                          BITS_TO_LONGS(ngpios) + ngpios), GFP_KERNEL);
> +       fwd = devm_kzalloc(dev, struct_size(fwd, tmp, fwd_tmp_size(ngpios)),
> +                          GFP_KERNEL);

Shouldn't we rather use devm_bitmap_zalloc() / bitmap_free()?

>         if (!fwd)
>                 return ERR_PTR(-ENOMEM);


-- 
With Best Regards,
Andy Shevchenko

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

* Re: [PATCH] gpio: aggregator: Wrap access to gpiochip_fwd.tmp[]
  2021-10-04 13:22 ` Andy Shevchenko
@ 2021-10-04 13:38   ` Geert Uytterhoeven
  0 siblings, 0 replies; 4+ messages in thread
From: Geert Uytterhoeven @ 2021-10-04 13:38 UTC (permalink / raw)
  To: Andy Shevchenko
  Cc: Linus Walleij, Bartosz Golaszewski, Andy Shevchenko,
	open list:GPIO SUBSYSTEM, Linux Kernel Mailing List

Hi Andy,

On Mon, Oct 4, 2021 at 3:23 PM Andy Shevchenko
<andy.shevchenko@gmail.com> wrote:
> On Mon, Oct 4, 2021 at 3:47 PM Geert Uytterhoeven
> <geert+renesas@glider.be> wrote:
> > The tmp[] member of the gpiochip_fwd structure is used to store both the
> > temporary values bitmap and the desc pointers for operations on multiple
> > GPIOs.  As both are arrays with sizes unknown at compile-time, accessing
> > them requires offset calculations, which are currently duplicated in
> > gpio_fwd_get_multiple() and gpio_fwd_set_multiple().
> >
> > Introduce (a) accessors for both arrays and (b) a macro to calculate the
> > needed storage size.  This confines the layout of the tmp[] member into
> > a single spot, to ease maintenance.
>
> ...
>
> > +#define fwd_tmp_descs(fwd)     (void *)&(fwd)->tmp[BITS_TO_LONGS((fwd)->chip.ngpio)]
> > +
> > +#define fwd_tmp_size(ngpios)   (BITS_TO_LONGS((ngpios)) + (ngpios))
>
> ...
>
> > -       fwd = devm_kzalloc(dev, struct_size(fwd, tmp,
> > -                          BITS_TO_LONGS(ngpios) + ngpios), GFP_KERNEL);
> > +       fwd = devm_kzalloc(dev, struct_size(fwd, tmp, fwd_tmp_size(ngpios)),
> > +                          GFP_KERNEL);
>
> Shouldn't we rather use devm_bitmap_zalloc() / bitmap_free()?

That's not sufficient: the bitmap is only one part. There are one
fixed-size and two variable-size objects to allocate.
Yes, they can be allocated separately, at the expense of more
allocations, and more data (pointers) to allocate to keep track of
all those objects.

>
> >         if (!fwd)
> >                 return ERR_PTR(-ENOMEM);

Gr{oetje,eeting}s,

                        Geert

-- 
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds

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

* Re: [PATCH] gpio: aggregator: Wrap access to gpiochip_fwd.tmp[]
  2021-10-04 12:45 [PATCH] gpio: aggregator: Wrap access to gpiochip_fwd.tmp[] Geert Uytterhoeven
  2021-10-04 13:22 ` Andy Shevchenko
@ 2021-10-13 14:20 ` Bartosz Golaszewski
  1 sibling, 0 replies; 4+ messages in thread
From: Bartosz Golaszewski @ 2021-10-13 14:20 UTC (permalink / raw)
  To: Geert Uytterhoeven; +Cc: Linus Walleij, Andy Shevchenko, linux-gpio, LKML

On Mon, Oct 4, 2021 at 2:45 PM Geert Uytterhoeven
<geert+renesas@glider.be> wrote:
>
> The tmp[] member of the gpiochip_fwd structure is used to store both the
> temporary values bitmap and the desc pointers for operations on multiple
> GPIOs.  As both are arrays with sizes unknown at compile-time, accessing
> them requires offset calculations, which are currently duplicated in
> gpio_fwd_get_multiple() and gpio_fwd_set_multiple().
>
> Introduce (a) accessors for both arrays and (b) a macro to calculate the
> needed storage size.  This confines the layout of the tmp[] member into
> a single spot, to ease maintenance.
>
> Signed-off-by: Geert Uytterhoeven <geert+renesas@glider.be>
> ---

Applied, thanks!

Bart

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

end of thread, other threads:[~2021-10-13 14:20 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-04 12:45 [PATCH] gpio: aggregator: Wrap access to gpiochip_fwd.tmp[] Geert Uytterhoeven
2021-10-04 13:22 ` Andy Shevchenko
2021-10-04 13:38   ` Geert Uytterhoeven
2021-10-13 14:20 ` Bartosz Golaszewski

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).