Received: from eggs.gnu.org ([2001:4830:134:3::10]:38216) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1c7E9G-0001XF-6i for qemu-devel@nongnu.org; Wed, 16 Nov 2016 23:18:07 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1c7E9E-0002Rg-Bh for qemu-devel@nongnu.org; Wed, 16 Nov 2016 23:18:06 -0500 Received: from ozlabs.org ([2401:3900:2:1::2]:57823) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1c7E9D-0002Oe-SN for qemu-devel@nongnu.org; Wed, 16 Nov 2016 23:18:04 -0500 Date: Thu, 17 Nov 2016 14:24:54 +1100 From: David Gibson Message-ID: <20161117032454.GE18808@umbus.fritz.box> References: <1479171568-19175-1-git-send-email-peterx@redhat.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="a+b56+3nqLzpiR9O" Content-Disposition: inline In-Reply-To: <1479171568-19175-1-git-send-email-peterx@redhat.com> Subject: Re: [Qemu-devel] [PATCH] vfio: avoid adding same iommu mr for notify List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Peter Xu Cc: qemu-devel@nongnu.org, alex.williamson@redhat.com, bd.aviv@gmail.com --a+b56+3nqLzpiR9O Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Mon, Nov 14, 2016 at 07:59:28PM -0500, Peter Xu wrote: > When one IOMMU memory region is splitted into multiple memory sections, > vfio will register multiple same notifiers to a vIOMMU for the same > region. That's not sensible. What we need is to register one IOMMU > notifier for each IOMMU region, not per section. >=20 > Solution is simple - we traverse the container->giommu_list, and skip > the registration if memory region is already registered. >=20 > To make vfio's region_add() short, vfio_listener_region_add_iommu() is > introduced. >=20 > Signed-off-by: Peter Xu This is wrong. It will work on the register side, but when the first section attached to the IOMMU is removed, the IOMMU will be removed =66rom the list (triggering an unmap of the whole vfio space), rather than when the *last* section attached to the MR is removed. You'll get away with it in the simple x86 case, because both sections will be removed at basically the same time, but it's not correct in general. I really think a better approach is to add the section boundary information to the IOMMUNotifier structure within VFIOGuestIOMMU, and check that as well as the MR on remove. That additionally means the IOMMU notifier won't get called for portions of the MR outside the mapped sections, which the notifier handler probably isn't going to expect. > --- > hw/vfio/common.c | 56 +++++++++++++++++++++++++++++++++++---------------= ------ > 1 file changed, 35 insertions(+), 21 deletions(-) >=20 > diff --git a/hw/vfio/common.c b/hw/vfio/common.c > index 801578b..5279fd1 100644 > --- a/hw/vfio/common.c > +++ b/hw/vfio/common.c > @@ -360,6 +360,40 @@ out: > rcu_read_unlock(); > } > =20 > +static void vfio_listener_region_add_iommu(VFIOContainer *container, > + MemoryRegionSection *section, > + hwaddr iova, > + hwaddr end) > +{ > + VFIOGuestIOMMU *giommu; > + > + QLIST_FOREACH(giommu, &container->giommu_list, giommu_next) { > + if (giommu->iommu =3D=3D section->mr) { > + /* We have already registered with this MR, skip */ > + return; > + } > + } > + > + trace_vfio_listener_region_add_iommu(iova, end); > + > + /* > + * FIXME: For VFIO iommu types which have KVM acceleration to > + * avoid bouncing all map/unmaps through qemu this way, this > + * would be the right place to wire that up (tell the KVM > + * device emulation the VFIO iommu handles to use). > + */ > + giommu =3D g_malloc0(sizeof(*giommu)); > + giommu->iommu =3D section->mr; > + giommu->iommu_offset =3D section->offset_within_address_space - > + section->offset_within_region; > + giommu->container =3D container; > + giommu->n.notify =3D vfio_iommu_map_notify; > + giommu->n.notifier_flags =3D IOMMU_NOTIFIER_ALL; > + QLIST_INSERT_HEAD(&container->giommu_list, giommu, giommu_next); > + memory_region_register_iommu_notifier(giommu->iommu, &giommu->n); > + memory_region_iommu_replay(giommu->iommu, &giommu->n, false); > +} > + > static void vfio_listener_region_add(MemoryListener *listener, > MemoryRegionSection *section) > { > @@ -439,27 +473,7 @@ static void vfio_listener_region_add(MemoryListener = *listener, > memory_region_ref(section->mr); > =20 > if (memory_region_is_iommu(section->mr)) { > - VFIOGuestIOMMU *giommu; > - > - trace_vfio_listener_region_add_iommu(iova, end); > - /* > - * FIXME: For VFIO iommu types which have KVM acceleration to > - * avoid bouncing all map/unmaps through qemu this way, this > - * would be the right place to wire that up (tell the KVM > - * device emulation the VFIO iommu handles to use). > - */ > - giommu =3D g_malloc0(sizeof(*giommu)); > - giommu->iommu =3D section->mr; > - giommu->iommu_offset =3D section->offset_within_address_space - > - section->offset_within_region; > - giommu->container =3D container; > - giommu->n.notify =3D vfio_iommu_map_notify; > - giommu->n.notifier_flags =3D IOMMU_NOTIFIER_ALL; > - QLIST_INSERT_HEAD(&container->giommu_list, giommu, giommu_next); > - > - memory_region_register_iommu_notifier(giommu->iommu, &giommu->n); > - memory_region_iommu_replay(giommu->iommu, &giommu->n, false); > - > + vfio_listener_region_add_iommu(container, section, iova, end); > return; > } > =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 --a+b56+3nqLzpiR9O Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v2 iQIcBAEBCAAGBQJYLSMGAAoJEGw4ysog2bOSv4gP/1W2mRc8YSs45FYjJz6QzKgL s5g03Vw4ytCkhEPMzUlLahelwuYsJJCf6sgjgrxUM4z1JCIpFBqQcX46M4XEAjTi D0MmXp3IIUnOwpp2GBOj8fEdqRY1uCNkNdfgF7iQ9IIHTBAn+wAJQav0BgC2jIe1 32ddGeiVPGCMC9HfFZFyYUbUG5TdixgDeGsLeTuOc5eWOfPKkoql2j5/wpmSDcSx /doDko9jgRp8r/sxOdcSrDYYOiV/3tN+lcsOImr5nkLhFQ0HpoXM9YRRihwGdz4h prPWcEZWZnV4GS7XS05xz2GSOIBIDDqUWnSRXPWjoB/glM3T5DmNZOINwo5ovNuw y6VDO9AyxhZQSShbbg/T5NVcvKrntCfFsvtnd+l4yVTfQiqkDXD2NVRbnCH0YjJp tykUUpr9gIEY0nkUyISlye9bSqeJYFBRe6yXYVwFAwa0J0Gk1ITpB2GU05AJR0iy VHe7xXUSktb67vNAJ29mViHYfjTi8UkZwd0p7wm76BEKX9FGr8lNt44cJiw3YyGe XMOHSnzsO9a5J86hIJG6DNTaK59kF/Y2TA2QHoBakguta3d9DQCyDr25bCZdo3qX Q7cUJuy05UPIlygK2CZZ+2XHsyoDyZxukhZNB0W3DPtEO10ZU7OEd/EWc7ojpk+1 YplaqiW70f3t7gi86VcS =7e6Y -----END PGP SIGNATURE----- --a+b56+3nqLzpiR9O--