From patchwork Tue Aug 1 22:07:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13337307 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 06A60C04A6A for ; Tue, 1 Aug 2023 22:07:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9C579280103; Tue, 1 Aug 2023 18:07:51 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 951B12800C8; Tue, 1 Aug 2023 18:07:51 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7A353280103; Tue, 1 Aug 2023 18:07:51 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 639982800C8 for ; Tue, 1 Aug 2023 18:07:51 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 3034BA0652 for ; Tue, 1 Aug 2023 22:07:51 +0000 (UTC) X-FDA: 81076923942.09.F20396F Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf06.hostedemail.com (Postfix) with ESMTP id 60FAD180014 for ; Tue, 1 Aug 2023 22:07:49 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=LGbOmtH0; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf06.hostedemail.com: domain of 3NILJZAYKCIs796t2qv33v0t.r310x29C-11zAprz.36v@flex--surenb.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3NILJZAYKCIs796t2qv33v0t.r310x29C-11zAprz.36v@flex--surenb.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1690927669; 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=QxBrE/UCHWlQTdGOylRc3uDNTY7RNXyaur9L23gJy4g=; b=xTJfQOJLawLCZ3oAGzAG4i9G7i5cg9E82yFFXwPlu5cunrHIUAWCkO0MUUhj3DCTUHRp8X ZMgrtrPx4pDtx4qi/tX2y3gUSQBinW9dgoJCS3KrUOWGZ4K68G5gIAbJEGsm0EpywzO1XS eibs/q7pKaCSubG2uu0GLnKjncioCYE= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=LGbOmtH0; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf06.hostedemail.com: domain of 3NILJZAYKCIs796t2qv33v0t.r310x29C-11zAprz.36v@flex--surenb.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3NILJZAYKCIs796t2qv33v0t.r310x29C-11zAprz.36v@flex--surenb.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1690927669; a=rsa-sha256; cv=none; b=nQbxM4roL8gs7rAIHTAFNDQ5RpCNTTLmhg+ML7XEh7QSUiHUH5bQUSaIoYaWu4Lta/OLio Ci+e+Z5DioGWkZ8G6SKVVMEniWThAzVKfWU99fxI0gme+OL10RpSqiXyaBt7GvuKjl/pBs 4F2COyFsrfF+mu7eBA5ShWnE9FjnFhg= Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-5840ea40c59so75696457b3.2 for ; Tue, 01 Aug 2023 15:07:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1690927668; x=1691532468; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=QxBrE/UCHWlQTdGOylRc3uDNTY7RNXyaur9L23gJy4g=; b=LGbOmtH0lMhdDYO3+2ECExEYuxVvpb5dUlLecAiJAGrPI9PAJfTN7UrlhG2D2+sMgI A0HPsE7Dh7snipQv/ppKfZkrtywqFIDj94GgYYg1RmHEKQD/JrUFvPL4mXaNgmgR43fS rzNN/7cPcSEEUD6MphOEKlUIyUYRI9RNEW1ibPgtDMhMmPz+MaEiw+C/wPcvGj3gdm+s wmBOfyKZ9Eq/c9nMUYmgRF+ijS83GqRJwTrxNUo/kn28ipzSQ7VAbYmtkG9fCdYkXi0z Hhzh6iMenp89gYpIovNtlhgbKlD0/7Qoap5BixiG9npmNeurITeJhKlGDSUEA6RAa5bC 2mng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690927668; x=1691532468; 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=QxBrE/UCHWlQTdGOylRc3uDNTY7RNXyaur9L23gJy4g=; b=E3hQVqx+s7KpBeGn3xXQCeJ+q8xs0UnvPUuU/BdnWVQ0t7JQWuXMn2szzbXTfLd/qi hTdL52lAxhKzaaVmaiTSCYosoAQIGFi0kN5RhXFMOBlT7XbASReFcRWrIhJ7vbbPo3rq fDkAb0tYSpSxE68bqIjCPcGg00m2c5GHoEVBdE8OyftY83ICfrB5DSoXeIBuTbeInSS0 P0DFh5jLiU6Zby2wudaFeTsd6UplHDTAow6DM62m66sF1fCF8X0DCXo3fU68r1ta0G4Y +iYMlWfpmkAxEfZICueWMTVNEGFee4Exo6EIi209VUf8qa1iat1OS6ThiXGgcQMBpd21 NjXw== X-Gm-Message-State: ABy/qLb5on2Lv5WE5HPNm21SW6ODOGhju6al7CTPmNjFUgUv3eS10tuL c7NOoIPvLrWNTFV2qQwxS9r1IGdvwU8= X-Google-Smtp-Source: APBJJlFGce4m/S34cz0bw8Xemucsi7OipxmEL+9L+engD7fhL2P1Th2X8JjKAj9JtV2822hEHcXYgtjOeKk= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:211c:a2ff:f17b:c5e9]) (user=surenb job=sendgmr) by 2002:a81:ae02:0:b0:576:b244:5a4e with SMTP id m2-20020a81ae02000000b00576b2445a4emr129366ywh.10.1690927668528; Tue, 01 Aug 2023 15:07:48 -0700 (PDT) Date: Tue, 1 Aug 2023 15:07:31 -0700 In-Reply-To: <20230801220733.1987762-1-surenb@google.com> Mime-Version: 1.0 References: <20230801220733.1987762-1-surenb@google.com> X-Mailer: git-send-email 2.41.0.585.gd2178a4bd4-goog Message-ID: <20230801220733.1987762-6-surenb@google.com> Subject: [PATCH v2 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 X-Rspamd-Queue-Id: 60FAD180014 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: ngh71s4hbxr1demm915rubatz3kgrcfq X-HE-Tag: 1690927669-459901 X-HE-Meta: U2FsdGVkX18GNxR5yVWz97guuwRG3tEoWnDHT5gOXcW0oaWlUo9wX9Wcznkvk3fBLLzsyXRGUZRBCuWzTzzrtNZ6D+SUTBZ1iYPvC3Ltl2hYfTjfHFM9xzHgvOdBv/bqXNBwYl91EVAAOiukPFQXw6MAPSEF9awgpA26eCIlexMLEZpBDJZCnVHXGAHIfE0bbjvsvlt+0ZN23YbAK+MH93gwHGhdmO9IpT2kT4isGrj3VrMqJVAspM3uVm4BjF6SzqFwhqV4mop1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HA6+wZQ2KwLfEm1gTeWngqnnpwI7cWae87eJ9Ihr33niSzGTqsZ9GApuV015EElg3palXLBH7ickieT7eFbq1ymEMII/up/YbuZ/EfeJWEWUB3BvaLCdeKqPqNxMs7cTUi5YXHI4mCNeoJ3GiyHEJb8bGfCNrSUONEGWzPjKjYH39TG/asGfCnELmDPLL0H+IVfiluQFQl4pwfQVSZqSNuqGtgw8fjjqfACEszX4TbB/eFAKzjd4p0KfDyahYJjhuvIorH3aeNQlEkvScG7Vv7E4= 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;