Skip to content

Commit af96d54

Browse files
committed
KVM: TDX: Use atomic64_dec_return() instead of a poor equivalent
Use atomic64_dec_return() when decrementing the number of "pre-mapped" S-EPT pages to ensure that the count can't go negative without KVM noticing. In theory, checking for '0' and then decrementing in a separate operation could miss a 0=>-1 transition. In practice, such a condition is impossible because nr_premapped is protected by slots_lock, i.e. doesn't actually need to be an atomic (that wart will be addressed shortly). Don't bother trying to keep the count non-negative, as the KVM_BUG_ON() ensures the VM is dead, i.e. there's no point in trying to limp along. Reviewed-by: Rick Edgecombe <rick.p.edgecombe@intel.com> Reviewed-by: Ira Weiny <ira.weiny@intel.com> Reviewed-by: Binbin Wu <binbin.wu@linux.intel.com> Reviewed-by: Yan Zhao <yan.y.zhao@intel.com> Tested-by: Yan Zhao <yan.y.zhao@intel.com> Tested-by: Kai Huang <kai.huang@intel.com> Link: https://patch.msgid.link/20251030200951.3402865-15-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 24adff3 commit af96d54

File tree

1 file changed

+2
-4
lines changed

1 file changed

+2
-4
lines changed

arch/x86/kvm/vmx/tdx.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1726,10 +1726,9 @@ static int tdx_sept_zap_private_spte(struct kvm *kvm, gfn_t gfn,
17261726
tdx_no_vcpus_enter_stop(kvm);
17271727
}
17281728
if (tdx_is_sept_zap_err_due_to_premap(kvm_tdx, err, entry, level)) {
1729-
if (KVM_BUG_ON(!atomic64_read(&kvm_tdx->nr_premapped), kvm))
1729+
if (KVM_BUG_ON(atomic64_dec_return(&kvm_tdx->nr_premapped) < 0, kvm))
17301730
return -EIO;
17311731

1732-
atomic64_dec(&kvm_tdx->nr_premapped);
17331732
return 0;
17341733
}
17351734

@@ -3171,8 +3170,7 @@ static int tdx_gmem_post_populate(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn,
31713170
goto out;
31723171
}
31733172

3174-
if (!KVM_BUG_ON(!atomic64_read(&kvm_tdx->nr_premapped), kvm))
3175-
atomic64_dec(&kvm_tdx->nr_premapped);
3173+
KVM_BUG_ON(atomic64_dec_return(&kvm_tdx->nr_premapped) < 0, kvm);
31763174

31773175
if (arg->flags & KVM_TDX_MEASURE_MEMORY_REGION) {
31783176
for (i = 0; i < PAGE_SIZE; i += TDX_EXTENDMR_CHUNKSIZE) {

0 commit comments

Comments
 (0)