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

From: Andy Shevchenko
Date: Thu Aug 10 2023 - 10:42:45 EST


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 ?

...

> - 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?

...

> + scoped_guard(mutex, &chip->lock)
> + bitmap_replace(chip->value_map, chip->value_map, bits, mask,
> + gc->ngpio);

Perhaps with {} ?

...

> 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.

...

> 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(...);

?

--
With Best Regards,
Andy Shevchenko