Re: [PATCHv2 08/12] KVM: TDX: Handle PAMT allocation in fault path

From: Sagi Shahar
Date: Thu Aug 21 2025 - 15:53:56 EST


On Thu, Aug 21, 2025 at 2:35 PM Edgecombe, Rick P
<rick.p.edgecombe@xxxxxxxxx> wrote:
>
> On Thu, 2025-08-21 at 14:21 -0500, Sagi Shahar wrote:
> > > int tdx_sept_set_private_spte(struct kvm *kvm, gfn_t gfn,
> > > enum pg_level level, kvm_pfn_t pfn)
> > > {
> > > + struct kvm_vcpu *vcpu = kvm_get_running_vcpu();
> > > struct kvm_tdx *kvm_tdx = to_kvm_tdx(kvm);
> > > struct page *page = pfn_to_page(pfn);
> > > + int ret;
> > > +
> > > + ret = tdx_pamt_get(page, level, tdx_alloc_pamt_page_atomic, vcpu);
> > > + if (ret)
> > > + return ret;
> >
> > tdx_pamt_get() can return non-zero value in case of success e.g.
> > returning 1 in case tdx_pamt_add() lost the race.
>
> No?
>
> +static int tdx_pamt_get(struct page *page, enum pg_level level)
> +{
> + unsigned long hpa = page_to_phys(page);
> + atomic_t *pamt_refcount;
> + LIST_HEAD(pamt_pages);
> + int ret;
> +
> + if (!tdx_supports_dynamic_pamt(&tdx_sysinfo))
> + return 0;
> +
> + if (level != PG_LEVEL_4K)
> + return 0;
> +
> + pamt_refcount = tdx_get_pamt_refcount(hpa);
> + WARN_ON_ONCE(atomic_read(pamt_refcount) < 0);
> +
> + if (atomic_inc_not_zero(pamt_refcount))
> + return 0;
> +
> + if (tdx_alloc_pamt_pages(&pamt_pages))
> + return -ENOMEM;
> +
> + ret = tdx_pamt_add(pamt_refcount, hpa, &pamt_pages);
> + if (ret)
> + tdx_free_pamt_pages(&pamt_pages);
> +
> + return ret >= 0 ? 0 : ret;
> +}
>
> > Shouldn't we check
> > for (ret < 0) here and below cases?
>
> I think you are thinking of tdx_pamt_add().

My bad.