From mboxrd@z Thu Jan 1 00:00:00 1970 From: Marcelo Tosatti Subject: Re: KVM: MMU: optimize set_spte for page sync Date: Tue, 25 Nov 2008 15:58:07 +0100 Message-ID: <20081125145807.GA14994@dmt.cnet> References: <20081121184927.GA20607@dmt.cnet> <4929322D.7050503@redhat.com> <20081124120423.GB4379@dmt.cnet> <20081124132306.GA13532@dmt.cnet> <492C0DD5.7070206@redhat.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Cc: kvm-devel To: Avi Kivity Return-path: Received: from mx2.redhat.com ([66.187.237.31]:45810 "EHLO mx2.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753553AbYKYSAQ (ORCPT ); Tue, 25 Nov 2008 13:00:16 -0500 Received: from int-mx2.corp.redhat.com (int-mx2.corp.redhat.com [172.16.27.26]) by mx2.redhat.com (8.13.8/8.13.8) with ESMTP id mAPI0Cdd020506 for ; Tue, 25 Nov 2008 13:00:12 -0500 Content-Disposition: inline In-Reply-To: <492C0DD5.7070206@redhat.com> Sender: kvm-owner@vger.kernel.org List-ID: On Tue, Nov 25, 2008 at 04:38:13PM +0200, Avi Kivity wrote: > Marcelo Tosatti wrote: >> *shadow_pte can point to a different page if the guest updates >> pagetable, there is a fault before resync, the fault updates the >> spte with new gfn (and pfn) via mmu_set_spte. In which case the gfn >> cache is updated since: >> >> } else if (pfn != spte_to_pfn(*shadow_pte)) { >> printk("hfn old %lx new %lx\n", >> spte_to_pfn(*shadow_pte), pfn); >> rmap_remove(vcpu->kvm, shadow_pte); >> > > Okay. Please resend but without the reversal of can_unsync, it will > make a more readable patch. If you like, send a follow on that only > does the reversal. Here it goes. KVM: MMU: optimize set_spte for page sync The write protect verification in set_spte is unnecessary for page sync. Its guaranteed that, if the unsync spte was writable, the target page does not have a write protected shadow (if it had, the spte would have been write protected under mmu_lock by rmap_write_protect before). Same reasoning applies to mark_page_dirty: the gfn has been marked as dirty via the pagefault path. The cost of hash table and memslot lookups are quite significant if the workload is pagetable write intensive resulting in increased mmu_lock contention. Signed-off-by: Marcelo Tosatti Index: kvm/arch/x86/kvm/mmu.c =================================================================== --- kvm.orig/arch/x86/kvm/mmu.c +++ kvm/arch/x86/kvm/mmu.c @@ -1593,6 +1593,15 @@ static int set_spte(struct kvm_vcpu *vcp spte |= PT_WRITABLE_MASK; + /* + * Optimization: for pte sync, if spte was writable the hash + * lookup is unnecessary (and expensive). Write protection + * is responsibility of mmu_get_page / kvm_sync_page. + * Same reasoning can be applied to dirty page accounting. + */ + if (!can_unsync && is_writeble_pte(*shadow_pte)) + goto set_pte; + if (mmu_need_write_protect(vcpu, gfn, can_unsync)) { pgprintk("%s: found shadow page for %lx, marking ro\n", __func__, gfn);