Re: [PATCH] KVM: Add helpers to allocate memslot metadata

From: Sean Christopherson

Date: Wed Nov 04 2026 - 09:04:36 EST


On Tue, Nov 03, 2026, Fred Griffoul wrote:
> From: Fred Griffoul <fgriffo@xxxxxxxxxxxx>
>
> __vcalloc() makes every allocation at least one page, so a memslot
> that spans a single page uses 8 KiB of vmalloc for 8 bytes of
> lpage_info, plus 4 KiB for a 16-byte dirty bitmap once dirty logging
> is enabled. The waste grows with the number of memslots and VMs on a
> host. kvcalloc() avoids it for small arrays, but refuses allocations
> above INT_MAX bytes, which the rmap and write-tracking arrays of a
> large memslot exceed.
>
> Add kvm_memslot_metadata_alloc() and kvm_memslot_metadata_free(),
> which use kvzalloc() up to INT_MAX bytes and vmalloc above, and route
> the dirty bitmap, rmaps, lpage_info and write-tracking arrays through
> them. That way, how KVM allocates memslot metadata is decided in one
> place instead of at each array.
>
> Suggested-by: Sean Christopherson <seanjc@xxxxxxxxxx>
> Link: https://lore.kernel.org/all/arr1VgGZmgUmY4hc@xxxxxxxxxx/
> Signed-off-by: Fred Griffoul <fgriffo@xxxxxxxxxxxx>
> ---
> Mushahid's patch [1] switched the lpage_info arrays and dirty bitmaps to
> kvcalloc(). Sean suggested dedicated helpers for memslot metadata
> instead [2], which this patch adds.

Split this into two patches: patch 1 to add the helpers, patch 2 to add the logic
to pivot on INT_MAX

As is, the shortlog is comically misleading. Pretty much anyone is going to read
this and assume it's just refactoring existing code.