Skip to content

Commit 8f2d1be

Browse files
committed
KVM: extend kvm_range_has_memory_attributes() to check subset of attributes
JIRA: https://issues.redhat.com/browse/RHEL-32435 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 <michael.roth@amd.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com> (cherry picked from commit 4b5f671) Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent 1006304 commit 8f2d1be

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
@@ -7430,7 +7430,7 @@ static bool hugepage_has_attrs(struct kvm *kvm, struct kvm_memory_slot *slot,
74307430
const unsigned long end = start + KVM_PAGES_PER_HPAGE(level);
74317431

74327432
if (level == PG_LEVEL_2M)
7433-
return kvm_range_has_memory_attributes(kvm, start, end, attrs);
7433+
return kvm_range_has_memory_attributes(kvm, start, end, ~0, attrs);
74347434

74357435
for (gfn = start; gfn < end; gfn += KVM_PAGES_PER_HPAGE(level - 1)) {
74367436
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
@@ -2412,7 +2412,7 @@ static inline unsigned long kvm_get_memory_attributes(struct kvm *kvm, gfn_t gfn
24122412
}
24132413

24142414
bool kvm_range_has_memory_attributes(struct kvm *kvm, gfn_t start, gfn_t end,
2415-
unsigned long attrs);
2415+
unsigned long mask, unsigned long attrs);
24162416
bool kvm_arch_pre_set_memory_attributes(struct kvm *kvm,
24172417
struct kvm_gfn_range *range);
24182418
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
@@ -2417,21 +2417,21 @@ static u64 kvm_supported_mem_attributes(struct kvm *kvm)
24172417

24182418
/*
24192419
* Returns true if _all_ gfns in the range [@start, @end) have attributes
2420-
* matching @attrs.
2420+
* such that the bits in @mask match @attrs.
24212421
*/
24222422
bool kvm_range_has_memory_attributes(struct kvm *kvm, gfn_t start, gfn_t end,
2423-
unsigned long attrs)
2423+
unsigned long mask, unsigned long attrs)
24242424
{
24252425
XA_STATE(xas, &kvm->mem_attr_array, start);
2426-
unsigned long mask = kvm_supported_mem_attributes(kvm);
24272426
unsigned long index;
24282427
void *entry;
24292428

2429+
mask &= kvm_supported_mem_attributes(kvm);
24302430
if (attrs & ~mask)
24312431
return false;
24322432

24332433
if (end == start + 1)
2434-
return kvm_get_memory_attributes(kvm, start) == attrs;
2434+
return (kvm_get_memory_attributes(kvm, start) & mask) == attrs;
24352435

24362436
guard(rcu)();
24372437
if (!attrs)
@@ -2442,7 +2442,8 @@ bool kvm_range_has_memory_attributes(struct kvm *kvm, gfn_t start, gfn_t end,
24422442
entry = xas_next(&xas);
24432443
} while (xas_retry(&xas, entry));
24442444

2445-
if (xas.xa_index != index || xa_to_value(entry) != attrs)
2445+
if (xas.xa_index != index ||
2446+
(xa_to_value(entry) & mask) != attrs)
24462447
return false;
24472448
}
24482449

@@ -2541,7 +2542,7 @@ static int kvm_vm_set_mem_attributes(struct kvm *kvm, gfn_t start, gfn_t end,
25412542
mutex_lock(&kvm->slots_lock);
25422543

25432544
/* Nothing to do if the entire range as the desired attributes. */
2544-
if (kvm_range_has_memory_attributes(kvm, start, end, attributes))
2545+
if (kvm_range_has_memory_attributes(kvm, start, end, ~0, attributes))
25452546
goto out_unlock;
25462547

25472548
/*

0 commit comments

Comments
 (0)