From patchwork Sat Mar 18 11:13:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 13179744 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 DE95BC7618A for ; Sat, 18 Mar 2023 11:15:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 354E3900004; Sat, 18 Mar 2023 07:15:32 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2DE30900002; Sat, 18 Mar 2023 07:15:32 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 156B8900004; Sat, 18 Mar 2023 07:15:32 -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 07DF0900002 for ; Sat, 18 Mar 2023 07:15:32 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id CA70E16067F for ; Sat, 18 Mar 2023 11:15:31 +0000 (UTC) X-FDA: 80581763262.15.3D643A6 Received: from mail-wm1-f48.google.com (mail-wm1-f48.google.com [209.85.128.48]) by imf18.hostedemail.com (Postfix) with ESMTP id E0A761C0013 for ; Sat, 18 Mar 2023 11:15:29 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=lcVvYIEW; spf=pass (imf18.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.128.48 as permitted sender) smtp.mailfrom=lstoakes@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1679138130; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=rtTyPgAyyTCsuj6xg05jXpazWd4/Ojg4wA/2tNBdu1s=; b=jt9YpLf+uVrxJNdYtGq7pzDKOpHzUloVIt/Y/sEsMsdCuSrnxgf2GN30sUz6Mssavu1TYB i8FADXMZ64SfLcJS0XeOgsl78B8nYrjHCip8dLWNE52RdG7MeRVJZW1d1wdZ/kJqJ6U+lZ gziiBjnrT+zHbMTxOEPfq/7NZgn1CrY= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=lcVvYIEW; spf=pass (imf18.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.128.48 as permitted sender) smtp.mailfrom=lstoakes@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1679138130; a=rsa-sha256; cv=none; b=MoRr7En+u8xH4p2kh5Mt+iWcrqwoZrVxJze5X9BmrmVM0ijx7fU68eRusVIeSgOMmRIl7r zkn/YPLyvXa6uwmVKzsUXuDS4xl2MsXbOjiE9+ZKWpiLx5OjC+FU43t2BJaoAmTQY9Hy+2 CR4EfPj3Fp9o4ML6w7OISdWpruuqPYM= Received: by mail-wm1-f48.google.com with SMTP id ay8so4735627wmb.1 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=ReGjcpF+StrzwZ3RhOGC40Z1AbZ1/o96bBop5VpJ5T2ssX3iz3f5sG0xBo2eHLNBuQ RqrTHakjMXw2JZBQYgGqorrWtwn/irPEJTiG8aksn0qp1yqA5BAyV+nvhK9ohXETIyxo Ok6dg8DNdSJ/lez1SASGs/8AC0Il0bL3LQRiViKfOeEErmdOcgfJKydSknE+YWQV7g8L 2VG8i0+kaMAIx8ufTTIhvBZQFY6LCeTuEJrG6DwHY3c1vjlbOo0z8+vtsDD6bEeNRUNe L5vjQKWMnU2s6jyvGpBwAEVpkZRiWqWyvWvK1xv/FCYpElgQL/KoVTBs7O7AtRQZvANE SnKg== X-Gm-Message-State: AO0yUKWuB/BCzjk2/3bN54HpbIA+an+8hJGK/jR2G2upCKpqUE9GKnQ6 MANUHumk3MElFB2a9qlCT4w0K8B5aO4= 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 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: E0A761C0013 X-Rspam-User: X-Stat-Signature: h936mpqk6ncbh97u3oxxejj1atqrspg5 X-HE-Tag: 1679138129-334254 X-HE-Meta: U2FsdGVkX19JQesHL9KJrrelKjBDQnuFCGliRffURKkN7Z0YlWbraR/7/u+Vw1K66nQZKQ7XhEwAFVfg5ZjClcwOu+jV7YPyozlEmSAZQf54h+U6CBucJpnKtpBqxwgSRFF4KBMvn5b500rU4RnodbgsfcCjr5i5/5SsEVyJ3/kVrMJKTuKl395+1vtG4tpdQHUZmf1raVaK/f9K9XjjIOib17+nrz+UVFQIY1UqqclnkXwvq7gxPnGEQq8CdMCLLrFRutR6lyGFMU3vMIVlg6d1VQvY82v9WCHo6UI/mFSLRuV1mhTcxIm4+aUEq0+7ZWhjRLzzVXgE10D0NTRRZh1lmZR5JUxrP1w+32NCZCpw3y9lPdRLZVgedNFi69YrVlwCP77UO8sSxVkuDEpFoRZEXdJALr8Rcqy9alR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6VMkBOmV 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: 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, unsigned 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_struct *mm, @@ -911,7 +911,7 @@ struct vm_area_struct *vma_merge(struct vma_iterator *vmi, struct mm_struct *mm, { pgoff_t pglen = (end - addr) >> PAGE_SHIFT; pgoff_t vma_pgoff; - struct vm_area_struct *mid, *next, *res = NULL; + struct vm_area_struct *curr, *next, *res = NULL; struct vm_area_struct *vma, *adjust, *remove, *remove2; int err = -1; bool merge_prev = 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; - mid = find_vma(mm, prev ? prev->vm_end : 0); - if (mid && mid->vm_end == end) /* cases 6, 7, 8 */ - next = find_vma(mm, mid->vm_end); + curr = find_vma(mm, prev ? prev->vm_end : 0); + if (curr && curr->vm_end == end) /* cases 6, 7, 8 */ + next = find_vma(mm, curr->vm_end); else - next = mid; + next = curr; - /* In cases 1 - 4 there's no NNNN vma */ - if (mid && end <= mid->vm_start) - mid = NULL; + /* In cases 1 - 4 there's no CCCC vma */ + if (curr && end <= curr->vm_start) + curr = NULL; /* verify some invariant that must be enforced by the caller */ VM_WARN_ON(prev && addr <= prev->vm_start); - VM_WARN_ON(mid && end > mid->vm_end); + VM_WARN_ON(curr && end > curr->vm_end); VM_WARN_ON(addr >= end); if (prev) { @@ -974,21 +974,21 @@ struct vm_area_struct *vma_merge(struct vma_iterator *vmi, struct mm_struct *mm, remove = next; /* case 1 */ vma_end = next->vm_end; err = dup_anon_vma(prev, next); - if (mid) { /* case 6 */ - remove = mid; + if (curr) { /* case 6 */ + remove = curr; remove2 = next; if (!next->anon_vma) - err = dup_anon_vma(prev, mid); + err = dup_anon_vma(prev, curr); } } else if (merge_prev) { err = 0; /* case 2 */ - if (mid) { - err = dup_anon_vma(prev, mid); - if (end == mid->vm_end) { /* case 7 */ - remove = mid; + if (curr) { + err = dup_anon_vma(prev, curr); + if (end == curr->vm_end) { /* case 7 */ + remove = curr; } else { /* case 5 */ - adjust = mid; - adj_start = (end - mid->vm_start); + adjust = curr; + adj_start = (end - curr->vm_start); } } } else if (merge_next) { @@ -1004,10 +1004,10 @@ struct vm_area_struct *vma_merge(struct vma_iterator *vmi, struct mm_struct *mm, vma_end = next->vm_end; vma_pgoff = next->vm_pgoff; err = 0; - if (mid) { /* case 8 */ - vma_pgoff = mid->vm_pgoff; - remove = mid; - err = dup_anon_vma(next, mid); + if (curr) { /* case 8 */ + vma_pgoff = curr->vm_pgoff; + remove = curr; + err = dup_anon_vma(next, curr); } } } From patchwork Sat Mar 18 11:13:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 13179745 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 11628C61DA4 for ; Sat, 18 Mar 2023 11:15:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DB481900005; Sat, 18 Mar 2023 07:15:33 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D64A0900002; Sat, 18 Mar 2023 07:15:33 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BDDDF900005; Sat, 18 Mar 2023 07:15:33 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id AF774900002 for ; Sat, 18 Mar 2023 07:15:33 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 836A21A05EE for ; Sat, 18 Mar 2023 11:15:33 +0000 (UTC) X-FDA: 80581763346.28.C49C62F Received: from mail-wm1-f43.google.com (mail-wm1-f43.google.com [209.85.128.43]) by imf30.hostedemail.com (Postfix) with ESMTP id B4CF280011 for ; Sat, 18 Mar 2023 11:15:31 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b="L6ZXj/o7"; spf=pass (imf30.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.128.43 as permitted sender) smtp.mailfrom=lstoakes@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1679138131; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=HZlYT4yL6gG4aiu138Y6FqESLQRK9cKcNW+GTnQ4unA=; b=Jo7MYEoJmqsYI32U/hlBzDLvSlBAz+1g5uLOwKBiN6CeJAlrRCSK5VWzPj/ZsoUWUVBPXp Uf6l+G2CB3eWBnJn/D8zXaPAmoBVfAThm79lfV4LXxi0GDM1HwBEA35heppeW9/ii44iCH j1Pt3DijuNqmNZB3XHnrazOPnUFADk0= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b="L6ZXj/o7"; spf=pass (imf30.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.128.43 as permitted sender) smtp.mailfrom=lstoakes@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1679138131; a=rsa-sha256; cv=none; b=gBdMZugFSGaOYanwOl1uW8M7p6ToSrq6rW+GtksX5Egz6R6P8jFRIRND5QP7BXvy7mgjIv EFa5ggdWkMCFB0lEi7CADX3b9wVYuUmYdc59X/+CXD7RF8VMA7sX1TNg16Se9v1qiIyfsq Xg+olnfPZvPagEoOT7rzynmZUoZvgI0= Received: by mail-wm1-f43.google.com with SMTP id w11so3671723wmo.2 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=E9cCeYVeUcc6R17wvMPySEIsd8wnQvBh6OQZOUyZYdDRyzODD8/8mssbseQ7Uus6w8 mwoBe45cWgOO/QT5aGMQ2wZrL3sHiyqvv6XZlmSm8inov9qOFQf239mgfbzt4d0whukx fnNiAKMxgHFiGypBPLGMeByOpd4bbuODzMixX2aDQ/clLsmi9Qyb8ji88l1sQL1HGlBm Q+JhM0ngxK18W3kQuplIZ2WyEsSNHN2Z+AWXig2keptnGaAAH5WcsnDKdy7YoITEUxR+ OAeT8TQ81m8la0T9Gl3avsfY2CyxVpmTnvUD3fhAAbtZCveOPA9g2tAAQlOZKrG7J6VD wb4Q== X-Gm-Message-State: AO0yUKWACDuzrGTm6jHY0ldaB1/T+Z+UdAipjkPiPqR84EUuya2LGkt5 TNakHk+B4TfyCN27m9WWk+AjsOV4D+M= 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 X-Stat-Signature: jqiwpwgdyhz47ta9i7bnu11f3w79busu X-Rspam-User: X-Rspamd-Queue-Id: B4CF280011 X-Rspamd-Server: rspam06 X-HE-Tag: 1679138131-680972 X-HE-Meta: U2FsdGVkX1+omQTGPnhBTnA6rqyELIXRykVRa0VYG2m9zCYdykSbGLSnjty5mCoMg/3Mk5RFj/M5Z9W8MLsn7esb68FWXWf1TLYokIT2fTJa5/7ObgzuCfBBd6KgaX9A7vJz+Scw4DUyR+exFDSJuwUrOc98t8InknpKbph650O914xlP0VsY2JBo0O25LDGcgI7NZsXpecABQsphRBG7i48iGAuORInJ8Cv6CwWEHZb/vt5SvzkaamhRErbTvD7maO+lI0jY+5+OT2+Z1xKubhK+JTfMe3IZ0R4YTtCeV2aLpSGn7viAWTFoT+KgjKvhkqZZM1zHLQUfznD3rFJwmU1hDt6mZ7f5t3ME+eNGHA7hD0QQcX0WLI8//GKavxdW9+hpPHH4mjGLvoay7fVYqfM55HIdbVULFsc7fsRNOL1Gte1Z+vjn3NzlYV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ogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: We are only interested in next if end == 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 Signed-off-by: Vernon Yang --- 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; - curr = find_vma(mm, prev ? prev->vm_end : 0); - if (curr && curr->vm_end == end) /* cases 6, 7, 8 */ - next = find_vma(mm, curr->vm_end); - else - next = curr; + /* + * If there is a previous VMA specified, find the next, otherwise find + * the first. + */ + vma = 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 == addr. + * + * Cases 5 - 8. + */ + if (vma && end > vma->vm_start) { + curr = vma; - /* In cases 1 - 4 there's no CCCC vma */ - if (curr && end <= 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 == end) + vma = find_vma(mm, curr->vm_end); + else + /* Case 5. */ + vma = 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 = 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 == vma->vm_start) + next = vma; + else + next = NULL; /* verify some invariant that must be enforced by the caller */ VM_WARN_ON(prev && addr <= 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 == 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 = true; } From patchwork Sat Mar 18 11:13:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 13179746 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 52FCEC7618A for ; Sat, 18 Mar 2023 11:15:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 35A77900002; Sat, 18 Mar 2023 07:15:35 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2DF5E280001; Sat, 18 Mar 2023 07:15:35 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1A573900006; Sat, 18 Mar 2023 07:15:35 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 0ACCD900002 for ; Sat, 18 Mar 2023 07:15:35 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id A6723AB599 for ; Sat, 18 Mar 2023 11:15:34 +0000 (UTC) X-FDA: 80581763388.14.210AD03 Received: from mail-wm1-f52.google.com (mail-wm1-f52.google.com [209.85.128.52]) by imf23.hostedemail.com (Postfix) with ESMTP id BE7F4140007 for ; Sat, 18 Mar 2023 11:15:32 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=YMQqEVZi; spf=pass (imf23.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.128.52 as permitted sender) smtp.mailfrom=lstoakes@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1679138132; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=YNIdJ6Vt6KcFvn7hpLjquLcVPqIZMiCegIYHYyxVYMQ=; b=DQrQYVw47k7k9b+8LkVLQxJaNrZOBW9QoVRKFDH5XZeM++pgRNbdB0Ur/x7037+RBMavZR N/WrH2zMfblnRJwPU0omMD2OerxuIj51axxs7qvnNtv822EXgvB83JUmu2cFqEItNYCpHR +UVSWyjUGARakwBmknbNaFbaoTmgt4I= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=YMQqEVZi; spf=pass (imf23.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.128.52 as permitted sender) smtp.mailfrom=lstoakes@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1679138132; a=rsa-sha256; cv=none; b=u05dQkfFlG3rPcPhIkgyumI0PPuQk0Bggcw4Wl9u2B9Q7AseNN9ZATb7jgLWtEZ09arcTS tcnsi+sIhQMj1PHP6ZpsH8EpNlhhwGttiIq4DdBpUTZ00AC4B+Rf34OQdLsUb1agGmsSzS A/IKBz8Cs/ZUIbPOD2j2fKZe1TklXwE= Received: by mail-wm1-f52.google.com with SMTP id p13-20020a05600c358d00b003ed346d4522so4779840wmq.2 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=H+8d+EcgN2IoM/rPdF/MrWGv98moqNvN1Lc6dUrCoIZOamapBdLR1JMJwduyMWyPwO 4LzyhNpcFiHwmUdv4IeEZkztuDatXMMiJeql8vk+cSpz3GS3M4tIhUuDlUwbWvOxdcPW Aqeb7U2Bo/MwEIra8bNPCxbpZ3Ee1qinQbFGSPdCsAJyU6dPRkd4hjuM/mZu3LEaz/dn NRR85ie357w8KZ36Ym3BtmB4T3JCMjGA1Whdbo146mk2MlhmEVzsmYSUQwhBr6etljY6 jgwotzNTihs2jIkdLKDAa0k3AdR7EvvjfdKE3AWMPjm05ahIWYMHEt+cbWCKH9wCm8Y1 mpIQ== X-Gm-Message-State: AO0yUKXfDcl5AV+YQcMIBIWYLKgo53l8deyVkHleN5LSoVGBQx2N87aj XmwErqTtRBwnu6hsiyrNPgYIlXRfWbo= 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 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: BE7F4140007 X-Stat-Signature: 6ec4zr1uodwnnkiu8rg3j5my5kwsxqnc X-HE-Tag: 1679138132-621686 X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Ug6lUXzo3cKSmd4yW3ALTJzPuUQAJKt8Zu1/1g24IqYtsNol8mjwtUhifMmjPG3JsHotSQF5Iz+ENivCcI6F1ZKPGFoyZIgtvbkk0E0BtSbtMjaTqzaBlpvokzYVmP2MwBNwIpzDQd7qMm0AtLKcDt//0alNyqWUntNnoEQPWnFvS117lUvAQB2xqMpck69619SEA5DQwjPD4ydv3loftgcHNy749VX+nlDOIakxpEJn+7tQUipGl+F9ga5FEpDycbStfJanH+ty0c4/H1cJkRnTq0GiAg590B0TpH4kInrhJhA4+kzpuX6kfIDD2+F78SdaKwWFwFjy416rum7TCHMKh2oXkJlhXHS9LyxDZuSYnv3ImlLMKqjF+t7qb0A8lSXu+SQ6hME9aMynGWFxVfXa5D2X86Ce23DAu82seFOuTnjVGaWZsgOamcFhRI5K5BYBjr/L5Pxle2tc 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: 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 == 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 *vmi, struct mm_struct *mm, { pgoff_t pglen = (end - addr) >> PAGE_SHIFT; pgoff_t vma_pgoff; - struct vm_area_struct *curr, *next, *res = NULL; + struct vm_area_struct *curr, *next, *res; struct vm_area_struct *vma, *adjust, *remove, *remove2; int err = -1; bool merge_prev = false; @@ -978,14 +978,18 @@ struct vm_area_struct *vma_merge(struct vma_iterator *vmi, struct mm_struct *mm, else next = NULL; - /* 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 = vma = prev; + + /* Verify some invariant that must be enforced by the caller. */ VM_WARN_ON(prev && addr <= prev->vm_start); - VM_WARN_ON(curr && end > curr->vm_end); + VM_WARN_ON(curr && (addr != curr->vm_start || end > curr->vm_end)); VM_WARN_ON(addr >= end); if (prev) { - res = prev; - vma = prev; vma_start = prev->vm_start; vma_pgoff = 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 = -(prev->vm_end - addr); err = 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 = next; /* case 3 */ vma_start = addr; vma_end = next->vm_end; From patchwork Sat Mar 18 11:13:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 13179747 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 138C0C76196 for ; Sat, 18 Mar 2023 11:15:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B1DA8280002; Sat, 18 Mar 2023 07:15:36 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id ACD0B280001; Sat, 18 Mar 2023 07:15:36 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8D130280002; Sat, 18 Mar 2023 07:15:36 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 7F293280001 for ; Sat, 18 Mar 2023 07:15:36 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 5A9C112053F for ; Sat, 18 Mar 2023 11:15:36 +0000 (UTC) X-FDA: 80581763472.27.D4D01F6 Received: from mail-wm1-f53.google.com (mail-wm1-f53.google.com [209.85.128.53]) by imf14.hostedemail.com (Postfix) with ESMTP id 7FEA4100012 for ; Sat, 18 Mar 2023 11:15:34 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=TNBn6zG1; spf=pass (imf14.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.128.53 as permitted sender) smtp.mailfrom=lstoakes@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1679138134; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=zGH+7XD9CZf7riiQs7tokzcrN5LQH5VfpFAGRw9yBzQ=; b=GNDmu4ByzloqtyUJGJo6ZAEeJp6zjQXdopDzZQGZeTxEwBJkS4afTSXbANSk9Z7es/3mc+ 4L+BFo+BBB4Z/r7bRyeTwKF9ltlDz3PxRwELirsYTli8YsDtYQ9/ZTviziWYSB/YCnODnZ VdOpaqi774QNH1GRUe1zMfH1EbiERQU= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=TNBn6zG1; spf=pass (imf14.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.128.53 as permitted sender) smtp.mailfrom=lstoakes@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1679138134; a=rsa-sha256; cv=none; b=miBL/+3Jft9efvNx1UM5UPqSCOdSluodpnl/1Wem0haPK73EOIbaX2a8KSX99mf4KQsmdO 06wge3iLWK4B4Q8n1XAur1nC2VEmoHJLttD/O5SCzoHjwRTD63GsLvl0tbDwozLjjHNodO mS0fAYkI6KLHgFQLompp07SZ18AiNLU= Received: by mail-wm1-f53.google.com with SMTP id l15-20020a05600c4f0f00b003ed58a9a15eso4764486wmq.5 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=3RnA9xZOaE5XP/esO+mv09BIorQa/9hBmYYTUfCQe6cS+Oyo6XW7AmLkOguDqd/qWW sVgo+xmiB19IC957XnzYoixCQVZ8ukcHredzb8iFZ4uwJmLnmiKLxqjjC/MLR8+rhyK1 lQIa93Nluz73BzbAB0k3npsmkNEah3uRRXU5h6TxJaeg5hHEuk43VIJgTrlR/H6i0Us0 epBCEi++omr88sPxOiBBV0EBAomVkA6nCHwXN8JjBKtfjEUkaVp3awql5X9uVOzrOLXm E14iUECo0iguK0hkXleMsSMdqqK08IAi8A8OvKpooXQUG4MQPhkLBRkyfxJgLdiIwJOH U3ZA== X-Gm-Message-State: AO0yUKUV9GY8Q9VJpZlwO25c15F64S+++x3xAZr/L/ASChqPpNTmx5IO gNolHSJNLet9lnobjx8Md4N3TOv6ACQ= 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 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 7FEA4100012 X-Rspam-User: X-Stat-Signature: n6f495cz3rq5brnnzjgm1bx63dccuj1c X-HE-Tag: 1679138134-525763 X-HE-Meta: U2FsdGVkX1/BFGMSF/KNdVyp/J2DG7RMyc4FQtdqNe54bxQDNnPZxW0A/5s3q61j4kk9TMUjm71nuGfhomrmtE0Zr9W6VAmjwhgedIxw/wJSI4Op0IQfU53J4uMjSWTgAHr9x0idQjqRZAPUBHEh41E9zfp1wIp8Vu6cIxAzF6uZxp6Ta8+nUpYanSJsontKGlPAY9xjibSaZsV2YIfnMFa8PZJkhi9zvfVzR0HWSC7MAPuaXyyJT3npVws7emWd8cfcaCzzyZqJP7PdMNZdRTBvtM3Q0pLbzuSe1F86CbNW1y3HDDfQwgcYZ3kOuQxxaNMEOGbcKnKFiuX6GJZdJMDqbLKaptVzq4kEFYTo6yk9kGpuw5Zu2jRG5Dtu8Zv7o4zNdTf19IZizynakUKkjpI+XvcCY9vwm/oqFWD4ycDy+QG0iFVf6H5NNDO6ckbk0ovoBuVbzKEovFW67owlzEpKTrixVL1zJ2hDs/m2UuA6ermB5sMdU3Brg+iKtqEI4yaT6q8hwBmx/KIxV/pLwOTGxmFijYebVZpSJSeHFR09srqFy/gLuFlJy8v9SXWD8gVOLpP529WvBPqOYIDZUciBScjTtPPiA3S3jNquGk7SvEQU31PHMoigQiDBB0ETvO7L3oUzVL4RC8S5X9dcYdK4BFHUP8eO6xoNYZPJs9vBNMD51VV1rdXXaIZQz4oNzeAU7PLaBvZKQ773vu8mJkdZLet/ik51Bl+F22C67UFXFSKo1usUygFEFxt0rNQzWS+me0uRyc8YbVaADFD+wH895VcjhLmwKsu9NdoUaz7i8giqf/Q9Iuir9ydGyWS+wg4iPc+eb25OqIIXrq7pWqkUk1SG/ULPQpH8pZD9qyKgAmxxLut2/xQQ0PwC2TSxTlwZS2WX8WP/SEpWk496MFwlJhe4anKLNWIqf9DFn3+oviRYxaUuKp21EH4DAqFHZGJa/mjTdjPJ8Qa2Fln lbx5pHB/ TExznwdo5niuuyRJUTnfGfaR1piBt+4ZkE7XTO38ZfQtVFVRxcJZByoOgdQAAjH5iRwOFQAJBjjLdzyHW6hWb1HljEMnzCWdgIDMHk7LZjM98YaUvrMDw97Yp7uJDL4DlxfiWiWplfDzev0R7SvgbJ8bJZHghSdkdC+8LHR8hOH0BFTLMY5ByZzi055ZLCc7fOBlFoy2DoMmvXKM+6T9h/sBMcgOkNvl2cgkMo8wqqiOo6vXjQTq7vDQpIuxtKj5jTSoiPV/jwKKA0tmJiAbkh4YOoIiGjsxRw+5Wam9+Gij/nln37ErCV9J/OCyM/ZiPAu1LzKHk0FHZC7T3NAZC0hzuCR5IpNL06oGqm/CvOrvd5JoS0DrLEWetarzks3CKDDwuJDpLUdGbJdn4EpAuPr/UZyhPRbaXs7/zPOs/zb6HqQtdQB9gAQz0rbuqoU1XSjOYDKZw2aSwpu522HwS2w40zl1sFplA/BorBSkW/Gp3R//+qupqdf2eKygAU7IlRtEZ 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: Rather than setting err = -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 *vmi, 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 = -1; + int err = 0; bool merge_prev = false; bool merge_next = false; bool vma_expanded = false; @@ -1002,12 +1002,15 @@ struct vm_area_struct *vma_merge(struct vma_iterator *vmi, struct mm_struct *mm, } /* 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 = true; - } + merge_next = 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; remove = remove2 = adjust = 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 = dup_anon_vma(prev, curr); } } else if (merge_prev) { - err = 0; /* case 2 */ + /* case 2 */ if (curr) { err = dup_anon_vma(prev, curr); if (end == 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 = (end - curr->vm_start); } } - } else if (merge_next) { + } else { /* merge_next */ res = next; if (prev && addr < prev->vm_end) { /* case 4 */ vma_end = addr; @@ -1049,7 +1052,7 @@ struct vm_area_struct *vma_merge(struct vma_iterator *vmi, struct mm_struct *mm, vma_start = addr; vma_end = next->vm_end; vma_pgoff = next->vm_pgoff; - err = 0; + if (curr) { /* case 8 */ vma_pgoff = curr->vm_pgoff; remove = curr; @@ -1058,7 +1061,7 @@ struct vm_area_struct *vma_merge(struct vma_iterator *vmi, struct mm_struct *mm, } } - /* Cannot merge or error in anon_vma clone */ + /* Error in anon_vma clone. */ if (err) return NULL;