From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:43085) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zeto9-0007nK-3e for qemu-devel@nongnu.org; Wed, 23 Sep 2015 19:50:42 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Zeto5-0000PT-PQ for qemu-devel@nongnu.org; Wed, 23 Sep 2015 19:50:41 -0400 Date: Thu, 24 Sep 2015 09:50:14 +1000 From: David Gibson Message-ID: <20150923235014.GE15944@voom.fritz.box> References: <1442495357-26547-1-git-send-email-david@gibson.dropbear.id.au> <1442495357-26547-6-git-send-email-david@gibson.dropbear.id.au> <5602DBB7.5050606@redhat.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="fWddYNRDgTk9wQGZ" Content-Disposition: inline In-Reply-To: <5602DBB7.5050606@redhat.com> Subject: Re: [Qemu-devel] [RFC PATCH 05/10] memory: Allow replay of IOMMU mapping notifications List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Laurent Vivier Cc: thuth@redhat.com, aik@ozlabs.ru, gwshan@linux.vnet.ibm.com, qemu-devel@nongnu.org, alex.williamson@redhat.com, qemu-ppc@nongnu.org, pbonzini@redhat.com --fWddYNRDgTk9wQGZ Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Wed, Sep 23, 2015 at 07:04:55PM +0200, Laurent Vivier wrote: >=20 >=20 > On 17/09/2015 15:09, David Gibson wrote: > > When we have guest visible IOMMUs, we allow notifiers to be registered > > which will be informed of all changes to IOMMU mappings. This is used = by > > vfio to keep the host IOMMU mappings in sync with guest IOMMU mappings. > >=20 > > However, unlike with a memory region listener, an iommu notifier won't = be > > told about any mappings which already exist in the (guest) IOMMU at the > > time it is registered. This can cause problems if hotplugging a VFIO > > device onto a guest bus which had existing guest IOMMU mappings, but di= dn't > > previously have an VFIO devices (and hence no host IOMMU mappings). > >=20 > > This adds a memory_region_register_iommu_notifier_replay() function to > > handle this case. As well as registering the new notifier it replays > > existing mappings. Because the IOMMU memory region doesn't internally > > remember the granularity of the guest IOMMU it has a small hack where t= he > > caller must specify a granularity at which to replay mappings. > >=20 > > If there are finer mappings in the guest IOMMU these will be reported in > > the iotlb structures passed to the notifier which it must handle (proba= bly > > causing it to flag an error). This isn't new - the VFIO iommu notifier > > must already handle notifications about guest IOMMU mappings too short > > for it to represent in the host IOMMU. > >=20 > > Signed-off-by: David Gibson > > --- > > include/exec/memory.h | 16 ++++++++++++++++ > > memory.c | 18 ++++++++++++++++++ > > 2 files changed, 34 insertions(+) > >=20 > > diff --git a/include/exec/memory.h b/include/exec/memory.h > > index 5baaf48..3cf145b 100644 > > --- a/include/exec/memory.h > > +++ b/include/exec/memory.h > > @@ -583,6 +583,22 @@ void memory_region_notify_iommu(MemoryRegion *mr, > > void memory_region_register_iommu_notifier(MemoryRegion *mr, Notifier = *n); > > =20 > > /** > > + * memory_region_register_iommu_notifier_replay: register a notifier > > + * for changes to IOMMU translation entries, and replay existing IOMMU > > + * translations to the new notifier. > > + * > > + * @mr: the memory region to observe > > + * @n: the notifier to be added; the notifier receives a pointer to an > > + * #IOMMUTLBEntry as the opaque value; the pointer ceases to be > > + * valid on exit from the notifier. > > + * @granularity: Minimum page granularity to replay notifications for > > + * @is_write: Whether to treat the replay as a translate "write" > > + * through the iommu > > + */ > > +void memory_region_register_iommu_notifier_replay(MemoryRegion *mr, No= tifier *n, > > + hwaddr granularity, = bool is_write); > > + > > +/** > > * memory_region_unregister_iommu_notifier: unregister a notifier for > > * changes to IOMMU translation entries. > > * > > diff --git a/memory.c b/memory.c > > index 0d8b2d9..6b5a2f1 100644 > > --- a/memory.c > > +++ b/memory.c > > @@ -1403,6 +1403,24 @@ void memory_region_register_iommu_notifier(Memor= yRegion *mr, Notifier *n) > > notifier_list_add(&mr->iommu_notify, n); > > } > > =20 > > +void memory_region_register_iommu_notifier_replay(MemoryRegion *mr, No= tifier *n, > > + hwaddr granularity, = bool is_write) > > +{ > > + hwaddr addr; > > + IOMMUTLBEntry iotlb; > > + > > + memory_region_register_iommu_notifier(mr, n); > > + > > + for (addr =3D 0; > > + int128_lt(int128_make64(addr), mr->size); >=20 > "addr < memory_region_size(mr)" should be enough. Ah, yes, much neater, thanks. > > + addr +=3D granularity) { > > + > > + iotlb =3D mr->iommu_ops->translate(mr, addr, is_write); > > + if (iotlb.perm !=3D IOMMU_NONE) > > + n->notify(n, &iotlb); > > + } > > +} > > + > > void memory_region_unregister_iommu_notifier(Notifier *n) > > { > > notifier_remove(n); > >=20 >=20 --=20 David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson --fWddYNRDgTk9wQGZ Content-Type: application/pgp-signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v1 iQIcBAEBAgAGBQJWAzq2AAoJEGw4ysog2bOSoeAQAIN4dVIkhZ9JsN8eWRH2xO/i ownqxthAKk029jyOkcvqDBFHMpAJkte5JSHv+U27r7ngjtYaNm1OiadGvUKQx3ct 1GyHGc/L+H1nEzPnTAzEDsQQusZ234yrrPl2n/5LvNkzcmP6Puk+eY8CZ6do+nYf CB3eH2CWI/K2N9trp3kdoNZ+gPepTCmINHbK43qXVIsJ3SeSsz0pFUFMfi6E3o2j kWj0lMgy+ojTLiOE3Jh5mbwW0/WJ+Yw8szyZqID064Rerlg30OjTmkX5jvBV36il tsxTcbUTcGiXgDKNIsQYRPhxxMgQCrS1aaknzCsX4VKdD4TWp6Ktw4ZfIEJQZ5/2 CPXRbk9hN4u6zvZvkLP6tc1TGbDFOodJutQiLAI2uOeBNRduVjMMsDXV4GaVlo/5 eWbCJzZRj7zU/EUdDM+no2JNzTS1l0uxpghRf/AIaDUlfwq5Ft6EHh2C8aOvi24W dFS3LDFxzCAGw7Af/1T0FAyRbWGoVrdYxs851ghHCvgEmaxcUUYBMQxKALkqu/MI fV1BxHmOF8Yex39ivV+Fw01thSq+nlHnPqiQ8qsfhZn3n4gZIlpMsgEpO1w00qk3 32mxlNlnjJwS89L62yHhGqAHEcFBdTFmglRXeKKKRjcxF8o0LHiR1bj7knZUKMWC CagmnU3GUb/Vud27cT7Y =xbNr -----END PGP SIGNATURE----- --fWddYNRDgTk9wQGZ--