From: Peter Zijlstra <peterz@infradead.org>
To: Wang Rui <wangrui@loongson.cn>
Cc: Guo Ren <guoren@kernel.org>, Boqun Feng <boqun.feng@gmail.com>,
Huacai Chen <chenhuacai@gmail.com>,
Geert Uytterhoeven <geert@linux-m68k.org>,
Huacai Chen <chenhuacai@loongson.cn>,
Ingo Molnar <mingo@redhat.com>, Will Deacon <will@kernel.org>,
Arnd Bergmann <arnd@arndb.de>, Waiman Long <longman@redhat.com>,
Linux-Arch <linux-arch@vger.kernel.org>,
Xuefeng Li <lixuefeng@loongson.cn>,
Jiaxun Yang <jiaxun.yang@flygoat.com>
Subject: Re: Re: [PATCH RFC 1/2] arch: Introduce ARCH_HAS_HW_XCHG_SMALL
Date: Tue, 27 Jul 2021 13:03:03 +0200 [thread overview]
Message-ID: <YP/n59uBjt8O4PlX@hirez.programming.kicks-ass.net> (raw)
In-Reply-To: <3897b19d.ff7d.17ae5a9c6e9.Coremail.wangrui@loongson.cn>
On Tue, Jul 27, 2021 at 09:52:26AM +0800, Wang Rui wrote:
> I think the forward progress are guaranteed while all operations are
> atomic(ll/sc or amo). If ll/sc runs on a fast cpu, there will be
> random delays, is that okay? Else, for such hardware, we can't even
> implement generic spinlock with ll/sc.
>
> And I also think that the hardware supports normal store for
> unlocking. (e.g. arch_spin_unlock)
>
> In qspinlock, when _Q_PENDING_BITS == 1, it's available for all
> hardware, because the clear_pending/clear_pending_set_locked are all
> atomic operations. Isn't it?
>
> Q: Why live lock happens while _Q_PENDING_BITS == 8?
> A: I found a case is:
>
> * CPU A updates sub-word of qpsinlock at high frequency with normal store.
> * CPU B do xchg_tail with load + cmpxchg, and the value of load is always not equal to the value of ll(cmpxchg).
>
> qspinlock:
> 0: locked
> 1: pending
> 2: tail
>
> CPU A CPU B
> 1: 1: <--------------------+
> sh $newval, &locked lw $v1, &qspinlock |
> add $newval, 1 and $t1, $v1, ~mask |
> b 1b or $t1, $t1, newval | (live lock path)
> ll $v2, &qspinlock |
> bne $v1, $v2, 1b -----+
> sc $t1, &qspinlock
> beq $t1, 0, 1b
>
> If xchg_tail like this, at least there is no live lock on Loongson
>
> xchg_tail:
>
> 1:
> ll $v1, &qspinlock
> and $t1, $v1, ~mask
> or $t1, $t1, newval
> sc $t1, &qspinlock
> beq $t1, 0, 1b
>
> For hardware that ll/sc is based on cache coherency, I think sc is
> easy to succeed. The ll makes cache-line is exclusive by CPU B, and
> the store of CPU A needs to acquire exclusive again, the sc may be
> completed before this.
This! I've been saying this for ages. All those xchg16() implementations
are broken for using cmpxchg() on LL/SC. Not because xchg16() is
fundamentally flawed.
Perhaps we should introduce:
atomic_nand_or() and atomic_fetch_nand_or()
and implement short xchg() using those, then we can have the whole masks
setup shared. It just means you get to implement those primitives for
*all* archs :-)
Also, the _Q_PENDING_BITS==1 case can use that primitive.
next prev parent reply other threads:[~2021-07-27 11:03 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-07-24 12:36 [PATCH RFC 1/2] arch: Introduce ARCH_HAS_HW_XCHG_SMALL Huacai Chen
2021-07-24 12:36 ` [PATCH RFC 2/2] qspinlock: Use ARCH_HAS_HW_XCHG_SMALL to select _Q_PENDING_BITS definition Huacai Chen
2021-07-24 19:24 ` [PATCH RFC 1/2] arch: Introduce ARCH_HAS_HW_XCHG_SMALL Arnd Bergmann
2021-07-25 3:06 ` Jiaxun Yang
2021-07-25 10:08 ` Arnd Bergmann
2021-07-26 8:36 ` Geert Uytterhoeven
2021-07-26 8:56 ` Huacai Chen
2021-07-26 10:39 ` Boqun Feng
2021-07-26 16:41 ` Guo Ren
2021-07-26 17:03 ` Boqun Feng
2021-07-26 21:20 ` Waiman Long
2021-07-27 1:27 ` Guo Ren
2021-07-27 2:29 ` Boqun Feng
2021-07-27 2:46 ` Waiman Long
2021-07-27 11:05 ` Peter Zijlstra
2021-07-28 10:40 ` Huacai Chen
2021-07-28 11:01 ` Peter Zijlstra
2021-07-29 16:38 ` Huacai Chen
2021-07-27 10:17 ` Peter Zijlstra
2021-07-27 1:07 ` Guo Ren
2021-07-27 1:52 ` Wang Rui
2021-07-27 11:03 ` Peter Zijlstra [this message]
2021-07-27 2:00 ` Boqun Feng
2021-07-27 10:50 ` Peter Zijlstra
2021-07-27 10:12 ` Peter Zijlstra
2021-07-26 9:00 ` Arnd Bergmann
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=YP/n59uBjt8O4PlX@hirez.programming.kicks-ass.net \
--to=peterz@infradead.org \
--cc=arnd@arndb.de \
--cc=boqun.feng@gmail.com \
--cc=chenhuacai@gmail.com \
--cc=chenhuacai@loongson.cn \
--cc=geert@linux-m68k.org \
--cc=guoren@kernel.org \
--cc=jiaxun.yang@flygoat.com \
--cc=linux-arch@vger.kernel.org \
--cc=lixuefeng@loongson.cn \
--cc=longman@redhat.com \
--cc=mingo@redhat.com \
--cc=wangrui@loongson.cn \
--cc=will@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;
as well as URLs for NNTP newsgroup(s).