linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Pasha Tatashin <pasha.tatashin@soleen.com>
To: akpm@linux-foundation.org, pasha.tatashin@soleen.com,
	rppt@kernel.org,  graf@amazon.com, linux-kernel@vger.kernel.org,
	kexec@lists.infradead.org,  linux-mm@kvack.org,
	pratyush@kernel.org,  ricardo.neri-calderon@linux.intel.com
Subject: Re: [PATCH] kho: validate preserved memory map during population
Date: Thu, 18 Dec 2025 19:49:11 -0500	[thread overview]
Message-ID: <CA+CK2bAOMooH+NE5YanRyfK0KraRug2VHT_M59UpbiD5T2qu-w@mail.gmail.com> (raw)
In-Reply-To: <20251219002355.3323896-1-pasha.tatashin@soleen.com>

On Thu, Dec 18, 2025 at 7:23 PM Pasha Tatashin
<pasha.tatashin@soleen.com> wrote:
>
> If the previous kernel enabled KHO but did not call kho_finalize()
> (e.g., CONFIG_LIVEUPDATE=n or userspace skipped the finalization step),
> the 'preserved-memory-map' property in the FDT remains empty/zero.
>
> Previously, kho_populate() would succeed regardless of the memory map's
> state, reserving the incoming scratch regions in memblock. However,
> kho_memory_init() would later fail to deserialize the empty map. By that
> time, the scratch regions were already registered, leading to partial
> initialization and subsequent list corruption (double-free) during
> kho_init().
>
> Move the validation of the preserved memory map earlier into
> kho_populate(). If the memory map is empty/NULL:
> 1. Abort kho_populate() immediately with -ENOENT.
> 2. Do not register or reserve the incoming scratch memory, allowing the new
>    kernel to reclaim those pages as standard free memory.
> 3. Leave the global 'kho_in' state uninitialized.
>
> Consequently, kho_memory_init() sees no active KHO context
> (kho_in.mem_chunks is NULL) and falls back to kho_reserve_scratch(),
> allocating fresh scratch memory as if it were a standard cold boot.
>
> Fixes: de51999e687c ("kho: allow memory preservation state updates after finalization")
> Reported-by: Ricardo <ricardo@example.com>
> Closes: https://lore.kernel.org/all/20251218215613.GA17304@ranerica-svr.sc.intel.com
> Signed-off-by: Pasha Tatashin <pasha.tatashin@soleen.com>
> ---
>  kernel/liveupdate/kexec_handover.c | 36 +++++++++++++++++-------------
>  1 file changed, 21 insertions(+), 15 deletions(-)
>
> diff --git a/kernel/liveupdate/kexec_handover.c b/kernel/liveupdate/kexec_handover.c
> index 9dc51fab604f..96c708f753d4 100644
> --- a/kernel/liveupdate/kexec_handover.c
> +++ b/kernel/liveupdate/kexec_handover.c
> @@ -460,10 +460,9 @@ static void __init deserialize_bitmap(unsigned int order,
>         }
>  }
>
> -/* Return true if memory was deserizlied */
> -static bool __init kho_mem_deserialize(const void *fdt)
> +/* Returns head of preserved physical memory chunks pointer from FDT */
> +static struct khoser_mem_chunk * __init kho_get_mem_chunks(const void *fdt)
>  {
> -       struct khoser_mem_chunk *chunk;
>         const void *mem_ptr;
>         u64 mem;
>         int len;
> @@ -471,16 +470,16 @@ static bool __init kho_mem_deserialize(const void *fdt)
>         mem_ptr = fdt_getprop(fdt, 0, PROP_PRESERVED_MEMORY_MAP, &len);
>         if (!mem_ptr || len != sizeof(u64)) {
>                 pr_err("failed to get preserved memory bitmaps\n");
> -               return false;
> +               return NULL;
>         }
>
>         mem = get_unaligned((const u64 *)mem_ptr);
> -       chunk = mem ? phys_to_virt(mem) : NULL;
>
> -       /* No preserved physical pages were passed, no deserialization */
> -       if (!chunk)
> -               return false;
> +       return mem ? phys_to_virt(mem) : NULL;

I need to update this patch, phys_to_virt() should not be called from
kho_populate() before KASLR is initialized. This causes a problem with
my live update test. I will send a new version soon.

Pasha


  reply	other threads:[~2025-12-19  0:49 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-12-19  0:23 Pasha Tatashin
2025-12-19  0:49 ` Pasha Tatashin [this message]
2025-12-19  2:34   ` Ricardo Neri

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CA+CK2bAOMooH+NE5YanRyfK0KraRug2VHT_M59UpbiD5T2qu-w@mail.gmail.com \
    --to=pasha.tatashin@soleen.com \
    --cc=akpm@linux-foundation.org \
    --cc=graf@amazon.com \
    --cc=kexec@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=pratyush@kernel.org \
    --cc=ricardo.neri-calderon@linux.intel.com \
    --cc=rppt@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox