> From: Christoph Hellwig <hch@xxxxxx> > Sent: Thursday, August 26, 2021 12:19 AM > [...] > @@ -2163,45 +2164,27 @@ static int vfio_iommu_type1_attach_group(void > *iommu_data, > struct vfio_iommu_group *group; > struct vfio_domain *domain, *d; > struct bus_type *bus = NULL; > - int ret; > bool resv_msi, msi_remap; > phys_addr_t resv_msi_base = 0; > struct iommu_domain_geometry *geo; > LIST_HEAD(iova_copy); > LIST_HEAD(group_resv_regions); > + int ret = -EINVAL; > > mutex_lock(&iommu->lock); > > /* Check for duplicates */ > - if (vfio_iommu_find_iommu_group(iommu, iommu_group)) { > - mutex_unlock(&iommu->lock); > - return -EINVAL; > - } > + if (vfio_iommu_find_iommu_group(iommu, iommu_group)) > + goto out_unlock; > > + ret = -ENOMEM; > group = kzalloc(sizeof(*group), GFP_KERNEL); > - domain = kzalloc(sizeof(*domain), GFP_KERNEL); > - if (!group || !domain) { > - ret = -ENOMEM; > - goto out_free; > - } > - > + if (!group) > + goto out_unlock; > group->iommu_group = iommu_group; > > - /* Determine bus_type in order to allocate a domain */ > - ret = iommu_group_for_each_dev(iommu_group, &bus, > vfio_bus_type); > - if (ret) > - goto out_free; > - > if (flags & VFIO_EMULATED_IOMMU) { > - if (!iommu->external_domain) { > - INIT_LIST_HEAD(&domain->group_list); > - iommu->external_domain = domain; > - vfio_update_pgsize_bitmap(iommu); > - } else { > - kfree(domain); > - } > - > - list_add(&group->next, &iommu->external_domain- > >group_list); > + list_add(&group->next, &iommu->emulated_iommu_groups); > /* > * Non-iommu backed group cannot dirty memory directly, it > can unify the naming e.g. "group with emulated iommu cannot dirty ..." > * only use interfaces that provide dirty tracking. > @@ -2209,16 +2192,24 @@ static int vfio_iommu_type1_attach_group(void > *iommu_data, > * dirty tracking group. > */ > group->pinned_page_dirty_scope = true; > - mutex_unlock(&iommu->lock); > - > - return 0; > + ret = 0; > + goto out_unlock; > } > > + /* Determine bus_type in order to allocate a domain */ > + ret = iommu_group_for_each_dev(iommu_group, &bus, > vfio_bus_type); > + if (ret) > + goto out_free_group; > + > + ret = -ENOMEM; > + domain = kzalloc(sizeof(*domain), GFP_KERNEL); > + if (!domain) > + goto out_free_group; > + > + ret = -EIO; > domain->domain = iommu_domain_alloc(bus); > - if (!domain->domain) { > - ret = -EIO; > - goto out_free; > - } -ENOMEM? > + if (!domain->domain) > + goto out_free_domain; > > if (iommu->nesting) { > ret = iommu_enable_nesting(domain->domain); looks your change of errno handling is incomplete. there are several other places down this function which set the errno right before goto. better to have a consistent style in one function. 😊 Thanks Kevin