pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
pgsql: Introduce pg_shmem_allocations_numa view
54+ messages / 8 participants
[nested] [flat]

Commits from this thread:
  2026-02-12 d7edcec35c b08033944e  Make pg_numa_query_pages() work in frontend programs
  2025-07-01 bf1119d74a 54ac4944c3  Add CHECK_FOR_INTERRUPTS into pg_numa_query_pages
  2025-07-01 81f287dc92 14e52227e5  Silence valgrind about pg_numa_touch_mem_if_required
  2025-07-01 7fe2f67c7c 45879f48f1  Limit the size of numa_move_pages requests
* pgsql: Introduce pg_shmem_allocations_numa view
@ 2025-04-07 21:18  Tomas Vondra <tomas.vondra@postgresql.org>
  0 siblings, 1 reply; 54+ messages in thread

From: Tomas Vondra @ 2025-04-07 21:18 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Introduce pg_shmem_allocations_numa view

Introduce new pg_shmem_alloctions_numa view with information about how
shared memory is distributed across NUMA nodes. For each shared memory
segment, the view returns one row for each NUMA node backing it, with
the total amount of memory allocated from that node.

The view may be relatively expensive, especially when executed for the
first time in a backend, as it has to touch all memory pages to get
reliable information about the NUMA node. This may also force allocation
of the shared memory.

Unlike pg_shmem_allocations, the view does not show anonymous shared
memory allocations. It also does not show memory allocated using the
dynamic shared memory infrastructure.

Author: Jakub Wartak <jakub.wartak@enterprisedb.com>
Reviewed-by: Andres Freund <andres@anarazel.de>
Reviewed-by: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
Reviewed-by: Tomas Vondra <tomas@vondra.me>
Discussion: https://postgr.es/m/CAKZiRmxh6KWo0aqRqvmcoaX2jUxZYb4kGp3N%3Dq1w%2BDiH-696Xw%40mail.gmail.com

Branch
------
master

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

Modified Files
--------------
doc/src/sgml/system-views.sgml           |  95 ++++++++++++++++++
src/backend/catalog/system_views.sql     |   8 ++
src/backend/storage/ipc/shmem.c          | 159 +++++++++++++++++++++++++++++++
src/include/catalog/catversion.h         |   2 +-
src/include/catalog/pg_proc.dat          |   8 ++
src/test/regress/expected/numa.out       |  13 +++
src/test/regress/expected/numa_1.out     |   5 +
src/test/regress/expected/privileges.out |  16 +++-
src/test/regress/expected/rules.out      |   4 +
src/test/regress/parallel_schedule       |   2 +-
src/test/regress/sql/numa.sql            |  10 ++
src/test/regress/sql/privileges.sql      |   6 +-
12 files changed, 322 insertions(+), 6 deletions(-)



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

* Re: pgsql: Introduce pg_shmem_allocations_numa view
@ 2025-06-12 21:16  Christoph Berg <myon@debian.org>
  parent: Tomas Vondra <tomas.vondra@postgresql.org>
  0 siblings, 1 reply; 54+ messages in thread

From: Christoph Berg @ 2025-06-12 21:16 UTC (permalink / raw)
  To: Tomas Vondra <tomas.vondra@postgresql.org>; +Cc: pgsql-hackers@lists.postgresql.org

Re: Tomas Vondra
> Introduce pg_shmem_allocations_numa view

This is acting up on Debian's 32-bit architectures, namely i386, armel
and armhf:

inline patch 1: /build/reproducible-path/postgresql-18-18~beta1+20250612/build/src/test/regress/results/numa.out (+1/-5 lines) download inline-1.patch--- /build/reproducible-path/postgresql-18-18~beta1+20250612/src/test/regress/expected/numa.out 2025-06-12 12:21:21.000000000 +0000 +++ /build/reproducible-path/postgresql-18-18~beta1+20250612/build/src/test/regress/results/numa.out 2025-06-12 20:20:33.124292694 +0000 @@ -6,8 +6,4 @@ -- switch to superuser \c - SELECT COUNT(*) >= 0 AS ok FROM pg_shmem_allocations_numa; - ok ----- - t -(1 row) - +ERROR: invalid NUMA node id outside of allowed range [0, 0]: -14
The diff is the same on all architectures. -14 seems to be -EFAULT, and move_pages(2) says: Page states in the status array The following values can be returned in each element of the status array. -EFAULT This is a zero page or the memory area is not mapped by the process. https://buildd.debian.org/status/logs.php?pkg=postgresql-18&ver=18%7Ebeta1%2B20250612-1 https://buildd.debian.org/status/fetch.php?pkg=postgresql-18&arch=armel&ver=18%7Ebeta1%2B202... Christoph ^ permalink raw reply [nested|flat] 54+ messages in thread

* Re: pgsql: Introduce pg_shmem_allocations_numa view
@ 2025-06-23 14:42  Christoph Berg <myon@debian.org>
  parent: Christoph Berg <myon@debian.org>
  0 siblings, 1 reply; 54+ messages in thread

From: Christoph Berg @ 2025-06-23 14:42 UTC (permalink / raw)
  To: Tomas Vondra <tomas.vondra@postgresql.org>; +Cc: pgsql-hackers@lists.postgresql.org

Re: To Tomas Vondra
> This is acting up on Debian's 32-bit architectures, namely i386, armel
> and armhf:

... and x32 (x86_64 instruction set with 32-bit pointers).

>  SELECT COUNT(*) >= 0 AS ok FROM pg_shmem_allocations_numa;
> +ERROR:  invalid NUMA node id outside of allowed range [0, 0]: -14
> 
> -14 seems to be -EFAULT, and move_pages(2) says:
>        -EFAULT
>               This is a zero page or the memory area is not mapped by the process.

I did some debugging on i386 and made it print the page numbers:

 SELECT COUNT(*) >= 0 AS ok FROM pg_shmem_allocations_numa;
+WARNING:  invalid NUMA node id outside of allowed range [0, 0]: -14 for page 35
+WARNING:  invalid NUMA node id outside of allowed range [0, 0]: -14 for page 36
...
+WARNING:  invalid NUMA node id outside of allowed range [0, 0]: -14 for page 32768
+WARNING:  invalid NUMA node id outside of allowed range [0, 0]: -14 for page 32769

So it works for the first few pages and then the rest is EFAULT.

I think the pg_numa_touch_mem_if_required() hack might not be enough
to force the pages to be allocated. Changing that to a memcpy() didn't
help. Is there some optimization that zero pages aren't allocated
until being written to?

Why do we try to force the pages to be allocated at all? This is just
a monitoring function, it should not change the actual system state.
Why not just skip any page where the status is <0 ?

The attached patch removes that logic. Regression tests pass, but we
probably have to think about whether to report these negative numbers
as-is or perhaps convert them to NULL.

Christoph

Attachments:

  0001-Don-t-force-allocate-pages-for-pg_get_shmem_allocati.patch (text/x-diff, 0B) download

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

* Re: pgsql: Introduce pg_shmem_allocations_numa view
@ 2025-06-23 14:48  Christoph Berg <myon@debian.org>
  parent: Christoph Berg <myon@debian.org>
  0 siblings, 1 reply; 54+ messages in thread

From: Christoph Berg @ 2025-06-23 14:48 UTC (permalink / raw)
  To: Tomas Vondra <tomas.vondra@postgresql.org>; +Cc: pgsql-hackers@lists.postgresql.org

Re: To Tomas Vondra
> Why do we try to force the pages to be allocated at all? This is just
> a monitoring function, it should not change the actual system state.

One-time touching might also not be enough, what if the pages later
get swapped out and the monitoring functions are called again? They
will have to deal with these "not in memory" error conditions anyway.

Christoph





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

* Re: pgsql: Introduce pg_shmem_allocations_numa view
@ 2025-06-23 15:14  Andres Freund <andres@anarazel.de>
  parent: Christoph Berg <myon@debian.org>
  0 siblings, 1 reply; 54+ messages in thread

From: Andres Freund @ 2025-06-23 15:14 UTC (permalink / raw)
  To: Christoph Berg <myon@debian.org>; +Cc: Tomas Vondra <tomas.vondra@postgresql.org>; pgsql-hackers@lists.postgresql.org

Hi,

On 2025-06-23 16:48:27 +0200, Christoph Berg wrote:
> Re: To Tomas Vondra
> > Why do we try to force the pages to be allocated at all? This is just
> > a monitoring function, it should not change the actual system state.

The problem is that the kernel function just gives bogus results for pages
that *are* present in memory but that have only touched in another process
that has mapped the same range of memory.


> One-time touching might also not be enough, what if the pages later
> get swapped out and the monitoring functions are called again?

I don't think that's a problem, the process still has a relevant page table
entry in that case.

Greetings,

Andres Freund





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

* Re: pgsql: Introduce pg_shmem_allocations_numa view
@ 2025-06-23 15:20  Christoph Berg <myon@debian.org>
  parent: Andres Freund <andres@anarazel.de>
  0 siblings, 1 reply; 54+ messages in thread

From: Christoph Berg @ 2025-06-23 15:20 UTC (permalink / raw)
  To: Andres Freund <andres@anarazel.de>; +Cc: Tomas Vondra <tomas.vondra@postgresql.org>; pgsql-hackers@lists.postgresql.org

Re: Andres Freund
> > > Why do we try to force the pages to be allocated at all? This is just
> > > a monitoring function, it should not change the actual system state.
> 
> The problem is that the kernel function just gives bogus results for pages
> that *are* present in memory but that have only touched in another process
> that has mapped the same range of memory.

Ok, so we leave the touching in, but still defend against negative
status values?

Christoph





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

* Re: pgsql: Introduce pg_shmem_allocations_numa view
@ 2025-06-23 15:59  Christoph Berg <myon@debian.org>
  parent: Christoph Berg <myon@debian.org>
  0 siblings, 1 reply; 54+ messages in thread

From: Christoph Berg @ 2025-06-23 15:59 UTC (permalink / raw)
  To: Andres Freund <andres@anarazel.de>; +Cc: Tomas Vondra <tomas.vondra@postgresql.org>; pgsql-hackers@lists.postgresql.org

Re: To Andres Freund
> Ok, so we leave the touching in, but still defend against negative
> status values?

v2 attached.

Christoph

Attachments:

  v2-0001-Accept-unmapped-NUMA-pages.patch (text/x-diff, 0B) download

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

* Re: pgsql: Introduce pg_shmem_allocations_numa view
@ 2025-06-23 16:26  Andres Freund <andres@anarazel.de>
  parent: Christoph Berg <myon@debian.org>
  0 siblings, 1 reply; 54+ messages in thread

From: Andres Freund @ 2025-06-23 16:26 UTC (permalink / raw)
  To: Christoph Berg <myon@debian.org>; +Cc: Tomas Vondra <tomas.vondra@postgresql.org>; pgsql-hackers@lists.postgresql.org

Hi,

On 2025-06-23 17:59:24 +0200, Christoph Berg wrote:
> Re: To Andres Freund
> > Ok, so we leave the touching in, but still defend against negative
> > status values?
> 
> v2 attached.

How confident are we that this isn't actually because we passed a bogus
address to the kernel or such? With this patch, are *any* pages recognized as
valid on the machines that triggered the error?

I wonder if we ought to report the failures as a separate "numa node"
(e.g. NULL as node id) instead ...

Greetings,

Andres Freund





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

* Re: pgsql: Introduce pg_shmem_allocations_numa view
@ 2025-06-23 19:57  Christoph Berg <myon@debian.org>
  parent: Andres Freund <andres@anarazel.de>
  0 siblings, 1 reply; 54+ messages in thread

From: Christoph Berg @ 2025-06-23 19:57 UTC (permalink / raw)
  To: Andres Freund <andres@anarazel.de>; +Cc: Tomas Vondra <tomas.vondra@postgresql.org>; pgsql-hackers@lists.postgresql.org

Re: Andres Freund
> How confident are we that this isn't actually because we passed a bogus
> address to the kernel or such? With this patch, are *any* pages recognized as
> valid on the machines that triggered the error?

See upthread - the first 35 pages were ok, then a lot of -14.

> I wonder if we ought to report the failures as a separate "numa node"
> (e.g. NULL as node id) instead ...

Did that now, using N+1 (== 1 here) for errors in this Debian i386
environment (chroot on an amd64 host):

select * from pg_shmem_allocations_numa \crosstabview 

                      name                      │    0     │    1
────────────────────────────────────────────────┼──────────┼──────────
 multixact_offset                               │    69632 │    65536
 subtransaction                                 │   139264 │   131072
 notify                                         │   139264 │        0
 Shared Memory Stats                            │   188416 │   131072
 serializable                                   │   188416 │    86016
 PROCLOCK hash                                  │     4096 │        0
 FinishedSerializableTransactions               │     4096 │        0
 XLOG Ctl                                       │  2117632 │  2097152
 Shared MultiXact State                         │     4096 │        0
 Proc Header                                    │     4096 │        0
 Archiver Data                                  │     4096 │        0
.... more 0s in the last column ...
 AioHandleData                                  │  1429504 │        0
 Buffer Blocks                                  │ 67117056 │ 67108864
 Buffer IO Condition Variables                  │   266240 │        0
 Proc Array                                     │     4096 │        0
.... more 0s
(73 rows)


There is something fishy with pg_buffercache. If I restart PG, I'm
getting "Bad address" (errno 14), this time as return value of
move_pages().

postgres =# select * from pg_buffercache_numa;
DEBUG:  00000: NUMA: NBuffers=16384 os_page_count=32768 os_page_size=4096
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:383
2025-06-23 19:41:41.315 UTC [1331894] ERROR:  failed NUMA pages inquiry: Bad address
2025-06-23 19:41:41.315 UTC [1331894] STATEMENT:  select * from pg_buffercache_numa;
ERROR:  XX000: failed NUMA pages inquiry: Bad address
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:394

Repeated calls are fine.

Maybe NUMA is just not supported on 32-bit archs, but I'd rather be
sure about that before play that card.

Christoph





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

* Re: pgsql: Introduce pg_shmem_allocations_numa view
@ 2025-06-23 20:10  Tomas Vondra <tomas@vondra.me>
  parent: Christoph Berg <myon@debian.org>
  0 siblings, 1 reply; 54+ messages in thread

From: Tomas Vondra @ 2025-06-23 20:10 UTC (permalink / raw)
  To: Christoph Berg <myon@debian.org>; Andres Freund <andres@anarazel.de>; +Cc: Tomas Vondra <tomas.vondra@postgresql.org>; pgsql-hackers@lists.postgresql.org



On 6/23/25 21:57, Christoph Berg wrote:
> Re: Andres Freund
>> How confident are we that this isn't actually because we passed a bogus
>> address to the kernel or such? With this patch, are *any* pages recognized as
>> valid on the machines that triggered the error?
> 
> See upthread - the first 35 pages were ok, then a lot of -14.
> 
>> I wonder if we ought to report the failures as a separate "numa node"
>> (e.g. NULL as node id) instead ...
> 
> Did that now, using N+1 (== 1 here) for errors in this Debian i386
> environment (chroot on an amd64 host):
> 
> select * from pg_shmem_allocations_numa \crosstabview 
> 
>                       name                      │    0     │    1
> ────────────────────────────────────────────────┼──────────┼──────────
>  multixact_offset                               │    69632 │    65536
>  subtransaction                                 │   139264 │   131072
>  notify                                         │   139264 │        0
>  Shared Memory Stats                            │   188416 │   131072
>  serializable                                   │   188416 │    86016
>  PROCLOCK hash                                  │     4096 │        0
>  FinishedSerializableTransactions               │     4096 │        0
>  XLOG Ctl                                       │  2117632 │  2097152
>  Shared MultiXact State                         │     4096 │        0
>  Proc Header                                    │     4096 │        0
>  Archiver Data                                  │     4096 │        0
> .... more 0s in the last column ...
>  AioHandleData                                  │  1429504 │        0
>  Buffer Blocks                                  │ 67117056 │ 67108864
>  Buffer IO Condition Variables                  │   266240 │        0
>  Proc Array                                     │     4096 │        0
> .... more 0s
> (73 rows)
> 
> 
> There is something fishy with pg_buffercache. If I restart PG, I'm
> getting "Bad address" (errno 14), this time as return value of
> move_pages().
> 
> postgres =# select * from pg_buffercache_numa;
> DEBUG:  00000: NUMA: NBuffers=16384 os_page_count=32768 os_page_size=4096
> LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:383
> 2025-06-23 19:41:41.315 UTC [1331894] ERROR:  failed NUMA pages inquiry: Bad address
> 2025-06-23 19:41:41.315 UTC [1331894] STATEMENT:  select * from pg_buffercache_numa;
> ERROR:  XX000: failed NUMA pages inquiry: Bad address
> LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:394
> 
> Repeated calls are fine.
> 

Huh. So it's only the first call that does this?

Can you maybe print the addresses passed to pg_numa_query_pages? I
wonder if there's some bug in how we fill that array. Not sure why would
it happen only on 32-bit systems, though.

I'll create a 32-bit VM so that I can try reproducing this.


regards

-- 
Tomas Vondra






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

* Re: pgsql: Introduce pg_shmem_allocations_numa view
@ 2025-06-23 20:31  Christoph Berg <myon@debian.org>
  parent: Tomas Vondra <tomas@vondra.me>
  0 siblings, 1 reply; 54+ messages in thread

From: Christoph Berg @ 2025-06-23 20:31 UTC (permalink / raw)
  To: Tomas Vondra <tomas@vondra.me>; +Cc: Andres Freund <andres@anarazel.de>; Tomas Vondra <tomas.vondra@postgresql.org>; pgsql-hackers@lists.postgresql.org

Re: Tomas Vondra
> Huh. So it's only the first call that does this?

The first call after a restart. Reconnecting is not enough.

> Can you maybe print the addresses passed to pg_numa_query_pages? I

The addresses look good:

Breakpoint 1, pg_numa_query_pages (pid=0, count=32768, pages=0xeb44d02c, status=0xeb42c02c) at ../src/port/pg_numa.c:49
49		return numa_move_pages(pid, count, pages, NULL, status, 0);
(gdb) p *pages
$1 = (void *) 0xebc33000
(gdb) p pages[1]
$2 = (void *) 0xebc34000
(gdb) p pages[2]
$3 = (void *) 0xebc35000


> wonder if there's some bug in how we fill that array. Not sure why would
> it happen only on 32-bit systems, though.

I found something, but that should be harmless:

inline patch 1: contrib/pg_buffercache/pg_buffercache_pages.c (+1/-1 lines) download inline-1.patch--- a/contrib/pg_buffercache/pg_buffercache_pages.c +++ b/contrib/pg_buffercache/pg_buffercache_pages.c @@ -365,7 +365,7 @@ pg_buffercache_numa_pages(PG_FUNCTION_ARGS) /* Used to determine the NUMA node for all OS pages at once */ os_page_ptrs = palloc0(sizeof(void *) * os_page_count); - os_page_status = palloc(sizeof(uint64) * os_page_count); + os_page_status = palloc(sizeof(int) * os_page_count); /* Fill pointers for all the memory pages. */ idx = 0;
Christoph ^ permalink raw reply [nested|flat] 54+ messages in thread

* Re: pgsql: Introduce pg_shmem_allocations_numa view
@ 2025-06-23 20:37  Tomas Vondra <tomas@vondra.me>
  parent: Christoph Berg <myon@debian.org>
  0 siblings, 1 reply; 54+ messages in thread

From: Tomas Vondra @ 2025-06-23 20:37 UTC (permalink / raw)
  To: Christoph Berg <myon@debian.org>; +Cc: Andres Freund <andres@anarazel.de>; Tomas Vondra <tomas.vondra@postgresql.org>; pgsql-hackers@lists.postgresql.org



On 6/23/25 22:31, Christoph Berg wrote:
> Re: Tomas Vondra
>> Huh. So it's only the first call that does this?
> 
> The first call after a restart. Reconnecting is not enough.
> 
>> Can you maybe print the addresses passed to pg_numa_query_pages? I
> 
> The addresses look good:
> 
> Breakpoint 1, pg_numa_query_pages (pid=0, count=32768, pages=0xeb44d02c, status=0xeb42c02c) at ../src/port/pg_numa.c:49
> 49		return numa_move_pages(pid, count, pages, NULL, status, 0);
> (gdb) p *pages
> $1 = (void *) 0xebc33000
> (gdb) p pages[1]
> $2 = (void *) 0xebc34000
> (gdb) p pages[2]
> $3 = (void *) 0xebc35000
> 

Didn't you say the first ~35 addresses succeed, right? What about the
addresses after that?

> 
>> wonder if there's some bug in how we fill that array. Not sure why would
>> it happen only on 32-bit systems, though.
> 
> I found something, but that should be harmless:
> 
> --- a/contrib/pg_buffercache/pg_buffercache_pages.c
> +++ b/contrib/pg_buffercache/pg_buffercache_pages.c
> @@ -365,7 +365,7 @@ pg_buffercache_numa_pages(PG_FUNCTION_ARGS)
> 
>  		/* Used to determine the NUMA node for all OS pages at once */
>  		os_page_ptrs = palloc0(sizeof(void *) * os_page_count);
> -		os_page_status = palloc(sizeof(uint64) * os_page_count);
> +		os_page_status = palloc(sizeof(int) * os_page_count);
> 

Yes, good catch. But as you say, that should be benign - we allocate
more memory than needed, I don't think it should break anything.


-- 
Tomas Vondra






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

* Re: pgsql: Introduce pg_shmem_allocations_numa view
@ 2025-06-23 20:51  Christoph Berg <myon@debian.org>
  parent: Tomas Vondra <tomas@vondra.me>
  0 siblings, 1 reply; 54+ messages in thread

From: Christoph Berg @ 2025-06-23 20:51 UTC (permalink / raw)
  To: Tomas Vondra <tomas@vondra.me>; +Cc: Andres Freund <andres@anarazel.de>; Tomas Vondra <tomas.vondra@postgresql.org>; pgsql-hackers@lists.postgresql.org

Re: Tomas Vondra
> Didn't you say the first ~35 addresses succeed, right? What about the
> addresses after that?

That was pg_shmem_allocations_numa. The pg_numa_query_pages() in there
works (does not return -1), but then some of the status[] values are
-14.

When pg_buffercache_numa fails, pg_numa_query_pages() itself
returns -14.

The printed os_page_ptrs[] contents are the same for the failing and
non-failing calls, so the problem is probably elsewhere.

        /* Fill pointers for all the memory pages. */
        idx = 0;
        for (char *ptr = startptr; ptr < endptr; ptr += os_page_size)
        {
+           if (idx < 50)
+               elog(DEBUG1, "os_page_ptrs idx %d = %p", idx, ptr);
            os_page_ptrs[idx++] = ptr;


20:47 myon@postgres =# select * from pg_buffercache_numa;
DEBUG:  00000: os_page_ptrs idx 0 = 0xebc44000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 1 = 0xebc45000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 2 = 0xebc46000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 3 = 0xebc47000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 4 = 0xebc48000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 5 = 0xebc49000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 6 = 0xebc4a000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 7 = 0xebc4b000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 8 = 0xebc4c000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 9 = 0xebc4d000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 10 = 0xebc4e000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 11 = 0xebc4f000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 12 = 0xebc50000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 13 = 0xebc51000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 14 = 0xebc52000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 15 = 0xebc53000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 16 = 0xebc54000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 17 = 0xebc55000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 18 = 0xebc56000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 19 = 0xebc57000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 20 = 0xebc58000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 21 = 0xebc59000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 22 = 0xebc5a000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 23 = 0xebc5b000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 24 = 0xebc5c000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 25 = 0xebc5d000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 26 = 0xebc5e000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 27 = 0xebc5f000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 28 = 0xebc60000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 29 = 0xebc61000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 30 = 0xebc62000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 31 = 0xebc63000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 32 = 0xebc64000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 33 = 0xebc65000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 34 = 0xebc66000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 35 = 0xebc67000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 36 = 0xebc68000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 37 = 0xebc69000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 38 = 0xebc6a000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 39 = 0xebc6b000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 40 = 0xebc6c000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 41 = 0xebc6d000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 42 = 0xebc6e000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 43 = 0xebc6f000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 44 = 0xebc70000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 45 = 0xebc71000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 46 = 0xebc72000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 47 = 0xebc73000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 48 = 0xebc74000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 49 = 0xebc75000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: NUMA: NBuffers=16384 os_page_count=32768 os_page_size=4096
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:385
2025-06-23 20:47:41.827 UTC [1368080] ERROR:  failed NUMA pages inquiry: Bad address
2025-06-23 20:47:41.827 UTC [1368080] STATEMENT:  select * from pg_buffercache_numa;
ERROR:  XX000: failed NUMA pages inquiry: Bad address
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:396
Time: 92.757 ms

20:47 myon@postgres =# select * from pg_buffercache_numa;
DEBUG:  00000: os_page_ptrs idx 0 = 0xebc44000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 1 = 0xebc45000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 2 = 0xebc46000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 3 = 0xebc47000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 4 = 0xebc48000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 5 = 0xebc49000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 6 = 0xebc4a000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 7 = 0xebc4b000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 8 = 0xebc4c000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 9 = 0xebc4d000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 10 = 0xebc4e000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 11 = 0xebc4f000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 12 = 0xebc50000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 13 = 0xebc51000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 14 = 0xebc52000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 15 = 0xebc53000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 16 = 0xebc54000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 17 = 0xebc55000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 18 = 0xebc56000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 19 = 0xebc57000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 20 = 0xebc58000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 21 = 0xebc59000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 22 = 0xebc5a000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 23 = 0xebc5b000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 24 = 0xebc5c000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 25 = 0xebc5d000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 26 = 0xebc5e000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 27 = 0xebc5f000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 28 = 0xebc60000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 29 = 0xebc61000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 30 = 0xebc62000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 31 = 0xebc63000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 32 = 0xebc64000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 33 = 0xebc65000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 34 = 0xebc66000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 35 = 0xebc67000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 36 = 0xebc68000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 37 = 0xebc69000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 38 = 0xebc6a000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 39 = 0xebc6b000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 40 = 0xebc6c000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 41 = 0xebc6d000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 42 = 0xebc6e000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 43 = 0xebc6f000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 44 = 0xebc70000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 45 = 0xebc71000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 46 = 0xebc72000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 47 = 0xebc73000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 48 = 0xebc74000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: os_page_ptrs idx 49 = 0xebc75000
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
DEBUG:  00000: NUMA: NBuffers=16384 os_page_count=32768 os_page_size=4096
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:385
DEBUG:  00000: NUMA: page-faulting the buffercache for proper NUMA readouts
LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:444
Time: 24.547 ms
20:47 myon@postgres =#


Christoph





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

* Re: pgsql: Introduce pg_shmem_allocations_numa view
@ 2025-06-23 21:14  Tomas Vondra <tomas@vondra.me>
  parent: Christoph Berg <myon@debian.org>
  0 siblings, 1 reply; 54+ messages in thread

From: Tomas Vondra @ 2025-06-23 21:14 UTC (permalink / raw)
  To: Christoph Berg <myon@debian.org>; +Cc: Andres Freund <andres@anarazel.de>; Tomas Vondra <tomas.vondra@postgresql.org>; pgsql-hackers@lists.postgresql.org



On 6/23/25 22:51, Christoph Berg wrote:
> Re: Tomas Vondra
>> Didn't you say the first ~35 addresses succeed, right? What about the
>> addresses after that?
> 
> That was pg_shmem_allocations_numa. The pg_numa_query_pages() in there
> works (does not return -1), but then some of the status[] values are
> -14.
> 
> When pg_buffercache_numa fails, pg_numa_query_pages() itself
> returns -14.
> 
> The printed os_page_ptrs[] contents are the same for the failing and
> non-failing calls, so the problem is probably elsewhere.
> 
>         /* Fill pointers for all the memory pages. */
>         idx = 0;
>         for (char *ptr = startptr; ptr < endptr; ptr += os_page_size)
>         {
> +           if (idx < 50)
> +               elog(DEBUG1, "os_page_ptrs idx %d = %p", idx, ptr);
>             os_page_ptrs[idx++] = ptr;
> 
> 
> 20:47 myon@postgres =# select * from pg_buffercache_numa;
> DEBUG:  00000: os_page_ptrs idx 0 = 0xebc44000
> LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
> DEBUG:  00000: os_page_ptrs idx 1 = 0xebc45000
> LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
> DEBUG:  00000: os_page_ptrs idx 2 = 0xebc46000
> LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
> DEBUG:  00000: os_page_ptrs idx 3 = 0xebc47000
...
> DEBUG:  00000: os_page_ptrs idx 47 = 0xebc73000
> LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
> DEBUG:  00000: os_page_ptrs idx 48 = 0xebc74000
> LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
> DEBUG:  00000: os_page_ptrs idx 49 = 0xebc75000
> LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
> DEBUG:  00000: NUMA: NBuffers=16384 os_page_count=32768 os_page_size=4096
> LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:385
> 2025-06-23 20:47:41.827 UTC [1368080] ERROR:  failed NUMA pages inquiry: Bad address
> 2025-06-23 20:47:41.827 UTC [1368080] STATEMENT:  select * from pg_buffercache_numa;
> ERROR:  XX000: failed NUMA pages inquiry: Bad address
> LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:396
> Time: 92.757 ms
> 
> 20:47 myon@postgres =# select * from pg_buffercache_numa;
> DEBUG:  00000: os_page_ptrs idx 0 = 0xebc44000
> LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
> DEBUG:  00000: os_page_ptrs idx 1 = 0xebc45000
> LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
> DEBUG:  00000: os_page_ptrs idx 2 = 0xebc46000
> LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
> DEBUG:  00000: os_page_ptrs idx 3 = 0xebc47000
...> DEBUG:  00000: os_page_ptrs idx 46 = 0xebc72000
> LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
> DEBUG:  00000: os_page_ptrs idx 47 = 0xebc73000
> LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
> DEBUG:  00000: os_page_ptrs idx 48 = 0xebc74000
> LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
> DEBUG:  00000: os_page_ptrs idx 49 = 0xebc75000
> LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:375
> DEBUG:  00000: NUMA: NBuffers=16384 os_page_count=32768 os_page_size=4096
> LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:385
> DEBUG:  00000: NUMA: page-faulting the buffercache for proper NUMA readouts
> LOCATION:  pg_buffercache_numa_pages, pg_buffercache_pages.c:444
> Time: 24.547 ms
> 20:47 myon@postgres =#
> 

True. If it fails on first call, but succeeds on the other, then the
problem is likely somewhere else. But also on the second call we won't
do the memory touching. Can you try setting firstNumaTouch=false, so
that we do this on every call?


At the beginning you mentioned this is happening on i386, armel and
armhf - are all those in qemu? I've tried on my rpi5 (with 32-bit user
space), and there everything seems to work fine. But that's aarch64
kernel, just the user space if 32-bit.


regards

-- 
Tomas Vondra






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

* Re: pgsql: Introduce pg_shmem_allocations_numa view
@ 2025-06-23 21:25  Christoph Berg <myon@debian.org>
  parent: Tomas Vondra <tomas@vondra.me>
  0 siblings, 1 reply; 54+ messages in thread

From: Christoph Berg @ 2025-06-23 21:25 UTC (permalink / raw)
  To: Tomas Vondra <tomas@vondra.me>; +Cc: Andres Freund <andres@anarazel.de>; Tomas Vondra <tomas.vondra@postgresql.org>; pgsql-hackers@lists.postgresql.org

Re: Tomas Vondra
> True. If it fails on first call, but succeeds on the other, then the
> problem is likely somewhere else. But also on the second call we won't
> do the memory touching. Can you try setting firstNumaTouch=false, so
> that we do this on every call?

firstNumaTouch=false, it still fails on the first call.

I assume you meant actually keeping firstNumaTouch=true - but it still
fails on the first call.

The memory touching is done for the first call in each backend, but
reconnecting doesn't reset it, I have to restart PG.

> At the beginning you mentioned this is happening on i386, armel and
> armhf - are all those in qemu? I've tried on my rpi5 (with 32-bit user
> space), and there everything seems to work fine. But that's aarch64
> kernel, just the user space if 32-bit.

I'm testing on i386 in a chroot on a amd64 kernel. (same for x32)
armel and armhf are also 32-bit chroots on a arm64 host.

https://buildd.debian.org/status/package.php?p=postgresql-18&suite=experimental

Maybe this is a kernel bug.

Christoph





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

* Re: pgsql: Introduce pg_shmem_allocations_numa view
@ 2025-06-23 21:47  Tomas Vondra <tomas@vondra.me>
  parent: Christoph Berg <myon@debian.org>
  0 siblings, 1 reply; 54+ messages in thread

From: Tomas Vondra @ 2025-06-23 21:47 UTC (permalink / raw)
  To: Christoph Berg <myon@debian.org>; +Cc: Andres Freund <andres@anarazel.de>; Tomas Vondra <tomas.vondra@postgresql.org>; pgsql-hackers@lists.postgresql.org



On 6/23/25 23:25, Christoph Berg wrote:
> Re: Tomas Vondra
>> True. If it fails on first call, but succeeds on the other, then the
>> problem is likely somewhere else. But also on the second call we won't
>> do the memory touching. Can you try setting firstNumaTouch=false, so
>> that we do this on every call?
> 
> firstNumaTouch=false, it still fails on the first call.
> 
> I assume you meant actually keeping firstNumaTouch=true - but it still
> fails on the first call.
> 

No, I meant firstNumaTouch=false, so that the touching happens on every
call. I was wondering if that makes all calls fail.

> The memory touching is done for the first call in each backend, but
> reconnecting doesn't reset it, I have to restart PG.
> 

I don't follow. Why wouldn't reconnecting reset it?

>> At the beginning you mentioned this is happening on i386, armel and
>> armhf - are all those in qemu? I've tried on my rpi5 (with 32-bit user
>> space), and there everything seems to work fine. But that's aarch64
>> kernel, just the user space if 32-bit.
> 
> I'm testing on i386 in a chroot on a amd64 kernel. (same for x32)
> armel and armhf are also 32-bit chroots on a arm64 host.
> 
> https://buildd.debian.org/status/package.php?p=postgresql-18&suite=experimental
> 
> Maybe this is a kernel bug.
> 

Or maybe the 32-bit chroot on 64-bit host matters and confuses some
calculation.


-- 
Tomas Vondra






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

* Re: pgsql: Introduce pg_shmem_allocations_numa view
@ 2025-06-24 01:43  Tomas Vondra <tomas@vondra.me>
  parent: Tomas Vondra <tomas@vondra.me>
  0 siblings, 2 replies; 54+ messages in thread

From: Tomas Vondra @ 2025-06-24 01:43 UTC (permalink / raw)
  To: Christoph Berg <myon@debian.org>; +Cc: Andres Freund <andres@anarazel.de>; Tomas Vondra <tomas.vondra@postgresql.org>; pgsql-hackers@lists.postgresql.org

On 6/23/25 23:47, Tomas Vondra wrote:
> ...
> 
> Or maybe the 32-bit chroot on 64-bit host matters and confuses some
> calculation.
>

I think it's likely something like this. I noticed that if I modify
pg_buffercache_numa_pages() to query the addresses one by one, it works.
And when I increase the number, it stops working somewhere between 16k
and 17k items.

It may be a coincidence, but I suspect it's related to the sizeof(void
*) being 8 in the kernel, but only 4 in the chroot. So the userspace
passes an array of 4-byte items, but kernel interprets that as 8-byte
items. That is, we call

long move_pages(int pid, unsigned long count, void *pages[.count], const
int nodes[.count], int status[.count], int flags);

Which (I assume) just passes the parameters to kernel. And it'll
interpret them per kernel pointer size.


If this is what's happening, I'm not sure what to do about it ...


FWIW while looking into this, I tried running this under valgrind (on a
regular 64-bit system, not in the chroot), and I get this report:

==65065== Invalid read of size 8
==65065==    at 0x113B0EBE: pg_buffercache_numa_pages
(pg_buffercache_pages.c:380)
==65065==    by 0x6B539D: ExecMakeTableFunctionResult (execSRF.c:234)
==65065==    by 0x6CEB7E: FunctionNext (nodeFunctionscan.c:94)
==65065==    by 0x6B6ACA: ExecScanFetch (execScan.h:126)
==65065==    by 0x6B6B31: ExecScanExtended (execScan.h:170)
==65065==    by 0x6B6C9D: ExecScan (execScan.c:59)
==65065==    by 0x6CEF0F: ExecFunctionScan (nodeFunctionscan.c:269)
==65065==    by 0x6B29FA: ExecProcNodeFirst (execProcnode.c:469)
==65065==    by 0x6A6F56: ExecProcNode (executor.h:313)
==65065==    by 0x6A9533: ExecutePlan (execMain.c:1679)
==65065==    by 0x6A7422: standard_ExecutorRun (execMain.c:367)
==65065==    by 0x6A7330: ExecutorRun (execMain.c:304)
==65065==    by 0x934EF0: PortalRunSelect (pquery.c:921)
==65065==    by 0x934BD8: PortalRun (pquery.c:765)
==65065==    by 0x92E4CD: exec_simple_query (postgres.c:1273)
==65065==    by 0x93301E: PostgresMain (postgres.c:4766)
==65065==    by 0x92A88B: BackendMain (backend_startup.c:124)
==65065==    by 0x85A7C7: postmaster_child_launch (launch_backend.c:290)
==65065==    by 0x860111: BackendStartup (postmaster.c:3580)
==65065==    by 0x85DE6F: ServerLoop (postmaster.c:1702)
==65065==  Address 0x7b6c000 is in a rw- anonymous segment


This fails here (on the pg_numa_touch_mem_if_required call):

    for (char *ptr = startptr; ptr < endptr; ptr += os_page_size)
    {
        os_page_ptrs[idx++] = ptr;

        /* Only need to touch memory once per backend process */
        if (firstNumaTouch)
            pg_numa_touch_mem_if_required(touch, ptr);
    }

The 0x7b6c000 is the very first pointer, and it's the only pointer that
triggers this warning. At first I thought there's something wrong with
how we align the pointer using TYPEALIGN_DOWN(), but then I noticed it's
actually the pointer of BufferGetBlock(1).

So I'm a bit puzzled by this, and I'm not sure it's related to the other
issue at all (it probably is not).

It's a bit too late here, I'll continue investigating this tomorrow.

-- 
Tomas Vondra






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

* Re: pgsql: Introduce pg_shmem_allocations_numa view
@ 2025-06-24 08:24  Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
  parent: Tomas Vondra <tomas@vondra.me>
  1 sibling, 1 reply; 54+ messages in thread

From: Bertrand Drouvot @ 2025-06-24 08:24 UTC (permalink / raw)
  To: Tomas Vondra <tomas@vondra.me>; +Cc: Christoph Berg <myon@debian.org>; Andres Freund <andres@anarazel.de>; Tomas Vondra <tomas.vondra@postgresql.org>; pgsql-hackers@lists.postgresql.org

Hi,

On Tue, Jun 24, 2025 at 03:43:19AM +0200, Tomas Vondra wrote:
> On 6/23/25 23:47, Tomas Vondra wrote:
> > ...
> > 
> > Or maybe the 32-bit chroot on 64-bit host matters and confuses some
> > calculation.
> >
> 
> I think it's likely something like this.

I think the same.

> I noticed that if I modify
> pg_buffercache_numa_pages() to query the addresses one by one, it works.
> And when I increase the number, it stops working somewhere between 16k
> and 17k items.

Yeah, same for me with pg_get_shmem_allocations_numa(). It works if
pg_numa_query_pages() is done on chunks <= 16 pages but fails if done on more
than 16 pages.

It's also confirmed by test_chunk_size.c attached:

$ gcc-11 -m32 -o test_chunk_size test_chunk_size.c
$ ./test_chunk_size
 1 pages: SUCCESS (0 errors)
 2 pages: SUCCESS (0 errors)
 3 pages: SUCCESS (0 errors)
 4 pages: SUCCESS (0 errors)
 5 pages: SUCCESS (0 errors)
 6 pages: SUCCESS (0 errors)
 7 pages: SUCCESS (0 errors)
 8 pages: SUCCESS (0 errors)
 9 pages: SUCCESS (0 errors)
10 pages: SUCCESS (0 errors)
11 pages: SUCCESS (0 errors)
12 pages: SUCCESS (0 errors)
13 pages: SUCCESS (0 errors)
14 pages: SUCCESS (0 errors)
15 pages: SUCCESS (0 errors)
16 pages: SUCCESS (0 errors)
17 pages: 1 errors
Threshold: 17 pages

No error if -m32 is not used.

> It may be a coincidence, but I suspect it's related to the sizeof(void
> *) being 8 in the kernel, but only 4 in the chroot. So the userspace
> passes an array of 4-byte items, but kernel interprets that as 8-byte
> items. That is, we call
> 
> long move_pages(int pid, unsigned long count, void *pages[.count], const
> int nodes[.count], int status[.count], int flags);
> 
> Which (I assume) just passes the parameters to kernel. And it'll
> interpret them per kernel pointer size.
>

I also suspect something in this area...

> If this is what's happening, I'm not sure what to do about it ...

We could work by chunks (16?) on 32 bits but would probably produce performance
degradation (we mention it in the doc though). Also would always 16 be a correct
chunk size? 

Regards,

-- 
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com

Attachments:

test_chunk_size.c (text/x-csrc, 1.6K) download #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/syscall.h> #include <string.h> #include <errno.h> int test_chunk_size(int chunk_size) { size_t page_size = sysconf(_SC_PAGESIZE); void *mem = malloc(page_size * chunk_size); if (!mem) return -1; memset(mem, 0xFF, page_size * chunk_size); void **ptrs = malloc(sizeof(void*) * chunk_size); int *status = malloc(sizeof(int) * chunk_size); for (int j = 0; j < chunk_size; j++) { ptrs[j] = (char*)mem + (j * page_size); status[j] = -999; } long result = syscall(SYS_move_pages, 0, chunk_size, ptrs, NULL, status, 0); int errors = 0; if (result == 0) { for (int j = 0; j < chunk_size; j++) { if (status[j] < 0) errors++; } } free(mem); free(ptrs); free(status); return (result == 0) ? errors : -1; } int main() { int threshold = -1; // Test sizes from 1 to 40 pages for (int size = 1; size <= 40; size++) { int errors = test_chunk_size(size); if (errors == -1) { if (threshold == -1) threshold = size; break; } else if (errors == 0) { printf("%2d pages: SUCCESS (0 errors)\n", size); } else { printf("%2d pages: %d errors\n", size, errors); threshold = size; break; } } if (threshold > 0) printf("Threshold: %d pages\n", threshold); else printf("No threshold found in range 1-40 pages\n"); return 0; }
^ permalink raw reply [nested|flat] 54+ messages in thread

* Re: pgsql: Introduce pg_shmem_allocations_numa view
@ 2025-06-24 09:20  Tomas Vondra <tomas@vondra.me>
  parent: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
  0 siblings, 1 reply; 54+ messages in thread

From: Tomas Vondra @ 2025-06-24 09:20 UTC (permalink / raw)
  To: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>; +Cc: Christoph Berg <myon@debian.org>; Andres Freund <andres@anarazel.de>; Tomas Vondra <tomas.vondra@postgresql.org>; pgsql-hackers@lists.postgresql.org

On 6/24/25 10:24, Bertrand Drouvot wrote:
> Hi,
> 
> On Tue, Jun 24, 2025 at 03:43:19AM +0200, Tomas Vondra wrote:
>> On 6/23/25 23:47, Tomas Vondra wrote:
>>> ...
>>>
>>> Or maybe the 32-bit chroot on 64-bit host matters and confuses some
>>> calculation.
>>>
>>
>> I think it's likely something like this.
> 
> I think the same.
> 
>> I noticed that if I modify
>> pg_buffercache_numa_pages() to query the addresses one by one, it works.
>> And when I increase the number, it stops working somewhere between 16k
>> and 17k items.
> 
> Yeah, same for me with pg_get_shmem_allocations_numa(). It works if
> pg_numa_query_pages() is done on chunks <= 16 pages but fails if done on more
> than 16 pages.
> 
> It's also confirmed by test_chunk_size.c attached:
> 
> $ gcc-11 -m32 -o test_chunk_size test_chunk_size.c
> $ ./test_chunk_size
>  1 pages: SUCCESS (0 errors)
>  2 pages: SUCCESS (0 errors)
>  3 pages: SUCCESS (0 errors)
>  4 pages: SUCCESS (0 errors)
>  5 pages: SUCCESS (0 errors)
>  6 pages: SUCCESS (0 errors)
>  7 pages: SUCCESS (0 errors)
>  8 pages: SUCCESS (0 errors)
>  9 pages: SUCCESS (0 errors)
> 10 pages: SUCCESS (0 errors)
> 11 pages: SUCCESS (0 errors)
> 12 pages: SUCCESS (0 errors)
> 13 pages: SUCCESS (0 errors)
> 14 pages: SUCCESS (0 errors)
> 15 pages: SUCCESS (0 errors)
> 16 pages: SUCCESS (0 errors)
> 17 pages: 1 errors
> Threshold: 17 pages
> 
> No error if -m32 is not used.
> 
>> It may be a coincidence, but I suspect it's related to the sizeof(void
>> *) being 8 in the kernel, but only 4 in the chroot. So the userspace
>> passes an array of 4-byte items, but kernel interprets that as 8-byte
>> items. That is, we call
>>
>> long move_pages(int pid, unsigned long count, void *pages[.count], const
>> int nodes[.count], int status[.count], int flags);
>>
>> Which (I assume) just passes the parameters to kernel. And it'll
>> interpret them per kernel pointer size.
>>
> 
> I also suspect something in this area...
> 
>> If this is what's happening, I'm not sure what to do about it ...
> 
> We could work by chunks (16?) on 32 bits but would probably produce performance
> degradation (we mention it in the doc though). Also would always 16 be a correct
> chunk size? 

I don't see how this would solve anything?

AFAICS the problem is the two places are confused about how large the
array elements are, and get to interpret that differently. Using a
smaller array won't solve that. The pg function would still allocate
array of 16 x 32-bit pointers, and the kernel would interpret this as 16
x 64-bit pointers. And that means the kernel will (a) write into memory
beyond the allocated buffer - a clear buffer overflow, and (b) see bogus
pointers, because it'll concatenate two 32-bit pointers.

I don't see how using smaller array makes this correct. That it works is
more a matter of luck, and also a consequence of still allocating the
whole array, so there's no overflow (at least I kept that, not sure how
you did the chunks).

If I fix the code to make the entries 64-bit (by treating the pointers
as int64), it suddenly starts working - no bad addresses, etc. Well,
almost, because I get this

 bufferid | os_page_num | numa_node
----------+-------------+-----------
        1 |           0 |         0
        1 |           1 |       -14
        2 |           2 |         0
        2 |           3 |       -14
        3 |           4 |         0
        3 |           5 |       -14
        4 |           6 |         0
        4 |           7 |       -14
        ...

The -14 status is interesting, because that's the same value Christoph
reported as the other issue (in pg_shmem_allocations_numa).

I did an experiment and changed os_page_status to be declared as int64,
not just int. And interestingly, that produced this:

 bufferid | os_page_num | numa_node
----------+-------------+-----------
        1 |           0 |         0
        1 |           1 |         0
        2 |           2 |         0
        2 |           3 |         0
        3 |           4 |         0
        3 |           5 |         0
        4 |           6 |         0
        4 |           7 |         0
        ...

But I don't see how this makes any sense, because "int" should be 4B in
both cases (in 64-bit kernel and 32-bit chroot).

FWIW I realized this applies to "official" systems with 32-bit user
space on 64-bit kernels, like e.g. rpi5 with RPi OS 32-bit. (Fun fact,
rpi5 has 8 NUMA nodes, with all CPUs attached to all NUMA nodes.)

I'm starting to think we need to disable NUMA for setups like this,
mixing 64-bit kernels with 32-bit chroot. Is there a good way to detect
those, so that we can error-out?

FWIW this doesn't explain the strange valgrind issue, though.


-- 
Tomas Vondra






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

* Re: pgsql: Introduce pg_shmem_allocations_numa view
@ 2025-06-24 11:10  Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
  parent: Tomas Vondra <tomas@vondra.me>
  0 siblings, 1 reply; 54+ messages in thread

From: Bertrand Drouvot @ 2025-06-24 11:10 UTC (permalink / raw)
  To: Tomas Vondra <tomas@vondra.me>; +Cc: Christoph Berg <myon@debian.org>; Andres Freund <andres@anarazel.de>; Tomas Vondra <tomas.vondra@postgresql.org>; pgsql-hackers@lists.postgresql.org

Hi,

On Tue, Jun 24, 2025 at 11:20:15AM +0200, Tomas Vondra wrote:
> On 6/24/25 10:24, Bertrand Drouvot wrote:
> > Yeah, same for me with pg_get_shmem_allocations_numa(). It works if
> > pg_numa_query_pages() is done on chunks <= 16 pages but fails if done on more
> > than 16 pages.
> > 
> > It's also confirmed by test_chunk_size.c attached:
> > 
> > $ gcc-11 -m32 -o test_chunk_size test_chunk_size.c
> > $ ./test_chunk_size
> >  1 pages: SUCCESS (0 errors)
> >  2 pages: SUCCESS (0 errors)
> >  3 pages: SUCCESS (0 errors)
> >  4 pages: SUCCESS (0 errors)
> >  5 pages: SUCCESS (0 errors)
> >  6 pages: SUCCESS (0 errors)
> >  7 pages: SUCCESS (0 errors)
> >  8 pages: SUCCESS (0 errors)
> >  9 pages: SUCCESS (0 errors)
> > 10 pages: SUCCESS (0 errors)
> > 11 pages: SUCCESS (0 errors)
> > 12 pages: SUCCESS (0 errors)
> > 13 pages: SUCCESS (0 errors)
> > 14 pages: SUCCESS (0 errors)
> > 15 pages: SUCCESS (0 errors)
> > 16 pages: SUCCESS (0 errors)
> > 17 pages: 1 errors
> > Threshold: 17 pages
> > 
> > No error if -m32 is not used.
> > 
> > We could work by chunks (16?) on 32 bits but would probably produce performance
> > degradation (we mention it in the doc though). Also would always 16 be a correct
> > chunk size? 
> 
> I don't see how this would solve anything?
> 
> AFAICS the problem is the two places are confused about how large the
> array elements are, and get to interpret that differently.

> I don't see how using smaller array makes this correct. That it works is
> more a matter of luck,

Not sure it's luck, maybe the wrong pointers arithmetic has no effect if batch
size is <= 16.

So we have kernel_move_pages() -> kernel_move_pages() (because nodes is NULL here
for us as we call "numa_move_pages(pid, count, pages, NULL, status, 0);").

So, if we look at do_pages_stat() ([1]), we can see that it uses an hardcoded
"#define DO_PAGES_STAT_CHUNK_NR 16UL" and that this pointers arithmetic:

"
        pages += chunk_nr;
        status += chunk_nr;
"

is done but has no effect since nr_pages will exit the loop if we use a batch
size <= 16.

So if this pointer arithmetic is not correct, (it seems that it should advance
by 16 * sizeof(compat_uptr_t) instead) then it has no effect as long as the batch
size is <= 16.

Does test_chunk_size also fails at 17 for you?

[1]: https://github.com/torvalds/linux/blob/master/mm/migrate.c

Regards,

-- 
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com





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

* Re: pgsql: Introduce pg_shmem_allocations_numa view
@ 2025-06-24 11:10  Andres Freund <andres@anarazel.de>
  parent: Tomas Vondra <tomas@vondra.me>
  1 sibling, 1 reply; 54+ messages in thread

From: Andres Freund @ 2025-06-24 11:10 UTC (permalink / raw)
  To: Tomas Vondra <tomas@vondra.me>; +Cc: Christoph Berg <myon@debian.org>; Tomas Vondra <tomas.vondra@postgresql.org>; pgsql-hackers@lists.postgresql.org

Hi,

On 2025-06-24 03:43:19 +0200, Tomas Vondra wrote:
> FWIW while looking into this, I tried running this under valgrind (on a
> regular 64-bit system, not in the chroot), and I get this report:
> 
> ==65065== Invalid read of size 8
> ==65065==    at 0x113B0EBE: pg_buffercache_numa_pages
> (pg_buffercache_pages.c:380)
> ==65065==    by 0x6B539D: ExecMakeTableFunctionResult (execSRF.c:234)
> ==65065==    by 0x6CEB7E: FunctionNext (nodeFunctionscan.c:94)
> ==65065==    by 0x6B6ACA: ExecScanFetch (execScan.h:126)
> ==65065==    by 0x6B6B31: ExecScanExtended (execScan.h:170)
> ==65065==    by 0x6B6C9D: ExecScan (execScan.c:59)
> ==65065==    by 0x6CEF0F: ExecFunctionScan (nodeFunctionscan.c:269)
> ==65065==    by 0x6B29FA: ExecProcNodeFirst (execProcnode.c:469)
> ==65065==    by 0x6A6F56: ExecProcNode (executor.h:313)
> ==65065==    by 0x6A9533: ExecutePlan (execMain.c:1679)
> ==65065==    by 0x6A7422: standard_ExecutorRun (execMain.c:367)
> ==65065==    by 0x6A7330: ExecutorRun (execMain.c:304)
> ==65065==    by 0x934EF0: PortalRunSelect (pquery.c:921)
> ==65065==    by 0x934BD8: PortalRun (pquery.c:765)
> ==65065==    by 0x92E4CD: exec_simple_query (postgres.c:1273)
> ==65065==    by 0x93301E: PostgresMain (postgres.c:4766)
> ==65065==    by 0x92A88B: BackendMain (backend_startup.c:124)
> ==65065==    by 0x85A7C7: postmaster_child_launch (launch_backend.c:290)
> ==65065==    by 0x860111: BackendStartup (postmaster.c:3580)
> ==65065==    by 0x85DE6F: ServerLoop (postmaster.c:1702)
> ==65065==  Address 0x7b6c000 is in a rw- anonymous segment
> 
> 
> This fails here (on the pg_numa_touch_mem_if_required call):
> 
>     for (char *ptr = startptr; ptr < endptr; ptr += os_page_size)
>     {
>         os_page_ptrs[idx++] = ptr;
> 
>         /* Only need to touch memory once per backend process */
>         if (firstNumaTouch)
>             pg_numa_touch_mem_if_required(touch, ptr);
>     }

That's because we mark unpinned pages as inaccessible / mark them as
accessible when pinning. See logic related to that in PinBuffer():

				/*
				 * Assume that we acquired a buffer pin for the purposes of
				 * Valgrind buffer client checks (even in !result case) to
				 * keep things simple.  Buffers that are unsafe to access are
				 * not generally guaranteed to be marked undefined or
				 * non-accessible in any case.
				 */


> The 0x7b6c000 is the very first pointer, and it's the only pointer that
> triggers this warning.

I suspect that that's because valgrind combines different reports or such.

Greetings,

Andres Freund





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

* Re: pgsql: Introduce pg_shmem_allocations_numa view
@ 2025-06-24 12:33  Tomas Vondra <tomas@vondra.me>
  parent: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
  0 siblings, 1 reply; 54+ messages in thread

From: Tomas Vondra @ 2025-06-24 12:33 UTC (permalink / raw)
  To: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>; +Cc: Christoph Berg <myon@debian.org>; Andres Freund <andres@anarazel.de>; Tomas Vondra <tomas.vondra@postgresql.org>; pgsql-hackers@lists.postgresql.org



On 6/24/25 13:10, Bertrand Drouvot wrote:
> Hi,
> 
> On Tue, Jun 24, 2025 at 11:20:15AM +0200, Tomas Vondra wrote:
>> On 6/24/25 10:24, Bertrand Drouvot wrote:
>>> Yeah, same for me with pg_get_shmem_allocations_numa(). It works if
>>> pg_numa_query_pages() is done on chunks <= 16 pages but fails if done on more
>>> than 16 pages.
>>>
>>> It's also confirmed by test_chunk_size.c attached:
>>>
>>> $ gcc-11 -m32 -o test_chunk_size test_chunk_size.c
>>> $ ./test_chunk_size
>>>  1 pages: SUCCESS (0 errors)
>>>  2 pages: SUCCESS (0 errors)
>>>  3 pages: SUCCESS (0 errors)
>>>  4 pages: SUCCESS (0 errors)
>>>  5 pages: SUCCESS (0 errors)
>>>  6 pages: SUCCESS (0 errors)
>>>  7 pages: SUCCESS (0 errors)
>>>  8 pages: SUCCESS (0 errors)
>>>  9 pages: SUCCESS (0 errors)
>>> 10 pages: SUCCESS (0 errors)
>>> 11 pages: SUCCESS (0 errors)
>>> 12 pages: SUCCESS (0 errors)
>>> 13 pages: SUCCESS (0 errors)
>>> 14 pages: SUCCESS (0 errors)
>>> 15 pages: SUCCESS (0 errors)
>>> 16 pages: SUCCESS (0 errors)
>>> 17 pages: 1 errors
>>> Threshold: 17 pages
>>>
>>> No error if -m32 is not used.
>>>
>>> We could work by chunks (16?) on 32 bits but would probably produce performance
>>> degradation (we mention it in the doc though). Also would always 16 be a correct
>>> chunk size? 
>>
>> I don't see how this would solve anything?
>>
>> AFAICS the problem is the two places are confused about how large the
>> array elements are, and get to interpret that differently.
> 
>> I don't see how using smaller array makes this correct. That it works is
>> more a matter of luck,
> 
> Not sure it's luck, maybe the wrong pointers arithmetic has no effect if batch
> size is <= 16.
> 
> So we have kernel_move_pages() -> kernel_move_pages() (because nodes is NULL here
> for us as we call "numa_move_pages(pid, count, pages, NULL, status, 0);").
> 
> So, if we look at do_pages_stat() ([1]), we can see that it uses an hardcoded
> "#define DO_PAGES_STAT_CHUNK_NR 16UL" and that this pointers arithmetic:
> 
> "
>         pages += chunk_nr;
>         status += chunk_nr;
> "
> 
> is done but has no effect since nr_pages will exit the loop if we use a batch
> size <= 16.
> 
> So if this pointer arithmetic is not correct, (it seems that it should advance
> by 16 * sizeof(compat_uptr_t) instead) then it has no effect as long as the batch
> size is <= 16.
> 
> Does test_chunk_size also fails at 17 for you?

Yes, it fails for me at 17 too. So you're saying the access within each
chunk of 16 elements is OK, but that maybe advancing to the next chunk
is not quite right? In which case limiting the access to 16 entries
might be a workaround.

In any case, this sounds like a kernel bug, right? I don't have much
experience with the kernel code, so don't want to rely too much on my
interpretation of it.


regards

-- 
Tomas Vondra






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

* Re: pgsql: Introduce pg_shmem_allocations_numa view
@ 2025-06-24 12:42  Tomas Vondra <tomas@vondra.me>
  parent: Andres Freund <andres@anarazel.de>
  0 siblings, 0 replies; 54+ messages in thread

From: Tomas Vondra @ 2025-06-24 12:42 UTC (permalink / raw)
  To: Andres Freund <andres@anarazel.de>; +Cc: Christoph Berg <myon@debian.org>; Tomas Vondra <tomas.vondra@postgresql.org>; pgsql-hackers@lists.postgresql.org



On 6/24/25 13:10, Andres Freund wrote:
> Hi,
> 
> On 2025-06-24 03:43:19 +0200, Tomas Vondra wrote:
>> FWIW while looking into this, I tried running this under valgrind (on a
>> regular 64-bit system, not in the chroot), and I get this report:
>>
>> ==65065== Invalid read of size 8
>> ==65065==    at 0x113B0EBE: pg_buffercache_numa_pages
>> (pg_buffercache_pages.c:380)
>> ==65065==    by 0x6B539D: ExecMakeTableFunctionResult (execSRF.c:234)
>> ==65065==    by 0x6CEB7E: FunctionNext (nodeFunctionscan.c:94)
>> ==65065==    by 0x6B6ACA: ExecScanFetch (execScan.h:126)
>> ==65065==    by 0x6B6B31: ExecScanExtended (execScan.h:170)
>> ==65065==    by 0x6B6C9D: ExecScan (execScan.c:59)
>> ==65065==    by 0x6CEF0F: ExecFunctionScan (nodeFunctionscan.c:269)
>> ==65065==    by 0x6B29FA: ExecProcNodeFirst (execProcnode.c:469)
>> ==65065==    by 0x6A6F56: ExecProcNode (executor.h:313)
>> ==65065==    by 0x6A9533: ExecutePlan (execMain.c:1679)
>> ==65065==    by 0x6A7422: standard_ExecutorRun (execMain.c:367)
>> ==65065==    by 0x6A7330: ExecutorRun (execMain.c:304)
>> ==65065==    by 0x934EF0: PortalRunSelect (pquery.c:921)
>> ==65065==    by 0x934BD8: PortalRun (pquery.c:765)
>> ==65065==    by 0x92E4CD: exec_simple_query (postgres.c:1273)
>> ==65065==    by 0x93301E: PostgresMain (postgres.c:4766)
>> ==65065==    by 0x92A88B: BackendMain (backend_startup.c:124)
>> ==65065==    by 0x85A7C7: postmaster_child_launch (launch_backend.c:290)
>> ==65065==    by 0x860111: BackendStartup (postmaster.c:3580)
>> ==65065==    by 0x85DE6F: ServerLoop (postmaster.c:1702)
>> ==65065==  Address 0x7b6c000 is in a rw- anonymous segment
>>
>>
>> This fails here (on the pg_numa_touch_mem_if_required call):
>>
>>     for (char *ptr = startptr; ptr < endptr; ptr += os_page_size)
>>     {
>>         os_page_ptrs[idx++] = ptr;
>>
>>         /* Only need to touch memory once per backend process */
>>         if (firstNumaTouch)
>>             pg_numa_touch_mem_if_required(touch, ptr);
>>     }
> 
> That's because we mark unpinned pages as inaccessible / mark them as
> accessible when pinning. See logic related to that in PinBuffer():
> 
> 				/*
> 				 * Assume that we acquired a buffer pin for the purposes of
> 				 * Valgrind buffer client checks (even in !result case) to
> 				 * keep things simple.  Buffers that are unsafe to access are
> 				 * not generally guaranteed to be marked undefined or
> 				 * non-accessible in any case.
> 				 */
> 
> 
>> The 0x7b6c000 is the very first pointer, and it's the only pointer that
>> triggers this warning.
> 
> I suspect that that's because valgrind combines different reports or such.
> 

Thanks. It probably is something like that, although I made sure to not
use any such options when running valgrind (so --error-limit=no). But
maybe there's something else, hiding the reports.

I guess there are two ways to address this - make sure the buffers are
marked as accessible/defined, or add a valgrind suppression. I think the
suppression is the right approach here, otherwise we'd need to worry
about already pinned buffers etc. Which seems not great, the functions
don't even care about buffers right now, they mostly work with memory
pages (especially pg_shmem_allocations_numa).

Barring objections, I'll fix it this way.


regards

-- 
Tomas Vondra






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

* Re: pgsql: Introduce pg_shmem_allocations_numa view
@ 2025-06-24 13:25  Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
  parent: Tomas Vondra <tomas@vondra.me>
  0 siblings, 2 replies; 54+ messages in thread

From: Bertrand Drouvot @ 2025-06-24 13:25 UTC (permalink / raw)
  To: Tomas Vondra <tomas@vondra.me>; +Cc: Christoph Berg <myon@debian.org>; Andres Freund <andres@anarazel.de>; Tomas Vondra <tomas.vondra@postgresql.org>; pgsql-hackers@lists.postgresql.org

Hi,

On Tue, Jun 24, 2025 at 02:33:59PM +0200, Tomas Vondra wrote:
> 
> 
> On 6/24/25 13:10, Bertrand Drouvot wrote:
> > So, if we look at do_pages_stat() ([1]), we can see that it uses an hardcoded
> > "#define DO_PAGES_STAT_CHUNK_NR 16UL" and that this pointers arithmetic:
> > 
> > "
> >         pages += chunk_nr;
> >         status += chunk_nr;
> > "
> > 
> > is done but has no effect since nr_pages will exit the loop if we use a batch
> > size <= 16.
> > 
> > So if this pointer arithmetic is not correct, (it seems that it should advance
> > by 16 * sizeof(compat_uptr_t) instead) then it has no effect as long as the batch
> > size is <= 16.
> > 
> > Does test_chunk_size also fails at 17 for you?
> 
> Yes, it fails for me at 17 too. So you're saying the access within each
> chunk of 16 elements is OK, but that maybe advancing to the next chunk
> is not quite right?

Yes, I think compat_uptr_t usage is missing in do_pages_stat() (while it's used
in do_pages_move()).

Having a chunk size <= DO_PAGES_STAT_CHUNK_NR ensures we are not affected
by the wrong pointer arithmetic.

> In which case limiting the access to 16 entries
> might be a workaround.

Yes, something like:

inline patch 1: src/backend/storage/ipc/shmem.c (+11/-2 lines) download inline-1.patchdiff --git a/src/backend/storage/ipc/shmem.c b/src/backend/storage/ipc/shmem.c index c9ae3b45b76..070ad2f13e7 100644 --- a/src/backend/storage/ipc/shmem.c +++ b/src/backend/storage/ipc/shmem.c @@ -689,8 +689,17 @@ pg_get_shmem_allocations_numa(PG_FUNCTION_ARGS) CHECK_FOR_INTERRUPTS(); } - if (pg_numa_query_pages(0, shm_ent_page_count, page_ptrs, pages_status) == -1) - elog(ERROR, "failed NUMA pages inquiry status: %m"); + #define NUMA_QUERY_CHUNK_SIZE 16 /* has to be <= DO_PAGES_STAT_CHUNK_NR (do_pages_stat())*/ + + for (uint64 chunk_start = 0; chunk_start < shm_ent_page_count; chunk_start += NUMA_QUERY_CHUNK_SIZE) { + uint64 chunk_size = Min(NUMA_QUERY_CHUNK_SIZE, shm_ent_page_count - chunk_start); + + if (pg_numa_query_pages(0, chunk_size, &page_ptrs[chunk_start], + &pages_status[chunk_start]) == -1) + elog(ERROR, "failed NUMA pages inquiry status: %m"); + } + + #undef NUMA_QUERY_CHUNK_SIZE
> In any case, this sounds like a kernel bug, right? yes it sounds like a kernel bug. > I don't have much > experience with the kernel code, so don't want to rely too much on my > interpretation of it. I don't have that much experience too but I think the issue is in do_pages_stat() and that "pages += chunk_nr" should be advanced by sizeof(compat_uptr_t) instead. Regards, -- Bertrand Drouvot PostgreSQL Contributors Team RDS Open Source Databases Amazon Web Services: https://aws.amazon.com ^ permalink raw reply [nested|flat] 54+ messages in thread

* Re: pgsql: Introduce pg_shmem_allocations_numa view
@ 2025-06-24 14:41  Christoph Berg <myon@debian.org>
  parent: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
  1 sibling, 2 replies; 54+ messages in thread

From: Christoph Berg @ 2025-06-24 14:41 UTC (permalink / raw)
  To: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>; +Cc: Tomas Vondra <tomas@vondra.me>; Andres Freund <andres@anarazel.de>; Tomas Vondra <tomas.vondra@postgresql.org>; pgsql-hackers@lists.postgresql.org

Re: Bertrand Drouvot
> Yes, I think compat_uptr_t usage is missing in do_pages_stat() (while it's used
> in do_pages_move()).

I was also reading the kernel source around that place but you spotted
the problem before me. This patch resolves the issue here:

inline patch 1: mm/migrate.c (+7/-1 lines) download inline-1.patchdiff --git a/mm/migrate.c b/mm/migrate.c index 8cf0f9c9599..542c81ec3ed 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -2444,7 +2444,13 @@ static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages, if (copy_to_user(status, chunk_status, chunk_nr * sizeof(*status))) break; - pages += chunk_nr; + if (in_compat_syscall()) { + compat_uptr_t __user *pages32 = (compat_uptr_t __user *)pages; + + pages32 += chunk_nr; + pages = (const void __user * __user *) pages32; + } else + pages += chunk_nr; status += chunk_nr; nr_pages -= chunk_nr; }
> Having a chunk size <= DO_PAGES_STAT_CHUNK_NR ensures we are not affected > by the wrong pointer arithmetic. Good idea. Buggy kernels will be around for some time. > + #define NUMA_QUERY_CHUNK_SIZE 16 /* has to be <= DO_PAGES_STAT_CHUNK_NR (do_pages_stat())*/ > + > + for (uint64 chunk_start = 0; chunk_start < shm_ent_page_count; chunk_start += NUMA_QUERY_CHUNK_SIZE) { Perhaps optimize it to this: #if sizeof(void *) == 4 #define NUMA_QUERY_CHUNK_SIZE 16 /* has to be <= DO_PAGES_STAT_CHUNK_NR (do_pages_stat())*/ #else #define NUMA_QUERY_CHUNK_SIZE 1024 #endif ... or some other bigger number. The loop could also include CHECK_FOR_INTERRUPTS(); > > I don't have much > > experience with the kernel code, so don't want to rely too much on my > > interpretation of it. > > I don't have that much experience too but I think the issue is in do_pages_stat() > and that "pages += chunk_nr" should be advanced by sizeof(compat_uptr_t) instead. Me neither, but I'll try submit this fix. Christoph ^ permalink raw reply [nested|flat] 54+ messages in thread

* Re: pgsql: Introduce pg_shmem_allocations_numa view
@ 2025-06-24 15:04  Tomas Vondra <tomas@vondra.me>
  parent: Christoph Berg <myon@debian.org>
  1 sibling, 1 reply; 54+ messages in thread

From: Tomas Vondra @ 2025-06-24 15:04 UTC (permalink / raw)
  To: Christoph Berg <myon@debian.org>; Bertrand Drouvot <bertranddrouvot.pg@gmail.com>; +Cc: Andres Freund <andres@anarazel.de>; Tomas Vondra <tomas.vondra@postgresql.org>; pgsql-hackers@lists.postgresql.org

On 6/24/25 16:41, Christoph Berg wrote:
> Re: Bertrand Drouvot
>> Yes, I think compat_uptr_t usage is missing in do_pages_stat() (while it's used
>> in do_pages_move()).
> 
> I was also reading the kernel source around that place but you spotted
> the problem before me. This patch resolves the issue here:
> 
> diff --git a/mm/migrate.c b/mm/migrate.c
> index 8cf0f9c9599..542c81ec3ed 100644
> --- a/mm/migrate.c
> +++ b/mm/migrate.c
> @@ -2444,7 +2444,13 @@ static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages,
>  		if (copy_to_user(status, chunk_status, chunk_nr * sizeof(*status)))
>  			break;
> 
> -		pages += chunk_nr;
> +		if (in_compat_syscall()) {
> +			compat_uptr_t __user *pages32 = (compat_uptr_t __user *)pages;
> +
> +			pages32 += chunk_nr;
> +			pages = (const void __user * __user *) pages32;
> +		} else
> +			pages += chunk_nr;
>  		status += chunk_nr;
>  		nr_pages -= chunk_nr;
>  	}
> 
> 
>> Having a chunk size <= DO_PAGES_STAT_CHUNK_NR ensures we are not affected
>> by the wrong pointer arithmetic.
> 
> Good idea. Buggy kernels will be around for some time.
> 

If it's a reliable fix, then I guess we can do it like this. But won't
that be a performance penalty on everyone? Or does the system split the
array into 16-element chunks anyway, so this makes no difference?

Anyway, maybe we should start by reporting this to the kernel people. Do
you want me to do that, or shall one of you take care of that? I suppose
that'd be better, as you already wrote a fix / know the code better.

>> +               #define NUMA_QUERY_CHUNK_SIZE 16  /* has to be <= DO_PAGES_STAT_CHUNK_NR (do_pages_stat())*/
>> +
>> +               for (uint64 chunk_start = 0; chunk_start < shm_ent_page_count; chunk_start += NUMA_QUERY_CHUNK_SIZE) {
> 
> Perhaps optimize it to this:
> 
> #if sizeof(void *) == 4
> #define NUMA_QUERY_CHUNK_SIZE 16  /* has to be <= DO_PAGES_STAT_CHUNK_NR (do_pages_stat())*/
> #else
> #define NUMA_QUERY_CHUNK_SIZE 1024
> #endif
> 
> ... or some other bigger number.
> 

Hmm, maybe. I guess that'd hurt only fully 32-bit systems, but that also
seems like a non-issue.

> The loop could also include CHECK_FOR_INTERRUPTS();
> 
>>> I don't have much
>>> experience with the kernel code, so don't want to rely too much on my
>>> interpretation of it.
>>
>> I don't have that much experience too but I think the issue is in do_pages_stat()
>> and that "pages += chunk_nr" should be advanced by sizeof(compat_uptr_t) instead.
> 
> Me neither, but I'll try submit this fix.
> 

+1

Thanks to both of you for the report and the investigation.


regards

-- 
Tomas Vondra






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

* Re: pgsql: Introduce pg_shmem_allocations_numa view
@ 2025-06-24 15:30  Christoph Berg <myon@debian.org>
  parent: Tomas Vondra <tomas@vondra.me>
  0 siblings, 3 replies; 54+ messages in thread

From: Christoph Berg @ 2025-06-24 15:30 UTC (permalink / raw)
  To: Tomas Vondra <tomas@vondra.me>; +Cc: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>; Andres Freund <andres@anarazel.de>; Tomas Vondra <tomas.vondra@postgresql.org>; pgsql-hackers@lists.postgresql.org

Re: Tomas Vondra
> If it's a reliable fix, then I guess we can do it like this. But won't
> that be a performance penalty on everyone? Or does the system split the
> array into 16-element chunks anyway, so this makes no difference?

There's still the overhead of the syscall itself. But no idea how
costly it is to have this 16-step loop in user or kernel space.

We could claim that on 32-bit systems, shared_buffers would be smaller
anyway, so there the overhead isn't that big. And the step size should
be larger (if at all) on 64-bit.

> Anyway, maybe we should start by reporting this to the kernel people. Do
> you want me to do that, or shall one of you take care of that? I suppose
> that'd be better, as you already wrote a fix / know the code better.

Submitted: https://marc.info/?l=linux-mm&m=175077821909222&w=2

Christoph





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

* Re: pgsql: Introduce pg_shmem_allocations_numa view
@ 2025-06-24 18:24  Christoph Berg <myon@debian.org>
  parent: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
  1 sibling, 0 replies; 54+ messages in thread

From: Christoph Berg @ 2025-06-24 18:24 UTC (permalink / raw)
  To: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>; +Cc: Tomas Vondra <tomas@vondra.me>; Andres Freund <andres@anarazel.de>; Tomas Vondra <tomas.vondra@postgresql.org>; pgsql-hackers@lists.postgresql.org

Re: Bertrand Drouvot
> Yes, something like:
> 
> diff --git a/src/backend/storage/ipc/shmem.c b/src/backend/storage/ipc/shmem.c
> index c9ae3b45b76..070ad2f13e7 100644
> --- a/src/backend/storage/ipc/shmem.c
> +++ b/src/backend/storage/ipc/shmem.c
> @@ -689,8 +689,17 @@ pg_get_shmem_allocations_numa(PG_FUNCTION_ARGS)
>                         CHECK_FOR_INTERRUPTS();
>                 }
> 
> -               if (pg_numa_query_pages(0, shm_ent_page_count, page_ptrs, pages_status) == -1)
> -                       elog(ERROR, "failed NUMA pages inquiry status: %m");
> +               #define NUMA_QUERY_CHUNK_SIZE 16  /* has to be <= DO_PAGES_STAT_CHUNK_NR (do_pages_stat())*/
> +
> +               for (uint64 chunk_start = 0; chunk_start < shm_ent_page_count; chunk_start += NUMA_QUERY_CHUNK_SIZE) {
> +                        uint64 chunk_size = Min(NUMA_QUERY_CHUNK_SIZE, shm_ent_page_count - chunk_start);
> +
> +                       if (pg_numa_query_pages(0, chunk_size, &page_ptrs[chunk_start],
> +                                                                       &pages_status[chunk_start]) == -1)
> +                               elog(ERROR, "failed NUMA pages inquiry status: %m");
> +               }
> +
> +               #undef NUMA_QUERY_CHUNK_SIZE

I uploaded a variant of this patch to Debian and it seems to have fixed the issue:

https://buildd.debian.org/status/package.php?p=postgresql-18&suite=experimental

(No reply from linux-mm yet.)

Christoph

Attachments:

move-pages32 (text/plain, 2.3K) download Work around a Linux bug in move_pages In 32-bit mode on 64-bit kernels, move_pages() does not correctly advance to the next chunk. Work around by not asking for more than 16 pages at once so move_pages() internal loop is not executed more than once. https://www.postgresql.org/message-id/flat/a3a4fe3d-1a80-4e03-aa8e-150ee15f6c35%40vondra.me#6abe7eaa802b5b07bb70cc3229e63a9f https://marc.info/?l=linux-mm&m=175077821909222&w=2 --- a/contrib/pg_buffercache/pg_buffercache_pages.c +++ b/contrib/pg_buffercache/pg_buffercache_pages.c @@ -390,8 +390,15 @@ pg_buffercache_numa_pages(PG_FUNCTION_AR memset(os_page_status, 0xff, sizeof(int) * os_page_count); /* Query NUMA status for all the pointers */ - if (pg_numa_query_pages(0, os_page_count, os_page_ptrs, os_page_status) == -1) - elog(ERROR, "failed NUMA pages inquiry: %m"); +#define NUMA_QUERY_CHUNK_SIZE 16 /* has to be <= DO_PAGES_STAT_CHUNK_NR (do_pages_stat())*/ + for (uint64 chunk_start = 0; chunk_start < os_page_count; chunk_start += NUMA_QUERY_CHUNK_SIZE) { + uint64 chunk_size = Min(NUMA_QUERY_CHUNK_SIZE, os_page_count - chunk_start); + + if (pg_numa_query_pages(0, chunk_size, &os_page_ptrs[chunk_start], + &os_page_status[chunk_start]) == -1) + elog(ERROR, "failed NUMA pages inquiry status: %m"); + } +#undef NUMA_QUERY_CHUNK_SIZE /* Initialize the multi-call context, load entries about buffers */ --- a/src/backend/storage/ipc/shmem.c +++ b/src/backend/storage/ipc/shmem.c @@ -689,8 +689,15 @@ pg_get_shmem_allocations_numa(PG_FUNCTIO CHECK_FOR_INTERRUPTS(); } - if (pg_numa_query_pages(0, shm_ent_page_count, page_ptrs, pages_status) == -1) - elog(ERROR, "failed NUMA pages inquiry status: %m"); +#define NUMA_QUERY_CHUNK_SIZE 16 /* has to be <= DO_PAGES_STAT_CHUNK_NR (do_pages_stat())*/ + for (uint64 chunk_start = 0; chunk_start < shm_ent_page_count; chunk_start += NUMA_QUERY_CHUNK_SIZE) { + uint64 chunk_size = Min(NUMA_QUERY_CHUNK_SIZE, shm_ent_page_count - chunk_start); + + if (pg_numa_query_pages(0, chunk_size, &page_ptrs[chunk_start], + &pages_status[chunk_start]) == -1) + elog(ERROR, "failed NUMA pages inquiry status: %m"); + } +#undef NUMA_QUERY_CHUNK_SIZE /* Count number of NUMA nodes used for this shared memory entry */ memset(nodes, 0, sizeof(Size) * (max_nodes + 1));
^ permalink raw reply [nested|flat] 54+ messages in thread

* Re: pgsql: Introduce pg_shmem_allocations_numa view
@ 2025-06-24 20:32  Tomas Vondra <tomas@vondra.me>
  parent: Christoph Berg <myon@debian.org>
  2 siblings, 2 replies; 54+ messages in thread

From: Tomas Vondra @ 2025-06-24 20:32 UTC (permalink / raw)
  To: Christoph Berg <myon@debian.org>; +Cc: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>; Andres Freund <andres@anarazel.de>; Tomas Vondra <tomas.vondra@postgresql.org>; pgsql-hackers@lists.postgresql.org

On 6/24/25 17:30, Christoph Berg wrote:
> Re: Tomas Vondra
>> If it's a reliable fix, then I guess we can do it like this. But won't
>> that be a performance penalty on everyone? Or does the system split the
>> array into 16-element chunks anyway, so this makes no difference?
> 
> There's still the overhead of the syscall itself. But no idea how
> costly it is to have this 16-step loop in user or kernel space.
> 
> We could claim that on 32-bit systems, shared_buffers would be smaller
> anyway, so there the overhead isn't that big. And the step size should
> be larger (if at all) on 64-bit.
> 
>> Anyway, maybe we should start by reporting this to the kernel people. Do
>> you want me to do that, or shall one of you take care of that? I suppose
>> that'd be better, as you already wrote a fix / know the code better.
> 
> Submitted: https://marc.info/?l=linux-mm&m=175077821909222&w=2
> 

Thanks! Now we wait ...

Attached is a minor tweak of the valgrind suppresion rules, to add the
two places touching the memory. I was hoping I could add a single rule
for pg_numa_touch_mem_if_required, but that does not work - it's a
macro, not a function. So I had to add one rule for both functions,
querying the NUMA. That's a bit disappointing, because it means it'll
hide all other failues (of Memcheck:Addr8 type) in those functions.

Perhaps it'd be be better to turn pg_numa_touch_mem_if_required into a
proper (inlined) function, at least with USE_VALGRIND defined. Something
like the v2 patch - needs more testing to ensure the inlined function
doesn't break the touching or something silly like that.

regards

-- 
Tomas Vondra

Attachments: download all (2 files, .tar.gz)

fix-valgrind-for-numa.patch (text/x-patch, 831B) download diff --git a/src/tools/valgrind.supp b/src/tools/valgrind.supp index 7ea464c8094..36bf3253f76 100644 --- a/src/tools/valgrind.supp +++ b/src/tools/valgrind.supp @@ -180,3 +180,22 @@ Memcheck:Cond fun:PyObject_Realloc } + +# Querying NUMA node for shared memory requires touching the memory so +# that it gets allocated in the process. But we'll touch memory backing +# buffers, but that memory may be marked as noaccess for buffers that +# are not pinned. So just ignore that, we're not really accessing the +# buffers, for both places querying the NUMA status. +{ + pg_buffercache_numa_pages + Memcheck:Addr8 + fun:pg_buffercache_numa_pages + fun:ExecMakeTableFunctionResult +} + +{ + pg_get_shmem_allocations_numa + Memcheck:Addr8 + fun:pg_get_shmem_allocations_numa + fun:ExecMakeTableFunctionResult +}
fix-valgrind-for-numa-v2.patch (text/x-patch, 1.4K) download diff --git a/src/include/port/pg_numa.h b/src/include/port/pg_numa.h index 40f1d324dcf..3b9a5b42898 100644 --- a/src/include/port/pg_numa.h +++ b/src/include/port/pg_numa.h @@ -24,9 +24,22 @@ extern PGDLLIMPORT int pg_numa_get_max_node(void); * This is required on Linux, before pg_numa_query_pages() as we * need to page-fault before move_pages(2) syscall returns valid results. */ +#ifdef USE_VALGRIND + +static inline void +pg_numa_touch_mem_if_required(uint64 tmp, char *ptr) +{ + volatile uint64 ro_volatile_var pg_attribute_unused(); + ro_volatile_var = *(volatile uint64 *) ptr; +} + +#else + #define pg_numa_touch_mem_if_required(ro_volatile_var, ptr) \ ro_volatile_var = *(volatile uint64 *) ptr +#endif + #else #define pg_numa_touch_mem_if_required(ro_volatile_var, ptr) \ diff --git a/src/tools/valgrind.supp b/src/tools/valgrind.supp index 7ea464c8094..6b9a8998f82 100644 --- a/src/tools/valgrind.supp +++ b/src/tools/valgrind.supp @@ -180,3 +180,14 @@ Memcheck:Cond fun:PyObject_Realloc } + +# Querying NUMA node for shared memory requires touching the memory so +# that it gets allocated in the process. But we'll touch memory backing +# buffers, but that memory may be marked as noaccess for buffers that +# are not pinned. So just ignore that, we're not really accessing the +# buffers, for all places querying the NUMA status. +{ + pg_numa_touch_mem_if_required + Memcheck:Addr8 + fun:pg_numa_touch_mem_if_required +}
^ permalink raw reply [nested|flat] 54+ messages in thread

* Re: pgsql: Introduce pg_shmem_allocations_numa view
@ 2025-06-25 06:05  Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
  parent: Christoph Berg <myon@debian.org>
  1 sibling, 1 reply; 54+ messages in thread

From: Bertrand Drouvot @ 2025-06-25 06:05 UTC (permalink / raw)
  To: Christoph Berg <myon@debian.org>; +Cc: Tomas Vondra <tomas@vondra.me>; Andres Freund <andres@anarazel.de>; Tomas Vondra <tomas.vondra@postgresql.org>; pgsql-hackers@lists.postgresql.org

Hi,

On Tue, Jun 24, 2025 at 04:41:33PM +0200, Christoph Berg wrote:
> Re: Bertrand Drouvot
> > Yes, I think compat_uptr_t usage is missing in do_pages_stat() (while it's used
> > in do_pages_move()).
> 
> I was also reading the kernel source around that place but you spotted
> the problem before me. This patch resolves the issue here:
> 
> diff --git a/mm/migrate.c b/mm/migrate.c
> index 8cf0f9c9599..542c81ec3ed 100644
> --- a/mm/migrate.c
> +++ b/mm/migrate.c
> @@ -2444,7 +2444,13 @@ static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages,
>  		if (copy_to_user(status, chunk_status, chunk_nr * sizeof(*status)))
>  			break;
> 
> -		pages += chunk_nr;
> +		if (in_compat_syscall()) {
> +			compat_uptr_t __user *pages32 = (compat_uptr_t __user *)pages;
> +
> +			pages32 += chunk_nr;
> +			pages = (const void __user * __user *) pages32;
> +		} else
> +			pages += chunk_nr;
>  		status += chunk_nr;
>  		nr_pages -= chunk_nr;
>  	}
> 

Thanks! Yeah, I had the same kind of patch idea in mind.

> > +               #define NUMA_QUERY_CHUNK_SIZE 16  /* has to be <= DO_PAGES_STAT_CHUNK_NR (do_pages_stat())*/
> > +
> > +               for (uint64 chunk_start = 0; chunk_start < shm_ent_page_count; chunk_start += NUMA_QUERY_CHUNK_SIZE) {
> 
> Perhaps optimize it to this:
> 
> #if sizeof(void *) == 4
> #define NUMA_QUERY_CHUNK_SIZE 16  /* has to be <= DO_PAGES_STAT_CHUNK_NR (do_pages_stat())*/
> #else
> #define NUMA_QUERY_CHUNK_SIZE 1024
> #endif
> 
> ... or some other bigger number.

I had in mind to split the batch size on the PG side only for 32-bits, what about
the attached?

Regards,

-- 
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com

Attachments:

  v1-0001-Work-around-Linux-kernel-bug-in-do_pages_stat.patch (text/x-diff, 0B) download

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

* Re: pgsql: Introduce pg_shmem_allocations_numa view
@ 2025-06-25 06:11  Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
  parent: Christoph Berg <myon@debian.org>
  2 siblings, 0 replies; 54+ messages in thread

From: Bertrand Drouvot @ 2025-06-25 06:11 UTC (permalink / raw)
  To: Christoph Berg <myon@debian.org>; +Cc: Tomas Vondra <tomas@vondra.me>; Andres Freund <andres@anarazel.de>; Tomas Vondra <tomas.vondra@postgresql.org>; pgsql-hackers@lists.postgresql.org

Hi,

On Tue, Jun 24, 2025 at 05:30:02PM +0200, Christoph Berg wrote:
> Re: Tomas Vondra
> > If it's a reliable fix, then I guess we can do it like this. But won't
> > that be a performance penalty on everyone? Or does the system split the
> > array into 16-element chunks anyway, so this makes no difference?
> 
> There's still the overhead of the syscall itself. But no idea how
> costly it is to have this 16-step loop in user or kernel space.
> 
> We could claim that on 32-bit systems, shared_buffers would be smaller
> anyway, so there the overhead isn't that big. And the step size should
> be larger (if at all) on 64-bit.

Right, and we already mention in the doc that using those views is "very slow"
or "can take a noticeable amount of time".

> > Anyway, maybe we should start by reporting this to the kernel people. Do
> > you want me to do that, or shall one of you take care of that? I suppose
> > that'd be better, as you already wrote a fix / know the code better.
> 
> Submitted: https://marc.info/?l=linux-mm&m=175077821909222&w=2

Thanks! I had in mind to look at how to report such a bug and provide a patch
but you beat me to it.

Regards,

-- 
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com





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

* Re: pgsql: Introduce pg_shmem_allocations_numa view
@ 2025-06-25 06:45  Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
  parent: Tomas Vondra <tomas@vondra.me>
  1 sibling, 0 replies; 54+ messages in thread

From: Bertrand Drouvot @ 2025-06-25 06:45 UTC (permalink / raw)
  To: Tomas Vondra <tomas@vondra.me>; +Cc: Christoph Berg <myon@debian.org>; Andres Freund <andres@anarazel.de>; Tomas Vondra <tomas.vondra@postgresql.org>; pgsql-hackers@lists.postgresql.org

Hi,

On Tue, Jun 24, 2025 at 10:32:25PM +0200, Tomas Vondra wrote:
> 
> Attached is a minor tweak of the valgrind suppresion rules,

Thanks!

> to add the
> two places touching the memory. I was hoping I could add a single rule
> for pg_numa_touch_mem_if_required, but that does not work - it's a
> macro, not a function. So I had to add one rule for both functions,
> querying the NUMA. That's a bit disappointing, because it means it'll
> hide all other failues (of Memcheck:Addr8 type) in those functions.
>

Shouldn't we add 2 rules for Memcheck:Addr4 too?

> Perhaps it'd be be better to turn pg_numa_touch_mem_if_required into a
> proper (inlined) function, at least with USE_VALGRIND defined.

Yeah I think that's probably better to reduce the scope to what we really want to.

> Something
> like the v2 patch -

yeah, maybe:

- add a rule for Memcheck:Addr4?
- have the same parameters name for the macro and the function?

Regards,

-- 
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com





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

* Re: pgsql: Introduce pg_shmem_allocations_numa view
@ 2025-06-25 07:15  Jakub Wartak <jakub.wartak@enterprisedb.com>
  parent: Christoph Berg <myon@debian.org>
  2 siblings, 1 reply; 54+ messages in thread

From: Jakub Wartak @ 2025-06-25 07:15 UTC (permalink / raw)
  To: Christoph Berg <myon@debian.org>; +Cc: Tomas Vondra <tomas@vondra.me>; Bertrand Drouvot <bertranddrouvot.pg@gmail.com>; Andres Freund <andres@anarazel.de>; Tomas Vondra <tomas.vondra@postgresql.org>; pgsql-hackers@lists.postgresql.org

On Tue, Jun 24, 2025 at 5:30 PM Christoph Berg <myon@debian.org> wrote:
>
> Re: Tomas Vondra
> > If it's a reliable fix, then I guess we can do it like this. But won't
> > that be a performance penalty on everyone? Or does the system split the
> > array into 16-element chunks anyway, so this makes no difference?
>
> There's still the overhead of the syscall itself. But no idea how
> costly it is to have this 16-step loop in user or kernel space.
>
> We could claim that on 32-bit systems, shared_buffers would be smaller
> anyway, so there the overhead isn't that big. And the step size should
> be larger (if at all) on 64-bit.
>
> > Anyway, maybe we should start by reporting this to the kernel people. Do
> > you want me to do that, or shall one of you take care of that? I suppose
> > that'd be better, as you already wrote a fix / know the code better.
>
> Submitted: https://marc.info/?l=linux-mm&m=175077821909222&w=2
>

Hi all, I'm quite late to the party (just noticed the thread), but
here's some addition context: it technically didn't make any sense to
me to have NUMA on 32-bit due too small amount of addressable memory
(after all, NUMA is about big iron, probably not even VMs), so in the
first versions of the patchset I've excluded 32-bit (and back then for
some reason I couldn't even find libnuma i386, but Andres pointed to
me that it exists, so we re-added it probably just to stay
consistent). The thread has kind of snowballed since then, but I still
believe that NUMA on 32-bit does not make a lot of sense.

Even assuming future shm interleaving one day in future version,
allocation of small s_b sizes will usually fit a single NUMA node.

-J.





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

* Re: pgsql: Introduce pg_shmem_allocations_numa view
@ 2025-06-25 09:00  Christoph Berg <myon@debian.org>
  parent: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
  0 siblings, 2 replies; 54+ messages in thread

From: Christoph Berg @ 2025-06-25 09:00 UTC (permalink / raw)
  To: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>; Tomas Vondra <tomas@vondra.me>; +Cc: Andres Freund <andres@anarazel.de>; Tomas Vondra <tomas.vondra@postgresql.org>; pgsql-hackers@lists.postgresql.org

Re: Bertrand Drouvot
> +/*
> + * Work around Linux kernel bug in 32-bit compat mode: do_pages_stat() has
> + * incorrect pointer arithmetic for more than DO_PAGES_STAT_CHUNK_NR pages.
> + */
> +#if SIZEOF_SIZE_T == 4

I was also missing it in my suggested patch draft, but this should
probably include #ifdef __linux__.


Re: Tomas Vondra
> +#ifdef USE_VALGRIND
> +
> +static inline void
> +pg_numa_touch_mem_if_required(uint64 tmp, char *ptr)

Stupid question, if this function gets properly inlined, why not
always use it as there should be no performance difference vs using a
macro?

Christoph





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

* Re: pgsql: Introduce pg_shmem_allocations_numa view
@ 2025-06-25 09:22  Tomas Vondra <tomas@vondra.me>
  parent: Christoph Berg <myon@debian.org>
  1 sibling, 0 replies; 54+ messages in thread

From: Tomas Vondra @ 2025-06-25 09:22 UTC (permalink / raw)
  To: Christoph Berg <myon@debian.org>; Bertrand Drouvot <bertranddrouvot.pg@gmail.com>; +Cc: Andres Freund <andres@anarazel.de>; Tomas Vondra <tomas.vondra@postgresql.org>; pgsql-hackers@lists.postgresql.org

On 6/25/25 11:00, Christoph Berg wrote:
> Re: Bertrand Drouvot
>> +/*
>> + * Work around Linux kernel bug in 32-bit compat mode: do_pages_stat() has
>> + * incorrect pointer arithmetic for more than DO_PAGES_STAT_CHUNK_NR pages.
>> + */
>> +#if SIZEOF_SIZE_T == 4
> 
> I was also missing it in my suggested patch draft, but this should
> probably include #ifdef __linux__.
> 


> 
> Re: Tomas Vondra
>> +#ifdef USE_VALGRIND
>> +
>> +static inline void
>> +pg_numa_touch_mem_if_required(uint64 tmp, char *ptr)
> 
> Stupid question, if this function gets properly inlined, why not
> always use it as there should be no performance difference vs using a
> macro?
> 

TBH I'm not 100% sure it works correctly, I need to check it actually
touches the memory etc. It's possible it was discussed in one of the
earlier NUMA threads, and there are reasons to do a macro.

I also dislike the ifdefs because it adds subtle differences between the
"normal" code and the code tested with valgrind. So just having the
inlined function would be "nicer".

regards

-- 
Tomas Vondra






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

* Re: pgsql: Introduce pg_shmem_allocations_numa view
@ 2025-06-25 09:31  Tomas Vondra <tomas@vondra.me>
  parent: Jakub Wartak <jakub.wartak@enterprisedb.com>
  0 siblings, 1 reply; 54+ messages in thread

From: Tomas Vondra @ 2025-06-25 09:31 UTC (permalink / raw)
  To: Jakub Wartak <jakub.wartak@enterprisedb.com>; Christoph Berg <myon@debian.org>; +Cc: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>; Andres Freund <andres@anarazel.de>; Tomas Vondra <tomas.vondra@postgresql.org>; pgsql-hackers@lists.postgresql.org

On 6/25/25 09:15, Jakub Wartak wrote:
> On Tue, Jun 24, 2025 at 5:30 PM Christoph Berg <myon@debian.org> wrote:
>>
>> Re: Tomas Vondra
>>> If it's a reliable fix, then I guess we can do it like this. But won't
>>> that be a performance penalty on everyone? Or does the system split the
>>> array into 16-element chunks anyway, so this makes no difference?
>>
>> There's still the overhead of the syscall itself. But no idea how
>> costly it is to have this 16-step loop in user or kernel space.
>>
>> We could claim that on 32-bit systems, shared_buffers would be smaller
>> anyway, so there the overhead isn't that big. And the step size should
>> be larger (if at all) on 64-bit.
>>
>>> Anyway, maybe we should start by reporting this to the kernel people. Do
>>> you want me to do that, or shall one of you take care of that? I suppose
>>> that'd be better, as you already wrote a fix / know the code better.
>>
>> Submitted: https://marc.info/?l=linux-mm&m=175077821909222&w=2
>>
> 
> Hi all, I'm quite late to the party (just noticed the thread), but
> here's some addition context: it technically didn't make any sense to
> me to have NUMA on 32-bit due too small amount of addressable memory
> (after all, NUMA is about big iron, probably not even VMs), so in the
> first versions of the patchset I've excluded 32-bit (and back then for
> some reason I couldn't even find libnuma i386, but Andres pointed to
> me that it exists, so we re-added it probably just to stay
> consistent). The thread has kind of snowballed since then, but I still
> believe that NUMA on 32-bit does not make a lot of sense.
> 
> Even assuming future shm interleaving one day in future version,
> allocation of small s_b sizes will usually fit a single NUMA node.
> 

Not sure. I thought NUMA doesn't matter very much on 32-bit systems too,
exactly because those systems tend to use small amounts of memory. But
then while investigating this issue I realized even rpi5 has NUMA, in
fact it has a whopping 8 nodes:

debian@raspberry-32:~ $ numactl --hardware
available: 8 nodes (0-7)
node 0 cpus: 0 1 2 3
node 0 size: 981 MB
node 0 free: 882 MB
node 1 cpus: 0 1 2 3
node 1 size: 1007 MB
node 1 free: 936 MB
node 2 cpus: 0 1 2 3
node 2 size: 1007 MB
node 2 free: 936 MB
node 3 cpus: 0 1 2 3
node 3 size: 943 MB
node 3 free: 873 MB
node 4 cpus: 0 1 2 3
node 4 size: 1007 MB
node 4 free: 936 MB
node 5 cpus: 0 1 2 3
node 5 size: 1007 MB
node 5 free: 935 MB
node 6 cpus: 0 1 2 3
node 6 size: 1007 MB
node 6 free: 936 MB
node 7 cpus: 0 1 2 3
node 7 size: 990 MB
node 7 free: 918 MB
node distances:
node   0   1   2   3   4   5   6   7
  0:  10  10  10  10  10  10  10  10
  1:  10  10  10  10  10  10  10  10
  2:  10  10  10  10  10  10  10  10
  3:  10  10  10  10  10  10  10  10
  4:  10  10  10  10  10  10  10  10
  5:  10  10  10  10  10  10  10  10
  6:  10  10  10  10  10  10  10  10
  7:  10  10  10  10  10  10  10  10


This is with the 32-bit system (which AFAICS means 64-bit kernel and
32-bit user space). I'm not saying it's a particularly interesting NUMA
system, considering all the costs are 10, and it's not like it's
critical to get the best performance on rpi5. But it's NUMA, and maybe
there are some other (more practical) systems. I find it interesting
mostly for testing purposes.


regards

-- 
Tomas Vondra






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

* Re: pgsql: Introduce pg_shmem_allocations_numa view
@ 2025-06-25 12:42  Álvaro Herrera <alvherre@kurilemu.de>
  parent: Tomas Vondra <tomas@vondra.me>
  0 siblings, 1 reply; 54+ messages in thread

From: Álvaro Herrera @ 2025-06-25 12:42 UTC (permalink / raw)
  To: Tomas Vondra <tomas@vondra.me>; +Cc: Jakub Wartak <jakub.wartak@enterprisedb.com>; Christoph Berg <myon@debian.org>; Bertrand Drouvot <bertranddrouvot.pg@gmail.com>; Andres Freund <andres@anarazel.de>; Tomas Vondra <tomas.vondra@postgresql.org>; pgsql-hackers@lists.postgresql.org

On 2025-Jun-25, Tomas Vondra wrote:

> Not sure. I thought NUMA doesn't matter very much on 32-bit systems too,
> exactly because those systems tend to use small amounts of memory. But
> then while investigating this issue I realized even rpi5 has NUMA, in
> fact it has a whopping 8 nodes:
> 
> debian@raspberry-32:~ $ numactl --hardware
> available: 8 nodes (0-7)

Interesting.  Mine only shows a single node.

alvherre@amras:~ $ uname -a
Linux amras 6.12.25+rpt-rpi-2712 #1 SMP PREEMPT Debian 1:6.12.25-1+rpt1 (2025-04-30) aarch64 GNU/Linux
alvherre@amras:~ $ sudo numactl --hardware
available: 1 nodes (0)
node 0 cpus: 0 1 2 3
node 0 size: 8051 MB
node 0 free: 202 MB
node distances:
node   0 
  0:  10 
alvherre@amras:~ $ sudo lscpu 
Architecture:             aarch64
  CPU op-mode(s):         32-bit, 64-bit
  Byte Order:             Little Endian
CPU(s):                   4
  On-line CPU(s) list:    0-3
Vendor ID:                ARM
  Model name:             Cortex-A76
    Model:                1
    Thread(s) per core:   1
    Core(s) per cluster:  4
    Socket(s):            -
    Cluster(s):           1
    Stepping:             r4p1
    CPU(s) scaling MHz:   62%
    CPU max MHz:          2400.0000
    CPU min MHz:          1500.0000
    BogoMIPS:             108.00
    Flags:                fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp
                           asimdhp cpuid asimdrdm lrcpc dcpop asimddp
[...]
NUMA:                     
  NUMA node(s):           1
  NUMA node0 CPU(s):      0-3


Did you enable something special on it maybe?

... Oh, I found this:
https://www.jeffgeerling.com/blog/2024/numa-emulation-speeds-pi-5-and-other-improvements
Sounds like you have this in your system and I don't in mine.

-- 
Álvaro Herrera         PostgreSQL Developer  —  https://www.EnterpriseDB.com/





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

* Re: pgsql: Introduce pg_shmem_allocations_numa view
@ 2025-06-25 12:53  Tomas Vondra <tomas@vondra.me>
  parent: Álvaro Herrera <alvherre@kurilemu.de>
  0 siblings, 0 replies; 54+ messages in thread

From: Tomas Vondra @ 2025-06-25 12:53 UTC (permalink / raw)
  To: Álvaro Herrera <alvherre@kurilemu.de>; +Cc: Jakub Wartak <jakub.wartak@enterprisedb.com>; Christoph Berg <myon@debian.org>; Bertrand Drouvot <bertranddrouvot.pg@gmail.com>; Andres Freund <andres@anarazel.de>; Tomas Vondra <tomas.vondra@postgresql.org>; pgsql-hackers@lists.postgresql.org



On 6/25/25 14:42, Álvaro Herrera wrote:
> On 2025-Jun-25, Tomas Vondra wrote:
> 
>> Not sure. I thought NUMA doesn't matter very much on 32-bit systems too,
>> exactly because those systems tend to use small amounts of memory. But
>> then while investigating this issue I realized even rpi5 has NUMA, in
>> fact it has a whopping 8 nodes:
>>
>> debian@raspberry-32:~ $ numactl --hardware
>> available: 8 nodes (0-7)
> 
> Interesting.  Mine only shows a single node.
> 
> alvherre@amras:~ $ uname -a
> Linux amras 6.12.25+rpt-rpi-2712 #1 SMP PREEMPT Debian 1:6.12.25-1+rpt1 (2025-04-30) aarch64 GNU/Linux
> alvherre@amras:~ $ sudo numactl --hardware
> available: 1 nodes (0)
> node 0 cpus: 0 1 2 3
> node 0 size: 8051 MB
> node 0 free: 202 MB
> node distances:
> node   0 
>   0:  10 
> alvherre@amras:~ $ sudo lscpu 
> Architecture:             aarch64
>   CPU op-mode(s):         32-bit, 64-bit
>   Byte Order:             Little Endian
> CPU(s):                   4
>   On-line CPU(s) list:    0-3
> Vendor ID:                ARM
>   Model name:             Cortex-A76
>     Model:                1
>     Thread(s) per core:   1
>     Core(s) per cluster:  4
>     Socket(s):            -
>     Cluster(s):           1
>     Stepping:             r4p1
>     CPU(s) scaling MHz:   62%
>     CPU max MHz:          2400.0000
>     CPU min MHz:          1500.0000
>     BogoMIPS:             108.00
>     Flags:                fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp
>                            asimdhp cpuid asimdrdm lrcpc dcpop asimddp
> [...]
> NUMA:                     
>   NUMA node(s):           1
>   NUMA node0 CPU(s):      0-3
> 
> 
> Did you enable something special on it maybe?
> 
> ... Oh, I found this:
> https://www.jeffgeerling.com/blog/2024/numa-emulation-speeds-pi-5-and-other-improvements
> Sounds like you have this in your system and I don't in mine.
> 

I don't think I had to enable anything special. On the machine running
32-bit RaspberryPi OS I had to install a newer kernel, but I don't
recall doing anything else. I certainly did not apply any kernel patches
or anything like that.

And it seems one of the rpi machines has exactly the same kernel version:

Linux raspberry-64 6.12.25+rpt-rpi-2712 #1 SMP PREEMPT Debian
1:6.12.25-1+rpt1 (2025-04-30) aarch64 GNU/Linux

So I wonder what's going on, why there's no NUMA on your rpi.


-- 
Tomas Vondra

Attachments:

rpi.log (text/x-log, 5.8K) download debian@raspberry-32:~ $ uname -a Linux raspberry-32 6.12.34-v8+ #1889 SMP PREEMPT Mon Jun 23 11:11:06 BST 2025 aarch64 GNU/Linux debian@raspberry-32:~ $ numactl --hardware available: 8 nodes (0-7) node 0 cpus: 0 1 2 3 node 0 size: 981 MB node 0 free: 881 MB node 1 cpus: 0 1 2 3 node 1 size: 1007 MB node 1 free: 935 MB node 2 cpus: 0 1 2 3 node 2 size: 1007 MB node 2 free: 936 MB node 3 cpus: 0 1 2 3 node 3 size: 943 MB node 3 free: 871 MB node 4 cpus: 0 1 2 3 node 4 size: 1007 MB node 4 free: 936 MB node 5 cpus: 0 1 2 3 node 5 size: 1007 MB node 5 free: 935 MB node 6 cpus: 0 1 2 3 node 6 size: 1007 MB node 6 free: 936 MB node 7 cpus: 0 1 2 3 node 7 size: 990 MB node 7 free: 918 MB node distances: node 0 1 2 3 4 5 6 7 0: 10 10 10 10 10 10 10 10 1: 10 10 10 10 10 10 10 10 2: 10 10 10 10 10 10 10 10 3: 10 10 10 10 10 10 10 10 4: 10 10 10 10 10 10 10 10 5: 10 10 10 10 10 10 10 10 6: 10 10 10 10 10 10 10 10 7: 10 10 10 10 10 10 10 10 debian@raspberry-32:~ $ lscpu Architecture: aarch64 Byte Order: Little Endian CPU(s): 4 On-line CPU(s) list: 0-3 Vendor ID: ARM Model name: Cortex-A76 Model: 1 Thread(s) per core: 1 Core(s) per cluster: 4 Socket(s): - Cluster(s): 1 Stepping: r4p1 CPU(s) scaling MHz: 100% CPU max MHz: 2400.0000 CPU min MHz: 1500.0000 BogoMIPS: 108.00 Flags: fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm lrcpc dcpop asimddp Caches (sum of all): L1d: 256 KiB (4 instances) L1i: 256 KiB (4 instances) L2: 2 MiB (4 instances) L3: 2 MiB (1 instance) NUMA: NUMA node(s): 8 NUMA node0 CPU(s): 0-3 NUMA node1 CPU(s): 0-3 NUMA node2 CPU(s): 0-3 NUMA node3 CPU(s): 0-3 NUMA node4 CPU(s): 0-3 NUMA node5 CPU(s): 0-3 NUMA node6 CPU(s): 0-3 NUMA node7 CPU(s): 0-3 Vulnerabilities: Gather data sampling: Not affected Indirect target selection: Not affected Itlb multihit: Not affected L1tf: Not affected Mds: Not affected Meltdown: Not affected Mmio stale data: Not affected Reg file data sampling: Not affected Retbleed: Not affected Spec rstack overflow: Not affected Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl Spectre v1: Mitigation; __user pointer sanitization Spectre v2: Mitigation; CSV2, BHB Srbds: Not affected Tsx async abort: Not affected debian@raspberry-64:~ $ uname -a Linux raspberry-64 6.12.25+rpt-rpi-2712 #1 SMP PREEMPT Debian 1:6.12.25-1+rpt1 (2025-04-30) aarch64 GNU/Linux debian@raspberry-64:~ $ numactl --hardware available: 8 nodes (0-7) node 0 cpus: 0 1 2 3 node 0 size: 992 MB node 0 free: 34 MB node 1 cpus: 0 1 2 3 node 1 size: 1019 MB node 1 free: 126 MB node 2 cpus: 0 1 2 3 node 2 size: 1019 MB node 2 free: 35 MB node 3 cpus: 0 1 2 3 node 3 size: 955 MB node 3 free: 34 MB node 4 cpus: 0 1 2 3 node 4 size: 1019 MB node 4 free: 35 MB node 5 cpus: 0 1 2 3 node 5 size: 1019 MB node 5 free: 41 MB node 6 cpus: 0 1 2 3 node 6 size: 1019 MB node 6 free: 287 MB node 7 cpus: 0 1 2 3 node 7 size: 1014 MB node 7 free: 28 MB node distances: node 0 1 2 3 4 5 6 7 0: 10 10 10 10 10 10 10 10 1: 10 10 10 10 10 10 10 10 2: 10 10 10 10 10 10 10 10 3: 10 10 10 10 10 10 10 10 4: 10 10 10 10 10 10 10 10 5: 10 10 10 10 10 10 10 10 6: 10 10 10 10 10 10 10 10 7: 10 10 10 10 10 10 10 10 debian@raspberry-64:~ $ lscpu Architecture: aarch64 CPU op-mode(s): 32-bit, 64-bit Byte Order: Little Endian CPU(s): 4 On-line CPU(s) list: 0-3 Vendor ID: ARM Model name: Cortex-A76 Model: 1 Thread(s) per core: 1 Core(s) per cluster: 4 Socket(s): - Cluster(s): 1 Stepping: r4p1 CPU(s) scaling MHz: 100% CPU max MHz: 2400.0000 CPU min MHz: 1500.0000 BogoMIPS: 108.00 Flags: fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm lrcpc dcpop asimddp Caches (sum of all): L1d: 256 KiB (4 instances) L1i: 256 KiB (4 instances) L2: 2 MiB (4 instances) L3: 2 MiB (1 instance) NUMA: NUMA node(s): 8 NUMA node0 CPU(s): 0-3 NUMA node1 CPU(s): 0-3 NUMA node2 CPU(s): 0-3 NUMA node3 CPU(s): 0-3 NUMA node4 CPU(s): 0-3 NUMA node5 CPU(s): 0-3 NUMA node6 CPU(s): 0-3 NUMA node7 CPU(s): 0-3 Vulnerabilities: Gather data sampling: Not affected Itlb multihit: Not affected L1tf: Not affected Mds: Not affected Meltdown: Not affected Mmio stale data: Not affected Reg file data sampling: Not affected Retbleed: Not affected Spec rstack overflow: Not affected Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl Spectre v1: Mitigation; __user pointer sanitization Spectre v2: Mitigation; CSV2, BHB Srbds: Not affected Tsx async abort: Not affected
^ permalink raw reply [nested|flat] 54+ messages in thread

* Re: pgsql: Introduce pg_shmem_allocations_numa view
@ 2025-06-26 05:28  Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
  parent: Christoph Berg <myon@debian.org>
  1 sibling, 1 reply; 54+ messages in thread

From: Bertrand Drouvot @ 2025-06-26 05:28 UTC (permalink / raw)
  To: Christoph Berg <myon@debian.org>; +Cc: Tomas Vondra <tomas@vondra.me>; Andres Freund <andres@anarazel.de>; Tomas Vondra <tomas.vondra@postgresql.org>; pgsql-hackers@lists.postgresql.org

Hi,

On Wed, Jun 25, 2025 at 11:00:38AM +0200, Christoph Berg wrote:
> Re: Bertrand Drouvot
> > +/*
> > + * Work around Linux kernel bug in 32-bit compat mode: do_pages_stat() has
> > + * incorrect pointer arithmetic for more than DO_PAGES_STAT_CHUNK_NR pages.
> > + */
> > +#if SIZEOF_SIZE_T == 4
> 
> I was also missing it in my suggested patch draft, but this should
> probably include #ifdef __linux__.

I'm not sure because the workaround is after this part of the code in pg_numa.c:

"
/*
 * At this point we provide support only for Linux thanks to libnuma, but in
 * future support for other platforms e.g. Win32 or FreeBSD might be possible
 * too. For Win32 NUMA APIs see
 * https://learn.microsoft.com/en-us/windows/win32/procthread/numa-support
 */
#ifdef USE_LIBNUMA
"

So I guess that the "#ifdef __linux__" would have to be at a higher level anyway
(should we support NUMA on more than Linux in the future).

Regards,

-- 
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com





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

* Re: pgsql: Introduce pg_shmem_allocations_numa view
@ 2025-06-26 06:00  Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
  parent: Tomas Vondra <tomas@vondra.me>
  1 sibling, 1 reply; 54+ messages in thread

From: Bertrand Drouvot @ 2025-06-26 06:00 UTC (permalink / raw)
  To: Tomas Vondra <tomas@vondra.me>; +Cc: Christoph Berg <myon@debian.org>; Andres Freund <andres@anarazel.de>; Tomas Vondra <tomas.vondra@postgresql.org>; pgsql-hackers@lists.postgresql.org

Hi,

On Tue, Jun 24, 2025 at 10:32:25PM +0200, Tomas Vondra wrote:
> On 6/24/25 17:30, Christoph Berg wrote:
> > Re: Tomas Vondra
> >> If it's a reliable fix, then I guess we can do it like this. But won't
> >> that be a performance penalty on everyone? Or does the system split the
> >> array into 16-element chunks anyway, so this makes no difference?
> > 
> > There's still the overhead of the syscall itself. But no idea how
> > costly it is to have this 16-step loop in user or kernel space.
> > 
> > We could claim that on 32-bit systems, shared_buffers would be smaller
> > anyway, so there the overhead isn't that big. And the step size should
> > be larger (if at all) on 64-bit.
> > 
> >> Anyway, maybe we should start by reporting this to the kernel people. Do
> >> you want me to do that, or shall one of you take care of that? I suppose
> >> that'd be better, as you already wrote a fix / know the code better.
> > 
> > Submitted: https://marc.info/?l=linux-mm&m=175077821909222&w=2
> > 
> 
> Thanks! Now we wait ...

It looks like that the bug is "confirmed" and that it will be fixed:
https://marc.info/?l=linux-kernel&m=175088392116841&w=2

Regards,

-- 
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com





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

* Re: pgsql: Introduce pg_shmem_allocations_numa view
@ 2025-06-26 08:53  Tomas Vondra <tomas@vondra.me>
  parent: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
  0 siblings, 1 reply; 54+ messages in thread

From: Tomas Vondra @ 2025-06-26 08:53 UTC (permalink / raw)
  To: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>; +Cc: Christoph Berg <myon@debian.org>; Andres Freund <andres@anarazel.de>; Tomas Vondra <tomas.vondra@postgresql.org>; pgsql-hackers@lists.postgresql.org

On 6/26/25 08:00, Bertrand Drouvot wrote:
> Hi,
> 
> On Tue, Jun 24, 2025 at 10:32:25PM +0200, Tomas Vondra wrote:
>> On 6/24/25 17:30, Christoph Berg wrote:
>>> Re: Tomas Vondra
>>>> If it's a reliable fix, then I guess we can do it like this. But won't
>>>> that be a performance penalty on everyone? Or does the system split the
>>>> array into 16-element chunks anyway, so this makes no difference?
>>>
>>> There's still the overhead of the syscall itself. But no idea how
>>> costly it is to have this 16-step loop in user or kernel space.
>>>
>>> We could claim that on 32-bit systems, shared_buffers would be smaller
>>> anyway, so there the overhead isn't that big. And the step size should
>>> be larger (if at all) on 64-bit.
>>>
>>>> Anyway, maybe we should start by reporting this to the kernel people. Do
>>>> you want me to do that, or shall one of you take care of that? I suppose
>>>> that'd be better, as you already wrote a fix / know the code better.
>>>
>>> Submitted: https://marc.info/?l=linux-mm&m=175077821909222&w=2
>>>
>>
>> Thanks! Now we wait ...
> 
> It looks like that the bug is "confirmed" and that it will be fixed:
> https://marc.info/?l=linux-kernel&m=175088392116841&w=2
> 

Yay! I like how the first response is "you sent the patch wrong" ;-)


cheers

-- 
Tomas Vondra






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

* Re: pgsql: Introduce pg_shmem_allocations_numa view
@ 2025-06-27 14:52  Tomas Vondra <tomas@vondra.me>
  parent: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
  0 siblings, 1 reply; 54+ messages in thread

From: Tomas Vondra @ 2025-06-27 14:52 UTC (permalink / raw)
  To: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>; Christoph Berg <myon@debian.org>; +Cc: Andres Freund <andres@anarazel.de>; Tomas Vondra <tomas.vondra@postgresql.org>; pgsql-hackers@lists.postgresql.org

Here's three small patches, that should handle the issue


0001 - Adds the batching into pg_numa_query_pages, so that the callers
don't need to do anything.

The batching doesn't seem to cause any performance regression. 32-bit
systems can't use that much memory anyway, and on 64-bit systems the
batch is sufficiently large (1024).


0002 - Silences the valgrind about the memory touching. It replaces the
macro with a static inline function, and adds suppressions for both
32-bit and 64-bits. The 32-bit may be a bit pointless, because on my
rpi5 valgrind produces about a bunch of other stuff anyway. But doesn't
hurt.

The function now looks like this:

  static inline void
  pg_numa_touch_mem_if_required(void *ptr)
  {
      volatile uint64 touch pg_attribute_unused();
      touch = *(volatile uint64 *) ptr;
  }

I did a lot of testing on multiple systems to check replacing the macro
with a static inline function still works - and it seems it does. But if
someone thinks the function won't work, I'd like to know.


0003 - While working on these patches, it occurred to me we could/should
add CHECK_FOR_INTERRUPTS() into the batch loop. This querying can take
quite a bit of time, so letting people to interrupt it seems reasonable.
It wasn't possible with just one call into the kernel, but with the
batching we can add a CFI.


Please, take a look.

regards

-- 
Tomas Vondra

Attachments: download all (3 files, .tar.gz)

0001-Add-batching-when-calling-numa_move_pages.patch (text/x-patch, 3.3K) download From 3d935f62665a18d96e6bec59cb1f3f7cd7daa068 Mon Sep 17 00:00:00 2001 From: Tomas Vondra <tomas@vondra.me> Date: Fri, 27 Jun 2025 12:43:20 +0200 Subject: [PATCH 1/3] Add batching when calling numa_move_pages There's a kernel bug in do_pages_stat(), resulting in numa_move_pages() producing bogus status when querying location of memory pages. The bug only affects systems combining 64-bit kernel and 32-bit user space. This may seem uncommon, but we use such systems for building 32-bit Debian packages (which happens in a 32-bit chroot). This is a long-standing kernel bug (since 2010), affecting pretty much all kernels, so it'll take time until all systems get a fixed kernel. Luckily, we can work around that on our end, by batching the requests the same way as in do_pages_stat(). On 32-bit systems we use batches of 16 pointers, same as do_pages_stat(). 64-bit systems are not affected, so we use a much larger batch of 1024. Reported-by: Christoph Berg <myon@debian.org> Author: Christoph Berg <myon@debian.org> Author: Bertrand Drouvot <bertranddrouvot.pg@gmail.com> Discussion: https://postgr.es/m/aEtDozLmtZddARdB@msg.df7cb.de --- src/include/port/pg_numa.h | 2 +- src/port/pg_numa.c | 45 +++++++++++++++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src/include/port/pg_numa.h b/src/include/port/pg_numa.h index 40f1d324dcf..d707d149a43 100644 --- a/src/include/port/pg_numa.h +++ b/src/include/port/pg_numa.h @@ -29,7 +29,7 @@ extern PGDLLIMPORT int pg_numa_get_max_node(void); #else -#define pg_numa_touch_mem_if_required(ro_volatile_var, ptr) \ +#define pg_numa_touch_mem_if_required(ptr) \ do {} while(0) #endif diff --git a/src/port/pg_numa.c b/src/port/pg_numa.c index 4b487a2a4e8..54ab9c70d56 100644 --- a/src/port/pg_numa.c +++ b/src/port/pg_numa.c @@ -29,6 +29,19 @@ #include <numa.h> #include <numaif.h> +/* + * numa_move_pages() batch size, has to be <= 16 to work around a kernel bug + * in do_pages_stat() (chunked by DO_PAGES_STAT_CHUNK_NR). By using the same + * batch size, we make it work even on unfixed kernels. + * + * 64-bit system are not affected by the bug, and so use much larger batches. + */ +#if SIZEOF_SIZE_T == 4 +#define NUMA_QUERY_BATCH_SIZE 16 +#else +#define NUMA_QUERY_BATCH_SIZE 1024 +#endif + /* libnuma requires initialization as per numa(3) on Linux */ int pg_numa_init(void) @@ -46,7 +59,37 @@ pg_numa_init(void) int pg_numa_query_pages(int pid, unsigned long count, void **pages, int *status) { - return numa_move_pages(pid, count, pages, NULL, status, 0); + unsigned long next = 0; + int ret = 0; + + /* + * Batch pointers passed to numa_move_pages to NUMA_QUERY_BATCH_SIZE + * items, to work around a kernel bug in do_pages_stat(). + */ + while (next < count) + { + unsigned long count_batch = Min(count - next, + NUMA_QUERY_BATCH_SIZE); + + /* + * Bail out if any of the batches errors out (ret<0). We ignore + * (ret>0) which is used to return number of nonmigrated pages, + * but we're not migrating any pages here. + */ + ret = numa_move_pages(pid, count_batch, &pages[next], NULL, &status[next], 0); + if (ret < 0) + { + /* plain error, return as is */ + return ret; + } + + next += count_batch; + } + + /* should have consumed the input array exactly */ + Assert(next == count); + + return 0; } int -- 2.49.0
0002-Silence-valgrind-about-pg_numa_touch_mem_if_required.patch (text/x-patch, 3.9K) download From 613a85c50a17574fcd34689582ce00c879187463 Mon Sep 17 00:00:00 2001 From: Tomas Vondra <tomas@vondra.me> Date: Fri, 27 Jun 2025 12:47:38 +0200 Subject: [PATCH 2/3] Silence valgrind about pg_numa_touch_mem_if_required When querying NUMA status of pages in shared memory, we need to touch the memory first to get valid results. This may trigger valgrind reports, because some of the memory (e.g. unpinned buffers) may be marked as noaccess. Solved by adding a valgrind suppresion, to ignore this. An alternative would be to adjust the access/noaccess status before touching the memory, but that seems far too invasive - it would require all those pages to have detailed knowledge of what the memory stores. The pg_numa_touch_mem_if_required() macro is replaced with a plain function. Macros are invisible to suppressions, so it'd have to suppress reports for the caller - e.g. pg_get_shmem_allocations_numa(). Which means we'd suppress reports for the whole function. Reviewed-by: Christoph Berg <myon@debian.org> Reviewed-by: Bertrand Drouvot <bertranddrouvot.pg@gmail.com> Discussion: https://postgr.es/m/aEtDozLmtZddARdB@msg.df7cb.de --- contrib/pg_buffercache/pg_buffercache_pages.c | 3 +-- src/backend/storage/ipc/shmem.c | 4 +--- src/include/port/pg_numa.h | 8 ++++++-- src/tools/valgrind.supp | 14 ++++++++++++++ 4 files changed, 22 insertions(+), 7 deletions(-) diff --git a/contrib/pg_buffercache/pg_buffercache_pages.c b/contrib/pg_buffercache/pg_buffercache_pages.c index 4b007f6e1b0..ae0291e6e96 100644 --- a/contrib/pg_buffercache/pg_buffercache_pages.c +++ b/contrib/pg_buffercache/pg_buffercache_pages.c @@ -320,7 +320,6 @@ pg_buffercache_numa_pages(PG_FUNCTION_ARGS) uint64 os_page_count; int pages_per_buffer; int max_entries; - volatile uint64 touch pg_attribute_unused(); char *startptr, *endptr; @@ -375,7 +374,7 @@ pg_buffercache_numa_pages(PG_FUNCTION_ARGS) /* Only need to touch memory once per backend process lifetime */ if (firstNumaTouch) - pg_numa_touch_mem_if_required(touch, ptr); + pg_numa_touch_mem_if_required(ptr); } Assert(idx == os_page_count); diff --git a/src/backend/storage/ipc/shmem.c b/src/backend/storage/ipc/shmem.c index c9ae3b45b76..ca3656fc76f 100644 --- a/src/backend/storage/ipc/shmem.c +++ b/src/backend/storage/ipc/shmem.c @@ -679,12 +679,10 @@ pg_get_shmem_allocations_numa(PG_FUNCTION_ARGS) */ for (i = 0; i < shm_ent_page_count; i++) { - volatile uint64 touch pg_attribute_unused(); - page_ptrs[i] = startptr + (i * os_page_size); if (firstNumaTouch) - pg_numa_touch_mem_if_required(touch, page_ptrs[i]); + pg_numa_touch_mem_if_required(page_ptrs[i]); CHECK_FOR_INTERRUPTS(); } diff --git a/src/include/port/pg_numa.h b/src/include/port/pg_numa.h index d707d149a43..6c8b7103cc3 100644 --- a/src/include/port/pg_numa.h +++ b/src/include/port/pg_numa.h @@ -24,8 +24,12 @@ extern PGDLLIMPORT int pg_numa_get_max_node(void); * This is required on Linux, before pg_numa_query_pages() as we * need to page-fault before move_pages(2) syscall returns valid results. */ -#define pg_numa_touch_mem_if_required(ro_volatile_var, ptr) \ - ro_volatile_var = *(volatile uint64 *) ptr +static inline void +pg_numa_touch_mem_if_required(void *ptr) +{ + volatile uint64 touch pg_attribute_unused(); + touch = *(volatile uint64 *) ptr; +} #else diff --git a/src/tools/valgrind.supp b/src/tools/valgrind.supp index 7ea464c8094..2ad5b81526d 100644 --- a/src/tools/valgrind.supp +++ b/src/tools/valgrind.supp @@ -180,3 +180,17 @@ Memcheck:Cond fun:PyObject_Realloc } + +# NUMA introspection requires touching memory first, and some of it may +# be marked as noacess (e.g. unpinned buffers). So just ignore that. +{ + pg_numa_touch_mem_if_required + Memcheck:Addr4 + fun:pg_numa_touch_mem_if_required +} + +{ + pg_numa_touch_mem_if_required + Memcheck:Addr8 + fun:pg_numa_touch_mem_if_required +} -- 2.49.0
0003-Add-CHECK_FOR_INTERRUPTS-into-pg_numa_query_pages.patch (text/x-patch, 1.2K) download From add3768156d05382b2de1dd64d8c420e8c50f92b Mon Sep 17 00:00:00 2001 From: Tomas Vondra <tomas@vondra.me> Date: Fri, 27 Jun 2025 16:42:43 +0200 Subject: [PATCH 3/3] Add CHECK_FOR_INTERRUPTS into pg_numa_query_pages Querying the NUMA status can be quite time consuming. Thanks to the batching, we can do CHECK_FOR_INTERRUPTS(), to allow users aborting the execution. Reviewed-by: Christoph Berg <myon@debian.org> Reviewed-by: Bertrand Drouvot <bertranddrouvot.pg@gmail.com> Discussion: https://postgr.es/m/aEtDozLmtZddARdB@msg.df7cb.de --- src/port/pg_numa.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/port/pg_numa.c b/src/port/pg_numa.c index 54ab9c70d56..f76876b2906 100644 --- a/src/port/pg_numa.c +++ b/src/port/pg_numa.c @@ -16,6 +16,7 @@ #include "c.h" #include <unistd.h> +#include "miscadmin.h" #include "port/pg_numa.h" /* @@ -71,6 +72,8 @@ pg_numa_query_pages(int pid, unsigned long count, void **pages, int *status) unsigned long count_batch = Min(count - next, NUMA_QUERY_BATCH_SIZE); + CHECK_FOR_INTERRUPTS(); + /* * Bail out if any of the batches errors out (ret<0). We ignore * (ret>0) which is used to return number of nonmigrated pages, -- 2.49.0
^ permalink raw reply [nested|flat] 54+ messages in thread

* Re: pgsql: Introduce pg_shmem_allocations_numa view
@ 2025-06-27 17:33  Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
  parent: Tomas Vondra <tomas@vondra.me>
  0 siblings, 1 reply; 54+ messages in thread

From: Bertrand Drouvot @ 2025-06-27 17:33 UTC (permalink / raw)
  To: Tomas Vondra <tomas@vondra.me>; +Cc: Christoph Berg <myon@debian.org>; Andres Freund <andres@anarazel.de>; Tomas Vondra <tomas.vondra@postgresql.org>; pgsql-hackers@lists.postgresql.org

Hi,

On Fri, Jun 27, 2025 at 04:52:08PM +0200, Tomas Vondra wrote:
> Here's three small patches, that should handle the issue

Thanks for the patches!

> 0001 - Adds the batching into pg_numa_query_pages, so that the callers
> don't need to do anything.
> 
> The batching doesn't seem to cause any performance regression. 32-bit
> systems can't use that much memory anyway, and on 64-bit systems the
> batch is sufficiently large (1024).

=== 1

-#define pg_numa_touch_mem_if_required(ro_volatile_var, ptr) \
+#define pg_numa_touch_mem_if_required(ptr) \

Looks unrelated, should be in 0002?

=== 2

I thought that it would be better to provide a batch size only in the 32-bit
case (see [1]), but I now think it makes sense to also provide (a larger) one
for non 32-bit (as you did) due to the CFI added in 0003 (as it's also good to
have it for non 32-bit).

> 0002 - Silences the valgrind about the memory touching. It replaces the
> macro with a static inline function, and adds suppressions for both
> 32-bit and 64-bits. The 32-bit may be a bit pointless, because on my
> rpi5 valgrind produces about a bunch of other stuff anyway. But doesn't
> hurt.
> 
> The function now looks like this:
> 
>   static inline void
>   pg_numa_touch_mem_if_required(void *ptr)
>   {
>       volatile uint64 touch pg_attribute_unused();
>       touch = *(volatile uint64 *) ptr;
>   }
> 
> I did a lot of testing on multiple systems to check replacing the macro
> with a static inline function still works - and it seems it does. But if
> someone thinks the function won't work, I'd like to know.

LGTM.

> 0003 - While working on these patches, it occurred to me we could/should
> add CHECK_FOR_INTERRUPTS() into the batch loop. This querying can take
> quite a bit of time, so letting people to interrupt it seems reasonable.
> It wasn't possible with just one call into the kernel, but with the
> batching we can add a CFI.

Yeah, LGTM.

[1]: https://www.postgresql.org/message-id/aFuRoUieUVh%2BpMfZ%40ip-10-97-1-34.eu-west-3.compute.internal 

Regards,

-- 
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com





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

* Re: pgsql: Introduce pg_shmem_allocations_numa view
@ 2025-06-30 18:56  Tomas Vondra <tomas@vondra.me>
  parent: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
  0 siblings, 1 reply; 54+ messages in thread

From: Tomas Vondra @ 2025-06-30 18:56 UTC (permalink / raw)
  To: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>; +Cc: Christoph Berg <myon@debian.org>; Andres Freund <andres@anarazel.de>; Tomas Vondra <tomas.vondra@postgresql.org>; pgsql-hackers@lists.postgresql.org

On 6/27/25 19:33, Bertrand Drouvot wrote:
> Hi,
> 
> On Fri, Jun 27, 2025 at 04:52:08PM +0200, Tomas Vondra wrote:
>> Here's three small patches, that should handle the issue
> 
> Thanks for the patches!
> 
>> 0001 - Adds the batching into pg_numa_query_pages, so that the callers
>> don't need to do anything.
>>
>> The batching doesn't seem to cause any performance regression. 32-bit
>> systems can't use that much memory anyway, and on 64-bit systems the
>> batch is sufficiently large (1024).
> 
> === 1
> 
> -#define pg_numa_touch_mem_if_required(ro_volatile_var, ptr) \
> +#define pg_numa_touch_mem_if_required(ptr) \
> 
> Looks unrelated, should be in 0002?
> 

Of course, I merged it into the wrong patch. Here's a v2 that fixes
this, and also reworded some of the comments and commit messages a
little bit.

In particular it now uses "chunking" instead of "batching". I believe
bathing is "combining multiple requests into a single one", but we're
doing exactly the opposite - splitting a large request into smaller
ones. Which is what "chunking" does.

> === 2
> 
> I thought that it would be better to provide a batch size only in the 32-bit
> case (see [1]), but I now think it makes sense to also provide (a larger) one
> for non 32-bit (as you did) due to the CFI added in 0003 (as it's also good to
> have it for non 32-bit).
> 

Agreed, I think the CFI is a good thing to have.

>> 0002 - Silences the valgrind about the memory touching. It replaces the
>> macro with a static inline function, and adds suppressions for both
>> 32-bit and 64-bits. The 32-bit may be a bit pointless, because on my
>> rpi5 valgrind produces about a bunch of other stuff anyway. But doesn't
>> hurt.
>>
>> The function now looks like this:
>>
>>   static inline void
>>   pg_numa_touch_mem_if_required(void *ptr)
>>   {
>>       volatile uint64 touch pg_attribute_unused();
>>       touch = *(volatile uint64 *) ptr;
>>   }
>>
>> I did a lot of testing on multiple systems to check replacing the macro
>> with a static inline function still works - and it seems it does. But if
>> someone thinks the function won't work, I'd like to know.
> 
> LGTM.
> >> 0003 - While working on these patches, it occurred to me we could/should
>> add CHECK_FOR_INTERRUPTS() into the batch loop. This querying can take
>> quite a bit of time, so letting people to interrupt it seems reasonable.
>> It wasn't possible with just one call into the kernel, but with the
>> batching we can add a CFI.
> 
> Yeah, LGTM.
> 

Thanks!

I plan to push this tomorrow morning.

-- 
Tomas Vondra

Attachments: download all (3 files, .tar.gz)

v2-0001-Limit-the-size-of-numa_move_pages-requests.patch (text/x-patch, 3.8K) download From d5dd0631c5c5233cabe2000f310a3f902230a284 Mon Sep 17 00:00:00 2001 From: Tomas Vondra <tomas@vondra.me> Date: Fri, 27 Jun 2025 12:43:20 +0200 Subject: [PATCH v2 1/3] Limit the size of numa_move_pages requests There's a kernel bug in do_pages_stat(), affecting systems combining 64-bit kernel and 32-bit user space. The function splits the request into chunks of 16 pointers, but forgets the pointers are 32-bit when advancing to the next chunk. Some of the pointers get skipped, and memory after the array is interpreted as pointers. The result is that the produced status of memory pages is mostly bogus. Systems combining 64-bit and 32-bit environments like this might seem rare, but that's not the case - all 32-bit Debian packages are built in a 32-bit chroot on a system with 64-bit kernel. This is a long-standing kernel bug (since 2010), affecting pretty much all kernels, so it'll take time until all systems get a fixed kernel. Luckily, we can work around the issue by chunking the requests the same way do_pages_stat() does, at least on affected systems. We don't know what kernel a 32-bit build will run on, so all 32-bit builds use chunks of 16 elements (the largest chunk before hitting the issue). 64-bit builds are not affected by this issue, and so could work without the chunking. But chunking has other advantages, so we apply chunking even for 64-bit builds, with chunks of 1024 elements. Reported-by: Christoph Berg <myon@debian.org> Author: Christoph Berg <myon@debian.org> Author: Bertrand Drouvot <bertranddrouvot.pg@gmail.com> Discussion: https://postgr.es/m/aEtDozLmtZddARdB@msg.df7cb.de --- src/port/pg_numa.c | 50 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/src/port/pg_numa.c b/src/port/pg_numa.c index 4b487a2a4e8..d5935207d0a 100644 --- a/src/port/pg_numa.c +++ b/src/port/pg_numa.c @@ -29,6 +29,19 @@ #include <numa.h> #include <numaif.h> +/* + * numa_move_pages() chunk size, has to be <= 16 to work around a kernel bug + * in do_pages_stat() (chunked by DO_PAGES_STAT_CHUNK_NR). By using the same + * chunk size, we make it work even on unfixed kernels. + * + * 64-bit system are not affected by the bug, and so use much larger chunks. + */ +#if SIZEOF_SIZE_T == 4 +#define NUMA_QUERY_CHUNK_SIZE 16 +#else +#define NUMA_QUERY_CHUNK_SIZE 1024 +#endif + /* libnuma requires initialization as per numa(3) on Linux */ int pg_numa_init(void) @@ -42,11 +55,46 @@ pg_numa_init(void) * We use move_pages(2) syscall here - instead of get_mempolicy(2) - as the * first one allows us to batch and query about many memory pages in one single * giant system call that is way faster. + * + * We call numa_move_pages() for smaller chunks of the whole array. The first + * reason is to work around a kernel bug, but also to allow interrupting the + * query between the calls (for many pointers processing the whole array can + * take a lot of time). */ int pg_numa_query_pages(int pid, unsigned long count, void **pages, int *status) { - return numa_move_pages(pid, count, pages, NULL, status, 0); + unsigned long next = 0; + int ret = 0; + + /* + * Chunk pointers passed to numa_move_pages to NUMA_QUERY_CHUNK_SIZE + * items, to work around a kernel bug in do_pages_stat(). + */ + while (next < count) + { + unsigned long count_chunk = Min(count - next, + NUMA_QUERY_CHUNK_SIZE); + + /* + * Bail out if any of the chunks errors out (ret<0). We ignore + * (ret>0) which is used to return number of nonmigrated pages, + * but we're not migrating any pages here. + */ + ret = numa_move_pages(pid, count_chunk, &pages[next], NULL, &status[next], 0); + if (ret < 0) + { + /* plain error, return as is */ + return ret; + } + + next += count_chunk; + } + + /* should have consumed the input array exactly */ + Assert(next == count); + + return 0; } int -- 2.49.0
v2-0002-Silence-valgrind-about-pg_numa_touch_mem_if_requi.patch (text/x-patch, 4.1K) download From 9cb095a2061deea7f0a781177f4c89928392d7ce Mon Sep 17 00:00:00 2001 From: Tomas Vondra <tomas@vondra.me> Date: Fri, 27 Jun 2025 12:47:38 +0200 Subject: [PATCH v2 2/3] Silence valgrind about pg_numa_touch_mem_if_required When querying NUMA status of pages in shared memory, we need to touch the memory first to get valid results. This may trigger valgrind reports, because some of the memory (e.g. unpinned buffers) may be marked as noaccess. Solved by adding a valgrind suppresion. An alternative would be to adjust the access/noaccess status before touching the memory, but that seems far too invasive. It would require all those places to have detailed knowledge of what the shared memory stores. The pg_numa_touch_mem_if_required() macro is replaced with a function. Macros are invisible to suppressions, so it'd have to suppress reports for the caller - e.g. pg_get_shmem_allocations_numa(). So we'd suppress reports for the whole function, and that seems to heavy-handed. It might easily hide other valid issues. Reviewed-by: Christoph Berg <myon@debian.org> Reviewed-by: Bertrand Drouvot <bertranddrouvot.pg@gmail.com> Discussion: https://postgr.es/m/aEtDozLmtZddARdB@msg.df7cb.de --- contrib/pg_buffercache/pg_buffercache_pages.c | 3 +-- src/backend/storage/ipc/shmem.c | 4 +--- src/include/port/pg_numa.h | 10 +++++++--- src/tools/valgrind.supp | 14 ++++++++++++++ 4 files changed, 23 insertions(+), 8 deletions(-) diff --git a/contrib/pg_buffercache/pg_buffercache_pages.c b/contrib/pg_buffercache/pg_buffercache_pages.c index 4b007f6e1b0..ae0291e6e96 100644 --- a/contrib/pg_buffercache/pg_buffercache_pages.c +++ b/contrib/pg_buffercache/pg_buffercache_pages.c @@ -320,7 +320,6 @@ pg_buffercache_numa_pages(PG_FUNCTION_ARGS) uint64 os_page_count; int pages_per_buffer; int max_entries; - volatile uint64 touch pg_attribute_unused(); char *startptr, *endptr; @@ -375,7 +374,7 @@ pg_buffercache_numa_pages(PG_FUNCTION_ARGS) /* Only need to touch memory once per backend process lifetime */ if (firstNumaTouch) - pg_numa_touch_mem_if_required(touch, ptr); + pg_numa_touch_mem_if_required(ptr); } Assert(idx == os_page_count); diff --git a/src/backend/storage/ipc/shmem.c b/src/backend/storage/ipc/shmem.c index c9ae3b45b76..ca3656fc76f 100644 --- a/src/backend/storage/ipc/shmem.c +++ b/src/backend/storage/ipc/shmem.c @@ -679,12 +679,10 @@ pg_get_shmem_allocations_numa(PG_FUNCTION_ARGS) */ for (i = 0; i < shm_ent_page_count; i++) { - volatile uint64 touch pg_attribute_unused(); - page_ptrs[i] = startptr + (i * os_page_size); if (firstNumaTouch) - pg_numa_touch_mem_if_required(touch, page_ptrs[i]); + pg_numa_touch_mem_if_required(page_ptrs[i]); CHECK_FOR_INTERRUPTS(); } diff --git a/src/include/port/pg_numa.h b/src/include/port/pg_numa.h index 40f1d324dcf..6c8b7103cc3 100644 --- a/src/include/port/pg_numa.h +++ b/src/include/port/pg_numa.h @@ -24,12 +24,16 @@ extern PGDLLIMPORT int pg_numa_get_max_node(void); * This is required on Linux, before pg_numa_query_pages() as we * need to page-fault before move_pages(2) syscall returns valid results. */ -#define pg_numa_touch_mem_if_required(ro_volatile_var, ptr) \ - ro_volatile_var = *(volatile uint64 *) ptr +static inline void +pg_numa_touch_mem_if_required(void *ptr) +{ + volatile uint64 touch pg_attribute_unused(); + touch = *(volatile uint64 *) ptr; +} #else -#define pg_numa_touch_mem_if_required(ro_volatile_var, ptr) \ +#define pg_numa_touch_mem_if_required(ptr) \ do {} while(0) #endif diff --git a/src/tools/valgrind.supp b/src/tools/valgrind.supp index 7ea464c8094..2ad5b81526d 100644 --- a/src/tools/valgrind.supp +++ b/src/tools/valgrind.supp @@ -180,3 +180,17 @@ Memcheck:Cond fun:PyObject_Realloc } + +# NUMA introspection requires touching memory first, and some of it may +# be marked as noacess (e.g. unpinned buffers). So just ignore that. +{ + pg_numa_touch_mem_if_required + Memcheck:Addr4 + fun:pg_numa_touch_mem_if_required +} + +{ + pg_numa_touch_mem_if_required + Memcheck:Addr8 + fun:pg_numa_touch_mem_if_required +} -- 2.49.0
v2-0003-Add-CHECK_FOR_INTERRUPTS-into-pg_numa_query_pages.patch (text/x-patch, 1.4K) download From 690904468235f7093214e1323714d14b8a22a6ca Mon Sep 17 00:00:00 2001 From: Tomas Vondra <tomas@vondra.me> Date: Fri, 27 Jun 2025 16:42:43 +0200 Subject: [PATCH v2 3/3] Add CHECK_FOR_INTERRUPTS into pg_numa_query_pages Querying the NUMA status can be quite time consuming, especially with large shared buffers. 8cc139bec34a simply called numa_move_pages(), which meant we have to wait for the syscall to complete. But with the chunking, introduced to work around the do_pages_stat() bug, we can do CHECK_FOR_INTERRUPTS() after each chunk, to allow users aborting the execution. Reviewed-by: Christoph Berg <myon@debian.org> Reviewed-by: Bertrand Drouvot <bertranddrouvot.pg@gmail.com> Discussion: https://postgr.es/m/aEtDozLmtZddARdB@msg.df7cb.de --- src/port/pg_numa.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/port/pg_numa.c b/src/port/pg_numa.c index d5935207d0a..c65f22020ea 100644 --- a/src/port/pg_numa.c +++ b/src/port/pg_numa.c @@ -16,6 +16,7 @@ #include "c.h" #include <unistd.h> +#include "miscadmin.h" #include "port/pg_numa.h" /* @@ -76,6 +77,8 @@ pg_numa_query_pages(int pid, unsigned long count, void **pages, int *status) unsigned long count_chunk = Min(count - next, NUMA_QUERY_CHUNK_SIZE); + CHECK_FOR_INTERRUPTS(); + /* * Bail out if any of the chunks errors out (ret<0). We ignore * (ret>0) which is used to return number of nonmigrated pages, -- 2.49.0
^ permalink raw reply [nested|flat] 54+ messages in thread

* Re: pgsql: Introduce pg_shmem_allocations_numa view
@ 2025-07-01 04:06  Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
  parent: Tomas Vondra <tomas@vondra.me>
  0 siblings, 1 reply; 54+ messages in thread

From: Bertrand Drouvot @ 2025-07-01 04:06 UTC (permalink / raw)
  To: Tomas Vondra <tomas@vondra.me>; +Cc: Christoph Berg <myon@debian.org>; Andres Freund <andres@anarazel.de>; Tomas Vondra <tomas.vondra@postgresql.org>; pgsql-hackers@lists.postgresql.org

Hi,

On Mon, Jun 30, 2025 at 08:56:43PM +0200, Tomas Vondra wrote:
> In particular it now uses "chunking" instead of "batching". I believe
> bathing is "combining multiple requests into a single one", but we're
> doing exactly the opposite - splitting a large request into smaller
> ones. Which is what "chunking" does.

I do agree that "chuncking" is more appropriate here.

> I plan to push this tomorrow morning.

Thanks!

LGTM, just 2 nit about the commit messages:

For 0001:

Is it worth to add a link to the Kernel Bug report or mentioned it can be
found in the discussion?

For 0003:

"
But with the chunking, introduced to work around the do_pages_stat()
bug"

Do you have in mind to quote the hex commit object name that will be generated
by 0001?

Regards,

-- 
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com





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

* Re: pgsql: Introduce pg_shmem_allocations_numa view
@ 2025-07-01 11:03  Tomas Vondra <tomas@vondra.me>
  parent: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
  0 siblings, 2 replies; 54+ messages in thread

From: Tomas Vondra @ 2025-07-01 11:03 UTC (permalink / raw)
  To: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>; +Cc: Christoph Berg <myon@debian.org>; Andres Freund <andres@anarazel.de>; Tomas Vondra <tomas.vondra@postgresql.org>; pgsql-hackers@lists.postgresql.org


On 7/1/25 06:06, Bertrand Drouvot wrote:
> Hi,
> 
> On Mon, Jun 30, 2025 at 08:56:43PM +0200, Tomas Vondra wrote:
>> In particular it now uses "chunking" instead of "batching". I believe
>> bathing is "combining multiple requests into a single one", but we're
>> doing exactly the opposite - splitting a large request into smaller
>> ones. Which is what "chunking" does.
> 
> I do agree that "chuncking" is more appropriate here.
> 
>> I plan to push this tomorrow morning.
> 
> Thanks!
> 
> LGTM, just 2 nit about the commit messages:
> 
> For 0001:
> 
> Is it worth to add a link to the Kernel Bug report or mentioned it can be
> found in the discussion?
> 
> For 0003:
> 
> "
> But with the chunking, introduced to work around the do_pages_stat()
> bug"
> 
> Do you have in mind to quote the hex commit object name that will be generated
> by 0001?
> 

Thanks! Pushed, with both adjustments (link to kernel thread, adding the
commit hash).

But damn it, right after pushing I noticed two typos in the second
commit message :-/

-- 
Tomas Vondra






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

* Re: pgsql: Introduce pg_shmem_allocations_numa view
@ 2025-07-21 20:52  Christoph Berg <myon@debian.org>
  parent: Tomas Vondra <tomas@vondra.me>
  0 siblings, 1 reply; 54+ messages in thread

From: Christoph Berg @ 2025-07-21 20:52 UTC (permalink / raw)
  To: Tomas Vondra <tomas@vondra.me>; +Cc: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>; Andres Freund <andres@anarazel.de>; Tomas Vondra <tomas.vondra@postgresql.org>; pgsql-hackers@lists.postgresql.org

Re: Tomas Vondra
> >>> Submitted: https://marc.info/?l=linux-mm&m=175077821909222&w=2
> >>>
> >>
> >> Thanks! Now we wait ...
> > 
> > It looks like that the bug is "confirmed" and that it will be fixed:
> > https://marc.info/?l=linux-kernel&m=175088392116841&w=2

If I'm reading the Linux git log correctly, the fix was merged into
Linux 6.16-rc7. Yay :)

> Yay! I like how the first response is "you sent the patch wrong" ;-)

I would have thought that coming from two major projects that use
email extensively (Debian, PostgreSQL), I would navigate that process
better. But it worked in the end...

Christoph





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

* Re: pgsql: Introduce pg_shmem_allocations_numa view
@ 2025-07-22 07:01  Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
  parent: Christoph Berg <myon@debian.org>
  0 siblings, 0 replies; 54+ messages in thread

From: Bertrand Drouvot @ 2025-07-22 07:01 UTC (permalink / raw)
  To: Christoph Berg <myon@debian.org>; +Cc: Tomas Vondra <tomas@vondra.me>; Andres Freund <andres@anarazel.de>; Tomas Vondra <tomas.vondra@postgresql.org>; pgsql-hackers@lists.postgresql.org

Hi,

On Mon, Jul 21, 2025 at 10:52:12PM +0200, Christoph Berg wrote:
> Re: Tomas Vondra
> > >>> Submitted: https://marc.info/?l=linux-mm&m=175077821909222&w=2
> > >>>
> > >>
> > >> Thanks! Now we wait ...
> > > 
> > > It looks like that the bug is "confirmed" and that it will be fixed:
> > > https://marc.info/?l=linux-kernel&m=175088392116841&w=2
> 
> If I'm reading the Linux git log correctly, the fix was merged into
> Linux 6.16-rc7. Yay :)

Yeah! ;-)  https://github.com/torvalds/linux/commit/10d04c26ab2b7

> > Yay! I like how the first response is "you sent the patch wrong" ;-)
> 
> I would have thought that coming from two major projects that use
> email extensively (Debian, PostgreSQL), I would navigate that process
> better. But it worked in the end...

Indeed, thanks!

Regards,

-- 
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com





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

* Re: pgsql: Introduce pg_shmem_allocations_numa view
@ 2025-09-11 11:36  Christoph Berg <myon@debian.org>
  parent: Tomas Vondra <tomas@vondra.me>
  1 sibling, 1 reply; 54+ messages in thread

From: Christoph Berg @ 2025-09-11 11:36 UTC (permalink / raw)
  To: Tomas Vondra <tomas@vondra.me>; +Cc: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>; Andres Freund <andres@anarazel.de>; Tomas Vondra <tomas.vondra@postgresql.org>; pgsql-hackers@lists.postgresql.org

Re: Tomas Vondra
> Thanks! Pushed, with both adjustments (link to kernel thread, adding the
> commit hash).

The PG18 Debian package is still carrying the contrib complement of
this patch (see attachment).

Should that be addressed before 18.0?

Christoph

Attachments:

move-pages32 (text/plain, 2.3K) download Work around a Linux bug in move_pages In 32-bit mode on 64-bit kernels, move_pages() does not correctly advance to the next chunk. Work around by not asking for more than 16 pages at once so move_pages() internal loop is not executed more than once. https://www.postgresql.org/message-id/flat/a3a4fe3d-1a80-4e03-aa8e-150ee15f6c35%40vondra.me#6abe7eaa802b5b07bb70cc3229e63a9f https://marc.info/?l=linux-mm&m=175077821909222&w=2 --- a/contrib/pg_buffercache/pg_buffercache_pages.c +++ b/contrib/pg_buffercache/pg_buffercache_pages.c @@ -390,8 +390,15 @@ pg_buffercache_numa_pages(PG_FUNCTION_AR memset(os_page_status, 0xff, sizeof(int) * os_page_count); /* Query NUMA status for all the pointers */ - if (pg_numa_query_pages(0, os_page_count, os_page_ptrs, os_page_status) == -1) - elog(ERROR, "failed NUMA pages inquiry: %m"); +#define NUMA_QUERY_CHUNK_SIZE 16 /* has to be <= DO_PAGES_STAT_CHUNK_NR (do_pages_stat())*/ + for (uint64 chunk_start = 0; chunk_start < os_page_count; chunk_start += NUMA_QUERY_CHUNK_SIZE) { + uint64 chunk_size = Min(NUMA_QUERY_CHUNK_SIZE, os_page_count - chunk_start); + + if (pg_numa_query_pages(0, chunk_size, &os_page_ptrs[chunk_start], + &os_page_status[chunk_start]) == -1) + elog(ERROR, "failed NUMA pages inquiry status: %m"); + } +#undef NUMA_QUERY_CHUNK_SIZE /* Initialize the multi-call context, load entries about buffers */ --- a/src/backend/storage/ipc/shmem.c +++ b/src/backend/storage/ipc/shmem.c @@ -689,8 +689,15 @@ pg_get_shmem_allocations_numa(PG_FUNCTIO CHECK_FOR_INTERRUPTS(); } - if (pg_numa_query_pages(0, shm_ent_page_count, page_ptrs, pages_status) == -1) - elog(ERROR, "failed NUMA pages inquiry status: %m"); +#define NUMA_QUERY_CHUNK_SIZE 16 /* has to be <= DO_PAGES_STAT_CHUNK_NR (do_pages_stat())*/ + for (uint64 chunk_start = 0; chunk_start < shm_ent_page_count; chunk_start += NUMA_QUERY_CHUNK_SIZE) { + uint64 chunk_size = Min(NUMA_QUERY_CHUNK_SIZE, shm_ent_page_count - chunk_start); + + if (pg_numa_query_pages(0, chunk_size, &page_ptrs[chunk_start], + &pages_status[chunk_start]) == -1) + elog(ERROR, "failed NUMA pages inquiry status: %m"); + } +#undef NUMA_QUERY_CHUNK_SIZE /* Count number of NUMA nodes used for this shared memory entry */ memset(nodes, 0, sizeof(Size) * (max_nodes + 1));
^ permalink raw reply [nested|flat] 54+ messages in thread

* Re: pgsql: Introduce pg_shmem_allocations_numa view
@ 2025-09-11 11:39  Christoph Berg <myon@debian.org>
  parent: Christoph Berg <myon@debian.org>
  0 siblings, 0 replies; 54+ messages in thread

From: Christoph Berg @ 2025-09-11 11:39 UTC (permalink / raw)
  To: Tomas Vondra <tomas@vondra.me>; +Cc: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>; Andres Freund <andres@anarazel.de>; Tomas Vondra <tomas.vondra@postgresql.org>; pgsql-hackers@lists.postgresql.org

Re: To Tomas Vondra
> The PG18 Debian package is still carrying the contrib complement of
> this patch (see attachment).

Ah sorry, I was confused here. I had assumed that the patch is
required as long as it doesn't conflict, but it doesn't conflict since
the problem was fixed inside pg_numa_query_pages() in git, while the
workaround was outside.

Christoph





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

* Re: pgsql: Introduce pg_shmem_allocations_numa view
@ 2026-02-12 14:21  Heikki Linnakangas <hlinnaka@iki.fi>
  parent: Tomas Vondra <tomas@vondra.me>
  1 sibling, 1 reply; 54+ messages in thread

From: Heikki Linnakangas @ 2026-02-12 14:21 UTC (permalink / raw)
  To: Tomas Vondra <tomas@vondra.me>; Bertrand Drouvot <bertranddrouvot.pg@gmail.com>; +Cc: Christoph Berg <myon@debian.org>; Andres Freund <andres@anarazel.de>; Tomas Vondra <tomas.vondra@postgresql.org>; pgsql-hackers@lists.postgresql.org

On 01/07/2025 14:03, Tomas Vondra wrote:
> Thanks! Pushed, with both adjustments (link to kernel thread, adding the
> commit hash).
I just noticed that this (commit bf1119d74a: Add CHECK_FOR_INTERRUPTS 
into pg_numa_query_pages) made the function unusable in frontend 
programs, because CHECK_FOR_INTERRUPTS is server only. It's not used in 
frontend programs today, but it was placed in src/port/ with the idea 
that it could be.

That's pretty easy to fix by wrapping it in an "#ifndef FRONTEND" block, 
per attached.

- Heikki

Attachments:

0001-Make-pg_numa_query_pages-work-in-frontend-programs.patch (text/x-patch, 1.1K) download From 92e66ea5f17cacfb3408d2af51b9d24d4413cc9e Mon Sep 17 00:00:00 2001 From: Heikki Linnakangas <heikki.linnakangas@iki.fi> Date: Thu, 12 Feb 2026 16:11:44 +0200 Subject: [PATCH 1/1] Make pg_numa_query_pages() work in frontend programs It's currently only used in the server, but it was placed in src/port with the idea that it might be useful in client programs too. However, it will currently fail to link if used in a client program, because CHECK_FOR_INTERRUPTS() is not usable in client programs. Fix that by wrapping it in "#ifndef FRONTEND". --- src/port/pg_numa.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/port/pg_numa.c b/src/port/pg_numa.c index d574a686b42..8954669273a 100644 --- a/src/port/pg_numa.c +++ b/src/port/pg_numa.c @@ -87,7 +87,9 @@ pg_numa_query_pages(int pid, unsigned long count, void **pages, int *status) unsigned long count_chunk = Min(count - next, NUMA_QUERY_CHUNK_SIZE); +#ifndef FRONTEND CHECK_FOR_INTERRUPTS(); +#endif /* * Bail out if any of the chunks errors out (ret<0). We ignore (ret>0) -- 2.47.3
^ permalink raw reply [nested|flat] 54+ messages in thread

* Re: pgsql: Introduce pg_shmem_allocations_numa view
@ 2026-02-12 16:43  Álvaro Herrera <alvherre@kurilemu.de>
  parent: Heikki Linnakangas <hlinnaka@iki.fi>
  0 siblings, 1 reply; 54+ messages in thread

From: Álvaro Herrera @ 2026-02-12 16:43 UTC (permalink / raw)
  To: Heikki Linnakangas <hlinnaka@iki.fi>; +Cc: Tomas Vondra <tomas@vondra.me>; Bertrand Drouvot <bertranddrouvot.pg@gmail.com>; Christoph Berg <myon@debian.org>; Andres Freund <andres@anarazel.de>; Tomas Vondra <tomas.vondra@postgresql.org>; pgsql-hackers@lists.postgresql.org

On 2026-Feb-12, Heikki Linnakangas wrote:

> I just noticed that this (commit bf1119d74a: Add CHECK_FOR_INTERRUPTS into
> pg_numa_query_pages) made the function unusable in frontend programs,
> because CHECK_FOR_INTERRUPTS is server only. It's not used in frontend
> programs today, but it was placed in src/port/ with the idea that it could
> be.

Your patch LGTM.

-- 
Álvaro Herrera               48°01'N 7°57'E  —  https://www.EnterpriseDB.com/
"Ed is the standard text editor."
      http://groups.google.com/group/alt.religion.emacs/msg/8d94ddab6a9b0ad3





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

* Re: pgsql: Introduce pg_shmem_allocations_numa view
@ 2026-02-12 17:23  Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
  parent: Álvaro Herrera <alvherre@kurilemu.de>
  0 siblings, 1 reply; 54+ messages in thread

From: Bertrand Drouvot @ 2026-02-12 17:23 UTC (permalink / raw)
  To: Álvaro Herrera <alvherre@kurilemu.de>; +Cc: Heikki Linnakangas <hlinnaka@iki.fi>; Tomas Vondra <tomas@vondra.me>; Christoph Berg <myon@debian.org>; Andres Freund <andres@anarazel.de>; Tomas Vondra <tomas.vondra@postgresql.org>; pgsql-hackers@lists.postgresql.org

Hi,

On Thu, Feb 12, 2026 at 05:43:47PM +0100, Álvaro Herrera wrote:
> On 2026-Feb-12, Heikki Linnakangas wrote:
> 
> > I just noticed that this (commit bf1119d74a: Add CHECK_FOR_INTERRUPTS into
> > pg_numa_query_pages) made the function unusable in frontend programs,
> > because CHECK_FOR_INTERRUPTS is server only.

Good catch! Out of curiosity how did you find the issue? Were you building a
client tool making used of pg_numa_query_pages()?

> It's not used in frontend
> > programs today, but it was placed in src/port/ with the idea that it could
> > be.
> 
> Your patch LGTM.

+1

Regards,

-- 
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com





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

* Re: pgsql: Introduce pg_shmem_allocations_numa view
@ 2026-02-12 17:42  Heikki Linnakangas <hlinnaka@iki.fi>
  parent: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
  0 siblings, 0 replies; 54+ messages in thread

From: Heikki Linnakangas @ 2026-02-12 17:42 UTC (permalink / raw)
  To: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>; Álvaro Herrera <alvherre@kurilemu.de>; +Cc: Tomas Vondra <tomas@vondra.me>; Christoph Berg <myon@debian.org>; Andres Freund <andres@anarazel.de>; Tomas Vondra <tomas.vondra@postgresql.org>; pgsql-hackers@lists.postgresql.org

On 12/02/2026 19:23, Bertrand Drouvot wrote:
> Hi,
> 
> On Thu, Feb 12, 2026 at 05:43:47PM +0100, Álvaro Herrera wrote:
>> On 2026-Feb-12, Heikki Linnakangas wrote:
>>
>>> I just noticed that this (commit bf1119d74a: Add CHECK_FOR_INTERRUPTS into
>>> pg_numa_query_pages) made the function unusable in frontend programs,
>>> because CHECK_FOR_INTERRUPTS is server only.
> 
> Good catch! Out of curiosity how did you find the issue? Were you building a
> client tool making used of pg_numa_query_pages()?

I was working on my "interrupts vs signals" patch, which needed to 
change some #includes in pg_numa.c, when I spotted that it already had 
that issue.

>> It's not used in frontend
>>> programs today, but it was placed in src/port/ with the idea that it could
>>> be.
>>
>> Your patch LGTM.
> 
> +1

Pushed, thanks!

- Heikki






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


end of thread, other threads:[~2026-02-12 17:42 UTC | newest]

Thread overview: 54+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2025-04-07 21:18 pgsql: Introduce pg_shmem_allocations_numa view Tomas Vondra <tomas.vondra@postgresql.org>
2025-06-12 21:16 ` Christoph Berg <myon@debian.org>
2025-06-23 14:42   ` Christoph Berg <myon@debian.org>
2025-06-23 14:48     ` Christoph Berg <myon@debian.org>
2025-06-23 15:14       ` Andres Freund <andres@anarazel.de>
2025-06-23 15:20         ` Christoph Berg <myon@debian.org>
2025-06-23 15:59           ` Christoph Berg <myon@debian.org>
2025-06-23 16:26             ` Andres Freund <andres@anarazel.de>
2025-06-23 19:57               ` Christoph Berg <myon@debian.org>
2025-06-23 20:10                 ` Tomas Vondra <tomas@vondra.me>
2025-06-23 20:31                   ` Christoph Berg <myon@debian.org>
2025-06-23 20:37                     ` Tomas Vondra <tomas@vondra.me>
2025-06-23 20:51                       ` Christoph Berg <myon@debian.org>
2025-06-23 21:14                         ` Tomas Vondra <tomas@vondra.me>
2025-06-23 21:25                           ` Christoph Berg <myon@debian.org>
2025-06-23 21:47                             ` Tomas Vondra <tomas@vondra.me>
2025-06-24 01:43                               ` Tomas Vondra <tomas@vondra.me>
2025-06-24 08:24                                 ` Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
2025-06-24 09:20                                   ` Tomas Vondra <tomas@vondra.me>
2025-06-24 11:10                                     ` Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
2025-06-24 12:33                                       ` Tomas Vondra <tomas@vondra.me>
2025-06-24 13:25                                         ` Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
2025-06-24 14:41                                           ` Christoph Berg <myon@debian.org>
2025-06-24 15:04                                             ` Tomas Vondra <tomas@vondra.me>
2025-06-24 15:30                                               ` Christoph Berg <myon@debian.org>
2025-06-24 20:32                                                 ` Tomas Vondra <tomas@vondra.me>
2025-06-25 06:45                                                   ` Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
2025-06-26 06:00                                                   ` Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
2025-06-26 08:53                                                     ` Tomas Vondra <tomas@vondra.me>
2025-07-21 20:52                                                       ` Christoph Berg <myon@debian.org>
2025-07-22 07:01                                                         ` Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
2025-06-25 06:11                                                 ` Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
2025-06-25 07:15                                                 ` Jakub Wartak <jakub.wartak@enterprisedb.com>
2025-06-25 09:31                                                   ` Tomas Vondra <tomas@vondra.me>
2025-06-25 12:42                                                     ` Álvaro Herrera <alvherre@kurilemu.de>
2025-06-25 12:53                                                       ` Tomas Vondra <tomas@vondra.me>
2025-06-25 06:05                                             ` Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
2025-06-25 09:00                                               ` Christoph Berg <myon@debian.org>
2025-06-25 09:22                                                 ` Tomas Vondra <tomas@vondra.me>
2025-06-26 05:28                                                 ` Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
2025-06-27 14:52                                                   ` Tomas Vondra <tomas@vondra.me>
2025-06-27 17:33                                                     ` Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
2025-06-30 18:56                                                       ` Tomas Vondra <tomas@vondra.me>
2025-07-01 04:06                                                         ` Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
2025-07-01 11:03                                                           ` Tomas Vondra <tomas@vondra.me>
2025-09-11 11:36                                                             ` Christoph Berg <myon@debian.org>
2025-09-11 11:39                                                               ` Christoph Berg <myon@debian.org>
2026-02-12 14:21                                                             ` Heikki Linnakangas <hlinnaka@iki.fi>
2026-02-12 16:43                                                               ` Álvaro Herrera <alvherre@kurilemu.de>
2026-02-12 17:23                                                                 ` Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
2026-02-12 17:42                                                                   ` Heikki Linnakangas <hlinnaka@iki.fi>
2025-06-24 18:24                                           ` Christoph Berg <myon@debian.org>
2025-06-24 11:10                                 ` Andres Freund <andres@anarazel.de>
2025-06-24 12:42                                   ` Tomas Vondra <tomas@vondra.me>

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