From mboxrd@z Thu Jan 1 00:00:00 1970 From: Xiao Guangrong Subject: Re: [PATCH 1/2] KVM: fix cache stale memslot info with correct mmio generation number Date: Thu, 14 Aug 2014 15:06:15 +0800 Message-ID: <53EC5FE7.6040904@linux.vnet.ibm.com> References: <1407999713-3726-1-git-send-email-xiaoguangrong@linux.vnet.ibm.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: QUOTED-PRINTABLE Cc: avi.kivity@gmail.com, mtosatti@redhat.com, pbonzini@redhat.com, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, stable@vger.kernel.org, David Matlack To: gleb@kernel.org Return-path: Received: from e28smtp08.in.ibm.com ([122.248.162.8]:46717 "EHLO e28smtp08.in.ibm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751836AbaHNHG1 (ORCPT ); Thu, 14 Aug 2014 03:06:27 -0400 Received: from /spool/local by e28smtp08.in.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Thu, 14 Aug 2014 12:36:25 +0530 In-Reply-To: <1407999713-3726-1-git-send-email-xiaoguangrong@linux.vnet.ibm.com> Sender: kvm-owner@vger.kernel.org List-ID: Sorry, the title is not clear enough. This is the v2 which fixes the issue pointed out by David: " the generation number actually decreases." Please review. On 08/14/2014 03:01 PM, Xiao Guangrong wrote: > We may cache the current mmio generation number and stale memslot inf= o > into spte, like this scenario=EF=BC=9A >=20 > CPU 0 CPU 1 > page fault: add a new memslot > read memslot and detecting its a mmio access > update memslots > update generation number > read generation number > cache the gpa and current gen number into spte >=20 > So, if guest accesses the gpa later, it will generate a incorrect > mmio exit >=20 > This patch fixes it by updating the generation number after > synchronize_srcu_expedited() that makes sure the generation > number updated only if memslots update is finished >=20 > Cc: stable@vger.kernel.org > Cc: David Matlack > Signed-off-by: Xiao Guangrong > --- > virt/kvm/kvm_main.c | 12 ++++++------ > 1 file changed, 6 insertions(+), 6 deletions(-) >=20 > diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c > index 33712fb..bb40df3 100644 > --- a/virt/kvm/kvm_main.c > +++ b/virt/kvm/kvm_main.c > @@ -96,7 +96,7 @@ static void hardware_disable_all(void); >=20 > static void kvm_io_bus_destroy(struct kvm_io_bus *bus); > static void update_memslots(struct kvm_memslots *slots, > - struct kvm_memory_slot *new, u64 last_generation); > + struct kvm_memory_slot *new); >=20 > static void kvm_release_pfn_dirty(pfn_t pfn); > static void mark_page_dirty_in_slot(struct kvm *kvm, > @@ -687,8 +687,7 @@ static void sort_memslots(struct kvm_memslots *sl= ots) > } >=20 > static void update_memslots(struct kvm_memslots *slots, > - struct kvm_memory_slot *new, > - u64 last_generation) > + struct kvm_memory_slot *new) > { > if (new) { > int id =3D new->id; > @@ -699,8 +698,6 @@ static void update_memslots(struct kvm_memslots *= slots, > if (new->npages !=3D npages) > sort_memslots(slots); > } > - > - slots->generation =3D last_generation + 1; > } >=20 > static int check_memory_region_flags(struct kvm_userspace_memory_reg= ion *mem) > @@ -722,9 +719,12 @@ static struct kvm_memslots *install_new_memslots= (struct kvm *kvm, > { > struct kvm_memslots *old_memslots =3D kvm->memslots; >=20 > - update_memslots(slots, new, kvm->memslots->generation); > + /* ensure generation number is always increased. */ > + slots->generation =3D old_memslots->generation; > + update_memslots(slots, new); > rcu_assign_pointer(kvm->memslots, slots); > synchronize_srcu_expedited(&kvm->srcu); > + slots->generation++; >=20 > kvm_arch_memslots_updated(kvm); >=20