[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <e455cb2c-a51c-494e-acc1-12743c4f4d3f@linux.intel.com>
Date: Tue, 23 Sep 2025 17:38:26 +0800
From: Binbin Wu <binbin.wu@...ux.intel.com>
To: "Huang, Kai" <kai.huang@...el.com>,
"Edgecombe, Rick P" <rick.p.edgecombe@...el.com>
Cc: "kvm@...r.kernel.org" <kvm@...r.kernel.org>,
"linux-coco@...ts.linux.dev" <linux-coco@...ts.linux.dev>,
"Zhao, Yan Y" <yan.y.zhao@...el.com>,
"dave.hansen@...ux.intel.com" <dave.hansen@...ux.intel.com>,
"kas@...nel.org" <kas@...nel.org>, "seanjc@...gle.com" <seanjc@...gle.com>,
"mingo@...hat.com" <mingo@...hat.com>,
"linux-kernel@...r.kernel.org" <linux-kernel@...r.kernel.org>,
"pbonzini@...hat.com" <pbonzini@...hat.com>,
"Yamahata, Isaku" <isaku.yamahata@...el.com>,
"tglx@...utronix.de" <tglx@...utronix.de>,
"Annapurve, Vishal" <vannapurve@...gle.com>, "Gao, Chao"
<chao.gao@...el.com>, "bp@...en8.de" <bp@...en8.de>,
"x86@...nel.org" <x86@...nel.org>,
"kirill.shutemov@...ux.intel.com" <kirill.shutemov@...ux.intel.com>
Subject: Re: [PATCH v3 06/16] x86/virt/tdx: Improve PAMT refcounters
allocation for sparse memory
On 9/19/2025 3:25 PM, Huang, Kai wrote:
>> +/* Map a page into the PAMT refcount vmalloc region */
>> +static int pamt_refcount_populate(pte_t *pte, unsigned long addr, void *data)
>> +{
>> + struct page *page;
>> + pte_t entry;
>>
>> - pamt_refcounts = vmalloc(size);
>> - if (!pamt_refcounts)
>> + page = alloc_page(GFP_KERNEL | __GFP_ZERO);
>> + if (!page)
>> return -ENOMEM;
>>
>> + entry = mk_pte(page, PAGE_KERNEL);
>> +
>> + spin_lock(&init_mm.page_table_lock);
>> + /*
>> + * PAMT refcount populations can overlap due to rounding of the
>> + * start/end pfn.
>>
> [...]
>
>> Make sure another PAMT range didn't already
>> + * populate it.
>> + */
> Make sure the same range only gets populated once ?
>
>> + if (pte_none(ptep_get(pte)))
>> + set_pte_at(&init_mm, addr, pte, entry);
>> + else
>> + __free_page(page);
>> + spin_unlock(&init_mm.page_table_lock);
>> +
>> return 0;
>> }
>>
>> +/*
>> + * Allocate PAMT reference counters for the given PFN range.
>> + *
>> + * It consumes 2MiB for every 1TiB of physical memory.
>> + */
>> +static int alloc_pamt_refcount(unsigned long start_pfn, unsigned long end_pfn)
>> +{
>> + unsigned long start, end;
>> +
>> + start = (unsigned long)tdx_find_pamt_refcount(PFN_PHYS(start_pfn));
>> + end = (unsigned long)tdx_find_pamt_refcount(PFN_PHYS(end_pfn + 1));
> (sorry didn't notice this in last version)
>
> I don't quite follow why we need "end_pfn + 1" instead of just "end_pfn"?
>
> IIUC this could result in an additional 2M range being populated
> unnecessarily when the end_pfn is 2M aligned.
IIUC, this will not happen.
The +1 page will be converted to 4KB, and will be ignored since in
tdx_find_pamt_refcount() the address is divided by 2M.
To handle the address unaligned to 2M, +511 should be used instead of +1?
>
> And ...
>
>> + start = round_down(start, PAGE_SIZE);
>> + end = round_up(end, PAGE_SIZE);
>> +
>> + return apply_to_page_range(&init_mm, start, end - start,
>> + pamt_refcount_populate, NULL);
>> +}
>> +
>> +/*
>> + * Reserve vmalloc range for PAMT reference counters. It covers all physical
>> + * address space up to max_pfn. It is going to be populated from
>> + * build_tdx_memlist() only for present memory that available for TDX use.
>> + *
>> + * It reserves 2MiB of virtual address space for every 1TiB of physical memory.
>> + */
>> +static int init_pamt_metadata(void)
>> +{
>> + struct vm_struct *area;
>> + size_t size;
>> +
>> + if (!tdx_supports_dynamic_pamt(&tdx_sysinfo))
>> + return 0;
>> +
>> + size = max_pfn / PTRS_PER_PTE * sizeof(*pamt_refcounts);
>> + size = round_up(size, PAGE_SIZE);
>> +
>> + area = get_vm_area(size, VM_SPARSE);
>> + if (!area)
>> + return -ENOMEM;
>> +
>> + pamt_refcounts = area->addr;
>> + return 0;
>> +}
>> +
>> +/* Unmap a page from the PAMT refcount vmalloc region */
>> +static int pamt_refcount_depopulate(pte_t *pte, unsigned long addr, void *data)
>> +{
>> + struct page *page;
>> + pte_t entry;
>> +
>> + spin_lock(&init_mm.page_table_lock);
>> +
>> + entry = ptep_get(pte);
>> + /* refount allocation is sparse, may not be populated */
refount -> refcount
>> + if (!pte_none(entry)) {
>> + pte_clear(&init_mm, addr, pte);
>> + page = pte_page(entry);
>> + __free_page(page);
>> + }
>> +
>> + spin_unlock(&init_mm.page_table_lock);
>> +
>> + return 0;
>> +}
>> +
>> +/* Unmap all PAMT refcount pages and free vmalloc range */
>> static void free_pamt_metadata(void)
>> {
>> + size_t size;
>> +
>> if (!tdx_supports_dynamic_pamt(&tdx_sysinfo))
>> return;
>>
>> + size = max_pfn / PTRS_PER_PTE * sizeof(*pamt_refcounts);
>> + size = round_up(size, PAGE_SIZE);
>> +
>> + apply_to_existing_page_range(&init_mm,
>> + (unsigned long)pamt_refcounts,
>> + size, pamt_refcount_depopulate,
>> + NULL);
>> vfree(pamt_refcounts);
>> pamt_refcounts = NULL;
>> }
>> @@ -288,10 +377,19 @@ static int build_tdx_memlist(struct list_head *tmb_list)
>> ret = add_tdx_memblock(tmb_list, start_pfn, end_pfn, nid);
>> if (ret)
>> goto err;
>> +
>> + /* Allocated PAMT refcountes for the memblock */
>> + ret = alloc_pamt_refcount(start_pfn, end_pfn);
>> + if (ret)
>> + goto err;
>> }
> ... when max_pfn == end_pfn of the last TDX memory block, this could
> result in an additional page of @pamt_refcounts being allocated, but it
> will never be freed since free_pamt_metadata() will only free mapping up
> to max_pfn.
>
> Am I missing anything?
Powered by blists - more mailing lists