From patchwork Tue Aug 1 22:07:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13337308 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 7C48BC00528 for ; Tue, 1 Aug 2023 22:07:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 16261280104; Tue, 1 Aug 2023 18:07:54 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0EA7C2800C8; Tue, 1 Aug 2023 18:07:54 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id ECEB9280104; Tue, 1 Aug 2023 18:07:53 -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 DA0152800C8 for ; Tue, 1 Aug 2023 18:07:53 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 9FE631203D7 for ; Tue, 1 Aug 2023 22:07:53 +0000 (UTC) X-FDA: 81076924026.07.7436DC5 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf14.hostedemail.com (Postfix) with ESMTP id D9D2F100016 for ; Tue, 1 Aug 2023 22:07:51 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=D1O6U13I; spf=pass (imf14.hostedemail.com: domain of 3N4LJZAYKCI4AC9w5ty66y3w.u64305CF-442Dsu2.69y@flex--surenb.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3N4LJZAYKCI4AC9w5ty66y3w.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=1690927671; 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=FgBjF0ZO5ynPdNrndPmrtDmv0LneRZKTmqqgx6E5wg8=; b=0BtRajdLlnPjmcZBJeQ8wLcfejeMEDCxvhEb8yPZ76WGVMPF6SRNyPtFEnYguJa3GyB0tb odVTMJwxtZvdJPPA5kf/UencBn4/mFEePoKE6FOYlrR6D8lleUp3vVawuM2ahw1Wrx27DN pvSzA0+H+QY8pRxRdEATM8QTxL7NCJk= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=D1O6U13I; spf=pass (imf14.hostedemail.com: domain of 3N4LJZAYKCI4AC9w5ty66y3w.u64305CF-442Dsu2.69y@flex--surenb.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3N4LJZAYKCI4AC9w5ty66y3w.u64305CF-442Dsu2.69y@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1690927671; a=rsa-sha256; cv=none; b=TUvJ7zWid2/P1i0fabXhKCus/43qNj6F4j60PrmzTHzOqquEkpZSfm7r9LLAzvqNE2xgZg 6vh22fdQAccHbl+buhrvsv/j1zu2DLCv/ybZUyDQ0OkmO2jVMpleE+9oJ5Vj4iEb+fmR/8 85PDOH3UfMjf+cTetJzuoT05LUm+lf8= Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-585fb08172bso43383617b3.2 for ; Tue, 01 Aug 2023 15:07:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1690927671; x=1691532471; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=FgBjF0ZO5ynPdNrndPmrtDmv0LneRZKTmqqgx6E5wg8=; b=D1O6U13IyQtW6sRKIgsBESsntgiHZD2NBKEqY9x4VzN/LKKhnlNfxDelhOtdMckxRn FHM+qzWy+1vIGVvjuSnC3HOPacbJwiF+FU/44s3FUSl9rg9/MlksilEUDdYILducKN5m QBspm1K1gtu0F9WcdyEIrEuwnPMiQ8okMkqevL/Euzqha1FW9VFmXK9Kd09SaJL8SIMi 4XcGgId5NKrdkKLAeWoz/iYiTpXzRmouek8VJ8B5ZERcnA/Lkgi6E3b5jwXkBi9h6gTm 7wI1MoX2O3/0bFhkSpJ1WB6U2kt7rsAJK91S/XGtkVxCLlTHZukUWI5CAAc5iREvhiOQ j9Gg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690927671; x=1691532471; 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=FgBjF0ZO5ynPdNrndPmrtDmv0LneRZKTmqqgx6E5wg8=; b=GRg6P5R2jvuUa/SmOG/h4829fRvIFlAdYs9oAn3MVQh8Vkp6ppH9p6rwpavxdqieqD o0iPuxIi74wmaonyh7WszGs7B4I3tN87O3CbOv+zk4I7n984qnjy5TDQqk1JHCrzqOmd O8Z1voV89aK9c+BXU9oY2a2BOGLHrebWrLpCJDQ5AiJuObrsMmEwQVh0oJeqpUtKgq9b kMQlY/CtpF7teSiIpqzSxUqlx9qhcLgvUJS/esRMcIjRTuJ/7P/D5Vg7Jxkt4NnXrVFX CycRZxAc7w8LYqVQmfPVRt1cshaUKe1Xo5h+GYm9L8mAsiXU9g57vtCNfP5XQwFbTYkf JD2Q== X-Gm-Message-State: ABy/qLYQfW0q8uSUYiV4TRQY8NluKnsbVMoHezgsOfhdNpHVD6peI3qs NLBlt2fSpIkNa+N9ScV5cZzzxanZAXQ= X-Google-Smtp-Source: APBJJlHKCcBKjQ9mL8CtWFWy75AVvvzqEFOjz4RA/pg75yQkmZcpThORh+6C2/yUUDZy/D/HgF30pmifT2Y= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:211c:a2ff:f17b:c5e9]) (user=surenb job=sendgmr) by 2002:a81:ae13:0:b0:583:a866:3450 with SMTP id m19-20020a81ae13000000b00583a8663450mr126859ywh.7.1690927671038; Tue, 01 Aug 2023 15:07:51 -0700 (PDT) Date: Tue, 1 Aug 2023 15:07:32 -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-7-surenb@google.com> Subject: [PATCH v2 6/6] mm: move vma locking out of vma_prepare 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 , Linus Torvalds , "Liam R . Howlett" X-Rspamd-Queue-Id: D9D2F100016 X-Rspam-User: X-Stat-Signature: b4tommzbm1h8xt6ipagy3wfmcwbnd563 X-Rspamd-Server: rspam01 X-HE-Tag: 1690927671-358220 X-HE-Meta: U2FsdGVkX19OQDbYQxHtLTJvNkoWMXFDxI63lcgk++m4FiAoZgj7+4sWYm/d+8x51Smzy+GsIZ7X5KoZrYAF7X/LQDLcOLqXVfI6Hzj7CU9cekiZxrU1Qfv2F2aUKFopWzv3mYvf1TmsH6Kcda1yIuKuHPksw5ByK+dyRveflNuv57ntSiDKsbLrWrKYQb65IU9a9LFPFBCcczkdZjgzFc6rNf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foI1wDu4EjszcEosq9UU4r3JOoa/mrkXvU2azwOWNoXAw29V7r1YczmyJWE8qssyF+J72VBJedsYQX80Tes4cwkhENZfSJf7f6qAi7FZ6R9wajmXTbakaf/7NSBzMmZK2BcrFvolaCZilS0Q4pt7D/UQFwKyCaM+FrGsuJ+B+Em4enK4wMHDrQ3K8Vwd/bdEm3hqpeSxjc1oR57XenV3p3uEncfjeuP2FxbJ823Zt3NzKF3T33bO+o0RnEGkQKANdYsIfDNR2kzZlsafhsDqoD9Q8b6LYwb4VVNAYb/i1i0UWOwrSXQkINXCpqM8g19ZoGxIiybS3DwK+Lu0+335YL+xAwdiXN8XsQ6t5Tu8PZIjc4EbnsXbo4O2yYrubyIK1cNvoGMbq6tu24FAa+hFxmvav0+rBc9SCFYiMfFBJ2z4QxCUnM2bHeiqMwA7JL5pCgow= 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: vma_prepare() is currently the central place where vmas are being locked before vma_complete() applies changes to them. While this is convenient, it also obscures vma locking and makes it hard to follow the locking rules. Move vma locking out of vma_prepare() and take vma locks explicitly at the locations where vmas are being modified. Suggested-by: Linus Torvalds Signed-off-by: Suren Baghdasaryan Reviewed-by: Liam R. Howlett --- mm/mmap.c | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/mm/mmap.c b/mm/mmap.c index 850a39dee075..e59d83cb1d7a 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -476,16 +476,6 @@ static inline void init_vma_prep(struct vma_prepare *vp, */ static inline void vma_prepare(struct vma_prepare *vp) { - vma_start_write(vp->vma); - if (vp->adj_next) - vma_start_write(vp->adj_next); - if (vp->insert) - vma_start_write(vp->insert); - if (vp->remove) - vma_start_write(vp->remove); - if (vp->remove2) - vma_start_write(vp->remove2); - if (vp->file) { uprobe_munmap(vp->vma, vp->vma->vm_start, vp->vma->vm_end); @@ -650,6 +640,7 @@ int vma_expand(struct vma_iterator *vmi, struct vm_area_struct *vma, bool remove_next = false; struct vma_prepare vp; + vma_start_write(vma); if (next && (vma != next) && (end == next->vm_end)) { int ret; @@ -657,6 +648,7 @@ int vma_expand(struct vma_iterator *vmi, struct vm_area_struct *vma, ret = dup_anon_vma(vma, next); if (ret) return ret; + vma_start_write(next); } init_multi_vma_prep(&vp, vma, NULL, remove_next ? next : NULL, NULL); @@ -708,6 +700,8 @@ int vma_shrink(struct vma_iterator *vmi, struct vm_area_struct *vma, if (vma_iter_prealloc(vmi)) return -ENOMEM; + vma_start_write(vma); + init_vma_prep(&vp, vma); vma_prepare(&vp); vma_adjust_trans_huge(vma, start, end, 0); @@ -946,10 +940,12 @@ struct vm_area_struct *vma_merge(struct vma_iterator *vmi, struct mm_struct *mm, /* Can we merge both the predecessor and the successor? */ if (merge_prev && merge_next && is_mergeable_anon_vma(prev->anon_vma, next->anon_vma, NULL)) { + vma_start_write(next); remove = next; /* case 1 */ vma_end = next->vm_end; err = dup_anon_vma(prev, next); if (curr) { /* case 6 */ + vma_start_write(curr); remove = curr; remove2 = next; if (!next->anon_vma) @@ -958,6 +954,7 @@ struct vm_area_struct *vma_merge(struct vma_iterator *vmi, struct mm_struct *mm, } else if (merge_prev) { /* case 2 */ if (curr) { err = dup_anon_vma(prev, curr); + vma_start_write(curr); if (end == curr->vm_end) { /* case 7 */ remove = curr; } else { /* case 5 */ @@ -969,6 +966,7 @@ struct vm_area_struct *vma_merge(struct vma_iterator *vmi, struct mm_struct *mm, res = next; if (prev && addr < prev->vm_end) { /* case 4 */ vma_end = addr; + vma_start_write(next); adjust = next; adj_start = -(prev->vm_end - addr); err = dup_anon_vma(next, prev); @@ -983,6 +981,7 @@ struct vm_area_struct *vma_merge(struct vma_iterator *vmi, struct mm_struct *mm, vma_pgoff = next->vm_pgoff - pglen; if (curr) { /* case 8 */ vma_pgoff = curr->vm_pgoff; + vma_start_write(curr); remove = curr; err = dup_anon_vma(next, curr); } @@ -996,6 +995,8 @@ struct vm_area_struct *vma_merge(struct vma_iterator *vmi, struct mm_struct *mm, if (vma_iter_prealloc(vmi)) return NULL; + vma_start_write(vma); + init_multi_vma_prep(&vp, vma, adjust, remove, remove2); VM_WARN_ON(vp.anon_vma && adjust && adjust->anon_vma && vp.anon_vma != adjust->anon_vma); @@ -2373,6 +2374,9 @@ int __split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma, if (new->vm_ops && new->vm_ops->open) new->vm_ops->open(new); + vma_start_write(vma); + vma_start_write(new); + init_vma_prep(&vp, vma); vp.insert = new; vma_prepare(&vp); @@ -3078,6 +3082,8 @@ static int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *vma, if (vma_iter_prealloc(vmi)) goto unacct_fail; + vma_start_write(vma); + init_vma_prep(&vp, vma); vma_prepare(&vp); vma_adjust_trans_huge(vma, vma->vm_start, addr + len, 0);