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 1tqblz-007fF7-9w for pgsql-hackers@arkaria.postgresql.org; Fri, 07 Mar 2025 17:46:11 +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 1tqbly-00DR1J-0m for pgsql-hackers@arkaria.postgresql.org; Fri, 07 Mar 2025 17:46:10 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tqblx-00DR0l-N4 for pgsql-hackers@lists.postgresql.org; Fri, 07 Mar 2025 17:46:09 +0000 Received: from mail-ej1-x634.google.com ([2a00:1450:4864:20::634]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1tqblv-001XEF-2U for pgsql-hackers@postgresql.org; Fri, 07 Mar 2025 17:46:08 +0000 Received: by mail-ej1-x634.google.com with SMTP id a640c23a62f3a-aaec61d0f65so428807766b.1 for ; Fri, 07 Mar 2025 09:46:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1741369566; x=1741974366; 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=tuHsKzNNhCLcYjrut8rJ3Gy+TaSrzNmjwGG43h2yErM=; b=g8FltRFfFCslD+Lzne8+qzbLanMxzqZ0ysHJpbdxsN28NIVKBuwBTgq9sPLQPciX0O hv7aOrkFuIgVEbp402CKWbXr2ABZC30GBM3Wj3HtHv2rMXKTyeXCCwaTYqyQuP8NyBdN G7ALDpa3k/5j7k6xIVvqahwfDnqpmns1ufIPQ++MBSHX1Cu9VgMz//oX+Iwfy31ksiTL +I9McS3cYPyfKMtqV+pLFBaZ+rZkgXZSrpWLcd/qhMaqxfVkOPv0l3O20xgkmrSJbEDA CmCwqZZtoFAz2TyL+Crh601Bn3NzAjmn2vjUUm3oV+3Hb3TUFZCi0XrJB8sw69zm074X jGwg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741369566; x=1741974366; 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=tuHsKzNNhCLcYjrut8rJ3Gy+TaSrzNmjwGG43h2yErM=; b=ipNh2Wf1d8OfyFX7n78x0ZDbCyZbcUyqi6Y1Nww+Hbs4jYAeZhQnhAgLK8V3SxPRSV FBL8I0D6yWWmy3EpGarJ7AnPra2CKWsDg+ELrkyug4HV3wG1jcwGi2vzId4fS/vtJTMf McToGW2CcA496medDrzS0JZQ/GmK0ZdhQQZeiPHJ5/kunt7+/WWcx9UlKy6oM6ePUv0j WbsENlsm9wojWZEnM10zFMqxIiRfyJHxZzcquufukxkzRETiuZrlVb/RbCLRje+oIUa5 atrO9dYoFP8HcjYwF9U/uIzsHG5QvFhiwOGn5936EdMQZOb5eOyp2NcaMknTOEql4G3r VT8w== X-Gm-Message-State: AOJu0YwnI8dvNqKA9RbTL/Bj4VDleGunHaj+l07K2LfjWJ5roW/At6s4 5+648dl048nhEN1wyG3nMxbnEsHE+TNP+ZEElZXMsUbmP7pXp+FEzW405UC7WflO3Vjxc5Apt1l cU9tzwvyLbOLjbva+6h3/7kVT8tM= X-Gm-Gg: ASbGnctQMZx1IN8cFVgphK8h7ASaLPZZAl2VYiiJ5Ox3v5kFY1F2x4L+G3STjyplV3S adr9PExEuFCdJ8b5O9LPI0bAdeqAi9AoO09KB3oLB/yDnCIaE3E39V3HGhtSvVw1waXVsx1t3oi +jkIn949SvrGlPTtWDekAVh+x84w== X-Google-Smtp-Source: AGHT+IHYm5rKXRHGpgu+LRTtf4//oNPhdbKmjsLl45h904zvgmbxFcOk+5q1FG0BCnkNAQ2WmUKUORo0uMn/auiQn5E= X-Received: by 2002:a17:907:61a2:b0:ac1:e759:fbf2 with SMTP id a640c23a62f3a-ac2525ba5a6mr378787466b.7.1741369566086; Fri, 07 Mar 2025 09:46:06 -0800 (PST) MIME-Version: 1.0 References: <2muwyx6a5vojkg7iegknhnkcch3lfxptsxk7icwuh7szkvvu2y@vc3ukkfvnu6i> In-Reply-To: From: Alexander Korotkov Date: Fri, 7 Mar 2025 19:45:55 +0200 X-Gm-Features: AQ5f1JpkNTKl2uditlAa4Yk-Y6YlPLvm0uiIpcK7ZHhJEl1V0trfB9z9E7H2IOs 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 On Fri, Mar 7, 2025 at 7:07=E2=80=AFPM Andres Freund w= rote: > On 2025-03-07 18:39:42 +0200, Alexander Korotkov wrote: > > On Fri, Mar 7, 2025 at 6:02=E2=80=AFPM Andres Freund wrote: > > > > > > On 2025-03-07 17:47:08 +0200, Alexander Korotkov wrote: > > > > While investigating a bug in the patch to get rid of WALBufMappingL= ock, I > > > > found that the surrounding pg_atomic_compare_exchange_u64() fixes t= he > > > > problem for me. > > > > > > That sounds more likely to be due to slowing down things enough to ma= ke 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 based > > > > 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, newva= l, false, > > > > __ATOMIC_SEQ_CST, __ATOMIC_S= EQ_CST); > > > > } > > > > > > > > According to the docs __ATOMIC_SEQ_CST enforces total ordering with= *all > > > > other __ATOMIC_SEQ_CST operations*. It only says about other > > > > __ATOMIC_SEQ_CST operations; nothing is said about regular reads/wr= ites. > > > > This sounds quite far from our comment, promising full barrier sema= ntics, > > > > which, in my understanding, means the equivalent of > > > > both pg_read_barrier()/pg_write_barrier(), which should barrier *al= l other > > > > read/writes*. > > > > > > A memory barrier in one process/thread always needs be paired with a = barrier > > > in another process/thread. It's not enough to have a memory barrier o= n one > > > 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? > > Yes, if it's paired with another barrier on the other side. The regular > read/write operations are basically protected transitively, due to > > a) An acquire barrier preventing non-atomic reads/writes in the same thre= ad > from appearing to have been moved to before the barrier > > b) A release barrier preventing non-atomic reads/writes in the same threa= d > from appearing to have been moved to after the barrier > > c) The other thread being guaranteed a) and b) for the other threads' > non-atomic reads/writes depending on the type of barrier > > d) The atomic value itself being guaranteed to be, well, atomic > > > > > > 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/stl= xr > > > > provides one-way barriers. Read/writes after ldaxr will be observe= d after, > > > > and read/writes before stlxr will be observed before. So, a pair o= f these > > > > instructions provides a full memory barrier. However, if we don't = observe > > > > the expected value, only ldaxr gets executed. So, an unsuccessful > > > > pg_atomic_compare_exchange_u64() attempt will leave us with a one-w= ay > > > > 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) > > Thinking more about it I wonder if the behaviour of not doing a release i= n > case the atomic fails *might* arguably actually be correct - if the > compare-exchange fails, there's nothing that the non-atomic values could = be > ordered against. There is another successful compare-exchange executed by a concurrent process to get ordered against. ------ Regards, Alexander Korotkov Supabase