From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5F395C433EF for ; Tue, 14 Jun 2022 14:05:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236775AbiFNOFk (ORCPT ); Tue, 14 Jun 2022 10:05:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48974 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235951AbiFNOFg (ORCPT ); Tue, 14 Jun 2022 10:05:36 -0400 Received: from frasgout.his.huawei.com (frasgout.his.huawei.com [185.176.79.56]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D0D063A724; Tue, 14 Jun 2022 07:05:34 -0700 (PDT) Received: from fraeml735-chm.china.huawei.com (unknown [172.18.147.226]) by frasgout.his.huawei.com (SkyGuard) with ESMTP id 4LMqt66rQzz67ZGL; Tue, 14 Jun 2022 22:03:58 +0800 (CST) Received: from lhreml710-chm.china.huawei.com (10.201.108.61) by fraeml735-chm.china.huawei.com (10.206.15.216) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Tue, 14 Jun 2022 16:05:32 +0200 Received: from localhost (10.81.210.75) by lhreml710-chm.china.huawei.com (10.201.108.61) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Tue, 14 Jun 2022 15:05:31 +0100 Date: Tue, 14 Jun 2022 15:05:29 +0100 From: Jonathan Cameron To: Greg Kroah-Hartman CC: , , Jonathan Cameron , Denis Ciocca , Miquel Raynal , Sasha Levin Subject: Re: [PATCH 5.15 167/247] iio: st_sensors: Add a local lock for protecting odr Message-ID: <20220614150529.00004e13@Huawei.com> In-Reply-To: <20220613094928.023172711@linuxfoundation.org> References: <20220613094922.843438024@linuxfoundation.org> <20220613094928.023172711@linuxfoundation.org> Organization: Huawei Technologies Research and Development (UK) Ltd. X-Mailer: Claws Mail 4.0.0 (GTK+ 3.24.29; i686-w64-mingw32) MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit X-Originating-IP: [10.81.210.75] X-ClientProxiedBy: lhreml740-chm.china.huawei.com (10.201.108.190) To lhreml710-chm.china.huawei.com (10.201.108.61) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, 13 Jun 2022 12:11:09 +0200 Greg Kroah-Hartman wrote: > From: Miquel Raynal > > [ 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. This is harmless, but not a fix but rather part of a long running effort to abstract away the use of iio_dev->mlock. What's there before this isn't broken, but rather isn't the way we would do it today. Thanks, Jonathan > > Suggested-by: Jonathan Cameron > Cc: Denis Ciocca > Signed-off-by: Miquel Raynal > Link: https://lore.kernel.org/r/20220207143840.707510-7-miquel.raynal@bootlin.com > Signed-off-by: Jonathan Cameron > Signed-off-by: Sasha Levin > --- > .../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 0bbb090b108c..aff981551617 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); > @@ -365,6 +369,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)) > @@ -558,18 +564,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 8bdbaf3f3796..69f4a1f6b536 100644 > --- a/include/linux/iio/common/st_sensors.h > +++ b/include/linux/iio/common/st_sensors.h > @@ -238,6 +238,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 device *dev; > @@ -263,6 +264,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