From patchwork Fri Aug 4 15:27:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13342036 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 320F5C001DF for ; Fri, 4 Aug 2023 15:27:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 49EBB900005; Fri, 4 Aug 2023 11:27:42 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 44E5E900004; Fri, 4 Aug 2023 11:27:42 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2EEFC900005; Fri, 4 Aug 2023 11:27:42 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 1EDDE900004 for ; Fri, 4 Aug 2023 11:27:42 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id E2FCBA13E0 for ; Fri, 4 Aug 2023 15:27:41 +0000 (UTC) X-FDA: 81086801922.18.D5D4A0C Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf11.hostedemail.com (Postfix) with ESMTP id 2C3204001D for ; Fri, 4 Aug 2023 15:27:39 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b="HmgF4Vu/"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf11.hostedemail.com: domain of 36xjNZAYKCH4uwtgpdiqqing.eqonkpwz-oomxcem.qti@flex--surenb.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=36xjNZAYKCH4uwtgpdiqqing.eqonkpwz-oomxcem.qti@flex--surenb.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1691162860; a=rsa-sha256; cv=none; b=R8d0A1j3tkUIOKIxeo9S+UVK1ndyFBRC930oMm51T0WHEIxjlXA3CBr1Xj35ZaCdqcmrQQ /ba0ENdKEHmn8l7Z9DXrvdQq9uGJRrUgHjrM1fdgiKzaR9HYGafYuZjJHLnXEGEnAZ7u97 BroIocP0dXkFgc1XkCqXPEN5VZ7CW0Q= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b="HmgF4Vu/"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf11.hostedemail.com: domain of 36xjNZAYKCH4uwtgpdiqqing.eqonkpwz-oomxcem.qti@flex--surenb.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=36xjNZAYKCH4uwtgpdiqqing.eqonkpwz-oomxcem.qti@flex--surenb.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1691162860; 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=YTdYivzT18thwmm+i4AKoDy/rcFtsAZb4kw+z52QDmA1FXU4I+v5bp9S82xMG56zK/z1Mf 62AItrvWH/NRF8Z7Alk7OsB8WSa9qeXasireN7gSk0Jw/bup0nZsALLIwTcPuLufEIVv/l sNM6WVCRBHokavvCPR5hd0tixWdvVPo= Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-d05883d850fso2183632276.3 for ; Fri, 04 Aug 2023 08:27:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1691162859; x=1691767659; 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=HmgF4Vu/z2UFpEEa5aNZa6sedWXmoJ/DZmFFNhEk4pWiLAQkZQVorYTQctKH3uDMLD mj3Hcmc2cHw9IvvhBack7cSbpm6kW8wjEkPiKsi66CbqYjjpDQLrQ5x1+cVpu8yMv2a9 XNBPj0bNqztWfJDeTb2l4iWSdIBL8kDpukfn8vzY9BFDRsihRFmI5G/xZTOwo4NUFRLU +ynAJ9lUkfrprv3GNKOEl6hufYJxWORcGXt0DMN24lgUdh8KXq9YdjamSzLozV9syQ99 NFeV7GUgWtYSH9/+ggUPmVVqbv4d2kVlEfEv2775n79aqrLFQ+3AD73ymHXIu0XeY1Mj vSlQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691162859; x=1691767659; 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=fwyWpYfLK8bPttl0Ry2qo/whfQG0iqfr43uLdEFx5EWhEXmBRuwrtwcO+IfqgyuJw3 y2rW4IpD6LC5fnVOaX1m9fPAmjXRG6vNCQT3s90QbJatZlc2vMVZ26jbFvHZrBeyUZPX Cax56wLxtCjES5+EJzy5wA8hPznW/PCAl7JcTWG99RAaKfs98SIlPBzHYet1AbZ+uy3o OJxHcBwTUzAfVQ4PLIqJXG00S0yLwqjNTsfPaZQpJD3qkrbMUweATBbTPfiwxo1wKdCD fNX86fEwZH/BXm+93P5/tZ3g9b2ZeDK8Y09FVjIxk++i2oRnqrOLql1eSC46Sv4SgKGm 4Hfw== X-Gm-Message-State: AOJu0YyfayyLOC2q19/zl/89dXKNuaN71DczWv2E5UBpoKKHY8i3HuD+ /iTIRE19zJmKrg2H96jRxQo3vRwvRE8= X-Google-Smtp-Source: AGHT+IHAOVPLc3SFAGSwKHt32pNTYMZjrIqM5CAxOwvtZsJvngxPNlrnfOqcpU9flEZvax2zIpplAXOvcxs= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:43a7:a50f:b0fd:a068]) (user=surenb job=sendgmr) by 2002:a05:6902:1614:b0:d0d:c74a:a6c0 with SMTP id bw20-20020a056902161400b00d0dc74aa6c0mr10191ybb.2.1691162859317; Fri, 04 Aug 2023 08:27:39 -0700 (PDT) Date: Fri, 4 Aug 2023 08:27:23 -0700 In-Reply-To: <20230804152724.3090321-1-surenb@google.com> Mime-Version: 1.0 References: <20230804152724.3090321-1-surenb@google.com> X-Mailer: git-send-email 2.41.0.585.gd2178a4bd4-goog Message-ID: <20230804152724.3090321-6-surenb@google.com> Subject: [PATCH v4 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, kernel-team@android.com, Suren Baghdasaryan , "Liam R . Howlett" X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 2C3204001D X-Stat-Signature: ta8dmc6tu8hiyq5tnc5h91tqwq1adtfw X-HE-Tag: 1691162859-618425 X-HE-Meta: U2FsdGVkX1+2H/jkXOsHtgyLOUeEaGtObbV9UUwFrK3d84wDYr/ciXELFPjhO39i+WMw4swLiSpFCVleobMdmWrXHUNujKFrklD6EuUk2y/8e0BT/Cz5Pw8ZvDNR14T/sQHNv342Gp/6JIt7utCWgByZ30NfZPJSfgu70QH/JZ7gPes/M4vgIVgaX7MB9TLOGRtEphdoGr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SLUt5qH+qUlY50/RozLUtqfnNWBHRbOTjEOoLdFe+EX2cUD3mcw37QK5YSPY0NClCS7PaC/Gysl3t9tXGIAp4uQThyulBxRlWUjflrqNOxQvZtOdX7X73qralm3cReZKtdFPAO6mKgWmgB2iQkwvdrk0H3DLbM4BVmlBSe+3HnG6qOOs5AwlnIMIRtE2ic2VLLVcUBo+9AVztsLYq0ottK81rQYJWe/eBTsM2mrt6MJcW3EkIOtql5sKWlf/H3rgwLt/DiUbyPHUHtmgi5VzX5buzdx6Vy9G7acD/ZY6mrCrf1lXRJ0qJelwrGKN2+FsqHVMeVkiVLHXm3AtQcrY8P1AmBNE38CzIaPr/uHbJ 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;