public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
From: Avi Kivity <avi@redhat.com>
To: Takuya Yoshikawa <yoshikawa.takuya@oss.ntt.co.jp>
Cc: mtosatti@redhat.com, kvm@vger.kernel.org
Subject: Re: [PATCH 1/1] KVM: x86: avoid unnecessary bitmap allocation when memslot is clean
Date: Tue, 27 Apr 2010 16:18:10 +0300	[thread overview]
Message-ID: <4BD6E412.7090202@redhat.com> (raw)
In-Reply-To: <20100426185854.5d606a04.yoshikawa.takuya@oss.ntt.co.jp>

On 04/26/2010 12:58 PM, Takuya Yoshikawa wrote:
> Although we always allocate a new dirty bitmap in x86's get_dirty_log(),
> it is only used as a zero-source of copy_to_user() and freed right after
> that when memslot is clean. This patch uses clear_user() instead of doing
> this unnecessary zero-source allocation.
>
> Performance improvement: as we can expect easily, the time needed to
> allocate a bitmap is completely reduced. In my test, the improved ioctl
> was about 4 to 10 times faster than the original one for clean slots.
> Furthermore, the reduced allocations seem to produce good effects for
> other cases too. Actually, I observed that the time for the ioctl was
> more stable than the original one and the average time for dirty slots
> was also reduced by some extent.
>    

Can you explain why the dirty slots were improved?

> Signed-off-by: Takuya Yoshikawa<yoshikawa.takuya@oss.ntt.co.jp>
> ---
>   arch/x86/kvm/x86.c |   36 ++++++++++++++++++++++--------------
>   1 files changed, 22 insertions(+), 14 deletions(-)
>
> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> index 6b2ce1d..0086d64 100644
> --- a/arch/x86/kvm/x86.c
> +++ b/arch/x86/kvm/x86.c
> @@ -2744,7 +2744,6 @@ int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
>   	struct kvm_memory_slot *memslot;
>   	unsigned long n;
>   	unsigned long is_dirty = 0;
> -	unsigned long *dirty_bitmap = NULL;
>
>   	mutex_lock(&kvm->slots_lock);
>
> @@ -2759,27 +2758,29 @@ int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
>
>   	n = kvm_dirty_bitmap_bytes(memslot);
>
> -	r = -ENOMEM;
> -	dirty_bitmap = vmalloc(n);
> -	if (!dirty_bitmap)
> -		goto out;
> -	memset(dirty_bitmap, 0, n);
> -
>   	for (i = 0; !is_dirty&&  i<  n/sizeof(long); i++)
>   		is_dirty = memslot->dirty_bitmap[i];
>
>   	/* If nothing is dirty, don't bother messing with page tables. */
>   	if (is_dirty) {
>   		struct kvm_memslots *slots, *old_slots;
> +		unsigned long *dirty_bitmap;
>
>   		spin_lock(&kvm->mmu_lock);
>   		kvm_mmu_slot_remove_write_access(kvm, log->slot);
>   		spin_unlock(&kvm->mmu_lock);
>
> -		slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
> -		if (!slots)
> -			goto out_free;
> +		r = -ENOMEM;
> +		dirty_bitmap = vmalloc(n);
> +		if (!dirty_bitmap)
> +			goto out;
> +		memset(dirty_bitmap, 0, n);
>
>    

Better to keep r = -ENOMEM here, so if something else is inserted, we 
don't lose the error.  It logically belongs with the allocation, not 
inherited.

> +		slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
> +		if (!slots) {
> +			vfree(dirty_bitmap);
> +			goto out;
> +		}
>   		memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots));
>   		slots->memslots[log->slot].dirty_bitmap = dirty_bitmap;
>
> @@ -2788,13 +2789,20 @@ int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
>   		synchronize_srcu_expedited(&kvm->srcu);
>   		dirty_bitmap = old_slots->memslots[log->slot].dirty_bitmap;
>   		kfree(old_slots);
> +
> +		r = -EFAULT;
> +		if (copy_to_user(log->dirty_bitmap, dirty_bitmap, n)) {
> +			vfree(dirty_bitmap);
>    

What about slots?  ah, I see they were already freed.

> +			goto out;
> +		}
> +		vfree(dirty_bitmap);
> +	} else {
> +		r = -EFAULT;
> +		if (clear_user(log->dirty_bitmap, n))
> +			goto out;
>   	}
>
>   	r = 0;
> -	if (copy_to_user(log->dirty_bitmap, dirty_bitmap, n))
> -		r = -EFAULT;
> -out_free:
> -	vfree(dirty_bitmap);
>   out:
>   	mutex_unlock(&kvm->slots_lock);
>   	return r;
>    


-- 
error compiling committee.c: too many arguments to function


  parent reply	other threads:[~2010-04-27 13:18 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-04-26  9:56 [PATCH 0/1] KVM: x86: avoid unnecessary bitmap allocation when memslot is clean Takuya Yoshikawa
2010-04-26  9:58 ` [PATCH 1/1] " Takuya Yoshikawa
2010-04-27 12:36   ` Marcelo Tosatti
2010-04-27 13:18   ` Avi Kivity [this message]
2010-04-27 13:46     ` Takuya Yoshikawa
2010-04-28  4:17       ` Takuya Yoshikawa
2010-04-28 10:27         ` Avi Kivity
2010-04-28 10:26       ` Avi Kivity
2010-04-28  9:50 ` [PATCH v2] " Takuya Yoshikawa
2010-05-04  9:36   ` Avi Kivity

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4BD6E412.7090202@redhat.com \
    --to=avi@redhat.com \
    --cc=kvm@vger.kernel.org \
    --cc=mtosatti@redhat.com \
    --cc=yoshikawa.takuya@oss.ntt.co.jp \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox