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 1sYURw-00BjYB-Oe for pgsql-committers@arkaria.postgresql.org; Mon, 29 Jul 2024 17:46:21 +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 1sYURv-001nZs-5x for pgsql-committers@arkaria.postgresql.org; Mon, 29 Jul 2024 17:46:19 +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 1sYURt-001nU4-7C for pgsql-committers@lists.postgresql.org; Mon, 29 Jul 2024 17:46:18 +0000 Received: from fhigh1-smtp.messagingengine.com ([103.168.172.152]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sYURp-0024XF-OY for pgsql-committers@lists.postgresql.org; Mon, 29 Jul 2024 17:46:15 +0000 Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailfhigh.nyi.internal (Postfix) with ESMTP id 2323A11401B1; Mon, 29 Jul 2024 13:46:11 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute3.internal (MEProxy); Mon, 29 Jul 2024 13:46:11 -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=1722275171; x=1722361571; bh=EbETOPJS3P PWV2FSRsm9F/x+uFI7ONQ3O3XwEOY4sxM=; b=aD9As0Pro/6BtnJbxH0p33QY8K 4apB9FmCAgnrI1XdvY/GoZBOvK0lc7Kvx3tEKatvUbKfcfmVgm3vTOazPIkVpHS5 JU8mu60AJlagI6XzKfDLBGqog711ws8ABXxaF/4TLxmyne+BZaujYz6ivdtC3I7P pG1WLl28avK7MV6hAqkH8ymvOnt406W6kexWajvME9hmDhERMhYY04rXFx88hqUv 2IwEj7tPFLh+Eio+NbU5kRu+Eag9/h33WEcAaxRUPoObbgQ0oTN7VVstxWkorpN2 zRCZaXmUaPDqS+skCr+BNbtVlhHrALArow5+JiHO++OC9DS4gc6UHQ6cRlmA== 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=1722275171; x=1722361571; bh=EbETOPJS3PPWV2FSRsm9F/x+uFI7 ONQ3O3XwEOY4sxM=; b=cYvz+hCNZYG9Qjc5dMBC2Y/Njk70Tz/3JarLs8SqR+Ti z3I5EqD3uAybtvsJBWrhCJNwyl90FjThRaywXrLfKjG+6/wPbv9mkXTp5Q2CWk/H PuEDiTwO8++3olNCZqVbQ2MRA1hDC6YrL32IGI6uoxxGZg9xx4J9lmw6vVJL9h0R 4XmHHZOkue2zs9hed49MBP9zlZdaYGU8oC+iuwB/98m5vIYqvVC6YjkX6NJ9ZWg3 +PLB52/oyzwRDRW9t9EZvsF43AFOYKjJktM8Zh6OfpJxFhcS/qOXt/9B4+wdoBMA Ir8lWe5VYmN4NW5QnMFx+oEDV99sbFUEB3CGqztOfQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrjedvgdduudekucetufdoteggodetrfdotf 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 13:46:10 -0400 (EDT) Date: Mon, 29 Jul 2024 10:46:09 -0700 From: Andres Freund To: Tom Lane Cc: Heikki Linnakangas , pgsql-committers@lists.postgresql.org Subject: Re: pgsql: Fix double-release of spinlock Message-ID: <20240729174609.jbfei3sokdobeeeo@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> <2803949.1722271519@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <2803949.1722271519@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 2024-07-29 12:45:19 -0400, Tom Lane wrote: > Andres Freund writes: > > On 2024-07-29 12:33:13 -0400, Tom Lane wrote: > >> I dunno, is that the only extra check that the --disable-spinlocks > >> implementation is providing? > > > I think it also provides the (valuable!) check that spinlocks were actually > > initialized. But that again seems like something we'd be better off adding > > more general infrastructure for - nobody runs --disable-spinlocks locally, we > > shouldn't need to run this on the buildfarm to find problems like this. > > Hmm, but how? I think there's a few ways: > One of the things we gave up by nuking HPPA support > was that that platform's representation of an initialized, free > spinlock was not all-zeroes, so that it'd catch this type of problem. > I think all the remaining platforms do use zeroes, so it's hard to > see how anything short of valgrind would be likely to catch it. 1) There's nothing forcing us to use 0/1 for most of the spinlock implementations. E.g. for x86-64 we could use 0 for uninitialized, 1 for free and 2 for locked. 2) We could also change the layout of slock_t in assert enabled builds, adding a dedicated 'initialized' field when assertions are enabled. But that might be annoying from an ABI POV? 1) seems preferrable, so I gave it a quick try. Seems to work. There's a *slight* difference in the instruction sequence: old: 41f6: f0 86 10 lock xchg %dl,(%rax) 41f9: 84 d2 test %dl,%dl 41fb: 75 1b jne 4218 new: 4216: f0 86 10 lock xchg %dl,(%rax) 4219: 80 fa 02 cmp $0x2,%dl 421c: 74 22 je 4240 I.e. the version using 2 as the locked state uses a three byte instruction vs a two byte instruction before. *If* we are worried about this, we could a) Change the representation only for assert enabled builds, but that'd have ABI issues again. 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. 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; which also might help catch some cases of type confusion - the char typedef is too forgiving imo. Greetings, Andres Freund