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

From: Dave Hansen

Date: Tue Sep 30 2025 - 13:38:32 EST


On 9/30/25 07:03, Xiaoyao Li wrote:
> My understanding/expectation is that, when refcount is not zero it needs
> to increment the refcount instead of simply return. And ...

That's a good point.

This doesn't properly handle the case where the PAMT exists and the
refcount is already incremented.

We need something like this with a fast path for a non-zero refcount
that doesn't take the lock:

// When the refcount is nonzero, the PAMT is allocated already.
// Bump it up and return.
if (atomic_inc_not_zero(pamt_refcount))
return;

// No PAMT is present. Take the lock to ensure there is no race
// to allocate it and proceed to allocate one.

... existing code here

Without this, all of the atomic manipulation is within the lock and the
atomic_t could just be a plain int.