All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jonathan Cameron <jic23@kernel.org>
To: William Breathitt Gray <william.gray@linaro.org>
Cc: Lars-Peter Clausen <lars@metafoo.de>,
	linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org,
	Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Subject: Re: [PATCH v5 1/6] iio: addac: stx104: Fix race condition for stx104_write_raw()
Date: Fri, 7 Apr 2023 17:51:47 +0100	[thread overview]
Message-ID: <20230407175147.180f9f44@jic23-huawei> (raw)
In-Reply-To: <c95c9a77fcef36b2a052282146950f23bbc1ebdc.1680790580.git.william.gray@linaro.org>

On Thu,  6 Apr 2023 10:40:10 -0400
William Breathitt Gray <william.gray@linaro.org> wrote:

> The priv->chan_out_states array and actual DAC value can become
> mismatched if stx104_write_raw() is called concurrently. Prevent such a
> race condition by utilizing a mutex.
> 
> Fixes: 97a445dad37a ("iio: Add IIO support for the DAC on the Apex Embedded Systems STX104")
> Signed-off-by: William Breathitt Gray <william.gray@linaro.org>
Marked these first two patches for stable as probably worth a backport
even if no one notices the affect of the race. I'll queue them for the
next merge window though as we are fairly late in this cycle and I don't
want to delay the 4 patches that follow a whole cycle.

Thanks,

Jonathan

> ---
> Changes in v5: none
> 
>  drivers/iio/addac/stx104.c | 8 ++++++++
>  1 file changed, 8 insertions(+)
> 
> diff --git a/drivers/iio/addac/stx104.c b/drivers/iio/addac/stx104.c
> index e45b70aa5bb7..4239aafe42fc 100644
> --- a/drivers/iio/addac/stx104.c
> +++ b/drivers/iio/addac/stx104.c
> @@ -15,6 +15,7 @@
>  #include <linux/kernel.h>
>  #include <linux/module.h>
>  #include <linux/moduleparam.h>
> +#include <linux/mutex.h>
>  #include <linux/spinlock.h>
>  #include <linux/types.h>
>  
> @@ -69,10 +70,12 @@ struct stx104_reg {
>  
>  /**
>   * struct stx104_iio - IIO device private data structure
> + * @lock: synchronization lock to prevent I/O race conditions
>   * @chan_out_states:	channels' output states
>   * @reg:		I/O address offset for the device registers
>   */
>  struct stx104_iio {
> +	struct mutex lock;
>  	unsigned int chan_out_states[STX104_NUM_OUT_CHAN];
>  	struct stx104_reg __iomem *reg;
>  };
> @@ -178,9 +181,12 @@ static int stx104_write_raw(struct iio_dev *indio_dev,
>  			if ((unsigned int)val > 65535)
>  				return -EINVAL;
>  
> +			mutex_lock(&priv->lock);
> +
>  			priv->chan_out_states[chan->channel] = val;
>  			iowrite16(val, &priv->reg->dac[chan->channel]);
>  
> +			mutex_unlock(&priv->lock);
>  			return 0;
>  		}
>  		return -EINVAL;
> @@ -351,6 +357,8 @@ static int stx104_probe(struct device *dev, unsigned int id)
>  
>  	indio_dev->name = dev_name(dev);
>  
> +	mutex_init(&priv->lock);
> +
>  	/* configure device for software trigger operation */
>  	iowrite8(0, &priv->reg->acr);
>  


  reply	other threads:[~2023-04-07 16:36 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-06 14:40 [PATCH v5 0/6] Migrate STX104 to the regmap API William Breathitt Gray
2023-04-06 14:40 ` [PATCH v5 1/6] iio: addac: stx104: Fix race condition for stx104_write_raw() William Breathitt Gray
2023-04-07 16:51   ` Jonathan Cameron [this message]
2023-04-06 14:40 ` [PATCH v5 2/6] iio: addac: stx104: Fix race condition when converting analog-to-digital William Breathitt Gray
2023-04-06 14:40 ` [PATCH v5 3/6] iio: addac: stx104: Use define rather than hardcoded limit for write val William Breathitt Gray
2023-04-06 14:40 ` [PATCH v5 4/6] iio: addac: stx104: Improve indentation in stx104_write_raw() William Breathitt Gray
2023-04-06 14:40 ` [PATCH v5 5/6] iio: addac: stx104: Migrate to the regmap API William Breathitt Gray
2023-04-06 14:40 ` [PATCH v5 6/6] iio: addac: stx104: Use regmap_read_poll_timeout() for conversion poll William Breathitt Gray
2023-04-07 16:49 ` [PATCH v5 0/6] Migrate STX104 to the regmap API Jonathan Cameron

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=20230407175147.180f9f44@jic23-huawei \
    --to=jic23@kernel.org \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=lars@metafoo.de \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=william.gray@linaro.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.