Skip to content

Commit 4b5f671

Browse files
committed
KVM: extend kvm_range_has_memory_attributes() to check subset of attributes
While currently there is no other attribute than KVM_MEMORY_ATTRIBUTE_PRIVATE, KVM code such as kvm_mem_is_private() is written to expect their existence. Allow using kvm_range_has_memory_attributes() as a multi-page version of kvm_mem_is_private(), without it breaking later when more attributes are introduced. Reviewed-by: Michael Roth <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent e300614 commit 4b5f671

File tree

3 files changed

+9
-8
lines changed

3 files changed

+9
-8
lines changed

arch/x86/kvm/mmu/mmu.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7513,7 +7513,7 @@ static bool hugepage_has_attrs(struct kvm *kvm, struct kvm_memory_slot *slot,
75137513
const unsigned long end = start + KVM_PAGES_PER_HPAGE(level);
75147514

75157515
if (level == PG_LEVEL_2M)
7516-
return kvm_range_has_memory_attributes(kvm, start, end, attrs);
7516+
return kvm_range_has_memory_attributes(kvm, start, end, ~0, attrs);
75177517

75187518
for (gfn = start; gfn < end; gfn += KVM_PAGES_PER_HPAGE(level - 1)) {
75197519
if (hugepage_test_mixed(slot, gfn, level - 1) ||

include/linux/kvm_host.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2414,7 +2414,7 @@ static inline unsigned long kvm_get_memory_attributes(struct kvm *kvm, gfn_t gfn
24142414
}
24152415

24162416
bool kvm_range_has_memory_attributes(struct kvm *kvm, gfn_t start, gfn_t end,
2417-
unsigned long attrs);
2417+
unsigned long mask, unsigned long attrs);
24182418
bool kvm_arch_pre_set_memory_attributes(struct kvm *kvm,
24192419
struct kvm_gfn_range *range);
24202420
bool kvm_arch_post_set_memory_attributes(struct kvm *kvm,

virt/kvm/kvm_main.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2408,21 +2408,21 @@ static u64 kvm_supported_mem_attributes(struct kvm *kvm)
24082408

24092409
/*
24102410
* Returns true if _all_ gfns in the range [@start, @end) have attributes
2411-
* matching @attrs.
2411+
* such that the bits in @mask match @attrs.
24122412
*/
24132413
bool kvm_range_has_memory_attributes(struct kvm *kvm, gfn_t start, gfn_t end,
2414-
unsigned long attrs)
2414+
unsigned long mask, unsigned long attrs)
24152415
{
24162416
XA_STATE(xas, &kvm->mem_attr_array, start);
2417-
unsigned long mask = kvm_supported_mem_attributes(kvm);
24182417
unsigned long index;
24192418
void *entry;
24202419

2420+
mask &= kvm_supported_mem_attributes(kvm);
24212421
if (attrs & ~mask)
24222422
return false;
24232423

24242424
if (end == start + 1)
2425-
return kvm_get_memory_attributes(kvm, start) == attrs;
2425+
return (kvm_get_memory_attributes(kvm, start) & mask) == attrs;
24262426

24272427
guard(rcu)();
24282428
if (!attrs)
@@ -2433,7 +2433,8 @@ bool kvm_range_has_memory_attributes(struct kvm *kvm, gfn_t start, gfn_t end,
24332433
entry = xas_next(&xas);
24342434
} while (xas_retry(&xas, entry));
24352435

2436-
if (xas.xa_index != index || xa_to_value(entry) != attrs)
2436+
if (xas.xa_index != index ||
2437+
(xa_to_value(entry) & mask) != attrs)
24372438
return false;
24382439
}
24392440

@@ -2532,7 +2533,7 @@ static int kvm_vm_set_mem_attributes(struct kvm *kvm, gfn_t start, gfn_t end,
25322533
mutex_lock(&kvm->slots_lock);
25332534

25342535
/* Nothing to do if the entire range as the desired attributes. */
2535-
if (kvm_range_has_memory_attributes(kvm, start, end, attributes))
2536+
if (kvm_range_has_memory_attributes(kvm, start, end, ~0, attributes))
25362537
goto out_unlock;
25372538

25382539
/*

0 commit comments

Comments
 (0)