From patchwork Wed Apr 5 13:07:02 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Sironi, Filippo" X-Patchwork-Id: 9663979 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 6A1C3602B8 for ; Wed, 5 Apr 2017 13:17:17 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 52516200F5 for ; Wed, 5 Apr 2017 13:17:17 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 46D5F205D1; Wed, 5 Apr 2017 13:17:17 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 00254200F5 for ; Wed, 5 Apr 2017 13:17:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933148AbdDENPb (ORCPT ); Wed, 5 Apr 2017 09:15:31 -0400 Received: from smtp-fw-6002.amazon.com ([52.95.49.90]:16733 "EHLO smtp-fw-6002.amazon.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933142AbdDENPZ (ORCPT ); Wed, 5 Apr 2017 09:15:25 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.de; i=@amazon.de; q=dns/txt; s=amazon201209; t=1491398125; x=1522934125; h=from:to:cc:subject:date:message-id; bh=jSstceUWNOv83tgH6HVpv2GLXHfIiBFspDPTXkVpcIQ=; b=raWYVZDjWms9sKy7NbK/iVbMHWPfjyZCZsiErvkJvDX9rthJ1abydzjc 3oVT+7lCoPB16E4Bk5McDiUaa9LJntY3K+XVo9xrnhif6N6pBlQsaARRP /JsLvv9x3ATDicIkawdteAo0MYsdCR0rAmU2mMFTBExtopJ4+nfOFE9kz Y=; X-IronPort-AV: E=Sophos;i="5.36,278,1486425600"; d="scan'208";a="267025272" Received: from iad6-co-svc-p1-lb1-vlan3.amazon.com (HELO email-inbound-relay-60013.pdx1.amazon.com) ([10.124.125.6]) by smtp-border-fw-out-6002.iad6.amazon.com with ESMTP/TLS/DHE-RSA-AES256-SHA; 05 Apr 2017 13:09:15 +0000 Received: from uf8b156e456a5587c9af4.ant.amazon.com (pdx2-ws-svc-lb17-vlan3.amazon.com [10.247.140.70]) by email-inbound-relay-60013.pdx1.amazon.com (8.14.7/8.14.7) with ESMTP id v35D9BVd020231 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Wed, 5 Apr 2017 13:09:12 GMT Received: from uf8b156e456a5587c9af4.ant.amazon.com (localhost [127.0.0.1]) by uf8b156e456a5587c9af4.ant.amazon.com (8.15.2/8.15.2/Debian-3) with ESMTP id v35D9Aeu016763; Wed, 5 Apr 2017 15:09:10 +0200 Received: (from sironi@localhost) by uf8b156e456a5587c9af4.ant.amazon.com (8.15.2/8.15.2/Submit) id v35D9Ai1016760; Wed, 5 Apr 2017 15:09:10 +0200 From: Filippo Sironi To: sironi@amazon.com Cc: Filippo Sironi , Anthony Liguori , kvm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] x86, kvm: Handle PFNs outside of kernel reach when touching GPTEs Date: Wed, 5 Apr 2017 15:07:02 +0200 Message-Id: <1491397622-16665-1-git-send-email-sironi@amazon.de> X-Mailer: git-send-email 2.7.4 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP cmpxchg_gpte() calls get_user_pages_fast() to retrieve the number of pages and the respective struct pages for mapping in the kernel virtual address space. This doesn't work if get_user_pages_fast() is invoked with a userspace virtual address that's backed by PFNs outside of kernel reach (e.g., when limiting the kernel memory with mem= in the command line and using /dev/mem to map memory). If get_user_pages_fast() fails, look up the VMA that backs the userspace virtual address, compute the PFN and the physical address, and map it in the kernel virtual address space with memremap(). Signed-off-by: Filippo Sironi Cc: Anthony Liguori Cc: kvm@vger.kernel.org Cc: linux-kernel@vger.kernel.org --- arch/x86/kvm/paging_tmpl.h | 39 ++++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/arch/x86/kvm/paging_tmpl.h b/arch/x86/kvm/paging_tmpl.h index a01105485315..ab4d6617238c 100644 --- a/arch/x86/kvm/paging_tmpl.h +++ b/arch/x86/kvm/paging_tmpl.h @@ -147,15 +147,36 @@ static int FNAME(cmpxchg_gpte)(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, struct page *page; npages = get_user_pages_fast((unsigned long)ptep_user, 1, 1, &page); - /* Check if the user is doing something meaningless. */ - if (unlikely(npages != 1)) - return -EFAULT; - - table = kmap_atomic(page); - ret = CMPXCHG(&table[index], orig_pte, new_pte); - kunmap_atomic(table); - - kvm_release_page_dirty(page); + if (likely(npages == 1)) { + table = kmap_atomic(page); + ret = CMPXCHG(&table[index], orig_pte, new_pte); + kunmap_atomic(table); + + kvm_release_page_dirty(page); + } else { + struct vm_area_struct *vma; + unsigned long vaddr = (unsigned long)ptep_user & PAGE_MASK; + unsigned long pfn; + unsigned long paddr; + + down_read(¤t->mm->mmap_sem); + vma = find_vma_intersection(current->mm, vaddr, + vaddr + PAGE_SIZE); + if (!vma || !(vma->vm_flags & VM_PFNMAP)) { + up_read(¤t->mm->mmap_sem); + return -EFAULT; + } + pfn = ((vaddr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; + paddr = pfn << PAGE_SHIFT; + table = memremap(paddr, PAGE_SIZE, MEMREMAP_WB); + if (!table) { + up_read(¤t->mm->mmap_sem); + return -EFAULT; + } + ret = CMPXCHG(&table[index], orig_pte, new_pte); + memunmap(table); + up_read(¤t->mm->mmap_sem); + } return (ret != orig_pte); }