Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp7311501rwi; Mon, 24 Oct 2022 12:46:14 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4rdlFyayaGelRUKjXQT79mHuUWQMmwR49O7tTTYxmkE4YC1JKiljpEWiqdzUxy7RJF4BX2 X-Received: by 2002:a05:6402:51c8:b0:461:ea0c:e12c with SMTP id r8-20020a05640251c800b00461ea0ce12cmr3425875edd.47.1666640774141; Mon, 24 Oct 2022 12:46:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666640774; cv=none; d=google.com; s=arc-20160816; b=vDFWuK/5Ts/5g+WAfdmrLSX+SakCuMOr/A622sOn6AbAKXnO8FfshhoOJavNM5zUXq afun8jeEi97NN4aejz5W7X+JabPV2X6ELBgvI7q+xyzFTI1GZSfD9c6D0Oim/QCnnDDK 9PoQoi7QRQvTyAzvWT8wZwYFh5cW2LOWYhUAlVJyPIdeLYkXBwfFwIdQlshrw0DBQq5e Iz1gnKlV8zM+9JcflyF61Kj9RD0r/sIDuoTcbQevAzzNZGXnfiE20CRqOr88RbWlJbgW ZUUNXjZsAMKvX/FFcaPg/0To3HlWbCHldKMvfMls6AHM5xH0jki6awOd+/7O5FcvzNiv 2NoQ== 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=zk/nYl0Fc+kTiCDrwtZTswfvnSTDMOBbCDV3WtmznGM=; b=0tFnCMttRUwyGg85Xu0OiMMsN/BlBhb/rVBz06D4yyH3kqdRHYSb8570NhWbHcHXXZ NDn5GUvOHw32tCnX78r0R4YLC7aG/+UeTdYT4Xnhzj0Dt56LE0S/bIv/zAERDHPWkGk/ vwvhKXINTc7q3Tvnmc2FZmCmGtIyP1Oq+NGFqBEtLgPtojlYLwiAWABuE+tIFs9YlVrI yE0af4/KNONwm+hf6JvXZyakHfHrD8+/zQnT5intDR+yK1/JKjVlTIljiXpGUiHT9smf DLh3oxgICI/TiaJvWGqLhmynl7BQFJfOC+7rIOxdE/FNOqzDel58at0Mk/iGQyJEmuWg fZYg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=uI0Sctpp; 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 n15-20020a170906b30f00b0079dc9dcbbb6si496602ejz.337.2022.10.24.12.45.46; Mon, 24 Oct 2022 12:46:14 -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=uI0Sctpp; 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 S232459AbiJXTl4 (ORCPT + 99 others); Mon, 24 Oct 2022 15:41:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58680 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233393AbiJXTkw (ORCPT ); Mon, 24 Oct 2022 15:40:52 -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 970CED77E2; Mon, 24 Oct 2022 11:10: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 ams.source.kernel.org (Postfix) with ESMTPS id 3C477B819D2; Mon, 24 Oct 2022 12:45:56 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9A4E8C433C1; Mon, 24 Oct 2022 12:45:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666615555; bh=uxcP2FiiZKLxS1kVH+09zHJLuFZQSaPpDIupalOGDrY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=uI0SctppeAbowVMPJNoCdx+U1OG/5TEUVFawo6CltMZAXOcAEKNqe6b+KaI9J1PIl ZxEApy12n5EQYFU3Cazs67sW3A90Vau5JXPGzyV7LYm4X26kRbWZbJQDLTKhpVwUu+ K+SbH5fm3Wk73o6IgnRBj/fOtPH93r2chmQf+LRc= 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.15 266/530] iio: adc: at91-sama5d2_adc: lock around oversampling and sample freq Date: Mon, 24 Oct 2022 13:30:10 +0200 Message-Id: <20221024113057.089994676@linuxfoundation.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221024113044.976326639@linuxfoundation.org> References: <20221024113044.976326639@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 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 6eb72ba6b4d2..9cd6a779a217 100644 --- a/drivers/iio/adc/at91-sama5d2_adc.c +++ b/drivers/iio/adc/at91-sama5d2_adc.c @@ -1329,10 +1329,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; } @@ -1345,10 +1345,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; } @@ -1441,16 +1441,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; -- 2.35.1