Received: by 2002:a25:7ec1:0:0:0:0:0 with SMTP id z184csp1829673ybc; Wed, 13 Nov 2019 05:05:55 -0800 (PST) X-Google-Smtp-Source: APXvYqzEiwpokTyRC7TlH04hnmUeudEZda+RygbeE1EBfk8tqNn1R8eNDMT8rbQAY89aL8measN7 X-Received: by 2002:a17:906:4bcb:: with SMTP id x11mr2726770ejv.100.1573650355448; Wed, 13 Nov 2019 05:05:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1573650355; cv=none; d=google.com; s=arc-20160816; b=y9LEt0BN+/qnF8letcK86BnXCD4hC78GzZsCspLm3ZLgcgKlpSYRiVcPKiWSBcJdRW SLBBVWyd7Jxocf+YM9LRjCHC5Tt8P/OZupUlioOBfj0yUl417A8D0MMOCAc7haKjcouE xEqDA3DZvnRtFg2ghsL8qK47YpZQoXWjT1yvPz31ldiU0f2BSYKTaDPhmiaoImAfMtNf br/MLIgMr15oBq3KZ1+QVLauKTQhkrNb7YNrn12J/N9bKMVQP8Uml5c0gTz4IVjDKOiw JNnjw28Vxt+kOXAyEuEogswVZtHBogLf6DoiA6tGTeaIFyjvUUWcVj2ybfVLklab/mB5 2cAA== 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=oMayMeIif+74vqoR9Hg244XFAjXhPl1nedXCyPx66Cc=; b=kcbDbuA1NRChXXudXjOB7yozdTiL4vWn71pqNNEAxHEJhUBvxpXy2XNEGhJQNiBK3k sTluL06lrHKO4dPFAjsOwGvgoOEPtN//6FU3182UGfnw10qOjpVNWxWZ+GC76kXH0T/O 1pUeYEQ9u3LHZKodCMCHVvOCYiX2VBgaOcRpmaqSVlJk1erSP+HRI3CkYe0kfYmeMTP9 Od29gAy2r6031Vjt3f9A0sc5OziojvRwW3Qn13Jk3MuZ13MVb380SWdrLBifaGoKSlga 9F2IxkPngnrej1y2lG9H57AL2F8xmJYv7KCFnAED1PHbwqcDpvPQYdY3pRqeMHp+SmAm X4ow== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ziepe.ca header.s=google header.b=nx3682LY; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f2si1084623ejf.231.2019.11.13.05.05.30; Wed, 13 Nov 2019 05:05:55 -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=@ziepe.ca header.s=google header.b=nx3682LY; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727279AbfKMNCG (ORCPT + 99 others); Wed, 13 Nov 2019 08:02:06 -0500 Received: from mail-qt1-f194.google.com ([209.85.160.194]:41922 "EHLO mail-qt1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727015AbfKMNCF (ORCPT ); Wed, 13 Nov 2019 08:02:05 -0500 Received: by mail-qt1-f194.google.com with SMTP id o3so2436216qtj.8 for ; Wed, 13 Nov 2019 05:02:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ziepe.ca; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=oMayMeIif+74vqoR9Hg244XFAjXhPl1nedXCyPx66Cc=; b=nx3682LY4M40wGJ/sVGEu1B6oRowx3yCQZUXaDyQPZ8wp2PPM1JbbnvXpnwJ+1w7kN lveYAqZtu6ZiCNE2GeOgCzqCbaPveYJh+bEnyDI89VRNGCHDXS9A7L5ZDDa96GF+3bm0 VmapuAT4FqF5vTUEuSGlcRA+NIY79GOrBmWnh2u551hGbiYfLURBD9nS87LvoXBLbrK0 o1TB/3BSIGkHlZVczCO3ubOoj5u0dVQwEYLzu7svoxCzx/+RmvcCH+ACjaNBf3sl94Zf NRZR8twQGuvzHtghZ4nzQu/TUj0hyyqfJ3dXa+O6cdFNGC6w6BZNjOabe9aQdvwtecNu AUCw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=oMayMeIif+74vqoR9Hg244XFAjXhPl1nedXCyPx66Cc=; b=XtbgfuVmpU+73eLdAGSZvmeMCCLq6/0ZYPfwUvfT3y3qHnffC4D7Dtsjv+5xZarEpS vLCB2sbMOzdaZc1tlULGuuhE3VyJby9hAJbUC7npfISTtG3+Xk3wxQv0kY5sUGrepbsQ Je9qAZZAjympfUGw9YTb2krzoIWB42R0dLzBRMhohFNB1ucA7kAVk9vYoJpjia5A3tZX NTNRa+Yv/NDu2H+QPnqku+Fv53Ki1vYMa3mQCrQel6aVUu7kyQv751YrcSWxlTC9jsI2 wxAMpgMYDelZgBOTQA6sRP2tgnKKNifKOUU7hVMMknG3YgooNSvtIVG2wp1VHmGLTGn3 q1AA== X-Gm-Message-State: APjAAAVCAFNKZ+Y+JJTrXheG4hb61ugvoGFw7txTYO+DLALtecodcB/F CQxkBuZMygYBDMioi6NGaFRdug== X-Received: by 2002:ac8:724f:: with SMTP id l15mr2476006qtp.234.1573650124104; Wed, 13 Nov 2019 05:02:04 -0800 (PST) Received: from ziepe.ca (hlfxns017vw-142-162-113-180.dhcp-dynamic.fibreop.ns.bellaliant.net. [142.162.113.180]) by smtp.gmail.com with ESMTPSA id 187sm918223qkk.103.2019.11.13.05.02.03 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 13 Nov 2019 05:02:03 -0800 (PST) Received: from jgg by mlx.ziepe.ca with local (Exim 4.90_1) (envelope-from ) id 1iUsHa-000767-Jc; Wed, 13 Nov 2019 09:02:02 -0400 Date: Wed, 13 Nov 2019 09:02:02 -0400 From: Jason Gunthorpe To: John Hubbard Cc: Andrew Morton , Al Viro , Alex Williamson , Benjamin Herrenschmidt , =?utf-8?B?QmrDtnJuIFTDtnBlbA==?= , Christoph Hellwig , Dan Williams , Daniel Vetter , Dave Chinner , David Airlie , "David S . Miller" , Ira Weiny , Jan Kara , Jens Axboe , Jonathan Corbet , =?utf-8?B?SsOpcsO0bWU=?= Glisse , Magnus Karlsson , Mauro Carvalho Chehab , Michael Ellerman , Michal Hocko , Mike Kravetz , Paul Mackerras , Shuah Khan , Vlastimil Babka , bpf@vger.kernel.org, dri-devel@lists.freedesktop.org, kvm@vger.kernel.org, linux-block@vger.kernel.org, linux-doc@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-media@vger.kernel.org, linux-rdma@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, netdev@vger.kernel.org, linux-mm@kvack.org, LKML Subject: Re: [PATCH v4 08/23] vfio, mm: fix get_user_pages_remote() and FOLL_LONGTERM Message-ID: <20191113130202.GA26068@ziepe.ca> References: <20191113042710.3997854-1-jhubbard@nvidia.com> <20191113042710.3997854-9-jhubbard@nvidia.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20191113042710.3997854-9-jhubbard@nvidia.com> User-Agent: Mutt/1.9.4 (2018-02-28) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Nov 12, 2019 at 08:26:55PM -0800, John Hubbard wrote: > As it says in the updated comment in gup.c: current FOLL_LONGTERM > behavior is incompatible with FAULT_FLAG_ALLOW_RETRY because of the > FS DAX check requirement on vmas. > > However, the corresponding restriction in get_user_pages_remote() was > slightly stricter than is actually required: it forbade all > FOLL_LONGTERM callers, but we can actually allow FOLL_LONGTERM callers > that do not set the "locked" arg. > > Update the code and comments accordingly, and update the VFIO caller > to take advantage of this, fixing a bug as a result: the VFIO caller > is logically a FOLL_LONGTERM user. > > Also, remove an unnessary pair of calls that were releasing and > reacquiring the mmap_sem. There is no need to avoid holding mmap_sem > just in order to call page_to_pfn(). > > Also, move the DAX check ("if a VMA is DAX, don't allow long term > pinning") from the VFIO call site, all the way into the internals > of get_user_pages_remote() and __gup_longterm_locked(). That is: > get_user_pages_remote() calls __gup_longterm_locked(), which in turn > calls check_dax_vmas(). It's lightly explained in the comments as well. > > Thanks to Jason Gunthorpe for pointing out a clean way to fix this, > and to Dan Williams for helping clarify the DAX refactoring. > > Suggested-by: Jason Gunthorpe > Cc: Dan Williams > Cc: Jerome Glisse > Cc: Ira Weiny > Signed-off-by: John Hubbard > drivers/vfio/vfio_iommu_type1.c | 25 ++----------------------- > mm/gup.c | 27 ++++++++++++++++++++++----- > 2 files changed, 24 insertions(+), 28 deletions(-) > > diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c > index d864277ea16f..7301b710c9a4 100644 > +++ b/drivers/vfio/vfio_iommu_type1.c > @@ -340,7 +340,6 @@ 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; > > @@ -348,33 +347,13 @@ static int vaddr_get_pfn(struct mm_struct *mm, unsigned long vaddr, > flags |= FOLL_WRITE; > > down_read(&mm->mmap_sem); > - if (mm == current->mm) { > - ret = get_user_pages(vaddr, 1, flags | FOLL_LONGTERM, page, > - vmas); > - } else { > - ret = get_user_pages_remote(NULL, mm, vaddr, 1, flags, page, > - vmas, NULL); > - /* > - * The lifetime of a vaddr_get_pfn() page pin is > - * userspace-controlled. In the fs-dax case this could > - * lead to indefinite stalls in filesystem operations. > - * Disallow attempts to pin fs-dax pages via this > - * interface. > - */ > - if (ret > 0 && vma_is_fsdax(vmas[0])) { > - ret = -EOPNOTSUPP; > - put_page(page[0]); > - } > - } > - up_read(&mm->mmap_sem); > - > + ret = get_user_pages_remote(NULL, mm, vaddr, 1, flags | FOLL_LONGTERM, > + page, NULL, NULL); > if (ret == 1) { > *pfn = page_to_pfn(page[0]); > return 0; Mind the return with the lock held this needs some goto unwind Jason