Re: [PATCH v3 05/16] x86/virt/tdx: Allocate reference counters for PAMT memory
From: Edgecombe, Rick P
Date: Mon Sep 29 2025 - 21:04:51 EST
On Mon, 2025-09-29 at 11:08 -0700, Dave Hansen wrote:
> On 9/29/25 10:41, Edgecombe, Rick P wrote:
> > On Tue, 2025-09-23 at 15:45 +0800, Binbin Wu wrote:
> > > > +/*
> > > > + * Allocate PAMT reference counters for all physical memory.
> > > > + *
> > > > + * It consumes 2MiB for every 1TiB of physical memory.
> > > > + */
> > > > +static int init_pamt_metadata(void)
> > > > +{
> > > > + size_t size = max_pfn / PTRS_PER_PTE * sizeof(*pamt_refcounts);
> > > Is there guarantee that max_pfn is PTRS_PER_PTE aligned?
> > > If not, it should be rounded up.
> > Vmalloc() should handle it?
>
> vmalloc() will, for instance, round up to 2 pages if you ask for 4097
> bytes in 'size'. But that's not the problem. The 'size' calculation
> itself is the problem.
>
> You need exactly 2 MiB for every 1 TiB of memory, so let's say we have:
>
> max_pfn = 1<<28
>
> (where 28 == 40-PAGE_SIZE) then size would be *exactly* 1<<21 (2 MiB).
> Right?
>
> But what if:
>
> max_pfn = (1<<28) + 1
>
> Then size needs to be one more page. Right? But what would the code do?
Doh, right. There is an additional issue. A later patch tweaks it to be:
+ size = max_pfn / PTRS_PER_PTE * sizeof(*pamt_refcounts);
+ size = round_up(size, PAGE_SIZE);
Perhaps an attempt to fix up the issue by Kirill? It should be fixed like Binbin
suggests, maybe:
+ size = DIV_ROUND_UP(max_pfn, PTRS_PER_PTE) * sizeof(*pamt_refcounts);
Thanks, and sorry for not giving the comment the proper attention the first time
around.