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

Xiaoyao Li
2025-09-30T14:04:01Z

On 9/19/2025 7:22 AM, Rick Edgecombe 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 ...

> + 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.

> + return;
> +
> + tdx_status = tdh_phymem_pamt_remove(hpa | TDX_PS_2M, pamt_pa_array);
> +
> + if (IS_TDX_SUCCESS(tdx_status)) {
> + atomic_dec(pamt_refcount);
> + } else {
> + pr_err("TDH_PHYMEM_PAMT_REMOVE failed: %#llx\n", tdx_status);
> + return;
> + }
> + }
> +
> + free_pamt_array(pamt_pa_array);
> +}
> +EXPORT_SYMBOL_GPL(tdx_pamt_put);