From patchwork Wed Sep 30 01:20:00 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jann Horn X-Patchwork-Id: 11807519 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id C7D95618 for ; Wed, 30 Sep 2020 01:20:08 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 69B762145D for ; Wed, 30 Sep 2020 01:20:08 +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 69B762145D 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) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id BC6CB8E0001; Tue, 29 Sep 2020 21:20:04 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org 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-Original-To: linux-mm@kvack.org 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-Spam-Summary: 1,0,0,5d98aa5642edb475,d41d8cd98f00b204,jannh@google.com,,RULES_HIT:41:152:355:379:541:800:960:968:973:988:989:1260:1277:1313:1314:1345:1359:1437:1516:1518:1535:1543:1593:1594:1711:1730:1747:1777:1792:2393:2553:2559:2562:2901:2918:3138:3139:3140:3141:3142:3152:3354:3865:3866:3867:3868:3870:3871:3872:3874:4321:4605:5007:6261:6653:7901:7903:10004:10400:11026:11232:11473:11658:11914:12043:12296:12297:12438:12519:12555:12895:12986:13141:13230:14096:14097:14181:14394:14659:14721:21080:21444:21627:21740:30012:30054:30070:30090,0,RBL:209.85.218.67:@google.com:.lbl8.mailshell.net-66.100.201.100 62.18.0.100;04y8rnu36iusz5h7ki9widpr98qptocw8tmsqx85khewpj6n6wukmdsa8hjwzoy.w47di8u6n49oh5wjnh5xmuak7is8z8m6fymsxnoow45dzhdpb7qx3c4u6g7j6dg.a-lbl8.mailshell.net-223.238.255.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fp,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:24,LUA_SUMMARY:none 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 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(-) #endif /* _LINUX_MMAP_LOCK_H */ 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 }