aboutsummaryrefslogtreecommitdiff
path: root/src/backend
diff options
context:
space:
mode:
authorAndres Freund <andres@anarazel.de>2014-12-19 14:29:52 +0100
committerAndres Freund <andres@anarazel.de>2014-12-19 14:55:54 +0100
commit86b9424e8620093cd5568476eb98a9fe5134ca62 (patch)
tree388b901a2cd569b5c051d2b7cecc4aeddf32add9 /src/backend
parent789c5b0179b1c615f95689708c6edf9a68c51546 (diff)
downloadpostgresql-86b9424e8620093cd5568476eb98a9fe5134ca62.tar.gz
postgresql-86b9424e8620093cd5568476eb98a9fe5134ca62.zip
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
Diffstat (limited to 'src/backend')
-rw-r--r--src/backend/storage/lmgr/lwlock.c20
1 files changed, 15 insertions, 5 deletions
diff --git a/src/backend/storage/lmgr/lwlock.c b/src/backend/storage/lmgr/lwlock.c
index f482f34bad3..61eadeff6b4 100644
--- a/src/backend/storage/lmgr/lwlock.c
+++ b/src/backend/storage/lmgr/lwlock.c
@@ -647,12 +647,22 @@ LWLockRelease(LWLockId lockid)
*/
while (head != NULL)
{
+ /*
+ * Try to guarantee that lwWaiting being unset only becomes visible
+ * once the unlink from the link has completed. Otherwise the target
+ * backend could be woken up for other reason and enqueue for a new
+ * lock - if that happens before the list unlink happens, the list
+ * would end up being corrupted. In later releases we can rely on
+ * barriers, but < 9.2 doesn't yet have them - so just use volatile.
+ */
+ volatile PGPROC *p;
+
LOG_LWDEBUG("LWLockRelease", lockid, "release waiter");
- proc = head;
- head = proc->lwWaitLink;
- proc->lwWaitLink = NULL;
- proc->lwWaiting = false;
- PGSemaphoreUnlock(&proc->sem);
+ p = head;
+ head = p->lwWaitLink;
+ p->lwWaitLink = NULL;
+ p->lwWaiting = false;
+ PGSemaphoreUnlock((PGSemaphore) &p->sem);
}
/*