[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <bfaswqmlsyycr3alibn6f422cjtpd6ybssjekvrrz4zdwgwfcz@pxy25ra4sln2>
Date: Tue, 16 Sep 2025 10:22:26 +0100
From: Kiryl Shutsemau <kirill@...temov.name>
To: "Edgecombe, Rick P" <rick.p.edgecombe@...el.com>
Cc: "kirill.shutemov@...ux.intel.com" <kirill.shutemov@...ux.intel.com>,
"pbonzini@...hat.com" <pbonzini@...hat.com>, "seanjc@...gle.com" <seanjc@...gle.com>,
"dave.hansen@...ux.intel.com" <dave.hansen@...ux.intel.com>, "Gao, Chao" <chao.gao@...el.com>, "bp@...en8.de" <bp@...en8.de>,
"Huang, Kai" <kai.huang@...el.com>, "x86@...nel.org" <x86@...nel.org>,
"mingo@...hat.com" <mingo@...hat.com>, "Zhao, Yan Y" <yan.y.zhao@...el.com>,
"tglx@...utronix.de" <tglx@...utronix.de>, "kvm@...r.kernel.org" <kvm@...r.kernel.org>,
"linux-coco@...ts.linux.dev" <linux-coco@...ts.linux.dev>, "Yamahata, Isaku" <isaku.yamahata@...el.com>,
"linux-kernel@...r.kernel.org" <linux-kernel@...r.kernel.org>
Subject: Re: [PATCHv2 04/12] x86/virt/tdx: Add tdx_alloc/free_page() helpers
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
Powered by blists - more mailing lists