Received: from eggs.gnu.org ([2001:4830:134:3::10]:57372) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b5ngh-0001Y5-Re for qemu-devel@nongnu.org; Thu, 26 May 2016 01:18:30 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1b5ngc-00064e-Lh for qemu-devel@nongnu.org; Thu, 26 May 2016 01:18:26 -0400 Date: Thu, 26 May 2016 14:53:06 +1000 From: David Gibson Message-ID: <20160526045306.GC17226@voom.fritz.box> References: <1462344751-28281-1-git-send-email-aik@ozlabs.ru> <1462344751-28281-15-git-send-email-aik@ozlabs.ru> <20160513162544.27e4f053@t450s.home> <748d24c7-467f-cf4d-9b27-908a9e074961@ozlabs.ru> <20160516142033.72b8ae71@t450s.home> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="v4PrGfol6wGsg/Ra" Content-Disposition: inline In-Reply-To: <20160516142033.72b8ae71@t450s.home> Subject: Re: [Qemu-devel] [PATCH qemu v16 14/19] vfio: spapr: Add DMA memory preregistering (SPAPR IOMMU v2) List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Alex Williamson Cc: Alexey Kardashevskiy , qemu-devel@nongnu.org, qemu-ppc@nongnu.org, Alexander Graf , Paolo Bonzini --v4PrGfol6wGsg/Ra Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Mon, May 16, 2016 at 02:20:33PM -0600, Alex Williamson wrote: > On Mon, 16 May 2016 11:10:05 +1000 > Alexey Kardashevskiy wrote: >=20 > > On 05/14/2016 08:25 AM, Alex Williamson wrote: > > > On Wed, 4 May 2016 16:52:26 +1000 > > > Alexey Kardashevskiy wrote: > > > =20 > > >> This makes use of the new "memory registering" feature. The idea is > > >> to provide the userspace ability to notify the host kernel about pag= es > > >> which are going to be used for DMA. Having this information, the host > > >> kernel can pin them all once per user process, do locked pages > > >> accounting (once) and not spent time on doing that in real time with > > >> possible failures which cannot be handled nicely in some cases. > > >> > > >> This adds a prereg memory listener which listens on address_space_me= mory > > >> and notifies a VFIO container about memory which needs to be > > >> pinned/unpinned. VFIO MMIO regions (i.e. "skip dump" regions) are sk= ipped. > > >> > > >> As there is no per-IOMMU-type release() callback anymore, this stores > > >> the IOMMU type in the container so vfio_listener_release() can deter= mine > > >> if it needs to unregister @prereg_listener. > > >> > > >> The feature is only enabled for SPAPR IOMMU v2. The host kernel chan= ges > > >> are required. Since v2 does not need/support VFIO_IOMMU_ENABLE, this= does > > >> not call it when v2 is detected and enabled. > > >> > > >> This enforces guest RAM blocks to be host page size aligned; however > > >> this is not new as KVM already requires memory slots to be host page > > >> size aligned. > > >> > > >> Signed-off-by: Alexey Kardashevskiy > > >> --- > > >> Changes: > > >> v16: > > >> * switched to 64bit math everywhere as there is no chance to see > > >> region_add on RAM blocks even remotely close to 1<<64bytes. > > >> > > >> v15: > > >> * banned unaligned sections > > >> * added an vfio_prereg_gpa_to_ua() helper > > >> > > >> v14: > > >> * s/free_container_exit/listener_release_exit/g > > >> * added "if memory_region_is_iommu()" to vfio_prereg_listener_skippe= d_section > > >> --- > > >> hw/vfio/Makefile.objs | 1 + > > >> hw/vfio/common.c | 38 +++++++++--- > > >> hw/vfio/prereg.c | 137 +++++++++++++++++++++++++++++++= +++++++++++ > > >> include/hw/vfio/vfio-common.h | 4 ++ > > >> trace-events | 2 + > > >> 5 files changed, 172 insertions(+), 10 deletions(-) > > >> create mode 100644 hw/vfio/prereg.c > > >> > > >> diff --git a/hw/vfio/Makefile.objs b/hw/vfio/Makefile.objs > > >> index ceddbb8..5800e0e 100644 > > >> --- a/hw/vfio/Makefile.objs > > >> +++ b/hw/vfio/Makefile.objs > > >> @@ -4,4 +4,5 @@ obj-$(CONFIG_PCI) +=3D pci.o pci-quirks.o > > >> obj-$(CONFIG_SOFTMMU) +=3D platform.o > > >> obj-$(CONFIG_SOFTMMU) +=3D calxeda-xgmac.o > > >> obj-$(CONFIG_SOFTMMU) +=3D amd-xgbe.o > > >> +obj-$(CONFIG_SOFTMMU) +=3D prereg.o > > >> endif > > >> diff --git a/hw/vfio/common.c b/hw/vfio/common.c > > >> index 2050040..496eb82 100644 > > >> --- a/hw/vfio/common.c > > >> +++ b/hw/vfio/common.c > > >> @@ -501,6 +501,9 @@ static const MemoryListener vfio_memory_listener= =3D { > > >> static void vfio_listener_release(VFIOContainer *container) > > >> { > > >> memory_listener_unregister(&container->listener); > > >> + if (container->iommu_type =3D=3D VFIO_SPAPR_TCE_v2_IOMMU) { > > >> + memory_listener_unregister(&container->prereg_listener); > > >> + } > > >> } > > >> > > >> int vfio_region_setup(Object *obj, VFIODevice *vbasedev, VFIORegion= *region, > > >> @@ -808,8 +811,8 @@ static int vfio_connect_container(VFIOGroup *gro= up, AddressSpace *as) > > >> goto free_container_exit; > > >> } > > >> > > >> - ret =3D ioctl(fd, VFIO_SET_IOMMU, > > >> - v2 ? VFIO_TYPE1v2_IOMMU : VFIO_TYPE1_IOMMU); > > >> + container->iommu_type =3D v2 ? VFIO_TYPE1v2_IOMMU : VFIO_TY= PE1_IOMMU; > > >> + ret =3D ioctl(fd, VFIO_SET_IOMMU, container->iommu_type); > > >> if (ret) { > > >> error_report("vfio: failed to set iommu for container: = %m"); > > >> ret =3D -errno; > > >> @@ -834,8 +837,10 @@ static int vfio_connect_container(VFIOGroup *gr= oup, AddressSpace *as) > > >> if ((ret =3D=3D 0) && (info.flags & VFIO_IOMMU_INFO_PGSIZES= )) { > > >> container->iova_pgsizes =3D info.iova_pgsizes; > > >> } > > >> - } else if (ioctl(fd, VFIO_CHECK_EXTENSION, VFIO_SPAPR_TCE_IOMMU= )) { > > >> + } else if (ioctl(fd, VFIO_CHECK_EXTENSION, VFIO_SPAPR_TCE_IOMMU= ) || > > >> + ioctl(fd, VFIO_CHECK_EXTENSION, VFIO_SPAPR_TCE_v2_IO= MMU)) { > > >> struct vfio_iommu_spapr_tce_info info; > > >> + bool v2 =3D !!ioctl(fd, VFIO_CHECK_EXTENSION, VFIO_SPAPR_TC= E_v2_IOMMU); > > >> > > >> ret =3D ioctl(group->fd, VFIO_GROUP_SET_CONTAINER, &fd); > > >> if (ret) { > > >> @@ -843,7 +848,9 @@ static int vfio_connect_container(VFIOGroup *gro= up, AddressSpace *as) > > >> ret =3D -errno; > > >> goto free_container_exit; > > >> } > > >> - ret =3D ioctl(fd, VFIO_SET_IOMMU, VFIO_SPAPR_TCE_IOMMU); > > >> + container->iommu_type =3D > > >> + v2 ? VFIO_SPAPR_TCE_v2_IOMMU : VFIO_SPAPR_TCE_IOMMU; > > >> + ret =3D ioctl(fd, VFIO_SET_IOMMU, container->iommu_type); > > >> if (ret) { > > >> error_report("vfio: failed to set iommu for container: = %m"); > > >> ret =3D -errno; > > >> @@ -855,11 +862,22 @@ static int vfio_connect_container(VFIOGroup *g= roup, AddressSpace *as) > > >> * when container fd is closed so we do not call it explici= tly > > >> * in this file. > > >> */ > > >> - ret =3D ioctl(fd, VFIO_IOMMU_ENABLE); > > >> - if (ret) { > > >> - error_report("vfio: failed to enable container: %m"); > > >> - ret =3D -errno; > > >> - goto free_container_exit; > > >> + if (!v2) { > > >> + ret =3D ioctl(fd, VFIO_IOMMU_ENABLE); > > >> + if (ret) { > > >> + error_report("vfio: failed to enable container: %m"= ); > > >> + ret =3D -errno; > > >> + goto free_container_exit; > > >> + } > > >> + } else { > > >> + container->prereg_listener =3D vfio_prereg_listener; > > >> + > > >> + memory_listener_register(&container->prereg_listener, > > >> + &address_space_memory); > > >> + if (container->error) { > > >> + error_report("vfio: RAM memory listener initializat= ion failed for container"); > > >> + goto listener_release_exit; > > >> + } > > >> } > > >> > > >> /* > > >> @@ -872,7 +890,7 @@ static int vfio_connect_container(VFIOGroup *gro= up, AddressSpace *as) > > >> if (ret) { > > >> error_report("vfio: VFIO_IOMMU_SPAPR_TCE_GET_INFO faile= d: %m"); > > >> ret =3D -errno; > > >> - goto free_container_exit; > > >> + goto listener_release_exit; > > >> } > > >> container->min_iova =3D info.dma32_window_start; > > >> container->max_iova =3D container->min_iova + info.dma32_wi= ndow_size - 1; > > >> diff --git a/hw/vfio/prereg.c b/hw/vfio/prereg.c > > >> new file mode 100644 > > >> index 0000000..d0e4728 > > >> --- /dev/null > > >> +++ b/hw/vfio/prereg.c > > >> @@ -0,0 +1,137 @@ > > >> +/* > > >> + * DMA memory preregistration > > >> + * > > >> + * Authors: > > >> + * Alexey Kardashevskiy > > >> + * > > >> + * This work is licensed under the terms of the GNU GPL, version 2.= See > > >> + * the COPYING file in the top-level directory. > > >> + */ > > >> + > > >> +#include "qemu/osdep.h" > > >> +#include > > >> +#include > > >> + > > >> +#include "hw/vfio/vfio-common.h" > > >> +#include "hw/hw.h" > > >> +#include "qemu/error-report.h" > > >> +#include "trace.h" > > >> + > > >> +static bool vfio_prereg_listener_skipped_section(MemoryRegionSectio= n *section) > > >> +{ > > >> + if (memory_region_is_iommu(section->mr)) { > > >> + error_report("Cannot possibly preregister IOMMU memory"); = =20 > > > > > > What is a user supposed to do with this error_report()? Is it > > > continue-able? How is it possible? What should they do differently?= =20 > >=20 > >=20 > > If I remember correctly, David did have theories where this may be=20 > > possible, not today with the existing code though. Could be assert() or= =20 > > abort(), what is better here? >=20 > If it's a hardware configuration error, then use hw_error(), I prefer > not to add either assert() or abort() calls to vfio. Personally I would have gone with assert() - hitting this is _almost_ certainly an indication of a bug in the code (an IOMMU translating into an AS that itself has IOMMUs). In theory a combination of a wierd platform with a multi-layered IOMMU and the wrong config options could trigger it, but it's very unlikely. hw_error() is fine by me too. > > >> + return true; > > >> + } > > >> + > > >> + return !memory_region_is_ram(section->mr) || > > >> + memory_region_is_skip_dump(section->mr); > > >> +} > > >> + > > >> +static void *vfio_prereg_gpa_to_ua(MemoryRegionSection *section, hw= addr gpa) =20 > > > > > > What's "ua"? =20 > >=20 > >=20 > > Userspace address. >=20 > But we use it to set a vaddr below, so let's just call it vaddr. >=20 > > > =20 > > >> +{ > > >> + return memory_region_get_ram_ptr(section->mr) + > > >> + section->offset_within_region + > > >> + (gpa - section->offset_within_address_space); > > >> +} > > >> + > > >> +static void vfio_prereg_listener_region_add(MemoryListener *listene= r, > > >> + MemoryRegionSection *se= ction) > > >> +{ > > >> + VFIOContainer *container =3D container_of(listener, VFIOContain= er, > > >> + prereg_listener); > > >> + const hwaddr gpa =3D section->offset_within_address_space; > > >> + hwaddr end; > > >> + int ret; > > >> + hwaddr page_mask =3D qemu_real_host_page_mask; > > >> + struct vfio_iommu_spapr_register_memory reg =3D { > > >> + .argsz =3D sizeof(reg), > > >> + .flags =3D 0, > > >> + }; =20 > > > > > > So we're just pretending that this spapr specific code is some sort of > > > generic pre-registration interface? =20 > >=20 > > Yes. >=20 > :-\ It's a bit of an odd mix because the actual caps and ioctls are spapr specific, but there's nothing inherently spapr specific about the concept of pre-registration. It's only implemented for spapr now, but pre-reg would probably be a good idea for performance on any platform where the guest expects to actively manage an IOMMU with a reasonably small window. >=20 > > >> + > > >> + if (vfio_prereg_listener_skipped_section(section)) { > > >> + trace_vfio_listener_region_add_skip( > > >> + section->offset_within_address_space, > > >> + section->offset_within_address_space + > > >> + int128_get64(int128_sub(section->size, int128_one()= ))); > > >> + return; > > >> + } > > >> + > > >> + if (unlikely((section->offset_within_address_space & ~page_mask= ) || > > >> + (section->offset_within_region & ~page_mask) || > > >> + (int128_get64(section->size) & ~page_mask))) { > > >> + error_report("%s received unaligned region", __func__); > > >> + return; > > >> + } > > >> + > > >> + end =3D section->offset_within_address_space + int128_get64(sec= tion->size); > > >> + g_assert(gpa < end); > > >> + > > >> + memory_region_ref(section->mr); > > >> + > > >> + reg.vaddr =3D (__u64) vfio_prereg_gpa_to_ua(section, gpa); =20 > > > > > > Hmm, why wasn't that simply gpa_to_vaddr? =20 > >=20 > > I wanted to keep a prefix in all functions, even if they are static, ea= sier=20 > > to grep. Bad idea? >=20 > My question about "ua" means that it's not obvious what we're returning > based on the name of the function alone, so I would avoid such a name. >=20 > > >> + reg.size =3D end - gpa; > > >> + > > >> + ret =3D ioctl(container->fd, VFIO_IOMMU_SPAPR_REGISTER_MEMORY, = ®); > > >> + trace_vfio_ram_register(reg.vaddr, reg.size, ret ? -errno : 0); > > >> + if (ret) { > > >> + /* > > >> + * On the initfn path, store the first error in the contain= er so we > > >> + * can gracefully fail. Runtime, there's not much we can d= o other > > >> + * than throw a hardware error. > > >> + */ > > >> + if (!container->initialized) { > > >> + if (!container->error) { > > >> + container->error =3D ret; > > >> + } > > >> + } else { > > >> + hw_error("vfio: Memory registering failed, unable to co= ntinue"); > > >> + } > > >> + } > > >> +} > > >> + > > >> +static void vfio_prereg_listener_region_del(MemoryListener *listene= r, > > >> + MemoryRegionSection *se= ction) > > >> +{ > > >> + VFIOContainer *container =3D container_of(listener, VFIOContain= er, > > >> + prereg_listener); > > >> + const hwaddr gpa =3D section->offset_within_address_space; > > >> + hwaddr end; > > >> + int ret; > > >> + hwaddr page_mask =3D qemu_real_host_page_mask; > > >> + struct vfio_iommu_spapr_register_memory reg =3D { > > >> + .argsz =3D sizeof(reg), > > >> + .flags =3D 0, > > >> + }; > > >> + > > >> + if (vfio_prereg_listener_skipped_section(section)) { > > >> + trace_vfio_listener_region_del_skip( > > >> + section->offset_within_address_space, > > >> + section->offset_within_address_space + > > >> + int128_get64(int128_sub(section->size, int128_one()= ))); > > >> + return; > > >> + } > > >> + > > >> + if (unlikely((section->offset_within_address_space & ~page_mask= ) || > > >> + (section->offset_within_region & ~page_mask) || > > >> + (int128_get64(section->size) & ~page_mask))) { > > >> + error_report("%s received unaligned region", __func__); > > >> + return; > > >> + } > > >> + > > >> + end =3D section->offset_within_address_space + int128_get64(sec= tion->size); > > >> + if (gpa >=3D end) { > > >> + return; > > >> + } > > >> + > > >> + reg.vaddr =3D (__u64) vfio_prereg_gpa_to_ua(section, gpa); > > >> + reg.size =3D end - gpa; > > >> + > > >> + ret =3D ioctl(container->fd, VFIO_IOMMU_SPAPR_UNREGISTER_MEMORY= , ®); > > >> + trace_vfio_ram_unregister(reg.vaddr, reg.size, ret ? -errno : 0= ); > > >> +} > > >> + > > >> +const MemoryListener vfio_prereg_listener =3D { > > >> + .region_add =3D vfio_prereg_listener_region_add, > > >> + .region_del =3D vfio_prereg_listener_region_del, > > >> +}; > > >> diff --git a/include/hw/vfio/vfio-common.h b/include/hw/vfio/vfio-co= mmon.h > > >> index c9b6622..c72e45a 100644 > > >> --- a/include/hw/vfio/vfio-common.h > > >> +++ b/include/hw/vfio/vfio-common.h > > >> @@ -73,6 +73,8 @@ typedef struct VFIOContainer { > > >> VFIOAddressSpace *space; > > >> int fd; /* /dev/vfio/vfio, empowered by the attached groups */ > > >> MemoryListener listener; > > >> + MemoryListener prereg_listener; > > >> + unsigned iommu_type; > > >> int error; > > >> bool initialized; > > >> /* > > >> @@ -156,4 +158,6 @@ extern QLIST_HEAD(vfio_as_head, VFIOAddressSpace= ) vfio_address_spaces; > > >> int vfio_get_region_info(VFIODevice *vbasedev, int index, > > >> struct vfio_region_info **info); > > >> #endif > > >> +extern const MemoryListener vfio_prereg_listener; > > >> + > > >> #endif /* !HW_VFIO_VFIO_COMMON_H */ > > >> diff --git a/trace-events b/trace-events > > >> index dd50005..d0d8615 100644 > > >> --- a/trace-events > > >> +++ b/trace-events > > >> @@ -1737,6 +1737,8 @@ vfio_region_mmap(const char *name, unsigned lo= ng offset, unsigned long end) "Reg > > >> vfio_region_exit(const char *name, int index) "Device %s, region %d" > > >> vfio_region_finalize(const char *name, int index) "Device %s, regio= n %d" > > >> vfio_region_mmaps_set_enabled(const char *name, bool enabled) "Regi= on %s mmaps enabled: %d" > > >> +vfio_ram_register(uint64_t va, uint64_t size, int ret) "va=3D%"PRIx= 64" size=3D%"PRIx64" ret=3D%d" > > >> +vfio_ram_unregister(uint64_t va, uint64_t size, int ret) "va=3D%"PR= Ix64" size=3D%"PRIx64" ret=3D%d" > > >> > > >> # hw/vfio/platform.c > > >> vfio_platform_base_device_init(char *name, int groupid) "%s belongs= to group #%d" =20 > >=20 > >=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 --v4PrGfol6wGsg/Ra Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v1 iQIcBAEBAgAGBQJXRoEyAAoJEGw4ysog2bOS4voP/jjZ1qrr+AGKybjNG6ec2N6q RNGNwr43IEdNq/7mSfwyfesCa6hR9q6xi1yOoYd4flpI+tkU/I272wl7LXrzRQaf AIJfSlvRmIWTn4sbPfSYccgKjO9f/HFMFzhr7W6rka2Cn9JxCLh+0G3knp4L+eIc AEc8Apf5i3aITXQg4UIHeOtQGYiGAhK7aDIArMuaTLf9MMZyciUzoFhYklaptG+Z +mNrSqNTv49495U6S0uiur9EtfBdV0anVdXf5pBaqW2dlpuJoEH9vQoHXnPxNqog 98ux2Iqvd+jv0RpNCI9qb0A4i3Zci8K6nIBK+sTTLsVvf9E8NxDeuGisLA/91raX xObDVx2B8PU1Wkqc3tjdVjMu2YFrKDyT/go+Nec8yIK7TnaaTn6fgkmoia25YscV US6cs04TmJP66rvolSVzJBwyTuePPw1wi22j3o6r2cLptjw/sxzOys+CPKREpqfu MDHT2PQqVrhPK2qzj76YPD3k9guUz0tauJNOIQviS74wpovU6DAnv1KB55+xns1k afDW0wbRdsKf/s8zF8Beqmqwkmm3NT5PK1/U//PcqOp/Idtq4pl8Y8D3BAZAais0 CzQKfMDK51KtqYVgJS3bEYSlSL/zq4WwRt3LSIx+yeoetJytvFCQ+lM4thxvCa5O xrou2ympuZkO7auuG8r4 =u7UO -----END PGP SIGNATURE----- --v4PrGfol6wGsg/Ra--