Skip to contentSkip to conversation

iommu/ipmmu-vmsa: Set context_id to non-existent value if allocation failed

PATCH Applied Oleksandr Tyshchenko 4 participants 11 messages Aug 21, 2017 – Aug 23, 2017 5 messages not in the archive IOMMU

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iommu/ipmmu-vmsa: Set context_id to non-existent value if allocation failed

From: Oleksandr Tyshchenko <[email protected]>

In ipmmu_domain_init_context() we are trying to allocate context and if allocation 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".

So, set context_id to non-existent value (IPMMU_CTX_MAX) before calling free_io_pgtable_ops() and check it for a valid value (< IPMMU_CTX_MAX) before calling ipmmu_tlb_invalidate().

---
 drivers/iommu/ipmmu-vmsa.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/iommu/ipmmu-vmsa.c +4 -0
index 2a38aa1..5b226c0 100644
@@ -303,6 +303,9 @@ static void ipmmu_tlb_flush_all(void *cookie)
 {
 	struct ipmmu_vmsa_domain *domain = cookie;
 
+	if (domain->context_id >= IPMMU_CTX_MAX)
+		return;
+
 	ipmmu_tlb_invalidate(domain);
 }
 
@@ -380,6 +383,7 @@ static int ipmmu_domain_init_context(struct ipmmu_vmsa_domain *domain)
 	 */
 	ret = ipmmu_domain_allocate_context(domain->mmu, domain);
 	if (ret == IPMMU_CTX_MAX) {
+		domain->context_id = IPMMU_CTX_MAX;
 		free_io_pgtable_ops(domain->iop);
 		return -EBUSY;
 	}
Signature · 1 line
2.7.4

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

Applied
#2 lore raw
Quoted 11 lines · Oleksandr Tyshchenko

From: Oleksandr Tyshchenko <[email protected]>

In ipmmu_domain_init_context() we are trying to allocate context and if allocation 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".

Quoted 8 lines

So, set context_id to non-existent value (IPMMU_CTX_MAX) before calling free_io_pgtable_ops() and check it for a valid value (< IPMMU_CTX_MAX) before calling ipmmu_tlb_invalidate().

---
 drivers/iommu/ipmmu-vmsa.c | 4 ++++
 1 file changed, 4 insertions(+)

Applied, thanks.

A message that is not in the archive (sent off-list or not yet received): 20170822142430.ft6yhl4etx2hhn7h-zLv9SwRftAIdnm+yROfE0A@public.gmane.org

#4 lore raw

Hi,

Quoted 16 lines · Joerg Roedel
Quoted 15 lines · Oleksandr Tyshchenko

From: Oleksandr Tyshchenko <[email protected]>

In ipmmu_domain_init_context() we are trying to allocate context and if allocation 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".

So, set context_id to non-existent value (IPMMU_CTX_MAX) before calling free_io_pgtable_ops() and check it for a valid value (< IPMMU_CTX_MAX) before calling ipmmu_tlb_invalidate().

Quoted 6 lines
Quoted 4 lines
---
 drivers/iommu/ipmmu-vmsa.c | 4 ++++
 1 file changed, 4 insertions(+)

Applied, thanks.

Thank you.

Quoted 1 line
Signature · 3 lines
Regards,

Oleksandr Tyshchenko
#5 lore raw
In reply to a message not in the archive

Hi Joerg,

Quoted 16 lines · On Tuesday, 22 August 2017 17:24:30 EEST Joerg Roedel wrote:
Quoted 15 lines · Oleksandr Tyshchenko

From: Oleksandr Tyshchenko <[email protected]>

In ipmmu_domain_init_context() we are trying to allocate context and if allocation 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".

So, set context_id to non-existent value (IPMMU_CTX_MAX) before calling free_io_pgtable_ops() and check it for a valid value (< IPMMU_CTX_MAX) before calling ipmmu_tlb_invalidate().

Quoted 7 lines
Quoted 5 lines

---

 drivers/iommu/ipmmu-vmsa.c | 4 ++++
 1 file changed, 4 insertions(+)

Applied, thanks.

It would be nice to give reviewers a week before applying a patch, especially when it has no review at all.

Signature · 3 lines
Regards,

Laurent Pinchart
#6 lore raw

Hi Laurent,

Quoted 2 lines · Laurent Pinchart

It would be nice to give reviewers a week before applying a patch, especially when it has no review at all.

It's not pushed yet and it looked simple enough during my review that I felt ok to take it. If you disagree with it, I can easily remove it again. Let me know what you prefer.

Regards,

Joerg

#7 lore raw
In reply to a message not in the archive

Hi Oleksandr,

Thank you for the patch.

Quoted 36 lines · On Monday, 21 August 2017 15:40:41 EEST Oleksandr Tyshchenko wrote:

From: Oleksandr Tyshchenko <[email protected]>

In ipmmu_domain_init_context() we are trying to allocate context and if allocation 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".

So, set context_id to non-existent value (IPMMU_CTX_MAX) before calling free_io_pgtable_ops() and check it for a valid value (< IPMMU_CTX_MAX) before calling ipmmu_tlb_invalidate().

---
 drivers/iommu/ipmmu-vmsa.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/iommu/ipmmu-vmsa.c +3 -0
index 2a38aa1..5b226c0 100644
@@ -303,6 +303,9 @@ static void ipmmu_tlb_flush_all(void *cookie)
 {
 	struct ipmmu_vmsa_domain *domain = cookie;
 
+	if (domain->context_id >= IPMMU_CTX_MAX)
+		return;
+
 	ipmmu_tlb_invalidate(domain);
 }
 
@@ -380,6 +383,7 @@ static int ipmmu_domain_init_context(struct

ipmmu_vmsa_domain *domain) */

drivers/iommu/ipmmu-vmsa.c static int ipmmu_domain_init_context(struct ipmmu_vmsa_domain *domain) · line 386

Quoted 3 lines
drivers/iommu/ipmmu-vmsa.c +1 -0
 	ret = ipmmu_domain_allocate_context(domain->mmu, domain);
 	if (ret == IPMMU_CTX_MAX) {
+		domain->context_id = IPMMU_CTX_MAX;

Wouldn't it make more sense to allocate the pgtable ops after initializing the context (moving the ipmmu_domain_allocate_context() call to the very end of the function) ? That way we would be less dependent on changes to pgtable ops init/cleanup code that could require the context to be set up.

Quoted 3 lines
drivers/iommu/ipmmu-vmsa.c +0 -0
 		free_io_pgtable_ops(domain->iop);
 		return -EBUSY;
 	}
Signature · 3 lines
Regards,

Laurent Pinchart
#8 lore raw

Hi, Laurent.

Quoted 39 lines · Laurent Pinchart

Hi Oleksandr,

Thank you for the patch.

Quoted 34 lines · On Monday, 21 August 2017 15:40:41 EEST Oleksandr Tyshchenko wrote:

From: Oleksandr Tyshchenko <[email protected]>

In ipmmu_domain_init_context() we are trying to allocate context and if allocation 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".

So, set context_id to non-existent value (IPMMU_CTX_MAX) before calling free_io_pgtable_ops() and check it for a valid value (< IPMMU_CTX_MAX) before calling ipmmu_tlb_invalidate().

---
 drivers/iommu/ipmmu-vmsa.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/iommu/ipmmu-vmsa.c +3 -0
index 2a38aa1..5b226c0 100644
@@ -303,6 +303,9 @@ static void ipmmu_tlb_flush_all(void *cookie)
 {
      struct ipmmu_vmsa_domain *domain = cookie;
 
+     if (domain->context_id >= IPMMU_CTX_MAX)
+             return;
+
      ipmmu_tlb_invalidate(domain);
 }
 
Quoted 10 lines
Quoted 5 lines
drivers/iommu/ipmmu-vmsa.c +0 -0
@@ -380,6 +383,7 @@ static int ipmmu_domain_init_context(struct

ipmmu_vmsa_domain *domain) */

      ret = ipmmu_domain_allocate_context(domain->mmu, domain);
      if (ret == IPMMU_CTX_MAX) {

+ domain->context_id = IPMMU_CTX_MAX;

Wouldn't it make more sense to allocate the pgtable ops after initializing the context (moving the ipmmu_domain_allocate_context() call to the very end of the function) ? That way we would be less dependent on changes to pgtable ops init/cleanup code that could require the context to be set up.

Why not. But, not sure about the very end of the function. Since for writing some HW registers down the function (IMTTLBR0/IMTTUBR0, IMMAIR0)
we need to have what pgtable ops sets up for us (ttbr, mair). What about to just swap alloc_io_pgtable_ops() and ipmmu_domain_allocate_context()?

...

drivers/iommu/ipmmu-vmsa.c +0 -0
index 2a38aa1..90af1c7 100644
@@ -370,22 +370,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);

...

Quoted 11 lines
Quoted 4 lines
              free_io_pgtable_ops(domain->iop);
              return -EBUSY;
      }

--
Regards,

Laurent Pinchart

Signature · 3 lines
Regards,

Oleksandr Tyshchenko

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

#10 lore raw
Quoted 54 lines · Oleksandr Tyshchenko

Hi, Laurent.

Quoted 39 lines · Laurent Pinchart

Hi Oleksandr,

Thank you for the patch.

Quoted 34 lines · On Monday, 21 August 2017 15:40:41 EEST Oleksandr Tyshchenko wrote:

From: Oleksandr Tyshchenko <[email protected]>

In ipmmu_domain_init_context() we are trying to allocate context and if allocation 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".

So, set context_id to non-existent value (IPMMU_CTX_MAX) before calling free_io_pgtable_ops() and check it for a valid value (< IPMMU_CTX_MAX) before calling ipmmu_tlb_invalidate().

---
 drivers/iommu/ipmmu-vmsa.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/iommu/ipmmu-vmsa.c +3 -0
index 2a38aa1..5b226c0 100644
@@ -303,6 +303,9 @@ static void ipmmu_tlb_flush_all(void *cookie)
 {
      struct ipmmu_vmsa_domain *domain = cookie;
 
+     if (domain->context_id >= IPMMU_CTX_MAX)
+             return;
+
      ipmmu_tlb_invalidate(domain);
 }
 
Quoted 10 lines
Quoted 5 lines
drivers/iommu/ipmmu-vmsa.c +0 -0
@@ -380,6 +383,7 @@ static int ipmmu_domain_init_context(struct

ipmmu_vmsa_domain *domain) */

      ret = ipmmu_domain_allocate_context(domain->mmu, domain);
      if (ret == IPMMU_CTX_MAX) {

+ domain->context_id = IPMMU_CTX_MAX;

Wouldn't it make more sense to allocate the pgtable ops after initializing the context (moving the ipmmu_domain_allocate_context() call to the very end of the function) ? That way we would be less dependent on changes to pgtable ops init/cleanup code that could require the context to be set up.

Quoted 6 lines

Why not. But, not sure about the very end of the function. Since for writing some HW registers down the function (IMTTLBR0/IMTTUBR0, IMMAIR0)
we need to have what pgtable ops sets up for us (ttbr, mair). What about to just swap alloc_io_pgtable_ops() and ipmmu_domain_allocate_context()?

This looks a lot more reasonable - reserving a free context is both quicker and more likely to fail (due to limited hardware resources) than setting up a pagetable, so it makes a lot of sense to do that before anything else.

Robin.

Quoted 53 lines

...

drivers/iommu/ipmmu-vmsa.c +8 -8
index 2a38aa1..90af1c7 100644
@@ -370,22 +370,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);

...

Quoted 11 lines
Quoted 4 lines
              free_io_pgtable_ops(domain->iop);
              return -EBUSY;
      }

--
Regards,

Laurent Pinchart

A message that is not in the archive (sent off-list or not yet received): 050cf91d-1edb-a791-fdd2-5867a1504cf5-5wv7dgnIgG8@public.gmane.org

#12 lore raw

Hi, Robin

Quoted 62 lines · Robin Murphy
Quoted 54 lines · Oleksandr Tyshchenko

Hi, Laurent.

Quoted 39 lines · Laurent Pinchart

Hi Oleksandr,

Thank you for the patch.

Quoted 34 lines · On Monday, 21 August 2017 15:40:41 EEST Oleksandr Tyshchenko wrote:

From: Oleksandr Tyshchenko <[email protected]>

In ipmmu_domain_init_context() we are trying to allocate context and if allocation 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".

So, set context_id to non-existent value (IPMMU_CTX_MAX) before calling free_io_pgtable_ops() and check it for a valid value (< IPMMU_CTX_MAX) before calling ipmmu_tlb_invalidate().

---
 drivers/iommu/ipmmu-vmsa.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/iommu/ipmmu-vmsa.c +3 -0
index 2a38aa1..5b226c0 100644
@@ -303,6 +303,9 @@ static void ipmmu_tlb_flush_all(void *cookie)
 {
      struct ipmmu_vmsa_domain *domain = cookie;
 
+     if (domain->context_id >= IPMMU_CTX_MAX)
+             return;
+
      ipmmu_tlb_invalidate(domain);
 }
 
Quoted 10 lines
Quoted 5 lines
drivers/iommu/ipmmu-vmsa.c +0 -0
@@ -380,6 +383,7 @@ static int ipmmu_domain_init_context(struct

ipmmu_vmsa_domain *domain) */

      ret = ipmmu_domain_allocate_context(domain->mmu, domain);
      if (ret == IPMMU_CTX_MAX) {

+ domain->context_id = IPMMU_CTX_MAX;

Wouldn't it make more sense to allocate the pgtable ops after initializing the context (moving the ipmmu_domain_allocate_context() call to the very end of the function) ? That way we would be less dependent on changes to pgtable ops init/cleanup code that could require the context to be set up.

Quoted 6 lines

Why not. But, not sure about the very end of the function. Since for writing some HW registers down the function (IMTTLBR0/IMTTUBR0, IMMAIR0)
we need to have what pgtable ops sets up for us (ttbr, mair). What about to just swap alloc_io_pgtable_ops() and ipmmu_domain_allocate_context()?

Quoted 4 lines

This looks a lot more reasonable - reserving a free context is both quicker and more likely to fail (due to limited hardware resources) than setting up a pagetable, so it makes a lot of sense to do that before anything else.

Agree.

Quoted 57 lines

Robin.

Quoted 53 lines

...

drivers/iommu/ipmmu-vmsa.c +8 -8
index 2a38aa1..90af1c7 100644
@@ -370,22 +370,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);

...

Quoted 11 lines
Quoted 4 lines
              free_io_pgtable_ops(domain->iop);
              return -EBUSY;
      }

--
Regards,

Laurent Pinchart

Signature · 3 lines
Regards,

Oleksandr Tyshchenko

A message that is not in the archive (sent off-list or not yet received): CAPD2p-n8M5eY1XHaW-3b-opecXo0ut6fSRXcPssrJ-TxsvkEtA-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

#14 lore raw

Hi Oleksandr,

Quoted 58 lines · On Wednesday, 23 August 2017 14:58:47 EEST Oleksandr Tyshchenko wrote:
Quoted 57 lines · Robin Murphy
Quoted 55 lines · Oleksandr Tyshchenko
Quoted 36 lines · Laurent Pinchart
Quoted 35 lines · On Monday, 21 August 2017 15:40:41 EEST Oleksandr Tyshchenko wrote:

From: Oleksandr Tyshchenko <[email protected]>

In ipmmu_domain_init_context() we are trying to allocate context and if allocation 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".

So, set context_id to non-existent value (IPMMU_CTX_MAX) before calling free_io_pgtable_ops() and check it for a valid value (< IPMMU_CTX_MAX) before calling ipmmu_tlb_invalidate().

---

 drivers/iommu/ipmmu-vmsa.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/iommu/ipmmu-vmsa.c +3 -0
index 2a38aa1..5b226c0 100644
@@ -303,6 +303,9 @@ static void ipmmu_tlb_flush_all(void *cookie)
 {
      struct ipmmu_vmsa_domain *domain = cookie;
 
+     if (domain->context_id >= IPMMU_CTX_MAX)
+             return;
+
      ipmmu_tlb_invalidate(domain);
 }
 
Quoted 12 lines

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

Quoted 6 lines
drivers/iommu/ipmmu-vmsa.c +1 -0
@@ -380,6 +383,7 @@ static int ipmmu_domain_init_context(struct
 ipmmu_vmsa_domain *domain)
 		*/
      ret = ipmmu_domain_allocate_context(domain->mmu, domain);
      if (ret == IPMMU_CTX_MAX) {
+             domain->context_id = IPMMU_CTX_MAX;

Wouldn't it make more sense to allocate the pgtable ops after initializing the context (moving the ipmmu_domain_allocate_context() call to the very end of the function) ? That way we would be less dependent on changes to pgtable ops init/cleanup code that could require the context to be set up.

Why not. But, not sure about the very end of the function. Since for writing some HW registers down the function (IMTTLBR0/IMTTUBR0, IMMAIR0) we need to have what pgtable ops sets up for us (ttbr, mair). What about to just swap alloc_io_pgtable_ops() and ipmmu_domain_allocate_context()?

Quoted 6 lines
Quoted 4 lines

This looks a lot more reasonable - reserving a free context is both quicker and more likely to fail (due to limited hardware resources) than setting up a pagetable, so it makes a lot of sense to do that before anything else.

Agree.

That looks good to me too. In general I prefer initializing everything needed by the error path before calling anything that could trigger that error path, instead of initializing variables to magic values that mean part of the cleanup should be skipped.

Will you send a v2 ?

Signature · 3 lines
Regards,

Laurent Pinchart
#15 lore raw

Hi, Laurent

Quoted 66 lines · Laurent Pinchart

Hi Oleksandr,

Quoted 63 lines · On Wednesday, 23 August 2017 14:58:47 EEST Oleksandr Tyshchenko wrote:
Quoted 61 lines · Robin Murphy
Quoted 50 lines · Oleksandr Tyshchenko
Quoted 36 lines · Laurent Pinchart
Quoted 35 lines · On Monday, 21 August 2017 15:40:41 EEST Oleksandr Tyshchenko wrote:

From: Oleksandr Tyshchenko <[email protected]>

In ipmmu_domain_init_context() we are trying to allocate context and if allocation 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".

So, set context_id to non-existent value (IPMMU_CTX_MAX) before calling free_io_pgtable_ops() and check it for a valid value (< IPMMU_CTX_MAX) before calling ipmmu_tlb_invalidate().

---

 drivers/iommu/ipmmu-vmsa.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/iommu/ipmmu-vmsa.c +3 -0
index 2a38aa1..5b226c0 100644
@@ -303,6 +303,9 @@ static void ipmmu_tlb_flush_all(void *cookie)
 {
      struct ipmmu_vmsa_domain *domain = cookie;
 
+     if (domain->context_id >= IPMMU_CTX_MAX)
+             return;
+
      ipmmu_tlb_invalidate(domain);
 }
 
Quoted 12 lines

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

Quoted 6 lines
drivers/iommu/ipmmu-vmsa.c +1 -0
@@ -380,6 +383,7 @@ static int ipmmu_domain_init_context(struct
 ipmmu_vmsa_domain *domain)
                 */
      ret = ipmmu_domain_allocate_context(domain->mmu, domain);
      if (ret == IPMMU_CTX_MAX) {
+             domain->context_id = IPMMU_CTX_MAX;

Wouldn't it make more sense to allocate the pgtable ops after initializing the context (moving the ipmmu_domain_allocate_context() call to the very end of the function) ? That way we would be less dependent on changes to pgtable ops init/cleanup code that could require the context to be set up.

Quoted 5 lines

Why not. But, not sure about the very end of the function. Since for writing some HW registers down the function (IMTTLBR0/IMTTUBR0, IMMAIR0) we need to have what pgtable ops sets up for us (ttbr, mair). What about to just swap alloc_io_pgtable_ops() and ipmmu_domain_allocate_context()?

This looks a lot more reasonable - reserving a free context is both quicker and more likely to fail (due to limited hardware resources) than setting up a pagetable, so it makes a lot of sense to do that before anything else.

Quoted 6 lines
Quoted 1 line

Agree.

That looks good to me too. In general I prefer initializing everything needed by the error path before calling anything that could trigger that error path, instead of initializing variables to magic values that mean part of the cleanup should be skipped.

Make sense.

Quoted 2 lines

Will you send a v2 ?

Yes.

Quoted 6 lines

--
Regards,

Laurent Pinchart

Signature · 3 lines
Regards,

Oleksandr Tyshchenko