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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id E34CEC77B76 for ; Mon, 17 Apr 2023 15:08:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 595016B0071; Mon, 17 Apr 2023 11:08:33 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 545358E0002; Mon, 17 Apr 2023 11:08:33 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 40CE48E0001; Mon, 17 Apr 2023 11:08:33 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 32BF16B0071 for ; Mon, 17 Apr 2023 11:08:33 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id E3185A065A for ; Mon, 17 Apr 2023 15:08:32 +0000 (UTC) X-FDA: 80691214464.01.13F6BA7 Received: from out02.mta.xmission.com (out02.mta.xmission.com [166.70.13.232]) by imf10.hostedemail.com (Postfix) with ESMTP id 5E09FC002F for ; Mon, 17 Apr 2023 15:08:29 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=xmission.com; spf=pass (imf10.hostedemail.com: domain of ebiederm@xmission.com designates 166.70.13.232 as permitted sender) smtp.mailfrom=ebiederm@xmission.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1681744109; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=eo/m/FappIvUqyuRkx9slYROzbave6/29QHLnfReu3c=; b=vQdVlKA/Iv8SA3qPAEa8g/RWWOoV/HUE/zMNay0XsB6NR3WPWm6ErAO7xB18nRXpzkb35e ep0CvWS4sOS9iu+DqJDvkW+lU/4bxtMjLpA/Wa0TG8kAeeKfHtjg8sFxMZhXCis54TRYGe F3El1ivzQmTiP5muIK8SLNpP/pThKdw= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=xmission.com; spf=pass (imf10.hostedemail.com: domain of ebiederm@xmission.com designates 166.70.13.232 as permitted sender) smtp.mailfrom=ebiederm@xmission.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1681744109; a=rsa-sha256; cv=none; b=MbiAg5j4FbU+D5fxvDj8wMMIObLauhhntwRmxCYnGpB4/Wy65mf07dVCxeI67NYYtPvLMd uXySHihfMRFla8iniKdyaYHuo0dI8UcVVkiqqcwvn46ZdVVS1j0get9CqbdKNyeBX3Tx+9 tJsfy4xUKred+hGl1cyoMzo0QPT0/Jc= 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 Content-Type: text/plain 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-Rspamd-Queue-Id: 5E09FC002F X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: is3rgp8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U3PAScBnsuUQA6bfy/foHU/K5Z2gsw7Gw+ZnsPFNHr2NXAgIg= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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