@samitouri / QOSamiQemu / commits / 846a29de2b

vhost: simplify vhost_memory_map() and vhost_memory_unmap()

Make these functions simple wrappers around address_space_map() and address_space_unmap(). Move IOMMU handling logic one layer up to the callers. Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru> Reviewed-by: Michael S. Tsirkin <mst@redhat.com> Based-on: <20260206095258.894504-1-vsementsov@yandex-team.ru> Signed-off-by: Michael S. Tsirkin <mst@redhat.com> Message-Id: <20260420202032.714884-6-vsementsov@yandex-team.ru>

Vladimir Sementsov-Ogievskiy committed Apr 20, 2026 at 23:20 UTC 846a29de2bd2a299acf54af2169c14b52fba6706
1 file changed +24 -19
hw/virtio/vhost.c
+24 -19
@@ -456,21 +456,17 @@ static inline void vhost_dev_log_resize(struct vhost_dev *dev, uint64_t size)
456 static void *vhost_memory_map(struct vhost_dev *dev, hwaddr addr,
457 hwaddr len, bool is_write)
458 {
459 - if (!vhost_dev_has_iommu(dev)) {
460 - hwaddr mapped_len = len;
461 - void *res = address_space_map(dev->vdev->dma_as, addr, &mapped_len,
462 - is_write, MEMTXATTRS_UNSPECIFIED);
463 - if (!res) {
464 - return NULL;
465 - }
466 - if (len != mapped_len) {
467 - address_space_unmap(dev->vdev->dma_as, res, mapped_len, 0, 0);
468 - return NULL;
469 - }
470 - return res;
471 - } else {
472 - return (void *)(uintptr_t)addr;
459 + hwaddr mapped_len = len;
460 + void *res = address_space_map(dev->vdev->dma_as, addr, &mapped_len,
461 + is_write, MEMTXATTRS_UNSPECIFIED);
462 + if (!res) {
463 + return NULL;
464 + }
465 + if (len != mapped_len) {
466 + address_space_unmap(dev->vdev->dma_as, res, mapped_len, 0, 0);
467 + return NULL;
468 }
469 + return res;
470 }
471
472 static void vhost_memory_unmap(struct vhost_dev *dev, void **buffer,
@@ -481,17 +477,18 @@ static void vhost_memory_unmap(struct vhost_dev *dev, void **buffer,
477 return;
478 }
479
484 - if (!vhost_dev_has_iommu(dev)) {
485 - address_space_unmap(dev->vdev->dma_as, *buffer, len, is_write,
486 - access_len);
487 - }
488 -
480 + address_space_unmap(dev->vdev->dma_as, *buffer, len, is_write,
481 + access_len);
482 *buffer = NULL;
483 }
484
485 static void vhost_vrings_unmap(struct vhost_dev *dev,
486 struct vhost_virtqueue *vq, bool touched)
487 {
488 + if (vhost_dev_has_iommu(dev)) {
489 + return;
490 + }
491 +
492 vhost_memory_unmap(dev, &vq->used, vq->used_size, touched,
493 touched ? vq->used_size : 0);
494 vhost_memory_unmap(dev, &vq->avail, vq->avail_size, 0,
@@ -519,6 +516,14 @@ static int vhost_vrings_map(struct vhost_dev *dev,
516 /* Queue might not be ready for start */
517 return 0;
518 }
519 +
520 + if (vhost_dev_has_iommu(dev)) {
521 + vq->desc = (void *)(uintptr_t)vq->desc_phys;
522 + vq->avail = (void *)(uintptr_t)vq->avail_phys;
523 + vq->used = (void *)(uintptr_t)vq->used_phys;
524 + return 1;
525 + }
526 +
527 vq->desc = vhost_memory_map(dev, vq->desc_phys, vq->desc_size, false);
528 if (!vq->desc) {
529 goto fail;