From patchwork Tue Sep 15 14:13:00 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "gregkh@linuxfoundation.org" X-Patchwork-Id: 263949 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.7 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9E445C433E2 for ; Tue, 15 Sep 2020 23:21:41 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5BBA420936 for ; Tue, 15 Sep 2020 23:21:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1600212101; bh=RyxKfKkL6hk8jdH12QM0DQHAPNf5UTd9QXsh5XxXdNw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=y/eQvxKiUOka0KE/HjKAC13GEWAn9v1/0Phhp1ENUlG2sR89/hE1s3smZXNU9hczj wviEQzNQwkFZdr6T3sDE2fXH7oLBZuxXT8OSZ4XHfc8SzkvH2BUx8I2PvERSLpE7BQ tL7DW8vsVVl7zWIq9fk8nySFQbgvhKaALMy3eFdg= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727400AbgIOXVi (ORCPT ); Tue, 15 Sep 2020 19:21:38 -0400 Received: from mail.kernel.org ([198.145.29.99]:48430 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726926AbgIOOiG (ORCPT ); Tue, 15 Sep 2020 10:38:06 -0400 Received: from localhost (83-86-74-64.cable.dynamic.v4.ziggo.nl [83.86.74.64]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 5391C23C84; Tue, 15 Sep 2020 14:28:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1600180101; bh=RyxKfKkL6hk8jdH12QM0DQHAPNf5UTd9QXsh5XxXdNw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=XQvb/lg0+wHRijRTTpUhb+GYY0Bp6L767zPiwUF8bEVJOf+SY1vyfDtcHwDrR8A0v ww7X6iiAxOFKpzNiHCT4Z4KuzNCCkNntEMiYYKyl+z8uK+1K7YvYh8fKM/PZNJxJST hvzZ/Bsw1G1nOm4MgeDV0AQip1VrXZUq/TA5sRy4= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Angelo Compagnucci , Stable@vger.kernel.org, Jonathan Cameron Subject: [PATCH 5.8 109/177] iio: adc: mcp3422: fix locking scope Date: Tue, 15 Sep 2020 16:13:00 +0200 Message-Id: <20200915140658.870673113@linuxfoundation.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200915140653.610388773@linuxfoundation.org> References: <20200915140653.610388773@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Sender: stable-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org From: Angelo Compagnucci commit 3f1093d83d7164e4705e4232ccf76da54adfda85 upstream. Locking should be held for the entire reading sequence involving setting the channel, waiting for the channel switch and reading from the channel. If not, reading from a channel can result mixing with the reading from another channel. Fixes: 07914c84ba30 ("iio: adc: Add driver for Microchip MCP3422/3/4 high resolution ADC") Signed-off-by: Angelo Compagnucci Link: https://lore.kernel.org/r/20200819075525.1395248-1-angelo.compagnucci@gmail.com Cc: Signed-off-by: Jonathan Cameron Signed-off-by: Greg Kroah-Hartman --- drivers/iio/adc/mcp3422.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) --- a/drivers/iio/adc/mcp3422.c +++ b/drivers/iio/adc/mcp3422.c @@ -96,16 +96,12 @@ static int mcp3422_update_config(struct { int ret; - mutex_lock(&adc->lock); - ret = i2c_master_send(adc->i2c, &newconfig, 1); if (ret > 0) { adc->config = newconfig; ret = 0; } - mutex_unlock(&adc->lock); - return ret; } @@ -138,6 +134,8 @@ static int mcp3422_read_channel(struct m u8 config; u8 req_channel = channel->channel; + mutex_lock(&adc->lock); + if (req_channel != MCP3422_CHANNEL(adc->config)) { config = adc->config; config &= ~MCP3422_CHANNEL_MASK; @@ -152,7 +150,11 @@ static int mcp3422_read_channel(struct m msleep(mcp3422_read_times[MCP3422_SAMPLE_RATE(adc->config)]); } - return mcp3422_read(adc, value, &config); + ret = mcp3422_read(adc, value, &config); + + mutex_unlock(&adc->lock); + + return ret; } static int mcp3422_read_raw(struct iio_dev *iio,