All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v6 00/14] KVM: Dirty ring interface
@ 2020-03-09 21:44 Peter Xu
  2020-03-09 21:44 ` [PATCH v6 01/14] KVM: X86: Change parameter for fast_page_fault tracepoint Peter Xu
                   ` (13 more replies)
  0 siblings, 14 replies; 35+ messages in thread
From: Peter Xu @ 2020-03-09 21:44 UTC (permalink / raw)
  To: linux-kernel, kvm
  Cc: Yan Zhao, Jason Wang, Alex Williamson, peterx, Vitaly Kuznetsov,
	Dr . David Alan Gilbert, Christophe de Dinechin,
	Michael S . Tsirkin, Kevin Tian, Paolo Bonzini,
	Sean Christopherson

KVM branch:
  https://github.com/xzpeter/linux/tree/kvm-dirty-ring

QEMU branch for testing:
  https://github.com/xzpeter/qemu/tree/kvm-dirty-ring

v6:
- fix typo in test case (LOG_MODE_CLERA_LOG) [Dave]
- fix s390 build (again), by moving kvm_reset_dirty_gfn into
  kvm_dirty_ring.c, with some macro movements [syzbot]

For previous versions, please refer to:

V1: https://lore.kernel.org/kvm/20191129213505.18472-1-peterx@redhat.com
V2: https://lore.kernel.org/kvm/20191221014938.58831-1-peterx@redhat.com
V3: https://lore.kernel.org/kvm/20200109145729.32898-1-peterx@redhat.com
V4: https://lore.kernel.org/kvm/20200205025105.367213-1-peterx@redhat.com
V5: https://lore.kernel.org/kvm/20200304174947.69595-1-peterx@redhat.com

Overview
============

This is a continued work from Lei Cao <lei.cao@stratus.com> 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                        |  85 +--
 arch/x86/kvm/x86.c                            |  49 +-
 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                |  44 ++
 tools/testing/selftests/kvm/Makefile          |   2 -
 .../selftests/kvm/clear_dirty_log_test.c      |   2 -
 tools/testing/selftests/kvm/dirty_log_test.c  | 488 ++++++++++++++++--
 .../testing/selftests/kvm/include/kvm_util.h  |   4 +
 tools/testing/selftests/kvm/lib/kvm_util.c    |  67 +++
 .../selftests/kvm/lib/kvm_util_internal.h     |   4 +
 virt/kvm/dirty_ring.c                         | 195 +++++++
 virt/kvm/kvm_main.c                           | 164 +++++-
 22 files changed, 1393 insertions(+), 125 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

-- 
2.24.1



^ permalink raw reply	[flat|nested] 35+ messages in thread

* [PATCH v6 01/14] KVM: X86: Change parameter for fast_page_fault tracepoint
  2020-03-09 21:44 [PATCH v6 00/14] KVM: Dirty ring interface Peter Xu
@ 2020-03-09 21:44 ` Peter Xu
  2020-03-09 21:44 ` [PATCH v6 02/14] KVM: Cache as_id in kvm_memory_slot Peter Xu
                   ` (12 subsequent siblings)
  13 siblings, 0 replies; 35+ messages in thread
From: Peter Xu @ 2020-03-09 21:44 UTC (permalink / raw)
  To: linux-kernel, kvm
  Cc: Yan Zhao, Jason Wang, Alex Williamson, peterx, Vitaly Kuznetsov,
	Dr . David Alan Gilbert, Christophe de Dinechin,
	Michael S . Tsirkin, Kevin Tian, Paolo Bonzini,
	Sean Christopherson

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.

Signed-off-by: Peter Xu <peterx@redhat.com>
---
 arch/x86/kvm/mmutrace.h | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)

diff --git a/arch/x86/kvm/mmutrace.h b/arch/x86/kvm/mmutrace.h
index ffcd96fc02d0..ef523e760743 100644
--- a/arch/x86/kvm/mmutrace.h
+++ b/arch/x86/kvm/mmutrace.h
@@ -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(
-- 
2.24.1


^ permalink raw reply related	[flat|nested] 35+ messages in thread

* [PATCH v6 02/14] KVM: Cache as_id in kvm_memory_slot
  2020-03-09 21:44 [PATCH v6 00/14] KVM: Dirty ring interface Peter Xu
  2020-03-09 21:44 ` [PATCH v6 01/14] KVM: X86: Change parameter for fast_page_fault tracepoint Peter Xu
@ 2020-03-09 21:44 ` Peter Xu
  2020-03-10 14:48   ` Sean Christopherson
  2020-03-09 21:44 ` [PATCH v6 03/14] KVM: X86: Don't track dirty for KVM_SET_[TSS_ADDR|IDENTITY_MAP_ADDR] Peter Xu
                   ` (11 subsequent siblings)
  13 siblings, 1 reply; 35+ messages in thread
From: Peter Xu @ 2020-03-09 21:44 UTC (permalink / raw)
  To: linux-kernel, kvm
  Cc: Yan Zhao, Jason Wang, Alex Williamson, peterx, Vitaly Kuznetsov,
	Dr . David Alan Gilbert, Christophe de Dinechin,
	Michael S . Tsirkin, Kevin Tian, Paolo Bonzini,
	Sean Christopherson

Cache the address space ID just like the slot ID.  It will be used in
order to fill in the dirty ring entries.

Suggested-by: Paolo Bonzini <pbonzini@redhat.com>
Suggested-by: Sean Christopherson <sean.j.christopherson@intel.com>
Signed-off-by: Peter Xu <peterx@redhat.com>
---
 include/linux/kvm_host.h | 1 +
 virt/kvm/kvm_main.c      | 2 ++
 2 files changed, 3 insertions(+)

diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
index bcb9b2ac0791..afa0e9034881 100644
--- a/include/linux/kvm_host.h
+++ b/include/linux/kvm_host.h
@@ -346,6 +346,7 @@ struct kvm_memory_slot {
 	unsigned long userspace_addr;
 	u32 flags;
 	short id;
+	u8 as_id;
 };
 
 static inline unsigned long kvm_dirty_bitmap_bytes(struct kvm_memory_slot *memslot)
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 70f03ce0e5c1..e6484dabfc59 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -1036,6 +1036,8 @@ int __kvm_set_memory_region(struct kvm *kvm,
 
 	new = old = *slot;
 
+	BUILD_BUG_ON(U8_MAX < KVM_ADDRESS_SPACE_NUM);
+	new.as_id = as_id;
 	new.id = id;
 	new.base_gfn = base_gfn;
 	new.npages = npages;
-- 
2.24.1


^ permalink raw reply related	[flat|nested] 35+ messages in thread

* [PATCH v6 03/14] KVM: X86: Don't track dirty for KVM_SET_[TSS_ADDR|IDENTITY_MAP_ADDR]
  2020-03-09 21:44 [PATCH v6 00/14] KVM: Dirty ring interface Peter Xu
  2020-03-09 21:44 ` [PATCH v6 01/14] KVM: X86: Change parameter for fast_page_fault tracepoint Peter Xu
  2020-03-09 21:44 ` [PATCH v6 02/14] KVM: Cache as_id in kvm_memory_slot Peter Xu
@ 2020-03-09 21:44 ` Peter Xu
  2020-03-10 15:06   ` Sean Christopherson
  2020-03-11  1:10     ` kbuild test robot
  2020-03-09 21:44 ` [PATCH v6 04/14] KVM: Pass in kvm pointer into mark_page_dirty_in_slot() Peter Xu
                   ` (10 subsequent siblings)
  13 siblings, 2 replies; 35+ messages in thread
From: Peter Xu @ 2020-03-09 21:44 UTC (permalink / raw)
  To: linux-kernel, kvm
  Cc: Yan Zhao, Jason Wang, Alex Williamson, peterx, Vitaly Kuznetsov,
	Dr . David Alan Gilbert, Christophe de Dinechin,
	Michael S . Tsirkin, Kevin Tian, Paolo Bonzini,
	Sean Christopherson

Originally, 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.

Suggested-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Peter Xu <peterx@redhat.com>
---
 arch/x86/include/asm/kvm_host.h |  3 +-
 arch/x86/kvm/svm.c              |  9 ++--
 arch/x86/kvm/vmx/vmx.c          | 78 ++++++++++++++++-----------------
 arch/x86/kvm/x86.c              | 40 ++++++++++++++---
 4 files changed, 80 insertions(+), 50 deletions(-)

diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
index 98959e8cd448..ae7641b6a473 100644
--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -1651,7 +1651,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);
 
diff --git a/arch/x86/kvm/svm.c b/arch/x86/kvm/svm.c
index 24c0b2ba8fb9..9b325599faf2 100644
--- a/arch/x86/kvm/svm.c
+++ b/arch/x86/kvm/svm.c
@@ -1736,7 +1736,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);
 	/*
@@ -1752,13 +1753,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)
diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
index 40b1e6138cd5..fc638a164e03 100644
--- a/arch/x86/kvm/vmx/vmx.c
+++ b/arch/x86/kvm/vmx/vmx.c
@@ -3467,34 +3467,26 @@ static bool guest_state_valid(struct kvm_vcpu *vcpu)
 	return true;
 }
 
-static int init_rmode_tss(struct kvm *kvm)
+static int init_rmode_tss(struct kvm *kvm, void __user *ua)
 {
-	gfn_t fn;
+	const void *zero_page = (const void *) __va(page_to_phys(ZERO_PAGE(0)));
 	u16 data = 0;
 	int idx, r;
 
-	idx = srcu_read_lock(&kvm->srcu);
-	fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
-	r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
-	if (r < 0)
-		goto out;
+	for (idx = 0; idx < 3; idx++) {
+		r = __copy_to_user(ua + PAGE_SIZE * idx, zero_page, PAGE_SIZE);
+		if (r)
+			return -EFAULT;
+	}
+
 	data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
-	r = kvm_write_guest_page(kvm, fn++, &data,
-			TSS_IOPB_BASE_OFFSET, sizeof(u16));
-	if (r < 0)
-		goto out;
-	r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
-	if (r < 0)
-		goto out;
-	r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
-	if (r < 0)
-		goto out;
+	r = __copy_to_user(ua + TSS_IOPB_BASE_OFFSET, &data, sizeof(u16));
+	if (r)
+		return -EFAULT;
+
 	data = ~0;
-	r = kvm_write_guest_page(kvm, fn, &data,
-				 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
-				 sizeof(u8));
-out:
-	srcu_read_unlock(&kvm->srcu, idx);
+	r = __copy_to_user(ua + RMODE_TSS_SIZE - 1, &data, sizeof(u8));
+
 	return r;
 }
 
@@ -3503,6 +3495,7 @@ static int init_rmode_identity_map(struct kvm *kvm)
 	struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
 	int i, r = 0;
 	kvm_pfn_t identity_map_pfn;
+	void __user *uaddr;
 	u32 tmp;
 
 	/* Protect kvm_vmx->ept_identity_pagetable_done. */
@@ -3515,22 +3508,24 @@ static int init_rmode_identity_map(struct kvm *kvm)
 		kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
 	identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
 
-	r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
-				    kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
-	if (r < 0)
+	uaddr = __x86_set_memory_region(kvm,
+					IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
+					kvm_vmx->ept_identity_map_addr,
+					PAGE_SIZE);
+	if (IS_ERR(uaddr)) {
+		r = PTR_ERR(uaddr);
 		goto out;
+	}
 
-	r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
-	if (r < 0)
-		goto out;
 	/* Set up identity-mapping pagetable for EPT in real mode */
 	for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
 		tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
 			_PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
-		r = kvm_write_guest_page(kvm, identity_map_pfn,
-				&tmp, i * sizeof(tmp), sizeof(tmp));
-		if (r < 0)
+		r = __copy_to_user(uaddr + i * sizeof(tmp), &tmp, sizeof(tmp));
+		if (r) {
+			r = -EFAULT;
 			goto out;
+		}
 	}
 	kvm_vmx->ept_identity_pagetable_done = true;
 
@@ -3557,19 +3552,22 @@ static void seg_setup(int seg)
 static int alloc_apic_access_page(struct kvm *kvm)
 {
 	struct page *page;
-	int r = 0;
+	void __user *r;
+	int ret = 0;
 
 	mutex_lock(&kvm->slots_lock);
 	if (kvm->arch.apic_access_page_done)
 		goto out;
 	r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
 				    APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
-	if (r)
+	if (IS_ERR(r)) {
+		ret = PTR_ERR(r);
 		goto out;
+	}
 
 	page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
 	if (is_error_page(page)) {
-		r = -EFAULT;
+		ret = -EFAULT;
 		goto out;
 	}
 
@@ -3581,7 +3579,7 @@ static int alloc_apic_access_page(struct kvm *kvm)
 	kvm->arch.apic_access_page_done = true;
 out:
 	mutex_unlock(&kvm->slots_lock);
-	return r;
+	return ret;
 }
 
 int allocate_vpid(void)
@@ -4503,7 +4501,7 @@ static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
 
 static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
 {
-	int ret;
+	void __user *ret;
 
 	if (enable_unrestricted_guest)
 		return 0;
@@ -4513,10 +4511,12 @@ static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
 				      PAGE_SIZE * 3);
 	mutex_unlock(&kvm->slots_lock);
 
-	if (ret)
-		return ret;
+	if (IS_ERR(ret))
+		return PTR_ERR(ret);
+
 	to_kvm_vmx(kvm)->tss_addr = addr;
-	return init_rmode_tss(kvm);
+
+	return init_rmode_tss(kvm, ret);
 }
 
 static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 5de200663f51..fe485d4ba6c7 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -9756,7 +9756,33 @@ 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)
+/**
+ * __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:
+ *
+ *   - An error number if error happened, or,
+ *   - For installation: the HVA of the newly mapped memory slot, or,
+ *   - For uninstallation: zero if we successfully uninstall a slot.
+ *
+ * The caller should always use IS_ERR() to check the return value
+ * before use.  NOTE: KVM internal memory slots are guaranteed and
+ * won't change until the VM is destroyed. This is also true to the
+ * returned HVA when installing a new memory slot.  The HVA can be
+ * invalidated by either an errornous userspace program or a VM under
+ * destruction, however as long as we use __copy_{to|from}_user()
+ * properly upon the HVAs and handle the failure paths always then
+ * we're safe.
+ */
+void __user * __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa,
+				      u32 size)
 {
 	int i, r;
 	unsigned long hva;
@@ -9765,12 +9791,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(-EINVAL);
 
 	slot = id_to_memslot(slots, id);
 	if (size) {
 		if (slot->npages)
-			return -EEXIST;
+			return ERR_PTR(-EEXIST);
 
 		/*
 		 * MAP_SHARED to prevent internal slot pages from being moved
@@ -9779,10 +9805,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->npages)
-			return 0;
+			return ERR_PTR(0);
 
 		hva = 0;
 	}
@@ -9798,13 +9824,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(r);
 	}
 
 	if (!size)
 		vm_munmap(old.userspace_addr, old.npages * PAGE_SIZE);
 
-	return 0;
+	return (void __user *)hva;
 }
 EXPORT_SYMBOL_GPL(__x86_set_memory_region);
 
-- 
2.24.1


^ permalink raw reply related	[flat|nested] 35+ messages in thread

* [PATCH v6 04/14] KVM: Pass in kvm pointer into mark_page_dirty_in_slot()
  2020-03-09 21:44 [PATCH v6 00/14] KVM: Dirty ring interface Peter Xu
                   ` (2 preceding siblings ...)
  2020-03-09 21:44 ` [PATCH v6 03/14] KVM: X86: Don't track dirty for KVM_SET_[TSS_ADDR|IDENTITY_MAP_ADDR] Peter Xu
@ 2020-03-09 21:44 ` Peter Xu
  2020-03-09 21:44 ` [PATCH v6 05/14] KVM: X86: Implement ring-based dirty memory tracking Peter Xu
                   ` (9 subsequent siblings)
  13 siblings, 0 replies; 35+ messages in thread
From: Peter Xu @ 2020-03-09 21:44 UTC (permalink / raw)
  To: linux-kernel, kvm
  Cc: Yan Zhao, Jason Wang, Alex Williamson, peterx, Vitaly Kuznetsov,
	Dr . David Alan Gilbert, Christophe de Dinechin,
	Michael S . Tsirkin, Kevin Tian, Paolo Bonzini,
	Sean Christopherson

The context will be needed to implement the kvm dirty ring.

Signed-off-by: Peter Xu <peterx@redhat.com>
---
 virt/kvm/kvm_main.c | 33 +++++++++++++++++++--------------
 1 file changed, 19 insertions(+), 14 deletions(-)

diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index e6484dabfc59..7280417d82f0 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -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);
@@ -1915,7 +1917,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)
@@ -1940,7 +1943,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;
@@ -1954,7 +1957,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;
 }
@@ -1962,8 +1965,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);
 
@@ -2137,7 +2140,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;
@@ -2149,7 +2153,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;
 }
 
@@ -2158,7 +2162,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);
 
@@ -2167,7 +2171,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);
 
@@ -2286,7 +2290,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;
 }
@@ -2353,7 +2357,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) {
@@ -2368,7 +2373,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);
 
@@ -2377,7 +2382,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);
 
-- 
2.24.1


^ permalink raw reply related	[flat|nested] 35+ messages in thread

* [PATCH v6 05/14] KVM: X86: Implement ring-based dirty memory tracking
  2020-03-09 21:44 [PATCH v6 00/14] KVM: Dirty ring interface Peter Xu
                   ` (3 preceding siblings ...)
  2020-03-09 21:44 ` [PATCH v6 04/14] KVM: Pass in kvm pointer into mark_page_dirty_in_slot() Peter Xu
@ 2020-03-09 21:44 ` Peter Xu
  2020-03-09 22:24 ` [PATCH v6 06/14] KVM: Make dirty ring exclusive to dirty bitmap log Peter Xu
                   ` (8 subsequent siblings)
  13 siblings, 0 replies; 35+ messages in thread
From: Peter Xu @ 2020-03-09 21:44 UTC (permalink / raw)
  To: linux-kernel, kvm
  Cc: Yan Zhao, Jason Wang, Alex Williamson, peterx, Vitaly Kuznetsov,
	Dr . David Alan Gilbert, Christophe de Dinechin,
	Michael S . Tsirkin, Kevin Tian, Paolo Bonzini,
	Sean Christopherson, Lei Cao

This patch is heavily based on previous work from Lei Cao
<lei.cao@stratus.com> and Paolo Bonzini <pbonzini@redhat.com>. [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/

Signed-off-by: Lei Cao <lei.cao@stratus.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Peter Xu <peterx@redhat.com>
---
 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

diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst
index ebd383fba939..bc17ca955495 100644
--- a/Documentation/virt/kvm/api.rst
+++ b/Documentation/virt/kvm/api.rst
@@ -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
 -------------------------
@@ -6027,3 +6040,106 @@ Architectures: s390
 
 This capability indicates that the KVM_S390_NORMAL_RESET and
 KVM_S390_CLEAR_RESET ioctls are available.
+
+8.23 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.
diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
index ae7641b6a473..8495193c962c 100644
--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -1210,6 +1210,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;
@@ -1699,4 +1700,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 */
diff --git a/arch/x86/include/uapi/asm/kvm.h b/arch/x86/include/uapi/asm/kvm.h
index 3f3f780c8c65..99b15ce39e75 100644
--- a/arch/x86/include/uapi/asm/kvm.h
+++ b/arch/x86/include/uapi/asm/kvm.h
@@ -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
diff --git a/arch/x86/kvm/Makefile b/arch/x86/kvm/Makefile
index e553f0fdd87d..8e12a6fe1524 100644
--- a/arch/x86/kvm/Makefile
+++ b/arch/x86/kvm/Makefile
@@ -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 \
diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c
index 87e9ba27ada1..0147f20f31f9 100644
--- a/arch/x86/kvm/mmu/mmu.c
+++ b/arch/x86/kvm/mmu/mmu.c
@@ -1757,7 +1757,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;
 }
 
diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
index fc638a164e03..1564139d48d5 100644
--- a/arch/x86/kvm/vmx/vmx.c
+++ b/arch/x86/kvm/vmx/vmx.c
@@ -7765,6 +7765,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())
@@ -7837,6 +7838,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,
@@ -7961,6 +7967,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,
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index fe485d4ba6c7..f3cad54458dc 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -8154,6 +8154,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))) {
diff --git a/include/linux/kvm_dirty_ring.h b/include/linux/kvm_dirty_ring.h
new file mode 100644
index 000000000000..120e5e90fa1d
--- /dev/null
+++ b/include/linux/kvm_dirty_ring.h
@@ -0,0 +1,103 @@
+#ifndef KVM_DIRTY_RING_H
+#define KVM_DIRTY_RING_H
+
+#include <linux/kvm.h>
+
+/**
+ * kvm_dirty_ring: KVM internal dirty ring structure
+ *
+ * @dirty_index: free running counter that points to the next slot in
+ *               dirty_ring->dirty_gfns, where a new dirty page should go
+ * @reset_index: free running counter that points to the next dirty page
+ *               in dirty_ring->dirty_gfns for which dirty trap needs to
+ *               be reenabled
+ * @size:        size of the compact list, dirty_ring->dirty_gfns
+ * @soft_limit:  when the number of dirty pages in the list reaches this
+ *               limit, vcpu that owns this ring should exit to userspace
+ *               to allow userspace to harvest all the dirty pages
+ * @dirty_gfns:  the array to keep the dirty gfns
+ * @index:       index of this dirty ring
+ */
+struct kvm_dirty_ring {
+	u32 dirty_index;
+	u32 reset_index;
+	u32 size;
+	u32 soft_limit;
+	struct kvm_dirty_gfn *dirty_gfns;
+	int index;
+};
+
+#if (KVM_DIRTY_LOG_PAGE_OFFSET == 0)
+/*
+ * If KVM_DIRTY_LOG_PAGE_OFFSET not defined, kvm_dirty_ring.o should
+ * not be included as well, so define these nop functions for the arch.
+ */
+static inline u32 kvm_dirty_ring_get_rsvd_entries(void)
+{
+	return 0;
+}
+
+static inline int kvm_dirty_ring_alloc(struct kvm_dirty_ring *ring,
+				       int index, u32 size)
+{
+	return 0;
+}
+
+static inline struct kvm_dirty_ring *kvm_dirty_ring_get(struct kvm *kvm)
+{
+	return NULL;
+}
+
+static inline int kvm_dirty_ring_reset(struct kvm *kvm,
+				       struct kvm_dirty_ring *ring)
+{
+	return 0;
+}
+
+static inline void kvm_dirty_ring_push(struct kvm_dirty_ring *ring,
+				       u32 slot, u64 offset)
+{
+}
+
+static inline struct page *kvm_dirty_ring_get_page(struct kvm_dirty_ring *ring,
+						   u32 offset)
+{
+	return NULL;
+}
+
+static inline void kvm_dirty_ring_free(struct kvm_dirty_ring *ring)
+{
+}
+
+static inline bool kvm_dirty_ring_soft_full(struct kvm_dirty_ring *ring)
+{
+	return true;
+}
+
+#else /* KVM_DIRTY_LOG_PAGE_OFFSET == 0 */
+
+u32 kvm_dirty_ring_get_rsvd_entries(void);
+int kvm_dirty_ring_alloc(struct kvm_dirty_ring *ring, int index, u32 size);
+struct kvm_dirty_ring *kvm_dirty_ring_get(struct kvm *kvm);
+
+/*
+ * called with kvm->slots_lock held, returns the number of
+ * processed pages.
+ */
+int kvm_dirty_ring_reset(struct kvm *kvm, struct kvm_dirty_ring *ring);
+
+/*
+ * returns =0: successfully pushed
+ *         <0: unable to push, need to wait
+ */
+void kvm_dirty_ring_push(struct kvm_dirty_ring *ring, u32 slot, u64 offset);
+
+/* for use in vm_operations_struct */
+struct page *kvm_dirty_ring_get_page(struct kvm_dirty_ring *ring, u32 offset);
+
+void kvm_dirty_ring_free(struct kvm_dirty_ring *ring);
+bool kvm_dirty_ring_soft_full(struct kvm_dirty_ring *ring);
+
+#endif /* KVM_DIRTY_LOG_PAGE_OFFSET == 0 */
+
+#endif	/* KVM_DIRTY_RING_H */
diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
index afa0e9034881..f0993dec3b77 100644
--- a/include/linux/kvm_host.h
+++ b/include/linux/kvm_host.h
@@ -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)
@@ -500,6 +502,7 @@ struct kvm {
 	struct srcu_struct srcu;
 	struct srcu_struct irq_srcu;
 	pid_t userspace_pid;
+	u32 dirty_ring_size;
 };
 
 #define kvm_err(fmt, ...) \
@@ -1413,4 +1416,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
diff --git a/include/trace/events/kvm.h b/include/trace/events/kvm.h
index 2c735a3e6613..3d850997940c 100644
--- a/include/trace/events/kvm.h
+++ b/include/trace/events/kvm.h
@@ -399,6 +399,84 @@ TRACE_EVENT(kvm_halt_poll_ns,
 #define trace_kvm_halt_poll_ns_shrink(vcpu_id, new, old) \
 	trace_kvm_halt_poll_ns(false, vcpu_id, new, old)
 
+TRACE_EVENT(kvm_dirty_ring_push,
+	TP_PROTO(struct kvm_dirty_ring *ring, u32 slot, u64 offset),
+	TP_ARGS(ring, slot, offset),
+
+	TP_STRUCT__entry(
+		__field(int, index)
+		__field(u32, dirty_index)
+		__field(u32, reset_index)
+		__field(u32, slot)
+		__field(u64, offset)
+	),
+
+	TP_fast_assign(
+		__entry->index          = ring->index;
+		__entry->dirty_index    = ring->dirty_index;
+		__entry->reset_index    = ring->reset_index;
+		__entry->slot           = slot;
+		__entry->offset         = offset;
+	),
+
+	TP_printk("ring %d: dirty 0x%x reset 0x%x "
+		  "slot %u offset 0x%llx (used %u)",
+		  __entry->index, __entry->dirty_index,
+		  __entry->reset_index,  __entry->slot, __entry->offset,
+		  __entry->dirty_index - __entry->reset_index)
+);
+
+TRACE_EVENT(kvm_dirty_ring_reset,
+	TP_PROTO(struct kvm_dirty_ring *ring),
+	TP_ARGS(ring),
+
+	TP_STRUCT__entry(
+		__field(int, index)
+		__field(u32, dirty_index)
+		__field(u32, reset_index)
+	),
+
+	TP_fast_assign(
+		__entry->index          = ring->index;
+		__entry->dirty_index    = ring->dirty_index;
+		__entry->reset_index    = ring->reset_index;
+	),
+
+	TP_printk("ring %d: dirty 0x%x reset 0x%x (used %u)",
+		  __entry->index, __entry->dirty_index, __entry->reset_index,
+		  __entry->dirty_index - __entry->reset_index)
+);
+
+TRACE_EVENT(kvm_dirty_ring_waitqueue,
+	TP_PROTO(bool enter),
+	TP_ARGS(enter),
+
+	TP_STRUCT__entry(
+	    __field(bool, enter)
+	),
+
+	TP_fast_assign(
+	    __entry->enter = enter;
+	),
+
+	TP_printk("%s", __entry->enter ? "wait" : "awake")
+);
+
+TRACE_EVENT(kvm_dirty_ring_exit,
+	TP_PROTO(struct kvm_vcpu *vcpu),
+	TP_ARGS(vcpu),
+
+	TP_STRUCT__entry(
+	    __field(int, vcpu_id)
+	),
+
+	TP_fast_assign(
+	    __entry->vcpu_id = vcpu->vcpu_id;
+	),
+
+	TP_printk("vcpu %d", __entry->vcpu_id)
+);
+
 #endif /* _TRACE_KVM_MAIN_H */
 
 /* This part must be outside protection */
diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h
index 4b95f9a31a2f..cdd19f50c0af 100644
--- a/include/uapi/linux/kvm.h
+++ b/include/uapi/linux/kvm.h
@@ -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. */
@@ -1010,6 +1011,7 @@ 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_DIRTY_LOG_RING 180
 
 #ifdef KVM_CAP_IRQ_ROUTING
 
@@ -1478,6 +1480,9 @@ struct kvm_enc_region {
 #define KVM_S390_NORMAL_RESET	_IO(KVMIO,   0xc3)
 #define KVM_S390_CLEAR_RESET	_IO(KVMIO,   0xc4)
 
+/* Available with KVM_CAP_DIRTY_LOG_RING */
+#define KVM_RESET_DIRTY_RINGS     _IO(KVMIO, 0xc5)
+
 /* Secure Encrypted Virtualization command */
 enum sev_cmd_id {
 	/* Guest initialization commands */
@@ -1628,4 +1633,52 @@ struct kvm_hyperv_eventfd {
 #define KVM_HYPERV_CONN_ID_MASK		0x00ffffff
 #define KVM_HYPERV_EVENTFD_DEASSIGN	(1 << 0)
 
+/*
+ * 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 */
diff --git a/virt/kvm/dirty_ring.c b/virt/kvm/dirty_ring.c
new file mode 100644
index 000000000000..12d09802b8a7
--- /dev/null
+++ b/virt/kvm/dirty_ring.c
@@ -0,0 +1,195 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * KVM dirty ring implementation
+ *
+ * Copyright 2019 Red Hat, Inc.
+ */
+#include <linux/kvm_host.h>
+#include <linux/kvm.h>
+#include <linux/vmalloc.h>
+#include <linux/kvm_dirty_ring.h>
+#include <trace/events/kvm.h>
+
+int __weak kvm_cpu_dirty_log_size(void)
+{
+	return 0;
+}
+
+u32 kvm_dirty_ring_get_rsvd_entries(void)
+{
+	return KVM_DIRTY_RING_RSVD_ENTRIES + kvm_cpu_dirty_log_size();
+}
+
+static u32 kvm_dirty_ring_used(struct kvm_dirty_ring *ring)
+{
+	return READ_ONCE(ring->dirty_index) - READ_ONCE(ring->reset_index);
+}
+
+bool kvm_dirty_ring_soft_full(struct kvm_dirty_ring *ring)
+{
+	return kvm_dirty_ring_used(ring) >= ring->soft_limit;
+}
+
+bool kvm_dirty_ring_full(struct kvm_dirty_ring *ring)
+{
+	return kvm_dirty_ring_used(ring) >= ring->size;
+}
+
+struct kvm_dirty_ring *kvm_dirty_ring_get(struct kvm *kvm)
+{
+	struct kvm_vcpu *vcpu = kvm_get_running_vcpu();
+
+	WARN_ON_ONCE(vcpu->kvm != kvm);
+
+	return &vcpu->dirty_ring;
+}
+
+static void kvm_reset_dirty_gfn(struct kvm *kvm, u32 slot, u64 offset, u64 mask)
+{
+	struct kvm_memory_slot *memslot;
+	int as_id, id;
+
+	as_id = slot >> 16;
+	id = (u16)slot;
+	if (as_id >= KVM_ADDRESS_SPACE_NUM || id >= KVM_USER_MEM_SLOTS)
+		return;
+
+	memslot = id_to_memslot(__kvm_memslots(kvm, as_id), id);
+	if (offset >= memslot->npages)
+		return;
+
+	spin_lock(&kvm->mmu_lock);
+	kvm_arch_mmu_enable_log_dirty_pt_masked(kvm, memslot, offset, mask);
+	spin_unlock(&kvm->mmu_lock);
+}
+
+int kvm_dirty_ring_alloc(struct kvm_dirty_ring *ring, int index, u32 size)
+{
+	ring->dirty_gfns = vmalloc(size);
+	if (!ring->dirty_gfns)
+		return -ENOMEM;
+	memset(ring->dirty_gfns, 0, size);
+
+	ring->size = size / sizeof(struct kvm_dirty_gfn);
+	ring->soft_limit = ring->size - kvm_dirty_ring_get_rsvd_entries();
+	ring->dirty_index = 0;
+	ring->reset_index = 0;
+	ring->index = index;
+
+	return 0;
+}
+
+static inline void kvm_dirty_gfn_set_invalid(struct kvm_dirty_gfn *gfn)
+{
+	gfn->flags = 0;
+}
+
+static inline void kvm_dirty_gfn_set_dirtied(struct kvm_dirty_gfn *gfn)
+{
+	gfn->flags = KVM_DIRTY_GFN_F_DIRTY;
+}
+
+static inline bool kvm_dirty_gfn_invalid(struct kvm_dirty_gfn *gfn)
+{
+	return gfn->flags == 0;
+}
+
+static inline bool kvm_dirty_gfn_collected(struct kvm_dirty_gfn *gfn)
+{
+	return gfn->flags & KVM_DIRTY_GFN_F_RESET;
+}
+
+int kvm_dirty_ring_reset(struct kvm *kvm, struct kvm_dirty_ring *ring)
+{
+	u32 cur_slot, next_slot;
+	u64 cur_offset, next_offset;
+	unsigned long mask;
+	int count = 0;
+	struct kvm_dirty_gfn *entry;
+	bool first_round = true;
+
+	/* This is only needed to make compilers happy */
+	cur_slot = cur_offset = mask = 0;
+
+	while (true) {
+		entry = &ring->dirty_gfns[ring->reset_index & (ring->size - 1)];
+
+		if (!kvm_dirty_gfn_collected(entry))
+			break;
+
+		next_slot = READ_ONCE(entry->slot);
+		next_offset = READ_ONCE(entry->offset);
+
+		/* Update the flags to reflect that this GFN is reset */
+		kvm_dirty_gfn_set_invalid(entry);
+
+		ring->reset_index++;
+		count++;
+		/*
+		 * Try to coalesce the reset operations when the guest is
+		 * scanning pages in the same slot.
+		 */
+		if (!first_round && next_slot == cur_slot) {
+			s64 delta = next_offset - cur_offset;
+
+			if (delta >= 0 && delta < BITS_PER_LONG) {
+				mask |= 1ull << delta;
+				continue;
+			}
+
+			/* Backwards visit, careful about overflows!  */
+			if (delta > -BITS_PER_LONG && delta < 0 &&
+			    (mask << -delta >> -delta) == mask) {
+				cur_offset = next_offset;
+				mask = (mask << -delta) | 1;
+				continue;
+			}
+		}
+		kvm_reset_dirty_gfn(kvm, cur_slot, cur_offset, mask);
+		cur_slot = next_slot;
+		cur_offset = next_offset;
+		mask = 1;
+		first_round = false;
+	}
+
+	kvm_reset_dirty_gfn(kvm, cur_slot, cur_offset, mask);
+
+	trace_kvm_dirty_ring_reset(ring);
+
+	return count;
+}
+
+void kvm_dirty_ring_push(struct kvm_dirty_ring *ring, u32 slot, u64 offset)
+{
+	struct kvm_dirty_gfn *entry;
+
+	/* It should never get full */
+	WARN_ON_ONCE(kvm_dirty_ring_full(ring));
+
+	entry = &ring->dirty_gfns[ring->dirty_index & (ring->size - 1)];
+
+	/* It should always be an invalid entry to fill in */
+	WARN_ON_ONCE(!kvm_dirty_gfn_invalid(entry));
+
+	entry->slot = slot;
+	entry->offset = offset;
+	/*
+	 * Make sure the data is filled in before we publish this to
+	 * the userspace program.  There's no paired kernel-side reader.
+	 */
+	smp_wmb();
+	kvm_dirty_gfn_set_dirtied(entry);
+	ring->dirty_index++;
+	trace_kvm_dirty_ring_push(ring, slot, offset);
+}
+
+struct page *kvm_dirty_ring_get_page(struct kvm_dirty_ring *ring, u32 offset)
+{
+	return vmalloc_to_page((void *)ring->dirty_gfns + offset * PAGE_SIZE);
+}
+
+void kvm_dirty_ring_free(struct kvm_dirty_ring *ring)
+{
+	vfree(ring->dirty_gfns);
+	ring->dirty_gfns = NULL;
+}
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 7280417d82f0..2112958c7500 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -64,6 +64,8 @@
 #define CREATE_TRACE_POINTS
 #include <trace/events/kvm.h>
 
+#include <linux/kvm_dirty_ring.h>
+
 /* Worst case buffer size needed for holding an integer. */
 #define ITOA_MAX_LEN 12
 
@@ -360,6 +362,7 @@ static void kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
 
 void kvm_vcpu_destroy(struct kvm_vcpu *vcpu)
 {
+	kvm_dirty_ring_free(&vcpu->dirty_ring);
 	kvm_arch_vcpu_destroy(vcpu);
 
 	/*
@@ -2363,8 +2366,13 @@ static void mark_page_dirty_in_slot(struct kvm *kvm,
 {
 	if (memslot && memslot->dirty_bitmap) {
 		unsigned long rel_gfn = gfn - memslot->base_gfn;
+		u32 slot = (memslot->as_id << 16) | memslot->id;
 
-		set_bit_le(rel_gfn, memslot->dirty_bitmap);
+		if (kvm->dirty_ring_size)
+			kvm_dirty_ring_push(kvm_dirty_ring_get(kvm),
+					    slot, rel_gfn);
+		else
+			set_bit_le(rel_gfn, memslot->dirty_bitmap);
 	}
 }
 
@@ -2711,6 +2719,16 @@ void kvm_vcpu_on_spin(struct kvm_vcpu *me, bool yield_to_kernel_mode)
 }
 EXPORT_SYMBOL_GPL(kvm_vcpu_on_spin);
 
+static bool kvm_page_in_dirty_ring(struct kvm *kvm, unsigned long pgoff)
+{
+	if (!KVM_DIRTY_LOG_PAGE_OFFSET)
+		return false;
+
+	return (pgoff >= KVM_DIRTY_LOG_PAGE_OFFSET) &&
+	    (pgoff < KVM_DIRTY_LOG_PAGE_OFFSET +
+	     kvm->dirty_ring_size / PAGE_SIZE);
+}
+
 static vm_fault_t kvm_vcpu_fault(struct vm_fault *vmf)
 {
 	struct kvm_vcpu *vcpu = vmf->vma->vm_file->private_data;
@@ -2726,6 +2744,10 @@ static vm_fault_t kvm_vcpu_fault(struct vm_fault *vmf)
 	else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
 		page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
 #endif
+	else if (kvm_page_in_dirty_ring(vcpu->kvm, vmf->pgoff))
+		page = kvm_dirty_ring_get_page(
+		    &vcpu->dirty_ring,
+		    vmf->pgoff - KVM_DIRTY_LOG_PAGE_OFFSET);
 	else
 		return kvm_arch_vcpu_fault(vcpu, vmf);
 	get_page(page);
@@ -2739,6 +2761,14 @@ static const struct vm_operations_struct kvm_vcpu_vm_ops = {
 
 static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
 {
+	struct kvm_vcpu *vcpu = file->private_data;
+	unsigned long pages = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
+
+	if ((kvm_page_in_dirty_ring(vcpu->kvm, vma->vm_pgoff) ||
+	     kvm_page_in_dirty_ring(vcpu->kvm, vma->vm_pgoff + pages - 1)) &&
+	    ((vma->vm_flags & VM_EXEC) || !(vma->vm_flags & VM_SHARED)))
+		return -EINVAL;
+
 	vma->vm_ops = &kvm_vcpu_vm_ops;
 	return 0;
 }
@@ -2832,6 +2862,13 @@ static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
 	if (r)
 		goto vcpu_free_run_page;
 
+	if (kvm->dirty_ring_size) {
+		r = kvm_dirty_ring_alloc(&vcpu->dirty_ring,
+					 id, kvm->dirty_ring_size);
+		if (r)
+			goto arch_vcpu_destroy;
+	}
+
 	kvm_create_vcpu_debugfs(vcpu);
 
 	mutex_lock(&kvm->lock);
@@ -2867,6 +2904,8 @@ static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
 unlock_vcpu_destroy:
 	mutex_unlock(&kvm->lock);
 	debugfs_remove_recursive(vcpu->debugfs_dentry);
+	kvm_dirty_ring_free(&vcpu->dirty_ring);
+arch_vcpu_destroy:
 	kvm_arch_vcpu_destroy(vcpu);
 vcpu_free_run_page:
 	free_page((unsigned long)vcpu->run);
@@ -3337,12 +3376,78 @@ static long kvm_vm_ioctl_check_extension_generic(struct kvm *kvm, long arg)
 #endif
 	case KVM_CAP_NR_MEMSLOTS:
 		return KVM_USER_MEM_SLOTS;
+	case KVM_CAP_DIRTY_LOG_RING:
+#ifdef CONFIG_X86
+		return KVM_DIRTY_RING_MAX_ENTRIES * sizeof(struct kvm_dirty_gfn);
+#else
+		return 0;
+#endif
 	default:
 		break;
 	}
 	return kvm_vm_ioctl_check_extension(kvm, arg);
 }
 
+static int kvm_vm_ioctl_enable_dirty_log_ring(struct kvm *kvm, u32 size)
+{
+	int r;
+
+	if (!KVM_DIRTY_LOG_PAGE_OFFSET)
+		return -EINVAL;
+
+	/* the size should be power of 2 */
+	if (!size || (size & (size - 1)))
+		return -EINVAL;
+
+	/* Should be bigger to keep the reserved entries, or a page */
+	if (size < kvm_dirty_ring_get_rsvd_entries() *
+	    sizeof(struct kvm_dirty_gfn) || size < PAGE_SIZE)
+		return -EINVAL;
+
+	if (size > KVM_DIRTY_RING_MAX_ENTRIES *
+	    sizeof(struct kvm_dirty_gfn))
+		return -E2BIG;
+
+	/* We only allow it to set once */
+	if (kvm->dirty_ring_size)
+		return -EINVAL;
+
+	mutex_lock(&kvm->lock);
+
+	if (kvm->created_vcpus) {
+		/* We don't allow to change this value after vcpu created */
+		r = -EINVAL;
+	} else {
+		kvm->dirty_ring_size = size;
+		r = 0;
+	}
+
+	mutex_unlock(&kvm->lock);
+	return r;
+}
+
+static int kvm_vm_ioctl_reset_dirty_pages(struct kvm *kvm)
+{
+	int i;
+	struct kvm_vcpu *vcpu;
+	int cleared = 0;
+
+	if (!kvm->dirty_ring_size)
+		return -EINVAL;
+
+	mutex_lock(&kvm->slots_lock);
+
+	kvm_for_each_vcpu(i, vcpu, kvm)
+		cleared += kvm_dirty_ring_reset(vcpu->kvm, &vcpu->dirty_ring);
+
+	mutex_unlock(&kvm->slots_lock);
+
+	if (cleared)
+		kvm_flush_remote_tlbs(kvm);
+
+	return cleared;
+}
+
 int __attribute__((weak)) kvm_vm_ioctl_enable_cap(struct kvm *kvm,
 						  struct kvm_enable_cap *cap)
 {
@@ -3360,6 +3465,8 @@ static int kvm_vm_ioctl_enable_cap_generic(struct kvm *kvm,
 		kvm->manual_dirty_log_protect = cap->args[0];
 		return 0;
 #endif
+	case KVM_CAP_DIRTY_LOG_RING:
+		return kvm_vm_ioctl_enable_dirty_log_ring(kvm, cap->args[0]);
 	default:
 		return kvm_vm_ioctl_enable_cap(kvm, cap);
 	}
@@ -3547,6 +3654,9 @@ static long kvm_vm_ioctl(struct file *filp,
 	case KVM_CHECK_EXTENSION:
 		r = kvm_vm_ioctl_check_extension_generic(kvm, arg);
 		break;
+	case KVM_RESET_DIRTY_RINGS:
+		r = kvm_vm_ioctl_reset_dirty_pages(kvm);
+		break;
 	default:
 		r = kvm_arch_vm_ioctl(filp, ioctl, arg);
 	}
-- 
2.24.1


^ permalink raw reply related	[flat|nested] 35+ messages in thread

* [PATCH v6 06/14] KVM: Make dirty ring exclusive to dirty bitmap log
  2020-03-09 21:44 [PATCH v6 00/14] KVM: Dirty ring interface Peter Xu
                   ` (4 preceding siblings ...)
  2020-03-09 21:44 ` [PATCH v6 05/14] KVM: X86: Implement ring-based dirty memory tracking Peter Xu
@ 2020-03-09 22:24 ` Peter Xu
  2020-03-09 22:25 ` [PATCH v6 07/14] KVM: Don't allocate dirty bitmap if dirty ring is enabled Peter Xu
                   ` (7 subsequent siblings)
  13 siblings, 0 replies; 35+ messages in thread
From: Peter Xu @ 2020-03-09 22:24 UTC (permalink / raw)
  To: linux-kernel, kvm
  Cc: Peter Xu, Yan Zhao, Jason Wang, Alex Williamson,
	Vitaly Kuznetsov, Dr . David Alan Gilbert,
	Christophe de Dinechin, Michael S . Tsirkin, Kevin Tian,
	Paolo Bonzini, Sean Christopherson

There'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.

Signed-off-by: Peter Xu <peterx@redhat.com>
---
 Documentation/virt/kvm/api.rst |  7 +++++++
 virt/kvm/kvm_main.c            | 12 ++++++++++++
 2 files changed, 19 insertions(+)

diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst
index bc17ca955495..55979d5095aa 100644
--- a/Documentation/virt/kvm/api.rst
+++ b/Documentation/virt/kvm/api.rst
@@ -6143,3 +6143,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 KVM_CAP_DIRTY_LOG_RING capability and the new ioctl
+KVM_RESET_DIRTY_RINGS are exclusive to the existing KVM_GET_DIRTY_LOG
+interface.  After enabling KVM_CAP_DIRTY_LOG_RING with an acceptable
+dirty ring size, the virtual machine will switch to the dirty ring
+tracking mode, and KVM_GET_DIRTY_LOG, KVM_CLEAR_DIRTY_LOG ioctls will
+stop working.
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 2112958c7500..e51a14d24619 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -1191,6 +1191,10 @@ int kvm_get_dirty_log(struct kvm *kvm,
 	unsigned long n;
 	unsigned long any = 0;
 
+	/* 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)
@@ -1248,6 +1252,10 @@ int kvm_get_dirty_log_protect(struct kvm *kvm,
 	unsigned long *dirty_bitmap;
 	unsigned long *dirty_bitmap_buffer;
 
+	/* 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)
@@ -1319,6 +1327,10 @@ int kvm_clear_dirty_log_protect(struct kvm *kvm,
 	unsigned long *dirty_bitmap;
 	unsigned long *dirty_bitmap_buffer;
 
+	/* 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)
-- 
2.24.1


^ permalink raw reply related	[flat|nested] 35+ messages in thread

* [PATCH v6 07/14] KVM: Don't allocate dirty bitmap if dirty ring is enabled
  2020-03-09 21:44 [PATCH v6 00/14] KVM: Dirty ring interface Peter Xu
                   ` (5 preceding siblings ...)
  2020-03-09 22:24 ` [PATCH v6 06/14] KVM: Make dirty ring exclusive to dirty bitmap log Peter Xu
@ 2020-03-09 22:25 ` Peter Xu
  2020-03-09 22:25 ` [PATCH v6 08/14] KVM: selftests: Always clear dirty bitmap after iteration Peter Xu
                   ` (6 subsequent siblings)
  13 siblings, 0 replies; 35+ messages in thread
From: Peter Xu @ 2020-03-09 22:25 UTC (permalink / raw)
  To: linux-kernel, kvm
  Cc: Peter Xu, Yan Zhao, Jason Wang, Alex Williamson,
	Vitaly Kuznetsov, Dr . David Alan Gilbert,
	Christophe de Dinechin, Michael S . Tsirkin, Kevin Tian,
	Paolo Bonzini, Sean Christopherson

Because 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).

Signed-off-by: Peter Xu <peterx@redhat.com>
---
 arch/x86/kvm/mmu/mmu.c   | 4 ++--
 include/linux/kvm_host.h | 5 +++++
 virt/kvm/kvm_main.c      | 5 +++--
 3 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c
index 0147f20f31f9..d2c6bd27053f 100644
--- a/arch/x86/kvm/mmu/mmu.c
+++ b/arch/x86/kvm/mmu/mmu.c
@@ -1284,8 +1284,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;
 }
diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
index f0993dec3b77..711ed191f663 100644
--- a/include/linux/kvm_host.h
+++ b/include/linux/kvm_host.h
@@ -351,6 +351,11 @@ struct kvm_memory_slot {
 	u8 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;
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index e51a14d24619..1839d5c15a0f 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -1100,7 +1100,8 @@ int __kvm_set_memory_region(struct kvm *kvm,
 	}
 
 	/* Allocate page dirty bitmap if needed */
-	if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
+	if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap &&
+	    !kvm->dirty_ring_size) {
 		if (kvm_create_dirty_bitmap(&new) < 0)
 			goto out_free;
 	}
@@ -2376,7 +2377,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;
 
-- 
2.24.1


^ permalink raw reply related	[flat|nested] 35+ messages in thread

* [PATCH v6 08/14] KVM: selftests: Always clear dirty bitmap after iteration
  2020-03-09 21:44 [PATCH v6 00/14] KVM: Dirty ring interface Peter Xu
                   ` (6 preceding siblings ...)
  2020-03-09 22:25 ` [PATCH v6 07/14] KVM: Don't allocate dirty bitmap if dirty ring is enabled Peter Xu
@ 2020-03-09 22:25 ` Peter Xu
  2020-03-09 22:25 ` [PATCH v6 09/14] KVM: selftests: Sync uapi/linux/kvm.h to tools/ Peter Xu
                   ` (5 subsequent siblings)
  13 siblings, 0 replies; 35+ messages in thread
From: Peter Xu @ 2020-03-09 22:25 UTC (permalink / raw)
  To: linux-kernel, kvm
  Cc: Peter Xu, Yan Zhao, Jason Wang, Alex Williamson,
	Vitaly Kuznetsov, Dr . David Alan Gilbert,
	Christophe de Dinechin, Michael S . Tsirkin, Kevin Tian,
	Paolo Bonzini, Sean Christopherson

We 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.

Signed-off-by: Peter Xu <peterx@redhat.com>
---
 tools/testing/selftests/kvm/dirty_log_test.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/testing/selftests/kvm/dirty_log_test.c b/tools/testing/selftests/kvm/dirty_log_test.c
index 5614222a6628..3c0ffd34b3b0 100644
--- a/tools/testing/selftests/kvm/dirty_log_test.c
+++ b/tools/testing/selftests/kvm/dirty_log_test.c
@@ -197,7 +197,7 @@ static void vm_dirty_log_verify(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
-- 
2.24.1


^ permalink raw reply related	[flat|nested] 35+ messages in thread

* [PATCH v6 09/14] KVM: selftests: Sync uapi/linux/kvm.h to tools/
  2020-03-09 21:44 [PATCH v6 00/14] KVM: Dirty ring interface Peter Xu
                   ` (7 preceding siblings ...)
  2020-03-09 22:25 ` [PATCH v6 08/14] KVM: selftests: Always clear dirty bitmap after iteration Peter Xu
@ 2020-03-09 22:25 ` Peter Xu
  2020-03-09 22:25 ` [PATCH v6 10/14] KVM: selftests: Use a single binary for dirty/clear log test Peter Xu
                   ` (4 subsequent siblings)
  13 siblings, 0 replies; 35+ messages in thread
From: Peter Xu @ 2020-03-09 22:25 UTC (permalink / raw)
  To: linux-kernel, kvm
  Cc: Peter Xu, Yan Zhao, Jason Wang, Alex Williamson,
	Vitaly Kuznetsov, Dr . David Alan Gilbert,
	Christophe de Dinechin, Michael S . Tsirkin, Kevin Tian,
	Paolo Bonzini, Sean Christopherson

This will be needed to extend the kvm selftest program.

Signed-off-by: Peter Xu <peterx@redhat.com>
---
 tools/include/uapi/linux/kvm.h | 44 ++++++++++++++++++++++++++++++++++
 1 file changed, 44 insertions(+)

diff --git a/tools/include/uapi/linux/kvm.h b/tools/include/uapi/linux/kvm.h
index 4b95f9a31a2f..50bf39d24f17 100644
--- a/tools/include/uapi/linux/kvm.h
+++ b/tools/include/uapi/linux/kvm.h
@@ -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. */
@@ -1010,6 +1011,7 @@ 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_DIRTY_LOG_RING 180
 
 #ifdef KVM_CAP_IRQ_ROUTING
 
@@ -1478,6 +1480,9 @@ struct kvm_enc_region {
 #define KVM_S390_NORMAL_RESET	_IO(KVMIO,   0xc3)
 #define KVM_S390_CLEAR_RESET	_IO(KVMIO,   0xc4)
 
+/* Available with KVM_CAP_DIRTY_LOG_RING */
+#define KVM_RESET_DIRTY_RINGS     _IO(KVMIO, 0xc5)
+
 /* Secure Encrypted Virtualization command */
 enum sev_cmd_id {
 	/* Guest initialization commands */
@@ -1628,4 +1633,43 @@ struct kvm_hyperv_eventfd {
 #define KVM_HYPERV_CONN_ID_MASK		0x00ffffff
 #define KVM_HYPERV_EVENTFD_DEASSIGN	(1 << 0)
 
+/*
+ * 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 */
-- 
2.24.1


^ permalink raw reply related	[flat|nested] 35+ messages in thread

* [PATCH v6 10/14] KVM: selftests: Use a single binary for dirty/clear log test
  2020-03-09 21:44 [PATCH v6 00/14] KVM: Dirty ring interface Peter Xu
                   ` (8 preceding siblings ...)
  2020-03-09 22:25 ` [PATCH v6 09/14] KVM: selftests: Sync uapi/linux/kvm.h to tools/ Peter Xu
@ 2020-03-09 22:25 ` Peter Xu
  2020-03-10  8:10   ` Andrew Jones
  2020-03-09 22:25 ` [PATCH v6 11/14] KVM: selftests: Introduce after_vcpu_run hook for dirty " Peter Xu
                   ` (3 subsequent siblings)
  13 siblings, 1 reply; 35+ messages in thread
From: Peter Xu @ 2020-03-09 22:25 UTC (permalink / raw)
  To: linux-kernel, kvm
  Cc: Peter Xu, Yan Zhao, Jason Wang, Alex Williamson,
	Vitaly Kuznetsov, Dr . David Alan Gilbert,
	Christophe de Dinechin, Michael S . Tsirkin, Kevin Tian,
	Paolo Bonzini, Sean Christopherson

Remove 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.

Signed-off-by: Peter Xu <peterx@redhat.com>
---
 tools/testing/selftests/kvm/Makefile          |   2 -
 .../selftests/kvm/clear_dirty_log_test.c      |   2 -
 tools/testing/selftests/kvm/dirty_log_test.c  | 169 +++++++++++++++---
 3 files changed, 146 insertions(+), 27 deletions(-)
 delete mode 100644 tools/testing/selftests/kvm/clear_dirty_log_test.c

diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile
index d91c53b726e6..941bfcd48eaa 100644
--- a/tools/testing/selftests/kvm/Makefile
+++ b/tools/testing/selftests/kvm/Makefile
@@ -27,11 +27,9 @@ 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 += x86_64/svm_vmcall_test
-TEST_GEN_PROGS_x86_64 += clear_dirty_log_test
 TEST_GEN_PROGS_x86_64 += dirty_log_test
 TEST_GEN_PROGS_x86_64 += kvm_create_max_vcpus
 
-TEST_GEN_PROGS_aarch64 += clear_dirty_log_test
 TEST_GEN_PROGS_aarch64 += dirty_log_test
 TEST_GEN_PROGS_aarch64 += kvm_create_max_vcpus
 
diff --git a/tools/testing/selftests/kvm/clear_dirty_log_test.c b/tools/testing/selftests/kvm/clear_dirty_log_test.c
deleted file mode 100644
index 749336937d37..000000000000
--- a/tools/testing/selftests/kvm/clear_dirty_log_test.c
+++ /dev/null
@@ -1,2 +0,0 @@
-#define USE_CLEAR_DIRTY_LOG
-#include "dirty_log_test.c"
diff --git a/tools/testing/selftests/kvm/dirty_log_test.c b/tools/testing/selftests/kvm/dirty_log_test.c
index 3c0ffd34b3b0..642886394e34 100644
--- a/tools/testing/selftests/kvm/dirty_log_test.c
+++ b/tools/testing/selftests/kvm/dirty_log_test.c
@@ -128,6 +128,73 @@ static uint64_t host_dirty_count;
 static uint64_t host_clear_count;
 static uint64_t host_track_next_count;
 
+enum log_mode_t {
+	/* Only use KVM_GET_DIRTY_LOG for logging */
+	LOG_MODE_DIRTY_LOG = 0,
+
+	/* Use both KVM_[GET|CLEAR]_DIRTY_LOG for logging */
+	LOG_MODE_CLEAR_LOG = 1,
+
+	LOG_MODE_NUM,
+
+	/* Run all supported modes */
+	LOG_MODE_ALL = LOG_MODE_NUM,
+};
+
+/* Mode of logging to test.  Default is to run all supported modes */
+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 bool clear_log_supported(void)
+{
+	return kvm_check_cap(KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2);
+}
+
+static void clear_log_create_vm_done(struct kvm_vm *vm)
+{
+	struct kvm_enable_cap cap = {};
+
+	cap.cap = KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2;
+	cap.args[0] = 1;
+	vm_enable_cap(vm, &cap);
+}
+
+static void dirty_log_collect_dirty_pages(struct kvm_vm *vm, int slot,
+					  void *bitmap, uint32_t num_pages)
+{
+	kvm_vm_get_dirty_log(vm, slot, bitmap);
+}
+
+static void clear_log_collect_dirty_pages(struct kvm_vm *vm, int slot,
+					  void *bitmap, uint32_t num_pages)
+{
+	kvm_vm_get_dirty_log(vm, slot, bitmap);
+	kvm_vm_clear_dirty_log(vm, slot, bitmap, 0, num_pages);
+}
+
+struct log_mode {
+	const char *name;
+	/* Return true if this mode is supported, otherwise false */
+	bool (*supported)(void);
+	/* Hook when the vm creation is done (before vcpu creation) */
+	void (*create_vm_done)(struct kvm_vm *vm);
+	/* 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);
+} log_modes[LOG_MODE_NUM] = {
+	{
+		.name = "dirty-log",
+		.collect_dirty_pages = dirty_log_collect_dirty_pages,
+	},
+	{
+		.name = "clear-log",
+		.supported = clear_log_supported,
+		.create_vm_done = clear_log_create_vm_done,
+		.collect_dirty_pages = clear_log_collect_dirty_pages,
+	},
+};
+
 /*
  * We use this bitmap to track some pages that should have its dirty
  * bit set in the _next_ iteration.  For example, if we detected the
@@ -137,6 +204,43 @@ static uint64_t host_track_next_count;
  */
 static unsigned long *host_bmap_track;
 
+static void log_modes_dump(void)
+{
+	int i;
+
+	for (i = 0; i < LOG_MODE_NUM; i++)
+		printf("%s, ", log_modes[i].name);
+	puts("\b\b  \b\b");
+}
+
+static bool log_mode_supported(void)
+{
+	struct log_mode *mode = &log_modes[host_log_mode];
+
+	if (mode->supported)
+		return mode->supported();
+
+	return true;
+}
+
+static void log_mode_create_vm_done(struct kvm_vm *vm)
+{
+	struct log_mode *mode = &log_modes[host_log_mode];
+
+	if (mode->create_vm_done)
+		mode->create_vm_done(vm);
+}
+
+static void log_mode_collect_dirty_pages(struct kvm_vm *vm, int slot,
+					 void *bitmap, uint32_t num_pages)
+{
+	struct log_mode *mode = &log_modes[host_log_mode];
+
+	TEST_ASSERT(mode->collect_dirty_pages != NULL,
+		    "collect_dirty_pages() is required for any log mode!");
+	mode->collect_dirty_pages(vm, slot, bitmap, num_pages);
+}
+
 static void generate_random_array(uint64_t *guest_array, uint64_t size)
 {
 	uint64_t i;
@@ -257,6 +361,7 @@ static struct kvm_vm *create_vm(enum vm_guest_mode mode, uint32_t vcpuid,
 #ifdef __x86_64__
 	vm_create_irqchip(vm);
 #endif
+	log_mode_create_vm_done(vm);
 	vm_vcpu_add_default(vm, vcpuid, guest_code);
 	return vm;
 }
@@ -271,6 +376,12 @@ static void run_test(enum vm_guest_mode mode, unsigned long iterations,
 	struct kvm_vm *vm;
 	unsigned long *bmap;
 
+	if (!log_mode_supported()) {
+		fprintf(stderr, "Log mode '%s' not supported, skip\n",
+			log_modes[host_log_mode].name);
+		return;
+	}
+
 	/*
 	 * We reserve page table for 2 times of extra dirty mem which
 	 * will definitely cover the original (1G+) test range.  Here
@@ -316,14 +427,6 @@ static void run_test(enum vm_guest_mode mode, unsigned long iterations,
 	bmap = bitmap_alloc(host_num_pages);
 	host_bmap_track = bitmap_alloc(host_num_pages);
 
-#ifdef USE_CLEAR_DIRTY_LOG
-	struct kvm_enable_cap cap = {};
-
-	cap.cap = KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2;
-	cap.args[0] = 1;
-	vm_enable_cap(vm, &cap);
-#endif
-
 	/* Add an extra memory slot for testing dirty logging */
 	vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS,
 				    guest_test_phys_mem,
@@ -364,11 +467,8 @@ static void run_test(enum vm_guest_mode mode, unsigned long iterations,
 	while (iteration < iterations) {
 		/* Give the vcpu thread some time to dirty some pages */
 		usleep(interval * 1000);
-		kvm_vm_get_dirty_log(vm, TEST_MEM_SLOT_INDEX, bmap);
-#ifdef USE_CLEAR_DIRTY_LOG
-		kvm_vm_clear_dirty_log(vm, TEST_MEM_SLOT_INDEX, bmap, 0,
-				       host_num_pages);
-#endif
+		log_mode_collect_dirty_pages(vm, TEST_MEM_SLOT_INDEX,
+					     bmap, host_num_pages);
 		vm_dirty_log_verify(bmap);
 		iteration++;
 		sync_global_to_guest(vm, iteration);
@@ -413,6 +513,9 @@ static void help(char *name)
 	       TEST_HOST_LOOP_INTERVAL);
 	printf(" -p: specify guest physical test memory offset\n"
 	       "     Warning: a low offset can conflict with the loaded test code.\n");
+	printf(" -M: specify the host logging mode "
+	       "(default: run all log modes).  Supported modes: \n\t");
+	log_modes_dump();
 	printf(" -m: specify the guest mode ID to test "
 	       "(default: test all supported modes)\n"
 	       "     This option may be used multiple times.\n"
@@ -432,18 +535,11 @@ int main(int argc, char *argv[])
 	bool mode_selected = false;
 	uint64_t phys_offset = 0;
 	unsigned int mode;
-	int opt, i;
+	int opt, i, j;
 #ifdef __aarch64__
 	unsigned int host_ipa_limit;
 #endif
 
-#ifdef USE_CLEAR_DIRTY_LOG
-	if (!kvm_check_cap(KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2)) {
-		fprintf(stderr, "KVM_CLEAR_DIRTY_LOG not available, skipping tests\n");
-		exit(KSFT_SKIP);
-	}
-#endif
-
 #ifdef __x86_64__
 	vm_guest_mode_params_init(VM_MODE_PXXV48_4K, true, true);
 #endif
@@ -463,7 +559,7 @@ int main(int argc, char *argv[])
 	vm_guest_mode_params_init(VM_MODE_P40V48_4K, true, true);
 #endif
 
-	while ((opt = getopt(argc, argv, "hi:I:p:m:")) != -1) {
+	while ((opt = getopt(argc, argv, "hi:I:p:m:M:")) != -1) {
 		switch (opt) {
 		case 'i':
 			iterations = strtol(optarg, NULL, 10);
@@ -485,6 +581,22 @@ int main(int argc, char *argv[])
 				    "Guest mode ID %d too big", mode);
 			vm_guest_mode_params[mode].enabled = true;
 			break;
+		case 'M':
+			for (i = 0; i < LOG_MODE_NUM; i++) {
+				if (!strcmp(optarg, log_modes[i].name)) {
+					DEBUG("Setting log mode to: '%s'\n",
+					      optarg);
+					host_log_mode_option = i;
+					break;
+				}
+			}
+			if (i == LOG_MODE_NUM) {
+				printf("Log mode '%s' is invalid.  "
+				       "Please choose from: ", optarg);
+				log_modes_dump();
+				exit(-1);
+			}
+			break;
 		case 'h':
 		default:
 			help(argv[0]);
@@ -506,7 +618,18 @@ int main(int argc, char *argv[])
 		TEST_ASSERT(vm_guest_mode_params[i].supported,
 			    "Guest mode ID %d (%s) not supported.",
 			    i, vm_guest_mode_string(i));
-		run_test(i, iterations, interval, phys_offset);
+		if (host_log_mode_option == LOG_MODE_ALL) {
+			/* Run each log mode */
+			for (j = 0; j < LOG_MODE_NUM; j++) {
+				DEBUG("Testing Log Mode '%s'\n",
+				      log_modes[j].name);
+				host_log_mode = j;
+				run_test(i, iterations, interval, phys_offset);
+			}
+		} else {
+			host_log_mode = host_log_mode_option;
+			run_test(i, iterations, interval, phys_offset);
+		}
 	}
 
 	return 0;
-- 
2.24.1


^ permalink raw reply related	[flat|nested] 35+ messages in thread

* [PATCH v6 11/14] KVM: selftests: Introduce after_vcpu_run hook for dirty log test
  2020-03-09 21:44 [PATCH v6 00/14] KVM: Dirty ring interface Peter Xu
                   ` (9 preceding siblings ...)
  2020-03-09 22:25 ` [PATCH v6 10/14] KVM: selftests: Use a single binary for dirty/clear log test Peter Xu
@ 2020-03-09 22:25 ` Peter Xu
  2020-03-09 22:25 ` [PATCH v6 12/14] KVM: selftests: Add dirty ring buffer test Peter Xu
                   ` (2 subsequent siblings)
  13 siblings, 0 replies; 35+ messages in thread
From: Peter Xu @ 2020-03-09 22:25 UTC (permalink / raw)
  To: linux-kernel, kvm
  Cc: Peter Xu, Yan Zhao, Jason Wang, Alex Williamson,
	Vitaly Kuznetsov, Dr . David Alan Gilbert,
	Christophe de Dinechin, Michael S . Tsirkin, Kevin Tian,
	Paolo Bonzini, Sean Christopherson

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_count because after all we have a better
summary right now with statistics, and clean it up a bit.

Signed-off-by: Peter Xu <peterx@redhat.com>
---
 tools/testing/selftests/kvm/dirty_log_test.c | 39 ++++++++++++--------
 1 file changed, 23 insertions(+), 16 deletions(-)

diff --git a/tools/testing/selftests/kvm/dirty_log_test.c b/tools/testing/selftests/kvm/dirty_log_test.c
index 642886394e34..b6fb4f86032c 100644
--- a/tools/testing/selftests/kvm/dirty_log_test.c
+++ b/tools/testing/selftests/kvm/dirty_log_test.c
@@ -173,6 +173,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 */
@@ -182,16 +191,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,
 	},
 };
 
@@ -241,6 +254,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;
@@ -254,31 +275,17 @@ static void *vcpu_worker(void *data)
 	int ret;
 	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);
 		/* 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_ASSERT(false,
-				    "Invalid guest sync status: "
-				    "exit_reason=%s\n",
-				    exit_reason_str(run->exit_reason));
-		}
+		log_mode_after_vcpu_run(vm);
 	}
 
-	DEBUG("Dirtied %"PRIu64" pages\n", pages_count);
-
 	return NULL;
 }
 
-- 
2.24.1


^ permalink raw reply related	[flat|nested] 35+ messages in thread

* [PATCH v6 12/14] KVM: selftests: Add dirty ring buffer test
  2020-03-09 21:44 [PATCH v6 00/14] KVM: Dirty ring interface Peter Xu
                   ` (10 preceding siblings ...)
  2020-03-09 22:25 ` [PATCH v6 11/14] KVM: selftests: Introduce after_vcpu_run hook for dirty " Peter Xu
@ 2020-03-09 22:25 ` Peter Xu
  2020-03-10  8:18   ` Andrew Jones
  2020-03-09 22:25 ` [PATCH v6 13/14] KVM: selftests: Let dirty_log_test async for dirty ring test Peter Xu
  2020-03-09 22:25 ` [PATCH v6 14/14] KVM: selftests: Add "-c" parameter to dirty log test Peter Xu
  13 siblings, 1 reply; 35+ messages in thread
From: Peter Xu @ 2020-03-09 22:25 UTC (permalink / raw)
  To: linux-kernel, kvm
  Cc: Peter Xu, Yan Zhao, Jason Wang, Alex Williamson,
	Vitaly Kuznetsov, Dr . David Alan Gilbert,
	Christophe de Dinechin, Michael S . Tsirkin, Kevin Tian,
	Paolo Bonzini, Sean Christopherson

Add 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.

Signed-off-by: Peter Xu <peterx@redhat.com>
---
 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    |  58 +++++
 .../selftests/kvm/lib/kvm_util_internal.h     |   4 +
 4 files changed, 264 insertions(+), 2 deletions(-)

diff --git a/tools/testing/selftests/kvm/dirty_log_test.c b/tools/testing/selftests/kvm/dirty_log_test.c
index b6fb4f86032c..134637267af4 100644
--- a/tools/testing/selftests/kvm/dirty_log_test.c
+++ b/tools/testing/selftests/kvm/dirty_log_test.c
@@ -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 */
@@ -182,6 +193,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%llx", cur->offset, num_pages);
+		DEBUG("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);
+
+	DEBUG("Notifying vcpu to continue\n");
+	sem_post(&dirty_ring_vcpu_cont);
+
+	DEBUG("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);
+		DEBUG("vcpu stops because dirty ring full...\n");
+		sem_wait(&dirty_ring_vcpu_cont);
+		DEBUG("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 */
@@ -193,6 +318,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",
@@ -206,6 +332,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,
+	},
 };
 
 /*
@@ -262,6 +396,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;
@@ -309,14 +451,65 @@ static void vm_dirty_log_verify(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);
@@ -483,6 +676,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);
 
 	DEBUG("Total bits checked: dirty (%"PRIu64"), clear (%"PRIu64"), "
@@ -547,6 +741,9 @@ int main(int argc, char *argv[])
 	unsigned int host_ipa_limit;
 #endif
 
+	sem_init(&dirty_ring_vcpu_stop, 0, 0);
+	sem_init(&dirty_ring_vcpu_cont, 0, 0);
+
 #ifdef __x86_64__
 	vm_guest_mode_params_init(VM_MODE_PXXV48_4K, true, true);
 #endif
diff --git a/tools/testing/selftests/kvm/include/kvm_util.h b/tools/testing/selftests/kvm/include/kvm_util.h
index ae0d14c2540a..6cdb3e62c6fb 100644
--- a/tools/testing/selftests/kvm/include/kvm_util.h
+++ b/tools/testing/selftests/kvm/include/kvm_util.h
@@ -67,6 +67,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);
@@ -76,6 +77,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);
@@ -143,6 +145,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);
 
diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c
index a6dd0401eb50..9be96b676f35 100644
--- a/tools/testing/selftests/kvm/lib/kvm_util.c
+++ b/tools/testing/selftests/kvm/lib/kvm_util.c
@@ -85,6 +85,16 @@ int vm_enable_cap(struct kvm_vm *vm, struct kvm_enable_cap *cap)
 	return ret;
 }
 
+void vm_enable_dirty_ring(struct kvm_vm *vm, uint32_t ring_size)
+{
+	struct kvm_enable_cap cap = { 0 };
+
+	cap.cap = KVM_CAP_DIRTY_LOG_RING;
+	cap.args[0] = ring_size;
+	vm_enable_cap(vm, &cap);
+	vm->dirty_ring_size = ring_size;
+}
+
 static void vm_open(struct kvm_vm *vm, int perm)
 {
 	vm->kvm_fd = open(KVM_DEV_PATH, perm);
@@ -297,6 +307,11 @@ void kvm_vm_clear_dirty_log(struct kvm_vm *vm, int slot, void *log,
 		    strerror(-ret));
 }
 
+uint32_t kvm_vm_reset_dirty_ring(struct kvm_vm *vm)
+{
+	return ioctl(vm->fd, KVM_RESET_DIRTY_RINGS);
+}
+
 /*
  * Userspace Memory Region Find
  *
@@ -408,6 +423,13 @@ static void vm_vcpu_rm(struct kvm_vm *vm, uint32_t vcpuid)
 	struct vcpu *vcpu = vcpu_find(vm, vcpuid);
 	int ret;
 
+	if (vcpu->dirty_gfns) {
+		ret = munmap(vcpu->dirty_gfns, vm->dirty_ring_size);
+		TEST_ASSERT(ret == 0, "munmap of VCPU dirty ring failed, "
+			    "rc: %i errno: %i", ret, errno);
+		vcpu->dirty_gfns = NULL;
+	}
+
 	ret = munmap(vcpu->state, sizeof(*vcpu->state));
 	TEST_ASSERT(ret == 0, "munmap of VCPU fd failed, rc: %i "
 		"errno: %i", ret, errno);
@@ -1445,6 +1467,41 @@ int _vcpu_ioctl(struct kvm_vm *vm, uint32_t vcpuid,
 	return ret;
 }
 
+void *vcpu_map_dirty_ring(struct kvm_vm *vm, uint32_t vcpuid)
+{
+	struct vcpu *vcpu;
+	uint32_t size = vm->dirty_ring_size;
+
+	TEST_ASSERT(size > 0, "Should enable dirty ring first");
+
+	vcpu = vcpu_find(vm, vcpuid);
+
+	TEST_ASSERT(vcpu, "Cannot find vcpu %u", vcpuid);
+
+	if (!vcpu->dirty_gfns) {
+		void *addr;
+
+		addr = mmap(NULL, size, PROT_READ,
+			    MAP_PRIVATE, vcpu->fd,
+			    vm->page_size * KVM_DIRTY_LOG_PAGE_OFFSET);
+		TEST_ASSERT(addr == MAP_FAILED, "Dirty ring mapped private");
+
+		addr = mmap(NULL, size, PROT_READ | PROT_EXEC,
+			    MAP_PRIVATE, vcpu->fd,
+			    vm->page_size * KVM_DIRTY_LOG_PAGE_OFFSET);
+		TEST_ASSERT(addr == MAP_FAILED, "Dirty ring mapped exec");
+
+		addr = mmap(NULL, size, PROT_READ | PROT_WRITE,
+			    MAP_SHARED, vcpu->fd,
+			    vm->page_size * KVM_DIRTY_LOG_PAGE_OFFSET);
+
+		vcpu->dirty_gfns = addr;
+		vcpu->dirty_gfns_count = size / sizeof(struct kvm_dirty_gfn);
+	}
+
+	return vcpu->dirty_gfns;
+}
+
 /*
  * VM Ioctl
  *
@@ -1539,6 +1596,7 @@ static struct exit_reason {
 	{KVM_EXIT_INTERNAL_ERROR, "INTERNAL_ERROR"},
 	{KVM_EXIT_OSI, "OSI"},
 	{KVM_EXIT_PAPR_HCALL, "PAPR_HCALL"},
+	{KVM_EXIT_DIRTY_RING_FULL, "DIRTY_RING_FULL"},
 #ifdef KVM_EXIT_MEMORY_NOT_PRESENT
 	{KVM_EXIT_MEMORY_NOT_PRESENT, "MEMORY_NOT_PRESENT"},
 #endif
diff --git a/tools/testing/selftests/kvm/lib/kvm_util_internal.h b/tools/testing/selftests/kvm/lib/kvm_util_internal.h
index ac50c42750cf..452e1f02611a 100644
--- a/tools/testing/selftests/kvm/lib/kvm_util_internal.h
+++ b/tools/testing/selftests/kvm/lib/kvm_util_internal.h
@@ -39,6 +39,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 {
@@ -61,6 +64,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);
-- 
2.24.1


^ permalink raw reply related	[flat|nested] 35+ messages in thread

* [PATCH v6 13/14] KVM: selftests: Let dirty_log_test async for dirty ring test
  2020-03-09 21:44 [PATCH v6 00/14] KVM: Dirty ring interface Peter Xu
                   ` (11 preceding siblings ...)
  2020-03-09 22:25 ` [PATCH v6 12/14] KVM: selftests: Add dirty ring buffer test Peter Xu
@ 2020-03-09 22:25 ` Peter Xu
  2020-03-10  8:27   ` Andrew Jones
  2020-03-09 22:25 ` [PATCH v6 14/14] KVM: selftests: Add "-c" parameter to dirty log test Peter Xu
  13 siblings, 1 reply; 35+ messages in thread
From: Peter Xu @ 2020-03-09 22:25 UTC (permalink / raw)
  To: linux-kernel, kvm
  Cc: Peter Xu, Yan Zhao, Jason Wang, Alex Williamson,
	Vitaly Kuznetsov, Dr . David Alan Gilbert,
	Christophe de Dinechin, Michael S . Tsirkin, Kevin Tian,
	Paolo Bonzini, Sean Christopherson

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.

Signed-off-by: Peter Xu <peterx@redhat.com>
---
 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(-)

diff --git a/tools/testing/selftests/kvm/dirty_log_test.c b/tools/testing/selftests/kvm/dirty_log_test.c
index 134637267af4..b07e52858e87 100644
--- a/tools/testing/selftests/kvm/dirty_log_test.c
+++ b/tools/testing/selftests/kvm/dirty_log_test.c
@@ -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;
+pthread_t vcpu_thread;
+
+/* Only way to pass this to the signal handler */
+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)
 {
@@ -184,10 +222,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));
@@ -243,27 +284,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)
+{
+	DEBUG("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),
@@ -275,13 +326,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);
 
-	DEBUG("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();
+	}
 
 	DEBUG("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);
 
@@ -289,10 +343,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);
-		DEBUG("vcpu stops because dirty ring full...\n");
-		sem_wait(&dirty_ring_vcpu_cont);
+		DEBUG("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);
 		DEBUG("vcpu continues now.\n");
 	} else {
 		TEST_ASSERT(false, "Invalid guest sync status: "
@@ -317,7 +377,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] = {
 	{
@@ -388,12 +448,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)
@@ -414,18 +474,25 @@ 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;
+	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);
 		/* 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);
 	}
 
 	return NULL;
@@ -572,7 +639,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;
 
diff --git a/tools/testing/selftests/kvm/include/kvm_util.h b/tools/testing/selftests/kvm/include/kvm_util.h
index 6cdb3e62c6fb..891deff4e16c 100644
--- a/tools/testing/selftests/kvm/include/kvm_util.h
+++ b/tools/testing/selftests/kvm/include/kvm_util.h
@@ -115,6 +115,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);
diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c
index 9be96b676f35..3fb33ae8941a 100644
--- a/tools/testing/selftests/kvm/lib/kvm_util.c
+++ b/tools/testing/selftests/kvm/lib/kvm_util.c
@@ -1177,6 +1177,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);
-- 
2.24.1


^ permalink raw reply related	[flat|nested] 35+ messages in thread

* [PATCH v6 14/14] KVM: selftests: Add "-c" parameter to dirty log test
  2020-03-09 21:44 [PATCH v6 00/14] KVM: Dirty ring interface Peter Xu
                   ` (12 preceding siblings ...)
  2020-03-09 22:25 ` [PATCH v6 13/14] KVM: selftests: Let dirty_log_test async for dirty ring test Peter Xu
@ 2020-03-09 22:25 ` Peter Xu
  13 siblings, 0 replies; 35+ messages in thread
From: Peter Xu @ 2020-03-09 22:25 UTC (permalink / raw)
  To: linux-kernel, kvm
  Cc: Peter Xu, Yan Zhao, Jason Wang, Alex Williamson,
	Vitaly Kuznetsov, Dr . David Alan Gilbert,
	Christophe de Dinechin, Michael S . Tsirkin, Kevin Tian,
	Paolo Bonzini, Sean Christopherson

It'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.

Signed-off-by: Peter Xu <peterx@redhat.com>
---
 tools/testing/selftests/kvm/dirty_log_test.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/tools/testing/selftests/kvm/dirty_log_test.c b/tools/testing/selftests/kvm/dirty_log_test.c
index b07e52858e87..60f3921e6c40 100644
--- a/tools/testing/selftests/kvm/dirty_log_test.c
+++ b/tools/testing/selftests/kvm/dirty_log_test.c
@@ -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;
 pthread_t vcpu_thread;
+static uint32_t test_dirty_ring_count = TEST_DIRTY_RING_COUNT;
 
 /* Only way to pass this to the signal handler */
 struct kvm_vm *current_vm;
@@ -245,7 +246,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));
 }
 
@@ -267,7 +268,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: "
@@ -774,6 +775,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",
@@ -829,8 +833,11 @@ int main(int argc, char *argv[])
 	vm_guest_mode_params_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;
-- 
2.24.1


^ permalink raw reply related	[flat|nested] 35+ messages in thread

* Re: [PATCH v6 10/14] KVM: selftests: Use a single binary for dirty/clear log test
  2020-03-09 22:25 ` [PATCH v6 10/14] KVM: selftests: Use a single binary for dirty/clear log test Peter Xu
@ 2020-03-10  8:10   ` Andrew Jones
  2020-03-11 17:43     ` Peter Xu
  0 siblings, 1 reply; 35+ messages in thread
From: Andrew Jones @ 2020-03-10  8:10 UTC (permalink / raw)
  To: Peter Xu
  Cc: linux-kernel, kvm, Yan Zhao, Jason Wang, Alex Williamson,
	Vitaly Kuznetsov, Dr . David Alan Gilbert,
	Christophe de Dinechin, Michael S . Tsirkin, Kevin Tian,
	Paolo Bonzini, Sean Christopherson

On Mon, Mar 09, 2020 at 06:25:19PM -0400, Peter Xu wrote:
> Remove 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.
> 
> Signed-off-by: Peter Xu <peterx@redhat.com>
> ---
>  tools/testing/selftests/kvm/Makefile          |   2 -
>  .../selftests/kvm/clear_dirty_log_test.c      |   2 -
>  tools/testing/selftests/kvm/dirty_log_test.c  | 169 +++++++++++++++---
>  3 files changed, 146 insertions(+), 27 deletions(-)
>  delete mode 100644 tools/testing/selftests/kvm/clear_dirty_log_test.c
> 
> diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile
> index d91c53b726e6..941bfcd48eaa 100644
> --- a/tools/testing/selftests/kvm/Makefile
> +++ b/tools/testing/selftests/kvm/Makefile
> @@ -27,11 +27,9 @@ 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 += x86_64/svm_vmcall_test
> -TEST_GEN_PROGS_x86_64 += clear_dirty_log_test
>  TEST_GEN_PROGS_x86_64 += dirty_log_test
>  TEST_GEN_PROGS_x86_64 += kvm_create_max_vcpus
>  
> -TEST_GEN_PROGS_aarch64 += clear_dirty_log_test
>  TEST_GEN_PROGS_aarch64 += dirty_log_test
>  TEST_GEN_PROGS_aarch64 += kvm_create_max_vcpus
>  
> diff --git a/tools/testing/selftests/kvm/clear_dirty_log_test.c b/tools/testing/selftests/kvm/clear_dirty_log_test.c
> deleted file mode 100644
> index 749336937d37..000000000000
> --- a/tools/testing/selftests/kvm/clear_dirty_log_test.c
> +++ /dev/null
> @@ -1,2 +0,0 @@
> -#define USE_CLEAR_DIRTY_LOG
> -#include "dirty_log_test.c"
> diff --git a/tools/testing/selftests/kvm/dirty_log_test.c b/tools/testing/selftests/kvm/dirty_log_test.c
> index 3c0ffd34b3b0..642886394e34 100644
> --- a/tools/testing/selftests/kvm/dirty_log_test.c
> +++ b/tools/testing/selftests/kvm/dirty_log_test.c
> @@ -128,6 +128,73 @@ static uint64_t host_dirty_count;
>  static uint64_t host_clear_count;
>  static uint64_t host_track_next_count;
>  
> +enum log_mode_t {
> +	/* Only use KVM_GET_DIRTY_LOG for logging */
> +	LOG_MODE_DIRTY_LOG = 0,
> +
> +	/* Use both KVM_[GET|CLEAR]_DIRTY_LOG for logging */
> +	LOG_MODE_CLEAR_LOG = 1,
> +
> +	LOG_MODE_NUM,
> +
> +	/* Run all supported modes */
> +	LOG_MODE_ALL = LOG_MODE_NUM,
> +};
> +
> +/* Mode of logging to test.  Default is to run all supported modes */
> +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 bool clear_log_supported(void)
> +{
> +	return kvm_check_cap(KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2);
> +}
> +
> +static void clear_log_create_vm_done(struct kvm_vm *vm)
> +{
> +	struct kvm_enable_cap cap = {};
> +
> +	cap.cap = KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2;
> +	cap.args[0] = 1;
> +	vm_enable_cap(vm, &cap);
> +}
> +
> +static void dirty_log_collect_dirty_pages(struct kvm_vm *vm, int slot,
> +					  void *bitmap, uint32_t num_pages)
> +{
> +	kvm_vm_get_dirty_log(vm, slot, bitmap);
> +}
> +
> +static void clear_log_collect_dirty_pages(struct kvm_vm *vm, int slot,
> +					  void *bitmap, uint32_t num_pages)
> +{
> +	kvm_vm_get_dirty_log(vm, slot, bitmap);
> +	kvm_vm_clear_dirty_log(vm, slot, bitmap, 0, num_pages);
> +}
> +
> +struct log_mode {
> +	const char *name;
> +	/* Return true if this mode is supported, otherwise false */
> +	bool (*supported)(void);
> +	/* Hook when the vm creation is done (before vcpu creation) */
> +	void (*create_vm_done)(struct kvm_vm *vm);
> +	/* 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);
> +} log_modes[LOG_MODE_NUM] = {
> +	{
> +		.name = "dirty-log",
> +		.collect_dirty_pages = dirty_log_collect_dirty_pages,
> +	},
> +	{
> +		.name = "clear-log",
> +		.supported = clear_log_supported,
> +		.create_vm_done = clear_log_create_vm_done,
> +		.collect_dirty_pages = clear_log_collect_dirty_pages,
> +	},
> +};
> +
>  /*
>   * We use this bitmap to track some pages that should have its dirty
>   * bit set in the _next_ iteration.  For example, if we detected the
> @@ -137,6 +204,43 @@ static uint64_t host_track_next_count;
>   */
>  static unsigned long *host_bmap_track;
>  
> +static void log_modes_dump(void)
> +{
> +	int i;
> +
> +	for (i = 0; i < LOG_MODE_NUM; i++)
> +		printf("%s, ", log_modes[i].name);
> +	puts("\b\b  \b\b");

This will be ugly when the output is redirected to a file.
How about just

printf("%s", log_modes[0].name);
for (i = 1; i < LOG_MODE_NUM; i++)
  printf(", %s", log_modes[i].name);
printf("\n");

> +}
> +
> +static bool log_mode_supported(void)
> +{
> +	struct log_mode *mode = &log_modes[host_log_mode];
> +
> +	if (mode->supported)
> +		return mode->supported();
> +
> +	return true;
> +}
> +
> +static void log_mode_create_vm_done(struct kvm_vm *vm)
> +{
> +	struct log_mode *mode = &log_modes[host_log_mode];
> +
> +	if (mode->create_vm_done)
> +		mode->create_vm_done(vm);
> +}
> +
> +static void log_mode_collect_dirty_pages(struct kvm_vm *vm, int slot,
> +					 void *bitmap, uint32_t num_pages)
> +{
> +	struct log_mode *mode = &log_modes[host_log_mode];
> +
> +	TEST_ASSERT(mode->collect_dirty_pages != NULL,
> +		    "collect_dirty_pages() is required for any log mode!");
> +	mode->collect_dirty_pages(vm, slot, bitmap, num_pages);
> +}
> +
>  static void generate_random_array(uint64_t *guest_array, uint64_t size)
>  {
>  	uint64_t i;
> @@ -257,6 +361,7 @@ static struct kvm_vm *create_vm(enum vm_guest_mode mode, uint32_t vcpuid,
>  #ifdef __x86_64__
>  	vm_create_irqchip(vm);
>  #endif
> +	log_mode_create_vm_done(vm);
>  	vm_vcpu_add_default(vm, vcpuid, guest_code);
>  	return vm;
>  }
> @@ -271,6 +376,12 @@ static void run_test(enum vm_guest_mode mode, unsigned long iterations,
>  	struct kvm_vm *vm;
>  	unsigned long *bmap;
>  
> +	if (!log_mode_supported()) {
> +		fprintf(stderr, "Log mode '%s' not supported, skip\n",
> +			log_modes[host_log_mode].name);

I think kvm selftests needs a skip_test() function that outputs a more
consistent test skip message. It seems we mostly do

fprintf(stderr, "%s, skipping test\n", custom_message);

but here we have ', skip'. Also, I see a few places were we output
skipping to stderr and others to stdout. I think I like stdout better.

> +		return;
> +	}
> +
>  	/*
>  	 * We reserve page table for 2 times of extra dirty mem which
>  	 * will definitely cover the original (1G+) test range.  Here
> @@ -316,14 +427,6 @@ static void run_test(enum vm_guest_mode mode, unsigned long iterations,
>  	bmap = bitmap_alloc(host_num_pages);
>  	host_bmap_track = bitmap_alloc(host_num_pages);
>  
> -#ifdef USE_CLEAR_DIRTY_LOG
> -	struct kvm_enable_cap cap = {};
> -
> -	cap.cap = KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2;
> -	cap.args[0] = 1;
> -	vm_enable_cap(vm, &cap);
> -#endif
> -
>  	/* Add an extra memory slot for testing dirty logging */
>  	vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS,
>  				    guest_test_phys_mem,
> @@ -364,11 +467,8 @@ static void run_test(enum vm_guest_mode mode, unsigned long iterations,
>  	while (iteration < iterations) {
>  		/* Give the vcpu thread some time to dirty some pages */
>  		usleep(interval * 1000);
> -		kvm_vm_get_dirty_log(vm, TEST_MEM_SLOT_INDEX, bmap);
> -#ifdef USE_CLEAR_DIRTY_LOG
> -		kvm_vm_clear_dirty_log(vm, TEST_MEM_SLOT_INDEX, bmap, 0,
> -				       host_num_pages);
> -#endif
> +		log_mode_collect_dirty_pages(vm, TEST_MEM_SLOT_INDEX,
> +					     bmap, host_num_pages);
>  		vm_dirty_log_verify(bmap);
>  		iteration++;
>  		sync_global_to_guest(vm, iteration);
> @@ -413,6 +513,9 @@ static void help(char *name)
>  	       TEST_HOST_LOOP_INTERVAL);
>  	printf(" -p: specify guest physical test memory offset\n"
>  	       "     Warning: a low offset can conflict with the loaded test code.\n");
> +	printf(" -M: specify the host logging mode "
> +	       "(default: run all log modes).  Supported modes: \n\t");
> +	log_modes_dump();
>  	printf(" -m: specify the guest mode ID to test "
>  	       "(default: test all supported modes)\n"
>  	       "     This option may be used multiple times.\n"
> @@ -432,18 +535,11 @@ int main(int argc, char *argv[])
>  	bool mode_selected = false;
>  	uint64_t phys_offset = 0;
>  	unsigned int mode;
> -	int opt, i;
> +	int opt, i, j;
>  #ifdef __aarch64__
>  	unsigned int host_ipa_limit;
>  #endif
>  
> -#ifdef USE_CLEAR_DIRTY_LOG
> -	if (!kvm_check_cap(KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2)) {
> -		fprintf(stderr, "KVM_CLEAR_DIRTY_LOG not available, skipping tests\n");
> -		exit(KSFT_SKIP);
> -	}
> -#endif
> -
>  #ifdef __x86_64__
>  	vm_guest_mode_params_init(VM_MODE_PXXV48_4K, true, true);
>  #endif
> @@ -463,7 +559,7 @@ int main(int argc, char *argv[])
>  	vm_guest_mode_params_init(VM_MODE_P40V48_4K, true, true);
>  #endif
>  
> -	while ((opt = getopt(argc, argv, "hi:I:p:m:")) != -1) {
> +	while ((opt = getopt(argc, argv, "hi:I:p:m:M:")) != -1) {
>  		switch (opt) {
>  		case 'i':
>  			iterations = strtol(optarg, NULL, 10);
> @@ -485,6 +581,22 @@ int main(int argc, char *argv[])
>  				    "Guest mode ID %d too big", mode);
>  			vm_guest_mode_params[mode].enabled = true;
>  			break;
> +		case 'M':

Can also add

if (!strcmp(optarg, "all"))
  host_log_mode_option = LOG_MODE_ALL;

> +			for (i = 0; i < LOG_MODE_NUM; i++) {
> +				if (!strcmp(optarg, log_modes[i].name)) {
> +					DEBUG("Setting log mode to: '%s'\n",
> +					      optarg);

Basing this on kvm/queue won't work as DEBUG() no longer exists. This
looks like a pr_info().

> +					host_log_mode_option = i;
> +					break;
> +				}
> +			}
> +			if (i == LOG_MODE_NUM) {
> +				printf("Log mode '%s' is invalid.  "
> +				       "Please choose from: ", optarg);
> +				log_modes_dump();
> +				exit(-1);

Exit code of 255? Probably just want exit(1);

> +			}
> +			break;
>  		case 'h':
>  		default:
>  			help(argv[0]);
> @@ -506,7 +618,18 @@ int main(int argc, char *argv[])
>  		TEST_ASSERT(vm_guest_mode_params[i].supported,
>  			    "Guest mode ID %d (%s) not supported.",
>  			    i, vm_guest_mode_string(i));
> -		run_test(i, iterations, interval, phys_offset);
> +		if (host_log_mode_option == LOG_MODE_ALL) {
> +			/* Run each log mode */
> +			for (j = 0; j < LOG_MODE_NUM; j++) {
> +				DEBUG("Testing Log Mode '%s'\n",
> +				      log_modes[j].name);
> +				host_log_mode = j;
> +				run_test(i, iterations, interval, phys_offset);
> +			}
> +		} else {
> +			host_log_mode = host_log_mode_option;
> +			run_test(i, iterations, interval, phys_offset);
> +		}
>  	}
>  
>  	return 0;
> -- 
> 2.24.1
>

Thanks,
drew 


^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v6 12/14] KVM: selftests: Add dirty ring buffer test
  2020-03-09 22:25 ` [PATCH v6 12/14] KVM: selftests: Add dirty ring buffer test Peter Xu
@ 2020-03-10  8:18   ` Andrew Jones
  2020-03-11 17:44     ` Peter Xu
  0 siblings, 1 reply; 35+ messages in thread
From: Andrew Jones @ 2020-03-10  8:18 UTC (permalink / raw)
  To: Peter Xu
  Cc: linux-kernel, kvm, Yan Zhao, Jason Wang, Alex Williamson,
	Vitaly Kuznetsov, Dr . David Alan Gilbert,
	Christophe de Dinechin, Michael S . Tsirkin, Kevin Tian,
	Paolo Bonzini, Sean Christopherson

On Mon, Mar 09, 2020 at 06:25:29PM -0400, Peter Xu wrote:
> +void *vcpu_map_dirty_ring(struct kvm_vm *vm, uint32_t vcpuid)
> +{
> +	struct vcpu *vcpu;
> +	uint32_t size = vm->dirty_ring_size;
> +
> +	TEST_ASSERT(size > 0, "Should enable dirty ring first");
> +
> +	vcpu = vcpu_find(vm, vcpuid);
> +
> +	TEST_ASSERT(vcpu, "Cannot find vcpu %u", vcpuid);
> +
> +	if (!vcpu->dirty_gfns) {
> +		void *addr;
> +
> +		addr = mmap(NULL, size, PROT_READ,
> +			    MAP_PRIVATE, vcpu->fd,
> +			    vm->page_size * KVM_DIRTY_LOG_PAGE_OFFSET);
> +		TEST_ASSERT(addr == MAP_FAILED, "Dirty ring mapped private");
> +
> +		addr = mmap(NULL, size, PROT_READ | PROT_EXEC,
> +			    MAP_PRIVATE, vcpu->fd,
> +			    vm->page_size * KVM_DIRTY_LOG_PAGE_OFFSET);
> +		TEST_ASSERT(addr == MAP_FAILED, "Dirty ring mapped exec");
> +
> +		addr = mmap(NULL, size, PROT_READ | PROT_WRITE,
> +			    MAP_SHARED, vcpu->fd,
> +			    vm->page_size * KVM_DIRTY_LOG_PAGE_OFFSET);

No TEST_ASSERT for this mmap?

> +
> +		vcpu->dirty_gfns = addr;
> +		vcpu->dirty_gfns_count = size / sizeof(struct kvm_dirty_gfn);
> +	}
> +
> +	return vcpu->dirty_gfns;
> +}


^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v6 13/14] KVM: selftests: Let dirty_log_test async for dirty ring test
  2020-03-09 22:25 ` [PATCH v6 13/14] KVM: selftests: Let dirty_log_test async for dirty ring test Peter Xu
@ 2020-03-10  8:27   ` Andrew Jones
  2020-03-11 17:45     ` Peter Xu
  0 siblings, 1 reply; 35+ messages in thread
From: Andrew Jones @ 2020-03-10  8:27 UTC (permalink / raw)
  To: Peter Xu
  Cc: linux-kernel, kvm, Yan Zhao, Jason Wang, Alex Williamson,
	Vitaly Kuznetsov, Dr . David Alan Gilbert,
	Christophe de Dinechin, Michael S . Tsirkin, Kevin Tian,
	Paolo Bonzini, Sean Christopherson

On Mon, Mar 09, 2020 at 06:25:34PM -0400, Peter Xu wrote:
> 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.
> 
> Signed-off-by: Peter Xu <peterx@redhat.com>
> ---
>  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(-)
> 
> diff --git a/tools/testing/selftests/kvm/dirty_log_test.c b/tools/testing/selftests/kvm/dirty_log_test.c
> index 134637267af4..b07e52858e87 100644
> --- a/tools/testing/selftests/kvm/dirty_log_test.c
> +++ b/tools/testing/selftests/kvm/dirty_log_test.c
> @@ -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;
> +pthread_t vcpu_thread;
> +
> +/* Only way to pass this to the signal handler */
> +struct kvm_vm *current_vm;

nit: above two new globals could be static


^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v6 02/14] KVM: Cache as_id in kvm_memory_slot
  2020-03-09 21:44 ` [PATCH v6 02/14] KVM: Cache as_id in kvm_memory_slot Peter Xu
@ 2020-03-10 14:48   ` Sean Christopherson
  2020-03-11 15:48     ` Peter Xu
  0 siblings, 1 reply; 35+ messages in thread
From: Sean Christopherson @ 2020-03-10 14:48 UTC (permalink / raw)
  To: Peter Xu
  Cc: linux-kernel, kvm, Yan Zhao, Jason Wang, Alex Williamson,
	Vitaly Kuznetsov, Dr . David Alan Gilbert,
	Christophe de Dinechin, Michael S . Tsirkin, Kevin Tian,
	Paolo Bonzini

On Mon, Mar 09, 2020 at 05:44:12PM -0400, Peter Xu wrote:
> Cache the address space ID just like the slot ID.  It will be used in
> order to fill in the dirty ring entries.
> 
> Suggested-by: Paolo Bonzini <pbonzini@redhat.com>
> Suggested-by: Sean Christopherson <sean.j.christopherson@intel.com>
> Signed-off-by: Peter Xu <peterx@redhat.com>
> ---
>  include/linux/kvm_host.h | 1 +
>  virt/kvm/kvm_main.c      | 2 ++
>  2 files changed, 3 insertions(+)
> 
> diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
> index bcb9b2ac0791..afa0e9034881 100644
> --- a/include/linux/kvm_host.h
> +++ b/include/linux/kvm_host.h
> @@ -346,6 +346,7 @@ struct kvm_memory_slot {
>  	unsigned long userspace_addr;
>  	u32 flags;
>  	short id;
> +	u8 as_id;

My vote is to store this as a u16 and remove the BUILD_BUG_ON.  Using a u8
doesn't save any memory since the compiler will pad out the struct.

>  };
>  
>  static inline unsigned long kvm_dirty_bitmap_bytes(struct kvm_memory_slot *memslot)
> diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
> index 70f03ce0e5c1..e6484dabfc59 100644
> --- a/virt/kvm/kvm_main.c
> +++ b/virt/kvm/kvm_main.c
> @@ -1036,6 +1036,8 @@ int __kvm_set_memory_region(struct kvm *kvm,
>  
>  	new = old = *slot;
>  
> +	BUILD_BUG_ON(U8_MAX < KVM_ADDRESS_SPACE_NUM);
> +	new.as_id = as_id;
>  	new.id = id;
>  	new.base_gfn = base_gfn;
>  	new.npages = npages;
> -- 
> 2.24.1
> 

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v6 03/14] KVM: X86: Don't track dirty for KVM_SET_[TSS_ADDR|IDENTITY_MAP_ADDR]
  2020-03-09 21:44 ` [PATCH v6 03/14] KVM: X86: Don't track dirty for KVM_SET_[TSS_ADDR|IDENTITY_MAP_ADDR] Peter Xu
@ 2020-03-10 15:06   ` Sean Christopherson
  2020-03-11 16:01     ` Peter Xu
  2020-03-11  1:10     ` kbuild test robot
  1 sibling, 1 reply; 35+ messages in thread
From: Sean Christopherson @ 2020-03-10 15:06 UTC (permalink / raw)
  To: Peter Xu
  Cc: linux-kernel, kvm, Yan Zhao, Jason Wang, Alex Williamson,
	Vitaly Kuznetsov, Dr . David Alan Gilbert,
	Christophe de Dinechin, Michael S . Tsirkin, Kevin Tian,
	Paolo Bonzini

On Mon, Mar 09, 2020 at 05:44:13PM -0400, Peter Xu wrote:
> diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
> index 40b1e6138cd5..fc638a164e03 100644
> --- a/arch/x86/kvm/vmx/vmx.c
> +++ b/arch/x86/kvm/vmx/vmx.c
> @@ -3467,34 +3467,26 @@ static bool guest_state_valid(struct kvm_vcpu *vcpu)
>  	return true;
>  }
>  
> -static int init_rmode_tss(struct kvm *kvm)
> +static int init_rmode_tss(struct kvm *kvm, void __user *ua)
>  {
> -	gfn_t fn;
> +	const void *zero_page = (const void *) __va(page_to_phys(ZERO_PAGE(0)));
>  	u16 data = 0;

"data" doesn't need to be intialized to zero, it's set below before it's used.

>  	int idx, r;

nit: I'd prefer to rename "idx" to "i" to make it more obvious it's a plain
ole loop counter.  Reusing the srcu index made me do a double take :-)

>  
> -	idx = srcu_read_lock(&kvm->srcu);
> -	fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
> -	r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
> -	if (r < 0)
> -		goto out;
> +	for (idx = 0; idx < 3; idx++) {
> +		r = __copy_to_user(ua + PAGE_SIZE * idx, zero_page, PAGE_SIZE);
> +		if (r)
> +			return -EFAULT;
> +	}

Can this be done in a single __copy_to_user(), or do those helpers not like
crossing page boundaries?

> +
>  	data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
> -	r = kvm_write_guest_page(kvm, fn++, &data,
> -			TSS_IOPB_BASE_OFFSET, sizeof(u16));
> -	if (r < 0)
> -		goto out;
> -	r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
> -	if (r < 0)
> -		goto out;
> -	r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
> -	if (r < 0)
> -		goto out;
> +	r = __copy_to_user(ua + TSS_IOPB_BASE_OFFSET, &data, sizeof(u16));
> +	if (r)
> +		return -EFAULT;
> +
>  	data = ~0;
> -	r = kvm_write_guest_page(kvm, fn, &data,
> -				 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
> -				 sizeof(u8));
> -out:
> -	srcu_read_unlock(&kvm->srcu, idx);
> +	r = __copy_to_user(ua + RMODE_TSS_SIZE - 1, &data, sizeof(u8));
> +
>  	return r;
>  }
>  
> @@ -3503,6 +3495,7 @@ static int init_rmode_identity_map(struct kvm *kvm)
>  	struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
>  	int i, r = 0;
>  	kvm_pfn_t identity_map_pfn;
> +	void __user *uaddr;
>  	u32 tmp;
>  
>  	/* Protect kvm_vmx->ept_identity_pagetable_done. */
> @@ -3515,22 +3508,24 @@ static int init_rmode_identity_map(struct kvm *kvm)
>  		kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
>  	identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
>  
> -	r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
> -				    kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
> -	if (r < 0)
> +	uaddr = __x86_set_memory_region(kvm,
> +					IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
> +					kvm_vmx->ept_identity_map_addr,
> +					PAGE_SIZE);
> +	if (IS_ERR(uaddr)) {
> +		r = PTR_ERR(uaddr);
>  		goto out;
> +	}
>  
> -	r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
> -	if (r < 0)
> -		goto out;
>  	/* Set up identity-mapping pagetable for EPT in real mode */
>  	for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
>  		tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
>  			_PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
> -		r = kvm_write_guest_page(kvm, identity_map_pfn,
> -				&tmp, i * sizeof(tmp), sizeof(tmp));
> -		if (r < 0)
> +		r = __copy_to_user(uaddr + i * sizeof(tmp), &tmp, sizeof(tmp));
> +		if (r) {
> +			r = -EFAULT;
>  			goto out;
> +		}
>  	}
>  	kvm_vmx->ept_identity_pagetable_done = true;
>  
> @@ -3557,19 +3552,22 @@ static void seg_setup(int seg)
>  static int alloc_apic_access_page(struct kvm *kvm)
>  {
>  	struct page *page;
> -	int r = 0;
> +	void __user *r;
> +	int ret = 0;
>  
>  	mutex_lock(&kvm->slots_lock);
>  	if (kvm->arch.apic_access_page_done)
>  		goto out;
>  	r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
>  				    APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
> -	if (r)
> +	if (IS_ERR(r)) {
> +		ret = PTR_ERR(r);
>  		goto out;
> +	}
>  
>  	page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
>  	if (is_error_page(page)) {
> -		r = -EFAULT;
> +		ret = -EFAULT;
>  		goto out;
>  	}
>  
> @@ -3581,7 +3579,7 @@ static int alloc_apic_access_page(struct kvm *kvm)
>  	kvm->arch.apic_access_page_done = true;
>  out:
>  	mutex_unlock(&kvm->slots_lock);
> -	return r;
> +	return ret;
>  }
>  
>  int allocate_vpid(void)
> @@ -4503,7 +4501,7 @@ static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
>  
>  static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
>  {
> -	int ret;
> +	void __user *ret;
>  
>  	if (enable_unrestricted_guest)
>  		return 0;
> @@ -4513,10 +4511,12 @@ static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
>  				      PAGE_SIZE * 3);
>  	mutex_unlock(&kvm->slots_lock);
>  
> -	if (ret)
> -		return ret;
> +	if (IS_ERR(ret))
> +		return PTR_ERR(ret);
> +
>  	to_kvm_vmx(kvm)->tss_addr = addr;
> -	return init_rmode_tss(kvm);
> +
> +	return init_rmode_tss(kvm, ret);
>  }
>  
>  static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> index 5de200663f51..fe485d4ba6c7 100644
> --- a/arch/x86/kvm/x86.c
> +++ b/arch/x86/kvm/x86.c
> @@ -9756,7 +9756,33 @@ 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)
> +/**
> + * __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:
> + *
> + *   - An error number if error happened, or,
> + *   - For installation: the HVA of the newly mapped memory slot, or,
> + *   - For uninstallation: zero if we successfully uninstall a slot.

Maybe tweak this so the return it stands out?  And returning zero on
uninstallation is no longer true in kvm/queue, at least not without further
modifications (as is it'll return 0xdead000000000000 on 64-bit).  The
0xdead shenanigans won't trigger IS_ERR(), so I think this can simply be:

 * Returns:
 *   hva:    on success
 *   -errno: on error

With the blurb below calling out that hva is bogus uninstallation.

> + *
> + * The caller should always use IS_ERR() to check the return value
> + * before use.  NOTE: KVM internal memory slots are guaranteed and
> + * won't change until the VM is destroyed. This is also true to the
> + * returned HVA when installing a new memory slot.  The HVA can be
> + * invalidated by either an errornous userspace program or a VM under
> + * destruction, however as long as we use __copy_{to|from}_user()
> + * properly upon the HVAs and handle the failure paths always then
> + * we're safe.
> + */
> +void __user * __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa,
> +				      u32 size)
>  {
>  	int i, r;
>  	unsigned long hva;
> @@ -9765,12 +9791,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(-EINVAL);
>  
>  	slot = id_to_memslot(slots, id);
>  	if (size) {
>  		if (slot->npages)
> -			return -EEXIST;
> +			return ERR_PTR(-EEXIST);
>  
>  		/*
>  		 * MAP_SHARED to prevent internal slot pages from being moved
> @@ -9779,10 +9805,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->npages)
> -			return 0;
> +			return ERR_PTR(0);
>  
>  		hva = 0;
>  	}
> @@ -9798,13 +9824,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(r);
>  	}
>  
>  	if (!size)
>  		vm_munmap(old.userspace_addr, old.npages * PAGE_SIZE);
>  
> -	return 0;
> +	return (void __user *)hva;
>  }
>  EXPORT_SYMBOL_GPL(__x86_set_memory_region);
>  
> -- 
> 2.24.1
> 

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v6 03/14] KVM: X86: Don't track dirty for KVM_SET_[TSS_ADDR|IDENTITY_MAP_ADDR]
  2020-03-09 21:44 ` [PATCH v6 03/14] KVM: X86: Don't track dirty for KVM_SET_[TSS_ADDR|IDENTITY_MAP_ADDR] Peter Xu
@ 2020-03-11  1:10     ` kbuild test robot
  2020-03-11  1:10     ` kbuild test robot
  1 sibling, 0 replies; 35+ messages in thread
From: kbuild test robot @ 2020-03-11  1:10 UTC (permalink / raw)
  To: Peter Xu
  Cc: kbuild-all, linux-kernel, kvm, Yan Zhao, Jason Wang,
	Alex Williamson, peterx, Vitaly Kuznetsov,
	Dr . David Alan Gilbert, Christophe de Dinechin,
	Michael S . Tsirkin, Kevin Tian, Paolo Bonzini,
	Sean Christopherson

Hi Peter,

Thank you for the patch! Perhaps something to improve:

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

url:    https://github.com/0day-ci/linux/commits/Peter-Xu/KVM-Dirty-ring-interface/20200310-070637
base:   https://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git 12481c76713078054f2d043b3ce946e4814ac29f
reproduce:
        # apt-get install sparse
        # sparse version: v0.6.1-174-g094d5a94-dirty
        make ARCH=x86_64 allmodconfig
        make C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__'

If you fix the issue, kindly add following tag
Reported-by: kbuild test robot <lkp@intel.com>


sparse warnings: (new ones prefixed by >>)

   arch/x86/kvm/x86.c:2599:38: sparse: sparse: incorrect type in argument 1 (different address spaces) @@    expected void const [noderef] <asn:1> * @@    got  const [noderef] <asn:1> * @@
   arch/x86/kvm/x86.c:2599:38: sparse:    expected void const [noderef] <asn:1> *
   arch/x86/kvm/x86.c:2599:38: sparse:    got unsigned char [usertype] *
   arch/x86/kvm/x86.c:7501:15: sparse: sparse: incompatible types in comparison expression (different address spaces):
   arch/x86/kvm/x86.c:7501:15: sparse:    struct kvm_apic_map [noderef] <asn:4> *
   arch/x86/kvm/x86.c:7501:15: sparse:    struct kvm_apic_map *
>> arch/x86/kvm/x86.c:9794:31: sparse: sparse: incorrect type in return expression (different address spaces) @@    expected void [noderef] <asn:1> * @@    got n:1> * @@
   arch/x86/kvm/x86.c:9794:31: sparse:    expected void [noderef] <asn:1> *
   arch/x86/kvm/x86.c:9794:31: sparse:    got void *
   arch/x86/kvm/x86.c:9799:39: sparse: sparse: incorrect type in return expression (different address spaces) @@    expected void [noderef] <asn:1> * @@    got n:1> * @@
   arch/x86/kvm/x86.c:9799:39: sparse:    expected void [noderef] <asn:1> *
   arch/x86/kvm/x86.c:9799:39: sparse:    got void *
   arch/x86/kvm/x86.c:9811:39: sparse: sparse: incorrect type in return expression (different address spaces) @@    expected void [noderef] <asn:1> * @@    got n:1> * @@
   arch/x86/kvm/x86.c:9811:39: sparse:    expected void [noderef] <asn:1> *
   arch/x86/kvm/x86.c:9811:39: sparse:    got void *
   arch/x86/kvm/x86.c:9827:39: sparse: sparse: incorrect type in return expression (different address spaces) @@    expected void [noderef] <asn:1> * @@    got n:1> * @@
   arch/x86/kvm/x86.c:9827:39: sparse:    expected void [noderef] <asn:1> *
   arch/x86/kvm/x86.c:9827:39: sparse:    got void *
   arch/x86/kvm/x86.c:9863:16: sparse: sparse: incompatible types in comparison expression (different address spaces):
   arch/x86/kvm/x86.c:9863:16: sparse:    struct kvm_apic_map [noderef] <asn:4> *
   arch/x86/kvm/x86.c:9863:16: sparse:    struct kvm_apic_map *
   arch/x86/kvm/x86.c:9864:15: sparse: sparse: incompatible types in comparison expression (different address spaces):
   arch/x86/kvm/x86.c:9864:15: sparse:    struct kvm_pmu_event_filter [noderef] <asn:4> *
   arch/x86/kvm/x86.c:9864:15: sparse:    struct kvm_pmu_event_filter *
   include/linux/srcu.h:179:9: sparse: sparse: context imbalance in 'vcpu_enter_guest' - unexpected unlock

vim +9794 arch/x86/kvm/x86.c

  9758	
  9759	/**
  9760	 * __x86_set_memory_region: Setup KVM internal memory slot
  9761	 *
  9762	 * @kvm: the kvm pointer to the VM.
  9763	 * @id: the slot ID to setup.
  9764	 * @gpa: the GPA to install the slot (unused when @size == 0).
  9765	 * @size: the size of the slot. Set to zero to uninstall a slot.
  9766	 *
  9767	 * This function helps to setup a KVM internal memory slot.  Specify
  9768	 * @size > 0 to install a new slot, while @size == 0 to uninstall a
  9769	 * slot.  The return code can be one of the following:
  9770	 *
  9771	 *   - An error number if error happened, or,
  9772	 *   - For installation: the HVA of the newly mapped memory slot, or,
  9773	 *   - For uninstallation: zero if we successfully uninstall a slot.
  9774	 *
  9775	 * The caller should always use IS_ERR() to check the return value
  9776	 * before use.  NOTE: KVM internal memory slots are guaranteed and
  9777	 * won't change until the VM is destroyed. This is also true to the
  9778	 * returned HVA when installing a new memory slot.  The HVA can be
  9779	 * invalidated by either an errornous userspace program or a VM under
  9780	 * destruction, however as long as we use __copy_{to|from}_user()
  9781	 * properly upon the HVAs and handle the failure paths always then
  9782	 * we're safe.
  9783	 */
  9784	void __user * __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa,
  9785					      u32 size)
  9786	{
  9787		int i, r;
  9788		unsigned long hva;
  9789		struct kvm_memslots *slots = kvm_memslots(kvm);
  9790		struct kvm_memory_slot *slot, old;
  9791	
  9792		/* Called with kvm->slots_lock held.  */
  9793		if (WARN_ON(id >= KVM_MEM_SLOTS_NUM))
> 9794			return ERR_PTR(-EINVAL);
  9795	
  9796		slot = id_to_memslot(slots, id);
  9797		if (size) {
  9798			if (slot->npages)
  9799				return ERR_PTR(-EEXIST);
  9800	
  9801			/*
  9802			 * MAP_SHARED to prevent internal slot pages from being moved
  9803			 * by fork()/COW.
  9804			 */
  9805			hva = vm_mmap(NULL, 0, size, PROT_READ | PROT_WRITE,
  9806				      MAP_SHARED | MAP_ANONYMOUS, 0);
  9807			if (IS_ERR((void *)hva))
  9808				return (void __user *)hva;
  9809		} else {
  9810			if (!slot->npages)
  9811				return ERR_PTR(0);
  9812	
  9813			hva = 0;
  9814		}
  9815	
  9816		old = *slot;
  9817		for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
  9818			struct kvm_userspace_memory_region m;
  9819	
  9820			m.slot = id | (i << 16);
  9821			m.flags = 0;
  9822			m.guest_phys_addr = gpa;
  9823			m.userspace_addr = hva;
  9824			m.memory_size = size;
  9825			r = __kvm_set_memory_region(kvm, &m);
  9826			if (r < 0)
  9827				return ERR_PTR(r);
  9828		}
  9829	
  9830		if (!size)
  9831			vm_munmap(old.userspace_addr, old.npages * PAGE_SIZE);
  9832	
  9833		return (void __user *)hva;
  9834	}
  9835	EXPORT_SYMBOL_GPL(__x86_set_memory_region);
  9836	

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

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v6 03/14] KVM: X86: Don't track dirty for KVM_SET_[TSS_ADDR|IDENTITY_MAP_ADDR]
@ 2020-03-11  1:10     ` kbuild test robot
  0 siblings, 0 replies; 35+ messages in thread
From: kbuild test robot @ 2020-03-11  1:10 UTC (permalink / raw)
  To: kbuild-all

[-- Attachment #1: Type: text/plain, Size: 6632 bytes --]

Hi Peter,

Thank you for the patch! Perhaps something to improve:

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

url:    https://github.com/0day-ci/linux/commits/Peter-Xu/KVM-Dirty-ring-interface/20200310-070637
base:   https://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git 12481c76713078054f2d043b3ce946e4814ac29f
reproduce:
        # apt-get install sparse
        # sparse version: v0.6.1-174-g094d5a94-dirty
        make ARCH=x86_64 allmodconfig
        make C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__'

If you fix the issue, kindly add following tag
Reported-by: kbuild test robot <lkp@intel.com>


sparse warnings: (new ones prefixed by >>)

   arch/x86/kvm/x86.c:2599:38: sparse: sparse: incorrect type in argument 1 (different address spaces) @@    expected void const [noderef] <asn:1> * @@    got  const [noderef] <asn:1> * @@
   arch/x86/kvm/x86.c:2599:38: sparse:    expected void const [noderef] <asn:1> *
   arch/x86/kvm/x86.c:2599:38: sparse:    got unsigned char [usertype] *
   arch/x86/kvm/x86.c:7501:15: sparse: sparse: incompatible types in comparison expression (different address spaces):
   arch/x86/kvm/x86.c:7501:15: sparse:    struct kvm_apic_map [noderef] <asn:4> *
   arch/x86/kvm/x86.c:7501:15: sparse:    struct kvm_apic_map *
>> arch/x86/kvm/x86.c:9794:31: sparse: sparse: incorrect type in return expression (different address spaces) @@    expected void [noderef] <asn:1> * @@    got n:1> * @@
   arch/x86/kvm/x86.c:9794:31: sparse:    expected void [noderef] <asn:1> *
   arch/x86/kvm/x86.c:9794:31: sparse:    got void *
   arch/x86/kvm/x86.c:9799:39: sparse: sparse: incorrect type in return expression (different address spaces) @@    expected void [noderef] <asn:1> * @@    got n:1> * @@
   arch/x86/kvm/x86.c:9799:39: sparse:    expected void [noderef] <asn:1> *
   arch/x86/kvm/x86.c:9799:39: sparse:    got void *
   arch/x86/kvm/x86.c:9811:39: sparse: sparse: incorrect type in return expression (different address spaces) @@    expected void [noderef] <asn:1> * @@    got n:1> * @@
   arch/x86/kvm/x86.c:9811:39: sparse:    expected void [noderef] <asn:1> *
   arch/x86/kvm/x86.c:9811:39: sparse:    got void *
   arch/x86/kvm/x86.c:9827:39: sparse: sparse: incorrect type in return expression (different address spaces) @@    expected void [noderef] <asn:1> * @@    got n:1> * @@
   arch/x86/kvm/x86.c:9827:39: sparse:    expected void [noderef] <asn:1> *
   arch/x86/kvm/x86.c:9827:39: sparse:    got void *
   arch/x86/kvm/x86.c:9863:16: sparse: sparse: incompatible types in comparison expression (different address spaces):
   arch/x86/kvm/x86.c:9863:16: sparse:    struct kvm_apic_map [noderef] <asn:4> *
   arch/x86/kvm/x86.c:9863:16: sparse:    struct kvm_apic_map *
   arch/x86/kvm/x86.c:9864:15: sparse: sparse: incompatible types in comparison expression (different address spaces):
   arch/x86/kvm/x86.c:9864:15: sparse:    struct kvm_pmu_event_filter [noderef] <asn:4> *
   arch/x86/kvm/x86.c:9864:15: sparse:    struct kvm_pmu_event_filter *
   include/linux/srcu.h:179:9: sparse: sparse: context imbalance in 'vcpu_enter_guest' - unexpected unlock

vim +9794 arch/x86/kvm/x86.c

  9758	
  9759	/**
  9760	 * __x86_set_memory_region: Setup KVM internal memory slot
  9761	 *
  9762	 * @kvm: the kvm pointer to the VM.
  9763	 * @id: the slot ID to setup.
  9764	 * @gpa: the GPA to install the slot (unused when @size == 0).
  9765	 * @size: the size of the slot. Set to zero to uninstall a slot.
  9766	 *
  9767	 * This function helps to setup a KVM internal memory slot.  Specify
  9768	 * @size > 0 to install a new slot, while @size == 0 to uninstall a
  9769	 * slot.  The return code can be one of the following:
  9770	 *
  9771	 *   - An error number if error happened, or,
  9772	 *   - For installation: the HVA of the newly mapped memory slot, or,
  9773	 *   - For uninstallation: zero if we successfully uninstall a slot.
  9774	 *
  9775	 * The caller should always use IS_ERR() to check the return value
  9776	 * before use.  NOTE: KVM internal memory slots are guaranteed and
  9777	 * won't change until the VM is destroyed. This is also true to the
  9778	 * returned HVA when installing a new memory slot.  The HVA can be
  9779	 * invalidated by either an errornous userspace program or a VM under
  9780	 * destruction, however as long as we use __copy_{to|from}_user()
  9781	 * properly upon the HVAs and handle the failure paths always then
  9782	 * we're safe.
  9783	 */
  9784	void __user * __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa,
  9785					      u32 size)
  9786	{
  9787		int i, r;
  9788		unsigned long hva;
  9789		struct kvm_memslots *slots = kvm_memslots(kvm);
  9790		struct kvm_memory_slot *slot, old;
  9791	
  9792		/* Called with kvm->slots_lock held.  */
  9793		if (WARN_ON(id >= KVM_MEM_SLOTS_NUM))
> 9794			return ERR_PTR(-EINVAL);
  9795	
  9796		slot = id_to_memslot(slots, id);
  9797		if (size) {
  9798			if (slot->npages)
  9799				return ERR_PTR(-EEXIST);
  9800	
  9801			/*
  9802			 * MAP_SHARED to prevent internal slot pages from being moved
  9803			 * by fork()/COW.
  9804			 */
  9805			hva = vm_mmap(NULL, 0, size, PROT_READ | PROT_WRITE,
  9806				      MAP_SHARED | MAP_ANONYMOUS, 0);
  9807			if (IS_ERR((void *)hva))
  9808				return (void __user *)hva;
  9809		} else {
  9810			if (!slot->npages)
  9811				return ERR_PTR(0);
  9812	
  9813			hva = 0;
  9814		}
  9815	
  9816		old = *slot;
  9817		for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
  9818			struct kvm_userspace_memory_region m;
  9819	
  9820			m.slot = id | (i << 16);
  9821			m.flags = 0;
  9822			m.guest_phys_addr = gpa;
  9823			m.userspace_addr = hva;
  9824			m.memory_size = size;
  9825			r = __kvm_set_memory_region(kvm, &m);
  9826			if (r < 0)
  9827				return ERR_PTR(r);
  9828		}
  9829	
  9830		if (!size)
  9831			vm_munmap(old.userspace_addr, old.npages * PAGE_SIZE);
  9832	
  9833		return (void __user *)hva;
  9834	}
  9835	EXPORT_SYMBOL_GPL(__x86_set_memory_region);
  9836	

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

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v6 02/14] KVM: Cache as_id in kvm_memory_slot
  2020-03-10 14:48   ` Sean Christopherson
@ 2020-03-11 15:48     ` Peter Xu
  0 siblings, 0 replies; 35+ messages in thread
From: Peter Xu @ 2020-03-11 15:48 UTC (permalink / raw)
  To: Sean Christopherson
  Cc: linux-kernel, kvm, Yan Zhao, Jason Wang, Alex Williamson,
	Vitaly Kuznetsov, Dr . David Alan Gilbert,
	Christophe de Dinechin, Michael S . Tsirkin, Kevin Tian,
	Paolo Bonzini

On Tue, Mar 10, 2020 at 07:48:59AM -0700, Sean Christopherson wrote:
> On Mon, Mar 09, 2020 at 05:44:12PM -0400, Peter Xu wrote:
> > Cache the address space ID just like the slot ID.  It will be used in
> > order to fill in the dirty ring entries.
> > 
> > Suggested-by: Paolo Bonzini <pbonzini@redhat.com>
> > Suggested-by: Sean Christopherson <sean.j.christopherson@intel.com>
> > Signed-off-by: Peter Xu <peterx@redhat.com>
> > ---
> >  include/linux/kvm_host.h | 1 +
> >  virt/kvm/kvm_main.c      | 2 ++
> >  2 files changed, 3 insertions(+)
> > 
> > diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
> > index bcb9b2ac0791..afa0e9034881 100644
> > --- a/include/linux/kvm_host.h
> > +++ b/include/linux/kvm_host.h
> > @@ -346,6 +346,7 @@ struct kvm_memory_slot {
> >  	unsigned long userspace_addr;
> >  	u32 flags;
> >  	short id;
> > +	u8 as_id;
> 
> My vote is to store this as a u16 and remove the BUILD_BUG_ON.  Using a u8
> doesn't save any memory since the compiler will pad out the struct.

Sure. Indeed that BUILD_BUG_ON does not help much in this case.  Thanks,

> 
> >  };
> >  
> >  static inline unsigned long kvm_dirty_bitmap_bytes(struct kvm_memory_slot *memslot)
> > diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
> > index 70f03ce0e5c1..e6484dabfc59 100644
> > --- a/virt/kvm/kvm_main.c
> > +++ b/virt/kvm/kvm_main.c
> > @@ -1036,6 +1036,8 @@ int __kvm_set_memory_region(struct kvm *kvm,
> >  
> >  	new = old = *slot;
> >  
> > +	BUILD_BUG_ON(U8_MAX < KVM_ADDRESS_SPACE_NUM);
> > +	new.as_id = as_id;
> >  	new.id = id;
> >  	new.base_gfn = base_gfn;
> >  	new.npages = npages;
> > -- 
> > 2.24.1
> > 
> 

-- 
Peter Xu


^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v6 03/14] KVM: X86: Don't track dirty for KVM_SET_[TSS_ADDR|IDENTITY_MAP_ADDR]
  2020-03-10 15:06   ` Sean Christopherson
@ 2020-03-11 16:01     ` Peter Xu
  2020-03-11 16:11       ` Sean Christopherson
  0 siblings, 1 reply; 35+ messages in thread
From: Peter Xu @ 2020-03-11 16:01 UTC (permalink / raw)
  To: Sean Christopherson
  Cc: linux-kernel, kvm, Yan Zhao, Jason Wang, Alex Williamson,
	Vitaly Kuznetsov, Dr . David Alan Gilbert,
	Christophe de Dinechin, Michael S . Tsirkin, Kevin Tian,
	Paolo Bonzini

On Tue, Mar 10, 2020 at 08:06:37AM -0700, Sean Christopherson wrote:
> On Mon, Mar 09, 2020 at 05:44:13PM -0400, Peter Xu wrote:
> > diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
> > index 40b1e6138cd5..fc638a164e03 100644
> > --- a/arch/x86/kvm/vmx/vmx.c
> > +++ b/arch/x86/kvm/vmx/vmx.c
> > @@ -3467,34 +3467,26 @@ static bool guest_state_valid(struct kvm_vcpu *vcpu)
> >  	return true;
> >  }
> >  
> > -static int init_rmode_tss(struct kvm *kvm)
> > +static int init_rmode_tss(struct kvm *kvm, void __user *ua)
> >  {
> > -	gfn_t fn;
> > +	const void *zero_page = (const void *) __va(page_to_phys(ZERO_PAGE(0)));
> >  	u16 data = 0;
> 
> "data" doesn't need to be intialized to zero, it's set below before it's used.

Yeah I didn't touch it because this change is irrelevant to the rest.
But I can remove it altogether.

> 
> >  	int idx, r;
> 
> nit: I'd prefer to rename "idx" to "i" to make it more obvious it's a plain
> ole loop counter.  Reusing the srcu index made me do a double take :-)

Another irrelevant change, but ok.

> 
> >  
> > -	idx = srcu_read_lock(&kvm->srcu);
> > -	fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
> > -	r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
> > -	if (r < 0)
> > -		goto out;
> > +	for (idx = 0; idx < 3; idx++) {
> > +		r = __copy_to_user(ua + PAGE_SIZE * idx, zero_page, PAGE_SIZE);
> > +		if (r)
> > +			return -EFAULT;
> > +	}
> 
> Can this be done in a single __copy_to_user(), or do those helpers not like
> crossing page boundaries?

Maybe because the zero_page is only PAGE_SIZE long? :)

[...]

> > -int __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, u32 size)
> > +/**
> > + * __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:
> > + *
> > + *   - An error number if error happened, or,
> > + *   - For installation: the HVA of the newly mapped memory slot, or,
> > + *   - For uninstallation: zero if we successfully uninstall a slot.
> 
> Maybe tweak this so the return it stands out?  And returning zero on
> uninstallation is no longer true in kvm/queue, at least not without further
> modifications (as is it'll return 0xdead000000000000 on 64-bit).  The
> 0xdead shenanigans won't trigger IS_ERR(), so I think this can simply be:
> 
>  * Returns:
>  *   hva:    on success
>  *   -errno: on error
> 
> With the blurb below calling out that hva is bogus uninstallation.

Sure, I'll rebase to kvm/queue for the next version with the
suggestion.

Thanks,

-- 
Peter Xu


^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v6 03/14] KVM: X86: Don't track dirty for KVM_SET_[TSS_ADDR|IDENTITY_MAP_ADDR]
  2020-03-11 16:01     ` Peter Xu
@ 2020-03-11 16:11       ` Sean Christopherson
  0 siblings, 0 replies; 35+ messages in thread
From: Sean Christopherson @ 2020-03-11 16:11 UTC (permalink / raw)
  To: Peter Xu
  Cc: linux-kernel, kvm, Yan Zhao, Jason Wang, Alex Williamson,
	Vitaly Kuznetsov, Dr . David Alan Gilbert,
	Christophe de Dinechin, Michael S . Tsirkin, Kevin Tian,
	Paolo Bonzini

On Wed, Mar 11, 2020 at 12:01:19PM -0400, Peter Xu wrote:
> On Tue, Mar 10, 2020 at 08:06:37AM -0700, Sean Christopherson wrote:
> > On Mon, Mar 09, 2020 at 05:44:13PM -0400, Peter Xu wrote:
> > > -	idx = srcu_read_lock(&kvm->srcu);
> > > -	fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
> > > -	r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
> > > -	if (r < 0)
> > > -		goto out;
> > > +	for (idx = 0; idx < 3; idx++) {
> > > +		r = __copy_to_user(ua + PAGE_SIZE * idx, zero_page, PAGE_SIZE);
> > > +		if (r)
> > > +			return -EFAULT;
> > > +	}
> > 
> > Can this be done in a single __copy_to_user(), or do those helpers not like
> > crossing page boundaries?
> 
> Maybe because the zero_page is only PAGE_SIZE long? :)

Ha, yeah, that'd be a good reason to loop.

> [...]


^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v6 03/14] KVM: X86: Don't track dirty for KVM_SET_[TSS_ADDR|IDENTITY_MAP_ADDR]
  2020-03-11  1:10     ` kbuild test robot
@ 2020-03-11 16:39       ` Peter Xu
  -1 siblings, 0 replies; 35+ messages in thread
From: Peter Xu @ 2020-03-11 16:39 UTC (permalink / raw)
  To: kbuild test robot
  Cc: kbuild-all, linux-kernel, kvm, Yan Zhao, Jason Wang,
	Alex Williamson, Vitaly Kuznetsov, Dr . David Alan Gilbert,
	Christophe de Dinechin, Michael S . Tsirkin, Kevin Tian,
	Paolo Bonzini, Sean Christopherson

On Wed, Mar 11, 2020 at 09:10:04AM +0800, kbuild test robot wrote:
> Hi Peter,
> 
> Thank you for the patch! Perhaps something to improve:
> 
> [auto build test WARNING on tip/auto-latest]
> [also build test WARNING on vhost/linux-next linus/master v5.6-rc5 next-20200310]
> [cannot apply to kvm/linux-next linux/master]
> [if your patch is applied to the wrong git tree, please drop us a note to help
> improve the system. BTW, we also suggest to use '--base' option to specify the
> base tree in git format-patch, please see https://stackoverflow.com/a/37406982]
> 
> url:    https://github.com/0day-ci/linux/commits/Peter-Xu/KVM-Dirty-ring-interface/20200310-070637
> base:   https://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git 12481c76713078054f2d043b3ce946e4814ac29f
> reproduce:
>         # apt-get install sparse
>         # sparse version: v0.6.1-174-g094d5a94-dirty
>         make ARCH=x86_64 allmodconfig
>         make C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__'
> 
> If you fix the issue, kindly add following tag
> Reported-by: kbuild test robot <lkp@intel.com>
> 
> 
> sparse warnings: (new ones prefixed by >>)
> 
>    arch/x86/kvm/x86.c:2599:38: sparse: sparse: incorrect type in argument 1 (different address spaces) @@    expected void const [noderef] <asn:1> * @@    got  const [noderef] <asn:1> * @@
>    arch/x86/kvm/x86.c:2599:38: sparse:    expected void const [noderef] <asn:1> *
>    arch/x86/kvm/x86.c:2599:38: sparse:    got unsigned char [usertype] *
>    arch/x86/kvm/x86.c:7501:15: sparse: sparse: incompatible types in comparison expression (different address spaces):
>    arch/x86/kvm/x86.c:7501:15: sparse:    struct kvm_apic_map [noderef] <asn:4> *
>    arch/x86/kvm/x86.c:7501:15: sparse:    struct kvm_apic_map *
> >> arch/x86/kvm/x86.c:9794:31: sparse: sparse: incorrect type in return expression (different address spaces) @@    expected void [noderef] <asn:1> * @@    got n:1> * @@

I'm not sure on how I can reproduce this locally, and also I'm not
very sure I understand this warning.  I'd be glad to know if anyone
knows...

If without further hints, I'll try to remove the __user for
__x86_set_memory_region() and use a cast on the callers next.

Thanks,

>    arch/x86/kvm/x86.c:9794:31: sparse:    expected void [noderef] <asn:1> *
>    arch/x86/kvm/x86.c:9794:31: sparse:    got void *
>    arch/x86/kvm/x86.c:9799:39: sparse: sparse: incorrect type in return expression (different address spaces) @@    expected void [noderef] <asn:1> * @@    got n:1> * @@
>    arch/x86/kvm/x86.c:9799:39: sparse:    expected void [noderef] <asn:1> *
>    arch/x86/kvm/x86.c:9799:39: sparse:    got void *
>    arch/x86/kvm/x86.c:9811:39: sparse: sparse: incorrect type in return expression (different address spaces) @@    expected void [noderef] <asn:1> * @@    got n:1> * @@
>    arch/x86/kvm/x86.c:9811:39: sparse:    expected void [noderef] <asn:1> *
>    arch/x86/kvm/x86.c:9811:39: sparse:    got void *
>    arch/x86/kvm/x86.c:9827:39: sparse: sparse: incorrect type in return expression (different address spaces) @@    expected void [noderef] <asn:1> * @@    got n:1> * @@
>    arch/x86/kvm/x86.c:9827:39: sparse:    expected void [noderef] <asn:1> *
>    arch/x86/kvm/x86.c:9827:39: sparse:    got void *
>    arch/x86/kvm/x86.c:9863:16: sparse: sparse: incompatible types in comparison expression (different address spaces):
>    arch/x86/kvm/x86.c:9863:16: sparse:    struct kvm_apic_map [noderef] <asn:4> *
>    arch/x86/kvm/x86.c:9863:16: sparse:    struct kvm_apic_map *
>    arch/x86/kvm/x86.c:9864:15: sparse: sparse: incompatible types in comparison expression (different address spaces):
>    arch/x86/kvm/x86.c:9864:15: sparse:    struct kvm_pmu_event_filter [noderef] <asn:4> *
>    arch/x86/kvm/x86.c:9864:15: sparse:    struct kvm_pmu_event_filter *
>    include/linux/srcu.h:179:9: sparse: sparse: context imbalance in 'vcpu_enter_guest' - unexpected unlock
> 
> vim +9794 arch/x86/kvm/x86.c
> 
>   9758	
>   9759	/**
>   9760	 * __x86_set_memory_region: Setup KVM internal memory slot
>   9761	 *
>   9762	 * @kvm: the kvm pointer to the VM.
>   9763	 * @id: the slot ID to setup.
>   9764	 * @gpa: the GPA to install the slot (unused when @size == 0).
>   9765	 * @size: the size of the slot. Set to zero to uninstall a slot.
>   9766	 *
>   9767	 * This function helps to setup a KVM internal memory slot.  Specify
>   9768	 * @size > 0 to install a new slot, while @size == 0 to uninstall a
>   9769	 * slot.  The return code can be one of the following:
>   9770	 *
>   9771	 *   - An error number if error happened, or,
>   9772	 *   - For installation: the HVA of the newly mapped memory slot, or,
>   9773	 *   - For uninstallation: zero if we successfully uninstall a slot.
>   9774	 *
>   9775	 * The caller should always use IS_ERR() to check the return value
>   9776	 * before use.  NOTE: KVM internal memory slots are guaranteed and
>   9777	 * won't change until the VM is destroyed. This is also true to the
>   9778	 * returned HVA when installing a new memory slot.  The HVA can be
>   9779	 * invalidated by either an errornous userspace program or a VM under
>   9780	 * destruction, however as long as we use __copy_{to|from}_user()
>   9781	 * properly upon the HVAs and handle the failure paths always then
>   9782	 * we're safe.
>   9783	 */
>   9784	void __user * __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa,
>   9785					      u32 size)
>   9786	{
>   9787		int i, r;
>   9788		unsigned long hva;
>   9789		struct kvm_memslots *slots = kvm_memslots(kvm);
>   9790		struct kvm_memory_slot *slot, old;
>   9791	
>   9792		/* Called with kvm->slots_lock held.  */
>   9793		if (WARN_ON(id >= KVM_MEM_SLOTS_NUM))
> > 9794			return ERR_PTR(-EINVAL);
>   9795	
>   9796		slot = id_to_memslot(slots, id);
>   9797		if (size) {
>   9798			if (slot->npages)
>   9799				return ERR_PTR(-EEXIST);
>   9800	
>   9801			/*
>   9802			 * MAP_SHARED to prevent internal slot pages from being moved
>   9803			 * by fork()/COW.
>   9804			 */
>   9805			hva = vm_mmap(NULL, 0, size, PROT_READ | PROT_WRITE,
>   9806				      MAP_SHARED | MAP_ANONYMOUS, 0);
>   9807			if (IS_ERR((void *)hva))
>   9808				return (void __user *)hva;
>   9809		} else {
>   9810			if (!slot->npages)
>   9811				return ERR_PTR(0);
>   9812	
>   9813			hva = 0;
>   9814		}
>   9815	
>   9816		old = *slot;
>   9817		for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
>   9818			struct kvm_userspace_memory_region m;
>   9819	
>   9820			m.slot = id | (i << 16);
>   9821			m.flags = 0;
>   9822			m.guest_phys_addr = gpa;
>   9823			m.userspace_addr = hva;
>   9824			m.memory_size = size;
>   9825			r = __kvm_set_memory_region(kvm, &m);
>   9826			if (r < 0)
>   9827				return ERR_PTR(r);
>   9828		}
>   9829	
>   9830		if (!size)
>   9831			vm_munmap(old.userspace_addr, old.npages * PAGE_SIZE);
>   9832	
>   9833		return (void __user *)hva;
>   9834	}
>   9835	EXPORT_SYMBOL_GPL(__x86_set_memory_region);
>   9836	
> 
> ---
> 0-DAY CI Kernel Test Service, Intel Corporation
> https://lists.01.org/hyperkitty/list/kbuild-all@lists.01.org
> 

-- 
Peter Xu


^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v6 03/14] KVM: X86: Don't track dirty for KVM_SET_[TSS_ADDR|IDENTITY_MAP_ADDR]
@ 2020-03-11 16:39       ` Peter Xu
  0 siblings, 0 replies; 35+ messages in thread
From: Peter Xu @ 2020-03-11 16:39 UTC (permalink / raw)
  To: kbuild-all

[-- Attachment #1: Type: text/plain, Size: 7285 bytes --]

On Wed, Mar 11, 2020 at 09:10:04AM +0800, kbuild test robot wrote:
> Hi Peter,
> 
> Thank you for the patch! Perhaps something to improve:
> 
> [auto build test WARNING on tip/auto-latest]
> [also build test WARNING on vhost/linux-next linus/master v5.6-rc5 next-20200310]
> [cannot apply to kvm/linux-next linux/master]
> [if your patch is applied to the wrong git tree, please drop us a note to help
> improve the system. BTW, we also suggest to use '--base' option to specify the
> base tree in git format-patch, please see https://stackoverflow.com/a/37406982]
> 
> url:    https://github.com/0day-ci/linux/commits/Peter-Xu/KVM-Dirty-ring-interface/20200310-070637
> base:   https://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git 12481c76713078054f2d043b3ce946e4814ac29f
> reproduce:
>         # apt-get install sparse
>         # sparse version: v0.6.1-174-g094d5a94-dirty
>         make ARCH=x86_64 allmodconfig
>         make C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__'
> 
> If you fix the issue, kindly add following tag
> Reported-by: kbuild test robot <lkp@intel.com>
> 
> 
> sparse warnings: (new ones prefixed by >>)
> 
>    arch/x86/kvm/x86.c:2599:38: sparse: sparse: incorrect type in argument 1 (different address spaces) @@    expected void const [noderef] <asn:1> * @@    got  const [noderef] <asn:1> * @@
>    arch/x86/kvm/x86.c:2599:38: sparse:    expected void const [noderef] <asn:1> *
>    arch/x86/kvm/x86.c:2599:38: sparse:    got unsigned char [usertype] *
>    arch/x86/kvm/x86.c:7501:15: sparse: sparse: incompatible types in comparison expression (different address spaces):
>    arch/x86/kvm/x86.c:7501:15: sparse:    struct kvm_apic_map [noderef] <asn:4> *
>    arch/x86/kvm/x86.c:7501:15: sparse:    struct kvm_apic_map *
> >> arch/x86/kvm/x86.c:9794:31: sparse: sparse: incorrect type in return expression (different address spaces) @@    expected void [noderef] <asn:1> * @@    got n:1> * @@

I'm not sure on how I can reproduce this locally, and also I'm not
very sure I understand this warning.  I'd be glad to know if anyone
knows...

If without further hints, I'll try to remove the __user for
__x86_set_memory_region() and use a cast on the callers next.

Thanks,

>    arch/x86/kvm/x86.c:9794:31: sparse:    expected void [noderef] <asn:1> *
>    arch/x86/kvm/x86.c:9794:31: sparse:    got void *
>    arch/x86/kvm/x86.c:9799:39: sparse: sparse: incorrect type in return expression (different address spaces) @@    expected void [noderef] <asn:1> * @@    got n:1> * @@
>    arch/x86/kvm/x86.c:9799:39: sparse:    expected void [noderef] <asn:1> *
>    arch/x86/kvm/x86.c:9799:39: sparse:    got void *
>    arch/x86/kvm/x86.c:9811:39: sparse: sparse: incorrect type in return expression (different address spaces) @@    expected void [noderef] <asn:1> * @@    got n:1> * @@
>    arch/x86/kvm/x86.c:9811:39: sparse:    expected void [noderef] <asn:1> *
>    arch/x86/kvm/x86.c:9811:39: sparse:    got void *
>    arch/x86/kvm/x86.c:9827:39: sparse: sparse: incorrect type in return expression (different address spaces) @@    expected void [noderef] <asn:1> * @@    got n:1> * @@
>    arch/x86/kvm/x86.c:9827:39: sparse:    expected void [noderef] <asn:1> *
>    arch/x86/kvm/x86.c:9827:39: sparse:    got void *
>    arch/x86/kvm/x86.c:9863:16: sparse: sparse: incompatible types in comparison expression (different address spaces):
>    arch/x86/kvm/x86.c:9863:16: sparse:    struct kvm_apic_map [noderef] <asn:4> *
>    arch/x86/kvm/x86.c:9863:16: sparse:    struct kvm_apic_map *
>    arch/x86/kvm/x86.c:9864:15: sparse: sparse: incompatible types in comparison expression (different address spaces):
>    arch/x86/kvm/x86.c:9864:15: sparse:    struct kvm_pmu_event_filter [noderef] <asn:4> *
>    arch/x86/kvm/x86.c:9864:15: sparse:    struct kvm_pmu_event_filter *
>    include/linux/srcu.h:179:9: sparse: sparse: context imbalance in 'vcpu_enter_guest' - unexpected unlock
> 
> vim +9794 arch/x86/kvm/x86.c
> 
>   9758	
>   9759	/**
>   9760	 * __x86_set_memory_region: Setup KVM internal memory slot
>   9761	 *
>   9762	 * @kvm: the kvm pointer to the VM.
>   9763	 * @id: the slot ID to setup.
>   9764	 * @gpa: the GPA to install the slot (unused when @size == 0).
>   9765	 * @size: the size of the slot. Set to zero to uninstall a slot.
>   9766	 *
>   9767	 * This function helps to setup a KVM internal memory slot.  Specify
>   9768	 * @size > 0 to install a new slot, while @size == 0 to uninstall a
>   9769	 * slot.  The return code can be one of the following:
>   9770	 *
>   9771	 *   - An error number if error happened, or,
>   9772	 *   - For installation: the HVA of the newly mapped memory slot, or,
>   9773	 *   - For uninstallation: zero if we successfully uninstall a slot.
>   9774	 *
>   9775	 * The caller should always use IS_ERR() to check the return value
>   9776	 * before use.  NOTE: KVM internal memory slots are guaranteed and
>   9777	 * won't change until the VM is destroyed. This is also true to the
>   9778	 * returned HVA when installing a new memory slot.  The HVA can be
>   9779	 * invalidated by either an errornous userspace program or a VM under
>   9780	 * destruction, however as long as we use __copy_{to|from}_user()
>   9781	 * properly upon the HVAs and handle the failure paths always then
>   9782	 * we're safe.
>   9783	 */
>   9784	void __user * __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa,
>   9785					      u32 size)
>   9786	{
>   9787		int i, r;
>   9788		unsigned long hva;
>   9789		struct kvm_memslots *slots = kvm_memslots(kvm);
>   9790		struct kvm_memory_slot *slot, old;
>   9791	
>   9792		/* Called with kvm->slots_lock held.  */
>   9793		if (WARN_ON(id >= KVM_MEM_SLOTS_NUM))
> > 9794			return ERR_PTR(-EINVAL);
>   9795	
>   9796		slot = id_to_memslot(slots, id);
>   9797		if (size) {
>   9798			if (slot->npages)
>   9799				return ERR_PTR(-EEXIST);
>   9800	
>   9801			/*
>   9802			 * MAP_SHARED to prevent internal slot pages from being moved
>   9803			 * by fork()/COW.
>   9804			 */
>   9805			hva = vm_mmap(NULL, 0, size, PROT_READ | PROT_WRITE,
>   9806				      MAP_SHARED | MAP_ANONYMOUS, 0);
>   9807			if (IS_ERR((void *)hva))
>   9808				return (void __user *)hva;
>   9809		} else {
>   9810			if (!slot->npages)
>   9811				return ERR_PTR(0);
>   9812	
>   9813			hva = 0;
>   9814		}
>   9815	
>   9816		old = *slot;
>   9817		for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
>   9818			struct kvm_userspace_memory_region m;
>   9819	
>   9820			m.slot = id | (i << 16);
>   9821			m.flags = 0;
>   9822			m.guest_phys_addr = gpa;
>   9823			m.userspace_addr = hva;
>   9824			m.memory_size = size;
>   9825			r = __kvm_set_memory_region(kvm, &m);
>   9826			if (r < 0)
>   9827				return ERR_PTR(r);
>   9828		}
>   9829	
>   9830		if (!size)
>   9831			vm_munmap(old.userspace_addr, old.npages * PAGE_SIZE);
>   9832	
>   9833		return (void __user *)hva;
>   9834	}
>   9835	EXPORT_SYMBOL_GPL(__x86_set_memory_region);
>   9836	
> 
> ---
> 0-DAY CI Kernel Test Service, Intel Corporation
> https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
> 

-- 
Peter Xu

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v6 03/14] KVM: X86: Don't track dirty for KVM_SET_[TSS_ADDR|IDENTITY_MAP_ADDR]
  2020-03-11 16:39       ` Peter Xu
@ 2020-03-11 17:09         ` Sean Christopherson
  -1 siblings, 0 replies; 35+ messages in thread
From: Sean Christopherson @ 2020-03-11 17:09 UTC (permalink / raw)
  To: Peter Xu
  Cc: kbuild test robot, kbuild-all, linux-kernel, kvm, Yan Zhao,
	Jason Wang, Alex Williamson, Vitaly Kuznetsov,
	Dr . David Alan Gilbert, Christophe de Dinechin,
	Michael S . Tsirkin, Kevin Tian, Paolo Bonzini

On Wed, Mar 11, 2020 at 12:39:06PM -0400, Peter Xu wrote:
> On Wed, Mar 11, 2020 at 09:10:04AM +0800, kbuild test robot wrote:
> > Hi Peter,
> > 
> > Thank you for the patch! Perhaps something to improve:
> > 
> > [auto build test WARNING on tip/auto-latest]
> > [also build test WARNING on vhost/linux-next linus/master v5.6-rc5 next-20200310]
> > [cannot apply to kvm/linux-next linux/master]
> > [if your patch is applied to the wrong git tree, please drop us a note to help
> > improve the system. BTW, we also suggest to use '--base' option to specify the
> > base tree in git format-patch, please see https://stackoverflow.com/a/37406982]
> > 
> > url:    https://github.com/0day-ci/linux/commits/Peter-Xu/KVM-Dirty-ring-interface/20200310-070637
> > base:   https://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git 12481c76713078054f2d043b3ce946e4814ac29f
> > reproduce:
> >         # apt-get install sparse
> >         # sparse version: v0.6.1-174-g094d5a94-dirty
> >         make ARCH=x86_64 allmodconfig
> >         make C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__'
> > 
> > If you fix the issue, kindly add following tag
> > Reported-by: kbuild test robot <lkp@intel.com>
> > 
> > 
> > sparse warnings: (new ones prefixed by >>)
> > 
> >    arch/x86/kvm/x86.c:2599:38: sparse: sparse: incorrect type in argument 1 (different address spaces) @@    expected void const [noderef] <asn:1> * @@    got  const [noderef] <asn:1> * @@
> >    arch/x86/kvm/x86.c:2599:38: sparse:    expected void const [noderef] <asn:1> *
> >    arch/x86/kvm/x86.c:2599:38: sparse:    got unsigned char [usertype] *
> >    arch/x86/kvm/x86.c:7501:15: sparse: sparse: incompatible types in comparison expression (different address spaces):
> >    arch/x86/kvm/x86.c:7501:15: sparse:    struct kvm_apic_map [noderef] <asn:4> *
> >    arch/x86/kvm/x86.c:7501:15: sparse:    struct kvm_apic_map *
> > >> arch/x86/kvm/x86.c:9794:31: sparse: sparse: incorrect type in return expression (different address spaces) @@    expected void [noderef] <asn:1> * @@    got n:1> * @@
> 
> I'm not sure on how I can reproduce this locally, and also I'm not
> very sure I understand this warning.  I'd be glad to know if anyone
> knows...
> 
> If without further hints, I'll try to remove the __user for
> __x86_set_memory_region() and use a cast on the callers next.

Ah, it's complaining that the ERR_PTR() returns in __x86_set_memory_region()
aren't explicitly casting to a __user pointer.

Part of me wonders if something along the lines of your original approach
of keeping the "int" return and passing a "void __user **p_hva" would be
cleaner overall, as opposed to having to cast everywhere.  The diff would
certainly be smaller.  E.g.

int __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, u32 size,
			    void __user **p_hva)
{
	...

	if (p_hva)
		*p_hva = (void __user *)hva;

        return 0;
}

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v6 03/14] KVM: X86: Don't track dirty for KVM_SET_[TSS_ADDR|IDENTITY_MAP_ADDR]
@ 2020-03-11 17:09         ` Sean Christopherson
  0 siblings, 0 replies; 35+ messages in thread
From: Sean Christopherson @ 2020-03-11 17:09 UTC (permalink / raw)
  To: kbuild-all

[-- Attachment #1: Type: text/plain, Size: 2948 bytes --]

On Wed, Mar 11, 2020 at 12:39:06PM -0400, Peter Xu wrote:
> On Wed, Mar 11, 2020 at 09:10:04AM +0800, kbuild test robot wrote:
> > Hi Peter,
> > 
> > Thank you for the patch! Perhaps something to improve:
> > 
> > [auto build test WARNING on tip/auto-latest]
> > [also build test WARNING on vhost/linux-next linus/master v5.6-rc5 next-20200310]
> > [cannot apply to kvm/linux-next linux/master]
> > [if your patch is applied to the wrong git tree, please drop us a note to help
> > improve the system. BTW, we also suggest to use '--base' option to specify the
> > base tree in git format-patch, please see https://stackoverflow.com/a/37406982]
> > 
> > url:    https://github.com/0day-ci/linux/commits/Peter-Xu/KVM-Dirty-ring-interface/20200310-070637
> > base:   https://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git 12481c76713078054f2d043b3ce946e4814ac29f
> > reproduce:
> >         # apt-get install sparse
> >         # sparse version: v0.6.1-174-g094d5a94-dirty
> >         make ARCH=x86_64 allmodconfig
> >         make C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__'
> > 
> > If you fix the issue, kindly add following tag
> > Reported-by: kbuild test robot <lkp@intel.com>
> > 
> > 
> > sparse warnings: (new ones prefixed by >>)
> > 
> >    arch/x86/kvm/x86.c:2599:38: sparse: sparse: incorrect type in argument 1 (different address spaces) @@    expected void const [noderef] <asn:1> * @@    got  const [noderef] <asn:1> * @@
> >    arch/x86/kvm/x86.c:2599:38: sparse:    expected void const [noderef] <asn:1> *
> >    arch/x86/kvm/x86.c:2599:38: sparse:    got unsigned char [usertype] *
> >    arch/x86/kvm/x86.c:7501:15: sparse: sparse: incompatible types in comparison expression (different address spaces):
> >    arch/x86/kvm/x86.c:7501:15: sparse:    struct kvm_apic_map [noderef] <asn:4> *
> >    arch/x86/kvm/x86.c:7501:15: sparse:    struct kvm_apic_map *
> > >> arch/x86/kvm/x86.c:9794:31: sparse: sparse: incorrect type in return expression (different address spaces) @@    expected void [noderef] <asn:1> * @@    got n:1> * @@
> 
> I'm not sure on how I can reproduce this locally, and also I'm not
> very sure I understand this warning.  I'd be glad to know if anyone
> knows...
> 
> If without further hints, I'll try to remove the __user for
> __x86_set_memory_region() and use a cast on the callers next.

Ah, it's complaining that the ERR_PTR() returns in __x86_set_memory_region()
aren't explicitly casting to a __user pointer.

Part of me wonders if something along the lines of your original approach
of keeping the "int" return and passing a "void __user **p_hva" would be
cleaner overall, as opposed to having to cast everywhere.  The diff would
certainly be smaller.  E.g.

int __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, u32 size,
			    void __user **p_hva)
{
	...

	if (p_hva)
		*p_hva = (void __user *)hva;

        return 0;
}

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v6 10/14] KVM: selftests: Use a single binary for dirty/clear log test
  2020-03-10  8:10   ` Andrew Jones
@ 2020-03-11 17:43     ` Peter Xu
  2020-03-11 18:47       ` Andrew Jones
  0 siblings, 1 reply; 35+ messages in thread
From: Peter Xu @ 2020-03-11 17:43 UTC (permalink / raw)
  To: Andrew Jones
  Cc: linux-kernel, kvm, Yan Zhao, Jason Wang, Alex Williamson,
	Vitaly Kuznetsov, Dr . David Alan Gilbert,
	Christophe de Dinechin, Michael S . Tsirkin, Kevin Tian,
	Paolo Bonzini, Sean Christopherson

On Tue, Mar 10, 2020 at 09:10:02AM +0100, Andrew Jones wrote:
> On Mon, Mar 09, 2020 at 06:25:19PM -0400, Peter Xu wrote:
> > Remove 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.
> > 
> > Signed-off-by: Peter Xu <peterx@redhat.com>
> > ---
> >  tools/testing/selftests/kvm/Makefile          |   2 -
> >  .../selftests/kvm/clear_dirty_log_test.c      |   2 -
> >  tools/testing/selftests/kvm/dirty_log_test.c  | 169 +++++++++++++++---
> >  3 files changed, 146 insertions(+), 27 deletions(-)
> >  delete mode 100644 tools/testing/selftests/kvm/clear_dirty_log_test.c
> > 
> > diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile
> > index d91c53b726e6..941bfcd48eaa 100644
> > --- a/tools/testing/selftests/kvm/Makefile
> > +++ b/tools/testing/selftests/kvm/Makefile
> > @@ -27,11 +27,9 @@ 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 += x86_64/svm_vmcall_test
> > -TEST_GEN_PROGS_x86_64 += clear_dirty_log_test
> >  TEST_GEN_PROGS_x86_64 += dirty_log_test
> >  TEST_GEN_PROGS_x86_64 += kvm_create_max_vcpus
> >  
> > -TEST_GEN_PROGS_aarch64 += clear_dirty_log_test
> >  TEST_GEN_PROGS_aarch64 += dirty_log_test
> >  TEST_GEN_PROGS_aarch64 += kvm_create_max_vcpus
> >  
> > diff --git a/tools/testing/selftests/kvm/clear_dirty_log_test.c b/tools/testing/selftests/kvm/clear_dirty_log_test.c
> > deleted file mode 100644
> > index 749336937d37..000000000000
> > --- a/tools/testing/selftests/kvm/clear_dirty_log_test.c
> > +++ /dev/null
> > @@ -1,2 +0,0 @@
> > -#define USE_CLEAR_DIRTY_LOG
> > -#include "dirty_log_test.c"
> > diff --git a/tools/testing/selftests/kvm/dirty_log_test.c b/tools/testing/selftests/kvm/dirty_log_test.c
> > index 3c0ffd34b3b0..642886394e34 100644
> > --- a/tools/testing/selftests/kvm/dirty_log_test.c
> > +++ b/tools/testing/selftests/kvm/dirty_log_test.c
> > @@ -128,6 +128,73 @@ static uint64_t host_dirty_count;
> >  static uint64_t host_clear_count;
> >  static uint64_t host_track_next_count;
> >  
> > +enum log_mode_t {
> > +	/* Only use KVM_GET_DIRTY_LOG for logging */
> > +	LOG_MODE_DIRTY_LOG = 0,
> > +
> > +	/* Use both KVM_[GET|CLEAR]_DIRTY_LOG for logging */
> > +	LOG_MODE_CLEAR_LOG = 1,
> > +
> > +	LOG_MODE_NUM,
> > +
> > +	/* Run all supported modes */
> > +	LOG_MODE_ALL = LOG_MODE_NUM,
> > +};
> > +
> > +/* Mode of logging to test.  Default is to run all supported modes */
> > +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 bool clear_log_supported(void)
> > +{
> > +	return kvm_check_cap(KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2);
> > +}
> > +
> > +static void clear_log_create_vm_done(struct kvm_vm *vm)
> > +{
> > +	struct kvm_enable_cap cap = {};
> > +
> > +	cap.cap = KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2;
> > +	cap.args[0] = 1;
> > +	vm_enable_cap(vm, &cap);
> > +}
> > +
> > +static void dirty_log_collect_dirty_pages(struct kvm_vm *vm, int slot,
> > +					  void *bitmap, uint32_t num_pages)
> > +{
> > +	kvm_vm_get_dirty_log(vm, slot, bitmap);
> > +}
> > +
> > +static void clear_log_collect_dirty_pages(struct kvm_vm *vm, int slot,
> > +					  void *bitmap, uint32_t num_pages)
> > +{
> > +	kvm_vm_get_dirty_log(vm, slot, bitmap);
> > +	kvm_vm_clear_dirty_log(vm, slot, bitmap, 0, num_pages);
> > +}
> > +
> > +struct log_mode {
> > +	const char *name;
> > +	/* Return true if this mode is supported, otherwise false */
> > +	bool (*supported)(void);
> > +	/* Hook when the vm creation is done (before vcpu creation) */
> > +	void (*create_vm_done)(struct kvm_vm *vm);
> > +	/* 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);
> > +} log_modes[LOG_MODE_NUM] = {
> > +	{
> > +		.name = "dirty-log",
> > +		.collect_dirty_pages = dirty_log_collect_dirty_pages,
> > +	},
> > +	{
> > +		.name = "clear-log",
> > +		.supported = clear_log_supported,
> > +		.create_vm_done = clear_log_create_vm_done,
> > +		.collect_dirty_pages = clear_log_collect_dirty_pages,
> > +	},
> > +};
> > +
> >  /*
> >   * We use this bitmap to track some pages that should have its dirty
> >   * bit set in the _next_ iteration.  For example, if we detected the
> > @@ -137,6 +204,43 @@ static uint64_t host_track_next_count;
> >   */
> >  static unsigned long *host_bmap_track;
> >  
> > +static void log_modes_dump(void)
> > +{
> > +	int i;
> > +
> > +	for (i = 0; i < LOG_MODE_NUM; i++)
> > +		printf("%s, ", log_modes[i].name);
> > +	puts("\b\b  \b\b");
> 
> This will be ugly when the output is redirected to a file.
> How about just
> 
> printf("%s", log_modes[0].name);
> for (i = 1; i < LOG_MODE_NUM; i++)
>   printf(", %s", log_modes[i].name);
> printf("\n");

Will do.

> 
> > +}
> > +
> > +static bool log_mode_supported(void)
> > +{
> > +	struct log_mode *mode = &log_modes[host_log_mode];
> > +
> > +	if (mode->supported)
> > +		return mode->supported();
> > +
> > +	return true;
> > +}
> > +
> > +static void log_mode_create_vm_done(struct kvm_vm *vm)
> > +{
> > +	struct log_mode *mode = &log_modes[host_log_mode];
> > +
> > +	if (mode->create_vm_done)
> > +		mode->create_vm_done(vm);
> > +}
> > +
> > +static void log_mode_collect_dirty_pages(struct kvm_vm *vm, int slot,
> > +					 void *bitmap, uint32_t num_pages)
> > +{
> > +	struct log_mode *mode = &log_modes[host_log_mode];
> > +
> > +	TEST_ASSERT(mode->collect_dirty_pages != NULL,
> > +		    "collect_dirty_pages() is required for any log mode!");
> > +	mode->collect_dirty_pages(vm, slot, bitmap, num_pages);
> > +}
> > +
> >  static void generate_random_array(uint64_t *guest_array, uint64_t size)
> >  {
> >  	uint64_t i;
> > @@ -257,6 +361,7 @@ static struct kvm_vm *create_vm(enum vm_guest_mode mode, uint32_t vcpuid,
> >  #ifdef __x86_64__
> >  	vm_create_irqchip(vm);
> >  #endif
> > +	log_mode_create_vm_done(vm);
> >  	vm_vcpu_add_default(vm, vcpuid, guest_code);
> >  	return vm;
> >  }
> > @@ -271,6 +376,12 @@ static void run_test(enum vm_guest_mode mode, unsigned long iterations,
> >  	struct kvm_vm *vm;
> >  	unsigned long *bmap;
> >  
> > +	if (!log_mode_supported()) {
> > +		fprintf(stderr, "Log mode '%s' not supported, skip\n",
> > +			log_modes[host_log_mode].name);
> 
> I think kvm selftests needs a skip_test() function that outputs a more
> consistent test skip message. It seems we mostly do

Yep, I can introduce one.

> 
> fprintf(stderr, "%s, skipping test\n", custom_message);
> 
> but here we have ', skip'. Also, I see a few places were we output
> skipping to stderr and others to stdout. I think I like stdout better.

Sure.

> 
> > +		return;
> > +	}
> > +
> >  	/*
> >  	 * We reserve page table for 2 times of extra dirty mem which
> >  	 * will definitely cover the original (1G+) test range.  Here
> > @@ -316,14 +427,6 @@ static void run_test(enum vm_guest_mode mode, unsigned long iterations,
> >  	bmap = bitmap_alloc(host_num_pages);
> >  	host_bmap_track = bitmap_alloc(host_num_pages);
> >  
> > -#ifdef USE_CLEAR_DIRTY_LOG
> > -	struct kvm_enable_cap cap = {};
> > -
> > -	cap.cap = KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2;
> > -	cap.args[0] = 1;
> > -	vm_enable_cap(vm, &cap);
> > -#endif
> > -
> >  	/* Add an extra memory slot for testing dirty logging */
> >  	vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS,
> >  				    guest_test_phys_mem,
> > @@ -364,11 +467,8 @@ static void run_test(enum vm_guest_mode mode, unsigned long iterations,
> >  	while (iteration < iterations) {
> >  		/* Give the vcpu thread some time to dirty some pages */
> >  		usleep(interval * 1000);
> > -		kvm_vm_get_dirty_log(vm, TEST_MEM_SLOT_INDEX, bmap);
> > -#ifdef USE_CLEAR_DIRTY_LOG
> > -		kvm_vm_clear_dirty_log(vm, TEST_MEM_SLOT_INDEX, bmap, 0,
> > -				       host_num_pages);
> > -#endif
> > +		log_mode_collect_dirty_pages(vm, TEST_MEM_SLOT_INDEX,
> > +					     bmap, host_num_pages);
> >  		vm_dirty_log_verify(bmap);
> >  		iteration++;
> >  		sync_global_to_guest(vm, iteration);
> > @@ -413,6 +513,9 @@ static void help(char *name)
> >  	       TEST_HOST_LOOP_INTERVAL);
> >  	printf(" -p: specify guest physical test memory offset\n"
> >  	       "     Warning: a low offset can conflict with the loaded test code.\n");
> > +	printf(" -M: specify the host logging mode "
> > +	       "(default: run all log modes).  Supported modes: \n\t");
> > +	log_modes_dump();
> >  	printf(" -m: specify the guest mode ID to test "
> >  	       "(default: test all supported modes)\n"
> >  	       "     This option may be used multiple times.\n"
> > @@ -432,18 +535,11 @@ int main(int argc, char *argv[])
> >  	bool mode_selected = false;
> >  	uint64_t phys_offset = 0;
> >  	unsigned int mode;
> > -	int opt, i;
> > +	int opt, i, j;
> >  #ifdef __aarch64__
> >  	unsigned int host_ipa_limit;
> >  #endif
> >  
> > -#ifdef USE_CLEAR_DIRTY_LOG
> > -	if (!kvm_check_cap(KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2)) {
> > -		fprintf(stderr, "KVM_CLEAR_DIRTY_LOG not available, skipping tests\n");
> > -		exit(KSFT_SKIP);
> > -	}
> > -#endif
> > -
> >  #ifdef __x86_64__
> >  	vm_guest_mode_params_init(VM_MODE_PXXV48_4K, true, true);
> >  #endif
> > @@ -463,7 +559,7 @@ int main(int argc, char *argv[])
> >  	vm_guest_mode_params_init(VM_MODE_P40V48_4K, true, true);
> >  #endif
> >  
> > -	while ((opt = getopt(argc, argv, "hi:I:p:m:")) != -1) {
> > +	while ((opt = getopt(argc, argv, "hi:I:p:m:M:")) != -1) {
> >  		switch (opt) {
> >  		case 'i':
> >  			iterations = strtol(optarg, NULL, 10);
> > @@ -485,6 +581,22 @@ int main(int argc, char *argv[])
> >  				    "Guest mode ID %d too big", mode);
> >  			vm_guest_mode_params[mode].enabled = true;
> >  			break;
> > +		case 'M':
> 
> Can also add
> 
> if (!strcmp(optarg, "all"))
>   host_log_mode_option = LOG_MODE_ALL;

Sure.

> 
> > +			for (i = 0; i < LOG_MODE_NUM; i++) {
> > +				if (!strcmp(optarg, log_modes[i].name)) {
> > +					DEBUG("Setting log mode to: '%s'\n",
> > +					      optarg);
> 
> Basing this on kvm/queue won't work as DEBUG() no longer exists. This
> looks like a pr_info().

I'll rebase to kvm/queue and see...

> 
> > +					host_log_mode_option = i;
> > +					break;
> > +				}
> > +			}
> > +			if (i == LOG_MODE_NUM) {
> > +				printf("Log mode '%s' is invalid.  "
> > +				       "Please choose from: ", optarg);
> > +				log_modes_dump();
> > +				exit(-1);
> 
> Exit code of 255? Probably just want exit(1);

Sure.  Thanks!

-- 
Peter Xu


^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v6 12/14] KVM: selftests: Add dirty ring buffer test
  2020-03-10  8:18   ` Andrew Jones
@ 2020-03-11 17:44     ` Peter Xu
  0 siblings, 0 replies; 35+ messages in thread
From: Peter Xu @ 2020-03-11 17:44 UTC (permalink / raw)
  To: Andrew Jones
  Cc: linux-kernel, kvm, Yan Zhao, Jason Wang, Alex Williamson,
	Vitaly Kuznetsov, Dr . David Alan Gilbert,
	Christophe de Dinechin, Michael S . Tsirkin, Kevin Tian,
	Paolo Bonzini, Sean Christopherson

On Tue, Mar 10, 2020 at 09:18:47AM +0100, Andrew Jones wrote:
> On Mon, Mar 09, 2020 at 06:25:29PM -0400, Peter Xu wrote:
> > +void *vcpu_map_dirty_ring(struct kvm_vm *vm, uint32_t vcpuid)
> > +{
> > +	struct vcpu *vcpu;
> > +	uint32_t size = vm->dirty_ring_size;
> > +
> > +	TEST_ASSERT(size > 0, "Should enable dirty ring first");
> > +
> > +	vcpu = vcpu_find(vm, vcpuid);
> > +
> > +	TEST_ASSERT(vcpu, "Cannot find vcpu %u", vcpuid);
> > +
> > +	if (!vcpu->dirty_gfns) {
> > +		void *addr;
> > +
> > +		addr = mmap(NULL, size, PROT_READ,
> > +			    MAP_PRIVATE, vcpu->fd,
> > +			    vm->page_size * KVM_DIRTY_LOG_PAGE_OFFSET);
> > +		TEST_ASSERT(addr == MAP_FAILED, "Dirty ring mapped private");
> > +
> > +		addr = mmap(NULL, size, PROT_READ | PROT_EXEC,
> > +			    MAP_PRIVATE, vcpu->fd,
> > +			    vm->page_size * KVM_DIRTY_LOG_PAGE_OFFSET);
> > +		TEST_ASSERT(addr == MAP_FAILED, "Dirty ring mapped exec");
> > +
> > +		addr = mmap(NULL, size, PROT_READ | PROT_WRITE,
> > +			    MAP_SHARED, vcpu->fd,
> > +			    vm->page_size * KVM_DIRTY_LOG_PAGE_OFFSET);
> 
> No TEST_ASSERT for this mmap?

It'll be used (verified) later. :) Though never harm to add one!

Thanks,
> 
> > +
> > +		vcpu->dirty_gfns = addr;
> > +		vcpu->dirty_gfns_count = size / sizeof(struct kvm_dirty_gfn);
> > +	}
> > +
> > +	return vcpu->dirty_gfns;
> > +}
> 

-- 
Peter Xu


^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v6 13/14] KVM: selftests: Let dirty_log_test async for dirty ring test
  2020-03-10  8:27   ` Andrew Jones
@ 2020-03-11 17:45     ` Peter Xu
  0 siblings, 0 replies; 35+ messages in thread
From: Peter Xu @ 2020-03-11 17:45 UTC (permalink / raw)
  To: Andrew Jones
  Cc: linux-kernel, kvm, Yan Zhao, Jason Wang, Alex Williamson,
	Vitaly Kuznetsov, Dr . David Alan Gilbert,
	Christophe de Dinechin, Michael S . Tsirkin, Kevin Tian,
	Paolo Bonzini, Sean Christopherson

On Tue, Mar 10, 2020 at 09:27:04AM +0100, Andrew Jones wrote:
> >  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;
> > +pthread_t vcpu_thread;
> > +
> > +/* Only way to pass this to the signal handler */
> > +struct kvm_vm *current_vm;
> 
> nit: above two new globals could be static

Will do.

-- 
Peter Xu


^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v6 10/14] KVM: selftests: Use a single binary for dirty/clear log test
  2020-03-11 17:43     ` Peter Xu
@ 2020-03-11 18:47       ` Andrew Jones
  0 siblings, 0 replies; 35+ messages in thread
From: Andrew Jones @ 2020-03-11 18:47 UTC (permalink / raw)
  To: Peter Xu
  Cc: linux-kernel, kvm, Yan Zhao, Jason Wang, Alex Williamson,
	Vitaly Kuznetsov, Dr . David Alan Gilbert,
	Christophe de Dinechin, Michael S . Tsirkin, Kevin Tian,
	Paolo Bonzini, Sean Christopherson

On Wed, Mar 11, 2020 at 01:43:24PM -0400, Peter Xu wrote:
> > >  
> > > +	if (!log_mode_supported()) {
> > > +		fprintf(stderr, "Log mode '%s' not supported, skip\n",
> > > +			log_modes[host_log_mode].name);
> > 
> > I think kvm selftests needs a skip_test() function that outputs a more
> > consistent test skip message. It seems we mostly do
> 
> Yep, I can introduce one.

I already did. Right after suggesting it.

https://www.spinics.net/lists/kvm/msg209545.html

Thanks,
drew


^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v6 03/14] KVM: X86: Don't track dirty for KVM_SET_[TSS_ADDR|IDENTITY_MAP_ADDR]
  2020-03-11 17:09         ` Sean Christopherson
@ 2020-03-18 20:04           ` Peter Xu
  -1 siblings, 0 replies; 35+ messages in thread
From: Peter Xu @ 2020-03-18 20:04 UTC (permalink / raw)
  To: Sean Christopherson
  Cc: kbuild test robot, kbuild-all, linux-kernel, kvm, Yan Zhao,
	Jason Wang, Alex Williamson, Vitaly Kuznetsov,
	Dr . David Alan Gilbert, Christophe de Dinechin,
	Michael S . Tsirkin, Kevin Tian, Paolo Bonzini

On Wed, Mar 11, 2020 at 10:09:40AM -0700, Sean Christopherson wrote:
> On Wed, Mar 11, 2020 at 12:39:06PM -0400, Peter Xu wrote:
> > On Wed, Mar 11, 2020 at 09:10:04AM +0800, kbuild test robot wrote:
> > > Hi Peter,
> > > 
> > > Thank you for the patch! Perhaps something to improve:
> > > 
> > > [auto build test WARNING on tip/auto-latest]
> > > [also build test WARNING on vhost/linux-next linus/master v5.6-rc5 next-20200310]
> > > [cannot apply to kvm/linux-next linux/master]
> > > [if your patch is applied to the wrong git tree, please drop us a note to help
> > > improve the system. BTW, we also suggest to use '--base' option to specify the
> > > base tree in git format-patch, please see https://stackoverflow.com/a/37406982]
> > > 
> > > url:    https://github.com/0day-ci/linux/commits/Peter-Xu/KVM-Dirty-ring-interface/20200310-070637
> > > base:   https://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git 12481c76713078054f2d043b3ce946e4814ac29f
> > > reproduce:
> > >         # apt-get install sparse
> > >         # sparse version: v0.6.1-174-g094d5a94-dirty
> > >         make ARCH=x86_64 allmodconfig
> > >         make C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__'
> > > 
> > > If you fix the issue, kindly add following tag
> > > Reported-by: kbuild test robot <lkp@intel.com>
> > > 
> > > 
> > > sparse warnings: (new ones prefixed by >>)
> > > 
> > >    arch/x86/kvm/x86.c:2599:38: sparse: sparse: incorrect type in argument 1 (different address spaces) @@    expected void const [noderef] <asn:1> * @@    got  const [noderef] <asn:1> * @@
> > >    arch/x86/kvm/x86.c:2599:38: sparse:    expected void const [noderef] <asn:1> *
> > >    arch/x86/kvm/x86.c:2599:38: sparse:    got unsigned char [usertype] *
> > >    arch/x86/kvm/x86.c:7501:15: sparse: sparse: incompatible types in comparison expression (different address spaces):
> > >    arch/x86/kvm/x86.c:7501:15: sparse:    struct kvm_apic_map [noderef] <asn:4> *
> > >    arch/x86/kvm/x86.c:7501:15: sparse:    struct kvm_apic_map *
> > > >> arch/x86/kvm/x86.c:9794:31: sparse: sparse: incorrect type in return expression (different address spaces) @@    expected void [noderef] <asn:1> * @@    got n:1> * @@
> > 
> > I'm not sure on how I can reproduce this locally, and also I'm not
> > very sure I understand this warning.  I'd be glad to know if anyone
> > knows...
> > 
> > If without further hints, I'll try to remove the __user for
> > __x86_set_memory_region() and use a cast on the callers next.
> 
> Ah, it's complaining that the ERR_PTR() returns in __x86_set_memory_region()
> aren't explicitly casting to a __user pointer.
> 
> Part of me wonders if something along the lines of your original approach
> of keeping the "int" return and passing a "void __user **p_hva" would be
> cleaner overall, as opposed to having to cast everywhere.  The diff would
> certainly be smaller.  E.g.
> 
> int __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, u32 size,
> 			    void __user **p_hva)
> {
> 	...
> 
> 	if (p_hva)
> 		*p_hva = (void __user *)hva;
> 
>         return 0;
> }

Returning an adress still has some advantage on one less param.  To
avoid going back and forth, I defined ERR_PTR_USR() and used there to
fix the sparse warnings.  Thanks,

-- 
Peter Xu


^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v6 03/14] KVM: X86: Don't track dirty for KVM_SET_[TSS_ADDR|IDENTITY_MAP_ADDR]
@ 2020-03-18 20:04           ` Peter Xu
  0 siblings, 0 replies; 35+ messages in thread
From: Peter Xu @ 2020-03-18 20:04 UTC (permalink / raw)
  To: kbuild-all

[-- Attachment #1: Type: text/plain, Size: 3334 bytes --]

On Wed, Mar 11, 2020 at 10:09:40AM -0700, Sean Christopherson wrote:
> On Wed, Mar 11, 2020 at 12:39:06PM -0400, Peter Xu wrote:
> > On Wed, Mar 11, 2020 at 09:10:04AM +0800, kbuild test robot wrote:
> > > Hi Peter,
> > > 
> > > Thank you for the patch! Perhaps something to improve:
> > > 
> > > [auto build test WARNING on tip/auto-latest]
> > > [also build test WARNING on vhost/linux-next linus/master v5.6-rc5 next-20200310]
> > > [cannot apply to kvm/linux-next linux/master]
> > > [if your patch is applied to the wrong git tree, please drop us a note to help
> > > improve the system. BTW, we also suggest to use '--base' option to specify the
> > > base tree in git format-patch, please see https://stackoverflow.com/a/37406982]
> > > 
> > > url:    https://github.com/0day-ci/linux/commits/Peter-Xu/KVM-Dirty-ring-interface/20200310-070637
> > > base:   https://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git 12481c76713078054f2d043b3ce946e4814ac29f
> > > reproduce:
> > >         # apt-get install sparse
> > >         # sparse version: v0.6.1-174-g094d5a94-dirty
> > >         make ARCH=x86_64 allmodconfig
> > >         make C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__'
> > > 
> > > If you fix the issue, kindly add following tag
> > > Reported-by: kbuild test robot <lkp@intel.com>
> > > 
> > > 
> > > sparse warnings: (new ones prefixed by >>)
> > > 
> > >    arch/x86/kvm/x86.c:2599:38: sparse: sparse: incorrect type in argument 1 (different address spaces) @@    expected void const [noderef] <asn:1> * @@    got  const [noderef] <asn:1> * @@
> > >    arch/x86/kvm/x86.c:2599:38: sparse:    expected void const [noderef] <asn:1> *
> > >    arch/x86/kvm/x86.c:2599:38: sparse:    got unsigned char [usertype] *
> > >    arch/x86/kvm/x86.c:7501:15: sparse: sparse: incompatible types in comparison expression (different address spaces):
> > >    arch/x86/kvm/x86.c:7501:15: sparse:    struct kvm_apic_map [noderef] <asn:4> *
> > >    arch/x86/kvm/x86.c:7501:15: sparse:    struct kvm_apic_map *
> > > >> arch/x86/kvm/x86.c:9794:31: sparse: sparse: incorrect type in return expression (different address spaces) @@    expected void [noderef] <asn:1> * @@    got n:1> * @@
> > 
> > I'm not sure on how I can reproduce this locally, and also I'm not
> > very sure I understand this warning.  I'd be glad to know if anyone
> > knows...
> > 
> > If without further hints, I'll try to remove the __user for
> > __x86_set_memory_region() and use a cast on the callers next.
> 
> Ah, it's complaining that the ERR_PTR() returns in __x86_set_memory_region()
> aren't explicitly casting to a __user pointer.
> 
> Part of me wonders if something along the lines of your original approach
> of keeping the "int" return and passing a "void __user **p_hva" would be
> cleaner overall, as opposed to having to cast everywhere.  The diff would
> certainly be smaller.  E.g.
> 
> int __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, u32 size,
> 			    void __user **p_hva)
> {
> 	...
> 
> 	if (p_hva)
> 		*p_hva = (void __user *)hva;
> 
>         return 0;
> }

Returning an adress still has some advantage on one less param.  To
avoid going back and forth, I defined ERR_PTR_USR() and used there to
fix the sparse warnings.  Thanks,

-- 
Peter Xu

^ permalink raw reply	[flat|nested] 35+ messages in thread

end of thread, other threads:[~2020-03-18 20:04 UTC | newest]

Thread overview: 35+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-09 21:44 [PATCH v6 00/14] KVM: Dirty ring interface Peter Xu
2020-03-09 21:44 ` [PATCH v6 01/14] KVM: X86: Change parameter for fast_page_fault tracepoint Peter Xu
2020-03-09 21:44 ` [PATCH v6 02/14] KVM: Cache as_id in kvm_memory_slot Peter Xu
2020-03-10 14:48   ` Sean Christopherson
2020-03-11 15:48     ` Peter Xu
2020-03-09 21:44 ` [PATCH v6 03/14] KVM: X86: Don't track dirty for KVM_SET_[TSS_ADDR|IDENTITY_MAP_ADDR] Peter Xu
2020-03-10 15:06   ` Sean Christopherson
2020-03-11 16:01     ` Peter Xu
2020-03-11 16:11       ` Sean Christopherson
2020-03-11  1:10   ` kbuild test robot
2020-03-11  1:10     ` kbuild test robot
2020-03-11 16:39     ` Peter Xu
2020-03-11 16:39       ` Peter Xu
2020-03-11 17:09       ` Sean Christopherson
2020-03-11 17:09         ` Sean Christopherson
2020-03-18 20:04         ` Peter Xu
2020-03-18 20:04           ` Peter Xu
2020-03-09 21:44 ` [PATCH v6 04/14] KVM: Pass in kvm pointer into mark_page_dirty_in_slot() Peter Xu
2020-03-09 21:44 ` [PATCH v6 05/14] KVM: X86: Implement ring-based dirty memory tracking Peter Xu
2020-03-09 22:24 ` [PATCH v6 06/14] KVM: Make dirty ring exclusive to dirty bitmap log Peter Xu
2020-03-09 22:25 ` [PATCH v6 07/14] KVM: Don't allocate dirty bitmap if dirty ring is enabled Peter Xu
2020-03-09 22:25 ` [PATCH v6 08/14] KVM: selftests: Always clear dirty bitmap after iteration Peter Xu
2020-03-09 22:25 ` [PATCH v6 09/14] KVM: selftests: Sync uapi/linux/kvm.h to tools/ Peter Xu
2020-03-09 22:25 ` [PATCH v6 10/14] KVM: selftests: Use a single binary for dirty/clear log test Peter Xu
2020-03-10  8:10   ` Andrew Jones
2020-03-11 17:43     ` Peter Xu
2020-03-11 18:47       ` Andrew Jones
2020-03-09 22:25 ` [PATCH v6 11/14] KVM: selftests: Introduce after_vcpu_run hook for dirty " Peter Xu
2020-03-09 22:25 ` [PATCH v6 12/14] KVM: selftests: Add dirty ring buffer test Peter Xu
2020-03-10  8:18   ` Andrew Jones
2020-03-11 17:44     ` Peter Xu
2020-03-09 22:25 ` [PATCH v6 13/14] KVM: selftests: Let dirty_log_test async for dirty ring test Peter Xu
2020-03-10  8:27   ` Andrew Jones
2020-03-11 17:45     ` Peter Xu
2020-03-09 22:25 ` [PATCH v6 14/14] KVM: selftests: Add "-c" parameter to dirty log test Peter Xu

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.