[PATCH v2] iio: adc: viperboard: Fix error handling in vprbrd_iio_read_raw

Salah Triki posted 1 patch 1 month ago
drivers/iio/adc/viperboard_adc.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
[PATCH v2] iio: adc: viperboard: Fix error handling in vprbrd_iio_read_raw
Posted by Salah Triki 1 month ago
The driver proceeds to the reception phase even if the preceding
transmission fails.

This uses a goto error label for an early bail out and ensures the mutex is
properly unlocked in case of failure.

Fixes: ffd8a6e7a778 ("iio: adc: Add viperboard adc driver")
Signed-off-by: Salah Triki <salah.triki@gmail.com>
Reviewed-by: Joshua Crofts <joshua.crofts1@gmail.com>
Reviewed-by: Maxwell Doose <m32285159@gmail.com>
---
Changes in v2:
  - Add "adc" to the patch title.
  - Move dev_err() out of the mutex critical section.
  - Include Reviewed-by tags from Joshua and Maxwell.

 drivers/iio/adc/viperboard_adc.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/iio/adc/viperboard_adc.c b/drivers/iio/adc/viperboard_adc.c
index 9bb0b83c8f67..f7ce3effd730 100644
--- a/drivers/iio/adc/viperboard_adc.c
+++ b/drivers/iio/adc/viperboard_adc.c
@@ -70,8 +70,10 @@ static int vprbrd_iio_read_raw(struct iio_dev *iio_dev,
 			VPRBRD_USB_TYPE_OUT, 0x0000, 0x0000, admsg,
 			sizeof(struct vprbrd_adc_msg), VPRBRD_USB_TIMEOUT_MS);
 		if (ret != sizeof(struct vprbrd_adc_msg)) {
-			dev_err(&iio_dev->dev, "usb send error on adc read\n");
 			error = -EREMOTEIO;
+			mutex_unlock(&vb->lock);
+			dev_err(&iio_dev->dev, "usb send error on adc read\n");
+			goto error;
 		}
 
 		ret = usb_control_msg(vb->usb_dev,
-- 
2.43.0
Re: [PATCH v2] iio: adc: viperboard: Fix error handling in vprbrd_iio_read_raw
Posted by Nuno Sá 1 month ago
On Thu, 2026-05-07 at 20:07 +0100, Salah Triki wrote:
> The driver proceeds to the reception phase even if the preceding
> transmission fails.
> 
> This uses a goto error label for an early bail out and ensures the mutex is
> properly unlocked in case of failure.
> 
> Fixes: ffd8a6e7a778 ("iio: adc: Add viperboard adc driver")
> Signed-off-by: Salah Triki <salah.triki@gmail.com>
> Reviewed-by: Joshua Crofts <joshua.crofts1@gmail.com>
> Reviewed-by: Maxwell Doose <m32285159@gmail.com>
> ---

LGTM (though Andy's comment makes sense):

Reviewed-by: Nuno Sá <nuno.sa@analog.com>

> Changes in v2:
>   - Add "adc" to the patch title.
>   - Move dev_err() out of the mutex critical section.
>   - Include Reviewed-by tags from Joshua and Maxwell.
> 
>  drivers/iio/adc/viperboard_adc.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/iio/adc/viperboard_adc.c b/drivers/iio/adc/viperboard_adc.c
> index 9bb0b83c8f67..f7ce3effd730 100644
> --- a/drivers/iio/adc/viperboard_adc.c
> +++ b/drivers/iio/adc/viperboard_adc.c
> @@ -70,8 +70,10 @@ static int vprbrd_iio_read_raw(struct iio_dev *iio_dev,
>  			VPRBRD_USB_TYPE_OUT, 0x0000, 0x0000, admsg,
>  			sizeof(struct vprbrd_adc_msg), VPRBRD_USB_TIMEOUT_MS);
>  		if (ret != sizeof(struct vprbrd_adc_msg)) {
> -			dev_err(&iio_dev->dev, "usb send error on adc read\n");
>  			error = -EREMOTEIO;
> +			mutex_unlock(&vb->lock);
> +			dev_err(&iio_dev->dev, "usb send error on adc read\n");
> +			goto error;
>  		}
>  
>  		ret = usb_control_msg(vb->usb_dev,
Re: [PATCH v2] iio: adc: viperboard: Fix error handling in vprbrd_iio_read_raw
Posted by Andy Shevchenko 1 month ago
On Thu, May 07, 2026 at 08:07:51PM +0100, Salah Triki wrote:
> The driver proceeds to the reception phase even if the preceding
> transmission fails.
> 
> This uses a goto error label for an early bail out and ensures the mutex is
> properly unlocked in case of failure.

...

>  		if (ret != sizeof(struct vprbrd_adc_msg)) {
> -			dev_err(&iio_dev->dev, "usb send error on adc read\n");
>  			error = -EREMOTEIO;

This also a local variable that doesn't need to be in the critical section,
right?

> +			mutex_unlock(&vb->lock);
> +			dev_err(&iio_dev->dev, "usb send error on adc read\n");
> +			goto error;
>  		}

-- 
With Best Regards,
Andy Shevchenko
Re: [PATCH v2] iio: adc: viperboard: Fix error handling in vprbrd_iio_read_raw
Posted by Jonathan Cameron 1 month ago
On Sat, 9 May 2026 10:56:26 +0300
Andy Shevchenko <andriy.shevchenko@intel.com> wrote:

> On Thu, May 07, 2026 at 08:07:51PM +0100, Salah Triki wrote:
> > The driver proceeds to the reception phase even if the preceding
> > transmission fails.
> > 
> > This uses a goto error label for an early bail out and ensures the mutex is
> > properly unlocked in case of failure.  
> 
> ...
> 
> >  		if (ret != sizeof(struct vprbrd_adc_msg)) {
> > -			dev_err(&iio_dev->dev, "usb send error on adc read\n");
> >  			error = -EREMOTEIO;  
> 
> This also a local variable that doesn't need to be in the critical section,
> right?
Tweaked and applied to the fixes-togreg branch of iio.git + marked for stable.

> 
> > +			mutex_unlock(&vb->lock);
> > +			dev_err(&iio_dev->dev, "usb send error on adc read\n");
> > +			goto error;
> >  		}  
>