Received: by 10.192.165.156 with SMTP id m28csp3011390imm; Sun, 15 Apr 2018 14:58:47 -0700 (PDT) X-Google-Smtp-Source: AIpwx4/pjoQ+8ASb+Oyp6Wn4F6tVNUG3HR94LTAek3XiVRq460o1+SBN6SAz7D5WXIhQwodexMto X-Received: by 2002:a17:902:bd41:: with SMTP id b1-v6mr8210920plx.302.1523829527056; Sun, 15 Apr 2018 14:58:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1523829527; cv=none; d=google.com; s=arc-20160816; b=ii/Zwn04Xua2HSw90hdvrPYuQ/pP/krnbXNp6XpapIWURlVZym8LiFMzgXjDdxgDtV n0E73QHubpA8mXCA+cEzHR90JthEAoONfNwRlVvnplP3759HC7chH0YzxeYyGj5I8Vxv H7VIz+PusguNHu8oH9VZ/ULiXgF4dS0FcZD6doGF1CZ/WDmskkwhUj2KTk3qp/RQMLAM 02fWcQ0WsU4eXhnmrbDH+/Iq1ujV/noGzbbLV8XSu/ZQ4RmSH5J4Ie3lf6sY/kZ/0Hlk 3YAIhAizDWGNXCBoleFhKx3mpDn5ZNxiAR9q9pdfaN79OnrlooR56OLhN9KDZCakhaFa YT9w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=2t4M3WORcUSN3z8hjlgpHvYj8VxP9yEhRLOS8D7JvKg=; b=CI2A0I1AEYX9YYr1k7uBvkfed8q7d2is92FCc8zIVtJi8f7WkQgWnpM4jDqZIkbrFy 6G3PWk+d5C42fD0Se/t6mRHrygskuBLCgR38xrZCzJZTK8yjm4rDBTSQ5MtqLADmXrBF hOwG4EEns32PUmWELIt9pzdpW2mYcEOIKoBh5k8rhLPDac8/dYP/MA3uyvji8upN6ufw Gke5oAlapjZPMfCjQWuY5ffP1/1PqTpHXkUL3uol9Iwq4jcazXHgef6661FoXLOl6kPb 7PK/w1kjmNqum3pWFRBzBJjwpnW1ApN5OpUnr4UsZfquPTM2NUAk/7jWc8+b5kmmHHog Sz7Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.de header.s=amazon201209 header.b=l/3a9SyW; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.de Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id m11si8625633pgd.683.2018.04.15.14.58.05; Sun, 15 Apr 2018 14:58:47 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.de header.s=amazon201209 header.b=l/3a9SyW; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752833AbeDOVzE (ORCPT + 99 others); Sun, 15 Apr 2018 17:55:04 -0400 Received: from smtp-fw-6002.amazon.com ([52.95.49.90]:24005 "EHLO smtp-fw-6002.amazon.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751489AbeDOVyz (ORCPT ); Sun, 15 Apr 2018 17:54:55 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.de; i=@amazon.de; q=dns/txt; s=amazon201209; t=1523829295; x=1555365295; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=2t4M3WORcUSN3z8hjlgpHvYj8VxP9yEhRLOS8D7JvKg=; b=l/3a9SyWCkRsP2GR+EMan2cNJPIgyA4eoFJdGQy7Nk4tAN4pK0grBLNT AIWmEwZtzNtBvPaj/ta4s2LVFMRAM0jx+F6nEOBMIzpDoDZxUeCLM0CKt CCudWvNJTEtFEcp3mcsvfaSq+LROdfP//bGZkizJA0miskgxgGU2W+Odw E=; X-IronPort-AV: E=Sophos;i="5.48,456,1517875200"; d="scan'208";a="339950784" Received: from iad6-co-svc-p1-lb1-vlan3.amazon.com (HELO email-inbound-relay-1d-98acfc19.us-east-1.amazon.com) ([10.124.125.6]) by smtp-border-fw-out-6002.iad6.amazon.com with ESMTP/TLS/DHE-RSA-AES256-SHA; 15 Apr 2018 21:54:54 +0000 Received: from u54e1ad5160425a4b64ea.ant.amazon.com (iad1-ws-svc-lb91-vlan2.amazon.com [10.0.103.146]) by email-inbound-relay-1d-98acfc19.us-east-1.amazon.com (8.14.7/8.14.7) with ESMTP id w3FLsnCg041825 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Sun, 15 Apr 2018 21:54:52 GMT Received: from u54e1ad5160425a4b64ea.ant.amazon.com (localhost [127.0.0.1]) by u54e1ad5160425a4b64ea.ant.amazon.com (8.15.2/8.15.2/Debian-3) with ESMTP id w3FLsnGo013426; Sun, 15 Apr 2018 23:54:49 +0200 Received: (from karahmed@localhost) by u54e1ad5160425a4b64ea.ant.amazon.com (8.15.2/8.15.2/Submit) id w3FLsmTl013424; Sun, 15 Apr 2018 23:54:48 +0200 From: KarimAllah Ahmed To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, pbonzini@redhat.com, rkrcmar@redhat.com Cc: Filippo Sironi , KarimAllah Ahmed Subject: [PATCH v2 04/12] X86/KVM: Handle PFNs outside of kernel reach when touching GPTEs Date: Sun, 15 Apr 2018 23:53:10 +0200 Message-Id: <1523829198-13236-5-git-send-email-karahmed@amazon.de> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1523829198-13236-1-git-send-email-karahmed@amazon.de> References: <1523829198-13236-1-git-send-email-karahmed@amazon.de> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Filippo Sironi cmpxchg_gpte() calls get_user_pages_fast() to retrieve the number of pages and the respective struct page to map 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 back 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 Signed-off-by: KarimAllah Ahmed --- arch/x86/kvm/paging_tmpl.h | 38 +++++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/arch/x86/kvm/paging_tmpl.h b/arch/x86/kvm/paging_tmpl.h index 6288e9d..31a37e4 100644 --- a/arch/x86/kvm/paging_tmpl.h +++ b/arch/x86/kvm/paging_tmpl.h @@ -141,15 +141,35 @@ 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); } -- 2.7.4