public inbox for linux-iio@vger.kernel.org
 help / color / mirror / Atom feed
From: William Breathitt Gray <william.gray@linaro.org>
To: Pavel Machek <pavel@ucw.cz>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org,
	Jonathan Cameron <jic23@kernel.org>,
	stable@vger.kernel.org
Subject: Re: [RESEND PATCH 5.15 v3 5/5] counter: 104-quad-8: Fix race condition between FLAG and CNTR reads
Date: Tue, 2 May 2023 20:12:56 -0400	[thread overview]
Message-ID: <ZFGnCJipt1Dwlxvo@fedora> (raw)
In-Reply-To: <ZFDHe0a7kcJXQoNM@duo.ucw.cz>

[-- Attachment #1: Type: text/plain, Size: 2179 bytes --]

On Tue, May 02, 2023 at 10:19:07AM +0200, Pavel Machek wrote:
> Hi!
> 
> > On Tue, Apr 11, 2023 at 11:52:20AM -0400, William Breathitt Gray wrote:
> > > 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.
> 
> I believe usual solution is to read the carry, read the counter, and
> read the carry again. If old_carry = new_carry, we are pretty sure we
> did not hit the race, and can use 25 bit value.
> 
> Best regards,
> 									Pavel
> -- 
> People of Russia, stop Putin before his war on Ukraine escalates.

That solution might work if the counter only increases, but if the
counter is straddling the zero threshold then the Carry bit will toggle
as the count overflows, underflows, and overflows again. For example:

    * START
        * Carry=0,Count=MAX
    * > Counting up...
        * Carry=1,Count=0
    * DRIVER READS Carry=1
    * > Counting down...
        * Carry=1,Count=MAX
    * > Counting up...
        * Carry=0,Count=0
    * > Counting up...
        * Carry=0,Count=42
    * DRIVER READS Count=42
    * > Counting down...
        * Carry=1,Count=MAX
    * DRIVER READS Carry=1
        * old_carry = new_carry
    * FINAL COUNT: Carry=1,Count=42
        * This was never a state the counter actually reported

Ultimately, the issue is that we have no way to get both Carry and Count
atomically from the device. As long as there's a race condition there,
we can't prevent possibly misinterpreting the Carry and Count values, so
we unfortunately cannot ensure that our 25-bit Count value is an actual
state the counter reported.

William Breathitt Gray

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 228 bytes --]

      reply	other threads:[~2023-05-03  0:13 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-11 15:52 [RESEND PATCH 4.14 v3 1/5] iio: counter: 104-quad-8: Fix race condition between FLAG and CNTR reads William Breathitt Gray
2023-04-11 15:52 ` [RESEND PATCH 4.19 v3 2/5] " William Breathitt Gray
2023-04-11 15:52 ` [RESEND PATCH 5.4 v3 3/5] " William Breathitt Gray
2023-04-11 15:52 ` [RESEND PATCH 5.10 v3 4/5] " William Breathitt Gray
2023-04-11 15:52 ` [RESEND PATCH 5.15 v3 5/5] " William Breathitt Gray
2023-04-17 13:40   ` William Breathitt Gray
2023-04-18  9:41     ` Greg Kroah-Hartman
2023-04-18 10:12       ` William Breathitt Gray
2023-05-02  8:19     ` Pavel Machek
2023-05-03  0:12       ` William Breathitt Gray [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ZFGnCJipt1Dwlxvo@fedora \
    --to=william.gray@linaro.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=jic23@kernel.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pavel@ucw.cz \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox