Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1brykk-000715-Sz for pgsql-hackers@arkaria.postgresql.org; Thu, 06 Oct 2016 02:49:46 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1brykk-0008JT-FS for pgsql-hackers@arkaria.postgresql.org; Thu, 06 Oct 2016 02:49:46 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1bryjM-0006TT-MU for pgsql-hackers@postgresql.org; Thu, 06 Oct 2016 02:48:20 +0000 Received: from out1-smtp.messagingengine.com ([66.111.4.25]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1bryjG-0001zl-Dt for pgsql-hackers@postgresql.org; Thu, 06 Oct 2016 02:48:19 +0000 Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.nyi.internal (Postfix) with ESMTP id 283B5204DB; Wed, 5 Oct 2016 22:48:13 -0400 (EDT) Received: from frontend1 ([10.202.2.160]) by compute3.internal (MEProxy); Wed, 05 Oct 2016 22:48:13 -0400 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-sasl-enc :x-sasl-enc; s=smtpout; bh=JRB9pu6cprYFA4kHFHcibjpPf+M=; b=isG+x rhOuIcf1JatVU2M7ydSaxvF04bk/2fSz5GKK/DLUxKeUFPOt8agsexpVbfBDumhV 997Ekaad4CvwTK+o/TaA7CGw0syejdu62SYYY/Bx9McV+rAToOE6dm8AogRDvkRa L8ZqZHPW/oVNF0ovV5gcDrsSw0TNNrkPMan7OU= X-Sasl-enc: So+Tb41QMWj8QfpkEsd6myS7vCrTE6dTMLdmZso3l79N 1475722092 Received: from intern.anarazel.de (somacentral1.static.monkeybrains.net [162.217.75.74]) by mail.messagingengine.com (Postfix) with ESMTPA id D3CAFF2985; Wed, 5 Oct 2016 22:48:12 -0400 (EDT) Date: Wed, 5 Oct 2016 19:48:11 -0700 From: Andres Freund To: Tom Lane Cc: pgsql-hackers@postgresql.org Subject: Re: Our "fallback" atomics implementation doesn't actually work Message-ID: <20161006024811.fcll3nnfmdn4qtpy@alap3.anarazel.de> References: <14947.1475690465@sss.pgh.pa.us> <20161005183402.6veikn2ajr52vosy@alap3.anarazel.de> <24585.1475694129@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="w7qiupalltjqpunp" Content-Disposition: inline In-Reply-To: <24585.1475694129@sss.pgh.pa.us> X-Pg-Spam-Score: -2.6 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org --w7qiupalltjqpunp Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On 2016-10-05 15:02:09 -0400, Tom Lane wrote: > Andres Freund 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 --w7qiupalltjqpunp Content-Type: text/x-patch; charset=us-ascii Content-Disposition: attachment; filename="atomic-write-fallback.patch" 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); --w7qiupalltjqpunp Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers --w7qiupalltjqpunp--