Skip to contentSkip to conversation

iommu/ipmmu-vmsa: Rereserving a free context before setting up a pagetable

PATCH Applied Oleksandr Tyshchenko 4 participants 7 messages Aug 23, 2017 – Aug 30, 2017 3 messages not in the archive IOMMU

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 iommu/ipmmu-vmsa: Rereserving a free context before setting up a pagetable

From: Oleksandr Tyshchenko <[email protected]>

Reserving a free context is both quicker and more likely to fail (due to limited hardware resources) than setting up a pagetable. What is more the pagetable init/cleanup code could require the context to be set up.

CC Robin Murphy <[email protected]>
CC Laurent Pinchart <laurent.pinchart-ryLnwIuWjnjg/[email protected]>
CC Joerg Roedel <[email protected]>

---
This patch fixes a bug during rollback logic:
In ipmmu_domain_init_context() we are trying to find an unused context and if operation fails we will call free_io_pgtable_ops(), but "domain->context_id" hasn't been initialized yet (likely it is 0 because of kzalloc). Having the following call stack:
free_io_pgtable_ops() -> io_pgtable_tlb_flush_all() -> ipmmu_tlb_flush_all() -> ipmmu_tlb_invalidate()
we will get a mistaken cache flush for a context pointed by uninitialized "domain->context_id".

v1 here:
https://lists.linuxfoundation.org/pipermail/iommu/2017-August/023857.html

---
 drivers/iommu/ipmmu-vmsa.c | 42 +++++++++++++++++++++---------------------
 1 file changed, 21 insertions(+), 21 deletions(-)
drivers/iommu/ipmmu-vmsa.c +21 -21
index 2a38aa1..382f387 100644
@@ -341,6 +341,19 @@ static int ipmmu_domain_allocate_context(struct ipmmu_vmsa_device *mmu,
 	return ret;
 }
 
+static void ipmmu_domain_free_context(struct ipmmu_vmsa_device *mmu,
+				      unsigned int context_id)
+{
+	unsigned long flags;
+
+	spin_lock_irqsave(&mmu->lock, flags);
+
+	clear_bit(context_id, mmu->ctx);
+	mmu->domains[context_id] = NULL;
+
+	spin_unlock_irqrestore(&mmu->lock, flags);
+}
+
 static int ipmmu_domain_init_context(struct ipmmu_vmsa_domain *domain)
 {
 	u64 ttbr;
@@ -370,22 +383,22 @@ static int ipmmu_domain_init_context(struct ipmmu_vmsa_domain *domain)
 	 */
 	domain->cfg.iommu_dev = domain->mmu->dev;
 
-	domain->iop = alloc_io_pgtable_ops(ARM_32_LPAE_S1, &domain->cfg,
-					   domain);
-	if (!domain->iop)
-		return -EINVAL;
-
 	/*
 	 * Find an unused context.
 	 */
 	ret = ipmmu_domain_allocate_context(domain->mmu, domain);
-	if (ret == IPMMU_CTX_MAX) {
-		free_io_pgtable_ops(domain->iop);
+	if (ret == IPMMU_CTX_MAX)
 		return -EBUSY;
-	}
 
 	domain->context_id = ret;
 
+	domain->iop = alloc_io_pgtable_ops(ARM_32_LPAE_S1, &domain->cfg,
+					   domain);
+	if (!domain->iop) {
+		ipmmu_domain_free_context(domain->mmu, domain->context_id);
+		return -EINVAL;
+	}
+
 	/* TTBR0 */
 	ttbr = domain->cfg.arm_lpae_s1_cfg.ttbr[0];
 	ipmmu_ctx_write(domain, IMTTLBR0, ttbr);
@@ -426,19 +439,6 @@ static int ipmmu_domain_init_context(struct ipmmu_vmsa_domain *domain)
 	return 0;
 }
 
-static void ipmmu_domain_free_context(struct ipmmu_vmsa_device *mmu,
-				      unsigned int context_id)
-{
-	unsigned long flags;
-
-	spin_lock_irqsave(&mmu->lock, flags);
-
-	clear_bit(context_id, mmu->ctx);
-	mmu->domains[context_id] = NULL;
-
-	spin_unlock_irqrestore(&mmu->lock, flags);
-}
-
 static void ipmmu_domain_destroy_context(struct ipmmu_vmsa_domain *domain)
 {
 	/*
Signature · 1 line
2.7.4

A message that is not in the archive (sent off-list or not yet received): 1503498702-27996-1-git-send-email-olekstysh-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#2 lore raw

Hi, all.

Any comments?

Quoted 108 lines · Oleksandr Tyshchenko

From: Oleksandr Tyshchenko <[email protected]>

Reserving a free context is both quicker and more likely to fail (due to limited hardware resources) than setting up a pagetable. What is more the pagetable init/cleanup code could require the context to be set up.

CC Robin Murphy <[email protected]>
CC Laurent Pinchart <laurent.pinchart-ryLnwIuWjnjg/[email protected]>
CC Joerg Roedel <[email protected]>

---
This patch fixes a bug during rollback logic:
In ipmmu_domain_init_context() we are trying to find an unused context and if operation fails we will call free_io_pgtable_ops(), but "domain->context_id" hasn't been initialized yet (likely it is 0 because of kzalloc). Having the following call stack:
free_io_pgtable_ops() -> io_pgtable_tlb_flush_all() -> ipmmu_tlb_flush_all() -> ipmmu_tlb_invalidate()
we will get a mistaken cache flush for a context pointed by uninitialized "domain->context_id".

v1 here:
https://lists.linuxfoundation.org/pipermail/iommu/2017-August/023857.html

---
 drivers/iommu/ipmmu-vmsa.c | 42 +++++++++++++++++++++---------------------
 1 file changed, 21 insertions(+), 21 deletions(-)
drivers/iommu/ipmmu-vmsa.c +21 -22
index 2a38aa1..382f387 100644
@@ -341,6 +341,19 @@ static int ipmmu_domain_allocate_context(struct ipmmu_vmsa_device *mmu,
        return ret;
 }
 
+static void ipmmu_domain_free_context(struct ipmmu_vmsa_device *mmu,
+                                     unsigned int context_id)
+{
+       unsigned long flags;
+
+       spin_lock_irqsave(&mmu->lock, flags);
+
+       clear_bit(context_id, mmu->ctx);
+       mmu->domains[context_id] = NULL;
+
+       spin_unlock_irqrestore(&mmu->lock, flags);
+}
+
 static int ipmmu_domain_init_context(struct ipmmu_vmsa_domain *domain)
 {
        u64 ttbr;
@@ -370,22 +383,22 @@ static int ipmmu_domain_init_context(struct ipmmu_vmsa_domain *domain)
         */
        domain->cfg.iommu_dev = domain->mmu->dev;
 
-       domain->iop = alloc_io_pgtable_ops(ARM_32_LPAE_S1, &domain->cfg,
-                                          domain);
-       if (!domain->iop)
-               return -EINVAL;
-
        /*
         * Find an unused context.
         */
        ret = ipmmu_domain_allocate_context(domain->mmu, domain);
-       if (ret == IPMMU_CTX_MAX) {
-               free_io_pgtable_ops(domain->iop);
+       if (ret == IPMMU_CTX_MAX)
                return -EBUSY;
-       }
 
        domain->context_id = ret;
 
+       domain->iop = alloc_io_pgtable_ops(ARM_32_LPAE_S1, &domain->cfg,
+                                          domain);
+       if (!domain->iop) {
+               ipmmu_domain_free_context(domain->mmu, domain->context_id);
+               return -EINVAL;
+       }
+
        /* TTBR0 */
        ttbr = domain->cfg.arm_lpae_s1_cfg.ttbr[0];
        ipmmu_ctx_write(domain, IMTTLBR0, ttbr);
@@ -426,19 +439,6 @@ static int ipmmu_domain_init_context(struct ipmmu_vmsa_domain *domain)
        return 0;
 }
 
-static void ipmmu_domain_free_context(struct ipmmu_vmsa_device *mmu,
-                                     unsigned int context_id)
-{
-       unsigned long flags;
-
-       spin_lock_irqsave(&mmu->lock, flags);
-
-       clear_bit(context_id, mmu->ctx);
-       mmu->domains[context_id] = NULL;
-
-       spin_unlock_irqrestore(&mmu->lock, flags);
-}
-
 static void ipmmu_domain_destroy_context(struct ipmmu_vmsa_domain *domain)
 {
        /*
--

2.7.4

Signature · 3 lines
Regards,

Oleksandr Tyshchenko

A message that is not in the archive (sent off-list or not yet received): CAPD2p-nA0XX2KqhzzeCrWwM4eN6EKhD_EnuVynCGGA+SnO=x8Q-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

Reviewed-by
#4 lore raw
Quoted 12 lines · Oleksandr Tyshchenko

Hi, all.

Any comments?

Quoted 6 lines · Oleksandr Tyshchenko

From: Oleksandr Tyshchenko <[email protected]>

Reserving a free context is both quicker and more likely to fail (due to limited hardware resources) than setting up a pagetable. What is more the pagetable init/cleanup code could require the context to be set up.

Looks OK to me,

Quoted 104 lines
Quoted 101 lines
CC Robin Murphy <[email protected]>
CC Laurent Pinchart <laurent.pinchart-ryLnwIuWjnjg/[email protected]>
CC Joerg Roedel <[email protected]>

---
This patch fixes a bug during rollback logic:
In ipmmu_domain_init_context() we are trying to find an unused context and if operation fails we will call free_io_pgtable_ops(), but "domain->context_id" hasn't been initialized yet (likely it is 0 because of kzalloc). Having the following call stack:
free_io_pgtable_ops() -> io_pgtable_tlb_flush_all() -> ipmmu_tlb_flush_all() -> ipmmu_tlb_invalidate()
we will get a mistaken cache flush for a context pointed by uninitialized "domain->context_id".

v1 here:
https://lists.linuxfoundation.org/pipermail/iommu/2017-August/023857.html

---
 drivers/iommu/ipmmu-vmsa.c | 42 +++++++++++++++++++++---------------------
 1 file changed, 21 insertions(+), 21 deletions(-)
drivers/iommu/ipmmu-vmsa.c +21 -22
index 2a38aa1..382f387 100644
@@ -341,6 +341,19 @@ static int ipmmu_domain_allocate_context(struct ipmmu_vmsa_device *mmu,
        return ret;
 }
 
+static void ipmmu_domain_free_context(struct ipmmu_vmsa_device *mmu,
+                                     unsigned int context_id)
+{
+       unsigned long flags;
+
+       spin_lock_irqsave(&mmu->lock, flags);
+
+       clear_bit(context_id, mmu->ctx);
+       mmu->domains[context_id] = NULL;
+
+       spin_unlock_irqrestore(&mmu->lock, flags);
+}
+
 static int ipmmu_domain_init_context(struct ipmmu_vmsa_domain *domain)
 {
        u64 ttbr;
@@ -370,22 +383,22 @@ static int ipmmu_domain_init_context(struct ipmmu_vmsa_domain *domain)
         */
        domain->cfg.iommu_dev = domain->mmu->dev;
 
-       domain->iop = alloc_io_pgtable_ops(ARM_32_LPAE_S1, &domain->cfg,
-                                          domain);
-       if (!domain->iop)
-               return -EINVAL;
-
        /*
         * Find an unused context.
         */
        ret = ipmmu_domain_allocate_context(domain->mmu, domain);
-       if (ret == IPMMU_CTX_MAX) {
-               free_io_pgtable_ops(domain->iop);
+       if (ret == IPMMU_CTX_MAX)
                return -EBUSY;
-       }
 
        domain->context_id = ret;
 
+       domain->iop = alloc_io_pgtable_ops(ARM_32_LPAE_S1, &domain->cfg,
+                                          domain);
+       if (!domain->iop) {
+               ipmmu_domain_free_context(domain->mmu, domain->context_id);
+               return -EINVAL;
+       }
+
        /* TTBR0 */
        ttbr = domain->cfg.arm_lpae_s1_cfg.ttbr[0];
        ipmmu_ctx_write(domain, IMTTLBR0, ttbr);
@@ -426,19 +439,6 @@ static int ipmmu_domain_init_context(struct ipmmu_vmsa_domain *domain)
        return 0;
 }
 
-static void ipmmu_domain_free_context(struct ipmmu_vmsa_device *mmu,
-                                     unsigned int context_id)
-{
-       unsigned long flags;
-
-       spin_lock_irqsave(&mmu->lock, flags);
-
-       clear_bit(context_id, mmu->ctx);
-       mmu->domains[context_id] = NULL;
-
-       spin_unlock_irqrestore(&mmu->lock, flags);
-}
-
 static void ipmmu_domain_destroy_context(struct ipmmu_vmsa_domain *domain)
 {
        /*
--

2.7.4

Reviewed-by
#5 lore raw
In reply to a message not in the archive

Hi Oleksandr,

Thank you for the patch.

Quoted 11 lines · On Wednesday, 23 August 2017 17:31:42 EEST Oleksandr Tyshchenko wrote:

From: Oleksandr Tyshchenko <[email protected]>

Reserving a free context is both quicker and more likely to fail (due to limited hardware resources) than setting up a pagetable. What is more the pagetable init/cleanup code could require the context to be set up.

CC Robin Murphy <[email protected]>
CC Laurent Pinchart <laurent.pinchart-ryLnwIuWjnjg/[email protected]>
CC Joerg Roedel <[email protected]>
Quoted 93 lines

---
This patch fixes a bug during rollback logic:
In ipmmu_domain_init_context() we are trying to find an unused context and if operation fails we will call free_io_pgtable_ops(), but "domain->context_id" hasn't been initialized yet (likely it is 0 because of kzalloc). Having the following call stack:
free_io_pgtable_ops() -> io_pgtable_tlb_flush_all() -> ipmmu_tlb_flush_all() -> ipmmu_tlb_invalidate()
we will get a mistaken cache flush for a context pointed by uninitialized "domain->context_id".

v1 here:
https://lists.linuxfoundation.org/pipermail/iommu/2017-August/023857.html

---
 drivers/iommu/ipmmu-vmsa.c | 42 +++++++++++++++++++++---------------------
 1 file changed, 21 insertions(+), 21 deletions(-)
drivers/iommu/ipmmu-vmsa.c +13 -0
index 2a38aa1..382f387 100644
@@ -341,6 +341,19 @@ static int ipmmu_domain_allocate_context(struct
 ipmmu_vmsa_device *mmu, return ret;
 }
 
+static void ipmmu_domain_free_context(struct ipmmu_vmsa_device *mmu,
+				      unsigned int context_id)
+{
+	unsigned long flags;
+
+	spin_lock_irqsave(&mmu->lock, flags);
+
+	clear_bit(context_id, mmu->ctx);
+	mmu->domains[context_id] = NULL;
+
+	spin_unlock_irqrestore(&mmu->lock, flags);
+}
+
 static int ipmmu_domain_init_context(struct ipmmu_vmsa_domain *domain)
 {
 	u64 ttbr;
@@ -370,22 +383,22 @@ static int ipmmu_domain_init_context(struct

ipmmu_vmsa_domain *domain) */

drivers/iommu/ipmmu-vmsa.c +8 -21
 	domain->cfg.iommu_dev = domain->mmu->dev;
 
-	domain->iop = alloc_io_pgtable_ops(ARM_32_LPAE_S1, &domain->cfg,
-					   domain);
-	if (!domain->iop)
-		return -EINVAL;
-
 	/*
 	 * Find an unused context.
 	 */
 	ret = ipmmu_domain_allocate_context(domain->mmu, domain);
-	if (ret == IPMMU_CTX_MAX) {
-		free_io_pgtable_ops(domain->iop);
+	if (ret == IPMMU_CTX_MAX)
 		return -EBUSY;
-	}
 
 	domain->context_id = ret;
 
+	domain->iop = alloc_io_pgtable_ops(ARM_32_LPAE_S1, &domain->cfg,
+					   domain);
+	if (!domain->iop) {
+		ipmmu_domain_free_context(domain->mmu, domain->context_id);
+		return -EINVAL;
+	}
+
 	/* TTBR0 */
 	ttbr = domain->cfg.arm_lpae_s1_cfg.ttbr[0];
 	ipmmu_ctx_write(domain, IMTTLBR0, ttbr);
@@ -426,19 +439,6 @@ static int ipmmu_domain_init_context(struct
 ipmmu_vmsa_domain *domain) return 0;
 }
 
-static void ipmmu_domain_free_context(struct ipmmu_vmsa_device *mmu,
-				      unsigned int context_id)
-{
-	unsigned long flags;
-
-	spin_lock_irqsave(&mmu->lock, flags);
-
-	clear_bit(context_id, mmu->ctx);
-	mmu->domains[context_id] = NULL;
-
-	spin_unlock_irqrestore(&mmu->lock, flags);
-}
-
 static void ipmmu_domain_destroy_context(struct ipmmu_vmsa_domain *domain)
 {
 	/*
Signature · 3 lines
Regards,

Laurent Pinchart
#6 lore raw

Hi Laurent, Robin

Quoted 117 lines · Laurent Pinchart

Hi Oleksandr,

Thank you for the patch.

Quoted 11 lines · On Wednesday, 23 August 2017 17:31:42 EEST Oleksandr Tyshchenko wrote:

From: Oleksandr Tyshchenko <[email protected]>

Reserving a free context is both quicker and more likely to fail (due to limited hardware resources) than setting up a pagetable. What is more the pagetable init/cleanup code could require the context to be set up.

CC Robin Murphy <[email protected]>
CC Laurent Pinchart <laurent.pinchart-ryLnwIuWjnjg/[email protected]>
CC Joerg Roedel <[email protected]>
Quoted 83 lines

---
This patch fixes a bug during rollback logic:
In ipmmu_domain_init_context() we are trying to find an unused context and if operation fails we will call free_io_pgtable_ops(), but "domain->context_id" hasn't been initialized yet (likely it is 0 because of kzalloc). Having the following call stack:
free_io_pgtable_ops() -> io_pgtable_tlb_flush_all() -> ipmmu_tlb_flush_all() -> ipmmu_tlb_invalidate()
we will get a mistaken cache flush for a context pointed by uninitialized "domain->context_id".

v1 here:
https://lists.linuxfoundation.org/pipermail/iommu/2017-August/023857.html

---
 drivers/iommu/ipmmu-vmsa.c | 42 +++++++++++++++++++++---------------------
 1 file changed, 21 insertions(+), 21 deletions(-)
drivers/iommu/ipmmu-vmsa.c +13 -0
index 2a38aa1..382f387 100644
@@ -341,6 +341,19 @@ static int ipmmu_domain_allocate_context(struct
 ipmmu_vmsa_device *mmu, return ret;
 }
 
+static void ipmmu_domain_free_context(struct ipmmu_vmsa_device *mmu,
+                                   unsigned int context_id)
+{
+     unsigned long flags;
+
+     spin_lock_irqsave(&mmu->lock, flags);
+
+     clear_bit(context_id, mmu->ctx);
+     mmu->domains[context_id] = NULL;
+
+     spin_unlock_irqrestore(&mmu->lock, flags);
+}
+
 static int ipmmu_domain_init_context(struct ipmmu_vmsa_domain *domain)
 {
      u64 ttbr;
@@ -370,22 +383,22 @@ static int ipmmu_domain_init_context(struct

ipmmu_vmsa_domain *domain) */

      domain->cfg.iommu_dev = domain->mmu->dev;
drivers/iommu/ipmmu-vmsa.c +8 -14
-     domain->iop = alloc_io_pgtable_ops(ARM_32_LPAE_S1, &domain->cfg,
-                                        domain);
-     if (!domain->iop)
-             return -EINVAL;
-
      /*
       * Find an unused context.
       */
      ret = ipmmu_domain_allocate_context(domain->mmu, domain);
-     if (ret == IPMMU_CTX_MAX) {
-             free_io_pgtable_ops(domain->iop);
+     if (ret == IPMMU_CTX_MAX)
              return -EBUSY;
-     }
 
      domain->context_id = ret;
 
+     domain->iop = alloc_io_pgtable_ops(ARM_32_LPAE_S1, &domain->cfg,
+                                        domain);
+     if (!domain->iop) {
+             ipmmu_domain_free_context(domain->mmu, domain->context_id);
+             return -EINVAL;
+     }
+
      /* TTBR0 */
      ttbr = domain->cfg.arm_lpae_s1_cfg.ttbr[0];
      ipmmu_ctx_write(domain, IMTTLBR0, ttbr);
@@ -426,19 +439,6 @@ static int ipmmu_domain_init_context(struct
 ipmmu_vmsa_domain *domain) return 0;
 }
 
-static void ipmmu_domain_free_context(struct ipmmu_vmsa_device *mmu,
-                                   unsigned int context_id)
-{
-     unsigned long flags;
-
-     spin_lock_irqsave(&mmu->lock, flags);

drivers/iommu/ipmmu-vmsa.c static int ipmmu_domain_init_context(struct · line 444

Quoted 10 lines
drivers/iommu/ipmmu-vmsa.c +0 -7
-
-     clear_bit(context_id, mmu->ctx);
-     mmu->domains[context_id] = NULL;
-
-     spin_unlock_irqrestore(&mmu->lock, flags);
-}
-
 static void ipmmu_domain_destroy_context(struct ipmmu_vmsa_domain *domain)
 {
      /*

--
Regards,

Quoted 2 lines

Laurent Pinchart

Thank you for the review.
Shall I resend patch with added R-bs?

Signature · 3 lines
Regards,

Oleksandr Tyshchenko

A message that is not in the archive (sent off-list or not yet received): CAPD2p-m1guANCo+y3AvmFKK=RfPf1iFRj65bh2+gwxBOAauQvQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

#8 lore raw
Quoted 2 lines · Oleksandr Tyshchenko

Thank you for the review.
Shall I resend patch with added R-bs?

No, I add them when applying the patch.

Thanks,

Joerg

Applied
#9 lore raw
In reply to a message not in the archive
Quoted 11 lines · Oleksandr Tyshchenko

From: Oleksandr Tyshchenko <[email protected]>

Reserving a free context is both quicker and more likely to fail (due to limited hardware resources) than setting up a pagetable. What is more the pagetable init/cleanup code could require the context to be set up.

CC Robin Murphy <[email protected]>
CC Laurent Pinchart <laurent.pinchart-ryLnwIuWjnjg/[email protected]>
CC Joerg Roedel <[email protected]>

Applied, thanks.