Skip to content

Commit 092edad

Browse files
TinaZhangZWjoergroedel
authored andcommitted
iommu: Support mm PASID 1:n with sva domains
Each mm bound to devices gets a PASID and corresponding sva domains allocated in iommu_sva_bind_device(), which are referenced by iommu_mm field of the mm. The PASID is released in __mmdrop(), while a sva domain is released when no one is using it (the reference count is decremented in iommu_sva_unbind_device()). However, although sva domains and their PASID are separate objects such that their own life cycles could be handled independently, an enqcmd use case may require releasing the PASID in releasing the mm (i.e., once a PASID is allocated for a mm, it will be permanently used by the mm and won't be released until the end of mm) and only allows to drop the PASID after the sva domains are released. To this end, mmgrab() is called in iommu_sva_domain_alloc() to increment the mm reference count and mmdrop() is invoked in iommu_domain_free() to decrement the mm reference count. Since the required info of PASID and sva domains is kept in struct iommu_mm_data of a mm, use mm->iommu_mm field instead of the old pasid field in mm struct. The sva domain list is protected by iommu_sva_lock. Besides, this patch removes mm_pasid_init(), as with the introduced iommu_mm structure, initializing mm pasid in mm_init() is unnecessary. Reviewed-by: Lu Baolu <baolu.lu@linux.intel.com> Reviewed-by: Vasant Hegde <vasant.hegde@amd.com> Reviewed-by: Jason Gunthorpe <jgg@nvidia.com> Tested-by: Nicolin Chen <nicolinc@nvidia.com> Signed-off-by: Tina Zhang <tina.zhang@intel.com> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com> Link: https://lore.kernel.org/r/20231027000525.1278806-6-tina.zhang@intel.com Signed-off-by: Joerg Roedel <jroedel@suse.de>
1 parent 541a3e2 commit 092edad

2 files changed

Lines changed: 74 additions & 41 deletions

File tree

drivers/iommu/iommu-sva.c

Lines changed: 54 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,42 @@
1212
static DEFINE_MUTEX(iommu_sva_lock);
1313

1414
/* Allocate a PASID for the mm within range (inclusive) */
15-
static int iommu_sva_alloc_pasid(struct mm_struct *mm, struct device *dev)
15+
static struct iommu_mm_data *iommu_alloc_mm_data(struct mm_struct *mm, struct device *dev)
1616
{
17+
struct iommu_mm_data *iommu_mm;
1718
ioasid_t pasid;
18-
int ret = 0;
19+
20+
lockdep_assert_held(&iommu_sva_lock);
1921

2022
if (!arch_pgtable_dma_compat(mm))
21-
return -EBUSY;
23+
return ERR_PTR(-EBUSY);
2224

23-
mutex_lock(&iommu_sva_lock);
25+
iommu_mm = mm->iommu_mm;
2426
/* Is a PASID already associated with this mm? */
25-
if (mm_valid_pasid(mm)) {
26-
if (mm->pasid >= dev->iommu->max_pasids)
27-
ret = -EOVERFLOW;
28-
goto out;
27+
if (iommu_mm) {
28+
if (iommu_mm->pasid >= dev->iommu->max_pasids)
29+
return ERR_PTR(-EOVERFLOW);
30+
return iommu_mm;
2931
}
3032

33+
iommu_mm = kzalloc(sizeof(struct iommu_mm_data), GFP_KERNEL);
34+
if (!iommu_mm)
35+
return ERR_PTR(-ENOMEM);
36+
3137
pasid = iommu_alloc_global_pasid(dev);
3238
if (pasid == IOMMU_PASID_INVALID) {
33-
ret = -ENOSPC;
34-
goto out;
39+
kfree(iommu_mm);
40+
return ERR_PTR(-ENOSPC);
3541
}
36-
mm->pasid = pasid;
37-
ret = 0;
38-
out:
39-
mutex_unlock(&iommu_sva_lock);
40-
return ret;
42+
iommu_mm->pasid = pasid;
43+
INIT_LIST_HEAD(&iommu_mm->sva_domains);
44+
/*
45+
* Make sure the write to mm->iommu_mm is not reordered in front of
46+
* initialization to iommu_mm fields. If it does, readers may see a
47+
* valid iommu_mm with uninitialized values.
48+
*/
49+
smp_store_release(&mm->iommu_mm, iommu_mm);
50+
return iommu_mm;
4151
}
4252

4353
/**
@@ -58,31 +68,33 @@ static int iommu_sva_alloc_pasid(struct mm_struct *mm, struct device *dev)
5868
*/
5969
struct iommu_sva *iommu_sva_bind_device(struct device *dev, struct mm_struct *mm)
6070
{
71+
struct iommu_mm_data *iommu_mm;
6172
struct iommu_domain *domain;
6273
struct iommu_sva *handle;
6374
int ret;
6475

76+
mutex_lock(&iommu_sva_lock);
77+
6578
/* Allocate mm->pasid if necessary. */
66-
ret = iommu_sva_alloc_pasid(mm, dev);
67-
if (ret)
68-
return ERR_PTR(ret);
79+
iommu_mm = iommu_alloc_mm_data(mm, dev);
80+
if (IS_ERR(iommu_mm)) {
81+
ret = PTR_ERR(iommu_mm);
82+
goto out_unlock;
83+
}
6984

7085
handle = kzalloc(sizeof(*handle), GFP_KERNEL);
71-
if (!handle)
72-
return ERR_PTR(-ENOMEM);
73-
74-
mutex_lock(&iommu_sva_lock);
75-
/* Search for an existing domain. */
76-
domain = iommu_get_domain_for_dev_pasid(dev, mm->pasid,
77-
IOMMU_DOMAIN_SVA);
78-
if (IS_ERR(domain)) {
79-
ret = PTR_ERR(domain);
86+
if (!handle) {
87+
ret = -ENOMEM;
8088
goto out_unlock;
8189
}
8290

83-
if (domain) {
84-
domain->users++;
85-
goto out;
91+
/* Search for an existing domain. */
92+
list_for_each_entry(domain, &mm->iommu_mm->sva_domains, next) {
93+
ret = iommu_attach_device_pasid(domain, dev, iommu_mm->pasid);
94+
if (!ret) {
95+
domain->users++;
96+
goto out;
97+
}
8698
}
8799

88100
/* Allocate a new domain and set it on device pasid. */
@@ -92,23 +104,23 @@ struct iommu_sva *iommu_sva_bind_device(struct device *dev, struct mm_struct *mm
92104
goto out_unlock;
93105
}
94106

95-
ret = iommu_attach_device_pasid(domain, dev, mm->pasid);
107+
ret = iommu_attach_device_pasid(domain, dev, iommu_mm->pasid);
96108
if (ret)
97109
goto out_free_domain;
98110
domain->users = 1;
111+
list_add(&domain->next, &mm->iommu_mm->sva_domains);
112+
99113
out:
100114
mutex_unlock(&iommu_sva_lock);
101115
handle->dev = dev;
102116
handle->domain = domain;
103-
104117
return handle;
105118

106119
out_free_domain:
107120
iommu_domain_free(domain);
121+
kfree(handle);
108122
out_unlock:
109123
mutex_unlock(&iommu_sva_lock);
110-
kfree(handle);
111-
112124
return ERR_PTR(ret);
113125
}
114126
EXPORT_SYMBOL_GPL(iommu_sva_bind_device);
@@ -124,12 +136,13 @@ EXPORT_SYMBOL_GPL(iommu_sva_bind_device);
124136
void iommu_sva_unbind_device(struct iommu_sva *handle)
125137
{
126138
struct iommu_domain *domain = handle->domain;
127-
ioasid_t pasid = domain->mm->pasid;
139+
struct iommu_mm_data *iommu_mm = domain->mm->iommu_mm;
128140
struct device *dev = handle->dev;
129141

130142
mutex_lock(&iommu_sva_lock);
143+
iommu_detach_device_pasid(domain, dev, iommu_mm->pasid);
131144
if (--domain->users == 0) {
132-
iommu_detach_device_pasid(domain, dev, pasid);
145+
list_del(&domain->next);
133146
iommu_domain_free(domain);
134147
}
135148
mutex_unlock(&iommu_sva_lock);
@@ -205,8 +218,11 @@ iommu_sva_handle_iopf(struct iommu_fault *fault, void *data)
205218

206219
void mm_pasid_drop(struct mm_struct *mm)
207220
{
208-
if (likely(!mm_valid_pasid(mm)))
221+
struct iommu_mm_data *iommu_mm = mm->iommu_mm;
222+
223+
if (!iommu_mm)
209224
return;
210225

211-
iommu_free_global_pasid(mm->pasid);
226+
iommu_free_global_pasid(iommu_mm->pasid);
227+
kfree(iommu_mm);
212228
}

include/linux/iommu.h

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,11 @@ struct iommu_domain {
121121
struct { /* IOMMU_DOMAIN_SVA */
122122
struct mm_struct *mm;
123123
int users;
124+
/*
125+
* Next iommu_domain in mm->iommu_mm->sva-domains list
126+
* protected by iommu_sva_lock.
127+
*/
128+
struct list_head next;
124129
};
125130
};
126131
};
@@ -1345,16 +1350,28 @@ static inline bool tegra_dev_iommu_get_stream_id(struct device *dev, u32 *stream
13451350
#ifdef CONFIG_IOMMU_MM_DATA
13461351
static inline void mm_pasid_init(struct mm_struct *mm)
13471352
{
1348-
mm->pasid = IOMMU_PASID_INVALID;
1353+
/*
1354+
* During dup_mm(), a new mm will be memcpy'd from an old one and that makes
1355+
* the new mm and the old one point to a same iommu_mm instance. When either
1356+
* one of the two mms gets released, the iommu_mm instance is freed, leaving
1357+
* the other mm running into a use-after-free/double-free problem. To avoid
1358+
* the problem, zeroing the iommu_mm pointer of a new mm is needed here.
1359+
*/
1360+
mm->iommu_mm = NULL;
13491361
}
1362+
13501363
static inline bool mm_valid_pasid(struct mm_struct *mm)
13511364
{
1352-
return mm->pasid != IOMMU_PASID_INVALID;
1365+
return READ_ONCE(mm->iommu_mm);
13531366
}
13541367

13551368
static inline u32 mm_get_enqcmd_pasid(struct mm_struct *mm)
13561369
{
1357-
return mm->pasid;
1370+
struct iommu_mm_data *iommu_mm = READ_ONCE(mm->iommu_mm);
1371+
1372+
if (!iommu_mm)
1373+
return IOMMU_PASID_INVALID;
1374+
return iommu_mm->pasid;
13581375
}
13591376

13601377
void mm_pasid_drop(struct mm_struct *mm);

0 commit comments

Comments
 (0)