Skip to contentSkip to conversation

iommu/vt-d: Fix infinite loop in free_all_cpu_cached_iovas

PATCH Applied Aaron Campbell 3 participants 3 messages Jul 3, 2016 – Jul 4, 2016 1 message not in the archive IOMMU

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iommu/vt-d: Fix infinite loop in free_all_cpu_cached_iovas

Per VT-d spec Section 10.4.2 ("Capability Register"), the maximum number of possible domains is 64K; indeed this is the maximum value that the cap_ndoms() macro will expand to. Since the value 65536 will not fix in a u16, the 'did' variable must be promoted to an int, otherwise the test for < 65536 will always be true and the loop will never end.

The symptom, in my case, was a hung machine during suspend.

Fixes 3bd4f9112f87 ("iommu/vt-d: Fix overflow of iommu->domains array")
---
 drivers/iommu/intel-iommu.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/iommu/intel-iommu.c +2 -2
index cfe410e..323dac9 100644
@@ -4602,13 +4602,13 @@ static void free_all_cpu_cached_iovas(unsigned int cpu)
 	for (i = 0; i < g_num_of_iommus; i++) {
 		struct intel_iommu *iommu = g_iommus[i];
 		struct dmar_domain *domain;
-		u16 did;
+		int did;
 
 		if (!iommu)
 			continue;
 
 		for (did = 0; did < cap_ndoms(iommu->cap); did++) {
-			domain = get_iommu_domain(iommu, did);
+			domain = get_iommu_domain(iommu, (u16)did);
 
 			if (!domain)
 				continue;
Signature · 1 line
2.7.4

A message that is not in the archive (sent off-list or not yet received): 1467505404-3561-1-git-send-email-aaron-J6AcJDG0ZEwdnm+yROfE0A@public.gmane.org

#2 lore raw
Quoted 3 lines · Aaron Campbell

Per VT-d spec Section 10.4.2 ("Capability Register"), the maximum number of possible domains is 64K; indeed this is the maximum value that the cap_ndoms() macro will expand to.

Good catch. Before 3bd4f9112f87, the loop ended at the fixed value 0xffff. I didn't notice that cap_ndoms() could not only be lower than that, like on my notebook, but could also be greater. Therefore I didn't even look at the type of 'did'.

So, strictly speaking, the original code would not only overflow for small values of cap_ndoms, but would miss the entry at 0xffff in case cap_ndoms was 0x10000. My patch unfortunately changed that to an endless loop, instead.

Jan

Applied
#3 lore raw
In reply to a message not in the archive
Quoted 8 lines · Aaron Campbell

Per VT-d spec Section 10.4.2 ("Capability Register"), the maximum number of possible domains is 64K; indeed this is the maximum value that the cap_ndoms() macro will expand to. Since the value 65536 will not fix in a u16, the 'did' variable must be promoted to an int, otherwise the test for < 65536 will always be true and the loop will never end.

The symptom, in my case, was a hung machine during suspend.

Applied, thanks.