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=-20.9 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS, UNPARSEABLE_RELAY,USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham 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 ADF69C4727C for ; Wed, 30 Sep 2020 01:20:07 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 344CB2145D for ; Wed, 30 Sep 2020 01:20:07 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="FciCqs7O" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 344CB2145D 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 E70156B005D; Tue, 29 Sep 2020 21:20:04 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BC6CB8E0001; Tue, 29 Sep 2020 21:20:04 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 982526B0071; Tue, 29 Sep 2020 21:20:04 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0039.hostedemail.com [216.40.44.39]) by kanga.kvack.org (Postfix) with ESMTP id 7054F6B0068 for ; Tue, 29 Sep 2020 21:20:04 -0400 (EDT) Received: from smtpin22.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 3D01A18095F35 for ; Wed, 30 Sep 2020 01:20:04 +0000 (UTC) X-FDA: 77317971528.22.mice73_4503e732718e Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin22.hostedemail.com (Postfix) with ESMTP id 12A9D18038E67 for ; Wed, 30 Sep 2020 01:20:04 +0000 (UTC) X-HE-Tag: mice73_4503e732718e X-Filterd-Recvd-Size: 5963 Received: from mail-ej1-f67.google.com (mail-ej1-f67.google.com [209.85.218.67]) by imf05.hostedemail.com (Postfix) with ESMTP for ; Wed, 30 Sep 2020 01:20:03 +0000 (UTC) Received: by mail-ej1-f67.google.com with SMTP id i26so335359ejb.12 for ; Tue, 29 Sep 2020 18:20:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=from:in-reply-to:references:mime-version:date:message-id:subject:to :cc; bh=FJ8M+pHggei1WQdL/4TrI43/85X7WQS6HxNpsqYJ8eA=; b=FciCqs7O89TYrgwgKLvIjXoyAZkwsaZpE6Nuzeap1QJBgETGE1A3XhDed3ACffrhl9 pI/uN6ZpsSHpqckKdJkUo29P7om+6IGaEzMistNwRXKqBM4TQLElVxZsPT/YPGdSnm37 ELe8Jvzmh45qfbqhnAeWgOAGD+6ZAXUr8tynXL+2fU26DTNrrzA7kC3BBysLWmV5diB4 GASCTl2Zgh380AIVbpjd3FciicoUwmHzW+dk4UZQDc/WEW9T3Ggbj4Umwe27DOEOGT6h GNbA2xZRC/wjnx70Cl0n+8QTXm+YZUkZMFTR0S2FSA3KBqyuXTbvTEV5AZt331tdi2kx RYFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:in-reply-to:references:mime-version:date :message-id:subject:to:cc; bh=FJ8M+pHggei1WQdL/4TrI43/85X7WQS6HxNpsqYJ8eA=; b=Q1jbNMp2H3nQ7zRriVxJLaDFDOEK2NVzOw4ny3VSCfQSh2M+6HmgBCnd5u0SccJ328 wKF+yqgTuVHmEPeqW0xw57PEa+Eq/QQjoSlIWlk/HzRgBTxNB3OXmy7oNG0Z1iTsFZBF S7O4krYbUgA6ZAwpHZ/hdNa5FmxTCHlSyfDR3dBzdSOOG6xWoGs+RddoxdddRoNDCJCE Qzgne+tvHdKhjfOaZD4/8y1Ujfd8x1AeuUTmHFPYyp1ulfDhuDy64xe7iI1WTBvTMN5N 6wyGSrHdZxXvkOaDQqYZguBb3M4VA3C+waaP1bpJPZl0MqacH1QLBibwMeCLee7b7aNp yjNQ== X-Gm-Message-State: AOAM531buHDbruB2cYP8tKmI/qwcPkpoGgCHLg8PTxxYs97byym+iANW e144dD0S+tABkKcOjcdxUawSrQrpYUTs1h6IAC9DLw== X-Google-Smtp-Source: ABdhPJzeVsqTjOdTgnQ+hOnlnWn0pG7xOE1ZrHUkyx2qFe5zL92TP/PmsJUyfJx4bTRaC1pNobIQ3TeqVhuZQYimHzI= X-Received: by 2002:a17:906:c447:: with SMTP id ck7mr412804ejb.358.1601428802412; Tue, 29 Sep 2020 18:20:02 -0700 (PDT) Received: from 913411032810 named unknown by gmailapi.google.com with HTTPREST; Tue, 29 Sep 2020 18:20:00 -0700 From: Jann Horn X-Mailer: git-send-email 2.28.0.709.gb0816b6eb0-goog In-Reply-To: <20200930011944.19869-1-jannh@google.com> References: <20200930011944.19869-1-jannh@google.com> MIME-Version: 1.0 Date: Tue, 29 Sep 2020 18:20:00 -0700 Message-ID: Subject: [PATCH 3/4] mmap locking API: Don't check locking if the mm isn't live yet To: Andrew Morton , linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, "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: 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. Existing code paths that do this are (reverse callgraph): get_user_pages_remote get_arg_page copy_strings copy_string_kernel remove_arg_zero tomoyo_dump_page tomoyo_print_bprm tomoyo_scan_bprm tomoyo_environ Signed-off-by: Jann Horn --- fs/exec.c | 8 ++++++++ include/linux/mm_types.h | 9 +++++++++ include/linux/mmap_lock.h | 16 ++++++++++++---- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/fs/exec.c b/fs/exec.c index a91003e28eaa..c02b0e8e1c0b 100644 --- a/fs/exec.c +++ b/fs/exec.c @@ -1129,6 +1129,14 @@ static int exec_mmap(struct mm_struct *mm) } } +#if defined(CONFIG_LOCKDEP) || defined(CONFIG_DEBUG_VM) + /* + * From here on, the mm may be accessed concurrently, and proper locking + * is required for things like get_user_pages_remote(). + */ + mm->mmap_lock_required = 1; +#endif + task_lock(tsk); active_mm = tsk->active_mm; membarrier_exec_mmap(mm); diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h index ed028af3cb19..89fee0d0d652 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -552,6 +552,15 @@ struct mm_struct { atomic_long_t hugetlb_usage; #endif struct work_struct async_put_work; +#if defined(CONFIG_LOCKDEP) || defined(CONFIG_DEBUG_VM) + /* + * Notes whether this mm has been installed on a process yet. + * If not, only the task going through execve() can access this + * mm, and no locking is needed around get_user_pages_remote(). + * This flag is only used for debug checks. + */ + bool mmap_lock_required; +#endif } __randomize_layout; /* diff --git a/include/linux/mmap_lock.h b/include/linux/mmap_lock.h index 0707671851a8..c4fd874954d7 100644 --- a/include/linux/mmap_lock.h +++ b/include/linux/mmap_lock.h @@ -77,14 +77,22 @@ static inline void mmap_read_unlock_non_owner(struct mm_struct *mm) static inline void mmap_assert_locked(struct mm_struct *mm) { - lockdep_assert_held(&mm->mmap_lock); - VM_BUG_ON_MM(!rwsem_is_locked(&mm->mmap_lock), mm); +#if defined(CONFIG_LOCKDEP) || defined(CONFIG_DEBUG_VM) + if (mm->mmap_lock_required) { + lockdep_assert_held(&mm->mmap_lock); + VM_BUG_ON_MM(!rwsem_is_locked(&mm->mmap_lock), mm); + } +#endif } static inline void mmap_assert_write_locked(struct mm_struct *mm) { - lockdep_assert_held_write(&mm->mmap_lock); - VM_BUG_ON_MM(!rwsem_is_locked(&mm->mmap_lock), mm); +#if defined(CONFIG_LOCKDEP) || defined(CONFIG_DEBUG_VM) + if (mm->mmap_lock_required) { + lockdep_assert_held_write(&mm->mmap_lock); + VM_BUG_ON_MM(!rwsem_is_locked(&mm->mmap_lock), mm); + } +#endif } #endif /* _LINUX_MMAP_LOCK_H */ -- 2.28.0.709.gb0816b6eb0-goog