From: Pratyush Yadav <pratyush@kernel.org>
To: Pasha Tatashin <pasha.tatashin@soleen.com>
Cc: akpm@linux-foundation.org, brauner@kernel.org, corbet@lwn.net,
graf@amazon.com, jgg@ziepe.ca, linux-kernel@vger.kernel.org,
linux-kselftest@vger.kernel.org, linux-mm@kvack.org,
masahiroy@kernel.org, ojeda@kernel.org, pratyush@kernel.org,
rdunlap@infradead.org, rppt@kernel.org, tj@kernel.org,
jasonmiu@google.com, dmatlack@google.com, skhawaja@google.com,
glider@google.com, elver@google.com
Subject: Re: [PATCH 2/2] liveupdate: kho: allocate metadata directly from the buddy allocator
Date: Wed, 15 Oct 2025 15:05:05 +0200 [thread overview]
Message-ID: <mafs0v7kgjoxq.fsf@kernel.org> (raw)
In-Reply-To: <20251015053121.3978358-3-pasha.tatashin@soleen.com> (Pasha Tatashin's message of "Wed, 15 Oct 2025 01:31:21 -0400")
+Cc Marco, Alexander
On Wed, Oct 15 2025, Pasha Tatashin wrote:
> KHO allocates metadata for its preserved memory map using the SLUB
> allocator via kzalloc(). This metadata is temporary and is used by the
> next kernel during early boot to find preserved memory.
>
> A problem arises when KFENCE is enabled. kzalloc() calls can be
> randomly intercepted by kfence_alloc(), which services the allocation
> from a dedicated KFENCE memory pool. This pool is allocated early in
> boot via memblock.
At some point, we'd probably want to add support for preserving slab
objects using KHO. That wouldn't work if the objects can land in scratch
memory. Right now, the kfence pools are allocated right before KHO goes
out of scratch-only and memblock frees pages to buddy.
kfence_alloc_pool_and_metadata();
report_meminit();
kmsan_init_shadow();
stack_depot_early_init();
[...]
kho_memory_init();
memblock_free_all();
Can't kfence allocate its pool right after memblock_free_all()? IIUC at
that point, there shouldn't be much fragmentation and allocations should
still be possible.
Another idea could be to disable scratch-only a bit earlier and add an
option in memblock_alloc() to avoid scratch areas?
Anyway, not something we need to solve right now with this series.
Something to figure out eventually.
>
> When booting via KHO, the memblock allocator is restricted to a "scratch
> area", forcing the KFENCE pool to be allocated within it. This creates a
> conflict, as the scratch area is expected to be ephemeral and
> overwriteable by a subsequent kexec. If KHO metadata is placed in this
> KFENCE pool, it leads to memory corruption when the next kernel is
> loaded.
>
> To fix this, modify KHO to allocate its metadata directly from the buddy
> allocator instead of SLUB.
>
> As part of this change, the metadata bitmap size is increased from 512
> bytes to PAGE_SIZE to align with the page-based allocations from the
> buddy system.
The implication of this change is that preservation metadata becomes
less memory-efficient when preserved pages are sparse. Mainly because if
only one bit is set in the bitmap, now 4k bytes of memory is used
instead of 512 bytes.
It is hard to say what difference this makes in practice without
sampling real workloads, but perhaps still worth mentioning in the
commit message?
Other than this,
Reviewed-by: Pratyush Yadav <pratyush@kernel.org>
>
> Fixes: fc33e4b44b27 ("kexec: enable KHO support for memory preservation")
> Signed-off-by: Pasha Tatashin <pasha.tatashin@soleen.com>
> ---
> kernel/liveupdate/kexec_handover.c | 23 +++++++++++++----------
> 1 file changed, 13 insertions(+), 10 deletions(-)
>
> diff --git a/kernel/liveupdate/kexec_handover.c b/kernel/liveupdate/kexec_handover.c
> index ef1e6f7a234b..519de6d68b27 100644
> --- a/kernel/liveupdate/kexec_handover.c
> +++ b/kernel/liveupdate/kexec_handover.c
> @@ -66,10 +66,10 @@ early_param("kho", kho_parse_enable);
> * Keep track of memory that is to be preserved across KHO.
> *
> * The serializing side uses two levels of xarrays to manage chunks of per-order
> - * 512 byte bitmaps. For instance if PAGE_SIZE = 4096, the entire 1G order of a
> - * 1TB system would fit inside a single 512 byte bitmap. For order 0 allocations
> - * each bitmap will cover 16M of address space. Thus, for 16G of memory at most
> - * 512K of bitmap memory will be needed for order 0.
> + * PAGE_SIZE byte bitmaps. For instance if PAGE_SIZE = 4096, the entire 1G order
> + * of a 8TB system would fit inside a single 4096 byte bitmap. For order 0
> + * allocations each bitmap will cover 128M of address space. Thus, for 16G of
> + * memory at most 512K of bitmap memory will be needed for order 0.
> *
> * This approach is fully incremental, as the serialization progresses folios
> * can continue be aggregated to the tracker. The final step, immediately prior
> @@ -77,7 +77,7 @@ early_param("kho", kho_parse_enable);
> * successor kernel to parse.
> */
>
> -#define PRESERVE_BITS (512 * 8)
> +#define PRESERVE_BITS (PAGE_SIZE * 8)
>
> struct kho_mem_phys_bits {
> DECLARE_BITMAP(preserve, PRESERVE_BITS);
> @@ -131,18 +131,21 @@ static struct kho_out kho_out = {
>
> static void *xa_load_or_alloc(struct xarray *xa, unsigned long index, size_t sz)
> {
> + unsigned int order;
> void *elm, *res;
>
> elm = xa_load(xa, index);
> if (elm)
> return elm;
>
> - elm = kzalloc(sz, GFP_KERNEL);
> + order = get_order(sz);
> + elm = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
> if (!elm)
> return ERR_PTR(-ENOMEM);
>
> - if (WARN_ON(kho_scratch_overlap(virt_to_phys(elm), sz))) {
> - kfree(elm);
> + if (WARN_ON(kho_scratch_overlap(virt_to_phys(elm),
> + PAGE_SIZE << order))) {
> + free_pages((unsigned long)elm, order);
> return ERR_PTR(-EINVAL);
> }
>
> @@ -151,7 +154,7 @@ static void *xa_load_or_alloc(struct xarray *xa, unsigned long index, size_t sz)
> res = ERR_PTR(xa_err(res));
>
> if (res) {
> - kfree(elm);
> + free_pages((unsigned long)elm, order);
> return res;
> }
>
> @@ -357,7 +360,7 @@ static struct khoser_mem_chunk *new_chunk(struct khoser_mem_chunk *cur_chunk,
> {
> struct khoser_mem_chunk *chunk;
>
> - chunk = kzalloc(PAGE_SIZE, GFP_KERNEL);
> + chunk = (void *)get_zeroed_page(GFP_KERNEL);
> if (!chunk)
> return ERR_PTR(-ENOMEM);
--
Regards,
Pratyush Yadav
next prev parent reply other threads:[~2025-10-15 13:05 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-10-15 5:31 [PATCH 0/2] KHO: Fix metadata allocation in scratch area Pasha Tatashin
2025-10-15 5:31 ` [PATCH 1/2] liveupdate: kho: warn and fail on metadata or preserved memory " Pasha Tatashin
2025-10-15 8:21 ` Mike Rapoport
2025-10-15 12:36 ` Pasha Tatashin
2025-10-16 17:23 ` Mike Rapoport
2025-10-18 15:31 ` Pasha Tatashin
2025-10-18 15:28 ` Pasha Tatashin
2025-10-15 12:10 ` Pratyush Yadav
2025-10-15 12:40 ` Pasha Tatashin
2025-10-15 13:11 ` Pratyush Yadav
2025-10-15 5:31 ` [PATCH 2/2] liveupdate: kho: allocate metadata directly from the buddy allocator Pasha Tatashin
2025-10-15 8:37 ` Mike Rapoport
2025-10-15 12:46 ` Pasha Tatashin
2025-10-15 13:05 ` Pratyush Yadav [this message]
2025-10-15 14:19 ` Pasha Tatashin
2025-10-15 14:36 ` Alexander Potapenko
2025-10-24 13:25 ` Jason Gunthorpe
2025-10-24 13:57 ` Pasha Tatashin
2025-10-24 14:20 ` Jason Gunthorpe
2025-10-24 14:36 ` Pasha Tatashin
2025-10-24 14:55 ` Jason Gunthorpe
2025-10-24 15:06 ` Pasha Tatashin
2025-10-15 14:22 ` Pasha Tatashin
2025-10-24 13:21 ` Jason Gunthorpe
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=mafs0v7kgjoxq.fsf@kernel.org \
--to=pratyush@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=brauner@kernel.org \
--cc=corbet@lwn.net \
--cc=dmatlack@google.com \
--cc=elver@google.com \
--cc=glider@google.com \
--cc=graf@amazon.com \
--cc=jasonmiu@google.com \
--cc=jgg@ziepe.ca \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=masahiroy@kernel.org \
--cc=ojeda@kernel.org \
--cc=pasha.tatashin@soleen.com \
--cc=rdunlap@infradead.org \
--cc=rppt@kernel.org \
--cc=skhawaja@google.com \
--cc=tj@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;
as well as URLs for NNTP newsgroup(s).