mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Fred Griffoul <griffoul@gmail.com>
To: Sean Christopherson <seanjc@google.com>,
	Paolo Bonzini <pbonzini@redhat.com>
Cc: Mushahid Hussain <hmushi@amazon.co.uk>,
	nh-open-source@amazon.com, kvm@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: [PATCH v2 2/2] KVM: Use kvzalloc() for memslot metadata that fits in a slab bucket
Date: Thu,  5 Nov 2026 00:01:27 +0000	[thread overview]
Message-ID: <20261105000127.63664-3-griffoul@gmail.com> (raw)
In-Reply-To: <20261105000127.63664-1-griffoul@gmail.com>

From: Fred Griffoul <fgriffo@amazon.co.uk>

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@google.com>
Link: https://lore.kernel.org/all/arr1VgGZmgUmY4hc@google.com/
Signed-off-by: Fred Griffoul <fgriffo@amazon.co.uk>
---
 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


      parent reply	other threads:[~2026-11-05  0:01 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-05  0:01 [PATCH v2 0/2] KVM: Allocate small memslot metadata with kvzalloc() Fred Griffoul
2026-11-05  0:01 ` [PATCH v2 1/2] KVM: Add helpers to allocate memslot metadata Fred Griffoul
2026-11-05  0:01 ` Fred Griffoul [this message]

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=20261105000127.63664-3-griffoul@gmail.com \
    --to=griffoul@gmail.com \
    --cc=hmushi@amazon.co.uk \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nh-open-source@amazon.com \
    --cc=pbonzini@redhat.com \
    --cc=seanjc@google.com \
    /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

all inboxes | Powered by JetHome®