Received: by 2002:a05:6a10:c604:0:0:0:0 with SMTP id y4csp157893pxt; Wed, 4 Aug 2021 08:06:17 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwfYcFIVhfVHnmwCwNJa38mgSfmS1CzyAZQq1//Kd8YBMszmUlEThn7uWVU601cd3dopJRj X-Received: by 2002:a17:906:1416:: with SMTP id p22mr25719528ejc.364.1628089577212; Wed, 04 Aug 2021 08:06:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1628089577; cv=none; d=google.com; s=arc-20160816; b=IB8xCHmlzh75hxCAHK5TLx5mYQRqCjeaRgOMc/Bq/rVnEFAgLMH+rQTQmOgIAfKj5r MLXdiDG/PPmNWVrtea14Xc6A4ZQtNQx7FavV0+KoeAmunu5sxsRyayPogjPKjdviWtBg AXdQNjGPk1QyEy58JyMjEz6EdYcJrWqde2YsJmGfgp+Pn5FJYdaVycMeqqxUUB90dAKt 9AAFtIs5kMhGJPe708XrlP9DlUxXiCTmjs8bGjIQSjk5X45NX57YHpxIefw/A4NaPBqv gfdLGjlq5WpKREtgVp2aMMfKUdUiqv1e1/kQem5F0oGwwsCbIC5MX1T+K8UTmvGpDVgw ZnjQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=epkSZNOyyPNAWPQz+jmngcd1GrS/+wWE4RVIUtrB7vM=; b=yPJCJKDzQn0t8HI/Wqcaso9Bu8/dO+ZdioSOvCowntCctaGHYkZjxidTALrbcaBs2x VTgk7JyJMcGcr0rVv/FlwwyUw/Gc6zbwWv2dM4cXVDACjxT8HlaILeXU0aigZrYJa4Zz UmsRuwIXN4+dBgev6k+zuctOiQTTM4hgsTSF/tkmLaQoeS6KSB2gDAXofL/vDNL2R2iT lN5tdGMPa1vAZrE2E+5E7z6ldVvRNzh5wEOiSqcIcRsdzo68y84armj60HC7THThWg1Y 0q7ilAbp3Hq33TpUbEUSX77I00AbaWvQBP/+4B9GJq2YEosuFdZRfL9ABvPN3jSbFPmJ zfxg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=S4cBM6Dp; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a9si2589579eda.118.2021.08.04.08.05.51; Wed, 04 Aug 2021 08:06:17 -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=@google.com header.s=20161025 header.b=S4cBM6Dp; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238225AbhHDOnF (ORCPT + 99 others); Wed, 4 Aug 2021 10:43:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58112 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238191AbhHDOnE (ORCPT ); Wed, 4 Aug 2021 10:43:04 -0400 Received: from mail-lj1-x22d.google.com (mail-lj1-x22d.google.com [IPv6:2a00:1450:4864:20::22d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9C742C0613D5 for ; Wed, 4 Aug 2021 07:42:51 -0700 (PDT) Received: by mail-lj1-x22d.google.com with SMTP id l4so2867375ljq.4 for ; Wed, 04 Aug 2021 07:42:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=epkSZNOyyPNAWPQz+jmngcd1GrS/+wWE4RVIUtrB7vM=; b=S4cBM6DpryzJ0dCWjdd4PwjY0cPQgQET23yLihuRJ9AyMfKWHeL+VbFZt+9zkGmt0m WzzcB1kb8eF6do2K5Jir9StwEMXk7WITNQiTl4gEmrSYkI0BotdknWsfgKJj+1fJnJf2 neWVuI/A9qzqPOzZ7YT8RH8kB7+bKNwjt1bYT8Gn5zRvNl2k6TrPgzmUhhtoxTj2Oyzv X2Xj1eY0ZFkNxWN9X1RMtU4dJUREUpEmbVVU9kRJHCIAkvA5dSEVYHfoUL5bC9kM650F 0y8JejK6927mdMM2YZkEHTnnyERfsexGghvgU24BYpVhaCR1Mk6bwWvnmAqhMGOaEnho /QYg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=epkSZNOyyPNAWPQz+jmngcd1GrS/+wWE4RVIUtrB7vM=; b=Ldwxi+jqpdcVb87osteLp4wUlPSlAfqrjkq7mJY3gBIx0E3qjL1QVxbcogm0pY9NMh GVJrTXOupC0g2JoWvsX0S9iXIgqBbkfc9IzVn6mYqxfvqQ8UxIFCSDHy354qGbytrQRi H9Zx1U2ARQNlDJp8FNcljX0PIoxIgcUqOJLhJY+E9ZHUihCNhdhzUo2VyPOQx/yg6Fi8 VAlvD6lEZmPVIBcldSE7Pp9LuG5dElqo9HDL3PytXpbkJBfDME7QXx9ZJVfKQZcrJ62M wQZuvpxdMv+BAzLx/hJqsoLM57YwLuEt0gvm1LwP+E7KovslyPwMwFDq8yzTOrFNRAZM pbAA== X-Gm-Message-State: AOAM531fET8q+wYXHK86Sm0eB9uogf4tLkX56G/6LZ9ECg9PBM4E+l8t Tvl48luIt9RU3bGseNgudzPVFMWKtPZErPjTRRBTKg== X-Received: by 2002:a2e:a817:: with SMTP id l23mr18320916ljq.86.1628088169741; Wed, 04 Aug 2021 07:42:49 -0700 (PDT) MIME-Version: 1.0 References: <20210731175341.3458608-1-lrizzo@google.com> <20210803160803.GG543798@ziepe.ca> <20210803230725.ao3i2emejyyor36n@revolver> In-Reply-To: <20210803230725.ao3i2emejyyor36n@revolver> From: Jann Horn Date: Wed, 4 Aug 2021 16:42:23 +0200 Message-ID: Subject: Re: [PATCH] Add mmap_assert_locked() annotations to find_vma*() To: Liam Howlett Cc: Luigi Rizzo , Jason Gunthorpe , linux-kernel , Andrew Morton , David Rientjes , "linux-mm@kvack.org" Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Aug 4, 2021 at 1:07 AM 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? The call to expand_downwards() takes a VMA pointer as argument, and the mmap lock is the only thing that normally prevents concurrent freeing of VMA structs. Taking a lock there would be of limited utility - either the lock is not necessary because nobody else can access the MM, or the lock is insufficient because someone could have freed the VMA pointer before the lock was taken. So I think that taking a lock around the expand_downwards() call would just be obfuscating things, unless you specifically want to prevent concurrent *reads* while concurrent *writes* are impossible. Since I haven't sent a new version of my old series for almost a year, I think it'd be fine to take Luigi's patch for now, and undo it at a later point when/if we want to actually use proper locking here because we're worried about concurrent access to the MM.