Re: [PATCH v6 6/8] KVM: Handle page fault for private memory

From: Sean Christopherson
Date: Thu Aug 25 2022 - 19:43:45 EST


On Fri, Aug 19, 2022, Kirill A. Shutemov wrote:
> On Fri, Jun 17, 2022 at 09:30:53PM +0000, Sean Christopherson wrote:
> > > @@ -4088,7 +4144,12 @@ static int direct_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault
> > > read_unlock(&vcpu->kvm->mmu_lock);
> > > else
> > > write_unlock(&vcpu->kvm->mmu_lock);
> > > - kvm_release_pfn_clean(fault->pfn);
> > > +
> > > + if (fault->is_private)
> > > + kvm_private_mem_put_pfn(fault->slot, fault->pfn);
> >
> > Why does the shmem path lock the page, and then unlock it here?
>
> Lock is require to avoid race with truncate / punch hole. Like if truncate
> happens after get_pfn(), but before it gets into SEPT we are screwed.

Getting the PFN into the SPTE doesn't provide protection in and of itself. The
protection against truncation and whatnot comes from KVM getting a notification
and either retrying the fault (notification acquires mmu_lock before
direct_page_fault()), or blocking the notification (truncate / punch hole) until
after KVM installs the SPTE. I.e. KVM just needs to ensure it doesn't install a
SPTE _after_ getting notified.

If the API is similar to gup(), i.e. only elevates the refcount but doesn't lock
the page, then there's no need for a separate kvm_private_mem_put_pfn(), and in
fact no need for ->put_unlock_pfn() because can KVM do set_page_dirty() and
put_page() directly as needed using all of KVM's existing mechanisms.