The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Frederic Weisbecker <frederic@kernel.org>
To: "Paul E. McKenney" <paulmck@kernel.org>
Cc: boqun.feng@gmail.com, joel@joelfernandes.org,
	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: Thu, 15 Dec 2022 17:54:52 +0100	[thread overview]
Message-ID: <20221215165452.GA1957735@lothringen> (raw)
In-Reply-To: <20221214191355.GA2596199@paulmck-ThinkPad-P17-Gen-1>

On Wed, Dec 14, 2022 at 11:13:55AM -0800, Paul E. McKenney wrote:
> The comment in srcu_readers_active_idx_check() following the smp_mb()
> is out of date, hailing from a simpler time when preemption was disabled
> across the bulk of __srcu_read_lock().  The fact that preemption was
> disabled meant that the number of tasks that had fetched the old index
> but not yet incremented counters was limited by the number of CPUs.
> 
> In our more complex modern times, the number of CPUs is no longer a limit.
> This commit therefore updates this comment, additionally giving more
> memory-ordering detail.
> 
> Reported-by: Boqun Feng <boqun.feng@gmail.com>
> Reported-by: Frederic Weisbecker <frederic@kernel.org>

Not really, while you guys were debating on that comment, I was still starring
at the previous one (as usual).

Or to put it in an SRCU way, while you guys saw the flipped idx, I was still
using the old one :)

> -	 * OK, how about nesting?  This does impose a limit on nesting
> -	 * of floor(ULONG_MAX/NR_CPUS/2), which should be sufficient,
> -	 * especially on 64-bit systems.
> +	 * It can clearly do so once, given that it has already fetched
> +	 * the old value of ->srcu_idx and is just about to use that value
> +	 * to index its increment of ->srcu_lock_count[idx].  But as soon as
> +	 * it leaves that SRCU read-side critical section, it will increment
> +	 * ->srcu_unlock_count[idx], which must follow the updater's above
> +	 * read from that same value.  Thus, as soon the reading task does
> +	 * an smp_mb() and a later fetch from ->srcu_idx, that task will be
> +	 * guaranteed to get the new index.  Except that the increment of
> +	 * ->srcu_unlock_count[idx] in __srcu_read_unlock() is after the
> +	 * smp_mb(), and the fetch from ->srcu_idx in __srcu_read_lock()
> +	 * is before the smp_mb().  Thus, that task might not see the new
> +	 * value of ->srcu_idx until the -second- __srcu_read_lock(),
> +	 * which in turn means that this task might well increment
> +	 * ->srcu_lock_count[idx] for the old value of ->srcu_idx twice,
> +	 * not just once.

You lost me on that one.

      UPDATER                               READER
      -------                               ------
      //srcu_readers_lock_idx               //srcu_read_lock
      idx = ssp->srcu_idx;                  idx = ssp->srcu_idx;
      READ srcu_lock_count[idx ^ 1]         srcu_lock_count[idx]++
      smp_mb();                             smp_mb();
      //flip_index                          /* srcu_read_unlock (ignoring on purpose) */
      ssp->srcu_idx++;                      /* smp_mb(); */
      smp_mb();                             /* srcu_unlock_count[old_idx]++ */
      //srcu_readers_lock_idx               //srcu_read_lock again
      idx = ssp->srcu_idx;                  idx = ssp->srcu_idx;
      READ srcu_lock_count[idx ^ 1]
                                            

Scenario for the reader to increment the old idx once:

_ Assume ssp->srcu_idx is initially 0.
_ The READER reads idx that is 0
_ The updater runs and flips the idx that is now 1
_ The reader resumes with 0 as an index but on the next srcu_read_lock()
  it will see the new idx which is 1

What could be the scenario for it to increment the old idx twice?

Thanks.

  parent reply	other threads:[~2022-12-15 16:55 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 [this message]
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
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=20221215165452.GA1957735@lothringen \
    --to=frederic@kernel.org \
    --cc=boqun.feng@gmail.com \
    --cc=joel@joelfernandes.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