Skip to contentSkip to conversation

memblock: remove repeat round

PATCH In review Yajun Deng 2 participants 5 messages Oct 19, 2022 – Oct 26, 2022

Subsystems (2)
lore
Share & tools
lore

Yajun Deng

author
#0 lore raw

PATCH 1/1 memblock: remove repeat round

There is no need round twice in memblock_add_range().

We can call memblock_double_array() to extand the size if type->cnt no less than type->max before memblock_insert_region(), otherwise we can insert the new region directly.

---
 mm/memblock.c | 54 +++++++++++++++------------------------------------
 1 file changed, 16 insertions(+), 38 deletions(-)
mm/memblock.c +16 -38
index 511d4783dcf1..1679244b4a1a 100644
@@ -578,7 +578,6 @@ 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;
@@ -598,22 +597,6 @@ 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;
 
@@ -635,10 +618,14 @@ static int __init_memblock memblock_add_range(struct memblock_type *type,
 #endif
 			WARN_ON(flags != rgn->flags);
 			nr_new++;
-			if (insert)
-				memblock_insert_region(type, idx++, base,
-						       rbase - base, nid,
-						       flags);
+
+			if ((type->cnt >= type->max) &&
+			    (memblock_double_array(type, obase, size) < 0))
+				return -ENOMEM;
+
+			memblock_insert_region(type, idx++, base,
+					       rbase - base, nid,
+					       flags);
 		}
 		/* area below @rend is dealt with, forget about it */
 		base = min(rend, end);
@@ -647,28 +634,19 @@ 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) &&
+		    (memblock_double_array(type, obase, size) < 0))
+			return -ENOMEM;
+
+		memblock_insert_region(type, idx, base, end - base,
+				       nid, flags);
 	}
 
 	if (!nr_new)
 		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;
 }
 
 /**
Signature · 1 line
2.25.1
maintainer
#1 lore raw

Hi,

Quoted 1 line · Yajun Deng

Subject: memblock: remove repeat round

Please make the patch subject more detailed. Say

membloc: don't run loop in memblock_add_range() twice

Quoted 3 lines

There is no need round twice in memblock_add_range().

We can call memblock_double_array() to extand the size if type->cnt no

^ extend

Quoted 1 line

less than type->max before memblock_insert_region(), otherwise we can

s/no less than/greater or equal to/

Quoted 31 lines

insert the new region directly.

---
 mm/memblock.c | 54 +++++++++++++++------------------------------------
 1 file changed, 16 insertions(+), 38 deletions(-)
mm/memblock.c +0 -9
index 511d4783dcf1..1679244b4a1a 100644
@@ -578,7 +578,6 @@ 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;
@@ -598,22 +597,6 @@ 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)

mm/memblock.c static int __init_memblock memblock_add_range(struct memblock_type *type, · line 601

Quoted 10 lines
mm/memblock.c +0 -8
-		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;

I believe nr_new variable is no longer needed, is it?

mm/memblock.c static int __init_memblock memblock_add_range(struct memblock_type *type, · line 623

Quoted 11 lines
mm/memblock.c +3 -4
@@ -635,10 +618,14 @@ static int __init_memblock memblock_add_range(struct memblock_type *type,
 #endif
 			WARN_ON(flags != rgn->flags);
 			nr_new++;
-			if (insert)
-				memblock_insert_region(type, idx++, base,
-						       rbase - base, nid,
-						       flags);
+
+			if ((type->cnt >= type->max) &&
+			    (memblock_double_array(type, obase, size) < 0))
	if ((type->cnt >= type->max) &&
	    memblock_double_array(type, obase, size))

would be just fine.

I'd appreciate a comment above the if statement explaining when the allocation is required.

Quoted 31 lines
mm/memblock.c +11 -8
+				return -ENOMEM;
+
+			memblock_insert_region(type, idx++, base,
+					       rbase - base, nid,
+					       flags);
 		}
 		/* area below @rend is dealt with, forget about it */
 		base = min(rend, end);
@@ -647,28 +634,19 @@ 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) &&
+		    (memblock_double_array(type, obase, size) < 0))
+			return -ENOMEM;
+
+		memblock_insert_region(type, idx, base, end - base,
+				       nid, flags);
 	}
 
 	if (!nr_new)
 		return 0;
 
-	/*
-	 * If this was the first round, resize array and repeat for actual
-	 * insertions; otherwise, merge and return.
-	 */
-	if (!insert) {

mm/memblock.c static int __init_memblock memblock_add_range(struct memblock_type *type, · line 648

Quoted 10 lines
mm/memblock.c +1 -9
-		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);

A blank line here would be nice.

Quoted 8 lines
mm/memblock.c +1 -0
+	return 0;
 }
 
 /**
Signature · 1 line
2.25.1
Signature · 2 lines
Sincerely yours,
Mike.
author
#2 lore raw
Quoted 10 lines · Mike Rapoport

Hi,

Quoted 1 line · Yajun Deng

Subject: memblock: remove repeat round

Please make the patch subject more detailed. Say

membloc: don't run loop in memblock_add_range() twice

Okay!

Quoted 10 lines
Quoted 3 lines

There is no need round twice in memblock_add_range().

We can call memblock_double_array() to extand the size if type->cnt no

^ extend

Quoted 1 line

less than type->max before memblock_insert_region(), otherwise we can

s/no less than/greater or equal to/

Got it.

Quoted 35 lines
Quoted 35 lines

insert the new region directly.

---
mm/memblock.c | 54 +++++++++++++++------------------------------------

1 file changed, 16 insertions(+), 38 deletions(-)
mm/memblock.c +0 -13
index 511d4783dcf1..1679244b4a1a 100644
@@ -578,7 +578,6 @@ 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;
@@ -598,22 +597,6 @@ 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:
- /*
Quoted 9 lines

mm/memblock.c static int __init_memblock memblock_add_range(struct memblock_type *type, · line 601

Quoted 6 lines
mm/memblock.c +0 -4
- * 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;

I believe nr_new variable is no longer needed, is it?

No, nr_new is needed before memblock_merge_regions() for return.

Quoted 15 lines

mm/memblock.c static int __init_memblock memblock_add_range(struct memblock_type *type, · line 623

Quoted 11 lines
mm/memblock.c +3 -4
@@ -635,10 +618,14 @@ static int __init_memblock memblock_add_range(struct memblock_type *type,
 #endif
 WARN_ON(flags != rgn->flags);
 nr_new++;
- if (insert)
- memblock_insert_region(type, idx++, base,
- rbase - base, nid,
- flags);
+
+ if ((type->cnt >= type->max) &&
+ (memblock_double_array(type, obase, size) < 0))

if ((type->cnt >= type->max) &&
memblock_double_array(type, obase, size))

Quoted 5 lines

would be just fine.

I'd appreciate a comment above the if statement explaining when the allocation is required.

Got it.

Quoted 35 lines
Quoted 35 lines
mm/memblock.c +11 -12
+ return -ENOMEM;
+
+ memblock_insert_region(type, idx++, base,
+ rbase - base, nid,
+ flags);
 }
 /* area below @rend is dealt with, forget about it */
 base = min(rend, end);
@@ -647,28 +634,19 @@ 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) &&
+ (memblock_double_array(type, obase, size) < 0))
+ return -ENOMEM;
+
+ memblock_insert_region(type, idx, base, end - base,
+ nid, flags);
 }
 
 if (!nr_new)
 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;
Quoted 9 lines

mm/memblock.c static int __init_memblock memblock_add_range(struct memblock_type *type, · line 648

Quoted 6 lines
mm/memblock.c +1 -5
- goto repeat;
- } else {
- memblock_merge_regions(type);
- return 0;
- }
+ memblock_merge_regions(type);

A blank line here would be nice.

Got it.

Quoted 10 lines
Quoted 6 lines
mm/memblock.c +1 -1
+ return 0;
 }
 
 /**
--

2.25.1

--
Sincerely yours,
Mike.

maintainer
#3 lore raw
Quoted 27 lines · Yajun Deng
Quoted 16 lines · Mike Rapoport
Quoted 16 lines
mm/memblock.c +0 -12
@@ -598,22 +597,6 @@ 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:
- /*
Quoted 9 lines

mm/memblock.c static int __init_memblock memblock_add_range(struct memblock_type *type, · line 601

Quoted 6 lines
mm/memblock.c +0 -4
- * 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;

I believe nr_new variable is no longer needed, is it?

Quoted 1 line

No, nr_new is needed before memblock_merge_regions() for return.

Why?

Signature · 2 lines
Sincerely yours,
Mike.
author
#4 lore raw
Quoted 27 lines · Mike Rapoport
Quoted 25 lines · Yajun Deng

October 25, 2022 12:15 AM, "Mike Rapoport" <[email protected]> wrote:

mm/memblock.c +0 -16
@@ -598,22 +597,6 @@ 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;
 
Quoted 6 lines
Quoted 3 lines

I believe nr_new variable is no longer needed, is it?

No, nr_new is needed before memblock_merge_regions() for return.

Why?

Sorry, nr_new was removed, and added ocnt variable for the original of type->cnt.

I already sent another patch with '[PATCH v2] memblock: don't run loop in memblock_add_range() twice' subject, you can see that.

Quoted 3 lines

--
Sincerely yours,
Mike.