From patchwork Thu Aug 3 17:26:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13340373 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 26016EB64DD for ; Thu, 3 Aug 2023 17:27:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B42DD280285; Thu, 3 Aug 2023 13:27:12 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AFE9E28022C; Thu, 3 Aug 2023 13:27:12 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 96BD6280285; Thu, 3 Aug 2023 13:27:12 -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 803F728022C for ; Thu, 3 Aug 2023 13:27:12 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 2B0301A03FF for ; Thu, 3 Aug 2023 17:27:12 +0000 (UTC) X-FDA: 81083474304.22.E6CB0AC Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf05.hostedemail.com (Postfix) with ESMTP id 42D2F10001F for ; Thu, 3 Aug 2023 17:27:10 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=zMcctUOh; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf05.hostedemail.com: domain of 3bePLZAYKCJACEBy7v08805y.w86527EH-664Fuw4.8B0@flex--surenb.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3bePLZAYKCJACEBy7v08805y.w86527EH-664Fuw4.8B0@flex--surenb.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1691083630; 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=enNugCeOmhfazHyzEjQZvfU7Wp5uGQcTSmtYdGh09Fo=; b=5IUjcS8VGIIIOFUOknCei1XCxyw631pHr2JvhBEsabqfxmy3L33nPcYQd+eqIuFQ5eXFO1 mrtwXejTMA2rEdxBtH6ATYwFp3oX3CssOF7i6itDJpHyx/edjpRvS9/8kXZNYEcaWjaf2n 74hf79YeDP0NW+1FebEJwro+CyDmzJ8= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=zMcctUOh; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf05.hostedemail.com: domain of 3bePLZAYKCJACEBy7v08805y.w86527EH-664Fuw4.8B0@flex--surenb.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3bePLZAYKCJACEBy7v08805y.w86527EH-664Fuw4.8B0@flex--surenb.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1691083630; a=rsa-sha256; cv=none; b=V1Op1ok0urecGYnuWfNhuFxbxufYuaPxL3wcZefKUDXMiR/5djciSR49bJetL7XEKQ2b+x eoAJll0hbtdniyJIVnTXziiixV1bal/TQ2kQ+dlthEeDSpgww9tIRadTAIN42y2f9kLsBC heBIBambLDFm701fI94r86FayqYewhA= Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-585f254c41aso13568427b3.1 for ; Thu, 03 Aug 2023 10:27:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1691083629; x=1691688429; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=enNugCeOmhfazHyzEjQZvfU7Wp5uGQcTSmtYdGh09Fo=; b=zMcctUOh7tYayLvckt9uz/YiCskGpP+YntI3h+0uVE/02dHvg03tF8YSHbRMyNGfZB vRYvs+C9OZgyahs0ZA60vqFKBKDcsU68jI9QF2xZbnvK1zTGlMYfnt4cUcYYUYYQEZgm QcZToqsbu8sycVp7w2dB3p8Tho375Ac9GC/kb1P9fL5XmVLzXVE4tmVCtIfbtq++EekQ sXwQggrredRKZtR876ti7Xpr8W/4VwmfvILxh9XXU6UIOZhAVePq4SmwRGAaJR1zzKb1 NYBjYG4UWWlZvCyBazMus+wGvZXN0rUGhkIT38Std3TmC/giO7p092GnBqUklLiC2uAW CiUA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691083629; x=1691688429; 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=enNugCeOmhfazHyzEjQZvfU7Wp5uGQcTSmtYdGh09Fo=; b=NMBBnBykvyqgnByQSWtbDKn8F3lKbzdwUpy4iHkDemnGa0TdW1gR1bgwrzhViQaHTL 5H7LHcxfC+dfqSLwh7Z87K9+7AsVFQtuGjOdvII5O7LR4Hs5ukmc6M2G1wHaXi94am/j nslC0h+ghRF8+N3Wv17bczBJYnJx3/8xlIqfNkMDAANgEzWFrj6Kok6BfPxgXZteQcrS OI6ScZd/SmBEwYy9FoSCCUml0YiTMZPWQgbskOpHV16WH/7DxszWGQTg2+qTMkStYT1R iW0RV1fDLUKRrEPu73nFcbgyJzZ8Wpdks2amBdPVPcva8FzDzI9pEy3YRH4B32sJ74hX oBIQ== X-Gm-Message-State: ABy/qLZfiqjMHN6ZiAMX2dc6grzFSeP5rshMi/9km5rhav3ytK6zMn5W n3KnIjx/BHcyD4brNCO0Ntw5REpyOB8= X-Google-Smtp-Source: APBJJlFs+p1+IFx0/LyJPwchu48cnzJ5YCTLzpQdjYobzqCAPy/6bN4ZVrBDwToPCue3BOGsJ6NlQ5kk+iE= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:3dec:ef9e:7bf4:7a6]) (user=surenb job=sendgmr) by 2002:a81:ac58:0:b0:579:e07c:2798 with SMTP id z24-20020a81ac58000000b00579e07c2798mr160834ywj.2.1691083629316; Thu, 03 Aug 2023 10:27:09 -0700 (PDT) Date: Thu, 3 Aug 2023 10:26:51 -0700 In-Reply-To: <20230803172652.2849981-1-surenb@google.com> Mime-Version: 1.0 References: <20230803172652.2849981-1-surenb@google.com> X-Mailer: git-send-email 2.41.0.585.gd2178a4bd4-goog Message-ID: <20230803172652.2849981-7-surenb@google.com> Subject: [PATCH v3 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, Suren Baghdasaryan , Linus Torvalds , "Liam R . Howlett" X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 42D2F10001F X-Stat-Signature: z9sq3g4rst3q6eq3bakhab5r3q1u37fw X-HE-Tag: 1691083630-900314 X-HE-Meta: U2FsdGVkX19nYvEe2pu4MY89/9Lx0NWhcTwCQkxfDYq+lDkgrSGNicXkyTrCUIWxZe5UlILwUA+6VZFCGWPFleZs8XddI0Dk+hKKDzL2gL+zkZc0w6UzA0bGTzDEn6w1sz8hWXR/JoqdwMA51beK2bZmOzLsONKyHJuP7na1me8l+HxIv6YAzCo2s55ojVCYQx08RD42fJfR44wOLF7AxtPh+o+9n9jZt4dTtl4uVWMbLzSQB21dEbFPHUWnR44UAxgZn7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tDISq/y4ScGJ6LsMfZJVDkMM+HFPN1FSR5GZzi8Q59W8PiYc/IW/wQhDFbmfKY9z9i72a3mrrwFi4dR41G3mXREAXJwp4rekkux/wjBZJqZ1QetsyueY+7XW2qNykWaigc2AHUsDEBw1ekSoaQ8Xjf8QeNP5fA8S3RbEKfgFYq365QS0jaDsuX1aBtfL69LMF5X3lh9A/Tpp5p0mEKwsYvqIy3jFp+SP3ja90XrH9DkcoRchqvuzffi/3MQ4uFM8vJCNgjA6fWdJW21+mICTMpwup+e1JlgstlC7xCa5kgyeY/gNVgmmDExqPfwD1D2GjKq980OcdbrK6Ceznrrv5crs9dgalJfO9tAFLn4YxuWXOqS+4Y0c48CRwvZ47gAFzm9HbyU= 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 | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/mm/mmap.c b/mm/mmap.c index 850a39dee075..ae28d6f94c34 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 (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,6 +966,7 @@ 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_end = addr; @@ -983,6 +984,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 +2375,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 +3083,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);