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 1sYV8K-00Bq1r-2m for pgsql-hackers@arkaria.postgresql.org; Mon, 29 Jul 2024 18:30:08 +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 1sYV8H-002E0E-5G for pgsql-hackers@arkaria.postgresql.org; Mon, 29 Jul 2024 18:30:05 +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 1sYV8G-002E06-QD for pgsql-hackers@lists.postgresql.org; Mon, 29 Jul 2024 18:30:04 +0000 Received: from fout5-smtp.messagingengine.com ([103.168.172.148]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sYV88-0026zb-Ui for pgsql-hackers@postgresql.org; Mon, 29 Jul 2024 18:30:03 +0000 Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailfout.nyi.internal (Postfix) with ESMTP id CED9B13801A1; Mon, 29 Jul 2024 14:29:54 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute3.internal (MEProxy); Mon, 29 Jul 2024 14:29:54 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=anarazel.de; h= cc:cc: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=fm3; t=1722277794; x=1722364194; bh=fuYVDK5/pw 0SaKPO8fwrjamHPOPCIL4sROQcUMMze/s=; b=yGTHpva25FOy7g6gUDkSPY/7YP DGSJW4ODOt4bFQzZsIfb28YDw1WZDKPLeb9yPItK8CcdB+8tp4x72nfJzhXTaspN /KHal8v5SnCYowUuzZm+//5i98EyvEVvdJObn9wlpoF8BBPr2v3S5wCkBcekPjaZ pekZtfRNsirAjT1PocuKvfaRyDPZejOWmyV2ENCz3iH4AAkASaldjq6IMCbkE1Jm ES/EzWrR06piB0+0K/I5WMHtLLu9nUb8ArM+6QHHYYfNv3pxwVRr5HtQ+5e0AVDw UsG8YGnhxUl13iy3NIBCZljlrvNWvXN/Jdv+oGiGp7HE0Rqq3YiWNlfcNW6w== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc: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-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm3; t=1722277794; x=1722364194; bh=fuYVDK5/pw0SaKPO8fwrjamHPOPC IL4sROQcUMMze/s=; b=mps5BCiY/GO+sxDt8ROjQ+ZlcCTQg/RF0dLQbfBSE4dZ 99uxlI38w8BZTax5SVVZfib8tvEtcb7fki2XTE3igHKbgPJ8ICjjbhSGwVwMdGEb DpI5sASk2og4iOBuA+ar+aHhVduEZruzKLcxdjtMark5OWOqw1EfKct4gaVB6RAc zpIg0WRuATUJm23NnGEj4/jR63Pf9VqEvlZp8zDpUFF6Q6GPqwjDeXl5b78GxniB 13dIDqdsg8VN2JzjHaXzIs1UptMxvUWf/0H49i/xkpXkjoyArYORW+dhEccNK1q5 GBM4gSYrQ5+6xlL097r5LswTt91Kp8WiNlGWa+UDWg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrjedvgdduvdejucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkfhggtggujgesmhdtreertddtvdenucfhrhhomheptehnughr vghsucfhrhgvuhhnugcuoegrnhgurhgvshesrghnrghrrgiivghlrdguvgeqnecuggftrf grthhtvghrnhepkeekueffgfejleefkedtffeghefffffhteehfefgleelgffgkefhheeg ledvfeeunecuffhomhgrihhnpehpohhsthhgrhdrvghsnecuvehluhhsthgvrhfuihiivg eptdenucfrrghrrghmpehmrghilhhfrhhomheprghnughrvghssegrnhgrrhgriigvlhdr uggvpdhnsggprhgtphhtthhopedt X-ME-Proxy: Feedback-ID: id4a34324:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 29 Jul 2024 14:29:54 -0400 (EDT) Date: Mon, 29 Jul 2024 11:29:52 -0700 From: Andres Freund To: Tom Lane Cc: Heikki Linnakangas , pgsql-hackers@postgresql.org Subject: Re: Detect double-release of spinlock Message-ID: <20240729182952.hua325647e2ggbsy@awork3.anarazel.de> References: <2600757.1722267116@sss.pgh.pa.us> <20240729161846.kgiafbe2dtgabxgb@awork3.anarazel.de> <2802449.1722270793@sss.pgh.pa.us> <20240729164026.yurg37tej34o76uk@awork3.anarazel.de> <20240729165154.56zqyg34x2ywkpsh@awork3.anarazel.de> <2809049.1722273922@sss.pgh.pa.us> <20240729174853.uo5qjqkxlacjg6vv@awork3.anarazel.de> <2812470.1722275822@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="fx3sz6dtlxai2igx" Content-Disposition: inline In-Reply-To: <2812470.1722275822@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --fx3sz6dtlxai2igx Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Hi, Partially replying here to an email on -committers [1]. On 2024-07-29 13:57:02 -0400, Tom Lane wrote: > Andres Freund writes: > > However, I still don't think it's a problem to assert that the lock is held in > > in the unlock "routine". As mentioned before, the spinlock implementation > > itself has never followed the "just straight line code" rule that users of > > spinlocks are supposed to follow. > > Yeah, that's fair. Cool. On 2024-07-29 13:56:05 -0400, Tom Lane wrote: > Andres Freund writes: > > b) Instead define the spinlock to have 1 as the unlocked state and 0 as the > > locked state. That makes it a bit harder to understand that initialization > > is missing, compared to a dedicated state, as the first use of the spinlock > > just blocks. > > This option works for me. > > To make 1) b) easier to understand it might be worth changing the slock_t > > typedef to be something like > > > typedef struct slock_t > > { > > char is_free; > > } slock_t; > > +1 Cool. I've attached a prototype. I just realized there's a nice little advantage to the "inverted" representation - it detects missing initialization even in optimized builds. > How much of this would we change across platforms, and how much > would be x86-only? I think there are enough people developing on > ARM (e.g. Mac) now to make it worth covering that, but maybe we > don't care so much about anything else. Not sure. Right now I've only hacked up x86-64 (not even touching i386), but it shouldn't be hard to change at least some additional platforms. My current prototype requires S_UNLOCK, S_LOCK_FREE, S_INIT_LOCK to be implemented for x86-64 instead of using the "generic" implementation. That'd be mildly annoying duplication if we did so for a few more platforms. It'd be more palatable to just change all platforms if we made more of them use __sync_lock_test_and_set (or some other intrinsic(s))... Greetings, Andres Freund [1] https://postgr.es/m/2812376.1722275765%40sss.pgh.pa.us --fx3sz6dtlxai2igx Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="v2-0001-Detect-unlocking-an-unlocked-spinlock.patch"