Received: by 2002:a05:6a10:c7c6:0:0:0:0 with SMTP id h6csp2868764pxy; Tue, 3 Aug 2021 18:09:58 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyH4Fk8hukNHwsKBPcXjk4Jgh6cEjgckt617sd8B+p/V7cx47xNKQ7gl2USFQarUg6Y9X9o X-Received: by 2002:a50:f113:: with SMTP id w19mr27865861edl.107.1628039397953; Tue, 03 Aug 2021 18:09:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1628039397; cv=none; d=google.com; s=arc-20160816; b=gS9c6cKnq+pADNAB5kiYnrbIqhj378ALrPoY+SFzOXlxMCgZb2r8CwCn1uVOv+jgHV ovv1af6TvGCDuIbyt5dVd3fEOaP7qtZ1QcYFQF0QOCK5xQx93Hc7YsrNzD9T/0aP4Z/F B48q908c6e1A0umpXtRBGWdsR/t4RJPc/VdwdXEjIZqe3i4S4Bj9rVACnnv8fp2gNJXA qCEuh5w24dOp362foZw2XY2dXPbJTy1LQ+QaF3fWfyZ5wEQqXyC/ZF3N4n8laeYA5ahP GF05D5o30lbapIiS3tVoace9Rk9aU7/yLncHZCMU2HlrZtu4/XWc5yf9zJUG38B/fO/Y Tmog== 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=uhDyUj2VSVRpxbG/0UXwj6BIhNgU6btFMLRW7jRZu+0=; b=jAp2WJqvoKg8cxtKX7CyuOWKM3lJ/FI5bWTRXWm1jJhXEBrN8JnF1IQMuZCzILq3D5 y7u1h+InJP/VSDc2raH0MwamZ6Cb7VyWb4f5Rbl9eMoV6oo8YcD0lwV6j7heFFnt9vV/ CrJtzVowjVWZSTXccETxKccqUNvB321xoadTiMo+QyPjd2kfkr4KfhmL1Q25Los8CKDY TEvBuKnmQVwjsHVVk72gVEpHEtx/hkD21EPpHk/rGKMIAkBWZiAzfPBR1t2c09VIr/Tj VDvpAIZehsj1Rgjbt82xHwaV+lu5t5M1xZVrIG8EjIlhly8IgUw2yF230l++d3ofXmaS pa6A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ziepe.ca header.s=google header.b=kZtnicXF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id rs17si484687ejb.193.2021.08.03.18.09.20; Tue, 03 Aug 2021 18:09:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@ziepe.ca header.s=google header.b=kZtnicXF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234248AbhHCXf5 (ORCPT + 99 others); Tue, 3 Aug 2021 19:35:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50410 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234178AbhHCXf4 (ORCPT ); Tue, 3 Aug 2021 19:35:56 -0400 Received: from mail-qk1-x730.google.com (mail-qk1-x730.google.com [IPv6:2607:f8b0:4864:20::730]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D0FB1C061757 for ; Tue, 3 Aug 2021 16:35:44 -0700 (PDT) Received: by mail-qk1-x730.google.com with SMTP id k7so1091316qki.11 for ; Tue, 03 Aug 2021 16:35:44 -0700 (PDT) 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; bh=uhDyUj2VSVRpxbG/0UXwj6BIhNgU6btFMLRW7jRZu+0=; b=kZtnicXFx9GD20ho5v8snd5tko753aUlUgTIuPa6FbiGiWZEJWWlvpZ+LQ8DqwRVMW tW/sbXcwp7Y00/DWrE8kE3MLU6XaxvcZQmjBeduRZm56uPe8Io+SoqspP6ERGOXZGXlP sMcMCUbCZl4uF7hFRclzuzwt3GcSQu5qp/w/3BwfR9pXXVP1cfr5PPTrCreq+C6D7Yj9 Vq17Iaymneru9n4bdT/CJxdAu4/2oF6aIFlTFu0RTyIgkvxZ8ZhfJ0CZXtfpwn2p4JzW FhEn9esnkhvsSGsZvkfYjZEd+dqK3fACCSHC5yoB2kHUg/Brs6CtNpj6jujsECF1WHQl yCaw== 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; bh=uhDyUj2VSVRpxbG/0UXwj6BIhNgU6btFMLRW7jRZu+0=; b=O7eJYA1n330M5l7SJVKWkjJm2LlZU9/5zClzKmhMoCe6a4+s3u86USZBVH2749k++N VNcJR4W7x2IJJuRuB1ba6Nk+Av+7OCQgf2KRi5mbaZII7A8L4hlYpl+yNPAdEkRH/RrC OwCmrzR5OhAa1RhGswTWDWwakyJYap3Lk5TPt/XTPimlAr2C25tgTZFJD9g3mGAAOMIF JXpdu7YMFwHk3LXJFzVL0SKPsDx6XGCYvi5V4rUzlHZY3pll1i/PCdFhurXb9tv/NCdl pUQ/UWycQccMrgxCD6GHaoe/Iimhf4DiSfQVmH3F4PSUeE9+FJG4ASMHm7pp11CXC/FQ UFww== X-Gm-Message-State: AOAM533BOtt3Blh7jYCiYNoS+VhEnce7cLQ0z3DA8UC1jnPpDv4PK0DC +xVCIACp7JURj/imN/wHBq85sg== X-Received: by 2002:a05:620a:64c:: with SMTP id a12mr18105687qka.377.1628033744011; Tue, 03 Aug 2021 16:35:44 -0700 (PDT) Received: from ziepe.ca ([206.223.160.26]) by smtp.gmail.com with ESMTPSA id 28sm309576qkp.26.2021.08.03.16.35.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Aug 2021 16:35:43 -0700 (PDT) Received: from jgg by mlx with local (Exim 4.94) (envelope-from ) id 1mB3wk-00CMna-H4; Tue, 03 Aug 2021 20:35:42 -0300 Date: Tue, 3 Aug 2021 20:35:42 -0300 From: Jason Gunthorpe To: Liam Howlett Cc: Luigi Rizzo , Jann Horn , linux-kernel , Andrew Morton , David Rientjes , "linux-mm@kvack.org" Subject: Re: [PATCH] Add mmap_assert_locked() annotations to find_vma*() Message-ID: <20210803233542.GH543798@ziepe.ca> References: <20210731175341.3458608-1-lrizzo@google.com> <20210803160803.GG543798@ziepe.ca> <20210803230725.ao3i2emejyyor36n@revolver> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210803230725.ao3i2emejyyor36n@revolver> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Aug 03, 2021 at 11:07:35PM +0000, Liam Howlett wrote: > * Luigi Rizzo [210803 17:49]: > > On Tue, Aug 3, 2021 at 6:08 PM Jason Gunthorpe wrote: > > > > > > On Sat, Jul 31, 2021 at 10:53:41AM -0700, Luigi Rizzo wrote: > > > > find_vma() and variants need protection when used. > > > > This patch adds mmap_assert_lock() calls in the functions. > > > > > > > > To make sure the invariant is satisfied, we also need to add a > > > > mmap_read_loc() around the get_user_pages_remote() call in > > > > get_arg_page(). The lock is not strictly necessary because the mm > > > > has been newly created, but the extra cost is limited because > > > > the same mutex was also acquired shortly before in __bprm_mm_init(), > > > > so it is hot and uncontended. > > > > > > > > Signed-off-by: Luigi Rizzo > > > > fs/exec.c | 2 ++ > > > > mm/mmap.c | 2 ++ > > > > 2 files changed, 4 insertions(+) > > > > > > > > diff --git a/fs/exec.c b/fs/exec.c > > > > index 38f63451b928..ac7603e985b4 100644 > > > > +++ b/fs/exec.c > > > > @@ -217,8 +217,10 @@ static struct page *get_arg_page(struct linux_binprm *bprm, unsigned long pos, > > > > * We are doing an exec(). 'current' is the process > > > > * doing the exec and bprm->mm is the new process's mm. > > > > */ > > > > + mmap_read_lock(bprm->mm); > > > > ret = get_user_pages_remote(bprm->mm, pos, 1, gup_flags, > > > > &page, NULL, NULL); > > > > + mmap_read_unlock(bprm->mm); > > > > if (ret <= 0) > > > > return NULL; > > > > > > Wasn't Jann Horn working on something like this too? > > > > > > https://lore.kernel.org/linux-mm/20201016225713.1971256-1-jannh@google.com/ > > > > > > IIRC it was very tricky here, are you sure it is OK to obtain this lock > > > here? > > > > I cannot comment on Jann's patch series but no other thread knows > > about this mm at this point in the code so the lock is definitely > > safe to acquire (shortly before there was also a write lock acquired > > on the same mm, in the same conditions). > > If there is no other code that knows about this mm, then does one need > the lock at all? Is this just to satisfy the new check you added? > > If you want to make this change, I would suggest writing it in a way to > ensure the call to expand_downwards() in the same function also holds > the lock. I believe this is technically required as well? What do you > think? This is essentially what Jann was doing. Since the mm is newly created we can create it write locked and then we can add proper locking tests to many of the functions called along this path. Adding useless locks around each troublesome callsite just seems really confusing to me. Jason