Skip to content

Commit 9101704

Browse files
committed
iommu/vt-d: Fix PASID table allocation
Somehow I ended up with an off-by-three error in calculating the size of the PASID and PASID State tables, which triggers allocations failures as those tables unfortunately have to be physically contiguous. In fact, even the *correct* maximum size of 8MiB is problematic and is wont to lead to allocation failures. Since I have extracted a promise that this *will* be fixed in hardware, I'm happy to limit it on the current hardware to a maximum of 0x20000 PASIDs, which gives us 1MiB tables — still not ideal, but better than before. Reported by Mika Kuoppala <[email protected]> and also by Xunlei Pang <[email protected]> who submitted a simpler patch to fix only the allocation (and not the free) to the "correct" limit... which was still problematic. Signed-off-by: David Woodhouse <[email protected]> Cc: [email protected]
1 parent 1c38718 commit 9101704

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

drivers/iommu/intel-svm.c

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,18 @@ int intel_svm_alloc_pasid_tables(struct intel_iommu *iommu)
3939
struct page *pages;
4040
int order;
4141

42-
order = ecap_pss(iommu->ecap) + 7 - PAGE_SHIFT;
43-
if (order < 0)
44-
order = 0;
45-
42+
/* Start at 2 because it's defined as 2^(1+PSS) */
43+
iommu->pasid_max = 2 << ecap_pss(iommu->ecap);
44+
45+
/* Eventually I'm promised we will get a multi-level PASID table
46+
* and it won't have to be physically contiguous. Until then,
47+
* limit the size because 8MiB contiguous allocations can be hard
48+
* to come by. The limit of 0x20000, which is 1MiB for each of
49+
* the PASID and PASID-state tables, is somewhat arbitrary. */
50+
if (iommu->pasid_max > 0x20000)
51+
iommu->pasid_max = 0x20000;
52+
53+
order = get_order(sizeof(struct pasid_entry) * iommu->pasid_max);
4654
pages = alloc_pages(GFP_KERNEL | __GFP_ZERO, order);
4755
if (!pages) {
4856
pr_warn("IOMMU: %s: Failed to allocate PASID table\n",
@@ -53,6 +61,8 @@ int intel_svm_alloc_pasid_tables(struct intel_iommu *iommu)
5361
pr_info("%s: Allocated order %d PASID table.\n", iommu->name, order);
5462

5563
if (ecap_dis(iommu->ecap)) {
64+
/* Just making it explicit... */
65+
BUILD_BUG_ON(sizeof(struct pasid_entry) != sizeof(struct pasid_state_entry));
5666
pages = alloc_pages(GFP_KERNEL | __GFP_ZERO, order);
5767
if (pages)
5868
iommu->pasid_state_table = page_address(pages);
@@ -68,11 +78,7 @@ int intel_svm_alloc_pasid_tables(struct intel_iommu *iommu)
6878

6979
int intel_svm_free_pasid_tables(struct intel_iommu *iommu)
7080
{
71-
int order;
72-
73-
order = ecap_pss(iommu->ecap) + 7 - PAGE_SHIFT;
74-
if (order < 0)
75-
order = 0;
81+
int order = get_order(sizeof(struct pasid_entry) * iommu->pasid_max);
7682

7783
if (iommu->pasid_table) {
7884
free_pages((unsigned long)iommu->pasid_table, order);
@@ -371,8 +377,8 @@ int intel_svm_bind_mm(struct device *dev, int *pasid, int flags, struct svm_dev_
371377
}
372378
svm->iommu = iommu;
373379

374-
if (pasid_max > 2 << ecap_pss(iommu->ecap))
375-
pasid_max = 2 << ecap_pss(iommu->ecap);
380+
if (pasid_max > iommu->pasid_max)
381+
pasid_max = iommu->pasid_max;
376382

377383
/* Do not use PASID 0 in caching mode (virtualised IOMMU) */
378384
ret = idr_alloc(&iommu->pasid_idr, svm,

include/linux/intel-iommu.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,7 @@ struct intel_iommu {
429429
struct page_req_dsc *prq;
430430
unsigned char prq_name[16]; /* Name for PRQ interrupt */
431431
struct idr pasid_idr;
432+
u32 pasid_max;
432433
#endif
433434
struct q_inval *qi; /* Queued invalidation info */
434435
u32 *iommu_state; /* Store iommu states between suspend and resume.*/

0 commit comments

Comments
 (0)