All of lore.kernel.org
 help / color / mirror / Atom feed
From: Marcelo Tosatti <mtosatti@redhat.com>
To: Avi Kivity <avi@redhat.com>
Cc: Xiao Guangrong <xiaoguangrong@linux.vnet.ibm.com>,
	Takuya Yoshikawa <yoshikawa.takuya@oss.ntt.co.jp>,
	kvm@vger.kernel.org
Subject: Re: [PATCH v2 3/5] KVM: Flush TLB in mmu notifier without holding mmu_lock
Date: Mon, 21 May 2012 17:58:50 -0300	[thread overview]
Message-ID: <20120521205850.GA27076@amt.cnet> (raw)
In-Reply-To: <1337250284-18607-3-git-send-email-avi@redhat.com>

On Thu, May 17, 2012 at 01:24:42PM +0300, Avi Kivity wrote:
> Signed-off-by: Avi Kivity <avi@redhat.com>
> ---
>  virt/kvm/kvm_main.c |   16 ++++++++--------
>  1 file changed, 8 insertions(+), 8 deletions(-)
> 
> diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
> index 585ab45..9f6d15d 100644
> --- a/virt/kvm/kvm_main.c
> +++ b/virt/kvm/kvm_main.c
> @@ -302,11 +302,11 @@ static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
>  	kvm->mmu_notifier_seq++;
>  	if (kvm_unmap_hva(kvm, address))
>  		kvm_mark_tlb_dirty(kvm);
> -	/* we've to flush the tlb before the pages can be freed */
> -	kvm_cond_flush_remote_tlbs(kvm);
> -
>  	spin_unlock(&kvm->mmu_lock);
>  	srcu_read_unlock(&kvm->srcu, idx);
> +
> +	/* we've to flush the tlb before the pages can be freed */
> +	kvm_cond_flush_remote_tlbs(kvm);
>  }

There are still sites that assumed implicitly that acquiring mmu_lock
guarantees that sptes and remote TLBs are in sync. Example:

void kvm_mmu_zap_all(struct kvm *kvm)
{
        struct kvm_mmu_page *sp, *node;
        LIST_HEAD(invalid_list);

        spin_lock(&kvm->mmu_lock);
restart:
        list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages,
link)
                if (kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list))
                        goto restart;

        kvm_mmu_commit_zap_page(kvm, &invalid_list);
        spin_unlock(&kvm->mmu_lock);
}

kvm_mmu_commit_zap_page only flushes if the TLB was dirtied by this
context, not before. kvm_mmu_unprotect_page is similar.

In general:

context 1                       context 2

lock(mmu_lock)
modify spte
mark_tlb_dirty
unlock(mmu_lock)
                                lock(mmu_lock)
                                read spte
                                make a decision based on spte value
                                unlock(mmu_lock)
flush remote TLBs


Is scary.

Perhaps have a rule that says:

1) Conditionally flush remote TLB after acquiring mmu_lock, 
before anything (even perhaps inside the lock macro).
2) Except special cases where it is clear that this is not 
necessary.


  reply	other threads:[~2012-05-21 21:03 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-05-17 10:24 [PATCH v2 1/5] KVM: Add APIs for unlocked TLB flush Avi Kivity
2012-05-17 10:24 ` [PATCH v2 2/5] KVM: MMU: Convert remote flushes to kvm_mark_tlb_dirty() and a conditional flush Avi Kivity
2012-05-21 20:13   ` Marcelo Tosatti
2012-05-22 14:46   ` Takuya Yoshikawa
2012-05-17 10:24 ` [PATCH v2 3/5] KVM: Flush TLB in mmu notifier without holding mmu_lock Avi Kivity
2012-05-21 20:58   ` Marcelo Tosatti [this message]
2012-05-21 21:07     ` Marcelo Tosatti
2012-07-02 12:05     ` Avi Kivity
2012-07-02 12:41       ` Avi Kivity
2012-07-02 14:09         ` Takuya Yoshikawa
2012-07-02 14:18           ` Avi Kivity
2012-05-17 10:24 ` [PATCH v2 4/5] KVM: Flush TLB in FNAME(invlpg) " Avi Kivity
2012-05-17 10:24 ` [PATCH v2 5/5] KVM: Flush TLB in change_pte mmu notifier " Avi Kivity
2012-05-17 11:51 ` [PATCH v2 1/5] KVM: Add APIs for unlocked TLB flush 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=20120521205850.GA27076@amt.cnet \
    --to=mtosatti@redhat.com \
    --cc=avi@redhat.com \
    --cc=kvm@vger.kernel.org \
    --cc=xiaoguangrong@linux.vnet.ibm.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 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.