The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Joel Fernandes <joel@joelfernandes.org>
To: "Paul E. McKenney" <paulmck@kernel.org>
Cc: Frederic Weisbecker <frederic@kernel.org>,
	boqun.feng@gmail.com, neeraj.iitr10@gmail.com, urezki@gmail.com,
	rcu@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH RFC] srcu: Yet more detail for srcu_readers_active_idx_check() comments
Date: Fri, 16 Dec 2022 16:39:31 +0000	[thread overview]
Message-ID: <Y5yfQwUpCU5cEQpE@google.com> (raw)
In-Reply-To: <Y5ydp2YPRqGHq+eM@google.com>

On Fri, Dec 16, 2022 at 04:32:39PM +0000, Joel Fernandes wrote:
> On Thu, Dec 15, 2022 at 05:09:14PM -0800, Paul E. McKenney wrote:
> [...]
> > > >> 2. unlock()'s smp_mb() happened before Flip+smp_mb() , now the reader
> > > >> has no new smp_mb() that happens AFTER the flip happened. So it can
> > > >> totally sample the old idx again -- that particular reader will
> > > >> increment twice, but the next time, it will see the flipped one.
> > > > 
> > > > I will let you transliterate both.  ;-)
> > > 
> > > I think I see what you mean now :)
> > > 
> > > I believe the access I am referring to is the read of idx on one side and
> > > the write to idx on the other. However that is incomplete and I need to
> > > pair that with some of other access on both sides.
> > > 
> > > So perhaps this:
> > > 
> > > Writer does flip + smp_mb + read unlock counts [1]
> > > 
> > > Reader does:
> > >  read idx + smp_mb() + increment lock counts [2]
> > > 
> > > And subsequently reader does
> > > Smp_mb() + increment unlock count. [3]
> > > 
> > > So [1] races with either [2] or [2]+[3].
> > > 
> > > Is that fair?
> > 
> > That does look much better, thank you!
> 
> Perhaps a comment with an ASCII diagram will help?
> 
> Case 2:

And sorry I did not mention that Case 1 for me is the more trivial one where
the reader is preempted after sampling idx, and then all of them do
lock+unlock in quick succession to induce the counter-delta wrap around.

thanks,

 - Joel


> Both the reader and the updater see each other's writes too late, but because
> of memory barriers on both sides, they will eventually see each other's write
> with respect to their own. This is similar to the store-buffer problem. This
> let's a single reader contribute a maximum (unlock minus lock) imbalance of 2.
> 
> The following diagram shows the subtle worst case followed by a simplified
> store-buffer explanation.
> 
> READER                  UPDATER
> -------------           ----------
>                            // idx is initially 0.
> read_lock() {
>   READ(idx) = 0;
>   lock[0]++; --------------------------------------------,
>                            flip() {                      |               
>                               smp_mb();                  |
>   smp_mb();                                              |
> }                                                        |
>                                                          |
> // RSCS                                                  |
>                                                          |
> read_unlock() {                                          |
>   smp_mb();                                              |
>                               idx++;  // P               |
>                               smp_mb();                  |
>                            }                             |
>                                                          |
>                            scan_readers_idx(0) {         |
>                                count all unlock[0];      |
>                                    |                     |
>                                    |                     |
>   unlock[0]++; //X <--not-counted--`-----,               |
>                                          |               |
> }                                        V               `------,
>                                // Will make sure next scan      |
>                                // will not miss this unlock (X) |
>                                // if other side saw flip (P) ,--`
>                                // Call this MB [1]           |
>                                // Order write(idx) with      |
>                                // next scan's unlock.        |
>                                smp_mb();                 ,---`
> read_lock() {                                            |
>   READ(idx)=0;                                           |
>   lock[0]++; ----------------> count all lock[0];        |
>   smp_mb();         |     }                              |
> }     |             |                                    V
>       |             `---> // Incorrect contribution to lock counting
>       |                   // upto a maximum of 2 times.
>       |
>        `---> // Pairs with MB [1]. Makes sure that
>              // the next read_lock()'s' idx read (Y) is ordered
>              // with above write to unlock[0] (X).
>                             |
> rcu_read_unlock() {         |
>   smp_mb(); <---------------`
>   unlock[0]++; 
> }
> 
> read_lock() {
>   READ(idx) = 1; //Y
>   lock[1]++;
>   ...
> }
>                            scan_readers_idx(0) {
>                                count all unlock[0]; //Q
>                                ...
>                           }
> 
> This makes it similar to the store buffer pattern. Using X, Y, P and Q
> annotated above, we get:
> 
> READER                    UPDATER
> X (write)                 P (write)
> 
> smp_mb();                 smp_mb();
> 
> Y (read)                  Q (read)
> 
> 
> thanks,
> 
>  - Joel
> 

  reply	other threads:[~2022-12-16 16:41 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-14 19:13 [PATCH RFC] srcu: Yet more detail for srcu_readers_active_idx_check() comments Paul E. McKenney
2022-12-14 20:51 ` Joel Fernandes
2022-12-14 21:24   ` Paul E. McKenney
2022-12-14 23:07     ` Joel Fernandes
2022-12-14 23:10       ` Joel Fernandes
2022-12-14 23:14         ` Joel Fernandes
2022-12-15  0:04           ` Paul E. McKenney
2022-12-15  1:34             ` Joel Fernandes
2022-12-15  1:39               ` Paul E. McKenney
2022-12-15  0:01       ` Paul E. McKenney
2022-12-15 16:54 ` Frederic Weisbecker
2022-12-15 17:08   ` Paul E. McKenney
2022-12-15 17:48     ` Joel Fernandes
2022-12-15 17:58       ` Joel Fernandes
2022-12-15 20:13         ` Paul E. McKenney
2022-12-15 22:13           ` Joel Fernandes
2022-12-15 22:22             ` Joel Fernandes
2022-12-16  1:13               ` Paul E. McKenney
2022-12-16  1:09             ` Paul E. McKenney
2022-12-16 16:32               ` Joel Fernandes
2022-12-16 16:39                 ` Joel Fernandes [this message]
2022-12-16 16:51                 ` Paul E. McKenney
2022-12-16 16:54                   ` Joel Fernandes
2022-12-16 17:13                     ` Paul E. McKenney
2022-12-17  3:19                     ` Joel Fernandes
2022-12-17  3:21                       ` Joel Fernandes
2022-12-17  5:15                         ` Paul E. McKenney
2022-12-17  6:32                           ` Joel Fernandes
2022-12-15 19:58       ` Paul E. McKenney
2022-12-15 20:03         ` Joel Fernandes
2022-12-15 20:33           ` Joel Fernandes
2022-12-15 21:39             ` Paul E. McKenney
2022-12-15 21:42               ` Joel Fernandes
2022-12-15 22:10                 ` Paul E. McKenney
2022-12-15 22:16                   ` Joel Fernandes
2022-12-15 17:58     ` Frederic Weisbecker
2022-12-15 18:53       ` Paul E. McKenney

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=Y5yfQwUpCU5cEQpE@google.com \
    --to=joel@joelfernandes.org \
    --cc=boqun.feng@gmail.com \
    --cc=frederic@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=neeraj.iitr10@gmail.com \
    --cc=paulmck@kernel.org \
    --cc=rcu@vger.kernel.org \
    --cc=urezki@gmail.com \
    /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