Re: [PATCH 04/14] KVM: Play nice with read-only memslots when querying host page size

Paolo Bonzini
2020-01-21T14:24:43Z

On 08/01/20 21:24, Sean Christopherson wrote:
> diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
> index 5f7f06824c2b..d9aced677ddd 100644
> --- a/virt/kvm/kvm_main.c
> +++ b/virt/kvm/kvm_main.c
> @@ -1418,15 +1418,23 @@ EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
>
> unsigned long kvm_host_page_size(struct kvm_vcpu *vcpu, gfn_t gfn)
> {
> + struct kvm_memory_slot *slot;
> struct vm_area_struct *vma;
> unsigned long addr, size;
>
> size = PAGE_SIZE;
>
> - addr = kvm_vcpu_gfn_to_hva(vcpu, gfn);
> - if (kvm_is_error_hva(addr))
> + /*
> + * Manually do the equivalent of kvm_vcpu_gfn_to_hva() to avoid the
> + * "writable" check in __gfn_to_hva_many(), which will always fail on
> + * read-only memslots due to gfn_to_hva() assuming writes.
> + */
> + slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn);
> + if (!slot || slot->flags & KVM_MEMSLOT_INVALID)
> return PAGE_SIZE;
>
> + addr = __gfn_to_hva_memslot(slot, gfn);
> +
> down_read(&current->mm->mmap_sem);
> vma = find_vma(current->mm, addr);
> if (!vma)
>

Even simpler: use kvm_vcpu_gfn_to_hva_prot

- addr = kvm_vcpu_gfn_to_hva(vcpu, gfn);
+ addr = kvm_vcpu_gfn_to_hva_prot(vcpu, gfn, NULL);

"You are in a maze of twisty little functions, all alike".

Paolo