[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20201022113735.ugfe6haxrd22oqjh@box>
Date: Thu, 22 Oct 2020 14:37:35 +0300
From: "Kirill A. Shutemov" <kirill@...temov.name>
To: Ira Weiny <ira.weiny@...el.com>
Cc: Dave Hansen <dave.hansen@...ux.intel.com>,
Andy Lutomirski <luto@...nel.org>,
Peter Zijlstra <peterz@...radead.org>,
Paolo Bonzini <pbonzini@...hat.com>,
Sean Christopherson <sean.j.christopherson@...el.com>,
Vitaly Kuznetsov <vkuznets@...hat.com>,
Wanpeng Li <wanpengli@...cent.com>,
Jim Mattson <jmattson@...gle.com>,
Joerg Roedel <joro@...tes.org>,
David Rientjes <rientjes@...gle.com>,
Andrea Arcangeli <aarcange@...hat.com>,
Kees Cook <keescook@...omium.org>,
Will Drewry <wad@...omium.org>,
"Edgecombe, Rick P" <rick.p.edgecombe@...el.com>,
"Kleen, Andi" <andi.kleen@...el.com>,
Liran Alon <liran.alon@...cle.com>,
Mike Rapoport <rppt@...nel.org>, x86@...nel.org,
kvm@...r.kernel.org, linux-mm@...ck.org,
linux-kernel@...r.kernel.org,
"Kirill A. Shutemov" <kirill.shutemov@...ux.intel.com>
Subject: Re: [RFCv2 08/16] KVM: Use GUP instead of copy_from/to_user() to
access guest memory
On Tue, Oct 20, 2020 at 10:29:44AM -0700, Ira Weiny wrote:
> On Tue, Oct 20, 2020 at 09:18:51AM +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@...ux.intel.com>
> > ---
> > include/linux/kvm_host.h | 4 ++
> > virt/kvm/kvm_main.c | 90 +++++++++++++++++++++++++++++++---------
> > 2 files changed, 75 insertions(+), 19 deletions(-)
> >
> > diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
> > index 05e3c2fb3ef7..380a64613880 100644
> > --- a/include/linux/kvm_host.h
> > +++ b/include/linux/kvm_host.h
> > @@ -504,6 +504,7 @@ struct kvm {
> > struct srcu_struct irq_srcu;
> > pid_t userspace_pid;
> > unsigned int max_halt_poll_ns;
> > + bool mem_protected;
> > };
> >
> > #define kvm_err(fmt, ...) \
> > @@ -728,6 +729,9 @@ void kvm_set_pfn_dirty(kvm_pfn_t pfn);
> > void kvm_set_pfn_accessed(kvm_pfn_t pfn);
> > void kvm_get_pfn(kvm_pfn_t pfn);
> >
> > +int copy_from_guest(void *data, unsigned long hva, int len, bool protected);
> > +int copy_to_guest(unsigned long hva, const void *data, int len, bool protected);
> > +
> > void kvm_release_pfn(kvm_pfn_t pfn, bool dirty, struct gfn_to_pfn_cache *cache);
> > int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
> > int len);
> > diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
> > index cf88233b819a..a9884cb8c867 100644
> > --- a/virt/kvm/kvm_main.c
> > +++ b/virt/kvm/kvm_main.c
> > @@ -2313,19 +2313,70 @@ static int next_segment(unsigned long len, int offset)
> > return len;
> > }
> >
> > +int copy_from_guest(void *data, unsigned long hva, int len, bool protected)
> > +{
> > + int offset = offset_in_page(hva);
> > + struct page *page;
> > + int npages, seg;
> > +
> > + if (!protected)
> > + return __copy_from_user(data, (void __user *)hva, len);
> > +
> > + might_fault();
> > + kasan_check_write(data, len);
> > + check_object_size(data, len, false);
> > +
> > + while ((seg = next_segment(len, offset)) != 0) {
> > + npages = get_user_pages_unlocked(hva, 1, &page, 0);
> > + if (npages != 1)
> > + return -EFAULT;
> > + memcpy(data, page_address(page) + offset, seg);
> > + put_page(page);
> > + len -= seg;
> > + hva += seg;
> > + offset = 0;
>
> Why is data not updated on each iteration?
Ouch. I guess no caller actually steps over page boundary. Will fix.
--
Kirill A. Shutemov
Powered by blists - more mailing lists