Skip to contentSkip to conversation

iommu/arm: Don't allow the same micro-TLB to be shared between domains

PATCH Superseded Oleksandr Tyshchenko 2 participants 5 messages Jan 29, 2020 – Jan 30, 2020

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iommu/arm: Don't allow the same micro-TLB to be shared between domains

From: Oleksandr Tyshchenko <[email protected]>

For the IPMMU-VMSA we need to prevent the use cases where devices which use the same micro-TLB are assigned to different Xen domains (micro-TLB cannot be shared between multiple Xen domains, since it points to the context bank to use for the page walk).

As each Xen domain uses individual context bank pointed by context_id, we can potentially recognize that use case by comparing current and new context_id for the already enabled micro-TLB and prevent different context bank from being set.

---

CC Julien Grall <[email protected]>
CC Stefano Stabellini <[email protected]>
CC Volodymyr Babchuk <[email protected]>
CC Yoshihiro Shimoda <[email protected]>
---
 xen/drivers/passthrough/arm/ipmmu-vmsa.c | 44 +++++++++++++++++++++++++++-----
 1 file changed, 38 insertions(+), 6 deletions(-)
xen/drivers/passthrough/arm/ipmmu-vmsa.c +38 -6
index 9cfae7e..c21d2d7 100644
@@ -257,6 +257,7 @@ static DEFINE_SPINLOCK(ipmmu_devices_lock);
 #define IMUCTR_TTSEL_MMU(n)    ((n) << 4)
 #define IMUCTR_TTSEL_PMB       (8 << 4)
 #define IMUCTR_TTSEL_MASK      (15 << 4)
+#define IMUCTR_TTSEL_SHIFT     4
 #define IMUCTR_FLUSH           (1 << 1)
 #define IMUCTR_MMUEN           (1 << 0)
 
@@ -434,19 +435,45 @@ static void ipmmu_tlb_invalidate(struct ipmmu_vmsa_domain *domain)
 }
 
 /* Enable MMU translation for the micro-TLB. */
-static void ipmmu_utlb_enable(struct ipmmu_vmsa_domain *domain,
-                              unsigned int utlb)
+static int ipmmu_utlb_enable(struct ipmmu_vmsa_domain *domain,
+                             unsigned int utlb)
 {
     struct ipmmu_vmsa_device *mmu = domain->mmu;
+    uint32_t data;
+
+    /*
+     * We need to prevent the use cases where devices which use the same
+     * micro-TLB are assigned to different Xen domains (micro-TLB cannot be
+     * shared between multiple Xen domains, since it points to the context bank
+     * to use for the page walk).
+     * As each Xen domain uses individual context bank pointed by context_id,
+     * we can potentially recognize that use case by comparing current and new
+     * context_id for already enabled micro-TLB and prevent different context
+     * bank from being set.
+     */
+    data = ipmmu_read(mmu, IMUCTR(utlb));
+    if ( data & IMUCTR_MMUEN )
+    {
+        unsigned int context_id;
+
+        context_id = (data & IMUCTR_TTSEL_MASK) >> IMUCTR_TTSEL_SHIFT;
+        if ( domain->context_id != context_id )
+        {
+            dev_err(mmu->dev, "Micro-TLB %u already assigned to IPMMU context %u\n",
+                    utlb, context_id);
+            return -EINVAL;
+        }
+    }
 
     /*
      * TODO: Reference-count the micro-TLB as several bus masters can be
-     * connected to the same micro-TLB. Prevent the use cases where
-     * the same micro-TLB could be shared between multiple Xen domains.
+     * connected to the same micro-TLB.
      */
     ipmmu_write(mmu, IMUASID(utlb), 0);
-    ipmmu_write(mmu, IMUCTR(utlb), ipmmu_read(mmu, IMUCTR(utlb)) |
+    ipmmu_write(mmu, IMUCTR(utlb), data |
                 IMUCTR_TTSEL_MMU(domain->context_id) | IMUCTR_MMUEN);
+
+    return 0;
 }
 
 /* Disable MMU translation for the micro-TLB. */
@@ -671,7 +698,12 @@ static int ipmmu_attach_device(struct ipmmu_vmsa_domain *domain,
         dev_info(dev, "Reusing IPMMU context %u\n", domain->context_id);
 
     for ( i = 0; i < fwspec->num_ids; ++i )
-        ipmmu_utlb_enable(domain, fwspec->ids[i]);
+    {
+        int ret = ipmmu_utlb_enable(domain, fwspec->ids[i]);
+
+        if ( ret )
+            return ret;
+    }
 
     return 0;
 }
Signature · 7 lines
2.7.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#1 lore raw

Hi Oleksandr,

Oleksandr Tyshchenko writes:

[...]

xen/drivers/passthrough/arm/ipmmu-vmsa.c static void ipmmu_tlb_invalidate(struct ipmmu_vmsa_domain *domain) · line 442

Quoted 11 lines
xen/drivers/passthrough/arm/ipmmu-vmsa.c +3 -2
@@ -434,19 +435,45 @@ static void ipmmu_tlb_invalidate(struct ipmmu_vmsa_domain *domain)
 }
 
 /* Enable MMU translation for the micro-TLB. */
-static void ipmmu_utlb_enable(struct ipmmu_vmsa_domain *domain,
-                              unsigned int utlb)
+static int ipmmu_utlb_enable(struct ipmmu_vmsa_domain *domain,
+                             unsigned int utlb)
 {
     struct ipmmu_vmsa_device *mmu = domain->mmu;
+    uint32_t data;

Just nitpicking: I believe, that "imuctr" is better name than "data".

xen/drivers/passthrough/arm/ipmmu-vmsa.c static void ipmmu_tlb_invalidate(struct ipmmu_vmsa_domain *domain) · line 454

Quoted 12 lines
xen/drivers/passthrough/arm/ipmmu-vmsa.c +12 -0
+
+    /*
+     * We need to prevent the use cases where devices which use the same
+     * micro-TLB are assigned to different Xen domains (micro-TLB cannot be
+     * shared between multiple Xen domains, since it points to the context bank
+     * to use for the page walk).
+     * As each Xen domain uses individual context bank pointed by context_id,
+     * we can potentially recognize that use case by comparing current and new
+     * context_id for already enabled micro-TLB and prevent different context
+     * bank from being set.
+     */
+    data = ipmmu_read(mmu, IMUCTR(utlb));

I can see that this code is not covered by spinlock. So, I believe, there can be a race comdition, when this register is being read on two CPUs simultaneously.

Quoted 29 lines
xen/drivers/passthrough/arm/ipmmu-vmsa.c +16 -3
+    if ( data & IMUCTR_MMUEN )
+    {
+        unsigned int context_id;
+
+        context_id = (data & IMUCTR_TTSEL_MASK) >> IMUCTR_TTSEL_SHIFT;
+        if ( domain->context_id != context_id )
+        {
+            dev_err(mmu->dev, "Micro-TLB %u already assigned to IPMMU context %u\n",
+                    utlb, context_id);
+            return -EINVAL;
+        }
+    }
 
     /*
      * TODO: Reference-count the micro-TLB as several bus masters can be
-     * connected to the same micro-TLB. Prevent the use cases where
-     * the same micro-TLB could be shared between multiple Xen domains.
+     * connected to the same micro-TLB.
      */
     ipmmu_write(mmu, IMUASID(utlb), 0);
-    ipmmu_write(mmu, IMUCTR(utlb), ipmmu_read(mmu, IMUCTR(utlb)) |
+    ipmmu_write(mmu, IMUCTR(utlb), data |
                 IMUCTR_TTSEL_MMU(domain->context_id) | IMUCTR_MMUEN);
+
+    return 0;
 }
 
 /* Disable MMU translation for the micro-TLB. */

xen/drivers/passthrough/arm/ipmmu-vmsa.c static int ipmmu_attach_device(struct ipmmu_vmsa_domain *domain, · line 705

Quoted 9 lines
xen/drivers/passthrough/arm/ipmmu-vmsa.c +5 -1
@@ -671,7 +698,12 @@ static int ipmmu_attach_device(struct ipmmu_vmsa_domain *domain,
         dev_info(dev, "Reusing IPMMU context %u\n", domain->context_id);
 
     for ( i = 0; i < fwspec->num_ids; ++i )
-        ipmmu_utlb_enable(domain, fwspec->ids[i]);
+    {
+        int ret = ipmmu_utlb_enable(domain, fwspec->ids[i]);
+
+        if ( ret )
+            return ret;

I can't see error path where ipmmu_utlb_disable() would be called for already enable uTLBs. Is this normal?

Quoted 4 lines
xen/drivers/passthrough/arm/ipmmu-vmsa.c +1 -0
+    }
 
     return 0;
 }
Signature · 5 lines
Volodymyr Babchuk at EPAM
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
author
#2 lore raw
Quoted 2 lines

Hi Oleksandr,

Hi Volodymyr

Quoted 12 lines

Review static void ipmmu_tlb_invalidate(struct ipmmu_vmsa_domain *domain) · line 442

Quoted 11 lines
(quoted diff) +3 -2
@@ -434,19 +435,45 @@ static void ipmmu_tlb_invalidate(struct ipmmu_vmsa_domain *domain)
  }
 
  /* Enable MMU translation for the micro-TLB. */
-static void ipmmu_utlb_enable(struct ipmmu_vmsa_domain *domain,
-                              unsigned int utlb)
+static int ipmmu_utlb_enable(struct ipmmu_vmsa_domain *domain,
+                             unsigned int utlb)
  {
      struct ipmmu_vmsa_device *mmu = domain->mmu;
+    uint32_t data;

Just nitpicking: I believe, that "imuctr" is better name than "data".

Agree, will rename

Quoted 13 lines

Review static void ipmmu_tlb_invalidate(struct ipmmu_vmsa_domain *domain) · line 454

Quoted 13 lines
(quoted diff) +12 -0
+
+    /*
+     * We need to prevent the use cases where devices which use the same
+     * micro-TLB are assigned to different Xen domains (micro-TLB cannot be
+     * shared between multiple Xen domains, since it points to the context bank
+     * to use for the page walk).
+     * As each Xen domain uses individual context bank pointed by context_id,
+     * we can potentially recognize that use case by comparing current and new
+     * context_id for already enabled micro-TLB and prevent different context
+     * bank from being set.
+     */
+    data = ipmmu_read(mmu, IMUCTR(utlb));
Quoted 3 lines

I can see that this code is not covered by spinlock. So, I believe, there can be a race comdition, when this register is being read on two CPUs simultaneously.

I don't think, ipmmu_assign(deassign)_device callbacks take a spinlock, so the micro-TLB management routines inside are protected.

Quoted 12 lines

/* Disable MMU translation for the micro-TLB. */

(quoted diff) +5 -1
@@ -671,7 +698,12 @@ static int ipmmu_attach_device(struct ipmmu_vmsa_domain *domain,
          dev_info(dev, "Reusing IPMMU context %u\n", domain->context_id);
 
      for ( i = 0; i < fwspec->num_ids; ++i )
-        ipmmu_utlb_enable(domain, fwspec->ids[i]);
+    {
+        int ret = ipmmu_utlb_enable(domain, fwspec->ids[i]);
+
+        if ( ret )
+            return ret;
Quoted 2 lines

I can't see error path where ipmmu_utlb_disable() would be called for already enable uTLBs. Is this normal?

Good question. Indeed, we need to restore previous state in case of error.

I will add the following:

xen/drivers/passthrough/arm/ipmmu-vmsa.c +0 -0
index c21d2d7..411fc0f 100644
@@ -702,7 +702,12 @@ static int ipmmu_attach_device(struct ipmmu_vmsa_domain *domain,

         int ret = ipmmu_utlb_enable(domain, fwspec->ids[i]);

         if ( ret )
+        {
+            while ( i-- )
+                ipmmu_utlb_disable(domain, fwspec->ids[i]); +
             return ret;
+        }
     }

     return 0;

Signature · 9 lines
Regards,

Oleksandr Tyshchenko


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#3 lore raw

Hi Oleksandr,

Oleksandr writes:

[...]

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
(quoted diff) +12 -0
+
+    /*
+     * We need to prevent the use cases where devices which use the same
+     * micro-TLB are assigned to different Xen domains (micro-TLB cannot be
+     * shared between multiple Xen domains, since it points to the context bank
+     * to use for the page walk).
+     * As each Xen domain uses individual context bank pointed by context_id,
+     * we can potentially recognize that use case by comparing current and new
+     * context_id for already enabled micro-TLB and prevent different context
+     * bank from being set.
+     */
+    data = ipmmu_read(mmu, IMUCTR(utlb));
Quoted 7 lines
Quoted 3 lines

I can see that this code is not covered by spinlock. So, I believe, there can be a race comdition, when this register is being read on two CPUs simultaneously.

I don't think, ipmmu_assign(deassign)_device callbacks take a spinlock, so the micro-TLB management routines inside are protected.

Yeah, you are right. Somehow I missed this when checked the code yesterday.

Quoted 25 lines
Quoted 11 lines
    /* Disable MMU translation for the micro-TLB. */
(quoted diff) +5 -1
@@ -671,7 +698,12 @@ static int ipmmu_attach_device(struct ipmmu_vmsa_domain *domain,
          dev_info(dev, "Reusing IPMMU context %u\n", domain->context_id);
        for ( i = 0; i < fwspec->num_ids; ++i )
-        ipmmu_utlb_enable(domain, fwspec->ids[i]);
+    {
+        int ret = ipmmu_utlb_enable(domain, fwspec->ids[i]);
+
+        if ( ret )
+            return ret;
Quoted 2 lines

I can't see error path where ipmmu_utlb_disable() would be called for already enable uTLBs. Is this normal?

Good question. Indeed, we need to restore previous state in case of error.

I will add the following:

xen/drivers/passthrough/arm/ipmmu-vmsa.c +0 -0
index c21d2d7..411fc0f 100644

xen/drivers/passthrough/arm/ipmmu-vmsa.c static int ipmmu_attach_device(struct · line 707

Quoted 6 lines
xen/drivers/passthrough/arm/ipmmu-vmsa.c +2 -0
@@ -702,7 +702,12 @@ static int ipmmu_attach_device(struct
 ipmmu_vmsa_domain *domain,
         int ret = ipmmu_utlb_enable(domain, fwspec->ids[i]);
 
         if ( ret )
+        {
+            while ( i-- )

You will call

xen/drivers/passthrough/arm/ipmmu-vmsa.c static int ipmmu_attach_device(struct · line 708

Quoted 1 line
xen/drivers/passthrough/arm/ipmmu-vmsa.c +1 -0
+                ipmmu_utlb_disable(domain, fwspec->ids[i]);

for uTLB that caused the error. Likely, this uTLB right now is assigned for another domain. So, you will disable active uTLB which belongs to that domain.

Quoted 6 lines
xen/drivers/passthrough/arm/ipmmu-vmsa.c +2 -0
+
             return ret;
+        }
     }
 
     return 0;

--
Volodymyr Babchuk at EPAM
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#4 lore raw

Volodymyr Babchuk writes:

Oleksandr,

[...]

Quoted 9 lines
Quoted 9 lines
xen/drivers/passthrough/arm/ipmmu-vmsa.c +0 -0
index c21d2d7..411fc0f 100644
@@ -702,7 +702,12 @@ static int ipmmu_attach_device(struct
 ipmmu_vmsa_domain *domain,
         int ret = ipmmu_utlb_enable(domain, fwspec->ids[i]);
 
Quoted 8 lines

xen/drivers/passthrough/arm/ipmmu-vmsa.c static int ipmmu_attach_device(struct · line 707

Quoted 3 lines
xen/drivers/passthrough/arm/ipmmu-vmsa.c +2 -0
         if ( ret )
+        {
+            while ( i-- )

You will call

xen/drivers/passthrough/arm/ipmmu-vmsa.c static int ipmmu_attach_device(struct · line 708

Quoted 1 line
xen/drivers/passthrough/arm/ipmmu-vmsa.c +1 -0
+                ipmmu_utlb_disable(domain, fwspec->ids[i]);

for uTLB that caused the error. Likely, this uTLB right now is assigned for another domain. So, you will disable active uTLB which belongs to that domain.

Please disregard this. I realized that i-- will ensure that right index will be used. Sorry for the noise.

Signature · 5 lines
Volodymyr Babchuk at EPAM
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel