[PATCH v2] staging: iio: frequency: ad9832: use guard(mutex)

From: Melih Emik

Date: Sun Oct 18 2026 - 07:01:24 EST


In ad9832_write(), manual mutex handling via mutex_lock() and
mutex_unlock() necessitates error-exit trampolines (goto error_ret),
intermediate variables, and deferred returns. This pattern is prone
to unlock-omission bugs during refactoring and clutters the control
flow.

Modernize the locking by using guard(mutex) from <linux/cleanup.h>.
Scope-based resource management automatically releases the lock upon
exiting the lexical scope, which enables immediate error propagation
at call sites, removes the error_ret label, and simplifies the control
flow across all switch cases.

Signed-off-by: Melih Emik <melihemik@xxxxxxxxxxx>
---
v2:
- Drop "Tested:" meta commentary from the commit message.

Test notes:
- Compiled with W=1 on x86_64 with zero warnings.
- Verified with lockdep and debug mutexes enabled
(CONFIG_PROVE_LOCKING=y, CONFIG_DEBUG_MUTEXES=y).
- Boot-tested full bzImage in QEMU with initcall_debug, confirming
successful driver initialization (ad9832_driver_init returned 0)
and zero lockdep/kernel warnings.
- Disassembly inspected via objdump to ensure mutex_unlock on all
exit paths.
- Verified with scripts/checkpatch.pl (0 errors, 0 warnings).

drivers/staging/iio/frequency/ad9832.c | 39 ++++++++++++++++++--------
1 file changed, 27 insertions(+), 12 deletions(-)

diff --git a/drivers/staging/iio/frequency/ad9832.c b/drivers/staging/iio/frequency/ad9832.c
index a78249812b26..3ae5defec615 100644
--- a/drivers/staging/iio/frequency/ad9832.c
+++ b/drivers/staging/iio/frequency/ad9832.c
@@ -8,6 +8,7 @@
#include <linux/array_size.h>
#include <linux/bitfield.h>
#include <linux/bitops.h>
+#include <linux/cleanup.h>
#include <linux/clk.h>
#include <linux/dev_printk.h>
#include <linux/err.h>
@@ -182,19 +183,26 @@ static ssize_t ad9832_write(struct device *dev, struct device_attribute *attr,

ret = kstrtoul(buf, 10, &val);
if (ret)
- goto error_ret;
+ return ret;
+
+ guard(mutex)(&st->lock);

- mutex_lock(&st->lock);
switch ((u32)this_attr->address) {
case AD9832_FREQ0HM:
case AD9832_FREQ1HM:
ret = ad9832_write_frequency(st, this_attr->address, val);
+ if (ret)
+ return ret;
+
break;
case AD9832_PHASE0H:
case AD9832_PHASE1H:
case AD9832_PHASE2H:
case AD9832_PHASE3H:
ret = ad9832_write_phase(st, this_attr->address, val);
+ if (ret)
+ return ret;
+
break;
case AD9832_PINCTRL_EN:
st->ctrl_ss &= ~AD9832_SELSRC;
@@ -203,24 +211,27 @@ static ssize_t ad9832_write(struct device *dev, struct device_attribute *attr,
st->data = cpu_to_be16(FIELD_PREP(AD9832_CMD_MSK, AD9832_CMD_SYNCSELSRC) |
st->ctrl_ss);
ret = spi_sync(st->spi, &st->msg);
+ if (ret)
+ return ret;
+
break;
case AD9832_FREQ_SYM:
if (val == 1 || val == 0) {
st->ctrl_fp &= ~AD9832_FREQ;
st->ctrl_fp |= FIELD_PREP(AD9832_FREQ, val ? 1 : 0);
} else {
- ret = -EINVAL;
- break;
+ return -EINVAL;
}
st->data = cpu_to_be16(FIELD_PREP(AD9832_CMD_MSK, AD9832_CMD_FPSELECT) |
st->ctrl_fp);
ret = spi_sync(st->spi, &st->msg);
+ if (ret)
+ return ret;
+
break;
case AD9832_PHASE_SYM:
- if (val > 3) {
- ret = -EINVAL;
- break;
- }
+ if (val > 3)
+ return -EINVAL;

st->ctrl_fp &= ~AD9832_PHASE_MASK;
st->ctrl_fp |= FIELD_PREP(AD9832_PHASE_MASK, val);
@@ -228,6 +239,9 @@ static ssize_t ad9832_write(struct device *dev, struct device_attribute *attr,
st->data = cpu_to_be16(FIELD_PREP(AD9832_CMD_MSK, AD9832_CMD_FPSELECT) |
st->ctrl_fp);
ret = spi_sync(st->spi, &st->msg);
+ if (ret)
+ return ret;
+
break;
case AD9832_OUTPUT_EN:
if (val)
@@ -238,14 +252,15 @@ static ssize_t ad9832_write(struct device *dev, struct device_attribute *attr,
st->data = cpu_to_be16(FIELD_PREP(AD9832_CMD_MSK, AD9832_CMD_SLEEPRESCLR) |
st->ctrl_src);
ret = spi_sync(st->spi, &st->msg);
+ if (ret)
+ return ret;
+
break;
default:
- ret = -ENODEV;
+ return -ENODEV;
}
- mutex_unlock(&st->lock);

-error_ret:
- return ret ? ret : len;
+ return len;
}

/*
--
2.56.0