From patchwork Mon Oct 2 10:56:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yajun Deng X-Patchwork-Id: 147526 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2a8e:b0:403:3b70:6f57 with SMTP id in14csp1723520vqb; Mon, 2 Oct 2023 15:20:50 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGGMh7Jtj7kHgzzLb71zEWgJMqW9jw/tkjxUEpotKl2v86RTXNNh+fYuYUGr0ucOkM7+gfH X-Received: by 2002:a05:6a00:2394:b0:690:2ad9:1454 with SMTP id f20-20020a056a00239400b006902ad91454mr15497946pfc.33.1696285249978; Mon, 02 Oct 2023 15:20:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696285249; cv=none; d=google.com; s=arc-20160816; b=jlsP3X0tNiujg0S7+6pSGAw2l2HkfXdnDhFgiCMaGF9zbCesK4dXZ8sM/O916/jF7K CRM1elVLVSIEXrRcXNkfL3gmxDT5pc/7rigBIYHGUOqfJ69+UE+R+sr3Esm8yD4Cc+IU rVIeE2JH/olaO0cNDdeXzXUI+0Na+8+m5yMoU/Hr6sVfVSZQjrhYxBCRvwgQwEzgdg2N AOXWVPI9upR2Vh4Ret51FK8Vfhwgr6hHaoFj3IGvY1udtj/JoTiAevP6YzdFPbetga7t PsjJ0spm5TMlE4yBZVM/O9X2dlqABvlJyZxaU1HH7wjz+2uwzjqR2g5DeeTjlBsf3S0T uwyA== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=aQO3XkBHu5rCLD4ypD1Z6J4F2frqkYY0S4aXHFwX8tQ=; fh=QZU3guN9hBCNbG3XWCsoVTJE3Km/GRJW/frPYUuNAVA=; b=tJmUpTH5l65Bj74ACvCnogp1qGO/JSpjfIHSEqwaVK14VjVYe14LoZ4osJO4JNth/n uWU9t7bZ4iDR21estHCwu+1Wzd8b+wMVlBZarp9x97DBG03+FrZq+ZtOj9R3ScFX+dbq ebGMqLnu8Y0mYowLlr9GYWCSJ/yZ9popHsyShky5E0dYiVWZW5/HPYP0xufbS84c6166 Ti+yM6wPJZP22xpULFAHIs/DvYL6v2tkCW2m2PzXXS+DJXgMDd9kRUvE52awk/+S9h5Z bc1VKPtutyUY1KHgcc8osp17zBw6sZ6j1jWSJ4NDBCi5+mp3rHGKgFklUfoVaCgMph0P 7TGQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=cD0MgmhC; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id u69-20020a627948000000b0068740bcd8d7si27100606pfc.362.2023.10.02.15.20.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Oct 2023 15:20:49 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=cD0MgmhC; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 8A9E88065CA1; Mon, 2 Oct 2023 03:57:34 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236520AbjJBK5U (ORCPT + 16 others); Mon, 2 Oct 2023 06:57:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58236 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236192AbjJBK5R (ORCPT ); Mon, 2 Oct 2023 06:57:17 -0400 Received: from out-190.mta1.migadu.com (out-190.mta1.migadu.com [IPv6:2001:41d0:203:375::be]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B9CDFB3 for ; Mon, 2 Oct 2023 03:57:14 -0700 (PDT) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1696244232; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=aQO3XkBHu5rCLD4ypD1Z6J4F2frqkYY0S4aXHFwX8tQ=; b=cD0MgmhCw1APCHXI0U9MBlpClZ+i2x8Hr54sVTocyWd+KcZNOKeijukaBQD7nRrYt0Pfgi zAutlO6DcswAcmq+elAvPFY/YwlGgbOu9yI4m7vI4TyKveLfFlE9g0N28iarbEQG3keg+R ofWm4eWWchDain5ptATvfaKOijIdnw0= From: Yajun Deng To: rppt@kernel.org, akpm@linux-foundation.org Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Yajun Deng Subject: [PATCH v2] memblock: don't run loop in memblock_add_range() twice Date: Mon, 2 Oct 2023 18:56:52 +0800 Message-Id: <20231002105652.2514182-1-yajun.deng@linux.dev> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_PASS 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 X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Mon, 02 Oct 2023 03:57:34 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778155237270420071 X-GMAIL-MSGID: 1778684002002081083 There is round twice in memblock_add_range(). The first counts the number of regions needed to accommodate the new area. The second actually inserts them. But the first round isn't really needed, we just need to check the counts before inserting them. Check the count before iterate memblock. If the count is equal to the maximum, it needs to resize the array. Otherwise, insert it directly. After that, it's similar logic to memblock_isolate_range. Signed-off-by: Yajun Deng --- v2: remove the changes of memblock_double_array. v1: https://lore.kernel.org/all/20230927013752.2515238-1-yajun.deng@linux.dev/ --- mm/memblock.c | 75 +++++++++++++++++---------------------------------- 1 file changed, 24 insertions(+), 51 deletions(-) diff --git a/mm/memblock.c b/mm/memblock.c index 5a88d6d24d79..655d8e82f90a 100644 --- a/mm/memblock.c +++ b/mm/memblock.c @@ -588,11 +588,11 @@ static int __init_memblock memblock_add_range(struct memblock_type *type, phys_addr_t base, phys_addr_t size, int nid, enum memblock_flags flags) { - bool insert = false; phys_addr_t obase = base; phys_addr_t end = base + memblock_cap_size(base, &size); - int idx, nr_new, start_rgn = -1, end_rgn; + int idx, start_rgn = -1, end_rgn; struct memblock_region *rgn; + unsigned long ocnt = type->cnt; if (!size) return 0; @@ -609,23 +609,13 @@ static int __init_memblock memblock_add_range(struct memblock_type *type, } /* - * The worst case is when new range overlaps all existing regions, - * then we'll need type->cnt + 1 empty regions in @type. So if - * type->cnt * 2 + 1 is less than or equal to type->max, we know - * that there is enough empty regions in @type, and we can insert - * regions directly. + * If type->cnt is equal to type->max, it means there's + * not enough empty region and the array needs to be + * resized. Otherwise, insert it directly. */ - if (type->cnt * 2 + 1 <= type->max) - insert = true; - -repeat: - /* - * The following is executed twice. Once with %false @insert and - * then with %true. The first counts the number of regions needed - * to accommodate the new area. The second actually inserts them. - */ - base = obase; - nr_new = 0; + if ((type->cnt == type->max) && + memblock_double_array(type, obase, size)) + return -ENOMEM; for_each_memblock_type(idx, type, rgn) { phys_addr_t rbase = rgn->base; @@ -644,15 +634,13 @@ static int __init_memblock memblock_add_range(struct memblock_type *type, WARN_ON(nid != memblock_get_region_node(rgn)); #endif WARN_ON(flags != rgn->flags); - nr_new++; - if (insert) { - if (start_rgn == -1) - start_rgn = idx; - end_rgn = idx + 1; - memblock_insert_region(type, idx++, base, - rbase - base, nid, - flags); - } + + if (start_rgn == -1) + start_rgn = idx; + end_rgn = idx + 1; + memblock_insert_region(type, idx++, base, + rbase - base, nid, + flags); } /* area below @rend is dealt with, forget about it */ base = min(rend, end); @@ -660,33 +648,18 @@ static int __init_memblock memblock_add_range(struct memblock_type *type, /* insert the remaining portion */ if (base < end) { - nr_new++; - if (insert) { - if (start_rgn == -1) - start_rgn = idx; - end_rgn = idx + 1; - memblock_insert_region(type, idx, base, end - base, - nid, flags); - } - } - if (!nr_new) - return 0; + if (start_rgn == -1) + start_rgn = idx; + end_rgn = idx + 1; + memblock_insert_region(type, idx, base, end - base, + nid, flags); + } - /* - * If this was the first round, resize array and repeat for actual - * insertions; otherwise, merge and return. - */ - if (!insert) { - while (type->cnt + nr_new > type->max) - if (memblock_double_array(type, obase, size) < 0) - return -ENOMEM; - insert = true; - goto repeat; - } else { + if (ocnt != type->cnt) memblock_merge_regions(type, start_rgn, end_rgn); - return 0; - } + + return 0; } /**