From: Eric Auger <eric.auger@redhat.com>
To: eric.auger.pro@gmail.com, eric.auger@redhat.com, mst@redhat.com,
peterx@redhat.com, qemu-arm@nongnu.org, qemu-devel@nongnu.org,
eperezma@redhat.com, jasowang@redhat.com
Subject: [PATCH] vhost: Warn if DEVIOTLB_UNMAP is not supported and ats is set
Date: Tue, 18 Oct 2022 14:28:52 +0200 [thread overview]
Message-ID: <20221018122852.1185395-1-eric.auger@redhat.com> (raw)
Since b68ba1ca5767 ("memory: Add IOMMU_NOTIFIER_DEVIOTLB_UNMAP
IOMMUTLBNotificationType"), vhost attempts to register DEVIOTLB_UNMAP
notifier. This latter is supported by the intel-iommu which supports
device-iotlb if the corresponding option is set. Then 958ec334bca3
("vhost: Unbreak SMMU and virtio-iommu on dev-iotlb support") allowed
silent fallback to the legacy UNMAP notifier if the viommu does not
support device iotlb.
Initially vhost/viommu integration was introduced with intel iommu
assuming ats=on was set on virtio-pci device and device-iotlb was set
on the intel iommu. vhost acts as an ATS capable device since it
implements an IOTLB on kernel side. However translated transactions
that hit the device IOTLB do not transit through the vIOMMU. So this
requires a limited ATS support on viommu side.
However, in theory, if ats=on is set on a pci device, the
viommu should support ATS for that device to work.
But neither SMMUv3 nor virtio-iommu do support ATS and the integration
with vhost just relies on the fact those vIOMMU send UNMAP notifications
whenever the guest trigger them.
So the situation with respect to ats setting on virtio-pci side and
ats support on viommu side has become quite confusing, especially on
ARM. Is ats needed whereas vIOMMUs do not support device IOTLBs.
This patch makes sure we get a warning if ats is set on a device
protected by virtio-iommu or vsmmuv3, reminding that we don't have
full support of ATS on those vIOMMUs and ats setting on end-point
is not a requirement.
Signed-off-by: Eric Auger <eric.auger@redhat.com>
---
include/hw/virtio/virtio-bus.h | 3 +++
hw/virtio/vhost.c | 21 ++++++++++++++++++++-
hw/virtio/virtio-bus.c | 14 ++++++++++++++
hw/virtio/virtio-pci.c | 11 +++++++++++
4 files changed, 48 insertions(+), 1 deletion(-)
diff --git a/include/hw/virtio/virtio-bus.h b/include/hw/virtio/virtio-bus.h
index 7ab8c9dab0..df96cf5b4d 100644
--- a/include/hw/virtio/virtio-bus.h
+++ b/include/hw/virtio/virtio-bus.h
@@ -94,6 +94,7 @@ struct VirtioBusClass {
bool has_variable_vring_alignment;
AddressSpace *(*get_dma_as)(DeviceState *d);
bool (*iommu_enabled)(DeviceState *d);
+ bool (*ats_enabled)(DeviceState *d);
};
struct VirtioBusState {
@@ -157,4 +158,6 @@ int virtio_bus_set_host_notifier(VirtioBusState *bus, int n, bool assign);
void virtio_bus_cleanup_host_notifier(VirtioBusState *bus, int n);
/* Whether the IOMMU is enabled for this device */
bool virtio_bus_device_iommu_enabled(VirtIODevice *vdev);
+/* Whether ATS is enabled for this device */
+bool virtio_bus_device_ats_enabled(VirtIODevice *vdev);
#endif /* VIRTIO_BUS_H */
diff --git a/hw/virtio/vhost.c b/hw/virtio/vhost.c
index 5185c15295..b5ad4262c1 100644
--- a/hw/virtio/vhost.c
+++ b/hw/virtio/vhost.c
@@ -324,6 +324,16 @@ static bool vhost_dev_has_iommu(struct vhost_dev *dev)
}
}
+static bool vhost_dev_ats_enabled(struct vhost_dev *dev)
+{
+ VirtIODevice *vdev = dev->vdev;
+
+ if (vdev && virtio_bus_device_ats_enabled(vdev)) {
+ return true;
+ }
+ return false;
+}
+
static void *vhost_memory_map(struct vhost_dev *dev, hwaddr addr,
hwaddr *plen, bool is_write)
{
@@ -737,6 +747,7 @@ static void vhost_iommu_region_add(MemoryListener *listener,
Int128 end;
int iommu_idx;
IOMMUMemoryRegion *iommu_mr;
+ Error *err = NULL;
int ret;
if (!memory_region_is_iommu(section->mr)) {
@@ -760,8 +771,16 @@ static void vhost_iommu_region_add(MemoryListener *listener,
iommu->iommu_offset = section->offset_within_address_space -
section->offset_within_region;
iommu->hdev = dev;
- ret = memory_region_register_iommu_notifier(section->mr, &iommu->n, NULL);
+ ret = memory_region_register_iommu_notifier(section->mr, &iommu->n, &err);
if (ret) {
+ if (vhost_dev_ats_enabled(dev)) {
+ error_reportf_err(err,
+ "vhost cannot register DEVIOTLB_UNMAP "
+ "although ATS is enabled, "
+ "fall back to legacy UNMAP notifier: ");
+ } else {
+ error_free(err);
+ }
/*
* Some vIOMMUs do not support dev-iotlb yet. If so, try to use the
* UNMAP legacy message
diff --git a/hw/virtio/virtio-bus.c b/hw/virtio/virtio-bus.c
index 896feb37a1..6276779b07 100644
--- a/hw/virtio/virtio-bus.c
+++ b/hw/virtio/virtio-bus.c
@@ -348,6 +348,20 @@ bool virtio_bus_device_iommu_enabled(VirtIODevice *vdev)
return klass->iommu_enabled(qbus->parent);
}
+bool virtio_bus_device_ats_enabled(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);
+
+ if (!klass->ats_enabled) {
+ return false;
+ }
+
+ return klass->ats_enabled(qbus->parent);
+}
+
static void virtio_bus_class_init(ObjectClass *klass, void *data)
{
BusClass *bus_class = BUS_CLASS(klass);
diff --git a/hw/virtio/virtio-pci.c b/hw/virtio/virtio-pci.c
index e7d80242b7..d33a47abbf 100644
--- a/hw/virtio/virtio-pci.c
+++ b/hw/virtio/virtio-pci.c
@@ -1141,6 +1141,16 @@ static bool virtio_pci_iommu_enabled(DeviceState *d)
return true;
}
+static bool virtio_pci_ats_enabled(DeviceState *d)
+{
+ VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
+
+ if (proxy->flags & VIRTIO_PCI_FLAG_ATS) {
+ return true;
+ }
+ return false;
+}
+
static bool virtio_pci_queue_enabled(DeviceState *d, int n)
{
VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
@@ -2229,6 +2239,7 @@ static void virtio_pci_bus_class_init(ObjectClass *klass, void *data)
k->ioeventfd_assign = virtio_pci_ioeventfd_assign;
k->get_dma_as = virtio_pci_get_dma_as;
k->iommu_enabled = virtio_pci_iommu_enabled;
+ k->ats_enabled = virtio_pci_ats_enabled;
k->queue_enabled = virtio_pci_queue_enabled;
}
--
2.35.3
next reply other threads:[~2022-10-18 12:32 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-10-18 12:28 Eric Auger [this message]
2022-10-18 14:25 ` [PATCH] vhost: Warn if DEVIOTLB_UNMAP is not supported and ats is set Peter Xu
2022-10-18 15:08 ` Eric Auger
2022-10-18 21:56 ` Peter Xu
2022-10-19 5:41 ` Jason Wang
2022-10-19 11:25 ` Eric Auger
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20221018122852.1185395-1-eric.auger@redhat.com \
--to=eric.auger@redhat.com \
--cc=eperezma@redhat.com \
--cc=eric.auger.pro@gmail.com \
--cc=jasowang@redhat.com \
--cc=mst@redhat.com \
--cc=peterx@redhat.com \
--cc=qemu-arm@nongnu.org \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.