Re: [PATCH v3 07/16] x86/virt/tdx: Add tdx_alloc/free_page() helpers

From: Edgecombe, Rick P

Date: Tue Sep 30 2025 - 13:47:24 EST


On Tue, 2025-09-30 at 22:03 +0800, Xiaoyao Li wrote:
> > +/* Bump PAMT refcount for the given page and allocate PAMT memory if needed
> > */
> > +int tdx_pamt_get(struct page *page)
> > +{
> > + unsigned long hpa = ALIGN_DOWN(page_to_phys(page), PMD_SIZE);
> > + u64 pamt_pa_array[MAX_DPAMT_ARG_SIZE];
> > + atomic_t *pamt_refcount;
> > + u64 tdx_status;
> > + int ret;
> > +
> > + if (!tdx_supports_dynamic_pamt(&tdx_sysinfo))
> > + return 0;
> > +
> > + ret = alloc_pamt_array(pamt_pa_array);
> > + if (ret)
> > + return ret;
> > +
> > + pamt_refcount = tdx_find_pamt_refcount(hpa);
> > +
> > + scoped_guard(spinlock, &pamt_lock) {
> > + if (atomic_read(pamt_refcount))
>
> It's not what I expect the refcount to work (maybe I miss something
> seriously?)
>
> My understanding/expectation is that, when refcount is not zero it needs
> to increment the refcount instead of simply return. And ...

Argh, yes. The following optimization patch should change this to behave
normally with respect the refcount. I should have tested it with the
optimization patches removed, I'll do that next time.

>
> > + goto out_free;
> > +
> > + tdx_status = tdh_phymem_pamt_add(hpa | TDX_PS_2M,
> > pamt_pa_array);
> > +
> > + if (IS_TDX_SUCCESS(tdx_status)) {
> > + atomic_inc(pamt_refcount);
> > + } else {
> > + pr_err("TDH_PHYMEM_PAMT_ADD failed: %#llx\n",
> > tdx_status);
> > + goto out_free;
> > + }
> > + }
> > +
> > + return ret;
> > +out_free:
> > + free_pamt_array(pamt_pa_array);
> > + return ret;
> > +}
> > +EXPORT_SYMBOL_GPL(tdx_pamt_get);
> > +
> > +/*
> > + * Drop PAMT refcount for the given page and free PAMT memory if it is no
> > + * longer needed.
> > + */
> > +void tdx_pamt_put(struct page *page)
> > +{
> > + unsigned long hpa = ALIGN_DOWN(page_to_phys(page), PMD_SIZE);
> > + u64 pamt_pa_array[MAX_DPAMT_ARG_SIZE];
> > + atomic_t *pamt_refcount;
> > + u64 tdx_status;
> > +
> > + if (!tdx_supports_dynamic_pamt(&tdx_sysinfo))
> > + return;
> > +
> > + hpa = ALIGN_DOWN(hpa, PMD_SIZE);
> > +
> > + pamt_refcount = tdx_find_pamt_refcount(hpa);
> > +
> > + scoped_guard(spinlock, &pamt_lock) {
> > + if (!atomic_read(pamt_refcount))
>
> ...
>
> when refcount > 1, decrease it.
> when refcount is 1, decrease it and remove the PAMT page pair.