hw/virtio/vhost: simplify ring mapping verification with loop
PATCH 1/1 hw/virtio/vhost: simplify ring mapping verification with loop
Replace the three nearly-identical vhost_verify_ring_part_mapping() calls with a simple loop over arrays. This eliminates code duplication while maintaining identical behavior and error reporting.
Signed-off-by Bin Guo <[email protected]>
--- hw/virtio/vhost.c | 34 ++++++++++++---------------------- 1 file changed, 12 insertions(+), 22 deletions(-)
hw/virtio/vhost.c +12 -22
@@ -525,31 +525,21 @@ static int vhost_verify_ring_mappings(struct vhost_dev *dev,continue ; } - j =0 ; - r =vhost_verify_ring_part_mapping ( - vq->desc, vq->desc_phys, vq->desc_size, - reg_hva, reg_gpa, reg_size); -if (r) { -break ; - } - - j++; - r =vhost_verify_ring_part_mapping ( - vq->avail, vq->avail_phys, vq->avail_size, - reg_hva, reg_gpa, reg_size); -if (r) { -break ; - } - - j++; - r =vhost_verify_ring_part_mapping ( - vq->used, vq->used_phys, vq->used_size, - reg_hva, reg_gpa, reg_size); -if (r) { -break ; +void *ring_addrs[] = {vq->desc, vq->avail, vq->used}; +uint64_t ring_phys[] = {vq->desc_phys, vq->avail_phys, vq->used_phys}; +uint64_t ring_sizes[] = {vq->desc_size, vq->avail_size, vq->used_size}; + +for (j =0 ; j <3 ; j++) { + r =vhost_verify_ring_part_mapping ( + ring_addrs[j], ring_phys[j], ring_sizes[j], + reg_hva, reg_gpa, reg_size); +if (r) { + goto out; + } } } +out:if (r == -ENOMEM ) {error_report ("Unable to map %s for ring %d" , part_name[j], i); }else if (r == -EBUSY ) {
Signature · 1 line
2.50.1 (Apple Git-155)
Quoted 46 lines · Bin Guo
Replace the three nearly-identical
vhost_verify_ring_part_mapping()calls with a simple loop over arrays. This eliminates code duplication while maintaining identical behavior and error reporting.Signed-off-by Bin Guo <[email protected]>--- hw/virtio/vhost.c | 34 ++++++++++++---------------------- 1 file changed, 12 insertions(+), 22 deletions(-)hw/virtio/vhost.c +7
-22@@ -525,31 +525,21 @@ static int vhost_verify_ring_mappings(struct vhost_dev *dev,continue ; } - j =0 ; - r =vhost_verify_ring_part_mapping ( - vq->desc, vq->desc_phys, vq->desc_size, - reg_hva, reg_gpa, reg_size); -if (r) { -break ; - } - - j++; - r =vhost_verify_ring_part_mapping ( - vq->avail, vq->avail_phys, vq->avail_size, - reg_hva, reg_gpa, reg_size); -if (r) { -break ; - } - - j++; - r =vhost_verify_ring_part_mapping ( - vq->used, vq->used_phys, vq->used_size, - reg_hva, reg_gpa, reg_size); -if (r) { -break ; +void *ring_addrs[] = {vq->desc, vq->avail, vq->used}; +uint64_t ring_phys[] = {vq->desc_phys, vq->avail_phys, vq->used_phys}; +uint64_t ring_sizes[] = {vq->desc_size, vq->avail_size, vq->used_size}; + +for (j =0 ; j <3 ; j++) { + r = vhost_verify_ring_part_mapping( + ring_addrs[j], ring_phys[j], ring_sizes[j],
hw/virtio/vhost.c static int vhost_verify_ring_mappings(struct vhost_dev *dev, · line 544
Quoted 10 lines
hw/virtio/vhost.c +5
-0+ reg_hva, reg_gpa, reg_size); +if (r) { + goto out; + } } } +out:if (r == -ENOMEM ) {error_report ("Unable to map %s for ring %d" , part_name[j], i);
I don't see the point, sorry. Maybe if it was a single array, and we used ARRAY_SIZE ... Even then, I am not sure.
As it is, we are replacing straight forward linear code with tricky arrays.
Quoted 3 lines
hw/virtio/vhost.c +0
-0}else if (r == -EBUSY ) {Signature · 1 line
2.50.1 (Apple Git-155)