* Re: [PATCH 03/11] qspinlock: Add pending bit
@ 2014-06-17 23:23 Konrad Rzeszutek Wilk
2014-06-24 8:46 ` Peter Zijlstra
0 siblings, 1 reply; 12+ messages in thread
From: Konrad Rzeszutek Wilk @ 2014-06-17 23:23 UTC (permalink / raw)
To: Waiman Long
Cc: raghavendra.kt, mingo, riel, oleg, gleb, virtualization, tglx,
chegu_vinod, boris.ostrovsky, david.vrabel, linux-kernel,
linux-arch, paolo.bonzini, Peter Zijlstra, scott.norton,
torvalds, kvm, paulmck, xen-devel, Peter Zijlstra
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain; charset=utf-8, Size: 4502 bytes --]
On Jun 17, 2014 6:25 PM, Waiman Long <waiman.long@hp.com> wrote:
>
> On 06/17/2014 05:10 PM, Konrad Rzeszutek Wilk wrote:
> > On Tue, Jun 17, 2014 at 05:07:29PM -0400, Konrad Rzeszutek Wilk wrote:
> >> On Tue, Jun 17, 2014 at 04:51:57PM -0400, Waiman Long wrote:
> >>> On 06/17/2014 04:36 PM, Konrad Rzeszutek Wilk wrote:
> >>>> On Sun, Jun 15, 2014 at 02:47:00PM +0200, Peter Zijlstra wrote:
> >>>>> Because the qspinlock needs to touch a second cacheline; add a pending
> >>>>> bit and allow a single in-word spinner before we punt to the second
> >>>>> cacheline.
> >>>> Could you add this in the description please:
> >>>>
> >>>> And by second cacheline we mean the local 'node'. That is the:
> >>>> mcs_nodes[0] and mcs_nodes[idx]
> >>>>
> >>>> Perhaps it might be better then to split this in the header file
> >>>> as this is trying to not be a slowpath code - but rather - a
> >>>> pre-slow-path-lets-try-if-we can do another cmpxchg in case
> >>>> the unlocker has just unlocked itself.
> >>>>
> >>>> So something like:
> >>>>
> >>>> diff --git a/include/asm-generic/qspinlock.h b/include/asm-generic/qspinlock.h
> >>>> index e8a7ae8..29cc9c7 100644
> >>>> --- a/include/asm-generic/qspinlock.h
> >>>> +++ b/include/asm-generic/qspinlock.h
> >>>> @@ -75,11 +75,21 @@ extern void queue_spin_lock_slowpath(struct qspinlock *lock, u32 val);
> >>>>Â Â Â */
> >>>>Â Â static __always_inline void queue_spin_lock(struct qspinlock *lock)
> >>>>Â Â {
> >>>> - u32 val;
> >>>> + u32 val, new;
> >>>>
> >>>>Â Â val = atomic_cmpxchg(&lock->val, 0, _Q_LOCKED_VAL);
> >>>>Â Â if (likely(val == 0))
> >>>>Â Â return;
> >>>> +
> >>>> + /* One more attempt - but if we fail mark it as pending. */
> >>>> + if (val == _Q_LOCKED_VAL) {
> >>>> + new = Q_LOCKED_VAL |_Q_PENDING_VAL;
> >>>> +
> >>>> + old = atomic_cmpxchg(&lock->val, val, new);
> >>>> + if (old == _Q_LOCKED_VAL) /* YEEY! */
> >>>> + return;
> >>> No, it can leave like that. The unlock path will not clear the pending bit.
> >> Err, you are right. It needs to go back in the slowpath.
> > What I should have wrote is:
> >
> > if (old == 0) /* YEEY */
> >Â Â Â return;
>
> Unfortunately, that still doesn't work. If old is 0, it just meant the
> cmpxchg failed. It still haven't got the lock.
> > As that would the same thing as this patch does on the pending bit - that
> > is if we can on the second compare and exchange set the pending bit (and the
> > lock) and the lock has been released - we are good.
>
> That is not true. When the lock is freed, the pending bit holder will
> still have to clear the pending bit and set the lock bit as is done in
> the slowpath. We cannot skip the step here. The problem of moving the
> pending code here is that it includes a wait loop which we don't want to
> put in the fastpath.
> >
> > And it is a quick path.
> >
> >>> We are trying to make the fastpath as simple as possible as it may be
> >>> inlined. The complexity of the queue spinlock is in the slowpath.
> >> Sure, but then it shouldn't be called slowpath anymore as it is not
> >> slow. It is a combination of fast path (the potential chance of
> >> grabbing the lock and setting the pending lock) and the real slow
> >> path (the queuing). Perhaps it should be called 'queue_spinlock_complex' ?
> >>
> > I forgot to mention - that was the crux of my comments - just change
> > the slowpath to complex name at that point to better reflect what
> > it does.
>
> Actually in my v11 patch, I subdivided the slowpath into a slowpath for
> the pending code and slowerpath for actual queuing. Perhaps, we could
> use quickpath and slowpath instead. Anyway, it is a minor detail that we
> can discuss after the core code get merged.
>
> -Longman
Why not do it the right way the first time around?
That aside - these optimization - seem to make the code harder to read. And they do remind me of the scheduler code in 2.6.x which was based on heuristics - and eventually ripped out.
So are these optimizations based on turning off certain hardware features? Say hardware prefetching?
What I am getting at - can the hardware do this at some point (or perhaps already does on IvyBridge-EX?) - that is prefetch the per-cpu areas so they are always hot? And rendering this optimization not needed?
Thanks!
ÿôèº{.nÇ+·®+%Ëÿ±éݶ\x17¥wÿº{.nÇ+·¥{±þG«éÿ{ayº\x1dÊÚë,j\a¢f£¢·hïêÿêçz_è®\x03(éÝ¢j"ú\x1a¶^[m§ÿÿ¾\a«þG«éÿ¢¸?¨èÚ&£ø§~á¶iOæ¬z·vØ^\x14\x04\x1a¶^[m§ÿÿÃ\fÿ¶ìÿ¢¸?I¥
^ permalink raw reply [flat|nested] 12+ messages in thread* Re: [PATCH 03/11] qspinlock: Add pending bit 2014-06-17 23:23 [PATCH 03/11] qspinlock: Add pending bit Konrad Rzeszutek Wilk @ 2014-06-24 8:46 ` Peter Zijlstra 0 siblings, 0 replies; 12+ messages in thread From: Peter Zijlstra @ 2014-06-24 8:46 UTC (permalink / raw) To: Konrad Rzeszutek Wilk Cc: Waiman Long, raghavendra.kt, mingo, riel, oleg, gleb, virtualization, tglx, chegu_vinod, boris.ostrovsky, david.vrabel, linux-kernel, linux-arch, paolo.bonzini, scott.norton, torvalds, kvm, paulmck, xen-devel On Tue, Jun 17, 2014 at 07:23:44PM -0400, Konrad Rzeszutek Wilk wrote: > > Actually in my v11 patch, I subdivided the slowpath into a slowpath for > > the pending code and slowerpath for actual queuing. Perhaps, we could > > use quickpath and slowpath instead. Anyway, it is a minor detail that we > > can discuss after the core code get merged. > Why not do it the right way the first time around? Because I told him to not do this. There's the fast path; the inline single trylock cmpxchg, and the slow path; the out-of-line thing doing the rest. Note that pretty much all other locking primitives are implemented similarly, with fast and slow paths. I find that having the entire state machine in a single function is easier. > That aside - these optimization - seem to make the code harder to > read. And they do remind me of the scheduler code in 2.6.x which was > based on heuristics - and eventually ripped out. Well, it increases the states and thereby the complexity, nothing to be done about that. Also, its not a random heuristic in the sense that it has odd behaviour. Its behaviour is very well controlled. Furthermore, without this the qspinlock performance is too far off the ticket lock performance to be a possible replacement. > So are these optimizations based on turning off certain hardware > features? Say hardware prefetching? We can try of course, but that doesn't help the code -- in fact, adding the switch to turn if off _adds_ code on top. > What I am getting at - can the hardware do this at some point (or > perhaps already does on IvyBridge-EX?) - that is prefetch the per-cpu > areas so they are always hot? And rendering this optimization not > needed? Got a ref to documentation on this new fancy stuff? I might have an IVB-EX, but I've not tried it yet. That said, memory fetches are 100s of cycles, and while prefetch can hide some of that, I'm not sure we can hide all of it, there's not _that_ much we do. If we observe the pending and locked bit set, we immediately drop to the queueing code and touch it. So there's only a few useful instructions to do. ^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH 00/11] qspinlock with paravirt support @ 2014-06-15 12:46 Peter Zijlstra 2014-06-15 12:47 ` [PATCH 03/11] qspinlock: Add pending bit Peter Zijlstra 0 siblings, 1 reply; 12+ messages in thread From: Peter Zijlstra @ 2014-06-15 12:46 UTC (permalink / raw) To: Waiman.Long, tglx, mingo Cc: linux-arch, linux-kernel, virtualization, xen-devel, kvm, paolo.bonzini, konrad.wilk, boris.ostrovsky, paulmck, riel, torvalds, raghavendra.kt, david.vrabel, oleg, gleb, scott.norton, chegu_vinod, Peter Zijlstra Since Waiman seems incapable of doing simple things; here's my take on the paravirt crap. The first few patches are taken from Waiman's latest series, but the virt support is completely new. Its primary aim is to not mess up the native code. I've not stress tested it, but the virt and paravirt (kvm) cases boot on simple smp guests. I've not done Xen, but the patch should be simple and similar. I ripped out all the unfair nonsense as its not at all required for paravirt and optimizations that make paravirt better at the cost of code clarity and/or native performance are just not worth it. Also; if we were to ever add some of that unfair nonsense you do so _after_ you got the simple things working. The thing I'm least sure about is the head tracking, I chose to do something different from what Waiman did, because his is O(nr_cpus) and had the assumption that guests have small nr_cpus. AFAIK this is not at all true. The biggest problem I have with what I did is that it contains wait loops itself. ^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH 03/11] qspinlock: Add pending bit 2014-06-15 12:46 [PATCH 00/11] qspinlock with paravirt support Peter Zijlstra @ 2014-06-15 12:47 ` Peter Zijlstra 2014-06-17 20:36 ` Konrad Rzeszutek Wilk 0 siblings, 1 reply; 12+ messages in thread From: Peter Zijlstra @ 2014-06-15 12:47 UTC (permalink / raw) To: Waiman.Long, tglx, mingo Cc: linux-arch, linux-kernel, virtualization, xen-devel, kvm, paolo.bonzini, konrad.wilk, boris.ostrovsky, paulmck, riel, torvalds, raghavendra.kt, david.vrabel, oleg, gleb, scott.norton, chegu_vinod, Peter Zijlstra [-- Attachment #1: peter_zijlstra-qspinlock-add_pending_bit.patch --] [-- Type: text/plain, Size: 6468 bytes --] Because the qspinlock needs to touch a second cacheline; add a pending bit and allow a single in-word spinner before we punt to the second cacheline. Signed-off-by: Peter Zijlstra <peterz@infradead.org> --- include/asm-generic/qspinlock_types.h | 12 ++- kernel/locking/qspinlock.c | 109 +++++++++++++++++++++++++++------- 2 files changed, 97 insertions(+), 24 deletions(-) --- a/include/asm-generic/qspinlock_types.h +++ b/include/asm-generic/qspinlock_types.h @@ -39,8 +39,9 @@ typedef struct qspinlock { * Bitfields in the atomic value: * * 0- 7: locked byte - * 8- 9: tail index - * 10-31: tail cpu (+1) + * 8: pending + * 9-10: tail index + * 11-31: tail cpu (+1) */ #define _Q_SET_MASK(type) (((1U << _Q_ ## type ## _BITS) - 1)\ << _Q_ ## type ## _OFFSET) @@ -48,7 +49,11 @@ typedef struct qspinlock { #define _Q_LOCKED_BITS 8 #define _Q_LOCKED_MASK _Q_SET_MASK(LOCKED) -#define _Q_TAIL_IDX_OFFSET (_Q_LOCKED_OFFSET + _Q_LOCKED_BITS) +#define _Q_PENDING_OFFSET (_Q_LOCKED_OFFSET + _Q_LOCKED_BITS) +#define _Q_PENDING_BITS 1 +#define _Q_PENDING_MASK _Q_SET_MASK(PENDING) + +#define _Q_TAIL_IDX_OFFSET (_Q_PENDING_OFFSET + _Q_PENDING_BITS) #define _Q_TAIL_IDX_BITS 2 #define _Q_TAIL_IDX_MASK _Q_SET_MASK(TAIL_IDX) @@ -57,5 +62,6 @@ typedef struct qspinlock { #define _Q_TAIL_CPU_MASK _Q_SET_MASK(TAIL_CPU) #define _Q_LOCKED_VAL (1U << _Q_LOCKED_OFFSET) +#define _Q_PENDING_VAL (1U << _Q_PENDING_OFFSET) #endif /* __ASM_GENERIC_QSPINLOCK_TYPES_H */ --- a/kernel/locking/qspinlock.c +++ b/kernel/locking/qspinlock.c @@ -83,24 +83,28 @@ static inline struct mcs_spinlock *decod return per_cpu_ptr(&mcs_nodes[idx], cpu); } +#define _Q_LOCKED_PENDING_MASK (_Q_LOCKED_MASK | _Q_PENDING_MASK) + /** * queue_spin_lock_slowpath - acquire the queue spinlock * @lock: Pointer to queue spinlock structure * @val: Current value of the queue spinlock 32-bit word * - * (queue tail, lock bit) - * - * fast : slow : unlock - * : : - * uncontended (0,0) --:--> (0,1) --------------------------------:--> (*,0) - * : | ^--------. / : - * : v \ | : - * uncontended : (n,x) --+--> (n,0) | : - * queue : | ^--' | : - * : v | : - * contended : (*,x) --+--> (*,0) -----> (*,1) ---' : - * queue : ^--' : + * (queue tail, pending bit, lock bit) * + * fast : slow : unlock + * : : + * uncontended (0,0,0) -:--> (0,0,1) ------------------------------:--> (*,*,0) + * : | ^--------.------. / : + * : v \ \ | : + * pending : (0,1,1) +--> (0,1,0) \ | : + * : | ^--' | | : + * : v | | : + * uncontended : (n,x,y) +--> (n,0,0) --' | : + * queue : | ^--' | : + * : v | : + * contended : (*,x,y) +--> (*,0,0) ---> (*,0,1) -' : + * queue : ^--' : */ void queue_spin_lock_slowpath(struct qspinlock *lock, u32 val) { @@ -110,6 +114,65 @@ void queue_spin_lock_slowpath(struct qsp BUILD_BUG_ON(CONFIG_NR_CPUS >= (1U << _Q_TAIL_CPU_BITS)); + /* + * trylock || pending + * + * 0,0,0 -> 0,0,1 ; trylock + * 0,0,1 -> 0,1,1 ; pending + */ + for (;;) { + /* + * If we observe any contention; queue. + */ + if (val & ~_Q_LOCKED_MASK) + goto queue; + + new = _Q_LOCKED_VAL; + if (val == new) + new |= _Q_PENDING_VAL; + + old = atomic_cmpxchg(&lock->val, val, new); + if (old == val) + break; + + val = old; + } + + /* + * we won the trylock + */ + if (new == _Q_LOCKED_VAL) + return; + + /* + * we're pending, wait for the owner to go away. + * + * *,1,1 -> *,1,0 + */ + while ((val = atomic_read(&lock->val)) & _Q_LOCKED_MASK) + cpu_relax(); + + /* + * take ownership and clear the pending bit. + * + * *,1,0 -> *,0,1 + */ + for (;;) { + new = (val & ~_Q_PENDING_MASK) | _Q_LOCKED_VAL; + + old = atomic_cmpxchg(&lock->val, val, new); + if (old == val) + break; + + val = old; + } + return; + + /* + * End of pending bit optimistic spinning and beginning of MCS + * queuing. + */ +queue: node = this_cpu_ptr(&mcs_nodes[0]); idx = node->count++; tail = encode_tail(smp_processor_id(), idx); @@ -119,15 +182,18 @@ void queue_spin_lock_slowpath(struct qsp node->next = NULL; /* + * we already touched the queueing cacheline; don't bother with pending + * stuff. + * * trylock || xchg(lock, node) * - * 0,0 -> 0,1 ; trylock - * p,x -> n,x ; prev = xchg(lock, node) + * 0,0,0 -> 0,0,1 ; trylock + * p,y,x -> n,y,x ; prev = xchg(lock, node) */ for (;;) { new = _Q_LOCKED_VAL; if (val) - new = tail | (val & _Q_LOCKED_MASK); + new = tail | (val & _Q_LOCKED_PENDING_MASK); old = atomic_cmpxchg(&lock->val, val, new); if (old == val) @@ -145,7 +211,7 @@ void queue_spin_lock_slowpath(struct qsp /* * if there was a previous node; link it and wait. */ - if (old & ~_Q_LOCKED_MASK) { + if (old & ~_Q_LOCKED_PENDING_MASK) { prev = decode_tail(old); ACCESS_ONCE(prev->next) = node; @@ -153,18 +219,19 @@ void queue_spin_lock_slowpath(struct qsp } /* - * we're at the head of the waitqueue, wait for the owner to go away. + * we're at the head of the waitqueue, wait for the owner & pending to + * go away. * - * *,x -> *,0 + * *,x,y -> *,0,0 */ - while ((val = atomic_read(&lock->val)) & _Q_LOCKED_MASK) + while ((val = atomic_read(&lock->val)) & _Q_LOCKED_PENDING_MASK) cpu_relax(); /* * claim the lock: * - * n,0 -> 0,1 : lock, uncontended - * *,0 -> *,1 : lock, contended + * n,0,0 -> 0,0,1 : lock, uncontended + * *,0,0 -> *,0,1 : lock, contended */ for (;;) { new = _Q_LOCKED_VAL; ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH 03/11] qspinlock: Add pending bit 2014-06-15 12:47 ` [PATCH 03/11] qspinlock: Add pending bit Peter Zijlstra @ 2014-06-17 20:36 ` Konrad Rzeszutek Wilk 2014-06-17 20:51 ` Waiman Long ` (2 more replies) 0 siblings, 3 replies; 12+ messages in thread From: Konrad Rzeszutek Wilk @ 2014-06-17 20:36 UTC (permalink / raw) To: Peter Zijlstra Cc: Waiman.Long, tglx, mingo, linux-arch, linux-kernel, virtualization, xen-devel, kvm, paolo.bonzini, boris.ostrovsky, paulmck, riel, torvalds, raghavendra.kt, david.vrabel, oleg, gleb, scott.norton, chegu_vinod, Peter Zijlstra On Sun, Jun 15, 2014 at 02:47:00PM +0200, Peter Zijlstra wrote: > Because the qspinlock needs to touch a second cacheline; add a pending > bit and allow a single in-word spinner before we punt to the second > cacheline. Could you add this in the description please: And by second cacheline we mean the local 'node'. That is the: mcs_nodes[0] and mcs_nodes[idx] Perhaps it might be better then to split this in the header file as this is trying to not be a slowpath code - but rather - a pre-slow-path-lets-try-if-we can do another cmpxchg in case the unlocker has just unlocked itself. So something like: diff --git a/include/asm-generic/qspinlock.h b/include/asm-generic/qspinlock.h index e8a7ae8..29cc9c7 100644 --- a/include/asm-generic/qspinlock.h +++ b/include/asm-generic/qspinlock.h @@ -75,11 +75,21 @@ extern void queue_spin_lock_slowpath(struct qspinlock *lock, u32 val); */ static __always_inline void queue_spin_lock(struct qspinlock *lock) { - u32 val; + u32 val, new; val = atomic_cmpxchg(&lock->val, 0, _Q_LOCKED_VAL); if (likely(val == 0)) return; + + /* One more attempt - but if we fail mark it as pending. */ + if (val == _Q_LOCKED_VAL) { + new = Q_LOCKED_VAL |_Q_PENDING_VAL; + + old = atomic_cmpxchg(&lock->val, val, new); + if (old == _Q_LOCKED_VAL) /* YEEY! */ + return; + val = old; + } queue_spin_lock_slowpath(lock, val); } and then the slowpath preserves most of the old logic path (with the pending bit stuff)? > > Signed-off-by: Peter Zijlstra <peterz@infradead.org> > --- > include/asm-generic/qspinlock_types.h | 12 ++- > kernel/locking/qspinlock.c | 109 +++++++++++++++++++++++++++------- > 2 files changed, 97 insertions(+), 24 deletions(-) > > --- a/include/asm-generic/qspinlock_types.h > +++ b/include/asm-generic/qspinlock_types.h > @@ -39,8 +39,9 @@ typedef struct qspinlock { > * Bitfields in the atomic value: > * > * 0- 7: locked byte > - * 8- 9: tail index > - * 10-31: tail cpu (+1) > + * 8: pending > + * 9-10: tail index > + * 11-31: tail cpu (+1) > */ > #define _Q_SET_MASK(type) (((1U << _Q_ ## type ## _BITS) - 1)\ > << _Q_ ## type ## _OFFSET) > @@ -48,7 +49,11 @@ typedef struct qspinlock { > #define _Q_LOCKED_BITS 8 > #define _Q_LOCKED_MASK _Q_SET_MASK(LOCKED) > > -#define _Q_TAIL_IDX_OFFSET (_Q_LOCKED_OFFSET + _Q_LOCKED_BITS) > +#define _Q_PENDING_OFFSET (_Q_LOCKED_OFFSET + _Q_LOCKED_BITS) > +#define _Q_PENDING_BITS 1 > +#define _Q_PENDING_MASK _Q_SET_MASK(PENDING) > + > +#define _Q_TAIL_IDX_OFFSET (_Q_PENDING_OFFSET + _Q_PENDING_BITS) > #define _Q_TAIL_IDX_BITS 2 > #define _Q_TAIL_IDX_MASK _Q_SET_MASK(TAIL_IDX) > > @@ -57,5 +62,6 @@ typedef struct qspinlock { > #define _Q_TAIL_CPU_MASK _Q_SET_MASK(TAIL_CPU) > > #define _Q_LOCKED_VAL (1U << _Q_LOCKED_OFFSET) > +#define _Q_PENDING_VAL (1U << _Q_PENDING_OFFSET) > > #endif /* __ASM_GENERIC_QSPINLOCK_TYPES_H */ > --- a/kernel/locking/qspinlock.c > +++ b/kernel/locking/qspinlock.c > @@ -83,24 +83,28 @@ static inline struct mcs_spinlock *decod > return per_cpu_ptr(&mcs_nodes[idx], cpu); > } > > +#define _Q_LOCKED_PENDING_MASK (_Q_LOCKED_MASK | _Q_PENDING_MASK) > + > /** > * queue_spin_lock_slowpath - acquire the queue spinlock > * @lock: Pointer to queue spinlock structure > * @val: Current value of the queue spinlock 32-bit word > * > - * (queue tail, lock bit) > - * > - * fast : slow : unlock > - * : : > - * uncontended (0,0) --:--> (0,1) --------------------------------:--> (*,0) > - * : | ^--------. / : > - * : v \ | : > - * uncontended : (n,x) --+--> (n,0) | : > - * queue : | ^--' | : > - * : v | : > - * contended : (*,x) --+--> (*,0) -----> (*,1) ---' : > - * queue : ^--' : > + * (queue tail, pending bit, lock bit) > * > + * fast : slow : unlock > + * : : > + * uncontended (0,0,0) -:--> (0,0,1) ------------------------------:--> (*,*,0) > + * : | ^--------.------. / : > + * : v \ \ | : > + * pending : (0,1,1) +--> (0,1,0) \ | : > + * : | ^--' | | : > + * : v | | : > + * uncontended : (n,x,y) +--> (n,0,0) --' | : > + * queue : | ^--' | : > + * : v | : > + * contended : (*,x,y) +--> (*,0,0) ---> (*,0,1) -' : > + * queue : ^--' : > */ > void queue_spin_lock_slowpath(struct qspinlock *lock, u32 val) > { > @@ -110,6 +114,65 @@ void queue_spin_lock_slowpath(struct qsp > > BUILD_BUG_ON(CONFIG_NR_CPUS >= (1U << _Q_TAIL_CPU_BITS)); > > + /* > + * trylock || pending > + * > + * 0,0,0 -> 0,0,1 ; trylock > + * 0,0,1 -> 0,1,1 ; pending > + */ > + for (;;) { > + /* > + * If we observe any contention; queue. > + */ > + if (val & ~_Q_LOCKED_MASK) > + goto queue; > + > + new = _Q_LOCKED_VAL; > + if (val == new) > + new |= _Q_PENDING_VAL; > + > + old = atomic_cmpxchg(&lock->val, val, new); > + if (old == val) > + break; > + > + val = old; > + } > + > + /* > + * we won the trylock > + */ > + if (new == _Q_LOCKED_VAL) > + return; > + > + /* > + * we're pending, wait for the owner to go away. > + * > + * *,1,1 -> *,1,0 > + */ > + while ((val = atomic_read(&lock->val)) & _Q_LOCKED_MASK) > + cpu_relax(); > + > + /* > + * take ownership and clear the pending bit. > + * > + * *,1,0 -> *,0,1 > + */ > + for (;;) { > + new = (val & ~_Q_PENDING_MASK) | _Q_LOCKED_VAL; > + > + old = atomic_cmpxchg(&lock->val, val, new); > + if (old == val) > + break; > + > + val = old; > + } > + return; > + > + /* > + * End of pending bit optimistic spinning and beginning of MCS > + * queuing. > + */ > +queue: > node = this_cpu_ptr(&mcs_nodes[0]); > idx = node->count++; > tail = encode_tail(smp_processor_id(), idx); > @@ -119,15 +182,18 @@ void queue_spin_lock_slowpath(struct qsp > node->next = NULL; > > /* > + * we already touched the queueing cacheline; don't bother with pending > + * stuff. > + * > * trylock || xchg(lock, node) > * > - * 0,0 -> 0,1 ; trylock > - * p,x -> n,x ; prev = xchg(lock, node) > + * 0,0,0 -> 0,0,1 ; trylock > + * p,y,x -> n,y,x ; prev = xchg(lock, node) > */ > for (;;) { > new = _Q_LOCKED_VAL; > if (val) > - new = tail | (val & _Q_LOCKED_MASK); > + new = tail | (val & _Q_LOCKED_PENDING_MASK); > > old = atomic_cmpxchg(&lock->val, val, new); > if (old == val) > @@ -145,7 +211,7 @@ void queue_spin_lock_slowpath(struct qsp > /* > * if there was a previous node; link it and wait. > */ > - if (old & ~_Q_LOCKED_MASK) { > + if (old & ~_Q_LOCKED_PENDING_MASK) { > prev = decode_tail(old); > ACCESS_ONCE(prev->next) = node; > > @@ -153,18 +219,19 @@ void queue_spin_lock_slowpath(struct qsp > } > > /* > - * we're at the head of the waitqueue, wait for the owner to go away. > + * we're at the head of the waitqueue, wait for the owner & pending to > + * go away. > * > - * *,x -> *,0 > + * *,x,y -> *,0,0 > */ > - while ((val = atomic_read(&lock->val)) & _Q_LOCKED_MASK) > + while ((val = atomic_read(&lock->val)) & _Q_LOCKED_PENDING_MASK) > cpu_relax(); > > /* > * claim the lock: > * > - * n,0 -> 0,1 : lock, uncontended > - * *,0 -> *,1 : lock, contended > + * n,0,0 -> 0,0,1 : lock, uncontended > + * *,0,0 -> *,0,1 : lock, contended > */ > for (;;) { > new = _Q_LOCKED_VAL; > > ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH 03/11] qspinlock: Add pending bit 2014-06-17 20:36 ` Konrad Rzeszutek Wilk @ 2014-06-17 20:51 ` Waiman Long 2014-06-17 21:07 ` Konrad Rzeszutek Wilk 2014-06-18 11:29 ` Paolo Bonzini 2014-06-23 16:35 ` Peter Zijlstra 2 siblings, 1 reply; 12+ messages in thread From: Waiman Long @ 2014-06-17 20:51 UTC (permalink / raw) To: Konrad Rzeszutek Wilk Cc: Peter Zijlstra, tglx, mingo, linux-arch, linux-kernel, virtualization, xen-devel, kvm, paolo.bonzini, boris.ostrovsky, paulmck, riel, torvalds, raghavendra.kt, david.vrabel, oleg, gleb, scott.norton, chegu_vinod, Peter Zijlstra On 06/17/2014 04:36 PM, Konrad Rzeszutek Wilk wrote: > On Sun, Jun 15, 2014 at 02:47:00PM +0200, Peter Zijlstra wrote: >> Because the qspinlock needs to touch a second cacheline; add a pending >> bit and allow a single in-word spinner before we punt to the second >> cacheline. > Could you add this in the description please: > > And by second cacheline we mean the local 'node'. That is the: > mcs_nodes[0] and mcs_nodes[idx] > > Perhaps it might be better then to split this in the header file > as this is trying to not be a slowpath code - but rather - a > pre-slow-path-lets-try-if-we can do another cmpxchg in case > the unlocker has just unlocked itself. > > So something like: > > diff --git a/include/asm-generic/qspinlock.h b/include/asm-generic/qspinlock.h > index e8a7ae8..29cc9c7 100644 > --- a/include/asm-generic/qspinlock.h > +++ b/include/asm-generic/qspinlock.h > @@ -75,11 +75,21 @@ extern void queue_spin_lock_slowpath(struct qspinlock *lock, u32 val); > */ > static __always_inline void queue_spin_lock(struct qspinlock *lock) > { > - u32 val; > + u32 val, new; > > val = atomic_cmpxchg(&lock->val, 0, _Q_LOCKED_VAL); > if (likely(val == 0)) > return; > + > + /* One more attempt - but if we fail mark it as pending. */ > + if (val == _Q_LOCKED_VAL) { > + new = Q_LOCKED_VAL |_Q_PENDING_VAL; > + > + old = atomic_cmpxchg(&lock->val, val, new); > + if (old == _Q_LOCKED_VAL) /* YEEY! */ > + return; No, it can leave like that. The unlock path will not clear the pending bit. We are trying to make the fastpath as simple as possible as it may be inlined. The complexity of the queue spinlock is in the slowpath. Moreover, an cmpxchg followed immediately followed by another cmpxchg will just increase the level of memory contention when a lock is fairly contended. The chance of second cmpxchg() succeeding will be pretty low. -Longman ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH 03/11] qspinlock: Add pending bit 2014-06-17 20:51 ` Waiman Long @ 2014-06-17 21:07 ` Konrad Rzeszutek Wilk 2014-06-17 21:10 ` Konrad Rzeszutek Wilk 2014-06-24 8:24 ` Peter Zijlstra 0 siblings, 2 replies; 12+ messages in thread From: Konrad Rzeszutek Wilk @ 2014-06-17 21:07 UTC (permalink / raw) To: Waiman Long Cc: Peter Zijlstra, tglx, mingo, linux-arch, linux-kernel, virtualization, xen-devel, kvm, paolo.bonzini, boris.ostrovsky, paulmck, riel, torvalds, raghavendra.kt, david.vrabel, oleg, gleb, scott.norton, chegu_vinod, Peter Zijlstra On Tue, Jun 17, 2014 at 04:51:57PM -0400, Waiman Long wrote: > On 06/17/2014 04:36 PM, Konrad Rzeszutek Wilk wrote: > >On Sun, Jun 15, 2014 at 02:47:00PM +0200, Peter Zijlstra wrote: > >>Because the qspinlock needs to touch a second cacheline; add a pending > >>bit and allow a single in-word spinner before we punt to the second > >>cacheline. > >Could you add this in the description please: > > > >And by second cacheline we mean the local 'node'. That is the: > >mcs_nodes[0] and mcs_nodes[idx] > > > >Perhaps it might be better then to split this in the header file > >as this is trying to not be a slowpath code - but rather - a > >pre-slow-path-lets-try-if-we can do another cmpxchg in case > >the unlocker has just unlocked itself. > > > >So something like: > > > >diff --git a/include/asm-generic/qspinlock.h b/include/asm-generic/qspinlock.h > >index e8a7ae8..29cc9c7 100644 > >--- a/include/asm-generic/qspinlock.h > >+++ b/include/asm-generic/qspinlock.h > >@@ -75,11 +75,21 @@ extern void queue_spin_lock_slowpath(struct qspinlock *lock, u32 val); > > */ > > static __always_inline void queue_spin_lock(struct qspinlock *lock) > > { > >- u32 val; > >+ u32 val, new; > > > > val = atomic_cmpxchg(&lock->val, 0, _Q_LOCKED_VAL); > > if (likely(val == 0)) > > return; > >+ > >+ /* One more attempt - but if we fail mark it as pending. */ > >+ if (val == _Q_LOCKED_VAL) { > >+ new = Q_LOCKED_VAL |_Q_PENDING_VAL; > >+ > >+ old = atomic_cmpxchg(&lock->val, val, new); > >+ if (old == _Q_LOCKED_VAL) /* YEEY! */ > >+ return; > > No, it can leave like that. The unlock path will not clear the pending bit. Err, you are right. It needs to go back in the slowpath. > We are trying to make the fastpath as simple as possible as it may be > inlined. The complexity of the queue spinlock is in the slowpath. Sure, but then it shouldn't be called slowpath anymore as it is not slow. It is a combination of fast path (the potential chance of grabbing the lock and setting the pending lock) and the real slow path (the queuing). Perhaps it should be called 'queue_spinlock_complex' ? > > Moreover, an cmpxchg followed immediately followed by another cmpxchg will > just increase the level of memory contention when a lock is fairly > contended. The chance of second cmpxchg() succeeding will be pretty low. Then why even do the pending bit - which is what the slowpath does for the first time. And if it grabs it (And sets the pending bit) it immediately exits. Why not perculate that piece of code in-to this header. And the leave all that slow code (queing, mcs_lock access, etc) in the slowpath. > > -Longman > > ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH 03/11] qspinlock: Add pending bit 2014-06-17 21:07 ` Konrad Rzeszutek Wilk @ 2014-06-17 21:10 ` Konrad Rzeszutek Wilk 2014-06-17 22:25 ` Waiman Long 2014-06-24 8:24 ` Peter Zijlstra 1 sibling, 1 reply; 12+ messages in thread From: Konrad Rzeszutek Wilk @ 2014-06-17 21:10 UTC (permalink / raw) To: Waiman Long Cc: Peter Zijlstra, tglx, mingo, linux-arch, linux-kernel, virtualization, xen-devel, kvm, paolo.bonzini, boris.ostrovsky, paulmck, riel, torvalds, raghavendra.kt, david.vrabel, oleg, gleb, scott.norton, chegu_vinod, Peter Zijlstra On Tue, Jun 17, 2014 at 05:07:29PM -0400, Konrad Rzeszutek Wilk wrote: > On Tue, Jun 17, 2014 at 04:51:57PM -0400, Waiman Long wrote: > > On 06/17/2014 04:36 PM, Konrad Rzeszutek Wilk wrote: > > >On Sun, Jun 15, 2014 at 02:47:00PM +0200, Peter Zijlstra wrote: > > >>Because the qspinlock needs to touch a second cacheline; add a pending > > >>bit and allow a single in-word spinner before we punt to the second > > >>cacheline. > > >Could you add this in the description please: > > > > > >And by second cacheline we mean the local 'node'. That is the: > > >mcs_nodes[0] and mcs_nodes[idx] > > > > > >Perhaps it might be better then to split this in the header file > > >as this is trying to not be a slowpath code - but rather - a > > >pre-slow-path-lets-try-if-we can do another cmpxchg in case > > >the unlocker has just unlocked itself. > > > > > >So something like: > > > > > >diff --git a/include/asm-generic/qspinlock.h b/include/asm-generic/qspinlock.h > > >index e8a7ae8..29cc9c7 100644 > > >--- a/include/asm-generic/qspinlock.h > > >+++ b/include/asm-generic/qspinlock.h > > >@@ -75,11 +75,21 @@ extern void queue_spin_lock_slowpath(struct qspinlock *lock, u32 val); > > > */ > > > static __always_inline void queue_spin_lock(struct qspinlock *lock) > > > { > > >- u32 val; > > >+ u32 val, new; > > > > > > val = atomic_cmpxchg(&lock->val, 0, _Q_LOCKED_VAL); > > > if (likely(val == 0)) > > > return; > > >+ > > >+ /* One more attempt - but if we fail mark it as pending. */ > > >+ if (val == _Q_LOCKED_VAL) { > > >+ new = Q_LOCKED_VAL |_Q_PENDING_VAL; > > >+ > > >+ old = atomic_cmpxchg(&lock->val, val, new); > > >+ if (old == _Q_LOCKED_VAL) /* YEEY! */ > > >+ return; > > > > No, it can leave like that. The unlock path will not clear the pending bit. > > Err, you are right. It needs to go back in the slowpath. What I should have wrote is: if (old == 0) /* YEEY */ return; As that would the same thing as this patch does on the pending bit - that is if we can on the second compare and exchange set the pending bit (and the lock) and the lock has been released - we are good. And it is a quick path. > > > We are trying to make the fastpath as simple as possible as it may be > > inlined. The complexity of the queue spinlock is in the slowpath. > > Sure, but then it shouldn't be called slowpath anymore as it is not > slow. It is a combination of fast path (the potential chance of > grabbing the lock and setting the pending lock) and the real slow > path (the queuing). Perhaps it should be called 'queue_spinlock_complex' ? > I forgot to mention - that was the crux of my comments - just change the slowpath to complex name at that point to better reflect what it does. > > > > Moreover, an cmpxchg followed immediately followed by another cmpxchg will > > just increase the level of memory contention when a lock is fairly > > contended. The chance of second cmpxchg() succeeding will be pretty low. > > Then why even do the pending bit - which is what the slowpath does > for the first time. And if it grabs it (And sets the pending bit) it > immediately exits. Why not perculate that piece of code in-to this header. > > And the leave all that slow code (queing, mcs_lock access, etc) in the slowpath. > > > > > -Longman > > > > ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH 03/11] qspinlock: Add pending bit 2014-06-17 21:10 ` Konrad Rzeszutek Wilk @ 2014-06-17 22:25 ` Waiman Long 0 siblings, 0 replies; 12+ messages in thread From: Waiman Long @ 2014-06-17 22:25 UTC (permalink / raw) To: Konrad Rzeszutek Wilk Cc: Peter Zijlstra, tglx, mingo, linux-arch, linux-kernel, virtualization, xen-devel, kvm, paolo.bonzini, boris.ostrovsky, paulmck, riel, torvalds, raghavendra.kt, david.vrabel, oleg, gleb, scott.norton, chegu_vinod, Peter Zijlstra On 06/17/2014 05:10 PM, Konrad Rzeszutek Wilk wrote: > On Tue, Jun 17, 2014 at 05:07:29PM -0400, Konrad Rzeszutek Wilk wrote: >> On Tue, Jun 17, 2014 at 04:51:57PM -0400, Waiman Long wrote: >>> On 06/17/2014 04:36 PM, Konrad Rzeszutek Wilk wrote: >>>> On Sun, Jun 15, 2014 at 02:47:00PM +0200, Peter Zijlstra wrote: >>>>> Because the qspinlock needs to touch a second cacheline; add a pending >>>>> bit and allow a single in-word spinner before we punt to the second >>>>> cacheline. >>>> Could you add this in the description please: >>>> >>>> And by second cacheline we mean the local 'node'. That is the: >>>> mcs_nodes[0] and mcs_nodes[idx] >>>> >>>> Perhaps it might be better then to split this in the header file >>>> as this is trying to not be a slowpath code - but rather - a >>>> pre-slow-path-lets-try-if-we can do another cmpxchg in case >>>> the unlocker has just unlocked itself. >>>> >>>> So something like: >>>> >>>> diff --git a/include/asm-generic/qspinlock.h b/include/asm-generic/qspinlock.h >>>> index e8a7ae8..29cc9c7 100644 >>>> --- a/include/asm-generic/qspinlock.h >>>> +++ b/include/asm-generic/qspinlock.h >>>> @@ -75,11 +75,21 @@ extern void queue_spin_lock_slowpath(struct qspinlock *lock, u32 val); >>>> */ >>>> static __always_inline void queue_spin_lock(struct qspinlock *lock) >>>> { >>>> - u32 val; >>>> + u32 val, new; >>>> >>>> val = atomic_cmpxchg(&lock->val, 0, _Q_LOCKED_VAL); >>>> if (likely(val == 0)) >>>> return; >>>> + >>>> + /* One more attempt - but if we fail mark it as pending. */ >>>> + if (val == _Q_LOCKED_VAL) { >>>> + new = Q_LOCKED_VAL |_Q_PENDING_VAL; >>>> + >>>> + old = atomic_cmpxchg(&lock->val, val, new); >>>> + if (old == _Q_LOCKED_VAL) /* YEEY! */ >>>> + return; >>> No, it can leave like that. The unlock path will not clear the pending bit. >> Err, you are right. It needs to go back in the slowpath. > What I should have wrote is: > > if (old == 0) /* YEEY */ > return; Unfortunately, that still doesn't work. If old is 0, it just meant the cmpxchg failed. It still haven't got the lock. > As that would the same thing as this patch does on the pending bit - that > is if we can on the second compare and exchange set the pending bit (and the > lock) and the lock has been released - we are good. That is not true. When the lock is freed, the pending bit holder will still have to clear the pending bit and set the lock bit as is done in the slowpath. We cannot skip the step here. The problem of moving the pending code here is that it includes a wait loop which we don't want to put in the fastpath. > > And it is a quick path. > >>> We are trying to make the fastpath as simple as possible as it may be >>> inlined. The complexity of the queue spinlock is in the slowpath. >> Sure, but then it shouldn't be called slowpath anymore as it is not >> slow. It is a combination of fast path (the potential chance of >> grabbing the lock and setting the pending lock) and the real slow >> path (the queuing). Perhaps it should be called 'queue_spinlock_complex' ? >> > I forgot to mention - that was the crux of my comments - just change > the slowpath to complex name at that point to better reflect what > it does. Actually in my v11 patch, I subdivided the slowpath into a slowpath for the pending code and slowerpath for actual queuing. Perhaps, we could use quickpath and slowpath instead. Anyway, it is a minor detail that we can discuss after the core code get merged. -Longman ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH 03/11] qspinlock: Add pending bit 2014-06-17 21:07 ` Konrad Rzeszutek Wilk 2014-06-17 21:10 ` Konrad Rzeszutek Wilk @ 2014-06-24 8:24 ` Peter Zijlstra 1 sibling, 0 replies; 12+ messages in thread From: Peter Zijlstra @ 2014-06-24 8:24 UTC (permalink / raw) To: Konrad Rzeszutek Wilk Cc: Waiman Long, tglx, mingo, linux-arch, linux-kernel, virtualization, xen-devel, kvm, paolo.bonzini, boris.ostrovsky, paulmck, riel, torvalds, raghavendra.kt, david.vrabel, oleg, gleb, scott.norton, chegu_vinod On Tue, Jun 17, 2014 at 05:07:29PM -0400, Konrad Rzeszutek Wilk wrote: > > We are trying to make the fastpath as simple as possible as it may be > > inlined. The complexity of the queue spinlock is in the slowpath. > > Sure, but then it shouldn't be called slowpath anymore as it is not > slow. Its common terminology to call the inline part the fast path and the out-of-line call on failure the slow path. ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH 03/11] qspinlock: Add pending bit 2014-06-17 20:36 ` Konrad Rzeszutek Wilk 2014-06-17 20:51 ` Waiman Long @ 2014-06-18 11:29 ` Paolo Bonzini 2014-06-18 13:36 ` Konrad Rzeszutek Wilk 2014-06-23 16:35 ` Peter Zijlstra 2 siblings, 1 reply; 12+ messages in thread From: Paolo Bonzini @ 2014-06-18 11:29 UTC (permalink / raw) To: Konrad Rzeszutek Wilk, Peter Zijlstra Cc: Waiman.Long, tglx, mingo, linux-arch, linux-kernel, virtualization, xen-devel, kvm, paolo.bonzini, boris.ostrovsky, paulmck, riel, torvalds, raghavendra.kt, david.vrabel, oleg, gleb, scott.norton, chegu_vinod, Peter Zijlstra Il 17/06/2014 22:36, Konrad Rzeszutek Wilk ha scritto: > + /* One more attempt - but if we fail mark it as pending. */ > + if (val == _Q_LOCKED_VAL) { > + new = Q_LOCKED_VAL |_Q_PENDING_VAL; > + > + old = atomic_cmpxchg(&lock->val, val, new); > + if (old == _Q_LOCKED_VAL) /* YEEY! */ > + return; > + val = old; > + } Note that Peter's code is in a for(;;) loop: + for (;;) { + /* + * If we observe any contention; queue. + */ + if (val & ~_Q_LOCKED_MASK) + goto queue; + + new = _Q_LOCKED_VAL; + if (val == new) + new |= _Q_PENDING_VAL; + + old = atomic_cmpxchg(&lock->val, val, new); + if (old == val) + break; + + val = old; + } + + /* + * we won the trylock + */ + if (new == _Q_LOCKED_VAL) + return; So what you'd have is basically: /* * One more attempt if no one is already in queue. Perhaps * they have unlocked the spinlock already. */ if (val == _Q_LOCKED_VAL && atomic_read(&lock->val) == 0) { old = atomic_cmpxchg(&lock->val, 0, _Q_LOCKED_VAL); if (old == 0) /* YEEY! */ return; val = old; } But I agree with Waiman that this is unlikely to trigger often enough. It does have to be handled in the slowpath for correctness, but the most likely path is (0,0,1)->(0,1,1). Paolo ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH 03/11] qspinlock: Add pending bit 2014-06-18 11:29 ` Paolo Bonzini @ 2014-06-18 13:36 ` Konrad Rzeszutek Wilk 0 siblings, 0 replies; 12+ messages in thread From: Konrad Rzeszutek Wilk @ 2014-06-18 13:36 UTC (permalink / raw) To: Paolo Bonzini Cc: Peter Zijlstra, Waiman.Long, tglx, mingo, linux-arch, linux-kernel, virtualization, xen-devel, kvm, paolo.bonzini, boris.ostrovsky, paulmck, riel, torvalds, raghavendra.kt, david.vrabel, oleg, gleb, scott.norton, chegu_vinod, Peter Zijlstra On Wed, Jun 18, 2014 at 01:29:48PM +0200, Paolo Bonzini wrote: > Il 17/06/2014 22:36, Konrad Rzeszutek Wilk ha scritto: > >+ /* One more attempt - but if we fail mark it as pending. */ > >+ if (val == _Q_LOCKED_VAL) { > >+ new = Q_LOCKED_VAL |_Q_PENDING_VAL; > >+ > >+ old = atomic_cmpxchg(&lock->val, val, new); > >+ if (old == _Q_LOCKED_VAL) /* YEEY! */ > >+ return; > >+ val = old; > >+ } > > Note that Peter's code is in a for(;;) loop: > > > + for (;;) { > + /* > + * If we observe any contention; queue. > + */ > + if (val & ~_Q_LOCKED_MASK) > + goto queue; > + > + new = _Q_LOCKED_VAL; > + if (val == new) > + new |= _Q_PENDING_VAL; > + > + old = atomic_cmpxchg(&lock->val, val, new); > + if (old == val) > + break; > + > + val = old; > + } > + > + /* > + * we won the trylock > + */ > + if (new == _Q_LOCKED_VAL) > + return; > > So what you'd have is basically: > > /* > * One more attempt if no one is already in queue. Perhaps > * they have unlocked the spinlock already. > */ > if (val == _Q_LOCKED_VAL && atomic_read(&lock->val) == 0) { > old = atomic_cmpxchg(&lock->val, 0, _Q_LOCKED_VAL); > if (old == 0) /* YEEY! */ > return; > val = old; > } > > But I agree with Waiman that this is unlikely to trigger often enough. It > does have to be handled in the slowpath for correctness, but the most likely > path is (0,0,1)->(0,1,1). <nods> > > Paolo ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH 03/11] qspinlock: Add pending bit 2014-06-17 20:36 ` Konrad Rzeszutek Wilk 2014-06-17 20:51 ` Waiman Long 2014-06-18 11:29 ` Paolo Bonzini @ 2014-06-23 16:35 ` Peter Zijlstra 2 siblings, 0 replies; 12+ messages in thread From: Peter Zijlstra @ 2014-06-23 16:35 UTC (permalink / raw) To: Konrad Rzeszutek Wilk Cc: Waiman.Long, tglx, mingo, linux-arch, linux-kernel, virtualization, xen-devel, kvm, paolo.bonzini, boris.ostrovsky, paulmck, riel, torvalds, raghavendra.kt, david.vrabel, oleg, gleb, scott.norton, chegu_vinod On Tue, Jun 17, 2014 at 04:36:15PM -0400, Konrad Rzeszutek Wilk wrote: > On Sun, Jun 15, 2014 at 02:47:00PM +0200, Peter Zijlstra wrote: > > Because the qspinlock needs to touch a second cacheline; add a pending > > bit and allow a single in-word spinner before we punt to the second > > cacheline. > > Could you add this in the description please: > > And by second cacheline we mean the local 'node'. That is the: > mcs_nodes[0] and mcs_nodes[idx] Those should be the very same cacheline :), but yes, I can add something like that. > Perhaps it might be better then to split this in the header file > as this is trying to not be a slowpath code - but rather - a > pre-slow-path-lets-try-if-we can do another cmpxchg in case > the unlocker has just unlocked itself. > > So something like: > > diff --git a/include/asm-generic/qspinlock.h b/include/asm-generic/qspinlock.h > index e8a7ae8..29cc9c7 100644 > --- a/include/asm-generic/qspinlock.h > +++ b/include/asm-generic/qspinlock.h > @@ -75,11 +75,21 @@ extern void queue_spin_lock_slowpath(struct qspinlock *lock, u32 val); > */ > static __always_inline void queue_spin_lock(struct qspinlock *lock) > { > - u32 val; > + u32 val, new; > > val = atomic_cmpxchg(&lock->val, 0, _Q_LOCKED_VAL); > if (likely(val == 0)) > return; > + > + /* One more attempt - but if we fail mark it as pending. */ > + if (val == _Q_LOCKED_VAL) { > + new = Q_LOCKED_VAL |_Q_PENDING_VAL; > + > + old = atomic_cmpxchg(&lock->val, val, new); > + if (old == _Q_LOCKED_VAL) /* YEEY! */ > + return; > + val = old; > + } > queue_spin_lock_slowpath(lock, val); > } I think that's too big for an inline function. ^ permalink raw reply [flat|nested] 12+ messages in thread
end of thread, other threads:[~2014-06-24 10:14 UTC | newest] Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2014-06-17 23:23 [PATCH 03/11] qspinlock: Add pending bit Konrad Rzeszutek Wilk 2014-06-24 8:46 ` Peter Zijlstra -- strict thread matches above, loose matches on Subject: below -- 2014-06-15 12:46 [PATCH 00/11] qspinlock with paravirt support Peter Zijlstra 2014-06-15 12:47 ` [PATCH 03/11] qspinlock: Add pending bit Peter Zijlstra 2014-06-17 20:36 ` Konrad Rzeszutek Wilk 2014-06-17 20:51 ` Waiman Long 2014-06-17 21:07 ` Konrad Rzeszutek Wilk 2014-06-17 21:10 ` Konrad Rzeszutek Wilk 2014-06-17 22:25 ` Waiman Long 2014-06-24 8:24 ` Peter Zijlstra 2014-06-18 11:29 ` Paolo Bonzini 2014-06-18 13:36 ` Konrad Rzeszutek Wilk 2014-06-23 16:35 ` Peter Zijlstra
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®