@samitouri / QOSamiQemu / commits / 4e61c89262

vhost: final refactoring of vhost vrings map/unmap

Introduce helper functions vhost_vrings_map() and vhost_vrings_unmap() and use them. Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru> Reviewed-by: Raphael Norwitz <raphael.s.norwitz@gmail.com> Reviewed-by: Michael S. Tsirkin <mst@redhat.com> Signed-off-by: Michael S. Tsirkin <mst@redhat.com> Message-Id: <20260420200339.708640-16-vsementsov@yandex-team.ru>

Vladimir Sementsov-Ogievskiy committed Apr 20, 2026 at 23:03 UTC 4e61c89262aa7fad36df39daf5d7ab759079bd9a
1 file changed +55 -34
hw/virtio/vhost.c
+55 -34
@@ -489,6 +489,56 @@ static void vhost_memory_unmap(struct vhost_dev *dev, void **buffer,
489 *buffer = NULL;
490 }
491
492 +static void vhost_vrings_unmap(struct vhost_dev *dev,
493 + struct vhost_virtqueue *vq, bool touched)
494 +{
495 + vhost_memory_unmap(dev, &vq->used, vq->used_size, touched,
496 + touched ? vq->used_size : 0);
497 + vhost_memory_unmap(dev, &vq->avail, vq->avail_size, 0,
498 + touched ? vq->avail_size : 0);
499 + vhost_memory_unmap(dev, &vq->desc, vq->desc_size, 0,
500 + touched ? vq->desc_size : 0);
501 +}
502 +
503 +static int vhost_vrings_map(struct vhost_dev *dev,
504 + struct VirtIODevice *vdev,
505 + struct vhost_virtqueue *vq,
506 + unsigned idx)
507 +{
508 + vq->desc_size = virtio_queue_get_desc_size(vdev, idx);
509 + vq->desc_phys = virtio_queue_get_desc_addr(vdev, idx);
510 + vq->desc = NULL;
511 + vq->avail_size = virtio_queue_get_avail_size(vdev, idx);
512 + vq->avail_phys = virtio_queue_get_avail_addr(vdev, idx);
513 + vq->avail = NULL;
514 + vq->used_size = virtio_queue_get_used_size(vdev, idx);
515 + vq->used_phys = virtio_queue_get_used_addr(vdev, idx);
516 + vq->used = NULL;
517 +
518 + if (vq->desc_phys == 0) {
519 + /* Queue might not be ready for start */
520 + return 0;
521 + }
522 + vq->desc = vhost_memory_map(dev, vq->desc_phys, vq->desc_size, false);
523 + if (!vq->desc) {
524 + goto fail;
525 + }
526 + vq->avail = vhost_memory_map(dev, vq->avail_phys, vq->avail_size, false);
527 + if (!vq->avail) {
528 + goto fail;
529 + }
530 + vq->used = vhost_memory_map(dev, vq->used_phys, vq->used_size, true);
531 + if (!vq->used) {
532 + goto fail;
533 + }
534 +
535 + return 1;
536 +
537 +fail:
538 + vhost_vrings_unmap(dev, vq, false);
539 + return -ENOMEM;
540 +}
541 +
542 static int vhost_verify_ring_part_mapping(void *ring_hva,
543 uint64_t ring_gpa,
544 uint64_t ring_size,
@@ -1287,34 +1337,9 @@ int vhost_virtqueue_start(struct vhost_dev *dev,
1337 };
1338 struct VirtQueue *vvq = virtio_get_queue(vdev, idx);
1339
1290 - vq->desc_size = virtio_queue_get_desc_size(vdev, idx);
1291 - vq->desc_phys = virtio_queue_get_desc_addr(vdev, idx);
1292 - vq->desc = NULL;
1293 - vq->avail_size = virtio_queue_get_avail_size(vdev, idx);
1294 - vq->avail_phys = virtio_queue_get_avail_addr(vdev, idx);
1295 - vq->avail = NULL;
1296 - vq->used_size = virtio_queue_get_used_size(vdev, idx);
1297 - vq->used_phys = virtio_queue_get_used_addr(vdev, idx);
1298 - vq->used = NULL;
1299 -
1300 - if (vq->desc_phys == 0) {
1301 - /* Queue might not be ready for start */
1302 - return 0;
1303 - }
1304 - vq->desc = vhost_memory_map(dev, vq->desc_phys, vq->desc_size, false);
1305 - if (!vq->desc) {
1306 - r = -ENOMEM;
1307 - goto fail;
1308 - }
1309 - vq->avail = vhost_memory_map(dev, vq->avail_phys, vq->avail_size, false);
1310 - if (!vq->avail) {
1311 - r = -ENOMEM;
1312 - goto fail;
1313 - }
1314 - vq->used = vhost_memory_map(dev, vq->used_phys, vq->used_size, true);
1315 - if (!vq->used) {
1316 - r = -ENOMEM;
1317 - goto fail;
1340 + r = vhost_vrings_map(dev, vdev, vq, idx);
1341 + if (r <= 0) {
1342 + return r;
1343 }
1344
1345 vq->num = state.num = virtio_queue_get_num(vdev, idx);
@@ -1375,9 +1400,7 @@ int vhost_virtqueue_start(struct vhost_dev *dev,
1400 return 0;
1401
1402 fail:
1378 - vhost_memory_unmap(dev, &vq->used, vq->used_size, 0, 0);
1379 - vhost_memory_unmap(dev, &vq->avail, vq->avail_size, 0, 0);
1380 - vhost_memory_unmap(dev, &vq->desc, vq->desc_size, 0, 0);
1403 + vhost_vrings_unmap(dev, vq, false);
1404 return r;
1405 }
1406
@@ -1424,9 +1447,7 @@ static int do_vhost_virtqueue_stop(struct vhost_dev *dev,
1447 vhost_vq_index);
1448 }
1449
1427 - vhost_memory_unmap(dev, &vq->used, vq->used_size, 1, vq->used_size);
1428 - vhost_memory_unmap(dev, &vq->avail, vq->avail_size, 0, vq->avail_size);
1429 - vhost_memory_unmap(dev, &vq->desc, vq->desc_size, 0, vq->desc_size);
1450 + vhost_vrings_unmap(dev, vq, true);
1451 return r;
1452 }
1453