Skip to contentSkip to conversation

mm: kmsan: Panic on failure to allocate early boot metadata

PATCH Superseded Pedro Falcato 2 participants 3 messages Oct 15, 2023 – Oct 16, 2023

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm: kmsan: Panic on failure to allocate early boot metadata

Given large enough allocations and a machine with low enough memory (i.e a default QEMU VM), it's entirely possible that kmsan_init_alloc_meta_for_range's shadow+origin allocation fails.

Instead of eating a NULL deref kernel oops, check explicitly for memblock_alloc() failure and panic with a nice error message.

---
 mm/kmsan/shadow.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
mm/kmsan/shadow.c +8 -2
index 87318f9170f..3dae3d9c0b3 100644
@@ -285,12 +285,18 @@ void __init kmsan_init_alloc_meta_for_range(void *start, void *end)
 	size = PAGE_ALIGN((u64)end - (u64)start);
 	shadow = memblock_alloc(size, PAGE_SIZE);
 	origin = memblock_alloc(size, PAGE_SIZE);
+
+	if (!shadow || !origin)
+		panic("%s: Failed to allocate metadata memory for early boot range "
+		      "of size %llu",
+		      __func__, size);
+
 	for (u64 addr = 0; addr < size; addr += PAGE_SIZE) {
 		page = virt_to_page_or_null((char *)start + addr);
-		shadow_p = virt_to_page_or_null((char *)shadow + addr);
+		shadow_p = virt_to_page((char *)shadow + addr);
 		set_no_shadow_origin_page(shadow_p);
 		shadow_page_for(page) = shadow_p;
-		origin_p = virt_to_page_or_null((char *)origin + addr);
+		origin_p = virt_to_page((char *)origin + addr);
 		set_no_shadow_origin_page(origin_p);
 		origin_page_for(page) = origin_p;
 	}
Signature · 1 line
2.42.0
#1 lore raw
Quoted 4 lines · Pedro Falcato

Given large enough allocations and a machine with low enough memory (i.e a default QEMU VM), it's entirely possible that kmsan_init_alloc_meta_for_range's shadow+origin allocation fails.

Ugh, forgot to run checkpatch.pl until it was too late :/

Quoted 1 line

Instead of eating a NULL deref kernel oops, check explicitly for memblock_alloc()

If there's no need for a v2, please wrap the above line and...

Quoted 12 lines

failure and panic with a nice error message.

---
 mm/kmsan/shadow.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
mm/kmsan/shadow.c +0 -0
index 87318f9170f..3dae3d9c0b3 100644

mm/kmsan/shadow.c void __init kmsan_init_alloc_meta_for_range(void *start, void *end) · line 291

Quoted 7 lines
mm/kmsan/shadow.c +4 -0
@@ -285,12 +285,18 @@ void __init kmsan_init_alloc_meta_for_range(void *start, void *end)
        size = PAGE_ALIGN((u64)end - (u64)start);
        shadow = memblock_alloc(size, PAGE_SIZE);
        origin = memblock_alloc(size, PAGE_SIZE);
+
+       if (!shadow || !origin)
+               panic("%s: Failed to allocate metadata memory for early boot range "
+                     "of size %llu",

unwrap this string like this:
"%s: Failed to allocate metadata memory for early boot range of size %llu",

Silly mistake...

Signature · 1 line
Pedro
reviewer
#2 lore raw
Quoted 7 lines · Pedro Falcato
Quoted 4 lines · Pedro Falcato

Given large enough allocations and a machine with low enough memory (i.e a default QEMU VM), it's entirely possible that kmsan_init_alloc_meta_for_range's shadow+origin allocation fails.

Quoted 5 lines

Ugh, forgot to run checkpatch.pl until it was too late :/

Quoted 1 line

Instead of eating a NULL deref kernel oops, check explicitly for memblock_alloc()

If there's no need for a v2, please wrap the above line and...

Probably easier to send v2.

Otherwise looks good.

Quoted 27 lines
Quoted 12 lines

failure and panic with a nice error message.

---
 mm/kmsan/shadow.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
mm/kmsan/shadow.c +0 -0
index 87318f9170f..3dae3d9c0b3 100644

mm/kmsan/shadow.c void __init kmsan_init_alloc_meta_for_range(void *start, void *end) · line 291

Quoted 7 lines
mm/kmsan/shadow.c +4 -0
@@ -285,12 +285,18 @@ void __init kmsan_init_alloc_meta_for_range(void *start, void *end)
        size = PAGE_ALIGN((u64)end - (u64)start);
        shadow = memblock_alloc(size, PAGE_SIZE);
        origin = memblock_alloc(size, PAGE_SIZE);
+
+       if (!shadow || !origin)
+               panic("%s: Failed to allocate metadata memory for early boot range "
+                     "of size %llu",

unwrap this string like this:
"%s: Failed to allocate metadata memory for early boot range of size %llu",

Silly mistake...

--
Pedro