Re: [RFC 06/16] KVM: Use GUP instead of copy_from/to_user() to access guest memory

From: Kirill A. Shutemov
Date: Tue May 26 2020 - 17:56:27 EST


On Tue, May 26, 2020 at 09:14:59AM +0300, Mike Rapoport wrote:
> On Fri, May 22, 2020 at 03:52:04PM +0300, Kirill A. Shutemov wrote:
> > New helpers copy_from_guest()/copy_to_guest() to be used if KVM memory
> > protection feature is enabled.
> >
> > Signed-off-by: Kirill A. Shutemov <kirill.shutemov@xxxxxxxxxxxxxxx>
> > ---
> > include/linux/kvm_host.h | 4 +++
> > virt/kvm/kvm_main.c | 78 ++++++++++++++++++++++++++++++++++------
> > 2 files changed, 72 insertions(+), 10 deletions(-)
> >
> > static int __kvm_read_guest_page(struct kvm_memory_slot *slot, gfn_t gfn,
> > - void *data, int offset, int len)
> > + void *data, int offset, int len,
> > + bool protected)
> > {
> > int r;
> > unsigned long addr;
> > @@ -2257,7 +2297,10 @@ static int __kvm_read_guest_page(struct kvm_memory_slot *slot, gfn_t gfn,
> > addr = gfn_to_hva_memslot_prot(slot, gfn, NULL);
> > if (kvm_is_error_hva(addr))
> > return -EFAULT;
> > - r = __copy_from_user(data, (void __user *)addr + offset, len);
> > + if (protected)
> > + r = copy_from_guest(data, addr + offset, len);
> > + else
> > + r = __copy_from_user(data, (void __user *)addr + offset, len);
>
> Maybe always use copy_{from,to}_guest() and move the 'if (protected)'
> there?
> If kvm is added to memory slot, it cab be the passed to copy_{to,from}_guest.

Right, Vitaly has pointed me to this already.

--
Kirill A. Shutemov