Re: [PATCHv2 04/12] x86/virt/tdx: Add tdx_alloc/free_page() helpers

From: Kiryl Shutsemau

Date: Tue Sep 16 2025 - 05:22:47 EST


On Tue, Sep 16, 2025 at 12:03:26AM +0000, Edgecombe, Rick P wrote:
> On Mon, 2025-06-09 at 22:13 +0300, Kirill A. Shutemov wrote:
> > +
> > +static int tdx_pamt_add(atomic_t *pamt_refcount, unsigned long hpa,
> > + struct list_head *pamt_pages)
> > +{
> > + u64 err;
> > +
> > + guard(spinlock)(&pamt_lock);
> > +
> > + hpa = ALIGN_DOWN(hpa, PMD_SIZE);
> > +
> > + /* Lost race to other tdx_pamt_add() */
> > + if (atomic_read(pamt_refcount) != 0) {
> > + atomic_inc(pamt_refcount);
> > + return 1;
> > + }
> > +
> > + err = tdh_phymem_pamt_add(hpa | TDX_PS_2M, pamt_pages);
> > +
> > + /*
> > + * tdx_hpa_range_not_free() is true if current task won race
> > + * against tdx_pamt_put().
> > + */
> > + if (err && !tdx_hpa_range_not_free(err)) {
> > + pr_err("TDH_PHYMEM_PAMT_ADD failed: %#llx\n", err);
> > + return -EIO;
> > + }
> > +
> > + atomic_set(pamt_refcount, 1);
> > +
> > + if (tdx_hpa_range_not_free(err))
> > + return 1;
>
> Hey Kirill,
>
> I couldn't figure out how this tdx_hpa_range_not_free() check helps. We are
> already inside the lock also taken by any operation that might affect PAMT
> state. Can you explain more about this? Otherwise I'm going to drop it for
> inability to explain.

My git has comment for the check:

https://git.kernel.org/pub/scm/linux/kernel/git/kas/linux.git/tree/arch/x86/virt/vmx/tdx/tdx.c?h=tdx/dpamt&id=375706fe73a8499dbdddb22c13d19d7286280ad6#n2160

Consider the following scenario

CPU0 CPU1
tdx_pamt_put()
atomic_dec_and_test() == true
tdx_pamt_get()
atomic_inc_not_zero() == false
tdx_pamt_add()
<takes pamt_lock>
// CPU0 never removed PAMT memory
tdh_phymem_pamt_add() == HPA_RANGE_NOT_FREE
atomic_set(1);
<drops pamt_lock>
<takes pamt_lock>
// Lost the race to CPU1
atomic_read() > 0
<drop pamt_lock>

Does it make sense?

--
Kiryl Shutsemau / Kirill A. Shutemov