[PATCH v2] lib: sbi: Refresh the memregion count after sanitize_domain()
Nickolai Zeldovich
nickolai at csail.mit.edu
Sun Nov 15 18:14:52 PST 2026
From: Nickolai Zeldovich <nickolai.zeldovich at gmail.com>
add_memregion() counts the regions once before its sort-and-merge loop,
but sanitize_domain() in that loop removes regions covered by another
one. The merge step's sbi_memmove() then uses the stale count and
copies slots past the terminating entry. Those slots still hold copies
of the old terminator and lie inside regions[], so the table comes out
right today, but only by accident.
Recount the regions after each sanitize_domain().
Fixes: 0d5629381731 ("lib: sbi: Fix sbi_domain_root_add_memregion() for merging memregions")
Signed-off-by: Nickolai Zeldovich <nickolai at csail.mit.edu>
---
Notes:
Changes in v2:
- Rebased onto the add_memregion() rework (145bd4cc, c4f098bf), where
the count is the local memregs_count. v1 was based on the master of
the time (8740e0a3); the root_memregs_count it changed was the local
variable added by de376252, not the removed global.
- Shorter source comment and message.
v1: https://lore.kernel.org/r/20261006193637.735638-1-nickolai@csail.mit.edu
Tested on QEMU virt with a test-only uart8250 change that also adds a
64 KiB range covering the console page: without the patch,
add_memregion() merges with a count of 7 for 6 live regions; with it,
the counts always match. The final region table is the same.
This patch was written with the assistance of an AI agent.
lib/sbi/sbi_domain.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/lib/sbi/sbi_domain.c b/lib/sbi/sbi_domain.c
index 42fe5632..f711f6bc 100644
--- a/lib/sbi/sbi_domain.c
+++ b/lib/sbi/sbi_domain.c
@@ -835,6 +835,9 @@ static int add_memregion(struct sbi_domain *dom, const struct sbi_domain_memregi
return rc;
}
+ /* sanitize_domain() may have removed covered regions */
+ memregs_count = sbi_domain_used_memregions(dom);
+
/* Merge consecutive memregions with same order and flags */
reg_merged = false;
sbi_domain_for_each_memregion(dom, nreg) {
base-commit: ed70256dd22c09962d146e433834239825ef0616
--
2.53.0
More information about the opensbi
mailing list