Create a new function kvm_is_visible_memslot() and use it from kvm_is_visible_gfn(); use the new function in try_async_pf() too, to avoid an extra memslot lookup. Opportunistically squish a multi-line comment into a single-line comment. Note, the end result, KVM_PFN_NOSLOT, is unchanged.
Unified · Split
| @@ -4082,19 +4082,16 @@ static bool try_async_pf(struct kvm_vcpu *vcpu, bool prefault, gfn_t gfn, · 4081 unmodified lines | ||
| 4082 | 4082 | gpa_t cr2_or_gpa, kvm_pfn_t *pfn, bool write, |
| 4083 | 4083 | bool *writable) |
| 4084 | 4084 | { |
| 4085 | − struct kvm_memory_slot *slot; | |
| 4085 | + struct kvm_memory_slot *slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn); | |
| 4086 | 4086 | bool async; |
| 4087 | 4087 | |
| 4088 | − /* | |
| 4089 | − * Don't expose private memslots to L2. | |
| 4090 | − */ | |
| 4091 | − if (is_guest_mode(vcpu) && !kvm_is_visible_gfn(vcpu->kvm, gfn)) { | |
| 4088 | + /* Don't expose private memslots to L2. */ | |
| 4089 | + if (is_guest_mode(vcpu) && !kvm_is_visible_memslot(slot)) { | |
| 4092 | 4090 | *pfn = KVM_PFN_NOSLOT; |
| 4093 | 4091 | *writable = false; |
| 4094 | 4092 | return false; |
| 4095 | 4093 | } |
| 4096 | 4094 | |
| 4097 | − slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn); | |
| 4098 | 4095 | async = false; |
| 4099 | 4096 | *pfn = __gfn_to_pfn_memslot(slot, gfn, false, &async, write, writable); |
| 4100 | 4097 | if (!async) |
| @@ -1357,6 +1357,12 @@ static inline void kvm_vcpu_set_dy_eligible(struct kvm_vcpu *vcpu, bool val) · 1356 unmodified lines | ||
| 1357 | 1357 | } |
| 1358 | 1358 | #endif /* CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT */ |
| 1359 | 1359 | |
| 1360 | +static inline bool kvm_is_visible_memslot(struct kvm_memory_slot *memslot) | |
| 1361 | +{ | |
| 1362 | + return (memslot && memslot->id < KVM_USER_MEM_SLOTS && | |
| 1363 | + !(memslot->flags & KVM_MEMSLOT_INVALID)); | |
| 1364 | +} | |
| 1365 | + | |
| 1360 | 1366 | struct kvm_vcpu *kvm_get_running_vcpu(void); |
| 1361 | 1367 | struct kvm_vcpu * __percpu *kvm_get_running_vcpus(void); |
| 1362 | 1368 | |
| @@ -1607,11 +1607,7 @@ bool kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn) · 1606 unmodified lines | ||
| 1607 | 1607 | { |
| 1608 | 1608 | struct kvm_memory_slot *memslot = gfn_to_memslot(kvm, gfn); |
| 1609 | 1609 | |
| 1610 | − if (!memslot || memslot->id >= KVM_USER_MEM_SLOTS || | |
| 1611 | − memslot->flags & KVM_MEMSLOT_INVALID) | |
| 1612 | − return false; | |
| 1613 | − | |
| 1614 | − return true; | |
| 1610 | + return kvm_is_visible_memslot(memslot); | |
| 1615 | 1611 | } |
| 1616 | 1612 | EXPORT_SYMBOL_GPL(kvm_is_visible_gfn); |
| 1617 | 1613 | |