From patchwork Mon Jul 31 17:12: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: 13335319 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 98AA9C001E0 for ; Mon, 31 Jul 2023 17:12:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 374B2280087; Mon, 31 Jul 2023 13:12:52 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2FEE728007A; Mon, 31 Jul 2023 13:12:52 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F33AF280087; Mon, 31 Jul 2023 13:12:51 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id CBB1328007A for ; Mon, 31 Jul 2023 13:12:51 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 7C82E4035E for ; Mon, 31 Jul 2023 17:12:51 +0000 (UTC) X-FDA: 81072551742.12.749F07B Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf30.hostedemail.com (Postfix) with ESMTP id A79EF80016 for ; Mon, 31 Jul 2023 17:12:49 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=LgDlXg7e; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf30.hostedemail.com: domain of 3kOvHZAYKCLMlnkXgUZhhZeX.Vhfebgnq-ffdoTVd.hkZ@flex--surenb.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3kOvHZAYKCLMlnkXgUZhhZeX.Vhfebgnq-ffdoTVd.hkZ@flex--surenb.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1690823569; a=rsa-sha256; cv=none; b=vYnp83LYIRN6LkWRNKff3FJscQwZsHiiBBiFrwHNVXHtrjMRe8s8VqUBjpWYX7YZ8pNFxm HbGbw9is5H5lEj76QsT1k5H6G5zUgm0i3gYjtrUSPao7EU66u109p/3EydP1wgNRwpCj4o +vDAhbfrsyfJmgPldfgsLCIs5J3yNI0= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=LgDlXg7e; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf30.hostedemail.com: domain of 3kOvHZAYKCLMlnkXgUZhhZeX.Vhfebgnq-ffdoTVd.hkZ@flex--surenb.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3kOvHZAYKCLMlnkXgUZhhZeX.Vhfebgnq-ffdoTVd.hkZ@flex--surenb.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1690823569; 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=fl37fA/fktQ3urfS+YwWUE+qcRJpc6Gt7zobpPnOyFQ=; b=1QiTFnebHfgbX4jPUri5+vellgPhDKGBXqrgmBewJ6r9GlGyOfx2XwP6GQVNjMVBPqH/8x x7MgDu/UGMN9LZkmgoMzAUUWsCML6P484hFxsUlgR0pObuthwk10sAgaP4EryHM34VTDH4 rmDY5+ruLQGqWRrjfgxMXpuKyvmJI2w= Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-c647150c254so8699964276.1 for ; Mon, 31 Jul 2023 10:12:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1690823569; x=1691428369; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=fl37fA/fktQ3urfS+YwWUE+qcRJpc6Gt7zobpPnOyFQ=; b=LgDlXg7e+opMqwWMES1LLqVmtZkaKcma+JDWWp+e4CicAgmkjcvE9XhQBziMq2eTa3 PXWdlSTMfbarj3wXfNgyENr1RvdnAJxrQvtE9y8G7VjJ080QaAAJjzFwuwEaNmQ0DHyN a1+nQsa+bGNOM4jP3sCAuVCP01EJzsRkj0hXximLEb2Mfn0aWk9hMuhEtMt7RzQ2IfWq dTRWuwzCf9427UAvrXN1bePAw/m+zuKvf0/dCjndjuyKyBwMXDIzsawqi0xrdLmdKZLv FQU527Nzu03LfekDUrASNgZQizxrizQv19ZDdXWFCDoALbzcJ8zvPoznDlPtXbvB3a/3 h6XQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690823569; x=1691428369; 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=fl37fA/fktQ3urfS+YwWUE+qcRJpc6Gt7zobpPnOyFQ=; b=LS6sOxbDRKkW/0FDE8SCT1SvKibEgibz/M+TVYdN/QRURgmx3ePB+CrX5cf0y8Ak5H 55GHLbTctcMXRjvjQLx8Eb+ZkIUH9Tt2G6+6F+GO+KS8bAkxF1eiHp/L0My+IZVrxR6g HRWK7uu/4+9g+HVuL5eN7mtxLQOwxPXCF5yQB+CVOosNQiUORoSUFCYlcIhlEspG/q0J qkUHu4ZBuMO0rj+e69XiUJ877Gk4NI8p0ig1BoChUqLgZAEOlC7QEXoS+0zlxtNcQyLP rq7yWBjPkn5s1Trm+RNccbhvkgO42bXg0Ik2FoNuH6YeW4ubZagtC7pkrkL9eVmKgBwI nh+A== X-Gm-Message-State: ABy/qLa7MFN0wiTjsYybVgQj2KlHy3pbEAdEMxkJeVX1mMwj953f/uLl eMNPI/j3ergczAQOHXOaLnLT7TIH7L0= X-Google-Smtp-Source: APBJJlHhlXYyfYo/Ka1CDxxExi2V2guG40bRgvHWyuWMJtZm8ytlVRtn32TdlCPCKVgUKlv/bdcqKCA4SCM= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:d4d3:7e41:cb80:21ea]) (user=surenb job=sendgmr) by 2002:a25:e658:0:b0:d1c:57aa:d267 with SMTP id d85-20020a25e658000000b00d1c57aad267mr113311ybh.5.1690823568750; Mon, 31 Jul 2023 10:12:48 -0700 (PDT) Date: Mon, 31 Jul 2023 10:12:31 -0700 In-Reply-To: <20230731171233.1098105-1-surenb@google.com> Mime-Version: 1.0 References: <20230731171233.1098105-1-surenb@google.com> X-Mailer: git-send-email 2.41.0.487.g6d72f3e995-goog Message-ID: <20230731171233.1098105-6-surenb@google.com> Subject: [PATCH 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-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: A79EF80016 X-Stat-Signature: yg5xmmr14znnugttp5fd7sei3eu4btsc X-HE-Tag: 1690823569-451437 X-HE-Meta: U2FsdGVkX19LfVok5zdvJb3XOq/IT9VVXOtklYAg22h/Z4gIwxFZVMH/WNEGjVZVjtnauKGlpsY1p8LnCs6IrdO5WGKQBEqDK4Ng+e9SbKw/HSOtGcwKIUssAJRlJAYanuehN+hhjwXXSGShdru1//fASHFiXagNxHjX8grpOnXRTx1AYa9DOYA/vXMpr3IC7aXUkY4UcNgHq2NosemTcfT9Z+eiRnkr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/ljhq4sTncqJ9mUxducdgayG1kJDVBKJI9A9ZlZyu13R4b1WpMlViPP4p6uPr5wciiEV+8r5JrLpT4ZbqzLf3S4vuP894OBTf5UVVqIHkWslDQCGtaGkbND0BAWINR4Hu90SqKKy0sQmT9Tj1XWQeYibsBpEp61f+mjGgD1kFs991tXDsfxCAcHTntrUBT0gxM6jthIVKY7EyT4B5yq1FwoTqiF75nchiTTcM2JNeOFJxudE+pYHdYImxNrJ8dL4H+aWF7ZlXSBJA24CZ6su5z2aBzHvgzWYN7QxV7MNrB/vLudb7GXZ2T7ok= 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 --- 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;