From patchwork Sun Mar 12 23:15:52 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: 13171893 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 01449C7618D for ; Sun, 12 Mar 2023 23:16:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229796AbjCLXQW (ORCPT ); Sun, 12 Mar 2023 19:16:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38054 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229735AbjCLXQQ (ORCPT ); Sun, 12 Mar 2023 19:16:16 -0400 Received: from mail-qv1-xf2e.google.com (mail-qv1-xf2e.google.com [IPv6:2607:f8b0:4864:20::f2e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5B7D6279A8 for ; Sun, 12 Mar 2023 16:16:14 -0700 (PDT) Received: by mail-qv1-xf2e.google.com with SMTP id ks17so7241586qvb.6 for ; Sun, 12 Mar 2023 16:16:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1678662973; 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=bCcXjp0lmCj4QKMRCOzzdkCGwVUe3cwJnV9tyaVVNvo=; b=U8pWOtImrgJIFjVZj/Xq28/NpjxPFf4vLzvZkVXzha0RwiZNbGTkVlx+8nsrFxRvrx jXOd94daNGzJuQaiJoKqIAW72RfljfcnTf8SmgRtf2Yy/4jyh93POzHWWPyP3/vjmDW7 kNS/hwRH0mC8W2H0Dzc3U8gj9PCqhahCDy+U0JjcZpAFiEeUF/p0+moqlX1cyCQHno+h thwIGBFuRVMl6uAz4UISKWlVaMCjtWgXuMoiPEFolSAkISbJJclZUEqpNEt/zWI/bVjb NmL4bLI0fXg6n6khREG9g4j1pISa2iT2g4ZTjFCy3fcZqcsSVrglBeiA069Kvobtap9l +ftA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678662973; 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=bCcXjp0lmCj4QKMRCOzzdkCGwVUe3cwJnV9tyaVVNvo=; b=oH6l13W5FBBtc3/e1/xF6otqQqwxYHRcHCsNaEv9xtHc+CiVp3gkksM1yzV7X2gE3K WuTwopSHgLhZgx1KL4wX/3/j41FcAmYrGRZFOJaR/e2I+xjoIIKzlftYcVU9ZeBqjQTg YQzXQyQk8pZandb1q129ULfG+zyaIJwf3AEhPz8F4GznT7QhNK1rile/Ajelo1/haSZU C+KzGWmF8XsQUNk/URGDGq5JmEGM9LTWHap4waJAtjF5pgYEeSpz6dsCOivbEsF4Yulb 2135XUtJa8ce9F/K8rGXZmUA6pgKc1VwdBuqHZ1QUeAEhpffecgKSTvCQ+VACsqvfT03 JNPg== X-Gm-Message-State: AO0yUKVN0L1PlbF6bayucUWNedbAulCOSrufxWZ6FcN2W3zVnIxU8xIj hIgmoOTxw27tZBJA2Qu/8VBiEBepJmx7XePlMgQ= X-Google-Smtp-Source: AK7set8TQw8kYJb3ilSZLR2EUfhLXZ5VF3QVyPy8kdoT5WxN/ahhT4HfrRMD+6Pc5opZDP1IqA54Hw== X-Received: by 2002:a05:6214:f06:b0:56e:a96a:2bdc with SMTP id gw6-20020a0562140f0600b0056ea96a2bdcmr11438508qvb.40.1678662973185; Sun, 12 Mar 2023 16:16:13 -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 dm21-20020a05620a1d5500b00742f250ebc0sm4307091qkb.9.2023.03.12.16.16.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 12 Mar 2023 16:16:12 -0700 (PDT) From: William Breathitt Gray To: linux-iio@vger.kernel.org Cc: linux-kernel@vger.kernel.org, jic23@kernel.org, lars@metafoo.de, William Breathitt Gray , stable@vger.kernel.org Subject: [PATCH 5.4 v3 4/6] counter: 104-quad-8: Fix race condition between FLAG and CNTR reads Date: Sun, 12 Mar 2023 19:15:52 -0400 Message-Id: <20230312231554.134858-4-william.gray@linaro.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230312231554.134858-1-william.gray@linaro.org> References: <20230312231554.134858-1-william.gray@linaro.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org 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: 126ee8982bfcae6111f0fd157ee5cdd0ec7f5ca5 diff --git a/drivers/counter/104-quad-8.c b/drivers/counter/104-quad-8.c index f261a57af..48de69f58 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,