Anshuman Khandual
2018-Jul-28 08:56 UTC
[RFC 2/4] virtio: Override device's DMA OPS with virtio_direct_dma_ops selectively
On 07/20/2018 09:29 AM, Anshuman Khandual wrote:> Now that virtio core always needs all virtio devices to have DMA OPS, we > need to make sure that the structure it points is the right one. In the > absence of VIRTIO_F_IOMMU_PLATFORM flag QEMU expects GPA from guest kernel. > In such case, virtio device must use default virtio_direct_dma_ops DMA OPS > structure which transforms scatter gather buffer addresses as GPA. This > DMA OPS override must happen as early as possible during virtio device > initializatin sequence before virtio core starts using given device's DMA > OPS callbacks for I/O transactions. This change detects device's IOMMU flag > and does the override in case the flag is cleared. > > Signed-off-by: Anshuman Khandual <khandual at linux.vnet.ibm.com> > --- > drivers/virtio/virtio.c | 5 +++++ > 1 file changed, 5 insertions(+) > > diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c > index 7907ad3..6b13987 100644 > --- a/drivers/virtio/virtio.c > +++ b/drivers/virtio/virtio.c > @@ -166,6 +166,8 @@ void virtio_add_status(struct virtio_device *dev, unsigned int status) > } > EXPORT_SYMBOL_GPL(virtio_add_status); > > +const struct dma_map_ops virtio_direct_dma_ops; > + > int virtio_finalize_features(struct virtio_device *dev) > { > int ret = dev->config->finalize_features(dev); > @@ -174,6 +176,9 @@ int virtio_finalize_features(struct virtio_device *dev) > if (ret) > return ret;The previous patch removed the code block for XEN guests which forced the use of DMA API all the time irrespective of VIRTIO_F_IOMMU_PLATFORM flag on the device. Here is what I have removed with patch 2/4 which breaks the existing semantics on XEN guests. -static bool vring_use_dma_api(struct virtio_device *vdev) -{ - if (!virtio_has_iommu_quirk(vdev)) - return true; - - /* Otherwise, we are left to guess. */ - /* - * In theory, it's possible to have a buggy QEMU-supposed - * emulated Q35 IOMMU and Xen enabled at the same time. On - * such a configuration, virtio has never worked and will - * not work without an even larger kludge. Instead, enable - * the DMA API if we're a Xen guest, which at least allows - * all of the sensible Xen configurations to work correctly. - */ - if (xen_domain()) - return true; - - return false; -} XEN guests would not like override with virtio_direct_dma_ops in any case irrespective of the flag VIRTIO_F_IOMMU_PLATFORM. So the existing semantics can be preserved with something like this. It just assumes that dev->dma_ops is non-NULL and a valid one set by the architecture. If required we can add those tests here before skipping the override. diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c index 7907ad3..6b13987 100644 --- a/drivers/virtio/virtio.c +++ b/drivers/virtio/virtio.c @@ -166,6 +166,8 @@ void virtio_add_status(struct virtio_device *dev, unsigned int status) } EXPORT_SYMBOL_GPL(virtio_add_status); +const struct dma_map_ops virtio_direct_dma_ops; + int virtio_finalize_features(struct virtio_device *dev) { int ret = dev->config->finalize_features(dev); @@ -174,6 +176,9 @@ int virtio_finalize_features(struct virtio_device *dev) if (ret) return ret; + + if (xen_domain()) + goto skip_override; + + if (virtio_has_iommu_quirk(dev)) + set_dma_ops(dev->dev.parent, &virtio_direct_dma_ops); + + skip_override: + if (!virtio_has_feature(dev, VIRTIO_F_VERSION_1)) return 0 Will incorporate these changes in the next version.
Michael S. Tsirkin
2018-Jul-28 21:16 UTC
[RFC 2/4] virtio: Override device's DMA OPS with virtio_direct_dma_ops selectively
On Sat, Jul 28, 2018 at 02:26:24PM +0530, Anshuman Khandual wrote:> On 07/20/2018 09:29 AM, Anshuman Khandual wrote: > > Now that virtio core always needs all virtio devices to have DMA OPS, we > > need to make sure that the structure it points is the right one. In the > > absence of VIRTIO_F_IOMMU_PLATFORM flag QEMU expects GPA from guest kernel. > > In such case, virtio device must use default virtio_direct_dma_ops DMA OPS > > structure which transforms scatter gather buffer addresses as GPA. This > > DMA OPS override must happen as early as possible during virtio device > > initializatin sequence before virtio core starts using given device's DMA > > OPS callbacks for I/O transactions. This change detects device's IOMMU flag > > and does the override in case the flag is cleared. > > > > Signed-off-by: Anshuman Khandual <khandual at linux.vnet.ibm.com> > > --- > > drivers/virtio/virtio.c | 5 +++++ > > 1 file changed, 5 insertions(+) > > > > diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c > > index 7907ad3..6b13987 100644 > > --- a/drivers/virtio/virtio.c > > +++ b/drivers/virtio/virtio.c > > @@ -166,6 +166,8 @@ void virtio_add_status(struct virtio_device *dev, unsigned int status) > > } > > EXPORT_SYMBOL_GPL(virtio_add_status); > > > > +const struct dma_map_ops virtio_direct_dma_ops; > > + > > int virtio_finalize_features(struct virtio_device *dev) > > { > > int ret = dev->config->finalize_features(dev); > > @@ -174,6 +176,9 @@ int virtio_finalize_features(struct virtio_device *dev) > > if (ret) > > return ret; > > > The previous patch removed the code block for XEN guests which forced > the use of DMA API all the time irrespective of VIRTIO_F_IOMMU_PLATFORM > flag on the device. Here is what I have removed with patch 2/4 which > breaks the existing semantics on XEN guests. > > -static bool vring_use_dma_api(struct virtio_device *vdev) > -{ > - if (!virtio_has_iommu_quirk(vdev)) > - return true; > - > - /* Otherwise, we are left to guess. */ > - /* > - * In theory, it's possible to have a buggy QEMU-supposed > - * emulated Q35 IOMMU and Xen enabled at the same time. On > - * such a configuration, virtio has never worked and will > - * not work without an even larger kludge. Instead, enable > - * the DMA API if we're a Xen guest, which at least allows > - * all of the sensible Xen configurations to work correctly. > - */ > - if (xen_domain()) > - return true; > - > - return false; > -} > > XEN guests would not like override with virtio_direct_dma_ops in any > case irrespective of the flag VIRTIO_F_IOMMU_PLATFORM. So the existing > semantics can be preserved with something like this. It just assumes > that dev->dma_ops is non-NULL and a valid one set by the architecture. > If required we can add those tests here before skipping the override. > > diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c > index 7907ad3..6b13987 100644 > --- a/drivers/virtio/virtio.c > +++ b/drivers/virtio/virtio.c > @@ -166,6 +166,8 @@ void virtio_add_status(struct virtio_device *dev, unsigned int status) > } > EXPORT_SYMBOL_GPL(virtio_add_status); > > +const struct dma_map_ops virtio_direct_dma_ops; > + > int virtio_finalize_features(struct virtio_device *dev) > { > int ret = dev->config->finalize_features(dev); > @@ -174,6 +176,9 @@ int virtio_finalize_features(struct virtio_device *dev) > if (ret) > return ret; > + > + if (xen_domain()) > + goto skip_override; > + > + if (virtio_has_iommu_quirk(dev)) > + set_dma_ops(dev->dev.parent, &virtio_direct_dma_ops); > + > + skip_override: > +I prefer normal if scoping as opposed to goto spaghetti pls. Better yet move vring_use_dma_api here and use it. Less of a chance something will break.> if (!virtio_has_feature(dev, VIRTIO_F_VERSION_1)) > return 0 > > Will incorporate these changes in the next version.
Anshuman Khandual
2018-Jul-30 04:15 UTC
[RFC 2/4] virtio: Override device's DMA OPS with virtio_direct_dma_ops selectively
On 07/29/2018 02:46 AM, Michael S. Tsirkin wrote:> On Sat, Jul 28, 2018 at 02:26:24PM +0530, Anshuman Khandual wrote: >> On 07/20/2018 09:29 AM, Anshuman Khandual wrote: >>> Now that virtio core always needs all virtio devices to have DMA OPS, we >>> need to make sure that the structure it points is the right one. In the >>> absence of VIRTIO_F_IOMMU_PLATFORM flag QEMU expects GPA from guest kernel. >>> In such case, virtio device must use default virtio_direct_dma_ops DMA OPS >>> structure which transforms scatter gather buffer addresses as GPA. This >>> DMA OPS override must happen as early as possible during virtio device >>> initializatin sequence before virtio core starts using given device's DMA >>> OPS callbacks for I/O transactions. This change detects device's IOMMU flag >>> and does the override in case the flag is cleared. >>> >>> Signed-off-by: Anshuman Khandual <khandual at linux.vnet.ibm.com> >>> --- >>> drivers/virtio/virtio.c | 5 +++++ >>> 1 file changed, 5 insertions(+) >>> >>> diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c >>> index 7907ad3..6b13987 100644 >>> --- a/drivers/virtio/virtio.c >>> +++ b/drivers/virtio/virtio.c >>> @@ -166,6 +166,8 @@ void virtio_add_status(struct virtio_device *dev, unsigned int status) >>> } >>> EXPORT_SYMBOL_GPL(virtio_add_status); >>> >>> +const struct dma_map_ops virtio_direct_dma_ops; >>> + >>> int virtio_finalize_features(struct virtio_device *dev) >>> { >>> int ret = dev->config->finalize_features(dev); >>> @@ -174,6 +176,9 @@ int virtio_finalize_features(struct virtio_device *dev) >>> if (ret) >>> return ret; >> >> >> The previous patch removed the code block for XEN guests which forced >> the use of DMA API all the time irrespective of VIRTIO_F_IOMMU_PLATFORM >> flag on the device. Here is what I have removed with patch 2/4 which >> breaks the existing semantics on XEN guests. >> >> -static bool vring_use_dma_api(struct virtio_device *vdev) >> -{ >> - if (!virtio_has_iommu_quirk(vdev)) >> - return true; >> - >> - /* Otherwise, we are left to guess. */ >> - /* >> - * In theory, it's possible to have a buggy QEMU-supposed >> - * emulated Q35 IOMMU and Xen enabled at the same time. On >> - * such a configuration, virtio has never worked and will >> - * not work without an even larger kludge. Instead, enable >> - * the DMA API if we're a Xen guest, which at least allows >> - * all of the sensible Xen configurations to work correctly. >> - */ >> - if (xen_domain()) >> - return true; >> - >> - return false; >> -} >> >> XEN guests would not like override with virtio_direct_dma_ops in any >> case irrespective of the flag VIRTIO_F_IOMMU_PLATFORM. So the existing >> semantics can be preserved with something like this. It just assumes >> that dev->dma_ops is non-NULL and a valid one set by the architecture. >> If required we can add those tests here before skipping the override. >> >> diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c >> index 7907ad3..6b13987 100644 >> --- a/drivers/virtio/virtio.c >> +++ b/drivers/virtio/virtio.c >> @@ -166,6 +166,8 @@ void virtio_add_status(struct virtio_device *dev, unsigned int status) >> } >> EXPORT_SYMBOL_GPL(virtio_add_status); >> >> +const struct dma_map_ops virtio_direct_dma_ops; >> + >> int virtio_finalize_features(struct virtio_device *dev) >> { >> int ret = dev->config->finalize_features(dev); >> @@ -174,6 +176,9 @@ int virtio_finalize_features(struct virtio_device *dev) >> if (ret) >> return ret; >> + >> + if (xen_domain()) >> + goto skip_override; >> + >> + if (virtio_has_iommu_quirk(dev)) >> + set_dma_ops(dev->dev.parent, &virtio_direct_dma_ops); >> + >> + skip_override: >> + > > I prefer normal if scoping as opposed to goto spaghetti pls. > Better yet move vring_use_dma_api here and use it. > Less of a chance something will break.Sure, will move vring_use_dma_api() function in here.
Christoph Hellwig
2018-Jul-30 09:30 UTC
[RFC 2/4] virtio: Override device's DMA OPS with virtio_direct_dma_ops selectively
> > + > > + if (xen_domain()) > > + goto skip_override; > > + > > + if (virtio_has_iommu_quirk(dev)) > > + set_dma_ops(dev->dev.parent, &virtio_direct_dma_ops); > > + > > + skip_override: > > + > > I prefer normal if scoping as opposed to goto spaghetti pls. > Better yet move vring_use_dma_api here and use it. > Less of a chance something will break.I agree about avoid pointless gotos here, but we can do things perfectly well without either gotos or a confusing helper here if we structure it right. E.g.: // suitably detailed comment here if (!xen_domain() && !virtio_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM)) set_dma_ops(dev->dev.parent, &virtio_direct_dma_ops); and while we're at it - modifying dma ops for the parent looks very dangerous. I don't think we can do that, as it could break iommu setup interactions. IFF we set a specific dma map ops it has to be on the virtio device itself, of which we have full control.
Possibly Parallel Threads
- [RFC 2/4] virtio: Override device's DMA OPS with virtio_direct_dma_ops selectively
- [RFC 2/4] virtio: Override device's DMA OPS with virtio_direct_dma_ops selectively
- [RFC 2/4] virtio: Override device's DMA OPS with virtio_direct_dma_ops selectively
- [RFC 4/4] virtio: Add platform specific DMA API translation for virito devices
- [RFC 4/4] virtio: Add platform specific DMA API translation for virito devices