KVM: Dirty ring interface
Subsystems (6)
v8 cover KVM: Dirty ring interface
KVM branch:
https://github.com/xzpeter/linux/tree/kvm-dirty-ring
QEMU branch for testing:
https://github.com/xzpeter/qemu/tree/kvm-dirty-ring
v8:
- rebase to kvm/next
- fix test bisection issues [Drew]
- reword comment for __x86_set_memory_region [Sean]
- document fixup on "mutual exclusive", etc. [Sean]
For previous versions, please refer to:
V1: https://lore.kernel.org/kvm/[email protected]
V2: https://lore.kernel.org/kvm/[email protected]
V3: https://lore.kernel.org/kvm/[email protected]
V4: https://lore.kernel.org/kvm/[email protected]
V5: https://lore.kernel.org/kvm/[email protected]
V6: https://lore.kernel.org/kvm/[email protected]
V7: https://lore.kernel.org/kvm/[email protected]
Overview
============
This is a continued work from Lei Cao <[email protected]> and Paolo Bonzini on the KVM dirty ring interface.
The new dirty ring interface is another way to collect dirty pages for the virtual machines. It is different from the existing dirty logging interface in a few ways, majorly:
- Data format: The dirty data was in a ring format rather than a bitmap format, so dirty bits to sync for dirty logging does not depend on the size of guest memory any more, but speed of dirtying. Also, the dirty ring is per-vcpu, while the dirty bitmap is per-vm.
- Data copy: The sync of dirty pages does not need data copy any more, but instead the ring is shared between the userspace and kernel by page sharings (mmap() on vcpu fd)
- Interface: Instead of using the old KVM_GET_DIRTY_LOG, KVM_CLEAR_DIRTY_LOG interfaces, the new ring uses the new KVM_RESET_DIRTY_RINGS ioctl when we want to reset the collected dirty pages to protected mode again (works like KVM_CLEAR_DIRTY_LOG, but ring based). To collecting dirty bits, we only need to read the ring data, no ioctl is needed.
Ring Layout
===========
KVM dirty ring is per-vcpu. Each ring is an array of kvm_dirty_gfn defined as:
struct kvm_dirty_gfn {
__u32 flags;
__u32 slot; /* as_id | slot_id */
__u64 offset;
};Each GFN is a state machine itself. The state is embeded in the flags field, as defined in the uapi header:
/*
* KVM dirty GFN flags, defined as:
*
* |---------------+---------------+--------------| * | bit 1 (reset) | bit 0 (dirty) | Status | * |---------------+---------------+--------------| * | 0 | 0 | Invalid GFN | * | 0 | 1 | Dirty GFN | * | 1 | X | GFN to reset | * |---------------+---------------+--------------| * * Lifecycle of a dirty GFN goes like: * * dirtied collected reset * 00 -----------> 01 -------------> 1X -------+ * ^ | * | | * +------------------------------------------+
*
* The userspace program is only responsible for the 01->1X state
* conversion (to collect dirty bits). Also, it must not skip any
* dirty bits so that dirty bits are always collected in sequence. */
Testing
=======
This series provided both the implementation of the KVM dirty ring and the test case. Also I've implemented the QEMU counterpart that can run with the new KVM, link can be found at the top of the cover letter. However that's still a very initial version which is prone to change and future optimizations.
I did some measurement with the new method with 24G guest running some dirty workload, I don't see any speedup so far, even in some heavy dirty load it'll be slower (e.g., when 800MB/s random dirty rate, kvm dirty ring takes average of ~73s to complete migration while dirty logging only needs average of ~55s). However that's understandable because 24G guest means only 1M dirty bitmap, that's still a suitable case for dirty logging. Meanwhile heavier workload means worst case for dirty ring.
More tests are welcomed if there's bigger host/guest, especially on COLO-like workload.
Please review, thanks.
Peter Xu (14):
KVM: X86: Change parameter for fast_page_fault tracepoint
KVM: Cache as_id in kvm_memory_slot
KVM: X86: Don't track dirty for KVM_SET_[TSS_ADDR|IDENTITY_MAP_ADDR]
KVM: Pass in kvm pointer into mark_page_dirty_in_slot()
KVM: X86: Implement ring-based dirty memory tracking
KVM: Make dirty ring exclusive to dirty bitmap log
KVM: Don't allocate dirty bitmap if dirty ring is enabled
KVM: selftests: Always clear dirty bitmap after iteration
KVM: selftests: Sync uapi/linux/kvm.h to tools/
KVM: selftests: Use a single binary for dirty/clear log test
KVM: selftests: Introduce after_vcpu_run hook for dirty log test
KVM: selftests: Add dirty ring buffer test
KVM: selftests: Let dirty_log_test async for dirty ring test
KVM: selftests: Add "-c" parameter to dirty log test
Documentation/virt/kvm/api.rst | 123 +++++ arch/x86/include/asm/kvm_host.h | 6 +- arch/x86/include/uapi/asm/kvm.h | 1 + arch/x86/kvm/Makefile | 3 +- arch/x86/kvm/mmu/mmu.c | 10 +- arch/x86/kvm/mmutrace.h | 9 +- arch/x86/kvm/svm.c | 9 +- arch/x86/kvm/vmx/vmx.c | 89 +-- arch/x86/kvm/x86.c | 48 +- include/linux/kvm_dirty_ring.h | 103 ++++ include/linux/kvm_host.h | 19 + include/trace/events/kvm.h | 78 +++ include/uapi/linux/kvm.h | 53 ++ tools/include/uapi/linux/kvm.h | 100 +++- tools/testing/selftests/kvm/Makefile | 2 - .../selftests/kvm/clear_dirty_log_test.c | 6 - tools/testing/selftests/kvm/dirty_log_test.c | 505 ++++++++++++++++-- .../testing/selftests/kvm/include/kvm_util.h | 4 + tools/testing/selftests/kvm/lib/kvm_util.c | 68 +++ .../selftests/kvm/lib/kvm_util_internal.h | 4 + virt/kvm/dirty_ring.c | 195 +++++++ virt/kvm/kvm_main.c | 162 +++++- 22 files changed, 1459 insertions(+), 138 deletions(-) create mode 100644 include/linux/kvm_dirty_ring.h delete mode 100644 tools/testing/selftests/kvm/clear_dirty_log_test.c create mode 100644 virt/kvm/dirty_ring.c
Signature · 1 line
2.24.1
PATCH v8 1/14 KVM: X86: Change parameter for fast_page_fault tracepoint
It would be clearer to dump the return value to know easily on whether did we go through the fast path for handling current page fault. Remove the old two last parameters because after all the old/new sptes were dumped in the same line.
--- arch/x86/kvm/mmutrace.h | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-)
arch/x86/kvm/mmutrace.h +2 -7
@@ -244,9 +244,6 @@ TRACE_EVENT( __entry->access) ); -#define __spte_satisfied (__spte) \ - (__entry->retry && is_writable_pte(__entry->__spte)) - TRACE_EVENT( fast_page_fault, TP_PROTO(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, u32 error_code,
@@ -274,12 +271,10 @@ TRACE_EVENT( ),TP_printk ("vcpu %d gva %llx error_code %s sptep %p old %#llx" -" new %llx spurious %d fixed %d" , __entry->vcpu_id, +" new %llx ret %d" , __entry->vcpu_id, __entry->cr2_or_gpa,__print_flags (__entry->error_code,"|" , kvm_mmu_trace_pferr_flags), __entry->sptep, - __entry->old_spte, __entry->new_spte, -__spte_satisfied (old_spte),__spte_satisfied (new_spte) - ) + __entry->old_spte, __entry->new_spte, __entry->retry) ); TRACE_EVENT(
Signature · 1 line
2.24.1
PATCH v8 2/14 KVM: Cache as_id in kvm_memory_slot
In reply to Peter XuCache the address space ID just like the slot ID. It will be used in order to fill in the dirty ring entries.
--- include/linux/kvm_host.h | 1 + virt/kvm/kvm_main.c | 1 + 2 files changed, 2 insertions(+)
include/linux/kvm_host.h +1 -0
@@ -346,6 +346,7 @@ struct kvm_memory_slot {unsigned long userspace_addr;u32 flags;short id; +u16 as_id; };static inline unsigned long kvm_dirty_bitmap_bytes (struct kvm_memory_slot *memslot)
virt/kvm/kvm_main.c +1 -0
@@ -1243,6 +1243,7 @@ int __kvm_set_memory_region(struct kvm *kvm,if (!mem->memory_size)return kvm_delete_memslot (kvm, mem, &old, as_id); + new.as_id = as_id; new.id = id; new.base_gfn = mem->guest_phys_addr >>PAGE_SHIFT ; new.npages = mem->memory_size >>PAGE_SHIFT ;
Signature · 1 line
2.24.1
PATCH v8 3/14 KVM: X86: Don't track dirty for KVM_SET_[TSS_ADDR|IDENTITY_MAP_ADDR]
In reply to Peter XuOriginally, we have three code paths that can dirty a page without vcpu context for X86:
- init_rmode_identity_map
- init_rmode_tss
- kvmgt_rw_gpa
init_rmode_identity_map and init_rmode_tss will be setup on destination VM no matter what (and the guest cannot even see them), so it does not make sense to track them at all.
To do this, allow __x86_set_memory_region() to return the userspace address that just allocated to the caller. Then in both of the functions we directly write to the userspace address instead of calling kvm_write_*() APIs.
Another trivial change is that we don't need to explicitly clear the identity page table root in init_rmode_identity_map() because no matter what we'll write to the whole page with 4M huge page entries.
--- arch/x86/include/asm/kvm_host.h | 3 +- arch/x86/kvm/svm.c | 9 ++-- arch/x86/kvm/vmx/vmx.c | 82 ++++++++++++++++----------------- arch/x86/kvm/x86.c | 39 +++++++++++++--- 4 files changed, 81 insertions(+), 52 deletions(-)
arch/x86/include/asm/kvm_host.h +2 -1
@@ -1645,7 +1645,8 @@ void __kvm_request_immediate_exit(struct kvm_vcpu *vcpu);int kvm_is_in_guest (void ); -int __x86_set_memory_region (struct kvm *kvm,int id,gpa_t gpa,u32 size); +void __user *__x86_set_memory_region (struct kvm *kvm,int id,gpa_t gpa, +u32 size);bool kvm_vcpu_is_reset_bsp (struct kvm_vcpu *vcpu);bool kvm_vcpu_is_bsp (struct kvm_vcpu *vcpu);
arch/x86/kvm/svm.c +6 -3
@@ -1785,7 +1785,8 @@ static u64 *avic_get_physical_id_entry(struct kvm_vcpu *vcpu,*/ static int avic_update_access_page (struct kvm *kvm,bool activate) { -int ret =0 ; +void __user *ret; +int r =0 ;mutex_lock (&kvm->slots_lock);/*
@@ -1801,13 +1802,15 @@ static int avic_update_access_page(struct kvm *kvm, bool activate)APIC_ACCESS_PAGE_PRIVATE_MEMSLOT ,APIC_DEFAULT_PHYS_BASE , activate ? PAGE_SIZE : 0); -if (ret) +if (IS_ERR (ret)) { + r =PTR_ERR (ret); goto out; + } kvm->arch.apic_access_page_done = activate; out:mutex_unlock (&kvm->slots_lock); -return ret; +return r; }static int avic_init_backing_page (struct kvm_vcpu *vcpu)
arch/x86/kvm/vmx/vmx.c +41 -41
146 lines not on this page · raw message
arch/x86/kvm/x86.c +32 -7
@@ -9791,7 +9791,32 @@ void kvm_arch_sync_events(struct kvm *kvm)kvm_free_pit (kvm); } -int __x86_set_memory_region (struct kvm *kvm,int id,gpa_t gpa,u32 size) +#define ERR_PTR_USR (e) ((void __user *)ERR_PTR(e)) + +/** +* __x86_set_memory_region: Setup KVM internal memory slot +* +* @kvm: the kvm pointer to the VM. +* @id: the slot ID to setup. +* @gpa: the GPA to install the slot (unused when @size == 0). +* @size: the size of the slot. Set to zero to uninstall a slot. +* +* This function helps to setup a KVM internal memory slot. Specify +* @size > 0 to install a new slot, while @size == 0 to uninstall a +* slot. The return code can be one of the following: +* +* HVA: on success (uninstall will return a bogus HVA) +* -errno: on error +* +* The caller should always use IS_ERR() to check the return value +* before use. Note, the KVM internal memory slots are guaranteed to +* remain valid and unchanged until the VM is destroyed, i.e., the +* GPA->HVA translation will not change. However, the HVA is a user +* address, i.e. its accessibility is not guaranteed, and must be +* accessed via __copy_{to,from}_user(). +*/ +void __user *__x86_set_memory_region (struct kvm *kvm,int id,gpa_t gpa, +u32 size) {int i, r;unsigned long hva,uninitialized_var (old_npages );
@@ -9800,12 +9825,12 @@ int __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, u32 size)/* Called with kvm->slots_lock held. */ if (WARN_ON (id >=KVM_MEM_SLOTS_NUM )) -return -EINVAL ; +return ERR_PTR_USR (-EINVAL ); slot =id_to_memslot (slots, id);if (size) {if (slot && slot->npages) -return -EEXIST ; +return ERR_PTR_USR (-EEXIST );/* * MAP_SHARED to prevent internal slot pages from being moved
@@ -9814,10 +9839,10 @@ int __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, u32 size) hva =vm_mmap (NULL ,0 , size,PROT_READ |PROT_WRITE ,MAP_SHARED |MAP_ANONYMOUS ,0 );if (IS_ERR ((void *)hva)) -return PTR_ERR ((void *)hva); +return (void __user *)hva; }else {if (!slot || !slot->npages) -return 0 ; +return ERR_PTR_USR (0 );/* * Stuff a non-canonical value to catch use-after-delete. This
@@ -9838,13 +9863,13 @@ int __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, u32 size) m.memory_size = size; r =__kvm_set_memory_region (kvm, &m);if (r <0 ) -return r; +return ERR_PTR_USR (r); }if (!size)vm_munmap (hva, old_npages *PAGE_SIZE ); -return 0 ; +return (void __user *)hva; }EXPORT_SYMBOL_GPL (__x86_set_memory_region);
Signature · 1 line
2.24.1
Re: [PATCH v8 03/14] KVM: X86: Don't track dirty for KVM_SET_[TSS_ADDR|IDENTITY_MAP_ADDR]
arch/x86/kvm/x86.c void kvm_arch_sync_events(struct kvm *kvm) · line 9794
Quoted 10 lines · Peter Xu
arch/x86/kvm/x86.c +1
-1@@ -9791,7 +9791,32 @@ void kvm_arch_sync_events(struct kvm *kvm)kvm_free_pit (kvm); } -int __x86_set_memory_region (struct kvm *kvm,int id,gpa_t gpa,u32 size) +#define ERR_PTR_USR (e) ((void __user *)ERR_PTR(e))
Heh, my first thought when reading the below code was "cool, I didn't know there was ERR_PTR_USR!". This probably should be in include/linux/err.h, or maybe a new arch specific implementation if it's not universally safe.
An alternative, which looks enticing given that proper user variants will be a bit of an explosion, would be to do:
static void *____x86_set_memory_region(...)
{
<actual function>
}
void __user *__x86_set_memory_region(...)
{
return (void __user *)____x86_set_memory_region(...);}
A second alternative would be to return an "unsigned long", i.e. force the one function that actually accesses the hva to do the cast. I think I like this option the best as it would minimize the churn in __x86_set_memory_region(). Callers can use IS_ERR_VALUE() to detect failure.
arch/x86/kvm/x86.c void kvm_arch_sync_events(struct kvm *kvm) · line 9808
Quoted 13 lines
arch/x86/kvm/x86.c +13
-0+/** +* __x86_set_memory_region: Setup KVM internal memory slot +* +* @kvm: the kvm pointer to the VM. +* @id: the slot ID to setup. +* @gpa: the GPA to install the slot (unused when @size == 0). +* @size: the size of the slot. Set to zero to uninstall a slot. +* +* This function helps to setup a KVM internal memory slot. Specify +* @size > 0 to install a new slot, while @size == 0 to uninstall a +* slot. The return code can be one of the following: +* +* HVA: on success (uninstall will return a bogus HVA)
I think it's important to call out that it returns '0' on uninstall, e.g. otherwise it's not clear how a caller can detect failure.
Quoted 30 lines
arch/x86/kvm/x86.c +13
-2+* -errno: on error +* +* The caller should always use IS_ERR() to check the return value +* before use. Note, the KVM internal memory slots are guaranteed to +* remain valid and unchanged until the VM is destroyed, i.e., the +* GPA->HVA translation will not change. However, the HVA is a user +* address, i.e. its accessibility is not guaranteed, and must be +* accessed via __copy_{to,from}_user(). +*/ +void __user *__x86_set_memory_region (struct kvm *kvm,int id,gpa_t gpa, +u32 size) {int i, r;unsigned long hva,uninitialized_var (old_npages );@@ -9800,12 +9825,12 @@ int __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, u32 size)/* Called with kvm->slots_lock held. */ if (WARN_ON (id >=KVM_MEM_SLOTS_NUM )) -return -EINVAL ; +return ERR_PTR_USR (-EINVAL ); slot =id_to_memslot (slots, id);if (size) {if (slot && slot->npages) -return -EEXIST ; +return ERR_PTR_USR (-EEXIST );/* * MAP_SHARED to prevent internal slot pages from being moved
arch/x86/kvm/x86.c int __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, u32 size) · line 9841
Quoted 3 lines
arch/x86/kvm/x86.c +0
-0@@ -9814,10 +9839,10 @@ int __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, u32 size) hva =vm_mmap (NULL ,0 , size,PROT_READ |PROT_WRITE ,MAP_SHARED |MAP_ANONYMOUS ,0 );if (IS_ERR ((void *)hva))
IS_ERR_VALUE() can be used to avoid the double cast.
arch/x86/kvm/x86.c int __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, u32 size) · line 9842
Quoted 2 lines
arch/x86/kvm/x86.c +1
-1-return PTR_ERR ((void *)hva); +return (void __user *)hva;
If we still want to go down the route of ERR_PTR_USR, then an ERR_CAST_USR seems in order.
arch/x86/kvm/x86.c int __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, u32 size) · line 9845
Quoted 4 lines
arch/x86/kvm/x86.c +1
-1}else {if (!slot || !slot->npages) -return 0 ; +return ERR_PTR_USR (0 );
"return ERR_PTR_USR(NULL)" or "return NULL" would be more intuitive. Moot point if the return is changed to "unsigned long".
Quoted 22 lines
arch/x86/kvm/x86.c +2
-2/* * Stuff a non-canonical value to catch use-after-delete. This @@ -9838,13 +9863,13 @@ int __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, u32 size) m.memory_size = size; r =__kvm_set_memory_region (kvm, &m);if (r <0 ) -return r; +return ERR_PTR_USR (r); }if (!size)vm_munmap (hva, old_npages *PAGE_SIZE ); -return 0 ; +return (void __user *)hva; }EXPORT_SYMBOL_GPL (__x86_set_memory_region);Signature · 1 line
2.24.1
Re: [PATCH v8 03/14] KVM: X86: Don't track dirty for KVM_SET_[TSS_ADDR|IDENTITY_MAP_ADDR]
Quoted 9 lines · Sean Christopherson
Quoted 8 lines · Peter Xu
arch/x86/kvm/x86.c +0
-0@@ -9791,7 +9791,32 @@ void kvm_arch_sync_events(struct kvm *kvm)kvm_free_pit (kvm); }
Quoted 6 lines
arch/x86/kvm/x86.c void kvm_arch_sync_events(struct kvm *kvm) · line 9794
Quoted 2 lines
arch/x86/kvm/x86.c +1
-1-int __x86_set_memory_region (struct kvm *kvm,int id,gpa_t gpa,u32 size) +#define ERR_PTR_USR (e) ((void __user *)ERR_PTR(e))Heh, my first thought when reading the below code was "cool, I didn't know there was
ERR_PTR_USR!". This probably should be ininclude/linux/err.h, or maybe a new arch specific implementation if it's not universally safe.
Yeah, I just wanted to avoid introducing things in common headers before I'm sure it'll be used in the rest of the world.. We can always replace them with a global definition when it comes.
Quoted 10 lines
An alternative, which looks enticing given that proper user variants will be a bit of an explosion, would be to do:
static void
*____x86_set_memory_region(...)
{
<actual function>
}void
__user*__x86_set_memory_region(...)
Quoted 8 lines
{ return (void __user *)____x86_set_memory_region(...);}
A second alternative would be to return an "unsigned long", i.e. force the one function that actually accesses the hva to do the cast. I think I like this option the best as it would minimize the churn in
__x86_set_memory_region(). Callers can useIS_ERR_VALUE()to detect failure.
If you won't mind, I would prefer a 2nd opinion (maybe Paolo?) so we can consolidate the idea before I change them... (I would for sure still prefer the current approach for simplicity since after all I don't have strong opionion..)
Quoted 15 lines
Quoted 4 lines
arch/x86/kvm/x86.c +3
-0+/** +* __x86_set_memory_region: Setup KVM internal memory slot +* arch/x86/kvm/x86.c void kvm_arch_sync_events(struct kvm *kvm) · line 9808
Quoted 10 lines
arch/x86/kvm/x86.c +10
-0+* @kvm: the kvm pointer to the VM. +* @id: the slot ID to setup. +* @gpa: the GPA to install the slot (unused when @size == 0). +* @size: the size of the slot. Set to zero to uninstall a slot. +* +* This function helps to setup a KVM internal memory slot. Specify +* @size > 0 to install a new slot, while @size == 0 to uninstall a +* slot. The return code can be one of the following: +* +* HVA: on success (uninstall will return a bogus HVA)
Quoted 2 lines
I think it's important to call out that it returns '0' on uninstall, e.g. otherwise it's not clear how a caller can detect failure.
It will "return (0xdeadull << 48)" as you proposed in abbed4fa94f6? :-)
Frankly speaking I always preferred zero but that's just not true any more after above change. This also reminded me that maybe we should also return the same thing at [1] below.
Quoted 28 lines
Quoted 28 lines
arch/x86/kvm/x86.c +13
-2+* -errno: on error +* +* The caller should always use IS_ERR() to check the return value +* before use. Note, the KVM internal memory slots are guaranteed to +* remain valid and unchanged until the VM is destroyed, i.e., the +* GPA->HVA translation will not change. However, the HVA is a user +* address, i.e. its accessibility is not guaranteed, and must be +* accessed via __copy_{to,from}_user(). +*/ +void __user *__x86_set_memory_region (struct kvm *kvm,int id,gpa_t gpa, +u32 size) {int i, r;unsigned long hva,uninitialized_var (old_npages );@@ -9800,12 +9825,12 @@ int __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, u32 size)/* Called with kvm->slots_lock held. */ if (WARN_ON (id >=KVM_MEM_SLOTS_NUM )) -return -EINVAL ; +return ERR_PTR_USR (-EINVAL ); slot =id_to_memslot (slots, id);if (size) {if (slot && slot->npages) -return -EEXIST ; +return ERR_PTR_USR (-EEXIST );
Quoted 8 lines
arch/x86/kvm/x86.c int __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, u32 size) · line 9841
Quoted 6 lines
arch/x86/kvm/x86.c +0
-0/* * MAP_SHARED to prevent internal slot pages from being moved @@ -9814,10 +9839,10 @@ int __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, u32 size) hva =vm_mmap (NULL ,0 , size,PROT_READ |PROT_WRITE ,MAP_SHARED |MAP_ANONYMOUS ,0 );if (IS_ERR ((void *)hva))
IS_ERR_VALUE()can be used to avoid the double cast.
Agreed. But it's a context cleanup, so I normally will keep it as is (or use a standalone patch).
Quoted 6 lines
arch/x86/kvm/x86.c int __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, u32 size) · line 9842
Quoted 3 lines
arch/x86/kvm/x86.c +1
-1-return PTR_ERR ((void *)hva); +return (void __user *)hva;If we still want to go down the route of
ERR_PTR_USR, then anERR_CAST_USRseems in order.
Sure. But I'll still keep it kvm-only if you won't mind...
Quoted 5 lines
arch/x86/kvm/x86.c int __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, u32 size) · line 9845
Quoted 5 lines
arch/x86/kvm/x86.c +1
-1}else {if (!slot || !slot->npages) -return 0 ; +return ERR_PTR_USR (0 );
[1]
Quoted 3 lines
"return
ERR_PTR_USR(NULL)" or "return NULL" would be more intuitive. Moot point if the return is changed to "unsigned long".
ERR_PTR_USR() takes a "long". I can use ERR_CAST_USR(NULL) if you prefer me to explicitly use NULL.
Thanks,
Signature · 1 line
Peter Xu
Re: [PATCH v8 03/14] KVM: X86: Don't track dirty for KVM_SET_[TSS_ADDR|IDENTITY_MAP_ADDR]
Quoted 17 lines · Peter Xu
Quoted 9 lines · Sean Christopherson
Quoted 8 lines · Peter Xu
arch/x86/kvm/x86.c +0
-0@@ -9791,7 +9791,32 @@ void kvm_arch_sync_events(struct kvm *kvm)kvm_free_pit (kvm); }Quoted 6 lines
arch/x86/kvm/x86.c void kvm_arch_sync_events(struct kvm *kvm) · line 9794
Quoted 2 lines
arch/x86/kvm/x86.c +1
-1-int __x86_set_memory_region (struct kvm *kvm,int id,gpa_t gpa,u32 size) +#define ERR_PTR_USR (e) ((void __user *)ERR_PTR(e))Heh, my first thought when reading the below code was "cool, I didn't know there was
ERR_PTR_USR!". This probably should be ininclude/linux/err.h, or maybe a new arch specific implementation if it's not universally safe.
Quoted 3 lines
Yeah, I just wanted to avoid introducing things in common headers before I'm sure it'll be used in the rest of the world.. We can always replace them with a global definition when it comes.
Gotcha.
Quoted 13 lines
Quoted 13 lines
An alternative, which looks enticing given that proper user variants will be a bit of an explosion, would be to do:
static void
*____x86_set_memory_region(...)
{
<actual function>
}void
__user*__x86_set_memory_region(...){ return (void __user *)____x86_set_memory_region(...);}
Quoted 8 lines
Quoted 4 lines
A second alternative would be to return an "unsigned long", i.e. force the one function that actually accesses the hva to do the cast. I think I like this option the best as it would minimize the churn in
__x86_set_memory_region(). Callers can useIS_ERR_VALUE()to detect failure.If you won't mind, I would prefer a 2nd opinion (maybe Paolo?) so we can consolidate the idea before I change them... (I would for sure still prefer the current approach for simplicity since after all I don't have strong opionion..)
Definitely makes sense for Paolo to weigh in.
Quoted 14 lines
Quoted 14 lines
Quoted 13 lines
arch/x86/kvm/x86.c +13
-0+/** +* __x86_set_memory_region: Setup KVM internal memory slot +* +* @kvm: the kvm pointer to the VM. +* @id: the slot ID to setup. +* @gpa: the GPA to install the slot (unused when @size == 0). +* @size: the size of the slot. Set to zero to uninstall a slot. +* +* This function helps to setup a KVM internal memory slot. Specify +* @size > 0 to install a new slot, while @size == 0 to uninstall a +* slot. The return code can be one of the following: +* +* HVA: on success (uninstall will return a bogus HVA)
Quoted 8 lines
Quoted 2 lines
I think it's important to call out that it returns '0' on uninstall, e.g. otherwise it's not clear how a caller can detect failure.
It will "return (
0xdeadull<< 48)" as you proposed inabbed4fa94f6? :-)Frankly speaking I always preferred zero but that's just not true any more after above change. This also reminded me that maybe we should also return the same thing at [1] below.
Ah, I was looking at this code:
if (!slot || !slot->npages) return 0;
That means deletion returns different success values for "deletion was a nop" and "deletion was successful". The nop path should probably return (or fill in) "(unsigned long)(0xdeadull << 48)" as well.
Quoted 68 lines
Quoted 27 lines
Quoted 27 lines
arch/x86/kvm/x86.c +13
-2+* -errno: on error +* +* The caller should always use IS_ERR() to check the return value +* before use. Note, the KVM internal memory slots are guaranteed to +* remain valid and unchanged until the VM is destroyed, i.e., the +* GPA->HVA translation will not change. However, the HVA is a user +* address, i.e. its accessibility is not guaranteed, and must be +* accessed via __copy_{to,from}_user(). +*/ +void __user *__x86_set_memory_region (struct kvm *kvm,int id,gpa_t gpa, +u32 size) {int i, r;unsigned long hva,uninitialized_var (old_npages );@@ -9800,12 +9825,12 @@ int __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, u32 size)/* Called with kvm->slots_lock held. */ if (WARN_ON (id >=KVM_MEM_SLOTS_NUM )) -return -EINVAL ; +return ERR_PTR_USR (-EINVAL ); slot =id_to_memslot (slots, id);if (size) {if (slot && slot->npages) -return -EEXIST ; +return ERR_PTR_USR (-EEXIST );Quoted 8 lines
arch/x86/kvm/x86.c int __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, u32 size) · line 9841
Quoted 6 lines
arch/x86/kvm/x86.c +0
-0/* * MAP_SHARED to prevent internal slot pages from being moved @@ -9814,10 +9839,10 @@ int __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, u32 size) hva =vm_mmap (NULL ,0 , size,PROT_READ |PROT_WRITE ,MAP_SHARED |MAP_ANONYMOUS ,0 );if (IS_ERR ((void *)hva))
IS_ERR_VALUE()can be used to avoid the double cast.Agreed. But it's a context cleanup, so I normally will keep it as is (or use a standalone patch).
Quoted 6 lines
arch/x86/kvm/x86.c int __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, u32 size) · line 9842
Quoted 3 lines
arch/x86/kvm/x86.c +1
-1-return PTR_ERR ((void *)hva); +return (void __user *)hva;If we still want to go down the route of
ERR_PTR_USR, then anERR_CAST_USRseems in order.Sure. But I'll still keep it kvm-only if you won't mind...
Quoted 5 lines
arch/x86/kvm/x86.c int __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, u32 size) · line 9845
Quoted 5 lines
arch/x86/kvm/x86.c +1
-1}else {if (!slot || !slot->npages) -return 0 ; +return ERR_PTR_USR (0 );[1]
Quoted 3 lines
"return
ERR_PTR_USR(NULL)" or "return NULL" would be more intuitive. Moot point if the return is changed to "unsigned long".
ERR_PTR_USR()takes a "long". I can useERR_CAST_USR(NULL)if you prefer me to explicitly use NULL.Thanks,
Signature · 1 line
Peter Xu
Re: [PATCH v8 03/14] KVM: X86: Don't track dirty for KVM_SET_[TSS_ADDR|IDENTITY_MAP_ADDR]
On Mon, Apr 27, 2020 at 11:10:54AM -0700, Sean Christopherson wrote:
[...]
Quoted 6 lines
Quoted 5 lines
It will "return (
0xdeadull<< 48)" as you proposed inabbed4fa94f6? :-)Frankly speaking I always preferred zero but that's just not true any more after above change. This also reminded me that maybe we should also return the same thing at [1] below.
Quoted 8 lines
Ah, I was looking at this code:
if (!slot || !slot->npages) return 0;That means deletion returns different success values for "deletion was a nop" and "deletion was successful". The nop path should probably return (or fill in) "(unsigned long)(0xdeadull << 48)" as well.
Yep. Since I touched the line here after all, I'll directly squash this small fix into this patch too when I repost. Thanks,
[...]
Quoted 7 lines
Quoted 7 lines
Quoted 5 lines
arch/x86/kvm/x86.c int __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, u32 size) · line 9845
Quoted 5 lines
arch/x86/kvm/x86.c +1
-1}else {if (!slot || !slot->npages) -return 0 ; +return ERR_PTR_USR (0 );[1]
Signature · 1 line
Peter Xu
PATCH v8 4/14 KVM: Pass in kvm pointer into mark_page_dirty_in_slot()
In reply to Peter XuThe context will be needed to implement the kvm dirty ring.
--- virt/kvm/kvm_main.c | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-)
virt/kvm/kvm_main.c +19 -14
@@ -144,7 +144,9 @@ static void hardware_disable_all(void);static void kvm_io_bus_destroy (struct kvm_io_bus *bus); -static void mark_page_dirty_in_slot (struct kvm_memory_slot *memslot,gfn_t gfn); +static void mark_page_dirty_in_slot (struct kvm *kvm, +struct kvm_memory_slot *memslot, +gfn_t gfn); __visible bool kvm_rebooting;EXPORT_SYMBOL_GPL (kvm_rebooting);
@@ -2120,7 +2122,8 @@ int kvm_vcpu_map(struct kvm_vcpu *vcpu, gfn_t gfn, struct kvm_host_map *map) }EXPORT_SYMBOL_GPL (kvm_vcpu_map); -static void __kvm_unmap_gfn (struct kvm_memory_slot *memslot, +static void __kvm_unmap_gfn (struct kvm *kvm, +struct kvm_memory_slot *memslot,struct kvm_host_map *map,struct gfn_to_pfn_cache *cache,bool dirty,bool atomic)
@@ -2145,7 +2148,7 @@ static void __kvm_unmap_gfn(struct kvm_memory_slot *memslot,#endif if (dirty) -mark_page_dirty_in_slot (memslot, map->gfn); +mark_page_dirty_in_slot (kvm, memslot, map->gfn);if (cache) cache->dirty |= dirty;
@@ -2159,7 +2162,7 @@ static void __kvm_unmap_gfn(struct kvm_memory_slot *memslot,int kvm_unmap_gfn (struct kvm_vcpu *vcpu,struct kvm_host_map *map,struct gfn_to_pfn_cache *cache,bool dirty,bool atomic) { -__kvm_unmap_gfn (gfn_to_memslot (vcpu->kvm, map->gfn), map, +__kvm_unmap_gfn (vcpu->kvm,gfn_to_memslot (vcpu->kvm, map->gfn), map, cache, dirty, atomic);return 0 ; }
@@ -2167,8 +2170,8 @@ EXPORT_SYMBOL_GPL(kvm_unmap_gfn);void kvm_vcpu_unmap (struct kvm_vcpu *vcpu,struct kvm_host_map *map,bool dirty) { -__kvm_unmap_gfn (kvm_vcpu_gfn_to_memslot (vcpu, map->gfn), map,NULL , - dirty, false); +__kvm_unmap_gfn (vcpu->kvm,kvm_vcpu_gfn_to_memslot (vcpu, map->gfn), + map,NULL , dirty, false); }EXPORT_SYMBOL_GPL (kvm_vcpu_unmap);
@@ -2342,7 +2345,8 @@ int kvm_vcpu_read_guest_atomic(struct kvm_vcpu *vcpu, gpa_t gpa, }EXPORT_SYMBOL_GPL (kvm_vcpu_read_guest_atomic); -static int __kvm_write_guest_page (struct kvm_memory_slot *memslot,gfn_t gfn, +static int __kvm_write_guest_page (struct kvm *kvm, +struct kvm_memory_slot *memslot,gfn_t gfn,const void *data,int offset,int len) {int r;
@@ -2354,7 +2358,7 @@ static int __kvm_write_guest_page(struct kvm_memory_slot *memslot, gfn_t gfn, r =__copy_to_user ((void __user *)addr + offset, data, len);if (r)return -EFAULT ; -mark_page_dirty_in_slot (memslot, gfn); +mark_page_dirty_in_slot (kvm, memslot, gfn);return 0 ; }
@@ -2363,7 +2367,7 @@ int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, {struct kvm_memory_slot *slot =gfn_to_memslot (kvm, gfn); -return __kvm_write_guest_page (slot, gfn, data, offset, len); +return __kvm_write_guest_page (kvm, slot, gfn, data, offset, len); }EXPORT_SYMBOL_GPL (kvm_write_guest_page);
@@ -2372,7 +2376,7 @@ int kvm_vcpu_write_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn, {struct kvm_memory_slot *slot =kvm_vcpu_gfn_to_memslot (vcpu, gfn); -return __kvm_write_guest_page (slot, gfn, data, offset, len); +return __kvm_write_guest_page (vcpu->kvm, slot, gfn, data, offset, len); }EXPORT_SYMBOL_GPL (kvm_vcpu_write_guest_page);
@@ -2491,7 +2495,7 @@ int kvm_write_guest_offset_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc, r =__copy_to_user ((void __user *)ghc->hva + offset, data, len);if (r)return -EFAULT ; -mark_page_dirty_in_slot (ghc->memslot, gpa >>PAGE_SHIFT ); +mark_page_dirty_in_slot (kvm, ghc->memslot, gpa >>PAGE_SHIFT );return 0 ; }
@@ -2558,7 +2562,8 @@ int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len) }EXPORT_SYMBOL_GPL (kvm_clear_guest); -static void mark_page_dirty_in_slot (struct kvm_memory_slot *memslot, +static void mark_page_dirty_in_slot (struct kvm *kvm, +struct kvm_memory_slot *memslot,gfn_t gfn) {if (memslot && memslot->dirty_bitmap) {
@@ -2573,7 +2578,7 @@ void mark_page_dirty(struct kvm *kvm, gfn_t gfn)struct kvm_memory_slot *memslot; memslot =gfn_to_memslot (kvm, gfn); -mark_page_dirty_in_slot (memslot, gfn); +mark_page_dirty_in_slot (kvm, memslot, gfn); }EXPORT_SYMBOL_GPL (mark_page_dirty);
@@ -2582,7 +2587,7 @@ void kvm_vcpu_mark_page_dirty(struct kvm_vcpu *vcpu, gfn_t gfn)struct kvm_memory_slot *memslot; memslot =kvm_vcpu_gfn_to_memslot (vcpu, gfn); -mark_page_dirty_in_slot (memslot, gfn); +mark_page_dirty_in_slot (vcpu->kvm, memslot, gfn); }EXPORT_SYMBOL_GPL (kvm_vcpu_mark_page_dirty);
Signature · 1 line
2.24.1
PATCH v8 5/14 KVM: X86: Implement ring-based dirty memory tracking
In reply to Peter XuThis patch is heavily based on previous work from Lei Cao <[email protected]> and Paolo Bonzini <[email protected]>. [1]
KVM currently uses large bitmaps to track dirty memory. These bitmaps are copied to userspace when userspace queries KVM for its dirty page information. The use of bitmaps is mostly sufficient for live migration, as large parts of memory are be dirtied from one log-dirty pass to another. However, in a checkpointing system, the number of dirty pages is small and in fact it is often bounded---the VM is paused when it has dirtied a pre-defined number of pages. Traversing a large, sparsely populated bitmap to find set bits is time-consuming, as is copying the bitmap to user-space.
A similar issue will be there for live migration when the guest memory is huge while the page dirty procedure is trivial. In that case for each dirty sync we need to pull the whole dirty bitmap to userspace and analyse every bit even if it's mostly zeros.
The preferred data structure for above scenarios is a dense list of guest frame numbers (GFN). This patch series stores the dirty list in kernel memory that can be memory mapped into userspace to allow speedy harvesting.
This patch enables dirty ring for X86 only. However it should be easily extended to other archs as well.
[1] https://patchwork.kernel.org/patch/10471409/
--- Documentation/virt/kvm/api.rst | 116 +++++++++++++++++++ arch/x86/include/asm/kvm_host.h | 3 + arch/x86/include/uapi/asm/kvm.h | 1 + arch/x86/kvm/Makefile | 3 +- arch/x86/kvm/mmu/mmu.c | 6 + arch/x86/kvm/vmx/vmx.c | 7 ++ arch/x86/kvm/x86.c | 9 ++ include/linux/kvm_dirty_ring.h | 103 +++++++++++++++++ include/linux/kvm_host.h | 13 +++ include/trace/events/kvm.h | 78 +++++++++++++ include/uapi/linux/kvm.h | 53 +++++++++ virt/kvm/dirty_ring.c | 195 ++++++++++++++++++++++++++++++++ virt/kvm/kvm_main.c | 112 +++++++++++++++++- 13 files changed, 697 insertions(+), 2 deletions(-) create mode 100644 include/linux/kvm_dirty_ring.h create mode 100644 virt/kvm/dirty_ring.c
Documentation/virt/kvm/api.rst +116 -0
@@ -249,6 +249,7 @@ Based on their initialization different VMs may have different capabilities. It is thus encouraged to use the vm ioctl to query for capabilities (available with KVM_CAP_CHECK_EXTENSION_VM on the vm fd) + 4.5 KVM_GET_VCPU_MMAP_SIZE --------------------------
@@ -262,6 +263,18 @@ The KVM_RUN ioctl (cf.) communicates with userspace via a shared memory region. This ioctl returns the size of that region. See the KVM_RUN documentation for details. +Besides the size of the KVM_RUN communication region, other areas of +the VCPU file descriptor can be mmap-ed, including: + +- if KVM_CAP_COALESCED_MMIO is available, a page at + KVM_COALESCED_MMIO_PAGE_OFFSET * PAGE_SIZE; for historical reasons, + this page is included in the result of KVM_GET_VCPU_MMAP_SIZE. + KVM_CAP_COALESCED_MMIO is not documented yet. + +- if KVM_CAP_DIRTY_LOG_RING is available, a number of pages at + KVM_DIRTY_LOG_PAGE_OFFSET * PAGE_SIZE. For more information on + KVM_CAP_DIRTY_LOG_RING, see section 8.3. + 4.6 KVM_SET_MEMORY_REGION -------------------------
@@ -6109,3 +6122,106 @@ KVM can therefore start protected VMs. This capability governs the KVM_S390_PV_COMMAND ioctl and the KVM_MP_STATE_LOAD MP_STATE. KVM_SET_MP_STATE can fail for protected guests when the state change is invalid. + +8.24 KVM_CAP_DIRTY_LOG_RING + +Architectures: x86 +Parameters: args[0] - size of the dirty log ring + +KVM is capable of tracking dirty memory using ring buffers that are +mmaped into userspace; there is one dirty ring per vcpu. + +One dirty ring is defined as below internally: + +struct kvm_dirty_ring { + u32 dirty_index; + u32 reset_index; + u32 size; + u32 soft_limit; + struct kvm_dirty_gfn *dirty_gfns; + int index; +}; + +Dirty GFNs (Guest Frame Numbers) are stored in the dirty_gfns array. +For each of the dirty entry it's defined as: + +struct kvm_dirty_gfn { + __u32 flags; + __u32 slot; /* as_id | slot_id */ + __u64 offset; +}; + +Each GFN is a state machine itself. The state is embeded in the flags +field, as defined in the uapi header: + +/* + * KVM dirty GFN flags, defined as: + * + * |---------------+---------------+--------------| + * | bit 1 (reset) | bit 0 (dirty) | Status | + * |---------------+---------------+--------------| + * | 0 | 0 | Invalid GFN | + * | 0 | 1 | Dirty GFN | + * | 1 | X | GFN to reset | + * |---------------+---------------+--------------| + * + * Lifecycle of a dirty GFN goes like: + * + * dirtied collected reset + * 00 -----------> 01 -------------> 1X -------+ + * ^ | + * | | + * +------------------------------------------+ + * + * The userspace program is only responsible for the 01->1X state + * conversion (to collect dirty bits). Also, it must not skip any + * dirty bits so that dirty bits are always collected in sequence. + */ +#define KVM_DIRTY_GFN_F_DIRTY BIT(0) +#define KVM_DIRTY_GFN_F_RESET BIT(1) +#define KVM_DIRTY_GFN_F_MASK 0x3 + +Userspace calls KVM_ENABLE_CAP ioctl right after KVM_CREATE_VM ioctl +to enable this capability for the new guest and set the size of the +rings. It is only allowed before creating any vCPU, and the size of +the ring must be a power of two. The larger the ring buffer, the less +likely the ring is full and the VM is forced to exit to userspace. The +optimal size depends on the workload, but it is recommended that it be +at least 64 KiB (4096 entries). + +Just like for dirty page bitmaps, the buffer tracks writes to +all user memory regions for which the KVM_MEM_LOG_DIRTY_PAGES flag was +set in KVM_SET_USER_MEMORY_REGION. Once a memory region is registered +with the flag set, userspace can start harvesting dirty pages from the +ring buffer. + +To harvest the dirty pages, userspace accesses the mmaped ring buffer +to read the dirty GFNs starting from zero. If the flags has the DIRTY +bit set (at this stage the RESET bit must be cleared), then it means +this GFN is a dirty GFN. The userspace should collect this GFN and +mark the flags from state 01b to 1Xb (bit 0 will be ignored by KVM, +but bit 1 must be set to show that this GFN is collected and waiting +for a reset), and move on to the next GFN. The userspace should +continue to do this until when the flags of a GFN has the DIRTY bit +cleared, it means we've collected all the dirty GFNs we have for now. +It's not a must that the userspace collects the all dirty GFNs in +once. However it must collect the dirty GFNs in sequence, i.e., the +userspace program cannot skip one dirty GFN to collect the one next to +it. + +After processing one or more entries in the ring buffer, userspace +calls the VM ioctl KVM_RESET_DIRTY_RINGS to notify the kernel about +it, so that the kernel will reprotect those collected GFNs. +Therefore, the ioctl must be called *before* reading the content of +the dirty pages. + +The dirty ring interface has a major difference comparing to the +KVM_GET_DIRTY_LOG interface in that, when reading the dirty ring from +userspace it's still possible that the kernel has not yet flushed the +hardware dirty buffers into the kernel buffer (the flushing was +previously done by the KVM_GET_DIRTY_LOG ioctl). To achieve that, one +needs to kick the vcpu out for a hardware buffer flush (vmexit) to +make sure all the existing dirty gfns are flushed to the dirty rings. + +The dirty ring can gets full. When it happens, the KVM_RUN of the +vcpu will return with exit reason KVM_EXIT_DIRTY_LOG_FULL.
arch/x86/include/asm/kvm_host.h +3 -0
@@ -1201,6 +1201,7 @@ struct kvm_x86_ops {struct kvm_memory_slot *slot, gfn_t offset, unsigned long mask);int (*write_log_dirty)(struct kvm_vcpu *vcpu); +int (*cpu_dirty_log_size)(void );/* pmu operations of sub-arch */ const struct kvm_pmu_ops *pmu_ops;
@@ -1693,4 +1694,6 @@ static inline int kvm_cpu_get_apicid(int mps_cpu)#define GET_SMSTATE (type, buf, offset) \ (*(type *)((buf) + (offset) - 0x7e00)) +int kvm_cpu_dirty_log_size (void ); +#endif /* _ASM_X86_KVM_HOST_H */
arch/x86/include/uapi/asm/kvm.h +1 -0
@@ -12,6 +12,7 @@#define KVM_PIO_PAGE_OFFSET 1#define KVM_COALESCED_MMIO_PAGE_OFFSET 2 +#define KVM_DIRTY_LOG_PAGE_OFFSET 64#define DE_VECTOR 0#define DB_VECTOR 1
arch/x86/kvm/Makefile +2 -1
@@ -6,7 +6,8 @@ ccflags-$(CONFIG_KVM_WERROR) += -Werror 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)/dirty_ring.o kvm-$(CONFIG_KVM_ASYNC_PF) += $(KVM)/async_pf.o kvm-y += x86.o emulate.o i8259.o irq.o lapic.o \
arch/x86/kvm/mmu/mmu.c +6 -0
@@ -1749,7 +1749,13 @@ int kvm_arch_write_log_dirty(struct kvm_vcpu *vcpu) {if (kvm_x86_ops->write_log_dirty)return kvm_x86_ops->write_log_dirty (vcpu); +return 0 ; +} +int kvm_cpu_dirty_log_size (void ) +{ +if (kvm_x86_ops->cpu_dirty_log_size) +return kvm_x86_ops->cpu_dirty_log_size ();return 0 ; }
arch/x86/kvm/vmx/vmx.c +7 -0
@@ -7761,6 +7761,7 @@ static __init int hardware_setup(void) kvm_x86_ops->slot_disable_log_dirty =NULL ; kvm_x86_ops->flush_log_dirty =NULL ; kvm_x86_ops->enable_log_dirty_pt_masked =NULL ; + kvm_x86_ops->cpu_dirty_log_size =NULL ; }if (!cpu_has_vmx_preemption_timer ())
@@ -7835,6 +7836,11 @@ static bool vmx_check_apicv_inhibit_reasons(ulong bit)return supported &BIT (bit); } +static int vmx_cpu_dirty_log_size (void ) +{ +return enable_pml ?PML_ENTITY_NUM :0 ; +} +static struct kvm_x86_ops vmx_x86_ops__ro_after_init = { .cpu_has_kvm_support = cpu_has_kvm_support, .disabled_by_bios = vmx_disabled_by_bios,
@@ -7945,6 +7951,7 @@ static struct kvm_x86_ops vmx_x86_ops __ro_after_init = { .flush_log_dirty = vmx_flush_log_dirty, .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked, .write_log_dirty = vmx_write_pml_buffer, + .cpu_dirty_log_size = vmx_cpu_dirty_log_size, .pre_block = vmx_pre_block, .post_block = vmx_post_block,
arch/x86/kvm/x86.c +9 -0
@@ -8176,6 +8176,15 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)bool req_immediate_exit = false; +/* Forbid vmenter if vcpu dirty ring is soft-full */ +if (unlikely (vcpu->kvm->dirty_ring_size && +kvm_dirty_ring_soft_full (&vcpu->dirty_ring))) { + vcpu->run->exit_reason =KVM_EXIT_DIRTY_RING_FULL ; +trace_kvm_dirty_ring_exit (vcpu); + r =0 ; + goto out; + } +if (kvm_request_pending (vcpu)) {if (kvm_check_request (KVM_REQ_GET_VMCS12_PAGES , vcpu)) {if (unlikely (!kvm_x86_ops->get_vmcs12_pages (vcpu))) {
include/linux/kvm_dirty_ring.h new file +103 -0
103 lines not on this page · raw message
include/linux/kvm_host.h +13 -0
@@ -34,6 +34,7 @@#include <linux/kvm_types.h> #include <asm/kvm_host.h> +#include <linux/kvm_dirty_ring.h> #ifndef KVM_MAX_VCPU_ID #define KVM_MAX_VCPU_ID KVM_MAX_VCPUS
@@ -319,6 +320,7 @@ struct kvm_vcpu {bool ready;struct kvm_vcpu_arch arch;struct dentry *debugfs_dentry; +struct kvm_dirty_ring dirty_ring; };static inline int kvm_vcpu_exiting_guest_mode (struct kvm_vcpu *vcpu)
@@ -504,6 +506,7 @@ struct kvm {struct srcu_struct srcu;struct srcu_struct irq_srcu;pid_t userspace_pid; +u32 dirty_ring_size; };#define kvm_err (fmt, ...) \
@@ -1422,4 +1425,14 @@ int kvm_vm_create_worker_thread(struct kvm *kvm, kvm_vm_thread_fn_t thread_fn, uintptr_t data, const char *name, struct task_struct **thread_ptr); +/* +* This defines how many reserved entries we want to keep before we +* kick the vcpu to the userspace to avoid dirty ring full. This +* value can be tuned to higher if e.g. PML is enabled on the host. +*/ +#define KVM_DIRTY_RING_RSVD_ENTRIES 64 + +/* Max number of entries allowed for each kvm dirty ring */ +#define KVM_DIRTY_RING_MAX_ENTRIES 65536 +#endif
include/trace/events/kvm.h +78 -0
84 lines not on this page · raw message
include/uapi/linux/kvm.h +53 -0
75 lines not on this page · raw message
virt/kvm/dirty_ring.c new file +195 -0
195 lines not on this page · raw message
virt/kvm/kvm_main.c +111 -1
185 lines not on this page · raw message
Signature · 1 line
2.24.1
PATCH v8 6/14 KVM: Make dirty ring exclusive to dirty bitmap log
In reply to Peter XuThere's no good reason to use both the dirty bitmap logging and the new dirty ring buffer to track dirty bits. We should be able to even support both of them at the same time, but it could complicate things which could actually help little. Let's simply make it the rule before we enable dirty ring on any arch, that we don't allow these two interfaces to be used together.
The big world switch would be KVM_CAP_DIRTY_LOG_RING capability enablement. That's where we'll switch from the default dirty logging way to the dirty ring way. As long as kvm->dirty_ring_size is setup correctly, we'll once and for all switch to the dirty ring buffer mode for the current virtual machine.
--- Documentation/virt/kvm/api.rst | 7 +++++++ virt/kvm/kvm_main.c | 12 ++++++++++++ 2 files changed, 19 insertions(+)
Documentation/virt/kvm/api.rst +7 -0
@@ -6225,3 +6225,10 @@ make sure all the existing dirty gfns are flushed to the dirty rings. The dirty ring can gets full. When it happens, the KVM_RUN of the vcpu will return with exit reason KVM_EXIT_DIRTY_LOG_FULL. + +NOTE: the capability KVM_CAP_DIRTY_LOG_RING and the corresponding +ioctl KVM_RESET_DIRTY_RINGS are mutual exclusive to the existing ioctl +KVM_GET_DIRTY_LOG. After enabling KVM_CAP_DIRTY_LOG_RING with an +acceptable dirty ring size, the virtual machine will switch to the +dirty ring tracking mode. Further ioctls to either KVM_GET_DIRTY_LOG +or KVM_CLEAR_DIRTY_LOG will fail.
virt/kvm/kvm_main.c +12 -0
@@ -1355,6 +1355,10 @@ int kvm_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log,unsigned long n;unsigned long any =0 ; +/* Dirty ring tracking is exclusive to dirty log tracking */ +if (kvm->dirty_ring_size) +return -EINVAL ; + *memslot =NULL ; *is_dirty =0 ;
@@ -1416,6 +1420,10 @@ static int kvm_get_dirty_log_protect(struct kvm *kvm, struct kvm_dirty_log *log)unsigned long *dirty_bitmap_buffer;bool flush; +/* Dirty ring tracking is exclusive to dirty log tracking */ +if (kvm->dirty_ring_size) +return -EINVAL ; + as_id = log->slot >>16 ; id = (u16 )log->slot;if (as_id >=KVM_ADDRESS_SPACE_NUM || id >=KVM_USER_MEM_SLOTS )
@@ -1524,6 +1532,10 @@ static int kvm_clear_dirty_log_protect(struct kvm *kvm,unsigned long *dirty_bitmap_buffer;bool flush; +/* Dirty ring tracking is exclusive to dirty log tracking */ +if (kvm->dirty_ring_size) +return -EINVAL ; + as_id = log->slot >>16 ; id = (u16 )log->slot;if (as_id >=KVM_ADDRESS_SPACE_NUM || id >=KVM_USER_MEM_SLOTS )
Signature · 1 line
2.24.1
PATCH v8 7/14 KVM: Don't allocate dirty bitmap if dirty ring is enabled
In reply to Peter XuBecause kvm dirty rings and kvm dirty log is used in an exclusive way, Let's avoid creating the dirty_bitmap when kvm dirty ring is enabled. At the meantime, since the dirty_bitmap will be conditionally created now, we can't use it as a sign of "whether this memory slot enabled dirty tracking". Change users like that to check against the kvm memory slot flags.
Note that there still can be chances where the kvm memory slot got its dirty_bitmap allocated, _if_ the memory slots are created before enabling of the dirty rings and at the same time with the dirty tracking capability enabled, they'll still with the dirty_bitmap. However it should not hurt much (e.g., the bitmaps will always be freed if they are there), and the real users normally won't trigger this because dirty bit tracking flag should in most cases only be applied to kvm slots only before migration starts, that should be far latter than kvm initializes (VM starts).
--- arch/x86/kvm/mmu/mmu.c | 4 ++-- include/linux/kvm_host.h | 5 +++++ virt/kvm/kvm_main.c | 4 ++-- 3 files changed, 9 insertions(+), 4 deletions(-)
arch/x86/kvm/mmu/mmu.c +2 -2
@@ -1276,8 +1276,8 @@ gfn_to_memslot_dirty_bitmap(struct kvm_vcpu *vcpu, gfn_t gfn, slot =kvm_vcpu_gfn_to_memslot (vcpu, gfn);if (!slot || slot->flags &KVM_MEMSLOT_INVALID )return NULL ; -if (no_dirty_log && slot->dirty_bitmap) -return NULL ; +if (no_dirty_log &&kvm_slot_dirty_track_enabled (slot)) +return false;return slot; }
include/linux/kvm_host.h +5 -0
@@ -351,6 +351,11 @@ struct kvm_memory_slot {u16 as_id; }; +static inline bool kvm_slot_dirty_track_enabled (struct kvm_memory_slot *slot) +{ +return slot->flags &KVM_MEM_LOG_DIRTY_PAGES ; +} +static inline unsigned long kvm_dirty_bitmap_bytes (struct kvm_memory_slot *memslot) {return ALIGN (memslot->npages,BITS_PER_LONG ) /8 ;
virt/kvm/kvm_main.c +2 -2
@@ -1294,7 +1294,7 @@ int __kvm_set_memory_region(struct kvm *kvm,/* Allocate/free page dirty bitmap as needed */ if (!(new.flags &KVM_MEM_LOG_DIRTY_PAGES )) new.dirty_bitmap =NULL ; -else if (!new.dirty_bitmap) { +else if (!new.dirty_bitmap && !kvm->dirty_ring_size) { r =kvm_alloc_dirty_bitmap (&new);if (r)return r;
@@ -2581,7 +2581,7 @@ static void mark_page_dirty_in_slot(struct kvm *kvm, struct kvm_memory_slot *memslot, gfn_t gfn) { -if (memslot && memslot->dirty_bitmap) { +if (memslot &&kvm_slot_dirty_track_enabled (memslot)) {unsigned long rel_gfn = gfn - memslot->base_gfn;u32 slot = (memslot->as_id <<16 ) | memslot->id;
Signature · 1 line
2.24.1
PATCH v8 8/14 KVM: selftests: Always clear dirty bitmap after iteration
In reply to Peter XuWe don't clear the dirty bitmap before because KVM_GET_DIRTY_LOG will clear it for us before copying the dirty log onto it. However we'd still better to clear it explicitly instead of assuming the kernel will always do it for us.
More importantly, in the upcoming dirty ring tests we'll start to fetch dirty pages from a ring buffer, so no one is going to clear the dirty bitmap for us.
--- tools/testing/selftests/kvm/dirty_log_test.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/kvm/dirty_log_test.c +1 -1
@@ -195,7 +195,7 @@ static void vm_dirty_log_verify(enum vm_guest_mode mode, unsigned long *bmap) page); } -if (test_bit_le (page, bmap)) { +if (test_and_clear_bit_le (page, bmap)) { host_dirty_count++;/* * If the bit is set, the value written onto
Signature · 1 line
2.24.1
Re: [PATCH v8 08/14] KVM: selftests: Always clear dirty bitmap after iteration
Quoted 30 lines · Peter Xu
We don't clear the dirty bitmap before because
KVM_GET_DIRTY_LOGwill clear it for us before copying the dirty log onto it. However we'd still better to clear it explicitly instead of assuming the kernel will always do it for us.More importantly, in the upcoming dirty ring tests we'll start to fetch dirty pages from a ring buffer, so no one is going to clear the dirty bitmap for us.
Signed-off-by Peter Xu <[email protected]>--- tools/testing/selftests/kvm/dirty_log_test.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tools/testing/selftests/kvm/dirty_log_test.c +1
-1@@ -195,7 +195,7 @@ static void vm_dirty_log_verify(enum vm_guest_mode mode, unsigned long *bmap) page); } -if (test_bit_le (page, bmap)) { +if (test_and_clear_bit_le (page, bmap)) { host_dirty_count++;/* * If the bit is set, the value written onto Signature · 1 line
2.24.1
PATCH v8 9/14 KVM: selftests: Sync uapi/linux/kvm.h to tools/
In reply to Peter XuThis will be needed to extend the kvm selftest program.
--- tools/include/uapi/linux/kvm.h | 100 ++++++++++++++++++++++++++++++++- 1 file changed, 98 insertions(+), 2 deletions(-)
tools/include/uapi/linux/kvm.h +98 -2
@@ -236,6 +236,7 @@ struct kvm_hyperv_exit {#define KVM_EXIT_IOAPIC_EOI 26#define KVM_EXIT_HYPERV 27#define KVM_EXIT_ARM_NISV 28 +#define KVM_EXIT_DIRTY_RING_FULL 29/* For KVM_EXIT_INTERNAL_ERROR */ /* Emulate instruction failed. */
@@ -474,12 +475,17 @@ struct kvm_s390_mem_op {__u32 size;/* amount of bytes */ __u32 op;/* type of operation */ __u64 buf;/* buffer in userspace */ -__u8 ar;/* the access register number */ -__u8 reserved[31 ];/* should be set to 0 */ +union { +__u8 ar;/* the access register number */ +__u32 sida_offset;/* offset into the sida */ +__u8 reserved[32 ];/* should be set to 0 */ + }; };/* types for kvm_s390_mem_op->op */ #define KVM_S390_MEMOP_LOGICAL_READ 0#define KVM_S390_MEMOP_LOGICAL_WRITE 1 +#define KVM_S390_MEMOP_SIDA_READ 2 +#define KVM_S390_MEMOP_SIDA_WRITE 3/* flags for kvm_s390_mem_op->flags */ #define KVM_S390_MEMOP_F_CHECK_ONLY (1ULL << 0)#define KVM_S390_MEMOP_F_INJECT_EXCEPTION (1ULL << 1)
@@ -1010,6 +1016,9 @@ struct kvm_ppc_resize_hpt {#define KVM_CAP_ARM_NISV_TO_USER 177#define KVM_CAP_ARM_INJECT_EXT_DABT 178#define KVM_CAP_S390_VCPU_RESETS 179 +#define KVM_CAP_S390_PROTECTED 180 +#define KVM_CAP_PPC_SECURE_GUEST 181 +#define KVM_CAP_DIRTY_LOG_RING 182#ifdef KVM_CAP_IRQ_ROUTING
@@ -1478,6 +1487,42 @@ struct kvm_enc_region {#define KVM_S390_NORMAL_RESET _IO(KVMIO, 0xc3)#define KVM_S390_CLEAR_RESET _IO(KVMIO, 0xc4) +struct kvm_s390_pv_sec_parm { +__u64 origin; +__u64 length; +}; + +struct kvm_s390_pv_unp { +__u64 addr; +__u64 size; +__u64 tweak; +}; + +enum pv_cmd_id { +KVM_PV_ENABLE , +KVM_PV_DISABLE , +KVM_PV_SET_SEC_PARMS , +KVM_PV_UNPACK , +KVM_PV_VERIFY , +KVM_PV_PREP_RESET , +KVM_PV_UNSHARE_ALL , +}; + +struct kvm_pv_cmd { +__u32 cmd;/* Command to be executed */ +__u16 rc;/* Ultravisor return code */ +__u16 rrc;/* Ultravisor return reason code */ +__u64 data;/* Data or address */ +__u32 flags;/* flags for future extensions. Must be 0 for now */ +__u32 reserved[3 ]; +}; + +/* Available with KVM_CAP_S390_PROTECTED */ +#define KVM_S390_PV_COMMAND _IOWR(KVMIO, 0xc5, struct kvm_pv_cmd) + +/* Available with KVM_CAP_DIRTY_LOG_RING */ +#define KVM_RESET_DIRTY_RINGS _IO(KVMIO, 0xc6) +/* Secure Encrypted Virtualization command */ enum sev_cmd_id {/* Guest initialization commands */
@@ -1628,4 +1673,55 @@ struct kvm_hyperv_eventfd {#define KVM_HYPERV_CONN_ID_MASK 0x00ffffff#define KVM_HYPERV_EVENTFD_DEASSIGN (1 << 0) +#define KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE (1 << 0) +#define KVM_DIRTY_LOG_INITIALLY_SET (1 << 1) + +/* +* Arch needs to define the macro after implementing the dirty ring +* feature. KVM_DIRTY_LOG_PAGE_OFFSET should be defined as the +* starting page offset of the dirty ring structures. +*/ +#ifndef KVM_DIRTY_LOG_PAGE_OFFSET +#define KVM_DIRTY_LOG_PAGE_OFFSET 0 +#endif + +/* +* KVM dirty GFN flags, defined as: +* +* |---------------+---------------+--------------| +* | bit 1 (reset) | bit 0 (dirty) | Status | +* |---------------+---------------+--------------| +* | 0 | 0 | Invalid GFN | +* | 0 | 1 | Dirty GFN | +* | 1 | X | GFN to reset | +* |---------------+---------------+--------------| +* +* Lifecycle of a dirty GFN goes like: +* +* dirtied collected reset +* 00 -----------> 01 -------------> 1X -------+ +* ^ | +* | | +* +------------------------------------------+ +* +* The userspace program is only responsible for the 01->1X state +* conversion (to collect dirty bits). Also, it must not skip any +* dirty bits so that dirty bits are always collected in sequence. +*/ +#define KVM_DIRTY_GFN_F_DIRTY BIT(0) +#define KVM_DIRTY_GFN_F_RESET BIT(1) +#define KVM_DIRTY_GFN_F_MASK 0x3 + +/* +* KVM dirty rings should be mapped at KVM_DIRTY_LOG_PAGE_OFFSET of +* per-vcpu mmaped regions as an array of struct kvm_dirty_gfn. The +* size of the gfn buffer is decided by the first argument when +* enabling KVM_CAP_DIRTY_LOG_RING. +*/ +struct kvm_dirty_gfn { +__u32 flags; +__u32 slot; +__u64 offset; +}; +#endif /* __LINUX_KVM_H */
Signature · 1 line
2.24.1
PATCH v8 10/14 KVM: selftests: Use a single binary for dirty/clear log test
In reply to Peter XuRemove the clear_dirty_log test, instead merge it into the existing dirty_log_test. It should be cleaner to use this single binary to do both tests, also it's a preparation for the upcoming dirty ring test.
The default behavior will run all the modes in sequence.
--- tools/testing/selftests/kvm/Makefile | 2 - .../selftests/kvm/clear_dirty_log_test.c | 6 - tools/testing/selftests/kvm/dirty_log_test.c | 187 +++++++++++++++--- 3 files changed, 156 insertions(+), 39 deletions(-) delete mode 100644 tools/testing/selftests/kvm/clear_dirty_log_test.c
tools/testing/selftests/kvm/Makefile +0 -2
@@ -28,13 +28,11 @@ TEST_GEN_PROGS_x86_64 += x86_64/vmx_dirty_log_test TEST_GEN_PROGS_x86_64 += x86_64/vmx_set_nested_state_test TEST_GEN_PROGS_x86_64 += x86_64/vmx_tsc_adjust_test TEST_GEN_PROGS_x86_64 += x86_64/xss_msr_test -TEST_GEN_PROGS_x86_64 += clear_dirty_log_test TEST_GEN_PROGS_x86_64 += demand_paging_test TEST_GEN_PROGS_x86_64 += dirty_log_test TEST_GEN_PROGS_x86_64 += kvm_create_max_vcpus TEST_GEN_PROGS_x86_64 += steal_time -TEST_GEN_PROGS_aarch64 += clear_dirty_log_test TEST_GEN_PROGS_aarch64 += demand_paging_test TEST_GEN_PROGS_aarch64 += dirty_log_test TEST_GEN_PROGS_aarch64 += kvm_create_max_vcpus
tools/testing/selftests/kvm/clear_dirty_log_test.c deleted +0 -6
@@ -1,6 +0,0 @@ -#define USE_CLEAR_DIRTY_LOG -#define KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE (1 << 0) -#define KVM_DIRTY_LOG_INITIALLY_SET (1 << 1) -#define KVM_DIRTY_LOG_MANUAL_CAPS (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE | \ - KVM_DIRTY_LOG_INITIALLY_SET) -#include "dirty_log_test.c"
tools/testing/selftests/kvm/dirty_log_test.c +156 -31
259 lines not on this page · raw message
Signature · 1 line
2.24.1
PATCH v8 11/14 KVM: selftests: Introduce after_vcpu_run hook for dirty log test
In reply to Peter XuProvide a hook for the checks after vcpu_run() completes. Preparation for the dirty ring test because we'll need to take care of another exit reason.
Since at it, drop the pages_count because after all we have a better summary right now with statistics, and clean it up a bit.
--- tools/testing/selftests/kvm/dirty_log_test.c | 36 +++++++++++++------- 1 file changed, 24 insertions(+), 12 deletions(-)
tools/testing/selftests/kvm/dirty_log_test.c +24 -12
@@ -178,6 +178,15 @@ static void clear_log_collect_dirty_pages(struct kvm_vm *vm, int slot,kvm_vm_clear_dirty_log (vm, slot, bitmap,0 , num_pages); } +static void default_after_vcpu_run (struct kvm_vm *vm) +{ +struct kvm_run *run =vcpu_state (vm,VCPU_ID ); + +TEST_ASSERT (get_ucall (vm,VCPU_ID ,NULL ) ==UCALL_SYNC , +"Invalid guest sync status: exit_reason=%s\n" , +exit_reason_str (run->exit_reason)); +} +struct log_mode {const char *name;/* Return true if this mode is supported, otherwise false */
@@ -187,16 +196,20 @@ struct log_mode {/* Hook to collect the dirty pages into the bitmap provided */ void (*collect_dirty_pages) (struct kvm_vm *vm,int slot,void *bitmap,uint32_t num_pages); +/* Hook to call when after each vcpu run */ +void (*after_vcpu_run)(struct kvm_vm *vm); } log_modes[LOG_MODE_NUM] = { { .name = "dirty-log", .collect_dirty_pages = dirty_log_collect_dirty_pages, + .after_vcpu_run = default_after_vcpu_run, }, { .name = "clear-log", .supported = clear_log_supported, .create_vm_done = clear_log_create_vm_done, .collect_dirty_pages = clear_log_collect_dirty_pages, + .after_vcpu_run = default_after_vcpu_run, }, };
@@ -247,6 +260,14 @@ static void log_mode_collect_dirty_pages(struct kvm_vm *vm, int slot, mode->collect_dirty_pages (vm, slot, bitmap, num_pages); } +static void log_mode_after_vcpu_run (struct kvm_vm *vm) +{ +struct log_mode *mode = &log_modes[host_log_mode]; + +if (mode->after_vcpu_run) + mode->after_vcpu_run (vm); +} +static void generate_random_array (uint64_t *guest_array,uint64_t size) {uint64_t i;
@@ -261,25 +282,16 @@ static void *vcpu_worker(void *data)struct kvm_vm *vm = data;uint64_t *guest_array;uint64_t pages_count =0 ; -struct kvm_run *run; - - run =vcpu_state (vm,VCPU_ID ); guest_array =addr_gva2hva (vm, (vm_vaddr_t )random_array); -generate_random_array (guest_array,TEST_PAGES_PER_LOOP );while (!READ_ONCE (host_quit)) { +generate_random_array (guest_array,TEST_PAGES_PER_LOOP ); + pages_count +=TEST_PAGES_PER_LOOP ;/* Let the guest dirty the random pages */ ret =_vcpu_run (vm,VCPU_ID );TEST_ASSERT (ret ==0 ,"vcpu_run failed: %d\n" , ret); -if (get_ucall (vm,VCPU_ID ,NULL ) ==UCALL_SYNC ) { - pages_count +=TEST_PAGES_PER_LOOP ; -generate_random_array (guest_array,TEST_PAGES_PER_LOOP ); - }else { -TEST_FAIL ("Invalid guest sync status: " -"exit_reason=%s\n" , -exit_reason_str (run->exit_reason)); - } +log_mode_after_vcpu_run (vm); }pr_info ("Dirtied %" PRIu64" pages\n" , pages_count);
Signature · 1 line
2.24.1
Re: [PATCH v8 11/14] KVM: selftests: Introduce after_vcpu_run hook for dirty log test
Quoted 6 lines · Peter Xu
Provide a hook for the checks after
vcpu_run()completes. Preparation for the dirty ring test because we'll need to take care of another exit reason.Since at it, drop the
pages_countbecause after all we have a better summary right now with statistics, and clean it up a bit.
I don't see what you mean by "drop the pages_count", because it's still there. But otherwise
Quoted 94 lines
Signed-off-by Peter Xu <[email protected]>--- tools/testing/selftests/kvm/dirty_log_test.c | 36 +++++++++++++------- 1 file changed, 24 insertions(+), 12 deletions(-)tools/testing/selftests/kvm/dirty_log_test.c +24
-12@@ -178,6 +178,15 @@ static void clear_log_collect_dirty_pages(struct kvm_vm *vm, int slot,kvm_vm_clear_dirty_log (vm, slot, bitmap,0 , num_pages); } +static void default_after_vcpu_run (struct kvm_vm *vm) +{ +struct kvm_run *run =vcpu_state (vm,VCPU_ID ); + +TEST_ASSERT (get_ucall (vm,VCPU_ID ,NULL ) ==UCALL_SYNC , +"Invalid guest sync status: exit_reason=%s\n" , +exit_reason_str (run->exit_reason)); +} +struct log_mode {const char *name;/* Return true if this mode is supported, otherwise false */ @@ -187,16 +196,20 @@ struct log_mode {/* Hook to collect the dirty pages into the bitmap provided */ void (*collect_dirty_pages) (struct kvm_vm *vm,int slot,void *bitmap,uint32_t num_pages); +/* Hook to call when after each vcpu run */ +void (*after_vcpu_run)(struct kvm_vm *vm); } log_modes[LOG_MODE_NUM] = { { .name = "dirty-log", .collect_dirty_pages = dirty_log_collect_dirty_pages, + .after_vcpu_run = default_after_vcpu_run, }, { .name = "clear-log", .supported = clear_log_supported, .create_vm_done = clear_log_create_vm_done, .collect_dirty_pages = clear_log_collect_dirty_pages, + .after_vcpu_run = default_after_vcpu_run, }, };@@ -247,6 +260,14 @@ static void log_mode_collect_dirty_pages(struct kvm_vm *vm, int slot, mode->collect_dirty_pages (vm, slot, bitmap, num_pages); } +static void log_mode_after_vcpu_run (struct kvm_vm *vm) +{ +struct log_mode *mode = &log_modes[host_log_mode]; + +if (mode->after_vcpu_run) + mode->after_vcpu_run (vm); +} +static void generate_random_array (uint64_t *guest_array,uint64_t size) {uint64_t i;@@ -261,25 +282,16 @@ static void *vcpu_worker(void *data)struct kvm_vm *vm = data;uint64_t *guest_array;uint64_t pages_count =0 ; -struct kvm_run *run; - - run =vcpu_state (vm,VCPU_ID ); guest_array =addr_gva2hva (vm, (vm_vaddr_t )random_array); -generate_random_array (guest_array,TEST_PAGES_PER_LOOP );while (!READ_ONCE (host_quit)) { +generate_random_array (guest_array,TEST_PAGES_PER_LOOP ); + pages_count +=TEST_PAGES_PER_LOOP ;/* Let the guest dirty the random pages */ ret =_vcpu_run (vm,VCPU_ID );TEST_ASSERT (ret ==0 ,"vcpu_run failed: %d\n" , ret); -if (get_ucall (vm,VCPU_ID ,NULL ) ==UCALL_SYNC ) { - pages_count +=TEST_PAGES_PER_LOOP ; -generate_random_array (guest_array,TEST_PAGES_PER_LOOP ); - }else { -TEST_FAIL ("Invalid guest sync status: " -"exit_reason=%s\n" , -exit_reason_str (run->exit_reason)); - } +log_mode_after_vcpu_run (vm); }pr_info ("Dirtied %" PRIu64" pages\n" , pages_count);Signature · 1 line
2.24.1
Re: [PATCH v8 11/14] KVM: selftests: Introduce after_vcpu_run hook for dirty log test
Quoted 5 lines · Andrew Jones
Quoted 4 lines · Peter Xu
Provide a hook for the checks after
vcpu_run()completes. Preparation for the dirty ring test because we'll need to take care of another exit reason.
Quoted 7 lines
Quoted 2 lines
Since at it, drop the
pages_countbecause after all we have a better summary right now with statistics, and clean it up a bit.I don't see what you mean by "drop the
pages_count", because it's still there. But otherwiseReviewed-by Andrew Jones <[email protected]>
I think the pages_count was dropped in some versions, at least the 1st version when I wrote the commit, but it must have went back during one of the rebases upon dirty_log_test.c... To make it simple, I'll just remove this paragraph and pick the r-b (assuming it's valid with that).
Thanks,
Signature · 1 line
Peter Xu
PATCH v8 12/14 KVM: selftests: Add dirty ring buffer test
In reply to Peter XuAdd the initial dirty ring buffer test.
The current test implements the userspace dirty ring collection, by only reaping the dirty ring when the ring is full.
So it's still running synchronously like this:
vcpu main thread
1. vcpu dirties pages
2. vcpu gets dirty ring full
(userspace exit)
3. main thread waits until full (so hardware buffers flushed)
4. main thread collects
5. main thread continues vcpu
6. vcpu continues, goes back to 1
We can't directly collects dirty bits during vcpu execution because otherwise we can't guarantee the hardware dirty bits were flushed when we collect and we're very strict on the dirty bits so otherwise we can fail the future verify procedure. A follow up patch will make this test to support async just like the existing dirty log test, by adding a vcpu kick mechanism.
--- tools/testing/selftests/kvm/dirty_log_test.c | 201 +++++++++++++++++- .../testing/selftests/kvm/include/kvm_util.h | 3 + tools/testing/selftests/kvm/lib/kvm_util.c | 59 +++++ .../selftests/kvm/lib/kvm_util_internal.h | 4 + 4 files changed, 265 insertions(+), 2 deletions(-)
tools/testing/selftests/kvm/dirty_log_test.c +199 -2
@@ -12,8 +12,10 @@#include <unistd.h> #include <time.h> #include <pthread.h> +#include <semaphore.h> #include <linux/bitmap.h> #include <linux/bitops.h> +#include <asm/barrier.h> #include "test_util.h" #include "kvm_util.h"
@@ -57,6 +59,8 @@# define test_and_clear_bit_le test_and_clear_bit#endif +#define TEST_DIRTY_RING_COUNT 1024 +/* * Guest/Host shared variables. Ensure addr_gva2hva() and/or * sync_global_to/from_guest() are used when accessing from
@@ -128,6 +132,10 @@ static uint64_t host_dirty_count;static uint64_t host_clear_count;static uint64_t host_track_next_count; +/* Whether dirty ring reset is requested, or finished */ +static sem_t dirty_ring_vcpu_stop; +static sem_t dirty_ring_vcpu_cont; +enum log_mode_t {/* Only use KVM_GET_DIRTY_LOG for logging */ LOG_MODE_DIRTY_LOG =0 ,
@@ -135,6 +143,9 @@ enum log_mode_t {/* Use both KVM_[GET|CLEAR]_DIRTY_LOG for logging */ LOG_MODE_CLEAR_LOG =1 , +/* Use dirty ring for logging */ +LOG_MODE_DIRTY_RING =2 , + LOG_MODE_NUM,/* Run all supported modes */
@@ -187,6 +198,120 @@ static void default_after_vcpu_run(struct kvm_vm *vm) exit_reason_str(run->exit_reason)); } +static bool dirty_ring_supported (void ) +{ +return kvm_check_cap (KVM_CAP_DIRTY_LOG_RING ); +} + +static void dirty_ring_create_vm_done (struct kvm_vm *vm) +{ +/* +* Switch to dirty ring mode after VM creation but before any +* of the vcpu creation. +*/ +vm_enable_dirty_ring (vm,TEST_DIRTY_RING_COUNT * +sizeof (struct kvm_dirty_gfn )); +} + +static inline bool dirty_gfn_is_dirtied (struct kvm_dirty_gfn *gfn) +{ +return gfn->flags ==KVM_DIRTY_GFN_F_DIRTY ; +} + +static inline void dirty_gfn_set_collected (struct kvm_dirty_gfn *gfn) +{ + gfn->flags =KVM_DIRTY_GFN_F_RESET ; +} + +static uint32_t dirty_ring_collect_one (struct kvm_dirty_gfn *dirty_gfns, +int slot,void *bitmap, +uint32_t num_pages,uint32_t *fetch_index) +{ +struct kvm_dirty_gfn *cur; +uint32_t count =0 ; + +while (true) { + cur = &dirty_gfns[*fetch_index %TEST_DIRTY_RING_COUNT ]; +if (!dirty_gfn_is_dirtied (cur)) +break ; +TEST_ASSERT (cur->slot == slot,"Slot number didn't match: " +"%u != %u" , cur->slot, slot); +TEST_ASSERT (cur->offset < num_pages,"Offset overflow: " +"0x%llx >= 0x%x" , cur->offset, num_pages); +pr_info ("fetch 0x%x page %llu\n" , *fetch_index, cur->offset); +set_bit (cur->offset, bitmap); +dirty_gfn_set_collected (cur); + (*fetch_index)++; + count++; + } + +return count; +} + +static void dirty_ring_collect_dirty_pages (struct kvm_vm *vm,int slot, +void *bitmap,uint32_t num_pages) +{ +/* We only have one vcpu */ +static uint32_t fetch_index =0 ; +uint32_t count =0 , cleared; + +/* +* Before fetching the dirty pages, we need a vmexit of the +* worker vcpu to make sure the hardware dirty buffers were +* flushed. This is not needed for dirty-log/clear-log tests +* because get dirty log will natually do so. +* +* For now we do it in the simple way - we simply wait until +* the vcpu uses up the soft dirty ring, then it'll always +* do a vmexit to make sure that PML buffers will be flushed. +* In real hypervisors, we probably need a vcpu kick or to +* stop the vcpus (before the final sync) to make sure we'll +* get all the existing dirty PFNs even cached in hardware. +*/ +sem_wait (&dirty_ring_vcpu_stop); + +/* Only have one vcpu */ + count =dirty_ring_collect_one (vcpu_map_dirty_ring (vm,VCPU_ID ), + slot, bitmap, num_pages, &fetch_index); + + cleared =kvm_vm_reset_dirty_ring (vm); + +/* Cleared pages should be the same as collected */ +TEST_ASSERT (cleared == count,"Reset dirty pages (%u) mismatch " +"with collected (%u)" , cleared, count); + +pr_info ("Notifying vcpu to continue\n" ); +sem_post (&dirty_ring_vcpu_cont); + +pr_info ("Iteration %ld collected %u pages\n" , iteration, count); +} + +static void dirty_ring_after_vcpu_run (struct kvm_vm *vm) +{ +struct kvm_run *run =vcpu_state (vm,VCPU_ID ); + +/* A ucall-sync or ring-full event is allowed */ +if (get_ucall (vm,VCPU_ID ,NULL ) ==UCALL_SYNC ) { +/* We should allow this to continue */ + ; + }else if (run->exit_reason ==KVM_EXIT_DIRTY_RING_FULL ) { +sem_post (&dirty_ring_vcpu_stop); +pr_info ("vcpu stops because dirty ring full...\n" ); +sem_wait (&dirty_ring_vcpu_cont); +pr_info ("vcpu continues now.\n" ); + }else { +TEST_ASSERT (false,"Invalid guest sync status: " +"exit_reason=%s\n" , +exit_reason_str (run->exit_reason)); + } +} + +static void dirty_ring_before_vcpu_join (void ) +{ +/* Kick another round of vcpu just to make sure it will quit */ +sem_post (&dirty_ring_vcpu_cont); +} +struct log_mode {const char *name;/* Return true if this mode is supported, otherwise false */
@@ -198,6 +323,7 @@ struct log_mode { void *bitmap, uint32_t num_pages);/* Hook to call when after each vcpu run */ void (*after_vcpu_run)(struct kvm_vm *vm); +void (*before_vcpu_join) (void ); } log_modes[LOG_MODE_NUM] = { { .name = "dirty-log",
@@ -211,6 +337,14 @@ struct log_mode { .collect_dirty_pages = clear_log_collect_dirty_pages, .after_vcpu_run = default_after_vcpu_run, }, + { + .name = "dirty-ring", + .supported = dirty_ring_supported, + .create_vm_done = dirty_ring_create_vm_done, + .collect_dirty_pages = dirty_ring_collect_dirty_pages, + .before_vcpu_join = dirty_ring_before_vcpu_join, + .after_vcpu_run = dirty_ring_after_vcpu_run, + }, };/*
@@ -268,6 +402,14 @@ static void log_mode_after_vcpu_run(struct kvm_vm *vm) mode->after_vcpu_run (vm); } +static void log_mode_before_vcpu_join (void ) +{ +struct log_mode *mode = &log_modes[host_log_mode]; + +if (mode->before_vcpu_join) + mode->before_vcpu_join (); +} +static void generate_random_array (uint64_t *guest_array,uint64_t size) {uint64_t i;
@@ -318,14 +460,65 @@ static void vm_dirty_log_verify(enum vm_guest_mode mode, unsigned long *bmap) }if (test_and_clear_bit_le (page, bmap)) { +bool matched; + host_dirty_count++; +/* * If the bit is set, the value written onto * the corresponding page should be either the * previous iteration number or the current one. */ -TEST_ASSERT (*value_ptr == iteration || - *value_ptr == iteration -1 , + matched = (*value_ptr == iteration || + *value_ptr == iteration -1 ); + +if (host_log_mode ==LOG_MODE_DIRTY_RING && !matched) { +if (*value_ptr == iteration -2 ) { +/* +* Short answer: this case is special +* only for dirty ring test where the +* page is the last page before a kvm +* dirty ring full in iteration N-2. +* +* Long answer: Assuming ring size R, +* one possible condition is: +* +* main thr vcpu thr +* -------- -------- +* iter=1 +* write 1 to page 0~(R-1) +* full, vmexit +* collect 0~(R-1) +* kick vcpu +* write 1 to (R-1)~(2R-2) +* full, vmexit +* iter=2 +* collect (R-1)~(2R-2) +* kick vcpu +* write 1 to (2R-2) +* (NOTE!!! "1" cached in cpu reg) +* write 2 to (2R-1)~(3R-3) +* full, vmexit +* iter=3 +* collect (2R-2)~(3R-3) +* (here if we read value on page +* "2R-2" is 1, while iter=3!!!) +*/ + matched = true; + }else { +/* +* This is also special for dirty ring +* when this page is exactly the last +* page touched before vcpu ring full. +* If it happens, we should expect the +* value to change in the next round. +*/ +set_bit_le (page, host_bmap_track); +continue ; + } + } + +TEST_ASSERT (matched,"Set page %" PRIu64" value %" PRIu64" incorrect (iteration=%" PRIu64")" , page, *value_ptr, iteration);
@@ -488,6 +681,7 @@ static void run_test(enum vm_guest_mode mode, unsigned long iterations,/* Tell the vcpu thread to quit */ host_quit = true; +log_mode_before_vcpu_join ();pthread_join (vcpu_thread,NULL ); pr_info("Total bits checked: dirty (%"PRIu64"), clear (%"PRIu64"), "
@@ -548,6 +742,9 @@ int main(int argc, char *argv[])unsigned int mode;int opt, i, j; +sem_init (&dirty_ring_vcpu_stop,0 ,0 ); +sem_init (&dirty_ring_vcpu_cont,0 ,0 ); +#ifdef __x86_64__guest_mode_init (VM_MODE_PXXV48_4K , true, true);#endif
tools/testing/selftests/kvm/include/kvm_util.h +3 -0
@@ -62,6 +62,7 @@ enum vm_mem_backing_src_type {int kvm_check_cap (long cap);int vm_enable_cap (struct kvm_vm *vm,struct kvm_enable_cap *cap); +void vm_enable_dirty_ring (struct kvm_vm *vm,uint32_t ring_size);struct kvm_vm *vm_create (enum vm_guest_mode mode,uint64_t phy_pages,int perm);struct kvm_vm *_vm_create (enum vm_guest_mode mode,uint64_t phy_pages,int perm);
@@ -71,6 +72,7 @@ void kvm_vm_release(struct kvm_vm *vmp);void kvm_vm_get_dirty_log (struct kvm_vm *vm,int slot,void *log);void kvm_vm_clear_dirty_log (struct kvm_vm *vm,int slot,void *log,uint64_t first_page,uint32_t num_pages); +uint32_t kvm_vm_reset_dirty_ring (struct kvm_vm *vm);int kvm_memcmp_hva_gva (void *hva,struct kvm_vm *vm,const vm_vaddr_t gva,size_t len);
@@ -192,6 +194,7 @@ void vcpu_nested_state_get(struct kvm_vm *vm, uint32_t vcpuid,int vcpu_nested_state_set (struct kvm_vm *vm,uint32_t vcpuid,struct kvm_nested_state *state,bool ignore_error);#endif +void *vcpu_map_dirty_ring (struct kvm_vm *vm,uint32_t vcpuid);const char *exit_reason_str (unsigned int exit_reason);
tools/testing/selftests/kvm/lib/kvm_util.c +59 -0
89 lines not on this page · raw message
tools/testing/selftests/kvm/lib/kvm_util_internal.h +4 -0
@@ -28,6 +28,9 @@ struct vcpu {uint32_t id;int fd;struct kvm_run *state; +struct kvm_dirty_gfn *dirty_gfns; +uint32_t fetch_index; +uint32_t dirty_gfns_count; };struct kvm_vm {
@@ -50,6 +53,7 @@ struct kvm_vm {vm_paddr_t pgd;vm_vaddr_t gdt;vm_vaddr_t tss; +uint32_t dirty_ring_size; };struct vcpu *vcpu_find (struct kvm_vm *vm,uint32_t vcpuid);
Signature · 1 line
2.24.1
PATCH v8 13/14 KVM: selftests: Let dirty_log_test async for dirty ring test
In reply to Peter XuPreviously the dirty ring test was working in synchronous way, because only with a vmexit (with that it was the ring full event) we'll know the hardware dirty bits will be flushed to the dirty ring.
With this patch we first introduced the vcpu kick mechanism by using SIGUSR1, meanwhile we can have a guarantee of vmexit and also the flushing of hardware dirty bits. With all these, we can keep the vcpu dirty work asynchronous of the whole collection procedure now. Still, we need to be very careful that we can only do it async if the vcpu is not reaching soft limit (no KVM_EXIT_DIRTY_RING_FULL). Otherwise we must collect the dirty bits before continuing the vcpu.
Further increase the dirty ring size to current maximum to make sure we torture more on the no-ring-full case, which should be the major scenario when the hypervisors like QEMU would like to use this feature.
--- tools/testing/selftests/kvm/dirty_log_test.c | 126 +++++++++++++----- .../testing/selftests/kvm/include/kvm_util.h | 1 + tools/testing/selftests/kvm/lib/kvm_util.c | 9 ++ 3 files changed, 106 insertions(+), 30 deletions(-)
tools/testing/selftests/kvm/dirty_log_test.c +96 -30
@@ -13,6 +13,9 @@#include <time.h> #include <pthread.h> #include <semaphore.h> +#include <sys/types.h> +#include <signal.h> +#include <errno.h> #include <linux/bitmap.h> #include <linux/bitops.h> #include <asm/barrier.h>
@@ -59,7 +62,9 @@# define test_and_clear_bit_le test_and_clear_bit#endif -#define TEST_DIRTY_RING_COUNT 1024 +#define TEST_DIRTY_RING_COUNT 65536 + +#define SIG_IPI SIGUSR1/* * Guest/Host shared variables. Ensure addr_gva2hva() and/or
@@ -135,6 +140,12 @@ static uint64_t host_track_next_count;/* Whether dirty ring reset is requested, or finished */ static sem_t dirty_ring_vcpu_stop;static sem_t dirty_ring_vcpu_cont; +/* +* This is updated by the vcpu thread to tell the host whether it's a +* ring-full event. It should only be read until a sem_wait() of +* dirty_ring_vcpu_stop and before vcpu continues to run. +*/ +static bool dirty_ring_vcpu_ring_full;enum log_mode_t {/* Only use KVM_GET_DIRTY_LOG for logging */
@@ -156,6 +167,33 @@ enum log_mode_t {static enum log_mode_t host_log_mode_option =LOG_MODE_ALL ;/* Logging mode for current run */ static enum log_mode_t host_log_mode; +static pthread_t vcpu_thread; + +/* Only way to pass this to the signal handler */ +static struct kvm_vm *current_vm; + +static void vcpu_sig_handler (int sig) +{ +TEST_ASSERT (sig ==SIG_IPI ,"unknown signal: %d" , sig); +} + +static void vcpu_kick (void ) +{ +pthread_kill (vcpu_thread,SIG_IPI ); +} + +/* +* In our test we do signal tricks, let's use a better version of +* sem_wait to avoid signal interrupts +*/ +static void sem_wait_until (sem_t *sem) +{ +int ret; + +do + ret =sem_wait (sem); +while (ret ==-1 && errno ==EINTR ); +}static bool clear_log_supported (void ) {
@@ -189,10 +227,13 @@ static void clear_log_collect_dirty_pages(struct kvm_vm *vm, int slot,kvm_vm_clear_dirty_log (vm, slot, bitmap,0 , num_pages); } -static void default_after_vcpu_run (struct kvm_vm *vm) +static void default_after_vcpu_run (struct kvm_vm *vm,int ret,int err) {struct kvm_run *run =vcpu_state (vm,VCPU_ID ); +TEST_ASSERT (ret ==0 || (ret ==-1 && err ==EINTR ), +"vcpu run failed: errno=%d" , err); +TEST_ASSERT (get_ucall (vm,VCPU_ID ,NULL ) ==UCALL_SYNC ,"Invalid guest sync status: exit_reason=%s\n" ,exit_reason_str (run->exit_reason));
@@ -248,27 +289,37 @@ static uint32_t dirty_ring_collect_one(struct kvm_dirty_gfn *dirty_gfns,return count; } +static void dirty_ring_wait_vcpu (void ) +{ +/* This makes sure that hardware PML cache flushed */ +vcpu_kick (); +sem_wait_until (&dirty_ring_vcpu_stop); +} + +static void dirty_ring_continue_vcpu (void ) +{ +pr_info ("Notifying vcpu to continue\n" ); +sem_post (&dirty_ring_vcpu_cont); +} +static void dirty_ring_collect_dirty_pages (struct kvm_vm *vm,int slot,void *bitmap,uint32_t num_pages) {/* We only have one vcpu */ static uint32_t fetch_index =0 ;uint32_t count =0 , cleared; +bool continued_vcpu = false; -/* -* Before fetching the dirty pages, we need a vmexit of the -* worker vcpu to make sure the hardware dirty buffers were -* flushed. This is not needed for dirty-log/clear-log tests -* because get dirty log will natually do so. -* -* For now we do it in the simple way - we simply wait until -* the vcpu uses up the soft dirty ring, then it'll always -* do a vmexit to make sure that PML buffers will be flushed. -* In real hypervisors, we probably need a vcpu kick or to -* stop the vcpus (before the final sync) to make sure we'll -* get all the existing dirty PFNs even cached in hardware. -*/ -sem_wait (&dirty_ring_vcpu_stop); +dirty_ring_wait_vcpu (); + +if (!dirty_ring_vcpu_ring_full) { +/* +* This is not a ring-full event, it's safe to allow +* vcpu to continue +*/ +dirty_ring_continue_vcpu (); + continued_vcpu = true; + }/* Only have one vcpu */ count = dirty_ring_collect_one(vcpu_map_dirty_ring(vm, VCPU_ID),
@@ -280,13 +331,16 @@ static void dirty_ring_collect_dirty_pages(struct kvm_vm *vm, int slot,TEST_ASSERT (cleared == count,"Reset dirty pages (%u) mismatch " "with collected (%u)" , cleared, count); -pr_info ("Notifying vcpu to continue\n" ); -sem_post (&dirty_ring_vcpu_cont); +if (!continued_vcpu) { +TEST_ASSERT (dirty_ring_vcpu_ring_full, +"Didn't continue vcpu even without ring full" ); +dirty_ring_continue_vcpu (); + }pr_info ("Iteration %ld collected %u pages\n" , iteration, count); } -static void dirty_ring_after_vcpu_run (struct kvm_vm *vm) +static void dirty_ring_after_vcpu_run (struct kvm_vm *vm,int ret,int err) {struct kvm_run *run =vcpu_state (vm,VCPU_ID );
@@ -294,10 +348,16 @@ static void dirty_ring_after_vcpu_run(struct kvm_vm *vm)if (get_ucall (vm,VCPU_ID ,NULL ) ==UCALL_SYNC ) {/* We should allow this to continue */ ; - }else if (run->exit_reason ==KVM_EXIT_DIRTY_RING_FULL ) { + }else if (run->exit_reason ==KVM_EXIT_DIRTY_RING_FULL || + (ret ==-1 && err ==EINTR )) { +/* Update the flag first before pause */ +WRITE_ONCE (dirty_ring_vcpu_ring_full, + run->exit_reason ==KVM_EXIT_DIRTY_RING_FULL );sem_post (&dirty_ring_vcpu_stop); -pr_info ("vcpu stops because dirty ring full...\n" ); -sem_wait (&dirty_ring_vcpu_cont); +pr_info ("vcpu stops because %s...\n" , + dirty_ring_vcpu_ring_full ? +"dirty ring is full" :"vcpu is kicked out" ); +sem_wait_until (&dirty_ring_vcpu_cont);pr_info ("vcpu continues now.\n" ); }else { TEST_ASSERT(false, "Invalid guest sync status: "
@@ -322,7 +382,7 @@ struct log_mode {void (*collect_dirty_pages) (struct kvm_vm *vm,int slot,void *bitmap,uint32_t num_pages);/* Hook to call when after each vcpu run */ -void (*after_vcpu_run)(struct kvm_vm *vm); +void (*after_vcpu_run)(struct kvm_vm *vm,int ret,int err);void (*before_vcpu_join) (void ); } log_modes[LOG_MODE_NUM] = { {
@@ -394,12 +454,12 @@ static void log_mode_collect_dirty_pages(struct kvm_vm *vm, int slot, mode->collect_dirty_pages (vm, slot, bitmap, num_pages); } -static void log_mode_after_vcpu_run (struct kvm_vm *vm) +static void log_mode_after_vcpu_run (struct kvm_vm *vm,int ret,int err) {struct log_mode *mode = &log_modes[host_log_mode];if (mode->after_vcpu_run) - mode->after_vcpu_run (vm); + mode->after_vcpu_run (vm, ret, err); }static void log_mode_before_vcpu_join (void )
@@ -420,20 +480,27 @@ static void generate_random_array(uint64_t *guest_array, uint64_t size)static void *vcpu_worker (void *data) { -int ret; +int ret, vcpu_fd;struct kvm_vm *vm = data;uint64_t *guest_array;uint64_t pages_count =0 ; +struct sigaction sigact; + + current_vm = vm; + vcpu_fd =vcpu_get_fd (vm,VCPU_ID ); +memset (&sigact,0 ,sizeof (sigact)); + sigact.sa_handler = vcpu_sig_handler; +sigaction (SIG_IPI , &sigact,NULL ); guest_array =addr_gva2hva (vm, (vm_vaddr_t )random_array);while (!READ_ONCE (host_quit)) { +/* Clear any existing kick signals */ generate_random_array (guest_array,TEST_PAGES_PER_LOOP ); pages_count +=TEST_PAGES_PER_LOOP ;/* Let the guest dirty the random pages */ - ret =_vcpu_run (vm,VCPU_ID ); -TEST_ASSERT (ret ==0 ,"vcpu_run failed: %d\n" , ret); -log_mode_after_vcpu_run (vm); + ret =ioctl (vcpu_fd,KVM_RUN ,NULL ); +log_mode_after_vcpu_run (vm, ret, errno); }pr_info ("Dirtied %" PRIu64" pages\n" , pages_count);
@@ -583,7 +650,6 @@ static struct kvm_vm *create_vm(enum vm_guest_mode mode, uint32_t vcpuid,static void run_test (enum vm_guest_mode mode,unsigned long iterations,unsigned long interval,uint64_t phys_offset) { -pthread_t vcpu_thread;struct kvm_vm *vm;unsigned long *bmap;
tools/testing/selftests/kvm/include/kvm_util.h +1 -0
@@ -144,6 +144,7 @@ vm_paddr_t addr_gva2gpa(struct kvm_vm *vm, vm_vaddr_t gva);struct kvm_run *vcpu_state (struct kvm_vm *vm,uint32_t vcpuid);void vcpu_run (struct kvm_vm *vm,uint32_t vcpuid);int _vcpu_run (struct kvm_vm *vm,uint32_t vcpuid); +int vcpu_get_fd (struct kvm_vm *vm,uint32_t vcpuid);void vcpu_run_complete_io (struct kvm_vm *vm,uint32_t vcpuid);void vcpu_set_mp_state (struct kvm_vm *vm,uint32_t vcpuid,struct kvm_mp_state *mp_state);
tools/testing/selftests/kvm/lib/kvm_util.c +9 -0
@@ -1207,6 +1207,15 @@ int _vcpu_run(struct kvm_vm *vm, uint32_t vcpuid)return rc; } +int vcpu_get_fd (struct kvm_vm *vm,uint32_t vcpuid) +{ +struct vcpu *vcpu =vcpu_find (vm, vcpuid); + +TEST_ASSERT (vcpu !=NULL ,"vcpu not found, vcpuid: %u" , vcpuid); + +return vcpu->fd; +} +void vcpu_run_complete_io (struct kvm_vm *vm,uint32_t vcpuid) {struct vcpu *vcpu =vcpu_find (vm, vcpuid);
Signature · 1 line
2.24.1
Re: [PATCH v8 13/14] KVM: selftests: Let dirty_log_test async for dirty ring test
Quoted 16 lines · Peter Xu
Previously the dirty ring test was working in synchronous way, because only with a vmexit (with that it was the ring full event) we'll know the hardware dirty bits will be flushed to the dirty ring.
With this patch we first introduced the vcpu kick mechanism by using SIGUSR1, meanwhile we can have a guarantee of vmexit and also the flushing of hardware dirty bits. With all these, we can keep the vcpu dirty work asynchronous of the whole collection procedure now. Still, we need to be very careful that we can only do it async if the vcpu is not reaching soft limit (no
KVM_EXIT_DIRTY_RING_FULL). Otherwise we must collect the dirty bits before continuing the vcpu.Further increase the dirty ring size to current maximum to make sure we torture more on the no-ring-full case, which should be the major scenario when the hypervisors like QEMU would like to use this feature.
Quoted 7 lines
Signed-off-by Peter Xu <[email protected]>--- tools/testing/selftests/kvm/dirty_log_test.c | 126 +++++++++++++----- .../testing/selftests/kvm/include/kvm_util.h | 1 + tools/testing/selftests/kvm/lib/kvm_util.c | 9 ++ 3 files changed, 106 insertions(+), 30 deletions(-)
For the vcpu_kick and sem_wait stuff
PATCH v8 14/14 KVM: selftests: Add "-c" parameter to dirty log test
In reply to Peter XuIt's only used to override the existing dirty ring size/count. If with a bigger ring count, we test async of dirty ring. If with a smaller ring count, we test ring full code path. Async is default.
It has no use for non-dirty-ring tests.
--- tools/testing/selftests/kvm/dirty_log_test.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-)
tools/testing/selftests/kvm/dirty_log_test.c +10 -3
@@ -168,6 +168,7 @@ static enum log_mode_t host_log_mode_option = LOG_MODE_ALL;/* Logging mode for current run */ static enum log_mode_t host_log_mode;static pthread_t vcpu_thread; +static uint32_t test_dirty_ring_count =TEST_DIRTY_RING_COUNT ;/* Only way to pass this to the signal handler */ static struct kvm_vm *current_vm;
@@ -250,7 +251,7 @@ static void dirty_ring_create_vm_done(struct kvm_vm *vm)* Switch to dirty ring mode after VM creation but before any * of the vcpu creation. */ -vm_enable_dirty_ring (vm,TEST_DIRTY_RING_COUNT * +vm_enable_dirty_ring (vm, test_dirty_ring_count *sizeof (struct kvm_dirty_gfn )); }
@@ -272,7 +273,7 @@ static uint32_t dirty_ring_collect_one(struct kvm_dirty_gfn *dirty_gfns,uint32_t count =0 ;while (true) { - cur = &dirty_gfns[*fetch_index %TEST_DIRTY_RING_COUNT ]; + cur = &dirty_gfns[*fetch_index % test_dirty_ring_count];if (!dirty_gfn_is_dirtied (cur))break ; TEST_ASSERT(cur->slot == slot, "Slot number didn't match: "
@@ -778,6 +779,9 @@ static void help(char *name)printf ("usage: %s [-h] [-i iterations] [-I interval] " "[-p offset] [-m mode]\n" , name);puts ("" ); +printf (" -c: specify dirty ring size, in number of entries\n" ); +printf (" (only useful for dirty-ring test; default: %" PRIu32")\n" , +TEST_DIRTY_RING_COUNT );printf (" -i: specify iteration counts (default: %" PRIu64")\n" ,TEST_HOST_LOOP_N ); printf(" -I: specify interval in ms (default: %"PRIu64" ms)\n",
@@ -833,8 +837,11 @@ int main(int argc, char *argv[])guest_mode_init (VM_MODE_P40V48_4K , true, true);#endif -while ((opt =getopt (argc, argv,"hi:I:p:m:M:" )) !=-1 ) { +while ((opt =getopt (argc, argv,"c:hi:I:p:m:M:" )) !=-1 ) {switch (opt) { +case 'c' : + test_dirty_ring_count =strtol (optarg,NULL ,10 ); +break ;case 'i' : iterations =strtol (optarg,NULL ,10 );break ;
Signature · 1 line
2.24.1
Hi,
TL;DR: I'm thinking whether we should record pure GPA/GFN instead of (slot_id, slot_offset) tuple for dirty pages in kvm dirty ring to unbind kvm_dirty_gfn with memslots.
(A slightly longer version starts...)
The problem is that binding dirty tracking operations to KVM memslots is a restriction that needs synchronization to memslot changes, which further needs synchronization across all the vcpus because they're the consumers of memslots. E.g., when we remove a memory slot, we need to flush all the dirty bits correctly before we do the removal of the memslot. That's actually an known defect for QEMU/KVM [1] (I bet it could be a defect for many other hypervisors...) right now with current dirty logging. Meanwhile, even if we fix it, that procedure is not scale at all, and error prone to dead locks.
Here memory removal is really an (still corner-cased but relatively) important scenario to think about for dirty logging comparing to memory additions & movings. Because memory addition will always have no initial dirty page, and we don't really move RAM a lot (or do we ever?!) for a general VM use case.
Then I went a step back to think about why we need these dirty bit information after all if the memslot is going to be removed?
There're two cases:
- When the memslot is going to be removed forever, then the dirty information is indeed meaningless and can be dropped, and,
- When the memslot is going to be removed but quickly added back with changed size, then we need to keep those dirty bits because it's just a commmon way to e.g. punch an MMIO hole in an existing RAM region (here I'd confess I feel like using "slot_id" to identify memslot is really unfriendly syscall design for things like "hole punchings" in the RAM address space... However such "punch hold" operation is really needed even for a common guest for either system reboots or device hotplugs, etc.).
The real scenario we want to cover for dirty tracking is the 2nd one.
If we can track dirty using raw GPA, the 2nd scenario is solved itself. Because we know we'll add those memslots back (though it might be with a different slot ID), then the GPA value will still make sense, which means we should be able to avoid any kind of synchronization for things like memory removals, as long as the userspace is aware of that.
With that, when we fetch the dirty bits, we lookup the memslot dynamically, drop bits if the memslot does not exist on that address (e.g., permanent removals), and use whatever memslot is there for that guest physical address. Though we for sure still need to handle memory move, that the userspace needs to still take care of dirty bit flushing and sync for a memory move, however that's merely not happening so nothing to take care about either.
Does this makes sense? Comments greatly welcomed..
Thanks,
[1] https://lists.gnu.org/archive/html/qemu-devel/2020-03/msg08361.html
Signature · 1 line
Peter Xu
Quoted 40 lines
From: Peter Xu <[email protected]>
Sent: Thursday, April 23, 2020 2:52 AMHi,
TL;DR: I'm thinking whether we should record pure GPA/GFN instead of (
slot_id,slot_offset) tuple for dirty pages in kvm dirty ring to unbindkvm_dirty_gfnwith memslots.(A slightly longer version starts...)
The problem is that binding dirty tracking operations to KVM memslots is a restriction that needs synchronization to memslot changes, which further needs
synchronization across all the vcpus because they're the consumers of memslots.
E.g., when we remove a memory slot, we need to flush all the dirty bits correctly before we do the removal of the memslot. That's actually an known
defect for QEMU/KVM [1] (I bet it could be a defect for many other hypervisors...) right now with current dirty logging. Meanwhile, even if we fix it, that procedure is not scale at all, and error prone to dead locks.Here memory removal is really an (still corner-cased but relatively) important scenario to think about for dirty logging comparing to memory additions & movings. Because memory addition will always have no initial dirty page, and
we don't really move RAM a lot (or do we ever?!) for a general VM use case.Then I went a step back to think about why we need these dirty bit information
after all if the memslot is going to be removed?There're two cases:
- When the memslot is going to be removed forever, then the dirty
information
is indeed meaningless and can be dropped, and,
Quoted 9 lines
- When the memslot is going to be removed but quickly added back with
changed
size, then we need to keep those dirty bits because it's just a commmon
way
to e.g. punch an MMIO hole in an existing RAM region (here I'd confess I feel like using "slot_id" to identify memslot is really unfriendly syscall design for things like "hole punchings" in the RAM address space... However such "punch hold" operation is really needed even for a common guest for either system reboots or device hotplugs, etc.).
why would device hotplug punch a hole in an existing RAM region?
Quoted 8 lines
The real scenario we want to cover for dirty tracking is the 2nd one.
If we can track dirty using raw GPA, the 2nd scenario is solved itself. Because we know we'll add those memslots back (though it might be with a different slot ID), then the GPA value will still make sense, which means we should be able to avoid any kind of synchronization for things like memory removals, as long as the userspace is aware of that.
A curious question. What about the backing storage of the affected GPA is changed after adding back? Is recorded dirty info for previous backing storage still making sense for the newer one?
Thanks
Kevin
Quoted 18 lines
With that, when we fetch the dirty bits, we lookup the memslot dynamically, drop bits if the memslot does not exist on that address (e.g., permanent removals), and use whatever memslot is there for that guest physical address.
Though we for sure still need to handle memory move, that the userspace needs
to still take care of dirty bit flushing and sync for a memory move, however that's merely not happening so nothing to take care about either.Does this makes sense? Comments greatly welcomed..
Thanks,
[1] https://lists.gnu.org/archive/html/qemu-devel/2020-03/msg08361.html
--
Peter Xu
Quoted 50 lines · Kevin
Quoted 40 lines
From: Peter Xu <[email protected]>
Sent: Thursday, April 23, 2020 2:52 AMHi,
TL;DR: I'm thinking whether we should record pure GPA/GFN instead of (
slot_id,slot_offset) tuple for dirty pages in kvm dirty ring to unbindkvm_dirty_gfnwith memslots.(A slightly longer version starts...)
The problem is that binding dirty tracking operations to KVM memslots is a restriction that needs synchronization to memslot changes, which further needs
synchronization across all the vcpus because they're the consumers of memslots.
E.g., when we remove a memory slot, we need to flush all the dirty bits correctly before we do the removal of the memslot. That's actually an known
defect for QEMU/KVM [1] (I bet it could be a defect for many other hypervisors...) right now with current dirty logging. Meanwhile, even if we fix it, that procedure is not scale at all, and error prone to dead locks.Here memory removal is really an (still corner-cased but relatively) important scenario to think about for dirty logging comparing to memory additions & movings. Because memory addition will always have no initial dirty page, and
we don't really move RAM a lot (or do we ever?!) for a general VM use case.Then I went a step back to think about why we need these dirty bit information
after all if the memslot is going to be removed?There're two cases:
- When the memslot is going to be removed forever, then the dirty
information
is indeed meaningless and can be dropped, and,Quoted 9 lines
- When the memslot is going to be removed but quickly added back with
changed
size, then we need to keep those dirty bits because it's just a commmon
way
to e.g. punch an MMIO hole in an existing RAM region (here I'd confess I feel like using "slot_id" to identify memslot is really unfriendly syscall design for things like "hole punchings" in the RAM address space... However such "punch hold" operation is really needed even for a common guest for either system reboots or device hotplugs, etc.).
Quoted 1 line
why would device hotplug punch a hole in an existing RAM region?
I thought it could happen because I used to trace the KVM ioctls and see the memslot changes during driver loading. But later when I tried to hotplug a device I do see that it won't... The new MMIO regions are added only into 0xfe000000 for a virtio-net:
00000000fe000000-00000000fe000fff (prio 0, i/o): virtio-pci-common 00000000fe001000-00000000fe001fff (prio 0, i/o): virtio-pci-isr 00000000fe002000-00000000fe002fff (prio 0, i/o): virtio-pci-device 00000000fe003000-00000000fe003fff (prio 0, i/o): virtio-pci-notify 00000000fe840000-00000000fe84002f (prio 0, i/o): msix-table 00000000fe840800-00000000fe840807 (prio 0, i/o): msix-pba
Does it mean that device plugging is guaranteed to not trigger RAM changes? I am really curious about what cases we need to consider in which we need to keep the dirty bits for a memory removal, and if system reset is the only case, then it could be even easier (because we might be able to avoid the sync in memory removal but do that once in a sys reset hook)...
Quoted 10 lines
Quoted 9 lines
The real scenario we want to cover for dirty tracking is the 2nd one.
If we can track dirty using raw GPA, the 2nd scenario is solved itself. Because we know we'll add those memslots back (though it might be with a different slot ID), then the GPA value will still make sense, which means we should be able to avoid any kind of synchronization for things like memory removals, as long as the userspace is aware of that.
Quoted 3 lines
A curious question. What about the backing storage of the affected GPA is changed after adding back? Is recorded dirty info for previous backing storage still making sense for the newer one?
It's the case of a permanent removal, plus another addition iiuc. Then the worst case is we get some extra dirty bits set on that new memory region, but IMHO that's benigh (we'll migrate some extra pages even they could be zero pages).
Thanks,
Quoted 23 lines
Thanks
KevinQuoted 18 lines
With that, when we fetch the dirty bits, we lookup the memslot dynamically, drop bits if the memslot does not exist on that address (e.g., permanent removals), and use whatever memslot is there for that guest physical address.
Though we for sure still need to handle memory move, that the userspace needs
to still take care of dirty bit flushing and sync for a memory move, however that's merely not happening so nothing to take care about either.Does this makes sense? Comments greatly welcomed..
Thanks,
[1] https://lists.gnu.org/archive/html/qemu-devel/2020-03/msg08361.html
--
Peter Xu
Signature · 1 line
Peter Xu
Quoted 59 lines
From: Peter Xu <[email protected]>
Sent: Thursday, April 23, 2020 11:23 PMQuoted 8 lines · Kevin
Quoted 8 lines
From: Peter Xu <[email protected]>
Sent: Thursday, April 23, 2020 2:52 AMHi,
TL;DR: I'm thinking whether we should record pure GPA/GFN instead of (
slot_id,slot_offset) tuple for dirty pages in kvm dirty ring to unbind
kvm_dirty_gfnQuoted 5 lines
Quoted 5 lines
with memslots.
(A slightly longer version starts...)
The problem is that binding dirty tracking operations to KVM memslots is
a
Quoted 9 lines
Quoted 9 lines
restriction that needs synchronization to memslot changes, which further needs
synchronization across all the vcpus because they're the consumers of memslots.
E.g., when we remove a memory slot, we need to flush all the dirty bits correctly before we do the removal of the memslot. That's actually an known
defect for QEMU/KVM [1] (I bet it could be a defect for many other hypervisors...) right now with current dirty logging. Meanwhile, even ifwe
Quoted 3 lines
Quoted 3 lines
fix it, that procedure is not scale at all, and error prone to dead locks.
Here memory removal is really an (still corner-cased but relatively)
important
Quoted 1 line
Quoted 1 line
scenario to think about for dirty logging comparing to memory additions
&
Quoted 3 lines
Quoted 3 lines
movings. Because memory addition will always have no initial dirty page, and
we don't really move RAM a lot (or do we ever?!) for a general VM usecase.
Quoted 11 lines
Quoted 11 lines
Then I went a step back to think about why we need these dirty bit information
after all if the memslot is going to be removed?There're two cases:
- When the memslot is going to be removed forever, then the dirty
information
is indeed meaningless and can be dropped, and,Quoted 5 lines
Quoted 5 lines
- When the memslot is going to be removed but quickly added back with
changed
size, then we need to keep those dirty bits because it's just a commmon
way
to e.g. punch an MMIO hole in an existing RAM region (here I'd confessI
Quoted 3 lines
Quoted 3 lines
feel like using "
slot_id" to identify memslot is really unfriendly syscall design for things like "hole punchings" in the RAM address space... However such "punch hold" operation is really needed even for a
Quoted 7 lines
common
Quoted 3 lines
Quoted 1 line
guest for either system reboots or device hotplugs, etc.).
why would device hotplug punch a hole in an existing RAM region?
I thought it could happen because I used to trace the KVM ioctls and see the memslot changes during driver loading. But later when I tried to hotplug a
Is there more detail why driver loading may lead to memslot changes?
Quoted 11 lines
device I do see that it won't... The new MMIO regions are added only into
0xfe000000for a virtio-net:00000000fe000000-00000000fe000fff (prio 0, i/o): virtio-pci-common 00000000fe001000-00000000fe001fff (prio 0, i/o): virtio-pci-isr 00000000fe002000-00000000fe002fff (prio 0, i/o): virtio-pci-device 00000000fe003000-00000000fe003fff (prio 0, i/o): virtio-pci-notify 00000000fe840000-00000000fe84002f (prio 0, i/o): msix-table 00000000fe840800-00000000fe840807 (prio 0, i/o): msix-pba
Does it mean that device plugging is guaranteed to not trigger RAM changes?
I'd think so. Otherwise from guest p.o.v any device hotplug implies doing a memory hot-unplug first then it's a bad design.
Quoted 7 lines
I
am really curious about what cases we need to consider in which we need to keep
the dirty bits for a memory removal, and if system reset is the only case, then it could be even easier (because we might be able to avoid the sync in memory
removal but do that once in a sys reset hook)...
Possibly memory hot-unplug, as allowed by recent virtio-mem?
btw VFIO faces a similar problem when unmapping a DMA range (e.g. when vIOMMU is enabled) in dirty log phase. There could be some dirty bits which are not retrieved when unmapping happens. VFIO chooses to return the dirty bits in a buffer passed in the unmapping parameters. Can memslot interface do similar thing by allowing the userspace to specify a buffer pointer to hold whatever dirty pages recorded for the slot that is being removed?
Quoted 18 lines
Quoted 7 lines
Quoted 7 lines
The real scenario we want to cover for dirty tracking is the 2nd one.
If we can track dirty using raw GPA, the 2nd scenario is solved itself. Because we know we'll add those memslots back (though it might be with
a
Quoted 1 line
Quoted 1 line
different slot ID), then the GPA value will still make sense, which means
we
Quoted 1 line
Quoted 1 line
should be able to avoid any kind of synchronization for things like
memory
Quoted 5 lines
Quoted 1 line
removals, as long as the userspace is aware of that.
A curious question. What about the backing storage of the affected GPA is changed after adding back? Is recorded dirty info for previous backing storage still making sense for the newer one?
Quoted 5 lines
It's the case of a permanent removal, plus another addition iiuc. Then the worst case is we get some extra dirty bits set on that new memory region, but
IMHO that's benigh (we'll migrate some extra pages even they could be zero pages).
yes, reporting more than necessary dirty bits doesn't hurt.
Quoted 33 lines
Thanks,
Quoted 6 lines
Thanks
KevinQuoted 2 lines
With that, when we fetch the dirty bits, we lookup the memslot
dynamically,
Quoted 4 lines
Quoted 4 lines
drop bits if the memslot does not exist on that address (e.g., permanent removals), and use whatever memslot is there for that guest physical address.
Though we for sure still need to handle memory move, that theuserspace
Quoted 2 lines
Quoted 2 lines
needs
to still take care of dirty bit flushing and sync for a memory move,however
Quoted 7 lines
Quoted 7 lines
that's merely not happening so nothing to take care about either.
Does this makes sense? Comments greatly welcomed..
Thanks,
03/msg08361.html
Quoted 4 lines
Quoted 3 lines
--
Peter Xu--
Peter Xu
Quoted 60 lines · Kevin
Quoted 60 lines
From: Peter Xu <[email protected]>
Sent: Thursday, April 23, 2020 11:23 PMQuoted 8 lines · Kevin
Quoted 8 lines
From: Peter Xu <[email protected]>
Sent: Thursday, April 23, 2020 2:52 AMHi,
TL;DR: I'm thinking whether we should record pure GPA/GFN instead of (
slot_id,slot_offset) tuple for dirty pages in kvm dirty ring to unbind
kvm_dirty_gfnQuoted 5 lines
Quoted 5 lines
with memslots.
(A slightly longer version starts...)
The problem is that binding dirty tracking operations to KVM memslots is
a
Quoted 9 lines
Quoted 9 lines
restriction that needs synchronization to memslot changes, which further needs
synchronization across all the vcpus because they're the consumers of memslots.
E.g., when we remove a memory slot, we need to flush all the dirty bits correctly before we do the removal of the memslot. That's actually an known
defect for QEMU/KVM [1] (I bet it could be a defect for many other hypervisors...) right now with current dirty logging. Meanwhile, even ifwe
Quoted 3 lines
Quoted 3 lines
fix it, that procedure is not scale at all, and error prone to dead locks.
Here memory removal is really an (still corner-cased but relatively)
important
Quoted 1 line
Quoted 1 line
scenario to think about for dirty logging comparing to memory additions
&
Quoted 3 lines
Quoted 3 lines
movings. Because memory addition will always have no initial dirty page, and
we don't really move RAM a lot (or do we ever?!) for a general VM usecase.
Quoted 11 lines
Quoted 11 lines
Then I went a step back to think about why we need these dirty bit information
after all if the memslot is going to be removed?There're two cases:
- When the memslot is going to be removed forever, then the dirty
information
is indeed meaningless and can be dropped, and,Quoted 5 lines
Quoted 5 lines
- When the memslot is going to be removed but quickly added back with
changed
size, then we need to keep those dirty bits because it's just a commmon
way
to e.g. punch an MMIO hole in an existing RAM region (here I'd confessI
Quoted 3 lines
Quoted 3 lines
feel like using "
slot_id" to identify memslot is really unfriendly syscall design for things like "hole punchings" in the RAM address space... However such "punch hold" operation is really needed even for acommon
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
Quoted 1 line
guest for either system reboots or device hotplugs, etc.).
why would device hotplug punch a hole in an existing RAM region?
I thought it could happen because I used to trace the KVM ioctls and see the memslot changes during driver loading. But later when I tried to hotplug a
Is there more detail why driver loading may lead to memslot changes?
E.g., I can observe these after Linux loads and before the prompt, which is a simplest VM with default devices on:
[email protected]:kvm_set_user_memory Slot#3 flags=0x0 gpa=0xfd000000 size=0x0 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#65539 flags=0x0 gpa=0xfd000000 size=0x0 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#3 flags=0x1 gpa=0xfd000000 size=0x1000000 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#65539 flags=0x1 gpa=0xfd000000 size=0x1000000 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#3 flags=0x0 gpa=0xfd000000 size=0x0 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#65539 flags=0x0 gpa=0xfd000000 size=0x0 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#3 flags=0x1 gpa=0xfd000000 size=0x1000000 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#65539 flags=0x1 gpa=0xfd000000 size=0x1000000 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#3 flags=0x0 gpa=0xfd000000 size=0x0 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#65539 flags=0x0 gpa=0xfd000000 size=0x0 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#3 flags=0x1 gpa=0xfd000000 size=0x1000000 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#65539 flags=0x1 gpa=0xfd000000 size=0x1000000 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#3 flags=0x0 gpa=0xfd000000 size=0x0 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#65539 flags=0x0 gpa=0xfd000000 size=0x0 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#3 flags=0x1 gpa=0xfd000000 size=0x1000000 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#65539 flags=0x1 gpa=0xfd000000 size=0x1000000 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#3 flags=0x0 gpa=0xfd000000 size=0x0 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#65539 flags=0x0 gpa=0xfd000000 size=0x0 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#3 flags=0x1 gpa=0xfd000000 size=0x1000000 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#65539 flags=0x1 gpa=0xfd000000 size=0x1000000 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#3 flags=0x0 gpa=0xfd000000 size=0x0 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#65539 flags=0x0 gpa=0xfd000000 size=0x0 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#3 flags=0x1 gpa=0xfd000000 size=0x1000000 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#65539 flags=0x1 gpa=0xfd000000 size=0x1000000 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#3 flags=0x0 gpa=0xfd000000 size=0x0 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#65539 flags=0x0 gpa=0xfd000000 size=0x0 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#3 flags=0x1 gpa=0xfd000000 size=0x1000000 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#65539 flags=0x1 gpa=0xfd000000 size=0x1000000 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#9 flags=0x1 gpa=0xa0000 size=0x10000 ua=0x7fadf6800000 ret=0
After a careful look, I noticed it's only the VGA device mostly turning slot 3 off & on. Frankly speaking I don't know why it happens to do so.
Quoted 11 lines
Quoted 11 lines
device I do see that it won't... The new MMIO regions are added only into
0xfe000000for a virtio-net:00000000fe000000-00000000fe000fff (prio 0, i/o): virtio-pci-common 00000000fe001000-00000000fe001fff (prio 0, i/o): virtio-pci-isr 00000000fe002000-00000000fe002fff (prio 0, i/o): virtio-pci-device 00000000fe003000-00000000fe003fff (prio 0, i/o): virtio-pci-notify 00000000fe840000-00000000fe84002f (prio 0, i/o): msix-table 00000000fe840800-00000000fe840807 (prio 0, i/o): msix-pba
Quoted 4 lines
Quoted 1 line
Does it mean that device plugging is guaranteed to not trigger RAM changes?
I'd think so. Otherwise from guest p.o.v any device hotplug implies doing a memory hot-unplug first then it's a bad design.
Right that's what I was confused about. Then maybe you're right. :)
Quoted 10 lines
Quoted 8 lines
I
am really curious about what cases we need to consider in which we need to keep
the dirty bits for a memory removal, and if system reset is the only case, then it could be even easier (because we might be able to avoid the sync in memory
removal but do that once in a sys reset hook)...Possibly memory hot-unplug, as allowed by recent virtio-mem?
That should belong to the case where dirty bits do not matter at all after the removal, right? I would be mostly curious about when we (1) remove a memory slot, and at the meantime (2) we still care about the dirty bits of that slot.
I'll see whether I can remove the dirty bit sync in kvm_set_phys_mem(), which I think is really nasty.
Quoted 7 lines
btw VFIO faces a similar problem when unmapping a DMA range (e.g. when vIOMMU is enabled) in dirty log phase. There could be some dirty bits which are not retrieved when unmapping happens. VFIO chooses to return the dirty bits in a buffer passed in the unmapping parameters. Can memslot interface do similar thing by allowing the userspace to specify a buffer pointer to hold whatever dirty pages recorded for the slot that is being removed?
Yes I think we can, but may not be necessary. Actually IMHO CPU access to pages are slightly different to device DMAs in that we can do these sequence to collect the dirty bits of a slot safely:
- mark slot as READONLY
- KVM_GET_DIRTY_LOG on the slot
- finally remove the slot
I guess VFIO cannot do that because there's no way to really "mark the region as read-only" for a device because DMA could happen and DMA would fail then when writting to a readonly slot.
On the KVM/CPU side, after we mark the slot as READONLY then the CPU writes will page fault and fallback to the QEMU userspace, then QEMU will take care of the writes (so those writes could be slow but still working) then even if we mark it READONLY it won't fail the writes but just fallback to QEMU.
Btw, since we're discussing the VFIO dirty logging across memory removal... the unmapping DMA range you're talking about needs to be added back later, right? Then what if the device does DMA after the removal but before it's added back? I think this is a more general question not for dirty logging but also for when dirty logging is not enabled - I never understand how this could be fixed with existing facilities.
Thanks,
Signature · 1 line
Peter Xu
Quoted 183 lines
From: Peter Xu
Sent: Friday, April 24, 2020 10:20 PMQuoted 10 lines · Kevin
Quoted 10 lines
From: Peter Xu <[email protected]>
Sent: Thursday, April 23, 2020 11:23 PMQuoted 6 lines · Kevin
Quoted 6 lines
From: Peter Xu <[email protected]>
Sent: Thursday, April 23, 2020 2:52 AMHi,
TL;DR: I'm thinking whether we should record pure GPA/GFN instead
of
Quoted 8 lines
Quoted 8 lines
Quoted 2 lines
Quoted 2 lines
(
slot_id,slot_offset) tuple for dirty pages in kvm dirty ring to unbind
kvm_dirty_gfnQuoted 5 lines
Quoted 5 lines
with memslots.
(A slightly longer version starts...)
The problem is that binding dirty tracking operations to KVM
memslots is
Quoted 2 lines
Quoted 2 lines
a
Quoted 1 line
Quoted 1 line
restriction that needs synchronization to memslot changes, which
further
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
needs
synchronization across all the vcpus because they're the consumers of memslots.
E.g., when we remove a memory slot, we need to flush all the dirtybits
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
correctly before we do the removal of the memslot. That's actually an known
defect for QEMU/KVM [1] (I bet it could be a defect for many other hypervisors...) right now with current dirty logging. Meanwhile, evenif
Quoted 6 lines
Quoted 6 lines
we
Quoted 3 lines
Quoted 3 lines
fix it, that procedure is not scale at all, and error prone to dead locks.
Here memory removal is really an (still corner-cased but relatively)
important
Quoted 1 line
Quoted 1 line
scenario to think about for dirty logging comparing to memory
additions
Quoted 2 lines
Quoted 2 lines
&
Quoted 1 line
Quoted 1 line
movings. Because memory addition will always have no initial dirty
page,
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
and
we don't really move RAM a lot (or do we ever?!) for a general VMuse
Quoted 6 lines
Quoted 6 lines
case.
Quoted 5 lines
Quoted 5 lines
Then I went a step back to think about why we need these dirty bit information
after all if the memslot is going to be removed?Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
There're two cases:
- When the memslot is going to be removed forever, then the dirty
information
is indeed meaningless and can be dropped, and,- When the memslot is going to be removed but quickly added back
with
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
changed
size, then we need to keep those dirty bits because it's just acommmon
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
way
to e.g. punch an MMIO hole in an existing RAM region (here I'dconfess
Quoted 2 lines
Quoted 2 lines
I
Quoted 1 line
Quoted 1 line
feel like using "
slot_id" to identify memslot is really unfriendlysyscall
Quoted 8 lines
Quoted 8 lines
Quoted 2 lines
Quoted 2 lines
design for things like "hole punchings" in the RAM address space... However such "punch hold" operation is really needed even for a
common
Quoted 3 lines
Quoted 1 line
guest for either system reboots or device hotplugs, etc.).
why would device hotplug punch a hole in an existing RAM region?
I thought it could happen because I used to trace the KVM ioctls and see
the
Quoted 1 line
Quoted 1 line
memslot changes during driver loading. But later when I tried to hotplug
a
Quoted 2 lines
Is there more detail why driver loading may lead to memslot changes?
E.g., I can observe these after Linux loads and before the prompt, which is a simplest VM with default devices on:
[email protected]:kvm_set_user_memory Slot#3 flags=0x0 gpa=0xfd000000 size=0x0 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#65539 flags=0x0 gpa=0xfd000000 size=0x0 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#3 flags=0x1 gpa=0xfd000000 size=0x1000000 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#65539 flags=0x1 gpa=0xfd000000 size=0x1000000 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#3 flags=0x0 gpa=0xfd000000 size=0x0 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#65539 flags=0x0 gpa=0xfd000000 size=0x0 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#3 flags=0x1 gpa=0xfd000000 size=0x1000000 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#65539 flags=0x1 gpa=0xfd000000 size=0x1000000 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#3 flags=0x0 gpa=0xfd000000 size=0x0 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#65539 flags=0x0 gpa=0xfd000000 size=0x0 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#3 flags=0x1 gpa=0xfd000000 size=0x1000000 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#65539 flags=0x1 gpa=0xfd000000 size=0x1000000 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#3 flags=0x0 gpa=0xfd000000 size=0x0 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#65539 flags=0x0 gpa=0xfd000000 size=0x0 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#3 flags=0x1 gpa=0xfd000000 size=0x1000000 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#65539 flags=0x1 gpa=0xfd000000 size=0x1000000 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#3 flags=0x0 gpa=0xfd000000 size=0x0 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#65539 flags=0x0 gpa=0xfd000000 size=0x0 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#3 flags=0x1 gpa=0xfd000000 size=0x1000000 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#65539 flags=0x1 gpa=0xfd000000 size=0x1000000 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#3 flags=0x0 gpa=0xfd000000 size=0x0 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#65539 flags=0x0 gpa=0xfd000000 size=0x0 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#3 flags=0x1 gpa=0xfd000000 size=0x1000000 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#65539 flags=0x1 gpa=0xfd000000 size=0x1000000 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#3 flags=0x0 gpa=0xfd000000 size=0x0 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#65539 flags=0x0 gpa=0xfd000000 size=0x0 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#3 flags=0x1 gpa=0xfd000000 size=0x1000000 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#65539 flags=0x1 gpa=0xfd000000 size=0x1000000 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#9 flags=0x1 gpa=0xa0000 size=0x10000 ua=0x7fadf6800000 ret=0
After a careful look, I noticed it's only the VGA device mostly turning slot 3 off & on. Frankly speaking I don't know why it happens to do so.
Quoted 2 lines
Quoted 2 lines
device I do see that it won't... The new MMIO regions are added only
into
Quoted 10 lines
Quoted 10 lines
0xfe000000for a virtio-net:00000000fe000000-00000000fe000fff (prio 0, i/o): virtio-pci-common 00000000fe001000-00000000fe001fff (prio 0, i/o): virtio-pci-isr 00000000fe002000-00000000fe002fff (prio 0, i/o): virtio-pci-device 00000000fe003000-00000000fe003fff (prio 0, i/o): virtio-pci-notify 00000000fe840000-00000000fe84002f (prio 0, i/o): msix-table 00000000fe840800-00000000fe840807 (prio 0, i/o): msix-pba
Does it mean that device plugging is guaranteed to not trigger RAM
changes?
Quoted 3 lines
I'd think so. Otherwise from guest p.o.v any device hotplug implies doing a memory hot-unplug first then it's a bad design.
Right that's what I was confused about. Then maybe you're right. :)
Quoted 3 lines
Quoted 3 lines
I
am really curious about what cases we need to consider in which weneed to
Quoted 2 lines
Quoted 2 lines
keep
the dirty bits for a memory removal, and if system reset is the only case,then
Quoted 4 lines
Quoted 3 lines
it could be even easier (because we might be able to avoid the sync in memory
removal but do that once in a sys reset hook)...
Quoted 6 lines
Quoted 1 line
Possibly memory hot-unplug, as allowed by recent virtio-mem?
That should belong to the case where dirty bits do not matter at all after the removal, right? I would be mostly curious about when we (1) remove a memory
slot, and at the meantime (2) we still care about the dirty bits of that slot.
I remember one case. PCIe spec defines a resizable BAR capability, allowing hardware to communicate supported resource sizes and software to set an optimal size back to hardware. If such a capability is presented to guest and the BAR is backed by memory, it's possible to observe a removal-and- add-back scenario. However in such case, the spec requires the software to clear memory space enable bit in command register before changing the BAR size. I suppose such thing should happen at boot phase where dirty bits related to old BAR size don't matter.
Quoted 32 lines
I'll see whether I can remove the dirty bit sync in
kvm_set_phys_mem(), which I
think is really nasty.Quoted 2 lines
btw VFIO faces a similar problem when unmapping a DMA range (e.g.
when
Quoted 1 line
vIOMMU is enabled) in dirty log phase. There could be some dirty bits
which are
Quoted 2 lines
not retrieved when unmapping happens. VFIO chooses to return the dirty bits in a buffer passed in the unmapping parameters. Can memslot
interface
Quoted 1 line
do similar thing by allowing the userspace to specify a buffer pointer to
hold
Quoted 1 line
whatever dirty pages recorded for the slot that is being removed?
Yes I think we can, but may not be necessary. Actually IMHO CPU access to pages are slightly different to device DMAs in that we can do these sequence to
collect the dirty bits of a slot safely:- mark slot as READONLY
-KVM_GET_DIRTY_LOGon the slot
- finally remove the slotI guess VFIO cannot do that because there's no way to really "mark the region
as read-only" for a device because DMA could happen and DMA would fail then
when writting to a readonly slot.
Quoted 6 lines
On the KVM/CPU side, after we mark the slot as READONLY then the CPU writes
will page fault and fallback to the QEMU userspace, then QEMU will take care of
the writes (so those writes could be slow but still working) then even if we mark it READONLY it won't fail the writes but just fallback to QEMU.
Yes, you are right.
Quoted 5 lines
Btw, since we're discussing the VFIO dirty logging across memory removal... the
unmapping DMA range you're talking about needs to be added back later, right?
pure memory removal doesn't need add-back. Or are you specifically referring to removal-and-add-back case?
Quoted 2 lines
Then what if the device does DMA after the removal but before it's added back?
then just got IOMMU page fault, but I guess that I didn't get your real question...
Quoted 4 lines
I think this is a more general question not for dirty logging but also for when dirty logging is not enabled - I never understand how this could be fixed with existing facilities.
Thanks,
Kevin
Quoted 190 lines · Kevin
Quoted 183 lines
From: Peter Xu
Sent: Friday, April 24, 2020 10:20 PMQuoted 10 lines · Kevin
Quoted 10 lines
From: Peter Xu <[email protected]>
Sent: Thursday, April 23, 2020 11:23 PMQuoted 6 lines · Kevin
Quoted 6 lines
From: Peter Xu <[email protected]>
Sent: Thursday, April 23, 2020 2:52 AMHi,
TL;DR: I'm thinking whether we should record pure GPA/GFN instead
of
Quoted 8 lines
Quoted 8 lines
Quoted 2 lines
Quoted 2 lines
(
slot_id,slot_offset) tuple for dirty pages in kvm dirty ring to unbind
kvm_dirty_gfnQuoted 5 lines
Quoted 5 lines
with memslots.
(A slightly longer version starts...)
The problem is that binding dirty tracking operations to KVM
memslots is
Quoted 2 lines
Quoted 2 lines
a
Quoted 1 line
Quoted 1 line
restriction that needs synchronization to memslot changes, which
further
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
needs
synchronization across all the vcpus because they're the consumers of memslots.
E.g., when we remove a memory slot, we need to flush all the dirtybits
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
correctly before we do the removal of the memslot. That's actually an known
defect for QEMU/KVM [1] (I bet it could be a defect for many other hypervisors...) right now with current dirty logging. Meanwhile, evenif
Quoted 6 lines
Quoted 6 lines
we
Quoted 3 lines
Quoted 3 lines
fix it, that procedure is not scale at all, and error prone to dead locks.
Here memory removal is really an (still corner-cased but relatively)
important
Quoted 1 line
Quoted 1 line
scenario to think about for dirty logging comparing to memory
additions
Quoted 2 lines
Quoted 2 lines
&
Quoted 1 line
Quoted 1 line
movings. Because memory addition will always have no initial dirty
page,
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
and
we don't really move RAM a lot (or do we ever?!) for a general VMuse
Quoted 6 lines
Quoted 6 lines
case.
Quoted 5 lines
Quoted 5 lines
Then I went a step back to think about why we need these dirty bit information
after all if the memslot is going to be removed?Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
There're two cases:
- When the memslot is going to be removed forever, then the dirty
information
is indeed meaningless and can be dropped, and,- When the memslot is going to be removed but quickly added back
with
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
changed
size, then we need to keep those dirty bits because it's just acommmon
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
way
to e.g. punch an MMIO hole in an existing RAM region (here I'dconfess
Quoted 2 lines
Quoted 2 lines
I
Quoted 1 line
Quoted 1 line
feel like using "
slot_id" to identify memslot is really unfriendlysyscall
Quoted 8 lines
Quoted 8 lines
Quoted 2 lines
Quoted 2 lines
design for things like "hole punchings" in the RAM address space... However such "punch hold" operation is really needed even for a
common
Quoted 3 lines
Quoted 1 line
guest for either system reboots or device hotplugs, etc.).
why would device hotplug punch a hole in an existing RAM region?
I thought it could happen because I used to trace the KVM ioctls and see
the
Quoted 1 line
Quoted 1 line
memslot changes during driver loading. But later when I tried to hotplug
a
Quoted 2 lines
Is there more detail why driver loading may lead to memslot changes?
E.g., I can observe these after Linux loads and before the prompt, which is a simplest VM with default devices on:
[email protected]:kvm_set_user_memory Slot#3 flags=0x0 gpa=0xfd000000 size=0x0 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#65539 flags=0x0 gpa=0xfd000000 size=0x0 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#3 flags=0x1 gpa=0xfd000000 size=0x1000000 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#65539 flags=0x1 gpa=0xfd000000 size=0x1000000 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#3 flags=0x0 gpa=0xfd000000 size=0x0 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#65539 flags=0x0 gpa=0xfd000000 size=0x0 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#3 flags=0x1 gpa=0xfd000000 size=0x1000000 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#65539 flags=0x1 gpa=0xfd000000 size=0x1000000 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#3 flags=0x0 gpa=0xfd000000 size=0x0 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#65539 flags=0x0 gpa=0xfd000000 size=0x0 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#3 flags=0x1 gpa=0xfd000000 size=0x1000000 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#65539 flags=0x1 gpa=0xfd000000 size=0x1000000 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#3 flags=0x0 gpa=0xfd000000 size=0x0 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#65539 flags=0x0 gpa=0xfd000000 size=0x0 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#3 flags=0x1 gpa=0xfd000000 size=0x1000000 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#65539 flags=0x1 gpa=0xfd000000 size=0x1000000 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#3 flags=0x0 gpa=0xfd000000 size=0x0 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#65539 flags=0x0 gpa=0xfd000000 size=0x0 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#3 flags=0x1 gpa=0xfd000000 size=0x1000000 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#65539 flags=0x1 gpa=0xfd000000 size=0x1000000 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#3 flags=0x0 gpa=0xfd000000 size=0x0 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#65539 flags=0x0 gpa=0xfd000000 size=0x0 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#3 flags=0x1 gpa=0xfd000000 size=0x1000000 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#65539 flags=0x1 gpa=0xfd000000 size=0x1000000 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#3 flags=0x0 gpa=0xfd000000 size=0x0 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#65539 flags=0x0 gpa=0xfd000000 size=0x0 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#3 flags=0x1 gpa=0xfd000000 size=0x1000000 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#65539 flags=0x1 gpa=0xfd000000 size=0x1000000 ua=0x7fadf6800000 ret=0 [email protected]:kvm_set_user_memory Slot#9 flags=0x1 gpa=0xa0000 size=0x10000 ua=0x7fadf6800000 ret=0
After a careful look, I noticed it's only the VGA device mostly turning slot 3 off & on. Frankly speaking I don't know why it happens to do so.
Quoted 2 lines
Quoted 2 lines
device I do see that it won't... The new MMIO regions are added only
into
Quoted 10 lines
Quoted 10 lines
0xfe000000for a virtio-net:00000000fe000000-00000000fe000fff (prio 0, i/o): virtio-pci-common 00000000fe001000-00000000fe001fff (prio 0, i/o): virtio-pci-isr 00000000fe002000-00000000fe002fff (prio 0, i/o): virtio-pci-device 00000000fe003000-00000000fe003fff (prio 0, i/o): virtio-pci-notify 00000000fe840000-00000000fe84002f (prio 0, i/o): msix-table 00000000fe840800-00000000fe840807 (prio 0, i/o): msix-pba
Does it mean that device plugging is guaranteed to not trigger RAM
changes?
Quoted 3 lines
I'd think so. Otherwise from guest p.o.v any device hotplug implies doing a memory hot-unplug first then it's a bad design.
Right that's what I was confused about. Then maybe you're right. :)
Quoted 3 lines
Quoted 3 lines
I
am really curious about what cases we need to consider in which weneed to
Quoted 2 lines
Quoted 2 lines
keep
the dirty bits for a memory removal, and if system reset is the only case,then
Quoted 4 lines
Quoted 3 lines
it could be even easier (because we might be able to avoid the sync in memory
removal but do that once in a sys reset hook)...Quoted 6 lines
Quoted 1 line
Possibly memory hot-unplug, as allowed by recent virtio-mem?
That should belong to the case where dirty bits do not matter at all after the removal, right? I would be mostly curious about when we (1) remove a memory
slot, and at the meantime (2) we still care about the dirty bits of that slot.
Quoted 8 lines
I remember one case. PCIe spec defines a resizable BAR capability, allowing hardware to communicate supported resource sizes and software to set an optimal size back to hardware. If such a capability is presented to guest and the BAR is backed by memory, it's possible to observe a removal-and- add-back scenario. However in such case, the spec requires the software to clear memory space enable bit in command register before changing the BAR size. I suppose such thing should happen at boot phase where dirty bits related to old BAR size don't matter.
Good to know. Yes it makes sense to have no valid data if the driver is trying to decide the bar size at boot time.
Quoted 43 lines
Quoted 33 lines
I'll see whether I can remove the dirty bit sync in
kvm_set_phys_mem(), which I
think is really nasty.Quoted 2 lines
btw VFIO faces a similar problem when unmapping a DMA range (e.g.
when
Quoted 1 line
vIOMMU is enabled) in dirty log phase. There could be some dirty bits
which are
Quoted 2 lines
not retrieved when unmapping happens. VFIO chooses to return the dirty bits in a buffer passed in the unmapping parameters. Can memslot
interface
Quoted 1 line
do similar thing by allowing the userspace to specify a buffer pointer to
hold
Quoted 1 line
whatever dirty pages recorded for the slot that is being removed?
Yes I think we can, but may not be necessary. Actually IMHO CPU access to pages are slightly different to device DMAs in that we can do these sequence to
collect the dirty bits of a slot safely:- mark slot as READONLY
-KVM_GET_DIRTY_LOGon the slot
- finally remove the slotI guess VFIO cannot do that because there's no way to really "mark the region
as read-only" for a device because DMA could happen and DMA would fail then
when writting to a readonly slot.Quoted 6 lines
On the KVM/CPU side, after we mark the slot as READONLY then the CPU writes
will page fault and fallback to the QEMU userspace, then QEMU will take care of
the writes (so those writes could be slow but still working) then even if we mark it READONLY it won't fail the writes but just fallback to QEMU.Yes, you are right.
Quoted 1 line
Quoted 7 lines
Quoted 4 lines
Btw, since we're discussing the VFIO dirty logging across memory removal... the
unmapping DMA range you're talking about needs to be added back later, right?pure memory removal doesn't need add-back. Or are you specifically referring to removal-and-add-back case?
Oh right I get the point now - VFIO unmap is different that it does not mean the RAM is going away, but the RAM is only not accessable any more from the device, so it's actually a different scenario here comparing to real memory removals. Sorry I obviously missed that... :)
Thanks,
Quoted 14 lines
Quoted 3 lines
Then what if the device does DMA after the removal but before it's added back?
then just got IOMMU page fault, but I guess that I didn't get your real question...
Quoted 4 lines
I think this is a more general question not for dirty logging but also for when dirty logging is not enabled - I never understand how this could be fixed with existing facilities.
Thanks,
Kevin
Signature · 1 line
Peter Xu