Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

iommu: Move lock from iommu_change_dev_def_domain() to its caller

The intention is to make it possible to put group ownership check and
default domain change in a same critical region protected by the group's
mutex lock. No intentional functional change.

Signed-off-by: Lu Baolu <baolu.lu@linux.intel.com>
Reviewed-by: Jason Gunthorpe <jgg@nvidia.com>
Link: https://lore.kernel.org/r/20230322064956.263419-5-baolu.lu@linux.intel.com
Signed-off-by: Joerg Roedel <jroedel@suse.de>

authored by

Lu Baolu and committed by
Joerg Roedel
33793748 dba9ca9d

+14 -15
+14 -15
drivers/iommu/iommu.c
··· 2889 2889 int ret, dev_def_dom; 2890 2890 struct device *dev; 2891 2891 2892 - mutex_lock(&group->mutex); 2892 + lockdep_assert_held(&group->mutex); 2893 2893 2894 2894 if (group->default_domain != group->domain) { 2895 2895 dev_err_ratelimited(prev_dev, "Group not assigned to default domain\n"); ··· 2978 2978 goto free_new_domain; 2979 2979 2980 2980 group->domain = group->default_domain; 2981 - 2982 - /* 2983 - * Release the mutex here because ops->probe_finalize() call-back of 2984 - * some vendor IOMMU drivers calls arm_iommu_attach_device() which 2985 - * in-turn might call back into IOMMU core code, where it tries to take 2986 - * group->mutex, resulting in a deadlock. 2987 - */ 2988 - mutex_unlock(&group->mutex); 2989 - 2990 - /* Make sure dma_ops is appropriatley set */ 2991 - iommu_group_do_probe_finalize(dev, group->default_domain); 2992 2981 iommu_domain_free(prev_dom); 2982 + 2993 2983 return 0; 2994 2984 2995 2985 free_new_domain: 2996 2986 iommu_domain_free(group->default_domain); 2997 2987 group->default_domain = prev_dom; 2998 2988 group->domain = prev_dom; 2999 - 3000 2989 out: 3001 - mutex_unlock(&group->mutex); 3002 - 3003 2990 return ret; 3004 2991 } 3005 2992 ··· 3076 3089 goto out; 3077 3090 } 3078 3091 3092 + mutex_lock(&group->mutex); 3079 3093 ret = iommu_change_dev_def_domain(group, dev, req_type); 3094 + /* 3095 + * Release the mutex here because ops->probe_finalize() call-back of 3096 + * some vendor IOMMU drivers calls arm_iommu_attach_device() which 3097 + * in-turn might call back into IOMMU core code, where it tries to take 3098 + * group->mutex, resulting in a deadlock. 3099 + */ 3100 + mutex_unlock(&group->mutex); 3101 + 3102 + /* Make sure dma_ops is appropriatley set */ 3103 + if (!ret) 3104 + iommu_group_do_probe_finalize(dev, group->default_domain); 3080 3105 ret = ret ?: count; 3081 3106 3082 3107 out: