[PATCH v3 6/6] KVM: Assert that slots_lock is held when resetting per-vCPU dirty rings

Sean Christopherson posted 6 patches 7 months ago
[PATCH v3 6/6] KVM: Assert that slots_lock is held when resetting per-vCPU dirty rings
Posted by Sean Christopherson 7 months ago
Assert that slots_lock is held in kvm_dirty_ring_reset() and add a comment
to explain _why_ slots needs to be held for the duration of the reset.

Link: https://lore.kernel.org/all/aCSns6Q5oTkdXUEe@google.com
Suggested-by: James Houghton <jthoughton@google.com>
Signed-off-by: Sean Christopherson <seanjc@google.com>
---
 virt/kvm/dirty_ring.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/virt/kvm/dirty_ring.c b/virt/kvm/dirty_ring.c
index 54734025658a..1ba02a06378c 100644
--- a/virt/kvm/dirty_ring.c
+++ b/virt/kvm/dirty_ring.c
@@ -122,6 +122,14 @@ int kvm_dirty_ring_reset(struct kvm *kvm, struct kvm_dirty_ring *ring,
 	unsigned long mask = 0;
 	struct kvm_dirty_gfn *entry;
 
+	/*
+	 * Ensure concurrent calls to KVM_RESET_DIRTY_RINGS are serialized,
+	 * e.g. so that KVM fully resets all entries processed by a given call
+	 * before returning to userspace.  Holding slots_lock also protects
+	 * the various memslot accesses.
+	 */
+	lockdep_assert_held(&kvm->slots_lock);
+
 	while (likely((*nr_entries_reset) < INT_MAX)) {
 		if (signal_pending(current))
 			return -EINTR;
-- 
2.49.0.1112.g889b7c5bd8-goog
Re: [PATCH v3 6/6] KVM: Assert that slots_lock is held when resetting per-vCPU dirty rings
Posted by Binbin Wu 6 months, 4 weeks ago

On 5/17/2025 5:35 AM, Sean Christopherson wrote:
> Assert that slots_lock is held in kvm_dirty_ring_reset() and add a comment
> to explain _why_ slots needs to be held for the duration of the reset.
>
> Link: https://lore.kernel.org/all/aCSns6Q5oTkdXUEe@google.com
> Suggested-by: James Houghton <jthoughton@google.com>
> Signed-off-by: Sean Christopherson <seanjc@google.com>
> ---
>   virt/kvm/dirty_ring.c | 8 ++++++++
>   1 file changed, 8 insertions(+)
>
> diff --git a/virt/kvm/dirty_ring.c b/virt/kvm/dirty_ring.c
> index 54734025658a..1ba02a06378c 100644
> --- a/virt/kvm/dirty_ring.c
> +++ b/virt/kvm/dirty_ring.c
> @@ -122,6 +122,14 @@ int kvm_dirty_ring_reset(struct kvm *kvm, struct kvm_dirty_ring *ring,
>   	unsigned long mask = 0;
>   	struct kvm_dirty_gfn *entry;
>   
> +	/*
> +	 * Ensure concurrent calls to KVM_RESET_DIRTY_RINGS are serialized,
> +	 * e.g. so that KVM fully resets all entries processed by a given call
It seems that "e.g." is not needed?

> +	 * before returning to userspace.  Holding slots_lock also protects
> +	 * the various memslot accesses.
> +	 */
> +	lockdep_assert_held(&kvm->slots_lock);
> +
>   	while (likely((*nr_entries_reset) < INT_MAX)) {
>   		if (signal_pending(current))
>   			return -EINTR;