From: Jonathan Cameron <jic23@kernel.org>
To: Jiasheng Jiang <jiashengjiangcool@gmail.com>
Cc: lars@metafoo.de, mcoquelin.stm32@gmail.com,
alexandre.torgue@foss.st.com, u.kleine-koenig@baylibre.com,
tgamblin@baylibre.com, fabrice.gasnier@st.com,
benjamin.gaignard@linaro.org, lee@kernel.org,
linux-iio@vger.kernel.org,
linux-stm32@st-md-mailman.stormreply.com,
linux-arm-kernel@lists.infradead.org,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2] iio: trigger: stm32-timer-trigger: Add check for clk_enable()
Date: Sat, 23 Nov 2024 15:02:47 +0000 [thread overview]
Message-ID: <20241123150247.718ee116@jic23-huawei> (raw)
In-Reply-To: <CANeGvZVxte61_FgrrYEt_oTRXXzCESrvq4q90xSWz4JqpMAtYg@mail.gmail.com>
On Mon, 11 Nov 2024 14:15:43 -0500
Jiasheng Jiang <jiashengjiangcool@gmail.com> wrote:
> On Sat, Nov 9, 2024 at 8:38 AM Jonathan Cameron <jic23@kernel.org> wrote:
> >
> > On Fri, 8 Nov 2024 20:09:00 +0000
> > Jiasheng Jiang <jiashengjiangcool@gmail.com> wrote:
> >
> > > Add check for the return value of clk_enable() in order to catch the
> > > potential exception.
> > >
> > > Signed-off-by: Jiasheng Jiang <jiashengjiangcool@gmail.com>
> > Hi,
> >
> > In principle this is fine, but I'd rather we made use of guard()
> > / scoped_guard() rather than adding the explicit unlocks.
> >
> > If you do that as a precursor patch in appropriate places
> > in the driver then this will be a little cleaner.
> >
> > Note I'll not be taking this until next cycle now anyway.
> >
> > Jonathan
> >
> > > ---
> > > Changelog:
> > >
> > > v1 -> v2:
> > >
> > > 1. Remove unsuitable dev_err_probe().
> > > ---
> > > drivers/iio/trigger/stm32-timer-trigger.c | 32 ++++++++++++++++++-----
> > > 1 file changed, 25 insertions(+), 7 deletions(-)
> > >
> > > diff --git a/drivers/iio/trigger/stm32-timer-trigger.c b/drivers/iio/trigger/stm32-timer-trigger.c
> > > index 0684329956d9..e1e077122f73 100644
> > > --- a/drivers/iio/trigger/stm32-timer-trigger.c
> > > +++ b/drivers/iio/trigger/stm32-timer-trigger.c
> > > @@ -119,7 +119,7 @@ static int stm32_timer_start(struct stm32_timer_trigger *priv,
> > > unsigned int frequency)
> > > {
> > > unsigned long long prd, div;
> > > - int prescaler = 0;
> > > + int prescaler = 0, ret;
> > > u32 ccer;
> > >
> > > /* Period and prescaler values depends of clock rate */
> > > @@ -153,7 +153,11 @@ static int stm32_timer_start(struct stm32_timer_trigger *priv,
> > > mutex_lock(&priv->lock);
> > > if (!priv->enabled) {
> > > priv->enabled = true;
> > > - clk_enable(priv->clk);
> > > + ret = clk_enable(priv->clk);
> > > + if (ret) {
> > > + mutex_unlock(&priv->lock);
> >
> > as below guard() for when the mutex is locked is cleaner.
> >
> > > + return ret;
> > > + }
> > > }
> > >
> > > regmap_write(priv->regmap, TIM_PSC, prescaler);
> > > @@ -307,7 +311,7 @@ static ssize_t stm32_tt_store_master_mode(struct device *dev,
> > > struct stm32_timer_trigger *priv = dev_get_drvdata(dev);
> > > struct iio_trigger *trig = to_iio_trigger(dev);
> > > u32 mask, shift, master_mode_max;
> > > - int i;
> > > + int i, ret;
> > >
> > > if (stm32_timer_is_trgo2_name(trig->name)) {
> > > mask = TIM_CR2_MMS2;
> > > @@ -326,7 +330,11 @@ static ssize_t stm32_tt_store_master_mode(struct device *dev,
> > > if (!priv->enabled) {
> > > /* Clock should be enabled first */
> > > priv->enabled = true;
> > > - clk_enable(priv->clk);
> > > + ret = clk_enable(priv->clk);
> > > + if (ret) {
> > > + mutex_unlock(&priv->lock);
> > As below. Prefer use of guard() so we don't have to handle the unlock manually.
> > > + return ret;
> > > + }
> > > }
> > > regmap_update_bits(priv->regmap, TIM_CR2, mask,
> > > i << shift);
> > > @@ -482,6 +490,7 @@ static int stm32_counter_write_raw(struct iio_dev *indio_dev,
> > > int val, int val2, long mask)
> > > {
> > > struct stm32_timer_trigger *priv = iio_priv(indio_dev);
> > > + int ret;
> > >
> > > switch (mask) {
> > > case IIO_CHAN_INFO_RAW:
> > > @@ -496,7 +505,11 @@ static int stm32_counter_write_raw(struct iio_dev *indio_dev,
> > > if (val) {
> > > if (!priv->enabled) {
> > > priv->enabled = true;
> > > - clk_enable(priv->clk);
> > > + ret = clk_enable(priv->clk);
> > > + if (ret) {
> > > + mutex_unlock(&priv->lock);
> > Add include of cleanup.h and swithch the place where the mutex is locked to
> > guard(mutex)(&priv->lock);
> > then remember to drop the explicit unlocks.
>
> I found that cleanup.h is already included.
>
> Moreover, since labels cannot be followed by declarations,
> I encountered a compilation error when using guard().
> Therefore, I switched to using scoped_guard().
Sometimes it is better to just add scope for blocks in switch statement
with
case XXX: {
guard()
}
because then we don't get the extra indent a scoped_guard() requires.
I'll take a look at the result in this case though!
Jonathan
> >
> > > + return ret;
> > > + }
> > > }
> > > regmap_set_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN);
> > > } else {
> > > @@ -601,7 +614,7 @@ static int stm32_set_enable_mode(struct iio_dev *indio_dev,
> > > unsigned int mode)
> > > {
> > > struct stm32_timer_trigger *priv = iio_priv(indio_dev);
> > > - int sms = stm32_enable_mode2sms(mode);
> > > + int sms = stm32_enable_mode2sms(mode), ret;
> > >
> > > if (sms < 0)
> > > return sms;
> > > @@ -611,7 +624,12 @@ static int stm32_set_enable_mode(struct iio_dev *indio_dev,
> > > */
> > > mutex_lock(&priv->lock);
> >
> > Perhaps scoped_guard() is appropriate here.
> >
> > > if (sms == 6 && !priv->enabled) {
> > > - clk_enable(priv->clk);
> > > + ret = clk_enable(priv->clk);
> > > + if (ret) {
> > > + mutex_unlock(&priv->lock);
> > > + return ret;
> > > + }
> > > +
> > > priv->enabled = true;
> > > }
> > > mutex_unlock(&priv->lock);
> >
>
> Thanks, I will submit a v2 to simplify code with cleanup helpers.
>
> -Jiasheng
prev parent reply other threads:[~2024-11-23 15:04 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-11-08 20:09 [PATCH v2] iio: trigger: stm32-timer-trigger: Add check for clk_enable() Jiasheng Jiang
2024-11-09 13:38 ` Jonathan Cameron
2024-11-11 19:15 ` Jiasheng Jiang
2024-11-23 15:02 ` Jonathan Cameron [this message]
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=20241123150247.718ee116@jic23-huawei \
--to=jic23@kernel.org \
--cc=alexandre.torgue@foss.st.com \
--cc=benjamin.gaignard@linaro.org \
--cc=fabrice.gasnier@st.com \
--cc=jiashengjiangcool@gmail.com \
--cc=lars@metafoo.de \
--cc=lee@kernel.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-stm32@st-md-mailman.stormreply.com \
--cc=mcoquelin.stm32@gmail.com \
--cc=tgamblin@baylibre.com \
--cc=u.kleine-koenig@baylibre.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