From patchwork Wed Dec 16 04:47:30 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Morton X-Patchwork-Id: 11976585 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=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED 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 0CEE0C4361B for ; Wed, 16 Dec 2020 04:47:34 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id A2EDF23137 for ; Wed, 16 Dec 2020 04:47:33 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org A2EDF23137 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=linux-foundation.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 36D568D0025; Tue, 15 Dec 2020 23:47:33 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 2F6B98D0015; Tue, 15 Dec 2020 23:47:33 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 196D98D0025; Tue, 15 Dec 2020 23:47:33 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0180.hostedemail.com [216.40.44.180]) by kanga.kvack.org (Postfix) with ESMTP id 0056F8D0015 for ; Tue, 15 Dec 2020 23:47:32 -0500 (EST) Received: from smtpin06.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id BD35D181AEF1E for ; Wed, 16 Dec 2020 04:47:32 +0000 (UTC) X-FDA: 77597911944.06.cork60_44091d227429 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin06.hostedemail.com (Postfix) with ESMTP id 96CF8100434B6 for ; Wed, 16 Dec 2020 04:47:32 +0000 (UTC) X-HE-Tag: cork60_44091d227429 X-Filterd-Recvd-Size: 4590 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by imf46.hostedemail.com (Postfix) with ESMTP for ; Wed, 16 Dec 2020 04:47:32 +0000 (UTC) Date: Tue, 15 Dec 2020 20:47:30 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux-foundation.org; s=korg; t=1608094051; bh=wmfc2tJk5mbg+NHtzMuO5l22kdOTXMsVRafsu+QDOL0=; h=From:To:Subject:In-Reply-To:From; b=W/94qqrNRzPAmIIJvKRnN7JTbGcNbwJ507fKf2AwzKCVNcjEUCi11XwiaJM+0WZ3D CrcX9/Pd1CqTXFcuaWXrPmIGM6TwMBFnaSp1CUkvjxk0hdROqMLrRO2rALUuIZVCEd op1zjqCHa2bwo2ua7JoYiUQWFn23QNI3ury65qWU= From: Andrew Morton To: akpm@linux-foundation.org, ebiederm@xmission.com, jannh@google.com, jgg@nvidia.com, jhubbard@nvidia.com, linux-mm@kvack.org, mchehab@kernel.org, mm-commits@vger.kernel.org, sakari.ailus@linux.intel.com, torvalds@linux-foundation.org, walken@google.com Subject: [patch 94/95] mmap locking API: don't check locking if the mm isn't live yet Message-ID: <20201216044730.ADFV7TjN3%akpm@linux-foundation.org> In-Reply-To: <20201215204156.f05ec694b907845bcfab5c44@linux-foundation.org> User-Agent: s-nail v14.8.16 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: From: Jann Horn Subject: mmap locking API: don't check locking if the mm isn't live yet 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 Link: https://lkml.kernel.org/r/CAG48ez03YJG9JU_6tGiMcaVjuTyRE_o4LEQ7901b5ZoCnNAjcg@mail.gmail.com Signed-off-by: Jann Horn Cc: "Eric W . Biederman" Cc: Jason Gunthorpe Cc: John Hubbard Cc: Mauro Carvalho Chehab Cc: Michel Lespinasse Cc: Sakari Ailus Signed-off-by: Andrew Morton --- fs/exec.c | 8 ++++++++ include/linux/mm_types.h | 10 ++++++++++ include/linux/mmap_lock.h | 16 ++++++++++++---- 3 files changed, 30 insertions(+), 4 deletions(-) --- a/fs/exec.c~mmap-locking-api-dont-check-locking-if-the-mm-isnt-live-yet +++ a/fs/exec.c @@ -1001,6 +1001,14 @@ static int exec_mmap(struct mm_struct *m } } +#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); membarrier_exec_mmap(mm); --- a/include/linux/mmap_lock.h~mmap-locking-api-dont-check-locking-if-the-mm-isnt-live-yet +++ a/include/linux/mmap_lock.h @@ -161,14 +161,22 @@ static inline void mmap_read_unlock_non_ 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 } static inline int mmap_lock_is_contended(struct mm_struct *mm) --- a/include/linux/mm_types.h~mmap-locking-api-dont-check-locking-if-the-mm-isnt-live-yet +++ a/include/linux/mm_types.h @@ -561,6 +561,16 @@ struct mm_struct { #ifdef CONFIG_IOMMU_SUPPORT u32 pasid; #endif + +#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; /*