* [PATCH v2] iio: gyro: adxrs290: Use guard(mutex) in lieu of manual lock+unlock
@ 2026-04-23 22:37 Guilherme Dias
2026-04-24 9:24 ` Andy Shevchenko
0 siblings, 1 reply; 5+ messages in thread
From: Guilherme Dias @ 2026-04-23 22:37 UTC (permalink / raw)
To: nish.malpani25, lars, Michael.Hennerich, jic23, dlechner, nuno.sa,
andy
Cc: Guilherme Dias, João Paulo Menezes Linaris, linux-iio
Use guard(mutex) to automatically release the lock on scope exit,
simplifying the error handling path and removing the need for
explicit unlock and goto-based cleanup.
Signed-off-by: Guilherme Dias <guilhermeabreu200105@usp.br>
Co-developed-by: João Paulo Menezes Linaris <jplinaris@usp.br>
Signed-off-by: João Paulo Menezes Linaris <jplinaris@usp.br>
---
v2:
- added returns mistakenly removed
- remove unnecessary ret in adxrs290_get_rate_data(),
adxrs290_get_temp_data() and adxrs290_get_3db_freq()
- invert condition in adxrs290_trigger_handler() to eliminate goto
drivers/iio/gyro/adxrs290.c | 65 ++++++++++++-------------------------
1 file changed, 20 insertions(+), 45 deletions(-)
diff --git a/drivers/iio/gyro/adxrs290.c b/drivers/iio/gyro/adxrs290.c
index 3efe385ebedc..25b6dd6e986d 100644
--- a/drivers/iio/gyro/adxrs290.c
+++ b/drivers/iio/gyro/adxrs290.c
@@ -115,65 +115,47 @@ static const int adxrs290_hpf_3db_freq_hz_table[][2] = {
static int adxrs290_get_rate_data(struct iio_dev *indio_dev, const u8 cmd, int *val)
{
struct adxrs290_state *st = iio_priv(indio_dev);
- int ret = 0;
int temp;
- mutex_lock(&st->lock);
+ guard(mutex)(&st->lock);
temp = spi_w8r16(st->spi, cmd);
- if (temp < 0) {
- ret = temp;
- goto err_unlock;
- }
+ if (temp < 0)
+ return temp;
*val = sign_extend32(temp, 15);
-
-err_unlock:
- mutex_unlock(&st->lock);
- return ret;
+ return 0;
}
static int adxrs290_get_temp_data(struct iio_dev *indio_dev, int *val)
{
const u8 cmd = ADXRS290_READ_REG(ADXRS290_REG_TEMP0);
struct adxrs290_state *st = iio_priv(indio_dev);
- int ret = 0;
int temp;
- mutex_lock(&st->lock);
+ guard(mutex)(&st->lock);
temp = spi_w8r16(st->spi, cmd);
- if (temp < 0) {
- ret = temp;
- goto err_unlock;
- }
+ if (temp < 0)
+ return temp;
/* extract lower 12 bits temperature reading */
*val = sign_extend32(temp, 11);
-
-err_unlock:
- mutex_unlock(&st->lock);
- return ret;
+ return 0;
}
static int adxrs290_get_3db_freq(struct iio_dev *indio_dev, u8 *val, u8 *val2)
{
const u8 cmd = ADXRS290_READ_REG(ADXRS290_REG_FILTER);
struct adxrs290_state *st = iio_priv(indio_dev);
- int ret = 0;
short temp;
- mutex_lock(&st->lock);
+ guard(mutex)(&st->lock);
temp = spi_w8r8(st->spi, cmd);
- if (temp < 0) {
- ret = temp;
- goto err_unlock;
- }
+ if (temp < 0)
+ return temp;
*val = FIELD_GET(ADXRS290_LPF_MASK, temp);
*val2 = FIELD_GET(ADXRS290_HPF_MASK, temp);
-
-err_unlock:
- mutex_unlock(&st->lock);
- return ret;
+ return 0;
}
static int adxrs290_spi_write_reg(struct spi_device *spi, const u8 reg,
@@ -220,11 +202,11 @@ static int adxrs290_set_mode(struct iio_dev *indio_dev, enum adxrs290_mode mode)
if (st->mode == mode)
return 0;
- mutex_lock(&st->lock);
+ guard(mutex)(&st->lock);
ret = spi_w8r8(st->spi, ADXRS290_READ_REG(ADXRS290_REG_POWER_CTL));
if (ret < 0)
- goto out_unlock;
+ return ret;
val = ret;
@@ -237,20 +219,17 @@ static int adxrs290_set_mode(struct iio_dev *indio_dev, enum adxrs290_mode mode)
break;
default:
ret = -EINVAL;
- goto out_unlock;
+ return ret;
}
ret = adxrs290_spi_write_reg(st->spi, ADXRS290_REG_POWER_CTL, val);
if (ret < 0) {
dev_err(&st->spi->dev, "unable to set mode: %d\n", ret);
- goto out_unlock;
+ return ret;
}
/* update cached mode */
st->mode = mode;
-
-out_unlock:
- mutex_unlock(&st->lock);
return ret;
}
@@ -506,21 +485,17 @@ static irqreturn_t adxrs290_trigger_handler(int irq, void *p)
u8 tx = ADXRS290_READ_REG(ADXRS290_REG_DATAX0);
int ret;
- mutex_lock(&st->lock);
+ scoped_guard(mutex, &st->lock){
/* exercise a bulk data capture starting from reg DATAX0... */
ret = spi_write_then_read(st->spi, &tx, sizeof(tx), st->buffer.channels,
sizeof(st->buffer.channels));
- if (ret < 0)
- goto out_unlock_notify;
-
- iio_push_to_buffers_with_timestamp(indio_dev, &st->buffer,
+ if (ret >= 0)
+ iio_push_to_buffers_with_timestamp(indio_dev, &st->buffer,
pf->timestamp);
+ }
-out_unlock_notify:
- mutex_unlock(&st->lock);
iio_trigger_notify_done(indio_dev->trig);
-
return IRQ_HANDLED;
}
--
2.43.0
^ permalink raw reply related [flat|nested] 5+ messages in thread* Re: [PATCH v2] iio: gyro: adxrs290: Use guard(mutex) in lieu of manual lock+unlock
2026-04-23 22:37 [PATCH v2] iio: gyro: adxrs290: Use guard(mutex) in lieu of manual lock+unlock Guilherme Dias
@ 2026-04-24 9:24 ` Andy Shevchenko
2026-04-24 10:39 ` Jonathan Cameron
0 siblings, 1 reply; 5+ messages in thread
From: Andy Shevchenko @ 2026-04-24 9:24 UTC (permalink / raw)
To: Guilherme Dias
Cc: nish.malpani25, lars, Michael.Hennerich, jic23, dlechner, nuno.sa,
andy, João Paulo Menezes Linaris, linux-iio
On Thu, Apr 23, 2026 at 07:37:51PM -0300, Guilherme Dias wrote:
> Use guard(mutex) to automatically release the lock on scope exit,
> simplifying the error handling path and removing the need for
> explicit unlock and goto-based cleanup.
...
> static int adxrs290_get_rate_data(struct iio_dev *indio_dev, const u8 cmd, int *val)
> {
> struct adxrs290_state *st = iio_priv(indio_dev);
> - int ret = 0;
> int temp;
>
> - mutex_lock(&st->lock);
> + guard(mutex)(&st->lock);
+ blank line.
We usually consider guard()() as not semantically linked to any code above
or below
> temp = spi_w8r16(st->spi, cmd);
> - if (temp < 0) {
> - ret = temp;
> - goto err_unlock;
> - }
> + if (temp < 0)
> + return temp;
>
> *val = sign_extend32(temp, 15);
> -
> -err_unlock:
> - mutex_unlock(&st->lock);
> - return ret;
> + return 0;
> }
Ditto for the similar cases below.
...
> static int adxrs290_set_mode(struct iio_dev *indio_dev, enum adxrs290_mode mode)
> default:
> ret = -EINVAL;
> - goto out_unlock;
> + return ret;
Just return directly the given error code.
...
> static irqreturn_t adxrs290_trigger_handler(int irq, void *p)
> - mutex_lock(&st->lock);
> + scoped_guard(mutex, &st->lock){
No. Besides wrong style of this line, this has a very broken indentation now.
> /* exercise a bulk data capture starting from reg DATAX0... */
> ret = spi_write_then_read(st->spi, &tx, sizeof(tx), st->buffer.channels,
> sizeof(st->buffer.channels));
> - if (ret < 0)
> - goto out_unlock_notify;
> -
> - iio_push_to_buffers_with_timestamp(indio_dev, &st->buffer,
> + if (ret >= 0)
> + iio_push_to_buffers_with_timestamp(indio_dev, &st->buffer,
> pf->timestamp);
> + }
>
> -out_unlock_notify:
> - mutex_unlock(&st->lock);
> iio_trigger_notify_done(indio_dev->trig);
> -
> return IRQ_HANDLED;
Stray change.
> }
--
With Best Regards,
Andy Shevchenko
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: [PATCH v2] iio: gyro: adxrs290: Use guard(mutex) in lieu of manual lock+unlock
2026-04-24 9:24 ` Andy Shevchenko
@ 2026-04-24 10:39 ` Jonathan Cameron
2026-04-27 22:04 ` Guilherme Dias
0 siblings, 1 reply; 5+ messages in thread
From: Jonathan Cameron @ 2026-04-24 10:39 UTC (permalink / raw)
To: Andy Shevchenko
Cc: Guilherme Dias, nish.malpani25, lars, Michael.Hennerich, dlechner,
nuno.sa, andy, João Paulo Menezes Linaris, linux-iio
On Fri, 24 Apr 2026 12:24:07 +0300
Andy Shevchenko <andriy.shevchenko@intel.com> wrote:
> On Thu, Apr 23, 2026 at 07:37:51PM -0300, Guilherme Dias wrote:
> > Use guard(mutex) to automatically release the lock on scope exit,
> > simplifying the error handling path and removing the need for
> > explicit unlock and goto-based cleanup.
>
> ...
>
> > static int adxrs290_get_rate_data(struct iio_dev *indio_dev, const u8 cmd, int *val)
> > {
> > struct adxrs290_state *st = iio_priv(indio_dev);
> > - int ret = 0;
> > int temp;
> >
> > - mutex_lock(&st->lock);
> > + guard(mutex)(&st->lock);
>
> + blank line.
>
> We usually consider guard()() as not semantically linked to any code above
> or below
>
> > temp = spi_w8r16(st->spi, cmd);
> > - if (temp < 0) {
> > - ret = temp;
> > - goto err_unlock;
> > - }
> > + if (temp < 0)
> > + return temp;
> >
> > *val = sign_extend32(temp, 15);
> > -
> > -err_unlock:
> > - mutex_unlock(&st->lock);
> > - return ret;
> > + return 0;
> > }
>
> Ditto for the similar cases below.
>
> ...
>
> > static int adxrs290_set_mode(struct iio_dev *indio_dev, enum adxrs290_mode mode)
>
> > default:
> > ret = -EINVAL;
> > - goto out_unlock;
> > + return ret;
>
> Just return directly the given error code.
>
> ...
>
> > static irqreturn_t adxrs290_trigger_handler(int irq, void *p)
>
> > - mutex_lock(&st->lock);
> > + scoped_guard(mutex, &st->lock){
>
> No. Besides wrong style of this line, this has a very broken indentation now.
>
> > /* exercise a bulk data capture starting from reg DATAX0... */
> > ret = spi_write_then_read(st->spi, &tx, sizeof(tx), st->buffer.channels,
> > sizeof(st->buffer.channels));
> > - if (ret < 0)
> > - goto out_unlock_notify;
> > -
> > - iio_push_to_buffers_with_timestamp(indio_dev, &st->buffer,
> > + if (ret >= 0)
No to this as well. Keep the error out of line. Which makes the use
of guard() here tricky. You could do a do { guard(); } while(0) loop so that
you can use break with out it being odd looking.
(I don't like breaks out of scoped_guard() because it's non obvious scoped
guard is actually a loop.
> > + iio_push_to_buffers_with_timestamp(indio_dev, &st->buffer,
> > pf->timestamp);
> > + }
> >
> > -out_unlock_notify:
> > - mutex_unlock(&st->lock);
>
> > iio_trigger_notify_done(indio_dev->trig);
> > -
> > return IRQ_HANDLED;
>
> Stray change.
>
> > }
>
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: [PATCH v2] iio: gyro: adxrs290: Use guard(mutex) in lieu of manual lock+unlock
2026-04-24 10:39 ` Jonathan Cameron
@ 2026-04-27 22:04 ` Guilherme Dias
2026-04-28 8:01 ` Andy Shevchenko
0 siblings, 1 reply; 5+ messages in thread
From: Guilherme Dias @ 2026-04-27 22:04 UTC (permalink / raw)
To: Jonathan Cameron
Cc: Andy Shevchenko, nish.malpani25, lars, Michael.Hennerich,
dlechner, nuno.sa, andy, João Paulo Menezes Linaris,
linux-iio
Em sex., 24 de abr. de 2026 às 07:39, Jonathan Cameron
<jic23@kernel.org> escreveu:
>
> On Fri, 24 Apr 2026 12:24:07 +0300
> Andy Shevchenko <andriy.shevchenko@intel.com> wrote:
>
> > On Thu, Apr 23, 2026 at 07:37:51PM -0300, Guilherme Dias wrote:
> > > Use guard(mutex) to automatically release the lock on scope exit,
> > > simplifying the error handling path and removing the need for
> > > explicit unlock and goto-based cleanup.
...
> > > /* exercise a bulk data capture starting from reg DATAX0... */
> > > ret = spi_write_then_read(st->spi, &tx, sizeof(tx), st->buffer.channels,
> > > sizeof(st->buffer.channels));
> > > - if (ret < 0)
> > > - goto out_unlock_notify;
> > > -
> > > - iio_push_to_buffers_with_timestamp(indio_dev, &st->buffer,
> > > + if (ret >= 0)
> No to this as well. Keep the error out of line. Which makes the use
> of guard() here tricky. You could do a do { guard(); } while(0) loop so that
> you can use break with out it being odd looking.
>
> (I don't like breaks out of scoped_guard() because it's non obvious scoped
> guard is actually a loop.
Thanks for the suggestion — just to confirm I understood correctly, do
you mean something like this?
...
do {
guard(mutex)(&st->lock);
/* exercise a bulk data capture starting from reg DATAX0... */
ret = spi_write_then_read(st->spi, &tx, sizeof(tx), st->buffer.channels,
sizeof(st->buffer.channels));
if (ret < 0)
break;
iio_push_to_buffers_with_timestamp(indio_dev, &st->buffer,
pf->timestamp);
} while (0);
iio_trigger_notify_done(indio_dev->trig);
return IRQ_HANDLED;
}
Keeping the error out of line again.
--
Kind regards,
Guilherme Dias
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: [PATCH v2] iio: gyro: adxrs290: Use guard(mutex) in lieu of manual lock+unlock
2026-04-27 22:04 ` Guilherme Dias
@ 2026-04-28 8:01 ` Andy Shevchenko
0 siblings, 0 replies; 5+ messages in thread
From: Andy Shevchenko @ 2026-04-28 8:01 UTC (permalink / raw)
To: Guilherme Dias
Cc: Jonathan Cameron, nish.malpani25, lars, Michael.Hennerich,
dlechner, nuno.sa, andy, João Paulo Menezes Linaris,
linux-iio
On Mon, Apr 27, 2026 at 07:04:23PM -0300, Guilherme Dias wrote:
> Em sex., 24 de abr. de 2026 às 07:39, Jonathan Cameron
> <jic23@kernel.org> escreveu:
> > On Fri, 24 Apr 2026 12:24:07 +0300
> > Andy Shevchenko <andriy.shevchenko@intel.com> wrote:
> > > On Thu, Apr 23, 2026 at 07:37:51PM -0300, Guilherme Dias wrote:
...
> > > > /* exercise a bulk data capture starting from reg DATAX0... */
> > > > ret = spi_write_then_read(st->spi, &tx, sizeof(tx), st->buffer.channels,
> > > > sizeof(st->buffer.channels));
> > > > - if (ret < 0)
> > > > - goto out_unlock_notify;
> > > > + if (ret >= 0)
> > No to this as well. Keep the error out of line. Which makes the use
> > of guard() here tricky. You could do a do { guard(); } while(0) loop so that
> > you can use break with out it being odd looking.
> >
> > (I don't like breaks out of scoped_guard() because it's non obvious scoped
> > guard is actually a loop.
>
> Thanks for the suggestion — just to confirm I understood correctly, do
> you mean something like this?
Yes, with proper indentation made.
--
With Best Regards,
Andy Shevchenko
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-04-28 8:01 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-23 22:37 [PATCH v2] iio: gyro: adxrs290: Use guard(mutex) in lieu of manual lock+unlock Guilherme Dias
2026-04-24 9:24 ` Andy Shevchenko
2026-04-24 10:39 ` Jonathan Cameron
2026-04-27 22:04 ` Guilherme Dias
2026-04-28 8:01 ` Andy Shevchenko
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).