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+vjn3NzlYVyJCVvUlknYy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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; }