linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Andy Shevchenko <andy.shevchenko@gmail.com>
To: Sean Nyekjaer <sean@geanix.com>
Cc: "Jean-Baptiste Maneyrol" <jean-baptiste.maneyrol@tdk.com>,
	"Jonathan Cameron" <jic23@kernel.org>,
	"David Lechner" <dlechner@baylibre.com>,
	"Nuno Sá" <nuno.sa@analog.com>,
	"Andy Shevchenko" <andy@kernel.org>,
	linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org,
	"Jean-Baptiste Maneyrol" <jmaneyrol@invensense.com>,
	"Jonathan Cameron" <Jonathan.Cameron@huawei.com>
Subject: Re: [PATCH v2 5/5] iio: imu: inv_icm42600: use guard() to release mutexes
Date: Fri, 8 Aug 2025 23:52:35 +0200	[thread overview]
Message-ID: <CAHp75VeA36CHbvmhHVesw3itRW0aGURTqCJPAtw_P=q12F_0Yw@mail.gmail.com> (raw)
In-Reply-To: <20250808-icm42pmreg-v2-5-a480279e7721@geanix.com>

On Fri, Aug 8, 2025 at 5:58 PM Sean Nyekjaer <sean@geanix.com> wrote:
>
> Replace explicit mutex_lock() and mutex_unlock() with the guard() macro
> for cleaner and safer mutex handling.

...

>         pm_runtime_get_sync(dev);
> -       mutex_lock(&st->lock);
> +       guard(mutex)(&st->lock);
>
>         ret = inv_icm42600_set_accel_conf(st, &conf, NULL);
>
> -       mutex_unlock(&st->lock);
>         pm_runtime_mark_last_busy(dev);
>         pm_runtime_put_autosuspend(dev);

This makes PM calls under the mutex. In some cases it may lead to deadlocks.
I think you wanted to use scoped_guard() here and in similar cases.

...

>         struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
>         int ret;
>
> -       mutex_lock(&st->lock);
> +       guard(mutex)(&st->lock);
>
>         st->fifo.watermark.accel = val;
>         ret = inv_icm42600_buffer_update_watermark(st);
>
> -       mutex_unlock(&st->lock);
> -
>         return ret;

Now remove ret and use return directly.

>  }

> -       mutex_lock(&st->lock);
> +       guard(mutex)(&st->lock);
>
>         ret = inv_icm42600_buffer_hwfifo_flush(st, count);
>         if (!ret)
>                 ret = st->fifo.nb.accel;
>
> -       mutex_unlock(&st->lock);
> -
>         return ret;

In the similar way as above.

ret = _flush();
if (ret)
  return ret;

return ...nb.accel;

...

>         struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);

>         int ret;

Now unneeded, just return directly.

> -       mutex_lock(&st->lock);
> +       guard(mutex)(&st->lock);
>
>         if (readval)
>                 ret = regmap_read(st->map, reg, readval);
>         else
>                 ret = regmap_write(st->map, reg, writeval);
>
> -       mutex_unlock(&st->lock);
> -
>         return ret;

...

>         int ret = 0;

Now unneeded assignment.

> -       mutex_lock(&st->lock);
> +       guard(mutex)(&st->lock);
>
>         st->suspended.gyro = st->conf.gyro.mode;
>         st->suspended.accel = st->conf.accel.mode;
>         st->suspended.temp = st->conf.temp_en;
> -       if (pm_runtime_suspended(dev))
> -               goto out_unlock;
> +       ret = pm_runtime_suspended(dev);
> +       if (ret)
> +               return ret;

...

>         /* disable vddio regulator if chip is sleeping */
>         if (!wakeup)
>                 regulator_disable(st->vddio_supply);
>
> -out_unlock:
> -       mutex_unlock(&st->lock);
>         return ret;

Now return 0 to make it clear that this is a success.

...

> @@ -881,10 +878,11 @@ static int inv_icm42600_resume(struct device *dev)
>         bool wakeup;
>         int ret = 0;

Assignment is useless now.

> -       mutex_lock(&st->lock);
> +       guard(mutex)(&st->lock);
>
> -       if (pm_runtime_suspended(dev))
> -               goto out_unlock;
> +       ret = pm_runtime_suspended(dev);
> +       if (ret)
> +               return ret;

...

> -out_unlock:
> -       mutex_unlock(&st->lock);
>         return ret;

  return 0;

?

...

>         regulator_disable(st->vddio_supply);
>
> -error_unlock:
> -       mutex_unlock(&st->lock);
>         return ret;

Ditto.

>  }

...

>         int ret;

Now useless variable.

>
> -       mutex_lock(&st->lock);
> +       guard(mutex)(&st->lock);
>
>         ret = inv_icm42600_enable_regulator_vddio(st);
>
> -       mutex_unlock(&st->lock);
>         return ret;
>  }

...

>         int ret;

Ditto.

> -       mutex_lock(&st->lock);
> +       guard(mutex)(&st->lock);
>
>         st->fifo.watermark.gyro = val;
>         ret = inv_icm42600_buffer_update_watermark(st);
>
> -       mutex_unlock(&st->lock);
> -
>         return ret;
>  }

...

> -       mutex_lock(&st->lock);
> +       guard(mutex)(&st->lock);
>
>         ret = inv_icm42600_buffer_hwfifo_flush(st, count);
>         if (!ret)
>                 ret = st->fifo.nb.gyro;

Invert conditional and return ret directly.

> -       mutex_unlock(&st->lock);
> -
>         return ret;

-- 
With Best Regards,
Andy Shevchenko

  reply	other threads:[~2025-08-08 21:53 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-08 15:57 [PATCH v2 0/5] iio: imu: inv_icm42600: pm_runtime fixes + various changes Sean Nyekjaer
2025-08-08 15:57 ` [PATCH v2 1/5] iio: imu: inv_icm42600: Simplify pm_runtime setup Sean Nyekjaer
2025-08-08 21:37   ` Andy Shevchenko
2025-08-09 18:06     ` Jonathan Cameron
2025-08-09 20:27       ` Andy Shevchenko
2025-08-11 14:21         ` Sean Nyekjaer
2025-08-11 14:24           ` Andy Shevchenko
2025-08-25 15:14           ` Sean Nyekjaer
2025-08-30 14:45             ` Jonathan Cameron
2025-08-08 15:57 ` [PATCH v2 2/5] iio: imu: inv_icm42600: Drop redundant pm_runtime reinitialization in resume Sean Nyekjaer
2025-08-08 15:57 ` [PATCH v2 3/5] iio: imu: inv_icm42600: Avoid configuring if already pm_runtime suspended Sean Nyekjaer
2025-08-08 15:57 ` [PATCH v2 4/5] iio: imu: inv_icm42600: Use devm_regulator_get_enable() for vdd regulator Sean Nyekjaer
2025-08-08 15:57 ` [PATCH v2 5/5] iio: imu: inv_icm42600: use guard() to release mutexes Sean Nyekjaer
2025-08-08 21:52   ` Andy Shevchenko [this message]
2025-08-11 11:47     ` Sean Nyekjaer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAHp75VeA36CHbvmhHVesw3itRW0aGURTqCJPAtw_P=q12F_0Yw@mail.gmail.com' \
    --to=andy.shevchenko@gmail.com \
    --cc=Jonathan.Cameron@huawei.com \
    --cc=andy@kernel.org \
    --cc=dlechner@baylibre.com \
    --cc=jean-baptiste.maneyrol@tdk.com \
    --cc=jic23@kernel.org \
    --cc=jmaneyrol@invensense.com \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nuno.sa@analog.com \
    --cc=sean@geanix.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).