Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Zeng Chi" <zeng_chi911@163.com>
Cc: kvm@vger.kernel.org
Subject: Re: [PATCH v2] KVM: Don't treat reserved xarray entries as having memory attributes
Date: Fri, 28 Aug 2026 10:41:01 +0000	[thread overview]
Message-ID: <20260828104101.9E45F1F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260828102728.1308266-1-zeng_chi911@163.com>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Missing XArray retry handling causes spurious memory attribute presence reporting
--

commit 4066b9047c8a4867ec41a5a11e687a69b4773334
Author: Zeng Chi <zengchi@kylinos.cn>

KVM: Don't treat reserved xarray entries as having memory attributes

This commit fixes a bug in KVM where failed xarray reservations leave zero
entries in the attributes array, causing fully shared memory ranges to be
incorrectly reported as having mixed attributes. It modifies the attribute
check to explicitly skip zero entries when checking if a range is empty.

> diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
> index 65eb26a0520d8..29534bcc7f020 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;

[Severity: Medium]
Does this lockless loop need to handle XArray retry entries?

If kvm_vm_set_mem_attributes() modifies the array concurrently (e.g., using
xa_reserve() or xa_store()), the XArray nodes might split or shrink,
temporarily exposing an XA_RETRY_ENTRY to readers.

While !xa_is_zero(entry) correctly evaluates to false for XA_ZERO_ENTRY, it
evaluates to true if the entry is an internal XA_RETRY_ENTRY. This causes
the loop to treat a retry entry as a non-zero attribute entry and
prematurely return false.

Could this cause KVM to incorrectly report a memory range as having
attributes when it actually has none? For instance, on x86, this might force
callers like hugepage_has_attrs() to permanently mark the range as mixed,
disabling hugepage mappings for the affected GFNs until their attributes are
explicitly modified again.

Should we explicitly restart the traversal when encountering retry entries?

		if (xas_retry(&xas, entry))
			continue;

> +		return true;
> +	}
>  
>  	for (index = start; index < end; index++) {
>  		do {
[ ... ]

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260828102728.1308266-1-zeng_chi911@163.com?part=1

  reply	other threads:[~2026-08-28 10:41 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 [this message]
2026-08-28 17:15     ` Sean Christopherson
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=20260828104101.9E45F1F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=kvm@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=zeng_chi911@163.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