Skip to content

Commit e300614

Browse files
committed
KVM: cleanup and add shortcuts to kvm_range_has_memory_attributes()
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 <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent de80252 commit e300614

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
@@ -2398,6 +2398,14 @@ static int kvm_vm_ioctl_clear_dirty_log(struct kvm *kvm,
23982398
#endif /* CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT */
23992399

24002400
#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES
2401+
static u64 kvm_supported_mem_attributes(struct kvm *kvm)
2402+
{
2403+
if (!kvm || kvm_arch_has_private_mem(kvm))
2404+
return KVM_MEMORY_ATTRIBUTE_PRIVATE;
2405+
2406+
return 0;
2407+
}
2408+
24012409
/*
24022410
* Returns true if _all_ gfns in the range [@start, @end) have attributes
24032411
* matching @attrs.
@@ -2406,40 +2414,30 @@ bool kvm_range_has_memory_attributes(struct kvm *kvm, gfn_t start, gfn_t end,
24062414
unsigned long attrs)
24072415
{
24082416
XA_STATE(xas, &kvm->mem_attr_array, start);
2417+
unsigned long mask = kvm_supported_mem_attributes(kvm);
24092418
unsigned long index;
2410-
bool has_attrs;
24112419
void *entry;
24122420

2413-
rcu_read_lock();
2421+
if (attrs & ~mask)
2422+
return false;
24142423

2415-
if (!attrs) {
2416-
has_attrs = !xas_find(&xas, end - 1);
2417-
goto out;
2418-
}
2424+
if (end == start + 1)
2425+
return kvm_get_memory_attributes(kvm, start) == attrs;
2426+
2427+
guard(rcu)();
2428+
if (!attrs)
2429+
return !xas_find(&xas, end - 1);
24192430

2420-
has_attrs = true;
24212431
for (index = start; index < end; index++) {
24222432
do {
24232433
entry = xas_next(&xas);
24242434
} while (xas_retry(&xas, entry));
24252435

2426-
if (xas.xa_index != index || xa_to_value(entry) != attrs) {
2427-
has_attrs = false;
2428-
break;
2429-
}
2436+
if (xas.xa_index != index || xa_to_value(entry) != attrs)
2437+
return false;
24302438
}
24312439

2432-
out:
2433-
rcu_read_unlock();
2434-
return has_attrs;
2435-
}
2436-
2437-
static u64 kvm_supported_mem_attributes(struct kvm *kvm)
2438-
{
2439-
if (!kvm || kvm_arch_has_private_mem(kvm))
2440-
return KVM_MEMORY_ATTRIBUTE_PRIVATE;
2441-
2442-
return 0;
2440+
return true;
24432441
}
24442442

24452443
static __always_inline void kvm_handle_gfn_range(struct kvm *kvm,

0 commit comments

Comments
 (0)