From patchwork Fri Aug 4 15:27:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13342037 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 80BB1C001DB for ; Fri, 4 Aug 2023 15:27:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 847E890000C; Fri, 4 Aug 2023 11:27:45 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7D2D7900004; Fri, 4 Aug 2023 11:27:45 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 671DB90000C; Fri, 4 Aug 2023 11:27:45 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 59C2E900004 for ; Fri, 4 Aug 2023 11:27:45 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 33A74814A7 for ; Fri, 4 Aug 2023 15:27:45 +0000 (UTC) X-FDA: 81086802090.16.411E27F Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf23.hostedemail.com (Postfix) with ESMTP id 19FD5140012 for ; Fri, 4 Aug 2023 15:27:42 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=Ze27H+8O; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf23.hostedemail.com: domain of 37RjNZAYKCIAwyvirfksskpi.gsqpmry1-qqozego.svk@flex--surenb.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=37RjNZAYKCIAwyvirfksskpi.gsqpmry1-qqozego.svk@flex--surenb.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1691162863; a=rsa-sha256; cv=none; b=y3x1tC6bQBicVri39bRzQThwMOT5r8P/HrPMQZozCgOw2WcTWz2AVfNWYGC1Zk8sp8XjJv B809Nq0v60V1R9PGT+xYQsl/7WrbtnOct2cfOTnC51b51I/rf2joK6pCet8u1VcNzipz8a cwrgmOvpX3wkjPzBINLCNWlu5UZiiHY= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=Ze27H+8O; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf23.hostedemail.com: domain of 37RjNZAYKCIAwyvirfksskpi.gsqpmry1-qqozego.svk@flex--surenb.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=37RjNZAYKCIAwyvirfksskpi.gsqpmry1-qqozego.svk@flex--surenb.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1691162863; 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=okHSXq0SYkri7E0to28JFQj9dGiLp+hbVT5qT1e9xdU=; b=0BcLNE1l7JDNRYr0DdJcxOsy7sG0jjRMubacQEvHJpJIXFNvqBokVSQ+asxDoYyBYmg6AH mkdn26Noew8yOXRzmcwJXD5uWtvWmg3AdKYm18fPE8y13yIr/AnzTE2hQTLv6lqVNFyvdx GYF4Z1GS8Q0lrepODAefC3CMbuNoFDk= Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-d3ae42d146cso2209575276.0 for ; Fri, 04 Aug 2023 08:27:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1691162862; x=1691767662; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=okHSXq0SYkri7E0to28JFQj9dGiLp+hbVT5qT1e9xdU=; b=Ze27H+8OOQhF7Oj+2ZOk+cY5b5gFDys4DfQgVT60v89OD7qi5UeZ/cauYLk5j1Z+GU XzmqYz49Fo48ZGd45wNkV/OWAGwTfMj8sHgA+WLxLRz9FINm38B5BG9wrka8I8py53e4 JxZXr7fPwYEz6xiF3SpxqoYBrBtxE6j/MgIItpyaYxeW+m/Vd5hX9SE2Cml3lUFOZW6r WM0e0CYffNX/JyZR7vi5OUlMXdaHPpAW0Kgep2I5V112BkSAgUR3PqlHz5NH22t6usrn YTi2zd8kbs4j6hx+FeLjGtgIsVutMy1LiPSegd4snkm0AH19LFF/qxuEmA9dhEOGf0FI 5fXA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691162862; x=1691767662; 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=okHSXq0SYkri7E0to28JFQj9dGiLp+hbVT5qT1e9xdU=; b=ge4SEwgujvTwbe/VGKZp5o1zRtN3dtTRliL3ubFjWMRRhOMSsG/Fl2XZGCdaw/2gYv VFWLx2A0RzYSDxGC8YOjzwdwlpYv0kGn1wfCC63nNO8wQzRWbh0w77ZX4V3KoLqSWrVU sfTQioTHg8lHHS8/E3ddh/zTwT45umz88TWfXsOHG9Qd5pbhy+boZPreL6tNxicvHWYk QKqPklJ9t1ZMr5WrWjrcQSLCvSX+Xwy9zFaJEo/Qtuv90JrVKwVZ01e7t4KlPTRuG20i /VKCY6/5ZBlBWqXp8ImKM96HuAAyFTQ3cPS4bmR+dGmFEhSZ+GKiO9ydNrTodJHsy9N0 E8gA== X-Gm-Message-State: AOJu0Yw6uky5WpOLY/Vtg5f77Jp/RItfK8ZuPq7zgycusXAjGsR9s3MI jlhZOD4i28iYbX3x4ZSeReScX1Rq2bQ= X-Google-Smtp-Source: AGHT+IGWFF8LV12V45AQ8PHaqPReOn9OGTO6qHNI1IHy9jYdCHxrz4hNZdLqwTqM3hhHqBDlThuUA+9rLzA= 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-20020a056902161400b00d0dc74aa6c0mr10193ybb.2.1691162861553; Fri, 04 Aug 2023 08:27:41 -0700 (PDT) Date: Fri, 4 Aug 2023 08:27:24 -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-7-surenb@google.com> Subject: [PATCH v4 6/6] mm: move vma locking out of vma_prepare and dup_anon_vma 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 , Linus Torvalds , "Liam R . Howlett" X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 19FD5140012 X-Stat-Signature: cjpkm4my3gwq4ufk6jfuz3pw46y4tc9r X-HE-Tag: 1691162862-164181 X-HE-Meta: U2FsdGVkX1/Zr3ZninQuPM+aQs+8vdm0sEdfYLHN/nWpZUB/xmO6hckVq+swa/+Ob7es2pSoUD1u3kS8fOxvcdoMbK7/BYh6pbrzZdJ/Xnv432IXxhCm4RdGX7d1YZHRuF1mQJJ311fp/Ne5rMM3MUclB7IPZEAZFvJc+aSw0GL/r7RdHx34A5HXB6a43KBb/9M+M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y2s5Ilx/+sJL+m/2zsHiZptC91bkViV0rtJi4mV3ZAWewjsaaDHJdkmyQ0B5TQqax8LAl/YY1s1LpeNwAmoIjs0jTGepD4nxsPOUTIkkOUd+w78ewAVc6V43WKRnhu4o1AAYPUWeEn6DBN8Eyl/4PKbkhEOwZSw+wRL/Rk6svBaCd7YHVrbi1YcefpHUBeoHz0QC0bgjycksVUnAzUJdzTRMT70GwKaZc0W2eIWQU4CjKg8vAAXdnBrPN9OS5KSPQtzCoJJGuecbWCgFPOzj2AakOm7TU2mMwzjARZ4nkYeG/iW3k6V+4Xn2YqMDIsIQ/fTRbtxwn79w/2ryjJs2YfDWldmJRzygCMkVEgTW/51GI/HbaTcVa2G+KNvzhZEGTly22WGMfQihPFIRkD8QWcEwKuYIt/iyuPsvLM2hIQRpIDiImALIbi0pGnq80urrYBm4= 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 harder 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. Move vma locking and replace it with an assertion inside dup_anon_vma() to further clarify the locking pattern inside vma_merge(). Suggested-by: Linus Torvalds Suggested-by: Liam R. Howlett Signed-off-by: Suren Baghdasaryan --- mm/mmap.c | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/mm/mmap.c b/mm/mmap.c index 850a39dee075..16661427d3e8 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); @@ -618,7 +608,7 @@ static inline int dup_anon_vma(struct vm_area_struct *dst, * anon pages imported. */ if (src->anon_vma && !dst->anon_vma) { - vma_start_write(dst); + vma_assert_write_locked(dst); dst->anon_vma = src->anon_vma; return anon_vma_clone(dst, src); } @@ -650,10 +640,12 @@ 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; remove_next = true; + vma_start_write(next); ret = dup_anon_vma(vma, next); if (ret) return ret; @@ -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); @@ -940,16 +934,21 @@ struct vm_area_struct *vma_merge(struct vma_iterator *vmi, struct mm_struct *mm, if (!merge_prev && !merge_next) return NULL; /* Not mergeable. */ + if (merge_prev) + vma_start_write(prev); + res = vma = prev; remove = remove2 = adjust = NULL; /* 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) @@ -957,6 +956,7 @@ struct vm_area_struct *vma_merge(struct vma_iterator *vmi, struct mm_struct *mm, } } else if (merge_prev) { /* case 2 */ if (curr) { + vma_start_write(curr); err = dup_anon_vma(prev, curr); if (end == curr->vm_end) { /* case 7 */ remove = curr; @@ -966,8 +966,10 @@ struct vm_area_struct *vma_merge(struct vma_iterator *vmi, struct mm_struct *mm, } } } else { /* merge_next */ + vma_start_write(next); res = next; if (prev && addr < prev->vm_end) { /* case 4 */ + vma_start_write(prev); vma_end = addr; adjust = next; adj_start = -(prev->vm_end - addr); @@ -983,6 +985,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); } @@ -2373,6 +2376,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 +3084,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);