[PATCH 4/4] KVM: Improve warning report in mark_page_dirty_in_slot()

From: Gavin Shan
Date: Sun Jan 15 2023 - 23:06:44 EST


There are two warning reports about the dirty ring in the function.
We have the wrong assumption that the dirty ring is always enabled when
CONFIG_HAVE_KVM_DIRTY_RING is selected. This leads to warning messages
about the dirty ring is reported even the dirty ring isn't enabled by
the user space. Actually, the expected behaviour is to report the
warning messages only when the dirty ring is enabled, instead of
being configured.

Fix it by enabling the checks and warning reports when the dirty ring
has been enabled by the user space.

Signed-off-by: Gavin Shan <gshan@xxxxxxxxxx>
---
include/linux/kvm_dirty_ring.h | 5 +++++
virt/kvm/kvm_main.c | 25 ++++++++++++++-----------
2 files changed, 19 insertions(+), 11 deletions(-)

diff --git a/include/linux/kvm_dirty_ring.h b/include/linux/kvm_dirty_ring.h
index 4862c98d80d3..3fda0aa42858 100644
--- a/include/linux/kvm_dirty_ring.h
+++ b/include/linux/kvm_dirty_ring.h
@@ -42,6 +42,11 @@ static inline bool kvm_use_dirty_bitmap(struct kvm *kvm)
return true;
}

+static inline bool kvm_arch_allow_write_without_running_vcpu(struct kvm *kvm)
+{
+ return false;
+}
+
static inline int kvm_dirty_ring_alloc(struct kvm_dirty_ring *ring,
int index, u32 size)
{
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 90f538433916..a35c32bc84e1 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -3316,26 +3316,29 @@ void mark_page_dirty_in_slot(struct kvm *kvm,
const struct kvm_memory_slot *memslot,
gfn_t gfn)
{
- struct kvm_vcpu *vcpu = kvm_get_running_vcpu();
+ struct kvm_vcpu *vcpu;
unsigned long rel_gfn;
u32 slot;

-#ifdef CONFIG_HAVE_KVM_DIRTY_RING
- if (WARN_ON_ONCE(vcpu && vcpu->kvm != kvm))
- return;
-
- WARN_ON_ONCE(!vcpu && !kvm_arch_allow_write_without_running_vcpu(kvm));
-#endif
-
if (!memslot || !kvm_slot_dirty_track_enabled(memslot))
return;

rel_gfn = gfn - memslot->base_gfn;
slot = (memslot->as_id << 16) | memslot->id;

- if (kvm->dirty_ring_size && vcpu)
- kvm_dirty_ring_push(vcpu, slot, rel_gfn);
- else if (memslot->dirty_bitmap)
+ if (kvm->dirty_ring_size) {
+ vcpu = kvm_get_running_vcpu();
+ if (vcpu) {
+ if (!WARN_ON_ONCE(vcpu->kvm != kvm))
+ kvm_dirty_ring_push(vcpu, slot, rel_gfn);
+
+ return;
+ }
+
+ WARN_ON_ONCE(!kvm_arch_allow_write_without_running_vcpu(kvm));
+ }
+
+ if (memslot->dirty_bitmap)
set_bit_le(rel_gfn, memslot->dirty_bitmap);
}
EXPORT_SYMBOL_GPL(mark_page_dirty_in_slot);
--
2.23.0