greenfly lwlock corruption in REL_14_STABLE and REL_15_STABLE

Поиск
Список
Период
Сортировка
Искать

greenfly lwlock corruption in REL_14_STABLE and REL_15_STABLE

От:
Thomas Munro <thomas.munro@gmail.com>
Дата:
Beginning a week ago, greenfly (RISC-V, Clang 20.1) has failed like
this in 5 of 8 runs of the pgbench tests on the two oldest branches:

TRAP: FailedAssertion("!(oldstate & LW_VAL_EXCLUSIVE)", File:
"lwlock.c", Line: 1850, PID: 1536294)
postgres: main: gburd postgres [local] CREATE
TYPE(ExceptionalCondition+0x72)[0x2ad1326922]
postgres: main: gburd postgres [local] CREATE
TYPE(LWLockRelease+0x51e)[0x2ad1634e60]
postgres: main: gburd postgres [local] CREATE
TYPE(_bt_first+0x7f8)[0x2ad139c314]
postgres: main: gburd postgres [local] CREATE
TYPE(btgettuple+0xca)[0x2ad13996f8]
postgres: main: gburd postgres [local] CREATE
TYPE(index_getnext_tid+0x2a)[0x2ad138bd66]
postgres: main: gburd postgres [local] CREATE
TYPE(index_getnext_slot+0x24)[0x2ad138bf56]
postgres: main: gburd postgres [local] CREATE
TYPE(systable_getnext+0x18)[0x2ad138a97c]
postgres: main: gburd postgres [local] CREATE
TYPE(GetNewOidWithIndex+0xfc)[0x2ad13ed284]
postgres: main: gburd postgres [local] CREATE
TYPE(EnumValuesCreate+0x58)[0x2ad14090ec]
postgres: main: gburd postgres [local] CREATE
TYPE(DefineEnum+0x10a)[0x2ad14bb948]
postgres: main: gburd postgres [local] CREATE TYPE(+0x3f0336)[0x2ad164a336]
postgres: main: gburd postgres [local] CREATE
TYPE(standard_ProcessUtility+0x468)[0x2ad1649560]
postgres: main: gburd postgres [local] CREATE TYPE(+0x3eec0e)[0x2ad1648c0e]
postgres: main: gburd postgres [local] CREATE TYPE(+0x3ee418)[0x2ad1648418]
postgres: main: gburd postgres [local] CREATE
TYPE(PortalRun+0x160)[0x2ad1647ec8]
postgres: main: gburd postgres [local] CREATE
TYPE(PostgresMain+0x1b34)[0x2ad1646000]
postgres: main: gburd postgres [local] CREATE TYPE(+0x36205a)[0x2ad15bc05a]
postgres: main: gburd postgres [local] CREATE
TYPE(ClosePostmasterPorts+0x0)[0x2ad15bb8e0]
postgres: main: gburd postgres [local] CREATE
TYPE(PostmasterMain+0x100a)[0x2ad15b92ac]
postgres: main: gburd postgres [local] CREATE TYPE(+0x2cac90)[0x2ad1524c90]
/lib/riscv64-linux-gnu/libc.so.6(+0x277cc)[0x3f9caa77cc]
/lib/riscv64-linux-gnu/libc.so.6(__libc_start_main+0x78)[0x3f9caa7878]
postgres: main: gburd postgres [local] CREATE TYPE(_start+0x20)[0x2ad1326ac0]

That's:

    if (mode == LW_EXCLUSIVE)
        oldstate = pg_atomic_sub_fetch_u32(&lock->state, LW_VAL_EXCLUSIVE);
    else
        oldstate = pg_atomic_sub_fetch_u32(&lock->state, LW_VAL_SHARED);

    /* nobody else can have that kind of lock */
    Assert(!(oldstate & LW_VAL_EXCLUSIVE));

I will see if I can reproduce it or see something wrong under qemu,
but that'll take some time to set up...

Since the RISC-V GCC animals aren't showing any problem, I wondered if
this could be related to commits d8ba910b, 1c7cba4, but that was ~30
days ago, applied to all branches and prevented reordering of
non-atomic loads, while here I assume we have __sync_fetch_and_sub()
without a connection to other memory as far as I can see immediately.
Commits 332693e7, da39714 touched lwlock.c ~15 days ago, but not in a
way that immediately seems relevant; if there were a relevant flag
protocol difference in these branches, then why only this system?  It
also passed half a dozen times before the cluster of failures.  That
seems to point back towards codegen problems, but perhaps of a
different kind.  Unless something else is going really wrong, but it's
hard to imagine that we forgot which lock type we held...

    date    |    branch     |             commit              | assert_failed
------------+---------------+---------------------------------+---------------
 2025-12-09 | REL_15_STABLE | f188bc5 doc: Fix statement a... |
 2025-12-09 | REL_14_STABLE | 4c4fa53 doc: Fix statement a... | t
 2025-12-09 | REL_15_STABLE | 52a9588 Doc: fix typo in has... | t
 2025-12-05 | REL_15_STABLE | b9a02b9 Fix setting next mul... |
 2025-12-05 | REL_14_STABLE | 4896955 Fix setting next mul... |
 2025-12-05 | REL_15_STABLE | 7e54eac Show version of node... | t
 2025-12-03 | REL_15_STABLE | 8cfb174 Set next multixid's ... | t
 2025-12-03 | REL_14_STABLE | 81416e1 Set next multixid's ... | t
 2025-12-02 | REL_15_STABLE | 7792bdc Fix amcheck's handli... |
 2025-12-02 | REL_14_STABLE | fbb4b60 Fix amcheck's handli... |
 2025-11-29 | REL_15_STABLE | 134a8ee Avoid rewriting data... |
 2025-11-29 | REL_14_STABLE | 2d5b97b Avoid rewriting data... |
 2025-11-27 | REL_15_STABLE | f19502f Allow indexscans on ... |
 2025-11-27 | REL_14_STABLE | 9e77323 Allow indexscans on ... |
 2025-11-27 | REL_15_STABLE | f9f9283 doc: Fix misleading ... |
 2025-11-26 | REL_15_STABLE | eb7743e doc: Clarify passphr... |
 2025-11-26 | REL_14_STABLE | 9a26ff8 doc: Clarify passphr... |
 2025-11-25 | REL_15_STABLE | da39714 lwlock: Fix, current... |
 2025-11-25 | REL_14_STABLE | 332693e lwlock: Fix, current... |
 2025-11-24 | REL_15_STABLE | ea757e8 Fix incorrect IndexO... |
 2025-11-24 | REL_14_STABLE | ea36c2f Fix incorrect IndexO... |
 2025-11-22 | REL_15_STABLE | 5516485 jit: Adjust AArch64-... |
 2025-11-22 | REL_14_STABLE | 035a1f5 jit: Adjust AArch64-... |
 2025-11-19 | REL_15_STABLE | 7c49407 Print new OldestXID ... |
 2025-11-19 | REL_14_STABLE | 11cc0f4 Print new OldestXID ... |
 2025-11-18 | REL_15_STABLE | 9f5a58a Don't allow CTEs to ... |
 2025-11-18 | REL_14_STABLE | b853974 Don't allow CTEs to ... |
 2025-11-18 | REL_15_STABLE | 3995e4a Define PS_USE_CLOBBE... |
 2025-11-18 | REL_14_STABLE | 29a3e22 Define PS_USE_CLOBBE... |
 2025-11-17 | REL_15_STABLE | ad5cc3a Update .abi-complian... |
 2025-11-16 | REL_15_STABLE | 5d5b05c Doc: include MERGE i... |
 2025-11-14 | REL_15_STABLE | d61af52 Add note about Creat... |
 2025-11-14 | REL_14_STABLE | 4c179cc Add note about Creat... |
 2025-11-13 | REL_15_STABLE | c663152 doc: Improve descrip... |
 2025-11-13 | REL_14_STABLE | 7aa83ea doc: Improve descrip... |
 2025-11-12 | REL_15_STABLE | 21a9014 Clear 'xid' in dummy... |
 2025-11-12 | REL_14_STABLE | 84f1bf4 Clear 'xid' in dummy... |
 2025-11-12 | REL_14_STABLE | 4ef048f doc: Document effect... |
 2025-11-12 | REL_15_STABLE | 608566b doc: Document effect... |
 2025-11-12 | REL_14_STABLE | f8a0ea8 Fix range for commit... |
 2025-11-12 | REL_15_STABLE | 97cd4b6 Fix pg_upgrade aroun... |
 2025-11-12 | REL_15_STABLE | 74b26c8 doc: Fix incorrect s... |
 2025-11-11 | REL_15_STABLE | 32f3881 Stamp 15.15....         |
 2025-11-11 | REL_14_STABLE | 9ad034b Stamp 14.20....         |
 2025-11-10 | REL_15_STABLE | 70d03b5 Last-minute updates ... |
 2025-11-10 | REL_14_STABLE | ee953cd Last-minute updates ... |
 2025-11-10 | REL_15_STABLE | 9142156 libpq: Prevent some ... |
 2025-11-10 | REL_14_STABLE | e792be6 Translation updates...  |
 2025-11-09 | REL_15_STABLE | e334e80 Release notes for 18... |
 2025-11-09 | REL_14_STABLE | 06827c5 Release notes for 18... |
 2025-11-08 | REL_15_STABLE | 1c7cba4 Fix generic read and... |
 2025-11-08 | REL_14_STABLE | d8ba910 Fix generic read and... |


Re: greenfly lwlock corruption in REL_14_STABLE and REL_15_STABLE

От:
"Greg Burd" <greg@burd.me>
Дата:

On Wed, Dec 10, 2025, at 12:10 AM, Thomas Munro wrote:
> Beginning a week ago, greenfly (RISC-V, Clang 20.1) has failed like
> this in 5 of 8 runs of the pgbench tests on the two oldest branches:

Hey Thomas, raising this.  I should more closely monitor my farm animals.  As greenfly is one of them and my login name is littered in the logs (gburd) I suppose I should dive into this.

> TRAP: FailedAssertion("!(oldstate & LW_VAL_EXCLUSIVE)", File:
> "lwlock.c", Line: 1850, PID: 1536294)
> postgres: main: gburd postgres [local] CREATE
> TYPE(ExceptionalCondition+0x72)[0x2ad1326922]
> postgres: main: gburd postgres [local] CREATE
> TYPE(LWLockRelease+0x51e)[0x2ad1634e60]
> postgres: main: gburd postgres [local] CREATE
> TYPE(_bt_first+0x7f8)[0x2ad139c314]
> postgres: main: gburd postgres [local] CREATE
> TYPE(btgettuple+0xca)[0x2ad13996f8]
> postgres: main: gburd postgres [local] CREATE
> TYPE(index_getnext_tid+0x2a)[0x2ad138bd66]
> postgres: main: gburd postgres [local] CREATE
> TYPE(index_getnext_slot+0x24)[0x2ad138bf56]
> postgres: main: gburd postgres [local] CREATE
> TYPE(systable_getnext+0x18)[0x2ad138a97c]
> postgres: main: gburd postgres [local] CREATE
> TYPE(GetNewOidWithIndex+0xfc)[0x2ad13ed284]
> postgres: main: gburd postgres [local] CREATE
> TYPE(EnumValuesCreate+0x58)[0x2ad14090ec]
> postgres: main: gburd postgres [local] CREATE
> TYPE(DefineEnum+0x10a)[0x2ad14bb948]
> postgres: main: gburd postgres [local] CREATE TYPE(+0x3f0336)[0x2ad164a336]
> postgres: main: gburd postgres [local] CREATE
> TYPE(standard_ProcessUtility+0x468)[0x2ad1649560]
> postgres: main: gburd postgres [local] CREATE TYPE(+0x3eec0e)[0x2ad1648c0e]
> postgres: main: gburd postgres [local] CREATE TYPE(+0x3ee418)[0x2ad1648418]
> postgres: main: gburd postgres [local] CREATE
> TYPE(PortalRun+0x160)[0x2ad1647ec8]
> postgres: main: gburd postgres [local] CREATE
> TYPE(PostgresMain+0x1b34)[0x2ad1646000]
> postgres: main: gburd postgres [local] CREATE TYPE(+0x36205a)[0x2ad15bc05a]
> postgres: main: gburd postgres [local] CREATE
> TYPE(ClosePostmasterPorts+0x0)[0x2ad15bb8e0]
> postgres: main: gburd postgres [local] CREATE
> TYPE(PostmasterMain+0x100a)[0x2ad15b92ac]
> postgres: main: gburd postgres [local] CREATE TYPE(+0x2cac90)[0x2ad1524c90]
> /lib/riscv64-linux-gnu/libc.so.6(+0x277cc)[0x3f9caa77cc]
> /lib/riscv64-linux-gnu/libc.so.6(__libc_start_main+0x78)[0x3f9caa7878]
> postgres: main: gburd postgres [local] CREATE TYPE(_start+0x20)[0x2ad1326ac0]
>
> That's:
>
>     if (mode == LW_EXCLUSIVE)
>         oldstate = pg_atomic_sub_fetch_u32(&lock->state, LW_VAL_EXCLUSIVE);
>     else
>         oldstate = pg_atomic_sub_fetch_u32(&lock->state, LW_VAL_SHARED);
>
>     /* nobody else can have that kind of lock */
>     Assert(!(oldstate & LW_VAL_EXCLUSIVE));
>
> I will see if I can reproduce it or see something wrong under qemu,
> but that'll take some time to set up...

It'll take me far less time to reproduce than you. :)

> Since the RISC-V GCC animals aren't showing any problem, I wondered if
> this could be related to commits d8ba910b, 1c7cba4, but that was ~30
> days ago, applied to all branches and prevented reordering of
> non-atomic loads, while here I assume we have __sync_fetch_and_sub()
> without a connection to other memory as far as I can see immediately.
> Commits 332693e7, da39714 touched lwlock.c ~15 days ago, but not in a
> way that immediately seems relevant; if there were a relevant flag
> protocol difference in these branches, then why only this system?  It
> also passed half a dozen times before the cluster of failures.  That
> seems to point back towards codegen problems, but perhaps of a
> different kind.  Unless something else is going really wrong, but it's
> hard to imagine that we forgot which lock type we held...
>
>     date    |    branch     |             commit              | assert_failed
> ------------+---------------+---------------------------------+---------------
>  2025-12-09 | REL_15_STABLE | f188bc5 doc: Fix statement a... |
>  2025-12-09 | REL_14_STABLE | 4c4fa53 doc: Fix statement a... | t
>  2025-12-09 | REL_15_STABLE | 52a9588 Doc: fix typo in has... | t
>  2025-12-05 | REL_15_STABLE | b9a02b9 Fix setting next mul... |
>  2025-12-05 | REL_14_STABLE | 4896955 Fix setting next mul... |
>  2025-12-05 | REL_15_STABLE | 7e54eac Show version of node... | t
>  2025-12-03 | REL_15_STABLE | 8cfb174 Set next multixid's ... | t
>  2025-12-03 | REL_14_STABLE | 81416e1 Set next multixid's ... | t
>  2025-12-02 | REL_15_STABLE | 7792bdc Fix amcheck's handli... |
>  2025-12-02 | REL_14_STABLE | fbb4b60 Fix amcheck's handli... |
>  2025-11-29 | REL_15_STABLE | 134a8ee Avoid rewriting data... |
>  2025-11-29 | REL_14_STABLE | 2d5b97b Avoid rewriting data... |
>  2025-11-27 | REL_15_STABLE | f19502f Allow indexscans on ... |
>  2025-11-27 | REL_14_STABLE | 9e77323 Allow indexscans on ... |
>  2025-11-27 | REL_15_STABLE | f9f9283 doc: Fix misleading ... |
>  2025-11-26 | REL_15_STABLE | eb7743e doc: Clarify passphr... |
>  2025-11-26 | REL_14_STABLE | 9a26ff8 doc: Clarify passphr... |
>  2025-11-25 | REL_15_STABLE | da39714 lwlock: Fix, current... |
>  2025-11-25 | REL_14_STABLE | 332693e lwlock: Fix, current... |
>  2025-11-24 | REL_15_STABLE | ea757e8 Fix incorrect IndexO... |
>  2025-11-24 | REL_14_STABLE | ea36c2f Fix incorrect IndexO... |
>  2025-11-22 | REL_15_STABLE | 5516485 jit: Adjust AArch64-... |
>  2025-11-22 | REL_14_STABLE | 035a1f5 jit: Adjust AArch64-... |
>  2025-11-19 | REL_15_STABLE | 7c49407 Print new OldestXID ... |
>  2025-11-19 | REL_14_STABLE | 11cc0f4 Print new OldestXID ... |
>  2025-11-18 | REL_15_STABLE | 9f5a58a Don't allow CTEs to ... |
>  2025-11-18 | REL_14_STABLE | b853974 Don't allow CTEs to ... |
>  2025-11-18 | REL_15_STABLE | 3995e4a Define PS_USE_CLOBBE... |
>  2025-11-18 | REL_14_STABLE | 29a3e22 Define PS_USE_CLOBBE... |
>  2025-11-17 | REL_15_STABLE | ad5cc3a Update .abi-complian... |
>  2025-11-16 | REL_15_STABLE | 5d5b05c Doc: include MERGE i... |
>  2025-11-14 | REL_15_STABLE | d61af52 Add note about Creat... |
>  2025-11-14 | REL_14_STABLE | 4c179cc Add note about Creat... |
>  2025-11-13 | REL_15_STABLE | c663152 doc: Improve descrip... |
>  2025-11-13 | REL_14_STABLE | 7aa83ea doc: Improve descrip... |
>  2025-11-12 | REL_15_STABLE | 21a9014 Clear 'xid' in dummy... |
>  2025-11-12 | REL_14_STABLE | 84f1bf4 Clear 'xid' in dummy... |
>  2025-11-12 | REL_14_STABLE | 4ef048f doc: Document effect... |
>  2025-11-12 | REL_15_STABLE | 608566b doc: Document effect... |
>  2025-11-12 | REL_14_STABLE | f8a0ea8 Fix range for commit... |
>  2025-11-12 | REL_15_STABLE | 97cd4b6 Fix pg_upgrade aroun... |
>  2025-11-12 | REL_15_STABLE | 74b26c8 doc: Fix incorrect s... |
>  2025-11-11 | REL_15_STABLE | 32f3881 Stamp 15.15....         |
>  2025-11-11 | REL_14_STABLE | 9ad034b Stamp 14.20....         |
>  2025-11-10 | REL_15_STABLE | 70d03b5 Last-minute updates ... |
>  2025-11-10 | REL_14_STABLE | ee953cd Last-minute updates ... |
>  2025-11-10 | REL_15_STABLE | 9142156 libpq: Prevent some ... |
>  2025-11-10 | REL_14_STABLE | e792be6 Translation updates...  |
>  2025-11-09 | REL_15_STABLE | e334e80 Release notes for 18... |
>  2025-11-09 | REL_14_STABLE | 06827c5 Release notes for 18... |
>  2025-11-08 | REL_15_STABLE | 1c7cba4 Fix generic read and... |
>  2025-11-08 | REL_14_STABLE | d8ba910 Fix generic read and... |

I'll see what I can do to find the offending commit(s).

best.

-greg


Re: greenfly lwlock corruption in REL_14_STABLE and REL_15_STABLE

От:
Thomas Munro <thomas.munro@gmail.com>
Дата:
On Fri, Dec 12, 2025 at 6:27 AM Greg Burd  wrote:
> On Wed, Dec 10, 2025, at 12:10 AM, Thomas Munro wrote:
> > Beginning a week ago, greenfly (RISC-V, Clang 20.1) has failed like
> > this in 5 of 8 runs of the pgbench tests on the two oldest branches:
>
> Hey Thomas, raising this.  I should more closely monitor my farm animals.  As greenfly is one of them and my login name is littered in the logs (gburd) I suppose I should dive into this.

Thanks!  Since then each branch had a non-failing run, so the new
failure stats are 5 out of 10.

> > I will see if I can reproduce it or see something wrong under qemu,
> > but that'll take some time to set up...
>
> It'll take me far less time to reproduce than you. :)

Yeah.  I have given up, after a couple of days of grinding through
REL_14_STABLE's src/bin/pgbench tests under qemu, with the same
optimisation level and the clang-20 package from Debian sid +
experimental.  I've learned to use an ARM host when I want to weak
memory order effects of other RISC-ish architectures via qemu JIT
translation (well obviously not the *same* weak memory behavior but
not none at all), so I would have tried that next if you weren't
offering to use the real hardware...

It's a bit confusing that the C does "oldstate =
pg_atomic_sub_fetch_u32()" (that's the convenience wrapper that
computes and returns the *new* value), but that also makes sense for
how it's used in the following assertion.

Assuming the state was already corrupted before we got here, I wonder
if you'd learn something by asserting the expected state before the
change:

if (mode == LW_EXCLUSIVE)
    Assert(pg_atomic_read_u32(&lock->state) & LW_VAL_EXCLUSIVE);
else
    Assert(pg_atomic_read_u32(&lock->state) & LW_SHARED_MASK);

(Since we acquired the lock, a relaxed load should be guaranteed to
see a new enough value?)

While trying to imagine how it might have become corrupted and noting
that the recent change related to a flag that has some synchronisation
requirement with the non-atomic accesses to ->lwWaitXXX members
declared in proc.h, I wondered if that atomic-vs-non-atomic fencing
and reordering stuff might be involved, ie the waitlist spinlock is
acquired and then we load MyProc->lwWaiting in LWLockDequeueSelf, and
then perhaps corrupt LW_FLAG_HAS_WAITERS, or some other place like
that, but I havent' found that in the code gen yet and I don't have an
actual theory...  If you can reproduce this, does anything change if
you mark them volatile?

Can you please attach the animal's lwlock.o from REL_14_STABLE?  I'd
like to check if I'm even looking at the right code...

> I'll see what I can do to find the offending commit(s).

Thanks!  Could also be environmental.  Maybe the snow arrived and you
adjusted your thermostat? :-)  But even then we'd still need to figure
out what's different on those branches...  One difference is autoconf
vs meson.  Both say -g -O2, but only Meson says -fPIC.  Can you
reproduce it on later branches with configure/make?


Re: greenfly lwlock corruption in REL_14_STABLE and REL_15_STABLE

От:
Alexander Lakhin <exclusion@gmail.com>
Дата:
Вложения:
Hello hackers,

12.12.2025 02:54, Thomas Munro wrote:
> Thanks!  Could also be environmental.  Maybe the snow arrived and you
> adjusted your thermostat? :-)  But even then we'd still need to figure
> out what's different on those branches...  One difference is autoconf
> vs meson.  Both say -g -O2, but only Meson says -fPIC.  Can you
> reproduce it on later branches with configure/make?

I'd like to revive this thread, while we have no snow in EU yet. We had
some off-list discussion of this topic at the end of the past year, but
didn't root-cause the issue then. But this week I have finally dedicated
some time to dive into this with my Orange Pi device, which is able to
reproduce those crashes more or less as often as greenfly.

I came to the following modification:
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -4250,3 +4250,10 @@ LockBuffer(Buffer buffer, int mode)
         else if (mode == BUFFER_LOCK_EXCLUSIVE)
+{
+for (int i = 0; i < 100; i++)
+{
+LWLockAcquire(BufferDescriptorGetContentLock(buf), LW_EXCLUSIVE);
+LWLockRelease(BufferDescriptorGetContentLock(buf));
+}
                 LWLockAcquire(BufferDescriptorGetContentLock(buf), LW_EXCLUSIVE);
+}
         else

which makes a build produced on REL_15_STABLE with:
CC=clang-20 CFLAGS="-O1" ./configure -q --enable-debug --enable-cassert --enable-tap-tests && make -s -j8

fail for me reliably (0 of 10 runs succeeded) as below:
PROVE_TESTS="t/001*" make -s check -C src/bin/pgbench
...
Files=1, Tests=3,  9 wallclock secs ( 0.11 usr  0.03 sys +  4.83 cusr  1.02 csys =  5.99 CPU)
Result: FAIL

TRAP: FailedAssertion("!(oldstate & LW_VAL_EXCLUSIVE)", File: "lwlock.c", Line: 1843, PID: 64342)
postgres: main: orangepi postgres [local] CREATE TYPE(ExceptionalCondition+0x72)[0x2ab2066546]
postgres: main: orangepi postgres [local] CREATE TYPE(LWLockRelease+0x58e)[0x2ab2385160]
postgres: main: orangepi postgres [local] CREATE TYPE(_bt_relbuf+0x14)[0x2ab20d5060]
postgres: main: orangepi postgres [local] CREATE TYPE(+0x13bc2a)[0x2ab20d1c2a]
...
(other observed result is a hang, once per ~10 runs)

Please find attached the assembler code for the LWLockWakeup() function
that is known to produce the assertion failure and note two instructions
I commented there:
   3eee4a:       lw a3,4(s6)                                        # 1014: old_state = pg_atomic_read_u32(&lock->state)
   3eee4e:       lw a5,8(s6)                                        # 1026: proclist_is_empty(&lock->waiters)

The following minimal modification:
--- a/src/backend/storage/lmgr/lwlock.c
+++ b/src/backend/storage/lmgr/lwlock.c
@@ -1016,2 +1016,3 @@ LWLockWakeup(LWLock *lock)
                 {
+__asm__ __volatile__("nop" ::: "memory");
                         desired_state = old_state;

which turns that pair into:
   3eee4a:       lw      a3,4(s6)
   3eee4e:       nop
   3eee50:       lw      a5,8(s6)

makes the test pass reliably for me (10+ iterations).

So it's not surprising now that this crash depends on postgres version
(v14/v15 only), compiler (gcc doesn't produce the code needed), its'
options (not reproduced with -O0), etc.

I've generated a minimal standalone reproducer for this anomaly (please
find it attached too), which replicates an essential part of
LWLockWakeup():
       sb      pending_wakeup, (waiting)
       lw      old_state, 4(lock)
       lw      head, 8(lock)
       and     desired_state, old_state, mask
   1:  lr.w    cur, (state)
       bne     cur, old_state, 2f
       sc.w    tmp, desired_state, (state)
       bnez    tmp, 1b
   2:

and demonstrates that LR/SC mechanism doesn't work as expected at least on
the given RISC-V CPU (8 SpacemiT X60 cores in 2 clusters):
Machine model: ky x1 orangepi-rv2 board
...
processor       : 7
hart            : 7
model name      : Ky(R) X1
isa             : 
rv64imafdcv_zicbom_zicboz_zicntr_zicond_zicsr_zifencei_zihintpause_zihpm_zfh_zfhmin_zca_zcd_zba_zbb_zbc_zbs_zkt_zve32f_zve32x_zve64d_zve64f_zve64x_zvfh_zvfhmin_zvkt_sscofpmf_sstc_svinval_svnapot_svpbmt
mmu             : sv39
uarch           : ky,x60
mvendorid       : 0x710
marchid         : 0x8000000058000001
mimpid          : 0x1000000049772200

Let me share results of a few experiments:
clang-20 -O1 ~/lrsc-anomaly.c -o ~/lrsc-anomaly
~/lrsc-anomaly
LOST UPDATE in round 28: value=0x70001d01 expected=0x00001d01
LOST UPDATE in round 29: value=0x70001e01 expected=0x00001e01
LOST UPDATE in round 30: value=0x70001f01 expected=0x00001f01
LOST UPDATE in round 31: value=0x70002001 expected=0x00002001
LOST UPDATE in round 32: value=0x70002101 expected=0x00002101
LOST UPDATE in round 33: value=0x70002201 expected=0x00002201
LOST UPDATE in round 34: value=0x70002301 expected=0x00002301
LOST UPDATE in round 35: value=0x70002401 expected=0x00002401
LOST UPDATE in round 37: value=0x70002601 expected=0x00002601
LOST UPDATE in round 38: value=0x70002701 expected=0x00002701
page_offset=0 rounds=1000000 retried=1824 lost=15523: FAILED

It reproduces the issue: process A's SC succeeds despite B's increment
happened in between.

for n in `seq 0 15`; do ~/lrsc-anomaly -n $n | tail -1; done
page_offset=0 rounds=1000000 retried=5374 lost=16160: FAILED
page_offset=1 rounds=1000000 retried=29563 lost=16438: FAILED
page_offset=2 rounds=1000000 retried=1251 lost=17209: FAILED
page_offset=3 rounds=1000000 retried=14960 lost=22: FAILED
page_offset=4 rounds=1000000 retried=12090 lost=19: FAILED
page_offset=5 rounds=1000000 retried=13561 lost=15: FAILED
page_offset=6 rounds=1000000 retried=15037 lost=32: FAILED
page_offset=7 rounds=1000000 retried=3124 lost=16774: FAILED
page_offset=8 rounds=1000000 retried=19813 lost=0: OK
page_offset=9 rounds=1000000 retried=20210 lost=0: OK
page_offset=10 rounds=1000000 retried=17992 lost=0: OK
page_offset=11 rounds=1000000 retried=18207 lost=0: OK
page_offset=12 rounds=1000000 retried=17903 lost=0: OK
page_offset=13 rounds=1000000 retried=18779 lost=0: OK
page_offset=14 rounds=1000000 retried=18384 lost=0: OK
page_offset=15 rounds=1000000 retried=18328 lost=0: OK

That is, the effect depends not only on that lw pair, but also on offset
of "waiting" in "sb pending_wakeup, (waiting)" preceding it.

The anomaly's probability also depends on whether these two cores/harts
are in the same cluster or not. For example, with cores 0 and 1 that belong
to cluster 0:
for n in `seq 0 4 15`; do taskset -c 0,1 ~/lrsc-anomaly -n $n | tail -1; done
page_offset=0 rounds=1000000 retried=1194 lost=16909: FAILED
page_offset=4 rounds=1000000 retried=1147 lost=16464: FAILED
page_offset=8 rounds=1000000 retried=18658 lost=0: OK
page_offset=12 rounds=1000000 retried=18154 lost=0: OK

And now with two cores that belong to different clusters:
for n in `seq 0 4 15`; do taskset -c 0,4 ~/lrsc-anomaly -n $n | tail -1; done
page_offset=0 rounds=1000000 retried=13350 lost=45: FAILED
page_offset=4 rounds=1000000 retried=15566 lost=37: FAILED
page_offset=8 rounds=1000000 retried=18223 lost=0: OK
page_offset=12 rounds=1000000 retried=17466 lost=0: OK

Also, nop inserted as above:
                 /* old_state = pg_atomic_read_u32(&lock->state) */
                 "       lw              %[old_state], 4(%[lock])\n"
+"nop\n"
                 /* proclist_is_empty(&lock->waiters) */
                 "       lw              %[head], 8(%[lock])\n"

changes the picture:
for n in `seq 0 15`; do taskset -c 0,1 ~/lrsc-anomaly -n $n | tail -1; done
page_offset=0 rounds=1000000 retried=15306 lost=17557: FAILED
page_offset=1 rounds=1000000 retried=2624 lost=16711: FAILED
page_offset=2 rounds=1000000 retried=1468 lost=16940: FAILED
page_offset=3 rounds=1000000 retried=815 lost=17098: FAILED
page_offset=4 rounds=1000000 retried=31171 lost=0: OK
page_offset=5 rounds=1000000 retried=17372 lost=0: OK
page_offset=6 rounds=1000000 retried=31803 lost=0: OK
page_offset=7 rounds=1000000 retried=19194 lost=0: OK
page_offset=8 rounds=1000000 retried=5777 lost=0: OK
page_offset=9 rounds=1000000 retried=5375 lost=0: OK
page_offset=10 rounds=1000000 retried=11920 lost=0: OK
page_offset=11 rounds=1000000 retried=5334 lost=0: OK
page_offset=12 rounds=1000000 retried=5087 lost=0: OK
page_offset=13 rounds=1000000 retried=12040 lost=0: OK
page_offset=14 rounds=1000000 retried=8479 lost=0: OK
page_offset=15 rounds=1000000 retried=4965 lost=0: OK

It kind of confirms the behavior observed with Postgres, when nop shifted
the code out of the problem area.

It also depends on number of distinct lines (buffers) in rotation; with
#define NBUFFERS        2

the probability drops almost to zero:
taskset -c 0,1 ~/lrsc-anomaly
LOST UPDATE in round 300830: value=0x7000ba01 expected=0x0000ba01
LOST UPDATE in round 300831: value=0x7000bb01 expected=0x0000bb01
page_offset=0 rounds=1000000 retried=2 lost=2: FAILED

Sorry for the long scroll, but it looks like this anomaly should be added
to our collection of the outer world's defects discovered during Postgres
testing.

Best regards,
Alexander
FAQ