From patchwork Sat Jun 30 20:17:48 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alex Williamson X-Patchwork-Id: 10498399 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 38747603B4 for ; Sat, 30 Jun 2018 20:18:03 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2630828CD4 for ; Sat, 30 Jun 2018 20:18:03 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 198DF28CE5; Sat, 30 Jun 2018 20:18:03 +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.9 required=2.0 tests=BAYES_00, MAILING_LIST_MULTI, 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 B1CA328CD4 for ; Sat, 30 Jun 2018 20:18:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751521AbeF3URv (ORCPT ); Sat, 30 Jun 2018 16:17:51 -0400 Received: from mx1.redhat.com ([209.132.183.28]:33428 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751466AbeF3URu (ORCPT ); Sat, 30 Jun 2018 16:17:50 -0400 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.24]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id D59F23082A25; Sat, 30 Jun 2018 20:17:49 +0000 (UTC) Received: from t450s.home (ovpn-116-135.phx2.redhat.com [10.3.116.135]) by smtp.corp.redhat.com (Postfix) with ESMTP id C3B91308BDB0; Sat, 30 Jun 2018 20:17:48 +0000 (UTC) Date: Sat, 30 Jun 2018 14:17:48 -0600 From: Alex Williamson To: Jason Gunthorpe Cc: linux-rdma@vger.kernel.org, Dan Williams , Michal Hocko , kvm@vger.kernel.org, Haozhong Zhang , Christoph Hellwig , Huy Nguyen , Leon Romanovsky Subject: Re: [PATCH vfio] vfio: Use get_user_pages_longterm correctly Message-ID: <20180630141748.41f0b1c7@t450s.home> In-Reply-To: <20180629173150.GA24833@ziepe.ca> References: <20180629173150.GA24833@ziepe.ca> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.24 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.45]); Sat, 30 Jun 2018 20:17:50 +0000 (UTC) Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP On Fri, 29 Jun 2018 11:31:50 -0600 Jason Gunthorpe wrote: > The patch noted in the fixes below converted get_user_pages_fast() to > get_user_pages_longterm(), however the two calls differ in a few ways. > > First _fast() is documented to not require the mmap_sem, while _longterm() > is documented to need it. Hold the mmap sem as required. > > Second, _fast accepts an 'int write' while _longterm uses 'unsigned int > gup_flags', so the expression '!!(prot & IOMMU_WRITE)' is only working by > luck as FOLL_WRITE is currently == 0x1. Use the expected FOLL_WRITE > constant instead. > > Fixes: 94db151dc892 ("vfio: disable filesystem-dax page pinning") > Cc: > Signed-off-by: Jason Gunthorpe > --- > drivers/vfio/vfio_iommu_type1.c | 4 ++-- > 1 file changed, 2 insertions(+), 2 deletions(-) Minor change as shown below, we don't need both branches coming up with the FOLL_WRITE flag in slightly different ways. > I noticed this while trying to review some RDMA code that was touching > our get_user_pages_longterm() call site and wanted to see what others > are doing.. > > If someone can explain that get_user_pages_longterm() is safe to call > without the mmap_sem held I'd love to here it! Me too :-\ > The comments in gup.c do seem to pretty clearly state the > __get_user_pages_locked() called internally by > get_user_pages_longterm() needs mmap_sem held.. > > This is confusing me because this is the only > get_user_pages_longterm() callsite that doesn't hold the mmap_sem, and > if it really isn't required I'd like to remove it from the RDMA code > as well :) commit 0e81a8fc0411c9baec88f3f65154285fede473f6 Author: Jason Gunthorpe Date: Fri Jun 29 11:31:50 2018 -0600 vfio: Use get_user_pages_longterm correctly The patch noted in the fixes below converted get_user_pages_fast() to get_user_pages_longterm(), however the two calls differ in a few ways. First _fast() is documented to not require the mmap_sem, while _longterm() is documented to need it. Hold the mmap sem as required. Second, _fast accepts an 'int write' while _longterm uses 'unsigned int gup_flags', so the expression '!!(prot & IOMMU_WRITE)' is only working by luck as FOLL_WRITE is currently == 0x1. Use the expected FOLL_WRITE constant instead. Fixes: 94db151dc892 ("vfio: disable filesystem-dax page pinning") Cc: Signed-off-by: Jason Gunthorpe Signed-off-by: Alex Williamson diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c index 2c75b33db4ac..3e5b17710a4f 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -343,18 +343,16 @@ static int vaddr_get_pfn(struct mm_struct *mm, unsigned long vaddr, struct page *page[1]; struct vm_area_struct *vma; struct vm_area_struct *vmas[1]; + unsigned int flags = 0; int ret; + if (prot & IOMMU_WRITE) + flags |= FOLL_WRITE; + + down_read(&mm->mmap_sem); if (mm == current->mm) { - ret = get_user_pages_longterm(vaddr, 1, !!(prot & IOMMU_WRITE), - page, vmas); + ret = get_user_pages_longterm(vaddr, 1, flags, page, vmas); } else { - unsigned int flags = 0; - - if (prot & IOMMU_WRITE) - flags |= FOLL_WRITE; - - down_read(&mm->mmap_sem); ret = get_user_pages_remote(NULL, mm, vaddr, 1, flags, page, vmas, NULL); /* @@ -368,8 +366,8 @@ static int vaddr_get_pfn(struct mm_struct *mm, unsigned long vaddr, ret = -EOPNOTSUPP; put_page(page[0]); } - up_read(&mm->mmap_sem); } + up_read(&mm->mmap_sem); if (ret == 1) { *pfn = page_to_pfn(page[0]);