Re: [PATCH 2/2] gpio: sim: simplify code with cleanup helpers
From: Bartosz Golaszewski
Date: Thu Aug 10 2023 - 15:04:26 EST
On Thu, Aug 10, 2023 at 4:42 PM Andy Shevchenko
<andriy.shevchenko@xxxxxxxxxxxxxxx> wrote:
>
> On Wed, Aug 09, 2023 at 03:14:42PM +0200, Bartosz Golaszewski wrote:
> > From: Bartosz Golaszewski <bartosz.golaszewski@xxxxxxxxxx>
> >
> > Use macros defined in linux/cleanup.h to automate resource lifetime
> > control in the gpio-simulator.
>
> gpio-sim ?
>
Meh, if you insist...
> ...
>
> > - mutex_lock(&chip->lock);
> > + guard(mutex)(&chip->lock);
>
> I hoped to see somehing like
>
> guard_mutex(...);
>
> But looking into cleanup.h it seems to me that the lock itself on GPIO library
> can be defined with respective class, no?
>
Why though? This is perfectly clear and concise as it is. It's similar
to going bare mutex_lock() everywhere instead of wrapping it with
foo_lock() which requires you to go and check what you're locking.
> ...
>
> > + scoped_guard(mutex, &chip->lock)
> > + bitmap_replace(chip->value_map, chip->value_map, bits, mask,
> > + gc->ngpio);
>
> Perhaps with {} ?
>
This scoped_guard() thing is in essence a for loop, so I believe
kernel coding style applies and a single statement doesn't require a
{}.
> ...
>
> > int ret;
> >
> > - mutex_lock(&dev->lock);
> > + guard(mutex)(&dev->lock);
> > +
> > pdev = dev->pdev;
> > if (pdev)
> > ret = sprintf(page, "%s\n", dev_name(&pdev->dev));
> > else
> > ret = sprintf(page, "gpio-sim.%d\n", dev->id);
> > - mutex_unlock(&dev->lock);
> >
> > return ret;
>
> Now can be
>
> if (...)
> return ...
> else // if you wish (not needed)
> return ...
>
> ...
>
> > int ret;
> >
> > - mutex_lock(&dev->lock);
> > + guard(mutex)(&dev->lock);
> > +
> > if (gpio_sim_device_is_live_unlocked(dev))
> > ret = device_for_each_child(&dev->pdev->dev, &ctx,
> > gpio_sim_emit_chip_name);
> > else
> > ret = sprintf(page, "none\n");
> > - mutex_unlock(&dev->lock);
> >
> > return ret;
>
> As per above. And may be other functions as well.
>
Sure.
> ...
>
> > int ret;
> >
> > - mutex_lock(&dev->lock);
> > - ret = sprintf(page, "%s\n", line->name ?: "");
> > - mutex_unlock(&dev->lock);
> > + scoped_guard(mutex, &dev->lock)
> > + ret = sprintf(page, "%s\n", line->name ?: "");
> >
> > return ret;
>
> Why not
>
> guard(...);
> return sprintf(...);
>
> ?
I'll change that too.
Bart
>
> --
> With Best Regards,
> Andy Shevchenko
>
>