From patchwork Wed Oct 19 08:29:06 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg KH X-Patchwork-Id: 4747 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp214554wrs; Wed, 19 Oct 2022 02:13:09 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6x0HuPxx4I3wVynY0BvMcX3JNjprN89Y65DO05R5f2lTAwrmtzETzy5+Lf6DgCoQRHtEeJ X-Received: by 2002:aa7:c751:0:b0:459:b0e1:4ad7 with SMTP id c17-20020aa7c751000000b00459b0e14ad7mr6454466eds.193.1666170789142; Wed, 19 Oct 2022 02:13:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666170789; cv=none; d=google.com; s=arc-20160816; b=b6ePgb+z8kt7iXYxE/Z+yzkfP4EnZl3zaq9EJbujsRw+1eB0+7TCo5Jmkrw8q5PjEo 3kLWS40RiJtR0YzhSfPXOP0pMzUTm2V7XOhxtGJTuwAkyQ5/dt1VT0zgEDxg0tyg0mt8 ldq1U9039Q2VHHiK9H4TK/TF/RqjP16KHSRopfLe1HFx3/Pbek10OrSa1VpS/RWBZuxn MbyBiGUN6wl88NmTA+fpK+it+UqLDSJWafrH25blD3W7irQ2IuqSlsjMVekca+t3qwKl 4beQbxHgAzXR6Jbn3Zzbgu13CHGKUdGov6pOU3JCUytGLg88gl2tCUqg4YJXIcH85xqw hIPQ== 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=CJfo40uAJFoFUsTMnQqudgvlfD7j7e3H7H/cKCjgq+8=; b=FpboWU7scXRnc8LhJaAUJhHY8yiO10Ex241UsLDtXOzrcwFD1K0PLwiAcQOfJy/fxa hXPeScVKUovPvngO6CjFv3sGOjCrp6FE0MFjlsFoWKk64hSD7rBl1ieLuLpB+4936cgl mv3gs8dQCoKO9PnlpPYDQ75JO9g98CDVGUNAuEmok6t3vgBt6NMObTR3lVqZ7Cpk+jhZ 08NB11prbCbFKKTF5ViKSnExyFpwHdjNIjCshKHI1wf1pkhwB6uJvVij+In+PHFt0xII rceL7TC0ctFU+3dV/L+xC+gGM4bKSe63LgFVk6BdXHb8QMaPnlry+xbwe6my66lTk3R1 REOw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=nNLulaaa; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id hh6-20020a170906a94600b00781b67df7f6si13569065ejb.167.2022.10.19.02.12.23; Wed, 19 Oct 2022 02:13:09 -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=nNLulaaa; 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 S232460AbiJSJKk (ORCPT + 99 others); Wed, 19 Oct 2022 05:10:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34262 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232427AbiJSJH2 (ORCPT ); Wed, 19 Oct 2022 05:07:28 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3EFC23C8E7; Wed, 19 Oct 2022 01:59:54 -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 dfw.source.kernel.org (Postfix) with ESMTPS id 5933C61866; Wed, 19 Oct 2022 08:58:40 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6800EC433D6; Wed, 19 Oct 2022 08:58:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666169919; bh=L322BFkuLIvqiR/Get1l0XbzU8LuDpj1WX+7zRzp758=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=nNLulaaacM0rT4Q2dJ9lH5LwiAdrzgT/sb/0WCR35c/5RvVPtWt1SFMwB8nRNV+4Y FLl5xYm4NcKKcRz248VSdrhAzlMxN1Sqh3Y5MKrUBpqj6x0A7b1I71UtQe3QU6VZZL e9cK4NzdPT6iQYn9n17qwEFcL4/yf6ZvwlyBZWHM= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Claudiu Beznea , Jonathan Cameron , Sasha Levin Subject: [PATCH 6.0 459/862] iio: adc: at91-sama5d2_adc: lock around oversampling and sample freq Date: Wed, 19 Oct 2022 10:29:06 +0200 Message-Id: <20221019083310.272084498@linuxfoundation.org> X-Mailer: git-send-email 2.38.0 In-Reply-To: <20221019083249.951566199@linuxfoundation.org> References: <20221019083249.951566199@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 X-Spam-Status: No, score=-7.4 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 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747106701405253691?= X-GMAIL-MSGID: =?utf-8?q?1747106701405253691?= From: Claudiu Beznea [ Upstream commit 9780a23ed5a0a0a63683e078f576719a98d4fb70 ] .read_raw()/.write_raw() could be called asynchronously from user space or other in kernel drivers. Without locking on st->lock these could be called asynchronously while there is a conversion in progress. Read will be harmless but changing registers while conversion is in progress may lead to inconsistent results. Thus, to avoid this lock st->lock. Fixes: 27e177190891 ("iio:adc:at91_adc8xx: introduce new atmel adc driver") Fixes: 6794e23fa3fe ("iio: adc: at91-sama5d2_adc: add support for oversampling resolution") Signed-off-by: Claudiu Beznea Link: https://lore.kernel.org/r/20220803102855.2191070-4-claudiu.beznea@microchip.com Signed-off-by: Jonathan Cameron Signed-off-by: Sasha Levin --- drivers/iio/adc/at91-sama5d2_adc.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/drivers/iio/adc/at91-sama5d2_adc.c b/drivers/iio/adc/at91-sama5d2_adc.c index 08d1f806c839..3734ddc82952 100644 --- a/drivers/iio/adc/at91-sama5d2_adc.c +++ b/drivers/iio/adc/at91-sama5d2_adc.c @@ -1542,10 +1542,10 @@ static int at91_adc_read_info_raw(struct iio_dev *indio_dev, ret = at91_adc_read_position(st, chan->channel, &tmp_val); *val = tmp_val; - mutex_unlock(&st->lock); - iio_device_release_direct_mode(indio_dev); if (ret > 0) ret = at91_adc_adjust_val_osr(st, val); + mutex_unlock(&st->lock); + iio_device_release_direct_mode(indio_dev); return ret; } @@ -1558,10 +1558,10 @@ static int at91_adc_read_info_raw(struct iio_dev *indio_dev, ret = at91_adc_read_pressure(st, chan->channel, &tmp_val); *val = tmp_val; - mutex_unlock(&st->lock); - iio_device_release_direct_mode(indio_dev); if (ret > 0) ret = at91_adc_adjust_val_osr(st, val); + mutex_unlock(&st->lock); + iio_device_release_direct_mode(indio_dev); return ret; } @@ -1650,16 +1650,20 @@ static int at91_adc_write_raw(struct iio_dev *indio_dev, /* if no change, optimize out */ if (val == st->oversampling_ratio) return 0; + mutex_lock(&st->lock); st->oversampling_ratio = val; /* update ratio */ at91_adc_config_emr(st); + mutex_unlock(&st->lock); return 0; case IIO_CHAN_INFO_SAMP_FREQ: if (val < st->soc_info.min_sample_rate || val > st->soc_info.max_sample_rate) return -EINVAL; + mutex_lock(&st->lock); at91_adc_setup_samp_freq(indio_dev, val); + mutex_unlock(&st->lock); return 0; default: return -EINVAL;