public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Christian Eggers <ceggers@arri.de>
To: Jonathan Cameron <jic23@kernel.org>,
	Lars-Peter Clausen <lars@metafoo.de>,
	Michael Hennerich <Michael.Hennerich@analog.com>,
	Alisa-Dariana Roman <alisa.roman@analog.com>,
	Peter Rosin <peda@axentia.se>,
	Paul Cercueil <paul@crapouillou.net>,
	Sebastian Reichel <sre@kernel.org>,
	Matteo Martelli <matteomartelli3@gmail.com>
Cc: <linux-iio@vger.kernel.org>, <linux-kernel@vger.kernel.org>,
	<linux-mips@vger.kernel.org>, <linux-pm@vger.kernel.org>,
	Matteo Martelli <matteomartelli3@gmail.com>
Subject: Re: [PATCH v2 4/7] iio: as73211: copy/release available integration times to fix race
Date: Mon, 7 Oct 2024 17:44:17 +0200	[thread overview]
Message-ID: <2287601.vFx2qVVIhK@n9w6sw14> (raw)
In-Reply-To: <20241007-iio-read-avail-release-v2-4-245002d5869e@gmail.com>

Hi Matteo,

originally the `mutex` member of `struct as73211_data` was only intended for
protecting the cached device registers. So can you please update the
documentation of this member?

I can perform some testing für as73211, but I would like to wait until
the "copy data twice" question has been solved. IMHO a solution like Nuno
proposed may be easier to understand.

regards,
Christian

On Monday, 7 October 2024, 10:37:13 CEST, Matteo Martelli wrote:
> While available integration times are being printed to sysfs by iio core
> (iio_read_channel_info_avail), the sampling frequency might be changed.
> This could cause the buffer shared with iio core to be corrupted. To
> prevent it, make a copy of the integration times buffer and free it in
> the read_avail_release_resource callback.
> 
> Signed-off-by: Matteo Martelli <matteomartelli3@gmail.com>
> ---
>  drivers/iio/light/as73211.c | 22 +++++++++++++++++++---
>  1 file changed, 19 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/iio/light/as73211.c b/drivers/iio/light/as73211.c
> index be0068081ebbbb37fdfb252b67a77b302ff725f6..27bc8cb791039944662a74fc72f09e2c3642cfa6 100644
> --- a/drivers/iio/light/as73211.c
> +++ b/drivers/iio/light/as73211.c
> @@ -493,17 +493,32 @@ static int as73211_read_avail(struct iio_dev *indio_dev, struct iio_chan_spec co
>  		*type = IIO_VAL_INT;
>  		return IIO_AVAIL_LIST;
>  
> -	case IIO_CHAN_INFO_INT_TIME:
> +	case IIO_CHAN_INFO_INT_TIME: {
>  		*length = ARRAY_SIZE(data->int_time_avail);
> -		*vals = data->int_time_avail;
>  		*type = IIO_VAL_INT_PLUS_MICRO;
> -		return IIO_AVAIL_LIST;
>  
> +		guard(mutex)(&data->mutex);
> +
> +		*vals = kmemdup_array(data->int_time_avail, *length,
> +				      sizeof(int), GFP_KERNEL);
> +		if (!*vals)
> +			return -ENOMEM;
> +
> +		return IIO_AVAIL_LIST;
> +	}
>  	default:
>  		return -EINVAL;
>  	}
>  }
>  
> +static void as73211_read_avail_release_res(struct iio_dev *indio_dev,
> +					   struct iio_chan_spec const *chan,
> +					   const int *vals, long mask)
> +{
> +	if (mask == IIO_CHAN_INFO_INT_TIME)
> +		kfree(vals);
> +}
> +
>  static int _as73211_write_raw(struct iio_dev *indio_dev,
>  			       struct iio_chan_spec const *chan __always_unused,
>  			       int val, int val2, long mask)
> @@ -699,6 +714,7 @@ static irqreturn_t as73211_trigger_handler(int irq __always_unused, void *p)
>  static const struct iio_info as73211_info = {
>  	.read_raw = as73211_read_raw,
>  	.read_avail = as73211_read_avail,
> +	.read_avail_release_resource = as73211_read_avail_release_res,
>  	.write_raw = as73211_write_raw,
>  };
>  
> 
> 





  reply	other threads:[~2024-10-07 15:44 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-10-07  8:37 [PATCH v2 0/7] iio: fix possible race condition during access of available info lists Matteo Martelli
2024-10-07  8:37 ` [PATCH v2 1/7] iio: core: add read_avail_release_resource callback to fix race Matteo Martelli
2024-10-07  8:37 ` [PATCH v2 2/7] iio: pac1921: use read_avail+release APIs instead of custom ext_info Matteo Martelli
2024-10-07  8:37 ` [PATCH v2 3/7] iio: ad7192: copy/release available filter frequencies to fix race Matteo Martelli
2024-10-07  8:37 ` [PATCH v2 4/7] iio: as73211: copy/release available integration times " Matteo Martelli
2024-10-07 15:44   ` Christian Eggers [this message]
2024-10-07  8:37 ` [PATCH v2 5/7] iio: inkern: copy/release available info from producer Matteo Martelli
2024-10-07 15:15   ` Nuno Sá
2024-10-08  6:47     ` Matteo Martelli
2024-10-08  7:29       ` Nuno Sá
2024-10-08  8:03         ` Matteo Martelli
2024-10-08 12:37           ` Nuno Sá
2024-10-09 18:30             ` Matteo Martelli
2024-10-12 15:47               ` Jonathan Cameron
2024-10-12 23:09                 ` Matteo Martelli
2024-10-14  6:39                   ` Nuno Sá
2024-10-07  8:37 ` [PATCH v2 6/7] iio: consumers: release available info buffer copied " Matteo Martelli
2024-10-07  8:37 ` [PATCH v2 7/7] power: supply: ingenic-battery: free scale buffer after use Matteo Martelli
2024-10-08 11:36 ` [PATCH v2 0/7] iio: fix possible race condition during access of available info lists Peter Rosin

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=2287601.vFx2qVVIhK@n9w6sw14 \
    --to=ceggers@arri.de \
    --cc=Michael.Hennerich@analog.com \
    --cc=alisa.roman@analog.com \
    --cc=jic23@kernel.org \
    --cc=lars@metafoo.de \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mips@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=matteomartelli3@gmail.com \
    --cc=paul@crapouillou.net \
    --cc=peda@axentia.se \
    --cc=sre@kernel.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox