From: Catalin Marinas <catalin.marinas@arm.com>
To: Valentin Schneider <valentin.schneider@arm.com>
Cc: Nikolay Borisov <nborisov@suse.com>,
linux-btrfs@vger.kernel.org, paulmck@linux.ibm.com,
andrea.parri@amarulasolutions.com, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2 0/2] Refactor snapshot vs nocow writers locking
Date: Mon, 29 Jul 2019 16:33:19 +0100 [thread overview]
Message-ID: <20190729153319.GH2368@arrakis.emea.arm.com> (raw)
In-Reply-To: <ed015bb1-490e-7102-d172-73c1d069476c@arm.com>
Some nitpicking below:
On Mon, Jul 29, 2019 at 03:13:42PM +0100, Valentin Schneider wrote:
> specs.tla:
>
> ---- MODULE specs ----
> EXTENDS Integers, Sequences, TLC
>
> CONSTANTS
> NR_WRITERS,
> NR_READERS,
> WRITER_TASK,
> READER_TASK
>
> WRITERS == {WRITER_TASK} \X (1..NR_WRITERS)
> READERS == {READER_TASK} \X (1..NR_READERS)
> THREADS == WRITERS \union READERS
Recommendation: use symbolic values for WRITERS and READERS (defined in
.cfg: e.g. r1, r2, r3, w1, w2, w2). It allows you do to symmetry
optimisations. We've also hit a TLC bug in the past with process values
made up of a Cartesian product (though it may have been fixed since).
> macro ReadLock(tid)
> {
> if (lock_state = "idle" \/ lock_state = "read_locked") {
> lock_state := "read_locked";
> threads[tid] := "read_locked";
> } else {
> assert lock_state = "write_locked";
> \* waiting for writers to finish
> threads[tid] := "write_waiting";
> await lock_state = "" \/ lock_state = "read_locked";
lock_state = "idle"?
> macro WriteLock(tid)
> {
> if (lock_state = "idle" \/ lock_state = "write_locked") {
> lock_state := "write_locked";
> threads[tid] := "write_locked";
> } else {
> assert lock_state = "read_locked";
> \* waiting for readers to finish
> threads[tid] := "read_waiting";
> await lock_state = "idle" \/ lock_state = "write_locked";
> };
> }
I'd say that's one of the pitfalls of PlusCal. The above is executed
atomically, so you'd have the lock_state read and updated in the same
action. Looking at the C patches, there is an
atomic_read(&lock->readers) followed by a
percpu_counter_inc(&lock->writers). Between these two, you can have
"readers" becoming non-zero via a different CPU.
My suggestion would be to use procedures with labels to express the
non-atomicity of such sequences.
> macro ReadUnlock(tid) {
> if (threads[tid] = "read_locked") {
> threads[tid] := "idle";
> if (\A thread \in THREADS: threads[thread] # "read_locked") {
> \* we were the last read holder, everyone else should be waiting, unlock the lock
> lock_state := "idle";
> };
> };
> }
I'd make this close to the proposed C code with atomic counters. You'd
not be able to check each thread atomically in practice anyway.
--
Catalin
next prev parent reply other threads:[~2019-07-29 15:33 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-07-19 8:39 [PATCH v2 0/2] Refactor snapshot vs nocow writers locking Nikolay Borisov
2019-07-19 8:39 ` [PATCH v2 1/2] btrfs: Implement DRW lock Nikolay Borisov
2019-07-19 8:39 ` [PATCH v2 2/2] btrfs: convert snapshot/nocow exlcusion to drw lock Nikolay Borisov
2019-07-19 8:48 ` [RFC PATCH] btrfs: Hook btrfs' DRW lock to locktorture infrastructure Nikolay Borisov
2019-08-05 16:36 ` Nathan Chancellor
2019-08-05 18:17 ` David Sterba
2019-07-29 14:13 ` [PATCH v2 0/2] Refactor snapshot vs nocow writers locking Valentin Schneider
2019-07-29 15:33 ` Catalin Marinas [this message]
2019-07-29 16:32 ` Valentin Schneider
2019-07-30 11:03 ` Valentin Schneider
2019-07-30 12:11 ` Nikolay Borisov
2019-07-30 13:36 ` Valentin Schneider
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=20190729153319.GH2368@arrakis.emea.arm.com \
--to=catalin.marinas@arm.com \
--cc=andrea.parri@amarulasolutions.com \
--cc=linux-btrfs@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=nborisov@suse.com \
--cc=paulmck@linux.ibm.com \
--cc=valentin.schneider@arm.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;
as well as URLs for NNTP newsgroup(s).