From mboxrd@z Thu Jan 1 00:00:00 1970 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C783AC001E0 for ; Mon, 31 Jul 2023 17:15:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233945AbjGaRPp (ORCPT ); Mon, 31 Jul 2023 13:15:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41330 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232733AbjGaRPQ (ORCPT ); Mon, 31 Jul 2023 13:15:16 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8F18E26A5 for ; Mon, 31 Jul 2023 10:12:49 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-c926075a50cso10815253276.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=ekpifvWt2w/jyMSwqINstliJDAWIt63cyrUT+EfrbB+675ywxtmCs2fR9WPkbGQUS7 Q2CGZBP+PZGJbiwhiv3JXSFIjGQvWTN6Oms5+b40+8ltSOG48PweugXYL4Smb3m22vLu byFgHaEQdErx1SuVOhcwOzC5wQ08WC/AwMk4GuJZ0O286LI0OFCnD0VISzXZu1ZNLqzh pRgecegvcK8ZF1FwqkIrq38K3gT5yAg3QGDiQM3hwGLhkpTE3+y124vKzNwrDVDrpBKy vhA6zxFCMyAR9qCJ03GAqJf017Bt7gzEWg5o7piwsPV5wTDRgq35Pthk91x+tKhNnQuI TodQ== X-Gm-Message-State: ABy/qLaVIm3bUkcz+fPwFpg/nMKbAT6lWqPu2d4+tOjWsYKbBr8XqrCm 6aCs5qQ1y0LJ3cv7A0WySfAIxOs/jj8= 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 Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org 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; -- 2.41.0.487.g6d72f3e995-goog