Received: by 2002:a05:6602:18e:0:0:0:0 with SMTP id m14csp9033684ioo; Sun, 5 Jun 2022 03:34:17 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzg5fOI1/fD23TtDmenseS+nVoAhHQJ6tC5SPKDJ6Ew4jClC5vNanetWn2RZuYOjzxyp2HN X-Received: by 2002:a63:8749:0:b0:3fd:43ba:9166 with SMTP id i70-20020a638749000000b003fd43ba9166mr7990314pge.192.1654425257640; Sun, 05 Jun 2022 03:34:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654425257; cv=none; d=google.com; s=arc-20160816; b=Ppeu6ENbZheqdY57sbfJm1mt29Ozc6AbOXi7d4F/f544psggsrvuQIerp7fP2FlLR3 kjIWYkwFvFbLBDBIe5MKv76HIMSz2crYi/XkKSWzdMje7hGTuVrFXzm28fsOrDN4nCFx nOgYrTbU5DQUuM/WoImxUp6y/ii/rEmlC4BEGmfjXm+XaVSDmeStRoU6uAgCT0koe6WI MrowZLSDUCtEvyQEAkzFxt3qruneNcx+SriPc5n/BAepFlR+I2FUOWwGjDcUVSB8V3Ir eaf4/Tj7TKQj6Lh/C0POc2u2W3rcuMCME4izgB2wm9SrXgqNuvju6CpEKK4cAg19VMPj sr/Q== 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=xDGUdvx1pZTgyR7PJdBJDYNZdw+7CJ97LAoW275dhVs=; b=N7TqviEOnNv9FG8DSeQl5OKJgMoVPdQcXJaVGPFsj9f5dvimXg/YbZ6cq/RT/NoFLu MvYLyYH5amZfeg6bG8x0hHnRFBrr+oVmotMX/rQ/nNc3SdfGiq1FrMRew2k1yS74aFQL gNsqjh9+kkcXJi0bf27pKByuHUwmaUBMwcrhyqFxEucY+mZVYbbbBc+s4vnXeAahBWqX +nbMi0+qsYOdT9/81fw7bkWnZgj9BD+vptZptXrxxSFL5PHpaMaJkA/wz6Lb6BhPAAJF kqLsIKH7r8VTFRx2M4CygxvjxhO2hF5loSl7ZEw3w2tKIUYVHooOTPDn9HzHRWiZGWLx GS2Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=RSfJURm0; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ik14-20020a170902ab0e00b0015830f35108si15184599plb.209.2022.06.05.03.33.59; Sun, 05 Jun 2022 03:34:17 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=RSfJURm0; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245245AbiFCO5G (ORCPT + 99 others); Fri, 3 Jun 2022 10:57:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46202 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S245252AbiFCO4x (ORCPT ); Fri, 3 Jun 2022 10:56:53 -0400 Received: from mail-ej1-x631.google.com (mail-ej1-x631.google.com [IPv6:2a00:1450:4864:20::631]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C276AAE78 for ; Fri, 3 Jun 2022 07:56:50 -0700 (PDT) Received: by mail-ej1-x631.google.com with SMTP id s6so4864125eja.0 for ; Fri, 03 Jun 2022 07:56:50 -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=xDGUdvx1pZTgyR7PJdBJDYNZdw+7CJ97LAoW275dhVs=; b=RSfJURm0QkP/9Vq/fBoprT4aTS2dTIKLUzM/4tcnKiBXK0q++Pvc6+HW05p0AqIUYG BY0IZq8t7He8sycbNSRuJgs0AE4a0HsC552jBOp6reIGUO0fkfJ56KuGWF+D1Pme6BOP OKhRndSEHmQaxthqzxydfKYJv9CQXEjn7sm70oJNM8qD/J/8nYgw4Qy6y/DpkCnqkloS 4cxKR8gPKRa/0Km47eUPxu3aWehYH6JGgXxX9MHVwla+IGMRIzzUzW2qftJRSjOY9uY5 qKs6sLNbJCWBbmWAxINU/Qy2nP8+oVKt2GIBxkdUv44hb/FEtTGkohnG2sYsEm2Y16Y+ sWhw== 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=xDGUdvx1pZTgyR7PJdBJDYNZdw+7CJ97LAoW275dhVs=; b=PWGz34P15I9J1KQ9ykFuDqz2Hj/4WkrWZ/enVURc8Q18R0uWPqH/0WdvMqxRW/XWSV xR7SfmG5ZGpcCfit8vNtMSyYVy0A2NPWMSTReVlFkRuUBsR00dNnV+eCNsem/dWy17h3 LbBKVDU4ZXfNYYb36G2OWL6GsCuF7N5Aq3JPnw5keWWTmCpowkF2wOSOZDTBmm+oS/cZ IFSgWxzw/LB7+atqKZVkVCabjOIvbWNKwh9/fjFEz1t673qojony6jYOa2YvQCdZoJ+f Ef94ZjeQEW4VFCF2GC9+GoADy7xqktTfy2XzlBWxIiKbxfuhydcMI6CMQvtwGR6KDNl8 n07Q== X-Gm-Message-State: AOAM531tCgaVfHGL7ufDpPwXA0S8Rw473LWKFCM3BSX4Om6/LOpK2Q2v FuEjU9yo3Wv7eyDwlVRP/3Y= X-Received: by 2002:a17:907:3e18:b0:6fe:cf44:8692 with SMTP id hp24-20020a1709073e1800b006fecf448692mr9050521ejc.601.1654268209258; Fri, 03 Jun 2022 07:56:49 -0700 (PDT) Received: from orion.localdomain ([93.99.228.15]) by smtp.gmail.com with ESMTPSA id k9-20020a1709061c0900b006fec27575f1sm2890627ejg.123.2022.06.03.07.56.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Jun 2022 07:56:46 -0700 (PDT) Received: by orion.localdomain (Postfix, from userid 1003) id ABBE8A0013; Fri, 3 Jun 2022 16:57:20 +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 v4 1/2] [PATCH 1/2] mm: refactor of vma_merge() Date: Fri, 3 Jun 2022 16:57:18 +0200 Message-Id: <20220603145719.1012094-2-matenajakub@gmail.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220603145719.1012094-1-matenajakub@gmail.com> References: <20220603145719.1012094-1-matenajakub@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham 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 313b57d55a63..91100fdc400a 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 = find_vma(mm, prev ? prev->vm_end : 0); - 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 = find_vma(mm, next->vm_end); /* 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(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(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(res, vm_flags); + return res; } /* -- 2.35.1