iio: resolver: ad2s1210: split trigger handler body into a helper
Subsystems (2)
PATCH 1/1 iio: resolver: ad2s1210: split trigger handler body into a helper
ad2s1210_trigger_handler() takes st->lock via guard(mutex), then runs the per-channel read/event/push sequence, and finally calls iio_trigger_notify_done() before returning IRQ_HANDLED. Because the guard releases at scope exit, iio_trigger_notify_done() runs with st->lock still held. The previous "goto error_ret" structure also hides that the locked region extends all the way to notify_done.
The device does not register any triggers itself, so this is not a deadlock source today, but holding a device mutex across the trigger notification path is fragile if a trigger ever gets attached elsewhere.
Lift the body of the handler into a helper, ad2s1210_collect_and_push(), that owns the guard and returns 0 / negative errno via direct returns instead of "goto error_ret". The irq handler then becomes a short wrapper that calls the helper, calls iio_trigger_notify_done() with no state lock held, and returns IRQ_HANDLED.
No change in behaviour for the read paths: every error path still skips the event/buffer push, and iio_trigger_notify_done() is still called on every IRQ regardless of result. The fault-register read no longer leaks a negative errno into irqreturn_t either, since the caller never propagates the helper's return value.
--- drivers/iio/resolver/ad2s1210.c | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-)
drivers/iio/resolver/ad2s1210.c +20 -13
@@ -1276,10 +1276,8 @@ static int ad2s1210_debugfs_reg_access(struct iio_dev *indio_dev,return regmap_write (st->regmap, reg, writeval); } -static irqreturn_t ad2s1210_trigger_handler (int irq,void *p) +static int ad2s1210_collect_and_push (struct iio_dev *indio_dev,s64 timestamp) { -struct iio_poll_func *pf = p; -struct iio_dev *indio_dev = pf->indio_dev;struct ad2s1210_state *st =iio_priv (indio_dev);size_t chan =0 ;int ret;
@@ -1295,15 +1293,15 @@ static irqreturn_t ad2s1210_trigger_handler(int irq, void *p)AD2S1210_REG_POSITION_MSB , &st->sample.raw, 2);if (ret <0 ) - goto error_ret; +return ret; }else { ret =ad2s1210_set_mode (st,MOD_POS );if (ret <0 ) - goto error_ret; +return ret; ret =spi_read (st->sdev, &st->sample,3 );if (ret <0 ) - goto error_ret; +return ret; }memcpy (&st->scan.chan[chan++], &st->sample.raw,2 );
@@ -1315,15 +1313,15 @@ static irqreturn_t ad2s1210_trigger_handler(int irq, void *p)AD2S1210_REG_VELOCITY_MSB , &st->sample.raw, 2);if (ret <0 ) - goto error_ret; +return ret; }else { ret =ad2s1210_set_mode (st,MOD_VEL );if (ret <0 ) - goto error_ret; +return ret; ret =spi_read (st->sdev, &st->sample,3 );if (ret <0 ) - goto error_ret; +return ret; }memcpy (&st->scan.chan[chan++], &st->sample.raw,2 );
@@ -1334,16 +1332,25 @@ static irqreturn_t ad2s1210_trigger_handler(int irq, void *p) ret =regmap_read (st->regmap,AD2S1210_REG_FAULT , ®_val);if (ret <0 ) - goto error_ret; +return ret; st->sample.fault = reg_val; } -ad2s1210_push_events (indio_dev, st->sample.fault, pf->timestamp); +ad2s1210_push_events (indio_dev, st->sample.fault, timestamp);iio_push_to_buffers_with_ts (indio_dev, &st->scan,sizeof (st->scan), - pf->timestamp); + timestamp); + +return 0 ; +} + +static irqreturn_t ad2s1210_trigger_handler (int irq,void *p) +{ +struct iio_poll_func *pf = p; +struct iio_dev *indio_dev = pf->indio_dev; + +ad2s1210_collect_and_push (indio_dev, pf->timestamp); -error_ret:iio_trigger_notify_done (indio_dev->trig);return IRQ_HANDLED ;
Signature · 1 line
2.43.0
Quoted 1 line · Stepan Ionichev
[PATCH] iio: resolver: ad2s1210: split trigger handler body into a helper
Please drop this -- I missed that David posted essentially the same refactor eight minutes earlier:
His version is cleaner (void helper, since the irq handler never looks at the return value).
Reviewing his patch separately.
Stepan
drivers/iio/resolver/ad2s1210.c static int ad2s1210_debugfs_reg_access(struct iio_dev *indio_dev, · line 1279
Quoted 10 lines · Stepan Ionichev
ad2s1210_trigger_handler()takesst->lockvia guard(mutex), then runsdrivers/iio/resolver/ad2s1210.c +1
-1@@ -1276,10 +1276,8 @@ static int ad2s1210_debugfs_reg_access(struct iio_dev *indio_dev,return regmap_write (st->regmap, reg, writeval); } -static irqreturn_t ad2s1210_trigger_handler (int irq,void *p) +static int ad2s1210_collect_and_push (struct iio_dev *indio_dev,s64 timestamp)
Hi Stepan,
Is the return value of this function checked by any caller?
If not, should this helper return void instead?
Quoted 45 lines
drivers/iio/resolver/ad2s1210.c +6
-8{ -struct iio_poll_func *pf = p; -struct iio_dev *indio_dev = pf->indio_dev;struct ad2s1210_state *st =iio_priv (indio_dev);size_t chan =0 ;int ret;@@ -1295,15 +1293,15 @@ static irqreturn_t ad2s1210_trigger_handler(int irq, void *p)AD2S1210_REG_POSITION_MSB , &st->sample.raw, 2);if (ret <0 ) - goto error_ret; +return ret; }else { ret =ad2s1210_set_mode (st,MOD_POS );if (ret <0 ) - goto error_ret; +return ret; ret =spi_read (st->sdev, &st->sample,3 );if (ret <0 ) - goto error_ret; +return ret; }memcpy (&st->scan.chan[chan++], &st->sample.raw,2 );@@ -1315,15 +1313,15 @@ static irqreturn_t ad2s1210_trigger_handler(int irq, void *p)AD2S1210_REG_VELOCITY_MSB , &st->sample.raw, 2);if (ret <0 ) - goto error_ret; +return ret; }else { ret =ad2s1210_set_mode (st,MOD_VEL );if (ret <0 ) - goto error_ret; +return ret; ret =spi_read (st->sdev, &st->sample,3 );if (ret <0 ) - goto error_ret; +return ret; }memcpy (&st->scan.chan[chan++], &st->sample.raw,2 );
drivers/iio/resolver/ad2s1210.c static irqreturn_t ad2s1210_trigger_handler(int irq, void *p) · line 1336
Quoted 6 lines
drivers/iio/resolver/ad2s1210.c +1
-1@@ -1334,16 +1332,25 @@ static irqreturn_t ad2s1210_trigger_handler(int irq, void *p) ret =regmap_read (st->regmap,AD2S1210_REG_FAULT , ®_val);if (ret <0 ) - goto error_ret; +return ret;
I cannot find this goto instance in mainline.
Is this patch based on top of iio/testing or another branch?
For patch dependencies like this, is it preferred to mention the base commit in the changelog for single patches as well or in the commit prefix?
Quoted 9 lines
drivers/iio/resolver/ad2s1210.c +3
-2st->sample.fault = reg_val; } -ad2s1210_push_events (indio_dev, st->sample.fault, pf->timestamp); +ad2s1210_push_events (indio_dev, st->sample.fault, timestamp);iio_push_to_buffers_with_ts (indio_dev, &st->scan,sizeof (st->scan), - pf->timestamp); + timestamp); +
drivers/iio/resolver/ad2s1210.c static irqreturn_t ad2s1210_trigger_handler(int irq, void *p) · line 1353
Quoted 10 lines
drivers/iio/resolver/ad2s1210.c +9
-0+return 0 ; +} + +static irqreturn_t ad2s1210_trigger_handler (int irq,void *p) +{ +struct iio_poll_func *pf = p; +struct iio_dev *indio_dev = pf->indio_dev; + +ad2s1210_collect_and_push (indio_dev, pf->timestamp);
The return value does not appear to be used by the caller.
Should this helper return void instead, or should the caller handle the returned error code?
The helper function changes look good otherwise.
Thanks, Sanjay
Quoted 4 lines
drivers/iio/resolver/ad2s1210.c +0
-1-error_ret:iio_trigger_notify_done (indio_dev->trig);return IRQ_HANDLED ;
Quoted 73 lines · Sanjay Chitroda
drivers/iio/resolver/ad2s1210.c static int ad2s1210_debugfs_reg_access(struct iio_dev *indio_dev, · line 1279
Quoted 10 lines · Stepan Ionichev
ad2s1210_trigger_handler()takesst->lockvia guard(mutex), then runsdrivers/iio/resolver/ad2s1210.c +1
-1@@ -1276,10 +1276,8 @@ static int ad2s1210_debugfs_reg_access(struct iio_dev *indio_dev,return regmap_write (st->regmap, reg, writeval); } -static irqreturn_t ad2s1210_trigger_handler (int irq,void *p) +static int ad2s1210_collect_and_push (struct iio_dev *indio_dev,s64 timestamp)Hi Stepan,
Is the return value of this function checked by any caller?
If not, should this helper return void instead?
Quoted 45 lines
drivers/iio/resolver/ad2s1210.c +6
-8{ -struct iio_poll_func *pf = p; -struct iio_dev *indio_dev = pf->indio_dev;struct ad2s1210_state *st =iio_priv (indio_dev);size_t chan =0 ;int ret;@@ -1295,15 +1293,15 @@ static irqreturn_t ad2s1210_trigger_handler(int irq, void *p)AD2S1210_REG_POSITION_MSB , &st->sample.raw, 2);if (ret <0 ) - goto error_ret; +return ret; }else { ret =ad2s1210_set_mode (st,MOD_POS );if (ret <0 ) - goto error_ret; +return ret; ret =spi_read (st->sdev, &st->sample,3 );if (ret <0 ) - goto error_ret; +return ret; }memcpy (&st->scan.chan[chan++], &st->sample.raw,2 );@@ -1315,15 +1313,15 @@ static irqreturn_t ad2s1210_trigger_handler(int irq, void *p)AD2S1210_REG_VELOCITY_MSB , &st->sample.raw, 2);if (ret <0 ) - goto error_ret; +return ret; }else { ret =ad2s1210_set_mode (st,MOD_VEL );if (ret <0 ) - goto error_ret; +return ret; ret =spi_read (st->sdev, &st->sample,3 );if (ret <0 ) - goto error_ret; +return ret; }memcpy (&st->scan.chan[chan++], &st->sample.raw,2 );drivers/iio/resolver/ad2s1210.c static irqreturn_t ad2s1210_trigger_handler(int irq, void *p) · line 1336
Quoted 6 lines
drivers/iio/resolver/ad2s1210.c +1
-1@@ -1334,16 +1332,25 @@ static irqreturn_t ad2s1210_trigger_handler(int irq, void *p) ret =regmap_read (st->regmap,AD2S1210_REG_FAULT , ®_val);if (ret <0 ) - goto error_ret; +return ret;I cannot find this goto instance in mainline.
Quoted 4 lines
Is this patch based on top of iio/testing or another branch?
For patch dependencies like this, is it preferred to mention the base commit in the changelog for single patches as well or in the commit prefix?
Looks like David has submitted change answers my concern.
Thanks, Sanjay
Quoted 32 lines
Quoted 10 lines
drivers/iio/resolver/ad2s1210.c +3
-2st->sample.fault = reg_val; } -ad2s1210_push_events (indio_dev, st->sample.fault, pf->timestamp); +ad2s1210_push_events (indio_dev, st->sample.fault, timestamp);iio_push_to_buffers_with_ts (indio_dev, &st->scan,sizeof (st->scan), - pf->timestamp); + timestamp); +drivers/iio/resolver/ad2s1210.c static irqreturn_t ad2s1210_trigger_handler(int irq, void *p) · line 1353
Quoted 10 lines
drivers/iio/resolver/ad2s1210.c +9
-0+return 0 ; +} + +static irqreturn_t ad2s1210_trigger_handler (int irq,void *p) +{ +struct iio_poll_func *pf = p; +struct iio_dev *indio_dev = pf->indio_dev; + +ad2s1210_collect_and_push (indio_dev, pf->timestamp);The return value does not appear to be used by the caller.
Should this helper return void instead, or should the caller handle the returned error code?
The helper function changes look good otherwise.
Thanks, Sanjay
Quoted 4 lines
drivers/iio/resolver/ad2s1210.c +0
-1-error_ret:iio_trigger_notify_done (indio_dev->trig);return IRQ_HANDLED ;