pgsql: aio: Make AIO more compatible with valgrind

From: Andres Freund <andres(at)anarazel(dot)de>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: aio: Make AIO more compatible with valgrind
Date: 2025-04-07 19:24:42
Message-ID: E1u1s5K-003Al0-0h@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers

aio: Make AIO more compatible with valgrind

In some edge cases valgrind flags issues with the memory referenced by
IOs. All of the cases addressed in this change are false positives.

Most of the false positives are caused by UnpinBuffer[NoOwner] marking buffer
data as inaccessible. This happens even though the AIO subsystem still holds a
pin. That's good, there shouldn't be accesses to the buffer outside of AIO
related code until it is pinned by "user" code again. But it requires some
explicit work - if the buffer is not pinned by the current backend, we need to
explicitly mark the buffer data accessible/inaccessible while executing
completion callbacks.

That however causes a cascading issue in IO workers: After the completion
callbacks for a buffer is executed, the page is marked as inaccessible. If
subsequently the same worker is executing IO targeting the same buffer, we
would get an error, as the memory is still marked inaccessible. To avoid that,
we need to explicitly mark the memory as accessible in IO workers.

Another issue is that IO executed in workers or via io_uring will not mark
memory as DEFINED. In the case of workers that is because valgrind does not
track memory definedness across processes. For io_uring that is because
valgrind does not understand io_uring, and therefore its IOs never mark memory
as defined, whether the completions are processed in the defining process or
in another context. It's not entirely clear how to best solve that. The
current user of AIO is not affected, as it explicitly marks buffers as DEFINED
& NOACCESS anyway. Defer solving this issue until we have a user with
different needs.

Per buildfarm animal skink.

Reviewed-by: Noah Misch <noah(at)leadboat(dot)com>
Co-authored-by: Noah Misch <noah(at)leadboat(dot)com>
Discussion: https://postgr.es/m/3pd4322mogfmdd5nln3zphdwhtmq3rzdldqjwb2sfqzcgs22lf@ok2gletdaoe6

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/8e293e689bab0267d26e3705fe1d537cd43e633a

Modified Files
--------------
src/backend/storage/aio/aio_io.c | 23 +++++++++++++++++++++++
src/backend/storage/aio/method_worker.c | 19 +++++++++++++++++++
src/backend/storage/buffer/bufmgr.c | 19 +++++++++++++++++++
src/backend/storage/smgr/smgr.c | 2 ++
src/include/storage/aio_internal.h | 1 +
5 files changed, 64 insertions(+)

Browse pgsql-committers by date

  From Date Subject
Next Message Alvaro Herrera 2025-04-07 19:52:06 Re: Support NOT VALID / VALIDATE constraint options for named NOT NULL constraints
Previous Message Andres Freund 2025-04-07 19:16:48 Re: pgsql: Allow NOT NULL constraints to be added as NOT VALID