Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp620078iog; Mon, 13 Jun 2022 09:16:57 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzmMdE5VsQsdDyvqnOAq/KQ1kcQrJmWtEuNf3eAXVnGiJfLvz8NypcyHU2Y5nmebje2nd1R X-Received: by 2002:a63:7c2:0:b0:3fc:6f1f:d12 with SMTP id 185-20020a6307c2000000b003fc6f1f0d12mr345074pgh.372.1655137017246; Mon, 13 Jun 2022 09:16:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655137017; cv=none; d=google.com; s=arc-20160816; b=X1nu+OZIq7wJS2E7FL58p7SeunZTPh044lq/9q86+G9E+VWvLyqgSQrPpCvwMMszOc pYizPsiQecmHKB/N2FDtN17mPB+LK/M4wlV9FeNc++QlB8yLXfSiHi4xsUBCxE/qGu6P 9bmOxyHsVvDVAOFGPAyhpcKDwQT4wIggPvVeB4p4N/9oJ0dtZ9ykgwBnRqwt4t2HXKy4 GOXwc3qW8kX/MBoolG79Z7blTjR9pbXVK7PlOjfS8mP4hiPGn5aVFqby8H5gZ2fe7Ii2 MVl+9iVI8kcHt6vThXfRVEtmCa78EZ0DZUUThxbHxT/vu4Y9cCJ4Jtfbi1MhyoXGUCEm aNRA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=wztqcmOeU791kX4B//Wc8BElfNf+KHMBVpvdwi4CPgs=; b=wR56d99fqahxixI4ks3cZCau+r3ZA33jNGVaEJcpiMCD7jH8JARMIzDfcWMSbY8OGP V9w5OnbfGBzPtktk2PxreIvZMx8iythSCcI4poQ7ySkz2fDwxo7cuywSMjculgw2qhSL 5zN1KP4aVqtcFM4+XzNG1DhiC3pJ1shmkmAVTVrgYGxoduJ4hocswqTPmoNROGVDMTKn WIMjaXv/bJMlGJhoiVIo16zWd//GN3r0/156OW8J81sCmWcUzS0TWN841s43V9q+R+ew cExmWEyTl0yU1mz5zSeMkkjHpcDXcB16WiF0T6iZfEiDDxfs2NhqdWl7gIIRnAC5QpM0 Ie1A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=tH34Jaem; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id m9-20020a17090a2c0900b001e299f85017si14203653pjd.63.2022.06.13.09.16.43; Mon, 13 Jun 2022 09:16:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=tH34Jaem; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1356275AbiFMLuN (ORCPT + 99 others); Mon, 13 Jun 2022 07:50:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41106 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1356335AbiFMLoT (ORCPT ); Mon, 13 Jun 2022 07:44:19 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E5C4C46CA6; Mon, 13 Jun 2022 03:50:37 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id C769BB80E59; Mon, 13 Jun 2022 10:50:28 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 065C6C34114; Mon, 13 Jun 2022 10:50:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1655117427; bh=E4J8qHqnbKH9RWVVx26+7JoGtwsNVBNGUHnQObsUXp4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=tH34JaemqqUTkdlylgiSaM5N8wkvJTTbc1fSj+fWiO8ri/xmUl07GqPT2r/hIMD/6 J8tWzFJ5Mtsgo+efPegd1AcTeMc/uX6UTFuqBsf4IPMfxX7zD4995a/9K1K6f9I0Vm 5cx5l53QS1G1TvmvLW8HcIanPBRHHTBEorncZSIc= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Jonathan Cameron , Denis Ciocca , Miquel Raynal , Jonathan Cameron , Sasha Levin Subject: [PATCH 5.4 366/411] iio: st_sensors: Add a local lock for protecting odr Date: Mon, 13 Jun 2022 12:10:39 +0200 Message-Id: <20220613094939.662327712@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220613094928.482772422@linuxfoundation.org> References: <20220613094928.482772422@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-8.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. 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 364683783ae5..c25b0bc89b0c 100644 --- a/drivers/iio/common/st_sensors/st_sensors_core.c +++ b/drivers/iio/common/st_sensors/st_sensors_core.c @@ -76,16 +76,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) && @@ -108,7 +110,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); @@ -384,6 +388,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_of_probe(indio_dev->dev.parent, pdata); if (of_pdata) @@ -575,18 +581,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 686be532f4cb..7816bf070f83 100644 --- a/include/linux/iio/common/st_sensors.h +++ b/include/linux/iio/common/st_sensors.h @@ -228,6 +228,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; @@ -253,6 +254,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