From nobody Sun Dec 14 11:32:40 2025 Received: from mail-vs1-f73.google.com (mail-vs1-f73.google.com [209.85.217.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6ABEF1547F0 for ; Tue, 4 Feb 2025 00:41:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.217.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738629667; cv=none; b=Yi8zaiVHFHR82P2ewmqdEC/Es/4GeTzAja5ySkpBMUStaOzZ+WO6NJqeTamgvpkK0iIfwIOK5zVzWc/McH363wI/FoAcIP9/bzvOiXAUvsU+W43D9iMgthg+Jy9uf5b+AhCvchI2cQrdd5FW/0jTTczHco6IQUwnjTCPNzTVoDY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738629667; c=relaxed/simple; bh=FDFCk2LogfvC6RP8YTOSiGFoYjSxrG9RdMJhIpmoHow=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=JvVLslntRIEkR2K0JQz3CMbEkw152rmVoC7LaXr97QvbzjiRebu23vykTYR4jOiFeIYhWXQYoolG/JVLWmqjvucVT2SujU7QXVQZekg6nmMfJmWf8zPijeGB/3nBnn+8nS5Gfv3rcdhUiImx12FEj5vLIH6/BorYSOaKH+tIZk8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=20rFfx47; arc=none smtp.client-ip=209.85.217.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="20rFfx47" Received: by mail-vs1-f73.google.com with SMTP id ada2fe7eead31-4b3cc537766so515721137.2 for ; Mon, 03 Feb 2025 16:41:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738629663; x=1739234463; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=NT/1Ea4uOdT49wXiAqqe8cJlC1P1xI9VIain2UmREN0=; b=20rFfx473GOBjoNUYs4JozDIW1yedvGFdyY9+PnUN34sLDpUhoNZNDrk3NsGTX2BJy eZQZgQUPh4x0Ao3HKHj/0T97hDgMuclZVFrtinNJjfwmjTK1GBlFY+qc9ngm4mfSGdRe pWY0YfVRjGzyFL+nGBt8/LPJeCumCEoqwJ4cV0zqHQBtYOSv8aXd3h4Y5fr76IcCKfmG QE/T1jx64XD7P1XOb85oAat7S5a5ZyPA5uUmY3fn2KTFGSw1oi8edZJA05xRbQFPoFie lRQXscmyKUP7LZvynWFvcMGOW1d8uBq8SjAV41qRYE3IgkKevpS2OAb1uf1dpVeE1r7k n9lg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738629663; x=1739234463; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=NT/1Ea4uOdT49wXiAqqe8cJlC1P1xI9VIain2UmREN0=; b=pJMPIdMZFNf6oUtgFhJwHZCxoAVktYBpz/tnqkaPMmXF1o8BO9UgBqkR/66INiEa4Y Yxz1ffU1Tuldaumn5Spzq+oq9OVLeCDT20ag5bYYRT/NBvNx1ou0N3hh7sqA8qOBa73C eaUoYsKMTzlZD8+9zli5K5ylDhkB0MtXPnVFYhiCzGRRxPBSQS03QWQYDRHlV6KafEvA w6e+dEnF7Gg9rxMLCdInUoe0JnvTQQ/j5kb1kVAc0Qkko/6lwPSm0vk+ZWT6ToW6io7X b8kOAlgLVeLevowOSAOZnpbIpEZ88DHUnbvet9SdJ97vOMV8NQZB4PUtT0w2vAVZ/9Pq X1bg== X-Forwarded-Encrypted: i=1; AJvYcCWNNB2/amjxmZ+zEmdC/FCCIeaa42YdWyx2M2mBzzBDdKr8m6Y6tH//wact5Ok8lr9oVwlwwrZ2mJu2MkU=@vger.kernel.org X-Gm-Message-State: AOJu0Yy46nD5qhu1c+/2hCp2OjhTSvGcGB+o4ReeKsap2xJZhrz0UiQD 3AFKXP8aUN4pkVrYItzpBEseYtTstAWoEf+bsyvQK8YRaR2h0tBfE0P4y7dhVpAYuUuYWs8Mqhk aui0qOc9TIeqThSbq6Q== X-Google-Smtp-Source: AGHT+IGvEefJIQ+qh5TNzlPbwPP5FFWQIpGkJTgioLbeggYzaRqHQ2iUX4TpLwM9epwUB9f8udHnoz355QhDxYKM X-Received: from vkbci31.prod.google.com ([2002:a05:6122:321f:b0:516:25ed:28e4]) (user=jthoughton job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6102:3913:b0:4b6:20a5:8a11 with SMTP id ada2fe7eead31-4b9a4ec0890mr18276154137.1.1738629663101; Mon, 03 Feb 2025 16:41:03 -0800 (PST) Date: Tue, 4 Feb 2025 00:40:37 +0000 In-Reply-To: <20250204004038.1680123-1-jthoughton@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250204004038.1680123-1-jthoughton@google.com> X-Mailer: git-send-email 2.48.1.362.g079036d154-goog Message-ID: <20250204004038.1680123-11-jthoughton@google.com> Subject: [PATCH v9 10/11] KVM: x86/mmu: Add support for lockless walks of rmap SPTEs From: James Houghton To: Sean Christopherson , Paolo Bonzini Cc: David Matlack , David Rientjes , James Houghton , Marc Zyngier , Oliver Upton , Wei Xu , Yu Zhao , Axel Rasmussen , kvm@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Sean Christopherson Add a lockless version of for_each_rmap_spte(), which is pretty much the same as the normal version, except that it doesn't BUG() the host if a non-present SPTE is encountered. When mmu_lock is held, it should be impossible for a different task to zap a SPTE, _and_ zapped SPTEs must be removed from their rmap chain prior to dropping mmu_lock. Thus, the normal walker BUG()s if a non-present SPTE is encountered as something is wildly broken. When walking rmaps without holding mmu_lock, the SPTEs pointed at by the rmap chain can be zapped/dropped, and so a lockless walk can observe a non-present SPTE if it runs concurrently with a different operation that is zapping SPTEs. Signed-off-by: Sean Christopherson Signed-off-by: James Houghton --- arch/x86/kvm/mmu/mmu.c | 135 ++++++++++++++++++++++++++++------------- 1 file changed, 94 insertions(+), 41 deletions(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 267cf2d4c3e3..a0f735eeaaeb 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -876,10 +876,12 @@ static struct kvm_memory_slot *gfn_to_memslot_dirty_b= itmap(struct kvm_vcpu *vcpu */ #define KVM_RMAP_LOCKED BIT(1) =20 -static unsigned long kvm_rmap_lock(struct kvm_rmap_head *rmap_head) +static unsigned long __kvm_rmap_lock(struct kvm_rmap_head *rmap_head) { unsigned long old_val, new_val; =20 + lockdep_assert_preemption_disabled(); + /* * Elide the lock if the rmap is empty, as lockless walkers (read-only * mode) don't need to (and can't) walk an empty rmap, nor can they add @@ -911,7 +913,7 @@ static unsigned long kvm_rmap_lock(struct kvm_rmap_head= *rmap_head) /* * Use try_cmpxchg_acquire to prevent reads and writes to the rmap * from being reordered outside of the critical section created by - * kvm_rmap_lock. + * __kvm_rmap_lock. * * Pairs with smp_store_release in kvm_rmap_unlock. * @@ -920,21 +922,42 @@ static unsigned long kvm_rmap_lock(struct kvm_rmap_he= ad *rmap_head) */ } while (!atomic_long_try_cmpxchg_acquire(&rmap_head->val, &old_val, new_= val)); =20 - /* Return the old value, i.e. _without_ the LOCKED bit set. */ + /* + * Return the old value, i.e. _without_ the LOCKED bit set. It's + * impossible for the return value to be 0 (see above), i.e. the read- + * only unlock flow can't get a false positive and fail to unlock. + */ return old_val; } =20 -static void kvm_rmap_unlock(struct kvm_rmap_head *rmap_head, - unsigned long new_val) +static unsigned long kvm_rmap_lock(struct kvm *kvm, + struct kvm_rmap_head *rmap_head) +{ + lockdep_assert_held_write(&kvm->mmu_lock); + + return __kvm_rmap_lock(rmap_head); +} + +static void __kvm_rmap_unlock(struct kvm_rmap_head *rmap_head, + unsigned long val) { - WARN_ON_ONCE(new_val & KVM_RMAP_LOCKED); + KVM_MMU_WARN_ON(val & KVM_RMAP_LOCKED); /* * Ensure that all accesses to the rmap have completed * before we actually unlock the rmap. * - * Pairs with the atomic_long_try_cmpxchg_acquire in kvm_rmap_lock. + * Pairs with the atomic_long_try_cmpxchg_acquire in __kvm_rmap_lock. */ - atomic_long_set_release(&rmap_head->val, new_val); + atomic_long_set_release(&rmap_head->val, val); +} + +static void kvm_rmap_unlock(struct kvm *kvm, + struct kvm_rmap_head *rmap_head, + unsigned long new_val) +{ + lockdep_assert_held_write(&kvm->mmu_lock); + + __kvm_rmap_unlock(rmap_head, new_val); } =20 static unsigned long kvm_rmap_get(struct kvm_rmap_head *rmap_head) @@ -942,17 +965,49 @@ static unsigned long kvm_rmap_get(struct kvm_rmap_hea= d *rmap_head) return atomic_long_read(&rmap_head->val) & ~KVM_RMAP_LOCKED; } =20 +/* + * If mmu_lock isn't held, rmaps can only be locked in read-only mode. The + * actual locking is the same, but the caller is disallowed from modifying= the + * rmap, and so the unlock flow is a nop if the rmap is/was empty. + */ +__maybe_unused +static unsigned long kvm_rmap_lock_readonly(struct kvm_rmap_head *rmap_hea= d) +{ + unsigned long rmap_val; + + preempt_disable(); + rmap_val =3D __kvm_rmap_lock(rmap_head); + + if (!rmap_val) + preempt_enable(); + + return rmap_val; +} + +__maybe_unused +static void kvm_rmap_unlock_readonly(struct kvm_rmap_head *rmap_head, + unsigned long old_val) +{ + if (!old_val) + return; + + KVM_MMU_WARN_ON(old_val !=3D kvm_rmap_get(rmap_head)); + + __kvm_rmap_unlock(rmap_head, old_val); + preempt_enable(); +} + /* * Returns the number of pointers in the rmap chain, not counting the new = one. */ -static int pte_list_add(struct kvm_mmu_memory_cache *cache, u64 *spte, - struct kvm_rmap_head *rmap_head) +static int pte_list_add(struct kvm *kvm, struct kvm_mmu_memory_cache *cach= e, + u64 *spte, struct kvm_rmap_head *rmap_head) { unsigned long old_val, new_val; struct pte_list_desc *desc; int count =3D 0; =20 - old_val =3D kvm_rmap_lock(rmap_head); + old_val =3D kvm_rmap_lock(kvm, rmap_head); =20 if (!old_val) { new_val =3D (unsigned long)spte; @@ -984,7 +1039,7 @@ static int pte_list_add(struct kvm_mmu_memory_cache *c= ache, u64 *spte, desc->sptes[desc->spte_count++] =3D spte; } =20 - kvm_rmap_unlock(rmap_head, new_val); + kvm_rmap_unlock(kvm, rmap_head, new_val); =20 return count; } @@ -1032,7 +1087,7 @@ static void pte_list_remove(struct kvm *kvm, u64 *spt= e, unsigned long rmap_val; int i; =20 - rmap_val =3D kvm_rmap_lock(rmap_head); + rmap_val =3D kvm_rmap_lock(kvm, rmap_head); if (KVM_BUG_ON_DATA_CORRUPTION(!rmap_val, kvm)) goto out; =20 @@ -1058,7 +1113,7 @@ static void pte_list_remove(struct kvm *kvm, u64 *spt= e, } =20 out: - kvm_rmap_unlock(rmap_head, rmap_val); + kvm_rmap_unlock(kvm, rmap_head, rmap_val); } =20 static void kvm_zap_one_rmap_spte(struct kvm *kvm, @@ -1076,7 +1131,7 @@ static bool kvm_zap_all_rmap_sptes(struct kvm *kvm, unsigned long rmap_val; int i; =20 - rmap_val =3D kvm_rmap_lock(rmap_head); + rmap_val =3D kvm_rmap_lock(kvm, rmap_head); if (!rmap_val) return false; =20 @@ -1095,7 +1150,7 @@ static bool kvm_zap_all_rmap_sptes(struct kvm *kvm, } out: /* rmap_head is meaningless now, remember to reset it */ - kvm_rmap_unlock(rmap_head, 0); + kvm_rmap_unlock(kvm, rmap_head, 0); return true; } =20 @@ -1168,23 +1223,18 @@ static u64 *rmap_get_first(struct kvm_rmap_head *rm= ap_head, struct rmap_iterator *iter) { unsigned long rmap_val =3D kvm_rmap_get(rmap_head); - u64 *sptep; =20 if (!rmap_val) return NULL; =20 if (!(rmap_val & KVM_RMAP_MANY)) { iter->desc =3D NULL; - sptep =3D (u64 *)rmap_val; - goto out; + return (u64 *)rmap_val; } =20 iter->desc =3D (struct pte_list_desc *)(rmap_val & ~KVM_RMAP_MANY); iter->pos =3D 0; - sptep =3D iter->desc->sptes[iter->pos]; -out: - BUG_ON(!is_shadow_present_pte(*sptep)); - return sptep; + return iter->desc->sptes[iter->pos]; } =20 /* @@ -1194,14 +1244,11 @@ static u64 *rmap_get_first(struct kvm_rmap_head *rm= ap_head, */ static u64 *rmap_get_next(struct rmap_iterator *iter) { - u64 *sptep; - if (iter->desc) { if (iter->pos < PTE_LIST_EXT - 1) { ++iter->pos; - sptep =3D iter->desc->sptes[iter->pos]; - if (sptep) - goto out; + if (iter->desc->sptes[iter->pos]) + return iter->desc->sptes[iter->pos]; } =20 iter->desc =3D iter->desc->more; @@ -1209,20 +1256,24 @@ static u64 *rmap_get_next(struct rmap_iterator *ite= r) if (iter->desc) { iter->pos =3D 0; /* desc->sptes[0] cannot be NULL */ - sptep =3D iter->desc->sptes[iter->pos]; - goto out; + return iter->desc->sptes[iter->pos]; } } =20 return NULL; -out: - BUG_ON(!is_shadow_present_pte(*sptep)); - return sptep; } =20 -#define for_each_rmap_spte(_rmap_head_, _iter_, _spte_) \ - for (_spte_ =3D rmap_get_first(_rmap_head_, _iter_); \ - _spte_; _spte_ =3D rmap_get_next(_iter_)) +#define __for_each_rmap_spte(_rmap_head_, _iter_, _sptep_) \ + for (_sptep_ =3D rmap_get_first(_rmap_head_, _iter_); \ + _sptep_; _sptep_ =3D rmap_get_next(_iter_)) + +#define for_each_rmap_spte(_rmap_head_, _iter_, _sptep_) \ + __for_each_rmap_spte(_rmap_head_, _iter_, _sptep_) \ + if (!WARN_ON_ONCE(!is_shadow_present_pte(*(_sptep_)))) \ + +#define for_each_rmap_spte_lockless(_rmap_head_, _iter_, _sptep_, _spte_) \ + __for_each_rmap_spte(_rmap_head_, _iter_, _sptep_) \ + if (is_shadow_present_pte(_spte_ =3D mmu_spte_get_lockless(sptep))) =20 static void drop_spte(struct kvm *kvm, u64 *sptep) { @@ -1308,12 +1359,13 @@ static bool __rmap_clear_dirty(struct kvm *kvm, str= uct kvm_rmap_head *rmap_head, struct rmap_iterator iter; bool flush =3D false; =20 - for_each_rmap_spte(rmap_head, &iter, sptep) + for_each_rmap_spte(rmap_head, &iter, sptep) { if (spte_ad_need_write_protect(*sptep)) flush |=3D test_and_clear_bit(PT_WRITABLE_SHIFT, (unsigned long *)sptep); else flush |=3D spte_clear_dirty(sptep); + } =20 return flush; } @@ -1634,7 +1686,7 @@ static void __rmap_add(struct kvm *kvm, kvm_update_page_stats(kvm, sp->role.level, 1); =20 rmap_head =3D gfn_to_rmap(gfn, sp->role.level, slot); - rmap_count =3D pte_list_add(cache, spte, rmap_head); + rmap_count =3D pte_list_add(kvm, cache, spte, rmap_head); =20 if (rmap_count > kvm->stat.max_mmu_rmap_size) kvm->stat.max_mmu_rmap_size =3D rmap_count; @@ -1768,13 +1820,14 @@ static unsigned kvm_page_table_hashfn(gfn_t gfn) return hash_64(gfn, KVM_MMU_HASH_SHIFT); } =20 -static void mmu_page_add_parent_pte(struct kvm_mmu_memory_cache *cache, +static void mmu_page_add_parent_pte(struct kvm *kvm, + struct kvm_mmu_memory_cache *cache, struct kvm_mmu_page *sp, u64 *parent_pte) { if (!parent_pte) return; =20 - pte_list_add(cache, parent_pte, &sp->parent_ptes); + pte_list_add(kvm, cache, parent_pte, &sp->parent_ptes); } =20 static void mmu_page_remove_parent_pte(struct kvm *kvm, struct kvm_mmu_pag= e *sp, @@ -2464,7 +2517,7 @@ static void __link_shadow_page(struct kvm *kvm, =20 mmu_spte_set(sptep, spte); =20 - mmu_page_add_parent_pte(cache, sp, sptep); + mmu_page_add_parent_pte(kvm, cache, sp, sptep); =20 /* * The non-direct sub-pagetable must be updated before linking. For --=20 2.48.1.362.g079036d154-goog