Skip to content

Commit ab7b257

Browse files
committed
iommu/amd: Lock dev_data in attach/detach code paths
Make sure that attaching a detaching a device can't race against each other and protect the iommu_dev_data with a spin_lock in these code paths. Fixes: 92d420e ("iommu/amd: Relax locking in dma_ops path") Reviewed-by: Filippo Sironi <[email protected]> Reviewed-by: Jerry Snitselaar <[email protected]> Signed-off-by: Joerg Roedel <[email protected]>
1 parent 45e528d commit ab7b257

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

drivers/iommu/amd_iommu.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ static struct iommu_dev_data *alloc_dev_data(u16 devid)
201201
if (!dev_data)
202202
return NULL;
203203

204+
spin_lock_init(&dev_data->lock);
204205
dev_data->devid = devid;
205206
ratelimit_default_init(&dev_data->rs);
206207

@@ -2157,6 +2158,8 @@ static int attach_device(struct device *dev,
21572158

21582159
dev_data = get_dev_data(dev);
21592160

2161+
spin_lock(&dev_data->lock);
2162+
21602163
ret = -EBUSY;
21612164
if (dev_data->domain != NULL)
21622165
goto out;
@@ -2199,6 +2202,8 @@ static int attach_device(struct device *dev,
21992202
domain_flush_complete(domain);
22002203

22012204
out:
2205+
spin_unlock(&dev_data->lock);
2206+
22022207
spin_unlock_irqrestore(&domain->lock, flags);
22032208

22042209
return ret;
@@ -2218,6 +2223,8 @@ static void detach_device(struct device *dev)
22182223

22192224
spin_lock_irqsave(&domain->lock, flags);
22202225

2226+
spin_lock(&dev_data->lock);
2227+
22212228
/*
22222229
* First check if the device is still attached. It might already
22232230
* be detached from its domain because the generic
@@ -2240,6 +2247,8 @@ static void detach_device(struct device *dev)
22402247
dev_data->ats.enabled = false;
22412248

22422249
out:
2250+
spin_unlock(&dev_data->lock);
2251+
22432252
spin_unlock_irqrestore(&domain->lock, flags);
22442253
}
22452254

drivers/iommu/amd_iommu_types.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,9 @@ struct devid_map {
633633
* This struct contains device specific data for the IOMMU
634634
*/
635635
struct iommu_dev_data {
636+
/*Protect against attach/detach races */
637+
spinlock_t lock;
638+
636639
struct list_head list; /* For domain->dev_list */
637640
struct llist_node dev_data_list; /* For global dev_data_list */
638641
struct protection_domain *domain; /* Domain the device is bound to */

0 commit comments

Comments
 (0)