Prevent potentially hazardous compiler/cpu reordering during lwlock release.
authorAndres Freund <andres@anarazel.de>
Fri, 19 Dec 2014 13:29:52 +0000 (14:29 +0100)
committerAndres Freund <andres@anarazel.de>
Fri, 19 Dec 2014 13:36:23 +0000 (14:36 +0100)
commit0e68570e8b4419b0484a0f96ee30ab34561c3a91
tree7e05c178c37be23541f796b1a68a32def0a3fd62
parentef8472bc7adb2740010ad4fefdae63151bc66f39
Prevent potentially hazardous compiler/cpu reordering during lwlock release.

In LWLockRelease() (and in 9.4+ LWLockUpdateVar()) we release enqueued
waiters using PGSemaphoreUnlock(). As there are other sources of such
unlocks backends only wake up if MyProc->lwWaiting is set to false;
which is only done in the aforementioned functions.

Before this commit there were dangers because the store to lwWaitLink
could become visible before the store to lwWaitLink. This could both
happen due to compiler reordering (on most compilers) and on some
platforms due to the CPU reordering stores.

The possible consequence of this is that a backend stops waiting
before lwWaitLink is set to NULL. If that backend then tries to
acquire another lock and has to wait there the list could become
corrupted once the lwWaitLink store is finally performed.

Add a write memory barrier to prevent that issue.

Unfortunately the barrier support has been only added in 9.2. Given
that the issue has not knowingly been observed in praxis it seems
sufficient to prohibit compiler reordering using volatile for 9.0 and
9.1. Actual problems due to compiler reordering are more likely
anyway.

Discussion: 20140210134625.GA15246@awork2.anarazel.de
src/backend/storage/lmgr/lwlock.c