Re: [PATCH v3 05/16] x86/virt/tdx: Allocate reference counters for PAMT memory

From: Dave Hansen

Date: Mon Sep 29 2025 - 14:08:19 EST


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?