Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tqaju-007P8D-TZ for pgsql-hackers@arkaria.postgresql.org; Fri, 07 Mar 2025 16:39:59 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1tqajt-00BNJ5-Lk for pgsql-hackers@arkaria.postgresql.org; Fri, 07 Mar 2025 16:39:57 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tqajt-00BNIv-Bp for pgsql-hackers@lists.postgresql.org; Fri, 07 Mar 2025 16:39:57 +0000 Received: from mail-ej1-x62a.google.com ([2a00:1450:4864:20::62a]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1tqajp-001X14-2D for pgsql-hackers@postgresql.org; Fri, 07 Mar 2025 16:39:57 +0000 Received: by mail-ej1-x62a.google.com with SMTP id a640c23a62f3a-abf5f4e82caso402095566b.1 for ; Fri, 07 Mar 2025 08:39:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1741365594; x=1741970394; darn=postgresql.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=Alk+zq1HTzJJzxNc/QVnbK1yFKChDqmp/KjKrT1AKb0=; b=eGTikyNgFfDjKsQpL7L1FTk41Ef7L/yb9GRrVAvRP2gyMkbMExxAM3GUHSkpEko9iF H7MtgDFCrZvN0d/B6M3NILuSgokxZ0JnqIGNEajYLCVME6YVsdi+47IICT244RuDbbl4 +QU0Ey/zdL0ta2MZlxuNLSyOEZ6SrvmHxT+jLNqzC/3AWoqcDOZwEFzbirHnasoS7hVd WHG+Q6lQlIx+kxs4I0o3EYUxpzEMObNLHwfazytX3SPYj8A5RmDGNdaSpvoID2fpoB1F wtNlUXCKD1OQ0BoP8pd4kxzV37eSkEReaVOAh3V1lPN4mko6QglKIhH2XHwIiCn9ttwq A31Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741365594; x=1741970394; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Alk+zq1HTzJJzxNc/QVnbK1yFKChDqmp/KjKrT1AKb0=; b=wAwbqJjmr6JjZlqgXIvYNc/9Gwz8FlkbuTLj0EJ/g1Fxj8B0OCnAHjGlIrekTyXRIY aVPszqQaGbkqZoDpeUtwJCBrX0irdwZKVFmGemgVY/sqY3WwBilUK5kDqncE+CIQ3xQ3 543XDHV2ftUb+6qReq/Nfdlcu1Q1sQLd/aqJhfWbAFHv4sy1XpygjA94A5/smRRdCjzc 85NEMArpSSHJ/S8QFUU4ENAZgr9kVWhO1aN3CDgXWmaRgf4tfSvO8qQ6JeDhBvPUSt1L zHgLFJ1clKqQDNNUq4agWvE/IsSdkFLBV6P3c99Qxg+9Gxk2luQWphWUyOzsErh/w5cU qzwg== X-Gm-Message-State: AOJu0YycsE9C5nbOyX+ou95lWkHrfZ/uItVH5g7Q1rChFM7eQH43ZN6m jy1OgKQW7PM/qFKuXnVgfJRn1IF8+zCx4vbo4kSy+NKXn3ur2oQ6sZXUuFmBkKV30QuWSxDGM4u tBXlv7D119nLc8Aj8OlBdf3TbAiUogufI X-Gm-Gg: ASbGncv2KSp8LUk1t9SRRVXajoMesD98G+Umi4vZlUBnXy3YLBvxts3kJjbwxYJ20EX v3YXC7hhL1F91a9fC4RDiwSaNkXVlRqKtlsSsOcXu6R4bnykRXkZ421JrfcHEZvSoMJYBDr1XeG 8gE3wAjCkhr9sQaT0wl/rx/1TjUw== X-Google-Smtp-Source: AGHT+IG5PrYYuZC6sjA5J98gqa2H3OwzHk6TsV/m9Mb+C4cp20g78amY9uNP0jCtzhvTMGbmQH75TIQrIU2MIdmEnRk= X-Received: by 2002:a17:906:dc8e:b0:abf:75ba:c99f with SMTP id a640c23a62f3a-ac252f8ffdcmr459940066b.46.1741365593772; Fri, 07 Mar 2025 08:39:53 -0800 (PST) MIME-Version: 1.0 References: <2muwyx6a5vojkg7iegknhnkcch3lfxptsxk7icwuh7szkvvu2y@vc3ukkfvnu6i> In-Reply-To: <2muwyx6a5vojkg7iegknhnkcch3lfxptsxk7icwuh7szkvvu2y@vc3ukkfvnu6i> From: Alexander Korotkov Date: Fri, 7 Mar 2025 18:39:42 +0200 X-Gm-Features: AQ5f1Jqt92XRl4ridqAhBF4OhILLx4RECbz6xkI_nbAgEiu0vuDNM4Am7CSS5Bw Message-ID: Subject: Re: pg_atomic_compare_exchange_*() and memory barriers To: Andres Freund Cc: pgsql-hackers Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi, Andres. Thank you for reply. On Fri, Mar 7, 2025 at 6:02=E2=80=AFPM Andres Freund w= rote: > > On 2025-03-07 17:47:08 +0200, Alexander Korotkov wrote: > > While investigating a bug in the patch to get rid of WALBufMappingLock,= I > > found that the surrounding pg_atomic_compare_exchange_u64() fixes the > > problem for me. > > That sounds more likely to be due to slowing down things enough to make a= race > less likely to be hit. Or a compiler bug. > > > > That doesn't feel right because, according to the > > comments, both pg_atomic_compare_exchange_u32() and > > pg_atomic_compare_exchange_u64() should provide full memory barrier > > semantics. So, I decided to investigate this further. > > > > In my case, the implementation of pg_atomic_compare_exchange_u64() is b= ased > > on __atomic_compare_exchange_n(). > > > > static inline bool > > pg_atomic_compare_exchange_u64_impl(volatile pg_atomic_uint64 *ptr, > > uint64 *expected, uint64 newval) > > { > > AssertPointerAlignment(expected, 8); > > return __atomic_compare_exchange_n(&ptr->value, expected, newval, f= alse, > > __ATOMIC_SEQ_CST, __ATOMIC_SEQ_C= ST); > > } > > > > According to the docs __ATOMIC_SEQ_CST enforces total ordering with *al= l > > other __ATOMIC_SEQ_CST operations*. It only says about other > > __ATOMIC_SEQ_CST operations; nothing is said about regular reads/writes= . > > This sounds quite far from our comment, promising full barrier semantic= s, > > which, in my understanding, means the equivalent of > > both pg_read_barrier()/pg_write_barrier(), which should barrier *all ot= her > > read/writes*. > > A memory barrier in one process/thread always needs be paired with a barr= ier > in another process/thread. It's not enough to have a memory barrier on on= e > side but not the other. Yes, there surely should be a memory barrier on another side. But does __ATOMIC_SEQ_CST works as a barrier for the regular read/write operations on the same side? > __ATOMIC_SEQ_CST is used to implement the C11/C++11 barriers, and for tho= se > it's more clearly formulated that that include acquire/release semantics.= The > standard formulation is a bit more complicated IIRC, but here's the > cppreference.com simplification: > > https://en.cppreference.com/w/c/atomic/memory_order > > A load operation with this memory order performs an acquire operation,= a > > store performs a release operation, and read-modify-write performs bot= h an > > acquire operation and a release operation, plus a single total order e= xists > > in which all threads observe all modifications in the same order (see > > Sequentially-consistent ordering below). Thank you. This is not yet clear for me. I probably need to meditate more on this :) > > > This function first checks if LSE instructions are present. If so, > > instruction LSE casal is used. If not (in my case), the loop of > > ldaxr/stlxr is used instead. The documentation says both ldaxr/stlxr > > provides one-way barriers. Read/writes after ldaxr will be observed af= ter, > > and read/writes before stlxr will be observed before. So, a pair of th= ese > > instructions provides a full memory barrier. However, if we don't obse= rve > > the expected value, only ldaxr gets executed. So, an unsuccessful > > pg_atomic_compare_exchange_u64() attempt will leave us with a one-way > > barrier, and that caused a bug in my case. > > That has to be a compiler bug. We specify __ATOMIC_SEQ_CST for both > success_memorder *and* failure_memorder. > > What compiler & version is this? I've tried and got the same results with two compilers. gcc (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0 Ubuntu clang version 19.1.1 (1ubuntu1~24.04.2) ------ Regards, Alexander Korotkov Supabase