Skip to content

Commit 1006304

Browse files
committed
KVM: cleanup and add shortcuts to kvm_range_has_memory_attributes()
JIRA: https://issues.redhat.com/browse/RHEL-32435 Use a guard to simplify early returns, and add two more easy shortcuts. If the requested attributes are invalid, the attributes xarray will never show them as set. And if testing a single page, kvm_get_memory_attributes() is more efficient. Reviewed-by: Michael Roth <michael.roth@amd.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com> (cherry picked from commit e300614) Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent b548786 commit 1006304

File tree

1 file changed

+20
-22
lines changed

1 file changed

+20
-22
lines changed

virt/kvm/kvm_main.c

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2407,6 +2407,14 @@ static int kvm_vm_ioctl_clear_dirty_log(struct kvm *kvm,
24072407
#endif /* CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT */
24082408

24092409
#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES
2410+
static u64 kvm_supported_mem_attributes(struct kvm *kvm)
2411+
{
2412+
if (!kvm || kvm_arch_has_private_mem(kvm))
2413+
return KVM_MEMORY_ATTRIBUTE_PRIVATE;
2414+
2415+
return 0;
2416+
}
2417+
24102418
/*
24112419
* Returns true if _all_ gfns in the range [@start, @end) have attributes
24122420
* matching @attrs.
@@ -2415,40 +2423,30 @@ bool kvm_range_has_memory_attributes(struct kvm *kvm, gfn_t start, gfn_t end,
24152423
unsigned long attrs)
24162424
{
24172425
XA_STATE(xas, &kvm->mem_attr_array, start);
2426+
unsigned long mask = kvm_supported_mem_attributes(kvm);
24182427
unsigned long index;
2419-
bool has_attrs;
24202428
void *entry;
24212429

2422-
rcu_read_lock();
2430+
if (attrs & ~mask)
2431+
return false;
24232432

2424-
if (!attrs) {
2425-
has_attrs = !xas_find(&xas, end - 1);
2426-
goto out;
2427-
}
2433+
if (end == start + 1)
2434+
return kvm_get_memory_attributes(kvm, start) == attrs;
2435+
2436+
guard(rcu)();
2437+
if (!attrs)
2438+
return !xas_find(&xas, end - 1);
24282439

2429-
has_attrs = true;
24302440
for (index = start; index < end; index++) {
24312441
do {
24322442
entry = xas_next(&xas);
24332443
} while (xas_retry(&xas, entry));
24342444

2435-
if (xas.xa_index != index || xa_to_value(entry) != attrs) {
2436-
has_attrs = false;
2437-
break;
2438-
}
2445+
if (xas.xa_index != index || xa_to_value(entry) != attrs)
2446+
return false;
24392447
}
24402448

2441-
out:
2442-
rcu_read_unlock();
2443-
return has_attrs;
2444-
}
2445-
2446-
static u64 kvm_supported_mem_attributes(struct kvm *kvm)
2447-
{
2448-
if (!kvm || kvm_arch_has_private_mem(kvm))
2449-
return KVM_MEMORY_ATTRIBUTE_PRIVATE;
2450-
2451-
return 0;
2449+
return true;
24522450
}
24532451

24542452
static __always_inline void kvm_handle_gfn_range(struct kvm *kvm,

0 commit comments

Comments
 (0)