Skip to contentSkip to conversation

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

PATCH In review Oleksandr Tyshchenko 4 participants 4 messages Feb 17, 2020 – Feb 24, 2020

lore
Share & tools
lore
#0 lore raw

PATCH v2 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]>

Changes V1 [1] -> V2:
- Rename "data" to "imuctr" in ipmmu_utlb_enable()
- Disable already enabled uTLBs in ipmmu_attach_device() in case of error

[1] https://patchwork.kernel.org/patch/11356303/

---
 xen/drivers/passthrough/arm/ipmmu-vmsa.c | 49 ++++++++++++++++++++++++++++----
 1 file changed, 43 insertions(+), 6 deletions(-)
xen/drivers/passthrough/arm/ipmmu-vmsa.c +43 -6
index 9cfae7e..b2a65df 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 imuctr;
+
+    /*
+     * 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.
+     */
+    imuctr = ipmmu_read(mmu, IMUCTR(utlb));
+    if ( imuctr & IMUCTR_MMUEN )
+    {
+        unsigned int context_id;
+
+        context_id = (imuctr & 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), imuctr |
                 IMUCTR_TTSEL_MMU(domain->context_id) | IMUCTR_MMUEN);
+
+    return 0;
 }
 
 /* Disable MMU translation for the micro-TLB. */
@@ -671,7 +698,17 @@ 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 )
+        {
+            while ( i-- )
+                ipmmu_utlb_disable(domain, fwspec->ids[i]);
+
+            return ret;
+        }
+    }
 
     return 0;
 }
Signature · 7 lines
2.7.4


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

Hello Oleksandr,

Quoted 7 lines · Oleksandr Tyshchenko

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).

Quoted 6 lines

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.

Quoted 11 lines

---

CC Julien Grall <[email protected]>
CC Stefano Stabellini <[email protected]>
CC Volodymyr Babchuk <[email protected]>
CC Yoshihiro Shimoda <[email protected]>

Changes V1 [1] -> V2:
- Rename "data" to "imuctr" in ipmmu_utlb_enable()
- Disable already enabled uTLBs in ipmmu_attach_device() in case of error

Signature · 5 lines
Volodymyr Babchuk at EPAM
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Acked-by on v2 1/1
#2 lore raw
In reply to Oleksandr Tyshchenko

Hi Oleksandr,

Quoted 7 lines · Oleksandr Tyshchenko

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).

Quoted 6 lines

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.

It would be good to get a review from Yoshihiro. If I don't see an answer by the end of the week, I will commit without it.

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Reviewed-by on v2 1/1
#3 lore raw
In reply to Oleksandr Tyshchenko

Hi Oleksandr-san,

Quoted 12 lines

From: Oleksandr Tyshchenko, Sent: Tuesday, February 18, 2020 12:06 AM

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.

Quoted 8 lines

---

CC Julien Grall <[email protected]>
CC Stefano Stabellini <[email protected]>
CC Volodymyr Babchuk <[email protected]>
CC Yoshihiro Shimoda <[email protected]>

Thank you for the patch!

Best regards,
Yoshihiro Shimoda

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