Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp24502pxk; Tue, 15 Sep 2020 16:42:34 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwD9st0tIMuPFoNtNpkI/3U4JIhRcsOu+2c6ws1KifFe3GHTjxzMLeAelMzudKOmInxXVDA X-Received: by 2002:a17:906:9346:: with SMTP id p6mr22003846ejw.305.1600213353921; Tue, 15 Sep 2020 16:42:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1600213353; cv=none; d=google.com; s=arc-20160816; b=K9K3wW8PEue5mI4gGF0Z5T4Fb4PmjdImuonL40Dmrnz86jJF+HALYrOd8nAHI35IOT L9Bz8c7Eo3MC3yDBwSLHPopOP7DsYSwPrAuCs4CoKoFAuUH5fdhjMG6MiaOLsid3eJS/ ojZCnZWc1NPqgxVv1iuwFL3LDaeTPw7zgso3eUxI8e9HqszC0tsrr5ljgMWNKDWM0lUf QJij29Y/jiznftIPVACI1CVBpWU4dkTQfCpLKb9GroHvxf8YL2bH/PepO+8ETJTTAAjR bH6+cc+f11trlMVCejZt8Tz+M/0s4qp+jayV8y1V9z4e0AOTPX0n3GBZoSuKKBgUK3Qm pZ4g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=I46WTV7DnAnhgnOpLONdDwcaAndY6tt2AofVvvZkMzc=; b=tCiMAdHsWdNANvsJoAQuQMY29BCFTo3tVu/lhNVVz2fapKa/a6AYKTmlZf1v6MirKj qzp8LMlkTb9mZVV/m5US0LTqYFsCzUda4Kf3GPtxGCk4ZWKOlV/3uJyTYFzImjCfM9aO 9wYgj6YoFIz5YlWHhxLQTOd+3qyOCRViD5pkZOOlB2gPmS66o4PVwq2WUgrL61xcyP6f 8QTZzYMxBTzrOZD823+P9epNbjFjYL25cFQ/oL28CofKM7WIsGOAuZ6QaV+X7rtdHk1r 2W4yukt7hr6vlJS1nZQWx7NYUqFFXW4Sae814pzUbz7pb/IqStVkUeuq6pUlh57zSR2w K1mw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="cD4f/N/6"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id b7si10395189edr.520.2020.09.15.16.42.12; Tue, 15 Sep 2020 16:42:33 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="cD4f/N/6"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727140AbgIOXlO (ORCPT + 99 others); Tue, 15 Sep 2020 19:41:14 -0400 Received: from mail.kernel.org ([198.145.29.99]:46442 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727112AbgIOOem (ORCPT ); Tue, 15 Sep 2020 10:34:42 -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 6550E2222B; Tue, 15 Sep 2020 14:16:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1600179386; bh=Dz3z0d97SlQA4zKA/Bynm64n63535f8ww6CFV3dTdAk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=cD4f/N/68sJwTLIbwA+fks+ERMJSHD5JN+PwHD3BSeaGC+/pNK3NfI0xYQI+gTdNI uTkwUkzx5ws3wLXyexsYxgaZFmAhWxOpANNrjPc14M8BefVElFl6aoce1KHhTjHsEQ nTDq3M45QwZKKgUOioO18cCm3qtnPmztDCitHVlA= 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 4.19 40/78] iio: adc: mcp3422: fix locking scope Date: Tue, 15 Sep 2020 16:13:05 +0200 Message-Id: <20200915140635.594299228@linuxfoundation.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200915140633.552502750@linuxfoundation.org> References: <20200915140633.552502750@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@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 @@ -99,16 +99,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; } @@ -141,6 +137,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; @@ -155,7 +153,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,