Skip to contentSkip to conversation

x86/IOMMU: Move allocation in iommu_identity_mapping

PATCH In review Teddy Astie 2 participants 2 messages Jul 17, 2024 – Jul 18, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/IOMMU: Move allocation in iommu_identity_mapping

If for some reason, xmalloc fails after having mapped the reserved regions, a error is reported, but the regions are actually mapped in p2m.

Move the allocation before trying to map the regions, in case the allocation fails, no mapping is actually done which could allows this operation to be retried with the same regions without failing due to already existing mappings.

Fixes c0e19d7c6c ("IOMMU: generalize VT-d's tracking of mapped RMRR regions")
---
 xen/drivers/passthrough/x86/iommu.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
xen/drivers/passthrough/x86/iommu.c +7 -3
index cc0062b027..b6bc6d71cb 100644
@@ -267,18 +267,22 @@ int iommu_identity_mapping(struct domain *d, p2m_access_t p2ma,
     if ( p2ma == p2m_access_x )
         return -ENOENT;
 
+    map = xmalloc(struct identity_map);
+    if ( !map )
+        return -ENOMEM;
+
     while ( base_pfn < end_pfn )
     {
         int err = set_identity_p2m_entry(d, base_pfn, p2ma, flag);
 
         if ( err )
+        {
+            xfree(map);
             return err;
+        }
         base_pfn++;
     }
 
-    map = xmalloc(struct identity_map);
-    if ( !map )
-        return -ENOMEM;
     map->base = base;
     map->end = end;
     map->access = p2ma;
Signature · 9 lines
2.45.2



Teddy Astie | Vates XCP-ng Intern

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech
#1 lore raw
Quoted 35 lines · Teddy Astie

If for some reason, xmalloc fails after having mapped the reserved regions, a error is reported, but the regions are actually mapped in p2m.

Move the allocation before trying to map the regions, in case the allocation fails, no mapping is actually done which could allows this operation to be retried with the same regions without failing due to already existing mappings.

Fixes c0e19d7c6c ("IOMMU: generalize VT-d's tracking of mapped RMRR regions")
---
 xen/drivers/passthrough/x86/iommu.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
xen/drivers/passthrough/x86/iommu.c +6 -0
index cc0062b027..b6bc6d71cb 100644
@@ -267,18 +267,22 @@ int iommu_identity_mapping(struct domain *d, p2m_access_t p2ma,
     if ( p2ma == p2m_access_x )
         return -ENOENT;
 
+    map = xmalloc(struct identity_map);
+    if ( !map )
+        return -ENOMEM;
+
     while ( base_pfn < end_pfn )
     {
         int err = set_identity_p2m_entry(d, base_pfn, p2ma, flag);
 
         if ( err )
+        {
+            xfree(map);
             return err;

xen/drivers/passthrough/x86/iommu.c int iommu_identity_mapping(struct domain *d, p2m_access_t p2ma, · line 288

Quoted 10 lines
xen/drivers/passthrough/x86/iommu.c +1 -3
+        }
         base_pfn++;
     }
 
-    map = xmalloc(struct identity_map);
-    if ( !map )
-        return -ENOMEM;
     map->base = base;
     map->end = end;
     map->access = p2ma;

That covers the case where xmalloc fails, but what about the case where set_identity_p2m_entry() fails in for a middle pfn? (i.e: due to ENOMEM).

Cheers,
Alejandro