vlib: Handle race in thread barrier processing 76/23176/3
authorAlexander Kabaev <kan@freebsd.org>
Fri, 1 Nov 2019 22:26:20 +0000 (18:26 -0400)
committerAlexander Kabaev <kan@freebsd.org>
Sat, 2 Nov 2019 01:57:49 +0000 (21:57 -0400)
commitfeda545105106d673fdca61028331c86eeb1f408
tree74cb9790744d3f73ac6145910543196bf7ee5c9c
parent5bec5f7860dafcef6aefd50b74de15d08910c6f4
vlib: Handle race in thread barrier processing

When CLIB_DEBUG is enabled, vlib_foreach_main macro asserts that
vlib_main it currently looks at is safely parked in barrier, by
checkling that vlib_main->parked_at_barrier is not 0.
Unfortunately, the check is racy - workers first increment the
atomic counter to indicate that they have reached the barrier
and _then_ set this_main->parked_at_barrier to 1. For the last
worker to suspend this opens the race - main thread is free
to execute and assert immediately after atomic counter has been
incremented, before worker gets to write to own parked_at_barrier.

Fix this by simply swapping the order of two operations.

Type: fix

Signed-off-by: Alexnader Kabaev <kan@FreeBSD.org>
Change-Id: Iae47abd6ca0be1c5413f5ecaefabc64cd7eac2ed
src/vlib/threads.h