mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: David Ballesteros <davimaba.v@proton.me>
To: pbonzini@redhat.com, seanjc@google.com
Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v4 1/3] KVM: Release memory-attribute reservations abandoned on ENOMEM
Date: Tue, 15 Sep 2026 17:53:47 +0000	[thread overview]
Message-ID: <20260915175335.138547-2-davimaba.v@proton.me> (raw)
In-Reply-To: <20260915175335.138547-1-davimaba.v@proton.me>

kvm_vm_set_mem_attributes() reserves an xarray entry for every GFN in the
range before storing anything, so that the store phase cannot fail partway
through.  When a reservation fails, the loop jumps to out_unlock and the
reservations already made are abandoned: nothing in the call releases them.
A later request that clears a range covering them does erase them, as the
clear stores NULL over the reservation, but nothing obliges userspace to
issue one and a caller exploiting this will not; absent such a call the
entries live until kvm_destroy_vm().  An unprivileged user with /dev/kvm on
a VM with private-memory support can therefore leak kernel memory across
calls (a 576-byte xa_node per 64 GFNs) for the life of the VM fd.

The abandoned entries are not inert.  A bare reservation is an
XA_ZERO_ENTRY.  kvm_range_has_memory_attributes() is inconsistent about it:
the end == start + 1 path and the general loop treat it as absent (matching
kvm_get_memory_attributes(), which maps it to NULL via xa_load()), but the
!attrs fast path calls xas_find() directly, which returns the zero entry as
present.  Via hugepage_has_attrs(), that makes
kvm_arch_post_set_memory_attributes() mark a straddling head/tail hugepage
"mixed" for a range whose attributes are in fact uniform, so KVM stops
using a hugepage there until a later request re-covers it.

Release the reservations this call made on the failure path.  xa_release()
erases an entry only while it is still a reservation, so value entries that
predate this call are left untouched; it takes no gfp and cannot fail.
Only [start, i) is walked, i being the index whose reservation failed (no
entry was created at or beyond it).

Runtime-verified on v6.18.48 (isolated sw-protected VM, no KASAN, no fault
injection; the reservations are left behind by real memcg pressure via
clone(CLONE_VM), not by fault injection).  Unpatched, a failed request
retains on the order of 450000 xa_nodes (~250 MiB), and with one of them
inside a 2 MiB region a clear of the region's head page leaves pages_2m
unchanged (the reservation is invisible to xa_load), a clear of two pages
drops pages_2m by one and raises pages_4k by 512 (the hugepage is
degraded), and a clear covering the whole 2 MiB restores it.  With this
patch the same run retains under 2000 nodes -- three orders of magnitude
less, at the level of run-to-run noise -- and pages_2m stays at 16 across
all three clears: the reservations are released, so the hugepage is never
degraded.  Both arms used the same kernel config and the same test binary.
The bug reproduces with XA_FLAGS_ACCOUNT applied (patch 3/3), i.e.
accounting alone does not fix it.

Found by an AI-assisted security audit.

Fixes: 5a475554db1e ("KVM: Introduce per-page memory attributes")
Cc: stable@vger.kernel.org
Assisted-by: Claude-Code:claude-opus-5
Signed-off-by: David Ballesteros <davimaba.v@proton.me>
---
 virt/kvm/kvm_main.c | 24 +++++++++++++++++++++++-
 1 file changed, 23 insertions(+), 1 deletion(-)

diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -2575,7 +2575,7 @@ static int kvm_vm_set_mem_attributes(struct kvm *kvm, gfn_t start, gfn_t end,
 	for (i = start; i < end; i++) {
 		r = xa_reserve(&kvm->mem_attr_array, i, GFP_KERNEL_ACCOUNT);
 		if (r)
-			goto out_unlock;
+			goto out_release;
 
 		cond_resched();
 	}
@@ -2594,6 +2594,28 @@ static int kvm_vm_set_mem_attributes(struct kvm *kvm, gfn_t start, gfn_t end,
 out_unlock:
 	mutex_unlock(&kvm->slots_lock);
 
+	return r;
+
+out_release:
+	/*
+	 * The reservation loop failed at @i; the entries in [start, i) were
+	 * reserved by this call and, without releasing them here, would be
+	 * retained until userspace happens to clear a range covering them, or
+	 * until the VM is destroyed.  The retained entries are not inert:
+	 * a bare reservation is an XA_ZERO_ENTRY, which the !attrs fast path of
+	 * kvm_range_has_memory_attributes() counts as present (it calls
+	 * xas_find() directly) even though kvm_get_memory_attributes() reports
+	 * it as absent, so a straddling hugepage over such an entry gets marked
+	 * mixed and KVM stops using a hugepage for a range whose attributes are
+	 * uniform.  xa_release() erases an entry only while it is still a
+	 * reservation, so value entries that predate this call are untouched.
+	 */
+	while (i-- > start) {
+		xa_release(&kvm->mem_attr_array, i);
+		cond_resched();
+	}
+	mutex_unlock(&kvm->slots_lock);
+
 	return r;
 }
 static int kvm_vm_ioctl_set_mem_attributes(struct kvm *kvm,


  reply	other threads:[~2026-09-15 17:53 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-15 17:53 [PATCH v4 0/3] KVM: Fix and account mem_attr_array " David Ballesteros
2026-09-15 17:53 ` David Ballesteros [this message]
2026-09-15 17:53 ` [PATCH v4 2/3] KVM: Make kvm_range_has_memory_attributes() consistent about reservations David Ballesteros
2026-09-15 17:53 ` [PATCH v4 3/3] KVM: Account mem_attr_array nodes to the caller's memcg David Ballesteros
2026-09-24 21:51 ` [PATCH v4 0/3] KVM: Fix and account mem_attr_array reservations abandoned on ENOMEM Sean Christopherson
2026-09-27  6:13   ` David Ballesteros
2026-09-28 16:30     ` Sean Christopherson

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=20260915175335.138547-2-davimaba.v@proton.me \
    --to=davimaba.v@proton.me \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --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®