From 0a48980d7346c582eeb7198d822d56a4c1926632 Mon Sep 17 00:00:00 2001
From: Yajun Deng <yajun.deng@linux.dev>
Date: Wed, 9 Nov 2022 17:35:27 +0800
Subject: [PATCH v3] memblock: don't run loop in memblock_add_range() twice in
 most case

There is no need round twice in memblock_add_range() in most case.

We can call memblock_double_array() to extend the size if type->cnt
greater or equal to type->max before memblock_insert_region(); otherwise,
we can insert the new region directly.

Special: memblock_double_array() would nested call memblock_add_range()
if slab_is_available() is NULL, so we should repeat again before insert
in that case.

v3:
 - Fix nested call in memblock_double_array().

v2:
 - Add a comment when the allocation is required.

Signed-off-by: Yajun Deng <yajun.deng@linux.dev>
---
 mm/memblock.c | 76 +++++++++++++++++++++++----------------------------
 1 file changed, 34 insertions(+), 42 deletions(-)

diff --git a/mm/memblock.c b/mm/memblock.c
index 511d4783dcf1..42d3e4157f4a 100644
--- a/mm/memblock.c
+++ b/mm/memblock.c
@@ -578,11 +578,11 @@ static int __init_memblock memblock_add_range(struct memblock_type *type,
 				phys_addr_t base, phys_addr_t size,
 				int nid, enum memblock_flags flags)
 {
-	bool insert = false;
 	phys_addr_t obase = base;
 	phys_addr_t end = base + memblock_cap_size(base, &size);
-	int idx, nr_new;
+	unsigned long ocnt = type->cnt;
 	struct memblock_region *rgn;
+	int idx;
 
 	if (!size)
 		return 0;
@@ -598,24 +598,8 @@ static int __init_memblock memblock_add_range(struct memblock_type *type,
 		return 0;
 	}
 
-	/*
-	 * The worst case is when new range overlaps all existing regions,
-	 * then we'll need type->cnt + 1 empty regions in @type. So if
-	 * type->cnt * 2 + 1 is less than type->max, we know
-	 * that there is enough empty regions in @type, and we can insert
-	 * regions directly.
-	 */
-	if (type->cnt * 2 + 1 < type->max)
-		insert = true;
-
 repeat:
-	/*
-	 * The following is executed twice.  Once with %false @insert and
-	 * then with %true.  The first counts the number of regions needed
-	 * to accommodate the new area.  The second actually inserts them.
-	 */
 	base = obase;
-	nr_new = 0;
 
 	for_each_memblock_type(idx, type, rgn) {
 		phys_addr_t rbase = rgn->base;
@@ -634,11 +618,25 @@ static int __init_memblock memblock_add_range(struct memblock_type *type,
 			WARN_ON(nid != memblock_get_region_node(rgn));
 #endif
 			WARN_ON(flags != rgn->flags);
-			nr_new++;
-			if (insert)
-				memblock_insert_region(type, idx++, base,
-						       rbase - base, nid,
-						       flags);
+
+			/*
+			 * if type->cnt greater or equal to type->max,
+			 * resize array; otherwise, insert directly.
+			 *
+			 * special: there may be nested call this function
+			 * in memblock_double_array(), so it should repeat in
+			 * that case.
+			 */
+			if (type->cnt >= type->max) {
+				if (memblock_double_array(type, obase, size))
+					return -ENOMEM;
+				else if (!slab_is_available())
+					goto repeat;
+			}
+
+			memblock_insert_region(type, idx++, base,
+					       rbase - base, nid,
+					       flags);
 		}
 		/* area below @rend is dealt with, forget about it */
 		base = min(rend, end);
@@ -646,29 +644,23 @@ static int __init_memblock memblock_add_range(struct memblock_type *type,
 
 	/* insert the remaining portion */
 	if (base < end) {
-		nr_new++;
-		if (insert)
-			memblock_insert_region(type, idx, base, end - base,
-					       nid, flags);
+		if (type->cnt >= type->max) {
+			if (memblock_double_array(type, obase, size))
+				return -ENOMEM;
+			else if (!slab_is_available())
+				goto repeat;
+		}
+
+		memblock_insert_region(type, idx, base, end - base,
+				       nid, flags);
 	}
 
-	if (!nr_new)
+	if (ocnt == type->cnt)
 		return 0;
 
-	/*
-	 * If this was the first round, resize array and repeat for actual
-	 * insertions; otherwise, merge and return.
-	 */
-	if (!insert) {
-		while (type->cnt + nr_new > type->max)
-			if (memblock_double_array(type, obase, size) < 0)
-				return -ENOMEM;
-		insert = true;
-		goto repeat;
-	} else {
-		memblock_merge_regions(type);
-		return 0;
-	}
+	memblock_merge_regions(type);
+
+	return 0;
 }
 
 /**
-- 
2.25.1

