From: Miquel Raynal <miquel.raynal@bootlin.com>
To: linux-iio@vger.kernel.org, Jonathan Cameron <jic23@kernel.org>,
Lars-Peter Clausen <lars@metafoo.de>
Cc: Thomas Petazzoni <thomas.petazzoni@bootlin.com>,
Alexandru Ardelean <ardeleanalex@gmail.com>,
Miquel Raynal <miquel.raynal@bootlin.com>,
Denis Ciocca <denis.ciocca@st.com>
Subject: [PATCH v3 06/13] iio: st_sensors: Add a local lock for protecting odr
Date: Mon, 7 Feb 2022 15:38:33 +0100 [thread overview]
Message-ID: <20220207143840.707510-7-miquel.raynal@bootlin.com> (raw)
In-Reply-To: <20220207143840.707510-1-miquel.raynal@bootlin.com>
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>
---
.../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 6c027150a5a4..665584e8bea4 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(st_sensors_set_odr);
@@ -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.27.0
next prev parent reply other threads:[~2022-02-07 14:59 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-02-07 14:38 [PATCH v3 00/13] Miscellaneous IIO core enhancements Miquel Raynal
2022-02-07 14:38 ` [PATCH v3 01/13] iio: core: Enhance the kernel doc of modes and currentmodes iio_dev entries Miquel Raynal
2022-02-07 14:38 ` [PATCH v3 02/13] iio: magnetometer: rm3100: Stop abusing the ->currentmode Miquel Raynal
2022-02-07 14:38 ` [PATCH v3 03/13] iio: adc: stm32-dfsdm: Avoid dereferencing ->currentmode Miquel Raynal
2022-02-07 14:38 ` [PATCH v3 04/13] iio: st_sensors: Return as early as possible from the _write_raw() callbacks Miquel Raynal
2022-02-07 14:38 ` [PATCH v3 05/13] iio: st_sensors: Drop the protection on _avail functions Miquel Raynal
2022-02-07 14:38 ` Miquel Raynal [this message]
2022-02-07 14:38 ` [PATCH v3 07/13] iio: st_sensors: Stop abusing mlock to ensure internal coherency Miquel Raynal
2022-02-07 14:38 ` [PATCH v3 08/13] iio: st_sensors: Use iio_device_claim/release_direct_mode() when relevant Miquel Raynal
2022-02-07 14:38 ` [PATCH v3 09/13] iio: Un-inline iio_buffer_enabled() Miquel Raynal
2022-02-07 14:38 ` [PATCH v3 10/13] iio: core: Hide read accesses to iio_dev->currentmode Miquel Raynal
2022-02-07 14:38 ` [PATCH v3 11/13] iio: core: Move the currentmode entry to the opaque structure Miquel Raynal
2022-02-07 14:38 ` [PATCH v3 12/13] iio: core: Simplify the registration of kfifo buffers Miquel Raynal
2022-02-07 14:38 ` [PATCH v3 13/13] iio: core: Clarify the modes Miquel Raynal
2022-02-13 18:42 ` Jonathan Cameron
2022-02-14 8:53 ` Miquel Raynal
2022-02-27 13:35 ` Jonathan Cameron
2022-03-15 15:44 ` Miquel Raynal
2022-04-05 8:02 ` Miquel Raynal
2022-04-10 15:27 ` Jonathan Cameron
2022-04-11 7:12 ` Miquel Raynal
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=20220207143840.707510-7-miquel.raynal@bootlin.com \
--to=miquel.raynal@bootlin.com \
--cc=ardeleanalex@gmail.com \
--cc=denis.ciocca@st.com \
--cc=jic23@kernel.org \
--cc=lars@metafoo.de \
--cc=linux-iio@vger.kernel.org \
--cc=thomas.petazzoni@bootlin.com \
/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.