From: Andres Freund <andres@anarazel.de>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: Heikki Linnakangas <hlinnaka@iki.fi>
Cc: pgsql-hackers@postgresql.org
Subject: Re: Detect double-release of spinlock
Date: Mon, 29 Jul 2024 11:29:52 -0700
Message-ID: <20240729182952.hua325647e2ggbsy@awork3.anarazel.de> (raw)
In-Reply-To: <2812470.1722275822@sss.pgh.pa.us>
References: <E1sYSF2-001lEB-D1@gemulon.postgresql.org>
<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>
<e8d4ef94-b8aa-4cda-8f31-081f78194275@iki.fi>
<2809049.1722273922@sss.pgh.pa.us>
<20240729174853.uo5qjqkxlacjg6vv@awork3.anarazel.de>
<2812470.1722275822@sss.pgh.pa.us>
Hi,
Partially replying here to an email on -committers [1].
On 2024-07-29 13:57:02 -0400, Tom Lane wrote:
> Andres Freund <andres@anarazel.de> 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 <andres@anarazel.de> 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.usAttachments:
v2-0001-Detect-unlocking-an-unlocked-spinlock.patch (text/x-diff, 0B) download
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-committers@postgresql.org
Cc: andres@anarazel.de, tgl@sss.pgh.pa.us, hlinnaka@iki.fi, pgsql-hackers@postgresql.org
Subject: Re: Detect double-release of spinlock
In-Reply-To: <20240729182952.hua325647e2ggbsy@awork3.anarazel.de>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox