From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 64787C77B76 for ; Mon, 17 Apr 2023 15:09:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Subject:MIME-Version:Message-ID: In-Reply-To:Date:References:Cc:To:From:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=Y1K+7f2JarRElKpadc/J1ZiLrSGZI26M2mbEpBspAIs=; b=Vuk75wH+6/xytho7MHryLLxa18 nIHAVax5bY2zfPj4UjBqIFPkuqXCUF3P81xeZQVfoU8kwI35SwrmGloKIPXPLQO4BeJBovfiz4JqI P//b0Dt9rvRsJ7BmPgXaUA+JZkzUSyWcU3ybLfu65c6faduW0ArvnP1rDdfg/THQWsX3q3kuH96nd e9S1+hWU/LTUoNTu3Bv9rbS2LeDv80/oJKtWFeDNGjdMa9G2Z4jkk/ZmTNstynlROvU1PpKlfH+xI 3+VYGYRaXXr8P7b/Zd5R6gwr1OyidijsDA9VR48qJ3V/KD9H3lNm4DmxaZ9/vQ+nBS7igZpqc++he xnT7T2Aw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1poQTI-00GmIv-1k; Mon, 17 Apr 2023 15:08:48 +0000 Received: from out02.mta.xmission.com ([166.70.13.232]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1poQTG-00GmDX-0W for linux-arm-kernel@lists.infradead.org; Mon, 17 Apr 2023 15:08:47 +0000 Received: from in02.mta.xmission.com ([166.70.13.52]:41666) by out02.mta.xmission.com with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.93) (envelope-from ) id 1poQSt-00AYUx-8R; Mon, 17 Apr 2023 09:08:23 -0600 Received: from ip68-110-29-46.om.om.cox.net ([68.110.29.46]:35070 helo=email.froward.int.ebiederm.org.xmission.com) by in02.mta.xmission.com with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.93) (envelope-from ) id 1poQSs-004oqS-3G; Mon, 17 Apr 2023 09:08:22 -0600 From: "Eric W. Biederman" To: Lorenzo Stoakes Cc: Jason Gunthorpe , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrew Morton , Matthew Wilcox , David Hildenbrand , linux-arm-kernel@lists.infradead.org, kvm@vger.kernel.org, linux-s390@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-perf-users@vger.kernel.org, linux-security-module@vger.kernel.org, Catalin Marinas , Will Deacon , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Heiko Carstens , Vasily Gorbik , Alexander Gordeev , Sven Schnelle , Kees Cook , Alexander Viro , Christian Brauner , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Kentaro Takeda , Tetsuo Handa , Paul Moore , James Morris , "Serge E . Hallyn" , Paolo Bonzini References: <5a4cf1ebf1c6cdfabbf2f5209facb0180dd20006.1681508038.git.lstoakes@gmail.com> <9be77e7e-4531-4e1c-9e0d-4edbb5ad3bd5@lucifer.local> <241f0c22-f3d6-436e-a0d8-be04e281ed2f@lucifer.local> Date: Mon, 17 Apr 2023 10:07:53 -0500 In-Reply-To: <241f0c22-f3d6-436e-a0d8-be04e281ed2f@lucifer.local> (Lorenzo Stoakes's message of "Mon, 17 Apr 2023 14:23:52 +0100") Message-ID: <87cz427diu.fsf@email.froward.int.ebiederm.org> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux) MIME-Version: 1.0 X-XM-SPF: eid=1poQSs-004oqS-3G;;;mid=<87cz427diu.fsf@email.froward.int.ebiederm.org>;;;hst=in02.mta.xmission.com;;;ip=68.110.29.46;;;frm=ebiederm@xmission.com;;;spf=pass X-XM-AID: U2FsdGVkX1/hlfhbMYdh8TSwqCEEto245myqDJhBbl8= X-SA-Exim-Connect-IP: 68.110.29.46 X-SA-Exim-Mail-From: ebiederm@xmission.com Subject: Re: [PATCH 3/7] mm/gup: remove vmas parameter from get_user_pages_remote() X-SA-Exim-Version: 4.2.1 (built Sat, 08 Feb 2020 21:53:50 +0000) X-SA-Exim-Scanned: Yes (on in02.mta.xmission.com) X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230417_080846_226497_EF95CE9D X-CRM114-Status: GOOD ( 38.35 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Lorenzo Stoakes writes: > On Mon, Apr 17, 2023 at 10:16:28AM -0300, Jason Gunthorpe wrote: >> On Mon, Apr 17, 2023 at 02:13:39PM +0100, Lorenzo Stoakes wrote: >> > On Mon, Apr 17, 2023 at 10:09:36AM -0300, Jason Gunthorpe wrote: >> > > On Sat, Apr 15, 2023 at 12:27:31AM +0100, Lorenzo Stoakes wrote: >> > > > The only instances of get_user_pages_remote() invocations which used the >> > > > vmas parameter were for a single page which can instead simply look up the >> > > > VMA directly. In particular:- >> > > > >> > > > - __update_ref_ctr() looked up the VMA but did nothing with it so we simply >> > > > remove it. >> > > > >> > > > - __access_remote_vm() was already using vma_lookup() when the original >> > > > lookup failed so by doing the lookup directly this also de-duplicates the >> > > > code. >> > > > >> > > > This forms part of a broader set of patches intended to eliminate the vmas >> > > > parameter altogether. >> > > > >> > > > Signed-off-by: Lorenzo Stoakes >> > > > --- >> > > > arch/arm64/kernel/mte.c | 5 +++-- >> > > > arch/s390/kvm/interrupt.c | 2 +- >> > > > fs/exec.c | 2 +- >> > > > include/linux/mm.h | 2 +- >> > > > kernel/events/uprobes.c | 10 +++++----- >> > > > mm/gup.c | 12 ++++-------- >> > > > mm/memory.c | 9 +++++---- >> > > > mm/rmap.c | 2 +- >> > > > security/tomoyo/domain.c | 2 +- >> > > > virt/kvm/async_pf.c | 3 +-- >> > > > 10 files changed, 23 insertions(+), 26 deletions(-) >> > > > >> > > > diff --git a/arch/arm64/kernel/mte.c b/arch/arm64/kernel/mte.c >> > > > index f5bcb0dc6267..74d8d4007dec 100644 >> > > > --- a/arch/arm64/kernel/mte.c >> > > > +++ b/arch/arm64/kernel/mte.c >> > > > @@ -437,8 +437,9 @@ static int __access_remote_tags(struct mm_struct *mm, unsigned long addr, >> > > > struct page *page = NULL; >> > > > >> > > > ret = get_user_pages_remote(mm, addr, 1, gup_flags, &page, >> > > > - &vma, NULL); >> > > > - if (ret <= 0) >> > > > + NULL); >> > > > + vma = vma_lookup(mm, addr); >> > > > + if (ret <= 0 || !vma) >> > > > break; >> > > >> > > Given the slightly tricky error handling, it would make sense to turn >> > > this pattern into a helper function: >> > > >> > > page = get_single_user_page_locked(mm, addr, gup_flags, &vma); >> > > if (IS_ERR(page)) >> > > [..] >> > > >> > > static inline struct page *get_single_user_page_locked(struct mm_struct *mm, >> > > unsigned long addr, int gup_flags, struct vm_area_struct **vma) >> > > { >> > > struct page *page; >> > > int ret; >> > > >> > > ret = get_user_pages_remote(*mm, addr, 1, gup_flags, &page, NULL, NULL); >> > > if (ret < 0) >> > > return ERR_PTR(ret); >> > > if (WARN_ON(ret == 0)) >> > > return ERR_PTR(-EINVAL); >> > > *vma = vma_lookup(mm, addr); >> > > if (WARN_ON(!*vma) { >> > > put_user_page(page); >> > > return ERR_PTR(-EINVAL); >> > > } >> > > return page; >> > > } >> > > >> > > It could be its own patch so this change was just a mechanical removal >> > > of NULL >> > > >> > > Jason >> > > >> > >> > Agreed, I think this would work better as a follow up patch however so as >> > not to distract too much from the core change. >> >> I don't think you should open code sketchy error handling in several >> places and then clean it up later. Just do it right from the start. >> > > Intent was to do smallest change possible (though through review that grew > of course), but I see your point, in this instance this is fiddly stuff and > probably better to abstract it to enforce correct handling. > > I'll respin + add something like this. Could you include in your description why looking up the vma after getting the page does not introduce a race? I am probably silly and just looking at this quickly but it does not seem immediately obvious why the vma and the page should match. I would not be surprised if you hold the appropriate mutex over the entire operation but it just isn't apparent from the diff. I am concerned because it is an easy mistake to refactor something into two steps and then discover you have introduced a race. Eric _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel