From: Waiman Long <longman@redhat.com>
To: Guo Ren <guoren@kernel.org>
Cc: Guo Ren <guoren@linux.alibaba.com>,
kvm@vger.kernel.org, linux-doc@vger.kernel.org,
peterz@infradead.org, catalin.marinas@arm.com,
bjorn@rivosinc.com, palmer@rivosinc.com,
virtualization@lists.linux-foundation.org,
conor.dooley@microchip.com, jszhang@kernel.org,
linux-riscv@lists.infradead.org, will@kernel.org,
keescook@chromium.org, linux-arch@vger.kernel.org,
anup@brainfault.org, linux-csky@vger.kernel.org,
xiaoguang.xing@sophgo.com, mingo@redhat.com,
greentime.hu@sifive.com, ajones@ventanamicro.com,
alexghiti@rivosinc.com, paulmck@kernel.org, boqun.feng@gmail.com,
rostedt@goodmis.org, leobras@redhat.com,
paul.walmsley@sifive.com, tglx@linutronix.de,
rdunlap@infradead.org, wuwei2016@iscas.ac.cn, wefu@redhat.com
Subject: Re: [PATCH V11 04/17] locking/qspinlock: Improve xchg_tail for number of cpus >= 16k
Date: Mon, 11 Sep 2023 09:03:20 -0400 [thread overview]
Message-ID: <06714da1-d566-766f-7a13-a3c93b5953c4@redhat.com> (raw)
In-Reply-To: <CAJF2gTSbUUdLhN8PFdFzQd0M1T2MVOL1cdZn46WKq1S8MuQYHw@mail.gmail.com>
On 9/10/23 23:09, Guo Ren wrote:
> On Mon, Sep 11, 2023 at 10:35 AM Waiman Long <longman@redhat.com> wrote:
>>
>> On 9/10/23 04:28, guoren@kernel.org wrote:
>>> From: Guo Ren <guoren@linux.alibaba.com>
>>>
>>> The target of xchg_tail is to write the tail to the lock value, so
>>> adding prefetchw could help the next cmpxchg step, which may
>>> decrease the cmpxchg retry loops of xchg_tail. Some processors may
>>> utilize this feature to give a forward guarantee, e.g., RISC-V
>>> XuanTie processors would block the snoop channel & irq for several
>>> cycles when prefetch.w instruction (from Zicbop extension) retired,
>>> which guarantees the next cmpxchg succeeds.
>>>
>>> Signed-off-by: Guo Ren <guoren@linux.alibaba.com>
>>> Signed-off-by: Guo Ren <guoren@kernel.org>
>>> ---
>>> kernel/locking/qspinlock.c | 5 ++++-
>>> 1 file changed, 4 insertions(+), 1 deletion(-)
>>>
>>> diff --git a/kernel/locking/qspinlock.c b/kernel/locking/qspinlock.c
>>> index d3f99060b60f..96b54e2ade86 100644
>>> --- a/kernel/locking/qspinlock.c
>>> +++ b/kernel/locking/qspinlock.c
>>> @@ -223,7 +223,10 @@ static __always_inline void clear_pending_set_locked(struct qspinlock *lock)
>>> */
>>> static __always_inline u32 xchg_tail(struct qspinlock *lock, u32 tail)
>>> {
>>> - u32 old, new, val = atomic_read(&lock->val);
>>> + u32 old, new, val;
>>> +
>>> + prefetchw(&lock->val);
>>> + val = atomic_read(&lock->val);
>>>
>>> for (;;) {
>>> new = (val & _Q_LOCKED_PENDING_MASK) | tail;
>> That looks a bit weird. You pre-fetch and then immediately read it. How
>> much performance gain you get by this change alone?
>>
>> Maybe you can define an arch specific primitive that default back to
>> atomic_read() if not defined.
> Thx for the reply. This is a generic optimization point I would like
> to talk about with you.
>
> First, prefetchw() makes cacheline an exclusive state and serves for
> the next cmpxchg loop semantic, which writes the idx_tail part of
> arch_spin_lock. The atomic_read only makes cacheline in the shared
> state, which couldn't give any guarantee for the next cmpxchg loop
> semantic. Micro-architecture could utilize prefetchw() to provide a
> strong forward progress guarantee for the xchg_tail, e.g., the T-HEAD
> XuanTie processor would hold the exclusive cacheline state until the
> next cmpxchg write success.
>
> In the end, Let's go back to the principle: the xchg_tail is an atomic
> swap operation that contains write eventually, so giving a prefetchw()
> at the beginning is acceptable for all architectures..
> ••••••••••••
I did realize afterward that prefetchw gets the cacheline in exclusive
state. I will suggest you mention that in your commit log as well as
adding a comment about its purpose in the code.
Thanks,
Longman
>> Cheers,
>> Longman
>>
>
_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
next prev parent reply other threads:[~2023-09-11 13:03 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20230910082911.3378782-1-guoren@kernel.org>
[not found] ` <20230910082911.3378782-5-guoren@kernel.org>
2023-09-11 2:35 ` [PATCH V11 04/17] locking/qspinlock: Improve xchg_tail for number of cpus >= 16k Waiman Long
[not found] ` <CAJF2gTSbUUdLhN8PFdFzQd0M1T2MVOL1cdZn46WKq1S8MuQYHw@mail.gmail.com>
2023-09-11 13:03 ` Waiman Long [this message]
[not found] ` <CAJF2gTQ3Q7f+FGorVTR66c6TGWsSeeKVvLF+LH1_m3kSHrm0yA@mail.gmail.com>
[not found] ` <ZQF49GIZoFceUGYH@redhat.com>
[not found] ` <CAJF2gTTHdCr-FQVSGUc+LapkJPmDiEYYa_1P6T86uCjRujgnTg@mail.gmail.com>
2023-09-13 13:06 ` Waiman Long
[not found] ` <20230910082911.3378782-8-guoren@kernel.org>
2023-09-11 15:22 ` [PATCH V11 07/17] riscv: qspinlock: Introduce qspinlock param for command line Waiman Long
2023-09-11 15:34 ` Waiman Long
[not found] ` <CAJF2gTT2hRxgnQt+WJ9P0YBWnUaZJ1-9g3ZE9tOz_MiLSsUjwQ@mail.gmail.com>
[not found] ` <ZQK2-CIL9U_QdMjh@redhat.com>
2023-09-14 17:23 ` Waiman Long
[not found] ` <ZUlPwQVG4OTkighB@redhat.com>
2023-11-12 4:23 ` [PATCH V11 00/17] riscv: Add Native/Paravirt qspinlock support Guo Ren
2023-11-13 10:19 ` Leonardo Bras Soares Passos
2023-12-31 8:29 ` [PATCH V11 03/17] riscv: Use Zicbop in arch_xchg when available guoren
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=06714da1-d566-766f-7a13-a3c93b5953c4@redhat.com \
--to=longman@redhat.com \
--cc=ajones@ventanamicro.com \
--cc=alexghiti@rivosinc.com \
--cc=anup@brainfault.org \
--cc=bjorn@rivosinc.com \
--cc=boqun.feng@gmail.com \
--cc=catalin.marinas@arm.com \
--cc=conor.dooley@microchip.com \
--cc=greentime.hu@sifive.com \
--cc=guoren@kernel.org \
--cc=guoren@linux.alibaba.com \
--cc=jszhang@kernel.org \
--cc=keescook@chromium.org \
--cc=kvm@vger.kernel.org \
--cc=leobras@redhat.com \
--cc=linux-arch@vger.kernel.org \
--cc=linux-csky@vger.kernel.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-riscv@lists.infradead.org \
--cc=mingo@redhat.com \
--cc=palmer@rivosinc.com \
--cc=paul.walmsley@sifive.com \
--cc=paulmck@kernel.org \
--cc=peterz@infradead.org \
--cc=rdunlap@infradead.org \
--cc=rostedt@goodmis.org \
--cc=tglx@linutronix.de \
--cc=virtualization@lists.linux-foundation.org \
--cc=wefu@redhat.com \
--cc=will@kernel.org \
--cc=wuwei2016@iscas.ac.cn \
--cc=xiaoguang.xing@sophgo.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).