From: Ben-Ami Yassour Subject: RE: [PATCH] KVM: PCIPT: VT-d: fix context mapping Date: Mon, 30 Jun 2008 18:32:52 +0300 Message-ID: <1214839972.25183.17.camel@cluwyn.haifa.ibm.com> References: <08DF4D958216244799FC84F3514D70F00180ED8B@pdsmsx415.ccr.corp.intel.com> <08DF4D958216244799FC84F3514D70F001850D10@pdsmsx415.ccr.corp.intel.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: QUOTED-PRINTABLE Cc: amit.shah@qumranet.com, kvm@vger.kernel.org, Muli Ben-Yehuda , raharper@us.ibm.com To: "Han, Weidong" Return-path: Received: from mtagate4.de.ibm.com ([195.212.29.153]:33480 "EHLO mtagate4.de.ibm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1763430AbYF3Pc4 (ORCPT ); Mon, 30 Jun 2008 11:32:56 -0400 Received: from d12nrmr1607.megacenter.de.ibm.com (d12nrmr1607.megacenter.de.ibm.com [9.149.167.49]) by mtagate4.de.ibm.com (8.13.8/8.13.8) with ESMTP id m5UFWsss261588 for ; Mon, 30 Jun 2008 15:32:54 GMT Received: from d12av02.megacenter.de.ibm.com (d12av02.megacenter.de.ibm.com [9.149.165.228]) by d12nrmr1607.megacenter.de.ibm.com (8.13.8/8.13.8/NCO v9.0) with ESMTP id m5UFWsip942232 for ; Mon, 30 Jun 2008 17:32:54 +0200 Received: from d12av02.megacenter.de.ibm.com (loopback [127.0.0.1]) by d12av02.megacenter.de.ibm.com (8.12.11.20060308/8.13.3) with ESMTP id m5UFWr3D024954 for ; Mon, 30 Jun 2008 17:32:54 +0200 In-Reply-To: <08DF4D958216244799FC84F3514D70F001850D10@pdsmsx415.ccr.corp.intel.com> Sender: kvm-owner@vger.kernel.org List-ID: On Fri, 2008-06-20 at 14:23 +0800, Han, Weidong wrote: > Ben-Ami Yassour1 wrote: > > "Han, Weidong" wrote on 19/06/2008 17:18:00= : > >=20 > >> Ben-Ami Yassour wrote: > >>> On Thu, 2008-06-19 at 16:59 +0800, Han, Weidong wrote: > >>>> benami@il.ibm.com wrote: > >>>>> From: Ben-Ami Yassour > >>>>>=20 > >>>>> When changing the VT-d context mapping, according to the spec, = it > >>>>> is required to first set the context to not present, flush and > >>>>> only then apply the new context.=20 > >>>>>=20 > >>>>> Signed-off-by: Ben-Ami Yassour > >>>>> --- > >>>>> drivers/pci/intel-iommu.c | 17 +++++++++++++++++ > >>>>> 1 files changed, 17 insertions(+), 0 deletions(-) > >>>>>=20 > >>>>> diff --git a/drivers/pci/intel-iommu.c b/drivers/pci/intel-iomm= u.c > >>>>> index 930874f..dcdfa97 100644 --- a/drivers/pci/intel-iommu.c > >>>>> +++ b/drivers/pci/intel-iommu.c > >>>>> @@ -56,6 +56,7 @@ > >>>>>=20 > >>>>>=20 > >>>>> static void flush_unmaps_timeout(unsigned long data); > >>>>> +static void detach_domain_for_dev(struct dmar_domain *domain, = u8 > >>>>> bus, u8 devfn);=20 > >>>>>=20 > >>>>> DEFINE_TIMER(unmap_timer, flush_unmaps_timeout, 0, 0); > >>>>>=20 > >>>>> @@ -1264,7 +1265,23 @@ static int > >>>>> domain_context_mapping_one(struct dmar_domain *domain, if > >>>>> (!context) return -ENOMEM; > >>>>> spin_lock_irqsave(&iommu->lock, flags); > >>>>> + > >>>>> + if (context_present(*context) && > >>>>> + (context_domain_id(*context) =3D=3D domain->id) && > >>>>> + (context_address_width(*context) =3D=3D domain->agaw) &= & > >>>>> + (context_address_root(*context) =3D=3D > >>>>> virt_to_phys(domain->pgd)) && + =20 > >>>>> (context_translation_type(*context) =3D=3D CONTEXT_TT_MULTI_LEV= EL) && > >>>>> + (!context_fault_disable(*context))) { > >>>>> + spin_unlock_irqrestore(&iommu->lock, flags); + retu= rn > >>>>> 0; + } > >>>>=20 > >>>> Only need to check context_present(*context) according to VT-d > >>>> spec, which says "software must not modify fields other than the > >>>> Present (P) field of currently present root-entries or > >>>> context-entries."=20 > >>>>=20 > >>>> Randy (Weidong) > >>>=20 > >>> The logic here is that, if no change is made to the context then > >>> just return ok (0). Otherwise, according to the spec, we need to > >>> first invalidate the context, flush it, and only then apply the > >>> changes to the context.=20 > >>>=20 > >>> The other option is to make sure that before every call to this > >>> function the context is invalidated, but disabling it inside the > >>> function seems safer. do you agree with that? > >>>=20 > >>=20 > >> After a device can be assigned to guest with VT-d, it needs a > >> context unmap function. When a device is assigned to a guest, map > >> context for it, while when it is detached from a guest, should unm= ap > >> its context. With the context unmap function, I think we needn't t= o > >> implement your logic in domain_context_mapping_one(), instead just > >> check its present. What's your opinion? > >=20 > > Sure, that's fine, this is the other option I mentioned. > > But we need to add the context unmap function. > > Something like: > > diff --git a/arch/x86/kvm/vtd.c b/arch/x86/kvm/vtd.c > > index be775cd..4b96fbb 100644 > > --- a/arch/x86/kvm/vtd.c > > +++ b/arch/x86/kvm/vtd.c > > @@ -109,11 +109,17 @@ found: > > kvm_iommu_unmap_memslots(kvm); > > return -EFAULT; > > } > > + > > + kvm_intel_iommu_detach_dev(kvm->arch.domain, > > + pdev->bus->number, pdev->devfn); > > + > > kvm_intel_iommu_context_mapping(kvm->arch.domain, pdev); > > return 0; > > } > > EXPORT_SYMBOL_GPL(kvm_iommu_map_guest); > >=20 > > Agree? > >=20 >=20 > I don't think it's necessary to add kvm_intel_iommu_detach_dev() here= =2E > If a device can be assigned to a guest, it should not be used by othe= r > guest (assuming no hotplug support). And also > kvm_intel_iommu_detach_dev() is already called in > kvm_iommu_unmap_guest(). Normally context won't be present here. > Otherwise, there should be some wrong. I attach a patch to change it > back to original kernel VT-d code. I think it's correct and clean. The problem is with the following scenario: 1. load the host NIC driver 2. =EF=BB=BFunload the host NIC driver 3. start kvm with passthrough for that NIC In this case the context is not cleaned, and we get VT-d failures. I agree with changing the VT-d driver code back to original. But we do need: diff --git a/arch/x86/kvm/vtd.c b/arch/x86/kvm/vtd.c index be775cd..4b96fbb 100644 --- a/arch/x86/kvm/vtd.c +++ b/arch/x86/kvm/vtd.c @@ -109,11 +109,17 @@ found: kvm_iommu_unmap_memslots(kvm); return -EFAULT; } + + kvm_intel_iommu_detach_dev(kvm->arch.domain, + pdev->bus->number, pdev->devfn); + kvm_intel_iommu_context_mapping(kvm->arch.domain, pdev); return 0; } EXPORT_SYMBOL_GPL(kvm_iommu_map_guest); Otherwise =EF=BB=BFkvm_intel_iommu_context_mapping will exit on the =EF= =BB=BFcontext_present(*context) check. This means that the context is still going to point to the iova of the = host NIC. If you do not agree, please explain what code path clears the context w= ritten for the host NIC driver in this scenario (note that empirically I see that my assumption is correct). Thanks, Ben >=20 > diff --git a/drivers/pci/intel-iommu.c b/drivers/pci/intel-iommu.c > index 930874f..04517a2 100644 > --- a/drivers/pci/intel-iommu.c > +++ b/drivers/pci/intel-iommu.c > @@ -1264,8 +1264,11 @@ static int domain_context_mapping_one(struct > dmar_domain *domain, > if (!context) > return -ENOMEM; > spin_lock_irqsave(&iommu->lock, flags); > + if (context_present(*context)) { > + spin_unlock_irqrestore(&iommu->lock, flags); > + return 0; > + } > =20 > - context_clear_entry(*context); > context_set_domain_id(*context, domain->id); > context_set_address_width(*context, domain->agaw); > context_set_address_root(*context, virt_to_phys(domain->pgd)); >=20 >=20 > Randy (Weidong) >=20 >=20 > -- > 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