[PATCH v2 2/2] KVM: Use kvzalloc() for memslot metadata that fits in a slab bucket

From: Fred Griffoul

Date: Wed Nov 04 2026 - 19:28:15 EST


From: Fred Griffoul <fgriffo@xxxxxxxxxxxx>

Use kvzalloc() instead of __vcalloc() for memslot metadata up to
KMALLOC_MAX_CACHE_SIZE, as vmalloc() rounds every allocation up to a
page, e.g. a memslot that spans a single page uses 8 KiB for 8 bytes of
lpage_info. The waste grows with the number of memslots and VMs.

Up to KMALLOC_MAX_CACHE_SIZE, kmalloc() serves the request from a slab
bucket that is never larger than the pages vmalloc() would use, and
above a page kvzalloc() falls back to vmalloc() if the slab allocation
fails. Above KMALLOC_MAX_CACHE_SIZE, kmalloc() rounds to a power-of-two
number of pages, so keep __vcalloc() there.

Free the metadata with kvfree(), which handles both kmalloc() and
vmalloc() memory.

Suggested-by: Sean Christopherson <seanjc@xxxxxxxxxx>
Link: https://lore.kernel.org/all/arr1VgGZmgUmY4hc@xxxxxxxxxx/
Signed-off-by: Fred Griffoul <fgriffo@xxxxxxxxxxxx>
---
virt/kvm/kvm_main.c | 10 +++++++++-
1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index ebdf9308d06c..1854e1b075c5 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -943,12 +943,20 @@ static void kvm_destroy_pm_notifier(struct kvm *kvm)

void *kvm_memslot_metadata_alloc(size_t n, size_t size)
{
+ size_t bytes;
+
+ if (unlikely(check_mul_overflow(n, size, &bytes)))
+ return NULL;
+
+ if (bytes <= KMALLOC_MAX_CACHE_SIZE)
+ return kvzalloc(bytes, GFP_KERNEL_ACCOUNT);
+
return __vcalloc(n, size, GFP_KERNEL_ACCOUNT);
}

void kvm_memslot_metadata_free(void *p)
{
- vfree(p);
+ kvfree(p);
}

static void kvm_destroy_dirty_bitmap(struct kvm_memory_slot *memslot)
--
2.47.3