@@ -2956,14 +2956,6 @@ static int iommu_change_dev_def_domain(struct iommu_group *group,
29562956 goto out ;
29572957 }
29582958
2959- /* We can bring up a flush queue without tearing down the domain */
2960- if (type == IOMMU_DOMAIN_DMA_FQ && prev_dom -> type == IOMMU_DOMAIN_DMA ) {
2961- ret = iommu_dma_init_fq (prev_dom );
2962- if (!ret )
2963- prev_dom -> type = IOMMU_DOMAIN_DMA_FQ ;
2964- goto out ;
2965- }
2966-
29672959 /* Sets group->default_domain to the newly allocated domain */
29682960 ret = iommu_group_alloc_default_domain (dev -> bus , group , type );
29692961 if (ret )
@@ -2996,7 +2988,7 @@ static int iommu_change_dev_def_domain(struct iommu_group *group,
29962988 * transition. Return failure if this isn't met.
29972989 *
29982990 * We need to consider the race between this and the device release path.
2999- * device_lock(dev) is used here to guarantee that the device release path
2991+ * group->mutex is used here to guarantee that the device release path
30002992 * will not be entered at the same time.
30012993 */
30022994static ssize_t iommu_group_store_type (struct iommu_group * group ,
@@ -3023,61 +3015,29 @@ static ssize_t iommu_group_store_type(struct iommu_group *group,
30233015 else
30243016 return - EINVAL ;
30253017
3026- /*
3027- * Lock/Unlock the group mutex here before device lock to
3028- * 1. Make sure that the iommu group has only one device (this is a
3029- * prerequisite for step 2)
3030- * 2. Get struct *dev which is needed to lock device
3031- */
30323018 mutex_lock (& group -> mutex );
3033- if (iommu_group_device_count (group ) != 1 ) {
3019+ /* We can bring up a flush queue without tearing down the domain. */
3020+ if (req_type == IOMMU_DOMAIN_DMA_FQ &&
3021+ group -> default_domain -> type == IOMMU_DOMAIN_DMA ) {
3022+ ret = iommu_dma_init_fq (group -> default_domain );
3023+ if (!ret )
3024+ group -> default_domain -> type = IOMMU_DOMAIN_DMA_FQ ;
30343025 mutex_unlock (& group -> mutex );
3035- pr_err_ratelimited ("Cannot change default domain: Group has more than one device\n" );
3036- return - EINVAL ;
3026+
3027+ return ret ?: count ;
3028+ }
3029+
3030+ /* Otherwise, ensure that device exists and no driver is bound. */
3031+ if (list_empty (& group -> devices ) || group -> owner_cnt ) {
3032+ mutex_unlock (& group -> mutex );
3033+ return - EPERM ;
30373034 }
30383035
3039- /* Since group has only one device */
30403036 grp_dev = list_first_entry (& group -> devices , struct group_device , list );
30413037 dev = grp_dev -> dev ;
3042- get_device (dev );
30433038
3044- /*
3045- * Don't hold the group mutex because taking group mutex first and then
3046- * the device lock could potentially cause a deadlock as below. Assume
3047- * two threads T1 and T2. T1 is trying to change default domain of an
3048- * iommu group and T2 is trying to hot unplug a device or release [1] VF
3049- * of a PCIe device which is in the same iommu group. T1 takes group
3050- * mutex and before it could take device lock assume T2 has taken device
3051- * lock and is yet to take group mutex. Now, both the threads will be
3052- * waiting for the other thread to release lock. Below, lock order was
3053- * suggested.
3054- * device_lock(dev);
3055- * mutex_lock(&group->mutex);
3056- * iommu_change_dev_def_domain();
3057- * mutex_unlock(&group->mutex);
3058- * device_unlock(dev);
3059- *
3060- * [1] Typical device release path
3061- * device_lock() from device/driver core code
3062- * -> bus_notifier()
3063- * -> iommu_bus_notifier()
3064- * -> iommu_release_device()
3065- * -> ops->release_device() vendor driver calls back iommu core code
3066- * -> mutex_lock() from iommu core code
3067- */
3068- mutex_unlock (& group -> mutex );
3069-
3070- /* Check if the device in the group still has a driver bound to it */
3071- device_lock (dev );
3072- if (device_is_bound (dev ) && !(req_type == IOMMU_DOMAIN_DMA_FQ &&
3073- group -> default_domain -> type == IOMMU_DOMAIN_DMA )) {
3074- pr_err_ratelimited ("Device is still bound to driver\n" );
3075- ret = - EBUSY ;
3076- goto out ;
3077- }
3078-
3079- mutex_lock (& group -> mutex );
30803039 ret = iommu_change_dev_def_domain (group , dev , req_type );
3040+
30813041 /*
30823042 * Release the mutex here because ops->probe_finalize() call-back of
30833043 * some vendor IOMMU drivers calls arm_iommu_attach_device() which
@@ -3088,14 +3048,9 @@ static ssize_t iommu_group_store_type(struct iommu_group *group,
30883048
30893049 /* Make sure dma_ops is appropriatley set */
30903050 if (!ret )
3091- iommu_group_do_probe_finalize (dev , group -> default_domain );
3092- ret = ret ?: count ;
3093-
3094- out :
3095- device_unlock (dev );
3096- put_device (dev );
3051+ __iommu_group_dma_finalize (group );
30973052
3098- return ret ;
3053+ return ret ?: count ;
30993054}
31003055
31013056static bool iommu_is_default_domain (struct iommu_group * group )
0 commit comments