From: Jonathan Cameron <jic23@kernel.org>
To: Maxwell Doose <m32285159@gmail.com>
Cc: songqiang1304521@gmail.com, dlechner@baylibre.com,
nuno.sa@analog.com, andy@kernel.org, linux-iio@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH v3] iio: magnetometer: rm3100: Modernize locking and refactor control flow
Date: Wed, 29 Apr 2026 11:41:54 +0100 [thread overview]
Message-ID: <20260429114154.290e588d@jic23-huawei> (raw)
In-Reply-To: <20260428222600.91322-1-m32285159@gmail.com>
On Tue, 28 Apr 2026 17:26:00 -0500
Maxwell Doose <m32285159@gmail.com> wrote:
> Replace mutex_lock() and mutex_unlock() calls in rm3100-core.c with
> the more modern guard(mutex)() family. This will help modernize the
> driver and bring it up-to-date with modern available macros/functions.
>
> While replacing mutex_lock() and mutex_unlock(), the critical sections
> of rm3100_read_mag() and rm3100_get_samp_freq() have been extended to
> include negligible operations for cleaner logic.
>
> Add new helper-wrapper function rm3100_guarded_regmap_bulk_read() to
> help keep rm3100_trigger_handler() switch-cases clean while maintaining
> mutex locking and avoiding re-entrancy risks from potential callbacks.
>
> While at it, remove redundant gotos where applicable, and use direct
> returns instead.
>
> Suggested-by: Jonathan Cameron <jic23@kernel.org>
> Signed-off-by: Maxwell Doose <m32285159@gmail.com>
Hi Maxwell.
The use of the helper worked out well but it also made me look
at some existing code and wonder why it is so complex!
> static irqreturn_t rm3100_trigger_handler(int irq, void *p)
> {
> struct iio_poll_func *pf = p;
> @@ -468,11 +481,10 @@ static irqreturn_t rm3100_trigger_handler(int irq, void *p)
> struct regmap *regmap = data->regmap;
> int ret, i, bit;
>
> - mutex_lock(&data->lock);
> switch (scan_mask) {
> default:
> for_each_set_bit(bit, &scan_mask, mask_len) {
> - ret = regmap_bulk_read(regmap, RM3100_REG_MX2 + 3 * bit,
> - data->buffer, 3);
> - if (ret < 0) {
> - mutex_unlock(&data->lock);
> + ret = rm3100_guarded_regmap_bulk_read(&data->lock, regmap,
> + RM3100_REG_MX2 + 3 * bit,
> + data->buffer, 3);
Hmm. Not one for this patch, but the only reason this doesn't
hammer the lock is the bitmap is one hot. Which is why the read is always to the same
location in the buffer. That could have been a lot easier to spot if instead
of a loop find_first_bit() was used.
I don't think we can ever get here with no bits set but maybe check that as well
(it's not supposed to be possible as doesn't make any sense to measure nothing ;)
> + if (ret < 0)
> goto done;
> - }
> }
> - mutex_unlock(&data->lock);
> }
> /*
> * Always using the same buffer so that we wouldn't need to set the
next prev parent reply other threads:[~2026-04-29 10:42 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-28 22:26 [PATCH v3] iio: magnetometer: rm3100: Modernize locking and refactor control flow Maxwell Doose
2026-04-29 9:12 ` Andy Shevchenko
2026-04-29 14:26 ` Maxwell Doose
2026-04-29 18:22 ` Andy Shevchenko
2026-04-29 18:58 ` Maxwell Doose
2026-04-29 10:41 ` Jonathan Cameron [this message]
2026-04-29 15:19 ` Maxwell Doose
2026-04-29 18:26 ` Andy Shevchenko
2026-04-29 19:00 ` Maxwell Doose
2026-04-29 19:12 ` Andy Shevchenko
2026-04-29 20:38 ` Maxwell Doose
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=20260429114154.290e588d@jic23-huawei \
--to=jic23@kernel.org \
--cc=andy@kernel.org \
--cc=dlechner@baylibre.com \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=m32285159@gmail.com \
--cc=nuno.sa@analog.com \
--cc=songqiang1304521@gmail.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