linux-iio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH AUTOSEL 5.18 01/68] iio: dummy: iio_simple_dummy: check the return value of kstrdup()
@ 2022-06-07 17:47 Sasha Levin
  2022-06-07 17:47 ` [PATCH AUTOSEL 5.18 03/68] iio: st_sensors: Add a local lock for protecting odr Sasha Levin
  0 siblings, 1 reply; 4+ messages in thread
From: Sasha Levin @ 2022-06-07 17:47 UTC (permalink / raw)
  To: linux-kernel, stable
  Cc: Xiaoke Wang, Jonathan Cameron, Sasha Levin, jic23, linux-iio

From: Xiaoke Wang <xkernel.wang@foxmail.com>

[ Upstream commit ba93642188a6fed754bf7447f638bc410e05a929 ]

kstrdup() is also a memory allocation-related function, it returns NULL
when some memory errors happen. So it is better to check the return
value of it so to catch the memory error in time. Besides, there should
have a kfree() to clear up the allocation if we get a failure later in
this function to prevent memory leak.

Signed-off-by: Xiaoke Wang <xkernel.wang@foxmail.com>
Link: https://lore.kernel.org/r/tencent_C920CFCC33B9CC1C63141FE1334A39FF8508@qq.com
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
 drivers/iio/dummy/iio_simple_dummy.c | 20 ++++++++++++--------
 1 file changed, 12 insertions(+), 8 deletions(-)

diff --git a/drivers/iio/dummy/iio_simple_dummy.c b/drivers/iio/dummy/iio_simple_dummy.c
index c0b7ef900735..c24f609c2ade 100644
--- a/drivers/iio/dummy/iio_simple_dummy.c
+++ b/drivers/iio/dummy/iio_simple_dummy.c
@@ -575,10 +575,9 @@ static struct iio_sw_device *iio_dummy_probe(const char *name)
 	 */
 
 	swd = kzalloc(sizeof(*swd), GFP_KERNEL);
-	if (!swd) {
-		ret = -ENOMEM;
-		goto error_kzalloc;
-	}
+	if (!swd)
+		return ERR_PTR(-ENOMEM);
+
 	/*
 	 * Allocate an IIO device.
 	 *
@@ -590,7 +589,7 @@ static struct iio_sw_device *iio_dummy_probe(const char *name)
 	indio_dev = iio_device_alloc(parent, sizeof(*st));
 	if (!indio_dev) {
 		ret = -ENOMEM;
-		goto error_ret;
+		goto error_free_swd;
 	}
 
 	st = iio_priv(indio_dev);
@@ -616,6 +615,10 @@ static struct iio_sw_device *iio_dummy_probe(const char *name)
 	 *    indio_dev->name = spi_get_device_id(spi)->name;
 	 */
 	indio_dev->name = kstrdup(name, GFP_KERNEL);
+	if (!indio_dev->name) {
+		ret = -ENOMEM;
+		goto error_free_device;
+	}
 
 	/* Provide description of available channels */
 	indio_dev->channels = iio_dummy_channels;
@@ -632,7 +635,7 @@ static struct iio_sw_device *iio_dummy_probe(const char *name)
 
 	ret = iio_simple_dummy_events_register(indio_dev);
 	if (ret < 0)
-		goto error_free_device;
+		goto error_free_name;
 
 	ret = iio_simple_dummy_configure_buffer(indio_dev);
 	if (ret < 0)
@@ -649,11 +652,12 @@ static struct iio_sw_device *iio_dummy_probe(const char *name)
 	iio_simple_dummy_unconfigure_buffer(indio_dev);
 error_unregister_events:
 	iio_simple_dummy_events_unregister(indio_dev);
+error_free_name:
+	kfree(indio_dev->name);
 error_free_device:
 	iio_device_free(indio_dev);
-error_ret:
+error_free_swd:
 	kfree(swd);
-error_kzalloc:
 	return ERR_PTR(ret);
 }
 
-- 
2.35.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH AUTOSEL 5.18 03/68] iio: st_sensors: Add a local lock for protecting odr
  2022-06-07 17:47 [PATCH AUTOSEL 5.18 01/68] iio: dummy: iio_simple_dummy: check the return value of kstrdup() Sasha Levin
@ 2022-06-07 17:47 ` Sasha Levin
  2022-06-08  9:26   ` Jonathan Cameron
  0 siblings, 1 reply; 4+ messages in thread
From: Sasha Levin @ 2022-06-07 17:47 UTC (permalink / raw)
  To: linux-kernel, stable
  Cc: Miquel Raynal, Jonathan Cameron, Denis Ciocca, Jonathan Cameron,
	Sasha Levin, linus.walleij, lars, andy.shevchenko, aardelean,
	cai.huoqing, linux-iio

From: Miquel Raynal <miquel.raynal@bootlin.com>

[ Upstream commit 474010127e2505fc463236470908e1ff5ddb3578 ]

Right now the (framework) mlock lock is (ab)used for multiple purposes:
1- protecting concurrent accesses over the odr local cache
2- avoid changing samplig frequency whilst buffer is running

Let's start by handling situation #1 with a local lock.

Suggested-by: Jonathan Cameron <jic23@kernel.org>
Cc: Denis Ciocca <denis.ciocca@st.com>
Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
Link: https://lore.kernel.org/r/20220207143840.707510-7-miquel.raynal@bootlin.com
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
 .../iio/common/st_sensors/st_sensors_core.c   | 24 ++++++++++++++-----
 include/linux/iio/common/st_sensors.h         |  3 +++
 2 files changed, 21 insertions(+), 6 deletions(-)

diff --git a/drivers/iio/common/st_sensors/st_sensors_core.c b/drivers/iio/common/st_sensors/st_sensors_core.c
index fa9bcdf0d190..b92de90a125c 100644
--- a/drivers/iio/common/st_sensors/st_sensors_core.c
+++ b/drivers/iio/common/st_sensors/st_sensors_core.c
@@ -71,16 +71,18 @@ static int st_sensors_match_odr(struct st_sensor_settings *sensor_settings,
 
 int st_sensors_set_odr(struct iio_dev *indio_dev, unsigned int odr)
 {
-	int err;
+	int err = 0;
 	struct st_sensor_odr_avl odr_out = {0, 0};
 	struct st_sensor_data *sdata = iio_priv(indio_dev);
 
+	mutex_lock(&sdata->odr_lock);
+
 	if (!sdata->sensor_settings->odr.mask)
-		return 0;
+		goto unlock_mutex;
 
 	err = st_sensors_match_odr(sdata->sensor_settings, odr, &odr_out);
 	if (err < 0)
-		goto st_sensors_match_odr_error;
+		goto unlock_mutex;
 
 	if ((sdata->sensor_settings->odr.addr ==
 					sdata->sensor_settings->pw.addr) &&
@@ -103,7 +105,9 @@ int st_sensors_set_odr(struct iio_dev *indio_dev, unsigned int odr)
 	if (err >= 0)
 		sdata->odr = odr_out.hz;
 
-st_sensors_match_odr_error:
+unlock_mutex:
+	mutex_unlock(&sdata->odr_lock);
+
 	return err;
 }
 EXPORT_SYMBOL_NS(st_sensors_set_odr, IIO_ST_SENSORS);
@@ -361,6 +365,8 @@ int st_sensors_init_sensor(struct iio_dev *indio_dev,
 	struct st_sensors_platform_data *of_pdata;
 	int err = 0;
 
+	mutex_init(&sdata->odr_lock);
+
 	/* If OF/DT pdata exists, it will take precedence of anything else */
 	of_pdata = st_sensors_dev_probe(indio_dev->dev.parent, pdata);
 	if (IS_ERR(of_pdata))
@@ -554,18 +560,24 @@ int st_sensors_read_info_raw(struct iio_dev *indio_dev,
 		err = -EBUSY;
 		goto out;
 	} else {
+		mutex_lock(&sdata->odr_lock);
 		err = st_sensors_set_enable(indio_dev, true);
-		if (err < 0)
+		if (err < 0) {
+			mutex_unlock(&sdata->odr_lock);
 			goto out;
+		}
 
 		msleep((sdata->sensor_settings->bootime * 1000) / sdata->odr);
 		err = st_sensors_read_axis_data(indio_dev, ch, val);
-		if (err < 0)
+		if (err < 0) {
+			mutex_unlock(&sdata->odr_lock);
 			goto out;
+		}
 
 		*val = *val >> ch->scan_type.shift;
 
 		err = st_sensors_set_enable(indio_dev, false);
+		mutex_unlock(&sdata->odr_lock);
 	}
 out:
 	mutex_unlock(&indio_dev->mlock);
diff --git a/include/linux/iio/common/st_sensors.h b/include/linux/iio/common/st_sensors.h
index 22f67845cdd3..db4a1b260348 100644
--- a/include/linux/iio/common/st_sensors.h
+++ b/include/linux/iio/common/st_sensors.h
@@ -237,6 +237,7 @@ struct st_sensor_settings {
  * @hw_irq_trigger: if we're using the hardware interrupt on the sensor.
  * @hw_timestamp: Latest timestamp from the interrupt handler, when in use.
  * @buffer_data: Data used by buffer part.
+ * @odr_lock: Local lock for preventing concurrent ODR accesses/changes
  */
 struct st_sensor_data {
 	struct iio_trigger *trig;
@@ -261,6 +262,8 @@ struct st_sensor_data {
 	s64 hw_timestamp;
 
 	char buffer_data[ST_SENSORS_MAX_BUFFER_SIZE] ____cacheline_aligned;
+
+	struct mutex odr_lock;
 };
 
 #ifdef CONFIG_IIO_BUFFER
-- 
2.35.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH AUTOSEL 5.18 03/68] iio: st_sensors: Add a local lock for protecting odr
  2022-06-07 17:47 ` [PATCH AUTOSEL 5.18 03/68] iio: st_sensors: Add a local lock for protecting odr Sasha Levin
@ 2022-06-08  9:26   ` Jonathan Cameron
  2022-06-09 13:56     ` Sasha Levin
  0 siblings, 1 reply; 4+ messages in thread
From: Jonathan Cameron @ 2022-06-08  9:26 UTC (permalink / raw)
  To: Sasha Levin
  Cc: linux-kernel, stable, Miquel Raynal, Jonathan Cameron,
	Denis Ciocca, linus.walleij, lars, andy.shevchenko, aardelean,
	cai.huoqing, linux-iio

On Tue,  7 Jun 2022 13:47:29 -0400
Sasha Levin <sashal@kernel.org> wrote:

> From: Miquel Raynal <miquel.raynal@bootlin.com>
> 
> [ Upstream commit 474010127e2505fc463236470908e1ff5ddb3578 ]
> 
> Right now the (framework) mlock lock is (ab)used for multiple purposes:
> 1- protecting concurrent accesses over the odr local cache
> 2- avoid changing samplig frequency whilst buffer is running
> 
> Let's start by handling situation #1 with a local lock.
> 
> Suggested-by: Jonathan Cameron <jic23@kernel.org>
> Cc: Denis Ciocca <denis.ciocca@st.com>
> Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
> Link: https://lore.kernel.org/r/20220207143840.707510-7-miquel.raynal@bootlin.com
> Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> Signed-off-by: Sasha Levin <sashal@kernel.org>

Hi Sasha,

This one is a cleanup rather than a fix. It's part of a long term move to stop
drivers using an internal lock (which works, but limits our ability to
change the core code).  No problem backporting it if it makes
taking some other fix easier, but I'm not immediately seeing such a patch.

Thanks,

Jonathan

> ---
>  .../iio/common/st_sensors/st_sensors_core.c   | 24 ++++++++++++++-----
>  include/linux/iio/common/st_sensors.h         |  3 +++
>  2 files changed, 21 insertions(+), 6 deletions(-)
> 
> diff --git a/drivers/iio/common/st_sensors/st_sensors_core.c b/drivers/iio/common/st_sensors/st_sensors_core.c
> index fa9bcdf0d190..b92de90a125c 100644
> --- a/drivers/iio/common/st_sensors/st_sensors_core.c
> +++ b/drivers/iio/common/st_sensors/st_sensors_core.c
> @@ -71,16 +71,18 @@ static int st_sensors_match_odr(struct st_sensor_settings *sensor_settings,
>  
>  int st_sensors_set_odr(struct iio_dev *indio_dev, unsigned int odr)
>  {
> -	int err;
> +	int err = 0;
>  	struct st_sensor_odr_avl odr_out = {0, 0};
>  	struct st_sensor_data *sdata = iio_priv(indio_dev);
>  
> +	mutex_lock(&sdata->odr_lock);
> +
>  	if (!sdata->sensor_settings->odr.mask)
> -		return 0;
> +		goto unlock_mutex;
>  
>  	err = st_sensors_match_odr(sdata->sensor_settings, odr, &odr_out);
>  	if (err < 0)
> -		goto st_sensors_match_odr_error;
> +		goto unlock_mutex;
>  
>  	if ((sdata->sensor_settings->odr.addr ==
>  					sdata->sensor_settings->pw.addr) &&
> @@ -103,7 +105,9 @@ int st_sensors_set_odr(struct iio_dev *indio_dev, unsigned int odr)
>  	if (err >= 0)
>  		sdata->odr = odr_out.hz;
>  
> -st_sensors_match_odr_error:
> +unlock_mutex:
> +	mutex_unlock(&sdata->odr_lock);
> +
>  	return err;
>  }
>  EXPORT_SYMBOL_NS(st_sensors_set_odr, IIO_ST_SENSORS);
> @@ -361,6 +365,8 @@ int st_sensors_init_sensor(struct iio_dev *indio_dev,
>  	struct st_sensors_platform_data *of_pdata;
>  	int err = 0;
>  
> +	mutex_init(&sdata->odr_lock);
> +
>  	/* If OF/DT pdata exists, it will take precedence of anything else */
>  	of_pdata = st_sensors_dev_probe(indio_dev->dev.parent, pdata);
>  	if (IS_ERR(of_pdata))
> @@ -554,18 +560,24 @@ int st_sensors_read_info_raw(struct iio_dev *indio_dev,
>  		err = -EBUSY;
>  		goto out;
>  	} else {
> +		mutex_lock(&sdata->odr_lock);
>  		err = st_sensors_set_enable(indio_dev, true);
> -		if (err < 0)
> +		if (err < 0) {
> +			mutex_unlock(&sdata->odr_lock);
>  			goto out;
> +		}
>  
>  		msleep((sdata->sensor_settings->bootime * 1000) / sdata->odr);
>  		err = st_sensors_read_axis_data(indio_dev, ch, val);
> -		if (err < 0)
> +		if (err < 0) {
> +			mutex_unlock(&sdata->odr_lock);
>  			goto out;
> +		}
>  
>  		*val = *val >> ch->scan_type.shift;
>  
>  		err = st_sensors_set_enable(indio_dev, false);
> +		mutex_unlock(&sdata->odr_lock);
>  	}
>  out:
>  	mutex_unlock(&indio_dev->mlock);
> diff --git a/include/linux/iio/common/st_sensors.h b/include/linux/iio/common/st_sensors.h
> index 22f67845cdd3..db4a1b260348 100644
> --- a/include/linux/iio/common/st_sensors.h
> +++ b/include/linux/iio/common/st_sensors.h
> @@ -237,6 +237,7 @@ struct st_sensor_settings {
>   * @hw_irq_trigger: if we're using the hardware interrupt on the sensor.
>   * @hw_timestamp: Latest timestamp from the interrupt handler, when in use.
>   * @buffer_data: Data used by buffer part.
> + * @odr_lock: Local lock for preventing concurrent ODR accesses/changes
>   */
>  struct st_sensor_data {
>  	struct iio_trigger *trig;
> @@ -261,6 +262,8 @@ struct st_sensor_data {
>  	s64 hw_timestamp;
>  
>  	char buffer_data[ST_SENSORS_MAX_BUFFER_SIZE] ____cacheline_aligned;
> +
> +	struct mutex odr_lock;
>  };
>  
>  #ifdef CONFIG_IIO_BUFFER


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH AUTOSEL 5.18 03/68] iio: st_sensors: Add a local lock for protecting odr
  2022-06-08  9:26   ` Jonathan Cameron
@ 2022-06-09 13:56     ` Sasha Levin
  0 siblings, 0 replies; 4+ messages in thread
From: Sasha Levin @ 2022-06-09 13:56 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: linux-kernel, stable, Miquel Raynal, Jonathan Cameron,
	Denis Ciocca, linus.walleij, lars, andy.shevchenko, aardelean,
	cai.huoqing, linux-iio

On Wed, Jun 08, 2022 at 10:26:51AM +0100, Jonathan Cameron wrote:
>On Tue,  7 Jun 2022 13:47:29 -0400
>Sasha Levin <sashal@kernel.org> wrote:
>
>> From: Miquel Raynal <miquel.raynal@bootlin.com>
>>
>> [ Upstream commit 474010127e2505fc463236470908e1ff5ddb3578 ]
>>
>> Right now the (framework) mlock lock is (ab)used for multiple purposes:
>> 1- protecting concurrent accesses over the odr local cache
>> 2- avoid changing samplig frequency whilst buffer is running
>>
>> Let's start by handling situation #1 with a local lock.
>>
>> Suggested-by: Jonathan Cameron <jic23@kernel.org>
>> Cc: Denis Ciocca <denis.ciocca@st.com>
>> Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
>> Link: https://lore.kernel.org/r/20220207143840.707510-7-miquel.raynal@bootlin.com
>> Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
>> Signed-off-by: Sasha Levin <sashal@kernel.org>
>
>Hi Sasha,
>
>This one is a cleanup rather than a fix. It's part of a long term move to stop
>drivers using an internal lock (which works, but limits our ability to
>change the core code).  No problem backporting it if it makes
>taking some other fix easier, but I'm not immediately seeing such a patch.

Yup, it's not a dependency. I'll drop it.

-- 
Thanks,
Sasha

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-06-09 13:56 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-06-07 17:47 [PATCH AUTOSEL 5.18 01/68] iio: dummy: iio_simple_dummy: check the return value of kstrdup() Sasha Levin
2022-06-07 17:47 ` [PATCH AUTOSEL 5.18 03/68] iio: st_sensors: Add a local lock for protecting odr Sasha Levin
2022-06-08  9:26   ` Jonathan Cameron
2022-06-09 13:56     ` Sasha Levin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).