Skip to contentSkip to conversation

KVM: x86: enable dirty log gradually in small chunks

PATCH Superseded Jay Zhou 5 participants 9 messages Feb 18, 2020 – Feb 20, 2020

Subsystems (3)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Jay Zhou

author
#0 lore raw

PATCH 1/1 KVM: x86: enable dirty log gradually in small chunks

It could take kvm->mmu_lock for an extended period of time when enabling dirty log for the first time. The main cost is to clear all the D-bits of last level SPTEs. This situation can benefit from manual dirty log protect as well, which can reduce the mmu_lock time taken. The sequence is like this:

1. Set all the bits of the first dirty bitmap to 1 when enabling dirty log for the first time
2. Only write protect the huge pages
3. KVM_GET_DIRTY_LOG returns the dirty bitmap info
4. KVM_CLEAR_DIRTY_LOG will clear D-bit for each of the leaf level SPTEs gradually in small chunks

Under the Intel(R) Xeon(R) Gold 6152 CPU @ 2.10GHz environment, I did some tests with a 128G windows VM and counted the time taken of memory_global_dirty_log_start, here is the numbers:

VM Size        Before    After optimization
128G           460ms     10ms
---
 arch/x86/kvm/vmx/vmx.c   |  5 +++++
 include/linux/kvm_host.h |  5 +++++
 virt/kvm/kvm_main.c      | 10 ++++++++--
 3 files changed, 18 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx/vmx.c +5 -0
index 3be25ec..a8d64f6 100644
@@ -7201,7 +7201,12 @@ static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
 static void vmx_slot_enable_log_dirty(struct kvm *kvm,
 				     struct kvm_memory_slot *slot)
 {
+#if CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT
+	if (!kvm->manual_dirty_log_protect)
+		kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
+#else
 	kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
+#endif
 	kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
 }
 
include/linux/kvm_host.h +5 -0
index e89eb67..fd149b0 100644
@@ -360,6 +360,11 @@ static inline unsigned long *kvm_second_dirty_bitmap(struct kvm_memory_slot *mem
 	return memslot->dirty_bitmap + len / sizeof(*memslot->dirty_bitmap);
 }
 
+static inline void kvm_set_first_dirty_bitmap(struct kvm_memory_slot *memslot)
+{
+	bitmap_set(memslot->dirty_bitmap, 0, memslot->npages);
+}
+
 struct kvm_s390_adapter_int {
 	u64 ind_addr;
 	u64 summary_addr;
virt/kvm/kvm_main.c +8 -2
index 70f03ce..08565ed 100644
@@ -862,7 +862,8 @@ static int kvm_vm_release(struct inode *inode, struct file *filp)
  * Allocation size is twice as large as the actual dirty bitmap size.
  * See x86's kvm_vm_ioctl_get_dirty_log() why this is needed.
  */
-static int kvm_create_dirty_bitmap(struct kvm_memory_slot *memslot)
+static int kvm_create_dirty_bitmap(struct kvm *kvm,
+				struct kvm_memory_slot *memslot)
 {
 	unsigned long dirty_bytes = 2 * kvm_dirty_bitmap_bytes(memslot);
 
@@ -870,6 +871,11 @@ static int kvm_create_dirty_bitmap(struct kvm_memory_slot *memslot)
 	if (!memslot->dirty_bitmap)
 		return -ENOMEM;
 
+#if CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT
+	if (kvm->manual_dirty_log_protect)
+		kvm_set_first_dirty_bitmap(memslot);
+#endif
+
 	return 0;
 }
 
@@ -1094,7 +1100,7 @@ int __kvm_set_memory_region(struct kvm *kvm,
 
 	/* Allocate page dirty bitmap if needed */
 	if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
-		if (kvm_create_dirty_bitmap(&new) < 0)
+		if (kvm_create_dirty_bitmap(kvm, &new) < 0)
 			goto out_free;
 	}
 
Signature · 1 line
1.8.3.1
maintainer
#1 lore raw
Quoted 13 lines · Jay Zhou

It could take kvm->mmu_lock for an extended period of time when enabling dirty log for the first time. The main cost is to clear all the D-bits of last level SPTEs. This situation can benefit from manual dirty log protect as well, which can reduce the mmu_lock time taken. The sequence is like this:

1. Set all the bits of the first dirty bitmap to 1 when enabling dirty log for the first time
2. Only write protect the huge pages
3. KVM_GET_DIRTY_LOG returns the dirty bitmap info
4. KVM_CLEAR_DIRTY_LOG will clear D-bit for each of the leaf level SPTEs gradually in small chunks

Quoted 6 lines

Under the Intel(R) Xeon(R) Gold 6152 CPU @ 2.10GHz environment, I did some tests with a 128G windows VM and counted the time taken of memory_global_dirty_log_start, here is the numbers:

VM Size        Before    After optimization
128G           460ms     10ms

This is a good idea, but could userspace expect the bitmap to be 0 for pages that haven't been touched? I think this should be added as a new bit to the KVM_ENABLE_CAP for KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2. That is:

- in kvm_vm_ioctl_check_extension_generic, return 3 for KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2 (better: define two constants KVM_DIRTY_LOG_MANUAL_PROTECT as 1 and KVM_DIRTY_LOG_INITIALLY_SET as 2).

- in kvm_vm_ioctl_enable_cap_generic, allow bit 0 and bit 1 for cap->args[0]

- in kvm_vm_ioctl_enable_cap_generic, check "if (!(kvm->manual_dirty_log_protect & KVM_DIRTY_LOG_INITIALLY_SET))".

Thanks,

Paolo

Quoted 76 lines
---
 arch/x86/kvm/vmx/vmx.c   |  5 +++++
 include/linux/kvm_host.h |  5 +++++
 virt/kvm/kvm_main.c      | 10 ++++++++--
 3 files changed, 18 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx/vmx.c +5 -0
index 3be25ec..a8d64f6 100644
@@ -7201,7 +7201,12 @@ static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
 static void vmx_slot_enable_log_dirty(struct kvm *kvm,
 				     struct kvm_memory_slot *slot)
 {
+#if CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT
+	if (!kvm->manual_dirty_log_protect)
+		kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
+#else
 	kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
+#endif
 	kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
 }
 
include/linux/kvm_host.h +5 -0
index e89eb67..fd149b0 100644
@@ -360,6 +360,11 @@ static inline unsigned long *kvm_second_dirty_bitmap(struct kvm_memory_slot *mem
 	return memslot->dirty_bitmap + len / sizeof(*memslot->dirty_bitmap);
 }
 
+static inline void kvm_set_first_dirty_bitmap(struct kvm_memory_slot *memslot)
+{
+	bitmap_set(memslot->dirty_bitmap, 0, memslot->npages);
+}
+
 struct kvm_s390_adapter_int {
 	u64 ind_addr;
 	u64 summary_addr;
virt/kvm/kvm_main.c +8 -2
index 70f03ce..08565ed 100644
@@ -862,7 +862,8 @@ static int kvm_vm_release(struct inode *inode, struct file *filp)
  * Allocation size is twice as large as the actual dirty bitmap size.
  * See x86's kvm_vm_ioctl_get_dirty_log() why this is needed.
  */
-static int kvm_create_dirty_bitmap(struct kvm_memory_slot *memslot)
+static int kvm_create_dirty_bitmap(struct kvm *kvm,
+				struct kvm_memory_slot *memslot)
 {
 	unsigned long dirty_bytes = 2 * kvm_dirty_bitmap_bytes(memslot);
 
@@ -870,6 +871,11 @@ static int kvm_create_dirty_bitmap(struct kvm_memory_slot *memslot)
 	if (!memslot->dirty_bitmap)
 		return -ENOMEM;
 
+#if CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT
+	if (kvm->manual_dirty_log_protect)
+		kvm_set_first_dirty_bitmap(memslot);
+#endif
+
 	return 0;
 }
 
@@ -1094,7 +1100,7 @@ int __kvm_set_memory_region(struct kvm *kvm,
 
 	/* Allocate page dirty bitmap if needed */
 	if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
-		if (kvm_create_dirty_bitmap(&new) < 0)
+		if (kvm_create_dirty_bitmap(kvm, &new) < 0)
 			goto out_free;
 	}
 
#2 lore raw

Hi Paolo,

Quoted 27 lines

-----Original Message-----
From: Paolo Bonzini [mailto:[email protected]]
Sent: Tuesday, February 18, 2020 7:40 PM
To: Zhoujian (jay) <[email protected]>; [email protected]
Cc: [email protected]; wangxin (U) <[email protected]>; linfeng (M) <[email protected]>; Huangweidong (C) <[email protected]>
Subject: Re: [PATCH] KVM: x86: enable dirty log gradually in small chunks

Quoted 17 lines · Jay Zhou

It could take kvm->mmu_lock for an extended period of time when enabling dirty log for the first time. The main cost is to clear all the D-bits of last level SPTEs. This situation can benefit from manual dirty log protect as well, which can reduce the mmu_lock time taken. The sequence is like this:

1. Set all the bits of the first dirty bitmap to 1 when enabling dirty log for the first time
2. Only write protect the huge pages
3. KVM_GET_DIRTY_LOG returns the dirty bitmap info 4. KVM_CLEAR_DIRTY_LOG will clear D-bit for each of the leaf level SPTEs gradually in small chunks

Under the Intel(R) Xeon(R) Gold 6152 CPU @ 2.10GHz environment, I did some tests with a 128G windows VM and counted the time taken of memory_global_dirty_log_start, here is the numbers:

Quoted 5 lines
Quoted 2 lines
VM Size        Before    After optimization
128G           460ms     10ms

This is a good idea, but could userspace expect the bitmap to be 0 for pages that haven't been touched?

The userspace gets the bitmap information only from the kernel side.
It depends on the kernel side to distinguish whether the pages have been touched I think, which using the rmap to traverse for now. I haven't the other ideas yet, :-(

But even though the userspace gets 1 for pages that haven't been touched, these pages will be filtered out too in the kernel space KVM_CLEAR_DIRTY_LOG ioctl path, since the rmap does not exist I think.

Quoted 8 lines

I think this should be added as a new bit to the KVM_ENABLE_CAP for KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2. That is:

- in kvm_vm_ioctl_check_extension_generic, return 3 for KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2 (better: define two constants KVM_DIRTY_LOG_MANUAL_PROTECT as 1 and KVM_DIRTY_LOG_INITIALLY_SET as 2).

Quoted 4 lines

- in kvm_vm_ioctl_enable_cap_generic, allow bit 0 and bit 1 for cap->args[0]

- in kvm_vm_ioctl_enable_cap_generic, check "if (!(kvm->manual_dirty_log_protect & KVM_DIRTY_LOG_INITIALLY_SET))".

Thanks for the details! I'll add them in the next version.

Regards,
Jay Zhou

Quoted 81 lines

Thanks,

Paolo

include/linux/kvm_host.h static inline unsigned long

Quoted 26 lines
---
 arch/x86/kvm/vmx/vmx.c   |  5 +++++
 include/linux/kvm_host.h |  5 +++++
 virt/kvm/kvm_main.c      | 10 ++++++++--
 3 files changed, 18 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx/vmx.c index renamed from arch/x86/kvm/vmx/vmx.c +0 -0

3be25ec..a8d64f6 100644

arch/x86/kvm/vmx/vmx.c +4 -0
@@ -7201,7 +7201,12 @@ static void vmx_sched_in(struct kvm_vcpu *vcpu,
 int cpu)  static void vmx_slot_enable_log_dirty(struct kvm *kvm,
 				     struct kvm_memory_slot *slot)  {
+#if CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT
+	if (!kvm->manual_dirty_log_protect)
+		kvm_mmu_slot_leaf_clear_dirty(kvm, slot); #else
 	kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
+#endif
 	kvm_mmu_slot_largepage_remove_write_access(kvm, slot);  }
 
include/linux/kvm_host.h index renamed from include/linux/kvm_host.h +0 -0

e89eb67..fd149b0 100644

include/linux/kvm_host.h +0 -0
@@ -360,6 +360,11 @@ static inline unsigned long

*kvm_second_dirty_bitmap(struct kvm_memory_slot *mem

virt/kvm/kvm_main.c static int kvm_vm_release(struct inode *inode,

Quoted 15 lines
include/linux/kvm_host.h +4 -0
 	return memslot->dirty_bitmap + len / sizeof(*memslot->dirty_bitmap);
 }
 
+static inline void kvm_set_first_dirty_bitmap(struct kvm_memory_slot
+*memslot) {
+	bitmap_set(memslot->dirty_bitmap, 0, memslot->npages); }
+
 struct kvm_s390_adapter_int {
 	u64 ind_addr;
 	u64 summary_addr;
virt/kvm/kvm_main.c index renamed from virt/kvm/kvm_main.c +0 -0

70f03ce..08565ed 100644

virt/kvm/kvm_main.c +0 -0
@@ -862,7 +862,8 @@ static int kvm_vm_release(struct inode *inode,

struct file *filp)

virt/kvm/kvm_main.c static int kvm_create_dirty_bitmap(struct

Quoted 10 lines
virt/kvm/kvm_main.c +2 -1
  * Allocation size is twice as large as the actual dirty bitmap size.
  * See x86's kvm_vm_ioctl_get_dirty_log() why this is needed.
  */
-static int kvm_create_dirty_bitmap(struct kvm_memory_slot *memslot)
+static int kvm_create_dirty_bitmap(struct kvm *kvm,
+				struct kvm_memory_slot *memslot)
 {
 	unsigned long dirty_bytes = 2 * kvm_dirty_bitmap_bytes(memslot);
 
@@ -870,6 +871,11 @@ static int kvm_create_dirty_bitmap(struct

kvm_memory_slot *memslot)

Quoted 21 lines
virt/kvm/kvm_main.c +6 -1
 	if (!memslot->dirty_bitmap)
 		return -ENOMEM;
 
+#if CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT
+	if (kvm->manual_dirty_log_protect)
+		kvm_set_first_dirty_bitmap(memslot);
+#endif
+
 	return 0;
 }
 
@@ -1094,7 +1100,7 @@ int __kvm_set_memory_region(struct kvm *kvm,
 
 	/* Allocate page dirty bitmap if needed */
 	if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
-		if (kvm_create_dirty_bitmap(&new) < 0)
+		if (kvm_create_dirty_bitmap(kvm, &new) < 0)
 			goto out_free;
 	}
 
#3 lore raw
Quoted 51 lines · Zhoujian (jay)

Hi Paolo,

Quoted 27 lines

-----Original Message-----
From: Paolo Bonzini [mailto:[email protected]]
Sent: Tuesday, February 18, 2020 7:40 PM
To: Zhoujian (jay) <[email protected]>; [email protected]
Cc: [email protected]; wangxin (U) <[email protected]>; linfeng (M) <[email protected]>; Huangweidong (C) <[email protected]>
Subject: Re: [PATCH] KVM: x86: enable dirty log gradually in small chunks

Quoted 17 lines · Jay Zhou

It could take kvm->mmu_lock for an extended period of time when enabling dirty log for the first time. The main cost is to clear all the D-bits of last level SPTEs. This situation can benefit from manual dirty log protect as well, which can reduce the mmu_lock time taken. The sequence is like this:

1. Set all the bits of the first dirty bitmap to 1 when enabling dirty log for the first time
2. Only write protect the huge pages
3. KVM_GET_DIRTY_LOG returns the dirty bitmap info 4. KVM_CLEAR_DIRTY_LOG will clear D-bit for each of the leaf level SPTEs gradually in small chunks

Under the Intel(R) Xeon(R) Gold 6152 CPU @ 2.10GHz environment, I did some tests with a 128G windows VM and counted the time taken of memory_global_dirty_log_start, here is the numbers:

Quoted 5 lines
Quoted 2 lines
VM Size        Before    After optimization
128G           460ms     10ms

This is a good idea, but could userspace expect the bitmap to be 0 for pages that haven't been touched?

The userspace gets the bitmap information only from the kernel side.
It depends on the kernel side to distinguish whether the pages have been touched I think, which using the rmap to traverse for now. I haven't the other ideas yet, :-(

But even though the userspace gets 1 for pages that haven't been touched, these pages will be filtered out too in the kernel space KVM_CLEAR_DIRTY_LOG ioctl path, since the rmap does not exist I think.

Quoted 8 lines

I think this should be added as a new bit to the KVM_ENABLE_CAP for KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2. That is:

- in kvm_vm_ioctl_check_extension_generic, return 3 for KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2 (better: define two constants KVM_DIRTY_LOG_MANUAL_PROTECT as 1 and KVM_DIRTY_LOG_INITIALLY_SET as 2).

Quoted 6 lines
Quoted 4 lines

- in kvm_vm_ioctl_enable_cap_generic, allow bit 0 and bit 1 for cap->args[0]

- in kvm_vm_ioctl_enable_cap_generic, check "if (!(kvm->manual_dirty_log_protect & KVM_DIRTY_LOG_INITIALLY_SET))".

Thanks for the details! I'll add them in the next version.

I agree with Paolo that we'd better introduce a new bit for the change, because we don't know whether userspace has the assumption with a zeroed dirty bitmap as initial state (which is still part of the kernel ABI IIUC, actually that could be a good thing for some userspace).

Another question is that I see you only modified the PML path. Could this also benefit the rest (say, SPTE write protects)?

Thanks,

Signature · 1 line
Peter Xu
#4 lore raw

Hi Peter,

Quoted 74 lines

-----Original Message-----
From: Peter Xu [mailto:[email protected]]
Sent: Wednesday, February 19, 2020 1:26 AM
To: Zhoujian (jay) <[email protected]>
Cc: Paolo Bonzini <[email protected]>; [email protected]; wangxin (U) <[email protected]>; linfeng (M) <[email protected]>; Huangweidong (C) <[email protected]>; Liujinsong (Paul) <[email protected]>
Subject: Re: [PATCH] KVM: x86: enable dirty log gradually in small chunks

Quoted 17 lines · Zhoujian (jay)

Hi Paolo,

Quoted 15 lines

-----Original Message-----
From: Paolo Bonzini [mailto:[email protected]]
Sent: Tuesday, February 18, 2020 7:40 PM
To: Zhoujian (jay) <[email protected]>; [email protected]
Cc: [email protected]; wangxin (U) <[email protected]>; linfeng (M) <[email protected]>; Huangweidong (C) <[email protected]>
Subject: Re: [PATCH] KVM: x86: enable dirty log gradually in small chunks

Quoted 4 lines · Jay Zhou

It could take kvm->mmu_lock for an extended period of time when enabling dirty log for the first time. The main cost is to clear all the D-bits of last level SPTEs. This situation can benefit from manual dirty log protect as well, which can reduce the mmu_lock

time taken.

Quoted 19 lines
Quoted 13 lines
Quoted 13 lines

The sequence is like this:

1. Set all the bits of the first dirty bitmap to 1 when enabling dirty log for the first time
2. Only write protect the huge pages 3. KVM_GET_DIRTY_LOG returns the dirty bitmap info 4.
KVM_CLEAR_DIRTY_LOG will clear D-bit for each of the leaf level
SPTEs gradually in small chunks

Under the Intel(R) Xeon(R) Gold 6152 CPU @ 2.10GHz environment, I did some tests with a 128G windows VM and counted the time taken of memory_global_dirty_log_start, here is the numbers:

Quoted 5 lines
Quoted 2 lines
VM Size        Before    After optimization
128G           460ms     10ms

This is a good idea, but could userspace expect the bitmap to be 0 for pages that haven't been touched?

Quoted 15 lines

The userspace gets the bitmap information only from the kernel side. It depends on the kernel side to distinguish whether the pages have been touched I think, which using the rmap to traverse for now. I haven't the other ideas yet, :-(

But even though the userspace gets 1 for pages that haven't been touched, these pages will be filtered out too in the kernel space KVM_CLEAR_DIRTY_LOG ioctl path, since the rmap does not exist I think.

Quoted 6 lines

I think this should be added as a new bit to the KVM_ENABLE_CAP for KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2. That is:

- in kvm_vm_ioctl_check_extension_generic, return 3 for KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2 (better: define two constants KVM_DIRTY_LOG_MANUAL_PROTECT as 1 and

KVM_DIRTY_LOG_INITIALLY_SET as

Quoted 9 lines
Quoted 7 lines

2).

- in kvm_vm_ioctl_enable_cap_generic, allow bit 0 and bit 1 for cap->args[0]

- in kvm_vm_ioctl_enable_cap_generic, check "if (!(kvm->manual_dirty_log_protect & KVM_DIRTY_LOG_INITIALLY_SET))".

Thanks for the details! I'll add them in the next version.

Quoted 7 lines

I agree with Paolo that we'd better introduce a new bit for the change, because we don't know whether userspace has the assumption with a zeroed dirty bitmap as initial state (which is still part of the kernel ABI IIUC, actually that could be a good thing for some userspace).

Another question is that I see you only modified the PML path. Could this also benefit the rest (say, SPTE write protects)?

Oh I missed the other path, thanks for reminding, I'll add it in V2.

Regards,
Jay Zhou

Quoted 5 lines

Thanks,

--
Peter Xu

#5 lore raw
In reply to Jay Zhou
Quoted 32 lines · Jay Zhou

It could take kvm->mmu_lock for an extended period of time when enabling dirty log for the first time. The main cost is to clear all the D-bits of last level SPTEs. This situation can benefit from manual dirty log protect as well, which can reduce the mmu_lock time taken. The sequence is like this:

1. Set all the bits of the first dirty bitmap to 1 when enabling dirty log for the first time
2. Only write protect the huge pages
3. KVM_GET_DIRTY_LOG returns the dirty bitmap info
4. KVM_CLEAR_DIRTY_LOG will clear D-bit for each of the leaf level SPTEs gradually in small chunks

Under the Intel(R) Xeon(R) Gold 6152 CPU @ 2.10GHz environment, I did some tests with a 128G windows VM and counted the time taken of memory_global_dirty_log_start, here is the numbers:

VM Size        Before    After optimization
128G           460ms     10ms
---
 arch/x86/kvm/vmx/vmx.c   |  5 +++++
 include/linux/kvm_host.h |  5 +++++
 virt/kvm/kvm_main.c      | 10 ++++++++--
 3 files changed, 18 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx/vmx.c +0 -0
index 3be25ec..a8d64f6 100644

arch/x86/kvm/vmx/vmx.c static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu) · line 7209

Quoted 9 lines
arch/x86/kvm/vmx/vmx.c +5 -0
@@ -7201,7 +7201,12 @@ static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
 static void vmx_slot_enable_log_dirty(struct kvm *kvm,
 				     struct kvm_memory_slot *slot)
 {
+#if CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT
+	if (!kvm->manual_dirty_log_protect)
+		kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
+#else
 	kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
+#endif

The ifdef is unnecessary, this is in VMX (x86) code, i.e. CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT is guaranteed to be defined.

Quoted 8 lines
arch/x86/kvm/vmx/vmx.c +0 -0
 	kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
 }
 
include/linux/kvm_host.h +0 -0
index e89eb67..fd149b0 100644

include/linux/kvm_host.h static inline unsigned long *kvm_second_dirty_bitmap(struct kvm_memory_slot *mem · line 366

Quoted 7 lines
include/linux/kvm_host.h +4 -0
@@ -360,6 +360,11 @@ static inline unsigned long *kvm_second_dirty_bitmap(struct kvm_memory_slot *mem
 	return memslot->dirty_bitmap + len / sizeof(*memslot->dirty_bitmap);
 }
 
+static inline void kvm_set_first_dirty_bitmap(struct kvm_memory_slot *memslot)
+{
+	bitmap_set(memslot->dirty_bitmap, 0, memslot->npages);
+}

I'd prefer this be open coded with a comment, e.g. "first" is misleading because it's really "initial dirty bitmap for this memslot after enabling dirty logging".

Quoted 19 lines
include/linux/kvm_host.h +1 -0
+
 struct kvm_s390_adapter_int {
 	u64 ind_addr;
 	u64 summary_addr;
virt/kvm/kvm_main.c +2 -1
index 70f03ce..08565ed 100644
@@ -862,7 +862,8 @@ static int kvm_vm_release(struct inode *inode, struct file *filp)
  * Allocation size is twice as large as the actual dirty bitmap size.
  * See x86's kvm_vm_ioctl_get_dirty_log() why this is needed.
  */
-static int kvm_create_dirty_bitmap(struct kvm_memory_slot *memslot)
+static int kvm_create_dirty_bitmap(struct kvm *kvm,
+				struct kvm_memory_slot *memslot)
 {
 	unsigned long dirty_bytes = 2 * kvm_dirty_bitmap_bytes(memslot);
 

virt/kvm/kvm_main.c static int kvm_create_dirty_bitmap(struct kvm_memory_slot *memslot) · line 874

Quoted 4 lines
virt/kvm/kvm_main.c +1 -0
@@ -870,6 +871,11 @@ static int kvm_create_dirty_bitmap(struct kvm_memory_slot *memslot)
 	if (!memslot->dirty_bitmap)
 		return -ENOMEM;
 
+#if CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT

The ifdef is unnecessary, manual_dirty_log_protect always exists and is guaranteed to be false if CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT=n. This isn't exactly a hot path so saving the uop isn't worth the #ifdef.

virt/kvm/kvm_main.c int __kvm_set_memory_region(struct kvm *kvm, · line 1103

Quoted 13 lines
virt/kvm/kvm_main.c +5 -1
+	if (kvm->manual_dirty_log_protect)
+		kvm_set_first_dirty_bitmap(memslot);
+#endif
+
 	return 0;
 }
 
@@ -1094,7 +1100,7 @@ int __kvm_set_memory_region(struct kvm *kvm,
 
 	/* Allocate page dirty bitmap if needed */
 	if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
-		if (kvm_create_dirty_bitmap(&new) < 0)
+		if (kvm_create_dirty_bitmap(kvm, &new) < 0)

Rather than pass @kvm, what about doing bitmap_set() in __kvm_set_memory_region() and s/kvm_create_dirty_bitmap/kvm_alloc_dirty_bitmap to make it clear that the helper is only responsible for allocation? And opportunistically drop the superfluous "< 0", e.g.

	if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
		if (kvm_alloc_dirty_bitmap(&new))
			goto out_free;

		/*
		 * WORDS!
		 */		
		if (kvm->manual_dirty_log_protect)
			bitmap_set(memslot->dirty_bitmap, 0, memslot->npages);
	}
Quoted 7 lines
virt/kvm/kvm_main.c +0 -0
 			goto out_free;
 	}
 
Signature · 1 line
1.8.3.1
#6 lore raw

Hi Sean,

Quoted 37 lines

-----Original Message-----
From: Sean Christopherson [mailto:[email protected]]
Sent: Wednesday, February 19, 2020 5:23 AM
To: Zhoujian (jay) <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; wangxin (U) <[email protected]>; linfeng (M) <[email protected]>; Huangweidong (C) <[email protected]>
Subject: Re: [PATCH] KVM: x86: enable dirty log gradually in small chunks

Quoted 27 lines · Jay Zhou

It could take kvm->mmu_lock for an extended period of time when enabling dirty log for the first time. The main cost is to clear all the D-bits of last level SPTEs. This situation can benefit from manual dirty log protect as well, which can reduce the mmu_lock time taken. The sequence is like this:

1. Set all the bits of the first dirty bitmap to 1 when enabling dirty log for the first time
2. Only write protect the huge pages
3. KVM_GET_DIRTY_LOG returns the dirty bitmap info 4. KVM_CLEAR_DIRTY_LOG will clear D-bit for each of the leaf level SPTEs gradually in small chunks

Under the Intel(R) Xeon(R) Gold 6152 CPU @ 2.10GHz environment, I did some tests with a 128G windows VM and counted the time taken of memory_global_dirty_log_start, here is the numbers:

VM Size        Before    After optimization
128G           460ms     10ms
---
 arch/x86/kvm/vmx/vmx.c   |  5 +++++
 include/linux/kvm_host.h |  5 +++++
 virt/kvm/kvm_main.c      | 10 ++++++++--
 3 files changed, 18 insertions(+), 2 deletions(-)
Quoted 16 lines

arch/x86/kvm/vmx/vmx.c static void vmx_sched_in(struct kvm_vcpu *vcpu, · line 7207

Quoted 12 lines
arch/x86/kvm/vmx/vmx.c index renamed from arch/x86/kvm/vmx/vmx.c +0 -0

3be25ec..a8d64f6 100644

arch/x86/kvm/vmx/vmx.c +4 -0
@@ -7201,7 +7201,12 @@ static void vmx_sched_in(struct kvm_vcpu *vcpu,
 int cpu)  static void vmx_slot_enable_log_dirty(struct kvm *kvm,
 				     struct kvm_memory_slot *slot)  {
+#if CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT
+	if (!kvm->manual_dirty_log_protect)
+		kvm_mmu_slot_leaf_clear_dirty(kvm, slot); #else
 	kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
+#endif

The ifdef is unnecessary, this is in VMX (x86) code, i.e. CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT is guaranteed to be defined.

I agree.

Quoted 12 lines

include/linux/kvm_host.h static inline unsigned long

Quoted 8 lines
arch/x86/kvm/vmx/vmx.c +0 -0
 	kvm_mmu_slot_largepage_remove_write_access(kvm, slot);  }
 
include/linux/kvm_host.h index renamed from include/linux/kvm_host.h +0 -0

e89eb67..fd149b0 100644

include/linux/kvm_host.h +0 -0
@@ -360,6 +360,11 @@ static inline unsigned long

*kvm_second_dirty_bitmap(struct kvm_memory_slot *mem

Quoted 3 lines
include/linux/kvm_host.h +0 -0
 	return memslot->dirty_bitmap + len / sizeof(*memslot->dirty_bitmap);
 }
 
Quoted 6 lines

include/linux/kvm_host.h static inline unsigned long · line 365

Quoted 3 lines
include/linux/kvm_host.h +3 -0
+static inline void kvm_set_first_dirty_bitmap(struct kvm_memory_slot
+*memslot) {
+	bitmap_set(memslot->dirty_bitmap, 0, memslot->npages); }

I'd prefer this be open coded with a comment, e.g. "first" is misleading because it's really "initial dirty bitmap for this memslot after enabling dirty logging".

kvm_create_dirty_bitmap allocates twice size as large as the actual dirty bitmap size, and there is kvm_second_dirty_bitmap to get the second part of the map, this is the reason why I use first_dirty_bitmap here, which means the first part (not first time) of the dirty bitmap.

I'll try to be more clear if this is misleading...

Quoted 24 lines

virt/kvm/kvm_main.c static int kvm_vm_release(struct inode *inode,

Quoted 9 lines
include/linux/kvm_host.h +1 -0
+
 struct kvm_s390_adapter_int {
 	u64 ind_addr;
 	u64 summary_addr;
virt/kvm/kvm_main.c index renamed from virt/kvm/kvm_main.c +0 -0

70f03ce..08565ed 100644

virt/kvm/kvm_main.c +0 -0
@@ -862,7 +862,8 @@ static int kvm_vm_release(struct inode *inode,

struct file *filp)

virt/kvm/kvm_main.c static int kvm_create_dirty_bitmap(struct

Quoted 10 lines
virt/kvm/kvm_main.c +2 -1
  * Allocation size is twice as large as the actual dirty bitmap size.
  * See x86's kvm_vm_ioctl_get_dirty_log() why this is needed.
  */
-static int kvm_create_dirty_bitmap(struct kvm_memory_slot *memslot)
+static int kvm_create_dirty_bitmap(struct kvm *kvm,
+				struct kvm_memory_slot *memslot)
 {
 	unsigned long dirty_bytes = 2 * kvm_dirty_bitmap_bytes(memslot);
 
@@ -870,6 +871,11 @@ static int kvm_create_dirty_bitmap(struct

kvm_memory_slot *memslot)

Quoted 3 lines
virt/kvm/kvm_main.c +0 -0
 	if (!memslot->dirty_bitmap)
 		return -ENOMEM;
 
Quoted 6 lines

virt/kvm/kvm_main.c static int kvm_create_dirty_bitmap(struct · line 874

Quoted 1 line
virt/kvm/kvm_main.c +1 -0
+#if CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT

The ifdef is unnecessary, manual_dirty_log_protect always exists and is guaranteed to be false if CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT=n. This isn't exactly a hot path so saving the uop isn't worth the #ifdef.

After rereading the code, I think you're right.

Quoted 21 lines
Quoted 9 lines
virt/kvm/kvm_main.c +4 -0
+	if (kvm->manual_dirty_log_protect)
+		kvm_set_first_dirty_bitmap(memslot);
+#endif
+
 	return 0;
 }
 

virt/kvm/kvm_main.c int __kvm_set_memory_region(struct kvm *kvm, · line 1103

Quoted 5 lines
virt/kvm/kvm_main.c +1 -1
@@ -1094,7 +1100,7 @@ int __kvm_set_memory_region(struct kvm *kvm,
 
 	/* Allocate page dirty bitmap if needed */
 	if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
-		if (kvm_create_dirty_bitmap(&new) < 0)
+		if (kvm_create_dirty_bitmap(kvm, &new) < 0)

Rather than pass @kvm, what about doing bitmap_set() in __kvm_set_memory_region() and s/kvm_create_dirty_bitmap/kvm_alloc_dirty_bitmap to make it clear that the helper is only responsible for allocation? And opportunistically drop the superfluous "< 0", e.g.

Quoted 10 lines
	if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
		if (kvm_alloc_dirty_bitmap(&new))
			goto out_free;

		/*
		 * WORDS!
		 */
		if (kvm->manual_dirty_log_protect)
			bitmap_set(memslot->dirty_bitmap, 0, memslot->npages);
	}

Seems to be more clear, thanks for the suggestion.

Regards,
Jay Zhou

Quoted 7 lines
Quoted 7 lines
virt/kvm/kvm_main.c +0 -1
 			goto out_free;
 	}
 
--

1.8.3.1

#7 lore raw
Quoted 14 lines · Zhoujian (jay)
Quoted 7 lines

include/linux/kvm_host.h static inline unsigned long

Quoted 3 lines
include/linux/kvm_host.h +0 -0
@@ -360,6 +360,11 @@ static inline unsigned long

*kvm_second_dirty_bitmap(struct kvm_memory_slot *mem

Quoted 3 lines
include/linux/kvm_host.h +0 -0
 	return memslot->dirty_bitmap + len / sizeof(*memslot->dirty_bitmap);
 }
 
Quoted 6 lines

include/linux/kvm_host.h static inline unsigned long · line 365

Quoted 3 lines
include/linux/kvm_host.h +3 -0
+static inline void kvm_set_first_dirty_bitmap(struct kvm_memory_slot
+*memslot) {
+	bitmap_set(memslot->dirty_bitmap, 0, memslot->npages); }

I'd prefer this be open coded with a comment, e.g. "first" is misleading because it's really "initial dirty bitmap for this memslot after enabling dirty logging".

Quoted 4 lines

kvm_create_dirty_bitmap allocates twice size as large as the actual dirty bitmap size, and there is kvm_second_dirty_bitmap to get the second part of the map, this is the reason why I use first_dirty_bitmap here, which means the first part (not first time) of the dirty bitmap.

Ha, I didn't consider that usage of "first", obviously :-)

Quoted 1 line

I'll try to be more clear if this is misleading...

#8 lore raw
In reply to Jay Zhou
Thank you for the patch! Perhaps something to improve:

Hi Jay,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on kvm/linux-next]
[also build test WARNING on vhost/linux-next v5.6-rc2 next-20200219] [if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please see https://stackoverflow.com/a/37406982]

url:    https://github.com/0day-ci/linux/commits/Jay-Zhou/KVM-x86-enable-dirty-log-gradually-in-small-chunks/20200220-052018
base:   https://git.kernel.org/pub/scm/virt/kvm/kvm.git linux-next
config: s390-debug_defconfig (attached as .config)
compiler: s390-linux-gcc (GCC) 7.5.0

reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
GCC_VERSION=7.5.0 make.cross ARCH=s390

If you fix the issue, kindly add following tag

All warnings (new ones prefixed by >>):

arch/s390/kvm/../../../virt/kvm/kvm_main.c: In function 'kvm_create_dirty_bitmap':

>> arch/s390/kvm/../../../virt/kvm/kvm_main.c:874:5: warning: "CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT" is not defined, evaluates to 0 [-Wundef]
    #if CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT
        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

vim +/CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT +874 arch/s390/kvm/../../../virt/kvm/kvm_main.c

860
861 /*
862 * Allocation size is twice as large as the actual dirty bitmap size.
863 * See x86's kvm_vm_ioctl_get_dirty_log() why this is needed.
864 */
865 static int kvm_create_dirty_bitmap(struct kvm *kvm,
866 struct kvm_memory_slot *memslot)
867 {
868 unsigned long dirty_bytes = 2 * kvm_dirty_bitmap_bytes(memslot);
869
870 memslot->dirty_bitmap = kvzalloc(dirty_bytes, GFP_KERNEL_ACCOUNT);
871 if (!memslot->dirty_bitmap)
872 return -ENOMEM;
873

 > 874	#if CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT

875 if (kvm->manual_dirty_log_protect)
876 kvm_set_first_dirty_bitmap(memslot);
877 #endif
878
879 return 0;
880 }
881

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org