@@ -918,6 +918,8 @@ static void __vfio_group_unset_container(struct vfio_group *group)
918
918
struct vfio_container * container = group -> container ;
919
919
struct vfio_iommu_driver * driver ;
920
920
921
+ lockdep_assert_held_write (& group -> group_rwsem );
922
+
921
923
down_write (& container -> group_lock );
922
924
923
925
driver = container -> iommu_driver ;
@@ -953,6 +955,8 @@ static int vfio_group_unset_container(struct vfio_group *group)
953
955
{
954
956
int users = atomic_cmpxchg (& group -> container_users , 1 , 0 );
955
957
958
+ lockdep_assert_held_write (& group -> group_rwsem );
959
+
956
960
if (!users )
957
961
return - EINVAL ;
958
962
if (users != 1 )
@@ -971,8 +975,10 @@ static int vfio_group_unset_container(struct vfio_group *group)
971
975
*/
972
976
static void vfio_group_try_dissolve_container (struct vfio_group * group )
973
977
{
978
+ down_write (& group -> group_rwsem );
974
979
if (0 == atomic_dec_if_positive (& group -> container_users ))
975
980
__vfio_group_unset_container (group );
981
+ up_write (& group -> group_rwsem );
976
982
}
977
983
978
984
static int vfio_group_set_container (struct vfio_group * group , int container_fd )
@@ -982,6 +988,8 @@ static int vfio_group_set_container(struct vfio_group *group, int container_fd)
982
988
struct vfio_iommu_driver * driver ;
983
989
int ret = 0 ;
984
990
991
+ lockdep_assert_held_write (& group -> group_rwsem );
992
+
985
993
if (atomic_read (& group -> container_users ))
986
994
return - EINVAL ;
987
995
@@ -1039,23 +1047,6 @@ static int vfio_group_set_container(struct vfio_group *group, int container_fd)
1039
1047
return ret ;
1040
1048
}
1041
1049
1042
- static int vfio_group_add_container_user (struct vfio_group * group )
1043
- {
1044
- if (!atomic_inc_not_zero (& group -> container_users ))
1045
- return - EINVAL ;
1046
-
1047
- if (group -> type == VFIO_NO_IOMMU ) {
1048
- atomic_dec (& group -> container_users );
1049
- return - EPERM ;
1050
- }
1051
- if (!group -> container -> iommu_driver ) {
1052
- atomic_dec (& group -> container_users );
1053
- return - EINVAL ;
1054
- }
1055
-
1056
- return 0 ;
1057
- }
1058
-
1059
1050
static const struct file_operations vfio_device_fops ;
1060
1051
1061
1052
/* true if the vfio_device has open_device() called but not close_device() */
@@ -1068,6 +1059,8 @@ static int vfio_device_assign_container(struct vfio_device *device)
1068
1059
{
1069
1060
struct vfio_group * group = device -> group ;
1070
1061
1062
+ lockdep_assert_held_write (& group -> group_rwsem );
1063
+
1071
1064
if (0 == atomic_read (& group -> container_users ) ||
1072
1065
!group -> container -> iommu_driver )
1073
1066
return - EINVAL ;
@@ -1084,7 +1077,9 @@ static struct file *vfio_device_open(struct vfio_device *device)
1084
1077
struct file * filep ;
1085
1078
int ret ;
1086
1079
1080
+ down_write (& device -> group -> group_rwsem );
1087
1081
ret = vfio_device_assign_container (device );
1082
+ up_write (& device -> group -> group_rwsem );
1088
1083
if (ret )
1089
1084
return ERR_PTR (ret );
1090
1085
@@ -1197,11 +1192,13 @@ static long vfio_group_fops_unl_ioctl(struct file *filep,
1197
1192
1198
1193
status .flags = 0 ;
1199
1194
1195
+ down_read (& group -> group_rwsem );
1200
1196
if (group -> container )
1201
1197
status .flags |= VFIO_GROUP_FLAGS_CONTAINER_SET |
1202
1198
VFIO_GROUP_FLAGS_VIABLE ;
1203
1199
else if (!iommu_group_dma_owner_claimed (group -> iommu_group ))
1204
1200
status .flags |= VFIO_GROUP_FLAGS_VIABLE ;
1201
+ up_read (& group -> group_rwsem );
1205
1202
1206
1203
if (copy_to_user ((void __user * )arg , & status , minsz ))
1207
1204
return - EFAULT ;
@@ -1219,11 +1216,15 @@ static long vfio_group_fops_unl_ioctl(struct file *filep,
1219
1216
if (fd < 0 )
1220
1217
return - EINVAL ;
1221
1218
1219
+ down_write (& group -> group_rwsem );
1222
1220
ret = vfio_group_set_container (group , fd );
1221
+ up_write (& group -> group_rwsem );
1223
1222
break ;
1224
1223
}
1225
1224
case VFIO_GROUP_UNSET_CONTAINER :
1225
+ down_write (& group -> group_rwsem );
1226
1226
ret = vfio_group_unset_container (group );
1227
+ up_write (& group -> group_rwsem );
1227
1228
break ;
1228
1229
case VFIO_GROUP_GET_DEVICE_FD :
1229
1230
{
@@ -1709,15 +1710,19 @@ bool vfio_file_enforced_coherent(struct file *file)
1709
1710
if (file -> f_op != & vfio_group_fops )
1710
1711
return true;
1711
1712
1712
- /*
1713
- * Since the coherency state is determined only once a container is
1714
- * attached the user must do so before they can prove they have
1715
- * permission.
1716
- */
1717
- if (vfio_group_add_container_user (group ))
1718
- return true;
1719
- ret = vfio_ioctl_check_extension (group -> container , VFIO_DMA_CC_IOMMU );
1720
- vfio_group_try_dissolve_container (group );
1713
+ down_read (& group -> group_rwsem );
1714
+ if (group -> container ) {
1715
+ ret = vfio_ioctl_check_extension (group -> container ,
1716
+ VFIO_DMA_CC_IOMMU );
1717
+ } else {
1718
+ /*
1719
+ * Since the coherency state is determined only once a container
1720
+ * is attached the user must do so before they can prove they
1721
+ * have permission.
1722
+ */
1723
+ ret = true;
1724
+ }
1725
+ up_read (& group -> group_rwsem );
1721
1726
return ret ;
1722
1727
}
1723
1728
EXPORT_SYMBOL_GPL (vfio_file_enforced_coherent );
@@ -1910,6 +1915,7 @@ int vfio_pin_pages(struct vfio_device *device, unsigned long *user_pfn,
1910
1915
if (group -> dev_counter > 1 )
1911
1916
return - EINVAL ;
1912
1917
1918
+ /* group->container cannot change while a vfio device is open */
1913
1919
container = group -> container ;
1914
1920
driver = container -> iommu_driver ;
1915
1921
if (likely (driver && driver -> ops -> pin_pages ))
@@ -1945,6 +1951,7 @@ int vfio_unpin_pages(struct vfio_device *device, unsigned long *user_pfn,
1945
1951
if (npage > VFIO_PIN_PAGES_MAX_ENTRIES )
1946
1952
return - E2BIG ;
1947
1953
1954
+ /* group->container cannot change while a vfio device is open */
1948
1955
container = device -> group -> container ;
1949
1956
driver = container -> iommu_driver ;
1950
1957
if (likely (driver && driver -> ops -> unpin_pages ))
@@ -1984,6 +1991,7 @@ int vfio_dma_rw(struct vfio_device *device, dma_addr_t user_iova, void *data,
1984
1991
if (!data || len <= 0 || !vfio_assert_device_open (device ))
1985
1992
return - EINVAL ;
1986
1993
1994
+ /* group->container cannot change while a vfio device is open */
1987
1995
container = device -> group -> container ;
1988
1996
driver = container -> iommu_driver ;
1989
1997
@@ -2004,13 +2012,16 @@ static int vfio_register_iommu_notifier(struct vfio_group *group,
2004
2012
struct vfio_iommu_driver * driver ;
2005
2013
int ret ;
2006
2014
2015
+ down_read (& group -> group_rwsem );
2007
2016
container = group -> container ;
2008
2017
driver = container -> iommu_driver ;
2009
2018
if (likely (driver && driver -> ops -> register_notifier ))
2010
2019
ret = driver -> ops -> register_notifier (container -> iommu_data ,
2011
2020
events , nb );
2012
2021
else
2013
2022
ret = - ENOTTY ;
2023
+ up_read (& group -> group_rwsem );
2024
+
2014
2025
return ret ;
2015
2026
}
2016
2027
@@ -2021,13 +2032,16 @@ static int vfio_unregister_iommu_notifier(struct vfio_group *group,
2021
2032
struct vfio_iommu_driver * driver ;
2022
2033
int ret ;
2023
2034
2035
+ down_read (& group -> group_rwsem );
2024
2036
container = group -> container ;
2025
2037
driver = container -> iommu_driver ;
2026
2038
if (likely (driver && driver -> ops -> unregister_notifier ))
2027
2039
ret = driver -> ops -> unregister_notifier (container -> iommu_data ,
2028
2040
nb );
2029
2041
else
2030
2042
ret = - ENOTTY ;
2043
+ up_read (& group -> group_rwsem );
2044
+
2031
2045
return ret ;
2032
2046
}
2033
2047
0 commit comments