From nobody Mon Dec 15 11:50:12 2025 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 BC3CCC74A5B for ; Sat, 18 Mar 2023 11:15:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229866AbjCRLPi (ORCPT ); Sat, 18 Mar 2023 07:15:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56122 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229822AbjCRLPf (ORCPT ); Sat, 18 Mar 2023 07:15:35 -0400 Received: from mail-wm1-x334.google.com (mail-wm1-x334.google.com [IPv6:2a00:1450:4864:20::334]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C35A0532B7 for ; Sat, 18 Mar 2023 04:15:29 -0700 (PDT) Received: by mail-wm1-x334.google.com with SMTP id p16so4725631wmq.5 for ; Sat, 18 Mar 2023 04:15:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1679138128; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=rtTyPgAyyTCsuj6xg05jXpazWd4/Ojg4wA/2tNBdu1s=; b=lcVvYIEW8+t4NkKIqKPZkQ/jm/KlLLZlZl9jbndrlXKESJ5B22z2oeoab9fhvx+Ntt 7oS48C9g1A1M+KR2O9NE6h0VV/leh0sMEzsU91SdB19XsZv0mQtwCQ5PnhewtgTzI7La CKKKpaD768kZd9t/WlyMRlj7ZSYq7zOC+J/qwyYJkN8786JINt+DfcRYSDM43SLE1R3R o6aD7LoZPqYpDm/NP89CyAhno6pNBm0a+eVvURQrJQtmWM8jHN9fbZzESqPfc3nFObZl XuW3leBcK6d5ksDIinObI2+pj8l5wd4WKhTKcUMybeeIZyUBx9Qn5OwL4nFxK/TEwlAE b0Ng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679138128; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=rtTyPgAyyTCsuj6xg05jXpazWd4/Ojg4wA/2tNBdu1s=; b=x7QXDAtDPTtQ3ZHrK8wY8Kwrjtu1fUkjliktePf2m3N7UJYHhAZfVmQcDD3ecaCT9A h54HNu5HMmXtFk29emCQnMCxHFaAxuSa2gWhpzOyxzU/sM7r8XfGXB1/aNlym/n1Oql5 OWr4N+mLVZXv3Qo+0yO0fkS5KwKXhW7oDSBFTle1o+fiMOp5ivVOHZqKRpXJgM1Jgv20 iQ2gtryFwZwXHvoiue1VcGXa9CF5rk0J78alRcGxL0SeecbwuaqDzxiBN9wi+uJo4beT QBPN8n6e8nKCnBe0IHYGqTgPxSncjjBkx4H4pWb47h8jwS/eVJ0KPNkHSWbIJLMmGH8f yNew== X-Gm-Message-State: AO0yUKU+YHjRqr3uB6cVCfZtK/Kz84JmoE23Lm7o07i0tsyqLtFIImPv wDXgblv7UtlN0XMbpju8ZiI= X-Google-Smtp-Source: AK7set8PKRFrmahfYs9pbXZ+Lyy8xMiJQ1AWofunInVbiM9ckVRROOif+SqzAwtD9W2TAhn8L3LM3w== X-Received: by 2002:a05:600c:4f07:b0:3ed:3cec:d2ec with SMTP id l7-20020a05600c4f0700b003ed3cecd2ecmr10106468wmq.15.1679138128112; Sat, 18 Mar 2023 04:15:28 -0700 (PDT) Received: from lucifer.home (host86-146-209-214.range86-146.btcentralplus.com. [86.146.209.214]) by smtp.googlemail.com with ESMTPSA id n23-20020a1c7217000000b003eb68bb61c8sm4849965wmc.3.2023.03.18.04.15.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 18 Mar 2023 04:15:27 -0700 (PDT) From: Lorenzo Stoakes To: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrew Morton Cc: David Hildenbrand , Matthew Wilcox , Vlastimil Babka , "Liam R . Howlett" , maple-tree@lists.infradead.org, Lorenzo Stoakes Subject: [PATCH 1/4] mm/mmap/vma_merge: further improve prev/next VMA naming Date: Sat, 18 Mar 2023 11:13:18 +0000 Message-Id: <6001e08fa7e119470cbb1d2b6275ad8d742ff9a7.1679137163.git.lstoakes@gmail.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Previously the ASCII diagram above vma_merge() and the accompanying variable naming was rather confusing, however recent efforts by Liam Howlett and Vlastimil Babka have significantly improved matters. This patch goes a little further - replacing 'X' with 'N' which feels a lot more natural and replacing what was 'N' with 'C' which stands for 'concurrent' VMA. No word quite describes a VMA that has coincident start as the input span, concurrent, abbreviated to 'curr' (and which can be thought of also as 'current') however fits intuitions well alongside prev and next. This has no functional impact. Signed-off-by: Lorenzo Stoakes --- mm/mmap.c | 86 +++++++++++++++++++++++++++---------------------------- 1 file changed, 43 insertions(+), 43 deletions(-) diff --git a/mm/mmap.c b/mm/mmap.c index 042d22e63528..c9834364ac98 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -861,44 +861,44 @@ can_vma_merge_after(struct vm_area_struct *vma, unsig= ned long vm_flags, * this area are about to be changed to vm_flags - and the no-change * case has already been eliminated. * - * The following mprotect cases have to be considered, where AAAA is + * The following mprotect cases have to be considered, where **** is * the area passed down from mprotect_fixup, never extending beyond one - * vma, PPPP is the previous vma, NNNN is a vma that starts at the same - * address as AAAA and is of the same or larger span, and XXXX the next - * vma after AAAA: + * vma, PPPP is the previous vma, CCCC is a concurrent vma that starts + * at the same address as **** and is of the same or larger span, and + * NNNN the next vma after ****: * - * AAAA AAAA AAAA - * PPPPPPXXXXXX PPPPPPXXXXXX PPPPPPNNNNNN + * **** **** **** + * PPPPPPNNNNNN PPPPPPNNNNNN PPPPPPCCCCCC * cannot merge might become might become - * PPXXXXXXXXXX PPPPPPPPPPNN + * PPNNNNNNNNNN PPPPPPPPPPCC * mmap, brk or case 4 below case 5 below * mremap move: - * AAAA AAAA - * PPPP XXXX PPPPNNNNXXXX + * **** **** + * PPPP NNNN PPPPCCCCNNNN * might become might become * PPPPPPPPPPPP 1 or PPPPPPPPPPPP 6 or - * PPPPPPPPXXXX 2 or PPPPPPPPXXXX 7 or - * PPPPXXXXXXXX 3 PPPPXXXXXXXX 8 + * PPPPPPPPNNNN 2 or PPPPPPPPNNNN 7 or + * PPPPNNNNNNNN 3 PPPPNNNNNNNN 8 * - * It is important for case 8 that the vma NNNN overlapping the - * region AAAA is never going to extended over XXXX. Instead XXXX must - * be extended in region AAAA and NNNN must be removed. This way in + * It is important for case 8 that the vma CCCC overlapping the + * region **** is never going to extended over NNNN. Instead NNNN must + * be extended in region **** and CCCC must be removed. This way in * all cases where vma_merge succeeds, the moment vma_merge drops the * rmap_locks, the properties of the merged vma will be already * correct for the whole merged range. Some of those properties like * vm_page_prot/vm_flags may be accessed by rmap_walks and they must * be correct for the whole merged range immediately after the - * rmap_locks are released. Otherwise if XXXX would be removed and - * NNNN would be extended over the XXXX range, remove_migration_ptes + * rmap_locks are released. Otherwise if NNNN would be removed and + * CCCC would be extended over the NNNN range, remove_migration_ptes * or other rmap walkers (if working on addresses beyond the "end" - * parameter) may establish ptes with the wrong permissions of NNNN - * instead of the right permissions of XXXX. + * parameter) may establish ptes with the wrong permissions of CCCC + * instead of the right permissions of NNNN. * * In the code below: * PPPP is represented by *prev - * NNNN is represented by *mid or not represented at all (NULL) - * XXXX is represented by *next or not represented at all (NULL) - * AAAA is not represented - it will be merged and the vma containing the + * CCCC is represented by *curr or not represented at all (NULL) + * NNNN is represented by *next or not represented at all (NULL) + * **** is not represented - it will be merged and the vma containing the * area is returned, or the function will return NULL */ struct vm_area_struct *vma_merge(struct vma_iterator *vmi, struct mm_struc= t *mm, @@ -911,7 +911,7 @@ struct vm_area_struct *vma_merge(struct vma_iterator *v= mi, struct mm_struct *mm, { pgoff_t pglen =3D (end - addr) >> PAGE_SHIFT; pgoff_t vma_pgoff; - struct vm_area_struct *mid, *next, *res =3D NULL; + struct vm_area_struct *curr, *next, *res =3D NULL; struct vm_area_struct *vma, *adjust, *remove, *remove2; int err =3D -1; bool merge_prev =3D false; @@ -930,19 +930,19 @@ struct vm_area_struct *vma_merge(struct vma_iterator = *vmi, struct mm_struct *mm, if (vm_flags & VM_SPECIAL) return NULL; =20 - mid =3D find_vma(mm, prev ? prev->vm_end : 0); - if (mid && mid->vm_end =3D=3D end) /* cases 6, 7, 8 */ - next =3D find_vma(mm, mid->vm_end); + curr =3D find_vma(mm, prev ? prev->vm_end : 0); + if (curr && curr->vm_end =3D=3D end) /* cases 6, 7, 8 */ + next =3D find_vma(mm, curr->vm_end); else - next =3D mid; + next =3D curr; =20 - /* In cases 1 - 4 there's no NNNN vma */ - if (mid && end <=3D mid->vm_start) - mid =3D NULL; + /* In cases 1 - 4 there's no CCCC vma */ + if (curr && end <=3D curr->vm_start) + curr =3D NULL; =20 /* verify some invariant that must be enforced by the caller */ VM_WARN_ON(prev && addr <=3D prev->vm_start); - VM_WARN_ON(mid && end > mid->vm_end); + VM_WARN_ON(curr && end > curr->vm_end); VM_WARN_ON(addr >=3D end); =20 if (prev) { @@ -974,21 +974,21 @@ struct vm_area_struct *vma_merge(struct vma_iterator = *vmi, struct mm_struct *mm, remove =3D next; /* case 1 */ vma_end =3D next->vm_end; err =3D dup_anon_vma(prev, next); - if (mid) { /* case 6 */ - remove =3D mid; + if (curr) { /* case 6 */ + remove =3D curr; remove2 =3D next; if (!next->anon_vma) - err =3D dup_anon_vma(prev, mid); + err =3D dup_anon_vma(prev, curr); } } else if (merge_prev) { err =3D 0; /* case 2 */ - if (mid) { - err =3D dup_anon_vma(prev, mid); - if (end =3D=3D mid->vm_end) { /* case 7 */ - remove =3D mid; + if (curr) { + err =3D dup_anon_vma(prev, curr); + if (end =3D=3D curr->vm_end) { /* case 7 */ + remove =3D curr; } else { /* case 5 */ - adjust =3D mid; - adj_start =3D (end - mid->vm_start); + adjust =3D curr; + adj_start =3D (end - curr->vm_start); } } } else if (merge_next) { @@ -1004,10 +1004,10 @@ struct vm_area_struct *vma_merge(struct vma_iterato= r *vmi, struct mm_struct *mm, vma_end =3D next->vm_end; vma_pgoff =3D next->vm_pgoff; err =3D 0; - if (mid) { /* case 8 */ - vma_pgoff =3D mid->vm_pgoff; - remove =3D mid; - err =3D dup_anon_vma(next, mid); + if (curr) { /* case 8 */ + vma_pgoff =3D curr->vm_pgoff; + remove =3D curr; + err =3D dup_anon_vma(next, curr); } } } --=20 2.39.2 From nobody Mon Dec 15 11:50:12 2025 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 F3C68C76195 for ; Sat, 18 Mar 2023 11:15:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229830AbjCRLPk (ORCPT ); Sat, 18 Mar 2023 07:15:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56120 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229502AbjCRLPf (ORCPT ); Sat, 18 Mar 2023 07:15:35 -0400 Received: from mail-wm1-x334.google.com (mail-wm1-x334.google.com [IPv6:2a00:1450:4864:20::334]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 573F653288 for ; Sat, 18 Mar 2023 04:15:31 -0700 (PDT) Received: by mail-wm1-x334.google.com with SMTP id r19-20020a05600c459300b003eb3e2a5e7bso4784405wmo.0 for ; Sat, 18 Mar 2023 04:15:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1679138130; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=HZlYT4yL6gG4aiu138Y6FqESLQRK9cKcNW+GTnQ4unA=; b=L6ZXj/o770m/a9MHhvqQP482KOTn/Lj9NjwPG2bsh3o+0lng/DLoGaGTRkkVC48X0U SEE5gUJVG4i9Bf2BJXChg2NvzUjyZNhT4dxwYM5inETsttenJ7OqCzo4V0NO0IhfbvHo kMmW5nJI0TFVYuNdPATbt+lnGhuqOdor7VhBo+1NePZK+MDnfikpIj79rkSQ/xLIewnJ ubaMjhza6pr7HR9PLLkoemCwGs9j9AgO0eY2zP3v+FEfbkC714K8Mdrz4SSKnhCqGRRr FjCF+Gyzb1n7CdbTvdUHInG6dD/JzL6dppEcUlk05fR99qnjWTFMtAcaf4lZUT4587Zb VcCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679138130; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=HZlYT4yL6gG4aiu138Y6FqESLQRK9cKcNW+GTnQ4unA=; b=vtvwnd5MjeiqvQWJof/0Gi/xnKGrdWueYfiVS6Z3azPBClDwwED/CEd3hOXMwbdFA/ NKD6p2XcXTzL2S4i1hpM1DgVs2SBjyEfqfRBTqa0wjSOdwWLgmZ0qScVpfU17Yz4OwG+ nkMUMzdjax6WwucsnzPFnFiTUHjHSBan30LjtvH9KZzLzJZk4evop/RzN6UqHihTkf23 rhlw5L2c9twor3ub9+/QwQhLFxffR9cK7+KIyCtzWCQ7RYeHjJx6BX1WEPEdXMd2FZl7 GzCajNKPDDFSRKChZ5Uzq0gWzY+qRisED5Sh4xTTFQyjAOPX2favAq0zKagzomZPcNbJ R3yQ== X-Gm-Message-State: AO0yUKU+/8b/pyGH7NdnFxEwEcGpD9Ji6AM+TqryrgZMsWWUrY2+UpLj fhT1s+NQh81+u6hbWDmMSo4= X-Google-Smtp-Source: AK7set/YBLlL51zfnZDvz3OnlvanbGT7LLpQQGQ8vQCU9BZh2LlMpZHdR/PxwGEXulU3Mw93kCFDbg== X-Received: by 2002:a05:600c:3aca:b0:3ed:6049:a5ae with SMTP id d10-20020a05600c3aca00b003ed6049a5aemr4655107wms.4.1679138129676; Sat, 18 Mar 2023 04:15:29 -0700 (PDT) Received: from lucifer.home (host86-146-209-214.range86-146.btcentralplus.com. [86.146.209.214]) by smtp.googlemail.com with ESMTPSA id n23-20020a1c7217000000b003eb68bb61c8sm4849965wmc.3.2023.03.18.04.15.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 18 Mar 2023 04:15:28 -0700 (PDT) From: Lorenzo Stoakes To: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrew Morton Cc: David Hildenbrand , Matthew Wilcox , Vlastimil Babka , "Liam R . Howlett" , maple-tree@lists.infradead.org, Lorenzo Stoakes Subject: [PATCH 2/4] mm/mmap/vma_merge: set next to NULL if not applicable Date: Sat, 18 Mar 2023 11:13:19 +0000 Message-Id: <4d717269303d8a6fe1d837968c252eeb6ff1d7e5.1679137163.git.lstoakes@gmail.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" We are only interested in next if end =3D=3D next->vm_start (in which case = we check to see if we can set merge_next), so perform this check alongside checking whether curr should be set. This groups all of the simple range checks together and establishes the invariant that, if prev, curr or next are non-NULL then their positions are as expected. Additionally, use the abstract 'vma' object to look up the possible curr or next VMA in order to avoid any confusion as to what these variables represent - now curr and next are assigned once and only once. This has no functional impact. Signed-off-by: Lorenzo Stoakes --- mm/mmap.c | 61 ++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 49 insertions(+), 12 deletions(-) diff --git a/mm/mmap.c b/mm/mmap.c index c9834364ac98..66893fc72e03 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -930,15 +930,53 @@ struct vm_area_struct *vma_merge(struct vma_iterator = *vmi, struct mm_struct *mm, if (vm_flags & VM_SPECIAL) return NULL; =20 - curr =3D find_vma(mm, prev ? prev->vm_end : 0); - if (curr && curr->vm_end =3D=3D end) /* cases 6, 7, 8 */ - next =3D find_vma(mm, curr->vm_end); - else - next =3D curr; + /* + * If there is a previous VMA specified, find the next, otherwise find + * the first. + */ + vma =3D find_vma(mm, prev ? prev->vm_end : 0); + + /* + * Does the input range span an existing VMA? If so, we designate this + * VMA 'curr'. The caller will have ensured that curr->vm_start =3D=3D ad= dr. + * + * Cases 5 - 8. + */ + if (vma && end > vma->vm_start) { + curr =3D vma; =20 - /* In cases 1 - 4 there's no CCCC vma */ - if (curr && end <=3D curr->vm_start) + /* + * If the addr - end range spans this VMA entirely, then we + * check to see if another VMA follows it. + * + * If it is _immediately_ adjacent (checked below), then we + * designate it 'next' (cases 6 - 8). + */ + if (curr->vm_end =3D=3D end) + vma =3D find_vma(mm, curr->vm_end); + else + /* Case 5. */ + vma =3D NULL; + } else { + /* + * The addr - end range either spans the end of prev or spans no + * VMA at all - in either case we dispense with 'curr' and + * maintain only 'prev' and (possibly) 'next'. + * + * Cases 1 - 4. + */ curr =3D NULL; + } + + /* + * We only actually examine the next VMA if it is immediately adjacent + * to end which sits either at the end of a hole (cases 1 - 3), PPPP + * (case 4) or CCCC (cases 6 - 8). + */ + if (vma && end =3D=3D vma->vm_start) + next =3D vma; + else + next =3D NULL; =20 /* verify some invariant that must be enforced by the caller */ VM_WARN_ON(prev && addr <=3D prev->vm_start); @@ -959,11 +997,10 @@ struct vm_area_struct *vma_merge(struct vma_iterator = *vmi, struct mm_struct *mm, } } /* Can we merge the successor? */ - if (next && end =3D=3D next->vm_start && - mpol_equal(policy, vma_policy(next)) && - can_vma_merge_before(next, vm_flags, - anon_vma, file, pgoff+pglen, - vm_userfaultfd_ctx, anon_name)) { + if (next && mpol_equal(policy, vma_policy(next)) && + can_vma_merge_before(next, vm_flags, + anon_vma, file, pgoff+pglen, + vm_userfaultfd_ctx, anon_name)) { merge_next =3D true; } =20 --=20 2.39.2 From nobody Mon Dec 15 11:50:12 2025 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 1BBA8C7618A for ; Sat, 18 Mar 2023 11:15:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229902AbjCRLPn (ORCPT ); Sat, 18 Mar 2023 07:15:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56118 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229832AbjCRLPg (ORCPT ); Sat, 18 Mar 2023 07:15:36 -0400 Received: from mail-wm1-x32d.google.com (mail-wm1-x32d.google.com [IPv6:2a00:1450:4864:20::32d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AAB6653705 for ; Sat, 18 Mar 2023 04:15:32 -0700 (PDT) Received: by mail-wm1-x32d.google.com with SMTP id o11-20020a05600c4fcb00b003eb33ea29a8so4779300wmq.1 for ; Sat, 18 Mar 2023 04:15:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1679138131; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=YNIdJ6Vt6KcFvn7hpLjquLcVPqIZMiCegIYHYyxVYMQ=; b=YMQqEVZicnvPGmZNhsvLOxCWnnQ4bjhFqhJ82RSQUi0l0UVsKNSk3G7LWahwyS905J swwajsI070tAoW5Lm6cSP7fXTIC4aTibf8W/tNdetrlBGyPiIq+HIZ7DLScmCKjLOTby nttJ+XmxMdfwYloG/jxJxQR/KmIjTQxvdQOFDnI7HKBupIH9vOALsYdb4cVJnJxRJwuo Onw484Ikw64e8ppcyEHnXW7y2q1jTAKZWcB9r0n0onaKB7xG7uUr1ZvhzjE892amjNLs 4i/8D/ciyaHtYYQF0XluT6STyjKEr6PUmgcjpPhiJLBjKSEvzfheFEii0uC8cUJfoDx7 ALIg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679138131; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=YNIdJ6Vt6KcFvn7hpLjquLcVPqIZMiCegIYHYyxVYMQ=; b=OeGkq8s4A2ow/4iSJrawXouc2QafEXZVvlCcmCJqGSE1YIdlPDsFZAs3WhP0rWahbE e/htEHEfQXm19Xmcrse8hzBfujWAqx6JFg3wQCuJWLpGwbfKkIrFQxFqomDui0mnSV2+ SN/hKTPdZ9B1ZxUrVBFFhoc9YvaFTi+3Pt/qH+rXC3RY01p/zV0vxfWAke/gE5KB+aqJ f58eNd24rQQ4o3B5ncvJKq/rEDXAO2+u5MfJNmVl0nIOKf65wA6q+ays/1FaZfQU1HXe 0/RuX03fWKPmarl558yccK/q39H6FVivtsx7+2G+f0ZKGufufCJpBDkg8Uu2UZupAELM Q8Hw== X-Gm-Message-State: AO0yUKV6LuI6O95websDde8nqxLWC5i8SpIYB6TA9AYSqD9luq/HUIA6 wMuee1UEMo0fwjBKYevnJz4= X-Google-Smtp-Source: AK7set94PT/Q7iAywBf0Cb7oTqcOQNbpnm2Aipfhodna7X7JLeaBkzyN1dtmmLsIFyBtm6OTuR62Qw== X-Received: by 2002:a05:600c:46d2:b0:3ed:46e2:85fb with SMTP id q18-20020a05600c46d200b003ed46e285fbmr7951903wmo.33.1679138131106; Sat, 18 Mar 2023 04:15:31 -0700 (PDT) Received: from lucifer.home (host86-146-209-214.range86-146.btcentralplus.com. [86.146.209.214]) by smtp.googlemail.com with ESMTPSA id n23-20020a1c7217000000b003eb68bb61c8sm4849965wmc.3.2023.03.18.04.15.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 18 Mar 2023 04:15:30 -0700 (PDT) From: Lorenzo Stoakes To: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrew Morton Cc: David Hildenbrand , Matthew Wilcox , Vlastimil Babka , "Liam R . Howlett" , maple-tree@lists.infradead.org, Lorenzo Stoakes Subject: [PATCH 3/4] mm/mmap/vma_merge: extend invariants, avoid invalid res, vma Date: Sat, 18 Mar 2023 11:13:20 +0000 Message-Id: <0b7d3e6c770a2bdc2c307ddde268561135ef3395.1679137163.git.lstoakes@gmail.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Previously, vma was an uninitialised variable which was only definitely assigned as a result of the logic covering all possible input cases - for it to have remained uninitialised, prev would have to be NULL, and next would _have_ to be mergeable. We now reuse vma to assign curr and next, so to be absolutely explicit, ensure this variable is _always_ assigned, and while we're at it remove the redundant assignment of both res and vma (if prev is NULL then we simply assign to NULL). In addition, we absolutely do rely on addr =3D=3D curr->vm_start should curr exist, so assert as much. Signed-off-by: Lorenzo Stoakes --- mm/mmap.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/mm/mmap.c b/mm/mmap.c index 66893fc72e03..e60c637f4e49 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -911,7 +911,7 @@ struct vm_area_struct *vma_merge(struct vma_iterator *v= mi, struct mm_struct *mm, { pgoff_t pglen =3D (end - addr) >> PAGE_SHIFT; pgoff_t vma_pgoff; - struct vm_area_struct *curr, *next, *res =3D NULL; + struct vm_area_struct *curr, *next, *res; struct vm_area_struct *vma, *adjust, *remove, *remove2; int err =3D -1; bool merge_prev =3D false; @@ -978,14 +978,18 @@ struct vm_area_struct *vma_merge(struct vma_iterator = *vmi, struct mm_struct *mm, else next =3D NULL; =20 - /* verify some invariant that must be enforced by the caller */ + /* + * By default, we return prev. Cases 3, 4, 8 will instead return next + * and cases 3, 8 will also update vma to point at next. + */ + res =3D vma =3D prev; + + /* Verify some invariant that must be enforced by the caller. */ VM_WARN_ON(prev && addr <=3D prev->vm_start); - VM_WARN_ON(curr && end > curr->vm_end); + VM_WARN_ON(curr && (addr !=3D curr->vm_start || end > curr->vm_end)); VM_WARN_ON(addr >=3D end); =20 if (prev) { - res =3D prev; - vma =3D prev; vma_start =3D prev->vm_start; vma_pgoff =3D prev->vm_pgoff; /* Can we merge the predecessor? */ @@ -996,6 +1000,7 @@ struct vm_area_struct *vma_merge(struct vma_iterator *= vmi, struct mm_struct *mm, vma_prev(vmi); } } + /* Can we merge the successor? */ if (next && mpol_equal(policy, vma_policy(next)) && can_vma_merge_before(next, vm_flags, @@ -1036,6 +1041,10 @@ struct vm_area_struct *vma_merge(struct vma_iterator= *vmi, struct mm_struct *mm, adj_start =3D -(prev->vm_end - addr); err =3D dup_anon_vma(next, prev); } else { + /* + * Note that cases 3 and 8 are the ONLY ones where prev + * is permitted to be (but is not necessarily) NULL. + */ vma =3D next; /* case 3 */ vma_start =3D addr; vma_end =3D next->vm_end; --=20 2.39.2 From nobody Mon Dec 15 11:50:12 2025 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 E12D8C61DA4 for ; Sat, 18 Mar 2023 11:15:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229843AbjCRLPu (ORCPT ); Sat, 18 Mar 2023 07:15:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56118 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229878AbjCRLPj (ORCPT ); Sat, 18 Mar 2023 07:15:39 -0400 Received: from mail-wm1-x32e.google.com (mail-wm1-x32e.google.com [IPv6:2a00:1450:4864:20::32e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 762BE366A3 for ; Sat, 18 Mar 2023 04:15:34 -0700 (PDT) Received: by mail-wm1-x32e.google.com with SMTP id w11so3671756wmo.2 for ; Sat, 18 Mar 2023 04:15:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1679138133; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=zGH+7XD9CZf7riiQs7tokzcrN5LQH5VfpFAGRw9yBzQ=; b=TNBn6zG1hdBUOTzqqLTQiTtEN0JRMCO/ZKWzRMNFgj98uLt+q4k8ghjew+1pvNNue0 WwuBPWByIjGO6uz6yW0hfU/p/UBnFVzOKoUSjnAJjKSqgICoaJmKntHF+CK5laaQom3b 1s/U5zrnWwkkTUXUeSuRY3AQEcLUewaUMMA+mlEsxedaVkkwOotRV04GT9Lo+B5HTSnS WEHauh80WAubtJWn7pTBHLj2ioyNwyVm3yLeSNxz7YYMVYY3uIoL9rZGI8ai495/0CVU jNKdhGVgyJ0bjR8u3rwu4aVVaVpzSZg9TPbaRJ2RZbZ2iRsUekHAW24kpm90Ip/zFoXW ubfw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679138133; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=zGH+7XD9CZf7riiQs7tokzcrN5LQH5VfpFAGRw9yBzQ=; b=sTYHkpCUfnY7dYWOmDsvNfJloMKlUHzLGpq4/wefccoYLP2Yv41rOOTpf9o77lUsKQ pYlN5+wknw2nGVnx1fcIBk17qY241BmTi9XQ3UyB4jY8Po4Kl7n+LtQ89Spk2CYErRqf iOwLZ5D62UDX2zSxAz7TQnDASinWggCSwMjFd7fZHryb/eaH+NqFfcqRJ0qhEQpX3AEW Of79oduYT7pCVahYqvPXti74z0nL2WeVag5EOGCiQopWjbUNgg2xUH2ZLjWM55bEdtoq yPAAGUOsTpIgEF68rf6HCtM1gFbVWI08YbkwExrRzW3S8WcxOEhsSvymPprWVpekT99y euOQ== X-Gm-Message-State: AO0yUKVP6cwewdqpn99Ur5SK5k5audXYslAUWENkyZ0H39KpZsAEpD8c MKh7U/Bwmnl3dezoWEobTSWr/kugvlE= X-Google-Smtp-Source: AK7set8EFNhOT1+VWTHfriGxmv6FTPoHw/mhcihpxX6H7XQbmsH1Imd3c4T2RUMnWGoOhIvwMJ06xw== X-Received: by 2002:a1c:ed16:0:b0:3ed:a45d:aee9 with SMTP id l22-20020a1ced16000000b003eda45daee9mr1705649wmh.39.1679138132644; Sat, 18 Mar 2023 04:15:32 -0700 (PDT) Received: from lucifer.home (host86-146-209-214.range86-146.btcentralplus.com. [86.146.209.214]) by smtp.googlemail.com with ESMTPSA id n23-20020a1c7217000000b003eb68bb61c8sm4849965wmc.3.2023.03.18.04.15.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 18 Mar 2023 04:15:31 -0700 (PDT) From: Lorenzo Stoakes To: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrew Morton Cc: David Hildenbrand , Matthew Wilcox , Vlastimil Babka , "Liam R . Howlett" , maple-tree@lists.infradead.org, Lorenzo Stoakes Subject: [PATCH 4/4] mm/mmap/vma_merge: be explicit about the non-mergeable case Date: Sat, 18 Mar 2023 11:13:21 +0000 Message-Id: X-Mailer: git-send-email 2.39.2 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Rather than setting err =3D -1 and only resetting if we hit merge cases, explicitly check the non-mergeable case to make it abundantly clear that we only proceed with the rest if something is mergeable, default err to 0 and only update if an error might occur. Additionally set merge_next directly as there is no need for an if() {} statement assigning a boolean. This has no functional impact. Signed-off-by: Lorenzo Stoakes --- mm/mmap.c | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/mm/mmap.c b/mm/mmap.c index e60c637f4e49..2ac43b2b9a00 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -913,7 +913,7 @@ struct vm_area_struct *vma_merge(struct vma_iterator *v= mi, struct mm_struct *mm, pgoff_t vma_pgoff; struct vm_area_struct *curr, *next, *res; struct vm_area_struct *vma, *adjust, *remove, *remove2; - int err =3D -1; + int err =3D 0; bool merge_prev =3D false; bool merge_next =3D false; bool vma_expanded =3D false; @@ -1002,12 +1002,15 @@ struct vm_area_struct *vma_merge(struct vma_iterato= r *vmi, struct mm_struct *mm, } =20 /* Can we merge the successor? */ - if (next && mpol_equal(policy, vma_policy(next)) && - can_vma_merge_before(next, vm_flags, - anon_vma, file, pgoff+pglen, - vm_userfaultfd_ctx, anon_name)) { - merge_next =3D true; - } + merge_next =3D next && + mpol_equal(policy, vma_policy(next)) && + can_vma_merge_before(next, vm_flags, + anon_vma, file, pgoff+pglen, + vm_userfaultfd_ctx, anon_name); + + /* Not mergeable. */ + if (!merge_prev && !merge_next) + return NULL; =20 remove =3D remove2 =3D adjust =3D NULL; /* Can we merge both the predecessor and the successor? */ @@ -1023,7 +1026,7 @@ struct vm_area_struct *vma_merge(struct vma_iterator = *vmi, struct mm_struct *mm, err =3D dup_anon_vma(prev, curr); } } else if (merge_prev) { - err =3D 0; /* case 2 */ + /* case 2 */ if (curr) { err =3D dup_anon_vma(prev, curr); if (end =3D=3D curr->vm_end) { /* case 7 */ @@ -1033,7 +1036,7 @@ struct vm_area_struct *vma_merge(struct vma_iterator = *vmi, struct mm_struct *mm, adj_start =3D (end - curr->vm_start); } } - } else if (merge_next) { + } else { /* merge_next */ res =3D next; if (prev && addr < prev->vm_end) { /* case 4 */ vma_end =3D addr; @@ -1049,7 +1052,7 @@ struct vm_area_struct *vma_merge(struct vma_iterator = *vmi, struct mm_struct *mm, vma_start =3D addr; vma_end =3D next->vm_end; vma_pgoff =3D next->vm_pgoff; - err =3D 0; + if (curr) { /* case 8 */ vma_pgoff =3D curr->vm_pgoff; remove =3D curr; @@ -1058,7 +1061,7 @@ struct vm_area_struct *vma_merge(struct vma_iterator = *vmi, struct mm_struct *mm, } } =20 - /* Cannot merge or error in anon_vma clone */ + /* Error in anon_vma clone. */ if (err) return NULL; =20 --=20 2.39.2