diff mbox

[02/13] KVM: reuse memslot in kvm_write_guest_page

Message ID 1430393772-27208-3-git-send-email-pbonzini@redhat.com (mailing list archive)
State New, archived
Headers show

Commit Message

Paolo Bonzini April 30, 2015, 11:36 a.m. UTC
From: Radim Kr?má? <rkrcmar@redhat.com>

Saves one O(log N) search.

Signed-off-by: Radim Kr?má? <rkrcmar@redhat.com>
Message-Id: <1428695247-27603-1-git-send-email-rkrcmar@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 virt/kvm/kvm_main.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

Comments

Bandan Das May 5, 2015, 3:03 p.m. UTC | #1
Paolo Bonzini <pbonzini@redhat.com> writes:

> From: Radim Kr?má? <rkrcmar@redhat.com>
>
> Saves one O(log N) search.

It really doesn't hurt to change this to:
"Caching memslot value helps us avoid another O(log N) search
later when calling mark_page_dirty_in_slot()."

Sorry I am not a fan of commit messages less than 75 characters :)
Or you can also call me dumb :)

> Signed-off-by: Radim Kr?má? <rkrcmar@redhat.com>
> Message-Id: <1428695247-27603-1-git-send-email-rkrcmar@redhat.com>
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> ---
>  virt/kvm/kvm_main.c | 6 ++++--
>  1 file changed, 4 insertions(+), 2 deletions(-)
>
> diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
> index 90977418aeb6..b6d415156283 100644
> --- a/virt/kvm/kvm_main.c
> +++ b/virt/kvm/kvm_main.c
> @@ -1590,15 +1590,17 @@ int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
>  			 int offset, int len)
>  {
>  	int r;
> +	struct kvm_memory_slot *memslot;
>  	unsigned long addr;
>  
> -	addr = gfn_to_hva(kvm, gfn);
> +	memslot = gfn_to_memslot(kvm, gfn);
> +	addr = gfn_to_hva_memslot(memslot, gfn);
>  	if (kvm_is_error_hva(addr))
>  		return -EFAULT;
>  	r = __copy_to_user((void __user *)addr + offset, data, len);
>  	if (r)
>  		return -EFAULT;
> -	mark_page_dirty(kvm, gfn);
> +	mark_page_dirty_in_slot(kvm, memslot, gfn);
>  	return 0;
>  }
>  EXPORT_SYMBOL_GPL(kvm_write_guest_page);
--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Radim Krčmář May 5, 2015, 4:29 p.m. UTC | #2
2015-05-05 11:03-0400, Bandan Das:
> Paolo Bonzini <pbonzini@redhat.com> writes:
> 
>> From: Radim Kr?má? <rkrcmar@redhat.com>
>>
>> Saves one O(log N) search.
> 
> It really doesn't hurt to change this to:
> "Caching memslot value helps us avoid another O(log N) search
> later when calling mark_page_dirty_in_slot()."
> 
> Sorry I am not a fan of commit messages less than 75 characters :)

Sure, I just couldn't find a message that would be easier to understand
than the code and not misleading at the same time ... what came out was
a teaser for people who care about performance :)

In verbose mode, I'd write it like this:
  gfn_to_hva() and mark_page_dirty() both call gfn_to_memslot().
  We can store a result of gfn_to_memslot() (memslot) and unwrap
  gfn_to_hva() and mark_page_dirty() to directly call functions that
  accept a memslot.
  gfn_to_memslot() does a binary search, so it also saves some cycles.
--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 90977418aeb6..b6d415156283 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -1590,15 +1590,17 @@  int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
 			 int offset, int len)
 {
 	int r;
+	struct kvm_memory_slot *memslot;
 	unsigned long addr;
 
-	addr = gfn_to_hva(kvm, gfn);
+	memslot = gfn_to_memslot(kvm, gfn);
+	addr = gfn_to_hva_memslot(memslot, gfn);
 	if (kvm_is_error_hva(addr))
 		return -EFAULT;
 	r = __copy_to_user((void __user *)addr + offset, data, len);
 	if (r)
 		return -EFAULT;
-	mark_page_dirty(kvm, gfn);
+	mark_page_dirty_in_slot(kvm, memslot, gfn);
 	return 0;
 }
 EXPORT_SYMBOL_GPL(kvm_write_guest_page);