Skip to contentSkip to conversation

iommu/iova: put free_iova_mem() outside of spinlock iova_rbtree_lock

PATCH Superseded chenxiang 2 participants 5 messages Apr 15, 2021 – Apr 16, 2021

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

chenxiang

author
#0 lore raw

PATCH v2 1/1 iommu/iova: put free_iova_mem() outside of spinlock iova_rbtree_lock

From: Xiang Chen <[email protected]>

It is not necessary to put free_iova_mem() inside of spinlock/unlock iova_rbtree_lock which only leads to more completion for the spinlock. It has a small promote on the performance after the change. And also rename private_free_iova() as remove_iova() because the function will not free iova after that change.

---
 drivers/iommu/iova.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
drivers/iommu/iova.c +7 -6
index b7ecd5b..c10af23 100644
@@ -412,12 +412,11 @@ private_find_iova(struct iova_domain *iovad, unsigned long pfn)
 	return NULL;
 }
 
-static void private_free_iova(struct iova_domain *iovad, struct iova *iova)
+static void remove_iova(struct iova_domain *iovad, struct iova *iova)
 {
 	assert_spin_locked(&iovad->iova_rbtree_lock);
 	__cached_rbnode_delete_update(iovad, iova);
 	rb_erase(&iova->node, &iovad->rbroot);
-	free_iova_mem(iova);
 }
 
 /**
@@ -452,8 +451,9 @@ __free_iova(struct iova_domain *iovad, struct iova *iova)
 	unsigned long flags;
 
 	spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
-	private_free_iova(iovad, iova);
+	remove_iova(iovad, iova);
 	spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
+	free_iova_mem(iova);
 }
 EXPORT_SYMBOL_GPL(__free_iova);
 
@@ -473,9 +473,9 @@ free_iova(struct iova_domain *iovad, unsigned long pfn)
 	spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
 	iova = private_find_iova(iovad, pfn);
 	if (iova)
-		private_free_iova(iovad, iova);
+		remove_iova(iovad, iova);
 	spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
-
+	free_iova_mem(iova);
 }
 EXPORT_SYMBOL_GPL(free_iova);
 
@@ -825,7 +825,8 @@ iova_magazine_free_pfns(struct iova_magazine *mag, struct iova_domain *iovad)
 		if (WARN_ON(!iova))
 			continue;
 
-		private_free_iova(iovad, iova);
+		remove_iova(iovad, iova);
+		free_iova_mem(iova);
 	}
 
 	spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
Signature · 6 lines
2.8.1

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
#1 lore raw
Quoted 43 lines · chenxiang

From: Xiang Chen <[email protected]>

It is not necessary to put free_iova_mem() inside of spinlock/unlock iova_rbtree_lock which only leads to more completion for the spinlock. It has a small promote on the performance after the change. And also rename private_free_iova() as remove_iova() because the function will not free iova after that change.

---
  drivers/iommu/iova.c | 13 +++++++------
  1 file changed, 7 insertions(+), 6 deletions(-)
drivers/iommu/iova.c +3 -3
index b7ecd5b..c10af23 100644
@@ -412,12 +412,11 @@ private_find_iova(struct iova_domain *iovad, unsigned long pfn)
  	return NULL;
  }
 
-static void private_free_iova(struct iova_domain *iovad, struct iova *iova)
+static void remove_iova(struct iova_domain *iovad, struct iova *iova)
  {
  	assert_spin_locked(&iovad->iova_rbtree_lock);
  	__cached_rbnode_delete_update(iovad, iova);
  	rb_erase(&iova->node, &iovad->rbroot);
-	free_iova_mem(iova);
  }
 
  /**
@@ -452,8 +451,9 @@ __free_iova(struct iova_domain *iovad, struct iova *iova)
  	unsigned long flags;
 
  	spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
-	private_free_iova(iovad, iova);
+	remove_iova(iovad, iova);
  	spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
+	free_iova_mem(iova);
  }
  EXPORT_SYMBOL_GPL(__free_iova);
 

drivers/iommu/iova.c free_iova(struct iova_domain *iovad, unsigned long pfn) · line 478

Quoted 8 lines
drivers/iommu/iova.c +2 -2
@@ -473,9 +473,9 @@ free_iova(struct iova_domain *iovad, unsigned long pfn)
  	spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
  	iova = private_find_iova(iovad, pfn);
  	if (iova)
-		private_free_iova(iovad, iova);
+		remove_iova(iovad, iova);
  	spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
-
+	free_iova_mem(iova);

you need to make this:

	if (iova)
		free_iova_mem(iova);

as free_iova_mem(iova) dereferences iova:

if (iova->pfn_lo != IOVA_ANCHOR)
kmem_cache_free(iova_cache, iova)

So if iova were NULL, we crash.

Or you can make free_iova_mem() NULL safe.

Quoted 5 lines
drivers/iommu/iova.c +0 -0
  }
  EXPORT_SYMBOL_GPL(free_iova);
 

drivers/iommu/iova.c iova_magazine_free_pfns(struct iova_magazine *mag, struct iova_domain *iovad) · line 832

Quoted 9 lines
drivers/iommu/iova.c +2 -1
@@ -825,7 +825,8 @@ iova_magazine_free_pfns(struct iova_magazine *mag, struct iova_domain *iovad)
  		if (WARN_ON(!iova))
  			continue;
 
-		private_free_iova(iovad, iova);
+		remove_iova(iovad, iova);
+		free_iova_mem(iova);
  	}
 
  	spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#2 lore raw
Quoted 57 lines · John Garry
Quoted 46 lines · chenxiang

From: Xiang Chen <[email protected]>

It is not necessary to put free_iova_mem() inside of spinlock/unlock iova_rbtree_lock which only leads to more completion for the spinlock. It has a small promote on the performance after the change. And also rename private_free_iova() as remove_iova() because the function will not free iova after that change.

---
  drivers/iommu/iova.c | 13 +++++++------
  1 file changed, 7 insertions(+), 6 deletions(-)
drivers/iommu/iova.c +3 -2
index b7ecd5b..c10af23 100644
@@ -412,12 +412,11 @@ private_find_iova(struct iova_domain *iovad, 
 unsigned long pfn)
      return NULL;
  }
  -static void private_free_iova(struct iova_domain *iovad, struct 
 iova *iova)
+static void remove_iova(struct iova_domain *iovad, struct iova *iova)
  {
      assert_spin_locked(&iovad->iova_rbtree_lock);
      __cached_rbnode_delete_update(iovad, iova);
      rb_erase(&iova->node, &iovad->rbroot);
-    free_iova_mem(iova);
  }
    /**
@@ -452,8 +451,9 @@ __free_iova(struct iova_domain *iovad, struct 
 iova *iova)
      unsigned long flags;
        spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
-    private_free_iova(iovad, iova);
+    remove_iova(iovad, iova);
      spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
+    free_iova_mem(iova);
  }
  EXPORT_SYMBOL_GPL(__free_iova);
  @@ -473,9 +473,9 @@ free_iova(struct iova_domain *iovad, unsigned 

long pfn)

      spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
      iova = private_find_iova(iovad, pfn);
      if (iova)

drivers/iommu/iova.c free_iova(struct iova_domain *iovad, unsigned long pfn) · line 478

Quoted 5 lines
drivers/iommu/iova.c +2 -2
-        private_free_iova(iovad, iova);
+        remove_iova(iovad, iova);
      spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
-
+    free_iova_mem(iova);

you need to make this:

    if (iova)
        free_iova_mem(iova);
Quoted 8 lines

as free_iova_mem(iova) dereferences iova:

if (iova->pfn_lo != IOVA_ANCHOR)
kmem_cache_free(iova_cache, iova)

So if iova were NULL, we crash.

Or you can make free_iova_mem() NULL safe.

Right, it has the issue. What about changing it as below?

@@ -472,10 +472,13 @@ free_iova(struct iova_domain *iovad, unsigned long pfn)

        spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
        iova = private_find_iova(iovad, pfn);

- if (iova)
- private_free_iova(iovad, iova);
+ if (!iova) {
+ spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
+ return;
+ }

+       remove_iova(iovad, iova);
        spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);

-
+ free_iova_mem(iova);
}
EXPORT_SYMBOL_GPL(free_iova);

Quoted 15 lines

drivers/iommu/iova.c iova_magazine_free_pfns(struct iova_magazine *mag, struct iova_domain *iovad) · line 832

Quoted 13 lines

}
EXPORT_SYMBOL_GPL(free_iova);
@@ -825,7 +825,8 @@ iova_magazine_free_pfns(struct iova_magazine
*mag, struct iova_domain *iovad)

          if (WARN_ON(!iova))
              continue;

- private_free_iova(iovad, iova);

drivers/iommu/iova.c +2 -0
+        remove_iova(iovad, iova);
+        free_iova_mem(iova);
      }
        spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
Quoted 2 lines

.

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#3 lore raw
Quoted 23 lines · chenxiang (M)
Quoted 5 lines

you need to make this:
    if (iova)
        free_iova_mem(iova);

Quoted 8 lines

as free_iova_mem(iova) dereferences iova:

if (iova->pfn_lo != IOVA_ANCHOR)
    kmem_cache_free(iova_cache, iova)

So if iova were NULL, we crash.

Or you can make free_iova_mem() NULL safe.

Right, it has the issue. What about changing it as below?

drivers/iommu/iova.c +0 -2
@@ -472,10 +472,13 @@ free_iova(struct iova_domain *iovad, unsigned long 
 pfn)
 
         spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
         iova = private_find_iova(iovad, pfn);
-       if (iova)
-               private_free_iova(iovad, iova);

drivers/iommu/iova.c free_iova(struct iova_domain *iovad, unsigned long · line 484

Quoted 10 lines
drivers/iommu/iova.c +6 -1
+       if (!iova) {
+ spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
+               return;
+       }
+       remove_iova(iovad, iova);
         spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
-
+       free_iova_mem(iova);
  }
  EXPORT_SYMBOL_GPL(free_iova);

I don't feel too strongly about how it's done.

Please note that kmem_cache_free() is not NULL safe. So the NULL check could be added in free_iova_mem(), but we prob don't want silent free_iova_mem(NULL) calls, so I would stick with changing free_iova().

Thanks,
John
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#4 lore raw
Quoted 37 lines · John Garry
Quoted 23 lines · chenxiang (M)
Quoted 5 lines

you need to make this:

    if (iova)
        free_iova_mem(iova);
Quoted 8 lines

as free_iova_mem(iova) dereferences iova:

if (iova->pfn_lo != IOVA_ANCHOR)
kmem_cache_free(iova_cache, iova)

So if iova were NULL, we crash.

Or you can make free_iova_mem() NULL safe.

Right, it has the issue. What about changing it as below?

drivers/iommu/iova.c +0 -2
@@ -472,10 +472,13 @@ free_iova(struct iova_domain *iovad, unsigned 
 long pfn)
 
         spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
         iova = private_find_iova(iovad, pfn);
-       if (iova)
-               private_free_iova(iovad, iova);

drivers/iommu/iova.c free_iova(struct iova_domain *iovad, unsigned · line 484

Quoted 10 lines
drivers/iommu/iova.c +6 -1
+       if (!iova) {
+ spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
+               return;
+       }
+       remove_iova(iovad, iova);
         spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
-
+       free_iova_mem(iova);
  }
  EXPORT_SYMBOL_GPL(free_iova);

I don't feel too strongly about how it's done.

Quoted 1 line

Please note that kmem_cache_free() is not NULL safe. So the NULL check could be added in free_iova_mem(), but we prob don't want silent free_iova_mem(NULL) calls, so I would stick with changing free_iova().

so I would stick with changing free_iova()
----- Do you mean free_iova_mem()?

But i think there is a check (judge iova is NULL) before free_iova_mem() for following scenarios, and it is not necessary to change in funciton free_iova_mem():
1) iova_magazine_free_pfns()
2) alloc_iova()
3) free_iova()
4) __free_iova(): Those functions which call __free_iova() have a check

Only for function put_iova_doamin(), it doesn't check iova, but i think iova in rbtree should not be NULL.

Quoted 6 lines

Thanks,
John

.

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu