Skip to contentSkip to conversation

lmb: Tweaks as discussed

RFC PATCH In review Simon Glass 2 participants 10 messages Jul 30, 2024 – Aug 1, 2024 Library code

lore
Share & tools
lore
#0 lore raw

cover lmb: Tweaks as discussed

This series adds a few tweaks to Sugosh's lmb series to show:
- save/restore of test stage
- putting lmb in a struct
- small code-size improvement

Simon Glass (4):
lmb: Put internal details in a struct
lmb: Tighten up the code in lmb_add_region_flags()
lmb: Split init into two functions
lmb: Ensure that tests don't change lmb state

 common/board_r.c  |   4 +-
 include/lmb.h     |  53 +++++++------
 lib/lmb.c         | 193 +++++++++++++++++++---------------------------
 test/cmd/bdinfo.c |   8 +-
 test/lmb_ut.c     |  54 +++++++++----
 5 files changed, 154 insertions(+), 158 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

RFC PATCH 1/4 lmb: Put internal details in a struct

Rather than exporting the internal details of lmb, create a struct which holds the lists.

---

 include/lmb.h     | 10 ++++++++-
 lib/lmb.c         | 56 +++++++++++++++++++++++++----------------------
 test/cmd/bdinfo.c |  8 +++----
 3 files changed, 43 insertions(+), 31 deletions(-)
include/lmb.h +9 -1
index 25a984e14f3..19231439c34 100644
@@ -13,7 +13,13 @@
  * Copyright (C) 2001 Peter Bergner, IBM Corp.
  */
 
-struct alist;
+#include <alist.h>
+
+struct lmb {
+	struct alist free_mem;
+	struct alist used_mem;
+};
+
 
 /**
  * enum lmb_flags - definition of memory region attributes
@@ -121,6 +127,8 @@ int lmb_mem_regions_init(struct alist **mem_lst, struct alist **used_lst,
  */
 void lmb_mem_regions_uninit(struct alist *mem_lst, struct alist *used_lst);
 
+struct lmb *lmb_get(void);
+
 #endif /* __KERNEL__ */
 
 #endif /* _LINUX_LMB_H */
lib/lmb.c +30 -26

154 lines not on this page · raw message

test/cmd/bdinfo.c +4 -4
index 3184aaf629f..e18e4701bbe 100644
@@ -129,12 +129,12 @@ static int lmb_test_dump_region(struct unit_test_state *uts,
 
 static int lmb_test_dump_all(struct unit_test_state *uts)
 {
-	extern struct alist lmb_free_mem;
-	extern struct alist lmb_used_mem;
+	struct lmb *lmb = lmb_get();
 
+	ut_assertnonnull(lmb);
 	ut_assert_nextline("lmb_dump_all:");
-	ut_assertok(lmb_test_dump_region(uts, &lmb_free_mem, "memory"));
-	ut_assertok(lmb_test_dump_region(uts, &lmb_used_mem, "reserved"));
+	ut_assertok(lmb_test_dump_region(uts, &lmb->free_mem, "memory"));
+	ut_assertok(lmb_test_dump_region(uts, &lmb->used_mem, "reserved"));
 
 	return 0;
 }
Signature · 1 line
2.34.1
#2 lore raw

RFC PATCH 2/4 lmb: Tighten up the code in lmb_add_region_flags()

In reply to Simon Glass

This function has more special cases than it needs. Simplify it to reduce code size and complexity.

---

 lib/lmb.c | 57 +++++++++++++++++++------------------------------------
 1 file changed, 19 insertions(+), 38 deletions(-)
lib/lmb.c +19 -38
index c11ce308c5b..83b060a2f4d 100644
@@ -396,14 +396,6 @@ static long lmb_add_region_flags(struct alist *lmb_rgn_lst, phys_addr_t base,
 	if (alist_err(lmb_rgn_lst))
 		return -1;
 
-	if (alist_empty(lmb_rgn_lst)) {
-		rgn[0].base = base;
-		rgn[0].size = size;
-		rgn[0].flags = flags;
-		lmb_rgn_lst->count = 1;
-		return 0;
-	}
-
 	/* First try and coalesce this LMB with another. */
 	for (i = 0; i < lmb_rgn_lst->count; i++) {
 		phys_addr_t rgnbase = rgn[i].base;
@@ -448,50 +440,39 @@ static long lmb_add_region_flags(struct alist *lmb_rgn_lst, phys_addr_t base,
 		}
 	}
 
-	if (i < lmb_rgn_lst->count - 1 &&
-	    rgn[i].flags == rgn[i + 1].flags) {
-		if (lmb_regions_adjacent(lmb_rgn_lst, i, i + 1)) {
-			lmb_coalesce_regions(lmb_rgn_lst, i, i + 1);
-			coalesced++;
-		} else if (lmb_regions_overlap(lmb_rgn_lst, i, i + 1)) {
-			/* fix overlapping area */
-			lmb_fix_over_lap_regions(lmb_rgn_lst, i, i + 1);
-			coalesced++;
+	if (lmb_rgn_lst->count && i < lmb_rgn_lst->count - 1) {
+		rgn = lmb_rgn_lst->data;
+		if (rgn[i].flags == rgn[i + 1].flags) {
+			if (lmb_regions_adjacent(lmb_rgn_lst, i, i + 1)) {
+				lmb_coalesce_regions(lmb_rgn_lst, i, i + 1);
+				coalesced++;
+			} else if (lmb_regions_overlap(lmb_rgn_lst, i, i + 1)) {
+				/* fix overlapping area */
+				lmb_fix_over_lap_regions(lmb_rgn_lst, i, i + 1);
+				coalesced++;
+			}
 		}
 	}
 
 	if (coalesced)
 		return coalesced;
 
-	if (alist_full(lmb_rgn_lst)) {
-		if (!alist_expand_by(lmb_rgn_lst, lmb_rgn_lst->alloc * 2))
-			return -1;
-		else
-			rgn = lmb_rgn_lst->data;
-	}
+	if (!alist_add_placeholder(lmb_rgn_lst))
+		return -1;
+	rgn = lmb_rgn_lst->data;
 
 	/* Couldn't coalesce the LMB, so add it to the sorted table. */
 	for (i = lmb_rgn_lst->count - 1; i >= 0; i--) {
-		if (base < rgn[i].base) {
-			rgn[i + 1].base = rgn[i].base;
-			rgn[i + 1].size = rgn[i].size;
-			rgn[i + 1].flags = rgn[i].flags;
+		if (i && base < rgn[i - 1].base) {
+			rgn[i] = rgn[i - 1];
 		} else {
-			rgn[i + 1].base = base;
-			rgn[i + 1].size = size;
-			rgn[i + 1].flags = flags;
+			rgn[i].base = base;
+			rgn[i].size = size;
+			rgn[i].flags = flags;
 			break;
 		}
 	}
 
-	if (base < rgn[0].base) {
-		rgn[0].base = base;
-		rgn[0].size = size;
-		rgn[0].flags = flags;
-	}
-
-	lmb_rgn_lst->count++;
-
 	return 0;
 }
 
Signature · 1 line
2.34.1
#3 lore raw

Re: [RFC PATCH 2/4] lmb: Tighten up the code in lmb_add_region_flags()

Quoted 58 lines · Simon Glass

This function has more special cases than it needs. Simplify it to reduce code size and complexity.

---

 lib/lmb.c | 57 +++++++++++++++++++------------------------------------
 1 file changed, 19 insertions(+), 38 deletions(-)
lib/lmb.c +11 -17
index c11ce308c5b..83b060a2f4d 100644
@@ -396,14 +396,6 @@ static long lmb_add_region_flags(struct alist *lmb_rgn_lst, phys_addr_t base,
        if (alist_err(lmb_rgn_lst))
                return -1;
 
-       if (alist_empty(lmb_rgn_lst)) {
-               rgn[0].base = base;
-               rgn[0].size = size;
-               rgn[0].flags = flags;
-               lmb_rgn_lst->count = 1;
-               return 0;
-       }
-
        /* First try and coalesce this LMB with another. */
        for (i = 0; i < lmb_rgn_lst->count; i++) {
                phys_addr_t rgnbase = rgn[i].base;
@@ -448,50 +440,39 @@ static long lmb_add_region_flags(struct alist *lmb_rgn_lst, phys_addr_t base,
                }
        }
 
-       if (i < lmb_rgn_lst->count - 1 &&
-           rgn[i].flags == rgn[i + 1].flags) {
-               if (lmb_regions_adjacent(lmb_rgn_lst, i, i + 1)) {
-                       lmb_coalesce_regions(lmb_rgn_lst, i, i + 1);
-                       coalesced++;
-               } else if (lmb_regions_overlap(lmb_rgn_lst, i, i + 1)) {
-                       /* fix overlapping area */
-                       lmb_fix_over_lap_regions(lmb_rgn_lst, i, i + 1);
-                       coalesced++;
+       if (lmb_rgn_lst->count && i < lmb_rgn_lst->count - 1) {
+               rgn = lmb_rgn_lst->data;
+               if (rgn[i].flags == rgn[i + 1].flags) {
+                       if (lmb_regions_adjacent(lmb_rgn_lst, i, i + 1)) {
+                               lmb_coalesce_regions(lmb_rgn_lst, i, i + 1);
+                               coalesced++;
+                       } else if (lmb_regions_overlap(lmb_rgn_lst, i, i + 1)) {
+                               /* fix overlapping area */
+                               lmb_fix_over_lap_regions(lmb_rgn_lst, i, i + 1);
+                               coalesced++;
+                       }
                }
        }
 
        if (coalesced)
                return coalesced;

lib/lmb.c static long lmb_add_region_flags(struct alist *lmb_rgn_lst, phys_addr_t base, · line 462

Quoted 10 lines
lib/lmb.c +3 -6
 
-       if (alist_full(lmb_rgn_lst)) {
-               if (!alist_expand_by(lmb_rgn_lst, lmb_rgn_lst->alloc * 2))
-                       return -1;
-               else
-                       rgn = lmb_rgn_lst->data;
-       }
+       if (!alist_add_placeholder(lmb_rgn_lst))
+               return -1;
+       rgn = lmb_rgn_lst->data;

I think the above should have a check for alist_full(), and then add to the list if full. Else we simply go on adding to the list on every new node that gets added.

Quoted 9 lines
lib/lmb.c +2 -4
        /* Couldn't coalesce the LMB, so add it to the sorted table. */
        for (i = lmb_rgn_lst->count - 1; i >= 0; i--) {
-               if (base < rgn[i].base) {
-                       rgn[i + 1].base = rgn[i].base;
-                       rgn[i + 1].size = rgn[i].size;
-                       rgn[i + 1].flags = rgn[i].flags;
+               if (i && base < rgn[i - 1].base) {
+                       rgn[i] = rgn[i - 1];

lib/lmb.c static long lmb_add_region_flags(struct alist *lmb_rgn_lst, phys_addr_t base, · line 474

Quoted 10 lines
lib/lmb.c +3 -3
                } else {
-                       rgn[i + 1].base = base;
-                       rgn[i + 1].size = size;
-                       rgn[i + 1].flags = flags;
+                       rgn[i].base = base;
+                       rgn[i].size = size;
+                       rgn[i].flags = flags;
                        break;
                }
        }

With the logic that you put above, should the loop not have 'i' initialised to lmb_rgn_lst-count? I mean

for (i = lmb_rgn_lst->count; i >= 0; i--)

Else we are overwriting one region?

-sughosh

Quoted 15 lines
lib/lmb.c +0 -9
-       if (base < rgn[0].base) {
-               rgn[0].base = base;
-               rgn[0].size = size;
-               rgn[0].flags = flags;
-       }
-
-       lmb_rgn_lst->count++;
-
        return 0;
 }
 
--

2.34.1

#4 lore raw

Re: [RFC PATCH 2/4] lmb: Tighten up the code in lmb_add_region_flags()

Quoted 95 lines · Sughosh Ganu
Quoted 58 lines · Simon Glass

This function has more special cases than it needs. Simplify it to reduce code size and complexity.

---

 lib/lmb.c | 57 +++++++++++++++++++------------------------------------
 1 file changed, 19 insertions(+), 38 deletions(-)
lib/lmb.c +11 -17
index c11ce308c5b..83b060a2f4d 100644
@@ -396,14 +396,6 @@ static long lmb_add_region_flags(struct alist *lmb_rgn_lst, phys_addr_t base,
        if (alist_err(lmb_rgn_lst))
                return -1;
 
-       if (alist_empty(lmb_rgn_lst)) {
-               rgn[0].base = base;
-               rgn[0].size = size;
-               rgn[0].flags = flags;
-               lmb_rgn_lst->count = 1;
-               return 0;
-       }
-
        /* First try and coalesce this LMB with another. */
        for (i = 0; i < lmb_rgn_lst->count; i++) {
                phys_addr_t rgnbase = rgn[i].base;
@@ -448,50 +440,39 @@ static long lmb_add_region_flags(struct alist *lmb_rgn_lst, phys_addr_t base,
                }
        }
 
-       if (i < lmb_rgn_lst->count - 1 &&
-           rgn[i].flags == rgn[i + 1].flags) {
-               if (lmb_regions_adjacent(lmb_rgn_lst, i, i + 1)) {
-                       lmb_coalesce_regions(lmb_rgn_lst, i, i + 1);
-                       coalesced++;
-               } else if (lmb_regions_overlap(lmb_rgn_lst, i, i + 1)) {
-                       /* fix overlapping area */
-                       lmb_fix_over_lap_regions(lmb_rgn_lst, i, i + 1);
-                       coalesced++;
+       if (lmb_rgn_lst->count && i < lmb_rgn_lst->count - 1) {
+               rgn = lmb_rgn_lst->data;
+               if (rgn[i].flags == rgn[i + 1].flags) {
+                       if (lmb_regions_adjacent(lmb_rgn_lst, i, i + 1)) {
+                               lmb_coalesce_regions(lmb_rgn_lst, i, i + 1);
+                               coalesced++;
+                       } else if (lmb_regions_overlap(lmb_rgn_lst, i, i + 1)) {
+                               /* fix overlapping area */
+                               lmb_fix_over_lap_regions(lmb_rgn_lst, i, i + 1);
+                               coalesced++;
+                       }
                }
        }
 
        if (coalesced)
                return coalesced;

lib/lmb.c static long lmb_add_region_flags(struct alist *lmb_rgn_lst, phys_addr_t base, · line 462

Quoted 10 lines
lib/lmb.c +3 -6
 
-       if (alist_full(lmb_rgn_lst)) {
-               if (!alist_expand_by(lmb_rgn_lst, lmb_rgn_lst->alloc * 2))
-                       return -1;
-               else
-                       rgn = lmb_rgn_lst->data;
-       }
+       if (!alist_add_placeholder(lmb_rgn_lst))
+               return -1;
+       rgn = lmb_rgn_lst->data;

I think the above should have a check for alist_full(), and then add to the list if full. Else we simply go on adding to the list on every new node that gets added.

Quoted 9 lines
lib/lmb.c +2 -4
        /* Couldn't coalesce the LMB, so add it to the sorted table. */
        for (i = lmb_rgn_lst->count - 1; i >= 0; i--) {
-               if (base < rgn[i].base) {
-                       rgn[i + 1].base = rgn[i].base;
-                       rgn[i + 1].size = rgn[i].size;
-                       rgn[i + 1].flags = rgn[i].flags;
+               if (i && base < rgn[i - 1].base) {
+                       rgn[i] = rgn[i - 1];

lib/lmb.c static long lmb_add_region_flags(struct alist *lmb_rgn_lst, phys_addr_t base, · line 474

Quoted 10 lines
lib/lmb.c +3 -3
                } else {
-                       rgn[i + 1].base = base;
-                       rgn[i + 1].size = size;
-                       rgn[i + 1].flags = flags;
+                       rgn[i].base = base;
+                       rgn[i].size = size;
+                       rgn[i].flags = flags;
                        break;
                }
        }
Quoted 6 lines

With the logic that you put above, should the loop not have 'i' initialised to lmb_rgn_lst-count? I mean

for (i = lmb_rgn_lst->count; i >= 0; i--)

Else we are overwriting one region?

To answer my question, yes we do need to change the loop start to accommodate the new node. This code is better, thanks for the suggestion!

-sughosh

Quoted 18 lines

-sughosh

Quoted 15 lines
lib/lmb.c +0 -9
-       if (base < rgn[0].base) {
-               rgn[0].base = base;
-               rgn[0].size = size;
-               rgn[0].flags = flags;
-       }
-
-       lmb_rgn_lst->count++;
-
        return 0;
 }
 
--

2.34.1

#5 lore raw

Re: [RFC PATCH 2/4] lmb: Tighten up the code in lmb_add_region_flags()

In reply to Sughosh Ganu

Hi Sughosh,

Quoted 61 lines · Sughosh Ganu
Quoted 59 lines · Simon Glass

This function has more special cases than it needs. Simplify it to reduce code size and complexity.

---

 lib/lmb.c | 57 +++++++++++++++++++------------------------------------
 1 file changed, 19 insertions(+), 38 deletions(-)
lib/lmb.c +11 -17
index c11ce308c5b..83b060a2f4d 100644
@@ -396,14 +396,6 @@ static long lmb_add_region_flags(struct alist *lmb_rgn_lst, phys_addr_t base,
        if (alist_err(lmb_rgn_lst))
                return -1;
 
-       if (alist_empty(lmb_rgn_lst)) {
-               rgn[0].base = base;
-               rgn[0].size = size;
-               rgn[0].flags = flags;
-               lmb_rgn_lst->count = 1;
-               return 0;
-       }
-
        /* First try and coalesce this LMB with another. */
        for (i = 0; i < lmb_rgn_lst->count; i++) {
                phys_addr_t rgnbase = rgn[i].base;
@@ -448,50 +440,39 @@ static long lmb_add_region_flags(struct alist *lmb_rgn_lst, phys_addr_t base,
                }
        }
 
-       if (i < lmb_rgn_lst->count - 1 &&
-           rgn[i].flags == rgn[i + 1].flags) {
-               if (lmb_regions_adjacent(lmb_rgn_lst, i, i + 1)) {
-                       lmb_coalesce_regions(lmb_rgn_lst, i, i + 1);
-                       coalesced++;
-               } else if (lmb_regions_overlap(lmb_rgn_lst, i, i + 1)) {
-                       /* fix overlapping area */
-                       lmb_fix_over_lap_regions(lmb_rgn_lst, i, i + 1);
-                       coalesced++;
+       if (lmb_rgn_lst->count && i < lmb_rgn_lst->count - 1) {
+               rgn = lmb_rgn_lst->data;
+               if (rgn[i].flags == rgn[i + 1].flags) {
+                       if (lmb_regions_adjacent(lmb_rgn_lst, i, i + 1)) {
+                               lmb_coalesce_regions(lmb_rgn_lst, i, i + 1);
+                               coalesced++;
+                       } else if (lmb_regions_overlap(lmb_rgn_lst, i, i + 1)) {
+                               /* fix overlapping area */
+                               lmb_fix_over_lap_regions(lmb_rgn_lst, i, i + 1);
+                               coalesced++;
+                       }
                }
        }
 
        if (coalesced)
                return coalesced;
 
Quoted 13 lines

lib/lmb.c static long lmb_add_region_flags(struct alist *lmb_rgn_lst, phys_addr_t base, · line 462

Quoted 9 lines
lib/lmb.c +3 -6
-       if (alist_full(lmb_rgn_lst)) {
-               if (!alist_expand_by(lmb_rgn_lst, lmb_rgn_lst->alloc * 2))
-                       return -1;
-               else
-                       rgn = lmb_rgn_lst->data;
-       }
+       if (!alist_add_placeholder(lmb_rgn_lst))
+               return -1;
+       rgn = lmb_rgn_lst->data;

I think the above should have a check for alist_full(), and then add to the list if full. Else we simply go on adding to the list on every new node that gets added.

At this point in the function, we know that we are adding a new entry. We could have an anlist_insert() perhaps?

Quoted 21 lines
Quoted 10 lines
lib/lmb.c +2 -4
        /* Couldn't coalesce the LMB, so add it to the sorted table. */
        for (i = lmb_rgn_lst->count - 1; i >= 0; i--) {
-               if (base < rgn[i].base) {
-                       rgn[i + 1].base = rgn[i].base;
-                       rgn[i + 1].size = rgn[i].size;
-                       rgn[i + 1].flags = rgn[i].flags;
+               if (i && base < rgn[i - 1].base) {
+                       rgn[i] = rgn[i - 1];

lib/lmb.c static long lmb_add_region_flags(struct alist *lmb_rgn_lst, phys_addr_t base, · line 474

Quoted 10 lines
lib/lmb.c +3 -3
                } else {
-                       rgn[i + 1].base = base;
-                       rgn[i + 1].size = size;
-                       rgn[i + 1].flags = flags;
+                       rgn[i].base = base;
+                       rgn[i].size = size;
+                       rgn[i].flags = flags;
                        break;
                }
        }
Quoted 6 lines

With the logic that you put above, should the loop not have 'i' initialised to lmb_rgn_lst-count? I mean

for (i = lmb_rgn_lst->count; i >= 0; i--)

Else we are overwriting one region?

It starts by doing the assignment rgn[count - 1 + 1] = rgn[count - 1], which I believe is correct?

Quoted 12 lines
Quoted 12 lines
lib/lmb.c +0 -8
-       if (base < rgn[0].base) {
-               rgn[0].base = base;
-               rgn[0].size = size;
-               rgn[0].flags = flags;
-       }
-
-       lmb_rgn_lst->count++;
-
        return 0;
 }
 
Quoted 3 lines
Quoted 3 lines
lib/lmb.c +0 -1
--

2.34.1

Regards,
SImon

#6 lore raw

Re: [RFC PATCH 2/4] lmb: Tighten up the code in lmb_add_region_flags()

Quoted 106 lines · Simon Glass

Hi Sughosh,

Quoted 61 lines · Sughosh Ganu
Quoted 59 lines · Simon Glass

This function has more special cases than it needs. Simplify it to reduce code size and complexity.

---

 lib/lmb.c | 57 +++++++++++++++++++------------------------------------
 1 file changed, 19 insertions(+), 38 deletions(-)
lib/lmb.c +11 -17
index c11ce308c5b..83b060a2f4d 100644
@@ -396,14 +396,6 @@ static long lmb_add_region_flags(struct alist *lmb_rgn_lst, phys_addr_t base,
        if (alist_err(lmb_rgn_lst))
                return -1;
 
-       if (alist_empty(lmb_rgn_lst)) {
-               rgn[0].base = base;
-               rgn[0].size = size;
-               rgn[0].flags = flags;
-               lmb_rgn_lst->count = 1;
-               return 0;
-       }
-
        /* First try and coalesce this LMB with another. */
        for (i = 0; i < lmb_rgn_lst->count; i++) {
                phys_addr_t rgnbase = rgn[i].base;
@@ -448,50 +440,39 @@ static long lmb_add_region_flags(struct alist *lmb_rgn_lst, phys_addr_t base,
                }
        }
 
-       if (i < lmb_rgn_lst->count - 1 &&
-           rgn[i].flags == rgn[i + 1].flags) {
-               if (lmb_regions_adjacent(lmb_rgn_lst, i, i + 1)) {
-                       lmb_coalesce_regions(lmb_rgn_lst, i, i + 1);
-                       coalesced++;
-               } else if (lmb_regions_overlap(lmb_rgn_lst, i, i + 1)) {
-                       /* fix overlapping area */
-                       lmb_fix_over_lap_regions(lmb_rgn_lst, i, i + 1);
-                       coalesced++;
+       if (lmb_rgn_lst->count && i < lmb_rgn_lst->count - 1) {
+               rgn = lmb_rgn_lst->data;
+               if (rgn[i].flags == rgn[i + 1].flags) {
+                       if (lmb_regions_adjacent(lmb_rgn_lst, i, i + 1)) {
+                               lmb_coalesce_regions(lmb_rgn_lst, i, i + 1);
+                               coalesced++;
+                       } else if (lmb_regions_overlap(lmb_rgn_lst, i, i + 1)) {
+                               /* fix overlapping area */
+                               lmb_fix_over_lap_regions(lmb_rgn_lst, i, i + 1);
+                               coalesced++;
+                       }
                }
        }
 
        if (coalesced)
                return coalesced;
 
Quoted 13 lines

lib/lmb.c static long lmb_add_region_flags(struct alist *lmb_rgn_lst, phys_addr_t base, · line 462

Quoted 9 lines
lib/lmb.c +3 -6
-       if (alist_full(lmb_rgn_lst)) {
-               if (!alist_expand_by(lmb_rgn_lst, lmb_rgn_lst->alloc * 2))
-                       return -1;
-               else
-                       rgn = lmb_rgn_lst->data;
-       }
+       if (!alist_add_placeholder(lmb_rgn_lst))
+               return -1;
+       rgn = lmb_rgn_lst->data;

I think the above should have a check for alist_full(), and then add to the list if full. Else we simply go on adding to the list on every new node that gets added.

At this point in the function, we know that we are adding a new entry. We could have an anlist_insert() perhaps?

Quoted 24 lines
Quoted 10 lines
lib/lmb.c +2 -4
        /* Couldn't coalesce the LMB, so add it to the sorted table. */
        for (i = lmb_rgn_lst->count - 1; i >= 0; i--) {
-               if (base < rgn[i].base) {
-                       rgn[i + 1].base = rgn[i].base;
-                       rgn[i + 1].size = rgn[i].size;
-                       rgn[i + 1].flags = rgn[i].flags;
+               if (i && base < rgn[i - 1].base) {
+                       rgn[i] = rgn[i - 1];

lib/lmb.c static long lmb_add_region_flags(struct alist *lmb_rgn_lst, phys_addr_t base, · line 474

Quoted 10 lines
lib/lmb.c +3 -3
                } else {
-                       rgn[i + 1].base = base;
-                       rgn[i + 1].size = size;
-                       rgn[i + 1].flags = flags;
+                       rgn[i].base = base;
+                       rgn[i].size = size;
+                       rgn[i].flags = flags;
                        break;
                }
        }

With the logic that you put above, should the loop not have 'i' initialised to lmb_rgn_lst-count? I mean

Quoted 6 lines
Quoted 3 lines

for (i = lmb_rgn_lst->count; i >= 0; i--)

Else we are overwriting one region?

It starts by doing the assignment rgn[count - 1 + 1] = rgn[count - 1], which I believe is correct?

That was the earlier logic, but now this is being replaced by

+ if (i && base < rgn[i - 1].base) {
+ rgn[i] = rgn[i - 1];

Which means 'i' should be initialised to lmb_rgn_cnt->count? If you see the patch, the for loop initialisation is not being changed.

-sughosh

Quoted 19 lines
Quoted 13 lines
Quoted 13 lines
lib/lmb.c +0 -8
-       if (base < rgn[0].base) {
-               rgn[0].base = base;
-               rgn[0].size = size;
-               rgn[0].flags = flags;
-       }
-
-       lmb_rgn_lst->count++;
-
        return 0;
 }
 
Quoted 3 lines
Quoted 3 lines
lib/lmb.c +0 -1
--

2.34.1

Regards,
SImon

#7 lore raw

Re: [RFC PATCH 2/4] lmb: Tighten up the code in lmb_add_region_flags()

Hi Sughosh,

Quoted 115 lines · Sughosh Ganu
Quoted 106 lines · Simon Glass

Hi Sughosh,

Quoted 61 lines · Sughosh Ganu
Quoted 59 lines · Simon Glass

This function has more special cases than it needs. Simplify it to reduce code size and complexity.

---

 lib/lmb.c | 57 +++++++++++++++++++------------------------------------
 1 file changed, 19 insertions(+), 38 deletions(-)
lib/lmb.c +11 -17
index c11ce308c5b..83b060a2f4d 100644
@@ -396,14 +396,6 @@ static long lmb_add_region_flags(struct alist *lmb_rgn_lst, phys_addr_t base,
        if (alist_err(lmb_rgn_lst))
                return -1;
 
-       if (alist_empty(lmb_rgn_lst)) {
-               rgn[0].base = base;
-               rgn[0].size = size;
-               rgn[0].flags = flags;
-               lmb_rgn_lst->count = 1;
-               return 0;
-       }
-
        /* First try and coalesce this LMB with another. */
        for (i = 0; i < lmb_rgn_lst->count; i++) {
                phys_addr_t rgnbase = rgn[i].base;
@@ -448,50 +440,39 @@ static long lmb_add_region_flags(struct alist *lmb_rgn_lst, phys_addr_t base,
                }
        }
 
-       if (i < lmb_rgn_lst->count - 1 &&
-           rgn[i].flags == rgn[i + 1].flags) {
-               if (lmb_regions_adjacent(lmb_rgn_lst, i, i + 1)) {
-                       lmb_coalesce_regions(lmb_rgn_lst, i, i + 1);
-                       coalesced++;
-               } else if (lmb_regions_overlap(lmb_rgn_lst, i, i + 1)) {
-                       /* fix overlapping area */
-                       lmb_fix_over_lap_regions(lmb_rgn_lst, i, i + 1);
-                       coalesced++;
+       if (lmb_rgn_lst->count && i < lmb_rgn_lst->count - 1) {
+               rgn = lmb_rgn_lst->data;
+               if (rgn[i].flags == rgn[i + 1].flags) {
+                       if (lmb_regions_adjacent(lmb_rgn_lst, i, i + 1)) {
+                               lmb_coalesce_regions(lmb_rgn_lst, i, i + 1);
+                               coalesced++;
+                       } else if (lmb_regions_overlap(lmb_rgn_lst, i, i + 1)) {
+                               /* fix overlapping area */
+                               lmb_fix_over_lap_regions(lmb_rgn_lst, i, i + 1);
+                               coalesced++;
+                       }
                }
        }
 
        if (coalesced)
                return coalesced;
 
Quoted 13 lines

lib/lmb.c static long lmb_add_region_flags(struct alist *lmb_rgn_lst, phys_addr_t base, · line 462

Quoted 9 lines
lib/lmb.c +3 -6
-       if (alist_full(lmb_rgn_lst)) {
-               if (!alist_expand_by(lmb_rgn_lst, lmb_rgn_lst->alloc * 2))
-                       return -1;
-               else
-                       rgn = lmb_rgn_lst->data;
-       }
+       if (!alist_add_placeholder(lmb_rgn_lst))
+               return -1;
+       rgn = lmb_rgn_lst->data;

I think the above should have a check for alist_full(), and then add to the list if full. Else we simply go on adding to the list on every new node that gets added.

At this point in the function, we know that we are adding a new entry. We could have an anlist_insert() perhaps?

Quoted 24 lines
Quoted 10 lines
lib/lmb.c +2 -4
        /* Couldn't coalesce the LMB, so add it to the sorted table. */
        for (i = lmb_rgn_lst->count - 1; i >= 0; i--) {
-               if (base < rgn[i].base) {
-                       rgn[i + 1].base = rgn[i].base;
-                       rgn[i + 1].size = rgn[i].size;
-                       rgn[i + 1].flags = rgn[i].flags;
+               if (i && base < rgn[i - 1].base) {
+                       rgn[i] = rgn[i - 1];

lib/lmb.c static long lmb_add_region_flags(struct alist *lmb_rgn_lst, phys_addr_t base, · line 474

Quoted 10 lines
lib/lmb.c +3 -3
                } else {
-                       rgn[i + 1].base = base;
-                       rgn[i + 1].size = size;
-                       rgn[i + 1].flags = flags;
+                       rgn[i].base = base;
+                       rgn[i].size = size;
+                       rgn[i].flags = flags;
                        break;
                }
        }

With the logic that you put above, should the loop not have 'i' initialised to lmb_rgn_lst-count? I mean

Quoted 6 lines
Quoted 3 lines

for (i = lmb_rgn_lst->count; i >= 0; i--)

Else we are overwriting one region?

It starts by doing the assignment rgn[count - 1 + 1] = rgn[count - 1], which I believe is correct?

Quoted 7 lines

That was the earlier logic, but now this is being replaced by

+ if (i && base < rgn[i - 1].base) {
+ rgn[i] = rgn[i - 1];

Which means 'i' should be initialised to lmb_rgn_cnt->count? If you see the patch, the for loop initialisation is not being changed.

OK, well I'll let you sort that out :-)

Regards,
Simon

#8 lore raw

RFC PATCH 3/4 lmb: Split init into two functions

In reply to Simon Glass

The normal case for initing lmb is to set up all the memory regions. Put that in a separate function, called lmb_init(). The other function is now only used for testing.

Note that tests never set up the memory regions, so we can drop that code.

---

 common/board_r.c |  4 +--
 include/lmb.h    | 13 +++++++
 lib/lmb.c        | 88 +++++++++++++++++++++++++-----------------------
 3 files changed, 59 insertions(+), 46 deletions(-)
common/board_r.c +1 -3
index eaf9b40ec02..9ce1b8cdd2b 100644
@@ -611,9 +611,7 @@ static init_fnc_t init_sequence_r[] = {
 #ifdef CONFIG_CLOCKS
 	set_cpu_clk_info, /* Setup clock information */
 #endif
-#if CONFIG_IS_ENABLED(LMB)
-	initr_lmb,
-#endif
+	lmb_init,
 #ifdef CONFIG_EFI_LOADER
 	efi_memory_init,
 #endif
include/lmb.h +13 -0
index 19231439c34..a2236a419fd 100644
@@ -96,6 +96,19 @@ void board_lmb_reserve(void);
 void arch_lmb_reserve(void);
 void arch_lmb_reserve_generic(ulong sp, ulong end, ulong align);
 
+#if CONFIG_IS_ENABLED(LMB)
+/**
+ * lmb_init() - Initialise the LMB memory
+ *
+ * Initialise the LMB-subsystem-related data structures.
+ *
+ * Return: 0 if OK, -ve on failure.
+ */
+int lmb_init(void);
+#else
+static int lmb_init(void) { return 0; }
+#endif
+
 /**
  * lmb_mem_regions_init() - Initialise the LMB memory
  * @mem_lst: Pointer to store location of free memory list
lib/lmb.c +45 -43

112 lines not on this page · raw message

Signature · 1 line
2.34.1
#9 lore raw

RFC PATCH 4/4 lmb: Ensure that tests don't change lmb state

In reply to Simon Glass

Tests should not change the lmb state, so provide a way to save and restore the state.

Note: The _norun tests can now become normal tests.

When tests fail, lmb is broken. This is probably fine. If it causes problems we could save/restore outside the test, using a UT_TESTF_ flag.

---

 include/lmb.h | 32 ++++++++---------------
 lib/lmb.c     | 70 ++++++++++++++++++---------------------------------
 test/lmb_ut.c | 54 +++++++++++++++++++++++++++------------
 3 files changed, 73 insertions(+), 83 deletions(-)
include/lmb.h +10 -22
index a2236a419fd..ed943b29234 100644
@@ -110,35 +110,23 @@ static int lmb_init(void) { return 0; }
 #endif
 
 /**
- * lmb_mem_regions_init() - Initialise the LMB memory
- * @mem_lst: Pointer to store location of free memory list
- * @used_lst: Pointer to store location of used memory list
- * @add_rsv_mem: flag to indicate if memory is to be added and reserved
+ * lmb_push() - Store existing lmb state and set up a new state
  *
- * Initialise the LMB subsystem related data structures. There are two
- * alloced lists that are initialised, one for the free memory, and one
- * for the used memory.
+ * This is only used for testing
  *
- * Initialise the two lists as part of board init during boot. When called
- * from a test, passes the pointers to the two lists to the caller. The
- * caller is then required to call the corresponding function to uninit
- * the lists.
- *
- * Return: 0 if OK, -ve on failure.
+ * @store: Place to store the current lmb state
+ * Return: 0 if OK, -ENOMEM if out of memory
  */
-int lmb_mem_regions_init(struct alist **mem_lst, struct alist **used_lst,
-			 bool add_rsv_mem);
+int lmb_push(struct lmb *store);
 
 /**
- * lmb_mem_regions_uninit() - Unitialise the lmb lists
- * @mem_lst: Pointer to store location of free memory list
- * @used_lst: Pointer to store location of used memory list
+ * lmb_pop() - Restore an old lmb state
+ *
+ * This is only used for testing
  *
- * Unitialise the LMB lists for free and used memory that was
- * initialised as part of the init function. Called when running
- * lmb test routines.
+ * @store: lmb state to restore
  */
-void lmb_mem_regions_uninit(struct alist *mem_lst, struct alist *used_lst);
+void lmb_pop(struct lmb *store);
 
 struct lmb *lmb_get(void);
 
lib/lmb.c +25 -45

97 lines not on this page · raw message

test/lmb_ut.c +38 -16

184 lines not on this page · raw message

Signature · 1 line
2.34.1