@@ -129,6 +129,8 @@ static int iommu_create_device_direct_mappings(struct iommu_domain *domain,
129
129
struct device * dev );
130
130
static ssize_t iommu_group_store_type (struct iommu_group * group ,
131
131
const char * buf , size_t count );
132
+ static struct group_device * iommu_group_alloc_device (struct iommu_group * group ,
133
+ struct device * dev );
132
134
133
135
#define IOMMU_GROUP_ATTR (_name , _mode , _show , _store ) \
134
136
struct iommu_group_attribute iommu_group_attr_##_name = \
@@ -435,6 +437,7 @@ static int __iommu_probe_device(struct device *dev, struct list_head *group_list
435
437
const struct iommu_ops * ops = dev -> bus -> iommu_ops ;
436
438
struct iommu_group * group ;
437
439
static DEFINE_MUTEX (iommu_probe_device_lock );
440
+ struct group_device * gdev ;
438
441
int ret ;
439
442
440
443
if (!ops )
@@ -459,16 +462,17 @@ static int __iommu_probe_device(struct device *dev, struct list_head *group_list
459
462
goto out_unlock ;
460
463
461
464
group = dev -> iommu_group ;
462
- ret = iommu_group_add_device (group , dev );
465
+ gdev = iommu_group_alloc_device (group , dev );
463
466
mutex_lock (& group -> mutex );
464
- if (ret )
467
+ if (IS_ERR (gdev )) {
468
+ ret = PTR_ERR (gdev );
465
469
goto err_put_group ;
470
+ }
466
471
472
+ list_add_tail (& gdev -> list , & group -> devices );
467
473
if (group_list && !group -> default_domain && list_empty (& group -> entry ))
468
474
list_add_tail (& group -> entry , group_list );
469
475
mutex_unlock (& group -> mutex );
470
- iommu_group_put (group );
471
-
472
476
mutex_unlock (& iommu_probe_device_lock );
473
477
474
478
return 0 ;
@@ -578,7 +582,10 @@ static void __iommu_group_remove_device(struct device *dev)
578
582
}
579
583
mutex_unlock (& group -> mutex );
580
584
581
- /* Pairs with the get in iommu_group_add_device() */
585
+ /*
586
+ * Pairs with the get in iommu_init_device() or
587
+ * iommu_group_add_device()
588
+ */
582
589
iommu_group_put (group );
583
590
}
584
591
@@ -1067,22 +1074,16 @@ static int iommu_create_device_direct_mappings(struct iommu_domain *domain,
1067
1074
return ret ;
1068
1075
}
1069
1076
1070
- /**
1071
- * iommu_group_add_device - add a device to an iommu group
1072
- * @group: the group into which to add the device (reference should be held)
1073
- * @dev: the device
1074
- *
1075
- * This function is called by an iommu driver to add a device into a
1076
- * group. Adding a device increments the group reference count.
1077
- */
1078
- int iommu_group_add_device (struct iommu_group * group , struct device * dev )
1077
+ /* This is undone by __iommu_group_free_device() */
1078
+ static struct group_device * iommu_group_alloc_device (struct iommu_group * group ,
1079
+ struct device * dev )
1079
1080
{
1080
1081
int ret , i = 0 ;
1081
1082
struct group_device * device ;
1082
1083
1083
1084
device = kzalloc (sizeof (* device ), GFP_KERNEL );
1084
1085
if (!device )
1085
- return - ENOMEM ;
1086
+ return ERR_PTR ( - ENOMEM ) ;
1086
1087
1087
1088
device -> dev = dev ;
1088
1089
@@ -1113,17 +1114,11 @@ int iommu_group_add_device(struct iommu_group *group, struct device *dev)
1113
1114
goto err_free_name ;
1114
1115
}
1115
1116
1116
- iommu_group_ref_get (group );
1117
- dev -> iommu_group = group ;
1118
-
1119
- mutex_lock (& group -> mutex );
1120
- list_add_tail (& device -> list , & group -> devices );
1121
- mutex_unlock (& group -> mutex );
1122
1117
trace_add_device_to_group (group -> id , dev );
1123
1118
1124
1119
dev_info (dev , "Adding to iommu group %d\n" , group -> id );
1125
1120
1126
- return 0 ;
1121
+ return device ;
1127
1122
1128
1123
err_free_name :
1129
1124
kfree (device -> name );
@@ -1132,7 +1127,32 @@ int iommu_group_add_device(struct iommu_group *group, struct device *dev)
1132
1127
err_free_device :
1133
1128
kfree (device );
1134
1129
dev_err (dev , "Failed to add to iommu group %d: %d\n" , group -> id , ret );
1135
- return ret ;
1130
+ return ERR_PTR (ret );
1131
+ }
1132
+
1133
+ /**
1134
+ * iommu_group_add_device - add a device to an iommu group
1135
+ * @group: the group into which to add the device (reference should be held)
1136
+ * @dev: the device
1137
+ *
1138
+ * This function is called by an iommu driver to add a device into a
1139
+ * group. Adding a device increments the group reference count.
1140
+ */
1141
+ int iommu_group_add_device (struct iommu_group * group , struct device * dev )
1142
+ {
1143
+ struct group_device * gdev ;
1144
+
1145
+ gdev = iommu_group_alloc_device (group , dev );
1146
+ if (IS_ERR (gdev ))
1147
+ return PTR_ERR (gdev );
1148
+
1149
+ iommu_group_ref_get (group );
1150
+ dev -> iommu_group = group ;
1151
+
1152
+ mutex_lock (& group -> mutex );
1153
+ list_add_tail (& gdev -> list , & group -> devices );
1154
+ mutex_unlock (& group -> mutex );
1155
+ return 0 ;
1136
1156
}
1137
1157
EXPORT_SYMBOL_GPL (iommu_group_add_device );
1138
1158
0 commit comments