From patchwork Mon Oct 24 11:29:44 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg KH X-Patchwork-Id: 9606 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp529735wru; Mon, 24 Oct 2022 09:04:04 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5sil0yGJgyyF0ijPVFZ0NjGb0NcuGJ1nOh+zdZ06NDcUbeB/pDyFM12Y16uiBaMo7uvhKa X-Received: by 2002:a17:90a:890b:b0:20d:981a:a5b5 with SMTP id u11-20020a17090a890b00b0020d981aa5b5mr70259545pjn.186.1666627444058; Mon, 24 Oct 2022 09:04:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666627444; cv=none; d=google.com; s=arc-20160816; b=Wd98qAy04tAEx/nAi/AEjYI8Go8ONYtSAzPEVYSxOUSx4xG67Zb82pHyxIeznJYAnA oydpyo2rZixbsDvPvD+AprgD2lsv1txToFroD7EhfcD4pDrq+acFKj2j3bfowKaG1bwj JPFGKC3ZrQH9HvvOsO8RabCT+gI3YytkBzwrtrDEdNfNS117HyGMy9qozSgYHR763DVE i6aCaVj9x01lNHsB1bXRgRk5Lbw2XaX+1e4hIh02to81BRfXpuiWuoiPVsdS1UCOufzV c5tUoXn3/qwuzvbXwHu7UqT/e0gkFRt6usFJ9LOqRjHXo+7lfE6breegS2dJ0x/AhNaK gcGQ== 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=zFiZV36VD6KllYtNvrOeJen03WOYpPxcaCLkpM/Zk8Y=; b=lPoKOnGeYYb947ndBwjOGQGxJg/gXRzKML59LSQo1yZ9/VeM5RDZExgCroFV053E7s /n+yLKR4/7Zp6Onpt0tDTT1DmfZl4STvTac5y/c+JMV+G1OqGMUUK1duWqHDUK01JI3F GyjBM9OslLg1vzOgTQKCG1INqBd7nwRWHa0Q0H+CtVuLxPvq0NPvKiqYoLstG4liOh1G RUsNKFZzBxgrU+JIUxSSao6b0Qn/IBY+LcZh7YyTeuobje01sOrshU4B74jbtkCtIf60 Cgnqv4oj1x6jrqMBrZ3LmEUzWy5yVJhyYFLk5vGk/GWo9NSmzyYY9TpG5BDRFKoTz/0c hCMw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=0Dng3LnK; 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 oa10-20020a17090b1bca00b00205e1caca43si396762pjb.34.2022.10.24.09.03.27; Mon, 24 Oct 2022 09:04:04 -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=0Dng3LnK; 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 S232625AbiJXQCK (ORCPT + 99 others); Mon, 24 Oct 2022 12:02:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44334 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231921AbiJXP7p (ORCPT ); Mon, 24 Oct 2022 11:59:45 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C6047870AD; Mon, 24 Oct 2022 07:55:07 -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 85788B81683; Mon, 24 Oct 2022 12:25:30 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E2B96C433C1; Mon, 24 Oct 2022 12:25:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666614329; bh=sYmnCnwfT8kM670N+dtsZiSMhPUdkCBCpcQmijME+Sg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=0Dng3LnKIe96k1JcAgiv+NSvgRVB474sFsixTcB1mtMiQRAVm8Lb5v2mt6ZwpfPAg YVovbDDzlZZ+clk4YSPWsx3cYKHYCWHkK6j+DOnPhiCkS2PwMC/+bkxNztl5zr+7Xd 0aH569jk8g9RxFqYm6lButkTnktjZ7AcZ5zkre58= 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 5.10 187/390] iio: adc: at91-sama5d2_adc: lock around oversampling and sample freq Date: Mon, 24 Oct 2022 13:29:44 +0200 Message-Id: <20221024113030.726389146@linuxfoundation.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221024113022.510008560@linuxfoundation.org> References: <20221024113022.510008560@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 X-Spam-Status: No, score=-7.6 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?1747585538420141810?= X-GMAIL-MSGID: =?utf-8?q?1747585538420141810?= 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 fe41689c5da6..ef6dc85024c1 100644 --- a/drivers/iio/adc/at91-sama5d2_adc.c +++ b/drivers/iio/adc/at91-sama5d2_adc.c @@ -1353,10 +1353,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; } @@ -1369,10 +1369,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; } @@ -1465,16 +1465,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;