From: Peter Zijlstra <peterz@infradead.org>
To: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Will Deacon <will.deacon@arm.com>,
Nicholas Piggin <npiggin@gmail.com>,
David Miller <davem@davemloft.net>,
"linux-arch@vger.kernel.org" <linux-arch@vger.kernel.org>,
Linux Kernel Mailing List <linux-kernel@vger.kernel.org>,
Anton Blanchard <anton@samba.org>,
linuxppc-dev list <linuxppc-dev@ozlabs.org>
Subject: Re: [RFC][PATCH] spin loop arch primitives for busy waiting
Date: Thu, 6 Apr 2017 21:23:42 +0200 [thread overview]
Message-ID: <20170406192342.GC3093@worktop> (raw)
In-Reply-To: <CA+55aFzTn9e=F68SOtEvvZLdT6zCj9+gLc-OS7qhDKdM7zaasA@mail.gmail.com>
On Thu, Apr 06, 2017 at 10:31:46AM -0700, Linus Torvalds wrote:
> And we'd probably want to make it even more strict, in that soem mwait
> implementations might simply not be very good for short waits.
Yeah, we need to find something that works; assuming its beneficial at
all on modern chips.
> > But it builds and boots, no clue if its better or worse. Changing mwait
> > eax to 0 would give us C1 and might also be worth a try I suppose.
>
> Hmm. Also:
>
> > + ___mwait(0xf0 /* C0 */, 0x01 /* INT */); \
>
> Do you actually want that "INT" bit set? It's only meaningful if
> interrupts are _masked_, afaik. Which doesn't necessarily make sense
> for this case.
Correct, in fact its quite broken. Corrected.
> But maybe "monitor" is really cheap. I suspect it's microcoded,
> though, which implies "no".
Something like so then. According to the SDM mwait is a no-op if we do
not execute monitor first. So this variant should get the first
iteration without expensive instructions.
And this variant is actually quite amenable to alternative(), with which
we can pick between PAUSE and this.
---
arch/x86/include/asm/barrier.h | 30 ++++++++++++++++++++++++++++++
1 file changed, 30 insertions(+)
diff --git a/arch/x86/include/asm/barrier.h b/arch/x86/include/asm/barrier.h
index bfb28ca..2b5d5a2 100644
--- a/arch/x86/include/asm/barrier.h
+++ b/arch/x86/include/asm/barrier.h
@@ -80,6 +80,36 @@ do { \
#define __smp_mb__before_atomic() barrier()
#define __smp_mb__after_atomic() barrier()
+static inline void ___monitor(const void *eax, unsigned long ecx,
+ unsigned long edx)
+{
+ /* "monitor %eax, %ecx, %edx;" */
+ asm volatile(".byte 0x0f, 0x01, 0xc8;"
+ :: "a" (eax), "c" (ecx), "d"(edx));
+}
+
+static inline void ___mwait(unsigned long eax, unsigned long ecx)
+{
+ /* "mwait %eax, %ecx;" */
+ asm volatile(".byte 0x0f, 0x01, 0xc9;"
+ :: "a" (eax), "c" (ecx));
+}
+
+#define smp_cond_load_acquire(ptr, cond_expr) \
+({ \
+ typeof(ptr) __PTR = (ptr); \
+ typeof(*ptr) VAL; \
+ for (;;) { \
+ VAL = READ_ONCE(*__PTR); \
+ if (cond_expr) \
+ break; \
+ ___mwait(0xf0 /* C0 */, 0); \
+ ___monitor(__PTR, 0, 0); \
+ } \
+ smp_acquire__after_ctrl_dep(); \
+ VAL; \
+})
+
#include <asm-generic/barrier.h>
#endif /* _ASM_X86_BARRIER_H */
next prev parent reply other threads:[~2017-04-06 19:23 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20170403081328.30266-1-npiggin@gmail.com>
[not found] ` <CA+55aFx92vOh28CWp5zid8RzbM=5pO0Or51zS4D8M97L=69hHA@mail.gmail.com>
2017-04-03 23:50 ` [RFC][PATCH] spin loop arch primitives for busy waiting Nicholas Piggin
2017-04-04 0:43 ` Linus Torvalds
2017-04-04 3:02 ` Nicholas Piggin
2017-04-04 4:11 ` Nicholas Piggin
2017-04-05 14:01 ` David Miller
2017-04-06 0:59 ` Nicholas Piggin
2017-04-06 14:13 ` Will Deacon
2017-04-06 15:16 ` Linus Torvalds
2017-04-06 16:36 ` Peter Zijlstra
2017-04-06 17:31 ` Linus Torvalds
2017-04-06 19:23 ` Peter Zijlstra [this message]
2017-04-06 19:41 ` Linus Torvalds
2017-04-07 3:31 ` Nicholas Piggin
2017-04-07 9:43 ` Peter Zijlstra
2017-04-07 11:26 ` Nicholas Piggin
2017-04-06 15:30 ` Nicholas Piggin
2017-04-07 16:13 ` Will Deacon
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=20170406192342.GC3093@worktop \
--to=peterz@infradead.org \
--cc=anton@samba.org \
--cc=davem@davemloft.net \
--cc=linux-arch@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linuxppc-dev@ozlabs.org \
--cc=npiggin@gmail.com \
--cc=torvalds@linux-foundation.org \
--cc=will.deacon@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).