提交 71e8ccdd 编写于 作者: M Miquel Raynal 提交者: Zheng Zengkai

iio: st_sensors: Add a local lock for protecting odr

stable inclusion
from stable-v5.10.122
commit 7821d743abb301a916731a4294eba4927482e148
category: bugfix
bugzilla: https://gitee.com/openeuler/kernel/issues/I5W6OE

Reference: https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/?id=7821d743abb301a916731a4294eba4927482e148

--------------------------------

[ Upstream commit 47401012 ]

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: NJonathan Cameron <jic23@kernel.org>
Cc: Denis Ciocca <denis.ciocca@st.com>
Signed-off-by: NMiquel Raynal <miquel.raynal@bootlin.com>
Link: https://lore.kernel.org/r/20220207143840.707510-7-miquel.raynal@bootlin.comSigned-off-by: NJonathan Cameron <Jonathan.Cameron@huawei.com>
Signed-off-by: NSasha Levin <sashal@kernel.org>
Signed-off-by: NZheng Zengkai <zhengzengkai@huawei.com>
Reviewed-by: NWei Li <liwei391@huawei.com>
上级 03ddabb7
...@@ -70,16 +70,18 @@ static int st_sensors_match_odr(struct st_sensor_settings *sensor_settings, ...@@ -70,16 +70,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 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_odr_avl odr_out = {0, 0};
struct st_sensor_data *sdata = iio_priv(indio_dev); struct st_sensor_data *sdata = iio_priv(indio_dev);
mutex_lock(&sdata->odr_lock);
if (!sdata->sensor_settings->odr.mask) if (!sdata->sensor_settings->odr.mask)
return 0; goto unlock_mutex;
err = st_sensors_match_odr(sdata->sensor_settings, odr, &odr_out); err = st_sensors_match_odr(sdata->sensor_settings, odr, &odr_out);
if (err < 0) if (err < 0)
goto st_sensors_match_odr_error; goto unlock_mutex;
if ((sdata->sensor_settings->odr.addr == if ((sdata->sensor_settings->odr.addr ==
sdata->sensor_settings->pw.addr) && sdata->sensor_settings->pw.addr) &&
...@@ -102,7 +104,9 @@ int st_sensors_set_odr(struct iio_dev *indio_dev, unsigned int odr) ...@@ -102,7 +104,9 @@ int st_sensors_set_odr(struct iio_dev *indio_dev, unsigned int odr)
if (err >= 0) if (err >= 0)
sdata->odr = odr_out.hz; sdata->odr = odr_out.hz;
st_sensors_match_odr_error: unlock_mutex:
mutex_unlock(&sdata->odr_lock);
return err; return err;
} }
EXPORT_SYMBOL(st_sensors_set_odr); EXPORT_SYMBOL(st_sensors_set_odr);
...@@ -364,6 +368,8 @@ int st_sensors_init_sensor(struct iio_dev *indio_dev, ...@@ -364,6 +368,8 @@ int st_sensors_init_sensor(struct iio_dev *indio_dev,
struct st_sensors_platform_data *of_pdata; struct st_sensors_platform_data *of_pdata;
int err = 0; int err = 0;
mutex_init(&sdata->odr_lock);
/* If OF/DT pdata exists, it will take precedence of anything else */ /* If OF/DT pdata exists, it will take precedence of anything else */
of_pdata = st_sensors_dev_probe(indio_dev->dev.parent, pdata); of_pdata = st_sensors_dev_probe(indio_dev->dev.parent, pdata);
if (IS_ERR(of_pdata)) if (IS_ERR(of_pdata))
...@@ -557,18 +563,24 @@ int st_sensors_read_info_raw(struct iio_dev *indio_dev, ...@@ -557,18 +563,24 @@ int st_sensors_read_info_raw(struct iio_dev *indio_dev,
err = -EBUSY; err = -EBUSY;
goto out; goto out;
} else { } else {
mutex_lock(&sdata->odr_lock);
err = st_sensors_set_enable(indio_dev, true); err = st_sensors_set_enable(indio_dev, true);
if (err < 0) if (err < 0) {
mutex_unlock(&sdata->odr_lock);
goto out; goto out;
}
msleep((sdata->sensor_settings->bootime * 1000) / sdata->odr); msleep((sdata->sensor_settings->bootime * 1000) / sdata->odr);
err = st_sensors_read_axis_data(indio_dev, ch, val); err = st_sensors_read_axis_data(indio_dev, ch, val);
if (err < 0) if (err < 0) {
mutex_unlock(&sdata->odr_lock);
goto out; goto out;
}
*val = *val >> ch->scan_type.shift; *val = *val >> ch->scan_type.shift;
err = st_sensors_set_enable(indio_dev, false); err = st_sensors_set_enable(indio_dev, false);
mutex_unlock(&sdata->odr_lock);
} }
out: out:
mutex_unlock(&indio_dev->mlock); mutex_unlock(&indio_dev->mlock);
......
...@@ -228,6 +228,7 @@ struct st_sensor_settings { ...@@ -228,6 +228,7 @@ struct st_sensor_settings {
* @hw_irq_trigger: if we're using the hardware interrupt on the sensor. * @hw_irq_trigger: if we're using the hardware interrupt on the sensor.
* @hw_timestamp: Latest timestamp from the interrupt handler, when in use. * @hw_timestamp: Latest timestamp from the interrupt handler, when in use.
* @buffer_data: Data used by buffer part. * @buffer_data: Data used by buffer part.
* @odr_lock: Local lock for preventing concurrent ODR accesses/changes
*/ */
struct st_sensor_data { struct st_sensor_data {
struct device *dev; struct device *dev;
...@@ -253,6 +254,8 @@ struct st_sensor_data { ...@@ -253,6 +254,8 @@ struct st_sensor_data {
s64 hw_timestamp; s64 hw_timestamp;
char buffer_data[ST_SENSORS_MAX_BUFFER_SIZE] ____cacheline_aligned; char buffer_data[ST_SENSORS_MAX_BUFFER_SIZE] ____cacheline_aligned;
struct mutex odr_lock;
}; };
#ifdef CONFIG_IIO_BUFFER #ifdef CONFIG_IIO_BUFFER
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册