Received: by 2002:a05:6602:18e:0:0:0:0 with SMTP id m14csp2436424ioo; Sat, 28 May 2022 13:38:05 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwN2s+QJFOgomE5bb0RqrQeojnvjfFL78QU9098M8ekGOTBINTXZZwdWUuEvYpHO85H+YjC X-Received: by 2002:a17:90b:33d0:b0:1e0:4953:d848 with SMTP id lk16-20020a17090b33d000b001e04953d848mr15147313pjb.64.1653770285135; Sat, 28 May 2022 13:38:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1653770285; cv=none; d=google.com; s=arc-20160816; b=mYqMupYSlXJ56Ku+nIHUHj21+EknnZ2B9gqH2b7Aj+d1bLYBLKcG0d0zgm/m4Dl7Cw i7vVfTGLSEjHL/QldGugHByWqJdpdaWO5Cy9bi1yQm63b5QjpNdGzav6ATqdI5Z9WI7F xSVjysDuS/jXNw0/5xYkjYagQ1Vq7yzJoQ3oXc2sPZsxdebYkYx9WCJ1sdN5LWy+MUhR dSExyWcI1+N8EVGx6Vm4Xdr0h8+ZiSx9Kdu24eLj2HN/lsnySZO59Fc8JlzXMZANZRIa f8SW7ZIUL55X+Mgm+8PmwBzlehu2PdNTNqTEW7V+Mo+WuN4/M4F3ymrzxiyD51iuhGZg ciBg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=ru3bkI46cGQXTo5hSsyPFl2jKgUjs/czASVw2IToYVQ=; b=tjuANqh3wR5GquvB6g1JtraBGKpKeFvmzKHBW1R/IgvMkGC5R2x54pERUEBTa6MJva 9JtAfFsG7X1Lc7FKk0FW3BnkI/RYHPY0HdB5VXvjPothMBnhnaYWQimxuHpa9z61qAgp BLjvQyStA9+UnYvObq9b0TAeGlRFQDj2NfgQAxzk963KWtE8LHc/tsTbYa6Yl4W4nsGI qVm8F0Y+1hIQZE/e5XD16pOuyiKkACr49/R1TaSalDRtBe85pscGb+eTF3PCeHJh30M5 mEWgp30Shc0+Qyw+hYvYPyTGlTnehV++2/4DWh8k0nMn1ArRAdViNg1wtlAzek8Jm1Cz w3CA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=EKUNVPeM; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id b2-20020a170902b60200b00163915db48fsi7889622pls.304.2022.05.28.13.38.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 28 May 2022 13:38:05 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=EKUNVPeM; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id E971B3DDEB; Sat, 28 May 2022 12:40:15 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1354734AbiE0VQy (ORCPT + 99 others); Fri, 27 May 2022 17:16:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41502 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1354709AbiE0VQo (ORCPT ); Fri, 27 May 2022 17:16:44 -0400 Received: from mail-ej1-x62c.google.com (mail-ej1-x62c.google.com [IPv6:2a00:1450:4864:20::62c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4FBC1EE34 for ; Fri, 27 May 2022 14:16:43 -0700 (PDT) Received: by mail-ej1-x62c.google.com with SMTP id f21so10834935ejh.11 for ; Fri, 27 May 2022 14:16:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ru3bkI46cGQXTo5hSsyPFl2jKgUjs/czASVw2IToYVQ=; b=EKUNVPeMnRd274p6w7NzjRvrzG+1Vzb7NNsyThVrB39jtt3BmGdNB/FDIDQ8Je0Wkx 79yb+Ur+1BNH3EWYCuXMnj0n4tKugWeTV/s6bQcmOjS/M4BzSwrdWhCKYflx5cS6JWwZ 6DG2nqaH9HMKivoo/hcCev2L4qhr4MGxmXJuLLmrTD17/fsjm24k+7Z6PCvgZjDVyqex Bx2LBogNAW2x+xspwzeZH54qtfWtb71D/v7ScGfgxdAqBegWi/gd0KzBvqIG2+af8VY0 kcnb24+Fo8IxXa+Yi9DSTzWv7OhloaWyqJX/a0queK5D4HGlSoS2mC1nVepbi8KmB/pm bUFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ru3bkI46cGQXTo5hSsyPFl2jKgUjs/czASVw2IToYVQ=; b=E6HRJdst92PUBvwx2uuBnWmZ2iCQDUzujmPNtrnEglZ/ETPtZV4QTe/mHye/Cj0M24 BhnYwvl9L9wsE/36KA5dEgxEkOPfAhGacqGGTrSqIDnfLlyHw5vNA+qUTTcIzmtx6bvz UQPs26l64SdLggrPKJHzgi9y/xDqJYr4RiKdJ6Ps0QVBbLFrTL+sX7HM166ZUoypLSSs W/9jG8nDBhYIcLo4lRSFPxUz/0SteOFKHh56PZp8w3JFzRBVKVW2OFqhFqh8BVh7d/gW lgDOXUgaFa0yR+4a4i3Nup/hVFjPvsciE0MFfmQKzac5eXnzC7MDO0TkpmDeu0M5qKCv vB7w== X-Gm-Message-State: AOAM533SkXr8NcGzv6EGmGPlCn0VFIbVxGa5DhfGNibjn1/msmP7u7Pt EIVaotokOIh3y1rt8gpniAE= X-Received: by 2002:a17:907:3e09:b0:6ff:20f:9b1a with SMTP id hp9-20020a1709073e0900b006ff020f9b1amr19318212ejc.679.1653686201666; Fri, 27 May 2022 14:16:41 -0700 (PDT) Received: from orion.localdomain ([93.99.228.15]) by smtp.gmail.com with ESMTPSA id m22-20020a17090679d600b006f3ef214da9sm1814854ejo.15.2022.05.27.14.16.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 May 2022 14:16:40 -0700 (PDT) Received: by orion.localdomain (Postfix, from userid 1003) id 11BDFA0018; Fri, 27 May 2022 23:17:10 +0200 (CEST) From: =?UTF-8?q?Jakub=20Mat=C4=9Bna?= To: akpm@linux-foundation.org Cc: linux-mm@kvack.org, patches@lists.linux.dev, linux-kernel@vger.kernel.org, vbabka@suse.cz, mhocko@kernel.org, mgorman@techsingularity.net, willy@infradead.org, liam.howlett@oracle.com, hughd@google.com, kirill@shutemov.name, riel@surriel.com, rostedt@goodmis.org, peterz@infradead.org, =?UTF-8?q?Jakub=20Mat=C4=9Bna?= Subject: [PATCH v2 1/2] [PATCH 1/2] mm: refactor of vma_merge() Date: Fri, 27 May 2022 23:17:07 +0200 Message-Id: <20220527211708.839033-2-matenajakub@gmail.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220527211708.839033-1-matenajakub@gmail.com> References: <20220527211708.839033-1-matenajakub@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Refactor vma_merge() to make it shorter and more understandable. Main change is the elimination of code duplicity in the case of merge next check. This is done by first doing checks and caching the results before executing the merge itself. The variable 'area' is divided into 'mid' and 'res' as previously it was used for two purposes, as the middle VMA between prev and next and also as the result of the merge itself. Exit paths are also unified. Signed-off-by: Jakub Matěna --- mm/mmap.c | 87 +++++++++++++++++++++++-------------------------------- 1 file changed, 37 insertions(+), 50 deletions(-) diff --git a/mm/mmap.c b/mm/mmap.c index 3aa839f81e63..bd0dde2ad3e2 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1170,8 +1170,10 @@ struct vm_area_struct *vma_merge(struct mm_struct *mm, struct anon_vma_name *anon_name) { pgoff_t pglen = (end - addr) >> PAGE_SHIFT; - struct vm_area_struct *area, *next; - int err; + struct vm_area_struct *mid, *next, *res; + int err = -1; + bool merge_prev = false; + bool merge_next = false; /* * We later require that vma->vm_flags == vm_flags, @@ -1181,75 +1183,60 @@ struct vm_area_struct *vma_merge(struct mm_struct *mm, return NULL; next = vma_next(mm, prev); - area = next; - if (area && area->vm_end == end) /* cases 6, 7, 8 */ + mid = next; + if (next && next->vm_end == end) /* cases 6, 7, 8 */ next = next->vm_next; /* verify some invariant that must be enforced by the caller */ VM_WARN_ON(prev && addr <= prev->vm_start); - VM_WARN_ON(area && end > area->vm_end); + VM_WARN_ON(mid && end > mid->vm_end); VM_WARN_ON(addr >= end); - /* - * Can it merge with the predecessor? - */ + /* Can we merge the predecessor? */ if (prev && prev->vm_end == addr && mpol_equal(vma_policy(prev), policy) && can_vma_merge_after(prev, vm_flags, anon_vma, file, pgoff, vm_userfaultfd_ctx, anon_name)) { - /* - * OK, it can. Can we now merge in the successor as well? - */ - 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) && - is_mergeable_anon_vma(prev->anon_vma, - next->anon_vma, NULL)) { - /* cases 1, 6 */ - err = __vma_adjust(prev, prev->vm_start, - next->vm_end, prev->vm_pgoff, NULL, - prev); - } else /* cases 2, 5, 7 */ - err = __vma_adjust(prev, prev->vm_start, - end, prev->vm_pgoff, NULL, prev); - if (err) - return NULL; - khugepaged_enter_vma_merge(prev, vm_flags); - return prev; + merge_prev = true; } - - /* - * Can this new request be merged in front of next? - */ + /* 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)) { + merge_next = true; + } + /* 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)) { /* cases 1, 6 */ + err = __vma_adjust(prev, prev->vm_start, + next->vm_end, prev->vm_pgoff, NULL, + prev); + res = prev; + } else if (merge_prev) { /* cases 2, 5, 7 */ + err = __vma_adjust(prev, prev->vm_start, + end, prev->vm_pgoff, NULL, prev); + res = prev; + } else if (merge_next) { if (prev && addr < prev->vm_end) /* case 4 */ err = __vma_adjust(prev, prev->vm_start, - addr, prev->vm_pgoff, NULL, next); - else { /* cases 3, 8 */ - err = __vma_adjust(area, addr, next->vm_end, - next->vm_pgoff - pglen, NULL, next); - /* - * In case 3 area is already equal to next and - * this is a noop, but in case 8 "area" has - * been removed and next was expanded over it. - */ - area = next; - } - if (err) - return NULL; - khugepaged_enter_vma_merge(area, vm_flags); - return area; + addr, prev->vm_pgoff, NULL, next); + else /* cases 3, 8 */ + err = __vma_adjust(mid, addr, next->vm_end, + next->vm_pgoff - pglen, NULL, next); + res = next; } - return NULL; + /* + * Cannot merge with predecessor or successor or error in __vma_adjust? + */ + if (err) + return NULL; + khugepaged_enter_vma_merge(res, vm_flags); + return res; } /* -- 2.35.1