If vhost is enabled for virtio-net, Device-TLB enable/disable events
must be passed to vhost for proper IOMMU unmap flag selection.
Signed-off-by: Viktor Prutyanov <viktor@daynix.com>
---
hw/net/vhost_net.c | 11 +++++++++++
hw/net/virtio-net.c | 8 ++++++++
include/net/vhost_net.h | 2 ++
3 files changed, 21 insertions(+)
diff --git a/hw/net/vhost_net.c b/hw/net/vhost_net.c
index c4eecc6f36..2364c8de99 100644
--- a/hw/net/vhost_net.c
+++ b/hw/net/vhost_net.c
@@ -552,6 +552,17 @@ int vhost_net_set_mtu(struct vhost_net *net, uint16_t mtu)
return vhost_ops->vhost_net_set_mtu(&net->dev, mtu);
}
+void vhost_net_toggle_device_iotlb(struct vhost_dev *dev, bool enable)
+{
+ const VhostOps *vhost_ops = dev->vhost_ops;
+
+ if (!vhost_ops->vhost_toggle_device_iotlb) {
+ return;
+ }
+
+ vhost_ops->vhost_toggle_device_iotlb(dev, enable);
+}
+
void vhost_net_virtqueue_reset(VirtIODevice *vdev, NetClientState *nc,
int vq_index)
{
diff --git a/hw/net/virtio-net.c b/hw/net/virtio-net.c
index 53e1c32643..e6851b885c 100644
--- a/hw/net/virtio-net.c
+++ b/hw/net/virtio-net.c
@@ -3843,6 +3843,13 @@ static struct vhost_dev *virtio_net_get_vhost(VirtIODevice *vdev)
return &net->dev;
}
+static void virtio_net_toggle_device_iotlb(VirtIODevice *vdev,
+ bool enable)
+{
+ if (vdev->vhost_started)
+ vhost_net_toggle_device_iotlb(virtio_net_get_vhost(vdev), enable);
+}
+
static const VMStateDescription vmstate_virtio_net = {
.name = "virtio-net",
.minimum_version_id = VIRTIO_NET_VM_VERSION,
@@ -3948,6 +3955,7 @@ static void virtio_net_class_init(ObjectClass *klass, void *data)
vdc->vmsd = &vmstate_virtio_net_device;
vdc->primary_unplug_pending = primary_unplug_pending;
vdc->get_vhost = virtio_net_get_vhost;
+ vdc->toggle_device_iotlb = virtio_net_toggle_device_iotlb;
}
static const TypeInfo virtio_net_info = {
diff --git a/include/net/vhost_net.h b/include/net/vhost_net.h
index c37aba35e6..36d527f321 100644
--- a/include/net/vhost_net.h
+++ b/include/net/vhost_net.h
@@ -56,4 +56,6 @@ int vhost_net_virtqueue_restart(VirtIODevice *vdev, NetClientState *nc,
int vq_index);
void vhost_net_save_acked_features(NetClientState *nc);
+
+void vhost_net_toggle_device_iotlb(struct vhost_dev *dev, bool enable);
#endif
--
2.35.1
On Mon, May 1, 2023 at 10:02 AM Viktor Prutyanov <viktor@daynix.com> wrote:
>
> If vhost is enabled for virtio-net, Device-TLB enable/disable events
> must be passed to vhost for proper IOMMU unmap flag selection.
The patch looks good, just wonder if you have tested it with vhost-user?
(It looks to me like it should work there).
Thanks
>
> Signed-off-by: Viktor Prutyanov <viktor@daynix.com>
> ---
> hw/net/vhost_net.c | 11 +++++++++++
> hw/net/virtio-net.c | 8 ++++++++
> include/net/vhost_net.h | 2 ++
> 3 files changed, 21 insertions(+)
>
> diff --git a/hw/net/vhost_net.c b/hw/net/vhost_net.c
> index c4eecc6f36..2364c8de99 100644
> --- a/hw/net/vhost_net.c
> +++ b/hw/net/vhost_net.c
> @@ -552,6 +552,17 @@ int vhost_net_set_mtu(struct vhost_net *net, uint16_t mtu)
> return vhost_ops->vhost_net_set_mtu(&net->dev, mtu);
> }
>
> +void vhost_net_toggle_device_iotlb(struct vhost_dev *dev, bool enable)
> +{
> + const VhostOps *vhost_ops = dev->vhost_ops;
> +
> + if (!vhost_ops->vhost_toggle_device_iotlb) {
> + return;
> + }
> +
> + vhost_ops->vhost_toggle_device_iotlb(dev, enable);
> +}
> +
> void vhost_net_virtqueue_reset(VirtIODevice *vdev, NetClientState *nc,
> int vq_index)
> {
> diff --git a/hw/net/virtio-net.c b/hw/net/virtio-net.c
> index 53e1c32643..e6851b885c 100644
> --- a/hw/net/virtio-net.c
> +++ b/hw/net/virtio-net.c
> @@ -3843,6 +3843,13 @@ static struct vhost_dev *virtio_net_get_vhost(VirtIODevice *vdev)
> return &net->dev;
> }
>
> +static void virtio_net_toggle_device_iotlb(VirtIODevice *vdev,
> + bool enable)
> +{
> + if (vdev->vhost_started)
> + vhost_net_toggle_device_iotlb(virtio_net_get_vhost(vdev), enable);
> +}
> +
> static const VMStateDescription vmstate_virtio_net = {
> .name = "virtio-net",
> .minimum_version_id = VIRTIO_NET_VM_VERSION,
> @@ -3948,6 +3955,7 @@ static void virtio_net_class_init(ObjectClass *klass, void *data)
> vdc->vmsd = &vmstate_virtio_net_device;
> vdc->primary_unplug_pending = primary_unplug_pending;
> vdc->get_vhost = virtio_net_get_vhost;
> + vdc->toggle_device_iotlb = virtio_net_toggle_device_iotlb;
> }
>
> static const TypeInfo virtio_net_info = {
> diff --git a/include/net/vhost_net.h b/include/net/vhost_net.h
> index c37aba35e6..36d527f321 100644
> --- a/include/net/vhost_net.h
> +++ b/include/net/vhost_net.h
> @@ -56,4 +56,6 @@ int vhost_net_virtqueue_restart(VirtIODevice *vdev, NetClientState *nc,
> int vq_index);
>
> void vhost_net_save_acked_features(NetClientState *nc);
> +
> +void vhost_net_toggle_device_iotlb(struct vhost_dev *dev, bool enable);
> #endif
> --
> 2.35.1
>
On Mon, May 8, 2023 at 8:26 AM Jason Wang <jasowang@redhat.com> wrote:
>
> On Mon, May 1, 2023 at 10:02 AM Viktor Prutyanov <viktor@daynix.com> wrote:
> >
> > If vhost is enabled for virtio-net, Device-TLB enable/disable events
> > must be passed to vhost for proper IOMMU unmap flag selection.
>
> The patch looks good, just wonder if you have tested it with vhost-user?
Not yet, because I don't know which use case to test. Actually, the
device_iotlb_enabled flag logic doesn't rely on a backend. And that's
really enough to fix the problem for all the guests I know at the
moment. The trigger logic which turned out to be backend-aware is
needed only if ATS is enabled/disabled in runtime and I create such a
situation manually and test that IOMMU flag re-registration is done.
Thanks,
Viktor Prutyanov
>
> (It looks to me like it should work there).
>
> Thanks
>
> >
> > Signed-off-by: Viktor Prutyanov <viktor@daynix.com>
> > ---
> > hw/net/vhost_net.c | 11 +++++++++++
> > hw/net/virtio-net.c | 8 ++++++++
> > include/net/vhost_net.h | 2 ++
> > 3 files changed, 21 insertions(+)
> >
> > diff --git a/hw/net/vhost_net.c b/hw/net/vhost_net.c
> > index c4eecc6f36..2364c8de99 100644
> > --- a/hw/net/vhost_net.c
> > +++ b/hw/net/vhost_net.c
> > @@ -552,6 +552,17 @@ int vhost_net_set_mtu(struct vhost_net *net, uint16_t mtu)
> > return vhost_ops->vhost_net_set_mtu(&net->dev, mtu);
> > }
> >
> > +void vhost_net_toggle_device_iotlb(struct vhost_dev *dev, bool enable)
> > +{
> > + const VhostOps *vhost_ops = dev->vhost_ops;
> > +
> > + if (!vhost_ops->vhost_toggle_device_iotlb) {
> > + return;
> > + }
> > +
> > + vhost_ops->vhost_toggle_device_iotlb(dev, enable);
> > +}
> > +
> > void vhost_net_virtqueue_reset(VirtIODevice *vdev, NetClientState *nc,
> > int vq_index)
> > {
> > diff --git a/hw/net/virtio-net.c b/hw/net/virtio-net.c
> > index 53e1c32643..e6851b885c 100644
> > --- a/hw/net/virtio-net.c
> > +++ b/hw/net/virtio-net.c
> > @@ -3843,6 +3843,13 @@ static struct vhost_dev *virtio_net_get_vhost(VirtIODevice *vdev)
> > return &net->dev;
> > }
> >
> > +static void virtio_net_toggle_device_iotlb(VirtIODevice *vdev,
> > + bool enable)
> > +{
> > + if (vdev->vhost_started)
> > + vhost_net_toggle_device_iotlb(virtio_net_get_vhost(vdev), enable);
> > +}
> > +
> > static const VMStateDescription vmstate_virtio_net = {
> > .name = "virtio-net",
> > .minimum_version_id = VIRTIO_NET_VM_VERSION,
> > @@ -3948,6 +3955,7 @@ static void virtio_net_class_init(ObjectClass *klass, void *data)
> > vdc->vmsd = &vmstate_virtio_net_device;
> > vdc->primary_unplug_pending = primary_unplug_pending;
> > vdc->get_vhost = virtio_net_get_vhost;
> > + vdc->toggle_device_iotlb = virtio_net_toggle_device_iotlb;
> > }
> >
> > static const TypeInfo virtio_net_info = {
> > diff --git a/include/net/vhost_net.h b/include/net/vhost_net.h
> > index c37aba35e6..36d527f321 100644
> > --- a/include/net/vhost_net.h
> > +++ b/include/net/vhost_net.h
> > @@ -56,4 +56,6 @@ int vhost_net_virtqueue_restart(VirtIODevice *vdev, NetClientState *nc,
> > int vq_index);
> >
> > void vhost_net_save_acked_features(NetClientState *nc);
> > +
> > +void vhost_net_toggle_device_iotlb(struct vhost_dev *dev, bool enable);
> > #endif
> > --
> > 2.35.1
> >
>
© 2016 - 2026 Red Hat, Inc.