pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Andres Freund <andres@anarazel.de>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: pgsql-hackers@postgresql.org
Subject: Re: Our "fallback" atomics implementation doesn't actually work
Date: Wed, 5 Oct 2016 19:48:11 -0700
Message-ID: <20161006024811.fcll3nnfmdn4qtpy@alap3.anarazel.de> (raw)
In-Reply-To: <24585.1475694129@sss.pgh.pa.us>
References: <14947.1475690465@sss.pgh.pa.us>
	<20161005183402.6veikn2ajr52vosy@alap3.anarazel.de>
	<24585.1475694129@sss.pgh.pa.us>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-hackers>

On 2016-10-05 15:02:09 -0400, Tom Lane wrote:
> Andres Freund <andres@anarazel.de> writes:
> > Without yet having analyzed this deeply, could it actually be that the
> > reason is that sem_post/wait aren't proper memory barriers?  On a glance
> > the symptoms look like values have been modified without proper locks...
>
> Hmm, possible ...

Hm. After a long battle of head vs. wall I think I see what the problem
is.  For the fallback atomics implementation I somehow had assumed that
pg_atomic_write_u32() doesn't need to lock, as it's just an unlocked
write.  But that's not true, because it has to cause
pg_atomic_compare_exchange_u32 to fail.  The lack of this can cause a
"leftover" lockbit, when UnlockBufHdr() occurs concurrently an operation
using compare_exchange.

For me the problem often takes a lot longer to reproduce (once only
after 40min), could you run with the attached patch, and see whether
that fixes things for you?

Andres

-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Attachments:

atomic-write-fallback.patch (text/x-patch, 1.4K) download diff --git a/src/backend/port/atomics.c b/src/backend/port/atomics.c index 42169a3..aa649b7 100644 --- a/src/backend/port/atomics.c +++ b/src/backend/port/atomics.c @@ -104,6 +104,19 @@ pg_atomic_init_u32_impl(volatile pg_atomic_uint32 *ptr, uint32 val_) ptr->value = val_; } +void +pg_atomic_write_u32_impl(volatile pg_atomic_uint32 *ptr, uint32 val) +{ + /* + * One might think that an unlocked write doesn't need a lock, but one + * would be wrong. Even an unlocked write has to cause + * pg_atomic_compare_exchange_u32() (et al) to fail. + */ + SpinLockAcquire((slock_t *) &ptr->sema); + ptr->value = val; + SpinLockRelease((slock_t *) &ptr->sema); +} + bool pg_atomic_compare_exchange_u32_impl(volatile pg_atomic_uint32 *ptr, uint32 *expected, uint32 newval) diff --git a/src/include/port/atomics/fallback.h b/src/include/port/atomics/fallback.h index bdaa795..2290fff 100644 --- a/src/include/port/atomics/fallback.h +++ b/src/include/port/atomics/fallback.h @@ -133,6 +133,9 @@ pg_atomic_unlocked_test_flag_impl(volatile pg_atomic_flag *ptr) #define PG_HAVE_ATOMIC_INIT_U32 extern void pg_atomic_init_u32_impl(volatile pg_atomic_uint32 *ptr, uint32 val_); +#define PG_HAVE_ATOMIC_WRITE_U32 +extern void pg_atomic_write_u32_impl(volatile pg_atomic_uint32 *ptr, uint32 val); + #define PG_HAVE_ATOMIC_COMPARE_EXCHANGE_U32 extern bool pg_atomic_compare_exchange_u32_impl(volatile pg_atomic_uint32 *ptr, uint32 *expected, uint32 newval);

view thread (12+ messages)  latest in thread

Message-ID: <20161006024811.fcll3nnfmdn4qtpy@alap3.anarazel.de>
Permalink:  ../20161006024811.fcll3nnfmdn4qtpy@alap3.anarazel.de/
Also on:    postgresql.org/message-id/20161006024811.fcll3nnfmdn4qtpy@alap3.anarazel.de

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-hackers@postgresql.org
  Cc: andres@anarazel.de, tgl@sss.pgh.pa.us
  Subject: Re: Our "fallback" atomics implementation doesn't actually work
  In-Reply-To: <20161006024811.fcll3nnfmdn4qtpy@alap3.anarazel.de>
  References: <14947.1475690465@sss.pgh.pa.us> <20161005183402.6veikn2ajr52vosy@alap3.anarazel.de> <24585.1475694129@sss.pgh.pa.us> <20161006024811.fcll3nnfmdn4qtpy@alap3.anarazel.de>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox