Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp1025504imu; Wed, 23 Jan 2019 09:34:19 -0800 (PST) X-Google-Smtp-Source: ALg8bN6Cfa0uoKBW9tNakb1DcMSgdnTfE/OIA5cnJT4XLJWiFkesHsrKOanc3RXY4i0+/WPhXnuq X-Received: by 2002:a17:902:714c:: with SMTP id u12mr3071793plm.234.1548264858963; Wed, 23 Jan 2019 09:34:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1548264858; cv=none; d=google.com; s=arc-20160816; b=qCK7gVWP5EVOrMWwW+D8i9+jRXMSKh5+G4fa5wdZQvrVyjYQu4I+UiU1A7C+6GisNV R91Y8JjXSD92UerpMDGoJIeclnQ0tuWZIj90/Hd8uoLFCaEPmSk5YYccW7RSiiBoUFqG +XwKlSuWzsiF805agONt252PnVl0XfDmYyaaQ8bvX+UoFHkAoA5P3JXsinRTSg+Bkcdd 5e79wUfwghQNWXw26c6GpIZbZAlT1BOTEy8pATCdaWuGM+GHICg8hhxWCNoVFfT2L6sw bvOKlCcQ+pIzfqwdACetIhltz/qYy/LVxt3DPVHY+UlnMRUSMHeG/PEcHWaasA59l/RT 7/Eg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=5NZpbGt9tGthD9CDDy0CZNcq7WVuy+ghR2I4NRadhKY=; b=lobQNYXYkCyVlr+qgC9N8XkBBWAg74DUR51/50c5g6ofmDJhdAAF+ylHGCPl34GZN4 YQE7hT/5ayGTBDruosnD4X22iIItdG1nyZ+UlzyBNEe3GNRErbBxGFxpCpDSE9T+1eeO 4nZ4lJ8Tsyinj3g5WkxEuf2BK+Nr5vBzG8NSykZpy63PapAwyF36Bj+4z+rRbx0WO9y+ T6appvTqCtTLBkhBnHYRH2QxaFpKXhobLB/slaP+EeyCm61XL9Ko4RM5kvaCj59T+l42 WtQr/u4UnpUNjIZHFhpCYseXFpTPvWCq4M7+WyRqdYej2c0zbArZxDvhW6Yc88gYzllE u1xw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2018-07-02 header.b=t9HAEXn3; 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=NONE sp=NONE dis=NONE) header.from=oracle.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id l5si18348079plt.5.2019.01.23.09.33.51; Wed, 23 Jan 2019 09:34:18 -0800 (PST) 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=@oracle.com header.s=corp-2018-07-02 header.b=t9HAEXn3; 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=NONE sp=NONE dis=NONE) header.from=oracle.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726202AbfAWRb7 (ORCPT + 99 others); Wed, 23 Jan 2019 12:31:59 -0500 Received: from userp2130.oracle.com ([156.151.31.86]:47348 "EHLO userp2130.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725896AbfAWRb7 (ORCPT ); Wed, 23 Jan 2019 12:31:59 -0500 Received: from pps.filterd (userp2130.oracle.com [127.0.0.1]) by userp2130.oracle.com (8.16.0.22/8.16.0.22) with SMTP id x0NHSb0A087718; Wed, 23 Jan 2019 17:31:53 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=date : from : to : cc : subject : message-id : references : mime-version : content-type : in-reply-to; s=corp-2018-07-02; bh=5NZpbGt9tGthD9CDDy0CZNcq7WVuy+ghR2I4NRadhKY=; b=t9HAEXn3T7/DOG6U41IvV4tHyDUbZVm5UAxCKK/CjzJKikVnxd1By2h311WlAuWIaVLE GqmrmZwAnM5xxg/GQSYF88bveCRDS25W1xQCW8q2Fqn2FivxQbc+kcUff2ZYRmDqfvFx xQeBhFrI7Wt0SAxu4vQTgLAvpJip6W15aoGBRf414rJ/jWg+KOq/0LiVQ150UjsCgsCk c8lC/MlOKu0viyR1Dw4MgNO7fCceu5TAIngRtid9ufO1qnL8erjm3RlO/JPTzOLPGrP8 i5Okr42iX8tKHXVA+3xH+tFL3dYvC5U6tCuepmENahGMdYkk/OLEAObPVZLXaNhX0OJK Sw== Received: from aserv0022.oracle.com (aserv0022.oracle.com [141.146.126.234]) by userp2130.oracle.com with ESMTP id 2q3uauubu2-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 23 Jan 2019 17:31:53 +0000 Received: from aserv0121.oracle.com (aserv0121.oracle.com [141.146.126.235]) by aserv0022.oracle.com (8.14.4/8.14.4) with ESMTP id x0NHVqUK025248 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 23 Jan 2019 17:31:52 GMT Received: from abhmp0018.oracle.com (abhmp0018.oracle.com [141.146.116.24]) by aserv0121.oracle.com (8.14.4/8.13.8) with ESMTP id x0NHVqv2012793; Wed, 23 Jan 2019 17:31:52 GMT Received: from Konrads-MacBook-Pro.local (/75.104.64.133) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Wed, 23 Jan 2019 09:31:52 -0800 Date: Wed, 23 Jan 2019 12:31:29 -0500 From: Konrad Rzeszutek Wilk To: KarimAllah Ahmed Cc: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, pbonzini@redhat.com, rkrcmar@redhat.com, Filippo Sironi Subject: Re: [PATCH v5 03/13] X86/KVM: Handle PFNs outside of kernel reach when touching GPTEs Message-ID: <20190123173051.GH19289@Konrads-MacBook-Pro.local> References: <1547026933-31226-1-git-send-email-karahmed@amazon.de> <1547026933-31226-4-git-send-email-karahmed@amazon.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1547026933-31226-4-git-send-email-karahmed@amazon.de> User-Agent: Mutt/1.9.4 (2018-02-28) X-Proofpoint-Virus-Version: vendor=nai engine=5900 definitions=9145 signatures=668682 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 malwarescore=0 phishscore=0 bulkscore=0 spamscore=0 mlxscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1810050000 definitions=main-1901230129 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Jan 09, 2019 at 10:42:03AM +0100, KarimAllah Ahmed wrote: > 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 I personally would have used some crafty goto statements to jump to 'err' label which would have up_read(¤t->mm->mmap_sem); return -EFAULT; which would be after 154 return (ret != orig_pte); But that is bike-shedding so feel free to ignore it. Either way: Reviewed-by: Konrad Rzeszutek Wilk > --- > 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 6bdca39..c40af67 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 >