Received: by 2002:a05:6358:53a8:b0:117:f937:c515 with SMTP id z40csp4123537rwe; Mon, 17 Apr 2023 08:15:27 -0700 (PDT) X-Google-Smtp-Source: AKy350ZjlPn+iyvYi1CNLde9/n2s1g6YYxyThiCysD6Iwcv6nYPcbiVzpc7pF+H8tth71N4CROBK X-Received: by 2002:a17:902:d353:b0:19e:61cc:6793 with SMTP id l19-20020a170902d35300b0019e61cc6793mr12347386plk.48.1681744527173; Mon, 17 Apr 2023 08:15:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681744527; cv=none; d=google.com; s=arc-20160816; b=qBgmhvNg1rKY+xsF2Od2OLzhvNNFT3J1x+krkDjtoGczfuB7WN+kR4sJybEBckeMal xA2WueVD3OL0U2ctw5IIKWxZehFoL/TER7lecQn1HDQ6L2kobo4z5MITkqtlAPk9ty6J 9vY1+bHh6Z0oE2QAV8coWBq7DM995IkUwYGzcyVTZMaCJNBnJwpl4RS+IhRXNM/wyyOW i7Ivd3DdP2BiJYc17Ny1dOMmAWnL0kfdr51Raf/bmvz0XBxvqAnk0rpboeeFDJ4d7HJK wt5HDaVft8oLCi+tk9Ce9+8Qj8TPxEMJ1ZO+1XVjn/nWHKjRP76LaJ83gZKt51otVniU jg/g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=e047zcn20dpW+BVxg6WrLYEA74ZTRhvYR9ivWfzVffI=; b=Sp97KmRp52cL4Bo7KbTIc3yNmvjYmqdu8+mr6Kll5+5pcjnq8lXsHupCthN6m9vW05 vTpNA8dIcNIZhYH5LZfLT8ciTkvYyNEu9lwjXpUWZ2Q/IvC/EK6Wwlatl2DIBxnickPV r/UfXlQRTqu8viih69YQP0JyK6OEH0sRGeeEH8VMx6Ae1Z1R0nq6ZLAhaCXErPRuVGOg 1TDsZxcRvQUGR0cG2Qqwhwev7RbosDDviyr6dq6NNqzlcRfUKJ0yRLLQ376JbXgbxcTy 6boSLlSSBtbx7v/EpDcrkNQsD0bMW3IfcMdEPDaoeW/9Ij9WpQY/poElPZQGxf1ErGz6 aOlQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=J6mNTdjJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j5-20020a170902c08500b001a6f49761c6si579437pld.433.2023.04.17.08.15.15; Mon, 17 Apr 2023 08:15:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=J6mNTdjJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230338AbjDQPOx (ORCPT + 99 others); Mon, 17 Apr 2023 11:14:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39314 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230052AbjDQPOu (ORCPT ); Mon, 17 Apr 2023 11:14:50 -0400 Received: from mail-wm1-x32f.google.com (mail-wm1-x32f.google.com [IPv6:2a00:1450:4864:20::32f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0EA40FB; Mon, 17 Apr 2023 08:14:37 -0700 (PDT) Received: by mail-wm1-x32f.google.com with SMTP id 5b1f17b1804b1-3f09b9ac51dso44003695e9.0; Mon, 17 Apr 2023 08:14:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1681744475; x=1684336475; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=e047zcn20dpW+BVxg6WrLYEA74ZTRhvYR9ivWfzVffI=; b=J6mNTdjJXRwthJ2zCT9NEY6Z9hezMuH8x4BK/1R4BCVgk7Ci/HPN+jrcwQsKcCuTkI I1UPqwJz5ABCTk7X5opTEZxdgNLtiV+gU4ZZA1u010mlWUCYGH/bChtn7BAwLYDZ3JHk RTI8uWQsSY0HbGSVWTcygeXA3TlQzhMwGYmt3V1Id6ozIaB9PWoHyCMpAq+wrBMf57z1 SGYdFjrENk4shHH5MXl7JnDBNcBGd0m9BV/6PlyFsgA6iVj6APNfSGWyjUQ/JZLa59nc mSzqI9Ijxbahm/QxlPpW5/WVK0DYMNibbaiJENbR3l7PJJ/9luB7VdSEH8WgJVZjnIyE roWw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681744475; x=1684336475; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=e047zcn20dpW+BVxg6WrLYEA74ZTRhvYR9ivWfzVffI=; b=WBcBkbbaQqQNO2EzqeDrWHWVJ212chCFO1VLfgN9SH6RSufnjrVHf6kiHrFPn0rV9K +tRHipe2oMx8fjZlW8fxbMn/dR5vEgfXYdHLbikX1/aYwG46hCF7I2qSQgbm+3qU4HyM 2E9v5S7P/aMWBUGsJQJBE2ZzaOKO41pxOf37dAeVcIU9G4tsiMpHPNVhEZcFkEcMl6BW n70KqLWMoiq96NDGFZKo6ERrX0Ii8wyckT1OEIiDZoWYaBgEHc8kHgGc4i57suqip30T YMhzvnES5C+KEMpHZS3qI0fjRFVeQ3G+PxjiQQXD5JQJJWjR6f051VB+PCh5wc2hA9GB SF7A== X-Gm-Message-State: AAQBX9ecJRV9oOfUDy+O+OB3DaT5DTrt2iQDK3qlyQMXCWlGHAyywTHR uLaJ/LOAR4cN9pQdIo0C+nE= X-Received: by 2002:adf:cf01:0:b0:2fa:abcd:59a2 with SMTP id o1-20020adfcf01000000b002faabcd59a2mr2273209wrj.30.1681744475252; Mon, 17 Apr 2023 08:14:35 -0700 (PDT) Received: from localhost (host86-156-84-164.range86-156.btcentralplus.com. [86.156.84.164]) by smtp.gmail.com with ESMTPSA id i4-20020a5d55c4000000b002f74578f494sm8341442wrw.41.2023.04.17.08.14.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Apr 2023 08:14:34 -0700 (PDT) Date: Mon, 17 Apr 2023 16:14:33 +0100 From: Lorenzo Stoakes To: "Eric W. Biederman" 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 Subject: Re: [PATCH 3/7] mm/gup: remove vmas parameter from get_user_pages_remote() Message-ID: References: <5a4cf1ebf1c6cdfabbf2f5209facb0180dd20006.1681508038.git.lstoakes@gmail.com> <9be77e7e-4531-4e1c-9e0d-4edbb5ad3bd5@lucifer.local> <241f0c22-f3d6-436e-a0d8-be04e281ed2f@lucifer.local> <87cz427diu.fsf@email.froward.int.ebiederm.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <87cz427diu.fsf@email.froward.int.ebiederm.org> X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Apr 17, 2023 at 10:07:53AM -0500, Eric W. Biederman wrote: > 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 > The mmap_lock is held so VMAs cannot be altered and no such race can occur. get_user_pages_remote() requires that the user calls it under the lock so it is implicitly assured that this cannot happen. I'll update the description to make this clear on the next spin! (side-note: here is another irritating issue with GUP, we don't suffix with _locked() consistently)