Skip to contentSkip to conversation

iommu/io-pgtable-arm: Check for leaf entry right after finding it

RFC PATCH New Oleksandr Tyshchenko 2 participants 5 messages Feb 9, 2017 – Feb 13, 2017

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 iommu/io-pgtable-arm: Check for leaf entry right after finding it

From: Oleksandr Tyshchenko <[email protected]>

Do a check for already installed leaf entry at the current level before performing any actions when trying to map.

This check is already present in arm_lpae_init_pte(), i.e. before installing new leaf entry at the current level if conditions to do so are met (size == block_size).

But, this might be insufficient in case when we have already installed block mapping at this level and it is not time to install new leaf entry (size != block_size).
In that case we continue walking the page table down with wrong pointer to the next level.

So, move check from arm_lpae_init_pte() to __arm_lpae_map() in order to avoid all cases.

---
I hope that following actions can help to catch it:
1. Call iommu_map for a block mapping (e.g. 2M) at some address (e.g. iova 0x80000000 pa 0x80000000).
2. Call iommu_map for a page mapping (4k) at some address from the previous mapped region (e.g. iova 0x80008000 pa 0x90000000).

I understand that after iommu_map should be iommu_unmap, but different scenarios may occur).
---

---
 drivers/iommu/io-pgtable-arm.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
drivers/iommu/io-pgtable-arm.c +8 -5
index f5c90e1..ebdb82f 100644
@@ -272,11 +272,7 @@ static int arm_lpae_init_pte(struct arm_lpae_io_pgtable *data,
 	arm_lpae_iopte pte = prot;
 	struct io_pgtable_cfg *cfg = &data->iop.cfg;
 
-	if (iopte_leaf(*ptep, lvl)) {
-		/* We require an unmap first */
-		WARN_ON(!selftest_running);
-		return -EEXIST;
-	} else if (iopte_type(*ptep, lvl) == ARM_LPAE_PTE_TYPE_TABLE) {
+	if (iopte_type(*ptep, lvl) == ARM_LPAE_PTE_TYPE_TABLE) {
 		/*
 		 * We need to unmap and free the old table before
 		 * overwriting it with a block entry.
@@ -315,6 +311,13 @@ static int __arm_lpae_map(struct arm_lpae_io_pgtable *data, unsigned long iova,
 	/* Find our entry at the current level */
 	ptep += ARM_LPAE_LVL_IDX(iova, lvl, data);
 
+	/* Check for already installed leaf entry */
+	if (iopte_leaf(*ptep, lvl)) {
+		/* We require an unmap first */
+		WARN_ON(!selftest_running);
+		return -EEXIST;
+	}
+
 	/* If we can install a leaf entry at this level, then do so */
 	if (size == block_size && (size & cfg->pgsize_bitmap))
 		return arm_lpae_init_pte(data, iova, paddr, prot, lvl, ptep);
Signature · 1 line
2.7.4
#1 lore raw

RFC PATCH 1/1 iommu/io-pgtable-arm: Check for leaf entry right after finding it

Hi, all.

Any comments?

Quoted 67 lines · Oleksandr Tyshchenko

From: Oleksandr Tyshchenko <[email protected]>

Do a check for already installed leaf entry at the current level before performing any actions when trying to map.

This check is already present in arm_lpae_init_pte(), i.e. before installing new leaf entry at the current level if conditions to do so are met (size == block_size).

But, this might be insufficient in case when we have already installed block mapping at this level and it is not time to install new leaf entry (size != block_size).
In that case we continue walking the page table down with wrong pointer to the next level.

So, move check from arm_lpae_init_pte() to __arm_lpae_map() in order to avoid all cases.

---
I hope that following actions can help to catch it:
1. Call iommu_map for a block mapping (e.g. 2M) at some address (e.g. iova 0x80000000 pa 0x80000000).
2. Call iommu_map for a page mapping (4k) at some address from the previous mapped region (e.g. iova 0x80008000 pa 0x90000000).

I understand that after iommu_map should be iommu_unmap, but different scenarios may occur).
---

---
 drivers/iommu/io-pgtable-arm.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
drivers/iommu/io-pgtable-arm.c +8 -6
index f5c90e1..ebdb82f 100644
@@ -272,11 +272,7 @@ static int arm_lpae_init_pte(struct arm_lpae_io_pgtable *data,
        arm_lpae_iopte pte = prot;
        struct io_pgtable_cfg *cfg = &data->iop.cfg;
 
-       if (iopte_leaf(*ptep, lvl)) {
-               /* We require an unmap first */
-               WARN_ON(!selftest_running);
-               return -EEXIST;
-       } else if (iopte_type(*ptep, lvl) == ARM_LPAE_PTE_TYPE_TABLE) {
+       if (iopte_type(*ptep, lvl) == ARM_LPAE_PTE_TYPE_TABLE) {
                /*
                 * We need to unmap and free the old table before
                 * overwriting it with a block entry.
@@ -315,6 +311,13 @@ static int __arm_lpae_map(struct arm_lpae_io_pgtable *data, unsigned long iova,
        /* Find our entry at the current level */
        ptep += ARM_LPAE_LVL_IDX(iova, lvl, data);
 
+       /* Check for already installed leaf entry */
+       if (iopte_leaf(*ptep, lvl)) {
+               /* We require an unmap first */
+               WARN_ON(!selftest_running);
+               return -EEXIST;
+       }
+
        /* If we can install a leaf entry at this level, then do so */
        if (size == block_size && (size & cfg->pgsize_bitmap))
                return arm_lpae_init_pte(data, iova, paddr, prot, lvl, ptep);
--

2.7.4

Signature · 3 lines
Regards,

Oleksandr Tyshchenko
#2 lore raw

RFC PATCH 1/1 iommu/io-pgtable-arm: Check for leaf entry right after finding it

Quoted 1 line · Oleksandr Tyshchenko

Any comments?

Looks fine to me, but I don't think it's urgent and I already sent my SMMU pull for 4.11. I'll send this as a fix after the merge window.

I suspect we need something similar for io-pgtable-arm-v7s.c, too.

Will

#3 lore raw

RFC PATCH 1/1 iommu/io-pgtable-arm: Check for leaf entry right after finding it

Quoted 5 lines · Will Deacon
Quoted 1 line · Oleksandr Tyshchenko

Any comments?

Looks fine to me, but I don't think it's urgent and I already sent my SMMU pull for 4.11. I'll send this as a fix after the merge window.

OK. Thank you.

Quoted 2 lines

I suspect we need something similar for io-pgtable-arm-v7s.c, too.

Agree. On the whole I will be able to make similar patch for arm-v7s, but I won't be 100% sure
since I don't have any boards where arm-v7s compatible IOMMU installed.

Shall I make patch for arm-v7s too?

Quoted 2 lines

Will

Signature · 3 lines
Regards,

Oleksandr Tyshchenko
#4 lore raw

RFC PATCH 1/1 iommu/io-pgtable-arm: Check for leaf entry right after finding it

Quoted 9 lines · Oleksandr Tyshchenko
Quoted 5 lines · Will Deacon
Quoted 1 line · Oleksandr Tyshchenko

Any comments?

Looks fine to me, but I don't think it's urgent and I already sent my SMMU pull for 4.11. I'll send this as a fix after the merge window.

OK. Thank you.

Quoted 1 line
Quoted 6 lines
Quoted 1 line

I suspect we need something similar for io-pgtable-arm-v7s.c, too.

Agree. On the whole I will be able to make similar patch for arm-v7s, but I won't be 100% sure
since I don't have any boards where arm-v7s compatible IOMMU installed.

Shall I make patch for arm-v7s too?

Yes, please. Robin seems to enjoy using short-descriptor, so he might give it a spin for you if you ask nicely.

Will