Skip to contentSkip to conversation

kho: Don't release incoming bootmem areas when kho_init() fails

PATCH In review Chris Down 3 participants 3 messages Nov 8 – Nov 9

Subsystems (3)
lore
Share & tools
lore

Chris Down

author
#0 lore raw

PATCH 1/1 kho: Don't release incoming bootmem areas when kho_init() fails

kho_populate() adds the incoming bootmem areas without reserving them. memblock_free_all() then hands the remainder to the buddy allocator.

Unfortunately however, if kho_init() fails, err_free_bootmem currently frees every area with free_reserved_area(). This can free pages still backing early allocations or free buddy pages a second time.

To fix this, let's only free the areas on a cold boot when kho_reserve_bootmem() reserved them.

Fixes c609c144b0e8 ("kexec: add KHO parsing support")
---
 kernel/liveupdate/kexec_handover.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
kernel/liveupdate/kexec_handover.c +6 -4
index 6f1f55fe3181..a566ad6634d8 100644
@@ -1899,11 +1899,13 @@ static __init int kho_init(void)
 	kho_radix_destroy_tree(tree);
 err_free_bootmem:
 	kho_out.fdt = NULL;
-	for (int i = 0; i < kho_bootmem_cnt; i++) {
-		void *start = __va(kho_bootmem[i].addr);
-		void *end = start + kho_bootmem[i].size;
+	if (!fdt) {
+		for (int i = 0; i < kho_bootmem_cnt; i++) {
+			void *start = __va(kho_bootmem[i].addr);
+			void *end = start + kho_bootmem[i].size;
 
-		free_reserved_area(start, end, -1, "");
+			free_reserved_area(start, end, -1, "");
+		}
 	}
 	kho_enable = false;
 	return err;

base-commit: fa4ad798f6f681031775266044ba1b0cf3c670bd

Signature · 1 line
2.54.0
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Chris Down

kho_populate() adds the incoming bootmem areas without reserving them. memblock_free_all() then hands the remainder to the buddy allocator.

Unfortunately however, if kho_init() fails, err_free_bootmem currently frees every area with free_reserved_area(). This can free pages still backing early allocations or free buddy pages a second time.

[...]

Signature · 1 line
Pasha
maintainer
#2 lore raw
In reply to Chris Down

Hi,

Quoted 3 lines · Chris Down

kho_populate() adds the incoming bootmem areas without reserving them. memblock_free_all() then hands the remainder to the buddy allocator.

This paragraph is confusing.
It reads like kho_populate() should have reserved bootmem areas, but it didn't.

Quoted 4 lines

Unfortunately however, if kho_init() fails, err_free_bootmem currently frees every area with free_reserved_area(). This can free pages still backing early allocations or free buddy pages a second time.

Is this an issue you hit in practice or it was reported by a code analysis?

Quoted 15 lines

To fix this, let's only free the areas on a cold boot when kho_reserve_bootmem() reserved them.

Fixes c609c144b0e8 ("kexec: add KHO parsing support")
---
 kernel/liveupdate/kexec_handover.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
kernel/liveupdate/kexec_handover.c +0 -0
index 6f1f55fe3181..a566ad6634d8 100644

kernel/liveupdate/kexec_handover.c static __init int kho_init(void) · line 1902

Quoted 7 lines
kernel/liveupdate/kexec_handover.c +1 -3
@@ -1899,11 +1899,13 @@ static __init int kho_init(void)
 	kho_radix_destroy_tree(tree);
 err_free_bootmem:
 	kho_out.fdt = NULL;
-	for (int i = 0; i < kho_bootmem_cnt; i++) {
-		void *start = __va(kho_bootmem[i].addr);
-		void *end = start + kho_bootmem[i].size;
+	if (!fdt) {

I think using !is_kho_boot() makes this clearer.

Quoted 15 lines
kernel/liveupdate/kexec_handover.c +5 -1
+		for (int i = 0; i < kho_bootmem_cnt; i++) {
+			void *start = __va(kho_bootmem[i].addr);
+			void *end = start + kho_bootmem[i].size;
 
-		free_reserved_area(start, end, -1, "");
+			free_reserved_area(start, end, -1, "");
+		}
 	}
 	kho_enable = false;
 	return err;

base-commit: fa4ad798f6f681031775266044ba1b0cf3c670bd

Signature · 1 line
2.54.0
Signature · 2 lines
Sincerely yours,
Mike.