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 X-Spam-Level: X-Spam-Status: No, score=-12.1 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_IN_DEF_DKIM_WL autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 59E2AC4741F for ; Wed, 30 Sep 2020 20:15:27 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0C7B420739 for ; Wed, 30 Sep 2020 20:15:26 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="L4wb/BfM" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728031AbgI3UP0 (ORCPT ); Wed, 30 Sep 2020 16:15:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58374 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725355AbgI3UPZ (ORCPT ); Wed, 30 Sep 2020 16:15:25 -0400 Received: from mail-ej1-x642.google.com (mail-ej1-x642.google.com [IPv6:2a00:1450:4864:20::642]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 64474C061755 for ; Wed, 30 Sep 2020 13:15:25 -0700 (PDT) Received: by mail-ej1-x642.google.com with SMTP id qp15so3777161ejb.3 for ; Wed, 30 Sep 2020 13:15:25 -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=3sPuIrm/H+6tsFTaFl1P+i+ksxlkVa8mKtAEYrx+o10=; b=L4wb/BfMnZfM59FoYSsrhnYFcWBnMSB7P03EwiydngMaA8RgPXyoP0JWUwlef486pY To9zt++muYCma88hlI3FOgRBHQKL9CjK/1aGelay7Vl9uCQhVeg5byiKXrwSRyQJGb9x F43pf5V/ZAS0vVVmxyBP9l9M1sIozAIA3/PJ5PqkRsW8rhJaVOmyGi9McLQDi8kf8l89 yR7tWr/YgWH7VTtV2W+Hur56sTq8j5p2Vl3vHm1UnikeVFptnc5vJYrsJI9hTthV9CKy U1+5DiWI2OYYWmb/tyFOp+J/VD9NK+xfOLPx/0N6sorU4kqAql9dXbqX4WzEWAd4ekih cc4A== 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=3sPuIrm/H+6tsFTaFl1P+i+ksxlkVa8mKtAEYrx+o10=; b=piPCTdyNJz0xcfKhAd5ofveJASSX9JMCTi8FzIxo2x1CY6uKIa/L6vabXTLDWEBTS9 EhB7pmEhEytnTYqm7YgRuDBEXq/t3KHlAg93+nWs5XCPU7zem1YicngME04nHyBuim0G apGn855e+tQ7pE6khfpxNZSngAse/ot+ywdxwatkTP8km8ZyvkvqYLLgcAc0SepJQlQR 53gx6Qji5/4yghwwoyVYUEqMijzcwhA59ApHOI+maOgm+bt7DFIqHL6PM3GbU07urwbv B9rm5uBSNBs6j/mvZ6HiPZaCBIGMtGeNMniX5dAZLuuiC7jm/qAHHErnhCpwHe0U8tMs IENQ== X-Gm-Message-State: AOAM530hTFSP0EhSLCsnq2J8YZKUBsVVxu64kAxwFnzaY4s3lKTosbhn IriRiwxDzQa4fa5T1c3f/UiZLIpMnrtkpva0GpRR8A== X-Google-Smtp-Source: ABdhPJw5Nn4AQT8htAqMK3nUr3VFWxN3Qkl4eMUl7dH3m+0DYnTprr2Z2/ryFq1vHTExiXLd4l0psdD2JFaOlcQ5Hok= X-Received: by 2002:a17:906:980f:: with SMTP id lm15mr4759153ejb.184.1601496923121; Wed, 30 Sep 2020 13:15:23 -0700 (PDT) MIME-Version: 1.0 References: <20200930011944.19869-1-jannh@google.com> <20200930123000.GC9916@ziepe.ca> In-Reply-To: From: Jann Horn Date: Wed, 30 Sep 2020 22:14:57 +0200 Message-ID: Subject: Re: [PATCH 3/4] mmap locking API: Don't check locking if the mm isn't live yet To: Jason Gunthorpe Cc: Andrew Morton , Linux-MM , kernel list , "Eric W . Biederman" , Michel Lespinasse , Mauro Carvalho Chehab , Sakari Ailus Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Sep 30, 2020 at 2:50 PM Jann Horn wrote: > On Wed, Sep 30, 2020 at 2:30 PM Jason Gunthorpe wrote: > > On Tue, Sep 29, 2020 at 06:20:00PM -0700, Jann Horn wrote: > > > In preparation for adding a mmap_assert_locked() check in > > > __get_user_pages(), teach the mmap_assert_*locked() helpers that it's fine > > > to operate on an mm without locking in the middle of execve() as long as > > > it hasn't been installed on a process yet. > > > > I'm happy to see lockdep being added here, but can you elaborate on > > why add this mmap_locked_required instead of obtaining the lock in the > > execv path? > > My thinking was: At that point, we're logically still in the > single-owner initialization phase of the mm_struct. Almost any object > has initialization and teardown steps that occur in a context where > the object only has a single owner, and therefore no locking is > required. It seems to me that adding locking in places like > get_arg_page() would be confusing because it would suggest the > existence of concurrency where there is no actual concurrency, and it > might be annoying in terms of lockdep if someone tries to use > something like get_arg_page() while holding the mmap_sem of the > calling process. It would also mean that we'd be doing extra locking > in normal kernel builds that isn't actually logically required. > > Hmm, on the other hand, dup_mmap() already locks the child mm (with > mmap_write_lock_nested()), so I guess it wouldn't be too bad to also > do it in get_arg_page() and tomoyo_dump_page(), with comments that > note that we're doing this for lockdep consistency... I guess I can go > change this in v2. Actually, I'm taking that back. There's an extra problem: get_arg_page() accesses bprm->vma, which is set all the way back in __bprm_mm_init(). We really shouldn't be pretending that we're properly taking the mmap_sem when actually, we keep reusing a vm_area_struct pointer. So for that reason I prefer the approach in the existing patch, where we make it clear that mm_struct has two different lifetime phases in which GUP works, and that those lifetime phases have very different locking requirements. Does that sound reasonable? 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 X-Spam-Level: X-Spam-Status: No, score=-12.1 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id F2543C4727C for ; Wed, 30 Sep 2020 20:15:26 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 5C1D820739 for ; Wed, 30 Sep 2020 20:15:26 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="L4wb/BfM" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 5C1D820739 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id AADB96B005C; Wed, 30 Sep 2020 16:15:25 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A5EDA6B0062; Wed, 30 Sep 2020 16:15:25 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 94CC86B0068; Wed, 30 Sep 2020 16:15:25 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0204.hostedemail.com [216.40.44.204]) by kanga.kvack.org (Postfix) with ESMTP id 836FE6B005C for ; Wed, 30 Sep 2020 16:15:25 -0400 (EDT) Received: from smtpin14.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 3E4D51E11 for ; Wed, 30 Sep 2020 20:15:25 +0000 (UTC) X-FDA: 77320832610.14.blood26_5311a7027195 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin14.hostedemail.com (Postfix) with ESMTP id 161A91822987A for ; Wed, 30 Sep 2020 20:15:25 +0000 (UTC) X-HE-Tag: blood26_5311a7027195 X-Filterd-Recvd-Size: 5266 Received: from mail-ej1-f67.google.com (mail-ej1-f67.google.com [209.85.218.67]) by imf29.hostedemail.com (Postfix) with ESMTP for ; Wed, 30 Sep 2020 20:15:24 +0000 (UTC) Received: by mail-ej1-f67.google.com with SMTP id p15so4676722ejm.7 for ; Wed, 30 Sep 2020 13:15:24 -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=3sPuIrm/H+6tsFTaFl1P+i+ksxlkVa8mKtAEYrx+o10=; b=L4wb/BfMnZfM59FoYSsrhnYFcWBnMSB7P03EwiydngMaA8RgPXyoP0JWUwlef486pY To9zt++muYCma88hlI3FOgRBHQKL9CjK/1aGelay7Vl9uCQhVeg5byiKXrwSRyQJGb9x F43pf5V/ZAS0vVVmxyBP9l9M1sIozAIA3/PJ5PqkRsW8rhJaVOmyGi9McLQDi8kf8l89 yR7tWr/YgWH7VTtV2W+Hur56sTq8j5p2Vl3vHm1UnikeVFptnc5vJYrsJI9hTthV9CKy U1+5DiWI2OYYWmb/tyFOp+J/VD9NK+xfOLPx/0N6sorU4kqAql9dXbqX4WzEWAd4ekih cc4A== 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=3sPuIrm/H+6tsFTaFl1P+i+ksxlkVa8mKtAEYrx+o10=; b=ARNqGJuuw4qRfbjWLL5zCVvkIHDMULvHq58mhgJhgWTpiwyTf0SaGe7TFszW256GY/ gKS2uSElB+Gqsw4ER6sZ62P32Vf6TQJIGOf/HhgrRtOMUN+LpVz/pPgwl7wzbQ8fdlbb huKtlw8K6B0aHEs6EpBHI/yLFr+BMrOhGmyDzsjeJEE37RWWA5+cfswQwWL4/47iJySJ vQG+Md7KpnB5FKWAdKJaluilPEWAtlvkxBaippI7FBqaFEf2G3J2qYtI6MC0sbWQ45BU LWNU30ZLD6QPB7JfZFVm6ewurmscCSzI5uUO+SDBANkzGeut9b3RoPlBV7XgZgXfoUou QGZQ== X-Gm-Message-State: AOAM530f49hYVl07QzBSS1C1tS2jOR8J2m0Wf53d8hLi8tn65vTNh5Lt hmRSocXEf40nZmvlsR7ahARaUb9XqNb+HLRYS6OAIA== X-Google-Smtp-Source: ABdhPJw5Nn4AQT8htAqMK3nUr3VFWxN3Qkl4eMUl7dH3m+0DYnTprr2Z2/ryFq1vHTExiXLd4l0psdD2JFaOlcQ5Hok= X-Received: by 2002:a17:906:980f:: with SMTP id lm15mr4759153ejb.184.1601496923121; Wed, 30 Sep 2020 13:15:23 -0700 (PDT) MIME-Version: 1.0 References: <20200930011944.19869-1-jannh@google.com> <20200930123000.GC9916@ziepe.ca> In-Reply-To: From: Jann Horn Date: Wed, 30 Sep 2020 22:14:57 +0200 Message-ID: Subject: Re: [PATCH 3/4] mmap locking API: Don't check locking if the mm isn't live yet To: Jason Gunthorpe Cc: Andrew Morton , Linux-MM , kernel list , "Eric W . Biederman" , Michel Lespinasse , Mauro Carvalho Chehab , Sakari Ailus Content-Type: text/plain; charset="UTF-8" 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: On Wed, Sep 30, 2020 at 2:50 PM Jann Horn wrote: > On Wed, Sep 30, 2020 at 2:30 PM Jason Gunthorpe wrote: > > On Tue, Sep 29, 2020 at 06:20:00PM -0700, Jann Horn wrote: > > > In preparation for adding a mmap_assert_locked() check in > > > __get_user_pages(), teach the mmap_assert_*locked() helpers that it's fine > > > to operate on an mm without locking in the middle of execve() as long as > > > it hasn't been installed on a process yet. > > > > I'm happy to see lockdep being added here, but can you elaborate on > > why add this mmap_locked_required instead of obtaining the lock in the > > execv path? > > My thinking was: At that point, we're logically still in the > single-owner initialization phase of the mm_struct. Almost any object > has initialization and teardown steps that occur in a context where > the object only has a single owner, and therefore no locking is > required. It seems to me that adding locking in places like > get_arg_page() would be confusing because it would suggest the > existence of concurrency where there is no actual concurrency, and it > might be annoying in terms of lockdep if someone tries to use > something like get_arg_page() while holding the mmap_sem of the > calling process. It would also mean that we'd be doing extra locking > in normal kernel builds that isn't actually logically required. > > Hmm, on the other hand, dup_mmap() already locks the child mm (with > mmap_write_lock_nested()), so I guess it wouldn't be too bad to also > do it in get_arg_page() and tomoyo_dump_page(), with comments that > note that we're doing this for lockdep consistency... I guess I can go > change this in v2. Actually, I'm taking that back. There's an extra problem: get_arg_page() accesses bprm->vma, which is set all the way back in __bprm_mm_init(). We really shouldn't be pretending that we're properly taking the mmap_sem when actually, we keep reusing a vm_area_struct pointer. So for that reason I prefer the approach in the existing patch, where we make it clear that mm_struct has two different lifetime phases in which GUP works, and that those lifetime phases have very different locking requirements. Does that sound reasonable?