* [Qemu-devel] [PATCH] virito: unbreak virtio device behinds IOMMU with region cache
@ 2017-03-03 9:06 Jason Wang
2017-03-03 12:56 ` Paolo Bonzini
0 siblings, 1 reply; 3+ messages in thread
From: Jason Wang @ 2017-03-03 9:06 UTC (permalink / raw)
To: mst, qemu-devel; +Cc: peterx, marcel, Jason Wang, Paolo Bonzini
Commit c611c76417f5 ("virtio: add MemoryListener to cache ring
translations") registers a memory listener to dma_as. This may not
work when IOMMU is enabled: dma_as(bus_master_as) were correctly
initialized in pcibus_machine_done() after virtio_realize() where we
try to register listener and initialize address space cache.
Fixing this by:
- delay the listener register to status set
- reset dma_as before trying to initialize address spaces to make sure
it works even IOMMU were created after virtio device
Fixes: c611c76417f5 ("virtio: add MemoryListener to cache ring translations")
Cc: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Jason Wang <jasowang@redhat.com>
---
hw/virtio/virtio.c | 29 ++++++++++++++++++++++++++++-
include/hw/virtio/virtio.h | 1 +
2 files changed, 29 insertions(+), 1 deletion(-)
diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c
index 23483c7..179030c 100644
--- a/hw/virtio/virtio.c
+++ b/hw/virtio/virtio.c
@@ -1084,9 +1084,22 @@ int virtio_set_status(VirtIODevice *vdev, uint8_t val)
}
}
}
+
+ virtio_device_reset_dma_as(vdev);
+
+ if (val == (VIRTIO_CONFIG_S_ACKNOWLEDGE | VIRTIO_CONFIG_S_DRIVER)) {
+ memory_listener_unregister(&vdev->listener);
+ memory_listener_register(&vdev->listener, vdev->dma_as);
+ }
+
if (k->set_status) {
k->set_status(vdev, val);
}
+
+ if (val == 0) {
+ memory_listener_unregister(&vdev->listener);
+ }
+
vdev->status = val;
return 0;
}
@@ -2402,7 +2415,6 @@ static void virtio_device_realize(DeviceState *dev, Error **errp)
}
vdev->listener.commit = virtio_memory_listener_commit;
- memory_listener_register(&vdev->listener, vdev->dma_as);
}
static void virtio_device_unrealize(DeviceState *dev, Error **errp)
@@ -2576,6 +2588,21 @@ bool virtio_device_ioeventfd_enabled(VirtIODevice *vdev)
return virtio_bus_ioeventfd_enabled(vbus);
}
+void virtio_device_reset_dma_as(VirtIODevice *vdev)
+{
+ DeviceState *qdev = DEVICE(vdev);
+ BusState *qbus = BUS(qdev_get_parent_bus(qdev));
+ VirtioBusState *bus = VIRTIO_BUS(qbus);
+ VirtioBusClass *klass = VIRTIO_BUS_GET_CLASS(bus);
+ bool has_iommu = virtio_host_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM);
+
+ if (klass->get_dma_as != NULL && has_iommu) {
+ vdev->dma_as = klass->get_dma_as(qbus->parent);
+ } else {
+ vdev->dma_as = &address_space_memory;
+ }
+}
+
static const TypeInfo virtio_device_info = {
.name = TYPE_VIRTIO_DEVICE,
.parent = TYPE_DEVICE,
diff --git a/include/hw/virtio/virtio.h b/include/hw/virtio/virtio.h
index 15efcf2..f7e0b4a 100644
--- a/include/hw/virtio/virtio.h
+++ b/include/hw/virtio/virtio.h
@@ -289,6 +289,7 @@ void virtio_queue_aio_set_host_notifier_handler(VirtQueue *vq, AioContext *ctx,
VirtIOHandleAIOOutput handle_output);
VirtQueue *virtio_vector_first_queue(VirtIODevice *vdev, uint16_t vector);
VirtQueue *virtio_vector_next_queue(VirtQueue *vq);
+void virtio_device_reset_dma_as(VirtIODevice *vdev);
static inline void virtio_add_feature(uint64_t *features, unsigned int fbit)
{
--
2.7.4
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [Qemu-devel] [PATCH] virito: unbreak virtio device behinds IOMMU with region cache
2017-03-03 9:06 [Qemu-devel] [PATCH] virito: unbreak virtio device behinds IOMMU with region cache Jason Wang
@ 2017-03-03 12:56 ` Paolo Bonzini
2017-03-06 7:21 ` Jason Wang
0 siblings, 1 reply; 3+ messages in thread
From: Paolo Bonzini @ 2017-03-03 12:56 UTC (permalink / raw)
To: Jason Wang, mst, qemu-devel; +Cc: peterx, marcel
On 03/03/2017 10:06, Jason Wang wrote:
> Commit c611c76417f5 ("virtio: add MemoryListener to cache ring
> translations") registers a memory listener to dma_as. This may not
> work when IOMMU is enabled: dma_as(bus_master_as) were correctly
> initialized in pcibus_machine_done() after virtio_realize() where we
> try to register listener and initialize address space cache.
>
> Fixing this by:
>
> - delay the listener register to status set
> - reset dma_as before trying to initialize address spaces to make sure
> it works even IOMMU were created after virtio device
>
> Fixes: c611c76417f5 ("virtio: add MemoryListener to cache ring translations")
> Cc: Paolo Bonzini <pbonzini@redhat.com>
> Signed-off-by: Jason Wang <jasowang@redhat.com>
For virtio 0.9 it is valid to use the virtio device while the status is 0.
You can add a function virtio_set_dma_as to generic virtio that does a
MemoryListener unregister+register, then PCI can call it when the
AddressSpace is ready.
Paolo
> ---
> hw/virtio/virtio.c | 29 ++++++++++++++++++++++++++++-
> include/hw/virtio/virtio.h | 1 +
> 2 files changed, 29 insertions(+), 1 deletion(-)
>
> diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c
> index 23483c7..179030c 100644
> --- a/hw/virtio/virtio.c
> +++ b/hw/virtio/virtio.c
> @@ -1084,9 +1084,22 @@ int virtio_set_status(VirtIODevice *vdev, uint8_t val)
> }
> }
> }
> +
> + virtio_device_reset_dma_as(vdev);
> +
> + if (val == (VIRTIO_CONFIG_S_ACKNOWLEDGE | VIRTIO_CONFIG_S_DRIVER)) {
> + memory_listener_unregister(&vdev->listener);
> + memory_listener_register(&vdev->listener, vdev->dma_as);
> + }
> +
> if (k->set_status) {
> k->set_status(vdev, val);
> }
> +
> + if (val == 0) {
> + memory_listener_unregister(&vdev->listener);
> + }
> +
> vdev->status = val;
> return 0;
> }
> @@ -2402,7 +2415,6 @@ static void virtio_device_realize(DeviceState *dev, Error **errp)
> }
>
> vdev->listener.commit = virtio_memory_listener_commit;
> - memory_listener_register(&vdev->listener, vdev->dma_as);
> }
>
> static void virtio_device_unrealize(DeviceState *dev, Error **errp)
> @@ -2576,6 +2588,21 @@ bool virtio_device_ioeventfd_enabled(VirtIODevice *vdev)
> return virtio_bus_ioeventfd_enabled(vbus);
> }
>
> +void virtio_device_reset_dma_as(VirtIODevice *vdev)
> +{
> + DeviceState *qdev = DEVICE(vdev);
> + BusState *qbus = BUS(qdev_get_parent_bus(qdev));
> + VirtioBusState *bus = VIRTIO_BUS(qbus);
> + VirtioBusClass *klass = VIRTIO_BUS_GET_CLASS(bus);
> + bool has_iommu = virtio_host_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM);
> +
> + if (klass->get_dma_as != NULL && has_iommu) {
> + vdev->dma_as = klass->get_dma_as(qbus->parent);
> + } else {
> + vdev->dma_as = &address_space_memory;
> + }
> +}
> +
> static const TypeInfo virtio_device_info = {
> .name = TYPE_VIRTIO_DEVICE,
> .parent = TYPE_DEVICE,
> diff --git a/include/hw/virtio/virtio.h b/include/hw/virtio/virtio.h
> index 15efcf2..f7e0b4a 100644
> --- a/include/hw/virtio/virtio.h
> +++ b/include/hw/virtio/virtio.h
> @@ -289,6 +289,7 @@ void virtio_queue_aio_set_host_notifier_handler(VirtQueue *vq, AioContext *ctx,
> VirtIOHandleAIOOutput handle_output);
> VirtQueue *virtio_vector_first_queue(VirtIODevice *vdev, uint16_t vector);
> VirtQueue *virtio_vector_next_queue(VirtQueue *vq);
> +void virtio_device_reset_dma_as(VirtIODevice *vdev);
>
> static inline void virtio_add_feature(uint64_t *features, unsigned int fbit)
> {
>
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [Qemu-devel] [PATCH] virito: unbreak virtio device behinds IOMMU with region cache
2017-03-03 12:56 ` Paolo Bonzini
@ 2017-03-06 7:21 ` Jason Wang
0 siblings, 0 replies; 3+ messages in thread
From: Jason Wang @ 2017-03-06 7:21 UTC (permalink / raw)
To: Paolo Bonzini, mst, qemu-devel; +Cc: marcel, peterx
On 2017年03月03日 20:56, Paolo Bonzini wrote:
>
> On 03/03/2017 10:06, Jason Wang wrote:
>> Commit c611c76417f5 ("virtio: add MemoryListener to cache ring
>> translations") registers a memory listener to dma_as. This may not
>> work when IOMMU is enabled: dma_as(bus_master_as) were correctly
>> initialized in pcibus_machine_done() after virtio_realize() where we
>> try to register listener and initialize address space cache.
>>
>> Fixing this by:
>>
>> - delay the listener register to status set
>> - reset dma_as before trying to initialize address spaces to make sure
>> it works even IOMMU were created after virtio device
>>
>> Fixes: c611c76417f5 ("virtio: add MemoryListener to cache ring translations")
>> Cc: Paolo Bonzini <pbonzini@redhat.com>
>> Signed-off-by: Jason Wang <jasowang@redhat.com>
> For virtio 0.9 it is valid to use the virtio device while the status is 0.
>
> You can add a function virtio_set_dma_as to generic virtio that does a
> MemoryListener unregister+register, then PCI can call it when the
> AddressSpace is ready.
>
> Paolo
>
>
Ok, will post a new version.
Thanks
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2017-03-06 7:21 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-03-03 9:06 [Qemu-devel] [PATCH] virito: unbreak virtio device behinds IOMMU with region cache Jason Wang
2017-03-03 12:56 ` Paolo Bonzini
2017-03-06 7:21 ` Jason Wang
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).