Re: [RFC, PATCH 02/12] x86/virt/tdx: Allocate reference counters for PAMT memory

From: Huang, Kai
Date: Thu May 08 2025 - 21:06:26 EST


On Thu, 2025-05-08 at 16:03 +0300, kirill.shutemov@xxxxxxxxxxxxxxx wrote:
> On Mon, May 05, 2025 at 11:05:12AM +0000, Huang, Kai wrote:
> >
> > > +static atomic_t *pamt_refcounts;
> > > +
> > >   static enum tdx_module_status_t tdx_module_status;
> > >   static DEFINE_MUTEX(tdx_module_lock);
> > >  
> > > @@ -1035,9 +1038,108 @@ static int config_global_keyid(void)
> > >    return ret;
> > >   }
> > >  
> > > +atomic_t *tdx_get_pamt_refcount(unsigned long hpa)
> > > +{
> > > + return &pamt_refcounts[hpa / PMD_SIZE];
> > > +}
> > > +EXPORT_SYMBOL_GPL(tdx_get_pamt_refcount);
> >
> > It's not quite clear why this function needs to be exported in this patch.  IMO
> > it's better to move the export to the patch which actually needs it.
> >
> > Looking at patch 5, tdx_pamt_get()/put() use it, and they are in KVM code.  But
> > I think we should just put them here in this file.  tdx_alloc_page() and
> > tdx_free_page() should be in this file too.
> >
> > And instead of exporting tdx_get_pamt_refcount(), the TDX core code here can
> > export tdx_alloc_page() and tdx_free_page(), providing two high level helpers to
> > allow the TDX users (e.g., KVM) to allocate/free TDX private pages.  How PAMT
> > pages are allocated is then hidden in the core TDX code.
>
> We would still need tdx_get_pamt_refcount() to handle case when we need to
> bump refcount for page allocated elsewhere.

Hmm I am not sure I am following this. What "page allocated" are you referring
to? I am probably missing something, but if the caller wants a TDX page then it
should just call tdx_alloc_page() which handles refcount bumping internally.
No?