Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp5704862rwl; Tue, 11 Apr 2023 08:55:02 -0700 (PDT) X-Google-Smtp-Source: AKy350YHwNyas00QWEdKHDnCMOBDzzcOnV9+KkDlH98YFb2zgbsfAUiziBqIv7r4jqj4C8xfbcgL X-Received: by 2002:a05:6402:156:b0:504:a2c4:c2b8 with SMTP id s22-20020a056402015600b00504a2c4c2b8mr5630027edu.11.1681228502120; Tue, 11 Apr 2023 08:55:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681228502; cv=none; d=google.com; s=arc-20160816; b=BNc5UA/eKzLDyQLU2POnmrkpLlLj3sKvyGOK0ycKtcdR9TNzgdF+GCWMP6sz2vvYxE r7rapCYPiOOoB6bg3zutbXlC8Vz7ga4er4NfSjfr/oGwk+EizAjkcu6Z2FnHP/Ylv1OV lBmbsNRSf4NjXcfrEb+gipWaXT9dhmJgxipwlQb/psQDZ8fnBax5TilbGWiteabq3l6q oS+AhFnA5s+PDNTTALhLaptIJXLTgZlQOWaGmrOkwmCgQUGuxgg3P17vE7KHs2oR1t5q E35xUvVnaN/AOK50+qSuiYij+AlCj8CjywJpNL1M8taoMGDYVrvxQnnFANw8+H9tpK3d Qb/A== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=fqIfvm1jwgfEXPWpkKRz5ERVmmhoVaNqmhNdHXexQ4w=; b=uRFEvZtkYy4I5pHMyj6VMtscnzpo/0C30G5NJhiSvyhepxr+P9eGMRYMWVGSpq9KlH e5MTQ6XWraJXFm95X1oAb7RlBOJbaom2pFWpUBkCeSvw+MzrpkD4z69hwZipJm3JKKr0 7lsr4vHW66V22btA9bEd7IcZhzDT6WkrMpE8Vj7M1VxMwKLZRvqqyJZ2jbg/Nst5Y18r +ro3ZJwzxpQcWuP1g9H/gRXmKFrG5Ina6JYVETEKjSwRGosedmTKmjUoiahS/4UDYfgC sCpz3KjRBz67LmvkKZlK5Wxug621Cm+473uRbI5oiQq3L/ZlqS+1ZhpDLxDonnGjHr4u 4uYw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="IXFXL/AL"; 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=linaro.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n10-20020aa7d04a000000b005049478d385si6977843edo.552.2023.04.11.08.54.37; Tue, 11 Apr 2023 08:55:02 -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=@linaro.org header.s=google header.b="IXFXL/AL"; 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=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230404AbjDKPwl (ORCPT + 99 others); Tue, 11 Apr 2023 11:52:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40066 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230211AbjDKPwf (ORCPT ); Tue, 11 Apr 2023 11:52:35 -0400 Received: from mail-yb1-xb2a.google.com (mail-yb1-xb2a.google.com [IPv6:2607:f8b0:4864:20::b2a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B1F8A4EF1 for ; Tue, 11 Apr 2023 08:52:34 -0700 (PDT) Received: by mail-yb1-xb2a.google.com with SMTP id u13so8777667ybu.5 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=GpvyCsspIHTeet832rNCSm8fa1AWqw5gUX+i90W/ZYXQ7CTRGhqAyaLwI0FYlaN9eh 5TYqN4YQugZZvvizI41Y0eXmZPhFFi+8wWg4RJ5t4mcYiXCN/Fnd5XdtpJd7qd2SgIWE URgBwTfKAK/IXYTG6iuNw3pLLTAZELG6BQVuXfxr3x/O8l0tb0cn5XP0X6hifFfKO8z+ PZ58p0+WXuk35zncobSP7ialLd3glRBd0HjhqC5rFXvMQRiucA1b7mTVanbpUInpivnc yTtg2AvQ8Z32mAeE6sSz2zc9M6OUtUMSjlkB2nuqNtDsL0zmj9OB/ikwKtuuYA55vLJV lKNA== X-Gm-Message-State: AAQBX9fa2ONCX6KDnNWZ2b7nmTcXlVYliZDvmnf65Y01URHvTQuE3tIK OVBJ/IE+nJlOiqQOTESG/j2lXqsoKfaJCtecqfp+sA== 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 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=unavailable 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 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(-) 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, base-commit: 32bea3bac5ca484c6f7e302c8c96fc686f62e7b4 -- 2.39.2