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.96) (envelope-from ) id 1vMWvo-00CnFj-2g for pgsql-hackers@arkaria.postgresql.org; Fri, 21 Nov 2025 19:36:33 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vMWvl-009BiD-0X for pgsql-hackers@arkaria.postgresql.org; Fri, 21 Nov 2025 19:36:29 +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.96) (envelope-from ) id 1vMWvk-009Bi4-1S for pgsql-hackers@lists.postgresql.org; Fri, 21 Nov 2025 19:36:29 +0000 Received: from fout-b8-smtp.messagingengine.com ([202.12.124.151]) by makus.postgresql.org with smtp (Exim 4.96) (envelope-from ) id 1vMWvh-000kIe-1Z for pgsql-hackers@lists.postgresql.org; Fri, 21 Nov 2025 19:36:27 +0000 Received: from phl-compute-11.internal (phl-compute-11.internal [10.202.2.51]) by mailfout.stl.internal (Postfix) with ESMTP id 9D4091D00086; Fri, 21 Nov 2025 14:36:24 -0500 (EST) Received: from phl-mailfrontend-02 ([10.202.2.163]) by phl-compute-11.internal (MEProxy); Fri, 21 Nov 2025 14:36:24 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=burd.me; h=cc:cc :content-transfer-encoding:content-type:content-type:date:date :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to; s=fm1; t=1763753784; x=1763840184; bh=JblmGeewhwoC0jSe99aCvmvln80bnhtFP7lVfJoWyKM=; b= RGumd/AlPUf3aTvywUFM3gTqa9JSxDi2HSAoSlpvmsWWF9vM+0IxOQf+9+WVoWWY 9Sh1In1wpFHEyRJHCyYBY4x0T5hz9nnr1IZAcMMeY38oF0Y6wyKrwNtVV2p66qTe 6YuvoeEWUJ/9U4MPB7C6hHYa9VUyXfGlX0gDgnYl7CgWsHABVdzawKTO98vGD4Wu 1SPZTZ3qhQGJLJFBg3mzZHiKLg0ERPN9xz6EZ4MIDBAX7mG2UsS+k8CucMpq3mq4 aXkbThAaE4b5wP2UfKrpx4apdwVsAtDuFhspcT168UKytacZ6GO8MFl3rALm5XuX GV7mb+43unjaxeK/aan90A== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm3; t=1763753784; x= 1763840184; bh=JblmGeewhwoC0jSe99aCvmvln80bnhtFP7lVfJoWyKM=; b=R eJdDZHKenQBctT+glmoa4qY7YKbTZrUYLUXP91d3Mj157CnMn+H4pTgOfFurtqut PZ2qGAjyJ3PNpCjTeZCE7YJNxo7Y4m4DSfcEaNYO+MsWOxmiVlMvLiAb6gItOEAf tf7Iu0Kj8CSEIWs0kBCyiYd/7q0BGH6YhHOO5olxWqDk8x7vUsum8WVS/A1mCVEC AncQHRrQfUzeM+GYucFuY+M60tv7Fs57TAzhsEKeAZnmsOhnO0c9YZOS94cMzOi5 k5sovfxjGPTS+48Vv5od0zYRsEVy1++yUJKqKtHPyZcsJ8JrZmODSFKrHbLxZgar 3v6WzIMIR/ktaJ0vBX75w== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeffedrtdeggddvfedtkedvucetufdoteggodetrf dotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfurfetoffkrfgpnffqhgenuceu rghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmnecujf gurhepfffhvfevkfgjfhfuofggtgfgugesthejmhdtredtjeenucfhrhhomhepifhrvghg uceuuhhrugcuoehgrhgvghessghurhgurdhmvgeqnecuggftrfgrthhtvghrnhepvdeihf fggfetvedvledtteettdehvdeggfeljefgfedvhfeftdetveekgeeitdevnecuffhomhgr ihhnpegrrhhmrdgtohhmnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrg hilhhfrhhomhepghhrvghgsegsuhhrugdrmhgvpdhnsggprhgtphhtthhopeefpdhmohgu vgepshhmthhpohhuthdprhgtphhtthhopegrnhgurhgvshesrghnrghrrgiivghlrdguvg dprhgtphhtthhopehpghhsqhhlqdhhrggtkhgvrhhssehlihhsthhsrdhpohhsthhgrhgv shhqlhdrohhrghdprhgtphhtthhopegurghvvggtrhgrmhgvrhesghhmrghilhdrtghomh X-ME-Proxy: Feedback-ID: i675e48f3:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 21 Nov 2025 14:36:23 -0500 (EST) Date: Fri, 21 Nov 2025 14:36:12 -0500 From: Greg Burd To: Andres Freund Cc: PostgreSQL Hackers , Dave Cramer Message-ID: <1D2B3555-359B-47A2-B291-B23E8F4CAF39@greg.burd.me> In-Reply-To: References: Subject: Re: [PATCH] Fix ARM64/MSVC atomic memory ordering issues on Win11 by adding explicit DMB =?utf-8?Q?=E2=80=8Bbarriers?= X-Mailer: Mailspring MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Content-Disposition: inline List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Nov 20 2025, at 7:03 pm, Andres Freund wrote: > Hi, > > On 2025-11-20 15:45:22 -0500, Greg Burd wrote: >> Dave and I have been working together to get ARM64 with MSVC functional. >> The attached patches accomplish that. Dave is the author of the first >> which addresses some build issues and fixes the spin_delay() semantics, >> I did the second which fixes some atomics in this combination. > > Thanks for working on this! You're welcome, thanks for reviewing it. :) >> >> MSVC's _InterlockedCompareExchange() intrinsic on ARM64 performs the >> atomic operation but does NOT emit the necessary Data Memory Barrier >> (DMB) instructions [4][5]. > > I couldn't reproduce this result when playing around on godbolt. By specifying > /arch:armv9.4 msvc can be convinced to emit the code for the > intrinsics inline > (at least for most of them). And that makes it visible that > _InterlockedCompareExchange() results in a "casal" instruction. > Looking that > up shows: > https://developer.arm.com/documentation/dui0801/l/A64-Data-Transfer-Instructions/CASA--CASAL--CAS--CASL--CASAL--CAS--CASL--A64- > which includes these two statements: > "CASA and CASAL load from memory with acquire semantics." > "CASL and CASAL store to memory with release semantics." I didn't even think to check for a compiler flag for the architecture, nice call! If this emits the correct instructions it is a much better approach. I'll give it a try, thanks for the nudge. >> Issue 2: S_UNLOCK() uses only a compiler barrier >> >> _ReadWriteBarrier() is a compiler barrier, NOT a hardware memory >> barrier [6]. It prevents the compiler from reordering operations, but >> the CPU can still reorder memory operations. This is fundamentally >> insufficient for ARM64's weaker memory model. > > Yea, that seems broken on a non-TSO architecture. Is the problem > fixed if you change just this to include a proper barrier? Using the flag from above the _ReadWriteBarrier() does (in godbolt) turn into a casal which (AFAIK) is going to do the trick. I'll see if I can update meson.build and get this work as intended. > Greetings, > > Andres Freund best. -greg