[PATCH 2/2] iova32

Chris Wilson chris at chris-wilson.co.uk
Sat Jan 16 14:20:54 UTC 2021


---
 drivers/iommu/iova.c | 202 +++++++++++++++++++++++++------------------
 include/linux/iova.h |   5 +-
 2 files changed, 121 insertions(+), 86 deletions(-)

diff --git a/drivers/iommu/iova.c b/drivers/iommu/iova.c
index 0c43c2b5f040..e59c3e2622aa 100644
--- a/drivers/iommu/iova.c
+++ b/drivers/iommu/iova.c
@@ -40,15 +40,13 @@ init_iova_domain(struct iova_domain *iovad, unsigned long granule,
 
 	spin_lock_init(&iovad->iova_rbtree_lock);
 	iovad->rbroot = RB_ROOT;
-	iovad->cached_node = &iovad->anchor.node;
-	iovad->cached32_node = &iovad->anchor.node;
 	iovad->granule = granule;
 	iovad->start_pfn = start_pfn;
 	iovad->dma_32bit_pfn = 1UL << (32 - iova_shift(iovad));
-	iovad->max32_alloc_size = iovad->dma_32bit_pfn;
 	iovad->flush_cb = NULL;
 	iovad->fq = NULL;
 	iovad->anchor.pfn_lo = iovad->anchor.pfn_hi = IOVA_ANCHOR;
+	INIT_LIST_HEAD(&iovad->anchor.hole);
 	rb_link_node(&iovad->anchor.node, NULL, &iovad->rbroot.rb_node);
 	rb_insert_color(&iovad->anchor.node, &iovad->rbroot);
 	init_iova_rcaches(iovad);
@@ -114,54 +112,19 @@ int init_iova_flush_queue(struct iova_domain *iovad,
 }
 EXPORT_SYMBOL_GPL(init_iova_flush_queue);
 
-static struct rb_node *
-__get_cached_rbnode(struct iova_domain *iovad, unsigned long limit_pfn)
-{
-	if (limit_pfn <= iovad->dma_32bit_pfn)
-		return iovad->cached32_node;
-
-	return iovad->cached_node;
-}
-
-static void
-__cached_rbnode_insert_update(struct iova_domain *iovad, struct iova *new)
-{
-	if (new->pfn_hi < iovad->dma_32bit_pfn)
-		iovad->cached32_node = &new->node;
-	else
-		iovad->cached_node = &new->node;
-}
-
-static void
-__cached_rbnode_delete_update(struct iova_domain *iovad, struct iova *free)
-{
-	struct iova *cached_iova;
-
-	cached_iova = rb_entry(iovad->cached32_node, struct iova, node);
-	if (free == cached_iova ||
-	    (free->pfn_hi < iovad->dma_32bit_pfn &&
-	     free->pfn_lo >= cached_iova->pfn_lo)) {
-		iovad->cached32_node = rb_next(&free->node);
-		iovad->max32_alloc_size = iovad->dma_32bit_pfn;
-	}
-
-	cached_iova = rb_entry(iovad->cached_node, struct iova, node);
-	if (free->pfn_lo >= cached_iova->pfn_lo)
-		iovad->cached_node = rb_next(&free->node);
-}
-
 /* Insert the iova into domain rbtree by holding writer lock */
 static void
 iova_insert_rbtree(struct rb_root *root, struct iova *iova,
-		   struct rb_node *start)
+		   struct rb_node *start,
+		   struct iova *anchor)
 {
 	struct rb_node **new, *parent = NULL;
+	struct iova *this;
 
 	new = (start) ? &start : &(root->rb_node);
 	/* Figure out where to put new node */
 	while (*new) {
-		struct iova *this = rb_entry(*new, struct iova, node);
-
+		this = rb_entry(*new, struct iova, node);
 		parent = *new;
 
 		if (iova->pfn_lo < this->pfn_lo)
@@ -176,67 +139,115 @@ iova_insert_rbtree(struct rb_root *root, struct iova *iova,
 	/* Add new node and rebalance tree. */
 	rb_link_node(&iova->node, parent, new);
 	rb_insert_color(&iova->node, root);
+
+	this = rb_entry(rb_next(&iova->node), struct iova, node);
+	if (iova->pfn_hi == this->pfn_lo - 1)
+		return;
+
+	if (start) {
+		this = rb_entry(start, struct iova, node);
+		if (this->pfn_hi + 1 == iova->pfn_lo)
+			list_replace(&this->hole, &iova->hole);
+		else
+			list_add(&iova->hole, &this->hole);
+	} else {
+		struct iova *this = iova;
+
+		do {
+			struct iova *it;
+
+			it = rb_entry_safe(rb_prev(&iova->node),
+					   struct iova, node);
+			if (!it) {
+				list_add(&iova->hole, &anchor->hole);
+				break;
+			}
+
+			if (it->pfn_hi + 1 < this->pfn_lo) {
+				list_add(&iova->hole, &it->hole);
+				break;
+			}
+
+			this = it;
+		} while (1);
+	}
 }
 
 static int __alloc_and_insert_iova_range(struct iova_domain *iovad,
 		unsigned long size, unsigned long limit_pfn,
 			struct iova *new, bool size_aligned)
 {
-	struct rb_node *curr, *prev;
-	struct iova *curr_iova;
-	unsigned long flags;
-	unsigned long new_pfn, retry_pfn;
+	unsigned long high_pfn, low_pfn, new_pfn;
 	unsigned long align_mask = ~0UL;
-	unsigned long high_pfn = limit_pfn, low_pfn = iovad->start_pfn;
+	struct rb_node *prev;
+	unsigned long flags;
+	struct iova *it;
+
+	if (size > limit_pfn)
+		return -ENOMEM;
 
 	if (size_aligned)
 		align_mask <<= fls_long(size - 1);
 
-	/* Walk the tree backwards */
 	spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
-	if (limit_pfn <= iovad->dma_32bit_pfn &&
-			size >= iovad->max32_alloc_size)
-		goto iova32_full;
-
-	curr = __get_cached_rbnode(iovad, limit_pfn);
-	curr_iova = rb_entry(curr, struct iova, node);
-	retry_pfn = curr_iova->pfn_hi + 1;
-
-retry:
-	do {
-		high_pfn = min(high_pfn, curr_iova->pfn_lo);
-		new_pfn = (high_pfn - size) & align_mask;
-		prev = curr;
-		curr = rb_prev(curr);
-		curr_iova = rb_entry(curr, struct iova, node);
-	} while (curr && new_pfn <= curr_iova->pfn_hi && new_pfn >= low_pfn);
-
-	if (high_pfn < size || new_pfn < low_pfn) {
-		if (low_pfn == iovad->start_pfn && retry_pfn < limit_pfn) {
-			high_pfn = limit_pfn;
-			low_pfn = retry_pfn;
-			curr = &iovad->anchor.node;
-			curr_iova = rb_entry(curr, struct iova, node);
-			goto retry;
+	if (limit_pfn <= iovad->dma_32bit_pfn) {
+		if (list_empty(&iovad->anchor.hole)) {
+			prev = &iovad->anchor.node;
+			new_pfn = 0;
+			goto done;
 		}
-		iovad->max32_alloc_size = size;
-		goto iova32_full;
-	}
 
+		list_for_each_entry(it, &iovad->anchor.hole, hole) {
+			low_pfn = it->pfn_hi + 1;
+			new_pfn = (low_pfn + ~align_mask) & align_mask;
+			high_pfn = new_pfn + size;
+			if (high_pfn >= limit_pfn)
+				break;
+
+			prev = &it->node;
+			it = rb_entry(rb_next(&it->node), struct iova, node);
+			if (high_pfn <= it->pfn_lo)
+				goto done;
+		}
+	} else {
+		if (list_empty(&iovad->anchor.hole)) {
+			prev = &iovad->anchor.node;
+			new_pfn = (limit_pfn - size) & align_mask;
+			goto done;
+		}
+
+		list_for_each_entry_reverse(it, &iovad->anchor.hole, hole) {
+			if (it->pfn_lo < size)
+				break;
+
+			if (it->pfn_lo > limit_pfn)
+				continue;
+
+			new_pfn = (it->pfn_lo - size) & align_mask;
+			if (new_pfn > limit_pfn)
+				continue;
+
+			it = rb_entry_safe(rb_prev(&it->node),
+					   struct iova, node);
+			if (!it || new_pfn > it->pfn_hi) {
+				prev = &it->node;
+				goto done;
+			}
+		}
+	}
+	spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
+	return -ENOMEM;
+
+done:
 	/* pfn_lo will point to size aligned address if size_aligned is set */
 	new->pfn_lo = new_pfn;
 	new->pfn_hi = new->pfn_lo + size - 1;
 
 	/* If we have 'prev', it's a valid place to start the insertion. */
-	iova_insert_rbtree(&iovad->rbroot, new, prev);
-	__cached_rbnode_insert_update(iovad, new);
+	iova_insert_rbtree(&iovad->rbroot, new, prev, &iovad->anchor);
 
 	spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
 	return 0;
-
-iova32_full:
-	spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
-	return -ENOMEM;
 }
 
 static struct kmem_cache *iova_cache;
@@ -345,10 +356,35 @@ private_find_iova(struct iova_domain *iovad, unsigned long pfn)
 	return NULL;
 }
 
+static void insert_hole(struct iova_domain *iovad, struct iova *iova)
+{
+	struct iova *prev;
+
+	prev = rb_entry_safe(rb_prev(&iova->node), struct iova, node);
+	if (prev && prev->pfn_hi + 1 == iova->pfn_lo) {
+		struct iova *it = iova;
+
+		do {
+			struct iova *next;
+
+			next = rb_entry(rb_next(&it->node), struct iova, node);
+			if (next == &iovad->anchor ||
+			    it->pfn_hi < next->pfn_lo - 1) {
+				list_add_tail(&prev->hole, &it->hole);
+				break;
+			}
+
+			it = next;
+		} while (1);
+	}
+
+	list_del(&iova->hole);
+}
+
 static void private_free_iova(struct iova_domain *iovad, struct iova *iova)
 {
 	assert_spin_locked(&iovad->iova_rbtree_lock);
-	__cached_rbnode_delete_update(iovad, iova);
+	insert_hole(iovad, iova);
 	rb_erase(&iova->node, &iovad->rbroot);
 	free_iova_mem(iova);
 }
@@ -664,7 +700,7 @@ __insert_new_range(struct iova_domain *iovad,
 
 	iova = alloc_and_init_iova(pfn_lo, pfn_hi);
 	if (iova)
-		iova_insert_rbtree(&iovad->rbroot, iova, NULL);
+		iova_insert_rbtree(&iovad->rbroot, iova, NULL, &iovad->anchor);
 
 	return iova;
 }
diff --git a/include/linux/iova.h b/include/linux/iova.h
index 76e16ae20729..4eb5de34b5ca 100644
--- a/include/linux/iova.h
+++ b/include/linux/iova.h
@@ -11,6 +11,7 @@
 
 #include <linux/types.h>
 #include <linux/kernel.h>
+#include <linux/list.h>
 #include <linux/rbtree.h>
 #include <linux/atomic.h>
 #include <linux/dma-mapping.h>
@@ -18,6 +19,7 @@
 /* iova structure */
 struct iova {
 	struct rb_node	node;
+	struct list_head hole;
 	unsigned long	pfn_hi; /* Highest allocated pfn */
 	unsigned long	pfn_lo; /* Lowest allocated pfn */
 };
@@ -68,12 +70,9 @@ struct iova_fq {
 struct iova_domain {
 	spinlock_t	iova_rbtree_lock; /* Lock to protect update of rbtree */
 	struct rb_root	rbroot;		/* iova domain rbtree root */
-	struct rb_node	*cached_node;	/* Save last alloced node */
-	struct rb_node	*cached32_node; /* Save last 32-bit alloced node */
 	unsigned long	granule;	/* pfn granularity for this domain */
 	unsigned long	start_pfn;	/* Lower limit for this domain */
 	unsigned long	dma_32bit_pfn;
-	unsigned long	max32_alloc_size; /* Size of last failed allocation */
 	struct iova_fq __percpu *fq;	/* Flush Queue */
 
 	atomic64_t	fq_flush_start_cnt;	/* Number of TLB flushes that
-- 
2.20.1



More information about the Intel-gfx-trybot mailing list