Skip to content

Commit 65d4418

Browse files
jgunthorpejoergroedel
authored andcommitted
iommu/sva: Restore SVA handle sharing
Prior to commit 092edad ("iommu: Support mm PASID 1:n with sva domains") the code allowed a SVA handle to be bound multiple times to the same (mm, device) pair. This was alluded to in the kdoc comment, but we had understood this to be more a remark about allowing multiple devices, not a literal same-driver re-opening the same SVA. It turns out uacce and idxd were both relying on the core code to handle reference counting for same-device same-mm scenarios. As this looks hard to resolve in the drivers bring it back to the core code. The new design has changed the meaning of the domain->users refcount to refer to the number of devices that are sharing that domain for the same mm. This is part of the design to lift the SVA domain de-duplication out of the drivers. Return the old behavior by explicitly de-duplicating the struct iommu_sva handle. The same (mm, device) will return the same handle pointer and the core code will handle tracking this. The last unbind of the handle will destroy it. Fixes: 092edad ("iommu: Support mm PASID 1:n with sva domains") Reported-by: Zhangfei Gao <zhangfei.gao@linaro.org> Closes: https://lore.kernel.org/all/20240221110658.529-1-zhangfei.gao@linaro.org/ Tested-by: Zhangfei Gao <zhangfei.gao@linaro.org> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com> Reviewed-by: Lu Baolu <baolu.lu@linux.intel.com> Link: https://lore.kernel.org/r/0-v1-9455fc497a6f+3b4-iommu_sva_sharing_jgg@nvidia.com Signed-off-by: Joerg Roedel <jroedel@suse.de>
1 parent 16b1b39 commit 65d4418

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

drivers/iommu/iommu-sva.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ static struct iommu_mm_data *iommu_alloc_mm_data(struct mm_struct *mm, struct de
4141
}
4242
iommu_mm->pasid = pasid;
4343
INIT_LIST_HEAD(&iommu_mm->sva_domains);
44+
INIT_LIST_HEAD(&iommu_mm->sva_handles);
4445
/*
4546
* Make sure the write to mm->iommu_mm is not reordered in front of
4647
* initialization to iommu_mm fields. If it does, readers may see a
@@ -82,6 +83,14 @@ struct iommu_sva *iommu_sva_bind_device(struct device *dev, struct mm_struct *mm
8283
goto out_unlock;
8384
}
8485

86+
list_for_each_entry(handle, &mm->iommu_mm->sva_handles, handle_item) {
87+
if (handle->dev == dev) {
88+
refcount_inc(&handle->users);
89+
mutex_unlock(&iommu_sva_lock);
90+
return handle;
91+
}
92+
}
93+
8594
handle = kzalloc(sizeof(*handle), GFP_KERNEL);
8695
if (!handle) {
8796
ret = -ENOMEM;
@@ -108,7 +117,9 @@ struct iommu_sva *iommu_sva_bind_device(struct device *dev, struct mm_struct *mm
108117
if (ret)
109118
goto out_free_domain;
110119
domain->users = 1;
120+
refcount_set(&handle->users, 1);
111121
list_add(&domain->next, &mm->iommu_mm->sva_domains);
122+
list_add(&handle->handle_item, &mm->iommu_mm->sva_handles);
112123

113124
out:
114125
mutex_unlock(&iommu_sva_lock);
@@ -141,6 +152,12 @@ void iommu_sva_unbind_device(struct iommu_sva *handle)
141152
struct device *dev = handle->dev;
142153

143154
mutex_lock(&iommu_sva_lock);
155+
if (!refcount_dec_and_test(&handle->users)) {
156+
mutex_unlock(&iommu_sva_lock);
157+
return;
158+
}
159+
list_del(&handle->handle_item);
160+
144161
iommu_detach_device_pasid(domain, dev, iommu_mm->pasid);
145162
if (--domain->users == 0) {
146163
list_del(&domain->next);

include/linux/iommu.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -892,11 +892,14 @@ struct iommu_fwspec {
892892
struct iommu_sva {
893893
struct device *dev;
894894
struct iommu_domain *domain;
895+
struct list_head handle_item;
896+
refcount_t users;
895897
};
896898

897899
struct iommu_mm_data {
898900
u32 pasid;
899901
struct list_head sva_domains;
902+
struct list_head sva_handles;
900903
};
901904

902905
int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode,

0 commit comments

Comments
 (0)