pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
BUG #19796: io_uring crashes with specific kernel/liburing versions + io_max_concurrency due to rings overlap
5+ messages / 3 participants
[nested] [flat]

* BUG #19796: io_uring crashes with specific kernel/liburing versions + io_max_concurrency due to rings overlap
@ 2026-10-25 07:00 PG Bug reporting form <noreply@postgresql.org>
  2026-10-26 08:27 ` Re: BUG #19796: io_uring crashes with specific kernel/liburing versions + io_max_concurrency due to rings overlap Andrey Rachitskiy <pl0h0yp1@gmail.com>
  2026-10-26 13:31 ` Re: BUG #19796: io_uring crashes with specific kernel/liburing versions + io_max_concurrency due to rings overlap Andres Freund <andres@anarazel.de>
  0 siblings, 2 replies; 5+ messages in thread

From: PG Bug reporting form @ 2026-10-25 07:00 UTC (permalink / raw)
  To: pgsql-bugs@lists.postgresql.org; +Cc: exclusion@gmail.com

The following bug has been logged on the website:

Bug reference:      19796
Logged by:          Alexander Lakhin
Email address:      exclusion@gmail.com
PostgreSQL version: 19rc1
Operating system:   Ubuntu 24.04
Description:        

The following script:
psql -c "CREATE TABLE t AS SELECT repeat('x', 100) FROM generate_series(1,
500000)"

echo "SELECT count(*) FROM t" > /tmp/q.sql
pgbench -n -f /tmp/q.sql -c 4 -T 60

with these parameters set:
io_method = io_uring
io_max_concurrency = 1024
shared_buffers = 16MB
max_connections = 30

crashes on Ubuntu 24.04 as below:
2026-10-25 06:03:22.401 UTC [39572] PANIC:  io_uring submit submitted only 0
of 1
2026-10-25 06:03:22.401 UTC [39572] STATEMENT:  SELECT count(*) FROM t
2026-10-25 06:03:22.401 UTC [39523] ERROR:  io_uring submit submitted only 0
of 1
2026-10-25 06:03:22.401 UTC [39523] CONTEXT:  parallel worker

or
TRAP: failed Assert("ioh->state == PGAIO_HS_SUBMITTED"), File: "aio.c",
Line: 530, PID: 39816
ExceptionalCondition at assert.c:51:13
pgaio_io_process_completion at aio.c:532:2
pgaio_uring_drain_locked at method_io_uring.c:565:20
pgaio_uring_wait_one at method_io_uring.c:643:4
pgaio_io_wait at aio.c:623:6
pgaio_wref_wait at aio.c:999:1
WaitReadBuffers at bufmgr.c:1831:17
read_stream_next_buffer at read_stream.c:1191:17
heap_fetch_next_buffer at heapam.c:743:16
heapgettup_pagemode at heapam.c:1110:8
heap_getnextslot at heapam.c:1484:3
table_scan_getnextslot at tableam.h:1105:1
SeqNext at nodeSeqscan.c:90:5
...

With this logging added:
inline patch 1: src/backend/storage/aio/method_io_uring.c (+0/-0 lines) download inline-1.patch--- a/src/backend/storage/aio/method_io_uring.c +++ b/src/backend/storage/aio/method_io_uring.c @@ -357,6 +357,12 @@ pgaio_uring_shmem_init(void *arg) struct io_uring_params p = {0}; ret = io_uring_queue_init_mem(io_max_concurrency,
&context->io_uring_ring, &p, ring_mem_next, ring_mem_remain); +if (ret > 0) +{ + int overlap = (char *) (context->io_uring_ring.sq.array + p.sq_entries) - (ring_mem_next + ret); + if (overlap > 0) + elog(LOG, "!!!pgaio_uring_shmem_init| io_uring ring %d overlaps the next ring by %d bytes", contextno, overlap); +} ring_mem_remain -= ret; ring_mem_next += ret; I can see: 2026-10-25 06:04:51.347 UTC [39804] LOG: !!!pgaio_uring_shmem_init| io_uring ring 0 overlaps the next ring by 64 bytes 2026-10-25 06:04:51.347 UTC [39804] LOG: !!!pgaio_uring_shmem_init| io_uring ring 1 overlaps the next ring by 64 bytes ... 2026-10-25 06:04:51.352 UTC [39804] LOG: !!!pgaio_uring_shmem_init| io_uring ring 70 overlaps the next ring by 64 bytes 2026-10-25 06:04:51.352 UTC [39804] LOG: !!!pgaio_uring_shmem_init| io_uring ring 71 overlaps the next ring by 64 bytes 2026-10-25 06:04:51.354 UTC [39804] LOG: starting PostgreSQL 20devel on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 13.3.0-6ubuntu2~24.04.1) 13.3.0, 64-bit I observed this on Ubuntu machines with kernels: Linux a 7.0.0-1014-aws #14~24.04.1-Ubuntu SMP PREEMPT Fri Sep 18 21:50:22 UTC 2026 aarch64 aarch64 aarch64 GNU/Linux Linux b 6.14.1-061401-generic #202504071048 SMP PREEMPT_DYNAMIC Mon Apr 7 11:20:41 UTC 2025 x86_64 x86_64 x86_64 GNU/Linux and liburing 2.5. The issue is not reproduced after https://github.com/axboe/liburing/commit/9f4464d. ^ permalink raw reply [nested|flat] 5+ messages in thread

* Re: BUG #19796: io_uring crashes with specific kernel/liburing versions + io_max_concurrency due to rings overlap
  2026-10-25 07:00 BUG #19796: io_uring crashes with specific kernel/liburing versions + io_max_concurrency due to rings overlap PG Bug reporting form <noreply@postgresql.org>
@ 2026-10-26 08:27 ` Andrey Rachitskiy <pl0h0yp1@gmail.com>
  2026-10-26 13:27   ` Re: BUG #19796: io_uring crashes with specific kernel/liburing versions + io_max_concurrency due to rings overlap Andres Freund <andres@anarazel.de>
  1 sibling, 1 reply; 5+ messages in thread

From: Andrey Rachitskiy @ 2026-10-26 08:27 UTC (permalink / raw)
  To: exclusion@gmail.com; pgsql-bugs@lists.postgresql.org; +Cc: Andres Freund <andres@anarazel.de>

вс, 25 окт. 2026 г. в 18:34, PG Bug reporting form <noreply@postgresql.org>:

> The following bug has been logged on the website:
>
> Bug reference:      19796
> Logged by:          Alexander Lakhin
> Email address:      exclusion@gmail.com
> PostgreSQL version: 19rc1
> Operating system:   Ubuntu 24.04
> Description:
>
> The following script:
> psql -c "CREATE TABLE t AS SELECT repeat('x', 100) FROM generate_series(1,
> 500000)"
>
> echo "SELECT count(*) FROM t" > /tmp/q.sql
> pgbench -n -f /tmp/q.sql -c 4 -T 60
>
> with these parameters set:
> io_method = io_uring
> io_max_concurrency = 1024
> shared_buffers = 16MB
> max_connections = 30
>
> crashes on Ubuntu 24.04 as below:
> 2026-10-25 06:03:22.401 UTC [39572] PANIC:  io_uring submit submitted only
> 0
> of 1
> 2026-10-25 06:03:22.401 UTC [39572] STATEMENT:  SELECT count(*) FROM t
> 2026-10-25 06:03:22.401 UTC [39523] ERROR:  io_uring submit submitted only
> 0
> of 1
> 2026-10-25 06:03:22.401 UTC [39523] CONTEXT:  parallel worker
>
> or
> TRAP: failed Assert("ioh->state == PGAIO_HS_SUBMITTED"), File: "aio.c",
> Line: 530, PID: 39816
> ExceptionalCondition at assert.c:51:13
> pgaio_io_process_completion at aio.c:532:2
> pgaio_uring_drain_locked at method_io_uring.c:565:20
> pgaio_uring_wait_one at method_io_uring.c:643:4
> pgaio_io_wait at aio.c:623:6
> pgaio_wref_wait at aio.c:999:1
> WaitReadBuffers at bufmgr.c:1831:17
> read_stream_next_buffer at read_stream.c:1191:17
> heap_fetch_next_buffer at heapam.c:743:16
> heapgettup_pagemode at heapam.c:1110:8
> heap_getnextslot at heapam.c:1484:3
> table_scan_getnextslot at tableam.h:1105:1
> SeqNext at nodeSeqscan.c:90:5
> ...
>
> With this logging added:
> --- a/src/backend/storage/aio/method_io_uring.c
> +++ b/src/backend/storage/aio/method_io_uring.c
> @@ -357,6 +357,12 @@ pgaio_uring_shmem_init(void *arg)
>                         struct io_uring_params p = {0};
>
>                         ret = io_uring_queue_init_mem(io_max_concurrency,
> &context->io_uring_ring, &p, ring_mem_next, ring_mem_remain);
> +if (ret > 0)
> +{
> +       int overlap = (char *) (context->io_uring_ring.sq.array +
> p.sq_entries) - (ring_mem_next + ret);
> +       if (overlap > 0)
> +               elog(LOG, "!!!pgaio_uring_shmem_init| io_uring ring %d
> overlaps the next ring by %d bytes", contextno, overlap);
> +}
>
>                         ring_mem_remain -= ret;
>                         ring_mem_next += ret;
>
> I can see:
> 2026-10-25 06:04:51.347 UTC [39804] LOG:  !!!pgaio_uring_shmem_init|
> io_uring ring 0 overlaps the next ring by 64 bytes
> 2026-10-25 06:04:51.347 UTC [39804] LOG:  !!!pgaio_uring_shmem_init|
> io_uring ring 1 overlaps the next ring by 64 bytes
> ...
> 2026-10-25 06:04:51.352 UTC [39804] LOG:  !!!pgaio_uring_shmem_init|
> io_uring ring 70 overlaps the next ring by 64 bytes
> 2026-10-25 06:04:51.352 UTC [39804] LOG:  !!!pgaio_uring_shmem_init|
> io_uring ring 71 overlaps the next ring by 64 bytes
> 2026-10-25 06:04:51.354 UTC [39804] LOG:  starting PostgreSQL 20devel on
> x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 13.3.0-6ubuntu2~24.04.1)
> 13.3.0, 64-bit
>
> I observed this on Ubuntu machines with kernels:
> Linux a 7.0.0-1014-aws #14~24.04.1-Ubuntu SMP PREEMPT Fri Sep 18 21:50:22
> UTC 2026 aarch64 aarch64 aarch64 GNU/Linux
> Linux b 6.14.1-061401-generic #202504071048 SMP PREEMPT_DYNAMIC Mon Apr  7
> 11:20:41 UTC 2025 x86_64 x86_64 x86_64 GNU/Linux
> and liburing 2.5. The issue is not reproduced after
> https://github.com/axboe/liburing/commit/9f4464d.
>
>
>
>
>
Hi, Alexander!

Thanks for the report.

PostgreSQL stores one io_uring ring per backend in one shared-memory
buffer. The next ring starts at the byte count io_uring_queue_init_mem()
returns. Before liburing 2.7 that count leaves out the io_rings header. The
kernel's ring is longer, and the next ring overlaps its tail.

The real length is in the parameters the kernel fills in. cq_off.cqes is
where the CQE array starts, and that offset already includes the header.
Add the CQE array, align to the cache line, then add the SQ index array if
the kernel created one. On a 1024-entry ring the missing part is 64 bytes.
With liburing 2.5 it is the end of the SQ index array.
Liburing 2.6 has no index array, and those 64 bytes are the end of the CQE
array. Liburing 2.7 includes the header, so the returned count is already
enough.
A fixed extra 64 bytes would also move a correct liburing, and the next
ring would no longer start on a page. The kernel rejects such an address.
The patch keeps whichever is further, the computed end or liburing's count,
and rounds that up to a page. If liburing's count is already past that end,
the reservation stays as it is. Without io_uring_queue_init_mem() and
IORING_SETUP_NO_MMAP this code is not compiled. Each ring still gets its
own mapping.

Checked with io_method=io_uring and io_max_concurrency=1024. liburing 2.5
reported 102400 bytes per ring.
Unpatched, pgbench -c 4 hit both "io_uring submit submitted only 0 of 1"
and Assert(ioh->state == PGAIO_HS_SUBMITTED).
Patched, the server reserved 106496 bytes and the same run finished.
On liburing 2.7 the reservation stayed 102400 bytes and the run finished.

Alexander, please try the builds you still have. liburing 2.5, 2.6 and 2.7,
on both machines from the report.
I only ran 2.5 and 2.7 on x86_64.

Andres in CC.

-- 
Regards,
Rachitskiy Andrey

Attachments:

0001-Account-for-io_uring-memory-liburing-does-not-report.patch (text/x-patch, 6.2K) download From 2198ef0fb20a91643283993ce0724b32cb358c77 Mon Sep 17 00:00:00 2001 From: Andrey Rachitskiy <pl0h0yp1@gmail.com> Date: Mon, 26 Oct 2026 12:12:33 +0500 Subject: [PATCH] Base packed io_uring rings on the kernel layout PostgreSQL places the next io_uring ring at the size returned by io_uring_queue_init_mem(). Liburing before 2.7 does not count the io_rings header, so the ring the kernel created is longer than that size. The ring parameters still describe the real extent. cq_off.cqes is the start of the CQE array and already includes the header. The array is aligned to a cache line, and the SQ index array follows when the kernel created one. Reserve through the further of this extent and the reported size, then round up to a page. The next ring has to start on a page boundary. When liburing already covered the layout, the reservation is unchanged. Author: Andrey Rachitskiy <pl0h0yp1@gmail.com> Reported-by: Alexander Lakhin <exclusion@gmail.com> Discussion: https://www.postgresql.org/message-id/19796-ee6b17b2c500ed5e@postgresql.org --- src/backend/storage/aio/method_io_uring.c | 123 ++++++++++++++++++++-- 1 file changed, 116 insertions(+), 7 deletions(-) diff --git a/src/backend/storage/aio/method_io_uring.c b/src/backend/storage/aio/method_io_uring.c index 3ffe5061a20..4def4bf4cb2 100644 --- a/src/backend/storage/aio/method_io_uring.c +++ b/src/backend/storage/aio/method_io_uring.c @@ -47,6 +47,9 @@ /* number of completions processed at once */ #define PGAIO_MAX_LOCAL_COMPLETED_IO 32 +/* SMP cache line on x86_64 and aarch64 */ +#define PGAIO_URING_SMP_CACHE_BYTES 64 + /* Entry points for IoMethodOps. */ static void pgaio_uring_shmem_request(void *arg); @@ -138,6 +141,76 @@ pgaio_uring_procs(void) return MaxBackends + NUM_AUXILIARY_PROCS - MAX_IO_WORKERS; } +#if defined(HAVE_IO_URING_QUEUE_INIT_MEM) && defined(IORING_SETUP_NO_MMAP) +/* + * Bytes to reserve for one ring after io_uring_queue_init_mem(). + * + * reported is the size liburing returned. Liburing before 2.7 does not + * count the io_rings header, so the ring can extend past that size into + * the next one. + * + * Follow the kernel's rings_size(): the CQE array, aligned to a cache + * line, then the SQ index array when the kernel created one. cq_off.cqes + * is the start of that CQE array and already includes the header. Take + * the further of this end and reported, then round up to a page. The + * next ring has to start on a page boundary, because the kernel rejects + * a user address that does not. When liburing already covered this + * layout, reported is unchanged. + * + * Returns that size, or -1 if the ring does not fit in buf_size. + */ +static int +pgaio_uring_ring_space(const void *buf, size_t buf_size, int reported, + const struct io_uring_params *p) +{ + uintptr_t base = (uintptr_t) buf; + uintptr_t rings = (uintptr_t) p->cq_off.user_addr; + uintptr_t end; + uintptr_t region_end; + long page_size; + size_t space; + + Assert(reported > 0); + + if ((size_t) reported > buf_size || + rings < base || rings >= base + buf_size) + return -1; + + region_end = (uintptr_t) p->cq_off.cqes + + (uintptr_t) p->cq_entries * sizeof(struct io_uring_cqe); + region_end = TYPEALIGN(PGAIO_URING_SMP_CACHE_BYTES, region_end); + + /* + * IORING_SETUP_NO_SQARRAY arrived in liburing 2.6. Liburing 2.5 has + * io_uring_queue_init_mem() but not this flag, and every such ring has an + * SQ index array. Count that array unless a newer liburing created the + * ring without it. + */ +#ifdef IORING_SETUP_NO_SQARRAY + if ((p->flags & IORING_SETUP_NO_SQARRAY) == 0) +#endif + { + region_end += (uintptr_t) p->sq_entries * sizeof(uint32); + } + + end = base + (uintptr_t) reported; + if (rings + region_end > end) + { + if (rings + region_end > base + buf_size) + return -1; + + end = rings + region_end; + } + + page_size = sysconf(_SC_PAGESIZE); + space = (size_t) TYPEALIGN(page_size, end - base); + if (space > buf_size || space > (size_t) INT_MAX) + return -1; + + return (int) space; +} +#endif + /* * Initializes pgaio_uring_caps, unless that's already done. */ @@ -193,11 +266,25 @@ pgaio_uring_check_capabilities(void) ret = io_uring_queue_init_mem(io_max_concurrency, &test_ring, &p, ring_ptr, ring_size); if (ret > 0) { - pgaio_uring_caps.mem_init_size = ret; + int space; - elog(DEBUG1, - "can use combined memory mapping for io_uring, each ring needs %d bytes", - ret); + space = pgaio_uring_ring_space(ring_ptr, ring_size, ret, &p); + if (space > 0) + { + pgaio_uring_caps.mem_init_size = space; + + if (space != ret) + elog(DEBUG1, + "can use combined memory mapping for io_uring, liburing reported %d bytes, reserving %d", + ret, space); + else + elog(DEBUG1, + "can use combined memory mapping for io_uring, each ring needs %d bytes", + space); + } + else + elog(DEBUG1, + "cannot use combined memory mapping for io_uring, ring does not fit in the probe buffer"); /* clean up the created ring, it was just for a test */ io_uring_queue_exit(&test_ring); @@ -355,11 +442,33 @@ pgaio_uring_shmem_init(void *arg) if (pgaio_uring_caps.mem_init_size > 0) { struct io_uring_params p = {0}; + int space; ret = io_uring_queue_init_mem(io_max_concurrency, &context->io_uring_ring, &p, ring_mem_next, ring_mem_remain); - - ring_mem_remain -= ret; - ring_mem_next += ret; + if (ret > 0) + { + space = pgaio_uring_ring_space(ring_mem_next, ring_mem_remain, + ret, &p); + + /* + * Step by the reserved size, not by ret. ret can be short of + * the bytes the kernel shared with us. See + * pgaio_uring_ring_space(). + */ + if (space < 0 || + (size_t) pgaio_uring_caps.mem_init_size > ring_mem_remain) + ereport(ERROR, + errcode(ERRCODE_INTERNAL_ERROR), + errmsg("io_uring ring does not fit in reserved shared memory")); + else if (space > pgaio_uring_caps.mem_init_size) + ereport(ERROR, + errcode(ERRCODE_INTERNAL_ERROR), + errmsg("io_uring ring needs %d bytes, but %d were reserved", + space, pgaio_uring_caps.mem_init_size)); + + ring_mem_remain -= pgaio_uring_caps.mem_init_size; + ring_mem_next += pgaio_uring_caps.mem_init_size; + } } else #endif -- 2.53.0
^ permalink raw reply [nested|flat] 5+ messages in thread

* Re: BUG #19796: io_uring crashes with specific kernel/liburing versions + io_max_concurrency due to rings overlap
  2026-10-25 07:00 BUG #19796: io_uring crashes with specific kernel/liburing versions + io_max_concurrency due to rings overlap PG Bug reporting form <noreply@postgresql.org>
  2026-10-26 08:27 ` Re: BUG #19796: io_uring crashes with specific kernel/liburing versions + io_max_concurrency due to rings overlap Andrey Rachitskiy <pl0h0yp1@gmail.com>
@ 2026-10-26 13:27   ` Andres Freund <andres@anarazel.de>
  0 siblings, 0 replies; 5+ messages in thread

From: Andres Freund @ 2026-10-26 13:27 UTC (permalink / raw)
  To: Andrey Rachitskiy <pl0h0yp1@gmail.com>; +Cc: exclusion@gmail.com; pgsql-bugs@lists.postgresql.org

Hi,

On 2026-10-26 13:27:28 +0500, Andrey Rachitskiy wrote:
> Thanks for the report.
> 
> PostgreSQL stores one io_uring ring per backend in one shared-memory
> buffer. The next ring starts at the byte count io_uring_queue_init_mem()
> returns. Before liburing 2.7 that count leaves out the io_rings header. The
> kernel's ring is longer, and the next ring overlaps its tail.
> 
> The real length is in the parameters the kernel fills in. cq_off.cqes is
> where the CQE array starts, and that offset already includes the header.
> Add the CQE array, align to the cache line, then add the SQ index array if
> the kernel created one. On a 1024-entry ring the missing part is 64 bytes.
> With liburing 2.5 it is the end of the SQ index array.
> Liburing 2.6 has no index array, and those 64 bytes are the end of the CQE
> array. Liburing 2.7 includes the header, so the returned count is already
> enough.
> A fixed extra 64 bytes would also move a correct liburing, and the next
> ring would no longer start on a page. The kernel rejects such an address.
> The patch keeps whichever is further, the computed end or liburing's count,
> and rounds that up to a page. If liburing's count is already past that end,
> the reservation stays as it is. Without io_uring_queue_init_mem() and
> IORING_SETUP_NO_MMAP this code is not compiled. Each ring still gets its
> own mapping.
> 
> Checked with io_method=io_uring and io_max_concurrency=1024. liburing 2.5
> reported 102400 bytes per ring.
> Unpatched, pgbench -c 4 hit both "io_uring submit submitted only 0 of 1"
> and Assert(ioh->state == PGAIO_HS_SUBMITTED).
> Patched, the server reserved 106496 bytes and the same run finished.
> On liburing 2.7 the reservation stayed 102400 bytes and the run finished.
> 
> Alexander, please try the builds you still have. liburing 2.5, 2.6 and 2.7,
> on both machines from the report.
> I only ran 2.5 and 2.7 on x86_64.

I'd really appreciate if you stopped copy-pasting bug reports to an LLM an
then copying the answer back. We all have access to this wondrous copy-paste
magic and most of us have access to LLMs. All this does is to further increase
the noise level of the lists which is already very high.

It's useful to analyze bugs, but it does require actual analysis and the
messages need to be actually readable stuff, rather than LLM goop.

Greetings,

Andres Freund





^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: BUG #19796: io_uring crashes with specific kernel/liburing versions + io_max_concurrency due to rings overlap
  2026-10-25 07:00 BUG #19796: io_uring crashes with specific kernel/liburing versions + io_max_concurrency due to rings overlap PG Bug reporting form <noreply@postgresql.org>
@ 2026-10-26 13:31 ` Andres Freund <andres@anarazel.de>
  2026-10-26 17:14   ` Re: BUG #19796: io_uring crashes with specific kernel/liburing versions + io_max_concurrency due to rings overlap Andrey Rachitskiy <pl0h0yp1@gmail.com>
  1 sibling, 1 reply; 5+ messages in thread

From: Andres Freund @ 2026-10-26 13:31 UTC (permalink / raw)
  To: exclusion@gmail.com; pgsql-bugs@lists.postgresql.org

Hi,

On 2026-10-25 07:00:02 +0000, PG Bug reporting form wrote:
> I observed this on Ubuntu machines with kernels:
> Linux a 7.0.0-1014-aws #14~24.04.1-Ubuntu SMP PREEMPT Fri Sep 18 21:50:22
> UTC 2026 aarch64 aarch64 aarch64 GNU/Linux
> Linux b 6.14.1-061401-generic #202504071048 SMP PREEMPT_DYNAMIC Mon Apr  7
> 11:20:41 UTC 2025 x86_64 x86_64 x86_64 GNU/Linux
> and liburing 2.5. The issue is not reproduced after
> https://github.com/axboe/liburing/commit/9f4464d.

Seems we either could error out on buggy liburings or we could just add
64bytes to the ring sizes for those versions. I assume the latter fixes the
problem for you?

Greetings,

Andres Freund






^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: BUG #19796: io_uring crashes with specific kernel/liburing versions + io_max_concurrency due to rings overlap
  2026-10-25 07:00 BUG #19796: io_uring crashes with specific kernel/liburing versions + io_max_concurrency due to rings overlap PG Bug reporting form <noreply@postgresql.org>
  2026-10-26 13:31 ` Re: BUG #19796: io_uring crashes with specific kernel/liburing versions + io_max_concurrency due to rings overlap Andres Freund <andres@anarazel.de>
@ 2026-10-26 17:14   ` Andrey Rachitskiy <pl0h0yp1@gmail.com>
  0 siblings, 0 replies; 5+ messages in thread

From: Andrey Rachitskiy @ 2026-10-26 17:14 UTC (permalink / raw)
  To: Andres Freund <andres@anarazel.de>; +Cc: exclusion@gmail.com; pgsql-bugs@lists.postgresql.org

пн, 26 окт. 2026 г. в 18:31, Andres Freund <andres@anarazel.de>:

>  we could just add 64bytes to the ring sizes for those versions.


v1 built the end of the ring itself from cq_off and the SQ index array, and
increased the reservation only when that end was longer than liburing's
answer.

In v2 on liburing older than 2.7, adds 64 bytes to the answer and rounds up
to a page - the code came out more compact.

Tested 2.5/2.6/2.7 - all good.

I've taken your remarks about the LLM for the future.

Attachments:

v2-0001-Reserve-one-extra-page-for-packed-io_uring-rings.patch (text/x-patch, 4.2K) download From caf827594941d18d7be2293b18a6215df78f0ec8 Mon Sep 17 00:00:00 2001 From: Andrey Rachitskiy <pl0h0yp1@gmail.com> Date: Mon, 26 Oct 2026 12:12:33 +0500 Subject: [PATCH v2] Reserve one extra page for packed io_uring rings before liburing 2.7 Before liburing 2.7, io_uring_queue_init_mem() leaves out the 64-byte io_rings header. Add it and round up to a page. Liburing 2.7 counts the header, and the returned size is kept. Author: Andrey Rachitskiy <pl0h0yp1@gmail.com> Reported-by: Alexander Lakhin <exclusion@gmail.com> Discussion: https://www.postgresql.org/message-id/19796-ee6b17b2c500ed5e@postgresql.org --- src/backend/storage/aio/method_io_uring.c | 72 ++++++++++++++++++++--- 1 file changed, 65 insertions(+), 7 deletions(-) diff --git a/src/backend/storage/aio/method_io_uring.c b/src/backend/storage/aio/method_io_uring.c index 3ffe5061a20..53bac86a8d1 100644 --- a/src/backend/storage/aio/method_io_uring.c +++ b/src/backend/storage/aio/method_io_uring.c @@ -47,6 +47,9 @@ /* number of completions processed at once */ #define PGAIO_MAX_LOCAL_COMPLETED_IO 32 +/* SMP cache line on x86_64 and aarch64 */ +#define PGAIO_URING_SMP_CACHE_BYTES 64 + /* Entry points for IoMethodOps. */ static void pgaio_uring_shmem_request(void *arg); @@ -138,6 +141,32 @@ pgaio_uring_procs(void) return MaxBackends + NUM_AUXILIARY_PROCS - MAX_IO_WORKERS; } +#if defined(HAVE_IO_URING_QUEUE_INIT_MEM) && defined(IORING_SETUP_NO_MMAP) +/* + * Bytes to step past one ring created with io_uring_queue_init_mem(). + * + * reported is the size liburing returned. It is already a multiple of the + * page size. Liburing before 2.7 does not count the 64-byte io_rings header, + * so add it and round up to a page. The next ring has to start on a page + * boundary. Liburing 2.7 counts the header, and reported is unchanged. + */ +static int +pgaio_uring_ring_space(int reported) +{ + long page_size = sysconf(_SC_PAGESIZE); + size_t bytes = (size_t) reported; + + Assert(reported > 0); + +#if IO_URING_VERSION_MAJOR < 2 || \ + (IO_URING_VERSION_MAJOR == 2 && IO_URING_VERSION_MINOR < 7) + bytes += PGAIO_URING_SMP_CACHE_BYTES; +#endif + + return (int) TYPEALIGN(page_size, bytes); +} +#endif + /* * Initializes pgaio_uring_caps, unless that's already done. */ @@ -193,11 +222,19 @@ pgaio_uring_check_capabilities(void) ret = io_uring_queue_init_mem(io_max_concurrency, &test_ring, &p, ring_ptr, ring_size); if (ret > 0) { - pgaio_uring_caps.mem_init_size = ret; + int space; - elog(DEBUG1, - "can use combined memory mapping for io_uring, each ring needs %d bytes", - ret); + space = pgaio_uring_ring_space(ret); + pgaio_uring_caps.mem_init_size = space; + + if (space != ret) + elog(DEBUG1, + "can use combined memory mapping for io_uring, liburing reported %d bytes, reserving %d", + ret, space); + else + elog(DEBUG1, + "can use combined memory mapping for io_uring, each ring needs %d bytes", + space); /* clean up the created ring, it was just for a test */ io_uring_queue_exit(&test_ring); @@ -355,11 +392,32 @@ pgaio_uring_shmem_init(void *arg) if (pgaio_uring_caps.mem_init_size > 0) { struct io_uring_params p = {0}; + int space; ret = io_uring_queue_init_mem(io_max_concurrency, &context->io_uring_ring, &p, ring_mem_next, ring_mem_remain); - - ring_mem_remain -= ret; - ring_mem_next += ret; + if (ret > 0) + { + space = pgaio_uring_ring_space(ret); + + /* + * Step by the reserved size, not by ret. ret omits the + * io_rings header on liburing before 2.7. See + * pgaio_uring_ring_space(). + */ + if ((size_t) space > ring_mem_remain || + (size_t) pgaio_uring_caps.mem_init_size > ring_mem_remain) + ereport(ERROR, + errcode(ERRCODE_INTERNAL_ERROR), + errmsg("io_uring ring does not fit in reserved shared memory")); + else if (space > pgaio_uring_caps.mem_init_size) + ereport(ERROR, + errcode(ERRCODE_INTERNAL_ERROR), + errmsg("io_uring ring needs %d bytes, but %d were reserved", + space, pgaio_uring_caps.mem_init_size)); + + ring_mem_remain -= pgaio_uring_caps.mem_init_size; + ring_mem_next += pgaio_uring_caps.mem_init_size; + } } else #endif -- 2.53.0
^ permalink raw reply [nested|flat] 5+ messages in thread


end of thread, other threads:[~2026-10-26 17:14 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-10-25 07:00 BUG #19796: io_uring crashes with specific kernel/liburing versions + io_max_concurrency due to rings overlap PG Bug reporting form <noreply@postgresql.org>
2026-10-26 08:27 ` Andrey Rachitskiy <pl0h0yp1@gmail.com>
2026-10-26 13:27   ` Andres Freund <andres@anarazel.de>
2026-10-26 13:31 ` Andres Freund <andres@anarazel.de>
2026-10-26 17:14   ` Andrey Rachitskiy <pl0h0yp1@gmail.com>

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