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 1sYUrH-00BnKP-9q for pgsql-hackers@arkaria.postgresql.org; Mon, 29 Jul 2024 18:12:31 +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 1sYUrF-0028Y3-RJ for pgsql-hackers@arkaria.postgresql.org; Mon, 29 Jul 2024 18:12: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.94.2) (envelope-from ) id 1sYUrF-0028Xu-I0 for pgsql-hackers@lists.postgresql.org; Mon, 29 Jul 2024 18:12:29 +0000 Received: from fout5-smtp.messagingengine.com ([103.168.172.148]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sYUr8-0024kN-VT for pgsql-hackers@postgresql.org; Mon, 29 Jul 2024 18:12:28 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailfout.nyi.internal (Postfix) with ESMTP id 48BA31380288; Mon, 29 Jul 2024 14:12:22 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute4.internal (MEProxy); Mon, 29 Jul 2024 14:12:22 -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=1722276742; x=1722363142; bh=5LsVIAqDdv bEjtqtACxjtneX1WnYoRPctMGP4cxsViA=; b=BGoNyNa7i0bCmEeBfW4Yj4TAGt Chqhe8ZIaWuxT5bXrh+7x4YqwBOZ5g4Ifr2yj5oq3F370FIiFFsDQAQAMS2Qpk3t Ujob5whtWklbbvUopSSljOViebDprLl8+BVFamGdNxRbXnA5Ntzb3ES/V5WYE3KE uNU1DfJkiO/rczPo2dWUMdVCW/fPoH19aNIvSYJudkXz7QjCy+HZ600bR/VsMX7c 5digfJ3vDS4hJbUioERBgV5yHmIRUUQxqP2vgQwlarsSZJ2bLOFoY5Gzx4nNAy/G BZyvdeBb/0mzGPVomc5tuWxfgAsYEL1Gu/Giup0ZuRt24FlYol6K+3/k0smg== 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=1722276742; x=1722363142; bh=5LsVIAqDdvbEjtqtACxjtneX1WnY oRPctMGP4cxsViA=; b=rHSOMq910/ehJcZftUcbAnCuTWi1YSyU1o5I6cibaNgC 4Rs6AsA3fjLg5L9LfkLks3M2ajJzlcJcrE7rGOuTrlI0c+QBbJuetsuuyXzbD9j1 ele+w/QUkhXzo6TStgHST3RlesxcYsBZAe/gxXDcNJZhhmJ5I9X+jDMq45Fm57JT dOVTzgL5/iMB8FD7ALqNICHTOzdzC4++7RIta9EH2zv1TdqX83MTHMnMwZaNkyw1 tDEg9glPVX1suVHZRtkcDpL6z5zYhBEolfyd6u63sk9WrF1881roqHP9NADcaYTg SNIa/1QhSoRLFBRuztGDPo8Gmf67BDSBqy9k0mLpGg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrjedvgdduvdefucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkfhggtggujgesthdtredttddtvdenucfhrhhomheptehnughr vghsucfhrhgvuhhnugcuoegrnhgurhgvshesrghnrghrrgiivghlrdguvgeqnecuggftrf grthhtvghrnhepvdfffeevhfetveffgeeiteefhfdtvdffjeevhfeuteegleduheetvedu ieettddunecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomh eprghnughrvghssegrnhgrrhgriigvlhdruggvpdhnsggprhgtphhtthhopedt X-ME-Proxy: Feedback-ID: id4a34324:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 29 Jul 2024 14:12:21 -0400 (EDT) Date: Mon, 29 Jul 2024 11:12:19 -0700 From: Andres Freund To: Heikki Linnakangas Cc: Tom Lane , pgsql-hackers@postgresql.org Subject: Re: Detect double-release of spinlock Message-ID: <20240729181219.vmrzzo7y52ui3pco@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> <790b901f-c0f6-4e97-b59a-97d8f921d57e@iki.fi> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <790b901f-c0f6-4e97-b59a-97d8f921d57e@iki.fi> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi, On 2024-07-29 21:00:35 +0300, Heikki Linnakangas wrote: > On 29/07/2024 20:48, Andres Freund wrote: > > On 2024-07-29 13:25:22 -0400, Tom Lane wrote: > > > Heikki Linnakangas writes: > > > > Yeah I'm not worried about that at all. Also, the assert is made when > > > > you have already released the spinlock; you are already out of the > > > > critical section. > > > > > > Not in the patch Andres posted. > > > > Which seems fairly fundamental - once outside of the critical section, we > > can't actually assert that the lock isn't acquired, somebody else *validly* > > might have acquired it by then. > > You could do: > > bool was_free = S_LOCK_FREE(lock); > > S_UNLOCK(lock); > Assert(!was_free); I don't really see the point - we're about to crash with an assertion failure, why would we want to do that outside of the critical section? If anything that will make it harder to debug the issue in a core dump, because other backends might "destroy evidence" due to being able to acquire the spinlock. > Depending on the underlying implementation, you could also use > compare-and-exchange. That'd scale a lot worse, at least on x86-64, as it requires the unlock to be an atomic op, whereas today it's a simple store (+ compiler barrier). I've experimented with replacing all spinlocks with lwlocks, and the fact that you need an atomic op for an rwlock release is one of the two major reasons they have a higher overhead (the remainder is boring stuff like the overhead of external function calls and ownership management). Greetings, Andres Freund