global barrier & atomics in signal handlers (Re: Atomic operations within spinlocks)
Andres Freund <andres@anarazel.de>
From: Andres Freund <andres@anarazel.de>
To: Robert Haas <robertmhaas@gmail.com>
Cc: pgsql-hackers@lists.postgresql.org, Tom Lane <tgl@sss.pgh.pa.us>
Date: 2020-06-09T19:37:23Z
Lists: pgsql-hackers
Hi, On 2020-06-08 23:08:47 -0700, Andres Freund wrote: > On 2020-06-05 17:19:26 -0700, Andres Freund wrote: > > I wrote a patch for this, and when I got around to to testing it, I > > found that our tests currently don't pass when using both > > --disable-spinlocks and --disable-atomics. Turns out to not be related > > to the issue above, but the global barrier support added in 13. > > > > That *reads* two 64 bit atomics in a signal handler. Which is normally > > fine, but not at all cool when atomics (or just 64 bit atomics) are > > backed by spinlocks. Because we can "self interrupt" while already > > holding the spinlock. > > > > It looks to me that that's a danger whenever 64bit atomics are backed by > > spinlocks, not just when both --disable-spinlocks and --disable-atomics > > are used. But I suspect that it's really hard to hit the tiny window of > > danger when those options aren't used. While we have buildfarm animals > > testing each of those separately, we don't have one that tests both > > together... > > > > I'm not really sure what to do about that issue. The easisest thing > > would probably be to change the barrier generation to 32bit (which > > doesn't have to use locks for reads in any situation). I tested doing > > that, and it fixes the hangs for me. > > > > > > Randomly noticed while looking at the code: > > uint64 flagbit = UINT64CONST(1) << (uint64) type; > > > > that shouldn't be 64bit, right? > > Attached is a series of patches addressing these issues, of varying > quality: > > 1) This fixes the above mentioned issue in the global barrier code by > using 32bit atomics. That might be fine, or it might not. I just > included it here because otherwise the tests cannot be run fully. Hm. Looking at this again, perhaps the better fix would be to simply not look at the concrete values of the barrier inside the signal handler? E.g. we could have a new PROCSIG_GLOBAL_BARRIER, which just triggers ProcSignalBarrierPending to be set. And then have ProcessProcSignalBarrier do the check that's currently in CheckProcSignalBarrier()? Greetings, Andres Freund
Commits
-
Convert SpinLock* macros to static inline functions.
- bfc321b4723e 19 (unreleased) landed
-
Clean up includes of s_lock.h.
- f219167910ad 14.0 landed
-
Fix deadlock danger when atomic ops are done under spinlock.
- 5fffa8fce37b 13.0 landed
- 6cc2866c4cdf 12.4 landed
- 825d89dda5ad 11.9 landed
- c2a84bee1228 10.14 landed
- 8bc13287ed3e 9.6.19 landed
- 3e69bf3b142b 9.5.23 landed
- cf1234a10e50 14.0 landed
-
Add basic spinlock tests to regression tests.
- 3f66e1c5a576 10.14 landed
- e027219f2131 11.9 landed
- 008c119928a3 12.4 landed
- 59225dcefef2 13.0 landed
- 3b37a6de027c 14.0 landed
- e8302f107af9 9.6.19 landed
- 7e91f90a8ed4 9.5.23 landed
-
spinlock emulation: Fix bug when more than INT_MAX spinlocks are initialized.
- dcf3cb60cbef 9.5.23 landed
- 660622053419 9.6.19 landed
- b295f666b7f0 10.14 landed
- 34d29222a0bb 11.9 landed
- b91cfaa34f44 12.4 landed
- 276bdc93924a 13.0 landed
- 4d4ca24efe8e 14.0 landed
-
Avoid potential spinlock in a signal handler as part of global barriers.
- 09bff91b316e 13.0 landed
- fd49d5380757 14.0 landed
-
Make pg_stat_wal_receiver consistent with the WAL receiver's shmem info
- 2c8dd05d6cbc 13.0 cited
-
Improve 64bit atomics support.
- e8fdbd58fe56 10.0 cited