Re: [PATCH v3] gpio: sim: simplify code with cleanup helpers

From: Bartosz Golaszewski
Date: Tue Aug 15 2023 - 15:11:29 EST


On Tue, Aug 15, 2023 at 12:31 PM Andy Shevchenko
<andriy.shevchenko@xxxxxxxxxxxxxxx> wrote:
>
> On Sat, Aug 12, 2023 at 08:36:35PM +0200, Bartosz Golaszewski wrote:
> > From: Bartosz Golaszewski <bartosz.golaszewski@xxxxxxxxxx>
> >
> > Use macros defined in linux/cleanup.h to automate resource lifetime
> > control in gpio-sim.
>
> ...
>
> > static void gpio_sim_set(struct gpio_chip *gc, unsigned int offset, int value)
> > {
> > struct gpio_sim_chip *chip = gpiochip_get_data(gc);
> >
> > - mutex_lock(&chip->lock);
> > - __assign_bit(offset, chip->value_map, value);
> > - mutex_unlock(&chip->lock);
> > + scoped_guard(mutex, &chip->lock)
> > + __assign_bit(offset, chip->value_map, value);
>
> But this can also be guarded.
>
> guard(mutex)(&chip->lock);
>
> __assign_bit(offset, chip->value_map, value);
>

Come on, this is total bikeshedding! I could produce ten arguments in
favor of the scoped variant.

Linus acked even the previous version and Peter says it looks right. I
will queue it unless some *real* issues come up.

> > }
>
> ...
>
> > {
> > struct gpio_sim_chip *chip = gpiochip_get_data(gc);
> >
> > - mutex_lock(&chip->lock);
> > - bitmap_replace(chip->value_map, chip->value_map, bits, mask, gc->ngpio);
> > - mutex_unlock(&chip->lock);
> > + scoped_guard(mutex, &chip->lock)
> > + bitmap_replace(chip->value_map, chip->value_map, bits, mask,
> > + gc->ngpio);
>
> Ditto.
>
> guard(mutex)(&chip->lock);
>
> bitmap_replace(chip->value_map, chip->value_map, bits, mask, gc->ngpio);
>
> (exactly 80 for the sectants of 80 characters :).
>
> > }
>
> ...
>
> > {
> > struct gpio_sim_chip *chip = gpiochip_get_data(gc);
> >
> > - mutex_lock(&chip->lock);
> > - __assign_bit(offset, chip->value_map, !!test_bit(offset, chip->pull_map));
> > - mutex_unlock(&chip->lock);
> > + scoped_guard(mutex, &chip->lock)
> > + __assign_bit(offset, chip->value_map,
> > + !!test_bit(offset, chip->pull_map));
>
> Ditto.
>
> guard(mutex)(&chip->lock);
>
> __assign_bit(offset, chip->value_map, test_bit(offset, chip->pull_map));
>
> (in this form fanatics of 80 can sleep well :-)
>
> Note that !! is redundant as test_bit() family of functions were fixed to
> return boolean.

Ha! TIL... I'll change it in a separate patch.

Bart

>
> > }
>
> --
> With Best Regards,
> Andy Shevchenko
>
>