Skip to content

Commit 3c9bd40

Browse files
zhoujianjay6bonzini
authored andcommitted
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. Initialize all the bits of the 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 Signed-off-by: Jay Zhou <jianjay.zhou@huawei.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent 0be4435 commit 3c9bd40

8 files changed

Lines changed: 73 additions & 20 deletions

File tree

‎Documentation/virt/kvm/api.rst‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5707,8 +5707,13 @@ and injected exceptions.
57075707
:Architectures: x86, arm, arm64, mips
57085708
:Parameters: args[0] whether feature should be enabled or not
57095709

5710-
With this capability enabled, KVM_GET_DIRTY_LOG will not automatically
5711-
clear and write-protect all pages that are returned as dirty.
5710+
Valid flags are::
5711+
5712+
#define KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE (1 << 0)
5713+
#define KVM_DIRTY_LOG_INITIALLY_SET (1 << 1)
5714+
5715+
With KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE is set, KVM_GET_DIRTY_LOG will not
5716+
automatically clear and write-protect all pages that are returned as dirty.
57125717
Rather, userspace will have to do this operation separately using
57135718
KVM_CLEAR_DIRTY_LOG.
57145719

@@ -5719,12 +5724,19 @@ than requiring to sync a full memslot; this ensures that KVM does not
57195724
take spinlocks for an extended period of time. Second, in some cases a
57205725
large amount of time can pass between a call to KVM_GET_DIRTY_LOG and
57215726
userspace actually using the data in the page. Pages can be modified
5722-
during this time, which is inefficint for both the guest and userspace:
5727+
during this time, which is inefficient for both the guest and userspace:
57235728
the guest will incur a higher penalty due to write protection faults,
57245729
while userspace can see false reports of dirty pages. Manual reprotection
57255730
helps reducing this time, improving guest performance and reducing the
57265731
number of dirty log false positives.
57275732

5733+
With KVM_DIRTY_LOG_INITIALLY_SET set, all the bits of the dirty bitmap
5734+
will be initialized to 1 when created. This also improves performance because
5735+
dirty logging can be enabled gradually in small chunks on the first call
5736+
to KVM_CLEAR_DIRTY_LOG. KVM_DIRTY_LOG_INITIALLY_SET depends on
5737+
KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE (it is also only available on
5738+
x86 for now).
5739+
57285740
KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2 was previously available under the name
57295741
KVM_CAP_MANUAL_DIRTY_LOG_PROTECT, but the implementation had bugs that make
57305742
it hard or impossible to use it correctly. The availability of

‎arch/x86/include/asm/kvm_host.h‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@
4949

5050
#define KVM_IRQCHIP_NUM_PINS KVM_IOAPIC_NUM_PINS
5151

52+
#define KVM_DIRTY_LOG_MANUAL_CAPS (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE | \
53+
KVM_DIRTY_LOG_INITIALLY_SET)
54+
5255
/* x86-specific vcpu->requests bit members */
5356
#define KVM_REQ_MIGRATE_TIMER KVM_ARCH_REQ(0)
5457
#define KVM_REQ_REPORT_TPR_ACCESS KVM_ARCH_REQ(1)
@@ -1306,7 +1309,8 @@ void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask,
13061309

13071310
void kvm_mmu_reset_context(struct kvm_vcpu *vcpu);
13081311
void kvm_mmu_slot_remove_write_access(struct kvm *kvm,
1309-
struct kvm_memory_slot *memslot);
1312+
struct kvm_memory_slot *memslot,
1313+
int start_level);
13101314
void kvm_mmu_zap_collapsible_sptes(struct kvm *kvm,
13111315
const struct kvm_memory_slot *memslot);
13121316
void kvm_mmu_slot_leaf_clear_dirty(struct kvm *kvm,

‎arch/x86/kvm/mmu/mmu.c‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5864,13 +5864,14 @@ static bool slot_rmap_write_protect(struct kvm *kvm,
58645864
}
58655865

58665866
void kvm_mmu_slot_remove_write_access(struct kvm *kvm,
5867-
struct kvm_memory_slot *memslot)
5867+
struct kvm_memory_slot *memslot,
5868+
int start_level)
58685869
{
58695870
bool flush;
58705871

58715872
spin_lock(&kvm->mmu_lock);
5872-
flush = slot_handle_all_level(kvm, memslot, slot_rmap_write_protect,
5873-
false);
5873+
flush = slot_handle_level(kvm, memslot, slot_rmap_write_protect,
5874+
start_level, PT_MAX_HUGEPAGE_LEVEL, false);
58745875
spin_unlock(&kvm->mmu_lock);
58755876

58765877
/*

‎arch/x86/kvm/vmx/vmx.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7280,7 +7280,8 @@ static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
72807280
static void vmx_slot_enable_log_dirty(struct kvm *kvm,
72817281
struct kvm_memory_slot *slot)
72827282
{
7283-
kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
7283+
if (!kvm_dirty_log_manual_protect_and_init_set(kvm))
7284+
kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
72847285
kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
72857286
}
72867287

‎arch/x86/kvm/x86.c‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9916,7 +9916,7 @@ static void kvm_mmu_slot_apply_flags(struct kvm *kvm,
99169916
{
99179917
/* Still write protect RO slot */
99189918
if (new->flags & KVM_MEM_READONLY) {
9919-
kvm_mmu_slot_remove_write_access(kvm, new);
9919+
kvm_mmu_slot_remove_write_access(kvm, new, PT_PAGE_TABLE_LEVEL);
99209920
return;
99219921
}
99229922

@@ -9951,10 +9951,23 @@ static void kvm_mmu_slot_apply_flags(struct kvm *kvm,
99519951
* See the comments in fast_page_fault().
99529952
*/
99539953
if (new->flags & KVM_MEM_LOG_DIRTY_PAGES) {
9954-
if (kvm_x86_ops->slot_enable_log_dirty)
9954+
if (kvm_x86_ops->slot_enable_log_dirty) {
99559955
kvm_x86_ops->slot_enable_log_dirty(kvm, new);
9956-
else
9957-
kvm_mmu_slot_remove_write_access(kvm, new);
9956+
} else {
9957+
int level =
9958+
kvm_dirty_log_manual_protect_and_init_set(kvm) ?
9959+
PT_DIRECTORY_LEVEL : PT_PAGE_TABLE_LEVEL;
9960+
9961+
/*
9962+
* If we're with initial-all-set, we don't need
9963+
* to write protect any small page because
9964+
* they're reported as dirty already. However
9965+
* we still need to write-protect huge pages
9966+
* so that the page split can happen lazily on
9967+
* the first write to the huge page.
9968+
*/
9969+
kvm_mmu_slot_remove_write_access(kvm, new, level);
9970+
}
99589971
} else {
99599972
if (kvm_x86_ops->slot_disable_log_dirty)
99609973
kvm_x86_ops->slot_disable_log_dirty(kvm, new);

‎include/linux/kvm_host.h‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,10 @@ static inline unsigned long *kvm_second_dirty_bitmap(struct kvm_memory_slot *mem
360360
return memslot->dirty_bitmap + len / sizeof(*memslot->dirty_bitmap);
361361
}
362362

363+
#ifndef KVM_DIRTY_LOG_MANUAL_CAPS
364+
#define KVM_DIRTY_LOG_MANUAL_CAPS KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE
365+
#endif
366+
363367
struct kvm_s390_adapter_int {
364368
u64 ind_addr;
365369
u64 summary_addr;
@@ -493,7 +497,7 @@ struct kvm {
493497
#endif
494498
long tlbs_dirty;
495499
struct list_head devices;
496-
bool manual_dirty_log_protect;
500+
u64 manual_dirty_log_protect;
497501
struct dentry *debugfs_dentry;
498502
struct kvm_stat_data **debugfs_stat_data;
499503
struct srcu_struct srcu;
@@ -527,6 +531,11 @@ struct kvm {
527531
#define vcpu_err(vcpu, fmt, ...) \
528532
kvm_err("vcpu%i " fmt, (vcpu)->vcpu_id, ## __VA_ARGS__)
529533

534+
static inline bool kvm_dirty_log_manual_protect_and_init_set(struct kvm *kvm)
535+
{
536+
return !!(kvm->manual_dirty_log_protect & KVM_DIRTY_LOG_INITIALLY_SET);
537+
}
538+
530539
static inline struct kvm_io_bus *kvm_get_bus(struct kvm *kvm, enum kvm_bus idx)
531540
{
532541
return srcu_dereference_check(kvm->buses[idx], &kvm->srcu,

‎include/uapi/linux/kvm.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1628,4 +1628,7 @@ struct kvm_hyperv_eventfd {
16281628
#define KVM_HYPERV_CONN_ID_MASK 0x00ffffff
16291629
#define KVM_HYPERV_EVENTFD_DEASSIGN (1 << 0)
16301630

1631+
#define KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE (1 << 0)
1632+
#define KVM_DIRTY_LOG_INITIALLY_SET (1 << 1)
1633+
16311634
#endif /* __LINUX_KVM_H */

‎virt/kvm/kvm_main.c‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -858,7 +858,7 @@ static int kvm_vm_release(struct inode *inode, struct file *filp)
858858
* Allocation size is twice as large as the actual dirty bitmap size.
859859
* See kvm_vm_ioctl_get_dirty_log() why this is needed.
860860
*/
861-
static int kvm_create_dirty_bitmap(struct kvm_memory_slot *memslot)
861+
static int kvm_alloc_dirty_bitmap(struct kvm_memory_slot *memslot)
862862
{
863863
unsigned long dirty_bytes = 2 * kvm_dirty_bitmap_bytes(memslot);
864864

@@ -1288,9 +1288,12 @@ int __kvm_set_memory_region(struct kvm *kvm,
12881288
if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
12891289
new.dirty_bitmap = NULL;
12901290
else if (!new.dirty_bitmap) {
1291-
r = kvm_create_dirty_bitmap(&new);
1291+
r = kvm_alloc_dirty_bitmap(&new);
12921292
if (r)
12931293
return r;
1294+
1295+
if (kvm_dirty_log_manual_protect_and_init_set(kvm))
1296+
bitmap_set(new.dirty_bitmap, 0, new.npages);
12941297
}
12951298

12961299
r = kvm_set_memslot(kvm, mem, &old, &new, as_id, change);
@@ -3529,16 +3532,17 @@ static long kvm_vm_ioctl_check_extension_generic(struct kvm *kvm, long arg)
35293532
case KVM_CAP_IOEVENTFD_ANY_LENGTH:
35303533
case KVM_CAP_CHECK_EXTENSION_VM:
35313534
case KVM_CAP_ENABLE_CAP_VM:
3532-
#ifdef CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT
3533-
case KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2:
3534-
#endif
35353535
return 1;
35363536
#ifdef CONFIG_KVM_MMIO
35373537
case KVM_CAP_COALESCED_MMIO:
35383538
return KVM_COALESCED_MMIO_PAGE_OFFSET;
35393539
case KVM_CAP_COALESCED_PIO:
35403540
return 1;
35413541
#endif
3542+
#ifdef CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT
3543+
case KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2:
3544+
return KVM_DIRTY_LOG_MANUAL_CAPS;
3545+
#endif
35423546
#ifdef CONFIG_HAVE_KVM_IRQ_ROUTING
35433547
case KVM_CAP_IRQ_ROUTING:
35443548
return KVM_MAX_IRQ_ROUTES;
@@ -3566,11 +3570,17 @@ static int kvm_vm_ioctl_enable_cap_generic(struct kvm *kvm,
35663570
{
35673571
switch (cap->cap) {
35683572
#ifdef CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT
3569-
case KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2:
3570-
if (cap->flags || (cap->args[0] & ~1))
3573+
case KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2: {
3574+
u64 allowed_options = KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE;
3575+
3576+
if (cap->args[0] & KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE)
3577+
allowed_options = KVM_DIRTY_LOG_MANUAL_CAPS;
3578+
3579+
if (cap->flags || (cap->args[0] & ~allowed_options))
35713580
return -EINVAL;
35723581
kvm->manual_dirty_log_protect = cap->args[0];
35733582
return 0;
3583+
}
35743584
#endif
35753585
default:
35763586
return kvm_vm_ioctl_enable_cap(kvm, cap);

0 commit comments

Comments
 (0)