Kernel KVM virtualization development
 help / color / mirror / Atom feed
* [PATCH] KVM: Fix kvm_range_has_memory_attributes() for a zero mask
@ 2026-12-01  1:21 liuqiangneo
  2026-12-01  1:29 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: liuqiangneo @ 2026-12-01  1:21 UTC (permalink / raw)
  To: pbonzini, seanjc, liuyun; +Cc: zengchi, kvm, Qiang Liu

From: Qiang Liu <liuqiang@kylinos.cn>

When asked whether a range has no attributes (attrs == 0), the loop uses
xas_next_entry(), which skips holes and may land past the current index.
Only perform the index check for attrs != 0, where it guards against a
missing entry.

No current caller passes a zero mask: both pass ~0.  This is a latent API
bug; fix it so the helper behaves consistently for all mask values.

Fixes: 277d3623d99a ("KVM: Don't treat reserved xarray entries as having memory attributes")
Signed-off-by: Qiang Liu <liuqiang@kylinos.cn>
---
 virt/kvm/kvm_main.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 3746682b1a72..1d55c4ee4835 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -2548,7 +2548,13 @@ bool kvm_range_has_vm_memory_attributes(struct kvm *kvm, gfn_t start, gfn_t end,
 
 		WARN_ON_ONCE(!xa_to_value(entry));
 
-		if (xas.xa_index != index ||
+		/*
+		 * With @attrs == 0, xas_next_entry() can land past @index.
+		 * Skipped holes and reserved entries match; only real entries
+		 * with bits in @mask set are rejected. Checking index wrongly
+		 * rejects holes when @mask is 0.
+		 */
+		if ((attrs && xas.xa_index != index) ||
 		    (xa_to_value(entry) & mask) != attrs)
 			return false;
 	}
-- 
2.43.0


No virus found
		Checked by Hillstone Network AntiVirus


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] KVM: Fix kvm_range_has_memory_attributes() for a zero mask
  2026-12-01  1:21 [PATCH] KVM: Fix kvm_range_has_memory_attributes() for a zero mask liuqiangneo
@ 2026-12-01  1:29 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-01  1:29 UTC (permalink / raw)
  To: liuqiangneo; +Cc: kvm

Thank you for your contribution! Sashiko AI review found no new issues
in this patch, but noticed 1 related pre-existing issue it does not
introduce:
- [Critical] KVM: x86/mmu: soft lockup in
  kvm_mmu_init_memslot_memory_attributes() [1]
--

commit 26d303ac2ea9b5803694f4100f1c929f14b5809e
Author: Qiang Liu <liuqiang@kylinos.cn>

KVM: Fix kvm_range_has_memory_attributes() for a zero mask

This commit skips the xarray index check in
kvm_range_has_vm_memory_attributes() when attrs is 0 so that holes skipped
by xas_next_entry() are not rejected when mask is 0.

> diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
> index 5a1a59d57e0b1..5367804209a2c 100644
> --- a/virt/kvm/kvm_main.c
> +++ b/virt/kvm/kvm_main.c
> @@ -2567,7 +2567,13 @@ bool kvm_range_has_vm_memory_attributes(struct kvm *kvm, gfn_t start, gfn_t end,
>  
>  		WARN_ON_ONCE(!xa_to_value(entry));
>  
> -		if (xas.xa_index != index ||
> +		/*
> +		 * With @attrs == 0, xas_next_entry() can land past @index.
> +		 * Skipped holes and reserved entries match; only real entries
> +		 * with bits in @mask set are rejected. Checking index wrongly
> +		 * rejects holes when @mask is 0.
> +		 */
> +		if ((attrs && xas.xa_index != index) ||
>  		    (xa_to_value(entry) & mask) != attrs)
>  			return false;
>  	}

[Severity: Critical]
This problem wasn't introduced by this patch, but can scanning a large
memslot with non-zero attributes trigger CPU soft lockups and RCU stalls
[1]?

When initializing memory attributes for a memslot,
kvm_mmu_init_memslot_memory_attributes() loops over every potential
hugepage and calls hugepage_has_attrs() ->
kvm_range_has_vm_memory_attributes(), which walks every 4K gfn via
xas_next() when attrs is non-zero:

arch/x86/kvm/mmu/mmu.c:kvm_mmu_init_memslot_memory_attributes() {
	...
	for (level = PG_LEVEL_2M; level <= KVM_MAX_HUGEPAGE_LEVEL; level++) {
		...
		for (gfn = start; gfn < end; gfn += nr_pages) {
			unsigned long attrs = kvm_get_vm_memory_attributes(kvm, gfn);

			if (hugepage_has_attrs(kvm, slot, gfn, level, attrs))
				hugepage_clear_mixed(slot, gfn, level);
	...
}

Neither the outer hugepage loop in
kvm_mmu_init_memslot_memory_attributes() nor this inner xarray loop yields
with cond_resched(), so initializing a large memslot runs through millions
of pages without rescheduling.

[1] https://sashiko.dev/#/bug/linux-6205ce47-c25f-4976-b384-7c6b88c376d3

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-01  1:29 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01  1:21 [PATCH] KVM: Fix kvm_range_has_memory_attributes() for a zero mask liuqiangneo
2026-12-01  1:29 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox