linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] mm: kmsan: Panic on failure to allocate early boot metadata
@ 2023-10-15 20:26 Pedro Falcato
  2023-10-15 20:35 ` Pedro Falcato
  0 siblings, 1 reply; 3+ messages in thread
From: Pedro Falcato @ 2023-10-15 20:26 UTC (permalink / raw)
  To: kasan-dev, Alexander Potapenko
  Cc: Marco Elver, Dmitry Vyukov, Andrew Morton, linux-mm,
	linux-kernel, 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.

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

Signed-off-by: Pedro Falcato <pedro.falcato@gmail.com>
---
 mm/kmsan/shadow.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/mm/kmsan/shadow.c b/mm/kmsan/shadow.c
index 87318f9170f..3dae3d9c0b3 100644
--- a/mm/kmsan/shadow.c
+++ b/mm/kmsan/shadow.c
@@ -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;
 	}
-- 
2.42.0



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] mm: kmsan: Panic on failure to allocate early boot metadata
  2023-10-15 20:26 [PATCH] mm: kmsan: Panic on failure to allocate early boot metadata Pedro Falcato
@ 2023-10-15 20:35 ` Pedro Falcato
  2023-10-16  7:24   ` Marco Elver
  0 siblings, 1 reply; 3+ messages in thread
From: Pedro Falcato @ 2023-10-15 20:35 UTC (permalink / raw)
  To: kasan-dev, Alexander Potapenko
  Cc: Marco Elver, Dmitry Vyukov, Andrew Morton, linux-mm, linux-kernel

On Sun, Oct 15, 2023 at 9:26 PM Pedro Falcato <pedro.falcato@gmail.com> wrote:
>
> 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 :/

> 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...

> failure and panic with a nice error message.
>
> Signed-off-by: Pedro Falcato <pedro.falcato@gmail.com>
> ---
>  mm/kmsan/shadow.c | 10 ++++++++--
>  1 file changed, 8 insertions(+), 2 deletions(-)
>
> diff --git a/mm/kmsan/shadow.c b/mm/kmsan/shadow.c
> index 87318f9170f..3dae3d9c0b3 100644
> --- a/mm/kmsan/shadow.c
> +++ b/mm/kmsan/shadow.c
> @@ -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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] mm: kmsan: Panic on failure to allocate early boot metadata
  2023-10-15 20:35 ` Pedro Falcato
@ 2023-10-16  7:24   ` Marco Elver
  0 siblings, 0 replies; 3+ messages in thread
From: Marco Elver @ 2023-10-16  7:24 UTC (permalink / raw)
  To: Pedro Falcato
  Cc: kasan-dev, Alexander Potapenko, Dmitry Vyukov, Andrew Morton,
	linux-mm, linux-kernel

On Sun, 15 Oct 2023 at 22:35, Pedro Falcato <pedro.falcato@gmail.com> wrote:
>
> On Sun, Oct 15, 2023 at 9:26 PM Pedro Falcato <pedro.falcato@gmail.com> wrote:
> >
> > 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 :/
>
> > 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.

> > failure and panic with a nice error message.
> >
> > Signed-off-by: Pedro Falcato <pedro.falcato@gmail.com>
> > ---
> >  mm/kmsan/shadow.c | 10 ++++++++--
> >  1 file changed, 8 insertions(+), 2 deletions(-)
> >
> > diff --git a/mm/kmsan/shadow.c b/mm/kmsan/shadow.c
> > index 87318f9170f..3dae3d9c0b3 100644
> > --- a/mm/kmsan/shadow.c
> > +++ b/mm/kmsan/shadow.c
> > @@ -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


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-10-16  7:25 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-10-15 20:26 [PATCH] mm: kmsan: Panic on failure to allocate early boot metadata Pedro Falcato
2023-10-15 20:35 ` Pedro Falcato
2023-10-16  7:24   ` Marco Elver

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox