[PATCH 5/5] KVM: SEV: Use kvzalloc_objs() when pinning userpages
From: Sean Christopherson
Date: Thu Mar 12 2026 - 20:35:25 EST
Use kvzalloc_objs() instead of sev_pin_memory()'s open coded (rough)
equivalent to harden the code and
Note! This sanity check in __kvmalloc_node_noprof()
/* Don't even allow crazy sizes */
if (unlikely(size > INT_MAX)) {
WARN_ON_ONCE(!(flags & __GFP_NOWARN));
return NULL;
}
will artificially limit the maximum size of any single pinned region to
just under 1TiB. While there do appear to be providers that support SEV
VMs with more than 1TiB of _total_ memory, it's unlikely any KVM-based
providers pin 1TiB in a single request.
Allocate with NOWARN so that fuzzers can't trip the WARN_ON_ONCE() when
they inevitably run on systems with copious amounts of RAM, i.e. when they
can get by KVM's "total_npages > totalram_pages()" restriction.
Note #2, KVM's usage of vmalloc()+kmalloc() instead of kvmalloc() predates
commit 7661809d493b ("mm: don't allow oversized kvmalloc() calls") by 4+
years (see commit 89c505809052 ("KVM: SVM: Add support for
KVM_SEV_LAUNCH_UPDATE_DATA command"). I.e. the open coded behavior wasn't
intended to avoid the aforementioned sanity check. The implementation
appears to be pure oversight at the time the code was written, as it showed
up in v3[1] of the early RFCs, whereas as v2[2] simply used kmalloc().
Cc: Liam Merwick <liam.merwick@xxxxxxxxxx>
Link: https://lore.kernel.org/all/20170724200303.12197-17-brijesh.singh@xxxxxxx [1]
Link: https://lore.kernel.org/all/148846786714.2349.17724971671841396908.stgit__25299.4950431914$1488470940$gmane$org@brijesh-build-machine [2]
Signed-off-by: Sean Christopherson <seanjc@xxxxxxxxxx>
---
arch/x86/kvm/svm/sev.c | 20 +++++++++-----------
1 file changed, 9 insertions(+), 11 deletions(-)
diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c
index ae5b370db9ed..4e4adab8d309 100644
--- a/arch/x86/kvm/svm/sev.c
+++ b/arch/x86/kvm/svm/sev.c
@@ -678,11 +678,9 @@ static struct page **sev_pin_memory(struct kvm *kvm, unsigned long uaddr,
unsigned int flags)
{
struct kvm_sev_info *sev = to_kvm_sev_info(kvm);
- unsigned long npages, size;
- int npinned;
- unsigned long total_npages, lock_limit;
+ unsigned long npages, total_npages, lock_limit;
struct page **pages;
- int ret;
+ int npinned, ret;
lockdep_assert_held(&kvm->lock);
@@ -709,13 +707,13 @@ static struct page **sev_pin_memory(struct kvm *kvm, unsigned long uaddr,
return ERR_PTR(-ENOMEM);
}
- /* Avoid using vmalloc for smaller buffers. */
- size = npages * sizeof(struct page *);
- if (size > PAGE_SIZE)
- pages = __vmalloc(size, GFP_KERNEL_ACCOUNT);
- else
- pages = kmalloc(size, GFP_KERNEL_ACCOUNT);
-
+ /*
+ * Don't WARN if the kernel (rightly) thinks the total size is absurd,
+ * i.e. rely on the kernel to reject outrageous range sizes. The above
+ * check on the number of pages is purely to avoid truncation as
+ * pin_user_pages_fast() takes the number of pages as a 32-bit int.
+ */
+ pages = kvzalloc_objs(*pages, npages, GFP_KERNEL_ACCOUNT | __GFP_NOWARN);
if (!pages)
return ERR_PTR(-ENOMEM);
--
2.53.0.851.ga537e3e6e9-goog