Skip to contentSkip to conversation

lmb: remove overlapping region with next range

PATCH Superseded Udit Kumar 5 participants 9 messages Sep 24, 2023 – Sep 25, 2023 Library code

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Udit Kumar

author
#0 lore raw

PATCH 1/1 lmb: remove overlapping region with next range

In case of new memory range to be added is coalesced with any already added non last lmb region.

And there is possibility that, then region in which new memory range added is not adjacent to next region. But have some sections are overlapping.

So along with adjacency check with next lmb region, check for overlap should be done.

In case overlap is found, adjust and merge these two lmb region into one.

---
logs
https://gist.github.com/uditkumarti/5d08c34442235ad270cfa863792ebcdc
seqeunce : line 1 to 13
without fix : line 96-100
with fix : line 199-202

 lib/lmb.c | 37 +++++++++++++++++++++++++++++++++----
 1 file changed, 33 insertions(+), 4 deletions(-)
lib/lmb.c +33 -4
index b2c233edb6..2580d01d90 100644
@@ -74,6 +74,16 @@ static long lmb_addrs_adjacent(phys_addr_t base1, phys_size_t size1,
 	return 0;
 }
 
+static long lmb_regions_overlap(struct lmb_region *rgn, unsigned long r1,
+				unsigned long r2)
+{
+	phys_addr_t base1 = rgn->region[r1].base;
+	phys_size_t size1 = rgn->region[r1].size;
+	phys_addr_t base2 = rgn->region[r2].base;
+	phys_size_t size2 = rgn->region[r2].size;
+
+	return lmb_addrs_overlap(base1, size1, base2, size2);
+}
 static long lmb_regions_adjacent(struct lmb_region *rgn, unsigned long r1,
 				 unsigned long r2)
 {
@@ -81,7 +91,6 @@ static long lmb_regions_adjacent(struct lmb_region *rgn, unsigned long r1,
 	phys_size_t size1 = rgn->region[r1].size;
 	phys_addr_t base2 = rgn->region[r2].base;
 	phys_size_t size2 = rgn->region[r2].size;
-
 	return lmb_addrs_adjacent(base1, size1, base2, size2);
 }
 
@@ -105,6 +114,23 @@ static void lmb_coalesce_regions(struct lmb_region *rgn, unsigned long r1,
 	lmb_remove_region(rgn, r2);
 }
 
+/*Assmptuon : base addr of region 1 < base addr of region 2*/
+static void lmb_fix_over_lap_regions(struct lmb_region *rgn, unsigned long r1,
+				     unsigned long r2)
+{
+	phys_addr_t base1 = rgn->region[r1].base;
+	phys_size_t size1 = rgn->region[r1].size;
+	phys_addr_t base2 = rgn->region[r2].base;
+	phys_size_t size2 = rgn->region[r2].size;
+
+	if (base1 + size1 > base2 + size2) {
+		printf("This will not be a case any time\n");
+		return;
+	}
+	rgn->region[r1].size = base2 + size2 - base1;
+	lmb_remove_region(rgn, r2);
+}
+
 void lmb_init(struct lmb *lmb)
 {
 #if IS_ENABLED(CONFIG_LMB_USE_MAX_REGIONS)
@@ -249,7 +275,6 @@ static long lmb_add_region_flags(struct lmb_region *rgn, phys_addr_t base,
 		phys_size_t rgnflags = rgn->region[i].flags;
 		phys_addr_t end = base + size - 1;
 		phys_addr_t rgnend = rgnbase + rgnsize - 1;
-
 		if (rgnbase <= base && end <= rgnend) {
 			if (flags == rgnflags)
 				/* Already have this region, so we're done */
@@ -278,10 +303,14 @@ static long lmb_add_region_flags(struct lmb_region *rgn, phys_addr_t base,
 		}
 	}
 
-	if ((i < rgn->cnt - 1) && lmb_regions_adjacent(rgn, i, i + 1)) {
-		if (rgn->region[i].flags == rgn->region[i + 1].flags) {
+	if (i < rgn->cnt - 1 && rgn->region[i].flags == rgn->region[i + 1].flags)  {
+		if (lmb_regions_adjacent(rgn, i, i + 1)) {
 			lmb_coalesce_regions(rgn, i, i + 1);
 			coalesced++;
+		} else if (lmb_regions_overlap(rgn, i, i + 1)) {
+			/* fix overlapping area */
+			lmb_fix_over_lap_regions(rgn, i, i + 1);
+			coalesced++;
 		}
 	}
 
Signature · 1 line
2.34.1
#1 lore raw

Am 24. September 2023 13:18:32 MESZ schrieb Udit Kumar <[email protected]>:

Quoted 55 lines

In case of new memory range to be added is coalesced with any already added non last lmb region.

And there is possibility that, then region in which new memory range added is not adjacent to next region. But have some sections are overlapping.

So along with adjacency check with next lmb region, check for overlap should be done.

In case overlap is found, adjust and merge these two lmb region into one.

---
logs
https://gist.github.com/uditkumarti/5d08c34442235ad270cfa863792ebcdc
seqeunce : line 1 to 13
without fix : line 96-100
with fix : line 199-202

lib/lmb.c | 37 +++++++++++++++++++++++++++++++++----

1 file changed, 33 insertions(+), 4 deletions(-)
lib/lmb.c +10 -1
index b2c233edb6..2580d01d90 100644
@@ -74,6 +74,16 @@ static long lmb_addrs_adjacent(phys_addr_t base1, phys_size_t size1,
 	return 0;
 }
 
+static long lmb_regions_overlap(struct lmb_region *rgn, unsigned long r1,
+				unsigned long r2)
+{
+	phys_addr_t base1 = rgn->region[r1].base;
+	phys_size_t size1 = rgn->region[r1].size;
+	phys_addr_t base2 = rgn->region[r2].base;
+	phys_size_t size2 = rgn->region[r2].size;
+
+	return lmb_addrs_overlap(base1, size1, base2, size2);
+}
 static long lmb_regions_adjacent(struct lmb_region *rgn, unsigned long r1,
 				 unsigned long r2)
 {
@@ -81,7 +91,6 @@ static long lmb_regions_adjacent(struct lmb_region *rgn, unsigned long r1,
 	phys_size_t size1 = rgn->region[r1].size;
 	phys_addr_t base2 = rgn->region[r2].base;
 	phys_size_t size2 = rgn->region[r2].size;
-
 	return lmb_addrs_adjacent(base1, size1, base2, size2);
 }
 

lib/lmb.c static void lmb_coalesce_regions(struct lmb_region *rgn, unsigned long r1, · line 117

Quoted 4 lines
lib/lmb.c +1 -0
@@ -105,6 +114,23 @@ static void lmb_coalesce_regions(struct lmb_region *rgn, unsigned long r1,
 	lmb_remove_region(rgn, r2);
 }
 
+/*Assmptuon : base addr of region 1 < base addr of region 2*/

Assumption

Regards Heinrich

Quoted 44 lines
lib/lmb.c +22 -3
+static void lmb_fix_over_lap_regions(struct lmb_region *rgn, unsigned long r1,
+				     unsigned long r2)
+{
+	phys_addr_t base1 = rgn->region[r1].base;
+	phys_size_t size1 = rgn->region[r1].size;
+	phys_addr_t base2 = rgn->region[r2].base;
+	phys_size_t size2 = rgn->region[r2].size;
+
+	if (base1 + size1 > base2 + size2) {
+		printf("This will not be a case any time\n");
+		return;
+	}
+	rgn->region[r1].size = base2 + size2 - base1;
+	lmb_remove_region(rgn, r2);
+}
+
 void lmb_init(struct lmb *lmb)
 {
 #if IS_ENABLED(CONFIG_LMB_USE_MAX_REGIONS)
@@ -249,7 +275,6 @@ static long lmb_add_region_flags(struct lmb_region *rgn, phys_addr_t base,
 		phys_size_t rgnflags = rgn->region[i].flags;
 		phys_addr_t end = base + size - 1;
 		phys_addr_t rgnend = rgnbase + rgnsize - 1;
-
 		if (rgnbase <= base && end <= rgnend) {
 			if (flags == rgnflags)
 				/* Already have this region, so we're done */
@@ -278,10 +303,14 @@ static long lmb_add_region_flags(struct lmb_region *rgn, phys_addr_t base,
 		}
 	}
 
-	if ((i < rgn->cnt - 1) && lmb_regions_adjacent(rgn, i, i + 1)) {
-		if (rgn->region[i].flags == rgn->region[i + 1].flags) {
+	if (i < rgn->cnt - 1 && rgn->region[i].flags == rgn->region[i + 1].flags)  {
+		if (lmb_regions_adjacent(rgn, i, i + 1)) {
 			lmb_coalesce_regions(rgn, i, i + 1);
 			coalesced++;
+		} else if (lmb_regions_overlap(rgn, i, i + 1)) {
+			/* fix overlapping area */
+			lmb_fix_over_lap_regions(rgn, i, i + 1);
+			coalesced++;
 		}
 	}
 
#2 lore raw
Quoted 56 lines · Heinrich Schuchardt

Am 24. September 2023 13:18:32 MESZ schrieb Udit Kumar <[email protected]>:

Quoted 54 lines

In case of new memory range to be added is coalesced with any already added non last lmb region.

And there is possibility that, then region in which new memory range added is not adjacent to next region. But have some sections are overlapping.

So along with adjacency check with next lmb region, check for overlap should be done.

In case overlap is found, adjust and merge these two lmb region into one.

---
logs
https://gist.github.com/uditkumarti/5d08c34442235ad270cfa863792ebcdc
seqeunce : line 1 to 13
without fix : line 96-100
with fix : line 199-202

lib/lmb.c | 37 +++++++++++++++++++++++++++++++++----

1 file changed, 33 insertions(+), 4 deletions(-)
lib/lmb.c +10 -1
index b2c233edb6..2580d01d90 100644
@@ -74,6 +74,16 @@ static long lmb_addrs_adjacent(phys_addr_t base1, phys_size_t size1,
 	return 0;
 }
 
+static long lmb_regions_overlap(struct lmb_region *rgn, unsigned long r1,
+				unsigned long r2)
+{
+	phys_addr_t base1 = rgn->region[r1].base;
+	phys_size_t size1 = rgn->region[r1].size;
+	phys_addr_t base2 = rgn->region[r2].base;
+	phys_size_t size2 = rgn->region[r2].size;
+
+	return lmb_addrs_overlap(base1, size1, base2, size2);
+}
 static long lmb_regions_adjacent(struct lmb_region *rgn, unsigned long r1,
 				 unsigned long r2)
 {
@@ -81,7 +91,6 @@ static long lmb_regions_adjacent(struct lmb_region *rgn, unsigned long r1,
 	phys_size_t size1 = rgn->region[r1].size;
 	phys_addr_t base2 = rgn->region[r2].base;
 	phys_size_t size2 = rgn->region[r2].size;
-
 	return lmb_addrs_adjacent(base1, size1, base2, size2);
 }
 
Quoted 6 lines

lib/lmb.c static void lmb_coalesce_regions(struct lmb_region *rgn, unsigned long r1, · line 117

Quoted 5 lines
lib/lmb.c +1 -0
@@ -105,6 +114,23 @@ static void lmb_coalesce_regions(struct lmb_region *rgn, unsigned long r1,
 	lmb_remove_region(rgn, r2);
 }
 
+/*Assmptuon : base addr of region 1 < base addr of region 2*/

Assumption

Thanks Heinrich  will fix this typo in v2.

Let me wait for other review feedback as well before proceeding for v2.

Quoted 46 lines

Regards Heinrich

Quoted 44 lines
lib/lmb.c +22 -3
+static void lmb_fix_over_lap_regions(struct lmb_region *rgn, unsigned long r1,
+				     unsigned long r2)
+{
+	phys_addr_t base1 = rgn->region[r1].base;
+	phys_size_t size1 = rgn->region[r1].size;
+	phys_addr_t base2 = rgn->region[r2].base;
+	phys_size_t size2 = rgn->region[r2].size;
+
+	if (base1 + size1 > base2 + size2) {
+		printf("This will not be a case any time\n");
+		return;
+	}
+	rgn->region[r1].size = base2 + size2 - base1;
+	lmb_remove_region(rgn, r2);
+}
+
 void lmb_init(struct lmb *lmb)
 {
 #if IS_ENABLED(CONFIG_LMB_USE_MAX_REGIONS)
@@ -249,7 +275,6 @@ static long lmb_add_region_flags(struct lmb_region *rgn, phys_addr_t base,
 		phys_size_t rgnflags = rgn->region[i].flags;
 		phys_addr_t end = base + size - 1;
 		phys_addr_t rgnend = rgnbase + rgnsize - 1;
-
 		if (rgnbase <= base && end <= rgnend) {
 			if (flags == rgnflags)
 				/* Already have this region, so we're done */
@@ -278,10 +303,14 @@ static long lmb_add_region_flags(struct lmb_region *rgn, phys_addr_t base,
 		}
 	}
 
-	if ((i < rgn->cnt - 1) && lmb_regions_adjacent(rgn, i, i + 1)) {
-		if (rgn->region[i].flags == rgn->region[i + 1].flags) {
+	if (i < rgn->cnt - 1 && rgn->region[i].flags == rgn->region[i + 1].flags)  {
+		if (lmb_regions_adjacent(rgn, i, i + 1)) {
 			lmb_coalesce_regions(rgn, i, i + 1);
 			coalesced++;
+		} else if (lmb_regions_overlap(rgn, i, i + 1)) {
+			/* fix overlapping area */
+			lmb_fix_over_lap_regions(rgn, i, i + 1);
+			coalesced++;
 		}
 	}
 
#3 lore raw

Hi Udit,

Quoted 104 lines · Udit
Quoted 56 lines · Heinrich Schuchardt

Am 24. September 2023 13:18:32 MESZ schrieb Udit Kumar <[email protected]>:

Quoted 54 lines

In case of new memory range to be added is coalesced with any already added non last lmb region.

And there is possibility that, then region in which new memory range added is not adjacent to next region. But have some sections are overlapping.

So along with adjacency check with next lmb region, check for overlap should be done.

In case overlap is found, adjust and merge these two lmb region into one.

---
logs
https://gist.github.com/uditkumarti/5d08c34442235ad270cfa863792ebcdc
seqeunce : line 1 to 13
without fix : line 96-100
with fix : line 199-202

lib/lmb.c | 37 +++++++++++++++++++++++++++++++++----

1 file changed, 33 insertions(+), 4 deletions(-)
lib/lmb.c +10 -1
index b2c233edb6..2580d01d90 100644
@@ -74,6 +74,16 @@ static long lmb_addrs_adjacent(phys_addr_t base1, phys_size_t size1,
     return 0;
 }
 
+static long lmb_regions_overlap(struct lmb_region *rgn, unsigned long r1,
+                            unsigned long r2)
+{
+    phys_addr_t base1 = rgn->region[r1].base;
+    phys_size_t size1 = rgn->region[r1].size;
+    phys_addr_t base2 = rgn->region[r2].base;
+    phys_size_t size2 = rgn->region[r2].size;
+
+    return lmb_addrs_overlap(base1, size1, base2, size2);
+}
 static long lmb_regions_adjacent(struct lmb_region *rgn, unsigned long r1,
                              unsigned long r2)
 {
@@ -81,7 +91,6 @@ static long lmb_regions_adjacent(struct lmb_region *rgn, unsigned long r1,
     phys_size_t size1 = rgn->region[r1].size;
     phys_addr_t base2 = rgn->region[r2].base;
     phys_size_t size2 = rgn->region[r2].size;
-
     return lmb_addrs_adjacent(base1, size1, base2, size2);
 }
 
Quoted 6 lines

lib/lmb.c static void lmb_coalesce_regions(struct lmb_region *rgn, unsigned long r1, · line 117

Quoted 5 lines
lib/lmb.c +1 -0
@@ -105,6 +114,23 @@ static void lmb_coalesce_regions(struct lmb_region *rgn, unsigned long r1,
     lmb_remove_region(rgn, r2);
 }
 
+/*Assmptuon : base addr of region 1 < base addr of region 2*/

Assumption

Thanks Heinrich will fix this typo in v2.

Let me wait for other review feedback as well before proceeding for v2.

Quoted 33 lines

Regards Heinrich

Quoted 31 lines
lib/lmb.c +16 -1
+static void lmb_fix_over_lap_regions(struct lmb_region *rgn, unsigned long r1,
+                                 unsigned long r2)
+{
+    phys_addr_t base1 = rgn->region[r1].base;
+    phys_size_t size1 = rgn->region[r1].size;
+    phys_addr_t base2 = rgn->region[r2].base;
+    phys_size_t size2 = rgn->region[r2].size;
+
+    if (base1 + size1 > base2 + size2) {
+            printf("This will not be a case any time\n");
+            return;
+    }
+    rgn->region[r1].size = base2 + size2 - base1;
+    lmb_remove_region(rgn, r2);
+}
+
 void lmb_init(struct lmb *lmb)
 {
 #if IS_ENABLED(CONFIG_LMB_USE_MAX_REGIONS)
@@ -249,7 +275,6 @@ static long lmb_add_region_flags(struct lmb_region *rgn, phys_addr_t base,
             phys_size_t rgnflags = rgn->region[i].flags;
             phys_addr_t end = base + size - 1;
             phys_addr_t rgnend = rgnbase + rgnsize - 1;
-
             if (rgnbase <= base && end <= rgnend) {
                     if (flags == rgnflags)
                             /* Already have this region, so we're done */
@@ -278,10 +303,14 @@ static long lmb_add_region_flags(struct lmb_region *rgn, phys_addr_t base,
             }
     }
 
Quoted 13 lines
Quoted 5 lines
Quoted 5 lines
lib/lmb.c +2 -2
-    if ((i < rgn->cnt - 1) && lmb_regions_adjacent(rgn, i, i + 1)) {
-            if (rgn->region[i].flags == rgn->region[i + 1].flags) {
+    if (i < rgn->cnt - 1 && rgn->region[i].flags == rgn->region[i + 1].flags)  {
+            if (lmb_regions_adjacent(rgn, i, i + 1)) {
                     lmb_coalesce_regions(rgn, i, i + 1);
Quoted 8 lines

lib/lmb.c static long lmb_add_region_flags(struct lmb_region *rgn, phys_addr_t base, · line 316

Quoted 8 lines
lib/lmb.c +4 -0
                     coalesced++;
+            } else if (lmb_regions_overlap(rgn, i, i + 1)) {
+                    /* fix overlapping area */
+                    lmb_fix_over_lap_regions(rgn, i, i + 1);
+                    coalesced++;
             }
     }
 

Can you please add a test to test/lib which fails before your change and passes after?

Regards,
Simon

#4 lore raw

Thanks Simon

Quoted 10 lines · Simon Glass

Hi Udit,

Quoted 7 lines · Udit
Quoted 3 lines · Heinrich Schuchardt

Am 24. September 2023 13:18:32 MESZ schrieb Udit Kumar <[email protected]>:

Quoted 1 line

[..]

Assumption

Thanks Heinrich will fix this typo in v2.

Quoted 8 lines
Quoted 6 lines

Let me wait for other review feedback as well before proceeding for v2.

Quoted 3 lines

Regards Heinrich

Quoted 1 line

[..]

Can you please add a test to test/lib which fails before your change and passes after?

Sure, will add test case.

Quoted 2 lines

Regards,
Simon

#5 lore raw
In reply to Udit Kumar
Quoted 7 lines · Udit Kumar

In case of new memory range to be added is coalesced with any already added non last lmb region.

And there is possibility that, then region in which new memory range added is not adjacent to next region. But have some sections are overlapping.

Quoted 8 lines

So along with adjacency check with next lmb region, check for overlap should be done.

In case overlap is found, adjust and merge these two lmb region into one.

This is good! I wonder if this addresses the issue that has caused other platforms to need to set CONFIG_LMB_MAX_REGIONS=64 and I'm wondering if any of the other maintainers I've cc'd here can drop back to the default number of regions and then re-test their failure case? Thanks!

Quoted 91 lines

---
logs
https://gist.github.com/uditkumarti/5d08c34442235ad270cfa863792ebcdc
seqeunce : line 1 to 13
without fix : line 96-100
with fix : line 199-202

 lib/lmb.c | 37 +++++++++++++++++++++++++++++++++----
 1 file changed, 33 insertions(+), 4 deletions(-)
lib/lmb.c +33 -4
index b2c233edb6..2580d01d90 100644
@@ -74,6 +74,16 @@ static long lmb_addrs_adjacent(phys_addr_t base1, phys_size_t size1,
 	return 0;
 }
 
+static long lmb_regions_overlap(struct lmb_region *rgn, unsigned long r1,
+				unsigned long r2)
+{
+	phys_addr_t base1 = rgn->region[r1].base;
+	phys_size_t size1 = rgn->region[r1].size;
+	phys_addr_t base2 = rgn->region[r2].base;
+	phys_size_t size2 = rgn->region[r2].size;
+
+	return lmb_addrs_overlap(base1, size1, base2, size2);
+}
 static long lmb_regions_adjacent(struct lmb_region *rgn, unsigned long r1,
 				 unsigned long r2)
 {
@@ -81,7 +91,6 @@ static long lmb_regions_adjacent(struct lmb_region *rgn, unsigned long r1,
 	phys_size_t size1 = rgn->region[r1].size;
 	phys_addr_t base2 = rgn->region[r2].base;
 	phys_size_t size2 = rgn->region[r2].size;
-
 	return lmb_addrs_adjacent(base1, size1, base2, size2);
 }
 
@@ -105,6 +114,23 @@ static void lmb_coalesce_regions(struct lmb_region *rgn, unsigned long r1,
 	lmb_remove_region(rgn, r2);
 }
 
+/*Assmptuon : base addr of region 1 < base addr of region 2*/
+static void lmb_fix_over_lap_regions(struct lmb_region *rgn, unsigned long r1,
+				     unsigned long r2)
+{
+	phys_addr_t base1 = rgn->region[r1].base;
+	phys_size_t size1 = rgn->region[r1].size;
+	phys_addr_t base2 = rgn->region[r2].base;
+	phys_size_t size2 = rgn->region[r2].size;
+
+	if (base1 + size1 > base2 + size2) {
+		printf("This will not be a case any time\n");
+		return;
+	}
+	rgn->region[r1].size = base2 + size2 - base1;
+	lmb_remove_region(rgn, r2);
+}
+
 void lmb_init(struct lmb *lmb)
 {
 #if IS_ENABLED(CONFIG_LMB_USE_MAX_REGIONS)
@@ -249,7 +275,6 @@ static long lmb_add_region_flags(struct lmb_region *rgn, phys_addr_t base,
 		phys_size_t rgnflags = rgn->region[i].flags;
 		phys_addr_t end = base + size - 1;
 		phys_addr_t rgnend = rgnbase + rgnsize - 1;
-
 		if (rgnbase <= base && end <= rgnend) {
 			if (flags == rgnflags)
 				/* Already have this region, so we're done */
@@ -278,10 +303,14 @@ static long lmb_add_region_flags(struct lmb_region *rgn, phys_addr_t base,
 		}
 	}
 
-	if ((i < rgn->cnt - 1) && lmb_regions_adjacent(rgn, i, i + 1)) {
-		if (rgn->region[i].flags == rgn->region[i + 1].flags) {
+	if (i < rgn->cnt - 1 && rgn->region[i].flags == rgn->region[i + 1].flags)  {
+		if (lmb_regions_adjacent(rgn, i, i + 1)) {
 			lmb_coalesce_regions(rgn, i, i + 1);
 			coalesced++;
+		} else if (lmb_regions_overlap(rgn, i, i + 1)) {
+			/* fix overlapping area */
+			lmb_fix_over_lap_regions(rgn, i, i + 1);
+			coalesced++;
 		}
 	}
 
Signature · 1 line
2.34.1
Signature · 1 line
Tom
#6 lore raw
Quoted 18 lines

Date: Mon, 25 Sep 2023 11:21:26 -0400
From: Tom Rini <[email protected]>

Quoted 13 lines · Udit Kumar

In case of new memory range to be added is coalesced with any already added non last lmb region.

And there is possibility that, then region in which new memory range added is not adjacent to next region. But have some sections are overlapping.

So along with adjacency check with next lmb region, check for overlap should be done.

In case overlap is found, adjust and merge these two lmb region into one.

Quoted 8 lines
Quoted 2 lines

This is good! I wonder if this addresses the issue that has caused other platforms to need to set CONFIG_LMB_MAX_REGIONS=64 and I'm wondering if any of the other maintainers I've cc'd here can drop back to the default number of regions and then re-test their failure case? Thanks!

Hi Tom,

For the Apple M1 systems I don't think it makes sense to reduce the number of regions. These systems don't have TPL or SPL, have plenty of memory (at least 8GB) and are really fast. And we anticipate that the number of memory regions will only grow in the future.

Cheers,

Mark

Quoted 97 lines
Quoted 91 lines

---
logs
https://gist.github.com/uditkumarti/5d08c34442235ad270cfa863792ebcdc
seqeunce : line 1 to 13
without fix : line 96-100
with fix : line 199-202

 lib/lmb.c | 37 +++++++++++++++++++++++++++++++++----
 1 file changed, 33 insertions(+), 4 deletions(-)
lib/lmb.c +33 -4
index b2c233edb6..2580d01d90 100644
@@ -74,6 +74,16 @@ static long lmb_addrs_adjacent(phys_addr_t base1, phys_size_t size1,
 	return 0;
 }
 
+static long lmb_regions_overlap(struct lmb_region *rgn, unsigned long r1,
+				unsigned long r2)
+{
+	phys_addr_t base1 = rgn->region[r1].base;
+	phys_size_t size1 = rgn->region[r1].size;
+	phys_addr_t base2 = rgn->region[r2].base;
+	phys_size_t size2 = rgn->region[r2].size;
+
+	return lmb_addrs_overlap(base1, size1, base2, size2);
+}
 static long lmb_regions_adjacent(struct lmb_region *rgn, unsigned long r1,
 				 unsigned long r2)
 {
@@ -81,7 +91,6 @@ static long lmb_regions_adjacent(struct lmb_region *rgn, unsigned long r1,
 	phys_size_t size1 = rgn->region[r1].size;
 	phys_addr_t base2 = rgn->region[r2].base;
 	phys_size_t size2 = rgn->region[r2].size;
-
 	return lmb_addrs_adjacent(base1, size1, base2, size2);
 }
 
@@ -105,6 +114,23 @@ static void lmb_coalesce_regions(struct lmb_region *rgn, unsigned long r1,
 	lmb_remove_region(rgn, r2);
 }
 
+/*Assmptuon : base addr of region 1 < base addr of region 2*/
+static void lmb_fix_over_lap_regions(struct lmb_region *rgn, unsigned long r1,
+				     unsigned long r2)
+{
+	phys_addr_t base1 = rgn->region[r1].base;
+	phys_size_t size1 = rgn->region[r1].size;
+	phys_addr_t base2 = rgn->region[r2].base;
+	phys_size_t size2 = rgn->region[r2].size;
+
+	if (base1 + size1 > base2 + size2) {
+		printf("This will not be a case any time\n");
+		return;
+	}
+	rgn->region[r1].size = base2 + size2 - base1;
+	lmb_remove_region(rgn, r2);
+}
+
 void lmb_init(struct lmb *lmb)
 {
 #if IS_ENABLED(CONFIG_LMB_USE_MAX_REGIONS)
@@ -249,7 +275,6 @@ static long lmb_add_region_flags(struct lmb_region *rgn, phys_addr_t base,
 		phys_size_t rgnflags = rgn->region[i].flags;
 		phys_addr_t end = base + size - 1;
 		phys_addr_t rgnend = rgnbase + rgnsize - 1;
-
 		if (rgnbase <= base && end <= rgnend) {
 			if (flags == rgnflags)
 				/* Already have this region, so we're done */
@@ -278,10 +303,14 @@ static long lmb_add_region_flags(struct lmb_region *rgn, phys_addr_t base,
 		}
 	}
 
-	if ((i < rgn->cnt - 1) && lmb_regions_adjacent(rgn, i, i + 1)) {
-		if (rgn->region[i].flags == rgn->region[i + 1].flags) {
+	if (i < rgn->cnt - 1 && rgn->region[i].flags == rgn->region[i + 1].flags)  {
+		if (lmb_regions_adjacent(rgn, i, i + 1)) {
 			lmb_coalesce_regions(rgn, i, i + 1);
 			coalesced++;
+		} else if (lmb_regions_overlap(rgn, i, i + 1)) {
+			/* fix overlapping area */
+			lmb_fix_over_lap_regions(rgn, i, i + 1);
+			coalesced++;
 		}
 	}
 
Signature · 1 line
2.34.1
Signature · 3 lines
Tom

[2:application/pgp-signature Show Save:signature.asc (659B)]
#7 lore raw
Quoted 27 lines · Mark Kettenis
Quoted 18 lines

Date: Mon, 25 Sep 2023 11:21:26 -0400
From: Tom Rini <[email protected]>

Quoted 13 lines · Udit Kumar

In case of new memory range to be added is coalesced with any already added non last lmb region.

And there is possibility that, then region in which new memory range added is not adjacent to next region. But have some sections are overlapping.

So along with adjacency check with next lmb region, check for overlap should be done.

In case overlap is found, adjust and merge these two lmb region into one.

Quoted 8 lines
Quoted 2 lines

This is good! I wonder if this addresses the issue that has caused other platforms to need to set CONFIG_LMB_MAX_REGIONS=64 and I'm wondering if any of the other maintainers I've cc'd here can drop back to the default number of regions and then re-test their failure case? Thanks!

Quoted 6 lines

Hi Tom,

For the Apple M1 systems I don't think it makes sense to reduce the number of regions. These systems don't have TPL or SPL, have plenty of memory (at least 8GB) and are really fast. And we anticipate that the number of memory regions will only grow in the future.

This partly gets at what I see as part of the problem. It's been unclear to me if it's reasonable to just bump the default to 64 for aarch64 (and riscv?) because the problem is that 16 is truly too small, or given what we're doing we still don't need a ton of LMB regions as at the end of the day things should be coalesced in to just a few and it's been an underlying bug in an area everyone assumed wasn't buggy.

Signature · 1 line
Tom
#8 lore raw

Hi Mark/Tom

Quoted 32 lines · Tom Rini
Quoted 25 lines · Mark Kettenis
Quoted 18 lines

Date: Mon, 25 Sep 2023 11:21:26 -0400
From: Tom Rini <[email protected]>

Quoted 13 lines · Udit Kumar

In case of new memory range to be added is coalesced with any already added non last lmb region.

And there is possibility that, then region in which new memory range added is not adjacent to next region. But have some sections are overlapping.

So along with adjacency check with next lmb region, check for overlap should be done.

In case overlap is found, adjust and merge these two lmb region into one.

Quoted 7 lines
Quoted 2 lines

This is good! I wonder if this addresses the issue that has caused other platforms to need to set CONFIG_LMB_MAX_REGIONS=64 and I'm wondering if any of the other maintainers I've cc'd here can drop back to the default number of regions and then re-test their failure case? Thanks!

Quoted 6 lines

Hi Tom,

For the Apple M1 systems I don't think it makes sense to reduce the number of regions. These systems don't have TPL or SPL, have plenty of memory (at least 8GB) and are really fast. And we anticipate that the number of memory regions will only grow in the future.

Quoted 6 lines

This partly gets at what I see as part of the problem. It's been unclear to me if it's reasonable to just bump the default to 64 for aarch64 (and riscv?) because the problem is that 16 is truly too small, or given what we're doing we still don't need a ton of LMB regions as at the end of the day things should be coalesced in to just a few and it's been an underlying bug in an area everyone assumed wasn't buggy.

Did we observed overlapped memory regions added in lmb region ?

which leads to increase in default number of regions.