From: Sean Christopherson <seanjc@google.com>
To: Zeng Chi <zeng_chi911@163.com>
Cc: chao.p.peng@linux.intel.com, kvm@vger.kernel.org,
linux-kernel@vger.kernel.org, pbonzini@redhat.com,
zengchi@kylinos.cn
Subject: Re: [PATCH v2] KVM: Don't treat reserved xarray entries as having memory attributes
Date: Fri, 28 Aug 2026 10:15:07 -0700 [thread overview]
Message-ID: <apHCG78KyLuzPrNa@google.com> (raw)
In-Reply-To: <20260828102728.1308266-1-zeng_chi911@163.com>
Please don't send a new version of a patch/series while there is active discussion
on the previous version. As is the case here, there's often not enough context
in the new, standalone patch to carry on the discussion. And even when there is
enough context, it's annoying to have to read one thread, and then skip over to
a different thread to respond.
On Fri, Aug 28, 2026, Zeng Chi wrote:
> From: Zeng Chi <zengchi@kylinos.cn>
>
> kvm_vm_set_mem_attributes() reserves an xarray entry for every gfn in
> the range before storing the new attributes, so that the store loop
> can't fail partway through. If one of the reservations fails, e.g. with
> -ENOMEM, the entries that were already reserved are left in the array.
> That is harmless as far as xa_reserve() is concerned, as the reserved
> entries read back as NULL via xa_load(), but it confuses the "does this
> range have no attributes at all" check:
>
> if (!attrs)
> return !xas_find(&xas, end - 1);
>
> A reserved entry is XA_ZERO_ENTRY, not NULL, and xas_find() returns it
> as present. So a leftover reservation makes KVM report that a fully
> shared range has attributes even though kvm_get_memory_attributes()
> returns none for every gfn in the range. On x86, the next time
> mixed-attribute tracking is recomputed for the range (memslot creation,
> or a later attribute change that straddles the 2MiB page),
> hugepage_has_attrs() treats a fully shared 2MiB range as mixed and
> refuses to map it with a hugepage, until userspace happens to set
> attributes on the range again.
>
> Walk the range and ignore reserved-but-unset entries when checking for
> the absence of attributes, so a leftover reservation is treated the same
> as an empty slot. Note, the generic loop for the attrs != 0 case
> already skips zero entries via xas_retry(), i.e. only the !attrs shortcut
> was affected.
>
> While at it, skip the reservation loop entirely when clearing attributes,
> as storing NULL only erases the entry and never needs to allocate, so no
> reservation (and no cleanup of a failed one) is required in that case.
>
> Fixes: 5a475554db1e ("KVM: Introduce per-page memory attributes")
> Signed-off-by: Zeng Chi <zengchi@kylinos.cn>
> ---
> virt/kvm/kvm_main.c | 21 +++++++++++++++++----
> 1 file changed, 17 insertions(+), 4 deletions(-)
>
> diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
> index 65eb26a0520d..29534bcc7f02 100644
> --- a/virt/kvm/kvm_main.c
> +++ b/virt/kvm/kvm_main.c
> @@ -2447,8 +2447,19 @@ bool kvm_range_has_memory_attributes(struct kvm *kvm, gfn_t start, gfn_t end,
> return (kvm_get_memory_attributes(kvm, start) & mask) == attrs;
>
> guard(rcu)();
> - if (!attrs)
> - return !xas_find(&xas, end - 1);
> + if (!attrs) {
> + /*
> + * Reserved but unset entries (XA_ZERO_ENTRY, e.g. left behind by
> + * a failed reservation in kvm_vm_set_mem_attributes()) are
> + * returned as present by xas_find(), but hold no attributes.
> + * Skip them so that the range is correctly reported as having no
> + * attributes.
> + */
> + xas_for_each(&xas, entry, end - 1)
Curly braces needed for the outer loop. And +1 to Sashiko's feedback, both from
a correctness perspective and from a "make boths paths look similar" perspective.
Though even better, we can use the same core logic. Pulling in your response from
v1:
: > I think it would be this?
: >
: > diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
: > index 65eb26a0520d..a01b2af1cb17 100644
: > --- a/virt/kvm/kvm_main.c
: > +++ b/virt/kvm/kvm_main.c
: > @@ -2447,8 +2447,9 @@ bool kvm_range_has_memory_attributes(struct kvm *kvm, gfn_t start, gfn_t end,
: > return (kvm_get_memory_attributes(kvm, start) & mask) == attrs;
: >
: > guard(rcu)();
: > - if (!attrs)
: > - return !xas_find(&xas, end - 1);
: > +
: > + if (!attrs && !xas_find(&xas, end - 1))
: > + return true;
: >
: > for (index = start; index < end; index++) {
: > do {
: >
: I tried that first, but it doesn't fix the false positive. Falling through to
: the generic loop for the !attrs case still returns false for a range that only
: contains reserved (zero) entries: the loop does
:
: do {
: entry = xas_next(&xas);
: } while (xas_retry(&xas, entry));
The other subtle wrinkle is that the xarray APIs reset the index when no entry is
found (this wasted a good 30 minutes of my time, argh). I.e. when on entry is
found, then KVM *must not* check the index, because it is effectively invalid.
E.g. I initially wanted to check for xas.xa_index >= end, but that doesn't work.
This code also needs comments, because the xarray APIs have all kinds of sharp
edges (or maybe a better way of looking at things, xarray isn't a great fit for
what KVM is doing here).
Yeesh, speaking of which, simply using xas_next_entry(), as I want to do, would
be slightly suboptimal for non-zero attributes, because xas_next() (confusingly,
IMO) doesn't return the next non-NULL entry, it returns literally the next entry,
whereas xas_next_entry() returns the next non-NULL entry, bounded by the max. I
don't actually care about the performance impact, but I want to document the
behavior, at which point it's just as easy to use next() vs. next_entry().
So after way, waaay too much fiddling, this? As a bonus, the changelog can call
out that xas_next_entry() is essentially an optimized version of xas_find(),
e.g. to communicate that the effective diff is actually just adding xas_retry().
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 65eb26a0520d..cc94d9881582 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -2447,14 +2447,39 @@ bool kvm_range_has_memory_attributes(struct kvm *kvm, gfn_t start, gfn_t end,
return (kvm_get_memory_attributes(kvm, start) & mask) == attrs;
guard(rcu)();
- if (!attrs)
- return !xas_find(&xas, end - 1);
+ /*
+ * Lookup the entry for each index instead of iterating over the xarray
+ * as KVM deletes/nullifies entries to represent "no attributes", and
+ * the xas index is effectively invalid when no entry is found. I.e.
+ * matching non-zero attributes for *every* entry effectively requires
+ * a manually lookup for each index.
+ *
+ * Skip pre-allocated, reserved entries, or restart the lookup if the
+ * xarray was concurrently modified, via xas_retry() ("retry" means the
+ * entry holds an internal xarray value, i.e. is either invalid or NULL
+ * from the caller's perspective.
+ *
+ * Use xas_next() when looking for non-zero attributes to optimize for
+ * the case where the start of the range (or the entire range) doesn't
+ * have any attributes, as xas_next() returns literally the next entry,
+ * whereas xas_next_entry() returns the next non-NULL entry (bounded by
+ * a maximum index).
+ */
for (index = start; index < end; index++) {
do {
- entry = xas_next(&xas);
+ entry = attrs ? xas_next(&xas) :
+ xas_next_entry(&xas, end - 1);
} while (xas_retry(&xas, entry));
+ /*
+ * Don't check the index if there's no entry; as above, the xas
+ * index is invalid (and if no entry was found, then the entire
+ * range has no attributes).
+ */
+ if (!entry)
+ return !attrs;
+
if (xas.xa_index != index ||
(xa_to_value(entry) & mask) != attrs)
return false;
next prev parent reply other threads:[~2026-08-28 17:15 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-27 11:05 [PATCH] KVM: Release reserved xarray entries if reserving memory attributes fails Zeng Chi
2026-08-27 11:17 ` sashiko-bot
2026-08-27 18:37 ` Sean Christopherson
2026-08-28 10:27 ` [PATCH v2] KVM: Don't treat reserved xarray entries as having memory attributes Zeng Chi
2026-08-28 10:41 ` sashiko-bot
2026-08-28 17:15 ` Sean Christopherson [this message]
2026-08-28 18:17 ` Sean Christopherson
2026-08-28 10:51 ` [PATCH] KVM: Release reserved xarray entries if reserving memory attributes fails Zeng Chi
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=apHCG78KyLuzPrNa@google.com \
--to=seanjc@google.com \
--cc=chao.p.peng@linux.intel.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=pbonzini@redhat.com \
--cc=zeng_chi911@163.com \
--cc=zengchi@kylinos.cn \
/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