virt/kvm/kvm_main.c | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-)
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)
+ if (!xa_is_zero(entry))
+ return false;
+ return true;
+ }
for (index = start; index < end; index++) {
do {
@@ -2571,9 +2582,11 @@ static int kvm_vm_set_mem_attributes(struct kvm *kvm, gfn_t start, gfn_t end,
/*
* Reserve memory ahead of time to avoid having to deal with failures
- * partway through setting the new attributes.
+ * partway through setting the new attributes. Clearing attributes
+ * only stores NULL, which never needs to allocate, so skip the
+ * reservations entirely in that case.
*/
- for (i = start; i < end; i++) {
+ for (i = start; entry && i < end; i++) {
r = xa_reserve(&kvm->mem_attr_array, i, GFP_KERNEL_ACCOUNT);
if (r)
goto out_unlock;
--
2.25.1
No virus found
Checked by Hillstone Network AntiVirus
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;
On Fri, Aug 28, 2026, Sean Christopherson wrote:
> 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;
One "flaw" with this exact code is that if KVM managed to get a non-null, '0'
entry into the xarray, the index check could mismatch and this function could
technically get a false negative.
Swapping the checks would also work:
if (!attrs)
return !entry;
but I don't love that that violates the "don't check the index because it's bogus"
statement above. And practically speaking, KVM should *never* observe a non-NULL
entry with a value of zero, assuming xas_retry() works as I think it does. So to
harden against KVM changes/goofs, maybe do this as well?
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index cc94d9881582..f009cb3e687d 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -2480,6 +2480,8 @@ bool kvm_range_has_memory_attributes(struct kvm *kvm, gfn_t start, gfn_t end,
if (!entry)
return !attrs;
+ WARN_ON_ONCE(!xa_to_value(entry));
+
if (xas.xa_index != index ||
(xa_to_value(entry) & mask) != attrs)
return false;
> +
> if (xas.xa_index != index ||
> (xa_to_value(entry) & mask) != attrs)
> return false;
>
© 2016 - 2026 Red Hat, Inc.