Skip to content

Commit

Permalink
mcslock: fix hang in weak memory model
Browse files Browse the repository at this point in the history
[ upstream commit 021b698 ]

The initialization me->locked=1 in lock() must happen before
next->locked=0 in unlock(), otherwise a thread may hang forever,
waiting me->locked become 0. On weak memory systems (such as ARMv8),
the current implementation allows me->locked=1 to be reordered with
announcing the node (pred->next=me) and, consequently, to be
reordered with next->locked=0 in unlock().

This fix adds a release barrier to pred->next=me, forcing
me->locked=1 to happen before this operation.

Fixes: 2173f33 ("mcslock: add MCS queued lock implementation")

Signed-off-by: Diogo Behrens <diogo.behrens@huawei.com>
Acked-by: Honnappa Nagarahalli <honnappa.nagarahalli@arm.com>
  • Loading branch information
db7 authored and bluca committed Nov 30, 2020
1 parent 1b51851 commit 22023fd
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/librte_eal/common/include/generic/rte_mcslock.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,14 @@ rte_mcslock_lock(rte_mcslock_t **msl, rte_mcslock_t *me)
*/
return;
}
__atomic_store_n(&prev->next, me, __ATOMIC_RELAXED);
/* The store to me->next above should also complete before the node is
* visible to predecessor thread releasing the lock. Hence, the store
* prev->next also requires release semantics. Note that, for example,
* on ARM, the release semantics in the exchange operation is not
* strong as a release fence and is not sufficient to enforce the
* desired order here.
*/
__atomic_store_n(&prev->next, me, __ATOMIC_RELEASE);

/* The while-load of me->locked should not move above the previous
* store to prev->next. Otherwise it will cause a deadlock. Need a
Expand Down

0 comments on commit 22023fd

Please sign in to comment.