[v7,11/14] KVM: selftests: Introduce after_vcpu_run hook for dirty log test
diff mbox series

Message ID 20200318163720.93929-12-peterx@redhat.com
State New
Headers show
Series
  • KVM: Dirty ring interface
Related show

Commit Message

Peter Xu March 18, 2020, 4:37 p.m. UTC
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 | 41 ++++++++++++++------
 1 file changed, 30 insertions(+), 11 deletions(-)

Comments

Andrew Jones March 19, 2020, 7:50 a.m. UTC | #1
On Wed, Mar 18, 2020 at 12:37:17PM -0400, Peter Xu wrote:
> 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 | 41 ++++++++++++++------
>  1 file changed, 30 insertions(+), 11 deletions(-)
> 
> diff --git a/tools/testing/selftests/kvm/dirty_log_test.c b/tools/testing/selftests/kvm/dirty_log_test.c
> index 139ccb550618..94122c2e0185 100644
> --- a/tools/testing/selftests/kvm/dirty_log_test.c
> +++ b/tools/testing/selftests/kvm/dirty_log_test.c
> @@ -178,6 +178,15 @@ static void clear_log_collect_dirty_pages(struct kvm_vm *vm, int slot,
>  	kvm_vm_clear_dirty_log(vm, slot, bitmap, 0, num_pages);
>  }
>  
> +static void default_after_vcpu_run(struct kvm_vm *vm)
> +{
> +	struct kvm_run *run = vcpu_state(vm, VCPU_ID);
> +
> +	TEST_ASSERT(get_ucall(vm, VCPU_ID, NULL) == UCALL_SYNC,
> +		    "Invalid guest sync status: exit_reason=%s\n",
> +		    exit_reason_str(run->exit_reason));
> +}
> +
>  struct log_mode {
>  	const char *name;
>  	/* Return true if this mode is supported, otherwise false */
> @@ -187,16 +196,20 @@ struct log_mode {
>  	/* Hook to collect the dirty pages into the bitmap provided */
>  	void (*collect_dirty_pages) (struct kvm_vm *vm, int slot,
>  				     void *bitmap, uint32_t num_pages);
> +	/* Hook to call when after each vcpu run */
> +	void (*after_vcpu_run)(struct kvm_vm *vm);
>  } log_modes[LOG_MODE_NUM] = {
>  	{
>  		.name = "dirty-log",
>  		.collect_dirty_pages = dirty_log_collect_dirty_pages,
> +		.after_vcpu_run = default_after_vcpu_run,
>  	},
>  	{
>  		.name = "clear-log",
>  		.supported = clear_log_supported,
>  		.create_vm_done = clear_log_create_vm_done,
>  		.collect_dirty_pages = clear_log_collect_dirty_pages,
> +		.after_vcpu_run = default_after_vcpu_run,
>  	},
>  };
>  
> @@ -247,6 +260,14 @@ static void log_mode_collect_dirty_pages(struct kvm_vm *vm, int slot,
>  	mode->collect_dirty_pages(vm, slot, bitmap, num_pages);
>  }
>  
> +static void log_mode_after_vcpu_run(struct kvm_vm *vm)
> +{
> +	struct log_mode *mode = &log_modes[host_log_mode];
> +
> +	if (mode->after_vcpu_run)
> +		mode->after_vcpu_run(vm);
> +}
> +
>  static void generate_random_array(uint64_t *guest_array, uint64_t size)
>  {
>  	uint64_t i;
> @@ -261,25 +282,23 @@ static void *vcpu_worker(void *data)
>  	struct kvm_vm *vm = data;
>  	uint64_t *guest_array;
>  	uint64_t pages_count = 0;
> -	struct kvm_run *run;
> +	struct sigaction sigact;
>  
> -	run = vcpu_state(vm, VCPU_ID);
> +	current_vm = vm;
> +	vcpu_fd = vcpu_get_fd(vm, VCPU_ID);

You don't add this call until 13/14, which means bisection is broken.
Please test the series with 'git rebase -i -x make'.


> +	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);
> -	generate_random_array(guest_array, TEST_PAGES_PER_LOOP);
>  
>  	while (!READ_ONCE(host_quit)) {
> +		generate_random_array(guest_array, TEST_PAGES_PER_LOOP);
> +		pages_count += TEST_PAGES_PER_LOOP;
>  		/* Let the guest dirty the random pages */
>  		ret = _vcpu_run(vm, VCPU_ID);
>  		TEST_ASSERT(ret == 0, "vcpu_run failed: %d\n", ret);
> -		if (get_ucall(vm, VCPU_ID, NULL) == UCALL_SYNC) {
> -			pages_count += TEST_PAGES_PER_LOOP;
> -			generate_random_array(guest_array, TEST_PAGES_PER_LOOP);
> -		} else {
> -			TEST_FAIL("Invalid guest sync status: "
> -				  "exit_reason=%s\n",
> -				  exit_reason_str(run->exit_reason));
> -		}
> +		log_mode_after_vcpu_run(vm);
>  	}
>  
>  	pr_info("Dirtied %"PRIu64" pages\n", pages_count);
> -- 
> 2.24.1
>
Peter Xu March 19, 2020, 4:37 p.m. UTC | #2
On Thu, Mar 19, 2020 at 08:50:05AM +0100, Andrew Jones wrote:
> >  static void generate_random_array(uint64_t *guest_array, uint64_t size)
> >  {
> >  	uint64_t i;
> > @@ -261,25 +282,23 @@ static void *vcpu_worker(void *data)
> >  	struct kvm_vm *vm = data;
> >  	uint64_t *guest_array;
> >  	uint64_t pages_count = 0;
> > -	struct kvm_run *run;
> > +	struct sigaction sigact;
> >  
> > -	run = vcpu_state(vm, VCPU_ID);
> > +	current_vm = vm;
> > +	vcpu_fd = vcpu_get_fd(vm, VCPU_ID);
> 
> You don't add this call until 13/14, which means bisection is broken.
> Please test the series with 'git rebase -i -x make'.

You're right... I do per-patch test but only for the initial version,
then I test the fixups as standalone changes for latter versions.
This must be a wrongly squashed fixup into the wrong patch.  Sorry.

I'll wait for some more time for another repost.  Thanks,

Patch
diff mbox series

diff --git a/tools/testing/selftests/kvm/dirty_log_test.c b/tools/testing/selftests/kvm/dirty_log_test.c
index 139ccb550618..94122c2e0185 100644
--- a/tools/testing/selftests/kvm/dirty_log_test.c
+++ b/tools/testing/selftests/kvm/dirty_log_test.c
@@ -178,6 +178,15 @@  static void clear_log_collect_dirty_pages(struct kvm_vm *vm, int slot,
 	kvm_vm_clear_dirty_log(vm, slot, bitmap, 0, num_pages);
 }
 
+static void default_after_vcpu_run(struct kvm_vm *vm)
+{
+	struct kvm_run *run = vcpu_state(vm, VCPU_ID);
+
+	TEST_ASSERT(get_ucall(vm, VCPU_ID, NULL) == UCALL_SYNC,
+		    "Invalid guest sync status: exit_reason=%s\n",
+		    exit_reason_str(run->exit_reason));
+}
+
 struct log_mode {
 	const char *name;
 	/* Return true if this mode is supported, otherwise false */
@@ -187,16 +196,20 @@  struct log_mode {
 	/* Hook to collect the dirty pages into the bitmap provided */
 	void (*collect_dirty_pages) (struct kvm_vm *vm, int slot,
 				     void *bitmap, uint32_t num_pages);
+	/* Hook to call when after each vcpu run */
+	void (*after_vcpu_run)(struct kvm_vm *vm);
 } log_modes[LOG_MODE_NUM] = {
 	{
 		.name = "dirty-log",
 		.collect_dirty_pages = dirty_log_collect_dirty_pages,
+		.after_vcpu_run = default_after_vcpu_run,
 	},
 	{
 		.name = "clear-log",
 		.supported = clear_log_supported,
 		.create_vm_done = clear_log_create_vm_done,
 		.collect_dirty_pages = clear_log_collect_dirty_pages,
+		.after_vcpu_run = default_after_vcpu_run,
 	},
 };
 
@@ -247,6 +260,14 @@  static void log_mode_collect_dirty_pages(struct kvm_vm *vm, int slot,
 	mode->collect_dirty_pages(vm, slot, bitmap, num_pages);
 }
 
+static void log_mode_after_vcpu_run(struct kvm_vm *vm)
+{
+	struct log_mode *mode = &log_modes[host_log_mode];
+
+	if (mode->after_vcpu_run)
+		mode->after_vcpu_run(vm);
+}
+
 static void generate_random_array(uint64_t *guest_array, uint64_t size)
 {
 	uint64_t i;
@@ -261,25 +282,23 @@  static void *vcpu_worker(void *data)
 	struct kvm_vm *vm = data;
 	uint64_t *guest_array;
 	uint64_t pages_count = 0;
-	struct kvm_run *run;
+	struct sigaction sigact;
 
-	run = vcpu_state(vm, VCPU_ID);
+	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);
-	generate_random_array(guest_array, TEST_PAGES_PER_LOOP);
 
 	while (!READ_ONCE(host_quit)) {
+		generate_random_array(guest_array, TEST_PAGES_PER_LOOP);
+		pages_count += TEST_PAGES_PER_LOOP;
 		/* Let the guest dirty the random pages */
 		ret = _vcpu_run(vm, VCPU_ID);
 		TEST_ASSERT(ret == 0, "vcpu_run failed: %d\n", ret);
-		if (get_ucall(vm, VCPU_ID, NULL) == UCALL_SYNC) {
-			pages_count += TEST_PAGES_PER_LOOP;
-			generate_random_array(guest_array, TEST_PAGES_PER_LOOP);
-		} else {
-			TEST_FAIL("Invalid guest sync status: "
-				  "exit_reason=%s\n",
-				  exit_reason_str(run->exit_reason));
-		}
+		log_mode_after_vcpu_run(vm);
 	}
 
 	pr_info("Dirtied %"PRIu64" pages\n", pages_count);