Re: Non-reproducible AIO failure

Konstantin Knizhnik <knizhnik@garret.ru>

From: Konstantin Knizhnik <knizhnik@garret.ru>
To: pgsql-hackers@lists.postgresql.org
Date: 2025-06-06T11:03:12Z
Lists: pgsql-hackers

Commits

Same data as JSON: GET /api/v1/messages/:b64id/commits the thread's linked commits as JSON, with link sources. API reference →
  1. aio: Stop using enum bitfields due to bad code generation

  2. amcheck: Fix posting tree checks in gin_index_check()

  3. aio: Add missing memory barrier when waiting for IO handle

There is really essential difference in code generated by clang 15 
(working) and 16 (not working).

```

pgaio_io_stage(PgAioHandle *ioh, PgAioOp op)

{

      ...

      HOLD_INTERRUPTS();

     ioh->op = op;
     ioh->result = 0;

     pgaio_io_update_state(ioh, PGAIO_HS_DEFINED);
     ...

}

```

clang15:

```

postgres[0x10035d4ac] <+84>:  add    x22, x22, #0x5d4          ; 
InterruptHoldoffCount
postgres[0x10035d4b0] <+88>:  ldr    w8, [x22]
postgres[0x10035d4b4] <+92>:  add    w8, w8, #0x1
postgres[0x10035d4b8] <+96>:  str    w8, [x22]
*postgres[0x10035d4bc] <+100>: strb   w20, [x19, #0x2]
*postgres[0x10035d4c0] <+104>: str    wzr, [x19, #0x14]
postgres[0x10035d4c4] <+108>: mov    x0, x19
postgres[0x10035d4c8] <+112>: mov    w1, #0x2
postgres[0x10035d4cc] <+116>: bl 0x10035cbdc               ; 
pgaio_io_update_state at aio.c:384
```

clang16:

```

postgres[0x100699e8c] <+200>: add    x9, x9, #0xd74 ; InterruptHoldoffCount
postgres[0x100699e90] <+204>: ldr    w8, [x9]
postgres[0x100699e94] <+208>: mov    w10, #0x1 ; =1
postgres[0x100699e98] <+212>: stur   w10, [x29, #-0x18]
postgres[0x100699e9c] <+216>: add    w8, w8, #0x1
postgres[0x100699ea0] <+220>: str    w8, [x9]
postgres[0x100699ea4] <+224>: ldur   w10, [x29, #-0xc]
postgres[0x100699ea8] <+228>: ldur   x9, [x29, #-0x8]
postgres[0x100699eac] <+232>: ldrh   w8, [x9]
postgres[0x100699eb0] <+236>: and    w10, w10, #0xff
postgres[0x100699eb4] <+240>: orr    w10, w8, w10, lsl #16
postgres[0x100699eb8] <+244>: lsr    w8, w10, #16
*postgres[0x100699ebc] <+248>: strh   w10, [x9]
postgres[0x100699ec0] <+252>: strb   w8, [x9, #0x2]
*postgres[0x100699ec4] <+256>: ldur   x8, [x29, #-0x8]
postgres[0x100699ec8] <+260>: str    wzr, [x8, #0x14]
postgres[0x100699ecc] <+264>: ldur   x0, [x29, #-0x8]
postgres[0x100699ed0] <+268>: mov    w1, #0x2 ; =2
postgres[0x100699ed4] <+272>: bl     0x100698efc    ; 
pgaio_io_update_state at aio.c:384
```

As you can see pg16 just updates third byte of the structure (`op` 
field). It is not affecting 'state' field at all.

While clang16 is also updating `state` field! And looks like it is moved 
before memory barrier (in `pgaio_io_update_state` function),