Skip to content

Commit 756b1df

Browse files
Peter ZijlstraIngo Molnar
authored andcommitted
locking/qspinlock: Rework some comments
While working my way through the code again; I felt the comments could use help. Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Acked-by: Will Deacon <will.deacon@arm.com> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: andrea.parri@amarulasolutions.com Cc: longman@redhat.com Link: https://lkml.kernel.org/r/20181003130257.156322446@infradead.org Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent 53bf57f commit 756b1df

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

kernel/locking/qspinlock.c

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -326,16 +326,23 @@ void queued_spin_lock_slowpath(struct qspinlock *lock, u32 val)
326326
/*
327327
* trylock || pending
328328
*
329-
* 0,0,0 -> 0,0,1 ; trylock
330-
* 0,0,1 -> 0,1,1 ; pending
329+
* 0,0,* -> 0,1,* -> 0,0,1 pending, trylock
331330
*/
332331
val = atomic_fetch_or_acquire(_Q_PENDING_VAL, &lock->val);
332+
333333
/*
334-
* If we observe any contention; undo and queue.
334+
* If we observe contention, there is a concurrent locker.
335+
*
336+
* Undo and queue; our setting of PENDING might have made the
337+
* n,0,0 -> 0,0,0 transition fail and it will now be waiting
338+
* on @next to become !NULL.
335339
*/
336340
if (unlikely(val & ~_Q_LOCKED_MASK)) {
341+
342+
/* Undo PENDING if we set it. */
337343
if (!(val & _Q_PENDING_MASK))
338344
clear_pending(lock);
345+
339346
goto queue;
340347
}
341348

@@ -474,16 +481,25 @@ void queued_spin_lock_slowpath(struct qspinlock *lock, u32 val)
474481
*/
475482

476483
/*
477-
* In the PV case we might already have _Q_LOCKED_VAL set.
484+
* In the PV case we might already have _Q_LOCKED_VAL set, because
485+
* of lock stealing; therefore we must also allow:
478486
*
479-
* The atomic_cond_read_acquire() call above has provided the
480-
* necessary acquire semantics required for locking.
487+
* n,0,1 -> 0,0,1
488+
*
489+
* Note: at this point: (val & _Q_PENDING_MASK) == 0, because of the
490+
* above wait condition, therefore any concurrent setting of
491+
* PENDING will make the uncontended transition fail.
481492
*/
482-
if (((val & _Q_TAIL_MASK) == tail) &&
483-
atomic_try_cmpxchg_relaxed(&lock->val, &val, _Q_LOCKED_VAL))
484-
goto release; /* No contention */
493+
if ((val & _Q_TAIL_MASK) == tail) {
494+
if (atomic_try_cmpxchg_relaxed(&lock->val, &val, _Q_LOCKED_VAL))
495+
goto release; /* No contention */
496+
}
485497

486-
/* Either somebody is queued behind us or _Q_PENDING_VAL is set */
498+
/*
499+
* Either somebody is queued behind us or _Q_PENDING_VAL got set
500+
* which will then detect the remaining tail and queue behind us
501+
* ensuring we'll see a @next.
502+
*/
487503
set_locked(lock);
488504

489505
/*

0 commit comments

Comments
 (0)