From patchwork Tue Apr 11 15:52:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: William Breathitt Gray X-Patchwork-Id: 13207760 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A43DFC76196 for ; Tue, 11 Apr 2023 15:52:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230174AbjDKPwf (ORCPT ); Tue, 11 Apr 2023 11:52:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40026 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229949AbjDKPwe (ORCPT ); Tue, 11 Apr 2023 11:52:34 -0400 Received: from mail-yb1-xb33.google.com (mail-yb1-xb33.google.com [IPv6:2607:f8b0:4864:20::b33]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2868A30E0 for ; Tue, 11 Apr 2023 08:52:33 -0700 (PDT) Received: by mail-yb1-xb33.google.com with SMTP id e127so8651359ybf.8 for ; Tue, 11 Apr 2023 08:52:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1681228352; x=1683820352; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=7DQjB9jjSniX8WUn0hxZzli/VJttza7GiVrQjQV9IoA=; b=dpyrf0ifpUMeAEvu8Wf578r8CUr/1v6SAD7UgK0B3YcGvaSKQ3g2wlO2F7wbE09ACW +80MxGqHgY9PUMgzKrtA96in292vvDoNetsu6pJHqDaTXVDcvi3MtnqLQvRbFfrpNlAv tPzlU1PvUacXf3sd3OYkdq8l3tlDRsxDMoZKBNZx/SAzrJU1TGZjUnflNmA0x6Y0OIic d2ucXIikHJeU5BYCm6xr1EQNtKZ1uW+nyqt+OCFJ/qE8LtMyQLupVNMTfdiEOgFkyd1T cp3Ih6v/GvBGoKcgEQfJaAUiglhOk/x/Z8zMh/bvqd2VJlzkCgzAX+FN3WJWEsHM5PX/ hW4A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1681228352; x=1683820352; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=7DQjB9jjSniX8WUn0hxZzli/VJttza7GiVrQjQV9IoA=; b=n32S3iOaljk8RUdwmQPBL59mm60R5UM6tI2Iwc7npsFCsG3CWj6NPa7cuayR4gF7o5 dzDAIUz0x1Jz5TFMsRm6lpj3RteRE+jMCdlJXYr71E/wEbkxZMN6tJWCXu2aQjE9aVXV ioFgObK22/HuIo3SgL2Oj2bSzXhyWd2R7qQ0sfjwI+CEkH148l6S4RODR6hrFY+2Rvl7 w0sf1ThrkmWEjyGjEvFBGz6ExmU2ugDLThObgbwABZ9pcKrVCsye9LvBFWT+fKVc1Anf bWJP49qiGA7CuLMTnibj32NQ0EDWi9rgWvW6dlDSe7AbB+C+APhjucZoT1CFahYAHv6i 8B5Q== X-Gm-Message-State: AAQBX9dlBcHFiFAgCDcyzZjk5Fjgz7C+oYFdawpMtHptE0aPesByltzC c7kX2kF4Pw09XvsAt7OOdxC7Lg== X-Google-Smtp-Source: AKy350bvU9oF7Z3oiDlXBWDjKnr5iukeWwTFd1dtkT3Gt8bLaR4FVVNT+wSpGQYLJwyBBI20EpbPWw== X-Received: by 2002:a25:e78e:0:b0:b8e:e4be:923a with SMTP id e136-20020a25e78e000000b00b8ee4be923amr6238760ybh.4.1681228352348; Tue, 11 Apr 2023 08:52:32 -0700 (PDT) Received: from fedora.attlocal.net (69-109-179-158.lightspeed.dybhfl.sbcglobal.net. [69.109.179.158]) by smtp.gmail.com with ESMTPSA id i13-20020a056902068d00b00b7767ca749fsm3683390ybt.60.2023.04.11.08.52.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Apr 2023 08:52:31 -0700 (PDT) From: William Breathitt Gray To: Greg Kroah-Hartman Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, Jonathan Cameron , William Breathitt Gray , stable@vger.kernel.org Subject: [RESEND PATCH 4.14 v3 1/5] iio: counter: 104-quad-8: Fix race condition between FLAG and CNTR reads Date: Tue, 11 Apr 2023 11:52:16 -0400 Message-Id: <20230411155220.9754-1-william.gray@linaro.org> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org commit 4aa3b75c74603c3374877d5fd18ad9cc3a9a62ed upstream. The Counter (CNTR) register is 24 bits wide, but we can have an effective 25-bit count value by setting bit 24 to the XOR of the Borrow flag and Carry flag. The flags can be read from the FLAG register, but a race condition exists: the Borrow flag and Carry flag are instantaneous and could change by the time the count value is read from the CNTR register. Since the race condition could result in an incorrect 25-bit count value, remove support for 25-bit count values from this driver. Fixes: 28e5d3bb0325 ("iio: 104-quad-8: Add IIO support for the ACCES 104-QUAD-8") Cc: # 4.14.x Signed-off-by: William Breathitt Gray --- drivers/iio/counter/104-quad-8.c | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) base-commit: f03c8bbaf6d9cbebee390e8353c5df75293aff7c diff --git a/drivers/iio/counter/104-quad-8.c b/drivers/iio/counter/104-quad-8.c index 181585ae6..bdb07694e 100644 --- a/drivers/iio/counter/104-quad-8.c +++ b/drivers/iio/counter/104-quad-8.c @@ -64,9 +64,6 @@ static int quad8_read_raw(struct iio_dev *indio_dev, { struct quad8_iio *const priv = iio_priv(indio_dev); const int base_offset = priv->base + 2 * chan->channel; - unsigned int flags; - unsigned int borrow; - unsigned int carry; int i; switch (mask) { @@ -76,12 +73,7 @@ static int quad8_read_raw(struct iio_dev *indio_dev, return IIO_VAL_INT; } - flags = inb(base_offset + 1); - borrow = flags & BIT(0); - carry = !!(flags & BIT(1)); - - /* Borrow XOR Carry effectively doubles count range */ - *val = (borrow ^ carry) << 24; + *val = 0; /* Reset Byte Pointer; transfer Counter to Output Latch */ outb(0x11, base_offset + 1); From patchwork Tue Apr 11 15:52:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: William Breathitt Gray X-Patchwork-Id: 13207761 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 81787C77B74 for ; Tue, 11 Apr 2023 15:52:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230242AbjDKPwg (ORCPT ); Tue, 11 Apr 2023 11:52:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40048 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230087AbjDKPwf (ORCPT ); Tue, 11 Apr 2023 11:52:35 -0400 Received: from mail-yb1-xb35.google.com (mail-yb1-xb35.google.com [IPv6:2607:f8b0:4864:20::b35]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EA1C34EC9 for ; Tue, 11 Apr 2023 08:52:33 -0700 (PDT) Received: by mail-yb1-xb35.google.com with SMTP id q5so11491961ybk.7 for ; Tue, 11 Apr 2023 08:52:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1681228353; x=1683820353; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=R2fQ8lpuhAnY5CWWZIXFfg7wKiEfX2djoV2eUnsPPVc=; b=QeRpnyUOHzGwuIeP0MNJJWJHdCioAjOAx1apI5BRCq0IYAMc/njk8B75syYr9AqXE2 tejgjgi07wNgTlMzVt7hopwjrFADlgs/D92apR5rb+ejmRJ1nEJbnEZMC4oLtN44t4DM /ZUauG7kYMGyCJXUv0YDarqKe929Ac22GVOww4nsqChyhez7viGB6+dBIDD5fe7F3Li1 56IEUCdZw0+Fuws9CIpj52fOiWM6wGDf7RSk00d8j5XtxHmb+NcXuTmq2FBUtp68BGO2 LFMnOce6hRQsYFA+/EhCW/NTi8KUgCXbqsgC4dzdfX3DUnbV/GBcjOe5RXfUea1y+7Zo Kq8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1681228353; x=1683820353; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=R2fQ8lpuhAnY5CWWZIXFfg7wKiEfX2djoV2eUnsPPVc=; b=WTey3Q+mmw5AtvEf1GzlwJiq2Mn29WHnSPvhcHKyeVrMKGSiFqcQzlZ7byZ0HOmn6f 93zaFjDjK+lkdZ2ylxt+Mx86RvaEyZHeI8ZqvK6vWF+da9HxqEOjXbOMhe5Zo6O3fx6A jZ4bLd92gYKc5OxZN1E9WsQMlG4KDy5eC3I2sailSlD9M8ePnyg5wEA43FxByge4n636 xVg3m9lckGGmgWwUmvigoodMJOx1q6A24GKzS7SEfpv2Gh23XGdEldywb8LPl8b4jT6p d7JktUjT7LQ/LkudnaOvrpTLu4A7NZEIwmQYH3gXRXyKSlZDtgIDurn9NQgZgytJYwgn i7Yw== X-Gm-Message-State: AAQBX9dm7SoGgHUIz1UO0NiALORzA2C7GUvsex41qdP4wnUOds43288m IOxLS62rlsfV792goHt0qfCjYw== X-Google-Smtp-Source: AKy350YaiHBMwi9YMqi/fZ+eHfktwCe2cCyF5zaXDxiQxeUqS2HU922wdMU3rxmLbgmRzBoeF8wuaA== X-Received: by 2002:a25:688a:0:b0:b8f:227a:b080 with SMTP id d132-20020a25688a000000b00b8f227ab080mr2237727ybc.18.1681228353118; Tue, 11 Apr 2023 08:52:33 -0700 (PDT) Received: from fedora.attlocal.net (69-109-179-158.lightspeed.dybhfl.sbcglobal.net. [69.109.179.158]) by smtp.gmail.com with ESMTPSA id i13-20020a056902068d00b00b7767ca749fsm3683390ybt.60.2023.04.11.08.52.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Apr 2023 08:52:32 -0700 (PDT) From: William Breathitt Gray To: Greg Kroah-Hartman Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, Jonathan Cameron , William Breathitt Gray , stable@vger.kernel.org Subject: [RESEND PATCH 4.19 v3 2/5] iio: counter: 104-quad-8: Fix race condition between FLAG and CNTR reads Date: Tue, 11 Apr 2023 11:52:17 -0400 Message-Id: <20230411155220.9754-2-william.gray@linaro.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230411155220.9754-1-william.gray@linaro.org> References: <20230411155220.9754-1-william.gray@linaro.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org commit 4aa3b75c74603c3374877d5fd18ad9cc3a9a62ed upstream. The Counter (CNTR) register is 24 bits wide, but we can have an effective 25-bit count value by setting bit 24 to the XOR of the Borrow flag and Carry flag. The flags can be read from the FLAG register, but a race condition exists: the Borrow flag and Carry flag are instantaneous and could change by the time the count value is read from the CNTR register. Since the race condition could result in an incorrect 25-bit count value, remove support for 25-bit count values from this driver. Fixes: 28e5d3bb0325 ("iio: 104-quad-8: Add IIO support for the ACCES 104-QUAD-8") Cc: # 4.19.x Signed-off-by: William Breathitt Gray --- drivers/iio/counter/104-quad-8.c | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) base-commit: 5c0966408dee90137adf2e96f949e50a2ba7e401 diff --git a/drivers/iio/counter/104-quad-8.c b/drivers/iio/counter/104-quad-8.c index 92be8d0f77..92e68cada8 100644 --- a/drivers/iio/counter/104-quad-8.c +++ b/drivers/iio/counter/104-quad-8.c @@ -61,10 +61,6 @@ struct quad8_iio { #define QUAD8_REG_CHAN_OP 0x11 #define QUAD8_REG_INDEX_INPUT_LEVELS 0x16 -/* Borrow Toggle flip-flop */ -#define QUAD8_FLAG_BT BIT(0) -/* Carry Toggle flip-flop */ -#define QUAD8_FLAG_CT BIT(1) /* Error flag */ #define QUAD8_FLAG_E BIT(4) /* Up/Down flag */ @@ -97,9 +93,6 @@ static int quad8_read_raw(struct iio_dev *indio_dev, { struct quad8_iio *const priv = iio_priv(indio_dev); const int base_offset = priv->base + 2 * chan->channel; - unsigned int flags; - unsigned int borrow; - unsigned int carry; int i; switch (mask) { @@ -110,12 +103,7 @@ static int quad8_read_raw(struct iio_dev *indio_dev, return IIO_VAL_INT; } - flags = inb(base_offset + 1); - borrow = flags & QUAD8_FLAG_BT; - carry = !!(flags & QUAD8_FLAG_CT); - - /* Borrow XOR Carry effectively doubles count range */ - *val = (borrow ^ carry) << 24; + *val = 0; /* Reset Byte Pointer; transfer Counter to Output Latch */ outb(QUAD8_CTR_RLD | QUAD8_RLD_RESET_BP | QUAD8_RLD_CNTR_OUT, From patchwork Tue Apr 11 15:52:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: William Breathitt Gray X-Patchwork-Id: 13207762 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3FC90C77B75 for ; Tue, 11 Apr 2023 15:52:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230087AbjDKPwg (ORCPT ); Tue, 11 Apr 2023 11:52:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40064 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230193AbjDKPwf (ORCPT ); Tue, 11 Apr 2023 11:52:35 -0400 Received: from mail-yb1-xb30.google.com (mail-yb1-xb30.google.com [IPv6:2607:f8b0:4864:20::b30]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B113E46B3 for ; Tue, 11 Apr 2023 08:52:34 -0700 (PDT) Received: by mail-yb1-xb30.google.com with SMTP id y186so8705874yby.13 for ; Tue, 11 Apr 2023 08:52:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1681228354; x=1683820354; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=fqIfvm1jwgfEXPWpkKRz5ERVmmhoVaNqmhNdHXexQ4w=; b=IXFXL/AL+uZl+iYIcoG9aarge/H8K1NeyCmpcIUo+APyfhlvVFnXUhfeKab+TZ37Wv uyul1KitAUjiHkMRUztnQZkI3MFselOfNNdKztA0TfZNzN4pRnL7etXrUiBWr0dgthEI K2v1DO4n5ASENLCSxuOKPvPIVYKSOWrSpYXS1/QIzniYOrH9fnsdF1bHbjqcpDXdN2eJ 5OfJdSops0MEQNTu01FWfRtLsSvlpEu4itzmlWjh/s+1yf8MiWAbzrJ9Zxb1MK9XnMp3 ZOl1XXoGT77K37S7zBfeQOp2jNSX+IVrki2zrQhW0UYfSSYNYOw2VGeCS1wLt+fk0AK/ I5+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1681228354; x=1683820354; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=fqIfvm1jwgfEXPWpkKRz5ERVmmhoVaNqmhNdHXexQ4w=; b=QZZV261UvgACv/Ntf7SkCeU9Cj8nP3dJ/k6xRfRFHsN1fspgr4W9SY8MwgkI2pQx2f SBbFeRKJGKZIlsptLuf1vlNLB7fHBAYJQKVmF3OdSHztlJMM8L/ZakoF+1Ja3idHISb4 fcPf6Xe1Avca+c30yC8MWxsLTuQV/9UIJUg11vjOMSrToTZ2TgVlxCuqpDrot1/wV+7g 2R+L9oOK9ps4lHSUQWESEMlfnacoIu67ZEDx/lsU/7uxzqn1DJXxHd3sMGcrDKE4OHJ6 ENLN/trp11Ffr7K2B+eygT9i+26UgUp27cKTSdt3VNo17548Ap4srkMrSdxkI+GucCbU +NZg== X-Gm-Message-State: AAQBX9ciWuyYNWLzI53KrqF4EeDUYprrhpPc7GqvLgrEw+16qXALq7Iq +472LCom6Xvg0LN28DXswcbttA== X-Google-Smtp-Source: AKy350aT0zxuuJUmLFgMSJj+vRNJhxHu2iFB85HVTAvunu3Pqv7KMsK/TGtVpKpmFb+IOxFMWXsb2g== X-Received: by 2002:a25:d3d0:0:b0:b8f:2033:c10d with SMTP id e199-20020a25d3d0000000b00b8f2033c10dmr2581494ybf.59.1681228353960; Tue, 11 Apr 2023 08:52:33 -0700 (PDT) Received: from fedora.attlocal.net (69-109-179-158.lightspeed.dybhfl.sbcglobal.net. [69.109.179.158]) by smtp.gmail.com with ESMTPSA id i13-20020a056902068d00b00b7767ca749fsm3683390ybt.60.2023.04.11.08.52.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Apr 2023 08:52:33 -0700 (PDT) From: William Breathitt Gray To: Greg Kroah-Hartman Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, Jonathan Cameron , William Breathitt Gray , stable@vger.kernel.org Subject: [RESEND PATCH 5.4 v3 3/5] counter: 104-quad-8: Fix race condition between FLAG and CNTR reads Date: Tue, 11 Apr 2023 11:52:18 -0400 Message-Id: <20230411155220.9754-3-william.gray@linaro.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230411155220.9754-1-william.gray@linaro.org> References: <20230411155220.9754-1-william.gray@linaro.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org commit 4aa3b75c74603c3374877d5fd18ad9cc3a9a62ed upstream. The Counter (CNTR) register is 24 bits wide, but we can have an effective 25-bit count value by setting bit 24 to the XOR of the Borrow flag and Carry flag. The flags can be read from the FLAG register, but a race condition exists: the Borrow flag and Carry flag are instantaneous and could change by the time the count value is read from the CNTR register. Since the race condition could result in an incorrect 25-bit count value, remove support for 25-bit count values from this driver. Fixes: 28e5d3bb0325 ("iio: 104-quad-8: Add IIO support for the ACCES 104-QUAD-8") Cc: # 5.4.x Signed-off-by: William Breathitt Gray --- drivers/counter/104-quad-8.c | 20 +++----------------- 1 file changed, 3 insertions(+), 17 deletions(-) base-commit: 32bea3bac5ca484c6f7e302c8c96fc686f62e7b4 diff --git a/drivers/counter/104-quad-8.c b/drivers/counter/104-quad-8.c index f261a57af1..48de69f58e 100644 --- a/drivers/counter/104-quad-8.c +++ b/drivers/counter/104-quad-8.c @@ -57,10 +57,6 @@ struct quad8_iio { #define QUAD8_REG_CHAN_OP 0x11 #define QUAD8_REG_INDEX_INPUT_LEVELS 0x16 -/* Borrow Toggle flip-flop */ -#define QUAD8_FLAG_BT BIT(0) -/* Carry Toggle flip-flop */ -#define QUAD8_FLAG_CT BIT(1) /* Error flag */ #define QUAD8_FLAG_E BIT(4) /* Up/Down flag */ @@ -639,19 +635,9 @@ static int quad8_count_read(struct counter_device *counter, { struct quad8_iio *const priv = counter->priv; const int base_offset = priv->base + 2 * count->id; - unsigned int flags; - unsigned int borrow; - unsigned int carry; - unsigned long position; + unsigned long position = 0; int i; - flags = inb(base_offset + 1); - borrow = flags & QUAD8_FLAG_BT; - carry = !!(flags & QUAD8_FLAG_CT); - - /* Borrow XOR Carry effectively doubles count range */ - position = (unsigned long)(borrow ^ carry) << 24; - mutex_lock(&priv->lock); /* Reset Byte Pointer; transfer Counter to Output Latch */ @@ -1204,8 +1190,8 @@ static ssize_t quad8_count_ceiling_read(struct counter_device *counter, mutex_unlock(&priv->lock); - /* By default 0x1FFFFFF (25 bits unsigned) is maximum count */ - return sprintf(buf, "33554431\n"); + /* By default 0xFFFFFF (24 bits unsigned) is maximum count */ + return sprintf(buf, "16777215\n"); } static ssize_t quad8_count_ceiling_write(struct counter_device *counter, From patchwork Tue Apr 11 15:52:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: William Breathitt Gray X-Patchwork-Id: 13207763 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B5616C77B6F for ; Tue, 11 Apr 2023 15:52:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230333AbjDKPwh (ORCPT ); Tue, 11 Apr 2023 11:52:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40102 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230253AbjDKPwg (ORCPT ); Tue, 11 Apr 2023 11:52:36 -0400 Received: from mail-yb1-xb2f.google.com (mail-yb1-xb2f.google.com [IPv6:2607:f8b0:4864:20::b2f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 74B0E30E0 for ; Tue, 11 Apr 2023 08:52:35 -0700 (PDT) Received: by mail-yb1-xb2f.google.com with SMTP id d204so253991ybh.6 for ; Tue, 11 Apr 2023 08:52:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1681228354; x=1683820354; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=daPYhCzB7SEVb4s3T1TV70tcgOBLpgetbD3DUYP20DI=; b=s8nmKgrHFtdn17kHL7e1AxEvw/l9oWgrKWasQIEECRD09VQvjuMH3wUp94Rgz6Q4XW NpVnJB8dFk+Gdu0wY2+P26KhCH7RhK0/a+8Jms/AXXbByLKejAlXnrehwcu33l2h9wfI lyWjcwofFlBT2M6R/AQeuZndzij/g4V3TvJpjYozj1NqRM/hGee/FklCDrqmFrvq/W5m jbMLBlZzdTyCt1liXeL7RDvEidKVs/8P5wS1n2PbpTIE5s8h1nKghywrbxj46Rr0mB5i AlECqO8KkbJVX0KHo5WHWyF4wRB4oWQ4Cv0Zh5fzOOiJM20ywsoMdcMMFtz7ZYiXcTVF 8Scg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1681228354; x=1683820354; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=daPYhCzB7SEVb4s3T1TV70tcgOBLpgetbD3DUYP20DI=; b=P7Hv5CvZZPrl8uL31Sn3MjZf9kmUzxpAf9SlhPhmf3exK2iwGHVzaBQi3x8qBknQyJ D3837iSOZDPFX9sXdrwHQ4rldQWuFKSwcjjFD5F0/zPfQz33x8iTAcr9HPGQjhsZr//D mpN98vGWk8fdC6uutFUNEherggsIYRl8Aq7tr/ywVjouvLeWtcLAki5Uc51NAuv2HESo G1pYJgevBTqHSLWOMOwdlOQ8nkdeoj0Ao4Uo+mpewnpZjkwl6Yj6Xbbx/RDhyLFP63GL xN84TRsUmCTAAnhC56j7+c+fWFYbxW462icalhx4Xjhba960eYpIuQLpJq1FgCstY4O+ /8xQ== X-Gm-Message-State: AAQBX9eDAa25bCkWX8whC51L8EI5592lw/4gonc3TMMQcAfyXeqrxEqV WkOakK31NvC8OqJ8ZAV1h2oa7A== X-Google-Smtp-Source: AKy350YqtHt8PRSduE8nry8VW1BZVJSdeFbIFj8k3jRBK2Jrmh+0Tt5oD9pk3wCgW8SunPZMEqrykg== X-Received: by 2002:a25:ea03:0:b0:b8f:f5f:13e9 with SMTP id p3-20020a25ea03000000b00b8f0f5f13e9mr3368186ybd.2.1681228354656; Tue, 11 Apr 2023 08:52:34 -0700 (PDT) Received: from fedora.attlocal.net (69-109-179-158.lightspeed.dybhfl.sbcglobal.net. [69.109.179.158]) by smtp.gmail.com with ESMTPSA id i13-20020a056902068d00b00b7767ca749fsm3683390ybt.60.2023.04.11.08.52.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Apr 2023 08:52:34 -0700 (PDT) From: William Breathitt Gray To: Greg Kroah-Hartman Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, Jonathan Cameron , William Breathitt Gray , stable@vger.kernel.org Subject: [RESEND PATCH 5.10 v3 4/5] counter: 104-quad-8: Fix race condition between FLAG and CNTR reads Date: Tue, 11 Apr 2023 11:52:19 -0400 Message-Id: <20230411155220.9754-4-william.gray@linaro.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230411155220.9754-1-william.gray@linaro.org> References: <20230411155220.9754-1-william.gray@linaro.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org commit 4aa3b75c74603c3374877d5fd18ad9cc3a9a62ed upstream. The Counter (CNTR) register is 24 bits wide, but we can have an effective 25-bit count value by setting bit 24 to the XOR of the Borrow flag and Carry flag. The flags can be read from the FLAG register, but a race condition exists: the Borrow flag and Carry flag are instantaneous and could change by the time the count value is read from the CNTR register. Since the race condition could result in an incorrect 25-bit count value, remove support for 25-bit count values from this driver. Fixes: 28e5d3bb0325 ("iio: 104-quad-8: Add IIO support for the ACCES 104-QUAD-8") Cc: # 5.10.x Signed-off-by: William Breathitt Gray --- drivers/counter/104-quad-8.c | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) base-commit: 387078f9030cf336cd9fef521540db75b61615e0 diff --git a/drivers/counter/104-quad-8.c b/drivers/counter/104-quad-8.c index 21bb2bb767..1b4fdee9d9 100644 --- a/drivers/counter/104-quad-8.c +++ b/drivers/counter/104-quad-8.c @@ -62,10 +62,6 @@ struct quad8_iio { #define QUAD8_REG_CHAN_OP 0x11 #define QUAD8_REG_INDEX_INPUT_LEVELS 0x16 #define QUAD8_DIFF_ENCODER_CABLE_STATUS 0x17 -/* Borrow Toggle flip-flop */ -#define QUAD8_FLAG_BT BIT(0) -/* Carry Toggle flip-flop */ -#define QUAD8_FLAG_CT BIT(1) /* Error flag */ #define QUAD8_FLAG_E BIT(4) /* Up/Down flag */ @@ -643,17 +639,9 @@ static int quad8_count_read(struct counter_device *counter, { struct quad8_iio *const priv = counter->priv; const int base_offset = priv->base + 2 * count->id; - unsigned int flags; - unsigned int borrow; - unsigned int carry; int i; - flags = inb(base_offset + 1); - borrow = flags & QUAD8_FLAG_BT; - carry = !!(flags & QUAD8_FLAG_CT); - - /* Borrow XOR Carry effectively doubles count range */ - *val = (unsigned long)(borrow ^ carry) << 24; + *val = 0; mutex_lock(&priv->lock); @@ -1198,8 +1186,8 @@ static ssize_t quad8_count_ceiling_read(struct counter_device *counter, mutex_unlock(&priv->lock); - /* By default 0x1FFFFFF (25 bits unsigned) is maximum count */ - return sprintf(buf, "33554431\n"); + /* By default 0xFFFFFF (24 bits unsigned) is maximum count */ + return sprintf(buf, "16777215\n"); } static ssize_t quad8_count_ceiling_write(struct counter_device *counter, From patchwork Tue Apr 11 15:52:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: William Breathitt Gray X-Patchwork-Id: 13207764 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 612A0C76196 for ; Tue, 11 Apr 2023 15:52:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230387AbjDKPwj (ORCPT ); Tue, 11 Apr 2023 11:52:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40102 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230358AbjDKPwi (ORCPT ); Tue, 11 Apr 2023 11:52:38 -0400 Received: from mail-yb1-xb34.google.com (mail-yb1-xb34.google.com [IPv6:2607:f8b0:4864:20::b34]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5C63E5251 for ; Tue, 11 Apr 2023 08:52:36 -0700 (PDT) Received: by mail-yb1-xb34.google.com with SMTP id y69so15547463ybe.2 for ; Tue, 11 Apr 2023 08:52:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1681228355; x=1683820355; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=YVnzCMuG8yOr6jsr0GEp0cpvdQzZRFZZBcpuklzVomM=; b=MP3/MBQD+/KeXIGk4Kq2i951jxW+nIqkXxpC4/5Ve3x6RHWDhUhRiLM5bhHVzAz46J LFwbUIMhG2IhEDlG6Bp9RQgJvMtkye5PzZldbGZQr83UyNzvyAGjI9GGIpKwkLGnk8Mf CdkJ7zJAWpyapByQSRGQuo/SKPvlSNySk6iZx5Lj9bZdfVe0xLA5YGLtJYlgS5NnJm5v Ms4LeTJoxunl2EvuENwyZtsEqQRqF4PkpNd9nbQLBsV/jE/muQ5JsmgmvjIqBPT+iuHP 2e4YxRUUIUkWKgSUKkgkPCiOn1ZVvVdNlEVI0AVljrjWP8g+8TTBwpiaq78o4KOsRrEV cNIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1681228355; x=1683820355; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=YVnzCMuG8yOr6jsr0GEp0cpvdQzZRFZZBcpuklzVomM=; b=ck3DR3VTGOO8xaij60wYHodZW4To6I0zR8X1YTdyuCKgnT/jrF/MnqE7bVZZ9Bx/nw c+W/SM/+wlZYBe1QPGnNgHieMkJFiGQVB96C/I+KyDOzBLpuzTeyXLDgAemZH+E5YqX0 hCSrdk71nn63l1Jgq9coDQlqRI1tmqJETH8YJ0Qx+wbfz9GUk5xc1xtzzeeaRP3lS14r xA2rCya+wTqAypplALBUYJMxgivlvjTv9AetrO5mkuAcpg1JulMz4M5VZvdbZyyx4iTu A5z5ezYNz8YbmOzTxk67rFiPu+P0nIgl3dxxul5CKma/dR27Wte6Fr1qVEQBul7mi1mO Ro7w== X-Gm-Message-State: AAQBX9fkXlMVNfGRUbs2Kb5qZ+LW99GEYV9nf5Vs3dbn33Ci1Ma9i4IR RkBiNFXHMKv9PQqRHHYwXRnCPA== X-Google-Smtp-Source: AKy350a2r0p6xSocXox5/NbH6U0w0MyqWWZVnRpVrTTXIQZjimC9nR1mrVSwDRzh9GaQCiZHx2TSIQ== X-Received: by 2002:a25:ab0a:0:b0:b72:245f:4411 with SMTP id u10-20020a25ab0a000000b00b72245f4411mr2396336ybi.46.1681228355448; Tue, 11 Apr 2023 08:52:35 -0700 (PDT) Received: from fedora.attlocal.net (69-109-179-158.lightspeed.dybhfl.sbcglobal.net. [69.109.179.158]) by smtp.gmail.com with ESMTPSA id i13-20020a056902068d00b00b7767ca749fsm3683390ybt.60.2023.04.11.08.52.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Apr 2023 08:52:35 -0700 (PDT) From: William Breathitt Gray To: Greg Kroah-Hartman Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, Jonathan Cameron , William Breathitt Gray , stable@vger.kernel.org Subject: [RESEND PATCH 5.15 v3 5/5] counter: 104-quad-8: Fix race condition between FLAG and CNTR reads Date: Tue, 11 Apr 2023 11:52:20 -0400 Message-Id: <20230411155220.9754-5-william.gray@linaro.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230411155220.9754-1-william.gray@linaro.org> References: <20230411155220.9754-1-william.gray@linaro.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org commit 4aa3b75c74603c3374877d5fd18ad9cc3a9a62ed upstream. The Counter (CNTR) register is 24 bits wide, but we can have an effective 25-bit count value by setting bit 24 to the XOR of the Borrow flag and Carry flag. The flags can be read from the FLAG register, but a race condition exists: the Borrow flag and Carry flag are instantaneous and could change by the time the count value is read from the CNTR register. Since the race condition could result in an incorrect 25-bit count value, remove support for 25-bit count values from this driver. Fixes: 28e5d3bb0325 ("iio: 104-quad-8: Add IIO support for the ACCES 104-QUAD-8") Cc: # 5.15.x Signed-off-by: William Breathitt Gray --- drivers/counter/104-quad-8.c | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) base-commit: d86dfc4d95cd218246b10ca7adf22c8626547599 diff --git a/drivers/counter/104-quad-8.c b/drivers/counter/104-quad-8.c index 0caa60537b..643aae0c9f 100644 --- a/drivers/counter/104-quad-8.c +++ b/drivers/counter/104-quad-8.c @@ -61,10 +61,6 @@ struct quad8 { #define QUAD8_REG_CHAN_OP 0x11 #define QUAD8_REG_INDEX_INPUT_LEVELS 0x16 #define QUAD8_DIFF_ENCODER_CABLE_STATUS 0x17 -/* Borrow Toggle flip-flop */ -#define QUAD8_FLAG_BT BIT(0) -/* Carry Toggle flip-flop */ -#define QUAD8_FLAG_CT BIT(1) /* Error flag */ #define QUAD8_FLAG_E BIT(4) /* Up/Down flag */ @@ -121,17 +117,9 @@ static int quad8_count_read(struct counter_device *counter, { struct quad8 *const priv = counter->priv; const int base_offset = priv->base + 2 * count->id; - unsigned int flags; - unsigned int borrow; - unsigned int carry; int i; - flags = inb(base_offset + 1); - borrow = flags & QUAD8_FLAG_BT; - carry = !!(flags & QUAD8_FLAG_CT); - - /* Borrow XOR Carry effectively doubles count range */ - *val = (unsigned long)(borrow ^ carry) << 24; + *val = 0; mutex_lock(&priv->lock); @@ -699,8 +687,8 @@ static ssize_t quad8_count_ceiling_read(struct counter_device *counter, mutex_unlock(&priv->lock); - /* By default 0x1FFFFFF (25 bits unsigned) is maximum count */ - return sprintf(buf, "33554431\n"); + /* By default 0xFFFFFF (24 bits unsigned) is maximum count */ + return sprintf(buf, "16777215\n"); } static ssize_t quad8_count_ceiling_write(struct counter_device *counter,