iommu/dart: Clear sid2group entry when a group is freed
Subsystems (2)
PATCH 1/1 iommu/dart: Clear sid2group entry when a group is freed
sid2groups keeps track of which stream id combinations belong to a iommu_group to assign those correctly to devices.
When a iommu_group is freed a stale pointer will however remain in sid2groups. This prevents devices with the same stream id combination to ever be attached again (see below).
Fix that by creating a shadow copy of the stream id configuration when a group is allocated for the first time and clear the sid2group entry when that group is freed.
# echo 1 >/sys/bus/pci/devices/0000\:03\:00.0/remove pci 0000:03:00.0: Removing from iommu group 1 # echo 1 >/sys/bus/pci/rescan [...] pci 0000:03:00.0: BAR 0: assigned [mem 0x6a0000000-0x6a000ffff 64bit pref] pci 0000:03:00.0: BAR 2: assigned [mem 0x6a0010000-0x6a001ffff 64bit pref] pci 0000:03:00.0: BAR 6: assigned [mem 0x6c0100000-0x6c01007ff pref] tg3 0000:03:00.0: Failed to add to iommu group 1: -2 [...]
--- drivers/iommu/apple-dart.c | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-)
drivers/iommu/apple-dart.c +35 -3
@@ -636,16 +636,34 @@ static int apple_dart_of_xlate(struct device *dev, struct of_phandle_args *args)return -EINVAL ; } +static DEFINE_MUTEX (apple_dart_groups_lock); + +static void apple_dart_release_group (void *iommu_data) +{ +int i, sid; +struct apple_dart_stream_map *stream_map; +struct apple_dart_master_cfg *group_master_cfg = iommu_data; + +mutex_lock (&apple_dart_groups_lock); + +for_each_stream_map (i, group_master_cfg, stream_map) +for_each_set_bit (sid, &stream_map->sidmap,DART_MAX_STREAMS ) + stream_map->dart->sid2group[sid] =NULL ; + +kfree (iommu_data); +mutex_unlock (&apple_dart_groups_lock); +} +static struct iommu_group *apple_dart_device_group (struct device *dev) { -static DEFINE_MUTEX (lock);int i, sid;struct apple_dart_master_cfg *cfg =dev_iommu_priv_get (dev);struct apple_dart_stream_map *stream_map; +struct apple_dart_master_cfg *group_master_cfg;struct iommu_group *group =NULL ;struct iommu_group *res =ERR_PTR (-EINVAL ); -mutex_lock (&lock); +mutex_lock (&apple_dart_groups_lock);for_each_stream_map (i, cfg, stream_map) {for_each_set_bit (sid, &stream_map->sidmap,DART_MAX_STREAMS ) {
@@ -673,6 +691,20 @@ static struct iommu_group *apple_dart_device_group(struct device *dev)#endif group =generic_device_group (dev); + res =ERR_PTR (-ENOMEM ); +if (!group) + goto out; + + group_master_cfg =kzalloc (sizeof (*group_master_cfg),GFP_KERNEL ); +if (!group_master_cfg) { +iommu_group_put (group); + goto out; + } + +memcpy (group_master_cfg, cfg,sizeof (*group_master_cfg)); +iommu_group_set_iommudata (group, group_master_cfg, + apple_dart_release_group); +for_each_stream_map (i, cfg, stream_map)for_each_set_bit (sid, &stream_map->sidmap,DART_MAX_STREAMS ) stream_map->dart->sid2group[sid] = group;
@@ -680,7 +712,7 @@ static struct iommu_group *apple_dart_device_group(struct device *dev) res = group; out: -mutex_unlock (&lock); +mutex_unlock (&apple_dart_groups_lock);return res; }
Signature · 1 line
2.25.1
Quoted 20 lines · Sven Peter
sid2groups keeps track of which stream id combinations belong to a
iommu_groupto assign those correctly to devices.
When aiommu_groupis freed a stale pointer will however remain in sid2groups. This prevents devices with the same stream id combination to ever be attached again (see below).
Fix that by creating a shadow copy of the stream id configuration when a group is allocated for the first time and clear the sid2group entry when that group is freed.# echo 1 >/sys/bus/pci/devices/0000\:03\:00.0/remove pci 0000:03:00.0: Removing from iommu group 1 # echo 1 >/sys/bus/pci/rescan [...] pci 0000:03:00.0: BAR 0: assigned [mem 0x6a0000000-0x6a000ffff 64bit pref] pci 0000:03:00.0: BAR 2: assigned [mem 0x6a0010000-0x6a001ffff 64bit pref] pci 0000:03:00.0: BAR 6: assigned [mem 0x6c0100000-0x6c01007ff pref] tg3 0000:03:00.0: Failed to add to iommu group 1: -2 [...]
Quoted 3 lines
Fixes 46d1fb072e76b161 ("iommu/dart: Add DART iommu driver")Reported-by Marc Zyngier <[email protected]>Signed-off-by Sven Peter <[email protected]>
Thanks for posting this.
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Joerg Roedel
maintainerQuoted 2 lines · Sven Peter
drivers/iommu/apple-dart.c | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-)
Applied for v5.15, thanks.