vfio/container: reform vfio_container_connect cleanup

Replace the proliferation of exit labels in vfio_container_connect with
conditionals for cleaning each piece of state.  No functional change.

Signed-off-by: Steve Sistare <steven.sistare@oracle.com>
Reviewed-by: Cedric Le Goater <clg@redhat.com>
Link: https://lore.kernel.org/qemu-devel/1746195760-101443-3-git-send-email-steven.sistare@oracle.com
[ clg: vfio_attach_discard_disable() -> vfio_container_attach_discard_disable() ]
Signed-off-by: Cédric Le Goater <clg@redhat.com>
This commit is contained in:
Steve Sistare 2025-05-02 07:22:39 -07:00 committed by Cédric Le Goater
parent 2cc3643df5
commit a1f267a7d4

View file

@ -563,9 +563,12 @@ static bool vfio_container_connect(VFIOGroup *group, AddressSpace *as,
{ {
VFIOContainer *container; VFIOContainer *container;
VFIOContainerBase *bcontainer; VFIOContainerBase *bcontainer;
int ret, fd; int ret, fd = -1;
VFIOAddressSpace *space; VFIOAddressSpace *space;
VFIOIOMMUClass *vioc; VFIOIOMMUClass *vioc = NULL;
bool new_container = false;
bool group_was_added = false;
bool discard_disabled = false;
space = vfio_address_space_get(as); space = vfio_address_space_get(as);
@ -584,35 +587,37 @@ static bool vfio_container_connect(VFIOGroup *group, AddressSpace *as,
fd = qemu_open("/dev/vfio/vfio", O_RDWR, errp); fd = qemu_open("/dev/vfio/vfio", O_RDWR, errp);
if (fd < 0) { if (fd < 0) {
goto put_space_exit; goto fail;
} }
ret = ioctl(fd, VFIO_GET_API_VERSION); ret = ioctl(fd, VFIO_GET_API_VERSION);
if (ret != VFIO_API_VERSION) { if (ret != VFIO_API_VERSION) {
error_setg(errp, "supported vfio version: %d, " error_setg(errp, "supported vfio version: %d, "
"reported version: %d", VFIO_API_VERSION, ret); "reported version: %d", VFIO_API_VERSION, ret);
goto close_fd_exit; goto fail;
} }
container = vfio_create_container(fd, group, errp); container = vfio_create_container(fd, group, errp);
if (!container) { if (!container) {
goto close_fd_exit; goto fail;
} }
new_container = true;
bcontainer = &container->bcontainer; bcontainer = &container->bcontainer;
if (!vfio_cpr_register_container(bcontainer, errp)) { if (!vfio_cpr_register_container(bcontainer, errp)) {
goto free_container_exit; goto fail;
} }
if (!vfio_container_attach_discard_disable(container, group, errp)) { if (!vfio_container_attach_discard_disable(container, group, errp)) {
goto unregister_container_exit; goto fail;
} }
discard_disabled = true;
vioc = VFIO_IOMMU_GET_CLASS(bcontainer); vioc = VFIO_IOMMU_GET_CLASS(bcontainer);
assert(vioc->setup); assert(vioc->setup);
if (!vioc->setup(bcontainer, errp)) { if (!vioc->setup(bcontainer, errp)) {
goto enable_discards_exit; goto fail;
} }
vfio_group_add_kvm_device(group); vfio_group_add_kvm_device(group);
@ -621,35 +626,36 @@ static bool vfio_container_connect(VFIOGroup *group, AddressSpace *as,
group->container = container; group->container = container;
QLIST_INSERT_HEAD(&container->group_list, group, container_next); QLIST_INSERT_HEAD(&container->group_list, group, container_next);
group_was_added = true;
if (!vfio_listener_register(bcontainer, errp)) { if (!vfio_listener_register(bcontainer, errp)) {
goto listener_release_exit; goto fail;
} }
bcontainer->initialized = true; bcontainer->initialized = true;
return true; return true;
listener_release_exit:
fail:
vfio_listener_unregister(bcontainer);
if (group_was_added) {
QLIST_REMOVE(group, container_next); QLIST_REMOVE(group, container_next);
vfio_group_del_kvm_device(group); vfio_group_del_kvm_device(group);
vfio_listener_unregister(bcontainer); }
if (vioc->release) { if (vioc && vioc->release) {
vioc->release(bcontainer); vioc->release(bcontainer);
} }
if (discard_disabled) {
enable_discards_exit:
vfio_ram_block_discard_disable(container, false); vfio_ram_block_discard_disable(container, false);
}
unregister_container_exit: if (new_container) {
vfio_cpr_unregister_container(bcontainer); vfio_cpr_unregister_container(bcontainer);
free_container_exit:
object_unref(container); object_unref(container);
}
close_fd_exit: if (fd >= 0) {
close(fd); close(fd);
}
put_space_exit:
vfio_address_space_put(space); vfio_address_space_put(space);
return false; return false;