From patchwork Sat Jul 31 17:53:41 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luigi Rizzo X-Patchwork-Id: 12412605 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=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, 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 3E741C4338F for ; Sat, 31 Jul 2021 18:26:58 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 804D160F3A for ; Sat, 31 Jul 2021 18:26:57 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 804D160F3A Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id B056B6B0033; Sat, 31 Jul 2021 14:26:56 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AB61F8D0001; Sat, 31 Jul 2021 14:26:56 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9A3E46B005D; Sat, 31 Jul 2021 14:26:56 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0122.hostedemail.com [216.40.44.122]) by kanga.kvack.org (Postfix) with ESMTP id 7F9FB6B0033 for ; Sat, 31 Jul 2021 14:26:56 -0400 (EDT) Received: from smtpin17.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id DB24C180AD820 for ; Sat, 31 Jul 2021 18:26:55 +0000 (UTC) X-FDA: 78423714390.17.B6DF4DC Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf23.hostedemail.com (Postfix) with ESMTP id 06A3E90099DE for ; Sat, 31 Jul 2021 18:26:54 +0000 (UTC) Received: by mail-yb1-f202.google.com with SMTP id f3-20020a25cf030000b029055a2303fc2dso14379325ybg.11 for ; Sat, 31 Jul 2021 11:26:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:message-id:mime-version:subject:from:to:cc; bh=ivMf2Yjdch9nwF/G9GfQW4akc0oLsluxTWhSUC3fYFk=; b=vsTqgJfo8r39fd6ueado1agpNnb9dTqc/VMN8x6fR4O7y06NcFd3lmXN+8wLDk4WkH BYxhcEYJX+yNzyJgb4Eq++dHCzGGmsr1sn3ROHGVPvEdHk6ecxBRGA/wu9ByweKnzYMC +DCXItDJL/zP1x5OTLRDf8YF6PGSLicd9OLU1qllo2qFK7D3rcVEa6ITEJhfB6fkzpl0 /jVusx2lewbkBmUvt8Gto17NK3EkSNhk/FHmkLX3IRD/iqFg3yJW6g6aM86EidMAkQA1 Q3rOrFU0CceKyYCan9Ka0NrimT5mihwKaWMqXPI7uGBpW/TXAOCxHBgYLLnJSOQg4+R6 eUew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=ivMf2Yjdch9nwF/G9GfQW4akc0oLsluxTWhSUC3fYFk=; b=LO+oJkcnrGeAEI9j9Lx7SuGQUiQAp1/9MI4gMuoRqb90aW4SONHtEO/w+aPDBWwDiG G+rzt/bxyl01FHh6OlxPCM2rzxds322M6DIqSMTSinhooXIJgMddglu0BCX5mW8VHOMV sF1Do3NTIfn94yex6blQTYISJ3NE/I52MnAZAs89liqHWhfm0c5rqT3Zxkx/cAV+CV7b 1SveeKxXCGT4sOIfAmn6n907iUqcMHlK9QiBf0OMcxox8FiKvMOP/NlZBAowPZVhS+N9 OL3zd4IADzEDb+1FXtfsijx0NL/g/rX3rZGsqrwvaI/wz/uD9zgflMvC+4/NvBdbZKJJ WjzA== X-Gm-Message-State: AOAM5316Ehk865i3O6OEgssVpEDmHwM01Vi2zNHFiueARpAMh0wrPpzr uqcje5QzbTmkRMpPl3+TD8LhbUioxFo= X-Google-Smtp-Source: ABdhPJxp4EdwE4NDYsUcJ9AHAVwjadmWCd+NDw9IOLv27oyK70lPHY75R7Gly0kdMG1fohG1LG2o0lPZ96Q= X-Received: from lrizzo2.svl.corp.google.com ([2620:15c:2c4:201:af39:9f08:202e:eb75]) (user=lrizzo job=sendgmr) by 2002:a25:804d:: with SMTP id a13mr10669276ybn.431.1627754044283; Sat, 31 Jul 2021 10:54:04 -0700 (PDT) Date: Sat, 31 Jul 2021 10:53:41 -0700 Message-Id: <20210731175341.3458608-1-lrizzo@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.32.0.554.ge1b32706d8-goog Subject: [PATCH] Add mmap_assert_locked() annotations to find_vma*() From: Luigi Rizzo To: linux-kernel@vger.kernel.org, Andrew Morton , David Rientjes Cc: linux-mm@kvack.org, Luigi Rizzo X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 06A3E90099DE Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=google.com header.s=20161025 header.b=vsTqgJfo; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf23.hostedemail.com: domain of 3PI4FYQYKCH0msj00phpphmf.dpnmjovy-nnlwbdl.psh@flex--lrizzo.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3PI4FYQYKCH0msj00phpphmf.dpnmjovy-nnlwbdl.psh@flex--lrizzo.bounces.google.com X-Stat-Signature: cqozremsi8ppsfxm9osa5ecpj9zmfn1w X-HE-Tag: 1627756014-122521 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: 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 --- a/fs/exec.c +++ 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; diff --git a/mm/mmap.c b/mm/mmap.c index ca54d36d203a..79f4f8ae43ec 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -534,6 +534,7 @@ static int find_vma_links(struct mm_struct *mm, unsigned long addr, { struct rb_node **__rb_link, *__rb_parent, *rb_prev; + mmap_assert_locked(mm); __rb_link = &mm->mm_rb.rb_node; rb_prev = __rb_parent = NULL; @@ -2303,6 +2304,7 @@ struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr) struct rb_node *rb_node; struct vm_area_struct *vma; + mmap_assert_locked(mm); /* Check the cache first. */ vma = vmacache_find(mm, addr); if (likely(vma))