Re: [PATCH RFC 12/15] hw/virtio/vhost-user: send isolation regions to device
Hanna Czenczek <[email protected]> Mon, 3 Aug 2026 15:46:21 +0200
| Newsgroups | dev.linux.lists.virtio-fs,org.nongnu.qemu-devel |
|---|---|
| Message-ID | <[email protected]> |
On 24.07.26 00:30, Connor Kite wrote: > Adds features to fill a vhost_user_set_mem_table message with the > addresses of isolation memory regions corresponding to bounce buffers > and vrings. > > Signed-off-by: Connor Kite <[email protected]> > --- > hw/virtio/vhost-user.c | 80 +++++++++++++++++++++++++++++++++++++++++++++++++- > 1 file changed, 79 insertions(+), 1 deletion(-) > > diff --git a/hw/virtio/vhost-user.c b/hw/virtio/vhost-user.c > index 710cf966f8..acabfb7f1c 100644 > --- a/hw/virtio/vhost-user.c > +++ b/hw/virtio/vhost-user.c > @@ -625,6 +625,21 @@ static void vhost_user_fill_msg_region(struct vhost_dev *dev, > dst->mmap_offset = mmap_offset; > } > > +static void vhost_user_fill_msg_region_iso(VhostUserMemoryRegion *dst, > + const struct vhost_user *u, > + const struct vhost_memory_region > + *iova_reg) > +{ > + assert(u != NULL && dst != NULL && iova_reg != NULL); > + uint64_t offset; > + > + offset = iova_reg->userspace_addr - u->iso_memory.base_addr; > + dst->userspace_addr = iova_reg->userspace_addr; > + dst->memory_size = iova_reg->memory_size; > + dst->guest_phys_addr = iova_reg->userspace_addr; > + dst->mmap_offset = offset; > +} > + > static int vhost_user_fill_set_mem_table_msg(struct vhost_user *u, > struct vhost_dev *dev, > VhostUserMsg *msg, > @@ -1136,7 +1151,33 @@ static void cleanup_isolation_regions(struct vhost_dev *dev) > } > } > > -__attribute__((unused)) > +struct iova_tree_traversal_args { > + VhostUserMsg *msg; > + struct vhost_user *u; > + int *fds; > + size_t *fd_num; > +}; > + > +static gboolean vhost_user_iova_tree_traverse_funct(gpointer key, > + gpointer value, > + gpointer data) This function name doesn’t really say what the function does, and more describes its type. > +{ > + struct iova_tree_traversal_args *args = data; > + struct vhost_memory_region msg_region; > + VhostUserMemoryRegion region_buffer; > + DMAMap *map = key; > + args->fds[*args->fd_num] = args->u->iso_memory.iso_fd; Generally: I find it quite hard to read code where general statements come after a list of declarations without an empty line, especially if there is an empty line later. Visually, this last line to me looks like another variable declaration. > + > + msg_region.guest_phys_addr = map->iova; > + msg_region.memory_size = map->size + 1; > + msg_region.userspace_addr = map->iova; > + vhost_user_fill_msg_region_iso(®ion_buffer, args->u, &msg_region); > + args->msg->payload.memory.regions[*args->fd_num] = region_buffer; Why does `vhost_user_fill_msg_region_iso()` exist when this is the only caller, and we could just fill `regions[i]` here directly? > + (*args->fd_num)++; As noted by Akihiko, there needs to be some bounds checking. At least against `VHOST_MEMORY_BASELINE_NREGIONS`, but even better against the array size of `args->msg->payload.memory.regions` and the caller should pass the size of `fds[]` via `args`. (And I’m also with Stefan, something with `index` would be better. Maybe `region_idx`.) > + > + return false; > +} > + > static int init_isolation_regions(struct vhost_dev *dev, > VhostUserMsg *msg, > int *fds, size_t *fd_num) > @@ -1234,6 +1275,24 @@ static int init_isolation_regions(struct vhost_dev *dev, > map->translated_addr); > } > > + struct iova_tree_traversal_args args = { > + .fd_num = fd_num, > + .fds = fds, > + .msg = msg, > + .u = u > + }; Personal preference: I would like `*fd_num = 0` somewhere here, or `assert(*fd_num == 0)` before the iteration. Yes, the caller initializes it to zero, but it is just not obvious here, and it seems necessary for correct semantics. (And the open question whether `fd_num` should be renamed here, too, or can keep that name. Not sure.) > + > + vhost_iova_tree_foreach(u->iso_iova_tree, > + vhost_user_iova_tree_traverse_funct, &args); > + > + msg->payload.memory.nregions = *fd_num; > + > + assert(*fd_num != 0); Am I wrong or should `*fd_num == nregions`? Is it possible that `*fd_num` is less because regions end up joined? Can you add a comment on when they would differ? Hanna > + > + msg->hdr.size = sizeof(msg->payload.memory.nregions); > + msg->hdr.size += sizeof(msg->payload.memory.padding); > + msg->hdr.size += *fd_num * sizeof(VhostUserMemoryRegion); > + > return 0; > } [...]