Re: [RFC PATCH v5 16/45] x86/virt/tdx: Add tdx_alloc/free_control_page() helpers

From: Huang, Kai

Date: Tue Feb 10 2026 - 18:02:50 EST


On Tue, 2026-02-10 at 14:50 -0800, Dave Hansen wrote:
> On 2/10/26 14:46, Huang, Kai wrote:
> > Sorry I am a bit confused. But I think the "1=>0 and lock" are atomic
> > together?
>
> Maybe I'm being pedantic. The 1=>0 happens under the lock, but the 1=>0
> and the lock acquisition itself are not atomic. You can see them
> happening at different times:

Oh I see. Thanks.

>
> int _atomic_dec_and_lock(atomic_t *atomic, spinlock_t *lock)
> {
> /* Subtract 1 from counter unless that drops it to 0...
> if (atomic_add_unless(atomic, -1, 1))
> return 0;
>
> /* Otherwise do it the slow way */
> spin_lock(lock);
> if (atomic_dec_and_test(atomic))
> return 1;
> spin_unlock(lock);
> return 0;
> }
>
> tl;dr: Kirill was right, atomic_dec_and_test() doesn't work by itself here.
>
> But I think atomic_dec_and_lock() will.

Agreed.