Skip to contentSkip to conversation

KVM: Implement ring-based dirty memory tracking

PATCH Superseded Cao, Lei 2 participants 3 messages Feb 3, 2017 – Feb 4, 2017 1 message not in the archive

Subsystems (3)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Cao, Lei

author
#1 lore raw

PATCH v3 5/5 KVM: Implement ring-based dirty memory tracking

In reply to a message not in the archive

Implement ring-base dirty memory tracking.

---
 arch/x86/kvm/Makefile    |   3 +-
 include/linux/kvm_host.h |  14 ++++
 virt/kvm/gfn_ring.c      | 100 +++++++++++++++++++++++
 virt/kvm/kvm_main.c      | 209 ++++++++++++++++++++++++++++++++++++++++++++---
 4 files changed, 313 insertions(+), 13 deletions(-)
 create mode 100644 virt/kvm/gfn_ring.c
arch/x86/kvm/Makefile +2 -1
index 3bff207..d832622 100644
@@ -8,7 +8,8 @@ CFLAGS_vmx.o := -I.
 KVM := ../../../virt/kvm
 
 kvm-y			+= $(KVM)/kvm_main.o $(KVM)/coalesced_mmio.o \
-				$(KVM)/eventfd.o $(KVM)/irqchip.o $(KVM)/vfio.o
+				$(KVM)/eventfd.o $(KVM)/irqchip.o $(KVM)/vfio.o \
+				$(KVM)/gfn_ring.o
 kvm-$(CONFIG_KVM_ASYNC_PF)	+= $(KVM)/async_pf.o
 
 kvm-y			+= x86.o mmu.o emulate.o i8259.o irq.o lapic.o \
include/linux/kvm_host.h +14 -0
index 65561bf..7fd045b 100644
@@ -34,6 +34,7 @@
 #include <linux/kvm_types.h>
 
 #include <asm/kvm_host.h>
+#include <linux/kvm_gfn_ring.h>
 
 #ifndef KVM_MAX_VCPU_ID
 #define KVM_MAX_VCPU_ID KVM_MAX_VCPUS
@@ -265,6 +266,11 @@ struct kvm_vcpu {
 	bool preempted;
 	struct kvm_vcpu_arch arch;
 	struct dentry *debugfs_dentry;
+
+#ifdef KVM_DIRTY_LOG_PAGE_OFFSET
+	u32 max_dirty_logs;
+	struct kvm_gfn_ring dirty_ring;
+#endif
 };
 
 static inline int kvm_vcpu_exiting_guest_mode(struct kvm_vcpu *vcpu)
@@ -430,6 +436,12 @@ struct kvm {
 	struct list_head devices;
 	struct dentry *debugfs_dentry;
 	struct kvm_stat_data **debugfs_stat_data;
+
+#ifdef KVM_DIRTY_LOG_PAGE_OFFSET
+	u32 dirty_ring_size;
+	u32 max_dirty_logs;
+	struct kvm_gfn_ring dirty_ring;
+#endif
 };
 
 #define kvm_err(fmt, ...) \
@@ -713,6 +725,8 @@ void kvm_arch_mmu_enable_log_dirty_pt_masked(struct kvm *kvm,
 					gfn_t gfn_offset,
 					unsigned long mask);
 
+void kvm_reset_dirty_gfn(struct kvm *kvm, u32 slot, u64 offset, u64 mask);
+
 int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
 				struct kvm_dirty_log *log);
 
virt/kvm/gfn_ring.c new file +100 -0

100 lines not on this page · raw message

virt/kvm/kvm_main.c +197 -12

326 lines not on this page · raw message

Signature · 1 line
2.5.0
maintainer
#2 lore raw

Thanks, this is much nicer. I have another generic comment on the ring buffer implementation.

If you make the size a power of two, you can use free running counters, i.e. avoid the modulo or mask operation every time you write them.

For example

	reset_index = 0, dirty_index = 0 -> 0 items
	reset_index = 0, dirty_index = 512 -> 512 items
	reset_index = 1, dirty_index = 512 -> 511 items

This also removes the need to leave a free item in the ring buffer. Of course KVM_ENABLE_CAP_VM then needs to check that the size is a power of two.

Also, please add some comments to kvm_gfn_ring.h's definition of struct kvm_gfn_ring. The ring buffer is split between struct kvm_dirty_list and struct kvm_gfn_ring, so it's nice to have some documentation and a reference to struct kvm_dirty_list. Feel free to harvest my review of v2 for some text.

virt/kvm/gfn_ring.c line 38

Quoted 5 lines · Lei
virt/kvm/gfn_ring.c new file +5 -0
+	cur_slot = READ_ONCE(list->dirty_gfns[gfnring->reset_index].slot);
+	cur_offset = READ_ONCE(list->dirty_gfns[gfnring->reset_index].offset);
+	mask = 1;
+	count++;
+	gfnring->reset_index = (gfnring->reset_index + 1) % gfnring->size;

This "(x + 1) % size" recurs often.

Let's make the size a power of two and then write it like

	struct kvm_dirty_gfn *entry;
	...
	entry = &list->dirty_gfns[gfnring->reset_index
				  & (gfnring->size - 1)];

	/*
	 * The ring buffer is shared with userspace, which might mmap
	 * it and concurrently modify slot and offset.  Userspace must
	 * not be trusted!  READ_ONCE prevents the compiler from
	 * the values after they've been range-checked (the checks are
	 * in kvm_reset_dirty_gfn).
	 */
	smp_read_barrier_depends();
	cur_slot = READ_ONCE(entry->slot);
	cur_offset = READ_ONCE(entry->offset);
	gfnring->reset_index++;

virt/kvm/gfn_ring.c line 46

Quoted 8 lines
virt/kvm/gfn_ring.c new file +8 -0
+	while (gfnring->reset_index != fetch) {
+		next_slot =
+		  READ_ONCE(list->dirty_gfns[gfnring->reset_index].slot);
+		next_offset =
+		  READ_ONCE(list->dirty_gfns[gfnring->reset_index].offset);
+		if ((next_slot != cur_slot) ||
+		    (next_offset < cur_offset) ||
+		    ((next_offset - cur_offset) > (BITS_PER_LONG - 1))) {

No extra parentheses please. Some optimization and cleanup is also possible:

	entry = &list->dirty_gfns[gfnring->reset_index
				  & (gfnring->size - 1)];
	smp_read_barrier_depends();
	next_slot = READ_ONCE(entry->slot);
	next_offset = READ_ONCE(entry->offset);
	gfnring->reset_index++;

	/*
	 * Try to coalesce the reset operations when the guest is
	 * scanning pages in the same slot.
	 */
	if (next_slot == cur_slot) {
		int delta = next_offset - cur_offset;
		if (delta >= 0 && delta < BITS_PER_LONG) {
			mask |= 1ull << delta;
			continue;
		}

		/* Backwards visit, careful about overflows!  */
		if (delta >= -BITS_PER_LONG && delta < 0 &&
		    (mask << -delta >> -delta) == mask) {
			cur_offset = next_offset;
			mask = (mask << -delta) | 1;
			continue;
		}
	}

	kvm_reset_dirty_gfn(kvm, cur_slot, cur_offset, mask);
	cur_slot = next_slot;
	cur_offset = next_offset;
	mask = 1;

This should handle backwards writes in the guest more effectively (e.g. large memmove). Maybe not super common, but easy enough to handle.

Quoted 12 lines
virt/kvm/gfn_ring.c new file +12 -0
+			kvm_reset_dirty_gfn(kvm, cur_slot, cur_offset, mask);
+			cur_slot = next_slot;
+			cur_offset = next_offset;
+			mask = 1;
+		} else
+			mask |= (u64)1 << (next_offset - cur_offset);
+		count++;
+		gfnring->reset_index = (gfnring->reset_index + 1) %
+					gfnring->size;
+	}
+	kvm_reset_dirty_gfn(kvm, cur_slot, cur_offset, mask);
+

[...]

Quoted 8 lines
virt/kvm/kvm_main.c +8 -0
+#ifdef KVM_DIRTY_LOG_PAGE_OFFSET
+	if (kvm->dirty_ring_size) {
+		r = kvm_gfn_ring_alloc(&vcpu->dirty_ring,
+				       kvm->dirty_ring_size);
+		if (r) {
+			kvm->dirty_ring_size = 0;
+			goto fail_free_run;
+		}

virt/kvm/kvm_main.c int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)

Quoted 10 lines
virt/kvm/kvm_main.c +8 -0
+		vcpu->max_dirty_logs =
+			(kvm->dirty_ring_size/sizeof(struct kvm_dirty_gfn))
+			- 1 - kvm_cpu_dirty_log_size();
+	}
+#endif
 
 
+
+	kvm->dirty_ring_size = size;
+	kvm->max_dirty_logs = (size/sizeof(struct kvm_dirty_gfn)) - 1;

Should there be some legroom, in case KVM writes several pages consecutively? I would feel safer if you had perhaps 16 empty pages when exiting to the guest (and then "if (num == max)" becomes "if (num > max)".

virt/kvm/kvm_main.c static long kvm_vm_ioctl_check_extension_generic(struct kvm *kvm, long arg) · line 3036

Quoted 8 lines
virt/kvm/kvm_main.c +7 -0
+	if (vcpu) {
+		gfnlist = &vcpu->dirty_ring;
+		max = vcpu->max_dirty_logs;
+	} else {
+		gfnlist = &kvm->dirty_ring;
+		max = kvm->max_dirty_logs;
+	}

This suggests making the soft limit (max_dirty_logs) an argument of kvm_gfn_ring_alloc. kvm_gfn_ring_push can return 0 if the soft limit is not reached, 1 if it is, and -EBUSY if the hard limit is reached.

virt/kvm/gfn_ring.c line 72

Quoted 5 lines
virt/kvm/gfn_ring.c new file +4 -0
+
+	if (((gfnring->dirty_index + 1) % gfnring->size) ==
+	    gfnring->reset_index)
+		return -EBUSY;

The spin lock can be taken in the error case too. It makes it simpler to understand the code, and the error case is obviously not hot.

virt/kvm/gfn_ring.c line 81

Quoted 8 lines
virt/kvm/gfn_ring.c new file +8 -0
+	if (locked)
+		spin_lock(&gfnring->lock);
+
+	list->dirty_gfns[gfnring->dirty_index].slot = slot;
+	list->dirty_gfns[gfnring->dirty_index].offset = offset;
+	smp_wmb();
+	gfnring->dirty_index = (gfnring->dirty_index+1) % gfnring->size;
+	num = (gfnring->dirty_index - gfnring->reset_index) % gfnring->size;

Using the free running counter technique, the invariants are clearer and modulo operations do not clutter the code as much:

	if (locked)
		spin_lock(&gfnring->lock);

	num = (u16)(gfnring->dirty_index - gfnring->reset_index);
	if (num >= gfnring->size) {
		WARN_ON_ONCE(num > gfnring->size);
		r = -EBUSY;
		goto out;
	}

	entry = &list->dirty_gfns[gfnring->dirty_index
				  & (gfnring->size - 1)];
	entry->slot = slot;
	entry->offset = offset;
	smp_wmb();
	gfnring->dirty_index++;
	num++;
	r = num >= gfnring->soft_limit;
out:
	if (locked)
		spin_unlock(&gfnring->lock);

	return r;

Paolo

maintainer
#3 lore raw
In reply to Cao, Lei

virt/kvm/gfn_ring.c line 94

Quoted 5 lines · Lei
virt/kvm/gfn_ring.c new file +5 -0
+struct page *kvm_gfn_ring_get_page(struct kvm_gfn_ring *ring, u32 i)
+{
+	return vmalloc_to_page((void *)ring->dirty_list+i*PAGE_SIZE);
+
+}

Oops, you need range checking here. Otherwise you have a gaping kernel memory leak! :)

Paolo