Re: [PATCH v6 06/11] x86/virt/tdx: Optimize tdx_pamt_get/put()

From: Chao Gao

Date: Tue Jul 07 2026 - 02:48:38 EST


On Mon, May 25, 2026 at 07:35:10PM -0700, Rick Edgecombe wrote:
>@@ -2057,32 +2057,50 @@ static int tdx_pamt_get(kvm_pfn_t pfn)

[snip]

>+ /*
>+ * Unlike tdx_pamt_put() which uses atomic_dec_and_lock() to
>+ * atomically handle the 1->0 transition, the get side has no
>+ * equivalent combined primitive for 0->1. Recheck under the
>+ * lock since another get may have already done the 0->1
>+ * transition after both saw atomic_inc_not_zero() fail.
>+ */
>+ if (atomic_read(pamt_refcount)) {
>+ atomic_inc(pamt_refcount);
>+ spin_unlock(&pamt_lock);
>+ goto out_free;

spin_unlock() can be moved to out_free to avoid repeating it before
each 'goto out_free'.

> }
>
>+ tdx_status = tdh_phymem_pamt_add(pfn, pamt_pages);
>+ if (tdx_status == TDX_SUCCESS) {
>+ /*
>+ * The refcount is zero, and this locked path is the
>+ * only way to increase it from 0->1.
>+ */
>+ atomic_set(pamt_refcount, 1);
>+ } else {
>+ WARN_ON_ONCE(1);
>+ ret = -EIO;
>+ spin_unlock(&pamt_lock);
>+ goto out_free;
>+ }

Reduce indentation for the normal path:

if (tdx_status != TDX_SUCCESS) {
WARN_ON_ONCE(1);
ret = -EIO;
spin_unlock(&pamt_lock);
goto out_free;
}

/*
* The refcount is zero, and this locked path is the
* only way to increase it from 0->1.
*/
atomic_set(pamt_refcount, 1);


The rest looks good to me.

Reviewed-by: Chao Gao <chao.gao@xxxxxxxxx>