From patchwork Thu Aug 3 17:26:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13340372 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9F560C001DF for ; Thu, 3 Aug 2023 17:27:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 379B1280284; Thu, 3 Aug 2023 13:27:10 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 32A9F28022C; Thu, 3 Aug 2023 13:27:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1A697280284; Thu, 3 Aug 2023 13:27:10 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 0A82128022C for ; Thu, 3 Aug 2023 13:27:10 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id AA090B2D58 for ; Thu, 3 Aug 2023 17:27:09 +0000 (UTC) X-FDA: 81083474178.22.4116189 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf18.hostedemail.com (Postfix) with ESMTP id D811A1C0010 for ; Thu, 3 Aug 2023 17:27:07 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b="iMlvdhm/"; spf=pass (imf18.hostedemail.com: domain of 3a-PLZAYKCI4AC9w5ty66y3w.u64305CF-442Dsu2.69y@flex--surenb.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3a-PLZAYKCI4AC9w5ty66y3w.u64305CF-442Dsu2.69y@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1691083627; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=GZEu7FQxJMwmMQ2ZbaGaDze4HsujzOay4eQMS+qpos8=; b=pbpC0W4mzjLbq6eD0c6Qv79gieoA86Qbkn3h62W+Nuo5h77hTQXcS1MN7JaR+nxXY5pA5y u9y9MfrUi1CpF1nsd6YDyp1XS+wv0hn0PIuG0i5D1nqbry/192x3Bn9av2dEaoPJZxiKhS mr27ZJPE72ve9r46XOFbm/JEmwm5aOk= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1691083627; a=rsa-sha256; cv=none; b=m5DeCXHwiEhMta6O/Uiyzih9IUiEEO6ffPEW8FtQXaCVOkcEfUNwEVkOCy61xVeHTE71IY Syt4e5iaaSaaNfWxfIbFrRDXDVlv2Px3BGxTE5XDPusjmNNqW/hRJ7nA+t5u+RqYbOQVo7 QyaxuzKdp8wfambjTCc/iHnyrPlxlVY= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b="iMlvdhm/"; spf=pass (imf18.hostedemail.com: domain of 3a-PLZAYKCI4AC9w5ty66y3w.u64305CF-442Dsu2.69y@flex--surenb.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3a-PLZAYKCI4AC9w5ty66y3w.u64305CF-442Dsu2.69y@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-d06d36b49f9so1381463276.1 for ; Thu, 03 Aug 2023 10:27:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1691083627; x=1691688427; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=GZEu7FQxJMwmMQ2ZbaGaDze4HsujzOay4eQMS+qpos8=; b=iMlvdhm/bxVV7kv0V79kx1hxAyb+1GfcPnJNgAo6LGdR0bN5ykKWLzgwRkr0IsKULR 2o34cI0Yf3y9b3S2Dr1CwRyv6f01fPNaOoXOCMRAQbhpK1DPGFgCjTrhlWjoMsqAvyL0 8VUIztQRc1OQyCbz5DagmCU/mu3dLq6uVQlfq3eZNISYdHHkvNHZJzuMW6YuKo90+kyC hjZ+CUef9CzPm33relwkpyOY3ShX8wwK6g3JLpiI+W0wl5DrWoPM2vnAEuwmowfvDdjR SwLvk9fJF/d9WaXsmtAEQnK/vJCJQgoZOjJbm+glRWLSwwbs7FTj9oR+NIkDHwsI3k9/ MBMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691083627; x=1691688427; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=GZEu7FQxJMwmMQ2ZbaGaDze4HsujzOay4eQMS+qpos8=; b=YRv+jRABkRBEg7FWheIHuZW0SpyH/0w4ELFyzdicz/OTbpePIIDRT1G00sAvOZaI+L PLeigqfcDJWUeGIy335xAY6QuLy/8F5j6zT+j3IQC599ZwP5mUL5TxuioiIB4/2aA6lA AwEiXJ923R/q2aJfFOqy6/uDaOH+hahRH++CLLNOaoTyUaRKJmRY+LzQE2CSfwPEk4R+ 4M9Qpi+YoZYU0cys8bki1Ytnj5nNtepd2rK0SqXPPeuROfNHhe4dvKFsmSzANb4ndCwf VVjX6GhDBhoStxiKW4PWWmo9Z2bopyxgl3n2VViMzUbVW8hwI4mX8mclLOixeV0XcS+z QzuA== X-Gm-Message-State: ABy/qLaZf5G2MRrLzRJANRWERSHdeGG/8Q1pOgS8/SDWzOq2KMKjALx1 qZmwyvNxFWhxDWap9dOUohcu6UQDXQw= X-Google-Smtp-Source: APBJJlEm+TAFgwKeycGXNkE9sZYz1YS9giHD/D1paY/qC/rrcXYAkNTs0f2pZWBHAJYKXSdQl1MdNfKRQzw= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:3dec:ef9e:7bf4:7a6]) (user=surenb job=sendgmr) by 2002:a25:19c2:0:b0:d0d:1563:58f2 with SMTP id 185-20020a2519c2000000b00d0d156358f2mr129793ybz.2.1691083627024; Thu, 03 Aug 2023 10:27:07 -0700 (PDT) Date: Thu, 3 Aug 2023 10:26:50 -0700 In-Reply-To: <20230803172652.2849981-1-surenb@google.com> Mime-Version: 1.0 References: <20230803172652.2849981-1-surenb@google.com> X-Mailer: git-send-email 2.41.0.585.gd2178a4bd4-goog Message-ID: <20230803172652.2849981-6-surenb@google.com> Subject: [PATCH v3 5/6] mm: always lock new vma before inserting into vma tree From: Suren Baghdasaryan To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, jannh@google.com, willy@infradead.org, liam.howlett@oracle.com, david@redhat.com, peterx@redhat.com, ldufour@linux.ibm.com, vbabka@suse.cz, michel@lespinasse.org, jglisse@google.com, mhocko@suse.com, hannes@cmpxchg.org, dave@stgolabs.net, hughd@google.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, stable@vger.kernel.org, Suren Baghdasaryan , "Liam R . Howlett" X-Rspamd-Queue-Id: D811A1C0010 X-Rspam-User: X-Stat-Signature: h46z795kw6xb4qq4peeztdfgsc89k5hz X-Rspamd-Server: rspam03 X-HE-Tag: 1691083627-840619 X-HE-Meta: U2FsdGVkX18+5dAAqtPC0yROTifHxlWqicM+Zz1SJE0/LERN+JcsFjAprBGbMZZx2/rNPwir1APn67cWcs2Vzw+VWKwFMSpgtgCH7d0w7X7VN9oCCt/woHqsne8C9VktPjFXChzKwJl1ScCP5+WdhUl1CeDubwnzzRaM7EZo8ji9uoWmGmV/slBC+5FZLfZ2cAMVkIniUdTOkH4rLf1X5fs2frLMLEC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7vVwbFmHH9qbrb7YxxIxxlPTpUJERWxSHEhLXO1uoxNy34puX/Mzeh6JXGFjVSRLC/VZCUX5qBT6wlCxJt+DFMCVH5oBeOb3Z9FC/Epa8BwVrvlxgkgAhtHjLRbHGOuFlprJli/aGubLmjmBhWxulKVj7/u7bdIu85AxJA7EVatuT0Tf9jik5UwKbnUWacBxVFNpwpIh6xegKw8ux8yPy7F/BDDF7yuXysFOhVP5QaBVx01vqh+DhgROWt+Fx4rbukFtjHFYJb/1WxGNFk+ZgJAg9OnM6KuNTpRNcXoZd3/dAXts7nxWJ5Hn/GGIXP+gRf0Akn12/PFKpj0MUUl 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: While it's not strictly necessary to lock a newly created vma before adding it into the vma tree (as long as no further changes are performed to it), it seems like a good policy to lock it and prevent accidental changes after it becomes visible to the page faults. Lock the vma before adding it into the vma tree. Suggested-by: Jann Horn Signed-off-by: Suren Baghdasaryan Reviewed-by: Liam R. Howlett --- mm/mmap.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/mm/mmap.c b/mm/mmap.c index 3937479d0e07..850a39dee075 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -412,6 +412,8 @@ static int vma_link(struct mm_struct *mm, struct vm_area_struct *vma) if (vma_iter_prealloc(&vmi)) return -ENOMEM; + vma_start_write(vma); + if (vma->vm_file) { mapping = vma->vm_file->f_mapping; i_mmap_lock_write(mapping); @@ -477,7 +479,8 @@ static inline void vma_prepare(struct vma_prepare *vp) vma_start_write(vp->vma); if (vp->adj_next) vma_start_write(vp->adj_next); - /* vp->insert is always a newly created VMA, no need for locking */ + if (vp->insert) + vma_start_write(vp->insert); if (vp->remove) vma_start_write(vp->remove); if (vp->remove2) @@ -3098,6 +3101,7 @@ static int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *vma, vma->vm_pgoff = addr >> PAGE_SHIFT; vm_flags_init(vma, flags); vma->vm_page_prot = vm_get_page_prot(flags); + vma_start_write(vma); if (vma_iter_store_gfp(vmi, vma, GFP_KERNEL)) goto mas_store_fail; @@ -3345,7 +3349,6 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap, get_file(new_vma->vm_file); if (new_vma->vm_ops && new_vma->vm_ops->open) new_vma->vm_ops->open(new_vma); - vma_start_write(new_vma); if (vma_link(mm, new_vma)) goto out_vma_link; *need_rmap_locks = false;