diff mbox series

[1/2] memblock: Differentiate regions overlap from both regions being the same

Message ID 20230406151429.524591-2-tanure@linux.com (mailing list archive)
State Superseded
Headers show
Series Fix Random Kernel panic from when fail to reserve memory | expand

Commit Message

Lucas Tanure April 6, 2023, 3:14 p.m. UTC
Add support for memblock_addrs_overlap to return a different value when
both regions are exactly the same region, where base and size are equal
between the regions.

Signed-off-by: Lucas Tanure <tanure@linux.com>
---
 include/linux/memblock.h | 18 +++++++++++++++---
 mm/memblock.c            | 37 ++++++++++++++++++++++++-------------
 2 files changed, 39 insertions(+), 16 deletions(-)
diff mbox series

Patch

diff --git a/include/linux/memblock.h b/include/linux/memblock.h
index 50ad19662a32..c7ba8b01a637 100644
--- a/include/linux/memblock.h
+++ b/include/linux/memblock.h
@@ -49,6 +49,18 @@  enum memblock_flags {
 	MEMBLOCK_DRIVER_MANAGED = 0x8,	/* always detected via a driver */
 };
 
+/**
+ * enum memblock_overlap_type - result of comparison between two memory regions
+ * @MEMBLOCK_NO_OVERLAPS: there is no overlap between the two regions
+ * @MEMBLOCK_OVERLAPS: the two regions overlap each other, but are not the same
+ * @MEMBLOCK_EQUAL: both bases and sizes are equal, so the two regions are exactly the same
+ */
+enum memblock_overlap_type {
+	MEMBLOCK_NO_OVERLAPS,
+	MEMBLOCK_OVERLAPS,
+	MEMBLOCK_EQUAL,
+};
+
 /**
  * struct memblock_region - represents a memory region
  * @base: base address of the region
@@ -118,8 +130,8 @@  int memblock_reserve(phys_addr_t base, phys_addr_t size);
 int memblock_physmem_add(phys_addr_t base, phys_addr_t size);
 #endif
 void memblock_trim_memory(phys_addr_t align);
-bool memblock_overlaps_region(struct memblock_type *type,
-			      phys_addr_t base, phys_addr_t size);
+unsigned int memblock_overlaps_region(struct memblock_type *type,
+				      phys_addr_t base, phys_addr_t size);
 int memblock_mark_hotplug(phys_addr_t base, phys_addr_t size);
 int memblock_clear_hotplug(phys_addr_t base, phys_addr_t size);
 int memblock_mark_mirror(phys_addr_t base, phys_addr_t size);
@@ -486,7 +498,7 @@  bool memblock_is_memory(phys_addr_t addr);
 bool memblock_is_map_memory(phys_addr_t addr);
 bool memblock_is_region_memory(phys_addr_t base, phys_addr_t size);
 bool memblock_is_reserved(phys_addr_t addr);
-bool memblock_is_region_reserved(phys_addr_t base, phys_addr_t size);
+unsigned int memblock_is_region_reserved(phys_addr_t base, phys_addr_t size);
 
 void memblock_dump_all(void);
 
diff --git a/mm/memblock.c b/mm/memblock.c
index 25fd0626a9e7..948cc1bc3edf 100644
--- a/mm/memblock.c
+++ b/mm/memblock.c
@@ -175,24 +175,33 @@  static inline phys_addr_t memblock_cap_size(phys_addr_t base, phys_addr_t *size)
 /*
  * Address comparison utilities
  */
-static unsigned long __init_memblock memblock_addrs_overlap(phys_addr_t base1, phys_addr_t size1,
-				       phys_addr_t base2, phys_addr_t size2)
+static unsigned int __init_memblock memblock_addrs_overlap(phys_addr_t base1, phys_addr_t size1,
+							   phys_addr_t base2, phys_addr_t size2)
 {
-	return ((base1 < (base2 + size2)) && (base2 < (base1 + size1)));
+	if (base1 == base2 && size1 == size2)
+		return MEMBLOCK_EQUAL;
+
+	if ((base1 < (base2 + size2)) && (base2 < (base1 + size1)))
+		return MEMBLOCK_OVERLAPS;
+
+	return MEMBLOCK_NO_OVERLAPS;
 }
 
-bool __init_memblock memblock_overlaps_region(struct memblock_type *type,
-					phys_addr_t base, phys_addr_t size)
+unsigned int __init_memblock memblock_overlaps_region(struct memblock_type *type,
+						      phys_addr_t base, phys_addr_t size)
 {
-	unsigned long i;
+	unsigned long i, ret;
 
 	memblock_cap_size(base, &size);
 
-	for (i = 0; i < type->cnt; i++)
-		if (memblock_addrs_overlap(base, size, type->regions[i].base,
-					   type->regions[i].size))
-			break;
-	return i < type->cnt;
+	for (i = 0; i < type->cnt; i++) {
+		ret = memblock_addrs_overlap(base, size, type->regions[i].base,
+					     type->regions[i].size);
+		if (ret)
+			return ret;
+	}
+
+	return MEMBLOCK_NO_OVERLAPS;
 }
 
 /**
@@ -1857,9 +1866,11 @@  bool __init_memblock memblock_is_region_memory(phys_addr_t base, phys_addr_t siz
  * memory block.
  *
  * Return:
- * True if they intersect, false if not.
+ * MEMBLOCK_NO_OVERLAPS if there is no intersection,
+ * MEMBLOCK_OVERLAPS if they only intersect,
+ * MEMBLOCK_EQUAL if the region matches base and size to an reserved memory.
  */
-bool __init_memblock memblock_is_region_reserved(phys_addr_t base, phys_addr_t size)
+unsigned int __init_memblock memblock_is_region_reserved(phys_addr_t base, phys_addr_t size)
 {
 	return memblock_overlaps_region(&memblock.reserved, base, size);
 }