Skip to contentSkip to conversation

iio: imu: bmi160: add mutex_lock for avoiding race

PATCH Tom Rix 3 participants 5 messages Jan 19, 2021 – Jan 26, 2021 1 message not in the archive IIO

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

Tom Rix

author
#1 lore raw
Quoted 46 lines · Guoqing Chi

From: chiguoqing <[email protected]>

Adding mutex_lock, when read and write reg need to use this lock to avoid race.

---
v2:Follow write function to fix read function. Adding mutex init in core probe function. Adding break in switch case at read and write function.

 drivers/iio/imu/bmi160/bmi160.h      |  2 ++
 drivers/iio/imu/bmi160/bmi160_core.c | 34 +++++++++++++++++++---------
 2 files changed, 25 insertions(+), 11 deletions(-)
drivers/iio/imu/bmi160/bmi160.h +2 -0
index 32c2ea2d7112..0c189a8b5b53 100644
@@ -3,9 +3,11 @@
 #define BMI160_H_
 
 #include <linux/iio/iio.h>
+#include <linux/mutex.h>
 #include <linux/regulator/consumer.h>
 
 struct bmi160_data {
+	struct mutex lock;
 	struct regmap *regmap;
 	struct iio_trigger *trig;
 	struct regulator_bulk_data supplies[2];
drivers/iio/imu/bmi160/bmi160_core.c +1 -3
index 290b5ef83f77..e303378f4841 100644
@@ -452,26 +452,32 @@ static int bmi160_read_raw(struct iio_dev *indio_dev,
 	int ret;
 	struct bmi160_data *data = iio_priv(indio_dev);
 
+	mutex_lock(&data->lock);
 	switch (mask) {
 	case IIO_CHAN_INFO_RAW:
 		ret = bmi160_get_data(data, chan->type, chan->channel2, val);
-		if (ret)
-			return ret;
-		return IIO_VAL_INT;

drivers/iio/imu/bmi160/bmi160_core.c static int bmi160_read_raw(struct iio_dev *indio_dev, · line 467

Quoted 10 lines
drivers/iio/imu/bmi160/bmi160_core.c +5 -1
+		if (!ret)
+			ret = IIO_VAL_INT;
+		break;
 	case IIO_CHAN_INFO_SCALE:
 		*val = 0;
 		ret = bmi160_get_scale(data,
 				       bmi160_to_sensor(chan->type), val2);
-		return ret ? ret : IIO_VAL_INT_PLUS_MICRO;
+		if (!ret)
+			ret = IIO_VAL_INT_PLUS_MICRO;

Looking better, another question..

Why does the write() function return the results directly while the read() function

translates them to other values ?

Tom

Quoted 56 lines
drivers/iio/imu/bmi160/bmi160_core.c +17 -7
+		break;
 	case IIO_CHAN_INFO_SAMP_FREQ:
 		ret = bmi160_get_odr(data, bmi160_to_sensor(chan->type),
 				     val, val2);
-		return ret ? ret : IIO_VAL_INT_PLUS_MICRO;
+		if (!ret)
+			ret = IIO_VAL_INT_PLUS_MICRO;
+		break;
 	default:
-		return -EINVAL;
+		ret = -EINVAL;
 	}
+	mutex_unlock(&data->lock);
 
-	return 0;
+	return ret;
 }
 
 static int bmi160_write_raw(struct iio_dev *indio_dev,
@@ -479,19 +485,24 @@ static int bmi160_write_raw(struct iio_dev *indio_dev,
 			    int val, int val2, long mask)
 {
 	struct bmi160_data *data = iio_priv(indio_dev);
+	int result;
 
+	mutex_lock(&data->lock);
 	switch (mask) {
 	case IIO_CHAN_INFO_SCALE:
-		return bmi160_set_scale(data,
+		result = bmi160_set_scale(data,
 					bmi160_to_sensor(chan->type), val2);
+		break;
 	case IIO_CHAN_INFO_SAMP_FREQ:
-		return bmi160_set_odr(data, bmi160_to_sensor(chan->type),
+		result = bmi160_set_odr(data, bmi160_to_sensor(chan->type),
 				      val, val2);
+		break;
 	default:
-		return -EINVAL;
+		result = -EINVAL;
 	}
+	mutex_unlock(&data->lock);
 
-	return 0;
+	return result;
 }
 
 static
@@ -838,6 +849,7 @@ int bmi160_core_probe(struct device *dev, struct regmap *regmap,
 		return -ENOMEM;
 
 	data = iio_priv(indio_dev);
+	mutex_init(&data->lock);
 	dev_set_drvdata(dev, indio_dev);
 	data->regmap = regmap;
 
#2 lore raw
Quoted 63 lines · On Tue, 19 Jan 2021 06:54:45 -0800 Tom Rix <[email protected]> wrote:
Quoted 56 lines · Guoqing Chi

From: chiguoqing <[email protected]>

Adding mutex_lock, when read and write reg need to use this lock to avoid race.

---
v2:Follow write function to fix read function. Adding mutex init in core probe function. Adding break in switch case at read and write function.

 drivers/iio/imu/bmi160/bmi160.h      |  2 ++
 drivers/iio/imu/bmi160/bmi160_core.c | 34

+++++++++++++++++++--------- 2 files changed, 25 insertions(+), 11 deletions(-)

(quoted diff) +0 -0

b/drivers/iio/imu/bmi160/bmi160.h index 32c2ea2d7112..0c189a8b5b53 100644 --- a/drivers/iio/imu/bmi160/bmi160.h
+++ b/drivers/iio/imu/bmi160/bmi160.h

(quoted diff) +2 -0
@@ -3,9 +3,11 @@
 #define BMI160_H_
 
 #include <linux/iio/iio.h>
+#include <linux/mutex.h>
 #include <linux/regulator/consumer.h>
 
 struct bmi160_data {
+	struct mutex lock;
 	struct regmap *regmap;
 	struct iio_trigger *trig;
 	struct regulator_bulk_data supplies[2];
(quoted diff) +0 -0

b/drivers/iio/imu/bmi160/bmi160_core.c index 290b5ef83f77..e303378f4841 100644 --- a/drivers/iio/imu/bmi160/bmi160_core.c +++ b/drivers/iio/imu/bmi160/bmi160_core.c @@ -452,26 +452,32 @@ static int bmi160_read_raw(struct iio_dev *indio_dev, int ret;

(quoted diff) +1 -0
 	struct bmi160_data *data = iio_priv(indio_dev);
 
+	mutex_lock(&data->lock);
 	switch (mask) {
 	case IIO_CHAN_INFO_RAW:
 		ret = bmi160_get_data(data, chan->type,

chan->channel2, val);

(quoted diff) +3 -3
-		if (ret)
-			return ret;
-		return IIO_VAL_INT;
+		if (!ret)
+			ret = IIO_VAL_INT;
+		break;
 	case IIO_CHAN_INFO_SCALE:
 		*val = 0;
 		ret = bmi160_get_scale(data,
 				       bmi160_to_sensor(chan->type),

val2);

Review

Quoted 3 lines
(quoted diff) +2 -1
-		return ret ? ret : IIO_VAL_INT_PLUS_MICRO;
+		if (!ret)
+			ret = IIO_VAL_INT_PLUS_MICRO;  

Looking better, another question..

Quoted 6 lines

Why does the write() function return the results directly while the read() function

translates them to other values ?

Tom

It is original design in this driver. In order to differentiate raw to scale and SAMP_FREQ, while the scale and SAMP_FREQ are needless. I think log information can be added for this purpose, and return results directly.
It is not change the return values for my modify.It's best to keep the original design.Is that all right?

Guoqing Chi

Quoted 59 lines
Quoted 59 lines
(quoted diff) +1 -0
+		break;
 	case IIO_CHAN_INFO_SAMP_FREQ:
 		ret = bmi160_get_odr(data,

bmi160_to_sensor(chan->type), val, val2);

(quoted diff) +15 -7
-		return ret ? ret : IIO_VAL_INT_PLUS_MICRO;
+		if (!ret)
+			ret = IIO_VAL_INT_PLUS_MICRO;
+		break;
 	default:
-		return -EINVAL;
+		ret = -EINVAL;
 	}
+	mutex_unlock(&data->lock);
 
-	return 0;
+	return ret;
 }
 
 static int bmi160_write_raw(struct iio_dev *indio_dev,
@@ -479,19 +485,24 @@ static int bmi160_write_raw(struct iio_dev
 *indio_dev, int val, int val2, long mask)
 {
 	struct bmi160_data *data = iio_priv(indio_dev);
+	int result;
 
+	mutex_lock(&data->lock);
 	switch (mask) {
 	case IIO_CHAN_INFO_SCALE:
-		return bmi160_set_scale(data,
+		result = bmi160_set_scale(data,
 					bmi160_to_sensor(chan->type),
 val2);
+		break;
 	case IIO_CHAN_INFO_SAMP_FREQ:
-		return bmi160_set_odr(data,
 bmi160_to_sensor(chan->type),
+		result = bmi160_set_odr(data,
 bmi160_to_sensor(chan->type), val, val2);
+		break;
 	default:
-		return -EINVAL;
+		result = -EINVAL;
 	}
+	mutex_unlock(&data->lock);
 
-	return 0;
+	return result;
 }

static

(quoted diff) +1 -0
@@ -838,6 +849,7 @@ int bmi160_core_probe(struct device *dev,
 struct regmap *regmap, return -ENOMEM;
 
 	data = iio_priv(indio_dev);
+	mutex_init(&data->lock);
 	dev_set_drvdata(dev, indio_dev);
 	data->regmap = regmap;
 
author Reviewed-by
#3 lore raw
Quoted 70 lines · Guoqing Chi
Quoted 67 lines · On Tue, 19 Jan 2021 06:54:45 -0800 Tom Rix <[email protected]> wrote:
Quoted 56 lines · Guoqing Chi

From: chiguoqing <[email protected]>

Adding mutex_lock, when read and write reg need to use this lock to avoid race.

---
v2:Follow write function to fix read function. Adding mutex init in core probe function. Adding break in switch case at read and write function.

 drivers/iio/imu/bmi160/bmi160.h      |  2 ++
 drivers/iio/imu/bmi160/bmi160_core.c | 34

+++++++++++++++++++--------- 2 files changed, 25 insertions(+), 11 deletions(-)

(quoted diff) +0 -0

b/drivers/iio/imu/bmi160/bmi160.h index 32c2ea2d7112..0c189a8b5b53 100644 --- a/drivers/iio/imu/bmi160/bmi160.h
+++ b/drivers/iio/imu/bmi160/bmi160.h

(quoted diff) +2 -0
@@ -3,9 +3,11 @@
 #define BMI160_H_
 
 #include <linux/iio/iio.h>
+#include <linux/mutex.h>
 #include <linux/regulator/consumer.h>
 
 struct bmi160_data {
+	struct mutex lock;
 	struct regmap *regmap;
 	struct iio_trigger *trig;
 	struct regulator_bulk_data supplies[2];
(quoted diff) +0 -0

b/drivers/iio/imu/bmi160/bmi160_core.c index 290b5ef83f77..e303378f4841 100644 --- a/drivers/iio/imu/bmi160/bmi160_core.c +++ b/drivers/iio/imu/bmi160/bmi160_core.c @@ -452,26 +452,32 @@ static int bmi160_read_raw(struct iio_dev *indio_dev, int ret;

(quoted diff) +1 -0
 	struct bmi160_data *data = iio_priv(indio_dev);
 
+	mutex_lock(&data->lock);
 	switch (mask) {
 	case IIO_CHAN_INFO_RAW:
 		ret = bmi160_get_data(data, chan->type,

chan->channel2, val);

(quoted diff) +3 -3
-		if (ret)
-			return ret;
-		return IIO_VAL_INT;
+		if (!ret)
+			ret = IIO_VAL_INT;
+		break;
 	case IIO_CHAN_INFO_SCALE:
 		*val = 0;
 		ret = bmi160_get_scale(data,
 				       bmi160_to_sensor(chan->type),

val2);

Review

Quoted 3 lines
(quoted diff) +2 -1
-		return ret ? ret : IIO_VAL_INT_PLUS_MICRO;
+		if (!ret)
+			ret = IIO_VAL_INT_PLUS_MICRO;  

Looking better, another question..

Why does the write() function return the results directly while the read() function

translates them to other values ?

Quoted 7 lines
Quoted 1 line

Tom

It is original design in this driver. In order to differentiate raw to scale and SAMP_FREQ, while the scale and SAMP_FREQ are needless. I think log information can be added for this purpose, and return results directly.
It is not change the return values for my modify.It's best to keep the original design.Is that all right?

Ok.

Quoted 59 lines

Guoqing Chi

Quoted 58 lines
Quoted 58 lines
(quoted diff) +1 -0
+		break;
 	case IIO_CHAN_INFO_SAMP_FREQ:
 		ret = bmi160_get_odr(data,

bmi160_to_sensor(chan->type), val, val2);

(quoted diff) +15 -7
-		return ret ? ret : IIO_VAL_INT_PLUS_MICRO;
+		if (!ret)
+			ret = IIO_VAL_INT_PLUS_MICRO;
+		break;
 	default:
-		return -EINVAL;
+		ret = -EINVAL;
 	}
+	mutex_unlock(&data->lock);
 
-	return 0;
+	return ret;
 }
 
 static int bmi160_write_raw(struct iio_dev *indio_dev,
@@ -479,19 +485,24 @@ static int bmi160_write_raw(struct iio_dev
 *indio_dev, int val, int val2, long mask)
 {
 	struct bmi160_data *data = iio_priv(indio_dev);
+	int result;
 
+	mutex_lock(&data->lock);
 	switch (mask) {
 	case IIO_CHAN_INFO_SCALE:
-		return bmi160_set_scale(data,
+		result = bmi160_set_scale(data,
 					bmi160_to_sensor(chan->type),
 val2);
+		break;
 	case IIO_CHAN_INFO_SAMP_FREQ:
-		return bmi160_set_odr(data,
 bmi160_to_sensor(chan->type),
+		result = bmi160_set_odr(data,
 bmi160_to_sensor(chan->type), val, val2);
+		break;
 	default:
-		return -EINVAL;
+		result = -EINVAL;
 	}
+	mutex_unlock(&data->lock);
 
-	return 0;
+	return result;
 }

static

(quoted diff) +1 -0
@@ -838,6 +849,7 @@ int bmi160_core_probe(struct device *dev,
 struct regmap *regmap, return -ENOMEM;
 
 	data = iio_priv(indio_dev);
+	mutex_init(&data->lock);
 	dev_set_drvdata(dev, indio_dev);
 	data->regmap = regmap;
 
maintainer
#4 lore raw
Quoted 79 lines · On Wed, 20 Jan 2021 07:06:23 -0800 Tom Rix <[email protected]> wrote:
Quoted 70 lines · Guoqing Chi
Quoted 67 lines · On Tue, 19 Jan 2021 06:54:45 -0800 Tom Rix <[email protected]> wrote:
Quoted 56 lines · Guoqing Chi

From: chiguoqing <[email protected]>

Adding mutex_lock, when read and write reg need to use this lock to avoid race.

---
v2:Follow write function to fix read function. Adding mutex init in core probe function. Adding break in switch case at read and write function.

 drivers/iio/imu/bmi160/bmi160.h      |  2 ++
 drivers/iio/imu/bmi160/bmi160_core.c | 34

+++++++++++++++++++--------- 2 files changed, 25 insertions(+), 11 deletions(-)

(quoted diff) +0 -0

b/drivers/iio/imu/bmi160/bmi160.h index 32c2ea2d7112..0c189a8b5b53 100644 --- a/drivers/iio/imu/bmi160/bmi160.h
+++ b/drivers/iio/imu/bmi160/bmi160.h

(quoted diff) +2 -0
@@ -3,9 +3,11 @@
 #define BMI160_H_
 
 #include <linux/iio/iio.h>
+#include <linux/mutex.h>
 #include <linux/regulator/consumer.h>
 
 struct bmi160_data {
+	struct mutex lock;
 	struct regmap *regmap;
 	struct iio_trigger *trig;
 	struct regulator_bulk_data supplies[2];
(quoted diff) +0 -0

b/drivers/iio/imu/bmi160/bmi160_core.c index 290b5ef83f77..e303378f4841 100644 --- a/drivers/iio/imu/bmi160/bmi160_core.c +++ b/drivers/iio/imu/bmi160/bmi160_core.c @@ -452,26 +452,32 @@ static int bmi160_read_raw(struct iio_dev *indio_dev, int ret;

(quoted diff) +1 -0
 	struct bmi160_data *data = iio_priv(indio_dev);
 
+	mutex_lock(&data->lock);
 	switch (mask) {
 	case IIO_CHAN_INFO_RAW:
 		ret = bmi160_get_data(data, chan->type,

chan->channel2, val);

(quoted diff) +3 -3
-		if (ret)
-			return ret;
-		return IIO_VAL_INT;
+		if (!ret)
+			ret = IIO_VAL_INT;
+		break;
 	case IIO_CHAN_INFO_SCALE:
 		*val = 0;
 		ret = bmi160_get_scale(data,
 				       bmi160_to_sensor(chan->type),

val2);

Review

Quoted 3 lines
(quoted diff) +2 -1
-		return ret ? ret : IIO_VAL_INT_PLUS_MICRO;
+		if (!ret)
+			ret = IIO_VAL_INT_PLUS_MICRO;    

Looking better, another question..

Why does the write() function return the results directly while the read() function

translates them to other values ?

Quoted 7 lines
Quoted 1 line

Tom

It is original design in this driver. In order to differentiate raw to scale and SAMP_FREQ, while the scale and SAMP_FREQ are needless. I think log information can be added for this purpose, and return results directly.
It is not change the return values for my modify.It's best to keep the original design.Is that all right?

Quoted 3 lines

Ok.

Hi Guoqing Chi,

For some reason the original patch email (start of this thread) never made it to my inbox or indeed the archive at lore.kernel.org.

Please resend (picking up Tom's reviewed by) and make sure to cc [email protected] + [email protected]

Then check if they make it to lore.kernel.org as that should highlight any issues where it is getting blocked etc.

Thanks,

Jonathan

Quoted 61 lines
Quoted 60 lines

Guoqing Chi

Quoted 58 lines
Quoted 58 lines
(quoted diff) +1 -0
+		break;
 	case IIO_CHAN_INFO_SAMP_FREQ:
 		ret = bmi160_get_odr(data,

bmi160_to_sensor(chan->type), val, val2);

(quoted diff) +15 -7
-		return ret ? ret : IIO_VAL_INT_PLUS_MICRO;
+		if (!ret)
+			ret = IIO_VAL_INT_PLUS_MICRO;
+		break;
 	default:
-		return -EINVAL;
+		ret = -EINVAL;
 	}
+	mutex_unlock(&data->lock);
 
-	return 0;
+	return ret;
 }
 
 static int bmi160_write_raw(struct iio_dev *indio_dev,
@@ -479,19 +485,24 @@ static int bmi160_write_raw(struct iio_dev
 *indio_dev, int val, int val2, long mask)
 {
 	struct bmi160_data *data = iio_priv(indio_dev);
+	int result;
 
+	mutex_lock(&data->lock);
 	switch (mask) {
 	case IIO_CHAN_INFO_SCALE:
-		return bmi160_set_scale(data,
+		result = bmi160_set_scale(data,
 					bmi160_to_sensor(chan->type),
 val2);
+		break;
 	case IIO_CHAN_INFO_SAMP_FREQ:
-		return bmi160_set_odr(data,
 bmi160_to_sensor(chan->type),
+		result = bmi160_set_odr(data,
 bmi160_to_sensor(chan->type), val, val2);
+		break;
 	default:
-		return -EINVAL;
+		result = -EINVAL;
 	}
+	mutex_unlock(&data->lock);
 
-	return 0;
+	return result;
 }

static

(quoted diff) +1 -0
@@ -838,6 +849,7 @@ int bmi160_core_probe(struct device *dev,
 struct regmap *regmap, return -ENOMEM;
 
 	data = iio_priv(indio_dev);
+	mutex_init(&data->lock);
 	dev_set_drvdata(dev, indio_dev);
 	data->regmap = regmap;
 
#5 lore raw
Quoted 94 lines · On Sat, 23 Jan 2021 15:26:59 +0000 Jonathan Cameron <[email protected]> wrote:
Quoted 79 lines · On Wed, 20 Jan 2021 07:06:23 -0800 Tom Rix <[email protected]> wrote:
Quoted 70 lines · Guoqing Chi
Quoted 67 lines · On Tue, 19 Jan 2021 06:54:45 -0800 Tom Rix <[email protected]> wrote:
Quoted 56 lines · Guoqing Chi

From: chiguoqing <[email protected]>

Adding mutex_lock, when read and write reg need to use this lock to avoid race.

---
v2:Follow write function to fix read function. Adding mutex init in core probe function. Adding break in switch case at read and write function.

 drivers/iio/imu/bmi160/bmi160.h      |  2 ++
 drivers/iio/imu/bmi160/bmi160_core.c | 34

+++++++++++++++++++--------- 2 files changed, 25 insertions(+), 11 deletions(-)

(quoted diff) +0 -0

b/drivers/iio/imu/bmi160/bmi160.h index 32c2ea2d7112..0c189a8b5b53 100644 --- a/drivers/iio/imu/bmi160/bmi160.h +++ b/drivers/iio/imu/bmi160/bmi160.h @@ -3,9 +3,11 @@
#define BMI160_H_

#include <linux/iio/iio.h>

(quoted diff) +2 -0
+#include <linux/mutex.h>
 #include <linux/regulator/consumer.h>
 
 struct bmi160_data {
+	struct mutex lock;
 	struct regmap *regmap;
 	struct iio_trigger *trig;
 	struct regulator_bulk_data supplies[2];
(quoted diff) +0 -0

b/drivers/iio/imu/bmi160/bmi160_core.c index 290b5ef83f77..e303378f4841 100644 --- a/drivers/iio/imu/bmi160/bmi160_core.c +++ b/drivers/iio/imu/bmi160/bmi160_core.c @@ -452,26 +452,32 @@ static int bmi160_read_raw(struct iio_dev *indio_dev, int ret;

(quoted diff) +1 -0
 	struct bmi160_data *data = iio_priv(indio_dev);
 
+	mutex_lock(&data->lock);
 	switch (mask) {
 	case IIO_CHAN_INFO_RAW:
 		ret = bmi160_get_data(data, chan->type,

chan->channel2, val);

(quoted diff) +3 -3
-		if (ret)
-			return ret;
-		return IIO_VAL_INT;
+		if (!ret)
+			ret = IIO_VAL_INT;
+		break;
 	case IIO_CHAN_INFO_SCALE:
 		*val = 0;
 		ret = bmi160_get_scale(data,
 				       bmi160_to_sensor(chan->type),

val2);

Review

Quoted 3 lines
(quoted diff) +2 -1
-		return ret ? ret : IIO_VAL_INT_PLUS_MICRO;
+		if (!ret)
+			ret = IIO_VAL_INT_PLUS_MICRO;      

Looking better, another question..

Why does the write() function return the results directly while the read() function

translates them to other values ?

Quoted 7 lines
Quoted 1 line

Tom

It is original design in this driver. In order to differentiate raw to scale and SAMP_FREQ, while the scale and SAMP_FREQ are needless. I think log information can be added for this purpose, and return results directly.
It is not change the return values for my modify.It's best to keep the original design.Is that all right?

Quoted 3 lines

Ok.

Hi Guoqing Chi,

For some reason the original patch email (start of this thread) never made it to my inbox or indeed the archive at lore.kernel.org.

Please resend (picking up Tom's reviewed by) and make sure to cc [email protected] + [email protected]

Quoted 6 lines

Then check if they make it to lore.kernel.org as that should highlight any issues where it is getting blocked etc.

Thanks,

Jonathan

Hi Jonathan,

I have resend patch V2(picking up Tom's reviewed by) for add [email protected] + [email protected].

Thanks.

Quoted 62 lines
Quoted 62 lines
Quoted 61 lines

Guoqing Chi

Quoted 58 lines
Quoted 58 lines
(quoted diff) +1 -0
+		break;
 	case IIO_CHAN_INFO_SAMP_FREQ:
 		ret = bmi160_get_odr(data,

bmi160_to_sensor(chan->type), val, val2);

(quoted diff) +15 -7
-		return ret ? ret : IIO_VAL_INT_PLUS_MICRO;
+		if (!ret)
+			ret = IIO_VAL_INT_PLUS_MICRO;
+		break;
 	default:
-		return -EINVAL;
+		ret = -EINVAL;
 	}
+	mutex_unlock(&data->lock);
 
-	return 0;
+	return ret;
 }
 
 static int bmi160_write_raw(struct iio_dev *indio_dev,
@@ -479,19 +485,24 @@ static int bmi160_write_raw(struct iio_dev
 *indio_dev, int val, int val2, long mask)
 {
 	struct bmi160_data *data = iio_priv(indio_dev);
+	int result;
 
+	mutex_lock(&data->lock);
 	switch (mask) {
 	case IIO_CHAN_INFO_SCALE:
-		return bmi160_set_scale(data,
+		result = bmi160_set_scale(data,
 					bmi160_to_sensor(chan->type),
 val2);
+		break;
 	case IIO_CHAN_INFO_SAMP_FREQ:
-		return bmi160_set_odr(data,
 bmi160_to_sensor(chan->type),
+		result = bmi160_set_odr(data,
 bmi160_to_sensor(chan->type), val, val2);
+		break;
 	default:
-		return -EINVAL;
+		result = -EINVAL;
 	}
+	mutex_unlock(&data->lock);
 
-	return 0;
+	return result;
 }

static

(quoted diff) +1 -0
@@ -838,6 +849,7 @@ int bmi160_core_probe(struct device *dev,
 struct regmap *regmap, return -ENOMEM;
 
 	data = iio_priv(indio_dev);
+	mutex_init(&data->lock);
 	dev_set_drvdata(dev, indio_dev);
 	data->regmap = regmap;