vhost: move IOTLB functions from vhost-backend.c to vhost.c
Move and rename vhost_backend_update_device_iotlb(), vhost_backend_invalidate_device_iotlb(), and vhost_backend_handle_iotlb_msg() from vhost-backend.c to vhost.c. vhost-backend.c is actually about vhost-kernel backend. But these functions are shared with vhost-user, so let's move them into generic place. Moreover, two of three functions becomes static as they are used only in vhost.c. 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-2-vsementsov@yandex-team.ru>
Vladimir Sementsov-Ogievskiy committed
Apr 20, 2026 at 23:20 UTC
e86a41c24adb0391b6d3de8d3f58d9fc044ff9b7
5 files changed
+86
-96
hw/virtio/vhost-backend.c
+2
-80
@@ -298,7 +298,7 @@ static void vhost_kernel_iotlb_read(void *opaque)
298
break;
299
}
300
301
- vhost_backend_handle_iotlb_msg(dev, &msg.iotlb);
301
+ vhost_handle_iotlb_msg(dev, &msg.iotlb);
302
}
303
} else {
304
struct vhost_msg msg;
@@ -313,7 +313,7 @@ static void vhost_kernel_iotlb_read(void *opaque)
313
break;
314
}
315
316
- vhost_backend_handle_iotlb_msg(dev, &msg.iotlb);
316
+ vhost_handle_iotlb_msg(dev, &msg.iotlb);
317
}
318
}
319
}
@@ -392,81 +392,3 @@ const VhostOps kernel_ops = {
392
.vhost_send_device_iotlb_msg = vhost_kernel_send_device_iotlb_msg,
393
};
394
#endif
395
-
396
-int vhost_backend_update_device_iotlb(struct vhost_dev *dev,
397
- uint64_t iova, uint64_t uaddr,
398
- uint64_t len,
399
- IOMMUAccessFlags perm)
400
-{
401
- struct vhost_iotlb_msg imsg;
402
-
403
- imsg.iova = iova;
404
- imsg.uaddr = uaddr;
405
- imsg.size = len;
406
- imsg.type = VHOST_IOTLB_UPDATE;
407
-
408
- switch (perm) {
409
- case IOMMU_RO:
410
- imsg.perm = VHOST_ACCESS_RO;
411
- break;
412
- case IOMMU_WO:
413
- imsg.perm = VHOST_ACCESS_WO;
414
- break;
415
- case IOMMU_RW:
416
- imsg.perm = VHOST_ACCESS_RW;
417
- break;
418
- default:
419
- return -EINVAL;
420
- }
421
-
422
- if (dev->vhost_ops && dev->vhost_ops->vhost_send_device_iotlb_msg)
423
- return dev->vhost_ops->vhost_send_device_iotlb_msg(dev, &imsg);
424
-
425
- return -ENODEV;
426
-}
427
-
428
-int vhost_backend_invalidate_device_iotlb(struct vhost_dev *dev,
429
- uint64_t iova, uint64_t len)
430
-{
431
- struct vhost_iotlb_msg imsg;
432
-
433
- imsg.iova = iova;
434
- imsg.size = len;
435
- imsg.type = VHOST_IOTLB_INVALIDATE;
436
-
437
- if (dev->vhost_ops && dev->vhost_ops->vhost_send_device_iotlb_msg)
438
- return dev->vhost_ops->vhost_send_device_iotlb_msg(dev, &imsg);
439
-
440
- return -ENODEV;
441
-}
442
-
443
-int vhost_backend_handle_iotlb_msg(struct vhost_dev *dev,
444
- struct vhost_iotlb_msg *imsg)
445
-{
446
- int ret = 0;
447
-
448
- if (unlikely(!dev->vdev)) {
449
- error_report("Unexpected IOTLB message when virtio device is stopped");
450
- return -EINVAL;
451
- }
452
-
453
- switch (imsg->type) {
454
- case VHOST_IOTLB_MISS:
455
- ret = vhost_device_iotlb_miss(dev, imsg->iova,
456
- imsg->perm != VHOST_ACCESS_RO);
457
- break;
458
- case VHOST_IOTLB_ACCESS_FAIL:
459
- /* FIXME: report device iotlb error */
460
- error_report("Access failure IOTLB message type not supported");
461
- ret = -ENOTSUP;
462
- break;
463
- case VHOST_IOTLB_UPDATE:
464
- case VHOST_IOTLB_INVALIDATE:
465
- default:
466
- error_report("Unexpected IOTLB message type");
467
- ret = -EINVAL;
468
- break;
469
- }
470
-
471
- return ret;
472
-}
hw/virtio/vhost-user.c
+1
-1
@@ -1901,7 +1901,7 @@ static gboolean backend_read(QIOChannel *ioc, GIOCondition condition,
1901
1902
switch (hdr.request) {
1903
case VHOST_USER_BACKEND_IOTLB_MSG:
1904
- ret = vhost_backend_handle_iotlb_msg(dev, &payload.iotlb);
1904
+ ret = vhost_handle_iotlb_msg(dev, &payload.iotlb);
1905
break;
1906
case VHOST_USER_BACKEND_CONFIG_CHANGE_MSG:
1907
ret = vhost_user_backend_handle_config_change(dev);
hw/virtio/vhost.c
+81
-4
@@ -916,14 +916,92 @@ static void vhost_region_addnop(MemoryListener *listener,
916
vhost_region_add_section(dev, section);
917
}
918
919
+static int vhost_update_device_iotlb(struct vhost_dev *dev,
920
+ uint64_t iova, uint64_t uaddr,
921
+ uint64_t len,
922
+ IOMMUAccessFlags perm)
923
+{
924
+ struct vhost_iotlb_msg imsg;
925
+
926
+ imsg.iova = iova;
927
+ imsg.uaddr = uaddr;
928
+ imsg.size = len;
929
+ imsg.type = VHOST_IOTLB_UPDATE;
930
+
931
+ switch (perm) {
932
+ case IOMMU_RO:
933
+ imsg.perm = VHOST_ACCESS_RO;
934
+ break;
935
+ case IOMMU_WO:
936
+ imsg.perm = VHOST_ACCESS_WO;
937
+ break;
938
+ case IOMMU_RW:
939
+ imsg.perm = VHOST_ACCESS_RW;
940
+ break;
941
+ default:
942
+ return -EINVAL;
943
+ }
944
+
945
+ if (dev->vhost_ops && dev->vhost_ops->vhost_send_device_iotlb_msg) {
946
+ return dev->vhost_ops->vhost_send_device_iotlb_msg(dev, &imsg);
947
+ }
948
+
949
+ return -ENODEV;
950
+}
951
+
952
+static int vhost_invalidate_device_iotlb(struct vhost_dev *dev,
953
+ uint64_t iova, uint64_t len)
954
+{
955
+ struct vhost_iotlb_msg imsg;
956
+
957
+ imsg.iova = iova;
958
+ imsg.size = len;
959
+ imsg.type = VHOST_IOTLB_INVALIDATE;
960
+
961
+ if (dev->vhost_ops && dev->vhost_ops->vhost_send_device_iotlb_msg) {
962
+ return dev->vhost_ops->vhost_send_device_iotlb_msg(dev, &imsg);
963
+ }
964
+
965
+ return -ENODEV;
966
+}
967
+
968
+int vhost_handle_iotlb_msg(struct vhost_dev *dev, struct vhost_iotlb_msg *imsg)
969
+{
970
+ int ret = 0;
971
+
972
+ if (unlikely(!dev->vdev)) {
973
+ error_report("Unexpected IOTLB message when virtio device is stopped");
974
+ return -EINVAL;
975
+ }
976
+
977
+ switch (imsg->type) {
978
+ case VHOST_IOTLB_MISS:
979
+ ret = vhost_device_iotlb_miss(dev, imsg->iova,
980
+ imsg->perm != VHOST_ACCESS_RO);
981
+ break;
982
+ case VHOST_IOTLB_ACCESS_FAIL:
983
+ /* FIXME: report device iotlb error */
984
+ error_report("Access failure IOTLB message type not supported");
985
+ ret = -ENOTSUP;
986
+ break;
987
+ case VHOST_IOTLB_UPDATE:
988
+ case VHOST_IOTLB_INVALIDATE:
989
+ default:
990
+ error_report("Unexpected IOTLB message type");
991
+ ret = -EINVAL;
992
+ break;
993
+ }
994
+
995
+ return ret;
996
+}
997
+
998
static void vhost_iommu_unmap_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)
999
{
1000
struct vhost_iommu *iommu = container_of(n, struct vhost_iommu, n);
1001
struct vhost_dev *hdev = iommu->hdev;
1002
hwaddr iova = iotlb->iova + iommu->iommu_offset;
1003
925
- if (vhost_backend_invalidate_device_iotlb(hdev, iova,
926
- iotlb->addr_mask + 1)) {
1004
+ if (vhost_invalidate_device_iotlb(hdev, iova, iotlb->addr_mask + 1)) {
1005
error_report("Fail to invalidate device iotlb");
1006
}
1007
}
@@ -1304,8 +1382,7 @@ int vhost_device_iotlb_miss(struct vhost_dev *dev, uint64_t iova, int write)
1382
len = MIN(iotlb.addr_mask + 1, len);
1383
iova = iova & ~iotlb.addr_mask;
1384
1307
- ret = vhost_backend_update_device_iotlb(dev, iova, uaddr,
1308
- len, iotlb.perm);
1385
+ ret = vhost_update_device_iotlb(dev, iova, uaddr, len, iotlb.perm);
1386
if (ret) {
1387
trace_vhost_iotlb_miss(dev, 4);
1388
error_report("Fail to update device iotlb");
include/hw/virtio/vhost-backend.h
-11
@@ -222,17 +222,6 @@ typedef struct VhostOps {
222
vhost_check_device_state_op vhost_check_device_state;
223
} VhostOps;
224
225
-int vhost_backend_update_device_iotlb(struct vhost_dev *dev,
226
- uint64_t iova, uint64_t uaddr,
227
- uint64_t len,
228
- IOMMUAccessFlags perm);
229
-
230
-int vhost_backend_invalidate_device_iotlb(struct vhost_dev *dev,
231
- uint64_t iova, uint64_t len);
232
-
233
-int vhost_backend_handle_iotlb_msg(struct vhost_dev *dev,
234
- struct vhost_iotlb_msg *imsg);
235
-
225
int vhost_user_gpu_set_socket(struct vhost_dev *dev, int fd);
226
227
int vhost_user_get_shared_object(struct vhost_dev *dev, unsigned char *uuid,
include/hw/virtio/vhost.h
+2
@@ -403,6 +403,8 @@ int vhost_dev_set_inflight(struct vhost_dev *dev,
403
int vhost_dev_get_inflight(struct vhost_dev *dev, uint16_t queue_size,
404
struct vhost_inflight *inflight);
405
bool vhost_dev_has_iommu(struct vhost_dev *dev);
406
+int vhost_handle_iotlb_msg(struct vhost_dev *dev, struct vhost_iotlb_msg *imsg);
407
+
408
409
static inline bool vhost_dev_has_feature(struct vhost_dev *dev,
410
uint64_t feature)